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