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