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 0x0008
48 #define fpga_dma_control_reg 0x0020
49 #define fpga_dma_bufno_reg 0x0024
50 #define fpga_dma_bufaddr_lowaddr_reg 0x0028
51 #define fpga_dma_bufaddr_highaddr_reg 0x002c
52 #define fpga_buf_ctrl_reg 0x0030
53 #define fpga_buf_offset_reg 0x0034
54 #define fpga_endian_reg 0x0040
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(struct xilly_endpoint
*endpoint
, 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 dev_warn(endpoint
->dev
,
116 "Malformed message (skipping): 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.
143 buf
= ep
->msgbuf_addr
;
144 buf_size
= ep
->msg_buf_size
/sizeof(u32
);
147 ep
->ephw
->hw_sync_sgl_for_cpu(ep
,
152 for (i
= 0; i
< buf_size
; i
+= 2)
153 if (((buf
[i
+1] >> 28) & 0xf) != ep
->msg_counter
) {
154 malformed_message(ep
, &buf
[i
]);
156 "Sending a NACK on counter %x (instead of %x) on entry %d\n",
157 ((buf
[i
+1] >> 28) & 0xf),
161 if (++ep
->failed_messages
> 10)
163 "Lost sync with interrupt messages. Stopping.\n");
165 ep
->ephw
->hw_sync_sgl_for_device(
171 iowrite32(0x01, /* Message NACK */
172 ep
->registers
+ fpga_msg_ctrl_reg
);
175 } else if (buf
[i
] & (1 << 22)) /* Last message */
179 dev_err(ep
->dev
, "Bad interrupt message. Stopping.\n");
185 for (i
= 0; i
<= buf_size
; i
+= 2) { /* Scan through messages */
186 opcode
= (buf
[i
] >> 24) & 0xff;
188 msg_dir
= buf
[i
] & 1;
189 msg_channel
= (buf
[i
] >> 1) & 0x7ff;
190 msg_bufno
= (buf
[i
] >> 12) & 0x3ff;
191 msg_data
= buf
[i
+1] & 0xfffffff;
194 case XILLYMSG_OPCODE_RELEASEBUF
:
196 if ((msg_channel
> ep
->num_channels
) ||
197 (msg_channel
== 0)) {
198 malformed_message(ep
, &buf
[i
]);
202 channel
= ep
->channels
[msg_channel
];
204 if (msg_dir
) { /* Write channel */
205 if (msg_bufno
>= channel
->num_wr_buffers
) {
206 malformed_message(ep
, &buf
[i
]);
209 spin_lock(&channel
->wr_spinlock
);
210 channel
->wr_buffers
[msg_bufno
]->end_offset
=
212 channel
->wr_fpga_buf_idx
= msg_bufno
;
213 channel
->wr_empty
= 0;
214 channel
->wr_sleepy
= 0;
215 spin_unlock(&channel
->wr_spinlock
);
217 wake_up_interruptible(&channel
->wr_wait
);
222 if (msg_bufno
>= channel
->num_rd_buffers
) {
223 malformed_message(ep
, &buf
[i
]);
227 spin_lock(&channel
->rd_spinlock
);
228 channel
->rd_fpga_buf_idx
= msg_bufno
;
229 channel
->rd_full
= 0;
230 spin_unlock(&channel
->rd_spinlock
);
232 wake_up_interruptible(&channel
->rd_wait
);
233 if (!channel
->rd_synchronous
)
236 &channel
->rd_workitem
,
241 case XILLYMSG_OPCODE_NONEMPTY
:
242 if ((msg_channel
> ep
->num_channels
) ||
243 (msg_channel
== 0) || (!msg_dir
) ||
244 !ep
->channels
[msg_channel
]->wr_supports_nonempty
) {
245 malformed_message(ep
, &buf
[i
]);
249 channel
= ep
->channels
[msg_channel
];
251 if (msg_bufno
>= channel
->num_wr_buffers
) {
252 malformed_message(ep
, &buf
[i
]);
255 spin_lock(&channel
->wr_spinlock
);
256 if (msg_bufno
== channel
->wr_host_buf_idx
)
257 channel
->wr_ready
= 1;
258 spin_unlock(&channel
->wr_spinlock
);
260 wake_up_interruptible(&channel
->wr_ready_wait
);
263 case XILLYMSG_OPCODE_QUIESCEACK
:
264 ep
->idtlen
= msg_data
;
265 wake_up_interruptible(&ep
->ep_wait
);
268 case XILLYMSG_OPCODE_FIFOEOF
:
269 if ((msg_channel
> ep
->num_channels
) ||
270 (msg_channel
== 0) || (!msg_dir
) ||
271 !ep
->channels
[msg_channel
]->num_wr_buffers
) {
272 malformed_message(ep
, &buf
[i
]);
275 channel
= ep
->channels
[msg_channel
];
276 spin_lock(&channel
->wr_spinlock
);
277 channel
->wr_eof
= msg_bufno
;
278 channel
->wr_sleepy
= 0;
280 channel
->wr_hangup
= channel
->wr_empty
&&
281 (channel
->wr_host_buf_idx
== msg_bufno
);
283 spin_unlock(&channel
->wr_spinlock
);
285 wake_up_interruptible(&channel
->wr_wait
);
288 case XILLYMSG_OPCODE_FATAL_ERROR
:
290 wake_up_interruptible(&ep
->ep_wait
); /* For select() */
292 "FPGA reported a fatal error. This means that the low-level communication with the device has failed. This hardware problem is most likely unrelated to Xillybus (neither kernel module nor FPGA core), but reports are still welcome. All I/O is aborted.\n");
295 malformed_message(ep
, &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 static void xillybus_autoflush(struct work_struct
*work
);
321 struct xilly_alloc_state
{
325 enum dma_data_direction direction
;
329 static int xilly_get_dma_buffers(struct xilly_endpoint
*ep
,
330 struct xilly_alloc_state
*s
,
331 struct xilly_buffer
**buffers
,
332 int bufnum
, int bytebufsize
)
336 struct device
*dev
= ep
->dev
;
337 struct xilly_buffer
*this_buffer
= NULL
; /* Init to silence warning */
339 if (buffers
) { /* Not the message buffer */
340 this_buffer
= devm_kzalloc(
341 dev
, bufnum
* sizeof(struct xilly_buffer
),
348 for (i
= 0; i
< bufnum
; i
++) {
350 * Buffers are expected in descending size order, so there
351 * is either enough space for this buffer or none at all.
354 if ((s
->left_of_salami
< bytebufsize
) &&
355 (s
->left_of_salami
> 0)) {
357 "Corrupt buffer allocation in IDT. Aborting.\n");
361 if (s
->left_of_salami
== 0) {
362 int allocorder
, allocsize
;
364 allocsize
= PAGE_SIZE
;
366 while (bytebufsize
> allocsize
) {
371 s
->salami
= (void *) devm_get_free_pages(
373 GFP_KERNEL
| __GFP_DMA32
| __GFP_ZERO
,
378 s
->left_of_salami
= allocsize
;
381 rc
= ep
->ephw
->map_single(ep
, s
->salami
,
382 bytebufsize
, s
->direction
,
388 iowrite32((u32
) (dma_addr
& 0xffffffff),
389 ep
->registers
+ fpga_dma_bufaddr_lowaddr_reg
);
390 iowrite32(((u32
) ((((u64
) dma_addr
) >> 32) & 0xffffffff)),
391 ep
->registers
+ fpga_dma_bufaddr_highaddr_reg
);
394 if (buffers
) { /* Not the message buffer */
395 this_buffer
->addr
= s
->salami
;
396 this_buffer
->dma_addr
= dma_addr
;
397 buffers
[i
] = this_buffer
++;
399 iowrite32(s
->regdirection
| s
->nbuffer
++,
400 ep
->registers
+ fpga_dma_bufno_reg
);
402 ep
->msgbuf_addr
= s
->salami
;
403 ep
->msgbuf_dma_addr
= dma_addr
;
404 ep
->msg_buf_size
= bytebufsize
;
406 iowrite32(s
->regdirection
,
407 ep
->registers
+ fpga_dma_bufno_reg
);
410 s
->left_of_salami
-= bytebufsize
;
411 s
->salami
+= bytebufsize
;
413 return 0; /* Success */
416 static int xilly_setupchannels(struct xilly_endpoint
*ep
,
417 unsigned char *chandesc
,
421 struct device
*dev
= ep
->dev
;
423 struct xilly_channel
*channel
;
424 int channelnum
, bufnum
, bufsize
, format
, is_writebuf
;
426 int synchronous
, allowpartial
, exclusive_open
, seekable
;
427 int supports_nonempty
;
428 int msg_buf_done
= 0;
430 struct xilly_alloc_state rd_alloc
= {
434 .direction
= DMA_TO_DEVICE
,
438 struct xilly_alloc_state wr_alloc
= {
442 .direction
= DMA_FROM_DEVICE
,
443 .regdirection
= 0x80000000,
446 channel
= devm_kzalloc(dev
, ep
->num_channels
*
447 sizeof(struct xilly_channel
), GFP_KERNEL
);
452 ep
->channels
= devm_kzalloc(dev
, (ep
->num_channels
+ 1) *
453 sizeof(struct xilly_channel
*),
459 ep
->channels
[0] = NULL
; /* Channel 0 is message buf. */
461 /* Initialize all channels with defaults */
463 for (i
= 1; i
<= ep
->num_channels
; i
++) {
464 channel
->wr_buffers
= NULL
;
465 channel
->rd_buffers
= NULL
;
466 channel
->num_wr_buffers
= 0;
467 channel
->num_rd_buffers
= 0;
468 channel
->wr_fpga_buf_idx
= -1;
469 channel
->wr_host_buf_idx
= 0;
470 channel
->wr_host_buf_pos
= 0;
471 channel
->wr_empty
= 1;
472 channel
->wr_ready
= 0;
473 channel
->wr_sleepy
= 1;
474 channel
->rd_fpga_buf_idx
= 0;
475 channel
->rd_host_buf_idx
= 0;
476 channel
->rd_host_buf_pos
= 0;
477 channel
->rd_full
= 0;
478 channel
->wr_ref_count
= 0;
479 channel
->rd_ref_count
= 0;
481 spin_lock_init(&channel
->wr_spinlock
);
482 spin_lock_init(&channel
->rd_spinlock
);
483 mutex_init(&channel
->wr_mutex
);
484 mutex_init(&channel
->rd_mutex
);
485 init_waitqueue_head(&channel
->rd_wait
);
486 init_waitqueue_head(&channel
->wr_wait
);
487 init_waitqueue_head(&channel
->wr_ready_wait
);
489 INIT_DELAYED_WORK(&channel
->rd_workitem
, xillybus_autoflush
);
491 channel
->endpoint
= ep
;
492 channel
->chan_num
= i
;
494 channel
->log2_element_size
= 0;
496 ep
->channels
[i
] = channel
++;
499 for (entry
= 0; entry
< entries
; entry
++, chandesc
+= 4) {
500 struct xilly_buffer
**buffers
= NULL
;
502 is_writebuf
= chandesc
[0] & 0x01;
503 channelnum
= (chandesc
[0] >> 1) | ((chandesc
[1] & 0x0f) << 7);
504 format
= (chandesc
[1] >> 4) & 0x03;
505 allowpartial
= (chandesc
[1] >> 6) & 0x01;
506 synchronous
= (chandesc
[1] >> 7) & 0x01;
507 bufsize
= 1 << (chandesc
[2] & 0x1f);
508 bufnum
= 1 << (chandesc
[3] & 0x0f);
509 exclusive_open
= (chandesc
[2] >> 7) & 0x01;
510 seekable
= (chandesc
[2] >> 6) & 0x01;
511 supports_nonempty
= (chandesc
[2] >> 5) & 0x01;
513 if ((channelnum
> ep
->num_channels
) ||
514 ((channelnum
== 0) && !is_writebuf
)) {
516 "IDT requests channel out of range. Aborting.\n");
520 channel
= ep
->channels
[channelnum
]; /* NULL for msg channel */
522 if (!is_writebuf
|| channelnum
> 0) {
523 channel
->log2_element_size
= ((format
> 2) ?
526 bytebufsize
= channel
->rd_buf_size
= bufsize
*
527 (1 << channel
->log2_element_size
);
529 buffers
= devm_kzalloc(dev
,
530 bufnum
* sizeof(struct xilly_buffer
*),
536 bytebufsize
= bufsize
<< 2;
539 channel
->num_rd_buffers
= bufnum
;
540 channel
->rd_allow_partial
= allowpartial
;
541 channel
->rd_synchronous
= synchronous
;
542 channel
->rd_exclusive_open
= exclusive_open
;
543 channel
->seekable
= seekable
;
545 channel
->rd_buffers
= buffers
;
546 rc
= xilly_get_dma_buffers(ep
, &rd_alloc
, buffers
,
547 bufnum
, bytebufsize
);
548 } else if (channelnum
> 0) {
549 channel
->num_wr_buffers
= bufnum
;
551 channel
->seekable
= seekable
;
552 channel
->wr_supports_nonempty
= supports_nonempty
;
554 channel
->wr_allow_partial
= allowpartial
;
555 channel
->wr_synchronous
= synchronous
;
556 channel
->wr_exclusive_open
= exclusive_open
;
558 channel
->wr_buffers
= buffers
;
559 rc
= xilly_get_dma_buffers(ep
, &wr_alloc
, buffers
,
560 bufnum
, bytebufsize
);
562 rc
= xilly_get_dma_buffers(ep
, &wr_alloc
, NULL
,
563 bufnum
, bytebufsize
);
573 "Corrupt IDT: No message buffer. Aborting.\n");
580 "Failed to assign DMA buffer memory. Aborting.\n");
584 static void xilly_scan_idt(struct xilly_endpoint
*endpoint
,
585 struct xilly_idt_handle
*idt_handle
)
588 unsigned char *idt
= endpoint
->channels
[1]->wr_buffers
[0]->addr
;
589 unsigned char *end_of_idt
= idt
+ endpoint
->idtlen
- 4;
594 idt_handle
->idt
= idt
;
596 scan
++; /* Skip version number */
598 while ((scan
<= end_of_idt
) && *scan
) {
599 while ((scan
<= end_of_idt
) && *scan
++)
600 /* Do nothing, just scan thru string */;
606 if (scan
> end_of_idt
) {
607 dev_err(endpoint
->dev
,
608 "IDT device name list overflow. Aborting.\n");
609 idt_handle
->chandesc
= NULL
;
612 idt_handle
->chandesc
= scan
;
614 len
= endpoint
->idtlen
- (3 + ((int) (scan
- idt
)));
617 idt_handle
->chandesc
= NULL
;
619 dev_err(endpoint
->dev
,
620 "Corrupt IDT device name list. Aborting.\n");
623 idt_handle
->entries
= len
>> 2;
625 endpoint
->num_channels
= count
;
628 static int xilly_obtain_idt(struct xilly_endpoint
*endpoint
)
631 struct xilly_channel
*channel
;
632 unsigned char *version
;
634 channel
= endpoint
->channels
[1]; /* This should be generated ad-hoc */
636 channel
->wr_sleepy
= 1;
637 wmb(); /* Setting wr_sleepy must come before the command */
640 (3 << 24), /* Opcode 3 for channel 0 = Send IDT */
641 endpoint
->registers
+ fpga_buf_ctrl_reg
);
642 mmiowb(); /* Just to appear safe */
644 wait_event_interruptible_timeout(channel
->wr_wait
,
645 (!channel
->wr_sleepy
),
648 if (channel
->wr_sleepy
) {
649 dev_err(endpoint
->dev
, "Failed to obtain IDT. Aborting.\n");
651 if (endpoint
->fatal_error
)
658 endpoint
->ephw
->hw_sync_sgl_for_cpu(
660 channel
->wr_buffers
[0]->dma_addr
,
661 channel
->wr_buf_size
,
664 if (channel
->wr_buffers
[0]->end_offset
!= endpoint
->idtlen
) {
665 dev_err(endpoint
->dev
,
666 "IDT length mismatch (%d != %d). Aborting.\n",
667 channel
->wr_buffers
[0]->end_offset
, endpoint
->idtlen
);
672 if (crc32_le(~0, channel
->wr_buffers
[0]->addr
,
673 endpoint
->idtlen
+1) != 0) {
674 dev_err(endpoint
->dev
, "IDT failed CRC check. Aborting.\n");
679 version
= channel
->wr_buffers
[0]->addr
;
681 /* Check version number. Accept anything below 0x82 for now. */
682 if (*version
> 0x82) {
683 dev_err(endpoint
->dev
,
684 "No support for IDT version 0x%02x. Maybe the xillybus driver needs an upgarde. Aborting.\n",
690 return 0; /* Success */
693 static ssize_t
xillybus_read(struct file
*filp
, char __user
*userbuf
,
694 size_t count
, loff_t
*f_pos
)
699 int no_time_left
= 0;
700 long deadline
, left_to_sleep
;
701 struct xilly_channel
*channel
= filp
->private_data
;
703 int empty
, reached_eof
, exhausted
, ready
;
704 /* Initializations are there only to silence warnings */
706 int howmany
= 0, bufpos
= 0, bufidx
= 0, bufferdone
= 0;
709 if (channel
->endpoint
->fatal_error
)
712 deadline
= jiffies
+ 1 + XILLY_RX_TIMEOUT
;
714 rc
= mutex_lock_interruptible(&channel
->wr_mutex
);
719 rc
= 0; /* Just to be clear about it. Compiler optimizes this out */
721 while (1) { /* Note that we may drop mutex within this loop */
722 int bytes_to_do
= count
- bytes_done
;
724 spin_lock_irqsave(&channel
->wr_spinlock
, flags
);
726 empty
= channel
->wr_empty
;
727 ready
= !empty
|| channel
->wr_ready
;
730 bufidx
= channel
->wr_host_buf_idx
;
731 bufpos
= channel
->wr_host_buf_pos
;
732 howmany
= ((channel
->wr_buffers
[bufidx
]->end_offset
733 + 1) << channel
->log2_element_size
)
736 /* Update wr_host_* to its post-operation state */
737 if (howmany
> bytes_to_do
) {
740 howmany
= bytes_to_do
;
741 channel
->wr_host_buf_pos
+= howmany
;
745 channel
->wr_host_buf_pos
= 0;
747 if (bufidx
== channel
->wr_fpga_buf_idx
) {
748 channel
->wr_empty
= 1;
749 channel
->wr_sleepy
= 1;
750 channel
->wr_ready
= 0;
753 if (bufidx
>= (channel
->num_wr_buffers
- 1))
754 channel
->wr_host_buf_idx
= 0;
756 channel
->wr_host_buf_idx
++;
761 * Marking our situation after the possible changes above,
762 * for use after releasing the spinlock.
764 * empty = empty before change
765 * exhasted = empty after possible change
768 reached_eof
= channel
->wr_empty
&&
769 (channel
->wr_host_buf_idx
== channel
->wr_eof
);
770 channel
->wr_hangup
= reached_eof
;
771 exhausted
= channel
->wr_empty
;
772 waiting_bufidx
= channel
->wr_host_buf_idx
;
774 spin_unlock_irqrestore(&channel
->wr_spinlock
, flags
);
776 if (!empty
) { /* Go on, now without the spinlock */
778 if (bufpos
== 0) /* Position zero means it's virgin */
779 channel
->endpoint
->ephw
->hw_sync_sgl_for_cpu(
781 channel
->wr_buffers
[bufidx
]->dma_addr
,
782 channel
->wr_buf_size
,
787 channel
->wr_buffers
[bufidx
]->addr
792 bytes_done
+= howmany
;
795 channel
->endpoint
->ephw
->
796 hw_sync_sgl_for_device
799 channel
->wr_buffers
[bufidx
]->
801 channel
->wr_buf_size
,
805 * Tell FPGA the buffer is done with. It's an
806 * atomic operation to the FPGA, so what
807 * happens with other channels doesn't matter,
808 * and the certain channel is protected with
809 * the channel-specific mutex.
812 iowrite32(1 | (channel
->chan_num
<< 1)
814 channel
->endpoint
->registers
+
816 mmiowb(); /* Just to appear safe */
820 mutex_unlock(&channel
->wr_mutex
);
825 /* This includes a zero-count return = EOF */
826 if ((bytes_done
>= count
) || reached_eof
)
830 continue; /* More in RAM buffer(s)? Just go on. */
832 if ((bytes_done
> 0) &&
834 (channel
->wr_synchronous
&& channel
->wr_allow_partial
)))
838 * Nonblocking read: The "ready" flag tells us that the FPGA
839 * has data to send. In non-blocking mode, if it isn't on,
840 * just return. But if there is, we jump directly to the point
841 * where we ask for the FPGA to send all it has, and wait
842 * until that data arrives. So in a sense, we *do* block in
843 * nonblocking mode, but only for a very short time.
846 if (!no_time_left
&& (filp
->f_flags
& O_NONBLOCK
)) {
853 bytes_done
= -EAGAIN
;
857 if (!no_time_left
|| (bytes_done
> 0)) {
859 * Note that in case of an element-misaligned read
860 * request, offsetlimit will include the last element,
861 * which will be partially read from.
863 int offsetlimit
= ((count
- bytes_done
) - 1) >>
864 channel
->log2_element_size
;
865 int buf_elements
= channel
->wr_buf_size
>>
866 channel
->log2_element_size
;
869 * In synchronous mode, always send an offset limit.
870 * Just don't send a value too big.
873 if (channel
->wr_synchronous
) {
874 /* Don't request more than one buffer */
875 if (channel
->wr_allow_partial
&&
876 (offsetlimit
>= buf_elements
))
877 offsetlimit
= buf_elements
- 1;
879 /* Don't request more than all buffers */
880 if (!channel
->wr_allow_partial
&&
882 (buf_elements
* channel
->num_wr_buffers
)))
883 offsetlimit
= buf_elements
*
884 channel
->num_wr_buffers
- 1;
888 * In asynchronous mode, force early flush of a buffer
889 * only if that will allow returning a full count. The
890 * "offsetlimit < ( ... )" rather than "<=" excludes
891 * requesting a full buffer, which would obviously
892 * cause a buffer transmission anyhow
895 if (channel
->wr_synchronous
||
896 (offsetlimit
< (buf_elements
- 1))) {
898 mutex_lock(&channel
->endpoint
->register_mutex
);
900 iowrite32(offsetlimit
,
901 channel
->endpoint
->registers
+
902 fpga_buf_offset_reg
);
905 iowrite32(1 | (channel
->chan_num
<< 1) |
906 (2 << 24) | /* 2 = offset limit */
907 (waiting_bufidx
<< 12),
908 channel
->endpoint
->registers
+
911 mmiowb(); /* Just to appear safe */
913 mutex_unlock(&channel
->endpoint
->
920 * If partial completion is disallowed, there is no point in
921 * timeout sleeping. Neither if no_time_left is set and
925 if (!channel
->wr_allow_partial
||
926 (no_time_left
&& (bytes_done
== 0))) {
929 * This do-loop will run more than once if another
930 * thread reasserted wr_sleepy before we got the mutex
931 * back, so we try again.
935 mutex_unlock(&channel
->wr_mutex
);
937 if (wait_event_interruptible(
939 (!channel
->wr_sleepy
)))
942 if (mutex_lock_interruptible(
945 } while (channel
->wr_sleepy
);
949 interrupted
: /* Mutex is not held if got here */
950 if (channel
->endpoint
->fatal_error
)
954 if (filp
->f_flags
& O_NONBLOCK
)
955 return -EAGAIN
; /* Don't admit snoozing */
959 left_to_sleep
= deadline
- ((long) jiffies
);
962 * If our time is out, skip the waiting. We may miss wr_sleepy
963 * being deasserted but hey, almost missing the train is like
967 if (left_to_sleep
> 0) {
969 wait_event_interruptible_timeout(
971 (!channel
->wr_sleepy
),
974 if (!channel
->wr_sleepy
)
977 if (left_to_sleep
< 0) { /* Interrupt */
978 mutex_unlock(&channel
->wr_mutex
);
979 if (channel
->endpoint
->fatal_error
)
988 no_time_left
= 1; /* We're out of sleeping time. Desperate! */
990 if (bytes_done
== 0) {
992 * Reaching here means that we allow partial return,
993 * that we've run out of time, and that we have
995 * So tell the FPGA to send anything it has or gets.
998 iowrite32(1 | (channel
->chan_num
<< 1) |
999 (3 << 24) | /* Opcode 3, flush it all! */
1000 (waiting_bufidx
<< 12),
1001 channel
->endpoint
->registers
+
1003 mmiowb(); /* Just to appear safe */
1007 * Formally speaking, we should block for data at this point.
1008 * But to keep the code cleaner, we'll just finish the loop,
1009 * make the unlikely check for data, and then block at the
1014 mutex_unlock(&channel
->wr_mutex
);
1016 if (channel
->endpoint
->fatal_error
)
1023 * The timeout argument takes values as follows:
1024 * >0 : Flush with timeout
1025 * ==0 : Flush, and wait idefinitely for the flush to complete
1026 * <0 : Autoflush: Flush only if there's a single buffer occupied
1029 static int xillybus_myflush(struct xilly_channel
*channel
, long timeout
)
1032 unsigned long flags
;
1034 int end_offset_plus1
;
1035 int bufidx
, bufidx_minus1
;
1038 int new_rd_host_buf_pos
;
1040 if (channel
->endpoint
->fatal_error
)
1042 rc
= mutex_lock_interruptible(&channel
->rd_mutex
);
1048 * Don't flush a closed channel. This can happen when the work queued
1049 * autoflush thread fires off after the file has closed. This is not
1050 * an error, just something to dismiss.
1053 if (!channel
->rd_ref_count
)
1056 bufidx
= channel
->rd_host_buf_idx
;
1058 bufidx_minus1
= (bufidx
== 0) ? channel
->num_rd_buffers
- 1 : bufidx
-1;
1060 end_offset_plus1
= channel
->rd_host_buf_pos
>>
1061 channel
->log2_element_size
;
1063 new_rd_host_buf_pos
= channel
->rd_host_buf_pos
-
1064 (end_offset_plus1
<< channel
->log2_element_size
);
1066 /* Submit the current buffer if it's nonempty */
1067 if (end_offset_plus1
) {
1068 unsigned char *tail
= channel
->rd_buffers
[bufidx
]->addr
+
1069 (end_offset_plus1
<< channel
->log2_element_size
);
1071 /* Copy unflushed data, so we can put it in next buffer */
1072 for (i
= 0; i
< new_rd_host_buf_pos
; i
++)
1073 channel
->rd_leftovers
[i
] = *tail
++;
1075 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1077 /* Autoflush only if a single buffer is occupied */
1079 if ((timeout
< 0) &&
1080 (channel
->rd_full
||
1081 (bufidx_minus1
!= channel
->rd_fpga_buf_idx
))) {
1082 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1084 * A new work item may be queued by the ISR exactly
1085 * now, since the execution of a work item allows the
1086 * queuing of a new one while it's running.
1091 /* The 4th element is never needed for data, so it's a flag */
1092 channel
->rd_leftovers
[3] = (new_rd_host_buf_pos
!= 0);
1094 /* Set up rd_full to reflect a certain moment's state */
1096 if (bufidx
== channel
->rd_fpga_buf_idx
)
1097 channel
->rd_full
= 1;
1098 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1100 if (bufidx
>= (channel
->num_rd_buffers
- 1))
1101 channel
->rd_host_buf_idx
= 0;
1103 channel
->rd_host_buf_idx
++;
1105 channel
->endpoint
->ephw
->hw_sync_sgl_for_device(
1107 channel
->rd_buffers
[bufidx
]->dma_addr
,
1108 channel
->rd_buf_size
,
1111 mutex_lock(&channel
->endpoint
->register_mutex
);
1113 iowrite32(end_offset_plus1
- 1,
1114 channel
->endpoint
->registers
+ fpga_buf_offset_reg
);
1117 iowrite32((channel
->chan_num
<< 1) | /* Channel ID */
1118 (2 << 24) | /* Opcode 2, submit buffer */
1120 channel
->endpoint
->registers
+ fpga_buf_ctrl_reg
);
1121 mmiowb(); /* Just to appear safe */
1123 mutex_unlock(&channel
->endpoint
->register_mutex
);
1124 } else if (bufidx
== 0)
1125 bufidx
= channel
->num_rd_buffers
- 1;
1129 channel
->rd_host_buf_pos
= new_rd_host_buf_pos
;
1132 goto done
; /* Autoflush */
1136 * bufidx is now the last buffer written to (or equal to
1137 * rd_fpga_buf_idx if buffer was never written to), and
1138 * channel->rd_host_buf_idx the one after it.
1140 * If bufidx == channel->rd_fpga_buf_idx we're either empty or full.
1145 while (1) { /* Loop waiting for draining of buffers */
1146 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1148 if (bufidx
!= channel
->rd_fpga_buf_idx
)
1149 channel
->rd_full
= 1; /*
1151 * but needs waiting.
1154 empty
= !channel
->rd_full
;
1156 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1162 * Indefinite sleep with mutex taken. With data waiting for
1163 * flushing user should not be surprised if open() for write
1167 wait_event_interruptible(channel
->rd_wait
,
1168 (!channel
->rd_full
));
1170 else if (wait_event_interruptible_timeout(
1172 (!channel
->rd_full
),
1174 dev_warn(channel
->endpoint
->dev
,
1175 "Timed out while flushing. Output data may be lost.\n");
1181 if (channel
->rd_full
) {
1188 mutex_unlock(&channel
->rd_mutex
);
1190 if (channel
->endpoint
->fatal_error
)
1196 static int xillybus_flush(struct file
*filp
, fl_owner_t id
)
1198 if (!(filp
->f_mode
& FMODE_WRITE
))
1201 return xillybus_myflush(filp
->private_data
, HZ
); /* 1 second timeout */
1204 static void xillybus_autoflush(struct work_struct
*work
)
1206 struct delayed_work
*workitem
= container_of(
1207 work
, struct delayed_work
, work
);
1208 struct xilly_channel
*channel
= container_of(
1209 workitem
, struct xilly_channel
, rd_workitem
);
1212 rc
= xillybus_myflush(channel
, -1);
1215 dev_warn(channel
->endpoint
->dev
,
1216 "Autoflush failed because work queue thread got a signal.\n");
1218 dev_err(channel
->endpoint
->dev
,
1219 "Autoflush failed under weird circumstances.\n");
1222 static ssize_t
xillybus_write(struct file
*filp
, const char __user
*userbuf
,
1223 size_t count
, loff_t
*f_pos
)
1226 unsigned long flags
;
1228 struct xilly_channel
*channel
= filp
->private_data
;
1230 int full
, exhausted
;
1231 /* Initializations are there only to silence warnings */
1233 int howmany
= 0, bufpos
= 0, bufidx
= 0, bufferdone
= 0;
1234 int end_offset_plus1
= 0;
1236 if (channel
->endpoint
->fatal_error
)
1239 rc
= mutex_lock_interruptible(&channel
->rd_mutex
);
1244 rc
= 0; /* Just to be clear about it. Compiler optimizes this out */
1247 int bytes_to_do
= count
- bytes_done
;
1249 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1251 full
= channel
->rd_full
;
1254 bufidx
= channel
->rd_host_buf_idx
;
1255 bufpos
= channel
->rd_host_buf_pos
;
1256 howmany
= channel
->rd_buf_size
- bufpos
;
1259 * Update rd_host_* to its state after this operation.
1260 * count=0 means committing the buffer immediately,
1261 * which is like flushing, but not necessarily block.
1264 if ((howmany
> bytes_to_do
) &&
1266 ((bufpos
>> channel
->log2_element_size
) == 0))) {
1269 howmany
= bytes_to_do
;
1270 channel
->rd_host_buf_pos
+= howmany
;
1276 channel
->rd_buf_size
>>
1277 channel
->log2_element_size
;
1278 channel
->rd_host_buf_pos
= 0;
1280 unsigned char *tail
;
1283 end_offset_plus1
= bufpos
>>
1284 channel
->log2_element_size
;
1286 channel
->rd_host_buf_pos
-=
1288 channel
->log2_element_size
;
1291 rd_buffers
[bufidx
]->addr
+
1292 (end_offset_plus1
<<
1293 channel
->log2_element_size
);
1296 i
< channel
->rd_host_buf_pos
;
1298 channel
->rd_leftovers
[i
] =
1302 if (bufidx
== channel
->rd_fpga_buf_idx
)
1303 channel
->rd_full
= 1;
1305 if (bufidx
>= (channel
->num_rd_buffers
- 1))
1306 channel
->rd_host_buf_idx
= 0;
1308 channel
->rd_host_buf_idx
++;
1313 * Marking our situation after the possible changes above,
1314 * for use after releasing the spinlock.
1316 * full = full before change
1317 * exhasted = full after possible change
1320 exhausted
= channel
->rd_full
;
1322 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1324 if (!full
) { /* Go on, now without the spinlock */
1325 unsigned char *head
=
1326 channel
->rd_buffers
[bufidx
]->addr
;
1329 if ((bufpos
== 0) || /* Zero means it's virgin */
1330 (channel
->rd_leftovers
[3] != 0)) {
1331 channel
->endpoint
->ephw
->hw_sync_sgl_for_cpu(
1333 channel
->rd_buffers
[bufidx
]->dma_addr
,
1334 channel
->rd_buf_size
,
1337 /* Virgin, but leftovers are due */
1338 for (i
= 0; i
< bufpos
; i
++)
1339 *head
++ = channel
->rd_leftovers
[i
];
1341 channel
->rd_leftovers
[3] = 0; /* Clear flag */
1345 channel
->rd_buffers
[bufidx
]->addr
+ bufpos
,
1350 bytes_done
+= howmany
;
1353 channel
->endpoint
->ephw
->
1354 hw_sync_sgl_for_device(
1356 channel
->rd_buffers
[bufidx
]->
1358 channel
->rd_buf_size
,
1361 mutex_lock(&channel
->endpoint
->register_mutex
);
1363 iowrite32(end_offset_plus1
- 1,
1364 channel
->endpoint
->registers
+
1365 fpga_buf_offset_reg
);
1367 iowrite32((channel
->chan_num
<< 1) |
1368 (2 << 24) | /* 2 = submit buffer */
1370 channel
->endpoint
->registers
+
1372 mmiowb(); /* Just to appear safe */
1374 mutex_unlock(&channel
->endpoint
->
1377 channel
->rd_leftovers
[3] =
1378 (channel
->rd_host_buf_pos
!= 0);
1382 mutex_unlock(&channel
->rd_mutex
);
1384 if (channel
->endpoint
->fatal_error
)
1387 if (!channel
->rd_synchronous
)
1390 &channel
->rd_workitem
,
1397 if (bytes_done
>= count
)
1401 continue; /* If there's more space, just go on */
1403 if ((bytes_done
> 0) && channel
->rd_allow_partial
)
1407 * Indefinite sleep with mutex taken. With data waiting for
1408 * flushing, user should not be surprised if open() for write
1412 if (filp
->f_flags
& O_NONBLOCK
) {
1413 bytes_done
= -EAGAIN
;
1417 wait_event_interruptible(channel
->rd_wait
,
1418 (!channel
->rd_full
));
1420 if (channel
->rd_full
) {
1421 mutex_unlock(&channel
->rd_mutex
);
1423 if (channel
->endpoint
->fatal_error
)
1432 mutex_unlock(&channel
->rd_mutex
);
1434 if (!channel
->rd_synchronous
)
1435 queue_delayed_work(xillybus_wq
,
1436 &channel
->rd_workitem
,
1439 if ((channel
->rd_synchronous
) && (bytes_done
> 0)) {
1440 rc
= xillybus_myflush(filp
->private_data
, 0); /* No timeout */
1442 if (rc
&& (rc
!= -EINTR
))
1446 if (channel
->endpoint
->fatal_error
)
1452 static int xillybus_open(struct inode
*inode
, struct file
*filp
)
1455 unsigned long flags
;
1456 int minor
= iminor(inode
);
1457 int major
= imajor(inode
);
1458 struct xilly_endpoint
*ep_iter
, *endpoint
= NULL
;
1459 struct xilly_channel
*channel
;
1461 mutex_lock(&ep_list_lock
);
1463 list_for_each_entry(ep_iter
, &list_of_endpoints
, ep_list
) {
1464 if ((ep_iter
->major
== major
) &&
1465 (minor
>= ep_iter
->lowest_minor
) &&
1466 (minor
< (ep_iter
->lowest_minor
+
1467 ep_iter
->num_channels
))) {
1472 mutex_unlock(&ep_list_lock
);
1475 pr_err("xillybus: open() failed to find a device for major=%d and minor=%d\n",
1480 if (endpoint
->fatal_error
)
1483 channel
= endpoint
->channels
[1 + minor
- endpoint
->lowest_minor
];
1484 filp
->private_data
= channel
;
1488 * It gets complicated because:
1489 * 1. We don't want to take a mutex we don't have to
1490 * 2. We don't want to open one direction if the other will fail.
1493 if ((filp
->f_mode
& FMODE_READ
) && (!channel
->num_wr_buffers
))
1496 if ((filp
->f_mode
& FMODE_WRITE
) && (!channel
->num_rd_buffers
))
1499 if ((filp
->f_mode
& FMODE_READ
) && (filp
->f_flags
& O_NONBLOCK
) &&
1500 (channel
->wr_synchronous
|| !channel
->wr_allow_partial
||
1501 !channel
->wr_supports_nonempty
)) {
1502 dev_err(endpoint
->dev
,
1503 "open() failed: O_NONBLOCK not allowed for read on this device\n");
1507 if ((filp
->f_mode
& FMODE_WRITE
) && (filp
->f_flags
& O_NONBLOCK
) &&
1508 (channel
->rd_synchronous
|| !channel
->rd_allow_partial
)) {
1509 dev_err(endpoint
->dev
,
1510 "open() failed: O_NONBLOCK not allowed for write on this device\n");
1515 * Note: open() may block on getting mutexes despite O_NONBLOCK.
1516 * This shouldn't occur normally, since multiple open of the same
1517 * file descriptor is almost always prohibited anyhow
1518 * (*_exclusive_open is normally set in real-life systems).
1521 if (filp
->f_mode
& FMODE_READ
) {
1522 rc
= mutex_lock_interruptible(&channel
->wr_mutex
);
1527 if (filp
->f_mode
& FMODE_WRITE
) {
1528 rc
= mutex_lock_interruptible(&channel
->rd_mutex
);
1533 if ((filp
->f_mode
& FMODE_READ
) &&
1534 (channel
->wr_ref_count
!= 0) &&
1535 (channel
->wr_exclusive_open
)) {
1540 if ((filp
->f_mode
& FMODE_WRITE
) &&
1541 (channel
->rd_ref_count
!= 0) &&
1542 (channel
->rd_exclusive_open
)) {
1548 if (filp
->f_mode
& FMODE_READ
) {
1549 if (channel
->wr_ref_count
== 0) { /* First open of file */
1550 /* Move the host to first buffer */
1551 spin_lock_irqsave(&channel
->wr_spinlock
, flags
);
1552 channel
->wr_host_buf_idx
= 0;
1553 channel
->wr_host_buf_pos
= 0;
1554 channel
->wr_fpga_buf_idx
= -1;
1555 channel
->wr_empty
= 1;
1556 channel
->wr_ready
= 0;
1557 channel
->wr_sleepy
= 1;
1558 channel
->wr_eof
= -1;
1559 channel
->wr_hangup
= 0;
1561 spin_unlock_irqrestore(&channel
->wr_spinlock
, flags
);
1563 iowrite32(1 | (channel
->chan_num
<< 1) |
1564 (4 << 24) | /* Opcode 4, open channel */
1565 ((channel
->wr_synchronous
& 1) << 23),
1566 channel
->endpoint
->registers
+
1568 mmiowb(); /* Just to appear safe */
1571 channel
->wr_ref_count
++;
1574 if (filp
->f_mode
& FMODE_WRITE
) {
1575 if (channel
->rd_ref_count
== 0) { /* First open of file */
1576 /* Move the host to first buffer */
1577 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1578 channel
->rd_host_buf_idx
= 0;
1579 channel
->rd_host_buf_pos
= 0;
1580 channel
->rd_leftovers
[3] = 0; /* No leftovers. */
1581 channel
->rd_fpga_buf_idx
= channel
->num_rd_buffers
- 1;
1582 channel
->rd_full
= 0;
1584 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1586 iowrite32((channel
->chan_num
<< 1) |
1587 (4 << 24), /* Opcode 4, open channel */
1588 channel
->endpoint
->registers
+
1590 mmiowb(); /* Just to appear safe */
1593 channel
->rd_ref_count
++;
1597 if (filp
->f_mode
& FMODE_WRITE
)
1598 mutex_unlock(&channel
->rd_mutex
);
1600 if (filp
->f_mode
& FMODE_READ
)
1601 mutex_unlock(&channel
->wr_mutex
);
1603 if (!rc
&& (!channel
->seekable
))
1604 return nonseekable_open(inode
, filp
);
1609 static int xillybus_release(struct inode
*inode
, struct file
*filp
)
1612 unsigned long flags
;
1613 struct xilly_channel
*channel
= filp
->private_data
;
1618 if (channel
->endpoint
->fatal_error
)
1621 if (filp
->f_mode
& FMODE_WRITE
) {
1622 rc
= mutex_lock_interruptible(&channel
->rd_mutex
);
1625 dev_warn(channel
->endpoint
->dev
,
1626 "Failed to close file. Hardware left in messy state.\n");
1630 channel
->rd_ref_count
--;
1632 if (channel
->rd_ref_count
== 0) {
1635 * We rely on the kernel calling flush()
1636 * before we get here.
1639 iowrite32((channel
->chan_num
<< 1) | /* Channel ID */
1640 (5 << 24), /* Opcode 5, close channel */
1641 channel
->endpoint
->registers
+
1643 mmiowb(); /* Just to appear safe */
1645 mutex_unlock(&channel
->rd_mutex
);
1648 if (filp
->f_mode
& FMODE_READ
) {
1649 rc
= mutex_lock_interruptible(&channel
->wr_mutex
);
1651 dev_warn(channel
->endpoint
->dev
,
1652 "Failed to close file. Hardware left in messy state.\n");
1656 channel
->wr_ref_count
--;
1658 if (channel
->wr_ref_count
== 0) {
1660 iowrite32(1 | (channel
->chan_num
<< 1) |
1661 (5 << 24), /* Opcode 5, close channel */
1662 channel
->endpoint
->registers
+
1664 mmiowb(); /* Just to appear safe */
1667 * This is crazily cautious: We make sure that not
1668 * only that we got an EOF (be it because we closed
1669 * the channel or because of a user's EOF), but verify
1670 * that it's one beyond the last buffer arrived, so
1671 * we have no leftover buffers pending before wrapping
1672 * up (which can only happen in asynchronous channels,
1677 spin_lock_irqsave(&channel
->wr_spinlock
,
1679 buf_idx
= channel
->wr_fpga_buf_idx
;
1680 eof
= channel
->wr_eof
;
1681 channel
->wr_sleepy
= 1;
1682 spin_unlock_irqrestore(&channel
->wr_spinlock
,
1686 * Check if eof points at the buffer after
1687 * the last one the FPGA submitted. Note that
1688 * no EOF is marked by negative eof.
1692 if (buf_idx
== channel
->num_wr_buffers
)
1699 * Steal extra 100 ms if awaken by interrupt.
1700 * This is a simple workaround for an
1701 * interrupt pending when entering, which would
1702 * otherwise result in declaring the hardware
1706 if (wait_event_interruptible(
1708 (!channel
->wr_sleepy
)))
1711 if (channel
->wr_sleepy
) {
1712 mutex_unlock(&channel
->wr_mutex
);
1713 dev_warn(channel
->endpoint
->dev
,
1714 "Hardware failed to respond to close command, therefore left in messy state.\n");
1720 mutex_unlock(&channel
->wr_mutex
);
1725 static loff_t
xillybus_llseek(struct file
*filp
, loff_t offset
, int whence
)
1727 struct xilly_channel
*channel
= filp
->private_data
;
1728 loff_t pos
= filp
->f_pos
;
1732 * Take both mutexes not allowing interrupts, since it seems like
1733 * common applications don't expect an -EINTR here. Besides, multiple
1734 * access to a single file descriptor on seekable devices is a mess
1738 if (channel
->endpoint
->fatal_error
)
1741 mutex_lock(&channel
->wr_mutex
);
1742 mutex_lock(&channel
->rd_mutex
);
1752 pos
= offset
; /* Going to the end => to the beginning */
1759 /* In any case, we must finish on an element boundary */
1760 if (pos
& ((1 << channel
->log2_element_size
) - 1)) {
1765 mutex_lock(&channel
->endpoint
->register_mutex
);
1767 iowrite32(pos
>> channel
->log2_element_size
,
1768 channel
->endpoint
->registers
+ fpga_buf_offset_reg
);
1770 iowrite32((channel
->chan_num
<< 1) |
1771 (6 << 24), /* Opcode 6, set address */
1772 channel
->endpoint
->registers
+ fpga_buf_ctrl_reg
);
1773 mmiowb(); /* Just to appear safe */
1775 mutex_unlock(&channel
->endpoint
->register_mutex
);
1778 mutex_unlock(&channel
->rd_mutex
);
1779 mutex_unlock(&channel
->wr_mutex
);
1781 if (rc
) /* Return error after releasing mutexes */
1787 * Since seekable devices are allowed only when the channel is
1788 * synchronous, we assume that there is no data pending in either
1789 * direction (which holds true as long as no concurrent access on the
1790 * file descriptor takes place).
1791 * The only thing we may need to throw away is leftovers from partial
1795 channel
->rd_leftovers
[3] = 0;
1800 static unsigned int xillybus_poll(struct file
*filp
, poll_table
*wait
)
1802 struct xilly_channel
*channel
= filp
->private_data
;
1803 unsigned int mask
= 0;
1804 unsigned long flags
;
1806 poll_wait(filp
, &channel
->endpoint
->ep_wait
, wait
);
1809 * poll() won't play ball regarding read() channels which
1810 * aren't asynchronous and support the nonempty message. Allowing
1811 * that will create situations where data has been delivered at
1812 * the FPGA, and users expecting select() to wake up, which it may
1816 if (!channel
->wr_synchronous
&& channel
->wr_supports_nonempty
) {
1817 poll_wait(filp
, &channel
->wr_wait
, wait
);
1818 poll_wait(filp
, &channel
->wr_ready_wait
, wait
);
1820 spin_lock_irqsave(&channel
->wr_spinlock
, flags
);
1821 if (!channel
->wr_empty
|| channel
->wr_ready
)
1822 mask
|= POLLIN
| POLLRDNORM
;
1824 if (channel
->wr_hangup
)
1826 * Not POLLHUP, because its behavior is in the
1827 * mist, and POLLIN does what we want: Wake up
1828 * the read file descriptor so it sees EOF.
1830 mask
|= POLLIN
| POLLRDNORM
;
1831 spin_unlock_irqrestore(&channel
->wr_spinlock
, flags
);
1835 * If partial data write is disallowed on a write() channel,
1836 * it's pointless to ever signal OK to write, because is could
1837 * block despite some space being available.
1840 if (channel
->rd_allow_partial
) {
1841 poll_wait(filp
, &channel
->rd_wait
, wait
);
1843 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1844 if (!channel
->rd_full
)
1845 mask
|= POLLOUT
| POLLWRNORM
;
1846 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1849 if (channel
->endpoint
->fatal_error
)
1855 static const struct file_operations xillybus_fops
= {
1856 .owner
= THIS_MODULE
,
1857 .read
= xillybus_read
,
1858 .write
= xillybus_write
,
1859 .open
= xillybus_open
,
1860 .flush
= xillybus_flush
,
1861 .release
= xillybus_release
,
1862 .llseek
= xillybus_llseek
,
1863 .poll
= xillybus_poll
,
1866 static int xillybus_init_chrdev(struct xilly_endpoint
*endpoint
,
1867 const unsigned char *idt
)
1871 int devnum
, i
, minor
, major
;
1873 struct device
*device
;
1875 rc
= alloc_chrdev_region(&dev
, 0, /* minor start */
1876 endpoint
->num_channels
,
1880 dev_warn(endpoint
->dev
, "Failed to obtain major/minors");
1884 endpoint
->major
= major
= MAJOR(dev
);
1885 endpoint
->lowest_minor
= minor
= MINOR(dev
);
1887 cdev_init(&endpoint
->cdev
, &xillybus_fops
);
1888 endpoint
->cdev
.owner
= endpoint
->ephw
->owner
;
1889 rc
= cdev_add(&endpoint
->cdev
, MKDEV(major
, minor
),
1890 endpoint
->num_channels
);
1892 dev_warn(endpoint
->dev
, "Failed to add cdev. Aborting.\n");
1898 for (i
= minor
, devnum
= 0;
1899 devnum
< endpoint
->num_channels
;
1901 snprintf(devname
, sizeof(devname
)-1, "xillybus_%s", idt
);
1903 devname
[sizeof(devname
)-1] = 0; /* Should never matter */
1908 device
= device_create(xillybus_class
,
1914 if (IS_ERR(device
)) {
1915 dev_warn(endpoint
->dev
,
1916 "Failed to create %s device. Aborting.\n",
1922 dev_info(endpoint
->dev
, "Created %d device files.\n",
1923 endpoint
->num_channels
);
1924 return 0; /* succeed */
1928 for (; devnum
>= 0; devnum
--, i
--)
1929 device_destroy(xillybus_class
, MKDEV(major
, i
));
1931 cdev_del(&endpoint
->cdev
);
1933 unregister_chrdev_region(MKDEV(major
, minor
), endpoint
->num_channels
);
1939 static void xillybus_cleanup_chrdev(struct xilly_endpoint
*endpoint
)
1943 for (minor
= endpoint
->lowest_minor
;
1944 minor
< (endpoint
->lowest_minor
+ endpoint
->num_channels
);
1946 device_destroy(xillybus_class
, MKDEV(endpoint
->major
, minor
));
1947 cdev_del(&endpoint
->cdev
);
1948 unregister_chrdev_region(MKDEV(endpoint
->major
,
1949 endpoint
->lowest_minor
),
1950 endpoint
->num_channels
);
1952 dev_info(endpoint
->dev
, "Removed %d device files.\n",
1953 endpoint
->num_channels
);
1957 struct xilly_endpoint
*xillybus_init_endpoint(struct pci_dev
*pdev
,
1959 struct xilly_endpoint_hardware
1962 struct xilly_endpoint
*endpoint
;
1964 endpoint
= devm_kzalloc(dev
, sizeof(*endpoint
), GFP_KERNEL
);
1968 endpoint
->pdev
= pdev
;
1969 endpoint
->dev
= dev
;
1970 endpoint
->ephw
= ephw
;
1971 endpoint
->msg_counter
= 0x0b;
1972 endpoint
->failed_messages
= 0;
1973 endpoint
->fatal_error
= 0;
1975 init_waitqueue_head(&endpoint
->ep_wait
);
1976 mutex_init(&endpoint
->register_mutex
);
1980 EXPORT_SYMBOL(xillybus_init_endpoint
);
1982 static int xilly_quiesce(struct xilly_endpoint
*endpoint
)
1984 endpoint
->idtlen
= -1;
1985 wmb(); /* Make sure idtlen is set before sending command */
1986 iowrite32((u32
) (endpoint
->dma_using_dac
& 0x0001),
1987 endpoint
->registers
+ fpga_dma_control_reg
);
1990 wait_event_interruptible_timeout(endpoint
->ep_wait
,
1991 (endpoint
->idtlen
>= 0),
1994 if (endpoint
->idtlen
< 0) {
1995 dev_err(endpoint
->dev
,
1996 "Failed to quiesce the device on exit.\n");
1999 return 0; /* Success */
2002 int xillybus_endpoint_discovery(struct xilly_endpoint
*endpoint
)
2006 void *bootstrap_resources
;
2007 int idtbuffersize
= (1 << PAGE_SHIFT
);
2008 struct device
*dev
= endpoint
->dev
;
2011 * The bogus IDT is used during bootstrap for allocating the initial
2012 * message buffer, and then the message buffer and space for the IDT
2013 * itself. The initial message buffer is of a single page's size, but
2014 * it's soon replaced with a more modest one (and memory is freed).
2017 unsigned char bogus_idt
[8] = { 1, 224, (PAGE_SHIFT
)-2, 0,
2018 3, 192, PAGE_SHIFT
, 0 };
2019 struct xilly_idt_handle idt_handle
;
2022 * Writing the value 0x00000001 to Endianness register signals which
2023 * endianness this processor is using, so the FPGA can swap words as
2027 iowrite32(1, endpoint
->registers
+ fpga_endian_reg
);
2028 mmiowb(); /* Writes below are affected by the one above. */
2030 /* Bootstrap phase I: Allocate temporary message buffer */
2032 bootstrap_resources
= devres_open_group(dev
, NULL
, GFP_KERNEL
);
2033 if (!bootstrap_resources
)
2036 endpoint
->num_channels
= 0;
2038 rc
= xilly_setupchannels(endpoint
, bogus_idt
, 1);
2043 /* Clear the message subsystem (and counter in particular) */
2044 iowrite32(0x04, endpoint
->registers
+ fpga_msg_ctrl_reg
);
2047 endpoint
->idtlen
= -1;
2052 * Set DMA 32/64 bit mode, quiesce the device (?!) and get IDT
2055 iowrite32((u32
) (endpoint
->dma_using_dac
& 0x0001),
2056 endpoint
->registers
+ fpga_dma_control_reg
);
2059 wait_event_interruptible_timeout(endpoint
->ep_wait
,
2060 (endpoint
->idtlen
>= 0),
2063 if (endpoint
->idtlen
< 0) {
2064 dev_err(endpoint
->dev
, "No response from FPGA. Aborting.\n");
2069 iowrite32((u32
) (0x0002 | (endpoint
->dma_using_dac
& 0x0001)),
2070 endpoint
->registers
+ fpga_dma_control_reg
);
2073 /* Bootstrap phase II: Allocate buffer for IDT and obtain it */
2074 while (endpoint
->idtlen
>= idtbuffersize
) {
2079 endpoint
->num_channels
= 1;
2081 rc
= xilly_setupchannels(endpoint
, bogus_idt
, 2);
2088 rc
= xilly_obtain_idt(endpoint
);
2093 xilly_scan_idt(endpoint
, &idt_handle
);
2095 if (!idt_handle
.chandesc
) {
2100 devres_close_group(dev
, bootstrap_resources
);
2102 /* Bootstrap phase III: Allocate buffers according to IDT */
2104 rc
= xilly_setupchannels(endpoint
,
2105 idt_handle
.chandesc
,
2106 idt_handle
.entries
);
2111 smp_wmb(); /* mutex_lock below should suffice, but won't hurt.*/
2114 * endpoint is now completely configured. We put it on the list
2115 * available to open() before registering the char device(s)
2118 mutex_lock(&ep_list_lock
);
2119 list_add_tail(&endpoint
->ep_list
, &list_of_endpoints
);
2120 mutex_unlock(&ep_list_lock
);
2122 rc
= xillybus_init_chrdev(endpoint
, idt_handle
.idt
);
2125 goto failed_chrdevs
;
2127 devres_release_group(dev
, bootstrap_resources
);
2132 mutex_lock(&ep_list_lock
);
2133 list_del(&endpoint
->ep_list
);
2134 mutex_unlock(&ep_list_lock
);
2137 xilly_quiesce(endpoint
);
2138 flush_workqueue(xillybus_wq
);
2142 EXPORT_SYMBOL(xillybus_endpoint_discovery
);
2144 void xillybus_endpoint_remove(struct xilly_endpoint
*endpoint
)
2146 xillybus_cleanup_chrdev(endpoint
);
2148 mutex_lock(&ep_list_lock
);
2149 list_del(&endpoint
->ep_list
);
2150 mutex_unlock(&ep_list_lock
);
2152 xilly_quiesce(endpoint
);
2155 * Flushing is done upon endpoint release to prevent access to memory
2156 * just about to be released. This makes the quiesce complete.
2158 flush_workqueue(xillybus_wq
);
2160 EXPORT_SYMBOL(xillybus_endpoint_remove
);
2162 static int __init
xillybus_init(void)
2166 mutex_init(&ep_list_lock
);
2168 xillybus_class
= class_create(THIS_MODULE
, xillyname
);
2169 if (IS_ERR(xillybus_class
)) {
2170 rc
= PTR_ERR(xillybus_class
);
2171 pr_warn("Failed to register class xillybus\n");
2176 xillybus_wq
= alloc_workqueue(xillyname
, 0, 0);
2178 class_destroy(xillybus_class
);
2185 static void __exit
xillybus_exit(void)
2187 /* flush_workqueue() was called for each endpoint released */
2188 destroy_workqueue(xillybus_wq
);
2190 class_destroy(xillybus_class
);
2193 module_init(xillybus_init
);
2194 module_exit(xillybus_exit
);