xref: /freebsd/sys/cam/scsi/scsi_da.c (revision c128b2d129a8e305b673ef5e3da76af1fb91ae60)
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 		 * PNY USB Flash keys
425 		 * PR: usb/75578, usb/72344, usb/65436
426 		 */
427 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
428 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
429 	},
430 	{
431 		/*
432 		 * Genesys 6-in-1 Card Reader
433 		 * No PR, reported by anders
434 		 */
435 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
436 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
437 	},
438 };
439 
440 static	disk_strategy_t	dastrategy;
441 static	dumper_t	dadump;
442 static	periph_init_t	dainit;
443 static	void		daasync(void *callback_arg, u_int32_t code,
444 				struct cam_path *path, void *arg);
445 static	void		dasysctlinit(void *context, int pending);
446 static	int		dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
447 static	periph_ctor_t	daregister;
448 static	periph_dtor_t	dacleanup;
449 static	periph_start_t	dastart;
450 static	periph_oninv_t	daoninvalidate;
451 static	void		dadone(struct cam_periph *periph,
452 			       union ccb *done_ccb);
453 static  int		daerror(union ccb *ccb, u_int32_t cam_flags,
454 				u_int32_t sense_flags);
455 static void		daprevent(struct cam_periph *periph, int action);
456 static int		dagetcapacity(struct cam_periph *periph);
457 static void		dasetgeom(struct cam_periph *periph, uint32_t block_len,
458 				  uint64_t maxsector);
459 static timeout_t	dasendorderedtag;
460 static void		dashutdown(void *arg, int howto);
461 
462 #ifndef DA_DEFAULT_TIMEOUT
463 #define DA_DEFAULT_TIMEOUT 60	/* Timeout in seconds */
464 #endif
465 
466 #ifndef	DA_DEFAULT_RETRY
467 #define	DA_DEFAULT_RETRY	4
468 #endif
469 
470 static int da_retry_count = DA_DEFAULT_RETRY;
471 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
472 
473 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
474             "CAM Direct Access Disk driver");
475 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
476            &da_retry_count, 0, "Normal I/O retry count");
477 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
478 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
479            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
480 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
481 
482 /*
483  * DA_ORDEREDTAG_INTERVAL determines how often, relative
484  * to the default timeout, we check to see whether an ordered
485  * tagged transaction is appropriate to prevent simple tag
486  * starvation.  Since we'd like to ensure that there is at least
487  * 1/2 of the timeout length left for a starved transaction to
488  * complete after we've sent an ordered tag, we must poll at least
489  * four times in every timeout period.  This takes care of the worst
490  * case where a starved transaction starts during an interval that
491  * meets the requirement "don't send an ordered tag" test so it takes
492  * us two intervals to determine that a tag must be sent.
493  */
494 #ifndef DA_ORDEREDTAG_INTERVAL
495 #define DA_ORDEREDTAG_INTERVAL 4
496 #endif
497 
498 static struct periph_driver dadriver =
499 {
500 	dainit, "da",
501 	TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
502 };
503 
504 PERIPHDRIVER_DECLARE(da, dadriver);
505 
506 static SLIST_HEAD(,da_softc) softc_list;
507 
508 static int
509 daopen(struct disk *dp)
510 {
511 	struct cam_periph *periph;
512 	struct da_softc *softc;
513 	int unit;
514 	int error;
515 	int s;
516 
517 	s = splsoftcam();
518 	periph = (struct cam_periph *)dp->d_drv1;
519 	if (periph == NULL) {
520 		splx(s);
521 		return (ENXIO);
522 	}
523 	unit = periph->unit_number;
524 
525 	softc = (struct da_softc *)periph->softc;
526 
527 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
528 	    ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
529 	     unit));
530 
531 	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0)
532 		return (error); /* error code from tsleep */
533 
534 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
535 		return(ENXIO);
536 	softc->flags |= DA_FLAG_OPEN;
537 
538 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
539 		/* Invalidate our pack information. */
540 		softc->flags &= ~DA_FLAG_PACK_INVALID;
541 	}
542 	splx(s);
543 
544 	error = dagetcapacity(periph);
545 
546 	if (error == 0) {
547 
548 		softc->disk->d_sectorsize = softc->params.secsize;
549 		softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
550 		/* XXX: these are not actually "firmware" values, so they may be wrong */
551 		softc->disk->d_fwsectors = softc->params.secs_per_track;
552 		softc->disk->d_fwheads = softc->params.heads;
553 		softc->disk->d_devstat->block_size = softc->params.secsize;
554 		softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
555 	}
556 
557 	if (error == 0) {
558 		if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
559 		    (softc->quirks & DA_Q_NO_PREVENT) == 0)
560 			daprevent(periph, PR_PREVENT);
561 	} else {
562 		softc->flags &= ~DA_FLAG_OPEN;
563 		cam_periph_release(periph);
564 	}
565 	cam_periph_unlock(periph);
566 	return (error);
567 }
568 
569 static int
570 daclose(struct disk *dp)
571 {
572 	struct	cam_periph *periph;
573 	struct	da_softc *softc;
574 	int	error;
575 
576 	periph = (struct cam_periph *)dp->d_drv1;
577 	if (periph == NULL)
578 		return (ENXIO);
579 
580 	softc = (struct da_softc *)periph->softc;
581 
582 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
583 		return (error); /* error code from tsleep */
584 	}
585 
586 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
587 		union	ccb *ccb;
588 
589 		ccb = cam_periph_getccb(periph, /*priority*/1);
590 
591 		scsi_synchronize_cache(&ccb->csio,
592 				       /*retries*/1,
593 				       /*cbfcnp*/dadone,
594 				       MSG_SIMPLE_Q_TAG,
595 				       /*begin_lba*/0,/* Cover the whole disk */
596 				       /*lb_count*/0,
597 				       SSD_FULL_SIZE,
598 				       5 * 60 * 1000);
599 
600 		cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
601 				  /*sense_flags*/SF_RETRY_UA,
602 				  softc->disk->d_devstat);
603 
604 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
605 			if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
606 			     CAM_SCSI_STATUS_ERROR) {
607 				int asc, ascq;
608 				int sense_key, error_code;
609 
610 				scsi_extract_sense(&ccb->csio.sense_data,
611 						   &error_code,
612 						   &sense_key,
613 						   &asc, &ascq);
614 				if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
615 					scsi_sense_print(&ccb->csio);
616 			} else {
617 				xpt_print_path(periph->path);
618 				printf("Synchronize cache failed, status "
619 				       "== 0x%x, scsi status == 0x%x\n",
620 				       ccb->csio.ccb_h.status,
621 				       ccb->csio.scsi_status);
622 			}
623 		}
624 
625 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
626 			cam_release_devq(ccb->ccb_h.path,
627 					 /*relsim_flags*/0,
628 					 /*reduction*/0,
629 					 /*timeout*/0,
630 					 /*getcount_only*/0);
631 
632 		xpt_release_ccb(ccb);
633 
634 	}
635 
636 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
637 		if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
638 			daprevent(periph, PR_ALLOW);
639 		/*
640 		 * If we've got removeable media, mark the blocksize as
641 		 * unavailable, since it could change when new media is
642 		 * inserted.
643 		 */
644 		softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
645 	}
646 
647 	softc->flags &= ~DA_FLAG_OPEN;
648 	cam_periph_unlock(periph);
649 	cam_periph_release(periph);
650 	return (0);
651 }
652 
653 /*
654  * Actually translate the requested transfer into one the physical driver
655  * can understand.  The transfer is described by a buf and will include
656  * only one physical transfer.
657  */
658 static void
659 dastrategy(struct bio *bp)
660 {
661 	struct cam_periph *periph;
662 	struct da_softc *softc;
663 	int    s;
664 
665 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
666 	if (periph == NULL) {
667 		biofinish(bp, NULL, ENXIO);
668 		return;
669 	}
670 	softc = (struct da_softc *)periph->softc;
671 #if 0
672 	/*
673 	 * check it's not too big a transfer for our adapter
674 	 */
675 	scsi_minphys(bp,&sd_switch);
676 #endif
677 
678 	/*
679 	 * Mask interrupts so that the pack cannot be invalidated until
680 	 * after we are in the queue.  Otherwise, we might not properly
681 	 * clean up one of the buffers.
682 	 */
683 	s = splbio();
684 
685 	/*
686 	 * If the device has been made invalid, error out
687 	 */
688 	if ((softc->flags & DA_FLAG_PACK_INVALID)) {
689 		splx(s);
690 		biofinish(bp, NULL, ENXIO);
691 		return;
692 	}
693 
694 	/*
695 	 * Place it in the queue of disk activities for this disk
696 	 */
697 	bioq_disksort(&softc->bio_queue, bp);
698 
699 	splx(s);
700 
701 	/*
702 	 * Schedule ourselves for performing the work.
703 	 */
704 	xpt_schedule(periph, /* XXX priority */1);
705 
706 	return;
707 }
708 
709 static int
710 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
711 {
712 	struct	    cam_periph *periph;
713 	struct	    da_softc *softc;
714 	u_int	    secsize;
715 	struct	    ccb_scsiio csio;
716 	struct	    disk *dp;
717 
718 	dp = arg;
719 	periph = dp->d_drv1;
720 	if (periph == NULL)
721 		return (ENXIO);
722 	softc = (struct da_softc *)periph->softc;
723 	secsize = softc->params.secsize;
724 
725 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
726 		return (ENXIO);
727 
728 	if (length > 0) {
729 		xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
730 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
731 		scsi_read_write(&csio,
732 				/*retries*/1,
733 				dadone,
734 				MSG_ORDERED_Q_TAG,
735 				/*read*/FALSE,
736 				/*byte2*/0,
737 				/*minimum_cmd_size*/ softc->minimum_cmd_size,
738 				offset / secsize,
739 				length / secsize,
740 				/*data_ptr*/(u_int8_t *) virtual,
741 				/*dxfer_len*/length,
742 				/*sense_len*/SSD_FULL_SIZE,
743 				DA_DEFAULT_TIMEOUT * 1000);
744 		xpt_polled_action((union ccb *)&csio);
745 
746 		if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
747 			printf("Aborting dump due to I/O error.\n");
748 			if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
749 			     CAM_SCSI_STATUS_ERROR)
750 				scsi_sense_print(&csio);
751 			else
752 				printf("status == 0x%x, scsi status == 0x%x\n",
753 				       csio.ccb_h.status, csio.scsi_status);
754 			return(EIO);
755 		}
756 		return(0);
757 	}
758 
759 	/*
760 	 * Sync the disk cache contents to the physical media.
761 	 */
762 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
763 
764 		xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
765 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
766 		scsi_synchronize_cache(&csio,
767 				       /*retries*/1,
768 				       /*cbfcnp*/dadone,
769 				       MSG_SIMPLE_Q_TAG,
770 				       /*begin_lba*/0,/* Cover the whole disk */
771 				       /*lb_count*/0,
772 				       SSD_FULL_SIZE,
773 				       5 * 60 * 1000);
774 		xpt_polled_action((union ccb *)&csio);
775 
776 		if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
777 			if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
778 			     CAM_SCSI_STATUS_ERROR) {
779 				int asc, ascq;
780 				int sense_key, error_code;
781 
782 				scsi_extract_sense(&csio.sense_data,
783 						   &error_code,
784 						   &sense_key,
785 						   &asc, &ascq);
786 				if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
787 					scsi_sense_print(&csio);
788 			} else {
789 				xpt_print_path(periph->path);
790 				printf("Synchronize cache failed, status "
791 				       "== 0x%x, scsi status == 0x%x\n",
792 				       csio.ccb_h.status, csio.scsi_status);
793 			}
794 		}
795 	}
796 	return (0);
797 }
798 
799 static void
800 dainit(void)
801 {
802 	cam_status status;
803 	struct cam_path *path;
804 
805 	SLIST_INIT(&softc_list);
806 
807 	/*
808 	 * Install a global async callback.  This callback will
809 	 * receive async callbacks like "new device found".
810 	 */
811 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
812 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
813 
814 	if (status == CAM_REQ_CMP) {
815 		struct ccb_setasync csa;
816 
817                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
818                 csa.ccb_h.func_code = XPT_SASYNC_CB;
819                 csa.event_enable = AC_FOUND_DEVICE;
820                 csa.callback = daasync;
821                 csa.callback_arg = NULL;
822                 xpt_action((union ccb *)&csa);
823 		status = csa.ccb_h.status;
824                 xpt_free_path(path);
825         }
826 
827 	if (status != CAM_REQ_CMP) {
828 		printf("da: Failed to attach master async callback "
829 		       "due to status 0x%x!\n", status);
830 	} else {
831 
832 		/*
833 		 * Schedule a periodic event to occasionally send an
834 		 * ordered tag to a device.
835 		 */
836 		timeout(dasendorderedtag, NULL,
837 			(DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
838 
839 		/* Register our shutdown event handler */
840 		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
841 					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
842 		    printf("dainit: shutdown event registration failed!\n");
843 	}
844 }
845 
846 static void
847 daoninvalidate(struct cam_periph *periph)
848 {
849 	int s;
850 	struct da_softc *softc;
851 	struct ccb_setasync csa;
852 
853 	softc = (struct da_softc *)periph->softc;
854 
855 	/*
856 	 * De-register any async callbacks.
857 	 */
858 	xpt_setup_ccb(&csa.ccb_h, periph->path,
859 		      /* priority */ 5);
860 	csa.ccb_h.func_code = XPT_SASYNC_CB;
861 	csa.event_enable = 0;
862 	csa.callback = daasync;
863 	csa.callback_arg = periph;
864 	xpt_action((union ccb *)&csa);
865 
866 	softc->flags |= DA_FLAG_PACK_INVALID;
867 
868 	/*
869 	 * Although the oninvalidate() routines are always called at
870 	 * splsoftcam, we need to be at splbio() here to keep the buffer
871 	 * queue from being modified while we traverse it.
872 	 */
873 	s = splbio();
874 
875 	/*
876 	 * Return all queued I/O with ENXIO.
877 	 * XXX Handle any transactions queued to the card
878 	 *     with XPT_ABORT_CCB.
879 	 */
880 	bioq_flush(&softc->bio_queue, NULL, ENXIO);
881 	splx(s);
882 
883 	SLIST_REMOVE(&softc_list, softc, da_softc, links);
884 
885 	disk_gone(softc->disk);
886 	xpt_print_path(periph->path);
887 	printf("lost device\n");
888 }
889 
890 static void
891 dacleanup(struct cam_periph *periph)
892 {
893 	struct da_softc *softc;
894 
895 	softc = (struct da_softc *)periph->softc;
896 
897 	xpt_print_path(periph->path);
898 	printf("removing device entry\n");
899 	/*
900 	 * If we can't free the sysctl tree, oh well...
901 	 */
902 	if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
903 	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
904 		xpt_print_path(periph->path);
905 		printf("can't remove sysctl context\n");
906 	}
907 	disk_destroy(softc->disk);
908 	free(softc, M_DEVBUF);
909 }
910 
911 static void
912 daasync(void *callback_arg, u_int32_t code,
913 	struct cam_path *path, void *arg)
914 {
915 	struct cam_periph *periph;
916 
917 	periph = (struct cam_periph *)callback_arg;
918 	switch (code) {
919 	case AC_FOUND_DEVICE:
920 	{
921 		struct ccb_getdev *cgd;
922 		cam_status status;
923 
924 		cgd = (struct ccb_getdev *)arg;
925 		if (cgd == NULL)
926 			break;
927 
928 		if (SID_TYPE(&cgd->inq_data) != T_DIRECT
929 		    && SID_TYPE(&cgd->inq_data) != T_RBC
930 		    && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
931 			break;
932 
933 		/*
934 		 * Allocate a peripheral instance for
935 		 * this device and start the probe
936 		 * process.
937 		 */
938 		status = cam_periph_alloc(daregister, daoninvalidate,
939 					  dacleanup, dastart,
940 					  "da", CAM_PERIPH_BIO,
941 					  cgd->ccb_h.path, daasync,
942 					  AC_FOUND_DEVICE, cgd);
943 
944 		if (status != CAM_REQ_CMP
945 		 && status != CAM_REQ_INPROG)
946 			printf("daasync: Unable to attach to new device "
947 				"due to status 0x%x\n", status);
948 		break;
949 	}
950 	case AC_SENT_BDR:
951 	case AC_BUS_RESET:
952 	{
953 		struct da_softc *softc;
954 		struct ccb_hdr *ccbh;
955 		int s;
956 
957 		softc = (struct da_softc *)periph->softc;
958 		s = splsoftcam();
959 		/*
960 		 * Don't fail on the expected unit attention
961 		 * that will occur.
962 		 */
963 		softc->flags |= DA_FLAG_RETRY_UA;
964 		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
965 			ccbh->ccb_state |= DA_CCB_RETRY_UA;
966 		splx(s);
967 		/* FALLTHROUGH*/
968 	}
969 	default:
970 		cam_periph_async(periph, code, path, arg);
971 		break;
972 	}
973 }
974 
975 static void
976 dasysctlinit(void *context, int pending)
977 {
978 	struct cam_periph *periph;
979 	struct da_softc *softc;
980 	char tmpstr[80], tmpstr2[80];
981 
982 	periph = (struct cam_periph *)context;
983 	softc = (struct da_softc *)periph->softc;
984 
985 	snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
986 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
987 
988 	mtx_lock(&Giant);
989 	sysctl_ctx_init(&softc->sysctl_ctx);
990 	softc->flags |= DA_FLAG_SCTX_INIT;
991 	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
992 		SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
993 		CTLFLAG_RD, 0, tmpstr);
994 	if (softc->sysctl_tree == NULL) {
995 		printf("dasysctlinit: unable to allocate sysctl tree\n");
996 		return;
997 	}
998 
999 	/*
1000 	 * Now register the sysctl handler, so the user can the value on
1001 	 * the fly.
1002 	 */
1003 	SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1004 		OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1005 		&softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1006 		"Minimum CDB size");
1007 
1008 	mtx_unlock(&Giant);
1009 }
1010 
1011 static int
1012 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1013 {
1014 	int error, value;
1015 
1016 	value = *(int *)arg1;
1017 
1018 	error = sysctl_handle_int(oidp, &value, 0, req);
1019 
1020 	if ((error != 0)
1021 	 || (req->newptr == NULL))
1022 		return (error);
1023 
1024 	/*
1025 	 * Acceptable values here are 6, 10, 12 or 16.
1026 	 */
1027 	if (value < 6)
1028 		value = 6;
1029 	else if ((value > 6)
1030 	      && (value <= 10))
1031 		value = 10;
1032 	else if ((value > 10)
1033 	      && (value <= 12))
1034 		value = 12;
1035 	else if (value > 12)
1036 		value = 16;
1037 
1038 	*(int *)arg1 = value;
1039 
1040 	return (0);
1041 }
1042 
1043 static cam_status
1044 daregister(struct cam_periph *periph, void *arg)
1045 {
1046 	int s;
1047 	struct da_softc *softc;
1048 	struct ccb_setasync csa;
1049 	struct ccb_pathinq cpi;
1050 	struct ccb_getdev *cgd;
1051 	char tmpstr[80];
1052 	caddr_t match;
1053 
1054 	cgd = (struct ccb_getdev *)arg;
1055 	if (periph == NULL) {
1056 		printf("daregister: periph was NULL!!\n");
1057 		return(CAM_REQ_CMP_ERR);
1058 	}
1059 
1060 	if (cgd == NULL) {
1061 		printf("daregister: no getdev CCB, can't register device\n");
1062 		return(CAM_REQ_CMP_ERR);
1063 	}
1064 
1065 	softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1066 	    M_NOWAIT|M_ZERO);
1067 
1068 	if (softc == NULL) {
1069 		printf("daregister: Unable to probe new device. "
1070 		       "Unable to allocate softc\n");
1071 		return(CAM_REQ_CMP_ERR);
1072 	}
1073 
1074 	LIST_INIT(&softc->pending_ccbs);
1075 	softc->state = DA_STATE_PROBE;
1076 	bioq_init(&softc->bio_queue);
1077 	if (SID_IS_REMOVABLE(&cgd->inq_data))
1078 		softc->flags |= DA_FLAG_PACK_REMOVABLE;
1079 	if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1080 		softc->flags |= DA_FLAG_TAGGED_QUEUING;
1081 
1082 	periph->softc = softc;
1083 
1084 	/*
1085 	 * See if this device has any quirks.
1086 	 */
1087 	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1088 			       (caddr_t)da_quirk_table,
1089 			       sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1090 			       sizeof(*da_quirk_table), scsi_inquiry_match);
1091 
1092 	if (match != NULL)
1093 		softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1094 	else
1095 		softc->quirks = DA_Q_NONE;
1096 
1097 	/* Check if the SIM does not want 6 byte commands */
1098 	xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1099 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1100 	xpt_action((union ccb *)&cpi);
1101 	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1102 		softc->quirks |= DA_Q_NO_6_BYTE;
1103 
1104 	TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1105 
1106 	/*
1107 	 * RBC devices don't have to support READ(6), only READ(10).
1108 	 */
1109 	if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1110 		softc->minimum_cmd_size = 10;
1111 	else
1112 		softc->minimum_cmd_size = 6;
1113 
1114 	/*
1115 	 * Load the user's default, if any.
1116 	 */
1117 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1118 		 periph->unit_number);
1119 	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1120 
1121 	/*
1122 	 * 6, 10, 12 and 16 are the currently permissible values.
1123 	 */
1124 	if (softc->minimum_cmd_size < 6)
1125 		softc->minimum_cmd_size = 6;
1126 	else if ((softc->minimum_cmd_size > 6)
1127 	      && (softc->minimum_cmd_size <= 10))
1128 		softc->minimum_cmd_size = 10;
1129 	else if ((softc->minimum_cmd_size > 10)
1130 	      && (softc->minimum_cmd_size <= 12))
1131 		softc->minimum_cmd_size = 12;
1132 	else if (softc->minimum_cmd_size > 12)
1133 		softc->minimum_cmd_size = 16;
1134 
1135 	/*
1136 	 * Block our timeout handler while we
1137 	 * add this softc to the dev list.
1138 	 */
1139 	s = splsoftclock();
1140 	SLIST_INSERT_HEAD(&softc_list, softc, links);
1141 	splx(s);
1142 
1143 	/*
1144 	 * Register this media as a disk
1145 	 */
1146 
1147 	softc->disk = disk_alloc();
1148 	softc->disk->d_open = daopen;
1149 	softc->disk->d_close = daclose;
1150 	softc->disk->d_strategy = dastrategy;
1151 	softc->disk->d_dump = dadump;
1152 	softc->disk->d_name = "da";
1153 	softc->disk->d_drv1 = periph;
1154 	softc->disk->d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */
1155 	softc->disk->d_unit = periph->unit_number;
1156 	softc->disk->d_flags = DISKFLAG_NEEDSGIANT;
1157 	disk_create(softc->disk, DISK_VERSION);
1158 
1159 	/*
1160 	 * Add async callbacks for bus reset and
1161 	 * bus device reset calls.  I don't bother
1162 	 * checking if this fails as, in most cases,
1163 	 * the system will function just fine without
1164 	 * them and the only alternative would be to
1165 	 * not attach the device on failure.
1166 	 */
1167 	xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1168 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1169 	csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1170 	csa.callback = daasync;
1171 	csa.callback_arg = periph;
1172 	xpt_action((union ccb *)&csa);
1173 	/*
1174 	 * Lock this peripheral until we are setup.
1175 	 * This first call can't block
1176 	 */
1177 	(void)cam_periph_lock(periph, PRIBIO);
1178 	xpt_schedule(periph, /*priority*/5);
1179 
1180 	return(CAM_REQ_CMP);
1181 }
1182 
1183 static void
1184 dastart(struct cam_periph *periph, union ccb *start_ccb)
1185 {
1186 	struct da_softc *softc;
1187 
1188 	softc = (struct da_softc *)periph->softc;
1189 
1190 
1191 	switch (softc->state) {
1192 	case DA_STATE_NORMAL:
1193 	{
1194 		/* Pull a buffer from the queue and get going on it */
1195 		struct bio *bp;
1196 		int s;
1197 
1198 		/*
1199 		 * See if there is a buf with work for us to do..
1200 		 */
1201 		s = splbio();
1202 		bp = bioq_first(&softc->bio_queue);
1203 		if (periph->immediate_priority <= periph->pinfo.priority) {
1204 			CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1205 					("queuing for immediate ccb\n"));
1206 			start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1207 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1208 					  periph_links.sle);
1209 			periph->immediate_priority = CAM_PRIORITY_NONE;
1210 			splx(s);
1211 			wakeup(&periph->ccb_list);
1212 		} else if (bp == NULL) {
1213 			splx(s);
1214 			xpt_release_ccb(start_ccb);
1215 		} else {
1216 			int oldspl;
1217 			u_int8_t tag_code;
1218 
1219 			bioq_remove(&softc->bio_queue, bp);
1220 
1221 			if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1222 				softc->flags &= ~DA_FLAG_NEED_OTAG;
1223 				softc->ordered_tag_count++;
1224 				tag_code = MSG_ORDERED_Q_TAG;
1225 			} else {
1226 				tag_code = MSG_SIMPLE_Q_TAG;
1227 			}
1228 			scsi_read_write(&start_ccb->csio,
1229 					/*retries*/da_retry_count,
1230 					/*cbfcnp*/dadone,
1231 					/*tag_action*/tag_code,
1232 					/*read_op*/bp->bio_cmd == BIO_READ,
1233 					/*byte2*/0,
1234 					softc->minimum_cmd_size,
1235 					/*lba*/bp->bio_pblkno,
1236 					/*block_count*/bp->bio_bcount /
1237 					softc->params.secsize,
1238 					/*data_ptr*/ bp->bio_data,
1239 					/*dxfer_len*/ bp->bio_bcount,
1240 					/*sense_len*/SSD_FULL_SIZE,
1241 					/*timeout*/da_default_timeout*1000);
1242 			start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1243 
1244 			/*
1245 			 * Block out any asyncronous callbacks
1246 			 * while we touch the pending ccb list.
1247 			 */
1248 			oldspl = splcam();
1249 			LIST_INSERT_HEAD(&softc->pending_ccbs,
1250 					 &start_ccb->ccb_h, periph_links.le);
1251 			softc->outstanding_cmds++;
1252 			splx(oldspl);
1253 
1254 			/* We expect a unit attention from this device */
1255 			if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1256 				start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1257 				softc->flags &= ~DA_FLAG_RETRY_UA;
1258 			}
1259 
1260 			start_ccb->ccb_h.ccb_bp = bp;
1261 			bp = bioq_first(&softc->bio_queue);
1262 			splx(s);
1263 
1264 			xpt_action(start_ccb);
1265 		}
1266 
1267 		if (bp != NULL) {
1268 			/* Have more work to do, so ensure we stay scheduled */
1269 			xpt_schedule(periph, /* XXX priority */1);
1270 		}
1271 		break;
1272 	}
1273 	case DA_STATE_PROBE:
1274 	{
1275 		struct ccb_scsiio *csio;
1276 		struct scsi_read_capacity_data *rcap;
1277 
1278 		rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1279 								M_TEMP,
1280 								M_NOWAIT);
1281 		if (rcap == NULL) {
1282 			printf("dastart: Couldn't malloc read_capacity data\n");
1283 			/* da_free_periph??? */
1284 			break;
1285 		}
1286 		csio = &start_ccb->csio;
1287 		scsi_read_capacity(csio,
1288 				   /*retries*/4,
1289 				   dadone,
1290 				   MSG_SIMPLE_Q_TAG,
1291 				   rcap,
1292 				   SSD_FULL_SIZE,
1293 				   /*timeout*/5000);
1294 		start_ccb->ccb_h.ccb_bp = NULL;
1295 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1296 		xpt_action(start_ccb);
1297 		break;
1298 	}
1299 	case DA_STATE_PROBE2:
1300 	{
1301 		struct ccb_scsiio *csio;
1302 		struct scsi_read_capacity_data_long *rcaplong;
1303 
1304 		rcaplong = (struct scsi_read_capacity_data_long *)
1305 			malloc(sizeof(*rcaplong), M_TEMP, M_NOWAIT);
1306 		if (rcaplong == NULL) {
1307 			printf("dastart: Couldn't malloc read_capacity data\n");
1308 			/* da_free_periph??? */
1309 			break;
1310 		}
1311 		csio = &start_ccb->csio;
1312 		scsi_read_capacity_16(csio,
1313 				      /*retries*/ 4,
1314 				      /*cbfcnp*/ dadone,
1315 				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
1316 				      /*lba*/ 0,
1317 				      /*reladr*/ 0,
1318 				      /*pmi*/ 0,
1319 				      rcaplong,
1320 				      /*sense_len*/ SSD_FULL_SIZE,
1321 				      /*timeout*/ 60000);
1322 		start_ccb->ccb_h.ccb_bp = NULL;
1323 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1324 		xpt_action(start_ccb);
1325 		break;
1326 	}
1327 	}
1328 }
1329 
1330 static int
1331 cmd6workaround(union ccb *ccb)
1332 {
1333 	struct scsi_rw_6 cmd6;
1334 	struct scsi_rw_10 *cmd10;
1335 	struct da_softc *softc;
1336 	u_int8_t *cdb;
1337 	int frozen;
1338 
1339 	cdb = ccb->csio.cdb_io.cdb_bytes;
1340 
1341 	/* Translation only possible if CDB is an array and cmd is R/W6 */
1342 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1343 	    (*cdb != READ_6 && *cdb != WRITE_6))
1344 		return 0;
1345 
1346 	xpt_print_path(ccb->ccb_h.path);
1347  	printf("READ(6)/WRITE(6) not supported, "
1348 	       "increasing minimum_cmd_size to 10.\n");
1349  	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1350 	softc->minimum_cmd_size = 10;
1351 
1352 	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1353 	cmd10 = (struct scsi_rw_10 *)cdb;
1354 	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1355 	cmd10->byte2 = 0;
1356 	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1357 	cmd10->reserved = 0;
1358 	scsi_ulto2b(cmd6.length, cmd10->length);
1359 	cmd10->control = cmd6.control;
1360 	ccb->csio.cdb_len = sizeof(*cmd10);
1361 
1362 	/* Requeue request, unfreezing queue if necessary */
1363 	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1364  	ccb->ccb_h.status = CAM_REQUEUE_REQ;
1365 	xpt_action(ccb);
1366 	if (frozen) {
1367 		cam_release_devq(ccb->ccb_h.path,
1368 				 /*relsim_flags*/0,
1369 				 /*reduction*/0,
1370 				 /*timeout*/0,
1371 				 /*getcount_only*/0);
1372 	}
1373 	return (ERESTART);
1374 }
1375 
1376 static void
1377 dadone(struct cam_periph *periph, union ccb *done_ccb)
1378 {
1379 	struct da_softc *softc;
1380 	struct ccb_scsiio *csio;
1381 
1382 	softc = (struct da_softc *)periph->softc;
1383 	csio = &done_ccb->csio;
1384 	switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1385 	case DA_CCB_BUFFER_IO:
1386 	{
1387 		struct bio *bp;
1388 		int    oldspl;
1389 
1390 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1391 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1392 			int error;
1393 			int s;
1394 			int sf;
1395 
1396 			if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1397 				sf = SF_RETRY_UA;
1398 			else
1399 				sf = 0;
1400 
1401 			error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1402 			if (error == ERESTART) {
1403 				/*
1404 				 * A retry was scheuled, so
1405 				 * just return.
1406 				 */
1407 				return;
1408 			}
1409 			if (error != 0) {
1410 
1411 				s = splbio();
1412 
1413 				if (error == ENXIO) {
1414 					/*
1415 					 * Catastrophic error.  Mark our pack as
1416 					 * invalid.
1417 					 */
1418 					/* XXX See if this is really a media
1419 					 *     change first.
1420 					 */
1421 					xpt_print_path(periph->path);
1422 					printf("Invalidating pack\n");
1423 					softc->flags |= DA_FLAG_PACK_INVALID;
1424 				}
1425 
1426 				/*
1427 				 * return all queued I/O with EIO, so that
1428 				 * the client can retry these I/Os in the
1429 				 * proper order should it attempt to recover.
1430 				 */
1431 				bioq_flush(&softc->bio_queue, NULL, EIO);
1432 				splx(s);
1433 				bp->bio_error = error;
1434 				bp->bio_resid = bp->bio_bcount;
1435 				bp->bio_flags |= BIO_ERROR;
1436 			} else {
1437 				bp->bio_resid = csio->resid;
1438 				bp->bio_error = 0;
1439 				if (bp->bio_resid != 0)
1440 					bp->bio_flags |= BIO_ERROR;
1441 			}
1442 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1443 				cam_release_devq(done_ccb->ccb_h.path,
1444 						 /*relsim_flags*/0,
1445 						 /*reduction*/0,
1446 						 /*timeout*/0,
1447 						 /*getcount_only*/0);
1448 		} else {
1449 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1450 				panic("REQ_CMP with QFRZN");
1451 			bp->bio_resid = csio->resid;
1452 			if (csio->resid > 0)
1453 				bp->bio_flags |= BIO_ERROR;
1454 		}
1455 
1456 		/*
1457 		 * Block out any asyncronous callbacks
1458 		 * while we touch the pending ccb list.
1459 		 */
1460 		oldspl = splcam();
1461 		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1462 		softc->outstanding_cmds--;
1463 		if (softc->outstanding_cmds == 0)
1464 			softc->flags |= DA_FLAG_WENT_IDLE;
1465 		splx(oldspl);
1466 
1467 		biodone(bp);
1468 		break;
1469 	}
1470 	case DA_CCB_PROBE:
1471 	case DA_CCB_PROBE2:
1472 	{
1473 		struct	   scsi_read_capacity_data *rdcap;
1474 		struct     scsi_read_capacity_data_long *rcaplong;
1475 		char	   announce_buf[80];
1476 
1477 		rdcap = NULL;
1478 		rcaplong = NULL;
1479 		if (softc->state == DA_STATE_PROBE)
1480 			rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1481 		else
1482 			rcaplong = (struct scsi_read_capacity_data_long *)
1483 				csio->data_ptr;
1484 
1485 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1486 			struct disk_params *dp;
1487 			uint32_t block_size;
1488 			uint64_t maxsector;
1489 
1490 			if (softc->state == DA_STATE_PROBE) {
1491 				block_size = scsi_4btoul(rdcap->length);
1492 				maxsector = scsi_4btoul(rdcap->addr);
1493 
1494 				/*
1495 				 * According to SBC-2, if the standard 10
1496 				 * byte READ CAPACITY command returns 2^32,
1497 				 * we should issue the 16 byte version of
1498 				 * the command, since the device in question
1499 				 * has more sectors than can be represented
1500 				 * with the short version of the command.
1501 				 */
1502 				if (maxsector == 0xffffffff) {
1503 					softc->state = DA_STATE_PROBE2;
1504 					free(rdcap, M_TEMP);
1505 					xpt_release_ccb(done_ccb);
1506 					xpt_schedule(periph, /*priority*/5);
1507 					return;
1508 				}
1509 			} else {
1510 				block_size = scsi_4btoul(rcaplong->length);
1511 				maxsector = scsi_8btou64(rcaplong->addr);
1512 			}
1513 			dasetgeom(periph, block_size, maxsector);
1514 			dp = &softc->params;
1515 			snprintf(announce_buf, sizeof(announce_buf),
1516 			        "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1517 				(uintmax_t) (((uintmax_t)dp->secsize *
1518 				dp->sectors) / (1024*1024)),
1519 			        (uintmax_t)dp->sectors,
1520 				dp->secsize, dp->heads, dp->secs_per_track,
1521 				dp->cylinders);
1522 		} else {
1523 			int	error;
1524 
1525 			announce_buf[0] = '\0';
1526 
1527 			/*
1528 			 * Retry any UNIT ATTENTION type errors.  They
1529 			 * are expected at boot.
1530 			 */
1531 			error = daerror(done_ccb, CAM_RETRY_SELTO,
1532 					SF_RETRY_UA|SF_NO_PRINT);
1533 			if (error == ERESTART) {
1534 				/*
1535 				 * A retry was scheuled, so
1536 				 * just return.
1537 				 */
1538 				return;
1539 			} else if (error != 0) {
1540 				struct scsi_sense_data *sense;
1541 				int asc, ascq;
1542 				int sense_key, error_code;
1543 				int have_sense;
1544 				cam_status status;
1545 				struct ccb_getdev cgd;
1546 
1547 				/* Don't wedge this device's queue */
1548 				status = done_ccb->ccb_h.status;
1549 				if ((status & CAM_DEV_QFRZN) != 0)
1550 					cam_release_devq(done_ccb->ccb_h.path,
1551 							 /*relsim_flags*/0,
1552 							 /*reduction*/0,
1553 							 /*timeout*/0,
1554 							 /*getcount_only*/0);
1555 
1556 
1557 				xpt_setup_ccb(&cgd.ccb_h,
1558 					      done_ccb->ccb_h.path,
1559 					      /* priority */ 1);
1560 				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1561 				xpt_action((union ccb *)&cgd);
1562 
1563 				if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1564 				 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1565 				 || ((status & CAM_AUTOSNS_VALID) == 0))
1566 					have_sense = FALSE;
1567 				else
1568 					have_sense = TRUE;
1569 
1570 				if (have_sense) {
1571 					sense = &csio->sense_data;
1572 					scsi_extract_sense(sense, &error_code,
1573 							   &sense_key,
1574 							   &asc, &ascq);
1575 				}
1576 				/*
1577 				 * Attach to anything that claims to be a
1578 				 * direct access or optical disk device,
1579 				 * as long as it doesn't return a "Logical
1580 				 * unit not supported" (0x25) error.
1581 				 */
1582 				if ((have_sense) && (asc != 0x25)
1583 				 && (error_code == SSD_CURRENT_ERROR)) {
1584 					const char *sense_key_desc;
1585 					const char *asc_desc;
1586 
1587 					scsi_sense_desc(sense_key, asc, ascq,
1588 							&cgd.inq_data,
1589 							&sense_key_desc,
1590 							&asc_desc);
1591 					snprintf(announce_buf,
1592 					    sizeof(announce_buf),
1593 						"Attempt to query device "
1594 						"size failed: %s, %s",
1595 						sense_key_desc,
1596 						asc_desc);
1597 				} else {
1598 					if (have_sense)
1599 						scsi_sense_print(
1600 							&done_ccb->csio);
1601 					else {
1602 						xpt_print_path(periph->path);
1603 						printf("got CAM status %#x\n",
1604 						       done_ccb->ccb_h.status);
1605 					}
1606 
1607 					xpt_print_path(periph->path);
1608 					printf("fatal error, failed"
1609 					       " to attach to device\n");
1610 
1611 					/*
1612 					 * Free up resources.
1613 					 */
1614 					cam_periph_invalidate(periph);
1615 				}
1616 			}
1617 		}
1618 		free(csio->data_ptr, M_TEMP);
1619 		if (announce_buf[0] != '\0') {
1620 			xpt_announce_periph(periph, announce_buf);
1621 			/*
1622 			 * Create our sysctl variables, now that we know
1623 			 * we have successfully attached.
1624 			 */
1625 			taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1626 		}
1627 		softc->state = DA_STATE_NORMAL;
1628 		/*
1629 		 * Since our peripheral may be invalidated by an error
1630 		 * above or an external event, we must release our CCB
1631 		 * before releasing the probe lock on the peripheral.
1632 		 * The peripheral will only go away once the last lock
1633 		 * is removed, and we need it around for the CCB release
1634 		 * operation.
1635 		 */
1636 		xpt_release_ccb(done_ccb);
1637 		cam_periph_unlock(periph);
1638 		return;
1639 	}
1640 	case DA_CCB_WAITING:
1641 	{
1642 		/* Caller will release the CCB */
1643 		wakeup(&done_ccb->ccb_h.cbfcnp);
1644 		return;
1645 	}
1646 	case DA_CCB_DUMP:
1647 		/* No-op.  We're polling */
1648 		return;
1649 	default:
1650 		break;
1651 	}
1652 	xpt_release_ccb(done_ccb);
1653 }
1654 
1655 static int
1656 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1657 {
1658 	struct da_softc	  *softc;
1659 	struct cam_periph *periph;
1660 	int error;
1661 
1662 	periph = xpt_path_periph(ccb->ccb_h.path);
1663 	softc = (struct da_softc *)periph->softc;
1664 
1665  	/*
1666 	 * Automatically detect devices that do not support
1667  	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1668  	 */
1669 	error = 0;
1670 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1671 		error = cmd6workaround(ccb);
1672 	} else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1673 		   CAM_SCSI_STATUS_ERROR)
1674 	 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1675 	 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1676 	 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1677 	 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1678 		int sense_key, error_code, asc, ascq;
1679 
1680  		scsi_extract_sense(&ccb->csio.sense_data,
1681 				   &error_code, &sense_key, &asc, &ascq);
1682 		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1683  			error = cmd6workaround(ccb);
1684 	}
1685 	if (error == ERESTART)
1686 		return (ERESTART);
1687 
1688 	/*
1689 	 * XXX
1690 	 * Until we have a better way of doing pack validation,
1691 	 * don't treat UAs as errors.
1692 	 */
1693 	sense_flags |= SF_RETRY_UA;
1694 	return(cam_periph_error(ccb, cam_flags, sense_flags,
1695 				&softc->saved_ccb));
1696 }
1697 
1698 static void
1699 daprevent(struct cam_periph *periph, int action)
1700 {
1701 	struct	da_softc *softc;
1702 	union	ccb *ccb;
1703 	int	error;
1704 
1705 	softc = (struct da_softc *)periph->softc;
1706 
1707 	if (((action == PR_ALLOW)
1708 	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1709 	 || ((action == PR_PREVENT)
1710 	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1711 		return;
1712 	}
1713 
1714 	ccb = cam_periph_getccb(periph, /*priority*/1);
1715 
1716 	scsi_prevent(&ccb->csio,
1717 		     /*retries*/1,
1718 		     /*cbcfp*/dadone,
1719 		     MSG_SIMPLE_Q_TAG,
1720 		     action,
1721 		     SSD_FULL_SIZE,
1722 		     5000);
1723 
1724 	error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1725 				  SF_RETRY_UA, softc->disk->d_devstat);
1726 
1727 	if (error == 0) {
1728 		if (action == PR_ALLOW)
1729 			softc->flags &= ~DA_FLAG_PACK_LOCKED;
1730 		else
1731 			softc->flags |= DA_FLAG_PACK_LOCKED;
1732 	}
1733 
1734 	xpt_release_ccb(ccb);
1735 }
1736 
1737 static int
1738 dagetcapacity(struct cam_periph *periph)
1739 {
1740 	struct da_softc *softc;
1741 	union ccb *ccb;
1742 	struct scsi_read_capacity_data *rcap;
1743 	struct scsi_read_capacity_data_long *rcaplong;
1744 	uint32_t block_len;
1745 	uint64_t maxsector;
1746 	int error;
1747 
1748 	softc = (struct da_softc *)periph->softc;
1749 	block_len = 0;
1750 	maxsector = 0;
1751 	error = 0;
1752 
1753 	/* Do a read capacity */
1754 	rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
1755 							M_TEMP,
1756 							M_WAITOK);
1757 
1758 	ccb = cam_periph_getccb(periph, /*priority*/1);
1759 	scsi_read_capacity(&ccb->csio,
1760 			   /*retries*/4,
1761 			   /*cbfncp*/dadone,
1762 			   MSG_SIMPLE_Q_TAG,
1763 			   rcap,
1764 			   SSD_FULL_SIZE,
1765 			   /*timeout*/60000);
1766 	ccb->ccb_h.ccb_bp = NULL;
1767 
1768 	error = cam_periph_runccb(ccb, daerror,
1769 				  /*cam_flags*/CAM_RETRY_SELTO,
1770 				  /*sense_flags*/SF_RETRY_UA,
1771 				  softc->disk->d_devstat);
1772 
1773 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1774 		cam_release_devq(ccb->ccb_h.path,
1775 				 /*relsim_flags*/0,
1776 				 /*reduction*/0,
1777 				 /*timeout*/0,
1778 				 /*getcount_only*/0);
1779 
1780 	if (error == 0) {
1781 		block_len = scsi_4btoul(rcap->length);
1782 		maxsector = scsi_4btoul(rcap->addr);
1783 
1784 		if (maxsector != 0xffffffff)
1785 			goto done;
1786 	} else
1787 		goto done;
1788 
1789 	rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1790 
1791 	scsi_read_capacity_16(&ccb->csio,
1792 			      /*retries*/ 4,
1793 			      /*cbfcnp*/ dadone,
1794 			      /*tag_action*/ MSG_SIMPLE_Q_TAG,
1795 			      /*lba*/ 0,
1796 			      /*reladr*/ 0,
1797 			      /*pmi*/ 0,
1798 			      rcaplong,
1799 			      /*sense_len*/ SSD_FULL_SIZE,
1800 			      /*timeout*/ 60000);
1801 	ccb->ccb_h.ccb_bp = NULL;
1802 
1803 	error = cam_periph_runccb(ccb, daerror,
1804 				  /*cam_flags*/CAM_RETRY_SELTO,
1805 				  /*sense_flags*/SF_RETRY_UA,
1806 				  softc->disk->d_devstat);
1807 
1808 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1809 		cam_release_devq(ccb->ccb_h.path,
1810 				 /*relsim_flags*/0,
1811 				 /*reduction*/0,
1812 				 /*timeout*/0,
1813 				 /*getcount_only*/0);
1814 
1815 	if (error == 0) {
1816 		block_len = scsi_4btoul(rcaplong->length);
1817 		maxsector = scsi_8btou64(rcaplong->addr);
1818 	}
1819 
1820 done:
1821 
1822 	if (error == 0)
1823 		dasetgeom(periph, block_len, maxsector);
1824 
1825 	xpt_release_ccb(ccb);
1826 
1827 	free(rcap, M_TEMP);
1828 
1829 	return (error);
1830 }
1831 
1832 static void
1833 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
1834 {
1835 	struct ccb_calc_geometry ccg;
1836 	struct da_softc *softc;
1837 	struct disk_params *dp;
1838 
1839 	softc = (struct da_softc *)periph->softc;
1840 
1841 	dp = &softc->params;
1842 	dp->secsize = block_len;
1843 	dp->sectors = maxsector + 1;
1844 	/*
1845 	 * Have the controller provide us with a geometry
1846 	 * for this disk.  The only time the geometry
1847 	 * matters is when we boot and the controller
1848 	 * is the only one knowledgeable enough to come
1849 	 * up with something that will make this a bootable
1850 	 * device.
1851 	 */
1852 	xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1853 	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1854 	ccg.block_size = dp->secsize;
1855 	ccg.volume_size = dp->sectors;
1856 	ccg.heads = 0;
1857 	ccg.secs_per_track = 0;
1858 	ccg.cylinders = 0;
1859 	xpt_action((union ccb*)&ccg);
1860 	dp->heads = ccg.heads;
1861 	dp->secs_per_track = ccg.secs_per_track;
1862 	dp->cylinders = ccg.cylinders;
1863 }
1864 
1865 static void
1866 dasendorderedtag(void *arg)
1867 {
1868 	struct da_softc *softc;
1869 	int s;
1870 
1871 	for (softc = SLIST_FIRST(&softc_list);
1872 	     softc != NULL;
1873 	     softc = SLIST_NEXT(softc, links)) {
1874 		s = splsoftcam();
1875 		if ((softc->ordered_tag_count == 0)
1876 		 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1877 			softc->flags |= DA_FLAG_NEED_OTAG;
1878 		}
1879 		if (softc->outstanding_cmds > 0)
1880 			softc->flags &= ~DA_FLAG_WENT_IDLE;
1881 
1882 		softc->ordered_tag_count = 0;
1883 		splx(s);
1884 	}
1885 	/* Queue us up again */
1886 	timeout(dasendorderedtag, NULL,
1887 		(da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
1888 }
1889 
1890 /*
1891  * Step through all DA peripheral drivers, and if the device is still open,
1892  * sync the disk cache to physical media.
1893  */
1894 static void
1895 dashutdown(void * arg, int howto)
1896 {
1897 	struct cam_periph *periph;
1898 	struct da_softc *softc;
1899 
1900 	TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
1901 		union ccb ccb;
1902 		softc = (struct da_softc *)periph->softc;
1903 
1904 		/*
1905 		 * We only sync the cache if the drive is still open, and
1906 		 * if the drive is capable of it..
1907 		 */
1908 		if (((softc->flags & DA_FLAG_OPEN) == 0)
1909 		 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1910 			continue;
1911 
1912 		xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1913 
1914 		ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1915 		scsi_synchronize_cache(&ccb.csio,
1916 				       /*retries*/1,
1917 				       /*cbfcnp*/dadone,
1918 				       MSG_SIMPLE_Q_TAG,
1919 				       /*begin_lba*/0, /* whole disk */
1920 				       /*lb_count*/0,
1921 				       SSD_FULL_SIZE,
1922 				       60 * 60 * 1000);
1923 
1924 		xpt_polled_action(&ccb);
1925 
1926 		if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1927 			if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1928 			     CAM_SCSI_STATUS_ERROR)
1929 			 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1930 				int error_code, sense_key, asc, ascq;
1931 
1932 				scsi_extract_sense(&ccb.csio.sense_data,
1933 						   &error_code, &sense_key,
1934 						   &asc, &ascq);
1935 
1936 				if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1937 					scsi_sense_print(&ccb.csio);
1938 			} else {
1939 				xpt_print_path(periph->path);
1940 				printf("Synchronize cache failed, status "
1941 				       "== 0x%x, scsi status == 0x%x\n",
1942 				       ccb.ccb_h.status, ccb.csio.scsi_status);
1943 			}
1944 		}
1945 
1946 		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1947 			cam_release_devq(ccb.ccb_h.path,
1948 					 /*relsim_flags*/0,
1949 					 /*reduction*/0,
1950 					 /*timeout*/0,
1951 					 /*getcount_only*/0);
1952 
1953 	}
1954 }
1955 
1956 #else /* !_KERNEL */
1957 
1958 /*
1959  * XXX This is only left out of the kernel build to silence warnings.  If,
1960  * for some reason this function is used in the kernel, the ifdefs should
1961  * be moved so it is included both in the kernel and userland.
1962  */
1963 void
1964 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1965 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
1966 		 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1967 		 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1968 		 u_int32_t timeout)
1969 {
1970 	struct scsi_format_unit *scsi_cmd;
1971 
1972 	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1973 	scsi_cmd->opcode = FORMAT_UNIT;
1974 	scsi_cmd->byte2 = byte2;
1975 	scsi_ulto2b(ileave, scsi_cmd->interleave);
1976 
1977 	cam_fill_csio(csio,
1978 		      retries,
1979 		      cbfcnp,
1980 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1981 		      tag_action,
1982 		      data_ptr,
1983 		      dxfer_len,
1984 		      sense_len,
1985 		      sizeof(*scsi_cmd),
1986 		      timeout);
1987 }
1988 
1989 #endif /* _KERNEL */
1990