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