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