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