1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2014 Alexander Motin <[email protected]>
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>
51 #include <cam/ctl/ctl_frontend.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>
58 #include <cam/ctl/ctl_scsi_all.h>
59 #include <cam/ctl/ctl_tpc.h>
60 #include <cam/ctl/ctl_error.h>
61
62 struct tpcl_softc {
63 struct ctl_port port;
64 int cur_tag_num;
65 };
66
67 static struct tpcl_softc tpcl_softc;
68
69 static int tpcl_init(void);
70 static int tpcl_shutdown(void);
71 static void tpcl_datamove(union ctl_io *io);
72 static void tpcl_done(union ctl_io *io);
73
74
75 static struct ctl_frontend tpcl_frontend =
76 {
77 .name = "tpc",
78 .init = tpcl_init,
79 .shutdown = tpcl_shutdown,
80 };
81 CTL_FRONTEND_DECLARE(ctltpc, tpcl_frontend);
82
83 static int
tpcl_init(void)84 tpcl_init(void)
85 {
86 struct tpcl_softc *tsoftc = &tpcl_softc;
87 struct ctl_port *port;
88 struct scsi_transportid_spi *tid;
89 int error, len;
90
91 memset(tsoftc, 0, sizeof(*tsoftc));
92
93 port = &tsoftc->port;
94 port->frontend = &tpcl_frontend;
95 port->port_type = CTL_PORT_INTERNAL;
96 port->num_requested_ctl_io = 100;
97 port->port_name = "tpc";
98 port->fe_datamove = tpcl_datamove;
99 port->fe_done = tpcl_done;
100 port->targ_port = -1;
101 port->max_initiators = 1;
102
103 if ((error = ctl_port_register(port)) != 0) {
104 printf("%s: tpc port registration failed\n", __func__);
105 return (error);
106 }
107
108 len = sizeof(struct scsi_transportid_spi);
109 port->init_devid = malloc(sizeof(struct ctl_devid) + len,
110 M_CTL, M_WAITOK | M_ZERO);
111 port->init_devid->len = len;
112 tid = (struct scsi_transportid_spi *)port->init_devid->data;
113 tid->format_protocol = SCSI_TRN_SPI_FORMAT_DEFAULT | SCSI_PROTO_SPI;
114 scsi_ulto2b(0, tid->scsi_addr);
115 scsi_ulto2b(port->targ_port, tid->rel_trgt_port_id);
116
117 ctl_port_online(port);
118 return (0);
119 }
120
121 static int
tpcl_shutdown(void)122 tpcl_shutdown(void)
123 {
124 struct tpcl_softc *tsoftc = &tpcl_softc;
125 struct ctl_port *port = &tsoftc->port;
126 int error;
127
128 ctl_port_offline(port);
129 if ((error = ctl_port_deregister(port)) != 0)
130 printf("%s: tpc port deregistration failed\n", __func__);
131 return (error);
132 }
133
134 static void
tpcl_datamove(union ctl_io * io)135 tpcl_datamove(union ctl_io *io)
136 {
137 struct ctl_sg_entry *ext_sglist, *kern_sglist;
138 struct ctl_sg_entry ext_entry, kern_entry;
139 int ext_sg_entries, kern_sg_entries;
140 int ext_sg_start, ext_offset;
141 int len_to_copy;
142 int kern_watermark, ext_watermark;
143 struct ctl_scsiio *ctsio;
144 int i, j;
145
146 CTL_DEBUG_PRINT(("%s\n", __func__));
147
148 ctsio = &io->scsiio;
149
150 /*
151 * If this is the case, we're probably doing a BBR read and don't
152 * actually need to transfer the data. This will effectively
153 * bit-bucket the data.
154 */
155 if (ctsio->ext_data_ptr == NULL)
156 goto bailout;
157
158 /*
159 * To simplify things here, if we have a single buffer, stick it in
160 * a S/G entry and just make it a single entry S/G list.
161 */
162 if (ctsio->ext_sg_entries > 0) {
163 int len_seen;
164
165 ext_sglist = (struct ctl_sg_entry *)ctsio->ext_data_ptr;
166 ext_sg_entries = ctsio->ext_sg_entries;
167 ext_sg_start = 0;
168 ext_offset = 0;
169 len_seen = 0;
170 for (i = 0; i < ext_sg_entries; i++) {
171 if ((len_seen + ext_sglist[i].len) >=
172 ctsio->ext_data_filled) {
173 ext_sg_start = i;
174 ext_offset = ctsio->ext_data_filled - len_seen;
175 break;
176 }
177 len_seen += ext_sglist[i].len;
178 }
179 } else {
180 ext_sglist = &ext_entry;
181 ext_sglist->addr = ctsio->ext_data_ptr;
182 ext_sglist->len = ctsio->ext_data_len;
183 ext_sg_entries = 1;
184 ext_sg_start = 0;
185 ext_offset = ctsio->ext_data_filled;
186 }
187
188 if (ctsio->kern_sg_entries > 0) {
189 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
190 kern_sg_entries = ctsio->kern_sg_entries;
191 } else {
192 kern_sglist = &kern_entry;
193 kern_sglist->addr = ctsio->kern_data_ptr;
194 kern_sglist->len = ctsio->kern_data_len;
195 kern_sg_entries = 1;
196 }
197
198 kern_watermark = 0;
199 ext_watermark = ext_offset;
200 for (i = ext_sg_start, j = 0;
201 i < ext_sg_entries && j < kern_sg_entries;) {
202 uint8_t *ext_ptr, *kern_ptr;
203
204 len_to_copy = min(ext_sglist[i].len - ext_watermark,
205 kern_sglist[j].len - kern_watermark);
206
207 ext_ptr = (uint8_t *)ext_sglist[i].addr;
208 ext_ptr = ext_ptr + ext_watermark;
209 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
210 /*
211 * XXX KDM fix this!
212 */
213 panic("need to implement bus address support");
214 #if 0
215 kern_ptr = bus_to_virt(kern_sglist[j].addr);
216 #endif
217 } else
218 kern_ptr = (uint8_t *)kern_sglist[j].addr;
219 kern_ptr = kern_ptr + kern_watermark;
220
221 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
222 CTL_FLAG_DATA_IN) {
223 CTL_DEBUG_PRINT(("%s: copying %d bytes to user\n",
224 __func__, len_to_copy));
225 CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
226 kern_ptr, ext_ptr));
227 memcpy(ext_ptr, kern_ptr, len_to_copy);
228 } else {
229 CTL_DEBUG_PRINT(("%s: copying %d bytes from user\n",
230 __func__, len_to_copy));
231 CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
232 ext_ptr, kern_ptr));
233 memcpy(kern_ptr, ext_ptr, len_to_copy);
234 }
235
236 ctsio->ext_data_filled += len_to_copy;
237 ctsio->kern_data_resid -= len_to_copy;
238
239 ext_watermark += len_to_copy;
240 if (ext_sglist[i].len == ext_watermark) {
241 i++;
242 ext_watermark = 0;
243 }
244
245 kern_watermark += len_to_copy;
246 if (kern_sglist[j].len == kern_watermark) {
247 j++;
248 kern_watermark = 0;
249 }
250 }
251
252 CTL_DEBUG_PRINT(("%s: ext_sg_entries: %d, kern_sg_entries: %d\n",
253 __func__, ext_sg_entries, kern_sg_entries));
254 CTL_DEBUG_PRINT(("%s: ext_data_len = %d, kern_data_len = %d\n",
255 __func__, ctsio->ext_data_len, ctsio->kern_data_len));
256
257 bailout:
258 io->scsiio.be_move_done(io);
259 }
260
261 static void
tpcl_done(union ctl_io * io)262 tpcl_done(union ctl_io *io)
263 {
264
265 tpc_done(io);
266 }
267
268 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)269 tpcl_resolve(struct ctl_softc *softc, int init_port,
270 struct scsi_ec_cscd *cscd, uint32_t *ss, uint32_t *ps, uint32_t *pso)
271 {
272 struct scsi_ec_cscd_id *cscdid;
273 struct ctl_port *port;
274 struct ctl_lun *lun;
275 uint64_t lunid = UINT64_MAX;
276
277 if (cscd->type_code != EC_CSCD_ID ||
278 (cscd->luidt_pdt & EC_LUIDT_MASK) != EC_LUIDT_LUN ||
279 (cscd->luidt_pdt & EC_NUL) != 0)
280 return (lunid);
281
282 cscdid = (struct scsi_ec_cscd_id *)cscd;
283 mtx_lock(&softc->ctl_lock);
284 if (init_port >= 0)
285 port = softc->ctl_ports[init_port];
286 else
287 port = NULL;
288 STAILQ_FOREACH(lun, &softc->lun_list, links) {
289 if (port != NULL &&
290 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
291 continue;
292 if (lun->lun_devid == NULL)
293 continue;
294 if (scsi_devid_match(lun->lun_devid->data,
295 lun->lun_devid->len, &cscdid->codeset,
296 cscdid->length + 4) == 0) {
297 lunid = lun->lun;
298 if (ss && lun->be_lun)
299 *ss = lun->be_lun->blocksize;
300 if (ps && lun->be_lun)
301 *ps = lun->be_lun->blocksize <<
302 lun->be_lun->pblockexp;
303 if (pso && lun->be_lun)
304 *pso = lun->be_lun->blocksize *
305 lun->be_lun->pblockoff;
306 break;
307 }
308 }
309 mtx_unlock(&softc->ctl_lock);
310 return (lunid);
311 };
312
313 union ctl_io *
tpcl_alloc_io(void)314 tpcl_alloc_io(void)
315 {
316 struct tpcl_softc *tsoftc = &tpcl_softc;
317
318 return (ctl_alloc_io(tsoftc->port.ctl_pool_ref));
319 };
320
321 int
tpcl_queue(union ctl_io * io,uint64_t lun)322 tpcl_queue(union ctl_io *io, uint64_t lun)
323 {
324 struct tpcl_softc *tsoftc = &tpcl_softc;
325
326 io->io_hdr.nexus.initid = 0;
327 io->io_hdr.nexus.targ_port = tsoftc->port.targ_port;
328 io->io_hdr.nexus.targ_lun = lun;
329 io->scsiio.tag_num = atomic_fetchadd_int(&tsoftc->cur_tag_num, 1);
330 io->scsiio.ext_data_filled = 0;
331 return (ctl_queue(io));
332 }
333