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