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 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 71 mtx_lock(&softc->ctl_lock); 72 /* 73 * Sanity check, make sure this isn't a duplicate registration. 74 */ 75 STAILQ_FOREACH(be_tmp, &softc->be_list, links) { 76 if (strcmp(be_tmp->name, be->name) == 0) { 77 mtx_unlock(&softc->ctl_lock); 78 return (-1); 79 } 80 } 81 mtx_unlock(&softc->ctl_lock); 82 83 /* 84 * Call the backend's initialization routine. 85 */ 86 be->init(); 87 88 mtx_lock(&softc->ctl_lock); 89 90 STAILQ_INSERT_TAIL(&softc->be_list, be, links); 91 92 softc->num_backends++; 93 94 /* 95 * Don't want to increment the usage count for internal consumers, 96 * we won't be able to unload otherwise. 97 */ 98 /* XXX KDM find a substitute for this? */ 99 #if 0 100 if ((be->flags & CTL_BE_FLAG_INTERNAL) == 0) 101 MOD_INC_USE_COUNT; 102 #endif 103 104 #ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED 105 be->config_move_done = ctl_config_move_done; 106 #endif 107 /* XXX KDM fix this! */ 108 be->num_luns = 0; 109 #if 0 110 atomic_set(&be->num_luns, 0); 111 #endif 112 113 mtx_unlock(&softc->ctl_lock); 114 115 return (0); 116 } 117 118 int 119 ctl_backend_deregister(struct ctl_backend_driver *be) 120 { 121 struct ctl_softc *softc = control_softc; 122 123 mtx_lock(&softc->ctl_lock); 124 125 #if 0 126 if (atomic_read(&be->num_luns) != 0) { 127 #endif 128 /* XXX KDM fix this! */ 129 if (be->num_luns != 0) { 130 mtx_unlock(&softc->ctl_lock); 131 return (-1); 132 } 133 134 STAILQ_REMOVE(&softc->be_list, be, ctl_backend_driver, links); 135 136 softc->num_backends--; 137 138 /* XXX KDM find a substitute for this? */ 139 #if 0 140 if ((be->flags & CTL_BE_FLAG_INTERNAL) == 0) 141 MOD_DEC_USE_COUNT; 142 #endif 143 144 mtx_unlock(&softc->ctl_lock); 145 146 return (0); 147 } 148 149 struct ctl_backend_driver * 150 ctl_backend_find(char *backend_name) 151 { 152 struct ctl_softc *softc = control_softc; 153 struct ctl_backend_driver *be_tmp; 154 155 mtx_lock(&softc->ctl_lock); 156 STAILQ_FOREACH(be_tmp, &softc->be_list, links) { 157 if (strcmp(be_tmp->name, backend_name) == 0) { 158 mtx_unlock(&softc->ctl_lock); 159 return (be_tmp); 160 } 161 } 162 mtx_unlock(&softc->ctl_lock); 163 164 return (NULL); 165 } 166 167 void 168 ctl_init_opts(ctl_options_t *opts, int num_args, struct ctl_be_arg *args) 169 { 170 struct ctl_option *opt; 171 int i; 172 173 STAILQ_INIT(opts); 174 for (i = 0; i < num_args; i++) { 175 if ((args[i].flags & CTL_BEARG_RD) == 0) 176 continue; 177 if ((args[i].flags & CTL_BEARG_ASCII) == 0) 178 continue; 179 opt = malloc(sizeof(*opt), M_CTL, M_WAITOK); 180 opt->name = strdup(args[i].kname, M_CTL); 181 opt->value = strdup(args[i].kvalue, M_CTL); 182 STAILQ_INSERT_TAIL(opts, opt, links); 183 } 184 } 185 186 void 187 ctl_update_opts(ctl_options_t *opts, int num_args, struct ctl_be_arg *args) 188 { 189 struct ctl_option *opt; 190 int i; 191 192 for (i = 0; i < num_args; i++) { 193 if ((args[i].flags & CTL_BEARG_RD) == 0) 194 continue; 195 if ((args[i].flags & CTL_BEARG_ASCII) == 0) 196 continue; 197 STAILQ_FOREACH(opt, opts, links) { 198 if (strcmp(opt->name, args[i].kname) == 0) 199 break; 200 } 201 if (args[i].kvalue != NULL && 202 ((char *)args[i].kvalue)[0] != 0) { 203 if (opt) { 204 free(opt->value, M_CTL); 205 opt->value = strdup(args[i].kvalue, M_CTL); 206 } else { 207 opt = malloc(sizeof(*opt), M_CTL, M_WAITOK); 208 opt->name = strdup(args[i].kname, M_CTL); 209 opt->value = strdup(args[i].kvalue, M_CTL); 210 STAILQ_INSERT_TAIL(opts, opt, links); 211 } 212 } else if (opt) { 213 STAILQ_REMOVE(opts, opt, ctl_option, links); 214 free(opt->name, M_CTL); 215 free(opt->value, M_CTL); 216 free(opt, M_CTL); 217 } 218 } 219 } 220 221 void 222 ctl_free_opts(ctl_options_t *opts) 223 { 224 struct ctl_option *opt; 225 226 while ((opt = STAILQ_FIRST(opts)) != NULL) { 227 STAILQ_REMOVE_HEAD(opts, links); 228 free(opt->name, M_CTL); 229 free(opt->value, M_CTL); 230 free(opt, M_CTL); 231 } 232 } 233 234 char * 235 ctl_get_opt(ctl_options_t *opts, const char *name) 236 { 237 struct ctl_option *opt; 238 239 STAILQ_FOREACH(opt, opts, links) { 240 if (strcmp(opt->name, name) == 0) { 241 return (opt->value); 242 } 243 } 244 return (NULL); 245 } 246 247 int 248 ctl_get_opt_number(ctl_options_t *opts, const char *name, uint64_t *val) 249 { 250 const char *value; 251 252 value = ctl_get_opt(opts, name); 253 if (value == NULL) 254 return (-2); 255 return (ctl_expand_number(value, val)); 256 } 257