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