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 /* Reset various index of Tx queue data structure. */
16 static void octep_iq_reset_indices(struct octep_iq *iq)
19 iq->host_write_index = 0;
20 iq->octep_read_index = 0;
22 iq->pkts_processed = 0;
27 * octep_iq_process_completions() - Process Tx queue completions.
29 * @iq: Octeon Tx queue data structure.
30 * @budget: max number of completions to be processed in one invocation.
32 int octep_iq_process_completions(struct octep_iq *iq, u16 budget)
34 u32 compl_pkts, compl_bytes, compl_sg;
35 struct octep_device *oct = iq->octep_dev;
36 struct octep_tx_buffer *tx_buffer;
37 struct skb_shared_info *shinfo;
38 u32 fi = iq->flush_index;
45 iq->octep_read_index = oct->hw_ops.update_iq_read_idx(iq);
47 while (likely(budget && (fi != iq->octep_read_index))) {
48 tx_buffer = iq->buff_info + fi;
52 if (unlikely(fi == iq->max_count))
54 compl_bytes += skb->len;
58 if (!tx_buffer->gather) {
59 dma_unmap_single(iq->dev, tx_buffer->dma,
60 tx_buffer->skb->len, DMA_TO_DEVICE);
61 dev_kfree_skb_any(skb);
66 shinfo = skb_shinfo(skb);
67 frags = shinfo->nr_frags;
70 dma_unmap_single(iq->dev, tx_buffer->sglist[0].dma_ptr[0],
71 tx_buffer->sglist[0].len[3], DMA_TO_DEVICE);
73 i = 1; /* entry 0 is main skb, unmapped above */
75 dma_unmap_page(iq->dev, tx_buffer->sglist[i >> 2].dma_ptr[i & 3],
76 tx_buffer->sglist[i >> 2].len[3 - (i & 3)], DMA_TO_DEVICE);
80 dev_kfree_skb_any(skb);
83 iq->pkts_processed += compl_pkts;
84 iq->stats.instr_completed += compl_pkts;
85 iq->stats.bytes_sent += compl_bytes;
86 iq->stats.sgentry_sent += compl_sg;
89 netdev_tx_completed_queue(iq->netdev_q, compl_pkts, compl_bytes);
91 if (unlikely(__netif_subqueue_stopped(iq->netdev, iq->q_no)) &&
93 OCTEP_WAKE_QUEUE_THRESHOLD))
94 netif_wake_subqueue(iq->netdev, iq->q_no);
99 * octep_iq_free_pending() - Free Tx buffers for pending completions.
101 * @iq: Octeon Tx queue data structure.
103 static void octep_iq_free_pending(struct octep_iq *iq)
105 struct octep_tx_buffer *tx_buffer;
106 struct skb_shared_info *shinfo;
107 u32 fi = iq->flush_index;
111 while (fi != iq->host_write_index) {
112 tx_buffer = iq->buff_info + fi;
113 skb = tx_buffer->skb;
116 if (unlikely(fi == iq->max_count))
119 if (!tx_buffer->gather) {
120 dma_unmap_single(iq->dev, tx_buffer->dma,
121 tx_buffer->skb->len, DMA_TO_DEVICE);
122 dev_kfree_skb_any(skb);
127 shinfo = skb_shinfo(skb);
128 frags = shinfo->nr_frags;
130 dma_unmap_single(iq->dev,
131 tx_buffer->sglist[0].dma_ptr[0],
132 tx_buffer->sglist[0].len[3],
135 i = 1; /* entry 0 is main skb, unmapped above */
137 dma_unmap_page(iq->dev, tx_buffer->sglist[i >> 2].dma_ptr[i & 3],
138 tx_buffer->sglist[i >> 2].len[3 - (i & 3)], DMA_TO_DEVICE);
142 dev_kfree_skb_any(skb);
145 iq->flush_index = fi;
146 netdev_tx_reset_queue(netdev_get_tx_queue(iq->netdev, iq->q_no));
150 * octep_clean_iqs() - Clean Tx queues to shutdown the device.
152 * @oct: Octeon device private data structure.
154 * Free the buffers in Tx queue descriptors pending completion and
155 * reset queue indices
157 void octep_clean_iqs(struct octep_device *oct)
161 for (i = 0; i < oct->num_iqs; i++) {
162 octep_iq_free_pending(oct->iq[i]);
163 octep_iq_reset_indices(oct->iq[i]);
168 * octep_setup_iq() - Setup a Tx queue.
170 * @oct: Octeon device private data structure.
171 * @q_no: Tx queue number to be setup.
173 * Allocate resources for a Tx queue.
175 static int octep_setup_iq(struct octep_device *oct, int q_no)
177 u32 desc_ring_size, buff_info_size, sglist_size;
181 iq = vzalloc(sizeof(*iq));
187 iq->netdev = oct->netdev;
188 iq->dev = &oct->pdev->dev;
190 iq->max_count = CFG_GET_IQ_NUM_DESC(oct->conf);
191 iq->ring_size_mask = iq->max_count - 1;
192 iq->fill_threshold = CFG_GET_IQ_DB_MIN(oct->conf);
193 iq->netdev_q = netdev_get_tx_queue(iq->netdev, q_no);
195 /* Allocate memory for hardware queue descriptors */
196 desc_ring_size = OCTEP_IQ_DESC_SIZE * CFG_GET_IQ_NUM_DESC(oct->conf);
197 iq->desc_ring = dma_alloc_coherent(iq->dev, desc_ring_size,
198 &iq->desc_ring_dma, GFP_KERNEL);
199 if (unlikely(!iq->desc_ring)) {
201 "Failed to allocate DMA memory for IQ-%d\n", q_no);
202 goto desc_dma_alloc_err;
205 /* Allocate memory for hardware SGLIST descriptors */
206 sglist_size = OCTEP_SGLIST_SIZE_PER_PKT *
207 CFG_GET_IQ_NUM_DESC(oct->conf);
208 iq->sglist = dma_alloc_coherent(iq->dev, sglist_size,
209 &iq->sglist_dma, GFP_KERNEL);
210 if (unlikely(!iq->sglist)) {
212 "Failed to allocate DMA memory for IQ-%d SGLIST\n",
214 goto sglist_alloc_err;
217 /* allocate memory to manage Tx packets pending completion */
218 buff_info_size = OCTEP_IQ_TXBUFF_INFO_SIZE * iq->max_count;
219 iq->buff_info = vzalloc(buff_info_size);
220 if (!iq->buff_info) {
222 "Failed to allocate buff info for IQ-%d\n", q_no);
226 /* Setup sglist addresses in tx_buffer entries */
227 for (i = 0; i < CFG_GET_IQ_NUM_DESC(oct->conf); i++) {
228 struct octep_tx_buffer *tx_buffer;
230 tx_buffer = &iq->buff_info[i];
232 &iq->sglist[i * OCTEP_SGLIST_ENTRIES_PER_PKT];
233 tx_buffer->sglist_dma =
234 iq->sglist_dma + (i * OCTEP_SGLIST_SIZE_PER_PKT);
237 octep_iq_reset_indices(iq);
238 oct->hw_ops.setup_iq_regs(oct, q_no);
244 dma_free_coherent(iq->dev, sglist_size, iq->sglist, iq->sglist_dma);
246 dma_free_coherent(iq->dev, desc_ring_size,
247 iq->desc_ring, iq->desc_ring_dma);
250 oct->iq[q_no] = NULL;
256 * octep_free_iq() - Free Tx queue resources.
258 * @iq: Octeon Tx queue data structure.
260 * Free all the resources allocated for a Tx queue.
262 static void octep_free_iq(struct octep_iq *iq)
264 struct octep_device *oct = iq->octep_dev;
265 u64 desc_ring_size, sglist_size;
268 desc_ring_size = OCTEP_IQ_DESC_SIZE * CFG_GET_IQ_NUM_DESC(oct->conf);
270 vfree(iq->buff_info);
273 dma_free_coherent(iq->dev, desc_ring_size,
274 iq->desc_ring, iq->desc_ring_dma);
276 sglist_size = OCTEP_SGLIST_SIZE_PER_PKT *
277 CFG_GET_IQ_NUM_DESC(oct->conf);
279 dma_free_coherent(iq->dev, sglist_size,
280 iq->sglist, iq->sglist_dma);
283 oct->iq[q_no] = NULL;
288 * octep_setup_iqs() - setup resources for all Tx queues.
290 * @oct: Octeon device private data structure.
292 int octep_setup_iqs(struct octep_device *oct)
297 for (i = 0; i < CFG_GET_PORTS_ACTIVE_IO_RINGS(oct->conf); i++) {
298 if (octep_setup_iq(oct, i)) {
299 dev_err(&oct->pdev->dev,
300 "Failed to setup IQ(TxQ)-%d.\n", i);
303 dev_dbg(&oct->pdev->dev, "Successfully setup IQ(TxQ)-%d.\n", i);
311 octep_free_iq(oct->iq[i]);
317 * octep_free_iqs() - Free resources of all Tx queues.
319 * @oct: Octeon device private data structure.
321 void octep_free_iqs(struct octep_device *oct)
325 for (i = 0; i < CFG_GET_PORTS_ACTIVE_IO_RINGS(oct->conf); i++) {
326 octep_free_iq(oct->iq[i]);
327 dev_dbg(&oct->pdev->dev,
328 "Successfully destroyed IQ(TxQ)-%d.\n", i);