1229997Sken/*-
2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp.
3229997Sken * All rights reserved.
4229997Sken *
5229997Sken * Redistribution and use in source and binary forms, with or without
6229997Sken * modification, are permitted provided that the following conditions
7229997Sken * are met:
8229997Sken * 1. Redistributions of source code must retain the above copyright
9229997Sken *    notice, this list of conditions, and the following disclaimer,
10229997Sken *    without modification.
11229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
13229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
14229997Sken *    including a substantially similar Disclaimer requirement for further
15229997Sken *    binary redistribution.
16229997Sken *
17229997Sken * NO WARRANTY
18229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28229997Sken * POSSIBILITY OF SUCH DAMAGES.
29229997Sken *
30229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.h#2 $
31229997Sken * $FreeBSD: releng/10.3/sys/cam/ctl/ctl_frontend.h 291388 2015-11-27 15:26:19Z mav $
32229997Sken */
33229997Sken/*
34229997Sken * CAM Target Layer front end registration hooks
35229997Sken *
36229997Sken * Author: Ken Merry <ken@FreeBSD.org>
37229997Sken */
38229997Sken
39229997Sken#ifndef	_CTL_FRONTEND_H_
40229997Sken#define	_CTL_FRONTEND_H_
41229997Sken
42229997Skentypedef enum {
43229997Sken	CTL_PORT_STATUS_NONE		= 0x00,
44291388Smav	CTL_PORT_STATUS_ONLINE		= 0x01,
45291388Smav	CTL_PORT_STATUS_HA_SHARED	= 0x02
46229997Sken} ctl_port_status;
47229997Sken
48268677Smavtypedef int (*fe_init_t)(void);
49268677Smavtypedef void (*fe_shutdown_t)(void);
50229997Skentypedef void (*port_func_t)(void *onoff_arg);
51268688Smavtypedef int (*port_info_func_t)(void *onoff_arg, struct sbuf *sb);
52284798Smavtypedef	int (*lun_func_t)(void *arg, int lun_id);
53254759Strasztypedef int (*fe_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
54254759Strasz			  struct thread *td);
55229997Sken
56268677Smav#define CTL_FRONTEND_DECLARE(name, driver) \
57268677Smav	static int name ## _modevent(module_t mod, int type, void *data) \
58268677Smav	{ \
59268677Smav		switch (type) { \
60268677Smav		case MOD_LOAD: \
61268677Smav			ctl_frontend_register( \
62268677Smav				(struct ctl_frontend *)data); \
63268677Smav			break; \
64268677Smav		case MOD_UNLOAD: \
65268677Smav			printf(#name " module unload - not possible for this module type\n"); \
66268677Smav			return EINVAL; \
67268677Smav		default: \
68268677Smav			return EOPNOTSUPP; \
69268677Smav		} \
70268677Smav		return 0; \
71268677Smav	} \
72268677Smav	static moduledata_t name ## _mod = { \
73268677Smav		#name, \
74268677Smav		name ## _modevent, \
75268677Smav		(void *)&driver \
76268677Smav	}; \
77268677Smav	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
78268677Smav	MODULE_DEPEND(name, ctl, 1, 1, 1); \
79268677Smav	MODULE_DEPEND(name, cam, 1, 1, 1)
80268677Smav
81268692Smavstruct ctl_wwpn_iid {
82268692Smav	int in_use;
83268692Smav	time_t last_use;
84268692Smav	uint64_t wwpn;
85268692Smav	char *name;
86268692Smav};
87268692Smav
88229997Sken/*
89229997Sken * The ctl_frontend structure is the registration mechanism between a FETD
90229997Sken * (Front End Target Driver) and the CTL layer.  Here is a description of
91229997Sken * the fields:
92229997Sken *
93229997Sken * port_type:		  This field tells CTL what kind of front end it is
94229997Sken *                        dealing with.  This field serves two purposes.
95229997Sken *                        The first is to let CTL know whether the frontend
96229997Sken *                        in question is inside the main CTL module (i.e.
97229997Sken *                        the ioctl front end), and therefore its module
98229997Sken *                        reference count shouldn't be incremented.  The
99229997Sken *                        CTL ioctl front end should continue to use the
100229997Sken *                        CTL_PORT_IOCTL argument as long as it is part of
101229997Sken *                        the main CTL module.  The second is to let CTL
102229997Sken *                        know what kind of front end it is dealing with, so
103229997Sken *                        it can return the proper inquiry data for that
104229997Sken *                        particular port.
105229997Sken *
106229997Sken * num_requested_ctl_io:  This is the number of ctl_io structures that the
107229997Sken *			  front end needs for its pool.  This should
108229997Sken * 			  generally be the maximum number of outstanding
109229997Sken *			  transactions that the FETD can handle.  The CTL
110229997Sken *			  layer will add a few to this to account for
111229997Sken *			  ctl_io buffers queued for pending sense data.
112229997Sken *			  (Pending sense only gets queued if the FETD
113229997Sken * 			  doesn't support autosense.  e.g. non-packetized
114229997Sken * 			  parallel SCSI doesn't support autosense.)
115229997Sken *
116229997Sken * port_name:		  A string describing the FETD.  e.g. "LSI 1030T U320"
117229997Sken *			  or whatever you want to use to describe the driver.
118229997Sken *
119229997Sken *
120229997Sken * physical_port:	  This is the physical port number of this
121229997Sken * 			  particular port within the driver/hardware.  This
122229997Sken * 			  number is hardware/driver specific.
123229997Sken * virtual_port:	  This is the virtual port number of this
124229997Sken * 			  particular port.  This is for things like NP-IV.
125229997Sken *
126229997Sken * port_online():	  This function is called, with onoff_arg as its
127229997Sken *			  argument, by the CTL layer when it wants the FETD
128229997Sken *			  to start responding to selections on the specified
129288731Smav * 			  target ID.
130229997Sken *
131229997Sken * port_offline():	  This function is called, with onoff_arg as its
132229997Sken *			  argument, by the CTL layer when it wants the FETD
133229997Sken * 			  to stop responding to selection on the specified
134288731Smav * 			  target ID.
135229997Sken *
136229997Sken * onoff_arg:		  This is supplied as an argument to port_online()
137229997Sken *			  and port_offline().  This is specified by the
138229997Sken *			  FETD.
139229997Sken *
140229997Sken * lun_enable():	  This function is called, with targ_lun_arg, a target
141229997Sken *			  ID and a LUN ID as its arguments, by CTL when it
142229997Sken *			  wants the FETD to enable a particular LUN.  If the
143229997Sken *			  FETD doesn't really know about LUNs, it should
144229997Sken *			  just ignore this call and return 0.  If the FETD
145229997Sken *			  cannot enable the requested LUN for some reason, the
146229997Sken *			  FETD should return non-zero status.
147229997Sken *
148229997Sken * lun_disable():	  This function is called, with targ_lun_arg, a target
149229997Sken *			  ID and LUN ID as its arguments, by CTL when it
150229997Sken *			  wants the FETD to disable a particular LUN.  If the
151229997Sken *			  FETD doesn't really know about LUNs, it should just
152229997Sken *			  ignore this call and return 0.  If the FETD cannot
153229997Sken *			  disable the requested LUN for some reason, the
154229997Sken *			  FETD should return non-zero status.
155229997Sken *
156229997Sken * targ_lun_arg:	  This is supplied as an argument to the targ/lun
157229997Sken *			  enable/disable() functions.  This is specified by
158229997Sken *			  the FETD.
159229997Sken *
160229997Sken * fe_datamove():	  This function is called one or more times per I/O
161229997Sken *			  by the CTL layer to tell the FETD to initiate a
162229997Sken *			  DMA to or from the data buffer(s) specified by
163229997Sken * 			  the passed-in ctl_io structure.
164229997Sken *
165229997Sken * fe_done():	  	  This function is called by the CTL layer when a
166229997Sken *			  particular SCSI I/O or task management command has
167229997Sken * 			  completed.  For SCSI I/O requests (CTL_IO_SCSI),
168229997Sken *			  sense data is always supplied if the status is
169229997Sken *			  CTL_SCSI_ERROR and the SCSI status byte is
170229997Sken *			  SCSI_STATUS_CHECK_COND.  If the FETD doesn't
171229997Sken *			  support autosense, the sense should be queued
172229997Sken *			  back to the CTL layer via ctl_queue_sense().
173229997Sken *
174229997Sken * fe_dump():		  This function, if it exists, is called by CTL
175229997Sken *			  to request a dump of any debugging information or
176229997Sken *			  state to the console.
177229997Sken *
178229997Sken * max_targets:		  The maximum number of targets that we can create
179229997Sken *			  per-port.
180229997Sken *
181229997Sken * max_target_id:	  The highest target ID that we can use.
182229997Sken *
183229997Sken * targ_port:		  The CTL layer assigns a "port number" to every
184229997Sken *			  FETD.  This port number should be passed back in
185229997Sken *			  in the header of every ctl_io that is queued to
186229997Sken *			  the CTL layer.  This enables us to determine
187229997Sken *			  which bus the command came in on.
188229997Sken *
189229997Sken * ctl_pool_ref:	  Memory pool reference used by the FETD in calls to
190229997Sken * 			  ctl_alloc_io().
191229997Sken *
192229997Sken * max_initiators:	  Maximum number of initiators that the FETD is
193229997Sken *			  allowed to have.  Initiators should be numbered
194229997Sken *			  from 0 to max_initiators - 1.  This value will
195229997Sken * 			  typically be 16, and thus not a problem for
196229997Sken * 			  parallel SCSI.  This may present issues for Fibre
197229997Sken *			  Channel.
198229997Sken *
199229997Sken * wwnn			  World Wide Node Name to be used by the FETD.
200229997Sken *			  Note that this is set *after* registration.  It
201229997Sken * 			  will be set prior to the online function getting
202229997Sken * 			  called.
203229997Sken *
204229997Sken * wwpn			  World Wide Port Name to be used by the FETD.
205229997Sken *			  Note that this is set *after* registration.  It
206229997Sken * 			  will be set prior to the online function getting
207229997Sken * 			  called.
208229997Sken *
209229997Sken * status:		  Used by CTL to keep track of per-FETD state.
210229997Sken *
211229997Sken * links:		  Linked list pointers, used by CTL.  The FETD
212229997Sken *			  shouldn't touch this field.
213229997Sken */
214268677Smavstruct ctl_port {
215288795Smav	struct ctl_softc *ctl_softc;
216268677Smav	struct ctl_frontend *frontend;
217229997Sken	ctl_port_type	port_type;		/* passed to CTL */
218229997Sken	int		num_requested_ctl_io;	/* passed to CTL */
219229997Sken	char		*port_name;		/* passed to CTL */
220229997Sken	int		physical_port;		/* passed to CTL */
221229997Sken	int		virtual_port;		/* passed to CTL */
222229997Sken	port_func_t	port_online;		/* passed to CTL */
223229997Sken	port_func_t	port_offline;		/* passed to CTL */
224268688Smav	port_info_func_t port_info;		/* passed to CTL */
225229997Sken	void		*onoff_arg;		/* passed to CTL */
226229997Sken	lun_func_t	lun_enable;		/* passed to CTL */
227229997Sken	lun_func_t	lun_disable;		/* passed to CTL */
228279002Smav	uint32_t	*lun_map;		/* passed to CTL */
229229997Sken	void		*targ_lun_arg;		/* passed to CTL */
230229997Sken	void		(*fe_datamove)(union ctl_io *io); /* passed to CTL */
231229997Sken	void		(*fe_done)(union ctl_io *io); /* passed to CTL */
232229997Sken	int		max_targets;		/* passed to CTL */
233229997Sken	int		max_target_id;		/* passed to CTL */
234229997Sken	int32_t		targ_port;		/* passed back to FETD */
235229997Sken	void		*ctl_pool_ref;		/* passed back to FETD */
236229997Sken	uint32_t	max_initiators;		/* passed back to FETD */
237268692Smav	struct ctl_wwpn_iid *wwpn_iid;		/* used by CTL */
238229997Sken	uint64_t	wwnn;			/* set by CTL before online */
239229997Sken	uint64_t	wwpn;			/* set by CTL before online */
240229997Sken	ctl_port_status	status;			/* used by CTL */
241268682Smav	ctl_options_t	options;		/* passed to CTL */
242268683Smav	struct ctl_devid *port_devid;		/* passed to CTL */
243268683Smav	struct ctl_devid *target_devid;		/* passed to CTL */
244269296Smav	struct ctl_devid *init_devid;		/* passed to CTL */
245268677Smav	STAILQ_ENTRY(ctl_port) fe_links;	/* used by CTL */
246268677Smav	STAILQ_ENTRY(ctl_port) links;		/* used by CTL */
247268677Smav};
248268677Smav
249268677Smavstruct ctl_frontend {
250268682Smav	char		name[CTL_DRIVER_NAME_LEN];	/* passed to CTL */
251268677Smav	fe_init_t	init;			/* passed to CTL */
252268677Smav	fe_ioctl_t	ioctl;			/* passed to CTL */
253268677Smav	void		(*fe_dump)(void);	/* passed to CTL */
254268677Smav	fe_shutdown_t	shutdown;		/* passed to CTL */
255268677Smav	STAILQ_HEAD(, ctl_port) port_list;	/* used by CTL */
256229997Sken	STAILQ_ENTRY(ctl_frontend) links;	/* used by CTL */
257229997Sken};
258229997Sken
259229997Sken/*
260229997Sken * This may block until resources are allocated.  Called at FETD module load
261229997Sken * time. Returns 0 for success, non-zero for failure.
262229997Sken */
263268677Smavint ctl_frontend_register(struct ctl_frontend *fe);
264229997Sken
265229997Sken/*
266229997Sken * Called at FETD module unload time.
267229997Sken * Returns 0 for success, non-zero for failure.
268229997Sken */
269229997Skenint ctl_frontend_deregister(struct ctl_frontend *fe);
270229997Sken
271229997Sken/*
272268680Smav * Find the frontend by its name. Returns NULL if not found.
273268680Smav */
274268680Smavstruct ctl_frontend * ctl_frontend_find(char *frontend_name);
275268680Smav
276268680Smav/*
277268677Smav * This may block until resources are allocated.  Called at FETD module load
278268677Smav * time. Returns 0 for success, non-zero for failure.
279268677Smav */
280275493Smavint ctl_port_register(struct ctl_port *port);
281268677Smav
282268677Smav/*
283268677Smav * Called at FETD module unload time.
284268677Smav * Returns 0 for success, non-zero for failure.
285268677Smav */
286268692Smavint ctl_port_deregister(struct ctl_port *port);
287268677Smav
288268677Smav/*
289229997Sken * Called to set the WWNN and WWPN for a particular frontend.
290229997Sken */
291268677Smavvoid ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid,
292229997Sken			   uint64_t wwnn, int wwpn_valid, uint64_t wwpn);
293229997Sken
294229997Sken/*
295229997Sken * Called to bring a particular frontend online.
296229997Sken */
297268677Smavvoid ctl_port_online(struct ctl_port *fe);
298229997Sken
299229997Sken/*
300229997Sken * Called to take a particular frontend offline.
301229997Sken */
302268677Smavvoid ctl_port_offline(struct ctl_port *fe);
303229997Sken
304229997Sken/*
305229997Sken * This routine queues I/O and task management requests from the FETD to the
306229997Sken * CTL layer.  Returns immediately.  Returns 0 for success, non-zero for
307229997Sken * failure.
308229997Sken */
309229997Skenint ctl_queue(union ctl_io *io);
310229997Sken
311229997Sken/*
312229997Sken * This routine is used if the front end interface doesn't support
313229997Sken * autosense (e.g. non-packetized parallel SCSI).  This will queue the
314229997Sken * scsiio structure back to a per-lun pending sense queue.  This MUST be
315229997Sken * called BEFORE any request sense can get queued to the CTL layer -- I
316229997Sken * need it in the queue in order to service the request.  The scsiio
317229997Sken * structure passed in here will be freed by the CTL layer when sense is
318229997Sken * retrieved by the initiator.  Returns 0 for success, non-zero for failure.
319229997Sken */
320229997Skenint ctl_queue_sense(union ctl_io *io);
321229997Sken
322229997Sken/*
323268692Smav * This routine adds an initiator to CTL's port database.
324229997Sken * The iid field should be the same as the iid passed in the nexus of each
325229997Sken * ctl_io from this initiator.
326268692Smav * The WWPN should be the FC WWPN, if available.
327229997Sken */
328268692Smavint ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name);
329229997Sken
330229997Sken/*
331268692Smav * This routine will remove an initiator from CTL's port database.
332268692Smav * The iid field should be the same as the iid passed in the nexus of each
333268692Smav * ctl_io from this initiator.
334229997Sken */
335268692Smavint ctl_remove_initiator(struct ctl_port *port, int iid);
336229997Sken
337229997Sken#endif	/* _CTL_FRONTEND_H_ */
338