FreeBSD/Linux Kernel Cross Reference
sys/cam/cam.c
1 /*-
2 * Generic utility routines for the Common Access Method layer.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 1997 Justin T. Gibbs.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #ifdef _KERNEL
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #else /* _KERNEL */
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <camlib.h>
44 #endif /* _KERNEL */
45
46 #include <cam/cam.h>
47 #include <cam/cam_ccb.h>
48 #include <cam/scsi/scsi_all.h>
49 #include <cam/scsi/smp_all.h>
50 #include <sys/sbuf.h>
51
52 #ifdef _KERNEL
53 #include <sys/libkern.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_xpt.h>
56
57 FEATURE(scbus, "SCSI devices support");
58
59 #endif
60
61 static int camstatusentrycomp(const void *key, const void *member);
62
63 const struct cam_status_entry cam_status_table[] = {
64 { CAM_REQ_INPROG, "CCB request is in progress" },
65 { CAM_REQ_CMP, "CCB request completed without error" },
66 { CAM_REQ_ABORTED, "CCB request aborted by the host" },
67 { CAM_UA_ABORT, "Unable to abort CCB request" },
68 { CAM_REQ_CMP_ERR, "CCB request completed with an error" },
69 { CAM_BUSY, "CAM subsystem is busy" },
70 { CAM_REQ_INVALID, "CCB request was invalid" },
71 { CAM_PATH_INVALID, "Supplied Path ID is invalid" },
72 { CAM_DEV_NOT_THERE, "Device Not Present" },
73 { CAM_UA_TERMIO, "Unable to terminate I/O CCB request" },
74 { CAM_SEL_TIMEOUT, "Selection Timeout" },
75 { CAM_CMD_TIMEOUT, "Command timeout" },
76 { CAM_SCSI_STATUS_ERROR, "SCSI Status Error" },
77 { CAM_MSG_REJECT_REC, "Message Reject Reveived" },
78 { CAM_SCSI_BUS_RESET, "SCSI Bus Reset Sent/Received" },
79 { CAM_UNCOR_PARITY, "Uncorrectable parity/CRC error" },
80 { CAM_AUTOSENSE_FAIL, "Auto-Sense Retrieval Failed" },
81 { CAM_NO_HBA, "No HBA Detected" },
82 { CAM_DATA_RUN_ERR, "Data Overrun error" },
83 { CAM_UNEXP_BUSFREE, "Unexpected Bus Free" },
84 { CAM_SEQUENCE_FAIL, "Target Bus Phase Sequence Failure" },
85 { CAM_CCB_LEN_ERR, "CCB length supplied is inadequate" },
86 { CAM_PROVIDE_FAIL, "Unable to provide requested capability" },
87 { CAM_BDR_SENT, "SCSI BDR Message Sent" },
88 { CAM_REQ_TERMIO, "CCB request terminated by the host" },
89 { CAM_UNREC_HBA_ERROR, "Unrecoverable Host Bus Adapter Error" },
90 { CAM_REQ_TOO_BIG, "The request was too large for this host" },
91 { CAM_REQUEUE_REQ, "Unconditionally Re-queue Request", },
92 { CAM_ATA_STATUS_ERROR, "ATA Status Error" },
93 { CAM_SCSI_IT_NEXUS_LOST,"Initiator/Target Nexus Lost" },
94 { CAM_SMP_STATUS_ERROR, "SMP Status Error" },
95 { CAM_IDE, "Initiator Detected Error Message Received" },
96 { CAM_RESRC_UNAVAIL, "Resource Unavailable" },
97 { CAM_UNACKED_EVENT, "Unacknowledged Event by Host" },
98 { CAM_MESSAGE_RECV, "Message Received in Host Target Mode" },
99 { CAM_INVALID_CDB, "Invalid CDB received in Host Target Mode" },
100 { CAM_LUN_INVALID, "Invalid Lun" },
101 { CAM_TID_INVALID, "Invalid Target ID" },
102 { CAM_FUNC_NOTAVAIL, "Function Not Available" },
103 { CAM_NO_NEXUS, "Nexus Not Established" },
104 { CAM_IID_INVALID, "Invalid Initiator ID" },
105 { CAM_CDB_RECVD, "CDB Received" },
106 { CAM_LUN_ALRDY_ENA, "LUN Already Enabled for Target Mode" },
107 { CAM_SCSI_BUSY, "SCSI Bus Busy" },
108 };
109
110 #ifdef _KERNEL
111 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
112
113 #ifndef CAM_DEFAULT_SORT_IO_QUEUES
114 #define CAM_DEFAULT_SORT_IO_QUEUES 1
115 #endif
116
117 int cam_sort_io_queues = CAM_DEFAULT_SORT_IO_QUEUES;
118 SYSCTL_INT(_kern_cam, OID_AUTO, sort_io_queues, CTLFLAG_RWTUN,
119 &cam_sort_io_queues, 0, "Sort IO queues to try and optimise disk access patterns");
120 #endif
121
122 void
123 cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
124 {
125 cam_strvis_flag(dst, src, srclen, dstlen,
126 CAM_STRVIS_FLAG_NONASCII_ESC);
127 }
128
129 void
130 cam_strvis_flag(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen,
131 uint32_t flags)
132 {
133 struct sbuf sb;
134
135 sbuf_new(&sb, dst, dstlen, SBUF_FIXEDLEN);
136 cam_strvis_sbuf(&sb, src, srclen, flags);
137 sbuf_finish(&sb);
138 }
139
140 void
141 cam_strvis_sbuf(struct sbuf *sb, const u_int8_t *src, int srclen,
142 uint32_t flags)
143 {
144
145 /* Trim leading/trailing spaces, nulls. */
146 while (srclen > 0 && src[0] == ' ')
147 src++, srclen--;
148 while (srclen > 0
149 && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
150 srclen--;
151
152 while (srclen > 0) {
153 if (*src < 0x20 || *src >= 0x80) {
154 /* SCSI-II Specifies that these should never occur. */
155 /* non-printable character */
156 switch (flags & CAM_STRVIS_FLAG_NONASCII_MASK) {
157 case CAM_STRVIS_FLAG_NONASCII_ESC:
158 sbuf_printf(sb, "\\%c%c%c",
159 ((*src & 0300) >> 6) + '',
160 ((*src & 0070) >> 3) + '',
161 ((*src & 0007) >> 0) + '');
162 break;
163 case CAM_STRVIS_FLAG_NONASCII_RAW:
164 /*
165 * If we run into a NUL, just transform it
166 * into a space.
167 */
168 if (*src != 0x00)
169 sbuf_putc(sb, *src);
170 else
171 sbuf_putc(sb, ' ');
172 break;
173 case CAM_STRVIS_FLAG_NONASCII_SPC:
174 sbuf_putc(sb, ' ');
175 break;
176 case CAM_STRVIS_FLAG_NONASCII_TRIM:
177 default:
178 break;
179 }
180 } else {
181 /* normal character */
182 sbuf_putc(sb, *src);
183 }
184 src++;
185 srclen--;
186 }
187 }
188
189
190 /*
191 * Compare string with pattern, returning 0 on match.
192 * Short pattern matches trailing blanks in name,
193 * Shell globbing rules apply: * matches 0 or more characters,
194 * ? matchces one character, [...] denotes a set to match one char,
195 * [^...] denotes a complimented set to match one character.
196 * Spaces in str used to match anything in the pattern string
197 * but was removed because it's a bug. No current patterns require
198 * it, as far as I know, but it's impossible to know what drives
199 * returned.
200 *
201 * Each '*' generates recursion, so keep the number of * in check.
202 */
203 int
204 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
205 {
206
207 while (*pattern != '\0' && str_len > 0) {
208 if (*pattern == '*') {
209 pattern++;
210 if (*pattern == '\0')
211 return (0);
212 do {
213 if (cam_strmatch(str, pattern, str_len) == 0)
214 return (0);
215 str++;
216 str_len--;
217 } while (str_len > 0);
218 return (1);
219 } else if (*pattern == '[') {
220 int negate_range, ok;
221 uint8_t pc = UCHAR_MAX;
222 uint8_t sc;
223
224 ok = 0;
225 sc = *str++;
226 str_len--;
227 pattern++;
228 if ((negate_range = (*pattern == '^')) != 0)
229 pattern++;
230 while ((*pattern != ']') && *pattern != '\0') {
231 if (*pattern == '-') {
232 if (pattern[1] == '\0') /* Bad pattern */
233 return (1);
234 if (sc >= pc && sc <= pattern[1])
235 ok = 1;
236 pattern++;
237 } else if (*pattern == sc)
238 ok = 1;
239 pc = *pattern;
240 pattern++;
241 }
242 if (ok == negate_range)
243 return (1);
244 pattern++;
245 } else if (*pattern == '?') {
246 /*
247 * NB: || *str == ' ' of the old code is a bug and was
248 * removed. If you add it back, keep this the last if
249 * before the naked else */
250 pattern++;
251 str++;
252 str_len--;
253 } else {
254 if (*str != *pattern)
255 return (1);
256 pattern++;
257 str++;
258 str_len--;
259 }
260 }
261
262 /* '*' is allowed to match nothing, so gobble it */
263 while (*pattern == '*')
264 pattern++;
265
266 if ( *pattern != '\0') {
267 /* Pattern not fully consumed. Not a match */
268 return (1);
269 }
270
271 /* Eat trailing spaces, which get added by SAT */
272 while (str_len > 0 && *str == ' ') {
273 str++;
274 str_len--;
275 }
276
277 return (str_len);
278 }
279
280 caddr_t
281 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
282 int entry_size, cam_quirkmatch_t *comp_func)
283 {
284 for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
285 if ((*comp_func)(target, quirk_table) == 0)
286 return (quirk_table);
287 }
288 return (NULL);
289 }
290
291 const struct cam_status_entry*
292 cam_fetch_status_entry(cam_status status)
293 {
294 status &= CAM_STATUS_MASK;
295 return (bsearch(&status, &cam_status_table,
296 nitems(cam_status_table),
297 sizeof(*cam_status_table),
298 camstatusentrycomp));
299 }
300
301 static int
302 camstatusentrycomp(const void *key, const void *member)
303 {
304 cam_status status;
305 const struct cam_status_entry *table_entry;
306
307 status = *(const cam_status *)key;
308 table_entry = (const struct cam_status_entry *)member;
309
310 return (status - table_entry->status_code);
311 }
312
313
314 #ifdef _KERNEL
315 char *
316 cam_error_string(union ccb *ccb, char *str, int str_len,
317 cam_error_string_flags flags,
318 cam_error_proto_flags proto_flags)
319 #else /* !_KERNEL */
320 char *
321 cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
322 int str_len, cam_error_string_flags flags,
323 cam_error_proto_flags proto_flags)
324 #endif /* _KERNEL/!_KERNEL */
325 {
326 char path_str[64];
327 struct sbuf sb;
328
329 if ((ccb == NULL)
330 || (str == NULL)
331 || (str_len <= 0))
332 return(NULL);
333
334 if (flags == CAM_ESF_NONE)
335 return(NULL);
336
337 switch (ccb->ccb_h.func_code) {
338 case XPT_ATA_IO:
339 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
340 case CAM_EPF_NONE:
341 break;
342 case CAM_EPF_ALL:
343 case CAM_EPF_NORMAL:
344 proto_flags |= CAM_EAF_PRINT_RESULT;
345 /* FALLTHROUGH */
346 case CAM_EPF_MINIMAL:
347 proto_flags |= CAM_EAF_PRINT_STATUS;
348 /* FALLTHROUGH */
349 default:
350 break;
351 }
352 break;
353 case XPT_SCSI_IO:
354 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
355 case CAM_EPF_NONE:
356 break;
357 case CAM_EPF_ALL:
358 case CAM_EPF_NORMAL:
359 proto_flags |= CAM_ESF_PRINT_SENSE;
360 /* FALLTHROUGH */
361 case CAM_EPF_MINIMAL:
362 proto_flags |= CAM_ESF_PRINT_STATUS;
363 /* FALLTHROUGH */
364 default:
365 break;
366 }
367 break;
368 case XPT_SMP_IO:
369 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
370 case CAM_EPF_NONE:
371 break;
372 case CAM_EPF_ALL:
373 proto_flags |= CAM_ESMF_PRINT_FULL_CMD;
374 /* FALLTHROUGH */
375 case CAM_EPF_NORMAL:
376 case CAM_EPF_MINIMAL:
377 proto_flags |= CAM_ESMF_PRINT_STATUS;
378 /* FALLTHROUGH */
379 default:
380 break;
381 }
382 break;
383 default:
384 break;
385 }
386 #ifdef _KERNEL
387 xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
388 #else /* !_KERNEL */
389 cam_path_string(device, path_str, sizeof(path_str));
390 #endif /* _KERNEL/!_KERNEL */
391
392 sbuf_new(&sb, str, str_len, 0);
393
394 if (flags & CAM_ESF_COMMAND) {
395 sbuf_cat(&sb, path_str);
396 switch (ccb->ccb_h.func_code) {
397 case XPT_ATA_IO:
398 ata_command_sbuf(&ccb->ataio, &sb);
399 break;
400 case XPT_SCSI_IO:
401 #ifdef _KERNEL
402 scsi_command_string(&ccb->csio, &sb);
403 #else /* !_KERNEL */
404 scsi_command_string(device, &ccb->csio, &sb);
405 #endif /* _KERNEL/!_KERNEL */
406 break;
407 case XPT_SMP_IO:
408 smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 -
409 strlen(path_str), (proto_flags &
410 CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0);
411 break;
412 case XPT_NVME_IO:
413 case XPT_NVME_ADMIN:
414 nvme_command_sbuf(&ccb->nvmeio, &sb);
415 break;
416 default:
417 sbuf_printf(&sb, "CAM func %#x",
418 ccb->ccb_h.func_code);
419 break;
420 }
421 sbuf_printf(&sb, "\n");
422 }
423
424 if (flags & CAM_ESF_CAM_STATUS) {
425 cam_status status;
426 const struct cam_status_entry *entry;
427
428 sbuf_cat(&sb, path_str);
429
430 status = ccb->ccb_h.status & CAM_STATUS_MASK;
431
432 entry = cam_fetch_status_entry(status);
433
434 if (entry == NULL)
435 sbuf_printf(&sb, "CAM status: Unknown (%#x)\n",
436 ccb->ccb_h.status);
437 else
438 sbuf_printf(&sb, "CAM status: %s\n",
439 entry->status_text);
440 }
441
442 if (flags & CAM_ESF_PROTO_STATUS) {
443
444 switch (ccb->ccb_h.func_code) {
445 case XPT_ATA_IO:
446 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
447 CAM_ATA_STATUS_ERROR)
448 break;
449 if (proto_flags & CAM_EAF_PRINT_STATUS) {
450 sbuf_cat(&sb, path_str);
451 ata_status_sbuf(&ccb->ataio, &sb);
452 sbuf_printf(&sb, "\n");
453 }
454 if (proto_flags & CAM_EAF_PRINT_RESULT) {
455 sbuf_cat(&sb, path_str);
456 sbuf_printf(&sb, "RES: ");
457 ata_res_sbuf(&ccb->ataio.res, &sb);
458 sbuf_printf(&sb, "\n");
459 }
460
461 break;
462 case XPT_SCSI_IO:
463 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
464 CAM_SCSI_STATUS_ERROR)
465 break;
466
467 if (proto_flags & CAM_ESF_PRINT_STATUS) {
468 sbuf_cat(&sb, path_str);
469 sbuf_printf(&sb, "SCSI status: %s\n",
470 scsi_status_string(&ccb->csio));
471 }
472
473 if ((proto_flags & CAM_ESF_PRINT_SENSE)
474 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
475 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {
476
477 #ifdef _KERNEL
478 scsi_sense_sbuf(&ccb->csio, &sb,
479 SSS_FLAG_NONE);
480 #else /* !_KERNEL */
481 scsi_sense_sbuf(device, &ccb->csio, &sb,
482 SSS_FLAG_NONE);
483 #endif /* _KERNEL/!_KERNEL */
484 }
485 break;
486 case XPT_SMP_IO:
487 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
488 CAM_SMP_STATUS_ERROR)
489 break;
490
491 if (proto_flags & CAM_ESF_PRINT_STATUS) {
492 sbuf_cat(&sb, path_str);
493 sbuf_printf(&sb, "SMP status: %s (%#x)\n",
494 smp_error_desc(ccb->smpio.smp_response[2]),
495 ccb->smpio.smp_response[2]);
496 }
497 /* There is no SMP equivalent to SCSI sense. */
498 break;
499 default:
500 break;
501 }
502 }
503
504 sbuf_finish(&sb);
505
506 return(sbuf_data(&sb));
507 }
508
509 #ifdef _KERNEL
510
511 void
512 cam_error_print(union ccb *ccb, cam_error_string_flags flags,
513 cam_error_proto_flags proto_flags)
514 {
515 char str[512];
516
517 printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
518 proto_flags));
519 }
520
521 #else /* !_KERNEL */
522
523 void
524 cam_error_print(struct cam_device *device, union ccb *ccb,
525 cam_error_string_flags flags, cam_error_proto_flags proto_flags,
526 FILE *ofile)
527 {
528 char str[512];
529
530 if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
531 return;
532
533 fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
534 flags, proto_flags));
535 }
536
537 #endif /* _KERNEL/!_KERNEL */
538
539 /*
540 * Common calculate geometry fuction
541 *
542 * Caller should set ccg->volume_size and block_size.
543 * The extended parameter should be zero if extended translation
544 * should not be used.
545 */
546 void
547 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
548 {
549 uint32_t size_mb, secs_per_cylinder;
550
551 if (ccg->block_size == 0) {
552 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
553 return;
554 }
555 size_mb = (1024L * 1024L) / ccg->block_size;
556 if (size_mb == 0) {
557 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
558 return;
559 }
560 size_mb = ccg->volume_size / size_mb;
561 if (size_mb > 1024 && extended) {
562 ccg->heads = 255;
563 ccg->secs_per_track = 63;
564 } else {
565 ccg->heads = 64;
566 ccg->secs_per_track = 32;
567 }
568 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
569 if (secs_per_cylinder == 0) {
570 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
571 return;
572 }
573 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
574 ccg->ccb_h.status = CAM_REQ_CMP;
575 }
Cache object: e194516d70002b010f9d60fc6fe078c2
|