]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/ctl/ctl_tpc_local.c
Drop "internal" CTL frontend.
[FreeBSD/FreeBSD.git] / sys / cam / ctl / ctl_tpc_local.c
1 /*-
2  * Copyright (c) 2014 Alexander Motin <mav@FreeBSD.org>
3  * Copyright (c) 2004, 2005 Silicon Graphics International Corp.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
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         int cur_tag_num;
63 };
64
65 static struct tpcl_softc tpcl_softc;
66
67 static int tpcl_init(void);
68 static void tpcl_shutdown(void);
69 static void tpcl_online(void *arg);
70 static void tpcl_offline(void *arg);
71 static int tpcl_lun_enable(void *arg, int lun_id);
72 static int tpcl_lun_disable(void *arg, int lun_id);
73 static void tpcl_datamove(union ctl_io *io);
74 static void tpcl_done(union ctl_io *io);
75
76
77 static struct ctl_frontend tpcl_frontend =
78 {
79         .name = "tpc",
80         .init = tpcl_init,
81         .shutdown = tpcl_shutdown,
82 };
83 CTL_FRONTEND_DECLARE(ctltpc, tpcl_frontend);
84
85 static int
86 tpcl_init(void)
87 {
88         struct tpcl_softc *tsoftc = &tpcl_softc;
89         struct ctl_port *port;
90         struct scsi_transportid_spi *tid;
91         int len;
92
93         memset(tsoftc, 0, sizeof(*tsoftc));
94
95         port = &tsoftc->port;
96         port->frontend = &tpcl_frontend;
97         port->port_type = CTL_PORT_INTERNAL;
98         port->num_requested_ctl_io = 100;
99         port->port_name = "tpc";
100         port->port_online = tpcl_online;
101         port->port_offline = tpcl_offline;
102         port->onoff_arg = tsoftc;
103         port->lun_enable = tpcl_lun_enable;
104         port->lun_disable = tpcl_lun_disable;
105         port->targ_lun_arg = tsoftc;
106         port->fe_datamove = tpcl_datamove;
107         port->fe_done = tpcl_done;
108         port->max_targets = 1;
109         port->max_target_id = 0;
110         port->max_initiators = 1;
111
112         if (ctl_port_register(port) != 0)
113         {
114                 printf("%s: tpc frontend registration failed\n", __func__);
115                 return (0);
116         }
117
118         len = sizeof(struct scsi_transportid_spi);
119         port->init_devid = malloc(sizeof(struct ctl_devid) + len,
120             M_CTL, M_WAITOK | M_ZERO);
121         port->init_devid->len = len;
122         tid = (struct scsi_transportid_spi *)port->init_devid->data;
123         tid->format_protocol = SCSI_TRN_SPI_FORMAT_DEFAULT | SCSI_PROTO_SPI;
124         scsi_ulto2b(0, tid->scsi_addr);
125         scsi_ulto2b(port->targ_port, tid->rel_trgt_port_id);
126
127         ctl_port_online(port);
128         return (0);
129 }
130
131 void
132 tpcl_shutdown(void)
133 {
134         struct tpcl_softc *tsoftc = &tpcl_softc;
135         struct ctl_port *port;
136
137         port = &tsoftc->port;
138         ctl_port_offline(port);
139         if (ctl_port_deregister(&tsoftc->port) != 0)
140                 printf("%s: ctl_frontend_deregister() failed\n", __func__);
141 }
142
143 static void
144 tpcl_online(void *arg)
145 {
146 }
147
148 static void
149 tpcl_offline(void *arg)
150 {
151 }
152
153 static int
154 tpcl_lun_enable(void *arg, int lun_id)
155 {
156
157         return (0);
158 }
159
160 static int
161 tpcl_lun_disable(void *arg, int lun_id)
162 {
163
164         return (0);
165 }
166
167 static void
168 tpcl_datamove(union ctl_io *io)
169 {
170         struct ctl_sg_entry *ext_sglist, *kern_sglist;
171         struct ctl_sg_entry ext_entry, kern_entry;
172         int ext_sg_entries, kern_sg_entries;
173         int ext_sg_start, ext_offset;
174         int len_to_copy, len_copied;
175         int kern_watermark, ext_watermark;
176         struct ctl_scsiio *ctsio;
177         int i, j;
178
179         ext_sg_start = 0;
180         ext_offset = 0;
181         ext_sglist = NULL;
182
183         CTL_DEBUG_PRINT(("%s\n", __func__));
184
185         ctsio = &io->scsiio;
186
187         /*
188          * If this is the case, we're probably doing a BBR read and don't
189          * actually need to transfer the data.  This will effectively
190          * bit-bucket the data.
191          */
192         if (ctsio->ext_data_ptr == NULL)
193                 goto bailout;
194
195         /*
196          * To simplify things here, if we have a single buffer, stick it in
197          * a S/G entry and just make it a single entry S/G list.
198          */
199         if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
200                 int len_seen;
201
202                 ext_sglist = (struct ctl_sg_entry *)ctsio->ext_data_ptr;
203                 ext_sg_entries = ctsio->ext_sg_entries;
204                 ext_sg_start = 0;
205                 ext_offset = 0;
206                 len_seen = 0;
207                 for (i = 0; i < ext_sg_entries; i++) {
208                         if ((len_seen + ext_sglist[i].len) >=
209                              ctsio->ext_data_filled) {
210                                 ext_sg_start = i;
211                                 ext_offset = ctsio->ext_data_filled - len_seen;
212                                 break;
213                         }
214                         len_seen += ext_sglist[i].len;
215                 }
216         } else {
217                 ext_sglist = &ext_entry;
218                 ext_sglist->addr = ctsio->ext_data_ptr;
219                 ext_sglist->len = ctsio->ext_data_len;
220                 ext_sg_entries = 1;
221                 ext_sg_start = 0;
222                 ext_offset = ctsio->ext_data_filled;
223         }
224
225         if (ctsio->kern_sg_entries > 0) {
226                 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
227                 kern_sg_entries = ctsio->kern_sg_entries;
228         } else {
229                 kern_sglist = &kern_entry;
230                 kern_sglist->addr = ctsio->kern_data_ptr;
231                 kern_sglist->len = ctsio->kern_data_len;
232                 kern_sg_entries = 1;
233         }
234
235         kern_watermark = 0;
236         ext_watermark = ext_offset;
237         len_copied = 0;
238         for (i = ext_sg_start, j = 0;
239              i < ext_sg_entries && j < kern_sg_entries;) {
240                 uint8_t *ext_ptr, *kern_ptr;
241
242                 len_to_copy = min(ext_sglist[i].len - ext_watermark,
243                                   kern_sglist[j].len - kern_watermark);
244
245                 ext_ptr = (uint8_t *)ext_sglist[i].addr;
246                 ext_ptr = ext_ptr + ext_watermark;
247                 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
248                         /*
249                          * XXX KDM fix this!
250                          */
251                         panic("need to implement bus address support");
252 #if 0
253                         kern_ptr = bus_to_virt(kern_sglist[j].addr);
254 #endif
255                 } else
256                         kern_ptr = (uint8_t *)kern_sglist[j].addr;
257                 kern_ptr = kern_ptr + kern_watermark;
258
259                 kern_watermark += len_to_copy;
260                 ext_watermark += len_to_copy;
261                 
262                 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
263                      CTL_FLAG_DATA_IN) {
264                         CTL_DEBUG_PRINT(("%s: copying %d bytes to user\n",
265                                          __func__, len_to_copy));
266                         CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
267                                          kern_ptr, ext_ptr));
268                         memcpy(ext_ptr, kern_ptr, len_to_copy);
269                 } else {
270                         CTL_DEBUG_PRINT(("%s: copying %d bytes from user\n",
271                                          __func__, len_to_copy));
272                         CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
273                                          ext_ptr, kern_ptr));
274                         memcpy(kern_ptr, ext_ptr, len_to_copy);
275                 }
276
277                 len_copied += len_to_copy;
278
279                 if (ext_sglist[i].len == ext_watermark) {
280                         i++;
281                         ext_watermark = 0;
282                 }
283
284                 if (kern_sglist[j].len == kern_watermark) {
285                         j++;
286                         kern_watermark = 0;
287                 }
288         }
289
290         ctsio->ext_data_filled += len_copied;
291
292         CTL_DEBUG_PRINT(("%s: ext_sg_entries: %d, kern_sg_entries: %d\n",
293                          __func__, ext_sg_entries, kern_sg_entries));
294         CTL_DEBUG_PRINT(("%s: ext_data_len = %d, kern_data_len = %d\n",
295                          __func__, ctsio->ext_data_len, ctsio->kern_data_len));
296
297         /* XXX KDM set residual?? */
298 bailout:
299         io->scsiio.be_move_done(io);
300 }
301
302 static void
303 tpcl_done(union ctl_io *io)
304 {
305
306         tpc_done(io);
307 }
308
309 uint64_t
310 tpcl_resolve(struct ctl_softc *softc, int init_port,
311     struct scsi_ec_cscd *cscd, uint32_t *ss, uint32_t *ps, uint32_t *pso)
312 {
313         struct scsi_ec_cscd_id *cscdid;
314         struct ctl_port *port;
315         struct ctl_lun *lun;
316         uint64_t lunid = UINT64_MAX;
317
318         if (cscd->type_code != EC_CSCD_ID)
319                 return (lunid);
320
321         cscdid = (struct scsi_ec_cscd_id *)cscd;
322         mtx_lock(&softc->ctl_lock);
323         if (init_port >= 0)
324                 port = softc->ctl_ports[ctl_port_idx(init_port)];
325         else
326                 port = NULL;
327         STAILQ_FOREACH(lun, &softc->lun_list, links) {
328                 if (port != NULL &&
329                     ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
330                         continue;
331                 if (lun->lun_devid == NULL)
332                         continue;
333                 if (scsi_devid_match(lun->lun_devid->data,
334                     lun->lun_devid->len, &cscdid->codeset,
335                     cscdid->length + 4) == 0) {
336                         lunid = lun->lun;
337                         if (ss && lun->be_lun)
338                                 *ss = lun->be_lun->blocksize;
339                         if (ps && lun->be_lun)
340                                 *ps = lun->be_lun->blocksize <<
341                                     lun->be_lun->pblockexp;
342                         if (pso && lun->be_lun)
343                                 *pso = lun->be_lun->blocksize *
344                                     lun->be_lun->pblockoff;
345                         break;
346                 }
347         }
348         mtx_unlock(&softc->ctl_lock);
349         return (lunid);
350 };
351
352 union ctl_io *
353 tpcl_alloc_io(void)
354 {
355         struct tpcl_softc *tsoftc = &tpcl_softc;
356
357         return (ctl_alloc_io(tsoftc->port.ctl_pool_ref));
358 };
359
360 int
361 tpcl_queue(union ctl_io *io, uint64_t lun)
362 {
363         struct tpcl_softc *tsoftc = &tpcl_softc;
364
365         io->io_hdr.nexus.initid.id = 0;
366         io->io_hdr.nexus.targ_port = tsoftc->port.targ_port;
367         io->io_hdr.nexus.targ_target.id = 0;
368         io->io_hdr.nexus.targ_lun = lun;
369         io->scsiio.tag_num = atomic_fetchadd_int(&tsoftc->cur_tag_num, 1);
370         io->scsiio.ext_data_filled = 0;
371         return (ctl_queue(io));
372 }