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