FreeBSD kernel CAM code
ctl_frontend.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2003 Silicon Graphics International Corp.
5 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * substantially similar to the "NO WARRANTY" disclaimer below
16 * ("Disclaimer") and any redistribution must be conditioned upon
17 * including a substantially similar Disclaimer requirement for further
18 * binary redistribution.
19 *
20 * NO WARRANTY
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGES.
32 *
33 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.c#4 $
34 */
35/*
36 * CAM Target Layer front end interface code
37 *
38 * Author: Ken Merry <ken@FreeBSD.org>
39 */
40
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD$");
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/types.h>
48#include <sys/malloc.h>
49#include <sys/lock.h>
50#include <sys/mutex.h>
51#include <sys/condvar.h>
52#include <sys/endian.h>
53#include <sys/queue.h>
54#include <sys/sysctl.h>
55#include <sys/nv.h>
56#include <sys/dnv.h>
57
58#include <cam/scsi/scsi_all.h>
59#include <cam/scsi/scsi_da.h>
60#include <cam/ctl/ctl_io.h>
61#include <cam/ctl/ctl.h>
63#include <cam/ctl/ctl_backend.h>
64/* XXX KDM move defines from ctl_ioctl.h to somewhere else */
65#include <cam/ctl/ctl_ioctl.h>
66#include <cam/ctl/ctl_ha.h>
67#include <cam/ctl/ctl_private.h>
68#include <cam/ctl/ctl_debug.h>
69
70extern struct ctl_softc *control_softc;
71
72int
74{
75 struct ctl_softc *softc = control_softc;
76 struct ctl_frontend *fe_tmp;
77 int error;
78
79 KASSERT(softc != NULL, ("CTL is not initialized"));
80
81 /* Sanity check, make sure this isn't a duplicate registration. */
82 mtx_lock(&softc->ctl_lock);
83 STAILQ_FOREACH(fe_tmp, &softc->fe_list, links) {
84 if (strcmp(fe_tmp->name, fe->name) == 0) {
85 mtx_unlock(&softc->ctl_lock);
86 return (-1);
87 }
88 }
89 mtx_unlock(&softc->ctl_lock);
90 STAILQ_INIT(&fe->port_list);
91
92 /* Call the frontend's initialization routine. */
93 if (fe->init != NULL) {
94 if ((error = fe->init()) != 0) {
95 printf("%s frontend init error: %d\n",
96 fe->name, error);
97 return (error);
98 }
99 }
100
101 mtx_lock(&softc->ctl_lock);
102 softc->num_frontends++;
103 STAILQ_INSERT_TAIL(&softc->fe_list, fe, links);
104 mtx_unlock(&softc->ctl_lock);
105 return (0);
106}
107
108int
110{
111 struct ctl_softc *softc = control_softc;
112 int error;
113
114 /* Call the frontend's shutdown routine.*/
115 if (fe->shutdown != NULL) {
116 if ((error = fe->shutdown()) != 0) {
117 printf("%s frontend shutdown error: %d\n",
118 fe->name, error);
119 return (error);
120 }
121 }
122
123 mtx_lock(&softc->ctl_lock);
124 STAILQ_REMOVE(&softc->fe_list, fe, ctl_frontend, links);
125 softc->num_frontends--;
126 mtx_unlock(&softc->ctl_lock);
127 return (0);
128}
129
130struct ctl_frontend *
131ctl_frontend_find(char *frontend_name)
132{
133 struct ctl_softc *softc = control_softc;
134 struct ctl_frontend *fe;
135
136 mtx_lock(&softc->ctl_lock);
137 STAILQ_FOREACH(fe, &softc->fe_list, links) {
138 if (strcmp(fe->name, frontend_name) == 0) {
139 mtx_unlock(&softc->ctl_lock);
140 return (fe);
141 }
142 }
143 mtx_unlock(&softc->ctl_lock);
144 return (NULL);
145}
146
147int
149{
150 struct ctl_softc *softc = control_softc;
151 struct ctl_port *tport, *nport;
152 void *pool;
153 int port_num;
154 int retval;
155
156 KASSERT(softc != NULL, ("CTL is not initialized"));
157 port->ctl_softc = softc;
158
159 mtx_lock(&softc->ctl_lock);
160 if (port->targ_port >= 0)
161 port_num = port->targ_port;
162 else
163 port_num = ctl_ffz(softc->ctl_port_mask,
164 softc->port_min, softc->port_max);
165 if ((port_num < 0) ||
166 (ctl_set_mask(softc->ctl_port_mask, port_num) < 0)) {
167 mtx_unlock(&softc->ctl_lock);
168 return (1);
169 }
170 softc->num_ports++;
171 mtx_unlock(&softc->ctl_lock);
172
173 /*
174 * Initialize the initiator and portname mappings
175 */
177 port->wwpn_iid = malloc(sizeof(*port->wwpn_iid) * port->max_initiators,
178 M_CTL, M_NOWAIT | M_ZERO);
179 if (port->wwpn_iid == NULL) {
180 retval = ENOMEM;
181 goto error;
182 }
183
184 /*
185 * We add 20 to whatever the caller requests, so he doesn't get
186 * burned by queueing things back to the pending sense queue. In
187 * theory, there should probably only be one outstanding item, at
188 * most, on the pending sense queue for a LUN. We'll clear the
189 * pending sense queue on the next command, whether or not it is
190 * a REQUEST SENSE.
191 */
192 retval = ctl_pool_create(softc, port->port_name,
193 port->num_requested_ctl_io + 20, &pool);
194 if (retval != 0) {
195 free(port->wwpn_iid, M_CTL);
196error:
197 port->targ_port = -1;
198 mtx_lock(&softc->ctl_lock);
199 ctl_clear_mask(softc->ctl_port_mask, port_num);
200 mtx_unlock(&softc->ctl_lock);
201 return (retval);
202 }
203 port->targ_port = port_num;
204 port->ctl_pool_ref = pool;
205 if (port->options == NULL)
206 port->options = nvlist_create(0);
207 port->stats.item = port_num;
208 mtx_init(&port->port_lock, "CTL port", NULL, MTX_DEF);
209
210 mtx_lock(&softc->ctl_lock);
211 STAILQ_INSERT_TAIL(&port->frontend->port_list, port, fe_links);
212 for (tport = NULL, nport = STAILQ_FIRST(&softc->port_list);
213 nport != NULL && nport->targ_port < port_num;
214 tport = nport, nport = STAILQ_NEXT(tport, links)) {
215 }
216 if (tport)
217 STAILQ_INSERT_AFTER(&softc->port_list, tport, port, links);
218 else
219 STAILQ_INSERT_HEAD(&softc->port_list, port, links);
220 softc->ctl_ports[port->targ_port] = port;
221 mtx_unlock(&softc->ctl_lock);
222
223 return (retval);
224}
225
226int
228{
229 struct ctl_softc *softc = port->ctl_softc;
230 struct ctl_io_pool *pool = (struct ctl_io_pool *)port->ctl_pool_ref;
231 int i;
232
233 if (port->targ_port == -1)
234 return (1);
235
236 mtx_lock(&softc->ctl_lock);
237 STAILQ_REMOVE(&softc->port_list, port, ctl_port, links);
238 STAILQ_REMOVE(&port->frontend->port_list, port, ctl_port, fe_links);
239 softc->num_ports--;
241 softc->ctl_ports[port->targ_port] = NULL;
242 mtx_unlock(&softc->ctl_lock);
243
244 ctl_pool_free(pool);
245 nvlist_destroy(port->options);
246
247 ctl_lun_map_deinit(port);
248 free(port->port_devid, M_CTL);
249 port->port_devid = NULL;
250 free(port->target_devid, M_CTL);
251 port->target_devid = NULL;
252 free(port->init_devid, M_CTL);
253 port->init_devid = NULL;
254 for (i = 0; i < port->max_initiators; i++)
255 free(port->wwpn_iid[i].name, M_CTL);
256 free(port->wwpn_iid, M_CTL);
257 mtx_destroy(&port->port_lock);
258
259 return (0);
260}
261
262void
263ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn,
264 int wwpn_valid, uint64_t wwpn)
265{
266 struct scsi_vpd_id_descriptor *desc;
267 int len, proto;
268
269 if (port->port_type == CTL_PORT_FC)
270 proto = SCSI_PROTO_FC << 4;
271 else if (port->port_type == CTL_PORT_SAS)
272 proto = SCSI_PROTO_SAS << 4;
273 else if (port->port_type == CTL_PORT_ISCSI)
274 proto = SCSI_PROTO_ISCSI << 4;
275 else
276 proto = SCSI_PROTO_SPI << 4;
277
278 if (wwnn_valid) {
279 port->wwnn = wwnn;
280
281 free(port->target_devid, M_CTL);
282
283 len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
284 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
285 M_CTL, M_WAITOK | M_ZERO);
286 port->target_devid->len = len;
287 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
291 desc->length = CTL_WWPN_LEN;
292 scsi_u64to8b(port->wwnn, desc->identifier);
293 }
294
295 if (wwpn_valid) {
296 port->wwpn = wwpn;
297
298 free(port->port_devid, M_CTL);
299
300 len = sizeof(struct scsi_vpd_device_id) + CTL_WWPN_LEN;
301 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
302 M_CTL, M_WAITOK | M_ZERO);
303 port->port_devid->len = len;
304 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
308 desc->length = CTL_WWPN_LEN;
309 scsi_u64to8b(port->wwpn, desc->identifier);
310 }
311}
312
313void
315{
316 struct ctl_softc *softc = port->ctl_softc;
317 struct ctl_lun *lun;
318 const char *value;
319 uint32_t l;
320
321 if (port->lun_enable != NULL) {
322 if (port->lun_map) {
323 for (l = 0; l < port->lun_map_size; l++) {
324 if (ctl_lun_map_from_port(port, l) ==
325 UINT32_MAX)
326 continue;
327 port->lun_enable(port->targ_lun_arg, l);
328 }
329 } else {
330 STAILQ_FOREACH(lun, &softc->lun_list, links)
331 port->lun_enable(port->targ_lun_arg, lun->lun);
332 }
333 }
334 if (port->port_online != NULL)
335 port->port_online(port->onoff_arg);
336 mtx_lock(&softc->ctl_lock);
337 if (softc->is_single == 0) {
338 value = dnvlist_get_string(port->options, "ha_shared", NULL);
339 if (value != NULL && strcmp(value, "on") == 0)
341 else
342 port->status &= ~CTL_PORT_STATUS_HA_SHARED;
343 }
345 STAILQ_FOREACH(lun, &softc->lun_list, links) {
346 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
347 continue;
348 mtx_lock(&lun->lun_lock);
350 mtx_unlock(&lun->lun_lock);
351 }
352 mtx_unlock(&softc->ctl_lock);
354}
355
356void
358{
359 struct ctl_softc *softc = port->ctl_softc;
360 struct ctl_lun *lun;
361 uint32_t l;
362
363 if (port->port_offline != NULL)
364 port->port_offline(port->onoff_arg);
365 if (port->lun_disable != NULL) {
366 if (port->lun_map) {
367 for (l = 0; l < port->lun_map_size; l++) {
368 if (ctl_lun_map_from_port(port, l) ==
369 UINT32_MAX)
370 continue;
371 port->lun_disable(port->targ_lun_arg, l);
372 }
373 } else {
374 STAILQ_FOREACH(lun, &softc->lun_list, links)
375 port->lun_disable(port->targ_lun_arg, lun->lun);
376 }
377 }
378 mtx_lock(&softc->ctl_lock);
379 port->status &= ~CTL_PORT_STATUS_ONLINE;
380 STAILQ_FOREACH(lun, &softc->lun_list, links) {
381 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
382 continue;
383 mtx_lock(&lun->lun_lock);
385 mtx_unlock(&lun->lun_lock);
386 }
387 mtx_unlock(&softc->ctl_lock);
389}
390
391/*
392 * vim: ts=8
393 */
uint32_t ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
Definition: ctl.c:3733
int ctl_lun_map_deinit(struct ctl_port *port)
Definition: ctl.c:3659
int ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
Definition: ctl.c:3810
int ctl_set_mask(uint32_t *mask, uint32_t bit)
Definition: ctl.c:3822
void ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
Definition: ctl.c:1837
int ctl_clear_mask(uint32_t *mask, uint32_t bit)
Definition: ctl.c:3838
void ctl_pool_free(struct ctl_io_pool *pool)
Definition: ctl.c:3948
uint32_t ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
Definition: ctl.c:3720
void ctl_isc_announce_port(struct ctl_port *port)
Definition: ctl.c:896
int ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, uint32_t total_ctl_io, void **npool)
Definition: ctl.c:3923
#define CTL_WWPN_LEN
Definition: ctl.h:101
@ CTL_PORT_ISCSI
Definition: ctl.h:56
@ CTL_PORT_FC
Definition: ctl.h:52
@ CTL_PORT_SAS
Definition: ctl.h:57
@ CTL_UA_INQ_CHANGE
Definition: ctl.h:120
int ctl_port_deregister(struct ctl_port *port)
Definition: ctl_frontend.c:227
struct ctl_softc * control_softc
Definition: ctl.c:92
void ctl_port_set_wwns(struct ctl_port *port, int wwnn_valid, uint64_t wwnn, int wwpn_valid, uint64_t wwpn)
Definition: ctl_frontend.c:263
int ctl_frontend_deregister(struct ctl_frontend *fe)
Definition: ctl_frontend.c:109
struct ctl_frontend * ctl_frontend_find(char *frontend_name)
Definition: ctl_frontend.c:131
void ctl_port_offline(struct ctl_port *port)
Definition: ctl_frontend.c:357
void ctl_port_online(struct ctl_port *port)
Definition: ctl_frontend.c:314
__FBSDID("$FreeBSD$")
int ctl_port_register(struct ctl_port *port)
Definition: ctl_frontend.c:148
int ctl_frontend_register(struct ctl_frontend *fe)
Definition: ctl_frontend.c:73
@ CTL_PORT_STATUS_HA_SHARED
Definition: ctl_frontend.h:51
@ CTL_PORT_STATUS_ONLINE
Definition: ctl_frontend.h:50
#define CTL_MAX_INIT_PER_PORT
Definition: ctl_ioctl.h:67
#define SVPD_ID_ASSOC_TARGET
Definition: scsi_all.h:2377
#define SVPD_ID_PIV
Definition: scsi_all.h:2374
#define SVPD_ID_CODESET_BINARY
Definition: scsi_all.h:2369
#define SVPD_ID_TYPE_NAA
Definition: scsi_all.h:2382
#define SCSI_PROTO_FC
Definition: scsi_all.h:857
#define SCSI_PROTO_SPI
Definition: scsi_all.h:858
#define SCSI_PROTO_ISCSI
Definition: scsi_all.h:862
#define SVPD_ID_ASSOC_PORT
Definition: scsi_all.h:2376
static __inline void scsi_u64to8b(u_int64_t val, u_int8_t *bytes)
Definition: scsi_all.h:4401
#define SCSI_PROTO_SAS
Definition: scsi_all.h:864
uint8_t data[]
Definition: ctl_private.h:370
char name[CTL_DRIVER_NAME_LEN]
Definition: ctl_frontend.h:252
fe_shutdown_t shutdown
Definition: ctl_frontend.h:256
fe_init_t init
Definition: ctl_frontend.h:253
uint32_t item
Definition: ctl_ioctl.h:120
uint64_t lun
Definition: ctl_private.h:380
char * port_name
Definition: ctl_frontend.h:220
uint32_t max_initiators
Definition: ctl_frontend.h:236
void * targ_lun_arg
Definition: ctl_frontend.h:231
uint64_t wwnn
Definition: ctl_frontend.h:238
ctl_port_type port_type
Definition: ctl_frontend.h:218
int32_t targ_port
Definition: ctl_frontend.h:234
int num_requested_ctl_io
Definition: ctl_frontend.h:219
struct ctl_frontend * frontend
Definition: ctl_frontend.h:217
port_func_t port_offline
Definition: ctl_frontend.h:224
lun_func_t lun_enable
Definition: ctl_frontend.h:227
lun_func_t lun_disable
Definition: ctl_frontend.h:228
struct mtx port_lock
Definition: ctl_frontend.h:246
struct ctl_devid * port_devid
Definition: ctl_frontend.h:242
struct ctl_io_stats stats
Definition: ctl_frontend.h:245
int lun_map_size
Definition: ctl_frontend.h:229
uint32_t * lun_map
Definition: ctl_frontend.h:230
void * ctl_pool_ref
Definition: ctl_frontend.h:235
struct ctl_softc * ctl_softc
Definition: ctl_frontend.h:216
struct ctl_devid * init_devid
Definition: ctl_frontend.h:244
void * onoff_arg
Definition: ctl_frontend.h:226
struct ctl_devid * target_devid
Definition: ctl_frontend.h:243
nvlist_t * options
Definition: ctl_frontend.h:241
port_func_t port_online
Definition: ctl_frontend.h:223
ctl_port_status status
Definition: ctl_frontend.h:240
struct ctl_wwpn_iid * wwpn_iid
Definition: ctl_frontend.h:237
uint64_t wwpn
Definition: ctl_frontend.h:239
int is_single
Definition: ctl_private.h:443
int port_min
Definition: ctl_private.h:445
uint32_t * ctl_port_mask
Definition: ctl_private.h:456
int port_max
Definition: ctl_private.h:446
uint32_t num_ports
Definition: ctl_private.h:460
uint32_t num_frontends
Definition: ctl_private.h:458
struct mtx ctl_lock
Definition: ctl_private.h:437
struct ctl_port ** ctl_ports
Definition: ctl_private.h:462
u_int8_t identifier[]
Definition: scsi_all.h:2395