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