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