2 * Copyright (c) 2003 Silicon Graphics International Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * substantially similar to the "NO WARRANTY" disclaimer below
13 * ("Disclaimer") and any redistribution must be conditioned upon
14 * including a substantially similar Disclaimer requirement for further
15 * binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
30 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.c#4 $
33 * CAM Target Layer front end interface code
35 * Author: Ken Merry <ken@FreeBSD.org>
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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>
53 #include <cam/scsi/scsi_all.h>
54 #include <cam/scsi/scsi_da.h>
55 #include <cam/ctl/ctl_io.h>
56 #include <cam/ctl/ctl.h>
57 #include <cam/ctl/ctl_frontend.h>
58 #include <cam/ctl/ctl_frontend_internal.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_frontend *fe_tmp;
73 KASSERT(control_softc != NULL, ("CTL is not initialized"));
76 * Sanity check, make sure this isn't a duplicate registration.
78 mtx_lock(&control_softc->ctl_lock);
79 STAILQ_FOREACH(fe_tmp, &control_softc->fe_list, links) {
80 if (strcmp(fe_tmp->name, fe->name) == 0) {
81 mtx_unlock(&control_softc->ctl_lock);
85 mtx_unlock(&control_softc->ctl_lock);
86 STAILQ_INIT(&fe->port_list);
89 * Call the frontend's initialization routine.
94 mtx_lock(&control_softc->ctl_lock);
95 control_softc->num_frontends++;
96 STAILQ_INSERT_TAIL(&control_softc->fe_list, fe, links);
97 mtx_unlock(&control_softc->ctl_lock);
102 ctl_frontend_deregister(struct ctl_frontend *fe)
105 if (!STAILQ_EMPTY(&fe->port_list))
108 mtx_lock(&control_softc->ctl_lock);
109 STAILQ_REMOVE(&control_softc->fe_list, fe, ctl_frontend, links);
110 control_softc->num_frontends--;
111 mtx_unlock(&control_softc->ctl_lock);
114 * Call the frontend's shutdown routine.
116 if (fe->shutdown != NULL)
121 struct ctl_frontend *
122 ctl_frontend_find(char *frontend_name)
124 struct ctl_softc *ctl_softc = control_softc;
125 struct ctl_frontend *fe;
127 mtx_lock(&ctl_softc->ctl_lock);
128 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
129 if (strcmp(fe->name, frontend_name) == 0) {
130 mtx_unlock(&ctl_softc->ctl_lock);
134 mtx_unlock(&ctl_softc->ctl_lock);
139 ctl_port_register(struct ctl_port *port, int master_shelf)
141 struct ctl_io_pool *pool;
147 KASSERT(control_softc != NULL, ("CTL is not initialized"));
149 mtx_lock(&control_softc->ctl_lock);
150 port_num = ctl_ffz(&control_softc->ctl_port_mask, CTL_MAX_PORTS);
152 || (ctl_set_mask(&control_softc->ctl_port_mask, port_num) == -1)) {
153 port->targ_port = -1;
154 mtx_unlock(&control_softc->ctl_lock);
157 control_softc->num_ports++;
158 mtx_unlock(&control_softc->ctl_lock);
161 * We add 20 to whatever the caller requests, so he doesn't get
162 * burned by queueing things back to the pending sense queue. In
163 * theory, there should probably only be one outstanding item, at
164 * most, on the pending sense queue for a LUN. We'll clear the
165 * pending sense queue on the next command, whether or not it is
168 retval = ctl_pool_create(control_softc, CTL_POOL_FETD,
169 port->num_requested_ctl_io + 20, &pool);
171 port->targ_port = -1;
172 mtx_lock(&control_softc->ctl_lock);
173 ctl_clear_mask(&control_softc->ctl_port_mask, port_num);
174 mtx_unlock(&control_softc->ctl_lock);
177 port->ctl_pool_ref = pool;
179 if (port->options.stqh_first == NULL)
180 STAILQ_INIT(&port->options);
182 mtx_lock(&control_softc->ctl_lock);
183 port->targ_port = port_num + (master_shelf != 0 ? 0 : CTL_MAX_PORTS);
184 port->max_initiators = CTL_MAX_INIT_PER_PORT;
185 STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
186 STAILQ_INSERT_TAIL(&control_softc->port_list, port, links);
187 control_softc->ctl_ports[port_num] = port;
188 mtx_unlock(&control_softc->ctl_lock);
194 ctl_port_deregister(struct ctl_port *port)
196 struct ctl_io_pool *pool;
202 pool = (struct ctl_io_pool *)port->ctl_pool_ref;
204 if (port->targ_port == -1) {
209 mtx_lock(&control_softc->ctl_lock);
210 STAILQ_REMOVE(&control_softc->port_list, port, ctl_port, links);
211 STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
212 control_softc->num_ports--;
213 port_num = (port->targ_port < CTL_MAX_PORTS) ? port->targ_port :
214 port->targ_port - CTL_MAX_PORTS;
215 ctl_clear_mask(&control_softc->ctl_port_mask, port_num);
216 control_softc->ctl_ports[port_num] = NULL;
217 mtx_unlock(&control_softc->ctl_lock);
220 ctl_free_opts(&port->options);
227 ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
228 int wwpn_valid, uint64_t wwpn)
238 ctl_port_online(struct ctl_port *port)
240 port->port_online(port->onoff_arg);
241 /* XXX KDM need a lock here? */
242 port->status |= CTL_PORT_STATUS_ONLINE;
246 ctl_port_offline(struct ctl_port *port)
248 port->port_offline(port->onoff_arg);
249 /* XXX KDM need a lock here? */
250 port->status &= ~CTL_PORT_STATUS_ONLINE;