1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003 Silicon Graphics International Corp. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification. 13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 14 * substantially similar to the "NO WARRANTY" disclaimer below 15 * ("Disclaimer") and any redistribution must be conditioned upon 16 * including a substantially similar Disclaimer requirement for further 17 * binary redistribution. 18 * 19 * NO WARRANTY 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGES. 31 * 32 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend.c#3 $ 33 */ 34 /* 35 * CTL backend driver registration routines 36 * 37 * Author: Ken Merry <ken@FreeBSD.org> 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/types.h> 47 #include <sys/malloc.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/condvar.h> 51 #include <sys/queue.h> 52 #include <sys/sysctl.h> 53 54 #include <cam/scsi/scsi_all.h> 55 #include <cam/scsi/scsi_da.h> 56 #include <cam/ctl/ctl_io.h> 57 #include <cam/ctl/ctl.h> 58 #include <cam/ctl/ctl_frontend.h> 59 #include <cam/ctl/ctl_backend.h> 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_backend_register(struct ctl_backend_driver *be) 69 { 70 struct ctl_softc *softc = control_softc; 71 struct ctl_backend_driver *be_tmp; 72 int error; 73 74 /* Sanity check, make sure this isn't a duplicate registration. */ 75 mtx_lock(&softc->ctl_lock); 76 STAILQ_FOREACH(be_tmp, &softc->be_list, links) { 77 if (strcmp(be_tmp->name, be->name) == 0) { 78 mtx_unlock(&softc->ctl_lock); 79 return (-1); 80 } 81 } 82 mtx_unlock(&softc->ctl_lock); 83 #ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED 84 be->config_move_done = ctl_config_move_done; 85 #endif 86 be->num_luns = 0; 87 88 /* Call the backend's initialization routine. */ 89 if (be->init != NULL) { 90 if ((error = be->init()) != 0) { 91 printf("%s backend init error: %d\n", 92 be->name, error); 93 return (error); 94 } 95 } 96 97 mtx_lock(&softc->ctl_lock); 98 STAILQ_INSERT_TAIL(&softc->be_list, be, links); 99 softc->num_backends++; 100 mtx_unlock(&softc->ctl_lock); 101 return (0); 102 } 103 104 int 105 ctl_backend_deregister(struct ctl_backend_driver *be) 106 { 107 struct ctl_softc *softc = control_softc; 108 int error; 109 110 /* Call the backend's shutdown routine. */ 111 if (be->shutdown != NULL) { 112 if ((error = be->shutdown()) != 0) { 113 printf("%s backend shutdown error: %d\n", 114 be->name, error); 115 return (error); 116 } 117 } 118 119 mtx_lock(&softc->ctl_lock); 120 STAILQ_REMOVE(&softc->be_list, be, ctl_backend_driver, links); 121 softc->num_backends--; 122 mtx_unlock(&softc->ctl_lock); 123 return (0); 124 } 125 126 struct ctl_backend_driver * 127 ctl_backend_find(char *backend_name) 128 { 129 struct ctl_softc *softc = control_softc; 130 struct ctl_backend_driver *be_tmp; 131 132 mtx_lock(&softc->ctl_lock); 133 STAILQ_FOREACH(be_tmp, &softc->be_list, links) { 134 if (strcmp(be_tmp->name, backend_name) == 0) { 135 mtx_unlock(&softc->ctl_lock); 136 return (be_tmp); 137 } 138 } 139 mtx_unlock(&softc->ctl_lock); 140 141 return (NULL); 142 } 143 144 void 145 ctl_init_opts(ctl_options_t *opts, int num_args, struct ctl_be_arg *args) 146 { 147 struct ctl_option *opt; 148 int i; 149 150 STAILQ_INIT(opts); 151 for (i = 0; i < num_args; i++) { 152 if ((args[i].flags & CTL_BEARG_RD) == 0) 153 continue; 154 if ((args[i].flags & CTL_BEARG_ASCII) == 0) 155 continue; 156 opt = malloc(sizeof(*opt), M_CTL, M_WAITOK); 157 opt->name = strdup(args[i].kname, M_CTL); 158 opt->value = strdup(args[i].kvalue, M_CTL); 159 STAILQ_INSERT_TAIL(opts, opt, links); 160 } 161 } 162 163 void 164 ctl_update_opts(ctl_options_t *opts, int num_args, struct ctl_be_arg *args) 165 { 166 struct ctl_option *opt; 167 int i; 168 169 for (i = 0; i < num_args; i++) { 170 if ((args[i].flags & CTL_BEARG_RD) == 0) 171 continue; 172 if ((args[i].flags & CTL_BEARG_ASCII) == 0) 173 continue; 174 STAILQ_FOREACH(opt, opts, links) { 175 if (strcmp(opt->name, args[i].kname) == 0) 176 break; 177 } 178 if (args[i].kvalue != NULL && 179 ((char *)args[i].kvalue)[0] != 0) { 180 if (opt) { 181 free(opt->value, M_CTL); 182 opt->value = strdup(args[i].kvalue, M_CTL); 183 } else { 184 opt = malloc(sizeof(*opt), M_CTL, M_WAITOK); 185 opt->name = strdup(args[i].kname, M_CTL); 186 opt->value = strdup(args[i].kvalue, M_CTL); 187 STAILQ_INSERT_TAIL(opts, opt, links); 188 } 189 } else if (opt) { 190 STAILQ_REMOVE(opts, opt, ctl_option, links); 191 free(opt->name, M_CTL); 192 free(opt->value, M_CTL); 193 free(opt, M_CTL); 194 } 195 } 196 } 197 198 void 199 ctl_free_opts(ctl_options_t *opts) 200 { 201 struct ctl_option *opt; 202 203 while ((opt = STAILQ_FIRST(opts)) != NULL) { 204 STAILQ_REMOVE_HEAD(opts, links); 205 free(opt->name, M_CTL); 206 free(opt->value, M_CTL); 207 free(opt, M_CTL); 208 } 209 } 210 211 char * 212 ctl_get_opt(ctl_options_t *opts, const char *name) 213 { 214 struct ctl_option *opt; 215 216 STAILQ_FOREACH(opt, opts, links) { 217 if (strcmp(opt->name, name) == 0) { 218 return (opt->value); 219 } 220 } 221 return (NULL); 222 } 223 224 int 225 ctl_get_opt_number(ctl_options_t *opts, const char *name, uint64_t *val) 226 { 227 const char *value; 228 229 value = ctl_get_opt(opts, name); 230 if (value == NULL) 231 return (-2); 232 return (ctl_expand_number(value, val)); 233 } 234