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