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/cdefs.h>
42 __FBSDID("$FreeBSD$");
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>
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>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_da.h>
60 #include <cam/ctl/ctl_io.h>
61 #include <cam/ctl/ctl.h>
62 #include <cam/ctl/ctl_frontend.h>
63 #include <cam/ctl/ctl_backend.h>
64 /* XXX KDM move defines from ctl_ioctl.h to somewhere else */
65 #include <cam/ctl/ctl_ioctl.h>
66 #include <cam/ctl/ctl_ha.h>
67 #include <cam/ctl/ctl_private.h>
68 #include <cam/ctl/ctl_debug.h>
70 extern struct ctl_softc *control_softc;
73 ctl_frontend_register(struct ctl_frontend *fe)
75 struct ctl_softc *softc = control_softc;
76 struct ctl_frontend *fe_tmp;
79 KASSERT(softc != NULL, ("CTL is not initialized"));
81 /* Sanity check, make sure this isn't a duplicate registration. */
82 mtx_lock(&softc->ctl_lock);
83 STAILQ_FOREACH(fe_tmp, &softc->fe_list, links) {
84 if (strcmp(fe_tmp->name, fe->name) == 0) {
85 mtx_unlock(&softc->ctl_lock);
89 mtx_unlock(&softc->ctl_lock);
90 STAILQ_INIT(&fe->port_list);
92 /* Call the frontend's initialization routine. */
93 if (fe->init != NULL) {
94 if ((error = fe->init()) != 0) {
95 printf("%s frontend init error: %d\n",
101 mtx_lock(&softc->ctl_lock);
102 softc->num_frontends++;
103 STAILQ_INSERT_TAIL(&softc->fe_list, fe, links);
104 mtx_unlock(&softc->ctl_lock);
109 ctl_frontend_deregister(struct ctl_frontend *fe)
111 struct ctl_softc *softc = control_softc;
114 /* Call the frontend's shutdown routine.*/
115 if (fe->shutdown != NULL) {
116 if ((error = fe->shutdown()) != 0) {
117 printf("%s frontend shutdown error: %d\n",
123 mtx_lock(&softc->ctl_lock);
124 STAILQ_REMOVE(&softc->fe_list, fe, ctl_frontend, links);
125 softc->num_frontends--;
126 mtx_unlock(&softc->ctl_lock);
130 struct ctl_frontend *
131 ctl_frontend_find(char *frontend_name)
133 struct ctl_softc *softc = control_softc;
134 struct ctl_frontend *fe;
136 mtx_lock(&softc->ctl_lock);
137 STAILQ_FOREACH(fe, &softc->fe_list, links) {
138 if (strcmp(fe->name, frontend_name) == 0) {
139 mtx_unlock(&softc->ctl_lock);
143 mtx_unlock(&softc->ctl_lock);
148 ctl_port_register(struct ctl_port *port)
150 struct ctl_softc *softc = control_softc;
151 struct ctl_port *tport, *nport;
156 KASSERT(softc != NULL, ("CTL is not initialized"));
157 port->ctl_softc = softc;
159 mtx_lock(&softc->ctl_lock);
160 if (port->targ_port >= 0)
161 port_num = port->targ_port;
163 port_num = ctl_ffz(softc->ctl_port_mask,
164 softc->port_min, softc->port_max);
165 if ((port_num < 0) ||
166 (ctl_set_mask(softc->ctl_port_mask, port_num) < 0)) {
167 mtx_unlock(&softc->ctl_lock);
171 mtx_unlock(&softc->ctl_lock);
174 * Initialize the initiator and portname mappings
176 port->max_initiators = CTL_MAX_INIT_PER_PORT;
177 port->wwpn_iid = malloc(sizeof(*port->wwpn_iid) * port->max_initiators,
178 M_CTL, M_NOWAIT | M_ZERO);
179 if (port->wwpn_iid == NULL) {
185 * We add 20 to whatever the caller requests, so he doesn't get
186 * burned by queueing things back to the pending sense queue. In
187 * theory, there should probably only be one outstanding item, at
188 * most, on the pending sense queue for a LUN. We'll clear the
189 * pending sense queue on the next command, whether or not it is
192 retval = ctl_pool_create(softc, port->port_name,
193 port->num_requested_ctl_io + 20, &pool);
195 free(port->wwpn_iid, M_CTL);
197 port->targ_port = -1;
198 mtx_lock(&softc->ctl_lock);
199 ctl_clear_mask(softc->ctl_port_mask, port_num);
200 mtx_unlock(&softc->ctl_lock);
203 port->targ_port = port_num;
204 port->ctl_pool_ref = pool;
205 if (port->options == NULL)
206 port->options = nvlist_create(0);
207 port->stats.item = port_num;
208 mtx_init(&port->port_lock, "CTL port", NULL, MTX_DEF);
210 mtx_lock(&softc->ctl_lock);
211 STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
212 for (tport = NULL, nport = STAILQ_FIRST(&softc->port_list);
213 nport != NULL && nport->targ_port < port_num;
214 tport = nport, nport = STAILQ_NEXT(tport, links)) {
217 STAILQ_INSERT_AFTER(&softc->port_list, tport, port, links);
219 STAILQ_INSERT_HEAD(&softc->port_list, port, links);
220 softc->ctl_ports[port->targ_port] = port;
221 mtx_unlock(&softc->ctl_lock);
227 ctl_port_deregister(struct ctl_port *port)
229 struct ctl_softc *softc = port->ctl_softc;
230 struct ctl_io_pool *pool = (struct ctl_io_pool *)port->ctl_pool_ref;
233 if (port->targ_port == -1)
236 mtx_lock(&softc->ctl_lock);
237 STAILQ_REMOVE(&softc->port_list, port, ctl_port, links);
238 STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
240 ctl_clear_mask(softc->ctl_port_mask, port->targ_port);
241 softc->ctl_ports[port->targ_port] = NULL;
242 mtx_unlock(&softc->ctl_lock);
245 nvlist_destroy(port->options);
247 ctl_lun_map_deinit(port);
248 free(port->port_devid, M_CTL);
249 port->port_devid = NULL;
250 free(port->target_devid, M_CTL);
251 port->target_devid = NULL;
252 free(port->init_devid, M_CTL);
253 port->init_devid = NULL;
254 for (i = 0; i < port->max_initiators; i++)
255 free(port->wwpn_iid[i].name, M_CTL);
256 free(port->wwpn_iid, M_CTL);
257 mtx_destroy(&port->port_lock);
263 ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
264 int wwpn_valid, uint64_t wwpn)
266 struct scsi_vpd_id_descriptor *desc;
269 if (port->port_type == CTL_PORT_FC)
270 proto = SCSI_PROTO_FC << 4;
271 else if (port->port_type == CTL_PORT_SAS)
272 proto = SCSI_PROTO_SAS << 4;
273 else if (port->port_type == CTL_PORT_ISCSI)
274 proto = SCSI_PROTO_ISCSI << 4;
276 proto = SCSI_PROTO_SPI << 4;
281 free(port->target_devid, M_CTL);
283 len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
284 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
285 M_CTL, M_WAITOK | M_ZERO);
286 port->target_devid->len = len;
287 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
288 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
289 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
291 desc->length = CTL_WWPN_LEN;
292 scsi_u64to8b(port->wwnn, desc->identifier);
298 free(port->port_devid, M_CTL);
300 len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
301 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
302 M_CTL, M_WAITOK | M_ZERO);
303 port->port_devid->len = len;
304 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
305 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
306 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
308 desc->length = CTL_WWPN_LEN;
309 scsi_u64to8b(port->wwpn, desc->identifier);
314 ctl_port_online(struct ctl_port *port)
316 struct ctl_softc *softc = port->ctl_softc;
321 if (port->lun_enable != NULL) {
323 for (l = 0; l < port->lun_map_size; l++) {
324 if (ctl_lun_map_from_port(port, l) ==
327 port->lun_enable(port->targ_lun_arg, l);
330 STAILQ_FOREACH(lun, &softc->lun_list, links)
331 port->lun_enable(port->targ_lun_arg, lun->lun);
334 if (port->port_online != NULL)
335 port->port_online(port->onoff_arg);
336 mtx_lock(&softc->ctl_lock);
337 if (softc->is_single == 0) {
338 value = dnvlist_get_string(port->options, "ha_shared", NULL);
339 if (value != NULL && strcmp(value, "on") == 0)
340 port->status |= CTL_PORT_STATUS_HA_SHARED;
342 port->status &= ~CTL_PORT_STATUS_HA_SHARED;
344 port->status |= CTL_PORT_STATUS_ONLINE;
345 STAILQ_FOREACH(lun, &softc->lun_list, links) {
346 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
348 mtx_lock(&lun->lun_lock);
349 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
350 mtx_unlock(&lun->lun_lock);
352 mtx_unlock(&softc->ctl_lock);
353 ctl_isc_announce_port(port);
357 ctl_port_offline(struct ctl_port *port)
359 struct ctl_softc *softc = port->ctl_softc;
363 if (port->port_offline != NULL)
364 port->port_offline(port->onoff_arg);
365 if (port->lun_disable != NULL) {
367 for (l = 0; l < port->lun_map_size; l++) {
368 if (ctl_lun_map_from_port(port, l) ==
371 port->lun_disable(port->targ_lun_arg, l);
374 STAILQ_FOREACH(lun, &softc->lun_list, links)
375 port->lun_disable(port->targ_lun_arg, lun->lun);
378 mtx_lock(&softc->ctl_lock);
379 port->status &= ~CTL_PORT_STATUS_ONLINE;
380 STAILQ_FOREACH(lun, &softc->lun_list, links) {
381 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
383 mtx_lock(&lun->lun_lock);
384 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
385 mtx_unlock(&lun->lun_lock);
387 mtx_unlock(&softc->ctl_lock);
388 ctl_isc_announce_port(port);