gpio: of: Break out OF-only code
[linux/fpc-iii.git] / drivers / platform / mellanox / mlxbf-tmfifo.c
blob9a5c9fd2dbc60cf2ab441ef48ba4d6db3cb4e119
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Mellanox BlueField SoC TmFifo driver
5 * Copyright (C) 2019 Mellanox Technologies
6 */
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"
26 /* Vring size. */
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
50 struct mlxbf_tmfifo;
52 /**
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
65 * @index: vring index
66 * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
67 * @fifo: pointer to the tmfifo structure
69 struct mlxbf_tmfifo_vring {
70 void *va;
71 dma_addr_t dma;
72 struct virtqueue *vq;
73 struct vring_desc *desc;
74 struct vring_desc *desc_head;
75 int cur_len;
76 int rem_len;
77 u32 pkt_len;
78 u16 next_avail;
79 int num;
80 int align;
81 int index;
82 int vdev_id;
83 struct mlxbf_tmfifo *fifo;
86 /* Interrupt types. */
87 enum {
88 MLXBF_TM_RX_LWM_IRQ,
89 MLXBF_TM_RX_HWM_IRQ,
90 MLXBF_TM_TX_LWM_IRQ,
91 MLXBF_TM_TX_HWM_IRQ,
92 MLXBF_TM_MAX_IRQ
95 /* Ring types (Rx & Tx). */
96 enum {
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;
117 u8 status;
118 u64 features;
119 struct mlxbf_tmfifo_vring vrings[MLXBF_TMFIFO_VRING_MAX];
120 union {
121 struct virtio_console_config cons;
122 struct virtio_net_config net;
123 } config;
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;
135 int irq;
136 int index;
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
151 * @vring: Tx/Rx ring
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;
160 int rx_fifo_size;
161 int tx_fifo_size;
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 */
168 bool is_ready;
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 {
180 u8 type;
181 __be16 len;
182 u8 unused[5];
183 } __packed __aligned(sizeof(u64));
186 * Default MAC.
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;
212 int i, size;
214 for (i = 0; i < ARRAY_SIZE(tm_vdev->vrings); i++) {
215 vring = &tm_vdev->vrings[i];
216 if (vring->va) {
217 size = vring_size(vring->num, vring->align);
218 dma_free_coherent(tm_vdev->vdev.dev.parent, size,
219 vring->va, vring->dma);
220 vring->va = NULL;
221 if (vring->vq) {
222 vring_del_virtqueue(vring->vq);
223 vring->vq = NULL;
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;
234 struct device *dev;
235 dma_addr_t dma;
236 int i, size;
237 void *va;
239 for (i = 0; i < ARRAY_SIZE(tm_vdev->vrings); i++) {
240 vring = &tm_vdev->vrings[i];
241 vring->fifo = fifo;
242 vring->num = MLXBF_TMFIFO_VRING_SIZE;
243 vring->align = SMP_CACHE_BYTES;
244 vring->index = i;
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);
250 if (!va) {
251 mlxbf_tmfifo_free_vrings(fifo, tm_vdev);
252 dev_err(dev->parent, "dma_alloc_coherent failed\n");
253 return -ENOMEM;
256 vring->va = va;
257 vring->dma = dma;
260 return 0;
263 /* Disable interrupts of the FIFO device. */
264 static void mlxbf_tmfifo_disable_irqs(struct mlxbf_tmfifo *fifo)
266 int i, irq;
268 for (i = 0; i < MLXBF_TM_MAX_IRQ; i++) {
269 irq = fifo->irq_info[i].irq;
270 fifo->irq_info[i].irq = 0;
271 disable_irq(irq);
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);
283 return IRQ_HANDLED;
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))
295 return NULL;
297 idx = vring->next_avail % vr->num;
298 head = virtio16_to_cpu(vdev, vr->avail->ring[idx]);
299 if (WARN_ON(head >= vr->num))
300 return NULL;
302 vring->next_avail++;
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;
313 u16 idx, vr_idx;
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.
325 mb();
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;
335 u32 len = 0, idx;
337 while (desc) {
338 len += virtio32_to_cpu(vdev, desc->len);
339 if (!(virtio16_to_cpu(vdev, desc->flags) & VRING_DESC_F_NEXT))
340 break;
341 idx = virtio16_to_cpu(vdev, desc->next);
342 desc = &vr->desc[idx];
345 return len;
348 static void mlxbf_tmfifo_release_pending_pkt(struct mlxbf_tmfifo_vring *vring)
350 struct vring_desc *desc_head;
351 u32 len = 0;
353 if (vring->desc_head) {
354 desc_head = vring->desc_head;
355 len = vring->pkt_len;
356 } else {
357 desc_head = mlxbf_tmfifo_get_next_desc(vring);
358 len = mlxbf_tmfifo_get_pkt_len(vring, desc_head);
361 if (desc_head)
362 mlxbf_tmfifo_release_desc(vring, desc_head, len);
364 vring->pkt_len = 0;
365 vring->desc = NULL;
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;
390 vring->desc = desc;
392 return 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);
399 int rx, tx;
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);
404 if (rx || tx)
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;
417 u32 len, idx, seg;
418 void *addr;
420 while (desc) {
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);
426 if (len <= seg) {
427 memcpy(cons->tx_buf.buf + cons->tx_buf.head, addr, len);
428 } else {
429 memcpy(cons->tx_buf.buf + cons->tx_buf.head, addr, seg);
430 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))
437 break;
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;
448 u32 len, avail;
450 desc = mlxbf_tmfifo_get_next_desc(vring);
451 while (desc) {
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);
458 break;
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)
470 u64 sts;
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)
479 int tx_reserve;
480 u32 count;
481 u64 sts;
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;
486 else
487 tx_reserve = 1;
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;
499 unsigned long flags;
500 int size, seg;
501 void *addr;
502 u64 data;
504 /* Return if not enough space available. */
505 if (avail < MLXBF_TMFIFO_DATA_MIN_WORDS)
506 return;
508 cons = fifo->vdev[VIRTIO_ID_CONSOLE];
509 if (!cons || !cons->tx_buf.buf)
510 return;
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);
515 if (size == 0)
516 return;
518 /* Adjust the size to available space. */
519 if (size + sizeof(hdr) > avail * sizeof(u64))
520 size = avail * sizeof(u64) - sizeof(hdr);
522 /* Write header. */
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);
530 while (size > 0) {
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));
537 } else {
538 memcpy(&data, addr, seg);
539 memcpy((u8 *)&data + seg, cons->tx_buf.buf,
540 sizeof(u64) - seg);
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;
547 size -= sizeof(u64);
548 } else {
549 cons->tx_buf.tail = (cons->tx_buf.tail + size) %
550 MLXBF_TMFIFO_CON_TX_BUF_SIZE;
551 size = 0;
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,
561 bool is_rx, int len)
563 struct virtio_device *vdev = vring->vq->vdev;
564 struct mlxbf_tmfifo *fifo = vring->fifo;
565 void *addr;
566 u64 data;
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. */
572 if (is_rx)
573 data = readq(fifo->rx_base + MLXBF_TMFIFO_RX_DATA);
575 if (vring->cur_len + sizeof(u64) <= len) {
576 /* The whole word. */
577 if (is_rx)
578 memcpy(addr + vring->cur_len, &data, sizeof(u64));
579 else
580 memcpy(&data, addr + vring->cur_len, sizeof(u64));
581 vring->cur_len += sizeof(u64);
582 } else {
583 /* Leftover bytes. */
584 if (is_rx)
585 memcpy(addr + vring->cur_len, &data,
586 len - vring->cur_len);
587 else
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. */
594 if (!is_rx)
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'
603 * flag is set.
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. */
615 if (is_rx) {
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). */
620 if (hdr.len == 0)
621 return;
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)
630 return;
631 } else {
632 vdev_id = VIRTIO_ID_CONSOLE;
633 hdr_len = 0;
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];
643 if (!tm_dev2)
644 return;
645 vring->desc = desc;
646 vring = &tm_dev2->vrings[MLXBF_TMFIFO_VRING_RX];
647 *vring_change = true;
649 vring->pkt_len = ntohs(hdr.len) + hdr_len;
650 } else {
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;
679 unsigned long flags;
680 u32 len, idx;
682 vdev = &fifo->vdev[vring->vdev_id]->vdev;
684 /* Get the descriptor of the next packet. */
685 if (!vring->desc) {
686 desc = mlxbf_tmfifo_get_next_pkt(vring, is_rx);
687 if (!desc)
688 return false;
689 } else {
690 desc = vring->desc;
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);
696 (*avail)--;
698 /* Return if new packet is for another ring. */
699 if (vring_change)
700 return false;
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);
712 (*avail)--;
715 /* Check again whether it's done. */
716 if (vring->cur_len == len) {
717 vring->cur_len = 0;
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);
730 desc = NULL;
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. */
741 vring->desc = desc;
743 return true;
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;
751 bool more;
753 fifo = vring->fifo;
755 /* Return if vdev is not ready. */
756 if (!fifo->vdev[devid])
757 return;
759 /* Return if another vring is running. */
760 if (fifo->vring[is_rx] && fifo->vring[is_rx] != vring)
761 return;
763 /* Only handle console and network for now. */
764 if (WARN_ON(devid != VIRTIO_ID_NET && devid != VIRTIO_ID_CONSOLE))
765 return;
767 do {
768 /* Get available FIFO space. */
769 if (avail == 0) {
770 if (is_rx)
771 avail = mlxbf_tmfifo_get_rx_avail(fifo);
772 else
773 avail = mlxbf_tmfifo_get_tx_avail(fifo, devid);
774 if (avail <= 0)
775 break;
778 /* Console output always comes from the Tx buffer. */
779 if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
780 mlxbf_tmfifo_console_tx(fifo, avail);
781 break;
784 /* Handle one descriptor. */
785 more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
786 } while (more);
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;
795 int i;
797 if (!test_and_clear_bit(irq_id, &fifo->pend_events) ||
798 !fifo->irq_info[irq_id].irq)
799 return;
801 for (i = 0; i < MLXBF_TMFIFO_VDEV_MAX; i++) {
802 tm_vdev = fifo->vdev[i];
803 if (tm_vdev) {
804 vring = &tm_vdev->vrings[queue_id];
805 if (vring->vq)
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);
817 if (!fifo->is_ready)
818 return;
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;
839 unsigned long flags;
841 fifo = vring->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
852 * worker handler.
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)) {
861 return true;
863 } else {
864 if (test_and_set_bit(MLXBF_TM_RX_HWM_IRQ, &fifo->pend_events))
865 return true;
868 schedule_work(&fifo->work);
870 return true;
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;
888 return 0;
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;
897 int i;
899 for (i = 0; i < ARRAY_SIZE(tm_vdev->vrings); i++) {
900 vring = &tm_vdev->vrings[i];
902 /* Release the pending packet. */
903 if (vring->desc)
904 mlxbf_tmfifo_release_pending_pkt(vring);
905 vq = vring->vq;
906 if (vq) {
907 vring->vq = NULL;
908 vring_del_virtqueue(vq);
913 /* Create and initialize the virtual queues. */
914 static int mlxbf_tmfifo_virtio_find_vqs(struct virtio_device *vdev,
915 unsigned int nvqs,
916 struct virtqueue *vqs[],
917 vq_callback_t *callbacks[],
918 const char * const names[],
919 const bool *ctx,
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;
925 int i, ret, size;
927 if (nvqs > ARRAY_SIZE(tm_vdev->vrings))
928 return -EINVAL;
930 for (i = 0; i < nvqs; ++i) {
931 if (!names[i]) {
932 ret = -EINVAL;
933 goto error;
935 vring = &tm_vdev->vrings[i];
937 /* zero vring */
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]);
944 if (!vq) {
945 dev_err(&vdev->dev, "vring_new_virtqueue failed\n");
946 ret = -ENOMEM;
947 goto error;
950 vqs[i] = vq;
951 vring->vq = vq;
952 vq->priv = vring;
955 return 0;
957 error:
958 mlxbf_tmfifo_virtio_del_vqs(vdev);
959 return ret;
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,
972 u8 status)
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);
984 tm_vdev->status = 0;
987 /* Read the value of a configuration field. */
988 static void mlxbf_tmfifo_virtio_get(struct virtio_device *vdev,
989 unsigned int offset,
990 void *buf,
991 unsigned int len)
993 struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
995 if ((u64)offset + len > sizeof(tm_vdev->config))
996 return;
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,
1004 const void *buf,
1005 unsigned int len)
1007 struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
1009 if ((u64)offset + len > sizeof(tm_vdev->config))
1010 return;
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);
1021 kfree(tm_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;
1044 int ret;
1046 mutex_lock(&fifo->lock);
1048 tm_vdev = fifo->vdev[vdev_id];
1049 if (tm_vdev) {
1050 dev_err(dev, "vdev %d already exists\n", vdev_id);
1051 ret = -EEXIST;
1052 goto fail;
1055 tm_vdev = kzalloc(sizeof(*tm_vdev), GFP_KERNEL);
1056 if (!tm_vdev) {
1057 ret = -ENOMEM;
1058 goto fail;
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;
1066 if (config)
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");
1071 ret = -ENOMEM;
1072 goto vdev_fail;
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,
1079 GFP_KERNEL);
1080 fifo->vdev[vdev_id] = tm_vdev;
1082 /* Register the virtio device. */
1083 ret = register_virtio_device(&tm_vdev->vdev);
1084 reg_dev = tm_vdev;
1085 if (ret) {
1086 dev_err(dev, "register_virtio_device failed\n");
1087 goto vdev_fail;
1090 mutex_unlock(&fifo->lock);
1091 return 0;
1093 vdev_fail:
1094 mlxbf_tmfifo_free_vrings(fifo, tm_vdev);
1095 fifo->vdev[vdev_id] = NULL;
1096 if (reg_dev)
1097 put_device(&tm_vdev->vdev.dev);
1098 else
1099 kfree(tm_vdev);
1100 fail:
1101 mutex_unlock(&fifo->lock);
1102 return ret;
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];
1114 if (tm_vdev) {
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);
1122 return 0;
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;
1130 u8 buf[ETH_ALEN];
1131 efi_status_t rc;
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);
1136 else
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)
1143 u64 ctl;
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)
1170 int i;
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;
1186 int i, rc;
1188 fifo = devm_kzalloc(dev, sizeof(*fifo), GFP_KERNEL);
1189 if (!fifo)
1190 return -ENOMEM;
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]);
1217 if (rc) {
1218 dev_err(dev, "devm_request_irq failed\n");
1219 fifo->irq_info[i].irq = 0;
1220 return rc;
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);
1228 if (rc)
1229 goto fail;
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));
1239 if (rc)
1240 goto fail;
1242 mod_timer(&fifo->timer, jiffies + MLXBF_TMFIFO_TIMER_INTERVAL);
1244 fifo->is_ready = true;
1245 return 0;
1247 fail:
1248 mlxbf_tmfifo_cleanup(fifo);
1249 return rc;
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);
1259 return 0;
1262 static const struct acpi_device_id mlxbf_tmfifo_acpi_match[] = {
1263 { "MLNXBF01", 0 },
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,
1271 .driver = {
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");