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