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