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