ctl_frontend.c revision 313369
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.c#4 $
32 */
33/*
34 * CAM Target Layer front end interface code
35 *
36 * Author: Ken Merry <ken@FreeBSD.org>
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_frontend.c 313369 2017-02-07 01:56:26Z mav $");
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/types.h>
46#include <sys/malloc.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/condvar.h>
50#include <sys/endian.h>
51#include <sys/queue.h>
52#include <sys/sysctl.h>
53
54#include <cam/scsi/scsi_all.h>
55#include <cam/scsi/scsi_da.h>
56#include <cam/ctl/ctl_io.h>
57#include <cam/ctl/ctl.h>
58#include <cam/ctl/ctl_frontend.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_softc *softc = control_softc;
72	struct ctl_frontend *fe_tmp;
73	int error;
74
75	KASSERT(softc != NULL, ("CTL is not initialized"));
76
77	/* Sanity check, make sure this isn't a duplicate registration. */
78	mtx_lock(&softc->ctl_lock);
79	STAILQ_FOREACH(fe_tmp, &softc->fe_list, links) {
80		if (strcmp(fe_tmp->name, fe->name) == 0) {
81			mtx_unlock(&softc->ctl_lock);
82			return (-1);
83		}
84	}
85	mtx_unlock(&softc->ctl_lock);
86	STAILQ_INIT(&fe->port_list);
87
88	/* Call the frontend's initialization routine. */
89	if (fe->init != NULL) {
90		if ((error = fe->init()) != 0) {
91			printf("%s frontend init error: %d\n",
92			    fe->name, error);
93			return (error);
94		}
95	}
96
97	mtx_lock(&softc->ctl_lock);
98	softc->num_frontends++;
99	STAILQ_INSERT_TAIL(&softc->fe_list, fe, links);
100	mtx_unlock(&softc->ctl_lock);
101	return (0);
102}
103
104int
105ctl_frontend_deregister(struct ctl_frontend *fe)
106{
107	struct ctl_softc *softc = control_softc;
108	int error;
109
110	/* Call the frontend's shutdown routine.*/
111	if (fe->shutdown != NULL) {
112		if ((error = fe->shutdown()) != 0) {
113			printf("%s frontend shutdown error: %d\n",
114			    fe->name, error);
115			return (error);
116		}
117	}
118
119	mtx_lock(&softc->ctl_lock);
120	STAILQ_REMOVE(&softc->fe_list, fe, ctl_frontend, links);
121	softc->num_frontends--;
122	mtx_unlock(&softc->ctl_lock);
123	return (0);
124}
125
126struct ctl_frontend *
127ctl_frontend_find(char *frontend_name)
128{
129	struct ctl_softc *softc = control_softc;
130	struct ctl_frontend *fe;
131
132	mtx_lock(&softc->ctl_lock);
133	STAILQ_FOREACH(fe, &softc->fe_list, links) {
134		if (strcmp(fe->name, frontend_name) == 0) {
135			mtx_unlock(&softc->ctl_lock);
136			return (fe);
137		}
138	}
139	mtx_unlock(&softc->ctl_lock);
140	return (NULL);
141}
142
143int
144ctl_port_register(struct ctl_port *port)
145{
146	struct ctl_softc *softc = control_softc;
147	struct ctl_port *tport, *nport;
148	void *pool;
149	int port_num;
150	int retval;
151
152	KASSERT(softc != NULL, ("CTL is not initialized"));
153	port->ctl_softc = softc;
154
155	mtx_lock(&softc->ctl_lock);
156	if (port->targ_port >= 0)
157		port_num = port->targ_port;
158	else
159		port_num = ctl_ffz(softc->ctl_port_mask,
160		    softc->port_min, softc->port_max);
161	if ((port_num < 0) ||
162	    (ctl_set_mask(softc->ctl_port_mask, port_num) < 0)) {
163		mtx_unlock(&softc->ctl_lock);
164		return (1);
165	}
166	softc->num_ports++;
167	mtx_unlock(&softc->ctl_lock);
168
169	/*
170	 * Initialize the initiator and portname mappings
171	 */
172	port->max_initiators = CTL_MAX_INIT_PER_PORT;
173	port->wwpn_iid = malloc(sizeof(*port->wwpn_iid) * port->max_initiators,
174	    M_CTL, M_NOWAIT | M_ZERO);
175	if (port->wwpn_iid == NULL) {
176		retval = ENOMEM;
177		goto error;
178	}
179
180	/*
181	 * We add 20 to whatever the caller requests, so he doesn't get
182	 * burned by queueing things back to the pending sense queue.  In
183	 * theory, there should probably only be one outstanding item, at
184	 * most, on the pending sense queue for a LUN.  We'll clear the
185	 * pending sense queue on the next command, whether or not it is
186	 * a REQUEST SENSE.
187	 */
188	retval = ctl_pool_create(softc, port->port_name,
189				 port->num_requested_ctl_io + 20, &pool);
190	if (retval != 0) {
191		free(port->wwpn_iid, M_CTL);
192error:
193		port->targ_port = -1;
194		mtx_lock(&softc->ctl_lock);
195		ctl_clear_mask(softc->ctl_port_mask, port_num);
196		mtx_unlock(&softc->ctl_lock);
197		return (retval);
198	}
199	port->targ_port = port_num;
200	port->ctl_pool_ref = pool;
201	if (port->options.stqh_first == NULL)
202		STAILQ_INIT(&port->options);
203	port->stats.item = port_num;
204	mtx_init(&port->port_lock, "CTL port", NULL, MTX_DEF);
205
206	mtx_lock(&softc->ctl_lock);
207	STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
208	for (tport = NULL, nport = STAILQ_FIRST(&softc->port_list);
209	    nport != NULL && nport->targ_port < port_num;
210	    tport = nport, nport = STAILQ_NEXT(tport, links)) {
211	}
212	if (tport)
213		STAILQ_INSERT_AFTER(&softc->port_list, tport, port, links);
214	else
215		STAILQ_INSERT_HEAD(&softc->port_list, port, links);
216	softc->ctl_ports[port->targ_port] = port;
217	mtx_unlock(&softc->ctl_lock);
218
219	return (retval);
220}
221
222int
223ctl_port_deregister(struct ctl_port *port)
224{
225	struct ctl_softc *softc = port->ctl_softc;
226	struct ctl_io_pool *pool = (struct ctl_io_pool *)port->ctl_pool_ref;
227	int i;
228
229	if (port->targ_port == -1)
230		return (1);
231
232	mtx_lock(&softc->ctl_lock);
233	STAILQ_REMOVE(&softc->port_list, port, ctl_port, links);
234	STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
235	softc->num_ports--;
236	ctl_clear_mask(softc->ctl_port_mask, port->targ_port);
237	softc->ctl_ports[port->targ_port] = NULL;
238	mtx_unlock(&softc->ctl_lock);
239
240	ctl_pool_free(pool);
241	ctl_free_opts(&port->options);
242
243	ctl_lun_map_deinit(port);
244	free(port->port_devid, M_CTL);
245	port->port_devid = NULL;
246	free(port->target_devid, M_CTL);
247	port->target_devid = NULL;
248	free(port->init_devid, M_CTL);
249	port->init_devid = NULL;
250	for (i = 0; i < port->max_initiators; i++)
251		free(port->wwpn_iid[i].name, M_CTL);
252	free(port->wwpn_iid, M_CTL);
253	mtx_destroy(&port->port_lock);
254
255	return (0);
256}
257
258void
259ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
260		      int wwpn_valid, uint64_t wwpn)
261{
262	struct scsi_vpd_id_descriptor *desc;
263	int len, proto;
264
265	if (port->port_type == CTL_PORT_FC)
266		proto = SCSI_PROTO_FC << 4;
267	else if (port->port_type == CTL_PORT_ISCSI)
268		proto = SCSI_PROTO_ISCSI << 4;
269	else
270		proto = SCSI_PROTO_SPI << 4;
271
272	if (wwnn_valid) {
273		port->wwnn = wwnn;
274
275		free(port->target_devid, M_CTL);
276
277		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
278		port->target_devid = malloc(sizeof(struct ctl_devid) + len,
279		    M_CTL, M_WAITOK | M_ZERO);
280		port->target_devid->len = len;
281		desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
282		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
283		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
284		    SVPD_ID_TYPE_NAA;
285		desc->length = CTL_WWPN_LEN;
286		scsi_u64to8b(port->wwnn, desc->identifier);
287	}
288
289	if (wwpn_valid) {
290		port->wwpn = wwpn;
291
292		free(port->port_devid, M_CTL);
293
294		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
295		port->port_devid = malloc(sizeof(struct ctl_devid) + len,
296		    M_CTL, M_WAITOK | M_ZERO);
297		port->port_devid->len = len;
298		desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
299		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
300		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
301		    SVPD_ID_TYPE_NAA;
302		desc->length = CTL_WWPN_LEN;
303		scsi_u64to8b(port->wwpn, desc->identifier);
304	}
305}
306
307void
308ctl_port_online(struct ctl_port *port)
309{
310	struct ctl_softc *softc = port->ctl_softc;
311	struct ctl_lun *lun;
312	const char *value;
313	uint32_t l;
314
315	if (port->lun_enable != NULL) {
316		if (port->lun_map) {
317			for (l = 0; l < port->lun_map_size; l++) {
318				if (ctl_lun_map_from_port(port, l) ==
319				    UINT32_MAX)
320					continue;
321				port->lun_enable(port->targ_lun_arg, l);
322			}
323		} else {
324			STAILQ_FOREACH(lun, &softc->lun_list, links)
325				port->lun_enable(port->targ_lun_arg, lun->lun);
326		}
327	}
328	if (port->port_online != NULL)
329		port->port_online(port->onoff_arg);
330	mtx_lock(&softc->ctl_lock);
331	if (softc->is_single == 0) {
332		value = ctl_get_opt(&port->options, "ha_shared");
333		if (value != NULL && strcmp(value, "on") == 0)
334			port->status |= CTL_PORT_STATUS_HA_SHARED;
335		else
336			port->status &= ~CTL_PORT_STATUS_HA_SHARED;
337	}
338	port->status |= CTL_PORT_STATUS_ONLINE;
339	STAILQ_FOREACH(lun, &softc->lun_list, links) {
340		if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
341			continue;
342		mtx_lock(&lun->lun_lock);
343		ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
344		mtx_unlock(&lun->lun_lock);
345	}
346	mtx_unlock(&softc->ctl_lock);
347	ctl_isc_announce_port(port);
348}
349
350void
351ctl_port_offline(struct ctl_port *port)
352{
353	struct ctl_softc *softc = port->ctl_softc;
354	struct ctl_lun *lun;
355	uint32_t l;
356
357	if (port->port_offline != NULL)
358		port->port_offline(port->onoff_arg);
359	if (port->lun_disable != NULL) {
360		if (port->lun_map) {
361			for (l = 0; l < port->lun_map_size; l++) {
362				if (ctl_lun_map_from_port(port, l) ==
363				    UINT32_MAX)
364					continue;
365				port->lun_disable(port->targ_lun_arg, l);
366			}
367		} else {
368			STAILQ_FOREACH(lun, &softc->lun_list, links)
369				port->lun_disable(port->targ_lun_arg, lun->lun);
370		}
371	}
372	mtx_lock(&softc->ctl_lock);
373	port->status &= ~CTL_PORT_STATUS_ONLINE;
374	STAILQ_FOREACH(lun, &softc->lun_list, links) {
375		if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
376			continue;
377		mtx_lock(&lun->lun_lock);
378		ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
379		mtx_unlock(&lun->lun_lock);
380	}
381	mtx_unlock(&softc->ctl_lock);
382	ctl_isc_announce_port(port);
383}
384
385/*
386 * vim: ts=8
387 */
388