1 // SPDX-License-Identifier: GPL-2.0-only
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/pci.h>
11 #include <linux/device.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/u64_stats_sync.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/skbuff.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/prefetch.h>
20 #include <linux/cpumask.h>
21 #include <linux/if_vlan.h>
22 #include <asm/barrier.h>
24 #include "hinic_common.h"
25 #include "hinic_hw_if.h"
26 #include "hinic_hw_wqe.h"
27 #include "hinic_hw_wq.h"
28 #include "hinic_hw_qp.h"
29 #include "hinic_hw_dev.h"
31 #include "hinic_dev.h"
33 #define RX_IRQ_NO_PENDING 0
34 #define RX_IRQ_NO_COALESC 0
35 #define RX_IRQ_NO_LLI_TIMER 0
36 #define RX_IRQ_NO_CREDIT 0
37 #define RX_IRQ_NO_RESEND_TIMER 0
38 #define HINIC_RX_BUFFER_WRITE 16
40 #define HINIC_RX_IPV6_PKT 7
41 #define LRO_PKT_HDR_LEN_IPV4 66
42 #define LRO_PKT_HDR_LEN_IPV6 86
43 #define LRO_REPLENISH_THLD 256
45 #define LRO_PKT_HDR_LEN(cqe) \
46 (HINIC_GET_RX_PKT_TYPE(be32_to_cpu((cqe)->offload_type)) == \
47 HINIC_RX_IPV6_PKT ? LRO_PKT_HDR_LEN_IPV6 : LRO_PKT_HDR_LEN_IPV4)
50 * hinic_rxq_clean_stats - Clean the statistics of specific queue
51 * @rxq: Logical Rx Queue
53 void hinic_rxq_clean_stats(struct hinic_rxq *rxq)
55 struct hinic_rxq_stats *rxq_stats = &rxq->rxq_stats;
57 u64_stats_update_begin(&rxq_stats->syncp);
60 rxq_stats->errors = 0;
61 rxq_stats->csum_errors = 0;
62 rxq_stats->other_errors = 0;
63 u64_stats_update_end(&rxq_stats->syncp);
67 * hinic_rxq_get_stats - get statistics of Rx Queue
68 * @rxq: Logical Rx Queue
69 * @stats: return updated stats here
71 void hinic_rxq_get_stats(struct hinic_rxq *rxq, struct hinic_rxq_stats *stats)
73 struct hinic_rxq_stats *rxq_stats = &rxq->rxq_stats;
76 u64_stats_update_begin(&stats->syncp);
78 start = u64_stats_fetch_begin(&rxq_stats->syncp);
79 stats->pkts = rxq_stats->pkts;
80 stats->bytes = rxq_stats->bytes;
81 stats->errors = rxq_stats->csum_errors +
82 rxq_stats->other_errors;
83 stats->csum_errors = rxq_stats->csum_errors;
84 stats->other_errors = rxq_stats->other_errors;
85 } while (u64_stats_fetch_retry(&rxq_stats->syncp, start));
86 u64_stats_update_end(&stats->syncp);
90 * rxq_stats_init - Initialize the statistics of specific queue
91 * @rxq: Logical Rx Queue
93 static void rxq_stats_init(struct hinic_rxq *rxq)
95 struct hinic_rxq_stats *rxq_stats = &rxq->rxq_stats;
97 u64_stats_init(&rxq_stats->syncp);
98 hinic_rxq_clean_stats(rxq);
101 static void rx_csum(struct hinic_rxq *rxq, u32 status,
104 struct net_device *netdev = rxq->netdev;
107 csum_err = HINIC_RQ_CQE_STATUS_GET(status, CSUM_ERR);
109 if (!(netdev->features & NETIF_F_RXCSUM))
113 skb->ip_summed = CHECKSUM_UNNECESSARY;
115 if (!(csum_err & (HINIC_RX_CSUM_HW_CHECK_NONE |
116 HINIC_RX_CSUM_IPSU_OTHER_ERR)))
117 rxq->rxq_stats.csum_errors++;
118 skb->ip_summed = CHECKSUM_NONE;
123 * rx_alloc_skb - allocate skb and map it to dma address
125 * @dma_addr: returned dma address for the skb
129 static struct sk_buff *rx_alloc_skb(struct hinic_rxq *rxq,
130 dma_addr_t *dma_addr)
132 struct hinic_dev *nic_dev = netdev_priv(rxq->netdev);
133 struct hinic_hwdev *hwdev = nic_dev->hwdev;
134 struct hinic_hwif *hwif = hwdev->hwif;
135 struct pci_dev *pdev = hwif->pdev;
140 skb = netdev_alloc_skb_ip_align(rxq->netdev, rxq->rq->buf_sz);
144 addr = dma_map_single(&pdev->dev, skb->data, rxq->rq->buf_sz,
146 err = dma_mapping_error(&pdev->dev, addr);
148 dev_err(&pdev->dev, "Failed to map Rx DMA, err = %d\n", err);
156 dev_kfree_skb_any(skb);
161 * rx_unmap_skb - unmap the dma address of the skb
163 * @dma_addr: dma address of the skb
165 static void rx_unmap_skb(struct hinic_rxq *rxq, dma_addr_t dma_addr)
167 struct hinic_dev *nic_dev = netdev_priv(rxq->netdev);
168 struct hinic_hwdev *hwdev = nic_dev->hwdev;
169 struct hinic_hwif *hwif = hwdev->hwif;
170 struct pci_dev *pdev = hwif->pdev;
172 dma_unmap_single(&pdev->dev, dma_addr, rxq->rq->buf_sz,
177 * rx_free_skb - unmap and free skb
180 * @dma_addr: dma address of the skb
182 static void rx_free_skb(struct hinic_rxq *rxq, struct sk_buff *skb,
185 rx_unmap_skb(rxq, dma_addr);
186 dev_kfree_skb_any(skb);
190 * rx_alloc_pkts - allocate pkts in rx queue
193 * Return number of skbs allocated
195 static int rx_alloc_pkts(struct hinic_rxq *rxq)
197 struct hinic_dev *nic_dev = netdev_priv(rxq->netdev);
198 struct hinic_rq_wqe *rq_wqe;
199 unsigned int free_wqebbs;
200 struct hinic_sge sge;
206 free_wqebbs = hinic_get_rq_free_wqebbs(rxq->rq);
208 /* Limit the allocation chunks */
209 if (free_wqebbs > nic_dev->rx_weight)
210 free_wqebbs = nic_dev->rx_weight;
212 for (i = 0; i < free_wqebbs; i++) {
213 skb = rx_alloc_skb(rxq, &dma_addr);
217 hinic_set_sge(&sge, dma_addr, skb->len);
219 rq_wqe = hinic_rq_get_wqe(rxq->rq, HINIC_RQ_WQE_SIZE,
222 rx_free_skb(rxq, skb, dma_addr);
226 hinic_rq_prepare_wqe(rxq->rq, prod_idx, rq_wqe, &sge);
228 hinic_rq_write_wqe(rxq->rq, prod_idx, rq_wqe, skb);
233 wmb(); /* write all the wqes before update PI */
235 hinic_rq_update(rxq->rq, prod_idx);
242 * free_all_rx_skbs - free all skbs in rx queue
245 static void free_all_rx_skbs(struct hinic_rxq *rxq)
247 struct hinic_rq *rq = rxq->rq;
248 struct hinic_hw_wqe *hw_wqe;
249 struct hinic_sge sge;
252 while ((hw_wqe = hinic_read_wqe(rq->wq, HINIC_RQ_WQE_SIZE, &ci))) {
256 hinic_rq_get_sge(rq, &hw_wqe->rq_wqe, ci, &sge);
258 hinic_put_wqe(rq->wq, HINIC_RQ_WQE_SIZE);
260 rx_free_skb(rxq, rq->saved_skb[ci], hinic_sge_to_dma(&sge));
265 * rx_recv_jumbo_pkt - Rx handler for jumbo pkt
267 * @head_skb: the first skb in the list
268 * @left_pkt_len: left size of the pkt exclude head skb
269 * @ci: consumer index
271 * Return number of wqes that used for the left of the pkt
273 static int rx_recv_jumbo_pkt(struct hinic_rxq *rxq, struct sk_buff *head_skb,
274 unsigned int left_pkt_len, u16 ci)
276 struct sk_buff *skb, *curr_skb = head_skb;
277 struct hinic_rq_wqe *rq_wqe;
278 unsigned int curr_len;
279 struct hinic_sge sge;
282 while (left_pkt_len > 0) {
283 rq_wqe = hinic_rq_read_next_wqe(rxq->rq, HINIC_RQ_WQE_SIZE,
288 hinic_rq_get_sge(rxq->rq, rq_wqe, ci, &sge);
290 rx_unmap_skb(rxq, hinic_sge_to_dma(&sge));
294 curr_len = (left_pkt_len > HINIC_RX_BUF_SZ) ? HINIC_RX_BUF_SZ :
297 left_pkt_len -= curr_len;
299 __skb_put(skb, curr_len);
301 if (curr_skb == head_skb)
302 skb_shinfo(head_skb)->frag_list = skb;
304 curr_skb->next = skb;
306 head_skb->len += skb->len;
307 head_skb->data_len += skb->len;
308 head_skb->truesize += skb->truesize;
316 static void hinic_copy_lp_data(struct hinic_dev *nic_dev,
319 struct net_device *netdev = nic_dev->netdev;
320 u8 *lb_buf = nic_dev->lb_test_rx_buf;
321 int lb_len = nic_dev->lb_pkt_len;
322 int pkt_offset, frag_len, i;
323 void *frag_data = NULL;
325 if (nic_dev->lb_test_rx_idx == LP_PKT_CNT) {
326 nic_dev->lb_test_rx_idx = 0;
327 netif_warn(nic_dev, drv, netdev, "Loopback test warning, receive too more test pkts\n");
330 if (skb->len != nic_dev->lb_pkt_len) {
331 netif_warn(nic_dev, drv, netdev, "Wrong packet length\n");
332 nic_dev->lb_test_rx_idx++;
336 pkt_offset = nic_dev->lb_test_rx_idx * lb_len;
337 frag_len = (int)skb_headlen(skb);
338 memcpy(lb_buf + pkt_offset, skb->data, frag_len);
339 pkt_offset += frag_len;
340 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
341 frag_data = skb_frag_address(&skb_shinfo(skb)->frags[i]);
342 frag_len = (int)skb_frag_size(&skb_shinfo(skb)->frags[i]);
343 memcpy((lb_buf + pkt_offset), frag_data, frag_len);
344 pkt_offset += frag_len;
346 nic_dev->lb_test_rx_idx++;
350 * rxq_recv - Rx handler
352 * @budget: maximum pkts to process
354 * Return number of pkts received
356 static int rxq_recv(struct hinic_rxq *rxq, int budget)
358 struct hinic_qp *qp = container_of(rxq->rq, struct hinic_qp, rq);
359 struct net_device *netdev = rxq->netdev;
360 u64 pkt_len = 0, rx_bytes = 0;
361 struct hinic_rq *rq = rxq->rq;
362 struct hinic_rq_wqe *rq_wqe;
363 struct hinic_dev *nic_dev;
364 unsigned int free_wqebbs;
365 struct hinic_rq_cqe *cqe;
366 int num_wqes, pkts = 0;
367 struct hinic_sge sge;
376 nic_dev = netdev_priv(netdev);
378 while (pkts < budget) {
381 rq_wqe = hinic_rq_read_wqe(rxq->rq, HINIC_RQ_WQE_SIZE, &skb,
386 /* make sure we read rx_done before packet length */
390 status = be32_to_cpu(cqe->status);
391 hinic_rq_get_sge(rxq->rq, rq_wqe, ci, &sge);
393 rx_unmap_skb(rxq, hinic_sge_to_dma(&sge));
395 rx_csum(rxq, status, skb);
401 if (pkt_len <= HINIC_RX_BUF_SZ) {
402 __skb_put(skb, pkt_len);
404 __skb_put(skb, HINIC_RX_BUF_SZ);
405 num_wqes = rx_recv_jumbo_pkt(rxq, skb, pkt_len -
406 HINIC_RX_BUF_SZ, ci);
409 hinic_rq_put_wqe(rq, ci,
410 (num_wqes + 1) * HINIC_RQ_WQE_SIZE);
412 offload_type = be32_to_cpu(cqe->offload_type);
413 vlan_len = be32_to_cpu(cqe->len);
414 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
415 HINIC_GET_RX_VLAN_OFFLOAD_EN(offload_type)) {
416 vid = HINIC_GET_RX_VLAN_TAG(vlan_len);
417 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
420 if (unlikely(nic_dev->flags & HINIC_LP_TEST))
421 hinic_copy_lp_data(nic_dev, skb);
423 skb_record_rx_queue(skb, qp->q_id);
424 skb->protocol = eth_type_trans(skb, rxq->netdev);
426 napi_gro_receive(&rxq->napi, skb);
431 num_lro = HINIC_GET_RX_NUM_LRO(status);
433 rx_bytes += ((num_lro - 1) *
434 LRO_PKT_HDR_LEN(cqe));
437 (u16)(pkt_len >> rxq->rx_buff_shift) +
438 ((pkt_len & (rxq->buf_len - 1)) ? 1 : 0);
443 if (num_wqe >= LRO_REPLENISH_THLD)
447 free_wqebbs = hinic_get_rq_free_wqebbs(rxq->rq);
448 if (free_wqebbs > HINIC_RX_BUFFER_WRITE)
451 u64_stats_update_begin(&rxq->rxq_stats.syncp);
452 rxq->rxq_stats.pkts += pkts;
453 rxq->rxq_stats.bytes += rx_bytes;
454 u64_stats_update_end(&rxq->rxq_stats.syncp);
459 static int rx_poll(struct napi_struct *napi, int budget)
461 struct hinic_rxq *rxq = container_of(napi, struct hinic_rxq, napi);
462 struct hinic_dev *nic_dev = netdev_priv(rxq->netdev);
463 struct hinic_rq *rq = rxq->rq;
466 pkts = rxq_recv(rxq, budget);
472 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
473 hinic_hwdev_set_msix_state(nic_dev->hwdev,
480 static void rx_add_napi(struct hinic_rxq *rxq)
482 struct hinic_dev *nic_dev = netdev_priv(rxq->netdev);
484 netif_napi_add(rxq->netdev, &rxq->napi, rx_poll, nic_dev->rx_weight);
485 napi_enable(&rxq->napi);
488 static void rx_del_napi(struct hinic_rxq *rxq)
490 napi_disable(&rxq->napi);
491 netif_napi_del(&rxq->napi);
494 static irqreturn_t rx_irq(int irq, void *data)
496 struct hinic_rxq *rxq = (struct hinic_rxq *)data;
497 struct hinic_rq *rq = rxq->rq;
498 struct hinic_dev *nic_dev;
500 /* Disable the interrupt until napi will be completed */
501 nic_dev = netdev_priv(rxq->netdev);
502 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
503 hinic_hwdev_set_msix_state(nic_dev->hwdev,
507 nic_dev = netdev_priv(rxq->netdev);
508 hinic_hwdev_msix_cnt_set(nic_dev->hwdev, rq->msix_entry);
510 napi_schedule(&rxq->napi);
514 static int rx_request_irq(struct hinic_rxq *rxq)
516 struct hinic_dev *nic_dev = netdev_priv(rxq->netdev);
517 struct hinic_msix_config interrupt_info = {0};
518 struct hinic_intr_coal_info *intr_coal = NULL;
519 struct hinic_hwdev *hwdev = nic_dev->hwdev;
520 struct hinic_rq *rq = rxq->rq;
524 qp = container_of(rq, struct hinic_qp, rq);
528 hinic_hwdev_msix_set(hwdev, rq->msix_entry,
529 RX_IRQ_NO_PENDING, RX_IRQ_NO_COALESC,
530 RX_IRQ_NO_LLI_TIMER, RX_IRQ_NO_CREDIT,
531 RX_IRQ_NO_RESEND_TIMER);
533 intr_coal = &nic_dev->rx_intr_coalesce[qp->q_id];
534 interrupt_info.msix_index = rq->msix_entry;
535 interrupt_info.coalesce_timer_cnt = intr_coal->coalesce_timer_cfg;
536 interrupt_info.pending_cnt = intr_coal->pending_limt;
537 interrupt_info.resend_timer_cnt = intr_coal->resend_timer_cfg;
539 err = hinic_set_interrupt_cfg(hwdev, &interrupt_info);
541 netif_err(nic_dev, drv, rxq->netdev,
542 "Failed to set RX interrupt coalescing attribute\n");
546 err = request_irq(rq->irq, rx_irq, 0, rxq->irq_name, rxq);
550 cpumask_set_cpu(qp->q_id % num_online_cpus(), &rq->affinity_mask);
551 err = irq_set_affinity_and_hint(rq->irq, &rq->affinity_mask);
553 goto err_irq_affinity;
558 free_irq(rq->irq, rxq);
564 static void rx_free_irq(struct hinic_rxq *rxq)
566 struct hinic_rq *rq = rxq->rq;
568 irq_update_affinity_hint(rq->irq, NULL);
569 free_irq(rq->irq, rxq);
574 * hinic_init_rxq - Initialize the Rx Queue
575 * @rxq: Logical Rx Queue
576 * @rq: Hardware Rx Queue to connect the Logical queue with
577 * @netdev: network device to connect the Logical queue with
579 * Return 0 - Success, negative - Failure
581 int hinic_init_rxq(struct hinic_rxq *rxq, struct hinic_rq *rq,
582 struct net_device *netdev)
584 struct hinic_qp *qp = container_of(rq, struct hinic_qp, rq);
587 rxq->netdev = netdev;
589 rxq->buf_len = HINIC_RX_BUF_SZ;
590 rxq->rx_buff_shift = ilog2(HINIC_RX_BUF_SZ);
594 rxq->irq_name = devm_kasprintf(&netdev->dev, GFP_KERNEL,
595 "%s_rxq%d", netdev->name, qp->q_id);
599 pkts = rx_alloc_pkts(rxq);
605 err = rx_request_irq(rxq);
607 netdev_err(netdev, "Failed to request Rx irq\n");
615 free_all_rx_skbs(rxq);
616 devm_kfree(&netdev->dev, rxq->irq_name);
621 * hinic_clean_rxq - Clean the Rx Queue
622 * @rxq: Logical Rx Queue
624 void hinic_clean_rxq(struct hinic_rxq *rxq)
626 struct net_device *netdev = rxq->netdev;
630 free_all_rx_skbs(rxq);
631 devm_kfree(&netdev->dev, rxq->irq_name);