GNU Linux-libre 4.9.333-gnu1
[releases.git] / drivers / net / ethernet / qualcomm / emac / emac-mac.c
1 /* 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
273 struct 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
317 #define EMAC_MAC_IRQ_RES                                        "core0"
318
319 void emac_mac_multicast_addr_set(struct emac_adapter *adpt, u8 *addr)
320 {
321         u32 crc32, bit, reg, mta;
322
323         /* Calculate the CRC of the MAC address */
324         crc32 = ether_crc(ETH_ALEN, addr);
325
326         /* The HASH Table is an array of 2 32-bit registers. It is
327          * treated like an array of 64 bits (BitArray[hash_value]).
328          * Use the upper 6 bits of the above CRC as the hash value.
329          */
330         reg = (crc32 >> 31) & 0x1;
331         bit = (crc32 >> 26) & 0x1F;
332
333         mta = readl(adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
334         mta |= BIT(bit);
335         writel(mta, adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
336 }
337
338 void emac_mac_multicast_addr_clear(struct emac_adapter *adpt)
339 {
340         writel(0, adpt->base + EMAC_HASH_TAB_REG0);
341         writel(0, adpt->base + EMAC_HASH_TAB_REG1);
342 }
343
344 /* definitions for RSS */
345 #define EMAC_RSS_KEY(_i, _type) \
346                 (EMAC_RSS_KEY0 + ((_i) * sizeof(_type)))
347 #define EMAC_RSS_TBL(_i, _type) \
348                 (EMAC_IDT_TABLE0 + ((_i) * sizeof(_type)))
349
350 /* Config MAC modes */
351 void emac_mac_mode_config(struct emac_adapter *adpt)
352 {
353         struct net_device *netdev = adpt->netdev;
354         u32 mac;
355
356         mac = readl(adpt->base + EMAC_MAC_CTRL);
357         mac &= ~(VLAN_STRIP | PROM_MODE | MULTI_ALL | MAC_LP_EN);
358
359         if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
360                 mac |= VLAN_STRIP;
361
362         if (netdev->flags & IFF_PROMISC)
363                 mac |= PROM_MODE;
364
365         if (netdev->flags & IFF_ALLMULTI)
366                 mac |= MULTI_ALL;
367
368         writel(mac, adpt->base + EMAC_MAC_CTRL);
369 }
370
371 /* Config descriptor rings */
372 static void emac_mac_dma_rings_config(struct emac_adapter *adpt)
373 {
374         static const unsigned short tpd_q_offset[] = {
375                 EMAC_DESC_CTRL_8,        EMAC_H1TPD_BASE_ADDR_LO,
376                 EMAC_H2TPD_BASE_ADDR_LO, EMAC_H3TPD_BASE_ADDR_LO};
377         static const unsigned short rfd_q_offset[] = {
378                 EMAC_DESC_CTRL_2,        EMAC_DESC_CTRL_10,
379                 EMAC_DESC_CTRL_12,       EMAC_DESC_CTRL_13};
380         static const unsigned short rrd_q_offset[] = {
381                 EMAC_DESC_CTRL_5,        EMAC_DESC_CTRL_14,
382                 EMAC_DESC_CTRL_15,       EMAC_DESC_CTRL_16};
383
384         /* TPD (Transmit Packet Descriptor) */
385         writel(upper_32_bits(adpt->tx_q.tpd.dma_addr),
386                adpt->base + EMAC_DESC_CTRL_1);
387
388         writel(lower_32_bits(adpt->tx_q.tpd.dma_addr),
389                adpt->base + tpd_q_offset[0]);
390
391         writel(adpt->tx_q.tpd.count & TPD_RING_SIZE_BMSK,
392                adpt->base + EMAC_DESC_CTRL_9);
393
394         /* RFD (Receive Free Descriptor) & RRD (Receive Return Descriptor) */
395         writel(upper_32_bits(adpt->rx_q.rfd.dma_addr),
396                adpt->base + EMAC_DESC_CTRL_0);
397
398         writel(lower_32_bits(adpt->rx_q.rfd.dma_addr),
399                adpt->base + rfd_q_offset[0]);
400         writel(lower_32_bits(adpt->rx_q.rrd.dma_addr),
401                adpt->base + rrd_q_offset[0]);
402
403         writel(adpt->rx_q.rfd.count & RFD_RING_SIZE_BMSK,
404                adpt->base + EMAC_DESC_CTRL_3);
405         writel(adpt->rx_q.rrd.count & RRD_RING_SIZE_BMSK,
406                adpt->base + EMAC_DESC_CTRL_6);
407
408         writel(adpt->rxbuf_size & RX_BUFFER_SIZE_BMSK,
409                adpt->base + EMAC_DESC_CTRL_4);
410
411         writel(0, adpt->base + EMAC_DESC_CTRL_11);
412
413         /* Load all of the base addresses above and ensure that triggering HW to
414          * read ring pointers is flushed
415          */
416         writel(1, adpt->base + EMAC_INTER_SRAM_PART9);
417 }
418
419 /* Config transmit parameters */
420 static void emac_mac_tx_config(struct emac_adapter *adpt)
421 {
422         u32 val;
423
424         writel((EMAC_MAX_TX_OFFLOAD_THRESH >> 3) &
425                JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK, adpt->base + EMAC_TXQ_CTRL_1);
426
427         val = (adpt->tpd_burst << NUM_TPD_BURST_PREF_SHFT) &
428                NUM_TPD_BURST_PREF_BMSK;
429
430         val |= TXQ_MODE | LS_8023_SP;
431         val |= (0x0100 << NUM_TXF_BURST_PREF_SHFT) &
432                 NUM_TXF_BURST_PREF_BMSK;
433
434         writel(val, adpt->base + EMAC_TXQ_CTRL_0);
435         emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_2,
436                           (TXF_HWM_BMSK | TXF_LWM_BMSK), 0);
437 }
438
439 /* Config receive parameters */
440 static void emac_mac_rx_config(struct emac_adapter *adpt)
441 {
442         u32 val;
443
444         val = (adpt->rfd_burst << NUM_RFD_BURST_PREF_SHFT) &
445                NUM_RFD_BURST_PREF_BMSK;
446         val |= (SP_IPV6 | CUT_THRU_EN);
447
448         writel(val, adpt->base + EMAC_RXQ_CTRL_0);
449
450         val = readl(adpt->base + EMAC_RXQ_CTRL_1);
451         val &= ~(JUMBO_1KAH_BMSK | RFD_PREF_LOW_THRESHOLD_BMSK |
452                  RFD_PREF_UP_THRESHOLD_BMSK);
453         val |= (JUMBO_1KAH << JUMBO_1KAH_SHFT) |
454                 (RFD_PREF_LOW_TH << RFD_PREF_LOW_THRESHOLD_SHFT) |
455                 (RFD_PREF_UP_TH  << RFD_PREF_UP_THRESHOLD_SHFT);
456         writel(val, adpt->base + EMAC_RXQ_CTRL_1);
457
458         val = readl(adpt->base + EMAC_RXQ_CTRL_2);
459         val &= ~(RXF_DOF_THRESHOLD_BMSK | RXF_UOF_THRESHOLD_BMSK);
460         val |= (RXF_DOF_THRESFHOLD  << RXF_DOF_THRESHOLD_SHFT) |
461                 (RXF_UOF_THRESFHOLD << RXF_UOF_THRESHOLD_SHFT);
462         writel(val, adpt->base + EMAC_RXQ_CTRL_2);
463
464         val = readl(adpt->base + EMAC_RXQ_CTRL_3);
465         val &= ~(RXD_TIMER_BMSK | RXD_THRESHOLD_BMSK);
466         val |= RXD_TH << RXD_THRESHOLD_SHFT;
467         writel(val, adpt->base + EMAC_RXQ_CTRL_3);
468 }
469
470 /* Config dma */
471 static void emac_mac_dma_config(struct emac_adapter *adpt)
472 {
473         u32 dma_ctrl = DMAR_REQ_PRI;
474
475         switch (adpt->dma_order) {
476         case emac_dma_ord_in:
477                 dma_ctrl |= IN_ORDER_MODE;
478                 break;
479         case emac_dma_ord_enh:
480                 dma_ctrl |= ENH_ORDER_MODE;
481                 break;
482         case emac_dma_ord_out:
483                 dma_ctrl |= OUT_ORDER_MODE;
484                 break;
485         default:
486                 break;
487         }
488
489         dma_ctrl |= (((u32)adpt->dmar_block) << REGRDBLEN_SHFT) &
490                                                 REGRDBLEN_BMSK;
491         dma_ctrl |= (((u32)adpt->dmaw_block) << REGWRBLEN_SHFT) &
492                                                 REGWRBLEN_BMSK;
493         dma_ctrl |= (((u32)adpt->dmar_dly_cnt) << DMAR_DLY_CNT_SHFT) &
494                                                 DMAR_DLY_CNT_BMSK;
495         dma_ctrl |= (((u32)adpt->dmaw_dly_cnt) << DMAW_DLY_CNT_SHFT) &
496                                                 DMAW_DLY_CNT_BMSK;
497
498         /* config DMA and ensure that configuration is flushed to HW */
499         writel(dma_ctrl, adpt->base + EMAC_DMA_CTRL);
500 }
501
502 /* set MAC address */
503 static void emac_set_mac_address(struct emac_adapter *adpt, u8 *addr)
504 {
505         u32 sta;
506
507         /* for example: 00-A0-C6-11-22-33
508          * 0<-->C6112233, 1<-->00A0.
509          */
510
511         /* low 32bit word */
512         sta = (((u32)addr[2]) << 24) | (((u32)addr[3]) << 16) |
513               (((u32)addr[4]) << 8)  | (((u32)addr[5]));
514         writel(sta, adpt->base + EMAC_MAC_STA_ADDR0);
515
516         /* hight 32bit word */
517         sta = (((u32)addr[0]) << 8) | (u32)addr[1];
518         writel(sta, adpt->base + EMAC_MAC_STA_ADDR1);
519 }
520
521 static void emac_mac_config(struct emac_adapter *adpt)
522 {
523         struct net_device *netdev = adpt->netdev;
524         unsigned int max_frame;
525         u32 val;
526
527         emac_set_mac_address(adpt, netdev->dev_addr);
528
529         max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
530         adpt->rxbuf_size = netdev->mtu > EMAC_DEF_RX_BUF_SIZE ?
531                 ALIGN(max_frame, 8) : EMAC_DEF_RX_BUF_SIZE;
532
533         emac_mac_dma_rings_config(adpt);
534
535         writel(netdev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
536                adpt->base + EMAC_MAX_FRAM_LEN_CTRL);
537
538         emac_mac_tx_config(adpt);
539         emac_mac_rx_config(adpt);
540         emac_mac_dma_config(adpt);
541
542         val = readl(adpt->base + EMAC_AXI_MAST_CTRL);
543         val &= ~(DATA_BYTE_SWAP | MAX_BOUND);
544         val |= MAX_BTYPE;
545         writel(val, adpt->base + EMAC_AXI_MAST_CTRL);
546         writel(0, adpt->base + EMAC_CLK_GATE_CTRL);
547         writel(RX_UNCPL_INT_EN, adpt->base + EMAC_MISC_CTRL);
548 }
549
550 void emac_mac_reset(struct emac_adapter *adpt)
551 {
552         emac_mac_stop(adpt);
553
554         emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, SOFT_RST);
555         usleep_range(100, 150); /* reset may take up to 100usec */
556
557         /* interrupt clear-on-read */
558         emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, INT_RD_CLR_EN);
559 }
560
561 void emac_mac_start(struct emac_adapter *adpt)
562 {
563         struct phy_device *phydev = adpt->phydev;
564         u32 mac, csr1;
565
566         /* enable tx queue */
567         emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, 0, TXQ_EN);
568
569         /* enable rx queue */
570         emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, 0, RXQ_EN);
571
572         /* enable mac control */
573         mac = readl(adpt->base + EMAC_MAC_CTRL);
574         csr1 = readl(adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
575
576         mac |= TXEN | RXEN;     /* enable RX/TX */
577
578         /* Configure MAC flow control to match the PHY's settings. */
579         if (phydev->pause)
580                 mac |= RXFC;
581         if (phydev->pause != phydev->asym_pause)
582                 mac |= TXFC;
583
584         /* setup link speed */
585         mac &= ~SPEED_MASK;
586         if (phydev->speed == SPEED_1000) {
587                 mac |= SPEED(2);
588                 csr1 |= FREQ_MODE;
589         } else {
590                 mac |= SPEED(1);
591                 csr1 &= ~FREQ_MODE;
592         }
593
594         if (phydev->duplex == DUPLEX_FULL)
595                 mac |= FULLD;
596         else
597                 mac &= ~FULLD;
598
599         /* other parameters */
600         mac |= (CRCE | PCRCE);
601         mac |= ((adpt->preamble << PRLEN_SHFT) & PRLEN_BMSK);
602         mac |= BROAD_EN;
603         mac |= FLCHK;
604         mac &= ~RX_CHKSUM_EN;
605         mac &= ~(HUGEN | VLAN_STRIP | TPAUSE | SIMR | HUGE | MULTI_ALL |
606                  DEBUG_MODE | SINGLE_PAUSE_MODE);
607
608         writel_relaxed(csr1, adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
609
610         writel_relaxed(mac, adpt->base + EMAC_MAC_CTRL);
611
612         /* enable interrupt read clear, low power sleep mode and
613          * the irq moderators
614          */
615
616         writel_relaxed(adpt->irq_mod, adpt->base + EMAC_IRQ_MOD_TIM_INIT);
617         writel_relaxed(INT_RD_CLR_EN | LPW_MODE | IRQ_MODERATOR_EN |
618                         IRQ_MODERATOR2_EN, adpt->base + EMAC_DMA_MAS_CTRL);
619
620         emac_mac_mode_config(adpt);
621
622         emac_reg_update32(adpt->base + EMAC_ATHR_HEADER_CTRL,
623                           (HEADER_ENABLE | HEADER_CNT_EN), 0);
624
625         emac_reg_update32(adpt->csr + EMAC_EMAC_WRAPPER_CSR2, 0, WOL_EN);
626 }
627
628 void emac_mac_stop(struct emac_adapter *adpt)
629 {
630         emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, RXQ_EN, 0);
631         emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, TXQ_EN, 0);
632         emac_reg_update32(adpt->base + EMAC_MAC_CTRL, TXEN | RXEN, 0);
633         usleep_range(1000, 1050); /* stopping mac may take upto 1msec */
634 }
635
636 /* Free all descriptors of given transmit queue */
637 static void emac_tx_q_descs_free(struct emac_adapter *adpt)
638 {
639         struct emac_tx_queue *tx_q = &adpt->tx_q;
640         unsigned int i;
641         size_t size;
642
643         /* ring already cleared, nothing to do */
644         if (!tx_q->tpd.tpbuff)
645                 return;
646
647         for (i = 0; i < tx_q->tpd.count; i++) {
648                 struct emac_buffer *tpbuf = GET_TPD_BUFFER(tx_q, i);
649
650                 if (tpbuf->dma_addr) {
651                         dma_unmap_single(adpt->netdev->dev.parent,
652                                          tpbuf->dma_addr, tpbuf->length,
653                                          DMA_TO_DEVICE);
654                         tpbuf->dma_addr = 0;
655                 }
656                 if (tpbuf->skb) {
657                         dev_kfree_skb_any(tpbuf->skb);
658                         tpbuf->skb = NULL;
659                 }
660         }
661
662         size = sizeof(struct emac_buffer) * tx_q->tpd.count;
663         memset(tx_q->tpd.tpbuff, 0, size);
664
665         /* clear the descriptor ring */
666         memset(tx_q->tpd.v_addr, 0, tx_q->tpd.size);
667
668         tx_q->tpd.consume_idx = 0;
669         tx_q->tpd.produce_idx = 0;
670 }
671
672 /* Free all descriptors of given receive queue */
673 static void emac_rx_q_free_descs(struct emac_adapter *adpt)
674 {
675         struct device *dev = adpt->netdev->dev.parent;
676         struct emac_rx_queue *rx_q = &adpt->rx_q;
677         unsigned int i;
678         size_t size;
679
680         /* ring already cleared, nothing to do */
681         if (!rx_q->rfd.rfbuff)
682                 return;
683
684         for (i = 0; i < rx_q->rfd.count; i++) {
685                 struct emac_buffer *rfbuf = GET_RFD_BUFFER(rx_q, i);
686
687                 if (rfbuf->dma_addr) {
688                         dma_unmap_single(dev, rfbuf->dma_addr, rfbuf->length,
689                                          DMA_FROM_DEVICE);
690                         rfbuf->dma_addr = 0;
691                 }
692                 if (rfbuf->skb) {
693                         dev_kfree_skb(rfbuf->skb);
694                         rfbuf->skb = NULL;
695                 }
696         }
697
698         size =  sizeof(struct emac_buffer) * rx_q->rfd.count;
699         memset(rx_q->rfd.rfbuff, 0, size);
700
701         /* clear the descriptor rings */
702         memset(rx_q->rrd.v_addr, 0, rx_q->rrd.size);
703         rx_q->rrd.produce_idx = 0;
704         rx_q->rrd.consume_idx = 0;
705
706         memset(rx_q->rfd.v_addr, 0, rx_q->rfd.size);
707         rx_q->rfd.produce_idx = 0;
708         rx_q->rfd.consume_idx = 0;
709 }
710
711 /* Free all buffers associated with given transmit queue */
712 static void emac_tx_q_bufs_free(struct emac_adapter *adpt)
713 {
714         struct emac_tx_queue *tx_q = &adpt->tx_q;
715
716         emac_tx_q_descs_free(adpt);
717
718         kfree(tx_q->tpd.tpbuff);
719         tx_q->tpd.tpbuff = NULL;
720         tx_q->tpd.v_addr = NULL;
721         tx_q->tpd.dma_addr = 0;
722         tx_q->tpd.size = 0;
723 }
724
725 /* Allocate TX descriptor ring for the given transmit queue */
726 static int emac_tx_q_desc_alloc(struct emac_adapter *adpt,
727                                 struct emac_tx_queue *tx_q)
728 {
729         struct emac_ring_header *ring_header = &adpt->ring_header;
730         size_t size;
731
732         size = sizeof(struct emac_buffer) * tx_q->tpd.count;
733         tx_q->tpd.tpbuff = kzalloc(size, GFP_KERNEL);
734         if (!tx_q->tpd.tpbuff)
735                 return -ENOMEM;
736
737         tx_q->tpd.size = tx_q->tpd.count * (adpt->tpd_size * 4);
738         tx_q->tpd.dma_addr = ring_header->dma_addr + ring_header->used;
739         tx_q->tpd.v_addr = ring_header->v_addr + ring_header->used;
740         ring_header->used += ALIGN(tx_q->tpd.size, 8);
741         tx_q->tpd.produce_idx = 0;
742         tx_q->tpd.consume_idx = 0;
743
744         return 0;
745 }
746
747 /* Free all buffers associated with given transmit queue */
748 static void emac_rx_q_bufs_free(struct emac_adapter *adpt)
749 {
750         struct emac_rx_queue *rx_q = &adpt->rx_q;
751
752         emac_rx_q_free_descs(adpt);
753
754         kfree(rx_q->rfd.rfbuff);
755         rx_q->rfd.rfbuff   = NULL;
756
757         rx_q->rfd.v_addr   = NULL;
758         rx_q->rfd.dma_addr = 0;
759         rx_q->rfd.size     = 0;
760
761         rx_q->rrd.v_addr   = NULL;
762         rx_q->rrd.dma_addr = 0;
763         rx_q->rrd.size     = 0;
764 }
765
766 /* Allocate RX descriptor rings for the given receive queue */
767 static int emac_rx_descs_alloc(struct emac_adapter *adpt)
768 {
769         struct emac_ring_header *ring_header = &adpt->ring_header;
770         struct emac_rx_queue *rx_q = &adpt->rx_q;
771         size_t size;
772
773         size = sizeof(struct emac_buffer) * rx_q->rfd.count;
774         rx_q->rfd.rfbuff = kzalloc(size, GFP_KERNEL);
775         if (!rx_q->rfd.rfbuff)
776                 return -ENOMEM;
777
778         rx_q->rrd.size = rx_q->rrd.count * (adpt->rrd_size * 4);
779         rx_q->rfd.size = rx_q->rfd.count * (adpt->rfd_size * 4);
780
781         rx_q->rrd.dma_addr = ring_header->dma_addr + ring_header->used;
782         rx_q->rrd.v_addr   = ring_header->v_addr + ring_header->used;
783         ring_header->used += ALIGN(rx_q->rrd.size, 8);
784
785         rx_q->rfd.dma_addr = ring_header->dma_addr + ring_header->used;
786         rx_q->rfd.v_addr   = ring_header->v_addr + ring_header->used;
787         ring_header->used += ALIGN(rx_q->rfd.size, 8);
788
789         rx_q->rrd.produce_idx = 0;
790         rx_q->rrd.consume_idx = 0;
791
792         rx_q->rfd.produce_idx = 0;
793         rx_q->rfd.consume_idx = 0;
794
795         return 0;
796 }
797
798 /* Allocate all TX and RX descriptor rings */
799 int emac_mac_rx_tx_rings_alloc_all(struct emac_adapter *adpt)
800 {
801         struct emac_ring_header *ring_header = &adpt->ring_header;
802         struct device *dev = adpt->netdev->dev.parent;
803         unsigned int num_tx_descs = adpt->tx_desc_cnt;
804         unsigned int num_rx_descs = adpt->rx_desc_cnt;
805         int ret;
806
807         adpt->tx_q.tpd.count = adpt->tx_desc_cnt;
808
809         adpt->rx_q.rrd.count = adpt->rx_desc_cnt;
810         adpt->rx_q.rfd.count = adpt->rx_desc_cnt;
811
812         /* Ring DMA buffer. Each ring may need up to 8 bytes for alignment,
813          * hence the additional padding bytes are allocated.
814          */
815         ring_header->size = num_tx_descs * (adpt->tpd_size * 4) +
816                             num_rx_descs * (adpt->rfd_size * 4) +
817                             num_rx_descs * (adpt->rrd_size * 4) +
818                             8 + 2 * 8; /* 8 byte per one Tx and two Rx rings */
819
820         ring_header->used = 0;
821         ring_header->v_addr = dma_zalloc_coherent(dev, ring_header->size,
822                                                  &ring_header->dma_addr,
823                                                  GFP_KERNEL);
824         if (!ring_header->v_addr)
825                 return -ENOMEM;
826
827         ring_header->used = ALIGN(ring_header->dma_addr, 8) -
828                                                         ring_header->dma_addr;
829
830         ret = emac_tx_q_desc_alloc(adpt, &adpt->tx_q);
831         if (ret) {
832                 netdev_err(adpt->netdev, "error: Tx Queue alloc failed\n");
833                 goto err_alloc_tx;
834         }
835
836         ret = emac_rx_descs_alloc(adpt);
837         if (ret) {
838                 netdev_err(adpt->netdev, "error: Rx Queue alloc failed\n");
839                 goto err_alloc_rx;
840         }
841
842         return 0;
843
844 err_alloc_rx:
845         emac_tx_q_bufs_free(adpt);
846 err_alloc_tx:
847         dma_free_coherent(dev, ring_header->size,
848                           ring_header->v_addr, ring_header->dma_addr);
849
850         ring_header->v_addr   = NULL;
851         ring_header->dma_addr = 0;
852         ring_header->size     = 0;
853         ring_header->used     = 0;
854
855         return ret;
856 }
857
858 /* Free all TX and RX descriptor rings */
859 void emac_mac_rx_tx_rings_free_all(struct emac_adapter *adpt)
860 {
861         struct emac_ring_header *ring_header = &adpt->ring_header;
862         struct device *dev = adpt->netdev->dev.parent;
863
864         emac_tx_q_bufs_free(adpt);
865         emac_rx_q_bufs_free(adpt);
866
867         dma_free_coherent(dev, ring_header->size,
868                           ring_header->v_addr, ring_header->dma_addr);
869
870         ring_header->v_addr   = NULL;
871         ring_header->dma_addr = 0;
872         ring_header->size     = 0;
873         ring_header->used     = 0;
874 }
875
876 /* Initialize descriptor rings */
877 static void emac_mac_rx_tx_ring_reset_all(struct emac_adapter *adpt)
878 {
879         unsigned int i;
880
881         adpt->tx_q.tpd.produce_idx = 0;
882         adpt->tx_q.tpd.consume_idx = 0;
883         for (i = 0; i < adpt->tx_q.tpd.count; i++)
884                 adpt->tx_q.tpd.tpbuff[i].dma_addr = 0;
885
886         adpt->rx_q.rrd.produce_idx = 0;
887         adpt->rx_q.rrd.consume_idx = 0;
888         adpt->rx_q.rfd.produce_idx = 0;
889         adpt->rx_q.rfd.consume_idx = 0;
890         for (i = 0; i < adpt->rx_q.rfd.count; i++)
891                 adpt->rx_q.rfd.rfbuff[i].dma_addr = 0;
892 }
893
894 /* Produce new receive free descriptor */
895 static void emac_mac_rx_rfd_create(struct emac_adapter *adpt,
896                                    struct emac_rx_queue *rx_q,
897                                    dma_addr_t addr)
898 {
899         u32 *hw_rfd = EMAC_RFD(rx_q, adpt->rfd_size, rx_q->rfd.produce_idx);
900
901         *(hw_rfd++) = lower_32_bits(addr);
902         *hw_rfd = upper_32_bits(addr);
903
904         if (++rx_q->rfd.produce_idx == rx_q->rfd.count)
905                 rx_q->rfd.produce_idx = 0;
906 }
907
908 /* Fill up receive queue's RFD with preallocated receive buffers */
909 static void emac_mac_rx_descs_refill(struct emac_adapter *adpt,
910                                     struct emac_rx_queue *rx_q)
911 {
912         struct emac_buffer *curr_rxbuf;
913         struct emac_buffer *next_rxbuf;
914         unsigned int count = 0;
915         u32 next_produce_idx;
916
917         next_produce_idx = rx_q->rfd.produce_idx + 1;
918         if (next_produce_idx == rx_q->rfd.count)
919                 next_produce_idx = 0;
920
921         curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
922         next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
923
924         /* this always has a blank rx_buffer*/
925         while (!next_rxbuf->dma_addr) {
926                 struct sk_buff *skb;
927                 int ret;
928
929                 skb = netdev_alloc_skb_ip_align(adpt->netdev, adpt->rxbuf_size);
930                 if (!skb)
931                         break;
932
933                 curr_rxbuf->dma_addr =
934                         dma_map_single(adpt->netdev->dev.parent, skb->data,
935                                        adpt->rxbuf_size, DMA_FROM_DEVICE);
936
937                 ret = dma_mapping_error(adpt->netdev->dev.parent,
938                                         curr_rxbuf->dma_addr);
939                 if (ret) {
940                         dev_kfree_skb(skb);
941                         break;
942                 }
943                 curr_rxbuf->skb = skb;
944                 curr_rxbuf->length = adpt->rxbuf_size;
945
946                 emac_mac_rx_rfd_create(adpt, rx_q, curr_rxbuf->dma_addr);
947                 next_produce_idx = rx_q->rfd.produce_idx + 1;
948                 if (next_produce_idx == rx_q->rfd.count)
949                         next_produce_idx = 0;
950
951                 curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
952                 next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
953                 count++;
954         }
955
956         if (count) {
957                 u32 prod_idx = (rx_q->rfd.produce_idx << rx_q->produce_shift) &
958                                 rx_q->produce_mask;
959                 emac_reg_update32(adpt->base + rx_q->produce_reg,
960                                   rx_q->produce_mask, prod_idx);
961         }
962 }
963
964 static void emac_adjust_link(struct net_device *netdev)
965 {
966         struct emac_adapter *adpt = netdev_priv(netdev);
967         struct phy_device *phydev = netdev->phydev;
968
969         if (phydev->link)
970                 emac_mac_start(adpt);
971         else
972                 emac_mac_stop(adpt);
973
974         phy_print_status(phydev);
975 }
976
977 /* Bringup the interface/HW */
978 int emac_mac_up(struct emac_adapter *adpt)
979 {
980         struct net_device *netdev = adpt->netdev;
981         struct emac_irq *irq = &adpt->irq;
982         int ret;
983
984         emac_mac_rx_tx_ring_reset_all(adpt);
985         emac_mac_config(adpt);
986
987         ret = request_irq(irq->irq, emac_isr, 0, EMAC_MAC_IRQ_RES, irq);
988         if (ret) {
989                 netdev_err(adpt->netdev, "could not request %s irq\n",
990                            EMAC_MAC_IRQ_RES);
991                 return ret;
992         }
993
994         emac_mac_rx_descs_refill(adpt, &adpt->rx_q);
995
996         ret = phy_connect_direct(netdev, adpt->phydev, emac_adjust_link,
997                                  PHY_INTERFACE_MODE_SGMII);
998         if (ret) {
999                 netdev_err(adpt->netdev, "could not connect phy\n");
1000                 free_irq(irq->irq, irq);
1001                 return ret;
1002         }
1003
1004         /* enable mac irq */
1005         writel((u32)~DIS_INT, adpt->base + EMAC_INT_STATUS);
1006         writel(adpt->irq.mask, adpt->base + EMAC_INT_MASK);
1007
1008         /* Enable pause frames.  Without this feature, the EMAC has been shown
1009          * to receive (and drop) frames with FCS errors at gigabit connections.
1010          */
1011         adpt->phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1012         adpt->phydev->advertising |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1013
1014         adpt->phydev->irq = PHY_IGNORE_INTERRUPT;
1015         phy_start(adpt->phydev);
1016
1017         napi_enable(&adpt->rx_q.napi);
1018         netif_start_queue(netdev);
1019
1020         return 0;
1021 }
1022
1023 /* Bring down the interface/HW */
1024 void emac_mac_down(struct emac_adapter *adpt)
1025 {
1026         struct net_device *netdev = adpt->netdev;
1027
1028         netif_stop_queue(netdev);
1029         napi_disable(&adpt->rx_q.napi);
1030
1031         phy_stop(adpt->phydev);
1032
1033         /* Interrupts must be disabled before the PHY is disconnected, to
1034          * avoid a race condition where adjust_link is null when we get
1035          * an interrupt.
1036          */
1037         writel(DIS_INT, adpt->base + EMAC_INT_STATUS);
1038         writel(0, adpt->base + EMAC_INT_MASK);
1039         synchronize_irq(adpt->irq.irq);
1040         free_irq(adpt->irq.irq, &adpt->irq);
1041
1042         phy_disconnect(adpt->phydev);
1043
1044         emac_mac_reset(adpt);
1045
1046         emac_tx_q_descs_free(adpt);
1047         netdev_reset_queue(adpt->netdev);
1048         emac_rx_q_free_descs(adpt);
1049 }
1050
1051 /* Consume next received packet descriptor */
1052 static bool emac_rx_process_rrd(struct emac_adapter *adpt,
1053                                 struct emac_rx_queue *rx_q,
1054                                 struct emac_rrd *rrd)
1055 {
1056         u32 *hw_rrd = EMAC_RRD(rx_q, adpt->rrd_size, rx_q->rrd.consume_idx);
1057
1058         rrd->word[3] = *(hw_rrd + 3);
1059
1060         if (!RRD_UPDT(rrd))
1061                 return false;
1062
1063         rrd->word[4] = 0;
1064         rrd->word[5] = 0;
1065
1066         rrd->word[0] = *(hw_rrd++);
1067         rrd->word[1] = *(hw_rrd++);
1068         rrd->word[2] = *(hw_rrd++);
1069
1070         if (unlikely(RRD_NOR(rrd) != 1)) {
1071                 netdev_err(adpt->netdev,
1072                            "error: multi-RFD not support yet! nor:%lu\n",
1073                            RRD_NOR(rrd));
1074         }
1075
1076         /* mark rrd as processed */
1077         RRD_UPDT_SET(rrd, 0);
1078         *hw_rrd = rrd->word[3];
1079
1080         if (++rx_q->rrd.consume_idx == rx_q->rrd.count)
1081                 rx_q->rrd.consume_idx = 0;
1082
1083         return true;
1084 }
1085
1086 /* Produce new transmit descriptor */
1087 static void emac_tx_tpd_create(struct emac_adapter *adpt,
1088                                struct emac_tx_queue *tx_q, struct emac_tpd *tpd)
1089 {
1090         u32 *hw_tpd;
1091
1092         tx_q->tpd.last_produce_idx = tx_q->tpd.produce_idx;
1093         hw_tpd = EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.produce_idx);
1094
1095         if (++tx_q->tpd.produce_idx == tx_q->tpd.count)
1096                 tx_q->tpd.produce_idx = 0;
1097
1098         *(hw_tpd++) = tpd->word[0];
1099         *(hw_tpd++) = tpd->word[1];
1100         *(hw_tpd++) = tpd->word[2];
1101         *hw_tpd = tpd->word[3];
1102 }
1103
1104 /* Mark the last transmit descriptor as such (for the transmit packet) */
1105 static void emac_tx_tpd_mark_last(struct emac_adapter *adpt,
1106                                   struct emac_tx_queue *tx_q)
1107 {
1108         u32 *hw_tpd =
1109                 EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.last_produce_idx);
1110         u32 tmp_tpd;
1111
1112         tmp_tpd = *(hw_tpd + 1);
1113         tmp_tpd |= EMAC_TPD_LAST_FRAGMENT;
1114         *(hw_tpd + 1) = tmp_tpd;
1115 }
1116
1117 static void emac_rx_rfd_clean(struct emac_rx_queue *rx_q, struct emac_rrd *rrd)
1118 {
1119         struct emac_buffer *rfbuf = rx_q->rfd.rfbuff;
1120         u32 consume_idx = RRD_SI(rrd);
1121         unsigned int i;
1122
1123         for (i = 0; i < RRD_NOR(rrd); i++) {
1124                 rfbuf[consume_idx].skb = NULL;
1125                 if (++consume_idx == rx_q->rfd.count)
1126                         consume_idx = 0;
1127         }
1128
1129         rx_q->rfd.consume_idx = consume_idx;
1130         rx_q->rfd.process_idx = consume_idx;
1131 }
1132
1133 /* Push the received skb to upper layers */
1134 static void emac_receive_skb(struct emac_rx_queue *rx_q,
1135                              struct sk_buff *skb,
1136                              u16 vlan_tag, bool vlan_flag)
1137 {
1138         if (vlan_flag) {
1139                 u16 vlan;
1140
1141                 EMAC_TAG_TO_VLAN(vlan_tag, vlan);
1142                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
1143         }
1144
1145         napi_gro_receive(&rx_q->napi, skb);
1146 }
1147
1148 /* Process receive event */
1149 void emac_mac_rx_process(struct emac_adapter *adpt, struct emac_rx_queue *rx_q,
1150                          int *num_pkts, int max_pkts)
1151 {
1152         u32 proc_idx, hw_consume_idx, num_consume_pkts;
1153         struct net_device *netdev  = adpt->netdev;
1154         struct emac_buffer *rfbuf;
1155         unsigned int count = 0;
1156         struct emac_rrd rrd;
1157         struct sk_buff *skb;
1158         u32 reg;
1159
1160         reg = readl_relaxed(adpt->base + rx_q->consume_reg);
1161
1162         hw_consume_idx = (reg & rx_q->consume_mask) >> rx_q->consume_shift;
1163         num_consume_pkts = (hw_consume_idx >= rx_q->rrd.consume_idx) ?
1164                 (hw_consume_idx -  rx_q->rrd.consume_idx) :
1165                 (hw_consume_idx + rx_q->rrd.count - rx_q->rrd.consume_idx);
1166
1167         do {
1168                 if (!num_consume_pkts)
1169                         break;
1170
1171                 if (!emac_rx_process_rrd(adpt, rx_q, &rrd))
1172                         break;
1173
1174                 if (likely(RRD_NOR(&rrd) == 1)) {
1175                         /* good receive */
1176                         rfbuf = GET_RFD_BUFFER(rx_q, RRD_SI(&rrd));
1177                         dma_unmap_single(adpt->netdev->dev.parent,
1178                                          rfbuf->dma_addr, rfbuf->length,
1179                                          DMA_FROM_DEVICE);
1180                         rfbuf->dma_addr = 0;
1181                         skb = rfbuf->skb;
1182                 } else {
1183                         netdev_err(adpt->netdev,
1184                                    "error: multi-RFD not support yet!\n");
1185                         break;
1186                 }
1187                 emac_rx_rfd_clean(rx_q, &rrd);
1188                 num_consume_pkts--;
1189                 count++;
1190
1191                 /* Due to a HW issue in L4 check sum detection (UDP/TCP frags
1192                  * with DF set are marked as error), drop packets based on the
1193                  * error mask rather than the summary bit (ignoring L4F errors)
1194                  */
1195                 if (rrd.word[EMAC_RRD_STATS_DW_IDX] & EMAC_RRD_ERROR) {
1196                         netif_dbg(adpt, rx_status, adpt->netdev,
1197                                   "Drop error packet[RRD: 0x%x:0x%x:0x%x:0x%x]\n",
1198                                   rrd.word[0], rrd.word[1],
1199                                   rrd.word[2], rrd.word[3]);
1200
1201                         dev_kfree_skb(skb);
1202                         continue;
1203                 }
1204
1205                 skb_put(skb, RRD_PKT_SIZE(&rrd) - ETH_FCS_LEN);
1206                 skb->dev = netdev;
1207                 skb->protocol = eth_type_trans(skb, skb->dev);
1208                 if (netdev->features & NETIF_F_RXCSUM)
1209                         skb->ip_summed = RRD_L4F(&rrd) ?
1210                                           CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
1211                 else
1212                         skb_checksum_none_assert(skb);
1213
1214                 emac_receive_skb(rx_q, skb, (u16)RRD_CVALN_TAG(&rrd),
1215                                  (bool)RRD_CVTAG(&rrd));
1216
1217                 netdev->last_rx = jiffies;
1218                 (*num_pkts)++;
1219         } while (*num_pkts < max_pkts);
1220
1221         if (count) {
1222                 proc_idx = (rx_q->rfd.process_idx << rx_q->process_shft) &
1223                                 rx_q->process_mask;
1224                 emac_reg_update32(adpt->base + rx_q->process_reg,
1225                                   rx_q->process_mask, proc_idx);
1226                 emac_mac_rx_descs_refill(adpt, rx_q);
1227         }
1228 }
1229
1230 /* get the number of free transmit descriptors */
1231 static unsigned int emac_tpd_num_free_descs(struct emac_tx_queue *tx_q)
1232 {
1233         u32 produce_idx = tx_q->tpd.produce_idx;
1234         u32 consume_idx = tx_q->tpd.consume_idx;
1235
1236         return (consume_idx > produce_idx) ?
1237                 (consume_idx - produce_idx - 1) :
1238                 (tx_q->tpd.count + consume_idx - produce_idx - 1);
1239 }
1240
1241 /* Process transmit event */
1242 void emac_mac_tx_process(struct emac_adapter *adpt, struct emac_tx_queue *tx_q)
1243 {
1244         u32 reg = readl_relaxed(adpt->base + tx_q->consume_reg);
1245         u32 hw_consume_idx, pkts_compl = 0, bytes_compl = 0;
1246         struct emac_buffer *tpbuf;
1247
1248         hw_consume_idx = (reg & tx_q->consume_mask) >> tx_q->consume_shift;
1249
1250         while (tx_q->tpd.consume_idx != hw_consume_idx) {
1251                 tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.consume_idx);
1252                 if (tpbuf->dma_addr) {
1253                         dma_unmap_page(adpt->netdev->dev.parent,
1254                                        tpbuf->dma_addr, tpbuf->length,
1255                                        DMA_TO_DEVICE);
1256                         tpbuf->dma_addr = 0;
1257                 }
1258
1259                 if (tpbuf->skb) {
1260                         pkts_compl++;
1261                         bytes_compl += tpbuf->skb->len;
1262                         dev_kfree_skb_irq(tpbuf->skb);
1263                         tpbuf->skb = NULL;
1264                 }
1265
1266                 if (++tx_q->tpd.consume_idx == tx_q->tpd.count)
1267                         tx_q->tpd.consume_idx = 0;
1268         }
1269
1270         netdev_completed_queue(adpt->netdev, pkts_compl, bytes_compl);
1271
1272         if (netif_queue_stopped(adpt->netdev))
1273                 if (emac_tpd_num_free_descs(tx_q) > (MAX_SKB_FRAGS + 1))
1274                         netif_wake_queue(adpt->netdev);
1275 }
1276
1277 /* Initialize all queue data structures */
1278 void emac_mac_rx_tx_ring_init_all(struct platform_device *pdev,
1279                                   struct emac_adapter *adpt)
1280 {
1281         adpt->rx_q.netdev = adpt->netdev;
1282
1283         adpt->rx_q.produce_reg  = EMAC_MAILBOX_0;
1284         adpt->rx_q.produce_mask = RFD0_PROD_IDX_BMSK;
1285         adpt->rx_q.produce_shift = RFD0_PROD_IDX_SHFT;
1286
1287         adpt->rx_q.process_reg  = EMAC_MAILBOX_0;
1288         adpt->rx_q.process_mask = RFD0_PROC_IDX_BMSK;
1289         adpt->rx_q.process_shft = RFD0_PROC_IDX_SHFT;
1290
1291         adpt->rx_q.consume_reg  = EMAC_MAILBOX_3;
1292         adpt->rx_q.consume_mask = RFD0_CONS_IDX_BMSK;
1293         adpt->rx_q.consume_shift = RFD0_CONS_IDX_SHFT;
1294
1295         adpt->rx_q.irq          = &adpt->irq;
1296         adpt->rx_q.intr         = adpt->irq.mask & ISR_RX_PKT;
1297
1298         adpt->tx_q.produce_reg  = EMAC_MAILBOX_15;
1299         adpt->tx_q.produce_mask = NTPD_PROD_IDX_BMSK;
1300         adpt->tx_q.produce_shift = NTPD_PROD_IDX_SHFT;
1301
1302         adpt->tx_q.consume_reg  = EMAC_MAILBOX_2;
1303         adpt->tx_q.consume_mask = NTPD_CONS_IDX_BMSK;
1304         adpt->tx_q.consume_shift = NTPD_CONS_IDX_SHFT;
1305 }
1306
1307 /* Fill up transmit descriptors with TSO and Checksum offload information */
1308 static int emac_tso_csum(struct emac_adapter *adpt,
1309                          struct emac_tx_queue *tx_q,
1310                          struct sk_buff *skb,
1311                          struct emac_tpd *tpd)
1312 {
1313         unsigned int hdr_len;
1314         int ret;
1315
1316         if (skb_is_gso(skb)) {
1317                 if (skb_header_cloned(skb)) {
1318                         ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1319                         if (unlikely(ret))
1320                                 return ret;
1321                 }
1322
1323                 if (skb->protocol == htons(ETH_P_IP)) {
1324                         u32 pkt_len = ((unsigned char *)ip_hdr(skb) - skb->data)
1325                                        + ntohs(ip_hdr(skb)->tot_len);
1326                         if (skb->len > pkt_len)
1327                                 pskb_trim(skb, pkt_len);
1328                 }
1329
1330                 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1331                 if (unlikely(skb->len == hdr_len)) {
1332                         /* we only need to do csum */
1333                         netif_warn(adpt, tx_err, adpt->netdev,
1334                                    "tso not needed for packet with 0 data\n");
1335                         goto do_csum;
1336                 }
1337
1338                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1339                         ip_hdr(skb)->check = 0;
1340                         tcp_hdr(skb)->check =
1341                                 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1342                                                    ip_hdr(skb)->daddr,
1343                                                    0, IPPROTO_TCP, 0);
1344                         TPD_IPV4_SET(tpd, 1);
1345                 }
1346
1347                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
1348                         /* ipv6 tso need an extra tpd */
1349                         struct emac_tpd extra_tpd;
1350
1351                         memset(tpd, 0, sizeof(*tpd));
1352                         memset(&extra_tpd, 0, sizeof(extra_tpd));
1353
1354                         ipv6_hdr(skb)->payload_len = 0;
1355                         tcp_hdr(skb)->check =
1356                                 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1357                                                  &ipv6_hdr(skb)->daddr,
1358                                                  0, IPPROTO_TCP, 0);
1359                         TPD_PKT_LEN_SET(&extra_tpd, skb->len);
1360                         TPD_LSO_SET(&extra_tpd, 1);
1361                         TPD_LSOV_SET(&extra_tpd, 1);
1362                         emac_tx_tpd_create(adpt, tx_q, &extra_tpd);
1363                         TPD_LSOV_SET(tpd, 1);
1364                 }
1365
1366                 TPD_LSO_SET(tpd, 1);
1367                 TPD_TCPHDR_OFFSET_SET(tpd, skb_transport_offset(skb));
1368                 TPD_MSS_SET(tpd, skb_shinfo(skb)->gso_size);
1369                 return 0;
1370         }
1371
1372 do_csum:
1373         if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1374                 unsigned int css, cso;
1375
1376                 cso = skb_transport_offset(skb);
1377                 if (unlikely(cso & 0x1)) {
1378                         netdev_err(adpt->netdev,
1379                                    "error: payload offset should be even\n");
1380                         return -EINVAL;
1381                 }
1382                 css = cso + skb->csum_offset;
1383
1384                 TPD_PAYLOAD_OFFSET_SET(tpd, cso >> 1);
1385                 TPD_CXSUM_OFFSET_SET(tpd, css >> 1);
1386                 TPD_CSX_SET(tpd, 1);
1387         }
1388
1389         return 0;
1390 }
1391
1392 /* Fill up transmit descriptors */
1393 static void emac_tx_fill_tpd(struct emac_adapter *adpt,
1394                              struct emac_tx_queue *tx_q, struct sk_buff *skb,
1395                              struct emac_tpd *tpd)
1396 {
1397         unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
1398         unsigned int first = tx_q->tpd.produce_idx;
1399         unsigned int len = skb_headlen(skb);
1400         struct emac_buffer *tpbuf = NULL;
1401         unsigned int mapped_len = 0;
1402         unsigned int i;
1403         int count = 0;
1404         int ret;
1405
1406         /* if Large Segment Offload is (in TCP Segmentation Offload struct) */
1407         if (TPD_LSO(tpd)) {
1408                 mapped_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1409
1410                 tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
1411                 tpbuf->length = mapped_len;
1412                 tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
1413                                                virt_to_page(skb->data),
1414                                                offset_in_page(skb->data),
1415                                                tpbuf->length,
1416                                                DMA_TO_DEVICE);
1417                 ret = dma_mapping_error(adpt->netdev->dev.parent,
1418                                         tpbuf->dma_addr);
1419                 if (ret)
1420                         goto error;
1421
1422                 TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
1423                 TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
1424                 TPD_BUF_LEN_SET(tpd, tpbuf->length);
1425                 emac_tx_tpd_create(adpt, tx_q, tpd);
1426                 count++;
1427         }
1428
1429         if (mapped_len < len) {
1430                 tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
1431                 tpbuf->length = len - mapped_len;
1432                 tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
1433                                                virt_to_page(skb->data +
1434                                                             mapped_len),
1435                                                offset_in_page(skb->data +
1436                                                               mapped_len),
1437                                                tpbuf->length, DMA_TO_DEVICE);
1438                 ret = dma_mapping_error(adpt->netdev->dev.parent,
1439                                         tpbuf->dma_addr);
1440                 if (ret)
1441                         goto error;
1442
1443                 TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
1444                 TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
1445                 TPD_BUF_LEN_SET(tpd, tpbuf->length);
1446                 emac_tx_tpd_create(adpt, tx_q, tpd);
1447                 count++;
1448         }
1449
1450         for (i = 0; i < nr_frags; i++) {
1451                 struct skb_frag_struct *frag;
1452
1453                 frag = &skb_shinfo(skb)->frags[i];
1454
1455                 tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
1456                 tpbuf->length = frag->size;
1457                 tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
1458                                                frag->page.p, frag->page_offset,
1459                                                tpbuf->length, DMA_TO_DEVICE);
1460                 ret = dma_mapping_error(adpt->netdev->dev.parent,
1461                                         tpbuf->dma_addr);
1462                 if (ret)
1463                         goto error;
1464
1465                 TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
1466                 TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
1467                 TPD_BUF_LEN_SET(tpd, tpbuf->length);
1468                 emac_tx_tpd_create(adpt, tx_q, tpd);
1469                 count++;
1470         }
1471
1472         /* The last tpd */
1473         wmb();
1474         emac_tx_tpd_mark_last(adpt, tx_q);
1475
1476         /* The last buffer info contain the skb address,
1477          * so it will be freed after unmap
1478          */
1479         tpbuf->skb = skb;
1480
1481         return;
1482
1483 error:
1484         /* One of the memory mappings failed, so undo everything */
1485         tx_q->tpd.produce_idx = first;
1486
1487         while (count--) {
1488                 tpbuf = GET_TPD_BUFFER(tx_q, first);
1489                 dma_unmap_page(adpt->netdev->dev.parent, tpbuf->dma_addr,
1490                                tpbuf->length, DMA_TO_DEVICE);
1491                 tpbuf->dma_addr = 0;
1492                 tpbuf->length = 0;
1493
1494                 if (++first == tx_q->tpd.count)
1495                         first = 0;
1496         }
1497
1498         dev_kfree_skb(skb);
1499 }
1500
1501 /* Transmit the packet using specified transmit queue */
1502 int emac_mac_tx_buf_send(struct emac_adapter *adpt, struct emac_tx_queue *tx_q,
1503                          struct sk_buff *skb)
1504 {
1505         struct emac_tpd tpd;
1506         u32 prod_idx;
1507         int len;
1508
1509         memset(&tpd, 0, sizeof(tpd));
1510
1511         if (emac_tso_csum(adpt, tx_q, skb, &tpd) != 0) {
1512                 dev_kfree_skb_any(skb);
1513                 return NETDEV_TX_OK;
1514         }
1515
1516         if (skb_vlan_tag_present(skb)) {
1517                 u16 tag;
1518
1519                 EMAC_VLAN_TO_TAG(skb_vlan_tag_get(skb), tag);
1520                 TPD_CVLAN_TAG_SET(&tpd, tag);
1521                 TPD_INSTC_SET(&tpd, 1);
1522         }
1523
1524         if (skb_network_offset(skb) != ETH_HLEN)
1525                 TPD_TYP_SET(&tpd, 1);
1526
1527         len = skb->len;
1528         emac_tx_fill_tpd(adpt, tx_q, skb, &tpd);
1529
1530         netdev_sent_queue(adpt->netdev, len);
1531
1532         /* Make sure the are enough free descriptors to hold one
1533          * maximum-sized SKB.  We need one desc for each fragment,
1534          * one for the checksum (emac_tso_csum), one for TSO, and
1535          * and one for the SKB header.
1536          */
1537         if (emac_tpd_num_free_descs(tx_q) < (MAX_SKB_FRAGS + 3))
1538                 netif_stop_queue(adpt->netdev);
1539
1540         /* update produce idx */
1541         prod_idx = (tx_q->tpd.produce_idx << tx_q->produce_shift) &
1542                     tx_q->produce_mask;
1543         emac_reg_update32(adpt->base + tx_q->produce_reg,
1544                           tx_q->produce_mask, prod_idx);
1545
1546         return NETDEV_TX_OK;
1547 }