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