]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/cam.c
sound: Remove hw.snd.version and SND_DRV_VERSION
[FreeBSD/FreeBSD.git] / sys / cam / cam.c
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_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"                             },
109 };
110
111 #ifdef _KERNEL
112 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
113     "CAM Subsystem");
114
115 #ifndef CAM_DEFAULT_SORT_IO_QUEUES
116 #define CAM_DEFAULT_SORT_IO_QUEUES 1
117 #endif
118
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");
122 #endif
123
124 void
125 cam_strvis(uint8_t *dst, const uint8_t *src, int srclen, int dstlen)
126 {
127         cam_strvis_flag(dst, src, srclen, dstlen,
128             CAM_STRVIS_FLAG_NONASCII_ESC);
129 }
130
131 void
132 cam_strvis_flag(uint8_t *dst, const uint8_t *src, int srclen, int dstlen,
133                 uint32_t flags)
134 {
135         struct sbuf sb;
136
137         sbuf_new(&sb, dst, dstlen, SBUF_FIXEDLEN);
138         cam_strvis_sbuf(&sb, src, srclen, flags);
139         sbuf_finish(&sb);
140 }
141
142 void
143 cam_strvis_sbuf(struct sbuf *sb, const uint8_t *src, int srclen,
144                 uint32_t flags)
145 {
146
147         /* Trim leading/trailing spaces, nulls. */
148         while (srclen > 0 && src[0] == ' ')
149                 src++, srclen--;
150         while (srclen > 0
151             && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
152                 srclen--;
153
154         while (srclen > 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');
164                                 break;
165                         case CAM_STRVIS_FLAG_NONASCII_RAW:
166                                 /*
167                                  * If we run into a NUL, just transform it
168                                  * into a space.
169                                  */
170                                 if (*src != 0x00)
171                                         sbuf_putc(sb, *src);
172                                 else
173                                         sbuf_putc(sb, ' ');
174                                 break;
175                         case CAM_STRVIS_FLAG_NONASCII_SPC:
176                                 sbuf_putc(sb, ' ');
177                                 break;
178                         case CAM_STRVIS_FLAG_NONASCII_TRIM:
179                         default:
180                                 break;
181                         }
182                 } else {
183                         /* normal character */
184                         sbuf_putc(sb, *src);
185                 }
186                 src++;
187                 srclen--;
188         }
189 }
190
191 /*
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
200  * returned.
201  *
202  * Each '*' generates recursion, so keep the number of * in check.
203  */
204 int
205 cam_strmatch(const uint8_t *str, const uint8_t *pattern, int str_len)
206 {
207
208         while (*pattern != '\0' && str_len > 0) {  
209                 if (*pattern == '*') {
210                         pattern++;
211                         if (*pattern == '\0')
212                                 return (0);
213                         do {
214                                 if (cam_strmatch(str, pattern, str_len) == 0)
215                                         return (0);
216                                 str++;
217                                 str_len--;
218                         } while (str_len > 0);
219                         return (1);
220                 } else if (*pattern == '[') {
221                         int negate_range, ok;
222                         uint8_t pc = UCHAR_MAX;
223                         uint8_t sc;
224
225                         ok = 0;
226                         sc = *str++;
227                         str_len--;
228                         pattern++;
229                         if ((negate_range = (*pattern == '^')) != 0)
230                                 pattern++;
231                         while ((*pattern != ']') && *pattern != '\0') {
232                                 if (*pattern == '-') {
233                                         if (pattern[1] == '\0') /* Bad pattern */
234                                                 return (1);
235                                         if (sc >= pc && sc <= pattern[1])
236                                                 ok = 1;
237                                         pattern++;
238                                 } else if (*pattern == sc)
239                                         ok = 1;
240                                 pc = *pattern;
241                                 pattern++;
242                         }
243                         if (ok == negate_range)
244                                 return (1);
245                         pattern++;
246                 } else if (*pattern == '?') {
247                         /*
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 */
251                         pattern++;
252                         str++;
253                         str_len--;
254                 } else {
255                         if (*str != *pattern)
256                                 return (1);
257                         pattern++;
258                         str++;
259                         str_len--;
260                 }
261         }
262
263         /* '*' is allowed to match nothing, so gobble it */
264         while (*pattern == '*')
265                 pattern++;
266
267         if ( *pattern != '\0') {
268                 /* Pattern not fully consumed.  Not a match */
269                 return (1);
270         }
271
272         /* Eat trailing spaces, which get added by SAT */
273         while (str_len > 0 && *str == ' ') {
274                 str++;
275                 str_len--;
276         }
277
278         return (str_len);
279 }
280
281 caddr_t
282 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
283                int entry_size, cam_quirkmatch_t *comp_func)
284 {
285         for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
286                 if ((*comp_func)(target, quirk_table) == 0)
287                         return (quirk_table);
288         }
289         return (NULL);
290 }
291
292 const struct cam_status_entry*
293 cam_fetch_status_entry(cam_status status)
294 {
295         status &= CAM_STATUS_MASK;
296         return (bsearch(&status, &cam_status_table,
297                         nitems(cam_status_table),
298                         sizeof(*cam_status_table),
299                         camstatusentrycomp));
300 }
301
302 static int
303 camstatusentrycomp(const void *key, const void *member)
304 {
305         cam_status status;
306         const struct cam_status_entry *table_entry;
307
308         status = *(const cam_status *)key;
309         table_entry = (const struct cam_status_entry *)member;
310
311         return (status - table_entry->status_code);
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_putc(&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_putc(&sb, '\n');
453                         }
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');
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 #ifdef _KERNEL
477                                 scsi_sense_sbuf(&ccb->csio, &sb,
478                                                 SSS_FLAG_NONE);
479 #else /* !_KERNEL */
480                                 scsi_sense_sbuf(device, &ccb->csio, &sb,
481                                                 SSS_FLAG_NONE);
482 #endif /* _KERNEL/!_KERNEL */
483                         }
484                         break;
485                 case XPT_SMP_IO:
486                         if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
487                              CAM_SMP_STATUS_ERROR)
488                                 break;
489
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]);
495                         }
496                         /* There is no SMP equivalent to SCSI sense. */
497                         break;
498                 default:
499                         break;
500                 }
501         }
502
503         sbuf_finish(&sb);
504
505         return(sbuf_data(&sb));
506 }
507
508 #ifdef _KERNEL
509
510 void
511 cam_error_print(union ccb *ccb, cam_error_string_flags flags,
512                 cam_error_proto_flags proto_flags)
513 {
514         char str[512];
515
516         printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
517                proto_flags));
518 }
519
520 #else /* !_KERNEL */
521
522 void
523 cam_error_print(struct cam_device *device, union ccb *ccb,
524                 cam_error_string_flags flags, cam_error_proto_flags proto_flags,
525                 FILE *ofile)
526 {
527         char str[512];
528
529         if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
530                 return;
531
532         fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
533                 flags, proto_flags));
534 }
535
536 #endif /* _KERNEL/!_KERNEL */
537
538 /*
539  * Common calculate geometry fuction
540  *
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.
544  */
545 void
546 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
547 {
548         uint32_t size_mb, secs_per_cylinder;
549
550         if (ccg->block_size == 0) {
551                 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
552                 return;
553         }
554         size_mb = (1024L * 1024L) / ccg->block_size;
555         if (size_mb == 0) {
556                 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
557                 return;
558         }
559         size_mb = ccg->volume_size / size_mb;
560         if (size_mb > 1024 && extended) {
561                 ccg->heads = 255;
562                 ccg->secs_per_track = 63;
563         } else {
564                 ccg->heads = 64;
565                 ccg->secs_per_track = 32;
566         }
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;
570                 return;
571         }
572         ccg->cylinders = ccg->volume_size / secs_per_cylinder;
573         ccg->ccb_h.status = CAM_REQ_CMP;
574 }
575
576 #ifdef _KERNEL
577 struct memdesc
578 memdesc_ccb(union ccb *ccb)
579 {
580         struct ccb_hdr *ccb_h;
581         void *data_ptr;
582         uint32_t dxfer_len;
583         uint16_t sglist_cnt;
584
585         ccb_h = &ccb->ccb_h;
586         switch (ccb_h->func_code) {
587         case XPT_SCSI_IO: {
588                 struct ccb_scsiio *csio;
589
590                 csio = &ccb->csio;
591                 data_ptr = csio->data_ptr;
592                 dxfer_len = csio->dxfer_len;
593                 sglist_cnt = csio->sglist_cnt;
594                 break;
595         }
596         case XPT_CONT_TARGET_IO: {
597                 struct ccb_scsiio *ctio;
598
599                 ctio = &ccb->ctio;
600                 data_ptr = ctio->data_ptr;
601                 dxfer_len = ctio->dxfer_len;
602                 sglist_cnt = ctio->sglist_cnt;
603                 break;
604         }
605         case XPT_ATA_IO: {
606                 struct ccb_ataio *ataio;
607
608                 ataio = &ccb->ataio;
609                 data_ptr = ataio->data_ptr;
610                 dxfer_len = ataio->dxfer_len;
611                 sglist_cnt = 0;
612                 break;
613         }
614         case XPT_NVME_IO:
615         case XPT_NVME_ADMIN: {
616                 struct ccb_nvmeio *nvmeio;
617
618                 nvmeio = &ccb->nvmeio;
619                 data_ptr = nvmeio->data_ptr;
620                 dxfer_len = nvmeio->dxfer_len;
621                 sglist_cnt = nvmeio->sglist_cnt;
622                 break;
623         }
624         default:
625                 panic("%s: Unsupported func code %d", __func__,
626                     ccb_h->func_code);
627         }
628
629         switch ((ccb_h->flags & CAM_DATA_MASK)) {
630         case CAM_DATA_VADDR:
631                 return (memdesc_vaddr(data_ptr, dxfer_len));
632         case CAM_DATA_PADDR:
633                 return (memdesc_paddr((vm_paddr_t)(uintptr_t)data_ptr,
634                     dxfer_len));
635         case CAM_DATA_SG:
636                 return (memdesc_vlist(data_ptr, sglist_cnt));
637         case CAM_DATA_SG_PADDR:
638                 return (memdesc_plist(data_ptr, sglist_cnt));
639         case CAM_DATA_BIO:
640                 return (memdesc_bio(data_ptr));
641         default:
642                 panic("%s: flags 0x%X unimplemented", __func__, ccb_h->flags);
643         }
644 }
645
646 int
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,
649                     int flags)
650 {
651         struct ccb_hdr *ccb_h;
652         struct memdesc mem;
653
654         ccb_h = &ccb->ccb_h;
655         if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_NONE) {
656                 callback(callback_arg, NULL, 0, 0);
657                 return (0);
658         }
659
660         mem = memdesc_ccb(ccb);
661         return (bus_dmamap_load_mem(dmat, map, &mem, callback, callback_arg,
662             flags));
663 }
664 #endif