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