af1ac82561c02a99f2b7c451dd4e1fe90dde2db3
[firefly-linux-kernel-4.4.55.git] / drivers / net / ethernet / marvell / mvneta.c
1 /*
2  * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
3  *
4  * Copyright (C) 2012 Marvell
5  *
6  * Rami Rosen <rosenr@marvell.com>
7  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
8  *
9  * This file is licensed under the terms of the GNU General Public
10  * License version 2. This program is licensed "as is" without any
11  * warranty of any kind, whether express or implied.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/platform_device.h>
18 #include <linux/skbuff.h>
19 #include <linux/inetdevice.h>
20 #include <linux/mbus.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <net/ip.h>
24 #include <net/ipv6.h>
25 #include <linux/io.h>
26 #include <net/tso.h>
27 #include <linux/of.h>
28 #include <linux/of_irq.h>
29 #include <linux/of_mdio.h>
30 #include <linux/of_net.h>
31 #include <linux/of_address.h>
32 #include <linux/phy.h>
33 #include <linux/clk.h>
34
35 /* Registers */
36 #define MVNETA_RXQ_CONFIG_REG(q)                (0x1400 + ((q) << 2))
37 #define      MVNETA_RXQ_HW_BUF_ALLOC            BIT(1)
38 #define      MVNETA_RXQ_PKT_OFFSET_ALL_MASK     (0xf    << 8)
39 #define      MVNETA_RXQ_PKT_OFFSET_MASK(offs)   ((offs) << 8)
40 #define MVNETA_RXQ_THRESHOLD_REG(q)             (0x14c0 + ((q) << 2))
41 #define      MVNETA_RXQ_NON_OCCUPIED(v)         ((v) << 16)
42 #define MVNETA_RXQ_BASE_ADDR_REG(q)             (0x1480 + ((q) << 2))
43 #define MVNETA_RXQ_SIZE_REG(q)                  (0x14a0 + ((q) << 2))
44 #define      MVNETA_RXQ_BUF_SIZE_SHIFT          19
45 #define      MVNETA_RXQ_BUF_SIZE_MASK           (0x1fff << 19)
46 #define MVNETA_RXQ_STATUS_REG(q)                (0x14e0 + ((q) << 2))
47 #define      MVNETA_RXQ_OCCUPIED_ALL_MASK       0x3fff
48 #define MVNETA_RXQ_STATUS_UPDATE_REG(q)         (0x1500 + ((q) << 2))
49 #define      MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT  16
50 #define      MVNETA_RXQ_ADD_NON_OCCUPIED_MAX    255
51 #define MVNETA_PORT_RX_RESET                    0x1cc0
52 #define      MVNETA_PORT_RX_DMA_RESET           BIT(0)
53 #define MVNETA_PHY_ADDR                         0x2000
54 #define      MVNETA_PHY_ADDR_MASK               0x1f
55 #define MVNETA_MBUS_RETRY                       0x2010
56 #define MVNETA_UNIT_INTR_CAUSE                  0x2080
57 #define MVNETA_UNIT_CONTROL                     0x20B0
58 #define      MVNETA_PHY_POLLING_ENABLE          BIT(1)
59 #define MVNETA_WIN_BASE(w)                      (0x2200 + ((w) << 3))
60 #define MVNETA_WIN_SIZE(w)                      (0x2204 + ((w) << 3))
61 #define MVNETA_WIN_REMAP(w)                     (0x2280 + ((w) << 2))
62 #define MVNETA_BASE_ADDR_ENABLE                 0x2290
63 #define MVNETA_PORT_CONFIG                      0x2400
64 #define      MVNETA_UNI_PROMISC_MODE            BIT(0)
65 #define      MVNETA_DEF_RXQ(q)                  ((q) << 1)
66 #define      MVNETA_DEF_RXQ_ARP(q)              ((q) << 4)
67 #define      MVNETA_TX_UNSET_ERR_SUM            BIT(12)
68 #define      MVNETA_DEF_RXQ_TCP(q)              ((q) << 16)
69 #define      MVNETA_DEF_RXQ_UDP(q)              ((q) << 19)
70 #define      MVNETA_DEF_RXQ_BPDU(q)             ((q) << 22)
71 #define      MVNETA_RX_CSUM_WITH_PSEUDO_HDR     BIT(25)
72 #define      MVNETA_PORT_CONFIG_DEFL_VALUE(q)   (MVNETA_DEF_RXQ(q)       | \
73                                                  MVNETA_DEF_RXQ_ARP(q)   | \
74                                                  MVNETA_DEF_RXQ_TCP(q)   | \
75                                                  MVNETA_DEF_RXQ_UDP(q)   | \
76                                                  MVNETA_DEF_RXQ_BPDU(q)  | \
77                                                  MVNETA_TX_UNSET_ERR_SUM | \
78                                                  MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
79 #define MVNETA_PORT_CONFIG_EXTEND                0x2404
80 #define MVNETA_MAC_ADDR_LOW                      0x2414
81 #define MVNETA_MAC_ADDR_HIGH                     0x2418
82 #define MVNETA_SDMA_CONFIG                       0x241c
83 #define      MVNETA_SDMA_BRST_SIZE_16            4
84 #define      MVNETA_RX_BRST_SZ_MASK(burst)       ((burst) << 1)
85 #define      MVNETA_RX_NO_DATA_SWAP              BIT(4)
86 #define      MVNETA_TX_NO_DATA_SWAP              BIT(5)
87 #define      MVNETA_DESC_SWAP                    BIT(6)
88 #define      MVNETA_TX_BRST_SZ_MASK(burst)       ((burst) << 22)
89 #define MVNETA_PORT_STATUS                       0x2444
90 #define      MVNETA_TX_IN_PRGRS                  BIT(1)
91 #define      MVNETA_TX_FIFO_EMPTY                BIT(8)
92 #define MVNETA_RX_MIN_FRAME_SIZE                 0x247c
93 #define MVNETA_SERDES_CFG                        0x24A0
94 #define      MVNETA_SGMII_SERDES_PROTO           0x0cc7
95 #define      MVNETA_QSGMII_SERDES_PROTO          0x0667
96 #define MVNETA_TYPE_PRIO                         0x24bc
97 #define      MVNETA_FORCE_UNI                    BIT(21)
98 #define MVNETA_TXQ_CMD_1                         0x24e4
99 #define MVNETA_TXQ_CMD                           0x2448
100 #define      MVNETA_TXQ_DISABLE_SHIFT            8
101 #define      MVNETA_TXQ_ENABLE_MASK              0x000000ff
102 #define MVNETA_ACC_MODE                          0x2500
103 #define MVNETA_CPU_MAP(cpu)                      (0x2540 + ((cpu) << 2))
104 #define      MVNETA_CPU_RXQ_ACCESS_ALL_MASK      0x000000ff
105 #define      MVNETA_CPU_TXQ_ACCESS_ALL_MASK      0x0000ff00
106 #define MVNETA_RXQ_TIME_COAL_REG(q)              (0x2580 + ((q) << 2))
107
108 /* Exception Interrupt Port/Queue Cause register */
109
110 #define MVNETA_INTR_NEW_CAUSE                    0x25a0
111 #define MVNETA_INTR_NEW_MASK                     0x25a4
112
113 /* bits  0..7  = TXQ SENT, one bit per queue.
114  * bits  8..15 = RXQ OCCUP, one bit per queue.
115  * bits 16..23 = RXQ FREE, one bit per queue.
116  * bit  29 = OLD_REG_SUM, see old reg ?
117  * bit  30 = TX_ERR_SUM, one bit for 4 ports
118  * bit  31 = MISC_SUM,   one bit for 4 ports
119  */
120 #define      MVNETA_TX_INTR_MASK(nr_txqs)        (((1 << nr_txqs) - 1) << 0)
121 #define      MVNETA_TX_INTR_MASK_ALL             (0xff << 0)
122 #define      MVNETA_RX_INTR_MASK(nr_rxqs)        (((1 << nr_rxqs) - 1) << 8)
123 #define      MVNETA_RX_INTR_MASK_ALL             (0xff << 8)
124
125 #define MVNETA_INTR_OLD_CAUSE                    0x25a8
126 #define MVNETA_INTR_OLD_MASK                     0x25ac
127
128 /* Data Path Port/Queue Cause Register */
129 #define MVNETA_INTR_MISC_CAUSE                   0x25b0
130 #define MVNETA_INTR_MISC_MASK                    0x25b4
131
132 #define      MVNETA_CAUSE_PHY_STATUS_CHANGE      BIT(0)
133 #define      MVNETA_CAUSE_LINK_CHANGE            BIT(1)
134 #define      MVNETA_CAUSE_PTP                    BIT(4)
135
136 #define      MVNETA_CAUSE_INTERNAL_ADDR_ERR      BIT(7)
137 #define      MVNETA_CAUSE_RX_OVERRUN             BIT(8)
138 #define      MVNETA_CAUSE_RX_CRC_ERROR           BIT(9)
139 #define      MVNETA_CAUSE_RX_LARGE_PKT           BIT(10)
140 #define      MVNETA_CAUSE_TX_UNDERUN             BIT(11)
141 #define      MVNETA_CAUSE_PRBS_ERR               BIT(12)
142 #define      MVNETA_CAUSE_PSC_SYNC_CHANGE        BIT(13)
143 #define      MVNETA_CAUSE_SERDES_SYNC_ERR        BIT(14)
144
145 #define      MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT    16
146 #define      MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK   (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
147 #define      MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
148
149 #define      MVNETA_CAUSE_TXQ_ERROR_SHIFT        24
150 #define      MVNETA_CAUSE_TXQ_ERROR_ALL_MASK     (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
151 #define      MVNETA_CAUSE_TXQ_ERROR_MASK(q)      (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
152
153 #define MVNETA_INTR_ENABLE                       0x25b8
154 #define      MVNETA_TXQ_INTR_ENABLE_ALL_MASK     0x0000ff00
155 #define      MVNETA_RXQ_INTR_ENABLE_ALL_MASK     0xff000000  // note: neta says it's 0x000000FF
156
157 #define MVNETA_RXQ_CMD                           0x2680
158 #define      MVNETA_RXQ_DISABLE_SHIFT            8
159 #define      MVNETA_RXQ_ENABLE_MASK              0x000000ff
160 #define MVETH_TXQ_TOKEN_COUNT_REG(q)             (0x2700 + ((q) << 4))
161 #define MVETH_TXQ_TOKEN_CFG_REG(q)               (0x2704 + ((q) << 4))
162 #define MVNETA_GMAC_CTRL_0                       0x2c00
163 #define      MVNETA_GMAC_MAX_RX_SIZE_SHIFT       2
164 #define      MVNETA_GMAC_MAX_RX_SIZE_MASK        0x7ffc
165 #define      MVNETA_GMAC0_PORT_ENABLE            BIT(0)
166 #define MVNETA_GMAC_CTRL_2                       0x2c08
167 #define      MVNETA_GMAC2_PCS_ENABLE             BIT(3)
168 #define      MVNETA_GMAC2_PORT_RGMII             BIT(4)
169 #define      MVNETA_GMAC2_PORT_RESET             BIT(6)
170 #define MVNETA_GMAC_STATUS                       0x2c10
171 #define      MVNETA_GMAC_LINK_UP                 BIT(0)
172 #define      MVNETA_GMAC_SPEED_1000              BIT(1)
173 #define      MVNETA_GMAC_SPEED_100               BIT(2)
174 #define      MVNETA_GMAC_FULL_DUPLEX             BIT(3)
175 #define      MVNETA_GMAC_RX_FLOW_CTRL_ENABLE     BIT(4)
176 #define      MVNETA_GMAC_TX_FLOW_CTRL_ENABLE     BIT(5)
177 #define      MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE     BIT(6)
178 #define      MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE     BIT(7)
179 #define MVNETA_GMAC_AUTONEG_CONFIG               0x2c0c
180 #define      MVNETA_GMAC_FORCE_LINK_DOWN         BIT(0)
181 #define      MVNETA_GMAC_FORCE_LINK_PASS         BIT(1)
182 #define      MVNETA_GMAC_CONFIG_MII_SPEED        BIT(5)
183 #define      MVNETA_GMAC_CONFIG_GMII_SPEED       BIT(6)
184 #define      MVNETA_GMAC_AN_SPEED_EN             BIT(7)
185 #define      MVNETA_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
186 #define      MVNETA_GMAC_AN_DUPLEX_EN            BIT(13)
187 #define MVNETA_MIB_COUNTERS_BASE                 0x3080
188 #define      MVNETA_MIB_LATE_COLLISION           0x7c
189 #define MVNETA_DA_FILT_SPEC_MCAST                0x3400
190 #define MVNETA_DA_FILT_OTH_MCAST                 0x3500
191 #define MVNETA_DA_FILT_UCAST_BASE                0x3600
192 #define MVNETA_TXQ_BASE_ADDR_REG(q)              (0x3c00 + ((q) << 2))
193 #define MVNETA_TXQ_SIZE_REG(q)                   (0x3c20 + ((q) << 2))
194 #define      MVNETA_TXQ_SENT_THRESH_ALL_MASK     0x3fff0000
195 #define      MVNETA_TXQ_SENT_THRESH_MASK(coal)   ((coal) << 16)
196 #define MVNETA_TXQ_UPDATE_REG(q)                 (0x3c60 + ((q) << 2))
197 #define      MVNETA_TXQ_DEC_SENT_SHIFT           16
198 #define MVNETA_TXQ_STATUS_REG(q)                 (0x3c40 + ((q) << 2))
199 #define      MVNETA_TXQ_SENT_DESC_SHIFT          16
200 #define      MVNETA_TXQ_SENT_DESC_MASK           0x3fff0000
201 #define MVNETA_PORT_TX_RESET                     0x3cf0
202 #define      MVNETA_PORT_TX_DMA_RESET            BIT(0)
203 #define MVNETA_TX_MTU                            0x3e0c
204 #define MVNETA_TX_TOKEN_SIZE                     0x3e14
205 #define      MVNETA_TX_TOKEN_SIZE_MAX            0xffffffff
206 #define MVNETA_TXQ_TOKEN_SIZE_REG(q)             (0x3e40 + ((q) << 2))
207 #define      MVNETA_TXQ_TOKEN_SIZE_MAX           0x7fffffff
208
209 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK      0xff
210
211 /* Descriptor ring Macros */
212 #define MVNETA_QUEUE_NEXT_DESC(q, index)        \
213         (((index) < (q)->last_desc) ? ((index) + 1) : 0)
214
215 /* Various constants */
216
217 /* Coalescing */
218 #define MVNETA_TXDONE_COAL_PKTS         16
219 #define MVNETA_RX_COAL_PKTS             32
220 #define MVNETA_RX_COAL_USEC             100
221
222 /* The two bytes Marvell header. Either contains a special value used
223  * by Marvell switches when a specific hardware mode is enabled (not
224  * supported by this driver) or is filled automatically by zeroes on
225  * the RX side. Those two bytes being at the front of the Ethernet
226  * header, they allow to have the IP header aligned on a 4 bytes
227  * boundary automatically: the hardware skips those two bytes on its
228  * own.
229  */
230 #define MVNETA_MH_SIZE                  2
231
232 #define MVNETA_VLAN_TAG_LEN             4
233
234 #define MVNETA_CPU_D_CACHE_LINE_SIZE    32
235 #define MVNETA_TX_CSUM_MAX_SIZE         9800
236 #define MVNETA_ACC_MODE_EXT             1
237
238 /* Timeout constants */
239 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC  1000
240 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC  1000
241 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT    10000
242
243 #define MVNETA_TX_MTU_MAX               0x3ffff
244
245 /* TSO header size */
246 #define TSO_HEADER_SIZE 128
247
248 /* Max number of Rx descriptors */
249 #define MVNETA_MAX_RXD 128
250
251 /* Max number of Tx descriptors */
252 #define MVNETA_MAX_TXD 532
253
254 /* descriptor aligned size */
255 #define MVNETA_DESC_ALIGNED_SIZE        32
256
257 #define MVNETA_RX_PKT_SIZE(mtu) \
258         ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
259               ETH_HLEN + ETH_FCS_LEN,                        \
260               MVNETA_CPU_D_CACHE_LINE_SIZE)
261
262 #define MVNETA_RX_BUF_SIZE(pkt_size)   ((pkt_size) + NET_SKB_PAD)
263
264 struct mvneta_pcpu_stats {
265         struct  u64_stats_sync syncp;
266         u64     rx_packets;
267         u64     rx_bytes;
268         u64     tx_packets;
269         u64     tx_bytes;
270 };
271
272 struct mvneta_port {
273         int pkt_size;
274         unsigned int frag_size;
275         void __iomem *base;
276         struct mvneta_rx_queue *rxqs;
277         struct mvneta_tx_queue *txqs;
278         struct net_device *dev;
279
280         u32 cause_rx_tx;
281         struct napi_struct napi;
282
283         /* Core clock */
284         struct clk *clk;
285         u8 mcast_count[256];
286         u16 tx_ring_size;
287         u16 rx_ring_size;
288         struct mvneta_pcpu_stats *stats;
289
290         struct mii_bus *mii_bus;
291         struct phy_device *phy_dev;
292         phy_interface_t phy_interface;
293         struct device_node *phy_node;
294         unsigned int link;
295         unsigned int duplex;
296         unsigned int speed;
297 };
298
299 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
300  * layout of the transmit and reception DMA descriptors, and their
301  * layout is therefore defined by the hardware design
302  */
303
304 #define MVNETA_TX_L3_OFF_SHIFT  0
305 #define MVNETA_TX_IP_HLEN_SHIFT 8
306 #define MVNETA_TX_L4_UDP        BIT(16)
307 #define MVNETA_TX_L3_IP6        BIT(17)
308 #define MVNETA_TXD_IP_CSUM      BIT(18)
309 #define MVNETA_TXD_Z_PAD        BIT(19)
310 #define MVNETA_TXD_L_DESC       BIT(20)
311 #define MVNETA_TXD_F_DESC       BIT(21)
312 #define MVNETA_TXD_FLZ_DESC     (MVNETA_TXD_Z_PAD  | \
313                                  MVNETA_TXD_L_DESC | \
314                                  MVNETA_TXD_F_DESC)
315 #define MVNETA_TX_L4_CSUM_FULL  BIT(30)
316 #define MVNETA_TX_L4_CSUM_NOT   BIT(31)
317
318 #define MVNETA_RXD_ERR_CRC              0x0
319 #define MVNETA_RXD_ERR_SUMMARY          BIT(16)
320 #define MVNETA_RXD_ERR_OVERRUN          BIT(17)
321 #define MVNETA_RXD_ERR_LEN              BIT(18)
322 #define MVNETA_RXD_ERR_RESOURCE         (BIT(17) | BIT(18))
323 #define MVNETA_RXD_ERR_CODE_MASK        (BIT(17) | BIT(18))
324 #define MVNETA_RXD_L3_IP4               BIT(25)
325 #define MVNETA_RXD_FIRST_LAST_DESC      (BIT(26) | BIT(27))
326 #define MVNETA_RXD_L4_CSUM_OK           BIT(30)
327
328 #if defined(__LITTLE_ENDIAN)
329 struct mvneta_tx_desc {
330         u32  command;           /* Options used by HW for packet transmitting.*/
331         u16  reserverd1;        /* csum_l4 (for future use)             */
332         u16  data_size;         /* Data size of transmitted packet in bytes */
333         u32  buf_phys_addr;     /* Physical addr of transmitted buffer  */
334         u32  reserved2;         /* hw_cmd - (for future use, PMT)       */
335         u32  reserved3[4];      /* Reserved - (for future use)          */
336 };
337
338 struct mvneta_rx_desc {
339         u32  status;            /* Info about received packet           */
340         u16  reserved1;         /* pnc_info - (for future use, PnC)     */
341         u16  data_size;         /* Size of received packet in bytes     */
342
343         u32  buf_phys_addr;     /* Physical address of the buffer       */
344         u32  reserved2;         /* pnc_flow_id  (for future use, PnC)   */
345
346         u32  buf_cookie;        /* cookie for access to RX buffer in rx path */
347         u16  reserved3;         /* prefetch_cmd, for future use         */
348         u16  reserved4;         /* csum_l4 - (for future use, PnC)      */
349
350         u32  reserved5;         /* pnc_extra PnC (for future use, PnC)  */
351         u32  reserved6;         /* hw_cmd (for future use, PnC and HWF) */
352 };
353 #else
354 struct mvneta_tx_desc {
355         u16  data_size;         /* Data size of transmitted packet in bytes */
356         u16  reserverd1;        /* csum_l4 (for future use)             */
357         u32  command;           /* Options used by HW for packet transmitting.*/
358         u32  reserved2;         /* hw_cmd - (for future use, PMT)       */
359         u32  buf_phys_addr;     /* Physical addr of transmitted buffer  */
360         u32  reserved3[4];      /* Reserved - (for future use)          */
361 };
362
363 struct mvneta_rx_desc {
364         u16  data_size;         /* Size of received packet in bytes     */
365         u16  reserved1;         /* pnc_info - (for future use, PnC)     */
366         u32  status;            /* Info about received packet           */
367
368         u32  reserved2;         /* pnc_flow_id  (for future use, PnC)   */
369         u32  buf_phys_addr;     /* Physical address of the buffer       */
370
371         u16  reserved4;         /* csum_l4 - (for future use, PnC)      */
372         u16  reserved3;         /* prefetch_cmd, for future use         */
373         u32  buf_cookie;        /* cookie for access to RX buffer in rx path */
374
375         u32  reserved5;         /* pnc_extra PnC (for future use, PnC)  */
376         u32  reserved6;         /* hw_cmd (for future use, PnC and HWF) */
377 };
378 #endif
379
380 struct mvneta_tx_queue {
381         /* Number of this TX queue, in the range 0-7 */
382         u8 id;
383
384         /* Number of TX DMA descriptors in the descriptor ring */
385         int size;
386
387         /* Number of currently used TX DMA descriptor in the
388          * descriptor ring
389          */
390         int count;
391
392         /* Array of transmitted skb */
393         struct sk_buff **tx_skb;
394
395         /* Index of last TX DMA descriptor that was inserted */
396         int txq_put_index;
397
398         /* Index of the TX DMA descriptor to be cleaned up */
399         int txq_get_index;
400
401         u32 done_pkts_coal;
402
403         /* Virtual address of the TX DMA descriptors array */
404         struct mvneta_tx_desc *descs;
405
406         /* DMA address of the TX DMA descriptors array */
407         dma_addr_t descs_phys;
408
409         /* Index of the last TX DMA descriptor */
410         int last_desc;
411
412         /* Index of the next TX DMA descriptor to process */
413         int next_desc_to_proc;
414
415         /* DMA buffers for TSO headers */
416         char *tso_hdrs;
417
418         /* DMA address of TSO headers */
419         dma_addr_t tso_hdrs_phys;
420 };
421
422 struct mvneta_rx_queue {
423         /* rx queue number, in the range 0-7 */
424         u8 id;
425
426         /* num of rx descriptors in the rx descriptor ring */
427         int size;
428
429         /* counter of times when mvneta_refill() failed */
430         int missed;
431
432         u32 pkts_coal;
433         u32 time_coal;
434
435         /* Virtual address of the RX DMA descriptors array */
436         struct mvneta_rx_desc *descs;
437
438         /* DMA address of the RX DMA descriptors array */
439         dma_addr_t descs_phys;
440
441         /* Index of the last RX DMA descriptor */
442         int last_desc;
443
444         /* Index of the next RX DMA descriptor to process */
445         int next_desc_to_proc;
446 };
447
448 /* The hardware supports eight (8) rx queues, but we are only allowing
449  * the first one to be used. Therefore, let's just allocate one queue.
450  */
451 static int rxq_number = 1;
452 static int txq_number = 8;
453
454 static int rxq_def;
455
456 static int rx_copybreak __read_mostly = 256;
457
458 #define MVNETA_DRIVER_NAME "mvneta"
459 #define MVNETA_DRIVER_VERSION "1.0"
460
461 /* Utility/helper methods */
462
463 /* Write helper method */
464 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
465 {
466         writel(data, pp->base + offset);
467 }
468
469 /* Read helper method */
470 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
471 {
472         return readl(pp->base + offset);
473 }
474
475 /* Increment txq get counter */
476 static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
477 {
478         txq->txq_get_index++;
479         if (txq->txq_get_index == txq->size)
480                 txq->txq_get_index = 0;
481 }
482
483 /* Increment txq put counter */
484 static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
485 {
486         txq->txq_put_index++;
487         if (txq->txq_put_index == txq->size)
488                 txq->txq_put_index = 0;
489 }
490
491
492 /* Clear all MIB counters */
493 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
494 {
495         int i;
496         u32 dummy;
497
498         /* Perform dummy reads from MIB counters */
499         for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
500                 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
501 }
502
503 /* Get System Network Statistics */
504 struct rtnl_link_stats64 *mvneta_get_stats64(struct net_device *dev,
505                                              struct rtnl_link_stats64 *stats)
506 {
507         struct mvneta_port *pp = netdev_priv(dev);
508         unsigned int start;
509         int cpu;
510
511         for_each_possible_cpu(cpu) {
512                 struct mvneta_pcpu_stats *cpu_stats;
513                 u64 rx_packets;
514                 u64 rx_bytes;
515                 u64 tx_packets;
516                 u64 tx_bytes;
517
518                 cpu_stats = per_cpu_ptr(pp->stats, cpu);
519                 do {
520                         start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
521                         rx_packets = cpu_stats->rx_packets;
522                         rx_bytes   = cpu_stats->rx_bytes;
523                         tx_packets = cpu_stats->tx_packets;
524                         tx_bytes   = cpu_stats->tx_bytes;
525                 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
526
527                 stats->rx_packets += rx_packets;
528                 stats->rx_bytes   += rx_bytes;
529                 stats->tx_packets += tx_packets;
530                 stats->tx_bytes   += tx_bytes;
531         }
532
533         stats->rx_errors        = dev->stats.rx_errors;
534         stats->rx_dropped       = dev->stats.rx_dropped;
535
536         stats->tx_dropped       = dev->stats.tx_dropped;
537
538         return stats;
539 }
540
541 /* Rx descriptors helper methods */
542
543 /* Checks whether the RX descriptor having this status is both the first
544  * and the last descriptor for the RX packet. Each RX packet is currently
545  * received through a single RX descriptor, so not having each RX
546  * descriptor with its first and last bits set is an error
547  */
548 static int mvneta_rxq_desc_is_first_last(u32 status)
549 {
550         return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
551                 MVNETA_RXD_FIRST_LAST_DESC;
552 }
553
554 /* Add number of descriptors ready to receive new packets */
555 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
556                                           struct mvneta_rx_queue *rxq,
557                                           int ndescs)
558 {
559         /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
560          * be added at once
561          */
562         while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
563                 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
564                             (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
565                              MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
566                 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
567         }
568
569         mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
570                     (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
571 }
572
573 /* Get number of RX descriptors occupied by received packets */
574 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
575                                         struct mvneta_rx_queue *rxq)
576 {
577         u32 val;
578
579         val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
580         return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
581 }
582
583 /* Update num of rx desc called upon return from rx path or
584  * from mvneta_rxq_drop_pkts().
585  */
586 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
587                                        struct mvneta_rx_queue *rxq,
588                                        int rx_done, int rx_filled)
589 {
590         u32 val;
591
592         if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
593                 val = rx_done |
594                   (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
595                 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
596                 return;
597         }
598
599         /* Only 255 descriptors can be added at once */
600         while ((rx_done > 0) || (rx_filled > 0)) {
601                 if (rx_done <= 0xff) {
602                         val = rx_done;
603                         rx_done = 0;
604                 } else {
605                         val = 0xff;
606                         rx_done -= 0xff;
607                 }
608                 if (rx_filled <= 0xff) {
609                         val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
610                         rx_filled = 0;
611                 } else {
612                         val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
613                         rx_filled -= 0xff;
614                 }
615                 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
616         }
617 }
618
619 /* Get pointer to next RX descriptor to be processed by SW */
620 static struct mvneta_rx_desc *
621 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
622 {
623         int rx_desc = rxq->next_desc_to_proc;
624
625         rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
626         prefetch(rxq->descs + rxq->next_desc_to_proc);
627         return rxq->descs + rx_desc;
628 }
629
630 /* Change maximum receive size of the port. */
631 static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
632 {
633         u32 val;
634
635         val =  mvreg_read(pp, MVNETA_GMAC_CTRL_0);
636         val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
637         val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
638                 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
639         mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
640 }
641
642
643 /* Set rx queue offset */
644 static void mvneta_rxq_offset_set(struct mvneta_port *pp,
645                                   struct mvneta_rx_queue *rxq,
646                                   int offset)
647 {
648         u32 val;
649
650         val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
651         val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
652
653         /* Offset is in */
654         val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
655         mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
656 }
657
658
659 /* Tx descriptors helper methods */
660
661 /* Update HW with number of TX descriptors to be sent */
662 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
663                                      struct mvneta_tx_queue *txq,
664                                      int pend_desc)
665 {
666         u32 val;
667
668         /* Only 255 descriptors can be added at once ; Assume caller
669          * process TX desriptors in quanta less than 256
670          */
671         val = pend_desc;
672         mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
673 }
674
675 /* Get pointer to next TX descriptor to be processed (send) by HW */
676 static struct mvneta_tx_desc *
677 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
678 {
679         int tx_desc = txq->next_desc_to_proc;
680
681         txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
682         return txq->descs + tx_desc;
683 }
684
685 /* Release the last allocated TX descriptor. Useful to handle DMA
686  * mapping failures in the TX path.
687  */
688 static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
689 {
690         if (txq->next_desc_to_proc == 0)
691                 txq->next_desc_to_proc = txq->last_desc - 1;
692         else
693                 txq->next_desc_to_proc--;
694 }
695
696 /* Set rxq buf size */
697 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
698                                     struct mvneta_rx_queue *rxq,
699                                     int buf_size)
700 {
701         u32 val;
702
703         val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
704
705         val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
706         val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
707
708         mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
709 }
710
711 /* Disable buffer management (BM) */
712 static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
713                                   struct mvneta_rx_queue *rxq)
714 {
715         u32 val;
716
717         val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
718         val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
719         mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
720 }
721
722 /* Start the Ethernet port RX and TX activity */
723 static void mvneta_port_up(struct mvneta_port *pp)
724 {
725         int queue;
726         u32 q_map;
727
728         /* Enable all initialized TXs. */
729         mvneta_mib_counters_clear(pp);
730         q_map = 0;
731         for (queue = 0; queue < txq_number; queue++) {
732                 struct mvneta_tx_queue *txq = &pp->txqs[queue];
733                 if (txq->descs != NULL)
734                         q_map |= (1 << queue);
735         }
736         mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
737
738         /* Enable all initialized RXQs. */
739         q_map = 0;
740         for (queue = 0; queue < rxq_number; queue++) {
741                 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
742                 if (rxq->descs != NULL)
743                         q_map |= (1 << queue);
744         }
745
746         mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
747 }
748
749 /* Stop the Ethernet port activity */
750 static void mvneta_port_down(struct mvneta_port *pp)
751 {
752         u32 val;
753         int count;
754
755         /* Stop Rx port activity. Check port Rx activity. */
756         val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
757
758         /* Issue stop command for active channels only */
759         if (val != 0)
760                 mvreg_write(pp, MVNETA_RXQ_CMD,
761                             val << MVNETA_RXQ_DISABLE_SHIFT);
762
763         /* Wait for all Rx activity to terminate. */
764         count = 0;
765         do {
766                 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
767                         netdev_warn(pp->dev,
768                                     "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
769                                     val);
770                         break;
771                 }
772                 mdelay(1);
773
774                 val = mvreg_read(pp, MVNETA_RXQ_CMD);
775         } while (val & 0xff);
776
777         /* Stop Tx port activity. Check port Tx activity. Issue stop
778          * command for active channels only
779          */
780         val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
781
782         if (val != 0)
783                 mvreg_write(pp, MVNETA_TXQ_CMD,
784                             (val << MVNETA_TXQ_DISABLE_SHIFT));
785
786         /* Wait for all Tx activity to terminate. */
787         count = 0;
788         do {
789                 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
790                         netdev_warn(pp->dev,
791                                     "TIMEOUT for TX stopped status=0x%08x\n",
792                                     val);
793                         break;
794                 }
795                 mdelay(1);
796
797                 /* Check TX Command reg that all Txqs are stopped */
798                 val = mvreg_read(pp, MVNETA_TXQ_CMD);
799
800         } while (val & 0xff);
801
802         /* Double check to verify that TX FIFO is empty */
803         count = 0;
804         do {
805                 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
806                         netdev_warn(pp->dev,
807                                     "TX FIFO empty timeout status=0x08%x\n",
808                                     val);
809                         break;
810                 }
811                 mdelay(1);
812
813                 val = mvreg_read(pp, MVNETA_PORT_STATUS);
814         } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
815                  (val & MVNETA_TX_IN_PRGRS));
816
817         udelay(200);
818 }
819
820 /* Enable the port by setting the port enable bit of the MAC control register */
821 static void mvneta_port_enable(struct mvneta_port *pp)
822 {
823         u32 val;
824
825         /* Enable port */
826         val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
827         val |= MVNETA_GMAC0_PORT_ENABLE;
828         mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
829 }
830
831 /* Disable the port and wait for about 200 usec before retuning */
832 static void mvneta_port_disable(struct mvneta_port *pp)
833 {
834         u32 val;
835
836         /* Reset the Enable bit in the Serial Control Register */
837         val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
838         val &= ~MVNETA_GMAC0_PORT_ENABLE;
839         mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
840
841         udelay(200);
842 }
843
844 /* Multicast tables methods */
845
846 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
847 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
848 {
849         int offset;
850         u32 val;
851
852         if (queue == -1) {
853                 val = 0;
854         } else {
855                 val = 0x1 | (queue << 1);
856                 val |= (val << 24) | (val << 16) | (val << 8);
857         }
858
859         for (offset = 0; offset <= 0xc; offset += 4)
860                 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
861 }
862
863 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
864 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
865 {
866         int offset;
867         u32 val;
868
869         if (queue == -1) {
870                 val = 0;
871         } else {
872                 val = 0x1 | (queue << 1);
873                 val |= (val << 24) | (val << 16) | (val << 8);
874         }
875
876         for (offset = 0; offset <= 0xfc; offset += 4)
877                 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
878
879 }
880
881 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
882 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
883 {
884         int offset;
885         u32 val;
886
887         if (queue == -1) {
888                 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
889                 val = 0;
890         } else {
891                 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
892                 val = 0x1 | (queue << 1);
893                 val |= (val << 24) | (val << 16) | (val << 8);
894         }
895
896         for (offset = 0; offset <= 0xfc; offset += 4)
897                 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
898 }
899
900 /* This method sets defaults to the NETA port:
901  *      Clears interrupt Cause and Mask registers.
902  *      Clears all MAC tables.
903  *      Sets defaults to all registers.
904  *      Resets RX and TX descriptor rings.
905  *      Resets PHY.
906  * This method can be called after mvneta_port_down() to return the port
907  *      settings to defaults.
908  */
909 static void mvneta_defaults_set(struct mvneta_port *pp)
910 {
911         int cpu;
912         int queue;
913         u32 val;
914
915         /* Clear all Cause registers */
916         mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
917         mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
918         mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
919
920         /* Mask all interrupts */
921         mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
922         mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
923         mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
924         mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
925
926         /* Enable MBUS Retry bit16 */
927         mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
928
929         /* Set CPU queue access map - all CPUs have access to all RX
930          * queues and to all TX queues
931          */
932         for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
933                 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
934                             (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
935                              MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
936
937         /* Reset RX and TX DMAs */
938         mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
939         mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
940
941         /* Disable Legacy WRR, Disable EJP, Release from reset */
942         mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
943         for (queue = 0; queue < txq_number; queue++) {
944                 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
945                 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
946         }
947
948         mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
949         mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
950
951         /* Set Port Acceleration Mode */
952         val = MVNETA_ACC_MODE_EXT;
953         mvreg_write(pp, MVNETA_ACC_MODE, val);
954
955         /* Update val of portCfg register accordingly with all RxQueue types */
956         val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
957         mvreg_write(pp, MVNETA_PORT_CONFIG, val);
958
959         val = 0;
960         mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
961         mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
962
963         /* Build PORT_SDMA_CONFIG_REG */
964         val = 0;
965
966         /* Default burst size */
967         val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
968         val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
969         val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
970
971 #if defined(__BIG_ENDIAN)
972         val |= MVNETA_DESC_SWAP;
973 #endif
974
975         /* Assign port SDMA configuration */
976         mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
977
978         /* Disable PHY polling in hardware, since we're using the
979          * kernel phylib to do this.
980          */
981         val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
982         val &= ~MVNETA_PHY_POLLING_ENABLE;
983         mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
984
985         mvneta_set_ucast_table(pp, -1);
986         mvneta_set_special_mcast_table(pp, -1);
987         mvneta_set_other_mcast_table(pp, -1);
988
989         /* Set port interrupt enable register - default enable all */
990         mvreg_write(pp, MVNETA_INTR_ENABLE,
991                     (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
992                      | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
993 }
994
995 /* Set max sizes for tx queues */
996 static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
997
998 {
999         u32 val, size, mtu;
1000         int queue;
1001
1002         mtu = max_tx_size * 8;
1003         if (mtu > MVNETA_TX_MTU_MAX)
1004                 mtu = MVNETA_TX_MTU_MAX;
1005
1006         /* Set MTU */
1007         val = mvreg_read(pp, MVNETA_TX_MTU);
1008         val &= ~MVNETA_TX_MTU_MAX;
1009         val |= mtu;
1010         mvreg_write(pp, MVNETA_TX_MTU, val);
1011
1012         /* TX token size and all TXQs token size must be larger that MTU */
1013         val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1014
1015         size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1016         if (size < mtu) {
1017                 size = mtu;
1018                 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1019                 val |= size;
1020                 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1021         }
1022         for (queue = 0; queue < txq_number; queue++) {
1023                 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1024
1025                 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1026                 if (size < mtu) {
1027                         size = mtu;
1028                         val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1029                         val |= size;
1030                         mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1031                 }
1032         }
1033 }
1034
1035 /* Set unicast address */
1036 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1037                                   int queue)
1038 {
1039         unsigned int unicast_reg;
1040         unsigned int tbl_offset;
1041         unsigned int reg_offset;
1042
1043         /* Locate the Unicast table entry */
1044         last_nibble = (0xf & last_nibble);
1045
1046         /* offset from unicast tbl base */
1047         tbl_offset = (last_nibble / 4) * 4;
1048
1049         /* offset within the above reg  */
1050         reg_offset = last_nibble % 4;
1051
1052         unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1053
1054         if (queue == -1) {
1055                 /* Clear accepts frame bit at specified unicast DA tbl entry */
1056                 unicast_reg &= ~(0xff << (8 * reg_offset));
1057         } else {
1058                 unicast_reg &= ~(0xff << (8 * reg_offset));
1059                 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1060         }
1061
1062         mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1063 }
1064
1065 /* Set mac address */
1066 static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1067                                 int queue)
1068 {
1069         unsigned int mac_h;
1070         unsigned int mac_l;
1071
1072         if (queue != -1) {
1073                 mac_l = (addr[4] << 8) | (addr[5]);
1074                 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1075                         (addr[2] << 8) | (addr[3] << 0);
1076
1077                 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1078                 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1079         }
1080
1081         /* Accept frames of this address */
1082         mvneta_set_ucast_addr(pp, addr[5], queue);
1083 }
1084
1085 /* Set the number of packets that will be received before RX interrupt
1086  * will be generated by HW.
1087  */
1088 static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1089                                     struct mvneta_rx_queue *rxq, u32 value)
1090 {
1091         mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1092                     value | MVNETA_RXQ_NON_OCCUPIED(0));
1093         rxq->pkts_coal = value;
1094 }
1095
1096 /* Set the time delay in usec before RX interrupt will be generated by
1097  * HW.
1098  */
1099 static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1100                                     struct mvneta_rx_queue *rxq, u32 value)
1101 {
1102         u32 val;
1103         unsigned long clk_rate;
1104
1105         clk_rate = clk_get_rate(pp->clk);
1106         val = (clk_rate / 1000000) * value;
1107
1108         mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1109         rxq->time_coal = value;
1110 }
1111
1112 /* Set threshold for TX_DONE pkts coalescing */
1113 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1114                                          struct mvneta_tx_queue *txq, u32 value)
1115 {
1116         u32 val;
1117
1118         val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1119
1120         val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1121         val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1122
1123         mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1124
1125         txq->done_pkts_coal = value;
1126 }
1127
1128 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1129 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1130                                 u32 phys_addr, u32 cookie)
1131 {
1132         rx_desc->buf_cookie = cookie;
1133         rx_desc->buf_phys_addr = phys_addr;
1134 }
1135
1136 /* Decrement sent descriptors counter */
1137 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1138                                      struct mvneta_tx_queue *txq,
1139                                      int sent_desc)
1140 {
1141         u32 val;
1142
1143         /* Only 255 TX descriptors can be updated at once */
1144         while (sent_desc > 0xff) {
1145                 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1146                 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1147                 sent_desc = sent_desc - 0xff;
1148         }
1149
1150         val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1151         mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1152 }
1153
1154 /* Get number of TX descriptors already sent by HW */
1155 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1156                                         struct mvneta_tx_queue *txq)
1157 {
1158         u32 val;
1159         int sent_desc;
1160
1161         val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1162         sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1163                 MVNETA_TXQ_SENT_DESC_SHIFT;
1164
1165         return sent_desc;
1166 }
1167
1168 /* Get number of sent descriptors and decrement counter.
1169  *  The number of sent descriptors is returned.
1170  */
1171 static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1172                                      struct mvneta_tx_queue *txq)
1173 {
1174         int sent_desc;
1175
1176         /* Get number of sent descriptors */
1177         sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1178
1179         /* Decrement sent descriptors counter */
1180         if (sent_desc)
1181                 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1182
1183         return sent_desc;
1184 }
1185
1186 /* Set TXQ descriptors fields relevant for CSUM calculation */
1187 static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1188                                 int ip_hdr_len, int l4_proto)
1189 {
1190         u32 command;
1191
1192         /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
1193          * G_L4_chk, L4_type; required only for checksum
1194          * calculation
1195          */
1196         command =  l3_offs    << MVNETA_TX_L3_OFF_SHIFT;
1197         command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1198
1199         if (l3_proto == swab16(ETH_P_IP))
1200                 command |= MVNETA_TXD_IP_CSUM;
1201         else
1202                 command |= MVNETA_TX_L3_IP6;
1203
1204         if (l4_proto == IPPROTO_TCP)
1205                 command |=  MVNETA_TX_L4_CSUM_FULL;
1206         else if (l4_proto == IPPROTO_UDP)
1207                 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1208         else
1209                 command |= MVNETA_TX_L4_CSUM_NOT;
1210
1211         return command;
1212 }
1213
1214
1215 /* Display more error info */
1216 static void mvneta_rx_error(struct mvneta_port *pp,
1217                             struct mvneta_rx_desc *rx_desc)
1218 {
1219         u32 status = rx_desc->status;
1220
1221         if (!mvneta_rxq_desc_is_first_last(status)) {
1222                 netdev_err(pp->dev,
1223                            "bad rx status %08x (buffer oversize), size=%d\n",
1224                            status, rx_desc->data_size);
1225                 return;
1226         }
1227
1228         switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1229         case MVNETA_RXD_ERR_CRC:
1230                 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1231                            status, rx_desc->data_size);
1232                 break;
1233         case MVNETA_RXD_ERR_OVERRUN:
1234                 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1235                            status, rx_desc->data_size);
1236                 break;
1237         case MVNETA_RXD_ERR_LEN:
1238                 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1239                            status, rx_desc->data_size);
1240                 break;
1241         case MVNETA_RXD_ERR_RESOURCE:
1242                 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1243                            status, rx_desc->data_size);
1244                 break;
1245         }
1246 }
1247
1248 /* Handle RX checksum offload based on the descriptor's status */
1249 static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
1250                            struct sk_buff *skb)
1251 {
1252         if ((status & MVNETA_RXD_L3_IP4) &&
1253             (status & MVNETA_RXD_L4_CSUM_OK)) {
1254                 skb->csum = 0;
1255                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1256                 return;
1257         }
1258
1259         skb->ip_summed = CHECKSUM_NONE;
1260 }
1261
1262 /* Return tx queue pointer (find last set bit) according to <cause> returned
1263  * form tx_done reg. <cause> must not be null. The return value is always a
1264  * valid queue for matching the first one found in <cause>.
1265  */
1266 static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1267                                                      u32 cause)
1268 {
1269         int queue = fls(cause) - 1;
1270
1271         return &pp->txqs[queue];
1272 }
1273
1274 /* Free tx queue skbuffs */
1275 static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1276                                  struct mvneta_tx_queue *txq, int num)
1277 {
1278         int i;
1279
1280         for (i = 0; i < num; i++) {
1281                 struct mvneta_tx_desc *tx_desc = txq->descs +
1282                         txq->txq_get_index;
1283                 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1284
1285                 mvneta_txq_inc_get(txq);
1286
1287                 if (!skb)
1288                         continue;
1289
1290                 dma_unmap_single(pp->dev->dev.parent, tx_desc->buf_phys_addr,
1291                                  tx_desc->data_size, DMA_TO_DEVICE);
1292                 dev_kfree_skb_any(skb);
1293         }
1294 }
1295
1296 /* Handle end of transmission */
1297 static void mvneta_txq_done(struct mvneta_port *pp,
1298                            struct mvneta_tx_queue *txq)
1299 {
1300         struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1301         int tx_done;
1302
1303         tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1304         if (!tx_done)
1305                 return;
1306
1307         mvneta_txq_bufs_free(pp, txq, tx_done);
1308
1309         txq->count -= tx_done;
1310
1311         if (netif_tx_queue_stopped(nq)) {
1312                 if (txq->size - txq->count >= MAX_SKB_FRAGS + 1)
1313                         netif_tx_wake_queue(nq);
1314         }
1315 }
1316
1317 static void *mvneta_frag_alloc(const struct mvneta_port *pp)
1318 {
1319         if (likely(pp->frag_size <= PAGE_SIZE))
1320                 return netdev_alloc_frag(pp->frag_size);
1321         else
1322                 return kmalloc(pp->frag_size, GFP_ATOMIC);
1323 }
1324
1325 static void mvneta_frag_free(const struct mvneta_port *pp, void *data)
1326 {
1327         if (likely(pp->frag_size <= PAGE_SIZE))
1328                 put_page(virt_to_head_page(data));
1329         else
1330                 kfree(data);
1331 }
1332
1333 /* Refill processing */
1334 static int mvneta_rx_refill(struct mvneta_port *pp,
1335                             struct mvneta_rx_desc *rx_desc)
1336
1337 {
1338         dma_addr_t phys_addr;
1339         void *data;
1340
1341         data = mvneta_frag_alloc(pp);
1342         if (!data)
1343                 return -ENOMEM;
1344
1345         phys_addr = dma_map_single(pp->dev->dev.parent, data,
1346                                    MVNETA_RX_BUF_SIZE(pp->pkt_size),
1347                                    DMA_FROM_DEVICE);
1348         if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
1349                 mvneta_frag_free(pp, data);
1350                 return -ENOMEM;
1351         }
1352
1353         mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)data);
1354         return 0;
1355 }
1356
1357 /* Handle tx checksum */
1358 static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1359 {
1360         if (skb->ip_summed == CHECKSUM_PARTIAL) {
1361                 int ip_hdr_len = 0;
1362                 u8 l4_proto;
1363
1364                 if (skb->protocol == htons(ETH_P_IP)) {
1365                         struct iphdr *ip4h = ip_hdr(skb);
1366
1367                         /* Calculate IPv4 checksum and L4 checksum */
1368                         ip_hdr_len = ip4h->ihl;
1369                         l4_proto = ip4h->protocol;
1370                 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1371                         struct ipv6hdr *ip6h = ipv6_hdr(skb);
1372
1373                         /* Read l4_protocol from one of IPv6 extra headers */
1374                         if (skb_network_header_len(skb) > 0)
1375                                 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1376                         l4_proto = ip6h->nexthdr;
1377                 } else
1378                         return MVNETA_TX_L4_CSUM_NOT;
1379
1380                 return mvneta_txq_desc_csum(skb_network_offset(skb),
1381                                 skb->protocol, ip_hdr_len, l4_proto);
1382         }
1383
1384         return MVNETA_TX_L4_CSUM_NOT;
1385 }
1386
1387 /* Returns rx queue pointer (find last set bit) according to causeRxTx
1388  * value
1389  */
1390 static struct mvneta_rx_queue *mvneta_rx_policy(struct mvneta_port *pp,
1391                                                 u32 cause)
1392 {
1393         int queue = fls(cause >> 8) - 1;
1394
1395         return (queue < 0 || queue >= rxq_number) ? NULL : &pp->rxqs[queue];
1396 }
1397
1398 /* Drop packets received by the RXQ and free buffers */
1399 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1400                                  struct mvneta_rx_queue *rxq)
1401 {
1402         int rx_done, i;
1403
1404         rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1405         for (i = 0; i < rxq->size; i++) {
1406                 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1407                 void *data = (void *)rx_desc->buf_cookie;
1408
1409                 mvneta_frag_free(pp, data);
1410                 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
1411                                  MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
1412         }
1413
1414         if (rx_done)
1415                 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1416 }
1417
1418 /* Main rx processing */
1419 static int mvneta_rx(struct mvneta_port *pp, int rx_todo,
1420                      struct mvneta_rx_queue *rxq)
1421 {
1422         struct net_device *dev = pp->dev;
1423         int rx_done, rx_filled;
1424         u32 rcvd_pkts = 0;
1425         u32 rcvd_bytes = 0;
1426
1427         /* Get number of received packets */
1428         rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1429
1430         if (rx_todo > rx_done)
1431                 rx_todo = rx_done;
1432
1433         rx_done = 0;
1434         rx_filled = 0;
1435
1436         /* Fairness NAPI loop */
1437         while (rx_done < rx_todo) {
1438                 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1439                 struct sk_buff *skb;
1440                 unsigned char *data;
1441                 u32 rx_status;
1442                 int rx_bytes, err;
1443
1444                 rx_done++;
1445                 rx_filled++;
1446                 rx_status = rx_desc->status;
1447                 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
1448                 data = (unsigned char *)rx_desc->buf_cookie;
1449
1450                 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1451                     (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1452                 err_drop_frame:
1453                         dev->stats.rx_errors++;
1454                         mvneta_rx_error(pp, rx_desc);
1455                         /* leave the descriptor untouched */
1456                         continue;
1457                 }
1458
1459                 if (rx_bytes <= rx_copybreak) {
1460                         /* better copy a small frame and not unmap the DMA region */
1461                         skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1462                         if (unlikely(!skb))
1463                                 goto err_drop_frame;
1464
1465                         dma_sync_single_range_for_cpu(dev->dev.parent,
1466                                                       rx_desc->buf_phys_addr,
1467                                                       MVNETA_MH_SIZE + NET_SKB_PAD,
1468                                                       rx_bytes,
1469                                                       DMA_FROM_DEVICE);
1470                         memcpy(skb_put(skb, rx_bytes),
1471                                data + MVNETA_MH_SIZE + NET_SKB_PAD,
1472                                rx_bytes);
1473
1474                         skb->protocol = eth_type_trans(skb, dev);
1475                         mvneta_rx_csum(pp, rx_status, skb);
1476                         napi_gro_receive(&pp->napi, skb);
1477
1478                         rcvd_pkts++;
1479                         rcvd_bytes += rx_bytes;
1480
1481                         /* leave the descriptor and buffer untouched */
1482                         continue;
1483                 }
1484
1485                 skb = build_skb(data, pp->frag_size > PAGE_SIZE ? 0 : pp->frag_size);
1486                 if (!skb)
1487                         goto err_drop_frame;
1488
1489                 dma_unmap_single(dev->dev.parent, rx_desc->buf_phys_addr,
1490                                  MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
1491
1492                 rcvd_pkts++;
1493                 rcvd_bytes += rx_bytes;
1494
1495                 /* Linux processing */
1496                 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
1497                 skb_put(skb, rx_bytes);
1498
1499                 skb->protocol = eth_type_trans(skb, dev);
1500
1501                 mvneta_rx_csum(pp, rx_status, skb);
1502
1503                 napi_gro_receive(&pp->napi, skb);
1504
1505                 /* Refill processing */
1506                 err = mvneta_rx_refill(pp, rx_desc);
1507                 if (err) {
1508                         netdev_err(dev, "Linux processing - Can't refill\n");
1509                         rxq->missed++;
1510                         rx_filled--;
1511                 }
1512         }
1513
1514         if (rcvd_pkts) {
1515                 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1516
1517                 u64_stats_update_begin(&stats->syncp);
1518                 stats->rx_packets += rcvd_pkts;
1519                 stats->rx_bytes   += rcvd_bytes;
1520                 u64_stats_update_end(&stats->syncp);
1521         }
1522
1523         /* Update rxq management counters */
1524         mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_filled);
1525
1526         return rx_done;
1527 }
1528
1529 static inline void
1530 mvneta_tso_put_hdr(struct sk_buff *skb,
1531                    struct mvneta_port *pp, struct mvneta_tx_queue *txq)
1532 {
1533         struct mvneta_tx_desc *tx_desc;
1534         int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1535
1536         txq->tx_skb[txq->txq_put_index] = NULL;
1537         tx_desc = mvneta_txq_next_desc_get(txq);
1538         tx_desc->data_size = hdr_len;
1539         tx_desc->command = mvneta_skb_tx_csum(pp, skb);
1540         tx_desc->command |= MVNETA_TXD_F_DESC;
1541         tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
1542                                  txq->txq_put_index * TSO_HEADER_SIZE;
1543         mvneta_txq_inc_put(txq);
1544 }
1545
1546 static inline int
1547 mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
1548                     struct sk_buff *skb, char *data, int size,
1549                     bool last_tcp, bool is_last)
1550 {
1551         struct mvneta_tx_desc *tx_desc;
1552
1553         tx_desc = mvneta_txq_next_desc_get(txq);
1554         tx_desc->data_size = size;
1555         tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
1556                                                 size, DMA_TO_DEVICE);
1557         if (unlikely(dma_mapping_error(dev->dev.parent,
1558                      tx_desc->buf_phys_addr))) {
1559                 mvneta_txq_desc_put(txq);
1560                 return -ENOMEM;
1561         }
1562
1563         tx_desc->command = 0;
1564         txq->tx_skb[txq->txq_put_index] = NULL;
1565
1566         if (last_tcp) {
1567                 /* last descriptor in the TCP packet */
1568                 tx_desc->command = MVNETA_TXD_L_DESC;
1569
1570                 /* last descriptor in SKB */
1571                 if (is_last)
1572                         txq->tx_skb[txq->txq_put_index] = skb;
1573         }
1574         mvneta_txq_inc_put(txq);
1575         return 0;
1576 }
1577
1578 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
1579                          struct mvneta_tx_queue *txq)
1580 {
1581         int total_len, data_left;
1582         int desc_count = 0;
1583         struct mvneta_port *pp = netdev_priv(dev);
1584         struct tso_t tso;
1585         int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1586         int i;
1587
1588         /* Count needed descriptors */
1589         if ((txq->count + tso_count_descs(skb)) >= txq->size)
1590                 return 0;
1591
1592         if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
1593                 pr_info("*** Is this even  possible???!?!?\n");
1594                 return 0;
1595         }
1596
1597         /* Initialize the TSO handler, and prepare the first payload */
1598         tso_start(skb, &tso);
1599
1600         total_len = skb->len - hdr_len;
1601         while (total_len > 0) {
1602                 char *hdr;
1603
1604                 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1605                 total_len -= data_left;
1606                 desc_count++;
1607
1608                 /* prepare packet headers: MAC + IP + TCP */
1609                 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
1610                 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
1611
1612                 mvneta_tso_put_hdr(skb, pp, txq);
1613
1614                 while (data_left > 0) {
1615                         int size;
1616                         desc_count++;
1617
1618                         size = min_t(int, tso.size, data_left);
1619
1620                         if (mvneta_tso_put_data(dev, txq, skb,
1621                                                  tso.data, size,
1622                                                  size == data_left,
1623                                                  total_len == 0))
1624                                 goto err_release;
1625                         data_left -= size;
1626
1627                         tso_build_data(skb, &tso, size);
1628                 }
1629         }
1630
1631         return desc_count;
1632
1633 err_release:
1634         /* Release all used data descriptors; header descriptors must not
1635          * be DMA-unmapped.
1636          */
1637         for (i = desc_count - 1; i >= 0; i--) {
1638                 struct mvneta_tx_desc *tx_desc = txq->descs + i;
1639                 if (!(tx_desc->command & MVNETA_TXD_F_DESC))
1640                         dma_unmap_single(pp->dev->dev.parent,
1641                                          tx_desc->buf_phys_addr,
1642                                          tx_desc->data_size,
1643                                          DMA_TO_DEVICE);
1644                 mvneta_txq_desc_put(txq);
1645         }
1646         return 0;
1647 }
1648
1649 /* Handle tx fragmentation processing */
1650 static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
1651                                   struct mvneta_tx_queue *txq)
1652 {
1653         struct mvneta_tx_desc *tx_desc;
1654         int i, nr_frags = skb_shinfo(skb)->nr_frags;
1655
1656         for (i = 0; i < nr_frags; i++) {
1657                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1658                 void *addr = page_address(frag->page.p) + frag->page_offset;
1659
1660                 tx_desc = mvneta_txq_next_desc_get(txq);
1661                 tx_desc->data_size = frag->size;
1662
1663                 tx_desc->buf_phys_addr =
1664                         dma_map_single(pp->dev->dev.parent, addr,
1665                                        tx_desc->data_size, DMA_TO_DEVICE);
1666
1667                 if (dma_mapping_error(pp->dev->dev.parent,
1668                                       tx_desc->buf_phys_addr)) {
1669                         mvneta_txq_desc_put(txq);
1670                         goto error;
1671                 }
1672
1673                 if (i == nr_frags - 1) {
1674                         /* Last descriptor */
1675                         tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
1676                         txq->tx_skb[txq->txq_put_index] = skb;
1677                 } else {
1678                         /* Descriptor in the middle: Not First, Not Last */
1679                         tx_desc->command = 0;
1680                         txq->tx_skb[txq->txq_put_index] = NULL;
1681                 }
1682                 mvneta_txq_inc_put(txq);
1683         }
1684
1685         return 0;
1686
1687 error:
1688         /* Release all descriptors that were used to map fragments of
1689          * this packet, as well as the corresponding DMA mappings
1690          */
1691         for (i = i - 1; i >= 0; i--) {
1692                 tx_desc = txq->descs + i;
1693                 dma_unmap_single(pp->dev->dev.parent,
1694                                  tx_desc->buf_phys_addr,
1695                                  tx_desc->data_size,
1696                                  DMA_TO_DEVICE);
1697                 mvneta_txq_desc_put(txq);
1698         }
1699
1700         return -ENOMEM;
1701 }
1702
1703 /* Main tx processing */
1704 static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
1705 {
1706         struct mvneta_port *pp = netdev_priv(dev);
1707         u16 txq_id = skb_get_queue_mapping(skb);
1708         struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
1709         struct mvneta_tx_desc *tx_desc;
1710         int frags = 0;
1711         u32 tx_cmd;
1712
1713         if (!netif_running(dev))
1714                 goto out;
1715
1716         if (skb_is_gso(skb)) {
1717                 frags = mvneta_tx_tso(skb, dev, txq);
1718                 goto out;
1719         }
1720
1721         frags = skb_shinfo(skb)->nr_frags + 1;
1722
1723         /* Get a descriptor for the first part of the packet */
1724         tx_desc = mvneta_txq_next_desc_get(txq);
1725
1726         tx_cmd = mvneta_skb_tx_csum(pp, skb);
1727
1728         tx_desc->data_size = skb_headlen(skb);
1729
1730         tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
1731                                                 tx_desc->data_size,
1732                                                 DMA_TO_DEVICE);
1733         if (unlikely(dma_mapping_error(dev->dev.parent,
1734                                        tx_desc->buf_phys_addr))) {
1735                 mvneta_txq_desc_put(txq);
1736                 frags = 0;
1737                 goto out;
1738         }
1739
1740         if (frags == 1) {
1741                 /* First and Last descriptor */
1742                 tx_cmd |= MVNETA_TXD_FLZ_DESC;
1743                 tx_desc->command = tx_cmd;
1744                 txq->tx_skb[txq->txq_put_index] = skb;
1745                 mvneta_txq_inc_put(txq);
1746         } else {
1747                 /* First but not Last */
1748                 tx_cmd |= MVNETA_TXD_F_DESC;
1749                 txq->tx_skb[txq->txq_put_index] = NULL;
1750                 mvneta_txq_inc_put(txq);
1751                 tx_desc->command = tx_cmd;
1752                 /* Continue with other skb fragments */
1753                 if (mvneta_tx_frag_process(pp, skb, txq)) {
1754                         dma_unmap_single(dev->dev.parent,
1755                                          tx_desc->buf_phys_addr,
1756                                          tx_desc->data_size,
1757                                          DMA_TO_DEVICE);
1758                         mvneta_txq_desc_put(txq);
1759                         frags = 0;
1760                         goto out;
1761                 }
1762         }
1763
1764 out:
1765         if (frags > 0) {
1766                 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1767                 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
1768
1769                 txq->count += frags;
1770                 mvneta_txq_pend_desc_add(pp, txq, frags);
1771
1772                 if (txq->size - txq->count < MAX_SKB_FRAGS + 1)
1773                         netif_tx_stop_queue(nq);
1774
1775                 u64_stats_update_begin(&stats->syncp);
1776                 stats->tx_packets++;
1777                 stats->tx_bytes  += skb->len;
1778                 u64_stats_update_end(&stats->syncp);
1779         } else {
1780                 dev->stats.tx_dropped++;
1781                 dev_kfree_skb_any(skb);
1782         }
1783
1784         return NETDEV_TX_OK;
1785 }
1786
1787
1788 /* Free tx resources, when resetting a port */
1789 static void mvneta_txq_done_force(struct mvneta_port *pp,
1790                                   struct mvneta_tx_queue *txq)
1791
1792 {
1793         int tx_done = txq->count;
1794
1795         mvneta_txq_bufs_free(pp, txq, tx_done);
1796
1797         /* reset txq */
1798         txq->count = 0;
1799         txq->txq_put_index = 0;
1800         txq->txq_get_index = 0;
1801 }
1802
1803 /* Handle tx done - called in softirq context. The <cause_tx_done> argument
1804  * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
1805  */
1806 static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
1807 {
1808         struct mvneta_tx_queue *txq;
1809         struct netdev_queue *nq;
1810
1811         while (cause_tx_done) {
1812                 txq = mvneta_tx_done_policy(pp, cause_tx_done);
1813
1814                 nq = netdev_get_tx_queue(pp->dev, txq->id);
1815                 __netif_tx_lock(nq, smp_processor_id());
1816
1817                 if (txq->count)
1818                         mvneta_txq_done(pp, txq);
1819
1820                 __netif_tx_unlock(nq);
1821                 cause_tx_done &= ~((1 << txq->id));
1822         }
1823 }
1824
1825 /* Compute crc8 of the specified address, using a unique algorithm ,
1826  * according to hw spec, different than generic crc8 algorithm
1827  */
1828 static int mvneta_addr_crc(unsigned char *addr)
1829 {
1830         int crc = 0;
1831         int i;
1832
1833         for (i = 0; i < ETH_ALEN; i++) {
1834                 int j;
1835
1836                 crc = (crc ^ addr[i]) << 8;
1837                 for (j = 7; j >= 0; j--) {
1838                         if (crc & (0x100 << j))
1839                                 crc ^= 0x107 << j;
1840                 }
1841         }
1842
1843         return crc;
1844 }
1845
1846 /* This method controls the net device special MAC multicast support.
1847  * The Special Multicast Table for MAC addresses supports MAC of the form
1848  * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1849  * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1850  * Table entries in the DA-Filter table. This method set the Special
1851  * Multicast Table appropriate entry.
1852  */
1853 static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
1854                                           unsigned char last_byte,
1855                                           int queue)
1856 {
1857         unsigned int smc_table_reg;
1858         unsigned int tbl_offset;
1859         unsigned int reg_offset;
1860
1861         /* Register offset from SMC table base    */
1862         tbl_offset = (last_byte / 4);
1863         /* Entry offset within the above reg */
1864         reg_offset = last_byte % 4;
1865
1866         smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
1867                                         + tbl_offset * 4));
1868
1869         if (queue == -1)
1870                 smc_table_reg &= ~(0xff << (8 * reg_offset));
1871         else {
1872                 smc_table_reg &= ~(0xff << (8 * reg_offset));
1873                 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1874         }
1875
1876         mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
1877                     smc_table_reg);
1878 }
1879
1880 /* This method controls the network device Other MAC multicast support.
1881  * The Other Multicast Table is used for multicast of another type.
1882  * A CRC-8 is used as an index to the Other Multicast Table entries
1883  * in the DA-Filter table.
1884  * The method gets the CRC-8 value from the calling routine and
1885  * sets the Other Multicast Table appropriate entry according to the
1886  * specified CRC-8 .
1887  */
1888 static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
1889                                         unsigned char crc8,
1890                                         int queue)
1891 {
1892         unsigned int omc_table_reg;
1893         unsigned int tbl_offset;
1894         unsigned int reg_offset;
1895
1896         tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
1897         reg_offset = crc8 % 4;       /* Entry offset within the above reg   */
1898
1899         omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
1900
1901         if (queue == -1) {
1902                 /* Clear accepts frame bit at specified Other DA table entry */
1903                 omc_table_reg &= ~(0xff << (8 * reg_offset));
1904         } else {
1905                 omc_table_reg &= ~(0xff << (8 * reg_offset));
1906                 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1907         }
1908
1909         mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
1910 }
1911
1912 /* The network device supports multicast using two tables:
1913  *    1) Special Multicast Table for MAC addresses of the form
1914  *       0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1915  *       The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1916  *       Table entries in the DA-Filter table.
1917  *    2) Other Multicast Table for multicast of another type. A CRC-8 value
1918  *       is used as an index to the Other Multicast Table entries in the
1919  *       DA-Filter table.
1920  */
1921 static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
1922                                  int queue)
1923 {
1924         unsigned char crc_result = 0;
1925
1926         if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
1927                 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
1928                 return 0;
1929         }
1930
1931         crc_result = mvneta_addr_crc(p_addr);
1932         if (queue == -1) {
1933                 if (pp->mcast_count[crc_result] == 0) {
1934                         netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
1935                                     crc_result);
1936                         return -EINVAL;
1937                 }
1938
1939                 pp->mcast_count[crc_result]--;
1940                 if (pp->mcast_count[crc_result] != 0) {
1941                         netdev_info(pp->dev,
1942                                     "After delete there are %d valid Mcast for crc8=0x%02x\n",
1943                                     pp->mcast_count[crc_result], crc_result);
1944                         return -EINVAL;
1945                 }
1946         } else
1947                 pp->mcast_count[crc_result]++;
1948
1949         mvneta_set_other_mcast_addr(pp, crc_result, queue);
1950
1951         return 0;
1952 }
1953
1954 /* Configure Fitering mode of Ethernet port */
1955 static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
1956                                           int is_promisc)
1957 {
1958         u32 port_cfg_reg, val;
1959
1960         port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
1961
1962         val = mvreg_read(pp, MVNETA_TYPE_PRIO);
1963
1964         /* Set / Clear UPM bit in port configuration register */
1965         if (is_promisc) {
1966                 /* Accept all Unicast addresses */
1967                 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
1968                 val |= MVNETA_FORCE_UNI;
1969                 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
1970                 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
1971         } else {
1972                 /* Reject all Unicast addresses */
1973                 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
1974                 val &= ~MVNETA_FORCE_UNI;
1975         }
1976
1977         mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
1978         mvreg_write(pp, MVNETA_TYPE_PRIO, val);
1979 }
1980
1981 /* register unicast and multicast addresses */
1982 static void mvneta_set_rx_mode(struct net_device *dev)
1983 {
1984         struct mvneta_port *pp = netdev_priv(dev);
1985         struct netdev_hw_addr *ha;
1986
1987         if (dev->flags & IFF_PROMISC) {
1988                 /* Accept all: Multicast + Unicast */
1989                 mvneta_rx_unicast_promisc_set(pp, 1);
1990                 mvneta_set_ucast_table(pp, rxq_def);
1991                 mvneta_set_special_mcast_table(pp, rxq_def);
1992                 mvneta_set_other_mcast_table(pp, rxq_def);
1993         } else {
1994                 /* Accept single Unicast */
1995                 mvneta_rx_unicast_promisc_set(pp, 0);
1996                 mvneta_set_ucast_table(pp, -1);
1997                 mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
1998
1999                 if (dev->flags & IFF_ALLMULTI) {
2000                         /* Accept all multicast */
2001                         mvneta_set_special_mcast_table(pp, rxq_def);
2002                         mvneta_set_other_mcast_table(pp, rxq_def);
2003                 } else {
2004                         /* Accept only initialized multicast */
2005                         mvneta_set_special_mcast_table(pp, -1);
2006                         mvneta_set_other_mcast_table(pp, -1);
2007
2008                         if (!netdev_mc_empty(dev)) {
2009                                 netdev_for_each_mc_addr(ha, dev) {
2010                                         mvneta_mcast_addr_set(pp, ha->addr,
2011                                                               rxq_def);
2012                                 }
2013                         }
2014                 }
2015         }
2016 }
2017
2018 /* Interrupt handling - the callback for request_irq() */
2019 static irqreturn_t mvneta_isr(int irq, void *dev_id)
2020 {
2021         struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2022
2023         /* Mask all interrupts */
2024         mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2025
2026         napi_schedule(&pp->napi);
2027
2028         return IRQ_HANDLED;
2029 }
2030
2031 /* NAPI handler
2032  * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2033  * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2034  * Bits 8 -15 of the cause Rx Tx register indicate that are received
2035  * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2036  * Each CPU has its own causeRxTx register
2037  */
2038 static int mvneta_poll(struct napi_struct *napi, int budget)
2039 {
2040         int rx_done = 0;
2041         u32 cause_rx_tx;
2042         unsigned long flags;
2043         struct mvneta_port *pp = netdev_priv(napi->dev);
2044
2045         if (!netif_running(pp->dev)) {
2046                 napi_complete(napi);
2047                 return rx_done;
2048         }
2049
2050         /* Read cause register */
2051         cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE) &
2052                 (MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
2053
2054         /* Release Tx descriptors */
2055         if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
2056                 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
2057                 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2058         }
2059
2060         /* For the case where the last mvneta_poll did not process all
2061          * RX packets
2062          */
2063         cause_rx_tx |= pp->cause_rx_tx;
2064         if (rxq_number > 1) {
2065                 while ((cause_rx_tx & MVNETA_RX_INTR_MASK_ALL) && (budget > 0)) {
2066                         int count;
2067                         struct mvneta_rx_queue *rxq;
2068                         /* get rx queue number from cause_rx_tx */
2069                         rxq = mvneta_rx_policy(pp, cause_rx_tx);
2070                         if (!rxq)
2071                                 break;
2072
2073                         /* process the packet in that rx queue */
2074                         count = mvneta_rx(pp, budget, rxq);
2075                         rx_done += count;
2076                         budget -= count;
2077                         if (budget > 0) {
2078                                 /* set off the rx bit of the
2079                                  * corresponding bit in the cause rx
2080                                  * tx register, so that next iteration
2081                                  * will find the next rx queue where
2082                                  * packets are received on
2083                                  */
2084                                 cause_rx_tx &= ~((1 << rxq->id) << 8);
2085                         }
2086                 }
2087         } else {
2088                 rx_done = mvneta_rx(pp, budget, &pp->rxqs[rxq_def]);
2089                 budget -= rx_done;
2090         }
2091
2092         if (budget > 0) {
2093                 cause_rx_tx = 0;
2094                 napi_complete(napi);
2095                 local_irq_save(flags);
2096                 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2097                             MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
2098                 local_irq_restore(flags);
2099         }
2100
2101         pp->cause_rx_tx = cause_rx_tx;
2102         return rx_done;
2103 }
2104
2105 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2106 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2107                            int num)
2108 {
2109         int i;
2110
2111         for (i = 0; i < num; i++) {
2112                 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2113                 if (mvneta_rx_refill(pp, rxq->descs + i) != 0) {
2114                         netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs  filled\n",
2115                                 __func__, rxq->id, i, num);
2116                         break;
2117                 }
2118         }
2119
2120         /* Add this number of RX descriptors as non occupied (ready to
2121          * get packets)
2122          */
2123         mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2124
2125         return i;
2126 }
2127
2128 /* Free all packets pending transmit from all TXQs and reset TX port */
2129 static void mvneta_tx_reset(struct mvneta_port *pp)
2130 {
2131         int queue;
2132
2133         /* free the skb's in the tx ring */
2134         for (queue = 0; queue < txq_number; queue++)
2135                 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2136
2137         mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2138         mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2139 }
2140
2141 static void mvneta_rx_reset(struct mvneta_port *pp)
2142 {
2143         mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2144         mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2145 }
2146
2147 /* Rx/Tx queue initialization/cleanup methods */
2148
2149 /* Create a specified RX queue */
2150 static int mvneta_rxq_init(struct mvneta_port *pp,
2151                            struct mvneta_rx_queue *rxq)
2152
2153 {
2154         rxq->size = pp->rx_ring_size;
2155
2156         /* Allocate memory for RX descriptors */
2157         rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2158                                         rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2159                                         &rxq->descs_phys, GFP_KERNEL);
2160         if (rxq->descs == NULL)
2161                 return -ENOMEM;
2162
2163         BUG_ON(rxq->descs !=
2164                PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2165
2166         rxq->last_desc = rxq->size - 1;
2167
2168         /* Set Rx descriptors queue starting address */
2169         mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2170         mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2171
2172         /* Set Offset */
2173         mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2174
2175         /* Set coalescing pkts and time */
2176         mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2177         mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2178
2179         /* Fill RXQ with buffers from RX pool */
2180         mvneta_rxq_buf_size_set(pp, rxq, MVNETA_RX_BUF_SIZE(pp->pkt_size));
2181         mvneta_rxq_bm_disable(pp, rxq);
2182         mvneta_rxq_fill(pp, rxq, rxq->size);
2183
2184         return 0;
2185 }
2186
2187 /* Cleanup Rx queue */
2188 static void mvneta_rxq_deinit(struct mvneta_port *pp,
2189                               struct mvneta_rx_queue *rxq)
2190 {
2191         mvneta_rxq_drop_pkts(pp, rxq);
2192
2193         if (rxq->descs)
2194                 dma_free_coherent(pp->dev->dev.parent,
2195                                   rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2196                                   rxq->descs,
2197                                   rxq->descs_phys);
2198
2199         rxq->descs             = NULL;
2200         rxq->last_desc         = 0;
2201         rxq->next_desc_to_proc = 0;
2202         rxq->descs_phys        = 0;
2203 }
2204
2205 /* Create and initialize a tx queue */
2206 static int mvneta_txq_init(struct mvneta_port *pp,
2207                            struct mvneta_tx_queue *txq)
2208 {
2209         txq->size = pp->tx_ring_size;
2210
2211         /* Allocate memory for TX descriptors */
2212         txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2213                                         txq->size * MVNETA_DESC_ALIGNED_SIZE,
2214                                         &txq->descs_phys, GFP_KERNEL);
2215         if (txq->descs == NULL)
2216                 return -ENOMEM;
2217
2218         /* Make sure descriptor address is cache line size aligned  */
2219         BUG_ON(txq->descs !=
2220                PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2221
2222         txq->last_desc = txq->size - 1;
2223
2224         /* Set maximum bandwidth for enabled TXQs */
2225         mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2226         mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2227
2228         /* Set Tx descriptors queue starting address */
2229         mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2230         mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2231
2232         txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2233         if (txq->tx_skb == NULL) {
2234                 dma_free_coherent(pp->dev->dev.parent,
2235                                   txq->size * MVNETA_DESC_ALIGNED_SIZE,
2236                                   txq->descs, txq->descs_phys);
2237                 return -ENOMEM;
2238         }
2239
2240         /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2241         txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2242                                            txq->size * TSO_HEADER_SIZE,
2243                                            &txq->tso_hdrs_phys, GFP_KERNEL);
2244         if (txq->tso_hdrs == NULL) {
2245                 kfree(txq->tx_skb);
2246                 dma_free_coherent(pp->dev->dev.parent,
2247                                   txq->size * MVNETA_DESC_ALIGNED_SIZE,
2248                                   txq->descs, txq->descs_phys);
2249                 return -ENOMEM;
2250         }
2251         mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2252
2253         return 0;
2254 }
2255
2256 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2257 static void mvneta_txq_deinit(struct mvneta_port *pp,
2258                               struct mvneta_tx_queue *txq)
2259 {
2260         kfree(txq->tx_skb);
2261
2262         if (txq->tso_hdrs)
2263                 dma_free_coherent(pp->dev->dev.parent,
2264                                   txq->size * TSO_HEADER_SIZE,
2265                                   txq->tso_hdrs, txq->tso_hdrs_phys);
2266         if (txq->descs)
2267                 dma_free_coherent(pp->dev->dev.parent,
2268                                   txq->size * MVNETA_DESC_ALIGNED_SIZE,
2269                                   txq->descs, txq->descs_phys);
2270
2271         txq->descs             = NULL;
2272         txq->last_desc         = 0;
2273         txq->next_desc_to_proc = 0;
2274         txq->descs_phys        = 0;
2275
2276         /* Set minimum bandwidth for disabled TXQs */
2277         mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2278         mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2279
2280         /* Set Tx descriptors queue starting address and size */
2281         mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2282         mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2283 }
2284
2285 /* Cleanup all Tx queues */
2286 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2287 {
2288         int queue;
2289
2290         for (queue = 0; queue < txq_number; queue++)
2291                 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2292 }
2293
2294 /* Cleanup all Rx queues */
2295 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2296 {
2297         int queue;
2298
2299         for (queue = 0; queue < rxq_number; queue++)
2300                 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
2301 }
2302
2303
2304 /* Init all Rx queues */
2305 static int mvneta_setup_rxqs(struct mvneta_port *pp)
2306 {
2307         int queue;
2308
2309         for (queue = 0; queue < rxq_number; queue++) {
2310                 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2311                 if (err) {
2312                         netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2313                                    __func__, queue);
2314                         mvneta_cleanup_rxqs(pp);
2315                         return err;
2316                 }
2317         }
2318
2319         return 0;
2320 }
2321
2322 /* Init all tx queues */
2323 static int mvneta_setup_txqs(struct mvneta_port *pp)
2324 {
2325         int queue;
2326
2327         for (queue = 0; queue < txq_number; queue++) {
2328                 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2329                 if (err) {
2330                         netdev_err(pp->dev, "%s: can't create txq=%d\n",
2331                                    __func__, queue);
2332                         mvneta_cleanup_txqs(pp);
2333                         return err;
2334                 }
2335         }
2336
2337         return 0;
2338 }
2339
2340 static void mvneta_start_dev(struct mvneta_port *pp)
2341 {
2342         mvneta_max_rx_size_set(pp, pp->pkt_size);
2343         mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2344
2345         /* start the Rx/Tx activity */
2346         mvneta_port_enable(pp);
2347
2348         /* Enable polling on the port */
2349         napi_enable(&pp->napi);
2350
2351         /* Unmask interrupts */
2352         mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2353                     MVNETA_RX_INTR_MASK(rxq_number) | MVNETA_TX_INTR_MASK(txq_number));
2354
2355         phy_start(pp->phy_dev);
2356         netif_tx_start_all_queues(pp->dev);
2357 }
2358
2359 static void mvneta_stop_dev(struct mvneta_port *pp)
2360 {
2361         phy_stop(pp->phy_dev);
2362
2363         napi_disable(&pp->napi);
2364
2365         netif_carrier_off(pp->dev);
2366
2367         mvneta_port_down(pp);
2368         netif_tx_stop_all_queues(pp->dev);
2369
2370         /* Stop the port activity */
2371         mvneta_port_disable(pp);
2372
2373         /* Clear all ethernet port interrupts */
2374         mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2375         mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
2376
2377         /* Mask all ethernet port interrupts */
2378         mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2379         mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
2380         mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
2381
2382         mvneta_tx_reset(pp);
2383         mvneta_rx_reset(pp);
2384 }
2385
2386 /* Return positive if MTU is valid */
2387 static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
2388 {
2389         if (mtu < 68) {
2390                 netdev_err(dev, "cannot change mtu to less than 68\n");
2391                 return -EINVAL;
2392         }
2393
2394         /* 9676 == 9700 - 20 and rounding to 8 */
2395         if (mtu > 9676) {
2396                 netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
2397                 mtu = 9676;
2398         }
2399
2400         if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
2401                 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
2402                         mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
2403                 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
2404         }
2405
2406         return mtu;
2407 }
2408
2409 /* Change the device mtu */
2410 static int mvneta_change_mtu(struct net_device *dev, int mtu)
2411 {
2412         struct mvneta_port *pp = netdev_priv(dev);
2413         int ret;
2414
2415         mtu = mvneta_check_mtu_valid(dev, mtu);
2416         if (mtu < 0)
2417                 return -EINVAL;
2418
2419         dev->mtu = mtu;
2420
2421         if (!netif_running(dev))
2422                 return 0;
2423
2424         /* The interface is running, so we have to force a
2425          * reallocation of the queues
2426          */
2427         mvneta_stop_dev(pp);
2428
2429         mvneta_cleanup_txqs(pp);
2430         mvneta_cleanup_rxqs(pp);
2431
2432         pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
2433         pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
2434                         SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2435
2436         ret = mvneta_setup_rxqs(pp);
2437         if (ret) {
2438                 netdev_err(dev, "unable to setup rxqs after MTU change\n");
2439                 return ret;
2440         }
2441
2442         ret = mvneta_setup_txqs(pp);
2443         if (ret) {
2444                 netdev_err(dev, "unable to setup txqs after MTU change\n");
2445                 return ret;
2446         }
2447
2448         mvneta_start_dev(pp);
2449         mvneta_port_up(pp);
2450
2451         return 0;
2452 }
2453
2454 /* Get mac address */
2455 static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
2456 {
2457         u32 mac_addr_l, mac_addr_h;
2458
2459         mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
2460         mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
2461         addr[0] = (mac_addr_h >> 24) & 0xFF;
2462         addr[1] = (mac_addr_h >> 16) & 0xFF;
2463         addr[2] = (mac_addr_h >> 8) & 0xFF;
2464         addr[3] = mac_addr_h & 0xFF;
2465         addr[4] = (mac_addr_l >> 8) & 0xFF;
2466         addr[5] = mac_addr_l & 0xFF;
2467 }
2468
2469 /* Handle setting mac address */
2470 static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
2471 {
2472         struct mvneta_port *pp = netdev_priv(dev);
2473         struct sockaddr *sockaddr = addr;
2474         int ret;
2475
2476         ret = eth_prepare_mac_addr_change(dev, addr);
2477         if (ret < 0)
2478                 return ret;
2479         /* Remove previous address table entry */
2480         mvneta_mac_addr_set(pp, dev->dev_addr, -1);
2481
2482         /* Set new addr in hw */
2483         mvneta_mac_addr_set(pp, sockaddr->sa_data, rxq_def);
2484
2485         eth_commit_mac_addr_change(dev, addr);
2486         return 0;
2487 }
2488
2489 static void mvneta_adjust_link(struct net_device *ndev)
2490 {
2491         struct mvneta_port *pp = netdev_priv(ndev);
2492         struct phy_device *phydev = pp->phy_dev;
2493         int status_change = 0;
2494
2495         if (phydev->link) {
2496                 if ((pp->speed != phydev->speed) ||
2497                     (pp->duplex != phydev->duplex)) {
2498                         u32 val;
2499
2500                         val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2501                         val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
2502                                  MVNETA_GMAC_CONFIG_GMII_SPEED |
2503                                  MVNETA_GMAC_CONFIG_FULL_DUPLEX |
2504                                  MVNETA_GMAC_AN_SPEED_EN |
2505                                  MVNETA_GMAC_AN_DUPLEX_EN);
2506
2507                         if (phydev->duplex)
2508                                 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
2509
2510                         if (phydev->speed == SPEED_1000)
2511                                 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
2512                         else
2513                                 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
2514
2515                         mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2516
2517                         pp->duplex = phydev->duplex;
2518                         pp->speed  = phydev->speed;
2519                 }
2520         }
2521
2522         if (phydev->link != pp->link) {
2523                 if (!phydev->link) {
2524                         pp->duplex = -1;
2525                         pp->speed = 0;
2526                 }
2527
2528                 pp->link = phydev->link;
2529                 status_change = 1;
2530         }
2531
2532         if (status_change) {
2533                 if (phydev->link) {
2534                         u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2535                         val |= (MVNETA_GMAC_FORCE_LINK_PASS |
2536                                 MVNETA_GMAC_FORCE_LINK_DOWN);
2537                         mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2538                         mvneta_port_up(pp);
2539                         netdev_info(pp->dev, "link up\n");
2540                 } else {
2541                         mvneta_port_down(pp);
2542                         netdev_info(pp->dev, "link down\n");
2543                 }
2544         }
2545 }
2546
2547 static int mvneta_mdio_probe(struct mvneta_port *pp)
2548 {
2549         struct phy_device *phy_dev;
2550
2551         phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
2552                                  pp->phy_interface);
2553         if (!phy_dev) {
2554                 netdev_err(pp->dev, "could not find the PHY\n");
2555                 return -ENODEV;
2556         }
2557
2558         phy_dev->supported &= PHY_GBIT_FEATURES;
2559         phy_dev->advertising = phy_dev->supported;
2560
2561         pp->phy_dev = phy_dev;
2562         pp->link    = 0;
2563         pp->duplex  = 0;
2564         pp->speed   = 0;
2565
2566         return 0;
2567 }
2568
2569 static void mvneta_mdio_remove(struct mvneta_port *pp)
2570 {
2571         phy_disconnect(pp->phy_dev);
2572         pp->phy_dev = NULL;
2573 }
2574
2575 static int mvneta_open(struct net_device *dev)
2576 {
2577         struct mvneta_port *pp = netdev_priv(dev);
2578         int ret;
2579
2580         pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
2581         pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
2582                         SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2583
2584         ret = mvneta_setup_rxqs(pp);
2585         if (ret)
2586                 return ret;
2587
2588         ret = mvneta_setup_txqs(pp);
2589         if (ret)
2590                 goto err_cleanup_rxqs;
2591
2592         /* Connect to port interrupt line */
2593         ret = request_irq(pp->dev->irq, mvneta_isr, 0,
2594                           MVNETA_DRIVER_NAME, pp);
2595         if (ret) {
2596                 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
2597                 goto err_cleanup_txqs;
2598         }
2599
2600         /* In default link is down */
2601         netif_carrier_off(pp->dev);
2602
2603         ret = mvneta_mdio_probe(pp);
2604         if (ret < 0) {
2605                 netdev_err(dev, "cannot probe MDIO bus\n");
2606                 goto err_free_irq;
2607         }
2608
2609         mvneta_start_dev(pp);
2610
2611         return 0;
2612
2613 err_free_irq:
2614         free_irq(pp->dev->irq, pp);
2615 err_cleanup_txqs:
2616         mvneta_cleanup_txqs(pp);
2617 err_cleanup_rxqs:
2618         mvneta_cleanup_rxqs(pp);
2619         return ret;
2620 }
2621
2622 /* Stop the port, free port interrupt line */
2623 static int mvneta_stop(struct net_device *dev)
2624 {
2625         struct mvneta_port *pp = netdev_priv(dev);
2626
2627         mvneta_stop_dev(pp);
2628         mvneta_mdio_remove(pp);
2629         free_irq(dev->irq, pp);
2630         mvneta_cleanup_rxqs(pp);
2631         mvneta_cleanup_txqs(pp);
2632
2633         return 0;
2634 }
2635
2636 static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2637 {
2638         struct mvneta_port *pp = netdev_priv(dev);
2639         int ret;
2640
2641         if (!pp->phy_dev)
2642                 return -ENOTSUPP;
2643
2644         ret = phy_mii_ioctl(pp->phy_dev, ifr, cmd);
2645         if (!ret)
2646                 mvneta_adjust_link(dev);
2647
2648         return ret;
2649 }
2650
2651 /* Ethtool methods */
2652
2653 /* Get settings (phy address, speed) for ethtools */
2654 int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2655 {
2656         struct mvneta_port *pp = netdev_priv(dev);
2657
2658         if (!pp->phy_dev)
2659                 return -ENODEV;
2660
2661         return phy_ethtool_gset(pp->phy_dev, cmd);
2662 }
2663
2664 /* Set settings (phy address, speed) for ethtools */
2665 int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2666 {
2667         struct mvneta_port *pp = netdev_priv(dev);
2668
2669         if (!pp->phy_dev)
2670                 return -ENODEV;
2671
2672         return phy_ethtool_sset(pp->phy_dev, cmd);
2673 }
2674
2675 /* Set interrupt coalescing for ethtools */
2676 static int mvneta_ethtool_set_coalesce(struct net_device *dev,
2677                                        struct ethtool_coalesce *c)
2678 {
2679         struct mvneta_port *pp = netdev_priv(dev);
2680         int queue;
2681
2682         for (queue = 0; queue < rxq_number; queue++) {
2683                 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2684                 rxq->time_coal = c->rx_coalesce_usecs;
2685                 rxq->pkts_coal = c->rx_max_coalesced_frames;
2686                 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2687                 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2688         }
2689
2690         for (queue = 0; queue < txq_number; queue++) {
2691                 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2692                 txq->done_pkts_coal = c->tx_max_coalesced_frames;
2693                 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2694         }
2695
2696         return 0;
2697 }
2698
2699 /* get coalescing for ethtools */
2700 static int mvneta_ethtool_get_coalesce(struct net_device *dev,
2701                                        struct ethtool_coalesce *c)
2702 {
2703         struct mvneta_port *pp = netdev_priv(dev);
2704
2705         c->rx_coalesce_usecs        = pp->rxqs[0].time_coal;
2706         c->rx_max_coalesced_frames  = pp->rxqs[0].pkts_coal;
2707
2708         c->tx_max_coalesced_frames =  pp->txqs[0].done_pkts_coal;
2709         return 0;
2710 }
2711
2712
2713 static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
2714                                     struct ethtool_drvinfo *drvinfo)
2715 {
2716         strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
2717                 sizeof(drvinfo->driver));
2718         strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
2719                 sizeof(drvinfo->version));
2720         strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
2721                 sizeof(drvinfo->bus_info));
2722 }
2723
2724
2725 static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
2726                                          struct ethtool_ringparam *ring)
2727 {
2728         struct mvneta_port *pp = netdev_priv(netdev);
2729
2730         ring->rx_max_pending = MVNETA_MAX_RXD;
2731         ring->tx_max_pending = MVNETA_MAX_TXD;
2732         ring->rx_pending = pp->rx_ring_size;
2733         ring->tx_pending = pp->tx_ring_size;
2734 }
2735
2736 static int mvneta_ethtool_set_ringparam(struct net_device *dev,
2737                                         struct ethtool_ringparam *ring)
2738 {
2739         struct mvneta_port *pp = netdev_priv(dev);
2740
2741         if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
2742                 return -EINVAL;
2743         pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
2744                 ring->rx_pending : MVNETA_MAX_RXD;
2745         pp->tx_ring_size = ring->tx_pending < MVNETA_MAX_TXD ?
2746                 ring->tx_pending : MVNETA_MAX_TXD;
2747
2748         if (netif_running(dev)) {
2749                 mvneta_stop(dev);
2750                 if (mvneta_open(dev)) {
2751                         netdev_err(dev,
2752                                    "error on opening device after ring param change\n");
2753                         return -ENOMEM;
2754                 }
2755         }
2756
2757         return 0;
2758 }
2759
2760 static const struct net_device_ops mvneta_netdev_ops = {
2761         .ndo_open            = mvneta_open,
2762         .ndo_stop            = mvneta_stop,
2763         .ndo_start_xmit      = mvneta_tx,
2764         .ndo_set_rx_mode     = mvneta_set_rx_mode,
2765         .ndo_set_mac_address = mvneta_set_mac_addr,
2766         .ndo_change_mtu      = mvneta_change_mtu,
2767         .ndo_get_stats64     = mvneta_get_stats64,
2768         .ndo_do_ioctl        = mvneta_ioctl,
2769 };
2770
2771 const struct ethtool_ops mvneta_eth_tool_ops = {
2772         .get_link       = ethtool_op_get_link,
2773         .get_settings   = mvneta_ethtool_get_settings,
2774         .set_settings   = mvneta_ethtool_set_settings,
2775         .set_coalesce   = mvneta_ethtool_set_coalesce,
2776         .get_coalesce   = mvneta_ethtool_get_coalesce,
2777         .get_drvinfo    = mvneta_ethtool_get_drvinfo,
2778         .get_ringparam  = mvneta_ethtool_get_ringparam,
2779         .set_ringparam  = mvneta_ethtool_set_ringparam,
2780 };
2781
2782 /* Initialize hw */
2783 static int mvneta_init(struct device *dev, struct mvneta_port *pp)
2784 {
2785         int queue;
2786
2787         /* Disable port */
2788         mvneta_port_disable(pp);
2789
2790         /* Set port default values */
2791         mvneta_defaults_set(pp);
2792
2793         pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
2794                                 GFP_KERNEL);
2795         if (!pp->txqs)
2796                 return -ENOMEM;
2797
2798         /* Initialize TX descriptor rings */
2799         for (queue = 0; queue < txq_number; queue++) {
2800                 struct mvneta_tx_queue *txq = &pp->txqs[queue];
2801                 txq->id = queue;
2802                 txq->size = pp->tx_ring_size;
2803                 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
2804         }
2805
2806         pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
2807                                 GFP_KERNEL);
2808         if (!pp->rxqs)
2809                 return -ENOMEM;
2810
2811         /* Create Rx descriptor rings */
2812         for (queue = 0; queue < rxq_number; queue++) {
2813                 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2814                 rxq->id = queue;
2815                 rxq->size = pp->rx_ring_size;
2816                 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
2817                 rxq->time_coal = MVNETA_RX_COAL_USEC;
2818         }
2819
2820         return 0;
2821 }
2822
2823 /* platform glue : initialize decoding windows */
2824 static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
2825                                      const struct mbus_dram_target_info *dram)
2826 {
2827         u32 win_enable;
2828         u32 win_protect;
2829         int i;
2830
2831         for (i = 0; i < 6; i++) {
2832                 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
2833                 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
2834
2835                 if (i < 4)
2836                         mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
2837         }
2838
2839         win_enable = 0x3f;
2840         win_protect = 0;
2841
2842         for (i = 0; i < dram->num_cs; i++) {
2843                 const struct mbus_dram_window *cs = dram->cs + i;
2844                 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
2845                             (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
2846
2847                 mvreg_write(pp, MVNETA_WIN_SIZE(i),
2848                             (cs->size - 1) & 0xffff0000);
2849
2850                 win_enable &= ~(1 << i);
2851                 win_protect |= 3 << (2 * i);
2852         }
2853
2854         mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
2855 }
2856
2857 /* Power up the port */
2858 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
2859 {
2860         u32 ctrl;
2861
2862         /* MAC Cause register should be cleared */
2863         mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
2864
2865         ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
2866
2867         /* Even though it might look weird, when we're configured in
2868          * SGMII or QSGMII mode, the RGMII bit needs to be set.
2869          */
2870         switch(phy_mode) {
2871         case PHY_INTERFACE_MODE_QSGMII:
2872                 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
2873                 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
2874                 break;
2875         case PHY_INTERFACE_MODE_SGMII:
2876                 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
2877                 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
2878                 break;
2879         case PHY_INTERFACE_MODE_RGMII:
2880         case PHY_INTERFACE_MODE_RGMII_ID:
2881                 ctrl |= MVNETA_GMAC2_PORT_RGMII;
2882                 break;
2883         default:
2884                 return -EINVAL;
2885         }
2886
2887         /* Cancel Port Reset */
2888         ctrl &= ~MVNETA_GMAC2_PORT_RESET;
2889         mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
2890
2891         while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
2892                 MVNETA_GMAC2_PORT_RESET) != 0)
2893                 continue;
2894
2895         return 0;
2896 }
2897
2898 /* Device initialization routine */
2899 static int mvneta_probe(struct platform_device *pdev)
2900 {
2901         const struct mbus_dram_target_info *dram_target_info;
2902         struct resource *res;
2903         struct device_node *dn = pdev->dev.of_node;
2904         struct device_node *phy_node;
2905         struct mvneta_port *pp;
2906         struct net_device *dev;
2907         const char *dt_mac_addr;
2908         char hw_mac_addr[ETH_ALEN];
2909         const char *mac_from;
2910         int phy_mode;
2911         int err;
2912
2913         /* Our multiqueue support is not complete, so for now, only
2914          * allow the usage of the first RX queue
2915          */
2916         if (rxq_def != 0) {
2917                 dev_err(&pdev->dev, "Invalid rxq_def argument: %d\n", rxq_def);
2918                 return -EINVAL;
2919         }
2920
2921         dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
2922         if (!dev)
2923                 return -ENOMEM;
2924
2925         dev->irq = irq_of_parse_and_map(dn, 0);
2926         if (dev->irq == 0) {
2927                 err = -EINVAL;
2928                 goto err_free_netdev;
2929         }
2930
2931         phy_node = of_parse_phandle(dn, "phy", 0);
2932         if (!phy_node) {
2933                 if (!of_phy_is_fixed_link(dn)) {
2934                         dev_err(&pdev->dev, "no PHY specified\n");
2935                         err = -ENODEV;
2936                         goto err_free_irq;
2937                 }
2938
2939                 err = of_phy_register_fixed_link(dn);
2940                 if (err < 0) {
2941                         dev_err(&pdev->dev, "cannot register fixed PHY\n");
2942                         goto err_free_irq;
2943                 }
2944
2945                 /* In the case of a fixed PHY, the DT node associated
2946                  * to the PHY is the Ethernet MAC DT node.
2947                  */
2948                 phy_node = dn;
2949         }
2950
2951         phy_mode = of_get_phy_mode(dn);
2952         if (phy_mode < 0) {
2953                 dev_err(&pdev->dev, "incorrect phy-mode\n");
2954                 err = -EINVAL;
2955                 goto err_free_irq;
2956         }
2957
2958         dev->tx_queue_len = MVNETA_MAX_TXD;
2959         dev->watchdog_timeo = 5 * HZ;
2960         dev->netdev_ops = &mvneta_netdev_ops;
2961
2962         dev->ethtool_ops = &mvneta_eth_tool_ops;
2963
2964         pp = netdev_priv(dev);
2965         pp->phy_node = phy_node;
2966         pp->phy_interface = phy_mode;
2967
2968         pp->clk = devm_clk_get(&pdev->dev, NULL);
2969         if (IS_ERR(pp->clk)) {
2970                 err = PTR_ERR(pp->clk);
2971                 goto err_free_irq;
2972         }
2973
2974         clk_prepare_enable(pp->clk);
2975
2976         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2977         pp->base = devm_ioremap_resource(&pdev->dev, res);
2978         if (IS_ERR(pp->base)) {
2979                 err = PTR_ERR(pp->base);
2980                 goto err_clk;
2981         }
2982
2983         /* Alloc per-cpu stats */
2984         pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
2985         if (!pp->stats) {
2986                 err = -ENOMEM;
2987                 goto err_clk;
2988         }
2989
2990         dt_mac_addr = of_get_mac_address(dn);
2991         if (dt_mac_addr) {
2992                 mac_from = "device tree";
2993                 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
2994         } else {
2995                 mvneta_get_mac_addr(pp, hw_mac_addr);
2996                 if (is_valid_ether_addr(hw_mac_addr)) {
2997                         mac_from = "hardware";
2998                         memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
2999                 } else {
3000                         mac_from = "random";
3001                         eth_hw_addr_random(dev);
3002                 }
3003         }
3004
3005         pp->tx_ring_size = MVNETA_MAX_TXD;
3006         pp->rx_ring_size = MVNETA_MAX_RXD;
3007
3008         pp->dev = dev;
3009         SET_NETDEV_DEV(dev, &pdev->dev);
3010
3011         err = mvneta_init(&pdev->dev, pp);
3012         if (err < 0)
3013                 goto err_free_stats;
3014
3015         err = mvneta_port_power_up(pp, phy_mode);
3016         if (err < 0) {
3017                 dev_err(&pdev->dev, "can't power up port\n");
3018                 goto err_free_stats;
3019         }
3020
3021         dram_target_info = mv_mbus_dram_info();
3022         if (dram_target_info)
3023                 mvneta_conf_mbus_windows(pp, dram_target_info);
3024
3025         netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
3026
3027         dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
3028         dev->hw_features |= dev->features;
3029         dev->vlan_features |= dev->features;
3030         dev->priv_flags |= IFF_UNICAST_FLT;
3031
3032         err = register_netdev(dev);
3033         if (err < 0) {
3034                 dev_err(&pdev->dev, "failed to register\n");
3035                 goto err_free_stats;
3036         }
3037
3038         netdev_info(dev, "Using %s mac address %pM\n", mac_from,
3039                     dev->dev_addr);
3040
3041         platform_set_drvdata(pdev, pp->dev);
3042
3043         return 0;
3044
3045 err_free_stats:
3046         free_percpu(pp->stats);
3047 err_clk:
3048         clk_disable_unprepare(pp->clk);
3049 err_free_irq:
3050         irq_dispose_mapping(dev->irq);
3051 err_free_netdev:
3052         free_netdev(dev);
3053         return err;
3054 }
3055
3056 /* Device removal routine */
3057 static int mvneta_remove(struct platform_device *pdev)
3058 {
3059         struct net_device  *dev = platform_get_drvdata(pdev);
3060         struct mvneta_port *pp = netdev_priv(dev);
3061
3062         unregister_netdev(dev);
3063         clk_disable_unprepare(pp->clk);
3064         free_percpu(pp->stats);
3065         irq_dispose_mapping(dev->irq);
3066         free_netdev(dev);
3067
3068         return 0;
3069 }
3070
3071 static const struct of_device_id mvneta_match[] = {
3072         { .compatible = "marvell,armada-370-neta" },
3073         { }
3074 };
3075 MODULE_DEVICE_TABLE(of, mvneta_match);
3076
3077 static struct platform_driver mvneta_driver = {
3078         .probe = mvneta_probe,
3079         .remove = mvneta_remove,
3080         .driver = {
3081                 .name = MVNETA_DRIVER_NAME,
3082                 .of_match_table = mvneta_match,
3083         },
3084 };
3085
3086 module_platform_driver(mvneta_driver);
3087
3088 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
3089 MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
3090 MODULE_LICENSE("GPL");
3091
3092 module_param(rxq_number, int, S_IRUGO);
3093 module_param(txq_number, int, S_IRUGO);
3094
3095 module_param(rxq_def, int, S_IRUGO);
3096 module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);