|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +// Copyright 2025 Cisco Systems, Inc. All rights reserved. |
| 3 | + |
| 4 | +#include <net/netdev_queues.h> |
| 5 | +#include "enic_res.h" |
| 6 | +#include "enic.h" |
| 7 | +#include "enic_wq.h" |
| 8 | + |
| 9 | +static void cq_desc_dec(const struct cq_desc *desc_arg, u8 *type, u8 *color, |
| 10 | + u16 *q_number, u16 *completed_index) |
| 11 | +{ |
| 12 | + const struct cq_desc *desc = desc_arg; |
| 13 | + const u8 type_color = desc->type_color; |
| 14 | + |
| 15 | + *color = (type_color >> CQ_DESC_COLOR_SHIFT) & CQ_DESC_COLOR_MASK; |
| 16 | + |
| 17 | + /* |
| 18 | + * Make sure color bit is read from desc *before* other fields |
| 19 | + * are read from desc. Hardware guarantees color bit is last |
| 20 | + * bit (byte) written. Adding the rmb() prevents the compiler |
| 21 | + * and/or CPU from reordering the reads which would potentially |
| 22 | + * result in reading stale values. |
| 23 | + */ |
| 24 | + rmb(); |
| 25 | + |
| 26 | + *type = type_color & CQ_DESC_TYPE_MASK; |
| 27 | + *q_number = le16_to_cpu(desc->q_number) & CQ_DESC_Q_NUM_MASK; |
| 28 | + *completed_index = le16_to_cpu(desc->completed_index) & |
| 29 | + CQ_DESC_COMP_NDX_MASK; |
| 30 | +} |
| 31 | + |
| 32 | +unsigned int vnic_cq_service(struct vnic_cq *cq, unsigned int work_to_do, |
| 33 | + int (*q_service)(struct vnic_dev *vdev, |
| 34 | + struct cq_desc *cq_desc, u8 type, |
| 35 | + u16 q_number, u16 completed_index, |
| 36 | + void *opaque), void *opaque) |
| 37 | +{ |
| 38 | + struct cq_desc *cq_desc; |
| 39 | + unsigned int work_done = 0; |
| 40 | + u16 q_number, completed_index; |
| 41 | + u8 type, color; |
| 42 | + |
| 43 | + cq_desc = (struct cq_desc *)((u8 *)cq->ring.descs + |
| 44 | + cq->ring.desc_size * cq->to_clean); |
| 45 | + cq_desc_dec(cq_desc, &type, &color, |
| 46 | + &q_number, &completed_index); |
| 47 | + |
| 48 | + while (color != cq->last_color) { |
| 49 | + if ((*q_service)(cq->vdev, cq_desc, type, q_number, |
| 50 | + completed_index, opaque)) |
| 51 | + break; |
| 52 | + |
| 53 | + cq->to_clean++; |
| 54 | + if (cq->to_clean == cq->ring.desc_count) { |
| 55 | + cq->to_clean = 0; |
| 56 | + cq->last_color = cq->last_color ? 0 : 1; |
| 57 | + } |
| 58 | + |
| 59 | + cq_desc = (struct cq_desc *)((u8 *)cq->ring.descs + |
| 60 | + cq->ring.desc_size * cq->to_clean); |
| 61 | + cq_desc_dec(cq_desc, &type, &color, |
| 62 | + &q_number, &completed_index); |
| 63 | + |
| 64 | + work_done++; |
| 65 | + if (work_done >= work_to_do) |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + return work_done; |
| 70 | +} |
| 71 | + |
| 72 | +void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf) |
| 73 | +{ |
| 74 | + struct enic *enic = vnic_dev_priv(wq->vdev); |
| 75 | + |
| 76 | + if (buf->sop) |
| 77 | + dma_unmap_single(&enic->pdev->dev, buf->dma_addr, buf->len, |
| 78 | + DMA_TO_DEVICE); |
| 79 | + else |
| 80 | + dma_unmap_page(&enic->pdev->dev, buf->dma_addr, buf->len, |
| 81 | + DMA_TO_DEVICE); |
| 82 | + |
| 83 | + if (buf->os_buf) |
| 84 | + dev_kfree_skb_any(buf->os_buf); |
| 85 | +} |
| 86 | + |
| 87 | +static void enic_wq_free_buf(struct vnic_wq *wq, struct cq_desc *cq_desc, |
| 88 | + struct vnic_wq_buf *buf, void *opaque) |
| 89 | +{ |
| 90 | + struct enic *enic = vnic_dev_priv(wq->vdev); |
| 91 | + |
| 92 | + enic->wq[wq->index].stats.cq_work++; |
| 93 | + enic->wq[wq->index].stats.cq_bytes += buf->len; |
| 94 | + enic_free_wq_buf(wq, buf); |
| 95 | +} |
| 96 | + |
| 97 | +int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc, u8 type, |
| 98 | + u16 q_number, u16 completed_index, void *opaque) |
| 99 | +{ |
| 100 | + struct enic *enic = vnic_dev_priv(vdev); |
| 101 | + |
| 102 | + spin_lock(&enic->wq[q_number].lock); |
| 103 | + |
| 104 | + vnic_wq_service(&enic->wq[q_number].vwq, cq_desc, |
| 105 | + completed_index, enic_wq_free_buf, opaque); |
| 106 | + |
| 107 | + if (netif_tx_queue_stopped(netdev_get_tx_queue(enic->netdev, q_number)) |
| 108 | + && vnic_wq_desc_avail(&enic->wq[q_number].vwq) >= |
| 109 | + (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)) { |
| 110 | + netif_wake_subqueue(enic->netdev, q_number); |
| 111 | + enic->wq[q_number].stats.wake++; |
| 112 | + } |
| 113 | + |
| 114 | + spin_unlock(&enic->wq[q_number].lock); |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
| 118 | + |
0 commit comments