1 // SPDX-License-Identifier: GPL-2.0+
3 * Mellanox BlueField SoC TmFifo driver
5 * Copyright (C) 2019 Mellanox Technologies
8 #include <linux/acpi.h>
9 #include <linux/bitfield.h>
10 #include <linux/circ_buf.h>
11 #include <linux/efi.h>
12 #include <linux/irq.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/platform_device.h>
16 #include <linux/types.h>
18 #include <linux/virtio_config.h>
19 #include <linux/virtio_console.h>
20 #include <linux/virtio_ids.h>
21 #include <linux/virtio_net.h>
22 #include <linux/virtio_ring.h>
24 #include "mlxbf-tmfifo-regs.h"
27 #define MLXBF_TMFIFO_VRING_SIZE SZ_1K
29 /* Console Tx buffer size. */
30 #define MLXBF_TMFIFO_CON_TX_BUF_SIZE SZ_32K
32 /* Console Tx buffer reserved space. */
33 #define MLXBF_TMFIFO_CON_TX_BUF_RSV_SIZE 8
35 /* House-keeping timer interval. */
36 #define MLXBF_TMFIFO_TIMER_INTERVAL (HZ / 10)
38 /* Virtual devices sharing the TM FIFO. */
39 #define MLXBF_TMFIFO_VDEV_MAX (VIRTIO_ID_CONSOLE + 1)
42 * Reserve 1/16 of TmFifo space, so console messages are not starved by
43 * the networking traffic.
45 #define MLXBF_TMFIFO_RESERVE_RATIO 16
47 /* Message with data needs at least two words (for header & data). */
48 #define MLXBF_TMFIFO_DATA_MIN_WORDS 2
53 * mlxbf_tmfifo_vring - Structure of the TmFifo virtual ring
54 * @va: virtual address of the ring
55 * @dma: dma address of the ring
56 * @vq: pointer to the virtio virtqueue
57 * @desc: current descriptor of the pending packet
58 * @desc_head: head descriptor of the pending packet
59 * @cur_len: processed length of the current descriptor
60 * @rem_len: remaining length of the pending packet
61 * @pkt_len: total length of the pending packet
62 * @next_avail: next avail descriptor id
63 * @num: vring size (number of descriptors)
64 * @align: vring alignment size
66 * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
67 * @fifo: pointer to the tmfifo structure
69 struct mlxbf_tmfifo_vring
{
73 struct vring_desc
*desc
;
74 struct vring_desc
*desc_head
;
83 struct mlxbf_tmfifo
*fifo
;
86 /* Interrupt types. */
95 /* Ring types (Rx & Tx). */
97 MLXBF_TMFIFO_VRING_RX
,
98 MLXBF_TMFIFO_VRING_TX
,
99 MLXBF_TMFIFO_VRING_MAX
103 * mlxbf_tmfifo_vdev - Structure of the TmFifo virtual device
104 * @vdev: virtio device, in which the vdev.id.device field has the
105 * VIRTIO_ID_xxx id to distinguish the virtual device.
106 * @status: status of the device
107 * @features: supported features of the device
108 * @vrings: array of tmfifo vrings of this device
109 * @config.cons: virtual console config -
110 * select if vdev.id.device is VIRTIO_ID_CONSOLE
111 * @config.net: virtual network config -
112 * select if vdev.id.device is VIRTIO_ID_NET
113 * @tx_buf: tx buffer used to buffer data before writing into the FIFO
115 struct mlxbf_tmfifo_vdev
{
116 struct virtio_device vdev
;
119 struct mlxbf_tmfifo_vring vrings
[MLXBF_TMFIFO_VRING_MAX
];
121 struct virtio_console_config cons
;
122 struct virtio_net_config net
;
124 struct circ_buf tx_buf
;
128 * mlxbf_tmfifo_irq_info - Structure of the interrupt information
129 * @fifo: pointer to the tmfifo structure
130 * @irq: interrupt number
131 * @index: index into the interrupt array
133 struct mlxbf_tmfifo_irq_info
{
134 struct mlxbf_tmfifo
*fifo
;
140 * mlxbf_tmfifo - Structure of the TmFifo
141 * @vdev: array of the virtual devices running over the TmFifo
142 * @lock: lock to protect the TmFifo access
143 * @rx_base: mapped register base address for the Rx FIFO
144 * @tx_base: mapped register base address for the Tx FIFO
145 * @rx_fifo_size: number of entries of the Rx FIFO
146 * @tx_fifo_size: number of entries of the Tx FIFO
147 * @pend_events: pending bits for deferred events
148 * @irq_info: interrupt information
149 * @work: work struct for deferred process
150 * @timer: background timer
152 * @spin_lock: spin lock
153 * @is_ready: ready flag
155 struct mlxbf_tmfifo
{
156 struct mlxbf_tmfifo_vdev
*vdev
[MLXBF_TMFIFO_VDEV_MAX
];
157 struct mutex lock
; /* TmFifo lock */
158 void __iomem
*rx_base
;
159 void __iomem
*tx_base
;
162 unsigned long pend_events
;
163 struct mlxbf_tmfifo_irq_info irq_info
[MLXBF_TM_MAX_IRQ
];
164 struct work_struct work
;
165 struct timer_list timer
;
166 struct mlxbf_tmfifo_vring
*vring
[2];
167 spinlock_t spin_lock
; /* spin lock */
172 * mlxbf_tmfifo_msg_hdr - Structure of the TmFifo message header
173 * @type: message type
174 * @len: payload length in network byte order. Messages sent into the FIFO
175 * will be read by the other side as data stream in the same byte order.
176 * The length needs to be encoded into network order so both sides
177 * could understand it.
179 struct mlxbf_tmfifo_msg_hdr
{
183 } __packed
__aligned(sizeof(u64
));
187 * This MAC address will be read from EFI persistent variable if configured.
188 * It can also be reconfigured with standard Linux tools.
190 static u8 mlxbf_tmfifo_net_default_mac
[ETH_ALEN
] = {
191 0x00, 0x1A, 0xCA, 0xFF, 0xFF, 0x01
194 /* EFI variable name of the MAC address. */
195 static efi_char16_t mlxbf_tmfifo_efi_name
[] = L
"RshimMacAddr";
197 /* Maximum L2 header length. */
198 #define MLXBF_TMFIFO_NET_L2_OVERHEAD 36
200 /* Supported virtio-net features. */
201 #define MLXBF_TMFIFO_NET_FEATURES \
202 (BIT_ULL(VIRTIO_NET_F_MTU) | BIT_ULL(VIRTIO_NET_F_STATUS) | \
203 BIT_ULL(VIRTIO_NET_F_MAC))
205 #define mlxbf_vdev_to_tmfifo(d) container_of(d, struct mlxbf_tmfifo_vdev, vdev)
207 /* Free vrings of the FIFO device. */
208 static void mlxbf_tmfifo_free_vrings(struct mlxbf_tmfifo
*fifo
,
209 struct mlxbf_tmfifo_vdev
*tm_vdev
)
211 struct mlxbf_tmfifo_vring
*vring
;
214 for (i
= 0; i
< ARRAY_SIZE(tm_vdev
->vrings
); i
++) {
215 vring
= &tm_vdev
->vrings
[i
];
217 size
= vring_size(vring
->num
, vring
->align
);
218 dma_free_coherent(tm_vdev
->vdev
.dev
.parent
, size
,
219 vring
->va
, vring
->dma
);
222 vring_del_virtqueue(vring
->vq
);
229 /* Allocate vrings for the FIFO. */
230 static int mlxbf_tmfifo_alloc_vrings(struct mlxbf_tmfifo
*fifo
,
231 struct mlxbf_tmfifo_vdev
*tm_vdev
)
233 struct mlxbf_tmfifo_vring
*vring
;
239 for (i
= 0; i
< ARRAY_SIZE(tm_vdev
->vrings
); i
++) {
240 vring
= &tm_vdev
->vrings
[i
];
242 vring
->num
= MLXBF_TMFIFO_VRING_SIZE
;
243 vring
->align
= SMP_CACHE_BYTES
;
245 vring
->vdev_id
= tm_vdev
->vdev
.id
.device
;
246 dev
= &tm_vdev
->vdev
.dev
;
248 size
= vring_size(vring
->num
, vring
->align
);
249 va
= dma_alloc_coherent(dev
->parent
, size
, &dma
, GFP_KERNEL
);
251 mlxbf_tmfifo_free_vrings(fifo
, tm_vdev
);
252 dev_err(dev
->parent
, "dma_alloc_coherent failed\n");
263 /* Disable interrupts of the FIFO device. */
264 static void mlxbf_tmfifo_disable_irqs(struct mlxbf_tmfifo
*fifo
)
268 for (i
= 0; i
< MLXBF_TM_MAX_IRQ
; i
++) {
269 irq
= fifo
->irq_info
[i
].irq
;
270 fifo
->irq_info
[i
].irq
= 0;
275 /* Interrupt handler. */
276 static irqreturn_t
mlxbf_tmfifo_irq_handler(int irq
, void *arg
)
278 struct mlxbf_tmfifo_irq_info
*irq_info
= arg
;
280 if (!test_and_set_bit(irq_info
->index
, &irq_info
->fifo
->pend_events
))
281 schedule_work(&irq_info
->fifo
->work
);
286 /* Get the next packet descriptor from the vring. */
287 static struct vring_desc
*
288 mlxbf_tmfifo_get_next_desc(struct mlxbf_tmfifo_vring
*vring
)
290 const struct vring
*vr
= virtqueue_get_vring(vring
->vq
);
291 struct virtio_device
*vdev
= vring
->vq
->vdev
;
292 unsigned int idx
, head
;
294 if (vring
->next_avail
== virtio16_to_cpu(vdev
, vr
->avail
->idx
))
297 idx
= vring
->next_avail
% vr
->num
;
298 head
= virtio16_to_cpu(vdev
, vr
->avail
->ring
[idx
]);
299 if (WARN_ON(head
>= vr
->num
))
304 return &vr
->desc
[head
];
307 /* Release virtio descriptor. */
308 static void mlxbf_tmfifo_release_desc(struct mlxbf_tmfifo_vring
*vring
,
309 struct vring_desc
*desc
, u32 len
)
311 const struct vring
*vr
= virtqueue_get_vring(vring
->vq
);
312 struct virtio_device
*vdev
= vring
->vq
->vdev
;
315 vr_idx
= virtio16_to_cpu(vdev
, vr
->used
->idx
);
316 idx
= vr_idx
% vr
->num
;
317 vr
->used
->ring
[idx
].id
= cpu_to_virtio32(vdev
, desc
- vr
->desc
);
318 vr
->used
->ring
[idx
].len
= cpu_to_virtio32(vdev
, len
);
321 * Virtio could poll and check the 'idx' to decide whether the desc is
322 * done or not. Add a memory barrier here to make sure the update above
323 * completes before updating the idx.
326 vr
->used
->idx
= cpu_to_virtio16(vdev
, vr_idx
+ 1);
329 /* Get the total length of the descriptor chain. */
330 static u32
mlxbf_tmfifo_get_pkt_len(struct mlxbf_tmfifo_vring
*vring
,
331 struct vring_desc
*desc
)
333 const struct vring
*vr
= virtqueue_get_vring(vring
->vq
);
334 struct virtio_device
*vdev
= vring
->vq
->vdev
;
338 len
+= virtio32_to_cpu(vdev
, desc
->len
);
339 if (!(virtio16_to_cpu(vdev
, desc
->flags
) & VRING_DESC_F_NEXT
))
341 idx
= virtio16_to_cpu(vdev
, desc
->next
);
342 desc
= &vr
->desc
[idx
];
348 static void mlxbf_tmfifo_release_pending_pkt(struct mlxbf_tmfifo_vring
*vring
)
350 struct vring_desc
*desc_head
;
353 if (vring
->desc_head
) {
354 desc_head
= vring
->desc_head
;
355 len
= vring
->pkt_len
;
357 desc_head
= mlxbf_tmfifo_get_next_desc(vring
);
358 len
= mlxbf_tmfifo_get_pkt_len(vring
, desc_head
);
362 mlxbf_tmfifo_release_desc(vring
, desc_head
, len
);
366 vring
->desc_head
= NULL
;
369 static void mlxbf_tmfifo_init_net_desc(struct mlxbf_tmfifo_vring
*vring
,
370 struct vring_desc
*desc
, bool is_rx
)
372 struct virtio_device
*vdev
= vring
->vq
->vdev
;
373 struct virtio_net_hdr
*net_hdr
;
375 net_hdr
= phys_to_virt(virtio64_to_cpu(vdev
, desc
->addr
));
376 memset(net_hdr
, 0, sizeof(*net_hdr
));
379 /* Get and initialize the next packet. */
380 static struct vring_desc
*
381 mlxbf_tmfifo_get_next_pkt(struct mlxbf_tmfifo_vring
*vring
, bool is_rx
)
383 struct vring_desc
*desc
;
385 desc
= mlxbf_tmfifo_get_next_desc(vring
);
386 if (desc
&& is_rx
&& vring
->vdev_id
== VIRTIO_ID_NET
)
387 mlxbf_tmfifo_init_net_desc(vring
, desc
, is_rx
);
389 vring
->desc_head
= desc
;
395 /* House-keeping timer. */
396 static void mlxbf_tmfifo_timer(struct timer_list
*t
)
398 struct mlxbf_tmfifo
*fifo
= container_of(t
, struct mlxbf_tmfifo
, timer
);
401 rx
= !test_and_set_bit(MLXBF_TM_RX_HWM_IRQ
, &fifo
->pend_events
);
402 tx
= !test_and_set_bit(MLXBF_TM_TX_LWM_IRQ
, &fifo
->pend_events
);
405 schedule_work(&fifo
->work
);
407 mod_timer(&fifo
->timer
, jiffies
+ MLXBF_TMFIFO_TIMER_INTERVAL
);
410 /* Copy one console packet into the output buffer. */
411 static void mlxbf_tmfifo_console_output_one(struct mlxbf_tmfifo_vdev
*cons
,
412 struct mlxbf_tmfifo_vring
*vring
,
413 struct vring_desc
*desc
)
415 const struct vring
*vr
= virtqueue_get_vring(vring
->vq
);
416 struct virtio_device
*vdev
= &cons
->vdev
;
421 addr
= phys_to_virt(virtio64_to_cpu(vdev
, desc
->addr
));
422 len
= virtio32_to_cpu(vdev
, desc
->len
);
424 seg
= CIRC_SPACE_TO_END(cons
->tx_buf
.head
, cons
->tx_buf
.tail
,
425 MLXBF_TMFIFO_CON_TX_BUF_SIZE
);
427 memcpy(cons
->tx_buf
.buf
+ cons
->tx_buf
.head
, addr
, len
);
429 memcpy(cons
->tx_buf
.buf
+ cons
->tx_buf
.head
, addr
, seg
);
431 memcpy(cons
->tx_buf
.buf
, addr
, len
- seg
);
433 cons
->tx_buf
.head
= (cons
->tx_buf
.head
+ len
) %
434 MLXBF_TMFIFO_CON_TX_BUF_SIZE
;
436 if (!(virtio16_to_cpu(vdev
, desc
->flags
) & VRING_DESC_F_NEXT
))
438 idx
= virtio16_to_cpu(vdev
, desc
->next
);
439 desc
= &vr
->desc
[idx
];
443 /* Copy console data into the output buffer. */
444 static void mlxbf_tmfifo_console_output(struct mlxbf_tmfifo_vdev
*cons
,
445 struct mlxbf_tmfifo_vring
*vring
)
447 struct vring_desc
*desc
;
450 desc
= mlxbf_tmfifo_get_next_desc(vring
);
452 /* Release the packet if not enough space. */
453 len
= mlxbf_tmfifo_get_pkt_len(vring
, desc
);
454 avail
= CIRC_SPACE(cons
->tx_buf
.head
, cons
->tx_buf
.tail
,
455 MLXBF_TMFIFO_CON_TX_BUF_SIZE
);
456 if (len
+ MLXBF_TMFIFO_CON_TX_BUF_RSV_SIZE
> avail
) {
457 mlxbf_tmfifo_release_desc(vring
, desc
, len
);
461 mlxbf_tmfifo_console_output_one(cons
, vring
, desc
);
462 mlxbf_tmfifo_release_desc(vring
, desc
, len
);
463 desc
= mlxbf_tmfifo_get_next_desc(vring
);
467 /* Get the number of available words in Rx FIFO for receiving. */
468 static int mlxbf_tmfifo_get_rx_avail(struct mlxbf_tmfifo
*fifo
)
472 sts
= readq(fifo
->rx_base
+ MLXBF_TMFIFO_RX_STS
);
473 return FIELD_GET(MLXBF_TMFIFO_RX_STS__COUNT_MASK
, sts
);
476 /* Get the number of available words in the TmFifo for sending. */
477 static int mlxbf_tmfifo_get_tx_avail(struct mlxbf_tmfifo
*fifo
, int vdev_id
)
483 /* Reserve some room in FIFO for console messages. */
484 if (vdev_id
== VIRTIO_ID_NET
)
485 tx_reserve
= fifo
->tx_fifo_size
/ MLXBF_TMFIFO_RESERVE_RATIO
;
489 sts
= readq(fifo
->tx_base
+ MLXBF_TMFIFO_TX_STS
);
490 count
= FIELD_GET(MLXBF_TMFIFO_TX_STS__COUNT_MASK
, sts
);
491 return fifo
->tx_fifo_size
- tx_reserve
- count
;
494 /* Console Tx (move data from the output buffer into the TmFifo). */
495 static void mlxbf_tmfifo_console_tx(struct mlxbf_tmfifo
*fifo
, int avail
)
497 struct mlxbf_tmfifo_msg_hdr hdr
;
498 struct mlxbf_tmfifo_vdev
*cons
;
504 /* Return if not enough space available. */
505 if (avail
< MLXBF_TMFIFO_DATA_MIN_WORDS
)
508 cons
= fifo
->vdev
[VIRTIO_ID_CONSOLE
];
509 if (!cons
|| !cons
->tx_buf
.buf
)
512 /* Return if no data to send. */
513 size
= CIRC_CNT(cons
->tx_buf
.head
, cons
->tx_buf
.tail
,
514 MLXBF_TMFIFO_CON_TX_BUF_SIZE
);
518 /* Adjust the size to available space. */
519 if (size
+ sizeof(hdr
) > avail
* sizeof(u64
))
520 size
= avail
* sizeof(u64
) - sizeof(hdr
);
523 hdr
.type
= VIRTIO_ID_CONSOLE
;
524 hdr
.len
= htons(size
);
525 writeq(*(u64
*)&hdr
, fifo
->tx_base
+ MLXBF_TMFIFO_TX_DATA
);
527 /* Use spin-lock to protect the 'cons->tx_buf'. */
528 spin_lock_irqsave(&fifo
->spin_lock
, flags
);
531 addr
= cons
->tx_buf
.buf
+ cons
->tx_buf
.tail
;
533 seg
= CIRC_CNT_TO_END(cons
->tx_buf
.head
, cons
->tx_buf
.tail
,
534 MLXBF_TMFIFO_CON_TX_BUF_SIZE
);
535 if (seg
>= sizeof(u64
)) {
536 memcpy(&data
, addr
, sizeof(u64
));
538 memcpy(&data
, addr
, seg
);
539 memcpy((u8
*)&data
+ seg
, cons
->tx_buf
.buf
,
542 writeq(data
, fifo
->tx_base
+ MLXBF_TMFIFO_TX_DATA
);
544 if (size
>= sizeof(u64
)) {
545 cons
->tx_buf
.tail
= (cons
->tx_buf
.tail
+ sizeof(u64
)) %
546 MLXBF_TMFIFO_CON_TX_BUF_SIZE
;
549 cons
->tx_buf
.tail
= (cons
->tx_buf
.tail
+ size
) %
550 MLXBF_TMFIFO_CON_TX_BUF_SIZE
;
555 spin_unlock_irqrestore(&fifo
->spin_lock
, flags
);
558 /* Rx/Tx one word in the descriptor buffer. */
559 static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring
*vring
,
560 struct vring_desc
*desc
,
563 struct virtio_device
*vdev
= vring
->vq
->vdev
;
564 struct mlxbf_tmfifo
*fifo
= vring
->fifo
;
568 /* Get the buffer address of this desc. */
569 addr
= phys_to_virt(virtio64_to_cpu(vdev
, desc
->addr
));
571 /* Read a word from FIFO for Rx. */
573 data
= readq(fifo
->rx_base
+ MLXBF_TMFIFO_RX_DATA
);
575 if (vring
->cur_len
+ sizeof(u64
) <= len
) {
576 /* The whole word. */
578 memcpy(addr
+ vring
->cur_len
, &data
, sizeof(u64
));
580 memcpy(&data
, addr
+ vring
->cur_len
, sizeof(u64
));
581 vring
->cur_len
+= sizeof(u64
);
583 /* Leftover bytes. */
585 memcpy(addr
+ vring
->cur_len
, &data
,
586 len
- vring
->cur_len
);
588 memcpy(&data
, addr
+ vring
->cur_len
,
589 len
- vring
->cur_len
);
590 vring
->cur_len
= len
;
593 /* Write the word into FIFO for Tx. */
595 writeq(data
, fifo
->tx_base
+ MLXBF_TMFIFO_TX_DATA
);
599 * Rx/Tx packet header.
601 * In Rx case, the packet might be found to belong to a different vring since
602 * the TmFifo is shared by different services. In such case, the 'vring_change'
605 static void mlxbf_tmfifo_rxtx_header(struct mlxbf_tmfifo_vring
*vring
,
606 struct vring_desc
*desc
,
607 bool is_rx
, bool *vring_change
)
609 struct mlxbf_tmfifo
*fifo
= vring
->fifo
;
610 struct virtio_net_config
*config
;
611 struct mlxbf_tmfifo_msg_hdr hdr
;
612 int vdev_id
, hdr_len
;
614 /* Read/Write packet header. */
616 /* Drain one word from the FIFO. */
617 *(u64
*)&hdr
= readq(fifo
->rx_base
+ MLXBF_TMFIFO_RX_DATA
);
619 /* Skip the length 0 packets (keepalive). */
623 /* Check packet type. */
624 if (hdr
.type
== VIRTIO_ID_NET
) {
625 vdev_id
= VIRTIO_ID_NET
;
626 hdr_len
= sizeof(struct virtio_net_hdr
);
627 config
= &fifo
->vdev
[vdev_id
]->config
.net
;
628 if (ntohs(hdr
.len
) > config
->mtu
+
629 MLXBF_TMFIFO_NET_L2_OVERHEAD
)
632 vdev_id
= VIRTIO_ID_CONSOLE
;
637 * Check whether the new packet still belongs to this vring.
638 * If not, update the pkt_len of the new vring.
640 if (vdev_id
!= vring
->vdev_id
) {
641 struct mlxbf_tmfifo_vdev
*tm_dev2
= fifo
->vdev
[vdev_id
];
646 vring
= &tm_dev2
->vrings
[MLXBF_TMFIFO_VRING_RX
];
647 *vring_change
= true;
649 vring
->pkt_len
= ntohs(hdr
.len
) + hdr_len
;
651 /* Network virtio has an extra header. */
652 hdr_len
= (vring
->vdev_id
== VIRTIO_ID_NET
) ?
653 sizeof(struct virtio_net_hdr
) : 0;
654 vring
->pkt_len
= mlxbf_tmfifo_get_pkt_len(vring
, desc
);
655 hdr
.type
= (vring
->vdev_id
== VIRTIO_ID_NET
) ?
656 VIRTIO_ID_NET
: VIRTIO_ID_CONSOLE
;
657 hdr
.len
= htons(vring
->pkt_len
- hdr_len
);
658 writeq(*(u64
*)&hdr
, fifo
->tx_base
+ MLXBF_TMFIFO_TX_DATA
);
661 vring
->cur_len
= hdr_len
;
662 vring
->rem_len
= vring
->pkt_len
;
663 fifo
->vring
[is_rx
] = vring
;
667 * Rx/Tx one descriptor.
669 * Return true to indicate more data available.
671 static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring
*vring
,
672 bool is_rx
, int *avail
)
674 const struct vring
*vr
= virtqueue_get_vring(vring
->vq
);
675 struct mlxbf_tmfifo
*fifo
= vring
->fifo
;
676 struct virtio_device
*vdev
;
677 bool vring_change
= false;
678 struct vring_desc
*desc
;
682 vdev
= &fifo
->vdev
[vring
->vdev_id
]->vdev
;
684 /* Get the descriptor of the next packet. */
686 desc
= mlxbf_tmfifo_get_next_pkt(vring
, is_rx
);
693 /* Beginning of a packet. Start to Rx/Tx packet header. */
694 if (vring
->pkt_len
== 0) {
695 mlxbf_tmfifo_rxtx_header(vring
, desc
, is_rx
, &vring_change
);
698 /* Return if new packet is for another ring. */
701 goto mlxbf_tmfifo_desc_done
;
704 /* Get the length of this desc. */
705 len
= virtio32_to_cpu(vdev
, desc
->len
);
706 if (len
> vring
->rem_len
)
707 len
= vring
->rem_len
;
709 /* Rx/Tx one word (8 bytes) if not done. */
710 if (vring
->cur_len
< len
) {
711 mlxbf_tmfifo_rxtx_word(vring
, desc
, is_rx
, len
);
715 /* Check again whether it's done. */
716 if (vring
->cur_len
== len
) {
718 vring
->rem_len
-= len
;
720 /* Get the next desc on the chain. */
721 if (vring
->rem_len
> 0 &&
722 (virtio16_to_cpu(vdev
, desc
->flags
) & VRING_DESC_F_NEXT
)) {
723 idx
= virtio16_to_cpu(vdev
, desc
->next
);
724 desc
= &vr
->desc
[idx
];
725 goto mlxbf_tmfifo_desc_done
;
728 /* Done and release the pending packet. */
729 mlxbf_tmfifo_release_pending_pkt(vring
);
731 fifo
->vring
[is_rx
] = NULL
;
733 /* Notify upper layer that packet is done. */
734 spin_lock_irqsave(&fifo
->spin_lock
, flags
);
735 vring_interrupt(0, vring
->vq
);
736 spin_unlock_irqrestore(&fifo
->spin_lock
, flags
);
739 mlxbf_tmfifo_desc_done
:
740 /* Save the current desc. */
746 /* Rx & Tx processing of a queue. */
747 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring
*vring
, bool is_rx
)
749 int avail
= 0, devid
= vring
->vdev_id
;
750 struct mlxbf_tmfifo
*fifo
;
755 /* Return if vdev is not ready. */
756 if (!fifo
->vdev
[devid
])
759 /* Return if another vring is running. */
760 if (fifo
->vring
[is_rx
] && fifo
->vring
[is_rx
] != vring
)
763 /* Only handle console and network for now. */
764 if (WARN_ON(devid
!= VIRTIO_ID_NET
&& devid
!= VIRTIO_ID_CONSOLE
))
768 /* Get available FIFO space. */
771 avail
= mlxbf_tmfifo_get_rx_avail(fifo
);
773 avail
= mlxbf_tmfifo_get_tx_avail(fifo
, devid
);
778 /* Console output always comes from the Tx buffer. */
779 if (!is_rx
&& devid
== VIRTIO_ID_CONSOLE
) {
780 mlxbf_tmfifo_console_tx(fifo
, avail
);
784 /* Handle one descriptor. */
785 more
= mlxbf_tmfifo_rxtx_one_desc(vring
, is_rx
, &avail
);
789 /* Handle Rx or Tx queues. */
790 static void mlxbf_tmfifo_work_rxtx(struct mlxbf_tmfifo
*fifo
, int queue_id
,
791 int irq_id
, bool is_rx
)
793 struct mlxbf_tmfifo_vdev
*tm_vdev
;
794 struct mlxbf_tmfifo_vring
*vring
;
797 if (!test_and_clear_bit(irq_id
, &fifo
->pend_events
) ||
798 !fifo
->irq_info
[irq_id
].irq
)
801 for (i
= 0; i
< MLXBF_TMFIFO_VDEV_MAX
; i
++) {
802 tm_vdev
= fifo
->vdev
[i
];
804 vring
= &tm_vdev
->vrings
[queue_id
];
806 mlxbf_tmfifo_rxtx(vring
, is_rx
);
811 /* Work handler for Rx and Tx case. */
812 static void mlxbf_tmfifo_work_handler(struct work_struct
*work
)
814 struct mlxbf_tmfifo
*fifo
;
816 fifo
= container_of(work
, struct mlxbf_tmfifo
, work
);
820 mutex_lock(&fifo
->lock
);
822 /* Tx (Send data to the TmFifo). */
823 mlxbf_tmfifo_work_rxtx(fifo
, MLXBF_TMFIFO_VRING_TX
,
824 MLXBF_TM_TX_LWM_IRQ
, false);
826 /* Rx (Receive data from the TmFifo). */
827 mlxbf_tmfifo_work_rxtx(fifo
, MLXBF_TMFIFO_VRING_RX
,
828 MLXBF_TM_RX_HWM_IRQ
, true);
830 mutex_unlock(&fifo
->lock
);
833 /* The notify function is called when new buffers are posted. */
834 static bool mlxbf_tmfifo_virtio_notify(struct virtqueue
*vq
)
836 struct mlxbf_tmfifo_vring
*vring
= vq
->priv
;
837 struct mlxbf_tmfifo_vdev
*tm_vdev
;
838 struct mlxbf_tmfifo
*fifo
;
844 * Virtio maintains vrings in pairs, even number ring for Rx
845 * and odd number ring for Tx.
847 if (vring
->index
& BIT(0)) {
849 * Console could make blocking call with interrupts disabled.
850 * In such case, the vring needs to be served right away. For
851 * other cases, just set the TX LWM bit to start Tx in the
854 if (vring
->vdev_id
== VIRTIO_ID_CONSOLE
) {
855 spin_lock_irqsave(&fifo
->spin_lock
, flags
);
856 tm_vdev
= fifo
->vdev
[VIRTIO_ID_CONSOLE
];
857 mlxbf_tmfifo_console_output(tm_vdev
, vring
);
858 spin_unlock_irqrestore(&fifo
->spin_lock
, flags
);
859 } else if (test_and_set_bit(MLXBF_TM_TX_LWM_IRQ
,
860 &fifo
->pend_events
)) {
864 if (test_and_set_bit(MLXBF_TM_RX_HWM_IRQ
, &fifo
->pend_events
))
868 schedule_work(&fifo
->work
);
873 /* Get the array of feature bits for this device. */
874 static u64
mlxbf_tmfifo_virtio_get_features(struct virtio_device
*vdev
)
876 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
878 return tm_vdev
->features
;
881 /* Confirm device features to use. */
882 static int mlxbf_tmfifo_virtio_finalize_features(struct virtio_device
*vdev
)
884 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
886 tm_vdev
->features
= vdev
->features
;
891 /* Free virtqueues found by find_vqs(). */
892 static void mlxbf_tmfifo_virtio_del_vqs(struct virtio_device
*vdev
)
894 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
895 struct mlxbf_tmfifo_vring
*vring
;
896 struct virtqueue
*vq
;
899 for (i
= 0; i
< ARRAY_SIZE(tm_vdev
->vrings
); i
++) {
900 vring
= &tm_vdev
->vrings
[i
];
902 /* Release the pending packet. */
904 mlxbf_tmfifo_release_pending_pkt(vring
);
908 vring_del_virtqueue(vq
);
913 /* Create and initialize the virtual queues. */
914 static int mlxbf_tmfifo_virtio_find_vqs(struct virtio_device
*vdev
,
916 struct virtqueue
*vqs
[],
917 vq_callback_t
*callbacks
[],
918 const char * const names
[],
920 struct irq_affinity
*desc
)
922 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
923 struct mlxbf_tmfifo_vring
*vring
;
924 struct virtqueue
*vq
;
927 if (nvqs
> ARRAY_SIZE(tm_vdev
->vrings
))
930 for (i
= 0; i
< nvqs
; ++i
) {
935 vring
= &tm_vdev
->vrings
[i
];
938 size
= vring_size(vring
->num
, vring
->align
);
939 memset(vring
->va
, 0, size
);
940 vq
= vring_new_virtqueue(i
, vring
->num
, vring
->align
, vdev
,
941 false, false, vring
->va
,
942 mlxbf_tmfifo_virtio_notify
,
943 callbacks
[i
], names
[i
]);
945 dev_err(&vdev
->dev
, "vring_new_virtqueue failed\n");
958 mlxbf_tmfifo_virtio_del_vqs(vdev
);
962 /* Read the status byte. */
963 static u8
mlxbf_tmfifo_virtio_get_status(struct virtio_device
*vdev
)
965 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
967 return tm_vdev
->status
;
970 /* Write the status byte. */
971 static void mlxbf_tmfifo_virtio_set_status(struct virtio_device
*vdev
,
974 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
976 tm_vdev
->status
= status
;
979 /* Reset the device. Not much here for now. */
980 static void mlxbf_tmfifo_virtio_reset(struct virtio_device
*vdev
)
982 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
987 /* Read the value of a configuration field. */
988 static void mlxbf_tmfifo_virtio_get(struct virtio_device
*vdev
,
993 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
995 if ((u64
)offset
+ len
> sizeof(tm_vdev
->config
))
998 memcpy(buf
, (u8
*)&tm_vdev
->config
+ offset
, len
);
1001 /* Write the value of a configuration field. */
1002 static void mlxbf_tmfifo_virtio_set(struct virtio_device
*vdev
,
1003 unsigned int offset
,
1007 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
1009 if ((u64
)offset
+ len
> sizeof(tm_vdev
->config
))
1012 memcpy((u8
*)&tm_vdev
->config
+ offset
, buf
, len
);
1015 static void tmfifo_virtio_dev_release(struct device
*device
)
1017 struct virtio_device
*vdev
=
1018 container_of(device
, struct virtio_device
, dev
);
1019 struct mlxbf_tmfifo_vdev
*tm_vdev
= mlxbf_vdev_to_tmfifo(vdev
);
1024 /* Virtio config operations. */
1025 static const struct virtio_config_ops mlxbf_tmfifo_virtio_config_ops
= {
1026 .get_features
= mlxbf_tmfifo_virtio_get_features
,
1027 .finalize_features
= mlxbf_tmfifo_virtio_finalize_features
,
1028 .find_vqs
= mlxbf_tmfifo_virtio_find_vqs
,
1029 .del_vqs
= mlxbf_tmfifo_virtio_del_vqs
,
1030 .reset
= mlxbf_tmfifo_virtio_reset
,
1031 .set_status
= mlxbf_tmfifo_virtio_set_status
,
1032 .get_status
= mlxbf_tmfifo_virtio_get_status
,
1033 .get
= mlxbf_tmfifo_virtio_get
,
1034 .set
= mlxbf_tmfifo_virtio_set
,
1037 /* Create vdev for the FIFO. */
1038 static int mlxbf_tmfifo_create_vdev(struct device
*dev
,
1039 struct mlxbf_tmfifo
*fifo
,
1040 int vdev_id
, u64 features
,
1041 void *config
, u32 size
)
1043 struct mlxbf_tmfifo_vdev
*tm_vdev
, *reg_dev
= NULL
;
1046 mutex_lock(&fifo
->lock
);
1048 tm_vdev
= fifo
->vdev
[vdev_id
];
1050 dev_err(dev
, "vdev %d already exists\n", vdev_id
);
1055 tm_vdev
= kzalloc(sizeof(*tm_vdev
), GFP_KERNEL
);
1061 tm_vdev
->vdev
.id
.device
= vdev_id
;
1062 tm_vdev
->vdev
.config
= &mlxbf_tmfifo_virtio_config_ops
;
1063 tm_vdev
->vdev
.dev
.parent
= dev
;
1064 tm_vdev
->vdev
.dev
.release
= tmfifo_virtio_dev_release
;
1065 tm_vdev
->features
= features
;
1067 memcpy(&tm_vdev
->config
, config
, size
);
1069 if (mlxbf_tmfifo_alloc_vrings(fifo
, tm_vdev
)) {
1070 dev_err(dev
, "unable to allocate vring\n");
1075 /* Allocate an output buffer for the console device. */
1076 if (vdev_id
== VIRTIO_ID_CONSOLE
)
1077 tm_vdev
->tx_buf
.buf
= devm_kmalloc(dev
,
1078 MLXBF_TMFIFO_CON_TX_BUF_SIZE
,
1080 fifo
->vdev
[vdev_id
] = tm_vdev
;
1082 /* Register the virtio device. */
1083 ret
= register_virtio_device(&tm_vdev
->vdev
);
1086 dev_err(dev
, "register_virtio_device failed\n");
1090 mutex_unlock(&fifo
->lock
);
1094 mlxbf_tmfifo_free_vrings(fifo
, tm_vdev
);
1095 fifo
->vdev
[vdev_id
] = NULL
;
1097 put_device(&tm_vdev
->vdev
.dev
);
1101 mutex_unlock(&fifo
->lock
);
1105 /* Delete vdev for the FIFO. */
1106 static int mlxbf_tmfifo_delete_vdev(struct mlxbf_tmfifo
*fifo
, int vdev_id
)
1108 struct mlxbf_tmfifo_vdev
*tm_vdev
;
1110 mutex_lock(&fifo
->lock
);
1112 /* Unregister vdev. */
1113 tm_vdev
= fifo
->vdev
[vdev_id
];
1115 unregister_virtio_device(&tm_vdev
->vdev
);
1116 mlxbf_tmfifo_free_vrings(fifo
, tm_vdev
);
1117 fifo
->vdev
[vdev_id
] = NULL
;
1120 mutex_unlock(&fifo
->lock
);
1125 /* Read the configured network MAC address from efi variable. */
1126 static void mlxbf_tmfifo_get_cfg_mac(u8
*mac
)
1128 efi_guid_t guid
= EFI_GLOBAL_VARIABLE_GUID
;
1129 unsigned long size
= ETH_ALEN
;
1133 rc
= efi
.get_variable(mlxbf_tmfifo_efi_name
, &guid
, NULL
, &size
, buf
);
1134 if (rc
== EFI_SUCCESS
&& size
== ETH_ALEN
)
1135 ether_addr_copy(mac
, buf
);
1137 ether_addr_copy(mac
, mlxbf_tmfifo_net_default_mac
);
1140 /* Set TmFifo thresolds which is used to trigger interrupts. */
1141 static void mlxbf_tmfifo_set_threshold(struct mlxbf_tmfifo
*fifo
)
1145 /* Get Tx FIFO size and set the low/high watermark. */
1146 ctl
= readq(fifo
->tx_base
+ MLXBF_TMFIFO_TX_CTL
);
1147 fifo
->tx_fifo_size
=
1148 FIELD_GET(MLXBF_TMFIFO_TX_CTL__MAX_ENTRIES_MASK
, ctl
);
1149 ctl
= (ctl
& ~MLXBF_TMFIFO_TX_CTL__LWM_MASK
) |
1150 FIELD_PREP(MLXBF_TMFIFO_TX_CTL__LWM_MASK
,
1151 fifo
->tx_fifo_size
/ 2);
1152 ctl
= (ctl
& ~MLXBF_TMFIFO_TX_CTL__HWM_MASK
) |
1153 FIELD_PREP(MLXBF_TMFIFO_TX_CTL__HWM_MASK
,
1154 fifo
->tx_fifo_size
- 1);
1155 writeq(ctl
, fifo
->tx_base
+ MLXBF_TMFIFO_TX_CTL
);
1157 /* Get Rx FIFO size and set the low/high watermark. */
1158 ctl
= readq(fifo
->rx_base
+ MLXBF_TMFIFO_RX_CTL
);
1159 fifo
->rx_fifo_size
=
1160 FIELD_GET(MLXBF_TMFIFO_RX_CTL__MAX_ENTRIES_MASK
, ctl
);
1161 ctl
= (ctl
& ~MLXBF_TMFIFO_RX_CTL__LWM_MASK
) |
1162 FIELD_PREP(MLXBF_TMFIFO_RX_CTL__LWM_MASK
, 0);
1163 ctl
= (ctl
& ~MLXBF_TMFIFO_RX_CTL__HWM_MASK
) |
1164 FIELD_PREP(MLXBF_TMFIFO_RX_CTL__HWM_MASK
, 1);
1165 writeq(ctl
, fifo
->rx_base
+ MLXBF_TMFIFO_RX_CTL
);
1168 static void mlxbf_tmfifo_cleanup(struct mlxbf_tmfifo
*fifo
)
1172 fifo
->is_ready
= false;
1173 del_timer_sync(&fifo
->timer
);
1174 mlxbf_tmfifo_disable_irqs(fifo
);
1175 cancel_work_sync(&fifo
->work
);
1176 for (i
= 0; i
< MLXBF_TMFIFO_VDEV_MAX
; i
++)
1177 mlxbf_tmfifo_delete_vdev(fifo
, i
);
1180 /* Probe the TMFIFO. */
1181 static int mlxbf_tmfifo_probe(struct platform_device
*pdev
)
1183 struct virtio_net_config net_config
;
1184 struct device
*dev
= &pdev
->dev
;
1185 struct mlxbf_tmfifo
*fifo
;
1188 fifo
= devm_kzalloc(dev
, sizeof(*fifo
), GFP_KERNEL
);
1192 spin_lock_init(&fifo
->spin_lock
);
1193 INIT_WORK(&fifo
->work
, mlxbf_tmfifo_work_handler
);
1194 mutex_init(&fifo
->lock
);
1196 /* Get the resource of the Rx FIFO. */
1197 fifo
->rx_base
= devm_platform_ioremap_resource(pdev
, 0);
1198 if (IS_ERR(fifo
->rx_base
))
1199 return PTR_ERR(fifo
->rx_base
);
1201 /* Get the resource of the Tx FIFO. */
1202 fifo
->tx_base
= devm_platform_ioremap_resource(pdev
, 1);
1203 if (IS_ERR(fifo
->tx_base
))
1204 return PTR_ERR(fifo
->tx_base
);
1206 platform_set_drvdata(pdev
, fifo
);
1208 timer_setup(&fifo
->timer
, mlxbf_tmfifo_timer
, 0);
1210 for (i
= 0; i
< MLXBF_TM_MAX_IRQ
; i
++) {
1211 fifo
->irq_info
[i
].index
= i
;
1212 fifo
->irq_info
[i
].fifo
= fifo
;
1213 fifo
->irq_info
[i
].irq
= platform_get_irq(pdev
, i
);
1214 rc
= devm_request_irq(dev
, fifo
->irq_info
[i
].irq
,
1215 mlxbf_tmfifo_irq_handler
, 0,
1216 "tmfifo", &fifo
->irq_info
[i
]);
1218 dev_err(dev
, "devm_request_irq failed\n");
1219 fifo
->irq_info
[i
].irq
= 0;
1224 mlxbf_tmfifo_set_threshold(fifo
);
1226 /* Create the console vdev. */
1227 rc
= mlxbf_tmfifo_create_vdev(dev
, fifo
, VIRTIO_ID_CONSOLE
, 0, NULL
, 0);
1231 /* Create the network vdev. */
1232 memset(&net_config
, 0, sizeof(net_config
));
1233 net_config
.mtu
= ETH_DATA_LEN
;
1234 net_config
.status
= VIRTIO_NET_S_LINK_UP
;
1235 mlxbf_tmfifo_get_cfg_mac(net_config
.mac
);
1236 rc
= mlxbf_tmfifo_create_vdev(dev
, fifo
, VIRTIO_ID_NET
,
1237 MLXBF_TMFIFO_NET_FEATURES
, &net_config
,
1238 sizeof(net_config
));
1242 mod_timer(&fifo
->timer
, jiffies
+ MLXBF_TMFIFO_TIMER_INTERVAL
);
1244 fifo
->is_ready
= true;
1248 mlxbf_tmfifo_cleanup(fifo
);
1252 /* Device remove function. */
1253 static int mlxbf_tmfifo_remove(struct platform_device
*pdev
)
1255 struct mlxbf_tmfifo
*fifo
= platform_get_drvdata(pdev
);
1257 mlxbf_tmfifo_cleanup(fifo
);
1262 static const struct acpi_device_id mlxbf_tmfifo_acpi_match
[] = {
1266 MODULE_DEVICE_TABLE(acpi
, mlxbf_tmfifo_acpi_match
);
1268 static struct platform_driver mlxbf_tmfifo_driver
= {
1269 .probe
= mlxbf_tmfifo_probe
,
1270 .remove
= mlxbf_tmfifo_remove
,
1272 .name
= "bf-tmfifo",
1273 .acpi_match_table
= mlxbf_tmfifo_acpi_match
,
1277 module_platform_driver(mlxbf_tmfifo_driver
);
1279 MODULE_DESCRIPTION("Mellanox BlueField SoC TmFifo Driver");
1280 MODULE_LICENSE("GPL v2");
1281 MODULE_AUTHOR("Mellanox Technologies");