1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Mellanox BlueField SoC TmFifo driver
4  *
5  * Copyright (C) 2019 Mellanox Technologies
6  */
7 
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>
17 
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>
23 
24 #include "mlxbf-tmfifo-regs.h"
25 
26 /* Vring size. */
27 #define MLXBF_TMFIFO_VRING_SIZE			SZ_1K
28 
29 /* Console Tx buffer size. */
30 #define MLXBF_TMFIFO_CON_TX_BUF_SIZE		SZ_32K
31 
32 /* Console Tx buffer reserved space. */
33 #define MLXBF_TMFIFO_CON_TX_BUF_RSV_SIZE	8
34 
35 /* House-keeping timer interval. */
36 #define MLXBF_TMFIFO_TIMER_INTERVAL		(HZ / 10)
37 
38 /* Virtual devices sharing the TM FIFO. */
39 #define MLXBF_TMFIFO_VDEV_MAX		(VIRTIO_ID_CONSOLE + 1)
40 
41 /*
42  * Reserve 1/16 of TmFifo space, so console messages are not starved by
43  * the networking traffic.
44  */
45 #define MLXBF_TMFIFO_RESERVE_RATIO		16
46 
47 /* Message with data needs at least two words (for header & data). */
48 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
49 
50 /* ACPI UID for BlueField-3. */
51 #define TMFIFO_BF3_UID				1
52 
53 struct mlxbf_tmfifo;
54 
55 /**
56  * struct mlxbf_tmfifo_vring - Structure of the TmFifo virtual ring
57  * @va: virtual address of the ring
58  * @dma: dma address of the ring
59  * @vq: pointer to the virtio virtqueue
60  * @desc: current descriptor of the pending packet
61  * @desc_head: head descriptor of the pending packet
62  * @drop_desc: dummy desc for packet dropping
63  * @cur_len: processed length of the current descriptor
64  * @rem_len: remaining length of the pending packet
65  * @pkt_len: total length of the pending packet
66  * @next_avail: next avail descriptor id
67  * @num: vring size (number of descriptors)
68  * @align: vring alignment size
69  * @index: vring index
70  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
71  * @fifo: pointer to the tmfifo structure
72  */
73 struct mlxbf_tmfifo_vring {
74 	void *va;
75 	dma_addr_t dma;
76 	struct virtqueue *vq;
77 	struct vring_desc *desc;
78 	struct vring_desc *desc_head;
79 	struct vring_desc drop_desc;
80 	int cur_len;
81 	int rem_len;
82 	u32 pkt_len;
83 	u16 next_avail;
84 	int num;
85 	int align;
86 	int index;
87 	int vdev_id;
88 	struct mlxbf_tmfifo *fifo;
89 };
90 
91 /* Check whether vring is in drop mode. */
92 #define IS_VRING_DROP(_r) ({ \
93 	typeof(_r) (r) = (_r); \
94 	(r->desc_head == &r->drop_desc ? true : false); })
95 
96 /* A stub length to drop maximum length packet. */
97 #define VRING_DROP_DESC_MAX_LEN		GENMASK(15, 0)
98 
99 /* Interrupt types. */
100 enum {
101 	MLXBF_TM_RX_LWM_IRQ,
102 	MLXBF_TM_RX_HWM_IRQ,
103 	MLXBF_TM_TX_LWM_IRQ,
104 	MLXBF_TM_TX_HWM_IRQ,
105 	MLXBF_TM_MAX_IRQ
106 };
107 
108 /* Ring types (Rx & Tx). */
109 enum {
110 	MLXBF_TMFIFO_VRING_RX,
111 	MLXBF_TMFIFO_VRING_TX,
112 	MLXBF_TMFIFO_VRING_MAX
113 };
114 
115 /**
116  * struct mlxbf_tmfifo_vdev - Structure of the TmFifo virtual device
117  * @vdev: virtio device, in which the vdev.id.device field has the
118  *        VIRTIO_ID_xxx id to distinguish the virtual device.
119  * @status: status of the device
120  * @features: supported features of the device
121  * @vrings: array of tmfifo vrings of this device
122  * @config: non-anonymous union for cons and net
123  * @config.cons: virtual console config -
124  *               select if vdev.id.device is VIRTIO_ID_CONSOLE
125  * @config.net: virtual network config -
126  *              select if vdev.id.device is VIRTIO_ID_NET
127  * @tx_buf: tx buffer used to buffer data before writing into the FIFO
128  */
129 struct mlxbf_tmfifo_vdev {
130 	struct virtio_device vdev;
131 	u8 status;
132 	u64 features;
133 	struct mlxbf_tmfifo_vring vrings[MLXBF_TMFIFO_VRING_MAX];
134 	union {
135 		struct virtio_console_config cons;
136 		struct virtio_net_config net;
137 	} config;
138 	struct circ_buf tx_buf;
139 };
140 
141 /**
142  * struct mlxbf_tmfifo_irq_info - Structure of the interrupt information
143  * @fifo: pointer to the tmfifo structure
144  * @irq: interrupt number
145  * @index: index into the interrupt array
146  */
147 struct mlxbf_tmfifo_irq_info {
148 	struct mlxbf_tmfifo *fifo;
149 	int irq;
150 	int index;
151 };
152 
153 /**
154  * struct mlxbf_tmfifo_io - Structure of the TmFifo IO resource (for both rx & tx)
155  * @ctl: control register offset (TMFIFO_RX_CTL / TMFIFO_TX_CTL)
156  * @sts: status register offset (TMFIFO_RX_STS / TMFIFO_TX_STS)
157  * @data: data register offset (TMFIFO_RX_DATA / TMFIFO_TX_DATA)
158  */
159 struct mlxbf_tmfifo_io {
160 	void __iomem *ctl;
161 	void __iomem *sts;
162 	void __iomem *data;
163 };
164 
165 /**
166  * struct mlxbf_tmfifo - Structure of the TmFifo
167  * @vdev: array of the virtual devices running over the TmFifo
168  * @lock: lock to protect the TmFifo access
169  * @res0: mapped resource block 0
170  * @res1: mapped resource block 1
171  * @rx: rx io resource
172  * @tx: tx io resource
173  * @rx_fifo_size: number of entries of the Rx FIFO
174  * @tx_fifo_size: number of entries of the Tx FIFO
175  * @pend_events: pending bits for deferred events
176  * @irq_info: interrupt information
177  * @work: work struct for deferred process
178  * @timer: background timer
179  * @vring: Tx/Rx ring
180  * @spin_lock: Tx/Rx spin lock
181  * @is_ready: ready flag
182  */
183 struct mlxbf_tmfifo {
184 	struct mlxbf_tmfifo_vdev *vdev[MLXBF_TMFIFO_VDEV_MAX];
185 	struct mutex lock;		/* TmFifo lock */
186 	void __iomem *res0;
187 	void __iomem *res1;
188 	struct mlxbf_tmfifo_io rx;
189 	struct mlxbf_tmfifo_io tx;
190 	int rx_fifo_size;
191 	int tx_fifo_size;
192 	unsigned long pend_events;
193 	struct mlxbf_tmfifo_irq_info irq_info[MLXBF_TM_MAX_IRQ];
194 	struct work_struct work;
195 	struct timer_list timer;
196 	struct mlxbf_tmfifo_vring *vring[2];
197 	spinlock_t spin_lock[2];	/* spin lock */
198 	bool is_ready;
199 };
200 
201 /**
202  * struct mlxbf_tmfifo_msg_hdr - Structure of the TmFifo message header
203  * @type: message type
204  * @len: payload length in network byte order. Messages sent into the FIFO
205  *       will be read by the other side as data stream in the same byte order.
206  *       The length needs to be encoded into network order so both sides
207  *       could understand it.
208  */
209 struct mlxbf_tmfifo_msg_hdr {
210 	u8 type;
211 	__be16 len;
212 	/* private: */
213 	u8 unused[5];
214 } __packed __aligned(sizeof(u64));
215 
216 /*
217  * Default MAC.
218  * This MAC address will be read from EFI persistent variable if configured.
219  * It can also be reconfigured with standard Linux tools.
220  */
221 static u8 mlxbf_tmfifo_net_default_mac[ETH_ALEN] = {
222 	0x00, 0x1A, 0xCA, 0xFF, 0xFF, 0x01
223 };
224 
225 /* EFI variable name of the MAC address. */
226 static efi_char16_t mlxbf_tmfifo_efi_name[] = L"RshimMacAddr";
227 
228 /* Maximum L2 header length. */
229 #define MLXBF_TMFIFO_NET_L2_OVERHEAD	(ETH_HLEN + VLAN_HLEN)
230 
231 /* Supported virtio-net features. */
232 #define MLXBF_TMFIFO_NET_FEATURES \
233 	(BIT_ULL(VIRTIO_NET_F_MTU) | BIT_ULL(VIRTIO_NET_F_STATUS) | \
234 	 BIT_ULL(VIRTIO_NET_F_MAC))
235 
236 #define mlxbf_vdev_to_tmfifo(d) container_of(d, struct mlxbf_tmfifo_vdev, vdev)
237 
238 /* Free vrings of the FIFO device. */
239 static void mlxbf_tmfifo_free_vrings(struct mlxbf_tmfifo *fifo,
240 				     struct mlxbf_tmfifo_vdev *tm_vdev)
241 {
242 	struct mlxbf_tmfifo_vring *vring;
243 	int i, size;
244 
245 	for (i = 0; i < ARRAY_SIZE(tm_vdev->vrings); i++) {
246 		vring = &tm_vdev->vrings[i];
247 		if (vring->va) {
248 			size = vring_size(vring->num, vring->align);
249 			dma_free_coherent(tm_vdev->vdev.dev.parent, size,
250 					  vring->va, vring->dma);
251 			vring->va = NULL;
252 			if (vring->vq) {
253 				vring_del_virtqueue(vring->vq);
254 				vring->vq = NULL;
255 			}
256 		}
257 	}
258 }
259 
260 /* Allocate vrings for the FIFO. */
261 static int mlxbf_tmfifo_alloc_vrings(struct mlxbf_tmfifo *fifo,
262 				     struct mlxbf_tmfifo_vdev *tm_vdev)
263 {
264 	struct mlxbf_tmfifo_vring *vring;
265 	struct device *dev;
266 	dma_addr_t dma;
267 	int i, size;
268 	void *va;
269 
270 	for (i = 0; i < ARRAY_SIZE(tm_vdev->vrings); i++) {
271 		vring = &tm_vdev->vrings[i];
272 		vring->fifo = fifo;
273 		vring->num = MLXBF_TMFIFO_VRING_SIZE;
274 		vring->align = SMP_CACHE_BYTES;
275 		vring->index = i;
276 		vring->vdev_id = tm_vdev->vdev.id.device;
277 		vring->drop_desc.len = VRING_DROP_DESC_MAX_LEN;
278 		dev = &tm_vdev->vdev.dev;
279 
280 		size = vring_size(vring->num, vring->align);
281 		va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
282 		if (!va) {
283 			mlxbf_tmfifo_free_vrings(fifo, tm_vdev);
284 			dev_err(dev->parent, "dma_alloc_coherent failed\n");
285 			return -ENOMEM;
286 		}
287 
288 		vring->va = va;
289 		vring->dma = dma;
290 	}
291 
292 	return 0;
293 }
294 
295 /* Disable interrupts of the FIFO device. */
296 static void mlxbf_tmfifo_disable_irqs(struct mlxbf_tmfifo *fifo)
297 {
298 	int i, irq;
299 
300 	for (i = 0; i < MLXBF_TM_MAX_IRQ; i++) {
301 		irq = fifo->irq_info[i].irq;
302 		fifo->irq_info[i].irq = 0;
303 		disable_irq(irq);
304 	}
305 }
306 
307 /* Interrupt handler. */
308 static irqreturn_t mlxbf_tmfifo_irq_handler(int irq, void *arg)
309 {
310 	struct mlxbf_tmfifo_irq_info *irq_info = arg;
311 
312 	if (!test_and_set_bit(irq_info->index, &irq_info->fifo->pend_events))
313 		schedule_work(&irq_info->fifo->work);
314 
315 	return IRQ_HANDLED;
316 }
317 
318 /* Get the next packet descriptor from the vring. */
319 static struct vring_desc *
320 mlxbf_tmfifo_get_next_desc(struct mlxbf_tmfifo_vring *vring)
321 {
322 	const struct vring *vr = virtqueue_get_vring(vring->vq);
323 	struct virtio_device *vdev = vring->vq->vdev;
324 	unsigned int idx, head;
325 
326 	if (vring->next_avail == virtio16_to_cpu(vdev, vr->avail->idx))
327 		return NULL;
328 
329 	/* Make sure 'avail->idx' is visible already. */
330 	virtio_rmb(false);
331 
332 	idx = vring->next_avail % vr->num;
333 	head = virtio16_to_cpu(vdev, vr->avail->ring[idx]);
334 	if (WARN_ON(head >= vr->num))
335 		return NULL;
336 
337 	vring->next_avail++;
338 
339 	return &vr->desc[head];
340 }
341 
342 /* Release virtio descriptor. */
343 static void mlxbf_tmfifo_release_desc(struct mlxbf_tmfifo_vring *vring,
344 				      struct vring_desc *desc, u32 len)
345 {
346 	const struct vring *vr = virtqueue_get_vring(vring->vq);
347 	struct virtio_device *vdev = vring->vq->vdev;
348 	u16 idx, vr_idx;
349 
350 	vr_idx = virtio16_to_cpu(vdev, vr->used->idx);
351 	idx = vr_idx % vr->num;
352 	vr->used->ring[idx].id = cpu_to_virtio32(vdev, desc - vr->desc);
353 	vr->used->ring[idx].len = cpu_to_virtio32(vdev, len);
354 
355 	/*
356 	 * Virtio could poll and check the 'idx' to decide whether the desc is
357 	 * done or not. Add a memory barrier here to make sure the update above
358 	 * completes before updating the idx.
359 	 */
360 	virtio_mb(false);
361 	vr->used->idx = cpu_to_virtio16(vdev, vr_idx + 1);
362 }
363 
364 /* Get the total length of the descriptor chain. */
365 static u32 mlxbf_tmfifo_get_pkt_len(struct mlxbf_tmfifo_vring *vring,
366 				    struct vring_desc *desc)
367 {
368 	const struct vring *vr = virtqueue_get_vring(vring->vq);
369 	struct virtio_device *vdev = vring->vq->vdev;
370 	u32 len = 0, idx;
371 
372 	while (desc) {
373 		len += virtio32_to_cpu(vdev, desc->len);
374 		if (!(virtio16_to_cpu(vdev, desc->flags) & VRING_DESC_F_NEXT))
375 			break;
376 		idx = virtio16_to_cpu(vdev, desc->next);
377 		desc = &vr->desc[idx];
378 	}
379 
380 	return len;
381 }
382 
383 static void mlxbf_tmfifo_release_pkt(struct mlxbf_tmfifo_vring *vring)
384 {
385 	struct vring_desc *desc_head;
386 	u32 len = 0;
387 
388 	if (vring->desc_head) {
389 		desc_head = vring->desc_head;
390 		len = vring->pkt_len;
391 	} else {
392 		desc_head = mlxbf_tmfifo_get_next_desc(vring);
393 		len = mlxbf_tmfifo_get_pkt_len(vring, desc_head);
394 	}
395 
396 	if (desc_head)
397 		mlxbf_tmfifo_release_desc(vring, desc_head, len);
398 
399 	vring->pkt_len = 0;
400 	vring->desc = NULL;
401 	vring->desc_head = NULL;
402 }
403 
404 static void mlxbf_tmfifo_init_net_desc(struct mlxbf_tmfifo_vring *vring,
405 				       struct vring_desc *desc, bool is_rx)
406 {
407 	struct virtio_device *vdev = vring->vq->vdev;
408 	struct virtio_net_hdr *net_hdr;
409 
410 	net_hdr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));
411 	memset(net_hdr, 0, sizeof(*net_hdr));
412 }
413 
414 /* Get and initialize the next packet. */
415 static struct vring_desc *
416 mlxbf_tmfifo_get_next_pkt(struct mlxbf_tmfifo_vring *vring, bool is_rx)
417 {
418 	struct vring_desc *desc;
419 
420 	desc = mlxbf_tmfifo_get_next_desc(vring);
421 	if (desc && is_rx && vring->vdev_id == VIRTIO_ID_NET)
422 		mlxbf_tmfifo_init_net_desc(vring, desc, is_rx);
423 
424 	vring->desc_head = desc;
425 	vring->desc = desc;
426 
427 	return desc;
428 }
429 
430 /* House-keeping timer. */
431 static void mlxbf_tmfifo_timer(struct timer_list *t)
432 {
433 	struct mlxbf_tmfifo *fifo = container_of(t, struct mlxbf_tmfifo, timer);
434 	int rx, tx;
435 
436 	rx = !test_and_set_bit(MLXBF_TM_RX_HWM_IRQ, &fifo->pend_events);
437 	tx = !test_and_set_bit(MLXBF_TM_TX_LWM_IRQ, &fifo->pend_events);
438 
439 	if (rx || tx)
440 		schedule_work(&fifo->work);
441 
442 	mod_timer(&fifo->timer, jiffies + MLXBF_TMFIFO_TIMER_INTERVAL);
443 }
444 
445 /* Copy one console packet into the output buffer. */
446 static void mlxbf_tmfifo_console_output_one(struct mlxbf_tmfifo_vdev *cons,
447 					    struct mlxbf_tmfifo_vring *vring,
448 					    struct vring_desc *desc)
449 {
450 	const struct vring *vr = virtqueue_get_vring(vring->vq);
451 	struct virtio_device *vdev = &cons->vdev;
452 	u32 len, idx, seg;
453 	void *addr;
454 
455 	while (desc) {
456 		addr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));
457 		len = virtio32_to_cpu(vdev, desc->len);
458 
459 		seg = CIRC_SPACE_TO_END(cons->tx_buf.head, cons->tx_buf.tail,
460 					MLXBF_TMFIFO_CON_TX_BUF_SIZE);
461 		if (len <= seg) {
462 			memcpy(cons->tx_buf.buf + cons->tx_buf.head, addr, len);
463 		} else {
464 			memcpy(cons->tx_buf.buf + cons->tx_buf.head, addr, seg);
465 			addr += seg;
466 			memcpy(cons->tx_buf.buf, addr, len - seg);
467 		}
468 		cons->tx_buf.head = (cons->tx_buf.head + len) %
469 			MLXBF_TMFIFO_CON_TX_BUF_SIZE;
470 
471 		if (!(virtio16_to_cpu(vdev, desc->flags) & VRING_DESC_F_NEXT))
472 			break;
473 		idx = virtio16_to_cpu(vdev, desc->next);
474 		desc = &vr->desc[idx];
475 	}
476 }
477 
478 /* Copy console data into the output buffer. */
479 static void mlxbf_tmfifo_console_output(struct mlxbf_tmfifo_vdev *cons,
480 					struct mlxbf_tmfifo_vring *vring)
481 {
482 	struct vring_desc *desc;
483 	u32 len, avail;
484 
485 	desc = mlxbf_tmfifo_get_next_desc(vring);
486 	while (desc) {
487 		/* Release the packet if not enough space. */
488 		len = mlxbf_tmfifo_get_pkt_len(vring, desc);
489 		avail = CIRC_SPACE(cons->tx_buf.head, cons->tx_buf.tail,
490 				   MLXBF_TMFIFO_CON_TX_BUF_SIZE);
491 		if (len + MLXBF_TMFIFO_CON_TX_BUF_RSV_SIZE > avail) {
492 			mlxbf_tmfifo_release_desc(vring, desc, len);
493 			break;
494 		}
495 
496 		mlxbf_tmfifo_console_output_one(cons, vring, desc);
497 		mlxbf_tmfifo_release_desc(vring, desc, len);
498 		desc = mlxbf_tmfifo_get_next_desc(vring);
499 	}
500 }
501 
502 /* Get the number of available words in Rx FIFO for receiving. */
503 static int mlxbf_tmfifo_get_rx_avail(struct mlxbf_tmfifo *fifo)
504 {
505 	u64 sts;
506 
507 	sts = readq(fifo->rx.sts);
508 	return FIELD_GET(MLXBF_TMFIFO_RX_STS__COUNT_MASK, sts);
509 }
510 
511 /* Get the number of available words in the TmFifo for sending. */
512 static int mlxbf_tmfifo_get_tx_avail(struct mlxbf_tmfifo *fifo, int vdev_id)
513 {
514 	int tx_reserve;
515 	u32 count;
516 	u64 sts;
517 
518 	/* Reserve some room in FIFO for console messages. */
519 	if (vdev_id == VIRTIO_ID_NET)
520 		tx_reserve = fifo->tx_fifo_size / MLXBF_TMFIFO_RESERVE_RATIO;
521 	else
522 		tx_reserve = 1;
523 
524 	sts = readq(fifo->tx.sts);
525 	count = FIELD_GET(MLXBF_TMFIFO_TX_STS__COUNT_MASK, sts);
526 	return fifo->tx_fifo_size - tx_reserve - count;
527 }
528 
529 /* Console Tx (move data from the output buffer into the TmFifo). */
530 static void mlxbf_tmfifo_console_tx(struct mlxbf_tmfifo *fifo, int avail)
531 {
532 	struct mlxbf_tmfifo_msg_hdr hdr;
533 	struct mlxbf_tmfifo_vdev *cons;
534 	unsigned long flags;
535 	int size, seg;
536 	void *addr;
537 	u64 data;
538 
539 	/* Return if not enough space available. */
540 	if (avail < MLXBF_TMFIFO_DATA_MIN_WORDS)
541 		return;
542 
543 	cons = fifo->vdev[VIRTIO_ID_CONSOLE];
544 	if (!cons || !cons->tx_buf.buf)
545 		return;
546 
547 	/* Return if no data to send. */
548 	size = CIRC_CNT(cons->tx_buf.head, cons->tx_buf.tail,
549 			MLXBF_TMFIFO_CON_TX_BUF_SIZE);
550 	if (size == 0)
551 		return;
552 
553 	/* Adjust the size to available space. */
554 	if (size + sizeof(hdr) > avail * sizeof(u64))
555 		size = avail * sizeof(u64) - sizeof(hdr);
556 
557 	/* Write header. */
558 	hdr.type = VIRTIO_ID_CONSOLE;
559 	hdr.len = htons(size);
560 	writeq(*(u64 *)&hdr, fifo->tx.data);
561 
562 	/* Use spin-lock to protect the 'cons->tx_buf'. */
563 	spin_lock_irqsave(&fifo->spin_lock[0], flags);
564 
565 	while (size > 0) {
566 		addr = cons->tx_buf.buf + cons->tx_buf.tail;
567 
568 		seg = CIRC_CNT_TO_END(cons->tx_buf.head, cons->tx_buf.tail,
569 				      MLXBF_TMFIFO_CON_TX_BUF_SIZE);
570 		if (seg >= sizeof(u64)) {
571 			memcpy(&data, addr, sizeof(u64));
572 		} else {
573 			memcpy(&data, addr, seg);
574 			memcpy((u8 *)&data + seg, cons->tx_buf.buf,
575 			       sizeof(u64) - seg);
576 		}
577 		writeq(data, fifo->tx.data);
578 
579 		if (size >= sizeof(u64)) {
580 			cons->tx_buf.tail = (cons->tx_buf.tail + sizeof(u64)) %
581 				MLXBF_TMFIFO_CON_TX_BUF_SIZE;
582 			size -= sizeof(u64);
583 		} else {
584 			cons->tx_buf.tail = (cons->tx_buf.tail + size) %
585 				MLXBF_TMFIFO_CON_TX_BUF_SIZE;
586 			size = 0;
587 		}
588 	}
589 
590 	spin_unlock_irqrestore(&fifo->spin_lock[0], flags);
591 }
592 
593 /* Rx/Tx one word in the descriptor buffer. */
594 static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring,
595 				   struct vring_desc *desc,
596 				   bool is_rx, int len)
597 {
598 	struct virtio_device *vdev = vring->vq->vdev;
599 	struct mlxbf_tmfifo *fifo = vring->fifo;
600 	void *addr;
601 	u64 data;
602 
603 	/* Get the buffer address of this desc. */
604 	addr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));
605 
606 	/* Read a word from FIFO for Rx. */
607 	if (is_rx)
608 		data = readq(fifo->rx.data);
609 
610 	if (vring->cur_len + sizeof(u64) <= len) {
611 		/* The whole word. */
612 		if (!IS_VRING_DROP(vring)) {
613 			if (is_rx)
614 				memcpy(addr + vring->cur_len, &data,
615 				       sizeof(u64));
616 			else
617 				memcpy(&data, addr + vring->cur_len,
618 				       sizeof(u64));
619 		}
620 		vring->cur_len += sizeof(u64);
621 	} else {
622 		/* Leftover bytes. */
623 		if (!IS_VRING_DROP(vring)) {
624 			if (is_rx)
625 				memcpy(addr + vring->cur_len, &data,
626 				       len - vring->cur_len);
627 			else
628 				memcpy(&data, addr + vring->cur_len,
629 				       len - vring->cur_len);
630 		}
631 		vring->cur_len = len;
632 	}
633 
634 	/* Write the word into FIFO for Tx. */
635 	if (!is_rx)
636 		writeq(data, fifo->tx.data);
637 }
638 
639 /*
640  * Rx/Tx packet header.
641  *
642  * In Rx case, the packet might be found to belong to a different vring since
643  * the TmFifo is shared by different services. In such case, the 'vring_change'
644  * flag is set.
645  */
646 static void mlxbf_tmfifo_rxtx_header(struct mlxbf_tmfifo_vring *vring,
647 				     struct vring_desc **desc,
648 				     bool is_rx, bool *vring_change)
649 {
650 	struct mlxbf_tmfifo *fifo = vring->fifo;
651 	struct virtio_net_config *config;
652 	struct mlxbf_tmfifo_msg_hdr hdr;
653 	int vdev_id, hdr_len;
654 	bool drop_rx = false;
655 
656 	/* Read/Write packet header. */
657 	if (is_rx) {
658 		/* Drain one word from the FIFO. */
659 		*(u64 *)&hdr = readq(fifo->rx.data);
660 
661 		/* Skip the length 0 packets (keepalive). */
662 		if (hdr.len == 0)
663 			return;
664 
665 		/* Check packet type. */
666 		if (hdr.type == VIRTIO_ID_NET) {
667 			vdev_id = VIRTIO_ID_NET;
668 			hdr_len = sizeof(struct virtio_net_hdr);
669 			config = &fifo->vdev[vdev_id]->config.net;
670 			/* A legacy-only interface for now. */
671 			if (ntohs(hdr.len) >
672 			    __virtio16_to_cpu(virtio_legacy_is_little_endian(),
673 					      config->mtu) +
674 					      MLXBF_TMFIFO_NET_L2_OVERHEAD)
675 				drop_rx = true;
676 		} else {
677 			vdev_id = VIRTIO_ID_CONSOLE;
678 			hdr_len = 0;
679 		}
680 
681 		/*
682 		 * Check whether the new packet still belongs to this vring.
683 		 * If not, update the pkt_len of the new vring.
684 		 */
685 		if (vdev_id != vring->vdev_id) {
686 			struct mlxbf_tmfifo_vdev *tm_dev2 = fifo->vdev[vdev_id];
687 
688 			if (!tm_dev2)
689 				return;
690 			vring->desc = *desc;
691 			vring = &tm_dev2->vrings[MLXBF_TMFIFO_VRING_RX];
692 			*vring_change = true;
693 		}
694 
695 		if (drop_rx && !IS_VRING_DROP(vring)) {
696 			if (vring->desc_head)
697 				mlxbf_tmfifo_release_pkt(vring);
698 			*desc = &vring->drop_desc;
699 			vring->desc_head = *desc;
700 			vring->desc = *desc;
701 		}
702 
703 		vring->pkt_len = ntohs(hdr.len) + hdr_len;
704 	} else {
705 		/* Network virtio has an extra header. */
706 		hdr_len = (vring->vdev_id == VIRTIO_ID_NET) ?
707 			   sizeof(struct virtio_net_hdr) : 0;
708 		vring->pkt_len = mlxbf_tmfifo_get_pkt_len(vring, *desc);
709 		hdr.type = (vring->vdev_id == VIRTIO_ID_NET) ?
710 			    VIRTIO_ID_NET : VIRTIO_ID_CONSOLE;
711 		hdr.len = htons(vring->pkt_len - hdr_len);
712 		writeq(*(u64 *)&hdr, fifo->tx.data);
713 	}
714 
715 	vring->cur_len = hdr_len;
716 	vring->rem_len = vring->pkt_len;
717 	fifo->vring[is_rx] = vring;
718 }
719 
720 /*
721  * Rx/Tx one descriptor.
722  *
723  * Return true to indicate more data available.
724  */
725 static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,
726 				       bool is_rx, int *avail)
727 {
728 	const struct vring *vr = virtqueue_get_vring(vring->vq);
729 	struct mlxbf_tmfifo *fifo = vring->fifo;
730 	struct virtio_device *vdev;
731 	bool vring_change = false;
732 	struct vring_desc *desc;
733 	unsigned long flags;
734 	u32 len, idx;
735 
736 	vdev = &fifo->vdev[vring->vdev_id]->vdev;
737 
738 	/* Get the descriptor of the next packet. */
739 	if (!vring->desc) {
740 		desc = mlxbf_tmfifo_get_next_pkt(vring, is_rx);
741 		if (!desc) {
742 			/* Drop next Rx packet to avoid stuck. */
743 			if (is_rx) {
744 				desc = &vring->drop_desc;
745 				vring->desc_head = desc;
746 				vring->desc = desc;
747 			} else {
748 				return false;
749 			}
750 		}
751 	} else {
752 		desc = vring->desc;
753 	}
754 
755 	/* Beginning of a packet. Start to Rx/Tx packet header. */
756 	if (vring->pkt_len == 0) {
757 		mlxbf_tmfifo_rxtx_header(vring, &desc, is_rx, &vring_change);
758 		(*avail)--;
759 
760 		/* Return if new packet is for another ring. */
761 		if (vring_change)
762 			return false;
763 		goto mlxbf_tmfifo_desc_done;
764 	}
765 
766 	/* Get the length of this desc. */
767 	len = virtio32_to_cpu(vdev, desc->len);
768 	if (len > vring->rem_len)
769 		len = vring->rem_len;
770 
771 	/* Rx/Tx one word (8 bytes) if not done. */
772 	if (vring->cur_len < len) {
773 		mlxbf_tmfifo_rxtx_word(vring, desc, is_rx, len);
774 		(*avail)--;
775 	}
776 
777 	/* Check again whether it's done. */
778 	if (vring->cur_len == len) {
779 		vring->cur_len = 0;
780 		vring->rem_len -= len;
781 
782 		/* Get the next desc on the chain. */
783 		if (!IS_VRING_DROP(vring) && vring->rem_len > 0 &&
784 		    (virtio16_to_cpu(vdev, desc->flags) & VRING_DESC_F_NEXT)) {
785 			idx = virtio16_to_cpu(vdev, desc->next);
786 			desc = &vr->desc[idx];
787 			goto mlxbf_tmfifo_desc_done;
788 		}
789 
790 		/* Done and release the packet. */
791 		desc = NULL;
792 		fifo->vring[is_rx] = NULL;
793 		if (!IS_VRING_DROP(vring)) {
794 			mlxbf_tmfifo_release_pkt(vring);
795 		} else {
796 			vring->pkt_len = 0;
797 			vring->desc_head = NULL;
798 			vring->desc = NULL;
799 			return false;
800 		}
801 
802 		/*
803 		 * Make sure the load/store are in order before
804 		 * returning back to virtio.
805 		 */
806 		virtio_mb(false);
807 
808 		/* Notify upper layer that packet is done. */
809 		spin_lock_irqsave(&fifo->spin_lock[is_rx], flags);
810 		vring_interrupt(0, vring->vq);
811 		spin_unlock_irqrestore(&fifo->spin_lock[is_rx], flags);
812 	}
813 
814 mlxbf_tmfifo_desc_done:
815 	/* Save the current desc. */
816 	vring->desc = desc;
817 
818 	return true;
819 }
820 
821 /* Rx & Tx processing of a queue. */
822 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
823 {
824 	int avail = 0, devid = vring->vdev_id;
825 	struct mlxbf_tmfifo *fifo;
826 	bool more;
827 
828 	fifo = vring->fifo;
829 
830 	/* Return if vdev is not ready. */
831 	if (!fifo || !fifo->vdev[devid])
832 		return;
833 
834 	/* Return if another vring is running. */
835 	if (fifo->vring[is_rx] && fifo->vring[is_rx] != vring)
836 		return;
837 
838 	/* Only handle console and network for now. */
839 	if (WARN_ON(devid != VIRTIO_ID_NET && devid != VIRTIO_ID_CONSOLE))
840 		return;
841 
842 	do {
843 		/* Get available FIFO space. */
844 		if (avail == 0) {
845 			if (is_rx)
846 				avail = mlxbf_tmfifo_get_rx_avail(fifo);
847 			else
848 				avail = mlxbf_tmfifo_get_tx_avail(fifo, devid);
849 			if (avail <= 0)
850 				break;
851 		}
852 
853 		/* Console output always comes from the Tx buffer. */
854 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
855 			mlxbf_tmfifo_console_tx(fifo, avail);
856 			break;
857 		}
858 
859 		/* Handle one descriptor. */
860 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
861 	} while (more);
862 }
863 
864 /* Handle Rx or Tx queues. */
865 static void mlxbf_tmfifo_work_rxtx(struct mlxbf_tmfifo *fifo, int queue_id,
866 				   int irq_id, bool is_rx)
867 {
868 	struct mlxbf_tmfifo_vdev *tm_vdev;
869 	struct mlxbf_tmfifo_vring *vring;
870 	int i;
871 
872 	if (!test_and_clear_bit(irq_id, &fifo->pend_events) ||
873 	    !fifo->irq_info[irq_id].irq)
874 		return;
875 
876 	for (i = 0; i < MLXBF_TMFIFO_VDEV_MAX; i++) {
877 		tm_vdev = fifo->vdev[i];
878 		if (tm_vdev) {
879 			vring = &tm_vdev->vrings[queue_id];
880 			if (vring->vq)
881 				mlxbf_tmfifo_rxtx(vring, is_rx);
882 		}
883 	}
884 }
885 
886 /* Work handler for Rx and Tx case. */
887 static void mlxbf_tmfifo_work_handler(struct work_struct *work)
888 {
889 	struct mlxbf_tmfifo *fifo;
890 
891 	fifo = container_of(work, struct mlxbf_tmfifo, work);
892 	if (!fifo->is_ready)
893 		return;
894 
895 	mutex_lock(&fifo->lock);
896 
897 	/* Tx (Send data to the TmFifo). */
898 	mlxbf_tmfifo_work_rxtx(fifo, MLXBF_TMFIFO_VRING_TX,
899 			       MLXBF_TM_TX_LWM_IRQ, false);
900 
901 	/* Rx (Receive data from the TmFifo). */
902 	mlxbf_tmfifo_work_rxtx(fifo, MLXBF_TMFIFO_VRING_RX,
903 			       MLXBF_TM_RX_HWM_IRQ, true);
904 
905 	mutex_unlock(&fifo->lock);
906 }
907 
908 /* The notify function is called when new buffers are posted. */
909 static bool mlxbf_tmfifo_virtio_notify(struct virtqueue *vq)
910 {
911 	struct mlxbf_tmfifo_vring *vring = vq->priv;
912 	struct mlxbf_tmfifo_vdev *tm_vdev;
913 	struct mlxbf_tmfifo *fifo;
914 	unsigned long flags;
915 
916 	fifo = vring->fifo;
917 
918 	/*
919 	 * Virtio maintains vrings in pairs, even number ring for Rx
920 	 * and odd number ring for Tx.
921 	 */
922 	if (vring->index & BIT(0)) {
923 		/*
924 		 * Console could make blocking call with interrupts disabled.
925 		 * In such case, the vring needs to be served right away. For
926 		 * other cases, just set the TX LWM bit to start Tx in the
927 		 * worker handler.
928 		 */
929 		if (vring->vdev_id == VIRTIO_ID_CONSOLE) {
930 			spin_lock_irqsave(&fifo->spin_lock[0], flags);
931 			tm_vdev = fifo->vdev[VIRTIO_ID_CONSOLE];
932 			mlxbf_tmfifo_console_output(tm_vdev, vring);
933 			spin_unlock_irqrestore(&fifo->spin_lock[0], flags);
934 			set_bit(MLXBF_TM_TX_LWM_IRQ, &fifo->pend_events);
935 		} else if (test_and_set_bit(MLXBF_TM_TX_LWM_IRQ,
936 					    &fifo->pend_events)) {
937 			return true;
938 		}
939 	} else {
940 		if (test_and_set_bit(MLXBF_TM_RX_HWM_IRQ, &fifo->pend_events))
941 			return true;
942 	}
943 
944 	schedule_work(&fifo->work);
945 
946 	return true;
947 }
948 
949 /* Get the array of feature bits for this device. */
950 static u64 mlxbf_tmfifo_virtio_get_features(struct virtio_device *vdev)
951 {
952 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
953 
954 	return tm_vdev->features;
955 }
956 
957 /* Confirm device features to use. */
958 static int mlxbf_tmfifo_virtio_finalize_features(struct virtio_device *vdev)
959 {
960 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
961 
962 	tm_vdev->features = vdev->features;
963 
964 	return 0;
965 }
966 
967 /* Free virtqueues found by find_vqs(). */
968 static void mlxbf_tmfifo_virtio_del_vqs(struct virtio_device *vdev)
969 {
970 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
971 	struct mlxbf_tmfifo_vring *vring;
972 	struct virtqueue *vq;
973 	int i;
974 
975 	for (i = 0; i < ARRAY_SIZE(tm_vdev->vrings); i++) {
976 		vring = &tm_vdev->vrings[i];
977 
978 		/* Release the pending packet. */
979 		if (vring->desc)
980 			mlxbf_tmfifo_release_pkt(vring);
981 		vq = vring->vq;
982 		if (vq) {
983 			vring->vq = NULL;
984 			vring_del_virtqueue(vq);
985 		}
986 	}
987 }
988 
989 /* Create and initialize the virtual queues. */
990 static int mlxbf_tmfifo_virtio_find_vqs(struct virtio_device *vdev,
991 					unsigned int nvqs,
992 					struct virtqueue *vqs[],
993 					vq_callback_t *callbacks[],
994 					const char * const names[],
995 					const bool *ctx,
996 					struct irq_affinity *desc)
997 {
998 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
999 	struct mlxbf_tmfifo_vring *vring;
1000 	struct virtqueue *vq;
1001 	int i, ret, size;
1002 
1003 	if (nvqs > ARRAY_SIZE(tm_vdev->vrings))
1004 		return -EINVAL;
1005 
1006 	for (i = 0; i < nvqs; ++i) {
1007 		if (!names[i]) {
1008 			ret = -EINVAL;
1009 			goto error;
1010 		}
1011 		vring = &tm_vdev->vrings[i];
1012 
1013 		/* zero vring */
1014 		size = vring_size(vring->num, vring->align);
1015 		memset(vring->va, 0, size);
1016 		vq = vring_new_virtqueue(i, vring->num, vring->align, vdev,
1017 					 false, false, vring->va,
1018 					 mlxbf_tmfifo_virtio_notify,
1019 					 callbacks[i], names[i]);
1020 		if (!vq) {
1021 			dev_err(&vdev->dev, "vring_new_virtqueue failed\n");
1022 			ret = -ENOMEM;
1023 			goto error;
1024 		}
1025 
1026 		vq->num_max = vring->num;
1027 
1028 		vq->priv = vring;
1029 
1030 		/* Make vq update visible before using it. */
1031 		virtio_mb(false);
1032 
1033 		vqs[i] = vq;
1034 		vring->vq = vq;
1035 	}
1036 
1037 	return 0;
1038 
1039 error:
1040 	mlxbf_tmfifo_virtio_del_vqs(vdev);
1041 	return ret;
1042 }
1043 
1044 /* Read the status byte. */
1045 static u8 mlxbf_tmfifo_virtio_get_status(struct virtio_device *vdev)
1046 {
1047 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
1048 
1049 	return tm_vdev->status;
1050 }
1051 
1052 /* Write the status byte. */
1053 static void mlxbf_tmfifo_virtio_set_status(struct virtio_device *vdev,
1054 					   u8 status)
1055 {
1056 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
1057 
1058 	tm_vdev->status = status;
1059 }
1060 
1061 /* Reset the device. Not much here for now. */
1062 static void mlxbf_tmfifo_virtio_reset(struct virtio_device *vdev)
1063 {
1064 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
1065 
1066 	tm_vdev->status = 0;
1067 }
1068 
1069 /* Read the value of a configuration field. */
1070 static void mlxbf_tmfifo_virtio_get(struct virtio_device *vdev,
1071 				    unsigned int offset,
1072 				    void *buf,
1073 				    unsigned int len)
1074 {
1075 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
1076 
1077 	if ((u64)offset + len > sizeof(tm_vdev->config))
1078 		return;
1079 
1080 	memcpy(buf, (u8 *)&tm_vdev->config + offset, len);
1081 }
1082 
1083 /* Write the value of a configuration field. */
1084 static void mlxbf_tmfifo_virtio_set(struct virtio_device *vdev,
1085 				    unsigned int offset,
1086 				    const void *buf,
1087 				    unsigned int len)
1088 {
1089 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
1090 
1091 	if ((u64)offset + len > sizeof(tm_vdev->config))
1092 		return;
1093 
1094 	memcpy((u8 *)&tm_vdev->config + offset, buf, len);
1095 }
1096 
1097 static void tmfifo_virtio_dev_release(struct device *device)
1098 {
1099 	struct virtio_device *vdev =
1100 			container_of(device, struct virtio_device, dev);
1101 	struct mlxbf_tmfifo_vdev *tm_vdev = mlxbf_vdev_to_tmfifo(vdev);
1102 
1103 	kfree(tm_vdev);
1104 }
1105 
1106 /* Virtio config operations. */
1107 static const struct virtio_config_ops mlxbf_tmfifo_virtio_config_ops = {
1108 	.get_features = mlxbf_tmfifo_virtio_get_features,
1109 	.finalize_features = mlxbf_tmfifo_virtio_finalize_features,
1110 	.find_vqs = mlxbf_tmfifo_virtio_find_vqs,
1111 	.del_vqs = mlxbf_tmfifo_virtio_del_vqs,
1112 	.reset = mlxbf_tmfifo_virtio_reset,
1113 	.set_status = mlxbf_tmfifo_virtio_set_status,
1114 	.get_status = mlxbf_tmfifo_virtio_get_status,
1115 	.get = mlxbf_tmfifo_virtio_get,
1116 	.set = mlxbf_tmfifo_virtio_set,
1117 };
1118 
1119 /* Create vdev for the FIFO. */
1120 static int mlxbf_tmfifo_create_vdev(struct device *dev,
1121 				    struct mlxbf_tmfifo *fifo,
1122 				    int vdev_id, u64 features,
1123 				    void *config, u32 size)
1124 {
1125 	struct mlxbf_tmfifo_vdev *tm_vdev, *reg_dev = NULL;
1126 	int ret;
1127 
1128 	mutex_lock(&fifo->lock);
1129 
1130 	tm_vdev = fifo->vdev[vdev_id];
1131 	if (tm_vdev) {
1132 		dev_err(dev, "vdev %d already exists\n", vdev_id);
1133 		ret = -EEXIST;
1134 		goto fail;
1135 	}
1136 
1137 	tm_vdev = kzalloc(sizeof(*tm_vdev), GFP_KERNEL);
1138 	if (!tm_vdev) {
1139 		ret = -ENOMEM;
1140 		goto fail;
1141 	}
1142 
1143 	tm_vdev->vdev.id.device = vdev_id;
1144 	tm_vdev->vdev.config = &mlxbf_tmfifo_virtio_config_ops;
1145 	tm_vdev->vdev.dev.parent = dev;
1146 	tm_vdev->vdev.dev.release = tmfifo_virtio_dev_release;
1147 	tm_vdev->features = features;
1148 	if (config)
1149 		memcpy(&tm_vdev->config, config, size);
1150 
1151 	if (mlxbf_tmfifo_alloc_vrings(fifo, tm_vdev)) {
1152 		dev_err(dev, "unable to allocate vring\n");
1153 		ret = -ENOMEM;
1154 		goto vdev_fail;
1155 	}
1156 
1157 	/* Allocate an output buffer for the console device. */
1158 	if (vdev_id == VIRTIO_ID_CONSOLE)
1159 		tm_vdev->tx_buf.buf = devm_kmalloc(dev,
1160 						   MLXBF_TMFIFO_CON_TX_BUF_SIZE,
1161 						   GFP_KERNEL);
1162 	fifo->vdev[vdev_id] = tm_vdev;
1163 
1164 	/* Register the virtio device. */
1165 	ret = register_virtio_device(&tm_vdev->vdev);
1166 	reg_dev = tm_vdev;
1167 	if (ret) {
1168 		dev_err(dev, "register_virtio_device failed\n");
1169 		goto vdev_fail;
1170 	}
1171 
1172 	mutex_unlock(&fifo->lock);
1173 	return 0;
1174 
1175 vdev_fail:
1176 	mlxbf_tmfifo_free_vrings(fifo, tm_vdev);
1177 	fifo->vdev[vdev_id] = NULL;
1178 	if (reg_dev)
1179 		put_device(&tm_vdev->vdev.dev);
1180 	else
1181 		kfree(tm_vdev);
1182 fail:
1183 	mutex_unlock(&fifo->lock);
1184 	return ret;
1185 }
1186 
1187 /* Delete vdev for the FIFO. */
1188 static int mlxbf_tmfifo_delete_vdev(struct mlxbf_tmfifo *fifo, int vdev_id)
1189 {
1190 	struct mlxbf_tmfifo_vdev *tm_vdev;
1191 
1192 	mutex_lock(&fifo->lock);
1193 
1194 	/* Unregister vdev. */
1195 	tm_vdev = fifo->vdev[vdev_id];
1196 	if (tm_vdev) {
1197 		unregister_virtio_device(&tm_vdev->vdev);
1198 		mlxbf_tmfifo_free_vrings(fifo, tm_vdev);
1199 		fifo->vdev[vdev_id] = NULL;
1200 	}
1201 
1202 	mutex_unlock(&fifo->lock);
1203 
1204 	return 0;
1205 }
1206 
1207 /* Read the configured network MAC address from efi variable. */
1208 static void mlxbf_tmfifo_get_cfg_mac(u8 *mac)
1209 {
1210 	efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
1211 	unsigned long size = ETH_ALEN;
1212 	u8 buf[ETH_ALEN];
1213 	efi_status_t rc;
1214 
1215 	rc = efi.get_variable(mlxbf_tmfifo_efi_name, &guid, NULL, &size, buf);
1216 	if (rc == EFI_SUCCESS && size == ETH_ALEN)
1217 		ether_addr_copy(mac, buf);
1218 	else
1219 		ether_addr_copy(mac, mlxbf_tmfifo_net_default_mac);
1220 }
1221 
1222 /* Set TmFifo thresolds which is used to trigger interrupts. */
1223 static void mlxbf_tmfifo_set_threshold(struct mlxbf_tmfifo *fifo)
1224 {
1225 	u64 ctl;
1226 
1227 	/* Get Tx FIFO size and set the low/high watermark. */
1228 	ctl = readq(fifo->tx.ctl);
1229 	fifo->tx_fifo_size =
1230 		FIELD_GET(MLXBF_TMFIFO_TX_CTL__MAX_ENTRIES_MASK, ctl);
1231 	ctl = (ctl & ~MLXBF_TMFIFO_TX_CTL__LWM_MASK) |
1232 		FIELD_PREP(MLXBF_TMFIFO_TX_CTL__LWM_MASK,
1233 			   fifo->tx_fifo_size / 2);
1234 	ctl = (ctl & ~MLXBF_TMFIFO_TX_CTL__HWM_MASK) |
1235 		FIELD_PREP(MLXBF_TMFIFO_TX_CTL__HWM_MASK,
1236 			   fifo->tx_fifo_size - 1);
1237 	writeq(ctl, fifo->tx.ctl);
1238 
1239 	/* Get Rx FIFO size and set the low/high watermark. */
1240 	ctl = readq(fifo->rx.ctl);
1241 	fifo->rx_fifo_size =
1242 		FIELD_GET(MLXBF_TMFIFO_RX_CTL__MAX_ENTRIES_MASK, ctl);
1243 	ctl = (ctl & ~MLXBF_TMFIFO_RX_CTL__LWM_MASK) |
1244 		FIELD_PREP(MLXBF_TMFIFO_RX_CTL__LWM_MASK, 0);
1245 	ctl = (ctl & ~MLXBF_TMFIFO_RX_CTL__HWM_MASK) |
1246 		FIELD_PREP(MLXBF_TMFIFO_RX_CTL__HWM_MASK, 1);
1247 	writeq(ctl, fifo->rx.ctl);
1248 }
1249 
1250 static void mlxbf_tmfifo_cleanup(struct mlxbf_tmfifo *fifo)
1251 {
1252 	int i;
1253 
1254 	fifo->is_ready = false;
1255 	del_timer_sync(&fifo->timer);
1256 	mlxbf_tmfifo_disable_irqs(fifo);
1257 	cancel_work_sync(&fifo->work);
1258 	for (i = 0; i < MLXBF_TMFIFO_VDEV_MAX; i++)
1259 		mlxbf_tmfifo_delete_vdev(fifo, i);
1260 }
1261 
1262 /* Probe the TMFIFO. */
1263 static int mlxbf_tmfifo_probe(struct platform_device *pdev)
1264 {
1265 	struct virtio_net_config net_config;
1266 	struct device *dev = &pdev->dev;
1267 	struct mlxbf_tmfifo *fifo;
1268 	u64 dev_id;
1269 	int i, rc;
1270 
1271 	rc = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &dev_id);
1272 	if (rc) {
1273 		dev_err(dev, "Cannot retrieve UID\n");
1274 		return rc;
1275 	}
1276 
1277 	fifo = devm_kzalloc(dev, sizeof(*fifo), GFP_KERNEL);
1278 	if (!fifo)
1279 		return -ENOMEM;
1280 
1281 	spin_lock_init(&fifo->spin_lock[0]);
1282 	spin_lock_init(&fifo->spin_lock[1]);
1283 	INIT_WORK(&fifo->work, mlxbf_tmfifo_work_handler);
1284 	mutex_init(&fifo->lock);
1285 
1286 	/* Get the resource of the Rx FIFO. */
1287 	fifo->res0 = devm_platform_ioremap_resource(pdev, 0);
1288 	if (IS_ERR(fifo->res0))
1289 		return PTR_ERR(fifo->res0);
1290 
1291 	/* Get the resource of the Tx FIFO. */
1292 	fifo->res1 = devm_platform_ioremap_resource(pdev, 1);
1293 	if (IS_ERR(fifo->res1))
1294 		return PTR_ERR(fifo->res1);
1295 
1296 	if (dev_id == TMFIFO_BF3_UID) {
1297 		fifo->rx.ctl = fifo->res1 + MLXBF_TMFIFO_RX_CTL_BF3;
1298 		fifo->rx.sts = fifo->res1 + MLXBF_TMFIFO_RX_STS_BF3;
1299 		fifo->rx.data = fifo->res0 + MLXBF_TMFIFO_RX_DATA_BF3;
1300 		fifo->tx.ctl = fifo->res1 + MLXBF_TMFIFO_TX_CTL_BF3;
1301 		fifo->tx.sts = fifo->res1 + MLXBF_TMFIFO_TX_STS_BF3;
1302 		fifo->tx.data = fifo->res0 + MLXBF_TMFIFO_TX_DATA_BF3;
1303 	} else {
1304 		fifo->rx.ctl = fifo->res0 + MLXBF_TMFIFO_RX_CTL;
1305 		fifo->rx.sts = fifo->res0 + MLXBF_TMFIFO_RX_STS;
1306 		fifo->rx.data = fifo->res0 + MLXBF_TMFIFO_RX_DATA;
1307 		fifo->tx.ctl = fifo->res1 + MLXBF_TMFIFO_TX_CTL;
1308 		fifo->tx.sts = fifo->res1 + MLXBF_TMFIFO_TX_STS;
1309 		fifo->tx.data = fifo->res1 + MLXBF_TMFIFO_TX_DATA;
1310 	}
1311 
1312 	platform_set_drvdata(pdev, fifo);
1313 
1314 	timer_setup(&fifo->timer, mlxbf_tmfifo_timer, 0);
1315 
1316 	for (i = 0; i < MLXBF_TM_MAX_IRQ; i++) {
1317 		fifo->irq_info[i].index = i;
1318 		fifo->irq_info[i].fifo = fifo;
1319 		fifo->irq_info[i].irq = platform_get_irq(pdev, i);
1320 		rc = devm_request_irq(dev, fifo->irq_info[i].irq,
1321 				      mlxbf_tmfifo_irq_handler, 0,
1322 				      "tmfifo", &fifo->irq_info[i]);
1323 		if (rc) {
1324 			dev_err(dev, "devm_request_irq failed\n");
1325 			fifo->irq_info[i].irq = 0;
1326 			return rc;
1327 		}
1328 	}
1329 
1330 	mlxbf_tmfifo_set_threshold(fifo);
1331 
1332 	/* Create the console vdev. */
1333 	rc = mlxbf_tmfifo_create_vdev(dev, fifo, VIRTIO_ID_CONSOLE, 0, NULL, 0);
1334 	if (rc)
1335 		goto fail;
1336 
1337 	/* Create the network vdev. */
1338 	memset(&net_config, 0, sizeof(net_config));
1339 
1340 	/* A legacy-only interface for now. */
1341 	net_config.mtu = __cpu_to_virtio16(virtio_legacy_is_little_endian(),
1342 					   ETH_DATA_LEN);
1343 	net_config.status = __cpu_to_virtio16(virtio_legacy_is_little_endian(),
1344 					      VIRTIO_NET_S_LINK_UP);
1345 	mlxbf_tmfifo_get_cfg_mac(net_config.mac);
1346 	rc = mlxbf_tmfifo_create_vdev(dev, fifo, VIRTIO_ID_NET,
1347 				      MLXBF_TMFIFO_NET_FEATURES, &net_config,
1348 				      sizeof(net_config));
1349 	if (rc)
1350 		goto fail;
1351 
1352 	mod_timer(&fifo->timer, jiffies + MLXBF_TMFIFO_TIMER_INTERVAL);
1353 
1354 	/* Make all updates visible before setting the 'is_ready' flag. */
1355 	virtio_mb(false);
1356 
1357 	fifo->is_ready = true;
1358 	return 0;
1359 
1360 fail:
1361 	mlxbf_tmfifo_cleanup(fifo);
1362 	return rc;
1363 }
1364 
1365 /* Device remove function. */
1366 static int mlxbf_tmfifo_remove(struct platform_device *pdev)
1367 {
1368 	struct mlxbf_tmfifo *fifo = platform_get_drvdata(pdev);
1369 
1370 	mlxbf_tmfifo_cleanup(fifo);
1371 
1372 	return 0;
1373 }
1374 
1375 static const struct acpi_device_id mlxbf_tmfifo_acpi_match[] = {
1376 	{ "MLNXBF01", 0 },
1377 	{}
1378 };
1379 MODULE_DEVICE_TABLE(acpi, mlxbf_tmfifo_acpi_match);
1380 
1381 static struct platform_driver mlxbf_tmfifo_driver = {
1382 	.probe = mlxbf_tmfifo_probe,
1383 	.remove = mlxbf_tmfifo_remove,
1384 	.driver = {
1385 		.name = "bf-tmfifo",
1386 		.acpi_match_table = mlxbf_tmfifo_acpi_match,
1387 	},
1388 };
1389 
1390 module_platform_driver(mlxbf_tmfifo_driver);
1391 
1392 MODULE_DESCRIPTION("Mellanox BlueField SoC TmFifo Driver");
1393 MODULE_LICENSE("GPL v2");
1394 MODULE_AUTHOR("Mellanox Technologies");
1395