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