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.c#4 $
31229997Sken */
32229997Sken/*
33229997Sken * CAM Target Layer front end interface code
34229997Sken *
35229997Sken * Author: Ken Merry <ken@FreeBSD.org>
36229997Sken */
37229997Sken
38229997Sken#include <sys/cdefs.h>
39229997Sken__FBSDID("$FreeBSD: releng/10.3/sys/cam/ctl/ctl_frontend.c 291388 2015-11-27 15:26:19Z mav $");
40229997Sken
41229997Sken#include <sys/param.h>
42229997Sken#include <sys/systm.h>
43229997Sken#include <sys/kernel.h>
44229997Sken#include <sys/types.h>
45229997Sken#include <sys/malloc.h>
46229997Sken#include <sys/lock.h>
47229997Sken#include <sys/mutex.h>
48229997Sken#include <sys/condvar.h>
49229997Sken#include <sys/endian.h>
50229997Sken#include <sys/queue.h>
51233963Sken#include <sys/sysctl.h>
52229997Sken
53229997Sken#include <cam/scsi/scsi_all.h>
54229997Sken#include <cam/scsi/scsi_da.h>
55229997Sken#include <cam/ctl/ctl_io.h>
56229997Sken#include <cam/ctl/ctl.h>
57229997Sken#include <cam/ctl/ctl_frontend.h>
58229997Sken#include <cam/ctl/ctl_backend.h>
59229997Sken/* XXX KDM move defines from ctl_ioctl.h to somewhere else */
60229997Sken#include <cam/ctl/ctl_ioctl.h>
61229997Sken#include <cam/ctl/ctl_ha.h>
62229997Sken#include <cam/ctl/ctl_private.h>
63229997Sken#include <cam/ctl/ctl_debug.h>
64229997Sken
65229997Skenextern struct ctl_softc *control_softc;
66229997Sken
67229997Skenint
68268677Smavctl_frontend_register(struct ctl_frontend *fe)
69229997Sken{
70276614Smav	struct ctl_softc *softc = control_softc;
71268677Smav	struct ctl_frontend *fe_tmp;
72268677Smav
73276614Smav	KASSERT(softc != NULL, ("CTL is not initialized"));
74268677Smav
75268677Smav	/*
76268677Smav	 * Sanity check, make sure this isn't a duplicate registration.
77268677Smav	 */
78276614Smav	mtx_lock(&softc->ctl_lock);
79276614Smav	STAILQ_FOREACH(fe_tmp, &softc->fe_list, links) {
80268677Smav		if (strcmp(fe_tmp->name, fe->name) == 0) {
81276614Smav			mtx_unlock(&softc->ctl_lock);
82268677Smav			return (-1);
83268677Smav		}
84268677Smav	}
85276614Smav	mtx_unlock(&softc->ctl_lock);
86268677Smav	STAILQ_INIT(&fe->port_list);
87268677Smav
88268677Smav	/*
89268677Smav	 * Call the frontend's initialization routine.
90268677Smav	 */
91268677Smav	if (fe->init != NULL)
92268677Smav		fe->init();
93268677Smav
94276614Smav	mtx_lock(&softc->ctl_lock);
95276614Smav	softc->num_frontends++;
96276614Smav	STAILQ_INSERT_TAIL(&softc->fe_list, fe, links);
97276614Smav	mtx_unlock(&softc->ctl_lock);
98268677Smav	return (0);
99268677Smav}
100268677Smav
101268677Smavint
102268677Smavctl_frontend_deregister(struct ctl_frontend *fe)
103268677Smav{
104276614Smav	struct ctl_softc *softc = control_softc;
105268677Smav
106268677Smav	if (!STAILQ_EMPTY(&fe->port_list))
107268677Smav		return (-1);
108268677Smav
109276614Smav	mtx_lock(&softc->ctl_lock);
110276614Smav	STAILQ_REMOVE(&softc->fe_list, fe, ctl_frontend, links);
111276614Smav	softc->num_frontends--;
112276614Smav	mtx_unlock(&softc->ctl_lock);
113268677Smav
114268677Smav	/*
115268677Smav	 * Call the frontend's shutdown routine.
116268677Smav	 */
117268677Smav	if (fe->shutdown != NULL)
118268677Smav		fe->shutdown();
119268677Smav	return (0);
120268677Smav}
121268677Smav
122268680Smavstruct ctl_frontend *
123268680Smavctl_frontend_find(char *frontend_name)
124268680Smav{
125276614Smav	struct ctl_softc *softc = control_softc;
126268680Smav	struct ctl_frontend *fe;
127268680Smav
128276614Smav	mtx_lock(&softc->ctl_lock);
129276614Smav	STAILQ_FOREACH(fe, &softc->fe_list, links) {
130268680Smav		if (strcmp(fe->name, frontend_name) == 0) {
131276614Smav			mtx_unlock(&softc->ctl_lock);
132268680Smav			return (fe);
133268680Smav		}
134268680Smav	}
135276614Smav	mtx_unlock(&softc->ctl_lock);
136268680Smav	return (NULL);
137268680Smav}
138268680Smav
139268677Smavint
140275493Smavctl_port_register(struct ctl_port *port)
141268677Smav{
142276614Smav	struct ctl_softc *softc = control_softc;
143288732Smav	struct ctl_port *tport, *nport;
144275878Smav	void *pool;
145229997Sken	int port_num;
146229997Sken	int retval;
147229997Sken
148276614Smav	KASSERT(softc != NULL, ("CTL is not initialized"));
149288795Smav	port->ctl_softc = softc;
150229997Sken
151276614Smav	mtx_lock(&softc->ctl_lock);
152288732Smav	if (port->targ_port >= 0)
153288732Smav		port_num = port->targ_port;
154288732Smav	else
155288732Smav		port_num = ctl_ffz(softc->ctl_port_mask,
156288732Smav		    softc->port_min, softc->port_max);
157288732Smav	if ((port_num < 0) ||
158288732Smav	    (ctl_set_mask(softc->ctl_port_mask, port_num) < 0)) {
159276614Smav		mtx_unlock(&softc->ctl_lock);
160229997Sken		return (1);
161229997Sken	}
162276614Smav	softc->num_ports++;
163276614Smav	mtx_unlock(&softc->ctl_lock);
164229997Sken
165229997Sken	/*
166268692Smav	 * Initialize the initiator and portname mappings
167268692Smav	 */
168268692Smav	port->max_initiators = CTL_MAX_INIT_PER_PORT;
169268692Smav	port->wwpn_iid = malloc(sizeof(*port->wwpn_iid) * port->max_initiators,
170268692Smav	    M_CTL, M_NOWAIT | M_ZERO);
171268692Smav	if (port->wwpn_iid == NULL) {
172268692Smav		retval = ENOMEM;
173268692Smav		goto error;
174268692Smav	}
175268692Smav
176268692Smav	/*
177229997Sken	 * We add 20 to whatever the caller requests, so he doesn't get
178229997Sken	 * burned by queueing things back to the pending sense queue.  In
179229997Sken	 * theory, there should probably only be one outstanding item, at
180229997Sken	 * most, on the pending sense queue for a LUN.  We'll clear the
181229997Sken	 * pending sense queue on the next command, whether or not it is
182229997Sken	 * a REQUEST SENSE.
183229997Sken	 */
184276614Smav	retval = ctl_pool_create(softc, port->port_name,
185268677Smav				 port->num_requested_ctl_io + 20, &pool);
186229997Sken	if (retval != 0) {
187268692Smav		free(port->wwpn_iid, M_CTL);
188268692Smaverror:
189268677Smav		port->targ_port = -1;
190276614Smav		mtx_lock(&softc->ctl_lock);
191276614Smav		ctl_clear_mask(softc->ctl_port_mask, port_num);
192276614Smav		mtx_unlock(&softc->ctl_lock);
193229997Sken		return (retval);
194229997Sken	}
195268677Smav	port->ctl_pool_ref = pool;
196229997Sken
197268682Smav	if (port->options.stqh_first == NULL)
198268682Smav		STAILQ_INIT(&port->options);
199268682Smav
200276614Smav	mtx_lock(&softc->ctl_lock);
201288732Smav	port->targ_port = port_num;
202268677Smav	STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
203288732Smav	for (tport = NULL, nport = STAILQ_FIRST(&softc->port_list);
204288732Smav	    nport != NULL && nport->targ_port < port_num;
205288732Smav	    tport = nport, nport = STAILQ_NEXT(tport, links)) {
206288732Smav	}
207288732Smav	if (tport)
208288732Smav		STAILQ_INSERT_AFTER(&softc->port_list, tport, port, links);
209288732Smav	else
210288732Smav		STAILQ_INSERT_HEAD(&softc->port_list, port, links);
211288732Smav	softc->ctl_ports[port->targ_port] = port;
212276614Smav	mtx_unlock(&softc->ctl_lock);
213229997Sken
214229997Sken	return (retval);
215229997Sken}
216229997Sken
217229997Skenint
218268677Smavctl_port_deregister(struct ctl_port *port)
219229997Sken{
220288795Smav	struct ctl_softc *softc = port->ctl_softc;
221229997Sken	struct ctl_io_pool *pool;
222288732Smav	int retval, i;
223229997Sken
224229997Sken	retval = 0;
225229997Sken
226268677Smav	pool = (struct ctl_io_pool *)port->ctl_pool_ref;
227229997Sken
228268677Smav	if (port->targ_port == -1) {
229229997Sken		retval = 1;
230229997Sken		goto bailout;
231229997Sken	}
232229997Sken
233276614Smav	mtx_lock(&softc->ctl_lock);
234276614Smav	STAILQ_REMOVE(&softc->port_list, port, ctl_port, links);
235268677Smav	STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
236276614Smav	softc->num_ports--;
237288732Smav	ctl_clear_mask(softc->ctl_port_mask, port->targ_port);
238288732Smav	softc->ctl_ports[port->targ_port] = NULL;
239276614Smav	mtx_unlock(&softc->ctl_lock);
240260477Smav
241260477Smav	ctl_pool_free(pool);
242268682Smav	ctl_free_opts(&port->options);
243260477Smav
244279002Smav	ctl_lun_map_deinit(port);
245268683Smav	free(port->port_devid, M_CTL);
246268683Smav	port->port_devid = NULL;
247268683Smav	free(port->target_devid, M_CTL);
248268683Smav	port->target_devid = NULL;
249269296Smav	free(port->init_devid, M_CTL);
250269296Smav	port->init_devid = NULL;
251268692Smav	for (i = 0; i < port->max_initiators; i++)
252268692Smav		free(port->wwpn_iid[i].name, M_CTL);
253268692Smav	free(port->wwpn_iid, M_CTL);
254268683Smav
255229997Skenbailout:
256229997Sken	return (retval);
257229997Sken}
258229997Sken
259229997Skenvoid
260268677Smavctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
261229997Sken		      int wwpn_valid, uint64_t wwpn)
262229997Sken{
263268683Smav	struct scsi_vpd_id_descriptor *desc;
264268683Smav	int len, proto;
265268683Smav
266268683Smav	if (port->port_type == CTL_PORT_FC)
267268683Smav		proto = SCSI_PROTO_FC << 4;
268268683Smav	else if (port->port_type == CTL_PORT_ISCSI)
269268683Smav		proto = SCSI_PROTO_ISCSI << 4;
270268683Smav	else
271268683Smav		proto = SCSI_PROTO_SPI << 4;
272268683Smav
273268683Smav	if (wwnn_valid) {
274268677Smav		port->wwnn = wwnn;
275229997Sken
276268683Smav		free(port->target_devid, M_CTL);
277268683Smav
278268683Smav		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
279268683Smav		port->target_devid = malloc(sizeof(struct ctl_devid) + len,
280268683Smav		    M_CTL, M_WAITOK | M_ZERO);
281268683Smav		port->target_devid->len = len;
282268683Smav		desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
283268683Smav		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
284268683Smav		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
285268683Smav		    SVPD_ID_TYPE_NAA;
286268683Smav		desc->length = CTL_WWPN_LEN;
287268683Smav		scsi_u64to8b(port->wwnn, desc->identifier);
288268683Smav	}
289268683Smav
290268683Smav	if (wwpn_valid) {
291268677Smav		port->wwpn = wwpn;
292268683Smav
293268683Smav		free(port->port_devid, M_CTL);
294268683Smav
295268683Smav		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
296268683Smav		port->port_devid = malloc(sizeof(struct ctl_devid) + len,
297268683Smav		    M_CTL, M_WAITOK | M_ZERO);
298268683Smav		port->port_devid->len = len;
299268683Smav		desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
300268683Smav		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
301268683Smav		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
302268683Smav		    SVPD_ID_TYPE_NAA;
303268683Smav		desc->length = CTL_WWPN_LEN;
304268683Smav		scsi_u64to8b(port->wwpn, desc->identifier);
305268683Smav	}
306229997Sken}
307229997Sken
308229997Skenvoid
309268677Smavctl_port_online(struct ctl_port *port)
310229997Sken{
311288795Smav	struct ctl_softc *softc = port->ctl_softc;
312284798Smav	struct ctl_lun *lun;
313291388Smav	const char *value;
314284798Smav	uint32_t l;
315284798Smav
316288724Smav	if (port->lun_enable != NULL) {
317288724Smav		if (port->lun_map) {
318288724Smav			for (l = 0; l < CTL_MAX_LUNS; l++) {
319288724Smav				if (ctl_lun_map_from_port(port, l) >=
320288724Smav				    CTL_MAX_LUNS)
321288724Smav					continue;
322288724Smav				port->lun_enable(port->targ_lun_arg, l);
323288724Smav			}
324288724Smav		} else {
325288724Smav			STAILQ_FOREACH(lun, &softc->lun_list, links)
326288724Smav				port->lun_enable(port->targ_lun_arg, lun->lun);
327284798Smav		}
328284798Smav	}
329288724Smav	if (port->port_online != NULL)
330288724Smav		port->port_online(port->onoff_arg);
331288747Smav	mtx_lock(&softc->ctl_lock);
332291388Smav	if (softc->is_single == 0) {
333291388Smav		value = ctl_get_opt(&port->options, "ha_shared");
334291388Smav		if (value != NULL && strcmp(value, "on") == 0)
335291388Smav			port->status |= CTL_PORT_STATUS_HA_SHARED;
336291388Smav		else
337291388Smav			port->status &= ~CTL_PORT_STATUS_HA_SHARED;
338291388Smav	}
339268677Smav	port->status |= CTL_PORT_STATUS_ONLINE;
340288747Smav	STAILQ_FOREACH(lun, &softc->lun_list, links) {
341288747Smav		if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
342288747Smav			continue;
343288747Smav		mtx_lock(&lun->lun_lock);
344288747Smav		ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
345288747Smav		mtx_unlock(&lun->lun_lock);
346288747Smav	}
347288747Smav	mtx_unlock(&softc->ctl_lock);
348288732Smav	ctl_isc_announce_port(port);
349229997Sken}
350229997Sken
351229997Skenvoid
352268677Smavctl_port_offline(struct ctl_port *port)
353229997Sken{
354288795Smav	struct ctl_softc *softc = port->ctl_softc;
355284798Smav	struct ctl_lun *lun;
356284798Smav	uint32_t l;
357284798Smav
358288724Smav	if (port->port_offline != NULL)
359288724Smav		port->port_offline(port->onoff_arg);
360288724Smav	if (port->lun_disable != NULL) {
361288724Smav		if (port->lun_map) {
362288724Smav			for (l = 0; l < CTL_MAX_LUNS; l++) {
363288724Smav				if (ctl_lun_map_from_port(port, l) >=
364288724Smav				    CTL_MAX_LUNS)
365288724Smav					continue;
366288724Smav				port->lun_disable(port->targ_lun_arg, l);
367288724Smav			}
368288724Smav		} else {
369288724Smav			STAILQ_FOREACH(lun, &softc->lun_list, links)
370288724Smav				port->lun_disable(port->targ_lun_arg, lun->lun);
371284798Smav		}
372284798Smav	}
373288747Smav	mtx_lock(&softc->ctl_lock);
374268677Smav	port->status &= ~CTL_PORT_STATUS_ONLINE;
375288747Smav	STAILQ_FOREACH(lun, &softc->lun_list, links) {
376288747Smav		if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
377288747Smav			continue;
378288747Smav		mtx_lock(&lun->lun_lock);
379288747Smav		ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
380288747Smav		mtx_unlock(&lun->lun_lock);
381288747Smav	}
382288747Smav	mtx_unlock(&softc->ctl_lock);
383288732Smav	ctl_isc_announce_port(port);
384229997Sken}
385229997Sken
386229997Sken/*
387229997Sken * vim: ts=8
388229997Sken */
389