]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/ctl/ctl_frontend_ioctl.c
MFC r287621: Reimplement CTL High Availability.
[FreeBSD/stable/10.git] / sys / cam / ctl / ctl_frontend_ioctl.c
1 /*-
2  * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3  * Copyright (c) 2012 The FreeBSD Foundation
4  * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/types.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/malloc.h>
41 #include <sys/conf.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44
45 #include <cam/cam.h>
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/scsi_da.h>
48 #include <cam/ctl/ctl_io.h>
49 #include <cam/ctl/ctl.h>
50 #include <cam/ctl/ctl_frontend.h>
51 #include <cam/ctl/ctl_util.h>
52 #include <cam/ctl/ctl_backend.h>
53 #include <cam/ctl/ctl_ioctl.h>
54 #include <cam/ctl/ctl_ha.h>
55 #include <cam/ctl/ctl_private.h>
56 #include <cam/ctl/ctl_debug.h>
57 #include <cam/ctl/ctl_error.h>
58
59 struct cfi_softc {
60         uint32_t                cur_tag_num;
61         struct ctl_port         port;
62 };
63
64 static struct cfi_softc cfi_softc;
65
66 static int cfi_init(void);
67 static void cfi_shutdown(void);
68 static void cfi_datamove(union ctl_io *io);
69 static void cfi_done(union ctl_io *io);
70
71 static struct ctl_frontend cfi_frontend =
72 {
73         .name = "ioctl",
74         .init = cfi_init,
75         .shutdown = cfi_shutdown,
76 };
77 CTL_FRONTEND_DECLARE(ctlioctl, cfi_frontend);
78
79 static int
80 cfi_init(void)
81 {
82         struct cfi_softc *isoftc = &cfi_softc;
83         struct ctl_port *port;
84
85         memset(isoftc, 0, sizeof(*isoftc));
86
87         port = &isoftc->port;
88         port->frontend = &cfi_frontend;
89         port->port_type = CTL_PORT_IOCTL;
90         port->num_requested_ctl_io = 100;
91         port->port_name = "ioctl";
92         port->fe_datamove = cfi_datamove;
93         port->fe_done = cfi_done;
94         port->max_targets = 1;
95         port->max_target_id = 0;
96         port->targ_port = -1;
97         port->max_initiators = 1;
98
99         if (ctl_port_register(port) != 0) {
100                 printf("%s: ioctl port registration failed\n", __func__);
101                 return (0);
102         }
103         ctl_port_online(port);
104         return (0);
105 }
106
107 void
108 cfi_shutdown(void)
109 {
110         struct cfi_softc *isoftc = &cfi_softc;
111         struct ctl_port *port;
112
113         port = &isoftc->port;
114         ctl_port_offline(port);
115         if (ctl_port_deregister(&isoftc->port) != 0)
116                 printf("%s: ctl_frontend_deregister() failed\n", __func__);
117 }
118
119 /*
120  * Data movement routine for the CTL ioctl frontend port.
121  */
122 static int
123 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
124 {
125         struct ctl_sg_entry *ext_sglist, *kern_sglist;
126         struct ctl_sg_entry ext_entry, kern_entry;
127         int ext_sglen, ext_sg_entries, kern_sg_entries;
128         int ext_sg_start, ext_offset;
129         int len_to_copy, len_copied;
130         int kern_watermark, ext_watermark;
131         int ext_sglist_malloced;
132         int i, j;
133
134         ext_sglist_malloced = 0;
135         ext_sg_start = 0;
136         ext_offset = 0;
137
138         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
139
140         /*
141          * If this flag is set, fake the data transfer.
142          */
143         if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
144                 ctsio->ext_data_filled = ctsio->ext_data_len;
145                 goto bailout;
146         }
147
148         /*
149          * To simplify things here, if we have a single buffer, stick it in
150          * a S/G entry and just make it a single entry S/G list.
151          */
152         if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
153                 int len_seen;
154
155                 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
156
157                 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
158                                                            M_WAITOK);
159                 ext_sglist_malloced = 1;
160                 if (copyin(ctsio->ext_data_ptr, ext_sglist,
161                                    ext_sglen) != 0) {
162                         ctl_set_internal_failure(ctsio,
163                                                  /*sks_valid*/ 0,
164                                                  /*retry_count*/ 0);
165                         goto bailout;
166                 }
167                 ext_sg_entries = ctsio->ext_sg_entries;
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
198         kern_watermark = 0;
199         ext_watermark = ext_offset;
200         len_copied = 0;
201         for (i = ext_sg_start, j = 0;
202              i < ext_sg_entries && j < kern_sg_entries;) {
203                 uint8_t *ext_ptr, *kern_ptr;
204
205                 len_to_copy = MIN(ext_sglist[i].len - ext_watermark,
206                                   kern_sglist[j].len - kern_watermark);
207
208                 ext_ptr = (uint8_t *)ext_sglist[i].addr;
209                 ext_ptr = ext_ptr + ext_watermark;
210                 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
211                         /*
212                          * XXX KDM fix this!
213                          */
214                         panic("need to implement bus address support");
215 #if 0
216                         kern_ptr = bus_to_virt(kern_sglist[j].addr);
217 #endif
218                 } else
219                         kern_ptr = (uint8_t *)kern_sglist[j].addr;
220                 kern_ptr = kern_ptr + kern_watermark;
221
222                 kern_watermark += len_to_copy;
223                 ext_watermark += len_to_copy;
224
225                 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
226                      CTL_FLAG_DATA_IN) {
227                         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
228                                          "bytes to user\n", len_to_copy));
229                         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
230                                          "to %p\n", kern_ptr, ext_ptr));
231                         if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
232                                 ctl_set_internal_failure(ctsio,
233                                                          /*sks_valid*/ 0,
234                                                          /*retry_count*/ 0);
235                                 goto bailout;
236                         }
237                 } else {
238                         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
239                                          "bytes from user\n", len_to_copy));
240                         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
241                                          "to %p\n", ext_ptr, kern_ptr));
242                         if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
243                                 ctl_set_internal_failure(ctsio,
244                                                          /*sks_valid*/ 0,
245                                                          /*retry_count*/0);
246                                 goto bailout;
247                         }
248                 }
249
250                 len_copied += len_to_copy;
251
252                 if (ext_sglist[i].len == ext_watermark) {
253                         i++;
254                         ext_watermark = 0;
255                 }
256
257                 if (kern_sglist[j].len == kern_watermark) {
258                         j++;
259                         kern_watermark = 0;
260                 }
261         }
262
263         ctsio->ext_data_filled += len_copied;
264
265         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
266                          "kern_sg_entries: %d\n", ext_sg_entries,
267                          kern_sg_entries));
268         CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
269                          "kern_data_len = %d\n", ctsio->ext_data_len,
270                          ctsio->kern_data_len));
271
272
273         /* XXX KDM set residual?? */
274 bailout:
275
276         if (ext_sglist_malloced != 0)
277                 free(ext_sglist, M_CTL);
278
279         return (CTL_RETVAL_COMPLETE);
280 }
281
282 static void
283 cfi_datamove(union ctl_io *io)
284 {
285         struct ctl_fe_ioctl_params *params;
286
287         params = (struct ctl_fe_ioctl_params *)
288                 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
289
290         mtx_lock(&params->ioctl_mtx);
291         params->state = CTL_IOCTL_DATAMOVE;
292         cv_broadcast(&params->sem);
293         mtx_unlock(&params->ioctl_mtx);
294 }
295
296 static void
297 cfi_done(union ctl_io *io)
298 {
299         struct ctl_fe_ioctl_params *params;
300
301         params = (struct ctl_fe_ioctl_params *)
302                 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
303
304         mtx_lock(&params->ioctl_mtx);
305         params->state = CTL_IOCTL_DONE;
306         cv_broadcast(&params->sem);
307         mtx_unlock(&params->ioctl_mtx);
308 }
309
310 static int
311 cfi_submit_wait(union ctl_io *io)
312 {
313         struct ctl_fe_ioctl_params params;
314         ctl_fe_ioctl_state last_state;
315         int done, retval;
316
317         retval = 0;
318
319         bzero(&params, sizeof(params));
320
321         mtx_init(&params.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
322         cv_init(&params.sem, "ctlioccv");
323         params.state = CTL_IOCTL_INPROG;
324         last_state = params.state;
325
326         io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = &params;
327
328         CTL_DEBUG_PRINT(("cfi_submit_wait\n"));
329
330         /* This shouldn't happen */
331         if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
332                 return (retval);
333
334         done = 0;
335
336         do {
337                 mtx_lock(&params.ioctl_mtx);
338                 /*
339                  * Check the state here, and don't sleep if the state has
340                  * already changed (i.e. wakeup has already occured, but we
341                  * weren't waiting yet).
342                  */
343                 if (params.state == last_state) {
344                         /* XXX KDM cv_wait_sig instead? */
345                         cv_wait(&params.sem, &params.ioctl_mtx);
346                 }
347                 last_state = params.state;
348
349                 switch (params.state) {
350                 case CTL_IOCTL_INPROG:
351                         /* Why did we wake up? */
352                         /* XXX KDM error here? */
353                         mtx_unlock(&params.ioctl_mtx);
354                         break;
355                 case CTL_IOCTL_DATAMOVE:
356                         CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
357
358                         /*
359                          * change last_state back to INPROG to avoid
360                          * deadlock on subsequent data moves.
361                          */
362                         params.state = last_state = CTL_IOCTL_INPROG;
363
364                         mtx_unlock(&params.ioctl_mtx);
365                         ctl_ioctl_do_datamove(&io->scsiio);
366                         /*
367                          * Note that in some cases, most notably writes,
368                          * this will queue the I/O and call us back later.
369                          * In other cases, generally reads, this routine
370                          * will immediately call back and wake us up,
371                          * probably using our own context.
372                          */
373                         io->scsiio.be_move_done(io);
374                         break;
375                 case CTL_IOCTL_DONE:
376                         mtx_unlock(&params.ioctl_mtx);
377                         CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
378                         done = 1;
379                         break;
380                 default:
381                         mtx_unlock(&params.ioctl_mtx);
382                         /* XXX KDM error here? */
383                         break;
384                 }
385         } while (done == 0);
386
387         mtx_destroy(&params.ioctl_mtx);
388         cv_destroy(&params.sem);
389
390         return (CTL_RETVAL_COMPLETE);
391 }
392
393 int
394 ctl_ioctl_io(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
395     struct thread *td)
396 {
397         union ctl_io *io;
398         void *pool_tmp;
399         int retval = 0;
400
401         /*
402          * If we haven't been "enabled", don't allow any SCSI I/O
403          * to this FETD.
404          */
405         if ((cfi_softc.port.status & CTL_PORT_STATUS_ONLINE) == 0)
406                 return (EPERM);
407
408         io = ctl_alloc_io(cfi_softc.port.ctl_pool_ref);
409
410         /*
411          * Need to save the pool reference so it doesn't get
412          * spammed by the user's ctl_io.
413          */
414         pool_tmp = io->io_hdr.pool;
415         memcpy(io, (void *)addr, sizeof(*io));
416         io->io_hdr.pool = pool_tmp;
417
418         /*
419          * No status yet, so make sure the status is set properly.
420          */
421         io->io_hdr.status = CTL_STATUS_NONE;
422
423         /*
424          * The user sets the initiator ID, target and LUN IDs.
425          */
426         io->io_hdr.nexus.targ_port = cfi_softc.port.targ_port;
427         io->io_hdr.flags |= CTL_FLAG_USER_REQ;
428         if ((io->io_hdr.io_type == CTL_IO_SCSI) &&
429             (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
430                 io->scsiio.tag_num = cfi_softc.cur_tag_num++;
431
432         retval = cfi_submit_wait(io);
433         if (retval == 0)
434                 memcpy((void *)addr, io, sizeof(*io));
435         ctl_free_io(io);
436         return (retval);
437 }