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