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