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