mlxbf-tmfifo.c 35 KB

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