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