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