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