]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/scsi/scsi_da.c
MFC r327996: geom_disk / scsi_da: deny opening write-protected disks for writing
[FreeBSD/stable/10.git] / sys / cam / scsi / scsi_da.c
1 /*-
2  * Implementation of SCSI Direct Access Peripheral driver for CAM.
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33
34 #ifdef _KERNEL
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bio.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/conf.h>
43 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
46 #include <sys/cons.h>
47 #include <sys/endian.h>
48 #include <sys/proc.h>
49 #include <geom/geom.h>
50 #include <geom/geom_disk.h>
51 #endif /* _KERNEL */
52
53 #ifndef _KERNEL
54 #include <stdio.h>
55 #include <string.h>
56 #endif /* _KERNEL */
57
58 #include <cam/cam.h>
59 #include <cam/cam_ccb.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_sim.h>
63
64 #include <cam/scsi/scsi_message.h>
65
66 #ifndef _KERNEL 
67 #include <cam/scsi/scsi_da.h>
68 #endif /* !_KERNEL */
69
70 #ifdef _KERNEL
71 typedef enum {
72         DA_STATE_PROBE_WP,
73         DA_STATE_PROBE_RC,
74         DA_STATE_PROBE_RC16,
75         DA_STATE_PROBE_LBP,
76         DA_STATE_PROBE_BLK_LIMITS,
77         DA_STATE_PROBE_BDC,
78         DA_STATE_PROBE_ATA,
79         DA_STATE_NORMAL
80 } da_state;
81
82 typedef enum {
83         DA_FLAG_PACK_INVALID    = 0x001,
84         DA_FLAG_NEW_PACK        = 0x002,
85         DA_FLAG_PACK_LOCKED     = 0x004,
86         DA_FLAG_PACK_REMOVABLE  = 0x008,
87         DA_FLAG_NEED_OTAG       = 0x020,
88         DA_FLAG_WAS_OTAG        = 0x040,
89         DA_FLAG_RETRY_UA        = 0x080,
90         DA_FLAG_OPEN            = 0x100,
91         DA_FLAG_SCTX_INIT       = 0x200,
92         DA_FLAG_CAN_RC16        = 0x400,
93         DA_FLAG_PROBED          = 0x800,
94         DA_FLAG_DIRTY           = 0x1000,
95         DA_FLAG_ANNOUNCED       = 0x2000
96 } da_flags;
97
98 typedef enum {
99         DA_Q_NONE               = 0x00,
100         DA_Q_NO_SYNC_CACHE      = 0x01,
101         DA_Q_NO_6_BYTE          = 0x02,
102         DA_Q_NO_PREVENT         = 0x04,
103         DA_Q_4K                 = 0x08,
104         DA_Q_NO_RC16            = 0x10,
105         DA_Q_NO_UNMAP           = 0x20,
106         DA_Q_RETRY_BUSY         = 0x40
107 } da_quirks;
108
109 #define DA_Q_BIT_STRING         \
110         "\020"                  \
111         "\001NO_SYNC_CACHE"     \
112         "\002NO_6_BYTE"         \
113         "\003NO_PREVENT"        \
114         "\0044K"                \
115         "\005NO_RC16"           \
116         "\006NO_UNMAP"          \
117         "\007RETRY_BUSY"
118
119 typedef enum {
120         DA_CCB_PROBE_RC         = 0x01,
121         DA_CCB_PROBE_RC16       = 0x02,
122         DA_CCB_PROBE_LBP        = 0x03,
123         DA_CCB_PROBE_BLK_LIMITS = 0x04,
124         DA_CCB_PROBE_BDC        = 0x05,
125         DA_CCB_PROBE_ATA        = 0x06,
126         DA_CCB_BUFFER_IO        = 0x07,
127         DA_CCB_DUMP             = 0x0A,
128         DA_CCB_DELETE           = 0x0B,
129         DA_CCB_TUR              = 0x0C,
130         DA_CCB_PROBE_WP         = 0x12,
131         DA_CCB_TYPE_MASK        = 0x1F,
132         DA_CCB_RETRY_UA         = 0x20
133 } da_ccb_state;
134
135 /*
136  * Order here is important for method choice
137  *
138  * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
139  * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
140  * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
141  * import taking 5mins.
142  *
143  */
144 typedef enum {
145         DA_DELETE_NONE,
146         DA_DELETE_DISABLE,
147         DA_DELETE_ATA_TRIM,
148         DA_DELETE_UNMAP,
149         DA_DELETE_WS16,
150         DA_DELETE_WS10,
151         DA_DELETE_ZERO,
152         DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
153         DA_DELETE_MAX = DA_DELETE_ZERO
154 } da_delete_methods;
155
156 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
157                               struct bio *bp);
158 static da_delete_func_t da_delete_trim;
159 static da_delete_func_t da_delete_unmap;
160 static da_delete_func_t da_delete_ws;
161
162 static const void * da_delete_functions[] = {
163         NULL,
164         NULL,
165         da_delete_trim,
166         da_delete_unmap,
167         da_delete_ws,
168         da_delete_ws,
169         da_delete_ws
170 };
171
172 static const char *da_delete_method_names[] =
173     { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
174 static const char *da_delete_method_desc[] =
175     { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
176       "WRITE SAME(10) with UNMAP", "ZERO" };
177
178 /* Offsets into our private area for storing information */
179 #define ccb_state       ppriv_field0
180 #define ccb_bp          ppriv_ptr1
181
182 struct disk_params {
183         u_int8_t  heads;
184         u_int32_t cylinders;
185         u_int8_t  secs_per_track;
186         u_int32_t secsize;      /* Number of bytes/sector */
187         u_int64_t sectors;      /* total number sectors */
188         u_int     stripesize;
189         u_int     stripeoffset;
190 };
191
192 #define UNMAP_RANGE_MAX         0xffffffff
193 #define UNMAP_HEAD_SIZE         8
194 #define UNMAP_RANGE_SIZE        16
195 #define UNMAP_MAX_RANGES        2048 /* Protocol Max is 4095 */
196 #define UNMAP_BUF_SIZE          ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
197                                 UNMAP_HEAD_SIZE)
198
199 #define WS10_MAX_BLKS           0xffff
200 #define WS16_MAX_BLKS           0xffffffff
201 #define ATA_TRIM_MAX_RANGES     ((UNMAP_BUF_SIZE / \
202         (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
203
204 struct da_softc {
205         struct   bio_queue_head bio_queue;
206         struct   bio_queue_head delete_queue;
207         struct   bio_queue_head delete_run_queue;
208         LIST_HEAD(, ccb_hdr) pending_ccbs;
209         int      tur;                   /* TEST UNIT READY should be sent */
210         int      refcount;              /* Active xpt_action() calls */
211         da_state state;
212         da_flags flags; 
213         da_quirks quirks;
214         int      sort_io_queue;
215         int      minimum_cmd_size;
216         int      error_inject;
217         int      trim_max_ranges;
218         int      delete_running;
219         int      delete_available;      /* Delete methods possibly available */
220         u_int    maxio;
221         uint32_t                unmap_max_ranges;
222         uint32_t                unmap_max_lba; /* Max LBAs in UNMAP req */
223         uint64_t                ws_max_blks;
224         da_delete_methods       delete_method_pref;
225         da_delete_methods       delete_method;
226         da_delete_func_t        *delete_func;
227         struct   disk_params params;
228         struct   disk *disk;
229         union    ccb saved_ccb;
230         struct task             sysctl_task;
231         struct sysctl_ctx_list  sysctl_ctx;
232         struct sysctl_oid       *sysctl_tree;
233         struct callout          sendordered_c;
234         uint64_t wwpn;
235         uint8_t  unmap_buf[UNMAP_BUF_SIZE];
236         struct scsi_read_capacity_data_long rcaplong;
237         struct callout          mediapoll_c;
238 };
239
240 #define dadeleteflag(softc, delete_method, enable)                      \
241         if (enable) {                                                   \
242                 softc->delete_available |= (1 << delete_method);        \
243         } else {                                                        \
244                 softc->delete_available &= ~(1 << delete_method);       \
245         }
246
247 struct da_quirk_entry {
248         struct scsi_inquiry_pattern inq_pat;
249         da_quirks quirks;
250 };
251
252 static const char quantum[] = "QUANTUM";
253 static const char microp[] = "MICROP";
254
255 static struct da_quirk_entry da_quirk_table[] =
256 {
257         /* SPI, FC devices */
258         {
259                 /*
260                  * Fujitsu M2513A MO drives.
261                  * Tested devices: M2513A2 firmware versions 1200 & 1300.
262                  * (dip switch selects whether T_DIRECT or T_OPTICAL device)
263                  * Reported by: W.Scholten <whs@xs4all.nl>
264                  */
265                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
266                 /*quirks*/ DA_Q_NO_SYNC_CACHE
267         },
268         {
269                 /* See above. */
270                 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
271                 /*quirks*/ DA_Q_NO_SYNC_CACHE
272         },
273         {
274                 /*
275                  * This particular Fujitsu drive doesn't like the
276                  * synchronize cache command.
277                  * Reported by: Tom Jackson <toj@gorilla.net>
278                  */
279                 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
280                 /*quirks*/ DA_Q_NO_SYNC_CACHE
281         },
282         {
283                 /*
284                  * This drive doesn't like the synchronize cache command
285                  * either.  Reported by: Matthew Jacob <mjacob@feral.com>
286                  * in NetBSD PR kern/6027, August 24, 1998.
287                  */
288                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
289                 /*quirks*/ DA_Q_NO_SYNC_CACHE
290         },
291         {
292                 /*
293                  * This drive doesn't like the synchronize cache command
294                  * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
295                  * (PR 8882).
296                  */
297                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
298                 /*quirks*/ DA_Q_NO_SYNC_CACHE
299         },
300         {
301                 /*
302                  * Doesn't like the synchronize cache command.
303                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
304                  */
305                 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
306                 /*quirks*/ DA_Q_NO_SYNC_CACHE
307         },
308         {
309                 /*
310                  * Doesn't like the synchronize cache command.
311                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
312                  */
313                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
314                 /*quirks*/ DA_Q_NO_SYNC_CACHE
315         },
316         {
317                 /*
318                  * Doesn't like the synchronize cache command.
319                  */
320                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
321                 /*quirks*/ DA_Q_NO_SYNC_CACHE
322         },
323         {
324                 /*
325                  * Doesn't like the synchronize cache command.
326                  * Reported by: walter@pelissero.de
327                  */
328                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
329                 /*quirks*/ DA_Q_NO_SYNC_CACHE
330         },
331         {
332                 /*
333                  * Doesn't work correctly with 6 byte reads/writes.
334                  * Returns illegal request, and points to byte 9 of the
335                  * 6-byte CDB.
336                  * Reported by:  Adam McDougall <bsdx@spawnet.com>
337                  */
338                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
339                 /*quirks*/ DA_Q_NO_6_BYTE
340         },
341         {
342                 /* See above. */
343                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
344                 /*quirks*/ DA_Q_NO_6_BYTE
345         },
346         {
347                 /*
348                  * Doesn't like the synchronize cache command.
349                  * Reported by: walter@pelissero.de
350                  */
351                 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
352                 /*quirks*/ DA_Q_NO_SYNC_CACHE
353         },
354         {
355                 /*
356                  * The CISS RAID controllers do not support SYNC_CACHE
357                  */
358                 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
359                 /*quirks*/ DA_Q_NO_SYNC_CACHE
360         },
361         {
362                 /*
363                  * The STEC SSDs sometimes hang on UNMAP.
364                  */
365                 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
366                 /*quirks*/ DA_Q_NO_UNMAP
367         },
368         {
369                 /*
370                  * VMware returns BUSY status when storage has transient
371                  * connectivity problems, so better wait.
372                  */
373                 {T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*"},
374                 /*quirks*/ DA_Q_RETRY_BUSY
375         },
376         /* USB mass storage devices supported by umass(4) */
377         {
378                 /*
379                  * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
380                  * PR: kern/51675
381                  */
382                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
383                 /*quirks*/ DA_Q_NO_SYNC_CACHE
384         },
385         {
386                 /*
387                  * Power Quotient Int. (PQI) USB flash key
388                  * PR: kern/53067
389                  */
390                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
391                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
392         },
393         {
394                 /*
395                  * Creative Nomad MUVO mp3 player (USB)
396                  * PR: kern/53094
397                  */
398                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
399                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
400         },
401         {
402                 /*
403                  * Jungsoft NEXDISK USB flash key
404                  * PR: kern/54737
405                  */
406                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
407                 /*quirks*/ DA_Q_NO_SYNC_CACHE
408         },
409         {
410                 /*
411                  * FreeDik USB Mini Data Drive
412                  * PR: kern/54786
413                  */
414                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
415                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
416         },
417         {
418                 /*
419                  * Sigmatel USB Flash MP3 Player
420                  * PR: kern/57046
421                  */
422                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
423                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
424         },
425         {
426                 /*
427                  * Neuros USB Digital Audio Computer
428                  * PR: kern/63645
429                  */
430                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
431                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
432         },
433         {
434                 /*
435                  * SEAGRAND NP-900 MP3 Player
436                  * PR: kern/64563
437                  */
438                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
439                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
440         },
441         {
442                 /*
443                  * iRiver iFP MP3 player (with UMS Firmware)
444                  * PR: kern/54881, i386/63941, kern/66124
445                  */
446                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
447                 /*quirks*/ DA_Q_NO_SYNC_CACHE
448         },
449         {
450                 /*
451                  * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
452                  * PR: kern/70158
453                  */
454                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
455                 /*quirks*/ DA_Q_NO_SYNC_CACHE
456         },
457         {
458                 /*
459                  * ZICPlay USB MP3 Player with FM
460                  * PR: kern/75057
461                  */
462                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
463                 /*quirks*/ DA_Q_NO_SYNC_CACHE
464         },
465         {
466                 /*
467                  * TEAC USB floppy mechanisms
468                  */
469                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
470                 /*quirks*/ DA_Q_NO_SYNC_CACHE
471         },
472         {
473                 /*
474                  * Kingston DataTraveler II+ USB Pen-Drive.
475                  * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
476                  */
477                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
478                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
479         },
480         {
481                 /*
482                  * USB DISK Pro PMAP
483                  * Reported by: jhs
484                  * PR: usb/96381
485                  */
486                 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
487                 /*quirks*/ DA_Q_NO_SYNC_CACHE
488         },
489         {
490                 /*
491                  * Motorola E398 Mobile Phone (TransFlash memory card).
492                  * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
493                  * PR: usb/89889
494                  */
495                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
496                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
497         },
498         {
499                 /*
500                  * Qware BeatZkey! Pro
501                  * PR: usb/79164
502                  */
503                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
504                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
505         },
506         {
507                 /*
508                  * Time DPA20B 1GB MP3 Player
509                  * PR: usb/81846
510                  */
511                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
512                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
513         },
514         {
515                 /*
516                  * Samsung USB key 128Mb
517                  * PR: usb/90081
518                  */
519                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
520                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
521         },
522         {
523                 /*
524                  * Kingston DataTraveler 2.0 USB Flash memory.
525                  * PR: usb/89196
526                  */
527                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
528                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
529         },
530         {
531                 /*
532                  * Creative MUVO Slim mp3 player (USB)
533                  * PR: usb/86131
534                  */
535                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
536                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
537                 },
538         {
539                 /*
540                  * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
541                  * PR: usb/80487
542                  */
543                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
544                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
545         },
546         {
547                 /*
548                  * SanDisk Micro Cruzer 128MB
549                  * PR: usb/75970
550                  */
551                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
552                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
553         },
554         {
555                 /*
556                  * TOSHIBA TransMemory USB sticks
557                  * PR: kern/94660
558                  */
559                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
560                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
561         },
562         {
563                 /*
564                  * PNY USB 3.0 Flash Drives
565                 */
566                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
567                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
568         },
569         {
570                 /*
571                  * PNY USB Flash keys
572                  * PR: usb/75578, usb/72344, usb/65436 
573                  */
574                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
575                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
576         },
577         {
578                 /*
579                  * Genesys 6-in-1 Card Reader
580                  * PR: usb/94647
581                  */
582                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
583                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
584         },
585         {
586                 /*
587                  * Rekam Digital CAMERA
588                  * PR: usb/98713
589                  */
590                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
591                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
592         },
593         {
594                 /*
595                  * iRiver H10 MP3 player
596                  * PR: usb/102547
597                  */
598                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
599                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
600         },
601         {
602                 /*
603                  * iRiver U10 MP3 player
604                  * PR: usb/92306
605                  */
606                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
607                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
608         },
609         {
610                 /*
611                  * X-Micro Flash Disk
612                  * PR: usb/96901
613                  */
614                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
615                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
616         },
617         {
618                 /*
619                  * EasyMP3 EM732X USB 2.0 Flash MP3 Player
620                  * PR: usb/96546
621                  */
622                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
623                 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
624         },
625         {
626                 /*
627                  * Denver MP3 player
628                  * PR: usb/107101
629                  */
630                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
631                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
632         },
633         {
634                 /*
635                  * Philips USB Key Audio KEY013
636                  * PR: usb/68412
637                  */
638                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
639                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
640         },
641         {
642                 /*
643                  * JNC MP3 Player
644                  * PR: usb/94439
645                  */
646                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
647                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
648         },
649         {
650                 /*
651                  * SAMSUNG MP0402H
652                  * PR: usb/108427
653                  */
654                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
655                 /*quirks*/ DA_Q_NO_SYNC_CACHE
656         },
657         {
658                 /*
659                  * I/O Magic USB flash - Giga Bank
660                  * PR: usb/108810
661                  */
662                 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
663                 /*quirks*/ DA_Q_NO_SYNC_CACHE
664         },
665         {
666                 /*
667                  * JoyFly 128mb USB Flash Drive
668                  * PR: 96133
669                  */
670                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
671                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
672         },
673         {
674                 /*
675                  * ChipsBnk usb stick
676                  * PR: 103702
677                  */
678                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
679                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
680         },
681         {
682                 /*
683                  * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
684                  * PR: 129858
685                  */
686                 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
687                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
688         },
689         {
690                 /*
691                  * Samsung YP-U3 mp3-player
692                  * PR: 125398
693                  */
694                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
695                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
696         },
697         {
698                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
699                  "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
700         },
701         {
702                 /*
703                  * Sony Cyber-Shot DSC cameras
704                  * PR: usb/137035
705                  */
706                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
707                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
708         },
709         {
710                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
711                  "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
712         },
713         {
714                 /* At least several Transcent USB sticks lie on RC16. */
715                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
716                  "*"}, /*quirks*/ DA_Q_NO_RC16
717         },
718         {
719                 /*
720                  * I-O Data USB Flash Disk
721                  * PR: usb/211716
722                  */
723                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "I-O DATA", "USB Flash Disk*",
724                  "*"}, /*quirks*/ DA_Q_NO_RC16
725         },
726         /* ATA/SATA devices over SAS/USB/... */
727         {
728                 /* Hitachi Advanced Format (4k) drives */
729                 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
730                 /*quirks*/DA_Q_4K
731         },
732         {
733                 /* Samsung Advanced Format (4k) drives */
734                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
735                 /*quirks*/DA_Q_4K
736         },
737         {
738                 /* Samsung Advanced Format (4k) drives */
739                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
740                 /*quirks*/DA_Q_4K
741         },
742         {
743                 /* Samsung Advanced Format (4k) drives */
744                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
745                 /*quirks*/DA_Q_4K
746         },
747         {
748                 /* Samsung Advanced Format (4k) drives */
749                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
750                 /*quirks*/DA_Q_4K
751         },
752         {
753                 /* Seagate Barracuda Green Advanced Format (4k) drives */
754                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
755                 /*quirks*/DA_Q_4K
756         },
757         {
758                 /* Seagate Barracuda Green Advanced Format (4k) drives */
759                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
760                 /*quirks*/DA_Q_4K
761         },
762         {
763                 /* Seagate Barracuda Green Advanced Format (4k) drives */
764                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
765                 /*quirks*/DA_Q_4K
766         },
767         {
768                 /* Seagate Barracuda Green Advanced Format (4k) drives */
769                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
770                 /*quirks*/DA_Q_4K
771         },
772         {
773                 /* Seagate Barracuda Green Advanced Format (4k) drives */
774                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
775                 /*quirks*/DA_Q_4K
776         },
777         {
778                 /* Seagate Barracuda Green Advanced Format (4k) drives */
779                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
780                 /*quirks*/DA_Q_4K
781         },
782         {
783                 /* Seagate Momentus Advanced Format (4k) drives */
784                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
785                 /*quirks*/DA_Q_4K
786         },
787         {
788                 /* Seagate Momentus Advanced Format (4k) drives */
789                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
790                 /*quirks*/DA_Q_4K
791         },
792         {
793                 /* Seagate Momentus Advanced Format (4k) drives */
794                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
795                 /*quirks*/DA_Q_4K
796         },
797         {
798                 /* Seagate Momentus Advanced Format (4k) drives */
799                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
800                 /*quirks*/DA_Q_4K
801         },
802         {
803                 /* Seagate Momentus Advanced Format (4k) drives */
804                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
805                 /*quirks*/DA_Q_4K
806         },
807         {
808                 /* Seagate Momentus Advanced Format (4k) drives */
809                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
810                 /*quirks*/DA_Q_4K
811         },
812         {
813                 /* Seagate Momentus Advanced Format (4k) drives */
814                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
815                 /*quirks*/DA_Q_4K
816         },
817         {
818                 /* Seagate Momentus Advanced Format (4k) drives */
819                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
820                 /*quirks*/DA_Q_4K
821         },
822         {
823                 /* Seagate Momentus Advanced Format (4k) drives */
824                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
825                 /*quirks*/DA_Q_4K
826         },
827         {
828                 /* Seagate Momentus Advanced Format (4k) drives */
829                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
830                 /*quirks*/DA_Q_4K
831         },
832         {
833                 /* Seagate Momentus Advanced Format (4k) drives */
834                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
835                 /*quirks*/DA_Q_4K
836         },
837         {
838                 /* Seagate Momentus Advanced Format (4k) drives */
839                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
840                 /*quirks*/DA_Q_4K
841         },
842         {
843                 /* Seagate Momentus Advanced Format (4k) drives */
844                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
845                 /*quirks*/DA_Q_4K
846         },
847         {
848                 /* Seagate Momentus Advanced Format (4k) drives */
849                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
850                 /*quirks*/DA_Q_4K
851         },
852         {
853                 /* Seagate Momentus Thin Advanced Format (4k) drives */
854                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
855                 /*quirks*/DA_Q_4K
856         },
857         {
858                 /* Seagate Momentus Thin Advanced Format (4k) drives */
859                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
860                 /*quirks*/DA_Q_4K
861         },
862         {
863                 /* WDC Caviar Green Advanced Format (4k) drives */
864                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
865                 /*quirks*/DA_Q_4K
866         },
867         {
868                 /* WDC Caviar Green Advanced Format (4k) drives */
869                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
870                 /*quirks*/DA_Q_4K
871         },
872         {
873                 /* WDC Caviar Green Advanced Format (4k) drives */
874                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
875                 /*quirks*/DA_Q_4K
876         },
877         {
878                 /* WDC Caviar Green Advanced Format (4k) drives */
879                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
880                 /*quirks*/DA_Q_4K
881         },
882         {
883                 /* WDC Caviar Green Advanced Format (4k) drives */
884                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
885                 /*quirks*/DA_Q_4K
886         },
887         {
888                 /* WDC Caviar Green Advanced Format (4k) drives */
889                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
890                 /*quirks*/DA_Q_4K
891         },
892         {
893                 /* WDC Caviar Green Advanced Format (4k) drives */
894                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
895                 /*quirks*/DA_Q_4K
896         },
897         {
898                 /* WDC Caviar Green Advanced Format (4k) drives */
899                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
900                 /*quirks*/DA_Q_4K
901         },
902         {
903                 /* WDC Scorpio Black Advanced Format (4k) drives */
904                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
905                 /*quirks*/DA_Q_4K
906         },
907         {
908                 /* WDC Scorpio Black Advanced Format (4k) drives */
909                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
910                 /*quirks*/DA_Q_4K
911         },
912         {
913                 /* WDC Scorpio Black Advanced Format (4k) drives */
914                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
915                 /*quirks*/DA_Q_4K
916         },
917         {
918                 /* WDC Scorpio Black Advanced Format (4k) drives */
919                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
920                 /*quirks*/DA_Q_4K
921         },
922         {
923                 /* WDC Scorpio Blue Advanced Format (4k) drives */
924                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
925                 /*quirks*/DA_Q_4K
926         },
927         {
928                 /* WDC Scorpio Blue Advanced Format (4k) drives */
929                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
930                 /*quirks*/DA_Q_4K
931         },
932         {
933                 /* WDC Scorpio Blue Advanced Format (4k) drives */
934                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
935                 /*quirks*/DA_Q_4K
936         },
937         {
938                 /* WDC Scorpio Blue Advanced Format (4k) drives */
939                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
940                 /*quirks*/DA_Q_4K
941         },
942         {
943                 /*
944                  * Olympus FE-210 camera
945                  */
946                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
947                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
948         },
949         {
950                 /*
951                  * LG UP3S MP3 player
952                  */
953                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
954                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
955         },
956         {
957                 /*
958                  * Laser MP3-2GA13 MP3 player
959                  */
960                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
961                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
962         },
963         {
964                 /*
965                  * LaCie external 250GB Hard drive des by Porsche
966                  * Submitted by: Ben Stuyts <ben@altesco.nl>
967                  * PR: 121474
968                  */
969                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
970                 /*quirks*/ DA_Q_NO_SYNC_CACHE
971         },
972         /* SATA SSDs */
973         {
974                 /*
975                  * Corsair Force 2 SSDs
976                  * 4k optimised & trim only works in 4k requests + 4k aligned
977                  */
978                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
979                 /*quirks*/DA_Q_4K
980         },
981         {
982                 /*
983                  * Corsair Force 3 SSDs
984                  * 4k optimised & trim only works in 4k requests + 4k aligned
985                  */
986                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
987                 /*quirks*/DA_Q_4K
988         },
989         {
990                 /*
991                  * Corsair Neutron GTX SSDs
992                  * 4k optimised & trim only works in 4k requests + 4k aligned
993                  */
994                 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
995                 /*quirks*/DA_Q_4K
996         },
997         {
998                 /*
999                  * Corsair Force GT & GS SSDs
1000                  * 4k optimised & trim only works in 4k requests + 4k aligned
1001                  */
1002                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
1003                 /*quirks*/DA_Q_4K
1004         },
1005         {
1006                 /*
1007                  * Crucial M4 SSDs
1008                  * 4k optimised & trim only works in 4k requests + 4k aligned
1009                  */
1010                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
1011                 /*quirks*/DA_Q_4K
1012         },
1013         {
1014                 /*
1015                  * Crucial RealSSD C300 SSDs
1016                  * 4k optimised
1017                  */
1018                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
1019                 "*" }, /*quirks*/DA_Q_4K
1020         },
1021         {
1022                 /*
1023                  * Intel 320 Series SSDs
1024                  * 4k optimised & trim only works in 4k requests + 4k aligned
1025                  */
1026                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1027                 /*quirks*/DA_Q_4K
1028         },
1029         {
1030                 /*
1031                  * Intel 330 Series SSDs
1032                  * 4k optimised & trim only works in 4k requests + 4k aligned
1033                  */
1034                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1035                 /*quirks*/DA_Q_4K
1036         },
1037         {
1038                 /*
1039                  * Intel 510 Series SSDs
1040                  * 4k optimised & trim only works in 4k requests + 4k aligned
1041                  */
1042                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1043                 /*quirks*/DA_Q_4K
1044         },
1045         {
1046                 /*
1047                  * Intel 520 Series SSDs
1048                  * 4k optimised & trim only works in 4k requests + 4k aligned
1049                  */
1050                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1051                 /*quirks*/DA_Q_4K
1052         },
1053         {
1054                 /*
1055                  * Intel X25-M Series SSDs
1056                  * 4k optimised & trim only works in 4k requests + 4k aligned
1057                  */
1058                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1059                 /*quirks*/DA_Q_4K
1060         },
1061         {
1062                 /*
1063                  * Kingston E100 Series SSDs
1064                  * 4k optimised & trim only works in 4k requests + 4k aligned
1065                  */
1066                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1067                 /*quirks*/DA_Q_4K
1068         },
1069         {
1070                 /*
1071                  * Kingston HyperX 3k SSDs
1072                  * 4k optimised & trim only works in 4k requests + 4k aligned
1073                  */
1074                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1075                 /*quirks*/DA_Q_4K
1076         },
1077         {
1078                 /*
1079                  * Marvell SSDs (entry taken from OpenSolaris)
1080                  * 4k optimised & trim only works in 4k requests + 4k aligned
1081                  */
1082                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1083                 /*quirks*/DA_Q_4K
1084         },
1085         {
1086                 /*
1087                  * OCZ Agility 2 SSDs
1088                  * 4k optimised & trim only works in 4k requests + 4k aligned
1089                  */
1090                 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1091                 /*quirks*/DA_Q_4K
1092         },
1093         {
1094                 /*
1095                  * OCZ Agility 3 SSDs
1096                  * 4k optimised & trim only works in 4k requests + 4k aligned
1097                  */
1098                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1099                 /*quirks*/DA_Q_4K
1100         },
1101         {
1102                 /*
1103                  * OCZ Deneva R Series SSDs
1104                  * 4k optimised & trim only works in 4k requests + 4k aligned
1105                  */
1106                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1107                 /*quirks*/DA_Q_4K
1108         },
1109         {
1110                 /*
1111                  * OCZ Vertex 2 SSDs (inc pro series)
1112                  * 4k optimised & trim only works in 4k requests + 4k aligned
1113                  */
1114                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1115                 /*quirks*/DA_Q_4K
1116         },
1117         {
1118                 /*
1119                  * OCZ Vertex 3 SSDs
1120                  * 4k optimised & trim only works in 4k requests + 4k aligned
1121                  */
1122                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1123                 /*quirks*/DA_Q_4K
1124         },
1125         {
1126                 /*
1127                  * OCZ Vertex 4 SSDs
1128                  * 4k optimised & trim only works in 4k requests + 4k aligned
1129                  */
1130                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1131                 /*quirks*/DA_Q_4K
1132         },
1133         {
1134                 /*
1135                  * Samsung 830 Series SSDs
1136                  * 4k optimised & trim only works in 4k requests + 4k aligned
1137                  */
1138                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1139                 /*quirks*/DA_Q_4K
1140         },
1141         {
1142                 /*
1143                  * Samsung 840 SSDs
1144                  * 4k optimised & trim only works in 4k requests + 4k aligned
1145                  */
1146                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1147                 /*quirks*/DA_Q_4K
1148         },
1149         {
1150                 /*
1151                  * Samsung 850 SSDs
1152                  * 4k optimised & trim only works in 4k requests + 4k aligned
1153                  */
1154                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1155                 /*quirks*/DA_Q_4K
1156         },
1157         {
1158                 /*
1159                  * Samsung 843T Series SSDs (MZ7WD*)
1160                  * Samsung PM851 Series SSDs (MZ7TE*)
1161                  * Samsung PM853T Series SSDs (MZ7GE*)
1162                  * Samsung SM863 Series SSDs (MZ7KM*)
1163                  * 4k optimised
1164                  */
1165                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
1166                 /*quirks*/DA_Q_4K
1167         },
1168         {
1169                 /*
1170                  * SuperTalent TeraDrive CT SSDs
1171                  * 4k optimised & trim only works in 4k requests + 4k aligned
1172                  */
1173                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1174                 /*quirks*/DA_Q_4K
1175         },
1176         {
1177                 /*
1178                  * XceedIOPS SATA SSDs
1179                  * 4k optimised
1180                  */
1181                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1182                 /*quirks*/DA_Q_4K
1183         },
1184         {
1185                 /*
1186                  * Hama Innostor USB-Stick 
1187                  */
1188                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" }, 
1189                 /*quirks*/DA_Q_NO_RC16
1190         },
1191         {
1192                 /*
1193                  * MX-ES USB Drive by Mach Xtreme
1194                  */
1195                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1196                 /*quirks*/DA_Q_NO_RC16
1197         },
1198 };
1199
1200 static  disk_strategy_t dastrategy;
1201 static  dumper_t        dadump;
1202 static  periph_init_t   dainit;
1203 static  void            daasync(void *callback_arg, u_int32_t code,
1204                                 struct cam_path *path, void *arg);
1205 static  void            dasysctlinit(void *context, int pending);
1206 static  int             dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1207 static  int             dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1208 static  int             dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1209 static  void            dadeletemethodset(struct da_softc *softc,
1210                                           da_delete_methods delete_method);
1211 static  off_t           dadeletemaxsize(struct da_softc *softc,
1212                                         da_delete_methods delete_method);
1213 static  void            dadeletemethodchoose(struct da_softc *softc,
1214                                              da_delete_methods default_method);
1215 static  void            daprobedone(struct cam_periph *periph, union ccb *ccb);
1216
1217 static  periph_ctor_t   daregister;
1218 static  periph_dtor_t   dacleanup;
1219 static  periph_start_t  dastart;
1220 static  periph_oninv_t  daoninvalidate;
1221 static  void            dadone(struct cam_periph *periph,
1222                                union ccb *done_ccb);
1223 static  int             daerror(union ccb *ccb, u_int32_t cam_flags,
1224                                 u_int32_t sense_flags);
1225 static void             daprevent(struct cam_periph *periph, int action);
1226 static void             dareprobe(struct cam_periph *periph);
1227 static void             dasetgeom(struct cam_periph *periph, uint32_t block_len,
1228                                   uint64_t maxsector,
1229                                   struct scsi_read_capacity_data_long *rcaplong,
1230                                   size_t rcap_size);
1231 static timeout_t        dasendorderedtag;
1232 static void             dashutdown(void *arg, int howto);
1233 static timeout_t        damediapoll;
1234
1235 #ifndef DA_DEFAULT_POLL_PERIOD
1236 #define DA_DEFAULT_POLL_PERIOD  3
1237 #endif
1238
1239 #ifndef DA_DEFAULT_TIMEOUT
1240 #define DA_DEFAULT_TIMEOUT 60   /* Timeout in seconds */
1241 #endif
1242
1243 #ifndef DA_DEFAULT_RETRY
1244 #define DA_DEFAULT_RETRY        4
1245 #endif
1246
1247 #ifndef DA_DEFAULT_SEND_ORDERED
1248 #define DA_DEFAULT_SEND_ORDERED 1
1249 #endif
1250
1251 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1252     softc->sort_io_queue : cam_sort_io_queues)
1253
1254 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1255 static int da_retry_count = DA_DEFAULT_RETRY;
1256 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1257 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1258
1259 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1260             "CAM Direct Access Disk driver");
1261 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1262            &da_poll_period, 0, "Media polling period in seconds");
1263 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1264 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1265            &da_retry_count, 0, "Normal I/O retry count");
1266 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1267 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1268            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1269 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1270 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1271            &da_send_ordered, 0, "Send Ordered Tags");
1272 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1273
1274 /*
1275  * DA_ORDEREDTAG_INTERVAL determines how often, relative
1276  * to the default timeout, we check to see whether an ordered
1277  * tagged transaction is appropriate to prevent simple tag
1278  * starvation.  Since we'd like to ensure that there is at least
1279  * 1/2 of the timeout length left for a starved transaction to
1280  * complete after we've sent an ordered tag, we must poll at least
1281  * four times in every timeout period.  This takes care of the worst
1282  * case where a starved transaction starts during an interval that
1283  * meets the requirement "don't send an ordered tag" test so it takes
1284  * us two intervals to determine that a tag must be sent.
1285  */
1286 #ifndef DA_ORDEREDTAG_INTERVAL
1287 #define DA_ORDEREDTAG_INTERVAL 4
1288 #endif
1289
1290 static struct periph_driver dadriver =
1291 {
1292         dainit, "da",
1293         TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1294 };
1295
1296 PERIPHDRIVER_DECLARE(da, dadriver);
1297
1298 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1299
1300 static int
1301 daopen(struct disk *dp)
1302 {
1303         struct cam_periph *periph;
1304         struct da_softc *softc;
1305         int error;
1306
1307         periph = (struct cam_periph *)dp->d_drv1;
1308         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1309                 return (ENXIO);
1310         }
1311
1312         cam_periph_lock(periph);
1313         if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1314                 cam_periph_unlock(periph);
1315                 cam_periph_release(periph);
1316                 return (error);
1317         }
1318
1319         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1320             ("daopen\n"));
1321
1322         softc = (struct da_softc *)periph->softc;
1323         dareprobe(periph);
1324
1325         /* Wait for the disk size update.  */
1326         error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1327             "dareprobe", 0);
1328         if (error != 0)
1329                 xpt_print(periph->path, "unable to retrieve capacity data\n");
1330
1331         if (periph->flags & CAM_PERIPH_INVALID)
1332                 error = ENXIO;
1333
1334         if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1335             (softc->quirks & DA_Q_NO_PREVENT) == 0)
1336                 daprevent(periph, PR_PREVENT);
1337
1338         if (error == 0) {
1339                 softc->flags &= ~DA_FLAG_PACK_INVALID;
1340                 softc->flags |= DA_FLAG_OPEN;
1341         }
1342
1343         cam_periph_unhold(periph);
1344         cam_periph_unlock(periph);
1345
1346         if (error != 0)
1347                 cam_periph_release(periph);
1348
1349         return (error);
1350 }
1351
1352 static int
1353 daclose(struct disk *dp)
1354 {
1355         struct  cam_periph *periph;
1356         struct  da_softc *softc;
1357         union   ccb *ccb;
1358         int error;
1359
1360         periph = (struct cam_periph *)dp->d_drv1;
1361         softc = (struct da_softc *)periph->softc;
1362         cam_periph_lock(periph);
1363         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1364             ("daclose\n"));
1365
1366         if (cam_periph_hold(periph, PRIBIO) == 0) {
1367
1368                 /* Flush disk cache. */
1369                 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1370                     (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1371                     (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1372                         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1373                         scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1374                             /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1375                             /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1376                             5 * 60 * 1000);
1377                         error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1378                             /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1379                             softc->disk->d_devstat);
1380                         softc->flags &= ~DA_FLAG_DIRTY;
1381                         xpt_release_ccb(ccb);
1382                 }
1383
1384                 /* Allow medium removal. */
1385                 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1386                     (softc->quirks & DA_Q_NO_PREVENT) == 0)
1387                         daprevent(periph, PR_ALLOW);
1388
1389                 cam_periph_unhold(periph);
1390         }
1391
1392         /*
1393          * If we've got removeable media, mark the blocksize as
1394          * unavailable, since it could change when new media is
1395          * inserted.
1396          */
1397         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1398                 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1399
1400         softc->flags &= ~DA_FLAG_OPEN;
1401         while (softc->refcount != 0)
1402                 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1403         cam_periph_unlock(periph);
1404         cam_periph_release(periph);
1405         return (0);
1406 }
1407
1408 static void
1409 daschedule(struct cam_periph *periph)
1410 {
1411         struct da_softc *softc = (struct da_softc *)periph->softc;
1412
1413         if (softc->state != DA_STATE_NORMAL)
1414                 return;
1415
1416         /* Check if we have more work to do. */
1417         if (bioq_first(&softc->bio_queue) ||
1418             (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1419             softc->tur) {
1420                 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1421         }
1422 }
1423
1424 /*
1425  * Actually translate the requested transfer into one the physical driver
1426  * can understand.  The transfer is described by a buf and will include
1427  * only one physical transfer.
1428  */
1429 static void
1430 dastrategy(struct bio *bp)
1431 {
1432         struct cam_periph *periph;
1433         struct da_softc *softc;
1434         
1435         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1436         softc = (struct da_softc *)periph->softc;
1437
1438         cam_periph_lock(periph);
1439
1440         /*
1441          * If the device has been made invalid, error out
1442          */
1443         if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1444                 cam_periph_unlock(periph);
1445                 biofinish(bp, NULL, ENXIO);
1446                 return;
1447         }
1448
1449         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1450
1451         /*
1452          * Place it in the queue of disk activities for this disk
1453          */
1454         if (bp->bio_cmd == BIO_DELETE) {
1455                 bioq_disksort(&softc->delete_queue, bp);
1456         } else if (DA_SIO) {
1457                 bioq_disksort(&softc->bio_queue, bp);
1458         } else {
1459                 bioq_insert_tail(&softc->bio_queue, bp);
1460         }
1461
1462         /*
1463          * Schedule ourselves for performing the work.
1464          */
1465         daschedule(periph);
1466         cam_periph_unlock(periph);
1467
1468         return;
1469 }
1470
1471 static int
1472 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1473 {
1474         struct      cam_periph *periph;
1475         struct      da_softc *softc;
1476         u_int       secsize;
1477         struct      ccb_scsiio csio;
1478         struct      disk *dp;
1479         int         error = 0;
1480
1481         dp = arg;
1482         periph = dp->d_drv1;
1483         softc = (struct da_softc *)periph->softc;
1484         cam_periph_lock(periph);
1485         secsize = softc->params.secsize;
1486         
1487         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1488                 cam_periph_unlock(periph);
1489                 return (ENXIO);
1490         }
1491
1492         if (length > 0) {
1493                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1494                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1495                 scsi_read_write(&csio,
1496                                 /*retries*/0,
1497                                 dadone,
1498                                 MSG_ORDERED_Q_TAG,
1499                                 /*read*/SCSI_RW_WRITE,
1500                                 /*byte2*/0,
1501                                 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1502                                 offset / secsize,
1503                                 length / secsize,
1504                                 /*data_ptr*/(u_int8_t *) virtual,
1505                                 /*dxfer_len*/length,
1506                                 /*sense_len*/SSD_FULL_SIZE,
1507                                 da_default_timeout * 1000);
1508                 xpt_polled_action((union ccb *)&csio);
1509
1510                 error = cam_periph_error((union ccb *)&csio,
1511                     0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1512                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1513                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1514                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1515                 if (error != 0)
1516                         printf("Aborting dump due to I/O error.\n");
1517                 cam_periph_unlock(periph);
1518                 return (error);
1519         }
1520                 
1521         /*
1522          * Sync the disk cache contents to the physical media.
1523          */
1524         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1525
1526                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1527                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1528                 scsi_synchronize_cache(&csio,
1529                                        /*retries*/0,
1530                                        /*cbfcnp*/dadone,
1531                                        MSG_SIMPLE_Q_TAG,
1532                                        /*begin_lba*/0,/* Cover the whole disk */
1533                                        /*lb_count*/0,
1534                                        SSD_FULL_SIZE,
1535                                        5 * 60 * 1000);
1536                 xpt_polled_action((union ccb *)&csio);
1537
1538                 error = cam_periph_error((union ccb *)&csio,
1539                     0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1540                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1541                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1542                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1543                 if (error != 0)
1544                         xpt_print(periph->path, "Synchronize cache failed\n");
1545         }
1546         cam_periph_unlock(periph);
1547         return (error);
1548 }
1549
1550 static int
1551 dagetattr(struct bio *bp)
1552 {
1553         int ret;
1554         struct cam_periph *periph;
1555
1556         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1557         cam_periph_lock(periph);
1558         ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1559             periph->path);
1560         cam_periph_unlock(periph);
1561         if (ret == 0)
1562                 bp->bio_completed = bp->bio_length;
1563         return ret;
1564 }
1565
1566 static void
1567 dainit(void)
1568 {
1569         cam_status status;
1570
1571         /*
1572          * Install a global async callback.  This callback will
1573          * receive async callbacks like "new device found".
1574          */
1575         status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1576
1577         if (status != CAM_REQ_CMP) {
1578                 printf("da: Failed to attach master async callback "
1579                        "due to status 0x%x!\n", status);
1580         } else if (da_send_ordered) {
1581
1582                 /* Register our shutdown event handler */
1583                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 
1584                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1585                     printf("dainit: shutdown event registration failed!\n");
1586         }
1587 }
1588
1589 /*
1590  * Callback from GEOM, called when it has finished cleaning up its
1591  * resources.
1592  */
1593 static void
1594 dadiskgonecb(struct disk *dp)
1595 {
1596         struct cam_periph *periph;
1597
1598         periph = (struct cam_periph *)dp->d_drv1;
1599         cam_periph_release(periph);
1600 }
1601
1602 static void
1603 daoninvalidate(struct cam_periph *periph)
1604 {
1605         struct da_softc *softc;
1606
1607         softc = (struct da_softc *)periph->softc;
1608
1609         /*
1610          * De-register any async callbacks.
1611          */
1612         xpt_register_async(0, daasync, periph, periph->path);
1613
1614         softc->flags |= DA_FLAG_PACK_INVALID;
1615
1616         /*
1617          * Return all queued I/O with ENXIO.
1618          * XXX Handle any transactions queued to the card
1619          *     with XPT_ABORT_CCB.
1620          */
1621         bioq_flush(&softc->bio_queue, NULL, ENXIO);
1622         bioq_flush(&softc->delete_queue, NULL, ENXIO);
1623
1624         /*
1625          * Tell GEOM that we've gone away, we'll get a callback when it is
1626          * done cleaning up its resources.
1627          */
1628         disk_gone(softc->disk);
1629 }
1630
1631 static void
1632 dacleanup(struct cam_periph *periph)
1633 {
1634         struct da_softc *softc;
1635
1636         softc = (struct da_softc *)periph->softc;
1637
1638         cam_periph_unlock(periph);
1639
1640         /*
1641          * If we can't free the sysctl tree, oh well...
1642          */
1643         if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1644             && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1645                 xpt_print(periph->path, "can't remove sysctl context\n");
1646         }
1647
1648         callout_drain(&softc->mediapoll_c);
1649         disk_destroy(softc->disk);
1650         callout_drain(&softc->sendordered_c);
1651         free(softc, M_DEVBUF);
1652         cam_periph_lock(periph);
1653 }
1654
1655 static void
1656 daasync(void *callback_arg, u_int32_t code,
1657         struct cam_path *path, void *arg)
1658 {
1659         struct cam_periph *periph;
1660         struct da_softc *softc;
1661
1662         periph = (struct cam_periph *)callback_arg;
1663         switch (code) {
1664         case AC_FOUND_DEVICE:
1665         {
1666                 struct ccb_getdev *cgd;
1667                 cam_status status;
1668  
1669                 cgd = (struct ccb_getdev *)arg;
1670                 if (cgd == NULL)
1671                         break;
1672
1673                 if (cgd->protocol != PROTO_SCSI)
1674                         break;
1675                 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
1676                         break;
1677                 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1678                     && SID_TYPE(&cgd->inq_data) != T_RBC
1679                     && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1680                         break;
1681
1682                 /*
1683                  * Allocate a peripheral instance for
1684                  * this device and start the probe
1685                  * process.
1686                  */
1687                 status = cam_periph_alloc(daregister, daoninvalidate,
1688                                           dacleanup, dastart,
1689                                           "da", CAM_PERIPH_BIO,
1690                                           path, daasync,
1691                                           AC_FOUND_DEVICE, cgd);
1692
1693                 if (status != CAM_REQ_CMP
1694                  && status != CAM_REQ_INPROG)
1695                         printf("daasync: Unable to attach to new device "
1696                                 "due to status 0x%x\n", status);
1697                 return;
1698         }
1699         case AC_ADVINFO_CHANGED:
1700         {
1701                 uintptr_t buftype;
1702
1703                 buftype = (uintptr_t)arg;
1704                 if (buftype == CDAI_TYPE_PHYS_PATH) {
1705                         struct da_softc *softc;
1706
1707                         softc = periph->softc;
1708                         disk_attr_changed(softc->disk, "GEOM::physpath",
1709                                           M_NOWAIT);
1710                 }
1711                 break;
1712         }
1713         case AC_UNIT_ATTENTION:
1714         {
1715                 union ccb *ccb;
1716                 int error_code, sense_key, asc, ascq;
1717
1718                 softc = (struct da_softc *)periph->softc;
1719                 ccb = (union ccb *)arg;
1720
1721                 /*
1722                  * Handle all UNIT ATTENTIONs except our own,
1723                  * as they will be handled by daerror().
1724                  */
1725                 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1726                     scsi_extract_sense_ccb(ccb,
1727                      &error_code, &sense_key, &asc, &ascq)) {
1728                         if (asc == 0x2A && ascq == 0x09) {
1729                                 xpt_print(ccb->ccb_h.path,
1730                                     "Capacity data has changed\n");
1731                                 softc->flags &= ~DA_FLAG_PROBED;
1732                                 dareprobe(periph);
1733                         } else if (asc == 0x28 && ascq == 0x00) {
1734                                 softc->flags &= ~DA_FLAG_PROBED;
1735                                 disk_media_changed(softc->disk, M_NOWAIT);
1736                         } else if (asc == 0x3F && ascq == 0x03) {
1737                                 xpt_print(ccb->ccb_h.path,
1738                                     "INQUIRY data has changed\n");
1739                                 softc->flags &= ~DA_FLAG_PROBED;
1740                                 dareprobe(periph);
1741                         }
1742                 }
1743                 cam_periph_async(periph, code, path, arg);
1744                 break;
1745         }
1746         case AC_SCSI_AEN:
1747                 softc = (struct da_softc *)periph->softc;
1748                 if (!softc->tur) {
1749                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1750                                 softc->tur = 1;
1751                                 daschedule(periph);
1752                         }
1753                 }
1754                 /* FALLTHROUGH */
1755         case AC_SENT_BDR:
1756         case AC_BUS_RESET:
1757         {
1758                 struct ccb_hdr *ccbh;
1759
1760                 softc = (struct da_softc *)periph->softc;
1761                 /*
1762                  * Don't fail on the expected unit attention
1763                  * that will occur.
1764                  */
1765                 softc->flags |= DA_FLAG_RETRY_UA;
1766                 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1767                         ccbh->ccb_state |= DA_CCB_RETRY_UA;
1768                 break;
1769         }
1770         case AC_INQ_CHANGED:
1771                 softc = (struct da_softc *)periph->softc;
1772                 softc->flags &= ~DA_FLAG_PROBED;
1773                 dareprobe(periph);
1774                 break;
1775         default:
1776                 break;
1777         }
1778         cam_periph_async(periph, code, path, arg);
1779 }
1780
1781 static void
1782 dasysctlinit(void *context, int pending)
1783 {
1784         struct cam_periph *periph;
1785         struct da_softc *softc;
1786         char tmpstr[80], tmpstr2[80];
1787         struct ccb_trans_settings cts;
1788
1789         periph = (struct cam_periph *)context;
1790         /*
1791          * periph was held for us when this task was enqueued
1792          */
1793         if (periph->flags & CAM_PERIPH_INVALID) {
1794                 cam_periph_release(periph);
1795                 return;
1796         }
1797
1798         softc = (struct da_softc *)periph->softc;
1799         snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1800         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1801
1802         sysctl_ctx_init(&softc->sysctl_ctx);
1803         softc->flags |= DA_FLAG_SCTX_INIT;
1804         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1805                 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1806                 CTLFLAG_RD, 0, tmpstr);
1807         if (softc->sysctl_tree == NULL) {
1808                 printf("dasysctlinit: unable to allocate sysctl tree\n");
1809                 cam_periph_release(periph);
1810                 return;
1811         }
1812
1813         /*
1814          * Now register the sysctl handler, so the user can change the value on
1815          * the fly.
1816          */
1817         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1818                 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RWTUN,
1819                 softc, 0, dadeletemethodsysctl, "A",
1820                 "BIO_DELETE execution method");
1821         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1822                 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1823                 softc, 0, dadeletemaxsysctl, "Q",
1824                 "Maximum BIO_DELETE size");
1825         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1826                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1827                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1828                 "Minimum CDB size");
1829         SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1830                 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1831                 "Sort IO queue to try and optimise disk access patterns");
1832
1833         SYSCTL_ADD_INT(&softc->sysctl_ctx,
1834                        SYSCTL_CHILDREN(softc->sysctl_tree),
1835                        OID_AUTO,
1836                        "error_inject",
1837                        CTLFLAG_RW,
1838                        &softc->error_inject,
1839                        0,
1840                        "error_inject leaf");
1841
1842
1843         /*
1844          * Add some addressing info.
1845          */
1846         memset(&cts, 0, sizeof (cts));
1847         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1848         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1849         cts.type = CTS_TYPE_CURRENT_SETTINGS;
1850         cam_periph_lock(periph);
1851         xpt_action((union ccb *)&cts);
1852         cam_periph_unlock(periph);
1853         if (cts.ccb_h.status != CAM_REQ_CMP) {
1854                 cam_periph_release(periph);
1855                 return;
1856         }
1857         if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1858                 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1859                 if (fc->valid & CTS_FC_VALID_WWPN) {
1860                         softc->wwpn = fc->wwpn;
1861                         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1862                             SYSCTL_CHILDREN(softc->sysctl_tree),
1863                             OID_AUTO, "wwpn", CTLFLAG_RD,
1864                             &softc->wwpn, "World Wide Port Name");
1865                 }
1866         }
1867         cam_periph_release(periph);
1868 }
1869
1870 static int
1871 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1872 {
1873         int error;
1874         uint64_t value;
1875         struct da_softc *softc;
1876
1877         softc = (struct da_softc *)arg1;
1878
1879         value = softc->disk->d_delmaxsize;
1880         error = sysctl_handle_64(oidp, &value, 0, req);
1881         if ((error != 0) || (req->newptr == NULL))
1882                 return (error);
1883
1884         /* only accept values smaller than the calculated value */
1885         if (value > dadeletemaxsize(softc, softc->delete_method)) {
1886                 return (EINVAL);
1887         }
1888         softc->disk->d_delmaxsize = value;
1889
1890         return (0);
1891 }
1892
1893 static int
1894 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1895 {
1896         int error, value;
1897
1898         value = *(int *)arg1;
1899
1900         error = sysctl_handle_int(oidp, &value, 0, req);
1901
1902         if ((error != 0)
1903          || (req->newptr == NULL))
1904                 return (error);
1905
1906         /*
1907          * Acceptable values here are 6, 10, 12 or 16.
1908          */
1909         if (value < 6)
1910                 value = 6;
1911         else if ((value > 6)
1912               && (value <= 10))
1913                 value = 10;
1914         else if ((value > 10)
1915               && (value <= 12))
1916                 value = 12;
1917         else if (value > 12)
1918                 value = 16;
1919
1920         *(int *)arg1 = value;
1921
1922         return (0);
1923 }
1924
1925 static void
1926 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1927 {
1928
1929         softc->delete_method = delete_method;
1930         softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1931         softc->delete_func = da_delete_functions[delete_method];
1932
1933         if (softc->delete_method > DA_DELETE_DISABLE)
1934                 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1935         else
1936                 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1937 }
1938
1939 static off_t
1940 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1941 {
1942         off_t sectors;
1943
1944         switch(delete_method) {
1945         case DA_DELETE_UNMAP:
1946                 sectors = (off_t)softc->unmap_max_lba;
1947                 break;
1948         case DA_DELETE_ATA_TRIM:
1949                 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1950                 break;
1951         case DA_DELETE_WS16:
1952                 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
1953                 break;
1954         case DA_DELETE_ZERO:
1955         case DA_DELETE_WS10:
1956                 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
1957                 break;
1958         default:
1959                 return 0;
1960         }
1961
1962         return (off_t)softc->params.secsize *
1963             omin(sectors, softc->params.sectors);
1964 }
1965
1966 static void
1967 daprobedone(struct cam_periph *periph, union ccb *ccb)
1968 {
1969         struct da_softc *softc;
1970
1971         softc = (struct da_softc *)periph->softc;
1972
1973         dadeletemethodchoose(softc, DA_DELETE_NONE);
1974
1975         if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1976                 char buf[80];
1977                 int i, sep;
1978
1979                 snprintf(buf, sizeof(buf), "Delete methods: <");
1980                 sep = 0;
1981                 for (i = 0; i <= DA_DELETE_MAX; i++) {
1982                         if ((softc->delete_available & (1 << i)) == 0 &&
1983                             i != softc->delete_method)
1984                                 continue;
1985                         if (sep)
1986                                 strlcat(buf, ",", sizeof(buf));
1987                         strlcat(buf, da_delete_method_names[i],
1988                             sizeof(buf));
1989                         if (i == softc->delete_method)
1990                                 strlcat(buf, "(*)", sizeof(buf));
1991                         sep = 1;
1992                 }
1993                 strlcat(buf, ">", sizeof(buf));
1994                 printf("%s%d: %s\n", periph->periph_name,
1995                     periph->unit_number, buf);
1996         }
1997
1998         /*
1999          * Since our peripheral may be invalidated by an error
2000          * above or an external event, we must release our CCB
2001          * before releasing the probe lock on the peripheral.
2002          * The peripheral will only go away once the last lock
2003          * is removed, and we need it around for the CCB release
2004          * operation.
2005          */
2006         xpt_release_ccb(ccb);
2007         softc->state = DA_STATE_NORMAL;
2008         softc->flags |= DA_FLAG_PROBED;
2009         daschedule(periph);
2010         wakeup(&softc->disk->d_mediasize);
2011         if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2012                 softc->flags |= DA_FLAG_ANNOUNCED;
2013                 cam_periph_unhold(periph);
2014         } else
2015                 cam_periph_release_locked(periph);
2016 }
2017
2018 static void
2019 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2020 {
2021         int i, methods;
2022
2023         /* If available, prefer the method requested by user. */
2024         i = softc->delete_method_pref;
2025         methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2026         if (methods & (1 << i)) {
2027                 dadeletemethodset(softc, i);
2028                 return;
2029         }
2030
2031         /* Use the pre-defined order to choose the best performing delete. */
2032         for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2033                 if (i == DA_DELETE_ZERO)
2034                         continue;
2035                 if (softc->delete_available & (1 << i)) {
2036                         dadeletemethodset(softc, i);
2037                         return;
2038                 }
2039         }
2040
2041         /* Fallback to default. */
2042         dadeletemethodset(softc, default_method);
2043 }
2044
2045 static int
2046 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2047 {
2048         char buf[16];
2049         const char *p;
2050         struct da_softc *softc;
2051         int i, error, methods, value;
2052
2053         softc = (struct da_softc *)arg1;
2054
2055         value = softc->delete_method;
2056         if (value < 0 || value > DA_DELETE_MAX)
2057                 p = "UNKNOWN";
2058         else
2059                 p = da_delete_method_names[value];
2060         strncpy(buf, p, sizeof(buf));
2061         error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2062         if (error != 0 || req->newptr == NULL)
2063                 return (error);
2064         methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2065         for (i = 0; i <= DA_DELETE_MAX; i++) {
2066                 if (strcmp(buf, da_delete_method_names[i]) == 0)
2067                         break;
2068         }
2069         if (i > DA_DELETE_MAX)
2070                 return (EINVAL);
2071         softc->delete_method_pref = i;
2072         dadeletemethodchoose(softc, DA_DELETE_NONE);
2073         return (0);
2074 }
2075
2076 static cam_status
2077 daregister(struct cam_periph *periph, void *arg)
2078 {
2079         struct da_softc *softc;
2080         struct ccb_pathinq cpi;
2081         struct ccb_getdev *cgd;
2082         char tmpstr[80];
2083         caddr_t match;
2084
2085         cgd = (struct ccb_getdev *)arg;
2086         if (cgd == NULL) {
2087                 printf("daregister: no getdev CCB, can't register device\n");
2088                 return(CAM_REQ_CMP_ERR);
2089         }
2090
2091         softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2092             M_NOWAIT|M_ZERO);
2093
2094         if (softc == NULL) {
2095                 printf("daregister: Unable to probe new device. "
2096                        "Unable to allocate softc\n");                           
2097                 return(CAM_REQ_CMP_ERR);
2098         }
2099
2100         LIST_INIT(&softc->pending_ccbs);
2101         softc->state = DA_STATE_PROBE_WP;
2102         bioq_init(&softc->bio_queue);
2103         bioq_init(&softc->delete_queue);
2104         bioq_init(&softc->delete_run_queue);
2105         if (SID_IS_REMOVABLE(&cgd->inq_data))
2106                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2107         softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2108         softc->unmap_max_lba = UNMAP_RANGE_MAX;
2109         softc->ws_max_blks = WS16_MAX_BLKS;
2110         softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2111         softc->sort_io_queue = -1;
2112
2113         periph->softc = softc;
2114
2115         /*
2116          * See if this device has any quirks.
2117          */
2118         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2119                                (caddr_t)da_quirk_table,
2120                                sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2121                                sizeof(*da_quirk_table), scsi_inquiry_match);
2122
2123         if (match != NULL)
2124                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2125         else
2126                 softc->quirks = DA_Q_NONE;
2127
2128         /* Check if the SIM does not want 6 byte commands */
2129         bzero(&cpi, sizeof(cpi));
2130         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2131         cpi.ccb_h.func_code = XPT_PATH_INQ;
2132         xpt_action((union ccb *)&cpi);
2133         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2134                 softc->quirks |= DA_Q_NO_6_BYTE;
2135
2136         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2137
2138         /*
2139          * Take an exclusive refcount on the periph while dastart is called
2140          * to finish the probe.  The reference will be dropped in dadone at
2141          * the end of probe.
2142          */
2143         (void)cam_periph_hold(periph, PRIBIO);
2144
2145         /*
2146          * Schedule a periodic event to occasionally send an
2147          * ordered tag to a device.
2148          */
2149         callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2150         callout_reset(&softc->sendordered_c,
2151             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2152             dasendorderedtag, softc);
2153
2154         cam_periph_unlock(periph);
2155         /*
2156          * RBC devices don't have to support READ(6), only READ(10).
2157          */
2158         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2159                 softc->minimum_cmd_size = 10;
2160         else
2161                 softc->minimum_cmd_size = 6;
2162
2163         /*
2164          * Load the user's default, if any.
2165          */
2166         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2167                  periph->unit_number);
2168         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2169
2170         /*
2171          * 6, 10, 12 and 16 are the currently permissible values.
2172          */
2173         if (softc->minimum_cmd_size < 6)
2174                 softc->minimum_cmd_size = 6;
2175         else if ((softc->minimum_cmd_size > 6)
2176               && (softc->minimum_cmd_size <= 10))
2177                 softc->minimum_cmd_size = 10;
2178         else if ((softc->minimum_cmd_size > 10)
2179               && (softc->minimum_cmd_size <= 12))
2180                 softc->minimum_cmd_size = 12;
2181         else if (softc->minimum_cmd_size > 12)
2182                 softc->minimum_cmd_size = 16;
2183
2184         /* Predict whether device may support READ CAPACITY(16). */
2185         if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2186             (softc->quirks & DA_Q_NO_RC16) == 0) {
2187                 softc->flags |= DA_FLAG_CAN_RC16;
2188         }
2189
2190         /*
2191          * Register this media as a disk.
2192          */
2193         softc->disk = disk_alloc();
2194         softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2195                           periph->unit_number, 0,
2196                           DEVSTAT_BS_UNAVAILABLE,
2197                           SID_TYPE(&cgd->inq_data) |
2198                           XPORT_DEVSTAT_TYPE(cpi.transport),
2199                           DEVSTAT_PRIORITY_DISK);
2200         softc->disk->d_open = daopen;
2201         softc->disk->d_close = daclose;
2202         softc->disk->d_strategy = dastrategy;
2203         softc->disk->d_dump = dadump;
2204         softc->disk->d_getattr = dagetattr;
2205         softc->disk->d_gone = dadiskgonecb;
2206         softc->disk->d_name = "da";
2207         softc->disk->d_drv1 = periph;
2208         if (cpi.maxio == 0)
2209                 softc->maxio = DFLTPHYS;        /* traditional default */
2210         else if (cpi.maxio > MAXPHYS)
2211                 softc->maxio = MAXPHYS;         /* for safety */
2212         else
2213                 softc->maxio = cpi.maxio;
2214         softc->disk->d_maxsize = softc->maxio;
2215         softc->disk->d_unit = periph->unit_number;
2216         softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2217         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2218                 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2219         if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2220                 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2221         cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2222             sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2223         strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2224         cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2225             cgd->inq_data.product, sizeof(cgd->inq_data.product),
2226             sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2227         softc->disk->d_hba_vendor = cpi.hba_vendor;
2228         softc->disk->d_hba_device = cpi.hba_device;
2229         softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2230         softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2231
2232         /*
2233          * Acquire a reference to the periph before we register with GEOM.
2234          * We'll release this reference once GEOM calls us back (via
2235          * dadiskgonecb()) telling us that our provider has been freed.
2236          */
2237         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2238                 xpt_print(periph->path, "%s: lost periph during "
2239                           "registration!\n", __func__);
2240                 cam_periph_lock(periph);
2241                 return (CAM_REQ_CMP_ERR);
2242         }
2243
2244         disk_create(softc->disk, DISK_VERSION);
2245         cam_periph_lock(periph);
2246
2247         /*
2248          * Add async callbacks for events of interest.
2249          * I don't bother checking if this fails as,
2250          * in most cases, the system will function just
2251          * fine without them and the only alternative
2252          * would be to not attach the device on failure.
2253          */
2254         xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2255             AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
2256             AC_INQ_CHANGED, daasync, periph, periph->path);
2257
2258         /*
2259          * Emit an attribute changed notification just in case 
2260          * physical path information arrived before our async
2261          * event handler was registered, but after anyone attaching
2262          * to our disk device polled it.
2263          */
2264         disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2265
2266         /*
2267          * Schedule a periodic media polling events.
2268          */
2269         callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2270         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2271             (cgd->inq_flags & SID_AEN) == 0 &&
2272             da_poll_period != 0)
2273                 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2274                     damediapoll, periph);
2275
2276         xpt_schedule(periph, CAM_PRIORITY_DEV);
2277
2278         return(CAM_REQ_CMP);
2279 }
2280
2281 static void
2282 dastart(struct cam_periph *periph, union ccb *start_ccb)
2283 {
2284         struct da_softc *softc;
2285
2286         softc = (struct da_softc *)periph->softc;
2287
2288         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2289
2290 skipstate:
2291         switch (softc->state) {
2292         case DA_STATE_NORMAL:
2293         {
2294                 struct bio *bp;
2295                 uint8_t tag_code;
2296
2297                 /* Run BIO_DELETE if not running yet. */
2298                 if (!softc->delete_running &&
2299                     (bp = bioq_first(&softc->delete_queue)) != NULL) {
2300                         if (softc->delete_func != NULL) {
2301                                 softc->delete_func(periph, start_ccb, bp);
2302                                 goto out;
2303                         } else {
2304                                 bioq_flush(&softc->delete_queue, NULL, 0);
2305                                 /* FALLTHROUGH */
2306                         }
2307                 }
2308
2309                 /* Run regular command. */
2310                 bp = bioq_takefirst(&softc->bio_queue);
2311                 if (bp == NULL) {
2312                         if (softc->tur) {
2313                                 softc->tur = 0;
2314                                 scsi_test_unit_ready(&start_ccb->csio,
2315                                      /*retries*/ da_retry_count,
2316                                      dadone,
2317                                      MSG_SIMPLE_Q_TAG,
2318                                      SSD_FULL_SIZE,
2319                                      da_default_timeout * 1000);
2320                                 start_ccb->ccb_h.ccb_bp = NULL;
2321                                 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2322                                 xpt_action(start_ccb);
2323                         } else
2324                                 xpt_release_ccb(start_ccb);
2325                         break;
2326                 }
2327                 if (softc->tur) {
2328                         softc->tur = 0;
2329                         cam_periph_release_locked(periph);
2330                 }
2331
2332                 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2333                     (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2334                         softc->flags &= ~DA_FLAG_NEED_OTAG;
2335                         softc->flags |= DA_FLAG_WAS_OTAG;
2336                         tag_code = MSG_ORDERED_Q_TAG;
2337                 } else {
2338                         tag_code = MSG_SIMPLE_Q_TAG;
2339                 }
2340
2341                 switch (bp->bio_cmd) {
2342                 case BIO_WRITE:
2343                 case BIO_READ:
2344                 {
2345                         void *data_ptr;
2346                         int rw_op;
2347
2348                         if (bp->bio_cmd == BIO_WRITE) {
2349                                 softc->flags |= DA_FLAG_DIRTY;
2350                                 rw_op = SCSI_RW_WRITE;
2351                         } else {
2352                                 rw_op = SCSI_RW_READ;
2353                         }
2354
2355                         data_ptr = bp->bio_data;
2356                         if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2357                                 rw_op |= SCSI_RW_BIO;
2358                                 data_ptr = bp;
2359                         }
2360
2361                         scsi_read_write(&start_ccb->csio,
2362                                         /*retries*/da_retry_count,
2363                                         /*cbfcnp*/dadone,
2364                                         /*tag_action*/tag_code,
2365                                         rw_op,
2366                                         /*byte2*/0,
2367                                         softc->minimum_cmd_size,
2368                                         /*lba*/bp->bio_pblkno,
2369                                         /*block_count*/bp->bio_bcount /
2370                                         softc->params.secsize,
2371                                         data_ptr,
2372                                         /*dxfer_len*/ bp->bio_bcount,
2373                                         /*sense_len*/SSD_FULL_SIZE,
2374                                         da_default_timeout * 1000);
2375                         break;
2376                 }
2377                 case BIO_FLUSH:
2378                         /*
2379                          * BIO_FLUSH doesn't currently communicate
2380                          * range data, so we synchronize the cache
2381                          * over the whole disk.  We also force
2382                          * ordered tag semantics the flush applies
2383                          * to all previously queued I/O.
2384                          */
2385                         scsi_synchronize_cache(&start_ccb->csio,
2386                                                /*retries*/1,
2387                                                /*cbfcnp*/dadone,
2388                                                MSG_ORDERED_Q_TAG,
2389                                                /*begin_lba*/0,
2390                                                /*lb_count*/0,
2391                                                SSD_FULL_SIZE,
2392                                                da_default_timeout*1000);
2393                         break;
2394                 }
2395                 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2396                 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2397
2398 out:
2399                 LIST_INSERT_HEAD(&softc->pending_ccbs,
2400                                  &start_ccb->ccb_h, periph_links.le);
2401
2402                 /* We expect a unit attention from this device */
2403                 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2404                         start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2405                         softc->flags &= ~DA_FLAG_RETRY_UA;
2406                 }
2407
2408                 start_ccb->ccb_h.ccb_bp = bp;
2409                 softc->refcount++;
2410                 cam_periph_unlock(periph);
2411                 xpt_action(start_ccb);
2412                 cam_periph_lock(periph);
2413                 softc->refcount--;
2414
2415                 /* May have more work to do, so ensure we stay scheduled */
2416                 daschedule(periph);
2417                 break;
2418         }
2419         case DA_STATE_PROBE_WP:
2420         {
2421                 void  *mode_buf;
2422                 int    mode_buf_len;
2423
2424                 mode_buf_len = 192;
2425                 mode_buf = malloc(mode_buf_len, M_SCSIDA, M_NOWAIT);
2426                 if (mode_buf == NULL) {
2427                         xpt_print(periph->path, "Unable to send mode sense - "
2428                             "malloc failure\n");
2429                         softc->state = DA_STATE_PROBE_RC;
2430                         goto skipstate;
2431                 }
2432                 scsi_mode_sense_len(&start_ccb->csio,
2433                                     /*retries*/ da_retry_count,
2434                                     /*cbfcnp*/ dadone,
2435                                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
2436                                     /*dbd*/ FALSE,
2437                                     /*pc*/ SMS_PAGE_CTRL_CURRENT,
2438                                     /*page*/ SMS_ALL_PAGES_PAGE,
2439                                     /*param_buf*/ mode_buf,
2440                                     /*param_len*/ mode_buf_len,
2441                                     /*minimum_cmd_size*/ softc->minimum_cmd_size,
2442                                     /*sense_len*/ SSD_FULL_SIZE,
2443                                     /*timeout*/ da_default_timeout * 1000);
2444                 start_ccb->ccb_h.ccb_bp = NULL;
2445                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_WP;
2446                 xpt_action(start_ccb);
2447                 break;
2448         }
2449         case DA_STATE_PROBE_RC:
2450         {
2451                 struct scsi_read_capacity_data *rcap;
2452
2453                 rcap = (struct scsi_read_capacity_data *)
2454                     malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2455                 if (rcap == NULL) {
2456                         printf("dastart: Couldn't malloc read_capacity data\n");
2457                         /* da_free_periph??? */
2458                         break;
2459                 }
2460                 scsi_read_capacity(&start_ccb->csio,
2461                                    /*retries*/da_retry_count,
2462                                    dadone,
2463                                    MSG_SIMPLE_Q_TAG,
2464                                    rcap,
2465                                    SSD_FULL_SIZE,
2466                                    /*timeout*/5000);
2467                 start_ccb->ccb_h.ccb_bp = NULL;
2468                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2469                 xpt_action(start_ccb);
2470                 break;
2471         }
2472         case DA_STATE_PROBE_RC16:
2473         {
2474                 struct scsi_read_capacity_data_long *rcaplong;
2475
2476                 rcaplong = (struct scsi_read_capacity_data_long *)
2477                         malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2478                 if (rcaplong == NULL) {
2479                         printf("dastart: Couldn't malloc read_capacity data\n");
2480                         /* da_free_periph??? */
2481                         break;
2482                 }
2483                 scsi_read_capacity_16(&start_ccb->csio,
2484                                       /*retries*/ da_retry_count,
2485                                       /*cbfcnp*/ dadone,
2486                                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
2487                                       /*lba*/ 0,
2488                                       /*reladr*/ 0,
2489                                       /*pmi*/ 0,
2490                                       /*rcap_buf*/ (uint8_t *)rcaplong,
2491                                       /*rcap_buf_len*/ sizeof(*rcaplong),
2492                                       /*sense_len*/ SSD_FULL_SIZE,
2493                                       /*timeout*/ da_default_timeout * 1000);
2494                 start_ccb->ccb_h.ccb_bp = NULL;
2495                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2496                 xpt_action(start_ccb);
2497                 break;
2498         }
2499         case DA_STATE_PROBE_LBP:
2500         {
2501                 struct scsi_vpd_logical_block_prov *lbp;
2502
2503                 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2504                         /*
2505                          * If we get here we don't support any SBC-3 delete
2506                          * methods with UNMAP as the Logical Block Provisioning
2507                          * VPD page support is required for devices which
2508                          * support it according to T10/1799-D Revision 31
2509                          * however older revisions of the spec don't mandate
2510                          * this so we currently don't remove these methods
2511                          * from the available set.
2512                          */
2513                         softc->state = DA_STATE_PROBE_BLK_LIMITS;
2514                         goto skipstate;
2515                 }
2516
2517                 lbp = (struct scsi_vpd_logical_block_prov *)
2518                         malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2519
2520                 if (lbp == NULL) {
2521                         printf("dastart: Couldn't malloc lbp data\n");
2522                         /* da_free_periph??? */
2523                         break;
2524                 }
2525
2526                 scsi_inquiry(&start_ccb->csio,
2527                              /*retries*/da_retry_count,
2528                              /*cbfcnp*/dadone,
2529                              /*tag_action*/MSG_SIMPLE_Q_TAG,
2530                              /*inq_buf*/(u_int8_t *)lbp,
2531                              /*inq_len*/sizeof(*lbp),
2532                              /*evpd*/TRUE,
2533                              /*page_code*/SVPD_LBP,
2534                              /*sense_len*/SSD_MIN_SIZE,
2535                              /*timeout*/da_default_timeout * 1000);
2536                 start_ccb->ccb_h.ccb_bp = NULL;
2537                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2538                 xpt_action(start_ccb);
2539                 break;
2540         }
2541         case DA_STATE_PROBE_BLK_LIMITS:
2542         {
2543                 struct scsi_vpd_block_limits *block_limits;
2544
2545                 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2546                         /* Not supported skip to next probe */
2547                         softc->state = DA_STATE_PROBE_BDC;
2548                         goto skipstate;
2549                 }
2550
2551                 block_limits = (struct scsi_vpd_block_limits *)
2552                         malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2553
2554                 if (block_limits == NULL) {
2555                         printf("dastart: Couldn't malloc block_limits data\n");
2556                         /* da_free_periph??? */
2557                         break;
2558                 }
2559
2560                 scsi_inquiry(&start_ccb->csio,
2561                              /*retries*/da_retry_count,
2562                              /*cbfcnp*/dadone,
2563                              /*tag_action*/MSG_SIMPLE_Q_TAG,
2564                              /*inq_buf*/(u_int8_t *)block_limits,
2565                              /*inq_len*/sizeof(*block_limits),
2566                              /*evpd*/TRUE,
2567                              /*page_code*/SVPD_BLOCK_LIMITS,
2568                              /*sense_len*/SSD_MIN_SIZE,
2569                              /*timeout*/da_default_timeout * 1000);
2570                 start_ccb->ccb_h.ccb_bp = NULL;
2571                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2572                 xpt_action(start_ccb);
2573                 break;
2574         }
2575         case DA_STATE_PROBE_BDC:
2576         {
2577                 struct scsi_vpd_block_characteristics *bdc;
2578
2579                 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2580                         softc->state = DA_STATE_PROBE_ATA;
2581                         goto skipstate;
2582                 }
2583
2584                 bdc = (struct scsi_vpd_block_characteristics *)
2585                         malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2586
2587                 if (bdc == NULL) {
2588                         printf("dastart: Couldn't malloc bdc data\n");
2589                         /* da_free_periph??? */
2590                         break;
2591                 }
2592
2593                 scsi_inquiry(&start_ccb->csio,
2594                              /*retries*/da_retry_count,
2595                              /*cbfcnp*/dadone,
2596                              /*tag_action*/MSG_SIMPLE_Q_TAG,
2597                              /*inq_buf*/(u_int8_t *)bdc,
2598                              /*inq_len*/sizeof(*bdc),
2599                              /*evpd*/TRUE,
2600                              /*page_code*/SVPD_BDC,
2601                              /*sense_len*/SSD_MIN_SIZE,
2602                              /*timeout*/da_default_timeout * 1000);
2603                 start_ccb->ccb_h.ccb_bp = NULL;
2604                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2605                 xpt_action(start_ccb);
2606                 break;
2607         }
2608         case DA_STATE_PROBE_ATA:
2609         {
2610                 struct ata_params *ata_params;
2611
2612                 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2613                         daprobedone(periph, start_ccb);
2614                         break;
2615                 }
2616
2617                 ata_params = (struct ata_params*)
2618                         malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2619
2620                 if (ata_params == NULL) {
2621                         printf("dastart: Couldn't malloc ata_params data\n");
2622                         /* da_free_periph??? */
2623                         break;
2624                 }
2625
2626                 scsi_ata_identify(&start_ccb->csio,
2627                                   /*retries*/da_retry_count,
2628                                   /*cbfcnp*/dadone,
2629                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
2630                                   /*data_ptr*/(u_int8_t *)ata_params,
2631                                   /*dxfer_len*/sizeof(*ata_params),
2632                                   /*sense_len*/SSD_FULL_SIZE,
2633                                   /*timeout*/da_default_timeout * 1000);
2634                 start_ccb->ccb_h.ccb_bp = NULL;
2635                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2636                 xpt_action(start_ccb);
2637                 break;
2638         }
2639         }
2640 }
2641
2642 /*
2643  * In each of the methods below, while its the caller's
2644  * responsibility to ensure the request will fit into a
2645  * single device request, we might have changed the delete
2646  * method due to the device incorrectly advertising either
2647  * its supported methods or limits.
2648  * 
2649  * To prevent this causing further issues we validate the
2650  * against the methods limits, and warn which would
2651  * otherwise be unnecessary.
2652  */
2653 static void
2654 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2655 {
2656         struct da_softc *softc = (struct da_softc *)periph->softc;;
2657         struct bio *bp1;
2658         uint8_t *buf = softc->unmap_buf;
2659         uint64_t lba, lastlba = (uint64_t)-1;
2660         uint64_t totalcount = 0;
2661         uint64_t count;
2662         uint32_t lastcount = 0, c;
2663         uint32_t off, ranges = 0;
2664
2665         /*
2666          * Currently this doesn't take the UNMAP
2667          * Granularity and Granularity Alignment
2668          * fields into account.
2669          *
2670          * This could result in both unoptimal unmap
2671          * requests as as well as UNMAP calls unmapping
2672          * fewer LBA's than requested.
2673          */
2674
2675         softc->delete_running = 1;
2676         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2677         bp1 = bp;
2678         do {
2679                 bioq_remove(&softc->delete_queue, bp1);
2680                 if (bp1 != bp)
2681                         bioq_insert_tail(&softc->delete_run_queue, bp1);
2682                 lba = bp1->bio_pblkno;
2683                 count = bp1->bio_bcount / softc->params.secsize;
2684
2685                 /* Try to extend the previous range. */
2686                 if (lba == lastlba) {
2687                         c = omin(count, UNMAP_RANGE_MAX - lastcount);
2688                         lastcount += c;
2689                         off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2690                               UNMAP_HEAD_SIZE;
2691                         scsi_ulto4b(lastcount, &buf[off + 8]);
2692                         count -= c;
2693                         lba +=c;
2694                         totalcount += c;
2695                 }
2696
2697                 while (count > 0) {
2698                         c = omin(count, UNMAP_RANGE_MAX);
2699                         if (totalcount + c > softc->unmap_max_lba ||
2700                             ranges >= softc->unmap_max_ranges) {
2701                                 xpt_print(periph->path,
2702                                     "%s issuing short delete %ld > %ld"
2703                                     "|| %d >= %d",
2704                                     da_delete_method_desc[softc->delete_method],
2705                                     totalcount + c, softc->unmap_max_lba,
2706                                     ranges, softc->unmap_max_ranges);
2707                                 break;
2708                         }
2709                         off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2710                         scsi_u64to8b(lba, &buf[off + 0]);
2711                         scsi_ulto4b(c, &buf[off + 8]);
2712                         lba += c;
2713                         totalcount += c;
2714                         ranges++;
2715                         count -= c;
2716                         lastcount = c;
2717                 }
2718                 lastlba = lba;
2719                 bp1 = bioq_first(&softc->delete_queue);
2720                 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2721                     totalcount + bp1->bio_bcount /
2722                     softc->params.secsize > softc->unmap_max_lba)
2723                         break;
2724         } while (1);
2725         scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2726         scsi_ulto2b(ranges * 16, &buf[2]);
2727
2728         scsi_unmap(&ccb->csio,
2729                    /*retries*/da_retry_count,
2730                    /*cbfcnp*/dadone,
2731                    /*tag_action*/MSG_SIMPLE_Q_TAG,
2732                    /*byte2*/0,
2733                    /*data_ptr*/ buf,
2734                    /*dxfer_len*/ ranges * 16 + 8,
2735                    /*sense_len*/SSD_FULL_SIZE,
2736                    da_default_timeout * 1000);
2737         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2738         ccb->ccb_h.flags |= CAM_UNLOCKED;
2739 }
2740
2741 static void
2742 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2743 {
2744         struct da_softc *softc = (struct da_softc *)periph->softc;
2745         struct bio *bp1;
2746         uint8_t *buf = softc->unmap_buf;
2747         uint64_t lastlba = (uint64_t)-1;
2748         uint64_t count;
2749         uint64_t lba;
2750         uint32_t lastcount = 0, c, requestcount;
2751         int ranges = 0, off, block_count;
2752
2753         softc->delete_running = 1;
2754         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2755         bp1 = bp;
2756         do {
2757                 bioq_remove(&softc->delete_queue, bp1);
2758                 if (bp1 != bp)
2759                         bioq_insert_tail(&softc->delete_run_queue, bp1);
2760                 lba = bp1->bio_pblkno;
2761                 count = bp1->bio_bcount / softc->params.secsize;
2762                 requestcount = count;
2763
2764                 /* Try to extend the previous range. */
2765                 if (lba == lastlba) {
2766                         c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
2767                         lastcount += c;
2768                         off = (ranges - 1) * 8;
2769                         buf[off + 6] = lastcount & 0xff;
2770                         buf[off + 7] = (lastcount >> 8) & 0xff;
2771                         count -= c;
2772                         lba += c;
2773                 }
2774
2775                 while (count > 0) {
2776                         c = omin(count, ATA_DSM_RANGE_MAX);
2777                         off = ranges * 8;
2778
2779                         buf[off + 0] = lba & 0xff;
2780                         buf[off + 1] = (lba >> 8) & 0xff;
2781                         buf[off + 2] = (lba >> 16) & 0xff;
2782                         buf[off + 3] = (lba >> 24) & 0xff;
2783                         buf[off + 4] = (lba >> 32) & 0xff;
2784                         buf[off + 5] = (lba >> 40) & 0xff;
2785                         buf[off + 6] = c & 0xff;
2786                         buf[off + 7] = (c >> 8) & 0xff;
2787                         lba += c;
2788                         ranges++;
2789                         count -= c;
2790                         lastcount = c;
2791                         if (count != 0 && ranges == softc->trim_max_ranges) {
2792                                 xpt_print(periph->path,
2793                                     "%s issuing short delete %ld > %ld\n",
2794                                     da_delete_method_desc[softc->delete_method],
2795                                     requestcount,
2796                                     (softc->trim_max_ranges - ranges) *
2797                                     ATA_DSM_RANGE_MAX);
2798                                 break;
2799                         }
2800                 }
2801                 lastlba = lba;
2802                 bp1 = bioq_first(&softc->delete_queue);
2803                 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2804                     (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2805                         break;
2806         } while (1);
2807
2808         block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2809         scsi_ata_trim(&ccb->csio,
2810                       /*retries*/da_retry_count,
2811                       /*cbfcnp*/dadone,
2812                       /*tag_action*/MSG_SIMPLE_Q_TAG,
2813                       block_count,
2814                       /*data_ptr*/buf,
2815                       /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2816                       /*sense_len*/SSD_FULL_SIZE,
2817                       da_default_timeout * 1000);
2818         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2819         ccb->ccb_h.flags |= CAM_UNLOCKED;
2820 }
2821
2822 /*
2823  * We calculate ws_max_blks here based off d_delmaxsize instead
2824  * of using softc->ws_max_blks as it is absolute max for the
2825  * device not the protocol max which may well be lower.
2826  */
2827 static void
2828 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2829 {
2830         struct da_softc *softc;
2831         struct bio *bp1;
2832         uint64_t ws_max_blks;
2833         uint64_t lba;
2834         uint64_t count; /* forward compat with WS32 */
2835
2836         softc = (struct da_softc *)periph->softc;
2837         ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2838         softc->delete_running = 1;
2839         lba = bp->bio_pblkno;
2840         count = 0;
2841         bp1 = bp;
2842         do {
2843                 bioq_remove(&softc->delete_queue, bp1);
2844                 if (bp1 != bp)
2845                         bioq_insert_tail(&softc->delete_run_queue, bp1);
2846                 count += bp1->bio_bcount / softc->params.secsize;
2847                 if (count > ws_max_blks) {
2848                         xpt_print(periph->path,
2849                             "%s issuing short delete %ld > %ld\n",
2850                             da_delete_method_desc[softc->delete_method],
2851                             count, ws_max_blks);
2852                         count = omin(count, ws_max_blks);
2853                         break;
2854                 }
2855                 bp1 = bioq_first(&softc->delete_queue);
2856                 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2857                     count + bp1->bio_bcount /
2858                     softc->params.secsize > ws_max_blks)
2859                         break;
2860         } while (1);
2861
2862         scsi_write_same(&ccb->csio,
2863                         /*retries*/da_retry_count,
2864                         /*cbfcnp*/dadone,
2865                         /*tag_action*/MSG_SIMPLE_Q_TAG,
2866                         /*byte2*/softc->delete_method ==
2867                             DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2868                         softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2869                         /*lba*/lba,
2870                         /*block_count*/count,
2871                         /*data_ptr*/ __DECONST(void *, zero_region),
2872                         /*dxfer_len*/ softc->params.secsize,
2873                         /*sense_len*/SSD_FULL_SIZE,
2874                         da_default_timeout * 1000);
2875         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2876         ccb->ccb_h.flags |= CAM_UNLOCKED;
2877 }
2878
2879 static int
2880 cmd6workaround(union ccb *ccb)
2881 {
2882         struct scsi_rw_6 cmd6;
2883         struct scsi_rw_10 *cmd10;
2884         struct da_softc *softc;
2885         u_int8_t *cdb;
2886         struct bio *bp;
2887         int frozen;
2888
2889         cdb = ccb->csio.cdb_io.cdb_bytes;
2890         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2891
2892         if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2893                 da_delete_methods old_method = softc->delete_method;
2894
2895                 /*
2896                  * Typically there are two reasons for failure here
2897                  * 1. Delete method was detected as supported but isn't
2898                  * 2. Delete failed due to invalid params e.g. too big
2899                  *
2900                  * While we will attempt to choose an alternative delete method
2901                  * this may result in short deletes if the existing delete
2902                  * requests from geom are big for the new method chosen.
2903                  *
2904                  * This method assumes that the error which triggered this
2905                  * will not retry the io otherwise a panic will occur
2906                  */
2907                 dadeleteflag(softc, old_method, 0);
2908                 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2909                 if (softc->delete_method == DA_DELETE_DISABLE)
2910                         xpt_print(ccb->ccb_h.path,
2911                                   "%s failed, disabling BIO_DELETE\n",
2912                                   da_delete_method_desc[old_method]);
2913                 else
2914                         xpt_print(ccb->ccb_h.path,
2915                                   "%s failed, switching to %s BIO_DELETE\n",
2916                                   da_delete_method_desc[old_method],
2917                                   da_delete_method_desc[softc->delete_method]);
2918
2919                 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2920                         bioq_disksort(&softc->delete_queue, bp);
2921                 bioq_disksort(&softc->delete_queue,
2922                     (struct bio *)ccb->ccb_h.ccb_bp);
2923                 ccb->ccb_h.ccb_bp = NULL;
2924                 return (0);
2925         }
2926
2927         /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2928         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2929             (*cdb == PREVENT_ALLOW) &&
2930             (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2931                 if (bootverbose)
2932                         xpt_print(ccb->ccb_h.path,
2933                             "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2934                 softc->quirks |= DA_Q_NO_PREVENT;
2935                 return (0);
2936         }
2937
2938         /* Detect unsupported SYNCHRONIZE CACHE(10). */
2939         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2940             (*cdb == SYNCHRONIZE_CACHE) &&
2941             (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2942                 if (bootverbose)
2943                         xpt_print(ccb->ccb_h.path,
2944                             "SYNCHRONIZE CACHE(10) not supported.\n");
2945                 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2946                 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2947                 return (0);
2948         }
2949
2950         /* Translation only possible if CDB is an array and cmd is R/W6 */
2951         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2952             (*cdb != READ_6 && *cdb != WRITE_6))
2953                 return 0;
2954
2955         xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2956             "increasing minimum_cmd_size to 10.\n");
2957         softc->minimum_cmd_size = 10;
2958
2959         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2960         cmd10 = (struct scsi_rw_10 *)cdb;
2961         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2962         cmd10->byte2 = 0;
2963         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2964         cmd10->reserved = 0;
2965         scsi_ulto2b(cmd6.length, cmd10->length);
2966         cmd10->control = cmd6.control;
2967         ccb->csio.cdb_len = sizeof(*cmd10);
2968
2969         /* Requeue request, unfreezing queue if necessary */
2970         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2971         ccb->ccb_h.status = CAM_REQUEUE_REQ;
2972         xpt_action(ccb);
2973         if (frozen) {
2974                 cam_release_devq(ccb->ccb_h.path,
2975                                  /*relsim_flags*/0,
2976                                  /*reduction*/0,
2977                                  /*timeout*/0,
2978                                  /*getcount_only*/0);
2979         }
2980         return (ERESTART);
2981 }
2982
2983 static void
2984 dadone(struct cam_periph *periph, union ccb *done_ccb)
2985 {
2986         struct da_softc *softc;
2987         struct ccb_scsiio *csio;
2988         u_int32_t  priority;
2989         da_ccb_state state;
2990
2991         softc = (struct da_softc *)periph->softc;
2992         priority = done_ccb->ccb_h.pinfo.priority;
2993
2994         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2995
2996         csio = &done_ccb->csio;
2997         state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2998         switch (state) {
2999         case DA_CCB_BUFFER_IO:
3000         case DA_CCB_DELETE:
3001         {
3002                 struct bio *bp, *bp1;
3003
3004                 cam_periph_lock(periph);
3005                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
3006                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3007                         int error;
3008                         int sf;
3009
3010                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
3011                                 sf = SF_RETRY_UA;
3012                         else
3013                                 sf = 0;
3014
3015                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
3016                         if (error == ERESTART) {
3017                                 /*
3018                                  * A retry was scheduled, so
3019                                  * just return.
3020                                  */
3021                                 cam_periph_unlock(periph);
3022                                 return;
3023                         }
3024                         bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
3025                         if (error != 0) {
3026                                 int queued_error;
3027
3028                                 /*
3029                                  * return all queued I/O with EIO, so that
3030                                  * the client can retry these I/Os in the
3031                                  * proper order should it attempt to recover.
3032                                  */
3033                                 queued_error = EIO;
3034
3035                                 if (error == ENXIO
3036                                  && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
3037                                         /*
3038                                          * Catastrophic error.  Mark our pack as
3039                                          * invalid.
3040                                          */
3041                                         /*
3042                                          * XXX See if this is really a media
3043                                          * XXX change first?
3044                                          */
3045                                         xpt_print(periph->path,
3046                                             "Invalidating pack\n");
3047                                         softc->flags |= DA_FLAG_PACK_INVALID;
3048                                         queued_error = ENXIO;
3049                                 }
3050                                 bioq_flush(&softc->bio_queue, NULL,
3051                                            queued_error);
3052                                 if (bp != NULL) {
3053                                         bp->bio_error = error;
3054                                         bp->bio_resid = bp->bio_bcount;
3055                                         bp->bio_flags |= BIO_ERROR;
3056                                 }
3057                         } else if (bp != NULL) {
3058                                 if (state == DA_CCB_DELETE)
3059                                         bp->bio_resid = 0;
3060                                 else
3061                                         bp->bio_resid = csio->resid;
3062                                 bp->bio_error = 0;
3063                                 if (bp->bio_resid != 0)
3064                                         bp->bio_flags |= BIO_ERROR;
3065                         }
3066                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3067                                 cam_release_devq(done_ccb->ccb_h.path,
3068                                                  /*relsim_flags*/0,
3069                                                  /*reduction*/0,
3070                                                  /*timeout*/0,
3071                                                  /*getcount_only*/0);
3072                 } else if (bp != NULL) {
3073                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3074                                 panic("REQ_CMP with QFRZN");
3075                         if (state == DA_CCB_DELETE)
3076                                 bp->bio_resid = 0;
3077                         else
3078                                 bp->bio_resid = csio->resid;
3079                         if (csio->resid > 0)
3080                                 bp->bio_flags |= BIO_ERROR;
3081                         if (softc->error_inject != 0) {
3082                                 bp->bio_error = softc->error_inject;
3083                                 bp->bio_resid = bp->bio_bcount;
3084                                 bp->bio_flags |= BIO_ERROR;
3085                                 softc->error_inject = 0;
3086                         }
3087                 }
3088
3089                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
3090                 if (LIST_EMPTY(&softc->pending_ccbs))
3091                         softc->flags |= DA_FLAG_WAS_OTAG;
3092
3093                 xpt_release_ccb(done_ccb);
3094                 if (state == DA_CCB_DELETE) {
3095                         TAILQ_HEAD(, bio) queue;
3096
3097                         TAILQ_INIT(&queue);
3098                         TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
3099                         softc->delete_run_queue.insert_point = NULL;
3100                         /*
3101                          * Normally, the xpt_release_ccb() above would make sure
3102                          * that when we have more work to do, that work would
3103                          * get kicked off. However, we specifically keep
3104                          * delete_running set to 0 before the call above to
3105                          * allow other I/O to progress when many BIO_DELETE
3106                          * requests are pushed down. We set delete_running to 0
3107                          * and call daschedule again so that we don't stall if
3108                          * there are no other I/Os pending apart from BIO_DELETEs.
3109                          */
3110                         softc->delete_running = 0;
3111                         daschedule(periph);
3112                         cam_periph_unlock(periph);
3113                         while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
3114                                 TAILQ_REMOVE(&queue, bp1, bio_queue);
3115                                 bp1->bio_error = bp->bio_error;
3116                                 if (bp->bio_flags & BIO_ERROR) {
3117                                         bp1->bio_flags |= BIO_ERROR;
3118                                         bp1->bio_resid = bp1->bio_bcount;
3119                                 } else
3120                                         bp1->bio_resid = 0;
3121                                 biodone(bp1);
3122                         }
3123                 } else
3124                         cam_periph_unlock(periph);
3125                 if (bp != NULL)
3126                         biodone(bp);
3127                 return;
3128         }
3129         case DA_CCB_PROBE_WP:
3130         {
3131                 struct scsi_mode_header_6 *mode_hdr6;
3132                 struct scsi_mode_header_10 *mode_hdr10;
3133                 uint8_t dev_spec;
3134
3135                 if (softc->minimum_cmd_size > 6) {
3136                         mode_hdr10 = (struct scsi_mode_header_10 *)csio->data_ptr;
3137                         dev_spec = mode_hdr10->dev_spec;
3138                 } else {
3139                         mode_hdr6 = (struct scsi_mode_header_6 *)csio->data_ptr;
3140                         dev_spec = mode_hdr6->dev_spec;
3141                 }
3142                 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
3143                         if ((dev_spec & 0x80) != 0)
3144                                 softc->disk->d_flags |= DISKFLAG_WRITE_PROTECT;
3145                         else
3146                                 softc->disk->d_flags &= ~DISKFLAG_WRITE_PROTECT;
3147                 } else {
3148                         int error;
3149
3150                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3151                                         SF_RETRY_UA|SF_NO_PRINT);
3152                         if (error == ERESTART)
3153                                 return;
3154                         else if (error != 0) {
3155                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3156                                         /* Don't wedge this device's queue */
3157                                         cam_release_devq(done_ccb->ccb_h.path,
3158                                                          /*relsim_flags*/0,
3159                                                          /*reduction*/0,
3160                                                          /*timeout*/0,
3161                                                          /*getcount_only*/0);
3162                                 }
3163                         }
3164                 }
3165
3166                 free(csio->data_ptr, M_SCSIDA);
3167                 xpt_release_ccb(done_ccb);
3168                 if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
3169                         softc->state = DA_STATE_PROBE_RC16;
3170                 else
3171                         softc->state = DA_STATE_PROBE_RC;
3172                 xpt_schedule(periph, priority);
3173                 return;
3174         }
3175         case DA_CCB_PROBE_RC:
3176         case DA_CCB_PROBE_RC16:
3177         {
3178                 struct     scsi_read_capacity_data *rdcap;
3179                 struct     scsi_read_capacity_data_long *rcaplong;
3180                 char       announce_buf[80];
3181                 int        lbp;
3182
3183                 lbp = 0;
3184                 rdcap = NULL;
3185                 rcaplong = NULL;
3186                 if (state == DA_CCB_PROBE_RC)
3187                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3188                 else
3189                         rcaplong = (struct scsi_read_capacity_data_long *)
3190                                 csio->data_ptr;
3191
3192                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3193                         struct disk_params *dp;
3194                         uint32_t block_size;
3195                         uint64_t maxsector;
3196                         u_int lbppbe;   /* LB per physical block exponent. */
3197                         u_int lalba;    /* Lowest aligned LBA. */
3198
3199                         if (state == DA_CCB_PROBE_RC) {
3200                                 block_size = scsi_4btoul(rdcap->length);
3201                                 maxsector = scsi_4btoul(rdcap->addr);
3202                                 lbppbe = 0;
3203                                 lalba = 0;
3204
3205                                 /*
3206                                  * According to SBC-2, if the standard 10
3207                                  * byte READ CAPACITY command returns 2^32,
3208                                  * we should issue the 16 byte version of
3209                                  * the command, since the device in question
3210                                  * has more sectors than can be represented
3211                                  * with the short version of the command.
3212                                  */
3213                                 if (maxsector == 0xffffffff) {
3214                                         free(rdcap, M_SCSIDA);
3215                                         xpt_release_ccb(done_ccb);
3216                                         softc->state = DA_STATE_PROBE_RC16;
3217                                         xpt_schedule(periph, priority);
3218                                         return;
3219                                 }
3220                         } else {
3221                                 block_size = scsi_4btoul(rcaplong->length);
3222                                 maxsector = scsi_8btou64(rcaplong->addr);
3223                                 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3224                                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3225                         }
3226
3227                         /*
3228                          * Because GEOM code just will panic us if we
3229                          * give them an 'illegal' value we'll avoid that
3230                          * here.
3231                          */
3232                         if (block_size == 0) {
3233                                 block_size = 512;
3234                                 if (maxsector == 0)
3235                                         maxsector = -1;
3236                         }
3237                         if (block_size >= MAXPHYS) {
3238                                 xpt_print(periph->path,
3239                                     "unsupportable block size %ju\n",
3240                                     (uintmax_t) block_size);
3241                                 announce_buf[0] = '\0';
3242                                 cam_periph_invalidate(periph);
3243                         } else {
3244                                 /*
3245                                  * We pass rcaplong into dasetgeom(),
3246                                  * because it will only use it if it is
3247                                  * non-NULL.
3248                                  */
3249                                 dasetgeom(periph, block_size, maxsector,
3250                                           rcaplong, sizeof(*rcaplong));
3251                                 lbp = (lalba & SRC16_LBPME_A);
3252                                 dp = &softc->params;
3253                                 snprintf(announce_buf, sizeof(announce_buf),
3254                                     "%juMB (%ju %u byte sectors)",
3255                                     ((uintmax_t)dp->secsize * dp->sectors) /
3256                                      (1024 * 1024),
3257                                     (uintmax_t)dp->sectors, dp->secsize);
3258                         }
3259                 } else {
3260                         int     error;
3261
3262                         announce_buf[0] = '\0';
3263
3264                         /*
3265                          * Retry any UNIT ATTENTION type errors.  They
3266                          * are expected at boot.
3267                          */
3268                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3269                                         SF_RETRY_UA|SF_NO_PRINT);
3270                         if (error == ERESTART) {
3271                                 /*
3272                                  * A retry was scheuled, so
3273                                  * just return.
3274                                  */
3275                                 return;
3276                         } else if (error != 0) {
3277                                 int asc, ascq;
3278                                 int sense_key, error_code;
3279                                 int have_sense;
3280                                 cam_status status;
3281                                 struct ccb_getdev cgd;
3282
3283                                 /* Don't wedge this device's queue */
3284                                 status = done_ccb->ccb_h.status;
3285                                 if ((status & CAM_DEV_QFRZN) != 0)
3286                                         cam_release_devq(done_ccb->ccb_h.path,
3287                                                          /*relsim_flags*/0,
3288                                                          /*reduction*/0,
3289                                                          /*timeout*/0,
3290                                                          /*getcount_only*/0);
3291
3292
3293                                 xpt_setup_ccb(&cgd.ccb_h, 
3294                                               done_ccb->ccb_h.path,
3295                                               CAM_PRIORITY_NORMAL);
3296                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3297                                 xpt_action((union ccb *)&cgd);
3298
3299                                 if (scsi_extract_sense_ccb(done_ccb,
3300                                     &error_code, &sense_key, &asc, &ascq))
3301                                         have_sense = TRUE;
3302                                 else
3303                                         have_sense = FALSE;
3304
3305                                 /*
3306                                  * If we tried READ CAPACITY(16) and failed,
3307                                  * fallback to READ CAPACITY(10).
3308                                  */
3309                                 if ((state == DA_CCB_PROBE_RC16) &&
3310                                     (softc->flags & DA_FLAG_CAN_RC16) &&
3311                                     (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3312                                         CAM_REQ_INVALID) ||
3313                                      ((have_sense) &&
3314                                       (error_code == SSD_CURRENT_ERROR) &&
3315                                       (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3316                                         softc->flags &= ~DA_FLAG_CAN_RC16;
3317                                         free(rdcap, M_SCSIDA);
3318                                         xpt_release_ccb(done_ccb);
3319                                         softc->state = DA_STATE_PROBE_RC;
3320                                         xpt_schedule(periph, priority);
3321                                         return;
3322                                 } else
3323                                 /*
3324                                  * Attach to anything that claims to be a
3325                                  * direct access or optical disk device,
3326                                  * as long as it doesn't return a "Logical
3327                                  * unit not supported" (0x25) error.
3328                                  */
3329                                 if ((have_sense) && (asc != 0x25)
3330                                  && (error_code == SSD_CURRENT_ERROR)) {
3331                                         const char *sense_key_desc;
3332                                         const char *asc_desc;
3333
3334                                         dasetgeom(periph, 512, -1, NULL, 0);
3335                                         scsi_sense_desc(sense_key, asc, ascq,
3336                                                         &cgd.inq_data,
3337                                                         &sense_key_desc,
3338                                                         &asc_desc);
3339                                         snprintf(announce_buf,
3340                                             sizeof(announce_buf),
3341                                                 "Attempt to query device "
3342                                                 "size failed: %s, %s",
3343                                                 sense_key_desc,
3344                                                 asc_desc);
3345                                 } else { 
3346                                         if (have_sense)
3347                                                 scsi_sense_print(
3348                                                         &done_ccb->csio);
3349                                         else {
3350                                                 xpt_print(periph->path,
3351                                                     "got CAM status %#x\n",
3352                                                     done_ccb->ccb_h.status);
3353                                         }
3354
3355                                         xpt_print(periph->path, "fatal error, "
3356                                             "failed to attach to device\n");
3357
3358                                         /*
3359                                          * Free up resources.
3360                                          */
3361                                         cam_periph_invalidate(periph);
3362                                 } 
3363                         }
3364                 }
3365                 free(csio->data_ptr, M_SCSIDA);
3366                 if (announce_buf[0] != '\0' &&
3367                     ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3368                         /*
3369                          * Create our sysctl variables, now that we know
3370                          * we have successfully attached.
3371                          */
3372                         /* increase the refcount */
3373                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3374                                 taskqueue_enqueue(taskqueue_thread,
3375                                                   &softc->sysctl_task);
3376                                 xpt_announce_periph(periph, announce_buf);
3377                                 xpt_announce_quirks(periph, softc->quirks,
3378                                     DA_Q_BIT_STRING);
3379                         } else {
3380                                 xpt_print(periph->path, "fatal error, "
3381                                     "could not acquire reference count\n");
3382                         }
3383                 }
3384
3385                 /* We already probed the device. */
3386                 if (softc->flags & DA_FLAG_PROBED) {
3387                         daprobedone(periph, done_ccb);
3388                         return;
3389                 }
3390
3391                 /* Ensure re-probe doesn't see old delete. */
3392                 softc->delete_available = 0;
3393                 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3394                 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3395                         /*
3396                          * Based on older SBC-3 spec revisions
3397                          * any of the UNMAP methods "may" be
3398                          * available via LBP given this flag so
3399                          * we flag all of them as available and
3400                          * then remove those which further
3401                          * probes confirm aren't available
3402                          * later.
3403                          *
3404                          * We could also check readcap(16) p_type
3405                          * flag to exclude one or more invalid
3406                          * write same (X) types here
3407                          */
3408                         dadeleteflag(softc, DA_DELETE_WS16, 1);
3409                         dadeleteflag(softc, DA_DELETE_WS10, 1);
3410                         dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3411
3412                         xpt_release_ccb(done_ccb);
3413                         softc->state = DA_STATE_PROBE_LBP;
3414                         xpt_schedule(periph, priority);
3415                         return;
3416                 }
3417
3418                 xpt_release_ccb(done_ccb);
3419                 softc->state = DA_STATE_PROBE_BDC;
3420                 xpt_schedule(periph, priority);
3421                 return;
3422         }
3423         case DA_CCB_PROBE_LBP:
3424         {
3425                 struct scsi_vpd_logical_block_prov *lbp;
3426
3427                 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3428
3429                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3430                         /*
3431                          * T10/1799-D Revision 31 states at least one of these
3432                          * must be supported but we don't currently enforce this.
3433                          */
3434                         dadeleteflag(softc, DA_DELETE_WS16,
3435                                      (lbp->flags & SVPD_LBP_WS16));
3436                         dadeleteflag(softc, DA_DELETE_WS10,
3437                                      (lbp->flags & SVPD_LBP_WS10));
3438                         dadeleteflag(softc, DA_DELETE_UNMAP,
3439                                      (lbp->flags & SVPD_LBP_UNMAP));
3440                 } else {
3441                         int error;
3442                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3443                                         SF_RETRY_UA|SF_NO_PRINT);
3444                         if (error == ERESTART)
3445                                 return;
3446                         else if (error != 0) {
3447                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3448                                         /* Don't wedge this device's queue */
3449                                         cam_release_devq(done_ccb->ccb_h.path,
3450                                                          /*relsim_flags*/0,
3451                                                          /*reduction*/0,
3452                                                          /*timeout*/0,
3453                                                          /*getcount_only*/0);
3454                                 }
3455
3456                                 /*
3457                                  * Failure indicates we don't support any SBC-3
3458                                  * delete methods with UNMAP
3459                                  */
3460                         }
3461                 }
3462
3463                 free(lbp, M_SCSIDA);
3464                 xpt_release_ccb(done_ccb);
3465                 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3466                 xpt_schedule(periph, priority);
3467                 return;
3468         }
3469         case DA_CCB_PROBE_BLK_LIMITS:
3470         {
3471                 struct scsi_vpd_block_limits *block_limits;
3472
3473                 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3474
3475                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3476                         uint32_t max_txfer_len = scsi_4btoul(
3477                                 block_limits->max_txfer_len);
3478                         uint32_t max_unmap_lba_cnt = scsi_4btoul(
3479                                 block_limits->max_unmap_lba_cnt);
3480                         uint32_t max_unmap_blk_cnt = scsi_4btoul(
3481                                 block_limits->max_unmap_blk_cnt);
3482                         uint64_t ws_max_blks = scsi_8btou64(
3483                                 block_limits->max_write_same_length);
3484
3485                         if (max_txfer_len != 0) {
3486                                 softc->disk->d_maxsize = MIN(softc->maxio,
3487                                     (off_t)max_txfer_len * softc->params.secsize);
3488                         }
3489
3490                         /*
3491                          * We should already support UNMAP but we check lba
3492                          * and block count to be sure
3493                          */
3494                         if (max_unmap_lba_cnt != 0x00L &&
3495                             max_unmap_blk_cnt != 0x00L) {
3496                                 softc->unmap_max_lba = max_unmap_lba_cnt;
3497                                 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3498                                         UNMAP_MAX_RANGES);
3499                         } else {
3500                                 /*
3501                                  * Unexpected UNMAP limits which means the
3502                                  * device doesn't actually support UNMAP
3503                                  */
3504                                 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3505                         }
3506
3507                         if (ws_max_blks != 0x00L)
3508                                 softc->ws_max_blks = ws_max_blks;
3509                 } else {
3510                         int error;
3511                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3512                                         SF_RETRY_UA|SF_NO_PRINT);
3513                         if (error == ERESTART)
3514                                 return;
3515                         else if (error != 0) {
3516                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3517                                         /* Don't wedge this device's queue */
3518                                         cam_release_devq(done_ccb->ccb_h.path,
3519                                                          /*relsim_flags*/0,
3520                                                          /*reduction*/0,
3521                                                          /*timeout*/0,
3522                                                          /*getcount_only*/0);
3523                                 }
3524
3525                                 /*
3526                                  * Failure here doesn't mean UNMAP is not
3527                                  * supported as this is an optional page.
3528                                  */
3529                                 softc->unmap_max_lba = 1;
3530                                 softc->unmap_max_ranges = 1;
3531                         }
3532                 }
3533
3534                 free(block_limits, M_SCSIDA);
3535                 xpt_release_ccb(done_ccb);
3536                 softc->state = DA_STATE_PROBE_BDC;
3537                 xpt_schedule(periph, priority);
3538                 return;
3539         }
3540         case DA_CCB_PROBE_BDC:
3541         {
3542                 struct scsi_vpd_block_characteristics *bdc;
3543
3544                 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3545
3546                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3547                         /*
3548                          * Disable queue sorting for non-rotational media
3549                          * by default.
3550                          */
3551                         u_int16_t old_rate = softc->disk->d_rotation_rate;
3552
3553                         softc->disk->d_rotation_rate =
3554                                 scsi_2btoul(bdc->medium_rotation_rate);
3555                         if (softc->disk->d_rotation_rate ==
3556                             SVPD_BDC_RATE_NON_ROTATING) {
3557                                 softc->sort_io_queue = 0;
3558                         }
3559                         if (softc->disk->d_rotation_rate != old_rate) {
3560                                 disk_attr_changed(softc->disk,
3561                                     "GEOM::rotation_rate", M_NOWAIT);
3562                         }
3563                 } else {
3564                         int error;
3565                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3566                                         SF_RETRY_UA|SF_NO_PRINT);
3567                         if (error == ERESTART)
3568                                 return;
3569                         else if (error != 0) {
3570                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3571                                         /* Don't wedge this device's queue */
3572                                         cam_release_devq(done_ccb->ccb_h.path,
3573                                                          /*relsim_flags*/0,
3574                                                          /*reduction*/0,
3575                                                          /*timeout*/0,
3576                                                          /*getcount_only*/0);
3577                                 }
3578                         }
3579                 }
3580
3581                 free(bdc, M_SCSIDA);
3582                 xpt_release_ccb(done_ccb);
3583                 softc->state = DA_STATE_PROBE_ATA;
3584                 xpt_schedule(periph, priority);
3585                 return;
3586         }
3587         case DA_CCB_PROBE_ATA:
3588         {
3589                 int i;
3590                 struct ata_params *ata_params;
3591                 int16_t *ptr;
3592
3593                 ata_params = (struct ata_params *)csio->data_ptr;
3594                 ptr = (uint16_t *)ata_params;
3595
3596                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3597                         uint16_t old_rate;
3598
3599                         for (i = 0; i < sizeof(*ata_params) / 2; i++)
3600                                 ptr[i] = le16toh(ptr[i]);
3601                         if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3602                             (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3603                                 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3604                                 if (ata_params->max_dsm_blocks != 0)
3605                                         softc->trim_max_ranges = min(
3606                                           softc->trim_max_ranges,
3607                                           ata_params->max_dsm_blocks *
3608                                           ATA_DSM_BLK_RANGES);
3609                         }
3610                         /*
3611                          * Disable queue sorting for non-rotational media
3612                          * by default.
3613                          */
3614                         old_rate = softc->disk->d_rotation_rate;
3615                         softc->disk->d_rotation_rate =
3616                             ata_params->media_rotation_rate;
3617                         if (softc->disk->d_rotation_rate ==
3618                             ATA_RATE_NON_ROTATING) {
3619                                 softc->sort_io_queue = 0;
3620                         }
3621
3622                         if (softc->disk->d_rotation_rate != old_rate) {
3623                                 disk_attr_changed(softc->disk,
3624                                     "GEOM::rotation_rate", M_NOWAIT);
3625                         }
3626                 } else {
3627                         int error;
3628                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3629                                         SF_RETRY_UA|SF_NO_PRINT);
3630                         if (error == ERESTART)
3631                                 return;
3632                         else if (error != 0) {
3633                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3634                                         /* Don't wedge this device's queue */
3635                                         cam_release_devq(done_ccb->ccb_h.path,
3636                                                          /*relsim_flags*/0,
3637                                                          /*reduction*/0,
3638                                                          /*timeout*/0,
3639                                                          /*getcount_only*/0);
3640                                 }
3641                         }
3642                 }
3643
3644                 free(ata_params, M_SCSIDA);
3645                 daprobedone(periph, done_ccb);
3646                 return;
3647         }
3648         case DA_CCB_DUMP:
3649                 /* No-op.  We're polling */
3650                 return;
3651         case DA_CCB_TUR:
3652         {
3653                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3654
3655                         if (daerror(done_ccb, CAM_RETRY_SELTO,
3656                             SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3657                             ERESTART)
3658                                 return;
3659                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3660                                 cam_release_devq(done_ccb->ccb_h.path,
3661                                                  /*relsim_flags*/0,
3662                                                  /*reduction*/0,
3663                                                  /*timeout*/0,
3664                                                  /*getcount_only*/0);
3665                 }
3666                 xpt_release_ccb(done_ccb);
3667                 cam_periph_release_locked(periph);
3668                 return;
3669         }
3670         default:
3671                 break;
3672         }
3673         xpt_release_ccb(done_ccb);
3674 }
3675
3676 static void
3677 dareprobe(struct cam_periph *periph)
3678 {
3679         struct da_softc   *softc;
3680         cam_status status;
3681
3682         softc = (struct da_softc *)periph->softc;
3683
3684         /* Probe in progress; don't interfere. */
3685         if (softc->state != DA_STATE_NORMAL)
3686                 return;
3687
3688         status = cam_periph_acquire(periph);
3689         KASSERT(status == CAM_REQ_CMP,
3690             ("dareprobe: cam_periph_acquire failed"));
3691
3692         softc->state = DA_STATE_PROBE_WP;
3693         xpt_schedule(periph, CAM_PRIORITY_DEV);
3694 }
3695
3696 static int
3697 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3698 {
3699         struct da_softc   *softc;
3700         struct cam_periph *periph;
3701         int error, error_code, sense_key, asc, ascq;
3702
3703         periph = xpt_path_periph(ccb->ccb_h.path);
3704         softc = (struct da_softc *)periph->softc;
3705
3706         /*
3707          * Automatically detect devices that do not support
3708          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3709          */
3710         error = 0;
3711         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3712                 error = cmd6workaround(ccb);
3713         } else if (scsi_extract_sense_ccb(ccb,
3714             &error_code, &sense_key, &asc, &ascq)) {
3715                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3716                         error = cmd6workaround(ccb);
3717                 /*
3718                  * If the target replied with CAPACITY DATA HAS CHANGED UA,
3719                  * query the capacity and notify upper layers.
3720                  */
3721                 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3722                     asc == 0x2A && ascq == 0x09) {
3723                         xpt_print(periph->path, "Capacity data has changed\n");
3724                         softc->flags &= ~DA_FLAG_PROBED;
3725                         dareprobe(periph);
3726                         sense_flags |= SF_NO_PRINT;
3727                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3728                     asc == 0x28 && ascq == 0x00) {
3729                         softc->flags &= ~DA_FLAG_PROBED;
3730                         disk_media_changed(softc->disk, M_NOWAIT);
3731                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3732                     asc == 0x3F && ascq == 0x03) {
3733                         xpt_print(periph->path, "INQUIRY data has changed\n");
3734                         softc->flags &= ~DA_FLAG_PROBED;
3735                         dareprobe(periph);
3736                         sense_flags |= SF_NO_PRINT;
3737                 } else if (sense_key == SSD_KEY_NOT_READY &&
3738                     asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3739                         softc->flags |= DA_FLAG_PACK_INVALID;
3740                         disk_media_gone(softc->disk, M_NOWAIT);
3741                 }
3742         }
3743         if (error == ERESTART)
3744                 return (ERESTART);
3745
3746         /*
3747          * XXX
3748          * Until we have a better way of doing pack validation,
3749          * don't treat UAs as errors.
3750          */
3751         sense_flags |= SF_RETRY_UA;
3752
3753         if (softc->quirks & DA_Q_RETRY_BUSY)
3754                 sense_flags |= SF_RETRY_BUSY;
3755         return(cam_periph_error(ccb, cam_flags, sense_flags,
3756                                 &softc->saved_ccb));
3757 }
3758
3759 static void
3760 damediapoll(void *arg)
3761 {
3762         struct cam_periph *periph = arg;
3763         struct da_softc *softc = periph->softc;
3764
3765         if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3766                 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3767                         softc->tur = 1;
3768                         daschedule(periph);
3769                 }
3770         }
3771         /* Queue us up again */
3772         if (da_poll_period != 0)
3773                 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3774 }
3775
3776 static void
3777 daprevent(struct cam_periph *periph, int action)
3778 {
3779         struct  da_softc *softc;
3780         union   ccb *ccb;               
3781         int     error;
3782                 
3783         softc = (struct da_softc *)periph->softc;
3784
3785         if (((action == PR_ALLOW)
3786           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3787          || ((action == PR_PREVENT)
3788           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3789                 return;
3790         }
3791
3792         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3793
3794         scsi_prevent(&ccb->csio,
3795                      /*retries*/1,
3796                      /*cbcfp*/dadone,
3797                      MSG_SIMPLE_Q_TAG,
3798                      action,
3799                      SSD_FULL_SIZE,
3800                      5000);
3801
3802         error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3803             SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3804
3805         if (error == 0) {
3806                 if (action == PR_ALLOW)
3807                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
3808                 else
3809                         softc->flags |= DA_FLAG_PACK_LOCKED;
3810         }
3811
3812         xpt_release_ccb(ccb);
3813 }
3814
3815 static void
3816 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3817           struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3818 {
3819         struct ccb_calc_geometry ccg;
3820         struct da_softc *softc;
3821         struct disk_params *dp;
3822         u_int lbppbe, lalba;
3823         int error;
3824
3825         softc = (struct da_softc *)periph->softc;
3826
3827         dp = &softc->params;
3828         dp->secsize = block_len;
3829         dp->sectors = maxsector + 1;
3830         if (rcaplong != NULL) {
3831                 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3832                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3833                 lalba &= SRC16_LALBA_A;
3834         } else {
3835                 lbppbe = 0;
3836                 lalba = 0;
3837         }
3838
3839         if (lbppbe > 0) {
3840                 dp->stripesize = block_len << lbppbe;
3841                 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3842                     dp->stripesize;
3843         } else if (softc->quirks & DA_Q_4K) {
3844                 dp->stripesize = 4096;
3845                 dp->stripeoffset = 0;
3846         } else {
3847                 dp->stripesize = 0;
3848                 dp->stripeoffset = 0;
3849         }
3850         /*
3851          * Have the controller provide us with a geometry
3852          * for this disk.  The only time the geometry
3853          * matters is when we boot and the controller
3854          * is the only one knowledgeable enough to come
3855          * up with something that will make this a bootable
3856          * device.
3857          */
3858         xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3859         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3860         ccg.block_size = dp->secsize;
3861         ccg.volume_size = dp->sectors;
3862         ccg.heads = 0;
3863         ccg.secs_per_track = 0;
3864         ccg.cylinders = 0;
3865         xpt_action((union ccb*)&ccg);
3866         if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3867                 /*
3868                  * We don't know what went wrong here- but just pick
3869                  * a geometry so we don't have nasty things like divide
3870                  * by zero.
3871                  */
3872                 dp->heads = 255;
3873                 dp->secs_per_track = 255;
3874                 dp->cylinders = dp->sectors / (255 * 255);
3875                 if (dp->cylinders == 0) {
3876                         dp->cylinders = 1;
3877                 }
3878         } else {
3879                 dp->heads = ccg.heads;
3880                 dp->secs_per_track = ccg.secs_per_track;
3881                 dp->cylinders = ccg.cylinders;
3882         }
3883
3884         /*
3885          * If the user supplied a read capacity buffer, and if it is
3886          * different than the previous buffer, update the data in the EDT.
3887          * If it's the same, we don't bother.  This avoids sending an
3888          * update every time someone opens this device.
3889          */
3890         if ((rcaplong != NULL)
3891          && (bcmp(rcaplong, &softc->rcaplong,
3892                   min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3893                 struct ccb_dev_advinfo cdai;
3894
3895                 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3896                 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3897                 cdai.buftype = CDAI_TYPE_RCAPLONG;
3898                 cdai.flags = CDAI_FLAG_STORE;
3899                 cdai.bufsiz = rcap_len;
3900                 cdai.buf = (uint8_t *)rcaplong;
3901                 xpt_action((union ccb *)&cdai);
3902                 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3903                         cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3904                 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3905                         xpt_print(periph->path, "%s: failed to set read "
3906                                   "capacity advinfo\n", __func__);
3907                         /* Use cam_error_print() to decode the status */
3908                         cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3909                                         CAM_EPF_ALL);
3910                 } else {
3911                         bcopy(rcaplong, &softc->rcaplong,
3912                               min(sizeof(softc->rcaplong), rcap_len));
3913                 }
3914         }
3915
3916         softc->disk->d_sectorsize = softc->params.secsize;
3917         softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3918         softc->disk->d_stripesize = softc->params.stripesize;
3919         softc->disk->d_stripeoffset = softc->params.stripeoffset;
3920         /* XXX: these are not actually "firmware" values, so they may be wrong */
3921         softc->disk->d_fwsectors = softc->params.secs_per_track;
3922         softc->disk->d_fwheads = softc->params.heads;
3923         softc->disk->d_devstat->block_size = softc->params.secsize;
3924         softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3925
3926         error = disk_resize(softc->disk, M_NOWAIT);
3927         if (error != 0)
3928                 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3929 }
3930
3931 static void
3932 dasendorderedtag(void *arg)
3933 {
3934         struct da_softc *softc = arg;
3935
3936         if (da_send_ordered) {
3937                 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3938                         if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3939                                 softc->flags |= DA_FLAG_NEED_OTAG;
3940                         softc->flags &= ~DA_FLAG_WAS_OTAG;
3941                 }
3942         }
3943         /* Queue us up again */
3944         callout_reset(&softc->sendordered_c,
3945             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3946             dasendorderedtag, softc);
3947 }
3948
3949 /*
3950  * Step through all DA peripheral drivers, and if the device is still open,
3951  * sync the disk cache to physical media.
3952  */
3953 static void
3954 dashutdown(void * arg, int howto)
3955 {
3956         struct cam_periph *periph;
3957         struct da_softc *softc;
3958         union ccb *ccb;
3959         int error;
3960
3961         CAM_PERIPH_FOREACH(periph, &dadriver) {
3962                 softc = (struct da_softc *)periph->softc;
3963                 if (SCHEDULER_STOPPED()) {
3964                         /* If we paniced with the lock held, do not recurse. */
3965                         if (!cam_periph_owned(periph) &&
3966                             (softc->flags & DA_FLAG_OPEN)) {
3967                                 dadump(softc->disk, NULL, 0, 0, 0);
3968                         }
3969                         continue;
3970                 }
3971                 cam_periph_lock(periph);
3972
3973                 /*
3974                  * We only sync the cache if the drive is still open, and
3975                  * if the drive is capable of it..
3976                  */
3977                 if (((softc->flags & DA_FLAG_OPEN) == 0)
3978                  || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3979                         cam_periph_unlock(periph);
3980                         continue;
3981                 }
3982
3983                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3984                 scsi_synchronize_cache(&ccb->csio,
3985                                        /*retries*/0,
3986                                        /*cbfcnp*/dadone,
3987                                        MSG_SIMPLE_Q_TAG,
3988                                        /*begin_lba*/0, /* whole disk */
3989                                        /*lb_count*/0,
3990                                        SSD_FULL_SIZE,
3991                                        60 * 60 * 1000);
3992
3993                 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3994                     /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3995                     softc->disk->d_devstat);
3996                 if (error != 0)
3997                         xpt_print(periph->path, "Synchronize cache failed\n");
3998                 xpt_release_ccb(ccb);
3999                 cam_periph_unlock(periph);
4000         }
4001 }
4002
4003 #else /* !_KERNEL */
4004
4005 /*
4006  * XXX These are only left out of the kernel build to silence warnings.  If,
4007  * for some reason these functions are used in the kernel, the ifdefs should
4008  * be moved so they are included both in the kernel and userland.
4009  */
4010 void
4011 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
4012                  void (*cbfcnp)(struct cam_periph *, union ccb *),
4013                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
4014                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4015                  u_int32_t timeout)
4016 {
4017         struct scsi_format_unit *scsi_cmd;
4018
4019         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
4020         scsi_cmd->opcode = FORMAT_UNIT;
4021         scsi_cmd->byte2 = byte2;
4022         scsi_ulto2b(ileave, scsi_cmd->interleave);
4023
4024         cam_fill_csio(csio,
4025                       retries,
4026                       cbfcnp,
4027                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4028                       tag_action,
4029                       data_ptr,
4030                       dxfer_len,
4031                       sense_len,
4032                       sizeof(*scsi_cmd),
4033                       timeout);
4034 }
4035
4036 void
4037 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
4038                   void (*cbfcnp)(struct cam_periph *, union ccb *),
4039                   uint8_t tag_action, uint8_t list_format,
4040                   uint32_t addr_desc_index, uint8_t *data_ptr,
4041                   uint32_t dxfer_len, int minimum_cmd_size, 
4042                   uint8_t sense_len, uint32_t timeout)
4043 {
4044         uint8_t cdb_len;
4045
4046         /*
4047          * These conditions allow using the 10 byte command.  Otherwise we
4048          * need to use the 12 byte command.
4049          */
4050         if ((minimum_cmd_size <= 10)
4051          && (addr_desc_index == 0) 
4052          && (dxfer_len <= SRDD10_MAX_LENGTH)) {
4053                 struct scsi_read_defect_data_10 *cdb10;
4054
4055                 cdb10 = (struct scsi_read_defect_data_10 *)
4056                         &csio->cdb_io.cdb_bytes;
4057
4058                 cdb_len = sizeof(*cdb10);
4059                 bzero(cdb10, cdb_len);
4060                 cdb10->opcode = READ_DEFECT_DATA_10;
4061                 cdb10->format = list_format;
4062                 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
4063         } else {
4064                 struct scsi_read_defect_data_12 *cdb12;
4065
4066                 cdb12 = (struct scsi_read_defect_data_12 *)
4067                         &csio->cdb_io.cdb_bytes;
4068
4069                 cdb_len = sizeof(*cdb12);
4070                 bzero(cdb12, cdb_len);
4071                 cdb12->opcode = READ_DEFECT_DATA_12;
4072                 cdb12->format = list_format;
4073                 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
4074                 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
4075         }
4076
4077         cam_fill_csio(csio,
4078                       retries,
4079                       cbfcnp,
4080                       /*flags*/ CAM_DIR_IN,
4081                       tag_action,
4082                       data_ptr,
4083                       dxfer_len,
4084                       sense_len,
4085                       cdb_len,
4086                       timeout);
4087 }
4088
4089 void
4090 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
4091               void (*cbfcnp)(struct cam_periph *, union ccb *),
4092               u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
4093               u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4094               u_int32_t timeout)
4095 {
4096         struct scsi_sanitize *scsi_cmd;
4097
4098         scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
4099         scsi_cmd->opcode = SANITIZE;
4100         scsi_cmd->byte2 = byte2;
4101         scsi_cmd->control = control;
4102         scsi_ulto2b(dxfer_len, scsi_cmd->length);
4103
4104         cam_fill_csio(csio,
4105                       retries,
4106                       cbfcnp,
4107                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4108                       tag_action,
4109                       data_ptr,
4110                       dxfer_len,
4111                       sense_len,
4112                       sizeof(*scsi_cmd),
4113                       timeout);
4114 }
4115
4116 #endif /* _KERNEL */