scsi_da.c revision 260475
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 260475 2014-01-09 10:49:14Z 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, 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	for (i = 0; i <= DA_DELETE_MAX; i++) {
1976		if (!(softc->delete_available & (1 << i)) ||
1977		    strcmp(buf, da_delete_method_names[i]) != 0)
1978			continue;
1979		dadeletemethodset(softc, i);
1980		return (0);
1981	}
1982	return (EINVAL);
1983}
1984
1985static cam_status
1986daregister(struct cam_periph *periph, void *arg)
1987{
1988	struct da_softc *softc;
1989	struct ccb_pathinq cpi;
1990	struct ccb_getdev *cgd;
1991	char tmpstr[80];
1992	caddr_t match;
1993
1994	cgd = (struct ccb_getdev *)arg;
1995	if (cgd == NULL) {
1996		printf("daregister: no getdev CCB, can't register device\n");
1997		return(CAM_REQ_CMP_ERR);
1998	}
1999
2000	softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2001	    M_NOWAIT|M_ZERO);
2002
2003	if (softc == NULL) {
2004		printf("daregister: Unable to probe new device. "
2005		       "Unable to allocate softc\n");
2006		return(CAM_REQ_CMP_ERR);
2007	}
2008
2009	LIST_INIT(&softc->pending_ccbs);
2010	softc->state = DA_STATE_PROBE_RC;
2011	bioq_init(&softc->bio_queue);
2012	bioq_init(&softc->delete_queue);
2013	bioq_init(&softc->delete_run_queue);
2014	if (SID_IS_REMOVABLE(&cgd->inq_data))
2015		softc->flags |= DA_FLAG_PACK_REMOVABLE;
2016	softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2017	softc->unmap_max_lba = UNMAP_RANGE_MAX;
2018	softc->ws_max_blks = WS16_MAX_BLKS;
2019	softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2020	softc->sort_io_queue = -1;
2021
2022	periph->softc = softc;
2023
2024	/*
2025	 * See if this device has any quirks.
2026	 */
2027	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2028			       (caddr_t)da_quirk_table,
2029			       sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2030			       sizeof(*da_quirk_table), scsi_inquiry_match);
2031
2032	if (match != NULL)
2033		softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2034	else
2035		softc->quirks = DA_Q_NONE;
2036
2037	/* Check if the SIM does not want 6 byte commands */
2038	bzero(&cpi, sizeof(cpi));
2039	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2040	cpi.ccb_h.func_code = XPT_PATH_INQ;
2041	xpt_action((union ccb *)&cpi);
2042	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2043		softc->quirks |= DA_Q_NO_6_BYTE;
2044
2045	TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2046
2047	/*
2048	 * Take an exclusive refcount on the periph while dastart is called
2049	 * to finish the probe.  The reference will be dropped in dadone at
2050	 * the end of probe.
2051	 */
2052	(void)cam_periph_hold(periph, PRIBIO);
2053
2054	/*
2055	 * Schedule a periodic event to occasionally send an
2056	 * ordered tag to a device.
2057	 */
2058	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2059	callout_reset(&softc->sendordered_c,
2060	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2061	    dasendorderedtag, softc);
2062
2063	cam_periph_unlock(periph);
2064	/*
2065	 * RBC devices don't have to support READ(6), only READ(10).
2066	 */
2067	if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2068		softc->minimum_cmd_size = 10;
2069	else
2070		softc->minimum_cmd_size = 6;
2071
2072	/*
2073	 * Load the user's default, if any.
2074	 */
2075	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2076		 periph->unit_number);
2077	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2078
2079	/*
2080	 * 6, 10, 12 and 16 are the currently permissible values.
2081	 */
2082	if (softc->minimum_cmd_size < 6)
2083		softc->minimum_cmd_size = 6;
2084	else if ((softc->minimum_cmd_size > 6)
2085	      && (softc->minimum_cmd_size <= 10))
2086		softc->minimum_cmd_size = 10;
2087	else if ((softc->minimum_cmd_size > 10)
2088	      && (softc->minimum_cmd_size <= 12))
2089		softc->minimum_cmd_size = 12;
2090	else if (softc->minimum_cmd_size > 12)
2091		softc->minimum_cmd_size = 16;
2092
2093	/* Predict whether device may support READ CAPACITY(16). */
2094	if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2095	    (softc->quirks & DA_Q_NO_RC16) == 0) {
2096		softc->flags |= DA_FLAG_CAN_RC16;
2097		softc->state = DA_STATE_PROBE_RC16;
2098	}
2099
2100	/*
2101	 * Register this media as a disk.
2102	 */
2103	softc->disk = disk_alloc();
2104	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2105			  periph->unit_number, 0,
2106			  DEVSTAT_BS_UNAVAILABLE,
2107			  SID_TYPE(&cgd->inq_data) |
2108			  XPORT_DEVSTAT_TYPE(cpi.transport),
2109			  DEVSTAT_PRIORITY_DISK);
2110	softc->disk->d_open = daopen;
2111	softc->disk->d_close = daclose;
2112	softc->disk->d_strategy = dastrategy;
2113	softc->disk->d_dump = dadump;
2114	softc->disk->d_getattr = dagetattr;
2115	softc->disk->d_gone = dadiskgonecb;
2116	softc->disk->d_name = "da";
2117	softc->disk->d_drv1 = periph;
2118	if (cpi.maxio == 0)
2119		softc->disk->d_maxsize = DFLTPHYS;	/* traditional default */
2120	else if (cpi.maxio > MAXPHYS)
2121		softc->disk->d_maxsize = MAXPHYS;	/* for safety */
2122	else
2123		softc->disk->d_maxsize = cpi.maxio;
2124	softc->disk->d_unit = periph->unit_number;
2125	softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2126	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2127		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2128	if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2129		softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2130	cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2131	    sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2132	strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2133	cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2134	    cgd->inq_data.product, sizeof(cgd->inq_data.product),
2135	    sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2136	softc->disk->d_hba_vendor = cpi.hba_vendor;
2137	softc->disk->d_hba_device = cpi.hba_device;
2138	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2139	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2140
2141	/*
2142	 * Acquire a reference to the periph before we register with GEOM.
2143	 * We'll release this reference once GEOM calls us back (via
2144	 * dadiskgonecb()) telling us that our provider has been freed.
2145	 */
2146	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2147		xpt_print(periph->path, "%s: lost periph during "
2148			  "registration!\n", __func__);
2149		cam_periph_lock(periph);
2150		return (CAM_REQ_CMP_ERR);
2151	}
2152
2153	disk_create(softc->disk, DISK_VERSION);
2154	cam_periph_lock(periph);
2155
2156	/*
2157	 * Add async callbacks for events of interest.
2158	 * I don't bother checking if this fails as,
2159	 * in most cases, the system will function just
2160	 * fine without them and the only alternative
2161	 * would be to not attach the device on failure.
2162	 */
2163	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2164	    AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2165	    daasync, periph, periph->path);
2166
2167	/*
2168	 * Emit an attribute changed notification just in case
2169	 * physical path information arrived before our async
2170	 * event handler was registered, but after anyone attaching
2171	 * to our disk device polled it.
2172	 */
2173	disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2174
2175	/*
2176	 * Schedule a periodic media polling events.
2177	 */
2178	callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2179	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2180	    (cgd->inq_flags & SID_AEN) == 0 &&
2181	    da_poll_period != 0)
2182		callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2183		    damediapoll, periph);
2184
2185	xpt_schedule(periph, CAM_PRIORITY_DEV);
2186
2187	return(CAM_REQ_CMP);
2188}
2189
2190static void
2191dastart(struct cam_periph *periph, union ccb *start_ccb)
2192{
2193	struct da_softc *softc;
2194
2195	softc = (struct da_softc *)periph->softc;
2196
2197	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2198
2199skipstate:
2200	switch (softc->state) {
2201	case DA_STATE_NORMAL:
2202	{
2203		struct bio *bp;
2204		uint8_t tag_code;
2205
2206		/* Run BIO_DELETE if not running yet. */
2207		if (!softc->delete_running &&
2208		    (bp = bioq_first(&softc->delete_queue)) != NULL) {
2209			if (softc->delete_func != NULL) {
2210				softc->delete_func(periph, start_ccb, bp);
2211				goto out;
2212			} else {
2213				bioq_flush(&softc->delete_queue, NULL, 0);
2214				/* FALLTHROUGH */
2215			}
2216		}
2217
2218		/* Run regular command. */
2219		bp = bioq_takefirst(&softc->bio_queue);
2220		if (bp == NULL) {
2221			if (softc->tur) {
2222				softc->tur = 0;
2223				scsi_test_unit_ready(&start_ccb->csio,
2224				     /*retries*/ da_retry_count,
2225				     dadone,
2226				     MSG_SIMPLE_Q_TAG,
2227				     SSD_FULL_SIZE,
2228				     da_default_timeout * 1000);
2229				start_ccb->ccb_h.ccb_bp = NULL;
2230				start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2231				xpt_action(start_ccb);
2232			} else
2233				xpt_release_ccb(start_ccb);
2234			break;
2235		}
2236		if (softc->tur) {
2237			softc->tur = 0;
2238			cam_periph_release_locked(periph);
2239		}
2240
2241		if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2242		    (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2243			softc->flags &= ~DA_FLAG_NEED_OTAG;
2244			softc->flags |= DA_FLAG_WAS_OTAG;
2245			tag_code = MSG_ORDERED_Q_TAG;
2246		} else {
2247			tag_code = MSG_SIMPLE_Q_TAG;
2248		}
2249
2250		switch (bp->bio_cmd) {
2251		case BIO_WRITE:
2252			softc->flags |= DA_FLAG_DIRTY;
2253			/* FALLTHROUGH */
2254		case BIO_READ:
2255			scsi_read_write(&start_ccb->csio,
2256					/*retries*/da_retry_count,
2257					/*cbfcnp*/dadone,
2258					/*tag_action*/tag_code,
2259					/*read_op*/(bp->bio_cmd == BIO_READ ?
2260					SCSI_RW_READ : SCSI_RW_WRITE) |
2261					((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2262					SCSI_RW_BIO : 0),
2263					/*byte2*/0,
2264					softc->minimum_cmd_size,
2265					/*lba*/bp->bio_pblkno,
2266					/*block_count*/bp->bio_bcount /
2267					softc->params.secsize,
2268					/*data_ptr*/ (bp->bio_flags &
2269					BIO_UNMAPPED) != 0 ? (void *)bp :
2270					bp->bio_data,
2271					/*dxfer_len*/ bp->bio_bcount,
2272					/*sense_len*/SSD_FULL_SIZE,
2273					da_default_timeout * 1000);
2274			break;
2275		case BIO_FLUSH:
2276			/*
2277			 * BIO_FLUSH doesn't currently communicate
2278			 * range data, so we synchronize the cache
2279			 * over the whole disk.  We also force
2280			 * ordered tag semantics the flush applies
2281			 * to all previously queued I/O.
2282			 */
2283			scsi_synchronize_cache(&start_ccb->csio,
2284					       /*retries*/1,
2285					       /*cbfcnp*/dadone,
2286					       MSG_ORDERED_Q_TAG,
2287					       /*begin_lba*/0,
2288					       /*lb_count*/0,
2289					       SSD_FULL_SIZE,
2290					       da_default_timeout*1000);
2291			break;
2292		}
2293		start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2294		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2295
2296out:
2297		LIST_INSERT_HEAD(&softc->pending_ccbs,
2298				 &start_ccb->ccb_h, periph_links.le);
2299
2300		/* We expect a unit attention from this device */
2301		if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2302			start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2303			softc->flags &= ~DA_FLAG_RETRY_UA;
2304		}
2305
2306		start_ccb->ccb_h.ccb_bp = bp;
2307		softc->refcount++;
2308		cam_periph_unlock(periph);
2309		xpt_action(start_ccb);
2310		cam_periph_lock(periph);
2311		softc->refcount--;
2312
2313		/* May have more work to do, so ensure we stay scheduled */
2314		daschedule(periph);
2315		break;
2316	}
2317	case DA_STATE_PROBE_RC:
2318	{
2319		struct scsi_read_capacity_data *rcap;
2320
2321		rcap = (struct scsi_read_capacity_data *)
2322		    malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2323		if (rcap == NULL) {
2324			printf("dastart: Couldn't malloc read_capacity data\n");
2325			/* da_free_periph??? */
2326			break;
2327		}
2328		scsi_read_capacity(&start_ccb->csio,
2329				   /*retries*/da_retry_count,
2330				   dadone,
2331				   MSG_SIMPLE_Q_TAG,
2332				   rcap,
2333				   SSD_FULL_SIZE,
2334				   /*timeout*/5000);
2335		start_ccb->ccb_h.ccb_bp = NULL;
2336		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2337		xpt_action(start_ccb);
2338		break;
2339	}
2340	case DA_STATE_PROBE_RC16:
2341	{
2342		struct scsi_read_capacity_data_long *rcaplong;
2343
2344		rcaplong = (struct scsi_read_capacity_data_long *)
2345			malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2346		if (rcaplong == NULL) {
2347			printf("dastart: Couldn't malloc read_capacity data\n");
2348			/* da_free_periph??? */
2349			break;
2350		}
2351		scsi_read_capacity_16(&start_ccb->csio,
2352				      /*retries*/ da_retry_count,
2353				      /*cbfcnp*/ dadone,
2354				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
2355				      /*lba*/ 0,
2356				      /*reladr*/ 0,
2357				      /*pmi*/ 0,
2358				      /*rcap_buf*/ (uint8_t *)rcaplong,
2359				      /*rcap_buf_len*/ sizeof(*rcaplong),
2360				      /*sense_len*/ SSD_FULL_SIZE,
2361				      /*timeout*/ da_default_timeout * 1000);
2362		start_ccb->ccb_h.ccb_bp = NULL;
2363		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2364		xpt_action(start_ccb);
2365		break;
2366	}
2367	case DA_STATE_PROBE_LBP:
2368	{
2369		struct scsi_vpd_logical_block_prov *lbp;
2370
2371		if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2372			/*
2373			 * If we get here we don't support any SBC-3 delete
2374			 * methods with UNMAP as the Logical Block Provisioning
2375			 * VPD page support is required for devices which
2376			 * support it according to T10/1799-D Revision 31
2377			 * however older revisions of the spec don't mandate
2378			 * this so we currently don't remove these methods
2379			 * from the available set.
2380			 */
2381			softc->state = DA_STATE_PROBE_BLK_LIMITS;
2382			goto skipstate;
2383		}
2384
2385		lbp = (struct scsi_vpd_logical_block_prov *)
2386			malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2387
2388		if (lbp == NULL) {
2389			printf("dastart: Couldn't malloc lbp data\n");
2390			/* da_free_periph??? */
2391			break;
2392		}
2393
2394		scsi_inquiry(&start_ccb->csio,
2395			     /*retries*/da_retry_count,
2396			     /*cbfcnp*/dadone,
2397			     /*tag_action*/MSG_SIMPLE_Q_TAG,
2398			     /*inq_buf*/(u_int8_t *)lbp,
2399			     /*inq_len*/sizeof(*lbp),
2400			     /*evpd*/TRUE,
2401			     /*page_code*/SVPD_LBP,
2402			     /*sense_len*/SSD_MIN_SIZE,
2403			     /*timeout*/da_default_timeout * 1000);
2404		start_ccb->ccb_h.ccb_bp = NULL;
2405		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2406		xpt_action(start_ccb);
2407		break;
2408	}
2409	case DA_STATE_PROBE_BLK_LIMITS:
2410	{
2411		struct scsi_vpd_block_limits *block_limits;
2412
2413		if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2414			/* Not supported skip to next probe */
2415			softc->state = DA_STATE_PROBE_BDC;
2416			goto skipstate;
2417		}
2418
2419		block_limits = (struct scsi_vpd_block_limits *)
2420			malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2421
2422		if (block_limits == NULL) {
2423			printf("dastart: Couldn't malloc block_limits data\n");
2424			/* da_free_periph??? */
2425			break;
2426		}
2427
2428		scsi_inquiry(&start_ccb->csio,
2429			     /*retries*/da_retry_count,
2430			     /*cbfcnp*/dadone,
2431			     /*tag_action*/MSG_SIMPLE_Q_TAG,
2432			     /*inq_buf*/(u_int8_t *)block_limits,
2433			     /*inq_len*/sizeof(*block_limits),
2434			     /*evpd*/TRUE,
2435			     /*page_code*/SVPD_BLOCK_LIMITS,
2436			     /*sense_len*/SSD_MIN_SIZE,
2437			     /*timeout*/da_default_timeout * 1000);
2438		start_ccb->ccb_h.ccb_bp = NULL;
2439		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2440		xpt_action(start_ccb);
2441		break;
2442	}
2443	case DA_STATE_PROBE_BDC:
2444	{
2445		struct scsi_vpd_block_characteristics *bdc;
2446
2447		if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2448			softc->state = DA_STATE_PROBE_ATA;
2449			goto skipstate;
2450		}
2451
2452		bdc = (struct scsi_vpd_block_characteristics *)
2453			malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2454
2455		if (bdc == NULL) {
2456			printf("dastart: Couldn't malloc bdc data\n");
2457			/* da_free_periph??? */
2458			break;
2459		}
2460
2461		scsi_inquiry(&start_ccb->csio,
2462			     /*retries*/da_retry_count,
2463			     /*cbfcnp*/dadone,
2464			     /*tag_action*/MSG_SIMPLE_Q_TAG,
2465			     /*inq_buf*/(u_int8_t *)bdc,
2466			     /*inq_len*/sizeof(*bdc),
2467			     /*evpd*/TRUE,
2468			     /*page_code*/SVPD_BDC,
2469			     /*sense_len*/SSD_MIN_SIZE,
2470			     /*timeout*/da_default_timeout * 1000);
2471		start_ccb->ccb_h.ccb_bp = NULL;
2472		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2473		xpt_action(start_ccb);
2474		break;
2475	}
2476	case DA_STATE_PROBE_ATA:
2477	{
2478		struct ata_params *ata_params;
2479
2480		if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2481			daprobedone(periph, start_ccb);
2482			break;
2483		}
2484
2485		ata_params = (struct ata_params*)
2486			malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2487
2488		if (ata_params == NULL) {
2489			printf("dastart: Couldn't malloc ata_params data\n");
2490			/* da_free_periph??? */
2491			break;
2492		}
2493
2494		scsi_ata_identify(&start_ccb->csio,
2495				  /*retries*/da_retry_count,
2496				  /*cbfcnp*/dadone,
2497                                  /*tag_action*/MSG_SIMPLE_Q_TAG,
2498				  /*data_ptr*/(u_int8_t *)ata_params,
2499				  /*dxfer_len*/sizeof(*ata_params),
2500				  /*sense_len*/SSD_FULL_SIZE,
2501				  /*timeout*/da_default_timeout * 1000);
2502		start_ccb->ccb_h.ccb_bp = NULL;
2503		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2504		xpt_action(start_ccb);
2505		break;
2506	}
2507	}
2508}
2509
2510/*
2511 * In each of the methods below, while its the caller's
2512 * responsibility to ensure the request will fit into a
2513 * single device request, we might have changed the delete
2514 * method due to the device incorrectly advertising either
2515 * its supported methods or limits.
2516 *
2517 * To prevent this causing further issues we validate the
2518 * against the methods limits, and warn which would
2519 * otherwise be unnecessary.
2520 */
2521static void
2522da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2523{
2524	struct da_softc *softc = (struct da_softc *)periph->softc;;
2525	struct bio *bp1;
2526	uint8_t *buf = softc->unmap_buf;
2527	uint64_t lba, lastlba = (uint64_t)-1;
2528	uint64_t totalcount = 0;
2529	uint64_t count;
2530	uint32_t lastcount = 0, c;
2531	uint32_t off, ranges = 0;
2532
2533	/*
2534	 * Currently this doesn't take the UNMAP
2535	 * Granularity and Granularity Alignment
2536	 * fields into account.
2537	 *
2538	 * This could result in both unoptimal unmap
2539	 * requests as as well as UNMAP calls unmapping
2540	 * fewer LBA's than requested.
2541	 */
2542
2543	softc->delete_running = 1;
2544	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2545	bp1 = bp;
2546	do {
2547		bioq_remove(&softc->delete_queue, bp1);
2548		if (bp1 != bp)
2549			bioq_insert_tail(&softc->delete_run_queue, bp1);
2550		lba = bp1->bio_pblkno;
2551		count = bp1->bio_bcount / softc->params.secsize;
2552
2553		/* Try to extend the previous range. */
2554		if (lba == lastlba) {
2555			c = min(count, softc->unmap_max_lba - lastcount);
2556			lastcount += c;
2557			off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2558			      UNMAP_HEAD_SIZE;
2559			scsi_ulto4b(lastcount, &buf[off + 8]);
2560			count -= c;
2561			lba +=c;
2562			totalcount += c;
2563		}
2564
2565		while (count > 0) {
2566			c = min(count, softc->unmap_max_lba);
2567			if (totalcount + c > softc->unmap_max_lba ||
2568			    ranges >= softc->unmap_max_ranges) {
2569				xpt_print(periph->path,
2570				    "%s issuing short delete %ld > %ld"
2571				    "|| %d >= %d",
2572				    da_delete_method_desc[softc->delete_method],
2573				    totalcount + c, softc->unmap_max_lba,
2574				    ranges, softc->unmap_max_ranges);
2575				break;
2576			}
2577			off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2578			scsi_u64to8b(lba, &buf[off + 0]);
2579			scsi_ulto4b(c, &buf[off + 8]);
2580			lba += c;
2581			totalcount += c;
2582			ranges++;
2583			count -= c;
2584			lastcount = c;
2585		}
2586		lastlba = lba;
2587		bp1 = bioq_first(&softc->delete_queue);
2588		if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2589		    totalcount + bp1->bio_bcount /
2590		    softc->params.secsize > softc->unmap_max_lba)
2591			break;
2592	} while (1);
2593	scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2594	scsi_ulto2b(ranges * 16, &buf[2]);
2595
2596	scsi_unmap(&ccb->csio,
2597		   /*retries*/da_retry_count,
2598		   /*cbfcnp*/dadone,
2599		   /*tag_action*/MSG_SIMPLE_Q_TAG,
2600		   /*byte2*/0,
2601		   /*data_ptr*/ buf,
2602		   /*dxfer_len*/ ranges * 16 + 8,
2603		   /*sense_len*/SSD_FULL_SIZE,
2604		   da_default_timeout * 1000);
2605	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2606	ccb->ccb_h.flags |= CAM_UNLOCKED;
2607}
2608
2609static void
2610da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2611{
2612	struct da_softc *softc = (struct da_softc *)periph->softc;
2613	struct bio *bp1;
2614	uint8_t *buf = softc->unmap_buf;
2615	uint64_t lastlba = (uint64_t)-1;
2616	uint64_t count;
2617	uint64_t lba;
2618	uint32_t lastcount = 0, c, requestcount;
2619	int ranges = 0, off, block_count;
2620
2621	softc->delete_running = 1;
2622	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2623	bp1 = bp;
2624	do {
2625		bioq_remove(&softc->delete_queue, bp1);
2626		if (bp1 != bp)
2627			bioq_insert_tail(&softc->delete_run_queue, bp1);
2628		lba = bp1->bio_pblkno;
2629		count = bp1->bio_bcount / softc->params.secsize;
2630		requestcount = count;
2631
2632		/* Try to extend the previous range. */
2633		if (lba == lastlba) {
2634			c = min(count, ATA_DSM_RANGE_MAX - lastcount);
2635			lastcount += c;
2636			off = (ranges - 1) * 8;
2637			buf[off + 6] = lastcount & 0xff;
2638			buf[off + 7] = (lastcount >> 8) & 0xff;
2639			count -= c;
2640			lba += c;
2641		}
2642
2643		while (count > 0) {
2644			c = min(count, ATA_DSM_RANGE_MAX);
2645			off = ranges * 8;
2646
2647			buf[off + 0] = lba & 0xff;
2648			buf[off + 1] = (lba >> 8) & 0xff;
2649			buf[off + 2] = (lba >> 16) & 0xff;
2650			buf[off + 3] = (lba >> 24) & 0xff;
2651			buf[off + 4] = (lba >> 32) & 0xff;
2652			buf[off + 5] = (lba >> 40) & 0xff;
2653			buf[off + 6] = c & 0xff;
2654			buf[off + 7] = (c >> 8) & 0xff;
2655			lba += c;
2656			ranges++;
2657			count -= c;
2658			lastcount = c;
2659			if (count != 0 && ranges == softc->trim_max_ranges) {
2660				xpt_print(periph->path,
2661				    "%s issuing short delete %ld > %ld",
2662				    da_delete_method_desc[softc->delete_method],
2663				    requestcount,
2664				    (softc->trim_max_ranges - ranges) *
2665				    ATA_DSM_RANGE_MAX);
2666				break;
2667			}
2668		}
2669		lastlba = lba;
2670		bp1 = bioq_first(&softc->delete_queue);
2671		if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2672		    (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2673			break;
2674	} while (1);
2675
2676	block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2677	scsi_ata_trim(&ccb->csio,
2678		      /*retries*/da_retry_count,
2679		      /*cbfcnp*/dadone,
2680		      /*tag_action*/MSG_SIMPLE_Q_TAG,
2681		      block_count,
2682		      /*data_ptr*/buf,
2683		      /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2684		      /*sense_len*/SSD_FULL_SIZE,
2685		      da_default_timeout * 1000);
2686	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2687	ccb->ccb_h.flags |= CAM_UNLOCKED;
2688}
2689
2690/*
2691 * We calculate ws_max_blks here based off d_delmaxsize instead
2692 * of using softc->ws_max_blks as it is absolute max for the
2693 * device not the protocol max which may well be lower
2694 */
2695static void
2696da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2697{
2698	struct da_softc *softc;
2699	struct bio *bp1;
2700	uint64_t ws_max_blks;
2701	uint64_t lba;
2702	uint64_t count; /* forward compat with WS32 */
2703
2704	softc = (struct da_softc *)periph->softc;
2705	ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2706	softc->delete_running = 1;
2707	lba = bp->bio_pblkno;
2708	count = 0;
2709	bp1 = bp;
2710	do {
2711		bioq_remove(&softc->delete_queue, bp1);
2712		if (bp1 != bp)
2713			bioq_insert_tail(&softc->delete_run_queue, bp1);
2714		count += bp1->bio_bcount / softc->params.secsize;
2715		if (count > ws_max_blks) {
2716			count = min(count, ws_max_blks);
2717			xpt_print(periph->path,
2718			    "%s issuing short delete %ld > %ld",
2719			    da_delete_method_desc[softc->delete_method],
2720			    count, ws_max_blks);
2721			break;
2722		}
2723		bp1 = bioq_first(&softc->delete_queue);
2724		if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2725		    count + bp1->bio_bcount /
2726		    softc->params.secsize > ws_max_blks)
2727			break;
2728	} while (1);
2729
2730	scsi_write_same(&ccb->csio,
2731			/*retries*/da_retry_count,
2732			/*cbfcnp*/dadone,
2733			/*tag_action*/MSG_SIMPLE_Q_TAG,
2734			/*byte2*/softc->delete_method ==
2735			    DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2736			softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2737			/*lba*/lba,
2738			/*block_count*/count,
2739			/*data_ptr*/ __DECONST(void *, zero_region),
2740			/*dxfer_len*/ softc->params.secsize,
2741			/*sense_len*/SSD_FULL_SIZE,
2742			da_default_timeout * 1000);
2743	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2744	ccb->ccb_h.flags |= CAM_UNLOCKED;
2745}
2746
2747static int
2748cmd6workaround(union ccb *ccb)
2749{
2750	struct scsi_rw_6 cmd6;
2751	struct scsi_rw_10 *cmd10;
2752	struct da_softc *softc;
2753	u_int8_t *cdb;
2754	struct bio *bp;
2755	int frozen;
2756
2757	cdb = ccb->csio.cdb_io.cdb_bytes;
2758	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2759
2760	if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2761		da_delete_methods old_method = softc->delete_method;
2762
2763		/*
2764		 * Typically there are two reasons for failure here
2765		 * 1. Delete method was detected as supported but isn't
2766		 * 2. Delete failed due to invalid params e.g. too big
2767		 *
2768		 * While we will attempt to choose an alternative delete method
2769		 * this may result in short deletes if the existing delete
2770		 * requests from geom are big for the new method choosen.
2771		 *
2772		 * This method assumes that the error which triggered this
2773		 * will not retry the io otherwise a panic will occur
2774		 */
2775		dadeleteflag(softc, old_method, 0);
2776		dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2777		if (softc->delete_method == DA_DELETE_DISABLE)
2778			xpt_print(ccb->ccb_h.path,
2779				  "%s failed, disabling BIO_DELETE\n",
2780				  da_delete_method_desc[old_method]);
2781		else
2782			xpt_print(ccb->ccb_h.path,
2783				  "%s failed, switching to %s BIO_DELETE\n",
2784				  da_delete_method_desc[old_method],
2785				  da_delete_method_desc[softc->delete_method]);
2786
2787		if (DA_SIO) {
2788			while ((bp = bioq_takefirst(&softc->delete_run_queue))
2789			    != NULL)
2790				bioq_disksort(&softc->delete_queue, bp);
2791		} else {
2792			while ((bp = bioq_takefirst(&softc->delete_run_queue))
2793			    != NULL)
2794				bioq_insert_tail(&softc->delete_queue, bp);
2795		}
2796		bioq_insert_tail(&softc->delete_queue,
2797		    (struct bio *)ccb->ccb_h.ccb_bp);
2798		ccb->ccb_h.ccb_bp = NULL;
2799		return (0);
2800	}
2801
2802	/* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2803	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2804	    (*cdb == PREVENT_ALLOW) &&
2805	    (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2806		if (bootverbose)
2807			xpt_print(ccb->ccb_h.path,
2808			    "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2809		softc->quirks |= DA_Q_NO_PREVENT;
2810		return (0);
2811	}
2812
2813	/* Detect unsupported SYNCHRONIZE CACHE(10). */
2814	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2815	    (*cdb == SYNCHRONIZE_CACHE) &&
2816	    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2817		if (bootverbose)
2818			xpt_print(ccb->ccb_h.path,
2819			    "SYNCHRONIZE CACHE(10) not supported.\n");
2820		softc->quirks |= DA_Q_NO_SYNC_CACHE;
2821		softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2822		return (0);
2823	}
2824
2825	/* Translation only possible if CDB is an array and cmd is R/W6 */
2826	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2827	    (*cdb != READ_6 && *cdb != WRITE_6))
2828		return 0;
2829
2830	xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2831	    "increasing minimum_cmd_size to 10.\n");
2832 	softc->minimum_cmd_size = 10;
2833
2834	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2835	cmd10 = (struct scsi_rw_10 *)cdb;
2836	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2837	cmd10->byte2 = 0;
2838	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2839	cmd10->reserved = 0;
2840	scsi_ulto2b(cmd6.length, cmd10->length);
2841	cmd10->control = cmd6.control;
2842	ccb->csio.cdb_len = sizeof(*cmd10);
2843
2844	/* Requeue request, unfreezing queue if necessary */
2845	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2846 	ccb->ccb_h.status = CAM_REQUEUE_REQ;
2847	xpt_action(ccb);
2848	if (frozen) {
2849		cam_release_devq(ccb->ccb_h.path,
2850				 /*relsim_flags*/0,
2851				 /*reduction*/0,
2852				 /*timeout*/0,
2853				 /*getcount_only*/0);
2854	}
2855	return (ERESTART);
2856}
2857
2858static void
2859dadone(struct cam_periph *periph, union ccb *done_ccb)
2860{
2861	struct da_softc *softc;
2862	struct ccb_scsiio *csio;
2863	u_int32_t  priority;
2864	da_ccb_state state;
2865
2866	softc = (struct da_softc *)periph->softc;
2867	priority = done_ccb->ccb_h.pinfo.priority;
2868
2869	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2870
2871	csio = &done_ccb->csio;
2872	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2873	switch (state) {
2874	case DA_CCB_BUFFER_IO:
2875	case DA_CCB_DELETE:
2876	{
2877		struct bio *bp, *bp1;
2878
2879		cam_periph_lock(periph);
2880		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2881		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2882			int error;
2883			int sf;
2884
2885			if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2886				sf = SF_RETRY_UA;
2887			else
2888				sf = 0;
2889
2890			error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2891			if (error == ERESTART) {
2892				/*
2893				 * A retry was scheduled, so
2894				 * just return.
2895				 */
2896				cam_periph_unlock(periph);
2897				return;
2898			}
2899			bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2900			if (error != 0) {
2901				int queued_error;
2902
2903				/*
2904				 * return all queued I/O with EIO, so that
2905				 * the client can retry these I/Os in the
2906				 * proper order should it attempt to recover.
2907				 */
2908				queued_error = EIO;
2909
2910				if (error == ENXIO
2911				 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2912					/*
2913					 * Catastrophic error.  Mark our pack as
2914					 * invalid.
2915					 */
2916					/*
2917					 * XXX See if this is really a media
2918					 * XXX change first?
2919					 */
2920					xpt_print(periph->path,
2921					    "Invalidating pack\n");
2922					softc->flags |= DA_FLAG_PACK_INVALID;
2923					queued_error = ENXIO;
2924				}
2925				bioq_flush(&softc->bio_queue, NULL,
2926					   queued_error);
2927				if (bp != NULL) {
2928					bp->bio_error = error;
2929					bp->bio_resid = bp->bio_bcount;
2930					bp->bio_flags |= BIO_ERROR;
2931				}
2932			} else if (bp != NULL) {
2933				if (state == DA_CCB_DELETE)
2934					bp->bio_resid = 0;
2935				else
2936					bp->bio_resid = csio->resid;
2937				bp->bio_error = 0;
2938				if (bp->bio_resid != 0)
2939					bp->bio_flags |= BIO_ERROR;
2940			}
2941			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2942				cam_release_devq(done_ccb->ccb_h.path,
2943						 /*relsim_flags*/0,
2944						 /*reduction*/0,
2945						 /*timeout*/0,
2946						 /*getcount_only*/0);
2947		} else if (bp != NULL) {
2948			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2949				panic("REQ_CMP with QFRZN");
2950			if (state == DA_CCB_DELETE)
2951				bp->bio_resid = 0;
2952			else
2953				bp->bio_resid = csio->resid;
2954			if (csio->resid > 0)
2955				bp->bio_flags |= BIO_ERROR;
2956			if (softc->error_inject != 0) {
2957				bp->bio_error = softc->error_inject;
2958				bp->bio_resid = bp->bio_bcount;
2959				bp->bio_flags |= BIO_ERROR;
2960				softc->error_inject = 0;
2961			}
2962		}
2963
2964		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2965		if (LIST_EMPTY(&softc->pending_ccbs))
2966			softc->flags |= DA_FLAG_WAS_OTAG;
2967
2968		xpt_release_ccb(done_ccb);
2969		if (state == DA_CCB_DELETE) {
2970			TAILQ_HEAD(, bio) queue;
2971
2972			TAILQ_INIT(&queue);
2973			TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
2974			softc->delete_run_queue.insert_point = NULL;
2975			softc->delete_running = 0;
2976			daschedule(periph);
2977			cam_periph_unlock(periph);
2978			while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
2979				TAILQ_REMOVE(&queue, bp1, bio_queue);
2980				bp1->bio_error = bp->bio_error;
2981				if (bp->bio_flags & BIO_ERROR) {
2982					bp1->bio_flags |= BIO_ERROR;
2983					bp1->bio_resid = bp1->bio_bcount;
2984				} else
2985					bp1->bio_resid = 0;
2986				biodone(bp1);
2987			}
2988		} else
2989			cam_periph_unlock(periph);
2990		if (bp != NULL)
2991			biodone(bp);
2992		return;
2993	}
2994	case DA_CCB_PROBE_RC:
2995	case DA_CCB_PROBE_RC16:
2996	{
2997		struct	   scsi_read_capacity_data *rdcap;
2998		struct     scsi_read_capacity_data_long *rcaplong;
2999		char	   announce_buf[80];
3000		int	   lbp;
3001
3002		lbp = 0;
3003		rdcap = NULL;
3004		rcaplong = NULL;
3005		if (state == DA_CCB_PROBE_RC)
3006			rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3007		else
3008			rcaplong = (struct scsi_read_capacity_data_long *)
3009				csio->data_ptr;
3010
3011		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3012			struct disk_params *dp;
3013			uint32_t block_size;
3014			uint64_t maxsector;
3015			u_int lbppbe;	/* LB per physical block exponent. */
3016			u_int lalba;	/* Lowest aligned LBA. */
3017
3018			if (state == DA_CCB_PROBE_RC) {
3019				block_size = scsi_4btoul(rdcap->length);
3020				maxsector = scsi_4btoul(rdcap->addr);
3021				lbppbe = 0;
3022				lalba = 0;
3023
3024				/*
3025				 * According to SBC-2, if the standard 10
3026				 * byte READ CAPACITY command returns 2^32,
3027				 * we should issue the 16 byte version of
3028				 * the command, since the device in question
3029				 * has more sectors than can be represented
3030				 * with the short version of the command.
3031				 */
3032				if (maxsector == 0xffffffff) {
3033					free(rdcap, M_SCSIDA);
3034					xpt_release_ccb(done_ccb);
3035					softc->state = DA_STATE_PROBE_RC16;
3036					xpt_schedule(periph, priority);
3037					return;
3038				}
3039			} else {
3040				block_size = scsi_4btoul(rcaplong->length);
3041				maxsector = scsi_8btou64(rcaplong->addr);
3042				lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3043				lalba = scsi_2btoul(rcaplong->lalba_lbp);
3044			}
3045
3046			/*
3047			 * Because GEOM code just will panic us if we
3048			 * give them an 'illegal' value we'll avoid that
3049			 * here.
3050			 */
3051			if (block_size == 0 && maxsector == 0) {
3052				block_size = 512;
3053				maxsector = -1;
3054			}
3055			if (block_size >= MAXPHYS || block_size == 0) {
3056				xpt_print(periph->path,
3057				    "unsupportable block size %ju\n",
3058				    (uintmax_t) block_size);
3059				announce_buf[0] = '\0';
3060				cam_periph_invalidate(periph);
3061			} else {
3062				/*
3063				 * We pass rcaplong into dasetgeom(),
3064				 * because it will only use it if it is
3065				 * non-NULL.
3066				 */
3067				dasetgeom(periph, block_size, maxsector,
3068					  rcaplong, sizeof(*rcaplong));
3069				lbp = (lalba & SRC16_LBPME_A);
3070				dp = &softc->params;
3071				snprintf(announce_buf, sizeof(announce_buf),
3072				        "%juMB (%ju %u byte sectors: %dH %dS/T "
3073                                        "%dC)", (uintmax_t)
3074	                                (((uintmax_t)dp->secsize *
3075				        dp->sectors) / (1024*1024)),
3076			                (uintmax_t)dp->sectors,
3077				        dp->secsize, dp->heads,
3078                                        dp->secs_per_track, dp->cylinders);
3079			}
3080		} else {
3081			int	error;
3082
3083			announce_buf[0] = '\0';
3084
3085			/*
3086			 * Retry any UNIT ATTENTION type errors.  They
3087			 * are expected at boot.
3088			 */
3089			error = daerror(done_ccb, CAM_RETRY_SELTO,
3090					SF_RETRY_UA|SF_NO_PRINT);
3091			if (error == ERESTART) {
3092				/*
3093				 * A retry was scheuled, so
3094				 * just return.
3095				 */
3096				return;
3097			} else if (error != 0) {
3098				int asc, ascq;
3099				int sense_key, error_code;
3100				int have_sense;
3101				cam_status status;
3102				struct ccb_getdev cgd;
3103
3104				/* Don't wedge this device's queue */
3105				status = done_ccb->ccb_h.status;
3106				if ((status & CAM_DEV_QFRZN) != 0)
3107					cam_release_devq(done_ccb->ccb_h.path,
3108							 /*relsim_flags*/0,
3109							 /*reduction*/0,
3110							 /*timeout*/0,
3111							 /*getcount_only*/0);
3112
3113
3114				xpt_setup_ccb(&cgd.ccb_h,
3115					      done_ccb->ccb_h.path,
3116					      CAM_PRIORITY_NORMAL);
3117				cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3118				xpt_action((union ccb *)&cgd);
3119
3120				if (scsi_extract_sense_ccb(done_ccb,
3121				    &error_code, &sense_key, &asc, &ascq))
3122					have_sense = TRUE;
3123				else
3124					have_sense = FALSE;
3125
3126				/*
3127				 * If we tried READ CAPACITY(16) and failed,
3128				 * fallback to READ CAPACITY(10).
3129				 */
3130				if ((state == DA_CCB_PROBE_RC16) &&
3131				    (softc->flags & DA_FLAG_CAN_RC16) &&
3132				    (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3133					CAM_REQ_INVALID) ||
3134				     ((have_sense) &&
3135				      (error_code == SSD_CURRENT_ERROR) &&
3136				      (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3137					softc->flags &= ~DA_FLAG_CAN_RC16;
3138					free(rdcap, M_SCSIDA);
3139					xpt_release_ccb(done_ccb);
3140					softc->state = DA_STATE_PROBE_RC;
3141					xpt_schedule(periph, priority);
3142					return;
3143				} else
3144				/*
3145				 * Attach to anything that claims to be a
3146				 * direct access or optical disk device,
3147				 * as long as it doesn't return a "Logical
3148				 * unit not supported" (0x25) error.
3149				 */
3150				if ((have_sense) && (asc != 0x25)
3151				 && (error_code == SSD_CURRENT_ERROR)) {
3152					const char *sense_key_desc;
3153					const char *asc_desc;
3154
3155					dasetgeom(periph, 512, -1, NULL, 0);
3156					scsi_sense_desc(sense_key, asc, ascq,
3157							&cgd.inq_data,
3158							&sense_key_desc,
3159							&asc_desc);
3160					snprintf(announce_buf,
3161					    sizeof(announce_buf),
3162						"Attempt to query device "
3163						"size failed: %s, %s",
3164						sense_key_desc,
3165						asc_desc);
3166				} else {
3167					if (have_sense)
3168						scsi_sense_print(
3169							&done_ccb->csio);
3170					else {
3171						xpt_print(periph->path,
3172						    "got CAM status %#x\n",
3173						    done_ccb->ccb_h.status);
3174					}
3175
3176					xpt_print(periph->path, "fatal error, "
3177					    "failed to attach to device\n");
3178
3179					/*
3180					 * Free up resources.
3181					 */
3182					cam_periph_invalidate(periph);
3183				}
3184			}
3185		}
3186		free(csio->data_ptr, M_SCSIDA);
3187		if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
3188			/*
3189			 * Create our sysctl variables, now that we know
3190			 * we have successfully attached.
3191			 */
3192			/* increase the refcount */
3193			if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3194				taskqueue_enqueue(taskqueue_thread,
3195						  &softc->sysctl_task);
3196				xpt_announce_periph(periph, announce_buf);
3197				xpt_announce_quirks(periph, softc->quirks,
3198				    DA_Q_BIT_STRING);
3199			} else {
3200				xpt_print(periph->path, "fatal error, "
3201				    "could not acquire reference count\n");
3202			}
3203		}
3204
3205		/* Ensure re-probe doesn't see old delete. */
3206		softc->delete_available = 0;
3207		if (lbp) {
3208			/*
3209			 * Based on older SBC-3 spec revisions
3210			 * any of the UNMAP methods "may" be
3211			 * available via LBP given this flag so
3212			 * we flag all of them as availble and
3213			 * then remove those which further
3214			 * probes confirm aren't available
3215			 * later.
3216			 *
3217			 * We could also check readcap(16) p_type
3218			 * flag to exclude one or more invalid
3219			 * write same (X) types here
3220			 */
3221			dadeleteflag(softc, DA_DELETE_WS16, 1);
3222			dadeleteflag(softc, DA_DELETE_WS10, 1);
3223			dadeleteflag(softc, DA_DELETE_ZERO, 1);
3224			dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3225
3226			xpt_release_ccb(done_ccb);
3227			softc->state = DA_STATE_PROBE_LBP;
3228			xpt_schedule(periph, priority);
3229			return;
3230		}
3231
3232		xpt_release_ccb(done_ccb);
3233		softc->state = DA_STATE_PROBE_BDC;
3234		xpt_schedule(periph, priority);
3235		return;
3236	}
3237	case DA_CCB_PROBE_LBP:
3238	{
3239		struct scsi_vpd_logical_block_prov *lbp;
3240
3241		lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3242
3243		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3244			/*
3245			 * T10/1799-D Revision 31 states at least one of these
3246			 * must be supported but we don't currently enforce this.
3247			 */
3248			dadeleteflag(softc, DA_DELETE_WS16,
3249				     (lbp->flags & SVPD_LBP_WS16));
3250			dadeleteflag(softc, DA_DELETE_WS10,
3251				     (lbp->flags & SVPD_LBP_WS10));
3252			dadeleteflag(softc, DA_DELETE_ZERO,
3253				     (lbp->flags & SVPD_LBP_WS10));
3254			dadeleteflag(softc, DA_DELETE_UNMAP,
3255				     (lbp->flags & SVPD_LBP_UNMAP));
3256
3257			if (lbp->flags & SVPD_LBP_UNMAP) {
3258				free(lbp, M_SCSIDA);
3259				xpt_release_ccb(done_ccb);
3260				softc->state = DA_STATE_PROBE_BLK_LIMITS;
3261				xpt_schedule(periph, priority);
3262				return;
3263			}
3264		} else {
3265			int error;
3266			error = daerror(done_ccb, CAM_RETRY_SELTO,
3267					SF_RETRY_UA|SF_NO_PRINT);
3268			if (error == ERESTART)
3269				return;
3270			else if (error != 0) {
3271				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3272					/* Don't wedge this device's queue */
3273					cam_release_devq(done_ccb->ccb_h.path,
3274							 /*relsim_flags*/0,
3275							 /*reduction*/0,
3276							 /*timeout*/0,
3277							 /*getcount_only*/0);
3278				}
3279
3280				/*
3281				 * Failure indicates we don't support any SBC-3
3282				 * delete methods with UNMAP
3283				 */
3284			}
3285		}
3286
3287		free(lbp, M_SCSIDA);
3288		xpt_release_ccb(done_ccb);
3289		softc->state = DA_STATE_PROBE_BDC;
3290		xpt_schedule(periph, priority);
3291		return;
3292	}
3293	case DA_CCB_PROBE_BLK_LIMITS:
3294	{
3295		struct scsi_vpd_block_limits *block_limits;
3296
3297		block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3298
3299		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3300			uint32_t max_unmap_lba_cnt = scsi_4btoul(
3301				block_limits->max_unmap_lba_cnt);
3302			uint32_t max_unmap_blk_cnt = scsi_4btoul(
3303				block_limits->max_unmap_blk_cnt);
3304			uint64_t ws_max_blks = scsi_8btou64(
3305				block_limits->max_write_same_length);
3306			/*
3307			 * We should already support UNMAP but we check lba
3308			 * and block count to be sure
3309			 */
3310			if (max_unmap_lba_cnt != 0x00L &&
3311			    max_unmap_blk_cnt != 0x00L) {
3312				softc->unmap_max_lba = max_unmap_lba_cnt;
3313				softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3314					UNMAP_MAX_RANGES);
3315			} else {
3316				/*
3317				 * Unexpected UNMAP limits which means the
3318				 * device doesn't actually support UNMAP
3319				 */
3320				dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3321			}
3322
3323			if (ws_max_blks != 0x00L)
3324				softc->ws_max_blks = ws_max_blks;
3325		} else {
3326			int error;
3327			error = daerror(done_ccb, CAM_RETRY_SELTO,
3328					SF_RETRY_UA|SF_NO_PRINT);
3329			if (error == ERESTART)
3330				return;
3331			else if (error != 0) {
3332				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3333					/* Don't wedge this device's queue */
3334					cam_release_devq(done_ccb->ccb_h.path,
3335							 /*relsim_flags*/0,
3336							 /*reduction*/0,
3337							 /*timeout*/0,
3338							 /*getcount_only*/0);
3339				}
3340
3341				/*
3342				 * Failure here doesn't mean UNMAP is not
3343				 * supported as this is an optional page.
3344				 */
3345				softc->unmap_max_lba = 1;
3346				softc->unmap_max_ranges = 1;
3347			}
3348		}
3349
3350		free(block_limits, M_SCSIDA);
3351		xpt_release_ccb(done_ccb);
3352		softc->state = DA_STATE_PROBE_BDC;
3353		xpt_schedule(periph, priority);
3354		return;
3355	}
3356	case DA_CCB_PROBE_BDC:
3357	{
3358		struct scsi_vpd_block_characteristics *bdc;
3359
3360		bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3361
3362		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3363			/*
3364			 * Disable queue sorting for non-rotational media
3365			 * by default.
3366			 */
3367			if (scsi_2btoul(bdc->medium_rotation_rate) ==
3368			    SVPD_BDC_RATE_NONE_ROTATING)
3369				softc->sort_io_queue = 0;
3370		} else {
3371			int error;
3372			error = daerror(done_ccb, CAM_RETRY_SELTO,
3373					SF_RETRY_UA|SF_NO_PRINT);
3374			if (error == ERESTART)
3375				return;
3376			else if (error != 0) {
3377				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3378					/* Don't wedge this device's queue */
3379					cam_release_devq(done_ccb->ccb_h.path,
3380							 /*relsim_flags*/0,
3381							 /*reduction*/0,
3382							 /*timeout*/0,
3383							 /*getcount_only*/0);
3384				}
3385			}
3386		}
3387
3388		free(bdc, M_SCSIDA);
3389		xpt_release_ccb(done_ccb);
3390		softc->state = DA_STATE_PROBE_ATA;
3391		xpt_schedule(periph, priority);
3392		return;
3393	}
3394	case DA_CCB_PROBE_ATA:
3395	{
3396		int i;
3397		struct ata_params *ata_params;
3398		int16_t *ptr;
3399
3400		ata_params = (struct ata_params *)csio->data_ptr;
3401		ptr = (uint16_t *)ata_params;
3402
3403		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3404			for (i = 0; i < sizeof(*ata_params) / 2; i++)
3405				ptr[i] = le16toh(ptr[i]);
3406			if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM) {
3407				dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3408				if (ata_params->max_dsm_blocks != 0)
3409					softc->trim_max_ranges = min(
3410					  softc->trim_max_ranges,
3411					  ata_params->max_dsm_blocks *
3412					  ATA_DSM_BLK_RANGES);
3413			}
3414			/*
3415			 * Disable queue sorting for non-rotational media
3416			 * by default.
3417			 */
3418			if (ata_params->media_rotation_rate == 1)
3419				softc->sort_io_queue = 0;
3420		} else {
3421			int error;
3422			error = daerror(done_ccb, CAM_RETRY_SELTO,
3423					SF_RETRY_UA|SF_NO_PRINT);
3424			if (error == ERESTART)
3425				return;
3426			else if (error != 0) {
3427				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3428					/* Don't wedge this device's queue */
3429					cam_release_devq(done_ccb->ccb_h.path,
3430							 /*relsim_flags*/0,
3431							 /*reduction*/0,
3432							 /*timeout*/0,
3433							 /*getcount_only*/0);
3434				}
3435			}
3436		}
3437
3438		free(ata_params, M_SCSIDA);
3439		daprobedone(periph, done_ccb);
3440		return;
3441	}
3442	case DA_CCB_DUMP:
3443		/* No-op.  We're polling */
3444		return;
3445	case DA_CCB_TUR:
3446	{
3447		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3448
3449			if (daerror(done_ccb, CAM_RETRY_SELTO,
3450			    SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3451			    ERESTART)
3452				return;
3453			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3454				cam_release_devq(done_ccb->ccb_h.path,
3455						 /*relsim_flags*/0,
3456						 /*reduction*/0,
3457						 /*timeout*/0,
3458						 /*getcount_only*/0);
3459		}
3460		xpt_release_ccb(done_ccb);
3461		cam_periph_release_locked(periph);
3462		return;
3463	}
3464	default:
3465		break;
3466	}
3467	xpt_release_ccb(done_ccb);
3468}
3469
3470static void
3471dareprobe(struct cam_periph *periph)
3472{
3473	struct da_softc	  *softc;
3474	cam_status status;
3475
3476	softc = (struct da_softc *)periph->softc;
3477
3478	/* Probe in progress; don't interfere. */
3479	if (softc->state != DA_STATE_NORMAL)
3480		return;
3481
3482	status = cam_periph_acquire(periph);
3483	KASSERT(status == CAM_REQ_CMP,
3484	    ("dareprobe: cam_periph_acquire failed"));
3485
3486	if (softc->flags & DA_FLAG_CAN_RC16)
3487		softc->state = DA_STATE_PROBE_RC16;
3488	else
3489		softc->state = DA_STATE_PROBE_RC;
3490
3491	xpt_schedule(periph, CAM_PRIORITY_DEV);
3492}
3493
3494static int
3495daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3496{
3497	struct da_softc	  *softc;
3498	struct cam_periph *periph;
3499	int error, error_code, sense_key, asc, ascq;
3500
3501	periph = xpt_path_periph(ccb->ccb_h.path);
3502	softc = (struct da_softc *)periph->softc;
3503
3504 	/*
3505	 * Automatically detect devices that do not support
3506 	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3507 	 */
3508	error = 0;
3509	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3510		error = cmd6workaround(ccb);
3511	} else if (scsi_extract_sense_ccb(ccb,
3512	    &error_code, &sense_key, &asc, &ascq)) {
3513		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3514 			error = cmd6workaround(ccb);
3515		/*
3516		 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3517		 * query the capacity and notify upper layers.
3518		 */
3519		else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3520		    asc == 0x2A && ascq == 0x09) {
3521			xpt_print(periph->path, "capacity data has changed\n");
3522			dareprobe(periph);
3523			sense_flags |= SF_NO_PRINT;
3524		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3525		    asc == 0x28 && ascq == 0x00)
3526			disk_media_changed(softc->disk, M_NOWAIT);
3527		else if (sense_key == SSD_KEY_NOT_READY &&
3528		    asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3529			softc->flags |= DA_FLAG_PACK_INVALID;
3530			disk_media_gone(softc->disk, M_NOWAIT);
3531		}
3532	}
3533	if (error == ERESTART)
3534		return (ERESTART);
3535
3536	/*
3537	 * XXX
3538	 * Until we have a better way of doing pack validation,
3539	 * don't treat UAs as errors.
3540	 */
3541	sense_flags |= SF_RETRY_UA;
3542	return(cam_periph_error(ccb, cam_flags, sense_flags,
3543				&softc->saved_ccb));
3544}
3545
3546static void
3547damediapoll(void *arg)
3548{
3549	struct cam_periph *periph = arg;
3550	struct da_softc *softc = periph->softc;
3551
3552	if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3553		if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3554			softc->tur = 1;
3555			daschedule(periph);
3556		}
3557	}
3558	/* Queue us up again */
3559	if (da_poll_period != 0)
3560		callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3561}
3562
3563static void
3564daprevent(struct cam_periph *periph, int action)
3565{
3566	struct	da_softc *softc;
3567	union	ccb *ccb;
3568	int	error;
3569
3570	softc = (struct da_softc *)periph->softc;
3571
3572	if (((action == PR_ALLOW)
3573	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3574	 || ((action == PR_PREVENT)
3575	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3576		return;
3577	}
3578
3579	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3580
3581	scsi_prevent(&ccb->csio,
3582		     /*retries*/1,
3583		     /*cbcfp*/dadone,
3584		     MSG_SIMPLE_Q_TAG,
3585		     action,
3586		     SSD_FULL_SIZE,
3587		     5000);
3588
3589	error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3590	    SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3591
3592	if (error == 0) {
3593		if (action == PR_ALLOW)
3594			softc->flags &= ~DA_FLAG_PACK_LOCKED;
3595		else
3596			softc->flags |= DA_FLAG_PACK_LOCKED;
3597	}
3598
3599	xpt_release_ccb(ccb);
3600}
3601
3602static void
3603dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3604	  struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3605{
3606	struct ccb_calc_geometry ccg;
3607	struct da_softc *softc;
3608	struct disk_params *dp;
3609	u_int lbppbe, lalba;
3610	int error;
3611
3612	softc = (struct da_softc *)periph->softc;
3613
3614	dp = &softc->params;
3615	dp->secsize = block_len;
3616	dp->sectors = maxsector + 1;
3617	if (rcaplong != NULL) {
3618		lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3619		lalba = scsi_2btoul(rcaplong->lalba_lbp);
3620		lalba &= SRC16_LALBA_A;
3621	} else {
3622		lbppbe = 0;
3623		lalba = 0;
3624	}
3625
3626	if (lbppbe > 0) {
3627		dp->stripesize = block_len << lbppbe;
3628		dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3629		    dp->stripesize;
3630	} else if (softc->quirks & DA_Q_4K) {
3631		dp->stripesize = 4096;
3632		dp->stripeoffset = 0;
3633	} else {
3634		dp->stripesize = 0;
3635		dp->stripeoffset = 0;
3636	}
3637	/*
3638	 * Have the controller provide us with a geometry
3639	 * for this disk.  The only time the geometry
3640	 * matters is when we boot and the controller
3641	 * is the only one knowledgeable enough to come
3642	 * up with something that will make this a bootable
3643	 * device.
3644	 */
3645	xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3646	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3647	ccg.block_size = dp->secsize;
3648	ccg.volume_size = dp->sectors;
3649	ccg.heads = 0;
3650	ccg.secs_per_track = 0;
3651	ccg.cylinders = 0;
3652	xpt_action((union ccb*)&ccg);
3653	if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3654		/*
3655		 * We don't know what went wrong here- but just pick
3656		 * a geometry so we don't have nasty things like divide
3657		 * by zero.
3658		 */
3659		dp->heads = 255;
3660		dp->secs_per_track = 255;
3661		dp->cylinders = dp->sectors / (255 * 255);
3662		if (dp->cylinders == 0) {
3663			dp->cylinders = 1;
3664		}
3665	} else {
3666		dp->heads = ccg.heads;
3667		dp->secs_per_track = ccg.secs_per_track;
3668		dp->cylinders = ccg.cylinders;
3669	}
3670
3671	/*
3672	 * If the user supplied a read capacity buffer, and if it is
3673	 * different than the previous buffer, update the data in the EDT.
3674	 * If it's the same, we don't bother.  This avoids sending an
3675	 * update every time someone opens this device.
3676	 */
3677	if ((rcaplong != NULL)
3678	 && (bcmp(rcaplong, &softc->rcaplong,
3679		  min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3680		struct ccb_dev_advinfo cdai;
3681
3682		xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3683		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3684		cdai.buftype = CDAI_TYPE_RCAPLONG;
3685		cdai.flags |= CDAI_FLAG_STORE;
3686		cdai.bufsiz = rcap_len;
3687		cdai.buf = (uint8_t *)rcaplong;
3688		xpt_action((union ccb *)&cdai);
3689		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3690			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3691		if (cdai.ccb_h.status != CAM_REQ_CMP) {
3692			xpt_print(periph->path, "%s: failed to set read "
3693				  "capacity advinfo\n", __func__);
3694			/* Use cam_error_print() to decode the status */
3695			cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3696					CAM_EPF_ALL);
3697		} else {
3698			bcopy(rcaplong, &softc->rcaplong,
3699			      min(sizeof(softc->rcaplong), rcap_len));
3700		}
3701	}
3702
3703	softc->disk->d_sectorsize = softc->params.secsize;
3704	softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3705	softc->disk->d_stripesize = softc->params.stripesize;
3706	softc->disk->d_stripeoffset = softc->params.stripeoffset;
3707	/* XXX: these are not actually "firmware" values, so they may be wrong */
3708	softc->disk->d_fwsectors = softc->params.secs_per_track;
3709	softc->disk->d_fwheads = softc->params.heads;
3710	softc->disk->d_devstat->block_size = softc->params.secsize;
3711	softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3712
3713	error = disk_resize(softc->disk, M_NOWAIT);
3714	if (error != 0)
3715		xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3716}
3717
3718static void
3719dasendorderedtag(void *arg)
3720{
3721	struct da_softc *softc = arg;
3722
3723	if (da_send_ordered) {
3724		if (!LIST_EMPTY(&softc->pending_ccbs)) {
3725			if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3726				softc->flags |= DA_FLAG_NEED_OTAG;
3727			softc->flags &= ~DA_FLAG_WAS_OTAG;
3728		}
3729	}
3730	/* Queue us up again */
3731	callout_reset(&softc->sendordered_c,
3732	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3733	    dasendorderedtag, softc);
3734}
3735
3736/*
3737 * Step through all DA peripheral drivers, and if the device is still open,
3738 * sync the disk cache to physical media.
3739 */
3740static void
3741dashutdown(void * arg, int howto)
3742{
3743	struct cam_periph *periph;
3744	struct da_softc *softc;
3745	union ccb *ccb;
3746	int error;
3747
3748	CAM_PERIPH_FOREACH(periph, &dadriver) {
3749		softc = (struct da_softc *)periph->softc;
3750		if (SCHEDULER_STOPPED()) {
3751			/* If we paniced with the lock held, do not recurse. */
3752			if (!cam_periph_owned(periph) &&
3753			    (softc->flags & DA_FLAG_OPEN)) {
3754				dadump(softc->disk, NULL, 0, 0, 0);
3755			}
3756			continue;
3757		}
3758		cam_periph_lock(periph);
3759
3760		/*
3761		 * We only sync the cache if the drive is still open, and
3762		 * if the drive is capable of it..
3763		 */
3764		if (((softc->flags & DA_FLAG_OPEN) == 0)
3765		 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3766			cam_periph_unlock(periph);
3767			continue;
3768		}
3769
3770		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3771		scsi_synchronize_cache(&ccb->csio,
3772				       /*retries*/0,
3773				       /*cbfcnp*/dadone,
3774				       MSG_SIMPLE_Q_TAG,
3775				       /*begin_lba*/0, /* whole disk */
3776				       /*lb_count*/0,
3777				       SSD_FULL_SIZE,
3778				       60 * 60 * 1000);
3779
3780		error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3781		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3782		    softc->disk->d_devstat);
3783		if (error != 0)
3784			xpt_print(periph->path, "Synchronize cache failed\n");
3785		xpt_release_ccb(ccb);
3786		cam_periph_unlock(periph);
3787	}
3788}
3789
3790#else /* !_KERNEL */
3791
3792/*
3793 * XXX This is only left out of the kernel build to silence warnings.  If,
3794 * for some reason this function is used in the kernel, the ifdefs should
3795 * be moved so it is included both in the kernel and userland.
3796 */
3797void
3798scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3799		 void (*cbfcnp)(struct cam_periph *, union ccb *),
3800		 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3801		 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3802		 u_int32_t timeout)
3803{
3804	struct scsi_format_unit *scsi_cmd;
3805
3806	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3807	scsi_cmd->opcode = FORMAT_UNIT;
3808	scsi_cmd->byte2 = byte2;
3809	scsi_ulto2b(ileave, scsi_cmd->interleave);
3810
3811	cam_fill_csio(csio,
3812		      retries,
3813		      cbfcnp,
3814		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3815		      tag_action,
3816		      data_ptr,
3817		      dxfer_len,
3818		      sense_len,
3819		      sizeof(*scsi_cmd),
3820		      timeout);
3821}
3822
3823void
3824scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
3825	      void (*cbfcnp)(struct cam_periph *, union ccb *),
3826	      u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
3827	      u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3828	      u_int32_t timeout)
3829{
3830	struct scsi_sanitize *scsi_cmd;
3831
3832	scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
3833	scsi_cmd->opcode = SANITIZE;
3834	scsi_cmd->byte2 = byte2;
3835	scsi_cmd->control = control;
3836	scsi_ulto2b(dxfer_len, scsi_cmd->length);
3837
3838	cam_fill_csio(csio,
3839		      retries,
3840		      cbfcnp,
3841		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3842		      tag_action,
3843		      data_ptr,
3844		      dxfer_len,
3845		      sense_len,
3846		      sizeof(*scsi_cmd),
3847		      timeout);
3848}
3849
3850#endif /* _KERNEL */
3851