xref: /illumos-gate/usr/src/uts/common/sys/blkdev.h (revision 698f4ab6008be205f4362675967638572eef4f21)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
23  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
24  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright 2019 Western Digital Corporation.
26  */
27 
28 #ifndef	_SYS_BLKDEV_H
29 #define	_SYS_BLKDEV_H
30 
31 #include <sys/types.h>
32 #include <sys/ksynch.h>
33 #include <sys/ddi.h>
34 #include <sys/sunddi.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * This describes a fairly simple block device.  The idea here is that
42  * these things want to take advantage of the common labelling support,
43  * but do not need all the capabilities of SCSA.  So we make quite a few
44  * simplifications:
45  *
46  * 1) Device block size is a power of 2 greater or equal to 512 bytes.
47  *    An optional physical block size can be reported if the underlying
48  *    device uses larger block sizes internally, so that writes can be
49  *    aligned properly.
50  *
51  * 2) Non-rotating media.  We assume a simple linear layout.
52  *
53  * 3) Fixed queue depth, for each device.  The adapter driver reports
54  *    the queue depth at registration.  We don't have any form of
55  *    dynamic flow control.
56  *
57  * 4) Negligible power management support.  The framework does not support
58  *    fine grained power management.  If the adapter driver wants to use
59  *    such, it will need to manage power on its own.
60  *
61  * 5) Suspend/resume support managed by the adapter driver.  We don't
62  *    support suspend/resume directly.  The adapter device driver will
63  *    need to manage this on its own behalf.
64  *
65  * 6) No request priorities.  Transfers are assumed to execute in
66  *    roughly FIFO order.  The adapter driver may reorder them, but the
67  *    submitter has no control over that.
68  *
69  * 7) No request cancellation.  Once submitted, the job completes or
70  *    fails.  It cannot be canceled.
71  *
72  * 8) Limited support for removable media.  There is no support for
73  *    locking bay doors or mechanised media bays.  This could be
74  *    added, but at present the only such interesting devices are
75  *    covered by the SCSI disk driver.
76  */
77 
78 typedef struct bd_handle *bd_handle_t;
79 typedef struct bd_xfer bd_xfer_t;
80 typedef struct bd_drive bd_drive_t;
81 typedef struct bd_media bd_media_t;
82 typedef struct bd_ops bd_ops_t;
83 
84 
85 struct bd_xfer {
86 	/*
87 	 * NB: If using DMA the br_ndmac will be non-zero.  Otherwise
88 	 * the br_kaddr will be non-NULL.
89 	 */
90 	diskaddr_t		x_blkno;
91 	size_t			x_nblks;
92 	ddi_dma_handle_t	x_dmah;
93 	ddi_dma_cookie_t	x_dmac;
94 	unsigned		x_ndmac;
95 	caddr_t			x_kaddr;
96 	unsigned		x_flags;
97 	unsigned		x_qnum;
98 };
99 
100 #define	BD_XFER_POLL		(1U << 0)	/* no interrupts (dump) */
101 
102 struct bd_drive {
103 	uint32_t		d_qsize;
104 	uint32_t		d_maxxfer;
105 	boolean_t		d_removable;
106 	boolean_t		d_hotpluggable;
107 	int			d_target;
108 	int			d_lun;
109 	size_t			d_vendor_len;
110 	char			*d_vendor;
111 	size_t			d_product_len;
112 	char			*d_product;
113 	size_t			d_model_len;
114 	char			*d_model;
115 	size_t			d_serial_len;
116 	char			*d_serial;
117 	size_t			d_revision_len;
118 	char			*d_revision;
119 	uint8_t			d_eui64[8];
120 	/* Added at the end to maintain binary compatibility */
121 	uint32_t		d_qcount;
122 };
123 
124 struct bd_media {
125 	/*
126 	 * NB: The block size must be a power of two not less than
127 	 * DEV_BSIZE (512).  Other values of the block size will
128 	 * simply not function and the media will be rejected.
129 	 *
130 	 * The block size must also divide evenly into the device's
131 	 * d_maxxfer field.  If the maxxfer is a power of two larger
132 	 * than the block size, then this will automatically be
133 	 * satisfied.
134 	 *
135 	 * The physical block size (m_pblksize) must be 0 or a power
136 	 * of two not less than the block size.
137 	 */
138 	uint64_t		m_nblks;
139 	uint32_t		m_blksize;
140 	boolean_t		m_readonly;
141 	boolean_t		m_solidstate;
142 	uint32_t		m_pblksize;
143 };
144 
145 #define	BD_INFO_FLAG_REMOVABLE		(1U << 0)
146 #define	BD_INFO_FLAG_HOTPLUGGABLE	(1U << 1)
147 #define	BD_INFO_FLAG_READ_ONLY		(1U << 2)
148 
149 /*
150  * If the API changes and we want to bump the version, add another
151  * enum value, Eg BD_OPS_VERSION_1. BD_OPS_CURRENT_VERSION should always
152  * be last.
153  */
154 typedef enum {
155 	BD_OPS_VERSION_0 = 0,
156 	BD_OPS_CURRENT_VERSION
157 } bd_version_t;
158 
159 struct bd_ops {
160 	bd_version_t	o_version;
161 	void		(*o_drive_info)(void *, bd_drive_t *);
162 	int		(*o_media_info)(void *, bd_media_t *);
163 	int		(*o_devid_init)(void *, dev_info_t *, ddi_devid_t *);
164 	int		(*o_sync_cache)(void *, bd_xfer_t *);
165 	int		(*o_read)(void *, bd_xfer_t *);
166 	int		(*o_write)(void *, bd_xfer_t *);
167 };
168 
169 struct bd_errstats {
170 	/* these are managed by blkdev itself */
171 	kstat_named_t	bd_softerrs;
172 	kstat_named_t	bd_harderrs;
173 	kstat_named_t	bd_transerrs;
174 	kstat_named_t	bd_model;
175 	kstat_named_t	bd_vid;
176 	kstat_named_t	bd_pid;
177 	kstat_named_t	bd_revision;
178 	kstat_named_t	bd_serial;
179 	kstat_named_t	bd_capacity;
180 
181 	/* the following are updated on behalf of the HW driver */
182 	kstat_named_t	bd_rq_media_err;
183 	kstat_named_t	bd_rq_ntrdy_err;
184 	kstat_named_t	bd_rq_nodev_err;
185 	kstat_named_t	bd_rq_recov_err;
186 	kstat_named_t	bd_rq_illrq_err;
187 	kstat_named_t	bd_rq_pfa_err;
188 };
189 
190 #define	BD_ERR_MEDIA	0
191 #define	BD_ERR_NTRDY	1
192 #define	BD_ERR_NODEV	2
193 #define	BD_ERR_RECOV	3
194 #define	BD_ERR_ILLRQ	4
195 #define	BD_ERR_PFA	5
196 
197 /*
198  * Note, one handler *per* address.  Drivers with multiple targets at
199  * different addresses must use separate handles.
200  */
201 bd_handle_t	bd_alloc_handle(void *, bd_ops_t *, ddi_dma_attr_t *, int);
202 void		bd_free_handle(bd_handle_t);
203 int		bd_attach_handle(dev_info_t *, bd_handle_t);
204 int		bd_detach_handle(bd_handle_t);
205 void		bd_state_change(bd_handle_t);
206 void		bd_xfer_done(bd_xfer_t *, int);
207 void		bd_error(bd_xfer_t *, int);
208 void		bd_mod_init(struct dev_ops *);
209 void		bd_mod_fini(struct dev_ops *);
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif	/* _SYS_BLKDEV_H */
216