xref: /freebsd/sys/cam/cam.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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/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/memdesc.h>
39 #include <sys/sysctl.h>
40 #else /* _KERNEL */
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <camlib.h>
45 #endif /* _KERNEL */
46 
47 #include <cam/cam.h>
48 #include <cam/cam_ccb.h>
49 #include <cam/scsi/scsi_all.h>
50 #include <cam/scsi/smp_all.h>
51 #include <sys/sbuf.h>
52 
53 #ifdef _KERNEL
54 #include <sys/libkern.h>
55 #include <machine/bus.h>
56 #include <cam/cam_queue.h>
57 #include <cam/cam_xpt.h>
58 
59 FEATURE(scbus, "SCSI devices support");
60 
61 #endif
62 
63 static int	camstatusentrycomp(const void *key, const void *member);
64 
65 const struct cam_status_entry cam_status_table[] = {
66 	{ CAM_REQ_INPROG,	 "CCB request is in progress"		     },
67 	{ CAM_REQ_CMP,		 "CCB request completed without error"	     },
68 	{ CAM_REQ_ABORTED,	 "CCB request aborted by the host"	     },
69 	{ CAM_UA_ABORT,		 "Unable to abort CCB request"		     },
70 	{ CAM_REQ_CMP_ERR,	 "CCB request completed with an error"	     },
71 	{ CAM_BUSY,		 "CAM subsystem is busy"		     },
72 	{ CAM_REQ_INVALID,	 "CCB request was invalid"		     },
73 	{ CAM_PATH_INVALID,	 "Supplied Path ID is invalid"		     },
74 	{ CAM_DEV_NOT_THERE,	 "Device Not Present"			     },
75 	{ CAM_UA_TERMIO,	 "Unable to terminate I/O CCB request"	     },
76 	{ CAM_SEL_TIMEOUT,	 "Selection Timeout"			     },
77 	{ CAM_CMD_TIMEOUT,	 "Command timeout"			     },
78 	{ CAM_SCSI_STATUS_ERROR, "SCSI Status Error"			     },
79 	{ CAM_MSG_REJECT_REC,	 "Message Reject Reveived"		     },
80 	{ CAM_SCSI_BUS_RESET,	 "SCSI Bus Reset Sent/Received"		     },
81 	{ CAM_UNCOR_PARITY,	 "Uncorrectable parity/CRC error"	     },
82 	{ CAM_AUTOSENSE_FAIL,	 "Auto-Sense Retrieval Failed"		     },
83 	{ CAM_NO_HBA,		 "No HBA Detected"			     },
84 	{ CAM_DATA_RUN_ERR,	 "Data Overrun error"			     },
85 	{ CAM_UNEXP_BUSFREE,	 "Unexpected Bus Free"			     },
86 	{ CAM_SEQUENCE_FAIL,	 "Target Bus Phase Sequence Failure"	     },
87 	{ CAM_CCB_LEN_ERR,	 "CCB length supplied is inadequate"	     },
88 	{ CAM_PROVIDE_FAIL,	 "Unable to provide requested capability"    },
89 	{ CAM_BDR_SENT,		 "SCSI BDR Message Sent"		     },
90 	{ CAM_REQ_TERMIO,	 "CCB request terminated by the host"	     },
91 	{ CAM_UNREC_HBA_ERROR,	 "Unrecoverable Host Bus Adapter Error"	     },
92 	{ CAM_REQ_TOO_BIG,	 "The request was too large for this host"   },
93 	{ CAM_REQUEUE_REQ,	 "Unconditionally Re-queue Request",	     },
94 	{ CAM_ATA_STATUS_ERROR,	 "ATA Status Error"			     },
95 	{ CAM_SCSI_IT_NEXUS_LOST,"Initiator/Target Nexus Lost"               },
96 	{ CAM_SMP_STATUS_ERROR,	 "SMP Status Error"                          },
97 	{ CAM_IDE,		 "Initiator Detected Error Message Received" },
98 	{ CAM_RESRC_UNAVAIL,	 "Resource Unavailable"			     },
99 	{ CAM_UNACKED_EVENT,	 "Unacknowledged Event by Host"		     },
100 	{ CAM_MESSAGE_RECV,	 "Message Received in Host Target Mode"	     },
101 	{ CAM_INVALID_CDB,	 "Invalid CDB received in Host Target Mode"  },
102 	{ CAM_LUN_INVALID,	 "Invalid Lun"				     },
103 	{ CAM_TID_INVALID,	 "Invalid Target ID"			     },
104 	{ CAM_FUNC_NOTAVAIL,	 "Function Not Available"		     },
105 	{ CAM_NO_NEXUS,		 "Nexus Not Established"		     },
106 	{ CAM_IID_INVALID,	 "Invalid Initiator ID"			     },
107 	{ CAM_CDB_RECVD,	 "CDB Received"				     },
108 	{ CAM_LUN_ALRDY_ENA,	 "LUN Already Enabled for Target Mode"	     },
109 	{ CAM_SCSI_BUSY,	 "SCSI Bus Busy"			     },
110 };
111 
112 #ifdef _KERNEL
113 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
114     "CAM Subsystem");
115 
116 #ifndef CAM_DEFAULT_SORT_IO_QUEUES
117 #define CAM_DEFAULT_SORT_IO_QUEUES 1
118 #endif
119 
120 int cam_sort_io_queues = CAM_DEFAULT_SORT_IO_QUEUES;
121 SYSCTL_INT(_kern_cam, OID_AUTO, sort_io_queues, CTLFLAG_RWTUN,
122     &cam_sort_io_queues, 0, "Sort IO queues to try and optimise disk access patterns");
123 #endif
124 
125 void
126 cam_strvis(uint8_t *dst, const uint8_t *src, int srclen, int dstlen)
127 {
128 	cam_strvis_flag(dst, src, srclen, dstlen,
129 	    CAM_STRVIS_FLAG_NONASCII_ESC);
130 }
131 
132 void
133 cam_strvis_flag(uint8_t *dst, const uint8_t *src, int srclen, int dstlen,
134 		uint32_t flags)
135 {
136 	struct sbuf sb;
137 
138 	sbuf_new(&sb, dst, dstlen, SBUF_FIXEDLEN);
139 	cam_strvis_sbuf(&sb, src, srclen, flags);
140 	sbuf_finish(&sb);
141 }
142 
143 void
144 cam_strvis_sbuf(struct sbuf *sb, const uint8_t *src, int srclen,
145 		uint32_t flags)
146 {
147 
148 	/* Trim leading/trailing spaces, nulls. */
149 	while (srclen > 0 && src[0] == ' ')
150 		src++, srclen--;
151 	while (srclen > 0
152 	    && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
153 		srclen--;
154 
155 	while (srclen > 0) {
156 		if (*src < 0x20 || *src >= 0x80) {
157 			/* SCSI-II Specifies that these should never occur. */
158 			/* non-printable character */
159 			switch (flags & CAM_STRVIS_FLAG_NONASCII_MASK) {
160 			case CAM_STRVIS_FLAG_NONASCII_ESC:
161 				sbuf_printf(sb, "\\%c%c%c",
162 				    ((*src & 0300) >> 6) + '0',
163 				    ((*src & 0070) >> 3) + '0',
164 				    ((*src & 0007) >> 0) + '0');
165 				break;
166 			case CAM_STRVIS_FLAG_NONASCII_RAW:
167 				/*
168 				 * If we run into a NUL, just transform it
169 				 * into a space.
170 				 */
171 				if (*src != 0x00)
172 					sbuf_putc(sb, *src);
173 				else
174 					sbuf_putc(sb, ' ');
175 				break;
176 			case CAM_STRVIS_FLAG_NONASCII_SPC:
177 				sbuf_putc(sb, ' ');
178 				break;
179 			case CAM_STRVIS_FLAG_NONASCII_TRIM:
180 			default:
181 				break;
182 			}
183 		} else {
184 			/* normal character */
185 			sbuf_putc(sb, *src);
186 		}
187 		src++;
188 		srclen--;
189 	}
190 }
191 
192 /*
193  * Compare string with pattern, returning 0 on match.
194  * Short pattern matches trailing blanks in name,
195  * Shell globbing rules apply: * matches 0 or more characters,
196  * ? matchces one character, [...] denotes a set to match one char,
197  * [^...] denotes a complimented set to match one character.
198  * Spaces in str used to match anything in the pattern string
199  * but was removed because it's a bug. No current patterns require
200  * it, as far as I know, but it's impossible to know what drives
201  * returned.
202  *
203  * Each '*' generates recursion, so keep the number of * in check.
204  */
205 int
206 cam_strmatch(const uint8_t *str, const uint8_t *pattern, int str_len)
207 {
208 
209 	while (*pattern != '\0' && str_len > 0) {
210 		if (*pattern == '*') {
211 			pattern++;
212 			if (*pattern == '\0')
213 				return (0);
214 			do {
215 				if (cam_strmatch(str, pattern, str_len) == 0)
216 					return (0);
217 				str++;
218 				str_len--;
219 			} while (str_len > 0);
220 			return (1);
221 		} else if (*pattern == '[') {
222 			int negate_range, ok;
223 			uint8_t pc = UCHAR_MAX;
224 			uint8_t sc;
225 
226 			ok = 0;
227 			sc = *str++;
228 			str_len--;
229 			pattern++;
230 			if ((negate_range = (*pattern == '^')) != 0)
231 				pattern++;
232 			while ((*pattern != ']') && *pattern != '\0') {
233 				if (*pattern == '-') {
234 					if (pattern[1] == '\0') /* Bad pattern */
235 						return (1);
236 					if (sc >= pc && sc <= pattern[1])
237 						ok = 1;
238 					pattern++;
239 				} else if (*pattern == sc)
240 					ok = 1;
241 				pc = *pattern;
242 				pattern++;
243 			}
244 			if (ok == negate_range)
245 				return (1);
246 			pattern++;
247 		} else if (*pattern == '?') {
248 			/*
249 			 * NB: || *str == ' ' of the old code is a bug and was
250 			 * removed.  If you add it back, keep this the last if
251 			 * before the naked else */
252 			pattern++;
253 			str++;
254 			str_len--;
255 		} else {
256 			if (*str != *pattern)
257 				return (1);
258 			pattern++;
259 			str++;
260 			str_len--;
261 		}
262 	}
263 
264 	/* '*' is allowed to match nothing, so gobble it */
265 	while (*pattern == '*')
266 		pattern++;
267 
268 	if ( *pattern != '\0') {
269 		/* Pattern not fully consumed.  Not a match */
270 		return (1);
271 	}
272 
273 	/* Eat trailing spaces, which get added by SAT */
274 	while (str_len > 0 && *str == ' ') {
275 		str++;
276 		str_len--;
277 	}
278 
279 	return (str_len);
280 }
281 
282 caddr_t
283 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
284 	       int entry_size, cam_quirkmatch_t *comp_func)
285 {
286 	for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
287 		if ((*comp_func)(target, quirk_table) == 0)
288 			return (quirk_table);
289 	}
290 	return (NULL);
291 }
292 
293 const struct cam_status_entry*
294 cam_fetch_status_entry(cam_status status)
295 {
296 	status &= CAM_STATUS_MASK;
297 	return (bsearch(&status, &cam_status_table,
298 			nitems(cam_status_table),
299 			sizeof(*cam_status_table),
300 			camstatusentrycomp));
301 }
302 
303 static int
304 camstatusentrycomp(const void *key, const void *member)
305 {
306 	cam_status status;
307 	const struct cam_status_entry *table_entry;
308 
309 	status = *(const cam_status *)key;
310 	table_entry = (const struct cam_status_entry *)member;
311 
312 	return (status - table_entry->status_code);
313 }
314 
315 #ifdef _KERNEL
316 char *
317 cam_error_string(union ccb *ccb, char *str, int str_len,
318 		 cam_error_string_flags flags,
319 		 cam_error_proto_flags proto_flags)
320 #else /* !_KERNEL */
321 char *
322 cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
323 		 int str_len, cam_error_string_flags flags,
324 		 cam_error_proto_flags proto_flags)
325 #endif /* _KERNEL/!_KERNEL */
326 {
327 	char path_str[64];
328 	struct sbuf sb;
329 
330 	if ((ccb == NULL)
331 	 || (str == NULL)
332 	 || (str_len <= 0))
333 		return(NULL);
334 
335 	if (flags == CAM_ESF_NONE)
336 		return(NULL);
337 
338 	switch (ccb->ccb_h.func_code) {
339 		case XPT_ATA_IO:
340 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
341 			case CAM_EPF_NONE:
342 				break;
343 			case CAM_EPF_ALL:
344 			case CAM_EPF_NORMAL:
345 				proto_flags |= CAM_EAF_PRINT_RESULT;
346 				/* FALLTHROUGH */
347 			case CAM_EPF_MINIMAL:
348 				proto_flags |= CAM_EAF_PRINT_STATUS;
349 				/* FALLTHROUGH */
350 			default:
351 				break;
352 			}
353 			break;
354 		case XPT_SCSI_IO:
355 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
356 			case CAM_EPF_NONE:
357 				break;
358 			case CAM_EPF_ALL:
359 			case CAM_EPF_NORMAL:
360 				proto_flags |= CAM_ESF_PRINT_SENSE;
361 				/* FALLTHROUGH */
362 			case CAM_EPF_MINIMAL:
363 				proto_flags |= CAM_ESF_PRINT_STATUS;
364 				/* FALLTHROUGH */
365 			default:
366 				break;
367 			}
368 			break;
369 		case XPT_SMP_IO:
370 			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
371 			case CAM_EPF_NONE:
372 				break;
373 			case CAM_EPF_ALL:
374 				proto_flags |= CAM_ESMF_PRINT_FULL_CMD;
375 				/* FALLTHROUGH */
376 			case CAM_EPF_NORMAL:
377 			case CAM_EPF_MINIMAL:
378 				proto_flags |= CAM_ESMF_PRINT_STATUS;
379 				/* FALLTHROUGH */
380 			default:
381 				break;
382 			}
383 			break;
384 		default:
385 			break;
386 	}
387 #ifdef _KERNEL
388 	xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
389 #else /* !_KERNEL */
390 	cam_path_string(device, path_str, sizeof(path_str));
391 #endif /* _KERNEL/!_KERNEL */
392 
393 	sbuf_new(&sb, str, str_len, 0);
394 
395 	if (flags & CAM_ESF_COMMAND) {
396 		sbuf_cat(&sb, path_str);
397 		switch (ccb->ccb_h.func_code) {
398 		case XPT_ATA_IO:
399 			ata_command_sbuf(&ccb->ataio, &sb);
400 			break;
401 		case XPT_SCSI_IO:
402 #ifdef _KERNEL
403 			scsi_command_string(&ccb->csio, &sb);
404 #else /* !_KERNEL */
405 			scsi_command_string(device, &ccb->csio, &sb);
406 #endif /* _KERNEL/!_KERNEL */
407 			break;
408 		case XPT_SMP_IO:
409 			smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 -
410 					 strlen(path_str), (proto_flags &
411 					 CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0);
412 			break;
413 		case XPT_NVME_IO:
414 		case XPT_NVME_ADMIN:
415 			nvme_command_sbuf(&ccb->nvmeio, &sb);
416 			break;
417 		default:
418 			sbuf_printf(&sb, "CAM func %#x",
419 			    ccb->ccb_h.func_code);
420 			break;
421 		}
422 		sbuf_printf(&sb, "\n");
423 	}
424 
425 	if (flags & CAM_ESF_CAM_STATUS) {
426 		cam_status status;
427 		const struct cam_status_entry *entry;
428 
429 		sbuf_cat(&sb, path_str);
430 
431 		status = ccb->ccb_h.status & CAM_STATUS_MASK;
432 
433 		entry = cam_fetch_status_entry(status);
434 
435 		if (entry == NULL)
436 			sbuf_printf(&sb, "CAM status: Unknown (%#x)\n",
437 				    ccb->ccb_h.status);
438 		else
439 			sbuf_printf(&sb, "CAM status: %s\n",
440 				    entry->status_text);
441 	}
442 
443 	if (flags & CAM_ESF_PROTO_STATUS) {
444 
445 		switch (ccb->ccb_h.func_code) {
446 		case XPT_ATA_IO:
447 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
448 			     CAM_ATA_STATUS_ERROR)
449 				break;
450 			if (proto_flags & CAM_EAF_PRINT_STATUS) {
451 				sbuf_cat(&sb, path_str);
452 				ata_status_sbuf(&ccb->ataio, &sb);
453 				sbuf_printf(&sb, "\n");
454 			}
455 			if (proto_flags & CAM_EAF_PRINT_RESULT) {
456 				sbuf_cat(&sb, path_str);
457 				sbuf_printf(&sb, "RES: ");
458 				ata_res_sbuf(&ccb->ataio.res, &sb);
459 				sbuf_printf(&sb, "\n");
460 			}
461 
462 			break;
463 		case XPT_SCSI_IO:
464 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
465 			     CAM_SCSI_STATUS_ERROR)
466 				break;
467 
468 			if (proto_flags & CAM_ESF_PRINT_STATUS) {
469 				sbuf_cat(&sb, path_str);
470 				sbuf_printf(&sb, "SCSI status: %s\n",
471 					    scsi_status_string(&ccb->csio));
472 			}
473 
474 			if ((proto_flags & CAM_ESF_PRINT_SENSE)
475 			 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
476 			 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {
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 }
576 
577 #ifdef _KERNEL
578 struct memdesc
579 memdesc_ccb(union ccb *ccb)
580 {
581 	struct ccb_hdr *ccb_h;
582 	void *data_ptr;
583 	uint32_t dxfer_len;
584 	uint16_t sglist_cnt;
585 
586 	ccb_h = &ccb->ccb_h;
587 	switch (ccb_h->func_code) {
588 	case XPT_SCSI_IO: {
589 		struct ccb_scsiio *csio;
590 
591 		csio = &ccb->csio;
592 		data_ptr = csio->data_ptr;
593 		dxfer_len = csio->dxfer_len;
594 		sglist_cnt = csio->sglist_cnt;
595 		break;
596 	}
597 	case XPT_CONT_TARGET_IO: {
598 		struct ccb_scsiio *ctio;
599 
600 		ctio = &ccb->ctio;
601 		data_ptr = ctio->data_ptr;
602 		dxfer_len = ctio->dxfer_len;
603 		sglist_cnt = ctio->sglist_cnt;
604 		break;
605 	}
606 	case XPT_ATA_IO: {
607 		struct ccb_ataio *ataio;
608 
609 		ataio = &ccb->ataio;
610 		data_ptr = ataio->data_ptr;
611 		dxfer_len = ataio->dxfer_len;
612 		sglist_cnt = 0;
613 		break;
614 	}
615 	case XPT_NVME_IO:
616 	case XPT_NVME_ADMIN: {
617 		struct ccb_nvmeio *nvmeio;
618 
619 		nvmeio = &ccb->nvmeio;
620 		data_ptr = nvmeio->data_ptr;
621 		dxfer_len = nvmeio->dxfer_len;
622 		sglist_cnt = nvmeio->sglist_cnt;
623 		break;
624 	}
625 	default:
626 		panic("%s: Unsupported func code %d", __func__,
627 		    ccb_h->func_code);
628 	}
629 
630 	switch ((ccb_h->flags & CAM_DATA_MASK)) {
631 	case CAM_DATA_VADDR:
632 		return (memdesc_vaddr(data_ptr, dxfer_len));
633 	case CAM_DATA_PADDR:
634 		return (memdesc_paddr((vm_paddr_t)(uintptr_t)data_ptr,
635 		    dxfer_len));
636 	case CAM_DATA_SG:
637 		return (memdesc_vlist(data_ptr, sglist_cnt));
638 	case CAM_DATA_SG_PADDR:
639 		return (memdesc_plist(data_ptr, sglist_cnt));
640 	case CAM_DATA_BIO:
641 		return (memdesc_bio(data_ptr));
642 	default:
643 		panic("%s: flags 0x%X unimplemented", __func__, ccb_h->flags);
644 	}
645 }
646 
647 int
648 bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
649 		    bus_dmamap_callback_t *callback, void *callback_arg,
650 		    int flags)
651 {
652 	struct ccb_hdr *ccb_h;
653 	struct memdesc mem;
654 
655 	ccb_h = &ccb->ccb_h;
656 	if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_NONE) {
657 		callback(callback_arg, NULL, 0, 0);
658 		return (0);
659 	}
660 
661 	mem = memdesc_ccb(ccb);
662 	return (bus_dmamap_load_mem(dmat, map, &mem, callback, callback_arg,
663 	    flags));
664 }
665 #endif
666