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