2 * Generic utility routines for the Common Access Method layer.
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 1997 Justin T. Gibbs.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/memdesc.h>
36 #include <sys/sysctl.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/smp_all.h>
51 #include <sys/libkern.h>
52 #include <machine/bus.h>
53 #include <cam/cam_queue.h>
54 #include <cam/cam_xpt.h>
56 FEATURE(scbus, "SCSI devices support");
60 static int camstatusentrycomp(const void *key, const void *member);
62 const struct cam_status_entry cam_status_table[] = {
63 { CAM_REQ_INPROG, "CCB request is in progress" },
64 { CAM_REQ_CMP, "CCB request completed without error" },
65 { CAM_REQ_ABORTED, "CCB request aborted by the host" },
66 { CAM_UA_ABORT, "Unable to abort CCB request" },
67 { CAM_REQ_CMP_ERR, "CCB request completed with an error" },
68 { CAM_BUSY, "CAM subsystem is busy" },
69 { CAM_REQ_INVALID, "CCB request was invalid" },
70 { CAM_PATH_INVALID, "Supplied Path ID is invalid" },
71 { CAM_DEV_NOT_THERE, "Device Not Present" },
72 { CAM_UA_TERMIO, "Unable to terminate I/O CCB request" },
73 { CAM_SEL_TIMEOUT, "Selection Timeout" },
74 { CAM_CMD_TIMEOUT, "Command timeout" },
75 { CAM_SCSI_STATUS_ERROR, "SCSI Status Error" },
76 { CAM_MSG_REJECT_REC, "Message Reject Reveived" },
77 { CAM_SCSI_BUS_RESET, "SCSI Bus Reset Sent/Received" },
78 { CAM_UNCOR_PARITY, "Uncorrectable parity/CRC error" },
79 { CAM_AUTOSENSE_FAIL, "Auto-Sense Retrieval Failed" },
80 { CAM_NO_HBA, "No HBA Detected" },
81 { CAM_DATA_RUN_ERR, "Data Overrun error" },
82 { CAM_UNEXP_BUSFREE, "Unexpected Bus Free" },
83 { CAM_SEQUENCE_FAIL, "Target Bus Phase Sequence Failure" },
84 { CAM_CCB_LEN_ERR, "CCB length supplied is inadequate" },
85 { CAM_PROVIDE_FAIL, "Unable to provide requested capability" },
86 { CAM_BDR_SENT, "SCSI BDR Message Sent" },
87 { CAM_REQ_TERMIO, "CCB request terminated by the host" },
88 { CAM_UNREC_HBA_ERROR, "Unrecoverable Host Bus Adapter Error" },
89 { CAM_REQ_TOO_BIG, "The request was too large for this host" },
90 { CAM_REQUEUE_REQ, "Unconditionally Re-queue Request" },
91 { CAM_ATA_STATUS_ERROR, "ATA Status Error" },
92 { CAM_SCSI_IT_NEXUS_LOST,"Initiator/Target Nexus Lost" },
93 { CAM_SMP_STATUS_ERROR, "SMP Status Error" },
94 { CAM_REQ_SOFTTIMEOUT, "Completed w/o error, but took too long" },
95 { CAM_NVME_STATUS_ERROR, "NVME Status Error" },
96 { CAM_IDE, "Initiator Detected Error Message Received" },
97 { CAM_RESRC_UNAVAIL, "Resource Unavailable" },
98 { CAM_UNACKED_EVENT, "Unacknowledged Event by Host" },
99 { CAM_MESSAGE_RECV, "Message Received in Host Target Mode" },
100 { CAM_INVALID_CDB, "Invalid CDB received in Host Target Mode" },
101 { CAM_LUN_INVALID, "Invalid Lun" },
102 { CAM_TID_INVALID, "Invalid Target ID" },
103 { CAM_FUNC_NOTAVAIL, "Function Not Available" },
104 { CAM_NO_NEXUS, "Nexus Not Established" },
105 { CAM_IID_INVALID, "Invalid Initiator ID" },
106 { CAM_CDB_RECVD, "CDB Received" },
107 { CAM_LUN_ALRDY_ENA, "LUN Already Enabled for Target Mode" },
108 { CAM_SCSI_BUSY, "SCSI Bus Busy" },
112 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
115 #ifndef CAM_DEFAULT_SORT_IO_QUEUES
116 #define CAM_DEFAULT_SORT_IO_QUEUES 1
119 int cam_sort_io_queues = CAM_DEFAULT_SORT_IO_QUEUES;
120 SYSCTL_INT(_kern_cam, OID_AUTO, sort_io_queues, CTLFLAG_RWTUN,
121 &cam_sort_io_queues, 0, "Sort IO queues to try and optimise disk access patterns");
125 cam_strvis(uint8_t *dst, const uint8_t *src, int srclen, int dstlen)
127 cam_strvis_flag(dst, src, srclen, dstlen,
128 CAM_STRVIS_FLAG_NONASCII_ESC);
132 cam_strvis_flag(uint8_t *dst, const uint8_t *src, int srclen, int dstlen,
137 sbuf_new(&sb, dst, dstlen, SBUF_FIXEDLEN);
138 cam_strvis_sbuf(&sb, src, srclen, flags);
143 cam_strvis_sbuf(struct sbuf *sb, const uint8_t *src, int srclen,
147 /* Trim leading/trailing spaces, nulls. */
148 while (srclen > 0 && src[0] == ' ')
151 && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
155 if (*src < 0x20 || *src >= 0x80) {
156 /* SCSI-II Specifies that these should never occur. */
157 /* non-printable character */
158 switch (flags & CAM_STRVIS_FLAG_NONASCII_MASK) {
159 case CAM_STRVIS_FLAG_NONASCII_ESC:
160 sbuf_printf(sb, "\\%c%c%c",
161 ((*src & 0300) >> 6) + '0',
162 ((*src & 0070) >> 3) + '0',
163 ((*src & 0007) >> 0) + '0');
165 case CAM_STRVIS_FLAG_NONASCII_RAW:
167 * If we run into a NUL, just transform it
175 case CAM_STRVIS_FLAG_NONASCII_SPC:
178 case CAM_STRVIS_FLAG_NONASCII_TRIM:
183 /* normal character */
192 * Compare string with pattern, returning 0 on match.
193 * Short pattern matches trailing blanks in name,
194 * Shell globbing rules apply: * matches 0 or more characters,
195 * ? matchces one character, [...] denotes a set to match one char,
196 * [^...] denotes a complimented set to match one character.
197 * Spaces in str used to match anything in the pattern string
198 * but was removed because it's a bug. No current patterns require
199 * it, as far as I know, but it's impossible to know what drives
202 * Each '*' generates recursion, so keep the number of * in check.
205 cam_strmatch(const uint8_t *str, const uint8_t *pattern, int str_len)
208 while (*pattern != '\0' && str_len > 0) {
209 if (*pattern == '*') {
211 if (*pattern == '\0')
214 if (cam_strmatch(str, pattern, str_len) == 0)
218 } while (str_len > 0);
220 } else if (*pattern == '[') {
221 int negate_range, ok;
222 uint8_t pc = UCHAR_MAX;
229 if ((negate_range = (*pattern == '^')) != 0)
231 while ((*pattern != ']') && *pattern != '\0') {
232 if (*pattern == '-') {
233 if (pattern[1] == '\0') /* Bad pattern */
235 if (sc >= pc && sc <= pattern[1])
238 } else if (*pattern == sc)
243 if (ok == negate_range)
246 } else if (*pattern == '?') {
248 * NB: || *str == ' ' of the old code is a bug and was
249 * removed. If you add it back, keep this the last if
250 * before the naked else */
255 if (*str != *pattern)
263 /* '*' is allowed to match nothing, so gobble it */
264 while (*pattern == '*')
267 if ( *pattern != '\0') {
268 /* Pattern not fully consumed. Not a match */
272 /* Eat trailing spaces, which get added by SAT */
273 while (str_len > 0 && *str == ' ') {
282 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
283 int entry_size, cam_quirkmatch_t *comp_func)
285 for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
286 if ((*comp_func)(target, quirk_table) == 0)
287 return (quirk_table);
292 const struct cam_status_entry*
293 cam_fetch_status_entry(cam_status status)
295 status &= CAM_STATUS_MASK;
296 return (bsearch(&status, &cam_status_table,
297 nitems(cam_status_table),
298 sizeof(*cam_status_table),
299 camstatusentrycomp));
303 camstatusentrycomp(const void *key, const void *member)
306 const struct cam_status_entry *table_entry;
308 status = *(const cam_status *)key;
309 table_entry = (const struct cam_status_entry *)member;
311 return (status - table_entry->status_code);
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)
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 */
334 if (flags == CAM_ESF_NONE)
337 switch (ccb->ccb_h.func_code) {
339 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
344 proto_flags |= CAM_EAF_PRINT_RESULT;
346 case CAM_EPF_MINIMAL:
347 proto_flags |= CAM_EAF_PRINT_STATUS;
354 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
359 proto_flags |= CAM_ESF_PRINT_SENSE;
361 case CAM_EPF_MINIMAL:
362 proto_flags |= CAM_ESF_PRINT_STATUS;
369 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
373 proto_flags |= CAM_ESMF_PRINT_FULL_CMD;
376 case CAM_EPF_MINIMAL:
377 proto_flags |= CAM_ESMF_PRINT_STATUS;
387 xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
389 cam_path_string(device, path_str, sizeof(path_str));
390 #endif /* _KERNEL/!_KERNEL */
392 sbuf_new(&sb, str, str_len, 0);
394 if (flags & CAM_ESF_COMMAND) {
395 sbuf_cat(&sb, path_str);
396 switch (ccb->ccb_h.func_code) {
398 ata_command_sbuf(&ccb->ataio, &sb);
402 scsi_command_string(&ccb->csio, &sb);
404 scsi_command_string(device, &ccb->csio, &sb);
405 #endif /* _KERNEL/!_KERNEL */
408 smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 -
409 strlen(path_str), (proto_flags &
410 CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0);
414 nvme_command_sbuf(&ccb->nvmeio, &sb);
417 sbuf_printf(&sb, "CAM func %#x",
418 ccb->ccb_h.func_code);
421 sbuf_putc(&sb, '\n');
424 if (flags & CAM_ESF_CAM_STATUS) {
426 const struct cam_status_entry *entry;
428 sbuf_cat(&sb, path_str);
430 status = ccb->ccb_h.status & CAM_STATUS_MASK;
432 entry = cam_fetch_status_entry(status);
435 sbuf_printf(&sb, "CAM status: Unknown (%#x)\n",
438 sbuf_printf(&sb, "CAM status: %s\n",
442 if (flags & CAM_ESF_PROTO_STATUS) {
444 switch (ccb->ccb_h.func_code) {
446 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
447 CAM_ATA_STATUS_ERROR)
449 if (proto_flags & CAM_EAF_PRINT_STATUS) {
450 sbuf_cat(&sb, path_str);
451 ata_status_sbuf(&ccb->ataio, &sb);
452 sbuf_putc(&sb, '\n');
454 if (proto_flags & CAM_EAF_PRINT_RESULT) {
455 sbuf_cat(&sb, path_str);
456 sbuf_cat(&sb, "RES: ");
457 ata_res_sbuf(&ccb->ataio.res, &sb);
458 sbuf_putc(&sb, '\n');
463 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
464 CAM_SCSI_STATUS_ERROR)
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));
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)) {
477 scsi_sense_sbuf(&ccb->csio, &sb,
480 scsi_sense_sbuf(device, &ccb->csio, &sb,
482 #endif /* _KERNEL/!_KERNEL */
486 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
487 CAM_SMP_STATUS_ERROR)
490 if (proto_flags & CAM_ESF_PRINT_STATUS) {
491 sbuf_cat(&sb, path_str);
492 sbuf_printf(&sb, "SMP status: %s (%#x)\n",
493 smp_error_desc(ccb->smpio.smp_response[2]),
494 ccb->smpio.smp_response[2]);
496 /* There is no SMP equivalent to SCSI sense. */
505 return(sbuf_data(&sb));
511 cam_error_print(union ccb *ccb, cam_error_string_flags flags,
512 cam_error_proto_flags proto_flags)
516 printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
523 cam_error_print(struct cam_device *device, union ccb *ccb,
524 cam_error_string_flags flags, cam_error_proto_flags proto_flags,
529 if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
532 fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
533 flags, proto_flags));
536 #endif /* _KERNEL/!_KERNEL */
539 * Common calculate geometry fuction
541 * Caller should set ccg->volume_size and block_size.
542 * The extended parameter should be zero if extended translation
543 * should not be used.
546 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
548 uint32_t size_mb, secs_per_cylinder;
550 if (ccg->block_size == 0) {
551 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
554 size_mb = (1024L * 1024L) / ccg->block_size;
556 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
559 size_mb = ccg->volume_size / size_mb;
560 if (size_mb > 1024 && extended) {
562 ccg->secs_per_track = 63;
565 ccg->secs_per_track = 32;
567 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
568 if (secs_per_cylinder == 0) {
569 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
572 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
573 ccg->ccb_h.status = CAM_REQ_CMP;
578 memdesc_ccb(union ccb *ccb)
580 struct ccb_hdr *ccb_h;
586 switch (ccb_h->func_code) {
588 struct ccb_scsiio *csio;
591 data_ptr = csio->data_ptr;
592 dxfer_len = csio->dxfer_len;
593 sglist_cnt = csio->sglist_cnt;
596 case XPT_CONT_TARGET_IO: {
597 struct ccb_scsiio *ctio;
600 data_ptr = ctio->data_ptr;
601 dxfer_len = ctio->dxfer_len;
602 sglist_cnt = ctio->sglist_cnt;
606 struct ccb_ataio *ataio;
609 data_ptr = ataio->data_ptr;
610 dxfer_len = ataio->dxfer_len;
615 case XPT_NVME_ADMIN: {
616 struct ccb_nvmeio *nvmeio;
618 nvmeio = &ccb->nvmeio;
619 data_ptr = nvmeio->data_ptr;
620 dxfer_len = nvmeio->dxfer_len;
621 sglist_cnt = nvmeio->sglist_cnt;
625 panic("%s: Unsupported func code %d", __func__,
629 switch ((ccb_h->flags & CAM_DATA_MASK)) {
631 return (memdesc_vaddr(data_ptr, dxfer_len));
633 return (memdesc_paddr((vm_paddr_t)(uintptr_t)data_ptr,
636 return (memdesc_vlist(data_ptr, sglist_cnt));
637 case CAM_DATA_SG_PADDR:
638 return (memdesc_plist(data_ptr, sglist_cnt));
640 return (memdesc_bio(data_ptr));
642 panic("%s: flags 0x%X unimplemented", __func__, ccb_h->flags);
647 bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
648 bus_dmamap_callback_t *callback, void *callback_arg,
651 struct ccb_hdr *ccb_h;
655 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_NONE) {
656 callback(callback_arg, NULL, 0, 0);
660 mem = memdesc_ccb(ccb);
661 return (bus_dmamap_load_mem(dmat, map, &mem, callback, callback_arg,