2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003 Silicon Graphics International Corp.
5 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
33 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.c#4 $
36 * CAM Target Layer front end interface code
38 * Author: Ken Merry <ken@FreeBSD.org>
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>
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>
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>
67 extern struct ctl_softc *control_softc;
70 ctl_frontend_register(struct ctl_frontend *fe)
72 struct ctl_softc *softc = control_softc;
73 struct ctl_frontend *fe_tmp;
76 KASSERT(softc != NULL, ("CTL is not initialized"));
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);
86 mtx_unlock(&softc->ctl_lock);
87 STAILQ_INIT(&fe->port_list);
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",
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);
106 ctl_frontend_deregister(struct ctl_frontend *fe)
108 struct ctl_softc *softc = control_softc;
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",
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);
127 struct ctl_frontend *
128 ctl_frontend_find(char *frontend_name)
130 struct ctl_softc *softc = control_softc;
131 struct ctl_frontend *fe;
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);
140 mtx_unlock(&softc->ctl_lock);
145 ctl_port_register(struct ctl_port *port)
147 struct ctl_softc *softc = control_softc;
148 struct ctl_port *tport, *nport;
153 KASSERT(softc != NULL, ("CTL is not initialized"));
154 port->ctl_softc = softc;
156 mtx_lock(&softc->ctl_lock);
157 if (port->targ_port >= 0)
158 port_num = port->targ_port;
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);
168 mtx_unlock(&softc->ctl_lock);
171 * Initialize the initiator and portname mappings
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) {
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
189 retval = ctl_pool_create(softc, port->port_name,
190 port->num_requested_ctl_io + 20, &pool);
192 free(port->wwpn_iid, M_CTL);
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);
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);
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)) {
214 STAILQ_INSERT_AFTER(&softc->port_list, tport, port, links);
216 STAILQ_INSERT_HEAD(&softc->port_list, port, links);
217 softc->ctl_ports[port->targ_port] = port;
218 mtx_unlock(&softc->ctl_lock);
224 ctl_port_deregister(struct ctl_port *port)
226 struct ctl_softc *softc = port->ctl_softc;
227 struct ctl_io_pool *pool = (struct ctl_io_pool *)port->ctl_pool_ref;
230 if (port->targ_port == -1)
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);
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);
242 nvlist_destroy(port->options);
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);
260 ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
261 int wwpn_valid, uint64_t wwpn)
263 struct scsi_vpd_id_descriptor *desc;
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;
273 proto = SCSI_PROTO_SPI << 4;
278 free(port->target_devid, M_CTL);
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 |
288 desc->length = CTL_WWPN_LEN;
289 scsi_u64to8b(port->wwnn, desc->identifier);
295 free(port->port_devid, M_CTL);
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 |
305 desc->length = CTL_WWPN_LEN;
306 scsi_u64to8b(port->wwpn, desc->identifier);
311 ctl_port_online(struct ctl_port *port)
313 struct ctl_softc *softc = port->ctl_softc;
318 if (port->lun_enable != NULL) {
320 for (l = 0; l < port->lun_map_size; l++) {
321 if (ctl_lun_map_from_port(port, l) ==
324 port->lun_enable(port->targ_lun_arg, l);
327 STAILQ_FOREACH(lun, &softc->lun_list, links)
328 port->lun_enable(port->targ_lun_arg, lun->lun);
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;
339 port->status &= ~CTL_PORT_STATUS_HA_SHARED;
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)
345 mtx_lock(&lun->lun_lock);
346 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
347 mtx_unlock(&lun->lun_lock);
349 mtx_unlock(&softc->ctl_lock);
350 ctl_isc_announce_port(port);
354 ctl_port_offline(struct ctl_port *port)
356 struct ctl_softc *softc = port->ctl_softc;
360 if (port->port_offline != NULL)
361 port->port_offline(port->onoff_arg);
362 if (port->lun_disable != NULL) {
364 for (l = 0; l < port->lun_map_size; l++) {
365 if (ctl_lun_map_from_port(port, l) ==
368 port->lun_disable(port->targ_lun_arg, l);
371 STAILQ_FOREACH(lun, &softc->lun_list, links)
372 port->lun_disable(port->targ_lun_arg, lun->lun);
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)
380 mtx_lock(&lun->lun_lock);
381 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
382 mtx_unlock(&lun->lun_lock);
384 mtx_unlock(&softc->ctl_lock);
385 ctl_isc_announce_port(port);