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