1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/misc/xillybus_core.c
5 * Copyright 2011 Xillybus Ltd, http://xillybus.com
7 * Driver for the Xillybus FPGA/host framework.
9 * This driver interfaces with a special IP core in an FPGA, setting up
10 * a pipe between a hardware FIFO in the programmable logic and a device
11 * file in the host. The number of such pipes and their attributes are
12 * set up on the logic. This driver detects these automatically and
13 * creates the device files accordingly.
16 #include <linux/list.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/interrupt.h>
22 #include <linux/sched.h>
24 #include <linux/cdev.h>
25 #include <linux/spinlock.h>
26 #include <linux/mutex.h>
27 #include <linux/crc32.h>
28 #include <linux/poll.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/workqueue.h>
34 MODULE_DESCRIPTION("Xillybus core functions");
35 MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
36 MODULE_VERSION("1.07");
37 MODULE_ALIAS("xillybus_core");
38 MODULE_LICENSE("GPL v2");
40 /* General timeout is 100 ms, rx timeout is 10 ms */
41 #define XILLY_RX_TIMEOUT (10*HZ/1000)
42 #define XILLY_TIMEOUT (100*HZ/1000)
44 #define fpga_msg_ctrl_reg 0x0008
45 #define fpga_dma_control_reg 0x0020
46 #define fpga_dma_bufno_reg 0x0024
47 #define fpga_dma_bufaddr_lowaddr_reg 0x0028
48 #define fpga_dma_bufaddr_highaddr_reg 0x002c
49 #define fpga_buf_ctrl_reg 0x0030
50 #define fpga_buf_offset_reg 0x0034
51 #define fpga_endian_reg 0x0040
53 #define XILLYMSG_OPCODE_RELEASEBUF 1
54 #define XILLYMSG_OPCODE_QUIESCEACK 2
55 #define XILLYMSG_OPCODE_FIFOEOF 3
56 #define XILLYMSG_OPCODE_FATAL_ERROR 4
57 #define XILLYMSG_OPCODE_NONEMPTY 5
59 static const char xillyname
[] = "xillybus";
61 static struct class *xillybus_class
;
64 * ep_list_lock is the last lock to be taken; No other lock requests are
65 * allowed while holding it. It merely protects list_of_endpoints, and not
66 * the endpoints listed in it.
69 static LIST_HEAD(list_of_endpoints
);
70 static struct mutex ep_list_lock
;
71 static struct workqueue_struct
*xillybus_wq
;
74 * Locking scheme: Mutexes protect invocations of character device methods.
75 * If both locks are taken, wr_mutex is taken first, rd_mutex second.
77 * wr_spinlock protects wr_*_buf_idx, wr_empty, wr_sleepy, wr_ready and the
78 * buffers' end_offset fields against changes made by IRQ handler (and in
79 * theory, other file request handlers, but the mutex handles that). Nothing
81 * They are held for short direct memory manipulations. Needless to say,
82 * no mutex locking is allowed when a spinlock is held.
84 * rd_spinlock does the same with rd_*_buf_idx, rd_empty and end_offset.
86 * register_mutex is endpoint-specific, and is held when non-atomic
87 * register operations are performed. wr_mutex and rd_mutex may be
88 * held when register_mutex is taken, but none of the spinlocks. Note that
89 * register_mutex doesn't protect against sporadic buf_ctrl_reg writes
90 * which are unrelated to buf_offset_reg, since they are harmless.
92 * Blocking on the wait queues is allowed with mutexes held, but not with
95 * Only interruptible blocking is allowed on mutexes and wait queues.
97 * All in all, the locking order goes (with skips allowed, of course):
98 * wr_mutex -> rd_mutex -> register_mutex -> wr_spinlock -> rd_spinlock
101 static void malformed_message(struct xilly_endpoint
*endpoint
, u32
*buf
)
104 int msg_channel
, msg_bufno
, msg_data
, msg_dir
;
106 opcode
= (buf
[0] >> 24) & 0xff;
107 msg_dir
= buf
[0] & 1;
108 msg_channel
= (buf
[0] >> 1) & 0x7ff;
109 msg_bufno
= (buf
[0] >> 12) & 0x3ff;
110 msg_data
= buf
[1] & 0xfffffff;
112 dev_warn(endpoint
->dev
,
113 "Malformed message (skipping): opcode=%d, channel=%03x, dir=%d, bufno=%03x, data=%07x\n",
114 opcode
, msg_channel
, msg_dir
, msg_bufno
, msg_data
);
118 * xillybus_isr assumes the interrupt is allocated exclusively to it,
119 * which is the natural case MSI and several other hardware-oriented
120 * interrupts. Sharing is not allowed.
123 irqreturn_t
xillybus_isr(int irq
, void *data
)
125 struct xilly_endpoint
*ep
= data
;
127 unsigned int buf_size
;
130 unsigned int msg_channel
, msg_bufno
, msg_data
, msg_dir
;
131 struct xilly_channel
*channel
;
133 buf
= ep
->msgbuf_addr
;
134 buf_size
= ep
->msg_buf_size
/sizeof(u32
);
136 ep
->ephw
->hw_sync_sgl_for_cpu(ep
,
141 for (i
= 0; i
< buf_size
; i
+= 2) {
142 if (((buf
[i
+1] >> 28) & 0xf) != ep
->msg_counter
) {
143 malformed_message(ep
, &buf
[i
]);
145 "Sending a NACK on counter %x (instead of %x) on entry %d\n",
146 ((buf
[i
+1] >> 28) & 0xf),
150 if (++ep
->failed_messages
> 10) {
152 "Lost sync with interrupt messages. Stopping.\n");
154 ep
->ephw
->hw_sync_sgl_for_device(
160 iowrite32(0x01, /* Message NACK */
161 ep
->registers
+ fpga_msg_ctrl_reg
);
164 } else if (buf
[i
] & (1 << 22)) /* Last message */
169 dev_err(ep
->dev
, "Bad interrupt message. Stopping.\n");
175 for (i
= 0; i
< buf_size
; i
+= 2) { /* Scan through messages */
176 opcode
= (buf
[i
] >> 24) & 0xff;
178 msg_dir
= buf
[i
] & 1;
179 msg_channel
= (buf
[i
] >> 1) & 0x7ff;
180 msg_bufno
= (buf
[i
] >> 12) & 0x3ff;
181 msg_data
= buf
[i
+1] & 0xfffffff;
184 case XILLYMSG_OPCODE_RELEASEBUF
:
185 if ((msg_channel
> ep
->num_channels
) ||
186 (msg_channel
== 0)) {
187 malformed_message(ep
, &buf
[i
]);
191 channel
= ep
->channels
[msg_channel
];
193 if (msg_dir
) { /* Write channel */
194 if (msg_bufno
>= channel
->num_wr_buffers
) {
195 malformed_message(ep
, &buf
[i
]);
198 spin_lock(&channel
->wr_spinlock
);
199 channel
->wr_buffers
[msg_bufno
]->end_offset
=
201 channel
->wr_fpga_buf_idx
= msg_bufno
;
202 channel
->wr_empty
= 0;
203 channel
->wr_sleepy
= 0;
204 spin_unlock(&channel
->wr_spinlock
);
206 wake_up_interruptible(&channel
->wr_wait
);
211 if (msg_bufno
>= channel
->num_rd_buffers
) {
212 malformed_message(ep
, &buf
[i
]);
216 spin_lock(&channel
->rd_spinlock
);
217 channel
->rd_fpga_buf_idx
= msg_bufno
;
218 channel
->rd_full
= 0;
219 spin_unlock(&channel
->rd_spinlock
);
221 wake_up_interruptible(&channel
->rd_wait
);
222 if (!channel
->rd_synchronous
)
225 &channel
->rd_workitem
,
230 case XILLYMSG_OPCODE_NONEMPTY
:
231 if ((msg_channel
> ep
->num_channels
) ||
232 (msg_channel
== 0) || (!msg_dir
) ||
233 !ep
->channels
[msg_channel
]->wr_supports_nonempty
) {
234 malformed_message(ep
, &buf
[i
]);
238 channel
= ep
->channels
[msg_channel
];
240 if (msg_bufno
>= channel
->num_wr_buffers
) {
241 malformed_message(ep
, &buf
[i
]);
244 spin_lock(&channel
->wr_spinlock
);
245 if (msg_bufno
== channel
->wr_host_buf_idx
)
246 channel
->wr_ready
= 1;
247 spin_unlock(&channel
->wr_spinlock
);
249 wake_up_interruptible(&channel
->wr_ready_wait
);
252 case XILLYMSG_OPCODE_QUIESCEACK
:
253 ep
->idtlen
= msg_data
;
254 wake_up_interruptible(&ep
->ep_wait
);
257 case XILLYMSG_OPCODE_FIFOEOF
:
258 if ((msg_channel
> ep
->num_channels
) ||
259 (msg_channel
== 0) || (!msg_dir
) ||
260 !ep
->channels
[msg_channel
]->num_wr_buffers
) {
261 malformed_message(ep
, &buf
[i
]);
264 channel
= ep
->channels
[msg_channel
];
265 spin_lock(&channel
->wr_spinlock
);
266 channel
->wr_eof
= msg_bufno
;
267 channel
->wr_sleepy
= 0;
269 channel
->wr_hangup
= channel
->wr_empty
&&
270 (channel
->wr_host_buf_idx
== msg_bufno
);
272 spin_unlock(&channel
->wr_spinlock
);
274 wake_up_interruptible(&channel
->wr_wait
);
277 case XILLYMSG_OPCODE_FATAL_ERROR
:
279 wake_up_interruptible(&ep
->ep_wait
); /* For select() */
281 "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");
284 malformed_message(ep
, &buf
[i
]);
289 ep
->ephw
->hw_sync_sgl_for_device(ep
,
294 ep
->msg_counter
= (ep
->msg_counter
+ 1) & 0xf;
295 ep
->failed_messages
= 0;
296 iowrite32(0x03, ep
->registers
+ fpga_msg_ctrl_reg
); /* Message ACK */
300 EXPORT_SYMBOL(xillybus_isr
);
303 * A few trivial memory management functions.
304 * NOTE: These functions are used only on probe and remove, and therefore
305 * no locks are applied!
308 static void xillybus_autoflush(struct work_struct
*work
);
310 struct xilly_alloc_state
{
314 enum dma_data_direction direction
;
318 static int xilly_get_dma_buffers(struct xilly_endpoint
*ep
,
319 struct xilly_alloc_state
*s
,
320 struct xilly_buffer
**buffers
,
321 int bufnum
, int bytebufsize
)
325 struct device
*dev
= ep
->dev
;
326 struct xilly_buffer
*this_buffer
= NULL
; /* Init to silence warning */
328 if (buffers
) { /* Not the message buffer */
329 this_buffer
= devm_kcalloc(dev
, bufnum
,
330 sizeof(struct xilly_buffer
),
336 for (i
= 0; i
< bufnum
; i
++) {
338 * Buffers are expected in descending size order, so there
339 * is either enough space for this buffer or none at all.
342 if ((s
->left_of_salami
< bytebufsize
) &&
343 (s
->left_of_salami
> 0)) {
345 "Corrupt buffer allocation in IDT. Aborting.\n");
349 if (s
->left_of_salami
== 0) {
350 int allocorder
, allocsize
;
352 allocsize
= PAGE_SIZE
;
354 while (bytebufsize
> allocsize
) {
359 s
->salami
= (void *) devm_get_free_pages(
361 GFP_KERNEL
| __GFP_DMA32
| __GFP_ZERO
,
366 s
->left_of_salami
= allocsize
;
369 rc
= ep
->ephw
->map_single(ep
, s
->salami
,
370 bytebufsize
, s
->direction
,
375 iowrite32((u32
) (dma_addr
& 0xffffffff),
376 ep
->registers
+ fpga_dma_bufaddr_lowaddr_reg
);
377 iowrite32(((u32
) ((((u64
) dma_addr
) >> 32) & 0xffffffff)),
378 ep
->registers
+ fpga_dma_bufaddr_highaddr_reg
);
380 if (buffers
) { /* Not the message buffer */
381 this_buffer
->addr
= s
->salami
;
382 this_buffer
->dma_addr
= dma_addr
;
383 buffers
[i
] = this_buffer
++;
385 iowrite32(s
->regdirection
| s
->nbuffer
++,
386 ep
->registers
+ fpga_dma_bufno_reg
);
388 ep
->msgbuf_addr
= s
->salami
;
389 ep
->msgbuf_dma_addr
= dma_addr
;
390 ep
->msg_buf_size
= bytebufsize
;
392 iowrite32(s
->regdirection
,
393 ep
->registers
+ fpga_dma_bufno_reg
);
396 s
->left_of_salami
-= bytebufsize
;
397 s
->salami
+= bytebufsize
;
402 static int xilly_setupchannels(struct xilly_endpoint
*ep
,
403 unsigned char *chandesc
,
406 struct device
*dev
= ep
->dev
;
408 struct xilly_channel
*channel
;
409 int channelnum
, bufnum
, bufsize
, format
, is_writebuf
;
411 int synchronous
, allowpartial
, exclusive_open
, seekable
;
412 int supports_nonempty
;
413 int msg_buf_done
= 0;
415 struct xilly_alloc_state rd_alloc
= {
419 .direction
= DMA_TO_DEVICE
,
423 struct xilly_alloc_state wr_alloc
= {
427 .direction
= DMA_FROM_DEVICE
,
428 .regdirection
= 0x80000000,
431 channel
= devm_kcalloc(dev
, ep
->num_channels
,
432 sizeof(struct xilly_channel
), GFP_KERNEL
);
436 ep
->channels
= devm_kcalloc(dev
, ep
->num_channels
+ 1,
437 sizeof(struct xilly_channel
*),
442 ep
->channels
[0] = NULL
; /* Channel 0 is message buf. */
444 /* Initialize all channels with defaults */
446 for (i
= 1; i
<= ep
->num_channels
; i
++) {
447 channel
->wr_buffers
= NULL
;
448 channel
->rd_buffers
= NULL
;
449 channel
->num_wr_buffers
= 0;
450 channel
->num_rd_buffers
= 0;
451 channel
->wr_fpga_buf_idx
= -1;
452 channel
->wr_host_buf_idx
= 0;
453 channel
->wr_host_buf_pos
= 0;
454 channel
->wr_empty
= 1;
455 channel
->wr_ready
= 0;
456 channel
->wr_sleepy
= 1;
457 channel
->rd_fpga_buf_idx
= 0;
458 channel
->rd_host_buf_idx
= 0;
459 channel
->rd_host_buf_pos
= 0;
460 channel
->rd_full
= 0;
461 channel
->wr_ref_count
= 0;
462 channel
->rd_ref_count
= 0;
464 spin_lock_init(&channel
->wr_spinlock
);
465 spin_lock_init(&channel
->rd_spinlock
);
466 mutex_init(&channel
->wr_mutex
);
467 mutex_init(&channel
->rd_mutex
);
468 init_waitqueue_head(&channel
->rd_wait
);
469 init_waitqueue_head(&channel
->wr_wait
);
470 init_waitqueue_head(&channel
->wr_ready_wait
);
472 INIT_DELAYED_WORK(&channel
->rd_workitem
, xillybus_autoflush
);
474 channel
->endpoint
= ep
;
475 channel
->chan_num
= i
;
477 channel
->log2_element_size
= 0;
479 ep
->channels
[i
] = channel
++;
482 for (entry
= 0; entry
< entries
; entry
++, chandesc
+= 4) {
483 struct xilly_buffer
**buffers
= NULL
;
485 is_writebuf
= chandesc
[0] & 0x01;
486 channelnum
= (chandesc
[0] >> 1) | ((chandesc
[1] & 0x0f) << 7);
487 format
= (chandesc
[1] >> 4) & 0x03;
488 allowpartial
= (chandesc
[1] >> 6) & 0x01;
489 synchronous
= (chandesc
[1] >> 7) & 0x01;
490 bufsize
= 1 << (chandesc
[2] & 0x1f);
491 bufnum
= 1 << (chandesc
[3] & 0x0f);
492 exclusive_open
= (chandesc
[2] >> 7) & 0x01;
493 seekable
= (chandesc
[2] >> 6) & 0x01;
494 supports_nonempty
= (chandesc
[2] >> 5) & 0x01;
496 if ((channelnum
> ep
->num_channels
) ||
497 ((channelnum
== 0) && !is_writebuf
)) {
499 "IDT requests channel out of range. Aborting.\n");
503 channel
= ep
->channels
[channelnum
]; /* NULL for msg channel */
505 if (!is_writebuf
|| channelnum
> 0) {
506 channel
->log2_element_size
= ((format
> 2) ?
509 bytebufsize
= bufsize
*
510 (1 << channel
->log2_element_size
);
512 buffers
= devm_kcalloc(dev
, bufnum
,
513 sizeof(struct xilly_buffer
*),
518 bytebufsize
= bufsize
<< 2;
522 channel
->num_rd_buffers
= bufnum
;
523 channel
->rd_buf_size
= bytebufsize
;
524 channel
->rd_allow_partial
= allowpartial
;
525 channel
->rd_synchronous
= synchronous
;
526 channel
->rd_exclusive_open
= exclusive_open
;
527 channel
->seekable
= seekable
;
529 channel
->rd_buffers
= buffers
;
530 rc
= xilly_get_dma_buffers(ep
, &rd_alloc
, buffers
,
531 bufnum
, bytebufsize
);
532 } else if (channelnum
> 0) {
533 channel
->num_wr_buffers
= bufnum
;
534 channel
->wr_buf_size
= bytebufsize
;
536 channel
->seekable
= seekable
;
537 channel
->wr_supports_nonempty
= supports_nonempty
;
539 channel
->wr_allow_partial
= allowpartial
;
540 channel
->wr_synchronous
= synchronous
;
541 channel
->wr_exclusive_open
= exclusive_open
;
543 channel
->wr_buffers
= buffers
;
544 rc
= xilly_get_dma_buffers(ep
, &wr_alloc
, buffers
,
545 bufnum
, bytebufsize
);
547 rc
= xilly_get_dma_buffers(ep
, &wr_alloc
, NULL
,
548 bufnum
, bytebufsize
);
558 "Corrupt IDT: No message buffer. Aborting.\n");
564 static int xilly_scan_idt(struct xilly_endpoint
*endpoint
,
565 struct xilly_idt_handle
*idt_handle
)
568 unsigned char *idt
= endpoint
->channels
[1]->wr_buffers
[0]->addr
;
569 unsigned char *end_of_idt
= idt
+ endpoint
->idtlen
- 4;
574 idt_handle
->idt
= idt
;
576 scan
++; /* Skip version number */
578 while ((scan
<= end_of_idt
) && *scan
) {
579 while ((scan
<= end_of_idt
) && *scan
++)
580 /* Do nothing, just scan thru string */;
586 if (scan
> end_of_idt
) {
587 dev_err(endpoint
->dev
,
588 "IDT device name list overflow. Aborting.\n");
591 idt_handle
->chandesc
= scan
;
593 len
= endpoint
->idtlen
- (3 + ((int) (scan
- idt
)));
596 dev_err(endpoint
->dev
,
597 "Corrupt IDT device name list. Aborting.\n");
601 idt_handle
->entries
= len
>> 2;
602 endpoint
->num_channels
= count
;
607 static int xilly_obtain_idt(struct xilly_endpoint
*endpoint
)
609 struct xilly_channel
*channel
;
610 unsigned char *version
;
613 channel
= endpoint
->channels
[1]; /* This should be generated ad-hoc */
615 channel
->wr_sleepy
= 1;
618 (3 << 24), /* Opcode 3 for channel 0 = Send IDT */
619 endpoint
->registers
+ fpga_buf_ctrl_reg
);
621 t
= wait_event_interruptible_timeout(channel
->wr_wait
,
622 (!channel
->wr_sleepy
),
626 dev_err(endpoint
->dev
, "Failed to obtain IDT. Aborting.\n");
628 if (endpoint
->fatal_error
)
634 endpoint
->ephw
->hw_sync_sgl_for_cpu(
636 channel
->wr_buffers
[0]->dma_addr
,
637 channel
->wr_buf_size
,
640 if (channel
->wr_buffers
[0]->end_offset
!= endpoint
->idtlen
) {
641 dev_err(endpoint
->dev
,
642 "IDT length mismatch (%d != %d). Aborting.\n",
643 channel
->wr_buffers
[0]->end_offset
, endpoint
->idtlen
);
647 if (crc32_le(~0, channel
->wr_buffers
[0]->addr
,
648 endpoint
->idtlen
+1) != 0) {
649 dev_err(endpoint
->dev
, "IDT failed CRC check. Aborting.\n");
653 version
= channel
->wr_buffers
[0]->addr
;
655 /* Check version number. Reject anything above 0x82. */
656 if (*version
> 0x82) {
657 dev_err(endpoint
->dev
,
658 "No support for IDT version 0x%02x. Maybe the xillybus driver needs an upgrade. Aborting.\n",
666 static ssize_t
xillybus_read(struct file
*filp
, char __user
*userbuf
,
667 size_t count
, loff_t
*f_pos
)
672 int no_time_left
= 0;
673 long deadline
, left_to_sleep
;
674 struct xilly_channel
*channel
= filp
->private_data
;
676 int empty
, reached_eof
, exhausted
, ready
;
677 /* Initializations are there only to silence warnings */
679 int howmany
= 0, bufpos
= 0, bufidx
= 0, bufferdone
= 0;
682 if (channel
->endpoint
->fatal_error
)
685 deadline
= jiffies
+ 1 + XILLY_RX_TIMEOUT
;
687 rc
= mutex_lock_interruptible(&channel
->wr_mutex
);
691 while (1) { /* Note that we may drop mutex within this loop */
692 int bytes_to_do
= count
- bytes_done
;
694 spin_lock_irqsave(&channel
->wr_spinlock
, flags
);
696 empty
= channel
->wr_empty
;
697 ready
= !empty
|| channel
->wr_ready
;
700 bufidx
= channel
->wr_host_buf_idx
;
701 bufpos
= channel
->wr_host_buf_pos
;
702 howmany
= ((channel
->wr_buffers
[bufidx
]->end_offset
703 + 1) << channel
->log2_element_size
)
706 /* Update wr_host_* to its post-operation state */
707 if (howmany
> bytes_to_do
) {
710 howmany
= bytes_to_do
;
711 channel
->wr_host_buf_pos
+= howmany
;
715 channel
->wr_host_buf_pos
= 0;
717 if (bufidx
== channel
->wr_fpga_buf_idx
) {
718 channel
->wr_empty
= 1;
719 channel
->wr_sleepy
= 1;
720 channel
->wr_ready
= 0;
723 if (bufidx
>= (channel
->num_wr_buffers
- 1))
724 channel
->wr_host_buf_idx
= 0;
726 channel
->wr_host_buf_idx
++;
731 * Marking our situation after the possible changes above,
732 * for use after releasing the spinlock.
734 * empty = empty before change
735 * exhasted = empty after possible change
738 reached_eof
= channel
->wr_empty
&&
739 (channel
->wr_host_buf_idx
== channel
->wr_eof
);
740 channel
->wr_hangup
= reached_eof
;
741 exhausted
= channel
->wr_empty
;
742 waiting_bufidx
= channel
->wr_host_buf_idx
;
744 spin_unlock_irqrestore(&channel
->wr_spinlock
, flags
);
746 if (!empty
) { /* Go on, now without the spinlock */
748 if (bufpos
== 0) /* Position zero means it's virgin */
749 channel
->endpoint
->ephw
->hw_sync_sgl_for_cpu(
751 channel
->wr_buffers
[bufidx
]->dma_addr
,
752 channel
->wr_buf_size
,
757 channel
->wr_buffers
[bufidx
]->addr
762 bytes_done
+= howmany
;
765 channel
->endpoint
->ephw
->hw_sync_sgl_for_device(
767 channel
->wr_buffers
[bufidx
]->dma_addr
,
768 channel
->wr_buf_size
,
772 * Tell FPGA the buffer is done with. It's an
773 * atomic operation to the FPGA, so what
774 * happens with other channels doesn't matter,
775 * and the certain channel is protected with
776 * the channel-specific mutex.
779 iowrite32(1 | (channel
->chan_num
<< 1) |
781 channel
->endpoint
->registers
+
786 mutex_unlock(&channel
->wr_mutex
);
791 /* This includes a zero-count return = EOF */
792 if ((bytes_done
>= count
) || reached_eof
)
796 continue; /* More in RAM buffer(s)? Just go on. */
798 if ((bytes_done
> 0) &&
800 (channel
->wr_synchronous
&& channel
->wr_allow_partial
)))
804 * Nonblocking read: The "ready" flag tells us that the FPGA
805 * has data to send. In non-blocking mode, if it isn't on,
806 * just return. But if there is, we jump directly to the point
807 * where we ask for the FPGA to send all it has, and wait
808 * until that data arrives. So in a sense, we *do* block in
809 * nonblocking mode, but only for a very short time.
812 if (!no_time_left
&& (filp
->f_flags
& O_NONBLOCK
)) {
823 if (!no_time_left
|| (bytes_done
> 0)) {
825 * Note that in case of an element-misaligned read
826 * request, offsetlimit will include the last element,
827 * which will be partially read from.
829 int offsetlimit
= ((count
- bytes_done
) - 1) >>
830 channel
->log2_element_size
;
831 int buf_elements
= channel
->wr_buf_size
>>
832 channel
->log2_element_size
;
835 * In synchronous mode, always send an offset limit.
836 * Just don't send a value too big.
839 if (channel
->wr_synchronous
) {
840 /* Don't request more than one buffer */
841 if (channel
->wr_allow_partial
&&
842 (offsetlimit
>= buf_elements
))
843 offsetlimit
= buf_elements
- 1;
845 /* Don't request more than all buffers */
846 if (!channel
->wr_allow_partial
&&
848 (buf_elements
* channel
->num_wr_buffers
)))
849 offsetlimit
= buf_elements
*
850 channel
->num_wr_buffers
- 1;
854 * In asynchronous mode, force early flush of a buffer
855 * only if that will allow returning a full count. The
856 * "offsetlimit < ( ... )" rather than "<=" excludes
857 * requesting a full buffer, which would obviously
858 * cause a buffer transmission anyhow
861 if (channel
->wr_synchronous
||
862 (offsetlimit
< (buf_elements
- 1))) {
863 mutex_lock(&channel
->endpoint
->register_mutex
);
865 iowrite32(offsetlimit
,
866 channel
->endpoint
->registers
+
867 fpga_buf_offset_reg
);
869 iowrite32(1 | (channel
->chan_num
<< 1) |
870 (2 << 24) | /* 2 = offset limit */
871 (waiting_bufidx
<< 12),
872 channel
->endpoint
->registers
+
875 mutex_unlock(&channel
->endpoint
->
881 * If partial completion is disallowed, there is no point in
882 * timeout sleeping. Neither if no_time_left is set and
886 if (!channel
->wr_allow_partial
||
887 (no_time_left
&& (bytes_done
== 0))) {
889 * This do-loop will run more than once if another
890 * thread reasserted wr_sleepy before we got the mutex
891 * back, so we try again.
895 mutex_unlock(&channel
->wr_mutex
);
897 if (wait_event_interruptible(
899 (!channel
->wr_sleepy
)))
902 if (mutex_lock_interruptible(
905 } while (channel
->wr_sleepy
);
909 interrupted
: /* Mutex is not held if got here */
910 if (channel
->endpoint
->fatal_error
)
914 if (filp
->f_flags
& O_NONBLOCK
)
915 return -EAGAIN
; /* Don't admit snoozing */
919 left_to_sleep
= deadline
- ((long) jiffies
);
922 * If our time is out, skip the waiting. We may miss wr_sleepy
923 * being deasserted but hey, almost missing the train is like
927 if (left_to_sleep
> 0) {
929 wait_event_interruptible_timeout(
931 (!channel
->wr_sleepy
),
934 if (left_to_sleep
> 0) /* wr_sleepy deasserted */
937 if (left_to_sleep
< 0) { /* Interrupt */
938 mutex_unlock(&channel
->wr_mutex
);
939 if (channel
->endpoint
->fatal_error
)
948 no_time_left
= 1; /* We're out of sleeping time. Desperate! */
950 if (bytes_done
== 0) {
952 * Reaching here means that we allow partial return,
953 * that we've run out of time, and that we have
955 * So tell the FPGA to send anything it has or gets.
958 iowrite32(1 | (channel
->chan_num
<< 1) |
959 (3 << 24) | /* Opcode 3, flush it all! */
960 (waiting_bufidx
<< 12),
961 channel
->endpoint
->registers
+
966 * Reaching here means that we *do* have data in the buffer,
967 * but the "partial" flag disallows returning less than
968 * required. And we don't have as much. So loop again,
969 * which is likely to end up blocking indefinitely until
970 * enough data has arrived.
974 mutex_unlock(&channel
->wr_mutex
);
976 if (channel
->endpoint
->fatal_error
)
986 * The timeout argument takes values as follows:
987 * >0 : Flush with timeout
988 * ==0 : Flush, and wait idefinitely for the flush to complete
989 * <0 : Autoflush: Flush only if there's a single buffer occupied
992 static int xillybus_myflush(struct xilly_channel
*channel
, long timeout
)
997 int end_offset_plus1
;
998 int bufidx
, bufidx_minus1
;
1001 int new_rd_host_buf_pos
;
1003 if (channel
->endpoint
->fatal_error
)
1005 rc
= mutex_lock_interruptible(&channel
->rd_mutex
);
1010 * Don't flush a closed channel. This can happen when the work queued
1011 * autoflush thread fires off after the file has closed. This is not
1012 * an error, just something to dismiss.
1015 if (!channel
->rd_ref_count
)
1018 bufidx
= channel
->rd_host_buf_idx
;
1020 bufidx_minus1
= (bufidx
== 0) ?
1021 channel
->num_rd_buffers
- 1 :
1024 end_offset_plus1
= channel
->rd_host_buf_pos
>>
1025 channel
->log2_element_size
;
1027 new_rd_host_buf_pos
= channel
->rd_host_buf_pos
-
1028 (end_offset_plus1
<< channel
->log2_element_size
);
1030 /* Submit the current buffer if it's nonempty */
1031 if (end_offset_plus1
) {
1032 unsigned char *tail
= channel
->rd_buffers
[bufidx
]->addr
+
1033 (end_offset_plus1
<< channel
->log2_element_size
);
1035 /* Copy unflushed data, so we can put it in next buffer */
1036 for (i
= 0; i
< new_rd_host_buf_pos
; i
++)
1037 channel
->rd_leftovers
[i
] = *tail
++;
1039 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1041 /* Autoflush only if a single buffer is occupied */
1043 if ((timeout
< 0) &&
1044 (channel
->rd_full
||
1045 (bufidx_minus1
!= channel
->rd_fpga_buf_idx
))) {
1046 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1048 * A new work item may be queued by the ISR exactly
1049 * now, since the execution of a work item allows the
1050 * queuing of a new one while it's running.
1055 /* The 4th element is never needed for data, so it's a flag */
1056 channel
->rd_leftovers
[3] = (new_rd_host_buf_pos
!= 0);
1058 /* Set up rd_full to reflect a certain moment's state */
1060 if (bufidx
== channel
->rd_fpga_buf_idx
)
1061 channel
->rd_full
= 1;
1062 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1064 if (bufidx
>= (channel
->num_rd_buffers
- 1))
1065 channel
->rd_host_buf_idx
= 0;
1067 channel
->rd_host_buf_idx
++;
1069 channel
->endpoint
->ephw
->hw_sync_sgl_for_device(
1071 channel
->rd_buffers
[bufidx
]->dma_addr
,
1072 channel
->rd_buf_size
,
1075 mutex_lock(&channel
->endpoint
->register_mutex
);
1077 iowrite32(end_offset_plus1
- 1,
1078 channel
->endpoint
->registers
+ fpga_buf_offset_reg
);
1080 iowrite32((channel
->chan_num
<< 1) | /* Channel ID */
1081 (2 << 24) | /* Opcode 2, submit buffer */
1083 channel
->endpoint
->registers
+ fpga_buf_ctrl_reg
);
1085 mutex_unlock(&channel
->endpoint
->register_mutex
);
1086 } else if (bufidx
== 0) {
1087 bufidx
= channel
->num_rd_buffers
- 1;
1092 channel
->rd_host_buf_pos
= new_rd_host_buf_pos
;
1095 goto done
; /* Autoflush */
1098 * bufidx is now the last buffer written to (or equal to
1099 * rd_fpga_buf_idx if buffer was never written to), and
1100 * channel->rd_host_buf_idx the one after it.
1102 * If bufidx == channel->rd_fpga_buf_idx we're either empty or full.
1105 while (1) { /* Loop waiting for draining of buffers */
1106 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1108 if (bufidx
!= channel
->rd_fpga_buf_idx
)
1109 channel
->rd_full
= 1; /*
1111 * but needs waiting.
1114 empty
= !channel
->rd_full
;
1116 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1122 * Indefinite sleep with mutex taken. With data waiting for
1123 * flushing user should not be surprised if open() for write
1127 wait_event_interruptible(channel
->rd_wait
,
1128 (!channel
->rd_full
));
1130 else if (wait_event_interruptible_timeout(
1132 (!channel
->rd_full
),
1134 dev_warn(channel
->endpoint
->dev
,
1135 "Timed out while flushing. Output data may be lost.\n");
1141 if (channel
->rd_full
) {
1148 mutex_unlock(&channel
->rd_mutex
);
1150 if (channel
->endpoint
->fatal_error
)
1156 static int xillybus_flush(struct file
*filp
, fl_owner_t id
)
1158 if (!(filp
->f_mode
& FMODE_WRITE
))
1161 return xillybus_myflush(filp
->private_data
, HZ
); /* 1 second timeout */
1164 static void xillybus_autoflush(struct work_struct
*work
)
1166 struct delayed_work
*workitem
= container_of(
1167 work
, struct delayed_work
, work
);
1168 struct xilly_channel
*channel
= container_of(
1169 workitem
, struct xilly_channel
, rd_workitem
);
1172 rc
= xillybus_myflush(channel
, -1);
1174 dev_warn(channel
->endpoint
->dev
,
1175 "Autoflush failed because work queue thread got a signal.\n");
1177 dev_err(channel
->endpoint
->dev
,
1178 "Autoflush failed under weird circumstances.\n");
1181 static ssize_t
xillybus_write(struct file
*filp
, const char __user
*userbuf
,
1182 size_t count
, loff_t
*f_pos
)
1185 unsigned long flags
;
1187 struct xilly_channel
*channel
= filp
->private_data
;
1189 int full
, exhausted
;
1190 /* Initializations are there only to silence warnings */
1192 int howmany
= 0, bufpos
= 0, bufidx
= 0, bufferdone
= 0;
1193 int end_offset_plus1
= 0;
1195 if (channel
->endpoint
->fatal_error
)
1198 rc
= mutex_lock_interruptible(&channel
->rd_mutex
);
1203 int bytes_to_do
= count
- bytes_done
;
1205 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1207 full
= channel
->rd_full
;
1210 bufidx
= channel
->rd_host_buf_idx
;
1211 bufpos
= channel
->rd_host_buf_pos
;
1212 howmany
= channel
->rd_buf_size
- bufpos
;
1215 * Update rd_host_* to its state after this operation.
1216 * count=0 means committing the buffer immediately,
1217 * which is like flushing, but not necessarily block.
1220 if ((howmany
> bytes_to_do
) &&
1222 ((bufpos
>> channel
->log2_element_size
) == 0))) {
1225 howmany
= bytes_to_do
;
1226 channel
->rd_host_buf_pos
+= howmany
;
1232 channel
->rd_buf_size
>>
1233 channel
->log2_element_size
;
1234 channel
->rd_host_buf_pos
= 0;
1236 unsigned char *tail
;
1241 end_offset_plus1
= bufpos
>>
1242 channel
->log2_element_size
;
1244 channel
->rd_host_buf_pos
-=
1246 channel
->log2_element_size
;
1249 rd_buffers
[bufidx
]->addr
+
1250 (end_offset_plus1
<<
1251 channel
->log2_element_size
);
1254 i
< channel
->rd_host_buf_pos
;
1256 channel
->rd_leftovers
[i
] =
1260 if (bufidx
== channel
->rd_fpga_buf_idx
)
1261 channel
->rd_full
= 1;
1263 if (bufidx
>= (channel
->num_rd_buffers
- 1))
1264 channel
->rd_host_buf_idx
= 0;
1266 channel
->rd_host_buf_idx
++;
1271 * Marking our situation after the possible changes above,
1272 * for use after releasing the spinlock.
1274 * full = full before change
1275 * exhasted = full after possible change
1278 exhausted
= channel
->rd_full
;
1280 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1282 if (!full
) { /* Go on, now without the spinlock */
1283 unsigned char *head
=
1284 channel
->rd_buffers
[bufidx
]->addr
;
1287 if ((bufpos
== 0) || /* Zero means it's virgin */
1288 (channel
->rd_leftovers
[3] != 0)) {
1289 channel
->endpoint
->ephw
->hw_sync_sgl_for_cpu(
1291 channel
->rd_buffers
[bufidx
]->dma_addr
,
1292 channel
->rd_buf_size
,
1295 /* Virgin, but leftovers are due */
1296 for (i
= 0; i
< bufpos
; i
++)
1297 *head
++ = channel
->rd_leftovers
[i
];
1299 channel
->rd_leftovers
[3] = 0; /* Clear flag */
1303 channel
->rd_buffers
[bufidx
]->addr
+ bufpos
,
1308 bytes_done
+= howmany
;
1311 channel
->endpoint
->ephw
->hw_sync_sgl_for_device(
1313 channel
->rd_buffers
[bufidx
]->dma_addr
,
1314 channel
->rd_buf_size
,
1317 mutex_lock(&channel
->endpoint
->register_mutex
);
1319 iowrite32(end_offset_plus1
- 1,
1320 channel
->endpoint
->registers
+
1321 fpga_buf_offset_reg
);
1323 iowrite32((channel
->chan_num
<< 1) |
1324 (2 << 24) | /* 2 = submit buffer */
1326 channel
->endpoint
->registers
+
1329 mutex_unlock(&channel
->endpoint
->
1332 channel
->rd_leftovers
[3] =
1333 (channel
->rd_host_buf_pos
!= 0);
1337 mutex_unlock(&channel
->rd_mutex
);
1339 if (channel
->endpoint
->fatal_error
)
1342 if (!channel
->rd_synchronous
)
1345 &channel
->rd_workitem
,
1352 if (bytes_done
>= count
)
1356 continue; /* If there's more space, just go on */
1358 if ((bytes_done
> 0) && channel
->rd_allow_partial
)
1362 * Indefinite sleep with mutex taken. With data waiting for
1363 * flushing, user should not be surprised if open() for write
1367 if (filp
->f_flags
& O_NONBLOCK
) {
1372 if (wait_event_interruptible(channel
->rd_wait
,
1373 (!channel
->rd_full
))) {
1374 mutex_unlock(&channel
->rd_mutex
);
1376 if (channel
->endpoint
->fatal_error
)
1385 mutex_unlock(&channel
->rd_mutex
);
1387 if (!channel
->rd_synchronous
)
1388 queue_delayed_work(xillybus_wq
,
1389 &channel
->rd_workitem
,
1392 if (channel
->endpoint
->fatal_error
)
1398 if ((channel
->rd_synchronous
) && (bytes_done
> 0)) {
1399 rc
= xillybus_myflush(filp
->private_data
, 0); /* No timeout */
1401 if (rc
&& (rc
!= -EINTR
))
1408 static int xillybus_open(struct inode
*inode
, struct file
*filp
)
1411 unsigned long flags
;
1412 int minor
= iminor(inode
);
1413 int major
= imajor(inode
);
1414 struct xilly_endpoint
*ep_iter
, *endpoint
= NULL
;
1415 struct xilly_channel
*channel
;
1417 mutex_lock(&ep_list_lock
);
1419 list_for_each_entry(ep_iter
, &list_of_endpoints
, ep_list
) {
1420 if ((ep_iter
->major
== major
) &&
1421 (minor
>= ep_iter
->lowest_minor
) &&
1422 (minor
< (ep_iter
->lowest_minor
+
1423 ep_iter
->num_channels
))) {
1428 mutex_unlock(&ep_list_lock
);
1431 pr_err("xillybus: open() failed to find a device for major=%d and minor=%d\n",
1436 if (endpoint
->fatal_error
)
1439 channel
= endpoint
->channels
[1 + minor
- endpoint
->lowest_minor
];
1440 filp
->private_data
= channel
;
1443 * It gets complicated because:
1444 * 1. We don't want to take a mutex we don't have to
1445 * 2. We don't want to open one direction if the other will fail.
1448 if ((filp
->f_mode
& FMODE_READ
) && (!channel
->num_wr_buffers
))
1451 if ((filp
->f_mode
& FMODE_WRITE
) && (!channel
->num_rd_buffers
))
1454 if ((filp
->f_mode
& FMODE_READ
) && (filp
->f_flags
& O_NONBLOCK
) &&
1455 (channel
->wr_synchronous
|| !channel
->wr_allow_partial
||
1456 !channel
->wr_supports_nonempty
)) {
1457 dev_err(endpoint
->dev
,
1458 "open() failed: O_NONBLOCK not allowed for read on this device\n");
1462 if ((filp
->f_mode
& FMODE_WRITE
) && (filp
->f_flags
& O_NONBLOCK
) &&
1463 (channel
->rd_synchronous
|| !channel
->rd_allow_partial
)) {
1464 dev_err(endpoint
->dev
,
1465 "open() failed: O_NONBLOCK not allowed for write on this device\n");
1470 * Note: open() may block on getting mutexes despite O_NONBLOCK.
1471 * This shouldn't occur normally, since multiple open of the same
1472 * file descriptor is almost always prohibited anyhow
1473 * (*_exclusive_open is normally set in real-life systems).
1476 if (filp
->f_mode
& FMODE_READ
) {
1477 rc
= mutex_lock_interruptible(&channel
->wr_mutex
);
1482 if (filp
->f_mode
& FMODE_WRITE
) {
1483 rc
= mutex_lock_interruptible(&channel
->rd_mutex
);
1488 if ((filp
->f_mode
& FMODE_READ
) &&
1489 (channel
->wr_ref_count
!= 0) &&
1490 (channel
->wr_exclusive_open
)) {
1495 if ((filp
->f_mode
& FMODE_WRITE
) &&
1496 (channel
->rd_ref_count
!= 0) &&
1497 (channel
->rd_exclusive_open
)) {
1502 if (filp
->f_mode
& FMODE_READ
) {
1503 if (channel
->wr_ref_count
== 0) { /* First open of file */
1504 /* Move the host to first buffer */
1505 spin_lock_irqsave(&channel
->wr_spinlock
, flags
);
1506 channel
->wr_host_buf_idx
= 0;
1507 channel
->wr_host_buf_pos
= 0;
1508 channel
->wr_fpga_buf_idx
= -1;
1509 channel
->wr_empty
= 1;
1510 channel
->wr_ready
= 0;
1511 channel
->wr_sleepy
= 1;
1512 channel
->wr_eof
= -1;
1513 channel
->wr_hangup
= 0;
1515 spin_unlock_irqrestore(&channel
->wr_spinlock
, flags
);
1517 iowrite32(1 | (channel
->chan_num
<< 1) |
1518 (4 << 24) | /* Opcode 4, open channel */
1519 ((channel
->wr_synchronous
& 1) << 23),
1520 channel
->endpoint
->registers
+
1524 channel
->wr_ref_count
++;
1527 if (filp
->f_mode
& FMODE_WRITE
) {
1528 if (channel
->rd_ref_count
== 0) { /* First open of file */
1529 /* Move the host to first buffer */
1530 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1531 channel
->rd_host_buf_idx
= 0;
1532 channel
->rd_host_buf_pos
= 0;
1533 channel
->rd_leftovers
[3] = 0; /* No leftovers. */
1534 channel
->rd_fpga_buf_idx
= channel
->num_rd_buffers
- 1;
1535 channel
->rd_full
= 0;
1537 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1539 iowrite32((channel
->chan_num
<< 1) |
1540 (4 << 24), /* Opcode 4, open channel */
1541 channel
->endpoint
->registers
+
1545 channel
->rd_ref_count
++;
1549 if (filp
->f_mode
& FMODE_WRITE
)
1550 mutex_unlock(&channel
->rd_mutex
);
1552 if (filp
->f_mode
& FMODE_READ
)
1553 mutex_unlock(&channel
->wr_mutex
);
1555 if (!rc
&& (!channel
->seekable
))
1556 return nonseekable_open(inode
, filp
);
1561 static int xillybus_release(struct inode
*inode
, struct file
*filp
)
1563 unsigned long flags
;
1564 struct xilly_channel
*channel
= filp
->private_data
;
1569 if (channel
->endpoint
->fatal_error
)
1572 if (filp
->f_mode
& FMODE_WRITE
) {
1573 mutex_lock(&channel
->rd_mutex
);
1575 channel
->rd_ref_count
--;
1577 if (channel
->rd_ref_count
== 0) {
1579 * We rely on the kernel calling flush()
1580 * before we get here.
1583 iowrite32((channel
->chan_num
<< 1) | /* Channel ID */
1584 (5 << 24), /* Opcode 5, close channel */
1585 channel
->endpoint
->registers
+
1588 mutex_unlock(&channel
->rd_mutex
);
1591 if (filp
->f_mode
& FMODE_READ
) {
1592 mutex_lock(&channel
->wr_mutex
);
1594 channel
->wr_ref_count
--;
1596 if (channel
->wr_ref_count
== 0) {
1597 iowrite32(1 | (channel
->chan_num
<< 1) |
1598 (5 << 24), /* Opcode 5, close channel */
1599 channel
->endpoint
->registers
+
1603 * This is crazily cautious: We make sure that not
1604 * only that we got an EOF (be it because we closed
1605 * the channel or because of a user's EOF), but verify
1606 * that it's one beyond the last buffer arrived, so
1607 * we have no leftover buffers pending before wrapping
1608 * up (which can only happen in asynchronous channels,
1613 spin_lock_irqsave(&channel
->wr_spinlock
,
1615 buf_idx
= channel
->wr_fpga_buf_idx
;
1616 eof
= channel
->wr_eof
;
1617 channel
->wr_sleepy
= 1;
1618 spin_unlock_irqrestore(&channel
->wr_spinlock
,
1622 * Check if eof points at the buffer after
1623 * the last one the FPGA submitted. Note that
1624 * no EOF is marked by negative eof.
1628 if (buf_idx
== channel
->num_wr_buffers
)
1635 * Steal extra 100 ms if awaken by interrupt.
1636 * This is a simple workaround for an
1637 * interrupt pending when entering, which would
1638 * otherwise result in declaring the hardware
1642 if (wait_event_interruptible(
1644 (!channel
->wr_sleepy
)))
1647 if (channel
->wr_sleepy
) {
1648 mutex_unlock(&channel
->wr_mutex
);
1649 dev_warn(channel
->endpoint
->dev
,
1650 "Hardware failed to respond to close command, therefore left in messy state.\n");
1656 mutex_unlock(&channel
->wr_mutex
);
1662 static loff_t
xillybus_llseek(struct file
*filp
, loff_t offset
, int whence
)
1664 struct xilly_channel
*channel
= filp
->private_data
;
1665 loff_t pos
= filp
->f_pos
;
1669 * Take both mutexes not allowing interrupts, since it seems like
1670 * common applications don't expect an -EINTR here. Besides, multiple
1671 * access to a single file descriptor on seekable devices is a mess
1675 if (channel
->endpoint
->fatal_error
)
1678 mutex_lock(&channel
->wr_mutex
);
1679 mutex_lock(&channel
->rd_mutex
);
1689 pos
= offset
; /* Going to the end => to the beginning */
1696 /* In any case, we must finish on an element boundary */
1697 if (pos
& ((1 << channel
->log2_element_size
) - 1)) {
1702 mutex_lock(&channel
->endpoint
->register_mutex
);
1704 iowrite32(pos
>> channel
->log2_element_size
,
1705 channel
->endpoint
->registers
+ fpga_buf_offset_reg
);
1707 iowrite32((channel
->chan_num
<< 1) |
1708 (6 << 24), /* Opcode 6, set address */
1709 channel
->endpoint
->registers
+ fpga_buf_ctrl_reg
);
1711 mutex_unlock(&channel
->endpoint
->register_mutex
);
1714 mutex_unlock(&channel
->rd_mutex
);
1715 mutex_unlock(&channel
->wr_mutex
);
1717 if (rc
) /* Return error after releasing mutexes */
1723 * Since seekable devices are allowed only when the channel is
1724 * synchronous, we assume that there is no data pending in either
1725 * direction (which holds true as long as no concurrent access on the
1726 * file descriptor takes place).
1727 * The only thing we may need to throw away is leftovers from partial
1731 channel
->rd_leftovers
[3] = 0;
1736 static __poll_t
xillybus_poll(struct file
*filp
, poll_table
*wait
)
1738 struct xilly_channel
*channel
= filp
->private_data
;
1740 unsigned long flags
;
1742 poll_wait(filp
, &channel
->endpoint
->ep_wait
, wait
);
1745 * poll() won't play ball regarding read() channels which
1746 * aren't asynchronous and support the nonempty message. Allowing
1747 * that will create situations where data has been delivered at
1748 * the FPGA, and users expecting select() to wake up, which it may
1752 if (!channel
->wr_synchronous
&& channel
->wr_supports_nonempty
) {
1753 poll_wait(filp
, &channel
->wr_wait
, wait
);
1754 poll_wait(filp
, &channel
->wr_ready_wait
, wait
);
1756 spin_lock_irqsave(&channel
->wr_spinlock
, flags
);
1757 if (!channel
->wr_empty
|| channel
->wr_ready
)
1758 mask
|= EPOLLIN
| EPOLLRDNORM
;
1760 if (channel
->wr_hangup
)
1762 * Not EPOLLHUP, because its behavior is in the
1763 * mist, and EPOLLIN does what we want: Wake up
1764 * the read file descriptor so it sees EOF.
1766 mask
|= EPOLLIN
| EPOLLRDNORM
;
1767 spin_unlock_irqrestore(&channel
->wr_spinlock
, flags
);
1771 * If partial data write is disallowed on a write() channel,
1772 * it's pointless to ever signal OK to write, because is could
1773 * block despite some space being available.
1776 if (channel
->rd_allow_partial
) {
1777 poll_wait(filp
, &channel
->rd_wait
, wait
);
1779 spin_lock_irqsave(&channel
->rd_spinlock
, flags
);
1780 if (!channel
->rd_full
)
1781 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1782 spin_unlock_irqrestore(&channel
->rd_spinlock
, flags
);
1785 if (channel
->endpoint
->fatal_error
)
1791 static const struct file_operations xillybus_fops
= {
1792 .owner
= THIS_MODULE
,
1793 .read
= xillybus_read
,
1794 .write
= xillybus_write
,
1795 .open
= xillybus_open
,
1796 .flush
= xillybus_flush
,
1797 .release
= xillybus_release
,
1798 .llseek
= xillybus_llseek
,
1799 .poll
= xillybus_poll
,
1802 static int xillybus_init_chrdev(struct xilly_endpoint
*endpoint
,
1803 const unsigned char *idt
)
1807 int devnum
, i
, minor
, major
;
1809 struct device
*device
;
1811 rc
= alloc_chrdev_region(&dev
, 0, /* minor start */
1812 endpoint
->num_channels
,
1815 dev_warn(endpoint
->dev
, "Failed to obtain major/minors");
1819 endpoint
->major
= major
= MAJOR(dev
);
1820 endpoint
->lowest_minor
= minor
= MINOR(dev
);
1822 cdev_init(&endpoint
->cdev
, &xillybus_fops
);
1823 endpoint
->cdev
.owner
= endpoint
->ephw
->owner
;
1824 rc
= cdev_add(&endpoint
->cdev
, MKDEV(major
, minor
),
1825 endpoint
->num_channels
);
1827 dev_warn(endpoint
->dev
, "Failed to add cdev. Aborting.\n");
1828 goto unregister_chrdev
;
1833 for (i
= minor
, devnum
= 0;
1834 devnum
< endpoint
->num_channels
;
1836 snprintf(devname
, sizeof(devname
)-1, "xillybus_%s", idt
);
1838 devname
[sizeof(devname
)-1] = 0; /* Should never matter */
1843 device
= device_create(xillybus_class
,
1849 if (IS_ERR(device
)) {
1850 dev_warn(endpoint
->dev
,
1851 "Failed to create %s device. Aborting.\n",
1854 goto unroll_device_create
;
1858 dev_info(endpoint
->dev
, "Created %d device files.\n",
1859 endpoint
->num_channels
);
1860 return 0; /* succeed */
1862 unroll_device_create
:
1864 for (; devnum
>= 0; devnum
--, i
--)
1865 device_destroy(xillybus_class
, MKDEV(major
, i
));
1867 cdev_del(&endpoint
->cdev
);
1869 unregister_chrdev_region(MKDEV(major
, minor
), endpoint
->num_channels
);
1874 static void xillybus_cleanup_chrdev(struct xilly_endpoint
*endpoint
)
1878 for (minor
= endpoint
->lowest_minor
;
1879 minor
< (endpoint
->lowest_minor
+ endpoint
->num_channels
);
1881 device_destroy(xillybus_class
, MKDEV(endpoint
->major
, minor
));
1882 cdev_del(&endpoint
->cdev
);
1883 unregister_chrdev_region(MKDEV(endpoint
->major
,
1884 endpoint
->lowest_minor
),
1885 endpoint
->num_channels
);
1887 dev_info(endpoint
->dev
, "Removed %d device files.\n",
1888 endpoint
->num_channels
);
1891 struct xilly_endpoint
*xillybus_init_endpoint(struct pci_dev
*pdev
,
1893 struct xilly_endpoint_hardware
1896 struct xilly_endpoint
*endpoint
;
1898 endpoint
= devm_kzalloc(dev
, sizeof(*endpoint
), GFP_KERNEL
);
1902 endpoint
->pdev
= pdev
;
1903 endpoint
->dev
= dev
;
1904 endpoint
->ephw
= ephw
;
1905 endpoint
->msg_counter
= 0x0b;
1906 endpoint
->failed_messages
= 0;
1907 endpoint
->fatal_error
= 0;
1909 init_waitqueue_head(&endpoint
->ep_wait
);
1910 mutex_init(&endpoint
->register_mutex
);
1914 EXPORT_SYMBOL(xillybus_init_endpoint
);
1916 static int xilly_quiesce(struct xilly_endpoint
*endpoint
)
1920 endpoint
->idtlen
= -1;
1922 iowrite32((u32
) (endpoint
->dma_using_dac
& 0x0001),
1923 endpoint
->registers
+ fpga_dma_control_reg
);
1925 t
= wait_event_interruptible_timeout(endpoint
->ep_wait
,
1926 (endpoint
->idtlen
>= 0),
1929 dev_err(endpoint
->dev
,
1930 "Failed to quiesce the device on exit.\n");
1936 int xillybus_endpoint_discovery(struct xilly_endpoint
*endpoint
)
1941 void *bootstrap_resources
;
1942 int idtbuffersize
= (1 << PAGE_SHIFT
);
1943 struct device
*dev
= endpoint
->dev
;
1946 * The bogus IDT is used during bootstrap for allocating the initial
1947 * message buffer, and then the message buffer and space for the IDT
1948 * itself. The initial message buffer is of a single page's size, but
1949 * it's soon replaced with a more modest one (and memory is freed).
1952 unsigned char bogus_idt
[8] = { 1, 224, (PAGE_SHIFT
)-2, 0,
1953 3, 192, PAGE_SHIFT
, 0 };
1954 struct xilly_idt_handle idt_handle
;
1957 * Writing the value 0x00000001 to Endianness register signals which
1958 * endianness this processor is using, so the FPGA can swap words as
1962 iowrite32(1, endpoint
->registers
+ fpga_endian_reg
);
1964 /* Bootstrap phase I: Allocate temporary message buffer */
1966 bootstrap_resources
= devres_open_group(dev
, NULL
, GFP_KERNEL
);
1967 if (!bootstrap_resources
)
1970 endpoint
->num_channels
= 0;
1972 rc
= xilly_setupchannels(endpoint
, bogus_idt
, 1);
1976 /* Clear the message subsystem (and counter in particular) */
1977 iowrite32(0x04, endpoint
->registers
+ fpga_msg_ctrl_reg
);
1979 endpoint
->idtlen
= -1;
1982 * Set DMA 32/64 bit mode, quiesce the device (?!) and get IDT
1985 iowrite32((u32
) (endpoint
->dma_using_dac
& 0x0001),
1986 endpoint
->registers
+ fpga_dma_control_reg
);
1988 t
= wait_event_interruptible_timeout(endpoint
->ep_wait
,
1989 (endpoint
->idtlen
>= 0),
1992 dev_err(endpoint
->dev
, "No response from FPGA. Aborting.\n");
1997 iowrite32((u32
) (0x0002 | (endpoint
->dma_using_dac
& 0x0001)),
1998 endpoint
->registers
+ fpga_dma_control_reg
);
2000 /* Bootstrap phase II: Allocate buffer for IDT and obtain it */
2001 while (endpoint
->idtlen
>= idtbuffersize
) {
2006 endpoint
->num_channels
= 1;
2008 rc
= xilly_setupchannels(endpoint
, bogus_idt
, 2);
2012 rc
= xilly_obtain_idt(endpoint
);
2016 rc
= xilly_scan_idt(endpoint
, &idt_handle
);
2020 devres_close_group(dev
, bootstrap_resources
);
2022 /* Bootstrap phase III: Allocate buffers according to IDT */
2024 rc
= xilly_setupchannels(endpoint
,
2025 idt_handle
.chandesc
,
2026 idt_handle
.entries
);
2031 * endpoint is now completely configured. We put it on the list
2032 * available to open() before registering the char device(s)
2035 mutex_lock(&ep_list_lock
);
2036 list_add_tail(&endpoint
->ep_list
, &list_of_endpoints
);
2037 mutex_unlock(&ep_list_lock
);
2039 rc
= xillybus_init_chrdev(endpoint
, idt_handle
.idt
);
2041 goto failed_chrdevs
;
2043 devres_release_group(dev
, bootstrap_resources
);
2048 mutex_lock(&ep_list_lock
);
2049 list_del(&endpoint
->ep_list
);
2050 mutex_unlock(&ep_list_lock
);
2053 xilly_quiesce(endpoint
);
2054 flush_workqueue(xillybus_wq
);
2058 EXPORT_SYMBOL(xillybus_endpoint_discovery
);
2060 void xillybus_endpoint_remove(struct xilly_endpoint
*endpoint
)
2062 xillybus_cleanup_chrdev(endpoint
);
2064 mutex_lock(&ep_list_lock
);
2065 list_del(&endpoint
->ep_list
);
2066 mutex_unlock(&ep_list_lock
);
2068 xilly_quiesce(endpoint
);
2071 * Flushing is done upon endpoint release to prevent access to memory
2072 * just about to be released. This makes the quiesce complete.
2074 flush_workqueue(xillybus_wq
);
2076 EXPORT_SYMBOL(xillybus_endpoint_remove
);
2078 static int __init
xillybus_init(void)
2080 mutex_init(&ep_list_lock
);
2082 xillybus_class
= class_create(THIS_MODULE
, xillyname
);
2083 if (IS_ERR(xillybus_class
))
2084 return PTR_ERR(xillybus_class
);
2086 xillybus_wq
= alloc_workqueue(xillyname
, 0, 0);
2088 class_destroy(xillybus_class
);
2095 static void __exit
xillybus_exit(void)
2097 /* flush_workqueue() was called for each endpoint released */
2098 destroy_workqueue(xillybus_wq
);
2100 class_destroy(xillybus_class
);
2103 module_init(xillybus_init
);
2104 module_exit(xillybus_exit
);