ctl_frontend.c revision 275493
1/*-
2 * Copyright (c) 2003 Silicon Graphics International Corp.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions, and the following disclaimer,
10 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    substantially similar to the "NO WARRANTY" disclaimer below
13 *    ("Disclaimer") and any redistribution must be conditioned upon
14 *    including a substantially similar Disclaimer requirement for further
15 *    binary redistribution.
16 *
17 * NO WARRANTY
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
29 *
30 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.c#4 $
31 */
32/*
33 * CAM Target Layer front end interface code
34 *
35 * Author: Ken Merry <ken@FreeBSD.org>
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_frontend.c 275493 2014-12-05 07:23:25Z mav $");
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/types.h>
45#include <sys/malloc.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <sys/condvar.h>
49#include <sys/endian.h>
50#include <sys/queue.h>
51#include <sys/sysctl.h>
52
53#include <cam/scsi/scsi_all.h>
54#include <cam/scsi/scsi_da.h>
55#include <cam/ctl/ctl_io.h>
56#include <cam/ctl/ctl.h>
57#include <cam/ctl/ctl_frontend.h>
58#include <cam/ctl/ctl_frontend_internal.h>
59#include <cam/ctl/ctl_backend.h>
60/* XXX KDM move defines from ctl_ioctl.h to somewhere else */
61#include <cam/ctl/ctl_ioctl.h>
62#include <cam/ctl/ctl_ha.h>
63#include <cam/ctl/ctl_private.h>
64#include <cam/ctl/ctl_debug.h>
65
66extern struct ctl_softc *control_softc;
67
68int
69ctl_frontend_register(struct ctl_frontend *fe)
70{
71	struct ctl_frontend *fe_tmp;
72
73	KASSERT(control_softc != NULL, ("CTL is not initialized"));
74
75	/*
76	 * Sanity check, make sure this isn't a duplicate registration.
77	 */
78	mtx_lock(&control_softc->ctl_lock);
79	STAILQ_FOREACH(fe_tmp, &control_softc->fe_list, links) {
80		if (strcmp(fe_tmp->name, fe->name) == 0) {
81			mtx_unlock(&control_softc->ctl_lock);
82			return (-1);
83		}
84	}
85	mtx_unlock(&control_softc->ctl_lock);
86	STAILQ_INIT(&fe->port_list);
87
88	/*
89	 * Call the frontend's initialization routine.
90	 */
91	if (fe->init != NULL)
92		fe->init();
93
94	mtx_lock(&control_softc->ctl_lock);
95	control_softc->num_frontends++;
96	STAILQ_INSERT_TAIL(&control_softc->fe_list, fe, links);
97	mtx_unlock(&control_softc->ctl_lock);
98	return (0);
99}
100
101int
102ctl_frontend_deregister(struct ctl_frontend *fe)
103{
104
105	if (!STAILQ_EMPTY(&fe->port_list))
106		return (-1);
107
108	mtx_lock(&control_softc->ctl_lock);
109	STAILQ_REMOVE(&control_softc->fe_list, fe, ctl_frontend, links);
110	control_softc->num_frontends--;
111	mtx_unlock(&control_softc->ctl_lock);
112
113	/*
114	 * Call the frontend's shutdown routine.
115	 */
116	if (fe->shutdown != NULL)
117		fe->shutdown();
118	return (0);
119}
120
121struct ctl_frontend *
122ctl_frontend_find(char *frontend_name)
123{
124	struct ctl_softc *ctl_softc = control_softc;
125	struct ctl_frontend *fe;
126
127	mtx_lock(&ctl_softc->ctl_lock);
128	STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
129		if (strcmp(fe->name, frontend_name) == 0) {
130			mtx_unlock(&ctl_softc->ctl_lock);
131			return (fe);
132		}
133	}
134	mtx_unlock(&ctl_softc->ctl_lock);
135	return (NULL);
136}
137
138int
139ctl_port_register(struct ctl_port *port)
140{
141	struct ctl_io_pool *pool;
142	int port_num;
143	int retval;
144
145	retval = 0;
146
147	KASSERT(control_softc != NULL, ("CTL is not initialized"));
148
149	mtx_lock(&control_softc->ctl_lock);
150	port_num = ctl_ffz(control_softc->ctl_port_mask, CTL_MAX_PORTS);
151	if ((port_num == -1)
152	 || (ctl_set_mask(control_softc->ctl_port_mask, port_num) == -1)) {
153		port->targ_port = -1;
154		mtx_unlock(&control_softc->ctl_lock);
155		return (1);
156	}
157	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 + control_softc->port_offset;
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	free(port->init_devid, M_CTL);
238	port->init_devid = NULL;
239	for (i = 0; i < port->max_initiators; i++)
240		free(port->wwpn_iid[i].name, M_CTL);
241	free(port->wwpn_iid, M_CTL);
242
243bailout:
244	return (retval);
245}
246
247void
248ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
249		      int wwpn_valid, uint64_t wwpn)
250{
251	struct scsi_vpd_id_descriptor *desc;
252	int len, proto;
253
254	if (port->port_type == CTL_PORT_FC)
255		proto = SCSI_PROTO_FC << 4;
256	else if (port->port_type == CTL_PORT_ISCSI)
257		proto = SCSI_PROTO_ISCSI << 4;
258	else
259		proto = SCSI_PROTO_SPI << 4;
260
261	if (wwnn_valid) {
262		port->wwnn = wwnn;
263
264		free(port->target_devid, M_CTL);
265
266		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
267		port->target_devid = malloc(sizeof(struct ctl_devid) + len,
268		    M_CTL, M_WAITOK | M_ZERO);
269		port->target_devid->len = len;
270		desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
271		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
272		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
273		    SVPD_ID_TYPE_NAA;
274		desc->length = CTL_WWPN_LEN;
275		scsi_u64to8b(port->wwnn, desc->identifier);
276	}
277
278	if (wwpn_valid) {
279		port->wwpn = wwpn;
280
281		free(port->port_devid, M_CTL);
282
283		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
284		port->port_devid = malloc(sizeof(struct ctl_devid) + len,
285		    M_CTL, M_WAITOK | M_ZERO);
286		port->port_devid->len = len;
287		desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
288		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
289		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
290		    SVPD_ID_TYPE_NAA;
291		desc->length = CTL_WWPN_LEN;
292		scsi_u64to8b(port->wwpn, desc->identifier);
293	}
294}
295
296void
297ctl_port_online(struct ctl_port *port)
298{
299	port->port_online(port->onoff_arg);
300	/* XXX KDM need a lock here? */
301	port->status |= CTL_PORT_STATUS_ONLINE;
302}
303
304void
305ctl_port_offline(struct ctl_port *port)
306{
307	port->port_offline(port->onoff_arg);
308	/* XXX KDM need a lock here? */
309	port->status &= ~CTL_PORT_STATUS_ONLINE;
310}
311
312/*
313 * vim: ts=8
314 */
315