2 * linux/drivers/misc/xillybus_core.c
4 * Copyright 2011 Xillybus Ltd, http://xillybus.com
6 * Driver for the Xillybus FPGA/host framework.
8 * This driver interfaces with a special IP core in an FPGA, setting up
9 * a pipe between a hardware FIFO in the programmable logic and a device
10 * file in the host. The number of such pipes and their attributes are
11 * set up on the logic. This driver detects these automatically and
12 * creates the device files accordingly.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the smems of the GNU General Public License as published by
16 * the Free Software Foundation; version 2 of the License.
19 #include <linux/list.h>
20 #include <linux/device.h>
21 #include <linux/module.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/interrupt.h>
25 #include <linux/sched.h>
27 #include <linux/cdev.h>
28 #include <linux/spinlock.h>
29 #include <linux/mutex.h>
30 #include <linux/crc32.h>
31 #include <linux/poll.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/workqueue.h>
37 MODULE_DESCRIPTION("Xillybus core functions");
38 MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
39 MODULE_VERSION("1.07");
40 MODULE_ALIAS("xillybus_core");
41 MODULE_LICENSE("GPL v2");
43 /* General timeout is 100 ms, rx timeout is 10 ms */
44 #define XILLY_RX_TIMEOUT (10*HZ/1000)
45 #define XILLY_TIMEOUT (100*HZ/1000)
47 #define fpga_msg_ctrl_reg 0x0002
48 #define fpga_dma_control_reg 0x0008
49 #define fpga_dma_bufno_reg 0x0009
50 #define fpga_dma_bufaddr_lowaddr_reg 0x000a
51 #define fpga_dma_bufaddr_highaddr_reg 0x000b
52 #define fpga_buf_ctrl_reg 0x000c
53 #define fpga_buf_offset_reg 0x000d
54 #define fpga_endian_reg 0x0010
56 #define XILLYMSG_OPCODE_RELEASEBUF 1
57 #define XILLYMSG_OPCODE_QUIESCEACK 2
58 #define XILLYMSG_OPCODE_FIFOEOF 3
59 #define XILLYMSG_OPCODE_FATAL_ERROR 4
60 #define XILLYMSG_OPCODE_NONEMPTY 5
62 static const char xillyname[] = "xillybus";
64 static struct class *xillybus_class;
67 * ep_list_lock is the last lock to be taken; No other lock requests are
68 * allowed while holding it. It merely protects list_of_endpoints, and not
69 * the endpoints listed in it.
72 static LIST_HEAD(list_of_endpoints);
73 static struct mutex ep_list_lock;
74 static struct workqueue_struct *xillybus_wq;
77 * Locking scheme: Mutexes protect invocations of character device methods.
78 * If both locks are taken, wr_mutex is taken first, rd_mutex second.
80 * wr_spinlock protects wr_*_buf_idx, wr_empty, wr_sleepy, wr_ready and the
81 * buffers' end_offset fields against changes made by IRQ handler (and in
82 * theory, other file request handlers, but the mutex handles that). Nothing
84 * They are held for short direct memory manipulations. Needless to say,
85 * no mutex locking is allowed when a spinlock is held.
87 * rd_spinlock does the same with rd_*_buf_idx, rd_empty and end_offset.
89 * register_mutex is endpoint-specific, and is held when non-atomic
90 * register operations are performed. wr_mutex and rd_mutex may be
91 * held when register_mutex is taken, but none of the spinlocks. Note that
92 * register_mutex doesn't protect against sporadic buf_ctrl_reg writes
93 * which are unrelated to buf_offset_reg, since they are harmless.
95 * Blocking on the wait queues is allowed with mutexes held, but not with
98 * Only interruptible blocking is allowed on mutexes and wait queues.
100 * All in all, the locking order goes (with skips allowed, of course):
101 * wr_mutex -> rd_mutex -> register_mutex -> wr_spinlock -> rd_spinlock
104 static void malformed_message(u32 *buf)
107 int msg_channel, msg_bufno, msg_data, msg_dir;
109 opcode = (buf[0] >> 24) & 0xff;
110 msg_dir = buf[0] & 1;
111 msg_channel = (buf[0] >> 1) & 0x7ff;
112 msg_bufno = (buf[0] >> 12) & 0x3ff;
113 msg_data = buf[1] & 0xfffffff;
115 pr_warn("xillybus: Malformed message (skipping): "
116 "opcode=%d, channel=%03x, dir=%d, bufno=%03x, data=%07x\n",
117 opcode, msg_channel, msg_dir, msg_bufno, msg_data);
121 * xillybus_isr assumes the interrupt is allocated exclusively to it,
122 * which is the natural case MSI and several other hardware-oriented
123 * interrupts. Sharing is not allowed.
126 irqreturn_t xillybus_isr(int irq, void *data)
128 struct xilly_endpoint *ep = data;
130 unsigned int buf_size;
133 unsigned int msg_channel, msg_bufno, msg_data, msg_dir;
134 struct xilly_channel *channel;
137 * The endpoint structure is altered during periods when it's
138 * guaranteed no interrupt will occur, but in theory, the cache
139 * lines may not be updated. So a memory barrier is issued.
144 buf = ep->msgbuf_addr;
145 buf_size = ep->msg_buf_size/sizeof(u32);
148 ep->ephw->hw_sync_sgl_for_cpu(ep,
153 for (i = 0; i < buf_size; i += 2)
154 if (((buf[i+1] >> 28) & 0xf) != ep->msg_counter) {
155 malformed_message(&buf[i]);
156 pr_warn("xillybus: Sending a NACK on "
157 "counter %x (instead of %x) on entry %d\n",
158 ((buf[i+1] >> 28) & 0xf),
162 if (++ep->failed_messages > 10)
163 pr_err("xillybus: Lost sync with "
164 "interrupt messages. Stopping.\n");
166 ep->ephw->hw_sync_sgl_for_device(
172 iowrite32(0x01, /* Message NACK */
173 &ep->registers[fpga_msg_ctrl_reg]);
176 } else if (buf[i] & (1 << 22)) /* Last message */
180 pr_err("xillybus: Bad interrupt message. Stopping.\n");
186 for (i = 0; i <= buf_size; i += 2) { /* Scan through messages */
187 opcode = (buf[i] >> 24) & 0xff;
189 msg_dir = buf[i] & 1;
190 msg_channel = (buf[i] >> 1) & 0x7ff;
191 msg_bufno = (buf[i] >> 12) & 0x3ff;
192 msg_data = buf[i+1] & 0xfffffff;
195 case XILLYMSG_OPCODE_RELEASEBUF:
197 if ((msg_channel > ep->num_channels) ||
198 (msg_channel == 0)) {
199 malformed_message(&buf[i]);
203 channel = ep->channels[msg_channel];
205 if (msg_dir) { /* Write channel */
206 if (msg_bufno >= channel->num_wr_buffers) {
207 malformed_message(&buf[i]);
210 spin_lock(&channel->wr_spinlock);
211 channel->wr_buffers[msg_bufno]->end_offset =
213 channel->wr_fpga_buf_idx = msg_bufno;
214 channel->wr_empty = 0;
215 channel->wr_sleepy = 0;
216 spin_unlock(&channel->wr_spinlock);
218 wake_up_interruptible(&channel->wr_wait);
223 if (msg_bufno >= channel->num_rd_buffers) {
224 malformed_message(&buf[i]);
228 spin_lock(&channel->rd_spinlock);
229 channel->rd_fpga_buf_idx = msg_bufno;
230 channel->rd_full = 0;
231 spin_unlock(&channel->rd_spinlock);
233 wake_up_interruptible(&channel->rd_wait);
234 if (!channel->rd_synchronous)
237 &channel->rd_workitem,
242 case XILLYMSG_OPCODE_NONEMPTY:
243 if ((msg_channel > ep->num_channels) ||
244 (msg_channel == 0) || (!msg_dir) ||
245 !ep->channels[msg_channel]->wr_supports_nonempty) {
246 malformed_message(&buf[i]);
250 channel = ep->channels[msg_channel];
252 if (msg_bufno >= channel->num_wr_buffers) {
253 malformed_message(&buf[i]);
256 spin_lock(&channel->wr_spinlock);
257 if (msg_bufno == channel->wr_host_buf_idx)
258 channel->wr_ready = 1;
259 spin_unlock(&channel->wr_spinlock);
261 wake_up_interruptible(&channel->wr_ready_wait);
264 case XILLYMSG_OPCODE_QUIESCEACK:
265 ep->idtlen = msg_data;
266 wake_up_interruptible(&ep->ep_wait);
269 case XILLYMSG_OPCODE_FIFOEOF:
270 channel = ep->channels[msg_channel];
271 spin_lock(&channel->wr_spinlock);
272 channel->wr_eof = msg_bufno;
273 channel->wr_sleepy = 0;
275 channel->wr_hangup = channel->wr_empty &&
276 (channel->wr_host_buf_idx == msg_bufno);
278 spin_unlock(&channel->wr_spinlock);
280 wake_up_interruptible(&channel->wr_wait);
283 case XILLYMSG_OPCODE_FATAL_ERROR:
285 wake_up_interruptible(&ep->ep_wait); /* For select() */
286 pr_err("xillybus: FPGA reported a fatal "
287 "error. This means that the low-level "
288 "communication with the device has failed. "
289 "This hardware problem is most likely "
290 "unrelated to xillybus (neither kernel "
291 "module nor FPGA core), but reports are "
292 "still welcome. All I/O is aborted.\n");
295 malformed_message(&buf[i]);
300 ep->ephw->hw_sync_sgl_for_device(ep,
305 ep->msg_counter = (ep->msg_counter + 1) & 0xf;
306 ep->failed_messages = 0;
307 iowrite32(0x03, &ep->registers[fpga_msg_ctrl_reg]); /* Message ACK */
311 EXPORT_SYMBOL(xillybus_isr);
314 * A few trivial memory management functions.
315 * NOTE: These functions are used only on probe and remove, and therefore
316 * no locks are applied!
319 void xillybus_do_cleanup(struct xilly_cleanup *mem,
320 struct xilly_endpoint *endpoint)
322 struct list_head *this, *next;
324 list_for_each_safe(this, next, &mem->to_unmap) {
325 struct xilly_dma *entry =
326 list_entry(this, struct xilly_dma, node);
328 endpoint->ephw->unmap_single(entry);
332 INIT_LIST_HEAD(&mem->to_unmap);
334 list_for_each_safe(this, next, &mem->to_kfree)
337 INIT_LIST_HEAD(&mem->to_kfree);
339 list_for_each_safe(this, next, &mem->to_pagefree) {
340 struct xilly_page *entry =
341 list_entry(this, struct xilly_page, node);
343 free_pages(entry->addr, entry->order);
346 INIT_LIST_HEAD(&mem->to_pagefree);
348 EXPORT_SYMBOL(xillybus_do_cleanup);
350 static void *xilly_malloc(struct xilly_cleanup *mem, size_t size)
354 ptr = kzalloc(sizeof(struct list_head) + size, GFP_KERNEL);
359 list_add_tail((struct list_head *) ptr, &mem->to_kfree);
361 return ptr + sizeof(struct list_head);
364 static unsigned long xilly_pagealloc(struct xilly_cleanup *mem,
368 struct xilly_page *this;
370 this = kmalloc(sizeof(struct xilly_page), GFP_KERNEL);
374 addr = __get_free_pages(GFP_KERNEL | __GFP_DMA32 | __GFP_ZERO, order);
384 list_add_tail(&this->node, &mem->to_pagefree);
390 static void xillybus_autoflush(struct work_struct *work);
392 static int xilly_setupchannels(struct xilly_endpoint *ep,
393 struct xilly_cleanup *mem,
394 unsigned char *chandesc,
398 int i, entry, wr_nbuffer, rd_nbuffer;
399 struct xilly_channel *channel;
400 int channelnum, bufnum, bufsize, format, is_writebuf;
402 int synchronous, allowpartial, exclusive_open, seekable;
403 int supports_nonempty;
404 void *wr_salami = NULL;
405 void *rd_salami = NULL;
406 int left_of_wr_salami = 0;
407 int left_of_rd_salami = 0;
409 int msg_buf_done = 0;
411 struct xilly_buffer *this_buffer = NULL; /* Init to silence warning */
413 channel = xilly_malloc(mem, ep->num_channels *
414 sizeof(struct xilly_channel));
419 ep->channels = xilly_malloc(mem, (ep->num_channels + 1) *
420 sizeof(struct xilly_channel *));
425 ep->channels[0] = NULL; /* Channel 0 is message buf. */
427 /* Initialize all channels with defaults */
429 for (i = 1; i <= ep->num_channels; i++) {
430 channel->wr_buffers = NULL;
431 channel->rd_buffers = NULL;
432 channel->num_wr_buffers = 0;
433 channel->num_rd_buffers = 0;
434 channel->wr_fpga_buf_idx = -1;
435 channel->wr_host_buf_idx = 0;
436 channel->wr_host_buf_pos = 0;
437 channel->wr_empty = 1;
438 channel->wr_ready = 0;
439 channel->wr_sleepy = 1;
440 channel->rd_fpga_buf_idx = 0;
441 channel->rd_host_buf_idx = 0;
442 channel->rd_host_buf_pos = 0;
443 channel->rd_full = 0;
444 channel->wr_ref_count = 0;
445 channel->rd_ref_count = 0;
447 spin_lock_init(&channel->wr_spinlock);
448 spin_lock_init(&channel->rd_spinlock);
449 mutex_init(&channel->wr_mutex);
450 mutex_init(&channel->rd_mutex);
451 init_waitqueue_head(&channel->rd_wait);
452 init_waitqueue_head(&channel->wr_wait);
453 init_waitqueue_head(&channel->wr_ready_wait);
455 INIT_DELAYED_WORK(&channel->rd_workitem, xillybus_autoflush);
457 channel->endpoint = ep;
458 channel->chan_num = i;
460 channel->log2_element_size = 0;
462 ep->channels[i] = channel++;
466 * The DMA buffer address update is atomic on the FPGA, so even if
467 * it was in the middle of sending messages to some buffer, changing
468 * the address is safe, since the data will go to either of the
469 * buffers. Not that this situation should occur at all anyhow.
473 rd_nbuffer = 1; /* Buffer zero isn't used at all */
475 for (entry = 0; entry < entries; entry++, chandesc += 4) {
476 is_writebuf = chandesc[0] & 0x01;
477 channelnum = (chandesc[0] >> 1) | ((chandesc[1] & 0x0f) << 7);
478 format = (chandesc[1] >> 4) & 0x03;
479 allowpartial = (chandesc[1] >> 6) & 0x01;
480 synchronous = (chandesc[1] >> 7) & 0x01;
481 bufsize = 1 << (chandesc[2] & 0x1f);
482 bufnum = 1 << (chandesc[3] & 0x0f);
483 exclusive_open = (chandesc[2] >> 7) & 0x01;
484 seekable = (chandesc[2] >> 6) & 0x01;
485 supports_nonempty = (chandesc[2] >> 5) & 0x01;
487 if ((channelnum > ep->num_channels) ||
488 ((channelnum == 0) && !is_writebuf)) {
489 pr_err("xillybus: IDT requests channel out "
490 "of range. Aborting.\n");
494 channel = ep->channels[channelnum]; /* NULL for msg channel */
496 bytebufsize = bufsize << 2; /* Overwritten just below */
499 channel->num_rd_buffers = bufnum;
500 channel->log2_element_size = ((format > 2) ?
502 bytebufsize = channel->rd_buf_size = bufsize *
503 (1 << channel->log2_element_size);
504 channel->rd_allow_partial = allowpartial;
505 channel->rd_synchronous = synchronous;
506 channel->rd_exclusive_open = exclusive_open;
507 channel->seekable = seekable;
509 channel->rd_buffers = xilly_malloc(
511 bufnum * sizeof(struct xilly_buffer *));
513 if (!channel->rd_buffers)
516 this_buffer = xilly_malloc(
518 bufnum * sizeof(struct xilly_buffer));
524 else if (channelnum > 0) {
525 channel->num_wr_buffers = bufnum;
526 channel->log2_element_size = ((format > 2) ?
528 bytebufsize = channel->wr_buf_size = bufsize *
529 (1 << channel->log2_element_size);
531 channel->seekable = seekable;
532 channel->wr_supports_nonempty = supports_nonempty;
534 channel->wr_allow_partial = allowpartial;
535 channel->wr_synchronous = synchronous;
536 channel->wr_exclusive_open = exclusive_open;
538 channel->wr_buffers = xilly_malloc(
540 bufnum * sizeof(struct xilly_buffer *));
542 if (!channel->wr_buffers)
545 this_buffer = xilly_malloc(
547 bufnum * sizeof(struct xilly_buffer));
554 * Although daunting, we cut the chunks for read buffers
555 * from a different salami than the write buffers',
556 * possibly improving performance.
560 for (i = 0; i < bufnum; i++) {
562 * Buffers are expected in descending
563 * byte-size order, so there is either
564 * enough for this buffer or none at all.
566 if ((left_of_wr_salami < bytebufsize) &&
567 (left_of_wr_salami > 0)) {
569 "Corrupt buffer allocation "
570 "in IDT. Aborting.\n");
574 if (left_of_wr_salami == 0) {
575 int allocorder, allocsize;
577 allocsize = PAGE_SIZE;
579 while (bytebufsize > allocsize) {
589 left_of_wr_salami = allocsize;
592 dma_addr = ep->ephw->map_single(
603 (u32) (dma_addr & 0xffffffff),
605 fpga_dma_bufaddr_lowaddr_reg]
608 ((u32) ((((u64) dma_addr) >> 32)
611 fpga_dma_bufaddr_highaddr_reg]
615 if (channelnum > 0) {
616 this_buffer->addr = wr_salami;
617 this_buffer->dma_addr = dma_addr;
618 channel->wr_buffers[i] = this_buffer++;
621 0x80000000 | wr_nbuffer++,
623 fpga_dma_bufno_reg]);
625 ep->msgbuf_addr = wr_salami;
626 ep->msgbuf_dma_addr = dma_addr;
627 ep->msg_buf_size = bytebufsize;
631 0x80000000, &ep->registers[
632 fpga_dma_bufno_reg]);
635 left_of_wr_salami -= bytebufsize;
636 wr_salami += bytebufsize;
638 else /* Read buffers */
639 for (i = 0; i < bufnum; i++) {
641 * Buffers are expected in descending
642 * byte-size order, so there is either
643 * enough for this buffer or none at all.
645 if ((left_of_rd_salami < bytebufsize) &&
646 (left_of_rd_salami > 0)) {
648 "Corrupt buffer allocation "
649 "in IDT. Aborting.\n");
653 if (left_of_rd_salami == 0) {
654 int allocorder, allocsize;
656 allocsize = PAGE_SIZE;
658 while (bytebufsize > allocsize) {
670 left_of_rd_salami = allocsize;
673 dma_addr = ep->ephw->map_single(
684 (u32) (dma_addr & 0xffffffff),
686 fpga_dma_bufaddr_lowaddr_reg]
689 ((u32) ((((u64) dma_addr) >> 32)
692 fpga_dma_bufaddr_highaddr_reg]
696 this_buffer->addr = rd_salami;
697 this_buffer->dma_addr = dma_addr;
698 channel->rd_buffers[i] = this_buffer++;
700 iowrite32(rd_nbuffer++,
701 &ep->registers[fpga_dma_bufno_reg]);
703 left_of_rd_salami -= bytebufsize;
704 rd_salami += bytebufsize;
709 pr_err("xillybus: Corrupt IDT: No message buffer. "
717 pr_err("xillybus: Failed to allocate write buffer memory. "
721 pr_err("xillybus: Failed to map DMA memory!. Aborting.\n");
725 static void xilly_scan_idt(struct xilly_endpoint *endpoint,
726 struct xilly_idt_handle *idt_handle)
729 unsigned char *idt = endpoint->channels[1]->wr_buffers[0]->addr;
730 unsigned char *end_of_idt = idt + endpoint->idtlen - 4;
735 idt_handle->idt = idt;
737 scan++; /* Skip version number */
739 while ((scan <= end_of_idt) && *scan) {
740 while ((scan <= end_of_idt) && *scan++)
741 /* Do nothing, just scan thru string */;
747 if (scan > end_of_idt) {
748 pr_err("xillybus: IDT device name list overflow. "
750 idt_handle->chandesc = NULL;
753 idt_handle->chandesc = scan;
755 len = endpoint->idtlen - (3 + ((int) (scan - idt)));
758 idt_handle->chandesc = NULL;
760 pr_err("xillybus: Corrupt IDT device name list. "
764 idt_handle->entries = len >> 2;
766 endpoint->num_channels = count;
769 static int xilly_obtain_idt(struct xilly_endpoint *endpoint)
772 struct xilly_channel *channel;
773 unsigned char *version;
775 channel = endpoint->channels[1]; /* This should be generated ad-hoc */
777 channel->wr_sleepy = 1;
778 wmb(); /* Setting wr_sleepy must come before the command */
781 (3 << 24), /* Opcode 3 for channel 0 = Send IDT */
782 &endpoint->registers[fpga_buf_ctrl_reg]);
783 mmiowb(); /* Just to appear safe */
785 wait_event_interruptible_timeout(channel->wr_wait,
786 (!channel->wr_sleepy),
789 if (channel->wr_sleepy) {
790 pr_err("xillybus: Failed to obtain IDT. Aborting.\n");
792 if (endpoint->fatal_error)
799 endpoint->ephw->hw_sync_sgl_for_cpu(
801 channel->wr_buffers[0]->dma_addr,
802 channel->wr_buf_size,
805 if (channel->wr_buffers[0]->end_offset != endpoint->idtlen) {
806 pr_err("xillybus: IDT length mismatch (%d != %d). "
808 channel->wr_buffers[0]->end_offset, endpoint->idtlen);
813 if (crc32_le(~0, channel->wr_buffers[0]->addr,
814 endpoint->idtlen+1) != 0) {
815 pr_err("xillybus: IDT failed CRC check. Aborting.\n");
820 version = channel->wr_buffers[0]->addr;
822 /* Check version number. Accept anything below 0x82 for now. */
823 if (*version > 0x82) {
824 pr_err("xillybus: No support for IDT version 0x%02x. "
825 "Maybe the xillybus driver needs an upgarde. "
832 return 0; /* Success */
835 static ssize_t xillybus_read(struct file *filp, char __user *userbuf,
836 size_t count, loff_t *f_pos)
841 int no_time_left = 0;
842 long deadline, left_to_sleep;
843 struct xilly_channel *channel = filp->private_data;
845 int empty, reached_eof, exhausted, ready;
846 /* Initializations are there only to silence warnings */
848 int howmany = 0, bufpos = 0, bufidx = 0, bufferdone = 0;
851 if (channel->endpoint->fatal_error)
854 deadline = jiffies + 1 + XILLY_RX_TIMEOUT;
856 rc = mutex_lock_interruptible(&channel->wr_mutex);
861 rc = 0; /* Just to be clear about it. Compiler optimizes this out */
863 while (1) { /* Note that we may drop mutex within this loop */
864 int bytes_to_do = count - bytes_done;
865 spin_lock_irqsave(&channel->wr_spinlock, flags);
867 empty = channel->wr_empty;
868 ready = !empty || channel->wr_ready;
871 bufidx = channel->wr_host_buf_idx;
872 bufpos = channel->wr_host_buf_pos;
873 howmany = ((channel->wr_buffers[bufidx]->end_offset
874 + 1) << channel->log2_element_size)
877 /* Update wr_host_* to its post-operation state */
878 if (howmany > bytes_to_do) {
881 howmany = bytes_to_do;
882 channel->wr_host_buf_pos += howmany;
886 channel->wr_host_buf_pos = 0;
888 if (bufidx == channel->wr_fpga_buf_idx) {
889 channel->wr_empty = 1;
890 channel->wr_sleepy = 1;
891 channel->wr_ready = 0;
894 if (bufidx >= (channel->num_wr_buffers - 1))
895 channel->wr_host_buf_idx = 0;
897 channel->wr_host_buf_idx++;
902 * Marking our situation after the possible changes above,
903 * for use after releasing the spinlock.
905 * empty = empty before change
906 * exhasted = empty after possible change
909 reached_eof = channel->wr_empty &&
910 (channel->wr_host_buf_idx == channel->wr_eof);
911 channel->wr_hangup = reached_eof;
912 exhausted = channel->wr_empty;
913 waiting_bufidx = channel->wr_host_buf_idx;
915 spin_unlock_irqrestore(&channel->wr_spinlock, flags);
917 if (!empty) { /* Go on, now without the spinlock */
919 if (bufpos == 0) /* Position zero means it's virgin */
920 channel->endpoint->ephw->hw_sync_sgl_for_cpu(
922 channel->wr_buffers[bufidx]->dma_addr,
923 channel->wr_buf_size,
928 channel->wr_buffers[bufidx]->addr
933 bytes_done += howmany;
936 channel->endpoint->ephw->
937 hw_sync_sgl_for_device
940 channel->wr_buffers[bufidx]->
942 channel->wr_buf_size,
946 * Tell FPGA the buffer is done with. It's an
947 * atomic operation to the FPGA, so what
948 * happens with other channels doesn't matter,
949 * and the certain channel is protected with
950 * the channel-specific mutex.
953 iowrite32(1 | (channel->chan_num << 1)
955 &channel->endpoint->registers[
957 mmiowb(); /* Just to appear safe */
961 mutex_unlock(&channel->wr_mutex);
966 /* This includes a zero-count return = EOF */
967 if ((bytes_done >= count) || reached_eof)
971 continue; /* More in RAM buffer(s)? Just go on. */
973 if ((bytes_done > 0) &&
975 (channel->wr_synchronous && channel->wr_allow_partial)))
979 * Nonblocking read: The "ready" flag tells us that the FPGA
980 * has data to send. In non-blocking mode, if it isn't on,
981 * just return. But if there is, we jump directly to the point
982 * where we ask for the FPGA to send all it has, and wait
983 * until that data arrives. So in a sense, we *do* block in
984 * nonblocking mode, but only for a very short time.
987 if (!no_time_left && (filp->f_flags & O_NONBLOCK)) {
994 bytes_done = -EAGAIN;
998 if (!no_time_left || (bytes_done > 0)) {
1000 * Note that in case of an element-misaligned read
1001 * request, offsetlimit will include the last element,
1002 * which will be partially read from.
1004 int offsetlimit = ((count - bytes_done) - 1) >>
1005 channel->log2_element_size;
1006 int buf_elements = channel->wr_buf_size >>
1007 channel->log2_element_size;
1010 * In synchronous mode, always send an offset limit.
1011 * Just don't send a value too big.
1014 if (channel->wr_synchronous) {
1015 /* Don't request more than one buffer */
1016 if (channel->wr_allow_partial &&
1017 (offsetlimit >= buf_elements))
1018 offsetlimit = buf_elements - 1;
1020 /* Don't request more than all buffers */
1021 if (!channel->wr_allow_partial &&
1023 (buf_elements * channel->num_wr_buffers)))
1024 offsetlimit = buf_elements *
1025 channel->num_wr_buffers - 1;
1029 * In asynchronous mode, force early flush of a buffer
1030 * only if that will allow returning a full count. The
1031 * "offsetlimit < ( ... )" rather than "<=" excludes
1032 * requesting a full buffer, which would obviously
1033 * cause a buffer transmission anyhow
1036 if (channel->wr_synchronous ||
1037 (offsetlimit < (buf_elements - 1))) {
1039 mutex_lock(&channel->endpoint->register_mutex);
1041 iowrite32(offsetlimit,
1042 &channel->endpoint->registers[
1043 fpga_buf_offset_reg]);
1046 iowrite32(1 | (channel->chan_num << 1) |
1047 (2 << 24) | /* 2 = offset limit */
1048 (waiting_bufidx << 12),
1049 &channel->endpoint->registers[
1050 fpga_buf_ctrl_reg]);
1052 mmiowb(); /* Just to appear safe */
1054 mutex_unlock(&channel->endpoint->
1061 * If partial completion is disallowed, there is no point in
1062 * timeout sleeping. Neither if no_time_left is set and
1066 if (!channel->wr_allow_partial ||
1067 (no_time_left && (bytes_done == 0))) {
1070 * This do-loop will run more than once if another
1071 * thread reasserted wr_sleepy before we got the mutex
1072 * back, so we try again.
1076 mutex_unlock(&channel->wr_mutex);
1078 if (wait_event_interruptible(
1080 (!channel->wr_sleepy)))
1083 if (mutex_lock_interruptible(
1084 &channel->wr_mutex))
1086 } while (channel->wr_sleepy);
1090 interrupted: /* Mutex is not held if got here */
1091 if (channel->endpoint->fatal_error)
1095 if (filp->f_flags & O_NONBLOCK)
1096 return -EAGAIN; /* Don't admit snoozing */
1100 left_to_sleep = deadline - ((long) jiffies);
1103 * If our time is out, skip the waiting. We may miss wr_sleepy
1104 * being deasserted but hey, almost missing the train is like
1108 if (left_to_sleep > 0) {
1110 wait_event_interruptible_timeout(
1112 (!channel->wr_sleepy),
1115 if (!channel->wr_sleepy)
1118 if (left_to_sleep < 0) { /* Interrupt */
1119 mutex_unlock(&channel->wr_mutex);
1120 if (channel->endpoint->fatal_error)
1129 no_time_left = 1; /* We're out of sleeping time. Desperate! */
1131 if (bytes_done == 0) {
1133 * Reaching here means that we allow partial return,
1134 * that we've run out of time, and that we have
1135 * nothing to return.
1136 * So tell the FPGA to send anything it has or gets.
1139 iowrite32(1 | (channel->chan_num << 1) |
1140 (3 << 24) | /* Opcode 3, flush it all! */
1141 (waiting_bufidx << 12),
1142 &channel->endpoint->registers[
1143 fpga_buf_ctrl_reg]);
1144 mmiowb(); /* Just to appear safe */
1148 * Formally speaking, we should block for data at this point.
1149 * But to keep the code cleaner, we'll just finish the loop,
1150 * make the unlikely check for data, and then block at the
1155 mutex_unlock(&channel->wr_mutex);
1157 if (channel->endpoint->fatal_error)
1164 * The timeout argument takes values as follows:
1165 * >0 : Flush with timeout
1166 * ==0 : Flush, and wait idefinitely for the flush to complete
1167 * <0 : Autoflush: Flush only if there's a single buffer occupied
1170 static int xillybus_myflush(struct xilly_channel *channel, long timeout)
1173 unsigned long flags;
1175 int end_offset_plus1;
1176 int bufidx, bufidx_minus1;
1179 int new_rd_host_buf_pos;
1181 if (channel->endpoint->fatal_error)
1183 rc = mutex_lock_interruptible(&channel->rd_mutex);
1189 * Don't flush a closed channel. This can happen when the work queued
1190 * autoflush thread fires off after the file has closed. This is not
1191 * an error, just something to dismiss.
1194 if (!channel->rd_ref_count)
1197 bufidx = channel->rd_host_buf_idx;
1199 bufidx_minus1 = (bufidx == 0) ? channel->num_rd_buffers - 1 : bufidx-1;
1201 end_offset_plus1 = channel->rd_host_buf_pos >>
1202 channel->log2_element_size;
1204 new_rd_host_buf_pos = channel->rd_host_buf_pos -
1205 (end_offset_plus1 << channel->log2_element_size);
1207 /* Submit the current buffer if it's nonempty */
1208 if (end_offset_plus1) {
1209 unsigned char *tail = channel->rd_buffers[bufidx]->addr +
1210 (end_offset_plus1 << channel->log2_element_size);
1212 /* Copy unflushed data, so we can put it in next buffer */
1213 for (i = 0; i < new_rd_host_buf_pos; i++)
1214 channel->rd_leftovers[i] = *tail++;
1216 spin_lock_irqsave(&channel->rd_spinlock, flags);
1218 /* Autoflush only if a single buffer is occupied */
1220 if ((timeout < 0) &&
1221 (channel->rd_full ||
1222 (bufidx_minus1 != channel->rd_fpga_buf_idx))) {
1223 spin_unlock_irqrestore(&channel->rd_spinlock, flags);
1225 * A new work item may be queued by the ISR exactly
1226 * now, since the execution of a work item allows the
1227 * queuing of a new one while it's running.
1232 /* The 4th element is never needed for data, so it's a flag */
1233 channel->rd_leftovers[3] = (new_rd_host_buf_pos != 0);
1235 /* Set up rd_full to reflect a certain moment's state */
1237 if (bufidx == channel->rd_fpga_buf_idx)
1238 channel->rd_full = 1;
1239 spin_unlock_irqrestore(&channel->rd_spinlock, flags);
1241 if (bufidx >= (channel->num_rd_buffers - 1))
1242 channel->rd_host_buf_idx = 0;
1244 channel->rd_host_buf_idx++;
1246 channel->endpoint->ephw->hw_sync_sgl_for_device(
1248 channel->rd_buffers[bufidx]->dma_addr,
1249 channel->rd_buf_size,
1252 mutex_lock(&channel->endpoint->register_mutex);
1254 iowrite32(end_offset_plus1 - 1,
1255 &channel->endpoint->registers[fpga_buf_offset_reg]);
1258 iowrite32((channel->chan_num << 1) | /* Channel ID */
1259 (2 << 24) | /* Opcode 2, submit buffer */
1261 &channel->endpoint->registers[fpga_buf_ctrl_reg]);
1262 mmiowb(); /* Just to appear safe */
1264 mutex_unlock(&channel->endpoint->register_mutex);
1265 } else if (bufidx == 0)
1266 bufidx = channel->num_rd_buffers - 1;
1270 channel->rd_host_buf_pos = new_rd_host_buf_pos;
1273 goto done; /* Autoflush */
1277 * bufidx is now the last buffer written to (or equal to
1278 * rd_fpga_buf_idx if buffer was never written to), and
1279 * channel->rd_host_buf_idx the one after it.
1281 * If bufidx == channel->rd_fpga_buf_idx we're either empty or full.
1286 while (1) { /* Loop waiting for draining of buffers */
1287 spin_lock_irqsave(&channel->rd_spinlock, flags);
1289 if (bufidx != channel->rd_fpga_buf_idx)
1290 channel->rd_full = 1; /*
1292 * but needs waiting.
1295 empty = !channel->rd_full;
1297 spin_unlock_irqrestore(&channel->rd_spinlock, flags);
1303 * Indefinite sleep with mutex taken. With data waiting for
1304 * flushing user should not be surprised if open() for write
1308 wait_event_interruptible(channel->rd_wait,
1309 (!channel->rd_full));
1311 else if (wait_event_interruptible_timeout(
1313 (!channel->rd_full),
1315 pr_warn("xillybus: "
1316 "Timed out while flushing. "
1317 "Output data may be lost.\n");
1323 if (channel->rd_full) {
1330 mutex_unlock(&channel->rd_mutex);
1332 if (channel->endpoint->fatal_error)
1338 static int xillybus_flush(struct file *filp, fl_owner_t id)
1340 if (!(filp->f_mode & FMODE_WRITE))
1343 return xillybus_myflush(filp->private_data, HZ); /* 1 second timeout */
1346 static void xillybus_autoflush(struct work_struct *work)
1348 struct delayed_work *workitem = container_of(
1349 work, struct delayed_work, work);
1350 struct xilly_channel *channel = container_of(
1351 workitem, struct xilly_channel, rd_workitem);
1354 rc = xillybus_myflush(channel, -1);
1357 pr_warn("xillybus: Autoflush failed because "
1358 "work queue thread got a signal.\n");
1360 pr_err("xillybus: Autoflush failed under "
1361 "weird circumstances.\n");
1365 static ssize_t xillybus_write(struct file *filp, const char __user *userbuf,
1366 size_t count, loff_t *f_pos)
1369 unsigned long flags;
1371 struct xilly_channel *channel = filp->private_data;
1373 int full, exhausted;
1374 /* Initializations are there only to silence warnings */
1376 int howmany = 0, bufpos = 0, bufidx = 0, bufferdone = 0;
1377 int end_offset_plus1 = 0;
1379 if (channel->endpoint->fatal_error)
1382 rc = mutex_lock_interruptible(&channel->rd_mutex);
1387 rc = 0; /* Just to be clear about it. Compiler optimizes this out */
1390 int bytes_to_do = count - bytes_done;
1392 spin_lock_irqsave(&channel->rd_spinlock, flags);
1394 full = channel->rd_full;
1397 bufidx = channel->rd_host_buf_idx;
1398 bufpos = channel->rd_host_buf_pos;
1399 howmany = channel->rd_buf_size - bufpos;
1402 * Update rd_host_* to its state after this operation.
1403 * count=0 means committing the buffer immediately,
1404 * which is like flushing, but not necessarily block.
1407 if ((howmany > bytes_to_do) &&
1409 ((bufpos >> channel->log2_element_size) == 0))) {
1412 howmany = bytes_to_do;
1413 channel->rd_host_buf_pos += howmany;
1419 channel->rd_buf_size >>
1420 channel->log2_element_size;
1421 channel->rd_host_buf_pos = 0;
1423 unsigned char *tail;
1426 end_offset_plus1 = bufpos >>
1427 channel->log2_element_size;
1429 channel->rd_host_buf_pos -=
1431 channel->log2_element_size;
1434 rd_buffers[bufidx]->addr +
1435 (end_offset_plus1 <<
1436 channel->log2_element_size);
1439 i < channel->rd_host_buf_pos;
1441 channel->rd_leftovers[i] =
1445 if (bufidx == channel->rd_fpga_buf_idx)
1446 channel->rd_full = 1;
1448 if (bufidx >= (channel->num_rd_buffers - 1))
1449 channel->rd_host_buf_idx = 0;
1451 channel->rd_host_buf_idx++;
1456 * Marking our situation after the possible changes above,
1457 * for use after releasing the spinlock.
1459 * full = full before change
1460 * exhasted = full after possible change
1463 exhausted = channel->rd_full;
1465 spin_unlock_irqrestore(&channel->rd_spinlock, flags);
1467 if (!full) { /* Go on, now without the spinlock */
1468 unsigned char *head =
1469 channel->rd_buffers[bufidx]->addr;
1472 if ((bufpos == 0) || /* Zero means it's virgin */
1473 (channel->rd_leftovers[3] != 0)) {
1474 channel->endpoint->ephw->hw_sync_sgl_for_cpu(
1476 channel->rd_buffers[bufidx]->dma_addr,
1477 channel->rd_buf_size,
1480 /* Virgin, but leftovers are due */
1481 for (i = 0; i < bufpos; i++)
1482 *head++ = channel->rd_leftovers[i];
1484 channel->rd_leftovers[3] = 0; /* Clear flag */
1488 channel->rd_buffers[bufidx]->addr + bufpos,
1493 bytes_done += howmany;
1496 channel->endpoint->ephw->
1497 hw_sync_sgl_for_device(
1499 channel->rd_buffers[bufidx]->
1501 channel->rd_buf_size,
1504 mutex_lock(&channel->endpoint->register_mutex);
1506 iowrite32(end_offset_plus1 - 1,
1507 &channel->endpoint->registers[
1508 fpga_buf_offset_reg]);
1510 iowrite32((channel->chan_num << 1) |
1511 (2 << 24) | /* 2 = submit buffer */
1513 &channel->endpoint->registers[
1514 fpga_buf_ctrl_reg]);
1515 mmiowb(); /* Just to appear safe */
1517 mutex_unlock(&channel->endpoint->
1520 channel->rd_leftovers[3] =
1521 (channel->rd_host_buf_pos != 0);
1525 mutex_unlock(&channel->rd_mutex);
1527 if (channel->endpoint->fatal_error)
1530 if (!channel->rd_synchronous)
1533 &channel->rd_workitem,
1540 if (bytes_done >= count)
1544 continue; /* If there's more space, just go on */
1546 if ((bytes_done > 0) && channel->rd_allow_partial)
1550 * Indefinite sleep with mutex taken. With data waiting for
1551 * flushing, user should not be surprised if open() for write
1555 if (filp->f_flags & O_NONBLOCK) {
1556 bytes_done = -EAGAIN;
1560 wait_event_interruptible(channel->rd_wait,
1561 (!channel->rd_full));
1563 if (channel->rd_full) {
1564 mutex_unlock(&channel->rd_mutex);
1566 if (channel->endpoint->fatal_error)
1575 mutex_unlock(&channel->rd_mutex);
1577 if (!channel->rd_synchronous)
1578 queue_delayed_work(xillybus_wq,
1579 &channel->rd_workitem,
1582 if ((channel->rd_synchronous) && (bytes_done > 0)) {
1583 rc = xillybus_myflush(filp->private_data, 0); /* No timeout */
1585 if (rc && (rc != -EINTR))
1589 if (channel->endpoint->fatal_error)
1595 static int xillybus_open(struct inode *inode, struct file *filp)
1598 unsigned long flags;
1599 int minor = iminor(inode);
1600 int major = imajor(inode);
1601 struct xilly_endpoint *ep_iter, *endpoint = NULL;
1602 struct xilly_channel *channel;
1604 mutex_lock(&ep_list_lock);
1606 list_for_each_entry(ep_iter, &list_of_endpoints, ep_list) {
1607 if ((ep_iter->major == major) &&
1608 (minor >= ep_iter->lowest_minor) &&
1609 (minor < (ep_iter->lowest_minor +
1610 ep_iter->num_channels))) {
1615 mutex_unlock(&ep_list_lock);
1618 pr_err("xillybus: open() failed to find a device "
1619 "for major=%d and minor=%d\n", major, minor);
1623 if (endpoint->fatal_error)
1626 channel = endpoint->channels[1 + minor - endpoint->lowest_minor];
1627 filp->private_data = channel;
1631 * It gets complicated because:
1632 * 1. We don't want to take a mutex we don't have to
1633 * 2. We don't want to open one direction if the other will fail.
1636 if ((filp->f_mode & FMODE_READ) && (!channel->num_wr_buffers))
1639 if ((filp->f_mode & FMODE_WRITE) && (!channel->num_rd_buffers))
1642 if ((filp->f_mode & FMODE_READ) && (filp->f_flags & O_NONBLOCK) &&
1643 (channel->wr_synchronous || !channel->wr_allow_partial ||
1644 !channel->wr_supports_nonempty)) {
1645 pr_err("xillybus: open() failed: "
1646 "O_NONBLOCK not allowed for read on this device\n");
1650 if ((filp->f_mode & FMODE_WRITE) && (filp->f_flags & O_NONBLOCK) &&
1651 (channel->rd_synchronous || !channel->rd_allow_partial)) {
1652 pr_err("xillybus: open() failed: "
1653 "O_NONBLOCK not allowed for write on this device\n");
1658 * Note: open() may block on getting mutexes despite O_NONBLOCK.
1659 * This shouldn't occur normally, since multiple open of the same
1660 * file descriptor is almost always prohibited anyhow
1661 * (*_exclusive_open is normally set in real-life systems).
1664 if (filp->f_mode & FMODE_READ) {
1665 rc = mutex_lock_interruptible(&channel->wr_mutex);
1670 if (filp->f_mode & FMODE_WRITE) {
1671 rc = mutex_lock_interruptible(&channel->rd_mutex);
1676 if ((filp->f_mode & FMODE_READ) &&
1677 (channel->wr_ref_count != 0) &&
1678 (channel->wr_exclusive_open)) {
1683 if ((filp->f_mode & FMODE_WRITE) &&
1684 (channel->rd_ref_count != 0) &&
1685 (channel->rd_exclusive_open)) {
1691 if (filp->f_mode & FMODE_READ) {
1692 if (channel->wr_ref_count == 0) { /* First open of file */
1693 /* Move the host to first buffer */
1694 spin_lock_irqsave(&channel->wr_spinlock, flags);
1695 channel->wr_host_buf_idx = 0;
1696 channel->wr_host_buf_pos = 0;
1697 channel->wr_fpga_buf_idx = -1;
1698 channel->wr_empty = 1;
1699 channel->wr_ready = 0;
1700 channel->wr_sleepy = 1;
1701 channel->wr_eof = -1;
1702 channel->wr_hangup = 0;
1704 spin_unlock_irqrestore(&channel->wr_spinlock, flags);
1706 iowrite32(1 | (channel->chan_num << 1) |
1707 (4 << 24) | /* Opcode 4, open channel */
1708 ((channel->wr_synchronous & 1) << 23),
1709 &channel->endpoint->registers[
1710 fpga_buf_ctrl_reg]);
1711 mmiowb(); /* Just to appear safe */
1714 channel->wr_ref_count++;
1717 if (filp->f_mode & FMODE_WRITE) {
1718 if (channel->rd_ref_count == 0) { /* First open of file */
1719 /* Move the host to first buffer */
1720 spin_lock_irqsave(&channel->rd_spinlock, flags);
1721 channel->rd_host_buf_idx = 0;
1722 channel->rd_host_buf_pos = 0;
1723 channel->rd_leftovers[3] = 0; /* No leftovers. */
1724 channel->rd_fpga_buf_idx = channel->num_rd_buffers - 1;
1725 channel->rd_full = 0;
1727 spin_unlock_irqrestore(&channel->rd_spinlock, flags);
1729 iowrite32((channel->chan_num << 1) |
1730 (4 << 24), /* Opcode 4, open channel */
1731 &channel->endpoint->registers[
1732 fpga_buf_ctrl_reg]);
1733 mmiowb(); /* Just to appear safe */
1736 channel->rd_ref_count++;
1740 if (filp->f_mode & FMODE_WRITE)
1741 mutex_unlock(&channel->rd_mutex);
1743 if (filp->f_mode & FMODE_READ)
1744 mutex_unlock(&channel->wr_mutex);
1746 if (!rc && (!channel->seekable))
1747 return nonseekable_open(inode, filp);
1752 static int xillybus_release(struct inode *inode, struct file *filp)
1755 unsigned long flags;
1756 struct xilly_channel *channel = filp->private_data;
1761 if (channel->endpoint->fatal_error)
1764 if (filp->f_mode & FMODE_WRITE) {
1765 rc = mutex_lock_interruptible(&channel->rd_mutex);
1768 pr_warn("xillybus: Failed to close file. "
1769 "Hardware left in messy state.\n");
1773 channel->rd_ref_count--;
1775 if (channel->rd_ref_count == 0) {
1778 * We rely on the kernel calling flush()
1779 * before we get here.
1782 iowrite32((channel->chan_num << 1) | /* Channel ID */
1783 (5 << 24), /* Opcode 5, close channel */
1784 &channel->endpoint->registers[
1785 fpga_buf_ctrl_reg]);
1786 mmiowb(); /* Just to appear safe */
1788 mutex_unlock(&channel->rd_mutex);
1791 if (filp->f_mode & FMODE_READ) {
1792 rc = mutex_lock_interruptible(&channel->wr_mutex);
1794 pr_warn("xillybus: Failed to close file. "
1795 "Hardware left in messy state.\n");
1799 channel->wr_ref_count--;
1801 if (channel->wr_ref_count == 0) {
1803 iowrite32(1 | (channel->chan_num << 1) |
1804 (5 << 24), /* Opcode 5, close channel */
1805 &channel->endpoint->registers[
1806 fpga_buf_ctrl_reg]);
1807 mmiowb(); /* Just to appear safe */
1810 * This is crazily cautious: We make sure that not
1811 * only that we got an EOF (be it because we closed
1812 * the channel or because of a user's EOF), but verify
1813 * that it's one beyond the last buffer arrived, so
1814 * we have no leftover buffers pending before wrapping
1815 * up (which can only happen in asynchronous channels,
1820 spin_lock_irqsave(&channel->wr_spinlock,
1822 buf_idx = channel->wr_fpga_buf_idx;
1823 eof = channel->wr_eof;
1824 channel->wr_sleepy = 1;
1825 spin_unlock_irqrestore(&channel->wr_spinlock,
1829 * Check if eof points at the buffer after
1830 * the last one the FPGA submitted. Note that
1831 * no EOF is marked by negative eof.
1835 if (buf_idx == channel->num_wr_buffers)
1842 * Steal extra 100 ms if awaken by interrupt.
1843 * This is a simple workaround for an
1844 * interrupt pending when entering, which would
1845 * otherwise result in declaring the hardware
1849 if (wait_event_interruptible(
1851 (!channel->wr_sleepy)))
1854 if (channel->wr_sleepy) {
1855 mutex_unlock(&channel->wr_mutex);
1856 pr_warn("xillybus: Hardware failed to "
1857 "respond to close command, "
1858 "therefore left in "
1865 mutex_unlock(&channel->wr_mutex);
1870 static loff_t xillybus_llseek(struct file *filp, loff_t offset, int whence)
1872 struct xilly_channel *channel = filp->private_data;
1873 loff_t pos = filp->f_pos;
1877 * Take both mutexes not allowing interrupts, since it seems like
1878 * common applications don't expect an -EINTR here. Besides, multiple
1879 * access to a single file descriptor on seekable devices is a mess
1883 if (channel->endpoint->fatal_error)
1886 mutex_lock(&channel->wr_mutex);
1887 mutex_lock(&channel->rd_mutex);
1897 pos = offset; /* Going to the end => to the beginning */
1904 /* In any case, we must finish on an element boundary */
1905 if (pos & ((1 << channel->log2_element_size) - 1)) {
1910 mutex_lock(&channel->endpoint->register_mutex);
1912 iowrite32(pos >> channel->log2_element_size,
1913 &channel->endpoint->registers[fpga_buf_offset_reg]);
1915 iowrite32((channel->chan_num << 1) |
1916 (6 << 24), /* Opcode 6, set address */
1917 &channel->endpoint->registers[fpga_buf_ctrl_reg]);
1918 mmiowb(); /* Just to appear safe */
1920 mutex_unlock(&channel->endpoint->register_mutex);
1923 mutex_unlock(&channel->rd_mutex);
1924 mutex_unlock(&channel->wr_mutex);
1926 if (rc) /* Return error after releasing mutexes */
1932 * Since seekable devices are allowed only when the channel is
1933 * synchronous, we assume that there is no data pending in either
1934 * direction (which holds true as long as no concurrent access on the
1935 * file descriptor takes place).
1936 * The only thing we may need to throw away is leftovers from partial
1940 channel->rd_leftovers[3] = 0;
1945 static unsigned int xillybus_poll(struct file *filp, poll_table *wait)
1947 struct xilly_channel *channel = filp->private_data;
1948 unsigned int mask = 0;
1949 unsigned long flags;
1951 poll_wait(filp, &channel->endpoint->ep_wait, wait);
1954 * poll() won't play ball regarding read() channels which
1955 * aren't asynchronous and support the nonempty message. Allowing
1956 * that will create situations where data has been delivered at
1957 * the FPGA, and users expecting select() to wake up, which it may
1961 if (!channel->wr_synchronous && channel->wr_supports_nonempty) {
1962 poll_wait(filp, &channel->wr_wait, wait);
1963 poll_wait(filp, &channel->wr_ready_wait, wait);
1965 spin_lock_irqsave(&channel->wr_spinlock, flags);
1966 if (!channel->wr_empty || channel->wr_ready)
1967 mask |= POLLIN | POLLRDNORM;
1969 if (channel->wr_hangup)
1971 * Not POLLHUP, because its behavior is in the
1972 * mist, and POLLIN does what we want: Wake up
1973 * the read file descriptor so it sees EOF.
1975 mask |= POLLIN | POLLRDNORM;
1976 spin_unlock_irqrestore(&channel->wr_spinlock, flags);
1980 * If partial data write is disallowed on a write() channel,
1981 * it's pointless to ever signal OK to write, because is could
1982 * block despite some space being available.
1985 if (channel->rd_allow_partial) {
1986 poll_wait(filp, &channel->rd_wait, wait);
1988 spin_lock_irqsave(&channel->rd_spinlock, flags);
1989 if (!channel->rd_full)
1990 mask |= POLLOUT | POLLWRNORM;
1991 spin_unlock_irqrestore(&channel->rd_spinlock, flags);
1994 if (channel->endpoint->fatal_error)
2000 static const struct file_operations xillybus_fops = {
2001 .owner = THIS_MODULE,
2002 .read = xillybus_read,
2003 .write = xillybus_write,
2004 .open = xillybus_open,
2005 .flush = xillybus_flush,
2006 .release = xillybus_release,
2007 .llseek = xillybus_llseek,
2008 .poll = xillybus_poll,
2011 static int xillybus_init_chrdev(struct xilly_endpoint *endpoint,
2012 const unsigned char *idt)
2016 int devnum, i, minor, major;
2018 struct device *device;
2020 rc = alloc_chrdev_region(&dev, 0, /* minor start */
2021 endpoint->num_channels,
2025 pr_warn("xillybus: Failed to obtain major/minors");
2029 endpoint->major = major = MAJOR(dev);
2030 endpoint->lowest_minor = minor = MINOR(dev);
2032 cdev_init(&endpoint->cdev, &xillybus_fops);
2033 endpoint->cdev.owner = endpoint->ephw->owner;
2034 rc = cdev_add(&endpoint->cdev, MKDEV(major, minor),
2035 endpoint->num_channels);
2037 pr_warn("xillybus: Failed to add cdev. Aborting.\n");
2043 for (i = minor, devnum = 0;
2044 devnum < endpoint->num_channels;
2046 snprintf(devname, sizeof(devname)-1, "xillybus_%s", idt);
2048 devname[sizeof(devname)-1] = 0; /* Should never matter */
2053 device = device_create(xillybus_class,
2059 if (IS_ERR(device)) {
2060 pr_warn("xillybus: Failed to create %s "
2061 "device. Aborting.\n", devname);
2066 pr_info("xillybus: Created %d device files.\n",
2067 endpoint->num_channels);
2068 return 0; /* succeed */
2072 for (; devnum >= 0; devnum--, i--)
2073 device_destroy(xillybus_class, MKDEV(major, i));
2075 cdev_del(&endpoint->cdev);
2077 unregister_chrdev_region(MKDEV(major, minor), endpoint->num_channels);
2083 static void xillybus_cleanup_chrdev(struct xilly_endpoint *endpoint)
2087 for (minor = endpoint->lowest_minor;
2088 minor < (endpoint->lowest_minor + endpoint->num_channels);
2090 device_destroy(xillybus_class, MKDEV(endpoint->major, minor));
2091 cdev_del(&endpoint->cdev);
2092 unregister_chrdev_region(MKDEV(endpoint->major,
2093 endpoint->lowest_minor),
2094 endpoint->num_channels);
2096 pr_info("xillybus: Removed %d device files.\n",
2097 endpoint->num_channels);
2101 struct xilly_endpoint *xillybus_init_endpoint(struct pci_dev *pdev,
2103 struct xilly_endpoint_hardware
2106 struct xilly_endpoint *endpoint;
2108 endpoint = kzalloc(sizeof(*endpoint), GFP_KERNEL);
2110 pr_err("xillybus: Failed to allocate memory. Aborting.\n");
2114 endpoint->pdev = pdev;
2115 endpoint->dev = dev;
2116 endpoint->ephw = ephw;
2117 INIT_LIST_HEAD(&endpoint->cleanup.to_kfree);
2118 INIT_LIST_HEAD(&endpoint->cleanup.to_pagefree);
2119 INIT_LIST_HEAD(&endpoint->cleanup.to_unmap);
2120 endpoint->msg_counter = 0x0b;
2121 endpoint->failed_messages = 0;
2122 endpoint->fatal_error = 0;
2124 init_waitqueue_head(&endpoint->ep_wait);
2125 mutex_init(&endpoint->register_mutex);
2129 EXPORT_SYMBOL(xillybus_init_endpoint);
2131 static int xilly_quiesce(struct xilly_endpoint *endpoint)
2133 endpoint->idtlen = -1;
2134 wmb(); /* Make sure idtlen is set before sending command */
2135 iowrite32((u32) (endpoint->dma_using_dac & 0x0001),
2136 &endpoint->registers[fpga_dma_control_reg]);
2139 wait_event_interruptible_timeout(endpoint->ep_wait,
2140 (endpoint->idtlen >= 0),
2143 if (endpoint->idtlen < 0) {
2144 pr_err("xillybus: Failed to quiesce the device on "
2145 "exit. Quitting while leaving a mess.\n");
2148 return 0; /* Success */
2151 int xillybus_endpoint_discovery(struct xilly_endpoint *endpoint)
2155 struct xilly_cleanup tmpmem;
2156 int idtbuffersize = (1 << PAGE_SHIFT);
2159 * The bogus IDT is used during bootstrap for allocating the initial
2160 * message buffer, and then the message buffer and space for the IDT
2161 * itself. The initial message buffer is of a single page's size, but
2162 * it's soon replaced with a more modest one (and memory is freed).
2165 unsigned char bogus_idt[8] = { 1, 224, (PAGE_SHIFT)-2, 0,
2166 3, 192, PAGE_SHIFT, 0 };
2167 struct xilly_idt_handle idt_handle;
2169 INIT_LIST_HEAD(&tmpmem.to_kfree);
2170 INIT_LIST_HEAD(&tmpmem.to_pagefree);
2171 INIT_LIST_HEAD(&tmpmem.to_unmap);
2174 * Writing the value 0x00000001 to Endianness register signals which
2175 * endianness this processor is using, so the FPGA can swap words as
2179 iowrite32(1, &endpoint->registers[fpga_endian_reg]);
2180 mmiowb(); /* Writes below are affected by the one above. */
2182 /* Bootstrap phase I: Allocate temporary message buffer */
2184 endpoint->num_channels = 0;
2186 rc = xilly_setupchannels(endpoint, &tmpmem, bogus_idt, 1);
2189 goto failed_buffers;
2191 /* Clear the message subsystem (and counter in particular) */
2192 iowrite32(0x04, &endpoint->registers[fpga_msg_ctrl_reg]);
2195 endpoint->idtlen = -1;
2200 * Set DMA 32/64 bit mode, quiesce the device (?!) and get IDT
2203 iowrite32((u32) (endpoint->dma_using_dac & 0x0001),
2204 &endpoint->registers[fpga_dma_control_reg]);
2207 wait_event_interruptible_timeout(endpoint->ep_wait,
2208 (endpoint->idtlen >= 0),
2211 if (endpoint->idtlen < 0) {
2212 pr_err("xillybus: No response from FPGA. Aborting.\n");
2214 goto failed_quiesce;
2218 iowrite32((u32) (0x0002 | (endpoint->dma_using_dac & 0x0001)),
2219 &endpoint->registers[fpga_dma_control_reg]);
2222 /* Bootstrap phase II: Allocate buffer for IDT and obtain it */
2223 while (endpoint->idtlen >= idtbuffersize) {
2228 endpoint->num_channels = 1;
2230 rc = xilly_setupchannels(endpoint, &tmpmem, bogus_idt, 2);
2237 rc = xilly_obtain_idt(endpoint);
2242 xilly_scan_idt(endpoint, &idt_handle);
2244 if (!idt_handle.chandesc) {
2248 /* Bootstrap phase III: Allocate buffers according to IDT */
2250 rc = xilly_setupchannels(endpoint,
2252 idt_handle.chandesc,
2253 idt_handle.entries);
2258 smp_wmb(); /* mutex_lock below should suffice, but won't hurt.*/
2261 * endpoint is now completely configured. We put it on the list
2262 * available to open() before registering the char device(s)
2265 mutex_lock(&ep_list_lock);
2266 list_add_tail(&endpoint->ep_list, &list_of_endpoints);
2267 mutex_unlock(&ep_list_lock);
2269 rc = xillybus_init_chrdev(endpoint, idt_handle.idt);
2272 goto failed_chrdevs;
2274 xillybus_do_cleanup(&tmpmem, endpoint);
2279 mutex_lock(&ep_list_lock);
2280 list_del(&endpoint->ep_list);
2281 mutex_unlock(&ep_list_lock);
2284 /* Quiesce the device. Now it's serious to do it */
2285 rc = xilly_quiesce(endpoint);
2288 return rc; /* FPGA may still DMA, so no release */
2290 flush_workqueue(xillybus_wq);
2293 xillybus_do_cleanup(&tmpmem, endpoint);
2297 EXPORT_SYMBOL(xillybus_endpoint_discovery);
2299 void xillybus_endpoint_remove(struct xilly_endpoint *endpoint)
2301 xillybus_cleanup_chrdev(endpoint);
2303 mutex_lock(&ep_list_lock);
2304 list_del(&endpoint->ep_list);
2305 mutex_unlock(&ep_list_lock);
2307 xilly_quiesce(endpoint);
2310 * Flushing is done upon endpoint release to prevent access to memory
2311 * just about to be released. This makes the quiesce complete.
2313 flush_workqueue(xillybus_wq);
2315 EXPORT_SYMBOL(xillybus_endpoint_remove);
2317 static int __init xillybus_init(void)
2321 mutex_init(&ep_list_lock);
2323 xillybus_class = class_create(THIS_MODULE, xillyname);
2324 if (IS_ERR(xillybus_class)) {
2325 rc = PTR_ERR(xillybus_class);
2326 pr_warn("xillybus: Failed to register class xillybus\n");
2331 xillybus_wq = alloc_workqueue(xillyname, 0, 0);
2333 return 0; /* Success */
2336 static void __exit xillybus_exit(void)
2338 /* flush_workqueue() was called for each endpoint released */
2339 destroy_workqueue(xillybus_wq);
2341 class_destroy(xillybus_class);
2344 module_init(xillybus_init);
2345 module_exit(xillybus_exit);