xref: /freebsd/sys/cam/ctl/ctl_backend.h (revision a0ee8cc6)
1 /*-
2  * Copyright (c) 2003 Silicon Graphics International Corp.
3  * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification.
12  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13  *    substantially similar to the "NO WARRANTY" disclaimer below
14  *    ("Disclaimer") and any redistribution must be conditioned upon
15  *    including a substantially similar Disclaimer requirement for further
16  *    binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGES.
30  *
31  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend.h#2 $
32  * $FreeBSD$
33  */
34 /*
35  * CTL backend driver definitions
36  *
37  * Author: Ken Merry <ken@FreeBSD.org>
38  */
39 
40 #ifndef	_CTL_BACKEND_H_
41 #define	_CTL_BACKEND_H_
42 
43 /*
44  * XXX KDM move this to another header file?
45  */
46 #define	CTL_BE_NAME_LEN		32
47 
48 /*
49  * The ID_REQ flag is used to say that the caller has requested a
50  * particular LUN ID in the req_lun_id field.  If we cannot allocate that
51  * LUN ID, the ctl_add_lun() call will fail.
52  *
53  * The STOPPED flag tells us that the LUN should default to the powered
54  * off state.  It will return 0x04,0x02 until it is powered up.  ("Logical
55  * unit not ready, initializing command required.")
56  *
57  * The NO_MEDIA flag tells us that the LUN has no media inserted.
58  *
59  * The PRIMARY flag tells us that this LUN is registered as a Primary LUN
60  * which is accessible via the Master shelf controller in an HA. This flag
61  * being set indicates a Primary LUN. This flag being reset represents a
62  * Secondary LUN controlled by the Secondary controller in an HA
63  * configuration. Flag is applicable at this time to T_DIRECT types.
64  *
65  * The SERIAL_NUM flag tells us that the serial_num field is filled in and
66  * valid for use in SCSI INQUIRY VPD page 0x80.
67  *
68  * The DEVID flag tells us that the device_id field is filled in and
69  * valid for use in SCSI INQUIRY VPD page 0x83.
70  *
71  * The DEV_TYPE flag tells us that the device_type field is filled in.
72  *
73  * The EJECTED flag tells us that the removable LUN has tray open.
74  *
75  * The UNMAP flag tells us that this LUN supports UNMAP.
76  *
77  * The OFFLINE flag tells us that this LUN can not access backing store.
78  */
79 typedef enum {
80 	CTL_LUN_FLAG_ID_REQ		= 0x01,
81 	CTL_LUN_FLAG_STOPPED		= 0x02,
82 	CTL_LUN_FLAG_NO_MEDIA		= 0x04,
83 	CTL_LUN_FLAG_PRIMARY		= 0x08,
84 	CTL_LUN_FLAG_SERIAL_NUM		= 0x10,
85 	CTL_LUN_FLAG_DEVID		= 0x20,
86 	CTL_LUN_FLAG_DEV_TYPE		= 0x40,
87 	CTL_LUN_FLAG_UNMAP		= 0x80,
88 	CTL_LUN_FLAG_EJECTED		= 0x100,
89 	CTL_LUN_FLAG_READONLY		= 0x200
90 } ctl_backend_lun_flags;
91 
92 typedef enum {
93 	CTL_LUN_SERSEQ_OFF,
94 	CTL_LUN_SERSEQ_READ,
95 	CTL_LUN_SERSEQ_ON
96 } ctl_lun_serseq;
97 
98 #ifdef _KERNEL
99 
100 #define CTL_BACKEND_DECLARE(name, driver) \
101 	static int name ## _modevent(module_t mod, int type, void *data) \
102 	{ \
103 		switch (type) { \
104 		case MOD_LOAD: \
105 			ctl_backend_register( \
106 				(struct ctl_backend_driver *)data); \
107 			break; \
108 		case MOD_UNLOAD: \
109 			printf(#name " module unload - not possible for this module type\n"); \
110 			return EINVAL; \
111 		default: \
112 			return EOPNOTSUPP; \
113 		} \
114 		return 0; \
115 	} \
116 	static moduledata_t name ## _mod = { \
117 		#name, \
118 		name ## _modevent, \
119 		(void *)&driver \
120 	}; \
121 	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
122 	MODULE_DEPEND(name, ctl, 1, 1, 1); \
123 	MODULE_DEPEND(name, cam, 1, 1, 1)
124 
125 
126 typedef enum {
127 	CTL_LUN_CONFIG_OK,
128 	CTL_LUN_CONFIG_FAILURE
129 } ctl_lun_config_status;
130 
131 typedef void (*be_callback_t)(void *be_lun);
132 typedef void (*be_lun_config_t)(void *be_lun,
133 				ctl_lun_config_status status);
134 
135 /*
136  * The lun_type field is the SCSI device type of this particular LUN.  In
137  * general, this should be T_DIRECT, although backends will want to create
138  * a processor LUN, typically at LUN 0.  See scsi_all.h for the defines for
139  * the various SCSI device types.
140  *
141  * The flags are described above.
142  *
143  * The be_lun field is the backend driver's own context that will get
144  * passsed back so that it can tell which LUN CTL is referencing.
145  *
146  * maxlba is the maximum accessible LBA on the LUN.  Note that this is
147  * different from the capacity of the array.  capacity = maxlba + 1
148  *
149  * blocksize is the size, in bytes, of each LBA on the LUN.  In general
150  * this should be 512.  In theory CTL should be able to handle other block
151  * sizes.  Host application software may not deal with it very well, though.
152  *
153  * pblockexp is the log2() of number of LBAs on the LUN per physical sector.
154  *
155  * pblockoff is the lowest LBA on the LUN aligned to physical sector.
156  *
157  * ublockexp is the log2() of number of LBAs on the LUN per UNMAP block.
158  *
159  * ublockoff is the lowest LBA on the LUN aligned to UNMAP block.
160  *
161  * atomicblock is the number of blocks that can be written atomically.
162  *
163  * opttxferlen is the number of blocks that can be written in one operation.
164  *
165  * req_lun_id is the requested LUN ID.  CTL only pays attention to this
166  * field if the CTL_LUN_FLAG_ID_REQ flag is set.  If the requested LUN ID is
167  * not available, the LUN addition will fail.  If a particular LUN ID isn't
168  * requested, the first available LUN ID will be allocated.
169  *
170  * serial_num is the device serial number returned in the SCSI INQUIRY VPD
171  * page 0x80.  This should be a unique, per-shelf value.  The data inside
172  * this field should be ASCII only, left aligned, and any unused space
173  * should be padded out with ASCII spaces.  This field should NOT be NULL
174  * terminated.
175  *
176  * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD
177  * page 0x83.  This should be a unique, per-LUN value.  The data inside
178  * this field should be ASCII only, left aligned, and any unused space
179  * should be padded with ASCII spaces.  This field should NOT be NULL
180  * terminated.
181  *
182  * The lun_shutdown() method is the callback for the ctl_invalidate_lun()
183  * call.  It is called when all outstanding I/O for that LUN has been
184  * completed and CTL has deleted the resources for that LUN.  When the CTL
185  * backend gets this call, it can safely free its per-LUN resources.
186  *
187  * The lun_config_status() method is the callback for the ctl_add_lun()
188  * call.  It is called when the LUN is successfully added, or when LUN
189  * addition fails.  If the LUN is successfully added, the backend may call
190  * the ctl_enable_lun() method to enable the LUN.
191  *
192  * The be field is a pointer to the ctl_backend_driver structure, which
193  * contains the backend methods to be called by CTL.
194  *
195  * The ctl_lun field is for CTL internal use only, and should not be used
196  * by the backend.
197  *
198  * The links field is for CTL internal use only, and should not be used by
199  * the backend.
200  */
201 struct ctl_be_lun {
202 	uint8_t			lun_type;	/* passed to CTL */
203 	ctl_backend_lun_flags	flags;		/* passed to CTL */
204 	ctl_lun_serseq		serseq;		/* passed to CTL */
205 	void			*be_lun;	/* passed to CTL */
206 	uint64_t		maxlba;		/* passed to CTL */
207 	uint32_t		blocksize;	/* passed to CTL */
208 	uint16_t		pblockexp;	/* passed to CTL */
209 	uint16_t		pblockoff;	/* passed to CTL */
210 	uint16_t		ublockexp;	/* passed to CTL */
211 	uint16_t		ublockoff;	/* passed to CTL */
212 	uint32_t		atomicblock;	/* passed to CTL */
213 	uint32_t		opttxferlen;	/* passed to CTL */
214 	uint32_t		req_lun_id;	/* passed to CTL */
215 	uint32_t		lun_id;		/* returned from CTL */
216 	uint8_t			serial_num[CTL_SN_LEN];	 /* passed to CTL */
217 	uint8_t			device_id[CTL_DEVID_LEN];/* passed to CTL */
218 	be_callback_t		lun_shutdown;	/* passed to CTL */
219 	be_lun_config_t		lun_config_status; /* passed to CTL */
220 	struct ctl_backend_driver *be;		/* passed to CTL */
221 	void			*ctl_lun;	/* used by CTL */
222 	ctl_options_t		options;	/* passed to CTL */
223 	STAILQ_ENTRY(ctl_be_lun) links;		/* used by CTL */
224 };
225 
226 typedef enum {
227 	CTL_BE_FLAG_NONE	= 0x00,	/* no flags */
228 	CTL_BE_FLAG_HAS_CONFIG	= 0x01,	/* can do config reads, writes */
229 	CTL_BE_FLAG_INTERNAL	= 0x02	/* don't inc mod refcount */
230 } ctl_backend_flags;
231 
232 typedef int (*be_init_t)(void);
233 typedef int (*be_func_t)(union ctl_io *io);
234 typedef void (*be_vfunc_t)(union ctl_io *io);
235 typedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
236 			  struct thread *td);
237 typedef int (*be_luninfo_t)(void *be_lun, struct sbuf *sb);
238 typedef uint64_t (*be_lunattr_t)(void *be_lun, const char *attrname);
239 
240 struct ctl_backend_driver {
241 	char		  name[CTL_BE_NAME_LEN]; /* passed to CTL */
242 	ctl_backend_flags flags;	         /* passed to CTL */
243 	be_init_t	  init;			 /* passed to CTL */
244 	be_func_t	  data_submit;		 /* passed to CTL */
245 	be_func_t	  data_move_done;	 /* passed to CTL */
246 	be_func_t	  config_read;		 /* passed to CTL */
247 	be_func_t	  config_write;		 /* passed to CTL */
248 	be_ioctl_t	  ioctl;		 /* passed to CTL */
249 	be_luninfo_t	  lun_info;		 /* passed to CTL */
250 	be_lunattr_t	  lun_attr;		 /* passed to CTL */
251 #ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
252 	be_func_t	  config_move_done;	 /* passed to backend */
253 #endif
254 #if 0
255 	be_vfunc_t	  config_write_done;	 /* passed to backend */
256 #endif
257 	u_int		  num_luns;		 /* used by CTL */
258 	STAILQ_ENTRY(ctl_backend_driver) links;	 /* used by CTL */
259 };
260 
261 int ctl_backend_register(struct ctl_backend_driver *be);
262 int ctl_backend_deregister(struct ctl_backend_driver *be);
263 struct ctl_backend_driver *ctl_backend_find(char *backend_name);
264 
265 /*
266  * To add a LUN, first call ctl_add_lun().  You will get the lun_config_status()
267  * callback when the LUN addition has either succeeded or failed.
268  *
269  * Once you get that callback, you can then call ctl_enable_lun() to enable
270  * the LUN.
271  */
272 int ctl_add_lun(struct ctl_be_lun *be_lun);
273 int ctl_enable_lun(struct ctl_be_lun *be_lun);
274 
275 /*
276  * To delete a LUN, first call ctl_disable_lun(), then
277  * ctl_invalidate_lun().  You will get the lun_shutdown() callback when all
278  * I/O to the LUN has completed and the LUN has been deleted.
279  */
280 int ctl_disable_lun(struct ctl_be_lun *be_lun);
281 int ctl_invalidate_lun(struct ctl_be_lun *be_lun);
282 
283 /*
284  * To start a LUN (transition from powered off to powered on state) call
285  * ctl_start_lun().  To stop a LUN (transition from powered on to powered
286  * off state) call ctl_stop_lun().
287  */
288 int ctl_start_lun(struct ctl_be_lun *be_lun);
289 int ctl_stop_lun(struct ctl_be_lun *be_lun);
290 
291 /*
292  * Methods to notify about media and tray status changes.
293  */
294 int ctl_lun_no_media(struct ctl_be_lun *be_lun);
295 int ctl_lun_has_media(struct ctl_be_lun *be_lun);
296 int ctl_lun_ejected(struct ctl_be_lun *be_lun);
297 
298 /*
299  * Called on LUN HA role change.
300  */
301 int ctl_lun_primary(struct ctl_be_lun *be_lun);
302 int ctl_lun_secondary(struct ctl_be_lun *be_lun);
303 
304 /*
305  * Let the backend notify the initiators about changes.
306  */
307 void ctl_lun_capacity_changed(struct ctl_be_lun *be_lun);
308 
309 #endif /* _KERNEL */
310 #endif /* _CTL_BACKEND_H_ */
311 
312 /*
313  * vim: ts=8
314  */
315