xref: /freebsd/sys/cam/scsi/scsi_da.c (revision b52f49a9a0f22207ad5130ad8faba08de3ed23d8)
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 #ifdef _KERNEL
33 #include "opt_hw_wdog.h"
34 #endif /* _KERNEL */
35 
36 #include <sys/param.h>
37 
38 #ifdef _KERNEL
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bio.h>
42 #include <sys/sysctl.h>
43 #endif /* _KERNEL */
44 
45 #include <sys/devicestat.h>
46 #include <sys/conf.h>
47 #include <sys/eventhandler.h>
48 #include <sys/malloc.h>
49 #include <sys/cons.h>
50 
51 #include <machine/md_var.h>
52 
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 
56 #include <geom/geom_disk.h>
57 
58 #ifndef _KERNEL
59 #include <stdio.h>
60 #include <string.h>
61 #endif /* _KERNEL */
62 
63 #include <cam/cam.h>
64 #include <cam/cam_ccb.h>
65 #include <cam/cam_periph.h>
66 #include <cam/cam_xpt_periph.h>
67 
68 #include <cam/scsi/scsi_message.h>
69 
70 #ifndef _KERNEL
71 #include <cam/scsi/scsi_da.h>
72 #endif /* !_KERNEL */
73 
74 #ifdef _KERNEL
75 typedef enum {
76 	DA_STATE_PROBE,
77 	DA_STATE_PROBE2,
78 	DA_STATE_NORMAL
79 } da_state;
80 
81 typedef enum {
82 	DA_FLAG_PACK_INVALID	= 0x001,
83 	DA_FLAG_NEW_PACK	= 0x002,
84 	DA_FLAG_PACK_LOCKED	= 0x004,
85 	DA_FLAG_PACK_REMOVABLE	= 0x008,
86 	DA_FLAG_TAGGED_QUEUING	= 0x010,
87 	DA_FLAG_NEED_OTAG	= 0x020,
88 	DA_FLAG_WENT_IDLE	= 0x040,
89 	DA_FLAG_RETRY_UA	= 0x080,
90 	DA_FLAG_OPEN		= 0x100
91 } da_flags;
92 
93 typedef enum {
94 	DA_Q_NONE		= 0x00,
95 	DA_Q_NO_SYNC_CACHE	= 0x01,
96 	DA_Q_NO_6_BYTE		= 0x02
97 } da_quirks;
98 
99 typedef enum {
100 	DA_CCB_PROBE		= 0x01,
101 	DA_CCB_PROBE2		= 0x02,
102 	DA_CCB_BUFFER_IO	= 0x03,
103 	DA_CCB_WAITING		= 0x04,
104 	DA_CCB_DUMP		= 0x05,
105 	DA_CCB_TYPE_MASK	= 0x0F,
106 	DA_CCB_RETRY_UA		= 0x10
107 } da_ccb_state;
108 
109 /* Offsets into our private area for storing information */
110 #define ccb_state	ppriv_field0
111 #define ccb_bp		ppriv_ptr1
112 
113 struct disk_params {
114 	u_int8_t  heads;
115 	u_int32_t cylinders;
116 	u_int8_t  secs_per_track;
117 	u_int32_t secsize;	/* Number of bytes/sector */
118 	u_int64_t sectors;	/* total number sectors */
119 };
120 
121 struct da_softc {
122 	struct	 bio_queue_head bio_queue;
123 	SLIST_ENTRY(da_softc) links;
124 	LIST_HEAD(, ccb_hdr) pending_ccbs;
125 	da_state state;
126 	da_flags flags;
127 	da_quirks quirks;
128 	int	 minimum_cmd_size;
129 	int	 ordered_tag_count;
130 	int	 outstanding_cmds;
131 	struct	 disk_params params;
132 	struct	 disk disk;
133 	union	 ccb saved_ccb;
134 	struct sysctl_ctx_list	sysctl_ctx;
135 	struct sysctl_oid	*sysctl_tree;
136 };
137 
138 struct da_quirk_entry {
139 	struct scsi_inquiry_pattern inq_pat;
140 	da_quirks quirks;
141 };
142 
143 static const char quantum[] = "QUANTUM";
144 static const char microp[] = "MICROP";
145 
146 static struct da_quirk_entry da_quirk_table[] =
147 {
148 	/*
149 	 * Logitec USB/Firewire LHD-P30FU
150 	 */
151 	{
152 		/* USB part */
153 		{T_DIRECT, SIP_MEDIA_FIXED, "HITACHI_", "DK23DA*", "*"},
154 		/*quirks*/ DA_Q_NO_6_BYTE
155 	},
156 	{
157 		/* Firewire part */
158 		{T_DIRECT, SIP_MEDIA_FIXED, "LSILogic", "SYM13FW*", "*"},
159 		/*quirks*/ DA_Q_NO_6_BYTE
160 	},
161 	{
162 		/*
163 		 * Fujitsu M2513A MO drives.
164 		 * Tested devices: M2513A2 firmware versions 1200 & 1300.
165 		 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
166 		 * Reported by: W.Scholten <whs@xs4all.nl>
167 		 */
168 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
169 		/*quirks*/ DA_Q_NO_SYNC_CACHE
170 	},
171 	{
172 		/* See above. */
173 		{T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
174 		/*quirks*/ DA_Q_NO_SYNC_CACHE
175 	},
176 	{
177 		/*
178 		 * This particular Fujitsu drive doesn't like the
179 		 * synchronize cache command.
180 		 * Reported by: Tom Jackson <toj@gorilla.net>
181 		 */
182 		{T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
183 		/*quirks*/ DA_Q_NO_SYNC_CACHE
184 
185 	},
186 	{
187 		/*
188 		 * This drive doesn't like the synchronize cache command
189 		 * either.  Reported by: Matthew Jacob <mjacob@feral.com>
190 		 * in NetBSD PR kern/6027, August 24, 1998.
191 		 */
192 		{T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
193 		/*quirks*/ DA_Q_NO_SYNC_CACHE
194 	},
195 	{
196 		/*
197 		 * This drive doesn't like the synchronize cache command
198 		 * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
199 		 * (PR 8882).
200 		 */
201 		{T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
202 		/*quirks*/ DA_Q_NO_SYNC_CACHE
203 	},
204 	{
205 		/*
206 		 * Doesn't like the synchronize cache command.
207 		 * Reported by: Blaz Zupan <blaz@gold.amis.net>
208 		 */
209 		{T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
210 		/*quirks*/ DA_Q_NO_SYNC_CACHE
211 	},
212 	{
213 		/*
214 		 * Doesn't like the synchronize cache command.
215 		 */
216 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
217 		/*quirks*/ DA_Q_NO_SYNC_CACHE
218 	},
219 	{
220 		/*
221 		 * Doesn't like the synchronize cache command.
222 		 */
223 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
224 		/*quirks*/ DA_Q_NO_SYNC_CACHE
225 	},
226 	{
227 		/*
228 		 * Doesn't work correctly with 6 byte reads/writes.
229 		 * Returns illegal request, and points to byte 9 of the
230 		 * 6-byte CDB.
231 		 * Reported by:  Adam McDougall <bsdx@spawnet.com>
232 		 */
233 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
234 		/*quirks*/ DA_Q_NO_6_BYTE
235 	},
236 	{
237 		/*
238 		 * See above.
239 		 */
240 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
241 		/*quirks*/ DA_Q_NO_6_BYTE
242 	},
243 
244 	/* Below a list of quirks for USB devices supported by umass. */
245 	{
246 		/*
247 		 * This USB floppy drive uses the UFI command set. This
248 		 * command set is a derivative of the ATAPI command set and
249 		 * does not support READ_6 commands only READ_10. It also does
250 		 * not support sync cache (0x35).
251 		 */
252 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
253 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
254 	},
255 	{
256 		/* Another USB floppy */
257 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
258 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
259 	},
260 	{
261 		/*
262 		 * Sony Memory Stick adapter MSAC-US1 and
263 		 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01).
264 		 * Make all sony MS* products use this quirk.
265 		 */
266 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
267 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
268 	},
269 	{
270 		/*
271 		 * Sony Memory Stick adapter for the CLIE series
272 		 * of PalmOS PDA's
273 		 */
274 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"},
275 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
276 	},
277 	{
278 		/*
279 		 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
280 		 */
281 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
282 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
283 	},
284 	{
285 		/*
286 		 * Maxtor 3000LE USB Drive
287 		 */
288 		{T_DIRECT, SIP_MEDIA_FIXED, "MAXTOR*", "K040H2*", "*"},
289 		/*quirks*/ DA_Q_NO_6_BYTE
290 	},
291 	{
292 		/*
293 		 * LaCie USB drive, among others
294 		 */
295 		{T_DIRECT, SIP_MEDIA_FIXED, "Maxtor*", "D080H4*", "*"},
296 		/*quirks*/ DA_Q_NO_6_BYTE
297 	},
298 	{
299 		{T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "MCF3064AP", "*"},
300 		/*quirks*/ DA_Q_NO_6_BYTE
301 	},
302 	{
303 		/*
304 		 * Microtech USB CameraMate
305 		 */
306 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB    Compact*",
307 		 "Compact Flash*", "*"},
308 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
309 	},
310 	{
311 		/*
312 		 * The vendor, product and version strings coming from the
313 		 * controller are null terminated instead of being padded with
314 		 * spaces. The trailing wildcard character '*' is required.
315 		 */
316 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
317 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
318 	},
319 	{
320 		/*
321 		 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
322 		 */
323 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"},
324 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
325 	},
326 	{
327 		/*
328 		 * Olympus digital cameras (D-370)
329 		 */
330 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "D-*", "*"},
331 		/*quirks*/ DA_Q_NO_6_BYTE
332 	},
333 	{
334 		/*
335 		 * Olympus digital cameras (E-100RS, E-10).
336 		 */
337 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"},
338 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
339 	},
340 	{
341 		/*
342 		 * KingByte Pen Drives
343 		 */
344 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"},
345 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
346  	},
347  	{
348 		/*
349 		 * FujiFilm Camera
350 		 */
351  		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
352 		 "USB-DRIVEUNIT", "*"},
353  		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
354  	},
355 	{
356 		/*
357 		 * Nikon Coolpix E775/E995 Cameras
358 		 */
359 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "NIKON", "NIKON DSC E*", "*"},
360 		/*quirks*/ DA_Q_NO_6_BYTE
361 	},
362 	{
363 		/*
364 		 * Nikon Coolpix E885 Camera
365 		 */
366 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Nikon", "Digital Camera", "*"},
367 		/*quirks*/ DA_Q_NO_6_BYTE
368 	},
369 	{
370 		/*
371 		 * SimpleTech FlashLink UCF-100
372 		 */
373 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "OEI-USB", "CompactFlash", "*"},
374 		/*quirks*/ DA_Q_NO_6_BYTE
375 	},
376 	{
377 		/*
378 		 * Minolta Dimage 2330
379 		 */
380 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DIMAGE 2330*", "*"},
381 		/*quirks*/ DA_Q_NO_6_BYTE
382 	},
383 	{
384 		/*
385 		 * Minolta Dimage E203
386 		 */
387 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
388 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
389 	},
390 	{
391 		/*
392 		 * DIVA USB Mp3 Player.
393 		 * PR: kern/33638
394 		 */
395 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "DIVA USB", "Media Reader","*"},
396 		/*quirks*/ DA_Q_NO_6_BYTE
397 	},
398 	{
399 		/*
400 		 * Daisy Technology PhotoClip USB Camera
401 		 */
402 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Digital", "World   DMC","*"},
403 		/*quirks*/ DA_Q_NO_6_BYTE
404 	},
405 	{
406 		/*
407 		 * Apacer HandyDrive
408 		 * PR: kern/43627
409 		 */
410 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
411 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
412 	},
413 	{
414 		/*
415 		 * Daisy Technology PhotoClip on Zoran chip
416 		 * PR: kern/43580
417 		 */
418 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
419 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
420 	},
421 	{
422 		/*
423 		 * HP 315 Digital Camera
424 		 * PR: kern/41010
425 		 */
426 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "HP", "USB CAMERA", "*"},
427 		/*quirks*/ DA_Q_NO_6_BYTE
428 	},
429 	{
430 		/*
431 		 * Fujitsu-Siemens Memorybird pen drive
432 		 * PR: kern/34712
433 		 */
434 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Fujitsu", "Memorybird", "*"},
435 		/*quirks*/ DA_Q_NO_6_BYTE
436 	},
437 	{
438 		/*
439 		 * Sony USB Key-Storage
440 		 * PR: kern/46386
441 		 */
442 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
443 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
444 	},
445 	{
446 		/*
447 		 * Lexar Media Jumpdrive
448 		 * PR: kern/47006
449 		 */
450 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "LEXAR", "DIGITAL FILM", "*"},
451 		/*quirks*/ DA_Q_NO_6_BYTE
452 	},
453 	{
454 		/*
455 		 * Pentax USB Optio 230 camera
456 		 * PR: kern/46369
457 		 */
458 		{T_DIRECT, SIP_MEDIA_REMOVABLE,
459 		 "PENTAX", "DIGITAL_CAMERA", "*"},
460 		/*quirks*/ DA_Q_NO_6_BYTE
461 	},
462 	{
463 		/*
464 		 * Casio QV-R3 USB camera (uses Pentax chip as above)
465 		 * PR: kern/46545
466 		 */
467 		{T_DIRECT, SIP_MEDIA_REMOVABLE,
468 		 "CASIO", "DIGITAL_CAMERA", "*"},
469 		/*quirks*/ DA_Q_NO_6_BYTE
470 	},
471 	{
472 		/*
473 		 * M-Systems DiskOnKey USB flash key
474 		 * PR: kern/47793
475 		 */
476 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "M-Sys", "DiskOnKey", "*"},
477 		/*quirks*/ DA_Q_NO_6_BYTE
478 	},
479 	{
480 		/*
481 		 * SanDisk ImageMate (I, II, ...) compact flash
482 		 * PR: kern/47877
483 		 */
484 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk", "ImageMate*", "*"},
485 		/*quirks*/ DA_Q_NO_6_BYTE
486 	},
487 	{
488 		/*
489 		 * Feiya "slider" dual-slot flash reader. The vendor field
490 		 * is blank so this may match other devices.
491 		 * PR: kern/50020
492 		 */
493 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "", "USB CARD READER", "*"},
494 		/*quirks*/ DA_Q_NO_6_BYTE
495 	},
496 	{
497 		/*
498 		 * SmartDisk (Mitsumi) USB floppy drive
499 		 * PR: kern/50226
500 		 */
501 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
502 		/*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
503 	},
504 	{
505 		/*
506 		 * OTi USB Flash Key
507 		 * PR: kern/51825
508 		 */
509 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "OTi", "Flash Disk", "*"},
510 		/*quirks*/ DA_Q_NO_6_BYTE
511 	}
512 };
513 
514 static	disk_strategy_t	dastrategy;
515 static	dumper_t	dadump;
516 static	periph_init_t	dainit;
517 static	void		daasync(void *callback_arg, u_int32_t code,
518 				struct cam_path *path, void *arg);
519 static	int		dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
520 static	periph_ctor_t	daregister;
521 static	periph_dtor_t	dacleanup;
522 static	periph_start_t	dastart;
523 static	periph_oninv_t	daoninvalidate;
524 static	void		dadone(struct cam_periph *periph,
525 			       union ccb *done_ccb);
526 static  int		daerror(union ccb *ccb, u_int32_t cam_flags,
527 				u_int32_t sense_flags);
528 static void		daprevent(struct cam_periph *periph, int action);
529 static int		dagetcapacity(struct cam_periph *periph);
530 static void		dasetgeom(struct cam_periph *periph, uint32_t block_len,
531 				  uint64_t maxsector);
532 static timeout_t	dasendorderedtag;
533 static void		dashutdown(void *arg, int howto);
534 
535 #ifndef DA_DEFAULT_TIMEOUT
536 #define DA_DEFAULT_TIMEOUT 60	/* Timeout in seconds */
537 #endif
538 
539 #ifndef	DA_DEFAULT_RETRY
540 #define	DA_DEFAULT_RETRY	4
541 #endif
542 
543 static int da_retry_count = DA_DEFAULT_RETRY;
544 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
545 
546 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
547             "CAM Direct Access Disk driver");
548 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
549            &da_retry_count, 0, "Normal I/O retry count");
550 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
551 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
552            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
553 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
554 
555 /*
556  * DA_ORDEREDTAG_INTERVAL determines how often, relative
557  * to the default timeout, we check to see whether an ordered
558  * tagged transaction is appropriate to prevent simple tag
559  * starvation.  Since we'd like to ensure that there is at least
560  * 1/2 of the timeout length left for a starved transaction to
561  * complete after we've sent an ordered tag, we must poll at least
562  * four times in every timeout period.  This takes care of the worst
563  * case where a starved transaction starts during an interval that
564  * meets the requirement "don't send an ordered tag" test so it takes
565  * us two intervals to determine that a tag must be sent.
566  */
567 #ifndef DA_ORDEREDTAG_INTERVAL
568 #define DA_ORDEREDTAG_INTERVAL 4
569 #endif
570 
571 static struct periph_driver dadriver =
572 {
573 	dainit, "da",
574 	TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
575 };
576 
577 PERIPHDRIVER_DECLARE(da, dadriver);
578 
579 static SLIST_HEAD(,da_softc) softc_list;
580 
581 static int
582 daopen(struct disk *dp)
583 {
584 	struct cam_periph *periph;
585 	struct da_softc *softc;
586 	int unit;
587 	int error;
588 	int s;
589 
590 	s = splsoftcam();
591 	periph = (struct cam_periph *)dp->d_drv1;
592 	unit = periph->unit_number;
593 	if (periph == NULL) {
594 		splx(s);
595 		return (ENXIO);
596 	}
597 
598 	softc = (struct da_softc *)periph->softc;
599 
600 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
601 	    ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
602 	     unit));
603 
604 	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0)
605 		return (error); /* error code from tsleep */
606 
607 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
608 		return(ENXIO);
609 	softc->flags |= DA_FLAG_OPEN;
610 
611 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
612 		/* Invalidate our pack information. */
613 		softc->flags &= ~DA_FLAG_PACK_INVALID;
614 	}
615 	splx(s);
616 
617 	error = dagetcapacity(periph);
618 
619 	if (error == 0) {
620 
621 		softc->disk.d_sectorsize = softc->params.secsize;
622 		softc->disk.d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
623 		/* XXX: these are not actually "firmware" values, so they may be wrong */
624 		softc->disk.d_fwsectors = softc->params.secs_per_track;
625 		softc->disk.d_fwheads = softc->params.heads;
626 		softc->disk.d_devstat->block_size = softc->params.secsize;
627 		softc->disk.d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
628 	}
629 
630 	if (error == 0) {
631 		if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
632 			daprevent(periph, PR_PREVENT);
633 	} else {
634 		softc->flags &= ~DA_FLAG_OPEN;
635 		cam_periph_release(periph);
636 	}
637 	cam_periph_unlock(periph);
638 	return (error);
639 }
640 
641 static int
642 daclose(struct disk *dp)
643 {
644 	struct	cam_periph *periph;
645 	struct	da_softc *softc;
646 	int	error;
647 
648 	periph = (struct cam_periph *)dp->d_drv1;
649 	if (periph == NULL)
650 		return (ENXIO);
651 
652 	softc = (struct da_softc *)periph->softc;
653 
654 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
655 		return (error); /* error code from tsleep */
656 	}
657 
658 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
659 		union	ccb *ccb;
660 
661 		ccb = cam_periph_getccb(periph, /*priority*/1);
662 
663 		scsi_synchronize_cache(&ccb->csio,
664 				       /*retries*/1,
665 				       /*cbfcnp*/dadone,
666 				       MSG_SIMPLE_Q_TAG,
667 				       /*begin_lba*/0,/* Cover the whole disk */
668 				       /*lb_count*/0,
669 				       SSD_FULL_SIZE,
670 				       5 * 60 * 1000);
671 
672 		cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
673 				  /*sense_flags*/SF_RETRY_UA,
674 				  softc->disk.d_devstat);
675 
676 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
677 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
678 			     CAM_SCSI_STATUS_ERROR) {
679 				int asc, ascq;
680 				int sense_key, error_code;
681 
682 				scsi_extract_sense(&ccb->csio.sense_data,
683 						   &error_code,
684 						   &sense_key,
685 						   &asc, &ascq);
686 				if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
687 					scsi_sense_print(&ccb->csio);
688 			} else {
689 				xpt_print_path(periph->path);
690 				printf("Synchronize cache failed, status "
691 				       "== 0x%x, scsi status == 0x%x\n",
692 				       ccb->csio.ccb_h.status,
693 				       ccb->csio.scsi_status);
694 			}
695 		}
696 
697 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
698 			cam_release_devq(ccb->ccb_h.path,
699 					 /*relsim_flags*/0,
700 					 /*reduction*/0,
701 					 /*timeout*/0,
702 					 /*getcount_only*/0);
703 
704 		xpt_release_ccb(ccb);
705 
706 	}
707 
708 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
709 		daprevent(periph, PR_ALLOW);
710 		/*
711 		 * If we've got removeable media, mark the blocksize as
712 		 * unavailable, since it could change when new media is
713 		 * inserted.
714 		 */
715 		softc->disk.d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
716 	}
717 
718 	softc->flags &= ~DA_FLAG_OPEN;
719 	cam_periph_unlock(periph);
720 	cam_periph_release(periph);
721 	return (0);
722 }
723 
724 /*
725  * Actually translate the requested transfer into one the physical driver
726  * can understand.  The transfer is described by a buf and will include
727  * only one physical transfer.
728  */
729 static void
730 dastrategy(struct bio *bp)
731 {
732 	struct cam_periph *periph;
733 	struct da_softc *softc;
734 	int    s;
735 
736 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
737 	if (periph == NULL) {
738 		biofinish(bp, NULL, ENXIO);
739 		return;
740 	}
741 	softc = (struct da_softc *)periph->softc;
742 #if 0
743 	/*
744 	 * check it's not too big a transfer for our adapter
745 	 */
746 	scsi_minphys(bp,&sd_switch);
747 #endif
748 
749 	/*
750 	 * Mask interrupts so that the pack cannot be invalidated until
751 	 * after we are in the queue.  Otherwise, we might not properly
752 	 * clean up one of the buffers.
753 	 */
754 	s = splbio();
755 
756 	/*
757 	 * If the device has been made invalid, error out
758 	 */
759 	if ((softc->flags & DA_FLAG_PACK_INVALID)) {
760 		splx(s);
761 		biofinish(bp, NULL, ENXIO);
762 		return;
763 	}
764 
765 	/*
766 	 * Place it in the queue of disk activities for this disk
767 	 */
768 	bioq_disksort(&softc->bio_queue, bp);
769 
770 	splx(s);
771 
772 	/*
773 	 * Schedule ourselves for performing the work.
774 	 */
775 	xpt_schedule(periph, /* XXX priority */1);
776 
777 	return;
778 }
779 
780 static int
781 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
782 {
783 	struct	    cam_periph *periph;
784 	struct	    da_softc *softc;
785 	u_int	    secsize;
786 	struct	    ccb_scsiio csio;
787 	struct	    disk *dp;
788 
789 	dp = arg;
790 	periph = dp->d_drv1;
791 	if (periph == NULL)
792 		return (ENXIO);
793 	softc = (struct da_softc *)periph->softc;
794 	secsize = softc->params.secsize;
795 
796 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
797 		return (ENXIO);
798 
799 	if (length > 0) {
800 		xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
801 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
802 		scsi_read_write(&csio,
803 				/*retries*/1,
804 				dadone,
805 				MSG_ORDERED_Q_TAG,
806 				/*read*/FALSE,
807 				/*byte2*/0,
808 				/*minimum_cmd_size*/ softc->minimum_cmd_size,
809 				offset / secsize,
810 				length / secsize,
811 				/*data_ptr*/(u_int8_t *) virtual,
812 				/*dxfer_len*/length,
813 				/*sense_len*/SSD_FULL_SIZE,
814 				DA_DEFAULT_TIMEOUT * 1000);
815 		xpt_polled_action((union ccb *)&csio);
816 
817 		if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
818 			printf("Aborting dump due to I/O error.\n");
819 			if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
820 			     CAM_SCSI_STATUS_ERROR)
821 				scsi_sense_print(&csio);
822 			else
823 				printf("status == 0x%x, scsi status == 0x%x\n",
824 				       csio.ccb_h.status, csio.scsi_status);
825 			return(EIO);
826 		}
827 		return(0);
828 	}
829 
830 	/*
831 	 * Sync the disk cache contents to the physical media.
832 	 */
833 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
834 
835 		xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
836 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
837 		scsi_synchronize_cache(&csio,
838 				       /*retries*/1,
839 				       /*cbfcnp*/dadone,
840 				       MSG_SIMPLE_Q_TAG,
841 				       /*begin_lba*/0,/* Cover the whole disk */
842 				       /*lb_count*/0,
843 				       SSD_FULL_SIZE,
844 				       5 * 60 * 1000);
845 		xpt_polled_action((union ccb *)&csio);
846 
847 		if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
848 			if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
849 			     CAM_SCSI_STATUS_ERROR) {
850 				int asc, ascq;
851 				int sense_key, error_code;
852 
853 				scsi_extract_sense(&csio.sense_data,
854 						   &error_code,
855 						   &sense_key,
856 						   &asc, &ascq);
857 				if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
858 					scsi_sense_print(&csio);
859 			} else {
860 				xpt_print_path(periph->path);
861 				printf("Synchronize cache failed, status "
862 				       "== 0x%x, scsi status == 0x%x\n",
863 				       csio.ccb_h.status, csio.scsi_status);
864 			}
865 		}
866 	}
867 	return (0);
868 }
869 
870 static void
871 dainit(void)
872 {
873 	cam_status status;
874 	struct cam_path *path;
875 
876 	SLIST_INIT(&softc_list);
877 
878 	/*
879 	 * Install a global async callback.  This callback will
880 	 * receive async callbacks like "new device found".
881 	 */
882 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
883 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
884 
885 	if (status == CAM_REQ_CMP) {
886 		struct ccb_setasync csa;
887 
888                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
889                 csa.ccb_h.func_code = XPT_SASYNC_CB;
890                 csa.event_enable = AC_FOUND_DEVICE;
891                 csa.callback = daasync;
892                 csa.callback_arg = NULL;
893                 xpt_action((union ccb *)&csa);
894 		status = csa.ccb_h.status;
895                 xpt_free_path(path);
896         }
897 
898 	if (status != CAM_REQ_CMP) {
899 		printf("da: Failed to attach master async callback "
900 		       "due to status 0x%x!\n", status);
901 	} else {
902 
903 		/*
904 		 * Schedule a periodic event to occasionally send an
905 		 * ordered tag to a device.
906 		 */
907 		timeout(dasendorderedtag, NULL,
908 			(DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
909 
910 		/* Register our shutdown event handler */
911 		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
912 					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
913 		    printf("dainit: shutdown event registration failed!\n");
914 	}
915 }
916 
917 static void
918 daoninvalidate(struct cam_periph *periph)
919 {
920 	int s;
921 	struct da_softc *softc;
922 	struct ccb_setasync csa;
923 
924 	softc = (struct da_softc *)periph->softc;
925 
926 	/*
927 	 * De-register any async callbacks.
928 	 */
929 	xpt_setup_ccb(&csa.ccb_h, periph->path,
930 		      /* priority */ 5);
931 	csa.ccb_h.func_code = XPT_SASYNC_CB;
932 	csa.event_enable = 0;
933 	csa.callback = daasync;
934 	csa.callback_arg = periph;
935 	xpt_action((union ccb *)&csa);
936 
937 	softc->flags |= DA_FLAG_PACK_INVALID;
938 
939 	/*
940 	 * Although the oninvalidate() routines are always called at
941 	 * splsoftcam, we need to be at splbio() here to keep the buffer
942 	 * queue from being modified while we traverse it.
943 	 */
944 	s = splbio();
945 
946 	/*
947 	 * Return all queued I/O with ENXIO.
948 	 * XXX Handle any transactions queued to the card
949 	 *     with XPT_ABORT_CCB.
950 	 */
951 	bioq_flush(&softc->bio_queue, NULL, ENXIO);
952 	splx(s);
953 
954 	SLIST_REMOVE(&softc_list, softc, da_softc, links);
955 
956 	xpt_print_path(periph->path);
957 	printf("lost device\n");
958 }
959 
960 static void
961 dacleanup(struct cam_periph *periph)
962 {
963 	struct da_softc *softc;
964 
965 	softc = (struct da_softc *)periph->softc;
966 
967 	xpt_print_path(periph->path);
968 	printf("removing device entry\n");
969 	/*
970 	 * If we can't free the sysctl tree, oh well...
971 	 */
972 	if (sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
973 		xpt_print_path(periph->path);
974 		printf("can't remove sysctl context\n");
975 	}
976 	disk_destroy(&softc->disk);
977 	free(softc, M_DEVBUF);
978 }
979 
980 static void
981 daasync(void *callback_arg, u_int32_t code,
982 	struct cam_path *path, void *arg)
983 {
984 	struct cam_periph *periph;
985 
986 	periph = (struct cam_periph *)callback_arg;
987 	switch (code) {
988 	case AC_FOUND_DEVICE:
989 	{
990 		struct ccb_getdev *cgd;
991 		cam_status status;
992 
993 		cgd = (struct ccb_getdev *)arg;
994 		if (cgd == NULL)
995 			break;
996 
997 		if (SID_TYPE(&cgd->inq_data) != T_DIRECT
998 		    && SID_TYPE(&cgd->inq_data) != T_RBC
999 		    && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1000 			break;
1001 
1002 		/*
1003 		 * Allocate a peripheral instance for
1004 		 * this device and start the probe
1005 		 * process.
1006 		 */
1007 		status = cam_periph_alloc(daregister, daoninvalidate,
1008 					  dacleanup, dastart,
1009 					  "da", CAM_PERIPH_BIO,
1010 					  cgd->ccb_h.path, daasync,
1011 					  AC_FOUND_DEVICE, cgd);
1012 
1013 		if (status != CAM_REQ_CMP
1014 		 && status != CAM_REQ_INPROG)
1015 			printf("daasync: Unable to attach to new device "
1016 				"due to status 0x%x\n", status);
1017 		break;
1018 	}
1019 	case AC_SENT_BDR:
1020 	case AC_BUS_RESET:
1021 	{
1022 		struct da_softc *softc;
1023 		struct ccb_hdr *ccbh;
1024 		int s;
1025 
1026 		softc = (struct da_softc *)periph->softc;
1027 		s = splsoftcam();
1028 		/*
1029 		 * Don't fail on the expected unit attention
1030 		 * that will occur.
1031 		 */
1032 		softc->flags |= DA_FLAG_RETRY_UA;
1033 		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1034 			ccbh->ccb_state |= DA_CCB_RETRY_UA;
1035 		splx(s);
1036 		/* FALLTHROUGH*/
1037 	}
1038 	default:
1039 		cam_periph_async(periph, code, path, arg);
1040 		break;
1041 	}
1042 }
1043 
1044 static int
1045 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1046 {
1047 	int error, value;
1048 
1049 	value = *(int *)arg1;
1050 
1051 	error = sysctl_handle_int(oidp, &value, 0, req);
1052 
1053 	if ((error != 0)
1054 	 || (req->newptr == NULL))
1055 		return (error);
1056 
1057 	/*
1058 	 * Acceptable values here are 6, 10, 12 or 16.
1059 	 */
1060 	if (value < 6)
1061 		value = 6;
1062 	else if ((value > 6)
1063 	      && (value <= 10))
1064 		value = 10;
1065 	else if ((value > 10)
1066 	      && (value <= 12))
1067 		value = 12;
1068 	else if (value > 12)
1069 		value = 16;
1070 
1071 	*(int *)arg1 = value;
1072 
1073 	return (0);
1074 }
1075 
1076 static cam_status
1077 daregister(struct cam_periph *periph, void *arg)
1078 {
1079 	int s;
1080 	struct da_softc *softc;
1081 	struct ccb_setasync csa;
1082 	struct ccb_getdev *cgd;
1083 	char tmpstr[80], tmpstr2[80];
1084 	caddr_t match;
1085 
1086 	cgd = (struct ccb_getdev *)arg;
1087 	if (periph == NULL) {
1088 		printf("daregister: periph was NULL!!\n");
1089 		return(CAM_REQ_CMP_ERR);
1090 	}
1091 
1092 	if (cgd == NULL) {
1093 		printf("daregister: no getdev CCB, can't register device\n");
1094 		return(CAM_REQ_CMP_ERR);
1095 	}
1096 
1097 	softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1098 	    M_NOWAIT|M_ZERO);
1099 
1100 	if (softc == NULL) {
1101 		printf("daregister: Unable to probe new device. "
1102 		       "Unable to allocate softc\n");
1103 		return(CAM_REQ_CMP_ERR);
1104 	}
1105 
1106 	LIST_INIT(&softc->pending_ccbs);
1107 	softc->state = DA_STATE_PROBE;
1108 	bioq_init(&softc->bio_queue);
1109 	if (SID_IS_REMOVABLE(&cgd->inq_data))
1110 		softc->flags |= DA_FLAG_PACK_REMOVABLE;
1111 	if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1112 		softc->flags |= DA_FLAG_TAGGED_QUEUING;
1113 
1114 	periph->softc = softc;
1115 
1116 	/*
1117 	 * See if this device has any quirks.
1118 	 */
1119 	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1120 			       (caddr_t)da_quirk_table,
1121 			       sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1122 			       sizeof(*da_quirk_table), scsi_inquiry_match);
1123 
1124 	if (match != NULL)
1125 		softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1126 	else
1127 		softc->quirks = DA_Q_NONE;
1128 
1129 	snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1130 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1131 	sysctl_ctx_init(&softc->sysctl_ctx);
1132 	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1133 		SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1134 		CTLFLAG_RD, 0, tmpstr);
1135 	if (softc->sysctl_tree == NULL) {
1136 		printf("daregister: unable to allocate sysctl tree\n");
1137 		free(softc, M_DEVBUF);
1138 		return (CAM_REQ_CMP_ERR);
1139 	}
1140 
1141 	/*
1142 	 * RBC devices don't have to support READ(6), only READ(10).
1143 	 */
1144 	if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1145 		softc->minimum_cmd_size = 10;
1146 	else
1147 		softc->minimum_cmd_size = 6;
1148 
1149 	/*
1150 	 * Load the user's default, if any.
1151 	 */
1152 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1153 		 periph->unit_number);
1154 	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1155 
1156 	/*
1157 	 * 6, 10, 12 and 16 are the currently permissible values.
1158 	 */
1159 	if (softc->minimum_cmd_size < 6)
1160 		softc->minimum_cmd_size = 6;
1161 	else if ((softc->minimum_cmd_size > 6)
1162 	      && (softc->minimum_cmd_size <= 10))
1163 		softc->minimum_cmd_size = 10;
1164 	else if ((softc->minimum_cmd_size > 10)
1165 	      && (softc->minimum_cmd_size <= 12))
1166 		softc->minimum_cmd_size = 12;
1167 	else if (softc->minimum_cmd_size > 12)
1168 		softc->minimum_cmd_size = 16;
1169 
1170 	/*
1171 	 * Now register the sysctl handler, so the user can the value on
1172 	 * the fly.
1173 	 */
1174 	SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1175 		OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1176 		&softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1177 		"Minimum CDB size");
1178 
1179 	/*
1180 	 * Block our timeout handler while we
1181 	 * add this softc to the dev list.
1182 	 */
1183 	s = splsoftclock();
1184 	SLIST_INSERT_HEAD(&softc_list, softc, links);
1185 	splx(s);
1186 
1187 	/*
1188 	 * Register this media as a disk
1189 	 */
1190 
1191 	softc->disk.d_open = daopen;
1192 	softc->disk.d_close = daclose;
1193 	softc->disk.d_strategy = dastrategy;
1194 	softc->disk.d_dump = dadump;
1195 	softc->disk.d_name = "da";
1196 	softc->disk.d_drv1 = periph;
1197 	softc->disk.d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */
1198 	disk_create(periph->unit_number, &softc->disk, 0, NULL, NULL);
1199 
1200 	/*
1201 	 * Add async callbacks for bus reset and
1202 	 * bus device reset calls.  I don't bother
1203 	 * checking if this fails as, in most cases,
1204 	 * the system will function just fine without
1205 	 * them and the only alternative would be to
1206 	 * not attach the device on failure.
1207 	 */
1208 	xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1209 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1210 	csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1211 	csa.callback = daasync;
1212 	csa.callback_arg = periph;
1213 	xpt_action((union ccb *)&csa);
1214 	/*
1215 	 * Lock this peripheral until we are setup.
1216 	 * This first call can't block
1217 	 */
1218 	(void)cam_periph_lock(periph, PRIBIO);
1219 	xpt_schedule(periph, /*priority*/5);
1220 
1221 	return(CAM_REQ_CMP);
1222 }
1223 
1224 static void
1225 dastart(struct cam_periph *periph, union ccb *start_ccb)
1226 {
1227 	struct da_softc *softc;
1228 
1229 	softc = (struct da_softc *)periph->softc;
1230 
1231 
1232 	switch (softc->state) {
1233 	case DA_STATE_NORMAL:
1234 	{
1235 		/* Pull a buffer from the queue and get going on it */
1236 		struct bio *bp;
1237 		int s;
1238 
1239 		/*
1240 		 * See if there is a buf with work for us to do..
1241 		 */
1242 		s = splbio();
1243 		bp = bioq_first(&softc->bio_queue);
1244 		if (periph->immediate_priority <= periph->pinfo.priority) {
1245 			CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1246 					("queuing for immediate ccb\n"));
1247 			start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1248 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1249 					  periph_links.sle);
1250 			periph->immediate_priority = CAM_PRIORITY_NONE;
1251 			splx(s);
1252 			wakeup(&periph->ccb_list);
1253 		} else if (bp == NULL) {
1254 			splx(s);
1255 			xpt_release_ccb(start_ccb);
1256 		} else {
1257 			int oldspl;
1258 			u_int8_t tag_code;
1259 
1260 			bioq_remove(&softc->bio_queue, bp);
1261 
1262 			if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1263 				softc->flags &= ~DA_FLAG_NEED_OTAG;
1264 				softc->ordered_tag_count++;
1265 				tag_code = MSG_ORDERED_Q_TAG;
1266 			} else {
1267 				tag_code = MSG_SIMPLE_Q_TAG;
1268 			}
1269 			scsi_read_write(&start_ccb->csio,
1270 					/*retries*/da_retry_count,
1271 					/*cbfcnp*/dadone,
1272 					/*tag_action*/tag_code,
1273 					/*read_op*/bp->bio_cmd == BIO_READ,
1274 					/*byte2*/0,
1275 					softc->minimum_cmd_size,
1276 					/*lba*/bp->bio_pblkno,
1277 					/*block_count*/bp->bio_bcount /
1278 					softc->params.secsize,
1279 					/*data_ptr*/ bp->bio_data,
1280 					/*dxfer_len*/ bp->bio_bcount,
1281 					/*sense_len*/SSD_FULL_SIZE,
1282 					/*timeout*/da_default_timeout*1000);
1283 			start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1284 
1285 			/*
1286 			 * Block out any asyncronous callbacks
1287 			 * while we touch the pending ccb list.
1288 			 */
1289 			oldspl = splcam();
1290 			LIST_INSERT_HEAD(&softc->pending_ccbs,
1291 					 &start_ccb->ccb_h, periph_links.le);
1292 			softc->outstanding_cmds++;
1293 			splx(oldspl);
1294 
1295 			/* We expect a unit attention from this device */
1296 			if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1297 				start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1298 				softc->flags &= ~DA_FLAG_RETRY_UA;
1299 			}
1300 
1301 			start_ccb->ccb_h.ccb_bp = bp;
1302 			bp = bioq_first(&softc->bio_queue);
1303 			splx(s);
1304 
1305 			xpt_action(start_ccb);
1306 		}
1307 
1308 		if (bp != NULL) {
1309 			/* Have more work to do, so ensure we stay scheduled */
1310 			xpt_schedule(periph, /* XXX priority */1);
1311 		}
1312 		break;
1313 	}
1314 	case DA_STATE_PROBE:
1315 	{
1316 		struct ccb_scsiio *csio;
1317 		struct scsi_read_capacity_data *rcap;
1318 
1319 		rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1320 								M_TEMP,
1321 								M_NOWAIT);
1322 		if (rcap == NULL) {
1323 			printf("dastart: Couldn't malloc read_capacity data\n");
1324 			/* da_free_periph??? */
1325 			break;
1326 		}
1327 		csio = &start_ccb->csio;
1328 		scsi_read_capacity(csio,
1329 				   /*retries*/4,
1330 				   dadone,
1331 				   MSG_SIMPLE_Q_TAG,
1332 				   rcap,
1333 				   SSD_FULL_SIZE,
1334 				   /*timeout*/5000);
1335 		start_ccb->ccb_h.ccb_bp = NULL;
1336 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1337 		xpt_action(start_ccb);
1338 		break;
1339 	}
1340 	case DA_STATE_PROBE2:
1341 	{
1342 		struct ccb_scsiio *csio;
1343 		struct scsi_read_capacity_data_long *rcaplong;
1344 
1345 		rcaplong = (struct scsi_read_capacity_data_long *)
1346 			malloc(sizeof(*rcaplong), M_TEMP, M_NOWAIT);
1347 		if (rcaplong == NULL) {
1348 			printf("dastart: Couldn't malloc read_capacity data\n");
1349 			/* da_free_periph??? */
1350 			break;
1351 		}
1352 		csio = &start_ccb->csio;
1353 		scsi_read_capacity_16(csio,
1354 				      /*retries*/ 4,
1355 				      /*cbfcnp*/ dadone,
1356 				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
1357 				      /*lba*/ 0,
1358 				      /*reladr*/ 0,
1359 				      /*pmi*/ 0,
1360 				      rcaplong,
1361 				      /*sense_len*/ SSD_FULL_SIZE,
1362 				      /*timeout*/ 60000);
1363 		start_ccb->ccb_h.ccb_bp = NULL;
1364 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1365 		xpt_action(start_ccb);
1366 		break;
1367 	}
1368 	}
1369 }
1370 
1371 static int
1372 cmd6workaround(union ccb *ccb)
1373 {
1374 	struct scsi_rw_6 cmd6;
1375 	struct scsi_rw_10 *cmd10;
1376 	struct da_softc *softc;
1377 	u_int8_t *cdb;
1378 	int frozen;
1379 
1380 	cdb = ccb->csio.cdb_io.cdb_bytes;
1381 
1382 	/* Translation only possible if CDB is an array and cmd is R/W6 */
1383 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1384 	    (*cdb != READ_6 && *cdb != WRITE_6))
1385 		return 0;
1386 
1387 	xpt_print_path(ccb->ccb_h.path);
1388  	printf("READ(6)/WRITE(6) not supported, "
1389 	       "increasing minimum_cmd_size to 10.\n");
1390  	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1391 	softc->minimum_cmd_size = 10;
1392 
1393 	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1394 	cmd10 = (struct scsi_rw_10 *)cdb;
1395 	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1396 	cmd10->byte2 = 0;
1397 	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1398 	cmd10->reserved = 0;
1399 	scsi_ulto2b(cmd6.length, cmd10->length);
1400 	cmd10->control = cmd6.control;
1401 	ccb->csio.cdb_len = sizeof(*cmd10);
1402 
1403 	/* Requeue request, unfreezing queue if necessary */
1404 	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1405  	ccb->ccb_h.status = CAM_REQUEUE_REQ;
1406 	xpt_action(ccb);
1407 	if (frozen) {
1408 		cam_release_devq(ccb->ccb_h.path,
1409 				 /*relsim_flags*/0,
1410 				 /*reduction*/0,
1411 				 /*timeout*/0,
1412 				 /*getcount_only*/0);
1413 	}
1414 	return (ERESTART);
1415 }
1416 
1417 static void
1418 dadone(struct cam_periph *periph, union ccb *done_ccb)
1419 {
1420 	struct da_softc *softc;
1421 	struct ccb_scsiio *csio;
1422 
1423 	softc = (struct da_softc *)periph->softc;
1424 	csio = &done_ccb->csio;
1425 	switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1426 	case DA_CCB_BUFFER_IO:
1427 	{
1428 		struct bio *bp;
1429 		int    oldspl;
1430 
1431 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1432 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1433 			int error;
1434 			int s;
1435 			int sf;
1436 
1437 			if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1438 				sf = SF_RETRY_UA;
1439 			else
1440 				sf = 0;
1441 
1442 			error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1443 			if (error == ERESTART) {
1444 				/*
1445 				 * A retry was scheuled, so
1446 				 * just return.
1447 				 */
1448 				return;
1449 			}
1450 			if (error != 0) {
1451 
1452 				s = splbio();
1453 
1454 				if (error == ENXIO) {
1455 					/*
1456 					 * Catastrophic error.  Mark our pack as
1457 					 * invalid.
1458 					 */
1459 					/* XXX See if this is really a media
1460 					 *     change first.
1461 					 */
1462 					xpt_print_path(periph->path);
1463 					printf("Invalidating pack\n");
1464 					softc->flags |= DA_FLAG_PACK_INVALID;
1465 				}
1466 
1467 				/*
1468 				 * return all queued I/O with EIO, so that
1469 				 * the client can retry these I/Os in the
1470 				 * proper order should it attempt to recover.
1471 				 */
1472 				bioq_flush(&softc->bio_queue, NULL, EIO);
1473 				splx(s);
1474 				bp->bio_error = error;
1475 				bp->bio_resid = bp->bio_bcount;
1476 				bp->bio_flags |= BIO_ERROR;
1477 			} else {
1478 				bp->bio_resid = csio->resid;
1479 				bp->bio_error = 0;
1480 				if (bp->bio_resid != 0) {
1481 					/* Short transfer ??? */
1482 #if 0
1483 					if (cmd6workaround(done_ccb)
1484 								== ERESTART)
1485 						return;
1486 #endif
1487 					bp->bio_flags |= BIO_ERROR;
1488 				}
1489 			}
1490 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1491 				cam_release_devq(done_ccb->ccb_h.path,
1492 						 /*relsim_flags*/0,
1493 						 /*reduction*/0,
1494 						 /*timeout*/0,
1495 						 /*getcount_only*/0);
1496 		} else {
1497 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1498 				panic("REQ_CMP with QFRZN");
1499 			bp->bio_resid = csio->resid;
1500 			if (csio->resid > 0) {
1501 				/* Short transfer ??? */
1502 #if 0 /* XXX most of the broken umass devices need this ad-hoc work around */
1503 				if (cmd6workaround(done_ccb) == ERESTART)
1504 					return;
1505 #endif
1506 				bp->bio_flags |= BIO_ERROR;
1507 			}
1508 		}
1509 
1510 		/*
1511 		 * Block out any asyncronous callbacks
1512 		 * while we touch the pending ccb list.
1513 		 */
1514 		oldspl = splcam();
1515 		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1516 		softc->outstanding_cmds--;
1517 		if (softc->outstanding_cmds == 0)
1518 			softc->flags |= DA_FLAG_WENT_IDLE;
1519 		splx(oldspl);
1520 
1521 		biodone(bp);
1522 		break;
1523 	}
1524 	case DA_CCB_PROBE:
1525 	case DA_CCB_PROBE2:
1526 	{
1527 		struct	   scsi_read_capacity_data *rdcap;
1528 		struct     scsi_read_capacity_data_long *rcaplong;
1529 		char	   announce_buf[80];
1530 
1531 		rdcap = NULL;
1532 		rcaplong = NULL;
1533 		if (softc->state == DA_STATE_PROBE)
1534 			rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1535 		else
1536 			rcaplong = (struct scsi_read_capacity_data_long *)
1537 				csio->data_ptr;
1538 
1539 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1540 			struct disk_params *dp;
1541 			uint32_t block_size;
1542 			uint64_t maxsector;
1543 
1544 			if (softc->state == DA_STATE_PROBE) {
1545 				block_size = scsi_4btoul(rdcap->length);
1546 				maxsector = scsi_4btoul(rdcap->addr);
1547 
1548 				/*
1549 				 * According to SBC-2, if the standard 10
1550 				 * byte READ CAPACITY command returns 2^32,
1551 				 * we should issue the 16 byte version of
1552 				 * the command, since the device in question
1553 				 * has more sectors than can be represented
1554 				 * with the short version of the command.
1555 				 */
1556 				if (maxsector == 0xffffffff) {
1557 					softc->state = DA_STATE_PROBE2;
1558 					free(rdcap, M_TEMP);
1559 					xpt_release_ccb(done_ccb);
1560 					xpt_schedule(periph, /*priority*/5);
1561 					return;
1562 				}
1563 			} else {
1564 				block_size = scsi_4btoul(rcaplong->length);
1565 				maxsector = scsi_8btou64(rcaplong->addr);
1566 			}
1567 			dasetgeom(periph, block_size, maxsector);
1568 			dp = &softc->params;
1569 			snprintf(announce_buf, sizeof(announce_buf),
1570 			        "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1571 				(uintmax_t) (((uintmax_t)dp->secsize *
1572 				dp->sectors) / (1024*1024)),
1573 			        (uintmax_t)dp->sectors,
1574 				dp->secsize, dp->heads, dp->secs_per_track,
1575 				dp->cylinders);
1576 		} else {
1577 			int	error;
1578 
1579 			announce_buf[0] = '\0';
1580 
1581 			/*
1582 			 * Retry any UNIT ATTENTION type errors.  They
1583 			 * are expected at boot.
1584 			 */
1585 			error = daerror(done_ccb, CAM_RETRY_SELTO,
1586 					SF_RETRY_UA|SF_NO_PRINT);
1587 			if (error == ERESTART) {
1588 				/*
1589 				 * A retry was scheuled, so
1590 				 * just return.
1591 				 */
1592 				return;
1593 			} else if (error != 0) {
1594 				struct scsi_sense_data *sense;
1595 				int asc, ascq;
1596 				int sense_key, error_code;
1597 				int have_sense;
1598 				cam_status status;
1599 				struct ccb_getdev cgd;
1600 
1601 				/* Don't wedge this device's queue */
1602 				status = done_ccb->ccb_h.status;
1603 				if ((status & CAM_DEV_QFRZN) != 0)
1604 					cam_release_devq(done_ccb->ccb_h.path,
1605 							 /*relsim_flags*/0,
1606 							 /*reduction*/0,
1607 							 /*timeout*/0,
1608 							 /*getcount_only*/0);
1609 
1610 
1611 				xpt_setup_ccb(&cgd.ccb_h,
1612 					      done_ccb->ccb_h.path,
1613 					      /* priority */ 1);
1614 				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1615 				xpt_action((union ccb *)&cgd);
1616 
1617 				if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1618 				 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1619 				 || ((status & CAM_AUTOSNS_VALID) == 0))
1620 					have_sense = FALSE;
1621 				else
1622 					have_sense = TRUE;
1623 
1624 				if (have_sense) {
1625 					sense = &csio->sense_data;
1626 					scsi_extract_sense(sense, &error_code,
1627 							   &sense_key,
1628 							   &asc, &ascq);
1629 				}
1630 				/*
1631 				 * Attach to anything that claims to be a
1632 				 * direct access or optical disk device,
1633 				 * as long as it doesn't return a "Logical
1634 				 * unit not supported" (0x25) error.
1635 				 */
1636 				if ((have_sense) && (asc != 0x25)
1637 				 && (error_code == SSD_CURRENT_ERROR)) {
1638 					const char *sense_key_desc;
1639 					const char *asc_desc;
1640 
1641 					scsi_sense_desc(sense_key, asc, ascq,
1642 							&cgd.inq_data,
1643 							&sense_key_desc,
1644 							&asc_desc);
1645 					snprintf(announce_buf,
1646 					    sizeof(announce_buf),
1647 						"Attempt to query device "
1648 						"size failed: %s, %s",
1649 						sense_key_desc,
1650 						asc_desc);
1651 				} else {
1652 					if (have_sense)
1653 						scsi_sense_print(
1654 							&done_ccb->csio);
1655 					else {
1656 						xpt_print_path(periph->path);
1657 						printf("got CAM status %#x\n",
1658 						       done_ccb->ccb_h.status);
1659 					}
1660 
1661 					xpt_print_path(periph->path);
1662 					printf("fatal error, failed"
1663 					       " to attach to device\n");
1664 
1665 					/*
1666 					 * Free up resources.
1667 					 */
1668 					cam_periph_invalidate(periph);
1669 				}
1670 			}
1671 		}
1672 		free(csio->data_ptr, M_TEMP);
1673 		if (announce_buf[0] != '\0')
1674 			xpt_announce_periph(periph, announce_buf);
1675 		softc->state = DA_STATE_NORMAL;
1676 		/*
1677 		 * Since our peripheral may be invalidated by an error
1678 		 * above or an external event, we must release our CCB
1679 		 * before releasing the probe lock on the peripheral.
1680 		 * The peripheral will only go away once the last lock
1681 		 * is removed, and we need it around for the CCB release
1682 		 * operation.
1683 		 */
1684 		xpt_release_ccb(done_ccb);
1685 		cam_periph_unlock(periph);
1686 		return;
1687 	}
1688 	case DA_CCB_WAITING:
1689 	{
1690 		/* Caller will release the CCB */
1691 		wakeup(&done_ccb->ccb_h.cbfcnp);
1692 		return;
1693 	}
1694 	case DA_CCB_DUMP:
1695 		/* No-op.  We're polling */
1696 		return;
1697 	default:
1698 		break;
1699 	}
1700 	xpt_release_ccb(done_ccb);
1701 }
1702 
1703 static int
1704 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1705 {
1706 	struct da_softc	  *softc;
1707 	struct cam_periph *periph;
1708 	int error;
1709 
1710 	periph = xpt_path_periph(ccb->ccb_h.path);
1711 	softc = (struct da_softc *)periph->softc;
1712 
1713  	/*
1714 	 * Automatically detect devices that do not support
1715  	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1716  	 */
1717 	error = 0;
1718 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1719 		error = cmd6workaround(ccb);
1720 	} else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1721 		   CAM_SCSI_STATUS_ERROR)
1722 	 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1723 	 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1724 	 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1725 	 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1726 		int sense_key, error_code, asc, ascq;
1727 
1728  		scsi_extract_sense(&ccb->csio.sense_data,
1729 				   &error_code, &sense_key, &asc, &ascq);
1730 		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1731  			error = cmd6workaround(ccb);
1732 	}
1733 	if (error == ERESTART)
1734 		return (ERESTART);
1735 
1736 	/*
1737 	 * XXX
1738 	 * Until we have a better way of doing pack validation,
1739 	 * don't treat UAs as errors.
1740 	 */
1741 	sense_flags |= SF_RETRY_UA;
1742 	return(cam_periph_error(ccb, cam_flags, sense_flags,
1743 				&softc->saved_ccb));
1744 }
1745 
1746 static void
1747 daprevent(struct cam_periph *periph, int action)
1748 {
1749 	struct	da_softc *softc;
1750 	union	ccb *ccb;
1751 	int	error;
1752 
1753 	softc = (struct da_softc *)periph->softc;
1754 
1755 	if (((action == PR_ALLOW)
1756 	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1757 	 || ((action == PR_PREVENT)
1758 	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1759 		return;
1760 	}
1761 
1762 	ccb = cam_periph_getccb(periph, /*priority*/1);
1763 
1764 	scsi_prevent(&ccb->csio,
1765 		     /*retries*/1,
1766 		     /*cbcfp*/dadone,
1767 		     MSG_SIMPLE_Q_TAG,
1768 		     action,
1769 		     SSD_FULL_SIZE,
1770 		     5000);
1771 
1772 	error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1773 				  SF_RETRY_UA, softc->disk.d_devstat);
1774 
1775 	if (error == 0) {
1776 		if (action == PR_ALLOW)
1777 			softc->flags &= ~DA_FLAG_PACK_LOCKED;
1778 		else
1779 			softc->flags |= DA_FLAG_PACK_LOCKED;
1780 	}
1781 
1782 	xpt_release_ccb(ccb);
1783 }
1784 
1785 static int
1786 dagetcapacity(struct cam_periph *periph)
1787 {
1788 	struct da_softc *softc;
1789 	union ccb *ccb;
1790 	struct scsi_read_capacity_data *rcap;
1791 	struct scsi_read_capacity_data_long *rcaplong;
1792 	uint32_t block_len;
1793 	uint64_t maxsector;
1794 	int error;
1795 
1796 	softc = (struct da_softc *)periph->softc;
1797 	block_len = 0;
1798 	maxsector = 0;
1799 	error = 0;
1800 
1801 	/* Do a read capacity */
1802 	rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
1803 							M_TEMP,
1804 							M_WAITOK);
1805 
1806 	ccb = cam_periph_getccb(periph, /*priority*/1);
1807 	scsi_read_capacity(&ccb->csio,
1808 			   /*retries*/4,
1809 			   /*cbfncp*/dadone,
1810 			   MSG_SIMPLE_Q_TAG,
1811 			   rcap,
1812 			   SSD_FULL_SIZE,
1813 			   /*timeout*/60000);
1814 	ccb->ccb_h.ccb_bp = NULL;
1815 
1816 	error = cam_periph_runccb(ccb, daerror,
1817 				  /*cam_flags*/CAM_RETRY_SELTO,
1818 				  /*sense_flags*/SF_RETRY_UA,
1819 				  softc->disk.d_devstat);
1820 
1821 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1822 		cam_release_devq(ccb->ccb_h.path,
1823 				 /*relsim_flags*/0,
1824 				 /*reduction*/0,
1825 				 /*timeout*/0,
1826 				 /*getcount_only*/0);
1827 
1828 	if (error == 0) {
1829 		block_len = scsi_4btoul(rcap->length);
1830 		maxsector = scsi_4btoul(rcap->addr);
1831 
1832 		if (maxsector != 0xffffffff)
1833 			goto done;
1834 	} else
1835 		goto done;
1836 
1837 	rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1838 
1839 	scsi_read_capacity_16(&ccb->csio,
1840 			      /*retries*/ 4,
1841 			      /*cbfcnp*/ dadone,
1842 			      /*tag_action*/ MSG_SIMPLE_Q_TAG,
1843 			      /*lba*/ 0,
1844 			      /*reladr*/ 0,
1845 			      /*pmi*/ 0,
1846 			      rcaplong,
1847 			      /*sense_len*/ SSD_FULL_SIZE,
1848 			      /*timeout*/ 60000);
1849 	ccb->ccb_h.ccb_bp = NULL;
1850 
1851 	error = cam_periph_runccb(ccb, daerror,
1852 				  /*cam_flags*/CAM_RETRY_SELTO,
1853 				  /*sense_flags*/SF_RETRY_UA,
1854 				  softc->disk.d_devstat);
1855 
1856 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1857 		cam_release_devq(ccb->ccb_h.path,
1858 				 /*relsim_flags*/0,
1859 				 /*reduction*/0,
1860 				 /*timeout*/0,
1861 				 /*getcount_only*/0);
1862 
1863 	if (error == 0) {
1864 		block_len = scsi_4btoul(rcaplong->length);
1865 		maxsector = scsi_8btou64(rcaplong->addr);
1866 	}
1867 
1868 done:
1869 
1870 	if (error == 0)
1871 		dasetgeom(periph, block_len, maxsector);
1872 
1873 	xpt_release_ccb(ccb);
1874 
1875 	free(rcap, M_TEMP);
1876 
1877 	return (error);
1878 }
1879 
1880 static void
1881 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
1882 {
1883 	struct ccb_calc_geometry ccg;
1884 	struct da_softc *softc;
1885 	struct disk_params *dp;
1886 
1887 	softc = (struct da_softc *)periph->softc;
1888 
1889 	dp = &softc->params;
1890 	dp->secsize = block_len;
1891 	dp->sectors = maxsector + 1;
1892 	/*
1893 	 * Have the controller provide us with a geometry
1894 	 * for this disk.  The only time the geometry
1895 	 * matters is when we boot and the controller
1896 	 * is the only one knowledgeable enough to come
1897 	 * up with something that will make this a bootable
1898 	 * device.
1899 	 */
1900 	xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1901 	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1902 	ccg.block_size = dp->secsize;
1903 	ccg.volume_size = dp->sectors;
1904 	ccg.heads = 0;
1905 	ccg.secs_per_track = 0;
1906 	ccg.cylinders = 0;
1907 	xpt_action((union ccb*)&ccg);
1908 	dp->heads = ccg.heads;
1909 	dp->secs_per_track = ccg.secs_per_track;
1910 	dp->cylinders = ccg.cylinders;
1911 }
1912 
1913 static void
1914 dasendorderedtag(void *arg)
1915 {
1916 	struct da_softc *softc;
1917 	int s;
1918 
1919 	for (softc = SLIST_FIRST(&softc_list);
1920 	     softc != NULL;
1921 	     softc = SLIST_NEXT(softc, links)) {
1922 		s = splsoftcam();
1923 		if ((softc->ordered_tag_count == 0)
1924 		 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1925 			softc->flags |= DA_FLAG_NEED_OTAG;
1926 		}
1927 		if (softc->outstanding_cmds > 0)
1928 			softc->flags &= ~DA_FLAG_WENT_IDLE;
1929 
1930 		softc->ordered_tag_count = 0;
1931 		splx(s);
1932 	}
1933 	/* Queue us up again */
1934 	timeout(dasendorderedtag, NULL,
1935 		(da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
1936 }
1937 
1938 /*
1939  * Step through all DA peripheral drivers, and if the device is still open,
1940  * sync the disk cache to physical media.
1941  */
1942 static void
1943 dashutdown(void * arg, int howto)
1944 {
1945 	struct cam_periph *periph;
1946 	struct da_softc *softc;
1947 
1948 	TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
1949 		union ccb ccb;
1950 		softc = (struct da_softc *)periph->softc;
1951 
1952 		/*
1953 		 * We only sync the cache if the drive is still open, and
1954 		 * if the drive is capable of it..
1955 		 */
1956 		if (((softc->flags & DA_FLAG_OPEN) == 0)
1957 		 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1958 			continue;
1959 
1960 		xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1961 
1962 		ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1963 		scsi_synchronize_cache(&ccb.csio,
1964 				       /*retries*/1,
1965 				       /*cbfcnp*/dadone,
1966 				       MSG_SIMPLE_Q_TAG,
1967 				       /*begin_lba*/0, /* whole disk */
1968 				       /*lb_count*/0,
1969 				       SSD_FULL_SIZE,
1970 				       60 * 60 * 1000);
1971 
1972 		xpt_polled_action(&ccb);
1973 
1974 		if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1975 			if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1976 			     CAM_SCSI_STATUS_ERROR)
1977 			 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1978 				int error_code, sense_key, asc, ascq;
1979 
1980 				scsi_extract_sense(&ccb.csio.sense_data,
1981 						   &error_code, &sense_key,
1982 						   &asc, &ascq);
1983 
1984 				if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1985 					scsi_sense_print(&ccb.csio);
1986 			} else {
1987 				xpt_print_path(periph->path);
1988 				printf("Synchronize cache failed, status "
1989 				       "== 0x%x, scsi status == 0x%x\n",
1990 				       ccb.ccb_h.status, ccb.csio.scsi_status);
1991 			}
1992 		}
1993 
1994 		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1995 			cam_release_devq(ccb.ccb_h.path,
1996 					 /*relsim_flags*/0,
1997 					 /*reduction*/0,
1998 					 /*timeout*/0,
1999 					 /*getcount_only*/0);
2000 
2001 	}
2002 }
2003 
2004 #else /* !_KERNEL */
2005 
2006 /*
2007  * XXX This is only left out of the kernel build to silence warnings.  If,
2008  * for some reason this function is used in the kernel, the ifdefs should
2009  * be moved so it is included both in the kernel and userland.
2010  */
2011 void
2012 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2013 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
2014 		 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2015 		 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2016 		 u_int32_t timeout)
2017 {
2018 	struct scsi_format_unit *scsi_cmd;
2019 
2020 	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2021 	scsi_cmd->opcode = FORMAT_UNIT;
2022 	scsi_cmd->byte2 = byte2;
2023 	scsi_ulto2b(ileave, scsi_cmd->interleave);
2024 
2025 	cam_fill_csio(csio,
2026 		      retries,
2027 		      cbfcnp,
2028 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2029 		      tag_action,
2030 		      data_ptr,
2031 		      dxfer_len,
2032 		      sense_len,
2033 		      sizeof(*scsi_cmd),
2034 		      timeout);
2035 }
2036 
2037 #endif /* _KERNEL */
2038