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