xref: /freebsd/sys/cam/scsi/scsi_da.c (revision a223d3ed90bfe313ce5987d468a25a915d7d1254)
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 SSDs
971 		 * 4k optimised & trim only works in 4k requests + 4k aligned
972 		 */
973 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force GT*", "*" },
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 		 * SuperTalent TeraDrive CT SSDs
1115 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1116 		 */
1117 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1118 		/*quirks*/DA_Q_4K
1119 	},
1120 	{
1121 		/*
1122 		 * XceedIOPS SATA SSDs
1123 		 * 4k optimised
1124 		 */
1125 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1126 		/*quirks*/DA_Q_4K
1127 	},
1128 };
1129 
1130 static	disk_strategy_t	dastrategy;
1131 static	dumper_t	dadump;
1132 static	periph_init_t	dainit;
1133 static	void		daasync(void *callback_arg, u_int32_t code,
1134 				struct cam_path *path, void *arg);
1135 static	void		dasysctlinit(void *context, int pending);
1136 static	int		dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1137 static	int		dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1138 static	int		dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1139 static	void		dadeletemethodset(struct da_softc *softc,
1140 					  da_delete_methods delete_method);
1141 static	off_t		dadeletemaxsize(struct da_softc *softc,
1142 					da_delete_methods delete_method);
1143 static	void		dadeletemethodchoose(struct da_softc *softc,
1144 					     da_delete_methods default_method);
1145 static	void		daprobedone(struct cam_periph *periph, union ccb *ccb);
1146 
1147 static	periph_ctor_t	daregister;
1148 static	periph_dtor_t	dacleanup;
1149 static	periph_start_t	dastart;
1150 static	periph_oninv_t	daoninvalidate;
1151 static	void		dadone(struct cam_periph *periph,
1152 			       union ccb *done_ccb);
1153 static  int		daerror(union ccb *ccb, u_int32_t cam_flags,
1154 				u_int32_t sense_flags);
1155 static void		daprevent(struct cam_periph *periph, int action);
1156 static void		dareprobe(struct cam_periph *periph);
1157 static void		dasetgeom(struct cam_periph *periph, uint32_t block_len,
1158 				  uint64_t maxsector,
1159 				  struct scsi_read_capacity_data_long *rcaplong,
1160 				  size_t rcap_size);
1161 static timeout_t	dasendorderedtag;
1162 static void		dashutdown(void *arg, int howto);
1163 static timeout_t	damediapoll;
1164 
1165 #ifndef	DA_DEFAULT_POLL_PERIOD
1166 #define	DA_DEFAULT_POLL_PERIOD	3
1167 #endif
1168 
1169 #ifndef DA_DEFAULT_TIMEOUT
1170 #define DA_DEFAULT_TIMEOUT 60	/* Timeout in seconds */
1171 #endif
1172 
1173 #ifndef	DA_DEFAULT_RETRY
1174 #define	DA_DEFAULT_RETRY	4
1175 #endif
1176 
1177 #ifndef	DA_DEFAULT_SEND_ORDERED
1178 #define	DA_DEFAULT_SEND_ORDERED	1
1179 #endif
1180 
1181 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1182     softc->sort_io_queue : cam_sort_io_queues)
1183 
1184 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1185 static int da_retry_count = DA_DEFAULT_RETRY;
1186 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1187 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1188 
1189 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1190             "CAM Direct Access Disk driver");
1191 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1192            &da_poll_period, 0, "Media polling period in seconds");
1193 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1194 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1195            &da_retry_count, 0, "Normal I/O retry count");
1196 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1197 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1198            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1199 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1200 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1201            &da_send_ordered, 0, "Send Ordered Tags");
1202 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1203 
1204 /*
1205  * DA_ORDEREDTAG_INTERVAL determines how often, relative
1206  * to the default timeout, we check to see whether an ordered
1207  * tagged transaction is appropriate to prevent simple tag
1208  * starvation.  Since we'd like to ensure that there is at least
1209  * 1/2 of the timeout length left for a starved transaction to
1210  * complete after we've sent an ordered tag, we must poll at least
1211  * four times in every timeout period.  This takes care of the worst
1212  * case where a starved transaction starts during an interval that
1213  * meets the requirement "don't send an ordered tag" test so it takes
1214  * us two intervals to determine that a tag must be sent.
1215  */
1216 #ifndef DA_ORDEREDTAG_INTERVAL
1217 #define DA_ORDEREDTAG_INTERVAL 4
1218 #endif
1219 
1220 static struct periph_driver dadriver =
1221 {
1222 	dainit, "da",
1223 	TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1224 };
1225 
1226 PERIPHDRIVER_DECLARE(da, dadriver);
1227 
1228 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1229 
1230 static int
1231 daopen(struct disk *dp)
1232 {
1233 	struct cam_periph *periph;
1234 	struct da_softc *softc;
1235 	int error;
1236 
1237 	periph = (struct cam_periph *)dp->d_drv1;
1238 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1239 		return (ENXIO);
1240 	}
1241 
1242 	cam_periph_lock(periph);
1243 	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1244 		cam_periph_unlock(periph);
1245 		cam_periph_release(periph);
1246 		return (error);
1247 	}
1248 
1249 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1250 	    ("daopen\n"));
1251 
1252 	softc = (struct da_softc *)periph->softc;
1253 	dareprobe(periph);
1254 
1255 	/* Wait for the disk size update.  */
1256 	error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1257 	    "dareprobe", 0);
1258 	if (error != 0)
1259 		xpt_print(periph->path, "unable to retrieve capacity data\n");
1260 
1261 	if (periph->flags & CAM_PERIPH_INVALID)
1262 		error = ENXIO;
1263 
1264 	if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1265 	    (softc->quirks & DA_Q_NO_PREVENT) == 0)
1266 		daprevent(periph, PR_PREVENT);
1267 
1268 	if (error == 0) {
1269 		softc->flags &= ~DA_FLAG_PACK_INVALID;
1270 		softc->flags |= DA_FLAG_OPEN;
1271 	}
1272 
1273 	cam_periph_unhold(periph);
1274 	cam_periph_unlock(periph);
1275 
1276 	if (error != 0)
1277 		cam_periph_release(periph);
1278 
1279 	return (error);
1280 }
1281 
1282 static int
1283 daclose(struct disk *dp)
1284 {
1285 	struct	cam_periph *periph;
1286 	struct	da_softc *softc;
1287 	union	ccb *ccb;
1288 	int error;
1289 
1290 	periph = (struct cam_periph *)dp->d_drv1;
1291 	softc = (struct da_softc *)periph->softc;
1292 	cam_periph_lock(periph);
1293 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1294 	    ("daclose\n"));
1295 
1296 	if (cam_periph_hold(periph, PRIBIO) == 0) {
1297 
1298 		/* Flush disk cache. */
1299 		if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1300 		    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1301 		    (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1302 			ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1303 			scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1304 			    /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1305 			    /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1306 			    5 * 60 * 1000);
1307 			error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1308 			    /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1309 			    softc->disk->d_devstat);
1310 			if (error == 0)
1311 				softc->flags &= ~DA_FLAG_DIRTY;
1312 			xpt_release_ccb(ccb);
1313 		}
1314 
1315 		/* Allow medium removal. */
1316 		if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1317 		    (softc->quirks & DA_Q_NO_PREVENT) == 0)
1318 			daprevent(periph, PR_ALLOW);
1319 
1320 		cam_periph_unhold(periph);
1321 	}
1322 
1323 	/*
1324 	 * If we've got removeable media, mark the blocksize as
1325 	 * unavailable, since it could change when new media is
1326 	 * inserted.
1327 	 */
1328 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1329 		softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1330 
1331 	softc->flags &= ~DA_FLAG_OPEN;
1332 	while (softc->refcount != 0)
1333 		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1334 	cam_periph_unlock(periph);
1335 	cam_periph_release(periph);
1336 	return (0);
1337 }
1338 
1339 static void
1340 daschedule(struct cam_periph *periph)
1341 {
1342 	struct da_softc *softc = (struct da_softc *)periph->softc;
1343 
1344 	if (softc->state != DA_STATE_NORMAL)
1345 		return;
1346 
1347 	/* Check if we have more work to do. */
1348 	if (bioq_first(&softc->bio_queue) ||
1349 	    (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1350 	    softc->tur) {
1351 		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1352 	}
1353 }
1354 
1355 /*
1356  * Actually translate the requested transfer into one the physical driver
1357  * can understand.  The transfer is described by a buf and will include
1358  * only one physical transfer.
1359  */
1360 static void
1361 dastrategy(struct bio *bp)
1362 {
1363 	struct cam_periph *periph;
1364 	struct da_softc *softc;
1365 
1366 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1367 	softc = (struct da_softc *)periph->softc;
1368 
1369 	cam_periph_lock(periph);
1370 
1371 	/*
1372 	 * If the device has been made invalid, error out
1373 	 */
1374 	if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1375 		cam_periph_unlock(periph);
1376 		biofinish(bp, NULL, ENXIO);
1377 		return;
1378 	}
1379 
1380 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1381 
1382 	/*
1383 	 * Place it in the queue of disk activities for this disk
1384 	 */
1385 	if (bp->bio_cmd == BIO_DELETE) {
1386 		bioq_disksort(&softc->delete_queue, bp);
1387 	} else if (DA_SIO) {
1388 		bioq_disksort(&softc->bio_queue, bp);
1389 	} else {
1390 		bioq_insert_tail(&softc->bio_queue, bp);
1391 	}
1392 
1393 	/*
1394 	 * Schedule ourselves for performing the work.
1395 	 */
1396 	daschedule(periph);
1397 	cam_periph_unlock(periph);
1398 
1399 	return;
1400 }
1401 
1402 static int
1403 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1404 {
1405 	struct	    cam_periph *periph;
1406 	struct	    da_softc *softc;
1407 	u_int	    secsize;
1408 	struct	    ccb_scsiio csio;
1409 	struct	    disk *dp;
1410 	int	    error = 0;
1411 
1412 	dp = arg;
1413 	periph = dp->d_drv1;
1414 	softc = (struct da_softc *)periph->softc;
1415 	cam_periph_lock(periph);
1416 	secsize = softc->params.secsize;
1417 
1418 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1419 		cam_periph_unlock(periph);
1420 		return (ENXIO);
1421 	}
1422 
1423 	if (length > 0) {
1424 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1425 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1426 		scsi_read_write(&csio,
1427 				/*retries*/0,
1428 				dadone,
1429 				MSG_ORDERED_Q_TAG,
1430 				/*read*/SCSI_RW_WRITE,
1431 				/*byte2*/0,
1432 				/*minimum_cmd_size*/ softc->minimum_cmd_size,
1433 				offset / secsize,
1434 				length / secsize,
1435 				/*data_ptr*/(u_int8_t *) virtual,
1436 				/*dxfer_len*/length,
1437 				/*sense_len*/SSD_FULL_SIZE,
1438 				da_default_timeout * 1000);
1439 		xpt_polled_action((union ccb *)&csio);
1440 
1441 		error = cam_periph_error((union ccb *)&csio,
1442 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1443 		if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1444 			cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1445 			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1446 		if (error != 0)
1447 			printf("Aborting dump due to I/O error.\n");
1448 		cam_periph_unlock(periph);
1449 		return (error);
1450 	}
1451 
1452 	/*
1453 	 * Sync the disk cache contents to the physical media.
1454 	 */
1455 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1456 
1457 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1458 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1459 		scsi_synchronize_cache(&csio,
1460 				       /*retries*/0,
1461 				       /*cbfcnp*/dadone,
1462 				       MSG_SIMPLE_Q_TAG,
1463 				       /*begin_lba*/0,/* Cover the whole disk */
1464 				       /*lb_count*/0,
1465 				       SSD_FULL_SIZE,
1466 				       5 * 60 * 1000);
1467 		xpt_polled_action((union ccb *)&csio);
1468 
1469 		error = cam_periph_error((union ccb *)&csio,
1470 		    0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1471 		if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1472 			cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1473 			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1474 		if (error != 0)
1475 			xpt_print(periph->path, "Synchronize cache failed\n");
1476 	}
1477 	cam_periph_unlock(periph);
1478 	return (error);
1479 }
1480 
1481 static int
1482 dagetattr(struct bio *bp)
1483 {
1484 	int ret;
1485 	struct cam_periph *periph;
1486 
1487 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1488 	cam_periph_lock(periph);
1489 	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1490 	    periph->path);
1491 	cam_periph_unlock(periph);
1492 	if (ret == 0)
1493 		bp->bio_completed = bp->bio_length;
1494 	return ret;
1495 }
1496 
1497 static void
1498 dainit(void)
1499 {
1500 	cam_status status;
1501 
1502 	/*
1503 	 * Install a global async callback.  This callback will
1504 	 * receive async callbacks like "new device found".
1505 	 */
1506 	status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1507 
1508 	if (status != CAM_REQ_CMP) {
1509 		printf("da: Failed to attach master async callback "
1510 		       "due to status 0x%x!\n", status);
1511 	} else if (da_send_ordered) {
1512 
1513 		/* Register our shutdown event handler */
1514 		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1515 					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1516 		    printf("dainit: shutdown event registration failed!\n");
1517 	}
1518 }
1519 
1520 /*
1521  * Callback from GEOM, called when it has finished cleaning up its
1522  * resources.
1523  */
1524 static void
1525 dadiskgonecb(struct disk *dp)
1526 {
1527 	struct cam_periph *periph;
1528 
1529 	periph = (struct cam_periph *)dp->d_drv1;
1530 	cam_periph_release(periph);
1531 }
1532 
1533 static void
1534 daoninvalidate(struct cam_periph *periph)
1535 {
1536 	struct da_softc *softc;
1537 
1538 	softc = (struct da_softc *)periph->softc;
1539 
1540 	/*
1541 	 * De-register any async callbacks.
1542 	 */
1543 	xpt_register_async(0, daasync, periph, periph->path);
1544 
1545 	softc->flags |= DA_FLAG_PACK_INVALID;
1546 
1547 	/*
1548 	 * Return all queued I/O with ENXIO.
1549 	 * XXX Handle any transactions queued to the card
1550 	 *     with XPT_ABORT_CCB.
1551 	 */
1552 	bioq_flush(&softc->bio_queue, NULL, ENXIO);
1553 	bioq_flush(&softc->delete_queue, NULL, ENXIO);
1554 
1555 	/*
1556 	 * Tell GEOM that we've gone away, we'll get a callback when it is
1557 	 * done cleaning up its resources.
1558 	 */
1559 	disk_gone(softc->disk);
1560 }
1561 
1562 static void
1563 dacleanup(struct cam_periph *periph)
1564 {
1565 	struct da_softc *softc;
1566 
1567 	softc = (struct da_softc *)periph->softc;
1568 
1569 	cam_periph_unlock(periph);
1570 
1571 	/*
1572 	 * If we can't free the sysctl tree, oh well...
1573 	 */
1574 	if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1575 	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1576 		xpt_print(periph->path, "can't remove sysctl context\n");
1577 	}
1578 
1579 	callout_drain(&softc->mediapoll_c);
1580 	disk_destroy(softc->disk);
1581 	callout_drain(&softc->sendordered_c);
1582 	free(softc, M_DEVBUF);
1583 	cam_periph_lock(periph);
1584 }
1585 
1586 static void
1587 daasync(void *callback_arg, u_int32_t code,
1588 	struct cam_path *path, void *arg)
1589 {
1590 	struct cam_periph *periph;
1591 	struct da_softc *softc;
1592 
1593 	periph = (struct cam_periph *)callback_arg;
1594 	switch (code) {
1595 	case AC_FOUND_DEVICE:
1596 	{
1597 		struct ccb_getdev *cgd;
1598 		cam_status status;
1599 
1600 		cgd = (struct ccb_getdev *)arg;
1601 		if (cgd == NULL)
1602 			break;
1603 
1604 		if (cgd->protocol != PROTO_SCSI)
1605 			break;
1606 
1607 		if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1608 		    && SID_TYPE(&cgd->inq_data) != T_RBC
1609 		    && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1610 			break;
1611 
1612 		/*
1613 		 * Allocate a peripheral instance for
1614 		 * this device and start the probe
1615 		 * process.
1616 		 */
1617 		status = cam_periph_alloc(daregister, daoninvalidate,
1618 					  dacleanup, dastart,
1619 					  "da", CAM_PERIPH_BIO,
1620 					  path, daasync,
1621 					  AC_FOUND_DEVICE, cgd);
1622 
1623 		if (status != CAM_REQ_CMP
1624 		 && status != CAM_REQ_INPROG)
1625 			printf("daasync: Unable to attach to new device "
1626 				"due to status 0x%x\n", status);
1627 		return;
1628 	}
1629 	case AC_ADVINFO_CHANGED:
1630 	{
1631 		uintptr_t buftype;
1632 
1633 		buftype = (uintptr_t)arg;
1634 		if (buftype == CDAI_TYPE_PHYS_PATH) {
1635 			struct da_softc *softc;
1636 
1637 			softc = periph->softc;
1638 			disk_attr_changed(softc->disk, "GEOM::physpath",
1639 					  M_NOWAIT);
1640 		}
1641 		break;
1642 	}
1643 	case AC_UNIT_ATTENTION:
1644 	{
1645 		union ccb *ccb;
1646 		int error_code, sense_key, asc, ascq;
1647 
1648 		softc = (struct da_softc *)periph->softc;
1649 		ccb = (union ccb *)arg;
1650 
1651 		/*
1652 		 * Handle all UNIT ATTENTIONs except our own,
1653 		 * as they will be handled by daerror().
1654 		 */
1655 		if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1656 		    scsi_extract_sense_ccb(ccb,
1657 		     &error_code, &sense_key, &asc, &ascq)) {
1658 			if (asc == 0x2A && ascq == 0x09) {
1659 				xpt_print(ccb->ccb_h.path,
1660 				    "Capacity data has changed\n");
1661 				softc->flags &= ~DA_FLAG_PROBED;
1662 				dareprobe(periph);
1663 			} else if (asc == 0x28 && ascq == 0x00) {
1664 				softc->flags &= ~DA_FLAG_PROBED;
1665 				disk_media_changed(softc->disk, M_NOWAIT);
1666 			} else if (asc == 0x3F && ascq == 0x03) {
1667 				xpt_print(ccb->ccb_h.path,
1668 				    "INQUIRY data has changed\n");
1669 				softc->flags &= ~DA_FLAG_PROBED;
1670 				dareprobe(periph);
1671 			}
1672 		}
1673 		cam_periph_async(periph, code, path, arg);
1674 		break;
1675 	}
1676 	case AC_SCSI_AEN:
1677 		softc = (struct da_softc *)periph->softc;
1678 		if (!softc->tur) {
1679 			if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1680 				softc->tur = 1;
1681 				daschedule(periph);
1682 			}
1683 		}
1684 		/* FALLTHROUGH */
1685 	case AC_SENT_BDR:
1686 	case AC_BUS_RESET:
1687 	{
1688 		struct ccb_hdr *ccbh;
1689 
1690 		softc = (struct da_softc *)periph->softc;
1691 		/*
1692 		 * Don't fail on the expected unit attention
1693 		 * that will occur.
1694 		 */
1695 		softc->flags |= DA_FLAG_RETRY_UA;
1696 		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1697 			ccbh->ccb_state |= DA_CCB_RETRY_UA;
1698 		break;
1699 	}
1700 	default:
1701 		break;
1702 	}
1703 	cam_periph_async(periph, code, path, arg);
1704 }
1705 
1706 static void
1707 dasysctlinit(void *context, int pending)
1708 {
1709 	struct cam_periph *periph;
1710 	struct da_softc *softc;
1711 	char tmpstr[80], tmpstr2[80];
1712 	struct ccb_trans_settings cts;
1713 
1714 	periph = (struct cam_periph *)context;
1715 	/*
1716 	 * periph was held for us when this task was enqueued
1717 	 */
1718 	if (periph->flags & CAM_PERIPH_INVALID) {
1719 		cam_periph_release(periph);
1720 		return;
1721 	}
1722 
1723 	softc = (struct da_softc *)periph->softc;
1724 	snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1725 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1726 
1727 	sysctl_ctx_init(&softc->sysctl_ctx);
1728 	softc->flags |= DA_FLAG_SCTX_INIT;
1729 	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1730 		SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1731 		CTLFLAG_RD, 0, tmpstr);
1732 	if (softc->sysctl_tree == NULL) {
1733 		printf("dasysctlinit: unable to allocate sysctl tree\n");
1734 		cam_periph_release(periph);
1735 		return;
1736 	}
1737 
1738 	/*
1739 	 * Now register the sysctl handler, so the user can change the value on
1740 	 * the fly.
1741 	 */
1742 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1743 		OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1744 		softc, 0, dadeletemethodsysctl, "A",
1745 		"BIO_DELETE execution method");
1746 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1747 		OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1748 		softc, 0, dadeletemaxsysctl, "Q",
1749 		"Maximum BIO_DELETE size");
1750 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1751 		OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1752 		&softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1753 		"Minimum CDB size");
1754 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1755 		OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1756 		"Sort IO queue to try and optimise disk access patterns");
1757 
1758 	SYSCTL_ADD_INT(&softc->sysctl_ctx,
1759 		       SYSCTL_CHILDREN(softc->sysctl_tree),
1760 		       OID_AUTO,
1761 		       "error_inject",
1762 		       CTLFLAG_RW,
1763 		       &softc->error_inject,
1764 		       0,
1765 		       "error_inject leaf");
1766 
1767 
1768 	/*
1769 	 * Add some addressing info.
1770 	 */
1771 	memset(&cts, 0, sizeof (cts));
1772 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1773 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1774 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1775 	cam_periph_lock(periph);
1776 	xpt_action((union ccb *)&cts);
1777 	cam_periph_unlock(periph);
1778 	if (cts.ccb_h.status != CAM_REQ_CMP) {
1779 		cam_periph_release(periph);
1780 		return;
1781 	}
1782 	if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1783 		struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1784 		if (fc->valid & CTS_FC_VALID_WWPN) {
1785 			softc->wwpn = fc->wwpn;
1786 			SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1787 			    SYSCTL_CHILDREN(softc->sysctl_tree),
1788 			    OID_AUTO, "wwpn", CTLFLAG_RD,
1789 			    &softc->wwpn, "World Wide Port Name");
1790 		}
1791 	}
1792 	cam_periph_release(periph);
1793 }
1794 
1795 static int
1796 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1797 {
1798 	int error;
1799 	uint64_t value;
1800 	struct da_softc *softc;
1801 
1802 	softc = (struct da_softc *)arg1;
1803 
1804 	value = softc->disk->d_delmaxsize;
1805 	error = sysctl_handle_64(oidp, &value, 0, req);
1806 	if ((error != 0) || (req->newptr == NULL))
1807 		return (error);
1808 
1809 	/* only accept values smaller than the calculated value */
1810 	if (value > dadeletemaxsize(softc, softc->delete_method)) {
1811 		return (EINVAL);
1812 	}
1813 	softc->disk->d_delmaxsize = value;
1814 
1815 	return (0);
1816 }
1817 
1818 static int
1819 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1820 {
1821 	int error, value;
1822 
1823 	value = *(int *)arg1;
1824 
1825 	error = sysctl_handle_int(oidp, &value, 0, req);
1826 
1827 	if ((error != 0)
1828 	 || (req->newptr == NULL))
1829 		return (error);
1830 
1831 	/*
1832 	 * Acceptable values here are 6, 10, 12 or 16.
1833 	 */
1834 	if (value < 6)
1835 		value = 6;
1836 	else if ((value > 6)
1837 	      && (value <= 10))
1838 		value = 10;
1839 	else if ((value > 10)
1840 	      && (value <= 12))
1841 		value = 12;
1842 	else if (value > 12)
1843 		value = 16;
1844 
1845 	*(int *)arg1 = value;
1846 
1847 	return (0);
1848 }
1849 
1850 static void
1851 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1852 {
1853 
1854 
1855 	softc->delete_method = delete_method;
1856 	softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1857 	softc->delete_func = da_delete_functions[delete_method];
1858 
1859 	if (softc->delete_method > DA_DELETE_DISABLE)
1860 		softc->disk->d_flags |= DISKFLAG_CANDELETE;
1861 	else
1862 		softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1863 }
1864 
1865 static off_t
1866 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1867 {
1868 	off_t sectors;
1869 
1870 	switch(delete_method) {
1871 	case DA_DELETE_UNMAP:
1872 		sectors = (off_t)softc->unmap_max_lba;
1873 		break;
1874 	case DA_DELETE_ATA_TRIM:
1875 		sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1876 		break;
1877 	case DA_DELETE_WS16:
1878 		sectors = (off_t)min(softc->ws_max_blks, WS16_MAX_BLKS);
1879 		break;
1880 	case DA_DELETE_ZERO:
1881 	case DA_DELETE_WS10:
1882 		sectors = (off_t)min(softc->ws_max_blks, WS10_MAX_BLKS);
1883 		break;
1884 	default:
1885 		return 0;
1886 	}
1887 
1888 	return (off_t)softc->params.secsize *
1889 	    min(sectors, (off_t)softc->params.sectors);
1890 }
1891 
1892 static void
1893 daprobedone(struct cam_periph *periph, union ccb *ccb)
1894 {
1895 	struct da_softc *softc;
1896 
1897 	softc = (struct da_softc *)periph->softc;
1898 
1899 	dadeletemethodchoose(softc, DA_DELETE_NONE);
1900 
1901 	if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1902 		char buf[80];
1903 		int i, sep;
1904 
1905 		snprintf(buf, sizeof(buf), "Delete methods: <");
1906 		sep = 0;
1907 		for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1908 			if (softc->delete_available & (1 << i)) {
1909 				if (sep) {
1910 					strlcat(buf, ",", sizeof(buf));
1911 				} else {
1912 				    sep = 1;
1913 				}
1914 				strlcat(buf, da_delete_method_names[i],
1915 				    sizeof(buf));
1916 				if (i == softc->delete_method) {
1917 					strlcat(buf, "(*)", sizeof(buf));
1918 				}
1919 			}
1920 		}
1921 		if (sep == 0) {
1922 			if (softc->delete_method == DA_DELETE_NONE)
1923 				strlcat(buf, "NONE(*)", sizeof(buf));
1924 			else
1925 				strlcat(buf, "DISABLED(*)", sizeof(buf));
1926 		}
1927 		strlcat(buf, ">", sizeof(buf));
1928 		printf("%s%d: %s\n", periph->periph_name,
1929 		    periph->unit_number, buf);
1930 	}
1931 
1932 	/*
1933 	 * Since our peripheral may be invalidated by an error
1934 	 * above or an external event, we must release our CCB
1935 	 * before releasing the probe lock on the peripheral.
1936 	 * The peripheral will only go away once the last lock
1937 	 * is removed, and we need it around for the CCB release
1938 	 * operation.
1939 	 */
1940 	xpt_release_ccb(ccb);
1941 	softc->state = DA_STATE_NORMAL;
1942 	softc->flags |= DA_FLAG_PROBED;
1943 	daschedule(periph);
1944 	wakeup(&softc->disk->d_mediasize);
1945 	if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1946 		softc->flags |= DA_FLAG_ANNOUNCED;
1947 		cam_periph_unhold(periph);
1948 	} else
1949 		cam_periph_release_locked(periph);
1950 }
1951 
1952 static void
1953 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
1954 {
1955 	int i, delete_method;
1956 
1957 	delete_method = default_method;
1958 
1959 	/*
1960 	 * Use the pre-defined order to choose the best
1961 	 * performing delete.
1962 	 */
1963 	for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1964 		if (softc->delete_available & (1 << i)) {
1965 			dadeletemethodset(softc, i);
1966 			return;
1967 		}
1968 	}
1969 	dadeletemethodset(softc, delete_method);
1970 }
1971 
1972 static int
1973 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1974 {
1975 	char buf[16];
1976 	const char *p;
1977 	struct da_softc *softc;
1978 	int i, error, methods, value;
1979 
1980 	softc = (struct da_softc *)arg1;
1981 
1982 	value = softc->delete_method;
1983 	if (value < 0 || value > DA_DELETE_MAX)
1984 		p = "UNKNOWN";
1985 	else
1986 		p = da_delete_method_names[value];
1987 	strncpy(buf, p, sizeof(buf));
1988 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1989 	if (error != 0 || req->newptr == NULL)
1990 		return (error);
1991 	methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
1992 	for (i = 0; i <= DA_DELETE_MAX; i++) {
1993 		if (!(methods & (1 << i)) ||
1994 		    strcmp(buf, da_delete_method_names[i]) != 0)
1995 			continue;
1996 		dadeletemethodset(softc, i);
1997 		return (0);
1998 	}
1999 	return (EINVAL);
2000 }
2001 
2002 static cam_status
2003 daregister(struct cam_periph *periph, void *arg)
2004 {
2005 	struct da_softc *softc;
2006 	struct ccb_pathinq cpi;
2007 	struct ccb_getdev *cgd;
2008 	char tmpstr[80];
2009 	caddr_t match;
2010 
2011 	cgd = (struct ccb_getdev *)arg;
2012 	if (cgd == NULL) {
2013 		printf("daregister: no getdev CCB, can't register device\n");
2014 		return(CAM_REQ_CMP_ERR);
2015 	}
2016 
2017 	softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2018 	    M_NOWAIT|M_ZERO);
2019 
2020 	if (softc == NULL) {
2021 		printf("daregister: Unable to probe new device. "
2022 		       "Unable to allocate softc\n");
2023 		return(CAM_REQ_CMP_ERR);
2024 	}
2025 
2026 	LIST_INIT(&softc->pending_ccbs);
2027 	softc->state = DA_STATE_PROBE_RC;
2028 	bioq_init(&softc->bio_queue);
2029 	bioq_init(&softc->delete_queue);
2030 	bioq_init(&softc->delete_run_queue);
2031 	if (SID_IS_REMOVABLE(&cgd->inq_data))
2032 		softc->flags |= DA_FLAG_PACK_REMOVABLE;
2033 	softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2034 	softc->unmap_max_lba = UNMAP_RANGE_MAX;
2035 	softc->ws_max_blks = WS16_MAX_BLKS;
2036 	softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2037 	softc->sort_io_queue = -1;
2038 
2039 	periph->softc = softc;
2040 
2041 	/*
2042 	 * See if this device has any quirks.
2043 	 */
2044 	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2045 			       (caddr_t)da_quirk_table,
2046 			       sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2047 			       sizeof(*da_quirk_table), scsi_inquiry_match);
2048 
2049 	if (match != NULL)
2050 		softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2051 	else
2052 		softc->quirks = DA_Q_NONE;
2053 
2054 	/* Check if the SIM does not want 6 byte commands */
2055 	bzero(&cpi, sizeof(cpi));
2056 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2057 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2058 	xpt_action((union ccb *)&cpi);
2059 	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2060 		softc->quirks |= DA_Q_NO_6_BYTE;
2061 
2062 	TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2063 
2064 	/*
2065 	 * Take an exclusive refcount on the periph while dastart is called
2066 	 * to finish the probe.  The reference will be dropped in dadone at
2067 	 * the end of probe.
2068 	 */
2069 	(void)cam_periph_hold(periph, PRIBIO);
2070 
2071 	/*
2072 	 * Schedule a periodic event to occasionally send an
2073 	 * ordered tag to a device.
2074 	 */
2075 	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2076 	callout_reset(&softc->sendordered_c,
2077 	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2078 	    dasendorderedtag, softc);
2079 
2080 	cam_periph_unlock(periph);
2081 	/*
2082 	 * RBC devices don't have to support READ(6), only READ(10).
2083 	 */
2084 	if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2085 		softc->minimum_cmd_size = 10;
2086 	else
2087 		softc->minimum_cmd_size = 6;
2088 
2089 	/*
2090 	 * Load the user's default, if any.
2091 	 */
2092 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2093 		 periph->unit_number);
2094 	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2095 
2096 	/*
2097 	 * 6, 10, 12 and 16 are the currently permissible values.
2098 	 */
2099 	if (softc->minimum_cmd_size < 6)
2100 		softc->minimum_cmd_size = 6;
2101 	else if ((softc->minimum_cmd_size > 6)
2102 	      && (softc->minimum_cmd_size <= 10))
2103 		softc->minimum_cmd_size = 10;
2104 	else if ((softc->minimum_cmd_size > 10)
2105 	      && (softc->minimum_cmd_size <= 12))
2106 		softc->minimum_cmd_size = 12;
2107 	else if (softc->minimum_cmd_size > 12)
2108 		softc->minimum_cmd_size = 16;
2109 
2110 	/* Predict whether device may support READ CAPACITY(16). */
2111 	if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2112 	    (softc->quirks & DA_Q_NO_RC16) == 0) {
2113 		softc->flags |= DA_FLAG_CAN_RC16;
2114 		softc->state = DA_STATE_PROBE_RC16;
2115 	}
2116 
2117 	/*
2118 	 * Register this media as a disk.
2119 	 */
2120 	softc->disk = disk_alloc();
2121 	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2122 			  periph->unit_number, 0,
2123 			  DEVSTAT_BS_UNAVAILABLE,
2124 			  SID_TYPE(&cgd->inq_data) |
2125 			  XPORT_DEVSTAT_TYPE(cpi.transport),
2126 			  DEVSTAT_PRIORITY_DISK);
2127 	softc->disk->d_open = daopen;
2128 	softc->disk->d_close = daclose;
2129 	softc->disk->d_strategy = dastrategy;
2130 	softc->disk->d_dump = dadump;
2131 	softc->disk->d_getattr = dagetattr;
2132 	softc->disk->d_gone = dadiskgonecb;
2133 	softc->disk->d_name = "da";
2134 	softc->disk->d_drv1 = periph;
2135 	if (cpi.maxio == 0)
2136 		softc->maxio = DFLTPHYS;	/* traditional default */
2137 	else if (cpi.maxio > MAXPHYS)
2138 		softc->maxio = MAXPHYS;		/* for safety */
2139 	else
2140 		softc->maxio = cpi.maxio;
2141 	softc->disk->d_maxsize = softc->maxio;
2142 	softc->disk->d_unit = periph->unit_number;
2143 	softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2144 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2145 		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2146 	if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2147 		softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2148 	cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2149 	    sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2150 	strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2151 	cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2152 	    cgd->inq_data.product, sizeof(cgd->inq_data.product),
2153 	    sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2154 	softc->disk->d_hba_vendor = cpi.hba_vendor;
2155 	softc->disk->d_hba_device = cpi.hba_device;
2156 	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2157 	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2158 
2159 	/*
2160 	 * Acquire a reference to the periph before we register with GEOM.
2161 	 * We'll release this reference once GEOM calls us back (via
2162 	 * dadiskgonecb()) telling us that our provider has been freed.
2163 	 */
2164 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2165 		xpt_print(periph->path, "%s: lost periph during "
2166 			  "registration!\n", __func__);
2167 		cam_periph_lock(periph);
2168 		return (CAM_REQ_CMP_ERR);
2169 	}
2170 
2171 	disk_create(softc->disk, DISK_VERSION);
2172 	cam_periph_lock(periph);
2173 
2174 	/*
2175 	 * Add async callbacks for events of interest.
2176 	 * I don't bother checking if this fails as,
2177 	 * in most cases, the system will function just
2178 	 * fine without them and the only alternative
2179 	 * would be to not attach the device on failure.
2180 	 */
2181 	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2182 	    AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2183 	    daasync, periph, periph->path);
2184 
2185 	/*
2186 	 * Emit an attribute changed notification just in case
2187 	 * physical path information arrived before our async
2188 	 * event handler was registered, but after anyone attaching
2189 	 * to our disk device polled it.
2190 	 */
2191 	disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2192 
2193 	/*
2194 	 * Schedule a periodic media polling events.
2195 	 */
2196 	callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2197 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2198 	    (cgd->inq_flags & SID_AEN) == 0 &&
2199 	    da_poll_period != 0)
2200 		callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2201 		    damediapoll, periph);
2202 
2203 	xpt_schedule(periph, CAM_PRIORITY_DEV);
2204 
2205 	return(CAM_REQ_CMP);
2206 }
2207 
2208 static void
2209 dastart(struct cam_periph *periph, union ccb *start_ccb)
2210 {
2211 	struct da_softc *softc;
2212 
2213 	softc = (struct da_softc *)periph->softc;
2214 
2215 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2216 
2217 skipstate:
2218 	switch (softc->state) {
2219 	case DA_STATE_NORMAL:
2220 	{
2221 		struct bio *bp;
2222 		uint8_t tag_code;
2223 
2224 		/* Run BIO_DELETE if not running yet. */
2225 		if (!softc->delete_running &&
2226 		    (bp = bioq_first(&softc->delete_queue)) != NULL) {
2227 			if (softc->delete_func != NULL) {
2228 				softc->delete_func(periph, start_ccb, bp);
2229 				goto out;
2230 			} else {
2231 				bioq_flush(&softc->delete_queue, NULL, 0);
2232 				/* FALLTHROUGH */
2233 			}
2234 		}
2235 
2236 		/* Run regular command. */
2237 		bp = bioq_takefirst(&softc->bio_queue);
2238 		if (bp == NULL) {
2239 			if (softc->tur) {
2240 				softc->tur = 0;
2241 				scsi_test_unit_ready(&start_ccb->csio,
2242 				     /*retries*/ da_retry_count,
2243 				     dadone,
2244 				     MSG_SIMPLE_Q_TAG,
2245 				     SSD_FULL_SIZE,
2246 				     da_default_timeout * 1000);
2247 				start_ccb->ccb_h.ccb_bp = NULL;
2248 				start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2249 				xpt_action(start_ccb);
2250 			} else
2251 				xpt_release_ccb(start_ccb);
2252 			break;
2253 		}
2254 		if (softc->tur) {
2255 			softc->tur = 0;
2256 			cam_periph_release_locked(periph);
2257 		}
2258 
2259 		if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2260 		    (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2261 			softc->flags &= ~DA_FLAG_NEED_OTAG;
2262 			softc->flags |= DA_FLAG_WAS_OTAG;
2263 			tag_code = MSG_ORDERED_Q_TAG;
2264 		} else {
2265 			tag_code = MSG_SIMPLE_Q_TAG;
2266 		}
2267 
2268 		switch (bp->bio_cmd) {
2269 		case BIO_WRITE:
2270 			softc->flags |= DA_FLAG_DIRTY;
2271 			/* FALLTHROUGH */
2272 		case BIO_READ:
2273 			scsi_read_write(&start_ccb->csio,
2274 					/*retries*/da_retry_count,
2275 					/*cbfcnp*/dadone,
2276 					/*tag_action*/tag_code,
2277 					/*read_op*/(bp->bio_cmd == BIO_READ ?
2278 					SCSI_RW_READ : SCSI_RW_WRITE) |
2279 					((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2280 					SCSI_RW_BIO : 0),
2281 					/*byte2*/0,
2282 					softc->minimum_cmd_size,
2283 					/*lba*/bp->bio_pblkno,
2284 					/*block_count*/bp->bio_bcount /
2285 					softc->params.secsize,
2286 					/*data_ptr*/ (bp->bio_flags &
2287 					BIO_UNMAPPED) != 0 ? (void *)bp :
2288 					bp->bio_data,
2289 					/*dxfer_len*/ bp->bio_bcount,
2290 					/*sense_len*/SSD_FULL_SIZE,
2291 					da_default_timeout * 1000);
2292 			break;
2293 		case BIO_FLUSH:
2294 			/*
2295 			 * BIO_FLUSH doesn't currently communicate
2296 			 * range data, so we synchronize the cache
2297 			 * over the whole disk.  We also force
2298 			 * ordered tag semantics the flush applies
2299 			 * to all previously queued I/O.
2300 			 */
2301 			scsi_synchronize_cache(&start_ccb->csio,
2302 					       /*retries*/1,
2303 					       /*cbfcnp*/dadone,
2304 					       MSG_ORDERED_Q_TAG,
2305 					       /*begin_lba*/0,
2306 					       /*lb_count*/0,
2307 					       SSD_FULL_SIZE,
2308 					       da_default_timeout*1000);
2309 			break;
2310 		}
2311 		start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2312 		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2313 
2314 out:
2315 		LIST_INSERT_HEAD(&softc->pending_ccbs,
2316 				 &start_ccb->ccb_h, periph_links.le);
2317 
2318 		/* We expect a unit attention from this device */
2319 		if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2320 			start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2321 			softc->flags &= ~DA_FLAG_RETRY_UA;
2322 		}
2323 
2324 		start_ccb->ccb_h.ccb_bp = bp;
2325 		softc->refcount++;
2326 		cam_periph_unlock(periph);
2327 		xpt_action(start_ccb);
2328 		cam_periph_lock(periph);
2329 		softc->refcount--;
2330 
2331 		/* May have more work to do, so ensure we stay scheduled */
2332 		daschedule(periph);
2333 		break;
2334 	}
2335 	case DA_STATE_PROBE_RC:
2336 	{
2337 		struct scsi_read_capacity_data *rcap;
2338 
2339 		rcap = (struct scsi_read_capacity_data *)
2340 		    malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2341 		if (rcap == NULL) {
2342 			printf("dastart: Couldn't malloc read_capacity data\n");
2343 			/* da_free_periph??? */
2344 			break;
2345 		}
2346 		scsi_read_capacity(&start_ccb->csio,
2347 				   /*retries*/da_retry_count,
2348 				   dadone,
2349 				   MSG_SIMPLE_Q_TAG,
2350 				   rcap,
2351 				   SSD_FULL_SIZE,
2352 				   /*timeout*/5000);
2353 		start_ccb->ccb_h.ccb_bp = NULL;
2354 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2355 		xpt_action(start_ccb);
2356 		break;
2357 	}
2358 	case DA_STATE_PROBE_RC16:
2359 	{
2360 		struct scsi_read_capacity_data_long *rcaplong;
2361 
2362 		rcaplong = (struct scsi_read_capacity_data_long *)
2363 			malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2364 		if (rcaplong == NULL) {
2365 			printf("dastart: Couldn't malloc read_capacity data\n");
2366 			/* da_free_periph??? */
2367 			break;
2368 		}
2369 		scsi_read_capacity_16(&start_ccb->csio,
2370 				      /*retries*/ da_retry_count,
2371 				      /*cbfcnp*/ dadone,
2372 				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
2373 				      /*lba*/ 0,
2374 				      /*reladr*/ 0,
2375 				      /*pmi*/ 0,
2376 				      /*rcap_buf*/ (uint8_t *)rcaplong,
2377 				      /*rcap_buf_len*/ sizeof(*rcaplong),
2378 				      /*sense_len*/ SSD_FULL_SIZE,
2379 				      /*timeout*/ da_default_timeout * 1000);
2380 		start_ccb->ccb_h.ccb_bp = NULL;
2381 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2382 		xpt_action(start_ccb);
2383 		break;
2384 	}
2385 	case DA_STATE_PROBE_LBP:
2386 	{
2387 		struct scsi_vpd_logical_block_prov *lbp;
2388 
2389 		if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2390 			/*
2391 			 * If we get here we don't support any SBC-3 delete
2392 			 * methods with UNMAP as the Logical Block Provisioning
2393 			 * VPD page support is required for devices which
2394 			 * support it according to T10/1799-D Revision 31
2395 			 * however older revisions of the spec don't mandate
2396 			 * this so we currently don't remove these methods
2397 			 * from the available set.
2398 			 */
2399 			softc->state = DA_STATE_PROBE_BLK_LIMITS;
2400 			goto skipstate;
2401 		}
2402 
2403 		lbp = (struct scsi_vpd_logical_block_prov *)
2404 			malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2405 
2406 		if (lbp == NULL) {
2407 			printf("dastart: Couldn't malloc lbp data\n");
2408 			/* da_free_periph??? */
2409 			break;
2410 		}
2411 
2412 		scsi_inquiry(&start_ccb->csio,
2413 			     /*retries*/da_retry_count,
2414 			     /*cbfcnp*/dadone,
2415 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
2416 			     /*inq_buf*/(u_int8_t *)lbp,
2417 			     /*inq_len*/sizeof(*lbp),
2418 			     /*evpd*/TRUE,
2419 			     /*page_code*/SVPD_LBP,
2420 			     /*sense_len*/SSD_MIN_SIZE,
2421 			     /*timeout*/da_default_timeout * 1000);
2422 		start_ccb->ccb_h.ccb_bp = NULL;
2423 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2424 		xpt_action(start_ccb);
2425 		break;
2426 	}
2427 	case DA_STATE_PROBE_BLK_LIMITS:
2428 	{
2429 		struct scsi_vpd_block_limits *block_limits;
2430 
2431 		if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2432 			/* Not supported skip to next probe */
2433 			softc->state = DA_STATE_PROBE_BDC;
2434 			goto skipstate;
2435 		}
2436 
2437 		block_limits = (struct scsi_vpd_block_limits *)
2438 			malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2439 
2440 		if (block_limits == NULL) {
2441 			printf("dastart: Couldn't malloc block_limits data\n");
2442 			/* da_free_periph??? */
2443 			break;
2444 		}
2445 
2446 		scsi_inquiry(&start_ccb->csio,
2447 			     /*retries*/da_retry_count,
2448 			     /*cbfcnp*/dadone,
2449 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
2450 			     /*inq_buf*/(u_int8_t *)block_limits,
2451 			     /*inq_len*/sizeof(*block_limits),
2452 			     /*evpd*/TRUE,
2453 			     /*page_code*/SVPD_BLOCK_LIMITS,
2454 			     /*sense_len*/SSD_MIN_SIZE,
2455 			     /*timeout*/da_default_timeout * 1000);
2456 		start_ccb->ccb_h.ccb_bp = NULL;
2457 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2458 		xpt_action(start_ccb);
2459 		break;
2460 	}
2461 	case DA_STATE_PROBE_BDC:
2462 	{
2463 		struct scsi_vpd_block_characteristics *bdc;
2464 
2465 		if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2466 			softc->state = DA_STATE_PROBE_ATA;
2467 			goto skipstate;
2468 		}
2469 
2470 		bdc = (struct scsi_vpd_block_characteristics *)
2471 			malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2472 
2473 		if (bdc == NULL) {
2474 			printf("dastart: Couldn't malloc bdc data\n");
2475 			/* da_free_periph??? */
2476 			break;
2477 		}
2478 
2479 		scsi_inquiry(&start_ccb->csio,
2480 			     /*retries*/da_retry_count,
2481 			     /*cbfcnp*/dadone,
2482 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
2483 			     /*inq_buf*/(u_int8_t *)bdc,
2484 			     /*inq_len*/sizeof(*bdc),
2485 			     /*evpd*/TRUE,
2486 			     /*page_code*/SVPD_BDC,
2487 			     /*sense_len*/SSD_MIN_SIZE,
2488 			     /*timeout*/da_default_timeout * 1000);
2489 		start_ccb->ccb_h.ccb_bp = NULL;
2490 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2491 		xpt_action(start_ccb);
2492 		break;
2493 	}
2494 	case DA_STATE_PROBE_ATA:
2495 	{
2496 		struct ata_params *ata_params;
2497 
2498 		if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2499 			daprobedone(periph, start_ccb);
2500 			break;
2501 		}
2502 
2503 		ata_params = (struct ata_params*)
2504 			malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2505 
2506 		if (ata_params == NULL) {
2507 			printf("dastart: Couldn't malloc ata_params data\n");
2508 			/* da_free_periph??? */
2509 			break;
2510 		}
2511 
2512 		scsi_ata_identify(&start_ccb->csio,
2513 				  /*retries*/da_retry_count,
2514 				  /*cbfcnp*/dadone,
2515                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
2516 				  /*data_ptr*/(u_int8_t *)ata_params,
2517 				  /*dxfer_len*/sizeof(*ata_params),
2518 				  /*sense_len*/SSD_FULL_SIZE,
2519 				  /*timeout*/da_default_timeout * 1000);
2520 		start_ccb->ccb_h.ccb_bp = NULL;
2521 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2522 		xpt_action(start_ccb);
2523 		break;
2524 	}
2525 	}
2526 }
2527 
2528 /*
2529  * In each of the methods below, while its the caller's
2530  * responsibility to ensure the request will fit into a
2531  * single device request, we might have changed the delete
2532  * method due to the device incorrectly advertising either
2533  * its supported methods or limits.
2534  *
2535  * To prevent this causing further issues we validate the
2536  * against the methods limits, and warn which would
2537  * otherwise be unnecessary.
2538  */
2539 static void
2540 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2541 {
2542 	struct da_softc *softc = (struct da_softc *)periph->softc;;
2543 	struct bio *bp1;
2544 	uint8_t *buf = softc->unmap_buf;
2545 	uint64_t lba, lastlba = (uint64_t)-1;
2546 	uint64_t totalcount = 0;
2547 	uint64_t count;
2548 	uint32_t lastcount = 0, c;
2549 	uint32_t off, ranges = 0;
2550 
2551 	/*
2552 	 * Currently this doesn't take the UNMAP
2553 	 * Granularity and Granularity Alignment
2554 	 * fields into account.
2555 	 *
2556 	 * This could result in both unoptimal unmap
2557 	 * requests as as well as UNMAP calls unmapping
2558 	 * fewer LBA's than requested.
2559 	 */
2560 
2561 	softc->delete_running = 1;
2562 	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2563 	bp1 = bp;
2564 	do {
2565 		bioq_remove(&softc->delete_queue, bp1);
2566 		if (bp1 != bp)
2567 			bioq_insert_tail(&softc->delete_run_queue, bp1);
2568 		lba = bp1->bio_pblkno;
2569 		count = bp1->bio_bcount / softc->params.secsize;
2570 
2571 		/* Try to extend the previous range. */
2572 		if (lba == lastlba) {
2573 			c = omin(count, UNMAP_RANGE_MAX - lastcount);
2574 			lastcount += c;
2575 			off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2576 			      UNMAP_HEAD_SIZE;
2577 			scsi_ulto4b(lastcount, &buf[off + 8]);
2578 			count -= c;
2579 			lba +=c;
2580 			totalcount += c;
2581 		}
2582 
2583 		while (count > 0) {
2584 			c = omin(count, UNMAP_RANGE_MAX);
2585 			if (totalcount + c > softc->unmap_max_lba ||
2586 			    ranges >= softc->unmap_max_ranges) {
2587 				xpt_print(periph->path,
2588 				    "%s issuing short delete %ld > %ld"
2589 				    "|| %d >= %d",
2590 				    da_delete_method_desc[softc->delete_method],
2591 				    totalcount + c, softc->unmap_max_lba,
2592 				    ranges, softc->unmap_max_ranges);
2593 				break;
2594 			}
2595 			off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2596 			scsi_u64to8b(lba, &buf[off + 0]);
2597 			scsi_ulto4b(c, &buf[off + 8]);
2598 			lba += c;
2599 			totalcount += c;
2600 			ranges++;
2601 			count -= c;
2602 			lastcount = c;
2603 		}
2604 		lastlba = lba;
2605 		bp1 = bioq_first(&softc->delete_queue);
2606 		if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2607 		    totalcount + bp1->bio_bcount /
2608 		    softc->params.secsize > softc->unmap_max_lba)
2609 			break;
2610 	} while (1);
2611 	scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2612 	scsi_ulto2b(ranges * 16, &buf[2]);
2613 
2614 	scsi_unmap(&ccb->csio,
2615 		   /*retries*/da_retry_count,
2616 		   /*cbfcnp*/dadone,
2617 		   /*tag_action*/MSG_SIMPLE_Q_TAG,
2618 		   /*byte2*/0,
2619 		   /*data_ptr*/ buf,
2620 		   /*dxfer_len*/ ranges * 16 + 8,
2621 		   /*sense_len*/SSD_FULL_SIZE,
2622 		   da_default_timeout * 1000);
2623 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2624 	ccb->ccb_h.flags |= CAM_UNLOCKED;
2625 }
2626 
2627 static void
2628 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2629 {
2630 	struct da_softc *softc = (struct da_softc *)periph->softc;
2631 	struct bio *bp1;
2632 	uint8_t *buf = softc->unmap_buf;
2633 	uint64_t lastlba = (uint64_t)-1;
2634 	uint64_t count;
2635 	uint64_t lba;
2636 	uint32_t lastcount = 0, c, requestcount;
2637 	int ranges = 0, off, block_count;
2638 
2639 	softc->delete_running = 1;
2640 	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2641 	bp1 = bp;
2642 	do {
2643 		bioq_remove(&softc->delete_queue, bp1);
2644 		if (bp1 != bp)
2645 			bioq_insert_tail(&softc->delete_run_queue, bp1);
2646 		lba = bp1->bio_pblkno;
2647 		count = bp1->bio_bcount / softc->params.secsize;
2648 		requestcount = count;
2649 
2650 		/* Try to extend the previous range. */
2651 		if (lba == lastlba) {
2652 			c = min(count, ATA_DSM_RANGE_MAX - lastcount);
2653 			lastcount += c;
2654 			off = (ranges - 1) * 8;
2655 			buf[off + 6] = lastcount & 0xff;
2656 			buf[off + 7] = (lastcount >> 8) & 0xff;
2657 			count -= c;
2658 			lba += c;
2659 		}
2660 
2661 		while (count > 0) {
2662 			c = min(count, ATA_DSM_RANGE_MAX);
2663 			off = ranges * 8;
2664 
2665 			buf[off + 0] = lba & 0xff;
2666 			buf[off + 1] = (lba >> 8) & 0xff;
2667 			buf[off + 2] = (lba >> 16) & 0xff;
2668 			buf[off + 3] = (lba >> 24) & 0xff;
2669 			buf[off + 4] = (lba >> 32) & 0xff;
2670 			buf[off + 5] = (lba >> 40) & 0xff;
2671 			buf[off + 6] = c & 0xff;
2672 			buf[off + 7] = (c >> 8) & 0xff;
2673 			lba += c;
2674 			ranges++;
2675 			count -= c;
2676 			lastcount = c;
2677 			if (count != 0 && ranges == softc->trim_max_ranges) {
2678 				xpt_print(periph->path,
2679 				    "%s issuing short delete %ld > %ld\n",
2680 				    da_delete_method_desc[softc->delete_method],
2681 				    requestcount,
2682 				    (softc->trim_max_ranges - ranges) *
2683 				    ATA_DSM_RANGE_MAX);
2684 				break;
2685 			}
2686 		}
2687 		lastlba = lba;
2688 		bp1 = bioq_first(&softc->delete_queue);
2689 		if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2690 		    (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2691 			break;
2692 	} while (1);
2693 
2694 	block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2695 	scsi_ata_trim(&ccb->csio,
2696 		      /*retries*/da_retry_count,
2697 		      /*cbfcnp*/dadone,
2698 		      /*tag_action*/MSG_SIMPLE_Q_TAG,
2699 		      block_count,
2700 		      /*data_ptr*/buf,
2701 		      /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2702 		      /*sense_len*/SSD_FULL_SIZE,
2703 		      da_default_timeout * 1000);
2704 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2705 	ccb->ccb_h.flags |= CAM_UNLOCKED;
2706 }
2707 
2708 /*
2709  * We calculate ws_max_blks here based off d_delmaxsize instead
2710  * of using softc->ws_max_blks as it is absolute max for the
2711  * device not the protocol max which may well be lower.
2712  */
2713 static void
2714 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2715 {
2716 	struct da_softc *softc;
2717 	struct bio *bp1;
2718 	uint64_t ws_max_blks;
2719 	uint64_t lba;
2720 	uint64_t count; /* forward compat with WS32 */
2721 
2722 	softc = (struct da_softc *)periph->softc;
2723 	ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2724 	softc->delete_running = 1;
2725 	lba = bp->bio_pblkno;
2726 	count = 0;
2727 	bp1 = bp;
2728 	do {
2729 		bioq_remove(&softc->delete_queue, bp1);
2730 		if (bp1 != bp)
2731 			bioq_insert_tail(&softc->delete_run_queue, bp1);
2732 		count += bp1->bio_bcount / softc->params.secsize;
2733 		if (count > ws_max_blks) {
2734 			xpt_print(periph->path,
2735 			    "%s issuing short delete %ld > %ld\n",
2736 			    da_delete_method_desc[softc->delete_method],
2737 			    count, ws_max_blks);
2738 			count = min(count, ws_max_blks);
2739 			break;
2740 		}
2741 		bp1 = bioq_first(&softc->delete_queue);
2742 		if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2743 		    count + bp1->bio_bcount /
2744 		    softc->params.secsize > ws_max_blks)
2745 			break;
2746 	} while (1);
2747 
2748 	scsi_write_same(&ccb->csio,
2749 			/*retries*/da_retry_count,
2750 			/*cbfcnp*/dadone,
2751 			/*tag_action*/MSG_SIMPLE_Q_TAG,
2752 			/*byte2*/softc->delete_method ==
2753 			    DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2754 			softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2755 			/*lba*/lba,
2756 			/*block_count*/count,
2757 			/*data_ptr*/ __DECONST(void *, zero_region),
2758 			/*dxfer_len*/ softc->params.secsize,
2759 			/*sense_len*/SSD_FULL_SIZE,
2760 			da_default_timeout * 1000);
2761 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2762 	ccb->ccb_h.flags |= CAM_UNLOCKED;
2763 }
2764 
2765 static int
2766 cmd6workaround(union ccb *ccb)
2767 {
2768 	struct scsi_rw_6 cmd6;
2769 	struct scsi_rw_10 *cmd10;
2770 	struct da_softc *softc;
2771 	u_int8_t *cdb;
2772 	struct bio *bp;
2773 	int frozen;
2774 
2775 	cdb = ccb->csio.cdb_io.cdb_bytes;
2776 	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2777 
2778 	if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2779 		da_delete_methods old_method = softc->delete_method;
2780 
2781 		/*
2782 		 * Typically there are two reasons for failure here
2783 		 * 1. Delete method was detected as supported but isn't
2784 		 * 2. Delete failed due to invalid params e.g. too big
2785 		 *
2786 		 * While we will attempt to choose an alternative delete method
2787 		 * this may result in short deletes if the existing delete
2788 		 * requests from geom are big for the new method choosen.
2789 		 *
2790 		 * This method assumes that the error which triggered this
2791 		 * will not retry the io otherwise a panic will occur
2792 		 */
2793 		dadeleteflag(softc, old_method, 0);
2794 		dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2795 		if (softc->delete_method == DA_DELETE_DISABLE)
2796 			xpt_print(ccb->ccb_h.path,
2797 				  "%s failed, disabling BIO_DELETE\n",
2798 				  da_delete_method_desc[old_method]);
2799 		else
2800 			xpt_print(ccb->ccb_h.path,
2801 				  "%s failed, switching to %s BIO_DELETE\n",
2802 				  da_delete_method_desc[old_method],
2803 				  da_delete_method_desc[softc->delete_method]);
2804 
2805 		while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2806 			bioq_disksort(&softc->delete_queue, bp);
2807 		bioq_disksort(&softc->delete_queue,
2808 		    (struct bio *)ccb->ccb_h.ccb_bp);
2809 		ccb->ccb_h.ccb_bp = NULL;
2810 		return (0);
2811 	}
2812 
2813 	/* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2814 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2815 	    (*cdb == PREVENT_ALLOW) &&
2816 	    (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2817 		if (bootverbose)
2818 			xpt_print(ccb->ccb_h.path,
2819 			    "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2820 		softc->quirks |= DA_Q_NO_PREVENT;
2821 		return (0);
2822 	}
2823 
2824 	/* Detect unsupported SYNCHRONIZE CACHE(10). */
2825 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2826 	    (*cdb == SYNCHRONIZE_CACHE) &&
2827 	    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2828 		if (bootverbose)
2829 			xpt_print(ccb->ccb_h.path,
2830 			    "SYNCHRONIZE CACHE(10) not supported.\n");
2831 		softc->quirks |= DA_Q_NO_SYNC_CACHE;
2832 		softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2833 		return (0);
2834 	}
2835 
2836 	/* Translation only possible if CDB is an array and cmd is R/W6 */
2837 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2838 	    (*cdb != READ_6 && *cdb != WRITE_6))
2839 		return 0;
2840 
2841 	xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2842 	    "increasing minimum_cmd_size to 10.\n");
2843  	softc->minimum_cmd_size = 10;
2844 
2845 	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2846 	cmd10 = (struct scsi_rw_10 *)cdb;
2847 	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2848 	cmd10->byte2 = 0;
2849 	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2850 	cmd10->reserved = 0;
2851 	scsi_ulto2b(cmd6.length, cmd10->length);
2852 	cmd10->control = cmd6.control;
2853 	ccb->csio.cdb_len = sizeof(*cmd10);
2854 
2855 	/* Requeue request, unfreezing queue if necessary */
2856 	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2857  	ccb->ccb_h.status = CAM_REQUEUE_REQ;
2858 	xpt_action(ccb);
2859 	if (frozen) {
2860 		cam_release_devq(ccb->ccb_h.path,
2861 				 /*relsim_flags*/0,
2862 				 /*reduction*/0,
2863 				 /*timeout*/0,
2864 				 /*getcount_only*/0);
2865 	}
2866 	return (ERESTART);
2867 }
2868 
2869 static void
2870 dadone(struct cam_periph *periph, union ccb *done_ccb)
2871 {
2872 	struct da_softc *softc;
2873 	struct ccb_scsiio *csio;
2874 	u_int32_t  priority;
2875 	da_ccb_state state;
2876 
2877 	softc = (struct da_softc *)periph->softc;
2878 	priority = done_ccb->ccb_h.pinfo.priority;
2879 
2880 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2881 
2882 	csio = &done_ccb->csio;
2883 	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2884 	switch (state) {
2885 	case DA_CCB_BUFFER_IO:
2886 	case DA_CCB_DELETE:
2887 	{
2888 		struct bio *bp, *bp1;
2889 
2890 		cam_periph_lock(periph);
2891 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2892 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2893 			int error;
2894 			int sf;
2895 
2896 			if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2897 				sf = SF_RETRY_UA;
2898 			else
2899 				sf = 0;
2900 
2901 			error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2902 			if (error == ERESTART) {
2903 				/*
2904 				 * A retry was scheduled, so
2905 				 * just return.
2906 				 */
2907 				cam_periph_unlock(periph);
2908 				return;
2909 			}
2910 			bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2911 			if (error != 0) {
2912 				int queued_error;
2913 
2914 				/*
2915 				 * return all queued I/O with EIO, so that
2916 				 * the client can retry these I/Os in the
2917 				 * proper order should it attempt to recover.
2918 				 */
2919 				queued_error = EIO;
2920 
2921 				if (error == ENXIO
2922 				 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2923 					/*
2924 					 * Catastrophic error.  Mark our pack as
2925 					 * invalid.
2926 					 */
2927 					/*
2928 					 * XXX See if this is really a media
2929 					 * XXX change first?
2930 					 */
2931 					xpt_print(periph->path,
2932 					    "Invalidating pack\n");
2933 					softc->flags |= DA_FLAG_PACK_INVALID;
2934 					queued_error = ENXIO;
2935 				}
2936 				bioq_flush(&softc->bio_queue, NULL,
2937 					   queued_error);
2938 				if (bp != NULL) {
2939 					bp->bio_error = error;
2940 					bp->bio_resid = bp->bio_bcount;
2941 					bp->bio_flags |= BIO_ERROR;
2942 				}
2943 			} else if (bp != NULL) {
2944 				if (state == DA_CCB_DELETE)
2945 					bp->bio_resid = 0;
2946 				else
2947 					bp->bio_resid = csio->resid;
2948 				bp->bio_error = 0;
2949 				if (bp->bio_resid != 0)
2950 					bp->bio_flags |= BIO_ERROR;
2951 			}
2952 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2953 				cam_release_devq(done_ccb->ccb_h.path,
2954 						 /*relsim_flags*/0,
2955 						 /*reduction*/0,
2956 						 /*timeout*/0,
2957 						 /*getcount_only*/0);
2958 		} else if (bp != NULL) {
2959 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2960 				panic("REQ_CMP with QFRZN");
2961 			if (state == DA_CCB_DELETE)
2962 				bp->bio_resid = 0;
2963 			else
2964 				bp->bio_resid = csio->resid;
2965 			if (csio->resid > 0)
2966 				bp->bio_flags |= BIO_ERROR;
2967 			if (softc->error_inject != 0) {
2968 				bp->bio_error = softc->error_inject;
2969 				bp->bio_resid = bp->bio_bcount;
2970 				bp->bio_flags |= BIO_ERROR;
2971 				softc->error_inject = 0;
2972 			}
2973 		}
2974 
2975 		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2976 		if (LIST_EMPTY(&softc->pending_ccbs))
2977 			softc->flags |= DA_FLAG_WAS_OTAG;
2978 
2979 		xpt_release_ccb(done_ccb);
2980 		if (state == DA_CCB_DELETE) {
2981 			TAILQ_HEAD(, bio) queue;
2982 
2983 			TAILQ_INIT(&queue);
2984 			TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
2985 			softc->delete_run_queue.insert_point = NULL;
2986 			softc->delete_running = 0;
2987 			daschedule(periph);
2988 			cam_periph_unlock(periph);
2989 			while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
2990 				TAILQ_REMOVE(&queue, bp1, bio_queue);
2991 				bp1->bio_error = bp->bio_error;
2992 				if (bp->bio_flags & BIO_ERROR) {
2993 					bp1->bio_flags |= BIO_ERROR;
2994 					bp1->bio_resid = bp1->bio_bcount;
2995 				} else
2996 					bp1->bio_resid = 0;
2997 				biodone(bp1);
2998 			}
2999 		} else
3000 			cam_periph_unlock(periph);
3001 		if (bp != NULL)
3002 			biodone(bp);
3003 		return;
3004 	}
3005 	case DA_CCB_PROBE_RC:
3006 	case DA_CCB_PROBE_RC16:
3007 	{
3008 		struct	   scsi_read_capacity_data *rdcap;
3009 		struct     scsi_read_capacity_data_long *rcaplong;
3010 		char	   announce_buf[80];
3011 		int	   lbp;
3012 
3013 		lbp = 0;
3014 		rdcap = NULL;
3015 		rcaplong = NULL;
3016 		if (state == DA_CCB_PROBE_RC)
3017 			rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3018 		else
3019 			rcaplong = (struct scsi_read_capacity_data_long *)
3020 				csio->data_ptr;
3021 
3022 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3023 			struct disk_params *dp;
3024 			uint32_t block_size;
3025 			uint64_t maxsector;
3026 			u_int lalba;	/* Lowest aligned LBA. */
3027 
3028 			if (state == DA_CCB_PROBE_RC) {
3029 				block_size = scsi_4btoul(rdcap->length);
3030 				maxsector = scsi_4btoul(rdcap->addr);
3031 				lalba = 0;
3032 
3033 				/*
3034 				 * According to SBC-2, if the standard 10
3035 				 * byte READ CAPACITY command returns 2^32,
3036 				 * we should issue the 16 byte version of
3037 				 * the command, since the device in question
3038 				 * has more sectors than can be represented
3039 				 * with the short version of the command.
3040 				 */
3041 				if (maxsector == 0xffffffff) {
3042 					free(rdcap, M_SCSIDA);
3043 					xpt_release_ccb(done_ccb);
3044 					softc->state = DA_STATE_PROBE_RC16;
3045 					xpt_schedule(periph, priority);
3046 					return;
3047 				}
3048 			} else {
3049 				block_size = scsi_4btoul(rcaplong->length);
3050 				maxsector = scsi_8btou64(rcaplong->addr);
3051 				lalba = scsi_2btoul(rcaplong->lalba_lbp);
3052 			}
3053 
3054 			/*
3055 			 * Because GEOM code just will panic us if we
3056 			 * give them an 'illegal' value we'll avoid that
3057 			 * here.
3058 			 */
3059 			if (block_size == 0 && maxsector == 0) {
3060 				block_size = 512;
3061 				maxsector = -1;
3062 			}
3063 			if (block_size >= MAXPHYS || block_size == 0) {
3064 				xpt_print(periph->path,
3065 				    "unsupportable block size %ju\n",
3066 				    (uintmax_t) block_size);
3067 				announce_buf[0] = '\0';
3068 				cam_periph_invalidate(periph);
3069 			} else {
3070 				/*
3071 				 * We pass rcaplong into dasetgeom(),
3072 				 * because it will only use it if it is
3073 				 * non-NULL.
3074 				 */
3075 				dasetgeom(periph, block_size, maxsector,
3076 					  rcaplong, sizeof(*rcaplong));
3077 				lbp = (lalba & SRC16_LBPME_A);
3078 				dp = &softc->params;
3079 				snprintf(announce_buf, sizeof(announce_buf),
3080 				        "%juMB (%ju %u byte sectors: %dH %dS/T "
3081                                         "%dC)", (uintmax_t)
3082 	                                (((uintmax_t)dp->secsize *
3083 				        dp->sectors) / (1024*1024)),
3084 			                (uintmax_t)dp->sectors,
3085 				        dp->secsize, dp->heads,
3086                                         dp->secs_per_track, dp->cylinders);
3087 			}
3088 		} else {
3089 			int	error;
3090 
3091 			announce_buf[0] = '\0';
3092 
3093 			/*
3094 			 * Retry any UNIT ATTENTION type errors.  They
3095 			 * are expected at boot.
3096 			 */
3097 			error = daerror(done_ccb, CAM_RETRY_SELTO,
3098 					SF_RETRY_UA|SF_NO_PRINT);
3099 			if (error == ERESTART) {
3100 				/*
3101 				 * A retry was scheuled, so
3102 				 * just return.
3103 				 */
3104 				return;
3105 			} else if (error != 0) {
3106 				int asc, ascq;
3107 				int sense_key, error_code;
3108 				int have_sense;
3109 				cam_status status;
3110 				struct ccb_getdev cgd;
3111 
3112 				/* Don't wedge this device's queue */
3113 				status = done_ccb->ccb_h.status;
3114 				if ((status & CAM_DEV_QFRZN) != 0)
3115 					cam_release_devq(done_ccb->ccb_h.path,
3116 							 /*relsim_flags*/0,
3117 							 /*reduction*/0,
3118 							 /*timeout*/0,
3119 							 /*getcount_only*/0);
3120 
3121 
3122 				xpt_setup_ccb(&cgd.ccb_h,
3123 					      done_ccb->ccb_h.path,
3124 					      CAM_PRIORITY_NORMAL);
3125 				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3126 				xpt_action((union ccb *)&cgd);
3127 
3128 				if (scsi_extract_sense_ccb(done_ccb,
3129 				    &error_code, &sense_key, &asc, &ascq))
3130 					have_sense = TRUE;
3131 				else
3132 					have_sense = FALSE;
3133 
3134 				/*
3135 				 * If we tried READ CAPACITY(16) and failed,
3136 				 * fallback to READ CAPACITY(10).
3137 				 */
3138 				if ((state == DA_CCB_PROBE_RC16) &&
3139 				    (softc->flags & DA_FLAG_CAN_RC16) &&
3140 				    (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3141 					CAM_REQ_INVALID) ||
3142 				     ((have_sense) &&
3143 				      (error_code == SSD_CURRENT_ERROR) &&
3144 				      (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3145 					softc->flags &= ~DA_FLAG_CAN_RC16;
3146 					free(rdcap, M_SCSIDA);
3147 					xpt_release_ccb(done_ccb);
3148 					softc->state = DA_STATE_PROBE_RC;
3149 					xpt_schedule(periph, priority);
3150 					return;
3151 				} else
3152 				/*
3153 				 * Attach to anything that claims to be a
3154 				 * direct access or optical disk device,
3155 				 * as long as it doesn't return a "Logical
3156 				 * unit not supported" (0x25) error.
3157 				 */
3158 				if ((have_sense) && (asc != 0x25)
3159 				 && (error_code == SSD_CURRENT_ERROR)) {
3160 					const char *sense_key_desc;
3161 					const char *asc_desc;
3162 
3163 					dasetgeom(periph, 512, -1, NULL, 0);
3164 					scsi_sense_desc(sense_key, asc, ascq,
3165 							&cgd.inq_data,
3166 							&sense_key_desc,
3167 							&asc_desc);
3168 					snprintf(announce_buf,
3169 					    sizeof(announce_buf),
3170 						"Attempt to query device "
3171 						"size failed: %s, %s",
3172 						sense_key_desc,
3173 						asc_desc);
3174 				} else {
3175 					if (have_sense)
3176 						scsi_sense_print(
3177 							&done_ccb->csio);
3178 					else {
3179 						xpt_print(periph->path,
3180 						    "got CAM status %#x\n",
3181 						    done_ccb->ccb_h.status);
3182 					}
3183 
3184 					xpt_print(periph->path, "fatal error, "
3185 					    "failed to attach to device\n");
3186 
3187 					/*
3188 					 * Free up resources.
3189 					 */
3190 					cam_periph_invalidate(periph);
3191 				}
3192 			}
3193 		}
3194 		free(csio->data_ptr, M_SCSIDA);
3195 		if (announce_buf[0] != '\0' &&
3196 		    ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3197 			/*
3198 			 * Create our sysctl variables, now that we know
3199 			 * we have successfully attached.
3200 			 */
3201 			/* increase the refcount */
3202 			if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3203 				taskqueue_enqueue(taskqueue_thread,
3204 						  &softc->sysctl_task);
3205 				xpt_announce_periph(periph, announce_buf);
3206 				xpt_announce_quirks(periph, softc->quirks,
3207 				    DA_Q_BIT_STRING);
3208 			} else {
3209 				xpt_print(periph->path, "fatal error, "
3210 				    "could not acquire reference count\n");
3211 			}
3212 		}
3213 
3214 		/* We already probed the device. */
3215 		if (softc->flags & DA_FLAG_PROBED) {
3216 			daprobedone(periph, done_ccb);
3217 			return;
3218 		}
3219 
3220 		/* Ensure re-probe doesn't see old delete. */
3221 		softc->delete_available = 0;
3222 		if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3223 			/*
3224 			 * Based on older SBC-3 spec revisions
3225 			 * any of the UNMAP methods "may" be
3226 			 * available via LBP given this flag so
3227 			 * we flag all of them as availble and
3228 			 * then remove those which further
3229 			 * probes confirm aren't available
3230 			 * later.
3231 			 *
3232 			 * We could also check readcap(16) p_type
3233 			 * flag to exclude one or more invalid
3234 			 * write same (X) types here
3235 			 */
3236 			dadeleteflag(softc, DA_DELETE_WS16, 1);
3237 			dadeleteflag(softc, DA_DELETE_WS10, 1);
3238 			dadeleteflag(softc, DA_DELETE_ZERO, 1);
3239 			dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3240 
3241 			xpt_release_ccb(done_ccb);
3242 			softc->state = DA_STATE_PROBE_LBP;
3243 			xpt_schedule(periph, priority);
3244 			return;
3245 		}
3246 
3247 		xpt_release_ccb(done_ccb);
3248 		softc->state = DA_STATE_PROBE_BDC;
3249 		xpt_schedule(periph, priority);
3250 		return;
3251 	}
3252 	case DA_CCB_PROBE_LBP:
3253 	{
3254 		struct scsi_vpd_logical_block_prov *lbp;
3255 
3256 		lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3257 
3258 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3259 			/*
3260 			 * T10/1799-D Revision 31 states at least one of these
3261 			 * must be supported but we don't currently enforce this.
3262 			 */
3263 			dadeleteflag(softc, DA_DELETE_WS16,
3264 				     (lbp->flags & SVPD_LBP_WS16));
3265 			dadeleteflag(softc, DA_DELETE_WS10,
3266 				     (lbp->flags & SVPD_LBP_WS10));
3267 			dadeleteflag(softc, DA_DELETE_ZERO,
3268 				     (lbp->flags & SVPD_LBP_WS10));
3269 			dadeleteflag(softc, DA_DELETE_UNMAP,
3270 				     (lbp->flags & SVPD_LBP_UNMAP));
3271 		} else {
3272 			int error;
3273 			error = daerror(done_ccb, CAM_RETRY_SELTO,
3274 					SF_RETRY_UA|SF_NO_PRINT);
3275 			if (error == ERESTART)
3276 				return;
3277 			else if (error != 0) {
3278 				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3279 					/* Don't wedge this device's queue */
3280 					cam_release_devq(done_ccb->ccb_h.path,
3281 							 /*relsim_flags*/0,
3282 							 /*reduction*/0,
3283 							 /*timeout*/0,
3284 							 /*getcount_only*/0);
3285 				}
3286 
3287 				/*
3288 				 * Failure indicates we don't support any SBC-3
3289 				 * delete methods with UNMAP
3290 				 */
3291 			}
3292 		}
3293 
3294 		free(lbp, M_SCSIDA);
3295 		xpt_release_ccb(done_ccb);
3296 		softc->state = DA_STATE_PROBE_BLK_LIMITS;
3297 		xpt_schedule(periph, priority);
3298 		return;
3299 	}
3300 	case DA_CCB_PROBE_BLK_LIMITS:
3301 	{
3302 		struct scsi_vpd_block_limits *block_limits;
3303 
3304 		block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3305 
3306 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3307 			uint32_t max_txfer_len = scsi_4btoul(
3308 				block_limits->max_txfer_len);
3309 			uint32_t max_unmap_lba_cnt = scsi_4btoul(
3310 				block_limits->max_unmap_lba_cnt);
3311 			uint32_t max_unmap_blk_cnt = scsi_4btoul(
3312 				block_limits->max_unmap_blk_cnt);
3313 			uint64_t ws_max_blks = scsi_8btou64(
3314 				block_limits->max_write_same_length);
3315 
3316 			if (max_txfer_len != 0) {
3317 				softc->disk->d_maxsize = MIN(softc->maxio,
3318 				    (off_t)max_txfer_len * softc->params.secsize);
3319 			}
3320 
3321 			/*
3322 			 * We should already support UNMAP but we check lba
3323 			 * and block count to be sure
3324 			 */
3325 			if (max_unmap_lba_cnt != 0x00L &&
3326 			    max_unmap_blk_cnt != 0x00L) {
3327 				softc->unmap_max_lba = max_unmap_lba_cnt;
3328 				softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3329 					UNMAP_MAX_RANGES);
3330 			} else {
3331 				/*
3332 				 * Unexpected UNMAP limits which means the
3333 				 * device doesn't actually support UNMAP
3334 				 */
3335 				dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3336 			}
3337 
3338 			if (ws_max_blks != 0x00L)
3339 				softc->ws_max_blks = ws_max_blks;
3340 		} else {
3341 			int error;
3342 			error = daerror(done_ccb, CAM_RETRY_SELTO,
3343 					SF_RETRY_UA|SF_NO_PRINT);
3344 			if (error == ERESTART)
3345 				return;
3346 			else if (error != 0) {
3347 				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3348 					/* Don't wedge this device's queue */
3349 					cam_release_devq(done_ccb->ccb_h.path,
3350 							 /*relsim_flags*/0,
3351 							 /*reduction*/0,
3352 							 /*timeout*/0,
3353 							 /*getcount_only*/0);
3354 				}
3355 
3356 				/*
3357 				 * Failure here doesn't mean UNMAP is not
3358 				 * supported as this is an optional page.
3359 				 */
3360 				softc->unmap_max_lba = 1;
3361 				softc->unmap_max_ranges = 1;
3362 			}
3363 		}
3364 
3365 		free(block_limits, M_SCSIDA);
3366 		xpt_release_ccb(done_ccb);
3367 		softc->state = DA_STATE_PROBE_BDC;
3368 		xpt_schedule(periph, priority);
3369 		return;
3370 	}
3371 	case DA_CCB_PROBE_BDC:
3372 	{
3373 		struct scsi_vpd_block_characteristics *bdc;
3374 
3375 		bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3376 
3377 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3378 			/*
3379 			 * Disable queue sorting for non-rotational media
3380 			 * by default.
3381 			 */
3382 			u_int16_t old_rate = softc->disk->d_rotation_rate;
3383 
3384 			softc->disk->d_rotation_rate =
3385 				scsi_2btoul(bdc->medium_rotation_rate);
3386 			if (softc->disk->d_rotation_rate ==
3387 			    SVPD_BDC_RATE_NON_ROTATING) {
3388 				softc->sort_io_queue = 0;
3389 			}
3390 			if (softc->disk->d_rotation_rate != old_rate) {
3391 				disk_attr_changed(softc->disk,
3392 				    "GEOM::rotation_rate", M_NOWAIT);
3393 			}
3394 		} else {
3395 			int error;
3396 			error = daerror(done_ccb, CAM_RETRY_SELTO,
3397 					SF_RETRY_UA|SF_NO_PRINT);
3398 			if (error == ERESTART)
3399 				return;
3400 			else if (error != 0) {
3401 				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3402 					/* Don't wedge this device's queue */
3403 					cam_release_devq(done_ccb->ccb_h.path,
3404 							 /*relsim_flags*/0,
3405 							 /*reduction*/0,
3406 							 /*timeout*/0,
3407 							 /*getcount_only*/0);
3408 				}
3409 			}
3410 		}
3411 
3412 		free(bdc, M_SCSIDA);
3413 		xpt_release_ccb(done_ccb);
3414 		softc->state = DA_STATE_PROBE_ATA;
3415 		xpt_schedule(periph, priority);
3416 		return;
3417 	}
3418 	case DA_CCB_PROBE_ATA:
3419 	{
3420 		int i;
3421 		struct ata_params *ata_params;
3422 		int16_t *ptr;
3423 
3424 		ata_params = (struct ata_params *)csio->data_ptr;
3425 		ptr = (uint16_t *)ata_params;
3426 
3427 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3428 			uint16_t old_rate;
3429 
3430 			for (i = 0; i < sizeof(*ata_params) / 2; i++)
3431 				ptr[i] = le16toh(ptr[i]);
3432 			if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3433 			    (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3434 				dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3435 				if (ata_params->max_dsm_blocks != 0)
3436 					softc->trim_max_ranges = min(
3437 					  softc->trim_max_ranges,
3438 					  ata_params->max_dsm_blocks *
3439 					  ATA_DSM_BLK_RANGES);
3440 			}
3441 			/*
3442 			 * Disable queue sorting for non-rotational media
3443 			 * by default.
3444 			 */
3445 			old_rate = softc->disk->d_rotation_rate;
3446 			softc->disk->d_rotation_rate =
3447 			    ata_params->media_rotation_rate;
3448 			if (softc->disk->d_rotation_rate ==
3449 			    ATA_RATE_NON_ROTATING) {
3450 				softc->sort_io_queue = 0;
3451 			}
3452 
3453 			if (softc->disk->d_rotation_rate != old_rate) {
3454 				disk_attr_changed(softc->disk,
3455 				    "GEOM::rotation_rate", M_NOWAIT);
3456 			}
3457 		} else {
3458 			int error;
3459 			error = daerror(done_ccb, CAM_RETRY_SELTO,
3460 					SF_RETRY_UA|SF_NO_PRINT);
3461 			if (error == ERESTART)
3462 				return;
3463 			else if (error != 0) {
3464 				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3465 					/* Don't wedge this device's queue */
3466 					cam_release_devq(done_ccb->ccb_h.path,
3467 							 /*relsim_flags*/0,
3468 							 /*reduction*/0,
3469 							 /*timeout*/0,
3470 							 /*getcount_only*/0);
3471 				}
3472 			}
3473 		}
3474 
3475 		free(ata_params, M_SCSIDA);
3476 		daprobedone(periph, done_ccb);
3477 		return;
3478 	}
3479 	case DA_CCB_DUMP:
3480 		/* No-op.  We're polling */
3481 		return;
3482 	case DA_CCB_TUR:
3483 	{
3484 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3485 
3486 			if (daerror(done_ccb, CAM_RETRY_SELTO,
3487 			    SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3488 			    ERESTART)
3489 				return;
3490 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3491 				cam_release_devq(done_ccb->ccb_h.path,
3492 						 /*relsim_flags*/0,
3493 						 /*reduction*/0,
3494 						 /*timeout*/0,
3495 						 /*getcount_only*/0);
3496 		}
3497 		xpt_release_ccb(done_ccb);
3498 		cam_periph_release_locked(periph);
3499 		return;
3500 	}
3501 	default:
3502 		break;
3503 	}
3504 	xpt_release_ccb(done_ccb);
3505 }
3506 
3507 static void
3508 dareprobe(struct cam_periph *periph)
3509 {
3510 	struct da_softc	  *softc;
3511 	cam_status status;
3512 
3513 	softc = (struct da_softc *)periph->softc;
3514 
3515 	/* Probe in progress; don't interfere. */
3516 	if (softc->state != DA_STATE_NORMAL)
3517 		return;
3518 
3519 	status = cam_periph_acquire(periph);
3520 	KASSERT(status == CAM_REQ_CMP,
3521 	    ("dareprobe: cam_periph_acquire failed"));
3522 
3523 	if (softc->flags & DA_FLAG_CAN_RC16)
3524 		softc->state = DA_STATE_PROBE_RC16;
3525 	else
3526 		softc->state = DA_STATE_PROBE_RC;
3527 
3528 	xpt_schedule(periph, CAM_PRIORITY_DEV);
3529 }
3530 
3531 static int
3532 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3533 {
3534 	struct da_softc	  *softc;
3535 	struct cam_periph *periph;
3536 	int error, error_code, sense_key, asc, ascq;
3537 
3538 	periph = xpt_path_periph(ccb->ccb_h.path);
3539 	softc = (struct da_softc *)periph->softc;
3540 
3541  	/*
3542 	 * Automatically detect devices that do not support
3543  	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3544  	 */
3545 	error = 0;
3546 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3547 		error = cmd6workaround(ccb);
3548 	} else if (scsi_extract_sense_ccb(ccb,
3549 	    &error_code, &sense_key, &asc, &ascq)) {
3550 		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3551  			error = cmd6workaround(ccb);
3552 		/*
3553 		 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3554 		 * query the capacity and notify upper layers.
3555 		 */
3556 		else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3557 		    asc == 0x2A && ascq == 0x09) {
3558 			xpt_print(periph->path, "Capacity data has changed\n");
3559 			softc->flags &= ~DA_FLAG_PROBED;
3560 			dareprobe(periph);
3561 			sense_flags |= SF_NO_PRINT;
3562 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3563 		    asc == 0x28 && ascq == 0x00) {
3564 			softc->flags &= ~DA_FLAG_PROBED;
3565 			disk_media_changed(softc->disk, M_NOWAIT);
3566 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3567 		    asc == 0x3F && ascq == 0x03) {
3568 			xpt_print(periph->path, "INQUIRY data has changed\n");
3569 			softc->flags &= ~DA_FLAG_PROBED;
3570 			dareprobe(periph);
3571 			sense_flags |= SF_NO_PRINT;
3572 		} else if (sense_key == SSD_KEY_NOT_READY &&
3573 		    asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3574 			softc->flags |= DA_FLAG_PACK_INVALID;
3575 			disk_media_gone(softc->disk, M_NOWAIT);
3576 		}
3577 	}
3578 	if (error == ERESTART)
3579 		return (ERESTART);
3580 
3581 	/*
3582 	 * XXX
3583 	 * Until we have a better way of doing pack validation,
3584 	 * don't treat UAs as errors.
3585 	 */
3586 	sense_flags |= SF_RETRY_UA;
3587 	return(cam_periph_error(ccb, cam_flags, sense_flags,
3588 				&softc->saved_ccb));
3589 }
3590 
3591 static void
3592 damediapoll(void *arg)
3593 {
3594 	struct cam_periph *periph = arg;
3595 	struct da_softc *softc = periph->softc;
3596 
3597 	if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3598 		if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3599 			softc->tur = 1;
3600 			daschedule(periph);
3601 		}
3602 	}
3603 	/* Queue us up again */
3604 	if (da_poll_period != 0)
3605 		callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3606 }
3607 
3608 static void
3609 daprevent(struct cam_periph *periph, int action)
3610 {
3611 	struct	da_softc *softc;
3612 	union	ccb *ccb;
3613 	int	error;
3614 
3615 	softc = (struct da_softc *)periph->softc;
3616 
3617 	if (((action == PR_ALLOW)
3618 	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3619 	 || ((action == PR_PREVENT)
3620 	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3621 		return;
3622 	}
3623 
3624 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3625 
3626 	scsi_prevent(&ccb->csio,
3627 		     /*retries*/1,
3628 		     /*cbcfp*/dadone,
3629 		     MSG_SIMPLE_Q_TAG,
3630 		     action,
3631 		     SSD_FULL_SIZE,
3632 		     5000);
3633 
3634 	error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3635 	    SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3636 
3637 	if (error == 0) {
3638 		if (action == PR_ALLOW)
3639 			softc->flags &= ~DA_FLAG_PACK_LOCKED;
3640 		else
3641 			softc->flags |= DA_FLAG_PACK_LOCKED;
3642 	}
3643 
3644 	xpt_release_ccb(ccb);
3645 }
3646 
3647 static void
3648 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3649 	  struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3650 {
3651 	struct ccb_calc_geometry ccg;
3652 	struct da_softc *softc;
3653 	struct disk_params *dp;
3654 	u_int lbppbe, lalba;
3655 	int error;
3656 
3657 	softc = (struct da_softc *)periph->softc;
3658 
3659 	dp = &softc->params;
3660 	dp->secsize = block_len;
3661 	dp->sectors = maxsector + 1;
3662 	if (rcaplong != NULL) {
3663 		lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3664 		lalba = scsi_2btoul(rcaplong->lalba_lbp);
3665 		lalba &= SRC16_LALBA_A;
3666 	} else {
3667 		lbppbe = 0;
3668 		lalba = 0;
3669 	}
3670 
3671 	if (lbppbe > 0) {
3672 		dp->stripesize = block_len << lbppbe;
3673 		dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3674 		    dp->stripesize;
3675 	} else if (softc->quirks & DA_Q_4K) {
3676 		dp->stripesize = 4096;
3677 		dp->stripeoffset = 0;
3678 	} else {
3679 		dp->stripesize = 0;
3680 		dp->stripeoffset = 0;
3681 	}
3682 	/*
3683 	 * Have the controller provide us with a geometry
3684 	 * for this disk.  The only time the geometry
3685 	 * matters is when we boot and the controller
3686 	 * is the only one knowledgeable enough to come
3687 	 * up with something that will make this a bootable
3688 	 * device.
3689 	 */
3690 	xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3691 	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3692 	ccg.block_size = dp->secsize;
3693 	ccg.volume_size = dp->sectors;
3694 	ccg.heads = 0;
3695 	ccg.secs_per_track = 0;
3696 	ccg.cylinders = 0;
3697 	xpt_action((union ccb*)&ccg);
3698 	if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3699 		/*
3700 		 * We don't know what went wrong here- but just pick
3701 		 * a geometry so we don't have nasty things like divide
3702 		 * by zero.
3703 		 */
3704 		dp->heads = 255;
3705 		dp->secs_per_track = 255;
3706 		dp->cylinders = dp->sectors / (255 * 255);
3707 		if (dp->cylinders == 0) {
3708 			dp->cylinders = 1;
3709 		}
3710 	} else {
3711 		dp->heads = ccg.heads;
3712 		dp->secs_per_track = ccg.secs_per_track;
3713 		dp->cylinders = ccg.cylinders;
3714 	}
3715 
3716 	/*
3717 	 * If the user supplied a read capacity buffer, and if it is
3718 	 * different than the previous buffer, update the data in the EDT.
3719 	 * If it's the same, we don't bother.  This avoids sending an
3720 	 * update every time someone opens this device.
3721 	 */
3722 	if ((rcaplong != NULL)
3723 	 && (bcmp(rcaplong, &softc->rcaplong,
3724 		  min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3725 		struct ccb_dev_advinfo cdai;
3726 
3727 		xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3728 		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3729 		cdai.buftype = CDAI_TYPE_RCAPLONG;
3730 		cdai.flags |= CDAI_FLAG_STORE;
3731 		cdai.bufsiz = rcap_len;
3732 		cdai.buf = (uint8_t *)rcaplong;
3733 		xpt_action((union ccb *)&cdai);
3734 		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3735 			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3736 		if (cdai.ccb_h.status != CAM_REQ_CMP) {
3737 			xpt_print(periph->path, "%s: failed to set read "
3738 				  "capacity advinfo\n", __func__);
3739 			/* Use cam_error_print() to decode the status */
3740 			cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3741 					CAM_EPF_ALL);
3742 		} else {
3743 			bcopy(rcaplong, &softc->rcaplong,
3744 			      min(sizeof(softc->rcaplong), rcap_len));
3745 		}
3746 	}
3747 
3748 	softc->disk->d_sectorsize = softc->params.secsize;
3749 	softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3750 	softc->disk->d_stripesize = softc->params.stripesize;
3751 	softc->disk->d_stripeoffset = softc->params.stripeoffset;
3752 	/* XXX: these are not actually "firmware" values, so they may be wrong */
3753 	softc->disk->d_fwsectors = softc->params.secs_per_track;
3754 	softc->disk->d_fwheads = softc->params.heads;
3755 	softc->disk->d_devstat->block_size = softc->params.secsize;
3756 	softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3757 
3758 	error = disk_resize(softc->disk, M_NOWAIT);
3759 	if (error != 0)
3760 		xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3761 }
3762 
3763 static void
3764 dasendorderedtag(void *arg)
3765 {
3766 	struct da_softc *softc = arg;
3767 
3768 	if (da_send_ordered) {
3769 		if (!LIST_EMPTY(&softc->pending_ccbs)) {
3770 			if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3771 				softc->flags |= DA_FLAG_NEED_OTAG;
3772 			softc->flags &= ~DA_FLAG_WAS_OTAG;
3773 		}
3774 	}
3775 	/* Queue us up again */
3776 	callout_reset(&softc->sendordered_c,
3777 	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3778 	    dasendorderedtag, softc);
3779 }
3780 
3781 /*
3782  * Step through all DA peripheral drivers, and if the device is still open,
3783  * sync the disk cache to physical media.
3784  */
3785 static void
3786 dashutdown(void * arg, int howto)
3787 {
3788 	struct cam_periph *periph;
3789 	struct da_softc *softc;
3790 	union ccb *ccb;
3791 	int error;
3792 
3793 	CAM_PERIPH_FOREACH(periph, &dadriver) {
3794 		softc = (struct da_softc *)periph->softc;
3795 		if (SCHEDULER_STOPPED()) {
3796 			/* If we paniced with the lock held, do not recurse. */
3797 			if (!cam_periph_owned(periph) &&
3798 			    (softc->flags & DA_FLAG_OPEN)) {
3799 				dadump(softc->disk, NULL, 0, 0, 0);
3800 			}
3801 			continue;
3802 		}
3803 		cam_periph_lock(periph);
3804 
3805 		/*
3806 		 * We only sync the cache if the drive is still open, and
3807 		 * if the drive is capable of it..
3808 		 */
3809 		if (((softc->flags & DA_FLAG_OPEN) == 0)
3810 		 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3811 			cam_periph_unlock(periph);
3812 			continue;
3813 		}
3814 
3815 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3816 		scsi_synchronize_cache(&ccb->csio,
3817 				       /*retries*/0,
3818 				       /*cbfcnp*/dadone,
3819 				       MSG_SIMPLE_Q_TAG,
3820 				       /*begin_lba*/0, /* whole disk */
3821 				       /*lb_count*/0,
3822 				       SSD_FULL_SIZE,
3823 				       60 * 60 * 1000);
3824 
3825 		error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3826 		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3827 		    softc->disk->d_devstat);
3828 		if (error != 0)
3829 			xpt_print(periph->path, "Synchronize cache failed\n");
3830 		xpt_release_ccb(ccb);
3831 		cam_periph_unlock(periph);
3832 	}
3833 }
3834 
3835 #else /* !_KERNEL */
3836 
3837 /*
3838  * XXX This is only left out of the kernel build to silence warnings.  If,
3839  * for some reason this function is used in the kernel, the ifdefs should
3840  * be moved so it is included both in the kernel and userland.
3841  */
3842 void
3843 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3844 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
3845 		 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3846 		 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3847 		 u_int32_t timeout)
3848 {
3849 	struct scsi_format_unit *scsi_cmd;
3850 
3851 	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3852 	scsi_cmd->opcode = FORMAT_UNIT;
3853 	scsi_cmd->byte2 = byte2;
3854 	scsi_ulto2b(ileave, scsi_cmd->interleave);
3855 
3856 	cam_fill_csio(csio,
3857 		      retries,
3858 		      cbfcnp,
3859 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3860 		      tag_action,
3861 		      data_ptr,
3862 		      dxfer_len,
3863 		      sense_len,
3864 		      sizeof(*scsi_cmd),
3865 		      timeout);
3866 }
3867 
3868 void
3869 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
3870 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
3871 	      u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
3872 	      u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3873 	      u_int32_t timeout)
3874 {
3875 	struct scsi_sanitize *scsi_cmd;
3876 
3877 	scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
3878 	scsi_cmd->opcode = SANITIZE;
3879 	scsi_cmd->byte2 = byte2;
3880 	scsi_cmd->control = control;
3881 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
3882 
3883 	cam_fill_csio(csio,
3884 		      retries,
3885 		      cbfcnp,
3886 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3887 		      tag_action,
3888 		      data_ptr,
3889 		      dxfer_len,
3890 		      sense_len,
3891 		      sizeof(*scsi_cmd),
3892 		      timeout);
3893 }
3894 
3895 #endif /* _KERNEL */
3896