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