xref: /freebsd/sys/cam/cam.c (revision b247ff70e8f1b4bf184b9fc85d2908ec4db2d1ab)
1 /*-
2  * Generic utility routines for the Common Access Method layer.
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
32 #ifdef _KERNEL
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/memdesc.h>
36 #include <sys/sysctl.h>
37 #else /* _KERNEL */
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <camlib.h>
42 #endif /* _KERNEL */
43 
44 #include <cam/cam.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/smp_all.h>
48 #include <sys/sbuf.h>
49 
50 #ifdef _KERNEL
51 #include <sys/libkern.h>
52 #include <machine/bus.h>
53 #include <cam/cam_queue.h>
54 #include <cam/cam_xpt.h>
55 
56 FEATURE(scbus, "SCSI devices support");
57 
58 #endif
59 
60 static int	camstatusentrycomp(const void *key, const void *member);
61 
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_IDE,		 "Initiator Detected Error Message Received" },
95 	{ CAM_RESRC_UNAVAIL,	 "Resource Unavailable"			     },
96 	{ CAM_UNACKED_EVENT,	 "Unacknowledged Event by Host"		     },
97 	{ CAM_MESSAGE_RECV,	 "Message Received in Host Target Mode"	     },
98 	{ CAM_INVALID_CDB,	 "Invalid CDB received in Host Target Mode"  },
99 	{ CAM_LUN_INVALID,	 "Invalid Lun"				     },
100 	{ CAM_TID_INVALID,	 "Invalid Target ID"			     },
101 	{ CAM_FUNC_NOTAVAIL,	 "Function Not Available"		     },
102 	{ CAM_NO_NEXUS,		 "Nexus Not Established"		     },
103 	{ CAM_IID_INVALID,	 "Invalid Initiator ID"			     },
104 	{ CAM_CDB_RECVD,	 "CDB Received"				     },
105 	{ CAM_LUN_ALRDY_ENA,	 "LUN Already Enabled for Target Mode"	     },
106 	{ CAM_SCSI_BUSY,	 "SCSI Bus Busy"			     },
107 };
108 
109 #ifdef _KERNEL
110 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
111     "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(uint8_t *dst, const uint8_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(uint8_t *dst, const uint8_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 uint8_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) + '0',
160 				    ((*src & 0070) >> 3) + '0',
161 				    ((*src & 0007) >> 0) + '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  * Compare string with pattern, returning 0 on match.
191  * Short pattern matches trailing blanks in name,
192  * Shell globbing rules apply: * matches 0 or more characters,
193  * ? matchces one character, [...] denotes a set to match one char,
194  * [^...] denotes a complimented set to match one character.
195  * Spaces in str used to match anything in the pattern string
196  * but was removed because it's a bug. No current patterns require
197  * it, as far as I know, but it's impossible to know what drives
198  * returned.
199  *
200  * Each '*' generates recursion, so keep the number of * in check.
201  */
202 int
203 cam_strmatch(const uint8_t *str, const uint8_t *pattern, int str_len)
204 {
205 
206 	while (*pattern != '\0' && str_len > 0) {
207 		if (*pattern == '*') {
208 			pattern++;
209 			if (*pattern == '\0')
210 				return (0);
211 			do {
212 				if (cam_strmatch(str, pattern, str_len) == 0)
213 					return (0);
214 				str++;
215 				str_len--;
216 			} while (str_len > 0);
217 			return (1);
218 		} else if (*pattern == '[') {
219 			int negate_range, ok;
220 			uint8_t pc = UCHAR_MAX;
221 			uint8_t sc;
222 
223 			ok = 0;
224 			sc = *str++;
225 			str_len--;
226 			pattern++;
227 			if ((negate_range = (*pattern == '^')) != 0)
228 				pattern++;
229 			while ((*pattern != ']') && *pattern != '\0') {
230 				if (*pattern == '-') {
231 					if (pattern[1] == '\0') /* Bad pattern */
232 						return (1);
233 					if (sc >= pc && sc <= pattern[1])
234 						ok = 1;
235 					pattern++;
236 				} else if (*pattern == sc)
237 					ok = 1;
238 				pc = *pattern;
239 				pattern++;
240 			}
241 			if (ok == negate_range)
242 				return (1);
243 			pattern++;
244 		} else if (*pattern == '?') {
245 			/*
246 			 * NB: || *str == ' ' of the old code is a bug and was
247 			 * removed.  If you add it back, keep this the last if
248 			 * before the naked else */
249 			pattern++;
250 			str++;
251 			str_len--;
252 		} else {
253 			if (*str != *pattern)
254 				return (1);
255 			pattern++;
256 			str++;
257 			str_len--;
258 		}
259 	}
260 
261 	/* '*' is allowed to match nothing, so gobble it */
262 	while (*pattern == '*')
263 		pattern++;
264 
265 	if ( *pattern != '\0') {
266 		/* Pattern not fully consumed.  Not a match */
267 		return (1);
268 	}
269 
270 	/* Eat trailing spaces, which get added by SAT */
271 	while (str_len > 0 && *str == ' ') {
272 		str++;
273 		str_len--;
274 	}
275 
276 	return (str_len);
277 }
278 
279 caddr_t
280 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
281 	       int entry_size, cam_quirkmatch_t *comp_func)
282 {
283 	for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
284 		if ((*comp_func)(target, quirk_table) == 0)
285 			return (quirk_table);
286 	}
287 	return (NULL);
288 }
289 
290 const struct cam_status_entry*
291 cam_fetch_status_entry(cam_status status)
292 {
293 	status &= CAM_STATUS_MASK;
294 	return (bsearch(&status, &cam_status_table,
295 			nitems(cam_status_table),
296 			sizeof(*cam_status_table),
297 			camstatusentrycomp));
298 }
299 
300 static int
301 camstatusentrycomp(const void *key, const void *member)
302 {
303 	cam_status status;
304 	const struct cam_status_entry *table_entry;
305 
306 	status = *(const cam_status *)key;
307 	table_entry = (const struct cam_status_entry *)member;
308 
309 	return (status - table_entry->status_code);
310 }
311 
312 #ifdef _KERNEL
313 char *
314 cam_error_string(union ccb *ccb, char *str, int str_len,
315 		 cam_error_string_flags flags,
316 		 cam_error_proto_flags proto_flags)
317 #else /* !_KERNEL */
318 char *
319 cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
320 		 int str_len, cam_error_string_flags flags,
321 		 cam_error_proto_flags proto_flags)
322 #endif /* _KERNEL/!_KERNEL */
323 {
324 	char path_str[64];
325 	struct sbuf sb;
326 
327 	if ((ccb == NULL)
328 	 || (str == NULL)
329 	 || (str_len <= 0))
330 		return(NULL);
331 
332 	if (flags == CAM_ESF_NONE)
333 		return(NULL);
334 
335 	switch (ccb->ccb_h.func_code) {
336 		case XPT_ATA_IO:
337 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
338 			case CAM_EPF_NONE:
339 				break;
340 			case CAM_EPF_ALL:
341 			case CAM_EPF_NORMAL:
342 				proto_flags |= CAM_EAF_PRINT_RESULT;
343 				/* FALLTHROUGH */
344 			case CAM_EPF_MINIMAL:
345 				proto_flags |= CAM_EAF_PRINT_STATUS;
346 				/* FALLTHROUGH */
347 			default:
348 				break;
349 			}
350 			break;
351 		case XPT_SCSI_IO:
352 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
353 			case CAM_EPF_NONE:
354 				break;
355 			case CAM_EPF_ALL:
356 			case CAM_EPF_NORMAL:
357 				proto_flags |= CAM_ESF_PRINT_SENSE;
358 				/* FALLTHROUGH */
359 			case CAM_EPF_MINIMAL:
360 				proto_flags |= CAM_ESF_PRINT_STATUS;
361 				/* FALLTHROUGH */
362 			default:
363 				break;
364 			}
365 			break;
366 		case XPT_SMP_IO:
367 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
368 			case CAM_EPF_NONE:
369 				break;
370 			case CAM_EPF_ALL:
371 				proto_flags |= CAM_ESMF_PRINT_FULL_CMD;
372 				/* FALLTHROUGH */
373 			case CAM_EPF_NORMAL:
374 			case CAM_EPF_MINIMAL:
375 				proto_flags |= CAM_ESMF_PRINT_STATUS;
376 				/* FALLTHROUGH */
377 			default:
378 				break;
379 			}
380 			break;
381 		default:
382 			break;
383 	}
384 #ifdef _KERNEL
385 	xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
386 #else /* !_KERNEL */
387 	cam_path_string(device, path_str, sizeof(path_str));
388 #endif /* _KERNEL/!_KERNEL */
389 
390 	sbuf_new(&sb, str, str_len, 0);
391 
392 	if (flags & CAM_ESF_COMMAND) {
393 		sbuf_cat(&sb, path_str);
394 		switch (ccb->ccb_h.func_code) {
395 		case XPT_ATA_IO:
396 			ata_command_sbuf(&ccb->ataio, &sb);
397 			break;
398 		case XPT_SCSI_IO:
399 #ifdef _KERNEL
400 			scsi_command_string(&ccb->csio, &sb);
401 #else /* !_KERNEL */
402 			scsi_command_string(device, &ccb->csio, &sb);
403 #endif /* _KERNEL/!_KERNEL */
404 			break;
405 		case XPT_SMP_IO:
406 			smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 -
407 					 strlen(path_str), (proto_flags &
408 					 CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0);
409 			break;
410 		case XPT_NVME_IO:
411 		case XPT_NVME_ADMIN:
412 			nvme_command_sbuf(&ccb->nvmeio, &sb);
413 			break;
414 		default:
415 			sbuf_printf(&sb, "CAM func %#x",
416 			    ccb->ccb_h.func_code);
417 			break;
418 		}
419 		sbuf_printf(&sb, "\n");
420 	}
421 
422 	if (flags & CAM_ESF_CAM_STATUS) {
423 		cam_status status;
424 		const struct cam_status_entry *entry;
425 
426 		sbuf_cat(&sb, path_str);
427 
428 		status = ccb->ccb_h.status & CAM_STATUS_MASK;
429 
430 		entry = cam_fetch_status_entry(status);
431 
432 		if (entry == NULL)
433 			sbuf_printf(&sb, "CAM status: Unknown (%#x)\n",
434 				    ccb->ccb_h.status);
435 		else
436 			sbuf_printf(&sb, "CAM status: %s\n",
437 				    entry->status_text);
438 	}
439 
440 	if (flags & CAM_ESF_PROTO_STATUS) {
441 
442 		switch (ccb->ccb_h.func_code) {
443 		case XPT_ATA_IO:
444 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
445 			     CAM_ATA_STATUS_ERROR)
446 				break;
447 			if (proto_flags & CAM_EAF_PRINT_STATUS) {
448 				sbuf_cat(&sb, path_str);
449 				ata_status_sbuf(&ccb->ataio, &sb);
450 				sbuf_printf(&sb, "\n");
451 			}
452 			if (proto_flags & CAM_EAF_PRINT_RESULT) {
453 				sbuf_cat(&sb, path_str);
454 				sbuf_printf(&sb, "RES: ");
455 				ata_res_sbuf(&ccb->ataio.res, &sb);
456 				sbuf_printf(&sb, "\n");
457 			}
458 
459 			break;
460 		case XPT_SCSI_IO:
461 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
462 			     CAM_SCSI_STATUS_ERROR)
463 				break;
464 
465 			if (proto_flags & CAM_ESF_PRINT_STATUS) {
466 				sbuf_cat(&sb, path_str);
467 				sbuf_printf(&sb, "SCSI status: %s\n",
468 					    scsi_status_string(&ccb->csio));
469 			}
470 
471 			if ((proto_flags & CAM_ESF_PRINT_SENSE)
472 			 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
473 			 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {
474 #ifdef _KERNEL
475 				scsi_sense_sbuf(&ccb->csio, &sb,
476 						SSS_FLAG_NONE);
477 #else /* !_KERNEL */
478 				scsi_sense_sbuf(device, &ccb->csio, &sb,
479 						SSS_FLAG_NONE);
480 #endif /* _KERNEL/!_KERNEL */
481 			}
482 			break;
483 		case XPT_SMP_IO:
484 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
485 			     CAM_SMP_STATUS_ERROR)
486 				break;
487 
488 			if (proto_flags & CAM_ESF_PRINT_STATUS) {
489 				sbuf_cat(&sb, path_str);
490 				sbuf_printf(&sb, "SMP status: %s (%#x)\n",
491 				    smp_error_desc(ccb->smpio.smp_response[2]),
492 						   ccb->smpio.smp_response[2]);
493 			}
494 			/* There is no SMP equivalent to SCSI sense. */
495 			break;
496 		default:
497 			break;
498 		}
499 	}
500 
501 	sbuf_finish(&sb);
502 
503 	return(sbuf_data(&sb));
504 }
505 
506 #ifdef _KERNEL
507 
508 void
509 cam_error_print(union ccb *ccb, cam_error_string_flags flags,
510 		cam_error_proto_flags proto_flags)
511 {
512 	char str[512];
513 
514 	printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
515 	       proto_flags));
516 }
517 
518 #else /* !_KERNEL */
519 
520 void
521 cam_error_print(struct cam_device *device, union ccb *ccb,
522 		cam_error_string_flags flags, cam_error_proto_flags proto_flags,
523 		FILE *ofile)
524 {
525 	char str[512];
526 
527 	if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
528 		return;
529 
530 	fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
531 		flags, proto_flags));
532 }
533 
534 #endif /* _KERNEL/!_KERNEL */
535 
536 /*
537  * Common calculate geometry fuction
538  *
539  * Caller should set ccg->volume_size and block_size.
540  * The extended parameter should be zero if extended translation
541  * should not be used.
542  */
543 void
544 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
545 {
546 	uint32_t size_mb, secs_per_cylinder;
547 
548 	if (ccg->block_size == 0) {
549 		ccg->ccb_h.status = CAM_REQ_CMP_ERR;
550 		return;
551 	}
552 	size_mb = (1024L * 1024L) / ccg->block_size;
553 	if (size_mb == 0) {
554 		ccg->ccb_h.status = CAM_REQ_CMP_ERR;
555 		return;
556 	}
557 	size_mb = ccg->volume_size / size_mb;
558 	if (size_mb > 1024 && extended) {
559 		ccg->heads = 255;
560 		ccg->secs_per_track = 63;
561 	} else {
562 		ccg->heads = 64;
563 		ccg->secs_per_track = 32;
564 	}
565 	secs_per_cylinder = ccg->heads * ccg->secs_per_track;
566 	if (secs_per_cylinder == 0) {
567 		ccg->ccb_h.status = CAM_REQ_CMP_ERR;
568 		return;
569 	}
570 	ccg->cylinders = ccg->volume_size / secs_per_cylinder;
571 	ccg->ccb_h.status = CAM_REQ_CMP;
572 }
573 
574 #ifdef _KERNEL
575 struct memdesc
576 memdesc_ccb(union ccb *ccb)
577 {
578 	struct ccb_hdr *ccb_h;
579 	void *data_ptr;
580 	uint32_t dxfer_len;
581 	uint16_t sglist_cnt;
582 
583 	ccb_h = &ccb->ccb_h;
584 	switch (ccb_h->func_code) {
585 	case XPT_SCSI_IO: {
586 		struct ccb_scsiio *csio;
587 
588 		csio = &ccb->csio;
589 		data_ptr = csio->data_ptr;
590 		dxfer_len = csio->dxfer_len;
591 		sglist_cnt = csio->sglist_cnt;
592 		break;
593 	}
594 	case XPT_CONT_TARGET_IO: {
595 		struct ccb_scsiio *ctio;
596 
597 		ctio = &ccb->ctio;
598 		data_ptr = ctio->data_ptr;
599 		dxfer_len = ctio->dxfer_len;
600 		sglist_cnt = ctio->sglist_cnt;
601 		break;
602 	}
603 	case XPT_ATA_IO: {
604 		struct ccb_ataio *ataio;
605 
606 		ataio = &ccb->ataio;
607 		data_ptr = ataio->data_ptr;
608 		dxfer_len = ataio->dxfer_len;
609 		sglist_cnt = 0;
610 		break;
611 	}
612 	case XPT_NVME_IO:
613 	case XPT_NVME_ADMIN: {
614 		struct ccb_nvmeio *nvmeio;
615 
616 		nvmeio = &ccb->nvmeio;
617 		data_ptr = nvmeio->data_ptr;
618 		dxfer_len = nvmeio->dxfer_len;
619 		sglist_cnt = nvmeio->sglist_cnt;
620 		break;
621 	}
622 	default:
623 		panic("%s: Unsupported func code %d", __func__,
624 		    ccb_h->func_code);
625 	}
626 
627 	switch ((ccb_h->flags & CAM_DATA_MASK)) {
628 	case CAM_DATA_VADDR:
629 		return (memdesc_vaddr(data_ptr, dxfer_len));
630 	case CAM_DATA_PADDR:
631 		return (memdesc_paddr((vm_paddr_t)(uintptr_t)data_ptr,
632 		    dxfer_len));
633 	case CAM_DATA_SG:
634 		return (memdesc_vlist(data_ptr, sglist_cnt));
635 	case CAM_DATA_SG_PADDR:
636 		return (memdesc_plist(data_ptr, sglist_cnt));
637 	case CAM_DATA_BIO:
638 		return (memdesc_bio(data_ptr));
639 	default:
640 		panic("%s: flags 0x%X unimplemented", __func__, ccb_h->flags);
641 	}
642 }
643 
644 int
645 bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
646 		    bus_dmamap_callback_t *callback, void *callback_arg,
647 		    int flags)
648 {
649 	struct ccb_hdr *ccb_h;
650 	struct memdesc mem;
651 
652 	ccb_h = &ccb->ccb_h;
653 	if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_NONE) {
654 		callback(callback_arg, NULL, 0, 0);
655 		return (0);
656 	}
657 
658 	mem = memdesc_ccb(ccb);
659 	return (bus_dmamap_load_mem(dmat, map, &mem, callback, callback_arg,
660 	    flags));
661 }
662 #endif
663