xref: /freebsd-11-stable/sys/cam/ctl/ctl_backend.c (revision 20fb28118ba3b7c74377018c9cecdea5022ae12e)
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$");
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_ioctl.h>
59 #include <cam/ctl/ctl_ha.h>
60 #include <cam/ctl/ctl_private.h>
61 #include <cam/ctl/ctl_debug.h>
62 
63 extern struct ctl_softc *control_softc;
64 
65 int
ctl_backend_register(struct ctl_backend_driver * be)66 ctl_backend_register(struct ctl_backend_driver *be)
67 {
68 	struct ctl_softc *softc = control_softc;
69 	struct ctl_backend_driver *be_tmp;
70 	int error;
71 
72 	/* Sanity check, make sure this isn't a duplicate registration. */
73 	mtx_lock(&softc->ctl_lock);
74 	STAILQ_FOREACH(be_tmp, &softc->be_list, links) {
75 		if (strcmp(be_tmp->name, be->name) == 0) {
76 			mtx_unlock(&softc->ctl_lock);
77 			return (-1);
78 		}
79 	}
80 	mtx_unlock(&softc->ctl_lock);
81 #ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
82 	be->config_move_done = ctl_config_move_done;
83 #endif
84 
85 	/* Call the backend's initialization routine. */
86 	if (be->init != NULL) {
87 		if ((error = be->init()) != 0) {
88 			printf("%s backend init error: %d\n",
89 			    be->name, error);
90 			return (error);
91 		}
92 	}
93 
94 	mtx_lock(&softc->ctl_lock);
95 	STAILQ_INSERT_TAIL(&softc->be_list, be, links);
96 	softc->num_backends++;
97 	mtx_unlock(&softc->ctl_lock);
98 	return (0);
99 }
100 
101 int
ctl_backend_deregister(struct ctl_backend_driver * be)102 ctl_backend_deregister(struct ctl_backend_driver *be)
103 {
104 	struct ctl_softc *softc = control_softc;
105 	int error;
106 
107 	/* Call the backend's shutdown routine. */
108 	if (be->shutdown != NULL) {
109 		if ((error = be->shutdown()) != 0) {
110 			printf("%s backend shutdown error: %d\n",
111 			    be->name, error);
112 			return (error);
113 		}
114 	}
115 
116 	mtx_lock(&softc->ctl_lock);
117 	STAILQ_REMOVE(&softc->be_list, be, ctl_backend_driver, links);
118 	softc->num_backends--;
119 	mtx_unlock(&softc->ctl_lock);
120 	return (0);
121 }
122 
123 struct ctl_backend_driver *
ctl_backend_find(char * backend_name)124 ctl_backend_find(char *backend_name)
125 {
126 	struct ctl_softc *softc = control_softc;
127 	struct ctl_backend_driver *be_tmp;
128 
129 	mtx_lock(&softc->ctl_lock);
130 	STAILQ_FOREACH(be_tmp, &softc->be_list, links) {
131 		if (strcmp(be_tmp->name, backend_name) == 0) {
132 			mtx_unlock(&softc->ctl_lock);
133 			return (be_tmp);
134 		}
135 	}
136 	mtx_unlock(&softc->ctl_lock);
137 
138 	return (NULL);
139 }
140 
141 void
ctl_init_opts(ctl_options_t * opts,int num_args,struct ctl_be_arg * args)142 ctl_init_opts(ctl_options_t *opts, int num_args, struct ctl_be_arg *args)
143 {
144 	struct ctl_option *opt;
145 	int i;
146 
147 	STAILQ_INIT(opts);
148 	for (i = 0; i < num_args; i++) {
149 		if ((args[i].flags & CTL_BEARG_RD) == 0)
150 			continue;
151 		if ((args[i].flags & CTL_BEARG_ASCII) == 0)
152 			continue;
153 		opt = malloc(sizeof(*opt), M_CTL, M_WAITOK);
154 		opt->name = strdup(args[i].kname, M_CTL);
155 		opt->value = strdup(args[i].kvalue, M_CTL);
156 		STAILQ_INSERT_TAIL(opts, opt, links);
157 	}
158 }
159 
160 void
ctl_update_opts(ctl_options_t * opts,int num_args,struct ctl_be_arg * args)161 ctl_update_opts(ctl_options_t *opts, int num_args, struct ctl_be_arg *args)
162 {
163 	struct ctl_option *opt;
164 	int i;
165 
166 	for (i = 0; i < num_args; i++) {
167 		if ((args[i].flags & CTL_BEARG_RD) == 0)
168 			continue;
169 		if ((args[i].flags & CTL_BEARG_ASCII) == 0)
170 			continue;
171 		STAILQ_FOREACH(opt, opts, links) {
172 			if (strcmp(opt->name, args[i].kname) == 0)
173 				break;
174 		}
175 		if (args[i].kvalue != NULL &&
176 		    ((char *)args[i].kvalue)[0] != 0) {
177 			if (opt) {
178 				free(opt->value, M_CTL);
179 				opt->value = strdup(args[i].kvalue, M_CTL);
180 			} else {
181 				opt = malloc(sizeof(*opt), M_CTL, M_WAITOK);
182 				opt->name = strdup(args[i].kname, M_CTL);
183 				opt->value = strdup(args[i].kvalue, M_CTL);
184 				STAILQ_INSERT_TAIL(opts, opt, links);
185 			}
186 		} else if (opt) {
187 			STAILQ_REMOVE(opts, opt, ctl_option, links);
188 			free(opt->name, M_CTL);
189 			free(opt->value, M_CTL);
190 			free(opt, M_CTL);
191 		}
192 	}
193 }
194 
195 void
ctl_free_opts(ctl_options_t * opts)196 ctl_free_opts(ctl_options_t *opts)
197 {
198 	struct ctl_option *opt;
199 
200 	while ((opt = STAILQ_FIRST(opts)) != NULL) {
201 		STAILQ_REMOVE_HEAD(opts, links);
202 		free(opt->name, M_CTL);
203 		free(opt->value, M_CTL);
204 		free(opt, M_CTL);
205 	}
206 }
207 
208 char *
ctl_get_opt(ctl_options_t * opts,const char * name)209 ctl_get_opt(ctl_options_t *opts, const char *name)
210 {
211 	struct ctl_option *opt;
212 
213 	STAILQ_FOREACH(opt, opts, links) {
214 		if (strcmp(opt->name, name) == 0) {
215 			return (opt->value);
216 		}
217 	}
218 	return (NULL);
219 }
220 
221 int
ctl_get_opt_number(ctl_options_t * opts,const char * name,uint64_t * val)222 ctl_get_opt_number(ctl_options_t *opts, const char *name, uint64_t *val)
223 {
224 	const char *value;
225 
226 	value = ctl_get_opt(opts, name);
227 	if (value == NULL)
228 		return (-2);
229 	return (ctl_expand_number(value, val));
230 }
231