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