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_backend.c#3 $
31 */
32 /*
33 * CTL backend driver registration routines
34 *
35 * Author: Ken Merry <ken@FreeBSD.org>
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: stable/9/sys/cam/ctl/ctl_backend.c 237941 2012-07-02 02:36:05Z ken $");
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/queue.h>
50 #include <sys/sysctl.h>
51
52 #include <cam/scsi/scsi_all.h>
53 #include <cam/scsi/scsi_da.h>
54 #include <cam/ctl/ctl_io.h>
55 #include <cam/ctl/ctl.h>
56 #include <cam/ctl/ctl_frontend.h>
57 #include <cam/ctl/ctl_backend.h>
58 #include <cam/ctl/ctl_frontend_internal.h>
59 #include <cam/ctl/ctl_ioctl.h>
60 #include <cam/ctl/ctl_ha.h>
61 #include <cam/ctl/ctl_private.h>
62 #include <cam/ctl/ctl_debug.h>
63
64 extern struct ctl_softc *control_softc;
65 extern int ctl_disable;
66
67 int
ctl_backend_register(struct ctl_backend_driver * be)68 ctl_backend_register(struct ctl_backend_driver *be)
69 {
70 struct ctl_softc *ctl_softc;
71 struct ctl_backend_driver *be_tmp;
72
73 ctl_softc = control_softc;
74
75 /* Don't continue if CTL is disabled */
76 if (ctl_disable != 0)
77 return (0);
78
79 mtx_lock(&ctl_softc->ctl_lock);
80 /*
81 * Sanity check, make sure this isn't a duplicate registration.
82 */
83 STAILQ_FOREACH(be_tmp, &ctl_softc->be_list, links) {
84 if (strcmp(be_tmp->name, be->name) == 0) {
85 mtx_unlock(&ctl_softc->ctl_lock);
86 return (-1);
87 }
88 }
89 mtx_unlock(&ctl_softc->ctl_lock);
90
91 /*
92 * Call the backend's initialization routine.
93 */
94 be->init();
95
96 mtx_lock(&ctl_softc->ctl_lock);
97
98 STAILQ_INSERT_TAIL(&ctl_softc->be_list, be, links);
99
100 ctl_softc->num_backends++;
101
102 /*
103 * Don't want to increment the usage count for internal consumers,
104 * we won't be able to unload otherwise.
105 */
106 /* XXX KDM find a substitute for this? */
107 #if 0
108 if ((be->flags & CTL_BE_FLAG_INTERNAL) == 0)
109 MOD_INC_USE_COUNT;
110 #endif
111
112 #ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
113 be->config_move_done = ctl_config_move_done;
114 #endif
115 /* XXX KDM fix this! */
116 be->num_luns = 0;
117 #if 0
118 atomic_set(&be->num_luns, 0);
119 #endif
120
121 mtx_unlock(&ctl_softc->ctl_lock);
122
123 return (0);
124 }
125
126 int
ctl_backend_deregister(struct ctl_backend_driver * be)127 ctl_backend_deregister(struct ctl_backend_driver *be)
128 {
129 struct ctl_softc *ctl_softc;
130
131 ctl_softc = control_softc;
132
133 mtx_lock(&ctl_softc->ctl_lock);
134
135 #if 0
136 if (atomic_read(&be->num_luns) != 0) {
137 #endif
138 /* XXX KDM fix this! */
139 if (be->num_luns != 0) {
140 mtx_unlock(&ctl_softc->ctl_lock);
141 return (-1);
142 }
143
144 STAILQ_REMOVE(&ctl_softc->be_list, be, ctl_backend_driver, links);
145
146 ctl_softc->num_backends--;
147
148 /* XXX KDM find a substitute for this? */
149 #if 0
150 if ((be->flags & CTL_BE_FLAG_INTERNAL) == 0)
151 MOD_DEC_USE_COUNT;
152 #endif
153
154 mtx_unlock(&ctl_softc->ctl_lock);
155
156 return (0);
157 }
158
159 struct ctl_backend_driver *
160 ctl_backend_find(char *backend_name)
161 {
162 struct ctl_softc *ctl_softc;
163 struct ctl_backend_driver *be_tmp;
164
165 ctl_softc = control_softc;
166
167 mtx_lock(&ctl_softc->ctl_lock);
168
169 STAILQ_FOREACH(be_tmp, &ctl_softc->be_list, links) {
170 if (strcmp(be_tmp->name, backend_name) == 0) {
171 mtx_unlock(&ctl_softc->ctl_lock);
172 return (be_tmp);
173 }
174 }
175
176 mtx_unlock(&ctl_softc->ctl_lock);
177
178 return (NULL);
179 }
180
181 /*
182 * vim: ts=8
183 */
184