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