xref: /freebsd/sys/cam/ctl/ctl_frontend.h (revision 09606165a0ab82291c6443d5c82b205256c56458)
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_frontend.h#2 $
32  * $FreeBSD$
33  */
34 /*
35  * CAM Target Layer front end registration hooks
36  *
37  * Author: Ken Merry <ken@FreeBSD.org>
38  */
39 
40 #ifndef	_CTL_FRONTEND_H_
41 #define	_CTL_FRONTEND_H_
42 
43 #include <cam/ctl/ctl_ioctl.h>
44 
45 typedef enum {
46 	CTL_PORT_STATUS_NONE		= 0x00,
47 	CTL_PORT_STATUS_ONLINE		= 0x01,
48 	CTL_PORT_STATUS_HA_SHARED	= 0x02
49 } ctl_port_status;
50 
51 typedef int (*fe_init_t)(void);
52 typedef void (*fe_shutdown_t)(void);
53 typedef void (*port_func_t)(void *onoff_arg);
54 typedef int (*port_info_func_t)(void *onoff_arg, struct sbuf *sb);
55 typedef	int (*lun_func_t)(void *arg, int lun_id);
56 typedef int (*fe_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
57 			  struct thread *td);
58 
59 #define CTL_FRONTEND_DECLARE(name, driver) \
60 	static int name ## _modevent(module_t mod, int type, void *data) \
61 	{ \
62 		switch (type) { \
63 		case MOD_LOAD: \
64 			ctl_frontend_register( \
65 				(struct ctl_frontend *)data); \
66 			break; \
67 		case MOD_UNLOAD: \
68 			printf(#name " module unload - not possible for this module type\n"); \
69 			return EINVAL; \
70 		default: \
71 			return EOPNOTSUPP; \
72 		} \
73 		return 0; \
74 	} \
75 	static moduledata_t name ## _mod = { \
76 		#name, \
77 		name ## _modevent, \
78 		(void *)&driver \
79 	}; \
80 	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
81 	MODULE_DEPEND(name, ctl, 1, 1, 1); \
82 	MODULE_DEPEND(name, cam, 1, 1, 1)
83 
84 struct ctl_wwpn_iid {
85 	int in_use;
86 	time_t last_use;
87 	uint64_t wwpn;
88 	char *name;
89 };
90 
91 /*
92  * The ctl_frontend structure is the registration mechanism between a FETD
93  * (Front End Target Driver) and the CTL layer.  Here is a description of
94  * the fields:
95  *
96  * port_type:		  This field tells CTL what kind of front end it is
97  *                        dealing with.  This field serves two purposes.
98  *                        The first is to let CTL know whether the frontend
99  *                        in question is inside the main CTL module (i.e.
100  *                        the ioctl front end), and therefore its module
101  *                        reference count shouldn't be incremented.  The
102  *                        CTL ioctl front end should continue to use the
103  *                        CTL_PORT_IOCTL argument as long as it is part of
104  *                        the main CTL module.  The second is to let CTL
105  *                        know what kind of front end it is dealing with, so
106  *                        it can return the proper inquiry data for that
107  *                        particular port.
108  *
109  * num_requested_ctl_io:  This is the number of ctl_io structures that the
110  *			  front end needs for its pool.  This should
111  * 			  generally be the maximum number of outstanding
112  *			  transactions that the FETD can handle.  The CTL
113  *			  layer will add a few to this to account for
114  *			  ctl_io buffers queued for pending sense data.
115  *			  (Pending sense only gets queued if the FETD
116  * 			  doesn't support autosense.  e.g. non-packetized
117  * 			  parallel SCSI doesn't support autosense.)
118  *
119  * port_name:		  A string describing the FETD.  e.g. "LSI 1030T U320"
120  *			  or whatever you want to use to describe the driver.
121  *
122  *
123  * physical_port:	  This is the physical port number of this
124  * 			  particular port within the driver/hardware.  This
125  * 			  number is hardware/driver specific.
126  * virtual_port:	  This is the virtual port number of this
127  * 			  particular port.  This is for things like NP-IV.
128  *
129  * port_online():	  This function is called, with onoff_arg as its
130  *			  argument, by the CTL layer when it wants the FETD
131  *			  to start responding to selections on the specified
132  * 			  target ID.
133  *
134  * port_offline():	  This function is called, with onoff_arg as its
135  *			  argument, by the CTL layer when it wants the FETD
136  * 			  to stop responding to selection on the specified
137  * 			  target ID.
138  *
139  * onoff_arg:		  This is supplied as an argument to port_online()
140  *			  and port_offline().  This is specified by the
141  *			  FETD.
142  *
143  * lun_enable():	  This function is called, with targ_lun_arg, a target
144  *			  ID and a LUN ID as its arguments, by CTL when it
145  *			  wants the FETD to enable a particular LUN.  If the
146  *			  FETD doesn't really know about LUNs, it should
147  *			  just ignore this call and return 0.  If the FETD
148  *			  cannot enable the requested LUN for some reason, the
149  *			  FETD should return non-zero status.
150  *
151  * lun_disable():	  This function is called, with targ_lun_arg, a target
152  *			  ID and LUN ID as its arguments, by CTL when it
153  *			  wants the FETD to disable a particular LUN.  If the
154  *			  FETD doesn't really know about LUNs, it should just
155  *			  ignore this call and return 0.  If the FETD cannot
156  *			  disable the requested LUN for some reason, the
157  *			  FETD should return non-zero status.
158  *
159  * targ_lun_arg:	  This is supplied as an argument to the targ/lun
160  *			  enable/disable() functions.  This is specified by
161  *			  the FETD.
162  *
163  * fe_datamove():	  This function is called one or more times per I/O
164  *			  by the CTL layer to tell the FETD to initiate a
165  *			  DMA to or from the data buffer(s) specified by
166  * 			  the passed-in ctl_io structure.
167  *
168  * fe_done():	  	  This function is called by the CTL layer when a
169  *			  particular SCSI I/O or task management command has
170  * 			  completed.  For SCSI I/O requests (CTL_IO_SCSI),
171  *			  sense data is always supplied if the status is
172  *			  CTL_SCSI_ERROR and the SCSI status byte is
173  *			  SCSI_STATUS_CHECK_COND.  If the FETD doesn't
174  *			  support autosense, the sense should be queued
175  *			  back to the CTL layer via ctl_queue_sense().
176  *
177  * fe_dump():		  This function, if it exists, is called by CTL
178  *			  to request a dump of any debugging information or
179  *			  state to the console.
180  *
181  * max_targets:		  The maximum number of targets that we can create
182  *			  per-port.
183  *
184  * max_target_id:	  The highest target ID that we can use.
185  *
186  * targ_port:		  The CTL layer assigns a "port number" to every
187  *			  FETD.  This port number should be passed back in
188  *			  in the header of every ctl_io that is queued to
189  *			  the CTL layer.  This enables us to determine
190  *			  which bus the command came in on.
191  *
192  * ctl_pool_ref:	  Memory pool reference used by the FETD in calls to
193  * 			  ctl_alloc_io().
194  *
195  * max_initiators:	  Maximum number of initiators that the FETD is
196  *			  allowed to have.  Initiators should be numbered
197  *			  from 0 to max_initiators - 1.  This value will
198  * 			  typically be 16, and thus not a problem for
199  * 			  parallel SCSI.  This may present issues for Fibre
200  *			  Channel.
201  *
202  * wwnn			  World Wide Node Name to be used by the FETD.
203  *			  Note that this is set *after* registration.  It
204  * 			  will be set prior to the online function getting
205  * 			  called.
206  *
207  * wwpn			  World Wide Port Name to be used by the FETD.
208  *			  Note that this is set *after* registration.  It
209  * 			  will be set prior to the online function getting
210  * 			  called.
211  *
212  * status:		  Used by CTL to keep track of per-FETD state.
213  *
214  * links:		  Linked list pointers, used by CTL.  The FETD
215  *			  shouldn't touch this field.
216  */
217 struct ctl_port {
218 	struct ctl_softc *ctl_softc;
219 	struct ctl_frontend *frontend;
220 	ctl_port_type	port_type;		/* passed to CTL */
221 	int		num_requested_ctl_io;	/* passed to CTL */
222 	char		*port_name;		/* passed to CTL */
223 	int		physical_port;		/* passed to CTL */
224 	int		virtual_port;		/* passed to CTL */
225 	port_func_t	port_online;		/* passed to CTL */
226 	port_func_t	port_offline;		/* passed to CTL */
227 	port_info_func_t port_info;		/* passed to CTL */
228 	void		*onoff_arg;		/* passed to CTL */
229 	lun_func_t	lun_enable;		/* passed to CTL */
230 	lun_func_t	lun_disable;		/* passed to CTL */
231 	int		lun_map_size;		/* passed to CTL */
232 	uint32_t	*lun_map;		/* passed to CTL */
233 	void		*targ_lun_arg;		/* passed to CTL */
234 	void		(*fe_datamove)(union ctl_io *io); /* passed to CTL */
235 	void		(*fe_done)(union ctl_io *io); /* passed to CTL */
236 	int		max_targets;		/* passed to CTL */
237 	int		max_target_id;		/* passed to CTL */
238 	int32_t		targ_port;		/* passed back to FETD */
239 	void		*ctl_pool_ref;		/* passed back to FETD */
240 	uint32_t	max_initiators;		/* passed back to FETD */
241 	struct ctl_wwpn_iid *wwpn_iid;		/* used by CTL */
242 	uint64_t	wwnn;			/* set by CTL before online */
243 	uint64_t	wwpn;			/* set by CTL before online */
244 	ctl_port_status	status;			/* used by CTL */
245 	ctl_options_t	options;		/* passed to CTL */
246 	struct ctl_devid *port_devid;		/* passed to CTL */
247 	struct ctl_devid *target_devid;		/* passed to CTL */
248 	struct ctl_devid *init_devid;		/* passed to CTL */
249 	struct ctl_io_stats stats;		/* used by CTL */
250 	struct mtx	port_lock;		/* used by CTL */
251 	STAILQ_ENTRY(ctl_port) fe_links;	/* used by CTL */
252 	STAILQ_ENTRY(ctl_port) links;		/* used by CTL */
253 };
254 
255 struct ctl_frontend {
256 	char		name[CTL_DRIVER_NAME_LEN];	/* passed to CTL */
257 	fe_init_t	init;			/* passed to CTL */
258 	fe_ioctl_t	ioctl;			/* passed to CTL */
259 	void		(*fe_dump)(void);	/* passed to CTL */
260 	fe_shutdown_t	shutdown;		/* passed to CTL */
261 	STAILQ_HEAD(, ctl_port) port_list;	/* used by CTL */
262 	STAILQ_ENTRY(ctl_frontend) links;	/* used by CTL */
263 };
264 
265 /*
266  * This may block until resources are allocated.  Called at FETD module load
267  * time. Returns 0 for success, non-zero for failure.
268  */
269 int ctl_frontend_register(struct ctl_frontend *fe);
270 
271 /*
272  * Called at FETD module unload time.
273  * Returns 0 for success, non-zero for failure.
274  */
275 int ctl_frontend_deregister(struct ctl_frontend *fe);
276 
277 /*
278  * Find the frontend by its name. Returns NULL if not found.
279  */
280 struct ctl_frontend * ctl_frontend_find(char *frontend_name);
281 
282 /*
283  * This may block until resources are allocated.  Called at FETD module load
284  * time. Returns 0 for success, non-zero for failure.
285  */
286 int ctl_port_register(struct ctl_port *port);
287 
288 /*
289  * Called at FETD module unload time.
290  * Returns 0 for success, non-zero for failure.
291  */
292 int ctl_port_deregister(struct ctl_port *port);
293 
294 /*
295  * Called to set the WWNN and WWPN for a particular frontend.
296  */
297 void ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid,
298 			   uint64_t wwnn, int wwpn_valid, uint64_t wwpn);
299 
300 /*
301  * Called to bring a particular frontend online.
302  */
303 void ctl_port_online(struct ctl_port *fe);
304 
305 /*
306  * Called to take a particular frontend offline.
307  */
308 void ctl_port_offline(struct ctl_port *fe);
309 
310 /*
311  * This routine queues I/O and task management requests from the FETD to the
312  * CTL layer.  Returns immediately.  Returns 0 for success, non-zero for
313  * failure.
314  */
315 int ctl_queue(union ctl_io *io);
316 
317 /*
318  * This routine is used if the front end interface doesn't support
319  * autosense (e.g. non-packetized parallel SCSI).  This will queue the
320  * scsiio structure back to a per-lun pending sense queue.  This MUST be
321  * called BEFORE any request sense can get queued to the CTL layer -- I
322  * need it in the queue in order to service the request.  The scsiio
323  * structure passed in here will be freed by the CTL layer when sense is
324  * retrieved by the initiator.  Returns 0 for success, non-zero for failure.
325  */
326 int ctl_queue_sense(union ctl_io *io);
327 
328 /*
329  * This routine adds an initiator to CTL's port database.
330  * The iid field should be the same as the iid passed in the nexus of each
331  * ctl_io from this initiator.
332  * The WWPN should be the FC WWPN, if available.
333  */
334 int ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name);
335 
336 /*
337  * This routine will remove an initiator from CTL's port database.
338  * The iid field should be the same as the iid passed in the nexus of each
339  * ctl_io from this initiator.
340  */
341 int ctl_remove_initiator(struct ctl_port *port, int iid);
342 
343 #endif	/* _CTL_FRONTEND_H_ */
344