xref: /freebsd/sys/cam/ctl/ctl_frontend.c (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Silicon Graphics International Corp.
5  * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification.
14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15  *    substantially similar to the "NO WARRANTY" disclaimer below
16  *    ("Disclaimer") and any redistribution must be conditioned upon
17  *    including a substantially similar Disclaimer requirement for further
18  *    binary redistribution.
19  *
20  * NO WARRANTY
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGES.
32  *
33  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.c#4 $
34  */
35 /*
36  * CAM Target Layer front end interface code
37  *
38  * Author: Ken Merry <ken@FreeBSD.org>
39  */
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 #include <sys/nv.h>
53 #include <sys/dnv.h>
54 
55 #include <cam/scsi/scsi_all.h>
56 #include <cam/scsi/scsi_da.h>
57 #include <cam/ctl/ctl_io.h>
58 #include <cam/ctl/ctl.h>
59 #include <cam/ctl/ctl_frontend.h>
60 #include <cam/ctl/ctl_backend.h>
61 /* XXX KDM move defines from ctl_ioctl.h to somewhere else */
62 #include <cam/ctl/ctl_ioctl.h>
63 #include <cam/ctl/ctl_ha.h>
64 #include <cam/ctl/ctl_private.h>
65 #include <cam/ctl/ctl_debug.h>
66 
67 int
68 ctl_frontend_register(struct ctl_frontend *fe)
69 {
70 	struct ctl_softc *softc = control_softc;
71 	struct ctl_frontend *fe_tmp;
72 	int error;
73 
74 	KASSERT(softc != NULL, ("CTL is not initialized"));
75 
76 	/* Sanity check, make sure this isn't a duplicate registration. */
77 	mtx_lock(&softc->ctl_lock);
78 	STAILQ_FOREACH(fe_tmp, &softc->fe_list, links) {
79 		if (strcmp(fe_tmp->name, fe->name) == 0) {
80 			mtx_unlock(&softc->ctl_lock);
81 			return (-1);
82 		}
83 	}
84 	mtx_unlock(&softc->ctl_lock);
85 	STAILQ_INIT(&fe->port_list);
86 
87 	/* Call the frontend's initialization routine. */
88 	if (fe->init != NULL) {
89 		if ((error = fe->init()) != 0) {
90 			printf("%s frontend init error: %d\n",
91 			    fe->name, error);
92 			return (error);
93 		}
94 	}
95 
96 	mtx_lock(&softc->ctl_lock);
97 	softc->num_frontends++;
98 	STAILQ_INSERT_TAIL(&softc->fe_list, fe, links);
99 	mtx_unlock(&softc->ctl_lock);
100 	return (0);
101 }
102 
103 int
104 ctl_frontend_deregister(struct ctl_frontend *fe)
105 {
106 	struct ctl_softc *softc = control_softc;
107 	int error;
108 
109 	/* Call the frontend's shutdown routine.*/
110 	if (fe->shutdown != NULL) {
111 		if ((error = fe->shutdown()) != 0) {
112 			printf("%s frontend shutdown error: %d\n",
113 			    fe->name, error);
114 			return (error);
115 		}
116 	}
117 
118 	mtx_lock(&softc->ctl_lock);
119 	STAILQ_REMOVE(&softc->fe_list, fe, ctl_frontend, links);
120 	softc->num_frontends--;
121 	mtx_unlock(&softc->ctl_lock);
122 	return (0);
123 }
124 
125 struct ctl_frontend *
126 ctl_frontend_find(char *frontend_name)
127 {
128 	struct ctl_softc *softc = control_softc;
129 	struct ctl_frontend *fe;
130 
131 	mtx_lock(&softc->ctl_lock);
132 	STAILQ_FOREACH(fe, &softc->fe_list, links) {
133 		if (strcmp(fe->name, frontend_name) == 0) {
134 			mtx_unlock(&softc->ctl_lock);
135 			return (fe);
136 		}
137 	}
138 	mtx_unlock(&softc->ctl_lock);
139 	return (NULL);
140 }
141 
142 int
143 ctl_port_register(struct ctl_port *port)
144 {
145 	struct ctl_softc *softc = control_softc;
146 	struct ctl_port *tport, *nport;
147 	void *pool;
148 	int port_num;
149 	int retval;
150 
151 	KASSERT(softc != NULL, ("CTL is not initialized"));
152 	port->ctl_softc = softc;
153 
154 	mtx_lock(&softc->ctl_lock);
155 	if (port->targ_port >= 0)
156 		port_num = port->targ_port;
157 	else
158 		port_num = ctl_ffz(softc->ctl_port_mask,
159 		    softc->port_min, softc->port_max);
160 	if ((port_num < 0) ||
161 	    (ctl_set_mask(softc->ctl_port_mask, port_num) < 0)) {
162 		mtx_unlock(&softc->ctl_lock);
163 		return (EBUSY);
164 	}
165 	softc->num_ports++;
166 	mtx_unlock(&softc->ctl_lock);
167 
168 	/*
169 	 * Initialize the initiator and portname mappings
170 	 */
171 	port->max_initiators = CTL_MAX_INIT_PER_PORT;
172 	port->wwpn_iid = malloc(sizeof(*port->wwpn_iid) * port->max_initiators,
173 	    M_CTL, M_NOWAIT | M_ZERO);
174 	if (port->wwpn_iid == NULL) {
175 		retval = ENOMEM;
176 		goto error;
177 	}
178 
179 	/*
180 	 * We add 20 to whatever the caller requests, so he doesn't get
181 	 * burned by queueing things back to the pending sense queue.  In
182 	 * theory, there should probably only be one outstanding item, at
183 	 * most, on the pending sense queue for a LUN.  We'll clear the
184 	 * pending sense queue on the next command, whether or not it is
185 	 * a REQUEST SENSE.
186 	 */
187 	retval = ctl_pool_create(softc, port->port_name,
188 				 port->num_requested_ctl_io + 20, &pool);
189 	if (retval != 0) {
190 		free(port->wwpn_iid, M_CTL);
191 error:
192 		port->targ_port = -1;
193 		mtx_lock(&softc->ctl_lock);
194 		ctl_clear_mask(softc->ctl_port_mask, port_num);
195 		mtx_unlock(&softc->ctl_lock);
196 		return (retval);
197 	}
198 	port->targ_port = port_num;
199 	port->ctl_pool_ref = pool;
200 	if (port->options == NULL)
201 		port->options = nvlist_create(0);
202 	port->stats.item = port_num;
203 	mtx_init(&port->port_lock, "CTL port", NULL, MTX_DEF);
204 
205 	mtx_lock(&softc->ctl_lock);
206 	STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
207 	for (tport = NULL, nport = STAILQ_FIRST(&softc->port_list);
208 	    nport != NULL && nport->targ_port < port_num;
209 	    tport = nport, nport = STAILQ_NEXT(tport, links)) {
210 	}
211 	if (tport)
212 		STAILQ_INSERT_AFTER(&softc->port_list, tport, port, links);
213 	else
214 		STAILQ_INSERT_HEAD(&softc->port_list, port, links);
215 	softc->ctl_ports[port->targ_port] = port;
216 	mtx_unlock(&softc->ctl_lock);
217 
218 	return (retval);
219 }
220 
221 int
222 ctl_port_deregister(struct ctl_port *port)
223 {
224 	struct ctl_softc *softc = port->ctl_softc;
225 	struct ctl_io_pool *pool = (struct ctl_io_pool *)port->ctl_pool_ref;
226 	int i;
227 
228 	if (port->targ_port == -1)
229 		return (1);
230 
231 	mtx_lock(&softc->ctl_lock);
232 	STAILQ_REMOVE(&softc->port_list, port, ctl_port, links);
233 	STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
234 	softc->num_ports--;
235 	ctl_clear_mask(softc->ctl_port_mask, port->targ_port);
236 	softc->ctl_ports[port->targ_port] = NULL;
237 	mtx_unlock(&softc->ctl_lock);
238 
239 	ctl_pool_free(pool);
240 	nvlist_destroy(port->options);
241 
242 	ctl_lun_map_deinit(port);
243 	free(port->port_devid, M_CTL);
244 	port->port_devid = NULL;
245 	free(port->target_devid, M_CTL);
246 	port->target_devid = NULL;
247 	free(port->init_devid, M_CTL);
248 	port->init_devid = NULL;
249 	for (i = 0; i < port->max_initiators; i++)
250 		free(port->wwpn_iid[i].name, M_CTL);
251 	free(port->wwpn_iid, M_CTL);
252 	mtx_destroy(&port->port_lock);
253 
254 	return (0);
255 }
256 
257 void
258 ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
259 		      int wwpn_valid, uint64_t wwpn)
260 {
261 	struct scsi_vpd_id_descriptor *desc;
262 	int len, proto;
263 
264 	if (port->port_type == CTL_PORT_FC)
265 		proto = SCSI_PROTO_FC << 4;
266 	else if (port->port_type == CTL_PORT_SAS)
267 		proto = SCSI_PROTO_SAS << 4;
268 	else if (port->port_type == CTL_PORT_ISCSI)
269 		proto = SCSI_PROTO_ISCSI << 4;
270 	else
271 		proto = SCSI_PROTO_SPI << 4;
272 
273 	if (wwnn_valid) {
274 		port->wwnn = wwnn;
275 
276 		free(port->target_devid, M_CTL);
277 
278 		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
279 		port->target_devid = malloc(sizeof(struct ctl_devid) + len,
280 		    M_CTL, M_WAITOK | M_ZERO);
281 		port->target_devid->len = len;
282 		desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
283 		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
284 		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
285 		    SVPD_ID_TYPE_NAA;
286 		desc->length = CTL_WWPN_LEN;
287 		scsi_u64to8b(port->wwnn, desc->identifier);
288 	}
289 
290 	if (wwpn_valid) {
291 		port->wwpn = wwpn;
292 
293 		free(port->port_devid, M_CTL);
294 
295 		len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
296 		port->port_devid = malloc(sizeof(struct ctl_devid) + len,
297 		    M_CTL, M_WAITOK | M_ZERO);
298 		port->port_devid->len = len;
299 		desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
300 		desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
301 		desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
302 		    SVPD_ID_TYPE_NAA;
303 		desc->length = CTL_WWPN_LEN;
304 		scsi_u64to8b(port->wwpn, desc->identifier);
305 	}
306 }
307 
308 void
309 ctl_port_online(struct ctl_port *port)
310 {
311 	struct ctl_softc *softc = port->ctl_softc;
312 	struct ctl_lun *lun;
313 	const char *value;
314 	uint32_t l;
315 
316 	if (port->lun_enable != NULL) {
317 		if (port->lun_map) {
318 			for (l = 0; l < port->lun_map_size; l++) {
319 				if (ctl_lun_map_from_port(port, l) ==
320 				    UINT32_MAX)
321 					continue;
322 				port->lun_enable(port->targ_lun_arg, l);
323 			}
324 		} else {
325 			STAILQ_FOREACH(lun, &softc->lun_list, links)
326 				port->lun_enable(port->targ_lun_arg, lun->lun);
327 		}
328 	}
329 	if (port->port_online != NULL)
330 		port->port_online(port->onoff_arg);
331 	mtx_lock(&softc->ctl_lock);
332 	if (softc->is_single == 0) {
333 		value = dnvlist_get_string(port->options, "ha_shared", NULL);
334 		if (value != NULL && strcmp(value, "on") == 0)
335 			port->status |= CTL_PORT_STATUS_HA_SHARED;
336 		else
337 			port->status &= ~CTL_PORT_STATUS_HA_SHARED;
338 	}
339 	port->status |= CTL_PORT_STATUS_ONLINE;
340 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
341 		if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
342 			continue;
343 		mtx_lock(&lun->lun_lock);
344 		ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
345 		mtx_unlock(&lun->lun_lock);
346 	}
347 	mtx_unlock(&softc->ctl_lock);
348 	ctl_isc_announce_port(port);
349 }
350 
351 void
352 ctl_port_offline(struct ctl_port *port)
353 {
354 	struct ctl_softc *softc = port->ctl_softc;
355 	struct ctl_lun *lun;
356 	uint32_t l;
357 
358 	if (port->port_offline != NULL)
359 		port->port_offline(port->onoff_arg);
360 	if (port->lun_disable != NULL) {
361 		if (port->lun_map) {
362 			for (l = 0; l < port->lun_map_size; l++) {
363 				if (ctl_lun_map_from_port(port, l) ==
364 				    UINT32_MAX)
365 					continue;
366 				port->lun_disable(port->targ_lun_arg, l);
367 			}
368 		} else {
369 			STAILQ_FOREACH(lun, &softc->lun_list, links)
370 				port->lun_disable(port->targ_lun_arg, lun->lun);
371 		}
372 	}
373 	mtx_lock(&softc->ctl_lock);
374 	port->status &= ~CTL_PORT_STATUS_ONLINE;
375 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
376 		if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
377 			continue;
378 		mtx_lock(&lun->lun_lock);
379 		ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
380 		mtx_unlock(&lun->lun_lock);
381 	}
382 	mtx_unlock(&softc->ctl_lock);
383 	ctl_isc_announce_port(port);
384 }
385 
386 /*
387  * vim: ts=8
388  */
389