]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/scsi/scsi_da.c
Include opt files in the kernel with "" instead of <>.
[FreeBSD/FreeBSD.git] / sys / cam / scsi / scsi_da.c
1 /*-
2  * Implementation of SCSI Direct Access Peripheral driver for CAM.
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33
34 #ifdef _KERNEL
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bio.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/conf.h>
43 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
46 #include <sys/cons.h>
47 #include <sys/endian.h>
48 #include <sys/proc.h>
49 #include <sys/sbuf.h>
50 #include <geom/geom.h>
51 #include <geom/geom_disk.h>
52 #endif /* _KERNEL */
53
54 #ifndef _KERNEL
55 #include <stdio.h>
56 #include <string.h>
57 #endif /* _KERNEL */
58
59 #include <cam/cam.h>
60 #include <cam/cam_ccb.h>
61 #include <cam/cam_periph.h>
62 #include <cam/cam_xpt_periph.h>
63 #include <cam/cam_sim.h>
64 #include <cam/cam_iosched.h>
65
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_da.h>
68
69 #ifdef _KERNEL
70 /*
71  * Note that there are probe ordering dependencies here.  The order isn't
72  * controlled by this enumeration, but by explicit state transitions in
73  * dastart() and dadone().  Here are some of the dependencies:
74  * 
75  * 1. RC should come first, before RC16, unless there is evidence that RC16
76  *    is supported.
77  * 2. BDC needs to come before any of the ATA probes, or the ZONE probe.
78  * 3. The ATA probes should go in this order:
79  *    ATA -> LOGDIR -> IDDIR -> SUP -> ATA_ZONE
80  */
81 typedef enum {
82         DA_STATE_PROBE_RC,
83         DA_STATE_PROBE_RC16,
84         DA_STATE_PROBE_LBP,
85         DA_STATE_PROBE_BLK_LIMITS,
86         DA_STATE_PROBE_BDC,
87         DA_STATE_PROBE_ATA,
88         DA_STATE_PROBE_ATA_LOGDIR,
89         DA_STATE_PROBE_ATA_IDDIR,
90         DA_STATE_PROBE_ATA_SUP,
91         DA_STATE_PROBE_ATA_ZONE,
92         DA_STATE_PROBE_ZONE,
93         DA_STATE_NORMAL
94 } da_state;
95
96 typedef enum {
97         DA_FLAG_PACK_INVALID    = 0x000001,
98         DA_FLAG_NEW_PACK        = 0x000002,
99         DA_FLAG_PACK_LOCKED     = 0x000004,
100         DA_FLAG_PACK_REMOVABLE  = 0x000008,
101         DA_FLAG_NEED_OTAG       = 0x000020,
102         DA_FLAG_WAS_OTAG        = 0x000040,
103         DA_FLAG_RETRY_UA        = 0x000080,
104         DA_FLAG_OPEN            = 0x000100,
105         DA_FLAG_SCTX_INIT       = 0x000200,
106         DA_FLAG_CAN_RC16        = 0x000400,
107         DA_FLAG_PROBED          = 0x000800,
108         DA_FLAG_DIRTY           = 0x001000,
109         DA_FLAG_ANNOUNCED       = 0x002000,
110         DA_FLAG_CAN_ATA_DMA     = 0x004000,
111         DA_FLAG_CAN_ATA_LOG     = 0x008000,
112         DA_FLAG_CAN_ATA_IDLOG   = 0x010000,
113         DA_FLAG_CAN_ATA_SUPCAP  = 0x020000,
114         DA_FLAG_CAN_ATA_ZONE    = 0x040000
115 } da_flags;
116
117 typedef enum {
118         DA_Q_NONE               = 0x00,
119         DA_Q_NO_SYNC_CACHE      = 0x01,
120         DA_Q_NO_6_BYTE          = 0x02,
121         DA_Q_NO_PREVENT         = 0x04,
122         DA_Q_4K                 = 0x08,
123         DA_Q_NO_RC16            = 0x10,
124         DA_Q_NO_UNMAP           = 0x20,
125         DA_Q_RETRY_BUSY         = 0x40,
126         DA_Q_SMR_DM             = 0x80,
127         DA_Q_STRICT_UNMAP       = 0x100
128 } da_quirks;
129
130 #define DA_Q_BIT_STRING         \
131         "\020"                  \
132         "\001NO_SYNC_CACHE"     \
133         "\002NO_6_BYTE"         \
134         "\003NO_PREVENT"        \
135         "\0044K"                \
136         "\005NO_RC16"           \
137         "\006NO_UNMAP"          \
138         "\007RETRY_BUSY"        \
139         "\010SMR_DM"            \
140         "\011STRICT_UNMAP"
141
142 typedef enum {
143         DA_CCB_PROBE_RC         = 0x01,
144         DA_CCB_PROBE_RC16       = 0x02,
145         DA_CCB_PROBE_LBP        = 0x03,
146         DA_CCB_PROBE_BLK_LIMITS = 0x04,
147         DA_CCB_PROBE_BDC        = 0x05,
148         DA_CCB_PROBE_ATA        = 0x06,
149         DA_CCB_BUFFER_IO        = 0x07,
150         DA_CCB_DUMP             = 0x0A,
151         DA_CCB_DELETE           = 0x0B,
152         DA_CCB_TUR              = 0x0C,
153         DA_CCB_PROBE_ZONE       = 0x0D,
154         DA_CCB_PROBE_ATA_LOGDIR = 0x0E,
155         DA_CCB_PROBE_ATA_IDDIR  = 0x0F,
156         DA_CCB_PROBE_ATA_SUP    = 0x10,
157         DA_CCB_PROBE_ATA_ZONE   = 0x11,
158         DA_CCB_TYPE_MASK        = 0x1F,
159         DA_CCB_RETRY_UA         = 0x20
160 } da_ccb_state;
161
162 /*
163  * Order here is important for method choice
164  *
165  * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
166  * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
167  * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
168  * import taking 5mins.
169  *
170  */
171 typedef enum {
172         DA_DELETE_NONE,
173         DA_DELETE_DISABLE,
174         DA_DELETE_ATA_TRIM,
175         DA_DELETE_UNMAP,
176         DA_DELETE_WS16,
177         DA_DELETE_WS10,
178         DA_DELETE_ZERO,
179         DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
180         DA_DELETE_MAX = DA_DELETE_ZERO
181 } da_delete_methods;
182
183 /*
184  * For SCSI, host managed drives show up as a separate device type.  For
185  * ATA, host managed drives also have a different device signature.
186  * XXX KDM figure out the ATA host managed signature.
187  */
188 typedef enum {
189         DA_ZONE_NONE            = 0x00,
190         DA_ZONE_DRIVE_MANAGED   = 0x01,
191         DA_ZONE_HOST_AWARE      = 0x02,
192         DA_ZONE_HOST_MANAGED    = 0x03
193 } da_zone_mode;
194
195 /*
196  * We distinguish between these interface cases in addition to the drive type:
197  * o ATA drive behind a SCSI translation layer that knows about ZBC/ZAC
198  * o ATA drive behind a SCSI translation layer that does not know about
199  *   ZBC/ZAC, and so needs to be managed via ATA passthrough.  In this
200  *   case, we would need to share the ATA code with the ada(4) driver.
201  * o SCSI drive.
202  */
203 typedef enum {
204         DA_ZONE_IF_SCSI,
205         DA_ZONE_IF_ATA_PASS,
206         DA_ZONE_IF_ATA_SAT,
207 } da_zone_interface;
208
209 typedef enum {
210         DA_ZONE_FLAG_RZ_SUP             = 0x0001,
211         DA_ZONE_FLAG_OPEN_SUP           = 0x0002,
212         DA_ZONE_FLAG_CLOSE_SUP          = 0x0004,
213         DA_ZONE_FLAG_FINISH_SUP         = 0x0008,
214         DA_ZONE_FLAG_RWP_SUP            = 0x0010,
215         DA_ZONE_FLAG_SUP_MASK           = (DA_ZONE_FLAG_RZ_SUP |
216                                            DA_ZONE_FLAG_OPEN_SUP |
217                                            DA_ZONE_FLAG_CLOSE_SUP |
218                                            DA_ZONE_FLAG_FINISH_SUP |
219                                            DA_ZONE_FLAG_RWP_SUP),
220         DA_ZONE_FLAG_URSWRZ             = 0x0020,
221         DA_ZONE_FLAG_OPT_SEQ_SET        = 0x0040,
222         DA_ZONE_FLAG_OPT_NONSEQ_SET     = 0x0080,
223         DA_ZONE_FLAG_MAX_SEQ_SET        = 0x0100,
224         DA_ZONE_FLAG_SET_MASK           = (DA_ZONE_FLAG_OPT_SEQ_SET |
225                                            DA_ZONE_FLAG_OPT_NONSEQ_SET |
226                                            DA_ZONE_FLAG_MAX_SEQ_SET)
227 } da_zone_flags;
228
229 static struct da_zone_desc {
230         da_zone_flags value;
231         const char *desc;
232 } da_zone_desc_table[] = {
233         {DA_ZONE_FLAG_RZ_SUP, "Report Zones" },
234         {DA_ZONE_FLAG_OPEN_SUP, "Open" },
235         {DA_ZONE_FLAG_CLOSE_SUP, "Close" },
236         {DA_ZONE_FLAG_FINISH_SUP, "Finish" },
237         {DA_ZONE_FLAG_RWP_SUP, "Reset Write Pointer" },
238 };
239
240 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
241                               struct bio *bp);
242 static da_delete_func_t da_delete_trim;
243 static da_delete_func_t da_delete_unmap;
244 static da_delete_func_t da_delete_ws;
245
246 static const void * da_delete_functions[] = {
247         NULL,
248         NULL,
249         da_delete_trim,
250         da_delete_unmap,
251         da_delete_ws,
252         da_delete_ws,
253         da_delete_ws
254 };
255
256 static const char *da_delete_method_names[] =
257     { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
258 static const char *da_delete_method_desc[] =
259     { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
260       "WRITE SAME(10) with UNMAP", "ZERO" };
261
262 /* Offsets into our private area for storing information */
263 #define ccb_state       ppriv_field0
264 #define ccb_bp          ppriv_ptr1
265
266 struct disk_params {
267         u_int8_t  heads;
268         u_int32_t cylinders;
269         u_int8_t  secs_per_track;
270         u_int32_t secsize;      /* Number of bytes/sector */
271         u_int64_t sectors;      /* total number sectors */
272         u_int     stripesize;
273         u_int     stripeoffset;
274 };
275
276 #define UNMAP_RANGE_MAX         0xffffffff
277 #define UNMAP_HEAD_SIZE         8
278 #define UNMAP_RANGE_SIZE        16
279 #define UNMAP_MAX_RANGES        2048 /* Protocol Max is 4095 */
280 #define UNMAP_BUF_SIZE          ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
281                                 UNMAP_HEAD_SIZE)
282
283 #define WS10_MAX_BLKS           0xffff
284 #define WS16_MAX_BLKS           0xffffffff
285 #define ATA_TRIM_MAX_RANGES     ((UNMAP_BUF_SIZE / \
286         (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
287
288 #define DA_WORK_TUR             (1 << 16)
289
290 struct da_softc {
291         struct   cam_iosched_softc *cam_iosched;
292         struct   bio_queue_head delete_run_queue;
293         LIST_HEAD(, ccb_hdr) pending_ccbs;
294         int      refcount;              /* Active xpt_action() calls */
295         da_state state;
296         da_flags flags; 
297         da_quirks quirks;
298         int      minimum_cmd_size;
299         int      error_inject;
300         int      trim_max_ranges;
301         int      delete_available;      /* Delete methods possibly available */
302         da_zone_mode                    zone_mode;
303         da_zone_interface               zone_interface;
304         da_zone_flags                   zone_flags;
305         struct ata_gp_log_dir           ata_logdir;
306         int                             valid_logdir_len;
307         struct ata_identify_log_pages   ata_iddir;
308         int                             valid_iddir_len;
309         uint64_t                        optimal_seq_zones;
310         uint64_t                        optimal_nonseq_zones;
311         uint64_t                        max_seq_zones;
312         u_int                   maxio;
313         uint32_t                unmap_max_ranges;
314         uint32_t                unmap_max_lba; /* Max LBAs in UNMAP req */
315         uint32_t                unmap_gran;
316         uint32_t                unmap_gran_align;
317         uint64_t                ws_max_blks;
318         da_delete_methods       delete_method_pref;
319         da_delete_methods       delete_method;
320         da_delete_func_t        *delete_func;
321         int                     unmappedio;
322         int                     rotating;
323         struct   disk_params params;
324         struct   disk *disk;
325         union    ccb saved_ccb;
326         struct task             sysctl_task;
327         struct sysctl_ctx_list  sysctl_ctx;
328         struct sysctl_oid       *sysctl_tree;
329         struct callout          sendordered_c;
330         uint64_t wwpn;
331         uint8_t  unmap_buf[UNMAP_BUF_SIZE];
332         struct scsi_read_capacity_data_long rcaplong;
333         struct callout          mediapoll_c;
334 #ifdef CAM_IO_STATS
335         struct sysctl_ctx_list  sysctl_stats_ctx;
336         struct sysctl_oid       *sysctl_stats_tree;
337         u_int   errors;
338         u_int   timeouts;
339         u_int   invalidations;
340 #endif
341 #define DA_ANNOUNCETMP_SZ 80
342         char                    announce_temp[DA_ANNOUNCETMP_SZ];
343 #define DA_ANNOUNCE_SZ 400
344         char                    announcebuf[DA_ANNOUNCE_SZ];
345 };
346
347 #define dadeleteflag(softc, delete_method, enable)                      \
348         if (enable) {                                                   \
349                 softc->delete_available |= (1 << delete_method);        \
350         } else {                                                        \
351                 softc->delete_available &= ~(1 << delete_method);       \
352         }
353
354 struct da_quirk_entry {
355         struct scsi_inquiry_pattern inq_pat;
356         da_quirks quirks;
357 };
358
359 static const char quantum[] = "QUANTUM";
360 static const char microp[] = "MICROP";
361
362 static struct da_quirk_entry da_quirk_table[] =
363 {
364         /* SPI, FC devices */
365         {
366                 /*
367                  * Fujitsu M2513A MO drives.
368                  * Tested devices: M2513A2 firmware versions 1200 & 1300.
369                  * (dip switch selects whether T_DIRECT or T_OPTICAL device)
370                  * Reported by: W.Scholten <whs@xs4all.nl>
371                  */
372                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
373                 /*quirks*/ DA_Q_NO_SYNC_CACHE
374         },
375         {
376                 /* See above. */
377                 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
378                 /*quirks*/ DA_Q_NO_SYNC_CACHE
379         },
380         {
381                 /*
382                  * This particular Fujitsu drive doesn't like the
383                  * synchronize cache command.
384                  * Reported by: Tom Jackson <toj@gorilla.net>
385                  */
386                 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
387                 /*quirks*/ DA_Q_NO_SYNC_CACHE
388         },
389         {
390                 /*
391                  * This drive doesn't like the synchronize cache command
392                  * either.  Reported by: Matthew Jacob <mjacob@feral.com>
393                  * in NetBSD PR kern/6027, August 24, 1998.
394                  */
395                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
396                 /*quirks*/ DA_Q_NO_SYNC_CACHE
397         },
398         {
399                 /*
400                  * This drive doesn't like the synchronize cache command
401                  * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
402                  * (PR 8882).
403                  */
404                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
405                 /*quirks*/ DA_Q_NO_SYNC_CACHE
406         },
407         {
408                 /*
409                  * Doesn't like the synchronize cache command.
410                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
411                  */
412                 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
413                 /*quirks*/ DA_Q_NO_SYNC_CACHE
414         },
415         {
416                 /*
417                  * Doesn't like the synchronize cache command.
418                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
419                  */
420                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
421                 /*quirks*/ DA_Q_NO_SYNC_CACHE
422         },
423         {
424                 /*
425                  * Doesn't like the synchronize cache command.
426                  */
427                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
428                 /*quirks*/ DA_Q_NO_SYNC_CACHE
429         },
430         {
431                 /*
432                  * Doesn't like the synchronize cache command.
433                  * Reported by: walter@pelissero.de
434                  */
435                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
436                 /*quirks*/ DA_Q_NO_SYNC_CACHE
437         },
438         {
439                 /*
440                  * Doesn't work correctly with 6 byte reads/writes.
441                  * Returns illegal request, and points to byte 9 of the
442                  * 6-byte CDB.
443                  * Reported by:  Adam McDougall <bsdx@spawnet.com>
444                  */
445                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
446                 /*quirks*/ DA_Q_NO_6_BYTE
447         },
448         {
449                 /* See above. */
450                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
451                 /*quirks*/ DA_Q_NO_6_BYTE
452         },
453         {
454                 /*
455                  * Doesn't like the synchronize cache command.
456                  * Reported by: walter@pelissero.de
457                  */
458                 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
459                 /*quirks*/ DA_Q_NO_SYNC_CACHE
460         },
461         {
462                 /*
463                  * The CISS RAID controllers do not support SYNC_CACHE
464                  */
465                 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
466                 /*quirks*/ DA_Q_NO_SYNC_CACHE
467         },
468         {
469                 /*
470                  * The STEC SSDs sometimes hang on UNMAP.
471                  */
472                 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
473                 /*quirks*/ DA_Q_NO_UNMAP
474         },
475         {
476                 /*
477                  * VMware returns BUSY status when storage has transient
478                  * connectivity problems, so better wait.
479                  * Also VMware returns odd errors on misaligned UNMAPs.
480                  */
481                 {T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*"},
482                 /*quirks*/ DA_Q_RETRY_BUSY | DA_Q_STRICT_UNMAP
483         },
484         /* USB mass storage devices supported by umass(4) */
485         {
486                 /*
487                  * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
488                  * PR: kern/51675
489                  */
490                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
491                 /*quirks*/ DA_Q_NO_SYNC_CACHE
492         },
493         {
494                 /*
495                  * Power Quotient Int. (PQI) USB flash key
496                  * PR: kern/53067
497                  */
498                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
499                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
500         },
501         {
502                 /*
503                  * Creative Nomad MUVO mp3 player (USB)
504                  * PR: kern/53094
505                  */
506                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
507                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
508         },
509         {
510                 /*
511                  * Jungsoft NEXDISK USB flash key
512                  * PR: kern/54737
513                  */
514                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
515                 /*quirks*/ DA_Q_NO_SYNC_CACHE
516         },
517         {
518                 /*
519                  * FreeDik USB Mini Data Drive
520                  * PR: kern/54786
521                  */
522                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
523                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
524         },
525         {
526                 /*
527                  * Sigmatel USB Flash MP3 Player
528                  * PR: kern/57046
529                  */
530                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
531                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
532         },
533         {
534                 /*
535                  * Neuros USB Digital Audio Computer
536                  * PR: kern/63645
537                  */
538                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
539                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
540         },
541         {
542                 /*
543                  * SEAGRAND NP-900 MP3 Player
544                  * PR: kern/64563
545                  */
546                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
547                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
548         },
549         {
550                 /*
551                  * iRiver iFP MP3 player (with UMS Firmware)
552                  * PR: kern/54881, i386/63941, kern/66124
553                  */
554                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
555                 /*quirks*/ DA_Q_NO_SYNC_CACHE
556         },
557         {
558                 /*
559                  * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
560                  * PR: kern/70158
561                  */
562                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
563                 /*quirks*/ DA_Q_NO_SYNC_CACHE
564         },
565         {
566                 /*
567                  * ZICPlay USB MP3 Player with FM
568                  * PR: kern/75057
569                  */
570                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
571                 /*quirks*/ DA_Q_NO_SYNC_CACHE
572         },
573         {
574                 /*
575                  * TEAC USB floppy mechanisms
576                  */
577                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
578                 /*quirks*/ DA_Q_NO_SYNC_CACHE
579         },
580         {
581                 /*
582                  * Kingston DataTraveler II+ USB Pen-Drive.
583                  * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
584                  */
585                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
586                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
587         },
588         {
589                 /*
590                  * USB DISK Pro PMAP
591                  * Reported by: jhs
592                  * PR: usb/96381
593                  */
594                 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
595                 /*quirks*/ DA_Q_NO_SYNC_CACHE
596         },
597         {
598                 /*
599                  * Motorola E398 Mobile Phone (TransFlash memory card).
600                  * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
601                  * PR: usb/89889
602                  */
603                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
604                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
605         },
606         {
607                 /*
608                  * Qware BeatZkey! Pro
609                  * PR: usb/79164
610                  */
611                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
612                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
613         },
614         {
615                 /*
616                  * Time DPA20B 1GB MP3 Player
617                  * PR: usb/81846
618                  */
619                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
620                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
621         },
622         {
623                 /*
624                  * Samsung USB key 128Mb
625                  * PR: usb/90081
626                  */
627                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
628                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
629         },
630         {
631                 /*
632                  * Kingston DataTraveler 2.0 USB Flash memory.
633                  * PR: usb/89196
634                  */
635                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
636                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
637         },
638         {
639                 /*
640                  * Creative MUVO Slim mp3 player (USB)
641                  * PR: usb/86131
642                  */
643                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
644                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
645                 },
646         {
647                 /*
648                  * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
649                  * PR: usb/80487
650                  */
651                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
652                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
653         },
654         {
655                 /*
656                  * SanDisk Micro Cruzer 128MB
657                  * PR: usb/75970
658                  */
659                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
660                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
661         },
662         {
663                 /*
664                  * TOSHIBA TransMemory USB sticks
665                  * PR: kern/94660
666                  */
667                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
668                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
669         },
670         {
671                 /*
672                  * PNY USB 3.0 Flash Drives
673                 */
674                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
675                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
676         },
677         {
678                 /*
679                  * PNY USB Flash keys
680                  * PR: usb/75578, usb/72344, usb/65436 
681                  */
682                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
683                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
684         },
685         {
686                 /*
687                  * Genesys GL3224
688                  */
689                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
690                 "120?"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_4K | DA_Q_NO_RC16
691         },
692         {
693                 /*
694                  * Genesys 6-in-1 Card Reader
695                  * PR: usb/94647
696                  */
697                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
698                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
699         },
700         {
701                 /*
702                  * Rekam Digital CAMERA
703                  * PR: usb/98713
704                  */
705                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
706                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
707         },
708         {
709                 /*
710                  * iRiver H10 MP3 player
711                  * PR: usb/102547
712                  */
713                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
714                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
715         },
716         {
717                 /*
718                  * iRiver U10 MP3 player
719                  * PR: usb/92306
720                  */
721                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
722                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
723         },
724         {
725                 /*
726                  * X-Micro Flash Disk
727                  * PR: usb/96901
728                  */
729                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
730                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
731         },
732         {
733                 /*
734                  * EasyMP3 EM732X USB 2.0 Flash MP3 Player
735                  * PR: usb/96546
736                  */
737                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
738                 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
739         },
740         {
741                 /*
742                  * Denver MP3 player
743                  * PR: usb/107101
744                  */
745                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
746                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
747         },
748         {
749                 /*
750                  * Philips USB Key Audio KEY013
751                  * PR: usb/68412
752                  */
753                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
754                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
755         },
756         {
757                 /*
758                  * JNC MP3 Player
759                  * PR: usb/94439
760                  */
761                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
762                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
763         },
764         {
765                 /*
766                  * SAMSUNG MP0402H
767                  * PR: usb/108427
768                  */
769                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
770                 /*quirks*/ DA_Q_NO_SYNC_CACHE
771         },
772         {
773                 /*
774                  * I/O Magic USB flash - Giga Bank
775                  * PR: usb/108810
776                  */
777                 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
778                 /*quirks*/ DA_Q_NO_SYNC_CACHE
779         },
780         {
781                 /*
782                  * JoyFly 128mb USB Flash Drive
783                  * PR: 96133
784                  */
785                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
786                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
787         },
788         {
789                 /*
790                  * ChipsBnk usb stick
791                  * PR: 103702
792                  */
793                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
794                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
795         },
796         {
797                 /*
798                  * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
799                  * PR: 129858
800                  */
801                 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
802                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
803         },
804         {
805                 /*
806                  * Samsung YP-U3 mp3-player
807                  * PR: 125398
808                  */
809                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
810                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
811         },
812         {
813                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
814                  "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
815         },
816         {
817                 /*
818                  * Sony Cyber-Shot DSC cameras
819                  * PR: usb/137035
820                  */
821                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
822                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
823         },
824         {
825                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
826                  "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
827         },
828         {
829                 /* At least several Transcent USB sticks lie on RC16. */
830                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
831                  "*"}, /*quirks*/ DA_Q_NO_RC16
832         },
833         {
834                 /*
835                  * I-O Data USB Flash Disk
836                  * PR: usb/211716
837                  */
838                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "I-O DATA", "USB Flash Disk*",
839                  "*"}, /*quirks*/ DA_Q_NO_RC16
840         },
841         /* ATA/SATA devices over SAS/USB/... */
842         {
843                 /* Hitachi Advanced Format (4k) drives */
844                 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
845                 /*quirks*/DA_Q_4K
846         },
847         {
848                 /* Micron Advanced Format (4k) drives */
849                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Micron 5100 MTFDDAK*", "*" },
850                 /*quirks*/DA_Q_4K
851         },
852         {
853                 /* Samsung Advanced Format (4k) drives */
854                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
855                 /*quirks*/DA_Q_4K
856         },
857         {
858                 /* Samsung Advanced Format (4k) drives */
859                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
860                 /*quirks*/DA_Q_4K
861         },
862         {
863                 /* Samsung Advanced Format (4k) drives */
864                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
865                 /*quirks*/DA_Q_4K
866         },
867         {
868                 /* Samsung Advanced Format (4k) drives */
869                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
870                 /*quirks*/DA_Q_4K
871         },
872         {
873                 /* Seagate Barracuda Green Advanced Format (4k) drives */
874                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
875                 /*quirks*/DA_Q_4K
876         },
877         {
878                 /* Seagate Barracuda Green Advanced Format (4k) drives */
879                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
880                 /*quirks*/DA_Q_4K
881         },
882         {
883                 /* Seagate Barracuda Green Advanced Format (4k) drives */
884                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
885                 /*quirks*/DA_Q_4K
886         },
887         {
888                 /* Seagate Barracuda Green Advanced Format (4k) drives */
889                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
890                 /*quirks*/DA_Q_4K
891         },
892         {
893                 /* Seagate Barracuda Green Advanced Format (4k) drives */
894                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
895                 /*quirks*/DA_Q_4K
896         },
897         {
898                 /* Seagate Barracuda Green Advanced Format (4k) drives */
899                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
900                 /*quirks*/DA_Q_4K
901         },
902         {
903                 /* Seagate Momentus Advanced Format (4k) drives */
904                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
905                 /*quirks*/DA_Q_4K
906         },
907         {
908                 /* Seagate Momentus Advanced Format (4k) drives */
909                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
910                 /*quirks*/DA_Q_4K
911         },
912         {
913                 /* Seagate Momentus Advanced Format (4k) drives */
914                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
915                 /*quirks*/DA_Q_4K
916         },
917         {
918                 /* Seagate Momentus Advanced Format (4k) drives */
919                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
920                 /*quirks*/DA_Q_4K
921         },
922         {
923                 /* Seagate Momentus Advanced Format (4k) drives */
924                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
925                 /*quirks*/DA_Q_4K
926         },
927         {
928                 /* Seagate Momentus Advanced Format (4k) drives */
929                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
930                 /*quirks*/DA_Q_4K
931         },
932         {
933                 /* Seagate Momentus Advanced Format (4k) drives */
934                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
935                 /*quirks*/DA_Q_4K
936         },
937         {
938                 /* Seagate Momentus Advanced Format (4k) drives */
939                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
940                 /*quirks*/DA_Q_4K
941         },
942         {
943                 /* Seagate Momentus Advanced Format (4k) drives */
944                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
945                 /*quirks*/DA_Q_4K
946         },
947         {
948                 /* Seagate Momentus Advanced Format (4k) drives */
949                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
950                 /*quirks*/DA_Q_4K
951         },
952         {
953                 /* Seagate Momentus Advanced Format (4k) drives */
954                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
955                 /*quirks*/DA_Q_4K
956         },
957         {
958                 /* Seagate Momentus Advanced Format (4k) drives */
959                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
960                 /*quirks*/DA_Q_4K
961         },
962         {
963                 /* Seagate Momentus Advanced Format (4k) drives */
964                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
965                 /*quirks*/DA_Q_4K
966         },
967         {
968                 /* Seagate Momentus Advanced Format (4k) drives */
969                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
970                 /*quirks*/DA_Q_4K
971         },
972         {
973                 /* Seagate Momentus Thin Advanced Format (4k) drives */
974                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
975                 /*quirks*/DA_Q_4K
976         },
977         {
978                 /* Seagate Momentus Thin Advanced Format (4k) drives */
979                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
980                 /*quirks*/DA_Q_4K
981         },
982         {
983                 /* WDC Caviar Green Advanced Format (4k) drives */
984                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
985                 /*quirks*/DA_Q_4K
986         },
987         {
988                 /* WDC Caviar Green Advanced Format (4k) drives */
989                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
990                 /*quirks*/DA_Q_4K
991         },
992         {
993                 /* WDC Caviar Green Advanced Format (4k) drives */
994                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
995                 /*quirks*/DA_Q_4K
996         },
997         {
998                 /* WDC Caviar Green Advanced Format (4k) drives */
999                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
1000                 /*quirks*/DA_Q_4K
1001         },
1002         {
1003                 /* WDC Caviar Green Advanced Format (4k) drives */
1004                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
1005                 /*quirks*/DA_Q_4K
1006         },
1007         {
1008                 /* WDC Caviar Green Advanced Format (4k) drives */
1009                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
1010                 /*quirks*/DA_Q_4K
1011         },
1012         {
1013                 /* WDC Caviar Green Advanced Format (4k) drives */
1014                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
1015                 /*quirks*/DA_Q_4K
1016         },
1017         {
1018                 /* WDC Caviar Green Advanced Format (4k) drives */
1019                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
1020                 /*quirks*/DA_Q_4K
1021         },
1022         {
1023                 /* WDC Scorpio Black Advanced Format (4k) drives */
1024                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
1025                 /*quirks*/DA_Q_4K
1026         },
1027         {
1028                 /* WDC Scorpio Black Advanced Format (4k) drives */
1029                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
1030                 /*quirks*/DA_Q_4K
1031         },
1032         {
1033                 /* WDC Scorpio Black Advanced Format (4k) drives */
1034                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
1035                 /*quirks*/DA_Q_4K
1036         },
1037         {
1038                 /* WDC Scorpio Black Advanced Format (4k) drives */
1039                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
1040                 /*quirks*/DA_Q_4K
1041         },
1042         {
1043                 /* WDC Scorpio Blue Advanced Format (4k) drives */
1044                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
1045                 /*quirks*/DA_Q_4K
1046         },
1047         {
1048                 /* WDC Scorpio Blue Advanced Format (4k) drives */
1049                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
1050                 /*quirks*/DA_Q_4K
1051         },
1052         {
1053                 /* WDC Scorpio Blue Advanced Format (4k) drives */
1054                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
1055                 /*quirks*/DA_Q_4K
1056         },
1057         {
1058                 /* WDC Scorpio Blue Advanced Format (4k) drives */
1059                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
1060                 /*quirks*/DA_Q_4K
1061         },
1062         {
1063                 /*
1064                  * Olympus FE-210 camera
1065                  */
1066                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
1067                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1068         },
1069         {
1070                 /*
1071                  * LG UP3S MP3 player
1072                  */
1073                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
1074                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1075         },
1076         {
1077                 /*
1078                  * Laser MP3-2GA13 MP3 player
1079                  */
1080                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
1081                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1082         },
1083         {
1084                 /*
1085                  * LaCie external 250GB Hard drive des by Porsche
1086                  * Submitted by: Ben Stuyts <ben@altesco.nl>
1087                  * PR: 121474
1088                  */
1089                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
1090                 /*quirks*/ DA_Q_NO_SYNC_CACHE
1091         },
1092         /* SATA SSDs */
1093         {
1094                 /*
1095                  * Corsair Force 2 SSDs
1096                  * 4k optimised & trim only works in 4k requests + 4k aligned
1097                  */
1098                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
1099                 /*quirks*/DA_Q_4K
1100         },
1101         {
1102                 /*
1103                  * Corsair Force 3 SSDs
1104                  * 4k optimised & trim only works in 4k requests + 4k aligned
1105                  */
1106                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
1107                 /*quirks*/DA_Q_4K
1108         },
1109         {
1110                 /*
1111                  * Corsair Neutron GTX SSDs
1112                  * 4k optimised & trim only works in 4k requests + 4k aligned
1113                  */
1114                 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
1115                 /*quirks*/DA_Q_4K
1116         },
1117         {
1118                 /*
1119                  * Corsair Force GT & GS SSDs
1120                  * 4k optimised & trim only works in 4k requests + 4k aligned
1121                  */
1122                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
1123                 /*quirks*/DA_Q_4K
1124         },
1125         {
1126                 /*
1127                  * Crucial M4 SSDs
1128                  * 4k optimised & trim only works in 4k requests + 4k aligned
1129                  */
1130                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
1131                 /*quirks*/DA_Q_4K
1132         },
1133         {
1134                 /*
1135                  * Crucial RealSSD C300 SSDs
1136                  * 4k optimised
1137                  */
1138                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
1139                 "*" }, /*quirks*/DA_Q_4K
1140         },
1141         {
1142                 /*
1143                  * Intel 320 Series SSDs
1144                  * 4k optimised & trim only works in 4k requests + 4k aligned
1145                  */
1146                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1147                 /*quirks*/DA_Q_4K
1148         },
1149         {
1150                 /*
1151                  * Intel 330 Series SSDs
1152                  * 4k optimised & trim only works in 4k requests + 4k aligned
1153                  */
1154                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1155                 /*quirks*/DA_Q_4K
1156         },
1157         {
1158                 /*
1159                  * Intel 510 Series SSDs
1160                  * 4k optimised & trim only works in 4k requests + 4k aligned
1161                  */
1162                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1163                 /*quirks*/DA_Q_4K
1164         },
1165         {
1166                 /*
1167                  * Intel 520 Series SSDs
1168                  * 4k optimised & trim only works in 4k requests + 4k aligned
1169                  */
1170                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1171                 /*quirks*/DA_Q_4K
1172         },
1173         {
1174                 /*
1175                  * Intel S3610 Series SSDs
1176                  * 4k optimised & trim only works in 4k requests + 4k aligned
1177                  */
1178                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BX*", "*" },
1179                 /*quirks*/DA_Q_4K
1180         },
1181         {
1182                 /*
1183                  * Intel X25-M Series SSDs
1184                  * 4k optimised & trim only works in 4k requests + 4k aligned
1185                  */
1186                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1187                 /*quirks*/DA_Q_4K
1188         },
1189         {
1190                 /*
1191                  * Kingston E100 Series SSDs
1192                  * 4k optimised & trim only works in 4k requests + 4k aligned
1193                  */
1194                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1195                 /*quirks*/DA_Q_4K
1196         },
1197         {
1198                 /*
1199                  * Kingston HyperX 3k SSDs
1200                  * 4k optimised & trim only works in 4k requests + 4k aligned
1201                  */
1202                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1203                 /*quirks*/DA_Q_4K
1204         },
1205         {
1206                 /*
1207                  * Marvell SSDs (entry taken from OpenSolaris)
1208                  * 4k optimised & trim only works in 4k requests + 4k aligned
1209                  */
1210                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1211                 /*quirks*/DA_Q_4K
1212         },
1213         {
1214                 /*
1215                  * OCZ Agility 2 SSDs
1216                  * 4k optimised & trim only works in 4k requests + 4k aligned
1217                  */
1218                 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1219                 /*quirks*/DA_Q_4K
1220         },
1221         {
1222                 /*
1223                  * OCZ Agility 3 SSDs
1224                  * 4k optimised & trim only works in 4k requests + 4k aligned
1225                  */
1226                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1227                 /*quirks*/DA_Q_4K
1228         },
1229         {
1230                 /*
1231                  * OCZ Deneva R Series SSDs
1232                  * 4k optimised & trim only works in 4k requests + 4k aligned
1233                  */
1234                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1235                 /*quirks*/DA_Q_4K
1236         },
1237         {
1238                 /*
1239                  * OCZ Vertex 2 SSDs (inc pro series)
1240                  * 4k optimised & trim only works in 4k requests + 4k aligned
1241                  */
1242                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1243                 /*quirks*/DA_Q_4K
1244         },
1245         {
1246                 /*
1247                  * OCZ Vertex 3 SSDs
1248                  * 4k optimised & trim only works in 4k requests + 4k aligned
1249                  */
1250                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1251                 /*quirks*/DA_Q_4K
1252         },
1253         {
1254                 /*
1255                  * OCZ Vertex 4 SSDs
1256                  * 4k optimised & trim only works in 4k requests + 4k aligned
1257                  */
1258                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1259                 /*quirks*/DA_Q_4K
1260         },
1261         {
1262                 /*
1263                  * Samsung 830 Series SSDs
1264                  * 4k optimised & trim only works in 4k requests + 4k aligned
1265                  */
1266                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1267                 /*quirks*/DA_Q_4K
1268         },
1269         {
1270                 /*
1271                  * Samsung 840 SSDs
1272                  * 4k optimised & trim only works in 4k requests + 4k aligned
1273                  */
1274                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1275                 /*quirks*/DA_Q_4K
1276         },
1277         {
1278                 /*
1279                  * Samsung 850 SSDs
1280                  * 4k optimised & trim only works in 4k requests + 4k aligned
1281                  */
1282                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1283                 /*quirks*/DA_Q_4K
1284         },
1285         {
1286                 /*
1287                  * Samsung 843T Series SSDs (MZ7WD*)
1288                  * Samsung PM851 Series SSDs (MZ7TE*)
1289                  * Samsung PM853T Series SSDs (MZ7GE*)
1290                  * Samsung SM863 Series SSDs (MZ7KM*)
1291                  * 4k optimised
1292                  */
1293                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
1294                 /*quirks*/DA_Q_4K
1295         },
1296         {
1297                 /*
1298                  * SuperTalent TeraDrive CT SSDs
1299                  * 4k optimised & trim only works in 4k requests + 4k aligned
1300                  */
1301                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1302                 /*quirks*/DA_Q_4K
1303         },
1304         {
1305                 /*
1306                  * XceedIOPS SATA SSDs
1307                  * 4k optimised
1308                  */
1309                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1310                 /*quirks*/DA_Q_4K
1311         },
1312         {
1313                 /*
1314                  * Hama Innostor USB-Stick 
1315                  */
1316                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" }, 
1317                 /*quirks*/DA_Q_NO_RC16
1318         },
1319         {
1320                 /*
1321                  * Seagate Lamarr 8TB Shingled Magnetic Recording (SMR)
1322                  * Drive Managed SATA hard drive.  This drive doesn't report
1323                  * in firmware that it is a drive managed SMR drive.
1324                  */
1325                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST8000AS0002*", "*" },
1326                 /*quirks*/DA_Q_SMR_DM
1327         },
1328         {
1329                 /*
1330                  * MX-ES USB Drive by Mach Xtreme
1331                  */
1332                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1333                 /*quirks*/DA_Q_NO_RC16
1334         },
1335 };
1336
1337 static  disk_strategy_t dastrategy;
1338 static  dumper_t        dadump;
1339 static  periph_init_t   dainit;
1340 static  void            daasync(void *callback_arg, u_int32_t code,
1341                                 struct cam_path *path, void *arg);
1342 static  void            dasysctlinit(void *context, int pending);
1343 static  int             dasysctlsofttimeout(SYSCTL_HANDLER_ARGS);
1344 static  int             dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1345 static  int             dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1346 static  int             dazonemodesysctl(SYSCTL_HANDLER_ARGS);
1347 static  int             dazonesupsysctl(SYSCTL_HANDLER_ARGS);
1348 static  int             dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1349 static  void            dadeletemethodset(struct da_softc *softc,
1350                                           da_delete_methods delete_method);
1351 static  off_t           dadeletemaxsize(struct da_softc *softc,
1352                                         da_delete_methods delete_method);
1353 static  void            dadeletemethodchoose(struct da_softc *softc,
1354                                              da_delete_methods default_method);
1355 static  void            daprobedone(struct cam_periph *periph, union ccb *ccb);
1356
1357 static  periph_ctor_t   daregister;
1358 static  periph_dtor_t   dacleanup;
1359 static  periph_start_t  dastart;
1360 static  periph_oninv_t  daoninvalidate;
1361 static  void            dazonedone(struct cam_periph *periph, union ccb *ccb);
1362 static  void            dadone(struct cam_periph *periph,
1363                                union ccb *done_ccb);
1364 static  int             daerror(union ccb *ccb, u_int32_t cam_flags,
1365                                 u_int32_t sense_flags);
1366 static void             daprevent(struct cam_periph *periph, int action);
1367 static void             dareprobe(struct cam_periph *periph);
1368 static void             dasetgeom(struct cam_periph *periph, uint32_t block_len,
1369                                   uint64_t maxsector,
1370                                   struct scsi_read_capacity_data_long *rcaplong,
1371                                   size_t rcap_size);
1372 static timeout_t        dasendorderedtag;
1373 static void             dashutdown(void *arg, int howto);
1374 static timeout_t        damediapoll;
1375
1376 #ifndef DA_DEFAULT_POLL_PERIOD
1377 #define DA_DEFAULT_POLL_PERIOD  3
1378 #endif
1379
1380 #ifndef DA_DEFAULT_TIMEOUT
1381 #define DA_DEFAULT_TIMEOUT 60   /* Timeout in seconds */
1382 #endif
1383
1384 #ifndef DA_DEFAULT_SOFTTIMEOUT
1385 #define DA_DEFAULT_SOFTTIMEOUT  0
1386 #endif
1387
1388 #ifndef DA_DEFAULT_RETRY
1389 #define DA_DEFAULT_RETRY        4
1390 #endif
1391
1392 #ifndef DA_DEFAULT_SEND_ORDERED
1393 #define DA_DEFAULT_SEND_ORDERED 1
1394 #endif
1395
1396 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1397 static int da_retry_count = DA_DEFAULT_RETRY;
1398 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1399 static sbintime_t da_default_softtimeout = DA_DEFAULT_SOFTTIMEOUT;
1400 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1401
1402 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1403             "CAM Direct Access Disk driver");
1404 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RWTUN,
1405            &da_poll_period, 0, "Media polling period in seconds");
1406 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RWTUN,
1407            &da_retry_count, 0, "Normal I/O retry count");
1408 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
1409            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1410 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
1411            &da_send_ordered, 0, "Send Ordered Tags");
1412
1413 SYSCTL_PROC(_kern_cam_da, OID_AUTO, default_softtimeout,
1414     CTLTYPE_UINT | CTLFLAG_RW, NULL, 0, dasysctlsofttimeout, "I",
1415     "Soft I/O timeout (ms)");
1416 TUNABLE_INT64("kern.cam.da.default_softtimeout", &da_default_softtimeout);
1417
1418 /*
1419  * DA_ORDEREDTAG_INTERVAL determines how often, relative
1420  * to the default timeout, we check to see whether an ordered
1421  * tagged transaction is appropriate to prevent simple tag
1422  * starvation.  Since we'd like to ensure that there is at least
1423  * 1/2 of the timeout length left for a starved transaction to
1424  * complete after we've sent an ordered tag, we must poll at least
1425  * four times in every timeout period.  This takes care of the worst
1426  * case where a starved transaction starts during an interval that
1427  * meets the requirement "don't send an ordered tag" test so it takes
1428  * us two intervals to determine that a tag must be sent.
1429  */
1430 #ifndef DA_ORDEREDTAG_INTERVAL
1431 #define DA_ORDEREDTAG_INTERVAL 4
1432 #endif
1433
1434 static struct periph_driver dadriver =
1435 {
1436         dainit, "da",
1437         TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1438 };
1439
1440 PERIPHDRIVER_DECLARE(da, dadriver);
1441
1442 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1443
1444 static int
1445 daopen(struct disk *dp)
1446 {
1447         struct cam_periph *periph;
1448         struct da_softc *softc;
1449         int error;
1450
1451         periph = (struct cam_periph *)dp->d_drv1;
1452         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1453                 return (ENXIO);
1454         }
1455
1456         cam_periph_lock(periph);
1457         if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1458                 cam_periph_unlock(periph);
1459                 cam_periph_release(periph);
1460                 return (error);
1461         }
1462
1463         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1464             ("daopen\n"));
1465
1466         softc = (struct da_softc *)periph->softc;
1467         dareprobe(periph);
1468
1469         /* Wait for the disk size update.  */
1470         error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1471             "dareprobe", 0);
1472         if (error != 0)
1473                 xpt_print(periph->path, "unable to retrieve capacity data\n");
1474
1475         if (periph->flags & CAM_PERIPH_INVALID)
1476                 error = ENXIO;
1477
1478         if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1479             (softc->quirks & DA_Q_NO_PREVENT) == 0)
1480                 daprevent(periph, PR_PREVENT);
1481
1482         if (error == 0) {
1483                 softc->flags &= ~DA_FLAG_PACK_INVALID;
1484                 softc->flags |= DA_FLAG_OPEN;
1485         }
1486
1487         cam_periph_unhold(periph);
1488         cam_periph_unlock(periph);
1489
1490         if (error != 0)
1491                 cam_periph_release(periph);
1492
1493         return (error);
1494 }
1495
1496 static int
1497 daclose(struct disk *dp)
1498 {
1499         struct  cam_periph *periph;
1500         struct  da_softc *softc;
1501         union   ccb *ccb;
1502         int error;
1503
1504         periph = (struct cam_periph *)dp->d_drv1;
1505         softc = (struct da_softc *)periph->softc;
1506         cam_periph_lock(periph);
1507         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1508             ("daclose\n"));
1509
1510         if (cam_periph_hold(periph, PRIBIO) == 0) {
1511
1512                 /* Flush disk cache. */
1513                 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1514                     (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1515                     (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1516                         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1517                         scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1518                             /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1519                             /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1520                             5 * 60 * 1000);
1521                         error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1522                             /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1523                             softc->disk->d_devstat);
1524                         softc->flags &= ~DA_FLAG_DIRTY;
1525                         xpt_release_ccb(ccb);
1526                 }
1527
1528                 /* Allow medium removal. */
1529                 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1530                     (softc->quirks & DA_Q_NO_PREVENT) == 0)
1531                         daprevent(periph, PR_ALLOW);
1532
1533                 cam_periph_unhold(periph);
1534         }
1535
1536         /*
1537          * If we've got removeable media, mark the blocksize as
1538          * unavailable, since it could change when new media is
1539          * inserted.
1540          */
1541         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1542                 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1543
1544         softc->flags &= ~DA_FLAG_OPEN;
1545         while (softc->refcount != 0)
1546                 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1547         cam_periph_unlock(periph);
1548         cam_periph_release(periph);
1549         return (0);
1550 }
1551
1552 static void
1553 daschedule(struct cam_periph *periph)
1554 {
1555         struct da_softc *softc = (struct da_softc *)periph->softc;
1556
1557         if (softc->state != DA_STATE_NORMAL)
1558                 return;
1559
1560         cam_iosched_schedule(softc->cam_iosched, periph);
1561 }
1562
1563 /*
1564  * Actually translate the requested transfer into one the physical driver
1565  * can understand.  The transfer is described by a buf and will include
1566  * only one physical transfer.
1567  */
1568 static void
1569 dastrategy(struct bio *bp)
1570 {
1571         struct cam_periph *periph;
1572         struct da_softc *softc;
1573         
1574         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1575         softc = (struct da_softc *)periph->softc;
1576
1577         cam_periph_lock(periph);
1578
1579         /*
1580          * If the device has been made invalid, error out
1581          */
1582         if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1583                 cam_periph_unlock(periph);
1584                 biofinish(bp, NULL, ENXIO);
1585                 return;
1586         }
1587
1588         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1589
1590         /*
1591          * Zone commands must be ordered, because they can depend on the
1592          * effects of previously issued commands, and they may affect
1593          * commands after them.
1594          */
1595         if (bp->bio_cmd == BIO_ZONE)
1596                 bp->bio_flags |= BIO_ORDERED;
1597
1598         /*
1599          * Place it in the queue of disk activities for this disk
1600          */
1601         cam_iosched_queue_work(softc->cam_iosched, bp);
1602
1603         /*
1604          * Schedule ourselves for performing the work.
1605          */
1606         daschedule(periph);
1607         cam_periph_unlock(periph);
1608
1609         return;
1610 }
1611
1612 static int
1613 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1614 {
1615         struct      cam_periph *periph;
1616         struct      da_softc *softc;
1617         u_int       secsize;
1618         struct      ccb_scsiio csio;
1619         struct      disk *dp;
1620         int         error = 0;
1621
1622         dp = arg;
1623         periph = dp->d_drv1;
1624         softc = (struct da_softc *)periph->softc;
1625         cam_periph_lock(periph);
1626         secsize = softc->params.secsize;
1627         
1628         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1629                 cam_periph_unlock(periph);
1630                 return (ENXIO);
1631         }
1632
1633         if (length > 0) {
1634                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1635                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1636                 scsi_read_write(&csio,
1637                                 /*retries*/0,
1638                                 dadone,
1639                                 MSG_ORDERED_Q_TAG,
1640                                 /*read*/SCSI_RW_WRITE,
1641                                 /*byte2*/0,
1642                                 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1643                                 offset / secsize,
1644                                 length / secsize,
1645                                 /*data_ptr*/(u_int8_t *) virtual,
1646                                 /*dxfer_len*/length,
1647                                 /*sense_len*/SSD_FULL_SIZE,
1648                                 da_default_timeout * 1000);
1649                 xpt_polled_action((union ccb *)&csio);
1650
1651                 error = cam_periph_error((union ccb *)&csio,
1652                     0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1653                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1654                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1655                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1656                 if (error != 0)
1657                         printf("Aborting dump due to I/O error.\n");
1658                 cam_periph_unlock(periph);
1659                 return (error);
1660         }
1661                 
1662         /*
1663          * Sync the disk cache contents to the physical media.
1664          */
1665         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1666
1667                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1668                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1669                 scsi_synchronize_cache(&csio,
1670                                        /*retries*/0,
1671                                        /*cbfcnp*/dadone,
1672                                        MSG_SIMPLE_Q_TAG,
1673                                        /*begin_lba*/0,/* Cover the whole disk */
1674                                        /*lb_count*/0,
1675                                        SSD_FULL_SIZE,
1676                                        5 * 1000);
1677                 xpt_polled_action((union ccb *)&csio);
1678
1679                 error = cam_periph_error((union ccb *)&csio,
1680                     0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1681                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1682                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1683                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1684                 if (error != 0)
1685                         xpt_print(periph->path, "Synchronize cache failed\n");
1686         }
1687         cam_periph_unlock(periph);
1688         return (error);
1689 }
1690
1691 static int
1692 dagetattr(struct bio *bp)
1693 {
1694         int ret;
1695         struct cam_periph *periph;
1696
1697         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1698         cam_periph_lock(periph);
1699         ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1700             periph->path);
1701         cam_periph_unlock(periph);
1702         if (ret == 0)
1703                 bp->bio_completed = bp->bio_length;
1704         return ret;
1705 }
1706
1707 static void
1708 dainit(void)
1709 {
1710         cam_status status;
1711
1712         /*
1713          * Install a global async callback.  This callback will
1714          * receive async callbacks like "new device found".
1715          */
1716         status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1717
1718         if (status != CAM_REQ_CMP) {
1719                 printf("da: Failed to attach master async callback "
1720                        "due to status 0x%x!\n", status);
1721         } else if (da_send_ordered) {
1722
1723                 /* Register our shutdown event handler */
1724                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 
1725                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1726                     printf("dainit: shutdown event registration failed!\n");
1727         }
1728 }
1729
1730 /*
1731  * Callback from GEOM, called when it has finished cleaning up its
1732  * resources.
1733  */
1734 static void
1735 dadiskgonecb(struct disk *dp)
1736 {
1737         struct cam_periph *periph;
1738
1739         periph = (struct cam_periph *)dp->d_drv1;
1740         cam_periph_release(periph);
1741 }
1742
1743 static void
1744 daoninvalidate(struct cam_periph *periph)
1745 {
1746         struct da_softc *softc;
1747
1748         softc = (struct da_softc *)periph->softc;
1749
1750         /*
1751          * De-register any async callbacks.
1752          */
1753         xpt_register_async(0, daasync, periph, periph->path);
1754
1755         softc->flags |= DA_FLAG_PACK_INVALID;
1756 #ifdef CAM_IO_STATS
1757         softc->invalidations++;
1758 #endif
1759
1760         /*
1761          * Return all queued I/O with ENXIO.
1762          * XXX Handle any transactions queued to the card
1763          *     with XPT_ABORT_CCB.
1764          */
1765         cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
1766
1767         /*
1768          * Tell GEOM that we've gone away, we'll get a callback when it is
1769          * done cleaning up its resources.
1770          */
1771         disk_gone(softc->disk);
1772 }
1773
1774 static void
1775 dacleanup(struct cam_periph *periph)
1776 {
1777         struct da_softc *softc;
1778
1779         softc = (struct da_softc *)periph->softc;
1780
1781         cam_periph_unlock(periph);
1782
1783         cam_iosched_fini(softc->cam_iosched);
1784
1785         /*
1786          * If we can't free the sysctl tree, oh well...
1787          */
1788         if ((softc->flags & DA_FLAG_SCTX_INIT) != 0) {
1789 #ifdef CAM_IO_STATS
1790                 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
1791                         xpt_print(periph->path,
1792                             "can't remove sysctl stats context\n");
1793 #endif
1794                 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
1795                         xpt_print(periph->path,
1796                             "can't remove sysctl context\n");
1797         }
1798
1799         callout_drain(&softc->mediapoll_c);
1800         disk_destroy(softc->disk);
1801         callout_drain(&softc->sendordered_c);
1802         free(softc, M_DEVBUF);
1803         cam_periph_lock(periph);
1804 }
1805
1806 static void
1807 daasync(void *callback_arg, u_int32_t code,
1808         struct cam_path *path, void *arg)
1809 {
1810         struct cam_periph *periph;
1811         struct da_softc *softc;
1812
1813         periph = (struct cam_periph *)callback_arg;
1814         switch (code) {
1815         case AC_FOUND_DEVICE:
1816         {
1817                 struct ccb_getdev *cgd;
1818                 cam_status status;
1819  
1820                 cgd = (struct ccb_getdev *)arg;
1821                 if (cgd == NULL)
1822                         break;
1823
1824                 if (cgd->protocol != PROTO_SCSI)
1825                         break;
1826                 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
1827                         break;
1828                 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1829                     && SID_TYPE(&cgd->inq_data) != T_RBC
1830                     && SID_TYPE(&cgd->inq_data) != T_OPTICAL
1831                     && SID_TYPE(&cgd->inq_data) != T_ZBC_HM)
1832                         break;
1833
1834                 /*
1835                  * Allocate a peripheral instance for
1836                  * this device and start the probe
1837                  * process.
1838                  */
1839                 status = cam_periph_alloc(daregister, daoninvalidate,
1840                                           dacleanup, dastart,
1841                                           "da", CAM_PERIPH_BIO,
1842                                           path, daasync,
1843                                           AC_FOUND_DEVICE, cgd);
1844
1845                 if (status != CAM_REQ_CMP
1846                  && status != CAM_REQ_INPROG)
1847                         printf("daasync: Unable to attach to new device "
1848                                 "due to status 0x%x\n", status);
1849                 return;
1850         }
1851         case AC_ADVINFO_CHANGED:
1852         {
1853                 uintptr_t buftype;
1854
1855                 buftype = (uintptr_t)arg;
1856                 if (buftype == CDAI_TYPE_PHYS_PATH) {
1857                         struct da_softc *softc;
1858
1859                         softc = periph->softc;
1860                         disk_attr_changed(softc->disk, "GEOM::physpath",
1861                                           M_NOWAIT);
1862                 }
1863                 break;
1864         }
1865         case AC_UNIT_ATTENTION:
1866         {
1867                 union ccb *ccb;
1868                 int error_code, sense_key, asc, ascq;
1869
1870                 softc = (struct da_softc *)periph->softc;
1871                 ccb = (union ccb *)arg;
1872
1873                 /*
1874                  * Handle all UNIT ATTENTIONs except our own,
1875                  * as they will be handled by daerror().
1876                  */
1877                 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1878                     scsi_extract_sense_ccb(ccb,
1879                      &error_code, &sense_key, &asc, &ascq)) {
1880                         if (asc == 0x2A && ascq == 0x09) {
1881                                 xpt_print(ccb->ccb_h.path,
1882                                     "Capacity data has changed\n");
1883                                 softc->flags &= ~DA_FLAG_PROBED;
1884                                 dareprobe(periph);
1885                         } else if (asc == 0x28 && ascq == 0x00) {
1886                                 softc->flags &= ~DA_FLAG_PROBED;
1887                                 disk_media_changed(softc->disk, M_NOWAIT);
1888                         } else if (asc == 0x3F && ascq == 0x03) {
1889                                 xpt_print(ccb->ccb_h.path,
1890                                     "INQUIRY data has changed\n");
1891                                 softc->flags &= ~DA_FLAG_PROBED;
1892                                 dareprobe(periph);
1893                         }
1894                 }
1895                 cam_periph_async(periph, code, path, arg);
1896                 break;
1897         }
1898         case AC_SCSI_AEN:
1899                 softc = (struct da_softc *)periph->softc;
1900                 if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
1901                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1902                                 cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
1903                                 daschedule(periph);
1904                         }
1905                 }
1906                 /* FALLTHROUGH */
1907         case AC_SENT_BDR:
1908         case AC_BUS_RESET:
1909         {
1910                 struct ccb_hdr *ccbh;
1911
1912                 softc = (struct da_softc *)periph->softc;
1913                 /*
1914                  * Don't fail on the expected unit attention
1915                  * that will occur.
1916                  */
1917                 softc->flags |= DA_FLAG_RETRY_UA;
1918                 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1919                         ccbh->ccb_state |= DA_CCB_RETRY_UA;
1920                 break;
1921         }
1922         case AC_INQ_CHANGED:
1923                 softc = (struct da_softc *)periph->softc;
1924                 softc->flags &= ~DA_FLAG_PROBED;
1925                 dareprobe(periph);
1926                 break;
1927         default:
1928                 break;
1929         }
1930         cam_periph_async(periph, code, path, arg);
1931 }
1932
1933 static void
1934 dasysctlinit(void *context, int pending)
1935 {
1936         struct cam_periph *periph;
1937         struct da_softc *softc;
1938         char tmpstr[80], tmpstr2[80];
1939         struct ccb_trans_settings cts;
1940
1941         periph = (struct cam_periph *)context;
1942         /*
1943          * periph was held for us when this task was enqueued
1944          */
1945         if (periph->flags & CAM_PERIPH_INVALID) {
1946                 cam_periph_release(periph);
1947                 return;
1948         }
1949
1950         softc = (struct da_softc *)periph->softc;
1951         snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1952         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1953
1954         sysctl_ctx_init(&softc->sysctl_ctx);
1955         softc->flags |= DA_FLAG_SCTX_INIT;
1956         softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
1957                 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1958                 CTLFLAG_RD, 0, tmpstr, "device_index");
1959         if (softc->sysctl_tree == NULL) {
1960                 printf("dasysctlinit: unable to allocate sysctl tree\n");
1961                 cam_periph_release(periph);
1962                 return;
1963         }
1964
1965         /*
1966          * Now register the sysctl handler, so the user can change the value on
1967          * the fly.
1968          */
1969         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1970                 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RWTUN,
1971                 softc, 0, dadeletemethodsysctl, "A",
1972                 "BIO_DELETE execution method");
1973         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1974                 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1975                 softc, 0, dadeletemaxsysctl, "Q",
1976                 "Maximum BIO_DELETE size");
1977         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1978                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1979                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1980                 "Minimum CDB size");
1981
1982         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1983                 OID_AUTO, "zone_mode", CTLTYPE_STRING | CTLFLAG_RD,
1984                 softc, 0, dazonemodesysctl, "A",
1985                 "Zone Mode");
1986         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1987                 OID_AUTO, "zone_support", CTLTYPE_STRING | CTLFLAG_RD,
1988                 softc, 0, dazonesupsysctl, "A",
1989                 "Zone Support");
1990         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1991                 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1992                 "optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones,
1993                 "Optimal Number of Open Sequential Write Preferred Zones");
1994         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1995                 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1996                 "optimal_nonseq_zones", CTLFLAG_RD,
1997                 &softc->optimal_nonseq_zones,
1998                 "Optimal Number of Non-Sequentially Written Sequential Write "
1999                 "Preferred Zones");
2000         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2001                 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2002                 "max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones,
2003                 "Maximum Number of Open Sequential Write Required Zones");
2004
2005         SYSCTL_ADD_INT(&softc->sysctl_ctx,
2006                        SYSCTL_CHILDREN(softc->sysctl_tree),
2007                        OID_AUTO,
2008                        "error_inject",
2009                        CTLFLAG_RW,
2010                        &softc->error_inject,
2011                        0,
2012                        "error_inject leaf");
2013
2014         SYSCTL_ADD_INT(&softc->sysctl_ctx,
2015                        SYSCTL_CHILDREN(softc->sysctl_tree),
2016                        OID_AUTO,
2017                        "unmapped_io",
2018                        CTLFLAG_RD, 
2019                        &softc->unmappedio,
2020                        0,
2021                        "Unmapped I/O leaf");
2022
2023         SYSCTL_ADD_INT(&softc->sysctl_ctx,
2024                        SYSCTL_CHILDREN(softc->sysctl_tree),
2025                        OID_AUTO,
2026                        "rotating",
2027                        CTLFLAG_RD, 
2028                        &softc->rotating,
2029                        0,
2030                        "Rotating media");
2031
2032         /*
2033          * Add some addressing info.
2034          */
2035         memset(&cts, 0, sizeof (cts));
2036         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
2037         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2038         cts.type = CTS_TYPE_CURRENT_SETTINGS;
2039         cam_periph_lock(periph);
2040         xpt_action((union ccb *)&cts);
2041         cam_periph_unlock(periph);
2042         if (cts.ccb_h.status != CAM_REQ_CMP) {
2043                 cam_periph_release(periph);
2044                 return;
2045         }
2046         if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
2047                 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
2048                 if (fc->valid & CTS_FC_VALID_WWPN) {
2049                         softc->wwpn = fc->wwpn;
2050                         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2051                             SYSCTL_CHILDREN(softc->sysctl_tree),
2052                             OID_AUTO, "wwpn", CTLFLAG_RD,
2053                             &softc->wwpn, "World Wide Port Name");
2054                 }
2055         }
2056
2057 #ifdef CAM_IO_STATS
2058         /*
2059          * Now add some useful stats.
2060          * XXX These should live in cam_periph and be common to all periphs
2061          */
2062         softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
2063             SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
2064             CTLFLAG_RD, 0, "Statistics");
2065         SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2066                        SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2067                        OID_AUTO,
2068                        "errors",
2069                        CTLFLAG_RD,
2070                        &softc->errors,
2071                        0,
2072                        "Transport errors reported by the SIM");
2073         SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2074                        SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2075                        OID_AUTO,
2076                        "timeouts",
2077                        CTLFLAG_RD,
2078                        &softc->timeouts,
2079                        0,
2080                        "Device timeouts reported by the SIM");
2081         SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2082                        SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2083                        OID_AUTO,
2084                        "pack_invalidations",
2085                        CTLFLAG_RD,
2086                        &softc->invalidations,
2087                        0,
2088                        "Device pack invalidations");
2089 #endif
2090
2091         cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
2092             softc->sysctl_tree);
2093
2094         cam_periph_release(periph);
2095 }
2096
2097 static int
2098 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
2099 {
2100         int error;
2101         uint64_t value;
2102         struct da_softc *softc;
2103
2104         softc = (struct da_softc *)arg1;
2105
2106         value = softc->disk->d_delmaxsize;
2107         error = sysctl_handle_64(oidp, &value, 0, req);
2108         if ((error != 0) || (req->newptr == NULL))
2109                 return (error);
2110
2111         /* only accept values smaller than the calculated value */
2112         if (value > dadeletemaxsize(softc, softc->delete_method)) {
2113                 return (EINVAL);
2114         }
2115         softc->disk->d_delmaxsize = value;
2116
2117         return (0);
2118 }
2119
2120 static int
2121 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
2122 {
2123         int error, value;
2124
2125         value = *(int *)arg1;
2126
2127         error = sysctl_handle_int(oidp, &value, 0, req);
2128
2129         if ((error != 0)
2130          || (req->newptr == NULL))
2131                 return (error);
2132
2133         /*
2134          * Acceptable values here are 6, 10, 12 or 16.
2135          */
2136         if (value < 6)
2137                 value = 6;
2138         else if ((value > 6)
2139               && (value <= 10))
2140                 value = 10;
2141         else if ((value > 10)
2142               && (value <= 12))
2143                 value = 12;
2144         else if (value > 12)
2145                 value = 16;
2146
2147         *(int *)arg1 = value;
2148
2149         return (0);
2150 }
2151
2152 static int
2153 dasysctlsofttimeout(SYSCTL_HANDLER_ARGS)
2154 {
2155         sbintime_t value;
2156         int error;
2157
2158         value = da_default_softtimeout / SBT_1MS;
2159
2160         error = sysctl_handle_int(oidp, (int *)&value, 0, req);
2161         if ((error != 0) || (req->newptr == NULL))
2162                 return (error);
2163
2164         /* XXX Should clip this to a reasonable level */
2165         if (value > da_default_timeout * 1000)
2166                 return (EINVAL);
2167
2168         da_default_softtimeout = value * SBT_1MS;
2169         return (0);
2170 }
2171
2172 static void
2173 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
2174 {
2175
2176         softc->delete_method = delete_method;
2177         softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
2178         softc->delete_func = da_delete_functions[delete_method];
2179
2180         if (softc->delete_method > DA_DELETE_DISABLE)
2181                 softc->disk->d_flags |= DISKFLAG_CANDELETE;
2182         else
2183                 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2184 }
2185
2186 static off_t
2187 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
2188 {
2189         off_t sectors;
2190
2191         switch(delete_method) {
2192         case DA_DELETE_UNMAP:
2193                 sectors = (off_t)softc->unmap_max_lba;
2194                 break;
2195         case DA_DELETE_ATA_TRIM:
2196                 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
2197                 break;
2198         case DA_DELETE_WS16:
2199                 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
2200                 break;
2201         case DA_DELETE_ZERO:
2202         case DA_DELETE_WS10:
2203                 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
2204                 break;
2205         default:
2206                 return 0;
2207         }
2208
2209         return (off_t)softc->params.secsize *
2210             omin(sectors, softc->params.sectors);
2211 }
2212
2213 static void
2214 daprobedone(struct cam_periph *periph, union ccb *ccb)
2215 {
2216         struct da_softc *softc;
2217
2218         softc = (struct da_softc *)periph->softc;
2219
2220         dadeletemethodchoose(softc, DA_DELETE_NONE);
2221
2222         if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2223                 char buf[80];
2224                 int i, sep;
2225
2226                 snprintf(buf, sizeof(buf), "Delete methods: <");
2227                 sep = 0;
2228                 for (i = 0; i <= DA_DELETE_MAX; i++) {
2229                         if ((softc->delete_available & (1 << i)) == 0 &&
2230                             i != softc->delete_method)
2231                                 continue;
2232                         if (sep)
2233                                 strlcat(buf, ",", sizeof(buf));
2234                         strlcat(buf, da_delete_method_names[i],
2235                             sizeof(buf));
2236                         if (i == softc->delete_method)
2237                                 strlcat(buf, "(*)", sizeof(buf));
2238                         sep = 1;
2239                 }
2240                 strlcat(buf, ">", sizeof(buf));
2241                 printf("%s%d: %s\n", periph->periph_name,
2242                     periph->unit_number, buf);
2243         }
2244
2245         /*
2246          * Since our peripheral may be invalidated by an error
2247          * above or an external event, we must release our CCB
2248          * before releasing the probe lock on the peripheral.
2249          * The peripheral will only go away once the last lock
2250          * is removed, and we need it around for the CCB release
2251          * operation.
2252          */
2253         xpt_release_ccb(ccb);
2254         softc->state = DA_STATE_NORMAL;
2255         softc->flags |= DA_FLAG_PROBED;
2256         daschedule(periph);
2257         wakeup(&softc->disk->d_mediasize);
2258         if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2259                 softc->flags |= DA_FLAG_ANNOUNCED;
2260                 cam_periph_unhold(periph);
2261         } else
2262                 cam_periph_release_locked(periph);
2263 }
2264
2265 static void
2266 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2267 {
2268         int i, methods;
2269
2270         /* If available, prefer the method requested by user. */
2271         i = softc->delete_method_pref;
2272         methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2273         if (methods & (1 << i)) {
2274                 dadeletemethodset(softc, i);
2275                 return;
2276         }
2277
2278         /* Use the pre-defined order to choose the best performing delete. */
2279         for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2280                 if (i == DA_DELETE_ZERO)
2281                         continue;
2282                 if (softc->delete_available & (1 << i)) {
2283                         dadeletemethodset(softc, i);
2284                         return;
2285                 }
2286         }
2287
2288         /* Fallback to default. */
2289         dadeletemethodset(softc, default_method);
2290 }
2291
2292 static int
2293 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2294 {
2295         char buf[16];
2296         const char *p;
2297         struct da_softc *softc;
2298         int i, error, methods, value;
2299
2300         softc = (struct da_softc *)arg1;
2301
2302         value = softc->delete_method;
2303         if (value < 0 || value > DA_DELETE_MAX)
2304                 p = "UNKNOWN";
2305         else
2306                 p = da_delete_method_names[value];
2307         strncpy(buf, p, sizeof(buf));
2308         error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2309         if (error != 0 || req->newptr == NULL)
2310                 return (error);
2311         methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2312         for (i = 0; i <= DA_DELETE_MAX; i++) {
2313                 if (strcmp(buf, da_delete_method_names[i]) == 0)
2314                         break;
2315         }
2316         if (i > DA_DELETE_MAX)
2317                 return (EINVAL);
2318         softc->delete_method_pref = i;
2319         dadeletemethodchoose(softc, DA_DELETE_NONE);
2320         return (0);
2321 }
2322
2323 static int
2324 dazonemodesysctl(SYSCTL_HANDLER_ARGS)
2325 {
2326         char tmpbuf[40];
2327         struct da_softc *softc;
2328         int error;
2329
2330         softc = (struct da_softc *)arg1;
2331
2332         switch (softc->zone_mode) {
2333         case DA_ZONE_DRIVE_MANAGED:
2334                 snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed");
2335                 break;
2336         case DA_ZONE_HOST_AWARE:
2337                 snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware");
2338                 break;
2339         case DA_ZONE_HOST_MANAGED:
2340                 snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed");
2341                 break;
2342         case DA_ZONE_NONE:
2343         default:
2344                 snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned");
2345                 break;
2346         }
2347
2348         error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req);
2349
2350         return (error);
2351 }
2352
2353 static int
2354 dazonesupsysctl(SYSCTL_HANDLER_ARGS)
2355 {
2356         char tmpbuf[180];
2357         struct da_softc *softc;
2358         struct sbuf sb;
2359         int error, first;
2360         unsigned int i;
2361
2362         softc = (struct da_softc *)arg1;
2363
2364         error = 0;
2365         first = 1;
2366         sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0);
2367
2368         for (i = 0; i < sizeof(da_zone_desc_table) /
2369              sizeof(da_zone_desc_table[0]); i++) {
2370                 if (softc->zone_flags & da_zone_desc_table[i].value) {
2371                         if (first == 0)
2372                                 sbuf_printf(&sb, ", ");
2373                         else
2374                                 first = 0;
2375                         sbuf_cat(&sb, da_zone_desc_table[i].desc);
2376                 }
2377         }
2378
2379         if (first == 1)
2380                 sbuf_printf(&sb, "None");
2381
2382         sbuf_finish(&sb);
2383
2384         error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
2385
2386         return (error);
2387 }
2388
2389 static cam_status
2390 daregister(struct cam_periph *periph, void *arg)
2391 {
2392         struct da_softc *softc;
2393         struct ccb_pathinq cpi;
2394         struct ccb_getdev *cgd;
2395         char tmpstr[80];
2396         caddr_t match;
2397
2398         cgd = (struct ccb_getdev *)arg;
2399         if (cgd == NULL) {
2400                 printf("daregister: no getdev CCB, can't register device\n");
2401                 return(CAM_REQ_CMP_ERR);
2402         }
2403
2404         softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2405             M_NOWAIT|M_ZERO);
2406
2407         if (softc == NULL) {
2408                 printf("daregister: Unable to probe new device. "
2409                        "Unable to allocate softc\n");
2410                 return(CAM_REQ_CMP_ERR);
2411         }
2412
2413         if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
2414                 printf("daregister: Unable to probe new device. "
2415                        "Unable to allocate iosched memory\n");
2416                 free(softc, M_DEVBUF);
2417                 return(CAM_REQ_CMP_ERR);
2418         }
2419         
2420         LIST_INIT(&softc->pending_ccbs);
2421         softc->state = DA_STATE_PROBE_RC;
2422         bioq_init(&softc->delete_run_queue);
2423         if (SID_IS_REMOVABLE(&cgd->inq_data))
2424                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2425         softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2426         softc->unmap_max_lba = UNMAP_RANGE_MAX;
2427         softc->unmap_gran = 0;
2428         softc->unmap_gran_align = 0;
2429         softc->ws_max_blks = WS16_MAX_BLKS;
2430         softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2431         softc->rotating = 1;
2432
2433         periph->softc = softc;
2434
2435         /*
2436          * See if this device has any quirks.
2437          */
2438         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2439                                (caddr_t)da_quirk_table,
2440                                nitems(da_quirk_table),
2441                                sizeof(*da_quirk_table), scsi_inquiry_match);
2442
2443         if (match != NULL)
2444                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2445         else
2446                 softc->quirks = DA_Q_NONE;
2447
2448         /* Check if the SIM does not want 6 byte commands */
2449         bzero(&cpi, sizeof(cpi));
2450         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2451         cpi.ccb_h.func_code = XPT_PATH_INQ;
2452         xpt_action((union ccb *)&cpi);
2453         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2454                 softc->quirks |= DA_Q_NO_6_BYTE;
2455
2456         if (SID_TYPE(&cgd->inq_data) == T_ZBC_HM)
2457                 softc->zone_mode = DA_ZONE_HOST_MANAGED;
2458         else if (softc->quirks & DA_Q_SMR_DM)
2459                 softc->zone_mode = DA_ZONE_DRIVE_MANAGED;
2460         else
2461                 softc->zone_mode = DA_ZONE_NONE;
2462
2463         if (softc->zone_mode != DA_ZONE_NONE) {
2464                 if (scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2465                         if (scsi_vpd_supported_page(periph, SVPD_ZONED_BDC))
2466                                 softc->zone_interface = DA_ZONE_IF_ATA_SAT;
2467                         else
2468                                 softc->zone_interface = DA_ZONE_IF_ATA_PASS;
2469                 } else
2470                         softc->zone_interface = DA_ZONE_IF_SCSI;
2471         }
2472
2473         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2474
2475         /*
2476          * Take an exclusive refcount on the periph while dastart is called
2477          * to finish the probe.  The reference will be dropped in dadone at
2478          * the end of probe.
2479          */
2480         (void)cam_periph_hold(periph, PRIBIO);
2481
2482         /*
2483          * Schedule a periodic event to occasionally send an
2484          * ordered tag to a device.
2485          */
2486         callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2487         callout_reset(&softc->sendordered_c,
2488             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2489             dasendorderedtag, softc);
2490
2491         cam_periph_unlock(periph);
2492         /*
2493          * RBC devices don't have to support READ(6), only READ(10).
2494          */
2495         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2496                 softc->minimum_cmd_size = 10;
2497         else
2498                 softc->minimum_cmd_size = 6;
2499
2500         /*
2501          * Load the user's default, if any.
2502          */
2503         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2504                  periph->unit_number);
2505         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2506
2507         /*
2508          * 6, 10, 12 and 16 are the currently permissible values.
2509          */
2510         if (softc->minimum_cmd_size > 12)
2511                 softc->minimum_cmd_size = 16;
2512         else if (softc->minimum_cmd_size > 10)
2513                 softc->minimum_cmd_size = 12;
2514         else if (softc->minimum_cmd_size > 6)
2515                 softc->minimum_cmd_size = 10;
2516         else
2517                 softc->minimum_cmd_size = 6;
2518                 
2519         /* Predict whether device may support READ CAPACITY(16). */
2520         if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2521             (softc->quirks & DA_Q_NO_RC16) == 0) {
2522                 softc->flags |= DA_FLAG_CAN_RC16;
2523                 softc->state = DA_STATE_PROBE_RC16;
2524         }
2525
2526         /*
2527          * Register this media as a disk.
2528          */
2529         softc->disk = disk_alloc();
2530         softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2531                           periph->unit_number, 0,
2532                           DEVSTAT_BS_UNAVAILABLE,
2533                           SID_TYPE(&cgd->inq_data) |
2534                           XPORT_DEVSTAT_TYPE(cpi.transport),
2535                           DEVSTAT_PRIORITY_DISK);
2536         softc->disk->d_open = daopen;
2537         softc->disk->d_close = daclose;
2538         softc->disk->d_strategy = dastrategy;
2539         softc->disk->d_dump = dadump;
2540         softc->disk->d_getattr = dagetattr;
2541         softc->disk->d_gone = dadiskgonecb;
2542         softc->disk->d_name = "da";
2543         softc->disk->d_drv1 = periph;
2544         if (cpi.maxio == 0)
2545                 softc->maxio = DFLTPHYS;        /* traditional default */
2546         else if (cpi.maxio > MAXPHYS)
2547                 softc->maxio = MAXPHYS;         /* for safety */
2548         else
2549                 softc->maxio = cpi.maxio;
2550         softc->disk->d_maxsize = softc->maxio;
2551         softc->disk->d_unit = periph->unit_number;
2552         softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
2553         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2554                 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2555         if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
2556                 softc->unmappedio = 1;
2557                 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2558         }
2559         cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2560             sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2561         strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2562         cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2563             cgd->inq_data.product, sizeof(cgd->inq_data.product),
2564             sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2565         softc->disk->d_hba_vendor = cpi.hba_vendor;
2566         softc->disk->d_hba_device = cpi.hba_device;
2567         softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2568         softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2569
2570         /*
2571          * Acquire a reference to the periph before we register with GEOM.
2572          * We'll release this reference once GEOM calls us back (via
2573          * dadiskgonecb()) telling us that our provider has been freed.
2574          */
2575         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2576                 xpt_print(periph->path, "%s: lost periph during "
2577                           "registration!\n", __func__);
2578                 cam_periph_lock(periph);
2579                 return (CAM_REQ_CMP_ERR);
2580         }
2581
2582         disk_create(softc->disk, DISK_VERSION);
2583         cam_periph_lock(periph);
2584
2585         /*
2586          * Add async callbacks for events of interest.
2587          * I don't bother checking if this fails as,
2588          * in most cases, the system will function just
2589          * fine without them and the only alternative
2590          * would be to not attach the device on failure.
2591          */
2592         xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2593             AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
2594             AC_INQ_CHANGED, daasync, periph, periph->path);
2595
2596         /*
2597          * Emit an attribute changed notification just in case 
2598          * physical path information arrived before our async
2599          * event handler was registered, but after anyone attaching
2600          * to our disk device polled it.
2601          */
2602         disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2603
2604         /*
2605          * Schedule a periodic media polling events.
2606          */
2607         callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2608         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2609             (cgd->inq_flags & SID_AEN) == 0 &&
2610             da_poll_period != 0)
2611                 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2612                     damediapoll, periph);
2613
2614         xpt_schedule(periph, CAM_PRIORITY_DEV);
2615
2616         return(CAM_REQ_CMP);
2617 }
2618
2619 static int
2620 da_zone_bio_to_scsi(int disk_zone_cmd)
2621 {
2622         switch (disk_zone_cmd) {
2623         case DISK_ZONE_OPEN:
2624                 return ZBC_OUT_SA_OPEN;
2625         case DISK_ZONE_CLOSE:
2626                 return ZBC_OUT_SA_CLOSE;
2627         case DISK_ZONE_FINISH:
2628                 return ZBC_OUT_SA_FINISH;
2629         case DISK_ZONE_RWP:
2630                 return ZBC_OUT_SA_RWP;
2631         }
2632
2633         return -1;
2634 }
2635
2636 static int
2637 da_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
2638             int *queue_ccb)
2639 {
2640         struct da_softc *softc;
2641         int error;
2642
2643         error = 0;
2644
2645         if (bp->bio_cmd != BIO_ZONE) {
2646                 error = EINVAL;
2647                 goto bailout;
2648         }
2649
2650         softc = periph->softc;
2651
2652         switch (bp->bio_zone.zone_cmd) {
2653         case DISK_ZONE_OPEN:
2654         case DISK_ZONE_CLOSE:
2655         case DISK_ZONE_FINISH:
2656         case DISK_ZONE_RWP: {
2657                 int zone_flags;
2658                 int zone_sa;
2659                 uint64_t lba;
2660
2661                 zone_sa = da_zone_bio_to_scsi(bp->bio_zone.zone_cmd);
2662                 if (zone_sa == -1) {
2663                         xpt_print(periph->path, "Cannot translate zone "
2664                             "cmd %#x to SCSI\n", bp->bio_zone.zone_cmd);
2665                         error = EINVAL;
2666                         goto bailout;
2667                 }
2668
2669                 zone_flags = 0;
2670                 lba = bp->bio_zone.zone_params.rwp.id;
2671
2672                 if (bp->bio_zone.zone_params.rwp.flags &
2673                     DISK_ZONE_RWP_FLAG_ALL)
2674                         zone_flags |= ZBC_OUT_ALL;
2675
2676                 if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
2677                         scsi_zbc_out(&ccb->csio,
2678                                      /*retries*/ da_retry_count,
2679                                      /*cbfcnp*/ dadone,
2680                                      /*tag_action*/ MSG_SIMPLE_Q_TAG,
2681                                      /*service_action*/ zone_sa,
2682                                      /*zone_id*/ lba,
2683                                      /*zone_flags*/ zone_flags,
2684                                      /*data_ptr*/ NULL,
2685                                      /*dxfer_len*/ 0,
2686                                      /*sense_len*/ SSD_FULL_SIZE,
2687                                      /*timeout*/ da_default_timeout * 1000);
2688                 } else {
2689                         /*
2690                          * Note that in this case, even though we can
2691                          * technically use NCQ, we don't bother for several
2692                          * reasons:
2693                          * 1. It hasn't been tested on a SAT layer that
2694                          *    supports it.  This is new as of SAT-4.
2695                          * 2. Even when there is a SAT layer that supports
2696                          *    it, that SAT layer will also probably support
2697                          *    ZBC -> ZAC translation, since they are both
2698                          *    in the SAT-4 spec.
2699                          * 3. Translation will likely be preferable to ATA
2700                          *    passthrough.  LSI / Avago at least single
2701                          *    steps ATA passthrough commands in the HBA,
2702                          *    regardless of protocol, so unless that
2703                          *    changes, there is a performance penalty for
2704                          *    doing ATA passthrough no matter whether
2705                          *    you're using NCQ/FPDMA, DMA or PIO.
2706                          * 4. It requires a 32-byte CDB, which at least at
2707                          *    this point in CAM requires a CDB pointer, which
2708                          *    would require us to allocate an additional bit
2709                          *    of storage separate from the CCB.
2710                          */
2711                         error = scsi_ata_zac_mgmt_out(&ccb->csio,
2712                             /*retries*/ da_retry_count,
2713                             /*cbfcnp*/ dadone,
2714                             /*tag_action*/ MSG_SIMPLE_Q_TAG,
2715                             /*use_ncq*/ 0,
2716                             /*zm_action*/ zone_sa,
2717                             /*zone_id*/ lba,
2718                             /*zone_flags*/ zone_flags,
2719                             /*data_ptr*/ NULL,
2720                             /*dxfer_len*/ 0,
2721                             /*cdb_storage*/ NULL,
2722                             /*cdb_storage_len*/ 0,
2723                             /*sense_len*/ SSD_FULL_SIZE,
2724                             /*timeout*/ da_default_timeout * 1000);
2725                         if (error != 0) {
2726                                 error = EINVAL;
2727                                 xpt_print(periph->path,
2728                                     "scsi_ata_zac_mgmt_out() returned an "
2729                                     "error!");
2730                                 goto bailout;
2731                         }
2732                 }
2733                 *queue_ccb = 1;
2734
2735                 break;
2736         }
2737         case DISK_ZONE_REPORT_ZONES: {
2738                 uint8_t *rz_ptr;
2739                 uint32_t num_entries, alloc_size;
2740                 struct disk_zone_report *rep;
2741
2742                 rep = &bp->bio_zone.zone_params.report;
2743
2744                 num_entries = rep->entries_allocated;
2745                 if (num_entries == 0) {
2746                         xpt_print(periph->path, "No entries allocated for "
2747                             "Report Zones request\n");
2748                         error = EINVAL;
2749                         goto bailout;
2750                 }
2751                 alloc_size = sizeof(struct scsi_report_zones_hdr) +
2752                     (sizeof(struct scsi_report_zones_desc) * num_entries);
2753                 alloc_size = min(alloc_size, softc->disk->d_maxsize);
2754                 rz_ptr = malloc(alloc_size, M_SCSIDA, M_NOWAIT | M_ZERO);
2755                 if (rz_ptr == NULL) {
2756                         xpt_print(periph->path, "Unable to allocate memory "
2757                            "for Report Zones request\n");
2758                         error = ENOMEM;
2759                         goto bailout;
2760                 }
2761                 
2762                 if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
2763                         scsi_zbc_in(&ccb->csio,
2764                                     /*retries*/ da_retry_count,
2765                                     /*cbcfnp*/ dadone,
2766                                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
2767                                     /*service_action*/ ZBC_IN_SA_REPORT_ZONES,
2768                                     /*zone_start_lba*/ rep->starting_id,
2769                                     /*zone_options*/ rep->rep_options,
2770                                     /*data_ptr*/ rz_ptr,
2771                                     /*dxfer_len*/ alloc_size,
2772                                     /*sense_len*/ SSD_FULL_SIZE,
2773                                     /*timeout*/ da_default_timeout * 1000);
2774                 } else {
2775                         /*
2776                          * Note that in this case, even though we can
2777                          * technically use NCQ, we don't bother for several
2778                          * reasons:
2779                          * 1. It hasn't been tested on a SAT layer that
2780                          *    supports it.  This is new as of SAT-4.
2781                          * 2. Even when there is a SAT layer that supports
2782                          *    it, that SAT layer will also probably support
2783                          *    ZBC -> ZAC translation, since they are both
2784                          *    in the SAT-4 spec.
2785                          * 3. Translation will likely be preferable to ATA
2786                          *    passthrough.  LSI / Avago at least single
2787                          *    steps ATA passthrough commands in the HBA,
2788                          *    regardless of protocol, so unless that
2789                          *    changes, there is a performance penalty for
2790                          *    doing ATA passthrough no matter whether
2791                          *    you're using NCQ/FPDMA, DMA or PIO.
2792                          * 4. It requires a 32-byte CDB, which at least at
2793                          *    this point in CAM requires a CDB pointer, which
2794                          *    would require us to allocate an additional bit
2795                          *    of storage separate from the CCB.
2796                          */
2797                         error = scsi_ata_zac_mgmt_in(&ccb->csio,
2798                             /*retries*/ da_retry_count,
2799                             /*cbcfnp*/ dadone,
2800                             /*tag_action*/ MSG_SIMPLE_Q_TAG,
2801                             /*use_ncq*/ 0,
2802                             /*zm_action*/ ATA_ZM_REPORT_ZONES,
2803                             /*zone_id*/ rep->starting_id,
2804                             /*zone_flags*/ rep->rep_options,
2805                             /*data_ptr*/ rz_ptr,
2806                             /*dxfer_len*/ alloc_size,
2807                             /*cdb_storage*/ NULL,
2808                             /*cdb_storage_len*/ 0,
2809                             /*sense_len*/ SSD_FULL_SIZE,
2810                             /*timeout*/ da_default_timeout * 1000);
2811                         if (error != 0) {
2812                                 error = EINVAL;
2813                                 xpt_print(periph->path,
2814                                     "scsi_ata_zac_mgmt_in() returned an "
2815                                     "error!");
2816                                 goto bailout;
2817                         }
2818                 }
2819
2820                 /*
2821                  * For BIO_ZONE, this isn't normally needed.  However, it
2822                  * is used by devstat_end_transaction_bio() to determine
2823                  * how much data was transferred.
2824                  */
2825                 /*
2826                  * XXX KDM we have a problem.  But I'm not sure how to fix
2827                  * it.  devstat uses bio_bcount - bio_resid to calculate
2828                  * the amount of data transferred.   The GEOM disk code
2829                  * uses bio_length - bio_resid to calculate the amount of
2830                  * data in bio_completed.  We have different structure
2831                  * sizes above and below the ada(4) driver.  So, if we
2832                  * use the sizes above, the amount transferred won't be
2833                  * quite accurate for devstat.  If we use different sizes
2834                  * for bio_bcount and bio_length (above and below
2835                  * respectively), then the residual needs to match one or
2836                  * the other.  Everything is calculated after the bio
2837                  * leaves the driver, so changing the values around isn't
2838                  * really an option.  For now, just set the count to the
2839                  * passed in length.  This means that the calculations
2840                  * above (e.g. bio_completed) will be correct, but the
2841                  * amount of data reported to devstat will be slightly
2842                  * under or overstated.
2843                  */
2844                 bp->bio_bcount = bp->bio_length;
2845
2846                 *queue_ccb = 1;
2847
2848                 break;
2849         }
2850         case DISK_ZONE_GET_PARAMS: {
2851                 struct disk_zone_disk_params *params;
2852
2853                 params = &bp->bio_zone.zone_params.disk_params;
2854                 bzero(params, sizeof(*params));
2855
2856                 switch (softc->zone_mode) {
2857                 case DA_ZONE_DRIVE_MANAGED:
2858                         params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
2859                         break;
2860                 case DA_ZONE_HOST_AWARE:
2861                         params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
2862                         break;
2863                 case DA_ZONE_HOST_MANAGED:
2864                         params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
2865                         break;
2866                 default:
2867                 case DA_ZONE_NONE:
2868                         params->zone_mode = DISK_ZONE_MODE_NONE;
2869                         break;
2870                 }
2871
2872                 if (softc->zone_flags & DA_ZONE_FLAG_URSWRZ)
2873                         params->flags |= DISK_ZONE_DISK_URSWRZ;
2874
2875                 if (softc->zone_flags & DA_ZONE_FLAG_OPT_SEQ_SET) {
2876                         params->optimal_seq_zones = softc->optimal_seq_zones;
2877                         params->flags |= DISK_ZONE_OPT_SEQ_SET;
2878                 }
2879
2880                 if (softc->zone_flags & DA_ZONE_FLAG_OPT_NONSEQ_SET) {
2881                         params->optimal_nonseq_zones =
2882                             softc->optimal_nonseq_zones;
2883                         params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
2884                 }
2885
2886                 if (softc->zone_flags & DA_ZONE_FLAG_MAX_SEQ_SET) {
2887                         params->max_seq_zones = softc->max_seq_zones;
2888                         params->flags |= DISK_ZONE_MAX_SEQ_SET;
2889                 }
2890                 if (softc->zone_flags & DA_ZONE_FLAG_RZ_SUP)
2891                         params->flags |= DISK_ZONE_RZ_SUP;
2892
2893                 if (softc->zone_flags & DA_ZONE_FLAG_OPEN_SUP)
2894                         params->flags |= DISK_ZONE_OPEN_SUP;
2895
2896                 if (softc->zone_flags & DA_ZONE_FLAG_CLOSE_SUP)
2897                         params->flags |= DISK_ZONE_CLOSE_SUP;
2898
2899                 if (softc->zone_flags & DA_ZONE_FLAG_FINISH_SUP)
2900                         params->flags |= DISK_ZONE_FINISH_SUP;
2901
2902                 if (softc->zone_flags & DA_ZONE_FLAG_RWP_SUP)
2903                         params->flags |= DISK_ZONE_RWP_SUP;
2904                 break;
2905         }
2906         default:
2907                 break;
2908         }
2909 bailout:
2910         return (error);
2911 }
2912
2913 static void
2914 dastart(struct cam_periph *periph, union ccb *start_ccb)
2915 {
2916         struct da_softc *softc;
2917
2918         softc = (struct da_softc *)periph->softc;
2919
2920         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2921
2922 skipstate:
2923         switch (softc->state) {
2924         case DA_STATE_NORMAL:
2925         {
2926                 struct bio *bp;
2927                 uint8_t tag_code;
2928
2929 more:
2930                 bp = cam_iosched_next_bio(softc->cam_iosched);
2931                 if (bp == NULL) {
2932                         if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
2933                                 cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
2934                                 scsi_test_unit_ready(&start_ccb->csio,
2935                                      /*retries*/ da_retry_count,
2936                                      dadone,
2937                                      MSG_SIMPLE_Q_TAG,
2938                                      SSD_FULL_SIZE,
2939                                      da_default_timeout * 1000);
2940                                 start_ccb->ccb_h.ccb_bp = NULL;
2941                                 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2942                                 xpt_action(start_ccb);
2943                         } else
2944                                 xpt_release_ccb(start_ccb);
2945                         break;
2946                 }
2947
2948                 if (bp->bio_cmd == BIO_DELETE) {
2949                         if (softc->delete_func != NULL) {
2950                                 softc->delete_func(periph, start_ccb, bp);
2951                                 goto out;
2952                         } else {
2953                                 /* Not sure this is possible, but failsafe by lying and saying "sure, done." */
2954                                 biofinish(bp, NULL, 0);
2955                                 goto more;
2956                         }
2957                 }
2958
2959                 if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
2960                         cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
2961                         cam_periph_release_locked(periph);      /* XXX is this still valid? I think so but unverified */
2962                 }
2963
2964                 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2965                     (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2966                         softc->flags &= ~DA_FLAG_NEED_OTAG;
2967                         softc->flags |= DA_FLAG_WAS_OTAG;
2968                         tag_code = MSG_ORDERED_Q_TAG;
2969                 } else {
2970                         tag_code = MSG_SIMPLE_Q_TAG;
2971                 }
2972
2973                 switch (bp->bio_cmd) {
2974                 case BIO_WRITE:
2975                 case BIO_READ:
2976                 {
2977                         void *data_ptr;
2978                         int rw_op;
2979
2980                         biotrack(bp, __func__);
2981
2982                         if (bp->bio_cmd == BIO_WRITE) {
2983                                 softc->flags |= DA_FLAG_DIRTY;
2984                                 rw_op = SCSI_RW_WRITE;
2985                         } else {
2986                                 rw_op = SCSI_RW_READ;
2987                         }
2988
2989                         data_ptr = bp->bio_data;
2990                         if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2991                                 rw_op |= SCSI_RW_BIO;
2992                                 data_ptr = bp;
2993                         }
2994
2995                         scsi_read_write(&start_ccb->csio,
2996                                         /*retries*/da_retry_count,
2997                                         /*cbfcnp*/dadone,
2998                                         /*tag_action*/tag_code,
2999                                         rw_op,
3000                                         /*byte2*/0,
3001                                         softc->minimum_cmd_size,
3002                                         /*lba*/bp->bio_pblkno,
3003                                         /*block_count*/bp->bio_bcount /
3004                                         softc->params.secsize,
3005                                         data_ptr,
3006                                         /*dxfer_len*/ bp->bio_bcount,
3007                                         /*sense_len*/SSD_FULL_SIZE,
3008                                         da_default_timeout * 1000);
3009 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
3010                         start_ccb->csio.bio = bp;
3011 #endif
3012                         break;
3013                 }
3014                 case BIO_FLUSH:
3015                         /*
3016                          * BIO_FLUSH doesn't currently communicate
3017                          * range data, so we synchronize the cache
3018                          * over the whole disk.  We also force
3019                          * ordered tag semantics the flush applies
3020                          * to all previously queued I/O.
3021                          */
3022                         scsi_synchronize_cache(&start_ccb->csio,
3023                                                /*retries*/1,
3024                                                /*cbfcnp*/dadone,
3025                                                MSG_ORDERED_Q_TAG,
3026                                                /*begin_lba*/0,
3027                                                /*lb_count*/0,
3028                                                SSD_FULL_SIZE,
3029                                                da_default_timeout*1000);
3030                         break;
3031                 case BIO_ZONE: {
3032                         int error, queue_ccb;
3033
3034                         queue_ccb = 0;
3035
3036                         error = da_zone_cmd(periph, start_ccb, bp,&queue_ccb);
3037                         if ((error != 0)
3038                          || (queue_ccb == 0)) {
3039                                 biofinish(bp, NULL, error);
3040                                 xpt_release_ccb(start_ccb);
3041                                 return;
3042                         }
3043                         break;
3044                 }
3045                 }
3046                 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
3047                 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
3048                 start_ccb->ccb_h.softtimeout = sbttotv(da_default_softtimeout);
3049
3050 out:
3051                 LIST_INSERT_HEAD(&softc->pending_ccbs,
3052                                  &start_ccb->ccb_h, periph_links.le);
3053
3054                 /* We expect a unit attention from this device */
3055                 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
3056                         start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
3057                         softc->flags &= ~DA_FLAG_RETRY_UA;
3058                 }
3059
3060                 start_ccb->ccb_h.ccb_bp = bp;
3061                 softc->refcount++;
3062                 cam_periph_unlock(periph);
3063                 xpt_action(start_ccb);
3064                 cam_periph_lock(periph);
3065                 softc->refcount--;
3066
3067                 /* May have more work to do, so ensure we stay scheduled */
3068                 daschedule(periph);
3069                 break;
3070         }
3071         case DA_STATE_PROBE_RC:
3072         {
3073                 struct scsi_read_capacity_data *rcap;
3074
3075                 rcap = (struct scsi_read_capacity_data *)
3076                     malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
3077                 if (rcap == NULL) {
3078                         printf("dastart: Couldn't malloc read_capacity data\n");
3079                         /* da_free_periph??? */
3080                         break;
3081                 }
3082                 scsi_read_capacity(&start_ccb->csio,
3083                                    /*retries*/da_retry_count,
3084                                    dadone,
3085                                    MSG_SIMPLE_Q_TAG,
3086                                    rcap,
3087                                    SSD_FULL_SIZE,
3088                                    /*timeout*/5000);
3089                 start_ccb->ccb_h.ccb_bp = NULL;
3090                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
3091                 xpt_action(start_ccb);
3092                 break;
3093         }
3094         case DA_STATE_PROBE_RC16:
3095         {
3096                 struct scsi_read_capacity_data_long *rcaplong;
3097
3098                 rcaplong = (struct scsi_read_capacity_data_long *)
3099                         malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
3100                 if (rcaplong == NULL) {
3101                         printf("dastart: Couldn't malloc read_capacity data\n");
3102                         /* da_free_periph??? */
3103                         break;
3104                 }
3105                 scsi_read_capacity_16(&start_ccb->csio,
3106                                       /*retries*/ da_retry_count,
3107                                       /*cbfcnp*/ dadone,
3108                                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
3109                                       /*lba*/ 0,
3110                                       /*reladr*/ 0,
3111                                       /*pmi*/ 0,
3112                                       /*rcap_buf*/ (uint8_t *)rcaplong,
3113                                       /*rcap_buf_len*/ sizeof(*rcaplong),
3114                                       /*sense_len*/ SSD_FULL_SIZE,
3115                                       /*timeout*/ da_default_timeout * 1000);
3116                 start_ccb->ccb_h.ccb_bp = NULL;
3117                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
3118                 xpt_action(start_ccb);
3119                 break;
3120         }
3121         case DA_STATE_PROBE_LBP:
3122         {
3123                 struct scsi_vpd_logical_block_prov *lbp;
3124
3125                 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
3126                         /*
3127                          * If we get here we don't support any SBC-3 delete
3128                          * methods with UNMAP as the Logical Block Provisioning
3129                          * VPD page support is required for devices which
3130                          * support it according to T10/1799-D Revision 31
3131                          * however older revisions of the spec don't mandate
3132                          * this so we currently don't remove these methods
3133                          * from the available set.
3134                          */
3135                         softc->state = DA_STATE_PROBE_BLK_LIMITS;
3136                         goto skipstate;
3137                 }
3138
3139                 lbp = (struct scsi_vpd_logical_block_prov *)
3140                         malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
3141
3142                 if (lbp == NULL) {
3143                         printf("dastart: Couldn't malloc lbp data\n");
3144                         /* da_free_periph??? */
3145                         break;
3146                 }
3147
3148                 scsi_inquiry(&start_ccb->csio,
3149                              /*retries*/da_retry_count,
3150                              /*cbfcnp*/dadone,
3151                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3152                              /*inq_buf*/(u_int8_t *)lbp,
3153                              /*inq_len*/sizeof(*lbp),
3154                              /*evpd*/TRUE,
3155                              /*page_code*/SVPD_LBP,
3156                              /*sense_len*/SSD_MIN_SIZE,
3157                              /*timeout*/da_default_timeout * 1000);
3158                 start_ccb->ccb_h.ccb_bp = NULL;
3159                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
3160                 xpt_action(start_ccb);
3161                 break;
3162         }
3163         case DA_STATE_PROBE_BLK_LIMITS:
3164         {
3165                 struct scsi_vpd_block_limits *block_limits;
3166
3167                 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
3168                         /* Not supported skip to next probe */
3169                         softc->state = DA_STATE_PROBE_BDC;
3170                         goto skipstate;
3171                 }
3172
3173                 block_limits = (struct scsi_vpd_block_limits *)
3174                         malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
3175
3176                 if (block_limits == NULL) {
3177                         printf("dastart: Couldn't malloc block_limits data\n");
3178                         /* da_free_periph??? */
3179                         break;
3180                 }
3181
3182                 scsi_inquiry(&start_ccb->csio,
3183                              /*retries*/da_retry_count,
3184                              /*cbfcnp*/dadone,
3185                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3186                              /*inq_buf*/(u_int8_t *)block_limits,
3187                              /*inq_len*/sizeof(*block_limits),
3188                              /*evpd*/TRUE,
3189                              /*page_code*/SVPD_BLOCK_LIMITS,
3190                              /*sense_len*/SSD_MIN_SIZE,
3191                              /*timeout*/da_default_timeout * 1000);
3192                 start_ccb->ccb_h.ccb_bp = NULL;
3193                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
3194                 xpt_action(start_ccb);
3195                 break;
3196         }
3197         case DA_STATE_PROBE_BDC:
3198         {
3199                 struct scsi_vpd_block_characteristics *bdc;
3200
3201                 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
3202                         softc->state = DA_STATE_PROBE_ATA;
3203                         goto skipstate;
3204                 }
3205
3206                 bdc = (struct scsi_vpd_block_characteristics *)
3207                         malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3208
3209                 if (bdc == NULL) {
3210                         printf("dastart: Couldn't malloc bdc data\n");
3211                         /* da_free_periph??? */
3212                         break;
3213                 }
3214
3215                 scsi_inquiry(&start_ccb->csio,
3216                              /*retries*/da_retry_count,
3217                              /*cbfcnp*/dadone,
3218                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3219                              /*inq_buf*/(u_int8_t *)bdc,
3220                              /*inq_len*/sizeof(*bdc),
3221                              /*evpd*/TRUE,
3222                              /*page_code*/SVPD_BDC,
3223                              /*sense_len*/SSD_MIN_SIZE,
3224                              /*timeout*/da_default_timeout * 1000);
3225                 start_ccb->ccb_h.ccb_bp = NULL;
3226                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
3227                 xpt_action(start_ccb);
3228                 break;
3229         }
3230         case DA_STATE_PROBE_ATA:
3231         {
3232                 struct ata_params *ata_params;
3233
3234                 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
3235                         if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
3236                          || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
3237                                 /*
3238                                  * Note that if the ATA VPD page isn't
3239                                  * supported, we aren't talking to an ATA
3240                                  * device anyway.  Support for that VPD
3241                                  * page is mandatory for SCSI to ATA (SAT)
3242                                  * translation layers.
3243                                  */
3244                                 softc->state = DA_STATE_PROBE_ZONE;
3245                                 goto skipstate;
3246                         }
3247                         daprobedone(periph, start_ccb);
3248                         break;
3249                 }
3250
3251                 ata_params = (struct ata_params*)
3252                         malloc(sizeof(*ata_params), M_SCSIDA,M_NOWAIT|M_ZERO);
3253
3254                 if (ata_params == NULL) {
3255                         xpt_print(periph->path, "Couldn't malloc ata_params "
3256                             "data\n");
3257                         /* da_free_periph??? */
3258                         break;
3259                 }
3260
3261                 scsi_ata_identify(&start_ccb->csio,
3262                                   /*retries*/da_retry_count,
3263                                   /*cbfcnp*/dadone,
3264                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
3265                                   /*data_ptr*/(u_int8_t *)ata_params,
3266                                   /*dxfer_len*/sizeof(*ata_params),
3267                                   /*sense_len*/SSD_FULL_SIZE,
3268                                   /*timeout*/da_default_timeout * 1000);
3269                 start_ccb->ccb_h.ccb_bp = NULL;
3270                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
3271                 xpt_action(start_ccb);
3272                 break;
3273         }
3274         case DA_STATE_PROBE_ATA_LOGDIR:
3275         {
3276                 struct ata_gp_log_dir *log_dir;
3277                 int retval;
3278
3279                 retval = 0;
3280
3281                 if ((softc->flags & DA_FLAG_CAN_ATA_LOG) == 0) {
3282                         /*
3283                          * If we don't have log support, not much point in
3284                          * trying to probe zone support.
3285                          */
3286                         daprobedone(periph, start_ccb);
3287                         break;
3288                 }
3289
3290                 /*
3291                  * If we have an ATA device (the SCSI ATA Information VPD
3292                  * page should be present and the ATA identify should have
3293                  * succeeded) and it supports logs, ask for the log directory.
3294                  */
3295
3296                 log_dir = malloc(sizeof(*log_dir), M_SCSIDA, M_NOWAIT|M_ZERO);
3297                 if (log_dir == NULL) {
3298                         xpt_print(periph->path, "Couldn't malloc log_dir "
3299                             "data\n");
3300                         daprobedone(periph, start_ccb);
3301                         break;
3302                 }
3303
3304                 retval = scsi_ata_read_log(&start_ccb->csio,
3305                     /*retries*/ da_retry_count,
3306                     /*cbfcnp*/ dadone,
3307                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3308                     /*log_address*/ ATA_LOG_DIRECTORY,
3309                     /*page_number*/ 0,
3310                     /*block_count*/ 1,
3311                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3312                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3313                     /*data_ptr*/ (uint8_t *)log_dir,
3314                     /*dxfer_len*/ sizeof(*log_dir),
3315                     /*sense_len*/ SSD_FULL_SIZE,
3316                     /*timeout*/ da_default_timeout * 1000);
3317
3318                 if (retval != 0) {
3319                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3320                         free(log_dir, M_SCSIDA);
3321                         daprobedone(periph, start_ccb);
3322                         break;
3323                 }
3324                 start_ccb->ccb_h.ccb_bp = NULL;
3325                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_LOGDIR;
3326                 xpt_action(start_ccb);
3327                 break;
3328         }
3329         case DA_STATE_PROBE_ATA_IDDIR:
3330         {
3331                 struct ata_identify_log_pages *id_dir;
3332                 int retval;
3333
3334                 retval = 0;
3335
3336                 /*
3337                  * Check here to see whether the Identify Device log is
3338                  * supported in the directory of logs.  If so, continue
3339                  * with requesting the log of identify device pages.
3340                  */
3341                 if ((softc->flags & DA_FLAG_CAN_ATA_IDLOG) == 0) {
3342                         daprobedone(periph, start_ccb);
3343                         break;
3344                 }
3345
3346                 id_dir = malloc(sizeof(*id_dir), M_SCSIDA, M_NOWAIT | M_ZERO);
3347                 if (id_dir == NULL) {
3348                         xpt_print(periph->path, "Couldn't malloc id_dir "
3349                             "data\n");
3350                         daprobedone(periph, start_ccb);
3351                         break;
3352                 }
3353
3354                 retval = scsi_ata_read_log(&start_ccb->csio,
3355                     /*retries*/ da_retry_count,
3356                     /*cbfcnp*/ dadone,
3357                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3358                     /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3359                     /*page_number*/ ATA_IDL_PAGE_LIST,
3360                     /*block_count*/ 1,
3361                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3362                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3363                     /*data_ptr*/ (uint8_t *)id_dir,
3364                     /*dxfer_len*/ sizeof(*id_dir),
3365                     /*sense_len*/ SSD_FULL_SIZE,
3366                     /*timeout*/ da_default_timeout * 1000);
3367
3368                 if (retval != 0) {
3369                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3370                         free(id_dir, M_SCSIDA);
3371                         daprobedone(periph, start_ccb);
3372                         break;
3373                 }
3374                 start_ccb->ccb_h.ccb_bp = NULL;
3375                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_IDDIR;
3376                 xpt_action(start_ccb);
3377                 break;
3378         }
3379         case DA_STATE_PROBE_ATA_SUP:
3380         {
3381                 struct ata_identify_log_sup_cap *sup_cap;
3382                 int retval;
3383
3384                 retval = 0;
3385
3386                 /*
3387                  * Check here to see whether the Supported Capabilities log
3388                  * is in the list of Identify Device logs.
3389                  */
3390                 if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP) == 0) {
3391                         daprobedone(periph, start_ccb);
3392                         break;
3393                 }
3394
3395                 sup_cap = malloc(sizeof(*sup_cap), M_SCSIDA, M_NOWAIT|M_ZERO);
3396                 if (sup_cap == NULL) {
3397                         xpt_print(periph->path, "Couldn't malloc sup_cap "
3398                             "data\n");
3399                         daprobedone(periph, start_ccb);
3400                         break;
3401                 }
3402
3403                 retval = scsi_ata_read_log(&start_ccb->csio,
3404                     /*retries*/ da_retry_count,
3405                     /*cbfcnp*/ dadone,
3406                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3407                     /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3408                     /*page_number*/ ATA_IDL_SUP_CAP,
3409                     /*block_count*/ 1,
3410                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3411                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3412                     /*data_ptr*/ (uint8_t *)sup_cap,
3413                     /*dxfer_len*/ sizeof(*sup_cap),
3414                     /*sense_len*/ SSD_FULL_SIZE,
3415                     /*timeout*/ da_default_timeout * 1000);
3416
3417                 if (retval != 0) {
3418                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3419                         free(sup_cap, M_SCSIDA);
3420                         daprobedone(periph, start_ccb);
3421                         break;
3422
3423                 }
3424
3425                 start_ccb->ccb_h.ccb_bp = NULL;
3426                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_SUP;
3427                 xpt_action(start_ccb);
3428                 break;
3429         }
3430         case DA_STATE_PROBE_ATA_ZONE:
3431         {
3432                 struct ata_zoned_info_log *ata_zone;
3433                 int retval;
3434
3435                 retval = 0;
3436
3437                 /*
3438                  * Check here to see whether the zoned device information
3439                  * page is supported.  If so, continue on to request it.
3440                  * If not, skip to DA_STATE_PROBE_LOG or done.
3441                  */
3442                 if ((softc->flags & DA_FLAG_CAN_ATA_ZONE) == 0) {
3443                         daprobedone(periph, start_ccb);
3444                         break;
3445                 }
3446                 ata_zone = malloc(sizeof(*ata_zone), M_SCSIDA,
3447                                   M_NOWAIT|M_ZERO);
3448                 if (ata_zone == NULL) {
3449                         xpt_print(periph->path, "Couldn't malloc ata_zone "
3450                             "data\n");
3451                         daprobedone(periph, start_ccb);
3452                         break;
3453                 }
3454
3455                 retval = scsi_ata_read_log(&start_ccb->csio,
3456                     /*retries*/ da_retry_count,
3457                     /*cbfcnp*/ dadone,
3458                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3459                     /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3460                     /*page_number*/ ATA_IDL_ZDI,
3461                     /*block_count*/ 1,
3462                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3463                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3464                     /*data_ptr*/ (uint8_t *)ata_zone,
3465                     /*dxfer_len*/ sizeof(*ata_zone),
3466                     /*sense_len*/ SSD_FULL_SIZE,
3467                     /*timeout*/ da_default_timeout * 1000);
3468
3469                 if (retval != 0) {
3470                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3471                         free(ata_zone, M_SCSIDA);
3472                         daprobedone(periph, start_ccb);
3473                         break;
3474                 }
3475                 start_ccb->ccb_h.ccb_bp = NULL;
3476                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_ZONE;
3477                 xpt_action(start_ccb);
3478
3479                 break;
3480         }
3481         case DA_STATE_PROBE_ZONE:
3482         {
3483                 struct scsi_vpd_zoned_bdc *bdc;
3484
3485                 /*
3486                  * Note that this page will be supported for SCSI protocol
3487                  * devices that support ZBC (SMR devices), as well as ATA
3488                  * protocol devices that are behind a SAT (SCSI to ATA
3489                  * Translation) layer that supports converting ZBC commands
3490                  * to their ZAC equivalents.
3491                  */
3492                 if (!scsi_vpd_supported_page(periph, SVPD_ZONED_BDC)) {
3493                         daprobedone(periph, start_ccb);
3494                         break;
3495                 }
3496                 bdc = (struct scsi_vpd_zoned_bdc *)
3497                         malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3498
3499                 if (bdc == NULL) {
3500                         xpt_release_ccb(start_ccb);
3501                         xpt_print(periph->path, "Couldn't malloc zone VPD "
3502                             "data\n");
3503                         break;
3504                 }
3505                 scsi_inquiry(&start_ccb->csio,
3506                              /*retries*/da_retry_count,
3507                              /*cbfcnp*/dadone,
3508                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3509                              /*inq_buf*/(u_int8_t *)bdc,
3510                              /*inq_len*/sizeof(*bdc),
3511                              /*evpd*/TRUE,
3512                              /*page_code*/SVPD_ZONED_BDC,
3513                              /*sense_len*/SSD_FULL_SIZE,
3514                              /*timeout*/da_default_timeout * 1000);
3515                 start_ccb->ccb_h.ccb_bp = NULL;
3516                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ZONE;
3517                 xpt_action(start_ccb);
3518                 break;
3519         }
3520         }
3521 }
3522
3523 /*
3524  * In each of the methods below, while its the caller's
3525  * responsibility to ensure the request will fit into a
3526  * single device request, we might have changed the delete
3527  * method due to the device incorrectly advertising either
3528  * its supported methods or limits.
3529  * 
3530  * To prevent this causing further issues we validate the
3531  * against the methods limits, and warn which would
3532  * otherwise be unnecessary.
3533  */
3534 static void
3535 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3536 {
3537         struct da_softc *softc = (struct da_softc *)periph->softc;;
3538         struct bio *bp1;
3539         uint8_t *buf = softc->unmap_buf;
3540         struct scsi_unmap_desc *d = (void *)&buf[UNMAP_HEAD_SIZE];
3541         uint64_t lba, lastlba = (uint64_t)-1;
3542         uint64_t totalcount = 0;
3543         uint64_t count;
3544         uint32_t c, lastcount = 0, ranges = 0;
3545
3546         /*
3547          * Currently this doesn't take the UNMAP
3548          * Granularity and Granularity Alignment
3549          * fields into account.
3550          *
3551          * This could result in both unoptimal unmap
3552          * requests as as well as UNMAP calls unmapping
3553          * fewer LBA's than requested.
3554          */
3555
3556         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
3557         bp1 = bp;
3558         do {
3559                 /*
3560                  * Note: ada and da are different in how they store the
3561                  * pending bp's in a trim. ada stores all of them in the
3562                  * trim_req.bps. da stores all but the first one in the
3563                  * delete_run_queue. ada then completes all the bps in
3564                  * its adadone() loop. da completes all the bps in the
3565                  * delete_run_queue in dadone, and relies on the biodone
3566                  * after to complete. This should be reconciled since there's
3567                  * no real reason to do it differently. XXX
3568                  */
3569                 if (bp1 != bp)
3570                         bioq_insert_tail(&softc->delete_run_queue, bp1);
3571                 lba = bp1->bio_pblkno;
3572                 count = bp1->bio_bcount / softc->params.secsize;
3573
3574                 /* Try to extend the previous range. */
3575                 if (lba == lastlba) {
3576                         c = omin(count, UNMAP_RANGE_MAX - lastcount);
3577                         lastlba += c;
3578                         lastcount += c;
3579                         scsi_ulto4b(lastcount, d[ranges - 1].length);
3580                         count -= c;
3581                         lba += c;
3582                         totalcount += c;
3583                 } else if ((softc->quirks & DA_Q_STRICT_UNMAP) &&
3584                     softc->unmap_gran != 0) {
3585                         /* Align length of the previous range. */
3586                         if ((c = lastcount % softc->unmap_gran) != 0) {
3587                                 if (lastcount <= c) {
3588                                         totalcount -= lastcount;
3589                                         lastlba = (uint64_t)-1;
3590                                         lastcount = 0;
3591                                         ranges--;
3592                                 } else {
3593                                         totalcount -= c;
3594                                         lastlba -= c;
3595                                         lastcount -= c;
3596                                         scsi_ulto4b(lastcount, d[ranges - 1].length);
3597                                 }
3598                         }
3599                         /* Align beginning of the new range. */
3600                         c = (lba - softc->unmap_gran_align) % softc->unmap_gran;
3601                         if (c != 0) {
3602                                 c = softc->unmap_gran - c;
3603                                 if (count <= c) {
3604                                         count = 0;
3605                                 } else {
3606                                         lba += c;
3607                                         count -= c;
3608                                 }
3609                         }
3610                 }
3611
3612                 while (count > 0) {
3613                         c = omin(count, UNMAP_RANGE_MAX);
3614                         if (totalcount + c > softc->unmap_max_lba ||
3615                             ranges >= softc->unmap_max_ranges) {
3616                                 xpt_print(periph->path,
3617                                     "%s issuing short delete %ld > %ld"
3618                                     "|| %d >= %d",
3619                                     da_delete_method_desc[softc->delete_method],
3620                                     totalcount + c, softc->unmap_max_lba,
3621                                     ranges, softc->unmap_max_ranges);
3622                                 break;
3623                         }
3624                         scsi_u64to8b(lba, d[ranges].lba);
3625                         scsi_ulto4b(c, d[ranges].length);
3626                         lba += c;
3627                         totalcount += c;
3628                         ranges++;
3629                         count -= c;
3630                         lastlba = lba;
3631                         lastcount = c;
3632                 }
3633                 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3634                 if (bp1 == NULL)
3635                         break;
3636                 if (ranges >= softc->unmap_max_ranges ||
3637                     totalcount + bp1->bio_bcount /
3638                     softc->params.secsize > softc->unmap_max_lba) {
3639                         cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3640                         break;
3641                 }
3642         } while (1);
3643
3644         /* Align length of the last range. */
3645         if ((softc->quirks & DA_Q_STRICT_UNMAP) && softc->unmap_gran != 0 &&
3646             (c = lastcount % softc->unmap_gran) != 0) {
3647                 if (lastcount <= c)
3648                         ranges--;
3649                 else
3650                         scsi_ulto4b(lastcount - c, d[ranges - 1].length);
3651         }
3652
3653         scsi_ulto2b(ranges * 16 + 6, &buf[0]);
3654         scsi_ulto2b(ranges * 16, &buf[2]);
3655
3656         scsi_unmap(&ccb->csio,
3657                    /*retries*/da_retry_count,
3658                    /*cbfcnp*/dadone,
3659                    /*tag_action*/MSG_SIMPLE_Q_TAG,
3660                    /*byte2*/0,
3661                    /*data_ptr*/ buf,
3662                    /*dxfer_len*/ ranges * 16 + 8,
3663                    /*sense_len*/SSD_FULL_SIZE,
3664                    da_default_timeout * 1000);
3665         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3666         ccb->ccb_h.flags |= CAM_UNLOCKED;
3667         cam_iosched_submit_trim(softc->cam_iosched);
3668 }
3669
3670 static void
3671 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3672 {
3673         struct da_softc *softc = (struct da_softc *)periph->softc;
3674         struct bio *bp1;
3675         uint8_t *buf = softc->unmap_buf;
3676         uint64_t lastlba = (uint64_t)-1;
3677         uint64_t count;
3678         uint64_t lba;
3679         uint32_t lastcount = 0, c, requestcount;
3680         int ranges = 0, off, block_count;
3681
3682         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
3683         bp1 = bp;
3684         do {
3685                 if (bp1 != bp)//XXX imp XXX
3686                         bioq_insert_tail(&softc->delete_run_queue, bp1);
3687                 lba = bp1->bio_pblkno;
3688                 count = bp1->bio_bcount / softc->params.secsize;
3689                 requestcount = count;
3690
3691                 /* Try to extend the previous range. */
3692                 if (lba == lastlba) {
3693                         c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
3694                         lastcount += c;
3695                         off = (ranges - 1) * 8;
3696                         buf[off + 6] = lastcount & 0xff;
3697                         buf[off + 7] = (lastcount >> 8) & 0xff;
3698                         count -= c;
3699                         lba += c;
3700                 }
3701
3702                 while (count > 0) {
3703                         c = omin(count, ATA_DSM_RANGE_MAX);
3704                         off = ranges * 8;
3705
3706                         buf[off + 0] = lba & 0xff;
3707                         buf[off + 1] = (lba >> 8) & 0xff;
3708                         buf[off + 2] = (lba >> 16) & 0xff;
3709                         buf[off + 3] = (lba >> 24) & 0xff;
3710                         buf[off + 4] = (lba >> 32) & 0xff;
3711                         buf[off + 5] = (lba >> 40) & 0xff;
3712                         buf[off + 6] = c & 0xff;
3713                         buf[off + 7] = (c >> 8) & 0xff;
3714                         lba += c;
3715                         ranges++;
3716                         count -= c;
3717                         lastcount = c;
3718                         if (count != 0 && ranges == softc->trim_max_ranges) {
3719                                 xpt_print(periph->path,
3720                                     "%s issuing short delete %ld > %ld\n",
3721                                     da_delete_method_desc[softc->delete_method],
3722                                     requestcount,
3723                                     (softc->trim_max_ranges - ranges) *
3724                                     ATA_DSM_RANGE_MAX);
3725                                 break;
3726                         }
3727                 }
3728                 lastlba = lba;
3729                 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3730                 if (bp1 == NULL)
3731                         break;
3732                 if (bp1->bio_bcount / softc->params.secsize >
3733                     (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
3734                         cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3735                         break;
3736                 }
3737         } while (1);
3738
3739         block_count = howmany(ranges, ATA_DSM_BLK_RANGES);
3740         scsi_ata_trim(&ccb->csio,
3741                       /*retries*/da_retry_count,
3742                       /*cbfcnp*/dadone,
3743                       /*tag_action*/MSG_SIMPLE_Q_TAG,
3744                       block_count,
3745                       /*data_ptr*/buf,
3746                       /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
3747                       /*sense_len*/SSD_FULL_SIZE,
3748                       da_default_timeout * 1000);
3749         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3750         ccb->ccb_h.flags |= CAM_UNLOCKED;
3751         cam_iosched_submit_trim(softc->cam_iosched);
3752 }
3753
3754 /*
3755  * We calculate ws_max_blks here based off d_delmaxsize instead
3756  * of using softc->ws_max_blks as it is absolute max for the
3757  * device not the protocol max which may well be lower.
3758  */
3759 static void
3760 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3761 {
3762         struct da_softc *softc;
3763         struct bio *bp1;
3764         uint64_t ws_max_blks;
3765         uint64_t lba;
3766         uint64_t count; /* forward compat with WS32 */
3767
3768         softc = (struct da_softc *)periph->softc;
3769         ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
3770         lba = bp->bio_pblkno;
3771         count = 0;
3772         bp1 = bp;
3773         do {
3774                 if (bp1 != bp)//XXX imp XXX
3775                         bioq_insert_tail(&softc->delete_run_queue, bp1);
3776                 count += bp1->bio_bcount / softc->params.secsize;
3777                 if (count > ws_max_blks) {
3778                         xpt_print(periph->path,
3779                             "%s issuing short delete %ld > %ld\n",
3780                             da_delete_method_desc[softc->delete_method],
3781                             count, ws_max_blks);
3782                         count = omin(count, ws_max_blks);
3783                         break;
3784                 }
3785                 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3786                 if (bp1 == NULL)
3787                         break;
3788                 if (lba + count != bp1->bio_pblkno ||
3789                     count + bp1->bio_bcount /
3790                     softc->params.secsize > ws_max_blks) {
3791                         cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3792                         break;
3793                 }
3794         } while (1);
3795
3796         scsi_write_same(&ccb->csio,
3797                         /*retries*/da_retry_count,
3798                         /*cbfcnp*/dadone,
3799                         /*tag_action*/MSG_SIMPLE_Q_TAG,
3800                         /*byte2*/softc->delete_method ==
3801                             DA_DELETE_ZERO ? 0 : SWS_UNMAP,
3802                         softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
3803                         /*lba*/lba,
3804                         /*block_count*/count,
3805                         /*data_ptr*/ __DECONST(void *, zero_region),
3806                         /*dxfer_len*/ softc->params.secsize,
3807                         /*sense_len*/SSD_FULL_SIZE,
3808                         da_default_timeout * 1000);
3809         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3810         ccb->ccb_h.flags |= CAM_UNLOCKED;
3811         cam_iosched_submit_trim(softc->cam_iosched);
3812 }
3813
3814 static int
3815 cmd6workaround(union ccb *ccb)
3816 {
3817         struct scsi_rw_6 cmd6;
3818         struct scsi_rw_10 *cmd10;
3819         struct da_softc *softc;
3820         u_int8_t *cdb;
3821         struct bio *bp;
3822         int frozen;
3823
3824         cdb = ccb->csio.cdb_io.cdb_bytes;
3825         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
3826
3827         if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
3828                 da_delete_methods old_method = softc->delete_method;
3829
3830                 /*
3831                  * Typically there are two reasons for failure here
3832                  * 1. Delete method was detected as supported but isn't
3833                  * 2. Delete failed due to invalid params e.g. too big
3834                  *
3835                  * While we will attempt to choose an alternative delete method
3836                  * this may result in short deletes if the existing delete
3837                  * requests from geom are big for the new method chosen.
3838                  *
3839                  * This method assumes that the error which triggered this
3840                  * will not retry the io otherwise a panic will occur
3841                  */
3842                 dadeleteflag(softc, old_method, 0);
3843                 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
3844                 if (softc->delete_method == DA_DELETE_DISABLE)
3845                         xpt_print(ccb->ccb_h.path,
3846                                   "%s failed, disabling BIO_DELETE\n",
3847                                   da_delete_method_desc[old_method]);
3848                 else
3849                         xpt_print(ccb->ccb_h.path,
3850                                   "%s failed, switching to %s BIO_DELETE\n",
3851                                   da_delete_method_desc[old_method],
3852                                   da_delete_method_desc[softc->delete_method]);
3853
3854                 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
3855                         cam_iosched_queue_work(softc->cam_iosched, bp);
3856                 cam_iosched_queue_work(softc->cam_iosched,
3857                     (struct bio *)ccb->ccb_h.ccb_bp);
3858                 ccb->ccb_h.ccb_bp = NULL;
3859                 return (0);
3860         }
3861
3862         /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
3863         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
3864             (*cdb == PREVENT_ALLOW) &&
3865             (softc->quirks & DA_Q_NO_PREVENT) == 0) {
3866                 if (bootverbose)
3867                         xpt_print(ccb->ccb_h.path,
3868                             "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
3869                 softc->quirks |= DA_Q_NO_PREVENT;
3870                 return (0);
3871         }
3872
3873         /* Detect unsupported SYNCHRONIZE CACHE(10). */
3874         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
3875             (*cdb == SYNCHRONIZE_CACHE) &&
3876             (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
3877                 if (bootverbose)
3878                         xpt_print(ccb->ccb_h.path,
3879                             "SYNCHRONIZE CACHE(10) not supported.\n");
3880                 softc->quirks |= DA_Q_NO_SYNC_CACHE;
3881                 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
3882                 return (0);
3883         }
3884
3885         /* Translation only possible if CDB is an array and cmd is R/W6 */
3886         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
3887             (*cdb != READ_6 && *cdb != WRITE_6))
3888                 return 0;
3889
3890         xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
3891             "increasing minimum_cmd_size to 10.\n");
3892         softc->minimum_cmd_size = 10;
3893
3894         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
3895         cmd10 = (struct scsi_rw_10 *)cdb;
3896         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
3897         cmd10->byte2 = 0;
3898         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
3899         cmd10->reserved = 0;
3900         scsi_ulto2b(cmd6.length, cmd10->length);
3901         cmd10->control = cmd6.control;
3902         ccb->csio.cdb_len = sizeof(*cmd10);
3903
3904         /* Requeue request, unfreezing queue if necessary */
3905         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3906         ccb->ccb_h.status = CAM_REQUEUE_REQ;
3907         xpt_action(ccb);
3908         if (frozen) {
3909                 cam_release_devq(ccb->ccb_h.path,
3910                                  /*relsim_flags*/0,
3911                                  /*reduction*/0,
3912                                  /*timeout*/0,
3913                                  /*getcount_only*/0);
3914         }
3915         return (ERESTART);
3916 }
3917
3918 static void
3919 dazonedone(struct cam_periph *periph, union ccb *ccb)
3920 {
3921         struct da_softc *softc;
3922         struct bio *bp;
3923
3924         softc = periph->softc;
3925         bp = (struct bio *)ccb->ccb_h.ccb_bp;
3926
3927         switch (bp->bio_zone.zone_cmd) {
3928         case DISK_ZONE_OPEN:
3929         case DISK_ZONE_CLOSE:
3930         case DISK_ZONE_FINISH:
3931         case DISK_ZONE_RWP:
3932                 break;
3933         case DISK_ZONE_REPORT_ZONES: {
3934                 uint32_t avail_len;
3935                 struct disk_zone_report *rep;
3936                 struct scsi_report_zones_hdr *hdr;
3937                 struct scsi_report_zones_desc *desc;
3938                 struct disk_zone_rep_entry *entry;
3939                 uint32_t num_alloced, hdr_len, num_avail;
3940                 uint32_t num_to_fill, i;
3941                 int ata;
3942
3943                 rep = &bp->bio_zone.zone_params.report;
3944                 avail_len = ccb->csio.dxfer_len - ccb->csio.resid;
3945                 /*
3946                  * Note that bio_resid isn't normally used for zone
3947                  * commands, but it is used by devstat_end_transaction_bio()
3948                  * to determine how much data was transferred.  Because
3949                  * the size of the SCSI/ATA data structures is different
3950                  * than the size of the BIO interface structures, the
3951                  * amount of data actually transferred from the drive will
3952                  * be different than the amount of data transferred to
3953                  * the user.
3954                  */
3955                 bp->bio_resid = ccb->csio.resid;
3956                 num_alloced = rep->entries_allocated;
3957                 hdr = (struct scsi_report_zones_hdr *)ccb->csio.data_ptr;
3958                 if (avail_len < sizeof(*hdr)) {
3959                         /*
3960                          * Is there a better error than EIO here?  We asked
3961                          * for at least the header, and we got less than
3962                          * that.
3963                          */
3964                         bp->bio_error = EIO;
3965                         bp->bio_flags |= BIO_ERROR;
3966                         bp->bio_resid = bp->bio_bcount;
3967                         break;
3968                 }
3969
3970                 if (softc->zone_interface == DA_ZONE_IF_ATA_PASS)
3971                         ata = 1;
3972                 else
3973                         ata = 0;
3974
3975                 hdr_len = ata ? le32dec(hdr->length) :
3976                                 scsi_4btoul(hdr->length);
3977                 if (hdr_len > 0)
3978                         rep->entries_available = hdr_len / sizeof(*desc);
3979                 else
3980                         rep->entries_available = 0;
3981                 /*
3982                  * NOTE: using the same values for the BIO version of the
3983                  * same field as the SCSI/ATA values.  This means we could
3984                  * get some additional values that aren't defined in bio.h
3985                  * if more values of the same field are defined later.
3986                  */
3987                 rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
3988                 rep->header.maximum_lba = ata ?  le64dec(hdr->maximum_lba) :
3989                                           scsi_8btou64(hdr->maximum_lba);
3990                 /*
3991                  * If the drive reports no entries that match the query,
3992                  * we're done.
3993                  */
3994                 if (hdr_len == 0) {
3995                         rep->entries_filled = 0;
3996                         break;
3997                 }
3998
3999                 num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
4000                                 hdr_len / sizeof(*desc));
4001                 /*
4002                  * If the drive didn't return any data, then we're done.
4003                  */
4004                 if (num_avail == 0) {
4005                         rep->entries_filled = 0;
4006                         break;
4007                 }
4008
4009                 num_to_fill = min(num_avail, rep->entries_allocated);
4010                 /*
4011                  * If the user didn't allocate any entries for us to fill,
4012                  * we're done.
4013                  */
4014                 if (num_to_fill == 0) {
4015                         rep->entries_filled = 0;
4016                         break;
4017                 }
4018
4019                 for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
4020                      i < num_to_fill; i++, desc++, entry++) {
4021                         /*
4022                          * NOTE: we're mapping the values here directly
4023                          * from the SCSI/ATA bit definitions to the bio.h
4024                          * definitons.  There is also a warning in
4025                          * disk_zone.h, but the impact is that if
4026                          * additional values are added in the SCSI/ATA
4027                          * specs these will be visible to consumers of
4028                          * this interface.
4029                          */
4030                         entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
4031                         entry->zone_condition =
4032                             (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
4033                             SRZ_ZONE_COND_SHIFT;
4034                         entry->zone_flags |= desc->zone_flags &
4035                             (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
4036                         entry->zone_length =
4037                             ata ? le64dec(desc->zone_length) :
4038                                   scsi_8btou64(desc->zone_length);
4039                         entry->zone_start_lba =
4040                             ata ? le64dec(desc->zone_start_lba) :
4041                                   scsi_8btou64(desc->zone_start_lba);
4042                         entry->write_pointer_lba =
4043                             ata ? le64dec(desc->write_pointer_lba) :
4044                                   scsi_8btou64(desc->write_pointer_lba);
4045                 }
4046                 rep->entries_filled = num_to_fill;
4047                 break;
4048         }
4049         case DISK_ZONE_GET_PARAMS:
4050         default:
4051                 /*
4052                  * In theory we should not get a GET_PARAMS bio, since it
4053                  * should be handled without queueing the command to the
4054                  * drive.
4055                  */
4056                 panic("%s: Invalid zone command %d", __func__,
4057                     bp->bio_zone.zone_cmd);
4058                 break;
4059         }
4060
4061         if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
4062                 free(ccb->csio.data_ptr, M_SCSIDA);
4063 }
4064
4065 static void
4066 dadone(struct cam_periph *periph, union ccb *done_ccb)
4067 {
4068         struct da_softc *softc;
4069         struct ccb_scsiio *csio;
4070         u_int32_t  priority;
4071         da_ccb_state state;
4072
4073         softc = (struct da_softc *)periph->softc;
4074         priority = done_ccb->ccb_h.pinfo.priority;
4075
4076         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
4077
4078         csio = &done_ccb->csio;
4079 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4080         if (csio->bio != NULL)
4081                 biotrack(csio->bio, __func__);
4082 #endif
4083         state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4084         switch (state) {
4085         case DA_CCB_BUFFER_IO:
4086         case DA_CCB_DELETE:
4087         {
4088                 struct bio *bp, *bp1;
4089
4090                 cam_periph_lock(periph);
4091                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4092                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4093                         int error;
4094                         int sf;
4095
4096                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
4097                                 sf = SF_RETRY_UA;
4098                         else
4099                                 sf = 0;
4100
4101                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
4102                         if (error == ERESTART) {
4103                                 /*
4104                                  * A retry was scheduled, so
4105                                  * just return.
4106                                  */
4107                                 cam_periph_unlock(periph);
4108                                 return;
4109                         }
4110                         bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4111                         if (error != 0) {
4112                                 int queued_error;
4113
4114                                 /*
4115                                  * return all queued I/O with EIO, so that
4116                                  * the client can retry these I/Os in the
4117                                  * proper order should it attempt to recover.
4118                                  */
4119                                 queued_error = EIO;
4120
4121                                 if (error == ENXIO
4122                                  && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
4123                                         /*
4124                                          * Catastrophic error.  Mark our pack as
4125                                          * invalid.
4126                                          */
4127                                         /*
4128                                          * XXX See if this is really a media
4129                                          * XXX change first?
4130                                          */
4131                                         xpt_print(periph->path,
4132                                             "Invalidating pack\n");
4133                                         softc->flags |= DA_FLAG_PACK_INVALID;
4134 #ifdef CAM_IO_STATS
4135                                         softc->invalidations++;
4136 #endif
4137                                         queued_error = ENXIO;
4138                                 }
4139                                 cam_iosched_flush(softc->cam_iosched, NULL,
4140                                            queued_error);
4141                                 if (bp != NULL) {
4142                                         bp->bio_error = error;
4143                                         bp->bio_resid = bp->bio_bcount;
4144                                         bp->bio_flags |= BIO_ERROR;
4145                                 }
4146                         } else if (bp != NULL) {
4147                                 if (state == DA_CCB_DELETE)
4148                                         bp->bio_resid = 0;
4149                                 else
4150                                         bp->bio_resid = csio->resid;
4151                                 bp->bio_error = 0;
4152                                 if (bp->bio_resid != 0)
4153                                         bp->bio_flags |= BIO_ERROR;
4154                         }
4155                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4156                                 cam_release_devq(done_ccb->ccb_h.path,
4157                                                  /*relsim_flags*/0,
4158                                                  /*reduction*/0,
4159                                                  /*timeout*/0,
4160                                                  /*getcount_only*/0);
4161                 } else if (bp != NULL) {
4162                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4163                                 panic("REQ_CMP with QFRZN");
4164                         if (bp->bio_cmd == BIO_ZONE)
4165                                 dazonedone(periph, done_ccb);
4166                         else if (state == DA_CCB_DELETE)
4167                                 bp->bio_resid = 0;
4168                         else
4169                                 bp->bio_resid = csio->resid;
4170                         if ((csio->resid > 0)
4171                          && (bp->bio_cmd != BIO_ZONE))
4172                                 bp->bio_flags |= BIO_ERROR;
4173                         if (softc->error_inject != 0) {
4174                                 bp->bio_error = softc->error_inject;
4175                                 bp->bio_resid = bp->bio_bcount;
4176                                 bp->bio_flags |= BIO_ERROR;
4177                                 softc->error_inject = 0;
4178                         }
4179                 }
4180
4181                 if (bp != NULL)
4182                         biotrack(bp, __func__);
4183                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
4184                 if (LIST_EMPTY(&softc->pending_ccbs))
4185                         softc->flags |= DA_FLAG_WAS_OTAG;
4186
4187                 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
4188                 xpt_release_ccb(done_ccb);
4189                 if (state == DA_CCB_DELETE) {
4190                         TAILQ_HEAD(, bio) queue;
4191
4192                         TAILQ_INIT(&queue);
4193                         TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
4194                         softc->delete_run_queue.insert_point = NULL;
4195                         /*
4196                          * Normally, the xpt_release_ccb() above would make sure
4197                          * that when we have more work to do, that work would
4198                          * get kicked off. However, we specifically keep
4199                          * delete_running set to 0 before the call above to
4200                          * allow other I/O to progress when many BIO_DELETE
4201                          * requests are pushed down. We set delete_running to 0
4202                          * and call daschedule again so that we don't stall if
4203                          * there are no other I/Os pending apart from BIO_DELETEs.
4204                          */
4205                         cam_iosched_trim_done(softc->cam_iosched);
4206                         daschedule(periph);
4207                         cam_periph_unlock(periph);
4208                         while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
4209                                 TAILQ_REMOVE(&queue, bp1, bio_queue);
4210                                 bp1->bio_error = bp->bio_error;
4211                                 if (bp->bio_flags & BIO_ERROR) {
4212                                         bp1->bio_flags |= BIO_ERROR;
4213                                         bp1->bio_resid = bp1->bio_bcount;
4214                                 } else
4215                                         bp1->bio_resid = 0;
4216                                 biodone(bp1);
4217                         }
4218                 } else {
4219                         daschedule(periph);
4220                         cam_periph_unlock(periph);
4221                 }
4222                 if (bp != NULL)
4223                         biodone(bp);
4224                 return;
4225         }
4226         case DA_CCB_PROBE_RC:
4227         case DA_CCB_PROBE_RC16:
4228         {
4229                 struct     scsi_read_capacity_data *rdcap;
4230                 struct     scsi_read_capacity_data_long *rcaplong;
4231                 char       *announce_buf;
4232                 int        lbp;
4233
4234                 lbp = 0;
4235                 rdcap = NULL;
4236                 rcaplong = NULL;
4237                 /* XXX TODO: can this be a malloc? */
4238                 announce_buf = softc->announce_temp;
4239                 bzero(announce_buf, DA_ANNOUNCETMP_SZ);
4240
4241                 if (state == DA_CCB_PROBE_RC)
4242                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
4243                 else
4244                         rcaplong = (struct scsi_read_capacity_data_long *)
4245                                 csio->data_ptr;
4246
4247                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4248                         struct disk_params *dp;
4249                         uint32_t block_size;
4250                         uint64_t maxsector;
4251                         u_int lalba;    /* Lowest aligned LBA. */
4252
4253                         if (state == DA_CCB_PROBE_RC) {
4254                                 block_size = scsi_4btoul(rdcap->length);
4255                                 maxsector = scsi_4btoul(rdcap->addr);
4256                                 lalba = 0;
4257
4258                                 /*
4259                                  * According to SBC-2, if the standard 10
4260                                  * byte READ CAPACITY command returns 2^32,
4261                                  * we should issue the 16 byte version of
4262                                  * the command, since the device in question
4263                                  * has more sectors than can be represented
4264                                  * with the short version of the command.
4265                                  */
4266                                 if (maxsector == 0xffffffff) {
4267                                         free(rdcap, M_SCSIDA);
4268                                         xpt_release_ccb(done_ccb);
4269                                         softc->state = DA_STATE_PROBE_RC16;
4270                                         xpt_schedule(periph, priority);
4271                                         return;
4272                                 }
4273                         } else {
4274                                 block_size = scsi_4btoul(rcaplong->length);
4275                                 maxsector = scsi_8btou64(rcaplong->addr);
4276                                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
4277                         }
4278
4279                         /*
4280                          * Because GEOM code just will panic us if we
4281                          * give them an 'illegal' value we'll avoid that
4282                          * here.
4283                          */
4284                         if (block_size == 0) {
4285                                 block_size = 512;
4286                                 if (maxsector == 0)
4287                                         maxsector = -1;
4288                         }
4289                         if (block_size >= MAXPHYS) {
4290                                 xpt_print(periph->path,
4291                                     "unsupportable block size %ju\n",
4292                                     (uintmax_t) block_size);
4293                                 announce_buf = NULL;
4294                                 cam_periph_invalidate(periph);
4295                         } else {
4296                                 /*
4297                                  * We pass rcaplong into dasetgeom(),
4298                                  * because it will only use it if it is
4299                                  * non-NULL.
4300                                  */
4301                                 dasetgeom(periph, block_size, maxsector,
4302                                           rcaplong, sizeof(*rcaplong));
4303                                 lbp = (lalba & SRC16_LBPME_A);
4304                                 dp = &softc->params;
4305                                 snprintf(announce_buf, DA_ANNOUNCETMP_SZ,
4306                                     "%juMB (%ju %u byte sectors)",
4307                                     ((uintmax_t)dp->secsize * dp->sectors) /
4308                                      (1024 * 1024),
4309                                     (uintmax_t)dp->sectors, dp->secsize);
4310                         }
4311                 } else {
4312                         int     error;
4313
4314                         /*
4315                          * Retry any UNIT ATTENTION type errors.  They
4316                          * are expected at boot.
4317                          */
4318                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4319                                         SF_RETRY_UA|SF_NO_PRINT);
4320                         if (error == ERESTART) {
4321                                 /*
4322                                  * A retry was scheuled, so
4323                                  * just return.
4324                                  */
4325                                 return;
4326                         } else if (error != 0) {
4327                                 int asc, ascq;
4328                                 int sense_key, error_code;
4329                                 int have_sense;
4330                                 cam_status status;
4331                                 struct ccb_getdev cgd;
4332
4333                                 /* Don't wedge this device's queue */
4334                                 status = done_ccb->ccb_h.status;
4335                                 if ((status & CAM_DEV_QFRZN) != 0)
4336                                         cam_release_devq(done_ccb->ccb_h.path,
4337                                                          /*relsim_flags*/0,
4338                                                          /*reduction*/0,
4339                                                          /*timeout*/0,
4340                                                          /*getcount_only*/0);
4341
4342
4343                                 xpt_setup_ccb(&cgd.ccb_h, 
4344                                               done_ccb->ccb_h.path,
4345                                               CAM_PRIORITY_NORMAL);
4346                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4347                                 xpt_action((union ccb *)&cgd);
4348
4349                                 if (scsi_extract_sense_ccb(done_ccb,
4350                                     &error_code, &sense_key, &asc, &ascq))
4351                                         have_sense = TRUE;
4352                                 else
4353                                         have_sense = FALSE;
4354
4355                                 /*
4356                                  * If we tried READ CAPACITY(16) and failed,
4357                                  * fallback to READ CAPACITY(10).
4358                                  */
4359                                 if ((state == DA_CCB_PROBE_RC16) &&
4360                                     (softc->flags & DA_FLAG_CAN_RC16) &&
4361                                     (((csio->ccb_h.status & CAM_STATUS_MASK) ==
4362                                         CAM_REQ_INVALID) ||
4363                                      ((have_sense) &&
4364                                       (error_code == SSD_CURRENT_ERROR) &&
4365                                       (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
4366                                         softc->flags &= ~DA_FLAG_CAN_RC16;
4367                                         free(rdcap, M_SCSIDA);
4368                                         xpt_release_ccb(done_ccb);
4369                                         softc->state = DA_STATE_PROBE_RC;
4370                                         xpt_schedule(periph, priority);
4371                                         return;
4372                                 }
4373
4374                                 /*
4375                                  * Attach to anything that claims to be a
4376                                  * direct access or optical disk device,
4377                                  * as long as it doesn't return a "Logical
4378                                  * unit not supported" (0x25) error.
4379                                  * "Internal Target Failure" (0x44) is also
4380                                  * special and typically means that the
4381                                  * device is a SATA drive behind a SATL
4382                                  * translation that's fallen into a
4383                                  * terminally fatal state.
4384                                  */
4385                                 if ((have_sense)
4386                                  && (asc != 0x25) && (asc != 0x44)
4387                                  && (error_code == SSD_CURRENT_ERROR)) {
4388                                         const char *sense_key_desc;
4389                                         const char *asc_desc;
4390
4391                                         dasetgeom(periph, 512, -1, NULL, 0);
4392                                         scsi_sense_desc(sense_key, asc, ascq,
4393                                                         &cgd.inq_data,
4394                                                         &sense_key_desc,
4395                                                         &asc_desc);
4396                                         snprintf(announce_buf,
4397                                             DA_ANNOUNCETMP_SZ,
4398                                             "Attempt to query device "
4399                                             "size failed: %s, %s",
4400                                             sense_key_desc, asc_desc);
4401                                 } else { 
4402                                         if (have_sense)
4403                                                 scsi_sense_print(
4404                                                         &done_ccb->csio);
4405                                         else {
4406                                                 xpt_print(periph->path,
4407                                                     "got CAM status %#x\n",
4408                                                     done_ccb->ccb_h.status);
4409                                         }
4410
4411                                         xpt_print(periph->path, "fatal error, "
4412                                             "failed to attach to device\n");
4413
4414                                         announce_buf = NULL;
4415
4416                                         /*
4417                                          * Free up resources.
4418                                          */
4419                                         cam_periph_invalidate(periph);
4420                                 } 
4421                         }
4422                 }
4423                 free(csio->data_ptr, M_SCSIDA);
4424                 if (announce_buf != NULL &&
4425                     ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
4426                         struct sbuf sb;
4427
4428                         sbuf_new(&sb, softc->announcebuf, DA_ANNOUNCE_SZ,
4429                             SBUF_FIXEDLEN);
4430                         xpt_announce_periph_sbuf(periph, &sb, announce_buf);
4431                         xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
4432                             DA_Q_BIT_STRING);
4433                         sbuf_finish(&sb);
4434                         sbuf_putbuf(&sb);
4435
4436                         /*
4437                          * Create our sysctl variables, now that we know
4438                          * we have successfully attached.
4439                          */
4440                         /* increase the refcount */
4441                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
4442
4443                                 taskqueue_enqueue(taskqueue_thread,
4444                                                   &softc->sysctl_task);
4445                         } else {
4446                                 /* XXX This message is useless! */
4447                                 xpt_print(periph->path, "fatal error, "
4448                                     "could not acquire reference count\n");
4449                         }
4450                 }
4451
4452                 /* We already probed the device. */
4453                 if (softc->flags & DA_FLAG_PROBED) {
4454                         daprobedone(periph, done_ccb);
4455                         return;
4456                 }
4457
4458                 /* Ensure re-probe doesn't see old delete. */
4459                 softc->delete_available = 0;
4460                 dadeleteflag(softc, DA_DELETE_ZERO, 1);
4461                 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
4462                         /*
4463                          * Based on older SBC-3 spec revisions
4464                          * any of the UNMAP methods "may" be
4465                          * available via LBP given this flag so
4466                          * we flag all of them as available and
4467                          * then remove those which further
4468                          * probes confirm aren't available
4469                          * later.
4470                          *
4471                          * We could also check readcap(16) p_type
4472                          * flag to exclude one or more invalid
4473                          * write same (X) types here
4474                          */
4475                         dadeleteflag(softc, DA_DELETE_WS16, 1);
4476                         dadeleteflag(softc, DA_DELETE_WS10, 1);
4477                         dadeleteflag(softc, DA_DELETE_UNMAP, 1);
4478
4479                         xpt_release_ccb(done_ccb);
4480                         softc->state = DA_STATE_PROBE_LBP;
4481                         xpt_schedule(periph, priority);
4482                         return;
4483                 }
4484
4485                 xpt_release_ccb(done_ccb);
4486                 softc->state = DA_STATE_PROBE_BDC;
4487                 xpt_schedule(periph, priority);
4488                 return;
4489         }
4490         case DA_CCB_PROBE_LBP:
4491         {
4492                 struct scsi_vpd_logical_block_prov *lbp;
4493
4494                 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
4495
4496                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4497                         /*
4498                          * T10/1799-D Revision 31 states at least one of these
4499                          * must be supported but we don't currently enforce this.
4500                          */
4501                         dadeleteflag(softc, DA_DELETE_WS16,
4502                                      (lbp->flags & SVPD_LBP_WS16));
4503                         dadeleteflag(softc, DA_DELETE_WS10,
4504                                      (lbp->flags & SVPD_LBP_WS10));
4505                         dadeleteflag(softc, DA_DELETE_UNMAP,
4506                                      (lbp->flags & SVPD_LBP_UNMAP));
4507                 } else {
4508                         int error;
4509                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4510                                         SF_RETRY_UA|SF_NO_PRINT);
4511                         if (error == ERESTART)
4512                                 return;
4513                         else if (error != 0) {
4514                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4515                                         /* Don't wedge this device's queue */
4516                                         cam_release_devq(done_ccb->ccb_h.path,
4517                                                          /*relsim_flags*/0,
4518                                                          /*reduction*/0,
4519                                                          /*timeout*/0,
4520                                                          /*getcount_only*/0);
4521                                 }
4522
4523                                 /*
4524                                  * Failure indicates we don't support any SBC-3
4525                                  * delete methods with UNMAP
4526                                  */
4527                         }
4528                 }
4529
4530                 free(lbp, M_SCSIDA);
4531                 xpt_release_ccb(done_ccb);
4532                 softc->state = DA_STATE_PROBE_BLK_LIMITS;
4533                 xpt_schedule(periph, priority);
4534                 return;
4535         }
4536         case DA_CCB_PROBE_BLK_LIMITS:
4537         {
4538                 struct scsi_vpd_block_limits *block_limits;
4539
4540                 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
4541
4542                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4543                         uint32_t max_txfer_len = scsi_4btoul(
4544                                 block_limits->max_txfer_len);
4545                         uint32_t max_unmap_lba_cnt = scsi_4btoul(
4546                                 block_limits->max_unmap_lba_cnt);
4547                         uint32_t max_unmap_blk_cnt = scsi_4btoul(
4548                                 block_limits->max_unmap_blk_cnt);
4549                         uint32_t unmap_gran = scsi_4btoul(
4550                                 block_limits->opt_unmap_grain);
4551                         uint32_t unmap_gran_align = scsi_4btoul(
4552                                 block_limits->unmap_grain_align);
4553                         uint64_t ws_max_blks = scsi_8btou64(
4554                                 block_limits->max_write_same_length);
4555
4556                         if (max_txfer_len != 0) {
4557                                 softc->disk->d_maxsize = MIN(softc->maxio,
4558                                     (off_t)max_txfer_len * softc->params.secsize);
4559                         }
4560
4561                         /*
4562                          * We should already support UNMAP but we check lba
4563                          * and block count to be sure
4564                          */
4565                         if (max_unmap_lba_cnt != 0x00L &&
4566                             max_unmap_blk_cnt != 0x00L) {
4567                                 softc->unmap_max_lba = max_unmap_lba_cnt;
4568                                 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
4569                                         UNMAP_MAX_RANGES);
4570                                 if (unmap_gran > 1) {
4571                                         softc->unmap_gran = unmap_gran;
4572                                         if (unmap_gran_align & 0x80000000) {
4573                                                 softc->unmap_gran_align =
4574                                                     unmap_gran_align &
4575                                                     0x7fffffff;
4576                                         }
4577                                 }
4578                         } else {
4579                                 /*
4580                                  * Unexpected UNMAP limits which means the
4581                                  * device doesn't actually support UNMAP
4582                                  */
4583                                 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
4584                         }
4585
4586                         if (ws_max_blks != 0x00L)
4587                                 softc->ws_max_blks = ws_max_blks;
4588                 } else {
4589                         int error;
4590                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4591                                         SF_RETRY_UA|SF_NO_PRINT);
4592                         if (error == ERESTART)
4593                                 return;
4594                         else if (error != 0) {
4595                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4596                                         /* Don't wedge this device's queue */
4597                                         cam_release_devq(done_ccb->ccb_h.path,
4598                                                          /*relsim_flags*/0,
4599                                                          /*reduction*/0,
4600                                                          /*timeout*/0,
4601                                                          /*getcount_only*/0);
4602                                 }
4603
4604                                 /*
4605                                  * Failure here doesn't mean UNMAP is not
4606                                  * supported as this is an optional page.
4607                                  */
4608                                 softc->unmap_max_lba = 1;
4609                                 softc->unmap_max_ranges = 1;
4610                         }
4611                 }
4612
4613                 free(block_limits, M_SCSIDA);
4614                 xpt_release_ccb(done_ccb);
4615                 softc->state = DA_STATE_PROBE_BDC;
4616                 xpt_schedule(periph, priority);
4617                 return;
4618         }
4619         case DA_CCB_PROBE_BDC:
4620         {
4621                 struct scsi_vpd_block_device_characteristics *bdc;
4622
4623                 bdc = (struct scsi_vpd_block_device_characteristics *)
4624                     csio->data_ptr;
4625
4626                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4627                         uint32_t valid_len;
4628
4629                         /*
4630                          * Disable queue sorting for non-rotational media
4631                          * by default.
4632                          */
4633                         u_int16_t old_rate = softc->disk->d_rotation_rate;
4634
4635                         valid_len = csio->dxfer_len - csio->resid;
4636                         if (SBDC_IS_PRESENT(bdc, valid_len,
4637                             medium_rotation_rate)) {
4638                                 softc->disk->d_rotation_rate =
4639                                         scsi_2btoul(bdc->medium_rotation_rate);
4640                                 if (softc->disk->d_rotation_rate ==
4641                                     SVPD_BDC_RATE_NON_ROTATING) {
4642                                         cam_iosched_set_sort_queue(
4643                                             softc->cam_iosched, 0);
4644                                         softc->rotating = 0;
4645                                 }
4646                                 if (softc->disk->d_rotation_rate != old_rate) {
4647                                         disk_attr_changed(softc->disk,
4648                                             "GEOM::rotation_rate", M_NOWAIT);
4649                                 }
4650                         }
4651                         if ((SBDC_IS_PRESENT(bdc, valid_len, flags))
4652                          && (softc->zone_mode == DA_ZONE_NONE)) {
4653                                 int ata_proto;
4654
4655                                 if (scsi_vpd_supported_page(periph,
4656                                     SVPD_ATA_INFORMATION))
4657                                         ata_proto = 1;
4658                                 else
4659                                         ata_proto = 0;
4660
4661                                 /*
4662                                  * The Zoned field will only be set for
4663                                  * Drive Managed and Host Aware drives.  If
4664                                  * they are Host Managed, the device type
4665                                  * in the standard INQUIRY data should be
4666                                  * set to T_ZBC_HM (0x14).
4667                                  */
4668                                 if ((bdc->flags & SVPD_ZBC_MASK) ==
4669                                      SVPD_HAW_ZBC) {
4670                                         softc->zone_mode = DA_ZONE_HOST_AWARE;
4671                                         softc->zone_interface = (ata_proto) ?
4672                                            DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
4673                                 } else if ((bdc->flags & SVPD_ZBC_MASK) ==
4674                                      SVPD_DM_ZBC) {
4675                                         softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
4676                                         softc->zone_interface = (ata_proto) ?
4677                                            DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
4678                                 } else if ((bdc->flags & SVPD_ZBC_MASK) != 
4679                                           SVPD_ZBC_NR) {
4680                                         xpt_print(periph->path, "Unknown zoned "
4681                                             "type %#x",
4682                                             bdc->flags & SVPD_ZBC_MASK);
4683                                 }
4684                         }
4685                 } else {
4686                         int error;
4687                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4688                                         SF_RETRY_UA|SF_NO_PRINT);
4689                         if (error == ERESTART)
4690                                 return;
4691                         else if (error != 0) {
4692                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4693                                         /* Don't wedge this device's queue */
4694                                         cam_release_devq(done_ccb->ccb_h.path,
4695                                                          /*relsim_flags*/0,
4696                                                          /*reduction*/0,
4697                                                          /*timeout*/0,
4698                                                          /*getcount_only*/0);
4699                                 }
4700                         }
4701                 }
4702
4703                 free(bdc, M_SCSIDA);
4704                 xpt_release_ccb(done_ccb);
4705                 softc->state = DA_STATE_PROBE_ATA;
4706                 xpt_schedule(periph, priority);
4707                 return;
4708         }
4709         case DA_CCB_PROBE_ATA:
4710         {
4711                 int i;
4712                 struct ata_params *ata_params;
4713                 int continue_probe;
4714                 int error;
4715                 int16_t *ptr;
4716
4717                 ata_params = (struct ata_params *)csio->data_ptr;
4718                 ptr = (uint16_t *)ata_params;
4719                 continue_probe = 0;
4720                 error = 0;
4721
4722                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4723                         uint16_t old_rate;
4724
4725                         for (i = 0; i < sizeof(*ata_params) / 2; i++)
4726                                 ptr[i] = le16toh(ptr[i]);
4727                         if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
4728                             (softc->quirks & DA_Q_NO_UNMAP) == 0) {
4729                                 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
4730                                 if (ata_params->max_dsm_blocks != 0)
4731                                         softc->trim_max_ranges = min(
4732                                           softc->trim_max_ranges,
4733                                           ata_params->max_dsm_blocks *
4734                                           ATA_DSM_BLK_RANGES);
4735                         }
4736                         /*
4737                          * Disable queue sorting for non-rotational media
4738                          * by default.
4739                          */
4740                         old_rate = softc->disk->d_rotation_rate;
4741                         softc->disk->d_rotation_rate =
4742                             ata_params->media_rotation_rate;
4743                         if (softc->disk->d_rotation_rate ==
4744                             ATA_RATE_NON_ROTATING) {
4745                                 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
4746                                 softc->rotating = 0;
4747                         }
4748                         if (softc->disk->d_rotation_rate != old_rate) {
4749                                 disk_attr_changed(softc->disk,
4750                                     "GEOM::rotation_rate", M_NOWAIT);
4751                         }
4752
4753                         if (ata_params->capabilities1 & ATA_SUPPORT_DMA)
4754                                 softc->flags |= DA_FLAG_CAN_ATA_DMA;
4755
4756                         if (ata_params->support.extension &
4757                             ATA_SUPPORT_GENLOG)
4758                                 softc->flags |= DA_FLAG_CAN_ATA_LOG;
4759
4760                         /*
4761                          * At this point, if we have a SATA host aware drive,
4762                          * we communicate via ATA passthrough unless the
4763                          * SAT layer supports ZBC -> ZAC translation.  In
4764                          * that case,
4765                          */
4766                         /*
4767                          * XXX KDM figure out how to detect a host managed
4768                          * SATA drive.
4769                          */
4770                         if (softc->zone_mode == DA_ZONE_NONE) {
4771                                 /*
4772                                  * Note that we don't override the zone
4773                                  * mode or interface if it has already been
4774                                  * set.  This is because it has either been
4775                                  * set as a quirk, or when we probed the
4776                                  * SCSI Block Device Characteristics page,
4777                                  * the zoned field was set.  The latter
4778                                  * means that the SAT layer supports ZBC to
4779                                  * ZAC translation, and we would prefer to
4780                                  * use that if it is available.
4781                                  */
4782                                 if ((ata_params->support3 &
4783                                     ATA_SUPPORT_ZONE_MASK) ==
4784                                     ATA_SUPPORT_ZONE_HOST_AWARE) {
4785                                         softc->zone_mode = DA_ZONE_HOST_AWARE;
4786                                         softc->zone_interface =
4787                                             DA_ZONE_IF_ATA_PASS;
4788                                 } else if ((ata_params->support3 &
4789                                             ATA_SUPPORT_ZONE_MASK) ==
4790                                             ATA_SUPPORT_ZONE_DEV_MANAGED) {
4791                                         softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
4792                                         softc->zone_interface =
4793                                             DA_ZONE_IF_ATA_PASS;
4794                                 }
4795                         }
4796
4797                 } else {
4798                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4799                                         SF_RETRY_UA|SF_NO_PRINT);
4800                         if (error == ERESTART)
4801                                 return;
4802                         else if (error != 0) {
4803                                 if ((done_ccb->ccb_h.status &
4804                                      CAM_DEV_QFRZN) != 0) {
4805                                         /* Don't wedge this device's queue */
4806                                         cam_release_devq(done_ccb->ccb_h.path,
4807                                                          /*relsim_flags*/0,
4808                                                          /*reduction*/0,
4809                                                          /*timeout*/0,
4810                                                          /*getcount_only*/0);
4811                                 }
4812                         }
4813                 }
4814
4815                 free(ata_params, M_SCSIDA);
4816                 if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
4817                  || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
4818                         /*
4819                          * If the ATA IDENTIFY failed, we could be talking
4820                          * to a SCSI drive, although that seems unlikely,
4821                          * since the drive did report that it supported the 
4822                          * ATA Information VPD page.  If the ATA IDENTIFY
4823                          * succeeded, and the SAT layer doesn't support
4824                          * ZBC -> ZAC translation, continue on to get the
4825                          * directory of ATA logs, and complete the rest of
4826                          * the ZAC probe.  If the SAT layer does support
4827                          * ZBC -> ZAC translation, we want to use that,
4828                          * and we'll probe the SCSI Zoned Block Device
4829                          * Characteristics VPD page next.
4830                          */
4831                         if ((error == 0)
4832                          && (softc->flags & DA_FLAG_CAN_ATA_LOG)
4833                          && (softc->zone_interface == DA_ZONE_IF_ATA_PASS))
4834                                 softc->state = DA_STATE_PROBE_ATA_LOGDIR;
4835                         else
4836                                 softc->state = DA_STATE_PROBE_ZONE;
4837                         continue_probe = 1;
4838                 }
4839                 if (continue_probe != 0) {
4840                         xpt_release_ccb(done_ccb);
4841                         xpt_schedule(periph, priority);
4842                         return;
4843                 } else
4844                         daprobedone(periph, done_ccb);
4845                 return;
4846         }
4847         case DA_CCB_PROBE_ATA_LOGDIR:
4848         {
4849                 int error;
4850
4851                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4852                         error = 0;
4853                         softc->valid_logdir_len = 0;
4854                         bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
4855                         softc->valid_logdir_len =
4856                                 csio->dxfer_len - csio->resid;
4857                         if (softc->valid_logdir_len > 0)
4858                                 bcopy(csio->data_ptr, &softc->ata_logdir,
4859                                     min(softc->valid_logdir_len,
4860                                         sizeof(softc->ata_logdir)));
4861                         /*
4862                          * Figure out whether the Identify Device log is
4863                          * supported.  The General Purpose log directory
4864                          * has a header, and lists the number of pages
4865                          * available for each GP log identified by the
4866                          * offset into the list.
4867                          */
4868                         if ((softc->valid_logdir_len >=
4869                             ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
4870                          && (le16dec(softc->ata_logdir.header) == 
4871                              ATA_GP_LOG_DIR_VERSION)
4872                          && (le16dec(&softc->ata_logdir.num_pages[
4873                              (ATA_IDENTIFY_DATA_LOG *
4874                              sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
4875                                 softc->flags |= DA_FLAG_CAN_ATA_IDLOG;
4876                         } else {
4877                                 softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
4878                         }
4879                 } else {
4880                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4881                                         SF_RETRY_UA|SF_NO_PRINT);
4882                         if (error == ERESTART)
4883                                 return;
4884                         else if (error != 0) {
4885                                 /*
4886                                  * If we can't get the ATA log directory,
4887                                  * then ATA logs are effectively not
4888                                  * supported even if the bit is set in the
4889                                  * identify data.
4890                                  */ 
4891                                 softc->flags &= ~(DA_FLAG_CAN_ATA_LOG |
4892                                                   DA_FLAG_CAN_ATA_IDLOG);
4893                                 if ((done_ccb->ccb_h.status &
4894                                      CAM_DEV_QFRZN) != 0) {
4895                                         /* Don't wedge this device's queue */
4896                                         cam_release_devq(done_ccb->ccb_h.path,
4897                                                          /*relsim_flags*/0,
4898                                                          /*reduction*/0,
4899                                                          /*timeout*/0,
4900                                                          /*getcount_only*/0);
4901                                 }
4902                         }
4903                 }
4904
4905                 free(csio->data_ptr, M_SCSIDA);
4906
4907                 if ((error == 0)
4908                  && (softc->flags & DA_FLAG_CAN_ATA_IDLOG)) {
4909                         softc->state = DA_STATE_PROBE_ATA_IDDIR;
4910                         xpt_release_ccb(done_ccb);
4911                         xpt_schedule(periph, priority);
4912                         return;
4913                 } 
4914                 daprobedone(periph, done_ccb);
4915                 return;
4916         }
4917         case DA_CCB_PROBE_ATA_IDDIR:
4918         {
4919                 int error;
4920
4921                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4922                         off_t entries_offset, max_entries;
4923                         error = 0;
4924
4925                         softc->valid_iddir_len = 0;
4926                         bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
4927                         softc->flags &= ~(DA_FLAG_CAN_ATA_SUPCAP |
4928                                           DA_FLAG_CAN_ATA_ZONE);
4929                         softc->valid_iddir_len =
4930                                 csio->dxfer_len - csio->resid;
4931                         if (softc->valid_iddir_len > 0)
4932                                 bcopy(csio->data_ptr, &softc->ata_iddir,
4933                                     min(softc->valid_iddir_len,
4934                                         sizeof(softc->ata_iddir)));
4935
4936                         entries_offset =
4937                             __offsetof(struct ata_identify_log_pages,entries);
4938                         max_entries = softc->valid_iddir_len - entries_offset;
4939                         if ((softc->valid_iddir_len > (entries_offset + 1))
4940                          && (le64dec(softc->ata_iddir.header) ==
4941                              ATA_IDLOG_REVISION)
4942                          && (softc->ata_iddir.entry_count > 0)) {
4943                                 int num_entries, i;
4944
4945                                 num_entries = softc->ata_iddir.entry_count;
4946                                 num_entries = min(num_entries,
4947                                    softc->valid_iddir_len - entries_offset);
4948                                 for (i = 0; i < num_entries &&
4949                                      i < max_entries; i++) {
4950                                         if (softc->ata_iddir.entries[i] ==
4951                                             ATA_IDL_SUP_CAP)
4952                                                 softc->flags |=
4953                                                     DA_FLAG_CAN_ATA_SUPCAP;
4954                                         else if (softc->ata_iddir.entries[i]==
4955                                                  ATA_IDL_ZDI)
4956                                                 softc->flags |=
4957                                                     DA_FLAG_CAN_ATA_ZONE;
4958
4959                                         if ((softc->flags &
4960                                              DA_FLAG_CAN_ATA_SUPCAP)
4961                                          && (softc->flags &
4962                                              DA_FLAG_CAN_ATA_ZONE))
4963                                                 break;
4964                                 }
4965                         }
4966                 } else {
4967                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4968                                         SF_RETRY_UA|SF_NO_PRINT);
4969                         if (error == ERESTART)
4970                                 return;
4971                         else if (error != 0) {
4972                                 /*
4973                                  * If we can't get the ATA Identify Data log
4974                                  * directory, then it effectively isn't
4975                                  * supported even if the ATA Log directory
4976                                  * a non-zero number of pages present for
4977                                  * this log.
4978                                  */
4979                                 softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
4980                                 if ((done_ccb->ccb_h.status &
4981                                      CAM_DEV_QFRZN) != 0) {
4982                                         /* Don't wedge this device's queue */
4983                                         cam_release_devq(done_ccb->ccb_h.path,
4984                                                          /*relsim_flags*/0,
4985                                                          /*reduction*/0,
4986                                                          /*timeout*/0,
4987                                                          /*getcount_only*/0);
4988                                 }
4989                         }
4990                 }
4991
4992                 free(csio->data_ptr, M_SCSIDA);
4993
4994                 if ((error == 0)
4995                  && (softc->flags & DA_FLAG_CAN_ATA_SUPCAP)) {
4996                         softc->state = DA_STATE_PROBE_ATA_SUP;
4997                         xpt_release_ccb(done_ccb);
4998                         xpt_schedule(periph, priority);
4999                         return;
5000                 } 
5001                 daprobedone(periph, done_ccb);
5002                 return;
5003         }
5004         case DA_CCB_PROBE_ATA_SUP:
5005         {
5006                 int error;
5007
5008                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5009                         uint32_t valid_len;
5010                         size_t needed_size;
5011                         struct ata_identify_log_sup_cap *sup_cap;
5012                         error = 0;
5013
5014                         sup_cap = (struct ata_identify_log_sup_cap *)
5015                             csio->data_ptr;
5016                         valid_len = csio->dxfer_len - csio->resid;
5017                         needed_size =
5018                             __offsetof(struct ata_identify_log_sup_cap,
5019                             sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
5020                         if (valid_len >= needed_size) {
5021                                 uint64_t zoned, zac_cap;
5022
5023                                 zoned = le64dec(sup_cap->zoned_cap);
5024                                 if (zoned & ATA_ZONED_VALID) {
5025                                         /*
5026                                          * This should have already been
5027                                          * set, because this is also in the
5028                                          * ATA identify data.
5029                                          */
5030                                         if ((zoned & ATA_ZONED_MASK) ==
5031                                             ATA_SUPPORT_ZONE_HOST_AWARE)
5032                                                 softc->zone_mode =
5033                                                     DA_ZONE_HOST_AWARE;
5034                                         else if ((zoned & ATA_ZONED_MASK) ==
5035                                             ATA_SUPPORT_ZONE_DEV_MANAGED)
5036                                                 softc->zone_mode =
5037                                                     DA_ZONE_DRIVE_MANAGED;
5038                                 }
5039
5040                                 zac_cap = le64dec(sup_cap->sup_zac_cap);
5041                                 if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
5042                                         if (zac_cap & ATA_REPORT_ZONES_SUP)
5043                                                 softc->zone_flags |=
5044                                                     DA_ZONE_FLAG_RZ_SUP;
5045                                         if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
5046                                                 softc->zone_flags |=
5047                                                     DA_ZONE_FLAG_OPEN_SUP;
5048                                         if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
5049                                                 softc->zone_flags |=
5050                                                     DA_ZONE_FLAG_CLOSE_SUP;
5051                                         if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
5052                                                 softc->zone_flags |=
5053                                                     DA_ZONE_FLAG_FINISH_SUP;
5054                                         if (zac_cap & ATA_ND_RWP_SUP)
5055                                                 softc->zone_flags |=
5056                                                     DA_ZONE_FLAG_RWP_SUP;
5057                                 } else {
5058                                         /*
5059                                          * This field was introduced in
5060                                          * ACS-4, r08 on April 28th, 2015.
5061                                          * If the drive firmware was written
5062                                          * to an earlier spec, it won't have
5063                                          * the field.  So, assume all
5064                                          * commands are supported.
5065                                          */ 
5066                                         softc->zone_flags |=
5067                                             DA_ZONE_FLAG_SUP_MASK;
5068                                 }
5069                                             
5070                         }
5071                 } else {
5072                         error = daerror(done_ccb, CAM_RETRY_SELTO,
5073                                         SF_RETRY_UA|SF_NO_PRINT);
5074                         if (error == ERESTART)
5075                                 return;
5076                         else if (error != 0) {
5077                                 /*
5078                                  * If we can't get the ATA Identify Data
5079                                  * Supported Capabilities page, clear the
5080                                  * flag...
5081                                  */
5082                                 softc->flags &= ~DA_FLAG_CAN_ATA_SUPCAP;
5083                                 /*
5084                                  * And clear zone capabilities.
5085                                  */
5086                                 softc->zone_flags &= ~DA_ZONE_FLAG_SUP_MASK;
5087                                 if ((done_ccb->ccb_h.status &
5088                                      CAM_DEV_QFRZN) != 0) {
5089                                         /* Don't wedge this device's queue */
5090                                         cam_release_devq(done_ccb->ccb_h.path,
5091                                                          /*relsim_flags*/0,
5092                                                          /*reduction*/0,
5093                                                          /*timeout*/0,
5094                                                          /*getcount_only*/0);
5095                                 }
5096                         }
5097                 }
5098
5099                 free(csio->data_ptr, M_SCSIDA);
5100
5101                 if ((error == 0)
5102                  && (softc->flags & DA_FLAG_CAN_ATA_ZONE)) {
5103                         softc->state = DA_STATE_PROBE_ATA_ZONE;
5104                         xpt_release_ccb(done_ccb);
5105                         xpt_schedule(periph, priority);
5106                         return;
5107                 } 
5108                 daprobedone(periph, done_ccb);
5109                 return;
5110         }
5111         case DA_CCB_PROBE_ATA_ZONE:
5112         {
5113                 int error;
5114
5115                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5116                         struct ata_zoned_info_log *zi_log;
5117                         uint32_t valid_len;
5118                         size_t needed_size;
5119
5120                         zi_log = (struct ata_zoned_info_log *)csio->data_ptr;
5121
5122                         valid_len = csio->dxfer_len - csio->resid;
5123                         needed_size = __offsetof(struct ata_zoned_info_log,
5124                             version_info) + 1 + sizeof(zi_log->version_info);
5125                         if (valid_len >= needed_size) {
5126                                 uint64_t tmpvar;
5127
5128                                 tmpvar = le64dec(zi_log->zoned_cap);
5129                                 if (tmpvar & ATA_ZDI_CAP_VALID) {
5130                                         if (tmpvar & ATA_ZDI_CAP_URSWRZ)
5131                                                 softc->zone_flags |=
5132                                                     DA_ZONE_FLAG_URSWRZ;
5133                                         else
5134                                                 softc->zone_flags &=
5135                                                     ~DA_ZONE_FLAG_URSWRZ;
5136                                 }
5137                                 tmpvar = le64dec(zi_log->optimal_seq_zones);
5138                                 if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
5139                                         softc->zone_flags |=
5140                                             DA_ZONE_FLAG_OPT_SEQ_SET;
5141                                         softc->optimal_seq_zones = (tmpvar &
5142                                             ATA_ZDI_OPT_SEQ_MASK);
5143                                 } else {
5144                                         softc->zone_flags &=
5145                                             ~DA_ZONE_FLAG_OPT_SEQ_SET;
5146                                         softc->optimal_seq_zones = 0;
5147                                 }
5148
5149                                 tmpvar =le64dec(zi_log->optimal_nonseq_zones);
5150                                 if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
5151                                         softc->zone_flags |=
5152                                             DA_ZONE_FLAG_OPT_NONSEQ_SET;
5153                                         softc->optimal_nonseq_zones =
5154                                             (tmpvar & ATA_ZDI_OPT_NS_MASK);
5155                                 } else {
5156                                         softc->zone_flags &=
5157                                             ~DA_ZONE_FLAG_OPT_NONSEQ_SET;
5158                                         softc->optimal_nonseq_zones = 0;
5159                                 }
5160
5161                                 tmpvar = le64dec(zi_log->max_seq_req_zones);
5162                                 if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
5163                                         softc->zone_flags |=
5164                                             DA_ZONE_FLAG_MAX_SEQ_SET;
5165                                         softc->max_seq_zones =
5166                                             (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
5167                                 } else {
5168                                         softc->zone_flags &=
5169                                             ~DA_ZONE_FLAG_MAX_SEQ_SET;
5170                                         softc->max_seq_zones = 0;
5171                                 }
5172                         }
5173                 } else {
5174                         error = daerror(done_ccb, CAM_RETRY_SELTO,
5175                                         SF_RETRY_UA|SF_NO_PRINT);
5176                         if (error == ERESTART)
5177                                 return;
5178                         else if (error != 0) {
5179                                 softc->flags &= ~DA_FLAG_CAN_ATA_ZONE;
5180                                 softc->flags &= ~DA_ZONE_FLAG_SET_MASK;
5181
5182                                 if ((done_ccb->ccb_h.status &
5183                                      CAM_DEV_QFRZN) != 0) {
5184                                         /* Don't wedge this device's queue */
5185                                         cam_release_devq(done_ccb->ccb_h.path,
5186                                                          /*relsim_flags*/0,
5187                                                          /*reduction*/0,
5188                                                          /*timeout*/0,
5189                                                          /*getcount_only*/0);
5190                                 }
5191                         }
5192         
5193                 }
5194                 free(csio->data_ptr, M_SCSIDA);
5195
5196                 daprobedone(periph, done_ccb);
5197                 return;
5198         }
5199         case DA_CCB_PROBE_ZONE:
5200         {
5201                 int error;
5202
5203                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5204                         uint32_t valid_len;
5205                         size_t needed_len;
5206                         struct scsi_vpd_zoned_bdc *zoned_bdc;
5207
5208                         error = 0;
5209                         zoned_bdc = (struct scsi_vpd_zoned_bdc *)
5210                                 csio->data_ptr;
5211                         valid_len = csio->dxfer_len - csio->resid;
5212                         needed_len = __offsetof(struct scsi_vpd_zoned_bdc,
5213                             max_seq_req_zones) + 1 +
5214                             sizeof(zoned_bdc->max_seq_req_zones);
5215                         if ((valid_len >= needed_len)
5216                          && (scsi_2btoul(zoned_bdc->page_length) >=
5217                              SVPD_ZBDC_PL)) {
5218                                 if (zoned_bdc->flags & SVPD_ZBDC_URSWRZ)
5219                                         softc->zone_flags |=
5220                                             DA_ZONE_FLAG_URSWRZ;
5221                                 else
5222                                         softc->zone_flags &= 
5223                                             ~DA_ZONE_FLAG_URSWRZ;
5224                                 softc->optimal_seq_zones =
5225                                     scsi_4btoul(zoned_bdc->optimal_seq_zones);
5226                                 softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5227                                 softc->optimal_nonseq_zones = scsi_4btoul(
5228                                     zoned_bdc->optimal_nonseq_zones);
5229                                 softc->zone_flags |=
5230                                     DA_ZONE_FLAG_OPT_NONSEQ_SET;
5231                                 softc->max_seq_zones =
5232                                     scsi_4btoul(zoned_bdc->max_seq_req_zones);
5233                                 softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5234                         }
5235                         /*
5236                          * All of the zone commands are mandatory for SCSI
5237                          * devices.
5238                          *
5239                          * XXX KDM this is valid as of September 2015.
5240                          * Re-check this assumption once the SAT spec is
5241                          * updated to support SCSI ZBC to ATA ZAC mapping.
5242                          * Since ATA allows zone commands to be reported
5243                          * as supported or not, this may not necessarily
5244                          * be true for an ATA device behind a SAT (SCSI to
5245                          * ATA Translation) layer.
5246                          */
5247                         softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5248                 } else {
5249                         error = daerror(done_ccb, CAM_RETRY_SELTO,
5250                                         SF_RETRY_UA|SF_NO_PRINT);
5251                         if (error == ERESTART)
5252                                 return;
5253                         else if (error != 0) {
5254                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5255                                         /* Don't wedge this device's queue */
5256                                         cam_release_devq(done_ccb->ccb_h.path,
5257                                                          /*relsim_flags*/0,
5258                                                          /*reduction*/0,
5259                                                          /*timeout*/0,
5260                                                          /*getcount_only*/0);
5261                                 }
5262                         }
5263                 }
5264                 daprobedone(periph, done_ccb);
5265                 return;
5266         }
5267         case DA_CCB_DUMP:
5268                 /* No-op.  We're polling */
5269                 return;
5270         case DA_CCB_TUR:
5271         {
5272                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5273
5274                         if (daerror(done_ccb, CAM_RETRY_SELTO,
5275                             SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
5276                             ERESTART)
5277                                 return;
5278                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5279                                 cam_release_devq(done_ccb->ccb_h.path,
5280                                                  /*relsim_flags*/0,
5281                                                  /*reduction*/0,
5282                                                  /*timeout*/0,
5283                                                  /*getcount_only*/0);
5284                 }
5285                 xpt_release_ccb(done_ccb);
5286                 cam_periph_release_locked(periph);
5287                 return;
5288         }
5289         default:
5290                 break;
5291         }
5292         xpt_release_ccb(done_ccb);
5293 }
5294
5295 static void
5296 dareprobe(struct cam_periph *periph)
5297 {
5298         struct da_softc   *softc;
5299         cam_status status;
5300
5301         softc = (struct da_softc *)periph->softc;
5302
5303         /* Probe in progress; don't interfere. */
5304         if (softc->state != DA_STATE_NORMAL)
5305                 return;
5306
5307         status = cam_periph_acquire(periph);
5308         KASSERT(status == CAM_REQ_CMP,
5309             ("dareprobe: cam_periph_acquire failed"));
5310
5311         if (softc->flags & DA_FLAG_CAN_RC16)
5312                 softc->state = DA_STATE_PROBE_RC16;
5313         else
5314                 softc->state = DA_STATE_PROBE_RC;
5315
5316         xpt_schedule(periph, CAM_PRIORITY_DEV);
5317 }
5318
5319 static int
5320 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
5321 {
5322         struct da_softc   *softc;
5323         struct cam_periph *periph;
5324         int error, error_code, sense_key, asc, ascq;
5325
5326 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5327         if (ccb->csio.bio != NULL)
5328                 biotrack(ccb->csio.bio, __func__);
5329 #endif
5330
5331         periph = xpt_path_periph(ccb->ccb_h.path);
5332         softc = (struct da_softc *)periph->softc;
5333
5334         /*
5335          * Automatically detect devices that do not support
5336          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
5337          */
5338         error = 0;
5339         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
5340                 error = cmd6workaround(ccb);
5341         } else if (scsi_extract_sense_ccb(ccb,
5342             &error_code, &sense_key, &asc, &ascq)) {
5343                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
5344                         error = cmd6workaround(ccb);
5345                 /*
5346                  * If the target replied with CAPACITY DATA HAS CHANGED UA,
5347                  * query the capacity and notify upper layers.
5348                  */
5349                 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5350                     asc == 0x2A && ascq == 0x09) {
5351                         xpt_print(periph->path, "Capacity data has changed\n");
5352                         softc->flags &= ~DA_FLAG_PROBED;
5353                         dareprobe(periph);
5354                         sense_flags |= SF_NO_PRINT;
5355                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5356                     asc == 0x28 && ascq == 0x00) {
5357                         softc->flags &= ~DA_FLAG_PROBED;
5358                         disk_media_changed(softc->disk, M_NOWAIT);
5359                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5360                     asc == 0x3F && ascq == 0x03) {
5361                         xpt_print(periph->path, "INQUIRY data has changed\n");
5362                         softc->flags &= ~DA_FLAG_PROBED;
5363                         dareprobe(periph);
5364                         sense_flags |= SF_NO_PRINT;
5365                 } else if (sense_key == SSD_KEY_NOT_READY &&
5366                     asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
5367                         softc->flags |= DA_FLAG_PACK_INVALID;
5368                         disk_media_gone(softc->disk, M_NOWAIT);
5369                 }
5370         }
5371         if (error == ERESTART)
5372                 return (ERESTART);
5373
5374 #ifdef CAM_IO_STATS
5375         switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
5376         case CAM_CMD_TIMEOUT:
5377                 softc->timeouts++;
5378                 break;
5379         case CAM_REQ_ABORTED:
5380         case CAM_REQ_CMP_ERR:
5381         case CAM_REQ_TERMIO:
5382         case CAM_UNREC_HBA_ERROR:
5383         case CAM_DATA_RUN_ERR:
5384                 softc->errors++;
5385                 break;
5386         default:
5387                 break;
5388         }
5389 #endif
5390
5391         /*
5392          * XXX
5393          * Until we have a better way of doing pack validation,
5394          * don't treat UAs as errors.
5395          */
5396         sense_flags |= SF_RETRY_UA;
5397
5398         if (softc->quirks & DA_Q_RETRY_BUSY)
5399                 sense_flags |= SF_RETRY_BUSY;
5400         return(cam_periph_error(ccb, cam_flags, sense_flags,
5401                                 &softc->saved_ccb));
5402 }
5403
5404 static void
5405 damediapoll(void *arg)
5406 {
5407         struct cam_periph *periph = arg;
5408         struct da_softc *softc = periph->softc;
5409
5410         if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
5411             LIST_EMPTY(&softc->pending_ccbs)) {
5412                 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
5413                         cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
5414                         daschedule(periph);
5415                 }
5416         }
5417         /* Queue us up again */
5418         if (da_poll_period != 0)
5419                 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
5420 }
5421
5422 static void
5423 daprevent(struct cam_periph *periph, int action)
5424 {
5425         struct  da_softc *softc;
5426         union   ccb *ccb;               
5427         int     error;
5428                 
5429         softc = (struct da_softc *)periph->softc;
5430
5431         if (((action == PR_ALLOW)
5432           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
5433          || ((action == PR_PREVENT)
5434           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
5435                 return;
5436         }
5437
5438         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5439
5440         scsi_prevent(&ccb->csio,
5441                      /*retries*/1,
5442                      /*cbcfp*/dadone,
5443                      MSG_SIMPLE_Q_TAG,
5444                      action,
5445                      SSD_FULL_SIZE,
5446                      5000);
5447
5448         error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
5449             SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
5450
5451         if (error == 0) {
5452                 if (action == PR_ALLOW)
5453                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
5454                 else
5455                         softc->flags |= DA_FLAG_PACK_LOCKED;
5456         }
5457
5458         xpt_release_ccb(ccb);
5459 }
5460
5461 static void
5462 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
5463           struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
5464 {
5465         struct ccb_calc_geometry ccg;
5466         struct da_softc *softc;
5467         struct disk_params *dp;
5468         u_int lbppbe, lalba;
5469         int error;
5470
5471         softc = (struct da_softc *)periph->softc;
5472
5473         dp = &softc->params;
5474         dp->secsize = block_len;
5475         dp->sectors = maxsector + 1;
5476         if (rcaplong != NULL) {
5477                 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
5478                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
5479                 lalba &= SRC16_LALBA_A;
5480         } else {
5481                 lbppbe = 0;
5482                 lalba = 0;
5483         }
5484
5485         if (lbppbe > 0) {
5486                 dp->stripesize = block_len << lbppbe;
5487                 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
5488                     dp->stripesize;
5489         } else if (softc->quirks & DA_Q_4K) {
5490                 dp->stripesize = 4096;
5491                 dp->stripeoffset = 0;
5492         } else if (softc->unmap_gran != 0) {
5493                 dp->stripesize = block_len * softc->unmap_gran;
5494                 dp->stripeoffset = (dp->stripesize - block_len *
5495                     softc->unmap_gran_align) % dp->stripesize;
5496         } else {
5497                 dp->stripesize = 0;
5498                 dp->stripeoffset = 0;
5499         }
5500         /*
5501          * Have the controller provide us with a geometry
5502          * for this disk.  The only time the geometry
5503          * matters is when we boot and the controller
5504          * is the only one knowledgeable enough to come
5505          * up with something that will make this a bootable
5506          * device.
5507          */
5508         xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5509         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
5510         ccg.block_size = dp->secsize;
5511         ccg.volume_size = dp->sectors;
5512         ccg.heads = 0;
5513         ccg.secs_per_track = 0;
5514         ccg.cylinders = 0;
5515         xpt_action((union ccb*)&ccg);
5516         if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5517                 /*
5518                  * We don't know what went wrong here- but just pick
5519                  * a geometry so we don't have nasty things like divide
5520                  * by zero.
5521                  */
5522                 dp->heads = 255;
5523                 dp->secs_per_track = 255;
5524                 dp->cylinders = dp->sectors / (255 * 255);
5525                 if (dp->cylinders == 0) {
5526                         dp->cylinders = 1;
5527                 }
5528         } else {
5529                 dp->heads = ccg.heads;
5530                 dp->secs_per_track = ccg.secs_per_track;
5531                 dp->cylinders = ccg.cylinders;
5532         }
5533
5534         /*
5535          * If the user supplied a read capacity buffer, and if it is
5536          * different than the previous buffer, update the data in the EDT.
5537          * If it's the same, we don't bother.  This avoids sending an
5538          * update every time someone opens this device.
5539          */
5540         if ((rcaplong != NULL)
5541          && (bcmp(rcaplong, &softc->rcaplong,
5542                   min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
5543                 struct ccb_dev_advinfo cdai;
5544
5545                 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5546                 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
5547                 cdai.buftype = CDAI_TYPE_RCAPLONG;
5548                 cdai.flags = CDAI_FLAG_STORE;
5549                 cdai.bufsiz = rcap_len;
5550                 cdai.buf = (uint8_t *)rcaplong;
5551                 xpt_action((union ccb *)&cdai);
5552                 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
5553                         cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
5554                 if (cdai.ccb_h.status != CAM_REQ_CMP) {
5555                         xpt_print(periph->path, "%s: failed to set read "
5556                                   "capacity advinfo\n", __func__);
5557                         /* Use cam_error_print() to decode the status */
5558                         cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
5559                                         CAM_EPF_ALL);
5560                 } else {
5561                         bcopy(rcaplong, &softc->rcaplong,
5562                               min(sizeof(softc->rcaplong), rcap_len));
5563                 }
5564         }
5565
5566         softc->disk->d_sectorsize = softc->params.secsize;
5567         softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
5568         softc->disk->d_stripesize = softc->params.stripesize;
5569         softc->disk->d_stripeoffset = softc->params.stripeoffset;
5570         /* XXX: these are not actually "firmware" values, so they may be wrong */
5571         softc->disk->d_fwsectors = softc->params.secs_per_track;
5572         softc->disk->d_fwheads = softc->params.heads;
5573         softc->disk->d_devstat->block_size = softc->params.secsize;
5574         softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
5575
5576         error = disk_resize(softc->disk, M_NOWAIT);
5577         if (error != 0)
5578                 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
5579 }
5580
5581 static void
5582 dasendorderedtag(void *arg)
5583 {
5584         struct da_softc *softc = arg;
5585
5586         if (da_send_ordered) {
5587                 if (!LIST_EMPTY(&softc->pending_ccbs)) {
5588                         if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
5589                                 softc->flags |= DA_FLAG_NEED_OTAG;
5590                         softc->flags &= ~DA_FLAG_WAS_OTAG;
5591                 }
5592         }
5593         /* Queue us up again */
5594         callout_reset(&softc->sendordered_c,
5595             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
5596             dasendorderedtag, softc);
5597 }
5598
5599 /*
5600  * Step through all DA peripheral drivers, and if the device is still open,
5601  * sync the disk cache to physical media.
5602  */
5603 static void
5604 dashutdown(void * arg, int howto)
5605 {
5606         struct cam_periph *periph;
5607         struct da_softc *softc;
5608         union ccb *ccb;
5609         int error;
5610
5611         CAM_PERIPH_FOREACH(periph, &dadriver) {
5612                 softc = (struct da_softc *)periph->softc;
5613                 if (SCHEDULER_STOPPED()) {
5614                         /* If we paniced with the lock held, do not recurse. */
5615                         if (!cam_periph_owned(periph) &&
5616                             (softc->flags & DA_FLAG_OPEN)) {
5617                                 dadump(softc->disk, NULL, 0, 0, 0);
5618                         }
5619                         continue;
5620                 }
5621                 cam_periph_lock(periph);
5622
5623                 /*
5624                  * We only sync the cache if the drive is still open, and
5625                  * if the drive is capable of it..
5626                  */
5627                 if (((softc->flags & DA_FLAG_OPEN) == 0)
5628                  || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
5629                         cam_periph_unlock(periph);
5630                         continue;
5631                 }
5632
5633                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5634                 scsi_synchronize_cache(&ccb->csio,
5635                                        /*retries*/0,
5636                                        /*cbfcnp*/dadone,
5637                                        MSG_SIMPLE_Q_TAG,
5638                                        /*begin_lba*/0, /* whole disk */
5639                                        /*lb_count*/0,
5640                                        SSD_FULL_SIZE,
5641                                        60 * 60 * 1000);
5642
5643                 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
5644                     /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
5645                     softc->disk->d_devstat);
5646                 if (error != 0)
5647                         xpt_print(periph->path, "Synchronize cache failed\n");
5648                 xpt_release_ccb(ccb);
5649                 cam_periph_unlock(periph);
5650         }
5651 }
5652
5653 #else /* !_KERNEL */
5654
5655 /*
5656  * XXX These are only left out of the kernel build to silence warnings.  If,
5657  * for some reason these functions are used in the kernel, the ifdefs should
5658  * be moved so they are included both in the kernel and userland.
5659  */
5660 void
5661 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
5662                  void (*cbfcnp)(struct cam_periph *, union ccb *),
5663                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
5664                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
5665                  u_int32_t timeout)
5666 {
5667         struct scsi_format_unit *scsi_cmd;
5668
5669         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
5670         scsi_cmd->opcode = FORMAT_UNIT;
5671         scsi_cmd->byte2 = byte2;
5672         scsi_ulto2b(ileave, scsi_cmd->interleave);
5673
5674         cam_fill_csio(csio,
5675                       retries,
5676                       cbfcnp,
5677                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5678                       tag_action,
5679                       data_ptr,
5680                       dxfer_len,
5681                       sense_len,
5682                       sizeof(*scsi_cmd),
5683                       timeout);
5684 }
5685
5686 void
5687 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
5688                   void (*cbfcnp)(struct cam_periph *, union ccb *),
5689                   uint8_t tag_action, uint8_t list_format,
5690                   uint32_t addr_desc_index, uint8_t *data_ptr,
5691                   uint32_t dxfer_len, int minimum_cmd_size, 
5692                   uint8_t sense_len, uint32_t timeout)
5693 {
5694         uint8_t cdb_len;
5695
5696         /*
5697          * These conditions allow using the 10 byte command.  Otherwise we
5698          * need to use the 12 byte command.
5699          */
5700         if ((minimum_cmd_size <= 10)
5701          && (addr_desc_index == 0) 
5702          && (dxfer_len <= SRDD10_MAX_LENGTH)) {
5703                 struct scsi_read_defect_data_10 *cdb10;
5704
5705                 cdb10 = (struct scsi_read_defect_data_10 *)
5706                         &csio->cdb_io.cdb_bytes;
5707
5708                 cdb_len = sizeof(*cdb10);
5709                 bzero(cdb10, cdb_len);
5710                 cdb10->opcode = READ_DEFECT_DATA_10;
5711                 cdb10->format = list_format;
5712                 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
5713         } else {
5714                 struct scsi_read_defect_data_12 *cdb12;
5715
5716                 cdb12 = (struct scsi_read_defect_data_12 *)
5717                         &csio->cdb_io.cdb_bytes;
5718
5719                 cdb_len = sizeof(*cdb12);
5720                 bzero(cdb12, cdb_len);
5721                 cdb12->opcode = READ_DEFECT_DATA_12;
5722                 cdb12->format = list_format;
5723                 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
5724                 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
5725         }
5726
5727         cam_fill_csio(csio,
5728                       retries,
5729                       cbfcnp,
5730                       /*flags*/ CAM_DIR_IN,
5731                       tag_action,
5732                       data_ptr,
5733                       dxfer_len,
5734                       sense_len,
5735                       cdb_len,
5736                       timeout);
5737 }
5738
5739 void
5740 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
5741               void (*cbfcnp)(struct cam_periph *, union ccb *),
5742               u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
5743               u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
5744               u_int32_t timeout)
5745 {
5746         struct scsi_sanitize *scsi_cmd;
5747
5748         scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
5749         scsi_cmd->opcode = SANITIZE;
5750         scsi_cmd->byte2 = byte2;
5751         scsi_cmd->control = control;
5752         scsi_ulto2b(dxfer_len, scsi_cmd->length);
5753
5754         cam_fill_csio(csio,
5755                       retries,
5756                       cbfcnp,
5757                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5758                       tag_action,
5759                       data_ptr,
5760                       dxfer_len,
5761                       sense_len,
5762                       sizeof(*scsi_cmd),
5763                       timeout);
5764 }
5765
5766 #endif /* _KERNEL */
5767
5768 void
5769 scsi_zbc_out(struct ccb_scsiio *csio, uint32_t retries, 
5770              void (*cbfcnp)(struct cam_periph *, union ccb *),
5771              uint8_t tag_action, uint8_t service_action, uint64_t zone_id,
5772              uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
5773              uint8_t sense_len, uint32_t timeout)
5774 {
5775         struct scsi_zbc_out *scsi_cmd;
5776
5777         scsi_cmd = (struct scsi_zbc_out *)&csio->cdb_io.cdb_bytes;
5778         scsi_cmd->opcode = ZBC_OUT;
5779         scsi_cmd->service_action = service_action;
5780         scsi_u64to8b(zone_id, scsi_cmd->zone_id);
5781         scsi_cmd->zone_flags = zone_flags;
5782
5783         cam_fill_csio(csio,
5784                       retries,
5785                       cbfcnp,
5786                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5787                       tag_action,
5788                       data_ptr,
5789                       dxfer_len,
5790                       sense_len,
5791                       sizeof(*scsi_cmd),
5792                       timeout);
5793 }
5794
5795 void
5796 scsi_zbc_in(struct ccb_scsiio *csio, uint32_t retries, 
5797             void (*cbfcnp)(struct cam_periph *, union ccb *),
5798             uint8_t tag_action, uint8_t service_action, uint64_t zone_start_lba,
5799             uint8_t zone_options, uint8_t *data_ptr, uint32_t dxfer_len,
5800             uint8_t sense_len, uint32_t timeout)
5801 {
5802         struct scsi_zbc_in *scsi_cmd;
5803
5804         scsi_cmd = (struct scsi_zbc_in *)&csio->cdb_io.cdb_bytes;
5805         scsi_cmd->opcode = ZBC_IN;
5806         scsi_cmd->service_action = service_action;
5807         scsi_ulto4b(dxfer_len, scsi_cmd->length);
5808         scsi_u64to8b(zone_start_lba, scsi_cmd->zone_start_lba);
5809         scsi_cmd->zone_options = zone_options;
5810
5811         cam_fill_csio(csio,
5812                       retries,
5813                       cbfcnp,
5814                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_IN : CAM_DIR_NONE,
5815                       tag_action,
5816                       data_ptr,
5817                       dxfer_len,
5818                       sense_len,
5819                       sizeof(*scsi_cmd),
5820                       timeout);
5821
5822 }
5823
5824 int
5825 scsi_ata_zac_mgmt_out(struct ccb_scsiio *csio, uint32_t retries, 
5826                       void (*cbfcnp)(struct cam_periph *, union ccb *),
5827                       uint8_t tag_action, int use_ncq,
5828                       uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
5829                       uint8_t *data_ptr, uint32_t dxfer_len,
5830                       uint8_t *cdb_storage, size_t cdb_storage_len,
5831                       uint8_t sense_len, uint32_t timeout)
5832 {
5833         uint8_t command_out, protocol, ata_flags;
5834         uint16_t features_out;
5835         uint32_t sectors_out, auxiliary;
5836         int retval;
5837
5838         retval = 0;
5839
5840         if (use_ncq == 0) {
5841                 command_out = ATA_ZAC_MANAGEMENT_OUT;
5842                 features_out = (zm_action & 0xf) | (zone_flags << 8);
5843                 ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
5844                 if (dxfer_len == 0) {
5845                         protocol = AP_PROTO_NON_DATA;
5846                         ata_flags |= AP_FLAG_TLEN_NO_DATA;
5847                         sectors_out = 0;
5848                 } else {
5849                         protocol = AP_PROTO_DMA;
5850                         ata_flags |= AP_FLAG_TLEN_SECT_CNT |
5851                                      AP_FLAG_TDIR_TO_DEV;
5852                         sectors_out = ((dxfer_len >> 9) & 0xffff);
5853                 }
5854                 auxiliary = 0;
5855         } else {
5856                 ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
5857                 if (dxfer_len == 0) {
5858                         command_out = ATA_NCQ_NON_DATA;
5859                         features_out = ATA_NCQ_ZAC_MGMT_OUT;
5860                         /*
5861                          * We're assuming the SCSI to ATA translation layer
5862                          * will set the NCQ tag number in the tag field.
5863                          * That isn't clear from the SAT-4 spec (as of rev 05).
5864                          */
5865                         sectors_out = 0;
5866                         ata_flags |= AP_FLAG_TLEN_NO_DATA;
5867                 } else {
5868                         command_out = ATA_SEND_FPDMA_QUEUED;
5869                         /*
5870                          * Note that we're defaulting to normal priority,
5871                          * and assuming that the SCSI to ATA translation
5872                          * layer will insert the NCQ tag number in the tag
5873                          * field.  That isn't clear in the SAT-4 spec (as
5874                          * of rev 05).
5875                          */
5876                         sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
5877
5878                         ata_flags |= AP_FLAG_TLEN_FEAT |
5879                                      AP_FLAG_TDIR_TO_DEV;
5880
5881                         /*
5882                          * For SEND FPDMA QUEUED, the transfer length is
5883                          * encoded in the FEATURE register, and 0 means
5884                          * that 65536 512 byte blocks are to be tranferred.
5885                          * In practice, it seems unlikely that we'll see
5886                          * a transfer that large, and it may confuse the
5887                          * the SAT layer, because generally that means that
5888                          * 0 bytes should be transferred.
5889                          */
5890                         if (dxfer_len == (65536 * 512)) {
5891                                 features_out = 0;
5892                         } else if (dxfer_len <= (65535 * 512)) {
5893                                 features_out = ((dxfer_len >> 9) & 0xffff);
5894                         } else {
5895                                 /* The transfer is too big. */
5896                                 retval = 1;
5897                                 goto bailout;
5898                         }
5899
5900                 }
5901
5902                 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
5903                 protocol = AP_PROTO_FPDMA;
5904         }
5905
5906         protocol |= AP_EXTEND;
5907
5908         retval = scsi_ata_pass(csio,
5909             retries,
5910             cbfcnp,
5911             /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5912             tag_action,
5913             /*protocol*/ protocol,
5914             /*ata_flags*/ ata_flags,
5915             /*features*/ features_out,
5916             /*sector_count*/ sectors_out,
5917             /*lba*/ zone_id,
5918             /*command*/ command_out,
5919             /*device*/ 0,
5920             /*icc*/ 0,
5921             /*auxiliary*/ auxiliary,
5922             /*control*/ 0,
5923             /*data_ptr*/ data_ptr,
5924             /*dxfer_len*/ dxfer_len,
5925             /*cdb_storage*/ cdb_storage,
5926             /*cdb_storage_len*/ cdb_storage_len,
5927             /*minimum_cmd_size*/ 0,
5928             /*sense_len*/ SSD_FULL_SIZE,
5929             /*timeout*/ timeout);
5930
5931 bailout:
5932
5933         return (retval);
5934 }
5935
5936 int
5937 scsi_ata_zac_mgmt_in(struct ccb_scsiio *csio, uint32_t retries, 
5938                      void (*cbfcnp)(struct cam_periph *, union ccb *),
5939                      uint8_t tag_action, int use_ncq,
5940                      uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
5941                      uint8_t *data_ptr, uint32_t dxfer_len,
5942                      uint8_t *cdb_storage, size_t cdb_storage_len,
5943                      uint8_t sense_len, uint32_t timeout)
5944 {
5945         uint8_t command_out, protocol;
5946         uint16_t features_out, sectors_out;
5947         uint32_t auxiliary;
5948         int ata_flags;
5949         int retval;
5950
5951         retval = 0;
5952         ata_flags = AP_FLAG_TDIR_FROM_DEV | AP_FLAG_BYT_BLOK_BLOCKS;
5953
5954         if (use_ncq == 0) {
5955                 command_out = ATA_ZAC_MANAGEMENT_IN;
5956                 /* XXX KDM put a macro here */
5957                 features_out = (zm_action & 0xf) | (zone_flags << 8);
5958                 sectors_out = dxfer_len >> 9; /* XXX KDM macro */
5959                 protocol = AP_PROTO_DMA;
5960                 ata_flags |= AP_FLAG_TLEN_SECT_CNT;
5961                 auxiliary = 0;
5962         } else {
5963                 ata_flags |= AP_FLAG_TLEN_FEAT;
5964
5965                 command_out = ATA_RECV_FPDMA_QUEUED;
5966                 sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
5967
5968                 /*
5969                  * For RECEIVE FPDMA QUEUED, the transfer length is
5970                  * encoded in the FEATURE register, and 0 means
5971                  * that 65536 512 byte blocks are to be tranferred.
5972                  * In practice, it seems unlikely that we'll see
5973                  * a transfer that large, and it may confuse the
5974                  * the SAT layer, because generally that means that
5975                  * 0 bytes should be transferred.
5976                  */
5977                 if (dxfer_len == (65536 * 512)) {
5978                         features_out = 0;
5979                 } else if (dxfer_len <= (65535 * 512)) {
5980                         features_out = ((dxfer_len >> 9) & 0xffff);
5981                 } else {
5982                         /* The transfer is too big. */
5983                         retval = 1;
5984                         goto bailout;
5985                 }
5986                 auxiliary = (zm_action & 0xf) | (zone_flags << 8),
5987                 protocol = AP_PROTO_FPDMA;
5988         }
5989
5990         protocol |= AP_EXTEND;
5991
5992         retval = scsi_ata_pass(csio,
5993             retries,
5994             cbfcnp,
5995             /*flags*/ CAM_DIR_IN,
5996             tag_action,
5997             /*protocol*/ protocol,
5998             /*ata_flags*/ ata_flags,
5999             /*features*/ features_out,
6000             /*sector_count*/ sectors_out,
6001             /*lba*/ zone_id,
6002             /*command*/ command_out,
6003             /*device*/ 0,
6004             /*icc*/ 0,
6005             /*auxiliary*/ auxiliary,
6006             /*control*/ 0,
6007             /*data_ptr*/ data_ptr,
6008             /*dxfer_len*/ (dxfer_len >> 9) * 512, /* XXX KDM */
6009             /*cdb_storage*/ cdb_storage,
6010             /*cdb_storage_len*/ cdb_storage_len,
6011             /*minimum_cmd_size*/ 0,
6012             /*sense_len*/ SSD_FULL_SIZE,
6013             /*timeout*/ timeout);
6014
6015 bailout:
6016         return (retval);
6017 }