xref: /freebsd/sys/cam/ctl/ctl_backend.h (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 /*-
2  * Copyright (c) 2003 Silicon Graphics International Corp.
3  * Copyright (c) 2014-2017 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 #include <cam/ctl/ctl_ioctl.h>
44 
45 typedef enum {
46 	CTL_LUN_SERSEQ_OFF,
47 	CTL_LUN_SERSEQ_READ,
48 	CTL_LUN_SERSEQ_ON
49 } ctl_lun_serseq;
50 
51 #ifdef _KERNEL
52 
53 #define CTL_BACKEND_DECLARE(name, driver) \
54 	static int name ## _modevent(module_t mod, int type, void *data) \
55 	{ \
56 		switch (type) { \
57 		case MOD_LOAD: \
58 			ctl_backend_register( \
59 				(struct ctl_backend_driver *)data); \
60 			break; \
61 		case MOD_UNLOAD: \
62 			printf(#name " module unload - not possible for this module type\n"); \
63 			return EINVAL; \
64 		default: \
65 			return EOPNOTSUPP; \
66 		} \
67 		return 0; \
68 	} \
69 	static moduledata_t name ## _mod = { \
70 		#name, \
71 		name ## _modevent, \
72 		(void *)&driver \
73 	}; \
74 	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
75 	MODULE_DEPEND(name, ctl, 1, 1, 1); \
76 	MODULE_DEPEND(name, cam, 1, 1, 1)
77 
78 
79 typedef enum {
80 	CTL_LUN_CONFIG_OK,
81 	CTL_LUN_CONFIG_FAILURE
82 } ctl_lun_config_status;
83 
84 typedef void (*be_callback_t)(void *be_lun);
85 typedef void (*be_lun_config_t)(void *be_lun,
86 				ctl_lun_config_status status);
87 
88 /*
89  * The lun_type field is the SCSI device type of this particular LUN.  In
90  * general, this should be T_DIRECT, although backends will want to create
91  * a processor LUN, typically at LUN 0.  See scsi_all.h for the defines for
92  * the various SCSI device types.
93  *
94  * The flags are described above.
95  *
96  * The be_lun field is the backend driver's own context that will get
97  * passsed back so that it can tell which LUN CTL is referencing.
98  *
99  * maxlba is the maximum accessible LBA on the LUN.  Note that this is
100  * different from the capacity of the array.  capacity = maxlba + 1
101  *
102  * blocksize is the size, in bytes, of each LBA on the LUN.  In general
103  * this should be 512.  In theory CTL should be able to handle other block
104  * sizes.  Host application software may not deal with it very well, though.
105  *
106  * pblockexp is the log2() of number of LBAs on the LUN per physical sector.
107  *
108  * pblockoff is the lowest LBA on the LUN aligned to physical sector.
109  *
110  * ublockexp is the log2() of number of LBAs on the LUN per UNMAP block.
111  *
112  * ublockoff is the lowest LBA on the LUN aligned to UNMAP block.
113  *
114  * atomicblock is the number of blocks that can be written atomically.
115  *
116  * opttxferlen is the number of blocks that can be written in one operation.
117  *
118  * req_lun_id is the requested LUN ID.  CTL only pays attention to this
119  * field if the CTL_LUN_FLAG_ID_REQ flag is set.  If the requested LUN ID is
120  * not available, the LUN addition will fail.  If a particular LUN ID isn't
121  * requested, the first available LUN ID will be allocated.
122  *
123  * serial_num is the device serial number returned in the SCSI INQUIRY VPD
124  * page 0x80.  This should be a unique, per-shelf value.  The data inside
125  * this field should be ASCII only, left aligned, and any unused space
126  * should be padded out with ASCII spaces.  This field should NOT be NULL
127  * terminated.
128  *
129  * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD
130  * page 0x83.  This should be a unique, per-LUN value.  The data inside
131  * this field should be ASCII only, left aligned, and any unused space
132  * should be padded with ASCII spaces.  This field should NOT be NULL
133  * terminated.
134  *
135  * The lun_shutdown() method is the callback for the ctl_invalidate_lun()
136  * call.  It is called when all outstanding I/O for that LUN has been
137  * completed and CTL has deleted the resources for that LUN.  When the CTL
138  * backend gets this call, it can safely free its per-LUN resources.
139  *
140  * The lun_config_status() method is the callback for the ctl_add_lun()
141  * call.  It is called when the LUN is successfully added, or when LUN
142  * addition fails.  If the LUN is successfully added, the backend may call
143  * the ctl_enable_lun() method to enable the LUN.
144  *
145  * The be field is a pointer to the ctl_backend_driver structure, which
146  * contains the backend methods to be called by CTL.
147  *
148  * The ctl_lun field is for CTL internal use only, and should not be used
149  * by the backend.
150  *
151  * The links field is for CTL internal use only, and should not be used by
152  * the backend.
153  */
154 struct ctl_be_lun {
155 	uint8_t			lun_type;	/* passed to CTL */
156 	ctl_backend_lun_flags	flags;		/* passed to CTL */
157 	ctl_lun_serseq		serseq;		/* passed to CTL */
158 	void			*be_lun;	/* passed to CTL */
159 	uint64_t		maxlba;		/* passed to CTL */
160 	uint32_t		blocksize;	/* passed to CTL */
161 	uint16_t		pblockexp;	/* passed to CTL */
162 	uint16_t		pblockoff;	/* passed to CTL */
163 	uint16_t		ublockexp;	/* passed to CTL */
164 	uint16_t		ublockoff;	/* passed to CTL */
165 	uint32_t		atomicblock;	/* passed to CTL */
166 	uint32_t		opttxferlen;	/* passed to CTL */
167 	uint32_t		req_lun_id;	/* passed to CTL */
168 	uint32_t		lun_id;		/* returned from CTL */
169 	uint8_t			serial_num[CTL_SN_LEN];	 /* passed to CTL */
170 	uint8_t			device_id[CTL_DEVID_LEN];/* passed to CTL */
171 	be_callback_t		lun_shutdown;	/* passed to CTL */
172 	be_lun_config_t		lun_config_status; /* passed to CTL */
173 	struct ctl_backend_driver *be;		/* passed to CTL */
174 	void			*ctl_lun;	/* used by CTL */
175 	ctl_options_t		options;	/* passed to CTL */
176 	STAILQ_ENTRY(ctl_be_lun) links;		/* used by CTL */
177 };
178 
179 typedef enum {
180 	CTL_BE_FLAG_NONE	= 0x00,	/* no flags */
181 	CTL_BE_FLAG_HAS_CONFIG	= 0x01,	/* can do config reads, writes */
182 	CTL_BE_FLAG_INTERNAL	= 0x02	/* don't inc mod refcount */
183 } ctl_backend_flags;
184 
185 typedef int (*be_init_t)(void);
186 typedef int (*be_func_t)(union ctl_io *io);
187 typedef void (*be_vfunc_t)(union ctl_io *io);
188 typedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
189 			  struct thread *td);
190 typedef int (*be_luninfo_t)(void *be_lun, struct sbuf *sb);
191 typedef uint64_t (*be_lunattr_t)(void *be_lun, const char *attrname);
192 
193 struct ctl_backend_driver {
194 	char		  name[CTL_BE_NAME_LEN]; /* passed to CTL */
195 	ctl_backend_flags flags;	         /* passed to CTL */
196 	be_init_t	  init;			 /* passed to CTL */
197 	be_func_t	  data_submit;		 /* passed to CTL */
198 	be_func_t	  data_move_done;	 /* passed to CTL */
199 	be_func_t	  config_read;		 /* passed to CTL */
200 	be_func_t	  config_write;		 /* passed to CTL */
201 	be_ioctl_t	  ioctl;		 /* passed to CTL */
202 	be_luninfo_t	  lun_info;		 /* passed to CTL */
203 	be_lunattr_t	  lun_attr;		 /* passed to CTL */
204 #ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
205 	be_func_t	  config_move_done;	 /* passed to backend */
206 #endif
207 #if 0
208 	be_vfunc_t	  config_write_done;	 /* passed to backend */
209 #endif
210 	u_int		  num_luns;		 /* used by CTL */
211 	STAILQ_ENTRY(ctl_backend_driver) links;	 /* used by CTL */
212 };
213 
214 int ctl_backend_register(struct ctl_backend_driver *be);
215 int ctl_backend_deregister(struct ctl_backend_driver *be);
216 struct ctl_backend_driver *ctl_backend_find(char *backend_name);
217 
218 /*
219  * To add a LUN, first call ctl_add_lun().  You will get the lun_config_status()
220  * callback when the LUN addition has either succeeded or failed.
221  *
222  * Once you get that callback, you can then call ctl_enable_lun() to enable
223  * the LUN.
224  */
225 int ctl_add_lun(struct ctl_be_lun *be_lun);
226 int ctl_enable_lun(struct ctl_be_lun *be_lun);
227 
228 /*
229  * To delete a LUN, first call ctl_disable_lun(), then
230  * ctl_invalidate_lun().  You will get the lun_shutdown() callback when all
231  * I/O to the LUN has completed and the LUN has been deleted.
232  */
233 int ctl_disable_lun(struct ctl_be_lun *be_lun);
234 int ctl_invalidate_lun(struct ctl_be_lun *be_lun);
235 
236 /*
237  * To start a LUN (transition from powered off to powered on state) call
238  * ctl_start_lun().  To stop a LUN (transition from powered on to powered
239  * off state) call ctl_stop_lun().
240  */
241 int ctl_start_lun(struct ctl_be_lun *be_lun);
242 int ctl_stop_lun(struct ctl_be_lun *be_lun);
243 
244 /*
245  * Methods to notify about media and tray status changes.
246  */
247 int ctl_lun_no_media(struct ctl_be_lun *be_lun);
248 int ctl_lun_has_media(struct ctl_be_lun *be_lun);
249 int ctl_lun_ejected(struct ctl_be_lun *be_lun);
250 
251 /*
252  * Called on LUN HA role change.
253  */
254 int ctl_lun_primary(struct ctl_be_lun *be_lun);
255 int ctl_lun_secondary(struct ctl_be_lun *be_lun);
256 
257 /*
258  * Let the backend notify the initiators about changes.
259  */
260 void ctl_lun_capacity_changed(struct ctl_be_lun *be_lun);
261 
262 #endif /* _KERNEL */
263 #endif /* _CTL_BACKEND_H_ */
264 
265 /*
266  * vim: ts=8
267  */
268