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