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