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