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