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