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