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