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