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