blob: bd69dd82efa336b81da487358d92655603b62812 [file] [log] [blame]
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301/*
Rakesh Nair8016fbd2018-01-03 15:46:06 +05302 * Copyright (c) 2014 - 2018, The Linux Foundation. All rights reserved.
Rakesh Nair9bcf2602017-01-06 16:02:16 +05303 *
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 */
15
16#include <linux/platform_device.h>
17#include <linux/if_vlan.h>
Rakesh Nair888af952017-06-30 18:41:58 +053018#include <linux/kernel.h>
Rakesh Nairc402d752018-01-20 16:21:54 +053019#include <asm/atomic.h>
Rakesh Nair9bcf2602017-01-06 16:02:16 +053020#include "ess_edma.h"
21#include "edma.h"
22
23extern struct net_device *edma_netdev[EDMA_MAX_PORTID_SUPPORTED];
Rakesh Naird4a11502017-11-07 17:02:11 +053024extern u32 edma_disable_queue_stop;
25
Rakesh Nair9bcf2602017-01-06 16:02:16 +053026bool edma_stp_rstp;
27u16 edma_ath_eth_type;
Rakesh Nair888af952017-06-30 18:41:58 +053028extern u8 edma_dscp2ac_tbl[EDMA_PRECEDENCE_MAX];
29extern u8 edma_per_prec_stats_enable;
Rakesh Nair1c6a18c2017-08-02 21:27:06 +053030extern u32 edma_iad_stats_enable;
Rakesh Nair9bcf2602017-01-06 16:02:16 +053031
32/* edma_skb_priority_offset()
33 * get edma skb priority
34 */
35static unsigned int edma_skb_priority_offset(struct sk_buff *skb)
36{
37 return (skb->priority >> 2) & 1;
38}
39
40/* edma_alloc_tx_ring()
41 * Allocate Tx descriptors ring
42 */
43static int edma_alloc_tx_ring(struct edma_common_info *edma_cinfo,
44 struct edma_tx_desc_ring *etdr)
45{
46 struct platform_device *pdev = edma_cinfo->pdev;
47 u16 sw_size = sizeof(struct edma_sw_desc) * etdr->count;
48
49 /* Initialize ring */
50 etdr->size = sizeof(struct edma_tx_desc) * etdr->count;
51 etdr->sw_next_to_fill = 0;
52 etdr->sw_next_to_clean = 0;
53
54 /* Allocate SW descriptors */
55 etdr->sw_desc = vzalloc(sw_size);
56 if (!etdr->sw_desc) {
57 dev_err(&pdev->dev, "buffer alloc of tx ring failed=%p", etdr);
58 return -ENOMEM;
59 }
60
61 /* Allocate HW descriptors */
62 etdr->hw_desc = dma_alloc_coherent(&pdev->dev, etdr->size, &etdr->dma,
63 GFP_KERNEL);
64 if (!etdr->hw_desc) {
65 dev_err(&pdev->dev, "descriptor allocation for tx ring failed");
66 vfree(etdr->sw_desc);
67 etdr->sw_desc = NULL;
68 return -ENOMEM;
69 }
70
71 return 0;
72}
73
74/* edma_free_tx_ring()
75 * Free tx rings allocated by edma_alloc_tx_rings
76 */
77static void edma_free_tx_ring(struct edma_common_info *edma_cinfo,
78 struct edma_tx_desc_ring *etdr)
79{
80 struct platform_device *pdev = edma_cinfo->pdev;
81
82 if (likely(etdr->hw_desc)) {
83 dma_free_coherent(&pdev->dev, etdr->size, etdr->hw_desc,
84 etdr->dma);
85
86 vfree(etdr->sw_desc);
87 etdr->sw_desc = NULL;
88 }
89}
90
91/* edma_alloc_rx_ring()
92 * allocate rx descriptor ring
93 */
94static int edma_alloc_rx_ring(struct edma_common_info *edma_cinfo,
95 struct edma_rfd_desc_ring *erxd)
96{
97 struct platform_device *pdev = edma_cinfo->pdev;
98 u16 sw_size = sizeof(struct edma_sw_desc) * erxd->count;
99
100 erxd->size = sizeof(struct edma_sw_desc) * erxd->count;
101 erxd->sw_next_to_fill = 0;
102 erxd->sw_next_to_clean = 0;
103
104 /* Allocate SW descriptors */
105 erxd->sw_desc = vzalloc(sw_size);
106 if (!erxd->sw_desc)
107 return -ENOMEM;
108
109 /* Alloc HW descriptors */
110 erxd->hw_desc = dma_alloc_coherent(&pdev->dev, erxd->size, &erxd->dma,
111 GFP_KERNEL);
112 if (!erxd->hw_desc) {
113 vfree(erxd->sw_desc);
114 erxd->sw_desc = NULL;
115 return -ENOMEM;
116 }
117
Rakesh Nair03b586c2017-04-03 18:28:58 +0530118 /* Initialize pending fill */
119 erxd->pending_fill = 0;
120
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530121 return 0;
122}
123
124/* edma_free_rx_ring()
125 * Free rx ring allocated by alloc_rx_ring
126 */
127static void edma_free_rx_ring(struct edma_common_info *edma_cinfo,
128 struct edma_rfd_desc_ring *erxd)
129{
130 struct platform_device *pdev = edma_cinfo->pdev;
131
132 if (likely(erxd->hw_desc)) {
133 dma_free_coherent(&pdev->dev, erxd->size, erxd->hw_desc,
134 erxd->dma);
135
136 vfree(erxd->sw_desc);
137 erxd->sw_desc = NULL;
138 }
139}
140
141/* edma_configure_tx()
142 * Configure transmission control data
143 */
144static void edma_configure_tx(struct edma_common_info *edma_cinfo)
145{
146 u32 txq_ctrl_data;
147
148 txq_ctrl_data = (EDMA_TPD_BURST << EDMA_TXQ_NUM_TPD_BURST_SHIFT);
149 txq_ctrl_data |= EDMA_TXQ_CTRL_TPD_BURST_EN;
150 txq_ctrl_data |= (EDMA_TXF_BURST << EDMA_TXQ_TXF_BURST_NUM_SHIFT);
151 edma_write_reg(EDMA_REG_TXQ_CTRL, txq_ctrl_data);
152}
153
154/* edma_configure_rx()
155 * configure reception control data
156 */
157static void edma_configure_rx(struct edma_common_info *edma_cinfo)
158{
159 struct edma_hw *hw = &edma_cinfo->hw;
160 u32 rss_type, rx_desc1, rxq_ctrl_data;
161
162 /* Set RSS type */
163 rss_type = hw->rss_type;
164 edma_write_reg(EDMA_REG_RSS_TYPE, rss_type);
165
166 /* Set RFD burst number */
167 rx_desc1 = (EDMA_RFD_BURST << EDMA_RXQ_RFD_BURST_NUM_SHIFT);
168
169 /* Set RFD prefetch threshold */
170 rx_desc1 |= (EDMA_RFD_THR << EDMA_RXQ_RFD_PF_THRESH_SHIFT);
171
172 /* Set RFD in host ring low threshold to generte interrupt */
173 rx_desc1 |= (EDMA_RFD_LTHR << EDMA_RXQ_RFD_LOW_THRESH_SHIFT);
174 edma_write_reg(EDMA_REG_RX_DESC1, rx_desc1);
175
176 /* Set Rx FIFO threshold to start to DMA data to host */
177 rxq_ctrl_data = EDMA_FIFO_THRESH_128_BYTE;
178
179 /* Set RX remove vlan bit */
180 rxq_ctrl_data |= EDMA_RXQ_CTRL_RMV_VLAN;
181
182 edma_write_reg(EDMA_REG_RXQ_CTRL, rxq_ctrl_data);
183}
184
185/* edma_alloc_rx_buf()
186 * does skb allocation for the received packets.
187 */
188static int edma_alloc_rx_buf(struct edma_common_info
189 *edma_cinfo,
190 struct edma_rfd_desc_ring *erdr,
191 int cleaned_count, int queue_id)
192{
193 struct platform_device *pdev = edma_cinfo->pdev;
194 struct edma_rx_free_desc *rx_desc;
195 struct edma_sw_desc *sw_desc;
196 struct sk_buff *skb;
197 unsigned int i;
198 u16 prod_idx, length;
199 u32 reg_data;
200
201 if (cleaned_count > erdr->count) {
202 dev_err(&pdev->dev, "Incorrect cleaned_count %d",
203 cleaned_count);
204 return -1;
205 }
206
207 i = erdr->sw_next_to_fill;
208
209 while (cleaned_count) {
210 sw_desc = &erdr->sw_desc[i];
211 length = edma_cinfo->rx_head_buffer_len;
212
213 if (sw_desc->flags & EDMA_SW_DESC_FLAG_SKB_REUSE) {
214 skb = sw_desc->skb;
215
216 /* Clear REUSE flag */
217 sw_desc->flags &= ~EDMA_SW_DESC_FLAG_SKB_REUSE;
218 } else {
219 /* alloc skb */
220 skb = netdev_alloc_skb(edma_netdev[0], length);
221 if (!skb) {
222 /* Better luck next round */
223 sw_desc->flags = 0;
224 break;
225 }
226 }
227
228 if (!edma_cinfo->page_mode) {
229 sw_desc->dma = dma_map_single(&pdev->dev, skb->data,
230 length, DMA_FROM_DEVICE);
231 if (dma_mapping_error(&pdev->dev, sw_desc->dma)) {
232 WARN_ONCE(0, "EDMA DMA mapping failed for linear address %x", sw_desc->dma);
233 sw_desc->flags = 0;
234 sw_desc->skb = NULL;
235 dev_kfree_skb_any(skb);
236 break;
237 }
238
239 /*
240 * We should not exit from here with REUSE flag set
241 * This is to avoid re-using same sk_buff for next
242 * time around
243 */
244 sw_desc->flags = EDMA_SW_DESC_FLAG_SKB_HEAD;
245 sw_desc->length = length;
246 } else {
247 struct page *pg = alloc_page(GFP_ATOMIC);
248
249 if (!pg) {
250 sw_desc->flags = 0;
251 sw_desc->skb = NULL;
252 dev_kfree_skb_any(skb);
253 break;
254 }
255
256 sw_desc->dma = dma_map_page(&pdev->dev, pg, 0,
257 edma_cinfo->rx_page_buffer_len,
258 DMA_FROM_DEVICE);
259 if (dma_mapping_error(&pdev->dev, sw_desc->dma)) {
260 WARN_ONCE(0, "EDMA DMA mapping failed for page address %x", sw_desc->dma);
261 sw_desc->flags = 0;
262 sw_desc->skb = NULL;
263 __free_page(pg);
264 dev_kfree_skb_any(skb);
265 break;
266 }
267
268 skb_fill_page_desc(skb, 0, pg, 0,
269 edma_cinfo->rx_page_buffer_len);
270 sw_desc->flags = EDMA_SW_DESC_FLAG_SKB_FRAG;
271 sw_desc->length = edma_cinfo->rx_page_buffer_len;
272 }
273
274 /* Update the buffer info */
275 sw_desc->skb = skb;
276 rx_desc = (&(erdr->hw_desc)[i]);
277 rx_desc->buffer_addr = cpu_to_le64(sw_desc->dma);
278 if (++i == erdr->count)
279 i = 0;
280 cleaned_count--;
281 }
282
283 erdr->sw_next_to_fill = i;
284
285 if (i == 0)
286 prod_idx = erdr->count - 1;
287 else
288 prod_idx = i - 1;
289
290 /* Update the producer index */
291 edma_read_reg(EDMA_REG_RFD_IDX_Q(queue_id), &reg_data);
292 reg_data &= ~EDMA_RFD_PROD_IDX_BITS;
293 reg_data |= prod_idx;
294 edma_write_reg(EDMA_REG_RFD_IDX_Q(queue_id), reg_data);
Rakesh Nair03b586c2017-04-03 18:28:58 +0530295
296 /* If we couldn't allocate all the buffers,
297 * we increment the alloc failure counters
298 */
299 if (cleaned_count)
300 edma_cinfo->edma_ethstats.rx_alloc_fail_ctr++;
301
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530302 return cleaned_count;
303}
304
305/* edma_init_desc()
306 * update descriptor ring size, buffer and producer/consumer index
307 */
308static void edma_init_desc(struct edma_common_info *edma_cinfo)
309{
310 struct edma_rfd_desc_ring *rfd_ring;
311 struct edma_tx_desc_ring *etdr;
312 int i = 0, j = 0;
313 u32 data = 0;
314 u16 hw_cons_idx = 0;
315
316 /* Set the base address of every TPD ring. */
317 for (i = 0; i < edma_cinfo->num_tx_queues; i++) {
318 etdr = edma_cinfo->tpd_ring[i];
319
320 /* Update descriptor ring base address */
321 edma_write_reg(EDMA_REG_TPD_BASE_ADDR_Q(i), (u32)etdr->dma);
322 edma_read_reg(EDMA_REG_TPD_IDX_Q(i), &data);
323
324 /* Calculate hardware consumer index */
325 hw_cons_idx = (data >> EDMA_TPD_CONS_IDX_SHIFT) & 0xffff;
326 etdr->sw_next_to_fill = hw_cons_idx;
327 etdr->sw_next_to_clean = hw_cons_idx;
328 data &= ~(EDMA_TPD_PROD_IDX_MASK << EDMA_TPD_PROD_IDX_SHIFT);
329 data |= hw_cons_idx;
330
331 /* update producer index */
332 edma_write_reg(EDMA_REG_TPD_IDX_Q(i), data);
333
334 /* update SW consumer index register */
335 edma_write_reg(EDMA_REG_TX_SW_CONS_IDX_Q(i), hw_cons_idx);
336
337 /* Set TPD ring size */
338 edma_write_reg(EDMA_REG_TPD_RING_SIZE,
339 edma_cinfo->tx_ring_count &
340 EDMA_TPD_RING_SIZE_MASK);
341 }
342
343 for (i = 0, j = 0; i < edma_cinfo->num_rx_queues; i++) {
344 rfd_ring = edma_cinfo->rfd_ring[j];
345 /* Update Receive Free descriptor ring base address */
346 edma_write_reg(EDMA_REG_RFD_BASE_ADDR_Q(j),
347 (u32)(rfd_ring->dma));
348 j += ((edma_cinfo->num_rx_queues == 4) ? 2 : 1);
349 }
350
351 data = edma_cinfo->rx_head_buffer_len;
352 if (edma_cinfo->page_mode)
353 data = edma_cinfo->rx_page_buffer_len;
354
355 data &= EDMA_RX_BUF_SIZE_MASK;
356 data <<= EDMA_RX_BUF_SIZE_SHIFT;
357
358 /* Update RFD ring size and RX buffer size */
359 data |= (edma_cinfo->rx_ring_count & EDMA_RFD_RING_SIZE_MASK)
360 << EDMA_RFD_RING_SIZE_SHIFT;
361
362 edma_write_reg(EDMA_REG_RX_DESC0, data);
363
364 /* Disable TX FIFO low watermark and high watermark */
365 edma_write_reg(EDMA_REG_TXF_WATER_MARK, 0);
366
367 /* Load all of base address above */
368 edma_read_reg(EDMA_REG_TX_SRAM_PART, &data);
369 data |= 1 << EDMA_LOAD_PTR_SHIFT;
370 edma_write_reg(EDMA_REG_TX_SRAM_PART, data);
371}
372
373/* edma_receive_checksum
374 * Api to check checksum on receive packets
375 */
376static void edma_receive_checksum(struct edma_rx_return_desc *rd,
377 struct sk_buff *skb)
378{
379 skb_checksum_none_assert(skb);
380
381 /* check the RRD IP/L4 checksum bit to see if
382 * its set, which in turn indicates checksum
383 * failure.
384 */
385 if (rd->rrd6 & EDMA_RRD_CSUM_FAIL_MASK)
386 return;
387
Rakesh Nair72e1d282017-05-19 22:21:01 +0530388 /*
389 * We disable checksum verification only if
390 * we have a TCP/UDP packet
391 */
392 if (rd->rrd7 & (EDMA_RRD_L4OFFSET_MASK << EDMA_RRD_L4OFFSET_SHIFT))
393 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530394}
395
396/* edma_clean_rfd()
397 * clean up rx resourcers on error
398 */
399static void edma_clean_rfd(struct platform_device *pdev,
400 struct edma_rfd_desc_ring *erdr,
401 u16 index,
402 int pos)
403{
404 struct edma_rx_free_desc *rx_desc = &(erdr->hw_desc[index]);
405 struct edma_sw_desc *sw_desc = &erdr->sw_desc[index];
406
407 /* Unmap non-first RFD positions in packet */
408 if (pos) {
409 if (likely(sw_desc->flags & EDMA_SW_DESC_FLAG_SKB_HEAD))
410 dma_unmap_single(&pdev->dev, sw_desc->dma,
411 sw_desc->length, DMA_FROM_DEVICE);
412 else
413 dma_unmap_page(&pdev->dev, sw_desc->dma,
414 sw_desc->length, DMA_FROM_DEVICE);
415 }
416
417 if (sw_desc->skb) {
418 dev_kfree_skb_any(sw_desc->skb);
419 sw_desc->skb = NULL;
420 }
421
422 sw_desc->flags = 0;
423 memset(rx_desc, 0, sizeof(struct edma_rx_free_desc));
424}
425
426/* edma_rx_complete_stp_rstp()
427 * Complete Rx processing for STP RSTP packets
428 */
429static void edma_rx_complete_stp_rstp(struct sk_buff *skb, int port_id, struct edma_rx_return_desc *rd)
430{
431 int i;
432 u32 priority;
433 u16 port_type;
434 u8 mac_addr[EDMA_ETH_HDR_LEN];
435
436 port_type = (rd->rrd1 >> EDMA_RRD_PORT_TYPE_SHIFT)
437 & EDMA_RRD_PORT_TYPE_MASK;
438 /* if port type is 0x4, then only proceed with
439 * other stp/rstp calculation
440 */
441 if (port_type == EDMA_RX_ATH_HDR_RSTP_PORT_TYPE) {
442 u8 bpdu_mac[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00};
443
444 /* calculate the frame priority */
445 priority = (rd->rrd1 >> EDMA_RRD_PRIORITY_SHIFT)
446 & EDMA_RRD_PRIORITY_MASK;
447
448 for (i = 0; i < EDMA_ETH_HDR_LEN; i++)
449 mac_addr[i] = skb->data[i];
450
451 /* Check if destination mac addr is bpdu addr */
452 if (!memcmp(mac_addr, bpdu_mac, 6)) {
453 /* destination mac address is BPDU
454 * destination mac address, then add
455 * atheros header to the packet.
456 */
457 u16 athr_hdr = (EDMA_RX_ATH_HDR_VERSION << EDMA_RX_ATH_HDR_VERSION_SHIFT) |
458 (priority << EDMA_RX_ATH_HDR_PRIORITY_SHIFT) |
459 (EDMA_RX_ATH_HDR_RSTP_PORT_TYPE << EDMA_RX_ATH_PORT_TYPE_SHIFT) | port_id;
460 skb_push(skb, 4);
461 memcpy(skb->data, mac_addr, EDMA_ETH_HDR_LEN);
462 *(uint16_t *)&skb->data[12] = htons(edma_ath_eth_type);
463 *(uint16_t *)&skb->data[14] = htons(athr_hdr);
464 }
465 }
466}
467
468/* edma_rx_complete_fraglist()
469 * Complete Rx processing for fraglist skbs
470 */
471static int edma_rx_complete_fraglist(struct sk_buff *skb, u16 num_rfds, u16 length, u32 sw_next_to_clean,
472 struct edma_rfd_desc_ring *erdr, struct edma_common_info *edma_cinfo)
473{
474 struct platform_device *pdev = edma_cinfo->pdev;
475 struct edma_hw *hw = &edma_cinfo->hw;
476 struct sk_buff *skb_temp;
477 struct edma_sw_desc *sw_desc;
478 int i;
479 u16 size_remaining;
480
481 skb->data_len = 0;
482 skb->tail += (hw->rx_head_buff_size - 16);
483 skb->len = skb->truesize = length;
484 size_remaining = length - (hw->rx_head_buff_size - 16);
485
486 /* clean-up all related sw_descs */
487 for (i = 1; i < num_rfds; i++) {
488 struct sk_buff *skb_prev;
489
490 sw_desc = &erdr->sw_desc[sw_next_to_clean];
491 skb_temp = sw_desc->skb;
492
493 dma_unmap_single(&pdev->dev, sw_desc->dma,
494 sw_desc->length, DMA_FROM_DEVICE);
495
496 if (size_remaining < hw->rx_head_buff_size)
497 skb_put(skb_temp, size_remaining);
498 else
499 skb_put(skb_temp, hw->rx_head_buff_size);
500
501 /* If we are processing the first rfd, we link
502 * skb->frag_list to the skb corresponding to the
503 * first RFD
504 */
505 if (i == 1)
506 skb_shinfo(skb)->frag_list = skb_temp;
507 else
508 skb_prev->next = skb_temp;
509 skb_prev = skb_temp;
510 skb_temp->next = NULL;
511
512 skb->data_len += skb_temp->len;
513 size_remaining -= skb_temp->len;
514
515 /* Increment SW index */
516 sw_next_to_clean = (sw_next_to_clean + 1) & (erdr->count - 1);
517 }
518
519 return sw_next_to_clean;
520}
521
522/* edma_rx_complete_paged()
523 * Complete Rx processing for paged skbs
524 */
525static int edma_rx_complete_paged(struct sk_buff *skb, u16 num_rfds,
526 u16 length, u32 sw_next_to_clean,
527 struct edma_rfd_desc_ring *erdr,
528 struct edma_common_info *edma_cinfo)
529{
530 struct platform_device *pdev = edma_cinfo->pdev;
531 struct sk_buff *skb_temp;
532 struct edma_sw_desc *sw_desc;
533 int i;
534 u16 size_remaining;
535
536 skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
537
538 /* Setup skbuff fields */
539 skb->len = length;
540
541 if (likely(num_rfds <= 1)) {
542 skb->data_len = length;
543 skb->truesize += edma_cinfo->rx_page_buffer_len;
544 skb_fill_page_desc(skb, 0, skb_frag_page(frag),
545 16, length);
546 } else {
547 frag->size -= 16;
548 skb->data_len = frag->size;
549 skb->truesize += edma_cinfo->rx_page_buffer_len;
550 size_remaining = length - frag->size;
551
552 skb_fill_page_desc(skb, 0, skb_frag_page(frag),
553 16, frag->size);
554
555 /* clean-up all related sw_descs */
556 for (i = 1; i < num_rfds; i++) {
557 sw_desc = &erdr->sw_desc[sw_next_to_clean];
558 skb_temp = sw_desc->skb;
559 frag = &skb_shinfo(skb_temp)->frags[0];
560 dma_unmap_page(&pdev->dev, sw_desc->dma,
561 sw_desc->length, DMA_FROM_DEVICE);
562
563 if (size_remaining < edma_cinfo->rx_page_buffer_len)
564 frag->size = size_remaining;
565
566 skb_fill_page_desc(skb, i, skb_frag_page(frag),
567 0, frag->size);
568
569 /* We used frag pages from skb_temp in skb */
570 skb_shinfo(skb_temp)->nr_frags = 0;
571 dev_kfree_skb_any(skb_temp);
572
573 skb->data_len += frag->size;
574 skb->truesize += edma_cinfo->rx_page_buffer_len;
575 size_remaining -= frag->size;
576
577 /* Increment SW index */
578 sw_next_to_clean = (sw_next_to_clean + 1) & (erdr->count - 1);
579 }
580 }
581
582 return sw_next_to_clean;
583}
584
585/*
586 * edma_rx_complete()
587 * Main api called from the poll function to process rx packets.
588 */
Rakesh Nair03b586c2017-04-03 18:28:58 +0530589static u16 edma_rx_complete(struct edma_common_info *edma_cinfo,
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530590 int *work_done, int work_to_do, int queue_id,
591 struct napi_struct *napi)
592{
593 struct platform_device *pdev = edma_cinfo->pdev;
594 struct edma_rfd_desc_ring *erdr = edma_cinfo->rfd_ring[queue_id];
595 u16 hash_type, rrd[8], cleaned_count = 0, length = 0, num_rfds = 1,
596 sw_next_to_clean, hw_next_to_clean = 0, vlan = 0, ret_count = 0;
597 u32 data = 0;
598 u16 count = erdr->count, rfd_avail;
599 u8 queue_to_rxid[8] = {0, 0, 1, 1, 2, 2, 3, 3};
600
Rakesh Nair03b586c2017-04-03 18:28:58 +0530601 cleaned_count = erdr->pending_fill;
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530602 sw_next_to_clean = erdr->sw_next_to_clean;
603
604 edma_read_reg(EDMA_REG_RFD_IDX_Q(queue_id), &data);
605 hw_next_to_clean = (data >> EDMA_RFD_CONS_IDX_SHIFT) &
606 EDMA_RFD_CONS_IDX_MASK;
607
608 do {
609 while (sw_next_to_clean != hw_next_to_clean) {
610 struct net_device *netdev;
611 struct edma_adapter *adapter;
612 struct edma_sw_desc *sw_desc;
613 struct sk_buff *skb;
614 struct edma_rx_return_desc *rd;
615 u8 *vaddr;
616 int port_id, i, drop_count = 0;
617 u32 priority;
618
619 if (!work_to_do)
620 break;
621
622 sw_desc = &erdr->sw_desc[sw_next_to_clean];
623 skb = sw_desc->skb;
624
625 /* Get RRD */
626 if (!edma_cinfo->page_mode) {
627 dma_unmap_single(&pdev->dev, sw_desc->dma,
628 sw_desc->length, DMA_FROM_DEVICE);
629 rd = (struct edma_rx_return_desc *)skb->data;
630
631 } else {
632 dma_unmap_page(&pdev->dev, sw_desc->dma,
633 sw_desc->length, DMA_FROM_DEVICE);
634 vaddr = kmap_atomic(skb_frag_page(&skb_shinfo(skb)->frags[0]));
635 memcpy((uint8_t *)&rrd[0], vaddr, 16);
636 rd = (struct edma_rx_return_desc *)rrd;
637 kunmap_atomic(vaddr);
638 }
639
640 /* Check if RRD is valid */
641 if (!(rd->rrd7 & EDMA_RRD_DESC_VALID)) {
642 dev_err(&pdev->dev, "Incorrect RRD DESC valid bit set");
643 edma_clean_rfd(pdev, erdr, sw_next_to_clean, 0);
644 sw_next_to_clean = (sw_next_to_clean + 1) &
645 (erdr->count - 1);
646 cleaned_count++;
647 continue;
648 }
649
650 /* Get the number of RFDs from RRD */
651 num_rfds = rd->rrd1 & EDMA_RRD_NUM_RFD_MASK;
652
653 /* Get Rx port ID from switch */
654 port_id = (rd->rrd1 >> EDMA_PORT_ID_SHIFT) & EDMA_PORT_ID_MASK;
655 if ((!port_id) || (port_id > EDMA_MAX_PORTID_SUPPORTED)) {
656 if (net_ratelimit()) {
657 dev_err(&pdev->dev, "Incorrect RRD source port bit set");
658 dev_err(&pdev->dev,
659 "RRD Dump\n rrd0:%x rrd1: %x rrd2: %x rrd3: %x rrd4: %x rrd5: %x rrd6: %x rrd7: %x",
660 rd->rrd0, rd->rrd1, rd->rrd2, rd->rrd3, rd->rrd4, rd->rrd5, rd->rrd6, rd->rrd7);
661 dev_err(&pdev->dev, "Num_rfds: %d, src_port: %d, pkt_size: %d, cvlan_tag: %d\n",
662 num_rfds, rd->rrd1 & EDMA_RRD_SRC_PORT_NUM_MASK,
663 rd->rrd6 & EDMA_RRD_PKT_SIZE_MASK, rd->rrd7 & EDMA_RRD_CVLAN);
664 }
665 for (i = 0; i < num_rfds; i++) {
666 edma_clean_rfd(pdev, erdr, sw_next_to_clean, i);
667 sw_next_to_clean = (sw_next_to_clean + 1) & (erdr->count - 1);
668 }
669
670 cleaned_count += num_rfds;
671 continue;
672 }
673
674 netdev = edma_cinfo->portid_netdev_lookup_tbl[port_id];
675 if (!netdev) {
676 dev_err(&pdev->dev, "Invalid netdev");
677 for (i = 0; i < num_rfds; i++) {
678 edma_clean_rfd(pdev, erdr, sw_next_to_clean, i);
679 sw_next_to_clean = (sw_next_to_clean + 1) & (erdr->count - 1);
680 }
681
682 cleaned_count += num_rfds;
683 continue;
684 }
685 adapter = netdev_priv(netdev);
686
687 /* This code is added to handle a usecase where high
688 * priority stream and a low priority stream are
689 * received simultaneously on DUT. The problem occurs
690 * if one of the Rx rings is full and the corresponding
691 * core is busy with other stuff. This causes ESS CPU
692 * port to backpressure all incoming traffic including
693 * high priority one. We monitor free descriptor count
694 * on each CPU and whenever it reaches threshold (< 80),
695 * we drop all low priority traffic and let only high
696 * priotiy traffic pass through. We can hence avoid
697 * ESS CPU port to send backpressure on high priroity
698 * stream.
699 */
700 priority = (rd->rrd1 >> EDMA_RRD_PRIORITY_SHIFT)
701 & EDMA_RRD_PRIORITY_MASK;
702 if (likely(!priority && !edma_cinfo->page_mode && (num_rfds <= 1))) {
703 rfd_avail = (count + sw_next_to_clean - hw_next_to_clean - 1) & (count - 1);
704 if (rfd_avail < EDMA_RFD_AVAIL_THR) {
705 sw_desc->flags |= EDMA_SW_DESC_FLAG_SKB_REUSE;
706 sw_next_to_clean = (sw_next_to_clean + 1) & (erdr->count - 1);
707 adapter->stats.rx_dropped++;
708 cleaned_count++;
709 drop_count++;
710 if (drop_count == 3) {
711 work_to_do--;
712 (*work_done)++;
713 drop_count = 0;
714 }
715 if (cleaned_count == EDMA_RX_BUFFER_WRITE) {
716 /* If buffer clean count reaches 16, we replenish HW buffers. */
717 ret_count = edma_alloc_rx_buf(edma_cinfo, erdr, cleaned_count, queue_id);
718 edma_write_reg(EDMA_REG_RX_SW_CONS_IDX_Q(queue_id),
719 sw_next_to_clean);
720 cleaned_count = ret_count;
Rakesh Nair03b586c2017-04-03 18:28:58 +0530721 erdr->pending_fill = ret_count;
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530722 }
723 continue;
724 }
725 }
726
727 work_to_do--;
728 (*work_done)++;
729
730 /* Increment SW index */
731 sw_next_to_clean = (sw_next_to_clean + 1) &
732 (erdr->count - 1);
733
734 /* Get the packet size and allocate buffer */
735 length = rd->rrd6 & EDMA_RRD_PKT_SIZE_MASK;
736
737 if (edma_cinfo->page_mode) {
738 /* paged skb */
739 sw_next_to_clean = edma_rx_complete_paged(skb, num_rfds, length,
740 sw_next_to_clean,
741 erdr, edma_cinfo);
742 if (!pskb_may_pull(skb, ETH_HLEN)) {
743 cleaned_count += num_rfds;
744 dev_kfree_skb_any(skb);
745 continue;
746 }
747 } else {
748 /* single or fraglist skb */
749
750 /* Addition of 16 bytes is required, as in the packet
751 * first 16 bytes are rrd descriptors, so actual data
752 * starts from an offset of 16.
753 */
754 skb_reserve(skb, 16);
755 if (likely((num_rfds <= 1) || !edma_cinfo->fraglist_mode))
756 skb_put(skb, length);
757 else
758 sw_next_to_clean = edma_rx_complete_fraglist(skb, num_rfds, length,
759 sw_next_to_clean,
760 erdr, edma_cinfo);
761 }
762
763 cleaned_count += num_rfds;
764
765 if (edma_stp_rstp)
766 edma_rx_complete_stp_rstp(skb, port_id, rd);
767
768 skb->protocol = eth_type_trans(skb, netdev);
769
770 /* Record Rx queue for RFS/RPS and fill flow hash from HW */
771 skb_record_rx_queue(skb, queue_to_rxid[queue_id]);
772 if (netdev->features & NETIF_F_RXHASH) {
773 hash_type = (rd->rrd5 >> EDMA_HASH_TYPE_SHIFT);
774 if ((hash_type > EDMA_HASH_TYPE_START) && (hash_type < EDMA_HASH_TYPE_END))
775 skb_set_hash(skb, rd->rrd2, PKT_HASH_TYPE_L4);
776 }
777
778#ifdef CONFIG_NF_FLOW_COOKIE
779 skb->flow_cookie = rd->rrd3 & EDMA_RRD_FLOW_COOKIE_MASK;
780#endif
781 edma_receive_checksum(rd, skb);
782
783 /* Process VLAN HW acceleration indication provided by HW */
784 if (adapter->default_vlan_tag != rd->rrd4) {
785 vlan = rd->rrd4;
786 if (likely(rd->rrd7 & EDMA_RRD_CVLAN))
787 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
788 else if (rd->rrd1 & EDMA_RRD_SVLAN)
789 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021AD), vlan);
790 }
791
792 /* Update rx statistics */
793 adapter->stats.rx_packets++;
794 adapter->stats.rx_bytes += length;
795
796 /* Check if we reached refill threshold */
797 if (cleaned_count == EDMA_RX_BUFFER_WRITE) {
798 ret_count = edma_alloc_rx_buf(edma_cinfo, erdr, cleaned_count, queue_id);
799 edma_write_reg(EDMA_REG_RX_SW_CONS_IDX_Q(queue_id),
800 sw_next_to_clean);
801 cleaned_count = ret_count;
Rakesh Nair03b586c2017-04-03 18:28:58 +0530802 erdr->pending_fill = ret_count;
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530803 }
804
Rakesh Nair888af952017-06-30 18:41:58 +0530805 /*
806 * We increment per-precedence counters for the rx packets
807 */
808 if (edma_per_prec_stats_enable) {
Rakesh Nairc402d752018-01-20 16:21:54 +0530809 atomic_inc(&edma_cinfo->edma_ethstats.rx_prec[priority]);
Rakesh Nair888af952017-06-30 18:41:58 +0530810 edma_cinfo->edma_ethstats.rx_ac[edma_dscp2ac_tbl[priority]]++;
Rakesh Nair1c6a18c2017-08-02 21:27:06 +0530811
812 if (edma_iad_stats_enable) {
813 if (edma_dscp2ac_tbl[priority] == EDMA_AC_VI)
814 edma_iad_process_flow(edma_cinfo, skb, EDMA_INGRESS_DIR, priority);
815 }
Rakesh Nair888af952017-06-30 18:41:58 +0530816 }
817
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530818 /* At this point skb should go to stack */
819 napi_gro_receive(napi, skb);
820 }
821
822 /* Check if we still have NAPI budget */
823 if (!work_to_do)
824 break;
825
826 /* Read index once again since we still have NAPI budget */
827 edma_read_reg(EDMA_REG_RFD_IDX_Q(queue_id), &data);
828 hw_next_to_clean = (data >> EDMA_RFD_CONS_IDX_SHIFT) &
829 EDMA_RFD_CONS_IDX_MASK;
830 } while (hw_next_to_clean != sw_next_to_clean);
831
832 erdr->sw_next_to_clean = sw_next_to_clean;
833
834 /* Refill here in case refill threshold wasn't reached */
835 if (likely(cleaned_count)) {
836 ret_count = edma_alloc_rx_buf(edma_cinfo, erdr, cleaned_count, queue_id);
Rakesh Nair03b586c2017-04-03 18:28:58 +0530837 erdr->pending_fill = ret_count;
838 if (ret_count) {
839 if(net_ratelimit())
840 dev_dbg(&pdev->dev, "Edma not getting memory for descriptors.\n");
841 }
842
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530843 edma_write_reg(EDMA_REG_RX_SW_CONS_IDX_Q(queue_id),
844 erdr->sw_next_to_clean);
845 }
Rakesh Nair03b586c2017-04-03 18:28:58 +0530846
847 return erdr->pending_fill;
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530848}
849
850/* edma_delete_rfs_filter()
851 * Remove RFS filter from switch
852 */
853static int edma_delete_rfs_filter(struct edma_adapter *adapter,
854 struct edma_rfs_filter_node *filter_node)
855{
856 int res = -1;
857
858 if (likely(adapter->set_rfs_rule))
859 res = (*adapter->set_rfs_rule)(adapter->netdev,
860#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
861 filter_node->keys.src,
862 filter_node->keys.dst, filter_node->keys.port16[0],
863 filter_node->keys.port16[1],
864 filter_node->keys.ip_proto,
865#else
866 filter_node->keys.addrs.v4addrs.src,
867 filter_node->keys.addrs.v4addrs.dst, filter_node->keys.ports.src,
868 filter_node->keys.ports.dst,
869 filter_node->keys.basic.ip_proto,
870#endif
871 filter_node->rq_id,
872 0);
873
874 return res;
875}
876
877/* edma_add_rfs_filter()
878 * Add RFS filter to switch
879 */
880static int edma_add_rfs_filter(struct edma_adapter *adapter,
881 struct flow_keys *keys, u16 rq,
882 struct edma_rfs_filter_node *filter_node)
883{
884 int res = -1;
885
886#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
887 filter_node->keys.src = keys->src;
888 filter_node->keys.dst = keys->dst;
889 filter_node->keys.ports = keys->ports;
890 filter_node->keys.ip_proto = keys->ip_proto;
891#else
892 filter_node->keys.addrs.v4addrs.src = keys->addrs.v4addrs.src;
893 filter_node->keys.addrs.v4addrs.dst = keys->addrs.v4addrs.dst;
894 filter_node->keys.ports.ports = keys->ports.ports;
895 filter_node->keys.basic.ip_proto = keys->basic.ip_proto;
896#endif
897
898 /* Call callback registered by ESS driver */
899 if (likely(adapter->set_rfs_rule))
900#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
901 res = (*adapter->set_rfs_rule)(adapter->netdev, keys->src,
902 keys->dst, keys->port16[0], keys->port16[1],
903 keys->ip_proto, rq, 1);
904#else
905 res = (*adapter->set_rfs_rule)(adapter->netdev, keys->addrs.v4addrs.src,
906 keys->addrs.v4addrs.dst, keys->ports.src, keys->ports.dst,
907 keys->basic.ip_proto, rq, 1);
908#endif
909
910 return res;
911}
912
913/* edma_rfs_key_search()
914 * Look for existing RFS entry
915 */
916static struct edma_rfs_filter_node *edma_rfs_key_search(struct hlist_head *h,
917 struct flow_keys *key)
918{
919 struct edma_rfs_filter_node *p;
920
921 hlist_for_each_entry(p, h, node)
922#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
923 if (p->keys.src == key->src &&
924 p->keys.dst == key->dst &&
925 p->keys.ports == key->ports &&
926 p->keys.ip_proto == key->ip_proto)
927#else
928 if (p->keys.addrs.v4addrs.src == key->addrs.v4addrs.src &&
929 p->keys.addrs.v4addrs.dst == key->addrs.v4addrs.dst &&
930 p->keys.ports.ports == key->ports.ports &&
931 p->keys.basic.ip_proto == key->basic.ip_proto)
932#endif
933 return p;
934 return NULL;
935}
936
937/* edma_initialise_rfs_flow_table()
938 * Initialise EDMA RFS flow table
939 */
940static void edma_initialise_rfs_flow_table(struct edma_adapter *adapter)
941{
942 int i;
943
944 spin_lock_init(&adapter->rfs.rfs_ftab_lock);
945
946 /* Initialize EDMA flow hash table */
947 for (i = 0; i < EDMA_RFS_FLOW_ENTRIES; i++)
948 INIT_HLIST_HEAD(&adapter->rfs.hlist_head[i]);
949
950 adapter->rfs.max_num_filter = EDMA_RFS_FLOW_ENTRIES;
951 adapter->rfs.filter_available = adapter->rfs.max_num_filter;
952 adapter->rfs.hashtoclean = 0;
953
954 /* Add timer to get periodic RFS updates from OS */
955 init_timer(&adapter->rfs.expire_rfs);
956 adapter->rfs.expire_rfs.function = edma_flow_may_expire;
957 adapter->rfs.expire_rfs.data = (unsigned long)adapter;
958 mod_timer(&adapter->rfs.expire_rfs, jiffies + HZ/4);
959}
960
961/* edma_free_rfs_flow_table()
962 * Free EDMA RFS flow table
963 */
964static void edma_free_rfs_flow_table(struct edma_adapter *adapter)
965{
966 int i;
967
968 /* Remove sync timer */
969 del_timer_sync(&adapter->rfs.expire_rfs);
970 spin_lock_bh(&adapter->rfs.rfs_ftab_lock);
971
972 /* Free EDMA RFS table entries */
973 adapter->rfs.filter_available = 0;
974
975 /* Clean-up EDMA flow hash table */
976 for (i = 0; i < EDMA_RFS_FLOW_ENTRIES; i++) {
977 struct hlist_head *hhead;
978 struct hlist_node *tmp;
979 struct edma_rfs_filter_node *filter_node;
980 int res;
981
982 hhead = &adapter->rfs.hlist_head[i];
983 hlist_for_each_entry_safe(filter_node, tmp, hhead, node) {
984 res = edma_delete_rfs_filter(adapter, filter_node);
985 if (res < 0)
986 dev_warn(&adapter->netdev->dev,
987 "EDMA going down but RFS entry %d not allowed to be flushed by Switch",
988 filter_node->flow_id);
989 hlist_del(&filter_node->node);
990 kfree(filter_node);
991 }
992 }
993 spin_unlock_bh(&adapter->rfs.rfs_ftab_lock);
994}
995
996/* edma_tx_unmap_and_free()
997 * clean TX buffer
998 */
999static inline void edma_tx_unmap_and_free(struct platform_device *pdev,
1000 struct edma_sw_desc *sw_desc)
1001{
1002 struct sk_buff *skb = sw_desc->skb;
1003
1004 if (likely((sw_desc->flags & EDMA_SW_DESC_FLAG_SKB_HEAD) ||
1005 (sw_desc->flags & EDMA_SW_DESC_FLAG_SKB_FRAGLIST)))
1006 /* unmap_single for skb head area */
1007 dma_unmap_single(&pdev->dev, sw_desc->dma,
1008 sw_desc->length, DMA_TO_DEVICE);
1009 else if (sw_desc->flags & EDMA_SW_DESC_FLAG_SKB_FRAG)
1010 /* unmap page for paged fragments */
1011 dma_unmap_page(&pdev->dev, sw_desc->dma,
1012 sw_desc->length, DMA_TO_DEVICE);
1013
1014 if (likely(sw_desc->flags & EDMA_SW_DESC_FLAG_LAST))
1015 dev_kfree_skb_any(skb);
1016
1017 sw_desc->flags = 0;
1018}
1019
1020/* edma_tx_complete()
1021 * Used to clean tx queues and update hardware and consumer index
1022 */
1023static void edma_tx_complete(struct edma_common_info *edma_cinfo, int queue_id)
1024{
1025 struct edma_tx_desc_ring *etdr = edma_cinfo->tpd_ring[queue_id];
1026 struct edma_sw_desc *sw_desc;
1027 struct platform_device *pdev = edma_cinfo->pdev;
1028 int i;
1029
1030 u16 sw_next_to_clean = etdr->sw_next_to_clean;
1031 u16 hw_next_to_clean;
1032 u32 data = 0;
1033
1034 edma_read_reg(EDMA_REG_TPD_IDX_Q(queue_id), &data);
1035 hw_next_to_clean = (data >> EDMA_TPD_CONS_IDX_SHIFT) & EDMA_TPD_CONS_IDX_MASK;
1036
1037 /* clean the buffer here */
1038 while (sw_next_to_clean != hw_next_to_clean) {
1039 sw_desc = &etdr->sw_desc[sw_next_to_clean];
1040 edma_tx_unmap_and_free(pdev, sw_desc);
1041 sw_next_to_clean = (sw_next_to_clean + 1) & (etdr->count - 1);
1042 }
1043
1044 etdr->sw_next_to_clean = sw_next_to_clean;
1045
1046 /* update the TPD consumer index register */
1047 edma_write_reg(EDMA_REG_TX_SW_CONS_IDX_Q(queue_id), sw_next_to_clean);
1048
1049 /* Wake the queue if queue is stopped and netdev link is up */
1050 for (i = 0; i < EDMA_MAX_NETDEV_PER_QUEUE && etdr->nq[i] ; i++) {
1051 if (netif_tx_queue_stopped(etdr->nq[i])) {
1052 if ((etdr->netdev[i]) && netif_carrier_ok(etdr->netdev[i]))
1053 netif_tx_wake_queue(etdr->nq[i]);
1054 }
1055 }
1056}
1057
1058/* edma_get_tx_buffer()
1059 * Get sw_desc corresponding to the TPD
1060 */
1061static struct edma_sw_desc *edma_get_tx_buffer(struct edma_common_info *edma_cinfo,
1062 struct edma_tx_desc *tpd, int queue_id)
1063{
1064 struct edma_tx_desc_ring *etdr = edma_cinfo->tpd_ring[queue_id];
1065
1066 return &etdr->sw_desc[tpd - (struct edma_tx_desc *)etdr->hw_desc];
1067}
1068
1069/* edma_get_next_tpd()
1070 * Return a TPD descriptor for transfer
1071 */
1072static struct edma_tx_desc *edma_get_next_tpd(struct edma_common_info *edma_cinfo,
1073 int queue_id)
1074{
1075 struct edma_tx_desc_ring *etdr = edma_cinfo->tpd_ring[queue_id];
1076 u16 sw_next_to_fill = etdr->sw_next_to_fill;
1077 struct edma_tx_desc *tpd_desc =
1078 (&((struct edma_tx_desc *)(etdr->hw_desc))[sw_next_to_fill]);
1079
1080 etdr->sw_next_to_fill = (etdr->sw_next_to_fill + 1) & (etdr->count - 1);
1081
1082 return tpd_desc;
1083}
1084
1085/* edma_tpd_available()
1086 * Check number of free TPDs
1087 */
1088static inline u16 edma_tpd_available(struct edma_common_info *edma_cinfo,
1089 int queue_id)
1090{
1091 struct edma_tx_desc_ring *etdr = edma_cinfo->tpd_ring[queue_id];
1092
1093 u16 sw_next_to_fill;
1094 u16 sw_next_to_clean;
1095 u16 count = 0;
1096
1097 sw_next_to_clean = etdr->sw_next_to_clean;
1098 sw_next_to_fill = etdr->sw_next_to_fill;
1099
1100 if (likely(sw_next_to_clean <= sw_next_to_fill))
1101 count = etdr->count;
1102
1103 return count + sw_next_to_clean - sw_next_to_fill - 1;
1104}
1105
1106/* edma_tx_queue_get()
1107 * Get the starting number of the queue
1108 */
1109static inline int edma_tx_queue_get(struct edma_adapter *adapter,
1110 struct sk_buff *skb, int txq_id)
1111{
1112 /* skb->priority is used as an index to skb priority table
1113 * and based on packet priority, correspong queue is assigned.
1114 */
1115 return adapter->tx_start_offset[txq_id] + edma_skb_priority_offset(skb);
1116}
1117
1118/* edma_tx_update_hw_idx()
1119 * update the producer index for the ring transmitted
1120 */
1121static void edma_tx_update_hw_idx(struct edma_common_info *edma_cinfo,
1122 struct sk_buff *skb, int queue_id)
1123{
1124 struct edma_tx_desc_ring *etdr = edma_cinfo->tpd_ring[queue_id];
1125 u32 tpd_idx_data;
1126
1127 /* Read and update the producer index */
1128 edma_read_reg(EDMA_REG_TPD_IDX_Q(queue_id), &tpd_idx_data);
1129 tpd_idx_data &= ~EDMA_TPD_PROD_IDX_BITS;
1130 tpd_idx_data |= (etdr->sw_next_to_fill & EDMA_TPD_PROD_IDX_MASK)
1131 << EDMA_TPD_PROD_IDX_SHIFT;
1132
1133 edma_write_reg(EDMA_REG_TPD_IDX_Q(queue_id), tpd_idx_data);
1134}
1135
1136/* edma_rollback_tx()
1137 * Function to retrieve tx resources in case of error
1138 */
1139static void edma_rollback_tx(struct edma_adapter *adapter,
1140 struct edma_tx_desc *start_tpd, int queue_id)
1141{
1142 struct edma_tx_desc_ring *etdr = adapter->edma_cinfo->tpd_ring[queue_id];
1143 struct edma_sw_desc *sw_desc;
1144 struct edma_tx_desc *tpd = NULL;
1145 u16 start_index, index;
1146
1147 start_index = start_tpd - (struct edma_tx_desc *)(etdr->hw_desc);
1148
1149 index = start_index;
1150 while (index != etdr->sw_next_to_fill) {
1151 tpd = (&((struct edma_tx_desc *)(etdr->hw_desc))[index]);
1152 sw_desc = &etdr->sw_desc[index];
1153 edma_tx_unmap_and_free(adapter->pdev, sw_desc);
1154 memset(tpd, 0, sizeof(struct edma_tx_desc));
1155 if (++index == etdr->count)
1156 index = 0;
1157 }
1158 etdr->sw_next_to_fill = start_index;
1159}
1160
Rakesh Nair7e053532017-08-18 17:53:25 +05301161/* edma_get_v4_precedence()
1162 * Function to retrieve precedence for IPv4
1163 */
1164static inline int edma_get_v4_precedence(struct sk_buff *skb, int nh_offset, u8 *precedence)
1165{
1166 const struct iphdr *iph;
1167 struct iphdr iph_hdr;
1168
1169 iph = skb_header_pointer(skb, nh_offset, sizeof(iph_hdr), &iph_hdr);
1170
1171 if (!iph || iph->ihl < 5)
1172 return -1;
1173
1174 *precedence = iph->tos >> EDMA_DSCP_PREC_SHIFT;
1175
1176 return 0;
1177}
1178
1179/* edma_get_v6_precedence()
1180 * Function to retrieve precedence for IPv6
1181 */
1182static inline int edma_get_v6_precedence(struct sk_buff *skb, int nh_offset, u8 *precedence)
1183{
1184 const struct ipv6hdr *iph;
1185 struct ipv6hdr iph_hdr;
1186
1187 iph = skb_header_pointer(skb, nh_offset, sizeof(iph_hdr), &iph_hdr);
1188
1189 if (!iph)
1190 return -1;
1191
1192 *precedence = iph->priority >> EDMA_DSCP6_PREC_SHIFT;
1193
1194 return 0;
1195}
1196
1197/* edma_get_skb_precedence()
1198 * Function to retrieve precedence from skb
1199 */
1200static int edma_get_skb_precedence(struct sk_buff *skb, u8 *precedence)
1201{
1202 int nhoff = skb_network_offset(skb);
1203 __be16 proto = skb->protocol;
1204 int ret;
1205 struct pppoeh_proto *pppoeh, ppp_hdr;
1206
1207 switch(proto) {
1208 case __constant_htons(ETH_P_IP): {
1209 ret = edma_get_v4_precedence(skb, nhoff, precedence);
1210 if (ret)
1211 return -1;
1212 break;
1213 }
1214 case __constant_htons(ETH_P_IPV6): {
1215 ret = edma_get_v6_precedence(skb, nhoff, precedence);
1216 if (ret)
1217 return -1;
1218 break;
1219 }
1220 case __constant_htons(ETH_P_PPP_SES): {
1221 pppoeh = skb_header_pointer(skb, nhoff, sizeof(ppp_hdr), &ppp_hdr);
1222 if (!pppoeh)
1223 return -1;
1224
1225 proto = pppoeh->proto;
1226 nhoff += PPPOE_SES_HLEN;
1227 switch (proto) {
1228 case __constant_htons(PPP_IP): {
1229 ret = edma_get_v4_precedence(skb, nhoff, precedence);
1230 if (ret)
1231 return -1;
1232 break;
1233 }
1234 case __constant_htons(PPP_IPV6): {
1235 ret = edma_get_v6_precedence(skb, nhoff, precedence);
1236 if (ret)
1237 return -1;
1238 break;
1239 }
1240 default:
1241 return -1;
1242 }
1243 break;
1244 }
1245 default:
1246 return -1;
1247 }
1248
1249 return 0;
1250}
1251
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301252/* edma_tx_map_and_fill()
1253 * gets called from edma_xmit_frame
1254 *
1255 * This is where the dma of the buffer to be transmitted
1256 * gets mapped
1257 */
1258static int edma_tx_map_and_fill(struct edma_common_info *edma_cinfo,
1259 struct edma_adapter *adapter,
1260 struct sk_buff *skb, int queue_id,
1261 unsigned int flags_transmit,
1262 u16 from_cpu, u16 dp_bitmap,
1263 bool packet_is_rstp, int nr_frags)
1264{
1265 struct edma_sw_desc *sw_desc = NULL;
1266 struct platform_device *pdev = edma_cinfo->pdev;
1267 struct edma_tx_desc *tpd = NULL;
1268 struct edma_tx_desc *start_tpd = NULL;
1269 struct sk_buff *iter_skb;
1270 int i;
1271 u32 word1 = 0, word3 = 0, lso_word1 = 0, svlan_tag = 0;
1272 u16 buf_len, lso_desc_len = 0;
1273
1274 if (skb_is_gso(skb)) {
1275 /* TODO: What additional checks need to be performed here */
1276 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1277 lso_word1 |= EDMA_TPD_IPV4_EN;
1278 ip_hdr(skb)->check = 0;
1279 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1280 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
1281 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
1282 lso_word1 |= EDMA_TPD_LSO_V2_EN;
1283 ipv6_hdr(skb)->payload_len = 0;
1284 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1285 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
1286 } else
1287 return -EINVAL;
1288
1289 lso_word1 |= EDMA_TPD_LSO_EN | ((skb_shinfo(skb)->gso_size & EDMA_TPD_MSS_MASK) << EDMA_TPD_MSS_SHIFT) |
1290 (skb_transport_offset(skb) << EDMA_TPD_HDR_SHIFT);
1291 } else if (flags_transmit & EDMA_HW_CHECKSUM) {
1292 u8 css, cso;
1293 cso = skb_checksum_start_offset(skb);
1294 css = cso + skb->csum_offset;
1295
1296 word1 |= (EDMA_TPD_CUSTOM_CSUM_EN);
1297 word1 |= (cso >> 1) << EDMA_TPD_HDR_SHIFT;
1298 word1 |= ((css >> 1) << EDMA_TPD_CUSTOM_CSUM_SHIFT);
1299 }
1300
1301 if (skb->protocol == htons(ETH_P_PPP_SES))
1302 word1 |= EDMA_TPD_PPPOE_EN;
1303
1304 if (flags_transmit & EDMA_VLAN_TX_TAG_INSERT_FLAG) {
1305 switch (skb->vlan_proto) {
1306 case htons(ETH_P_8021Q):
1307 word3 |= (1 << EDMA_TX_INS_CVLAN);
1308#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
1309 word3 |= vlan_tx_tag_get(skb) << EDMA_TX_CVLAN_TAG_SHIFT;
1310#else
1311 word3 |= skb_vlan_tag_get(skb) << EDMA_TX_CVLAN_TAG_SHIFT;
1312#endif
1313 break;
1314 case htons(ETH_P_8021AD):
1315 word1 |= (1 << EDMA_TX_INS_SVLAN);
1316#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
1317 svlan_tag = vlan_tx_tag_get(skb) << EDMA_TX_SVLAN_TAG_SHIFT;
1318#else
1319 svlan_tag = skb_vlan_tag_get(skb) << EDMA_TX_SVLAN_TAG_SHIFT;
1320#endif
1321 break;
1322 default:
1323 dev_err(&pdev->dev, "no ctag or stag present\n");
1324 goto vlan_tag_error;
1325 }
1326 } else if (flags_transmit & EDMA_VLAN_TX_TAG_INSERT_DEFAULT_FLAG) {
1327 word3 |= (1 << EDMA_TX_INS_CVLAN);
1328 word3 |= (adapter->default_vlan_tag) << EDMA_TX_CVLAN_TAG_SHIFT;
1329 }
1330
1331 if (packet_is_rstp) {
1332 word3 |= dp_bitmap << EDMA_TPD_PORT_BITMAP_SHIFT;
1333 word3 |= from_cpu << EDMA_TPD_FROM_CPU_SHIFT;
1334 } else {
1335 word3 |= adapter->dp_bitmap << EDMA_TPD_PORT_BITMAP_SHIFT;
1336 }
1337
1338 buf_len = skb_headlen(skb);
1339
1340 if (lso_word1) {
1341 if (lso_word1 & EDMA_TPD_LSO_V2_EN) {
1342
1343 /* IPv6 LSOv2 descriptor */
1344 start_tpd = tpd = edma_get_next_tpd(edma_cinfo, queue_id);
1345 sw_desc = edma_get_tx_buffer(edma_cinfo, tpd, queue_id);
1346 sw_desc->flags |= EDMA_SW_DESC_FLAG_SKB_NONE;
1347
1348 /* LSOv2 descriptor overrides addr field to pass length */
1349 tpd->addr = cpu_to_le16(skb->len);
1350 tpd->svlan_tag = svlan_tag;
1351 tpd->word1 = word1 | lso_word1;
1352 tpd->word3 = word3;
1353 }
1354
1355 tpd = edma_get_next_tpd(edma_cinfo, queue_id);
1356 if (!start_tpd)
1357 start_tpd = tpd;
1358 sw_desc = edma_get_tx_buffer(edma_cinfo, tpd, queue_id);
1359
1360 /* The last buffer info contain the skb address,
1361 * so skb will be freed after unmap
1362 */
1363 sw_desc->length = lso_desc_len;
1364 sw_desc->flags |= EDMA_SW_DESC_FLAG_SKB_HEAD;
1365
1366 sw_desc->dma = dma_map_single(&adapter->pdev->dev,
1367 skb->data, buf_len, DMA_TO_DEVICE);
1368 if (dma_mapping_error(&pdev->dev, sw_desc->dma))
1369 goto dma_error;
1370
1371 tpd->addr = cpu_to_le32(sw_desc->dma);
1372 tpd->len = cpu_to_le16(buf_len);
1373
1374 tpd->svlan_tag = svlan_tag;
1375 tpd->word1 = word1 | lso_word1;
1376 tpd->word3 = word3;
1377
1378 /* The last buffer info contain the skb address,
1379 * so it will be freed after unmap
1380 */
1381 sw_desc->length = lso_desc_len;
1382 sw_desc->flags |= EDMA_SW_DESC_FLAG_SKB_HEAD;
1383
1384 buf_len = 0;
1385 }
1386
1387 if (likely(buf_len)) {
1388
1389 /* TODO Do not dequeue descriptor if there is a potential error */
1390 tpd = edma_get_next_tpd(edma_cinfo, queue_id);
1391
1392 if (!start_tpd)
1393 start_tpd = tpd;
1394
1395 sw_desc = edma_get_tx_buffer(edma_cinfo, tpd, queue_id);
1396
1397 /* The last buffer info contain the skb address,
1398 * so it will be free after unmap
1399 */
1400 sw_desc->length = buf_len;
1401 sw_desc->flags |= EDMA_SW_DESC_FLAG_SKB_HEAD;
1402 sw_desc->dma = dma_map_single(&adapter->pdev->dev,
1403 skb->data, buf_len, DMA_TO_DEVICE);
1404 if (dma_mapping_error(&pdev->dev, sw_desc->dma))
1405 goto dma_error;
1406
1407 tpd->addr = cpu_to_le32(sw_desc->dma);
1408 tpd->len = cpu_to_le16(buf_len);
1409
1410 tpd->svlan_tag = svlan_tag;
1411 tpd->word1 = word1 | lso_word1;
1412 tpd->word3 = word3;
1413 }
1414
1415 i = 0;
1416
1417 /* Walk through paged frags for head skb */
1418 while (nr_frags--) {
1419 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1420 buf_len = skb_frag_size(frag);
1421 tpd = edma_get_next_tpd(edma_cinfo, queue_id);
1422 sw_desc = edma_get_tx_buffer(edma_cinfo, tpd, queue_id);
1423 sw_desc->length = buf_len;
1424 sw_desc->flags |= EDMA_SW_DESC_FLAG_SKB_FRAG;
1425
1426 sw_desc->dma = skb_frag_dma_map(&pdev->dev, frag, 0, buf_len, DMA_TO_DEVICE);
1427
1428 if (dma_mapping_error(NULL, sw_desc->dma))
1429 goto dma_error;
1430
1431 tpd->addr = cpu_to_le32(sw_desc->dma);
1432 tpd->len = cpu_to_le16(buf_len);
1433
1434 tpd->svlan_tag = svlan_tag;
1435 tpd->word1 = word1 | lso_word1;
1436 tpd->word3 = word3;
1437 i++;
1438 }
1439
1440 /* Walk through all fraglist skbs */
1441 skb_walk_frags(skb, iter_skb) {
1442 buf_len = iter_skb->len;
1443 tpd = edma_get_next_tpd(edma_cinfo, queue_id);
1444 sw_desc = edma_get_tx_buffer(edma_cinfo, tpd, queue_id);
1445 sw_desc->length = buf_len;
1446 sw_desc->dma = dma_map_single(&adapter->pdev->dev,
1447 iter_skb->data, buf_len, DMA_TO_DEVICE);
1448
1449 if (dma_mapping_error(NULL, sw_desc->dma))
1450 goto dma_error;
1451
1452 tpd->addr = cpu_to_le32(sw_desc->dma);
1453 tpd->len = cpu_to_le16(buf_len);
1454 tpd->svlan_tag = svlan_tag;
1455 tpd->word1 = word1 | lso_word1;
1456 tpd->word3 = word3;
1457 sw_desc->flags |= EDMA_SW_DESC_FLAG_SKB_FRAGLIST;
1458
1459 i = 0;
1460
1461 nr_frags = skb_shinfo(iter_skb)->nr_frags;
1462
1463 /* Walk through paged frags for this fraglist skb */
1464 while (nr_frags--) {
1465 skb_frag_t *frag = &skb_shinfo(iter_skb)->frags[i];
1466 buf_len = skb_frag_size(frag);
1467 tpd = edma_get_next_tpd(edma_cinfo, queue_id);
1468 sw_desc = edma_get_tx_buffer(edma_cinfo, tpd, queue_id);
1469 sw_desc->length = buf_len;
1470 sw_desc->flags |= EDMA_SW_DESC_FLAG_SKB_FRAG;
1471
1472 sw_desc->dma = skb_frag_dma_map(&pdev->dev, frag,
1473 0, buf_len, DMA_TO_DEVICE);
1474 if (dma_mapping_error(NULL, sw_desc->dma))
1475 goto dma_error;
1476
1477 tpd->addr = cpu_to_le32(sw_desc->dma);
1478 tpd->len = cpu_to_le16(buf_len);
1479 tpd->svlan_tag = svlan_tag;
1480 tpd->word1 = word1 | lso_word1;
1481 tpd->word3 = word3;
1482 i++;
1483 }
1484 }
1485
Rakesh Nair888af952017-06-30 18:41:58 +05301486 /* If sysctl support for per-precedence stats are enabled */
1487 if (edma_per_prec_stats_enable) {
Rakesh Nair7e053532017-08-18 17:53:25 +05301488 uint8_t precedence = 0;
Rakesh Nair888af952017-06-30 18:41:58 +05301489
Rakesh Nair7e053532017-08-18 17:53:25 +05301490 if(!edma_get_skb_precedence(skb, &precedence)) {
Rakesh Nair888af952017-06-30 18:41:58 +05301491 /* Increment per-precedence counters for tx packets
1492 * and set the precedence in the TPD.
1493 */
Rakesh Nairc402d752018-01-20 16:21:54 +05301494 atomic_inc(&edma_cinfo->edma_ethstats.tx_prec[precedence]);
Rakesh Nair888af952017-06-30 18:41:58 +05301495 edma_cinfo->edma_ethstats.tx_ac[edma_dscp2ac_tbl[precedence]]++;
Rakesh Nairdadf1fb2017-09-07 11:58:28 +05301496 if (tpd)
1497 tpd->word3 |= precedence << EDMA_TPD_PRIO_SHIFT;
Rakesh Nair888af952017-06-30 18:41:58 +05301498 }
Rakesh Nair1c6a18c2017-08-02 21:27:06 +05301499
1500 /* If sysctl support for IAD stats are enabled */
1501 if (edma_iad_stats_enable) {
1502 if (edma_dscp2ac_tbl[precedence] == EDMA_AC_VI)
1503 edma_iad_process_flow(edma_cinfo, skb, EDMA_EGRESS_DIR, precedence);
1504 }
Rakesh Nair888af952017-06-30 18:41:58 +05301505 }
1506
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301507 /* If tpd or sw_desc is still unitiialized then we need to return */
1508 if ((!tpd) || (!sw_desc))
1509 return -EINVAL;
1510
1511 tpd->word1 |= 1 << EDMA_TPD_EOP_SHIFT;
1512
1513 sw_desc->skb = skb;
1514 sw_desc->flags |= EDMA_SW_DESC_FLAG_LAST;
1515
1516 return 0;
1517
1518dma_error:
1519 edma_rollback_tx(adapter, start_tpd, queue_id);
1520 dev_err(&pdev->dev, "TX DMA map failed\n");
1521vlan_tag_error:
1522 return -ENOMEM;
1523}
1524
1525/* edma_check_link()
1526 * check Link status
1527 */
1528static int edma_check_link(struct edma_adapter *adapter)
1529{
1530 struct phy_device *phydev = adapter->phydev;
1531
1532 if (!(adapter->poll_required))
1533 return __EDMA_LINKUP;
1534
1535 if (phydev->link)
1536 return __EDMA_LINKUP;
1537
1538 return __EDMA_LINKDOWN;
1539}
1540
1541/* edma_adjust_link()
1542 * check for edma link status
1543 */
1544void edma_adjust_link(struct net_device *netdev)
1545{
1546 int status;
1547 struct edma_adapter *adapter = netdev_priv(netdev);
1548 struct phy_device *phydev = adapter->phydev;
1549
1550 if (!test_bit(__EDMA_UP, &adapter->state_flags))
1551 return;
1552
1553 status = edma_check_link(adapter);
1554
1555 if (status == __EDMA_LINKUP && adapter->link_state == __EDMA_LINKDOWN) {
1556 dev_info(&adapter->pdev->dev, "%s: GMAC Link is up with phy_speed=%d\n", netdev->name, phydev->speed);
1557 adapter->link_state = __EDMA_LINKUP;
1558 netif_carrier_on(netdev);
1559 if (netif_running(netdev))
1560 netif_tx_wake_all_queues(netdev);
1561 } else if (status == __EDMA_LINKDOWN && adapter->link_state == __EDMA_LINKUP) {
1562 dev_info(&adapter->pdev->dev, "%s: GMAC Link is down\n", netdev->name);
1563 adapter->link_state = __EDMA_LINKDOWN;
1564 netif_carrier_off(netdev);
1565 netif_tx_stop_all_queues(netdev);
1566 }
1567}
1568
Bhaskar Valabojue429bab2017-03-15 09:01:23 +05301569/* edma_get_stats64()
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301570 * Statistics api used to retreive the tx/rx statistics
1571 */
Bhaskar Valabojue429bab2017-03-15 09:01:23 +05301572struct rtnl_link_stats64 *edma_get_stats64(struct net_device *netdev,
1573 struct rtnl_link_stats64 *stats)
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301574{
1575 struct edma_adapter *adapter = netdev_priv(netdev);
1576
Bhaskar Valabojue429bab2017-03-15 09:01:23 +05301577 memcpy(stats, &adapter->stats, sizeof(*stats));
1578
1579 return stats;
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301580}
1581
1582/* edma_xmit()
1583 * Main api to be called by the core for packet transmission
1584 */
1585netdev_tx_t edma_xmit(struct sk_buff *skb,
1586 struct net_device *net_dev)
1587{
1588 struct edma_adapter *adapter = netdev_priv(net_dev);
1589 struct edma_common_info *edma_cinfo = adapter->edma_cinfo;
1590 struct edma_tx_desc_ring *etdr;
1591 u16 from_cpu = 0, dp_bitmap = 0, txq_id;
1592 int ret, nr_frags_first = 0, num_tpds_needed = 1, queue_id = 0;
1593 unsigned int flags_transmit = 0;
1594 bool packet_is_rstp = false;
1595 struct netdev_queue *nq = NULL;
1596
1597 if (skb_shinfo(skb)->nr_frags) {
1598 nr_frags_first = skb_shinfo(skb)->nr_frags;
1599
1600 /* It is unlikely below check hits, BUG_ON */
1601 BUG_ON(nr_frags_first > MAX_SKB_FRAGS);
1602
1603 num_tpds_needed += nr_frags_first;
1604 }
1605
1606 if (skb_has_frag_list(skb)) {
1607 struct sk_buff *iter_skb;
1608
1609 /* Walk through fraglist skbs making a note of nr_frags */
1610 skb_walk_frags(skb, iter_skb) {
1611 unsigned char nr_frags = skb_shinfo(iter_skb)->nr_frags;
1612
1613 /* It is unlikely below check hits, BUG_ON */
1614 BUG_ON(nr_frags > MAX_SKB_FRAGS);
1615
1616 /* One TPD for skb->data and more for nr_frags */
1617 num_tpds_needed += (1 + nr_frags);
1618 }
1619 }
1620
1621 if (edma_stp_rstp) {
1622 u16 ath_hdr, ath_eth_type;
1623 u8 mac_addr[EDMA_ETH_HDR_LEN];
1624 ath_eth_type = ntohs(*(uint16_t *)&skb->data[12]);
1625 if (ath_eth_type == edma_ath_eth_type) {
1626 packet_is_rstp = true;
1627 ath_hdr = htons(*(uint16_t *)&skb->data[14]);
1628 dp_bitmap = ath_hdr & EDMA_TX_ATH_HDR_PORT_BITMAP_MASK;
1629 from_cpu = (ath_hdr & EDMA_TX_ATH_HDR_FROM_CPU_MASK) >> EDMA_TX_ATH_HDR_FROM_CPU_SHIFT;
1630 memcpy(mac_addr, skb->data, EDMA_ETH_HDR_LEN);
1631
1632 skb_pull(skb, 4);
1633
1634 memcpy(skb->data, mac_addr, EDMA_ETH_HDR_LEN);
1635 }
1636 }
1637
1638 /* this will be one of the 4 TX queues exposed to linux kernel */
1639 txq_id = skb_get_queue_mapping(skb);
1640 queue_id = edma_tx_queue_get(adapter, skb, txq_id);
1641 etdr = edma_cinfo->tpd_ring[queue_id];
1642 nq = netdev_get_tx_queue(net_dev, txq_id);
1643
1644 local_bh_disable();
1645 /* Tx is not handled in bottom half context. Hence, we need to protect
1646 * Tx from tasks and bottom half
1647 */
1648
1649 if (num_tpds_needed > edma_tpd_available(edma_cinfo, queue_id)) {
Rakesh Naird4a11502017-11-07 17:02:11 +05301650 if (edma_disable_queue_stop) {
1651 local_bh_enable();
1652 dev_dbg(&net_dev->dev, "Packet dropped as queue is full");
1653 dev_kfree_skb_any(skb);
1654 adapter->stats.tx_errors++;
1655 return NETDEV_TX_OK;
1656 }
1657
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301658 /* not enough descriptor, just stop queue */
1659 netif_tx_stop_queue(nq);
1660 local_bh_enable();
1661 dev_dbg(&net_dev->dev, "Not enough descriptors available");
1662 edma_cinfo->edma_ethstats.tx_desc_error++;
1663 return NETDEV_TX_BUSY;
1664 }
1665
1666 /* Check and mark VLAN tag offload */
1667#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
1668 if (vlan_tx_tag_present(skb))
1669#else
1670 if (skb_vlan_tag_present(skb))
1671#endif
1672 flags_transmit |= EDMA_VLAN_TX_TAG_INSERT_FLAG;
1673 else if (adapter->default_vlan_tag)
1674 flags_transmit |= EDMA_VLAN_TX_TAG_INSERT_DEFAULT_FLAG;
1675
1676 /* Check and mark checksum offload */
1677 if (likely(skb->ip_summed == CHECKSUM_PARTIAL))
1678 flags_transmit |= EDMA_HW_CHECKSUM;
1679
1680 /* Map and fill descriptor for Tx */
1681 ret = edma_tx_map_and_fill(edma_cinfo, adapter, skb, queue_id,
1682 flags_transmit, from_cpu, dp_bitmap,
1683 packet_is_rstp, nr_frags_first);
1684 if (ret) {
1685 dev_kfree_skb_any(skb);
1686 adapter->stats.tx_errors++;
1687 goto netdev_okay;
1688 }
1689
1690 /* Update SW producer index */
1691 edma_tx_update_hw_idx(edma_cinfo, skb, queue_id);
1692
1693 /* update tx statistics */
1694 adapter->stats.tx_packets++;
1695 adapter->stats.tx_bytes += skb->len;
1696
1697netdev_okay:
1698 local_bh_enable();
1699 return NETDEV_TX_OK;
1700}
1701
1702/*
1703 * edma_flow_may_expire()
1704 * Timer function called periodically to delete the node
1705 */
1706void edma_flow_may_expire(unsigned long data)
1707{
1708 struct edma_adapter *adapter = (struct edma_adapter *)data;
1709 int j;
1710
1711 spin_lock_bh(&adapter->rfs.rfs_ftab_lock);
1712 for (j = 0; j < EDMA_RFS_EXPIRE_COUNT_PER_CALL; j++) {
1713 struct hlist_head *hhead;
1714 struct hlist_node *tmp;
1715 struct edma_rfs_filter_node *n;
1716 bool res;
1717
1718 hhead = &adapter->rfs.hlist_head[adapter->rfs.hashtoclean++];
1719 hlist_for_each_entry_safe(n, tmp, hhead, node) {
1720 res = rps_may_expire_flow(adapter->netdev, n->rq_id,
1721 n->flow_id, n->filter_id);
1722 if (res) {
1723 res = edma_delete_rfs_filter(adapter, n);
1724 if (res < 0)
1725 dev_dbg(&adapter->netdev->dev,
1726 "RFS entry %d not allowed to be flushed by Switch",
1727 n->flow_id);
1728 else {
1729 hlist_del(&n->node);
1730 kfree(n);
1731 adapter->rfs.filter_available++;
1732 }
1733 }
1734 }
1735 }
1736
1737 adapter->rfs.hashtoclean = adapter->rfs.hashtoclean & (EDMA_RFS_FLOW_ENTRIES - 1);
1738 spin_unlock_bh(&adapter->rfs.rfs_ftab_lock);
1739 mod_timer(&adapter->rfs.expire_rfs, jiffies + HZ/4);
1740}
1741
1742/* edma_rx_flow_steer()
1743 * Called by core to to steer the flow to CPU
1744 */
1745int edma_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
1746 u16 rxq, u32 flow_id)
1747{
1748 struct flow_keys keys;
1749 struct edma_rfs_filter_node *filter_node;
1750 struct edma_adapter *adapter = netdev_priv(dev);
1751 u16 hash_tblid;
1752 int res;
1753
1754 if (skb->protocol == htons(ETH_P_IPV6)) {
1755 res = -EPROTONOSUPPORT;
1756 goto no_protocol_err;
1757 }
1758
1759 /* Dissect flow parameters
1760 * We only support IPv4 + TCP/UDP
1761 */
1762#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
1763 res = skb_flow_dissect(skb, &keys);
1764 if (!((keys.ip_proto == IPPROTO_TCP) || (keys.ip_proto == IPPROTO_UDP))) {
1765#else
1766 res = skb_flow_dissect_flow_keys(skb, &keys, 0);
1767 if (!((keys.basic.ip_proto == IPPROTO_TCP) || (keys.basic.ip_proto == IPPROTO_UDP))) {
1768#endif
1769 res = -EPROTONOSUPPORT;
1770 goto no_protocol_err;
1771 }
1772
1773 /* Check if table entry exists */
1774 hash_tblid = skb_get_hash_raw(skb) & EDMA_RFS_FLOW_ENTRIES_MASK;
1775
1776 spin_lock_bh(&adapter->rfs.rfs_ftab_lock);
1777 filter_node = edma_rfs_key_search(&adapter->rfs.hlist_head[hash_tblid], &keys);
1778
1779 if (filter_node) {
1780 if (rxq == filter_node->rq_id) {
1781 res = -EEXIST;
1782 goto out;
1783 } else {
1784 res = edma_delete_rfs_filter(adapter, filter_node);
1785 if (res < 0)
1786 dev_warn(&adapter->netdev->dev,
1787 "Cannot steer flow %d to different queue",
1788 filter_node->flow_id);
1789 else {
1790 adapter->rfs.filter_available++;
1791 res = edma_add_rfs_filter(adapter, &keys, rxq, filter_node);
1792 if (res < 0) {
1793 dev_warn(&adapter->netdev->dev,
1794 "Cannot steer flow %d to different queue",
1795 filter_node->flow_id);
1796 } else {
1797 adapter->rfs.filter_available--;
1798 filter_node->rq_id = rxq;
1799 filter_node->filter_id = res;
1800 }
1801 }
1802 }
1803 } else {
1804 if (adapter->rfs.filter_available == 0) {
1805 res = -EBUSY;
1806 goto out;
1807 }
1808
1809 filter_node = kmalloc(sizeof(*filter_node), GFP_ATOMIC);
1810 if (!filter_node) {
1811 res = -ENOMEM;
1812 goto out;
1813 }
1814
1815 res = edma_add_rfs_filter(adapter, &keys, rxq, filter_node);
1816 if (res < 0) {
1817 kfree(filter_node);
1818 goto out;
1819 }
1820
1821 adapter->rfs.filter_available--;
1822 filter_node->rq_id = rxq;
1823 filter_node->filter_id = res;
1824 filter_node->flow_id = flow_id;
1825 filter_node->keys = keys;
1826 INIT_HLIST_NODE(&filter_node->node);
1827 hlist_add_head(&filter_node->node, &adapter->rfs.hlist_head[hash_tblid]);
1828 }
1829
1830out:
1831 spin_unlock_bh(&adapter->rfs.rfs_ftab_lock);
1832no_protocol_err:
1833 return res;
1834}
1835
1836#ifdef CONFIG_RFS_ACCEL
1837/* edma_register_rfs_filter()
1838 * Add RFS filter callback
1839 */
1840int edma_register_rfs_filter(struct net_device *netdev,
1841 set_rfs_filter_callback_t set_filter)
1842{
1843 struct edma_adapter *adapter = netdev_priv(netdev);
1844
1845 spin_lock_bh(&adapter->rfs.rfs_ftab_lock);
1846
1847 if (adapter->set_rfs_rule) {
1848 spin_unlock_bh(&adapter->rfs.rfs_ftab_lock);
1849 return -1;
1850 }
1851
1852 adapter->set_rfs_rule = set_filter;
1853 spin_unlock_bh(&adapter->rfs.rfs_ftab_lock);
1854
1855 return 0;
1856}
1857#endif
1858
1859/* edma_select_xps_queue()
1860 * Called by Linux TX stack to populate Linux TX queue
1861 */
1862u16 edma_select_xps_queue(struct net_device *dev, struct sk_buff *skb,
1863 void *accel_priv, select_queue_fallback_t fallback)
1864{
1865#if (LINUX_VERSION_CODE <= KERNEL_VERSION(3, 18, 21))
1866 return smp_processor_id();
1867#else
1868 int cpu = get_cpu();
1869 put_cpu();
1870
1871 return cpu;
1872#endif
1873}
1874
1875/* edma_alloc_tx_rings()
1876 * Allocate rx rings
1877 */
1878int edma_alloc_tx_rings(struct edma_common_info *edma_cinfo)
1879{
1880 struct platform_device *pdev = edma_cinfo->pdev;
1881 int i, err = 0;
1882
1883 for (i = 0; i < edma_cinfo->num_tx_queues; i++) {
1884 err = edma_alloc_tx_ring(edma_cinfo, edma_cinfo->tpd_ring[i]);
1885 if (err) {
1886 dev_err(&pdev->dev, "Tx Queue alloc %u failed\n", i);
1887 return err;
1888 }
1889 }
1890
1891 return 0;
1892}
1893
1894/* edma_free_tx_rings()
1895 * Free tx rings
1896 */
1897void edma_free_tx_rings(struct edma_common_info *edma_cinfo)
1898{
1899 int i;
1900
1901 for (i = 0; i < edma_cinfo->num_tx_queues; i++)
1902 edma_free_tx_ring(edma_cinfo, edma_cinfo->tpd_ring[i]);
1903}
1904
1905/* edma_free_tx_resources()
1906 * Free buffers associated with tx rings
1907 */
1908void edma_free_tx_resources(struct edma_common_info *edma_cinfo)
1909{
1910 struct edma_tx_desc_ring *etdr;
1911 struct edma_sw_desc *sw_desc;
1912 struct platform_device *pdev = edma_cinfo->pdev;
1913 int i, j;
1914
1915 for (i = 0; i < edma_cinfo->num_tx_queues; i++) {
1916 etdr = edma_cinfo->tpd_ring[i];
Rakesh Nair3a756882017-11-15 12:18:21 +05301917 for (j = 0; j < edma_cinfo->tx_ring_count; j++) {
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301918 sw_desc = &etdr->sw_desc[j];
1919 if (sw_desc->flags & (EDMA_SW_DESC_FLAG_SKB_HEAD |
1920 EDMA_SW_DESC_FLAG_SKB_FRAG | EDMA_SW_DESC_FLAG_SKB_FRAGLIST))
1921 edma_tx_unmap_and_free(pdev, sw_desc);
1922 }
1923 }
1924}
1925
1926/* edma_alloc_rx_rings()
1927 * Allocate rx rings
1928 */
1929int edma_alloc_rx_rings(struct edma_common_info *edma_cinfo)
1930{
1931 struct platform_device *pdev = edma_cinfo->pdev;
1932 int i, j, err = 0;
1933
1934 for (i = 0, j = 0; i < edma_cinfo->num_rx_queues; i++) {
1935 err = edma_alloc_rx_ring(edma_cinfo, edma_cinfo->rfd_ring[j]);
1936 if (err) {
1937 dev_err(&pdev->dev, "Rx Queue alloc%u failed\n", i);
1938 return err;
1939 }
1940 j += ((edma_cinfo->num_rx_queues == 4) ? 2 : 1);
1941 }
1942
1943 return 0;
1944}
1945
1946/* edma_free_rx_rings()
1947 * free rx rings
1948 */
1949void edma_free_rx_rings(struct edma_common_info *edma_cinfo)
1950{
1951 int i, j;
1952
1953 for (i = 0, j = 0; i < edma_cinfo->num_rx_queues; i++) {
1954 edma_free_rx_ring(edma_cinfo, edma_cinfo->rfd_ring[j]);
1955 j += ((edma_cinfo->num_rx_queues == 4) ? 2 : 1);
1956 }
1957}
1958
1959/* edma_free_queues()
1960 * Free the queues allocaated
1961 */
1962void edma_free_queues(struct edma_common_info *edma_cinfo)
1963{
1964 int i , j;
1965
1966 for (i = 0; i < edma_cinfo->num_tx_queues; i++) {
1967 if (edma_cinfo->tpd_ring[i])
1968 kfree(edma_cinfo->tpd_ring[i]);
1969 edma_cinfo->tpd_ring[i] = NULL;
1970 }
1971
1972 for (i = 0, j = 0; i < edma_cinfo->num_rx_queues; i++) {
1973 if (edma_cinfo->rfd_ring[j])
1974 kfree(edma_cinfo->rfd_ring[j]);
1975 edma_cinfo->rfd_ring[j] = NULL;
1976 j += ((edma_cinfo->num_rx_queues == 4) ? 2 : 1);
1977 }
1978
1979 edma_cinfo->num_rx_queues = 0;
1980 edma_cinfo->num_tx_queues = 0;
1981
1982 return;
1983}
1984
1985/* edma_free_rx_resources()
1986 * Free buffers associated with tx rings
1987 */
1988void edma_free_rx_resources(struct edma_common_info *edma_cinfo)
1989{
1990 struct edma_rfd_desc_ring *erdr;
1991 struct platform_device *pdev = edma_cinfo->pdev;
1992 int i, j, k;
1993
1994 for (i = 0, k = 0; i < edma_cinfo->num_rx_queues; i++) {
1995 erdr = edma_cinfo->rfd_ring[k];
Rakesh Nair3a756882017-11-15 12:18:21 +05301996 for (j = 0; j < edma_cinfo->rx_ring_count; j++) {
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301997 /* unmap all descriptors while cleaning */
1998 edma_clean_rfd(pdev, erdr, j, 1);
1999 }
2000 k += ((edma_cinfo->num_rx_queues == 4) ? 2 : 1);
2001
2002 }
2003}
2004
2005/* edma_alloc_queues_tx()
2006 * Allocate memory for all rings
2007 */
2008int edma_alloc_queues_tx(struct edma_common_info *edma_cinfo)
2009{
2010 int i;
2011
2012 for (i = 0; i < edma_cinfo->num_tx_queues; i++) {
2013 struct edma_tx_desc_ring *etdr;
2014 etdr = kzalloc(sizeof(struct edma_tx_desc_ring), GFP_KERNEL);
2015 if (!etdr)
2016 goto err;
2017 etdr->count = edma_cinfo->tx_ring_count;
2018 edma_cinfo->tpd_ring[i] = etdr;
2019 }
2020
2021 return 0;
2022err:
2023 edma_free_queues(edma_cinfo);
2024 return -1;
2025}
2026
2027/* edma_alloc_queues_rx()
2028 * Allocate memory for all rings
2029 */
2030int edma_alloc_queues_rx(struct edma_common_info *edma_cinfo)
2031{
2032 int i, j;
2033
2034 for (i = 0, j = 0; i < edma_cinfo->num_rx_queues; i++) {
2035 struct edma_rfd_desc_ring *rfd_ring;
2036 rfd_ring = kzalloc(sizeof(struct edma_rfd_desc_ring),
2037 GFP_KERNEL);
2038 if (!rfd_ring)
2039 goto err;
2040 rfd_ring->count = edma_cinfo->rx_ring_count;
2041 edma_cinfo->rfd_ring[j] = rfd_ring;
2042 j += ((edma_cinfo->num_rx_queues == 4) ? 2 : 1);
2043 }
2044 return 0;
2045err:
2046 edma_free_queues(edma_cinfo);
2047 return -1;
2048}
2049
2050/* edma_clear_irq_status()
2051 * Clear interrupt status
2052 */
2053void edma_clear_irq_status(void)
2054{
2055 edma_write_reg(EDMA_REG_RX_ISR, 0xff);
2056 edma_write_reg(EDMA_REG_TX_ISR, 0xffff);
2057 edma_write_reg(EDMA_REG_MISC_ISR, 0x1fff);
2058 edma_write_reg(EDMA_REG_WOL_ISR, 0x1);
2059};
2060
2061/* edma_configure()
2062 * Configure skb, edma interrupts and control register.
2063 */
2064int edma_configure(struct edma_common_info *edma_cinfo)
2065{
2066 struct edma_hw *hw = &edma_cinfo->hw;
2067 u32 intr_modrt_data;
2068 u32 intr_ctrl_data = 0;
2069 int i, j, ret_count;
2070
2071 edma_read_reg(EDMA_REG_INTR_CTRL, &intr_ctrl_data);
2072 intr_ctrl_data &= ~(1 << EDMA_INTR_SW_IDX_W_TYP_SHIFT);
2073 intr_ctrl_data |= hw->intr_sw_idx_w << EDMA_INTR_SW_IDX_W_TYP_SHIFT;
2074 edma_write_reg(EDMA_REG_INTR_CTRL, intr_ctrl_data);
2075
2076 edma_clear_irq_status();
2077
2078 /* Clear any WOL status */
2079 edma_write_reg(EDMA_REG_WOL_CTRL, 0);
2080 intr_modrt_data = (EDMA_TX_IMT << EDMA_IRQ_MODRT_TX_TIMER_SHIFT);
2081 intr_modrt_data |= (EDMA_RX_IMT << EDMA_IRQ_MODRT_RX_TIMER_SHIFT);
2082 edma_write_reg(EDMA_REG_IRQ_MODRT_TIMER_INIT, intr_modrt_data);
2083 edma_configure_tx(edma_cinfo);
2084 edma_configure_rx(edma_cinfo);
2085
2086 /* Allocate the RX buffer */
2087 for (i = 0, j = 0; i < edma_cinfo->num_rx_queues; i++) {
2088 struct edma_rfd_desc_ring *ring = edma_cinfo->rfd_ring[j];
2089 ret_count = edma_alloc_rx_buf(edma_cinfo, ring, ring->count, j);
2090 if (ret_count)
2091 dev_dbg(&edma_cinfo->pdev->dev, "not all rx buffers allocated\n");
Rakesh Nair03b586c2017-04-03 18:28:58 +05302092 ring->pending_fill = ret_count;
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302093 j += ((edma_cinfo->num_rx_queues == 4) ? 2 : 1);
2094 }
2095
2096 /* Configure descriptor Ring */
2097 edma_init_desc(edma_cinfo);
2098 return 0;
2099}
2100
2101/* edma_irq_enable()
2102 * Enable default interrupt generation settings
2103 */
2104void edma_irq_enable(struct edma_common_info *edma_cinfo)
2105{
2106 struct edma_hw *hw = &edma_cinfo->hw;
2107 int i, j;
2108
2109 edma_write_reg(EDMA_REG_RX_ISR, 0xff);
2110 for (i = 0, j = 0; i < edma_cinfo->num_rx_queues; i++) {
2111 edma_write_reg(EDMA_REG_RX_INT_MASK_Q(j), hw->rx_intr_mask);
2112 j += ((edma_cinfo->num_rx_queues == 4) ? 2 : 1);
2113 }
2114 edma_write_reg(EDMA_REG_TX_ISR, 0xffff);
2115 for (i = 0; i < edma_cinfo->num_tx_queues; i++)
2116 edma_write_reg(EDMA_REG_TX_INT_MASK_Q(i), hw->tx_intr_mask);
2117}
2118
2119/* edma_irq_disable()
2120 * Disable Interrupt
2121 */
2122void edma_irq_disable(struct edma_common_info *edma_cinfo)
2123{
2124 int i;
2125
2126 for (i = 0; i < EDMA_MAX_RECEIVE_QUEUE; i++)
2127 edma_write_reg(EDMA_REG_RX_INT_MASK_Q(i), 0x0);
2128
2129 for (i = 0; i < EDMA_MAX_TRANSMIT_QUEUE; i++)
2130 edma_write_reg(EDMA_REG_TX_INT_MASK_Q(i), 0x0);
2131 edma_write_reg(EDMA_REG_MISC_IMR, 0);
2132 edma_write_reg(EDMA_REG_WOL_IMR, 0);
2133}
2134
2135/* edma_free_irqs()
2136 * Free All IRQs
2137 */
2138void edma_free_irqs(struct edma_adapter *adapter)
2139{
2140 struct edma_common_info *edma_cinfo = adapter->edma_cinfo;
2141 int i, j;
2142 int k = ((edma_cinfo->num_rx_queues == 4) ? 1 : 2);
2143
2144 for (i = 0; i < CONFIG_NR_CPUS; i++) {
Rakesh Nair8016fbd2018-01-03 15:46:06 +05302145 for (j = edma_cinfo->edma_percpu_info[i].tx_comp_start; j < (edma_cinfo->edma_percpu_info[i].tx_comp_start + 4); j++)
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302146 free_irq(edma_cinfo->tx_irq[j], &edma_cinfo->edma_percpu_info[i]);
2147
2148 for (j = edma_cinfo->edma_percpu_info[i].rx_start; j < (edma_cinfo->edma_percpu_info[i].rx_start + k); j++)
2149 free_irq(edma_cinfo->rx_irq[j], &edma_cinfo->edma_percpu_info[i]);
2150 }
2151}
2152
2153/* edma_enable_rx_ctrl()
2154 * Enable RX queue control
2155 */
2156void edma_enable_rx_ctrl(struct edma_hw *hw)
2157{
2158 u32 data;
2159
2160 edma_read_reg(EDMA_REG_RXQ_CTRL, &data);
2161 data |= EDMA_RXQ_CTRL_EN;
2162 edma_write_reg(EDMA_REG_RXQ_CTRL, data);
2163}
2164
2165
2166/* edma_enable_tx_ctrl()
2167 * Enable TX queue control
2168 */
2169void edma_enable_tx_ctrl(struct edma_hw *hw)
2170{
2171 u32 data;
2172
2173 edma_read_reg(EDMA_REG_TXQ_CTRL, &data);
2174 data |= EDMA_TXQ_CTRL_TXQ_EN;
2175 edma_write_reg(EDMA_REG_TXQ_CTRL, data);
2176}
2177
2178/* edma_stop_rx_tx()
2179 * Disable RX/TQ Queue control
2180 */
2181void edma_stop_rx_tx(struct edma_hw *hw)
2182{
2183 u32 data;
2184
2185 edma_read_reg(EDMA_REG_RXQ_CTRL, &data);
2186 data &= ~EDMA_RXQ_CTRL_EN;
2187 edma_write_reg(EDMA_REG_RXQ_CTRL, data);
2188 edma_read_reg(EDMA_REG_TXQ_CTRL, &data);
2189 data &= ~EDMA_TXQ_CTRL_TXQ_EN;
2190 edma_write_reg(EDMA_REG_TXQ_CTRL, data);
2191}
2192
2193/* edma_reset()
2194 * Reset the EDMA
2195 */
2196int edma_reset(struct edma_common_info *edma_cinfo)
2197{
2198 struct edma_hw *hw = &edma_cinfo->hw;
2199
2200 edma_irq_disable(edma_cinfo);
2201
2202 edma_clear_irq_status();
2203
2204 edma_stop_rx_tx(hw);
2205
2206 return 0;
2207}
2208
2209/* edma_fill_netdev()
2210 * Fill netdev for each etdr
2211 */
2212int edma_fill_netdev(struct edma_common_info *edma_cinfo, int queue_id,
2213 int dev, int txq_id)
2214{
2215 struct edma_tx_desc_ring *etdr;
2216 int i = 0;
2217
2218 etdr = edma_cinfo->tpd_ring[queue_id];
2219
2220 while (etdr->netdev[i])
2221 i++;
2222
2223 if (i >= EDMA_MAX_NETDEV_PER_QUEUE)
2224 return -1;
2225
2226 /* Populate the netdev associated with the tpd ring */
2227 etdr->netdev[i] = edma_netdev[dev];
2228 etdr->nq[i] = netdev_get_tx_queue(edma_netdev[dev], txq_id);
2229
2230 return 0;
2231}
2232
2233/* edma_change_mtu()
2234 * change the MTU of the NIC.
2235 */
2236int edma_change_mtu(struct net_device *netdev, int new_mtu)
2237{
2238 struct edma_adapter *adapter = netdev_priv(netdev);
2239 struct edma_common_info *edma_cinfo = adapter->edma_cinfo;
2240 int old_mtu = netdev->mtu;
2241 int max_frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + (2 * VLAN_HLEN);
2242
2243 if ((max_frame_size < ETH_ZLEN + ETH_FCS_LEN) ||
2244 (max_frame_size > EDMA_MAX_JUMBO_FRAME_SIZE)) {
2245 dev_err(&edma_cinfo->pdev->dev, "MTU setting not correct\n");
2246 return -EINVAL;
2247 }
2248
2249 /* set MTU */
2250 if (old_mtu != new_mtu) {
2251 netdev->mtu = new_mtu;
2252 netdev_update_features(netdev);
2253 }
2254
2255 return 0;
2256}
2257
2258/* edma_set_mac()
2259 * Change the Ethernet Address of the NIC
2260 */
2261int edma_set_mac_addr(struct net_device *netdev, void *p)
2262{
2263 struct sockaddr *addr = p;
2264
2265 if (!is_valid_ether_addr(addr->sa_data))
2266 return -EINVAL;
2267
2268 if (netif_running(netdev))
2269 return -EBUSY;
2270
2271 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2272 return 0;
2273}
2274
2275/* edma_set_stp_rstp()
2276 * set stp/rstp
2277 */
2278void edma_set_stp_rstp(bool rstp)
2279{
2280 edma_stp_rstp = rstp;
2281}
2282
2283/* edma_assign_ath_hdr_type()
2284 * assign atheros header eth type
2285 */
2286void edma_assign_ath_hdr_type(int eth_type)
2287{
2288 edma_ath_eth_type = eth_type & EDMA_ETH_TYPE_MASK;
2289}
2290
2291/* edma_get_default_vlan_tag()
2292 * Used by other modules to get the default vlan tag
2293 */
2294int edma_get_default_vlan_tag(struct net_device *netdev)
2295{
2296 struct edma_adapter *adapter = netdev_priv(netdev);
2297
2298 if (adapter->default_vlan_tag)
2299 return adapter->default_vlan_tag;
2300
2301 return 0;
2302}
2303
2304/* edma_open()
2305 * gets called when netdevice is up, start the queue.
2306 */
2307int edma_open(struct net_device *netdev)
2308{
2309 struct edma_adapter *adapter = netdev_priv(netdev);
2310 struct platform_device *pdev = adapter->edma_cinfo->pdev;
2311
2312 netif_tx_start_all_queues(netdev);
2313 edma_initialise_rfs_flow_table(adapter);
2314 set_bit(__EDMA_UP, &adapter->state_flags);
2315
2316 /* if Link polling is enabled, in our case enabled for WAN, then
2317 * do a phy start, else always set link as UP
2318 */
Rakesh Naired29f6b2017-04-04 15:48:08 +05302319 mutex_lock(&adapter->poll_mutex);
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302320 if (adapter->poll_required) {
2321 if (!IS_ERR(adapter->phydev)) {
2322 phy_start(adapter->phydev);
2323 phy_start_aneg(adapter->phydev);
2324 adapter->link_state = __EDMA_LINKDOWN;
2325 } else {
2326 dev_dbg(&pdev->dev, "Invalid PHY device for a link polled interface\n");
2327 }
2328 } else {
2329 adapter->link_state = __EDMA_LINKUP;
2330 netif_carrier_on(netdev);
2331 }
Rakesh Naired29f6b2017-04-04 15:48:08 +05302332 mutex_unlock(&adapter->poll_mutex);
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302333
2334 return 0;
2335}
2336
2337
2338/* edma_close()
2339 * gets called when netdevice is down, stops the queue.
2340 */
2341int edma_close(struct net_device *netdev)
2342{
2343 struct edma_adapter *adapter = netdev_priv(netdev);
2344
2345 edma_free_rfs_flow_table(adapter);
2346 netif_carrier_off(netdev);
2347 netif_tx_stop_all_queues(netdev);
2348
Rakesh Naired29f6b2017-04-04 15:48:08 +05302349 mutex_lock(&adapter->poll_mutex);
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302350 if (adapter->poll_required) {
2351 if (!IS_ERR(adapter->phydev))
2352 phy_stop(adapter->phydev);
2353 }
Rakesh Naired29f6b2017-04-04 15:48:08 +05302354 mutex_unlock(&adapter->poll_mutex);
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302355
2356 adapter->link_state = __EDMA_LINKDOWN;
2357
2358 /* Set GMAC state to UP before link state is checked
2359 */
2360 clear_bit(__EDMA_UP, &adapter->state_flags);
2361
2362 return 0;
2363}
2364
2365/* edma_poll
2366 * polling function that gets called when the napi gets scheduled.
2367 *
2368 * Main sequence of task performed in this api
2369 * is clear irq status -> clear_tx_irq -> clean_rx_irq->
2370 * enable interrupts.
2371 */
2372int edma_poll(struct napi_struct *napi, int budget)
2373{
2374 struct edma_per_cpu_queues_info *edma_percpu_info = container_of(napi,
2375 struct edma_per_cpu_queues_info, napi);
2376 struct edma_common_info *edma_cinfo = edma_percpu_info->edma_cinfo;
2377 u32 reg_data;
2378 u32 shadow_rx_status, shadow_tx_status;
2379 int queue_id;
2380 int i, work_done = 0;
Rakesh Nair03b586c2017-04-03 18:28:58 +05302381 u16 rx_pending_fill;
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302382
2383 /* Store the Rx/Tx status by ANDing it with
2384 * appropriate CPU RX?TX mask
2385 */
2386 edma_read_reg(EDMA_REG_RX_ISR, &reg_data);
2387 edma_percpu_info->rx_status |= reg_data & edma_percpu_info->rx_mask;
2388 shadow_rx_status = edma_percpu_info->rx_status;
2389 edma_read_reg(EDMA_REG_TX_ISR, &reg_data);
2390 edma_percpu_info->tx_status |= reg_data & edma_percpu_info->tx_mask;
2391 shadow_tx_status = edma_percpu_info->tx_status;
2392
2393 /* Every core will have a start, which will be computed
2394 * in probe and stored in edma_percpu_info->tx_start variable.
2395 * We will shift the status bit by tx_start to obtain
2396 * status bits for the core on which the current processing
2397 * is happening. Since, there are 4 tx queues per core,
2398 * we will run the loop till we get the correct queue to clear.
2399 */
2400 while (edma_percpu_info->tx_status) {
2401 queue_id = ffs(edma_percpu_info->tx_status) - 1;
2402 edma_tx_complete(edma_cinfo, queue_id);
2403 edma_percpu_info->tx_status &= ~(1 << queue_id);
2404 }
2405
2406 /* Every core will have a start, which will be computed
2407 * in probe and stored in edma_percpu_info->tx_start variable.
2408 * We will shift the status bit by tx_start to obtain
2409 * status bits for the core on which the current processing
2410 * is happening. Since, there are 4 tx queues per core, we
2411 * will run the loop till we get the correct queue to clear.
2412 */
2413 while (edma_percpu_info->rx_status) {
2414 queue_id = ffs(edma_percpu_info->rx_status) - 1;
Rakesh Nair03b586c2017-04-03 18:28:58 +05302415 rx_pending_fill = edma_rx_complete(edma_cinfo, &work_done,
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302416 budget, queue_id, napi);
2417
Rakesh Nair03b586c2017-04-03 18:28:58 +05302418 if (likely(work_done < budget)) {
2419 if (rx_pending_fill) {
2420 work_done = budget;
2421 break;
2422 }
2423
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302424 edma_percpu_info->rx_status &= ~(1 << queue_id);
Rakesh Nair03b586c2017-04-03 18:28:58 +05302425 }
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302426 else
2427 break;
2428 }
2429
2430 /* Clear the status register, to avoid the interrupts to
2431 * reoccur.This clearing of interrupt status register is
2432 * done here as writing to status register only takes place
2433 * once the producer/consumer index has been updated to
2434 * reflect that the packet transmission/reception went fine.
2435 */
2436 edma_write_reg(EDMA_REG_RX_ISR, shadow_rx_status);
2437 edma_write_reg(EDMA_REG_TX_ISR, shadow_tx_status);
2438
2439 /* If budget not fully consumed, exit the polling mode */
2440 if (likely(work_done < budget)) {
2441 napi_complete(napi);
2442
2443 /* re-enable the interrupts */
2444 for (i = 0; i < edma_cinfo->num_rxq_per_core; i++)
2445 edma_write_reg(EDMA_REG_RX_INT_MASK_Q(edma_percpu_info->rx_start + i), 0x1);
2446 for (i = 0; i < edma_cinfo->num_txq_per_core; i++)
Rakesh Nair8016fbd2018-01-03 15:46:06 +05302447 edma_write_reg(EDMA_REG_TX_INT_MASK_Q(edma_percpu_info->tx_comp_start + i), 0x1);
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302448 }
2449
2450 return work_done;
2451}
2452
2453/* edma interrupt()
2454 * interrupt handler
2455 */
2456irqreturn_t edma_interrupt(int irq, void *dev)
2457{
2458 struct edma_per_cpu_queues_info *edma_percpu_info = (struct edma_per_cpu_queues_info *) dev;
2459 struct edma_common_info *edma_cinfo = edma_percpu_info->edma_cinfo;
2460 int i;
2461
2462 /* Unmask the TX/RX interrupt register */
2463 for (i = 0; i < edma_cinfo->num_rxq_per_core; i++)
2464 edma_write_reg(EDMA_REG_RX_INT_MASK_Q(edma_percpu_info->rx_start + i), 0x0);
2465
2466 for (i = 0; i < edma_cinfo->num_txq_per_core; i++)
Rakesh Nair8016fbd2018-01-03 15:46:06 +05302467 edma_write_reg(EDMA_REG_TX_INT_MASK_Q(edma_percpu_info->tx_comp_start + i), 0x0);
Rakesh Nair9bcf2602017-01-06 16:02:16 +05302468
2469 napi_schedule(&edma_percpu_info->napi);
2470
2471 return IRQ_HANDLED;
2472}