FreeBSD kernel CAM code
ctl_tpc_local.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2014 Alexander Motin <mav@FreeBSD.org>
5 * Copyright (c) 2004, 2005 Silicon Graphics International Corp.
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, immediately at the beginning of the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/types.h>
37#include <sys/lock.h>
38#include <sys/module.h>
39#include <sys/mutex.h>
40#include <sys/condvar.h>
41#include <sys/malloc.h>
42#include <sys/conf.h>
43#include <sys/queue.h>
44#include <sys/sysctl.h>
45
46#include <cam/cam.h>
47#include <cam/scsi/scsi_all.h>
48#include <cam/scsi/scsi_da.h>
49#include <cam/ctl/ctl_io.h>
50#include <cam/ctl/ctl.h>
52#include <cam/ctl/ctl_util.h>
53#include <cam/ctl/ctl_backend.h>
54#include <cam/ctl/ctl_ioctl.h>
55#include <cam/ctl/ctl_ha.h>
56#include <cam/ctl/ctl_private.h>
57#include <cam/ctl/ctl_debug.h>
59#include <cam/ctl/ctl_tpc.h>
60#include <cam/ctl/ctl_error.h>
61
62struct tpcl_softc {
63 struct ctl_port port;
65};
66
67static struct tpcl_softc tpcl_softc;
68
69static int tpcl_init(void);
70static int tpcl_shutdown(void);
71static void tpcl_datamove(union ctl_io *io);
72static void tpcl_done(union ctl_io *io);
73
75{
76 .name = "tpc",
77 .init = tpcl_init,
78 .shutdown = tpcl_shutdown,
79};
81
82static int
84{
85 struct tpcl_softc *tsoftc = &tpcl_softc;
86 struct ctl_port *port;
87 struct scsi_transportid_spi *tid;
88 int error, len;
89
90 memset(tsoftc, 0, sizeof(*tsoftc));
91
92 port = &tsoftc->port;
93 port->frontend = &tpcl_frontend;
95 port->num_requested_ctl_io = 100;
96 port->port_name = "tpc";
98 port->fe_done = tpcl_done;
99 port->targ_port = -1;
100 port->max_initiators = 1;
101
102 if ((error = ctl_port_register(port)) != 0) {
103 printf("%s: tpc port registration failed\n", __func__);
104 return (error);
105 }
106
107 len = sizeof(struct scsi_transportid_spi);
108 port->init_devid = malloc(sizeof(struct ctl_devid) + len,
109 M_CTL, M_WAITOK | M_ZERO);
110 port->init_devid->len = len;
111 tid = (struct scsi_transportid_spi *)port->init_devid->data;
113 scsi_ulto2b(0, tid->scsi_addr);
115
116 ctl_port_online(port);
117 return (0);
118}
119
120static int
122{
123 struct tpcl_softc *tsoftc = &tpcl_softc;
124 struct ctl_port *port = &tsoftc->port;
125 int error;
126
127 ctl_port_offline(port);
128 if ((error = ctl_port_deregister(port)) != 0)
129 printf("%s: tpc port deregistration failed\n", __func__);
130 return (error);
131}
132
133static void
135{
136 struct ctl_sg_entry *ext_sglist, *kern_sglist;
137 struct ctl_sg_entry ext_entry, kern_entry;
138 int ext_sg_entries, kern_sg_entries;
139 int ext_sg_start, ext_offset;
140 int len_to_copy;
141 int kern_watermark, ext_watermark;
142 struct ctl_scsiio *ctsio;
143 int i, j;
144
145 CTL_DEBUG_PRINT(("%s\n", __func__));
146
147 ctsio = &io->scsiio;
148
149 /*
150 * If this is the case, we're probably doing a BBR read and don't
151 * actually need to transfer the data. This will effectively
152 * bit-bucket the data.
153 */
154 if (ctsio->ext_data_ptr == NULL)
155 goto bailout;
156
157 /*
158 * To simplify things here, if we have a single buffer, stick it in
159 * a S/G entry and just make it a single entry S/G list.
160 */
161 if (ctsio->ext_sg_entries > 0) {
162 int len_seen;
163
164 ext_sglist = (struct ctl_sg_entry *)ctsio->ext_data_ptr;
165 ext_sg_entries = ctsio->ext_sg_entries;
166 ext_sg_start = 0;
167 ext_offset = 0;
168 len_seen = 0;
169 for (i = 0; i < ext_sg_entries; i++) {
170 if ((len_seen + ext_sglist[i].len) >=
171 ctsio->ext_data_filled) {
172 ext_sg_start = i;
173 ext_offset = ctsio->ext_data_filled - len_seen;
174 break;
175 }
176 len_seen += ext_sglist[i].len;
177 }
178 } else {
179 ext_sglist = &ext_entry;
180 ext_sglist->addr = ctsio->ext_data_ptr;
181 ext_sglist->len = ctsio->ext_data_len;
182 ext_sg_entries = 1;
183 ext_sg_start = 0;
184 ext_offset = ctsio->ext_data_filled;
185 }
186
187 if (ctsio->kern_sg_entries > 0) {
188 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
189 kern_sg_entries = ctsio->kern_sg_entries;
190 } else {
191 kern_sglist = &kern_entry;
192 kern_sglist->addr = ctsio->kern_data_ptr;
193 kern_sglist->len = ctsio->kern_data_len;
194 kern_sg_entries = 1;
195 }
196
197 kern_watermark = 0;
198 ext_watermark = ext_offset;
199 for (i = ext_sg_start, j = 0;
200 i < ext_sg_entries && j < kern_sg_entries;) {
201 uint8_t *ext_ptr, *kern_ptr;
202
203 len_to_copy = min(ext_sglist[i].len - ext_watermark,
204 kern_sglist[j].len - kern_watermark);
205
206 ext_ptr = (uint8_t *)ext_sglist[i].addr;
207 ext_ptr = ext_ptr + ext_watermark;
208 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
209 /*
210 * XXX KDM fix this!
211 */
212 panic("need to implement bus address support");
213#if 0
214 kern_ptr = bus_to_virt(kern_sglist[j].addr);
215#endif
216 } else
217 kern_ptr = (uint8_t *)kern_sglist[j].addr;
218 kern_ptr = kern_ptr + kern_watermark;
219
220 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
222 CTL_DEBUG_PRINT(("%s: copying %d bytes to user\n",
223 __func__, len_to_copy));
224 CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
225 kern_ptr, ext_ptr));
226 memcpy(ext_ptr, kern_ptr, len_to_copy);
227 } else {
228 CTL_DEBUG_PRINT(("%s: copying %d bytes from user\n",
229 __func__, len_to_copy));
230 CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
231 ext_ptr, kern_ptr));
232 memcpy(kern_ptr, ext_ptr, len_to_copy);
233 }
234
235 ctsio->ext_data_filled += len_to_copy;
236 ctsio->kern_data_resid -= len_to_copy;
237
238 ext_watermark += len_to_copy;
239 if (ext_sglist[i].len == ext_watermark) {
240 i++;
241 ext_watermark = 0;
242 }
243
244 kern_watermark += len_to_copy;
245 if (kern_sglist[j].len == kern_watermark) {
246 j++;
247 kern_watermark = 0;
248 }
249 }
250
251 CTL_DEBUG_PRINT(("%s: ext_sg_entries: %d, kern_sg_entries: %d\n",
252 __func__, ext_sg_entries, kern_sg_entries));
253 CTL_DEBUG_PRINT(("%s: ext_data_len = %d, kern_data_len = %d\n",
254 __func__, ctsio->ext_data_len, ctsio->kern_data_len));
255
256bailout:
257 ctl_datamove_done(io, true);
258}
259
260static void
262{
263
264 tpc_done(io);
265}
266
267uint64_t
268tpcl_resolve(struct ctl_softc *softc, int init_port,
269 struct scsi_ec_cscd *cscd, uint32_t *ss, uint32_t *ps, uint32_t *pso)
270{
271 struct scsi_ec_cscd_id *cscdid;
272 struct ctl_port *port;
273 struct ctl_lun *lun;
274 uint64_t lunid = UINT64_MAX;
275
276 if (cscd->type_code != EC_CSCD_ID ||
277 (cscd->luidt_pdt & EC_LUIDT_MASK) != EC_LUIDT_LUN ||
278 (cscd->luidt_pdt & EC_NUL) != 0)
279 return (lunid);
280
281 cscdid = (struct scsi_ec_cscd_id *)cscd;
282 mtx_lock(&softc->ctl_lock);
283 if (init_port >= 0)
284 port = softc->ctl_ports[init_port];
285 else
286 port = NULL;
287 STAILQ_FOREACH(lun, &softc->lun_list, links) {
288 if (port != NULL &&
289 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
290 continue;
291 if (lun->lun_devid == NULL)
292 continue;
294 lun->lun_devid->len, &cscdid->codeset,
295 cscdid->length + 4) == 0) {
296 lunid = lun->lun;
297 if (ss)
298 *ss = lun->be_lun->blocksize;
299 if (ps)
300 *ps = lun->be_lun->blocksize <<
301 lun->be_lun->pblockexp;
302 if (pso)
303 *pso = lun->be_lun->blocksize *
304 lun->be_lun->pblockoff;
305 break;
306 }
307 }
308 mtx_unlock(&softc->ctl_lock);
309 return (lunid);
310};
311
312union ctl_io *
314{
315 struct tpcl_softc *tsoftc = &tpcl_softc;
316
317 return (ctl_alloc_io(tsoftc->port.ctl_pool_ref));
318};
319
320int
321tpcl_queue(union ctl_io *io, uint64_t lun)
322{
323 struct tpcl_softc *tsoftc = &tpcl_softc;
324
325 io->io_hdr.nexus.initid = 0;
326 io->io_hdr.nexus.targ_port = tsoftc->port.targ_port;
327 io->io_hdr.nexus.targ_lun = lun;
328 io->scsiio.tag_num = atomic_fetchadd_int(&tsoftc->cur_tag_num, 1);
329 io->scsiio.ext_data_filled = 0;
330 return (ctl_queue(io));
331}
uint32_t ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
Definition: ctl.c:3733
void ctl_datamove_done(union ctl_io *io, bool samethr)
Definition: ctl.c:12530
int ctl_queue(union ctl_io *io)
Definition: ctl.c:13270
union ctl_io * ctl_alloc_io(void *pool_ref)
Definition: ctl.c:3961
@ CTL_PORT_INTERNAL
Definition: ctl.h:55
#define CTL_DEBUG_PRINT(X)
Definition: ctl_debug.h:61
int ctl_port_deregister(struct ctl_port *port)
Definition: ctl_frontend.c:227
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
int ctl_port_register(struct ctl_port *port)
Definition: ctl_frontend.c:148
@ CTL_FLAG_DATA_MASK
Definition: ctl_io.h:90
@ CTL_FLAG_BUS_ADDR
Definition: ctl_io.h:102
@ CTL_FLAG_DATA_IN
Definition: ctl_io.h:87
void tpc_done(union ctl_io *io)
Definition: ctl_tpc.c:1603
static int tpcl_init(void)
Definition: ctl_tpc_local.c:83
uint64_t tpcl_resolve(struct ctl_softc *softc, int init_port, struct scsi_ec_cscd *cscd, uint32_t *ss, uint32_t *ps, uint32_t *pso)
static void tpcl_done(union ctl_io *io)
static int tpcl_shutdown(void)
__FBSDID("$FreeBSD$")
union ctl_io * tpcl_alloc_io(void)
static void tpcl_datamove(union ctl_io *io)
CTL_FRONTEND_DECLARE(ctltpc, tpcl_frontend)
static struct tpcl_softc tpcl_softc
Definition: ctl_tpc_local.c:67
static struct ctl_frontend tpcl_frontend
Definition: ctl_tpc_local.c:74
int tpcl_queue(union ctl_io *io, uint64_t lun)
int scsi_devid_match(uint8_t *lhs, size_t lhs_len, uint8_t *rhs, size_t rhs_len)
Definition: scsi_all.c:9227
#define SCSI_TRN_SPI_FORMAT_DEFAULT
Definition: scsi_all.h:485
static __inline void scsi_ulto2b(u_int32_t val, u_int8_t *bytes)
Definition: scsi_all.h:4374
#define EC_CSCD_ID
Definition: scsi_all.h:1814
#define SCSI_PROTO_SPI
Definition: scsi_all.h:858
#define EC_LUIDT_LUN
Definition: scsi_all.h:1804
#define EC_LUIDT_MASK
Definition: scsi_all.h:1803
#define EC_NUL
Definition: scsi_all.h:1802
uint32_t blocksize
Definition: ctl_backend.h:152
uint16_t pblockexp
Definition: ctl_backend.h:153
uint16_t pblockoff
Definition: ctl_backend.h:154
uint8_t data[]
Definition: ctl_private.h:370
char name[CTL_DRIVER_NAME_LEN]
Definition: ctl_frontend.h:252
struct ctl_nexus nexus
Definition: ctl_io.h:226
uint32_t flags
Definition: ctl_io.h:228
struct ctl_devid * lun_devid
Definition: ctl_private.h:415
uint64_t lun
Definition: ctl_private.h:380
struct ctl_be_lun * be_lun
Definition: ctl_private.h:385
uint32_t targ_lun
Definition: ctl_io.h:198
uint32_t targ_port
Definition: ctl_io.h:197
uint32_t initid
Definition: ctl_io.h:196
char * port_name
Definition: ctl_frontend.h:220
uint32_t max_initiators
Definition: ctl_frontend.h:236
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
void(* fe_datamove)(union ctl_io *io)
Definition: ctl_frontend.h:232
void * ctl_pool_ref
Definition: ctl_frontend.h:235
struct ctl_devid * init_devid
Definition: ctl_frontend.h:244
void(* fe_done)(union ctl_io *io)
Definition: ctl_frontend.h:233
uint32_t ext_data_len
Definition: ctl_io.h:283
uint32_t kern_sg_entries
Definition: ctl_io.h:290
uint8_t * ext_data_ptr
Definition: ctl_io.h:282
uint32_t kern_data_len
Definition: ctl_io.h:306
struct ctl_io_hdr io_hdr
Definition: ctl_io.h:275
uint32_t ext_data_filled
Definition: ctl_io.h:284
uint32_t tag_num
Definition: ctl_io.h:332
uint32_t ext_sg_entries
Definition: ctl_io.h:281
uint32_t kern_data_resid
Definition: ctl_io.h:317
uint8_t * kern_data_ptr
Definition: ctl_io.h:297
Definition: ctl_io.h:184
void * addr
Definition: ctl_io.h:185
size_t len
Definition: ctl_io.h:186
struct mtx ctl_lock
Definition: ctl_private.h:437
struct ctl_port ** ctl_ports
Definition: ctl_private.h:462
uint8_t length
Definition: scsi_all.h:1820
uint8_t codeset
Definition: scsi_all.h:1817
uint8_t type_code
Definition: scsi_all.h:1799
uint8_t luidt_pdt
Definition: scsi_all.h:1801
uint8_t rel_trgt_port_id[2]
Definition: scsi_all.h:489
uint8_t format_protocol
Definition: scsi_all.h:484
uint8_t scsi_addr[2]
Definition: scsi_all.h:487
struct ctl_port port
Definition: ctl_tpc_local.c:63
Definition: ctl_io.h:586
struct ctl_scsiio scsiio
Definition: ctl_io.h:588
struct ctl_io_hdr io_hdr
Definition: ctl_io.h:587