xref: /freebsd/sys/cam/scsi/scsi_da.c (revision 52ec752989b2e6d4e9a59a8ff25d8ff596d85e62)
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 	unit = periph->unit_number;
468 	if (periph == NULL) {
469 		splx(s);
470 		return (ENXIO);
471 	}
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.d_open = daopen;
1095 	softc->disk.d_close = daclose;
1096 	softc->disk.d_strategy = dastrategy;
1097 	softc->disk.d_dump = dadump;
1098 	softc->disk.d_name = "da";
1099 	softc->disk.d_drv1 = periph;
1100 	softc->disk.d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */
1101 	disk_create(periph->unit_number, &softc->disk, 0, NULL, NULL);
1102 
1103 	/*
1104 	 * Add async callbacks for bus reset and
1105 	 * bus device reset calls.  I don't bother
1106 	 * checking if this fails as, in most cases,
1107 	 * the system will function just fine without
1108 	 * them and the only alternative would be to
1109 	 * not attach the device on failure.
1110 	 */
1111 	xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1112 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1113 	csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1114 	csa.callback = daasync;
1115 	csa.callback_arg = periph;
1116 	xpt_action((union ccb *)&csa);
1117 	/*
1118 	 * Lock this peripheral until we are setup.
1119 	 * This first call can't block
1120 	 */
1121 	(void)cam_periph_lock(periph, PRIBIO);
1122 	xpt_schedule(periph, /*priority*/5);
1123 
1124 	return(CAM_REQ_CMP);
1125 }
1126 
1127 static void
1128 dastart(struct cam_periph *periph, union ccb *start_ccb)
1129 {
1130 	struct da_softc *softc;
1131 
1132 	softc = (struct da_softc *)periph->softc;
1133 
1134 
1135 	switch (softc->state) {
1136 	case DA_STATE_NORMAL:
1137 	{
1138 		/* Pull a buffer from the queue and get going on it */
1139 		struct bio *bp;
1140 		int s;
1141 
1142 		/*
1143 		 * See if there is a buf with work for us to do..
1144 		 */
1145 		s = splbio();
1146 		bp = bioq_first(&softc->bio_queue);
1147 		if (periph->immediate_priority <= periph->pinfo.priority) {
1148 			CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1149 					("queuing for immediate ccb\n"));
1150 			start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1151 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1152 					  periph_links.sle);
1153 			periph->immediate_priority = CAM_PRIORITY_NONE;
1154 			splx(s);
1155 			wakeup(&periph->ccb_list);
1156 		} else if (bp == NULL) {
1157 			splx(s);
1158 			xpt_release_ccb(start_ccb);
1159 		} else {
1160 			int oldspl;
1161 			u_int8_t tag_code;
1162 
1163 			bioq_remove(&softc->bio_queue, bp);
1164 
1165 			if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1166 				softc->flags &= ~DA_FLAG_NEED_OTAG;
1167 				softc->ordered_tag_count++;
1168 				tag_code = MSG_ORDERED_Q_TAG;
1169 			} else {
1170 				tag_code = MSG_SIMPLE_Q_TAG;
1171 			}
1172 			scsi_read_write(&start_ccb->csio,
1173 					/*retries*/da_retry_count,
1174 					/*cbfcnp*/dadone,
1175 					/*tag_action*/tag_code,
1176 					/*read_op*/bp->bio_cmd == BIO_READ,
1177 					/*byte2*/0,
1178 					softc->minimum_cmd_size,
1179 					/*lba*/bp->bio_pblkno,
1180 					/*block_count*/bp->bio_bcount /
1181 					softc->params.secsize,
1182 					/*data_ptr*/ bp->bio_data,
1183 					/*dxfer_len*/ bp->bio_bcount,
1184 					/*sense_len*/SSD_FULL_SIZE,
1185 					/*timeout*/da_default_timeout*1000);
1186 			start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1187 
1188 			/*
1189 			 * Block out any asyncronous callbacks
1190 			 * while we touch the pending ccb list.
1191 			 */
1192 			oldspl = splcam();
1193 			LIST_INSERT_HEAD(&softc->pending_ccbs,
1194 					 &start_ccb->ccb_h, periph_links.le);
1195 			softc->outstanding_cmds++;
1196 			splx(oldspl);
1197 
1198 			/* We expect a unit attention from this device */
1199 			if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1200 				start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1201 				softc->flags &= ~DA_FLAG_RETRY_UA;
1202 			}
1203 
1204 			start_ccb->ccb_h.ccb_bp = bp;
1205 			bp = bioq_first(&softc->bio_queue);
1206 			splx(s);
1207 
1208 			xpt_action(start_ccb);
1209 		}
1210 
1211 		if (bp != NULL) {
1212 			/* Have more work to do, so ensure we stay scheduled */
1213 			xpt_schedule(periph, /* XXX priority */1);
1214 		}
1215 		break;
1216 	}
1217 	case DA_STATE_PROBE:
1218 	{
1219 		struct ccb_scsiio *csio;
1220 		struct scsi_read_capacity_data *rcap;
1221 
1222 		rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1223 								M_TEMP,
1224 								M_NOWAIT);
1225 		if (rcap == NULL) {
1226 			printf("dastart: Couldn't malloc read_capacity data\n");
1227 			/* da_free_periph??? */
1228 			break;
1229 		}
1230 		csio = &start_ccb->csio;
1231 		scsi_read_capacity(csio,
1232 				   /*retries*/4,
1233 				   dadone,
1234 				   MSG_SIMPLE_Q_TAG,
1235 				   rcap,
1236 				   SSD_FULL_SIZE,
1237 				   /*timeout*/5000);
1238 		start_ccb->ccb_h.ccb_bp = NULL;
1239 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1240 		xpt_action(start_ccb);
1241 		break;
1242 	}
1243 	case DA_STATE_PROBE2:
1244 	{
1245 		struct ccb_scsiio *csio;
1246 		struct scsi_read_capacity_data_long *rcaplong;
1247 
1248 		rcaplong = (struct scsi_read_capacity_data_long *)
1249 			malloc(sizeof(*rcaplong), M_TEMP, M_NOWAIT);
1250 		if (rcaplong == NULL) {
1251 			printf("dastart: Couldn't malloc read_capacity data\n");
1252 			/* da_free_periph??? */
1253 			break;
1254 		}
1255 		csio = &start_ccb->csio;
1256 		scsi_read_capacity_16(csio,
1257 				      /*retries*/ 4,
1258 				      /*cbfcnp*/ dadone,
1259 				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
1260 				      /*lba*/ 0,
1261 				      /*reladr*/ 0,
1262 				      /*pmi*/ 0,
1263 				      rcaplong,
1264 				      /*sense_len*/ SSD_FULL_SIZE,
1265 				      /*timeout*/ 60000);
1266 		start_ccb->ccb_h.ccb_bp = NULL;
1267 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1268 		xpt_action(start_ccb);
1269 		break;
1270 	}
1271 	}
1272 }
1273 
1274 static int
1275 cmd6workaround(union ccb *ccb)
1276 {
1277 	struct scsi_rw_6 cmd6;
1278 	struct scsi_rw_10 *cmd10;
1279 	struct da_softc *softc;
1280 	u_int8_t *cdb;
1281 	int frozen;
1282 
1283 	cdb = ccb->csio.cdb_io.cdb_bytes;
1284 
1285 	/* Translation only possible if CDB is an array and cmd is R/W6 */
1286 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1287 	    (*cdb != READ_6 && *cdb != WRITE_6))
1288 		return 0;
1289 
1290 	xpt_print_path(ccb->ccb_h.path);
1291  	printf("READ(6)/WRITE(6) not supported, "
1292 	       "increasing minimum_cmd_size to 10.\n");
1293  	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1294 	softc->minimum_cmd_size = 10;
1295 
1296 	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1297 	cmd10 = (struct scsi_rw_10 *)cdb;
1298 	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1299 	cmd10->byte2 = 0;
1300 	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1301 	cmd10->reserved = 0;
1302 	scsi_ulto2b(cmd6.length, cmd10->length);
1303 	cmd10->control = cmd6.control;
1304 	ccb->csio.cdb_len = sizeof(*cmd10);
1305 
1306 	/* Requeue request, unfreezing queue if necessary */
1307 	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1308  	ccb->ccb_h.status = CAM_REQUEUE_REQ;
1309 	xpt_action(ccb);
1310 	if (frozen) {
1311 		cam_release_devq(ccb->ccb_h.path,
1312 				 /*relsim_flags*/0,
1313 				 /*reduction*/0,
1314 				 /*timeout*/0,
1315 				 /*getcount_only*/0);
1316 	}
1317 	return (ERESTART);
1318 }
1319 
1320 static void
1321 dadone(struct cam_periph *periph, union ccb *done_ccb)
1322 {
1323 	struct da_softc *softc;
1324 	struct ccb_scsiio *csio;
1325 
1326 	softc = (struct da_softc *)periph->softc;
1327 	csio = &done_ccb->csio;
1328 	switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1329 	case DA_CCB_BUFFER_IO:
1330 	{
1331 		struct bio *bp;
1332 		int    oldspl;
1333 
1334 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1335 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1336 			int error;
1337 			int s;
1338 			int sf;
1339 
1340 			if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1341 				sf = SF_RETRY_UA;
1342 			else
1343 				sf = 0;
1344 
1345 			error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1346 			if (error == ERESTART) {
1347 				/*
1348 				 * A retry was scheuled, so
1349 				 * just return.
1350 				 */
1351 				return;
1352 			}
1353 			if (error != 0) {
1354 
1355 				s = splbio();
1356 
1357 				if (error == ENXIO) {
1358 					/*
1359 					 * Catastrophic error.  Mark our pack as
1360 					 * invalid.
1361 					 */
1362 					/* XXX See if this is really a media
1363 					 *     change first.
1364 					 */
1365 					xpt_print_path(periph->path);
1366 					printf("Invalidating pack\n");
1367 					softc->flags |= DA_FLAG_PACK_INVALID;
1368 				}
1369 
1370 				/*
1371 				 * return all queued I/O with EIO, so that
1372 				 * the client can retry these I/Os in the
1373 				 * proper order should it attempt to recover.
1374 				 */
1375 				bioq_flush(&softc->bio_queue, NULL, EIO);
1376 				splx(s);
1377 				bp->bio_error = error;
1378 				bp->bio_resid = bp->bio_bcount;
1379 				bp->bio_flags |= BIO_ERROR;
1380 			} else {
1381 				bp->bio_resid = csio->resid;
1382 				bp->bio_error = 0;
1383 				if (bp->bio_resid != 0)
1384 					bp->bio_flags |= BIO_ERROR;
1385 			}
1386 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1387 				cam_release_devq(done_ccb->ccb_h.path,
1388 						 /*relsim_flags*/0,
1389 						 /*reduction*/0,
1390 						 /*timeout*/0,
1391 						 /*getcount_only*/0);
1392 		} else {
1393 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1394 				panic("REQ_CMP with QFRZN");
1395 			bp->bio_resid = csio->resid;
1396 			if (csio->resid > 0)
1397 				bp->bio_flags |= BIO_ERROR;
1398 		}
1399 
1400 		/*
1401 		 * Block out any asyncronous callbacks
1402 		 * while we touch the pending ccb list.
1403 		 */
1404 		oldspl = splcam();
1405 		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1406 		softc->outstanding_cmds--;
1407 		if (softc->outstanding_cmds == 0)
1408 			softc->flags |= DA_FLAG_WENT_IDLE;
1409 		splx(oldspl);
1410 
1411 		biodone(bp);
1412 		break;
1413 	}
1414 	case DA_CCB_PROBE:
1415 	case DA_CCB_PROBE2:
1416 	{
1417 		struct	   scsi_read_capacity_data *rdcap;
1418 		struct     scsi_read_capacity_data_long *rcaplong;
1419 		char	   announce_buf[80];
1420 
1421 		rdcap = NULL;
1422 		rcaplong = NULL;
1423 		if (softc->state == DA_STATE_PROBE)
1424 			rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1425 		else
1426 			rcaplong = (struct scsi_read_capacity_data_long *)
1427 				csio->data_ptr;
1428 
1429 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1430 			struct disk_params *dp;
1431 			uint32_t block_size;
1432 			uint64_t maxsector;
1433 
1434 			if (softc->state == DA_STATE_PROBE) {
1435 				block_size = scsi_4btoul(rdcap->length);
1436 				maxsector = scsi_4btoul(rdcap->addr);
1437 
1438 				/*
1439 				 * According to SBC-2, if the standard 10
1440 				 * byte READ CAPACITY command returns 2^32,
1441 				 * we should issue the 16 byte version of
1442 				 * the command, since the device in question
1443 				 * has more sectors than can be represented
1444 				 * with the short version of the command.
1445 				 */
1446 				if (maxsector == 0xffffffff) {
1447 					softc->state = DA_STATE_PROBE2;
1448 					free(rdcap, M_TEMP);
1449 					xpt_release_ccb(done_ccb);
1450 					xpt_schedule(periph, /*priority*/5);
1451 					return;
1452 				}
1453 			} else {
1454 				block_size = scsi_4btoul(rcaplong->length);
1455 				maxsector = scsi_8btou64(rcaplong->addr);
1456 			}
1457 			dasetgeom(periph, block_size, maxsector);
1458 			dp = &softc->params;
1459 			snprintf(announce_buf, sizeof(announce_buf),
1460 			        "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1461 				(uintmax_t) (((uintmax_t)dp->secsize *
1462 				dp->sectors) / (1024*1024)),
1463 			        (uintmax_t)dp->sectors,
1464 				dp->secsize, dp->heads, dp->secs_per_track,
1465 				dp->cylinders);
1466 		} else {
1467 			int	error;
1468 
1469 			announce_buf[0] = '\0';
1470 
1471 			/*
1472 			 * Retry any UNIT ATTENTION type errors.  They
1473 			 * are expected at boot.
1474 			 */
1475 			error = daerror(done_ccb, CAM_RETRY_SELTO,
1476 					SF_RETRY_UA|SF_NO_PRINT);
1477 			if (error == ERESTART) {
1478 				/*
1479 				 * A retry was scheuled, so
1480 				 * just return.
1481 				 */
1482 				return;
1483 			} else if (error != 0) {
1484 				struct scsi_sense_data *sense;
1485 				int asc, ascq;
1486 				int sense_key, error_code;
1487 				int have_sense;
1488 				cam_status status;
1489 				struct ccb_getdev cgd;
1490 
1491 				/* Don't wedge this device's queue */
1492 				status = done_ccb->ccb_h.status;
1493 				if ((status & CAM_DEV_QFRZN) != 0)
1494 					cam_release_devq(done_ccb->ccb_h.path,
1495 							 /*relsim_flags*/0,
1496 							 /*reduction*/0,
1497 							 /*timeout*/0,
1498 							 /*getcount_only*/0);
1499 
1500 
1501 				xpt_setup_ccb(&cgd.ccb_h,
1502 					      done_ccb->ccb_h.path,
1503 					      /* priority */ 1);
1504 				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1505 				xpt_action((union ccb *)&cgd);
1506 
1507 				if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1508 				 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1509 				 || ((status & CAM_AUTOSNS_VALID) == 0))
1510 					have_sense = FALSE;
1511 				else
1512 					have_sense = TRUE;
1513 
1514 				if (have_sense) {
1515 					sense = &csio->sense_data;
1516 					scsi_extract_sense(sense, &error_code,
1517 							   &sense_key,
1518 							   &asc, &ascq);
1519 				}
1520 				/*
1521 				 * Attach to anything that claims to be a
1522 				 * direct access or optical disk device,
1523 				 * as long as it doesn't return a "Logical
1524 				 * unit not supported" (0x25) error.
1525 				 */
1526 				if ((have_sense) && (asc != 0x25)
1527 				 && (error_code == SSD_CURRENT_ERROR)) {
1528 					const char *sense_key_desc;
1529 					const char *asc_desc;
1530 
1531 					scsi_sense_desc(sense_key, asc, ascq,
1532 							&cgd.inq_data,
1533 							&sense_key_desc,
1534 							&asc_desc);
1535 					snprintf(announce_buf,
1536 					    sizeof(announce_buf),
1537 						"Attempt to query device "
1538 						"size failed: %s, %s",
1539 						sense_key_desc,
1540 						asc_desc);
1541 				} else {
1542 					if (have_sense)
1543 						scsi_sense_print(
1544 							&done_ccb->csio);
1545 					else {
1546 						xpt_print_path(periph->path);
1547 						printf("got CAM status %#x\n",
1548 						       done_ccb->ccb_h.status);
1549 					}
1550 
1551 					xpt_print_path(periph->path);
1552 					printf("fatal error, failed"
1553 					       " to attach to device\n");
1554 
1555 					/*
1556 					 * Free up resources.
1557 					 */
1558 					cam_periph_invalidate(periph);
1559 				}
1560 			}
1561 		}
1562 		free(csio->data_ptr, M_TEMP);
1563 		if (announce_buf[0] != '\0') {
1564 			xpt_announce_periph(periph, announce_buf);
1565 			/*
1566 			 * Create our sysctl variables, now that we know
1567 			 * we have successfully attached.
1568 			 */
1569 			taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1570 		}
1571 		softc->state = DA_STATE_NORMAL;
1572 		/*
1573 		 * Since our peripheral may be invalidated by an error
1574 		 * above or an external event, we must release our CCB
1575 		 * before releasing the probe lock on the peripheral.
1576 		 * The peripheral will only go away once the last lock
1577 		 * is removed, and we need it around for the CCB release
1578 		 * operation.
1579 		 */
1580 		xpt_release_ccb(done_ccb);
1581 		cam_periph_unlock(periph);
1582 		return;
1583 	}
1584 	case DA_CCB_WAITING:
1585 	{
1586 		/* Caller will release the CCB */
1587 		wakeup(&done_ccb->ccb_h.cbfcnp);
1588 		return;
1589 	}
1590 	case DA_CCB_DUMP:
1591 		/* No-op.  We're polling */
1592 		return;
1593 	default:
1594 		break;
1595 	}
1596 	xpt_release_ccb(done_ccb);
1597 }
1598 
1599 static int
1600 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1601 {
1602 	struct da_softc	  *softc;
1603 	struct cam_periph *periph;
1604 	int error;
1605 
1606 	periph = xpt_path_periph(ccb->ccb_h.path);
1607 	softc = (struct da_softc *)periph->softc;
1608 
1609  	/*
1610 	 * Automatically detect devices that do not support
1611  	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1612  	 */
1613 	error = 0;
1614 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1615 		error = cmd6workaround(ccb);
1616 	} else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1617 		   CAM_SCSI_STATUS_ERROR)
1618 	 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1619 	 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1620 	 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1621 	 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1622 		int sense_key, error_code, asc, ascq;
1623 
1624  		scsi_extract_sense(&ccb->csio.sense_data,
1625 				   &error_code, &sense_key, &asc, &ascq);
1626 		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1627  			error = cmd6workaround(ccb);
1628 	}
1629 	if (error == ERESTART)
1630 		return (ERESTART);
1631 
1632 	/*
1633 	 * XXX
1634 	 * Until we have a better way of doing pack validation,
1635 	 * don't treat UAs as errors.
1636 	 */
1637 	sense_flags |= SF_RETRY_UA;
1638 	return(cam_periph_error(ccb, cam_flags, sense_flags,
1639 				&softc->saved_ccb));
1640 }
1641 
1642 static void
1643 daprevent(struct cam_periph *periph, int action)
1644 {
1645 	struct	da_softc *softc;
1646 	union	ccb *ccb;
1647 	int	error;
1648 
1649 	softc = (struct da_softc *)periph->softc;
1650 
1651 	if (((action == PR_ALLOW)
1652 	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1653 	 || ((action == PR_PREVENT)
1654 	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1655 		return;
1656 	}
1657 
1658 	ccb = cam_periph_getccb(periph, /*priority*/1);
1659 
1660 	scsi_prevent(&ccb->csio,
1661 		     /*retries*/1,
1662 		     /*cbcfp*/dadone,
1663 		     MSG_SIMPLE_Q_TAG,
1664 		     action,
1665 		     SSD_FULL_SIZE,
1666 		     5000);
1667 
1668 	error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1669 				  SF_RETRY_UA, softc->disk.d_devstat);
1670 
1671 	if (error == 0) {
1672 		if (action == PR_ALLOW)
1673 			softc->flags &= ~DA_FLAG_PACK_LOCKED;
1674 		else
1675 			softc->flags |= DA_FLAG_PACK_LOCKED;
1676 	}
1677 
1678 	xpt_release_ccb(ccb);
1679 }
1680 
1681 static int
1682 dagetcapacity(struct cam_periph *periph)
1683 {
1684 	struct da_softc *softc;
1685 	union ccb *ccb;
1686 	struct scsi_read_capacity_data *rcap;
1687 	struct scsi_read_capacity_data_long *rcaplong;
1688 	uint32_t block_len;
1689 	uint64_t maxsector;
1690 	int error;
1691 
1692 	softc = (struct da_softc *)periph->softc;
1693 	block_len = 0;
1694 	maxsector = 0;
1695 	error = 0;
1696 
1697 	/* Do a read capacity */
1698 	rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
1699 							M_TEMP,
1700 							M_WAITOK);
1701 
1702 	ccb = cam_periph_getccb(periph, /*priority*/1);
1703 	scsi_read_capacity(&ccb->csio,
1704 			   /*retries*/4,
1705 			   /*cbfncp*/dadone,
1706 			   MSG_SIMPLE_Q_TAG,
1707 			   rcap,
1708 			   SSD_FULL_SIZE,
1709 			   /*timeout*/60000);
1710 	ccb->ccb_h.ccb_bp = NULL;
1711 
1712 	error = cam_periph_runccb(ccb, daerror,
1713 				  /*cam_flags*/CAM_RETRY_SELTO,
1714 				  /*sense_flags*/SF_RETRY_UA,
1715 				  softc->disk.d_devstat);
1716 
1717 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1718 		cam_release_devq(ccb->ccb_h.path,
1719 				 /*relsim_flags*/0,
1720 				 /*reduction*/0,
1721 				 /*timeout*/0,
1722 				 /*getcount_only*/0);
1723 
1724 	if (error == 0) {
1725 		block_len = scsi_4btoul(rcap->length);
1726 		maxsector = scsi_4btoul(rcap->addr);
1727 
1728 		if (maxsector != 0xffffffff)
1729 			goto done;
1730 	} else
1731 		goto done;
1732 
1733 	rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1734 
1735 	scsi_read_capacity_16(&ccb->csio,
1736 			      /*retries*/ 4,
1737 			      /*cbfcnp*/ dadone,
1738 			      /*tag_action*/ MSG_SIMPLE_Q_TAG,
1739 			      /*lba*/ 0,
1740 			      /*reladr*/ 0,
1741 			      /*pmi*/ 0,
1742 			      rcaplong,
1743 			      /*sense_len*/ SSD_FULL_SIZE,
1744 			      /*timeout*/ 60000);
1745 	ccb->ccb_h.ccb_bp = NULL;
1746 
1747 	error = cam_periph_runccb(ccb, daerror,
1748 				  /*cam_flags*/CAM_RETRY_SELTO,
1749 				  /*sense_flags*/SF_RETRY_UA,
1750 				  softc->disk.d_devstat);
1751 
1752 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1753 		cam_release_devq(ccb->ccb_h.path,
1754 				 /*relsim_flags*/0,
1755 				 /*reduction*/0,
1756 				 /*timeout*/0,
1757 				 /*getcount_only*/0);
1758 
1759 	if (error == 0) {
1760 		block_len = scsi_4btoul(rcaplong->length);
1761 		maxsector = scsi_8btou64(rcaplong->addr);
1762 	}
1763 
1764 done:
1765 
1766 	if (error == 0)
1767 		dasetgeom(periph, block_len, maxsector);
1768 
1769 	xpt_release_ccb(ccb);
1770 
1771 	free(rcap, M_TEMP);
1772 
1773 	return (error);
1774 }
1775 
1776 static void
1777 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
1778 {
1779 	struct ccb_calc_geometry ccg;
1780 	struct da_softc *softc;
1781 	struct disk_params *dp;
1782 
1783 	softc = (struct da_softc *)periph->softc;
1784 
1785 	dp = &softc->params;
1786 	dp->secsize = block_len;
1787 	dp->sectors = maxsector + 1;
1788 	/*
1789 	 * Have the controller provide us with a geometry
1790 	 * for this disk.  The only time the geometry
1791 	 * matters is when we boot and the controller
1792 	 * is the only one knowledgeable enough to come
1793 	 * up with something that will make this a bootable
1794 	 * device.
1795 	 */
1796 	xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1797 	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1798 	ccg.block_size = dp->secsize;
1799 	ccg.volume_size = dp->sectors;
1800 	ccg.heads = 0;
1801 	ccg.secs_per_track = 0;
1802 	ccg.cylinders = 0;
1803 	xpt_action((union ccb*)&ccg);
1804 	dp->heads = ccg.heads;
1805 	dp->secs_per_track = ccg.secs_per_track;
1806 	dp->cylinders = ccg.cylinders;
1807 }
1808 
1809 static void
1810 dasendorderedtag(void *arg)
1811 {
1812 	struct da_softc *softc;
1813 	int s;
1814 
1815 	for (softc = SLIST_FIRST(&softc_list);
1816 	     softc != NULL;
1817 	     softc = SLIST_NEXT(softc, links)) {
1818 		s = splsoftcam();
1819 		if ((softc->ordered_tag_count == 0)
1820 		 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1821 			softc->flags |= DA_FLAG_NEED_OTAG;
1822 		}
1823 		if (softc->outstanding_cmds > 0)
1824 			softc->flags &= ~DA_FLAG_WENT_IDLE;
1825 
1826 		softc->ordered_tag_count = 0;
1827 		splx(s);
1828 	}
1829 	/* Queue us up again */
1830 	timeout(dasendorderedtag, NULL,
1831 		(da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
1832 }
1833 
1834 /*
1835  * Step through all DA peripheral drivers, and if the device is still open,
1836  * sync the disk cache to physical media.
1837  */
1838 static void
1839 dashutdown(void * arg, int howto)
1840 {
1841 	struct cam_periph *periph;
1842 	struct da_softc *softc;
1843 
1844 	TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
1845 		union ccb ccb;
1846 		softc = (struct da_softc *)periph->softc;
1847 
1848 		/*
1849 		 * We only sync the cache if the drive is still open, and
1850 		 * if the drive is capable of it..
1851 		 */
1852 		if (((softc->flags & DA_FLAG_OPEN) == 0)
1853 		 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1854 			continue;
1855 
1856 		xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1857 
1858 		ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1859 		scsi_synchronize_cache(&ccb.csio,
1860 				       /*retries*/1,
1861 				       /*cbfcnp*/dadone,
1862 				       MSG_SIMPLE_Q_TAG,
1863 				       /*begin_lba*/0, /* whole disk */
1864 				       /*lb_count*/0,
1865 				       SSD_FULL_SIZE,
1866 				       60 * 60 * 1000);
1867 
1868 		xpt_polled_action(&ccb);
1869 
1870 		if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1871 			if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1872 			     CAM_SCSI_STATUS_ERROR)
1873 			 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1874 				int error_code, sense_key, asc, ascq;
1875 
1876 				scsi_extract_sense(&ccb.csio.sense_data,
1877 						   &error_code, &sense_key,
1878 						   &asc, &ascq);
1879 
1880 				if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1881 					scsi_sense_print(&ccb.csio);
1882 			} else {
1883 				xpt_print_path(periph->path);
1884 				printf("Synchronize cache failed, status "
1885 				       "== 0x%x, scsi status == 0x%x\n",
1886 				       ccb.ccb_h.status, ccb.csio.scsi_status);
1887 			}
1888 		}
1889 
1890 		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1891 			cam_release_devq(ccb.ccb_h.path,
1892 					 /*relsim_flags*/0,
1893 					 /*reduction*/0,
1894 					 /*timeout*/0,
1895 					 /*getcount_only*/0);
1896 
1897 	}
1898 }
1899 
1900 #else /* !_KERNEL */
1901 
1902 /*
1903  * XXX This is only left out of the kernel build to silence warnings.  If,
1904  * for some reason this function is used in the kernel, the ifdefs should
1905  * be moved so it is included both in the kernel and userland.
1906  */
1907 void
1908 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1909 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
1910 		 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1911 		 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1912 		 u_int32_t timeout)
1913 {
1914 	struct scsi_format_unit *scsi_cmd;
1915 
1916 	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1917 	scsi_cmd->opcode = FORMAT_UNIT;
1918 	scsi_cmd->byte2 = byte2;
1919 	scsi_ulto2b(ileave, scsi_cmd->interleave);
1920 
1921 	cam_fill_csio(csio,
1922 		      retries,
1923 		      cbfcnp,
1924 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1925 		      tag_action,
1926 		      data_ptr,
1927 		      dxfer_len,
1928 		      sense_len,
1929 		      sizeof(*scsi_cmd),
1930 		      timeout);
1931 }
1932 
1933 #endif /* _KERNEL */
1934