1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell Octeon EP (EndPoint) Ethernet Driver
4 * Copyright (C) 2020 Marvell.
9 #include <linux/etherdevice.h>
10 #include <linux/vmalloc.h>
12 #include "octep_config.h"
13 #include "octep_main.h"
15 static void octep_oq_reset_indices(struct octep_oq *oq)
17 oq->host_read_idx = 0;
18 oq->host_refill_idx = 0;
20 oq->last_pkt_count = 0;
25 * octep_oq_fill_ring_buffers() - fill initial receive buffers for Rx ring.
27 * @oq: Octeon Rx queue data structure.
29 * Return: 0, if successfully filled receive buffers for all descriptors.
30 * -1, if failed to allocate a buffer or failed to map for DMA.
32 static int octep_oq_fill_ring_buffers(struct octep_oq *oq)
34 struct octep_oq_desc_hw *desc_ring = oq->desc_ring;
38 for (i = 0; i < oq->max_count; i++) {
39 page = dev_alloc_page();
40 if (unlikely(!page)) {
41 dev_err(oq->dev, "Rx buffer alloc failed\n");
42 goto rx_buf_alloc_err;
44 desc_ring[i].buffer_ptr = dma_map_page(oq->dev, page, 0,
47 if (dma_mapping_error(oq->dev, desc_ring[i].buffer_ptr)) {
49 "OQ-%d buffer alloc: DMA mapping error!\n",
54 oq->buff_info[i].page = page;
63 dma_unmap_page(oq->dev, desc_ring[i].buffer_ptr, PAGE_SIZE, DMA_FROM_DEVICE);
64 put_page(oq->buff_info[i].page);
65 oq->buff_info[i].page = NULL;
72 * octep_oq_refill() - refill buffers for used Rx ring descriptors.
74 * @oct: Octeon device private data structure.
75 * @oq: Octeon Rx queue data structure.
77 * Return: number of descriptors successfully refilled with receive buffers.
79 static int octep_oq_refill(struct octep_device *oct, struct octep_oq *oq)
81 struct octep_oq_desc_hw *desc_ring = oq->desc_ring;
85 refill_idx = oq->host_refill_idx;
86 for (i = 0; i < oq->refill_count; i++) {
87 page = dev_alloc_page();
88 if (unlikely(!page)) {
89 dev_err(oq->dev, "refill: rx buffer alloc failed\n");
90 oq->stats.alloc_failures++;
94 desc_ring[refill_idx].buffer_ptr = dma_map_page(oq->dev, page, 0,
95 PAGE_SIZE, DMA_FROM_DEVICE);
96 if (dma_mapping_error(oq->dev, desc_ring[refill_idx].buffer_ptr)) {
98 "OQ-%d buffer refill: DMA mapping error!\n",
101 oq->stats.alloc_failures++;
104 oq->buff_info[refill_idx].page = page;
106 if (refill_idx == oq->max_count)
109 oq->host_refill_idx = refill_idx;
110 oq->refill_count -= i;
116 * octep_setup_oq() - Setup a Rx queue.
118 * @oct: Octeon device private data structure.
119 * @q_no: Rx queue number to be setup.
121 * Allocate resources for a Rx queue.
123 static int octep_setup_oq(struct octep_device *oct, int q_no)
128 oq = vzalloc(sizeof(*oq));
134 oq->netdev = oct->netdev;
135 oq->dev = &oct->pdev->dev;
137 oq->max_count = CFG_GET_OQ_NUM_DESC(oct->conf);
138 oq->ring_size_mask = oq->max_count - 1;
139 oq->buffer_size = CFG_GET_OQ_BUF_SIZE(oct->conf);
140 oq->max_single_buffer_size = oq->buffer_size - OCTEP_OQ_RESP_HW_SIZE;
142 /* When the hardware/firmware supports additional capabilities,
143 * additional header is filled-in by Octeon after length field in
144 * Rx packets. this header contains additional packet information.
146 if (oct->conf->fw_info.rx_ol_flags)
147 oq->max_single_buffer_size -= OCTEP_OQ_RESP_HW_EXT_SIZE;
149 oq->refill_threshold = CFG_GET_OQ_REFILL_THRESHOLD(oct->conf);
151 desc_ring_size = oq->max_count * OCTEP_OQ_DESC_SIZE;
152 oq->desc_ring = dma_alloc_coherent(oq->dev, desc_ring_size,
153 &oq->desc_ring_dma, GFP_KERNEL);
155 if (unlikely(!oq->desc_ring)) {
157 "Failed to allocate DMA memory for OQ-%d !!\n", q_no);
158 goto desc_dma_alloc_err;
161 oq->buff_info = vcalloc(oq->max_count, OCTEP_OQ_RECVBUF_SIZE);
162 if (unlikely(!oq->buff_info)) {
163 dev_err(&oct->pdev->dev,
164 "Failed to allocate buffer info for OQ-%d\n", q_no);
168 if (octep_oq_fill_ring_buffers(oq))
169 goto oq_fill_buff_err;
171 octep_oq_reset_indices(oq);
172 oct->hw_ops.setup_oq_regs(oct, q_no);
178 vfree(oq->buff_info);
179 oq->buff_info = NULL;
181 dma_free_coherent(oq->dev, desc_ring_size,
182 oq->desc_ring, oq->desc_ring_dma);
183 oq->desc_ring = NULL;
186 oct->oq[q_no] = NULL;
192 * octep_oq_free_ring_buffers() - Free ring buffers.
194 * @oq: Octeon Rx queue data structure.
196 * Free receive buffers in unused Rx queue descriptors.
198 static void octep_oq_free_ring_buffers(struct octep_oq *oq)
200 struct octep_oq_desc_hw *desc_ring = oq->desc_ring;
203 if (!oq->desc_ring || !oq->buff_info)
206 for (i = 0; i < oq->max_count; i++) {
207 if (oq->buff_info[i].page) {
208 dma_unmap_page(oq->dev, desc_ring[i].buffer_ptr,
209 PAGE_SIZE, DMA_FROM_DEVICE);
210 put_page(oq->buff_info[i].page);
211 oq->buff_info[i].page = NULL;
212 desc_ring[i].buffer_ptr = 0;
215 octep_oq_reset_indices(oq);
219 * octep_free_oq() - Free Rx queue resources.
221 * @oq: Octeon Rx queue data structure.
223 * Free all resources of a Rx queue.
225 static int octep_free_oq(struct octep_oq *oq)
227 struct octep_device *oct = oq->octep_dev;
230 octep_oq_free_ring_buffers(oq);
232 vfree(oq->buff_info);
235 dma_free_coherent(oq->dev,
236 oq->max_count * OCTEP_OQ_DESC_SIZE,
237 oq->desc_ring, oq->desc_ring_dma);
240 oct->oq[q_no] = NULL;
246 * octep_setup_oqs() - setup resources for all Rx queues.
248 * @oct: Octeon device private data structure.
250 int octep_setup_oqs(struct octep_device *oct)
255 for (i = 0; i < CFG_GET_PORTS_ACTIVE_IO_RINGS(oct->conf); i++) {
256 retval = octep_setup_oq(oct, i);
258 dev_err(&oct->pdev->dev,
259 "Failed to setup OQ(RxQ)-%d.\n", i);
262 dev_dbg(&oct->pdev->dev, "Successfully setup OQ(RxQ)-%d.\n", i);
270 octep_free_oq(oct->oq[i]);
276 * octep_oq_dbell_init() - Initialize Rx queue doorbell.
278 * @oct: Octeon device private data structure.
280 * Write number of descriptors to Rx queue doorbell register.
282 void octep_oq_dbell_init(struct octep_device *oct)
286 for (i = 0; i < oct->num_oqs; i++)
287 writel(oct->oq[i]->max_count, oct->oq[i]->pkts_credit_reg);
291 * octep_free_oqs() - Free resources of all Rx queues.
293 * @oct: Octeon device private data structure.
295 void octep_free_oqs(struct octep_device *oct)
299 for (i = 0; i < CFG_GET_PORTS_ACTIVE_IO_RINGS(oct->conf); i++) {
302 octep_free_oq(oct->oq[i]);
303 dev_dbg(&oct->pdev->dev,
304 "Successfully freed OQ(RxQ)-%d.\n", i);
309 * octep_oq_check_hw_for_pkts() - Check for new Rx packets.
311 * @oct: Octeon device private data structure.
312 * @oq: Octeon Rx queue data structure.
314 * Return: packets received after previous check.
316 static int octep_oq_check_hw_for_pkts(struct octep_device *oct,
319 u32 pkt_count, new_pkts;
321 pkt_count = readl(oq->pkts_sent_reg);
322 new_pkts = pkt_count - oq->last_pkt_count;
324 /* Clear the hardware packets counter register if the rx queue is
325 * being processed continuously with-in a single interrupt and
326 * reached half its max value.
327 * this counter is not cleared every time read, to save write cycles.
329 if (unlikely(pkt_count > 0xF0000000U)) {
330 writel(pkt_count, oq->pkts_sent_reg);
331 pkt_count = readl(oq->pkts_sent_reg);
332 new_pkts += pkt_count;
334 oq->last_pkt_count = pkt_count;
335 oq->pkts_pending += new_pkts;
340 * __octep_oq_process_rx() - Process hardware Rx queue and push to stack.
342 * @oct: Octeon device private data structure.
343 * @oq: Octeon Rx queue data structure.
344 * @pkts_to_process: number of packets to be processed.
346 * Process the new packets in Rx queue.
347 * Packets larger than single Rx buffer arrive in consecutive descriptors.
348 * But, count returned by the API only accounts full packets, not fragments.
350 * Return: number of packets processed and pushed to stack.
352 static int __octep_oq_process_rx(struct octep_device *oct,
353 struct octep_oq *oq, u16 pkts_to_process)
355 struct octep_oq_resp_hw_ext *resp_hw_ext = NULL;
356 netdev_features_t feat = oq->netdev->features;
357 struct octep_rx_buffer *buff_info;
358 struct octep_oq_resp_hw *resp_hw;
359 u32 pkt, rx_bytes, desc_used;
365 read_idx = oq->host_read_idx;
368 for (pkt = 0; pkt < pkts_to_process; pkt++) {
369 buff_info = (struct octep_rx_buffer *)&oq->buff_info[read_idx];
370 dma_unmap_page(oq->dev, oq->desc_ring[read_idx].buffer_ptr,
371 PAGE_SIZE, DMA_FROM_DEVICE);
372 resp_hw = page_address(buff_info->page);
373 buff_info->page = NULL;
375 /* Swap the length field that is in Big-Endian to CPU */
376 buff_info->len = be64_to_cpu(resp_hw->length);
377 if (oct->conf->fw_info.rx_ol_flags) {
378 /* Extended response header is immediately after
379 * response header (resp_hw)
381 resp_hw_ext = (struct octep_oq_resp_hw_ext *)
383 buff_info->len -= OCTEP_OQ_RESP_HW_EXT_SIZE;
384 /* Packet Data is immediately after
385 * extended response header.
387 data_offset = OCTEP_OQ_RESP_HW_SIZE +
388 OCTEP_OQ_RESP_HW_EXT_SIZE;
389 rx_ol_flags = resp_hw_ext->rx_ol_flags;
391 /* Data is immediately after
392 * Hardware Rx response header.
394 data_offset = OCTEP_OQ_RESP_HW_SIZE;
397 rx_bytes += buff_info->len;
399 if (buff_info->len <= oq->max_single_buffer_size) {
400 skb = build_skb((void *)resp_hw, PAGE_SIZE);
401 skb_reserve(skb, data_offset);
402 skb_put(skb, buff_info->len);
405 if (read_idx == oq->max_count)
408 struct skb_shared_info *shinfo;
411 skb = build_skb((void *)resp_hw, PAGE_SIZE);
412 skb_reserve(skb, data_offset);
413 /* Head fragment includes response header(s);
414 * subsequent fragments contains only data.
416 skb_put(skb, oq->max_single_buffer_size);
419 if (read_idx == oq->max_count)
422 shinfo = skb_shinfo(skb);
423 data_len = buff_info->len - oq->max_single_buffer_size;
425 dma_unmap_page(oq->dev, oq->desc_ring[read_idx].buffer_ptr,
426 PAGE_SIZE, DMA_FROM_DEVICE);
427 buff_info = (struct octep_rx_buffer *)
428 &oq->buff_info[read_idx];
429 if (data_len < oq->buffer_size) {
430 buff_info->len = data_len;
433 buff_info->len = oq->buffer_size;
434 data_len -= oq->buffer_size;
437 skb_add_rx_frag(skb, shinfo->nr_frags,
441 buff_info->page = NULL;
444 if (read_idx == oq->max_count)
449 skb->dev = oq->netdev;
450 skb->protocol = eth_type_trans(skb, skb->dev);
451 if (feat & NETIF_F_RXCSUM &&
452 OCTEP_RX_CSUM_VERIFIED(rx_ol_flags))
453 skb->ip_summed = CHECKSUM_UNNECESSARY;
455 skb->ip_summed = CHECKSUM_NONE;
456 napi_gro_receive(oq->napi, skb);
459 oq->host_read_idx = read_idx;
460 oq->refill_count += desc_used;
461 oq->stats.packets += pkt;
462 oq->stats.bytes += rx_bytes;
468 * octep_oq_process_rx() - Process Rx queue.
470 * @oq: Octeon Rx queue data structure.
471 * @budget: max number of packets can be processed in one invocation.
473 * Check for newly received packets and process them.
474 * Keeps checking for new packets until budget is used or no new packets seen.
476 * Return: number of packets processed.
478 int octep_oq_process_rx(struct octep_oq *oq, int budget)
480 u32 pkts_available, pkts_processed, total_pkts_processed;
481 struct octep_device *oct = oq->octep_dev;
485 total_pkts_processed = 0;
486 while (total_pkts_processed < budget) {
487 /* update pending count only when current one exhausted */
488 if (oq->pkts_pending == 0)
489 octep_oq_check_hw_for_pkts(oct, oq);
490 pkts_available = min(budget - total_pkts_processed,
495 pkts_processed = __octep_oq_process_rx(oct, oq,
497 oq->pkts_pending -= pkts_processed;
498 total_pkts_processed += pkts_processed;
501 if (oq->refill_count >= oq->refill_threshold) {
502 u32 desc_refilled = octep_oq_refill(oct, oq);
504 /* flush pending writes before updating credits */
506 writel(desc_refilled, oq->pkts_credit_reg);
509 return total_pkts_processed;