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