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