xref: /freebsd/sys/cam/scsi/scsi_da.c (revision 8ec81e392654c03ebba7792e47bf3744d623cf54)
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 <geom/geom.h>
48 #include <geom/geom_disk.h>
49 #endif /* _KERNEL */
50 
51 #ifndef _KERNEL
52 #include <stdio.h>
53 #include <string.h>
54 #endif /* _KERNEL */
55 
56 #include <cam/cam.h>
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_sim.h>
61 
62 #include <cam/scsi/scsi_message.h>
63 
64 #ifndef _KERNEL
65 #include <cam/scsi/scsi_da.h>
66 #endif /* !_KERNEL */
67 
68 #ifdef _KERNEL
69 typedef enum {
70 	DA_STATE_PROBE,
71 	DA_STATE_PROBE2,
72 	DA_STATE_NORMAL
73 } da_state;
74 
75 typedef enum {
76 	DA_FLAG_PACK_INVALID	= 0x001,
77 	DA_FLAG_NEW_PACK	= 0x002,
78 	DA_FLAG_PACK_LOCKED	= 0x004,
79 	DA_FLAG_PACK_REMOVABLE	= 0x008,
80 	DA_FLAG_SAW_MEDIA	= 0x010,
81 	DA_FLAG_NEED_OTAG	= 0x020,
82 	DA_FLAG_WENT_IDLE	= 0x040,
83 	DA_FLAG_RETRY_UA	= 0x080,
84 	DA_FLAG_OPEN		= 0x100,
85 	DA_FLAG_SCTX_INIT	= 0x200,
86 	DA_FLAG_CAN_RC16	= 0x400,
87 	DA_FLAG_PROBED		= 0x800
88 } da_flags;
89 
90 typedef enum {
91 	DA_Q_NONE		= 0x00,
92 	DA_Q_NO_SYNC_CACHE	= 0x01,
93 	DA_Q_NO_6_BYTE		= 0x02,
94 	DA_Q_NO_PREVENT		= 0x04,
95 	DA_Q_4K			= 0x08
96 } da_quirks;
97 
98 typedef enum {
99 	DA_CCB_PROBE		= 0x01,
100 	DA_CCB_PROBE2		= 0x02,
101 	DA_CCB_BUFFER_IO	= 0x03,
102 	DA_CCB_WAITING		= 0x04,
103 	DA_CCB_DUMP		= 0x05,
104 	DA_CCB_DELETE		= 0x06,
105 	DA_CCB_TUR		= 0x07,
106 	DA_CCB_TYPE_MASK	= 0x0F,
107 	DA_CCB_RETRY_UA		= 0x10
108 } da_ccb_state;
109 
110 typedef enum {
111 	DA_DELETE_NONE,
112 	DA_DELETE_DISABLE,
113 	DA_DELETE_ZERO,
114 	DA_DELETE_WS10,
115 	DA_DELETE_WS16,
116 	DA_DELETE_UNMAP,
117 	DA_DELETE_MAX = DA_DELETE_UNMAP
118 } da_delete_methods;
119 
120 static const char *da_delete_method_names[] =
121     { "NONE", "DISABLE", "ZERO", "WS10", "WS16", "UNMAP" };
122 
123 /* Offsets into our private area for storing information */
124 #define ccb_state	ppriv_field0
125 #define ccb_bp		ppriv_ptr1
126 
127 struct disk_params {
128 	u_int8_t  heads;
129 	u_int32_t cylinders;
130 	u_int8_t  secs_per_track;
131 	u_int32_t secsize;	/* Number of bytes/sector */
132 	u_int64_t sectors;	/* total number sectors */
133 	u_int     stripesize;
134 	u_int     stripeoffset;
135 };
136 
137 #define UNMAP_MAX_RANGES	512
138 
139 struct da_softc {
140 	struct	 bio_queue_head bio_queue;
141 	struct	 bio_queue_head delete_queue;
142 	struct	 bio_queue_head delete_run_queue;
143 	SLIST_ENTRY(da_softc) links;
144 	LIST_HEAD(, ccb_hdr) pending_ccbs;
145 	da_state state;
146 	da_flags flags;
147 	da_quirks quirks;
148 	int	 minimum_cmd_size;
149 	int	 error_inject;
150 	int	 ordered_tag_count;
151 	int	 outstanding_cmds;
152 	int	 unmap_max_ranges;
153 	int	 unmap_max_lba;
154 	int	 delete_running;
155 	int	 tur;
156 	da_delete_methods	 delete_method;
157 	struct	 disk_params params;
158 	struct	 disk *disk;
159 	union	 ccb saved_ccb;
160 	struct task		sysctl_task;
161 	struct sysctl_ctx_list	sysctl_ctx;
162 	struct sysctl_oid	*sysctl_tree;
163 	struct callout		sendordered_c;
164 	uint64_t wwpn;
165 	uint8_t	 unmap_buf[UNMAP_MAX_RANGES * 16 + 8];
166 	struct scsi_read_capacity_data_long rcaplong;
167 	struct callout		mediapoll_c;
168 };
169 
170 struct da_quirk_entry {
171 	struct scsi_inquiry_pattern inq_pat;
172 	da_quirks quirks;
173 };
174 
175 static const char quantum[] = "QUANTUM";
176 static const char microp[] = "MICROP";
177 
178 static struct da_quirk_entry da_quirk_table[] =
179 {
180 	/* SPI, FC devices */
181 	{
182 		/*
183 		 * Fujitsu M2513A MO drives.
184 		 * Tested devices: M2513A2 firmware versions 1200 & 1300.
185 		 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
186 		 * Reported by: W.Scholten <whs@xs4all.nl>
187 		 */
188 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
189 		/*quirks*/ DA_Q_NO_SYNC_CACHE
190 	},
191 	{
192 		/* See above. */
193 		{T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
194 		/*quirks*/ DA_Q_NO_SYNC_CACHE
195 	},
196 	{
197 		/*
198 		 * This particular Fujitsu drive doesn't like the
199 		 * synchronize cache command.
200 		 * Reported by: Tom Jackson <toj@gorilla.net>
201 		 */
202 		{T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
203 		/*quirks*/ DA_Q_NO_SYNC_CACHE
204 	},
205 	{
206 		/*
207 		 * This drive doesn't like the synchronize cache command
208 		 * either.  Reported by: Matthew Jacob <mjacob@feral.com>
209 		 * in NetBSD PR kern/6027, August 24, 1998.
210 		 */
211 		{T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
212 		/*quirks*/ DA_Q_NO_SYNC_CACHE
213 	},
214 	{
215 		/*
216 		 * This drive doesn't like the synchronize cache command
217 		 * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
218 		 * (PR 8882).
219 		 */
220 		{T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
221 		/*quirks*/ DA_Q_NO_SYNC_CACHE
222 	},
223 	{
224 		/*
225 		 * Doesn't like the synchronize cache command.
226 		 * Reported by: Blaz Zupan <blaz@gold.amis.net>
227 		 */
228 		{T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
229 		/*quirks*/ DA_Q_NO_SYNC_CACHE
230 	},
231 	{
232 		/*
233 		 * Doesn't like the synchronize cache command.
234 		 * Reported by: Blaz Zupan <blaz@gold.amis.net>
235 		 */
236 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
237 		/*quirks*/ DA_Q_NO_SYNC_CACHE
238 	},
239 	{
240 		/*
241 		 * Doesn't like the synchronize cache command.
242 		 */
243 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
244 		/*quirks*/ DA_Q_NO_SYNC_CACHE
245 	},
246 	{
247 		/*
248 		 * Doesn't like the synchronize cache command.
249 		 * Reported by: walter@pelissero.de
250 		 */
251 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
252 		/*quirks*/ DA_Q_NO_SYNC_CACHE
253 	},
254 	{
255 		/*
256 		 * Doesn't work correctly with 6 byte reads/writes.
257 		 * Returns illegal request, and points to byte 9 of the
258 		 * 6-byte CDB.
259 		 * Reported by:  Adam McDougall <bsdx@spawnet.com>
260 		 */
261 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
262 		/*quirks*/ DA_Q_NO_6_BYTE
263 	},
264 	{
265 		/* See above. */
266 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
267 		/*quirks*/ DA_Q_NO_6_BYTE
268 	},
269 	{
270 		/*
271 		 * Doesn't like the synchronize cache command.
272 		 * Reported by: walter@pelissero.de
273 		 */
274 		{T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
275 		/*quirks*/ DA_Q_NO_SYNC_CACHE
276 	},
277 	{
278 		/*
279 		 * The CISS RAID controllers do not support SYNC_CACHE
280 		 */
281 		{T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
282 		/*quirks*/ DA_Q_NO_SYNC_CACHE
283 	},
284 	/* USB mass storage devices supported by umass(4) */
285 	{
286 		/*
287 		 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
288 		 * PR: kern/51675
289 		 */
290 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
291 		/*quirks*/ DA_Q_NO_SYNC_CACHE
292 	},
293 	{
294 		/*
295 		 * Power Quotient Int. (PQI) USB flash key
296 		 * PR: kern/53067
297 		 */
298 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
299 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
300 	},
301  	{
302  		/*
303  		 * Creative Nomad MUVO mp3 player (USB)
304  		 * PR: kern/53094
305  		 */
306  		{T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
307  		/*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
308  	},
309 	{
310 		/*
311 		 * Jungsoft NEXDISK USB flash key
312 		 * PR: kern/54737
313 		 */
314 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
315 		/*quirks*/ DA_Q_NO_SYNC_CACHE
316 	},
317 	{
318 		/*
319 		 * FreeDik USB Mini Data Drive
320 		 * PR: kern/54786
321 		 */
322 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
323 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
324 	},
325 	{
326 		/*
327 		 * Sigmatel USB Flash MP3 Player
328 		 * PR: kern/57046
329 		 */
330 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
331 		/*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
332 	},
333 	{
334 		/*
335 		 * Neuros USB Digital Audio Computer
336 		 * PR: kern/63645
337 		 */
338 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
339 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
340 	},
341 	{
342 		/*
343 		 * SEAGRAND NP-900 MP3 Player
344 		 * PR: kern/64563
345 		 */
346 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
347 		/*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
348 	},
349 	{
350 		/*
351 		 * iRiver iFP MP3 player (with UMS Firmware)
352 		 * PR: kern/54881, i386/63941, kern/66124
353 		 */
354 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
355 		/*quirks*/ DA_Q_NO_SYNC_CACHE
356  	},
357 	{
358 		/*
359 		 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
360 		 * PR: kern/70158
361 		 */
362 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
363 		/*quirks*/ DA_Q_NO_SYNC_CACHE
364 	},
365 	{
366 		/*
367 		 * ZICPlay USB MP3 Player with FM
368 		 * PR: kern/75057
369 		 */
370 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
371 		/*quirks*/ DA_Q_NO_SYNC_CACHE
372 	},
373 	{
374 		/*
375 		 * TEAC USB floppy mechanisms
376 		 */
377 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
378 		/*quirks*/ DA_Q_NO_SYNC_CACHE
379 	},
380 	{
381 		/*
382 		 * Kingston DataTraveler II+ USB Pen-Drive.
383 		 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
384 		 */
385 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
386 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
387 	},
388 	{
389 		/*
390 		 * USB DISK Pro PMAP
391 		 * Reported by: jhs
392 		 * PR: usb/96381
393 		 */
394 		{T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
395 		/*quirks*/ DA_Q_NO_SYNC_CACHE
396 	},
397 	{
398 		/*
399 		 * Motorola E398 Mobile Phone (TransFlash memory card).
400 		 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
401 		 * PR: usb/89889
402 		 */
403 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
404 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
405 	},
406 	{
407 		/*
408 		 * Qware BeatZkey! Pro
409 		 * PR: usb/79164
410 		 */
411 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
412 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
413 	},
414 	{
415 		/*
416 		 * Time DPA20B 1GB MP3 Player
417 		 * PR: usb/81846
418 		 */
419 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
420 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
421 	},
422 	{
423 		/*
424 		 * Samsung USB key 128Mb
425 		 * PR: usb/90081
426 		 */
427 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
428 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
429 	},
430 	{
431 		/*
432 		 * Kingston DataTraveler 2.0 USB Flash memory.
433 		 * PR: usb/89196
434 		 */
435 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
436 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
437 	},
438 	{
439 		/*
440 		 * Creative MUVO Slim mp3 player (USB)
441 		 * PR: usb/86131
442 		 */
443 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
444 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
445 		},
446 	{
447 		/*
448 		 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
449 		 * PR: usb/80487
450 		 */
451 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
452 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
453 	},
454 	{
455 		/*
456 		 * SanDisk Micro Cruzer 128MB
457 		 * PR: usb/75970
458 		 */
459 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
460 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
461 	},
462 	{
463 		/*
464 		 * TOSHIBA TransMemory USB sticks
465 		 * PR: kern/94660
466 		 */
467 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
468 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
469 	},
470 	{
471 		/*
472 		 * PNY USB Flash keys
473 		 * PR: usb/75578, usb/72344, usb/65436
474 		 */
475 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
476 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
477 	},
478 	{
479 		/*
480 		 * Genesys 6-in-1 Card Reader
481 		 * PR: usb/94647
482 		 */
483 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
484 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
485 	},
486 	{
487 		/*
488 		 * Rekam Digital CAMERA
489 		 * PR: usb/98713
490 		 */
491 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
492 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
493 	},
494 	{
495 		/*
496 		 * iRiver H10 MP3 player
497 		 * PR: usb/102547
498 		 */
499 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
500 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
501 	},
502 	{
503 		/*
504 		 * iRiver U10 MP3 player
505 		 * PR: usb/92306
506 		 */
507 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
508 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
509 	},
510 	{
511 		/*
512 		 * X-Micro Flash Disk
513 		 * PR: usb/96901
514 		 */
515 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
516 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
517 	},
518 	{
519 		/*
520 		 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
521 		 * PR: usb/96546
522 		 */
523 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
524 		"1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
525 	},
526 	{
527 		/*
528 		 * Denver MP3 player
529 		 * PR: usb/107101
530 		 */
531 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
532 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
533 	},
534 	{
535 		/*
536 		 * Philips USB Key Audio KEY013
537 		 * PR: usb/68412
538 		 */
539 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
540 		/*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
541 	},
542 	{
543 		/*
544 		 * JNC MP3 Player
545 		 * PR: usb/94439
546 		 */
547 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
548 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
549 	},
550 	{
551 		/*
552 		 * SAMSUNG MP0402H
553 		 * PR: usb/108427
554 		 */
555 		{T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
556 		/*quirks*/ DA_Q_NO_SYNC_CACHE
557 	},
558 	{
559 		/*
560 		 * I/O Magic USB flash - Giga Bank
561 		 * PR: usb/108810
562 		 */
563 		{T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
564 		/*quirks*/ DA_Q_NO_SYNC_CACHE
565 	},
566 	{
567 		/*
568 		 * JoyFly 128mb USB Flash Drive
569 		 * PR: 96133
570 		 */
571 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
572 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
573 	},
574 	{
575 		/*
576 		 * ChipsBnk usb stick
577 		 * PR: 103702
578 		 */
579 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
580 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
581 	},
582 	{
583 		/*
584 		 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
585 		 * PR: 129858
586 		 */
587 		{T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
588 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
589 	},
590 	{
591 		/*
592 		 * Samsung YP-U3 mp3-player
593 		 * PR: 125398
594 		 */
595 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
596 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
597 	},
598 	{
599 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
600 		 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
601 	},
602 	{
603 		/*
604 		 * Sony Cyber-Shot DSC cameras
605 		 * PR: usb/137035
606 		 */
607 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
608 		/*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
609 	},
610 	/* ATA/SATA devices over SAS/USB/... */
611 	{
612 		/* Hitachi Advanced Format (4k) drives */
613 		{ T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
614 		/*quirks*/DA_Q_4K
615 	},
616 	{
617 		/* Samsung Advanced Format (4k) drives */
618 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
619 		/*quirks*/DA_Q_4K
620 	},
621 	{
622 		/* Samsung Advanced Format (4k) drives */
623 		{ T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
624 		/*quirks*/DA_Q_4K
625 	},
626 	{
627 		/* Samsung Advanced Format (4k) drives */
628 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
629 		/*quirks*/DA_Q_4K
630 	},
631 	{
632 		/* Samsung Advanced Format (4k) drives */
633 		{ T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
634 		/*quirks*/DA_Q_4K
635 	},
636 	{
637 		/* Seagate Barracuda Green Advanced Format (4k) drives */
638 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
639 		/*quirks*/DA_Q_4K
640 	},
641 	{
642 		/* Seagate Barracuda Green Advanced Format (4k) drives */
643 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
644 		/*quirks*/DA_Q_4K
645 	},
646 	{
647 		/* Seagate Barracuda Green Advanced Format (4k) drives */
648 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
649 		/*quirks*/DA_Q_4K
650 	},
651 	{
652 		/* Seagate Barracuda Green Advanced Format (4k) drives */
653 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
654 		/*quirks*/DA_Q_4K
655 	},
656 	{
657 		/* Seagate Barracuda Green Advanced Format (4k) drives */
658 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
659 		/*quirks*/DA_Q_4K
660 	},
661 	{
662 		/* Seagate Barracuda Green Advanced Format (4k) drives */
663 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
664 		/*quirks*/DA_Q_4K
665 	},
666 	{
667 		/* Seagate Momentus Advanced Format (4k) drives */
668 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
669 		/*quirks*/DA_Q_4K
670 	},
671 	{
672 		/* Seagate Momentus Advanced Format (4k) drives */
673 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
674 		/*quirks*/DA_Q_4K
675 	},
676 	{
677 		/* Seagate Momentus Advanced Format (4k) drives */
678 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
679 		/*quirks*/DA_Q_4K
680 	},
681 	{
682 		/* Seagate Momentus Advanced Format (4k) drives */
683 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
684 		/*quirks*/DA_Q_4K
685 	},
686 	{
687 		/* Seagate Momentus Advanced Format (4k) drives */
688 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
689 		/*quirks*/DA_Q_4K
690 	},
691 	{
692 		/* Seagate Momentus Advanced Format (4k) drives */
693 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
694 		/*quirks*/DA_Q_4K
695 	},
696 	{
697 		/* Seagate Momentus Advanced Format (4k) drives */
698 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
699 		/*quirks*/DA_Q_4K
700 	},
701 	{
702 		/* Seagate Momentus Advanced Format (4k) drives */
703 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
704 		/*quirks*/DA_Q_4K
705 	},
706 	{
707 		/* Seagate Momentus Advanced Format (4k) drives */
708 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
709 		/*quirks*/DA_Q_4K
710 	},
711 	{
712 		/* Seagate Momentus Advanced Format (4k) drives */
713 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
714 		/*quirks*/DA_Q_4K
715 	},
716 	{
717 		/* Seagate Momentus Advanced Format (4k) drives */
718 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
719 		/*quirks*/DA_Q_4K
720 	},
721 	{
722 		/* Seagate Momentus Advanced Format (4k) drives */
723 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
724 		/*quirks*/DA_Q_4K
725 	},
726 	{
727 		/* Seagate Momentus Advanced Format (4k) drives */
728 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
729 		/*quirks*/DA_Q_4K
730 	},
731 	{
732 		/* Seagate Momentus Advanced Format (4k) drives */
733 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
734 		/*quirks*/DA_Q_4K
735 	},
736 	{
737 		/* Seagate Momentus Thin Advanced Format (4k) drives */
738 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
739 		/*quirks*/DA_Q_4K
740 	},
741 	{
742 		/* Seagate Momentus Thin Advanced Format (4k) drives */
743 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
744 		/*quirks*/DA_Q_4K
745 	},
746 	{
747 		/* WDC Caviar Green Advanced Format (4k) drives */
748 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
749 		/*quirks*/DA_Q_4K
750 	},
751 	{
752 		/* WDC Caviar Green Advanced Format (4k) drives */
753 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
754 		/*quirks*/DA_Q_4K
755 	},
756 	{
757 		/* WDC Caviar Green Advanced Format (4k) drives */
758 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
759 		/*quirks*/DA_Q_4K
760 	},
761 	{
762 		/* WDC Caviar Green Advanced Format (4k) drives */
763 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
764 		/*quirks*/DA_Q_4K
765 	},
766 	{
767 		/* WDC Caviar Green Advanced Format (4k) drives */
768 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
769 		/*quirks*/DA_Q_4K
770 	},
771 	{
772 		/* WDC Caviar Green Advanced Format (4k) drives */
773 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
774 		/*quirks*/DA_Q_4K
775 	},
776 	{
777 		/* WDC Caviar Green Advanced Format (4k) drives */
778 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
779 		/*quirks*/DA_Q_4K
780 	},
781 	{
782 		/* WDC Caviar Green Advanced Format (4k) drives */
783 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
784 		/*quirks*/DA_Q_4K
785 	},
786 	{
787 		/* WDC Scorpio Black Advanced Format (4k) drives */
788 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
789 		/*quirks*/DA_Q_4K
790 	},
791 	{
792 		/* WDC Scorpio Black Advanced Format (4k) drives */
793 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
794 		/*quirks*/DA_Q_4K
795 	},
796 	{
797 		/* WDC Scorpio Black Advanced Format (4k) drives */
798 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
799 		/*quirks*/DA_Q_4K
800 	},
801 	{
802 		/* WDC Scorpio Black Advanced Format (4k) drives */
803 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
804 		/*quirks*/DA_Q_4K
805 	},
806 	{
807 		/* WDC Scorpio Blue Advanced Format (4k) drives */
808 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
809 		/*quirks*/DA_Q_4K
810 	},
811 	{
812 		/* WDC Scorpio Blue Advanced Format (4k) drives */
813 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
814 		/*quirks*/DA_Q_4K
815 	},
816 	{
817 		/* WDC Scorpio Blue Advanced Format (4k) drives */
818 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
819 		/*quirks*/DA_Q_4K
820 	},
821 	{
822 		/* WDC Scorpio Blue Advanced Format (4k) drives */
823 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
824 		/*quirks*/DA_Q_4K
825 	},
826 	{
827 		/*
828 		 * Olympus FE-210 camera
829 		 */
830 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
831 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
832 	},
833 	{
834 		/*
835 		 * LG UP3S MP3 player
836 		 */
837 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
838 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
839 	},
840 	{
841 		/*
842 		 * Laser MP3-2GA13 MP3 player
843 		 */
844 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
845 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
846 	},
847 	{
848 		/*
849 		 * LaCie external 250GB Hard drive des by Porsche
850 		 * Submitted by: Ben Stuyts <ben@altesco.nl>
851 		 * PR: 121474
852 		 */
853 		{T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
854 		/*quirks*/ DA_Q_NO_SYNC_CACHE
855 	},
856 };
857 
858 static	disk_strategy_t	dastrategy;
859 static	dumper_t	dadump;
860 static	periph_init_t	dainit;
861 static	void		daasync(void *callback_arg, u_int32_t code,
862 				struct cam_path *path, void *arg);
863 static	void		dasysctlinit(void *context, int pending);
864 static	int		dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
865 static	int		dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
866 static	int		dadeletemethodset(struct da_softc *softc,
867 					  da_delete_methods delete_method);
868 static	periph_ctor_t	daregister;
869 static	periph_dtor_t	dacleanup;
870 static	periph_start_t	dastart;
871 static	periph_oninv_t	daoninvalidate;
872 static	void		dadone(struct cam_periph *periph,
873 			       union ccb *done_ccb);
874 static  int		daerror(union ccb *ccb, u_int32_t cam_flags,
875 				u_int32_t sense_flags);
876 static void		daprevent(struct cam_periph *periph, int action);
877 static void		dareprobe(struct cam_periph *periph);
878 static void		dasetgeom(struct cam_periph *periph, uint32_t block_len,
879 				  uint64_t maxsector,
880 				  struct scsi_read_capacity_data_long *rcaplong,
881 				  size_t rcap_size);
882 static timeout_t	dasendorderedtag;
883 static void		dashutdown(void *arg, int howto);
884 static timeout_t	damediapoll;
885 
886 #ifndef	DA_DEFAULT_POLL_PERIOD
887 #define	DA_DEFAULT_POLL_PERIOD	3
888 #endif
889 
890 #ifndef DA_DEFAULT_TIMEOUT
891 #define DA_DEFAULT_TIMEOUT 60	/* Timeout in seconds */
892 #endif
893 
894 #ifndef	DA_DEFAULT_RETRY
895 #define	DA_DEFAULT_RETRY	4
896 #endif
897 
898 #ifndef	DA_DEFAULT_SEND_ORDERED
899 #define	DA_DEFAULT_SEND_ORDERED	1
900 #endif
901 
902 
903 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
904 static int da_retry_count = DA_DEFAULT_RETRY;
905 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
906 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
907 
908 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
909             "CAM Direct Access Disk driver");
910 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
911            &da_poll_period, 0, "Media polling period in seconds");
912 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
913 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
914            &da_retry_count, 0, "Normal I/O retry count");
915 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
916 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
917            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
918 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
919 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
920            &da_send_ordered, 0, "Send Ordered Tags");
921 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
922 
923 /*
924  * DA_ORDEREDTAG_INTERVAL determines how often, relative
925  * to the default timeout, we check to see whether an ordered
926  * tagged transaction is appropriate to prevent simple tag
927  * starvation.  Since we'd like to ensure that there is at least
928  * 1/2 of the timeout length left for a starved transaction to
929  * complete after we've sent an ordered tag, we must poll at least
930  * four times in every timeout period.  This takes care of the worst
931  * case where a starved transaction starts during an interval that
932  * meets the requirement "don't send an ordered tag" test so it takes
933  * us two intervals to determine that a tag must be sent.
934  */
935 #ifndef DA_ORDEREDTAG_INTERVAL
936 #define DA_ORDEREDTAG_INTERVAL 4
937 #endif
938 
939 static struct periph_driver dadriver =
940 {
941 	dainit, "da",
942 	TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
943 };
944 
945 PERIPHDRIVER_DECLARE(da, dadriver);
946 
947 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
948 
949 static int
950 daopen(struct disk *dp)
951 {
952 	struct cam_periph *periph;
953 	struct da_softc *softc;
954 	int unit;
955 	int error;
956 
957 	periph = (struct cam_periph *)dp->d_drv1;
958 	if (periph == NULL) {
959 		return (ENXIO);
960 	}
961 
962 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
963 		return (ENXIO);
964 	}
965 
966 	cam_periph_lock(periph);
967 	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
968 		cam_periph_unlock(periph);
969 		cam_periph_release(periph);
970 		return (error);
971 	}
972 
973 	unit = periph->unit_number;
974 	softc = (struct da_softc *)periph->softc;
975 	softc->flags |= DA_FLAG_OPEN;
976 
977 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
978 	    ("daopen\n"));
979 
980 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
981 		/* Invalidate our pack information. */
982 		softc->flags &= ~DA_FLAG_PACK_INVALID;
983 	}
984 
985 	dareprobe(periph);
986 
987 	/* Wait for the disk size update.  */
988 	error = msleep(&softc->disk->d_mediasize, periph->sim->mtx, PRIBIO,
989 	    "dareprobe", 0);
990 	if (error != 0)
991 		xpt_print(periph->path, "unable to retrieve capacity data");
992 
993 	if (periph->flags & CAM_PERIPH_INVALID ||
994 	    softc->disk->d_sectorsize == 0 ||
995 	    softc->disk->d_mediasize == 0)
996 		error = ENXIO;
997 
998 	if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
999 	    (softc->quirks & DA_Q_NO_PREVENT) == 0)
1000 		daprevent(periph, PR_PREVENT);
1001 
1002 	if (error == 0)
1003 		softc->flags |= DA_FLAG_SAW_MEDIA;
1004 
1005 	cam_periph_unhold(periph);
1006 	cam_periph_unlock(periph);
1007 
1008 	if (error != 0) {
1009 		softc->flags &= ~DA_FLAG_OPEN;
1010 		cam_periph_release(periph);
1011 	}
1012 
1013 	return (error);
1014 }
1015 
1016 static int
1017 daclose(struct disk *dp)
1018 {
1019 	struct	cam_periph *periph;
1020 	struct	da_softc *softc;
1021 
1022 	periph = (struct cam_periph *)dp->d_drv1;
1023 	if (periph == NULL)
1024 		return (0);
1025 
1026 	cam_periph_lock(periph);
1027 	if (cam_periph_hold(periph, PRIBIO) != 0) {
1028 		cam_periph_unlock(periph);
1029 		cam_periph_release(periph);
1030 		return (0);
1031 	}
1032 
1033 	softc = (struct da_softc *)periph->softc;
1034 
1035 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1036 	    ("daclose\n"));
1037 
1038 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
1039 	 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1040 		union	ccb *ccb;
1041 
1042 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1043 
1044 		scsi_synchronize_cache(&ccb->csio,
1045 				       /*retries*/1,
1046 				       /*cbfcnp*/dadone,
1047 				       MSG_SIMPLE_Q_TAG,
1048 				       /*begin_lba*/0,/* Cover the whole disk */
1049 				       /*lb_count*/0,
1050 				       SSD_FULL_SIZE,
1051 				       5 * 60 * 1000);
1052 
1053 		cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1054 				  /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1055 				  softc->disk->d_devstat);
1056 		xpt_release_ccb(ccb);
1057 
1058 	}
1059 
1060 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1061 		if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1062 			daprevent(periph, PR_ALLOW);
1063 		/*
1064 		 * If we've got removeable media, mark the blocksize as
1065 		 * unavailable, since it could change when new media is
1066 		 * inserted.
1067 		 */
1068 		softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1069 	}
1070 
1071 	softc->flags &= ~DA_FLAG_OPEN;
1072 	cam_periph_unhold(periph);
1073 	cam_periph_unlock(periph);
1074 	cam_periph_release(periph);
1075 	return (0);
1076 }
1077 
1078 static void
1079 daschedule(struct cam_periph *periph)
1080 {
1081 	struct da_softc *softc = (struct da_softc *)periph->softc;
1082 	uint32_t prio;
1083 
1084 	if (softc->state != DA_STATE_NORMAL)
1085 		return;
1086 
1087 	/* Check if cam_periph_getccb() was called. */
1088 	prio = periph->immediate_priority;
1089 
1090 	/* Check if we have more work to do. */
1091 	if (bioq_first(&softc->bio_queue) ||
1092 	    (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1093 	    softc->tur) {
1094 		prio = CAM_PRIORITY_NORMAL;
1095 	}
1096 
1097 	/* Schedule CCB if any of above is true. */
1098 	if (prio != CAM_PRIORITY_NONE)
1099 		xpt_schedule(periph, prio);
1100 }
1101 
1102 /*
1103  * Actually translate the requested transfer into one the physical driver
1104  * can understand.  The transfer is described by a buf and will include
1105  * only one physical transfer.
1106  */
1107 static void
1108 dastrategy(struct bio *bp)
1109 {
1110 	struct cam_periph *periph;
1111 	struct da_softc *softc;
1112 
1113 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1114 	if (periph == NULL) {
1115 		biofinish(bp, NULL, ENXIO);
1116 		return;
1117 	}
1118 	softc = (struct da_softc *)periph->softc;
1119 
1120 	cam_periph_lock(periph);
1121 
1122 	/*
1123 	 * If the device has been made invalid, error out
1124 	 */
1125 	if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1126 		cam_periph_unlock(periph);
1127 		biofinish(bp, NULL, ENXIO);
1128 		return;
1129 	}
1130 
1131 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1132 
1133 	/*
1134 	 * Place it in the queue of disk activities for this disk
1135 	 */
1136 	if (bp->bio_cmd == BIO_DELETE) {
1137 		if (bp->bio_bcount == 0)
1138 			biodone(bp);
1139 		else
1140 			bioq_disksort(&softc->delete_queue, bp);
1141 	} else
1142 		bioq_disksort(&softc->bio_queue, bp);
1143 
1144 	/*
1145 	 * Schedule ourselves for performing the work.
1146 	 */
1147 	daschedule(periph);
1148 	cam_periph_unlock(periph);
1149 
1150 	return;
1151 }
1152 
1153 static int
1154 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1155 {
1156 	struct	    cam_periph *periph;
1157 	struct	    da_softc *softc;
1158 	u_int	    secsize;
1159 	struct	    ccb_scsiio csio;
1160 	struct	    disk *dp;
1161 	int	    error = 0;
1162 
1163 	dp = arg;
1164 	periph = dp->d_drv1;
1165 	if (periph == NULL)
1166 		return (ENXIO);
1167 	softc = (struct da_softc *)periph->softc;
1168 	cam_periph_lock(periph);
1169 	secsize = softc->params.secsize;
1170 
1171 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1172 		cam_periph_unlock(periph);
1173 		return (ENXIO);
1174 	}
1175 
1176 	if (length > 0) {
1177 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1178 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1179 		scsi_read_write(&csio,
1180 				/*retries*/0,
1181 				dadone,
1182 				MSG_ORDERED_Q_TAG,
1183 				/*read*/FALSE,
1184 				/*byte2*/0,
1185 				/*minimum_cmd_size*/ softc->minimum_cmd_size,
1186 				offset / secsize,
1187 				length / secsize,
1188 				/*data_ptr*/(u_int8_t *) virtual,
1189 				/*dxfer_len*/length,
1190 				/*sense_len*/SSD_FULL_SIZE,
1191 				da_default_timeout * 1000);
1192 		xpt_polled_action((union ccb *)&csio);
1193 
1194 		error = cam_periph_error((union ccb *)&csio,
1195 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1196 		if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1197 			cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1198 			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1199 		if (error != 0)
1200 			printf("Aborting dump due to I/O error.\n");
1201 		cam_periph_unlock(periph);
1202 		return (error);
1203 	}
1204 
1205 	/*
1206 	 * Sync the disk cache contents to the physical media.
1207 	 */
1208 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1209 
1210 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1211 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1212 		scsi_synchronize_cache(&csio,
1213 				       /*retries*/0,
1214 				       /*cbfcnp*/dadone,
1215 				       MSG_SIMPLE_Q_TAG,
1216 				       /*begin_lba*/0,/* Cover the whole disk */
1217 				       /*lb_count*/0,
1218 				       SSD_FULL_SIZE,
1219 				       5 * 60 * 1000);
1220 		xpt_polled_action((union ccb *)&csio);
1221 
1222 		error = cam_periph_error((union ccb *)&csio,
1223 		    0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1224 		if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1225 			cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1226 			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1227 		if (error != 0)
1228 			xpt_print(periph->path, "Synchronize cache failed\n");
1229 	}
1230 	cam_periph_unlock(periph);
1231 	return (error);
1232 }
1233 
1234 static int
1235 dagetattr(struct bio *bp)
1236 {
1237 	int ret;
1238 	struct cam_periph *periph;
1239 
1240 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1241 	if (periph == NULL)
1242 		return (ENXIO);
1243 
1244 	cam_periph_lock(periph);
1245 	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1246 	    periph->path);
1247 	cam_periph_unlock(periph);
1248 	if (ret == 0)
1249 		bp->bio_completed = bp->bio_length;
1250 	return ret;
1251 }
1252 
1253 static void
1254 dainit(void)
1255 {
1256 	cam_status status;
1257 
1258 	/*
1259 	 * Install a global async callback.  This callback will
1260 	 * receive async callbacks like "new device found".
1261 	 */
1262 	status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1263 
1264 	if (status != CAM_REQ_CMP) {
1265 		printf("da: Failed to attach master async callback "
1266 		       "due to status 0x%x!\n", status);
1267 	} else if (da_send_ordered) {
1268 
1269 		/* Register our shutdown event handler */
1270 		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1271 					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1272 		    printf("dainit: shutdown event registration failed!\n");
1273 	}
1274 }
1275 
1276 /*
1277  * Callback from GEOM, called when it has finished cleaning up its
1278  * resources.
1279  */
1280 static void
1281 dadiskgonecb(struct disk *dp)
1282 {
1283 	struct cam_periph *periph;
1284 
1285 	periph = (struct cam_periph *)dp->d_drv1;
1286 
1287 	cam_periph_release(periph);
1288 }
1289 
1290 static void
1291 daoninvalidate(struct cam_periph *periph)
1292 {
1293 	struct da_softc *softc;
1294 
1295 	softc = (struct da_softc *)periph->softc;
1296 
1297 	/*
1298 	 * De-register any async callbacks.
1299 	 */
1300 	xpt_register_async(0, daasync, periph, periph->path);
1301 
1302 	softc->flags |= DA_FLAG_PACK_INVALID;
1303 
1304 	/*
1305 	 * Return all queued I/O with ENXIO.
1306 	 * XXX Handle any transactions queued to the card
1307 	 *     with XPT_ABORT_CCB.
1308 	 */
1309 	bioq_flush(&softc->bio_queue, NULL, ENXIO);
1310 	bioq_flush(&softc->delete_queue, NULL, ENXIO);
1311 
1312 	/*
1313 	 * Tell GEOM that we've gone away, we'll get a callback when it is
1314 	 * done cleaning up its resources.
1315 	 */
1316 	disk_gone(softc->disk);
1317 
1318 	xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1319 		  softc->outstanding_cmds, periph->refcount);
1320 }
1321 
1322 static void
1323 dacleanup(struct cam_periph *periph)
1324 {
1325 	struct da_softc *softc;
1326 
1327 	softc = (struct da_softc *)periph->softc;
1328 
1329 	xpt_print(periph->path, "removing device entry\n");
1330 	cam_periph_unlock(periph);
1331 
1332 	/*
1333 	 * If we can't free the sysctl tree, oh well...
1334 	 */
1335 	if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1336 	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1337 		xpt_print(periph->path, "can't remove sysctl context\n");
1338 	}
1339 
1340 	callout_drain(&softc->mediapoll_c);
1341 	disk_destroy(softc->disk);
1342 	callout_drain(&softc->sendordered_c);
1343 	free(softc, M_DEVBUF);
1344 	cam_periph_lock(periph);
1345 }
1346 
1347 static void
1348 daasync(void *callback_arg, u_int32_t code,
1349 	struct cam_path *path, void *arg)
1350 {
1351 	struct cam_periph *periph;
1352 	struct da_softc *softc;
1353 
1354 	periph = (struct cam_periph *)callback_arg;
1355 	switch (code) {
1356 	case AC_FOUND_DEVICE:
1357 	{
1358 		struct ccb_getdev *cgd;
1359 		cam_status status;
1360 
1361 		cgd = (struct ccb_getdev *)arg;
1362 		if (cgd == NULL)
1363 			break;
1364 
1365 		if (cgd->protocol != PROTO_SCSI)
1366 			break;
1367 
1368 		if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1369 		    && SID_TYPE(&cgd->inq_data) != T_RBC
1370 		    && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1371 			break;
1372 
1373 		/*
1374 		 * Allocate a peripheral instance for
1375 		 * this device and start the probe
1376 		 * process.
1377 		 */
1378 		status = cam_periph_alloc(daregister, daoninvalidate,
1379 					  dacleanup, dastart,
1380 					  "da", CAM_PERIPH_BIO,
1381 					  cgd->ccb_h.path, daasync,
1382 					  AC_FOUND_DEVICE, cgd);
1383 
1384 		if (status != CAM_REQ_CMP
1385 		 && status != CAM_REQ_INPROG)
1386 			printf("daasync: Unable to attach to new device "
1387 				"due to status 0x%x\n", status);
1388 		return;
1389 	}
1390 	case AC_ADVINFO_CHANGED:
1391 	{
1392 		uintptr_t buftype;
1393 
1394 		buftype = (uintptr_t)arg;
1395 		if (buftype == CDAI_TYPE_PHYS_PATH) {
1396 			struct da_softc *softc;
1397 
1398 			softc = periph->softc;
1399 			disk_attr_changed(softc->disk, "GEOM::physpath",
1400 					  M_NOWAIT);
1401 		}
1402 		break;
1403 	}
1404 	case AC_UNIT_ATTENTION:
1405 	{
1406 		union ccb *ccb;
1407 		int error_code, sense_key, asc, ascq;
1408 
1409 		softc = (struct da_softc *)periph->softc;
1410 		ccb = (union ccb *)arg;
1411 
1412 		/*
1413 		 * Handle all UNIT ATTENTIONs except our own,
1414 		 * as they will be handled by daerror().
1415 		 */
1416 		if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1417 		    scsi_extract_sense_ccb(ccb,
1418 		     &error_code, &sense_key, &asc, &ascq)) {
1419 			if (asc == 0x2A && ascq == 0x09) {
1420 				xpt_print(ccb->ccb_h.path,
1421 				    "capacity data has changed\n");
1422 				dareprobe(periph);
1423 			} else if (asc == 0x28 && ascq == 0x00)
1424 				disk_media_changed(softc->disk, M_NOWAIT);
1425 		}
1426 		cam_periph_async(periph, code, path, arg);
1427 		break;
1428 	}
1429 	case AC_SCSI_AEN:
1430 		softc = (struct da_softc *)periph->softc;
1431 		if (!softc->tur) {
1432 			if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1433 				softc->tur = 1;
1434 				daschedule(periph);
1435 			}
1436 		}
1437 		/* FALLTHROUGH */
1438 	case AC_SENT_BDR:
1439 	case AC_BUS_RESET:
1440 	{
1441 		struct ccb_hdr *ccbh;
1442 
1443 		softc = (struct da_softc *)periph->softc;
1444 		/*
1445 		 * Don't fail on the expected unit attention
1446 		 * that will occur.
1447 		 */
1448 		softc->flags |= DA_FLAG_RETRY_UA;
1449 		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1450 			ccbh->ccb_state |= DA_CCB_RETRY_UA;
1451 		break;
1452 	}
1453 	default:
1454 		break;
1455 	}
1456 	cam_periph_async(periph, code, path, arg);
1457 }
1458 
1459 static void
1460 dasysctlinit(void *context, int pending)
1461 {
1462 	struct cam_periph *periph;
1463 	struct da_softc *softc;
1464 	char tmpstr[80], tmpstr2[80];
1465 	struct ccb_trans_settings cts;
1466 
1467 	periph = (struct cam_periph *)context;
1468 	/*
1469 	 * periph was held for us when this task was enqueued
1470 	 */
1471 	if (periph->flags & CAM_PERIPH_INVALID) {
1472 		cam_periph_release(periph);
1473 		return;
1474 	}
1475 
1476 	softc = (struct da_softc *)periph->softc;
1477 	snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1478 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1479 
1480 	sysctl_ctx_init(&softc->sysctl_ctx);
1481 	softc->flags |= DA_FLAG_SCTX_INIT;
1482 	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1483 		SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1484 		CTLFLAG_RD, 0, tmpstr);
1485 	if (softc->sysctl_tree == NULL) {
1486 		printf("dasysctlinit: unable to allocate sysctl tree\n");
1487 		cam_periph_release(periph);
1488 		return;
1489 	}
1490 
1491 	/*
1492 	 * Now register the sysctl handler, so the user can change the value on
1493 	 * the fly.
1494 	 */
1495 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1496 		OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1497 		softc, 0, dadeletemethodsysctl, "A",
1498 		"BIO_DELETE execution method");
1499 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1500 		OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1501 		&softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1502 		"Minimum CDB size");
1503 
1504 	SYSCTL_ADD_INT(&softc->sysctl_ctx,
1505 		       SYSCTL_CHILDREN(softc->sysctl_tree),
1506 		       OID_AUTO,
1507 		       "error_inject",
1508 		       CTLFLAG_RW,
1509 		       &softc->error_inject,
1510 		       0,
1511 		       "error_inject leaf");
1512 
1513 
1514 	/*
1515 	 * Add some addressing info.
1516 	 */
1517 	memset(&cts, 0, sizeof (cts));
1518 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1519 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1520 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1521 	cam_periph_lock(periph);
1522 	xpt_action((union ccb *)&cts);
1523 	cam_periph_unlock(periph);
1524 	if (cts.ccb_h.status != CAM_REQ_CMP) {
1525 		cam_periph_release(periph);
1526 		return;
1527 	}
1528 	if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1529 		struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1530 		if (fc->valid & CTS_FC_VALID_WWPN) {
1531 			softc->wwpn = fc->wwpn;
1532 			SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1533 			    SYSCTL_CHILDREN(softc->sysctl_tree),
1534 			    OID_AUTO, "wwpn", CTLFLAG_RD,
1535 			    &softc->wwpn, "World Wide Port Name");
1536 		}
1537 	}
1538 	cam_periph_release(periph);
1539 }
1540 
1541 static int
1542 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1543 {
1544 	int error, value;
1545 
1546 	value = *(int *)arg1;
1547 
1548 	error = sysctl_handle_int(oidp, &value, 0, req);
1549 
1550 	if ((error != 0)
1551 	 || (req->newptr == NULL))
1552 		return (error);
1553 
1554 	/*
1555 	 * Acceptable values here are 6, 10, 12 or 16.
1556 	 */
1557 	if (value < 6)
1558 		value = 6;
1559 	else if ((value > 6)
1560 	      && (value <= 10))
1561 		value = 10;
1562 	else if ((value > 10)
1563 	      && (value <= 12))
1564 		value = 12;
1565 	else if (value > 12)
1566 		value = 16;
1567 
1568 	*(int *)arg1 = value;
1569 
1570 	return (0);
1571 }
1572 
1573 static int
1574 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1575 {
1576 
1577 	if (delete_method < 0 || delete_method > DA_DELETE_MAX)
1578 		return (EINVAL);
1579 
1580 	softc->delete_method = delete_method;
1581 
1582 	if (softc->delete_method > DA_DELETE_DISABLE)
1583 		softc->disk->d_flags |= DISKFLAG_CANDELETE;
1584 	else
1585 		softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1586 
1587 	return (0);
1588 }
1589 
1590 static int
1591 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1592 {
1593 	char buf[16];
1594 	const char *p;
1595 	struct da_softc *softc;
1596 	int i, error, value;
1597 
1598 	softc = (struct da_softc *)arg1;
1599 
1600 	value = softc->delete_method;
1601 	if (value < 0 || value > DA_DELETE_MAX)
1602 		p = "UNKNOWN";
1603 	else
1604 		p = da_delete_method_names[value];
1605 	strncpy(buf, p, sizeof(buf));
1606 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1607 	if (error != 0 || req->newptr == NULL)
1608 		return (error);
1609 	for (i = 0; i <= DA_DELETE_MAX; i++) {
1610 		if (strcmp(buf, da_delete_method_names[i]) != 0)
1611 			continue;
1612 		return dadeletemethodset(softc, i);
1613 	}
1614 	return (EINVAL);
1615 }
1616 
1617 static cam_status
1618 daregister(struct cam_periph *periph, void *arg)
1619 {
1620 	struct da_softc *softc;
1621 	struct ccb_pathinq cpi;
1622 	struct ccb_getdev *cgd;
1623 	char tmpstr[80];
1624 	caddr_t match;
1625 
1626 	cgd = (struct ccb_getdev *)arg;
1627 	if (cgd == NULL) {
1628 		printf("daregister: no getdev CCB, can't register device\n");
1629 		return(CAM_REQ_CMP_ERR);
1630 	}
1631 
1632 	softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1633 	    M_NOWAIT|M_ZERO);
1634 
1635 	if (softc == NULL) {
1636 		printf("daregister: Unable to probe new device. "
1637 		       "Unable to allocate softc\n");
1638 		return(CAM_REQ_CMP_ERR);
1639 	}
1640 
1641 	LIST_INIT(&softc->pending_ccbs);
1642 	softc->state = DA_STATE_PROBE;
1643 	bioq_init(&softc->bio_queue);
1644 	bioq_init(&softc->delete_queue);
1645 	bioq_init(&softc->delete_run_queue);
1646 	if (SID_IS_REMOVABLE(&cgd->inq_data))
1647 		softc->flags |= DA_FLAG_PACK_REMOVABLE;
1648 	softc->unmap_max_ranges = UNMAP_MAX_RANGES;
1649 	softc->unmap_max_lba = 1024*1024*2;
1650 
1651 	periph->softc = softc;
1652 
1653 	/*
1654 	 * See if this device has any quirks.
1655 	 */
1656 	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1657 			       (caddr_t)da_quirk_table,
1658 			       sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1659 			       sizeof(*da_quirk_table), scsi_inquiry_match);
1660 
1661 	if (match != NULL)
1662 		softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1663 	else
1664 		softc->quirks = DA_Q_NONE;
1665 
1666 	/* Check if the SIM does not want 6 byte commands */
1667 	bzero(&cpi, sizeof(cpi));
1668 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1669 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1670 	xpt_action((union ccb *)&cpi);
1671 	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1672 		softc->quirks |= DA_Q_NO_6_BYTE;
1673 
1674 	TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1675 
1676 	/*
1677 	 * Take an exclusive refcount on the periph while dastart is called
1678 	 * to finish the probe.  The reference will be dropped in dadone at
1679 	 * the end of probe.
1680 	 */
1681 	(void)cam_periph_hold(periph, PRIBIO);
1682 
1683 	/*
1684 	 * Schedule a periodic event to occasionally send an
1685 	 * ordered tag to a device.
1686 	 */
1687 	callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1688 	callout_reset(&softc->sendordered_c,
1689 	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1690 	    dasendorderedtag, softc);
1691 
1692 	mtx_unlock(periph->sim->mtx);
1693 	/*
1694 	 * RBC devices don't have to support READ(6), only READ(10).
1695 	 */
1696 	if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1697 		softc->minimum_cmd_size = 10;
1698 	else
1699 		softc->minimum_cmd_size = 6;
1700 
1701 	/*
1702 	 * Load the user's default, if any.
1703 	 */
1704 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1705 		 periph->unit_number);
1706 	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1707 
1708 	/*
1709 	 * 6, 10, 12 and 16 are the currently permissible values.
1710 	 */
1711 	if (softc->minimum_cmd_size < 6)
1712 		softc->minimum_cmd_size = 6;
1713 	else if ((softc->minimum_cmd_size > 6)
1714 	      && (softc->minimum_cmd_size <= 10))
1715 		softc->minimum_cmd_size = 10;
1716 	else if ((softc->minimum_cmd_size > 10)
1717 	      && (softc->minimum_cmd_size <= 12))
1718 		softc->minimum_cmd_size = 12;
1719 	else if (softc->minimum_cmd_size > 12)
1720 		softc->minimum_cmd_size = 16;
1721 
1722 	/* Predict whether device may support READ CAPACITY(16). */
1723 	if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
1724 		softc->flags |= DA_FLAG_CAN_RC16;
1725 		softc->state = DA_STATE_PROBE2;
1726 	}
1727 
1728 	/*
1729 	 * Register this media as a disk.
1730 	 */
1731 	softc->disk = disk_alloc();
1732 	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1733 			  periph->unit_number, 0,
1734 			  DEVSTAT_BS_UNAVAILABLE,
1735 			  SID_TYPE(&cgd->inq_data) |
1736 			  XPORT_DEVSTAT_TYPE(cpi.transport),
1737 			  DEVSTAT_PRIORITY_DISK);
1738 	softc->disk->d_open = daopen;
1739 	softc->disk->d_close = daclose;
1740 	softc->disk->d_strategy = dastrategy;
1741 	softc->disk->d_dump = dadump;
1742 	softc->disk->d_getattr = dagetattr;
1743 	softc->disk->d_gone = dadiskgonecb;
1744 	softc->disk->d_name = "da";
1745 	softc->disk->d_drv1 = periph;
1746 	if (cpi.maxio == 0)
1747 		softc->disk->d_maxsize = DFLTPHYS;	/* traditional default */
1748 	else if (cpi.maxio > MAXPHYS)
1749 		softc->disk->d_maxsize = MAXPHYS;	/* for safety */
1750 	else
1751 		softc->disk->d_maxsize = cpi.maxio;
1752 	softc->disk->d_unit = periph->unit_number;
1753 	softc->disk->d_flags = 0;
1754 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1755 		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1756 	cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1757 	    sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1758 	strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1759 	cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1760 	    cgd->inq_data.product, sizeof(cgd->inq_data.product),
1761 	    sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1762 	softc->disk->d_hba_vendor = cpi.hba_vendor;
1763 	softc->disk->d_hba_device = cpi.hba_device;
1764 	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1765 	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1766 
1767 	/*
1768 	 * Acquire a reference to the periph before we register with GEOM.
1769 	 * We'll release this reference once GEOM calls us back (via
1770 	 * dadiskgonecb()) telling us that our provider has been freed.
1771 	 */
1772 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1773 		xpt_print(periph->path, "%s: lost periph during "
1774 			  "registration!\n", __func__);
1775 		mtx_lock(periph->sim->mtx);
1776 		return (CAM_REQ_CMP_ERR);
1777 	}
1778 
1779 	disk_create(softc->disk, DISK_VERSION);
1780 	mtx_lock(periph->sim->mtx);
1781 
1782 	/*
1783 	 * Add async callbacks for events of interest.
1784 	 * I don't bother checking if this fails as,
1785 	 * in most cases, the system will function just
1786 	 * fine without them and the only alternative
1787 	 * would be to not attach the device on failure.
1788 	 */
1789 	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1790 	    AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
1791 	    daasync, periph, periph->path);
1792 
1793 	/*
1794 	 * Emit an attribute changed notification just in case
1795 	 * physical path information arrived before our async
1796 	 * event handler was registered, but after anyone attaching
1797 	 * to our disk device polled it.
1798 	 */
1799 	disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1800 
1801 	/*
1802 	 * Schedule a periodic media polling events.
1803 	 */
1804 	callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
1805 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
1806 	    (cgd->inq_flags & SID_AEN) == 0 &&
1807 	    da_poll_period != 0)
1808 		callout_reset(&softc->mediapoll_c, da_poll_period * hz,
1809 		    damediapoll, periph);
1810 
1811 	xpt_schedule(periph, CAM_PRIORITY_DEV);
1812 
1813 	return(CAM_REQ_CMP);
1814 }
1815 
1816 static void
1817 dastart(struct cam_periph *periph, union ccb *start_ccb)
1818 {
1819 	struct da_softc *softc;
1820 
1821 	softc = (struct da_softc *)periph->softc;
1822 
1823 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
1824 
1825 	switch (softc->state) {
1826 	case DA_STATE_NORMAL:
1827 	{
1828 		struct bio *bp, *bp1;
1829 		uint8_t tag_code;
1830 
1831 		/* Execute immediate CCB if waiting. */
1832 		if (periph->immediate_priority <= periph->pinfo.priority) {
1833 			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1834 					("queuing for immediate ccb\n"));
1835 			start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1836 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1837 					  periph_links.sle);
1838 			periph->immediate_priority = CAM_PRIORITY_NONE;
1839 			wakeup(&periph->ccb_list);
1840 			/* May have more work to do, so ensure we stay scheduled */
1841 			daschedule(periph);
1842 			break;
1843 		}
1844 
1845 		/* Run BIO_DELETE if not running yet. */
1846 		if (!softc->delete_running &&
1847 		    (bp = bioq_first(&softc->delete_queue)) != NULL) {
1848 		    uint64_t lba;
1849 		    u_int count;
1850 
1851 		    if (softc->delete_method == DA_DELETE_UNMAP) {
1852 			uint8_t *buf = softc->unmap_buf;
1853 			uint64_t lastlba = (uint64_t)-1;
1854 			uint32_t lastcount = 0;
1855 			int blocks = 0, off, ranges = 0;
1856 
1857 			softc->delete_running = 1;
1858 			bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
1859 			bp1 = bp;
1860 			do {
1861 				bioq_remove(&softc->delete_queue, bp1);
1862 				if (bp1 != bp)
1863 					bioq_insert_tail(&softc->delete_run_queue, bp1);
1864 				lba = bp1->bio_pblkno;
1865 				count = bp1->bio_bcount / softc->params.secsize;
1866 
1867 				/* Try to extend the previous range. */
1868 				if (lba == lastlba) {
1869 					lastcount += count;
1870 					off = (ranges - 1) * 16 + 8;
1871 					scsi_ulto4b(lastcount, &buf[off + 8]);
1872 				} else if (count > 0) {
1873 					off = ranges * 16 + 8;
1874 					scsi_u64to8b(lba, &buf[off + 0]);
1875 					scsi_ulto4b(count, &buf[off + 8]);
1876 					lastcount = count;
1877 					ranges++;
1878 				}
1879 				blocks += count;
1880 				lastlba = lba + count;
1881 				bp1 = bioq_first(&softc->delete_queue);
1882 				if (bp1 == NULL ||
1883 				    ranges >= softc->unmap_max_ranges ||
1884 				    blocks + bp1->bio_bcount /
1885 				     softc->params.secsize > softc->unmap_max_lba)
1886 					break;
1887 			} while (1);
1888 			scsi_ulto2b(ranges * 16 + 6, &buf[0]);
1889 			scsi_ulto2b(ranges * 16, &buf[2]);
1890 
1891 			scsi_unmap(&start_ccb->csio,
1892 					/*retries*/da_retry_count,
1893 					/*cbfcnp*/dadone,
1894 					/*tag_action*/MSG_SIMPLE_Q_TAG,
1895 					/*byte2*/0,
1896 					/*data_ptr*/ buf,
1897 					/*dxfer_len*/ ranges * 16 + 8,
1898 					/*sense_len*/SSD_FULL_SIZE,
1899 					da_default_timeout * 1000);
1900 			start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1901 			goto out;
1902 		    } else if (softc->delete_method == DA_DELETE_ZERO ||
1903 			       softc->delete_method == DA_DELETE_WS10 ||
1904 			       softc->delete_method == DA_DELETE_WS16) {
1905 			softc->delete_running = 1;
1906 			lba = bp->bio_pblkno;
1907 			count = 0;
1908 			bp1 = bp;
1909 			do {
1910 				bioq_remove(&softc->delete_queue, bp1);
1911 				if (bp1 != bp)
1912 					bioq_insert_tail(&softc->delete_run_queue, bp1);
1913 				count += bp1->bio_bcount / softc->params.secsize;
1914 				bp1 = bioq_first(&softc->delete_queue);
1915 				if (bp1 == NULL ||
1916 				    lba + count != bp1->bio_pblkno ||
1917 				    count + bp1->bio_bcount /
1918 				     softc->params.secsize > 0xffff)
1919 					break;
1920 			} while (1);
1921 
1922 			scsi_write_same(&start_ccb->csio,
1923 					/*retries*/da_retry_count,
1924 					/*cbfcnp*/dadone,
1925 					/*tag_action*/MSG_SIMPLE_Q_TAG,
1926 					/*byte2*/softc->delete_method ==
1927 					    DA_DELETE_ZERO ? 0 : SWS_UNMAP,
1928 					softc->delete_method ==
1929 					    DA_DELETE_WS16 ? 16 : 10,
1930 					/*lba*/lba,
1931 					/*block_count*/count,
1932 					/*data_ptr*/ __DECONST(void *,
1933 					    zero_region),
1934 					/*dxfer_len*/ softc->params.secsize,
1935 					/*sense_len*/SSD_FULL_SIZE,
1936 					da_default_timeout * 1000);
1937 			start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1938 			goto out;
1939 		    } else {
1940 			bioq_flush(&softc->delete_queue, NULL, 0);
1941 			/* FALLTHROUGH */
1942 		    }
1943 		}
1944 
1945 		/* Run regular command. */
1946 		bp = bioq_takefirst(&softc->bio_queue);
1947 		if (bp == NULL) {
1948 			if (softc->tur) {
1949 				softc->tur = 0;
1950 				scsi_test_unit_ready(&start_ccb->csio,
1951 				     /*retries*/ da_retry_count,
1952 				     dadone,
1953 				     MSG_SIMPLE_Q_TAG,
1954 				     SSD_FULL_SIZE,
1955 				     da_default_timeout * 1000);
1956 				start_ccb->ccb_h.ccb_bp = NULL;
1957 				start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
1958 				xpt_action(start_ccb);
1959 			} else
1960 				xpt_release_ccb(start_ccb);
1961 			break;
1962 		}
1963 		if (softc->tur) {
1964 			softc->tur = 0;
1965 			cam_periph_release_locked(periph);
1966 		}
1967 
1968 		if ((bp->bio_flags & BIO_ORDERED) != 0 ||
1969 		    (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1970 			softc->flags &= ~DA_FLAG_NEED_OTAG;
1971 			softc->ordered_tag_count++;
1972 			tag_code = MSG_ORDERED_Q_TAG;
1973 		} else {
1974 			tag_code = MSG_SIMPLE_Q_TAG;
1975 		}
1976 
1977 		switch (bp->bio_cmd) {
1978 		case BIO_READ:
1979 		case BIO_WRITE:
1980 			scsi_read_write(&start_ccb->csio,
1981 					/*retries*/da_retry_count,
1982 					/*cbfcnp*/dadone,
1983 					/*tag_action*/tag_code,
1984 					/*read_op*/bp->bio_cmd
1985 						== BIO_READ,
1986 					/*byte2*/0,
1987 					softc->minimum_cmd_size,
1988 					/*lba*/bp->bio_pblkno,
1989 					/*block_count*/bp->bio_bcount /
1990 					softc->params.secsize,
1991 					/*data_ptr*/ bp->bio_data,
1992 					/*dxfer_len*/ bp->bio_bcount,
1993 					/*sense_len*/SSD_FULL_SIZE,
1994 					da_default_timeout * 1000);
1995 			break;
1996 		case BIO_FLUSH:
1997 			/*
1998 			 * BIO_FLUSH doesn't currently communicate
1999 			 * range data, so we synchronize the cache
2000 			 * over the whole disk.  We also force
2001 			 * ordered tag semantics the flush applies
2002 			 * to all previously queued I/O.
2003 			 */
2004 			scsi_synchronize_cache(&start_ccb->csio,
2005 					       /*retries*/1,
2006 					       /*cbfcnp*/dadone,
2007 					       MSG_ORDERED_Q_TAG,
2008 					       /*begin_lba*/0,
2009 					       /*lb_count*/0,
2010 					       SSD_FULL_SIZE,
2011 					       da_default_timeout*1000);
2012 			break;
2013 		}
2014 		start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2015 
2016 out:
2017 		/*
2018 		 * Block out any asyncronous callbacks
2019 		 * while we touch the pending ccb list.
2020 		 */
2021 		LIST_INSERT_HEAD(&softc->pending_ccbs,
2022 				 &start_ccb->ccb_h, periph_links.le);
2023 		softc->outstanding_cmds++;
2024 
2025 		/* We expect a unit attention from this device */
2026 		if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2027 			start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2028 			softc->flags &= ~DA_FLAG_RETRY_UA;
2029 		}
2030 
2031 		start_ccb->ccb_h.ccb_bp = bp;
2032 		xpt_action(start_ccb);
2033 
2034 		/* May have more work to do, so ensure we stay scheduled */
2035 		daschedule(periph);
2036 		break;
2037 	}
2038 	case DA_STATE_PROBE:
2039 	{
2040 		struct scsi_read_capacity_data *rcap;
2041 
2042 		rcap = (struct scsi_read_capacity_data *)
2043 		    malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2044 		if (rcap == NULL) {
2045 			printf("dastart: Couldn't malloc read_capacity data\n");
2046 			/* da_free_periph??? */
2047 			break;
2048 		}
2049 		scsi_read_capacity(&start_ccb->csio,
2050 				   /*retries*/da_retry_count,
2051 				   dadone,
2052 				   MSG_SIMPLE_Q_TAG,
2053 				   rcap,
2054 				   SSD_FULL_SIZE,
2055 				   /*timeout*/5000);
2056 		start_ccb->ccb_h.ccb_bp = NULL;
2057 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
2058 		xpt_action(start_ccb);
2059 		break;
2060 	}
2061 	case DA_STATE_PROBE2:
2062 	{
2063 		struct scsi_read_capacity_data_long *rcaplong;
2064 
2065 		rcaplong = (struct scsi_read_capacity_data_long *)
2066 			malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2067 		if (rcaplong == NULL) {
2068 			printf("dastart: Couldn't malloc read_capacity data\n");
2069 			/* da_free_periph??? */
2070 			break;
2071 		}
2072 		scsi_read_capacity_16(&start_ccb->csio,
2073 				      /*retries*/ da_retry_count,
2074 				      /*cbfcnp*/ dadone,
2075 				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
2076 				      /*lba*/ 0,
2077 				      /*reladr*/ 0,
2078 				      /*pmi*/ 0,
2079 				      /*rcap_buf*/ (uint8_t *)rcaplong,
2080 				      /*rcap_buf_len*/ sizeof(*rcaplong),
2081 				      /*sense_len*/ SSD_FULL_SIZE,
2082 				      /*timeout*/ da_default_timeout * 1000);
2083 		start_ccb->ccb_h.ccb_bp = NULL;
2084 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
2085 		xpt_action(start_ccb);
2086 		break;
2087 	}
2088 	}
2089 }
2090 
2091 static int
2092 cmd6workaround(union ccb *ccb)
2093 {
2094 	struct scsi_rw_6 cmd6;
2095 	struct scsi_rw_10 *cmd10;
2096 	struct da_softc *softc;
2097 	u_int8_t *cdb;
2098 	struct bio *bp;
2099 	int frozen;
2100 
2101 	cdb = ccb->csio.cdb_io.cdb_bytes;
2102 	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2103 
2104 	if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2105 		if (softc->delete_method == DA_DELETE_UNMAP) {
2106 			xpt_print(ccb->ccb_h.path, "UNMAP is not supported, "
2107 			    "switching to WRITE SAME(16) with UNMAP.\n");
2108 			dadeletemethodset(softc, DA_DELETE_WS16);
2109 		} else if (softc->delete_method == DA_DELETE_WS16) {
2110 			xpt_print(ccb->ccb_h.path,
2111 			    "WRITE SAME(16) with UNMAP is not supported, "
2112 			    "disabling BIO_DELETE.\n");
2113 			dadeletemethodset(softc, DA_DELETE_DISABLE);
2114 		} else if (softc->delete_method == DA_DELETE_WS10) {
2115 			xpt_print(ccb->ccb_h.path,
2116 			    "WRITE SAME(10) with UNMAP is not supported, "
2117 			    "disabling BIO_DELETE.\n");
2118 			dadeletemethodset(softc, DA_DELETE_DISABLE);
2119 		} else if (softc->delete_method == DA_DELETE_ZERO) {
2120 			xpt_print(ccb->ccb_h.path,
2121 			    "WRITE SAME(10) is not supported, "
2122 			    "disabling BIO_DELETE.\n");
2123 			dadeletemethodset(softc, DA_DELETE_DISABLE);
2124 		} else
2125 			dadeletemethodset(softc, DA_DELETE_DISABLE);
2126 		while ((bp = bioq_takefirst(&softc->delete_run_queue))
2127 		    != NULL)
2128 			bioq_disksort(&softc->delete_queue, bp);
2129 		bioq_insert_tail(&softc->delete_queue,
2130 		    (struct bio *)ccb->ccb_h.ccb_bp);
2131 		ccb->ccb_h.ccb_bp = NULL;
2132 		return (0);
2133 	}
2134 
2135 	/* Translation only possible if CDB is an array and cmd is R/W6 */
2136 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2137 	    (*cdb != READ_6 && *cdb != WRITE_6))
2138 		return 0;
2139 
2140 	xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2141 	    "increasing minimum_cmd_size to 10.\n");
2142  	softc->minimum_cmd_size = 10;
2143 
2144 	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2145 	cmd10 = (struct scsi_rw_10 *)cdb;
2146 	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2147 	cmd10->byte2 = 0;
2148 	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2149 	cmd10->reserved = 0;
2150 	scsi_ulto2b(cmd6.length, cmd10->length);
2151 	cmd10->control = cmd6.control;
2152 	ccb->csio.cdb_len = sizeof(*cmd10);
2153 
2154 	/* Requeue request, unfreezing queue if necessary */
2155 	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2156  	ccb->ccb_h.status = CAM_REQUEUE_REQ;
2157 	xpt_action(ccb);
2158 	if (frozen) {
2159 		cam_release_devq(ccb->ccb_h.path,
2160 				 /*relsim_flags*/0,
2161 				 /*reduction*/0,
2162 				 /*timeout*/0,
2163 				 /*getcount_only*/0);
2164 	}
2165 	return (ERESTART);
2166 }
2167 
2168 static void
2169 dadone(struct cam_periph *periph, union ccb *done_ccb)
2170 {
2171 	struct da_softc *softc;
2172 	struct ccb_scsiio *csio;
2173 	u_int32_t  priority;
2174 	da_ccb_state state;
2175 
2176 	softc = (struct da_softc *)periph->softc;
2177 	priority = done_ccb->ccb_h.pinfo.priority;
2178 
2179 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2180 
2181 	csio = &done_ccb->csio;
2182 	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2183 	switch (state) {
2184 	case DA_CCB_BUFFER_IO:
2185 	case DA_CCB_DELETE:
2186 	{
2187 		struct bio *bp, *bp1;
2188 
2189 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2190 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2191 			int error;
2192 			int sf;
2193 
2194 			if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2195 				sf = SF_RETRY_UA;
2196 			else
2197 				sf = 0;
2198 
2199 			error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2200 			if (error == ERESTART) {
2201 				/*
2202 				 * A retry was scheuled, so
2203 				 * just return.
2204 				 */
2205 				return;
2206 			}
2207 			bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2208 			if (error != 0) {
2209 				int queued_error;
2210 
2211 				/*
2212 				 * return all queued I/O with EIO, so that
2213 				 * the client can retry these I/Os in the
2214 				 * proper order should it attempt to recover.
2215 				 */
2216 				queued_error = EIO;
2217 
2218 				if (error == ENXIO
2219 				 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2220 					/*
2221 					 * Catastrophic error.  Mark our pack as
2222 					 * invalid.
2223 					 */
2224 					/*
2225 					 * XXX See if this is really a media
2226 					 * XXX change first?
2227 					 */
2228 					xpt_print(periph->path,
2229 					    "Invalidating pack\n");
2230 					softc->flags |= DA_FLAG_PACK_INVALID;
2231 					queued_error = ENXIO;
2232 				}
2233 				bioq_flush(&softc->bio_queue, NULL,
2234 					   queued_error);
2235 				if (bp != NULL) {
2236 					bp->bio_error = error;
2237 					bp->bio_resid = bp->bio_bcount;
2238 					bp->bio_flags |= BIO_ERROR;
2239 				}
2240 			} else if (bp != NULL) {
2241 				bp->bio_resid = csio->resid;
2242 				bp->bio_error = 0;
2243 				if (bp->bio_resid != 0)
2244 					bp->bio_flags |= BIO_ERROR;
2245 			}
2246 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2247 				cam_release_devq(done_ccb->ccb_h.path,
2248 						 /*relsim_flags*/0,
2249 						 /*reduction*/0,
2250 						 /*timeout*/0,
2251 						 /*getcount_only*/0);
2252 		} else if (bp != NULL) {
2253 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2254 				panic("REQ_CMP with QFRZN");
2255 			bp->bio_resid = csio->resid;
2256 			if (csio->resid > 0)
2257 				bp->bio_flags |= BIO_ERROR;
2258 			if (softc->error_inject != 0) {
2259 				bp->bio_error = softc->error_inject;
2260 				bp->bio_resid = bp->bio_bcount;
2261 				bp->bio_flags |= BIO_ERROR;
2262 				softc->error_inject = 0;
2263 			}
2264 
2265 		}
2266 
2267 		/*
2268 		 * Block out any asyncronous callbacks
2269 		 * while we touch the pending ccb list.
2270 		 */
2271 		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2272 		softc->outstanding_cmds--;
2273 		if (softc->outstanding_cmds == 0)
2274 			softc->flags |= DA_FLAG_WENT_IDLE;
2275 
2276 		if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
2277 			xpt_print(periph->path, "oustanding %d\n",
2278 				  softc->outstanding_cmds);
2279 		}
2280 
2281 		if (state == DA_CCB_DELETE) {
2282 			while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2283 			    != NULL) {
2284 				bp1->bio_resid = bp->bio_resid;
2285 				bp1->bio_error = bp->bio_error;
2286 				if (bp->bio_flags & BIO_ERROR)
2287 					bp1->bio_flags |= BIO_ERROR;
2288 				biodone(bp1);
2289 			}
2290 			softc->delete_running = 0;
2291 			if (bp != NULL)
2292 				biodone(bp);
2293 			daschedule(periph);
2294 		} else if (bp != NULL)
2295 			biodone(bp);
2296 		break;
2297 	}
2298 	case DA_CCB_PROBE:
2299 	case DA_CCB_PROBE2:
2300 	{
2301 		struct	   scsi_read_capacity_data *rdcap;
2302 		struct     scsi_read_capacity_data_long *rcaplong;
2303 		char	   announce_buf[80];
2304 
2305 		rdcap = NULL;
2306 		rcaplong = NULL;
2307 		if (state == DA_CCB_PROBE)
2308 			rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
2309 		else
2310 			rcaplong = (struct scsi_read_capacity_data_long *)
2311 				csio->data_ptr;
2312 
2313 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2314 			struct disk_params *dp;
2315 			uint32_t block_size;
2316 			uint64_t maxsector;
2317 			u_int lbppbe;	/* LB per physical block exponent. */
2318 			u_int lalba;	/* Lowest aligned LBA. */
2319 
2320 			if (state == DA_CCB_PROBE) {
2321 				block_size = scsi_4btoul(rdcap->length);
2322 				maxsector = scsi_4btoul(rdcap->addr);
2323 				lbppbe = 0;
2324 				lalba = 0;
2325 
2326 				/*
2327 				 * According to SBC-2, if the standard 10
2328 				 * byte READ CAPACITY command returns 2^32,
2329 				 * we should issue the 16 byte version of
2330 				 * the command, since the device in question
2331 				 * has more sectors than can be represented
2332 				 * with the short version of the command.
2333 				 */
2334 				if (maxsector == 0xffffffff) {
2335 					softc->state = DA_STATE_PROBE2;
2336 					free(rdcap, M_SCSIDA);
2337 					xpt_release_ccb(done_ccb);
2338 					xpt_schedule(periph, priority);
2339 					return;
2340 				}
2341 			} else {
2342 				block_size = scsi_4btoul(rcaplong->length);
2343 				maxsector = scsi_8btou64(rcaplong->addr);
2344 				lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2345 				lalba = scsi_2btoul(rcaplong->lalba_lbp);
2346 			}
2347 
2348 			/*
2349 			 * Because GEOM code just will panic us if we
2350 			 * give them an 'illegal' value we'll avoid that
2351 			 * here.
2352 			 */
2353 			if (block_size == 0 && maxsector == 0) {
2354 				snprintf(announce_buf, sizeof(announce_buf),
2355 				        "0MB (no media?)");
2356 			} else if (block_size >= MAXPHYS || block_size == 0) {
2357 				xpt_print(periph->path,
2358 				    "unsupportable block size %ju\n",
2359 				    (uintmax_t) block_size);
2360 				announce_buf[0] = '\0';
2361 				cam_periph_invalidate(periph);
2362 			} else {
2363 				/*
2364 				 * We pass rcaplong into dasetgeom(),
2365 				 * because it will only use it if it is
2366 				 * non-NULL.
2367 				 */
2368 				dasetgeom(periph, block_size, maxsector,
2369 					  rcaplong, sizeof(*rcaplong));
2370 				if ((lalba & SRC16_LBPME_A)
2371 				 && softc->delete_method == DA_DELETE_NONE)
2372 					dadeletemethodset(softc, DA_DELETE_UNMAP);
2373 				dp = &softc->params;
2374 				snprintf(announce_buf, sizeof(announce_buf),
2375 				        "%juMB (%ju %u byte sectors: %dH %dS/T "
2376                                         "%dC)", (uintmax_t)
2377 	                                (((uintmax_t)dp->secsize *
2378 				        dp->sectors) / (1024*1024)),
2379 			                (uintmax_t)dp->sectors,
2380 				        dp->secsize, dp->heads,
2381                                         dp->secs_per_track, dp->cylinders);
2382 			}
2383 		} else {
2384 			int	error;
2385 
2386 			announce_buf[0] = '\0';
2387 
2388 			/*
2389 			 * Retry any UNIT ATTENTION type errors.  They
2390 			 * are expected at boot.
2391 			 */
2392 			error = daerror(done_ccb, CAM_RETRY_SELTO,
2393 					SF_RETRY_UA|SF_NO_PRINT);
2394 			if (error == ERESTART) {
2395 				/*
2396 				 * A retry was scheuled, so
2397 				 * just return.
2398 				 */
2399 				return;
2400 			} else if (error != 0) {
2401 				int asc, ascq;
2402 				int sense_key, error_code;
2403 				int have_sense;
2404 				cam_status status;
2405 				struct ccb_getdev cgd;
2406 
2407 				/* Don't wedge this device's queue */
2408 				status = done_ccb->ccb_h.status;
2409 				if ((status & CAM_DEV_QFRZN) != 0)
2410 					cam_release_devq(done_ccb->ccb_h.path,
2411 							 /*relsim_flags*/0,
2412 							 /*reduction*/0,
2413 							 /*timeout*/0,
2414 							 /*getcount_only*/0);
2415 
2416 
2417 				xpt_setup_ccb(&cgd.ccb_h,
2418 					      done_ccb->ccb_h.path,
2419 					      CAM_PRIORITY_NORMAL);
2420 				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2421 				xpt_action((union ccb *)&cgd);
2422 
2423 				if (scsi_extract_sense_ccb(done_ccb,
2424 				    &error_code, &sense_key, &asc, &ascq))
2425 					have_sense = TRUE;
2426 				else
2427 					have_sense = FALSE;
2428 
2429 				/*
2430 				 * If we tried READ CAPACITY(16) and failed,
2431 				 * fallback to READ CAPACITY(10).
2432 				 */
2433 				if ((state == DA_CCB_PROBE2) &&
2434 				    (softc->flags & DA_FLAG_CAN_RC16) &&
2435 				    (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2436 					CAM_REQ_INVALID) ||
2437 				     ((have_sense) &&
2438 				      (error_code == SSD_CURRENT_ERROR) &&
2439 				      (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2440 					softc->flags &= ~DA_FLAG_CAN_RC16;
2441 					softc->state = DA_STATE_PROBE;
2442 					free(rdcap, M_SCSIDA);
2443 					xpt_release_ccb(done_ccb);
2444 					xpt_schedule(periph, priority);
2445 					return;
2446 				} else
2447 				/*
2448 				 * Attach to anything that claims to be a
2449 				 * direct access or optical disk device,
2450 				 * as long as it doesn't return a "Logical
2451 				 * unit not supported" (0x25) error.
2452 				 */
2453 				if ((have_sense) && (asc != 0x25)
2454 				 && (error_code == SSD_CURRENT_ERROR)) {
2455 					const char *sense_key_desc;
2456 					const char *asc_desc;
2457 
2458 					scsi_sense_desc(sense_key, asc, ascq,
2459 							&cgd.inq_data,
2460 							&sense_key_desc,
2461 							&asc_desc);
2462 					snprintf(announce_buf,
2463 					    sizeof(announce_buf),
2464 						"Attempt to query device "
2465 						"size failed: %s, %s",
2466 						sense_key_desc,
2467 						asc_desc);
2468 				} else {
2469 					if (have_sense)
2470 						scsi_sense_print(
2471 							&done_ccb->csio);
2472 					else {
2473 						xpt_print(periph->path,
2474 						    "got CAM status %#x\n",
2475 						    done_ccb->ccb_h.status);
2476 					}
2477 
2478 					xpt_print(periph->path, "fatal error, "
2479 					    "failed to attach to device\n");
2480 
2481 					/*
2482 					 * Free up resources.
2483 					 */
2484 					cam_periph_invalidate(periph);
2485 				}
2486 			}
2487 		}
2488 		free(csio->data_ptr, M_SCSIDA);
2489 		if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
2490 			/*
2491 			 * Create our sysctl variables, now that we know
2492 			 * we have successfully attached.
2493 			 */
2494 			/* increase the refcount */
2495 			if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2496 				taskqueue_enqueue(taskqueue_thread,
2497 						  &softc->sysctl_task);
2498 				xpt_announce_periph(periph, announce_buf);
2499 			} else {
2500 				xpt_print(periph->path, "fatal error, "
2501 				    "could not acquire reference count\n");
2502 			}
2503 		}
2504 		/*
2505 		 * Since our peripheral may be invalidated by an error
2506 		 * above or an external event, we must release our CCB
2507 		 * before releasing the probe lock on the peripheral.
2508 		 * The peripheral will only go away once the last lock
2509 		 * is removed, and we need it around for the CCB release
2510 		 * operation.
2511 		 */
2512 		xpt_release_ccb(done_ccb);
2513 		softc->state = DA_STATE_NORMAL;
2514 		daschedule(periph);
2515 		wakeup(&softc->disk->d_mediasize);
2516 		if ((softc->flags & DA_FLAG_PROBED) == 0) {
2517 			softc->flags |= DA_FLAG_PROBED;
2518 			cam_periph_unhold(periph);
2519 		} else
2520 			cam_periph_release_locked(periph);
2521 		return;
2522 	}
2523 	case DA_CCB_WAITING:
2524 	{
2525 		/* Caller will release the CCB */
2526 		wakeup(&done_ccb->ccb_h.cbfcnp);
2527 		return;
2528 	}
2529 	case DA_CCB_DUMP:
2530 		/* No-op.  We're polling */
2531 		return;
2532 	case DA_CCB_TUR:
2533 	{
2534 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2535 
2536 			if (daerror(done_ccb, CAM_RETRY_SELTO,
2537 			    SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
2538 			    ERESTART)
2539 				return;
2540 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2541 				cam_release_devq(done_ccb->ccb_h.path,
2542 						 /*relsim_flags*/0,
2543 						 /*reduction*/0,
2544 						 /*timeout*/0,
2545 						 /*getcount_only*/0);
2546 		}
2547 		xpt_release_ccb(done_ccb);
2548 		cam_periph_release_locked(periph);
2549 		return;
2550 	}
2551 	default:
2552 		break;
2553 	}
2554 	xpt_release_ccb(done_ccb);
2555 }
2556 
2557 static void
2558 dareprobe(struct cam_periph *periph)
2559 {
2560 	struct da_softc	  *softc;
2561 	cam_status status;
2562 
2563 	softc = (struct da_softc *)periph->softc;
2564 
2565 	/* Probe in progress; don't interfere. */
2566 	if ((softc->flags & DA_FLAG_PROBED) == 0)
2567 		return;
2568 
2569 	status = cam_periph_acquire(periph);
2570 	KASSERT(status == CAM_REQ_CMP,
2571 	    ("dareprobe: cam_periph_acquire failed"));
2572 
2573 	if (softc->flags & DA_FLAG_CAN_RC16)
2574 		softc->state = DA_STATE_PROBE2;
2575 	else
2576 		softc->state = DA_STATE_PROBE;
2577 
2578 	xpt_schedule(periph, CAM_PRIORITY_DEV);
2579 }
2580 
2581 static int
2582 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2583 {
2584 	struct da_softc	  *softc;
2585 	struct cam_periph *periph;
2586 	int error, error_code, sense_key, asc, ascq;
2587 
2588 	periph = xpt_path_periph(ccb->ccb_h.path);
2589 	softc = (struct da_softc *)periph->softc;
2590 
2591  	/*
2592 	 * Automatically detect devices that do not support
2593  	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2594  	 */
2595 	error = 0;
2596 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2597 		error = cmd6workaround(ccb);
2598 	} else if (scsi_extract_sense_ccb(ccb,
2599 	    &error_code, &sense_key, &asc, &ascq)) {
2600 		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2601  			error = cmd6workaround(ccb);
2602 		/*
2603 		 * If the target replied with CAPACITY DATA HAS CHANGED UA,
2604 		 * query the capacity and notify upper layers.
2605 		 */
2606 		else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
2607 		    asc == 0x2A && ascq == 0x09) {
2608 			xpt_print(periph->path, "capacity data has changed\n");
2609 			dareprobe(periph);
2610 			sense_flags |= SF_NO_PRINT;
2611 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
2612 		    asc == 0x28 && ascq == 0x00)
2613 			disk_media_changed(softc->disk, M_NOWAIT);
2614 		else if (sense_key == SSD_KEY_NOT_READY &&
2615 		    asc == 0x3a && (softc->flags & DA_FLAG_SAW_MEDIA)) {
2616 			softc->flags &= ~DA_FLAG_SAW_MEDIA;
2617 			disk_media_gone(softc->disk, M_NOWAIT);
2618 		}
2619 	}
2620 	if (error == ERESTART)
2621 		return (ERESTART);
2622 
2623 	/*
2624 	 * XXX
2625 	 * Until we have a better way of doing pack validation,
2626 	 * don't treat UAs as errors.
2627 	 */
2628 	sense_flags |= SF_RETRY_UA;
2629 	return(cam_periph_error(ccb, cam_flags, sense_flags,
2630 				&softc->saved_ccb));
2631 }
2632 
2633 static void
2634 damediapoll(void *arg)
2635 {
2636 	struct cam_periph *periph = arg;
2637 	struct da_softc *softc = periph->softc;
2638 
2639 	if (!softc->tur && softc->outstanding_cmds == 0) {
2640 		if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2641 			softc->tur = 1;
2642 			daschedule(periph);
2643 		}
2644 	}
2645 	/* Queue us up again */
2646 	if (da_poll_period != 0)
2647 		callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
2648 }
2649 
2650 static void
2651 daprevent(struct cam_periph *periph, int action)
2652 {
2653 	struct	da_softc *softc;
2654 	union	ccb *ccb;
2655 	int	error;
2656 
2657 	softc = (struct da_softc *)periph->softc;
2658 
2659 	if (((action == PR_ALLOW)
2660 	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2661 	 || ((action == PR_PREVENT)
2662 	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2663 		return;
2664 	}
2665 
2666 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2667 
2668 	scsi_prevent(&ccb->csio,
2669 		     /*retries*/1,
2670 		     /*cbcfp*/dadone,
2671 		     MSG_SIMPLE_Q_TAG,
2672 		     action,
2673 		     SSD_FULL_SIZE,
2674 		     5000);
2675 
2676 	error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
2677 	    SF_RETRY_UA | SF_QUIET_IR, softc->disk->d_devstat);
2678 
2679 	if (error == 0) {
2680 		if (action == PR_ALLOW)
2681 			softc->flags &= ~DA_FLAG_PACK_LOCKED;
2682 		else
2683 			softc->flags |= DA_FLAG_PACK_LOCKED;
2684 	}
2685 
2686 	xpt_release_ccb(ccb);
2687 }
2688 
2689 static void
2690 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2691 	  struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
2692 {
2693 	struct ccb_calc_geometry ccg;
2694 	struct da_softc *softc;
2695 	struct disk_params *dp;
2696 	u_int lbppbe, lalba;
2697 	int error;
2698 
2699 	softc = (struct da_softc *)periph->softc;
2700 
2701 	dp = &softc->params;
2702 	dp->secsize = block_len;
2703 	dp->sectors = maxsector + 1;
2704 	if (rcaplong != NULL) {
2705 		lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2706 		lalba = scsi_2btoul(rcaplong->lalba_lbp);
2707 		lalba &= SRC16_LALBA_A;
2708 	} else {
2709 		lbppbe = 0;
2710 		lalba = 0;
2711 	}
2712 
2713 	if (lbppbe > 0) {
2714 		dp->stripesize = block_len << lbppbe;
2715 		dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2716 		    dp->stripesize;
2717 	} else if (softc->quirks & DA_Q_4K) {
2718 		dp->stripesize = 4096;
2719 		dp->stripeoffset = 0;
2720 	} else {
2721 		dp->stripesize = 0;
2722 		dp->stripeoffset = 0;
2723 	}
2724 	/*
2725 	 * Have the controller provide us with a geometry
2726 	 * for this disk.  The only time the geometry
2727 	 * matters is when we boot and the controller
2728 	 * is the only one knowledgeable enough to come
2729 	 * up with something that will make this a bootable
2730 	 * device.
2731 	 */
2732 	xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2733 	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2734 	ccg.block_size = dp->secsize;
2735 	ccg.volume_size = dp->sectors;
2736 	ccg.heads = 0;
2737 	ccg.secs_per_track = 0;
2738 	ccg.cylinders = 0;
2739 	xpt_action((union ccb*)&ccg);
2740 	if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2741 		/*
2742 		 * We don't know what went wrong here- but just pick
2743 		 * a geometry so we don't have nasty things like divide
2744 		 * by zero.
2745 		 */
2746 		dp->heads = 255;
2747 		dp->secs_per_track = 255;
2748 		dp->cylinders = dp->sectors / (255 * 255);
2749 		if (dp->cylinders == 0) {
2750 			dp->cylinders = 1;
2751 		}
2752 	} else {
2753 		dp->heads = ccg.heads;
2754 		dp->secs_per_track = ccg.secs_per_track;
2755 		dp->cylinders = ccg.cylinders;
2756 	}
2757 
2758 	/*
2759 	 * If the user supplied a read capacity buffer, and if it is
2760 	 * different than the previous buffer, update the data in the EDT.
2761 	 * If it's the same, we don't bother.  This avoids sending an
2762 	 * update every time someone opens this device.
2763 	 */
2764 	if ((rcaplong != NULL)
2765 	 && (bcmp(rcaplong, &softc->rcaplong,
2766 		  min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
2767 		struct ccb_dev_advinfo cdai;
2768 
2769 		xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2770 		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
2771 		cdai.buftype = CDAI_TYPE_RCAPLONG;
2772 		cdai.flags |= CDAI_FLAG_STORE;
2773 		cdai.bufsiz = rcap_len;
2774 		cdai.buf = (uint8_t *)rcaplong;
2775 		xpt_action((union ccb *)&cdai);
2776 		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
2777 			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
2778 		if (cdai.ccb_h.status != CAM_REQ_CMP) {
2779 			xpt_print(periph->path, "%s: failed to set read "
2780 				  "capacity advinfo\n", __func__);
2781 			/* Use cam_error_print() to decode the status */
2782 			cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
2783 					CAM_EPF_ALL);
2784 		} else {
2785 			bcopy(rcaplong, &softc->rcaplong,
2786 			      min(sizeof(softc->rcaplong), rcap_len));
2787 		}
2788 	}
2789 
2790 	softc->disk->d_sectorsize = softc->params.secsize;
2791 	softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
2792 	softc->disk->d_stripesize = softc->params.stripesize;
2793 	softc->disk->d_stripeoffset = softc->params.stripeoffset;
2794 	/* XXX: these are not actually "firmware" values, so they may be wrong */
2795 	softc->disk->d_fwsectors = softc->params.secs_per_track;
2796 	softc->disk->d_fwheads = softc->params.heads;
2797 	softc->disk->d_devstat->block_size = softc->params.secsize;
2798 	softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2799 	if (softc->delete_method > DA_DELETE_DISABLE)
2800 		softc->disk->d_flags |= DISKFLAG_CANDELETE;
2801 	else
2802 		softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2803 
2804 	error = disk_resize(softc->disk, M_NOWAIT);
2805 	if (error != 0)
2806 		xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
2807 }
2808 
2809 static void
2810 dasendorderedtag(void *arg)
2811 {
2812 	struct da_softc *softc = arg;
2813 
2814 	if (da_send_ordered) {
2815 		if ((softc->ordered_tag_count == 0)
2816 		 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2817 			softc->flags |= DA_FLAG_NEED_OTAG;
2818 		}
2819 		if (softc->outstanding_cmds > 0)
2820 			softc->flags &= ~DA_FLAG_WENT_IDLE;
2821 
2822 		softc->ordered_tag_count = 0;
2823 	}
2824 	/* Queue us up again */
2825 	callout_reset(&softc->sendordered_c,
2826 	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2827 	    dasendorderedtag, softc);
2828 }
2829 
2830 /*
2831  * Step through all DA peripheral drivers, and if the device is still open,
2832  * sync the disk cache to physical media.
2833  */
2834 static void
2835 dashutdown(void * arg, int howto)
2836 {
2837 	struct cam_periph *periph;
2838 	struct da_softc *softc;
2839 	int error;
2840 
2841 	TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2842 		union ccb ccb;
2843 
2844 		cam_periph_lock(periph);
2845 		softc = (struct da_softc *)periph->softc;
2846 
2847 		/*
2848 		 * We only sync the cache if the drive is still open, and
2849 		 * if the drive is capable of it..
2850 		 */
2851 		if (((softc->flags & DA_FLAG_OPEN) == 0)
2852 		 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2853 			cam_periph_unlock(periph);
2854 			continue;
2855 		}
2856 
2857 		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2858 
2859 		ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2860 		scsi_synchronize_cache(&ccb.csio,
2861 				       /*retries*/0,
2862 				       /*cbfcnp*/dadone,
2863 				       MSG_SIMPLE_Q_TAG,
2864 				       /*begin_lba*/0, /* whole disk */
2865 				       /*lb_count*/0,
2866 				       SSD_FULL_SIZE,
2867 				       60 * 60 * 1000);
2868 
2869 		xpt_polled_action(&ccb);
2870 
2871 		error = cam_periph_error(&ccb,
2872 		    0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
2873 		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2874 			cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
2875 			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
2876 		if (error != 0)
2877 			xpt_print(periph->path, "Synchronize cache failed\n");
2878 		cam_periph_unlock(periph);
2879 	}
2880 }
2881 
2882 #else /* !_KERNEL */
2883 
2884 /*
2885  * XXX This is only left out of the kernel build to silence warnings.  If,
2886  * for some reason this function is used in the kernel, the ifdefs should
2887  * be moved so it is included both in the kernel and userland.
2888  */
2889 void
2890 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2891 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
2892 		 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2893 		 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2894 		 u_int32_t timeout)
2895 {
2896 	struct scsi_format_unit *scsi_cmd;
2897 
2898 	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2899 	scsi_cmd->opcode = FORMAT_UNIT;
2900 	scsi_cmd->byte2 = byte2;
2901 	scsi_ulto2b(ileave, scsi_cmd->interleave);
2902 
2903 	cam_fill_csio(csio,
2904 		      retries,
2905 		      cbfcnp,
2906 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2907 		      tag_action,
2908 		      data_ptr,
2909 		      dxfer_len,
2910 		      sense_len,
2911 		      sizeof(*scsi_cmd),
2912 		      timeout);
2913 }
2914 
2915 #endif /* _KERNEL */
2916