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