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