ctl_frontend.c revision 268692
1247835Skib/*-
2247835Skib * Copyright (c) 2003 Silicon Graphics International Corp.
3247835Skib * All rights reserved.
4247835Skib *
5247835Skib * Redistribution and use in source and binary forms, with or without
6247835Skib * modification, are permitted provided that the following conditions
7247835Skib * are met:
8247835Skib * 1. Redistributions of source code must retain the above copyright
9247835Skib *    notice, this list of conditions, and the following disclaimer,
10247835Skib *    without modification.
11247835Skib * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12247835Skib *    substantially similar to the "NO WARRANTY" disclaimer below
13247835Skib *    ("Disclaimer") and any redistribution must be conditioned upon
14247835Skib *    including a substantially similar Disclaimer requirement for further
15247835Skib *    binary redistribution.
16247835Skib *
17247835Skib * NO WARRANTY
18247835Skib * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19247835Skib * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20247835Skib * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21247835Skib * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22247835Skib * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23247835Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24247835Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25247835Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26247835Skib * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27247835Skib * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28247835Skib * POSSIBILITY OF SUCH DAMAGES.
29247835Skib *
30247835Skib * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.c#4 $
31247835Skib */
32247835Skib/*
33247835Skib * CAM Target Layer front end interface code
34247835Skib *
35247835Skib * Author: Ken Merry <ken@FreeBSD.org>
36247835Skib */
37247835Skib
38247835Skib#include <sys/cdefs.h>
39247835Skib__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_frontend.c 268692 2014-07-15 17:14:53Z mav $");
40247835Skib
41247835Skib#include <sys/param.h>
42247835Skib#include <sys/systm.h>
43247835Skib#include <sys/kernel.h>
44247835Skib#include <sys/types.h>
45247835Skib#include <sys/malloc.h>
46247835Skib#include <sys/lock.h>
47247835Skib#include <sys/mutex.h>
48247835Skib#include <sys/condvar.h>
49247835Skib#include <sys/endian.h>
50247835Skib#include <sys/queue.h>
51247835Skib#include <sys/sysctl.h>
52247835Skib
53247835Skib#include <cam/scsi/scsi_all.h>
54247835Skib#include <cam/scsi/scsi_da.h>
55247835Skib#include <cam/ctl/ctl_io.h>
56247835Skib#include <cam/ctl/ctl.h>
57247835Skib#include <cam/ctl/ctl_frontend.h>
58247835Skib#include <cam/ctl/ctl_frontend_internal.h>
59247835Skib#include <cam/ctl/ctl_backend.h>
60247835Skib/* XXX KDM move defines from ctl_ioctl.h to somewhere else */
61247835Skib#include <cam/ctl/ctl_ioctl.h>
62247835Skib#include <cam/ctl/ctl_ha.h>
63247835Skib#include <cam/ctl/ctl_private.h>
64247835Skib#include <cam/ctl/ctl_debug.h>
65247835Skib
66247835Skibextern struct ctl_softc *control_softc;
67247835Skib
68247835Skibint
69247835Skibctl_frontend_register(struct ctl_frontend *fe)
70247835Skib{
71247835Skib	struct ctl_frontend *fe_tmp;
72247835Skib
73247835Skib	KASSERT(control_softc != NULL, ("CTL is not initialized"));
74247835Skib
75247835Skib	/*
76247835Skib	 * Sanity check, make sure this isn't a duplicate registration.
77247835Skib	 */
78247835Skib	mtx_lock(&control_softc->ctl_lock);
79247835Skib	STAILQ_FOREACH(fe_tmp, &control_softc->fe_list, links) {
80247835Skib		if (strcmp(fe_tmp->name, fe->name) == 0) {
81247835Skib			mtx_unlock(&control_softc->ctl_lock);
82247835Skib			return (-1);
83247835Skib		}
84247835Skib	}
85247835Skib	mtx_unlock(&control_softc->ctl_lock);
86247835Skib	STAILQ_INIT(&fe->port_list);
87247835Skib
88247835Skib	/*
89247835Skib	 * Call the frontend's initialization routine.
90247835Skib	 */
91247835Skib	if (fe->init != NULL)
92247835Skib		fe->init();
93247835Skib
94247835Skib	mtx_lock(&control_softc->ctl_lock);
95247835Skib	control_softc->num_frontends++;
96247835Skib	STAILQ_INSERT_TAIL(&control_softc->fe_list, fe, links);
97247835Skib	mtx_unlock(&control_softc->ctl_lock);
98247835Skib	return (0);
99247835Skib}
100247835Skib
101247835Skibint
102247835Skibctl_frontend_deregister(struct ctl_frontend *fe)
103247835Skib{
104247835Skib
105247835Skib	if (!STAILQ_EMPTY(&fe->port_list))
106247835Skib		return (-1);
107247835Skib
108247835Skib	mtx_lock(&control_softc->ctl_lock);
109247835Skib	STAILQ_REMOVE(&control_softc->fe_list, fe, ctl_frontend, links);
110247835Skib	control_softc->num_frontends--;
111247835Skib	mtx_unlock(&control_softc->ctl_lock);
112247835Skib
113247835Skib	/*
114247835Skib	 * Call the frontend's shutdown routine.
115247835Skib	 */
116247835Skib	if (fe->shutdown != NULL)
117247835Skib		fe->shutdown();
118247835Skib	return (0);
119247835Skib}
120247835Skib
121247835Skibstruct ctl_frontend *
122247835Skibctl_frontend_find(char *frontend_name)
123247835Skib{
124247835Skib	struct ctl_softc *ctl_softc = control_softc;
125247835Skib	struct ctl_frontend *fe;
126247835Skib
127247835Skib	mtx_lock(&ctl_softc->ctl_lock);
128247835Skib	STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
129247835Skib		if (strcmp(fe->name, frontend_name) == 0) {
130247835Skib			mtx_unlock(&ctl_softc->ctl_lock);
131247835Skib			return (fe);
132247835Skib		}
133247835Skib	}
134247835Skib	mtx_unlock(&ctl_softc->ctl_lock);
135247835Skib	return (NULL);
136247835Skib}
137247835Skib
138247835Skibint
139247835Skibctl_port_register(struct ctl_port *port, int master_shelf)
140247835Skib{
141247835Skib	struct ctl_io_pool *pool;
142247835Skib	int port_num;
143247835Skib	int retval;
144247835Skib
145247835Skib	retval = 0;
146247835Skib
147247835Skib	KASSERT(control_softc != NULL, ("CTL is not initialized"));
148247835Skib
149247835Skib	mtx_lock(&control_softc->ctl_lock);
150247835Skib	port_num = ctl_ffz(&control_softc->ctl_port_mask, CTL_MAX_PORTS);
151247835Skib	if ((port_num == -1)
152247835Skib	 || (ctl_set_mask(&control_softc->ctl_port_mask, port_num) == -1)) {
153247835Skib		port->targ_port = -1;
154247835Skib		mtx_unlock(&control_softc->ctl_lock);
155247835Skib		return (1);
156247835Skib	}
157247835Skib	control_softc->num_ports++;
158	mtx_unlock(&control_softc->ctl_lock);
159
160	/*
161	 * Initialize the initiator and portname mappings
162	 */
163	port->max_initiators = CTL_MAX_INIT_PER_PORT;
164	port->wwpn_iid = malloc(sizeof(*port->wwpn_iid) * port->max_initiators,
165	    M_CTL, M_NOWAIT | M_ZERO);
166	if (port->wwpn_iid == NULL) {
167		retval = ENOMEM;
168		goto error;
169	}
170
171	/*
172	 * We add 20 to whatever the caller requests, so he doesn't get
173	 * burned by queueing things back to the pending sense queue.  In
174	 * theory, there should probably only be one outstanding item, at
175	 * most, on the pending sense queue for a LUN.  We'll clear the
176	 * pending sense queue on the next command, whether or not it is
177	 * a REQUEST SENSE.
178	 */
179	retval = ctl_pool_create(control_softc, CTL_POOL_FETD,
180				 port->num_requested_ctl_io + 20, &pool);
181	if (retval != 0) {
182		free(port->wwpn_iid, M_CTL);
183error:
184		port->targ_port = -1;
185		mtx_lock(&control_softc->ctl_lock);
186		ctl_clear_mask(&control_softc->ctl_port_mask, port_num);
187		mtx_unlock(&control_softc->ctl_lock);
188		return (retval);
189	}
190	port->ctl_pool_ref = pool;
191
192	if (port->options.stqh_first == NULL)
193		STAILQ_INIT(&port->options);
194
195	mtx_lock(&control_softc->ctl_lock);
196	port->targ_port = port_num + (master_shelf != 0 ? 0 : CTL_MAX_PORTS);
197	STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
198	STAILQ_INSERT_TAIL(&control_softc->port_list, port, links);
199	control_softc->ctl_ports[port_num] = port;
200	mtx_unlock(&control_softc->ctl_lock);
201
202	return (retval);
203}
204
205int
206ctl_port_deregister(struct ctl_port *port)
207{
208	struct ctl_io_pool *pool;
209	int port_num, retval, i;
210
211	retval = 0;
212
213	pool = (struct ctl_io_pool *)port->ctl_pool_ref;
214
215	if (port->targ_port == -1) {
216		retval = 1;
217		goto bailout;
218	}
219
220	mtx_lock(&control_softc->ctl_lock);
221	STAILQ_REMOVE(&control_softc->port_list, port, ctl_port, links);
222	STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
223	control_softc->num_ports--;
224	port_num = (port->targ_port < CTL_MAX_PORTS) ? port->targ_port :
225	    port->targ_port - CTL_MAX_PORTS;
226	ctl_clear_mask(&control_softc->ctl_port_mask, port_num);
227	control_softc->ctl_ports[port_num] = NULL;
228	mtx_unlock(&control_softc->ctl_lock);
229
230	ctl_pool_free(pool);
231	ctl_free_opts(&port->options);
232
233	free(port->port_devid, M_CTL);
234	port->port_devid = NULL;
235	free(port->target_devid, M_CTL);
236	port->target_devid = NULL;
237	for (i = 0; i < port->max_initiators; i++)
238		free(port->wwpn_iid[i].name, M_CTL);
239	free(port->wwpn_iid, M_CTL);
240
241bailout:
242	return (retval);
243}
244
245void
246ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
247		      int wwpn_valid, uint64_t wwpn)
248{
249	struct scsi_vpd_id_descriptor *desc;
250	int len, proto;
251
252	if (port->port_type == CTL_PORT_FC)
253		proto = SCSI_PROTO_FC << 4;
254	else if (port->port_type == CTL_PORT_ISCSI)
255		proto = SCSI_PROTO_ISCSI << 4;
256	else
257		proto = SCSI_PROTO_SPI << 4;
258
259	if (wwnn_valid) {
260		port->wwnn = wwnn;
261
262		free(port->target_devid, M_CTL);
263
264		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
265		port->target_devid = malloc(sizeof(struct ctl_devid) + len,
266		    M_CTL, M_WAITOK | M_ZERO);
267		port->target_devid->len = len;
268		desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
269		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
270		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
271		    SVPD_ID_TYPE_NAA;
272		desc->length = CTL_WWPN_LEN;
273		scsi_u64to8b(port->wwnn, desc->identifier);
274	}
275
276	if (wwpn_valid) {
277		port->wwpn = wwpn;
278
279		free(port->port_devid, M_CTL);
280
281		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
282		port->port_devid = malloc(sizeof(struct ctl_devid) + len,
283		    M_CTL, M_WAITOK | M_ZERO);
284		port->port_devid->len = len;
285		desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
286		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
287		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
288		    SVPD_ID_TYPE_NAA;
289		desc->length = CTL_WWPN_LEN;
290		scsi_u64to8b(port->wwpn, desc->identifier);
291	}
292}
293
294void
295ctl_port_online(struct ctl_port *port)
296{
297	port->port_online(port->onoff_arg);
298	/* XXX KDM need a lock here? */
299	port->status |= CTL_PORT_STATUS_ONLINE;
300}
301
302void
303ctl_port_offline(struct ctl_port *port)
304{
305	port->port_offline(port->onoff_arg);
306	/* XXX KDM need a lock here? */
307	port->status &= ~CTL_PORT_STATUS_ONLINE;
308}
309
310/*
311 * vim: ts=8
312 */
313