]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/ctl/ctl_frontend.c
MFC r288211: Remove some control_softc references.
[FreeBSD/stable/10.git] / sys / cam / ctl / ctl_frontend.c
1 /*-
2  * Copyright (c) 2003 Silicon Graphics International Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
17  * NO WARRANTY
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.
29  *
30  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.c#4 $
31  */
32 /*
33  * CAM Target Layer front end interface code
34  *
35  * Author: Ken Merry <ken@FreeBSD.org>
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
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>
46 #include <sys/lock.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>
52
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_backend.h>
59 /* XXX KDM move defines from ctl_ioctl.h to somewhere else */
60 #include <cam/ctl/ctl_ioctl.h>
61 #include <cam/ctl/ctl_ha.h>
62 #include <cam/ctl/ctl_private.h>
63 #include <cam/ctl/ctl_debug.h>
64
65 extern struct ctl_softc *control_softc;
66
67 int
68 ctl_frontend_register(struct ctl_frontend *fe)
69 {
70         struct ctl_softc *softc = control_softc;
71         struct ctl_frontend *fe_tmp;
72
73         KASSERT(softc != NULL, ("CTL is not initialized"));
74
75         /*
76          * Sanity check, make sure this isn't a duplicate registration.
77          */
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);
82                         return (-1);
83                 }
84         }
85         mtx_unlock(&softc->ctl_lock);
86         STAILQ_INIT(&fe->port_list);
87
88         /*
89          * Call the frontend's initialization routine.
90          */
91         if (fe->init != NULL)
92                 fe->init();
93
94         mtx_lock(&softc->ctl_lock);
95         softc->num_frontends++;
96         STAILQ_INSERT_TAIL(&softc->fe_list, fe, links);
97         mtx_unlock(&softc->ctl_lock);
98         return (0);
99 }
100
101 int
102 ctl_frontend_deregister(struct ctl_frontend *fe)
103 {
104         struct ctl_softc *softc = control_softc;
105
106         if (!STAILQ_EMPTY(&fe->port_list))
107                 return (-1);
108
109         mtx_lock(&softc->ctl_lock);
110         STAILQ_REMOVE(&softc->fe_list, fe, ctl_frontend, links);
111         softc->num_frontends--;
112         mtx_unlock(&softc->ctl_lock);
113
114         /*
115          * Call the frontend's shutdown routine.
116          */
117         if (fe->shutdown != NULL)
118                 fe->shutdown();
119         return (0);
120 }
121
122 struct ctl_frontend *
123 ctl_frontend_find(char *frontend_name)
124 {
125         struct ctl_softc *softc = control_softc;
126         struct ctl_frontend *fe;
127
128         mtx_lock(&softc->ctl_lock);
129         STAILQ_FOREACH(fe, &softc->fe_list, links) {
130                 if (strcmp(fe->name, frontend_name) == 0) {
131                         mtx_unlock(&softc->ctl_lock);
132                         return (fe);
133                 }
134         }
135         mtx_unlock(&softc->ctl_lock);
136         return (NULL);
137 }
138
139 int
140 ctl_port_register(struct ctl_port *port)
141 {
142         struct ctl_softc *softc = control_softc;
143         struct ctl_port *tport, *nport;
144         void *pool;
145         int port_num;
146         int retval;
147
148         retval = 0;
149
150         KASSERT(softc != NULL, ("CTL is not initialized"));
151         port->ctl_softc = softc;
152
153         mtx_lock(&softc->ctl_lock);
154         if (port->targ_port >= 0)
155                 port_num = port->targ_port;
156         else
157                 port_num = ctl_ffz(softc->ctl_port_mask,
158                     softc->port_min, softc->port_max);
159         if ((port_num < 0) ||
160             (ctl_set_mask(softc->ctl_port_mask, port_num) < 0)) {
161                 mtx_unlock(&softc->ctl_lock);
162                 return (1);
163         }
164         softc->num_ports++;
165         mtx_unlock(&softc->ctl_lock);
166
167         /*
168          * Initialize the initiator and portname mappings
169          */
170         port->max_initiators = CTL_MAX_INIT_PER_PORT;
171         port->wwpn_iid = malloc(sizeof(*port->wwpn_iid) * port->max_initiators,
172             M_CTL, M_NOWAIT | M_ZERO);
173         if (port->wwpn_iid == NULL) {
174                 retval = ENOMEM;
175                 goto error;
176         }
177
178         /*
179          * We add 20 to whatever the caller requests, so he doesn't get
180          * burned by queueing things back to the pending sense queue.  In
181          * theory, there should probably only be one outstanding item, at
182          * most, on the pending sense queue for a LUN.  We'll clear the
183          * pending sense queue on the next command, whether or not it is
184          * a REQUEST SENSE.
185          */
186         retval = ctl_pool_create(softc, port->port_name,
187                                  port->num_requested_ctl_io + 20, &pool);
188         if (retval != 0) {
189                 free(port->wwpn_iid, M_CTL);
190 error:
191                 port->targ_port = -1;
192                 mtx_lock(&softc->ctl_lock);
193                 ctl_clear_mask(softc->ctl_port_mask, port_num);
194                 mtx_unlock(&softc->ctl_lock);
195                 return (retval);
196         }
197         port->ctl_pool_ref = pool;
198
199         if (port->options.stqh_first == NULL)
200                 STAILQ_INIT(&port->options);
201
202         mtx_lock(&softc->ctl_lock);
203         port->targ_port = port_num;
204         STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
205         for (tport = NULL, nport = STAILQ_FIRST(&softc->port_list);
206             nport != NULL && nport->targ_port < port_num;
207             tport = nport, nport = STAILQ_NEXT(tport, links)) {
208         }
209         if (tport)
210                 STAILQ_INSERT_AFTER(&softc->port_list, tport, port, links);
211         else
212                 STAILQ_INSERT_HEAD(&softc->port_list, port, links);
213         softc->ctl_ports[port->targ_port] = port;
214         mtx_unlock(&softc->ctl_lock);
215
216         return (retval);
217 }
218
219 int
220 ctl_port_deregister(struct ctl_port *port)
221 {
222         struct ctl_softc *softc = port->ctl_softc;
223         struct ctl_io_pool *pool;
224         int retval, i;
225
226         retval = 0;
227
228         pool = (struct ctl_io_pool *)port->ctl_pool_ref;
229
230         if (port->targ_port == -1) {
231                 retval = 1;
232                 goto bailout;
233         }
234
235         mtx_lock(&softc->ctl_lock);
236         STAILQ_REMOVE(&softc->port_list, port, ctl_port, links);
237         STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
238         softc->num_ports--;
239         ctl_clear_mask(softc->ctl_port_mask, port->targ_port);
240         softc->ctl_ports[port->targ_port] = NULL;
241         mtx_unlock(&softc->ctl_lock);
242
243         ctl_pool_free(pool);
244         ctl_free_opts(&port->options);
245
246         ctl_lun_map_deinit(port);
247         free(port->port_devid, M_CTL);
248         port->port_devid = NULL;
249         free(port->target_devid, M_CTL);
250         port->target_devid = NULL;
251         free(port->init_devid, M_CTL);
252         port->init_devid = NULL;
253         for (i = 0; i < port->max_initiators; i++)
254                 free(port->wwpn_iid[i].name, M_CTL);
255         free(port->wwpn_iid, M_CTL);
256
257 bailout:
258         return (retval);
259 }
260
261 void
262 ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
263                       int wwpn_valid, uint64_t wwpn)
264 {
265         struct scsi_vpd_id_descriptor *desc;
266         int len, proto;
267
268         if (port->port_type == CTL_PORT_FC)
269                 proto = SCSI_PROTO_FC << 4;
270         else if (port->port_type == CTL_PORT_ISCSI)
271                 proto = SCSI_PROTO_ISCSI << 4;
272         else
273                 proto = SCSI_PROTO_SPI << 4;
274
275         if (wwnn_valid) {
276                 port->wwnn = wwnn;
277
278                 free(port->target_devid, M_CTL);
279
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 |
287                     SVPD_ID_TYPE_NAA;
288                 desc->length = CTL_WWPN_LEN;
289                 scsi_u64to8b(port->wwnn, desc->identifier);
290         }
291
292         if (wwpn_valid) {
293                 port->wwpn = wwpn;
294
295                 free(port->port_devid, M_CTL);
296
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 |
304                     SVPD_ID_TYPE_NAA;
305                 desc->length = CTL_WWPN_LEN;
306                 scsi_u64to8b(port->wwpn, desc->identifier);
307         }
308 }
309
310 void
311 ctl_port_online(struct ctl_port *port)
312 {
313         struct ctl_softc *softc = port->ctl_softc;
314         struct ctl_lun *lun;
315         uint32_t l;
316
317         if (port->lun_enable != NULL) {
318                 if (port->lun_map) {
319                         for (l = 0; l < CTL_MAX_LUNS; l++) {
320                                 if (ctl_lun_map_from_port(port, l) >=
321                                     CTL_MAX_LUNS)
322                                         continue;
323                                 port->lun_enable(port->targ_lun_arg, l);
324                         }
325                 } else {
326                         STAILQ_FOREACH(lun, &softc->lun_list, links)
327                                 port->lun_enable(port->targ_lun_arg, lun->lun);
328                 }
329         }
330         if (port->port_online != NULL)
331                 port->port_online(port->onoff_arg);
332         mtx_lock(&softc->ctl_lock);
333         port->status |= CTL_PORT_STATUS_ONLINE;
334         STAILQ_FOREACH(lun, &softc->lun_list, links) {
335                 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
336                         continue;
337                 mtx_lock(&lun->lun_lock);
338                 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
339                 mtx_unlock(&lun->lun_lock);
340         }
341         mtx_unlock(&softc->ctl_lock);
342         ctl_isc_announce_port(port);
343 }
344
345 void
346 ctl_port_offline(struct ctl_port *port)
347 {
348         struct ctl_softc *softc = port->ctl_softc;
349         struct ctl_lun *lun;
350         uint32_t l;
351
352         if (port->port_offline != NULL)
353                 port->port_offline(port->onoff_arg);
354         if (port->lun_disable != NULL) {
355                 if (port->lun_map) {
356                         for (l = 0; l < CTL_MAX_LUNS; l++) {
357                                 if (ctl_lun_map_from_port(port, l) >=
358                                     CTL_MAX_LUNS)
359                                         continue;
360                                 port->lun_disable(port->targ_lun_arg, l);
361                         }
362                 } else {
363                         STAILQ_FOREACH(lun, &softc->lun_list, links)
364                                 port->lun_disable(port->targ_lun_arg, lun->lun);
365                 }
366         }
367         mtx_lock(&softc->ctl_lock);
368         port->status &= ~CTL_PORT_STATUS_ONLINE;
369         STAILQ_FOREACH(lun, &softc->lun_list, links) {
370                 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
371                         continue;
372                 mtx_lock(&lun->lun_lock);
373                 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
374                 mtx_unlock(&lun->lun_lock);
375         }
376         mtx_unlock(&softc->ctl_lock);
377         ctl_isc_announce_port(port);
378 }
379
380 /*
381  * vim: ts=8
382  */