]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/scsi/scsi_da.c
Import mandoc 1.14.3
[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 750 Series SSDs
1264                  * 4k optimised & trim only works in 4k requests + 4k aligned
1265                  */
1266                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 750*", "*" },
1267                 /*quirks*/DA_Q_4K
1268         },
1269         {
1270                 /*
1271                  * Samsung 830 Series SSDs
1272                  * 4k optimised & trim only works in 4k requests + 4k aligned
1273                  */
1274                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1275                 /*quirks*/DA_Q_4K
1276         },
1277         {
1278                 /*
1279                  * Samsung 840 SSDs
1280                  * 4k optimised & trim only works in 4k requests + 4k aligned
1281                  */
1282                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1283                 /*quirks*/DA_Q_4K
1284         },
1285         {
1286                 /*
1287                  * Samsung 845 SSDs
1288                  * 4k optimised & trim only works in 4k requests + 4k aligned
1289                  */
1290                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 845*", "*" },
1291                 /*quirks*/DA_Q_4K
1292         },
1293         {
1294                 /*
1295                  * Samsung 850 SSDs
1296                  * 4k optimised & trim only works in 4k requests + 4k aligned
1297                  */
1298                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1299                 /*quirks*/DA_Q_4K
1300         },
1301         {
1302                 /*
1303                  * Samsung 843T Series SSDs (MZ7WD*)
1304                  * Samsung PM851 Series SSDs (MZ7TE*)
1305                  * Samsung PM853T Series SSDs (MZ7GE*)
1306                  * Samsung SM863 Series SSDs (MZ7KM*)
1307                  * 4k optimised
1308                  */
1309                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
1310                 /*quirks*/DA_Q_4K
1311         },
1312         {
1313                 /*
1314                  * SuperTalent TeraDrive CT SSDs
1315                  * 4k optimised & trim only works in 4k requests + 4k aligned
1316                  */
1317                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1318                 /*quirks*/DA_Q_4K
1319         },
1320         {
1321                 /*
1322                  * XceedIOPS SATA SSDs
1323                  * 4k optimised
1324                  */
1325                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1326                 /*quirks*/DA_Q_4K
1327         },
1328         {
1329                 /*
1330                  * Hama Innostor USB-Stick 
1331                  */
1332                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" }, 
1333                 /*quirks*/DA_Q_NO_RC16
1334         },
1335         {
1336                 /*
1337                  * Seagate Lamarr 8TB Shingled Magnetic Recording (SMR)
1338                  * Drive Managed SATA hard drive.  This drive doesn't report
1339                  * in firmware that it is a drive managed SMR drive.
1340                  */
1341                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST8000AS0002*", "*" },
1342                 /*quirks*/DA_Q_SMR_DM
1343         },
1344         {
1345                 /*
1346                  * MX-ES USB Drive by Mach Xtreme
1347                  */
1348                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1349                 /*quirks*/DA_Q_NO_RC16
1350         },
1351 };
1352
1353 static  disk_strategy_t dastrategy;
1354 static  dumper_t        dadump;
1355 static  periph_init_t   dainit;
1356 static  void            daasync(void *callback_arg, u_int32_t code,
1357                                 struct cam_path *path, void *arg);
1358 static  void            dasysctlinit(void *context, int pending);
1359 static  int             dasysctlsofttimeout(SYSCTL_HANDLER_ARGS);
1360 static  int             dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1361 static  int             dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1362 static  int             dazonemodesysctl(SYSCTL_HANDLER_ARGS);
1363 static  int             dazonesupsysctl(SYSCTL_HANDLER_ARGS);
1364 static  int             dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1365 static  void            dadeletemethodset(struct da_softc *softc,
1366                                           da_delete_methods delete_method);
1367 static  off_t           dadeletemaxsize(struct da_softc *softc,
1368                                         da_delete_methods delete_method);
1369 static  void            dadeletemethodchoose(struct da_softc *softc,
1370                                              da_delete_methods default_method);
1371 static  void            daprobedone(struct cam_periph *periph, union ccb *ccb);
1372
1373 static  periph_ctor_t   daregister;
1374 static  periph_dtor_t   dacleanup;
1375 static  periph_start_t  dastart;
1376 static  periph_oninv_t  daoninvalidate;
1377 static  void            dazonedone(struct cam_periph *periph, union ccb *ccb);
1378 static  void            dadone(struct cam_periph *periph,
1379                                union ccb *done_ccb);
1380 static  int             daerror(union ccb *ccb, u_int32_t cam_flags,
1381                                 u_int32_t sense_flags);
1382 static void             daprevent(struct cam_periph *periph, int action);
1383 static void             dareprobe(struct cam_periph *periph);
1384 static void             dasetgeom(struct cam_periph *periph, uint32_t block_len,
1385                                   uint64_t maxsector,
1386                                   struct scsi_read_capacity_data_long *rcaplong,
1387                                   size_t rcap_size);
1388 static timeout_t        dasendorderedtag;
1389 static void             dashutdown(void *arg, int howto);
1390 static timeout_t        damediapoll;
1391
1392 #ifndef DA_DEFAULT_POLL_PERIOD
1393 #define DA_DEFAULT_POLL_PERIOD  3
1394 #endif
1395
1396 #ifndef DA_DEFAULT_TIMEOUT
1397 #define DA_DEFAULT_TIMEOUT 60   /* Timeout in seconds */
1398 #endif
1399
1400 #ifndef DA_DEFAULT_SOFTTIMEOUT
1401 #define DA_DEFAULT_SOFTTIMEOUT  0
1402 #endif
1403
1404 #ifndef DA_DEFAULT_RETRY
1405 #define DA_DEFAULT_RETRY        4
1406 #endif
1407
1408 #ifndef DA_DEFAULT_SEND_ORDERED
1409 #define DA_DEFAULT_SEND_ORDERED 1
1410 #endif
1411
1412 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1413 static int da_retry_count = DA_DEFAULT_RETRY;
1414 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1415 static sbintime_t da_default_softtimeout = DA_DEFAULT_SOFTTIMEOUT;
1416 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1417
1418 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1419             "CAM Direct Access Disk driver");
1420 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RWTUN,
1421            &da_poll_period, 0, "Media polling period in seconds");
1422 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RWTUN,
1423            &da_retry_count, 0, "Normal I/O retry count");
1424 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
1425            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1426 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
1427            &da_send_ordered, 0, "Send Ordered Tags");
1428
1429 SYSCTL_PROC(_kern_cam_da, OID_AUTO, default_softtimeout,
1430     CTLTYPE_UINT | CTLFLAG_RW, NULL, 0, dasysctlsofttimeout, "I",
1431     "Soft I/O timeout (ms)");
1432 TUNABLE_INT64("kern.cam.da.default_softtimeout", &da_default_softtimeout);
1433
1434 /*
1435  * DA_ORDEREDTAG_INTERVAL determines how often, relative
1436  * to the default timeout, we check to see whether an ordered
1437  * tagged transaction is appropriate to prevent simple tag
1438  * starvation.  Since we'd like to ensure that there is at least
1439  * 1/2 of the timeout length left for a starved transaction to
1440  * complete after we've sent an ordered tag, we must poll at least
1441  * four times in every timeout period.  This takes care of the worst
1442  * case where a starved transaction starts during an interval that
1443  * meets the requirement "don't send an ordered tag" test so it takes
1444  * us two intervals to determine that a tag must be sent.
1445  */
1446 #ifndef DA_ORDEREDTAG_INTERVAL
1447 #define DA_ORDEREDTAG_INTERVAL 4
1448 #endif
1449
1450 static struct periph_driver dadriver =
1451 {
1452         dainit, "da",
1453         TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1454 };
1455
1456 PERIPHDRIVER_DECLARE(da, dadriver);
1457
1458 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1459
1460 static int
1461 daopen(struct disk *dp)
1462 {
1463         struct cam_periph *periph;
1464         struct da_softc *softc;
1465         int error;
1466
1467         periph = (struct cam_periph *)dp->d_drv1;
1468         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1469                 return (ENXIO);
1470         }
1471
1472         cam_periph_lock(periph);
1473         if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1474                 cam_periph_unlock(periph);
1475                 cam_periph_release(periph);
1476                 return (error);
1477         }
1478
1479         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1480             ("daopen\n"));
1481
1482         softc = (struct da_softc *)periph->softc;
1483         dareprobe(periph);
1484
1485         /* Wait for the disk size update.  */
1486         error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1487             "dareprobe", 0);
1488         if (error != 0)
1489                 xpt_print(periph->path, "unable to retrieve capacity data\n");
1490
1491         if (periph->flags & CAM_PERIPH_INVALID)
1492                 error = ENXIO;
1493
1494         if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1495             (softc->quirks & DA_Q_NO_PREVENT) == 0)
1496                 daprevent(periph, PR_PREVENT);
1497
1498         if (error == 0) {
1499                 softc->flags &= ~DA_FLAG_PACK_INVALID;
1500                 softc->flags |= DA_FLAG_OPEN;
1501         }
1502
1503         cam_periph_unhold(periph);
1504         cam_periph_unlock(periph);
1505
1506         if (error != 0)
1507                 cam_periph_release(periph);
1508
1509         return (error);
1510 }
1511
1512 static int
1513 daclose(struct disk *dp)
1514 {
1515         struct  cam_periph *periph;
1516         struct  da_softc *softc;
1517         union   ccb *ccb;
1518         int error;
1519
1520         periph = (struct cam_periph *)dp->d_drv1;
1521         softc = (struct da_softc *)periph->softc;
1522         cam_periph_lock(periph);
1523         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1524             ("daclose\n"));
1525
1526         if (cam_periph_hold(periph, PRIBIO) == 0) {
1527
1528                 /* Flush disk cache. */
1529                 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1530                     (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1531                     (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1532                         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1533                         scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1534                             /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1535                             /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1536                             5 * 60 * 1000);
1537                         error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1538                             /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1539                             softc->disk->d_devstat);
1540                         softc->flags &= ~DA_FLAG_DIRTY;
1541                         xpt_release_ccb(ccb);
1542                 }
1543
1544                 /* Allow medium removal. */
1545                 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1546                     (softc->quirks & DA_Q_NO_PREVENT) == 0)
1547                         daprevent(periph, PR_ALLOW);
1548
1549                 cam_periph_unhold(periph);
1550         }
1551
1552         /*
1553          * If we've got removeable media, mark the blocksize as
1554          * unavailable, since it could change when new media is
1555          * inserted.
1556          */
1557         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1558                 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1559
1560         softc->flags &= ~DA_FLAG_OPEN;
1561         while (softc->refcount != 0)
1562                 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1563         cam_periph_unlock(periph);
1564         cam_periph_release(periph);
1565         return (0);
1566 }
1567
1568 static void
1569 daschedule(struct cam_periph *periph)
1570 {
1571         struct da_softc *softc = (struct da_softc *)periph->softc;
1572
1573         if (softc->state != DA_STATE_NORMAL)
1574                 return;
1575
1576         cam_iosched_schedule(softc->cam_iosched, periph);
1577 }
1578
1579 /*
1580  * Actually translate the requested transfer into one the physical driver
1581  * can understand.  The transfer is described by a buf and will include
1582  * only one physical transfer.
1583  */
1584 static void
1585 dastrategy(struct bio *bp)
1586 {
1587         struct cam_periph *periph;
1588         struct da_softc *softc;
1589         
1590         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1591         softc = (struct da_softc *)periph->softc;
1592
1593         cam_periph_lock(periph);
1594
1595         /*
1596          * If the device has been made invalid, error out
1597          */
1598         if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1599                 cam_periph_unlock(periph);
1600                 biofinish(bp, NULL, ENXIO);
1601                 return;
1602         }
1603
1604         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1605
1606         /*
1607          * Zone commands must be ordered, because they can depend on the
1608          * effects of previously issued commands, and they may affect
1609          * commands after them.
1610          */
1611         if (bp->bio_cmd == BIO_ZONE)
1612                 bp->bio_flags |= BIO_ORDERED;
1613
1614         /*
1615          * Place it in the queue of disk activities for this disk
1616          */
1617         cam_iosched_queue_work(softc->cam_iosched, bp);
1618
1619         /*
1620          * Schedule ourselves for performing the work.
1621          */
1622         daschedule(periph);
1623         cam_periph_unlock(periph);
1624
1625         return;
1626 }
1627
1628 static int
1629 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1630 {
1631         struct      cam_periph *periph;
1632         struct      da_softc *softc;
1633         u_int       secsize;
1634         struct      ccb_scsiio csio;
1635         struct      disk *dp;
1636         int         error = 0;
1637
1638         dp = arg;
1639         periph = dp->d_drv1;
1640         softc = (struct da_softc *)periph->softc;
1641         cam_periph_lock(periph);
1642         secsize = softc->params.secsize;
1643         
1644         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1645                 cam_periph_unlock(periph);
1646                 return (ENXIO);
1647         }
1648
1649         if (length > 0) {
1650                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1651                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1652                 scsi_read_write(&csio,
1653                                 /*retries*/0,
1654                                 dadone,
1655                                 MSG_ORDERED_Q_TAG,
1656                                 /*read*/SCSI_RW_WRITE,
1657                                 /*byte2*/0,
1658                                 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1659                                 offset / secsize,
1660                                 length / secsize,
1661                                 /*data_ptr*/(u_int8_t *) virtual,
1662                                 /*dxfer_len*/length,
1663                                 /*sense_len*/SSD_FULL_SIZE,
1664                                 da_default_timeout * 1000);
1665                 xpt_polled_action((union ccb *)&csio);
1666
1667                 error = cam_periph_error((union ccb *)&csio,
1668                     0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1669                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1670                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1671                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1672                 if (error != 0)
1673                         printf("Aborting dump due to I/O error.\n");
1674                 cam_periph_unlock(periph);
1675                 return (error);
1676         }
1677                 
1678         /*
1679          * Sync the disk cache contents to the physical media.
1680          */
1681         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1682
1683                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1684                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1685                 scsi_synchronize_cache(&csio,
1686                                        /*retries*/0,
1687                                        /*cbfcnp*/dadone,
1688                                        MSG_SIMPLE_Q_TAG,
1689                                        /*begin_lba*/0,/* Cover the whole disk */
1690                                        /*lb_count*/0,
1691                                        SSD_FULL_SIZE,
1692                                        5 * 1000);
1693                 xpt_polled_action((union ccb *)&csio);
1694
1695                 error = cam_periph_error((union ccb *)&csio,
1696                     0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1697                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1698                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1699                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1700                 if (error != 0)
1701                         xpt_print(periph->path, "Synchronize cache failed\n");
1702         }
1703         cam_periph_unlock(periph);
1704         return (error);
1705 }
1706
1707 static int
1708 dagetattr(struct bio *bp)
1709 {
1710         int ret;
1711         struct cam_periph *periph;
1712
1713         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1714         cam_periph_lock(periph);
1715         ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1716             periph->path);
1717         cam_periph_unlock(periph);
1718         if (ret == 0)
1719                 bp->bio_completed = bp->bio_length;
1720         return ret;
1721 }
1722
1723 static void
1724 dainit(void)
1725 {
1726         cam_status status;
1727
1728         /*
1729          * Install a global async callback.  This callback will
1730          * receive async callbacks like "new device found".
1731          */
1732         status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1733
1734         if (status != CAM_REQ_CMP) {
1735                 printf("da: Failed to attach master async callback "
1736                        "due to status 0x%x!\n", status);
1737         } else if (da_send_ordered) {
1738
1739                 /* Register our shutdown event handler */
1740                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 
1741                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1742                     printf("dainit: shutdown event registration failed!\n");
1743         }
1744 }
1745
1746 /*
1747  * Callback from GEOM, called when it has finished cleaning up its
1748  * resources.
1749  */
1750 static void
1751 dadiskgonecb(struct disk *dp)
1752 {
1753         struct cam_periph *periph;
1754
1755         periph = (struct cam_periph *)dp->d_drv1;
1756         cam_periph_release(periph);
1757 }
1758
1759 static void
1760 daoninvalidate(struct cam_periph *periph)
1761 {
1762         struct da_softc *softc;
1763
1764         softc = (struct da_softc *)periph->softc;
1765
1766         /*
1767          * De-register any async callbacks.
1768          */
1769         xpt_register_async(0, daasync, periph, periph->path);
1770
1771         softc->flags |= DA_FLAG_PACK_INVALID;
1772 #ifdef CAM_IO_STATS
1773         softc->invalidations++;
1774 #endif
1775
1776         /*
1777          * Return all queued I/O with ENXIO.
1778          * XXX Handle any transactions queued to the card
1779          *     with XPT_ABORT_CCB.
1780          */
1781         cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
1782
1783         /*
1784          * Tell GEOM that we've gone away, we'll get a callback when it is
1785          * done cleaning up its resources.
1786          */
1787         disk_gone(softc->disk);
1788 }
1789
1790 static void
1791 dacleanup(struct cam_periph *periph)
1792 {
1793         struct da_softc *softc;
1794
1795         softc = (struct da_softc *)periph->softc;
1796
1797         cam_periph_unlock(periph);
1798
1799         cam_iosched_fini(softc->cam_iosched);
1800
1801         /*
1802          * If we can't free the sysctl tree, oh well...
1803          */
1804         if ((softc->flags & DA_FLAG_SCTX_INIT) != 0) {
1805 #ifdef CAM_IO_STATS
1806                 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
1807                         xpt_print(periph->path,
1808                             "can't remove sysctl stats context\n");
1809 #endif
1810                 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
1811                         xpt_print(periph->path,
1812                             "can't remove sysctl context\n");
1813         }
1814
1815         callout_drain(&softc->mediapoll_c);
1816         disk_destroy(softc->disk);
1817         callout_drain(&softc->sendordered_c);
1818         free(softc, M_DEVBUF);
1819         cam_periph_lock(periph);
1820 }
1821
1822 static void
1823 daasync(void *callback_arg, u_int32_t code,
1824         struct cam_path *path, void *arg)
1825 {
1826         struct cam_periph *periph;
1827         struct da_softc *softc;
1828
1829         periph = (struct cam_periph *)callback_arg;
1830         switch (code) {
1831         case AC_FOUND_DEVICE:
1832         {
1833                 struct ccb_getdev *cgd;
1834                 cam_status status;
1835  
1836                 cgd = (struct ccb_getdev *)arg;
1837                 if (cgd == NULL)
1838                         break;
1839
1840                 if (cgd->protocol != PROTO_SCSI)
1841                         break;
1842                 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
1843                         break;
1844                 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1845                     && SID_TYPE(&cgd->inq_data) != T_RBC
1846                     && SID_TYPE(&cgd->inq_data) != T_OPTICAL
1847                     && SID_TYPE(&cgd->inq_data) != T_ZBC_HM)
1848                         break;
1849
1850                 /*
1851                  * Allocate a peripheral instance for
1852                  * this device and start the probe
1853                  * process.
1854                  */
1855                 status = cam_periph_alloc(daregister, daoninvalidate,
1856                                           dacleanup, dastart,
1857                                           "da", CAM_PERIPH_BIO,
1858                                           path, daasync,
1859                                           AC_FOUND_DEVICE, cgd);
1860
1861                 if (status != CAM_REQ_CMP
1862                  && status != CAM_REQ_INPROG)
1863                         printf("daasync: Unable to attach to new device "
1864                                 "due to status 0x%x\n", status);
1865                 return;
1866         }
1867         case AC_ADVINFO_CHANGED:
1868         {
1869                 uintptr_t buftype;
1870
1871                 buftype = (uintptr_t)arg;
1872                 if (buftype == CDAI_TYPE_PHYS_PATH) {
1873                         struct da_softc *softc;
1874
1875                         softc = periph->softc;
1876                         disk_attr_changed(softc->disk, "GEOM::physpath",
1877                                           M_NOWAIT);
1878                 }
1879                 break;
1880         }
1881         case AC_UNIT_ATTENTION:
1882         {
1883                 union ccb *ccb;
1884                 int error_code, sense_key, asc, ascq;
1885
1886                 softc = (struct da_softc *)periph->softc;
1887                 ccb = (union ccb *)arg;
1888
1889                 /*
1890                  * Handle all UNIT ATTENTIONs except our own,
1891                  * as they will be handled by daerror().
1892                  */
1893                 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1894                     scsi_extract_sense_ccb(ccb,
1895                      &error_code, &sense_key, &asc, &ascq)) {
1896                         if (asc == 0x2A && ascq == 0x09) {
1897                                 xpt_print(ccb->ccb_h.path,
1898                                     "Capacity data has changed\n");
1899                                 softc->flags &= ~DA_FLAG_PROBED;
1900                                 dareprobe(periph);
1901                         } else if (asc == 0x28 && ascq == 0x00) {
1902                                 softc->flags &= ~DA_FLAG_PROBED;
1903                                 disk_media_changed(softc->disk, M_NOWAIT);
1904                         } else if (asc == 0x3F && ascq == 0x03) {
1905                                 xpt_print(ccb->ccb_h.path,
1906                                     "INQUIRY data has changed\n");
1907                                 softc->flags &= ~DA_FLAG_PROBED;
1908                                 dareprobe(periph);
1909                         }
1910                 }
1911                 cam_periph_async(periph, code, path, arg);
1912                 break;
1913         }
1914         case AC_SCSI_AEN:
1915                 softc = (struct da_softc *)periph->softc;
1916                 if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
1917                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1918                                 cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
1919                                 daschedule(periph);
1920                         }
1921                 }
1922                 /* FALLTHROUGH */
1923         case AC_SENT_BDR:
1924         case AC_BUS_RESET:
1925         {
1926                 struct ccb_hdr *ccbh;
1927
1928                 softc = (struct da_softc *)periph->softc;
1929                 /*
1930                  * Don't fail on the expected unit attention
1931                  * that will occur.
1932                  */
1933                 softc->flags |= DA_FLAG_RETRY_UA;
1934                 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1935                         ccbh->ccb_state |= DA_CCB_RETRY_UA;
1936                 break;
1937         }
1938         case AC_INQ_CHANGED:
1939                 softc = (struct da_softc *)periph->softc;
1940                 softc->flags &= ~DA_FLAG_PROBED;
1941                 dareprobe(periph);
1942                 break;
1943         default:
1944                 break;
1945         }
1946         cam_periph_async(periph, code, path, arg);
1947 }
1948
1949 static void
1950 dasysctlinit(void *context, int pending)
1951 {
1952         struct cam_periph *periph;
1953         struct da_softc *softc;
1954         char tmpstr[80], tmpstr2[80];
1955         struct ccb_trans_settings cts;
1956
1957         periph = (struct cam_periph *)context;
1958         /*
1959          * periph was held for us when this task was enqueued
1960          */
1961         if (periph->flags & CAM_PERIPH_INVALID) {
1962                 cam_periph_release(periph);
1963                 return;
1964         }
1965
1966         softc = (struct da_softc *)periph->softc;
1967         snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1968         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1969
1970         sysctl_ctx_init(&softc->sysctl_ctx);
1971         softc->flags |= DA_FLAG_SCTX_INIT;
1972         softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
1973                 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1974                 CTLFLAG_RD, 0, tmpstr, "device_index");
1975         if (softc->sysctl_tree == NULL) {
1976                 printf("dasysctlinit: unable to allocate sysctl tree\n");
1977                 cam_periph_release(periph);
1978                 return;
1979         }
1980
1981         /*
1982          * Now register the sysctl handler, so the user can change the value on
1983          * the fly.
1984          */
1985         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1986                 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RWTUN,
1987                 softc, 0, dadeletemethodsysctl, "A",
1988                 "BIO_DELETE execution method");
1989         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1990                 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1991                 softc, 0, dadeletemaxsysctl, "Q",
1992                 "Maximum BIO_DELETE size");
1993         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1994                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1995                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1996                 "Minimum CDB size");
1997
1998         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1999                 OID_AUTO, "zone_mode", CTLTYPE_STRING | CTLFLAG_RD,
2000                 softc, 0, dazonemodesysctl, "A",
2001                 "Zone Mode");
2002         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2003                 OID_AUTO, "zone_support", CTLTYPE_STRING | CTLFLAG_RD,
2004                 softc, 0, dazonesupsysctl, "A",
2005                 "Zone Support");
2006         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2007                 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2008                 "optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones,
2009                 "Optimal Number of Open Sequential Write Preferred Zones");
2010         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2011                 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2012                 "optimal_nonseq_zones", CTLFLAG_RD,
2013                 &softc->optimal_nonseq_zones,
2014                 "Optimal Number of Non-Sequentially Written Sequential Write "
2015                 "Preferred Zones");
2016         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2017                 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2018                 "max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones,
2019                 "Maximum Number of Open Sequential Write Required Zones");
2020
2021         SYSCTL_ADD_INT(&softc->sysctl_ctx,
2022                        SYSCTL_CHILDREN(softc->sysctl_tree),
2023                        OID_AUTO,
2024                        "error_inject",
2025                        CTLFLAG_RW,
2026                        &softc->error_inject,
2027                        0,
2028                        "error_inject leaf");
2029
2030         SYSCTL_ADD_INT(&softc->sysctl_ctx,
2031                        SYSCTL_CHILDREN(softc->sysctl_tree),
2032                        OID_AUTO,
2033                        "unmapped_io",
2034                        CTLFLAG_RD, 
2035                        &softc->unmappedio,
2036                        0,
2037                        "Unmapped I/O leaf");
2038
2039         SYSCTL_ADD_INT(&softc->sysctl_ctx,
2040                        SYSCTL_CHILDREN(softc->sysctl_tree),
2041                        OID_AUTO,
2042                        "rotating",
2043                        CTLFLAG_RD, 
2044                        &softc->rotating,
2045                        0,
2046                        "Rotating media");
2047
2048         /*
2049          * Add some addressing info.
2050          */
2051         memset(&cts, 0, sizeof (cts));
2052         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
2053         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2054         cts.type = CTS_TYPE_CURRENT_SETTINGS;
2055         cam_periph_lock(periph);
2056         xpt_action((union ccb *)&cts);
2057         cam_periph_unlock(periph);
2058         if (cts.ccb_h.status != CAM_REQ_CMP) {
2059                 cam_periph_release(periph);
2060                 return;
2061         }
2062         if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
2063                 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
2064                 if (fc->valid & CTS_FC_VALID_WWPN) {
2065                         softc->wwpn = fc->wwpn;
2066                         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2067                             SYSCTL_CHILDREN(softc->sysctl_tree),
2068                             OID_AUTO, "wwpn", CTLFLAG_RD,
2069                             &softc->wwpn, "World Wide Port Name");
2070                 }
2071         }
2072
2073 #ifdef CAM_IO_STATS
2074         /*
2075          * Now add some useful stats.
2076          * XXX These should live in cam_periph and be common to all periphs
2077          */
2078         softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
2079             SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
2080             CTLFLAG_RD, 0, "Statistics");
2081         SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2082                        SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2083                        OID_AUTO,
2084                        "errors",
2085                        CTLFLAG_RD,
2086                        &softc->errors,
2087                        0,
2088                        "Transport errors reported by the SIM");
2089         SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2090                        SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2091                        OID_AUTO,
2092                        "timeouts",
2093                        CTLFLAG_RD,
2094                        &softc->timeouts,
2095                        0,
2096                        "Device timeouts reported by the SIM");
2097         SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2098                        SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2099                        OID_AUTO,
2100                        "pack_invalidations",
2101                        CTLFLAG_RD,
2102                        &softc->invalidations,
2103                        0,
2104                        "Device pack invalidations");
2105 #endif
2106
2107         cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
2108             softc->sysctl_tree);
2109
2110         cam_periph_release(periph);
2111 }
2112
2113 static int
2114 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
2115 {
2116         int error;
2117         uint64_t value;
2118         struct da_softc *softc;
2119
2120         softc = (struct da_softc *)arg1;
2121
2122         value = softc->disk->d_delmaxsize;
2123         error = sysctl_handle_64(oidp, &value, 0, req);
2124         if ((error != 0) || (req->newptr == NULL))
2125                 return (error);
2126
2127         /* only accept values smaller than the calculated value */
2128         if (value > dadeletemaxsize(softc, softc->delete_method)) {
2129                 return (EINVAL);
2130         }
2131         softc->disk->d_delmaxsize = value;
2132
2133         return (0);
2134 }
2135
2136 static int
2137 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
2138 {
2139         int error, value;
2140
2141         value = *(int *)arg1;
2142
2143         error = sysctl_handle_int(oidp, &value, 0, req);
2144
2145         if ((error != 0)
2146          || (req->newptr == NULL))
2147                 return (error);
2148
2149         /*
2150          * Acceptable values here are 6, 10, 12 or 16.
2151          */
2152         if (value < 6)
2153                 value = 6;
2154         else if ((value > 6)
2155               && (value <= 10))
2156                 value = 10;
2157         else if ((value > 10)
2158               && (value <= 12))
2159                 value = 12;
2160         else if (value > 12)
2161                 value = 16;
2162
2163         *(int *)arg1 = value;
2164
2165         return (0);
2166 }
2167
2168 static int
2169 dasysctlsofttimeout(SYSCTL_HANDLER_ARGS)
2170 {
2171         sbintime_t value;
2172         int error;
2173
2174         value = da_default_softtimeout / SBT_1MS;
2175
2176         error = sysctl_handle_int(oidp, (int *)&value, 0, req);
2177         if ((error != 0) || (req->newptr == NULL))
2178                 return (error);
2179
2180         /* XXX Should clip this to a reasonable level */
2181         if (value > da_default_timeout * 1000)
2182                 return (EINVAL);
2183
2184         da_default_softtimeout = value * SBT_1MS;
2185         return (0);
2186 }
2187
2188 static void
2189 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
2190 {
2191
2192         softc->delete_method = delete_method;
2193         softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
2194         softc->delete_func = da_delete_functions[delete_method];
2195
2196         if (softc->delete_method > DA_DELETE_DISABLE)
2197                 softc->disk->d_flags |= DISKFLAG_CANDELETE;
2198         else
2199                 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2200 }
2201
2202 static off_t
2203 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
2204 {
2205         off_t sectors;
2206
2207         switch(delete_method) {
2208         case DA_DELETE_UNMAP:
2209                 sectors = (off_t)softc->unmap_max_lba;
2210                 break;
2211         case DA_DELETE_ATA_TRIM:
2212                 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
2213                 break;
2214         case DA_DELETE_WS16:
2215                 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
2216                 break;
2217         case DA_DELETE_ZERO:
2218         case DA_DELETE_WS10:
2219                 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
2220                 break;
2221         default:
2222                 return 0;
2223         }
2224
2225         return (off_t)softc->params.secsize *
2226             omin(sectors, softc->params.sectors);
2227 }
2228
2229 static void
2230 daprobedone(struct cam_periph *periph, union ccb *ccb)
2231 {
2232         struct da_softc *softc;
2233
2234         softc = (struct da_softc *)periph->softc;
2235
2236         dadeletemethodchoose(softc, DA_DELETE_NONE);
2237
2238         if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2239                 char buf[80];
2240                 int i, sep;
2241
2242                 snprintf(buf, sizeof(buf), "Delete methods: <");
2243                 sep = 0;
2244                 for (i = 0; i <= DA_DELETE_MAX; i++) {
2245                         if ((softc->delete_available & (1 << i)) == 0 &&
2246                             i != softc->delete_method)
2247                                 continue;
2248                         if (sep)
2249                                 strlcat(buf, ",", sizeof(buf));
2250                         strlcat(buf, da_delete_method_names[i],
2251                             sizeof(buf));
2252                         if (i == softc->delete_method)
2253                                 strlcat(buf, "(*)", sizeof(buf));
2254                         sep = 1;
2255                 }
2256                 strlcat(buf, ">", sizeof(buf));
2257                 printf("%s%d: %s\n", periph->periph_name,
2258                     periph->unit_number, buf);
2259         }
2260
2261         /*
2262          * Since our peripheral may be invalidated by an error
2263          * above or an external event, we must release our CCB
2264          * before releasing the probe lock on the peripheral.
2265          * The peripheral will only go away once the last lock
2266          * is removed, and we need it around for the CCB release
2267          * operation.
2268          */
2269         xpt_release_ccb(ccb);
2270         softc->state = DA_STATE_NORMAL;
2271         softc->flags |= DA_FLAG_PROBED;
2272         daschedule(periph);
2273         wakeup(&softc->disk->d_mediasize);
2274         if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2275                 softc->flags |= DA_FLAG_ANNOUNCED;
2276                 cam_periph_unhold(periph);
2277         } else
2278                 cam_periph_release_locked(periph);
2279 }
2280
2281 static void
2282 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2283 {
2284         int i, methods;
2285
2286         /* If available, prefer the method requested by user. */
2287         i = softc->delete_method_pref;
2288         methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2289         if (methods & (1 << i)) {
2290                 dadeletemethodset(softc, i);
2291                 return;
2292         }
2293
2294         /* Use the pre-defined order to choose the best performing delete. */
2295         for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2296                 if (i == DA_DELETE_ZERO)
2297                         continue;
2298                 if (softc->delete_available & (1 << i)) {
2299                         dadeletemethodset(softc, i);
2300                         return;
2301                 }
2302         }
2303
2304         /* Fallback to default. */
2305         dadeletemethodset(softc, default_method);
2306 }
2307
2308 static int
2309 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2310 {
2311         char buf[16];
2312         const char *p;
2313         struct da_softc *softc;
2314         int i, error, methods, value;
2315
2316         softc = (struct da_softc *)arg1;
2317
2318         value = softc->delete_method;
2319         if (value < 0 || value > DA_DELETE_MAX)
2320                 p = "UNKNOWN";
2321         else
2322                 p = da_delete_method_names[value];
2323         strncpy(buf, p, sizeof(buf));
2324         error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2325         if (error != 0 || req->newptr == NULL)
2326                 return (error);
2327         methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2328         for (i = 0; i <= DA_DELETE_MAX; i++) {
2329                 if (strcmp(buf, da_delete_method_names[i]) == 0)
2330                         break;
2331         }
2332         if (i > DA_DELETE_MAX)
2333                 return (EINVAL);
2334         softc->delete_method_pref = i;
2335         dadeletemethodchoose(softc, DA_DELETE_NONE);
2336         return (0);
2337 }
2338
2339 static int
2340 dazonemodesysctl(SYSCTL_HANDLER_ARGS)
2341 {
2342         char tmpbuf[40];
2343         struct da_softc *softc;
2344         int error;
2345
2346         softc = (struct da_softc *)arg1;
2347
2348         switch (softc->zone_mode) {
2349         case DA_ZONE_DRIVE_MANAGED:
2350                 snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed");
2351                 break;
2352         case DA_ZONE_HOST_AWARE:
2353                 snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware");
2354                 break;
2355         case DA_ZONE_HOST_MANAGED:
2356                 snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed");
2357                 break;
2358         case DA_ZONE_NONE:
2359         default:
2360                 snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned");
2361                 break;
2362         }
2363
2364         error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req);
2365
2366         return (error);
2367 }
2368
2369 static int
2370 dazonesupsysctl(SYSCTL_HANDLER_ARGS)
2371 {
2372         char tmpbuf[180];
2373         struct da_softc *softc;
2374         struct sbuf sb;
2375         int error, first;
2376         unsigned int i;
2377
2378         softc = (struct da_softc *)arg1;
2379
2380         error = 0;
2381         first = 1;
2382         sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0);
2383
2384         for (i = 0; i < sizeof(da_zone_desc_table) /
2385              sizeof(da_zone_desc_table[0]); i++) {
2386                 if (softc->zone_flags & da_zone_desc_table[i].value) {
2387                         if (first == 0)
2388                                 sbuf_printf(&sb, ", ");
2389                         else
2390                                 first = 0;
2391                         sbuf_cat(&sb, da_zone_desc_table[i].desc);
2392                 }
2393         }
2394
2395         if (first == 1)
2396                 sbuf_printf(&sb, "None");
2397
2398         sbuf_finish(&sb);
2399
2400         error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
2401
2402         return (error);
2403 }
2404
2405 static cam_status
2406 daregister(struct cam_periph *periph, void *arg)
2407 {
2408         struct da_softc *softc;
2409         struct ccb_pathinq cpi;
2410         struct ccb_getdev *cgd;
2411         char tmpstr[80];
2412         caddr_t match;
2413
2414         cgd = (struct ccb_getdev *)arg;
2415         if (cgd == NULL) {
2416                 printf("daregister: no getdev CCB, can't register device\n");
2417                 return(CAM_REQ_CMP_ERR);
2418         }
2419
2420         softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2421             M_NOWAIT|M_ZERO);
2422
2423         if (softc == NULL) {
2424                 printf("daregister: Unable to probe new device. "
2425                        "Unable to allocate softc\n");
2426                 return(CAM_REQ_CMP_ERR);
2427         }
2428
2429         if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
2430                 printf("daregister: Unable to probe new device. "
2431                        "Unable to allocate iosched memory\n");
2432                 free(softc, M_DEVBUF);
2433                 return(CAM_REQ_CMP_ERR);
2434         }
2435         
2436         LIST_INIT(&softc->pending_ccbs);
2437         softc->state = DA_STATE_PROBE_RC;
2438         bioq_init(&softc->delete_run_queue);
2439         if (SID_IS_REMOVABLE(&cgd->inq_data))
2440                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2441         softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2442         softc->unmap_max_lba = UNMAP_RANGE_MAX;
2443         softc->unmap_gran = 0;
2444         softc->unmap_gran_align = 0;
2445         softc->ws_max_blks = WS16_MAX_BLKS;
2446         softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2447         softc->rotating = 1;
2448
2449         periph->softc = softc;
2450
2451         /*
2452          * See if this device has any quirks.
2453          */
2454         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2455                                (caddr_t)da_quirk_table,
2456                                nitems(da_quirk_table),
2457                                sizeof(*da_quirk_table), scsi_inquiry_match);
2458
2459         if (match != NULL)
2460                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2461         else
2462                 softc->quirks = DA_Q_NONE;
2463
2464         /* Check if the SIM does not want 6 byte commands */
2465         bzero(&cpi, sizeof(cpi));
2466         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2467         cpi.ccb_h.func_code = XPT_PATH_INQ;
2468         xpt_action((union ccb *)&cpi);
2469         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2470                 softc->quirks |= DA_Q_NO_6_BYTE;
2471
2472         if (SID_TYPE(&cgd->inq_data) == T_ZBC_HM)
2473                 softc->zone_mode = DA_ZONE_HOST_MANAGED;
2474         else if (softc->quirks & DA_Q_SMR_DM)
2475                 softc->zone_mode = DA_ZONE_DRIVE_MANAGED;
2476         else
2477                 softc->zone_mode = DA_ZONE_NONE;
2478
2479         if (softc->zone_mode != DA_ZONE_NONE) {
2480                 if (scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2481                         if (scsi_vpd_supported_page(periph, SVPD_ZONED_BDC))
2482                                 softc->zone_interface = DA_ZONE_IF_ATA_SAT;
2483                         else
2484                                 softc->zone_interface = DA_ZONE_IF_ATA_PASS;
2485                 } else
2486                         softc->zone_interface = DA_ZONE_IF_SCSI;
2487         }
2488
2489         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2490
2491         /*
2492          * Take an exclusive refcount on the periph while dastart is called
2493          * to finish the probe.  The reference will be dropped in dadone at
2494          * the end of probe.
2495          */
2496         (void)cam_periph_hold(periph, PRIBIO);
2497
2498         /*
2499          * Schedule a periodic event to occasionally send an
2500          * ordered tag to a device.
2501          */
2502         callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2503         callout_reset(&softc->sendordered_c,
2504             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2505             dasendorderedtag, softc);
2506
2507         cam_periph_unlock(periph);
2508         /*
2509          * RBC devices don't have to support READ(6), only READ(10).
2510          */
2511         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2512                 softc->minimum_cmd_size = 10;
2513         else
2514                 softc->minimum_cmd_size = 6;
2515
2516         /*
2517          * Load the user's default, if any.
2518          */
2519         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2520                  periph->unit_number);
2521         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2522
2523         /*
2524          * 6, 10, 12 and 16 are the currently permissible values.
2525          */
2526         if (softc->minimum_cmd_size > 12)
2527                 softc->minimum_cmd_size = 16;
2528         else if (softc->minimum_cmd_size > 10)
2529                 softc->minimum_cmd_size = 12;
2530         else if (softc->minimum_cmd_size > 6)
2531                 softc->minimum_cmd_size = 10;
2532         else
2533                 softc->minimum_cmd_size = 6;
2534                 
2535         /* Predict whether device may support READ CAPACITY(16). */
2536         if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2537             (softc->quirks & DA_Q_NO_RC16) == 0) {
2538                 softc->flags |= DA_FLAG_CAN_RC16;
2539                 softc->state = DA_STATE_PROBE_RC16;
2540         }
2541
2542         /*
2543          * Register this media as a disk.
2544          */
2545         softc->disk = disk_alloc();
2546         softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2547                           periph->unit_number, 0,
2548                           DEVSTAT_BS_UNAVAILABLE,
2549                           SID_TYPE(&cgd->inq_data) |
2550                           XPORT_DEVSTAT_TYPE(cpi.transport),
2551                           DEVSTAT_PRIORITY_DISK);
2552         softc->disk->d_open = daopen;
2553         softc->disk->d_close = daclose;
2554         softc->disk->d_strategy = dastrategy;
2555         softc->disk->d_dump = dadump;
2556         softc->disk->d_getattr = dagetattr;
2557         softc->disk->d_gone = dadiskgonecb;
2558         softc->disk->d_name = "da";
2559         softc->disk->d_drv1 = periph;
2560         if (cpi.maxio == 0)
2561                 softc->maxio = DFLTPHYS;        /* traditional default */
2562         else if (cpi.maxio > MAXPHYS)
2563                 softc->maxio = MAXPHYS;         /* for safety */
2564         else
2565                 softc->maxio = cpi.maxio;
2566         softc->disk->d_maxsize = softc->maxio;
2567         softc->disk->d_unit = periph->unit_number;
2568         softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
2569         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2570                 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2571         if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
2572                 softc->unmappedio = 1;
2573                 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2574         }
2575         cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2576             sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2577         strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2578         cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2579             cgd->inq_data.product, sizeof(cgd->inq_data.product),
2580             sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2581         softc->disk->d_hba_vendor = cpi.hba_vendor;
2582         softc->disk->d_hba_device = cpi.hba_device;
2583         softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2584         softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2585
2586         /*
2587          * Acquire a reference to the periph before we register with GEOM.
2588          * We'll release this reference once GEOM calls us back (via
2589          * dadiskgonecb()) telling us that our provider has been freed.
2590          */
2591         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2592                 xpt_print(periph->path, "%s: lost periph during "
2593                           "registration!\n", __func__);
2594                 cam_periph_lock(periph);
2595                 return (CAM_REQ_CMP_ERR);
2596         }
2597
2598         disk_create(softc->disk, DISK_VERSION);
2599         cam_periph_lock(periph);
2600
2601         /*
2602          * Add async callbacks for events of interest.
2603          * I don't bother checking if this fails as,
2604          * in most cases, the system will function just
2605          * fine without them and the only alternative
2606          * would be to not attach the device on failure.
2607          */
2608         xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2609             AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
2610             AC_INQ_CHANGED, daasync, periph, periph->path);
2611
2612         /*
2613          * Emit an attribute changed notification just in case 
2614          * physical path information arrived before our async
2615          * event handler was registered, but after anyone attaching
2616          * to our disk device polled it.
2617          */
2618         disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2619
2620         /*
2621          * Schedule a periodic media polling events.
2622          */
2623         callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2624         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2625             (cgd->inq_flags & SID_AEN) == 0 &&
2626             da_poll_period != 0)
2627                 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2628                     damediapoll, periph);
2629
2630         xpt_schedule(periph, CAM_PRIORITY_DEV);
2631
2632         return(CAM_REQ_CMP);
2633 }
2634
2635 static int
2636 da_zone_bio_to_scsi(int disk_zone_cmd)
2637 {
2638         switch (disk_zone_cmd) {
2639         case DISK_ZONE_OPEN:
2640                 return ZBC_OUT_SA_OPEN;
2641         case DISK_ZONE_CLOSE:
2642                 return ZBC_OUT_SA_CLOSE;
2643         case DISK_ZONE_FINISH:
2644                 return ZBC_OUT_SA_FINISH;
2645         case DISK_ZONE_RWP:
2646                 return ZBC_OUT_SA_RWP;
2647         }
2648
2649         return -1;
2650 }
2651
2652 static int
2653 da_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
2654             int *queue_ccb)
2655 {
2656         struct da_softc *softc;
2657         int error;
2658
2659         error = 0;
2660
2661         if (bp->bio_cmd != BIO_ZONE) {
2662                 error = EINVAL;
2663                 goto bailout;
2664         }
2665
2666         softc = periph->softc;
2667
2668         switch (bp->bio_zone.zone_cmd) {
2669         case DISK_ZONE_OPEN:
2670         case DISK_ZONE_CLOSE:
2671         case DISK_ZONE_FINISH:
2672         case DISK_ZONE_RWP: {
2673                 int zone_flags;
2674                 int zone_sa;
2675                 uint64_t lba;
2676
2677                 zone_sa = da_zone_bio_to_scsi(bp->bio_zone.zone_cmd);
2678                 if (zone_sa == -1) {
2679                         xpt_print(periph->path, "Cannot translate zone "
2680                             "cmd %#x to SCSI\n", bp->bio_zone.zone_cmd);
2681                         error = EINVAL;
2682                         goto bailout;
2683                 }
2684
2685                 zone_flags = 0;
2686                 lba = bp->bio_zone.zone_params.rwp.id;
2687
2688                 if (bp->bio_zone.zone_params.rwp.flags &
2689                     DISK_ZONE_RWP_FLAG_ALL)
2690                         zone_flags |= ZBC_OUT_ALL;
2691
2692                 if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
2693                         scsi_zbc_out(&ccb->csio,
2694                                      /*retries*/ da_retry_count,
2695                                      /*cbfcnp*/ dadone,
2696                                      /*tag_action*/ MSG_SIMPLE_Q_TAG,
2697                                      /*service_action*/ zone_sa,
2698                                      /*zone_id*/ lba,
2699                                      /*zone_flags*/ zone_flags,
2700                                      /*data_ptr*/ NULL,
2701                                      /*dxfer_len*/ 0,
2702                                      /*sense_len*/ SSD_FULL_SIZE,
2703                                      /*timeout*/ da_default_timeout * 1000);
2704                 } else {
2705                         /*
2706                          * Note that in this case, even though we can
2707                          * technically use NCQ, we don't bother for several
2708                          * reasons:
2709                          * 1. It hasn't been tested on a SAT layer that
2710                          *    supports it.  This is new as of SAT-4.
2711                          * 2. Even when there is a SAT layer that supports
2712                          *    it, that SAT layer will also probably support
2713                          *    ZBC -> ZAC translation, since they are both
2714                          *    in the SAT-4 spec.
2715                          * 3. Translation will likely be preferable to ATA
2716                          *    passthrough.  LSI / Avago at least single
2717                          *    steps ATA passthrough commands in the HBA,
2718                          *    regardless of protocol, so unless that
2719                          *    changes, there is a performance penalty for
2720                          *    doing ATA passthrough no matter whether
2721                          *    you're using NCQ/FPDMA, DMA or PIO.
2722                          * 4. It requires a 32-byte CDB, which at least at
2723                          *    this point in CAM requires a CDB pointer, which
2724                          *    would require us to allocate an additional bit
2725                          *    of storage separate from the CCB.
2726                          */
2727                         error = scsi_ata_zac_mgmt_out(&ccb->csio,
2728                             /*retries*/ da_retry_count,
2729                             /*cbfcnp*/ dadone,
2730                             /*tag_action*/ MSG_SIMPLE_Q_TAG,
2731                             /*use_ncq*/ 0,
2732                             /*zm_action*/ zone_sa,
2733                             /*zone_id*/ lba,
2734                             /*zone_flags*/ zone_flags,
2735                             /*data_ptr*/ NULL,
2736                             /*dxfer_len*/ 0,
2737                             /*cdb_storage*/ NULL,
2738                             /*cdb_storage_len*/ 0,
2739                             /*sense_len*/ SSD_FULL_SIZE,
2740                             /*timeout*/ da_default_timeout * 1000);
2741                         if (error != 0) {
2742                                 error = EINVAL;
2743                                 xpt_print(periph->path,
2744                                     "scsi_ata_zac_mgmt_out() returned an "
2745                                     "error!");
2746                                 goto bailout;
2747                         }
2748                 }
2749                 *queue_ccb = 1;
2750
2751                 break;
2752         }
2753         case DISK_ZONE_REPORT_ZONES: {
2754                 uint8_t *rz_ptr;
2755                 uint32_t num_entries, alloc_size;
2756                 struct disk_zone_report *rep;
2757
2758                 rep = &bp->bio_zone.zone_params.report;
2759
2760                 num_entries = rep->entries_allocated;
2761                 if (num_entries == 0) {
2762                         xpt_print(periph->path, "No entries allocated for "
2763                             "Report Zones request\n");
2764                         error = EINVAL;
2765                         goto bailout;
2766                 }
2767                 alloc_size = sizeof(struct scsi_report_zones_hdr) +
2768                     (sizeof(struct scsi_report_zones_desc) * num_entries);
2769                 alloc_size = min(alloc_size, softc->disk->d_maxsize);
2770                 rz_ptr = malloc(alloc_size, M_SCSIDA, M_NOWAIT | M_ZERO);
2771                 if (rz_ptr == NULL) {
2772                         xpt_print(periph->path, "Unable to allocate memory "
2773                            "for Report Zones request\n");
2774                         error = ENOMEM;
2775                         goto bailout;
2776                 }
2777                 
2778                 if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
2779                         scsi_zbc_in(&ccb->csio,
2780                                     /*retries*/ da_retry_count,
2781                                     /*cbcfnp*/ dadone,
2782                                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
2783                                     /*service_action*/ ZBC_IN_SA_REPORT_ZONES,
2784                                     /*zone_start_lba*/ rep->starting_id,
2785                                     /*zone_options*/ rep->rep_options,
2786                                     /*data_ptr*/ rz_ptr,
2787                                     /*dxfer_len*/ alloc_size,
2788                                     /*sense_len*/ SSD_FULL_SIZE,
2789                                     /*timeout*/ da_default_timeout * 1000);
2790                 } else {
2791                         /*
2792                          * Note that in this case, even though we can
2793                          * technically use NCQ, we don't bother for several
2794                          * reasons:
2795                          * 1. It hasn't been tested on a SAT layer that
2796                          *    supports it.  This is new as of SAT-4.
2797                          * 2. Even when there is a SAT layer that supports
2798                          *    it, that SAT layer will also probably support
2799                          *    ZBC -> ZAC translation, since they are both
2800                          *    in the SAT-4 spec.
2801                          * 3. Translation will likely be preferable to ATA
2802                          *    passthrough.  LSI / Avago at least single
2803                          *    steps ATA passthrough commands in the HBA,
2804                          *    regardless of protocol, so unless that
2805                          *    changes, there is a performance penalty for
2806                          *    doing ATA passthrough no matter whether
2807                          *    you're using NCQ/FPDMA, DMA or PIO.
2808                          * 4. It requires a 32-byte CDB, which at least at
2809                          *    this point in CAM requires a CDB pointer, which
2810                          *    would require us to allocate an additional bit
2811                          *    of storage separate from the CCB.
2812                          */
2813                         error = scsi_ata_zac_mgmt_in(&ccb->csio,
2814                             /*retries*/ da_retry_count,
2815                             /*cbcfnp*/ dadone,
2816                             /*tag_action*/ MSG_SIMPLE_Q_TAG,
2817                             /*use_ncq*/ 0,
2818                             /*zm_action*/ ATA_ZM_REPORT_ZONES,
2819                             /*zone_id*/ rep->starting_id,
2820                             /*zone_flags*/ rep->rep_options,
2821                             /*data_ptr*/ rz_ptr,
2822                             /*dxfer_len*/ alloc_size,
2823                             /*cdb_storage*/ NULL,
2824                             /*cdb_storage_len*/ 0,
2825                             /*sense_len*/ SSD_FULL_SIZE,
2826                             /*timeout*/ da_default_timeout * 1000);
2827                         if (error != 0) {
2828                                 error = EINVAL;
2829                                 xpt_print(periph->path,
2830                                     "scsi_ata_zac_mgmt_in() returned an "
2831                                     "error!");
2832                                 goto bailout;
2833                         }
2834                 }
2835
2836                 /*
2837                  * For BIO_ZONE, this isn't normally needed.  However, it
2838                  * is used by devstat_end_transaction_bio() to determine
2839                  * how much data was transferred.
2840                  */
2841                 /*
2842                  * XXX KDM we have a problem.  But I'm not sure how to fix
2843                  * it.  devstat uses bio_bcount - bio_resid to calculate
2844                  * the amount of data transferred.   The GEOM disk code
2845                  * uses bio_length - bio_resid to calculate the amount of
2846                  * data in bio_completed.  We have different structure
2847                  * sizes above and below the ada(4) driver.  So, if we
2848                  * use the sizes above, the amount transferred won't be
2849                  * quite accurate for devstat.  If we use different sizes
2850                  * for bio_bcount and bio_length (above and below
2851                  * respectively), then the residual needs to match one or
2852                  * the other.  Everything is calculated after the bio
2853                  * leaves the driver, so changing the values around isn't
2854                  * really an option.  For now, just set the count to the
2855                  * passed in length.  This means that the calculations
2856                  * above (e.g. bio_completed) will be correct, but the
2857                  * amount of data reported to devstat will be slightly
2858                  * under or overstated.
2859                  */
2860                 bp->bio_bcount = bp->bio_length;
2861
2862                 *queue_ccb = 1;
2863
2864                 break;
2865         }
2866         case DISK_ZONE_GET_PARAMS: {
2867                 struct disk_zone_disk_params *params;
2868
2869                 params = &bp->bio_zone.zone_params.disk_params;
2870                 bzero(params, sizeof(*params));
2871
2872                 switch (softc->zone_mode) {
2873                 case DA_ZONE_DRIVE_MANAGED:
2874                         params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
2875                         break;
2876                 case DA_ZONE_HOST_AWARE:
2877                         params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
2878                         break;
2879                 case DA_ZONE_HOST_MANAGED:
2880                         params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
2881                         break;
2882                 default:
2883                 case DA_ZONE_NONE:
2884                         params->zone_mode = DISK_ZONE_MODE_NONE;
2885                         break;
2886                 }
2887
2888                 if (softc->zone_flags & DA_ZONE_FLAG_URSWRZ)
2889                         params->flags |= DISK_ZONE_DISK_URSWRZ;
2890
2891                 if (softc->zone_flags & DA_ZONE_FLAG_OPT_SEQ_SET) {
2892                         params->optimal_seq_zones = softc->optimal_seq_zones;
2893                         params->flags |= DISK_ZONE_OPT_SEQ_SET;
2894                 }
2895
2896                 if (softc->zone_flags & DA_ZONE_FLAG_OPT_NONSEQ_SET) {
2897                         params->optimal_nonseq_zones =
2898                             softc->optimal_nonseq_zones;
2899                         params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
2900                 }
2901
2902                 if (softc->zone_flags & DA_ZONE_FLAG_MAX_SEQ_SET) {
2903                         params->max_seq_zones = softc->max_seq_zones;
2904                         params->flags |= DISK_ZONE_MAX_SEQ_SET;
2905                 }
2906                 if (softc->zone_flags & DA_ZONE_FLAG_RZ_SUP)
2907                         params->flags |= DISK_ZONE_RZ_SUP;
2908
2909                 if (softc->zone_flags & DA_ZONE_FLAG_OPEN_SUP)
2910                         params->flags |= DISK_ZONE_OPEN_SUP;
2911
2912                 if (softc->zone_flags & DA_ZONE_FLAG_CLOSE_SUP)
2913                         params->flags |= DISK_ZONE_CLOSE_SUP;
2914
2915                 if (softc->zone_flags & DA_ZONE_FLAG_FINISH_SUP)
2916                         params->flags |= DISK_ZONE_FINISH_SUP;
2917
2918                 if (softc->zone_flags & DA_ZONE_FLAG_RWP_SUP)
2919                         params->flags |= DISK_ZONE_RWP_SUP;
2920                 break;
2921         }
2922         default:
2923                 break;
2924         }
2925 bailout:
2926         return (error);
2927 }
2928
2929 static void
2930 dastart(struct cam_periph *periph, union ccb *start_ccb)
2931 {
2932         struct da_softc *softc;
2933
2934         softc = (struct da_softc *)periph->softc;
2935
2936         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2937
2938 skipstate:
2939         switch (softc->state) {
2940         case DA_STATE_NORMAL:
2941         {
2942                 struct bio *bp;
2943                 uint8_t tag_code;
2944
2945 more:
2946                 bp = cam_iosched_next_bio(softc->cam_iosched);
2947                 if (bp == NULL) {
2948                         if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
2949                                 cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
2950                                 scsi_test_unit_ready(&start_ccb->csio,
2951                                      /*retries*/ da_retry_count,
2952                                      dadone,
2953                                      MSG_SIMPLE_Q_TAG,
2954                                      SSD_FULL_SIZE,
2955                                      da_default_timeout * 1000);
2956                                 start_ccb->ccb_h.ccb_bp = NULL;
2957                                 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2958                                 xpt_action(start_ccb);
2959                         } else
2960                                 xpt_release_ccb(start_ccb);
2961                         break;
2962                 }
2963
2964                 if (bp->bio_cmd == BIO_DELETE) {
2965                         if (softc->delete_func != NULL) {
2966                                 softc->delete_func(periph, start_ccb, bp);
2967                                 goto out;
2968                         } else {
2969                                 /* Not sure this is possible, but failsafe by lying and saying "sure, done." */
2970                                 biofinish(bp, NULL, 0);
2971                                 goto more;
2972                         }
2973                 }
2974
2975                 if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
2976                         cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
2977                         cam_periph_release_locked(periph);      /* XXX is this still valid? I think so but unverified */
2978                 }
2979
2980                 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2981                     (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2982                         softc->flags &= ~DA_FLAG_NEED_OTAG;
2983                         softc->flags |= DA_FLAG_WAS_OTAG;
2984                         tag_code = MSG_ORDERED_Q_TAG;
2985                 } else {
2986                         tag_code = MSG_SIMPLE_Q_TAG;
2987                 }
2988
2989                 switch (bp->bio_cmd) {
2990                 case BIO_WRITE:
2991                 case BIO_READ:
2992                 {
2993                         void *data_ptr;
2994                         int rw_op;
2995
2996                         biotrack(bp, __func__);
2997
2998                         if (bp->bio_cmd == BIO_WRITE) {
2999                                 softc->flags |= DA_FLAG_DIRTY;
3000                                 rw_op = SCSI_RW_WRITE;
3001                         } else {
3002                                 rw_op = SCSI_RW_READ;
3003                         }
3004
3005                         data_ptr = bp->bio_data;
3006                         if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
3007                                 rw_op |= SCSI_RW_BIO;
3008                                 data_ptr = bp;
3009                         }
3010
3011                         scsi_read_write(&start_ccb->csio,
3012                                         /*retries*/da_retry_count,
3013                                         /*cbfcnp*/dadone,
3014                                         /*tag_action*/tag_code,
3015                                         rw_op,
3016                                         /*byte2*/0,
3017                                         softc->minimum_cmd_size,
3018                                         /*lba*/bp->bio_pblkno,
3019                                         /*block_count*/bp->bio_bcount /
3020                                         softc->params.secsize,
3021                                         data_ptr,
3022                                         /*dxfer_len*/ bp->bio_bcount,
3023                                         /*sense_len*/SSD_FULL_SIZE,
3024                                         da_default_timeout * 1000);
3025 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
3026                         start_ccb->csio.bio = bp;
3027 #endif
3028                         break;
3029                 }
3030                 case BIO_FLUSH:
3031                         /*
3032                          * BIO_FLUSH doesn't currently communicate
3033                          * range data, so we synchronize the cache
3034                          * over the whole disk.  We also force
3035                          * ordered tag semantics the flush applies
3036                          * to all previously queued I/O.
3037                          */
3038                         scsi_synchronize_cache(&start_ccb->csio,
3039                                                /*retries*/1,
3040                                                /*cbfcnp*/dadone,
3041                                                MSG_ORDERED_Q_TAG,
3042                                                /*begin_lba*/0,
3043                                                /*lb_count*/0,
3044                                                SSD_FULL_SIZE,
3045                                                da_default_timeout*1000);
3046                         break;
3047                 case BIO_ZONE: {
3048                         int error, queue_ccb;
3049
3050                         queue_ccb = 0;
3051
3052                         error = da_zone_cmd(periph, start_ccb, bp,&queue_ccb);
3053                         if ((error != 0)
3054                          || (queue_ccb == 0)) {
3055                                 biofinish(bp, NULL, error);
3056                                 xpt_release_ccb(start_ccb);
3057                                 return;
3058                         }
3059                         break;
3060                 }
3061                 }
3062                 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
3063                 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
3064                 start_ccb->ccb_h.softtimeout = sbttotv(da_default_softtimeout);
3065
3066 out:
3067                 LIST_INSERT_HEAD(&softc->pending_ccbs,
3068                                  &start_ccb->ccb_h, periph_links.le);
3069
3070                 /* We expect a unit attention from this device */
3071                 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
3072                         start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
3073                         softc->flags &= ~DA_FLAG_RETRY_UA;
3074                 }
3075
3076                 start_ccb->ccb_h.ccb_bp = bp;
3077                 softc->refcount++;
3078                 cam_periph_unlock(periph);
3079                 xpt_action(start_ccb);
3080                 cam_periph_lock(periph);
3081                 softc->refcount--;
3082
3083                 /* May have more work to do, so ensure we stay scheduled */
3084                 daschedule(periph);
3085                 break;
3086         }
3087         case DA_STATE_PROBE_RC:
3088         {
3089                 struct scsi_read_capacity_data *rcap;
3090
3091                 rcap = (struct scsi_read_capacity_data *)
3092                     malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
3093                 if (rcap == NULL) {
3094                         printf("dastart: Couldn't malloc read_capacity data\n");
3095                         /* da_free_periph??? */
3096                         break;
3097                 }
3098                 scsi_read_capacity(&start_ccb->csio,
3099                                    /*retries*/da_retry_count,
3100                                    dadone,
3101                                    MSG_SIMPLE_Q_TAG,
3102                                    rcap,
3103                                    SSD_FULL_SIZE,
3104                                    /*timeout*/5000);
3105                 start_ccb->ccb_h.ccb_bp = NULL;
3106                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
3107                 xpt_action(start_ccb);
3108                 break;
3109         }
3110         case DA_STATE_PROBE_RC16:
3111         {
3112                 struct scsi_read_capacity_data_long *rcaplong;
3113
3114                 rcaplong = (struct scsi_read_capacity_data_long *)
3115                         malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
3116                 if (rcaplong == NULL) {
3117                         printf("dastart: Couldn't malloc read_capacity data\n");
3118                         /* da_free_periph??? */
3119                         break;
3120                 }
3121                 scsi_read_capacity_16(&start_ccb->csio,
3122                                       /*retries*/ da_retry_count,
3123                                       /*cbfcnp*/ dadone,
3124                                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
3125                                       /*lba*/ 0,
3126                                       /*reladr*/ 0,
3127                                       /*pmi*/ 0,
3128                                       /*rcap_buf*/ (uint8_t *)rcaplong,
3129                                       /*rcap_buf_len*/ sizeof(*rcaplong),
3130                                       /*sense_len*/ SSD_FULL_SIZE,
3131                                       /*timeout*/ da_default_timeout * 1000);
3132                 start_ccb->ccb_h.ccb_bp = NULL;
3133                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
3134                 xpt_action(start_ccb);
3135                 break;
3136         }
3137         case DA_STATE_PROBE_LBP:
3138         {
3139                 struct scsi_vpd_logical_block_prov *lbp;
3140
3141                 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
3142                         /*
3143                          * If we get here we don't support any SBC-3 delete
3144                          * methods with UNMAP as the Logical Block Provisioning
3145                          * VPD page support is required for devices which
3146                          * support it according to T10/1799-D Revision 31
3147                          * however older revisions of the spec don't mandate
3148                          * this so we currently don't remove these methods
3149                          * from the available set.
3150                          */
3151                         softc->state = DA_STATE_PROBE_BLK_LIMITS;
3152                         goto skipstate;
3153                 }
3154
3155                 lbp = (struct scsi_vpd_logical_block_prov *)
3156                         malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
3157
3158                 if (lbp == NULL) {
3159                         printf("dastart: Couldn't malloc lbp data\n");
3160                         /* da_free_periph??? */
3161                         break;
3162                 }
3163
3164                 scsi_inquiry(&start_ccb->csio,
3165                              /*retries*/da_retry_count,
3166                              /*cbfcnp*/dadone,
3167                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3168                              /*inq_buf*/(u_int8_t *)lbp,
3169                              /*inq_len*/sizeof(*lbp),
3170                              /*evpd*/TRUE,
3171                              /*page_code*/SVPD_LBP,
3172                              /*sense_len*/SSD_MIN_SIZE,
3173                              /*timeout*/da_default_timeout * 1000);
3174                 start_ccb->ccb_h.ccb_bp = NULL;
3175                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
3176                 xpt_action(start_ccb);
3177                 break;
3178         }
3179         case DA_STATE_PROBE_BLK_LIMITS:
3180         {
3181                 struct scsi_vpd_block_limits *block_limits;
3182
3183                 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
3184                         /* Not supported skip to next probe */
3185                         softc->state = DA_STATE_PROBE_BDC;
3186                         goto skipstate;
3187                 }
3188
3189                 block_limits = (struct scsi_vpd_block_limits *)
3190                         malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
3191
3192                 if (block_limits == NULL) {
3193                         printf("dastart: Couldn't malloc block_limits data\n");
3194                         /* da_free_periph??? */
3195                         break;
3196                 }
3197
3198                 scsi_inquiry(&start_ccb->csio,
3199                              /*retries*/da_retry_count,
3200                              /*cbfcnp*/dadone,
3201                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3202                              /*inq_buf*/(u_int8_t *)block_limits,
3203                              /*inq_len*/sizeof(*block_limits),
3204                              /*evpd*/TRUE,
3205                              /*page_code*/SVPD_BLOCK_LIMITS,
3206                              /*sense_len*/SSD_MIN_SIZE,
3207                              /*timeout*/da_default_timeout * 1000);
3208                 start_ccb->ccb_h.ccb_bp = NULL;
3209                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
3210                 xpt_action(start_ccb);
3211                 break;
3212         }
3213         case DA_STATE_PROBE_BDC:
3214         {
3215                 struct scsi_vpd_block_characteristics *bdc;
3216
3217                 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
3218                         softc->state = DA_STATE_PROBE_ATA;
3219                         goto skipstate;
3220                 }
3221
3222                 bdc = (struct scsi_vpd_block_characteristics *)
3223                         malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3224
3225                 if (bdc == NULL) {
3226                         printf("dastart: Couldn't malloc bdc data\n");
3227                         /* da_free_periph??? */
3228                         break;
3229                 }
3230
3231                 scsi_inquiry(&start_ccb->csio,
3232                              /*retries*/da_retry_count,
3233                              /*cbfcnp*/dadone,
3234                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3235                              /*inq_buf*/(u_int8_t *)bdc,
3236                              /*inq_len*/sizeof(*bdc),
3237                              /*evpd*/TRUE,
3238                              /*page_code*/SVPD_BDC,
3239                              /*sense_len*/SSD_MIN_SIZE,
3240                              /*timeout*/da_default_timeout * 1000);
3241                 start_ccb->ccb_h.ccb_bp = NULL;
3242                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
3243                 xpt_action(start_ccb);
3244                 break;
3245         }
3246         case DA_STATE_PROBE_ATA:
3247         {
3248                 struct ata_params *ata_params;
3249
3250                 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
3251                         if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
3252                          || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
3253                                 /*
3254                                  * Note that if the ATA VPD page isn't
3255                                  * supported, we aren't talking to an ATA
3256                                  * device anyway.  Support for that VPD
3257                                  * page is mandatory for SCSI to ATA (SAT)
3258                                  * translation layers.
3259                                  */
3260                                 softc->state = DA_STATE_PROBE_ZONE;
3261                                 goto skipstate;
3262                         }
3263                         daprobedone(periph, start_ccb);
3264                         break;
3265                 }
3266
3267                 ata_params = (struct ata_params*)
3268                         malloc(sizeof(*ata_params), M_SCSIDA,M_NOWAIT|M_ZERO);
3269
3270                 if (ata_params == NULL) {
3271                         xpt_print(periph->path, "Couldn't malloc ata_params "
3272                             "data\n");
3273                         /* da_free_periph??? */
3274                         break;
3275                 }
3276
3277                 scsi_ata_identify(&start_ccb->csio,
3278                                   /*retries*/da_retry_count,
3279                                   /*cbfcnp*/dadone,
3280                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
3281                                   /*data_ptr*/(u_int8_t *)ata_params,
3282                                   /*dxfer_len*/sizeof(*ata_params),
3283                                   /*sense_len*/SSD_FULL_SIZE,
3284                                   /*timeout*/da_default_timeout * 1000);
3285                 start_ccb->ccb_h.ccb_bp = NULL;
3286                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
3287                 xpt_action(start_ccb);
3288                 break;
3289         }
3290         case DA_STATE_PROBE_ATA_LOGDIR:
3291         {
3292                 struct ata_gp_log_dir *log_dir;
3293                 int retval;
3294
3295                 retval = 0;
3296
3297                 if ((softc->flags & DA_FLAG_CAN_ATA_LOG) == 0) {
3298                         /*
3299                          * If we don't have log support, not much point in
3300                          * trying to probe zone support.
3301                          */
3302                         daprobedone(periph, start_ccb);
3303                         break;
3304                 }
3305
3306                 /*
3307                  * If we have an ATA device (the SCSI ATA Information VPD
3308                  * page should be present and the ATA identify should have
3309                  * succeeded) and it supports logs, ask for the log directory.
3310                  */
3311
3312                 log_dir = malloc(sizeof(*log_dir), M_SCSIDA, M_NOWAIT|M_ZERO);
3313                 if (log_dir == NULL) {
3314                         xpt_print(periph->path, "Couldn't malloc log_dir "
3315                             "data\n");
3316                         daprobedone(periph, start_ccb);
3317                         break;
3318                 }
3319
3320                 retval = scsi_ata_read_log(&start_ccb->csio,
3321                     /*retries*/ da_retry_count,
3322                     /*cbfcnp*/ dadone,
3323                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3324                     /*log_address*/ ATA_LOG_DIRECTORY,
3325                     /*page_number*/ 0,
3326                     /*block_count*/ 1,
3327                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3328                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3329                     /*data_ptr*/ (uint8_t *)log_dir,
3330                     /*dxfer_len*/ sizeof(*log_dir),
3331                     /*sense_len*/ SSD_FULL_SIZE,
3332                     /*timeout*/ da_default_timeout * 1000);
3333
3334                 if (retval != 0) {
3335                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3336                         free(log_dir, M_SCSIDA);
3337                         daprobedone(periph, start_ccb);
3338                         break;
3339                 }
3340                 start_ccb->ccb_h.ccb_bp = NULL;
3341                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_LOGDIR;
3342                 xpt_action(start_ccb);
3343                 break;
3344         }
3345         case DA_STATE_PROBE_ATA_IDDIR:
3346         {
3347                 struct ata_identify_log_pages *id_dir;
3348                 int retval;
3349
3350                 retval = 0;
3351
3352                 /*
3353                  * Check here to see whether the Identify Device log is
3354                  * supported in the directory of logs.  If so, continue
3355                  * with requesting the log of identify device pages.
3356                  */
3357                 if ((softc->flags & DA_FLAG_CAN_ATA_IDLOG) == 0) {
3358                         daprobedone(periph, start_ccb);
3359                         break;
3360                 }
3361
3362                 id_dir = malloc(sizeof(*id_dir), M_SCSIDA, M_NOWAIT | M_ZERO);
3363                 if (id_dir == NULL) {
3364                         xpt_print(periph->path, "Couldn't malloc id_dir "
3365                             "data\n");
3366                         daprobedone(periph, start_ccb);
3367                         break;
3368                 }
3369
3370                 retval = scsi_ata_read_log(&start_ccb->csio,
3371                     /*retries*/ da_retry_count,
3372                     /*cbfcnp*/ dadone,
3373                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3374                     /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3375                     /*page_number*/ ATA_IDL_PAGE_LIST,
3376                     /*block_count*/ 1,
3377                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3378                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3379                     /*data_ptr*/ (uint8_t *)id_dir,
3380                     /*dxfer_len*/ sizeof(*id_dir),
3381                     /*sense_len*/ SSD_FULL_SIZE,
3382                     /*timeout*/ da_default_timeout * 1000);
3383
3384                 if (retval != 0) {
3385                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3386                         free(id_dir, M_SCSIDA);
3387                         daprobedone(periph, start_ccb);
3388                         break;
3389                 }
3390                 start_ccb->ccb_h.ccb_bp = NULL;
3391                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_IDDIR;
3392                 xpt_action(start_ccb);
3393                 break;
3394         }
3395         case DA_STATE_PROBE_ATA_SUP:
3396         {
3397                 struct ata_identify_log_sup_cap *sup_cap;
3398                 int retval;
3399
3400                 retval = 0;
3401
3402                 /*
3403                  * Check here to see whether the Supported Capabilities log
3404                  * is in the list of Identify Device logs.
3405                  */
3406                 if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP) == 0) {
3407                         daprobedone(periph, start_ccb);
3408                         break;
3409                 }
3410
3411                 sup_cap = malloc(sizeof(*sup_cap), M_SCSIDA, M_NOWAIT|M_ZERO);
3412                 if (sup_cap == NULL) {
3413                         xpt_print(periph->path, "Couldn't malloc sup_cap "
3414                             "data\n");
3415                         daprobedone(periph, start_ccb);
3416                         break;
3417                 }
3418
3419                 retval = scsi_ata_read_log(&start_ccb->csio,
3420                     /*retries*/ da_retry_count,
3421                     /*cbfcnp*/ dadone,
3422                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3423                     /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3424                     /*page_number*/ ATA_IDL_SUP_CAP,
3425                     /*block_count*/ 1,
3426                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3427                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3428                     /*data_ptr*/ (uint8_t *)sup_cap,
3429                     /*dxfer_len*/ sizeof(*sup_cap),
3430                     /*sense_len*/ SSD_FULL_SIZE,
3431                     /*timeout*/ da_default_timeout * 1000);
3432
3433                 if (retval != 0) {
3434                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3435                         free(sup_cap, M_SCSIDA);
3436                         daprobedone(periph, start_ccb);
3437                         break;
3438
3439                 }
3440
3441                 start_ccb->ccb_h.ccb_bp = NULL;
3442                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_SUP;
3443                 xpt_action(start_ccb);
3444                 break;
3445         }
3446         case DA_STATE_PROBE_ATA_ZONE:
3447         {
3448                 struct ata_zoned_info_log *ata_zone;
3449                 int retval;
3450
3451                 retval = 0;
3452
3453                 /*
3454                  * Check here to see whether the zoned device information
3455                  * page is supported.  If so, continue on to request it.
3456                  * If not, skip to DA_STATE_PROBE_LOG or done.
3457                  */
3458                 if ((softc->flags & DA_FLAG_CAN_ATA_ZONE) == 0) {
3459                         daprobedone(periph, start_ccb);
3460                         break;
3461                 }
3462                 ata_zone = malloc(sizeof(*ata_zone), M_SCSIDA,
3463                                   M_NOWAIT|M_ZERO);
3464                 if (ata_zone == NULL) {
3465                         xpt_print(periph->path, "Couldn't malloc ata_zone "
3466                             "data\n");
3467                         daprobedone(periph, start_ccb);
3468                         break;
3469                 }
3470
3471                 retval = scsi_ata_read_log(&start_ccb->csio,
3472                     /*retries*/ da_retry_count,
3473                     /*cbfcnp*/ dadone,
3474                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3475                     /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3476                     /*page_number*/ ATA_IDL_ZDI,
3477                     /*block_count*/ 1,
3478                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3479                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3480                     /*data_ptr*/ (uint8_t *)ata_zone,
3481                     /*dxfer_len*/ sizeof(*ata_zone),
3482                     /*sense_len*/ SSD_FULL_SIZE,
3483                     /*timeout*/ da_default_timeout * 1000);
3484
3485                 if (retval != 0) {
3486                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3487                         free(ata_zone, M_SCSIDA);
3488                         daprobedone(periph, start_ccb);
3489                         break;
3490                 }
3491                 start_ccb->ccb_h.ccb_bp = NULL;
3492                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_ZONE;
3493                 xpt_action(start_ccb);
3494
3495                 break;
3496         }
3497         case DA_STATE_PROBE_ZONE:
3498         {
3499                 struct scsi_vpd_zoned_bdc *bdc;
3500
3501                 /*
3502                  * Note that this page will be supported for SCSI protocol
3503                  * devices that support ZBC (SMR devices), as well as ATA
3504                  * protocol devices that are behind a SAT (SCSI to ATA
3505                  * Translation) layer that supports converting ZBC commands
3506                  * to their ZAC equivalents.
3507                  */
3508                 if (!scsi_vpd_supported_page(periph, SVPD_ZONED_BDC)) {
3509                         daprobedone(periph, start_ccb);
3510                         break;
3511                 }
3512                 bdc = (struct scsi_vpd_zoned_bdc *)
3513                         malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3514
3515                 if (bdc == NULL) {
3516                         xpt_release_ccb(start_ccb);
3517                         xpt_print(periph->path, "Couldn't malloc zone VPD "
3518                             "data\n");
3519                         break;
3520                 }
3521                 scsi_inquiry(&start_ccb->csio,
3522                              /*retries*/da_retry_count,
3523                              /*cbfcnp*/dadone,
3524                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3525                              /*inq_buf*/(u_int8_t *)bdc,
3526                              /*inq_len*/sizeof(*bdc),
3527                              /*evpd*/TRUE,
3528                              /*page_code*/SVPD_ZONED_BDC,
3529                              /*sense_len*/SSD_FULL_SIZE,
3530                              /*timeout*/da_default_timeout * 1000);
3531                 start_ccb->ccb_h.ccb_bp = NULL;
3532                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ZONE;
3533                 xpt_action(start_ccb);
3534                 break;
3535         }
3536         }
3537 }
3538
3539 /*
3540  * In each of the methods below, while its the caller's
3541  * responsibility to ensure the request will fit into a
3542  * single device request, we might have changed the delete
3543  * method due to the device incorrectly advertising either
3544  * its supported methods or limits.
3545  * 
3546  * To prevent this causing further issues we validate the
3547  * against the methods limits, and warn which would
3548  * otherwise be unnecessary.
3549  */
3550 static void
3551 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3552 {
3553         struct da_softc *softc = (struct da_softc *)periph->softc;;
3554         struct bio *bp1;
3555         uint8_t *buf = softc->unmap_buf;
3556         struct scsi_unmap_desc *d = (void *)&buf[UNMAP_HEAD_SIZE];
3557         uint64_t lba, lastlba = (uint64_t)-1;
3558         uint64_t totalcount = 0;
3559         uint64_t count;
3560         uint32_t c, lastcount = 0, ranges = 0;
3561
3562         /*
3563          * Currently this doesn't take the UNMAP
3564          * Granularity and Granularity Alignment
3565          * fields into account.
3566          *
3567          * This could result in both unoptimal unmap
3568          * requests as as well as UNMAP calls unmapping
3569          * fewer LBA's than requested.
3570          */
3571
3572         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
3573         bp1 = bp;
3574         do {
3575                 /*
3576                  * Note: ada and da are different in how they store the
3577                  * pending bp's in a trim. ada stores all of them in the
3578                  * trim_req.bps. da stores all but the first one in the
3579                  * delete_run_queue. ada then completes all the bps in
3580                  * its adadone() loop. da completes all the bps in the
3581                  * delete_run_queue in dadone, and relies on the biodone
3582                  * after to complete. This should be reconciled since there's
3583                  * no real reason to do it differently. XXX
3584                  */
3585                 if (bp1 != bp)
3586                         bioq_insert_tail(&softc->delete_run_queue, bp1);
3587                 lba = bp1->bio_pblkno;
3588                 count = bp1->bio_bcount / softc->params.secsize;
3589
3590                 /* Try to extend the previous range. */
3591                 if (lba == lastlba) {
3592                         c = omin(count, UNMAP_RANGE_MAX - lastcount);
3593                         lastlba += c;
3594                         lastcount += c;
3595                         scsi_ulto4b(lastcount, d[ranges - 1].length);
3596                         count -= c;
3597                         lba += c;
3598                         totalcount += c;
3599                 } else if ((softc->quirks & DA_Q_STRICT_UNMAP) &&
3600                     softc->unmap_gran != 0) {
3601                         /* Align length of the previous range. */
3602                         if ((c = lastcount % softc->unmap_gran) != 0) {
3603                                 if (lastcount <= c) {
3604                                         totalcount -= lastcount;
3605                                         lastlba = (uint64_t)-1;
3606                                         lastcount = 0;
3607                                         ranges--;
3608                                 } else {
3609                                         totalcount -= c;
3610                                         lastlba -= c;
3611                                         lastcount -= c;
3612                                         scsi_ulto4b(lastcount, d[ranges - 1].length);
3613                                 }
3614                         }
3615                         /* Align beginning of the new range. */
3616                         c = (lba - softc->unmap_gran_align) % softc->unmap_gran;
3617                         if (c != 0) {
3618                                 c = softc->unmap_gran - c;
3619                                 if (count <= c) {
3620                                         count = 0;
3621                                 } else {
3622                                         lba += c;
3623                                         count -= c;
3624                                 }
3625                         }
3626                 }
3627
3628                 while (count > 0) {
3629                         c = omin(count, UNMAP_RANGE_MAX);
3630                         if (totalcount + c > softc->unmap_max_lba ||
3631                             ranges >= softc->unmap_max_ranges) {
3632                                 xpt_print(periph->path,
3633                                     "%s issuing short delete %ld > %ld"
3634                                     "|| %d >= %d",
3635                                     da_delete_method_desc[softc->delete_method],
3636                                     totalcount + c, softc->unmap_max_lba,
3637                                     ranges, softc->unmap_max_ranges);
3638                                 break;
3639                         }
3640                         scsi_u64to8b(lba, d[ranges].lba);
3641                         scsi_ulto4b(c, d[ranges].length);
3642                         lba += c;
3643                         totalcount += c;
3644                         ranges++;
3645                         count -= c;
3646                         lastlba = lba;
3647                         lastcount = c;
3648                 }
3649                 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3650                 if (bp1 == NULL)
3651                         break;
3652                 if (ranges >= softc->unmap_max_ranges ||
3653                     totalcount + bp1->bio_bcount /
3654                     softc->params.secsize > softc->unmap_max_lba) {
3655                         cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3656                         break;
3657                 }
3658         } while (1);
3659
3660         /* Align length of the last range. */
3661         if ((softc->quirks & DA_Q_STRICT_UNMAP) && softc->unmap_gran != 0 &&
3662             (c = lastcount % softc->unmap_gran) != 0) {
3663                 if (lastcount <= c)
3664                         ranges--;
3665                 else
3666                         scsi_ulto4b(lastcount - c, d[ranges - 1].length);
3667         }
3668
3669         scsi_ulto2b(ranges * 16 + 6, &buf[0]);
3670         scsi_ulto2b(ranges * 16, &buf[2]);
3671
3672         scsi_unmap(&ccb->csio,
3673                    /*retries*/da_retry_count,
3674                    /*cbfcnp*/dadone,
3675                    /*tag_action*/MSG_SIMPLE_Q_TAG,
3676                    /*byte2*/0,
3677                    /*data_ptr*/ buf,
3678                    /*dxfer_len*/ ranges * 16 + 8,
3679                    /*sense_len*/SSD_FULL_SIZE,
3680                    da_default_timeout * 1000);
3681         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3682         ccb->ccb_h.flags |= CAM_UNLOCKED;
3683         cam_iosched_submit_trim(softc->cam_iosched);
3684 }
3685
3686 static void
3687 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3688 {
3689         struct da_softc *softc = (struct da_softc *)periph->softc;
3690         struct bio *bp1;
3691         uint8_t *buf = softc->unmap_buf;
3692         uint64_t lastlba = (uint64_t)-1;
3693         uint64_t count;
3694         uint64_t lba;
3695         uint32_t lastcount = 0, c, requestcount;
3696         int ranges = 0, off, block_count;
3697
3698         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
3699         bp1 = bp;
3700         do {
3701                 if (bp1 != bp)//XXX imp XXX
3702                         bioq_insert_tail(&softc->delete_run_queue, bp1);
3703                 lba = bp1->bio_pblkno;
3704                 count = bp1->bio_bcount / softc->params.secsize;
3705                 requestcount = count;
3706
3707                 /* Try to extend the previous range. */
3708                 if (lba == lastlba) {
3709                         c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
3710                         lastcount += c;
3711                         off = (ranges - 1) * 8;
3712                         buf[off + 6] = lastcount & 0xff;
3713                         buf[off + 7] = (lastcount >> 8) & 0xff;
3714                         count -= c;
3715                         lba += c;
3716                 }
3717
3718                 while (count > 0) {
3719                         c = omin(count, ATA_DSM_RANGE_MAX);
3720                         off = ranges * 8;
3721
3722                         buf[off + 0] = lba & 0xff;
3723                         buf[off + 1] = (lba >> 8) & 0xff;
3724                         buf[off + 2] = (lba >> 16) & 0xff;
3725                         buf[off + 3] = (lba >> 24) & 0xff;
3726                         buf[off + 4] = (lba >> 32) & 0xff;
3727                         buf[off + 5] = (lba >> 40) & 0xff;
3728                         buf[off + 6] = c & 0xff;
3729                         buf[off + 7] = (c >> 8) & 0xff;
3730                         lba += c;
3731                         ranges++;
3732                         count -= c;
3733                         lastcount = c;
3734                         if (count != 0 && ranges == softc->trim_max_ranges) {
3735                                 xpt_print(periph->path,
3736                                     "%s issuing short delete %ld > %ld\n",
3737                                     da_delete_method_desc[softc->delete_method],
3738                                     requestcount,
3739                                     (softc->trim_max_ranges - ranges) *
3740                                     ATA_DSM_RANGE_MAX);
3741                                 break;
3742                         }
3743                 }
3744                 lastlba = lba;
3745                 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3746                 if (bp1 == NULL)
3747                         break;
3748                 if (bp1->bio_bcount / softc->params.secsize >
3749                     (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
3750                         cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3751                         break;
3752                 }
3753         } while (1);
3754
3755         block_count = howmany(ranges, ATA_DSM_BLK_RANGES);
3756         scsi_ata_trim(&ccb->csio,
3757                       /*retries*/da_retry_count,
3758                       /*cbfcnp*/dadone,
3759                       /*tag_action*/MSG_SIMPLE_Q_TAG,
3760                       block_count,
3761                       /*data_ptr*/buf,
3762                       /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
3763                       /*sense_len*/SSD_FULL_SIZE,
3764                       da_default_timeout * 1000);
3765         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3766         ccb->ccb_h.flags |= CAM_UNLOCKED;
3767         cam_iosched_submit_trim(softc->cam_iosched);
3768 }
3769
3770 /*
3771  * We calculate ws_max_blks here based off d_delmaxsize instead
3772  * of using softc->ws_max_blks as it is absolute max for the
3773  * device not the protocol max which may well be lower.
3774  */
3775 static void
3776 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3777 {
3778         struct da_softc *softc;
3779         struct bio *bp1;
3780         uint64_t ws_max_blks;
3781         uint64_t lba;
3782         uint64_t count; /* forward compat with WS32 */
3783
3784         softc = (struct da_softc *)periph->softc;
3785         ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
3786         lba = bp->bio_pblkno;
3787         count = 0;
3788         bp1 = bp;
3789         do {
3790                 if (bp1 != bp)//XXX imp XXX
3791                         bioq_insert_tail(&softc->delete_run_queue, bp1);
3792                 count += bp1->bio_bcount / softc->params.secsize;
3793                 if (count > ws_max_blks) {
3794                         xpt_print(periph->path,
3795                             "%s issuing short delete %ld > %ld\n",
3796                             da_delete_method_desc[softc->delete_method],
3797                             count, ws_max_blks);
3798                         count = omin(count, ws_max_blks);
3799                         break;
3800                 }
3801                 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3802                 if (bp1 == NULL)
3803                         break;
3804                 if (lba + count != bp1->bio_pblkno ||
3805                     count + bp1->bio_bcount /
3806                     softc->params.secsize > ws_max_blks) {
3807                         cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3808                         break;
3809                 }
3810         } while (1);
3811
3812         scsi_write_same(&ccb->csio,
3813                         /*retries*/da_retry_count,
3814                         /*cbfcnp*/dadone,
3815                         /*tag_action*/MSG_SIMPLE_Q_TAG,
3816                         /*byte2*/softc->delete_method ==
3817                             DA_DELETE_ZERO ? 0 : SWS_UNMAP,
3818                         softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
3819                         /*lba*/lba,
3820                         /*block_count*/count,
3821                         /*data_ptr*/ __DECONST(void *, zero_region),
3822                         /*dxfer_len*/ softc->params.secsize,
3823                         /*sense_len*/SSD_FULL_SIZE,
3824                         da_default_timeout * 1000);
3825         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3826         ccb->ccb_h.flags |= CAM_UNLOCKED;
3827         cam_iosched_submit_trim(softc->cam_iosched);
3828 }
3829
3830 static int
3831 cmd6workaround(union ccb *ccb)
3832 {
3833         struct scsi_rw_6 cmd6;
3834         struct scsi_rw_10 *cmd10;
3835         struct da_softc *softc;
3836         u_int8_t *cdb;
3837         struct bio *bp;
3838         int frozen;
3839
3840         cdb = ccb->csio.cdb_io.cdb_bytes;
3841         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
3842
3843         if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
3844                 da_delete_methods old_method = softc->delete_method;
3845
3846                 /*
3847                  * Typically there are two reasons for failure here
3848                  * 1. Delete method was detected as supported but isn't
3849                  * 2. Delete failed due to invalid params e.g. too big
3850                  *
3851                  * While we will attempt to choose an alternative delete method
3852                  * this may result in short deletes if the existing delete
3853                  * requests from geom are big for the new method chosen.
3854                  *
3855                  * This method assumes that the error which triggered this
3856                  * will not retry the io otherwise a panic will occur
3857                  */
3858                 dadeleteflag(softc, old_method, 0);
3859                 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
3860                 if (softc->delete_method == DA_DELETE_DISABLE)
3861                         xpt_print(ccb->ccb_h.path,
3862                                   "%s failed, disabling BIO_DELETE\n",
3863                                   da_delete_method_desc[old_method]);
3864                 else
3865                         xpt_print(ccb->ccb_h.path,
3866                                   "%s failed, switching to %s BIO_DELETE\n",
3867                                   da_delete_method_desc[old_method],
3868                                   da_delete_method_desc[softc->delete_method]);
3869
3870                 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
3871                         cam_iosched_queue_work(softc->cam_iosched, bp);
3872                 cam_iosched_queue_work(softc->cam_iosched,
3873                     (struct bio *)ccb->ccb_h.ccb_bp);
3874                 ccb->ccb_h.ccb_bp = NULL;
3875                 return (0);
3876         }
3877
3878         /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
3879         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
3880             (*cdb == PREVENT_ALLOW) &&
3881             (softc->quirks & DA_Q_NO_PREVENT) == 0) {
3882                 if (bootverbose)
3883                         xpt_print(ccb->ccb_h.path,
3884                             "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
3885                 softc->quirks |= DA_Q_NO_PREVENT;
3886                 return (0);
3887         }
3888
3889         /* Detect unsupported SYNCHRONIZE CACHE(10). */
3890         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
3891             (*cdb == SYNCHRONIZE_CACHE) &&
3892             (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
3893                 if (bootverbose)
3894                         xpt_print(ccb->ccb_h.path,
3895                             "SYNCHRONIZE CACHE(10) not supported.\n");
3896                 softc->quirks |= DA_Q_NO_SYNC_CACHE;
3897                 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
3898                 return (0);
3899         }
3900
3901         /* Translation only possible if CDB is an array and cmd is R/W6 */
3902         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
3903             (*cdb != READ_6 && *cdb != WRITE_6))
3904                 return 0;
3905
3906         xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
3907             "increasing minimum_cmd_size to 10.\n");
3908         softc->minimum_cmd_size = 10;
3909
3910         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
3911         cmd10 = (struct scsi_rw_10 *)cdb;
3912         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
3913         cmd10->byte2 = 0;
3914         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
3915         cmd10->reserved = 0;
3916         scsi_ulto2b(cmd6.length, cmd10->length);
3917         cmd10->control = cmd6.control;
3918         ccb->csio.cdb_len = sizeof(*cmd10);
3919
3920         /* Requeue request, unfreezing queue if necessary */
3921         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3922         ccb->ccb_h.status = CAM_REQUEUE_REQ;
3923         xpt_action(ccb);
3924         if (frozen) {
3925                 cam_release_devq(ccb->ccb_h.path,
3926                                  /*relsim_flags*/0,
3927                                  /*reduction*/0,
3928                                  /*timeout*/0,
3929                                  /*getcount_only*/0);
3930         }
3931         return (ERESTART);
3932 }
3933
3934 static void
3935 dazonedone(struct cam_periph *periph, union ccb *ccb)
3936 {
3937         struct da_softc *softc;
3938         struct bio *bp;
3939
3940         softc = periph->softc;
3941         bp = (struct bio *)ccb->ccb_h.ccb_bp;
3942
3943         switch (bp->bio_zone.zone_cmd) {
3944         case DISK_ZONE_OPEN:
3945         case DISK_ZONE_CLOSE:
3946         case DISK_ZONE_FINISH:
3947         case DISK_ZONE_RWP:
3948                 break;
3949         case DISK_ZONE_REPORT_ZONES: {
3950                 uint32_t avail_len;
3951                 struct disk_zone_report *rep;
3952                 struct scsi_report_zones_hdr *hdr;
3953                 struct scsi_report_zones_desc *desc;
3954                 struct disk_zone_rep_entry *entry;
3955                 uint32_t num_alloced, hdr_len, num_avail;
3956                 uint32_t num_to_fill, i;
3957                 int ata;
3958
3959                 rep = &bp->bio_zone.zone_params.report;
3960                 avail_len = ccb->csio.dxfer_len - ccb->csio.resid;
3961                 /*
3962                  * Note that bio_resid isn't normally used for zone
3963                  * commands, but it is used by devstat_end_transaction_bio()
3964                  * to determine how much data was transferred.  Because
3965                  * the size of the SCSI/ATA data structures is different
3966                  * than the size of the BIO interface structures, the
3967                  * amount of data actually transferred from the drive will
3968                  * be different than the amount of data transferred to
3969                  * the user.
3970                  */
3971                 bp->bio_resid = ccb->csio.resid;
3972                 num_alloced = rep->entries_allocated;
3973                 hdr = (struct scsi_report_zones_hdr *)ccb->csio.data_ptr;
3974                 if (avail_len < sizeof(*hdr)) {
3975                         /*
3976                          * Is there a better error than EIO here?  We asked
3977                          * for at least the header, and we got less than
3978                          * that.
3979                          */
3980                         bp->bio_error = EIO;
3981                         bp->bio_flags |= BIO_ERROR;
3982                         bp->bio_resid = bp->bio_bcount;
3983                         break;
3984                 }
3985
3986                 if (softc->zone_interface == DA_ZONE_IF_ATA_PASS)
3987                         ata = 1;
3988                 else
3989                         ata = 0;
3990
3991                 hdr_len = ata ? le32dec(hdr->length) :
3992                                 scsi_4btoul(hdr->length);
3993                 if (hdr_len > 0)
3994                         rep->entries_available = hdr_len / sizeof(*desc);
3995                 else
3996                         rep->entries_available = 0;
3997                 /*
3998                  * NOTE: using the same values for the BIO version of the
3999                  * same field as the SCSI/ATA values.  This means we could
4000                  * get some additional values that aren't defined in bio.h
4001                  * if more values of the same field are defined later.
4002                  */
4003                 rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
4004                 rep->header.maximum_lba = ata ?  le64dec(hdr->maximum_lba) :
4005                                           scsi_8btou64(hdr->maximum_lba);
4006                 /*
4007                  * If the drive reports no entries that match the query,
4008                  * we're done.
4009                  */
4010                 if (hdr_len == 0) {
4011                         rep->entries_filled = 0;
4012                         break;
4013                 }
4014
4015                 num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
4016                                 hdr_len / sizeof(*desc));
4017                 /*
4018                  * If the drive didn't return any data, then we're done.
4019                  */
4020                 if (num_avail == 0) {
4021                         rep->entries_filled = 0;
4022                         break;
4023                 }
4024
4025                 num_to_fill = min(num_avail, rep->entries_allocated);
4026                 /*
4027                  * If the user didn't allocate any entries for us to fill,
4028                  * we're done.
4029                  */
4030                 if (num_to_fill == 0) {
4031                         rep->entries_filled = 0;
4032                         break;
4033                 }
4034
4035                 for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
4036                      i < num_to_fill; i++, desc++, entry++) {
4037                         /*
4038                          * NOTE: we're mapping the values here directly
4039                          * from the SCSI/ATA bit definitions to the bio.h
4040                          * definitons.  There is also a warning in
4041                          * disk_zone.h, but the impact is that if
4042                          * additional values are added in the SCSI/ATA
4043                          * specs these will be visible to consumers of
4044                          * this interface.
4045                          */
4046                         entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
4047                         entry->zone_condition =
4048                             (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
4049                             SRZ_ZONE_COND_SHIFT;
4050                         entry->zone_flags |= desc->zone_flags &
4051                             (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
4052                         entry->zone_length =
4053                             ata ? le64dec(desc->zone_length) :
4054                                   scsi_8btou64(desc->zone_length);
4055                         entry->zone_start_lba =
4056                             ata ? le64dec(desc->zone_start_lba) :
4057                                   scsi_8btou64(desc->zone_start_lba);
4058                         entry->write_pointer_lba =
4059                             ata ? le64dec(desc->write_pointer_lba) :
4060                                   scsi_8btou64(desc->write_pointer_lba);
4061                 }
4062                 rep->entries_filled = num_to_fill;
4063                 break;
4064         }
4065         case DISK_ZONE_GET_PARAMS:
4066         default:
4067                 /*
4068                  * In theory we should not get a GET_PARAMS bio, since it
4069                  * should be handled without queueing the command to the
4070                  * drive.
4071                  */
4072                 panic("%s: Invalid zone command %d", __func__,
4073                     bp->bio_zone.zone_cmd);
4074                 break;
4075         }
4076
4077         if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
4078                 free(ccb->csio.data_ptr, M_SCSIDA);
4079 }
4080
4081 static void
4082 dadone(struct cam_periph *periph, union ccb *done_ccb)
4083 {
4084         struct da_softc *softc;
4085         struct ccb_scsiio *csio;
4086         u_int32_t  priority;
4087         da_ccb_state state;
4088
4089         softc = (struct da_softc *)periph->softc;
4090         priority = done_ccb->ccb_h.pinfo.priority;
4091
4092         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
4093
4094         csio = &done_ccb->csio;
4095 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4096         if (csio->bio != NULL)
4097                 biotrack(csio->bio, __func__);
4098 #endif
4099         state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4100         switch (state) {
4101         case DA_CCB_BUFFER_IO:
4102         case DA_CCB_DELETE:
4103         {
4104                 struct bio *bp, *bp1;
4105
4106                 cam_periph_lock(periph);
4107                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4108                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4109                         int error;
4110                         int sf;
4111
4112                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
4113                                 sf = SF_RETRY_UA;
4114                         else
4115                                 sf = 0;
4116
4117                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
4118                         if (error == ERESTART) {
4119                                 /*
4120                                  * A retry was scheduled, so
4121                                  * just return.
4122                                  */
4123                                 cam_periph_unlock(periph);
4124                                 return;
4125                         }
4126                         bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4127                         if (error != 0) {
4128                                 int queued_error;
4129
4130                                 /*
4131                                  * return all queued I/O with EIO, so that
4132                                  * the client can retry these I/Os in the
4133                                  * proper order should it attempt to recover.
4134                                  */
4135                                 queued_error = EIO;
4136
4137                                 if (error == ENXIO
4138                                  && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
4139                                         /*
4140                                          * Catastrophic error.  Mark our pack as
4141                                          * invalid.
4142                                          */
4143                                         /*
4144                                          * XXX See if this is really a media
4145                                          * XXX change first?
4146                                          */
4147                                         xpt_print(periph->path,
4148                                             "Invalidating pack\n");
4149                                         softc->flags |= DA_FLAG_PACK_INVALID;
4150 #ifdef CAM_IO_STATS
4151                                         softc->invalidations++;
4152 #endif
4153                                         queued_error = ENXIO;
4154                                 }
4155                                 cam_iosched_flush(softc->cam_iosched, NULL,
4156                                            queued_error);
4157                                 if (bp != NULL) {
4158                                         bp->bio_error = error;
4159                                         bp->bio_resid = bp->bio_bcount;
4160                                         bp->bio_flags |= BIO_ERROR;
4161                                 }
4162                         } else if (bp != NULL) {
4163                                 if (state == DA_CCB_DELETE)
4164                                         bp->bio_resid = 0;
4165                                 else
4166                                         bp->bio_resid = csio->resid;
4167                                 bp->bio_error = 0;
4168                                 if (bp->bio_resid != 0)
4169                                         bp->bio_flags |= BIO_ERROR;
4170                         }
4171                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4172                                 cam_release_devq(done_ccb->ccb_h.path,
4173                                                  /*relsim_flags*/0,
4174                                                  /*reduction*/0,
4175                                                  /*timeout*/0,
4176                                                  /*getcount_only*/0);
4177                 } else if (bp != NULL) {
4178                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4179                                 panic("REQ_CMP with QFRZN");
4180                         if (bp->bio_cmd == BIO_ZONE)
4181                                 dazonedone(periph, done_ccb);
4182                         else if (state == DA_CCB_DELETE)
4183                                 bp->bio_resid = 0;
4184                         else
4185                                 bp->bio_resid = csio->resid;
4186                         if ((csio->resid > 0)
4187                          && (bp->bio_cmd != BIO_ZONE))
4188                                 bp->bio_flags |= BIO_ERROR;
4189                         if (softc->error_inject != 0) {
4190                                 bp->bio_error = softc->error_inject;
4191                                 bp->bio_resid = bp->bio_bcount;
4192                                 bp->bio_flags |= BIO_ERROR;
4193                                 softc->error_inject = 0;
4194                         }
4195                 }
4196
4197                 if (bp != NULL)
4198                         biotrack(bp, __func__);
4199                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
4200                 if (LIST_EMPTY(&softc->pending_ccbs))
4201                         softc->flags |= DA_FLAG_WAS_OTAG;
4202
4203                 /*
4204                  * We need to call cam_iosched before we call biodone so that we
4205                  * don't measure any activity that happens in the completion
4206                  * routine, which in the case of sendfile can be quite
4207                  * extensive.
4208                  */
4209                 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
4210                 xpt_release_ccb(done_ccb);
4211                 if (state == DA_CCB_DELETE) {
4212                         TAILQ_HEAD(, bio) queue;
4213
4214                         TAILQ_INIT(&queue);
4215                         TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
4216                         softc->delete_run_queue.insert_point = NULL;
4217                         /*
4218                          * Normally, the xpt_release_ccb() above would make sure
4219                          * that when we have more work to do, that work would
4220                          * get kicked off. However, we specifically keep
4221                          * delete_running set to 0 before the call above to
4222                          * allow other I/O to progress when many BIO_DELETE
4223                          * requests are pushed down. We set delete_running to 0
4224                          * and call daschedule again so that we don't stall if
4225                          * there are no other I/Os pending apart from BIO_DELETEs.
4226                          */
4227                         cam_iosched_trim_done(softc->cam_iosched);
4228                         daschedule(periph);
4229                         cam_periph_unlock(periph);
4230                         while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
4231                                 TAILQ_REMOVE(&queue, bp1, bio_queue);
4232                                 bp1->bio_error = bp->bio_error;
4233                                 if (bp->bio_flags & BIO_ERROR) {
4234                                         bp1->bio_flags |= BIO_ERROR;
4235                                         bp1->bio_resid = bp1->bio_bcount;
4236                                 } else
4237                                         bp1->bio_resid = 0;
4238                                 biodone(bp1);
4239                         }
4240                 } else {
4241                         daschedule(periph);
4242                         cam_periph_unlock(periph);
4243                 }
4244                 if (bp != NULL)
4245                         biodone(bp);
4246                 return;
4247         }
4248         case DA_CCB_PROBE_RC:
4249         case DA_CCB_PROBE_RC16:
4250         {
4251                 struct     scsi_read_capacity_data *rdcap;
4252                 struct     scsi_read_capacity_data_long *rcaplong;
4253                 char       *announce_buf;
4254                 int        lbp;
4255
4256                 lbp = 0;
4257                 rdcap = NULL;
4258                 rcaplong = NULL;
4259                 /* XXX TODO: can this be a malloc? */
4260                 announce_buf = softc->announce_temp;
4261                 bzero(announce_buf, DA_ANNOUNCETMP_SZ);
4262
4263                 if (state == DA_CCB_PROBE_RC)
4264                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
4265                 else
4266                         rcaplong = (struct scsi_read_capacity_data_long *)
4267                                 csio->data_ptr;
4268
4269                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4270                         struct disk_params *dp;
4271                         uint32_t block_size;
4272                         uint64_t maxsector;
4273                         u_int lalba;    /* Lowest aligned LBA. */
4274
4275                         if (state == DA_CCB_PROBE_RC) {
4276                                 block_size = scsi_4btoul(rdcap->length);
4277                                 maxsector = scsi_4btoul(rdcap->addr);
4278                                 lalba = 0;
4279
4280                                 /*
4281                                  * According to SBC-2, if the standard 10
4282                                  * byte READ CAPACITY command returns 2^32,
4283                                  * we should issue the 16 byte version of
4284                                  * the command, since the device in question
4285                                  * has more sectors than can be represented
4286                                  * with the short version of the command.
4287                                  */
4288                                 if (maxsector == 0xffffffff) {
4289                                         free(rdcap, M_SCSIDA);
4290                                         xpt_release_ccb(done_ccb);
4291                                         softc->state = DA_STATE_PROBE_RC16;
4292                                         xpt_schedule(periph, priority);
4293                                         return;
4294                                 }
4295                         } else {
4296                                 block_size = scsi_4btoul(rcaplong->length);
4297                                 maxsector = scsi_8btou64(rcaplong->addr);
4298                                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
4299                         }
4300
4301                         /*
4302                          * Because GEOM code just will panic us if we
4303                          * give them an 'illegal' value we'll avoid that
4304                          * here.
4305                          */
4306                         if (block_size == 0) {
4307                                 block_size = 512;
4308                                 if (maxsector == 0)
4309                                         maxsector = -1;
4310                         }
4311                         if (block_size >= MAXPHYS) {
4312                                 xpt_print(periph->path,
4313                                     "unsupportable block size %ju\n",
4314                                     (uintmax_t) block_size);
4315                                 announce_buf = NULL;
4316                                 cam_periph_invalidate(periph);
4317                         } else {
4318                                 /*
4319                                  * We pass rcaplong into dasetgeom(),
4320                                  * because it will only use it if it is
4321                                  * non-NULL.
4322                                  */
4323                                 dasetgeom(periph, block_size, maxsector,
4324                                           rcaplong, sizeof(*rcaplong));
4325                                 lbp = (lalba & SRC16_LBPME_A);
4326                                 dp = &softc->params;
4327                                 snprintf(announce_buf, DA_ANNOUNCETMP_SZ,
4328                                     "%juMB (%ju %u byte sectors)",
4329                                     ((uintmax_t)dp->secsize * dp->sectors) /
4330                                      (1024 * 1024),
4331                                     (uintmax_t)dp->sectors, dp->secsize);
4332                         }
4333                 } else {
4334                         int     error;
4335
4336                         /*
4337                          * Retry any UNIT ATTENTION type errors.  They
4338                          * are expected at boot.
4339                          */
4340                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4341                                         SF_RETRY_UA|SF_NO_PRINT);
4342                         if (error == ERESTART) {
4343                                 /*
4344                                  * A retry was scheuled, so
4345                                  * just return.
4346                                  */
4347                                 return;
4348                         } else if (error != 0) {
4349                                 int asc, ascq;
4350                                 int sense_key, error_code;
4351                                 int have_sense;
4352                                 cam_status status;
4353                                 struct ccb_getdev cgd;
4354
4355                                 /* Don't wedge this device's queue */
4356                                 status = done_ccb->ccb_h.status;
4357                                 if ((status & CAM_DEV_QFRZN) != 0)
4358                                         cam_release_devq(done_ccb->ccb_h.path,
4359                                                          /*relsim_flags*/0,
4360                                                          /*reduction*/0,
4361                                                          /*timeout*/0,
4362                                                          /*getcount_only*/0);
4363
4364
4365                                 xpt_setup_ccb(&cgd.ccb_h, 
4366                                               done_ccb->ccb_h.path,
4367                                               CAM_PRIORITY_NORMAL);
4368                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4369                                 xpt_action((union ccb *)&cgd);
4370
4371                                 if (scsi_extract_sense_ccb(done_ccb,
4372                                     &error_code, &sense_key, &asc, &ascq))
4373                                         have_sense = TRUE;
4374                                 else
4375                                         have_sense = FALSE;
4376
4377                                 /*
4378                                  * If we tried READ CAPACITY(16) and failed,
4379                                  * fallback to READ CAPACITY(10).
4380                                  */
4381                                 if ((state == DA_CCB_PROBE_RC16) &&
4382                                     (softc->flags & DA_FLAG_CAN_RC16) &&
4383                                     (((csio->ccb_h.status & CAM_STATUS_MASK) ==
4384                                         CAM_REQ_INVALID) ||
4385                                      ((have_sense) &&
4386                                       (error_code == SSD_CURRENT_ERROR) &&
4387                                       (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
4388                                         softc->flags &= ~DA_FLAG_CAN_RC16;
4389                                         free(rdcap, M_SCSIDA);
4390                                         xpt_release_ccb(done_ccb);
4391                                         softc->state = DA_STATE_PROBE_RC;
4392                                         xpt_schedule(periph, priority);
4393                                         return;
4394                                 }
4395
4396                                 /*
4397                                  * Attach to anything that claims to be a
4398                                  * direct access or optical disk device,
4399                                  * as long as it doesn't return a "Logical
4400                                  * unit not supported" (0x25) error.
4401                                  * "Internal Target Failure" (0x44) is also
4402                                  * special and typically means that the
4403                                  * device is a SATA drive behind a SATL
4404                                  * translation that's fallen into a
4405                                  * terminally fatal state.
4406                                  */
4407                                 if ((have_sense)
4408                                  && (asc != 0x25) && (asc != 0x44)
4409                                  && (error_code == SSD_CURRENT_ERROR)) {
4410                                         const char *sense_key_desc;
4411                                         const char *asc_desc;
4412
4413                                         dasetgeom(periph, 512, -1, NULL, 0);
4414                                         scsi_sense_desc(sense_key, asc, ascq,
4415                                                         &cgd.inq_data,
4416                                                         &sense_key_desc,
4417                                                         &asc_desc);
4418                                         snprintf(announce_buf,
4419                                             DA_ANNOUNCETMP_SZ,
4420                                             "Attempt to query device "
4421                                             "size failed: %s, %s",
4422                                             sense_key_desc, asc_desc);
4423                                 } else { 
4424                                         if (have_sense)
4425                                                 scsi_sense_print(
4426                                                         &done_ccb->csio);
4427                                         else {
4428                                                 xpt_print(periph->path,
4429                                                     "got CAM status %#x\n",
4430                                                     done_ccb->ccb_h.status);
4431                                         }
4432
4433                                         xpt_print(periph->path, "fatal error, "
4434                                             "failed to attach to device\n");
4435
4436                                         announce_buf = NULL;
4437
4438                                         /*
4439                                          * Free up resources.
4440                                          */
4441                                         cam_periph_invalidate(periph);
4442                                 } 
4443                         }
4444                 }
4445                 free(csio->data_ptr, M_SCSIDA);
4446                 if (announce_buf != NULL &&
4447                     ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
4448                         struct sbuf sb;
4449
4450                         sbuf_new(&sb, softc->announcebuf, DA_ANNOUNCE_SZ,
4451                             SBUF_FIXEDLEN);
4452                         xpt_announce_periph_sbuf(periph, &sb, announce_buf);
4453                         xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
4454                             DA_Q_BIT_STRING);
4455                         sbuf_finish(&sb);
4456                         sbuf_putbuf(&sb);
4457
4458                         /*
4459                          * Create our sysctl variables, now that we know
4460                          * we have successfully attached.
4461                          */
4462                         /* increase the refcount */
4463                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
4464
4465                                 taskqueue_enqueue(taskqueue_thread,
4466                                                   &softc->sysctl_task);
4467                         } else {
4468                                 /* XXX This message is useless! */
4469                                 xpt_print(periph->path, "fatal error, "
4470                                     "could not acquire reference count\n");
4471                         }
4472                 }
4473
4474                 /* We already probed the device. */
4475                 if (softc->flags & DA_FLAG_PROBED) {
4476                         daprobedone(periph, done_ccb);
4477                         return;
4478                 }
4479
4480                 /* Ensure re-probe doesn't see old delete. */
4481                 softc->delete_available = 0;
4482                 dadeleteflag(softc, DA_DELETE_ZERO, 1);
4483                 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
4484                         /*
4485                          * Based on older SBC-3 spec revisions
4486                          * any of the UNMAP methods "may" be
4487                          * available via LBP given this flag so
4488                          * we flag all of them as available and
4489                          * then remove those which further
4490                          * probes confirm aren't available
4491                          * later.
4492                          *
4493                          * We could also check readcap(16) p_type
4494                          * flag to exclude one or more invalid
4495                          * write same (X) types here
4496                          */
4497                         dadeleteflag(softc, DA_DELETE_WS16, 1);
4498                         dadeleteflag(softc, DA_DELETE_WS10, 1);
4499                         dadeleteflag(softc, DA_DELETE_UNMAP, 1);
4500
4501                         xpt_release_ccb(done_ccb);
4502                         softc->state = DA_STATE_PROBE_LBP;
4503                         xpt_schedule(periph, priority);
4504                         return;
4505                 }
4506
4507                 xpt_release_ccb(done_ccb);
4508                 softc->state = DA_STATE_PROBE_BDC;
4509                 xpt_schedule(periph, priority);
4510                 return;
4511         }
4512         case DA_CCB_PROBE_LBP:
4513         {
4514                 struct scsi_vpd_logical_block_prov *lbp;
4515
4516                 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
4517
4518                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4519                         /*
4520                          * T10/1799-D Revision 31 states at least one of these
4521                          * must be supported but we don't currently enforce this.
4522                          */
4523                         dadeleteflag(softc, DA_DELETE_WS16,
4524                                      (lbp->flags & SVPD_LBP_WS16));
4525                         dadeleteflag(softc, DA_DELETE_WS10,
4526                                      (lbp->flags & SVPD_LBP_WS10));
4527                         dadeleteflag(softc, DA_DELETE_UNMAP,
4528                                      (lbp->flags & SVPD_LBP_UNMAP));
4529                 } else {
4530                         int error;
4531                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4532                                         SF_RETRY_UA|SF_NO_PRINT);
4533                         if (error == ERESTART)
4534                                 return;
4535                         else if (error != 0) {
4536                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4537                                         /* Don't wedge this device's queue */
4538                                         cam_release_devq(done_ccb->ccb_h.path,
4539                                                          /*relsim_flags*/0,
4540                                                          /*reduction*/0,
4541                                                          /*timeout*/0,
4542                                                          /*getcount_only*/0);
4543                                 }
4544
4545                                 /*
4546                                  * Failure indicates we don't support any SBC-3
4547                                  * delete methods with UNMAP
4548                                  */
4549                         }
4550                 }
4551
4552                 free(lbp, M_SCSIDA);
4553                 xpt_release_ccb(done_ccb);
4554                 softc->state = DA_STATE_PROBE_BLK_LIMITS;
4555                 xpt_schedule(periph, priority);
4556                 return;
4557         }
4558         case DA_CCB_PROBE_BLK_LIMITS:
4559         {
4560                 struct scsi_vpd_block_limits *block_limits;
4561
4562                 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
4563
4564                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4565                         uint32_t max_txfer_len = scsi_4btoul(
4566                                 block_limits->max_txfer_len);
4567                         uint32_t max_unmap_lba_cnt = scsi_4btoul(
4568                                 block_limits->max_unmap_lba_cnt);
4569                         uint32_t max_unmap_blk_cnt = scsi_4btoul(
4570                                 block_limits->max_unmap_blk_cnt);
4571                         uint32_t unmap_gran = scsi_4btoul(
4572                                 block_limits->opt_unmap_grain);
4573                         uint32_t unmap_gran_align = scsi_4btoul(
4574                                 block_limits->unmap_grain_align);
4575                         uint64_t ws_max_blks = scsi_8btou64(
4576                                 block_limits->max_write_same_length);
4577
4578                         if (max_txfer_len != 0) {
4579                                 softc->disk->d_maxsize = MIN(softc->maxio,
4580                                     (off_t)max_txfer_len * softc->params.secsize);
4581                         }
4582
4583                         /*
4584                          * We should already support UNMAP but we check lba
4585                          * and block count to be sure
4586                          */
4587                         if (max_unmap_lba_cnt != 0x00L &&
4588                             max_unmap_blk_cnt != 0x00L) {
4589                                 softc->unmap_max_lba = max_unmap_lba_cnt;
4590                                 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
4591                                         UNMAP_MAX_RANGES);
4592                                 if (unmap_gran > 1) {
4593                                         softc->unmap_gran = unmap_gran;
4594                                         if (unmap_gran_align & 0x80000000) {
4595                                                 softc->unmap_gran_align =
4596                                                     unmap_gran_align &
4597                                                     0x7fffffff;
4598                                         }
4599                                 }
4600                         } else {
4601                                 /*
4602                                  * Unexpected UNMAP limits which means the
4603                                  * device doesn't actually support UNMAP
4604                                  */
4605                                 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
4606                         }
4607
4608                         if (ws_max_blks != 0x00L)
4609                                 softc->ws_max_blks = ws_max_blks;
4610                 } else {
4611                         int error;
4612                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4613                                         SF_RETRY_UA|SF_NO_PRINT);
4614                         if (error == ERESTART)
4615                                 return;
4616                         else if (error != 0) {
4617                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4618                                         /* Don't wedge this device's queue */
4619                                         cam_release_devq(done_ccb->ccb_h.path,
4620                                                          /*relsim_flags*/0,
4621                                                          /*reduction*/0,
4622                                                          /*timeout*/0,
4623                                                          /*getcount_only*/0);
4624                                 }
4625
4626                                 /*
4627                                  * Failure here doesn't mean UNMAP is not
4628                                  * supported as this is an optional page.
4629                                  */
4630                                 softc->unmap_max_lba = 1;
4631                                 softc->unmap_max_ranges = 1;
4632                         }
4633                 }
4634
4635                 free(block_limits, M_SCSIDA);
4636                 xpt_release_ccb(done_ccb);
4637                 softc->state = DA_STATE_PROBE_BDC;
4638                 xpt_schedule(periph, priority);
4639                 return;
4640         }
4641         case DA_CCB_PROBE_BDC:
4642         {
4643                 struct scsi_vpd_block_device_characteristics *bdc;
4644
4645                 bdc = (struct scsi_vpd_block_device_characteristics *)
4646                     csio->data_ptr;
4647
4648                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4649                         uint32_t valid_len;
4650
4651                         /*
4652                          * Disable queue sorting for non-rotational media
4653                          * by default.
4654                          */
4655                         u_int16_t old_rate = softc->disk->d_rotation_rate;
4656
4657                         valid_len = csio->dxfer_len - csio->resid;
4658                         if (SBDC_IS_PRESENT(bdc, valid_len,
4659                             medium_rotation_rate)) {
4660                                 softc->disk->d_rotation_rate =
4661                                         scsi_2btoul(bdc->medium_rotation_rate);
4662                                 if (softc->disk->d_rotation_rate ==
4663                                     SVPD_BDC_RATE_NON_ROTATING) {
4664                                         cam_iosched_set_sort_queue(
4665                                             softc->cam_iosched, 0);
4666                                         softc->rotating = 0;
4667                                 }
4668                                 if (softc->disk->d_rotation_rate != old_rate) {
4669                                         disk_attr_changed(softc->disk,
4670                                             "GEOM::rotation_rate", M_NOWAIT);
4671                                 }
4672                         }
4673                         if ((SBDC_IS_PRESENT(bdc, valid_len, flags))
4674                          && (softc->zone_mode == DA_ZONE_NONE)) {
4675                                 int ata_proto;
4676
4677                                 if (scsi_vpd_supported_page(periph,
4678                                     SVPD_ATA_INFORMATION))
4679                                         ata_proto = 1;
4680                                 else
4681                                         ata_proto = 0;
4682
4683                                 /*
4684                                  * The Zoned field will only be set for
4685                                  * Drive Managed and Host Aware drives.  If
4686                                  * they are Host Managed, the device type
4687                                  * in the standard INQUIRY data should be
4688                                  * set to T_ZBC_HM (0x14).
4689                                  */
4690                                 if ((bdc->flags & SVPD_ZBC_MASK) ==
4691                                      SVPD_HAW_ZBC) {
4692                                         softc->zone_mode = DA_ZONE_HOST_AWARE;
4693                                         softc->zone_interface = (ata_proto) ?
4694                                            DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
4695                                 } else if ((bdc->flags & SVPD_ZBC_MASK) ==
4696                                      SVPD_DM_ZBC) {
4697                                         softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
4698                                         softc->zone_interface = (ata_proto) ?
4699                                            DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
4700                                 } else if ((bdc->flags & SVPD_ZBC_MASK) != 
4701                                           SVPD_ZBC_NR) {
4702                                         xpt_print(periph->path, "Unknown zoned "
4703                                             "type %#x",
4704                                             bdc->flags & SVPD_ZBC_MASK);
4705                                 }
4706                         }
4707                 } else {
4708                         int error;
4709                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4710                                         SF_RETRY_UA|SF_NO_PRINT);
4711                         if (error == ERESTART)
4712                                 return;
4713                         else if (error != 0) {
4714                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4715                                         /* Don't wedge this device's queue */
4716                                         cam_release_devq(done_ccb->ccb_h.path,
4717                                                          /*relsim_flags*/0,
4718                                                          /*reduction*/0,
4719                                                          /*timeout*/0,
4720                                                          /*getcount_only*/0);
4721                                 }
4722                         }
4723                 }
4724
4725                 free(bdc, M_SCSIDA);
4726                 xpt_release_ccb(done_ccb);
4727                 softc->state = DA_STATE_PROBE_ATA;
4728                 xpt_schedule(periph, priority);
4729                 return;
4730         }
4731         case DA_CCB_PROBE_ATA:
4732         {
4733                 int i;
4734                 struct ata_params *ata_params;
4735                 int continue_probe;
4736                 int error;
4737                 int16_t *ptr;
4738
4739                 ata_params = (struct ata_params *)csio->data_ptr;
4740                 ptr = (uint16_t *)ata_params;
4741                 continue_probe = 0;
4742                 error = 0;
4743
4744                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4745                         uint16_t old_rate;
4746
4747                         for (i = 0; i < sizeof(*ata_params) / 2; i++)
4748                                 ptr[i] = le16toh(ptr[i]);
4749                         if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
4750                             (softc->quirks & DA_Q_NO_UNMAP) == 0) {
4751                                 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
4752                                 if (ata_params->max_dsm_blocks != 0)
4753                                         softc->trim_max_ranges = min(
4754                                           softc->trim_max_ranges,
4755                                           ata_params->max_dsm_blocks *
4756                                           ATA_DSM_BLK_RANGES);
4757                         }
4758                         /*
4759                          * Disable queue sorting for non-rotational media
4760                          * by default.
4761                          */
4762                         old_rate = softc->disk->d_rotation_rate;
4763                         softc->disk->d_rotation_rate =
4764                             ata_params->media_rotation_rate;
4765                         if (softc->disk->d_rotation_rate ==
4766                             ATA_RATE_NON_ROTATING) {
4767                                 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
4768                                 softc->rotating = 0;
4769                         }
4770                         if (softc->disk->d_rotation_rate != old_rate) {
4771                                 disk_attr_changed(softc->disk,
4772                                     "GEOM::rotation_rate", M_NOWAIT);
4773                         }
4774
4775                         if (ata_params->capabilities1 & ATA_SUPPORT_DMA)
4776                                 softc->flags |= DA_FLAG_CAN_ATA_DMA;
4777
4778                         if (ata_params->support.extension &
4779                             ATA_SUPPORT_GENLOG)
4780                                 softc->flags |= DA_FLAG_CAN_ATA_LOG;
4781
4782                         /*
4783                          * At this point, if we have a SATA host aware drive,
4784                          * we communicate via ATA passthrough unless the
4785                          * SAT layer supports ZBC -> ZAC translation.  In
4786                          * that case,
4787                          */
4788                         /*
4789                          * XXX KDM figure out how to detect a host managed
4790                          * SATA drive.
4791                          */
4792                         if (softc->zone_mode == DA_ZONE_NONE) {
4793                                 /*
4794                                  * Note that we don't override the zone
4795                                  * mode or interface if it has already been
4796                                  * set.  This is because it has either been
4797                                  * set as a quirk, or when we probed the
4798                                  * SCSI Block Device Characteristics page,
4799                                  * the zoned field was set.  The latter
4800                                  * means that the SAT layer supports ZBC to
4801                                  * ZAC translation, and we would prefer to
4802                                  * use that if it is available.
4803                                  */
4804                                 if ((ata_params->support3 &
4805                                     ATA_SUPPORT_ZONE_MASK) ==
4806                                     ATA_SUPPORT_ZONE_HOST_AWARE) {
4807                                         softc->zone_mode = DA_ZONE_HOST_AWARE;
4808                                         softc->zone_interface =
4809                                             DA_ZONE_IF_ATA_PASS;
4810                                 } else if ((ata_params->support3 &
4811                                             ATA_SUPPORT_ZONE_MASK) ==
4812                                             ATA_SUPPORT_ZONE_DEV_MANAGED) {
4813                                         softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
4814                                         softc->zone_interface =
4815                                             DA_ZONE_IF_ATA_PASS;
4816                                 }
4817                         }
4818
4819                 } else {
4820                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4821                                         SF_RETRY_UA|SF_NO_PRINT);
4822                         if (error == ERESTART)
4823                                 return;
4824                         else if (error != 0) {
4825                                 if ((done_ccb->ccb_h.status &
4826                                      CAM_DEV_QFRZN) != 0) {
4827                                         /* Don't wedge this device's queue */
4828                                         cam_release_devq(done_ccb->ccb_h.path,
4829                                                          /*relsim_flags*/0,
4830                                                          /*reduction*/0,
4831                                                          /*timeout*/0,
4832                                                          /*getcount_only*/0);
4833                                 }
4834                         }
4835                 }
4836
4837                 free(ata_params, M_SCSIDA);
4838                 if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
4839                  || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
4840                         /*
4841                          * If the ATA IDENTIFY failed, we could be talking
4842                          * to a SCSI drive, although that seems unlikely,
4843                          * since the drive did report that it supported the 
4844                          * ATA Information VPD page.  If the ATA IDENTIFY
4845                          * succeeded, and the SAT layer doesn't support
4846                          * ZBC -> ZAC translation, continue on to get the
4847                          * directory of ATA logs, and complete the rest of
4848                          * the ZAC probe.  If the SAT layer does support
4849                          * ZBC -> ZAC translation, we want to use that,
4850                          * and we'll probe the SCSI Zoned Block Device
4851                          * Characteristics VPD page next.
4852                          */
4853                         if ((error == 0)
4854                          && (softc->flags & DA_FLAG_CAN_ATA_LOG)
4855                          && (softc->zone_interface == DA_ZONE_IF_ATA_PASS))
4856                                 softc->state = DA_STATE_PROBE_ATA_LOGDIR;
4857                         else
4858                                 softc->state = DA_STATE_PROBE_ZONE;
4859                         continue_probe = 1;
4860                 }
4861                 if (continue_probe != 0) {
4862                         xpt_release_ccb(done_ccb);
4863                         xpt_schedule(periph, priority);
4864                         return;
4865                 } else
4866                         daprobedone(periph, done_ccb);
4867                 return;
4868         }
4869         case DA_CCB_PROBE_ATA_LOGDIR:
4870         {
4871                 int error;
4872
4873                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4874                         error = 0;
4875                         softc->valid_logdir_len = 0;
4876                         bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
4877                         softc->valid_logdir_len =
4878                                 csio->dxfer_len - csio->resid;
4879                         if (softc->valid_logdir_len > 0)
4880                                 bcopy(csio->data_ptr, &softc->ata_logdir,
4881                                     min(softc->valid_logdir_len,
4882                                         sizeof(softc->ata_logdir)));
4883                         /*
4884                          * Figure out whether the Identify Device log is
4885                          * supported.  The General Purpose log directory
4886                          * has a header, and lists the number of pages
4887                          * available for each GP log identified by the
4888                          * offset into the list.
4889                          */
4890                         if ((softc->valid_logdir_len >=
4891                             ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
4892                          && (le16dec(softc->ata_logdir.header) == 
4893                              ATA_GP_LOG_DIR_VERSION)
4894                          && (le16dec(&softc->ata_logdir.num_pages[
4895                              (ATA_IDENTIFY_DATA_LOG *
4896                              sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
4897                                 softc->flags |= DA_FLAG_CAN_ATA_IDLOG;
4898                         } else {
4899                                 softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
4900                         }
4901                 } else {
4902                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4903                                         SF_RETRY_UA|SF_NO_PRINT);
4904                         if (error == ERESTART)
4905                                 return;
4906                         else if (error != 0) {
4907                                 /*
4908                                  * If we can't get the ATA log directory,
4909                                  * then ATA logs are effectively not
4910                                  * supported even if the bit is set in the
4911                                  * identify data.
4912                                  */ 
4913                                 softc->flags &= ~(DA_FLAG_CAN_ATA_LOG |
4914                                                   DA_FLAG_CAN_ATA_IDLOG);
4915                                 if ((done_ccb->ccb_h.status &
4916                                      CAM_DEV_QFRZN) != 0) {
4917                                         /* Don't wedge this device's queue */
4918                                         cam_release_devq(done_ccb->ccb_h.path,
4919                                                          /*relsim_flags*/0,
4920                                                          /*reduction*/0,
4921                                                          /*timeout*/0,
4922                                                          /*getcount_only*/0);
4923                                 }
4924                         }
4925                 }
4926
4927                 free(csio->data_ptr, M_SCSIDA);
4928
4929                 if ((error == 0)
4930                  && (softc->flags & DA_FLAG_CAN_ATA_IDLOG)) {
4931                         softc->state = DA_STATE_PROBE_ATA_IDDIR;
4932                         xpt_release_ccb(done_ccb);
4933                         xpt_schedule(periph, priority);
4934                         return;
4935                 } 
4936                 daprobedone(periph, done_ccb);
4937                 return;
4938         }
4939         case DA_CCB_PROBE_ATA_IDDIR:
4940         {
4941                 int error;
4942
4943                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4944                         off_t entries_offset, max_entries;
4945                         error = 0;
4946
4947                         softc->valid_iddir_len = 0;
4948                         bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
4949                         softc->flags &= ~(DA_FLAG_CAN_ATA_SUPCAP |
4950                                           DA_FLAG_CAN_ATA_ZONE);
4951                         softc->valid_iddir_len =
4952                                 csio->dxfer_len - csio->resid;
4953                         if (softc->valid_iddir_len > 0)
4954                                 bcopy(csio->data_ptr, &softc->ata_iddir,
4955                                     min(softc->valid_iddir_len,
4956                                         sizeof(softc->ata_iddir)));
4957
4958                         entries_offset =
4959                             __offsetof(struct ata_identify_log_pages,entries);
4960                         max_entries = softc->valid_iddir_len - entries_offset;
4961                         if ((softc->valid_iddir_len > (entries_offset + 1))
4962                          && (le64dec(softc->ata_iddir.header) ==
4963                              ATA_IDLOG_REVISION)
4964                          && (softc->ata_iddir.entry_count > 0)) {
4965                                 int num_entries, i;
4966
4967                                 num_entries = softc->ata_iddir.entry_count;
4968                                 num_entries = min(num_entries,
4969                                    softc->valid_iddir_len - entries_offset);
4970                                 for (i = 0; i < num_entries &&
4971                                      i < max_entries; i++) {
4972                                         if (softc->ata_iddir.entries[i] ==
4973                                             ATA_IDL_SUP_CAP)
4974                                                 softc->flags |=
4975                                                     DA_FLAG_CAN_ATA_SUPCAP;
4976                                         else if (softc->ata_iddir.entries[i]==
4977                                                  ATA_IDL_ZDI)
4978                                                 softc->flags |=
4979                                                     DA_FLAG_CAN_ATA_ZONE;
4980
4981                                         if ((softc->flags &
4982                                              DA_FLAG_CAN_ATA_SUPCAP)
4983                                          && (softc->flags &
4984                                              DA_FLAG_CAN_ATA_ZONE))
4985                                                 break;
4986                                 }
4987                         }
4988                 } else {
4989                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4990                                         SF_RETRY_UA|SF_NO_PRINT);
4991                         if (error == ERESTART)
4992                                 return;
4993                         else if (error != 0) {
4994                                 /*
4995                                  * If we can't get the ATA Identify Data log
4996                                  * directory, then it effectively isn't
4997                                  * supported even if the ATA Log directory
4998                                  * a non-zero number of pages present for
4999                                  * this log.
5000                                  */
5001                                 softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
5002                                 if ((done_ccb->ccb_h.status &
5003                                      CAM_DEV_QFRZN) != 0) {
5004                                         /* Don't wedge this device's queue */
5005                                         cam_release_devq(done_ccb->ccb_h.path,
5006                                                          /*relsim_flags*/0,
5007                                                          /*reduction*/0,
5008                                                          /*timeout*/0,
5009                                                          /*getcount_only*/0);
5010                                 }
5011                         }
5012                 }
5013
5014                 free(csio->data_ptr, M_SCSIDA);
5015
5016                 if ((error == 0)
5017                  && (softc->flags & DA_FLAG_CAN_ATA_SUPCAP)) {
5018                         softc->state = DA_STATE_PROBE_ATA_SUP;
5019                         xpt_release_ccb(done_ccb);
5020                         xpt_schedule(periph, priority);
5021                         return;
5022                 } 
5023                 daprobedone(periph, done_ccb);
5024                 return;
5025         }
5026         case DA_CCB_PROBE_ATA_SUP:
5027         {
5028                 int error;
5029
5030                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5031                         uint32_t valid_len;
5032                         size_t needed_size;
5033                         struct ata_identify_log_sup_cap *sup_cap;
5034                         error = 0;
5035
5036                         sup_cap = (struct ata_identify_log_sup_cap *)
5037                             csio->data_ptr;
5038                         valid_len = csio->dxfer_len - csio->resid;
5039                         needed_size =
5040                             __offsetof(struct ata_identify_log_sup_cap,
5041                             sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
5042                         if (valid_len >= needed_size) {
5043                                 uint64_t zoned, zac_cap;
5044
5045                                 zoned = le64dec(sup_cap->zoned_cap);
5046                                 if (zoned & ATA_ZONED_VALID) {
5047                                         /*
5048                                          * This should have already been
5049                                          * set, because this is also in the
5050                                          * ATA identify data.
5051                                          */
5052                                         if ((zoned & ATA_ZONED_MASK) ==
5053                                             ATA_SUPPORT_ZONE_HOST_AWARE)
5054                                                 softc->zone_mode =
5055                                                     DA_ZONE_HOST_AWARE;
5056                                         else if ((zoned & ATA_ZONED_MASK) ==
5057                                             ATA_SUPPORT_ZONE_DEV_MANAGED)
5058                                                 softc->zone_mode =
5059                                                     DA_ZONE_DRIVE_MANAGED;
5060                                 }
5061
5062                                 zac_cap = le64dec(sup_cap->sup_zac_cap);
5063                                 if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
5064                                         if (zac_cap & ATA_REPORT_ZONES_SUP)
5065                                                 softc->zone_flags |=
5066                                                     DA_ZONE_FLAG_RZ_SUP;
5067                                         if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
5068                                                 softc->zone_flags |=
5069                                                     DA_ZONE_FLAG_OPEN_SUP;
5070                                         if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
5071                                                 softc->zone_flags |=
5072                                                     DA_ZONE_FLAG_CLOSE_SUP;
5073                                         if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
5074                                                 softc->zone_flags |=
5075                                                     DA_ZONE_FLAG_FINISH_SUP;
5076                                         if (zac_cap & ATA_ND_RWP_SUP)
5077                                                 softc->zone_flags |=
5078                                                     DA_ZONE_FLAG_RWP_SUP;
5079                                 } else {
5080                                         /*
5081                                          * This field was introduced in
5082                                          * ACS-4, r08 on April 28th, 2015.
5083                                          * If the drive firmware was written
5084                                          * to an earlier spec, it won't have
5085                                          * the field.  So, assume all
5086                                          * commands are supported.
5087                                          */ 
5088                                         softc->zone_flags |=
5089                                             DA_ZONE_FLAG_SUP_MASK;
5090                                 }
5091                                             
5092                         }
5093                 } else {
5094                         error = daerror(done_ccb, CAM_RETRY_SELTO,
5095                                         SF_RETRY_UA|SF_NO_PRINT);
5096                         if (error == ERESTART)
5097                                 return;
5098                         else if (error != 0) {
5099                                 /*
5100                                  * If we can't get the ATA Identify Data
5101                                  * Supported Capabilities page, clear the
5102                                  * flag...
5103                                  */
5104                                 softc->flags &= ~DA_FLAG_CAN_ATA_SUPCAP;
5105                                 /*
5106                                  * And clear zone capabilities.
5107                                  */
5108                                 softc->zone_flags &= ~DA_ZONE_FLAG_SUP_MASK;
5109                                 if ((done_ccb->ccb_h.status &
5110                                      CAM_DEV_QFRZN) != 0) {
5111                                         /* Don't wedge this device's queue */
5112                                         cam_release_devq(done_ccb->ccb_h.path,
5113                                                          /*relsim_flags*/0,
5114                                                          /*reduction*/0,
5115                                                          /*timeout*/0,
5116                                                          /*getcount_only*/0);
5117                                 }
5118                         }
5119                 }
5120
5121                 free(csio->data_ptr, M_SCSIDA);
5122
5123                 if ((error == 0)
5124                  && (softc->flags & DA_FLAG_CAN_ATA_ZONE)) {
5125                         softc->state = DA_STATE_PROBE_ATA_ZONE;
5126                         xpt_release_ccb(done_ccb);
5127                         xpt_schedule(periph, priority);
5128                         return;
5129                 } 
5130                 daprobedone(periph, done_ccb);
5131                 return;
5132         }
5133         case DA_CCB_PROBE_ATA_ZONE:
5134         {
5135                 int error;
5136
5137                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5138                         struct ata_zoned_info_log *zi_log;
5139                         uint32_t valid_len;
5140                         size_t needed_size;
5141
5142                         zi_log = (struct ata_zoned_info_log *)csio->data_ptr;
5143
5144                         valid_len = csio->dxfer_len - csio->resid;
5145                         needed_size = __offsetof(struct ata_zoned_info_log,
5146                             version_info) + 1 + sizeof(zi_log->version_info);
5147                         if (valid_len >= needed_size) {
5148                                 uint64_t tmpvar;
5149
5150                                 tmpvar = le64dec(zi_log->zoned_cap);
5151                                 if (tmpvar & ATA_ZDI_CAP_VALID) {
5152                                         if (tmpvar & ATA_ZDI_CAP_URSWRZ)
5153                                                 softc->zone_flags |=
5154                                                     DA_ZONE_FLAG_URSWRZ;
5155                                         else
5156                                                 softc->zone_flags &=
5157                                                     ~DA_ZONE_FLAG_URSWRZ;
5158                                 }
5159                                 tmpvar = le64dec(zi_log->optimal_seq_zones);
5160                                 if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
5161                                         softc->zone_flags |=
5162                                             DA_ZONE_FLAG_OPT_SEQ_SET;
5163                                         softc->optimal_seq_zones = (tmpvar &
5164                                             ATA_ZDI_OPT_SEQ_MASK);
5165                                 } else {
5166                                         softc->zone_flags &=
5167                                             ~DA_ZONE_FLAG_OPT_SEQ_SET;
5168                                         softc->optimal_seq_zones = 0;
5169                                 }
5170
5171                                 tmpvar =le64dec(zi_log->optimal_nonseq_zones);
5172                                 if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
5173                                         softc->zone_flags |=
5174                                             DA_ZONE_FLAG_OPT_NONSEQ_SET;
5175                                         softc->optimal_nonseq_zones =
5176                                             (tmpvar & ATA_ZDI_OPT_NS_MASK);
5177                                 } else {
5178                                         softc->zone_flags &=
5179                                             ~DA_ZONE_FLAG_OPT_NONSEQ_SET;
5180                                         softc->optimal_nonseq_zones = 0;
5181                                 }
5182
5183                                 tmpvar = le64dec(zi_log->max_seq_req_zones);
5184                                 if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
5185                                         softc->zone_flags |=
5186                                             DA_ZONE_FLAG_MAX_SEQ_SET;
5187                                         softc->max_seq_zones =
5188                                             (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
5189                                 } else {
5190                                         softc->zone_flags &=
5191                                             ~DA_ZONE_FLAG_MAX_SEQ_SET;
5192                                         softc->max_seq_zones = 0;
5193                                 }
5194                         }
5195                 } else {
5196                         error = daerror(done_ccb, CAM_RETRY_SELTO,
5197                                         SF_RETRY_UA|SF_NO_PRINT);
5198                         if (error == ERESTART)
5199                                 return;
5200                         else if (error != 0) {
5201                                 softc->flags &= ~DA_FLAG_CAN_ATA_ZONE;
5202                                 softc->flags &= ~DA_ZONE_FLAG_SET_MASK;
5203
5204                                 if ((done_ccb->ccb_h.status &
5205                                      CAM_DEV_QFRZN) != 0) {
5206                                         /* Don't wedge this device's queue */
5207                                         cam_release_devq(done_ccb->ccb_h.path,
5208                                                          /*relsim_flags*/0,
5209                                                          /*reduction*/0,
5210                                                          /*timeout*/0,
5211                                                          /*getcount_only*/0);
5212                                 }
5213                         }
5214         
5215                 }
5216                 free(csio->data_ptr, M_SCSIDA);
5217
5218                 daprobedone(periph, done_ccb);
5219                 return;
5220         }
5221         case DA_CCB_PROBE_ZONE:
5222         {
5223                 int error;
5224
5225                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5226                         uint32_t valid_len;
5227                         size_t needed_len;
5228                         struct scsi_vpd_zoned_bdc *zoned_bdc;
5229
5230                         error = 0;
5231                         zoned_bdc = (struct scsi_vpd_zoned_bdc *)
5232                                 csio->data_ptr;
5233                         valid_len = csio->dxfer_len - csio->resid;
5234                         needed_len = __offsetof(struct scsi_vpd_zoned_bdc,
5235                             max_seq_req_zones) + 1 +
5236                             sizeof(zoned_bdc->max_seq_req_zones);
5237                         if ((valid_len >= needed_len)
5238                          && (scsi_2btoul(zoned_bdc->page_length) >=
5239                              SVPD_ZBDC_PL)) {
5240                                 if (zoned_bdc->flags & SVPD_ZBDC_URSWRZ)
5241                                         softc->zone_flags |=
5242                                             DA_ZONE_FLAG_URSWRZ;
5243                                 else
5244                                         softc->zone_flags &= 
5245                                             ~DA_ZONE_FLAG_URSWRZ;
5246                                 softc->optimal_seq_zones =
5247                                     scsi_4btoul(zoned_bdc->optimal_seq_zones);
5248                                 softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5249                                 softc->optimal_nonseq_zones = scsi_4btoul(
5250                                     zoned_bdc->optimal_nonseq_zones);
5251                                 softc->zone_flags |=
5252                                     DA_ZONE_FLAG_OPT_NONSEQ_SET;
5253                                 softc->max_seq_zones =
5254                                     scsi_4btoul(zoned_bdc->max_seq_req_zones);
5255                                 softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5256                         }
5257                         /*
5258                          * All of the zone commands are mandatory for SCSI
5259                          * devices.
5260                          *
5261                          * XXX KDM this is valid as of September 2015.
5262                          * Re-check this assumption once the SAT spec is
5263                          * updated to support SCSI ZBC to ATA ZAC mapping.
5264                          * Since ATA allows zone commands to be reported
5265                          * as supported or not, this may not necessarily
5266                          * be true for an ATA device behind a SAT (SCSI to
5267                          * ATA Translation) layer.
5268                          */
5269                         softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5270                 } else {
5271                         error = daerror(done_ccb, CAM_RETRY_SELTO,
5272                                         SF_RETRY_UA|SF_NO_PRINT);
5273                         if (error == ERESTART)
5274                                 return;
5275                         else if (error != 0) {
5276                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5277                                         /* Don't wedge this device's queue */
5278                                         cam_release_devq(done_ccb->ccb_h.path,
5279                                                          /*relsim_flags*/0,
5280                                                          /*reduction*/0,
5281                                                          /*timeout*/0,
5282                                                          /*getcount_only*/0);
5283                                 }
5284                         }
5285                 }
5286                 daprobedone(periph, done_ccb);
5287                 return;
5288         }
5289         case DA_CCB_DUMP:
5290                 /* No-op.  We're polling */
5291                 return;
5292         case DA_CCB_TUR:
5293         {
5294                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5295
5296                         if (daerror(done_ccb, CAM_RETRY_SELTO,
5297                             SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
5298                             ERESTART)
5299                                 return;
5300                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5301                                 cam_release_devq(done_ccb->ccb_h.path,
5302                                                  /*relsim_flags*/0,
5303                                                  /*reduction*/0,
5304                                                  /*timeout*/0,
5305                                                  /*getcount_only*/0);
5306                 }
5307                 xpt_release_ccb(done_ccb);
5308                 cam_periph_release_locked(periph);
5309                 return;
5310         }
5311         default:
5312                 break;
5313         }
5314         xpt_release_ccb(done_ccb);
5315 }
5316
5317 static void
5318 dareprobe(struct cam_periph *periph)
5319 {
5320         struct da_softc   *softc;
5321         cam_status status;
5322
5323         softc = (struct da_softc *)periph->softc;
5324
5325         /* Probe in progress; don't interfere. */
5326         if (softc->state != DA_STATE_NORMAL)
5327                 return;
5328
5329         status = cam_periph_acquire(periph);
5330         KASSERT(status == CAM_REQ_CMP,
5331             ("dareprobe: cam_periph_acquire failed"));
5332
5333         if (softc->flags & DA_FLAG_CAN_RC16)
5334                 softc->state = DA_STATE_PROBE_RC16;
5335         else
5336                 softc->state = DA_STATE_PROBE_RC;
5337
5338         xpt_schedule(periph, CAM_PRIORITY_DEV);
5339 }
5340
5341 static int
5342 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
5343 {
5344         struct da_softc   *softc;
5345         struct cam_periph *periph;
5346         int error, error_code, sense_key, asc, ascq;
5347
5348 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5349         if (ccb->csio.bio != NULL)
5350                 biotrack(ccb->csio.bio, __func__);
5351 #endif
5352
5353         periph = xpt_path_periph(ccb->ccb_h.path);
5354         softc = (struct da_softc *)periph->softc;
5355
5356         /*
5357          * Automatically detect devices that do not support
5358          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
5359          */
5360         error = 0;
5361         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
5362                 error = cmd6workaround(ccb);
5363         } else if (scsi_extract_sense_ccb(ccb,
5364             &error_code, &sense_key, &asc, &ascq)) {
5365                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
5366                         error = cmd6workaround(ccb);
5367                 /*
5368                  * If the target replied with CAPACITY DATA HAS CHANGED UA,
5369                  * query the capacity and notify upper layers.
5370                  */
5371                 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5372                     asc == 0x2A && ascq == 0x09) {
5373                         xpt_print(periph->path, "Capacity data has changed\n");
5374                         softc->flags &= ~DA_FLAG_PROBED;
5375                         dareprobe(periph);
5376                         sense_flags |= SF_NO_PRINT;
5377                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5378                     asc == 0x28 && ascq == 0x00) {
5379                         softc->flags &= ~DA_FLAG_PROBED;
5380                         disk_media_changed(softc->disk, M_NOWAIT);
5381                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5382                     asc == 0x3F && ascq == 0x03) {
5383                         xpt_print(periph->path, "INQUIRY data has changed\n");
5384                         softc->flags &= ~DA_FLAG_PROBED;
5385                         dareprobe(periph);
5386                         sense_flags |= SF_NO_PRINT;
5387                 } else if (sense_key == SSD_KEY_NOT_READY &&
5388                     asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
5389                         softc->flags |= DA_FLAG_PACK_INVALID;
5390                         disk_media_gone(softc->disk, M_NOWAIT);
5391                 }
5392         }
5393         if (error == ERESTART)
5394                 return (ERESTART);
5395
5396 #ifdef CAM_IO_STATS
5397         switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
5398         case CAM_CMD_TIMEOUT:
5399                 softc->timeouts++;
5400                 break;
5401         case CAM_REQ_ABORTED:
5402         case CAM_REQ_CMP_ERR:
5403         case CAM_REQ_TERMIO:
5404         case CAM_UNREC_HBA_ERROR:
5405         case CAM_DATA_RUN_ERR:
5406                 softc->errors++;
5407                 break;
5408         default:
5409                 break;
5410         }
5411 #endif
5412
5413         /*
5414          * XXX
5415          * Until we have a better way of doing pack validation,
5416          * don't treat UAs as errors.
5417          */
5418         sense_flags |= SF_RETRY_UA;
5419
5420         if (softc->quirks & DA_Q_RETRY_BUSY)
5421                 sense_flags |= SF_RETRY_BUSY;
5422         return(cam_periph_error(ccb, cam_flags, sense_flags,
5423                                 &softc->saved_ccb));
5424 }
5425
5426 static void
5427 damediapoll(void *arg)
5428 {
5429         struct cam_periph *periph = arg;
5430         struct da_softc *softc = periph->softc;
5431
5432         if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
5433             LIST_EMPTY(&softc->pending_ccbs)) {
5434                 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
5435                         cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
5436                         daschedule(periph);
5437                 }
5438         }
5439         /* Queue us up again */
5440         if (da_poll_period != 0)
5441                 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
5442 }
5443
5444 static void
5445 daprevent(struct cam_periph *periph, int action)
5446 {
5447         struct  da_softc *softc;
5448         union   ccb *ccb;               
5449         int     error;
5450                 
5451         softc = (struct da_softc *)periph->softc;
5452
5453         if (((action == PR_ALLOW)
5454           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
5455          || ((action == PR_PREVENT)
5456           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
5457                 return;
5458         }
5459
5460         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5461
5462         scsi_prevent(&ccb->csio,
5463                      /*retries*/1,
5464                      /*cbcfp*/dadone,
5465                      MSG_SIMPLE_Q_TAG,
5466                      action,
5467                      SSD_FULL_SIZE,
5468                      5000);
5469
5470         error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
5471             SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
5472
5473         if (error == 0) {
5474                 if (action == PR_ALLOW)
5475                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
5476                 else
5477                         softc->flags |= DA_FLAG_PACK_LOCKED;
5478         }
5479
5480         xpt_release_ccb(ccb);
5481 }
5482
5483 static void
5484 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
5485           struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
5486 {
5487         struct ccb_calc_geometry ccg;
5488         struct da_softc *softc;
5489         struct disk_params *dp;
5490         u_int lbppbe, lalba;
5491         int error;
5492
5493         softc = (struct da_softc *)periph->softc;
5494
5495         dp = &softc->params;
5496         dp->secsize = block_len;
5497         dp->sectors = maxsector + 1;
5498         if (rcaplong != NULL) {
5499                 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
5500                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
5501                 lalba &= SRC16_LALBA_A;
5502         } else {
5503                 lbppbe = 0;
5504                 lalba = 0;
5505         }
5506
5507         if (lbppbe > 0) {
5508                 dp->stripesize = block_len << lbppbe;
5509                 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
5510                     dp->stripesize;
5511         } else if (softc->quirks & DA_Q_4K) {
5512                 dp->stripesize = 4096;
5513                 dp->stripeoffset = 0;
5514         } else if (softc->unmap_gran != 0) {
5515                 dp->stripesize = block_len * softc->unmap_gran;
5516                 dp->stripeoffset = (dp->stripesize - block_len *
5517                     softc->unmap_gran_align) % dp->stripesize;
5518         } else {
5519                 dp->stripesize = 0;
5520                 dp->stripeoffset = 0;
5521         }
5522         /*
5523          * Have the controller provide us with a geometry
5524          * for this disk.  The only time the geometry
5525          * matters is when we boot and the controller
5526          * is the only one knowledgeable enough to come
5527          * up with something that will make this a bootable
5528          * device.
5529          */
5530         xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5531         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
5532         ccg.block_size = dp->secsize;
5533         ccg.volume_size = dp->sectors;
5534         ccg.heads = 0;
5535         ccg.secs_per_track = 0;
5536         ccg.cylinders = 0;
5537         xpt_action((union ccb*)&ccg);
5538         if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5539                 /*
5540                  * We don't know what went wrong here- but just pick
5541                  * a geometry so we don't have nasty things like divide
5542                  * by zero.
5543                  */
5544                 dp->heads = 255;
5545                 dp->secs_per_track = 255;
5546                 dp->cylinders = dp->sectors / (255 * 255);
5547                 if (dp->cylinders == 0) {
5548                         dp->cylinders = 1;
5549                 }
5550         } else {
5551                 dp->heads = ccg.heads;
5552                 dp->secs_per_track = ccg.secs_per_track;
5553                 dp->cylinders = ccg.cylinders;
5554         }
5555
5556         /*
5557          * If the user supplied a read capacity buffer, and if it is
5558          * different than the previous buffer, update the data in the EDT.
5559          * If it's the same, we don't bother.  This avoids sending an
5560          * update every time someone opens this device.
5561          */
5562         if ((rcaplong != NULL)
5563          && (bcmp(rcaplong, &softc->rcaplong,
5564                   min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
5565                 struct ccb_dev_advinfo cdai;
5566
5567                 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5568                 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
5569                 cdai.buftype = CDAI_TYPE_RCAPLONG;
5570                 cdai.flags = CDAI_FLAG_STORE;
5571                 cdai.bufsiz = rcap_len;
5572                 cdai.buf = (uint8_t *)rcaplong;
5573                 xpt_action((union ccb *)&cdai);
5574                 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
5575                         cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
5576                 if (cdai.ccb_h.status != CAM_REQ_CMP) {
5577                         xpt_print(periph->path, "%s: failed to set read "
5578                                   "capacity advinfo\n", __func__);
5579                         /* Use cam_error_print() to decode the status */
5580                         cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
5581                                         CAM_EPF_ALL);
5582                 } else {
5583                         bcopy(rcaplong, &softc->rcaplong,
5584                               min(sizeof(softc->rcaplong), rcap_len));
5585                 }
5586         }
5587
5588         softc->disk->d_sectorsize = softc->params.secsize;
5589         softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
5590         softc->disk->d_stripesize = softc->params.stripesize;
5591         softc->disk->d_stripeoffset = softc->params.stripeoffset;
5592         /* XXX: these are not actually "firmware" values, so they may be wrong */
5593         softc->disk->d_fwsectors = softc->params.secs_per_track;
5594         softc->disk->d_fwheads = softc->params.heads;
5595         softc->disk->d_devstat->block_size = softc->params.secsize;
5596         softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
5597
5598         error = disk_resize(softc->disk, M_NOWAIT);
5599         if (error != 0)
5600                 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
5601 }
5602
5603 static void
5604 dasendorderedtag(void *arg)
5605 {
5606         struct da_softc *softc = arg;
5607
5608         if (da_send_ordered) {
5609                 if (!LIST_EMPTY(&softc->pending_ccbs)) {
5610                         if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
5611                                 softc->flags |= DA_FLAG_NEED_OTAG;
5612                         softc->flags &= ~DA_FLAG_WAS_OTAG;
5613                 }
5614         }
5615         /* Queue us up again */
5616         callout_reset(&softc->sendordered_c,
5617             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
5618             dasendorderedtag, softc);
5619 }
5620
5621 /*
5622  * Step through all DA peripheral drivers, and if the device is still open,
5623  * sync the disk cache to physical media.
5624  */
5625 static void
5626 dashutdown(void * arg, int howto)
5627 {
5628         struct cam_periph *periph;
5629         struct da_softc *softc;
5630         union ccb *ccb;
5631         int error;
5632
5633         CAM_PERIPH_FOREACH(periph, &dadriver) {
5634                 softc = (struct da_softc *)periph->softc;
5635                 if (SCHEDULER_STOPPED()) {
5636                         /* If we paniced with the lock held, do not recurse. */
5637                         if (!cam_periph_owned(periph) &&
5638                             (softc->flags & DA_FLAG_OPEN)) {
5639                                 dadump(softc->disk, NULL, 0, 0, 0);
5640                         }
5641                         continue;
5642                 }
5643                 cam_periph_lock(periph);
5644
5645                 /*
5646                  * We only sync the cache if the drive is still open, and
5647                  * if the drive is capable of it..
5648                  */
5649                 if (((softc->flags & DA_FLAG_OPEN) == 0)
5650                  || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
5651                         cam_periph_unlock(periph);
5652                         continue;
5653                 }
5654
5655                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5656                 scsi_synchronize_cache(&ccb->csio,
5657                                        /*retries*/0,
5658                                        /*cbfcnp*/dadone,
5659                                        MSG_SIMPLE_Q_TAG,
5660                                        /*begin_lba*/0, /* whole disk */
5661                                        /*lb_count*/0,
5662                                        SSD_FULL_SIZE,
5663                                        60 * 60 * 1000);
5664
5665                 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
5666                     /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
5667                     softc->disk->d_devstat);
5668                 if (error != 0)
5669                         xpt_print(periph->path, "Synchronize cache failed\n");
5670                 xpt_release_ccb(ccb);
5671                 cam_periph_unlock(periph);
5672         }
5673 }
5674
5675 #else /* !_KERNEL */
5676
5677 /*
5678  * XXX These are only left out of the kernel build to silence warnings.  If,
5679  * for some reason these functions are used in the kernel, the ifdefs should
5680  * be moved so they are included both in the kernel and userland.
5681  */
5682 void
5683 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
5684                  void (*cbfcnp)(struct cam_periph *, union ccb *),
5685                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
5686                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
5687                  u_int32_t timeout)
5688 {
5689         struct scsi_format_unit *scsi_cmd;
5690
5691         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
5692         scsi_cmd->opcode = FORMAT_UNIT;
5693         scsi_cmd->byte2 = byte2;
5694         scsi_ulto2b(ileave, scsi_cmd->interleave);
5695
5696         cam_fill_csio(csio,
5697                       retries,
5698                       cbfcnp,
5699                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5700                       tag_action,
5701                       data_ptr,
5702                       dxfer_len,
5703                       sense_len,
5704                       sizeof(*scsi_cmd),
5705                       timeout);
5706 }
5707
5708 void
5709 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
5710                   void (*cbfcnp)(struct cam_periph *, union ccb *),
5711                   uint8_t tag_action, uint8_t list_format,
5712                   uint32_t addr_desc_index, uint8_t *data_ptr,
5713                   uint32_t dxfer_len, int minimum_cmd_size, 
5714                   uint8_t sense_len, uint32_t timeout)
5715 {
5716         uint8_t cdb_len;
5717
5718         /*
5719          * These conditions allow using the 10 byte command.  Otherwise we
5720          * need to use the 12 byte command.
5721          */
5722         if ((minimum_cmd_size <= 10)
5723          && (addr_desc_index == 0) 
5724          && (dxfer_len <= SRDD10_MAX_LENGTH)) {
5725                 struct scsi_read_defect_data_10 *cdb10;
5726
5727                 cdb10 = (struct scsi_read_defect_data_10 *)
5728                         &csio->cdb_io.cdb_bytes;
5729
5730                 cdb_len = sizeof(*cdb10);
5731                 bzero(cdb10, cdb_len);
5732                 cdb10->opcode = READ_DEFECT_DATA_10;
5733                 cdb10->format = list_format;
5734                 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
5735         } else {
5736                 struct scsi_read_defect_data_12 *cdb12;
5737
5738                 cdb12 = (struct scsi_read_defect_data_12 *)
5739                         &csio->cdb_io.cdb_bytes;
5740
5741                 cdb_len = sizeof(*cdb12);
5742                 bzero(cdb12, cdb_len);
5743                 cdb12->opcode = READ_DEFECT_DATA_12;
5744                 cdb12->format = list_format;
5745                 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
5746                 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
5747         }
5748
5749         cam_fill_csio(csio,
5750                       retries,
5751                       cbfcnp,
5752                       /*flags*/ CAM_DIR_IN,
5753                       tag_action,
5754                       data_ptr,
5755                       dxfer_len,
5756                       sense_len,
5757                       cdb_len,
5758                       timeout);
5759 }
5760
5761 void
5762 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
5763               void (*cbfcnp)(struct cam_periph *, union ccb *),
5764               u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
5765               u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
5766               u_int32_t timeout)
5767 {
5768         struct scsi_sanitize *scsi_cmd;
5769
5770         scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
5771         scsi_cmd->opcode = SANITIZE;
5772         scsi_cmd->byte2 = byte2;
5773         scsi_cmd->control = control;
5774         scsi_ulto2b(dxfer_len, scsi_cmd->length);
5775
5776         cam_fill_csio(csio,
5777                       retries,
5778                       cbfcnp,
5779                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5780                       tag_action,
5781                       data_ptr,
5782                       dxfer_len,
5783                       sense_len,
5784                       sizeof(*scsi_cmd),
5785                       timeout);
5786 }
5787
5788 #endif /* _KERNEL */
5789
5790 void
5791 scsi_zbc_out(struct ccb_scsiio *csio, uint32_t retries, 
5792              void (*cbfcnp)(struct cam_periph *, union ccb *),
5793              uint8_t tag_action, uint8_t service_action, uint64_t zone_id,
5794              uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
5795              uint8_t sense_len, uint32_t timeout)
5796 {
5797         struct scsi_zbc_out *scsi_cmd;
5798
5799         scsi_cmd = (struct scsi_zbc_out *)&csio->cdb_io.cdb_bytes;
5800         scsi_cmd->opcode = ZBC_OUT;
5801         scsi_cmd->service_action = service_action;
5802         scsi_u64to8b(zone_id, scsi_cmd->zone_id);
5803         scsi_cmd->zone_flags = zone_flags;
5804
5805         cam_fill_csio(csio,
5806                       retries,
5807                       cbfcnp,
5808                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5809                       tag_action,
5810                       data_ptr,
5811                       dxfer_len,
5812                       sense_len,
5813                       sizeof(*scsi_cmd),
5814                       timeout);
5815 }
5816
5817 void
5818 scsi_zbc_in(struct ccb_scsiio *csio, uint32_t retries, 
5819             void (*cbfcnp)(struct cam_periph *, union ccb *),
5820             uint8_t tag_action, uint8_t service_action, uint64_t zone_start_lba,
5821             uint8_t zone_options, uint8_t *data_ptr, uint32_t dxfer_len,
5822             uint8_t sense_len, uint32_t timeout)
5823 {
5824         struct scsi_zbc_in *scsi_cmd;
5825
5826         scsi_cmd = (struct scsi_zbc_in *)&csio->cdb_io.cdb_bytes;
5827         scsi_cmd->opcode = ZBC_IN;
5828         scsi_cmd->service_action = service_action;
5829         scsi_ulto4b(dxfer_len, scsi_cmd->length);
5830         scsi_u64to8b(zone_start_lba, scsi_cmd->zone_start_lba);
5831         scsi_cmd->zone_options = zone_options;
5832
5833         cam_fill_csio(csio,
5834                       retries,
5835                       cbfcnp,
5836                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_IN : CAM_DIR_NONE,
5837                       tag_action,
5838                       data_ptr,
5839                       dxfer_len,
5840                       sense_len,
5841                       sizeof(*scsi_cmd),
5842                       timeout);
5843
5844 }
5845
5846 int
5847 scsi_ata_zac_mgmt_out(struct ccb_scsiio *csio, uint32_t retries, 
5848                       void (*cbfcnp)(struct cam_periph *, union ccb *),
5849                       uint8_t tag_action, int use_ncq,
5850                       uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
5851                       uint8_t *data_ptr, uint32_t dxfer_len,
5852                       uint8_t *cdb_storage, size_t cdb_storage_len,
5853                       uint8_t sense_len, uint32_t timeout)
5854 {
5855         uint8_t command_out, protocol, ata_flags;
5856         uint16_t features_out;
5857         uint32_t sectors_out, auxiliary;
5858         int retval;
5859
5860         retval = 0;
5861
5862         if (use_ncq == 0) {
5863                 command_out = ATA_ZAC_MANAGEMENT_OUT;
5864                 features_out = (zm_action & 0xf) | (zone_flags << 8);
5865                 ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
5866                 if (dxfer_len == 0) {
5867                         protocol = AP_PROTO_NON_DATA;
5868                         ata_flags |= AP_FLAG_TLEN_NO_DATA;
5869                         sectors_out = 0;
5870                 } else {
5871                         protocol = AP_PROTO_DMA;
5872                         ata_flags |= AP_FLAG_TLEN_SECT_CNT |
5873                                      AP_FLAG_TDIR_TO_DEV;
5874                         sectors_out = ((dxfer_len >> 9) & 0xffff);
5875                 }
5876                 auxiliary = 0;
5877         } else {
5878                 ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
5879                 if (dxfer_len == 0) {
5880                         command_out = ATA_NCQ_NON_DATA;
5881                         features_out = ATA_NCQ_ZAC_MGMT_OUT;
5882                         /*
5883                          * We're assuming the SCSI to ATA translation layer
5884                          * will set the NCQ tag number in the tag field.
5885                          * That isn't clear from the SAT-4 spec (as of rev 05).
5886                          */
5887                         sectors_out = 0;
5888                         ata_flags |= AP_FLAG_TLEN_NO_DATA;
5889                 } else {
5890                         command_out = ATA_SEND_FPDMA_QUEUED;
5891                         /*
5892                          * Note that we're defaulting to normal priority,
5893                          * and assuming that the SCSI to ATA translation
5894                          * layer will insert the NCQ tag number in the tag
5895                          * field.  That isn't clear in the SAT-4 spec (as
5896                          * of rev 05).
5897                          */
5898                         sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
5899
5900                         ata_flags |= AP_FLAG_TLEN_FEAT |
5901                                      AP_FLAG_TDIR_TO_DEV;
5902
5903                         /*
5904                          * For SEND FPDMA QUEUED, the transfer length is
5905                          * encoded in the FEATURE register, and 0 means
5906                          * that 65536 512 byte blocks are to be tranferred.
5907                          * In practice, it seems unlikely that we'll see
5908                          * a transfer that large, and it may confuse the
5909                          * the SAT layer, because generally that means that
5910                          * 0 bytes should be transferred.
5911                          */
5912                         if (dxfer_len == (65536 * 512)) {
5913                                 features_out = 0;
5914                         } else if (dxfer_len <= (65535 * 512)) {
5915                                 features_out = ((dxfer_len >> 9) & 0xffff);
5916                         } else {
5917                                 /* The transfer is too big. */
5918                                 retval = 1;
5919                                 goto bailout;
5920                         }
5921
5922                 }
5923
5924                 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
5925                 protocol = AP_PROTO_FPDMA;
5926         }
5927
5928         protocol |= AP_EXTEND;
5929
5930         retval = scsi_ata_pass(csio,
5931             retries,
5932             cbfcnp,
5933             /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5934             tag_action,
5935             /*protocol*/ protocol,
5936             /*ata_flags*/ ata_flags,
5937             /*features*/ features_out,
5938             /*sector_count*/ sectors_out,
5939             /*lba*/ zone_id,
5940             /*command*/ command_out,
5941             /*device*/ 0,
5942             /*icc*/ 0,
5943             /*auxiliary*/ auxiliary,
5944             /*control*/ 0,
5945             /*data_ptr*/ data_ptr,
5946             /*dxfer_len*/ dxfer_len,
5947             /*cdb_storage*/ cdb_storage,
5948             /*cdb_storage_len*/ cdb_storage_len,
5949             /*minimum_cmd_size*/ 0,
5950             /*sense_len*/ SSD_FULL_SIZE,
5951             /*timeout*/ timeout);
5952
5953 bailout:
5954
5955         return (retval);
5956 }
5957
5958 int
5959 scsi_ata_zac_mgmt_in(struct ccb_scsiio *csio, uint32_t retries, 
5960                      void (*cbfcnp)(struct cam_periph *, union ccb *),
5961                      uint8_t tag_action, int use_ncq,
5962                      uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
5963                      uint8_t *data_ptr, uint32_t dxfer_len,
5964                      uint8_t *cdb_storage, size_t cdb_storage_len,
5965                      uint8_t sense_len, uint32_t timeout)
5966 {
5967         uint8_t command_out, protocol;
5968         uint16_t features_out, sectors_out;
5969         uint32_t auxiliary;
5970         int ata_flags;
5971         int retval;
5972
5973         retval = 0;
5974         ata_flags = AP_FLAG_TDIR_FROM_DEV | AP_FLAG_BYT_BLOK_BLOCKS;
5975
5976         if (use_ncq == 0) {
5977                 command_out = ATA_ZAC_MANAGEMENT_IN;
5978                 /* XXX KDM put a macro here */
5979                 features_out = (zm_action & 0xf) | (zone_flags << 8);
5980                 sectors_out = dxfer_len >> 9; /* XXX KDM macro */
5981                 protocol = AP_PROTO_DMA;
5982                 ata_flags |= AP_FLAG_TLEN_SECT_CNT;
5983                 auxiliary = 0;
5984         } else {
5985                 ata_flags |= AP_FLAG_TLEN_FEAT;
5986
5987                 command_out = ATA_RECV_FPDMA_QUEUED;
5988                 sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
5989
5990                 /*
5991                  * For RECEIVE FPDMA QUEUED, the transfer length is
5992                  * encoded in the FEATURE register, and 0 means
5993                  * that 65536 512 byte blocks are to be tranferred.
5994                  * In practice, it seems unlikely that we'll see
5995                  * a transfer that large, and it may confuse the
5996                  * the SAT layer, because generally that means that
5997                  * 0 bytes should be transferred.
5998                  */
5999                 if (dxfer_len == (65536 * 512)) {
6000                         features_out = 0;
6001                 } else if (dxfer_len <= (65535 * 512)) {
6002                         features_out = ((dxfer_len >> 9) & 0xffff);
6003                 } else {
6004                         /* The transfer is too big. */
6005                         retval = 1;
6006                         goto bailout;
6007                 }
6008                 auxiliary = (zm_action & 0xf) | (zone_flags << 8),
6009                 protocol = AP_PROTO_FPDMA;
6010         }
6011
6012         protocol |= AP_EXTEND;
6013
6014         retval = scsi_ata_pass(csio,
6015             retries,
6016             cbfcnp,
6017             /*flags*/ CAM_DIR_IN,
6018             tag_action,
6019             /*protocol*/ protocol,
6020             /*ata_flags*/ ata_flags,
6021             /*features*/ features_out,
6022             /*sector_count*/ sectors_out,
6023             /*lba*/ zone_id,
6024             /*command*/ command_out,
6025             /*device*/ 0,
6026             /*icc*/ 0,
6027             /*auxiliary*/ auxiliary,
6028             /*control*/ 0,
6029             /*data_ptr*/ data_ptr,
6030             /*dxfer_len*/ (dxfer_len >> 9) * 512, /* XXX KDM */
6031             /*cdb_storage*/ cdb_storage,
6032             /*cdb_storage_len*/ cdb_storage_len,
6033             /*minimum_cmd_size*/ 0,
6034             /*sense_len*/ SSD_FULL_SIZE,
6035             /*timeout*/ timeout);
6036
6037 bailout:
6038         return (retval);
6039 }