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