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