xref: /freebsd/sys/cam/ctl/ctl_backend.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/types.h>
44 #include <sys/malloc.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/queue.h>
49 #include <sys/sysctl.h>
50 
51 #include <cam/scsi/scsi_all.h>
52 #include <cam/scsi/scsi_da.h>
53 #include <cam/ctl/ctl_io.h>
54 #include <cam/ctl/ctl.h>
55 #include <cam/ctl/ctl_frontend.h>
56 #include <cam/ctl/ctl_backend.h>
57 #include <cam/ctl/ctl_ioctl.h>
58 #include <cam/ctl/ctl_ha.h>
59 #include <cam/ctl/ctl_private.h>
60 #include <cam/ctl/ctl_debug.h>
61 
62 extern struct ctl_softc *control_softc;
63 
64 int
65 ctl_backend_register(struct ctl_backend_driver *be)
66 {
67 	struct ctl_softc *softc = control_softc;
68 	struct ctl_backend_driver *be_tmp;
69 	int error;
70 
71 	/* Sanity check, make sure this isn't a duplicate registration. */
72 	mtx_lock(&softc->ctl_lock);
73 	STAILQ_FOREACH(be_tmp, &softc->be_list, links) {
74 		if (strcmp(be_tmp->name, be->name) == 0) {
75 			mtx_unlock(&softc->ctl_lock);
76 			return (-1);
77 		}
78 	}
79 	mtx_unlock(&softc->ctl_lock);
80 #ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
81 	be->config_move_done = ctl_config_move_done;
82 #endif
83 
84 	/* Call the backend's initialization routine. */
85 	if (be->init != NULL) {
86 		if ((error = be->init()) != 0) {
87 			printf("%s backend init error: %d\n",
88 			    be->name, error);
89 			return (error);
90 		}
91 	}
92 
93 	mtx_lock(&softc->ctl_lock);
94 	STAILQ_INSERT_TAIL(&softc->be_list, be, links);
95 	softc->num_backends++;
96 	mtx_unlock(&softc->ctl_lock);
97 	return (0);
98 }
99 
100 int
101 ctl_backend_deregister(struct ctl_backend_driver *be)
102 {
103 	struct ctl_softc *softc = control_softc;
104 	int error;
105 
106 	/* Call the backend's shutdown routine. */
107 	if (be->shutdown != NULL) {
108 		if ((error = be->shutdown()) != 0) {
109 			printf("%s backend shutdown error: %d\n",
110 			    be->name, error);
111 			return (error);
112 		}
113 	}
114 
115 	mtx_lock(&softc->ctl_lock);
116 	STAILQ_REMOVE(&softc->be_list, be, ctl_backend_driver, links);
117 	softc->num_backends--;
118 	mtx_unlock(&softc->ctl_lock);
119 	return (0);
120 }
121 
122 struct ctl_backend_driver *
123 ctl_backend_find(char *backend_name)
124 {
125 	struct ctl_softc *softc = control_softc;
126 	struct ctl_backend_driver *be_tmp;
127 
128 	mtx_lock(&softc->ctl_lock);
129 	STAILQ_FOREACH(be_tmp, &softc->be_list, links) {
130 		if (strcmp(be_tmp->name, backend_name) == 0) {
131 			mtx_unlock(&softc->ctl_lock);
132 			return (be_tmp);
133 		}
134 	}
135 	mtx_unlock(&softc->ctl_lock);
136 
137 	return (NULL);
138 }
139