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