2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
56 #include <cam/scsi/scsi_all.h>
57 #include <cam/scsi/scsi_da.h>
58 #include <cam/ctl/ctl_io.h>
59 #include <cam/ctl/ctl.h>
60 #include <cam/ctl/ctl_frontend.h>
61 #include <cam/ctl/ctl_backend.h>
62 /* XXX KDM move defines from ctl_ioctl.h to somewhere else */
63 #include <cam/ctl/ctl_ioctl.h>
64 #include <cam/ctl/ctl_ha.h>
65 #include <cam/ctl/ctl_private.h>
66 #include <cam/ctl/ctl_debug.h>
68 extern struct ctl_softc *control_softc;
71 ctl_frontend_register(struct ctl_frontend *fe)
73 struct ctl_softc *softc = control_softc;
74 struct ctl_frontend *fe_tmp;
77 KASSERT(softc != NULL, ("CTL is not initialized"));
79 /* Sanity check, make sure this isn't a duplicate registration. */
80 mtx_lock(&softc->ctl_lock);
81 STAILQ_FOREACH(fe_tmp, &softc->fe_list, links) {
82 if (strcmp(fe_tmp->name, fe->name) == 0) {
83 mtx_unlock(&softc->ctl_lock);
87 mtx_unlock(&softc->ctl_lock);
88 STAILQ_INIT(&fe->port_list);
90 /* Call the frontend's initialization routine. */
91 if (fe->init != NULL) {
92 if ((error = fe->init()) != 0) {
93 printf("%s frontend init error: %d\n",
99 mtx_lock(&softc->ctl_lock);
100 softc->num_frontends++;
101 STAILQ_INSERT_TAIL(&softc->fe_list, fe, links);
102 mtx_unlock(&softc->ctl_lock);
107 ctl_frontend_deregister(struct ctl_frontend *fe)
109 struct ctl_softc *softc = control_softc;
112 /* Call the frontend's shutdown routine.*/
113 if (fe->shutdown != NULL) {
114 if ((error = fe->shutdown()) != 0) {
115 printf("%s frontend shutdown error: %d\n",
121 mtx_lock(&softc->ctl_lock);
122 STAILQ_REMOVE(&softc->fe_list, fe, ctl_frontend, links);
123 softc->num_frontends--;
124 mtx_unlock(&softc->ctl_lock);
128 struct ctl_frontend *
129 ctl_frontend_find(char *frontend_name)
131 struct ctl_softc *softc = control_softc;
132 struct ctl_frontend *fe;
134 mtx_lock(&softc->ctl_lock);
135 STAILQ_FOREACH(fe, &softc->fe_list, links) {
136 if (strcmp(fe->name, frontend_name) == 0) {
137 mtx_unlock(&softc->ctl_lock);
141 mtx_unlock(&softc->ctl_lock);
146 ctl_port_register(struct ctl_port *port)
148 struct ctl_softc *softc = control_softc;
149 struct ctl_port *tport, *nport;
154 KASSERT(softc != NULL, ("CTL is not initialized"));
155 port->ctl_softc = softc;
157 mtx_lock(&softc->ctl_lock);
158 if (port->targ_port >= 0)
159 port_num = port->targ_port;
161 port_num = ctl_ffz(softc->ctl_port_mask,
162 softc->port_min, softc->port_max);
163 if ((port_num < 0) ||
164 (ctl_set_mask(softc->ctl_port_mask, port_num) < 0)) {
165 mtx_unlock(&softc->ctl_lock);
169 mtx_unlock(&softc->ctl_lock);
172 * Initialize the initiator and portname mappings
174 port->max_initiators = CTL_MAX_INIT_PER_PORT;
175 port->wwpn_iid = malloc(sizeof(*port->wwpn_iid) * port->max_initiators,
176 M_CTL, M_NOWAIT | M_ZERO);
177 if (port->wwpn_iid == NULL) {
183 * We add 20 to whatever the caller requests, so he doesn't get
184 * burned by queueing things back to the pending sense queue. In
185 * theory, there should probably only be one outstanding item, at
186 * most, on the pending sense queue for a LUN. We'll clear the
187 * pending sense queue on the next command, whether or not it is
190 retval = ctl_pool_create(softc, port->port_name,
191 port->num_requested_ctl_io + 20, &pool);
193 free(port->wwpn_iid, M_CTL);
195 port->targ_port = -1;
196 mtx_lock(&softc->ctl_lock);
197 ctl_clear_mask(softc->ctl_port_mask, port_num);
198 mtx_unlock(&softc->ctl_lock);
201 port->targ_port = port_num;
202 port->ctl_pool_ref = pool;
203 if (port->options.stqh_first == NULL)
204 STAILQ_INIT(&port->options);
205 port->stats.item = port_num;
206 mtx_init(&port->port_lock, "CTL port", NULL, MTX_DEF);
208 mtx_lock(&softc->ctl_lock);
209 STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
210 for (tport = NULL, nport = STAILQ_FIRST(&softc->port_list);
211 nport != NULL && nport->targ_port < port_num;
212 tport = nport, nport = STAILQ_NEXT(tport, links)) {
215 STAILQ_INSERT_AFTER(&softc->port_list, tport, port, links);
217 STAILQ_INSERT_HEAD(&softc->port_list, port, links);
218 softc->ctl_ports[port->targ_port] = port;
219 mtx_unlock(&softc->ctl_lock);
225 ctl_port_deregister(struct ctl_port *port)
227 struct ctl_softc *softc = port->ctl_softc;
228 struct ctl_io_pool *pool = (struct ctl_io_pool *)port->ctl_pool_ref;
231 if (port->targ_port == -1)
234 mtx_lock(&softc->ctl_lock);
235 STAILQ_REMOVE(&softc->port_list, port, ctl_port, links);
236 STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
238 ctl_clear_mask(softc->ctl_port_mask, port->targ_port);
239 softc->ctl_ports[port->targ_port] = NULL;
240 mtx_unlock(&softc->ctl_lock);
243 ctl_free_opts(&port->options);
245 ctl_lun_map_deinit(port);
246 free(port->port_devid, M_CTL);
247 port->port_devid = NULL;
248 free(port->target_devid, M_CTL);
249 port->target_devid = NULL;
250 free(port->init_devid, M_CTL);
251 port->init_devid = NULL;
252 for (i = 0; i < port->max_initiators; i++)
253 free(port->wwpn_iid[i].name, M_CTL);
254 free(port->wwpn_iid, M_CTL);
255 mtx_destroy(&port->port_lock);
261 ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
262 int wwpn_valid, uint64_t wwpn)
264 struct scsi_vpd_id_descriptor *desc;
267 if (port->port_type == CTL_PORT_FC)
268 proto = SCSI_PROTO_FC << 4;
269 else if (port->port_type == CTL_PORT_SAS)
270 proto = SCSI_PROTO_SAS << 4;
271 else if (port->port_type == CTL_PORT_ISCSI)
272 proto = SCSI_PROTO_ISCSI << 4;
274 proto = SCSI_PROTO_SPI << 4;
279 free(port->target_devid, M_CTL);
281 len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
282 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
283 M_CTL, M_WAITOK | M_ZERO);
284 port->target_devid->len = len;
285 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
286 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
287 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
289 desc->length = CTL_WWPN_LEN;
290 scsi_u64to8b(port->wwnn, desc->identifier);
296 free(port->port_devid, M_CTL);
298 len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
299 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
300 M_CTL, M_WAITOK | M_ZERO);
301 port->port_devid->len = len;
302 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
303 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
304 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
306 desc->length = CTL_WWPN_LEN;
307 scsi_u64to8b(port->wwpn, desc->identifier);
312 ctl_port_online(struct ctl_port *port)
314 struct ctl_softc *softc = port->ctl_softc;
319 if (port->lun_enable != NULL) {
321 for (l = 0; l < port->lun_map_size; l++) {
322 if (ctl_lun_map_from_port(port, l) ==
325 port->lun_enable(port->targ_lun_arg, l);
328 STAILQ_FOREACH(lun, &softc->lun_list, links)
329 port->lun_enable(port->targ_lun_arg, lun->lun);
332 if (port->port_online != NULL)
333 port->port_online(port->onoff_arg);
334 mtx_lock(&softc->ctl_lock);
335 if (softc->is_single == 0) {
336 value = ctl_get_opt(&port->options, "ha_shared");
337 if (value != NULL && strcmp(value, "on") == 0)
338 port->status |= CTL_PORT_STATUS_HA_SHARED;
340 port->status &= ~CTL_PORT_STATUS_HA_SHARED;
342 port->status |= CTL_PORT_STATUS_ONLINE;
343 STAILQ_FOREACH(lun, &softc->lun_list, links) {
344 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
346 mtx_lock(&lun->lun_lock);
347 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
348 mtx_unlock(&lun->lun_lock);
350 mtx_unlock(&softc->ctl_lock);
351 ctl_isc_announce_port(port);
355 ctl_port_offline(struct ctl_port *port)
357 struct ctl_softc *softc = port->ctl_softc;
361 if (port->port_offline != NULL)
362 port->port_offline(port->onoff_arg);
363 if (port->lun_disable != NULL) {
365 for (l = 0; l < port->lun_map_size; l++) {
366 if (ctl_lun_map_from_port(port, l) ==
369 port->lun_disable(port->targ_lun_arg, l);
372 STAILQ_FOREACH(lun, &softc->lun_list, links)
373 port->lun_disable(port->targ_lun_arg, lun->lun);
376 mtx_lock(&softc->ctl_lock);
377 port->status &= ~CTL_PORT_STATUS_ONLINE;
378 STAILQ_FOREACH(lun, &softc->lun_list, links) {
379 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
381 mtx_lock(&lun->lun_lock);
382 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
383 mtx_unlock(&lun->lun_lock);
385 mtx_unlock(&softc->ctl_lock);
386 ctl_isc_announce_port(port);