2 * Copyright (c) 2003 Silicon Graphics International Corp.
3 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
31 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.c#4 $
34 * CAM Target Layer front end interface code
36 * Author: Ken Merry <ken@FreeBSD.org>
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
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>
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>
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>
66 extern struct ctl_softc *control_softc;
69 ctl_frontend_register(struct ctl_frontend *fe)
71 struct ctl_softc *softc = control_softc;
72 struct ctl_frontend *fe_tmp;
75 KASSERT(softc != NULL, ("CTL is not initialized"));
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);
85 mtx_unlock(&softc->ctl_lock);
86 STAILQ_INIT(&fe->port_list);
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",
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);
105 ctl_frontend_deregister(struct ctl_frontend *fe)
107 struct ctl_softc *softc = control_softc;
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",
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);
126 struct ctl_frontend *
127 ctl_frontend_find(char *frontend_name)
129 struct ctl_softc *softc = control_softc;
130 struct ctl_frontend *fe;
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);
139 mtx_unlock(&softc->ctl_lock);
144 ctl_port_register(struct ctl_port *port)
146 struct ctl_softc *softc = control_softc;
147 struct ctl_port *tport, *nport;
152 KASSERT(softc != NULL, ("CTL is not initialized"));
153 port->ctl_softc = softc;
155 mtx_lock(&softc->ctl_lock);
156 if (port->targ_port >= 0)
157 port_num = port->targ_port;
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);
167 mtx_unlock(&softc->ctl_lock);
170 * Initialize the initiator and portname mappings
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) {
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
188 retval = ctl_pool_create(softc, port->port_name,
189 port->num_requested_ctl_io + 20, &pool);
191 free(port->wwpn_iid, M_CTL);
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);
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);
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)) {
213 STAILQ_INSERT_AFTER(&softc->port_list, tport, port, links);
215 STAILQ_INSERT_HEAD(&softc->port_list, port, links);
216 softc->ctl_ports[port->targ_port] = port;
217 mtx_unlock(&softc->ctl_lock);
223 ctl_port_deregister(struct ctl_port *port)
225 struct ctl_softc *softc = port->ctl_softc;
226 struct ctl_io_pool *pool = (struct ctl_io_pool *)port->ctl_pool_ref;
229 if (port->targ_port == -1)
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);
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);
241 ctl_free_opts(&port->options);
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);
259 ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
260 int wwpn_valid, uint64_t wwpn)
262 struct scsi_vpd_id_descriptor *desc;
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;
272 proto = SCSI_PROTO_SPI << 4;
277 free(port->target_devid, M_CTL);
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 |
287 desc->length = CTL_WWPN_LEN;
288 scsi_u64to8b(port->wwnn, desc->identifier);
294 free(port->port_devid, M_CTL);
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 |
304 desc->length = CTL_WWPN_LEN;
305 scsi_u64to8b(port->wwpn, desc->identifier);
310 ctl_port_online(struct ctl_port *port)
312 struct ctl_softc *softc = port->ctl_softc;
317 if (port->lun_enable != NULL) {
319 for (l = 0; l < port->lun_map_size; l++) {
320 if (ctl_lun_map_from_port(port, l) ==
323 port->lun_enable(port->targ_lun_arg, l);
326 STAILQ_FOREACH(lun, &softc->lun_list, links)
327 port->lun_enable(port->targ_lun_arg, lun->lun);
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;
338 port->status &= ~CTL_PORT_STATUS_HA_SHARED;
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)
344 mtx_lock(&lun->lun_lock);
345 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
346 mtx_unlock(&lun->lun_lock);
348 mtx_unlock(&softc->ctl_lock);
349 ctl_isc_announce_port(port);
353 ctl_port_offline(struct ctl_port *port)
355 struct ctl_softc *softc = port->ctl_softc;
359 if (port->port_offline != NULL)
360 port->port_offline(port->onoff_arg);
361 if (port->lun_disable != NULL) {
363 for (l = 0; l < port->lun_map_size; l++) {
364 if (ctl_lun_map_from_port(port, l) ==
367 port->lun_disable(port->targ_lun_arg, l);
370 STAILQ_FOREACH(lun, &softc->lun_list, links)
371 port->lun_disable(port->targ_lun_arg, lun->lun);
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)
379 mtx_lock(&lun->lun_lock);
380 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
381 mtx_unlock(&lun->lun_lock);
383 mtx_unlock(&softc->ctl_lock);
384 ctl_isc_announce_port(port);