blob: 155e273ab3ded5ee373142e0b0cc94a57336f451 [file] [log] [blame]
Timur Tabib9b17de2016-08-31 18:22:08 -05001/* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13/* Qualcomm Technologies, Inc. EMAC Ethernet Controller MAC layer support
14 */
15
16#include <linux/tcp.h>
17#include <linux/ip.h>
18#include <linux/ipv6.h>
19#include <linux/crc32.h>
20#include <linux/if_vlan.h>
21#include <linux/jiffies.h>
22#include <linux/phy.h>
23#include <linux/of.h>
24#include <net/ip6_checksum.h>
25#include "emac.h"
26#include "emac-sgmii.h"
27
28/* EMAC base register offsets */
29#define EMAC_MAC_CTRL 0x001480
30#define EMAC_WOL_CTRL0 0x0014a0
31#define EMAC_RSS_KEY0 0x0014b0
32#define EMAC_H1TPD_BASE_ADDR_LO 0x0014e0
33#define EMAC_H2TPD_BASE_ADDR_LO 0x0014e4
34#define EMAC_H3TPD_BASE_ADDR_LO 0x0014e8
35#define EMAC_INTER_SRAM_PART9 0x001534
36#define EMAC_DESC_CTRL_0 0x001540
37#define EMAC_DESC_CTRL_1 0x001544
38#define EMAC_DESC_CTRL_2 0x001550
39#define EMAC_DESC_CTRL_10 0x001554
40#define EMAC_DESC_CTRL_12 0x001558
41#define EMAC_DESC_CTRL_13 0x00155c
42#define EMAC_DESC_CTRL_3 0x001560
43#define EMAC_DESC_CTRL_4 0x001564
44#define EMAC_DESC_CTRL_5 0x001568
45#define EMAC_DESC_CTRL_14 0x00156c
46#define EMAC_DESC_CTRL_15 0x001570
47#define EMAC_DESC_CTRL_16 0x001574
48#define EMAC_DESC_CTRL_6 0x001578
49#define EMAC_DESC_CTRL_8 0x001580
50#define EMAC_DESC_CTRL_9 0x001584
51#define EMAC_DESC_CTRL_11 0x001588
52#define EMAC_TXQ_CTRL_0 0x001590
53#define EMAC_TXQ_CTRL_1 0x001594
54#define EMAC_TXQ_CTRL_2 0x001598
55#define EMAC_RXQ_CTRL_0 0x0015a0
56#define EMAC_RXQ_CTRL_1 0x0015a4
57#define EMAC_RXQ_CTRL_2 0x0015a8
58#define EMAC_RXQ_CTRL_3 0x0015ac
59#define EMAC_BASE_CPU_NUMBER 0x0015b8
60#define EMAC_DMA_CTRL 0x0015c0
61#define EMAC_MAILBOX_0 0x0015e0
62#define EMAC_MAILBOX_5 0x0015e4
63#define EMAC_MAILBOX_6 0x0015e8
64#define EMAC_MAILBOX_13 0x0015ec
65#define EMAC_MAILBOX_2 0x0015f4
66#define EMAC_MAILBOX_3 0x0015f8
67#define EMAC_MAILBOX_11 0x00160c
68#define EMAC_AXI_MAST_CTRL 0x001610
69#define EMAC_MAILBOX_12 0x001614
70#define EMAC_MAILBOX_9 0x001618
71#define EMAC_MAILBOX_10 0x00161c
72#define EMAC_ATHR_HEADER_CTRL 0x001620
73#define EMAC_CLK_GATE_CTRL 0x001814
74#define EMAC_MISC_CTRL 0x001990
75#define EMAC_MAILBOX_7 0x0019e0
76#define EMAC_MAILBOX_8 0x0019e4
77#define EMAC_MAILBOX_15 0x001bd4
78#define EMAC_MAILBOX_16 0x001bd8
79
80/* EMAC_MAC_CTRL */
81#define SINGLE_PAUSE_MODE 0x10000000
82#define DEBUG_MODE 0x08000000
83#define BROAD_EN 0x04000000
84#define MULTI_ALL 0x02000000
85#define RX_CHKSUM_EN 0x01000000
86#define HUGE 0x00800000
87#define SPEED(x) (((x) & 0x3) << 20)
88#define SPEED_MASK SPEED(0x3)
89#define SIMR 0x00080000
90#define TPAUSE 0x00010000
91#define PROM_MODE 0x00008000
92#define VLAN_STRIP 0x00004000
93#define PRLEN_BMSK 0x00003c00
94#define PRLEN_SHFT 10
95#define HUGEN 0x00000200
96#define FLCHK 0x00000100
97#define PCRCE 0x00000080
98#define CRCE 0x00000040
99#define FULLD 0x00000020
100#define MAC_LP_EN 0x00000010
101#define RXFC 0x00000008
102#define TXFC 0x00000004
103#define RXEN 0x00000002
104#define TXEN 0x00000001
105
106
107/* EMAC_WOL_CTRL0 */
108#define LK_CHG_PME 0x20
109#define LK_CHG_EN 0x10
110#define MG_FRAME_PME 0x8
111#define MG_FRAME_EN 0x4
112#define WK_FRAME_EN 0x1
113
114/* EMAC_DESC_CTRL_3 */
115#define RFD_RING_SIZE_BMSK 0xfff
116
117/* EMAC_DESC_CTRL_4 */
118#define RX_BUFFER_SIZE_BMSK 0xffff
119
120/* EMAC_DESC_CTRL_6 */
121#define RRD_RING_SIZE_BMSK 0xfff
122
123/* EMAC_DESC_CTRL_9 */
124#define TPD_RING_SIZE_BMSK 0xffff
125
126/* EMAC_TXQ_CTRL_0 */
127#define NUM_TXF_BURST_PREF_BMSK 0xffff0000
128#define NUM_TXF_BURST_PREF_SHFT 16
129#define LS_8023_SP 0x80
130#define TXQ_MODE 0x40
131#define TXQ_EN 0x20
132#define IP_OP_SP 0x10
133#define NUM_TPD_BURST_PREF_BMSK 0xf
134#define NUM_TPD_BURST_PREF_SHFT 0
135
136/* EMAC_TXQ_CTRL_1 */
137#define JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK 0x7ff
138
139/* EMAC_TXQ_CTRL_2 */
140#define TXF_HWM_BMSK 0xfff0000
141#define TXF_LWM_BMSK 0xfff
142
143/* EMAC_RXQ_CTRL_0 */
144#define RXQ_EN BIT(31)
145#define CUT_THRU_EN BIT(30)
146#define RSS_HASH_EN BIT(29)
147#define NUM_RFD_BURST_PREF_BMSK 0x3f00000
148#define NUM_RFD_BURST_PREF_SHFT 20
149#define IDT_TABLE_SIZE_BMSK 0x1ff00
150#define IDT_TABLE_SIZE_SHFT 8
151#define SP_IPV6 0x80
152
153/* EMAC_RXQ_CTRL_1 */
154#define JUMBO_1KAH_BMSK 0xf000
155#define JUMBO_1KAH_SHFT 12
156#define RFD_PREF_LOW_TH 0x10
157#define RFD_PREF_LOW_THRESHOLD_BMSK 0xfc0
158#define RFD_PREF_LOW_THRESHOLD_SHFT 6
159#define RFD_PREF_UP_TH 0x10
160#define RFD_PREF_UP_THRESHOLD_BMSK 0x3f
161#define RFD_PREF_UP_THRESHOLD_SHFT 0
162
163/* EMAC_RXQ_CTRL_2 */
164#define RXF_DOF_THRESFHOLD 0x1a0
165#define RXF_DOF_THRESHOLD_BMSK 0xfff0000
166#define RXF_DOF_THRESHOLD_SHFT 16
167#define RXF_UOF_THRESFHOLD 0xbe
168#define RXF_UOF_THRESHOLD_BMSK 0xfff
169#define RXF_UOF_THRESHOLD_SHFT 0
170
171/* EMAC_RXQ_CTRL_3 */
172#define RXD_TIMER_BMSK 0xffff0000
173#define RXD_THRESHOLD_BMSK 0xfff
174#define RXD_THRESHOLD_SHFT 0
175
176/* EMAC_DMA_CTRL */
177#define DMAW_DLY_CNT_BMSK 0xf0000
178#define DMAW_DLY_CNT_SHFT 16
179#define DMAR_DLY_CNT_BMSK 0xf800
180#define DMAR_DLY_CNT_SHFT 11
181#define DMAR_REQ_PRI 0x400
182#define REGWRBLEN_BMSK 0x380
183#define REGWRBLEN_SHFT 7
184#define REGRDBLEN_BMSK 0x70
185#define REGRDBLEN_SHFT 4
186#define OUT_ORDER_MODE 0x4
187#define ENH_ORDER_MODE 0x2
188#define IN_ORDER_MODE 0x1
189
190/* EMAC_MAILBOX_13 */
191#define RFD3_PROC_IDX_BMSK 0xfff0000
192#define RFD3_PROC_IDX_SHFT 16
193#define RFD3_PROD_IDX_BMSK 0xfff
194#define RFD3_PROD_IDX_SHFT 0
195
196/* EMAC_MAILBOX_2 */
197#define NTPD_CONS_IDX_BMSK 0xffff0000
198#define NTPD_CONS_IDX_SHFT 16
199
200/* EMAC_MAILBOX_3 */
201#define RFD0_CONS_IDX_BMSK 0xfff
202#define RFD0_CONS_IDX_SHFT 0
203
204/* EMAC_MAILBOX_11 */
205#define H3TPD_PROD_IDX_BMSK 0xffff0000
206#define H3TPD_PROD_IDX_SHFT 16
207
208/* EMAC_AXI_MAST_CTRL */
209#define DATA_BYTE_SWAP 0x8
210#define MAX_BOUND 0x2
211#define MAX_BTYPE 0x1
212
213/* EMAC_MAILBOX_12 */
214#define H3TPD_CONS_IDX_BMSK 0xffff0000
215#define H3TPD_CONS_IDX_SHFT 16
216
217/* EMAC_MAILBOX_9 */
218#define H2TPD_PROD_IDX_BMSK 0xffff
219#define H2TPD_PROD_IDX_SHFT 0
220
221/* EMAC_MAILBOX_10 */
222#define H1TPD_CONS_IDX_BMSK 0xffff0000
223#define H1TPD_CONS_IDX_SHFT 16
224#define H2TPD_CONS_IDX_BMSK 0xffff
225#define H2TPD_CONS_IDX_SHFT 0
226
227/* EMAC_ATHR_HEADER_CTRL */
228#define HEADER_CNT_EN 0x2
229#define HEADER_ENABLE 0x1
230
231/* EMAC_MAILBOX_0 */
232#define RFD0_PROC_IDX_BMSK 0xfff0000
233#define RFD0_PROC_IDX_SHFT 16
234#define RFD0_PROD_IDX_BMSK 0xfff
235#define RFD0_PROD_IDX_SHFT 0
236
237/* EMAC_MAILBOX_5 */
238#define RFD1_PROC_IDX_BMSK 0xfff0000
239#define RFD1_PROC_IDX_SHFT 16
240#define RFD1_PROD_IDX_BMSK 0xfff
241#define RFD1_PROD_IDX_SHFT 0
242
243/* EMAC_MISC_CTRL */
244#define RX_UNCPL_INT_EN 0x1
245
246/* EMAC_MAILBOX_7 */
247#define RFD2_CONS_IDX_BMSK 0xfff0000
248#define RFD2_CONS_IDX_SHFT 16
249#define RFD1_CONS_IDX_BMSK 0xfff
250#define RFD1_CONS_IDX_SHFT 0
251
252/* EMAC_MAILBOX_8 */
253#define RFD3_CONS_IDX_BMSK 0xfff
254#define RFD3_CONS_IDX_SHFT 0
255
256/* EMAC_MAILBOX_15 */
257#define NTPD_PROD_IDX_BMSK 0xffff
258#define NTPD_PROD_IDX_SHFT 0
259
260/* EMAC_MAILBOX_16 */
261#define H1TPD_PROD_IDX_BMSK 0xffff
262#define H1TPD_PROD_IDX_SHFT 0
263
264#define RXQ0_RSS_HSTYP_IPV6_TCP_EN 0x20
265#define RXQ0_RSS_HSTYP_IPV6_EN 0x10
266#define RXQ0_RSS_HSTYP_IPV4_TCP_EN 0x8
267#define RXQ0_RSS_HSTYP_IPV4_EN 0x4
268
269/* EMAC_EMAC_WRAPPER_TX_TS_INX */
270#define EMAC_WRAPPER_TX_TS_EMPTY BIT(31)
271#define EMAC_WRAPPER_TX_TS_INX_BMSK 0xffff
272
273struct emac_skb_cb {
274 u32 tpd_idx;
275 unsigned long jiffies;
276};
277
278#define EMAC_SKB_CB(skb) ((struct emac_skb_cb *)(skb)->cb)
279#define EMAC_RSS_IDT_SIZE 256
280#define JUMBO_1KAH 0x4
281#define RXD_TH 0x100
282#define EMAC_TPD_LAST_FRAGMENT 0x80000000
283#define EMAC_TPD_TSTAMP_SAVE 0x80000000
284
285/* EMAC Errors in emac_rrd.word[3] */
286#define EMAC_RRD_L4F BIT(14)
287#define EMAC_RRD_IPF BIT(15)
288#define EMAC_RRD_CRC BIT(21)
289#define EMAC_RRD_FAE BIT(22)
290#define EMAC_RRD_TRN BIT(23)
291#define EMAC_RRD_RNT BIT(24)
292#define EMAC_RRD_INC BIT(25)
293#define EMAC_RRD_FOV BIT(29)
294#define EMAC_RRD_LEN BIT(30)
295
296/* Error bits that will result in a received frame being discarded */
297#define EMAC_RRD_ERROR (EMAC_RRD_IPF | EMAC_RRD_CRC | EMAC_RRD_FAE | \
298 EMAC_RRD_TRN | EMAC_RRD_RNT | EMAC_RRD_INC | \
299 EMAC_RRD_FOV | EMAC_RRD_LEN)
300#define EMAC_RRD_STATS_DW_IDX 3
301
302#define EMAC_RRD(RXQ, SIZE, IDX) ((RXQ)->rrd.v_addr + (SIZE * (IDX)))
303#define EMAC_RFD(RXQ, SIZE, IDX) ((RXQ)->rfd.v_addr + (SIZE * (IDX)))
304#define EMAC_TPD(TXQ, SIZE, IDX) ((TXQ)->tpd.v_addr + (SIZE * (IDX)))
305
306#define GET_RFD_BUFFER(RXQ, IDX) (&((RXQ)->rfd.rfbuff[(IDX)]))
307#define GET_TPD_BUFFER(RTQ, IDX) (&((RTQ)->tpd.tpbuff[(IDX)]))
308
309#define EMAC_TX_POLL_HWTXTSTAMP_THRESHOLD 8
310
311#define ISR_RX_PKT (\
312 RX_PKT_INT0 |\
313 RX_PKT_INT1 |\
314 RX_PKT_INT2 |\
315 RX_PKT_INT3)
316
Timur Tabib9b17de2016-08-31 18:22:08 -0500317void emac_mac_multicast_addr_set(struct emac_adapter *adpt, u8 *addr)
318{
319 u32 crc32, bit, reg, mta;
320
321 /* Calculate the CRC of the MAC address */
322 crc32 = ether_crc(ETH_ALEN, addr);
323
324 /* The HASH Table is an array of 2 32-bit registers. It is
325 * treated like an array of 64 bits (BitArray[hash_value]).
326 * Use the upper 6 bits of the above CRC as the hash value.
327 */
328 reg = (crc32 >> 31) & 0x1;
329 bit = (crc32 >> 26) & 0x1F;
330
331 mta = readl(adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
332 mta |= BIT(bit);
333 writel(mta, adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
334}
335
336void emac_mac_multicast_addr_clear(struct emac_adapter *adpt)
337{
338 writel(0, adpt->base + EMAC_HASH_TAB_REG0);
339 writel(0, adpt->base + EMAC_HASH_TAB_REG1);
340}
341
342/* definitions for RSS */
343#define EMAC_RSS_KEY(_i, _type) \
344 (EMAC_RSS_KEY0 + ((_i) * sizeof(_type)))
345#define EMAC_RSS_TBL(_i, _type) \
346 (EMAC_IDT_TABLE0 + ((_i) * sizeof(_type)))
347
348/* Config MAC modes */
349void emac_mac_mode_config(struct emac_adapter *adpt)
350{
351 struct net_device *netdev = adpt->netdev;
352 u32 mac;
353
354 mac = readl(adpt->base + EMAC_MAC_CTRL);
355 mac &= ~(VLAN_STRIP | PROM_MODE | MULTI_ALL | MAC_LP_EN);
356
357 if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
358 mac |= VLAN_STRIP;
359
360 if (netdev->flags & IFF_PROMISC)
361 mac |= PROM_MODE;
362
363 if (netdev->flags & IFF_ALLMULTI)
364 mac |= MULTI_ALL;
365
366 writel(mac, adpt->base + EMAC_MAC_CTRL);
367}
368
369/* Config descriptor rings */
370static void emac_mac_dma_rings_config(struct emac_adapter *adpt)
371{
372 static const unsigned short tpd_q_offset[] = {
373 EMAC_DESC_CTRL_8, EMAC_H1TPD_BASE_ADDR_LO,
374 EMAC_H2TPD_BASE_ADDR_LO, EMAC_H3TPD_BASE_ADDR_LO};
375 static const unsigned short rfd_q_offset[] = {
376 EMAC_DESC_CTRL_2, EMAC_DESC_CTRL_10,
377 EMAC_DESC_CTRL_12, EMAC_DESC_CTRL_13};
378 static const unsigned short rrd_q_offset[] = {
379 EMAC_DESC_CTRL_5, EMAC_DESC_CTRL_14,
380 EMAC_DESC_CTRL_15, EMAC_DESC_CTRL_16};
381
382 /* TPD (Transmit Packet Descriptor) */
383 writel(upper_32_bits(adpt->tx_q.tpd.dma_addr),
384 adpt->base + EMAC_DESC_CTRL_1);
385
386 writel(lower_32_bits(adpt->tx_q.tpd.dma_addr),
387 adpt->base + tpd_q_offset[0]);
388
389 writel(adpt->tx_q.tpd.count & TPD_RING_SIZE_BMSK,
390 adpt->base + EMAC_DESC_CTRL_9);
391
392 /* RFD (Receive Free Descriptor) & RRD (Receive Return Descriptor) */
393 writel(upper_32_bits(adpt->rx_q.rfd.dma_addr),
394 adpt->base + EMAC_DESC_CTRL_0);
395
396 writel(lower_32_bits(adpt->rx_q.rfd.dma_addr),
397 adpt->base + rfd_q_offset[0]);
398 writel(lower_32_bits(adpt->rx_q.rrd.dma_addr),
399 adpt->base + rrd_q_offset[0]);
400
401 writel(adpt->rx_q.rfd.count & RFD_RING_SIZE_BMSK,
402 adpt->base + EMAC_DESC_CTRL_3);
403 writel(adpt->rx_q.rrd.count & RRD_RING_SIZE_BMSK,
404 adpt->base + EMAC_DESC_CTRL_6);
405
406 writel(adpt->rxbuf_size & RX_BUFFER_SIZE_BMSK,
407 adpt->base + EMAC_DESC_CTRL_4);
408
409 writel(0, adpt->base + EMAC_DESC_CTRL_11);
410
411 /* Load all of the base addresses above and ensure that triggering HW to
412 * read ring pointers is flushed
413 */
414 writel(1, adpt->base + EMAC_INTER_SRAM_PART9);
415}
416
417/* Config transmit parameters */
418static void emac_mac_tx_config(struct emac_adapter *adpt)
419{
420 u32 val;
421
422 writel((EMAC_MAX_TX_OFFLOAD_THRESH >> 3) &
423 JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK, adpt->base + EMAC_TXQ_CTRL_1);
424
425 val = (adpt->tpd_burst << NUM_TPD_BURST_PREF_SHFT) &
426 NUM_TPD_BURST_PREF_BMSK;
427
428 val |= TXQ_MODE | LS_8023_SP;
429 val |= (0x0100 << NUM_TXF_BURST_PREF_SHFT) &
430 NUM_TXF_BURST_PREF_BMSK;
431
432 writel(val, adpt->base + EMAC_TXQ_CTRL_0);
433 emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_2,
434 (TXF_HWM_BMSK | TXF_LWM_BMSK), 0);
435}
436
437/* Config receive parameters */
438static void emac_mac_rx_config(struct emac_adapter *adpt)
439{
440 u32 val;
441
442 val = (adpt->rfd_burst << NUM_RFD_BURST_PREF_SHFT) &
443 NUM_RFD_BURST_PREF_BMSK;
444 val |= (SP_IPV6 | CUT_THRU_EN);
445
446 writel(val, adpt->base + EMAC_RXQ_CTRL_0);
447
448 val = readl(adpt->base + EMAC_RXQ_CTRL_1);
449 val &= ~(JUMBO_1KAH_BMSK | RFD_PREF_LOW_THRESHOLD_BMSK |
450 RFD_PREF_UP_THRESHOLD_BMSK);
451 val |= (JUMBO_1KAH << JUMBO_1KAH_SHFT) |
452 (RFD_PREF_LOW_TH << RFD_PREF_LOW_THRESHOLD_SHFT) |
453 (RFD_PREF_UP_TH << RFD_PREF_UP_THRESHOLD_SHFT);
454 writel(val, adpt->base + EMAC_RXQ_CTRL_1);
455
456 val = readl(adpt->base + EMAC_RXQ_CTRL_2);
457 val &= ~(RXF_DOF_THRESHOLD_BMSK | RXF_UOF_THRESHOLD_BMSK);
458 val |= (RXF_DOF_THRESFHOLD << RXF_DOF_THRESHOLD_SHFT) |
459 (RXF_UOF_THRESFHOLD << RXF_UOF_THRESHOLD_SHFT);
460 writel(val, adpt->base + EMAC_RXQ_CTRL_2);
461
462 val = readl(adpt->base + EMAC_RXQ_CTRL_3);
463 val &= ~(RXD_TIMER_BMSK | RXD_THRESHOLD_BMSK);
464 val |= RXD_TH << RXD_THRESHOLD_SHFT;
465 writel(val, adpt->base + EMAC_RXQ_CTRL_3);
466}
467
468/* Config dma */
469static void emac_mac_dma_config(struct emac_adapter *adpt)
470{
471 u32 dma_ctrl = DMAR_REQ_PRI;
472
473 switch (adpt->dma_order) {
474 case emac_dma_ord_in:
475 dma_ctrl |= IN_ORDER_MODE;
476 break;
477 case emac_dma_ord_enh:
478 dma_ctrl |= ENH_ORDER_MODE;
479 break;
480 case emac_dma_ord_out:
481 dma_ctrl |= OUT_ORDER_MODE;
482 break;
483 default:
484 break;
485 }
486
487 dma_ctrl |= (((u32)adpt->dmar_block) << REGRDBLEN_SHFT) &
488 REGRDBLEN_BMSK;
489 dma_ctrl |= (((u32)adpt->dmaw_block) << REGWRBLEN_SHFT) &
490 REGWRBLEN_BMSK;
491 dma_ctrl |= (((u32)adpt->dmar_dly_cnt) << DMAR_DLY_CNT_SHFT) &
492 DMAR_DLY_CNT_BMSK;
493 dma_ctrl |= (((u32)adpt->dmaw_dly_cnt) << DMAW_DLY_CNT_SHFT) &
494 DMAW_DLY_CNT_BMSK;
495
496 /* config DMA and ensure that configuration is flushed to HW */
497 writel(dma_ctrl, adpt->base + EMAC_DMA_CTRL);
498}
499
500/* set MAC address */
501static void emac_set_mac_address(struct emac_adapter *adpt, u8 *addr)
502{
503 u32 sta;
504
505 /* for example: 00-A0-C6-11-22-33
506 * 0<-->C6112233, 1<-->00A0.
507 */
508
509 /* low 32bit word */
510 sta = (((u32)addr[2]) << 24) | (((u32)addr[3]) << 16) |
511 (((u32)addr[4]) << 8) | (((u32)addr[5]));
512 writel(sta, adpt->base + EMAC_MAC_STA_ADDR0);
513
514 /* hight 32bit word */
515 sta = (((u32)addr[0]) << 8) | (u32)addr[1];
516 writel(sta, adpt->base + EMAC_MAC_STA_ADDR1);
517}
518
519static void emac_mac_config(struct emac_adapter *adpt)
520{
521 struct net_device *netdev = adpt->netdev;
522 unsigned int max_frame;
523 u32 val;
524
525 emac_set_mac_address(adpt, netdev->dev_addr);
526
527 max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
528 adpt->rxbuf_size = netdev->mtu > EMAC_DEF_RX_BUF_SIZE ?
529 ALIGN(max_frame, 8) : EMAC_DEF_RX_BUF_SIZE;
530
531 emac_mac_dma_rings_config(adpt);
532
533 writel(netdev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
534 adpt->base + EMAC_MAX_FRAM_LEN_CTRL);
535
536 emac_mac_tx_config(adpt);
537 emac_mac_rx_config(adpt);
538 emac_mac_dma_config(adpt);
539
540 val = readl(adpt->base + EMAC_AXI_MAST_CTRL);
541 val &= ~(DATA_BYTE_SWAP | MAX_BOUND);
542 val |= MAX_BTYPE;
543 writel(val, adpt->base + EMAC_AXI_MAST_CTRL);
544 writel(0, adpt->base + EMAC_CLK_GATE_CTRL);
545 writel(RX_UNCPL_INT_EN, adpt->base + EMAC_MISC_CTRL);
546}
547
548void emac_mac_reset(struct emac_adapter *adpt)
549{
550 emac_mac_stop(adpt);
551
552 emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, SOFT_RST);
553 usleep_range(100, 150); /* reset may take up to 100usec */
554
555 /* interrupt clear-on-read */
556 emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, INT_RD_CLR_EN);
557}
558
559void emac_mac_start(struct emac_adapter *adpt)
560{
561 struct phy_device *phydev = adpt->phydev;
562 u32 mac, csr1;
563
564 /* enable tx queue */
565 emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, 0, TXQ_EN);
566
567 /* enable rx queue */
568 emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, 0, RXQ_EN);
569
570 /* enable mac control */
571 mac = readl(adpt->base + EMAC_MAC_CTRL);
572 csr1 = readl(adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
573
574 mac |= TXEN | RXEN; /* enable RX/TX */
575
Timur Tabidf630222016-11-07 10:51:41 -0600576 /* Configure MAC flow control to match the PHY's settings. */
577 if (phydev->pause)
578 mac |= RXFC;
579 if (phydev->pause != phydev->asym_pause)
580 mac |= TXFC;
Timur Tabib9b17de2016-08-31 18:22:08 -0500581
582 /* setup link speed */
583 mac &= ~SPEED_MASK;
584 if (phydev->speed == SPEED_1000) {
585 mac |= SPEED(2);
586 csr1 |= FREQ_MODE;
587 } else {
588 mac |= SPEED(1);
589 csr1 &= ~FREQ_MODE;
590 }
591
592 if (phydev->duplex == DUPLEX_FULL)
593 mac |= FULLD;
594 else
595 mac &= ~FULLD;
596
597 /* other parameters */
598 mac |= (CRCE | PCRCE);
599 mac |= ((adpt->preamble << PRLEN_SHFT) & PRLEN_BMSK);
600 mac |= BROAD_EN;
601 mac |= FLCHK;
602 mac &= ~RX_CHKSUM_EN;
603 mac &= ~(HUGEN | VLAN_STRIP | TPAUSE | SIMR | HUGE | MULTI_ALL |
604 DEBUG_MODE | SINGLE_PAUSE_MODE);
605
606 writel_relaxed(csr1, adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
607
608 writel_relaxed(mac, adpt->base + EMAC_MAC_CTRL);
609
610 /* enable interrupt read clear, low power sleep mode and
611 * the irq moderators
612 */
613
614 writel_relaxed(adpt->irq_mod, adpt->base + EMAC_IRQ_MOD_TIM_INIT);
615 writel_relaxed(INT_RD_CLR_EN | LPW_MODE | IRQ_MODERATOR_EN |
616 IRQ_MODERATOR2_EN, adpt->base + EMAC_DMA_MAS_CTRL);
617
618 emac_mac_mode_config(adpt);
619
620 emac_reg_update32(adpt->base + EMAC_ATHR_HEADER_CTRL,
621 (HEADER_ENABLE | HEADER_CNT_EN), 0);
622
623 emac_reg_update32(adpt->csr + EMAC_EMAC_WRAPPER_CSR2, 0, WOL_EN);
624}
625
626void emac_mac_stop(struct emac_adapter *adpt)
627{
628 emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, RXQ_EN, 0);
629 emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, TXQ_EN, 0);
630 emac_reg_update32(adpt->base + EMAC_MAC_CTRL, TXEN | RXEN, 0);
631 usleep_range(1000, 1050); /* stopping mac may take upto 1msec */
632}
633
634/* Free all descriptors of given transmit queue */
635static void emac_tx_q_descs_free(struct emac_adapter *adpt)
636{
637 struct emac_tx_queue *tx_q = &adpt->tx_q;
638 unsigned int i;
639 size_t size;
640
641 /* ring already cleared, nothing to do */
642 if (!tx_q->tpd.tpbuff)
643 return;
644
645 for (i = 0; i < tx_q->tpd.count; i++) {
646 struct emac_buffer *tpbuf = GET_TPD_BUFFER(tx_q, i);
647
648 if (tpbuf->dma_addr) {
649 dma_unmap_single(adpt->netdev->dev.parent,
650 tpbuf->dma_addr, tpbuf->length,
651 DMA_TO_DEVICE);
652 tpbuf->dma_addr = 0;
653 }
654 if (tpbuf->skb) {
655 dev_kfree_skb_any(tpbuf->skb);
656 tpbuf->skb = NULL;
657 }
658 }
659
660 size = sizeof(struct emac_buffer) * tx_q->tpd.count;
661 memset(tx_q->tpd.tpbuff, 0, size);
662
663 /* clear the descriptor ring */
664 memset(tx_q->tpd.v_addr, 0, tx_q->tpd.size);
665
666 tx_q->tpd.consume_idx = 0;
667 tx_q->tpd.produce_idx = 0;
668}
669
670/* Free all descriptors of given receive queue */
671static void emac_rx_q_free_descs(struct emac_adapter *adpt)
672{
673 struct device *dev = adpt->netdev->dev.parent;
674 struct emac_rx_queue *rx_q = &adpt->rx_q;
675 unsigned int i;
676 size_t size;
677
678 /* ring already cleared, nothing to do */
679 if (!rx_q->rfd.rfbuff)
680 return;
681
682 for (i = 0; i < rx_q->rfd.count; i++) {
683 struct emac_buffer *rfbuf = GET_RFD_BUFFER(rx_q, i);
684
685 if (rfbuf->dma_addr) {
686 dma_unmap_single(dev, rfbuf->dma_addr, rfbuf->length,
687 DMA_FROM_DEVICE);
688 rfbuf->dma_addr = 0;
689 }
690 if (rfbuf->skb) {
691 dev_kfree_skb(rfbuf->skb);
692 rfbuf->skb = NULL;
693 }
694 }
695
696 size = sizeof(struct emac_buffer) * rx_q->rfd.count;
697 memset(rx_q->rfd.rfbuff, 0, size);
698
699 /* clear the descriptor rings */
700 memset(rx_q->rrd.v_addr, 0, rx_q->rrd.size);
701 rx_q->rrd.produce_idx = 0;
702 rx_q->rrd.consume_idx = 0;
703
704 memset(rx_q->rfd.v_addr, 0, rx_q->rfd.size);
705 rx_q->rfd.produce_idx = 0;
706 rx_q->rfd.consume_idx = 0;
707}
708
709/* Free all buffers associated with given transmit queue */
710static void emac_tx_q_bufs_free(struct emac_adapter *adpt)
711{
712 struct emac_tx_queue *tx_q = &adpt->tx_q;
713
714 emac_tx_q_descs_free(adpt);
715
716 kfree(tx_q->tpd.tpbuff);
717 tx_q->tpd.tpbuff = NULL;
718 tx_q->tpd.v_addr = NULL;
719 tx_q->tpd.dma_addr = 0;
720 tx_q->tpd.size = 0;
721}
722
723/* Allocate TX descriptor ring for the given transmit queue */
724static int emac_tx_q_desc_alloc(struct emac_adapter *adpt,
725 struct emac_tx_queue *tx_q)
726{
727 struct emac_ring_header *ring_header = &adpt->ring_header;
728 size_t size;
729
730 size = sizeof(struct emac_buffer) * tx_q->tpd.count;
731 tx_q->tpd.tpbuff = kzalloc(size, GFP_KERNEL);
732 if (!tx_q->tpd.tpbuff)
733 return -ENOMEM;
734
735 tx_q->tpd.size = tx_q->tpd.count * (adpt->tpd_size * 4);
736 tx_q->tpd.dma_addr = ring_header->dma_addr + ring_header->used;
737 tx_q->tpd.v_addr = ring_header->v_addr + ring_header->used;
738 ring_header->used += ALIGN(tx_q->tpd.size, 8);
739 tx_q->tpd.produce_idx = 0;
740 tx_q->tpd.consume_idx = 0;
741
742 return 0;
743}
744
745/* Free all buffers associated with given transmit queue */
746static void emac_rx_q_bufs_free(struct emac_adapter *adpt)
747{
748 struct emac_rx_queue *rx_q = &adpt->rx_q;
749
750 emac_rx_q_free_descs(adpt);
751
752 kfree(rx_q->rfd.rfbuff);
753 rx_q->rfd.rfbuff = NULL;
754
755 rx_q->rfd.v_addr = NULL;
756 rx_q->rfd.dma_addr = 0;
757 rx_q->rfd.size = 0;
758
759 rx_q->rrd.v_addr = NULL;
760 rx_q->rrd.dma_addr = 0;
761 rx_q->rrd.size = 0;
762}
763
764/* Allocate RX descriptor rings for the given receive queue */
765static int emac_rx_descs_alloc(struct emac_adapter *adpt)
766{
767 struct emac_ring_header *ring_header = &adpt->ring_header;
768 struct emac_rx_queue *rx_q = &adpt->rx_q;
769 size_t size;
770
771 size = sizeof(struct emac_buffer) * rx_q->rfd.count;
772 rx_q->rfd.rfbuff = kzalloc(size, GFP_KERNEL);
773 if (!rx_q->rfd.rfbuff)
774 return -ENOMEM;
775
776 rx_q->rrd.size = rx_q->rrd.count * (adpt->rrd_size * 4);
777 rx_q->rfd.size = rx_q->rfd.count * (adpt->rfd_size * 4);
778
779 rx_q->rrd.dma_addr = ring_header->dma_addr + ring_header->used;
780 rx_q->rrd.v_addr = ring_header->v_addr + ring_header->used;
781 ring_header->used += ALIGN(rx_q->rrd.size, 8);
782
783 rx_q->rfd.dma_addr = ring_header->dma_addr + ring_header->used;
784 rx_q->rfd.v_addr = ring_header->v_addr + ring_header->used;
785 ring_header->used += ALIGN(rx_q->rfd.size, 8);
786
787 rx_q->rrd.produce_idx = 0;
788 rx_q->rrd.consume_idx = 0;
789
790 rx_q->rfd.produce_idx = 0;
791 rx_q->rfd.consume_idx = 0;
792
793 return 0;
794}
795
796/* Allocate all TX and RX descriptor rings */
797int emac_mac_rx_tx_rings_alloc_all(struct emac_adapter *adpt)
798{
799 struct emac_ring_header *ring_header = &adpt->ring_header;
800 struct device *dev = adpt->netdev->dev.parent;
801 unsigned int num_tx_descs = adpt->tx_desc_cnt;
802 unsigned int num_rx_descs = adpt->rx_desc_cnt;
803 int ret;
804
805 adpt->tx_q.tpd.count = adpt->tx_desc_cnt;
806
807 adpt->rx_q.rrd.count = adpt->rx_desc_cnt;
808 adpt->rx_q.rfd.count = adpt->rx_desc_cnt;
809
810 /* Ring DMA buffer. Each ring may need up to 8 bytes for alignment,
811 * hence the additional padding bytes are allocated.
812 */
813 ring_header->size = num_tx_descs * (adpt->tpd_size * 4) +
814 num_rx_descs * (adpt->rfd_size * 4) +
815 num_rx_descs * (adpt->rrd_size * 4) +
816 8 + 2 * 8; /* 8 byte per one Tx and two Rx rings */
817
818 ring_header->used = 0;
819 ring_header->v_addr = dma_zalloc_coherent(dev, ring_header->size,
820 &ring_header->dma_addr,
821 GFP_KERNEL);
822 if (!ring_header->v_addr)
823 return -ENOMEM;
824
825 ring_header->used = ALIGN(ring_header->dma_addr, 8) -
826 ring_header->dma_addr;
827
828 ret = emac_tx_q_desc_alloc(adpt, &adpt->tx_q);
829 if (ret) {
830 netdev_err(adpt->netdev, "error: Tx Queue alloc failed\n");
831 goto err_alloc_tx;
832 }
833
834 ret = emac_rx_descs_alloc(adpt);
835 if (ret) {
836 netdev_err(adpt->netdev, "error: Rx Queue alloc failed\n");
837 goto err_alloc_rx;
838 }
839
840 return 0;
841
842err_alloc_rx:
843 emac_tx_q_bufs_free(adpt);
844err_alloc_tx:
845 dma_free_coherent(dev, ring_header->size,
846 ring_header->v_addr, ring_header->dma_addr);
847
848 ring_header->v_addr = NULL;
849 ring_header->dma_addr = 0;
850 ring_header->size = 0;
851 ring_header->used = 0;
852
853 return ret;
854}
855
856/* Free all TX and RX descriptor rings */
857void emac_mac_rx_tx_rings_free_all(struct emac_adapter *adpt)
858{
859 struct emac_ring_header *ring_header = &adpt->ring_header;
860 struct device *dev = adpt->netdev->dev.parent;
861
862 emac_tx_q_bufs_free(adpt);
863 emac_rx_q_bufs_free(adpt);
864
865 dma_free_coherent(dev, ring_header->size,
866 ring_header->v_addr, ring_header->dma_addr);
867
868 ring_header->v_addr = NULL;
869 ring_header->dma_addr = 0;
870 ring_header->size = 0;
871 ring_header->used = 0;
872}
873
874/* Initialize descriptor rings */
875static void emac_mac_rx_tx_ring_reset_all(struct emac_adapter *adpt)
876{
877 unsigned int i;
878
879 adpt->tx_q.tpd.produce_idx = 0;
880 adpt->tx_q.tpd.consume_idx = 0;
881 for (i = 0; i < adpt->tx_q.tpd.count; i++)
882 adpt->tx_q.tpd.tpbuff[i].dma_addr = 0;
883
884 adpt->rx_q.rrd.produce_idx = 0;
885 adpt->rx_q.rrd.consume_idx = 0;
886 adpt->rx_q.rfd.produce_idx = 0;
887 adpt->rx_q.rfd.consume_idx = 0;
888 for (i = 0; i < adpt->rx_q.rfd.count; i++)
889 adpt->rx_q.rfd.rfbuff[i].dma_addr = 0;
890}
891
892/* Produce new receive free descriptor */
893static void emac_mac_rx_rfd_create(struct emac_adapter *adpt,
894 struct emac_rx_queue *rx_q,
895 dma_addr_t addr)
896{
897 u32 *hw_rfd = EMAC_RFD(rx_q, adpt->rfd_size, rx_q->rfd.produce_idx);
898
899 *(hw_rfd++) = lower_32_bits(addr);
900 *hw_rfd = upper_32_bits(addr);
901
902 if (++rx_q->rfd.produce_idx == rx_q->rfd.count)
903 rx_q->rfd.produce_idx = 0;
904}
905
906/* Fill up receive queue's RFD with preallocated receive buffers */
907static void emac_mac_rx_descs_refill(struct emac_adapter *adpt,
908 struct emac_rx_queue *rx_q)
909{
910 struct emac_buffer *curr_rxbuf;
911 struct emac_buffer *next_rxbuf;
912 unsigned int count = 0;
913 u32 next_produce_idx;
914
915 next_produce_idx = rx_q->rfd.produce_idx + 1;
916 if (next_produce_idx == rx_q->rfd.count)
917 next_produce_idx = 0;
918
919 curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
920 next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
921
922 /* this always has a blank rx_buffer*/
923 while (!next_rxbuf->dma_addr) {
924 struct sk_buff *skb;
925 int ret;
926
927 skb = netdev_alloc_skb_ip_align(adpt->netdev, adpt->rxbuf_size);
928 if (!skb)
929 break;
930
931 curr_rxbuf->dma_addr =
932 dma_map_single(adpt->netdev->dev.parent, skb->data,
933 curr_rxbuf->length, DMA_FROM_DEVICE);
934 ret = dma_mapping_error(adpt->netdev->dev.parent,
935 curr_rxbuf->dma_addr);
936 if (ret) {
937 dev_kfree_skb(skb);
938 break;
939 }
940 curr_rxbuf->skb = skb;
941 curr_rxbuf->length = adpt->rxbuf_size;
942
943 emac_mac_rx_rfd_create(adpt, rx_q, curr_rxbuf->dma_addr);
944 next_produce_idx = rx_q->rfd.produce_idx + 1;
945 if (next_produce_idx == rx_q->rfd.count)
946 next_produce_idx = 0;
947
948 curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
949 next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
950 count++;
951 }
952
953 if (count) {
954 u32 prod_idx = (rx_q->rfd.produce_idx << rx_q->produce_shift) &
955 rx_q->produce_mask;
956 emac_reg_update32(adpt->base + rx_q->produce_reg,
957 rx_q->produce_mask, prod_idx);
958 }
959}
960
961static void emac_adjust_link(struct net_device *netdev)
962{
963 struct emac_adapter *adpt = netdev_priv(netdev);
964 struct phy_device *phydev = netdev->phydev;
965
966 if (phydev->link)
967 emac_mac_start(adpt);
968 else
969 emac_mac_stop(adpt);
970
971 phy_print_status(phydev);
972}
973
974/* Bringup the interface/HW */
975int emac_mac_up(struct emac_adapter *adpt)
976{
977 struct net_device *netdev = adpt->netdev;
Timur Tabib9b17de2016-08-31 18:22:08 -0500978 int ret;
979
980 emac_mac_rx_tx_ring_reset_all(adpt);
981 emac_mac_config(adpt);
Timur Tabib9b17de2016-08-31 18:22:08 -0500982 emac_mac_rx_descs_refill(adpt, &adpt->rx_q);
983
Timur Tabi9da34f22017-01-27 16:43:43 -0600984 adpt->phydev->irq = PHY_IGNORE_INTERRUPT;
Timur Tabib9b17de2016-08-31 18:22:08 -0500985 ret = phy_connect_direct(netdev, adpt->phydev, emac_adjust_link,
986 PHY_INTERFACE_MODE_SGMII);
987 if (ret) {
988 netdev_err(adpt->netdev, "could not connect phy\n");
Timur Tabib9b17de2016-08-31 18:22:08 -0500989 return ret;
990 }
991
Timur Tabi9da34f22017-01-27 16:43:43 -0600992 phy_attached_print(adpt->phydev, NULL);
993
Timur Tabib9b17de2016-08-31 18:22:08 -0500994 /* enable mac irq */
995 writel((u32)~DIS_INT, adpt->base + EMAC_INT_STATUS);
996 writel(adpt->irq.mask, adpt->base + EMAC_INT_MASK);
997
Timur Tabib9b17de2016-08-31 18:22:08 -0500998 phy_start(adpt->phydev);
999
1000 napi_enable(&adpt->rx_q.napi);
1001 netif_start_queue(netdev);
1002
1003 return 0;
1004}
1005
1006/* Bring down the interface/HW */
1007void emac_mac_down(struct emac_adapter *adpt)
1008{
1009 struct net_device *netdev = adpt->netdev;
1010
1011 netif_stop_queue(netdev);
1012 napi_disable(&adpt->rx_q.napi);
1013
1014 phy_stop(adpt->phydev);
Timur Tabib9b17de2016-08-31 18:22:08 -05001015
Timur Tabi93966b72016-10-14 14:14:35 -05001016 /* Interrupts must be disabled before the PHY is disconnected, to
1017 * avoid a race condition where adjust_link is null when we get
1018 * an interrupt.
1019 */
Timur Tabib9b17de2016-08-31 18:22:08 -05001020 writel(DIS_INT, adpt->base + EMAC_INT_STATUS);
1021 writel(0, adpt->base + EMAC_INT_MASK);
1022 synchronize_irq(adpt->irq.irq);
Timur Tabib9b17de2016-08-31 18:22:08 -05001023
Timur Tabi93966b72016-10-14 14:14:35 -05001024 phy_disconnect(adpt->phydev);
1025
Timur Tabib9b17de2016-08-31 18:22:08 -05001026 emac_mac_reset(adpt);
1027
1028 emac_tx_q_descs_free(adpt);
1029 netdev_reset_queue(adpt->netdev);
1030 emac_rx_q_free_descs(adpt);
1031}
1032
1033/* Consume next received packet descriptor */
1034static bool emac_rx_process_rrd(struct emac_adapter *adpt,
1035 struct emac_rx_queue *rx_q,
1036 struct emac_rrd *rrd)
1037{
1038 u32 *hw_rrd = EMAC_RRD(rx_q, adpt->rrd_size, rx_q->rrd.consume_idx);
1039
1040 rrd->word[3] = *(hw_rrd + 3);
1041
1042 if (!RRD_UPDT(rrd))
1043 return false;
1044
1045 rrd->word[4] = 0;
1046 rrd->word[5] = 0;
1047
1048 rrd->word[0] = *(hw_rrd++);
1049 rrd->word[1] = *(hw_rrd++);
1050 rrd->word[2] = *(hw_rrd++);
1051
1052 if (unlikely(RRD_NOR(rrd) != 1)) {
1053 netdev_err(adpt->netdev,
1054 "error: multi-RFD not support yet! nor:%lu\n",
1055 RRD_NOR(rrd));
1056 }
1057
1058 /* mark rrd as processed */
1059 RRD_UPDT_SET(rrd, 0);
1060 *hw_rrd = rrd->word[3];
1061
1062 if (++rx_q->rrd.consume_idx == rx_q->rrd.count)
1063 rx_q->rrd.consume_idx = 0;
1064
1065 return true;
1066}
1067
1068/* Produce new transmit descriptor */
1069static void emac_tx_tpd_create(struct emac_adapter *adpt,
1070 struct emac_tx_queue *tx_q, struct emac_tpd *tpd)
1071{
1072 u32 *hw_tpd;
1073
1074 tx_q->tpd.last_produce_idx = tx_q->tpd.produce_idx;
1075 hw_tpd = EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.produce_idx);
1076
1077 if (++tx_q->tpd.produce_idx == tx_q->tpd.count)
1078 tx_q->tpd.produce_idx = 0;
1079
1080 *(hw_tpd++) = tpd->word[0];
1081 *(hw_tpd++) = tpd->word[1];
1082 *(hw_tpd++) = tpd->word[2];
1083 *hw_tpd = tpd->word[3];
1084}
1085
1086/* Mark the last transmit descriptor as such (for the transmit packet) */
1087static void emac_tx_tpd_mark_last(struct emac_adapter *adpt,
1088 struct emac_tx_queue *tx_q)
1089{
1090 u32 *hw_tpd =
1091 EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.last_produce_idx);
1092 u32 tmp_tpd;
1093
1094 tmp_tpd = *(hw_tpd + 1);
1095 tmp_tpd |= EMAC_TPD_LAST_FRAGMENT;
1096 *(hw_tpd + 1) = tmp_tpd;
1097}
1098
1099static void emac_rx_rfd_clean(struct emac_rx_queue *rx_q, struct emac_rrd *rrd)
1100{
1101 struct emac_buffer *rfbuf = rx_q->rfd.rfbuff;
1102 u32 consume_idx = RRD_SI(rrd);
1103 unsigned int i;
1104
1105 for (i = 0; i < RRD_NOR(rrd); i++) {
1106 rfbuf[consume_idx].skb = NULL;
1107 if (++consume_idx == rx_q->rfd.count)
1108 consume_idx = 0;
1109 }
1110
1111 rx_q->rfd.consume_idx = consume_idx;
1112 rx_q->rfd.process_idx = consume_idx;
1113}
1114
1115/* Push the received skb to upper layers */
1116static void emac_receive_skb(struct emac_rx_queue *rx_q,
1117 struct sk_buff *skb,
1118 u16 vlan_tag, bool vlan_flag)
1119{
1120 if (vlan_flag) {
1121 u16 vlan;
1122
1123 EMAC_TAG_TO_VLAN(vlan_tag, vlan);
1124 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
1125 }
1126
1127 napi_gro_receive(&rx_q->napi, skb);
1128}
1129
1130/* Process receive event */
1131void emac_mac_rx_process(struct emac_adapter *adpt, struct emac_rx_queue *rx_q,
1132 int *num_pkts, int max_pkts)
1133{
1134 u32 proc_idx, hw_consume_idx, num_consume_pkts;
1135 struct net_device *netdev = adpt->netdev;
1136 struct emac_buffer *rfbuf;
1137 unsigned int count = 0;
1138 struct emac_rrd rrd;
1139 struct sk_buff *skb;
1140 u32 reg;
1141
1142 reg = readl_relaxed(adpt->base + rx_q->consume_reg);
1143
1144 hw_consume_idx = (reg & rx_q->consume_mask) >> rx_q->consume_shift;
1145 num_consume_pkts = (hw_consume_idx >= rx_q->rrd.consume_idx) ?
1146 (hw_consume_idx - rx_q->rrd.consume_idx) :
1147 (hw_consume_idx + rx_q->rrd.count - rx_q->rrd.consume_idx);
1148
1149 do {
1150 if (!num_consume_pkts)
1151 break;
1152
1153 if (!emac_rx_process_rrd(adpt, rx_q, &rrd))
1154 break;
1155
1156 if (likely(RRD_NOR(&rrd) == 1)) {
1157 /* good receive */
1158 rfbuf = GET_RFD_BUFFER(rx_q, RRD_SI(&rrd));
1159 dma_unmap_single(adpt->netdev->dev.parent,
1160 rfbuf->dma_addr, rfbuf->length,
1161 DMA_FROM_DEVICE);
1162 rfbuf->dma_addr = 0;
1163 skb = rfbuf->skb;
1164 } else {
1165 netdev_err(adpt->netdev,
1166 "error: multi-RFD not support yet!\n");
1167 break;
1168 }
1169 emac_rx_rfd_clean(rx_q, &rrd);
1170 num_consume_pkts--;
1171 count++;
1172
1173 /* Due to a HW issue in L4 check sum detection (UDP/TCP frags
1174 * with DF set are marked as error), drop packets based on the
1175 * error mask rather than the summary bit (ignoring L4F errors)
1176 */
1177 if (rrd.word[EMAC_RRD_STATS_DW_IDX] & EMAC_RRD_ERROR) {
1178 netif_dbg(adpt, rx_status, adpt->netdev,
1179 "Drop error packet[RRD: 0x%x:0x%x:0x%x:0x%x]\n",
1180 rrd.word[0], rrd.word[1],
1181 rrd.word[2], rrd.word[3]);
1182
1183 dev_kfree_skb(skb);
1184 continue;
1185 }
1186
1187 skb_put(skb, RRD_PKT_SIZE(&rrd) - ETH_FCS_LEN);
1188 skb->dev = netdev;
1189 skb->protocol = eth_type_trans(skb, skb->dev);
1190 if (netdev->features & NETIF_F_RXCSUM)
1191 skb->ip_summed = RRD_L4F(&rrd) ?
1192 CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
1193 else
1194 skb_checksum_none_assert(skb);
1195
1196 emac_receive_skb(rx_q, skb, (u16)RRD_CVALN_TAG(&rrd),
1197 (bool)RRD_CVTAG(&rrd));
1198
Timur Tabib9b17de2016-08-31 18:22:08 -05001199 (*num_pkts)++;
1200 } while (*num_pkts < max_pkts);
1201
1202 if (count) {
1203 proc_idx = (rx_q->rfd.process_idx << rx_q->process_shft) &
1204 rx_q->process_mask;
1205 emac_reg_update32(adpt->base + rx_q->process_reg,
1206 rx_q->process_mask, proc_idx);
1207 emac_mac_rx_descs_refill(adpt, rx_q);
1208 }
1209}
1210
1211/* get the number of free transmit descriptors */
1212static unsigned int emac_tpd_num_free_descs(struct emac_tx_queue *tx_q)
1213{
1214 u32 produce_idx = tx_q->tpd.produce_idx;
1215 u32 consume_idx = tx_q->tpd.consume_idx;
1216
1217 return (consume_idx > produce_idx) ?
1218 (consume_idx - produce_idx - 1) :
1219 (tx_q->tpd.count + consume_idx - produce_idx - 1);
1220}
1221
1222/* Process transmit event */
1223void emac_mac_tx_process(struct emac_adapter *adpt, struct emac_tx_queue *tx_q)
1224{
1225 u32 reg = readl_relaxed(adpt->base + tx_q->consume_reg);
1226 u32 hw_consume_idx, pkts_compl = 0, bytes_compl = 0;
1227 struct emac_buffer *tpbuf;
1228
1229 hw_consume_idx = (reg & tx_q->consume_mask) >> tx_q->consume_shift;
1230
1231 while (tx_q->tpd.consume_idx != hw_consume_idx) {
1232 tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.consume_idx);
1233 if (tpbuf->dma_addr) {
1234 dma_unmap_single(adpt->netdev->dev.parent,
1235 tpbuf->dma_addr, tpbuf->length,
1236 DMA_TO_DEVICE);
1237 tpbuf->dma_addr = 0;
1238 }
1239
1240 if (tpbuf->skb) {
1241 pkts_compl++;
1242 bytes_compl += tpbuf->skb->len;
1243 dev_kfree_skb_irq(tpbuf->skb);
1244 tpbuf->skb = NULL;
1245 }
1246
1247 if (++tx_q->tpd.consume_idx == tx_q->tpd.count)
1248 tx_q->tpd.consume_idx = 0;
1249 }
1250
1251 netdev_completed_queue(adpt->netdev, pkts_compl, bytes_compl);
1252
1253 if (netif_queue_stopped(adpt->netdev))
1254 if (emac_tpd_num_free_descs(tx_q) > (MAX_SKB_FRAGS + 1))
1255 netif_wake_queue(adpt->netdev);
1256}
1257
1258/* Initialize all queue data structures */
1259void emac_mac_rx_tx_ring_init_all(struct platform_device *pdev,
1260 struct emac_adapter *adpt)
1261{
1262 adpt->rx_q.netdev = adpt->netdev;
1263
1264 adpt->rx_q.produce_reg = EMAC_MAILBOX_0;
1265 adpt->rx_q.produce_mask = RFD0_PROD_IDX_BMSK;
1266 adpt->rx_q.produce_shift = RFD0_PROD_IDX_SHFT;
1267
1268 adpt->rx_q.process_reg = EMAC_MAILBOX_0;
1269 adpt->rx_q.process_mask = RFD0_PROC_IDX_BMSK;
1270 adpt->rx_q.process_shft = RFD0_PROC_IDX_SHFT;
1271
1272 adpt->rx_q.consume_reg = EMAC_MAILBOX_3;
1273 adpt->rx_q.consume_mask = RFD0_CONS_IDX_BMSK;
1274 adpt->rx_q.consume_shift = RFD0_CONS_IDX_SHFT;
1275
1276 adpt->rx_q.irq = &adpt->irq;
1277 adpt->rx_q.intr = adpt->irq.mask & ISR_RX_PKT;
1278
1279 adpt->tx_q.produce_reg = EMAC_MAILBOX_15;
1280 adpt->tx_q.produce_mask = NTPD_PROD_IDX_BMSK;
1281 adpt->tx_q.produce_shift = NTPD_PROD_IDX_SHFT;
1282
1283 adpt->tx_q.consume_reg = EMAC_MAILBOX_2;
1284 adpt->tx_q.consume_mask = NTPD_CONS_IDX_BMSK;
1285 adpt->tx_q.consume_shift = NTPD_CONS_IDX_SHFT;
1286}
1287
1288/* Fill up transmit descriptors with TSO and Checksum offload information */
1289static int emac_tso_csum(struct emac_adapter *adpt,
1290 struct emac_tx_queue *tx_q,
1291 struct sk_buff *skb,
1292 struct emac_tpd *tpd)
1293{
1294 unsigned int hdr_len;
1295 int ret;
1296
1297 if (skb_is_gso(skb)) {
1298 if (skb_header_cloned(skb)) {
1299 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1300 if (unlikely(ret))
1301 return ret;
1302 }
1303
1304 if (skb->protocol == htons(ETH_P_IP)) {
1305 u32 pkt_len = ((unsigned char *)ip_hdr(skb) - skb->data)
1306 + ntohs(ip_hdr(skb)->tot_len);
1307 if (skb->len > pkt_len)
1308 pskb_trim(skb, pkt_len);
1309 }
1310
1311 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1312 if (unlikely(skb->len == hdr_len)) {
1313 /* we only need to do csum */
1314 netif_warn(adpt, tx_err, adpt->netdev,
1315 "tso not needed for packet with 0 data\n");
1316 goto do_csum;
1317 }
1318
1319 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1320 ip_hdr(skb)->check = 0;
1321 tcp_hdr(skb)->check =
1322 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1323 ip_hdr(skb)->daddr,
1324 0, IPPROTO_TCP, 0);
1325 TPD_IPV4_SET(tpd, 1);
1326 }
1327
1328 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
1329 /* ipv6 tso need an extra tpd */
1330 struct emac_tpd extra_tpd;
1331
1332 memset(tpd, 0, sizeof(*tpd));
1333 memset(&extra_tpd, 0, sizeof(extra_tpd));
1334
1335 ipv6_hdr(skb)->payload_len = 0;
1336 tcp_hdr(skb)->check =
1337 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1338 &ipv6_hdr(skb)->daddr,
1339 0, IPPROTO_TCP, 0);
1340 TPD_PKT_LEN_SET(&extra_tpd, skb->len);
1341 TPD_LSO_SET(&extra_tpd, 1);
1342 TPD_LSOV_SET(&extra_tpd, 1);
1343 emac_tx_tpd_create(adpt, tx_q, &extra_tpd);
1344 TPD_LSOV_SET(tpd, 1);
1345 }
1346
1347 TPD_LSO_SET(tpd, 1);
1348 TPD_TCPHDR_OFFSET_SET(tpd, skb_transport_offset(skb));
1349 TPD_MSS_SET(tpd, skb_shinfo(skb)->gso_size);
1350 return 0;
1351 }
1352
1353do_csum:
1354 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1355 unsigned int css, cso;
1356
1357 cso = skb_transport_offset(skb);
1358 if (unlikely(cso & 0x1)) {
1359 netdev_err(adpt->netdev,
1360 "error: payload offset should be even\n");
1361 return -EINVAL;
1362 }
1363 css = cso + skb->csum_offset;
1364
1365 TPD_PAYLOAD_OFFSET_SET(tpd, cso >> 1);
1366 TPD_CXSUM_OFFSET_SET(tpd, css >> 1);
1367 TPD_CSX_SET(tpd, 1);
1368 }
1369
1370 return 0;
1371}
1372
1373/* Fill up transmit descriptors */
1374static void emac_tx_fill_tpd(struct emac_adapter *adpt,
1375 struct emac_tx_queue *tx_q, struct sk_buff *skb,
1376 struct emac_tpd *tpd)
1377{
1378 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
1379 unsigned int first = tx_q->tpd.produce_idx;
1380 unsigned int len = skb_headlen(skb);
1381 struct emac_buffer *tpbuf = NULL;
1382 unsigned int mapped_len = 0;
1383 unsigned int i;
1384 int count = 0;
1385 int ret;
1386
1387 /* if Large Segment Offload is (in TCP Segmentation Offload struct) */
1388 if (TPD_LSO(tpd)) {
1389 mapped_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1390
1391 tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
1392 tpbuf->length = mapped_len;
1393 tpbuf->dma_addr = dma_map_single(adpt->netdev->dev.parent,
1394 skb->data, tpbuf->length,
1395 DMA_TO_DEVICE);
1396 ret = dma_mapping_error(adpt->netdev->dev.parent,
1397 tpbuf->dma_addr);
1398 if (ret)
1399 goto error;
1400
1401 TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
1402 TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
1403 TPD_BUF_LEN_SET(tpd, tpbuf->length);
1404 emac_tx_tpd_create(adpt, tx_q, tpd);
1405 count++;
1406 }
1407
1408 if (mapped_len < len) {
1409 tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
1410 tpbuf->length = len - mapped_len;
1411 tpbuf->dma_addr = dma_map_single(adpt->netdev->dev.parent,
1412 skb->data + mapped_len,
1413 tpbuf->length, DMA_TO_DEVICE);
1414 ret = dma_mapping_error(adpt->netdev->dev.parent,
1415 tpbuf->dma_addr);
1416 if (ret)
1417 goto error;
1418
1419 TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
1420 TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
1421 TPD_BUF_LEN_SET(tpd, tpbuf->length);
1422 emac_tx_tpd_create(adpt, tx_q, tpd);
1423 count++;
1424 }
1425
1426 for (i = 0; i < nr_frags; i++) {
1427 struct skb_frag_struct *frag;
1428
1429 frag = &skb_shinfo(skb)->frags[i];
1430
1431 tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
1432 tpbuf->length = frag->size;
1433 tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
1434 frag->page.p, frag->page_offset,
1435 tpbuf->length, DMA_TO_DEVICE);
1436 ret = dma_mapping_error(adpt->netdev->dev.parent,
1437 tpbuf->dma_addr);
1438 if (ret)
1439 goto error;
1440
1441 TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
1442 TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
1443 TPD_BUF_LEN_SET(tpd, tpbuf->length);
1444 emac_tx_tpd_create(adpt, tx_q, tpd);
1445 count++;
1446 }
1447
1448 /* The last tpd */
1449 wmb();
1450 emac_tx_tpd_mark_last(adpt, tx_q);
1451
1452 /* The last buffer info contain the skb address,
1453 * so it will be freed after unmap
1454 */
1455 tpbuf->skb = skb;
1456
1457 return;
1458
1459error:
1460 /* One of the memory mappings failed, so undo everything */
1461 tx_q->tpd.produce_idx = first;
1462
1463 while (count--) {
1464 tpbuf = GET_TPD_BUFFER(tx_q, first);
1465 dma_unmap_page(adpt->netdev->dev.parent, tpbuf->dma_addr,
1466 tpbuf->length, DMA_TO_DEVICE);
1467 tpbuf->dma_addr = 0;
1468 tpbuf->length = 0;
1469
1470 if (++first == tx_q->tpd.count)
1471 first = 0;
1472 }
1473
1474 dev_kfree_skb(skb);
1475}
1476
1477/* Transmit the packet using specified transmit queue */
1478int emac_mac_tx_buf_send(struct emac_adapter *adpt, struct emac_tx_queue *tx_q,
1479 struct sk_buff *skb)
1480{
1481 struct emac_tpd tpd;
1482 u32 prod_idx;
1483
1484 memset(&tpd, 0, sizeof(tpd));
1485
1486 if (emac_tso_csum(adpt, tx_q, skb, &tpd) != 0) {
1487 dev_kfree_skb_any(skb);
1488 return NETDEV_TX_OK;
1489 }
1490
1491 if (skb_vlan_tag_present(skb)) {
1492 u16 tag;
1493
1494 EMAC_VLAN_TO_TAG(skb_vlan_tag_get(skb), tag);
1495 TPD_CVLAN_TAG_SET(&tpd, tag);
1496 TPD_INSTC_SET(&tpd, 1);
1497 }
1498
1499 if (skb_network_offset(skb) != ETH_HLEN)
1500 TPD_TYP_SET(&tpd, 1);
1501
1502 emac_tx_fill_tpd(adpt, tx_q, skb, &tpd);
1503
1504 netdev_sent_queue(adpt->netdev, skb->len);
1505
1506 /* Make sure the are enough free descriptors to hold one
1507 * maximum-sized SKB. We need one desc for each fragment,
1508 * one for the checksum (emac_tso_csum), one for TSO, and
1509 * and one for the SKB header.
1510 */
1511 if (emac_tpd_num_free_descs(tx_q) < (MAX_SKB_FRAGS + 3))
1512 netif_stop_queue(adpt->netdev);
1513
1514 /* update produce idx */
1515 prod_idx = (tx_q->tpd.produce_idx << tx_q->produce_shift) &
1516 tx_q->produce_mask;
1517 emac_reg_update32(adpt->base + tx_q->produce_reg,
1518 tx_q->produce_mask, prod_idx);
1519
1520 return NETDEV_TX_OK;
1521}