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