1229997Sken/*-
2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp.
3312841Smav * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
4229997Sken * All rights reserved.
5229997Sken *
6229997Sken * Redistribution and use in source and binary forms, with or without
7229997Sken * modification, are permitted provided that the following conditions
8229997Sken * are met:
9229997Sken * 1. Redistributions of source code must retain the above copyright
10229997Sken *    notice, this list of conditions, and the following disclaimer,
11229997Sken *    without modification.
12229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
14229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
15229997Sken *    including a substantially similar Disclaimer requirement for further
16229997Sken *    binary redistribution.
17229997Sken *
18229997Sken * NO WARRANTY
19229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29229997Sken * POSSIBILITY OF SUCH DAMAGES.
30229997Sken *
31229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.h#2 $
32229997Sken * $FreeBSD: stable/10/sys/cam/ctl/ctl_frontend.h 313369 2017-02-07 01:56:26Z mav $
33229997Sken */
34229997Sken/*
35229997Sken * CAM Target Layer front end registration hooks
36229997Sken *
37229997Sken * Author: Ken Merry <ken@FreeBSD.org>
38229997Sken */
39229997Sken
40229997Sken#ifndef	_CTL_FRONTEND_H_
41229997Sken#define	_CTL_FRONTEND_H_
42229997Sken
43312841Smav#include <cam/ctl/ctl_ioctl.h>
44312841Smav
45229997Skentypedef enum {
46229997Sken	CTL_PORT_STATUS_NONE		= 0x00,
47291388Smav	CTL_PORT_STATUS_ONLINE		= 0x01,
48291388Smav	CTL_PORT_STATUS_HA_SHARED	= 0x02
49229997Sken} ctl_port_status;
50229997Sken
51268677Smavtypedef int (*fe_init_t)(void);
52313369Smavtypedef int (*fe_shutdown_t)(void);
53229997Skentypedef void (*port_func_t)(void *onoff_arg);
54268688Smavtypedef int (*port_info_func_t)(void *onoff_arg, struct sbuf *sb);
55284798Smavtypedef	int (*lun_func_t)(void *arg, int lun_id);
56254759Strasztypedef int (*fe_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
57254759Strasz			  struct thread *td);
58229997Sken
59268677Smav#define CTL_FRONTEND_DECLARE(name, driver) \
60268677Smav	static int name ## _modevent(module_t mod, int type, void *data) \
61268677Smav	{ \
62268677Smav		switch (type) { \
63268677Smav		case MOD_LOAD: \
64313369Smav			return (ctl_frontend_register( \
65313369Smav				(struct ctl_frontend *)data)); \
66268677Smav			break; \
67268677Smav		case MOD_UNLOAD: \
68313369Smav			return (ctl_frontend_deregister( \
69313369Smav				(struct ctl_frontend *)data)); \
70313369Smav			break; \
71268677Smav		default: \
72268677Smav			return EOPNOTSUPP; \
73268677Smav		} \
74268677Smav		return 0; \
75268677Smav	} \
76268677Smav	static moduledata_t name ## _mod = { \
77268677Smav		#name, \
78268677Smav		name ## _modevent, \
79268677Smav		(void *)&driver \
80268677Smav	}; \
81268677Smav	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
82268677Smav	MODULE_DEPEND(name, ctl, 1, 1, 1); \
83268677Smav	MODULE_DEPEND(name, cam, 1, 1, 1)
84268677Smav
85268692Smavstruct ctl_wwpn_iid {
86268692Smav	int in_use;
87268692Smav	time_t last_use;
88268692Smav	uint64_t wwpn;
89268692Smav	char *name;
90268692Smav};
91268692Smav
92229997Sken/*
93229997Sken * The ctl_frontend structure is the registration mechanism between a FETD
94229997Sken * (Front End Target Driver) and the CTL layer.  Here is a description of
95229997Sken * the fields:
96229997Sken *
97229997Sken * port_type:		  This field tells CTL what kind of front end it is
98229997Sken *                        dealing with.  This field serves two purposes.
99229997Sken *                        The first is to let CTL know whether the frontend
100229997Sken *                        in question is inside the main CTL module (i.e.
101229997Sken *                        the ioctl front end), and therefore its module
102229997Sken *                        reference count shouldn't be incremented.  The
103229997Sken *                        CTL ioctl front end should continue to use the
104229997Sken *                        CTL_PORT_IOCTL argument as long as it is part of
105229997Sken *                        the main CTL module.  The second is to let CTL
106229997Sken *                        know what kind of front end it is dealing with, so
107229997Sken *                        it can return the proper inquiry data for that
108229997Sken *                        particular port.
109229997Sken *
110229997Sken * num_requested_ctl_io:  This is the number of ctl_io structures that the
111229997Sken *			  front end needs for its pool.  This should
112229997Sken * 			  generally be the maximum number of outstanding
113229997Sken *			  transactions that the FETD can handle.  The CTL
114229997Sken *			  layer will add a few to this to account for
115229997Sken *			  ctl_io buffers queued for pending sense data.
116229997Sken *			  (Pending sense only gets queued if the FETD
117229997Sken * 			  doesn't support autosense.  e.g. non-packetized
118229997Sken * 			  parallel SCSI doesn't support autosense.)
119229997Sken *
120229997Sken * port_name:		  A string describing the FETD.  e.g. "LSI 1030T U320"
121229997Sken *			  or whatever you want to use to describe the driver.
122229997Sken *
123229997Sken *
124229997Sken * physical_port:	  This is the physical port number of this
125229997Sken * 			  particular port within the driver/hardware.  This
126229997Sken * 			  number is hardware/driver specific.
127229997Sken * virtual_port:	  This is the virtual port number of this
128229997Sken * 			  particular port.  This is for things like NP-IV.
129229997Sken *
130229997Sken * port_online():	  This function is called, with onoff_arg as its
131229997Sken *			  argument, by the CTL layer when it wants the FETD
132229997Sken *			  to start responding to selections on the specified
133288731Smav * 			  target ID.
134229997Sken *
135229997Sken * port_offline():	  This function is called, with onoff_arg as its
136229997Sken *			  argument, by the CTL layer when it wants the FETD
137229997Sken * 			  to stop responding to selection on the specified
138288731Smav * 			  target ID.
139229997Sken *
140229997Sken * onoff_arg:		  This is supplied as an argument to port_online()
141229997Sken *			  and port_offline().  This is specified by the
142229997Sken *			  FETD.
143229997Sken *
144229997Sken * lun_enable():	  This function is called, with targ_lun_arg, a target
145229997Sken *			  ID and a LUN ID as its arguments, by CTL when it
146229997Sken *			  wants the FETD to enable a particular LUN.  If the
147229997Sken *			  FETD doesn't really know about LUNs, it should
148229997Sken *			  just ignore this call and return 0.  If the FETD
149229997Sken *			  cannot enable the requested LUN for some reason, the
150229997Sken *			  FETD should return non-zero status.
151229997Sken *
152229997Sken * lun_disable():	  This function is called, with targ_lun_arg, a target
153229997Sken *			  ID and LUN ID as its arguments, by CTL when it
154229997Sken *			  wants the FETD to disable a particular LUN.  If the
155229997Sken *			  FETD doesn't really know about LUNs, it should just
156229997Sken *			  ignore this call and return 0.  If the FETD cannot
157229997Sken *			  disable the requested LUN for some reason, the
158229997Sken *			  FETD should return non-zero status.
159229997Sken *
160229997Sken * targ_lun_arg:	  This is supplied as an argument to the targ/lun
161229997Sken *			  enable/disable() functions.  This is specified by
162229997Sken *			  the FETD.
163229997Sken *
164229997Sken * fe_datamove():	  This function is called one or more times per I/O
165229997Sken *			  by the CTL layer to tell the FETD to initiate a
166229997Sken *			  DMA to or from the data buffer(s) specified by
167229997Sken * 			  the passed-in ctl_io structure.
168229997Sken *
169229997Sken * fe_done():	  	  This function is called by the CTL layer when a
170229997Sken *			  particular SCSI I/O or task management command has
171229997Sken * 			  completed.  For SCSI I/O requests (CTL_IO_SCSI),
172229997Sken *			  sense data is always supplied if the status is
173229997Sken *			  CTL_SCSI_ERROR and the SCSI status byte is
174229997Sken *			  SCSI_STATUS_CHECK_COND.  If the FETD doesn't
175229997Sken *			  support autosense, the sense should be queued
176229997Sken *			  back to the CTL layer via ctl_queue_sense().
177229997Sken *
178229997Sken * fe_dump():		  This function, if it exists, is called by CTL
179229997Sken *			  to request a dump of any debugging information or
180229997Sken *			  state to the console.
181229997Sken *
182229997Sken * max_targets:		  The maximum number of targets that we can create
183229997Sken *			  per-port.
184229997Sken *
185229997Sken * max_target_id:	  The highest target ID that we can use.
186229997Sken *
187229997Sken * targ_port:		  The CTL layer assigns a "port number" to every
188229997Sken *			  FETD.  This port number should be passed back in
189229997Sken *			  in the header of every ctl_io that is queued to
190229997Sken *			  the CTL layer.  This enables us to determine
191229997Sken *			  which bus the command came in on.
192229997Sken *
193229997Sken * ctl_pool_ref:	  Memory pool reference used by the FETD in calls to
194229997Sken * 			  ctl_alloc_io().
195229997Sken *
196229997Sken * max_initiators:	  Maximum number of initiators that the FETD is
197229997Sken *			  allowed to have.  Initiators should be numbered
198229997Sken *			  from 0 to max_initiators - 1.  This value will
199229997Sken * 			  typically be 16, and thus not a problem for
200229997Sken * 			  parallel SCSI.  This may present issues for Fibre
201229997Sken *			  Channel.
202229997Sken *
203229997Sken * wwnn			  World Wide Node Name to be used by the FETD.
204229997Sken *			  Note that this is set *after* registration.  It
205229997Sken * 			  will be set prior to the online function getting
206229997Sken * 			  called.
207229997Sken *
208229997Sken * wwpn			  World Wide Port Name to be used by the FETD.
209229997Sken *			  Note that this is set *after* registration.  It
210229997Sken * 			  will be set prior to the online function getting
211229997Sken * 			  called.
212229997Sken *
213229997Sken * status:		  Used by CTL to keep track of per-FETD state.
214229997Sken *
215229997Sken * links:		  Linked list pointers, used by CTL.  The FETD
216229997Sken *			  shouldn't touch this field.
217229997Sken */
218268677Smavstruct ctl_port {
219288795Smav	struct ctl_softc *ctl_softc;
220268677Smav	struct ctl_frontend *frontend;
221229997Sken	ctl_port_type	port_type;		/* passed to CTL */
222229997Sken	int		num_requested_ctl_io;	/* passed to CTL */
223229997Sken	char		*port_name;		/* passed to CTL */
224229997Sken	int		physical_port;		/* passed to CTL */
225229997Sken	int		virtual_port;		/* passed to CTL */
226229997Sken	port_func_t	port_online;		/* passed to CTL */
227229997Sken	port_func_t	port_offline;		/* passed to CTL */
228268688Smav	port_info_func_t port_info;		/* passed to CTL */
229229997Sken	void		*onoff_arg;		/* passed to CTL */
230229997Sken	lun_func_t	lun_enable;		/* passed to CTL */
231229997Sken	lun_func_t	lun_disable;		/* passed to CTL */
232312577Smav	int		lun_map_size;		/* passed to CTL */
233279002Smav	uint32_t	*lun_map;		/* passed to CTL */
234229997Sken	void		*targ_lun_arg;		/* passed to CTL */
235229997Sken	void		(*fe_datamove)(union ctl_io *io); /* passed to CTL */
236229997Sken	void		(*fe_done)(union ctl_io *io); /* passed to CTL */
237229997Sken	int		max_targets;		/* passed to CTL */
238229997Sken	int		max_target_id;		/* passed to CTL */
239229997Sken	int32_t		targ_port;		/* passed back to FETD */
240229997Sken	void		*ctl_pool_ref;		/* passed back to FETD */
241229997Sken	uint32_t	max_initiators;		/* passed back to FETD */
242268692Smav	struct ctl_wwpn_iid *wwpn_iid;		/* used by CTL */
243229997Sken	uint64_t	wwnn;			/* set by CTL before online */
244229997Sken	uint64_t	wwpn;			/* set by CTL before online */
245229997Sken	ctl_port_status	status;			/* used by CTL */
246268682Smav	ctl_options_t	options;		/* passed to CTL */
247268683Smav	struct ctl_devid *port_devid;		/* passed to CTL */
248268683Smav	struct ctl_devid *target_devid;		/* passed to CTL */
249269296Smav	struct ctl_devid *init_devid;		/* passed to CTL */
250312841Smav	struct ctl_io_stats stats;		/* used by CTL */
251312841Smav	struct mtx	port_lock;		/* used by CTL */
252268677Smav	STAILQ_ENTRY(ctl_port) fe_links;	/* used by CTL */
253268677Smav	STAILQ_ENTRY(ctl_port) links;		/* used by CTL */
254268677Smav};
255268677Smav
256268677Smavstruct ctl_frontend {
257268682Smav	char		name[CTL_DRIVER_NAME_LEN];	/* passed to CTL */
258268677Smav	fe_init_t	init;			/* passed to CTL */
259268677Smav	fe_ioctl_t	ioctl;			/* passed to CTL */
260268677Smav	void		(*fe_dump)(void);	/* passed to CTL */
261268677Smav	fe_shutdown_t	shutdown;		/* passed to CTL */
262268677Smav	STAILQ_HEAD(, ctl_port) port_list;	/* used by CTL */
263229997Sken	STAILQ_ENTRY(ctl_frontend) links;	/* used by CTL */
264229997Sken};
265229997Sken
266229997Sken/*
267229997Sken * This may block until resources are allocated.  Called at FETD module load
268229997Sken * time. Returns 0 for success, non-zero for failure.
269229997Sken */
270268677Smavint ctl_frontend_register(struct ctl_frontend *fe);
271229997Sken
272229997Sken/*
273229997Sken * Called at FETD module unload time.
274229997Sken * Returns 0 for success, non-zero for failure.
275229997Sken */
276229997Skenint ctl_frontend_deregister(struct ctl_frontend *fe);
277229997Sken
278229997Sken/*
279268680Smav * Find the frontend by its name. Returns NULL if not found.
280268680Smav */
281268680Smavstruct ctl_frontend * ctl_frontend_find(char *frontend_name);
282268680Smav
283268680Smav/*
284268677Smav * This may block until resources are allocated.  Called at FETD module load
285268677Smav * time. Returns 0 for success, non-zero for failure.
286268677Smav */
287275493Smavint ctl_port_register(struct ctl_port *port);
288268677Smav
289268677Smav/*
290268677Smav * Called at FETD module unload time.
291268677Smav * Returns 0 for success, non-zero for failure.
292268677Smav */
293268692Smavint ctl_port_deregister(struct ctl_port *port);
294268677Smav
295268677Smav/*
296229997Sken * Called to set the WWNN and WWPN for a particular frontend.
297229997Sken */
298268677Smavvoid ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid,
299229997Sken			   uint64_t wwnn, int wwpn_valid, uint64_t wwpn);
300229997Sken
301229997Sken/*
302229997Sken * Called to bring a particular frontend online.
303229997Sken */
304268677Smavvoid ctl_port_online(struct ctl_port *fe);
305229997Sken
306229997Sken/*
307229997Sken * Called to take a particular frontend offline.
308229997Sken */
309268677Smavvoid ctl_port_offline(struct ctl_port *fe);
310229997Sken
311229997Sken/*
312229997Sken * This routine queues I/O and task management requests from the FETD to the
313229997Sken * CTL layer.  Returns immediately.  Returns 0 for success, non-zero for
314229997Sken * failure.
315229997Sken */
316229997Skenint ctl_queue(union ctl_io *io);
317229997Sken
318229997Sken/*
319229997Sken * This routine is used if the front end interface doesn't support
320229997Sken * autosense (e.g. non-packetized parallel SCSI).  This will queue the
321229997Sken * scsiio structure back to a per-lun pending sense queue.  This MUST be
322229997Sken * called BEFORE any request sense can get queued to the CTL layer -- I
323229997Sken * need it in the queue in order to service the request.  The scsiio
324229997Sken * structure passed in here will be freed by the CTL layer when sense is
325229997Sken * retrieved by the initiator.  Returns 0 for success, non-zero for failure.
326229997Sken */
327229997Skenint ctl_queue_sense(union ctl_io *io);
328229997Sken
329229997Sken/*
330268692Smav * This routine adds an initiator to CTL's port database.
331229997Sken * The iid field should be the same as the iid passed in the nexus of each
332229997Sken * ctl_io from this initiator.
333268692Smav * The WWPN should be the FC WWPN, if available.
334229997Sken */
335268692Smavint ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name);
336229997Sken
337229997Sken/*
338268692Smav * This routine will remove an initiator from CTL's port database.
339268692Smav * The iid field should be the same as the iid passed in the nexus of each
340268692Smav * ctl_io from this initiator.
341229997Sken */
342268692Smavint ctl_remove_initiator(struct ctl_port *port, int iid);
343229997Sken
344229997Sken#endif	/* _CTL_FRONTEND_H_ */
345