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