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