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