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