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