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