]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/scsi/scsi_da.c
Merge r320600 from stable/11 into releng/11.1:
[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         }
2537         cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2538             sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2539         strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2540         cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2541             cgd->inq_data.product, sizeof(cgd->inq_data.product),
2542             sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2543         softc->disk->d_hba_vendor = cpi.hba_vendor;
2544         softc->disk->d_hba_device = cpi.hba_device;
2545         softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2546         softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2547
2548         /*
2549          * Acquire a reference to the periph before we register with GEOM.
2550          * We'll release this reference once GEOM calls us back (via
2551          * dadiskgonecb()) telling us that our provider has been freed.
2552          */
2553         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2554                 xpt_print(periph->path, "%s: lost periph during "
2555                           "registration!\n", __func__);
2556                 cam_periph_lock(periph);
2557                 return (CAM_REQ_CMP_ERR);
2558         }
2559
2560         disk_create(softc->disk, DISK_VERSION);
2561         cam_periph_lock(periph);
2562
2563         /*
2564          * Add async callbacks for events of interest.
2565          * I don't bother checking if this fails as,
2566          * in most cases, the system will function just
2567          * fine without them and the only alternative
2568          * would be to not attach the device on failure.
2569          */
2570         xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2571             AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
2572             AC_INQ_CHANGED, daasync, periph, periph->path);
2573
2574         /*
2575          * Emit an attribute changed notification just in case 
2576          * physical path information arrived before our async
2577          * event handler was registered, but after anyone attaching
2578          * to our disk device polled it.
2579          */
2580         disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2581
2582         /*
2583          * Schedule a periodic media polling events.
2584          */
2585         callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2586         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2587             (cgd->inq_flags & SID_AEN) == 0 &&
2588             da_poll_period != 0)
2589                 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2590                     damediapoll, periph);
2591
2592         xpt_schedule(periph, CAM_PRIORITY_DEV);
2593
2594         return(CAM_REQ_CMP);
2595 }
2596
2597 static int
2598 da_zone_bio_to_scsi(int disk_zone_cmd)
2599 {
2600         switch (disk_zone_cmd) {
2601         case DISK_ZONE_OPEN:
2602                 return ZBC_OUT_SA_OPEN;
2603         case DISK_ZONE_CLOSE:
2604                 return ZBC_OUT_SA_CLOSE;
2605         case DISK_ZONE_FINISH:
2606                 return ZBC_OUT_SA_FINISH;
2607         case DISK_ZONE_RWP:
2608                 return ZBC_OUT_SA_RWP;
2609         }
2610
2611         return -1;
2612 }
2613
2614 static int
2615 da_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
2616             int *queue_ccb)
2617 {
2618         struct da_softc *softc;
2619         int error;
2620
2621         error = 0;
2622
2623         if (bp->bio_cmd != BIO_ZONE) {
2624                 error = EINVAL;
2625                 goto bailout;
2626         }
2627
2628         softc = periph->softc;
2629
2630         switch (bp->bio_zone.zone_cmd) {
2631         case DISK_ZONE_OPEN:
2632         case DISK_ZONE_CLOSE:
2633         case DISK_ZONE_FINISH:
2634         case DISK_ZONE_RWP: {
2635                 int zone_flags;
2636                 int zone_sa;
2637                 uint64_t lba;
2638
2639                 zone_sa = da_zone_bio_to_scsi(bp->bio_zone.zone_cmd);
2640                 if (zone_sa == -1) {
2641                         xpt_print(periph->path, "Cannot translate zone "
2642                             "cmd %#x to SCSI\n", bp->bio_zone.zone_cmd);
2643                         error = EINVAL;
2644                         goto bailout;
2645                 }
2646
2647                 zone_flags = 0;
2648                 lba = bp->bio_zone.zone_params.rwp.id;
2649
2650                 if (bp->bio_zone.zone_params.rwp.flags &
2651                     DISK_ZONE_RWP_FLAG_ALL)
2652                         zone_flags |= ZBC_OUT_ALL;
2653
2654                 if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
2655                         scsi_zbc_out(&ccb->csio,
2656                                      /*retries*/ da_retry_count,
2657                                      /*cbfcnp*/ dadone,
2658                                      /*tag_action*/ MSG_SIMPLE_Q_TAG,
2659                                      /*service_action*/ zone_sa,
2660                                      /*zone_id*/ lba,
2661                                      /*zone_flags*/ zone_flags,
2662                                      /*data_ptr*/ NULL,
2663                                      /*dxfer_len*/ 0,
2664                                      /*sense_len*/ SSD_FULL_SIZE,
2665                                      /*timeout*/ da_default_timeout * 1000);
2666                 } else {
2667                         /*
2668                          * Note that in this case, even though we can
2669                          * technically use NCQ, we don't bother for several
2670                          * reasons:
2671                          * 1. It hasn't been tested on a SAT layer that
2672                          *    supports it.  This is new as of SAT-4.
2673                          * 2. Even when there is a SAT layer that supports
2674                          *    it, that SAT layer will also probably support
2675                          *    ZBC -> ZAC translation, since they are both
2676                          *    in the SAT-4 spec.
2677                          * 3. Translation will likely be preferable to ATA
2678                          *    passthrough.  LSI / Avago at least single
2679                          *    steps ATA passthrough commands in the HBA,
2680                          *    regardless of protocol, so unless that
2681                          *    changes, there is a performance penalty for
2682                          *    doing ATA passthrough no matter whether
2683                          *    you're using NCQ/FPDMA, DMA or PIO.
2684                          * 4. It requires a 32-byte CDB, which at least at
2685                          *    this point in CAM requires a CDB pointer, which
2686                          *    would require us to allocate an additional bit
2687                          *    of storage separate from the CCB.
2688                          */
2689                         error = scsi_ata_zac_mgmt_out(&ccb->csio,
2690                             /*retries*/ da_retry_count,
2691                             /*cbfcnp*/ dadone,
2692                             /*tag_action*/ MSG_SIMPLE_Q_TAG,
2693                             /*use_ncq*/ 0,
2694                             /*zm_action*/ zone_sa,
2695                             /*zone_id*/ lba,
2696                             /*zone_flags*/ zone_flags,
2697                             /*data_ptr*/ NULL,
2698                             /*dxfer_len*/ 0,
2699                             /*cdb_storage*/ NULL,
2700                             /*cdb_storage_len*/ 0,
2701                             /*sense_len*/ SSD_FULL_SIZE,
2702                             /*timeout*/ da_default_timeout * 1000);
2703                         if (error != 0) {
2704                                 error = EINVAL;
2705                                 xpt_print(periph->path,
2706                                     "scsi_ata_zac_mgmt_out() returned an "
2707                                     "error!");
2708                                 goto bailout;
2709                         }
2710                 }
2711                 *queue_ccb = 1;
2712
2713                 break;
2714         }
2715         case DISK_ZONE_REPORT_ZONES: {
2716                 uint8_t *rz_ptr;
2717                 uint32_t num_entries, alloc_size;
2718                 struct disk_zone_report *rep;
2719
2720                 rep = &bp->bio_zone.zone_params.report;
2721
2722                 num_entries = rep->entries_allocated;
2723                 if (num_entries == 0) {
2724                         xpt_print(periph->path, "No entries allocated for "
2725                             "Report Zones request\n");
2726                         error = EINVAL;
2727                         goto bailout;
2728                 }
2729                 alloc_size = sizeof(struct scsi_report_zones_hdr) +
2730                     (sizeof(struct scsi_report_zones_desc) * num_entries);
2731                 alloc_size = min(alloc_size, softc->disk->d_maxsize);
2732                 rz_ptr = malloc(alloc_size, M_SCSIDA, M_NOWAIT | M_ZERO);
2733                 if (rz_ptr == NULL) {
2734                         xpt_print(periph->path, "Unable to allocate memory "
2735                            "for Report Zones request\n");
2736                         error = ENOMEM;
2737                         goto bailout;
2738                 }
2739                 
2740                 if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
2741                         scsi_zbc_in(&ccb->csio,
2742                                     /*retries*/ da_retry_count,
2743                                     /*cbcfnp*/ dadone,
2744                                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
2745                                     /*service_action*/ ZBC_IN_SA_REPORT_ZONES,
2746                                     /*zone_start_lba*/ rep->starting_id,
2747                                     /*zone_options*/ rep->rep_options,
2748                                     /*data_ptr*/ rz_ptr,
2749                                     /*dxfer_len*/ alloc_size,
2750                                     /*sense_len*/ SSD_FULL_SIZE,
2751                                     /*timeout*/ da_default_timeout * 1000);
2752                 } else {
2753                         /*
2754                          * Note that in this case, even though we can
2755                          * technically use NCQ, we don't bother for several
2756                          * reasons:
2757                          * 1. It hasn't been tested on a SAT layer that
2758                          *    supports it.  This is new as of SAT-4.
2759                          * 2. Even when there is a SAT layer that supports
2760                          *    it, that SAT layer will also probably support
2761                          *    ZBC -> ZAC translation, since they are both
2762                          *    in the SAT-4 spec.
2763                          * 3. Translation will likely be preferable to ATA
2764                          *    passthrough.  LSI / Avago at least single
2765                          *    steps ATA passthrough commands in the HBA,
2766                          *    regardless of protocol, so unless that
2767                          *    changes, there is a performance penalty for
2768                          *    doing ATA passthrough no matter whether
2769                          *    you're using NCQ/FPDMA, DMA or PIO.
2770                          * 4. It requires a 32-byte CDB, which at least at
2771                          *    this point in CAM requires a CDB pointer, which
2772                          *    would require us to allocate an additional bit
2773                          *    of storage separate from the CCB.
2774                          */
2775                         error = scsi_ata_zac_mgmt_in(&ccb->csio,
2776                             /*retries*/ da_retry_count,
2777                             /*cbcfnp*/ dadone,
2778                             /*tag_action*/ MSG_SIMPLE_Q_TAG,
2779                             /*use_ncq*/ 0,
2780                             /*zm_action*/ ATA_ZM_REPORT_ZONES,
2781                             /*zone_id*/ rep->starting_id,
2782                             /*zone_flags*/ rep->rep_options,
2783                             /*data_ptr*/ rz_ptr,
2784                             /*dxfer_len*/ alloc_size,
2785                             /*cdb_storage*/ NULL,
2786                             /*cdb_storage_len*/ 0,
2787                             /*sense_len*/ SSD_FULL_SIZE,
2788                             /*timeout*/ da_default_timeout * 1000);
2789                         if (error != 0) {
2790                                 error = EINVAL;
2791                                 xpt_print(periph->path,
2792                                     "scsi_ata_zac_mgmt_in() returned an "
2793                                     "error!");
2794                                 goto bailout;
2795                         }
2796                 }
2797
2798                 /*
2799                  * For BIO_ZONE, this isn't normally needed.  However, it
2800                  * is used by devstat_end_transaction_bio() to determine
2801                  * how much data was transferred.
2802                  */
2803                 /*
2804                  * XXX KDM we have a problem.  But I'm not sure how to fix
2805                  * it.  devstat uses bio_bcount - bio_resid to calculate
2806                  * the amount of data transferred.   The GEOM disk code
2807                  * uses bio_length - bio_resid to calculate the amount of
2808                  * data in bio_completed.  We have different structure
2809                  * sizes above and below the ada(4) driver.  So, if we
2810                  * use the sizes above, the amount transferred won't be
2811                  * quite accurate for devstat.  If we use different sizes
2812                  * for bio_bcount and bio_length (above and below
2813                  * respectively), then the residual needs to match one or
2814                  * the other.  Everything is calculated after the bio
2815                  * leaves the driver, so changing the values around isn't
2816                  * really an option.  For now, just set the count to the
2817                  * passed in length.  This means that the calculations
2818                  * above (e.g. bio_completed) will be correct, but the
2819                  * amount of data reported to devstat will be slightly
2820                  * under or overstated.
2821                  */
2822                 bp->bio_bcount = bp->bio_length;
2823
2824                 *queue_ccb = 1;
2825
2826                 break;
2827         }
2828         case DISK_ZONE_GET_PARAMS: {
2829                 struct disk_zone_disk_params *params;
2830
2831                 params = &bp->bio_zone.zone_params.disk_params;
2832                 bzero(params, sizeof(*params));
2833
2834                 switch (softc->zone_mode) {
2835                 case DA_ZONE_DRIVE_MANAGED:
2836                         params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
2837                         break;
2838                 case DA_ZONE_HOST_AWARE:
2839                         params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
2840                         break;
2841                 case DA_ZONE_HOST_MANAGED:
2842                         params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
2843                         break;
2844                 default:
2845                 case DA_ZONE_NONE:
2846                         params->zone_mode = DISK_ZONE_MODE_NONE;
2847                         break;
2848                 }
2849
2850                 if (softc->zone_flags & DA_ZONE_FLAG_URSWRZ)
2851                         params->flags |= DISK_ZONE_DISK_URSWRZ;
2852
2853                 if (softc->zone_flags & DA_ZONE_FLAG_OPT_SEQ_SET) {
2854                         params->optimal_seq_zones = softc->optimal_seq_zones;
2855                         params->flags |= DISK_ZONE_OPT_SEQ_SET;
2856                 }
2857
2858                 if (softc->zone_flags & DA_ZONE_FLAG_OPT_NONSEQ_SET) {
2859                         params->optimal_nonseq_zones =
2860                             softc->optimal_nonseq_zones;
2861                         params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
2862                 }
2863
2864                 if (softc->zone_flags & DA_ZONE_FLAG_MAX_SEQ_SET) {
2865                         params->max_seq_zones = softc->max_seq_zones;
2866                         params->flags |= DISK_ZONE_MAX_SEQ_SET;
2867                 }
2868                 if (softc->zone_flags & DA_ZONE_FLAG_RZ_SUP)
2869                         params->flags |= DISK_ZONE_RZ_SUP;
2870
2871                 if (softc->zone_flags & DA_ZONE_FLAG_OPEN_SUP)
2872                         params->flags |= DISK_ZONE_OPEN_SUP;
2873
2874                 if (softc->zone_flags & DA_ZONE_FLAG_CLOSE_SUP)
2875                         params->flags |= DISK_ZONE_CLOSE_SUP;
2876
2877                 if (softc->zone_flags & DA_ZONE_FLAG_FINISH_SUP)
2878                         params->flags |= DISK_ZONE_FINISH_SUP;
2879
2880                 if (softc->zone_flags & DA_ZONE_FLAG_RWP_SUP)
2881                         params->flags |= DISK_ZONE_RWP_SUP;
2882                 break;
2883         }
2884         default:
2885                 break;
2886         }
2887 bailout:
2888         return (error);
2889 }
2890
2891 static void
2892 dastart(struct cam_periph *periph, union ccb *start_ccb)
2893 {
2894         struct da_softc *softc;
2895
2896         softc = (struct da_softc *)periph->softc;
2897
2898         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2899
2900 skipstate:
2901         switch (softc->state) {
2902         case DA_STATE_NORMAL:
2903         {
2904                 struct bio *bp;
2905                 uint8_t tag_code;
2906
2907 more:
2908                 bp = cam_iosched_next_bio(softc->cam_iosched);
2909                 if (bp == NULL) {
2910                         if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
2911                                 cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
2912                                 scsi_test_unit_ready(&start_ccb->csio,
2913                                      /*retries*/ da_retry_count,
2914                                      dadone,
2915                                      MSG_SIMPLE_Q_TAG,
2916                                      SSD_FULL_SIZE,
2917                                      da_default_timeout * 1000);
2918                                 start_ccb->ccb_h.ccb_bp = NULL;
2919                                 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2920                                 xpt_action(start_ccb);
2921                         } else
2922                                 xpt_release_ccb(start_ccb);
2923                         break;
2924                 }
2925
2926                 if (bp->bio_cmd == BIO_DELETE) {
2927                         if (softc->delete_func != NULL) {
2928                                 softc->delete_func(periph, start_ccb, bp);
2929                                 goto out;
2930                         } else {
2931                                 /* Not sure this is possible, but failsafe by lying and saying "sure, done." */
2932                                 biofinish(bp, NULL, 0);
2933                                 goto more;
2934                         }
2935                 }
2936
2937                 if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
2938                         cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
2939                         cam_periph_release_locked(periph);      /* XXX is this still valid? I think so but unverified */
2940                 }
2941
2942                 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2943                     (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2944                         softc->flags &= ~DA_FLAG_NEED_OTAG;
2945                         softc->flags |= DA_FLAG_WAS_OTAG;
2946                         tag_code = MSG_ORDERED_Q_TAG;
2947                 } else {
2948                         tag_code = MSG_SIMPLE_Q_TAG;
2949                 }
2950
2951                 switch (bp->bio_cmd) {
2952                 case BIO_WRITE:
2953                 case BIO_READ:
2954                 {
2955                         void *data_ptr;
2956                         int rw_op;
2957
2958                         if (bp->bio_cmd == BIO_WRITE) {
2959                                 softc->flags |= DA_FLAG_DIRTY;
2960                                 rw_op = SCSI_RW_WRITE;
2961                         } else {
2962                                 rw_op = SCSI_RW_READ;
2963                         }
2964
2965                         data_ptr = bp->bio_data;
2966                         if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2967                                 rw_op |= SCSI_RW_BIO;
2968                                 data_ptr = bp;
2969                         }
2970
2971                         scsi_read_write(&start_ccb->csio,
2972                                         /*retries*/da_retry_count,
2973                                         /*cbfcnp*/dadone,
2974                                         /*tag_action*/tag_code,
2975                                         rw_op,
2976                                         /*byte2*/0,
2977                                         softc->minimum_cmd_size,
2978                                         /*lba*/bp->bio_pblkno,
2979                                         /*block_count*/bp->bio_bcount /
2980                                         softc->params.secsize,
2981                                         data_ptr,
2982                                         /*dxfer_len*/ bp->bio_bcount,
2983                                         /*sense_len*/SSD_FULL_SIZE,
2984                                         da_default_timeout * 1000);
2985                         break;
2986                 }
2987                 case BIO_FLUSH:
2988                         /*
2989                          * BIO_FLUSH doesn't currently communicate
2990                          * range data, so we synchronize the cache
2991                          * over the whole disk.  We also force
2992                          * ordered tag semantics the flush applies
2993                          * to all previously queued I/O.
2994                          */
2995                         scsi_synchronize_cache(&start_ccb->csio,
2996                                                /*retries*/1,
2997                                                /*cbfcnp*/dadone,
2998                                                MSG_ORDERED_Q_TAG,
2999                                                /*begin_lba*/0,
3000                                                /*lb_count*/0,
3001                                                SSD_FULL_SIZE,
3002                                                da_default_timeout*1000);
3003                         break;
3004                 case BIO_ZONE: {
3005                         int error, queue_ccb;
3006
3007                         queue_ccb = 0;
3008
3009                         error = da_zone_cmd(periph, start_ccb, bp,&queue_ccb);
3010                         if ((error != 0)
3011                          || (queue_ccb == 0)) {
3012                                 biofinish(bp, NULL, error);
3013                                 xpt_release_ccb(start_ccb);
3014                                 return;
3015                         }
3016                         break;
3017                 }
3018                 }
3019                 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
3020                 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
3021                 start_ccb->ccb_h.softtimeout = sbttotv(da_default_softtimeout);
3022
3023 out:
3024                 LIST_INSERT_HEAD(&softc->pending_ccbs,
3025                                  &start_ccb->ccb_h, periph_links.le);
3026
3027                 /* We expect a unit attention from this device */
3028                 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
3029                         start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
3030                         softc->flags &= ~DA_FLAG_RETRY_UA;
3031                 }
3032
3033                 start_ccb->ccb_h.ccb_bp = bp;
3034                 softc->refcount++;
3035                 cam_periph_unlock(periph);
3036                 xpt_action(start_ccb);
3037                 cam_periph_lock(periph);
3038                 softc->refcount--;
3039
3040                 /* May have more work to do, so ensure we stay scheduled */
3041                 daschedule(periph);
3042                 break;
3043         }
3044         case DA_STATE_PROBE_RC:
3045         {
3046                 struct scsi_read_capacity_data *rcap;
3047
3048                 rcap = (struct scsi_read_capacity_data *)
3049                     malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
3050                 if (rcap == NULL) {
3051                         printf("dastart: Couldn't malloc read_capacity data\n");
3052                         /* da_free_periph??? */
3053                         break;
3054                 }
3055                 scsi_read_capacity(&start_ccb->csio,
3056                                    /*retries*/da_retry_count,
3057                                    dadone,
3058                                    MSG_SIMPLE_Q_TAG,
3059                                    rcap,
3060                                    SSD_FULL_SIZE,
3061                                    /*timeout*/5000);
3062                 start_ccb->ccb_h.ccb_bp = NULL;
3063                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
3064                 xpt_action(start_ccb);
3065                 break;
3066         }
3067         case DA_STATE_PROBE_RC16:
3068         {
3069                 struct scsi_read_capacity_data_long *rcaplong;
3070
3071                 rcaplong = (struct scsi_read_capacity_data_long *)
3072                         malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
3073                 if (rcaplong == NULL) {
3074                         printf("dastart: Couldn't malloc read_capacity data\n");
3075                         /* da_free_periph??? */
3076                         break;
3077                 }
3078                 scsi_read_capacity_16(&start_ccb->csio,
3079                                       /*retries*/ da_retry_count,
3080                                       /*cbfcnp*/ dadone,
3081                                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
3082                                       /*lba*/ 0,
3083                                       /*reladr*/ 0,
3084                                       /*pmi*/ 0,
3085                                       /*rcap_buf*/ (uint8_t *)rcaplong,
3086                                       /*rcap_buf_len*/ sizeof(*rcaplong),
3087                                       /*sense_len*/ SSD_FULL_SIZE,
3088                                       /*timeout*/ da_default_timeout * 1000);
3089                 start_ccb->ccb_h.ccb_bp = NULL;
3090                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
3091                 xpt_action(start_ccb);
3092                 break;
3093         }
3094         case DA_STATE_PROBE_LBP:
3095         {
3096                 struct scsi_vpd_logical_block_prov *lbp;
3097
3098                 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
3099                         /*
3100                          * If we get here we don't support any SBC-3 delete
3101                          * methods with UNMAP as the Logical Block Provisioning
3102                          * VPD page support is required for devices which
3103                          * support it according to T10/1799-D Revision 31
3104                          * however older revisions of the spec don't mandate
3105                          * this so we currently don't remove these methods
3106                          * from the available set.
3107                          */
3108                         softc->state = DA_STATE_PROBE_BLK_LIMITS;
3109                         goto skipstate;
3110                 }
3111
3112                 lbp = (struct scsi_vpd_logical_block_prov *)
3113                         malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
3114
3115                 if (lbp == NULL) {
3116                         printf("dastart: Couldn't malloc lbp data\n");
3117                         /* da_free_periph??? */
3118                         break;
3119                 }
3120
3121                 scsi_inquiry(&start_ccb->csio,
3122                              /*retries*/da_retry_count,
3123                              /*cbfcnp*/dadone,
3124                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3125                              /*inq_buf*/(u_int8_t *)lbp,
3126                              /*inq_len*/sizeof(*lbp),
3127                              /*evpd*/TRUE,
3128                              /*page_code*/SVPD_LBP,
3129                              /*sense_len*/SSD_MIN_SIZE,
3130                              /*timeout*/da_default_timeout * 1000);
3131                 start_ccb->ccb_h.ccb_bp = NULL;
3132                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
3133                 xpt_action(start_ccb);
3134                 break;
3135         }
3136         case DA_STATE_PROBE_BLK_LIMITS:
3137         {
3138                 struct scsi_vpd_block_limits *block_limits;
3139
3140                 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
3141                         /* Not supported skip to next probe */
3142                         softc->state = DA_STATE_PROBE_BDC;
3143                         goto skipstate;
3144                 }
3145
3146                 block_limits = (struct scsi_vpd_block_limits *)
3147                         malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
3148
3149                 if (block_limits == NULL) {
3150                         printf("dastart: Couldn't malloc block_limits data\n");
3151                         /* da_free_periph??? */
3152                         break;
3153                 }
3154
3155                 scsi_inquiry(&start_ccb->csio,
3156                              /*retries*/da_retry_count,
3157                              /*cbfcnp*/dadone,
3158                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3159                              /*inq_buf*/(u_int8_t *)block_limits,
3160                              /*inq_len*/sizeof(*block_limits),
3161                              /*evpd*/TRUE,
3162                              /*page_code*/SVPD_BLOCK_LIMITS,
3163                              /*sense_len*/SSD_MIN_SIZE,
3164                              /*timeout*/da_default_timeout * 1000);
3165                 start_ccb->ccb_h.ccb_bp = NULL;
3166                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
3167                 xpt_action(start_ccb);
3168                 break;
3169         }
3170         case DA_STATE_PROBE_BDC:
3171         {
3172                 struct scsi_vpd_block_characteristics *bdc;
3173
3174                 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
3175                         softc->state = DA_STATE_PROBE_ATA;
3176                         goto skipstate;
3177                 }
3178
3179                 bdc = (struct scsi_vpd_block_characteristics *)
3180                         malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3181
3182                 if (bdc == NULL) {
3183                         printf("dastart: Couldn't malloc bdc data\n");
3184                         /* da_free_periph??? */
3185                         break;
3186                 }
3187
3188                 scsi_inquiry(&start_ccb->csio,
3189                              /*retries*/da_retry_count,
3190                              /*cbfcnp*/dadone,
3191                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3192                              /*inq_buf*/(u_int8_t *)bdc,
3193                              /*inq_len*/sizeof(*bdc),
3194                              /*evpd*/TRUE,
3195                              /*page_code*/SVPD_BDC,
3196                              /*sense_len*/SSD_MIN_SIZE,
3197                              /*timeout*/da_default_timeout * 1000);
3198                 start_ccb->ccb_h.ccb_bp = NULL;
3199                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
3200                 xpt_action(start_ccb);
3201                 break;
3202         }
3203         case DA_STATE_PROBE_ATA:
3204         {
3205                 struct ata_params *ata_params;
3206
3207                 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
3208                         if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
3209                          || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
3210                                 /*
3211                                  * Note that if the ATA VPD page isn't
3212                                  * supported, we aren't talking to an ATA
3213                                  * device anyway.  Support for that VPD
3214                                  * page is mandatory for SCSI to ATA (SAT)
3215                                  * translation layers.
3216                                  */
3217                                 softc->state = DA_STATE_PROBE_ZONE;
3218                                 goto skipstate;
3219                         }
3220                         daprobedone(periph, start_ccb);
3221                         break;
3222                 }
3223
3224                 ata_params = (struct ata_params*)
3225                         malloc(sizeof(*ata_params), M_SCSIDA,M_NOWAIT|M_ZERO);
3226
3227                 if (ata_params == NULL) {
3228                         xpt_print(periph->path, "Couldn't malloc ata_params "
3229                             "data\n");
3230                         /* da_free_periph??? */
3231                         break;
3232                 }
3233
3234                 scsi_ata_identify(&start_ccb->csio,
3235                                   /*retries*/da_retry_count,
3236                                   /*cbfcnp*/dadone,
3237                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
3238                                   /*data_ptr*/(u_int8_t *)ata_params,
3239                                   /*dxfer_len*/sizeof(*ata_params),
3240                                   /*sense_len*/SSD_FULL_SIZE,
3241                                   /*timeout*/da_default_timeout * 1000);
3242                 start_ccb->ccb_h.ccb_bp = NULL;
3243                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
3244                 xpt_action(start_ccb);
3245                 break;
3246         }
3247         case DA_STATE_PROBE_ATA_LOGDIR:
3248         {
3249                 struct ata_gp_log_dir *log_dir;
3250                 int retval;
3251
3252                 retval = 0;
3253
3254                 if ((softc->flags & DA_FLAG_CAN_ATA_LOG) == 0) {
3255                         /*
3256                          * If we don't have log support, not much point in
3257                          * trying to probe zone support.
3258                          */
3259                         daprobedone(periph, start_ccb);
3260                         break;
3261                 }
3262
3263                 /*
3264                  * If we have an ATA device (the SCSI ATA Information VPD
3265                  * page should be present and the ATA identify should have
3266                  * succeeded) and it supports logs, ask for the log directory.
3267                  */
3268
3269                 log_dir = malloc(sizeof(*log_dir), M_SCSIDA, M_NOWAIT|M_ZERO);
3270                 if (log_dir == NULL) {
3271                         xpt_print(periph->path, "Couldn't malloc log_dir "
3272                             "data\n");
3273                         daprobedone(periph, start_ccb);
3274                         break;
3275                 }
3276
3277                 retval = scsi_ata_read_log(&start_ccb->csio,
3278                     /*retries*/ da_retry_count,
3279                     /*cbfcnp*/ dadone,
3280                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3281                     /*log_address*/ ATA_LOG_DIRECTORY,
3282                     /*page_number*/ 0,
3283                     /*block_count*/ 1,
3284                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3285                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3286                     /*data_ptr*/ (uint8_t *)log_dir,
3287                     /*dxfer_len*/ sizeof(*log_dir),
3288                     /*sense_len*/ SSD_FULL_SIZE,
3289                     /*timeout*/ da_default_timeout * 1000);
3290
3291                 if (retval != 0) {
3292                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3293                         free(log_dir, M_SCSIDA);
3294                         daprobedone(periph, start_ccb);
3295                         break;
3296                 }
3297                 start_ccb->ccb_h.ccb_bp = NULL;
3298                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_LOGDIR;
3299                 xpt_action(start_ccb);
3300                 break;
3301         }
3302         case DA_STATE_PROBE_ATA_IDDIR:
3303         {
3304                 struct ata_identify_log_pages *id_dir;
3305                 int retval;
3306
3307                 retval = 0;
3308
3309                 /*
3310                  * Check here to see whether the Identify Device log is
3311                  * supported in the directory of logs.  If so, continue
3312                  * with requesting the log of identify device pages.
3313                  */
3314                 if ((softc->flags & DA_FLAG_CAN_ATA_IDLOG) == 0) {
3315                         daprobedone(periph, start_ccb);
3316                         break;
3317                 }
3318
3319                 id_dir = malloc(sizeof(*id_dir), M_SCSIDA, M_NOWAIT | M_ZERO);
3320                 if (id_dir == NULL) {
3321                         xpt_print(periph->path, "Couldn't malloc id_dir "
3322                             "data\n");
3323                         daprobedone(periph, start_ccb);
3324                         break;
3325                 }
3326
3327                 retval = scsi_ata_read_log(&start_ccb->csio,
3328                     /*retries*/ da_retry_count,
3329                     /*cbfcnp*/ dadone,
3330                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3331                     /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3332                     /*page_number*/ ATA_IDL_PAGE_LIST,
3333                     /*block_count*/ 1,
3334                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3335                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3336                     /*data_ptr*/ (uint8_t *)id_dir,
3337                     /*dxfer_len*/ sizeof(*id_dir),
3338                     /*sense_len*/ SSD_FULL_SIZE,
3339                     /*timeout*/ da_default_timeout * 1000);
3340
3341                 if (retval != 0) {
3342                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3343                         free(id_dir, M_SCSIDA);
3344                         daprobedone(periph, start_ccb);
3345                         break;
3346                 }
3347                 start_ccb->ccb_h.ccb_bp = NULL;
3348                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_IDDIR;
3349                 xpt_action(start_ccb);
3350                 break;
3351         }
3352         case DA_STATE_PROBE_ATA_SUP:
3353         {
3354                 struct ata_identify_log_sup_cap *sup_cap;
3355                 int retval;
3356
3357                 retval = 0;
3358
3359                 /*
3360                  * Check here to see whether the Supported Capabilities log
3361                  * is in the list of Identify Device logs.
3362                  */
3363                 if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP) == 0) {
3364                         daprobedone(periph, start_ccb);
3365                         break;
3366                 }
3367
3368                 sup_cap = malloc(sizeof(*sup_cap), M_SCSIDA, M_NOWAIT|M_ZERO);
3369                 if (sup_cap == NULL) {
3370                         xpt_print(periph->path, "Couldn't malloc sup_cap "
3371                             "data\n");
3372                         daprobedone(periph, start_ccb);
3373                         break;
3374                 }
3375
3376                 retval = scsi_ata_read_log(&start_ccb->csio,
3377                     /*retries*/ da_retry_count,
3378                     /*cbfcnp*/ dadone,
3379                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3380                     /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3381                     /*page_number*/ ATA_IDL_SUP_CAP,
3382                     /*block_count*/ 1,
3383                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3384                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3385                     /*data_ptr*/ (uint8_t *)sup_cap,
3386                     /*dxfer_len*/ sizeof(*sup_cap),
3387                     /*sense_len*/ SSD_FULL_SIZE,
3388                     /*timeout*/ da_default_timeout * 1000);
3389
3390                 if (retval != 0) {
3391                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3392                         free(sup_cap, M_SCSIDA);
3393                         daprobedone(periph, start_ccb);
3394                         break;
3395
3396                 }
3397
3398                 start_ccb->ccb_h.ccb_bp = NULL;
3399                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_SUP;
3400                 xpt_action(start_ccb);
3401                 break;
3402         }
3403         case DA_STATE_PROBE_ATA_ZONE:
3404         {
3405                 struct ata_zoned_info_log *ata_zone;
3406                 int retval;
3407
3408                 retval = 0;
3409
3410                 /*
3411                  * Check here to see whether the zoned device information
3412                  * page is supported.  If so, continue on to request it.
3413                  * If not, skip to DA_STATE_PROBE_LOG or done.
3414                  */
3415                 if ((softc->flags & DA_FLAG_CAN_ATA_ZONE) == 0) {
3416                         daprobedone(periph, start_ccb);
3417                         break;
3418                 }
3419                 ata_zone = malloc(sizeof(*ata_zone), M_SCSIDA,
3420                                   M_NOWAIT|M_ZERO);
3421                 if (ata_zone == NULL) {
3422                         xpt_print(periph->path, "Couldn't malloc ata_zone "
3423                             "data\n");
3424                         daprobedone(periph, start_ccb);
3425                         break;
3426                 }
3427
3428                 retval = scsi_ata_read_log(&start_ccb->csio,
3429                     /*retries*/ da_retry_count,
3430                     /*cbfcnp*/ dadone,
3431                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3432                     /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3433                     /*page_number*/ ATA_IDL_ZDI,
3434                     /*block_count*/ 1,
3435                     /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3436                                  AP_PROTO_DMA : AP_PROTO_PIO_IN,
3437                     /*data_ptr*/ (uint8_t *)ata_zone,
3438                     /*dxfer_len*/ sizeof(*ata_zone),
3439                     /*sense_len*/ SSD_FULL_SIZE,
3440                     /*timeout*/ da_default_timeout * 1000);
3441
3442                 if (retval != 0) {
3443                         xpt_print(periph->path, "scsi_ata_read_log() failed!");
3444                         free(ata_zone, M_SCSIDA);
3445                         daprobedone(periph, start_ccb);
3446                         break;
3447                 }
3448                 start_ccb->ccb_h.ccb_bp = NULL;
3449                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_ZONE;
3450                 xpt_action(start_ccb);
3451
3452                 break;
3453         }
3454         case DA_STATE_PROBE_ZONE:
3455         {
3456                 struct scsi_vpd_zoned_bdc *bdc;
3457
3458                 /*
3459                  * Note that this page will be supported for SCSI protocol
3460                  * devices that support ZBC (SMR devices), as well as ATA
3461                  * protocol devices that are behind a SAT (SCSI to ATA
3462                  * Translation) layer that supports converting ZBC commands
3463                  * to their ZAC equivalents.
3464                  */
3465                 if (!scsi_vpd_supported_page(periph, SVPD_ZONED_BDC)) {
3466                         daprobedone(periph, start_ccb);
3467                         break;
3468                 }
3469                 bdc = (struct scsi_vpd_zoned_bdc *)
3470                         malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3471
3472                 if (bdc == NULL) {
3473                         xpt_release_ccb(start_ccb);
3474                         xpt_print(periph->path, "Couldn't malloc zone VPD "
3475                             "data\n");
3476                         break;
3477                 }
3478                 scsi_inquiry(&start_ccb->csio,
3479                              /*retries*/da_retry_count,
3480                              /*cbfcnp*/dadone,
3481                              /*tag_action*/MSG_SIMPLE_Q_TAG,
3482                              /*inq_buf*/(u_int8_t *)bdc,
3483                              /*inq_len*/sizeof(*bdc),
3484                              /*evpd*/TRUE,
3485                              /*page_code*/SVPD_ZONED_BDC,
3486                              /*sense_len*/SSD_FULL_SIZE,
3487                              /*timeout*/da_default_timeout * 1000);
3488                 start_ccb->ccb_h.ccb_bp = NULL;
3489                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ZONE;
3490                 xpt_action(start_ccb);
3491                 break;
3492         }
3493         }
3494 }
3495
3496 /*
3497  * In each of the methods below, while its the caller's
3498  * responsibility to ensure the request will fit into a
3499  * single device request, we might have changed the delete
3500  * method due to the device incorrectly advertising either
3501  * its supported methods or limits.
3502  * 
3503  * To prevent this causing further issues we validate the
3504  * against the methods limits, and warn which would
3505  * otherwise be unnecessary.
3506  */
3507 static void
3508 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3509 {
3510         struct da_softc *softc = (struct da_softc *)periph->softc;;
3511         struct bio *bp1;
3512         uint8_t *buf = softc->unmap_buf;
3513         struct scsi_unmap_desc *d = (void *)&buf[UNMAP_HEAD_SIZE];
3514         uint64_t lba, lastlba = (uint64_t)-1;
3515         uint64_t totalcount = 0;
3516         uint64_t count;
3517         uint32_t c, lastcount = 0, ranges = 0;
3518
3519         /*
3520          * Currently this doesn't take the UNMAP
3521          * Granularity and Granularity Alignment
3522          * fields into account.
3523          *
3524          * This could result in both unoptimal unmap
3525          * requests as as well as UNMAP calls unmapping
3526          * fewer LBA's than requested.
3527          */
3528
3529         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
3530         bp1 = bp;
3531         do {
3532                 /*
3533                  * Note: ada and da are different in how they store the
3534                  * pending bp's in a trim. ada stores all of them in the
3535                  * trim_req.bps. da stores all but the first one in the
3536                  * delete_run_queue. ada then completes all the bps in
3537                  * its adadone() loop. da completes all the bps in the
3538                  * delete_run_queue in dadone, and relies on the biodone
3539                  * after to complete. This should be reconciled since there's
3540                  * no real reason to do it differently. XXX
3541                  */
3542                 if (bp1 != bp)
3543                         bioq_insert_tail(&softc->delete_run_queue, bp1);
3544                 lba = bp1->bio_pblkno;
3545                 count = bp1->bio_bcount / softc->params.secsize;
3546
3547                 /* Try to extend the previous range. */
3548                 if (lba == lastlba) {
3549                         c = omin(count, UNMAP_RANGE_MAX - lastcount);
3550                         lastlba += c;
3551                         lastcount += c;
3552                         scsi_ulto4b(lastcount, d[ranges - 1].length);
3553                         count -= c;
3554                         lba += c;
3555                         totalcount += c;
3556                 } else if ((softc->quirks & DA_Q_STRICT_UNMAP) &&
3557                     softc->unmap_gran != 0) {
3558                         /* Align length of the previous range. */
3559                         if ((c = lastcount % softc->unmap_gran) != 0) {
3560                                 if (lastcount <= c) {
3561                                         totalcount -= lastcount;
3562                                         lastlba = (uint64_t)-1;
3563                                         lastcount = 0;
3564                                         ranges--;
3565                                 } else {
3566                                         totalcount -= c;
3567                                         lastlba -= c;
3568                                         lastcount -= c;
3569                                         scsi_ulto4b(lastcount, d[ranges - 1].length);
3570                                 }
3571                         }
3572                         /* Align beginning of the new range. */
3573                         c = (lba - softc->unmap_gran_align) % softc->unmap_gran;
3574                         if (c != 0) {
3575                                 c = softc->unmap_gran - c;
3576                                 if (count <= c) {
3577                                         count = 0;
3578                                 } else {
3579                                         lba += c;
3580                                         count -= c;
3581                                 }
3582                         }
3583                 }
3584
3585                 while (count > 0) {
3586                         c = omin(count, UNMAP_RANGE_MAX);
3587                         if (totalcount + c > softc->unmap_max_lba ||
3588                             ranges >= softc->unmap_max_ranges) {
3589                                 xpt_print(periph->path,
3590                                     "%s issuing short delete %ld > %ld"
3591                                     "|| %d >= %d",
3592                                     da_delete_method_desc[softc->delete_method],
3593                                     totalcount + c, softc->unmap_max_lba,
3594                                     ranges, softc->unmap_max_ranges);
3595                                 break;
3596                         }
3597                         scsi_u64to8b(lba, d[ranges].lba);
3598                         scsi_ulto4b(c, d[ranges].length);
3599                         lba += c;
3600                         totalcount += c;
3601                         ranges++;
3602                         count -= c;
3603                         lastlba = lba;
3604                         lastcount = c;
3605                 }
3606                 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3607                 if (bp1 == NULL)
3608                         break;
3609                 if (ranges >= softc->unmap_max_ranges ||
3610                     totalcount + bp1->bio_bcount /
3611                     softc->params.secsize > softc->unmap_max_lba) {
3612                         cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3613                         break;
3614                 }
3615         } while (1);
3616
3617         /* Align length of the last range. */
3618         if ((softc->quirks & DA_Q_STRICT_UNMAP) && softc->unmap_gran != 0 &&
3619             (c = lastcount % softc->unmap_gran) != 0) {
3620                 if (lastcount <= c)
3621                         ranges--;
3622                 else
3623                         scsi_ulto4b(lastcount - c, d[ranges - 1].length);
3624         }
3625
3626         scsi_ulto2b(ranges * 16 + 6, &buf[0]);
3627         scsi_ulto2b(ranges * 16, &buf[2]);
3628
3629         scsi_unmap(&ccb->csio,
3630                    /*retries*/da_retry_count,
3631                    /*cbfcnp*/dadone,
3632                    /*tag_action*/MSG_SIMPLE_Q_TAG,
3633                    /*byte2*/0,
3634                    /*data_ptr*/ buf,
3635                    /*dxfer_len*/ ranges * 16 + 8,
3636                    /*sense_len*/SSD_FULL_SIZE,
3637                    da_default_timeout * 1000);
3638         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3639         ccb->ccb_h.flags |= CAM_UNLOCKED;
3640         cam_iosched_submit_trim(softc->cam_iosched);
3641 }
3642
3643 static void
3644 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3645 {
3646         struct da_softc *softc = (struct da_softc *)periph->softc;
3647         struct bio *bp1;
3648         uint8_t *buf = softc->unmap_buf;
3649         uint64_t lastlba = (uint64_t)-1;
3650         uint64_t count;
3651         uint64_t lba;
3652         uint32_t lastcount = 0, c, requestcount;
3653         int ranges = 0, off, block_count;
3654
3655         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
3656         bp1 = bp;
3657         do {
3658                 if (bp1 != bp)//XXX imp XXX
3659                         bioq_insert_tail(&softc->delete_run_queue, bp1);
3660                 lba = bp1->bio_pblkno;
3661                 count = bp1->bio_bcount / softc->params.secsize;
3662                 requestcount = count;
3663
3664                 /* Try to extend the previous range. */
3665                 if (lba == lastlba) {
3666                         c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
3667                         lastcount += c;
3668                         off = (ranges - 1) * 8;
3669                         buf[off + 6] = lastcount & 0xff;
3670                         buf[off + 7] = (lastcount >> 8) & 0xff;
3671                         count -= c;
3672                         lba += c;
3673                 }
3674
3675                 while (count > 0) {
3676                         c = omin(count, ATA_DSM_RANGE_MAX);
3677                         off = ranges * 8;
3678
3679                         buf[off + 0] = lba & 0xff;
3680                         buf[off + 1] = (lba >> 8) & 0xff;
3681                         buf[off + 2] = (lba >> 16) & 0xff;
3682                         buf[off + 3] = (lba >> 24) & 0xff;
3683                         buf[off + 4] = (lba >> 32) & 0xff;
3684                         buf[off + 5] = (lba >> 40) & 0xff;
3685                         buf[off + 6] = c & 0xff;
3686                         buf[off + 7] = (c >> 8) & 0xff;
3687                         lba += c;
3688                         ranges++;
3689                         count -= c;
3690                         lastcount = c;
3691                         if (count != 0 && ranges == softc->trim_max_ranges) {
3692                                 xpt_print(periph->path,
3693                                     "%s issuing short delete %ld > %ld\n",
3694                                     da_delete_method_desc[softc->delete_method],
3695                                     requestcount,
3696                                     (softc->trim_max_ranges - ranges) *
3697                                     ATA_DSM_RANGE_MAX);
3698                                 break;
3699                         }
3700                 }
3701                 lastlba = lba;
3702                 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3703                 if (bp1 == NULL)
3704                         break;
3705                 if (bp1->bio_bcount / softc->params.secsize >
3706                     (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
3707                         cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3708                         break;
3709                 }
3710         } while (1);
3711
3712         block_count = howmany(ranges, ATA_DSM_BLK_RANGES);
3713         scsi_ata_trim(&ccb->csio,
3714                       /*retries*/da_retry_count,
3715                       /*cbfcnp*/dadone,
3716                       /*tag_action*/MSG_SIMPLE_Q_TAG,
3717                       block_count,
3718                       /*data_ptr*/buf,
3719                       /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
3720                       /*sense_len*/SSD_FULL_SIZE,
3721                       da_default_timeout * 1000);
3722         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3723         ccb->ccb_h.flags |= CAM_UNLOCKED;
3724         cam_iosched_submit_trim(softc->cam_iosched);
3725 }
3726
3727 /*
3728  * We calculate ws_max_blks here based off d_delmaxsize instead
3729  * of using softc->ws_max_blks as it is absolute max for the
3730  * device not the protocol max which may well be lower.
3731  */
3732 static void
3733 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3734 {
3735         struct da_softc *softc;
3736         struct bio *bp1;
3737         uint64_t ws_max_blks;
3738         uint64_t lba;
3739         uint64_t count; /* forward compat with WS32 */
3740
3741         softc = (struct da_softc *)periph->softc;
3742         ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
3743         lba = bp->bio_pblkno;
3744         count = 0;
3745         bp1 = bp;
3746         do {
3747                 if (bp1 != bp)//XXX imp XXX
3748                         bioq_insert_tail(&softc->delete_run_queue, bp1);
3749                 count += bp1->bio_bcount / softc->params.secsize;
3750                 if (count > ws_max_blks) {
3751                         xpt_print(periph->path,
3752                             "%s issuing short delete %ld > %ld\n",
3753                             da_delete_method_desc[softc->delete_method],
3754                             count, ws_max_blks);
3755                         count = omin(count, ws_max_blks);
3756                         break;
3757                 }
3758                 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3759                 if (bp1 == NULL)
3760                         break;
3761                 if (lba + count != bp1->bio_pblkno ||
3762                     count + bp1->bio_bcount /
3763                     softc->params.secsize > ws_max_blks) {
3764                         cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3765                         break;
3766                 }
3767         } while (1);
3768
3769         scsi_write_same(&ccb->csio,
3770                         /*retries*/da_retry_count,
3771                         /*cbfcnp*/dadone,
3772                         /*tag_action*/MSG_SIMPLE_Q_TAG,
3773                         /*byte2*/softc->delete_method ==
3774                             DA_DELETE_ZERO ? 0 : SWS_UNMAP,
3775                         softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
3776                         /*lba*/lba,
3777                         /*block_count*/count,
3778                         /*data_ptr*/ __DECONST(void *, zero_region),
3779                         /*dxfer_len*/ softc->params.secsize,
3780                         /*sense_len*/SSD_FULL_SIZE,
3781                         da_default_timeout * 1000);
3782         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3783         ccb->ccb_h.flags |= CAM_UNLOCKED;
3784         cam_iosched_submit_trim(softc->cam_iosched);
3785 }
3786
3787 static int
3788 cmd6workaround(union ccb *ccb)
3789 {
3790         struct scsi_rw_6 cmd6;
3791         struct scsi_rw_10 *cmd10;
3792         struct da_softc *softc;
3793         u_int8_t *cdb;
3794         struct bio *bp;
3795         int frozen;
3796
3797         cdb = ccb->csio.cdb_io.cdb_bytes;
3798         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
3799
3800         if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
3801                 da_delete_methods old_method = softc->delete_method;
3802
3803                 /*
3804                  * Typically there are two reasons for failure here
3805                  * 1. Delete method was detected as supported but isn't
3806                  * 2. Delete failed due to invalid params e.g. too big
3807                  *
3808                  * While we will attempt to choose an alternative delete method
3809                  * this may result in short deletes if the existing delete
3810                  * requests from geom are big for the new method chosen.
3811                  *
3812                  * This method assumes that the error which triggered this
3813                  * will not retry the io otherwise a panic will occur
3814                  */
3815                 dadeleteflag(softc, old_method, 0);
3816                 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
3817                 if (softc->delete_method == DA_DELETE_DISABLE)
3818                         xpt_print(ccb->ccb_h.path,
3819                                   "%s failed, disabling BIO_DELETE\n",
3820                                   da_delete_method_desc[old_method]);
3821                 else
3822                         xpt_print(ccb->ccb_h.path,
3823                                   "%s failed, switching to %s BIO_DELETE\n",
3824                                   da_delete_method_desc[old_method],
3825                                   da_delete_method_desc[softc->delete_method]);
3826
3827                 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
3828                         cam_iosched_queue_work(softc->cam_iosched, bp);
3829                 cam_iosched_queue_work(softc->cam_iosched,
3830                     (struct bio *)ccb->ccb_h.ccb_bp);
3831                 ccb->ccb_h.ccb_bp = NULL;
3832                 return (0);
3833         }
3834
3835         /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
3836         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
3837             (*cdb == PREVENT_ALLOW) &&
3838             (softc->quirks & DA_Q_NO_PREVENT) == 0) {
3839                 if (bootverbose)
3840                         xpt_print(ccb->ccb_h.path,
3841                             "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
3842                 softc->quirks |= DA_Q_NO_PREVENT;
3843                 return (0);
3844         }
3845
3846         /* Detect unsupported SYNCHRONIZE CACHE(10). */
3847         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
3848             (*cdb == SYNCHRONIZE_CACHE) &&
3849             (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
3850                 if (bootverbose)
3851                         xpt_print(ccb->ccb_h.path,
3852                             "SYNCHRONIZE CACHE(10) not supported.\n");
3853                 softc->quirks |= DA_Q_NO_SYNC_CACHE;
3854                 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
3855                 return (0);
3856         }
3857
3858         /* Translation only possible if CDB is an array and cmd is R/W6 */
3859         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
3860             (*cdb != READ_6 && *cdb != WRITE_6))
3861                 return 0;
3862
3863         xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
3864             "increasing minimum_cmd_size to 10.\n");
3865         softc->minimum_cmd_size = 10;
3866
3867         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
3868         cmd10 = (struct scsi_rw_10 *)cdb;
3869         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
3870         cmd10->byte2 = 0;
3871         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
3872         cmd10->reserved = 0;
3873         scsi_ulto2b(cmd6.length, cmd10->length);
3874         cmd10->control = cmd6.control;
3875         ccb->csio.cdb_len = sizeof(*cmd10);
3876
3877         /* Requeue request, unfreezing queue if necessary */
3878         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3879         ccb->ccb_h.status = CAM_REQUEUE_REQ;
3880         xpt_action(ccb);
3881         if (frozen) {
3882                 cam_release_devq(ccb->ccb_h.path,
3883                                  /*relsim_flags*/0,
3884                                  /*reduction*/0,
3885                                  /*timeout*/0,
3886                                  /*getcount_only*/0);
3887         }
3888         return (ERESTART);
3889 }
3890
3891 static void
3892 dazonedone(struct cam_periph *periph, union ccb *ccb)
3893 {
3894         struct da_softc *softc;
3895         struct bio *bp;
3896
3897         softc = periph->softc;
3898         bp = (struct bio *)ccb->ccb_h.ccb_bp;
3899
3900         switch (bp->bio_zone.zone_cmd) {
3901         case DISK_ZONE_OPEN:
3902         case DISK_ZONE_CLOSE:
3903         case DISK_ZONE_FINISH:
3904         case DISK_ZONE_RWP:
3905                 break;
3906         case DISK_ZONE_REPORT_ZONES: {
3907                 uint32_t avail_len;
3908                 struct disk_zone_report *rep;
3909                 struct scsi_report_zones_hdr *hdr;
3910                 struct scsi_report_zones_desc *desc;
3911                 struct disk_zone_rep_entry *entry;
3912                 uint32_t num_alloced, hdr_len, num_avail;
3913                 uint32_t num_to_fill, i;
3914                 int ata;
3915
3916                 rep = &bp->bio_zone.zone_params.report;
3917                 avail_len = ccb->csio.dxfer_len - ccb->csio.resid;
3918                 /*
3919                  * Note that bio_resid isn't normally used for zone
3920                  * commands, but it is used by devstat_end_transaction_bio()
3921                  * to determine how much data was transferred.  Because
3922                  * the size of the SCSI/ATA data structures is different
3923                  * than the size of the BIO interface structures, the
3924                  * amount of data actually transferred from the drive will
3925                  * be different than the amount of data transferred to
3926                  * the user.
3927                  */
3928                 bp->bio_resid = ccb->csio.resid;
3929                 num_alloced = rep->entries_allocated;
3930                 hdr = (struct scsi_report_zones_hdr *)ccb->csio.data_ptr;
3931                 if (avail_len < sizeof(*hdr)) {
3932                         /*
3933                          * Is there a better error than EIO here?  We asked
3934                          * for at least the header, and we got less than
3935                          * that.
3936                          */
3937                         bp->bio_error = EIO;
3938                         bp->bio_flags |= BIO_ERROR;
3939                         bp->bio_resid = bp->bio_bcount;
3940                         break;
3941                 }
3942
3943                 if (softc->zone_interface == DA_ZONE_IF_ATA_PASS)
3944                         ata = 1;
3945                 else
3946                         ata = 0;
3947
3948                 hdr_len = ata ? le32dec(hdr->length) :
3949                                 scsi_4btoul(hdr->length);
3950                 if (hdr_len > 0)
3951                         rep->entries_available = hdr_len / sizeof(*desc);
3952                 else
3953                         rep->entries_available = 0;
3954                 /*
3955                  * NOTE: using the same values for the BIO version of the
3956                  * same field as the SCSI/ATA values.  This means we could
3957                  * get some additional values that aren't defined in bio.h
3958                  * if more values of the same field are defined later.
3959                  */
3960                 rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
3961                 rep->header.maximum_lba = ata ?  le64dec(hdr->maximum_lba) :
3962                                           scsi_8btou64(hdr->maximum_lba);
3963                 /*
3964                  * If the drive reports no entries that match the query,
3965                  * we're done.
3966                  */
3967                 if (hdr_len == 0) {
3968                         rep->entries_filled = 0;
3969                         break;
3970                 }
3971
3972                 num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
3973                                 hdr_len / sizeof(*desc));
3974                 /*
3975                  * If the drive didn't return any data, then we're done.
3976                  */
3977                 if (num_avail == 0) {
3978                         rep->entries_filled = 0;
3979                         break;
3980                 }
3981
3982                 num_to_fill = min(num_avail, rep->entries_allocated);
3983                 /*
3984                  * If the user didn't allocate any entries for us to fill,
3985                  * we're done.
3986                  */
3987                 if (num_to_fill == 0) {
3988                         rep->entries_filled = 0;
3989                         break;
3990                 }
3991
3992                 for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
3993                      i < num_to_fill; i++, desc++, entry++) {
3994                         /*
3995                          * NOTE: we're mapping the values here directly
3996                          * from the SCSI/ATA bit definitions to the bio.h
3997                          * definitons.  There is also a warning in
3998                          * disk_zone.h, but the impact is that if
3999                          * additional values are added in the SCSI/ATA
4000                          * specs these will be visible to consumers of
4001                          * this interface.
4002                          */
4003                         entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
4004                         entry->zone_condition =
4005                             (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
4006                             SRZ_ZONE_COND_SHIFT;
4007                         entry->zone_flags |= desc->zone_flags &
4008                             (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
4009                         entry->zone_length =
4010                             ata ? le64dec(desc->zone_length) :
4011                                   scsi_8btou64(desc->zone_length);
4012                         entry->zone_start_lba =
4013                             ata ? le64dec(desc->zone_start_lba) :
4014                                   scsi_8btou64(desc->zone_start_lba);
4015                         entry->write_pointer_lba =
4016                             ata ? le64dec(desc->write_pointer_lba) :
4017                                   scsi_8btou64(desc->write_pointer_lba);
4018                 }
4019                 rep->entries_filled = num_to_fill;
4020                 break;
4021         }
4022         case DISK_ZONE_GET_PARAMS:
4023         default:
4024                 /*
4025                  * In theory we should not get a GET_PARAMS bio, since it
4026                  * should be handled without queueing the command to the
4027                  * drive.
4028                  */
4029                 panic("%s: Invalid zone command %d", __func__,
4030                     bp->bio_zone.zone_cmd);
4031                 break;
4032         }
4033
4034         if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
4035                 free(ccb->csio.data_ptr, M_SCSIDA);
4036 }
4037
4038 static void
4039 dadone(struct cam_periph *periph, union ccb *done_ccb)
4040 {
4041         struct da_softc *softc;
4042         struct ccb_scsiio *csio;
4043         u_int32_t  priority;
4044         da_ccb_state state;
4045
4046         softc = (struct da_softc *)periph->softc;
4047         priority = done_ccb->ccb_h.pinfo.priority;
4048
4049         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
4050
4051         csio = &done_ccb->csio;
4052         state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4053         switch (state) {
4054         case DA_CCB_BUFFER_IO:
4055         case DA_CCB_DELETE:
4056         {
4057                 struct bio *bp, *bp1;
4058
4059                 cam_periph_lock(periph);
4060                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4061                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4062                         int error;
4063                         int sf;
4064
4065                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
4066                                 sf = SF_RETRY_UA;
4067                         else
4068                                 sf = 0;
4069
4070                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
4071                         if (error == ERESTART) {
4072                                 /*
4073                                  * A retry was scheduled, so
4074                                  * just return.
4075                                  */
4076                                 cam_periph_unlock(periph);
4077                                 return;
4078                         }
4079                         bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4080                         if (error != 0) {
4081                                 int queued_error;
4082
4083                                 /*
4084                                  * return all queued I/O with EIO, so that
4085                                  * the client can retry these I/Os in the
4086                                  * proper order should it attempt to recover.
4087                                  */
4088                                 queued_error = EIO;
4089
4090                                 if (error == ENXIO
4091                                  && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
4092                                         /*
4093                                          * Catastrophic error.  Mark our pack as
4094                                          * invalid.
4095                                          */
4096                                         /*
4097                                          * XXX See if this is really a media
4098                                          * XXX change first?
4099                                          */
4100                                         xpt_print(periph->path,
4101                                             "Invalidating pack\n");
4102                                         softc->flags |= DA_FLAG_PACK_INVALID;
4103 #ifdef CAM_IO_STATS
4104                                         softc->invalidations++;
4105 #endif
4106                                         queued_error = ENXIO;
4107                                 }
4108                                 cam_iosched_flush(softc->cam_iosched, NULL,
4109                                            queued_error);
4110                                 if (bp != NULL) {
4111                                         bp->bio_error = error;
4112                                         bp->bio_resid = bp->bio_bcount;
4113                                         bp->bio_flags |= BIO_ERROR;
4114                                 }
4115                         } else if (bp != NULL) {
4116                                 if (state == DA_CCB_DELETE)
4117                                         bp->bio_resid = 0;
4118                                 else
4119                                         bp->bio_resid = csio->resid;
4120                                 bp->bio_error = 0;
4121                                 if (bp->bio_resid != 0)
4122                                         bp->bio_flags |= BIO_ERROR;
4123                         }
4124                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4125                                 cam_release_devq(done_ccb->ccb_h.path,
4126                                                  /*relsim_flags*/0,
4127                                                  /*reduction*/0,
4128                                                  /*timeout*/0,
4129                                                  /*getcount_only*/0);
4130                 } else if (bp != NULL) {
4131                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4132                                 panic("REQ_CMP with QFRZN");
4133                         if (bp->bio_cmd == BIO_ZONE)
4134                                 dazonedone(periph, done_ccb);
4135                         else if (state == DA_CCB_DELETE)
4136                                 bp->bio_resid = 0;
4137                         else
4138                                 bp->bio_resid = csio->resid;
4139                         if ((csio->resid > 0)
4140                          && (bp->bio_cmd != BIO_ZONE))
4141                                 bp->bio_flags |= BIO_ERROR;
4142                         if (softc->error_inject != 0) {
4143                                 bp->bio_error = softc->error_inject;
4144                                 bp->bio_resid = bp->bio_bcount;
4145                                 bp->bio_flags |= BIO_ERROR;
4146                                 softc->error_inject = 0;
4147                         }
4148                 }
4149
4150                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
4151                 if (LIST_EMPTY(&softc->pending_ccbs))
4152                         softc->flags |= DA_FLAG_WAS_OTAG;
4153
4154                 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
4155                 xpt_release_ccb(done_ccb);
4156                 if (state == DA_CCB_DELETE) {
4157                         TAILQ_HEAD(, bio) queue;
4158
4159                         TAILQ_INIT(&queue);
4160                         TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
4161                         softc->delete_run_queue.insert_point = NULL;
4162                         /*
4163                          * Normally, the xpt_release_ccb() above would make sure
4164                          * that when we have more work to do, that work would
4165                          * get kicked off. However, we specifically keep
4166                          * delete_running set to 0 before the call above to
4167                          * allow other I/O to progress when many BIO_DELETE
4168                          * requests are pushed down. We set delete_running to 0
4169                          * and call daschedule again so that we don't stall if
4170                          * there are no other I/Os pending apart from BIO_DELETEs.
4171                          */
4172                         cam_iosched_trim_done(softc->cam_iosched);
4173                         daschedule(periph);
4174                         cam_periph_unlock(periph);
4175                         while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
4176                                 TAILQ_REMOVE(&queue, bp1, bio_queue);
4177                                 bp1->bio_error = bp->bio_error;
4178                                 if (bp->bio_flags & BIO_ERROR) {
4179                                         bp1->bio_flags |= BIO_ERROR;
4180                                         bp1->bio_resid = bp1->bio_bcount;
4181                                 } else
4182                                         bp1->bio_resid = 0;
4183                                 biodone(bp1);
4184                         }
4185                 } else {
4186                         daschedule(periph);
4187                         cam_periph_unlock(periph);
4188                 }
4189                 if (bp != NULL)
4190                         biodone(bp);
4191                 return;
4192         }
4193         case DA_CCB_PROBE_RC:
4194         case DA_CCB_PROBE_RC16:
4195         {
4196                 struct     scsi_read_capacity_data *rdcap;
4197                 struct     scsi_read_capacity_data_long *rcaplong;
4198                 char       announce_buf[80];
4199                 int        lbp;
4200
4201                 lbp = 0;
4202                 rdcap = NULL;
4203                 rcaplong = NULL;
4204                 if (state == DA_CCB_PROBE_RC)
4205                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
4206                 else
4207                         rcaplong = (struct scsi_read_capacity_data_long *)
4208                                 csio->data_ptr;
4209
4210                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4211                         struct disk_params *dp;
4212                         uint32_t block_size;
4213                         uint64_t maxsector;
4214                         u_int lalba;    /* Lowest aligned LBA. */
4215
4216                         if (state == DA_CCB_PROBE_RC) {
4217                                 block_size = scsi_4btoul(rdcap->length);
4218                                 maxsector = scsi_4btoul(rdcap->addr);
4219                                 lalba = 0;
4220
4221                                 /*
4222                                  * According to SBC-2, if the standard 10
4223                                  * byte READ CAPACITY command returns 2^32,
4224                                  * we should issue the 16 byte version of
4225                                  * the command, since the device in question
4226                                  * has more sectors than can be represented
4227                                  * with the short version of the command.
4228                                  */
4229                                 if (maxsector == 0xffffffff) {
4230                                         free(rdcap, M_SCSIDA);
4231                                         xpt_release_ccb(done_ccb);
4232                                         softc->state = DA_STATE_PROBE_RC16;
4233                                         xpt_schedule(periph, priority);
4234                                         return;
4235                                 }
4236                         } else {
4237                                 block_size = scsi_4btoul(rcaplong->length);
4238                                 maxsector = scsi_8btou64(rcaplong->addr);
4239                                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
4240                         }
4241
4242                         /*
4243                          * Because GEOM code just will panic us if we
4244                          * give them an 'illegal' value we'll avoid that
4245                          * here.
4246                          */
4247                         if (block_size == 0) {
4248                                 block_size = 512;
4249                                 if (maxsector == 0)
4250                                         maxsector = -1;
4251                         }
4252                         if (block_size >= MAXPHYS) {
4253                                 xpt_print(periph->path,
4254                                     "unsupportable block size %ju\n",
4255                                     (uintmax_t) block_size);
4256                                 announce_buf[0] = '\0';
4257                                 cam_periph_invalidate(periph);
4258                         } else {
4259                                 /*
4260                                  * We pass rcaplong into dasetgeom(),
4261                                  * because it will only use it if it is
4262                                  * non-NULL.
4263                                  */
4264                                 dasetgeom(periph, block_size, maxsector,
4265                                           rcaplong, sizeof(*rcaplong));
4266                                 lbp = (lalba & SRC16_LBPME_A);
4267                                 dp = &softc->params;
4268                                 snprintf(announce_buf, sizeof(announce_buf),
4269                                     "%juMB (%ju %u byte sectors)",
4270                                     ((uintmax_t)dp->secsize * dp->sectors) /
4271                                      (1024 * 1024),
4272                                     (uintmax_t)dp->sectors, dp->secsize);
4273                         }
4274                 } else {
4275                         int     error;
4276
4277                         announce_buf[0] = '\0';
4278
4279                         /*
4280                          * Retry any UNIT ATTENTION type errors.  They
4281                          * are expected at boot.
4282                          */
4283                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4284                                         SF_RETRY_UA|SF_NO_PRINT);
4285                         if (error == ERESTART) {
4286                                 /*
4287                                  * A retry was scheuled, so
4288                                  * just return.
4289                                  */
4290                                 return;
4291                         } else if (error != 0) {
4292                                 int asc, ascq;
4293                                 int sense_key, error_code;
4294                                 int have_sense;
4295                                 cam_status status;
4296                                 struct ccb_getdev cgd;
4297
4298                                 /* Don't wedge this device's queue */
4299                                 status = done_ccb->ccb_h.status;
4300                                 if ((status & CAM_DEV_QFRZN) != 0)
4301                                         cam_release_devq(done_ccb->ccb_h.path,
4302                                                          /*relsim_flags*/0,
4303                                                          /*reduction*/0,
4304                                                          /*timeout*/0,
4305                                                          /*getcount_only*/0);
4306
4307
4308                                 xpt_setup_ccb(&cgd.ccb_h, 
4309                                               done_ccb->ccb_h.path,
4310                                               CAM_PRIORITY_NORMAL);
4311                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4312                                 xpt_action((union ccb *)&cgd);
4313
4314                                 if (scsi_extract_sense_ccb(done_ccb,
4315                                     &error_code, &sense_key, &asc, &ascq))
4316                                         have_sense = TRUE;
4317                                 else
4318                                         have_sense = FALSE;
4319
4320                                 /*
4321                                  * If we tried READ CAPACITY(16) and failed,
4322                                  * fallback to READ CAPACITY(10).
4323                                  */
4324                                 if ((state == DA_CCB_PROBE_RC16) &&
4325                                     (softc->flags & DA_FLAG_CAN_RC16) &&
4326                                     (((csio->ccb_h.status & CAM_STATUS_MASK) ==
4327                                         CAM_REQ_INVALID) ||
4328                                      ((have_sense) &&
4329                                       (error_code == SSD_CURRENT_ERROR) &&
4330                                       (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
4331                                         softc->flags &= ~DA_FLAG_CAN_RC16;
4332                                         free(rdcap, M_SCSIDA);
4333                                         xpt_release_ccb(done_ccb);
4334                                         softc->state = DA_STATE_PROBE_RC;
4335                                         xpt_schedule(periph, priority);
4336                                         return;
4337                                 }
4338
4339                                 /*
4340                                  * Attach to anything that claims to be a
4341                                  * direct access or optical disk device,
4342                                  * as long as it doesn't return a "Logical
4343                                  * unit not supported" (0x25) error.
4344                                  */
4345                                 if ((have_sense) && (asc != 0x25)
4346                                  && (error_code == SSD_CURRENT_ERROR)) {
4347                                         const char *sense_key_desc;
4348                                         const char *asc_desc;
4349
4350                                         dasetgeom(periph, 512, -1, NULL, 0);
4351                                         scsi_sense_desc(sense_key, asc, ascq,
4352                                                         &cgd.inq_data,
4353                                                         &sense_key_desc,
4354                                                         &asc_desc);
4355                                         snprintf(announce_buf,
4356                                             sizeof(announce_buf),
4357                                                 "Attempt to query device "
4358                                                 "size failed: %s, %s",
4359                                                 sense_key_desc,
4360                                                 asc_desc);
4361                                 } else { 
4362                                         if (have_sense)
4363                                                 scsi_sense_print(
4364                                                         &done_ccb->csio);
4365                                         else {
4366                                                 xpt_print(periph->path,
4367                                                     "got CAM status %#x\n",
4368                                                     done_ccb->ccb_h.status);
4369                                         }
4370
4371                                         xpt_print(periph->path, "fatal error, "
4372                                             "failed to attach to device\n");
4373
4374                                         /*
4375                                          * Free up resources.
4376                                          */
4377                                         cam_periph_invalidate(periph);
4378                                 } 
4379                         }
4380                 }
4381                 free(csio->data_ptr, M_SCSIDA);
4382                 if (announce_buf[0] != '\0' &&
4383                     ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
4384                         /*
4385                          * Create our sysctl variables, now that we know
4386                          * we have successfully attached.
4387                          */
4388                         /* increase the refcount */
4389                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
4390                                 taskqueue_enqueue(taskqueue_thread,
4391                                                   &softc->sysctl_task);
4392                                 xpt_announce_periph(periph, announce_buf);
4393                                 xpt_announce_quirks(periph, softc->quirks,
4394                                     DA_Q_BIT_STRING);
4395                         } else {
4396                                 xpt_print(periph->path, "fatal error, "
4397                                     "could not acquire reference count\n");
4398                         }
4399                 }
4400
4401                 /* We already probed the device. */
4402                 if (softc->flags & DA_FLAG_PROBED) {
4403                         daprobedone(periph, done_ccb);
4404                         return;
4405                 }
4406
4407                 /* Ensure re-probe doesn't see old delete. */
4408                 softc->delete_available = 0;
4409                 dadeleteflag(softc, DA_DELETE_ZERO, 1);
4410                 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
4411                         /*
4412                          * Based on older SBC-3 spec revisions
4413                          * any of the UNMAP methods "may" be
4414                          * available via LBP given this flag so
4415                          * we flag all of them as available and
4416                          * then remove those which further
4417                          * probes confirm aren't available
4418                          * later.
4419                          *
4420                          * We could also check readcap(16) p_type
4421                          * flag to exclude one or more invalid
4422                          * write same (X) types here
4423                          */
4424                         dadeleteflag(softc, DA_DELETE_WS16, 1);
4425                         dadeleteflag(softc, DA_DELETE_WS10, 1);
4426                         dadeleteflag(softc, DA_DELETE_UNMAP, 1);
4427
4428                         xpt_release_ccb(done_ccb);
4429                         softc->state = DA_STATE_PROBE_LBP;
4430                         xpt_schedule(periph, priority);
4431                         return;
4432                 }
4433
4434                 xpt_release_ccb(done_ccb);
4435                 softc->state = DA_STATE_PROBE_BDC;
4436                 xpt_schedule(periph, priority);
4437                 return;
4438         }
4439         case DA_CCB_PROBE_LBP:
4440         {
4441                 struct scsi_vpd_logical_block_prov *lbp;
4442
4443                 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
4444
4445                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4446                         /*
4447                          * T10/1799-D Revision 31 states at least one of these
4448                          * must be supported but we don't currently enforce this.
4449                          */
4450                         dadeleteflag(softc, DA_DELETE_WS16,
4451                                      (lbp->flags & SVPD_LBP_WS16));
4452                         dadeleteflag(softc, DA_DELETE_WS10,
4453                                      (lbp->flags & SVPD_LBP_WS10));
4454                         dadeleteflag(softc, DA_DELETE_UNMAP,
4455                                      (lbp->flags & SVPD_LBP_UNMAP));
4456                 } else {
4457                         int error;
4458                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4459                                         SF_RETRY_UA|SF_NO_PRINT);
4460                         if (error == ERESTART)
4461                                 return;
4462                         else if (error != 0) {
4463                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4464                                         /* Don't wedge this device's queue */
4465                                         cam_release_devq(done_ccb->ccb_h.path,
4466                                                          /*relsim_flags*/0,
4467                                                          /*reduction*/0,
4468                                                          /*timeout*/0,
4469                                                          /*getcount_only*/0);
4470                                 }
4471
4472                                 /*
4473                                  * Failure indicates we don't support any SBC-3
4474                                  * delete methods with UNMAP
4475                                  */
4476                         }
4477                 }
4478
4479                 free(lbp, M_SCSIDA);
4480                 xpt_release_ccb(done_ccb);
4481                 softc->state = DA_STATE_PROBE_BLK_LIMITS;
4482                 xpt_schedule(periph, priority);
4483                 return;
4484         }
4485         case DA_CCB_PROBE_BLK_LIMITS:
4486         {
4487                 struct scsi_vpd_block_limits *block_limits;
4488
4489                 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
4490
4491                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4492                         uint32_t max_txfer_len = scsi_4btoul(
4493                                 block_limits->max_txfer_len);
4494                         uint32_t max_unmap_lba_cnt = scsi_4btoul(
4495                                 block_limits->max_unmap_lba_cnt);
4496                         uint32_t max_unmap_blk_cnt = scsi_4btoul(
4497                                 block_limits->max_unmap_blk_cnt);
4498                         uint32_t unmap_gran = scsi_4btoul(
4499                                 block_limits->opt_unmap_grain);
4500                         uint32_t unmap_gran_align = scsi_4btoul(
4501                                 block_limits->unmap_grain_align);
4502                         uint64_t ws_max_blks = scsi_8btou64(
4503                                 block_limits->max_write_same_length);
4504
4505                         if (max_txfer_len != 0) {
4506                                 softc->disk->d_maxsize = MIN(softc->maxio,
4507                                     (off_t)max_txfer_len * softc->params.secsize);
4508                         }
4509
4510                         /*
4511                          * We should already support UNMAP but we check lba
4512                          * and block count to be sure
4513                          */
4514                         if (max_unmap_lba_cnt != 0x00L &&
4515                             max_unmap_blk_cnt != 0x00L) {
4516                                 softc->unmap_max_lba = max_unmap_lba_cnt;
4517                                 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
4518                                         UNMAP_MAX_RANGES);
4519                                 if (unmap_gran > 1) {
4520                                         softc->unmap_gran = unmap_gran;
4521                                         if (unmap_gran_align & 0x80000000) {
4522                                                 softc->unmap_gran_align =
4523                                                     unmap_gran_align &
4524                                                     0x7fffffff;
4525                                         }
4526                                 }
4527                         } else {
4528                                 /*
4529                                  * Unexpected UNMAP limits which means the
4530                                  * device doesn't actually support UNMAP
4531                                  */
4532                                 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
4533                         }
4534
4535                         if (ws_max_blks != 0x00L)
4536                                 softc->ws_max_blks = ws_max_blks;
4537                 } else {
4538                         int error;
4539                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4540                                         SF_RETRY_UA|SF_NO_PRINT);
4541                         if (error == ERESTART)
4542                                 return;
4543                         else if (error != 0) {
4544                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4545                                         /* Don't wedge this device's queue */
4546                                         cam_release_devq(done_ccb->ccb_h.path,
4547                                                          /*relsim_flags*/0,
4548                                                          /*reduction*/0,
4549                                                          /*timeout*/0,
4550                                                          /*getcount_only*/0);
4551                                 }
4552
4553                                 /*
4554                                  * Failure here doesn't mean UNMAP is not
4555                                  * supported as this is an optional page.
4556                                  */
4557                                 softc->unmap_max_lba = 1;
4558                                 softc->unmap_max_ranges = 1;
4559                         }
4560                 }
4561
4562                 free(block_limits, M_SCSIDA);
4563                 xpt_release_ccb(done_ccb);
4564                 softc->state = DA_STATE_PROBE_BDC;
4565                 xpt_schedule(periph, priority);
4566                 return;
4567         }
4568         case DA_CCB_PROBE_BDC:
4569         {
4570                 struct scsi_vpd_block_device_characteristics *bdc;
4571
4572                 bdc = (struct scsi_vpd_block_device_characteristics *)
4573                     csio->data_ptr;
4574
4575                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4576                         uint32_t valid_len;
4577
4578                         /*
4579                          * Disable queue sorting for non-rotational media
4580                          * by default.
4581                          */
4582                         u_int16_t old_rate = softc->disk->d_rotation_rate;
4583
4584                         valid_len = csio->dxfer_len - csio->resid;
4585                         if (SBDC_IS_PRESENT(bdc, valid_len,
4586                             medium_rotation_rate)) {
4587                                 softc->disk->d_rotation_rate =
4588                                         scsi_2btoul(bdc->medium_rotation_rate);
4589                                 if (softc->disk->d_rotation_rate ==
4590                                     SVPD_BDC_RATE_NON_ROTATING) {
4591                                         cam_iosched_set_sort_queue(
4592                                             softc->cam_iosched, 0);
4593                                         softc->rotating = 0;
4594                                 }
4595                                 if (softc->disk->d_rotation_rate != old_rate) {
4596                                         disk_attr_changed(softc->disk,
4597                                             "GEOM::rotation_rate", M_NOWAIT);
4598                                 }
4599                         }
4600                         if ((SBDC_IS_PRESENT(bdc, valid_len, flags))
4601                          && (softc->zone_mode == DA_ZONE_NONE)) {
4602                                 int ata_proto;
4603
4604                                 if (scsi_vpd_supported_page(periph,
4605                                     SVPD_ATA_INFORMATION))
4606                                         ata_proto = 1;
4607                                 else
4608                                         ata_proto = 0;
4609
4610                                 /*
4611                                  * The Zoned field will only be set for
4612                                  * Drive Managed and Host Aware drives.  If
4613                                  * they are Host Managed, the device type
4614                                  * in the standard INQUIRY data should be
4615                                  * set to T_ZBC_HM (0x14).
4616                                  */
4617                                 if ((bdc->flags & SVPD_ZBC_MASK) ==
4618                                      SVPD_HAW_ZBC) {
4619                                         softc->zone_mode = DA_ZONE_HOST_AWARE;
4620                                         softc->zone_interface = (ata_proto) ?
4621                                            DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
4622                                 } else if ((bdc->flags & SVPD_ZBC_MASK) ==
4623                                      SVPD_DM_ZBC) {
4624                                         softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
4625                                         softc->zone_interface = (ata_proto) ?
4626                                            DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
4627                                 } else if ((bdc->flags & SVPD_ZBC_MASK) != 
4628                                           SVPD_ZBC_NR) {
4629                                         xpt_print(periph->path, "Unknown zoned "
4630                                             "type %#x",
4631                                             bdc->flags & SVPD_ZBC_MASK);
4632                                 }
4633                         }
4634                 } else {
4635                         int error;
4636                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4637                                         SF_RETRY_UA|SF_NO_PRINT);
4638                         if (error == ERESTART)
4639                                 return;
4640                         else if (error != 0) {
4641                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4642                                         /* Don't wedge this device's queue */
4643                                         cam_release_devq(done_ccb->ccb_h.path,
4644                                                          /*relsim_flags*/0,
4645                                                          /*reduction*/0,
4646                                                          /*timeout*/0,
4647                                                          /*getcount_only*/0);
4648                                 }
4649                         }
4650                 }
4651
4652                 free(bdc, M_SCSIDA);
4653                 xpt_release_ccb(done_ccb);
4654                 softc->state = DA_STATE_PROBE_ATA;
4655                 xpt_schedule(periph, priority);
4656                 return;
4657         }
4658         case DA_CCB_PROBE_ATA:
4659         {
4660                 int i;
4661                 struct ata_params *ata_params;
4662                 int continue_probe;
4663                 int error;
4664                 int16_t *ptr;
4665
4666                 ata_params = (struct ata_params *)csio->data_ptr;
4667                 ptr = (uint16_t *)ata_params;
4668                 continue_probe = 0;
4669                 error = 0;
4670
4671                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4672                         uint16_t old_rate;
4673
4674                         for (i = 0; i < sizeof(*ata_params) / 2; i++)
4675                                 ptr[i] = le16toh(ptr[i]);
4676                         if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
4677                             (softc->quirks & DA_Q_NO_UNMAP) == 0) {
4678                                 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
4679                                 if (ata_params->max_dsm_blocks != 0)
4680                                         softc->trim_max_ranges = min(
4681                                           softc->trim_max_ranges,
4682                                           ata_params->max_dsm_blocks *
4683                                           ATA_DSM_BLK_RANGES);
4684                         }
4685                         /*
4686                          * Disable queue sorting for non-rotational media
4687                          * by default.
4688                          */
4689                         old_rate = softc->disk->d_rotation_rate;
4690                         softc->disk->d_rotation_rate =
4691                             ata_params->media_rotation_rate;
4692                         if (softc->disk->d_rotation_rate ==
4693                             ATA_RATE_NON_ROTATING) {
4694                                 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
4695                                 softc->rotating = 0;
4696                         }
4697                         if (softc->disk->d_rotation_rate != old_rate) {
4698                                 disk_attr_changed(softc->disk,
4699                                     "GEOM::rotation_rate", M_NOWAIT);
4700                         }
4701
4702                         if (ata_params->capabilities1 & ATA_SUPPORT_DMA)
4703                                 softc->flags |= DA_FLAG_CAN_ATA_DMA;
4704
4705                         if (ata_params->support.extension &
4706                             ATA_SUPPORT_GENLOG)
4707                                 softc->flags |= DA_FLAG_CAN_ATA_LOG;
4708
4709                         /*
4710                          * At this point, if we have a SATA host aware drive,
4711                          * we communicate via ATA passthrough unless the
4712                          * SAT layer supports ZBC -> ZAC translation.  In
4713                          * that case,
4714                          */
4715                         /*
4716                          * XXX KDM figure out how to detect a host managed
4717                          * SATA drive.
4718                          */
4719                         if (softc->zone_mode == DA_ZONE_NONE) {
4720                                 /*
4721                                  * Note that we don't override the zone
4722                                  * mode or interface if it has already been
4723                                  * set.  This is because it has either been
4724                                  * set as a quirk, or when we probed the
4725                                  * SCSI Block Device Characteristics page,
4726                                  * the zoned field was set.  The latter
4727                                  * means that the SAT layer supports ZBC to
4728                                  * ZAC translation, and we would prefer to
4729                                  * use that if it is available.
4730                                  */
4731                                 if ((ata_params->support3 &
4732                                     ATA_SUPPORT_ZONE_MASK) ==
4733                                     ATA_SUPPORT_ZONE_HOST_AWARE) {
4734                                         softc->zone_mode = DA_ZONE_HOST_AWARE;
4735                                         softc->zone_interface =
4736                                             DA_ZONE_IF_ATA_PASS;
4737                                 } else if ((ata_params->support3 &
4738                                             ATA_SUPPORT_ZONE_MASK) ==
4739                                             ATA_SUPPORT_ZONE_DEV_MANAGED) {
4740                                         softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
4741                                         softc->zone_interface =
4742                                             DA_ZONE_IF_ATA_PASS;
4743                                 }
4744                         }
4745
4746                 } else {
4747                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4748                                         SF_RETRY_UA|SF_NO_PRINT);
4749                         if (error == ERESTART)
4750                                 return;
4751                         else if (error != 0) {
4752                                 if ((done_ccb->ccb_h.status &
4753                                      CAM_DEV_QFRZN) != 0) {
4754                                         /* Don't wedge this device's queue */
4755                                         cam_release_devq(done_ccb->ccb_h.path,
4756                                                          /*relsim_flags*/0,
4757                                                          /*reduction*/0,
4758                                                          /*timeout*/0,
4759                                                          /*getcount_only*/0);
4760                                 }
4761                         }
4762                 }
4763
4764                 free(ata_params, M_SCSIDA);
4765                 if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
4766                  || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
4767                         /*
4768                          * If the ATA IDENTIFY failed, we could be talking
4769                          * to a SCSI drive, although that seems unlikely,
4770                          * since the drive did report that it supported the 
4771                          * ATA Information VPD page.  If the ATA IDENTIFY
4772                          * succeeded, and the SAT layer doesn't support
4773                          * ZBC -> ZAC translation, continue on to get the
4774                          * directory of ATA logs, and complete the rest of
4775                          * the ZAC probe.  If the SAT layer does support
4776                          * ZBC -> ZAC translation, we want to use that,
4777                          * and we'll probe the SCSI Zoned Block Device
4778                          * Characteristics VPD page next.
4779                          */
4780                         if ((error == 0)
4781                          && (softc->flags & DA_FLAG_CAN_ATA_LOG)
4782                          && (softc->zone_interface == DA_ZONE_IF_ATA_PASS))
4783                                 softc->state = DA_STATE_PROBE_ATA_LOGDIR;
4784                         else
4785                                 softc->state = DA_STATE_PROBE_ZONE;
4786                         continue_probe = 1;
4787                 }
4788                 if (continue_probe != 0) {
4789                         xpt_release_ccb(done_ccb);
4790                         xpt_schedule(periph, priority);
4791                         return;
4792                 } else
4793                         daprobedone(periph, done_ccb);
4794                 return;
4795         }
4796         case DA_CCB_PROBE_ATA_LOGDIR:
4797         {
4798                 int error;
4799
4800                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4801                         error = 0;
4802                         softc->valid_logdir_len = 0;
4803                         bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
4804                         softc->valid_logdir_len =
4805                                 csio->dxfer_len - csio->resid;
4806                         if (softc->valid_logdir_len > 0)
4807                                 bcopy(csio->data_ptr, &softc->ata_logdir,
4808                                     min(softc->valid_logdir_len,
4809                                         sizeof(softc->ata_logdir)));
4810                         /*
4811                          * Figure out whether the Identify Device log is
4812                          * supported.  The General Purpose log directory
4813                          * has a header, and lists the number of pages
4814                          * available for each GP log identified by the
4815                          * offset into the list.
4816                          */
4817                         if ((softc->valid_logdir_len >=
4818                             ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
4819                          && (le16dec(softc->ata_logdir.header) == 
4820                              ATA_GP_LOG_DIR_VERSION)
4821                          && (le16dec(&softc->ata_logdir.num_pages[
4822                              (ATA_IDENTIFY_DATA_LOG *
4823                              sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
4824                                 softc->flags |= DA_FLAG_CAN_ATA_IDLOG;
4825                         } else {
4826                                 softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
4827                         }
4828                 } else {
4829                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4830                                         SF_RETRY_UA|SF_NO_PRINT);
4831                         if (error == ERESTART)
4832                                 return;
4833                         else if (error != 0) {
4834                                 /*
4835                                  * If we can't get the ATA log directory,
4836                                  * then ATA logs are effectively not
4837                                  * supported even if the bit is set in the
4838                                  * identify data.
4839                                  */ 
4840                                 softc->flags &= ~(DA_FLAG_CAN_ATA_LOG |
4841                                                   DA_FLAG_CAN_ATA_IDLOG);
4842                                 if ((done_ccb->ccb_h.status &
4843                                      CAM_DEV_QFRZN) != 0) {
4844                                         /* Don't wedge this device's queue */
4845                                         cam_release_devq(done_ccb->ccb_h.path,
4846                                                          /*relsim_flags*/0,
4847                                                          /*reduction*/0,
4848                                                          /*timeout*/0,
4849                                                          /*getcount_only*/0);
4850                                 }
4851                         }
4852                 }
4853
4854                 free(csio->data_ptr, M_SCSIDA);
4855
4856                 if ((error == 0)
4857                  && (softc->flags & DA_FLAG_CAN_ATA_IDLOG)) {
4858                         softc->state = DA_STATE_PROBE_ATA_IDDIR;
4859                         xpt_release_ccb(done_ccb);
4860                         xpt_schedule(periph, priority);
4861                         return;
4862                 } 
4863                 daprobedone(periph, done_ccb);
4864                 return;
4865         }
4866         case DA_CCB_PROBE_ATA_IDDIR:
4867         {
4868                 int error;
4869
4870                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4871                         off_t entries_offset, max_entries;
4872                         error = 0;
4873
4874                         softc->valid_iddir_len = 0;
4875                         bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
4876                         softc->flags &= ~(DA_FLAG_CAN_ATA_SUPCAP |
4877                                           DA_FLAG_CAN_ATA_ZONE);
4878                         softc->valid_iddir_len =
4879                                 csio->dxfer_len - csio->resid;
4880                         if (softc->valid_iddir_len > 0)
4881                                 bcopy(csio->data_ptr, &softc->ata_iddir,
4882                                     min(softc->valid_iddir_len,
4883                                         sizeof(softc->ata_iddir)));
4884
4885                         entries_offset =
4886                             __offsetof(struct ata_identify_log_pages,entries);
4887                         max_entries = softc->valid_iddir_len - entries_offset;
4888                         if ((softc->valid_iddir_len > (entries_offset + 1))
4889                          && (le64dec(softc->ata_iddir.header) ==
4890                              ATA_IDLOG_REVISION)
4891                          && (softc->ata_iddir.entry_count > 0)) {
4892                                 int num_entries, i;
4893
4894                                 num_entries = softc->ata_iddir.entry_count;
4895                                 num_entries = min(num_entries,
4896                                    softc->valid_iddir_len - entries_offset);
4897                                 for (i = 0; i < num_entries &&
4898                                      i < max_entries; i++) {
4899                                         if (softc->ata_iddir.entries[i] ==
4900                                             ATA_IDL_SUP_CAP)
4901                                                 softc->flags |=
4902                                                     DA_FLAG_CAN_ATA_SUPCAP;
4903                                         else if (softc->ata_iddir.entries[i]==
4904                                                  ATA_IDL_ZDI)
4905                                                 softc->flags |=
4906                                                     DA_FLAG_CAN_ATA_ZONE;
4907
4908                                         if ((softc->flags &
4909                                              DA_FLAG_CAN_ATA_SUPCAP)
4910                                          && (softc->flags &
4911                                              DA_FLAG_CAN_ATA_ZONE))
4912                                                 break;
4913                                 }
4914                         }
4915                 } else {
4916                         error = daerror(done_ccb, CAM_RETRY_SELTO,
4917                                         SF_RETRY_UA|SF_NO_PRINT);
4918                         if (error == ERESTART)
4919                                 return;
4920                         else if (error != 0) {
4921                                 /*
4922                                  * If we can't get the ATA Identify Data log
4923                                  * directory, then it effectively isn't
4924                                  * supported even if the ATA Log directory
4925                                  * a non-zero number of pages present for
4926                                  * this log.
4927                                  */
4928                                 softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
4929                                 if ((done_ccb->ccb_h.status &
4930                                      CAM_DEV_QFRZN) != 0) {
4931                                         /* Don't wedge this device's queue */
4932                                         cam_release_devq(done_ccb->ccb_h.path,
4933                                                          /*relsim_flags*/0,
4934                                                          /*reduction*/0,
4935                                                          /*timeout*/0,
4936                                                          /*getcount_only*/0);
4937                                 }
4938                         }
4939                 }
4940
4941                 free(csio->data_ptr, M_SCSIDA);
4942
4943                 if ((error == 0)
4944                  && (softc->flags & DA_FLAG_CAN_ATA_SUPCAP)) {
4945                         softc->state = DA_STATE_PROBE_ATA_SUP;
4946                         xpt_release_ccb(done_ccb);
4947                         xpt_schedule(periph, priority);
4948                         return;
4949                 } 
4950                 daprobedone(periph, done_ccb);
4951                 return;
4952         }
4953         case DA_CCB_PROBE_ATA_SUP:
4954         {
4955                 int error;
4956
4957                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4958                         uint32_t valid_len;
4959                         size_t needed_size;
4960                         struct ata_identify_log_sup_cap *sup_cap;
4961                         error = 0;
4962
4963                         sup_cap = (struct ata_identify_log_sup_cap *)
4964                             csio->data_ptr;
4965                         valid_len = csio->dxfer_len - csio->resid;
4966                         needed_size =
4967                             __offsetof(struct ata_identify_log_sup_cap,
4968                             sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
4969                         if (valid_len >= needed_size) {
4970                                 uint64_t zoned, zac_cap;
4971
4972                                 zoned = le64dec(sup_cap->zoned_cap);
4973                                 if (zoned & ATA_ZONED_VALID) {
4974                                         /*
4975                                          * This should have already been
4976                                          * set, because this is also in the
4977                                          * ATA identify data.
4978                                          */
4979                                         if ((zoned & ATA_ZONED_MASK) ==
4980                                             ATA_SUPPORT_ZONE_HOST_AWARE)
4981                                                 softc->zone_mode =
4982                                                     DA_ZONE_HOST_AWARE;
4983                                         else if ((zoned & ATA_ZONED_MASK) ==
4984                                             ATA_SUPPORT_ZONE_DEV_MANAGED)
4985                                                 softc->zone_mode =
4986                                                     DA_ZONE_DRIVE_MANAGED;
4987                                 }
4988
4989                                 zac_cap = le64dec(sup_cap->sup_zac_cap);
4990                                 if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
4991                                         if (zac_cap & ATA_REPORT_ZONES_SUP)
4992                                                 softc->zone_flags |=
4993                                                     DA_ZONE_FLAG_RZ_SUP;
4994                                         if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
4995                                                 softc->zone_flags |=
4996                                                     DA_ZONE_FLAG_OPEN_SUP;
4997                                         if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
4998                                                 softc->zone_flags |=
4999                                                     DA_ZONE_FLAG_CLOSE_SUP;
5000                                         if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
5001                                                 softc->zone_flags |=
5002                                                     DA_ZONE_FLAG_FINISH_SUP;
5003                                         if (zac_cap & ATA_ND_RWP_SUP)
5004                                                 softc->zone_flags |=
5005                                                     DA_ZONE_FLAG_RWP_SUP;
5006                                 } else {
5007                                         /*
5008                                          * This field was introduced in
5009                                          * ACS-4, r08 on April 28th, 2015.
5010                                          * If the drive firmware was written
5011                                          * to an earlier spec, it won't have
5012                                          * the field.  So, assume all
5013                                          * commands are supported.
5014                                          */ 
5015                                         softc->zone_flags |=
5016                                             DA_ZONE_FLAG_SUP_MASK;
5017                                 }
5018                                             
5019                         }
5020                 } else {
5021                         error = daerror(done_ccb, CAM_RETRY_SELTO,
5022                                         SF_RETRY_UA|SF_NO_PRINT);
5023                         if (error == ERESTART)
5024                                 return;
5025                         else if (error != 0) {
5026                                 /*
5027                                  * If we can't get the ATA Identify Data
5028                                  * Supported Capabilities page, clear the
5029                                  * flag...
5030                                  */
5031                                 softc->flags &= ~DA_FLAG_CAN_ATA_SUPCAP;
5032                                 /*
5033                                  * And clear zone capabilities.
5034                                  */
5035                                 softc->zone_flags &= ~DA_ZONE_FLAG_SUP_MASK;
5036                                 if ((done_ccb->ccb_h.status &
5037                                      CAM_DEV_QFRZN) != 0) {
5038                                         /* Don't wedge this device's queue */
5039                                         cam_release_devq(done_ccb->ccb_h.path,
5040                                                          /*relsim_flags*/0,
5041                                                          /*reduction*/0,
5042                                                          /*timeout*/0,
5043                                                          /*getcount_only*/0);
5044                                 }
5045                         }
5046                 }
5047
5048                 free(csio->data_ptr, M_SCSIDA);
5049
5050                 if ((error == 0)
5051                  && (softc->flags & DA_FLAG_CAN_ATA_ZONE)) {
5052                         softc->state = DA_STATE_PROBE_ATA_ZONE;
5053                         xpt_release_ccb(done_ccb);
5054                         xpt_schedule(periph, priority);
5055                         return;
5056                 } 
5057                 daprobedone(periph, done_ccb);
5058                 return;
5059         }
5060         case DA_CCB_PROBE_ATA_ZONE:
5061         {
5062                 int error;
5063
5064                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5065                         struct ata_zoned_info_log *zi_log;
5066                         uint32_t valid_len;
5067                         size_t needed_size;
5068
5069                         zi_log = (struct ata_zoned_info_log *)csio->data_ptr;
5070
5071                         valid_len = csio->dxfer_len - csio->resid;
5072                         needed_size = __offsetof(struct ata_zoned_info_log,
5073                             version_info) + 1 + sizeof(zi_log->version_info);
5074                         if (valid_len >= needed_size) {
5075                                 uint64_t tmpvar;
5076
5077                                 tmpvar = le64dec(zi_log->zoned_cap);
5078                                 if (tmpvar & ATA_ZDI_CAP_VALID) {
5079                                         if (tmpvar & ATA_ZDI_CAP_URSWRZ)
5080                                                 softc->zone_flags |=
5081                                                     DA_ZONE_FLAG_URSWRZ;
5082                                         else
5083                                                 softc->zone_flags &=
5084                                                     ~DA_ZONE_FLAG_URSWRZ;
5085                                 }
5086                                 tmpvar = le64dec(zi_log->optimal_seq_zones);
5087                                 if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
5088                                         softc->zone_flags |=
5089                                             DA_ZONE_FLAG_OPT_SEQ_SET;
5090                                         softc->optimal_seq_zones = (tmpvar &
5091                                             ATA_ZDI_OPT_SEQ_MASK);
5092                                 } else {
5093                                         softc->zone_flags &=
5094                                             ~DA_ZONE_FLAG_OPT_SEQ_SET;
5095                                         softc->optimal_seq_zones = 0;
5096                                 }
5097
5098                                 tmpvar =le64dec(zi_log->optimal_nonseq_zones);
5099                                 if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
5100                                         softc->zone_flags |=
5101                                             DA_ZONE_FLAG_OPT_NONSEQ_SET;
5102                                         softc->optimal_nonseq_zones =
5103                                             (tmpvar & ATA_ZDI_OPT_NS_MASK);
5104                                 } else {
5105                                         softc->zone_flags &=
5106                                             ~DA_ZONE_FLAG_OPT_NONSEQ_SET;
5107                                         softc->optimal_nonseq_zones = 0;
5108                                 }
5109
5110                                 tmpvar = le64dec(zi_log->max_seq_req_zones);
5111                                 if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
5112                                         softc->zone_flags |=
5113                                             DA_ZONE_FLAG_MAX_SEQ_SET;
5114                                         softc->max_seq_zones =
5115                                             (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
5116                                 } else {
5117                                         softc->zone_flags &=
5118                                             ~DA_ZONE_FLAG_MAX_SEQ_SET;
5119                                         softc->max_seq_zones = 0;
5120                                 }
5121                         }
5122                 } else {
5123                         error = daerror(done_ccb, CAM_RETRY_SELTO,
5124                                         SF_RETRY_UA|SF_NO_PRINT);
5125                         if (error == ERESTART)
5126                                 return;
5127                         else if (error != 0) {
5128                                 softc->flags &= ~DA_FLAG_CAN_ATA_ZONE;
5129                                 softc->flags &= ~DA_ZONE_FLAG_SET_MASK;
5130
5131                                 if ((done_ccb->ccb_h.status &
5132                                      CAM_DEV_QFRZN) != 0) {
5133                                         /* Don't wedge this device's queue */
5134                                         cam_release_devq(done_ccb->ccb_h.path,
5135                                                          /*relsim_flags*/0,
5136                                                          /*reduction*/0,
5137                                                          /*timeout*/0,
5138                                                          /*getcount_only*/0);
5139                                 }
5140                         }
5141         
5142                 }
5143                 free(csio->data_ptr, M_SCSIDA);
5144
5145                 daprobedone(periph, done_ccb);
5146                 return;
5147         }
5148         case DA_CCB_PROBE_ZONE:
5149         {
5150                 int error;
5151
5152                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5153                         uint32_t valid_len;
5154                         size_t needed_len;
5155                         struct scsi_vpd_zoned_bdc *zoned_bdc;
5156
5157                         error = 0;
5158                         zoned_bdc = (struct scsi_vpd_zoned_bdc *)
5159                                 csio->data_ptr;
5160                         valid_len = csio->dxfer_len - csio->resid;
5161                         needed_len = __offsetof(struct scsi_vpd_zoned_bdc,
5162                             max_seq_req_zones) + 1 +
5163                             sizeof(zoned_bdc->max_seq_req_zones);
5164                         if ((valid_len >= needed_len)
5165                          && (scsi_2btoul(zoned_bdc->page_length) >=
5166                              SVPD_ZBDC_PL)) {
5167                                 if (zoned_bdc->flags & SVPD_ZBDC_URSWRZ)
5168                                         softc->zone_flags |=
5169                                             DA_ZONE_FLAG_URSWRZ;
5170                                 else
5171                                         softc->zone_flags &= 
5172                                             ~DA_ZONE_FLAG_URSWRZ;
5173                                 softc->optimal_seq_zones =
5174                                     scsi_4btoul(zoned_bdc->optimal_seq_zones);
5175                                 softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5176                                 softc->optimal_nonseq_zones = scsi_4btoul(
5177                                     zoned_bdc->optimal_nonseq_zones);
5178                                 softc->zone_flags |=
5179                                     DA_ZONE_FLAG_OPT_NONSEQ_SET;
5180                                 softc->max_seq_zones =
5181                                     scsi_4btoul(zoned_bdc->max_seq_req_zones);
5182                                 softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5183                         }
5184                         /*
5185                          * All of the zone commands are mandatory for SCSI
5186                          * devices.
5187                          *
5188                          * XXX KDM this is valid as of September 2015.
5189                          * Re-check this assumption once the SAT spec is
5190                          * updated to support SCSI ZBC to ATA ZAC mapping.
5191                          * Since ATA allows zone commands to be reported
5192                          * as supported or not, this may not necessarily
5193                          * be true for an ATA device behind a SAT (SCSI to
5194                          * ATA Translation) layer.
5195                          */
5196                         softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5197                 } else {
5198                         error = daerror(done_ccb, CAM_RETRY_SELTO,
5199                                         SF_RETRY_UA|SF_NO_PRINT);
5200                         if (error == ERESTART)
5201                                 return;
5202                         else if (error != 0) {
5203                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5204                                         /* Don't wedge this device's queue */
5205                                         cam_release_devq(done_ccb->ccb_h.path,
5206                                                          /*relsim_flags*/0,
5207                                                          /*reduction*/0,
5208                                                          /*timeout*/0,
5209                                                          /*getcount_only*/0);
5210                                 }
5211                         }
5212                 }
5213                 daprobedone(periph, done_ccb);
5214                 return;
5215         }
5216         case DA_CCB_DUMP:
5217                 /* No-op.  We're polling */
5218                 return;
5219         case DA_CCB_TUR:
5220         {
5221                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5222
5223                         if (daerror(done_ccb, CAM_RETRY_SELTO,
5224                             SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
5225                             ERESTART)
5226                                 return;
5227                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5228                                 cam_release_devq(done_ccb->ccb_h.path,
5229                                                  /*relsim_flags*/0,
5230                                                  /*reduction*/0,
5231                                                  /*timeout*/0,
5232                                                  /*getcount_only*/0);
5233                 }
5234                 xpt_release_ccb(done_ccb);
5235                 cam_periph_release_locked(periph);
5236                 return;
5237         }
5238         default:
5239                 break;
5240         }
5241         xpt_release_ccb(done_ccb);
5242 }
5243
5244 static void
5245 dareprobe(struct cam_periph *periph)
5246 {
5247         struct da_softc   *softc;
5248         cam_status status;
5249
5250         softc = (struct da_softc *)periph->softc;
5251
5252         /* Probe in progress; don't interfere. */
5253         if (softc->state != DA_STATE_NORMAL)
5254                 return;
5255
5256         status = cam_periph_acquire(periph);
5257         KASSERT(status == CAM_REQ_CMP,
5258             ("dareprobe: cam_periph_acquire failed"));
5259
5260         if (softc->flags & DA_FLAG_CAN_RC16)
5261                 softc->state = DA_STATE_PROBE_RC16;
5262         else
5263                 softc->state = DA_STATE_PROBE_RC;
5264
5265         xpt_schedule(periph, CAM_PRIORITY_DEV);
5266 }
5267
5268 static int
5269 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
5270 {
5271         struct da_softc   *softc;
5272         struct cam_periph *periph;
5273         int error, error_code, sense_key, asc, ascq;
5274
5275         periph = xpt_path_periph(ccb->ccb_h.path);
5276         softc = (struct da_softc *)periph->softc;
5277
5278         /*
5279          * Automatically detect devices that do not support
5280          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
5281          */
5282         error = 0;
5283         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
5284                 error = cmd6workaround(ccb);
5285         } else if (scsi_extract_sense_ccb(ccb,
5286             &error_code, &sense_key, &asc, &ascq)) {
5287                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
5288                         error = cmd6workaround(ccb);
5289                 /*
5290                  * If the target replied with CAPACITY DATA HAS CHANGED UA,
5291                  * query the capacity and notify upper layers.
5292                  */
5293                 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5294                     asc == 0x2A && ascq == 0x09) {
5295                         xpt_print(periph->path, "Capacity data has changed\n");
5296                         softc->flags &= ~DA_FLAG_PROBED;
5297                         dareprobe(periph);
5298                         sense_flags |= SF_NO_PRINT;
5299                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5300                     asc == 0x28 && ascq == 0x00) {
5301                         softc->flags &= ~DA_FLAG_PROBED;
5302                         disk_media_changed(softc->disk, M_NOWAIT);
5303                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5304                     asc == 0x3F && ascq == 0x03) {
5305                         xpt_print(periph->path, "INQUIRY data has changed\n");
5306                         softc->flags &= ~DA_FLAG_PROBED;
5307                         dareprobe(periph);
5308                         sense_flags |= SF_NO_PRINT;
5309                 } else if (sense_key == SSD_KEY_NOT_READY &&
5310                     asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
5311                         softc->flags |= DA_FLAG_PACK_INVALID;
5312                         disk_media_gone(softc->disk, M_NOWAIT);
5313                 }
5314         }
5315         if (error == ERESTART)
5316                 return (ERESTART);
5317
5318 #ifdef CAM_IO_STATS
5319         switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
5320         case CAM_CMD_TIMEOUT:
5321                 softc->timeouts++;
5322                 break;
5323         case CAM_REQ_ABORTED:
5324         case CAM_REQ_CMP_ERR:
5325         case CAM_REQ_TERMIO:
5326         case CAM_UNREC_HBA_ERROR:
5327         case CAM_DATA_RUN_ERR:
5328                 softc->errors++;
5329                 break;
5330         default:
5331                 break;
5332         }
5333 #endif
5334
5335         /*
5336          * XXX
5337          * Until we have a better way of doing pack validation,
5338          * don't treat UAs as errors.
5339          */
5340         sense_flags |= SF_RETRY_UA;
5341
5342         if (softc->quirks & DA_Q_RETRY_BUSY)
5343                 sense_flags |= SF_RETRY_BUSY;
5344         return(cam_periph_error(ccb, cam_flags, sense_flags,
5345                                 &softc->saved_ccb));
5346 }
5347
5348 static void
5349 damediapoll(void *arg)
5350 {
5351         struct cam_periph *periph = arg;
5352         struct da_softc *softc = periph->softc;
5353
5354         if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
5355             LIST_EMPTY(&softc->pending_ccbs)) {
5356                 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
5357                         cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
5358                         daschedule(periph);
5359                 }
5360         }
5361         /* Queue us up again */
5362         if (da_poll_period != 0)
5363                 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
5364 }
5365
5366 static void
5367 daprevent(struct cam_periph *periph, int action)
5368 {
5369         struct  da_softc *softc;
5370         union   ccb *ccb;               
5371         int     error;
5372                 
5373         softc = (struct da_softc *)periph->softc;
5374
5375         if (((action == PR_ALLOW)
5376           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
5377          || ((action == PR_PREVENT)
5378           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
5379                 return;
5380         }
5381
5382         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5383
5384         scsi_prevent(&ccb->csio,
5385                      /*retries*/1,
5386                      /*cbcfp*/dadone,
5387                      MSG_SIMPLE_Q_TAG,
5388                      action,
5389                      SSD_FULL_SIZE,
5390                      5000);
5391
5392         error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
5393             SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
5394
5395         if (error == 0) {
5396                 if (action == PR_ALLOW)
5397                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
5398                 else
5399                         softc->flags |= DA_FLAG_PACK_LOCKED;
5400         }
5401
5402         xpt_release_ccb(ccb);
5403 }
5404
5405 static void
5406 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
5407           struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
5408 {
5409         struct ccb_calc_geometry ccg;
5410         struct da_softc *softc;
5411         struct disk_params *dp;
5412         u_int lbppbe, lalba;
5413         int error;
5414
5415         softc = (struct da_softc *)periph->softc;
5416
5417         dp = &softc->params;
5418         dp->secsize = block_len;
5419         dp->sectors = maxsector + 1;
5420         if (rcaplong != NULL) {
5421                 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
5422                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
5423                 lalba &= SRC16_LALBA_A;
5424         } else {
5425                 lbppbe = 0;
5426                 lalba = 0;
5427         }
5428
5429         if (lbppbe > 0) {
5430                 dp->stripesize = block_len << lbppbe;
5431                 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
5432                     dp->stripesize;
5433         } else if (softc->quirks & DA_Q_4K) {
5434                 dp->stripesize = 4096;
5435                 dp->stripeoffset = 0;
5436         } else if (softc->unmap_gran != 0) {
5437                 dp->stripesize = block_len * softc->unmap_gran;
5438                 dp->stripeoffset = (dp->stripesize - block_len *
5439                     softc->unmap_gran_align) % dp->stripesize;
5440         } else {
5441                 dp->stripesize = 0;
5442                 dp->stripeoffset = 0;
5443         }
5444         /*
5445          * Have the controller provide us with a geometry
5446          * for this disk.  The only time the geometry
5447          * matters is when we boot and the controller
5448          * is the only one knowledgeable enough to come
5449          * up with something that will make this a bootable
5450          * device.
5451          */
5452         xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5453         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
5454         ccg.block_size = dp->secsize;
5455         ccg.volume_size = dp->sectors;
5456         ccg.heads = 0;
5457         ccg.secs_per_track = 0;
5458         ccg.cylinders = 0;
5459         xpt_action((union ccb*)&ccg);
5460         if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5461                 /*
5462                  * We don't know what went wrong here- but just pick
5463                  * a geometry so we don't have nasty things like divide
5464                  * by zero.
5465                  */
5466                 dp->heads = 255;
5467                 dp->secs_per_track = 255;
5468                 dp->cylinders = dp->sectors / (255 * 255);
5469                 if (dp->cylinders == 0) {
5470                         dp->cylinders = 1;
5471                 }
5472         } else {
5473                 dp->heads = ccg.heads;
5474                 dp->secs_per_track = ccg.secs_per_track;
5475                 dp->cylinders = ccg.cylinders;
5476         }
5477
5478         /*
5479          * If the user supplied a read capacity buffer, and if it is
5480          * different than the previous buffer, update the data in the EDT.
5481          * If it's the same, we don't bother.  This avoids sending an
5482          * update every time someone opens this device.
5483          */
5484         if ((rcaplong != NULL)
5485          && (bcmp(rcaplong, &softc->rcaplong,
5486                   min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
5487                 struct ccb_dev_advinfo cdai;
5488
5489                 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5490                 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
5491                 cdai.buftype = CDAI_TYPE_RCAPLONG;
5492                 cdai.flags = CDAI_FLAG_STORE;
5493                 cdai.bufsiz = rcap_len;
5494                 cdai.buf = (uint8_t *)rcaplong;
5495                 xpt_action((union ccb *)&cdai);
5496                 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
5497                         cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
5498                 if (cdai.ccb_h.status != CAM_REQ_CMP) {
5499                         xpt_print(periph->path, "%s: failed to set read "
5500                                   "capacity advinfo\n", __func__);
5501                         /* Use cam_error_print() to decode the status */
5502                         cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
5503                                         CAM_EPF_ALL);
5504                 } else {
5505                         bcopy(rcaplong, &softc->rcaplong,
5506                               min(sizeof(softc->rcaplong), rcap_len));
5507                 }
5508         }
5509
5510         softc->disk->d_sectorsize = softc->params.secsize;
5511         softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
5512         softc->disk->d_stripesize = softc->params.stripesize;
5513         softc->disk->d_stripeoffset = softc->params.stripeoffset;
5514         /* XXX: these are not actually "firmware" values, so they may be wrong */
5515         softc->disk->d_fwsectors = softc->params.secs_per_track;
5516         softc->disk->d_fwheads = softc->params.heads;
5517         softc->disk->d_devstat->block_size = softc->params.secsize;
5518         softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
5519
5520         error = disk_resize(softc->disk, M_NOWAIT);
5521         if (error != 0)
5522                 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
5523 }
5524
5525 static void
5526 dasendorderedtag(void *arg)
5527 {
5528         struct da_softc *softc = arg;
5529
5530         if (da_send_ordered) {
5531                 if (!LIST_EMPTY(&softc->pending_ccbs)) {
5532                         if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
5533                                 softc->flags |= DA_FLAG_NEED_OTAG;
5534                         softc->flags &= ~DA_FLAG_WAS_OTAG;
5535                 }
5536         }
5537         /* Queue us up again */
5538         callout_reset(&softc->sendordered_c,
5539             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
5540             dasendorderedtag, softc);
5541 }
5542
5543 /*
5544  * Step through all DA peripheral drivers, and if the device is still open,
5545  * sync the disk cache to physical media.
5546  */
5547 static void
5548 dashutdown(void * arg, int howto)
5549 {
5550         struct cam_periph *periph;
5551         struct da_softc *softc;
5552         union ccb *ccb;
5553         int error;
5554
5555         CAM_PERIPH_FOREACH(periph, &dadriver) {
5556                 softc = (struct da_softc *)periph->softc;
5557                 if (SCHEDULER_STOPPED()) {
5558                         /* If we paniced with the lock held, do not recurse. */
5559                         if (!cam_periph_owned(periph) &&
5560                             (softc->flags & DA_FLAG_OPEN)) {
5561                                 dadump(softc->disk, NULL, 0, 0, 0);
5562                         }
5563                         continue;
5564                 }
5565                 cam_periph_lock(periph);
5566
5567                 /*
5568                  * We only sync the cache if the drive is still open, and
5569                  * if the drive is capable of it..
5570                  */
5571                 if (((softc->flags & DA_FLAG_OPEN) == 0)
5572                  || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
5573                         cam_periph_unlock(periph);
5574                         continue;
5575                 }
5576
5577                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5578                 scsi_synchronize_cache(&ccb->csio,
5579                                        /*retries*/0,
5580                                        /*cbfcnp*/dadone,
5581                                        MSG_SIMPLE_Q_TAG,
5582                                        /*begin_lba*/0, /* whole disk */
5583                                        /*lb_count*/0,
5584                                        SSD_FULL_SIZE,
5585                                        60 * 60 * 1000);
5586
5587                 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
5588                     /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
5589                     softc->disk->d_devstat);
5590                 if (error != 0)
5591                         xpt_print(periph->path, "Synchronize cache failed\n");
5592                 xpt_release_ccb(ccb);
5593                 cam_periph_unlock(periph);
5594         }
5595 }
5596
5597 #else /* !_KERNEL */
5598
5599 /*
5600  * XXX These are only left out of the kernel build to silence warnings.  If,
5601  * for some reason these functions are used in the kernel, the ifdefs should
5602  * be moved so they are included both in the kernel and userland.
5603  */
5604 void
5605 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
5606                  void (*cbfcnp)(struct cam_periph *, union ccb *),
5607                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
5608                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
5609                  u_int32_t timeout)
5610 {
5611         struct scsi_format_unit *scsi_cmd;
5612
5613         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
5614         scsi_cmd->opcode = FORMAT_UNIT;
5615         scsi_cmd->byte2 = byte2;
5616         scsi_ulto2b(ileave, scsi_cmd->interleave);
5617
5618         cam_fill_csio(csio,
5619                       retries,
5620                       cbfcnp,
5621                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5622                       tag_action,
5623                       data_ptr,
5624                       dxfer_len,
5625                       sense_len,
5626                       sizeof(*scsi_cmd),
5627                       timeout);
5628 }
5629
5630 void
5631 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
5632                   void (*cbfcnp)(struct cam_periph *, union ccb *),
5633                   uint8_t tag_action, uint8_t list_format,
5634                   uint32_t addr_desc_index, uint8_t *data_ptr,
5635                   uint32_t dxfer_len, int minimum_cmd_size, 
5636                   uint8_t sense_len, uint32_t timeout)
5637 {
5638         uint8_t cdb_len;
5639
5640         /*
5641          * These conditions allow using the 10 byte command.  Otherwise we
5642          * need to use the 12 byte command.
5643          */
5644         if ((minimum_cmd_size <= 10)
5645          && (addr_desc_index == 0) 
5646          && (dxfer_len <= SRDD10_MAX_LENGTH)) {
5647                 struct scsi_read_defect_data_10 *cdb10;
5648
5649                 cdb10 = (struct scsi_read_defect_data_10 *)
5650                         &csio->cdb_io.cdb_bytes;
5651
5652                 cdb_len = sizeof(*cdb10);
5653                 bzero(cdb10, cdb_len);
5654                 cdb10->opcode = READ_DEFECT_DATA_10;
5655                 cdb10->format = list_format;
5656                 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
5657         } else {
5658                 struct scsi_read_defect_data_12 *cdb12;
5659
5660                 cdb12 = (struct scsi_read_defect_data_12 *)
5661                         &csio->cdb_io.cdb_bytes;
5662
5663                 cdb_len = sizeof(*cdb12);
5664                 bzero(cdb12, cdb_len);
5665                 cdb12->opcode = READ_DEFECT_DATA_12;
5666                 cdb12->format = list_format;
5667                 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
5668                 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
5669         }
5670
5671         cam_fill_csio(csio,
5672                       retries,
5673                       cbfcnp,
5674                       /*flags*/ CAM_DIR_IN,
5675                       tag_action,
5676                       data_ptr,
5677                       dxfer_len,
5678                       sense_len,
5679                       cdb_len,
5680                       timeout);
5681 }
5682
5683 void
5684 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
5685               void (*cbfcnp)(struct cam_periph *, union ccb *),
5686               u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
5687               u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
5688               u_int32_t timeout)
5689 {
5690         struct scsi_sanitize *scsi_cmd;
5691
5692         scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
5693         scsi_cmd->opcode = SANITIZE;
5694         scsi_cmd->byte2 = byte2;
5695         scsi_cmd->control = control;
5696         scsi_ulto2b(dxfer_len, scsi_cmd->length);
5697
5698         cam_fill_csio(csio,
5699                       retries,
5700                       cbfcnp,
5701                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5702                       tag_action,
5703                       data_ptr,
5704                       dxfer_len,
5705                       sense_len,
5706                       sizeof(*scsi_cmd),
5707                       timeout);
5708 }
5709
5710 #endif /* _KERNEL */
5711
5712 void
5713 scsi_zbc_out(struct ccb_scsiio *csio, uint32_t retries, 
5714              void (*cbfcnp)(struct cam_periph *, union ccb *),
5715              uint8_t tag_action, uint8_t service_action, uint64_t zone_id,
5716              uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
5717              uint8_t sense_len, uint32_t timeout)
5718 {
5719         struct scsi_zbc_out *scsi_cmd;
5720
5721         scsi_cmd = (struct scsi_zbc_out *)&csio->cdb_io.cdb_bytes;
5722         scsi_cmd->opcode = ZBC_OUT;
5723         scsi_cmd->service_action = service_action;
5724         scsi_u64to8b(zone_id, scsi_cmd->zone_id);
5725         scsi_cmd->zone_flags = zone_flags;
5726
5727         cam_fill_csio(csio,
5728                       retries,
5729                       cbfcnp,
5730                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5731                       tag_action,
5732                       data_ptr,
5733                       dxfer_len,
5734                       sense_len,
5735                       sizeof(*scsi_cmd),
5736                       timeout);
5737 }
5738
5739 void
5740 scsi_zbc_in(struct ccb_scsiio *csio, uint32_t retries, 
5741             void (*cbfcnp)(struct cam_periph *, union ccb *),
5742             uint8_t tag_action, uint8_t service_action, uint64_t zone_start_lba,
5743             uint8_t zone_options, uint8_t *data_ptr, uint32_t dxfer_len,
5744             uint8_t sense_len, uint32_t timeout)
5745 {
5746         struct scsi_zbc_in *scsi_cmd;
5747
5748         scsi_cmd = (struct scsi_zbc_in *)&csio->cdb_io.cdb_bytes;
5749         scsi_cmd->opcode = ZBC_IN;
5750         scsi_cmd->service_action = service_action;
5751         scsi_ulto4b(dxfer_len, scsi_cmd->length);
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 }