scsi_da.c revision 260958
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: stable/10/sys/cam/scsi/scsi_da.c 260958 2014-01-20 23:56:49Z mav $");
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#include <sys/lock.h>
41#include <sys/mutex.h>
42#include <sys/conf.h>
43#include <sys/devicestat.h>
44#include <sys/eventhandler.h>
45#include <sys/malloc.h>
46#include <sys/cons.h>
47#include <sys/endian.h>
48#include <sys/proc.h>
49#include <geom/geom.h>
50#include <geom/geom_disk.h>
51#endif /* _KERNEL */
52
53#ifndef _KERNEL
54#include <stdio.h>
55#include <string.h>
56#endif /* _KERNEL */
57
58#include <cam/cam.h>
59#include <cam/cam_ccb.h>
60#include <cam/cam_periph.h>
61#include <cam/cam_xpt_periph.h>
62#include <cam/cam_sim.h>
63
64#include <cam/scsi/scsi_message.h>
65
66#ifndef _KERNEL
67#include <cam/scsi/scsi_da.h>
68#endif /* !_KERNEL */
69
70#ifdef _KERNEL
71typedef enum {
72	DA_STATE_PROBE_RC,
73	DA_STATE_PROBE_RC16,
74	DA_STATE_PROBE_LBP,
75	DA_STATE_PROBE_BLK_LIMITS,
76	DA_STATE_PROBE_BDC,
77	DA_STATE_PROBE_ATA,
78	DA_STATE_NORMAL
79} da_state;
80
81typedef 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_NEED_OTAG	= 0x020,
87	DA_FLAG_WAS_OTAG	= 0x040,
88	DA_FLAG_RETRY_UA	= 0x080,
89	DA_FLAG_OPEN		= 0x100,
90	DA_FLAG_SCTX_INIT	= 0x200,
91	DA_FLAG_CAN_RC16	= 0x400,
92	DA_FLAG_PROBED		= 0x800,
93	DA_FLAG_DIRTY		= 0x1000
94} da_flags;
95
96typedef enum {
97	DA_Q_NONE		= 0x00,
98	DA_Q_NO_SYNC_CACHE	= 0x01,
99	DA_Q_NO_6_BYTE		= 0x02,
100	DA_Q_NO_PREVENT		= 0x04,
101	DA_Q_4K			= 0x08,
102	DA_Q_NO_RC16		= 0x10
103} da_quirks;
104
105#define DA_Q_BIT_STRING		\
106	"\020"			\
107	"\001NO_SYNC_CACHE"	\
108	"\002NO_6_BYTE"		\
109	"\003NO_PREVENT"	\
110	"\0044K"		\
111	"\005NO_RC16"
112
113typedef enum {
114	DA_CCB_PROBE_RC		= 0x01,
115	DA_CCB_PROBE_RC16	= 0x02,
116	DA_CCB_PROBE_LBP	= 0x03,
117	DA_CCB_PROBE_BLK_LIMITS	= 0x04,
118	DA_CCB_PROBE_BDC	= 0x05,
119	DA_CCB_PROBE_ATA	= 0x06,
120	DA_CCB_BUFFER_IO	= 0x07,
121	DA_CCB_DUMP		= 0x0A,
122	DA_CCB_DELETE		= 0x0B,
123 	DA_CCB_TUR		= 0x0C,
124	DA_CCB_TYPE_MASK	= 0x0F,
125	DA_CCB_RETRY_UA		= 0x10
126} da_ccb_state;
127
128/*
129 * Order here is important for method choice
130 *
131 * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
132 * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
133 * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
134 * import taking 5mins.
135 *
136 */
137typedef enum {
138	DA_DELETE_NONE,
139	DA_DELETE_DISABLE,
140	DA_DELETE_ATA_TRIM,
141	DA_DELETE_UNMAP,
142	DA_DELETE_WS16,
143	DA_DELETE_WS10,
144	DA_DELETE_ZERO,
145	DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
146	DA_DELETE_MAX = DA_DELETE_ZERO
147} da_delete_methods;
148
149typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
150			      struct bio *bp);
151static da_delete_func_t da_delete_trim;
152static da_delete_func_t da_delete_unmap;
153static da_delete_func_t da_delete_ws;
154
155static const void * da_delete_functions[] = {
156	NULL,
157	NULL,
158	da_delete_trim,
159	da_delete_unmap,
160	da_delete_ws,
161	da_delete_ws,
162	da_delete_ws
163};
164
165static const char *da_delete_method_names[] =
166    { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
167static const char *da_delete_method_desc[] =
168    { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
169      "WRITE SAME(10) with UNMAP", "ZERO" };
170
171/* Offsets into our private area for storing information */
172#define ccb_state	ppriv_field0
173#define ccb_bp		ppriv_ptr1
174
175struct disk_params {
176	u_int8_t  heads;
177	u_int32_t cylinders;
178	u_int8_t  secs_per_track;
179	u_int32_t secsize;	/* Number of bytes/sector */
180	u_int64_t sectors;	/* total number sectors */
181	u_int     stripesize;
182	u_int     stripeoffset;
183};
184
185#define UNMAP_RANGE_MAX		0xffffffff
186#define UNMAP_HEAD_SIZE		8
187#define UNMAP_RANGE_SIZE	16
188#define UNMAP_MAX_RANGES	2048 /* Protocol Max is 4095 */
189#define UNMAP_BUF_SIZE		((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
190				UNMAP_HEAD_SIZE)
191
192#define WS10_MAX_BLKS		0xffff
193#define WS16_MAX_BLKS		0xffffffff
194#define ATA_TRIM_MAX_RANGES	((UNMAP_BUF_SIZE / \
195	(ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
196
197struct da_softc {
198	struct	 bio_queue_head bio_queue;
199	struct	 bio_queue_head delete_queue;
200	struct	 bio_queue_head delete_run_queue;
201	LIST_HEAD(, ccb_hdr) pending_ccbs;
202	int	 tur;			/* TEST UNIT READY should be sent */
203	int	 refcount;		/* Active xpt_action() calls */
204	da_state state;
205	da_flags flags;
206	da_quirks quirks;
207	int	 sort_io_queue;
208	int	 minimum_cmd_size;
209	int	 error_inject;
210	int	 trim_max_ranges;
211	int	 delete_running;
212	int	 delete_available;	/* Delete methods possibly available */
213	uint32_t		unmap_max_ranges;
214	uint32_t		unmap_max_lba;
215	uint64_t		ws_max_blks;
216	da_delete_methods	delete_method;
217	da_delete_func_t	*delete_func;
218	struct	 disk_params params;
219	struct	 disk *disk;
220	union	 ccb saved_ccb;
221	struct task		sysctl_task;
222	struct sysctl_ctx_list	sysctl_ctx;
223	struct sysctl_oid	*sysctl_tree;
224	struct callout		sendordered_c;
225	uint64_t wwpn;
226	uint8_t	 unmap_buf[UNMAP_BUF_SIZE];
227	struct scsi_read_capacity_data_long rcaplong;
228	struct callout		mediapoll_c;
229};
230
231#define dadeleteflag(softc, delete_method, enable)			\
232	if (enable) {							\
233		softc->delete_available |= (1 << delete_method);	\
234	} else {							\
235		softc->delete_available &= ~(1 << delete_method);	\
236	}
237
238struct da_quirk_entry {
239	struct scsi_inquiry_pattern inq_pat;
240	da_quirks quirks;
241};
242
243static const char quantum[] = "QUANTUM";
244static const char microp[] = "MICROP";
245
246static struct da_quirk_entry da_quirk_table[] =
247{
248	/* SPI, FC devices */
249	{
250		/*
251		 * Fujitsu M2513A MO drives.
252		 * Tested devices: M2513A2 firmware versions 1200 & 1300.
253		 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
254		 * Reported by: W.Scholten <whs@xs4all.nl>
255		 */
256		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
257		/*quirks*/ DA_Q_NO_SYNC_CACHE
258	},
259	{
260		/* See above. */
261		{T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
262		/*quirks*/ DA_Q_NO_SYNC_CACHE
263	},
264	{
265		/*
266		 * This particular Fujitsu drive doesn't like the
267		 * synchronize cache command.
268		 * Reported by: Tom Jackson <toj@gorilla.net>
269		 */
270		{T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
271		/*quirks*/ DA_Q_NO_SYNC_CACHE
272	},
273	{
274		/*
275		 * This drive doesn't like the synchronize cache command
276		 * either.  Reported by: Matthew Jacob <mjacob@feral.com>
277		 * in NetBSD PR kern/6027, August 24, 1998.
278		 */
279		{T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
280		/*quirks*/ DA_Q_NO_SYNC_CACHE
281	},
282	{
283		/*
284		 * This drive doesn't like the synchronize cache command
285		 * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
286		 * (PR 8882).
287		 */
288		{T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
289		/*quirks*/ DA_Q_NO_SYNC_CACHE
290	},
291	{
292		/*
293		 * Doesn't like the synchronize cache command.
294		 * Reported by: Blaz Zupan <blaz@gold.amis.net>
295		 */
296		{T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
297		/*quirks*/ DA_Q_NO_SYNC_CACHE
298	},
299	{
300		/*
301		 * Doesn't like the synchronize cache command.
302		 * Reported by: Blaz Zupan <blaz@gold.amis.net>
303		 */
304		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
305		/*quirks*/ DA_Q_NO_SYNC_CACHE
306	},
307	{
308		/*
309		 * Doesn't like the synchronize cache command.
310		 */
311		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
312		/*quirks*/ DA_Q_NO_SYNC_CACHE
313	},
314	{
315		/*
316		 * Doesn't like the synchronize cache command.
317		 * Reported by: walter@pelissero.de
318		 */
319		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
320		/*quirks*/ DA_Q_NO_SYNC_CACHE
321	},
322	{
323		/*
324		 * Doesn't work correctly with 6 byte reads/writes.
325		 * Returns illegal request, and points to byte 9 of the
326		 * 6-byte CDB.
327		 * Reported by:  Adam McDougall <bsdx@spawnet.com>
328		 */
329		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
330		/*quirks*/ DA_Q_NO_6_BYTE
331	},
332	{
333		/* See above. */
334		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
335		/*quirks*/ DA_Q_NO_6_BYTE
336	},
337	{
338		/*
339		 * Doesn't like the synchronize cache command.
340		 * Reported by: walter@pelissero.de
341		 */
342		{T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
343		/*quirks*/ DA_Q_NO_SYNC_CACHE
344	},
345	{
346		/*
347		 * The CISS RAID controllers do not support SYNC_CACHE
348		 */
349		{T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
350		/*quirks*/ DA_Q_NO_SYNC_CACHE
351	},
352	/* USB mass storage devices supported by umass(4) */
353	{
354		/*
355		 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
356		 * PR: kern/51675
357		 */
358		{T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
359		/*quirks*/ DA_Q_NO_SYNC_CACHE
360	},
361	{
362		/*
363		 * Power Quotient Int. (PQI) USB flash key
364		 * PR: kern/53067
365		 */
366		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
367		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
368	},
369 	{
370 		/*
371 		 * Creative Nomad MUVO mp3 player (USB)
372 		 * PR: kern/53094
373 		 */
374 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
375 		/*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
376 	},
377	{
378		/*
379		 * Jungsoft NEXDISK USB flash key
380		 * PR: kern/54737
381		 */
382		{T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
383		/*quirks*/ DA_Q_NO_SYNC_CACHE
384	},
385	{
386		/*
387		 * FreeDik USB Mini Data Drive
388		 * PR: kern/54786
389		 */
390		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
391		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
392	},
393	{
394		/*
395		 * Sigmatel USB Flash MP3 Player
396		 * PR: kern/57046
397		 */
398		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
399		/*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
400	},
401	{
402		/*
403		 * Neuros USB Digital Audio Computer
404		 * PR: kern/63645
405		 */
406		{T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
407		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
408	},
409	{
410		/*
411		 * SEAGRAND NP-900 MP3 Player
412		 * PR: kern/64563
413		 */
414		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
415		/*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
416	},
417	{
418		/*
419		 * iRiver iFP MP3 player (with UMS Firmware)
420		 * PR: kern/54881, i386/63941, kern/66124
421		 */
422		{T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
423		/*quirks*/ DA_Q_NO_SYNC_CACHE
424 	},
425	{
426		/*
427		 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
428		 * PR: kern/70158
429		 */
430		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
431		/*quirks*/ DA_Q_NO_SYNC_CACHE
432	},
433	{
434		/*
435		 * ZICPlay USB MP3 Player with FM
436		 * PR: kern/75057
437		 */
438		{T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
439		/*quirks*/ DA_Q_NO_SYNC_CACHE
440	},
441	{
442		/*
443		 * TEAC USB floppy mechanisms
444		 */
445		{T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
446		/*quirks*/ DA_Q_NO_SYNC_CACHE
447	},
448	{
449		/*
450		 * Kingston DataTraveler II+ USB Pen-Drive.
451		 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
452		 */
453		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
454		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
455	},
456	{
457		/*
458		 * USB DISK Pro PMAP
459		 * Reported by: jhs
460		 * PR: usb/96381
461		 */
462		{T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
463		/*quirks*/ DA_Q_NO_SYNC_CACHE
464	},
465	{
466		/*
467		 * Motorola E398 Mobile Phone (TransFlash memory card).
468		 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
469		 * PR: usb/89889
470		 */
471		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
472		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
473	},
474	{
475		/*
476		 * Qware BeatZkey! Pro
477		 * PR: usb/79164
478		 */
479		{T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
480		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
481	},
482	{
483		/*
484		 * Time DPA20B 1GB MP3 Player
485		 * PR: usb/81846
486		 */
487		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
488		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
489	},
490	{
491		/*
492		 * Samsung USB key 128Mb
493		 * PR: usb/90081
494		 */
495		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
496		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
497	},
498	{
499		/*
500		 * Kingston DataTraveler 2.0 USB Flash memory.
501		 * PR: usb/89196
502		 */
503		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
504		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
505	},
506	{
507		/*
508		 * Creative MUVO Slim mp3 player (USB)
509		 * PR: usb/86131
510		 */
511		{T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
512		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
513		},
514	{
515		/*
516		 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
517		 * PR: usb/80487
518		 */
519		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
520		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
521	},
522	{
523		/*
524		 * SanDisk Micro Cruzer 128MB
525		 * PR: usb/75970
526		 */
527		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
528		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
529	},
530	{
531		/*
532		 * TOSHIBA TransMemory USB sticks
533		 * PR: kern/94660
534		 */
535		{T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
536		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
537	},
538	{
539		/*
540		 * PNY USB Flash keys
541		 * PR: usb/75578, usb/72344, usb/65436
542		 */
543		{T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
544		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
545	},
546	{
547		/*
548		 * Genesys 6-in-1 Card Reader
549		 * PR: usb/94647
550		 */
551		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
552		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
553	},
554	{
555		/*
556		 * Rekam Digital CAMERA
557		 * PR: usb/98713
558		 */
559		{T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
560		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
561	},
562	{
563		/*
564		 * iRiver H10 MP3 player
565		 * PR: usb/102547
566		 */
567		{T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
568		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
569	},
570	{
571		/*
572		 * iRiver U10 MP3 player
573		 * PR: usb/92306
574		 */
575		{T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
576		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
577	},
578	{
579		/*
580		 * X-Micro Flash Disk
581		 * PR: usb/96901
582		 */
583		{T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
584		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
585	},
586	{
587		/*
588		 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
589		 * PR: usb/96546
590		 */
591		{T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
592		"1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
593	},
594	{
595		/*
596		 * Denver MP3 player
597		 * PR: usb/107101
598		 */
599		{T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
600		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
601	},
602	{
603		/*
604		 * Philips USB Key Audio KEY013
605		 * PR: usb/68412
606		 */
607		{T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
608		/*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
609	},
610	{
611		/*
612		 * JNC MP3 Player
613		 * PR: usb/94439
614		 */
615		{T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
616		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
617	},
618	{
619		/*
620		 * SAMSUNG MP0402H
621		 * PR: usb/108427
622		 */
623		{T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
624		/*quirks*/ DA_Q_NO_SYNC_CACHE
625	},
626	{
627		/*
628		 * I/O Magic USB flash - Giga Bank
629		 * PR: usb/108810
630		 */
631		{T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
632		/*quirks*/ DA_Q_NO_SYNC_CACHE
633	},
634	{
635		/*
636		 * JoyFly 128mb USB Flash Drive
637		 * PR: 96133
638		 */
639		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
640		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
641	},
642	{
643		/*
644		 * ChipsBnk usb stick
645		 * PR: 103702
646		 */
647		{T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
648		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
649	},
650	{
651		/*
652		 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
653		 * PR: 129858
654		 */
655		{T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
656		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
657	},
658	{
659		/*
660		 * Samsung YP-U3 mp3-player
661		 * PR: 125398
662		 */
663		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
664		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
665	},
666	{
667		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
668		 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
669	},
670	{
671		/*
672		 * Sony Cyber-Shot DSC cameras
673		 * PR: usb/137035
674		 */
675		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
676		/*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
677	},
678	{
679		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
680		 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
681	},
682	{
683		/* At least several Transcent USB sticks lie on RC16. */
684		{T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
685		 "*"}, /*quirks*/ DA_Q_NO_RC16
686	},
687	/* ATA/SATA devices over SAS/USB/... */
688	{
689		/* Hitachi Advanced Format (4k) drives */
690		{ T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
691		/*quirks*/DA_Q_4K
692	},
693	{
694		/* Samsung Advanced Format (4k) drives */
695		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
696		/*quirks*/DA_Q_4K
697	},
698	{
699		/* Samsung Advanced Format (4k) drives */
700		{ T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
701		/*quirks*/DA_Q_4K
702	},
703	{
704		/* Samsung Advanced Format (4k) drives */
705		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
706		/*quirks*/DA_Q_4K
707	},
708	{
709		/* Samsung Advanced Format (4k) drives */
710		{ T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
711		/*quirks*/DA_Q_4K
712	},
713	{
714		/* Seagate Barracuda Green Advanced Format (4k) drives */
715		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
716		/*quirks*/DA_Q_4K
717	},
718	{
719		/* Seagate Barracuda Green Advanced Format (4k) drives */
720		{ T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
721		/*quirks*/DA_Q_4K
722	},
723	{
724		/* Seagate Barracuda Green Advanced Format (4k) drives */
725		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
726		/*quirks*/DA_Q_4K
727	},
728	{
729		/* Seagate Barracuda Green Advanced Format (4k) drives */
730		{ T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
731		/*quirks*/DA_Q_4K
732	},
733	{
734		/* Seagate Barracuda Green Advanced Format (4k) drives */
735		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
736		/*quirks*/DA_Q_4K
737	},
738	{
739		/* Seagate Barracuda Green Advanced Format (4k) drives */
740		{ T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
741		/*quirks*/DA_Q_4K
742	},
743	{
744		/* Seagate Momentus Advanced Format (4k) drives */
745		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
746		/*quirks*/DA_Q_4K
747	},
748	{
749		/* Seagate Momentus Advanced Format (4k) drives */
750		{ T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
751		/*quirks*/DA_Q_4K
752	},
753	{
754		/* Seagate Momentus Advanced Format (4k) drives */
755		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
756		/*quirks*/DA_Q_4K
757	},
758	{
759		/* Seagate Momentus Advanced Format (4k) drives */
760		{ T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
761		/*quirks*/DA_Q_4K
762	},
763	{
764		/* Seagate Momentus Advanced Format (4k) drives */
765		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
766		/*quirks*/DA_Q_4K
767	},
768	{
769		/* Seagate Momentus Advanced Format (4k) drives */
770		{ T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
771		/*quirks*/DA_Q_4K
772	},
773	{
774		/* Seagate Momentus Advanced Format (4k) drives */
775		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
776		/*quirks*/DA_Q_4K
777	},
778	{
779		/* Seagate Momentus Advanced Format (4k) drives */
780		{ T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
781		/*quirks*/DA_Q_4K
782	},
783	{
784		/* Seagate Momentus Advanced Format (4k) drives */
785		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
786		/*quirks*/DA_Q_4K
787	},
788	{
789		/* Seagate Momentus Advanced Format (4k) drives */
790		{ T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
791		/*quirks*/DA_Q_4K
792	},
793	{
794		/* Seagate Momentus Advanced Format (4k) drives */
795		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
796		/*quirks*/DA_Q_4K
797	},
798	{
799		/* Seagate Momentus Advanced Format (4k) drives */
800		{ T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
801		/*quirks*/DA_Q_4K
802	},
803	{
804		/* Seagate Momentus Advanced Format (4k) drives */
805		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
806		/*quirks*/DA_Q_4K
807	},
808	{
809		/* Seagate Momentus Advanced Format (4k) drives */
810		{ T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
811		/*quirks*/DA_Q_4K
812	},
813	{
814		/* Seagate Momentus Thin Advanced Format (4k) drives */
815		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
816		/*quirks*/DA_Q_4K
817	},
818	{
819		/* Seagate Momentus Thin Advanced Format (4k) drives */
820		{ T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
821		/*quirks*/DA_Q_4K
822	},
823	{
824		/* WDC Caviar Green Advanced Format (4k) drives */
825		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
826		/*quirks*/DA_Q_4K
827	},
828	{
829		/* WDC Caviar Green Advanced Format (4k) drives */
830		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
831		/*quirks*/DA_Q_4K
832	},
833	{
834		/* WDC Caviar Green Advanced Format (4k) drives */
835		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
836		/*quirks*/DA_Q_4K
837	},
838	{
839		/* WDC Caviar Green Advanced Format (4k) drives */
840		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
841		/*quirks*/DA_Q_4K
842	},
843	{
844		/* WDC Caviar Green Advanced Format (4k) drives */
845		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
846		/*quirks*/DA_Q_4K
847	},
848	{
849		/* WDC Caviar Green Advanced Format (4k) drives */
850		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
851		/*quirks*/DA_Q_4K
852	},
853	{
854		/* WDC Caviar Green Advanced Format (4k) drives */
855		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
856		/*quirks*/DA_Q_4K
857	},
858	{
859		/* WDC Caviar Green Advanced Format (4k) drives */
860		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
861		/*quirks*/DA_Q_4K
862	},
863	{
864		/* WDC Scorpio Black Advanced Format (4k) drives */
865		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
866		/*quirks*/DA_Q_4K
867	},
868	{
869		/* WDC Scorpio Black Advanced Format (4k) drives */
870		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
871		/*quirks*/DA_Q_4K
872	},
873	{
874		/* WDC Scorpio Black Advanced Format (4k) drives */
875		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
876		/*quirks*/DA_Q_4K
877	},
878	{
879		/* WDC Scorpio Black Advanced Format (4k) drives */
880		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
881		/*quirks*/DA_Q_4K
882	},
883	{
884		/* WDC Scorpio Blue Advanced Format (4k) drives */
885		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
886		/*quirks*/DA_Q_4K
887	},
888	{
889		/* WDC Scorpio Blue Advanced Format (4k) drives */
890		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
891		/*quirks*/DA_Q_4K
892	},
893	{
894		/* WDC Scorpio Blue Advanced Format (4k) drives */
895		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
896		/*quirks*/DA_Q_4K
897	},
898	{
899		/* WDC Scorpio Blue Advanced Format (4k) drives */
900		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
901		/*quirks*/DA_Q_4K
902	},
903	{
904		/*
905		 * Olympus FE-210 camera
906		 */
907		{T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
908		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
909	},
910	{
911		/*
912		 * LG UP3S MP3 player
913		 */
914		{T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
915		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
916	},
917	{
918		/*
919		 * Laser MP3-2GA13 MP3 player
920		 */
921		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
922		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
923	},
924	{
925		/*
926		 * LaCie external 250GB Hard drive des by Porsche
927		 * Submitted by: Ben Stuyts <ben@altesco.nl>
928		 * PR: 121474
929		 */
930		{T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
931		/*quirks*/ DA_Q_NO_SYNC_CACHE
932	},
933	/* SATA SSDs */
934	{
935		/*
936		 * Corsair Force 2 SSDs
937		 * 4k optimised & trim only works in 4k requests + 4k aligned
938		 */
939		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
940		/*quirks*/DA_Q_4K
941	},
942	{
943		/*
944		 * Corsair Force 3 SSDs
945		 * 4k optimised & trim only works in 4k requests + 4k aligned
946		 */
947		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
948		/*quirks*/DA_Q_4K
949	},
950        {
951		/*
952		 * Corsair Neutron GTX SSDs
953		 * 4k optimised & trim only works in 4k requests + 4k aligned
954		 */
955		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
956		/*quirks*/DA_Q_4K
957	},
958	{
959		/*
960		 * Corsair Force GT SSDs
961		 * 4k optimised & trim only works in 4k requests + 4k aligned
962		 */
963		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force GT*", "*" },
964		/*quirks*/DA_Q_4K
965	},
966	{
967		/*
968		 * Crucial M4 SSDs
969		 * 4k optimised & trim only works in 4k requests + 4k aligned
970		 */
971		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
972		/*quirks*/DA_Q_4K
973	},
974	{
975		/*
976		 * Crucial RealSSD C300 SSDs
977		 * 4k optimised
978		 */
979		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
980		"*" }, /*quirks*/DA_Q_4K
981	},
982	{
983		/*
984		 * Intel 320 Series SSDs
985		 * 4k optimised & trim only works in 4k requests + 4k aligned
986		 */
987		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
988		/*quirks*/DA_Q_4K
989	},
990	{
991		/*
992		 * Intel 330 Series SSDs
993		 * 4k optimised & trim only works in 4k requests + 4k aligned
994		 */
995		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
996		/*quirks*/DA_Q_4K
997	},
998	{
999		/*
1000		 * Intel 510 Series SSDs
1001		 * 4k optimised & trim only works in 4k requests + 4k aligned
1002		 */
1003		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1004		/*quirks*/DA_Q_4K
1005	},
1006	{
1007		/*
1008		 * Intel 520 Series SSDs
1009		 * 4k optimised & trim only works in 4k requests + 4k aligned
1010		 */
1011		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1012		/*quirks*/DA_Q_4K
1013	},
1014	{
1015		/*
1016		 * Intel X25-M Series SSDs
1017		 * 4k optimised & trim only works in 4k requests + 4k aligned
1018		 */
1019		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1020		/*quirks*/DA_Q_4K
1021	},
1022	{
1023		/*
1024		 * Kingston E100 Series SSDs
1025		 * 4k optimised & trim only works in 4k requests + 4k aligned
1026		 */
1027		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1028		/*quirks*/DA_Q_4K
1029	},
1030	{
1031		/*
1032		 * Kingston HyperX 3k SSDs
1033		 * 4k optimised & trim only works in 4k requests + 4k aligned
1034		 */
1035		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1036		/*quirks*/DA_Q_4K
1037	},
1038	{
1039		/*
1040		 * Marvell SSDs (entry taken from OpenSolaris)
1041		 * 4k optimised & trim only works in 4k requests + 4k aligned
1042		 */
1043		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1044		/*quirks*/DA_Q_4K
1045	},
1046	{
1047		/*
1048		 * OCZ Agility 2 SSDs
1049		 * 4k optimised & trim only works in 4k requests + 4k aligned
1050		 */
1051		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1052		/*quirks*/DA_Q_4K
1053	},
1054	{
1055		/*
1056		 * OCZ Agility 3 SSDs
1057		 * 4k optimised & trim only works in 4k requests + 4k aligned
1058		 */
1059		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1060		/*quirks*/DA_Q_4K
1061	},
1062	{
1063		/*
1064		 * OCZ Deneva R Series SSDs
1065		 * 4k optimised & trim only works in 4k requests + 4k aligned
1066		 */
1067		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1068		/*quirks*/DA_Q_4K
1069	},
1070	{
1071		/*
1072		 * OCZ Vertex 2 SSDs (inc pro series)
1073		 * 4k optimised & trim only works in 4k requests + 4k aligned
1074		 */
1075		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1076		/*quirks*/DA_Q_4K
1077	},
1078	{
1079		/*
1080		 * OCZ Vertex 3 SSDs
1081		 * 4k optimised & trim only works in 4k requests + 4k aligned
1082		 */
1083		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1084		/*quirks*/DA_Q_4K
1085	},
1086	{
1087		/*
1088		 * OCZ Vertex 4 SSDs
1089		 * 4k optimised & trim only works in 4k requests + 4k aligned
1090		 */
1091		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1092		/*quirks*/DA_Q_4K
1093	},
1094	{
1095		/*
1096		 * Samsung 830 Series SSDs
1097		 * 4k optimised & trim only works in 4k requests + 4k aligned
1098		 */
1099		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1100		/*quirks*/DA_Q_4K
1101	},
1102	{
1103		/*
1104		 * SuperTalent TeraDrive CT SSDs
1105		 * 4k optimised & trim only works in 4k requests + 4k aligned
1106		 */
1107		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1108		/*quirks*/DA_Q_4K
1109	},
1110	{
1111		/*
1112		 * XceedIOPS SATA SSDs
1113		 * 4k optimised
1114		 */
1115		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1116		/*quirks*/DA_Q_4K
1117	},
1118};
1119
1120static	disk_strategy_t	dastrategy;
1121static	dumper_t	dadump;
1122static	periph_init_t	dainit;
1123static	void		daasync(void *callback_arg, u_int32_t code,
1124				struct cam_path *path, void *arg);
1125static	void		dasysctlinit(void *context, int pending);
1126static	int		dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1127static	int		dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1128static	int		dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1129static	void		dadeletemethodset(struct da_softc *softc,
1130					  da_delete_methods delete_method);
1131static	off_t		dadeletemaxsize(struct da_softc *softc,
1132					da_delete_methods delete_method);
1133static	void		dadeletemethodchoose(struct da_softc *softc,
1134					     da_delete_methods default_method);
1135static	void		daprobedone(struct cam_periph *periph, union ccb *ccb);
1136
1137static	periph_ctor_t	daregister;
1138static	periph_dtor_t	dacleanup;
1139static	periph_start_t	dastart;
1140static	periph_oninv_t	daoninvalidate;
1141static	void		dadone(struct cam_periph *periph,
1142			       union ccb *done_ccb);
1143static  int		daerror(union ccb *ccb, u_int32_t cam_flags,
1144				u_int32_t sense_flags);
1145static void		daprevent(struct cam_periph *periph, int action);
1146static void		dareprobe(struct cam_periph *periph);
1147static void		dasetgeom(struct cam_periph *periph, uint32_t block_len,
1148				  uint64_t maxsector,
1149				  struct scsi_read_capacity_data_long *rcaplong,
1150				  size_t rcap_size);
1151static timeout_t	dasendorderedtag;
1152static void		dashutdown(void *arg, int howto);
1153static timeout_t	damediapoll;
1154
1155#ifndef	DA_DEFAULT_POLL_PERIOD
1156#define	DA_DEFAULT_POLL_PERIOD	3
1157#endif
1158
1159#ifndef DA_DEFAULT_TIMEOUT
1160#define DA_DEFAULT_TIMEOUT 60	/* Timeout in seconds */
1161#endif
1162
1163#ifndef	DA_DEFAULT_RETRY
1164#define	DA_DEFAULT_RETRY	4
1165#endif
1166
1167#ifndef	DA_DEFAULT_SEND_ORDERED
1168#define	DA_DEFAULT_SEND_ORDERED	1
1169#endif
1170
1171#define DA_SIO (softc->sort_io_queue >= 0 ? \
1172    softc->sort_io_queue : cam_sort_io_queues)
1173
1174static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1175static int da_retry_count = DA_DEFAULT_RETRY;
1176static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1177static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1178
1179static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1180            "CAM Direct Access Disk driver");
1181SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1182           &da_poll_period, 0, "Media polling period in seconds");
1183TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1184SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1185           &da_retry_count, 0, "Normal I/O retry count");
1186TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1187SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1188           &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1189TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1190SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1191           &da_send_ordered, 0, "Send Ordered Tags");
1192TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1193
1194/*
1195 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1196 * to the default timeout, we check to see whether an ordered
1197 * tagged transaction is appropriate to prevent simple tag
1198 * starvation.  Since we'd like to ensure that there is at least
1199 * 1/2 of the timeout length left for a starved transaction to
1200 * complete after we've sent an ordered tag, we must poll at least
1201 * four times in every timeout period.  This takes care of the worst
1202 * case where a starved transaction starts during an interval that
1203 * meets the requirement "don't send an ordered tag" test so it takes
1204 * us two intervals to determine that a tag must be sent.
1205 */
1206#ifndef DA_ORDEREDTAG_INTERVAL
1207#define DA_ORDEREDTAG_INTERVAL 4
1208#endif
1209
1210static struct periph_driver dadriver =
1211{
1212	dainit, "da",
1213	TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1214};
1215
1216PERIPHDRIVER_DECLARE(da, dadriver);
1217
1218static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1219
1220static int
1221daopen(struct disk *dp)
1222{
1223	struct cam_periph *periph;
1224	struct da_softc *softc;
1225	int error;
1226
1227	periph = (struct cam_periph *)dp->d_drv1;
1228	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1229		return (ENXIO);
1230	}
1231
1232	cam_periph_lock(periph);
1233	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1234		cam_periph_unlock(periph);
1235		cam_periph_release(periph);
1236		return (error);
1237	}
1238
1239	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1240	    ("daopen\n"));
1241
1242	softc = (struct da_softc *)periph->softc;
1243	dareprobe(periph);
1244
1245	/* Wait for the disk size update.  */
1246	error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1247	    "dareprobe", 0);
1248	if (error != 0)
1249		xpt_print(periph->path, "unable to retrieve capacity data");
1250
1251	if (periph->flags & CAM_PERIPH_INVALID)
1252		error = ENXIO;
1253
1254	if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1255	    (softc->quirks & DA_Q_NO_PREVENT) == 0)
1256		daprevent(periph, PR_PREVENT);
1257
1258	if (error == 0) {
1259		softc->flags &= ~DA_FLAG_PACK_INVALID;
1260		softc->flags |= DA_FLAG_OPEN;
1261	}
1262
1263	cam_periph_unhold(periph);
1264	cam_periph_unlock(periph);
1265
1266	if (error != 0)
1267		cam_periph_release(periph);
1268
1269	return (error);
1270}
1271
1272static int
1273daclose(struct disk *dp)
1274{
1275	struct	cam_periph *periph;
1276	struct	da_softc *softc;
1277	union	ccb *ccb;
1278	int error;
1279
1280	periph = (struct cam_periph *)dp->d_drv1;
1281	softc = (struct da_softc *)periph->softc;
1282	cam_periph_lock(periph);
1283	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1284	    ("daclose\n"));
1285
1286	if (cam_periph_hold(periph, PRIBIO) == 0) {
1287
1288		/* Flush disk cache. */
1289		if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1290		    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1291		    (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1292			ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1293			scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1294			    /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1295			    /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1296			    5 * 60 * 1000);
1297			error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1298			    /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1299			    softc->disk->d_devstat);
1300			if (error == 0)
1301				softc->flags &= ~DA_FLAG_DIRTY;
1302			xpt_release_ccb(ccb);
1303		}
1304
1305		/* Allow medium removal. */
1306		if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1307		    (softc->quirks & DA_Q_NO_PREVENT) == 0)
1308			daprevent(periph, PR_ALLOW);
1309
1310		cam_periph_unhold(periph);
1311	}
1312
1313	/*
1314	 * If we've got removeable media, mark the blocksize as
1315	 * unavailable, since it could change when new media is
1316	 * inserted.
1317	 */
1318	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1319		softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1320
1321	softc->flags &= ~DA_FLAG_OPEN;
1322	while (softc->refcount != 0)
1323		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1324	cam_periph_unlock(periph);
1325	cam_periph_release(periph);
1326	return (0);
1327}
1328
1329static void
1330daschedule(struct cam_periph *periph)
1331{
1332	struct da_softc *softc = (struct da_softc *)periph->softc;
1333
1334	if (softc->state != DA_STATE_NORMAL)
1335		return;
1336
1337	/* Check if we have more work to do. */
1338	if (bioq_first(&softc->bio_queue) ||
1339	    (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1340	    softc->tur) {
1341		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1342	}
1343}
1344
1345/*
1346 * Actually translate the requested transfer into one the physical driver
1347 * can understand.  The transfer is described by a buf and will include
1348 * only one physical transfer.
1349 */
1350static void
1351dastrategy(struct bio *bp)
1352{
1353	struct cam_periph *periph;
1354	struct da_softc *softc;
1355
1356	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1357	softc = (struct da_softc *)periph->softc;
1358
1359	cam_periph_lock(periph);
1360
1361	/*
1362	 * If the device has been made invalid, error out
1363	 */
1364	if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1365		cam_periph_unlock(periph);
1366		biofinish(bp, NULL, ENXIO);
1367		return;
1368	}
1369
1370	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1371
1372	/*
1373	 * Place it in the queue of disk activities for this disk
1374	 */
1375	if (bp->bio_cmd == BIO_DELETE) {
1376		if (DA_SIO)
1377			bioq_disksort(&softc->delete_queue, bp);
1378		else
1379			bioq_insert_tail(&softc->delete_queue, bp);
1380	} else if (DA_SIO) {
1381		bioq_disksort(&softc->bio_queue, bp);
1382	} else {
1383		bioq_insert_tail(&softc->bio_queue, bp);
1384	}
1385
1386	/*
1387	 * Schedule ourselves for performing the work.
1388	 */
1389	daschedule(periph);
1390	cam_periph_unlock(periph);
1391
1392	return;
1393}
1394
1395static int
1396dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1397{
1398	struct	    cam_periph *periph;
1399	struct	    da_softc *softc;
1400	u_int	    secsize;
1401	struct	    ccb_scsiio csio;
1402	struct	    disk *dp;
1403	int	    error = 0;
1404
1405	dp = arg;
1406	periph = dp->d_drv1;
1407	softc = (struct da_softc *)periph->softc;
1408	cam_periph_lock(periph);
1409	secsize = softc->params.secsize;
1410
1411	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1412		cam_periph_unlock(periph);
1413		return (ENXIO);
1414	}
1415
1416	if (length > 0) {
1417		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1418		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1419		scsi_read_write(&csio,
1420				/*retries*/0,
1421				dadone,
1422				MSG_ORDERED_Q_TAG,
1423				/*read*/SCSI_RW_WRITE,
1424				/*byte2*/0,
1425				/*minimum_cmd_size*/ softc->minimum_cmd_size,
1426				offset / secsize,
1427				length / secsize,
1428				/*data_ptr*/(u_int8_t *) virtual,
1429				/*dxfer_len*/length,
1430				/*sense_len*/SSD_FULL_SIZE,
1431				da_default_timeout * 1000);
1432		xpt_polled_action((union ccb *)&csio);
1433
1434		error = cam_periph_error((union ccb *)&csio,
1435		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1436		if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1437			cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1438			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1439		if (error != 0)
1440			printf("Aborting dump due to I/O error.\n");
1441		cam_periph_unlock(periph);
1442		return (error);
1443	}
1444
1445	/*
1446	 * Sync the disk cache contents to the physical media.
1447	 */
1448	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1449
1450		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1451		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1452		scsi_synchronize_cache(&csio,
1453				       /*retries*/0,
1454				       /*cbfcnp*/dadone,
1455				       MSG_SIMPLE_Q_TAG,
1456				       /*begin_lba*/0,/* Cover the whole disk */
1457				       /*lb_count*/0,
1458				       SSD_FULL_SIZE,
1459				       5 * 60 * 1000);
1460		xpt_polled_action((union ccb *)&csio);
1461
1462		error = cam_periph_error((union ccb *)&csio,
1463		    0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1464		if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1465			cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1466			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1467		if (error != 0)
1468			xpt_print(periph->path, "Synchronize cache failed\n");
1469	}
1470	cam_periph_unlock(periph);
1471	return (error);
1472}
1473
1474static int
1475dagetattr(struct bio *bp)
1476{
1477	int ret;
1478	struct cam_periph *periph;
1479
1480	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1481	cam_periph_lock(periph);
1482	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1483	    periph->path);
1484	cam_periph_unlock(periph);
1485	if (ret == 0)
1486		bp->bio_completed = bp->bio_length;
1487	return ret;
1488}
1489
1490static void
1491dainit(void)
1492{
1493	cam_status status;
1494
1495	/*
1496	 * Install a global async callback.  This callback will
1497	 * receive async callbacks like "new device found".
1498	 */
1499	status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1500
1501	if (status != CAM_REQ_CMP) {
1502		printf("da: Failed to attach master async callback "
1503		       "due to status 0x%x!\n", status);
1504	} else if (da_send_ordered) {
1505
1506		/* Register our shutdown event handler */
1507		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1508					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1509		    printf("dainit: shutdown event registration failed!\n");
1510	}
1511}
1512
1513/*
1514 * Callback from GEOM, called when it has finished cleaning up its
1515 * resources.
1516 */
1517static void
1518dadiskgonecb(struct disk *dp)
1519{
1520	struct cam_periph *periph;
1521
1522	periph = (struct cam_periph *)dp->d_drv1;
1523	cam_periph_release(periph);
1524}
1525
1526static void
1527daoninvalidate(struct cam_periph *periph)
1528{
1529	struct da_softc *softc;
1530
1531	softc = (struct da_softc *)periph->softc;
1532
1533	/*
1534	 * De-register any async callbacks.
1535	 */
1536	xpt_register_async(0, daasync, periph, periph->path);
1537
1538	softc->flags |= DA_FLAG_PACK_INVALID;
1539
1540	/*
1541	 * Return all queued I/O with ENXIO.
1542	 * XXX Handle any transactions queued to the card
1543	 *     with XPT_ABORT_CCB.
1544	 */
1545	bioq_flush(&softc->bio_queue, NULL, ENXIO);
1546	bioq_flush(&softc->delete_queue, NULL, ENXIO);
1547
1548	/*
1549	 * Tell GEOM that we've gone away, we'll get a callback when it is
1550	 * done cleaning up its resources.
1551	 */
1552	disk_gone(softc->disk);
1553}
1554
1555static void
1556dacleanup(struct cam_periph *periph)
1557{
1558	struct da_softc *softc;
1559
1560	softc = (struct da_softc *)periph->softc;
1561
1562	cam_periph_unlock(periph);
1563
1564	/*
1565	 * If we can't free the sysctl tree, oh well...
1566	 */
1567	if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1568	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1569		xpt_print(periph->path, "can't remove sysctl context\n");
1570	}
1571
1572	callout_drain(&softc->mediapoll_c);
1573	disk_destroy(softc->disk);
1574	callout_drain(&softc->sendordered_c);
1575	free(softc, M_DEVBUF);
1576	cam_periph_lock(periph);
1577}
1578
1579static void
1580daasync(void *callback_arg, u_int32_t code,
1581	struct cam_path *path, void *arg)
1582{
1583	struct cam_periph *periph;
1584	struct da_softc *softc;
1585
1586	periph = (struct cam_periph *)callback_arg;
1587	switch (code) {
1588	case AC_FOUND_DEVICE:
1589	{
1590		struct ccb_getdev *cgd;
1591		cam_status status;
1592
1593		cgd = (struct ccb_getdev *)arg;
1594		if (cgd == NULL)
1595			break;
1596
1597		if (cgd->protocol != PROTO_SCSI)
1598			break;
1599
1600		if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1601		    && SID_TYPE(&cgd->inq_data) != T_RBC
1602		    && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1603			break;
1604
1605		/*
1606		 * Allocate a peripheral instance for
1607		 * this device and start the probe
1608		 * process.
1609		 */
1610		status = cam_periph_alloc(daregister, daoninvalidate,
1611					  dacleanup, dastart,
1612					  "da", CAM_PERIPH_BIO,
1613					  path, daasync,
1614					  AC_FOUND_DEVICE, cgd);
1615
1616		if (status != CAM_REQ_CMP
1617		 && status != CAM_REQ_INPROG)
1618			printf("daasync: Unable to attach to new device "
1619				"due to status 0x%x\n", status);
1620		return;
1621	}
1622	case AC_ADVINFO_CHANGED:
1623	{
1624		uintptr_t buftype;
1625
1626		buftype = (uintptr_t)arg;
1627		if (buftype == CDAI_TYPE_PHYS_PATH) {
1628			struct da_softc *softc;
1629
1630			softc = periph->softc;
1631			disk_attr_changed(softc->disk, "GEOM::physpath",
1632					  M_NOWAIT);
1633		}
1634		break;
1635	}
1636	case AC_UNIT_ATTENTION:
1637	{
1638		union ccb *ccb;
1639		int error_code, sense_key, asc, ascq;
1640
1641		softc = (struct da_softc *)periph->softc;
1642		ccb = (union ccb *)arg;
1643
1644		/*
1645		 * Handle all UNIT ATTENTIONs except our own,
1646		 * as they will be handled by daerror().
1647		 */
1648		if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1649		    scsi_extract_sense_ccb(ccb,
1650		     &error_code, &sense_key, &asc, &ascq)) {
1651			if (asc == 0x2A && ascq == 0x09) {
1652				xpt_print(ccb->ccb_h.path,
1653				    "capacity data has changed\n");
1654				dareprobe(periph);
1655			} else if (asc == 0x28 && ascq == 0x00)
1656				disk_media_changed(softc->disk, M_NOWAIT);
1657		}
1658		cam_periph_async(periph, code, path, arg);
1659		break;
1660	}
1661	case AC_SCSI_AEN:
1662		softc = (struct da_softc *)periph->softc;
1663		if (!softc->tur) {
1664			if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1665				softc->tur = 1;
1666				daschedule(periph);
1667			}
1668		}
1669		/* FALLTHROUGH */
1670	case AC_SENT_BDR:
1671	case AC_BUS_RESET:
1672	{
1673		struct ccb_hdr *ccbh;
1674
1675		softc = (struct da_softc *)periph->softc;
1676		/*
1677		 * Don't fail on the expected unit attention
1678		 * that will occur.
1679		 */
1680		softc->flags |= DA_FLAG_RETRY_UA;
1681		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1682			ccbh->ccb_state |= DA_CCB_RETRY_UA;
1683		break;
1684	}
1685	default:
1686		break;
1687	}
1688	cam_periph_async(periph, code, path, arg);
1689}
1690
1691static void
1692dasysctlinit(void *context, int pending)
1693{
1694	struct cam_periph *periph;
1695	struct da_softc *softc;
1696	char tmpstr[80], tmpstr2[80];
1697	struct ccb_trans_settings cts;
1698
1699	periph = (struct cam_periph *)context;
1700	/*
1701	 * periph was held for us when this task was enqueued
1702	 */
1703	if (periph->flags & CAM_PERIPH_INVALID) {
1704		cam_periph_release(periph);
1705		return;
1706	}
1707
1708	softc = (struct da_softc *)periph->softc;
1709	snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1710	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1711
1712	sysctl_ctx_init(&softc->sysctl_ctx);
1713	softc->flags |= DA_FLAG_SCTX_INIT;
1714	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1715		SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1716		CTLFLAG_RD, 0, tmpstr);
1717	if (softc->sysctl_tree == NULL) {
1718		printf("dasysctlinit: unable to allocate sysctl tree\n");
1719		cam_periph_release(periph);
1720		return;
1721	}
1722
1723	/*
1724	 * Now register the sysctl handler, so the user can change the value on
1725	 * the fly.
1726	 */
1727	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1728		OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1729		softc, 0, dadeletemethodsysctl, "A",
1730		"BIO_DELETE execution method");
1731	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1732		OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1733		softc, 0, dadeletemaxsysctl, "Q",
1734		"Maximum BIO_DELETE size");
1735	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1736		OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1737		&softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1738		"Minimum CDB size");
1739	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1740		OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1741		"Sort IO queue to try and optimise disk access patterns");
1742
1743	SYSCTL_ADD_INT(&softc->sysctl_ctx,
1744		       SYSCTL_CHILDREN(softc->sysctl_tree),
1745		       OID_AUTO,
1746		       "error_inject",
1747		       CTLFLAG_RW,
1748		       &softc->error_inject,
1749		       0,
1750		       "error_inject leaf");
1751
1752
1753	/*
1754	 * Add some addressing info.
1755	 */
1756	memset(&cts, 0, sizeof (cts));
1757	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1758	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1759	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1760	cam_periph_lock(periph);
1761	xpt_action((union ccb *)&cts);
1762	cam_periph_unlock(periph);
1763	if (cts.ccb_h.status != CAM_REQ_CMP) {
1764		cam_periph_release(periph);
1765		return;
1766	}
1767	if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1768		struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1769		if (fc->valid & CTS_FC_VALID_WWPN) {
1770			softc->wwpn = fc->wwpn;
1771			SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1772			    SYSCTL_CHILDREN(softc->sysctl_tree),
1773			    OID_AUTO, "wwpn", CTLFLAG_RD,
1774			    &softc->wwpn, "World Wide Port Name");
1775		}
1776	}
1777	cam_periph_release(periph);
1778}
1779
1780static int
1781dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1782{
1783	int error;
1784	uint64_t value;
1785	struct da_softc *softc;
1786
1787	softc = (struct da_softc *)arg1;
1788
1789	value = softc->disk->d_delmaxsize;
1790	error = sysctl_handle_64(oidp, &value, 0, req);
1791	if ((error != 0) || (req->newptr == NULL))
1792		return (error);
1793
1794	/* only accept values smaller than the calculated value */
1795	if (value > dadeletemaxsize(softc, softc->delete_method)) {
1796		return (EINVAL);
1797	}
1798	softc->disk->d_delmaxsize = value;
1799
1800	return (0);
1801}
1802
1803static int
1804dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1805{
1806	int error, value;
1807
1808	value = *(int *)arg1;
1809
1810	error = sysctl_handle_int(oidp, &value, 0, req);
1811
1812	if ((error != 0)
1813	 || (req->newptr == NULL))
1814		return (error);
1815
1816	/*
1817	 * Acceptable values here are 6, 10, 12 or 16.
1818	 */
1819	if (value < 6)
1820		value = 6;
1821	else if ((value > 6)
1822	      && (value <= 10))
1823		value = 10;
1824	else if ((value > 10)
1825	      && (value <= 12))
1826		value = 12;
1827	else if (value > 12)
1828		value = 16;
1829
1830	*(int *)arg1 = value;
1831
1832	return (0);
1833}
1834
1835static void
1836dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1837{
1838
1839
1840	softc->delete_method = delete_method;
1841	softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1842	softc->delete_func = da_delete_functions[delete_method];
1843
1844	if (softc->delete_method > DA_DELETE_DISABLE)
1845		softc->disk->d_flags |= DISKFLAG_CANDELETE;
1846	else
1847		softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1848}
1849
1850static off_t
1851dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1852{
1853	off_t sectors;
1854
1855	switch(delete_method) {
1856	case DA_DELETE_UNMAP:
1857		sectors = (off_t)softc->unmap_max_lba * softc->unmap_max_ranges;
1858		break;
1859	case DA_DELETE_ATA_TRIM:
1860		sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1861		break;
1862	case DA_DELETE_WS16:
1863		sectors = (off_t)min(softc->ws_max_blks, WS16_MAX_BLKS);
1864		break;
1865	case DA_DELETE_ZERO:
1866	case DA_DELETE_WS10:
1867		sectors = (off_t)min(softc->ws_max_blks, WS10_MAX_BLKS);
1868		break;
1869	default:
1870		return 0;
1871	}
1872
1873	return (off_t)softc->params.secsize *
1874	    min(sectors, (off_t)softc->params.sectors);
1875}
1876
1877static void
1878daprobedone(struct cam_periph *periph, union ccb *ccb)
1879{
1880	struct da_softc *softc;
1881
1882	softc = (struct da_softc *)periph->softc;
1883
1884	dadeletemethodchoose(softc, DA_DELETE_NONE);
1885
1886	if (bootverbose && (softc->flags & DA_FLAG_PROBED) == 0) {
1887		char buf[80];
1888		int i, sep;
1889
1890		snprintf(buf, sizeof(buf), "Delete methods: <");
1891		sep = 0;
1892		for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1893			if (softc->delete_available & (1 << i)) {
1894				if (sep) {
1895					strlcat(buf, ",", sizeof(buf));
1896				} else {
1897				    sep = 1;
1898				}
1899				strlcat(buf, da_delete_method_names[i],
1900				    sizeof(buf));
1901				if (i == softc->delete_method) {
1902					strlcat(buf, "(*)", sizeof(buf));
1903				}
1904			}
1905		}
1906		if (sep == 0) {
1907			if (softc->delete_method == DA_DELETE_NONE)
1908				strlcat(buf, "NONE(*)", sizeof(buf));
1909			else
1910				strlcat(buf, "DISABLED(*)", sizeof(buf));
1911		}
1912		strlcat(buf, ">", sizeof(buf));
1913		printf("%s%d: %s\n", periph->periph_name,
1914		    periph->unit_number, buf);
1915	}
1916
1917	/*
1918	 * Since our peripheral may be invalidated by an error
1919	 * above or an external event, we must release our CCB
1920	 * before releasing the probe lock on the peripheral.
1921	 * The peripheral will only go away once the last lock
1922	 * is removed, and we need it around for the CCB release
1923	 * operation.
1924	 */
1925	xpt_release_ccb(ccb);
1926	softc->state = DA_STATE_NORMAL;
1927	daschedule(periph);
1928	wakeup(&softc->disk->d_mediasize);
1929	if ((softc->flags & DA_FLAG_PROBED) == 0) {
1930		softc->flags |= DA_FLAG_PROBED;
1931		cam_periph_unhold(periph);
1932	} else
1933		cam_periph_release_locked(periph);
1934}
1935
1936static void
1937dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
1938{
1939	int i, delete_method;
1940
1941	delete_method = default_method;
1942
1943	/*
1944	 * Use the pre-defined order to choose the best
1945	 * performing delete.
1946	 */
1947	for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1948		if (softc->delete_available & (1 << i)) {
1949			dadeletemethodset(softc, i);
1950			return;
1951		}
1952	}
1953	dadeletemethodset(softc, delete_method);
1954}
1955
1956static int
1957dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1958{
1959	char buf[16];
1960	const char *p;
1961	struct da_softc *softc;
1962	int i, error, methods, value;
1963
1964	softc = (struct da_softc *)arg1;
1965
1966	value = softc->delete_method;
1967	if (value < 0 || value > DA_DELETE_MAX)
1968		p = "UNKNOWN";
1969	else
1970		p = da_delete_method_names[value];
1971	strncpy(buf, p, sizeof(buf));
1972	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1973	if (error != 0 || req->newptr == NULL)
1974		return (error);
1975	methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
1976	for (i = 0; i <= DA_DELETE_MAX; i++) {
1977		if (!(methods & (1 << i)) ||
1978		    strcmp(buf, da_delete_method_names[i]) != 0)
1979			continue;
1980		dadeletemethodset(softc, i);
1981		return (0);
1982	}
1983	return (EINVAL);
1984}
1985
1986static cam_status
1987daregister(struct cam_periph *periph, void *arg)
1988{
1989	struct da_softc *softc;
1990	struct ccb_pathinq cpi;
1991	struct ccb_getdev *cgd;
1992	char tmpstr[80];
1993	caddr_t match;
1994
1995	cgd = (struct ccb_getdev *)arg;
1996	if (cgd == NULL) {
1997		printf("daregister: no getdev CCB, can't register device\n");
1998		return(CAM_REQ_CMP_ERR);
1999	}
2000
2001	softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2002	    M_NOWAIT|M_ZERO);
2003
2004	if (softc == NULL) {
2005		printf("daregister: Unable to probe new device. "
2006		       "Unable to allocate softc\n");
2007		return(CAM_REQ_CMP_ERR);
2008	}
2009
2010	LIST_INIT(&softc->pending_ccbs);
2011	softc->state = DA_STATE_PROBE_RC;
2012	bioq_init(&softc->bio_queue);
2013	bioq_init(&softc->delete_queue);
2014	bioq_init(&softc->delete_run_queue);
2015	if (SID_IS_REMOVABLE(&cgd->inq_data))
2016		softc->flags |= DA_FLAG_PACK_REMOVABLE;
2017	softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2018	softc->unmap_max_lba = UNMAP_RANGE_MAX;
2019	softc->ws_max_blks = WS16_MAX_BLKS;
2020	softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2021	softc->sort_io_queue = -1;
2022
2023	periph->softc = softc;
2024
2025	/*
2026	 * See if this device has any quirks.
2027	 */
2028	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2029			       (caddr_t)da_quirk_table,
2030			       sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2031			       sizeof(*da_quirk_table), scsi_inquiry_match);
2032
2033	if (match != NULL)
2034		softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2035	else
2036		softc->quirks = DA_Q_NONE;
2037
2038	/* Check if the SIM does not want 6 byte commands */
2039	bzero(&cpi, sizeof(cpi));
2040	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2041	cpi.ccb_h.func_code = XPT_PATH_INQ;
2042	xpt_action((union ccb *)&cpi);
2043	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2044		softc->quirks |= DA_Q_NO_6_BYTE;
2045
2046	TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2047
2048	/*
2049	 * Take an exclusive refcount on the periph while dastart is called
2050	 * to finish the probe.  The reference will be dropped in dadone at
2051	 * the end of probe.
2052	 */
2053	(void)cam_periph_hold(periph, PRIBIO);
2054
2055	/*
2056	 * Schedule a periodic event to occasionally send an
2057	 * ordered tag to a device.
2058	 */
2059	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2060	callout_reset(&softc->sendordered_c,
2061	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2062	    dasendorderedtag, softc);
2063
2064	cam_periph_unlock(periph);
2065	/*
2066	 * RBC devices don't have to support READ(6), only READ(10).
2067	 */
2068	if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2069		softc->minimum_cmd_size = 10;
2070	else
2071		softc->minimum_cmd_size = 6;
2072
2073	/*
2074	 * Load the user's default, if any.
2075	 */
2076	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2077		 periph->unit_number);
2078	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2079
2080	/*
2081	 * 6, 10, 12 and 16 are the currently permissible values.
2082	 */
2083	if (softc->minimum_cmd_size < 6)
2084		softc->minimum_cmd_size = 6;
2085	else if ((softc->minimum_cmd_size > 6)
2086	      && (softc->minimum_cmd_size <= 10))
2087		softc->minimum_cmd_size = 10;
2088	else if ((softc->minimum_cmd_size > 10)
2089	      && (softc->minimum_cmd_size <= 12))
2090		softc->minimum_cmd_size = 12;
2091	else if (softc->minimum_cmd_size > 12)
2092		softc->minimum_cmd_size = 16;
2093
2094	/* Predict whether device may support READ CAPACITY(16). */
2095	if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2096	    (softc->quirks & DA_Q_NO_RC16) == 0) {
2097		softc->flags |= DA_FLAG_CAN_RC16;
2098		softc->state = DA_STATE_PROBE_RC16;
2099	}
2100
2101	/*
2102	 * Register this media as a disk.
2103	 */
2104	softc->disk = disk_alloc();
2105	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2106			  periph->unit_number, 0,
2107			  DEVSTAT_BS_UNAVAILABLE,
2108			  SID_TYPE(&cgd->inq_data) |
2109			  XPORT_DEVSTAT_TYPE(cpi.transport),
2110			  DEVSTAT_PRIORITY_DISK);
2111	softc->disk->d_open = daopen;
2112	softc->disk->d_close = daclose;
2113	softc->disk->d_strategy = dastrategy;
2114	softc->disk->d_dump = dadump;
2115	softc->disk->d_getattr = dagetattr;
2116	softc->disk->d_gone = dadiskgonecb;
2117	softc->disk->d_name = "da";
2118	softc->disk->d_drv1 = periph;
2119	if (cpi.maxio == 0)
2120		softc->disk->d_maxsize = DFLTPHYS;	/* traditional default */
2121	else if (cpi.maxio > MAXPHYS)
2122		softc->disk->d_maxsize = MAXPHYS;	/* for safety */
2123	else
2124		softc->disk->d_maxsize = cpi.maxio;
2125	softc->disk->d_unit = periph->unit_number;
2126	softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2127	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2128		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2129	if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2130		softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2131	cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2132	    sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2133	strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2134	cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2135	    cgd->inq_data.product, sizeof(cgd->inq_data.product),
2136	    sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2137	softc->disk->d_hba_vendor = cpi.hba_vendor;
2138	softc->disk->d_hba_device = cpi.hba_device;
2139	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2140	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2141
2142	/*
2143	 * Acquire a reference to the periph before we register with GEOM.
2144	 * We'll release this reference once GEOM calls us back (via
2145	 * dadiskgonecb()) telling us that our provider has been freed.
2146	 */
2147	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2148		xpt_print(periph->path, "%s: lost periph during "
2149			  "registration!\n", __func__);
2150		cam_periph_lock(periph);
2151		return (CAM_REQ_CMP_ERR);
2152	}
2153
2154	disk_create(softc->disk, DISK_VERSION);
2155	cam_periph_lock(periph);
2156
2157	/*
2158	 * Add async callbacks for events of interest.
2159	 * I don't bother checking if this fails as,
2160	 * in most cases, the system will function just
2161	 * fine without them and the only alternative
2162	 * would be to not attach the device on failure.
2163	 */
2164	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2165	    AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2166	    daasync, periph, periph->path);
2167
2168	/*
2169	 * Emit an attribute changed notification just in case
2170	 * physical path information arrived before our async
2171	 * event handler was registered, but after anyone attaching
2172	 * to our disk device polled it.
2173	 */
2174	disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2175
2176	/*
2177	 * Schedule a periodic media polling events.
2178	 */
2179	callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2180	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2181	    (cgd->inq_flags & SID_AEN) == 0 &&
2182	    da_poll_period != 0)
2183		callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2184		    damediapoll, periph);
2185
2186	xpt_schedule(periph, CAM_PRIORITY_DEV);
2187
2188	return(CAM_REQ_CMP);
2189}
2190
2191static void
2192dastart(struct cam_periph *periph, union ccb *start_ccb)
2193{
2194	struct da_softc *softc;
2195
2196	softc = (struct da_softc *)periph->softc;
2197
2198	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2199
2200skipstate:
2201	switch (softc->state) {
2202	case DA_STATE_NORMAL:
2203	{
2204		struct bio *bp;
2205		uint8_t tag_code;
2206
2207		/* Run BIO_DELETE if not running yet. */
2208		if (!softc->delete_running &&
2209		    (bp = bioq_first(&softc->delete_queue)) != NULL) {
2210			if (softc->delete_func != NULL) {
2211				softc->delete_func(periph, start_ccb, bp);
2212				goto out;
2213			} else {
2214				bioq_flush(&softc->delete_queue, NULL, 0);
2215				/* FALLTHROUGH */
2216			}
2217		}
2218
2219		/* Run regular command. */
2220		bp = bioq_takefirst(&softc->bio_queue);
2221		if (bp == NULL) {
2222			if (softc->tur) {
2223				softc->tur = 0;
2224				scsi_test_unit_ready(&start_ccb->csio,
2225				     /*retries*/ da_retry_count,
2226				     dadone,
2227				     MSG_SIMPLE_Q_TAG,
2228				     SSD_FULL_SIZE,
2229				     da_default_timeout * 1000);
2230				start_ccb->ccb_h.ccb_bp = NULL;
2231				start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2232				xpt_action(start_ccb);
2233			} else
2234				xpt_release_ccb(start_ccb);
2235			break;
2236		}
2237		if (softc->tur) {
2238			softc->tur = 0;
2239			cam_periph_release_locked(periph);
2240		}
2241
2242		if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2243		    (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2244			softc->flags &= ~DA_FLAG_NEED_OTAG;
2245			softc->flags |= DA_FLAG_WAS_OTAG;
2246			tag_code = MSG_ORDERED_Q_TAG;
2247		} else {
2248			tag_code = MSG_SIMPLE_Q_TAG;
2249		}
2250
2251		switch (bp->bio_cmd) {
2252		case BIO_WRITE:
2253			softc->flags |= DA_FLAG_DIRTY;
2254			/* FALLTHROUGH */
2255		case BIO_READ:
2256			scsi_read_write(&start_ccb->csio,
2257					/*retries*/da_retry_count,
2258					/*cbfcnp*/dadone,
2259					/*tag_action*/tag_code,
2260					/*read_op*/(bp->bio_cmd == BIO_READ ?
2261					SCSI_RW_READ : SCSI_RW_WRITE) |
2262					((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2263					SCSI_RW_BIO : 0),
2264					/*byte2*/0,
2265					softc->minimum_cmd_size,
2266					/*lba*/bp->bio_pblkno,
2267					/*block_count*/bp->bio_bcount /
2268					softc->params.secsize,
2269					/*data_ptr*/ (bp->bio_flags &
2270					BIO_UNMAPPED) != 0 ? (void *)bp :
2271					bp->bio_data,
2272					/*dxfer_len*/ bp->bio_bcount,
2273					/*sense_len*/SSD_FULL_SIZE,
2274					da_default_timeout * 1000);
2275			break;
2276		case BIO_FLUSH:
2277			/*
2278			 * BIO_FLUSH doesn't currently communicate
2279			 * range data, so we synchronize the cache
2280			 * over the whole disk.  We also force
2281			 * ordered tag semantics the flush applies
2282			 * to all previously queued I/O.
2283			 */
2284			scsi_synchronize_cache(&start_ccb->csio,
2285					       /*retries*/1,
2286					       /*cbfcnp*/dadone,
2287					       MSG_ORDERED_Q_TAG,
2288					       /*begin_lba*/0,
2289					       /*lb_count*/0,
2290					       SSD_FULL_SIZE,
2291					       da_default_timeout*1000);
2292			break;
2293		}
2294		start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2295		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2296
2297out:
2298		LIST_INSERT_HEAD(&softc->pending_ccbs,
2299				 &start_ccb->ccb_h, periph_links.le);
2300
2301		/* We expect a unit attention from this device */
2302		if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2303			start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2304			softc->flags &= ~DA_FLAG_RETRY_UA;
2305		}
2306
2307		start_ccb->ccb_h.ccb_bp = bp;
2308		softc->refcount++;
2309		cam_periph_unlock(periph);
2310		xpt_action(start_ccb);
2311		cam_periph_lock(periph);
2312		softc->refcount--;
2313
2314		/* May have more work to do, so ensure we stay scheduled */
2315		daschedule(periph);
2316		break;
2317	}
2318	case DA_STATE_PROBE_RC:
2319	{
2320		struct scsi_read_capacity_data *rcap;
2321
2322		rcap = (struct scsi_read_capacity_data *)
2323		    malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2324		if (rcap == NULL) {
2325			printf("dastart: Couldn't malloc read_capacity data\n");
2326			/* da_free_periph??? */
2327			break;
2328		}
2329		scsi_read_capacity(&start_ccb->csio,
2330				   /*retries*/da_retry_count,
2331				   dadone,
2332				   MSG_SIMPLE_Q_TAG,
2333				   rcap,
2334				   SSD_FULL_SIZE,
2335				   /*timeout*/5000);
2336		start_ccb->ccb_h.ccb_bp = NULL;
2337		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2338		xpt_action(start_ccb);
2339		break;
2340	}
2341	case DA_STATE_PROBE_RC16:
2342	{
2343		struct scsi_read_capacity_data_long *rcaplong;
2344
2345		rcaplong = (struct scsi_read_capacity_data_long *)
2346			malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2347		if (rcaplong == NULL) {
2348			printf("dastart: Couldn't malloc read_capacity data\n");
2349			/* da_free_periph??? */
2350			break;
2351		}
2352		scsi_read_capacity_16(&start_ccb->csio,
2353				      /*retries*/ da_retry_count,
2354				      /*cbfcnp*/ dadone,
2355				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
2356				      /*lba*/ 0,
2357				      /*reladr*/ 0,
2358				      /*pmi*/ 0,
2359				      /*rcap_buf*/ (uint8_t *)rcaplong,
2360				      /*rcap_buf_len*/ sizeof(*rcaplong),
2361				      /*sense_len*/ SSD_FULL_SIZE,
2362				      /*timeout*/ da_default_timeout * 1000);
2363		start_ccb->ccb_h.ccb_bp = NULL;
2364		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2365		xpt_action(start_ccb);
2366		break;
2367	}
2368	case DA_STATE_PROBE_LBP:
2369	{
2370		struct scsi_vpd_logical_block_prov *lbp;
2371
2372		if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2373			/*
2374			 * If we get here we don't support any SBC-3 delete
2375			 * methods with UNMAP as the Logical Block Provisioning
2376			 * VPD page support is required for devices which
2377			 * support it according to T10/1799-D Revision 31
2378			 * however older revisions of the spec don't mandate
2379			 * this so we currently don't remove these methods
2380			 * from the available set.
2381			 */
2382			softc->state = DA_STATE_PROBE_BLK_LIMITS;
2383			goto skipstate;
2384		}
2385
2386		lbp = (struct scsi_vpd_logical_block_prov *)
2387			malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2388
2389		if (lbp == NULL) {
2390			printf("dastart: Couldn't malloc lbp data\n");
2391			/* da_free_periph??? */
2392			break;
2393		}
2394
2395		scsi_inquiry(&start_ccb->csio,
2396			     /*retries*/da_retry_count,
2397			     /*cbfcnp*/dadone,
2398			     /*tag_action*/MSG_SIMPLE_Q_TAG,
2399			     /*inq_buf*/(u_int8_t *)lbp,
2400			     /*inq_len*/sizeof(*lbp),
2401			     /*evpd*/TRUE,
2402			     /*page_code*/SVPD_LBP,
2403			     /*sense_len*/SSD_MIN_SIZE,
2404			     /*timeout*/da_default_timeout * 1000);
2405		start_ccb->ccb_h.ccb_bp = NULL;
2406		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2407		xpt_action(start_ccb);
2408		break;
2409	}
2410	case DA_STATE_PROBE_BLK_LIMITS:
2411	{
2412		struct scsi_vpd_block_limits *block_limits;
2413
2414		if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2415			/* Not supported skip to next probe */
2416			softc->state = DA_STATE_PROBE_BDC;
2417			goto skipstate;
2418		}
2419
2420		block_limits = (struct scsi_vpd_block_limits *)
2421			malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2422
2423		if (block_limits == NULL) {
2424			printf("dastart: Couldn't malloc block_limits data\n");
2425			/* da_free_periph??? */
2426			break;
2427		}
2428
2429		scsi_inquiry(&start_ccb->csio,
2430			     /*retries*/da_retry_count,
2431			     /*cbfcnp*/dadone,
2432			     /*tag_action*/MSG_SIMPLE_Q_TAG,
2433			     /*inq_buf*/(u_int8_t *)block_limits,
2434			     /*inq_len*/sizeof(*block_limits),
2435			     /*evpd*/TRUE,
2436			     /*page_code*/SVPD_BLOCK_LIMITS,
2437			     /*sense_len*/SSD_MIN_SIZE,
2438			     /*timeout*/da_default_timeout * 1000);
2439		start_ccb->ccb_h.ccb_bp = NULL;
2440		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2441		xpt_action(start_ccb);
2442		break;
2443	}
2444	case DA_STATE_PROBE_BDC:
2445	{
2446		struct scsi_vpd_block_characteristics *bdc;
2447
2448		if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2449			softc->state = DA_STATE_PROBE_ATA;
2450			goto skipstate;
2451		}
2452
2453		bdc = (struct scsi_vpd_block_characteristics *)
2454			malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2455
2456		if (bdc == NULL) {
2457			printf("dastart: Couldn't malloc bdc data\n");
2458			/* da_free_periph??? */
2459			break;
2460		}
2461
2462		scsi_inquiry(&start_ccb->csio,
2463			     /*retries*/da_retry_count,
2464			     /*cbfcnp*/dadone,
2465			     /*tag_action*/MSG_SIMPLE_Q_TAG,
2466			     /*inq_buf*/(u_int8_t *)bdc,
2467			     /*inq_len*/sizeof(*bdc),
2468			     /*evpd*/TRUE,
2469			     /*page_code*/SVPD_BDC,
2470			     /*sense_len*/SSD_MIN_SIZE,
2471			     /*timeout*/da_default_timeout * 1000);
2472		start_ccb->ccb_h.ccb_bp = NULL;
2473		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2474		xpt_action(start_ccb);
2475		break;
2476	}
2477	case DA_STATE_PROBE_ATA:
2478	{
2479		struct ata_params *ata_params;
2480
2481		if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2482			daprobedone(periph, start_ccb);
2483			break;
2484		}
2485
2486		ata_params = (struct ata_params*)
2487			malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2488
2489		if (ata_params == NULL) {
2490			printf("dastart: Couldn't malloc ata_params data\n");
2491			/* da_free_periph??? */
2492			break;
2493		}
2494
2495		scsi_ata_identify(&start_ccb->csio,
2496				  /*retries*/da_retry_count,
2497				  /*cbfcnp*/dadone,
2498                                  /*tag_action*/MSG_SIMPLE_Q_TAG,
2499				  /*data_ptr*/(u_int8_t *)ata_params,
2500				  /*dxfer_len*/sizeof(*ata_params),
2501				  /*sense_len*/SSD_FULL_SIZE,
2502				  /*timeout*/da_default_timeout * 1000);
2503		start_ccb->ccb_h.ccb_bp = NULL;
2504		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2505		xpt_action(start_ccb);
2506		break;
2507	}
2508	}
2509}
2510
2511/*
2512 * In each of the methods below, while its the caller's
2513 * responsibility to ensure the request will fit into a
2514 * single device request, we might have changed the delete
2515 * method due to the device incorrectly advertising either
2516 * its supported methods or limits.
2517 *
2518 * To prevent this causing further issues we validate the
2519 * against the methods limits, and warn which would
2520 * otherwise be unnecessary.
2521 */
2522static void
2523da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2524{
2525	struct da_softc *softc = (struct da_softc *)periph->softc;;
2526	struct bio *bp1;
2527	uint8_t *buf = softc->unmap_buf;
2528	uint64_t lba, lastlba = (uint64_t)-1;
2529	uint64_t totalcount = 0;
2530	uint64_t count;
2531	uint32_t lastcount = 0, c;
2532	uint32_t off, ranges = 0;
2533
2534	/*
2535	 * Currently this doesn't take the UNMAP
2536	 * Granularity and Granularity Alignment
2537	 * fields into account.
2538	 *
2539	 * This could result in both unoptimal unmap
2540	 * requests as as well as UNMAP calls unmapping
2541	 * fewer LBA's than requested.
2542	 */
2543
2544	softc->delete_running = 1;
2545	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2546	bp1 = bp;
2547	do {
2548		bioq_remove(&softc->delete_queue, bp1);
2549		if (bp1 != bp)
2550			bioq_insert_tail(&softc->delete_run_queue, bp1);
2551		lba = bp1->bio_pblkno;
2552		count = bp1->bio_bcount / softc->params.secsize;
2553
2554		/* Try to extend the previous range. */
2555		if (lba == lastlba) {
2556			c = min(count, softc->unmap_max_lba - lastcount);
2557			lastcount += c;
2558			off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2559			      UNMAP_HEAD_SIZE;
2560			scsi_ulto4b(lastcount, &buf[off + 8]);
2561			count -= c;
2562			lba +=c;
2563			totalcount += c;
2564		}
2565
2566		while (count > 0) {
2567			c = min(count, softc->unmap_max_lba);
2568			if (totalcount + c > softc->unmap_max_lba ||
2569			    ranges >= softc->unmap_max_ranges) {
2570				xpt_print(periph->path,
2571				    "%s issuing short delete %ld > %ld"
2572				    "|| %d >= %d",
2573				    da_delete_method_desc[softc->delete_method],
2574				    totalcount + c, softc->unmap_max_lba,
2575				    ranges, softc->unmap_max_ranges);
2576				break;
2577			}
2578			off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2579			scsi_u64to8b(lba, &buf[off + 0]);
2580			scsi_ulto4b(c, &buf[off + 8]);
2581			lba += c;
2582			totalcount += c;
2583			ranges++;
2584			count -= c;
2585			lastcount = c;
2586		}
2587		lastlba = lba;
2588		bp1 = bioq_first(&softc->delete_queue);
2589		if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2590		    totalcount + bp1->bio_bcount /
2591		    softc->params.secsize > softc->unmap_max_lba)
2592			break;
2593	} while (1);
2594	scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2595	scsi_ulto2b(ranges * 16, &buf[2]);
2596
2597	scsi_unmap(&ccb->csio,
2598		   /*retries*/da_retry_count,
2599		   /*cbfcnp*/dadone,
2600		   /*tag_action*/MSG_SIMPLE_Q_TAG,
2601		   /*byte2*/0,
2602		   /*data_ptr*/ buf,
2603		   /*dxfer_len*/ ranges * 16 + 8,
2604		   /*sense_len*/SSD_FULL_SIZE,
2605		   da_default_timeout * 1000);
2606	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2607	ccb->ccb_h.flags |= CAM_UNLOCKED;
2608}
2609
2610static void
2611da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2612{
2613	struct da_softc *softc = (struct da_softc *)periph->softc;
2614	struct bio *bp1;
2615	uint8_t *buf = softc->unmap_buf;
2616	uint64_t lastlba = (uint64_t)-1;
2617	uint64_t count;
2618	uint64_t lba;
2619	uint32_t lastcount = 0, c, requestcount;
2620	int ranges = 0, off, block_count;
2621
2622	softc->delete_running = 1;
2623	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2624	bp1 = bp;
2625	do {
2626		bioq_remove(&softc->delete_queue, bp1);
2627		if (bp1 != bp)
2628			bioq_insert_tail(&softc->delete_run_queue, bp1);
2629		lba = bp1->bio_pblkno;
2630		count = bp1->bio_bcount / softc->params.secsize;
2631		requestcount = count;
2632
2633		/* Try to extend the previous range. */
2634		if (lba == lastlba) {
2635			c = min(count, ATA_DSM_RANGE_MAX - lastcount);
2636			lastcount += c;
2637			off = (ranges - 1) * 8;
2638			buf[off + 6] = lastcount & 0xff;
2639			buf[off + 7] = (lastcount >> 8) & 0xff;
2640			count -= c;
2641			lba += c;
2642		}
2643
2644		while (count > 0) {
2645			c = min(count, ATA_DSM_RANGE_MAX);
2646			off = ranges * 8;
2647
2648			buf[off + 0] = lba & 0xff;
2649			buf[off + 1] = (lba >> 8) & 0xff;
2650			buf[off + 2] = (lba >> 16) & 0xff;
2651			buf[off + 3] = (lba >> 24) & 0xff;
2652			buf[off + 4] = (lba >> 32) & 0xff;
2653			buf[off + 5] = (lba >> 40) & 0xff;
2654			buf[off + 6] = c & 0xff;
2655			buf[off + 7] = (c >> 8) & 0xff;
2656			lba += c;
2657			ranges++;
2658			count -= c;
2659			lastcount = c;
2660			if (count != 0 && ranges == softc->trim_max_ranges) {
2661				xpt_print(periph->path,
2662				    "%s issuing short delete %ld > %ld",
2663				    da_delete_method_desc[softc->delete_method],
2664				    requestcount,
2665				    (softc->trim_max_ranges - ranges) *
2666				    ATA_DSM_RANGE_MAX);
2667				break;
2668			}
2669		}
2670		lastlba = lba;
2671		bp1 = bioq_first(&softc->delete_queue);
2672		if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2673		    (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2674			break;
2675	} while (1);
2676
2677	block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2678	scsi_ata_trim(&ccb->csio,
2679		      /*retries*/da_retry_count,
2680		      /*cbfcnp*/dadone,
2681		      /*tag_action*/MSG_SIMPLE_Q_TAG,
2682		      block_count,
2683		      /*data_ptr*/buf,
2684		      /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2685		      /*sense_len*/SSD_FULL_SIZE,
2686		      da_default_timeout * 1000);
2687	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2688	ccb->ccb_h.flags |= CAM_UNLOCKED;
2689}
2690
2691/*
2692 * We calculate ws_max_blks here based off d_delmaxsize instead
2693 * of using softc->ws_max_blks as it is absolute max for the
2694 * device not the protocol max which may well be lower
2695 */
2696static void
2697da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2698{
2699	struct da_softc *softc;
2700	struct bio *bp1;
2701	uint64_t ws_max_blks;
2702	uint64_t lba;
2703	uint64_t count; /* forward compat with WS32 */
2704
2705	softc = (struct da_softc *)periph->softc;
2706	ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2707	softc->delete_running = 1;
2708	lba = bp->bio_pblkno;
2709	count = 0;
2710	bp1 = bp;
2711	do {
2712		bioq_remove(&softc->delete_queue, bp1);
2713		if (bp1 != bp)
2714			bioq_insert_tail(&softc->delete_run_queue, bp1);
2715		count += bp1->bio_bcount / softc->params.secsize;
2716		if (count > ws_max_blks) {
2717			count = min(count, ws_max_blks);
2718			xpt_print(periph->path,
2719			    "%s issuing short delete %ld > %ld",
2720			    da_delete_method_desc[softc->delete_method],
2721			    count, ws_max_blks);
2722			break;
2723		}
2724		bp1 = bioq_first(&softc->delete_queue);
2725		if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2726		    count + bp1->bio_bcount /
2727		    softc->params.secsize > ws_max_blks)
2728			break;
2729	} while (1);
2730
2731	scsi_write_same(&ccb->csio,
2732			/*retries*/da_retry_count,
2733			/*cbfcnp*/dadone,
2734			/*tag_action*/MSG_SIMPLE_Q_TAG,
2735			/*byte2*/softc->delete_method ==
2736			    DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2737			softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2738			/*lba*/lba,
2739			/*block_count*/count,
2740			/*data_ptr*/ __DECONST(void *, zero_region),
2741			/*dxfer_len*/ softc->params.secsize,
2742			/*sense_len*/SSD_FULL_SIZE,
2743			da_default_timeout * 1000);
2744	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2745	ccb->ccb_h.flags |= CAM_UNLOCKED;
2746}
2747
2748static int
2749cmd6workaround(union ccb *ccb)
2750{
2751	struct scsi_rw_6 cmd6;
2752	struct scsi_rw_10 *cmd10;
2753	struct da_softc *softc;
2754	u_int8_t *cdb;
2755	struct bio *bp;
2756	int frozen;
2757
2758	cdb = ccb->csio.cdb_io.cdb_bytes;
2759	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2760
2761	if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2762		da_delete_methods old_method = softc->delete_method;
2763
2764		/*
2765		 * Typically there are two reasons for failure here
2766		 * 1. Delete method was detected as supported but isn't
2767		 * 2. Delete failed due to invalid params e.g. too big
2768		 *
2769		 * While we will attempt to choose an alternative delete method
2770		 * this may result in short deletes if the existing delete
2771		 * requests from geom are big for the new method choosen.
2772		 *
2773		 * This method assumes that the error which triggered this
2774		 * will not retry the io otherwise a panic will occur
2775		 */
2776		dadeleteflag(softc, old_method, 0);
2777		dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2778		if (softc->delete_method == DA_DELETE_DISABLE)
2779			xpt_print(ccb->ccb_h.path,
2780				  "%s failed, disabling BIO_DELETE\n",
2781				  da_delete_method_desc[old_method]);
2782		else
2783			xpt_print(ccb->ccb_h.path,
2784				  "%s failed, switching to %s BIO_DELETE\n",
2785				  da_delete_method_desc[old_method],
2786				  da_delete_method_desc[softc->delete_method]);
2787
2788		if (DA_SIO) {
2789			while ((bp = bioq_takefirst(&softc->delete_run_queue))
2790			    != NULL)
2791				bioq_disksort(&softc->delete_queue, bp);
2792		} else {
2793			while ((bp = bioq_takefirst(&softc->delete_run_queue))
2794			    != NULL)
2795				bioq_insert_tail(&softc->delete_queue, bp);
2796		}
2797		bioq_insert_tail(&softc->delete_queue,
2798		    (struct bio *)ccb->ccb_h.ccb_bp);
2799		ccb->ccb_h.ccb_bp = NULL;
2800		return (0);
2801	}
2802
2803	/* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2804	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2805	    (*cdb == PREVENT_ALLOW) &&
2806	    (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2807		if (bootverbose)
2808			xpt_print(ccb->ccb_h.path,
2809			    "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2810		softc->quirks |= DA_Q_NO_PREVENT;
2811		return (0);
2812	}
2813
2814	/* Detect unsupported SYNCHRONIZE CACHE(10). */
2815	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2816	    (*cdb == SYNCHRONIZE_CACHE) &&
2817	    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2818		if (bootverbose)
2819			xpt_print(ccb->ccb_h.path,
2820			    "SYNCHRONIZE CACHE(10) not supported.\n");
2821		softc->quirks |= DA_Q_NO_SYNC_CACHE;
2822		softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2823		return (0);
2824	}
2825
2826	/* Translation only possible if CDB is an array and cmd is R/W6 */
2827	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2828	    (*cdb != READ_6 && *cdb != WRITE_6))
2829		return 0;
2830
2831	xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2832	    "increasing minimum_cmd_size to 10.\n");
2833 	softc->minimum_cmd_size = 10;
2834
2835	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2836	cmd10 = (struct scsi_rw_10 *)cdb;
2837	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2838	cmd10->byte2 = 0;
2839	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2840	cmd10->reserved = 0;
2841	scsi_ulto2b(cmd6.length, cmd10->length);
2842	cmd10->control = cmd6.control;
2843	ccb->csio.cdb_len = sizeof(*cmd10);
2844
2845	/* Requeue request, unfreezing queue if necessary */
2846	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2847 	ccb->ccb_h.status = CAM_REQUEUE_REQ;
2848	xpt_action(ccb);
2849	if (frozen) {
2850		cam_release_devq(ccb->ccb_h.path,
2851				 /*relsim_flags*/0,
2852				 /*reduction*/0,
2853				 /*timeout*/0,
2854				 /*getcount_only*/0);
2855	}
2856	return (ERESTART);
2857}
2858
2859static void
2860dadone(struct cam_periph *periph, union ccb *done_ccb)
2861{
2862	struct da_softc *softc;
2863	struct ccb_scsiio *csio;
2864	u_int32_t  priority;
2865	da_ccb_state state;
2866
2867	softc = (struct da_softc *)periph->softc;
2868	priority = done_ccb->ccb_h.pinfo.priority;
2869
2870	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2871
2872	csio = &done_ccb->csio;
2873	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2874	switch (state) {
2875	case DA_CCB_BUFFER_IO:
2876	case DA_CCB_DELETE:
2877	{
2878		struct bio *bp, *bp1;
2879
2880		cam_periph_lock(periph);
2881		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2882		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2883			int error;
2884			int sf;
2885
2886			if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2887				sf = SF_RETRY_UA;
2888			else
2889				sf = 0;
2890
2891			error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2892			if (error == ERESTART) {
2893				/*
2894				 * A retry was scheduled, so
2895				 * just return.
2896				 */
2897				cam_periph_unlock(periph);
2898				return;
2899			}
2900			bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2901			if (error != 0) {
2902				int queued_error;
2903
2904				/*
2905				 * return all queued I/O with EIO, so that
2906				 * the client can retry these I/Os in the
2907				 * proper order should it attempt to recover.
2908				 */
2909				queued_error = EIO;
2910
2911				if (error == ENXIO
2912				 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2913					/*
2914					 * Catastrophic error.  Mark our pack as
2915					 * invalid.
2916					 */
2917					/*
2918					 * XXX See if this is really a media
2919					 * XXX change first?
2920					 */
2921					xpt_print(periph->path,
2922					    "Invalidating pack\n");
2923					softc->flags |= DA_FLAG_PACK_INVALID;
2924					queued_error = ENXIO;
2925				}
2926				bioq_flush(&softc->bio_queue, NULL,
2927					   queued_error);
2928				if (bp != NULL) {
2929					bp->bio_error = error;
2930					bp->bio_resid = bp->bio_bcount;
2931					bp->bio_flags |= BIO_ERROR;
2932				}
2933			} else if (bp != NULL) {
2934				if (state == DA_CCB_DELETE)
2935					bp->bio_resid = 0;
2936				else
2937					bp->bio_resid = csio->resid;
2938				bp->bio_error = 0;
2939				if (bp->bio_resid != 0)
2940					bp->bio_flags |= BIO_ERROR;
2941			}
2942			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2943				cam_release_devq(done_ccb->ccb_h.path,
2944						 /*relsim_flags*/0,
2945						 /*reduction*/0,
2946						 /*timeout*/0,
2947						 /*getcount_only*/0);
2948		} else if (bp != NULL) {
2949			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2950				panic("REQ_CMP with QFRZN");
2951			if (state == DA_CCB_DELETE)
2952				bp->bio_resid = 0;
2953			else
2954				bp->bio_resid = csio->resid;
2955			if (csio->resid > 0)
2956				bp->bio_flags |= BIO_ERROR;
2957			if (softc->error_inject != 0) {
2958				bp->bio_error = softc->error_inject;
2959				bp->bio_resid = bp->bio_bcount;
2960				bp->bio_flags |= BIO_ERROR;
2961				softc->error_inject = 0;
2962			}
2963		}
2964
2965		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2966		if (LIST_EMPTY(&softc->pending_ccbs))
2967			softc->flags |= DA_FLAG_WAS_OTAG;
2968
2969		xpt_release_ccb(done_ccb);
2970		if (state == DA_CCB_DELETE) {
2971			TAILQ_HEAD(, bio) queue;
2972
2973			TAILQ_INIT(&queue);
2974			TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
2975			softc->delete_run_queue.insert_point = NULL;
2976			softc->delete_running = 0;
2977			daschedule(periph);
2978			cam_periph_unlock(periph);
2979			while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
2980				TAILQ_REMOVE(&queue, bp1, bio_queue);
2981				bp1->bio_error = bp->bio_error;
2982				if (bp->bio_flags & BIO_ERROR) {
2983					bp1->bio_flags |= BIO_ERROR;
2984					bp1->bio_resid = bp1->bio_bcount;
2985				} else
2986					bp1->bio_resid = 0;
2987				biodone(bp1);
2988			}
2989		} else
2990			cam_periph_unlock(periph);
2991		if (bp != NULL)
2992			biodone(bp);
2993		return;
2994	}
2995	case DA_CCB_PROBE_RC:
2996	case DA_CCB_PROBE_RC16:
2997	{
2998		struct	   scsi_read_capacity_data *rdcap;
2999		struct     scsi_read_capacity_data_long *rcaplong;
3000		char	   announce_buf[80];
3001		int	   lbp;
3002
3003		lbp = 0;
3004		rdcap = NULL;
3005		rcaplong = NULL;
3006		if (state == DA_CCB_PROBE_RC)
3007			rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3008		else
3009			rcaplong = (struct scsi_read_capacity_data_long *)
3010				csio->data_ptr;
3011
3012		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3013			struct disk_params *dp;
3014			uint32_t block_size;
3015			uint64_t maxsector;
3016			u_int lbppbe;	/* LB per physical block exponent. */
3017			u_int lalba;	/* Lowest aligned LBA. */
3018
3019			if (state == DA_CCB_PROBE_RC) {
3020				block_size = scsi_4btoul(rdcap->length);
3021				maxsector = scsi_4btoul(rdcap->addr);
3022				lbppbe = 0;
3023				lalba = 0;
3024
3025				/*
3026				 * According to SBC-2, if the standard 10
3027				 * byte READ CAPACITY command returns 2^32,
3028				 * we should issue the 16 byte version of
3029				 * the command, since the device in question
3030				 * has more sectors than can be represented
3031				 * with the short version of the command.
3032				 */
3033				if (maxsector == 0xffffffff) {
3034					free(rdcap, M_SCSIDA);
3035					xpt_release_ccb(done_ccb);
3036					softc->state = DA_STATE_PROBE_RC16;
3037					xpt_schedule(periph, priority);
3038					return;
3039				}
3040			} else {
3041				block_size = scsi_4btoul(rcaplong->length);
3042				maxsector = scsi_8btou64(rcaplong->addr);
3043				lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3044				lalba = scsi_2btoul(rcaplong->lalba_lbp);
3045			}
3046
3047			/*
3048			 * Because GEOM code just will panic us if we
3049			 * give them an 'illegal' value we'll avoid that
3050			 * here.
3051			 */
3052			if (block_size == 0 && maxsector == 0) {
3053				block_size = 512;
3054				maxsector = -1;
3055			}
3056			if (block_size >= MAXPHYS || block_size == 0) {
3057				xpt_print(periph->path,
3058				    "unsupportable block size %ju\n",
3059				    (uintmax_t) block_size);
3060				announce_buf[0] = '\0';
3061				cam_periph_invalidate(periph);
3062			} else {
3063				/*
3064				 * We pass rcaplong into dasetgeom(),
3065				 * because it will only use it if it is
3066				 * non-NULL.
3067				 */
3068				dasetgeom(periph, block_size, maxsector,
3069					  rcaplong, sizeof(*rcaplong));
3070				lbp = (lalba & SRC16_LBPME_A);
3071				dp = &softc->params;
3072				snprintf(announce_buf, sizeof(announce_buf),
3073				        "%juMB (%ju %u byte sectors: %dH %dS/T "
3074                                        "%dC)", (uintmax_t)
3075	                                (((uintmax_t)dp->secsize *
3076				        dp->sectors) / (1024*1024)),
3077			                (uintmax_t)dp->sectors,
3078				        dp->secsize, dp->heads,
3079                                        dp->secs_per_track, dp->cylinders);
3080			}
3081		} else {
3082			int	error;
3083
3084			announce_buf[0] = '\0';
3085
3086			/*
3087			 * Retry any UNIT ATTENTION type errors.  They
3088			 * are expected at boot.
3089			 */
3090			error = daerror(done_ccb, CAM_RETRY_SELTO,
3091					SF_RETRY_UA|SF_NO_PRINT);
3092			if (error == ERESTART) {
3093				/*
3094				 * A retry was scheuled, so
3095				 * just return.
3096				 */
3097				return;
3098			} else if (error != 0) {
3099				int asc, ascq;
3100				int sense_key, error_code;
3101				int have_sense;
3102				cam_status status;
3103				struct ccb_getdev cgd;
3104
3105				/* Don't wedge this device's queue */
3106				status = done_ccb->ccb_h.status;
3107				if ((status & CAM_DEV_QFRZN) != 0)
3108					cam_release_devq(done_ccb->ccb_h.path,
3109							 /*relsim_flags*/0,
3110							 /*reduction*/0,
3111							 /*timeout*/0,
3112							 /*getcount_only*/0);
3113
3114
3115				xpt_setup_ccb(&cgd.ccb_h,
3116					      done_ccb->ccb_h.path,
3117					      CAM_PRIORITY_NORMAL);
3118				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3119				xpt_action((union ccb *)&cgd);
3120
3121				if (scsi_extract_sense_ccb(done_ccb,
3122				    &error_code, &sense_key, &asc, &ascq))
3123					have_sense = TRUE;
3124				else
3125					have_sense = FALSE;
3126
3127				/*
3128				 * If we tried READ CAPACITY(16) and failed,
3129				 * fallback to READ CAPACITY(10).
3130				 */
3131				if ((state == DA_CCB_PROBE_RC16) &&
3132				    (softc->flags & DA_FLAG_CAN_RC16) &&
3133				    (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3134					CAM_REQ_INVALID) ||
3135				     ((have_sense) &&
3136				      (error_code == SSD_CURRENT_ERROR) &&
3137				      (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3138					softc->flags &= ~DA_FLAG_CAN_RC16;
3139					free(rdcap, M_SCSIDA);
3140					xpt_release_ccb(done_ccb);
3141					softc->state = DA_STATE_PROBE_RC;
3142					xpt_schedule(periph, priority);
3143					return;
3144				} else
3145				/*
3146				 * Attach to anything that claims to be a
3147				 * direct access or optical disk device,
3148				 * as long as it doesn't return a "Logical
3149				 * unit not supported" (0x25) error.
3150				 */
3151				if ((have_sense) && (asc != 0x25)
3152				 && (error_code == SSD_CURRENT_ERROR)) {
3153					const char *sense_key_desc;
3154					const char *asc_desc;
3155
3156					dasetgeom(periph, 512, -1, NULL, 0);
3157					scsi_sense_desc(sense_key, asc, ascq,
3158							&cgd.inq_data,
3159							&sense_key_desc,
3160							&asc_desc);
3161					snprintf(announce_buf,
3162					    sizeof(announce_buf),
3163						"Attempt to query device "
3164						"size failed: %s, %s",
3165						sense_key_desc,
3166						asc_desc);
3167				} else {
3168					if (have_sense)
3169						scsi_sense_print(
3170							&done_ccb->csio);
3171					else {
3172						xpt_print(periph->path,
3173						    "got CAM status %#x\n",
3174						    done_ccb->ccb_h.status);
3175					}
3176
3177					xpt_print(periph->path, "fatal error, "
3178					    "failed to attach to device\n");
3179
3180					/*
3181					 * Free up resources.
3182					 */
3183					cam_periph_invalidate(periph);
3184				}
3185			}
3186		}
3187		free(csio->data_ptr, M_SCSIDA);
3188		if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
3189			/*
3190			 * Create our sysctl variables, now that we know
3191			 * we have successfully attached.
3192			 */
3193			/* increase the refcount */
3194			if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3195				taskqueue_enqueue(taskqueue_thread,
3196						  &softc->sysctl_task);
3197				xpt_announce_periph(periph, announce_buf);
3198				xpt_announce_quirks(periph, softc->quirks,
3199				    DA_Q_BIT_STRING);
3200			} else {
3201				xpt_print(periph->path, "fatal error, "
3202				    "could not acquire reference count\n");
3203			}
3204		}
3205
3206		/* Ensure re-probe doesn't see old delete. */
3207		softc->delete_available = 0;
3208		if (lbp) {
3209			/*
3210			 * Based on older SBC-3 spec revisions
3211			 * any of the UNMAP methods "may" be
3212			 * available via LBP given this flag so
3213			 * we flag all of them as availble and
3214			 * then remove those which further
3215			 * probes confirm aren't available
3216			 * later.
3217			 *
3218			 * We could also check readcap(16) p_type
3219			 * flag to exclude one or more invalid
3220			 * write same (X) types here
3221			 */
3222			dadeleteflag(softc, DA_DELETE_WS16, 1);
3223			dadeleteflag(softc, DA_DELETE_WS10, 1);
3224			dadeleteflag(softc, DA_DELETE_ZERO, 1);
3225			dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3226
3227			xpt_release_ccb(done_ccb);
3228			softc->state = DA_STATE_PROBE_LBP;
3229			xpt_schedule(periph, priority);
3230			return;
3231		}
3232
3233		xpt_release_ccb(done_ccb);
3234		softc->state = DA_STATE_PROBE_BDC;
3235		xpt_schedule(periph, priority);
3236		return;
3237	}
3238	case DA_CCB_PROBE_LBP:
3239	{
3240		struct scsi_vpd_logical_block_prov *lbp;
3241
3242		lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3243
3244		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3245			/*
3246			 * T10/1799-D Revision 31 states at least one of these
3247			 * must be supported but we don't currently enforce this.
3248			 */
3249			dadeleteflag(softc, DA_DELETE_WS16,
3250				     (lbp->flags & SVPD_LBP_WS16));
3251			dadeleteflag(softc, DA_DELETE_WS10,
3252				     (lbp->flags & SVPD_LBP_WS10));
3253			dadeleteflag(softc, DA_DELETE_ZERO,
3254				     (lbp->flags & SVPD_LBP_WS10));
3255			dadeleteflag(softc, DA_DELETE_UNMAP,
3256				     (lbp->flags & SVPD_LBP_UNMAP));
3257
3258			if (lbp->flags & SVPD_LBP_UNMAP) {
3259				free(lbp, M_SCSIDA);
3260				xpt_release_ccb(done_ccb);
3261				softc->state = DA_STATE_PROBE_BLK_LIMITS;
3262				xpt_schedule(periph, priority);
3263				return;
3264			}
3265		} else {
3266			int error;
3267			error = daerror(done_ccb, CAM_RETRY_SELTO,
3268					SF_RETRY_UA|SF_NO_PRINT);
3269			if (error == ERESTART)
3270				return;
3271			else if (error != 0) {
3272				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3273					/* Don't wedge this device's queue */
3274					cam_release_devq(done_ccb->ccb_h.path,
3275							 /*relsim_flags*/0,
3276							 /*reduction*/0,
3277							 /*timeout*/0,
3278							 /*getcount_only*/0);
3279				}
3280
3281				/*
3282				 * Failure indicates we don't support any SBC-3
3283				 * delete methods with UNMAP
3284				 */
3285			}
3286		}
3287
3288		free(lbp, M_SCSIDA);
3289		xpt_release_ccb(done_ccb);
3290		softc->state = DA_STATE_PROBE_BDC;
3291		xpt_schedule(periph, priority);
3292		return;
3293	}
3294	case DA_CCB_PROBE_BLK_LIMITS:
3295	{
3296		struct scsi_vpd_block_limits *block_limits;
3297
3298		block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3299
3300		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3301			uint32_t max_unmap_lba_cnt = scsi_4btoul(
3302				block_limits->max_unmap_lba_cnt);
3303			uint32_t max_unmap_blk_cnt = scsi_4btoul(
3304				block_limits->max_unmap_blk_cnt);
3305			uint64_t ws_max_blks = scsi_8btou64(
3306				block_limits->max_write_same_length);
3307			/*
3308			 * We should already support UNMAP but we check lba
3309			 * and block count to be sure
3310			 */
3311			if (max_unmap_lba_cnt != 0x00L &&
3312			    max_unmap_blk_cnt != 0x00L) {
3313				softc->unmap_max_lba = max_unmap_lba_cnt;
3314				softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3315					UNMAP_MAX_RANGES);
3316			} else {
3317				/*
3318				 * Unexpected UNMAP limits which means the
3319				 * device doesn't actually support UNMAP
3320				 */
3321				dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3322			}
3323
3324			if (ws_max_blks != 0x00L)
3325				softc->ws_max_blks = ws_max_blks;
3326		} else {
3327			int error;
3328			error = daerror(done_ccb, CAM_RETRY_SELTO,
3329					SF_RETRY_UA|SF_NO_PRINT);
3330			if (error == ERESTART)
3331				return;
3332			else if (error != 0) {
3333				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3334					/* Don't wedge this device's queue */
3335					cam_release_devq(done_ccb->ccb_h.path,
3336							 /*relsim_flags*/0,
3337							 /*reduction*/0,
3338							 /*timeout*/0,
3339							 /*getcount_only*/0);
3340				}
3341
3342				/*
3343				 * Failure here doesn't mean UNMAP is not
3344				 * supported as this is an optional page.
3345				 */
3346				softc->unmap_max_lba = 1;
3347				softc->unmap_max_ranges = 1;
3348			}
3349		}
3350
3351		free(block_limits, M_SCSIDA);
3352		xpt_release_ccb(done_ccb);
3353		softc->state = DA_STATE_PROBE_BDC;
3354		xpt_schedule(periph, priority);
3355		return;
3356	}
3357	case DA_CCB_PROBE_BDC:
3358	{
3359		struct scsi_vpd_block_characteristics *bdc;
3360
3361		bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3362
3363		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3364			/*
3365			 * Disable queue sorting for non-rotational media
3366			 * by default.
3367			 */
3368			if (scsi_2btoul(bdc->medium_rotation_rate) ==
3369			    SVPD_BDC_RATE_NONE_ROTATING)
3370				softc->sort_io_queue = 0;
3371		} else {
3372			int error;
3373			error = daerror(done_ccb, CAM_RETRY_SELTO,
3374					SF_RETRY_UA|SF_NO_PRINT);
3375			if (error == ERESTART)
3376				return;
3377			else if (error != 0) {
3378				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3379					/* Don't wedge this device's queue */
3380					cam_release_devq(done_ccb->ccb_h.path,
3381							 /*relsim_flags*/0,
3382							 /*reduction*/0,
3383							 /*timeout*/0,
3384							 /*getcount_only*/0);
3385				}
3386			}
3387		}
3388
3389		free(bdc, M_SCSIDA);
3390		xpt_release_ccb(done_ccb);
3391		softc->state = DA_STATE_PROBE_ATA;
3392		xpt_schedule(periph, priority);
3393		return;
3394	}
3395	case DA_CCB_PROBE_ATA:
3396	{
3397		int i;
3398		struct ata_params *ata_params;
3399		int16_t *ptr;
3400
3401		ata_params = (struct ata_params *)csio->data_ptr;
3402		ptr = (uint16_t *)ata_params;
3403
3404		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3405			for (i = 0; i < sizeof(*ata_params) / 2; i++)
3406				ptr[i] = le16toh(ptr[i]);
3407			if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM) {
3408				dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3409				if (ata_params->max_dsm_blocks != 0)
3410					softc->trim_max_ranges = min(
3411					  softc->trim_max_ranges,
3412					  ata_params->max_dsm_blocks *
3413					  ATA_DSM_BLK_RANGES);
3414			}
3415			/*
3416			 * Disable queue sorting for non-rotational media
3417			 * by default.
3418			 */
3419			if (ata_params->media_rotation_rate == 1)
3420				softc->sort_io_queue = 0;
3421		} else {
3422			int error;
3423			error = daerror(done_ccb, CAM_RETRY_SELTO,
3424					SF_RETRY_UA|SF_NO_PRINT);
3425			if (error == ERESTART)
3426				return;
3427			else if (error != 0) {
3428				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3429					/* Don't wedge this device's queue */
3430					cam_release_devq(done_ccb->ccb_h.path,
3431							 /*relsim_flags*/0,
3432							 /*reduction*/0,
3433							 /*timeout*/0,
3434							 /*getcount_only*/0);
3435				}
3436			}
3437		}
3438
3439		free(ata_params, M_SCSIDA);
3440		daprobedone(periph, done_ccb);
3441		return;
3442	}
3443	case DA_CCB_DUMP:
3444		/* No-op.  We're polling */
3445		return;
3446	case DA_CCB_TUR:
3447	{
3448		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3449
3450			if (daerror(done_ccb, CAM_RETRY_SELTO,
3451			    SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3452			    ERESTART)
3453				return;
3454			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3455				cam_release_devq(done_ccb->ccb_h.path,
3456						 /*relsim_flags*/0,
3457						 /*reduction*/0,
3458						 /*timeout*/0,
3459						 /*getcount_only*/0);
3460		}
3461		xpt_release_ccb(done_ccb);
3462		cam_periph_release_locked(periph);
3463		return;
3464	}
3465	default:
3466		break;
3467	}
3468	xpt_release_ccb(done_ccb);
3469}
3470
3471static void
3472dareprobe(struct cam_periph *periph)
3473{
3474	struct da_softc	  *softc;
3475	cam_status status;
3476
3477	softc = (struct da_softc *)periph->softc;
3478
3479	/* Probe in progress; don't interfere. */
3480	if (softc->state != DA_STATE_NORMAL)
3481		return;
3482
3483	status = cam_periph_acquire(periph);
3484	KASSERT(status == CAM_REQ_CMP,
3485	    ("dareprobe: cam_periph_acquire failed"));
3486
3487	if (softc->flags & DA_FLAG_CAN_RC16)
3488		softc->state = DA_STATE_PROBE_RC16;
3489	else
3490		softc->state = DA_STATE_PROBE_RC;
3491
3492	xpt_schedule(periph, CAM_PRIORITY_DEV);
3493}
3494
3495static int
3496daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3497{
3498	struct da_softc	  *softc;
3499	struct cam_periph *periph;
3500	int error, error_code, sense_key, asc, ascq;
3501
3502	periph = xpt_path_periph(ccb->ccb_h.path);
3503	softc = (struct da_softc *)periph->softc;
3504
3505 	/*
3506	 * Automatically detect devices that do not support
3507 	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3508 	 */
3509	error = 0;
3510	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3511		error = cmd6workaround(ccb);
3512	} else if (scsi_extract_sense_ccb(ccb,
3513	    &error_code, &sense_key, &asc, &ascq)) {
3514		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3515 			error = cmd6workaround(ccb);
3516		/*
3517		 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3518		 * query the capacity and notify upper layers.
3519		 */
3520		else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3521		    asc == 0x2A && ascq == 0x09) {
3522			xpt_print(periph->path, "capacity data has changed\n");
3523			dareprobe(periph);
3524			sense_flags |= SF_NO_PRINT;
3525		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3526		    asc == 0x28 && ascq == 0x00)
3527			disk_media_changed(softc->disk, M_NOWAIT);
3528		else if (sense_key == SSD_KEY_NOT_READY &&
3529		    asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3530			softc->flags |= DA_FLAG_PACK_INVALID;
3531			disk_media_gone(softc->disk, M_NOWAIT);
3532		}
3533	}
3534	if (error == ERESTART)
3535		return (ERESTART);
3536
3537	/*
3538	 * XXX
3539	 * Until we have a better way of doing pack validation,
3540	 * don't treat UAs as errors.
3541	 */
3542	sense_flags |= SF_RETRY_UA;
3543	return(cam_periph_error(ccb, cam_flags, sense_flags,
3544				&softc->saved_ccb));
3545}
3546
3547static void
3548damediapoll(void *arg)
3549{
3550	struct cam_periph *periph = arg;
3551	struct da_softc *softc = periph->softc;
3552
3553	if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3554		if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3555			softc->tur = 1;
3556			daschedule(periph);
3557		}
3558	}
3559	/* Queue us up again */
3560	if (da_poll_period != 0)
3561		callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3562}
3563
3564static void
3565daprevent(struct cam_periph *periph, int action)
3566{
3567	struct	da_softc *softc;
3568	union	ccb *ccb;
3569	int	error;
3570
3571	softc = (struct da_softc *)periph->softc;
3572
3573	if (((action == PR_ALLOW)
3574	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3575	 || ((action == PR_PREVENT)
3576	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3577		return;
3578	}
3579
3580	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3581
3582	scsi_prevent(&ccb->csio,
3583		     /*retries*/1,
3584		     /*cbcfp*/dadone,
3585		     MSG_SIMPLE_Q_TAG,
3586		     action,
3587		     SSD_FULL_SIZE,
3588		     5000);
3589
3590	error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3591	    SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3592
3593	if (error == 0) {
3594		if (action == PR_ALLOW)
3595			softc->flags &= ~DA_FLAG_PACK_LOCKED;
3596		else
3597			softc->flags |= DA_FLAG_PACK_LOCKED;
3598	}
3599
3600	xpt_release_ccb(ccb);
3601}
3602
3603static void
3604dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3605	  struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3606{
3607	struct ccb_calc_geometry ccg;
3608	struct da_softc *softc;
3609	struct disk_params *dp;
3610	u_int lbppbe, lalba;
3611	int error;
3612
3613	softc = (struct da_softc *)periph->softc;
3614
3615	dp = &softc->params;
3616	dp->secsize = block_len;
3617	dp->sectors = maxsector + 1;
3618	if (rcaplong != NULL) {
3619		lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3620		lalba = scsi_2btoul(rcaplong->lalba_lbp);
3621		lalba &= SRC16_LALBA_A;
3622	} else {
3623		lbppbe = 0;
3624		lalba = 0;
3625	}
3626
3627	if (lbppbe > 0) {
3628		dp->stripesize = block_len << lbppbe;
3629		dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3630		    dp->stripesize;
3631	} else if (softc->quirks & DA_Q_4K) {
3632		dp->stripesize = 4096;
3633		dp->stripeoffset = 0;
3634	} else {
3635		dp->stripesize = 0;
3636		dp->stripeoffset = 0;
3637	}
3638	/*
3639	 * Have the controller provide us with a geometry
3640	 * for this disk.  The only time the geometry
3641	 * matters is when we boot and the controller
3642	 * is the only one knowledgeable enough to come
3643	 * up with something that will make this a bootable
3644	 * device.
3645	 */
3646	xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3647	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3648	ccg.block_size = dp->secsize;
3649	ccg.volume_size = dp->sectors;
3650	ccg.heads = 0;
3651	ccg.secs_per_track = 0;
3652	ccg.cylinders = 0;
3653	xpt_action((union ccb*)&ccg);
3654	if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3655		/*
3656		 * We don't know what went wrong here- but just pick
3657		 * a geometry so we don't have nasty things like divide
3658		 * by zero.
3659		 */
3660		dp->heads = 255;
3661		dp->secs_per_track = 255;
3662		dp->cylinders = dp->sectors / (255 * 255);
3663		if (dp->cylinders == 0) {
3664			dp->cylinders = 1;
3665		}
3666	} else {
3667		dp->heads = ccg.heads;
3668		dp->secs_per_track = ccg.secs_per_track;
3669		dp->cylinders = ccg.cylinders;
3670	}
3671
3672	/*
3673	 * If the user supplied a read capacity buffer, and if it is
3674	 * different than the previous buffer, update the data in the EDT.
3675	 * If it's the same, we don't bother.  This avoids sending an
3676	 * update every time someone opens this device.
3677	 */
3678	if ((rcaplong != NULL)
3679	 && (bcmp(rcaplong, &softc->rcaplong,
3680		  min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3681		struct ccb_dev_advinfo cdai;
3682
3683		xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3684		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3685		cdai.buftype = CDAI_TYPE_RCAPLONG;
3686		cdai.flags |= CDAI_FLAG_STORE;
3687		cdai.bufsiz = rcap_len;
3688		cdai.buf = (uint8_t *)rcaplong;
3689		xpt_action((union ccb *)&cdai);
3690		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3691			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3692		if (cdai.ccb_h.status != CAM_REQ_CMP) {
3693			xpt_print(periph->path, "%s: failed to set read "
3694				  "capacity advinfo\n", __func__);
3695			/* Use cam_error_print() to decode the status */
3696			cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3697					CAM_EPF_ALL);
3698		} else {
3699			bcopy(rcaplong, &softc->rcaplong,
3700			      min(sizeof(softc->rcaplong), rcap_len));
3701		}
3702	}
3703
3704	softc->disk->d_sectorsize = softc->params.secsize;
3705	softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3706	softc->disk->d_stripesize = softc->params.stripesize;
3707	softc->disk->d_stripeoffset = softc->params.stripeoffset;
3708	/* XXX: these are not actually "firmware" values, so they may be wrong */
3709	softc->disk->d_fwsectors = softc->params.secs_per_track;
3710	softc->disk->d_fwheads = softc->params.heads;
3711	softc->disk->d_devstat->block_size = softc->params.secsize;
3712	softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3713
3714	error = disk_resize(softc->disk, M_NOWAIT);
3715	if (error != 0)
3716		xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3717}
3718
3719static void
3720dasendorderedtag(void *arg)
3721{
3722	struct da_softc *softc = arg;
3723
3724	if (da_send_ordered) {
3725		if (!LIST_EMPTY(&softc->pending_ccbs)) {
3726			if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3727				softc->flags |= DA_FLAG_NEED_OTAG;
3728			softc->flags &= ~DA_FLAG_WAS_OTAG;
3729		}
3730	}
3731	/* Queue us up again */
3732	callout_reset(&softc->sendordered_c,
3733	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3734	    dasendorderedtag, softc);
3735}
3736
3737/*
3738 * Step through all DA peripheral drivers, and if the device is still open,
3739 * sync the disk cache to physical media.
3740 */
3741static void
3742dashutdown(void * arg, int howto)
3743{
3744	struct cam_periph *periph;
3745	struct da_softc *softc;
3746	union ccb *ccb;
3747	int error;
3748
3749	CAM_PERIPH_FOREACH(periph, &dadriver) {
3750		softc = (struct da_softc *)periph->softc;
3751		if (SCHEDULER_STOPPED()) {
3752			/* If we paniced with the lock held, do not recurse. */
3753			if (!cam_periph_owned(periph) &&
3754			    (softc->flags & DA_FLAG_OPEN)) {
3755				dadump(softc->disk, NULL, 0, 0, 0);
3756			}
3757			continue;
3758		}
3759		cam_periph_lock(periph);
3760
3761		/*
3762		 * We only sync the cache if the drive is still open, and
3763		 * if the drive is capable of it..
3764		 */
3765		if (((softc->flags & DA_FLAG_OPEN) == 0)
3766		 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3767			cam_periph_unlock(periph);
3768			continue;
3769		}
3770
3771		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3772		scsi_synchronize_cache(&ccb->csio,
3773				       /*retries*/0,
3774				       /*cbfcnp*/dadone,
3775				       MSG_SIMPLE_Q_TAG,
3776				       /*begin_lba*/0, /* whole disk */
3777				       /*lb_count*/0,
3778				       SSD_FULL_SIZE,
3779				       60 * 60 * 1000);
3780
3781		error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3782		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3783		    softc->disk->d_devstat);
3784		if (error != 0)
3785			xpt_print(periph->path, "Synchronize cache failed\n");
3786		xpt_release_ccb(ccb);
3787		cam_periph_unlock(periph);
3788	}
3789}
3790
3791#else /* !_KERNEL */
3792
3793/*
3794 * XXX This is only left out of the kernel build to silence warnings.  If,
3795 * for some reason this function is used in the kernel, the ifdefs should
3796 * be moved so it is included both in the kernel and userland.
3797 */
3798void
3799scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3800		 void (*cbfcnp)(struct cam_periph *, union ccb *),
3801		 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3802		 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3803		 u_int32_t timeout)
3804{
3805	struct scsi_format_unit *scsi_cmd;
3806
3807	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3808	scsi_cmd->opcode = FORMAT_UNIT;
3809	scsi_cmd->byte2 = byte2;
3810	scsi_ulto2b(ileave, scsi_cmd->interleave);
3811
3812	cam_fill_csio(csio,
3813		      retries,
3814		      cbfcnp,
3815		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3816		      tag_action,
3817		      data_ptr,
3818		      dxfer_len,
3819		      sense_len,
3820		      sizeof(*scsi_cmd),
3821		      timeout);
3822}
3823
3824void
3825scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
3826	      void (*cbfcnp)(struct cam_periph *, union ccb *),
3827	      u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
3828	      u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3829	      u_int32_t timeout)
3830{
3831	struct scsi_sanitize *scsi_cmd;
3832
3833	scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
3834	scsi_cmd->opcode = SANITIZE;
3835	scsi_cmd->byte2 = byte2;
3836	scsi_cmd->control = control;
3837	scsi_ulto2b(dxfer_len, scsi_cmd->length);
3838
3839	cam_fill_csio(csio,
3840		      retries,
3841		      cbfcnp,
3842		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3843		      tag_action,
3844		      data_ptr,
3845		      dxfer_len,
3846		      sense_len,
3847		      sizeof(*scsi_cmd),
3848		      timeout);
3849}
3850
3851#endif /* _KERNEL */
3852