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