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