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