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