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