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