xref: /openbmc/linux/drivers/net/ethernet/sun/niu.c (revision 3d40aed8)
1c861ef83SShannon Nelson // SPDX-License-Identifier: GPL-2.0
2e689cf4aSJeff Kirsher /* niu.c: Neptune ethernet driver.
3e689cf4aSJeff Kirsher  *
4e689cf4aSJeff Kirsher  * Copyright (C) 2007, 2008 David S. Miller (davem@davemloft.net)
5e689cf4aSJeff Kirsher  */
6e689cf4aSJeff Kirsher 
7e689cf4aSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8e689cf4aSJeff Kirsher 
9e689cf4aSJeff Kirsher #include <linux/module.h>
10e689cf4aSJeff Kirsher #include <linux/init.h>
11e689cf4aSJeff Kirsher #include <linux/interrupt.h>
12e689cf4aSJeff Kirsher #include <linux/pci.h>
13e689cf4aSJeff Kirsher #include <linux/dma-mapping.h>
14e689cf4aSJeff Kirsher #include <linux/netdevice.h>
15e689cf4aSJeff Kirsher #include <linux/ethtool.h>
16e689cf4aSJeff Kirsher #include <linux/etherdevice.h>
17e689cf4aSJeff Kirsher #include <linux/platform_device.h>
18e689cf4aSJeff Kirsher #include <linux/delay.h>
19e689cf4aSJeff Kirsher #include <linux/bitops.h>
20e689cf4aSJeff Kirsher #include <linux/mii.h>
2101789349SJiri Pirko #include <linux/if.h>
22e689cf4aSJeff Kirsher #include <linux/if_ether.h>
23e689cf4aSJeff Kirsher #include <linux/if_vlan.h>
24e689cf4aSJeff Kirsher #include <linux/ip.h>
25e689cf4aSJeff Kirsher #include <linux/in.h>
26e689cf4aSJeff Kirsher #include <linux/ipv6.h>
27e689cf4aSJeff Kirsher #include <linux/log2.h>
28e689cf4aSJeff Kirsher #include <linux/jiffies.h>
29e689cf4aSJeff Kirsher #include <linux/crc32.h>
30e689cf4aSJeff Kirsher #include <linux/list.h>
31e689cf4aSJeff Kirsher #include <linux/slab.h>
32e689cf4aSJeff Kirsher 
33e689cf4aSJeff Kirsher #include <linux/io.h>
34*3d40aed8SRob Herring #include <linux/of.h>
35e689cf4aSJeff Kirsher 
36e689cf4aSJeff Kirsher #include "niu.h"
37e689cf4aSJeff Kirsher 
382dcfe9e2SKees Cook /* This driver wants to store a link to a "next page" within the
392dcfe9e2SKees Cook  * page struct itself by overloading the content of the "mapping"
402dcfe9e2SKees Cook  * member. This is not expected by the page API, but does currently
412dcfe9e2SKees Cook  * work. However, the randstruct plugin gets very bothered by this
422dcfe9e2SKees Cook  * case because "mapping" (struct address_space) is randomized, so
432dcfe9e2SKees Cook  * casts to/from it trigger warnings. Hide this by way of a union,
442dcfe9e2SKees Cook  * to create a typed alias of "mapping", since that's how it is
452dcfe9e2SKees Cook  * actually being used here.
462dcfe9e2SKees Cook  */
472dcfe9e2SKees Cook union niu_page {
482dcfe9e2SKees Cook 	struct page page;
492dcfe9e2SKees Cook 	struct {
502dcfe9e2SKees Cook 		unsigned long __flags;	/* unused alias of "flags" */
512dcfe9e2SKees Cook 		struct list_head __lru;	/* unused alias of "lru" */
522dcfe9e2SKees Cook 		struct page *next;	/* alias of "mapping" */
532dcfe9e2SKees Cook 	};
542dcfe9e2SKees Cook };
552dcfe9e2SKees Cook #define niu_next_page(p)	container_of(p, union niu_page, page)->next
562dcfe9e2SKees Cook 
57e689cf4aSJeff Kirsher #define DRV_MODULE_NAME		"niu"
58e689cf4aSJeff Kirsher #define DRV_MODULE_VERSION	"1.1"
59e689cf4aSJeff Kirsher #define DRV_MODULE_RELDATE	"Apr 22, 2010"
60e689cf4aSJeff Kirsher 
61f73d12bdSBill Pemberton static char version[] =
62e689cf4aSJeff Kirsher 	DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
63e689cf4aSJeff Kirsher 
64e689cf4aSJeff Kirsher MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
65e689cf4aSJeff Kirsher MODULE_DESCRIPTION("NIU ethernet driver");
66e689cf4aSJeff Kirsher MODULE_LICENSE("GPL");
67e689cf4aSJeff Kirsher MODULE_VERSION(DRV_MODULE_VERSION);
68e689cf4aSJeff Kirsher 
69e689cf4aSJeff Kirsher #ifndef readq
readq(void __iomem * reg)70e689cf4aSJeff Kirsher static u64 readq(void __iomem *reg)
71e689cf4aSJeff Kirsher {
72e689cf4aSJeff Kirsher 	return ((u64) readl(reg)) | (((u64) readl(reg + 4UL)) << 32);
73e689cf4aSJeff Kirsher }
74e689cf4aSJeff Kirsher 
writeq(u64 val,void __iomem * reg)75e689cf4aSJeff Kirsher static void writeq(u64 val, void __iomem *reg)
76e689cf4aSJeff Kirsher {
77e689cf4aSJeff Kirsher 	writel(val & 0xffffffff, reg);
78e689cf4aSJeff Kirsher 	writel(val >> 32, reg + 0x4UL);
79e689cf4aSJeff Kirsher }
80e689cf4aSJeff Kirsher #endif
81e689cf4aSJeff Kirsher 
829baa3c34SBenoit Taine static const struct pci_device_id niu_pci_tbl[] = {
83e689cf4aSJeff Kirsher 	{PCI_DEVICE(PCI_VENDOR_ID_SUN, 0xabcd)},
84e689cf4aSJeff Kirsher 	{}
85e689cf4aSJeff Kirsher };
86e689cf4aSJeff Kirsher 
87e689cf4aSJeff Kirsher MODULE_DEVICE_TABLE(pci, niu_pci_tbl);
88e689cf4aSJeff Kirsher 
89e689cf4aSJeff Kirsher #define NIU_TX_TIMEOUT			(5 * HZ)
90e689cf4aSJeff Kirsher 
91e689cf4aSJeff Kirsher #define nr64(reg)		readq(np->regs + (reg))
92e689cf4aSJeff Kirsher #define nw64(reg, val)		writeq((val), np->regs + (reg))
93e689cf4aSJeff Kirsher 
94e689cf4aSJeff Kirsher #define nr64_mac(reg)		readq(np->mac_regs + (reg))
95e689cf4aSJeff Kirsher #define nw64_mac(reg, val)	writeq((val), np->mac_regs + (reg))
96e689cf4aSJeff Kirsher 
97e689cf4aSJeff Kirsher #define nr64_ipp(reg)		readq(np->regs + np->ipp_off + (reg))
98e689cf4aSJeff Kirsher #define nw64_ipp(reg, val)	writeq((val), np->regs + np->ipp_off + (reg))
99e689cf4aSJeff Kirsher 
100e689cf4aSJeff Kirsher #define nr64_pcs(reg)		readq(np->regs + np->pcs_off + (reg))
101e689cf4aSJeff Kirsher #define nw64_pcs(reg, val)	writeq((val), np->regs + np->pcs_off + (reg))
102e689cf4aSJeff Kirsher 
103e689cf4aSJeff Kirsher #define nr64_xpcs(reg)		readq(np->regs + np->xpcs_off + (reg))
104e689cf4aSJeff Kirsher #define nw64_xpcs(reg, val)	writeq((val), np->regs + np->xpcs_off + (reg))
105e689cf4aSJeff Kirsher 
106e689cf4aSJeff Kirsher #define NIU_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
107e689cf4aSJeff Kirsher 
108e689cf4aSJeff Kirsher static int niu_debug;
109e689cf4aSJeff Kirsher static int debug = -1;
110e689cf4aSJeff Kirsher module_param(debug, int, 0);
111e689cf4aSJeff Kirsher MODULE_PARM_DESC(debug, "NIU debug level");
112e689cf4aSJeff Kirsher 
113e689cf4aSJeff Kirsher #define niu_lock_parent(np, flags) \
114e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->parent->lock, flags)
115e689cf4aSJeff Kirsher #define niu_unlock_parent(np, flags) \
116e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->parent->lock, flags)
117e689cf4aSJeff Kirsher 
118e689cf4aSJeff Kirsher static int serdes_init_10g_serdes(struct niu *np);
119e689cf4aSJeff Kirsher 
__niu_wait_bits_clear_mac(struct niu * np,unsigned long reg,u64 bits,int limit,int delay)120e689cf4aSJeff Kirsher static int __niu_wait_bits_clear_mac(struct niu *np, unsigned long reg,
121e689cf4aSJeff Kirsher 				     u64 bits, int limit, int delay)
122e689cf4aSJeff Kirsher {
123e689cf4aSJeff Kirsher 	while (--limit >= 0) {
124e689cf4aSJeff Kirsher 		u64 val = nr64_mac(reg);
125e689cf4aSJeff Kirsher 
126e689cf4aSJeff Kirsher 		if (!(val & bits))
127e689cf4aSJeff Kirsher 			break;
128e689cf4aSJeff Kirsher 		udelay(delay);
129e689cf4aSJeff Kirsher 	}
130e689cf4aSJeff Kirsher 	if (limit < 0)
131e689cf4aSJeff Kirsher 		return -ENODEV;
132e689cf4aSJeff Kirsher 	return 0;
133e689cf4aSJeff Kirsher }
134e689cf4aSJeff Kirsher 
__niu_set_and_wait_clear_mac(struct niu * np,unsigned long reg,u64 bits,int limit,int delay,const char * reg_name)135e689cf4aSJeff Kirsher static int __niu_set_and_wait_clear_mac(struct niu *np, unsigned long reg,
136e689cf4aSJeff Kirsher 					u64 bits, int limit, int delay,
137e689cf4aSJeff Kirsher 					const char *reg_name)
138e689cf4aSJeff Kirsher {
139e689cf4aSJeff Kirsher 	int err;
140e689cf4aSJeff Kirsher 
141e689cf4aSJeff Kirsher 	nw64_mac(reg, bits);
142e689cf4aSJeff Kirsher 	err = __niu_wait_bits_clear_mac(np, reg, bits, limit, delay);
143e689cf4aSJeff Kirsher 	if (err)
144e689cf4aSJeff Kirsher 		netdev_err(np->dev, "bits (%llx) of register %s would not clear, val[%llx]\n",
145e689cf4aSJeff Kirsher 			   (unsigned long long)bits, reg_name,
146e689cf4aSJeff Kirsher 			   (unsigned long long)nr64_mac(reg));
147e689cf4aSJeff Kirsher 	return err;
148e689cf4aSJeff Kirsher }
149e689cf4aSJeff Kirsher 
150e689cf4aSJeff Kirsher #define niu_set_and_wait_clear_mac(NP, REG, BITS, LIMIT, DELAY, REG_NAME) \
151e689cf4aSJeff Kirsher ({	BUILD_BUG_ON(LIMIT <= 0 || DELAY < 0); \
152e689cf4aSJeff Kirsher 	__niu_set_and_wait_clear_mac(NP, REG, BITS, LIMIT, DELAY, REG_NAME); \
153e689cf4aSJeff Kirsher })
154e689cf4aSJeff Kirsher 
__niu_wait_bits_clear_ipp(struct niu * np,unsigned long reg,u64 bits,int limit,int delay)155e689cf4aSJeff Kirsher static int __niu_wait_bits_clear_ipp(struct niu *np, unsigned long reg,
156e689cf4aSJeff Kirsher 				     u64 bits, int limit, int delay)
157e689cf4aSJeff Kirsher {
158e689cf4aSJeff Kirsher 	while (--limit >= 0) {
159e689cf4aSJeff Kirsher 		u64 val = nr64_ipp(reg);
160e689cf4aSJeff Kirsher 
161e689cf4aSJeff Kirsher 		if (!(val & bits))
162e689cf4aSJeff Kirsher 			break;
163e689cf4aSJeff Kirsher 		udelay(delay);
164e689cf4aSJeff Kirsher 	}
165e689cf4aSJeff Kirsher 	if (limit < 0)
166e689cf4aSJeff Kirsher 		return -ENODEV;
167e689cf4aSJeff Kirsher 	return 0;
168e689cf4aSJeff Kirsher }
169e689cf4aSJeff Kirsher 
__niu_set_and_wait_clear_ipp(struct niu * np,unsigned long reg,u64 bits,int limit,int delay,const char * reg_name)170e689cf4aSJeff Kirsher static int __niu_set_and_wait_clear_ipp(struct niu *np, unsigned long reg,
171e689cf4aSJeff Kirsher 					u64 bits, int limit, int delay,
172e689cf4aSJeff Kirsher 					const char *reg_name)
173e689cf4aSJeff Kirsher {
174e689cf4aSJeff Kirsher 	int err;
175e689cf4aSJeff Kirsher 	u64 val;
176e689cf4aSJeff Kirsher 
177e689cf4aSJeff Kirsher 	val = nr64_ipp(reg);
178e689cf4aSJeff Kirsher 	val |= bits;
179e689cf4aSJeff Kirsher 	nw64_ipp(reg, val);
180e689cf4aSJeff Kirsher 
181e689cf4aSJeff Kirsher 	err = __niu_wait_bits_clear_ipp(np, reg, bits, limit, delay);
182e689cf4aSJeff Kirsher 	if (err)
183e689cf4aSJeff Kirsher 		netdev_err(np->dev, "bits (%llx) of register %s would not clear, val[%llx]\n",
184e689cf4aSJeff Kirsher 			   (unsigned long long)bits, reg_name,
185e689cf4aSJeff Kirsher 			   (unsigned long long)nr64_ipp(reg));
186e689cf4aSJeff Kirsher 	return err;
187e689cf4aSJeff Kirsher }
188e689cf4aSJeff Kirsher 
189e689cf4aSJeff Kirsher #define niu_set_and_wait_clear_ipp(NP, REG, BITS, LIMIT, DELAY, REG_NAME) \
190e689cf4aSJeff Kirsher ({	BUILD_BUG_ON(LIMIT <= 0 || DELAY < 0); \
191e689cf4aSJeff Kirsher 	__niu_set_and_wait_clear_ipp(NP, REG, BITS, LIMIT, DELAY, REG_NAME); \
192e689cf4aSJeff Kirsher })
193e689cf4aSJeff Kirsher 
__niu_wait_bits_clear(struct niu * np,unsigned long reg,u64 bits,int limit,int delay)194e689cf4aSJeff Kirsher static int __niu_wait_bits_clear(struct niu *np, unsigned long reg,
195e689cf4aSJeff Kirsher 				 u64 bits, int limit, int delay)
196e689cf4aSJeff Kirsher {
197e689cf4aSJeff Kirsher 	while (--limit >= 0) {
198e689cf4aSJeff Kirsher 		u64 val = nr64(reg);
199e689cf4aSJeff Kirsher 
200e689cf4aSJeff Kirsher 		if (!(val & bits))
201e689cf4aSJeff Kirsher 			break;
202e689cf4aSJeff Kirsher 		udelay(delay);
203e689cf4aSJeff Kirsher 	}
204e689cf4aSJeff Kirsher 	if (limit < 0)
205e689cf4aSJeff Kirsher 		return -ENODEV;
206e689cf4aSJeff Kirsher 	return 0;
207e689cf4aSJeff Kirsher }
208e689cf4aSJeff Kirsher 
209e689cf4aSJeff Kirsher #define niu_wait_bits_clear(NP, REG, BITS, LIMIT, DELAY) \
210e689cf4aSJeff Kirsher ({	BUILD_BUG_ON(LIMIT <= 0 || DELAY < 0); \
211e689cf4aSJeff Kirsher 	__niu_wait_bits_clear(NP, REG, BITS, LIMIT, DELAY); \
212e689cf4aSJeff Kirsher })
213e689cf4aSJeff Kirsher 
__niu_set_and_wait_clear(struct niu * np,unsigned long reg,u64 bits,int limit,int delay,const char * reg_name)214e689cf4aSJeff Kirsher static int __niu_set_and_wait_clear(struct niu *np, unsigned long reg,
215e689cf4aSJeff Kirsher 				    u64 bits, int limit, int delay,
216e689cf4aSJeff Kirsher 				    const char *reg_name)
217e689cf4aSJeff Kirsher {
218e689cf4aSJeff Kirsher 	int err;
219e689cf4aSJeff Kirsher 
220e689cf4aSJeff Kirsher 	nw64(reg, bits);
221e689cf4aSJeff Kirsher 	err = __niu_wait_bits_clear(np, reg, bits, limit, delay);
222e689cf4aSJeff Kirsher 	if (err)
223e689cf4aSJeff Kirsher 		netdev_err(np->dev, "bits (%llx) of register %s would not clear, val[%llx]\n",
224e689cf4aSJeff Kirsher 			   (unsigned long long)bits, reg_name,
225e689cf4aSJeff Kirsher 			   (unsigned long long)nr64(reg));
226e689cf4aSJeff Kirsher 	return err;
227e689cf4aSJeff Kirsher }
228e689cf4aSJeff Kirsher 
229e689cf4aSJeff Kirsher #define niu_set_and_wait_clear(NP, REG, BITS, LIMIT, DELAY, REG_NAME) \
230e689cf4aSJeff Kirsher ({	BUILD_BUG_ON(LIMIT <= 0 || DELAY < 0); \
231e689cf4aSJeff Kirsher 	__niu_set_and_wait_clear(NP, REG, BITS, LIMIT, DELAY, REG_NAME); \
232e689cf4aSJeff Kirsher })
233e689cf4aSJeff Kirsher 
niu_ldg_rearm(struct niu * np,struct niu_ldg * lp,int on)234e689cf4aSJeff Kirsher static void niu_ldg_rearm(struct niu *np, struct niu_ldg *lp, int on)
235e689cf4aSJeff Kirsher {
236e689cf4aSJeff Kirsher 	u64 val = (u64) lp->timer;
237e689cf4aSJeff Kirsher 
238e689cf4aSJeff Kirsher 	if (on)
239e689cf4aSJeff Kirsher 		val |= LDG_IMGMT_ARM;
240e689cf4aSJeff Kirsher 
241e689cf4aSJeff Kirsher 	nw64(LDG_IMGMT(lp->ldg_num), val);
242e689cf4aSJeff Kirsher }
243e689cf4aSJeff Kirsher 
niu_ldn_irq_enable(struct niu * np,int ldn,int on)244e689cf4aSJeff Kirsher static int niu_ldn_irq_enable(struct niu *np, int ldn, int on)
245e689cf4aSJeff Kirsher {
246e689cf4aSJeff Kirsher 	unsigned long mask_reg, bits;
247e689cf4aSJeff Kirsher 	u64 val;
248e689cf4aSJeff Kirsher 
249e689cf4aSJeff Kirsher 	if (ldn < 0 || ldn > LDN_MAX)
250e689cf4aSJeff Kirsher 		return -EINVAL;
251e689cf4aSJeff Kirsher 
252e689cf4aSJeff Kirsher 	if (ldn < 64) {
253e689cf4aSJeff Kirsher 		mask_reg = LD_IM0(ldn);
254e689cf4aSJeff Kirsher 		bits = LD_IM0_MASK;
255e689cf4aSJeff Kirsher 	} else {
256e689cf4aSJeff Kirsher 		mask_reg = LD_IM1(ldn - 64);
257e689cf4aSJeff Kirsher 		bits = LD_IM1_MASK;
258e689cf4aSJeff Kirsher 	}
259e689cf4aSJeff Kirsher 
260e689cf4aSJeff Kirsher 	val = nr64(mask_reg);
261e689cf4aSJeff Kirsher 	if (on)
262e689cf4aSJeff Kirsher 		val &= ~bits;
263e689cf4aSJeff Kirsher 	else
264e689cf4aSJeff Kirsher 		val |= bits;
265e689cf4aSJeff Kirsher 	nw64(mask_reg, val);
266e689cf4aSJeff Kirsher 
267e689cf4aSJeff Kirsher 	return 0;
268e689cf4aSJeff Kirsher }
269e689cf4aSJeff Kirsher 
niu_enable_ldn_in_ldg(struct niu * np,struct niu_ldg * lp,int on)270e689cf4aSJeff Kirsher static int niu_enable_ldn_in_ldg(struct niu *np, struct niu_ldg *lp, int on)
271e689cf4aSJeff Kirsher {
272e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
273e689cf4aSJeff Kirsher 	int i;
274e689cf4aSJeff Kirsher 
275e689cf4aSJeff Kirsher 	for (i = 0; i <= LDN_MAX; i++) {
276e689cf4aSJeff Kirsher 		int err;
277e689cf4aSJeff Kirsher 
278e689cf4aSJeff Kirsher 		if (parent->ldg_map[i] != lp->ldg_num)
279e689cf4aSJeff Kirsher 			continue;
280e689cf4aSJeff Kirsher 
281e689cf4aSJeff Kirsher 		err = niu_ldn_irq_enable(np, i, on);
282e689cf4aSJeff Kirsher 		if (err)
283e689cf4aSJeff Kirsher 			return err;
284e689cf4aSJeff Kirsher 	}
285e689cf4aSJeff Kirsher 	return 0;
286e689cf4aSJeff Kirsher }
287e689cf4aSJeff Kirsher 
niu_enable_interrupts(struct niu * np,int on)288e689cf4aSJeff Kirsher static int niu_enable_interrupts(struct niu *np, int on)
289e689cf4aSJeff Kirsher {
290e689cf4aSJeff Kirsher 	int i;
291e689cf4aSJeff Kirsher 
292e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_ldg; i++) {
293e689cf4aSJeff Kirsher 		struct niu_ldg *lp = &np->ldg[i];
294e689cf4aSJeff Kirsher 		int err;
295e689cf4aSJeff Kirsher 
296e689cf4aSJeff Kirsher 		err = niu_enable_ldn_in_ldg(np, lp, on);
297e689cf4aSJeff Kirsher 		if (err)
298e689cf4aSJeff Kirsher 			return err;
299e689cf4aSJeff Kirsher 	}
300e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_ldg; i++)
301e689cf4aSJeff Kirsher 		niu_ldg_rearm(np, &np->ldg[i], on);
302e689cf4aSJeff Kirsher 
303e689cf4aSJeff Kirsher 	return 0;
304e689cf4aSJeff Kirsher }
305e689cf4aSJeff Kirsher 
phy_encode(u32 type,int port)306e689cf4aSJeff Kirsher static u32 phy_encode(u32 type, int port)
307e689cf4aSJeff Kirsher {
308e689cf4aSJeff Kirsher 	return type << (port * 2);
309e689cf4aSJeff Kirsher }
310e689cf4aSJeff Kirsher 
phy_decode(u32 val,int port)311e689cf4aSJeff Kirsher static u32 phy_decode(u32 val, int port)
312e689cf4aSJeff Kirsher {
313e689cf4aSJeff Kirsher 	return (val >> (port * 2)) & PORT_TYPE_MASK;
314e689cf4aSJeff Kirsher }
315e689cf4aSJeff Kirsher 
mdio_wait(struct niu * np)316e689cf4aSJeff Kirsher static int mdio_wait(struct niu *np)
317e689cf4aSJeff Kirsher {
318e689cf4aSJeff Kirsher 	int limit = 1000;
319e689cf4aSJeff Kirsher 	u64 val;
320e689cf4aSJeff Kirsher 
321e689cf4aSJeff Kirsher 	while (--limit > 0) {
322e689cf4aSJeff Kirsher 		val = nr64(MIF_FRAME_OUTPUT);
323e689cf4aSJeff Kirsher 		if ((val >> MIF_FRAME_OUTPUT_TA_SHIFT) & 0x1)
324e689cf4aSJeff Kirsher 			return val & MIF_FRAME_OUTPUT_DATA;
325e689cf4aSJeff Kirsher 
326e689cf4aSJeff Kirsher 		udelay(10);
327e689cf4aSJeff Kirsher 	}
328e689cf4aSJeff Kirsher 
329e689cf4aSJeff Kirsher 	return -ENODEV;
330e689cf4aSJeff Kirsher }
331e689cf4aSJeff Kirsher 
mdio_read(struct niu * np,int port,int dev,int reg)332e689cf4aSJeff Kirsher static int mdio_read(struct niu *np, int port, int dev, int reg)
333e689cf4aSJeff Kirsher {
334e689cf4aSJeff Kirsher 	int err;
335e689cf4aSJeff Kirsher 
336e689cf4aSJeff Kirsher 	nw64(MIF_FRAME_OUTPUT, MDIO_ADDR_OP(port, dev, reg));
337e689cf4aSJeff Kirsher 	err = mdio_wait(np);
338e689cf4aSJeff Kirsher 	if (err < 0)
339e689cf4aSJeff Kirsher 		return err;
340e689cf4aSJeff Kirsher 
341e689cf4aSJeff Kirsher 	nw64(MIF_FRAME_OUTPUT, MDIO_READ_OP(port, dev));
342e689cf4aSJeff Kirsher 	return mdio_wait(np);
343e689cf4aSJeff Kirsher }
344e689cf4aSJeff Kirsher 
mdio_write(struct niu * np,int port,int dev,int reg,int data)345e689cf4aSJeff Kirsher static int mdio_write(struct niu *np, int port, int dev, int reg, int data)
346e689cf4aSJeff Kirsher {
347e689cf4aSJeff Kirsher 	int err;
348e689cf4aSJeff Kirsher 
349e689cf4aSJeff Kirsher 	nw64(MIF_FRAME_OUTPUT, MDIO_ADDR_OP(port, dev, reg));
350e689cf4aSJeff Kirsher 	err = mdio_wait(np);
351e689cf4aSJeff Kirsher 	if (err < 0)
352e689cf4aSJeff Kirsher 		return err;
353e689cf4aSJeff Kirsher 
354e689cf4aSJeff Kirsher 	nw64(MIF_FRAME_OUTPUT, MDIO_WRITE_OP(port, dev, data));
355e689cf4aSJeff Kirsher 	err = mdio_wait(np);
356e689cf4aSJeff Kirsher 	if (err < 0)
357e689cf4aSJeff Kirsher 		return err;
358e689cf4aSJeff Kirsher 
359e689cf4aSJeff Kirsher 	return 0;
360e689cf4aSJeff Kirsher }
361e689cf4aSJeff Kirsher 
mii_read(struct niu * np,int port,int reg)362e689cf4aSJeff Kirsher static int mii_read(struct niu *np, int port, int reg)
363e689cf4aSJeff Kirsher {
364e689cf4aSJeff Kirsher 	nw64(MIF_FRAME_OUTPUT, MII_READ_OP(port, reg));
365e689cf4aSJeff Kirsher 	return mdio_wait(np);
366e689cf4aSJeff Kirsher }
367e689cf4aSJeff Kirsher 
mii_write(struct niu * np,int port,int reg,int data)368e689cf4aSJeff Kirsher static int mii_write(struct niu *np, int port, int reg, int data)
369e689cf4aSJeff Kirsher {
370e689cf4aSJeff Kirsher 	int err;
371e689cf4aSJeff Kirsher 
372e689cf4aSJeff Kirsher 	nw64(MIF_FRAME_OUTPUT, MII_WRITE_OP(port, reg, data));
373e689cf4aSJeff Kirsher 	err = mdio_wait(np);
374e689cf4aSJeff Kirsher 	if (err < 0)
375e689cf4aSJeff Kirsher 		return err;
376e689cf4aSJeff Kirsher 
377e689cf4aSJeff Kirsher 	return 0;
378e689cf4aSJeff Kirsher }
379e689cf4aSJeff Kirsher 
esr2_set_tx_cfg(struct niu * np,unsigned long channel,u32 val)380e689cf4aSJeff Kirsher static int esr2_set_tx_cfg(struct niu *np, unsigned long channel, u32 val)
381e689cf4aSJeff Kirsher {
382e689cf4aSJeff Kirsher 	int err;
383e689cf4aSJeff Kirsher 
384e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
385e689cf4aSJeff Kirsher 			 ESR2_TI_PLL_TX_CFG_L(channel),
386e689cf4aSJeff Kirsher 			 val & 0xffff);
387e689cf4aSJeff Kirsher 	if (!err)
388e689cf4aSJeff Kirsher 		err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
389e689cf4aSJeff Kirsher 				 ESR2_TI_PLL_TX_CFG_H(channel),
390e689cf4aSJeff Kirsher 				 val >> 16);
391e689cf4aSJeff Kirsher 	return err;
392e689cf4aSJeff Kirsher }
393e689cf4aSJeff Kirsher 
esr2_set_rx_cfg(struct niu * np,unsigned long channel,u32 val)394e689cf4aSJeff Kirsher static int esr2_set_rx_cfg(struct niu *np, unsigned long channel, u32 val)
395e689cf4aSJeff Kirsher {
396e689cf4aSJeff Kirsher 	int err;
397e689cf4aSJeff Kirsher 
398e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
399e689cf4aSJeff Kirsher 			 ESR2_TI_PLL_RX_CFG_L(channel),
400e689cf4aSJeff Kirsher 			 val & 0xffff);
401e689cf4aSJeff Kirsher 	if (!err)
402e689cf4aSJeff Kirsher 		err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
403e689cf4aSJeff Kirsher 				 ESR2_TI_PLL_RX_CFG_H(channel),
404e689cf4aSJeff Kirsher 				 val >> 16);
405e689cf4aSJeff Kirsher 	return err;
406e689cf4aSJeff Kirsher }
407e689cf4aSJeff Kirsher 
408e689cf4aSJeff Kirsher /* Mode is always 10G fiber.  */
serdes_init_niu_10g_fiber(struct niu * np)409e689cf4aSJeff Kirsher static int serdes_init_niu_10g_fiber(struct niu *np)
410e689cf4aSJeff Kirsher {
411e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
412e689cf4aSJeff Kirsher 	u32 tx_cfg, rx_cfg;
413e689cf4aSJeff Kirsher 	unsigned long i;
414e689cf4aSJeff Kirsher 
415e689cf4aSJeff Kirsher 	tx_cfg = (PLL_TX_CFG_ENTX | PLL_TX_CFG_SWING_1375MV);
416e689cf4aSJeff Kirsher 	rx_cfg = (PLL_RX_CFG_ENRX | PLL_RX_CFG_TERM_0P8VDDT |
417e689cf4aSJeff Kirsher 		  PLL_RX_CFG_ALIGN_ENA | PLL_RX_CFG_LOS_LTHRESH |
418e689cf4aSJeff Kirsher 		  PLL_RX_CFG_EQ_LP_ADAPTIVE);
419e689cf4aSJeff Kirsher 
420e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_PHY) {
421e689cf4aSJeff Kirsher 		u16 test_cfg = PLL_TEST_CFG_LOOPBACK_CML_DIS;
422e689cf4aSJeff Kirsher 
423e689cf4aSJeff Kirsher 		mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
424e689cf4aSJeff Kirsher 			   ESR2_TI_PLL_TEST_CFG_L, test_cfg);
425e689cf4aSJeff Kirsher 
426e689cf4aSJeff Kirsher 		tx_cfg |= PLL_TX_CFG_ENTEST;
427e689cf4aSJeff Kirsher 		rx_cfg |= PLL_RX_CFG_ENTEST;
428e689cf4aSJeff Kirsher 	}
429e689cf4aSJeff Kirsher 
430e689cf4aSJeff Kirsher 	/* Initialize all 4 lanes of the SERDES.  */
431e689cf4aSJeff Kirsher 	for (i = 0; i < 4; i++) {
432e689cf4aSJeff Kirsher 		int err = esr2_set_tx_cfg(np, i, tx_cfg);
433e689cf4aSJeff Kirsher 		if (err)
434e689cf4aSJeff Kirsher 			return err;
435e689cf4aSJeff Kirsher 	}
436e689cf4aSJeff Kirsher 
437e689cf4aSJeff Kirsher 	for (i = 0; i < 4; i++) {
438e689cf4aSJeff Kirsher 		int err = esr2_set_rx_cfg(np, i, rx_cfg);
439e689cf4aSJeff Kirsher 		if (err)
440e689cf4aSJeff Kirsher 			return err;
441e689cf4aSJeff Kirsher 	}
442e689cf4aSJeff Kirsher 
443e689cf4aSJeff Kirsher 	return 0;
444e689cf4aSJeff Kirsher }
445e689cf4aSJeff Kirsher 
serdes_init_niu_1g_serdes(struct niu * np)446e689cf4aSJeff Kirsher static int serdes_init_niu_1g_serdes(struct niu *np)
447e689cf4aSJeff Kirsher {
448e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
449e689cf4aSJeff Kirsher 	u16 pll_cfg, pll_sts;
450e689cf4aSJeff Kirsher 	int max_retry = 100;
4513f649ab7SKees Cook 	u64 sig, mask, val;
452e689cf4aSJeff Kirsher 	u32 tx_cfg, rx_cfg;
453e689cf4aSJeff Kirsher 	unsigned long i;
454e689cf4aSJeff Kirsher 	int err;
455e689cf4aSJeff Kirsher 
456e689cf4aSJeff Kirsher 	tx_cfg = (PLL_TX_CFG_ENTX | PLL_TX_CFG_SWING_1375MV |
457e689cf4aSJeff Kirsher 		  PLL_TX_CFG_RATE_HALF);
458e689cf4aSJeff Kirsher 	rx_cfg = (PLL_RX_CFG_ENRX | PLL_RX_CFG_TERM_0P8VDDT |
459e689cf4aSJeff Kirsher 		  PLL_RX_CFG_ALIGN_ENA | PLL_RX_CFG_LOS_LTHRESH |
460e689cf4aSJeff Kirsher 		  PLL_RX_CFG_RATE_HALF);
461e689cf4aSJeff Kirsher 
462e689cf4aSJeff Kirsher 	if (np->port == 0)
463e689cf4aSJeff Kirsher 		rx_cfg |= PLL_RX_CFG_EQ_LP_ADAPTIVE;
464e689cf4aSJeff Kirsher 
465e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_PHY) {
466e689cf4aSJeff Kirsher 		u16 test_cfg = PLL_TEST_CFG_LOOPBACK_CML_DIS;
467e689cf4aSJeff Kirsher 
468e689cf4aSJeff Kirsher 		mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
469e689cf4aSJeff Kirsher 			   ESR2_TI_PLL_TEST_CFG_L, test_cfg);
470e689cf4aSJeff Kirsher 
471e689cf4aSJeff Kirsher 		tx_cfg |= PLL_TX_CFG_ENTEST;
472e689cf4aSJeff Kirsher 		rx_cfg |= PLL_RX_CFG_ENTEST;
473e689cf4aSJeff Kirsher 	}
474e689cf4aSJeff Kirsher 
475e689cf4aSJeff Kirsher 	/* Initialize PLL for 1G */
476e689cf4aSJeff Kirsher 	pll_cfg = (PLL_CFG_ENPLL | PLL_CFG_MPY_8X);
477e689cf4aSJeff Kirsher 
478e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
479e689cf4aSJeff Kirsher 			 ESR2_TI_PLL_CFG_L, pll_cfg);
480e689cf4aSJeff Kirsher 	if (err) {
481e689cf4aSJeff Kirsher 		netdev_err(np->dev, "NIU Port %d %s() mdio write to ESR2_TI_PLL_CFG_L failed\n",
482e689cf4aSJeff Kirsher 			   np->port, __func__);
483e689cf4aSJeff Kirsher 		return err;
484e689cf4aSJeff Kirsher 	}
485e689cf4aSJeff Kirsher 
486e689cf4aSJeff Kirsher 	pll_sts = PLL_CFG_ENPLL;
487e689cf4aSJeff Kirsher 
488e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
489e689cf4aSJeff Kirsher 			 ESR2_TI_PLL_STS_L, pll_sts);
490e689cf4aSJeff Kirsher 	if (err) {
491e689cf4aSJeff Kirsher 		netdev_err(np->dev, "NIU Port %d %s() mdio write to ESR2_TI_PLL_STS_L failed\n",
492e689cf4aSJeff Kirsher 			   np->port, __func__);
493e689cf4aSJeff Kirsher 		return err;
494e689cf4aSJeff Kirsher 	}
495e689cf4aSJeff Kirsher 
496e689cf4aSJeff Kirsher 	udelay(200);
497e689cf4aSJeff Kirsher 
498e689cf4aSJeff Kirsher 	/* Initialize all 4 lanes of the SERDES.  */
499e689cf4aSJeff Kirsher 	for (i = 0; i < 4; i++) {
500e689cf4aSJeff Kirsher 		err = esr2_set_tx_cfg(np, i, tx_cfg);
501e689cf4aSJeff Kirsher 		if (err)
502e689cf4aSJeff Kirsher 			return err;
503e689cf4aSJeff Kirsher 	}
504e689cf4aSJeff Kirsher 
505e689cf4aSJeff Kirsher 	for (i = 0; i < 4; i++) {
506e689cf4aSJeff Kirsher 		err = esr2_set_rx_cfg(np, i, rx_cfg);
507e689cf4aSJeff Kirsher 		if (err)
508e689cf4aSJeff Kirsher 			return err;
509e689cf4aSJeff Kirsher 	}
510e689cf4aSJeff Kirsher 
511e689cf4aSJeff Kirsher 	switch (np->port) {
512e689cf4aSJeff Kirsher 	case 0:
513e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P0 | ESR_INT_DET0_P0);
514e689cf4aSJeff Kirsher 		mask = val;
515e689cf4aSJeff Kirsher 		break;
516e689cf4aSJeff Kirsher 
517e689cf4aSJeff Kirsher 	case 1:
518e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P1 | ESR_INT_DET0_P1);
519e689cf4aSJeff Kirsher 		mask = val;
520e689cf4aSJeff Kirsher 		break;
521e689cf4aSJeff Kirsher 
522e689cf4aSJeff Kirsher 	default:
523e689cf4aSJeff Kirsher 		return -EINVAL;
524e689cf4aSJeff Kirsher 	}
525e689cf4aSJeff Kirsher 
526e689cf4aSJeff Kirsher 	while (max_retry--) {
527e689cf4aSJeff Kirsher 		sig = nr64(ESR_INT_SIGNALS);
528e689cf4aSJeff Kirsher 		if ((sig & mask) == val)
529e689cf4aSJeff Kirsher 			break;
530e689cf4aSJeff Kirsher 
531e689cf4aSJeff Kirsher 		mdelay(500);
532e689cf4aSJeff Kirsher 	}
533e689cf4aSJeff Kirsher 
534e689cf4aSJeff Kirsher 	if ((sig & mask) != val) {
535e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Port %u signal bits [%08x] are not [%08x]\n",
536e689cf4aSJeff Kirsher 			   np->port, (int)(sig & mask), (int)val);
537e689cf4aSJeff Kirsher 		return -ENODEV;
538e689cf4aSJeff Kirsher 	}
539e689cf4aSJeff Kirsher 
540e689cf4aSJeff Kirsher 	return 0;
541e689cf4aSJeff Kirsher }
542e689cf4aSJeff Kirsher 
serdes_init_niu_10g_serdes(struct niu * np)543e689cf4aSJeff Kirsher static int serdes_init_niu_10g_serdes(struct niu *np)
544e689cf4aSJeff Kirsher {
545e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
546e689cf4aSJeff Kirsher 	u32 tx_cfg, rx_cfg, pll_cfg, pll_sts;
547e689cf4aSJeff Kirsher 	int max_retry = 100;
5483f649ab7SKees Cook 	u64 sig, mask, val;
549e689cf4aSJeff Kirsher 	unsigned long i;
550e689cf4aSJeff Kirsher 	int err;
551e689cf4aSJeff Kirsher 
552e689cf4aSJeff Kirsher 	tx_cfg = (PLL_TX_CFG_ENTX | PLL_TX_CFG_SWING_1375MV);
553e689cf4aSJeff Kirsher 	rx_cfg = (PLL_RX_CFG_ENRX | PLL_RX_CFG_TERM_0P8VDDT |
554e689cf4aSJeff Kirsher 		  PLL_RX_CFG_ALIGN_ENA | PLL_RX_CFG_LOS_LTHRESH |
555e689cf4aSJeff Kirsher 		  PLL_RX_CFG_EQ_LP_ADAPTIVE);
556e689cf4aSJeff Kirsher 
557e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_PHY) {
558e689cf4aSJeff Kirsher 		u16 test_cfg = PLL_TEST_CFG_LOOPBACK_CML_DIS;
559e689cf4aSJeff Kirsher 
560e689cf4aSJeff Kirsher 		mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
561e689cf4aSJeff Kirsher 			   ESR2_TI_PLL_TEST_CFG_L, test_cfg);
562e689cf4aSJeff Kirsher 
563e689cf4aSJeff Kirsher 		tx_cfg |= PLL_TX_CFG_ENTEST;
564e689cf4aSJeff Kirsher 		rx_cfg |= PLL_RX_CFG_ENTEST;
565e689cf4aSJeff Kirsher 	}
566e689cf4aSJeff Kirsher 
567e689cf4aSJeff Kirsher 	/* Initialize PLL for 10G */
568e689cf4aSJeff Kirsher 	pll_cfg = (PLL_CFG_ENPLL | PLL_CFG_MPY_10X);
569e689cf4aSJeff Kirsher 
570e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
571e689cf4aSJeff Kirsher 			 ESR2_TI_PLL_CFG_L, pll_cfg & 0xffff);
572e689cf4aSJeff Kirsher 	if (err) {
573e689cf4aSJeff Kirsher 		netdev_err(np->dev, "NIU Port %d %s() mdio write to ESR2_TI_PLL_CFG_L failed\n",
574e689cf4aSJeff Kirsher 			   np->port, __func__);
575e689cf4aSJeff Kirsher 		return err;
576e689cf4aSJeff Kirsher 	}
577e689cf4aSJeff Kirsher 
578e689cf4aSJeff Kirsher 	pll_sts = PLL_CFG_ENPLL;
579e689cf4aSJeff Kirsher 
580e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
581e689cf4aSJeff Kirsher 			 ESR2_TI_PLL_STS_L, pll_sts & 0xffff);
582e689cf4aSJeff Kirsher 	if (err) {
583e689cf4aSJeff Kirsher 		netdev_err(np->dev, "NIU Port %d %s() mdio write to ESR2_TI_PLL_STS_L failed\n",
584e689cf4aSJeff Kirsher 			   np->port, __func__);
585e689cf4aSJeff Kirsher 		return err;
586e689cf4aSJeff Kirsher 	}
587e689cf4aSJeff Kirsher 
588e689cf4aSJeff Kirsher 	udelay(200);
589e689cf4aSJeff Kirsher 
590e689cf4aSJeff Kirsher 	/* Initialize all 4 lanes of the SERDES.  */
591e689cf4aSJeff Kirsher 	for (i = 0; i < 4; i++) {
592e689cf4aSJeff Kirsher 		err = esr2_set_tx_cfg(np, i, tx_cfg);
593e689cf4aSJeff Kirsher 		if (err)
594e689cf4aSJeff Kirsher 			return err;
595e689cf4aSJeff Kirsher 	}
596e689cf4aSJeff Kirsher 
597e689cf4aSJeff Kirsher 	for (i = 0; i < 4; i++) {
598e689cf4aSJeff Kirsher 		err = esr2_set_rx_cfg(np, i, rx_cfg);
599e689cf4aSJeff Kirsher 		if (err)
600e689cf4aSJeff Kirsher 			return err;
601e689cf4aSJeff Kirsher 	}
602e689cf4aSJeff Kirsher 
603e689cf4aSJeff Kirsher 	/* check if serdes is ready */
604e689cf4aSJeff Kirsher 
605e689cf4aSJeff Kirsher 	switch (np->port) {
606e689cf4aSJeff Kirsher 	case 0:
607e689cf4aSJeff Kirsher 		mask = ESR_INT_SIGNALS_P0_BITS;
608e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P0 |
609e689cf4aSJeff Kirsher 		       ESR_INT_DET0_P0 |
610e689cf4aSJeff Kirsher 		       ESR_INT_XSRDY_P0 |
611e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH3 |
612e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH2 |
613e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH1 |
614e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH0);
615e689cf4aSJeff Kirsher 		break;
616e689cf4aSJeff Kirsher 
617e689cf4aSJeff Kirsher 	case 1:
618e689cf4aSJeff Kirsher 		mask = ESR_INT_SIGNALS_P1_BITS;
619e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P1 |
620e689cf4aSJeff Kirsher 		       ESR_INT_DET0_P1 |
621e689cf4aSJeff Kirsher 		       ESR_INT_XSRDY_P1 |
622e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH3 |
623e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH2 |
624e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH1 |
625e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH0);
626e689cf4aSJeff Kirsher 		break;
627e689cf4aSJeff Kirsher 
628e689cf4aSJeff Kirsher 	default:
629e689cf4aSJeff Kirsher 		return -EINVAL;
630e689cf4aSJeff Kirsher 	}
631e689cf4aSJeff Kirsher 
632e689cf4aSJeff Kirsher 	while (max_retry--) {
633e689cf4aSJeff Kirsher 		sig = nr64(ESR_INT_SIGNALS);
634e689cf4aSJeff Kirsher 		if ((sig & mask) == val)
635e689cf4aSJeff Kirsher 			break;
636e689cf4aSJeff Kirsher 
637e689cf4aSJeff Kirsher 		mdelay(500);
638e689cf4aSJeff Kirsher 	}
639e689cf4aSJeff Kirsher 
640e689cf4aSJeff Kirsher 	if ((sig & mask) != val) {
641e689cf4aSJeff Kirsher 		pr_info("NIU Port %u signal bits [%08x] are not [%08x] for 10G...trying 1G\n",
642e689cf4aSJeff Kirsher 			np->port, (int)(sig & mask), (int)val);
643e689cf4aSJeff Kirsher 
644e689cf4aSJeff Kirsher 		/* 10G failed, try initializing at 1G */
645e689cf4aSJeff Kirsher 		err = serdes_init_niu_1g_serdes(np);
646e689cf4aSJeff Kirsher 		if (!err) {
647e689cf4aSJeff Kirsher 			np->flags &= ~NIU_FLAGS_10G;
648e689cf4aSJeff Kirsher 			np->mac_xcvr = MAC_XCVR_PCS;
649e689cf4aSJeff Kirsher 		}  else {
650e689cf4aSJeff Kirsher 			netdev_err(np->dev, "Port %u 10G/1G SERDES Link Failed\n",
651e689cf4aSJeff Kirsher 				   np->port);
652e689cf4aSJeff Kirsher 			return -ENODEV;
653e689cf4aSJeff Kirsher 		}
654e689cf4aSJeff Kirsher 	}
655e689cf4aSJeff Kirsher 	return 0;
656e689cf4aSJeff Kirsher }
657e689cf4aSJeff Kirsher 
esr_read_rxtx_ctrl(struct niu * np,unsigned long chan,u32 * val)658e689cf4aSJeff Kirsher static int esr_read_rxtx_ctrl(struct niu *np, unsigned long chan, u32 *val)
659e689cf4aSJeff Kirsher {
660e689cf4aSJeff Kirsher 	int err;
661e689cf4aSJeff Kirsher 
662e689cf4aSJeff Kirsher 	err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR, ESR_RXTX_CTRL_L(chan));
663e689cf4aSJeff Kirsher 	if (err >= 0) {
664e689cf4aSJeff Kirsher 		*val = (err & 0xffff);
665e689cf4aSJeff Kirsher 		err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
666e689cf4aSJeff Kirsher 				ESR_RXTX_CTRL_H(chan));
667e689cf4aSJeff Kirsher 		if (err >= 0)
668e689cf4aSJeff Kirsher 			*val |= ((err & 0xffff) << 16);
669e689cf4aSJeff Kirsher 		err = 0;
670e689cf4aSJeff Kirsher 	}
671e689cf4aSJeff Kirsher 	return err;
672e689cf4aSJeff Kirsher }
673e689cf4aSJeff Kirsher 
esr_read_glue0(struct niu * np,unsigned long chan,u32 * val)674e689cf4aSJeff Kirsher static int esr_read_glue0(struct niu *np, unsigned long chan, u32 *val)
675e689cf4aSJeff Kirsher {
676e689cf4aSJeff Kirsher 	int err;
677e689cf4aSJeff Kirsher 
678e689cf4aSJeff Kirsher 	err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
679e689cf4aSJeff Kirsher 			ESR_GLUE_CTRL0_L(chan));
680e689cf4aSJeff Kirsher 	if (err >= 0) {
681e689cf4aSJeff Kirsher 		*val = (err & 0xffff);
682e689cf4aSJeff Kirsher 		err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
683e689cf4aSJeff Kirsher 				ESR_GLUE_CTRL0_H(chan));
684e689cf4aSJeff Kirsher 		if (err >= 0) {
685e689cf4aSJeff Kirsher 			*val |= ((err & 0xffff) << 16);
686e689cf4aSJeff Kirsher 			err = 0;
687e689cf4aSJeff Kirsher 		}
688e689cf4aSJeff Kirsher 	}
689e689cf4aSJeff Kirsher 	return err;
690e689cf4aSJeff Kirsher }
691e689cf4aSJeff Kirsher 
esr_read_reset(struct niu * np,u32 * val)692e689cf4aSJeff Kirsher static int esr_read_reset(struct niu *np, u32 *val)
693e689cf4aSJeff Kirsher {
694e689cf4aSJeff Kirsher 	int err;
695e689cf4aSJeff Kirsher 
696e689cf4aSJeff Kirsher 	err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
697e689cf4aSJeff Kirsher 			ESR_RXTX_RESET_CTRL_L);
698e689cf4aSJeff Kirsher 	if (err >= 0) {
699e689cf4aSJeff Kirsher 		*val = (err & 0xffff);
700e689cf4aSJeff Kirsher 		err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
701e689cf4aSJeff Kirsher 				ESR_RXTX_RESET_CTRL_H);
702e689cf4aSJeff Kirsher 		if (err >= 0) {
703e689cf4aSJeff Kirsher 			*val |= ((err & 0xffff) << 16);
704e689cf4aSJeff Kirsher 			err = 0;
705e689cf4aSJeff Kirsher 		}
706e689cf4aSJeff Kirsher 	}
707e689cf4aSJeff Kirsher 	return err;
708e689cf4aSJeff Kirsher }
709e689cf4aSJeff Kirsher 
esr_write_rxtx_ctrl(struct niu * np,unsigned long chan,u32 val)710e689cf4aSJeff Kirsher static int esr_write_rxtx_ctrl(struct niu *np, unsigned long chan, u32 val)
711e689cf4aSJeff Kirsher {
712e689cf4aSJeff Kirsher 	int err;
713e689cf4aSJeff Kirsher 
714e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
715e689cf4aSJeff Kirsher 			 ESR_RXTX_CTRL_L(chan), val & 0xffff);
716e689cf4aSJeff Kirsher 	if (!err)
717e689cf4aSJeff Kirsher 		err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
718e689cf4aSJeff Kirsher 				 ESR_RXTX_CTRL_H(chan), (val >> 16));
719e689cf4aSJeff Kirsher 	return err;
720e689cf4aSJeff Kirsher }
721e689cf4aSJeff Kirsher 
esr_write_glue0(struct niu * np,unsigned long chan,u32 val)722e689cf4aSJeff Kirsher static int esr_write_glue0(struct niu *np, unsigned long chan, u32 val)
723e689cf4aSJeff Kirsher {
724e689cf4aSJeff Kirsher 	int err;
725e689cf4aSJeff Kirsher 
726e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
727e689cf4aSJeff Kirsher 			ESR_GLUE_CTRL0_L(chan), val & 0xffff);
728e689cf4aSJeff Kirsher 	if (!err)
729e689cf4aSJeff Kirsher 		err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
730e689cf4aSJeff Kirsher 				 ESR_GLUE_CTRL0_H(chan), (val >> 16));
731e689cf4aSJeff Kirsher 	return err;
732e689cf4aSJeff Kirsher }
733e689cf4aSJeff Kirsher 
esr_reset(struct niu * np)734e689cf4aSJeff Kirsher static int esr_reset(struct niu *np)
735e689cf4aSJeff Kirsher {
7363f649ab7SKees Cook 	u32 reset;
737e689cf4aSJeff Kirsher 	int err;
738e689cf4aSJeff Kirsher 
739e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
740e689cf4aSJeff Kirsher 			 ESR_RXTX_RESET_CTRL_L, 0x0000);
741e689cf4aSJeff Kirsher 	if (err)
742e689cf4aSJeff Kirsher 		return err;
743e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
744e689cf4aSJeff Kirsher 			 ESR_RXTX_RESET_CTRL_H, 0xffff);
745e689cf4aSJeff Kirsher 	if (err)
746e689cf4aSJeff Kirsher 		return err;
747e689cf4aSJeff Kirsher 	udelay(200);
748e689cf4aSJeff Kirsher 
749e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
750e689cf4aSJeff Kirsher 			 ESR_RXTX_RESET_CTRL_L, 0xffff);
751e689cf4aSJeff Kirsher 	if (err)
752e689cf4aSJeff Kirsher 		return err;
753e689cf4aSJeff Kirsher 	udelay(200);
754e689cf4aSJeff Kirsher 
755e689cf4aSJeff Kirsher 	err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
756e689cf4aSJeff Kirsher 			 ESR_RXTX_RESET_CTRL_H, 0x0000);
757e689cf4aSJeff Kirsher 	if (err)
758e689cf4aSJeff Kirsher 		return err;
759e689cf4aSJeff Kirsher 	udelay(200);
760e689cf4aSJeff Kirsher 
761e689cf4aSJeff Kirsher 	err = esr_read_reset(np, &reset);
762e689cf4aSJeff Kirsher 	if (err)
763e689cf4aSJeff Kirsher 		return err;
764e689cf4aSJeff Kirsher 	if (reset != 0) {
765e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Port %u ESR_RESET did not clear [%08x]\n",
766e689cf4aSJeff Kirsher 			   np->port, reset);
767e689cf4aSJeff Kirsher 		return -ENODEV;
768e689cf4aSJeff Kirsher 	}
769e689cf4aSJeff Kirsher 
770e689cf4aSJeff Kirsher 	return 0;
771e689cf4aSJeff Kirsher }
772e689cf4aSJeff Kirsher 
serdes_init_10g(struct niu * np)773e689cf4aSJeff Kirsher static int serdes_init_10g(struct niu *np)
774e689cf4aSJeff Kirsher {
775e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
776e689cf4aSJeff Kirsher 	unsigned long ctrl_reg, test_cfg_reg, i;
777e689cf4aSJeff Kirsher 	u64 ctrl_val, test_cfg_val, sig, mask, val;
778e689cf4aSJeff Kirsher 	int err;
779e689cf4aSJeff Kirsher 
780e689cf4aSJeff Kirsher 	switch (np->port) {
781e689cf4aSJeff Kirsher 	case 0:
782e689cf4aSJeff Kirsher 		ctrl_reg = ENET_SERDES_0_CTRL_CFG;
783e689cf4aSJeff Kirsher 		test_cfg_reg = ENET_SERDES_0_TEST_CFG;
784e689cf4aSJeff Kirsher 		break;
785e689cf4aSJeff Kirsher 	case 1:
786e689cf4aSJeff Kirsher 		ctrl_reg = ENET_SERDES_1_CTRL_CFG;
787e689cf4aSJeff Kirsher 		test_cfg_reg = ENET_SERDES_1_TEST_CFG;
788e689cf4aSJeff Kirsher 		break;
789e689cf4aSJeff Kirsher 
790e689cf4aSJeff Kirsher 	default:
791e689cf4aSJeff Kirsher 		return -EINVAL;
792e689cf4aSJeff Kirsher 	}
793e689cf4aSJeff Kirsher 	ctrl_val = (ENET_SERDES_CTRL_SDET_0 |
794e689cf4aSJeff Kirsher 		    ENET_SERDES_CTRL_SDET_1 |
795e689cf4aSJeff Kirsher 		    ENET_SERDES_CTRL_SDET_2 |
796e689cf4aSJeff Kirsher 		    ENET_SERDES_CTRL_SDET_3 |
797e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_0_SHIFT) |
798e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_1_SHIFT) |
799e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_2_SHIFT) |
800e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_3_SHIFT) |
801e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_0_SHIFT) |
802e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_1_SHIFT) |
803e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_2_SHIFT) |
804e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_3_SHIFT));
805e689cf4aSJeff Kirsher 	test_cfg_val = 0;
806e689cf4aSJeff Kirsher 
807e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_PHY) {
808e689cf4aSJeff Kirsher 		test_cfg_val |= ((ENET_TEST_MD_PAD_LOOPBACK <<
809e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_0_SHIFT) |
810e689cf4aSJeff Kirsher 				 (ENET_TEST_MD_PAD_LOOPBACK <<
811e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_1_SHIFT) |
812e689cf4aSJeff Kirsher 				 (ENET_TEST_MD_PAD_LOOPBACK <<
813e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_2_SHIFT) |
814e689cf4aSJeff Kirsher 				 (ENET_TEST_MD_PAD_LOOPBACK <<
815e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_3_SHIFT));
816e689cf4aSJeff Kirsher 	}
817e689cf4aSJeff Kirsher 
818e689cf4aSJeff Kirsher 	nw64(ctrl_reg, ctrl_val);
819e689cf4aSJeff Kirsher 	nw64(test_cfg_reg, test_cfg_val);
820e689cf4aSJeff Kirsher 
821e689cf4aSJeff Kirsher 	/* Initialize all 4 lanes of the SERDES.  */
822e689cf4aSJeff Kirsher 	for (i = 0; i < 4; i++) {
823e689cf4aSJeff Kirsher 		u32 rxtx_ctrl, glue0;
824e689cf4aSJeff Kirsher 
825e689cf4aSJeff Kirsher 		err = esr_read_rxtx_ctrl(np, i, &rxtx_ctrl);
826e689cf4aSJeff Kirsher 		if (err)
827e689cf4aSJeff Kirsher 			return err;
828e689cf4aSJeff Kirsher 		err = esr_read_glue0(np, i, &glue0);
829e689cf4aSJeff Kirsher 		if (err)
830e689cf4aSJeff Kirsher 			return err;
831e689cf4aSJeff Kirsher 
832e689cf4aSJeff Kirsher 		rxtx_ctrl &= ~(ESR_RXTX_CTRL_VMUXLO);
833e689cf4aSJeff Kirsher 		rxtx_ctrl |= (ESR_RXTX_CTRL_ENSTRETCH |
834e689cf4aSJeff Kirsher 			      (2 << ESR_RXTX_CTRL_VMUXLO_SHIFT));
835e689cf4aSJeff Kirsher 
836e689cf4aSJeff Kirsher 		glue0 &= ~(ESR_GLUE_CTRL0_SRATE |
837e689cf4aSJeff Kirsher 			   ESR_GLUE_CTRL0_THCNT |
838e689cf4aSJeff Kirsher 			   ESR_GLUE_CTRL0_BLTIME);
839e689cf4aSJeff Kirsher 		glue0 |= (ESR_GLUE_CTRL0_RXLOSENAB |
840e689cf4aSJeff Kirsher 			  (0xf << ESR_GLUE_CTRL0_SRATE_SHIFT) |
841e689cf4aSJeff Kirsher 			  (0xff << ESR_GLUE_CTRL0_THCNT_SHIFT) |
842e689cf4aSJeff Kirsher 			  (BLTIME_300_CYCLES <<
843e689cf4aSJeff Kirsher 			   ESR_GLUE_CTRL0_BLTIME_SHIFT));
844e689cf4aSJeff Kirsher 
845e689cf4aSJeff Kirsher 		err = esr_write_rxtx_ctrl(np, i, rxtx_ctrl);
846e689cf4aSJeff Kirsher 		if (err)
847e689cf4aSJeff Kirsher 			return err;
848e689cf4aSJeff Kirsher 		err = esr_write_glue0(np, i, glue0);
849e689cf4aSJeff Kirsher 		if (err)
850e689cf4aSJeff Kirsher 			return err;
851e689cf4aSJeff Kirsher 	}
852e689cf4aSJeff Kirsher 
853e689cf4aSJeff Kirsher 	err = esr_reset(np);
854e689cf4aSJeff Kirsher 	if (err)
855e689cf4aSJeff Kirsher 		return err;
856e689cf4aSJeff Kirsher 
857e689cf4aSJeff Kirsher 	sig = nr64(ESR_INT_SIGNALS);
858e689cf4aSJeff Kirsher 	switch (np->port) {
859e689cf4aSJeff Kirsher 	case 0:
860e689cf4aSJeff Kirsher 		mask = ESR_INT_SIGNALS_P0_BITS;
861e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P0 |
862e689cf4aSJeff Kirsher 		       ESR_INT_DET0_P0 |
863e689cf4aSJeff Kirsher 		       ESR_INT_XSRDY_P0 |
864e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH3 |
865e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH2 |
866e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH1 |
867e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH0);
868e689cf4aSJeff Kirsher 		break;
869e689cf4aSJeff Kirsher 
870e689cf4aSJeff Kirsher 	case 1:
871e689cf4aSJeff Kirsher 		mask = ESR_INT_SIGNALS_P1_BITS;
872e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P1 |
873e689cf4aSJeff Kirsher 		       ESR_INT_DET0_P1 |
874e689cf4aSJeff Kirsher 		       ESR_INT_XSRDY_P1 |
875e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH3 |
876e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH2 |
877e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH1 |
878e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH0);
879e689cf4aSJeff Kirsher 		break;
880e689cf4aSJeff Kirsher 
881e689cf4aSJeff Kirsher 	default:
882e689cf4aSJeff Kirsher 		return -EINVAL;
883e689cf4aSJeff Kirsher 	}
884e689cf4aSJeff Kirsher 
885e689cf4aSJeff Kirsher 	if ((sig & mask) != val) {
886e689cf4aSJeff Kirsher 		if (np->flags & NIU_FLAGS_HOTPLUG_PHY) {
887e689cf4aSJeff Kirsher 			np->flags &= ~NIU_FLAGS_HOTPLUG_PHY_PRESENT;
888e689cf4aSJeff Kirsher 			return 0;
889e689cf4aSJeff Kirsher 		}
890e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Port %u signal bits [%08x] are not [%08x]\n",
891e689cf4aSJeff Kirsher 			   np->port, (int)(sig & mask), (int)val);
892e689cf4aSJeff Kirsher 		return -ENODEV;
893e689cf4aSJeff Kirsher 	}
894e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_HOTPLUG_PHY)
895e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_HOTPLUG_PHY_PRESENT;
896e689cf4aSJeff Kirsher 	return 0;
897e689cf4aSJeff Kirsher }
898e689cf4aSJeff Kirsher 
serdes_init_1g(struct niu * np)899e689cf4aSJeff Kirsher static int serdes_init_1g(struct niu *np)
900e689cf4aSJeff Kirsher {
901e689cf4aSJeff Kirsher 	u64 val;
902e689cf4aSJeff Kirsher 
903e689cf4aSJeff Kirsher 	val = nr64(ENET_SERDES_1_PLL_CFG);
904e689cf4aSJeff Kirsher 	val &= ~ENET_SERDES_PLL_FBDIV2;
905e689cf4aSJeff Kirsher 	switch (np->port) {
906e689cf4aSJeff Kirsher 	case 0:
907e689cf4aSJeff Kirsher 		val |= ENET_SERDES_PLL_HRATE0;
908e689cf4aSJeff Kirsher 		break;
909e689cf4aSJeff Kirsher 	case 1:
910e689cf4aSJeff Kirsher 		val |= ENET_SERDES_PLL_HRATE1;
911e689cf4aSJeff Kirsher 		break;
912e689cf4aSJeff Kirsher 	case 2:
913e689cf4aSJeff Kirsher 		val |= ENET_SERDES_PLL_HRATE2;
914e689cf4aSJeff Kirsher 		break;
915e689cf4aSJeff Kirsher 	case 3:
916e689cf4aSJeff Kirsher 		val |= ENET_SERDES_PLL_HRATE3;
917e689cf4aSJeff Kirsher 		break;
918e689cf4aSJeff Kirsher 	default:
919e689cf4aSJeff Kirsher 		return -EINVAL;
920e689cf4aSJeff Kirsher 	}
921e689cf4aSJeff Kirsher 	nw64(ENET_SERDES_1_PLL_CFG, val);
922e689cf4aSJeff Kirsher 
923e689cf4aSJeff Kirsher 	return 0;
924e689cf4aSJeff Kirsher }
925e689cf4aSJeff Kirsher 
serdes_init_1g_serdes(struct niu * np)926e689cf4aSJeff Kirsher static int serdes_init_1g_serdes(struct niu *np)
927e689cf4aSJeff Kirsher {
928e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
929e689cf4aSJeff Kirsher 	unsigned long ctrl_reg, test_cfg_reg, pll_cfg, i;
930e689cf4aSJeff Kirsher 	u64 ctrl_val, test_cfg_val, sig, mask, val;
931e689cf4aSJeff Kirsher 	int err;
932e689cf4aSJeff Kirsher 	u64 reset_val, val_rd;
933e689cf4aSJeff Kirsher 
934e689cf4aSJeff Kirsher 	val = ENET_SERDES_PLL_HRATE0 | ENET_SERDES_PLL_HRATE1 |
935e689cf4aSJeff Kirsher 		ENET_SERDES_PLL_HRATE2 | ENET_SERDES_PLL_HRATE3 |
936e689cf4aSJeff Kirsher 		ENET_SERDES_PLL_FBDIV0;
937e689cf4aSJeff Kirsher 	switch (np->port) {
938e689cf4aSJeff Kirsher 	case 0:
939e689cf4aSJeff Kirsher 		reset_val =  ENET_SERDES_RESET_0;
940e689cf4aSJeff Kirsher 		ctrl_reg = ENET_SERDES_0_CTRL_CFG;
941e689cf4aSJeff Kirsher 		test_cfg_reg = ENET_SERDES_0_TEST_CFG;
942e689cf4aSJeff Kirsher 		pll_cfg = ENET_SERDES_0_PLL_CFG;
943e689cf4aSJeff Kirsher 		break;
944e689cf4aSJeff Kirsher 	case 1:
945e689cf4aSJeff Kirsher 		reset_val =  ENET_SERDES_RESET_1;
946e689cf4aSJeff Kirsher 		ctrl_reg = ENET_SERDES_1_CTRL_CFG;
947e689cf4aSJeff Kirsher 		test_cfg_reg = ENET_SERDES_1_TEST_CFG;
948e689cf4aSJeff Kirsher 		pll_cfg = ENET_SERDES_1_PLL_CFG;
949e689cf4aSJeff Kirsher 		break;
950e689cf4aSJeff Kirsher 
951e689cf4aSJeff Kirsher 	default:
952e689cf4aSJeff Kirsher 		return -EINVAL;
953e689cf4aSJeff Kirsher 	}
954e689cf4aSJeff Kirsher 	ctrl_val = (ENET_SERDES_CTRL_SDET_0 |
955e689cf4aSJeff Kirsher 		    ENET_SERDES_CTRL_SDET_1 |
956e689cf4aSJeff Kirsher 		    ENET_SERDES_CTRL_SDET_2 |
957e689cf4aSJeff Kirsher 		    ENET_SERDES_CTRL_SDET_3 |
958e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_0_SHIFT) |
959e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_1_SHIFT) |
960e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_2_SHIFT) |
961e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_3_SHIFT) |
962e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_0_SHIFT) |
963e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_1_SHIFT) |
964e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_2_SHIFT) |
965e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_3_SHIFT));
966e689cf4aSJeff Kirsher 	test_cfg_val = 0;
967e689cf4aSJeff Kirsher 
968e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_PHY) {
969e689cf4aSJeff Kirsher 		test_cfg_val |= ((ENET_TEST_MD_PAD_LOOPBACK <<
970e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_0_SHIFT) |
971e689cf4aSJeff Kirsher 				 (ENET_TEST_MD_PAD_LOOPBACK <<
972e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_1_SHIFT) |
973e689cf4aSJeff Kirsher 				 (ENET_TEST_MD_PAD_LOOPBACK <<
974e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_2_SHIFT) |
975e689cf4aSJeff Kirsher 				 (ENET_TEST_MD_PAD_LOOPBACK <<
976e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_3_SHIFT));
977e689cf4aSJeff Kirsher 	}
978e689cf4aSJeff Kirsher 
979e689cf4aSJeff Kirsher 	nw64(ENET_SERDES_RESET, reset_val);
980e689cf4aSJeff Kirsher 	mdelay(20);
981e689cf4aSJeff Kirsher 	val_rd = nr64(ENET_SERDES_RESET);
982e689cf4aSJeff Kirsher 	val_rd &= ~reset_val;
983e689cf4aSJeff Kirsher 	nw64(pll_cfg, val);
984e689cf4aSJeff Kirsher 	nw64(ctrl_reg, ctrl_val);
985e689cf4aSJeff Kirsher 	nw64(test_cfg_reg, test_cfg_val);
986e689cf4aSJeff Kirsher 	nw64(ENET_SERDES_RESET, val_rd);
987e689cf4aSJeff Kirsher 	mdelay(2000);
988e689cf4aSJeff Kirsher 
989e689cf4aSJeff Kirsher 	/* Initialize all 4 lanes of the SERDES.  */
990e689cf4aSJeff Kirsher 	for (i = 0; i < 4; i++) {
991e689cf4aSJeff Kirsher 		u32 rxtx_ctrl, glue0;
992e689cf4aSJeff Kirsher 
993e689cf4aSJeff Kirsher 		err = esr_read_rxtx_ctrl(np, i, &rxtx_ctrl);
994e689cf4aSJeff Kirsher 		if (err)
995e689cf4aSJeff Kirsher 			return err;
996e689cf4aSJeff Kirsher 		err = esr_read_glue0(np, i, &glue0);
997e689cf4aSJeff Kirsher 		if (err)
998e689cf4aSJeff Kirsher 			return err;
999e689cf4aSJeff Kirsher 
1000e689cf4aSJeff Kirsher 		rxtx_ctrl &= ~(ESR_RXTX_CTRL_VMUXLO);
1001e689cf4aSJeff Kirsher 		rxtx_ctrl |= (ESR_RXTX_CTRL_ENSTRETCH |
1002e689cf4aSJeff Kirsher 			      (2 << ESR_RXTX_CTRL_VMUXLO_SHIFT));
1003e689cf4aSJeff Kirsher 
1004e689cf4aSJeff Kirsher 		glue0 &= ~(ESR_GLUE_CTRL0_SRATE |
1005e689cf4aSJeff Kirsher 			   ESR_GLUE_CTRL0_THCNT |
1006e689cf4aSJeff Kirsher 			   ESR_GLUE_CTRL0_BLTIME);
1007e689cf4aSJeff Kirsher 		glue0 |= (ESR_GLUE_CTRL0_RXLOSENAB |
1008e689cf4aSJeff Kirsher 			  (0xf << ESR_GLUE_CTRL0_SRATE_SHIFT) |
1009e689cf4aSJeff Kirsher 			  (0xff << ESR_GLUE_CTRL0_THCNT_SHIFT) |
1010e689cf4aSJeff Kirsher 			  (BLTIME_300_CYCLES <<
1011e689cf4aSJeff Kirsher 			   ESR_GLUE_CTRL0_BLTIME_SHIFT));
1012e689cf4aSJeff Kirsher 
1013e689cf4aSJeff Kirsher 		err = esr_write_rxtx_ctrl(np, i, rxtx_ctrl);
1014e689cf4aSJeff Kirsher 		if (err)
1015e689cf4aSJeff Kirsher 			return err;
1016e689cf4aSJeff Kirsher 		err = esr_write_glue0(np, i, glue0);
1017e689cf4aSJeff Kirsher 		if (err)
1018e689cf4aSJeff Kirsher 			return err;
1019e689cf4aSJeff Kirsher 	}
1020e689cf4aSJeff Kirsher 
1021e689cf4aSJeff Kirsher 
1022e689cf4aSJeff Kirsher 	sig = nr64(ESR_INT_SIGNALS);
1023e689cf4aSJeff Kirsher 	switch (np->port) {
1024e689cf4aSJeff Kirsher 	case 0:
1025e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P0 | ESR_INT_DET0_P0);
1026e689cf4aSJeff Kirsher 		mask = val;
1027e689cf4aSJeff Kirsher 		break;
1028e689cf4aSJeff Kirsher 
1029e689cf4aSJeff Kirsher 	case 1:
1030e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P1 | ESR_INT_DET0_P1);
1031e689cf4aSJeff Kirsher 		mask = val;
1032e689cf4aSJeff Kirsher 		break;
1033e689cf4aSJeff Kirsher 
1034e689cf4aSJeff Kirsher 	default:
1035e689cf4aSJeff Kirsher 		return -EINVAL;
1036e689cf4aSJeff Kirsher 	}
1037e689cf4aSJeff Kirsher 
1038e689cf4aSJeff Kirsher 	if ((sig & mask) != val) {
1039e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Port %u signal bits [%08x] are not [%08x]\n",
1040e689cf4aSJeff Kirsher 			   np->port, (int)(sig & mask), (int)val);
1041e689cf4aSJeff Kirsher 		return -ENODEV;
1042e689cf4aSJeff Kirsher 	}
1043e689cf4aSJeff Kirsher 
1044e689cf4aSJeff Kirsher 	return 0;
1045e689cf4aSJeff Kirsher }
1046e689cf4aSJeff Kirsher 
link_status_1g_serdes(struct niu * np,int * link_up_p)1047e689cf4aSJeff Kirsher static int link_status_1g_serdes(struct niu *np, int *link_up_p)
1048e689cf4aSJeff Kirsher {
1049e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
1050e689cf4aSJeff Kirsher 	int link_up;
1051e689cf4aSJeff Kirsher 	u64 val;
1052e689cf4aSJeff Kirsher 	u16 current_speed;
1053e689cf4aSJeff Kirsher 	unsigned long flags;
1054e689cf4aSJeff Kirsher 	u8 current_duplex;
1055e689cf4aSJeff Kirsher 
1056e689cf4aSJeff Kirsher 	link_up = 0;
1057e689cf4aSJeff Kirsher 	current_speed = SPEED_INVALID;
1058e689cf4aSJeff Kirsher 	current_duplex = DUPLEX_INVALID;
1059e689cf4aSJeff Kirsher 
1060e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
1061e689cf4aSJeff Kirsher 
1062e689cf4aSJeff Kirsher 	val = nr64_pcs(PCS_MII_STAT);
1063e689cf4aSJeff Kirsher 
1064e689cf4aSJeff Kirsher 	if (val & PCS_MII_STAT_LINK_STATUS) {
1065e689cf4aSJeff Kirsher 		link_up = 1;
1066e689cf4aSJeff Kirsher 		current_speed = SPEED_1000;
1067e689cf4aSJeff Kirsher 		current_duplex = DUPLEX_FULL;
1068e689cf4aSJeff Kirsher 	}
1069e689cf4aSJeff Kirsher 
1070e689cf4aSJeff Kirsher 	lp->active_speed = current_speed;
1071e689cf4aSJeff Kirsher 	lp->active_duplex = current_duplex;
1072e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
1073e689cf4aSJeff Kirsher 
1074e689cf4aSJeff Kirsher 	*link_up_p = link_up;
1075e689cf4aSJeff Kirsher 	return 0;
1076e689cf4aSJeff Kirsher }
1077e689cf4aSJeff Kirsher 
link_status_10g_serdes(struct niu * np,int * link_up_p)1078e689cf4aSJeff Kirsher static int link_status_10g_serdes(struct niu *np, int *link_up_p)
1079e689cf4aSJeff Kirsher {
1080e689cf4aSJeff Kirsher 	unsigned long flags;
1081e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
1082e689cf4aSJeff Kirsher 	int link_up = 0;
1083e689cf4aSJeff Kirsher 	int link_ok = 1;
1084e689cf4aSJeff Kirsher 	u64 val, val2;
1085e689cf4aSJeff Kirsher 	u16 current_speed;
1086e689cf4aSJeff Kirsher 	u8 current_duplex;
1087e689cf4aSJeff Kirsher 
1088e689cf4aSJeff Kirsher 	if (!(np->flags & NIU_FLAGS_10G))
1089e689cf4aSJeff Kirsher 		return link_status_1g_serdes(np, link_up_p);
1090e689cf4aSJeff Kirsher 
1091e689cf4aSJeff Kirsher 	current_speed = SPEED_INVALID;
1092e689cf4aSJeff Kirsher 	current_duplex = DUPLEX_INVALID;
1093e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
1094e689cf4aSJeff Kirsher 
1095e689cf4aSJeff Kirsher 	val = nr64_xpcs(XPCS_STATUS(0));
1096e689cf4aSJeff Kirsher 	val2 = nr64_mac(XMAC_INTER2);
1097e689cf4aSJeff Kirsher 	if (val2 & 0x01000000)
1098e689cf4aSJeff Kirsher 		link_ok = 0;
1099e689cf4aSJeff Kirsher 
1100e689cf4aSJeff Kirsher 	if ((val & 0x1000ULL) && link_ok) {
1101e689cf4aSJeff Kirsher 		link_up = 1;
1102e689cf4aSJeff Kirsher 		current_speed = SPEED_10000;
1103e689cf4aSJeff Kirsher 		current_duplex = DUPLEX_FULL;
1104e689cf4aSJeff Kirsher 	}
1105e689cf4aSJeff Kirsher 	lp->active_speed = current_speed;
1106e689cf4aSJeff Kirsher 	lp->active_duplex = current_duplex;
1107e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
1108e689cf4aSJeff Kirsher 	*link_up_p = link_up;
1109e689cf4aSJeff Kirsher 	return 0;
1110e689cf4aSJeff Kirsher }
1111e689cf4aSJeff Kirsher 
link_status_mii(struct niu * np,int * link_up_p)1112e689cf4aSJeff Kirsher static int link_status_mii(struct niu *np, int *link_up_p)
1113e689cf4aSJeff Kirsher {
1114e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
1115e689cf4aSJeff Kirsher 	int err;
1116e689cf4aSJeff Kirsher 	int bmsr, advert, ctrl1000, stat1000, lpa, bmcr, estatus;
1117e689cf4aSJeff Kirsher 	int supported, advertising, active_speed, active_duplex;
1118e689cf4aSJeff Kirsher 
1119e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_BMCR);
1120e689cf4aSJeff Kirsher 	if (unlikely(err < 0))
1121e689cf4aSJeff Kirsher 		return err;
1122e689cf4aSJeff Kirsher 	bmcr = err;
1123e689cf4aSJeff Kirsher 
1124e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_BMSR);
1125e689cf4aSJeff Kirsher 	if (unlikely(err < 0))
1126e689cf4aSJeff Kirsher 		return err;
1127e689cf4aSJeff Kirsher 	bmsr = err;
1128e689cf4aSJeff Kirsher 
1129e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_ADVERTISE);
1130e689cf4aSJeff Kirsher 	if (unlikely(err < 0))
1131e689cf4aSJeff Kirsher 		return err;
1132e689cf4aSJeff Kirsher 	advert = err;
1133e689cf4aSJeff Kirsher 
1134e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_LPA);
1135e689cf4aSJeff Kirsher 	if (unlikely(err < 0))
1136e689cf4aSJeff Kirsher 		return err;
1137e689cf4aSJeff Kirsher 	lpa = err;
1138e689cf4aSJeff Kirsher 
1139e689cf4aSJeff Kirsher 	if (likely(bmsr & BMSR_ESTATEN)) {
1140e689cf4aSJeff Kirsher 		err = mii_read(np, np->phy_addr, MII_ESTATUS);
1141e689cf4aSJeff Kirsher 		if (unlikely(err < 0))
1142e689cf4aSJeff Kirsher 			return err;
1143e689cf4aSJeff Kirsher 		estatus = err;
1144e689cf4aSJeff Kirsher 
1145e689cf4aSJeff Kirsher 		err = mii_read(np, np->phy_addr, MII_CTRL1000);
1146e689cf4aSJeff Kirsher 		if (unlikely(err < 0))
1147e689cf4aSJeff Kirsher 			return err;
1148e689cf4aSJeff Kirsher 		ctrl1000 = err;
1149e689cf4aSJeff Kirsher 
1150e689cf4aSJeff Kirsher 		err = mii_read(np, np->phy_addr, MII_STAT1000);
1151e689cf4aSJeff Kirsher 		if (unlikely(err < 0))
1152e689cf4aSJeff Kirsher 			return err;
1153e689cf4aSJeff Kirsher 		stat1000 = err;
1154e689cf4aSJeff Kirsher 	} else
1155e689cf4aSJeff Kirsher 		estatus = ctrl1000 = stat1000 = 0;
1156e689cf4aSJeff Kirsher 
1157e689cf4aSJeff Kirsher 	supported = 0;
1158e689cf4aSJeff Kirsher 	if (bmsr & BMSR_ANEGCAPABLE)
1159e689cf4aSJeff Kirsher 		supported |= SUPPORTED_Autoneg;
1160e689cf4aSJeff Kirsher 	if (bmsr & BMSR_10HALF)
1161e689cf4aSJeff Kirsher 		supported |= SUPPORTED_10baseT_Half;
1162e689cf4aSJeff Kirsher 	if (bmsr & BMSR_10FULL)
1163e689cf4aSJeff Kirsher 		supported |= SUPPORTED_10baseT_Full;
1164e689cf4aSJeff Kirsher 	if (bmsr & BMSR_100HALF)
1165e689cf4aSJeff Kirsher 		supported |= SUPPORTED_100baseT_Half;
1166e689cf4aSJeff Kirsher 	if (bmsr & BMSR_100FULL)
1167e689cf4aSJeff Kirsher 		supported |= SUPPORTED_100baseT_Full;
1168e689cf4aSJeff Kirsher 	if (estatus & ESTATUS_1000_THALF)
1169e689cf4aSJeff Kirsher 		supported |= SUPPORTED_1000baseT_Half;
1170e689cf4aSJeff Kirsher 	if (estatus & ESTATUS_1000_TFULL)
1171e689cf4aSJeff Kirsher 		supported |= SUPPORTED_1000baseT_Full;
1172e689cf4aSJeff Kirsher 	lp->supported = supported;
1173e689cf4aSJeff Kirsher 
117437f07023SMatt Carlson 	advertising = mii_adv_to_ethtool_adv_t(advert);
117537f07023SMatt Carlson 	advertising |= mii_ctrl1000_to_ethtool_adv_t(ctrl1000);
1176e689cf4aSJeff Kirsher 
1177e689cf4aSJeff Kirsher 	if (bmcr & BMCR_ANENABLE) {
1178e689cf4aSJeff Kirsher 		int neg, neg1000;
1179e689cf4aSJeff Kirsher 
1180e689cf4aSJeff Kirsher 		lp->active_autoneg = 1;
1181e689cf4aSJeff Kirsher 		advertising |= ADVERTISED_Autoneg;
1182e689cf4aSJeff Kirsher 
1183e689cf4aSJeff Kirsher 		neg = advert & lpa;
1184e689cf4aSJeff Kirsher 		neg1000 = (ctrl1000 << 2) & stat1000;
1185e689cf4aSJeff Kirsher 
1186e689cf4aSJeff Kirsher 		if (neg1000 & (LPA_1000FULL | LPA_1000HALF))
1187e689cf4aSJeff Kirsher 			active_speed = SPEED_1000;
1188e689cf4aSJeff Kirsher 		else if (neg & LPA_100)
1189e689cf4aSJeff Kirsher 			active_speed = SPEED_100;
1190e689cf4aSJeff Kirsher 		else if (neg & (LPA_10HALF | LPA_10FULL))
1191e689cf4aSJeff Kirsher 			active_speed = SPEED_10;
1192e689cf4aSJeff Kirsher 		else
1193e689cf4aSJeff Kirsher 			active_speed = SPEED_INVALID;
1194e689cf4aSJeff Kirsher 
1195e689cf4aSJeff Kirsher 		if ((neg1000 & LPA_1000FULL) || (neg & LPA_DUPLEX))
1196e689cf4aSJeff Kirsher 			active_duplex = DUPLEX_FULL;
1197e689cf4aSJeff Kirsher 		else if (active_speed != SPEED_INVALID)
1198e689cf4aSJeff Kirsher 			active_duplex = DUPLEX_HALF;
1199e689cf4aSJeff Kirsher 		else
1200e689cf4aSJeff Kirsher 			active_duplex = DUPLEX_INVALID;
1201e689cf4aSJeff Kirsher 	} else {
1202e689cf4aSJeff Kirsher 		lp->active_autoneg = 0;
1203e689cf4aSJeff Kirsher 
1204e689cf4aSJeff Kirsher 		if ((bmcr & BMCR_SPEED1000) && !(bmcr & BMCR_SPEED100))
1205e689cf4aSJeff Kirsher 			active_speed = SPEED_1000;
1206e689cf4aSJeff Kirsher 		else if (bmcr & BMCR_SPEED100)
1207e689cf4aSJeff Kirsher 			active_speed = SPEED_100;
1208e689cf4aSJeff Kirsher 		else
1209e689cf4aSJeff Kirsher 			active_speed = SPEED_10;
1210e689cf4aSJeff Kirsher 
1211e689cf4aSJeff Kirsher 		if (bmcr & BMCR_FULLDPLX)
1212e689cf4aSJeff Kirsher 			active_duplex = DUPLEX_FULL;
1213e689cf4aSJeff Kirsher 		else
1214e689cf4aSJeff Kirsher 			active_duplex = DUPLEX_HALF;
1215e689cf4aSJeff Kirsher 	}
1216e689cf4aSJeff Kirsher 
1217e689cf4aSJeff Kirsher 	lp->active_advertising = advertising;
1218e689cf4aSJeff Kirsher 	lp->active_speed = active_speed;
1219e689cf4aSJeff Kirsher 	lp->active_duplex = active_duplex;
1220e689cf4aSJeff Kirsher 	*link_up_p = !!(bmsr & BMSR_LSTATUS);
1221e689cf4aSJeff Kirsher 
1222e689cf4aSJeff Kirsher 	return 0;
1223e689cf4aSJeff Kirsher }
1224e689cf4aSJeff Kirsher 
link_status_1g_rgmii(struct niu * np,int * link_up_p)1225e689cf4aSJeff Kirsher static int link_status_1g_rgmii(struct niu *np, int *link_up_p)
1226e689cf4aSJeff Kirsher {
1227e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
1228e689cf4aSJeff Kirsher 	u16 current_speed, bmsr;
1229e689cf4aSJeff Kirsher 	unsigned long flags;
1230e689cf4aSJeff Kirsher 	u8 current_duplex;
1231e689cf4aSJeff Kirsher 	int err, link_up;
1232e689cf4aSJeff Kirsher 
1233e689cf4aSJeff Kirsher 	link_up = 0;
1234e689cf4aSJeff Kirsher 	current_speed = SPEED_INVALID;
1235e689cf4aSJeff Kirsher 	current_duplex = DUPLEX_INVALID;
1236e689cf4aSJeff Kirsher 
1237e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
1238e689cf4aSJeff Kirsher 
1239e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_BMSR);
1240e689cf4aSJeff Kirsher 	if (err < 0)
1241e689cf4aSJeff Kirsher 		goto out;
1242e689cf4aSJeff Kirsher 
1243e689cf4aSJeff Kirsher 	bmsr = err;
1244e689cf4aSJeff Kirsher 	if (bmsr & BMSR_LSTATUS) {
1245e689cf4aSJeff Kirsher 		link_up = 1;
1246e689cf4aSJeff Kirsher 		current_speed = SPEED_1000;
1247e689cf4aSJeff Kirsher 		current_duplex = DUPLEX_FULL;
1248e689cf4aSJeff Kirsher 	}
1249e689cf4aSJeff Kirsher 	lp->active_speed = current_speed;
1250e689cf4aSJeff Kirsher 	lp->active_duplex = current_duplex;
1251e689cf4aSJeff Kirsher 	err = 0;
1252e689cf4aSJeff Kirsher 
1253e689cf4aSJeff Kirsher out:
1254e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
1255e689cf4aSJeff Kirsher 
1256e689cf4aSJeff Kirsher 	*link_up_p = link_up;
1257e689cf4aSJeff Kirsher 	return err;
1258e689cf4aSJeff Kirsher }
1259e689cf4aSJeff Kirsher 
link_status_1g(struct niu * np,int * link_up_p)1260e689cf4aSJeff Kirsher static int link_status_1g(struct niu *np, int *link_up_p)
1261e689cf4aSJeff Kirsher {
1262e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
1263e689cf4aSJeff Kirsher 	unsigned long flags;
1264e689cf4aSJeff Kirsher 	int err;
1265e689cf4aSJeff Kirsher 
1266e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
1267e689cf4aSJeff Kirsher 
1268e689cf4aSJeff Kirsher 	err = link_status_mii(np, link_up_p);
1269e689cf4aSJeff Kirsher 	lp->supported |= SUPPORTED_TP;
1270e689cf4aSJeff Kirsher 	lp->active_advertising |= ADVERTISED_TP;
1271e689cf4aSJeff Kirsher 
1272e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
1273e689cf4aSJeff Kirsher 	return err;
1274e689cf4aSJeff Kirsher }
1275e689cf4aSJeff Kirsher 
bcm8704_reset(struct niu * np)1276e689cf4aSJeff Kirsher static int bcm8704_reset(struct niu *np)
1277e689cf4aSJeff Kirsher {
1278e689cf4aSJeff Kirsher 	int err, limit;
1279e689cf4aSJeff Kirsher 
1280e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr,
1281e689cf4aSJeff Kirsher 			BCM8704_PHYXS_DEV_ADDR, MII_BMCR);
1282e689cf4aSJeff Kirsher 	if (err < 0 || err == 0xffff)
1283e689cf4aSJeff Kirsher 		return err;
1284e689cf4aSJeff Kirsher 	err |= BMCR_RESET;
1285e689cf4aSJeff Kirsher 	err = mdio_write(np, np->phy_addr, BCM8704_PHYXS_DEV_ADDR,
1286e689cf4aSJeff Kirsher 			 MII_BMCR, err);
1287e689cf4aSJeff Kirsher 	if (err)
1288e689cf4aSJeff Kirsher 		return err;
1289e689cf4aSJeff Kirsher 
1290e689cf4aSJeff Kirsher 	limit = 1000;
1291e689cf4aSJeff Kirsher 	while (--limit >= 0) {
1292e689cf4aSJeff Kirsher 		err = mdio_read(np, np->phy_addr,
1293e689cf4aSJeff Kirsher 				BCM8704_PHYXS_DEV_ADDR, MII_BMCR);
1294e689cf4aSJeff Kirsher 		if (err < 0)
1295e689cf4aSJeff Kirsher 			return err;
1296e689cf4aSJeff Kirsher 		if (!(err & BMCR_RESET))
1297e689cf4aSJeff Kirsher 			break;
1298e689cf4aSJeff Kirsher 	}
1299e689cf4aSJeff Kirsher 	if (limit < 0) {
1300e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Port %u PHY will not reset (bmcr=%04x)\n",
1301e689cf4aSJeff Kirsher 			   np->port, (err & 0xffff));
1302e689cf4aSJeff Kirsher 		return -ENODEV;
1303e689cf4aSJeff Kirsher 	}
1304e689cf4aSJeff Kirsher 	return 0;
1305e689cf4aSJeff Kirsher }
1306e689cf4aSJeff Kirsher 
1307e689cf4aSJeff Kirsher /* When written, certain PHY registers need to be read back twice
1308e689cf4aSJeff Kirsher  * in order for the bits to settle properly.
1309e689cf4aSJeff Kirsher  */
bcm8704_user_dev3_readback(struct niu * np,int reg)1310e689cf4aSJeff Kirsher static int bcm8704_user_dev3_readback(struct niu *np, int reg)
1311e689cf4aSJeff Kirsher {
1312e689cf4aSJeff Kirsher 	int err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR, reg);
1313e689cf4aSJeff Kirsher 	if (err < 0)
1314e689cf4aSJeff Kirsher 		return err;
1315e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR, reg);
1316e689cf4aSJeff Kirsher 	if (err < 0)
1317e689cf4aSJeff Kirsher 		return err;
1318e689cf4aSJeff Kirsher 	return 0;
1319e689cf4aSJeff Kirsher }
1320e689cf4aSJeff Kirsher 
bcm8706_init_user_dev3(struct niu * np)1321e689cf4aSJeff Kirsher static int bcm8706_init_user_dev3(struct niu *np)
1322e689cf4aSJeff Kirsher {
1323e689cf4aSJeff Kirsher 	int err;
1324e689cf4aSJeff Kirsher 
1325e689cf4aSJeff Kirsher 
1326e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
1327e689cf4aSJeff Kirsher 			BCM8704_USER_OPT_DIGITAL_CTRL);
1328e689cf4aSJeff Kirsher 	if (err < 0)
1329e689cf4aSJeff Kirsher 		return err;
1330e689cf4aSJeff Kirsher 	err &= ~USER_ODIG_CTRL_GPIOS;
1331e689cf4aSJeff Kirsher 	err |= (0x3 << USER_ODIG_CTRL_GPIOS_SHIFT);
1332e689cf4aSJeff Kirsher 	err |=  USER_ODIG_CTRL_RESV2;
1333e689cf4aSJeff Kirsher 	err = mdio_write(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
1334e689cf4aSJeff Kirsher 			 BCM8704_USER_OPT_DIGITAL_CTRL, err);
1335e689cf4aSJeff Kirsher 	if (err)
1336e689cf4aSJeff Kirsher 		return err;
1337e689cf4aSJeff Kirsher 
1338e689cf4aSJeff Kirsher 	mdelay(1000);
1339e689cf4aSJeff Kirsher 
1340e689cf4aSJeff Kirsher 	return 0;
1341e689cf4aSJeff Kirsher }
1342e689cf4aSJeff Kirsher 
bcm8704_init_user_dev3(struct niu * np)1343e689cf4aSJeff Kirsher static int bcm8704_init_user_dev3(struct niu *np)
1344e689cf4aSJeff Kirsher {
1345e689cf4aSJeff Kirsher 	int err;
1346e689cf4aSJeff Kirsher 
1347e689cf4aSJeff Kirsher 	err = mdio_write(np, np->phy_addr,
1348e689cf4aSJeff Kirsher 			 BCM8704_USER_DEV3_ADDR, BCM8704_USER_CONTROL,
1349e689cf4aSJeff Kirsher 			 (USER_CONTROL_OPTXRST_LVL |
1350e689cf4aSJeff Kirsher 			  USER_CONTROL_OPBIASFLT_LVL |
1351e689cf4aSJeff Kirsher 			  USER_CONTROL_OBTMPFLT_LVL |
1352e689cf4aSJeff Kirsher 			  USER_CONTROL_OPPRFLT_LVL |
1353e689cf4aSJeff Kirsher 			  USER_CONTROL_OPTXFLT_LVL |
1354e689cf4aSJeff Kirsher 			  USER_CONTROL_OPRXLOS_LVL |
1355e689cf4aSJeff Kirsher 			  USER_CONTROL_OPRXFLT_LVL |
1356e689cf4aSJeff Kirsher 			  USER_CONTROL_OPTXON_LVL |
1357e689cf4aSJeff Kirsher 			  (0x3f << USER_CONTROL_RES1_SHIFT)));
1358e689cf4aSJeff Kirsher 	if (err)
1359e689cf4aSJeff Kirsher 		return err;
1360e689cf4aSJeff Kirsher 
1361e689cf4aSJeff Kirsher 	err = mdio_write(np, np->phy_addr,
1362e689cf4aSJeff Kirsher 			 BCM8704_USER_DEV3_ADDR, BCM8704_USER_PMD_TX_CONTROL,
1363e689cf4aSJeff Kirsher 			 (USER_PMD_TX_CTL_XFP_CLKEN |
1364e689cf4aSJeff Kirsher 			  (1 << USER_PMD_TX_CTL_TX_DAC_TXD_SH) |
1365e689cf4aSJeff Kirsher 			  (2 << USER_PMD_TX_CTL_TX_DAC_TXCK_SH) |
1366e689cf4aSJeff Kirsher 			  USER_PMD_TX_CTL_TSCK_LPWREN));
1367e689cf4aSJeff Kirsher 	if (err)
1368e689cf4aSJeff Kirsher 		return err;
1369e689cf4aSJeff Kirsher 
1370e689cf4aSJeff Kirsher 	err = bcm8704_user_dev3_readback(np, BCM8704_USER_CONTROL);
1371e689cf4aSJeff Kirsher 	if (err)
1372e689cf4aSJeff Kirsher 		return err;
1373e689cf4aSJeff Kirsher 	err = bcm8704_user_dev3_readback(np, BCM8704_USER_PMD_TX_CONTROL);
1374e689cf4aSJeff Kirsher 	if (err)
1375e689cf4aSJeff Kirsher 		return err;
1376e689cf4aSJeff Kirsher 
1377e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
1378e689cf4aSJeff Kirsher 			BCM8704_USER_OPT_DIGITAL_CTRL);
1379e689cf4aSJeff Kirsher 	if (err < 0)
1380e689cf4aSJeff Kirsher 		return err;
1381e689cf4aSJeff Kirsher 	err &= ~USER_ODIG_CTRL_GPIOS;
1382e689cf4aSJeff Kirsher 	err |= (0x3 << USER_ODIG_CTRL_GPIOS_SHIFT);
1383e689cf4aSJeff Kirsher 	err = mdio_write(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
1384e689cf4aSJeff Kirsher 			 BCM8704_USER_OPT_DIGITAL_CTRL, err);
1385e689cf4aSJeff Kirsher 	if (err)
1386e689cf4aSJeff Kirsher 		return err;
1387e689cf4aSJeff Kirsher 
1388e689cf4aSJeff Kirsher 	mdelay(1000);
1389e689cf4aSJeff Kirsher 
1390e689cf4aSJeff Kirsher 	return 0;
1391e689cf4aSJeff Kirsher }
1392e689cf4aSJeff Kirsher 
mrvl88x2011_act_led(struct niu * np,int val)1393e689cf4aSJeff Kirsher static int mrvl88x2011_act_led(struct niu *np, int val)
1394e689cf4aSJeff Kirsher {
1395e689cf4aSJeff Kirsher 	int	err;
1396e689cf4aSJeff Kirsher 
1397e689cf4aSJeff Kirsher 	err  = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV2_ADDR,
1398e689cf4aSJeff Kirsher 		MRVL88X2011_LED_8_TO_11_CTL);
1399e689cf4aSJeff Kirsher 	if (err < 0)
1400e689cf4aSJeff Kirsher 		return err;
1401e689cf4aSJeff Kirsher 
1402e689cf4aSJeff Kirsher 	err &= ~MRVL88X2011_LED(MRVL88X2011_LED_ACT,MRVL88X2011_LED_CTL_MASK);
1403e689cf4aSJeff Kirsher 	err |=  MRVL88X2011_LED(MRVL88X2011_LED_ACT,val);
1404e689cf4aSJeff Kirsher 
1405e689cf4aSJeff Kirsher 	return mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV2_ADDR,
1406e689cf4aSJeff Kirsher 			  MRVL88X2011_LED_8_TO_11_CTL, err);
1407e689cf4aSJeff Kirsher }
1408e689cf4aSJeff Kirsher 
mrvl88x2011_led_blink_rate(struct niu * np,int rate)1409e689cf4aSJeff Kirsher static int mrvl88x2011_led_blink_rate(struct niu *np, int rate)
1410e689cf4aSJeff Kirsher {
1411e689cf4aSJeff Kirsher 	int	err;
1412e689cf4aSJeff Kirsher 
1413e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV2_ADDR,
1414e689cf4aSJeff Kirsher 			MRVL88X2011_LED_BLINK_CTL);
1415e689cf4aSJeff Kirsher 	if (err >= 0) {
1416e689cf4aSJeff Kirsher 		err &= ~MRVL88X2011_LED_BLKRATE_MASK;
1417e689cf4aSJeff Kirsher 		err |= (rate << 4);
1418e689cf4aSJeff Kirsher 
1419e689cf4aSJeff Kirsher 		err = mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV2_ADDR,
1420e689cf4aSJeff Kirsher 				 MRVL88X2011_LED_BLINK_CTL, err);
1421e689cf4aSJeff Kirsher 	}
1422e689cf4aSJeff Kirsher 
1423e689cf4aSJeff Kirsher 	return err;
1424e689cf4aSJeff Kirsher }
1425e689cf4aSJeff Kirsher 
xcvr_init_10g_mrvl88x2011(struct niu * np)1426e689cf4aSJeff Kirsher static int xcvr_init_10g_mrvl88x2011(struct niu *np)
1427e689cf4aSJeff Kirsher {
1428e689cf4aSJeff Kirsher 	int	err;
1429e689cf4aSJeff Kirsher 
1430e689cf4aSJeff Kirsher 	/* Set LED functions */
1431e689cf4aSJeff Kirsher 	err = mrvl88x2011_led_blink_rate(np, MRVL88X2011_LED_BLKRATE_134MS);
1432e689cf4aSJeff Kirsher 	if (err)
1433e689cf4aSJeff Kirsher 		return err;
1434e689cf4aSJeff Kirsher 
1435e689cf4aSJeff Kirsher 	/* led activity */
1436e689cf4aSJeff Kirsher 	err = mrvl88x2011_act_led(np, MRVL88X2011_LED_CTL_OFF);
1437e689cf4aSJeff Kirsher 	if (err)
1438e689cf4aSJeff Kirsher 		return err;
1439e689cf4aSJeff Kirsher 
1440e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV3_ADDR,
1441e689cf4aSJeff Kirsher 			MRVL88X2011_GENERAL_CTL);
1442e689cf4aSJeff Kirsher 	if (err < 0)
1443e689cf4aSJeff Kirsher 		return err;
1444e689cf4aSJeff Kirsher 
1445e689cf4aSJeff Kirsher 	err |= MRVL88X2011_ENA_XFPREFCLK;
1446e689cf4aSJeff Kirsher 
1447e689cf4aSJeff Kirsher 	err = mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV3_ADDR,
1448e689cf4aSJeff Kirsher 			 MRVL88X2011_GENERAL_CTL, err);
1449e689cf4aSJeff Kirsher 	if (err < 0)
1450e689cf4aSJeff Kirsher 		return err;
1451e689cf4aSJeff Kirsher 
1452e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
1453e689cf4aSJeff Kirsher 			MRVL88X2011_PMA_PMD_CTL_1);
1454e689cf4aSJeff Kirsher 	if (err < 0)
1455e689cf4aSJeff Kirsher 		return err;
1456e689cf4aSJeff Kirsher 
1457e689cf4aSJeff Kirsher 	if (np->link_config.loopback_mode == LOOPBACK_MAC)
1458e689cf4aSJeff Kirsher 		err |= MRVL88X2011_LOOPBACK;
1459e689cf4aSJeff Kirsher 	else
1460e689cf4aSJeff Kirsher 		err &= ~MRVL88X2011_LOOPBACK;
1461e689cf4aSJeff Kirsher 
1462e689cf4aSJeff Kirsher 	err = mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
1463e689cf4aSJeff Kirsher 			 MRVL88X2011_PMA_PMD_CTL_1, err);
1464e689cf4aSJeff Kirsher 	if (err < 0)
1465e689cf4aSJeff Kirsher 		return err;
1466e689cf4aSJeff Kirsher 
1467e689cf4aSJeff Kirsher 	/* Enable PMD  */
1468e689cf4aSJeff Kirsher 	return mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
1469e689cf4aSJeff Kirsher 			  MRVL88X2011_10G_PMD_TX_DIS, MRVL88X2011_ENA_PMDTX);
1470e689cf4aSJeff Kirsher }
1471e689cf4aSJeff Kirsher 
1472e689cf4aSJeff Kirsher 
xcvr_diag_bcm870x(struct niu * np)1473e689cf4aSJeff Kirsher static int xcvr_diag_bcm870x(struct niu *np)
1474e689cf4aSJeff Kirsher {
1475e689cf4aSJeff Kirsher 	u16 analog_stat0, tx_alarm_status;
1476e689cf4aSJeff Kirsher 	int err = 0;
1477e689cf4aSJeff Kirsher 
1478e689cf4aSJeff Kirsher #if 1
1479e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_PMA_PMD_DEV_ADDR,
1480e689cf4aSJeff Kirsher 			MII_STAT1000);
1481e689cf4aSJeff Kirsher 	if (err < 0)
1482e689cf4aSJeff Kirsher 		return err;
1483e689cf4aSJeff Kirsher 	pr_info("Port %u PMA_PMD(MII_STAT1000) [%04x]\n", np->port, err);
1484e689cf4aSJeff Kirsher 
1485e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR, 0x20);
1486e689cf4aSJeff Kirsher 	if (err < 0)
1487e689cf4aSJeff Kirsher 		return err;
1488e689cf4aSJeff Kirsher 	pr_info("Port %u USER_DEV3(0x20) [%04x]\n", np->port, err);
1489e689cf4aSJeff Kirsher 
1490e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_PHYXS_DEV_ADDR,
1491e689cf4aSJeff Kirsher 			MII_NWAYTEST);
1492e689cf4aSJeff Kirsher 	if (err < 0)
1493e689cf4aSJeff Kirsher 		return err;
1494e689cf4aSJeff Kirsher 	pr_info("Port %u PHYXS(MII_NWAYTEST) [%04x]\n", np->port, err);
1495e689cf4aSJeff Kirsher #endif
1496e689cf4aSJeff Kirsher 
1497e689cf4aSJeff Kirsher 	/* XXX dig this out it might not be so useful XXX */
1498e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
1499e689cf4aSJeff Kirsher 			BCM8704_USER_ANALOG_STATUS0);
1500e689cf4aSJeff Kirsher 	if (err < 0)
1501e689cf4aSJeff Kirsher 		return err;
1502e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
1503e689cf4aSJeff Kirsher 			BCM8704_USER_ANALOG_STATUS0);
1504e689cf4aSJeff Kirsher 	if (err < 0)
1505e689cf4aSJeff Kirsher 		return err;
1506e689cf4aSJeff Kirsher 	analog_stat0 = err;
1507e689cf4aSJeff Kirsher 
1508e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
1509e689cf4aSJeff Kirsher 			BCM8704_USER_TX_ALARM_STATUS);
1510e689cf4aSJeff Kirsher 	if (err < 0)
1511e689cf4aSJeff Kirsher 		return err;
1512e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
1513e689cf4aSJeff Kirsher 			BCM8704_USER_TX_ALARM_STATUS);
1514e689cf4aSJeff Kirsher 	if (err < 0)
1515e689cf4aSJeff Kirsher 		return err;
1516e689cf4aSJeff Kirsher 	tx_alarm_status = err;
1517e689cf4aSJeff Kirsher 
1518e689cf4aSJeff Kirsher 	if (analog_stat0 != 0x03fc) {
1519e689cf4aSJeff Kirsher 		if ((analog_stat0 == 0x43bc) && (tx_alarm_status != 0)) {
1520e689cf4aSJeff Kirsher 			pr_info("Port %u cable not connected or bad cable\n",
1521e689cf4aSJeff Kirsher 				np->port);
1522e689cf4aSJeff Kirsher 		} else if (analog_stat0 == 0x639c) {
1523e689cf4aSJeff Kirsher 			pr_info("Port %u optical module is bad or missing\n",
1524e689cf4aSJeff Kirsher 				np->port);
1525e689cf4aSJeff Kirsher 		}
1526e689cf4aSJeff Kirsher 	}
1527e689cf4aSJeff Kirsher 
1528e689cf4aSJeff Kirsher 	return 0;
1529e689cf4aSJeff Kirsher }
1530e689cf4aSJeff Kirsher 
xcvr_10g_set_lb_bcm870x(struct niu * np)1531e689cf4aSJeff Kirsher static int xcvr_10g_set_lb_bcm870x(struct niu *np)
1532e689cf4aSJeff Kirsher {
1533e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
1534e689cf4aSJeff Kirsher 	int err;
1535e689cf4aSJeff Kirsher 
1536e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_PCS_DEV_ADDR,
1537e689cf4aSJeff Kirsher 			MII_BMCR);
1538e689cf4aSJeff Kirsher 	if (err < 0)
1539e689cf4aSJeff Kirsher 		return err;
1540e689cf4aSJeff Kirsher 
1541e689cf4aSJeff Kirsher 	err &= ~BMCR_LOOPBACK;
1542e689cf4aSJeff Kirsher 
1543e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_MAC)
1544e689cf4aSJeff Kirsher 		err |= BMCR_LOOPBACK;
1545e689cf4aSJeff Kirsher 
1546e689cf4aSJeff Kirsher 	err = mdio_write(np, np->phy_addr, BCM8704_PCS_DEV_ADDR,
1547e689cf4aSJeff Kirsher 			 MII_BMCR, err);
1548e689cf4aSJeff Kirsher 	if (err)
1549e689cf4aSJeff Kirsher 		return err;
1550e689cf4aSJeff Kirsher 
1551e689cf4aSJeff Kirsher 	return 0;
1552e689cf4aSJeff Kirsher }
1553e689cf4aSJeff Kirsher 
xcvr_init_10g_bcm8706(struct niu * np)1554e689cf4aSJeff Kirsher static int xcvr_init_10g_bcm8706(struct niu *np)
1555e689cf4aSJeff Kirsher {
1556e689cf4aSJeff Kirsher 	int err = 0;
1557e689cf4aSJeff Kirsher 	u64 val;
1558e689cf4aSJeff Kirsher 
1559e689cf4aSJeff Kirsher 	if ((np->flags & NIU_FLAGS_HOTPLUG_PHY) &&
1560e689cf4aSJeff Kirsher 	    (np->flags & NIU_FLAGS_HOTPLUG_PHY_PRESENT) == 0)
1561e689cf4aSJeff Kirsher 			return err;
1562e689cf4aSJeff Kirsher 
1563e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_CONFIG);
1564e689cf4aSJeff Kirsher 	val &= ~XMAC_CONFIG_LED_POLARITY;
1565e689cf4aSJeff Kirsher 	val |= XMAC_CONFIG_FORCE_LED_ON;
1566e689cf4aSJeff Kirsher 	nw64_mac(XMAC_CONFIG, val);
1567e689cf4aSJeff Kirsher 
1568e689cf4aSJeff Kirsher 	val = nr64(MIF_CONFIG);
1569e689cf4aSJeff Kirsher 	val |= MIF_CONFIG_INDIRECT_MODE;
1570e689cf4aSJeff Kirsher 	nw64(MIF_CONFIG, val);
1571e689cf4aSJeff Kirsher 
1572e689cf4aSJeff Kirsher 	err = bcm8704_reset(np);
1573e689cf4aSJeff Kirsher 	if (err)
1574e689cf4aSJeff Kirsher 		return err;
1575e689cf4aSJeff Kirsher 
1576e689cf4aSJeff Kirsher 	err = xcvr_10g_set_lb_bcm870x(np);
1577e689cf4aSJeff Kirsher 	if (err)
1578e689cf4aSJeff Kirsher 		return err;
1579e689cf4aSJeff Kirsher 
1580e689cf4aSJeff Kirsher 	err = bcm8706_init_user_dev3(np);
1581e689cf4aSJeff Kirsher 	if (err)
1582e689cf4aSJeff Kirsher 		return err;
1583e689cf4aSJeff Kirsher 
1584e689cf4aSJeff Kirsher 	err = xcvr_diag_bcm870x(np);
1585e689cf4aSJeff Kirsher 	if (err)
1586e689cf4aSJeff Kirsher 		return err;
1587e689cf4aSJeff Kirsher 
1588e689cf4aSJeff Kirsher 	return 0;
1589e689cf4aSJeff Kirsher }
1590e689cf4aSJeff Kirsher 
xcvr_init_10g_bcm8704(struct niu * np)1591e689cf4aSJeff Kirsher static int xcvr_init_10g_bcm8704(struct niu *np)
1592e689cf4aSJeff Kirsher {
1593e689cf4aSJeff Kirsher 	int err;
1594e689cf4aSJeff Kirsher 
1595e689cf4aSJeff Kirsher 	err = bcm8704_reset(np);
1596e689cf4aSJeff Kirsher 	if (err)
1597e689cf4aSJeff Kirsher 		return err;
1598e689cf4aSJeff Kirsher 
1599e689cf4aSJeff Kirsher 	err = bcm8704_init_user_dev3(np);
1600e689cf4aSJeff Kirsher 	if (err)
1601e689cf4aSJeff Kirsher 		return err;
1602e689cf4aSJeff Kirsher 
1603e689cf4aSJeff Kirsher 	err = xcvr_10g_set_lb_bcm870x(np);
1604e689cf4aSJeff Kirsher 	if (err)
1605e689cf4aSJeff Kirsher 		return err;
1606e689cf4aSJeff Kirsher 
1607e689cf4aSJeff Kirsher 	err =  xcvr_diag_bcm870x(np);
1608e689cf4aSJeff Kirsher 	if (err)
1609e689cf4aSJeff Kirsher 		return err;
1610e689cf4aSJeff Kirsher 
1611e689cf4aSJeff Kirsher 	return 0;
1612e689cf4aSJeff Kirsher }
1613e689cf4aSJeff Kirsher 
xcvr_init_10g(struct niu * np)1614e689cf4aSJeff Kirsher static int xcvr_init_10g(struct niu *np)
1615e689cf4aSJeff Kirsher {
1616e689cf4aSJeff Kirsher 	int phy_id, err;
1617e689cf4aSJeff Kirsher 	u64 val;
1618e689cf4aSJeff Kirsher 
1619e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_CONFIG);
1620e689cf4aSJeff Kirsher 	val &= ~XMAC_CONFIG_LED_POLARITY;
1621e689cf4aSJeff Kirsher 	val |= XMAC_CONFIG_FORCE_LED_ON;
1622e689cf4aSJeff Kirsher 	nw64_mac(XMAC_CONFIG, val);
1623e689cf4aSJeff Kirsher 
1624e689cf4aSJeff Kirsher 	/* XXX shared resource, lock parent XXX */
1625e689cf4aSJeff Kirsher 	val = nr64(MIF_CONFIG);
1626e689cf4aSJeff Kirsher 	val |= MIF_CONFIG_INDIRECT_MODE;
1627e689cf4aSJeff Kirsher 	nw64(MIF_CONFIG, val);
1628e689cf4aSJeff Kirsher 
1629e689cf4aSJeff Kirsher 	phy_id = phy_decode(np->parent->port_phy, np->port);
1630e689cf4aSJeff Kirsher 	phy_id = np->parent->phy_probe_info.phy_id[phy_id][np->port];
1631e689cf4aSJeff Kirsher 
1632e689cf4aSJeff Kirsher 	/* handle different phy types */
1633e689cf4aSJeff Kirsher 	switch (phy_id & NIU_PHY_ID_MASK) {
1634e689cf4aSJeff Kirsher 	case NIU_PHY_ID_MRVL88X2011:
1635e689cf4aSJeff Kirsher 		err = xcvr_init_10g_mrvl88x2011(np);
1636e689cf4aSJeff Kirsher 		break;
1637e689cf4aSJeff Kirsher 
1638e689cf4aSJeff Kirsher 	default: /* bcom 8704 */
1639e689cf4aSJeff Kirsher 		err = xcvr_init_10g_bcm8704(np);
1640e689cf4aSJeff Kirsher 		break;
1641e689cf4aSJeff Kirsher 	}
1642e689cf4aSJeff Kirsher 
1643e689cf4aSJeff Kirsher 	return err;
1644e689cf4aSJeff Kirsher }
1645e689cf4aSJeff Kirsher 
mii_reset(struct niu * np)1646e689cf4aSJeff Kirsher static int mii_reset(struct niu *np)
1647e689cf4aSJeff Kirsher {
1648e689cf4aSJeff Kirsher 	int limit, err;
1649e689cf4aSJeff Kirsher 
1650e689cf4aSJeff Kirsher 	err = mii_write(np, np->phy_addr, MII_BMCR, BMCR_RESET);
1651e689cf4aSJeff Kirsher 	if (err)
1652e689cf4aSJeff Kirsher 		return err;
1653e689cf4aSJeff Kirsher 
1654e689cf4aSJeff Kirsher 	limit = 1000;
1655e689cf4aSJeff Kirsher 	while (--limit >= 0) {
1656e689cf4aSJeff Kirsher 		udelay(500);
1657e689cf4aSJeff Kirsher 		err = mii_read(np, np->phy_addr, MII_BMCR);
1658e689cf4aSJeff Kirsher 		if (err < 0)
1659e689cf4aSJeff Kirsher 			return err;
1660e689cf4aSJeff Kirsher 		if (!(err & BMCR_RESET))
1661e689cf4aSJeff Kirsher 			break;
1662e689cf4aSJeff Kirsher 	}
1663e689cf4aSJeff Kirsher 	if (limit < 0) {
1664e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Port %u MII would not reset, bmcr[%04x]\n",
1665e689cf4aSJeff Kirsher 			   np->port, err);
1666e689cf4aSJeff Kirsher 		return -ENODEV;
1667e689cf4aSJeff Kirsher 	}
1668e689cf4aSJeff Kirsher 
1669e689cf4aSJeff Kirsher 	return 0;
1670e689cf4aSJeff Kirsher }
1671e689cf4aSJeff Kirsher 
xcvr_init_1g_rgmii(struct niu * np)1672e689cf4aSJeff Kirsher static int xcvr_init_1g_rgmii(struct niu *np)
1673e689cf4aSJeff Kirsher {
1674e689cf4aSJeff Kirsher 	int err;
1675e689cf4aSJeff Kirsher 	u64 val;
1676e689cf4aSJeff Kirsher 	u16 bmcr, bmsr, estat;
1677e689cf4aSJeff Kirsher 
1678e689cf4aSJeff Kirsher 	val = nr64(MIF_CONFIG);
1679e689cf4aSJeff Kirsher 	val &= ~MIF_CONFIG_INDIRECT_MODE;
1680e689cf4aSJeff Kirsher 	nw64(MIF_CONFIG, val);
1681e689cf4aSJeff Kirsher 
1682e689cf4aSJeff Kirsher 	err = mii_reset(np);
1683e689cf4aSJeff Kirsher 	if (err)
1684e689cf4aSJeff Kirsher 		return err;
1685e689cf4aSJeff Kirsher 
1686e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_BMSR);
1687e689cf4aSJeff Kirsher 	if (err < 0)
1688e689cf4aSJeff Kirsher 		return err;
1689e689cf4aSJeff Kirsher 	bmsr = err;
1690e689cf4aSJeff Kirsher 
1691e689cf4aSJeff Kirsher 	estat = 0;
1692e689cf4aSJeff Kirsher 	if (bmsr & BMSR_ESTATEN) {
1693e689cf4aSJeff Kirsher 		err = mii_read(np, np->phy_addr, MII_ESTATUS);
1694e689cf4aSJeff Kirsher 		if (err < 0)
1695e689cf4aSJeff Kirsher 			return err;
1696e689cf4aSJeff Kirsher 		estat = err;
1697e689cf4aSJeff Kirsher 	}
1698e689cf4aSJeff Kirsher 
1699e689cf4aSJeff Kirsher 	bmcr = 0;
1700e689cf4aSJeff Kirsher 	err = mii_write(np, np->phy_addr, MII_BMCR, bmcr);
1701e689cf4aSJeff Kirsher 	if (err)
1702e689cf4aSJeff Kirsher 		return err;
1703e689cf4aSJeff Kirsher 
1704e689cf4aSJeff Kirsher 	if (bmsr & BMSR_ESTATEN) {
1705e689cf4aSJeff Kirsher 		u16 ctrl1000 = 0;
1706e689cf4aSJeff Kirsher 
1707e689cf4aSJeff Kirsher 		if (estat & ESTATUS_1000_TFULL)
1708e689cf4aSJeff Kirsher 			ctrl1000 |= ADVERTISE_1000FULL;
1709e689cf4aSJeff Kirsher 		err = mii_write(np, np->phy_addr, MII_CTRL1000, ctrl1000);
1710e689cf4aSJeff Kirsher 		if (err)
1711e689cf4aSJeff Kirsher 			return err;
1712e689cf4aSJeff Kirsher 	}
1713e689cf4aSJeff Kirsher 
1714e689cf4aSJeff Kirsher 	bmcr = (BMCR_SPEED1000 | BMCR_FULLDPLX);
1715e689cf4aSJeff Kirsher 
1716e689cf4aSJeff Kirsher 	err = mii_write(np, np->phy_addr, MII_BMCR, bmcr);
1717e689cf4aSJeff Kirsher 	if (err)
1718e689cf4aSJeff Kirsher 		return err;
1719e689cf4aSJeff Kirsher 
1720e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_BMCR);
1721e689cf4aSJeff Kirsher 	if (err < 0)
1722e689cf4aSJeff Kirsher 		return err;
1723e689cf4aSJeff Kirsher 	bmcr = mii_read(np, np->phy_addr, MII_BMCR);
1724e689cf4aSJeff Kirsher 
1725e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_BMSR);
1726e689cf4aSJeff Kirsher 	if (err < 0)
1727e689cf4aSJeff Kirsher 		return err;
1728e689cf4aSJeff Kirsher 
1729e689cf4aSJeff Kirsher 	return 0;
1730e689cf4aSJeff Kirsher }
1731e689cf4aSJeff Kirsher 
mii_init_common(struct niu * np)1732e689cf4aSJeff Kirsher static int mii_init_common(struct niu *np)
1733e689cf4aSJeff Kirsher {
1734e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
1735e689cf4aSJeff Kirsher 	u16 bmcr, bmsr, adv, estat;
1736e689cf4aSJeff Kirsher 	int err;
1737e689cf4aSJeff Kirsher 
1738e689cf4aSJeff Kirsher 	err = mii_reset(np);
1739e689cf4aSJeff Kirsher 	if (err)
1740e689cf4aSJeff Kirsher 		return err;
1741e689cf4aSJeff Kirsher 
1742e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_BMSR);
1743e689cf4aSJeff Kirsher 	if (err < 0)
1744e689cf4aSJeff Kirsher 		return err;
1745e689cf4aSJeff Kirsher 	bmsr = err;
1746e689cf4aSJeff Kirsher 
1747e689cf4aSJeff Kirsher 	estat = 0;
1748e689cf4aSJeff Kirsher 	if (bmsr & BMSR_ESTATEN) {
1749e689cf4aSJeff Kirsher 		err = mii_read(np, np->phy_addr, MII_ESTATUS);
1750e689cf4aSJeff Kirsher 		if (err < 0)
1751e689cf4aSJeff Kirsher 			return err;
1752e689cf4aSJeff Kirsher 		estat = err;
1753e689cf4aSJeff Kirsher 	}
1754e689cf4aSJeff Kirsher 
1755e689cf4aSJeff Kirsher 	bmcr = 0;
1756e689cf4aSJeff Kirsher 	err = mii_write(np, np->phy_addr, MII_BMCR, bmcr);
1757e689cf4aSJeff Kirsher 	if (err)
1758e689cf4aSJeff Kirsher 		return err;
1759e689cf4aSJeff Kirsher 
1760e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_MAC) {
1761e689cf4aSJeff Kirsher 		bmcr |= BMCR_LOOPBACK;
1762e689cf4aSJeff Kirsher 		if (lp->active_speed == SPEED_1000)
1763e689cf4aSJeff Kirsher 			bmcr |= BMCR_SPEED1000;
1764e689cf4aSJeff Kirsher 		if (lp->active_duplex == DUPLEX_FULL)
1765e689cf4aSJeff Kirsher 			bmcr |= BMCR_FULLDPLX;
1766e689cf4aSJeff Kirsher 	}
1767e689cf4aSJeff Kirsher 
1768e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_PHY) {
1769e689cf4aSJeff Kirsher 		u16 aux;
1770e689cf4aSJeff Kirsher 
1771e689cf4aSJeff Kirsher 		aux = (BCM5464R_AUX_CTL_EXT_LB |
1772e689cf4aSJeff Kirsher 		       BCM5464R_AUX_CTL_WRITE_1);
1773e689cf4aSJeff Kirsher 		err = mii_write(np, np->phy_addr, BCM5464R_AUX_CTL, aux);
1774e689cf4aSJeff Kirsher 		if (err)
1775e689cf4aSJeff Kirsher 			return err;
1776e689cf4aSJeff Kirsher 	}
1777e689cf4aSJeff Kirsher 
1778e689cf4aSJeff Kirsher 	if (lp->autoneg) {
1779e689cf4aSJeff Kirsher 		u16 ctrl1000;
1780e689cf4aSJeff Kirsher 
1781e689cf4aSJeff Kirsher 		adv = ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP;
1782e689cf4aSJeff Kirsher 		if ((bmsr & BMSR_10HALF) &&
1783e689cf4aSJeff Kirsher 			(lp->advertising & ADVERTISED_10baseT_Half))
1784e689cf4aSJeff Kirsher 			adv |= ADVERTISE_10HALF;
1785e689cf4aSJeff Kirsher 		if ((bmsr & BMSR_10FULL) &&
1786e689cf4aSJeff Kirsher 			(lp->advertising & ADVERTISED_10baseT_Full))
1787e689cf4aSJeff Kirsher 			adv |= ADVERTISE_10FULL;
1788e689cf4aSJeff Kirsher 		if ((bmsr & BMSR_100HALF) &&
1789e689cf4aSJeff Kirsher 			(lp->advertising & ADVERTISED_100baseT_Half))
1790e689cf4aSJeff Kirsher 			adv |= ADVERTISE_100HALF;
1791e689cf4aSJeff Kirsher 		if ((bmsr & BMSR_100FULL) &&
1792e689cf4aSJeff Kirsher 			(lp->advertising & ADVERTISED_100baseT_Full))
1793e689cf4aSJeff Kirsher 			adv |= ADVERTISE_100FULL;
1794e689cf4aSJeff Kirsher 		err = mii_write(np, np->phy_addr, MII_ADVERTISE, adv);
1795e689cf4aSJeff Kirsher 		if (err)
1796e689cf4aSJeff Kirsher 			return err;
1797e689cf4aSJeff Kirsher 
1798e689cf4aSJeff Kirsher 		if (likely(bmsr & BMSR_ESTATEN)) {
1799e689cf4aSJeff Kirsher 			ctrl1000 = 0;
1800e689cf4aSJeff Kirsher 			if ((estat & ESTATUS_1000_THALF) &&
1801e689cf4aSJeff Kirsher 				(lp->advertising & ADVERTISED_1000baseT_Half))
1802e689cf4aSJeff Kirsher 				ctrl1000 |= ADVERTISE_1000HALF;
1803e689cf4aSJeff Kirsher 			if ((estat & ESTATUS_1000_TFULL) &&
1804e689cf4aSJeff Kirsher 				(lp->advertising & ADVERTISED_1000baseT_Full))
1805e689cf4aSJeff Kirsher 				ctrl1000 |= ADVERTISE_1000FULL;
1806e689cf4aSJeff Kirsher 			err = mii_write(np, np->phy_addr,
1807e689cf4aSJeff Kirsher 					MII_CTRL1000, ctrl1000);
1808e689cf4aSJeff Kirsher 			if (err)
1809e689cf4aSJeff Kirsher 				return err;
1810e689cf4aSJeff Kirsher 		}
1811e689cf4aSJeff Kirsher 
1812e689cf4aSJeff Kirsher 		bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
1813e689cf4aSJeff Kirsher 	} else {
1814e689cf4aSJeff Kirsher 		/* !lp->autoneg */
1815e689cf4aSJeff Kirsher 		int fulldpx;
1816e689cf4aSJeff Kirsher 
1817e689cf4aSJeff Kirsher 		if (lp->duplex == DUPLEX_FULL) {
1818e689cf4aSJeff Kirsher 			bmcr |= BMCR_FULLDPLX;
1819e689cf4aSJeff Kirsher 			fulldpx = 1;
1820e689cf4aSJeff Kirsher 		} else if (lp->duplex == DUPLEX_HALF)
1821e689cf4aSJeff Kirsher 			fulldpx = 0;
1822e689cf4aSJeff Kirsher 		else
1823e689cf4aSJeff Kirsher 			return -EINVAL;
1824e689cf4aSJeff Kirsher 
1825e689cf4aSJeff Kirsher 		if (lp->speed == SPEED_1000) {
1826e689cf4aSJeff Kirsher 			/* if X-full requested while not supported, or
1827e689cf4aSJeff Kirsher 			   X-half requested while not supported... */
1828e689cf4aSJeff Kirsher 			if ((fulldpx && !(estat & ESTATUS_1000_TFULL)) ||
1829e689cf4aSJeff Kirsher 				(!fulldpx && !(estat & ESTATUS_1000_THALF)))
1830e689cf4aSJeff Kirsher 				return -EINVAL;
1831e689cf4aSJeff Kirsher 			bmcr |= BMCR_SPEED1000;
1832e689cf4aSJeff Kirsher 		} else if (lp->speed == SPEED_100) {
1833e689cf4aSJeff Kirsher 			if ((fulldpx && !(bmsr & BMSR_100FULL)) ||
1834e689cf4aSJeff Kirsher 				(!fulldpx && !(bmsr & BMSR_100HALF)))
1835e689cf4aSJeff Kirsher 				return -EINVAL;
1836e689cf4aSJeff Kirsher 			bmcr |= BMCR_SPEED100;
1837e689cf4aSJeff Kirsher 		} else if (lp->speed == SPEED_10) {
1838e689cf4aSJeff Kirsher 			if ((fulldpx && !(bmsr & BMSR_10FULL)) ||
1839e689cf4aSJeff Kirsher 				(!fulldpx && !(bmsr & BMSR_10HALF)))
1840e689cf4aSJeff Kirsher 				return -EINVAL;
1841e689cf4aSJeff Kirsher 		} else
1842e689cf4aSJeff Kirsher 			return -EINVAL;
1843e689cf4aSJeff Kirsher 	}
1844e689cf4aSJeff Kirsher 
1845e689cf4aSJeff Kirsher 	err = mii_write(np, np->phy_addr, MII_BMCR, bmcr);
1846e689cf4aSJeff Kirsher 	if (err)
1847e689cf4aSJeff Kirsher 		return err;
1848e689cf4aSJeff Kirsher 
1849e689cf4aSJeff Kirsher #if 0
1850e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_BMCR);
1851e689cf4aSJeff Kirsher 	if (err < 0)
1852e689cf4aSJeff Kirsher 		return err;
1853e689cf4aSJeff Kirsher 	bmcr = err;
1854e689cf4aSJeff Kirsher 
1855e689cf4aSJeff Kirsher 	err = mii_read(np, np->phy_addr, MII_BMSR);
1856e689cf4aSJeff Kirsher 	if (err < 0)
1857e689cf4aSJeff Kirsher 		return err;
1858e689cf4aSJeff Kirsher 	bmsr = err;
1859e689cf4aSJeff Kirsher 
1860e689cf4aSJeff Kirsher 	pr_info("Port %u after MII init bmcr[%04x] bmsr[%04x]\n",
1861e689cf4aSJeff Kirsher 		np->port, bmcr, bmsr);
1862e689cf4aSJeff Kirsher #endif
1863e689cf4aSJeff Kirsher 
1864e689cf4aSJeff Kirsher 	return 0;
1865e689cf4aSJeff Kirsher }
1866e689cf4aSJeff Kirsher 
xcvr_init_1g(struct niu * np)1867e689cf4aSJeff Kirsher static int xcvr_init_1g(struct niu *np)
1868e689cf4aSJeff Kirsher {
1869e689cf4aSJeff Kirsher 	u64 val;
1870e689cf4aSJeff Kirsher 
1871e689cf4aSJeff Kirsher 	/* XXX shared resource, lock parent XXX */
1872e689cf4aSJeff Kirsher 	val = nr64(MIF_CONFIG);
1873e689cf4aSJeff Kirsher 	val &= ~MIF_CONFIG_INDIRECT_MODE;
1874e689cf4aSJeff Kirsher 	nw64(MIF_CONFIG, val);
1875e689cf4aSJeff Kirsher 
1876e689cf4aSJeff Kirsher 	return mii_init_common(np);
1877e689cf4aSJeff Kirsher }
1878e689cf4aSJeff Kirsher 
niu_xcvr_init(struct niu * np)1879e689cf4aSJeff Kirsher static int niu_xcvr_init(struct niu *np)
1880e689cf4aSJeff Kirsher {
1881e689cf4aSJeff Kirsher 	const struct niu_phy_ops *ops = np->phy_ops;
1882e689cf4aSJeff Kirsher 	int err;
1883e689cf4aSJeff Kirsher 
1884e689cf4aSJeff Kirsher 	err = 0;
1885e689cf4aSJeff Kirsher 	if (ops->xcvr_init)
1886e689cf4aSJeff Kirsher 		err = ops->xcvr_init(np);
1887e689cf4aSJeff Kirsher 
1888e689cf4aSJeff Kirsher 	return err;
1889e689cf4aSJeff Kirsher }
1890e689cf4aSJeff Kirsher 
niu_serdes_init(struct niu * np)1891e689cf4aSJeff Kirsher static int niu_serdes_init(struct niu *np)
1892e689cf4aSJeff Kirsher {
1893e689cf4aSJeff Kirsher 	const struct niu_phy_ops *ops = np->phy_ops;
1894e689cf4aSJeff Kirsher 	int err;
1895e689cf4aSJeff Kirsher 
1896e689cf4aSJeff Kirsher 	err = 0;
1897e689cf4aSJeff Kirsher 	if (ops->serdes_init)
1898e689cf4aSJeff Kirsher 		err = ops->serdes_init(np);
1899e689cf4aSJeff Kirsher 
1900e689cf4aSJeff Kirsher 	return err;
1901e689cf4aSJeff Kirsher }
1902e689cf4aSJeff Kirsher 
1903e689cf4aSJeff Kirsher static void niu_init_xif(struct niu *);
1904e689cf4aSJeff Kirsher static void niu_handle_led(struct niu *, int status);
1905e689cf4aSJeff Kirsher 
niu_link_status_common(struct niu * np,int link_up)1906e689cf4aSJeff Kirsher static int niu_link_status_common(struct niu *np, int link_up)
1907e689cf4aSJeff Kirsher {
1908e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
1909e689cf4aSJeff Kirsher 	struct net_device *dev = np->dev;
1910e689cf4aSJeff Kirsher 	unsigned long flags;
1911e689cf4aSJeff Kirsher 
1912e689cf4aSJeff Kirsher 	if (!netif_carrier_ok(dev) && link_up) {
1913e689cf4aSJeff Kirsher 		netif_info(np, link, dev, "Link is up at %s, %s duplex\n",
1914e689cf4aSJeff Kirsher 			   lp->active_speed == SPEED_10000 ? "10Gb/sec" :
1915e689cf4aSJeff Kirsher 			   lp->active_speed == SPEED_1000 ? "1Gb/sec" :
1916e689cf4aSJeff Kirsher 			   lp->active_speed == SPEED_100 ? "100Mbit/sec" :
1917e689cf4aSJeff Kirsher 			   "10Mbit/sec",
1918e689cf4aSJeff Kirsher 			   lp->active_duplex == DUPLEX_FULL ? "full" : "half");
1919e689cf4aSJeff Kirsher 
1920e689cf4aSJeff Kirsher 		spin_lock_irqsave(&np->lock, flags);
1921e689cf4aSJeff Kirsher 		niu_init_xif(np);
1922e689cf4aSJeff Kirsher 		niu_handle_led(np, 1);
1923e689cf4aSJeff Kirsher 		spin_unlock_irqrestore(&np->lock, flags);
1924e689cf4aSJeff Kirsher 
1925e689cf4aSJeff Kirsher 		netif_carrier_on(dev);
1926e689cf4aSJeff Kirsher 	} else if (netif_carrier_ok(dev) && !link_up) {
1927e689cf4aSJeff Kirsher 		netif_warn(np, link, dev, "Link is down\n");
1928e689cf4aSJeff Kirsher 		spin_lock_irqsave(&np->lock, flags);
1929e689cf4aSJeff Kirsher 		niu_handle_led(np, 0);
1930e689cf4aSJeff Kirsher 		spin_unlock_irqrestore(&np->lock, flags);
1931e689cf4aSJeff Kirsher 		netif_carrier_off(dev);
1932e689cf4aSJeff Kirsher 	}
1933e689cf4aSJeff Kirsher 
1934e689cf4aSJeff Kirsher 	return 0;
1935e689cf4aSJeff Kirsher }
1936e689cf4aSJeff Kirsher 
link_status_10g_mrvl(struct niu * np,int * link_up_p)1937e689cf4aSJeff Kirsher static int link_status_10g_mrvl(struct niu *np, int *link_up_p)
1938e689cf4aSJeff Kirsher {
1939e689cf4aSJeff Kirsher 	int err, link_up, pma_status, pcs_status;
1940e689cf4aSJeff Kirsher 
1941e689cf4aSJeff Kirsher 	link_up = 0;
1942e689cf4aSJeff Kirsher 
1943e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
1944e689cf4aSJeff Kirsher 			MRVL88X2011_10G_PMD_STATUS_2);
1945e689cf4aSJeff Kirsher 	if (err < 0)
1946e689cf4aSJeff Kirsher 		goto out;
1947e689cf4aSJeff Kirsher 
1948e689cf4aSJeff Kirsher 	/* Check PMA/PMD Register: 1.0001.2 == 1 */
1949e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
1950e689cf4aSJeff Kirsher 			MRVL88X2011_PMA_PMD_STATUS_1);
1951e689cf4aSJeff Kirsher 	if (err < 0)
1952e689cf4aSJeff Kirsher 		goto out;
1953e689cf4aSJeff Kirsher 
1954e689cf4aSJeff Kirsher 	pma_status = ((err & MRVL88X2011_LNK_STATUS_OK) ? 1 : 0);
1955e689cf4aSJeff Kirsher 
1956e689cf4aSJeff Kirsher         /* Check PMC Register : 3.0001.2 == 1: read twice */
1957e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV3_ADDR,
1958e689cf4aSJeff Kirsher 			MRVL88X2011_PMA_PMD_STATUS_1);
1959e689cf4aSJeff Kirsher 	if (err < 0)
1960e689cf4aSJeff Kirsher 		goto out;
1961e689cf4aSJeff Kirsher 
1962e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV3_ADDR,
1963e689cf4aSJeff Kirsher 			MRVL88X2011_PMA_PMD_STATUS_1);
1964e689cf4aSJeff Kirsher 	if (err < 0)
1965e689cf4aSJeff Kirsher 		goto out;
1966e689cf4aSJeff Kirsher 
1967e689cf4aSJeff Kirsher 	pcs_status = ((err & MRVL88X2011_LNK_STATUS_OK) ? 1 : 0);
1968e689cf4aSJeff Kirsher 
1969e689cf4aSJeff Kirsher         /* Check XGXS Register : 4.0018.[0-3,12] */
1970e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV4_ADDR,
1971e689cf4aSJeff Kirsher 			MRVL88X2011_10G_XGXS_LANE_STAT);
1972e689cf4aSJeff Kirsher 	if (err < 0)
1973e689cf4aSJeff Kirsher 		goto out;
1974e689cf4aSJeff Kirsher 
1975e689cf4aSJeff Kirsher 	if (err == (PHYXS_XGXS_LANE_STAT_ALINGED | PHYXS_XGXS_LANE_STAT_LANE3 |
1976e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE2 | PHYXS_XGXS_LANE_STAT_LANE1 |
1977e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE0 | PHYXS_XGXS_LANE_STAT_MAGIC |
1978e689cf4aSJeff Kirsher 		    0x800))
1979e689cf4aSJeff Kirsher 		link_up = (pma_status && pcs_status) ? 1 : 0;
1980e689cf4aSJeff Kirsher 
1981e689cf4aSJeff Kirsher 	np->link_config.active_speed = SPEED_10000;
1982e689cf4aSJeff Kirsher 	np->link_config.active_duplex = DUPLEX_FULL;
1983e689cf4aSJeff Kirsher 	err = 0;
1984e689cf4aSJeff Kirsher out:
1985e689cf4aSJeff Kirsher 	mrvl88x2011_act_led(np, (link_up ?
1986e689cf4aSJeff Kirsher 				 MRVL88X2011_LED_CTL_PCS_ACT :
1987e689cf4aSJeff Kirsher 				 MRVL88X2011_LED_CTL_OFF));
1988e689cf4aSJeff Kirsher 
1989e689cf4aSJeff Kirsher 	*link_up_p = link_up;
1990e689cf4aSJeff Kirsher 	return err;
1991e689cf4aSJeff Kirsher }
1992e689cf4aSJeff Kirsher 
link_status_10g_bcm8706(struct niu * np,int * link_up_p)1993e689cf4aSJeff Kirsher static int link_status_10g_bcm8706(struct niu *np, int *link_up_p)
1994e689cf4aSJeff Kirsher {
1995e689cf4aSJeff Kirsher 	int err, link_up;
1996e689cf4aSJeff Kirsher 	link_up = 0;
1997e689cf4aSJeff Kirsher 
1998e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_PMA_PMD_DEV_ADDR,
1999e689cf4aSJeff Kirsher 			BCM8704_PMD_RCV_SIGDET);
2000e689cf4aSJeff Kirsher 	if (err < 0 || err == 0xffff)
2001e689cf4aSJeff Kirsher 		goto out;
2002e689cf4aSJeff Kirsher 	if (!(err & PMD_RCV_SIGDET_GLOBAL)) {
2003e689cf4aSJeff Kirsher 		err = 0;
2004e689cf4aSJeff Kirsher 		goto out;
2005e689cf4aSJeff Kirsher 	}
2006e689cf4aSJeff Kirsher 
2007e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_PCS_DEV_ADDR,
2008e689cf4aSJeff Kirsher 			BCM8704_PCS_10G_R_STATUS);
2009e689cf4aSJeff Kirsher 	if (err < 0)
2010e689cf4aSJeff Kirsher 		goto out;
2011e689cf4aSJeff Kirsher 
2012e689cf4aSJeff Kirsher 	if (!(err & PCS_10G_R_STATUS_BLK_LOCK)) {
2013e689cf4aSJeff Kirsher 		err = 0;
2014e689cf4aSJeff Kirsher 		goto out;
2015e689cf4aSJeff Kirsher 	}
2016e689cf4aSJeff Kirsher 
2017e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_PHYXS_DEV_ADDR,
2018e689cf4aSJeff Kirsher 			BCM8704_PHYXS_XGXS_LANE_STAT);
2019e689cf4aSJeff Kirsher 	if (err < 0)
2020e689cf4aSJeff Kirsher 		goto out;
2021e689cf4aSJeff Kirsher 	if (err != (PHYXS_XGXS_LANE_STAT_ALINGED |
2022e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_MAGIC |
2023e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_PATTEST |
2024e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE3 |
2025e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE2 |
2026e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE1 |
2027e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE0)) {
2028e689cf4aSJeff Kirsher 		err = 0;
2029e689cf4aSJeff Kirsher 		np->link_config.active_speed = SPEED_INVALID;
2030e689cf4aSJeff Kirsher 		np->link_config.active_duplex = DUPLEX_INVALID;
2031e689cf4aSJeff Kirsher 		goto out;
2032e689cf4aSJeff Kirsher 	}
2033e689cf4aSJeff Kirsher 
2034e689cf4aSJeff Kirsher 	link_up = 1;
2035e689cf4aSJeff Kirsher 	np->link_config.active_speed = SPEED_10000;
2036e689cf4aSJeff Kirsher 	np->link_config.active_duplex = DUPLEX_FULL;
2037e689cf4aSJeff Kirsher 	err = 0;
2038e689cf4aSJeff Kirsher 
2039e689cf4aSJeff Kirsher out:
2040e689cf4aSJeff Kirsher 	*link_up_p = link_up;
2041e689cf4aSJeff Kirsher 	return err;
2042e689cf4aSJeff Kirsher }
2043e689cf4aSJeff Kirsher 
link_status_10g_bcom(struct niu * np,int * link_up_p)2044e689cf4aSJeff Kirsher static int link_status_10g_bcom(struct niu *np, int *link_up_p)
2045e689cf4aSJeff Kirsher {
2046e689cf4aSJeff Kirsher 	int err, link_up;
2047e689cf4aSJeff Kirsher 
2048e689cf4aSJeff Kirsher 	link_up = 0;
2049e689cf4aSJeff Kirsher 
2050e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_PMA_PMD_DEV_ADDR,
2051e689cf4aSJeff Kirsher 			BCM8704_PMD_RCV_SIGDET);
2052e689cf4aSJeff Kirsher 	if (err < 0)
2053e689cf4aSJeff Kirsher 		goto out;
2054e689cf4aSJeff Kirsher 	if (!(err & PMD_RCV_SIGDET_GLOBAL)) {
2055e689cf4aSJeff Kirsher 		err = 0;
2056e689cf4aSJeff Kirsher 		goto out;
2057e689cf4aSJeff Kirsher 	}
2058e689cf4aSJeff Kirsher 
2059e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_PCS_DEV_ADDR,
2060e689cf4aSJeff Kirsher 			BCM8704_PCS_10G_R_STATUS);
2061e689cf4aSJeff Kirsher 	if (err < 0)
2062e689cf4aSJeff Kirsher 		goto out;
2063e689cf4aSJeff Kirsher 	if (!(err & PCS_10G_R_STATUS_BLK_LOCK)) {
2064e689cf4aSJeff Kirsher 		err = 0;
2065e689cf4aSJeff Kirsher 		goto out;
2066e689cf4aSJeff Kirsher 	}
2067e689cf4aSJeff Kirsher 
2068e689cf4aSJeff Kirsher 	err = mdio_read(np, np->phy_addr, BCM8704_PHYXS_DEV_ADDR,
2069e689cf4aSJeff Kirsher 			BCM8704_PHYXS_XGXS_LANE_STAT);
2070e689cf4aSJeff Kirsher 	if (err < 0)
2071e689cf4aSJeff Kirsher 		goto out;
2072e689cf4aSJeff Kirsher 
2073e689cf4aSJeff Kirsher 	if (err != (PHYXS_XGXS_LANE_STAT_ALINGED |
2074e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_MAGIC |
2075e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE3 |
2076e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE2 |
2077e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE1 |
2078e689cf4aSJeff Kirsher 		    PHYXS_XGXS_LANE_STAT_LANE0)) {
2079e689cf4aSJeff Kirsher 		err = 0;
2080e689cf4aSJeff Kirsher 		goto out;
2081e689cf4aSJeff Kirsher 	}
2082e689cf4aSJeff Kirsher 
2083e689cf4aSJeff Kirsher 	link_up = 1;
2084e689cf4aSJeff Kirsher 	np->link_config.active_speed = SPEED_10000;
2085e689cf4aSJeff Kirsher 	np->link_config.active_duplex = DUPLEX_FULL;
2086e689cf4aSJeff Kirsher 	err = 0;
2087e689cf4aSJeff Kirsher 
2088e689cf4aSJeff Kirsher out:
2089e689cf4aSJeff Kirsher 	*link_up_p = link_up;
2090e689cf4aSJeff Kirsher 	return err;
2091e689cf4aSJeff Kirsher }
2092e689cf4aSJeff Kirsher 
link_status_10g(struct niu * np,int * link_up_p)2093e689cf4aSJeff Kirsher static int link_status_10g(struct niu *np, int *link_up_p)
2094e689cf4aSJeff Kirsher {
2095e689cf4aSJeff Kirsher 	unsigned long flags;
2096e689cf4aSJeff Kirsher 	int err = -EINVAL;
2097e689cf4aSJeff Kirsher 
2098e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
2099e689cf4aSJeff Kirsher 
2100e689cf4aSJeff Kirsher 	if (np->link_config.loopback_mode == LOOPBACK_DISABLED) {
2101e689cf4aSJeff Kirsher 		int phy_id;
2102e689cf4aSJeff Kirsher 
2103e689cf4aSJeff Kirsher 		phy_id = phy_decode(np->parent->port_phy, np->port);
2104e689cf4aSJeff Kirsher 		phy_id = np->parent->phy_probe_info.phy_id[phy_id][np->port];
2105e689cf4aSJeff Kirsher 
2106e689cf4aSJeff Kirsher 		/* handle different phy types */
2107e689cf4aSJeff Kirsher 		switch (phy_id & NIU_PHY_ID_MASK) {
2108e689cf4aSJeff Kirsher 		case NIU_PHY_ID_MRVL88X2011:
2109e689cf4aSJeff Kirsher 			err = link_status_10g_mrvl(np, link_up_p);
2110e689cf4aSJeff Kirsher 			break;
2111e689cf4aSJeff Kirsher 
2112e689cf4aSJeff Kirsher 		default: /* bcom 8704 */
2113e689cf4aSJeff Kirsher 			err = link_status_10g_bcom(np, link_up_p);
2114e689cf4aSJeff Kirsher 			break;
2115e689cf4aSJeff Kirsher 		}
2116e689cf4aSJeff Kirsher 	}
2117e689cf4aSJeff Kirsher 
2118e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
2119e689cf4aSJeff Kirsher 
2120e689cf4aSJeff Kirsher 	return err;
2121e689cf4aSJeff Kirsher }
2122e689cf4aSJeff Kirsher 
niu_10g_phy_present(struct niu * np)2123e689cf4aSJeff Kirsher static int niu_10g_phy_present(struct niu *np)
2124e689cf4aSJeff Kirsher {
2125e689cf4aSJeff Kirsher 	u64 sig, mask, val;
2126e689cf4aSJeff Kirsher 
2127e689cf4aSJeff Kirsher 	sig = nr64(ESR_INT_SIGNALS);
2128e689cf4aSJeff Kirsher 	switch (np->port) {
2129e689cf4aSJeff Kirsher 	case 0:
2130e689cf4aSJeff Kirsher 		mask = ESR_INT_SIGNALS_P0_BITS;
2131e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P0 |
2132e689cf4aSJeff Kirsher 		       ESR_INT_DET0_P0 |
2133e689cf4aSJeff Kirsher 		       ESR_INT_XSRDY_P0 |
2134e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH3 |
2135e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH2 |
2136e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH1 |
2137e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH0);
2138e689cf4aSJeff Kirsher 		break;
2139e689cf4aSJeff Kirsher 
2140e689cf4aSJeff Kirsher 	case 1:
2141e689cf4aSJeff Kirsher 		mask = ESR_INT_SIGNALS_P1_BITS;
2142e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P1 |
2143e689cf4aSJeff Kirsher 		       ESR_INT_DET0_P1 |
2144e689cf4aSJeff Kirsher 		       ESR_INT_XSRDY_P1 |
2145e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH3 |
2146e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH2 |
2147e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH1 |
2148e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH0);
2149e689cf4aSJeff Kirsher 		break;
2150e689cf4aSJeff Kirsher 
2151e689cf4aSJeff Kirsher 	default:
2152e689cf4aSJeff Kirsher 		return 0;
2153e689cf4aSJeff Kirsher 	}
2154e689cf4aSJeff Kirsher 
2155e689cf4aSJeff Kirsher 	if ((sig & mask) != val)
2156e689cf4aSJeff Kirsher 		return 0;
2157e689cf4aSJeff Kirsher 	return 1;
2158e689cf4aSJeff Kirsher }
2159e689cf4aSJeff Kirsher 
link_status_10g_hotplug(struct niu * np,int * link_up_p)2160e689cf4aSJeff Kirsher static int link_status_10g_hotplug(struct niu *np, int *link_up_p)
2161e689cf4aSJeff Kirsher {
2162e689cf4aSJeff Kirsher 	unsigned long flags;
2163e689cf4aSJeff Kirsher 	int err = 0;
2164e689cf4aSJeff Kirsher 	int phy_present;
2165e689cf4aSJeff Kirsher 	int phy_present_prev;
2166e689cf4aSJeff Kirsher 
2167e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
2168e689cf4aSJeff Kirsher 
2169e689cf4aSJeff Kirsher 	if (np->link_config.loopback_mode == LOOPBACK_DISABLED) {
2170e689cf4aSJeff Kirsher 		phy_present_prev = (np->flags & NIU_FLAGS_HOTPLUG_PHY_PRESENT) ?
2171e689cf4aSJeff Kirsher 			1 : 0;
2172e689cf4aSJeff Kirsher 		phy_present = niu_10g_phy_present(np);
2173e689cf4aSJeff Kirsher 		if (phy_present != phy_present_prev) {
2174e689cf4aSJeff Kirsher 			/* state change */
2175e689cf4aSJeff Kirsher 			if (phy_present) {
2176e689cf4aSJeff Kirsher 				/* A NEM was just plugged in */
2177e689cf4aSJeff Kirsher 				np->flags |= NIU_FLAGS_HOTPLUG_PHY_PRESENT;
2178e689cf4aSJeff Kirsher 				if (np->phy_ops->xcvr_init)
2179e689cf4aSJeff Kirsher 					err = np->phy_ops->xcvr_init(np);
2180e689cf4aSJeff Kirsher 				if (err) {
2181e689cf4aSJeff Kirsher 					err = mdio_read(np, np->phy_addr,
2182e689cf4aSJeff Kirsher 						BCM8704_PHYXS_DEV_ADDR, MII_BMCR);
2183e689cf4aSJeff Kirsher 					if (err == 0xffff) {
2184e689cf4aSJeff Kirsher 						/* No mdio, back-to-back XAUI */
2185e689cf4aSJeff Kirsher 						goto out;
2186e689cf4aSJeff Kirsher 					}
2187e689cf4aSJeff Kirsher 					/* debounce */
2188e689cf4aSJeff Kirsher 					np->flags &= ~NIU_FLAGS_HOTPLUG_PHY_PRESENT;
2189e689cf4aSJeff Kirsher 				}
2190e689cf4aSJeff Kirsher 			} else {
2191e689cf4aSJeff Kirsher 				np->flags &= ~NIU_FLAGS_HOTPLUG_PHY_PRESENT;
2192e689cf4aSJeff Kirsher 				*link_up_p = 0;
2193e689cf4aSJeff Kirsher 				netif_warn(np, link, np->dev,
2194e689cf4aSJeff Kirsher 					   "Hotplug PHY Removed\n");
2195e689cf4aSJeff Kirsher 			}
2196e689cf4aSJeff Kirsher 		}
2197e689cf4aSJeff Kirsher out:
2198e689cf4aSJeff Kirsher 		if (np->flags & NIU_FLAGS_HOTPLUG_PHY_PRESENT) {
2199e689cf4aSJeff Kirsher 			err = link_status_10g_bcm8706(np, link_up_p);
2200e689cf4aSJeff Kirsher 			if (err == 0xffff) {
2201e689cf4aSJeff Kirsher 				/* No mdio, back-to-back XAUI: it is C10NEM */
2202e689cf4aSJeff Kirsher 				*link_up_p = 1;
2203e689cf4aSJeff Kirsher 				np->link_config.active_speed = SPEED_10000;
2204e689cf4aSJeff Kirsher 				np->link_config.active_duplex = DUPLEX_FULL;
2205e689cf4aSJeff Kirsher 			}
2206e689cf4aSJeff Kirsher 		}
2207e689cf4aSJeff Kirsher 	}
2208e689cf4aSJeff Kirsher 
2209e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
2210e689cf4aSJeff Kirsher 
2211e689cf4aSJeff Kirsher 	return 0;
2212e689cf4aSJeff Kirsher }
2213e689cf4aSJeff Kirsher 
niu_link_status(struct niu * np,int * link_up_p)2214e689cf4aSJeff Kirsher static int niu_link_status(struct niu *np, int *link_up_p)
2215e689cf4aSJeff Kirsher {
2216e689cf4aSJeff Kirsher 	const struct niu_phy_ops *ops = np->phy_ops;
2217e689cf4aSJeff Kirsher 	int err;
2218e689cf4aSJeff Kirsher 
2219e689cf4aSJeff Kirsher 	err = 0;
2220e689cf4aSJeff Kirsher 	if (ops->link_status)
2221e689cf4aSJeff Kirsher 		err = ops->link_status(np, link_up_p);
2222e689cf4aSJeff Kirsher 
2223e689cf4aSJeff Kirsher 	return err;
2224e689cf4aSJeff Kirsher }
2225e689cf4aSJeff Kirsher 
niu_timer(struct timer_list * t)22260822c5d9SKees Cook static void niu_timer(struct timer_list *t)
2227e689cf4aSJeff Kirsher {
22280822c5d9SKees Cook 	struct niu *np = from_timer(np, t, timer);
2229e689cf4aSJeff Kirsher 	unsigned long off;
2230e689cf4aSJeff Kirsher 	int err, link_up;
2231e689cf4aSJeff Kirsher 
2232e689cf4aSJeff Kirsher 	err = niu_link_status(np, &link_up);
2233e689cf4aSJeff Kirsher 	if (!err)
2234e689cf4aSJeff Kirsher 		niu_link_status_common(np, link_up);
2235e689cf4aSJeff Kirsher 
2236e689cf4aSJeff Kirsher 	if (netif_carrier_ok(np->dev))
2237e689cf4aSJeff Kirsher 		off = 5 * HZ;
2238e689cf4aSJeff Kirsher 	else
2239e689cf4aSJeff Kirsher 		off = 1 * HZ;
2240e689cf4aSJeff Kirsher 	np->timer.expires = jiffies + off;
2241e689cf4aSJeff Kirsher 
2242e689cf4aSJeff Kirsher 	add_timer(&np->timer);
2243e689cf4aSJeff Kirsher }
2244e689cf4aSJeff Kirsher 
2245e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_10g_serdes = {
2246e689cf4aSJeff Kirsher 	.serdes_init		= serdes_init_10g_serdes,
2247e689cf4aSJeff Kirsher 	.link_status		= link_status_10g_serdes,
2248e689cf4aSJeff Kirsher };
2249e689cf4aSJeff Kirsher 
2250e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_10g_serdes_niu = {
2251e689cf4aSJeff Kirsher 	.serdes_init		= serdes_init_niu_10g_serdes,
2252e689cf4aSJeff Kirsher 	.link_status		= link_status_10g_serdes,
2253e689cf4aSJeff Kirsher };
2254e689cf4aSJeff Kirsher 
2255e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_1g_serdes_niu = {
2256e689cf4aSJeff Kirsher 	.serdes_init		= serdes_init_niu_1g_serdes,
2257e689cf4aSJeff Kirsher 	.link_status		= link_status_1g_serdes,
2258e689cf4aSJeff Kirsher };
2259e689cf4aSJeff Kirsher 
2260e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_1g_rgmii = {
2261e689cf4aSJeff Kirsher 	.xcvr_init		= xcvr_init_1g_rgmii,
2262e689cf4aSJeff Kirsher 	.link_status		= link_status_1g_rgmii,
2263e689cf4aSJeff Kirsher };
2264e689cf4aSJeff Kirsher 
2265e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_10g_fiber_niu = {
2266e689cf4aSJeff Kirsher 	.serdes_init		= serdes_init_niu_10g_fiber,
2267e689cf4aSJeff Kirsher 	.xcvr_init		= xcvr_init_10g,
2268e689cf4aSJeff Kirsher 	.link_status		= link_status_10g,
2269e689cf4aSJeff Kirsher };
2270e689cf4aSJeff Kirsher 
2271e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_10g_fiber = {
2272e689cf4aSJeff Kirsher 	.serdes_init		= serdes_init_10g,
2273e689cf4aSJeff Kirsher 	.xcvr_init		= xcvr_init_10g,
2274e689cf4aSJeff Kirsher 	.link_status		= link_status_10g,
2275e689cf4aSJeff Kirsher };
2276e689cf4aSJeff Kirsher 
2277e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_10g_fiber_hotplug = {
2278e689cf4aSJeff Kirsher 	.serdes_init		= serdes_init_10g,
2279e689cf4aSJeff Kirsher 	.xcvr_init		= xcvr_init_10g_bcm8706,
2280e689cf4aSJeff Kirsher 	.link_status		= link_status_10g_hotplug,
2281e689cf4aSJeff Kirsher };
2282e689cf4aSJeff Kirsher 
2283e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_niu_10g_hotplug = {
2284e689cf4aSJeff Kirsher 	.serdes_init		= serdes_init_niu_10g_fiber,
2285e689cf4aSJeff Kirsher 	.xcvr_init		= xcvr_init_10g_bcm8706,
2286e689cf4aSJeff Kirsher 	.link_status		= link_status_10g_hotplug,
2287e689cf4aSJeff Kirsher };
2288e689cf4aSJeff Kirsher 
2289e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_10g_copper = {
2290e689cf4aSJeff Kirsher 	.serdes_init		= serdes_init_10g,
2291e689cf4aSJeff Kirsher 	.link_status		= link_status_10g, /* XXX */
2292e689cf4aSJeff Kirsher };
2293e689cf4aSJeff Kirsher 
2294e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_1g_fiber = {
2295e689cf4aSJeff Kirsher 	.serdes_init		= serdes_init_1g,
2296e689cf4aSJeff Kirsher 	.xcvr_init		= xcvr_init_1g,
2297e689cf4aSJeff Kirsher 	.link_status		= link_status_1g,
2298e689cf4aSJeff Kirsher };
2299e689cf4aSJeff Kirsher 
2300e689cf4aSJeff Kirsher static const struct niu_phy_ops phy_ops_1g_copper = {
2301e689cf4aSJeff Kirsher 	.xcvr_init		= xcvr_init_1g,
2302e689cf4aSJeff Kirsher 	.link_status		= link_status_1g,
2303e689cf4aSJeff Kirsher };
2304e689cf4aSJeff Kirsher 
2305e689cf4aSJeff Kirsher struct niu_phy_template {
2306e689cf4aSJeff Kirsher 	const struct niu_phy_ops	*ops;
2307e689cf4aSJeff Kirsher 	u32				phy_addr_base;
2308e689cf4aSJeff Kirsher };
2309e689cf4aSJeff Kirsher 
2310e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_niu_10g_fiber = {
2311e689cf4aSJeff Kirsher 	.ops		= &phy_ops_10g_fiber_niu,
2312e689cf4aSJeff Kirsher 	.phy_addr_base	= 16,
2313e689cf4aSJeff Kirsher };
2314e689cf4aSJeff Kirsher 
2315e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_niu_10g_serdes = {
2316e689cf4aSJeff Kirsher 	.ops		= &phy_ops_10g_serdes_niu,
2317e689cf4aSJeff Kirsher 	.phy_addr_base	= 0,
2318e689cf4aSJeff Kirsher };
2319e689cf4aSJeff Kirsher 
2320e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_niu_1g_serdes = {
2321e689cf4aSJeff Kirsher 	.ops		= &phy_ops_1g_serdes_niu,
2322e689cf4aSJeff Kirsher 	.phy_addr_base	= 0,
2323e689cf4aSJeff Kirsher };
2324e689cf4aSJeff Kirsher 
2325e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_10g_fiber = {
2326e689cf4aSJeff Kirsher 	.ops		= &phy_ops_10g_fiber,
2327e689cf4aSJeff Kirsher 	.phy_addr_base	= 8,
2328e689cf4aSJeff Kirsher };
2329e689cf4aSJeff Kirsher 
2330e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_10g_fiber_hotplug = {
2331e689cf4aSJeff Kirsher 	.ops		= &phy_ops_10g_fiber_hotplug,
2332e689cf4aSJeff Kirsher 	.phy_addr_base	= 8,
2333e689cf4aSJeff Kirsher };
2334e689cf4aSJeff Kirsher 
2335e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_niu_10g_hotplug = {
2336e689cf4aSJeff Kirsher 	.ops		= &phy_ops_niu_10g_hotplug,
2337e689cf4aSJeff Kirsher 	.phy_addr_base	= 8,
2338e689cf4aSJeff Kirsher };
2339e689cf4aSJeff Kirsher 
2340e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_10g_copper = {
2341e689cf4aSJeff Kirsher 	.ops		= &phy_ops_10g_copper,
2342e689cf4aSJeff Kirsher 	.phy_addr_base	= 10,
2343e689cf4aSJeff Kirsher };
2344e689cf4aSJeff Kirsher 
2345e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_1g_fiber = {
2346e689cf4aSJeff Kirsher 	.ops		= &phy_ops_1g_fiber,
2347e689cf4aSJeff Kirsher 	.phy_addr_base	= 0,
2348e689cf4aSJeff Kirsher };
2349e689cf4aSJeff Kirsher 
2350e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_1g_copper = {
2351e689cf4aSJeff Kirsher 	.ops		= &phy_ops_1g_copper,
2352e689cf4aSJeff Kirsher 	.phy_addr_base	= 0,
2353e689cf4aSJeff Kirsher };
2354e689cf4aSJeff Kirsher 
2355e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_1g_rgmii = {
2356e689cf4aSJeff Kirsher 	.ops		= &phy_ops_1g_rgmii,
2357e689cf4aSJeff Kirsher 	.phy_addr_base	= 0,
2358e689cf4aSJeff Kirsher };
2359e689cf4aSJeff Kirsher 
2360e689cf4aSJeff Kirsher static const struct niu_phy_template phy_template_10g_serdes = {
2361e689cf4aSJeff Kirsher 	.ops		= &phy_ops_10g_serdes,
2362e689cf4aSJeff Kirsher 	.phy_addr_base	= 0,
2363e689cf4aSJeff Kirsher };
2364e689cf4aSJeff Kirsher 
2365e689cf4aSJeff Kirsher static int niu_atca_port_num[4] = {
2366e689cf4aSJeff Kirsher 	0, 0,  11, 10
2367e689cf4aSJeff Kirsher };
2368e689cf4aSJeff Kirsher 
serdes_init_10g_serdes(struct niu * np)2369e689cf4aSJeff Kirsher static int serdes_init_10g_serdes(struct niu *np)
2370e689cf4aSJeff Kirsher {
2371e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
2372e689cf4aSJeff Kirsher 	unsigned long ctrl_reg, test_cfg_reg, pll_cfg, i;
2373e689cf4aSJeff Kirsher 	u64 ctrl_val, test_cfg_val, sig, mask, val;
2374e689cf4aSJeff Kirsher 
2375e689cf4aSJeff Kirsher 	switch (np->port) {
2376e689cf4aSJeff Kirsher 	case 0:
2377e689cf4aSJeff Kirsher 		ctrl_reg = ENET_SERDES_0_CTRL_CFG;
2378e689cf4aSJeff Kirsher 		test_cfg_reg = ENET_SERDES_0_TEST_CFG;
2379e689cf4aSJeff Kirsher 		pll_cfg = ENET_SERDES_0_PLL_CFG;
2380e689cf4aSJeff Kirsher 		break;
2381e689cf4aSJeff Kirsher 	case 1:
2382e689cf4aSJeff Kirsher 		ctrl_reg = ENET_SERDES_1_CTRL_CFG;
2383e689cf4aSJeff Kirsher 		test_cfg_reg = ENET_SERDES_1_TEST_CFG;
2384e689cf4aSJeff Kirsher 		pll_cfg = ENET_SERDES_1_PLL_CFG;
2385e689cf4aSJeff Kirsher 		break;
2386e689cf4aSJeff Kirsher 
2387e689cf4aSJeff Kirsher 	default:
2388e689cf4aSJeff Kirsher 		return -EINVAL;
2389e689cf4aSJeff Kirsher 	}
2390e689cf4aSJeff Kirsher 	ctrl_val = (ENET_SERDES_CTRL_SDET_0 |
2391e689cf4aSJeff Kirsher 		    ENET_SERDES_CTRL_SDET_1 |
2392e689cf4aSJeff Kirsher 		    ENET_SERDES_CTRL_SDET_2 |
2393e689cf4aSJeff Kirsher 		    ENET_SERDES_CTRL_SDET_3 |
2394e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_0_SHIFT) |
2395e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_1_SHIFT) |
2396e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_2_SHIFT) |
2397e689cf4aSJeff Kirsher 		    (0x5 << ENET_SERDES_CTRL_EMPH_3_SHIFT) |
2398e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_0_SHIFT) |
2399e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_1_SHIFT) |
2400e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_2_SHIFT) |
2401e689cf4aSJeff Kirsher 		    (0x1 << ENET_SERDES_CTRL_LADJ_3_SHIFT));
2402e689cf4aSJeff Kirsher 	test_cfg_val = 0;
2403e689cf4aSJeff Kirsher 
2404e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_PHY) {
2405e689cf4aSJeff Kirsher 		test_cfg_val |= ((ENET_TEST_MD_PAD_LOOPBACK <<
2406e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_0_SHIFT) |
2407e689cf4aSJeff Kirsher 				 (ENET_TEST_MD_PAD_LOOPBACK <<
2408e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_1_SHIFT) |
2409e689cf4aSJeff Kirsher 				 (ENET_TEST_MD_PAD_LOOPBACK <<
2410e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_2_SHIFT) |
2411e689cf4aSJeff Kirsher 				 (ENET_TEST_MD_PAD_LOOPBACK <<
2412e689cf4aSJeff Kirsher 				  ENET_SERDES_TEST_MD_3_SHIFT));
2413e689cf4aSJeff Kirsher 	}
2414e689cf4aSJeff Kirsher 
2415e689cf4aSJeff Kirsher 	esr_reset(np);
2416e689cf4aSJeff Kirsher 	nw64(pll_cfg, ENET_SERDES_PLL_FBDIV2);
2417e689cf4aSJeff Kirsher 	nw64(ctrl_reg, ctrl_val);
2418e689cf4aSJeff Kirsher 	nw64(test_cfg_reg, test_cfg_val);
2419e689cf4aSJeff Kirsher 
2420e689cf4aSJeff Kirsher 	/* Initialize all 4 lanes of the SERDES.  */
2421e689cf4aSJeff Kirsher 	for (i = 0; i < 4; i++) {
2422e689cf4aSJeff Kirsher 		u32 rxtx_ctrl, glue0;
2423e689cf4aSJeff Kirsher 		int err;
2424e689cf4aSJeff Kirsher 
2425e689cf4aSJeff Kirsher 		err = esr_read_rxtx_ctrl(np, i, &rxtx_ctrl);
2426e689cf4aSJeff Kirsher 		if (err)
2427e689cf4aSJeff Kirsher 			return err;
2428e689cf4aSJeff Kirsher 		err = esr_read_glue0(np, i, &glue0);
2429e689cf4aSJeff Kirsher 		if (err)
2430e689cf4aSJeff Kirsher 			return err;
2431e689cf4aSJeff Kirsher 
2432e689cf4aSJeff Kirsher 		rxtx_ctrl &= ~(ESR_RXTX_CTRL_VMUXLO);
2433e689cf4aSJeff Kirsher 		rxtx_ctrl |= (ESR_RXTX_CTRL_ENSTRETCH |
2434e689cf4aSJeff Kirsher 			      (2 << ESR_RXTX_CTRL_VMUXLO_SHIFT));
2435e689cf4aSJeff Kirsher 
2436e689cf4aSJeff Kirsher 		glue0 &= ~(ESR_GLUE_CTRL0_SRATE |
2437e689cf4aSJeff Kirsher 			   ESR_GLUE_CTRL0_THCNT |
2438e689cf4aSJeff Kirsher 			   ESR_GLUE_CTRL0_BLTIME);
2439e689cf4aSJeff Kirsher 		glue0 |= (ESR_GLUE_CTRL0_RXLOSENAB |
2440e689cf4aSJeff Kirsher 			  (0xf << ESR_GLUE_CTRL0_SRATE_SHIFT) |
2441e689cf4aSJeff Kirsher 			  (0xff << ESR_GLUE_CTRL0_THCNT_SHIFT) |
2442e689cf4aSJeff Kirsher 			  (BLTIME_300_CYCLES <<
2443e689cf4aSJeff Kirsher 			   ESR_GLUE_CTRL0_BLTIME_SHIFT));
2444e689cf4aSJeff Kirsher 
2445e689cf4aSJeff Kirsher 		err = esr_write_rxtx_ctrl(np, i, rxtx_ctrl);
2446e689cf4aSJeff Kirsher 		if (err)
2447e689cf4aSJeff Kirsher 			return err;
2448e689cf4aSJeff Kirsher 		err = esr_write_glue0(np, i, glue0);
2449e689cf4aSJeff Kirsher 		if (err)
2450e689cf4aSJeff Kirsher 			return err;
2451e689cf4aSJeff Kirsher 	}
2452e689cf4aSJeff Kirsher 
2453e689cf4aSJeff Kirsher 
2454e689cf4aSJeff Kirsher 	sig = nr64(ESR_INT_SIGNALS);
2455e689cf4aSJeff Kirsher 	switch (np->port) {
2456e689cf4aSJeff Kirsher 	case 0:
2457e689cf4aSJeff Kirsher 		mask = ESR_INT_SIGNALS_P0_BITS;
2458e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P0 |
2459e689cf4aSJeff Kirsher 		       ESR_INT_DET0_P0 |
2460e689cf4aSJeff Kirsher 		       ESR_INT_XSRDY_P0 |
2461e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH3 |
2462e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH2 |
2463e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH1 |
2464e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P0_CH0);
2465e689cf4aSJeff Kirsher 		break;
2466e689cf4aSJeff Kirsher 
2467e689cf4aSJeff Kirsher 	case 1:
2468e689cf4aSJeff Kirsher 		mask = ESR_INT_SIGNALS_P1_BITS;
2469e689cf4aSJeff Kirsher 		val = (ESR_INT_SRDY0_P1 |
2470e689cf4aSJeff Kirsher 		       ESR_INT_DET0_P1 |
2471e689cf4aSJeff Kirsher 		       ESR_INT_XSRDY_P1 |
2472e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH3 |
2473e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH2 |
2474e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH1 |
2475e689cf4aSJeff Kirsher 		       ESR_INT_XDP_P1_CH0);
2476e689cf4aSJeff Kirsher 		break;
2477e689cf4aSJeff Kirsher 
2478e689cf4aSJeff Kirsher 	default:
2479e689cf4aSJeff Kirsher 		return -EINVAL;
2480e689cf4aSJeff Kirsher 	}
2481e689cf4aSJeff Kirsher 
2482e689cf4aSJeff Kirsher 	if ((sig & mask) != val) {
2483e689cf4aSJeff Kirsher 		int err;
2484e689cf4aSJeff Kirsher 		err = serdes_init_1g_serdes(np);
2485e689cf4aSJeff Kirsher 		if (!err) {
2486e689cf4aSJeff Kirsher 			np->flags &= ~NIU_FLAGS_10G;
2487e689cf4aSJeff Kirsher 			np->mac_xcvr = MAC_XCVR_PCS;
2488e689cf4aSJeff Kirsher 		}  else {
2489e689cf4aSJeff Kirsher 			netdev_err(np->dev, "Port %u 10G/1G SERDES Link Failed\n",
2490e689cf4aSJeff Kirsher 				   np->port);
2491e689cf4aSJeff Kirsher 			return -ENODEV;
2492e689cf4aSJeff Kirsher 		}
2493e689cf4aSJeff Kirsher 	}
2494e689cf4aSJeff Kirsher 
2495e689cf4aSJeff Kirsher 	return 0;
2496e689cf4aSJeff Kirsher }
2497e689cf4aSJeff Kirsher 
niu_determine_phy_disposition(struct niu * np)2498e689cf4aSJeff Kirsher static int niu_determine_phy_disposition(struct niu *np)
2499e689cf4aSJeff Kirsher {
2500e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
2501e689cf4aSJeff Kirsher 	u8 plat_type = parent->plat_type;
2502e689cf4aSJeff Kirsher 	const struct niu_phy_template *tp;
2503e689cf4aSJeff Kirsher 	u32 phy_addr_off = 0;
2504e689cf4aSJeff Kirsher 
2505e689cf4aSJeff Kirsher 	if (plat_type == PLAT_TYPE_NIU) {
2506e689cf4aSJeff Kirsher 		switch (np->flags &
2507e689cf4aSJeff Kirsher 			(NIU_FLAGS_10G |
2508e689cf4aSJeff Kirsher 			 NIU_FLAGS_FIBER |
2509e689cf4aSJeff Kirsher 			 NIU_FLAGS_XCVR_SERDES)) {
2510e689cf4aSJeff Kirsher 		case NIU_FLAGS_10G | NIU_FLAGS_XCVR_SERDES:
2511e689cf4aSJeff Kirsher 			/* 10G Serdes */
2512e689cf4aSJeff Kirsher 			tp = &phy_template_niu_10g_serdes;
2513e689cf4aSJeff Kirsher 			break;
2514e689cf4aSJeff Kirsher 		case NIU_FLAGS_XCVR_SERDES:
2515e689cf4aSJeff Kirsher 			/* 1G Serdes */
2516e689cf4aSJeff Kirsher 			tp = &phy_template_niu_1g_serdes;
2517e689cf4aSJeff Kirsher 			break;
2518e689cf4aSJeff Kirsher 		case NIU_FLAGS_10G | NIU_FLAGS_FIBER:
2519e689cf4aSJeff Kirsher 			/* 10G Fiber */
2520e689cf4aSJeff Kirsher 		default:
2521e689cf4aSJeff Kirsher 			if (np->flags & NIU_FLAGS_HOTPLUG_PHY) {
2522e689cf4aSJeff Kirsher 				tp = &phy_template_niu_10g_hotplug;
2523e689cf4aSJeff Kirsher 				if (np->port == 0)
2524e689cf4aSJeff Kirsher 					phy_addr_off = 8;
2525e689cf4aSJeff Kirsher 				if (np->port == 1)
2526e689cf4aSJeff Kirsher 					phy_addr_off = 12;
2527e689cf4aSJeff Kirsher 			} else {
2528e689cf4aSJeff Kirsher 				tp = &phy_template_niu_10g_fiber;
2529e689cf4aSJeff Kirsher 				phy_addr_off += np->port;
2530e689cf4aSJeff Kirsher 			}
2531e689cf4aSJeff Kirsher 			break;
2532e689cf4aSJeff Kirsher 		}
2533e689cf4aSJeff Kirsher 	} else {
2534e689cf4aSJeff Kirsher 		switch (np->flags &
2535e689cf4aSJeff Kirsher 			(NIU_FLAGS_10G |
2536e689cf4aSJeff Kirsher 			 NIU_FLAGS_FIBER |
2537e689cf4aSJeff Kirsher 			 NIU_FLAGS_XCVR_SERDES)) {
2538e689cf4aSJeff Kirsher 		case 0:
2539e689cf4aSJeff Kirsher 			/* 1G copper */
2540e689cf4aSJeff Kirsher 			tp = &phy_template_1g_copper;
2541e689cf4aSJeff Kirsher 			if (plat_type == PLAT_TYPE_VF_P0)
2542e689cf4aSJeff Kirsher 				phy_addr_off = 10;
2543e689cf4aSJeff Kirsher 			else if (plat_type == PLAT_TYPE_VF_P1)
2544e689cf4aSJeff Kirsher 				phy_addr_off = 26;
2545e689cf4aSJeff Kirsher 
2546e689cf4aSJeff Kirsher 			phy_addr_off += (np->port ^ 0x3);
2547e689cf4aSJeff Kirsher 			break;
2548e689cf4aSJeff Kirsher 
2549e689cf4aSJeff Kirsher 		case NIU_FLAGS_10G:
2550e689cf4aSJeff Kirsher 			/* 10G copper */
2551e689cf4aSJeff Kirsher 			tp = &phy_template_10g_copper;
2552e689cf4aSJeff Kirsher 			break;
2553e689cf4aSJeff Kirsher 
2554e689cf4aSJeff Kirsher 		case NIU_FLAGS_FIBER:
2555e689cf4aSJeff Kirsher 			/* 1G fiber */
2556e689cf4aSJeff Kirsher 			tp = &phy_template_1g_fiber;
2557e689cf4aSJeff Kirsher 			break;
2558e689cf4aSJeff Kirsher 
2559e689cf4aSJeff Kirsher 		case NIU_FLAGS_10G | NIU_FLAGS_FIBER:
2560e689cf4aSJeff Kirsher 			/* 10G fiber */
2561e689cf4aSJeff Kirsher 			tp = &phy_template_10g_fiber;
2562e689cf4aSJeff Kirsher 			if (plat_type == PLAT_TYPE_VF_P0 ||
2563e689cf4aSJeff Kirsher 			    plat_type == PLAT_TYPE_VF_P1)
2564e689cf4aSJeff Kirsher 				phy_addr_off = 8;
2565e689cf4aSJeff Kirsher 			phy_addr_off += np->port;
2566e689cf4aSJeff Kirsher 			if (np->flags & NIU_FLAGS_HOTPLUG_PHY) {
2567e689cf4aSJeff Kirsher 				tp = &phy_template_10g_fiber_hotplug;
2568e689cf4aSJeff Kirsher 				if (np->port == 0)
2569e689cf4aSJeff Kirsher 					phy_addr_off = 8;
2570e689cf4aSJeff Kirsher 				if (np->port == 1)
2571e689cf4aSJeff Kirsher 					phy_addr_off = 12;
2572e689cf4aSJeff Kirsher 			}
2573e689cf4aSJeff Kirsher 			break;
2574e689cf4aSJeff Kirsher 
2575e689cf4aSJeff Kirsher 		case NIU_FLAGS_10G | NIU_FLAGS_XCVR_SERDES:
2576e689cf4aSJeff Kirsher 		case NIU_FLAGS_XCVR_SERDES | NIU_FLAGS_FIBER:
2577e689cf4aSJeff Kirsher 		case NIU_FLAGS_XCVR_SERDES:
2578e689cf4aSJeff Kirsher 			switch(np->port) {
2579e689cf4aSJeff Kirsher 			case 0:
2580e689cf4aSJeff Kirsher 			case 1:
2581e689cf4aSJeff Kirsher 				tp = &phy_template_10g_serdes;
2582e689cf4aSJeff Kirsher 				break;
2583e689cf4aSJeff Kirsher 			case 2:
2584e689cf4aSJeff Kirsher 			case 3:
2585e689cf4aSJeff Kirsher 				tp = &phy_template_1g_rgmii;
2586e689cf4aSJeff Kirsher 				break;
2587e689cf4aSJeff Kirsher 			default:
2588e689cf4aSJeff Kirsher 				return -EINVAL;
2589e689cf4aSJeff Kirsher 			}
2590e689cf4aSJeff Kirsher 			phy_addr_off = niu_atca_port_num[np->port];
2591e689cf4aSJeff Kirsher 			break;
2592e689cf4aSJeff Kirsher 
2593e689cf4aSJeff Kirsher 		default:
2594e689cf4aSJeff Kirsher 			return -EINVAL;
2595e689cf4aSJeff Kirsher 		}
2596e689cf4aSJeff Kirsher 	}
2597e689cf4aSJeff Kirsher 
2598e689cf4aSJeff Kirsher 	np->phy_ops = tp->ops;
2599e689cf4aSJeff Kirsher 	np->phy_addr = tp->phy_addr_base + phy_addr_off;
2600e689cf4aSJeff Kirsher 
2601e689cf4aSJeff Kirsher 	return 0;
2602e689cf4aSJeff Kirsher }
2603e689cf4aSJeff Kirsher 
niu_init_link(struct niu * np)2604e689cf4aSJeff Kirsher static int niu_init_link(struct niu *np)
2605e689cf4aSJeff Kirsher {
2606e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
2607e689cf4aSJeff Kirsher 	int err, ignore;
2608e689cf4aSJeff Kirsher 
2609e689cf4aSJeff Kirsher 	if (parent->plat_type == PLAT_TYPE_NIU) {
2610e689cf4aSJeff Kirsher 		err = niu_xcvr_init(np);
2611e689cf4aSJeff Kirsher 		if (err)
2612e689cf4aSJeff Kirsher 			return err;
2613e689cf4aSJeff Kirsher 		msleep(200);
2614e689cf4aSJeff Kirsher 	}
2615e689cf4aSJeff Kirsher 	err = niu_serdes_init(np);
2616e689cf4aSJeff Kirsher 	if (err && !(np->flags & NIU_FLAGS_HOTPLUG_PHY))
2617e689cf4aSJeff Kirsher 		return err;
2618e689cf4aSJeff Kirsher 	msleep(200);
2619e689cf4aSJeff Kirsher 	err = niu_xcvr_init(np);
2620e689cf4aSJeff Kirsher 	if (!err || (np->flags & NIU_FLAGS_HOTPLUG_PHY))
2621e689cf4aSJeff Kirsher 		niu_link_status(np, &ignore);
2622e689cf4aSJeff Kirsher 	return 0;
2623e689cf4aSJeff Kirsher }
2624e689cf4aSJeff Kirsher 
niu_set_primary_mac(struct niu * np,const unsigned char * addr)2625a7639279SJakub Kicinski static void niu_set_primary_mac(struct niu *np, const unsigned char *addr)
2626e689cf4aSJeff Kirsher {
2627e689cf4aSJeff Kirsher 	u16 reg0 = addr[4] << 8 | addr[5];
2628e689cf4aSJeff Kirsher 	u16 reg1 = addr[2] << 8 | addr[3];
2629e689cf4aSJeff Kirsher 	u16 reg2 = addr[0] << 8 | addr[1];
2630e689cf4aSJeff Kirsher 
2631e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC) {
2632e689cf4aSJeff Kirsher 		nw64_mac(XMAC_ADDR0, reg0);
2633e689cf4aSJeff Kirsher 		nw64_mac(XMAC_ADDR1, reg1);
2634e689cf4aSJeff Kirsher 		nw64_mac(XMAC_ADDR2, reg2);
2635e689cf4aSJeff Kirsher 	} else {
2636e689cf4aSJeff Kirsher 		nw64_mac(BMAC_ADDR0, reg0);
2637e689cf4aSJeff Kirsher 		nw64_mac(BMAC_ADDR1, reg1);
2638e689cf4aSJeff Kirsher 		nw64_mac(BMAC_ADDR2, reg2);
2639e689cf4aSJeff Kirsher 	}
2640e689cf4aSJeff Kirsher }
2641e689cf4aSJeff Kirsher 
niu_num_alt_addr(struct niu * np)2642e689cf4aSJeff Kirsher static int niu_num_alt_addr(struct niu *np)
2643e689cf4aSJeff Kirsher {
2644e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
2645e689cf4aSJeff Kirsher 		return XMAC_NUM_ALT_ADDR;
2646e689cf4aSJeff Kirsher 	else
2647e689cf4aSJeff Kirsher 		return BMAC_NUM_ALT_ADDR;
2648e689cf4aSJeff Kirsher }
2649e689cf4aSJeff Kirsher 
niu_set_alt_mac(struct niu * np,int index,unsigned char * addr)2650e689cf4aSJeff Kirsher static int niu_set_alt_mac(struct niu *np, int index, unsigned char *addr)
2651e689cf4aSJeff Kirsher {
2652e689cf4aSJeff Kirsher 	u16 reg0 = addr[4] << 8 | addr[5];
2653e689cf4aSJeff Kirsher 	u16 reg1 = addr[2] << 8 | addr[3];
2654e689cf4aSJeff Kirsher 	u16 reg2 = addr[0] << 8 | addr[1];
2655e689cf4aSJeff Kirsher 
2656e689cf4aSJeff Kirsher 	if (index >= niu_num_alt_addr(np))
2657e689cf4aSJeff Kirsher 		return -EINVAL;
2658e689cf4aSJeff Kirsher 
2659e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC) {
2660e689cf4aSJeff Kirsher 		nw64_mac(XMAC_ALT_ADDR0(index), reg0);
2661e689cf4aSJeff Kirsher 		nw64_mac(XMAC_ALT_ADDR1(index), reg1);
2662e689cf4aSJeff Kirsher 		nw64_mac(XMAC_ALT_ADDR2(index), reg2);
2663e689cf4aSJeff Kirsher 	} else {
2664e689cf4aSJeff Kirsher 		nw64_mac(BMAC_ALT_ADDR0(index), reg0);
2665e689cf4aSJeff Kirsher 		nw64_mac(BMAC_ALT_ADDR1(index), reg1);
2666e689cf4aSJeff Kirsher 		nw64_mac(BMAC_ALT_ADDR2(index), reg2);
2667e689cf4aSJeff Kirsher 	}
2668e689cf4aSJeff Kirsher 
2669e689cf4aSJeff Kirsher 	return 0;
2670e689cf4aSJeff Kirsher }
2671e689cf4aSJeff Kirsher 
niu_enable_alt_mac(struct niu * np,int index,int on)2672e689cf4aSJeff Kirsher static int niu_enable_alt_mac(struct niu *np, int index, int on)
2673e689cf4aSJeff Kirsher {
2674e689cf4aSJeff Kirsher 	unsigned long reg;
2675e689cf4aSJeff Kirsher 	u64 val, mask;
2676e689cf4aSJeff Kirsher 
2677e689cf4aSJeff Kirsher 	if (index >= niu_num_alt_addr(np))
2678e689cf4aSJeff Kirsher 		return -EINVAL;
2679e689cf4aSJeff Kirsher 
2680e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC) {
2681e689cf4aSJeff Kirsher 		reg = XMAC_ADDR_CMPEN;
2682e689cf4aSJeff Kirsher 		mask = 1 << index;
2683e689cf4aSJeff Kirsher 	} else {
2684e689cf4aSJeff Kirsher 		reg = BMAC_ADDR_CMPEN;
2685e689cf4aSJeff Kirsher 		mask = 1 << (index + 1);
2686e689cf4aSJeff Kirsher 	}
2687e689cf4aSJeff Kirsher 
2688e689cf4aSJeff Kirsher 	val = nr64_mac(reg);
2689e689cf4aSJeff Kirsher 	if (on)
2690e689cf4aSJeff Kirsher 		val |= mask;
2691e689cf4aSJeff Kirsher 	else
2692e689cf4aSJeff Kirsher 		val &= ~mask;
2693e689cf4aSJeff Kirsher 	nw64_mac(reg, val);
2694e689cf4aSJeff Kirsher 
2695e689cf4aSJeff Kirsher 	return 0;
2696e689cf4aSJeff Kirsher }
2697e689cf4aSJeff Kirsher 
__set_rdc_table_num_hw(struct niu * np,unsigned long reg,int num,int mac_pref)2698e689cf4aSJeff Kirsher static void __set_rdc_table_num_hw(struct niu *np, unsigned long reg,
2699e689cf4aSJeff Kirsher 				   int num, int mac_pref)
2700e689cf4aSJeff Kirsher {
2701e689cf4aSJeff Kirsher 	u64 val = nr64_mac(reg);
2702e689cf4aSJeff Kirsher 	val &= ~(HOST_INFO_MACRDCTBLN | HOST_INFO_MPR);
2703e689cf4aSJeff Kirsher 	val |= num;
2704e689cf4aSJeff Kirsher 	if (mac_pref)
2705e689cf4aSJeff Kirsher 		val |= HOST_INFO_MPR;
2706e689cf4aSJeff Kirsher 	nw64_mac(reg, val);
2707e689cf4aSJeff Kirsher }
2708e689cf4aSJeff Kirsher 
__set_rdc_table_num(struct niu * np,int xmac_index,int bmac_index,int rdc_table_num,int mac_pref)2709e689cf4aSJeff Kirsher static int __set_rdc_table_num(struct niu *np,
2710e689cf4aSJeff Kirsher 			       int xmac_index, int bmac_index,
2711e689cf4aSJeff Kirsher 			       int rdc_table_num, int mac_pref)
2712e689cf4aSJeff Kirsher {
2713e689cf4aSJeff Kirsher 	unsigned long reg;
2714e689cf4aSJeff Kirsher 
2715e689cf4aSJeff Kirsher 	if (rdc_table_num & ~HOST_INFO_MACRDCTBLN)
2716e689cf4aSJeff Kirsher 		return -EINVAL;
2717e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
2718e689cf4aSJeff Kirsher 		reg = XMAC_HOST_INFO(xmac_index);
2719e689cf4aSJeff Kirsher 	else
2720e689cf4aSJeff Kirsher 		reg = BMAC_HOST_INFO(bmac_index);
2721e689cf4aSJeff Kirsher 	__set_rdc_table_num_hw(np, reg, rdc_table_num, mac_pref);
2722e689cf4aSJeff Kirsher 	return 0;
2723e689cf4aSJeff Kirsher }
2724e689cf4aSJeff Kirsher 
niu_set_primary_mac_rdc_table(struct niu * np,int table_num,int mac_pref)2725e689cf4aSJeff Kirsher static int niu_set_primary_mac_rdc_table(struct niu *np, int table_num,
2726e689cf4aSJeff Kirsher 					 int mac_pref)
2727e689cf4aSJeff Kirsher {
2728e689cf4aSJeff Kirsher 	return __set_rdc_table_num(np, 17, 0, table_num, mac_pref);
2729e689cf4aSJeff Kirsher }
2730e689cf4aSJeff Kirsher 
niu_set_multicast_mac_rdc_table(struct niu * np,int table_num,int mac_pref)2731e689cf4aSJeff Kirsher static int niu_set_multicast_mac_rdc_table(struct niu *np, int table_num,
2732e689cf4aSJeff Kirsher 					   int mac_pref)
2733e689cf4aSJeff Kirsher {
2734e689cf4aSJeff Kirsher 	return __set_rdc_table_num(np, 16, 8, table_num, mac_pref);
2735e689cf4aSJeff Kirsher }
2736e689cf4aSJeff Kirsher 
niu_set_alt_mac_rdc_table(struct niu * np,int idx,int table_num,int mac_pref)2737e689cf4aSJeff Kirsher static int niu_set_alt_mac_rdc_table(struct niu *np, int idx,
2738e689cf4aSJeff Kirsher 				     int table_num, int mac_pref)
2739e689cf4aSJeff Kirsher {
2740e689cf4aSJeff Kirsher 	if (idx >= niu_num_alt_addr(np))
2741e689cf4aSJeff Kirsher 		return -EINVAL;
2742e689cf4aSJeff Kirsher 	return __set_rdc_table_num(np, idx, idx + 1, table_num, mac_pref);
2743e689cf4aSJeff Kirsher }
2744e689cf4aSJeff Kirsher 
vlan_entry_set_parity(u64 reg_val)2745e689cf4aSJeff Kirsher static u64 vlan_entry_set_parity(u64 reg_val)
2746e689cf4aSJeff Kirsher {
2747e689cf4aSJeff Kirsher 	u64 port01_mask;
2748e689cf4aSJeff Kirsher 	u64 port23_mask;
2749e689cf4aSJeff Kirsher 
2750e689cf4aSJeff Kirsher 	port01_mask = 0x00ff;
2751e689cf4aSJeff Kirsher 	port23_mask = 0xff00;
2752e689cf4aSJeff Kirsher 
2753e689cf4aSJeff Kirsher 	if (hweight64(reg_val & port01_mask) & 1)
2754e689cf4aSJeff Kirsher 		reg_val |= ENET_VLAN_TBL_PARITY0;
2755e689cf4aSJeff Kirsher 	else
2756e689cf4aSJeff Kirsher 		reg_val &= ~ENET_VLAN_TBL_PARITY0;
2757e689cf4aSJeff Kirsher 
2758e689cf4aSJeff Kirsher 	if (hweight64(reg_val & port23_mask) & 1)
2759e689cf4aSJeff Kirsher 		reg_val |= ENET_VLAN_TBL_PARITY1;
2760e689cf4aSJeff Kirsher 	else
2761e689cf4aSJeff Kirsher 		reg_val &= ~ENET_VLAN_TBL_PARITY1;
2762e689cf4aSJeff Kirsher 
2763e689cf4aSJeff Kirsher 	return reg_val;
2764e689cf4aSJeff Kirsher }
2765e689cf4aSJeff Kirsher 
vlan_tbl_write(struct niu * np,unsigned long index,int port,int vpr,int rdc_table)2766e689cf4aSJeff Kirsher static void vlan_tbl_write(struct niu *np, unsigned long index,
2767e689cf4aSJeff Kirsher 			   int port, int vpr, int rdc_table)
2768e689cf4aSJeff Kirsher {
2769e689cf4aSJeff Kirsher 	u64 reg_val = nr64(ENET_VLAN_TBL(index));
2770e689cf4aSJeff Kirsher 
2771e689cf4aSJeff Kirsher 	reg_val &= ~((ENET_VLAN_TBL_VPR |
2772e689cf4aSJeff Kirsher 		      ENET_VLAN_TBL_VLANRDCTBLN) <<
2773e689cf4aSJeff Kirsher 		     ENET_VLAN_TBL_SHIFT(port));
2774e689cf4aSJeff Kirsher 	if (vpr)
2775e689cf4aSJeff Kirsher 		reg_val |= (ENET_VLAN_TBL_VPR <<
2776e689cf4aSJeff Kirsher 			    ENET_VLAN_TBL_SHIFT(port));
2777e689cf4aSJeff Kirsher 	reg_val |= (rdc_table << ENET_VLAN_TBL_SHIFT(port));
2778e689cf4aSJeff Kirsher 
2779e689cf4aSJeff Kirsher 	reg_val = vlan_entry_set_parity(reg_val);
2780e689cf4aSJeff Kirsher 
2781e689cf4aSJeff Kirsher 	nw64(ENET_VLAN_TBL(index), reg_val);
2782e689cf4aSJeff Kirsher }
2783e689cf4aSJeff Kirsher 
vlan_tbl_clear(struct niu * np)2784e689cf4aSJeff Kirsher static void vlan_tbl_clear(struct niu *np)
2785e689cf4aSJeff Kirsher {
2786e689cf4aSJeff Kirsher 	int i;
2787e689cf4aSJeff Kirsher 
2788e689cf4aSJeff Kirsher 	for (i = 0; i < ENET_VLAN_TBL_NUM_ENTRIES; i++)
2789e689cf4aSJeff Kirsher 		nw64(ENET_VLAN_TBL(i), 0);
2790e689cf4aSJeff Kirsher }
2791e689cf4aSJeff Kirsher 
tcam_wait_bit(struct niu * np,u64 bit)2792e689cf4aSJeff Kirsher static int tcam_wait_bit(struct niu *np, u64 bit)
2793e689cf4aSJeff Kirsher {
2794e689cf4aSJeff Kirsher 	int limit = 1000;
2795e689cf4aSJeff Kirsher 
2796e689cf4aSJeff Kirsher 	while (--limit > 0) {
2797e689cf4aSJeff Kirsher 		if (nr64(TCAM_CTL) & bit)
2798e689cf4aSJeff Kirsher 			break;
2799e689cf4aSJeff Kirsher 		udelay(1);
2800e689cf4aSJeff Kirsher 	}
2801e689cf4aSJeff Kirsher 	if (limit <= 0)
2802e689cf4aSJeff Kirsher 		return -ENODEV;
2803e689cf4aSJeff Kirsher 
2804e689cf4aSJeff Kirsher 	return 0;
2805e689cf4aSJeff Kirsher }
2806e689cf4aSJeff Kirsher 
tcam_flush(struct niu * np,int index)2807e689cf4aSJeff Kirsher static int tcam_flush(struct niu *np, int index)
2808e689cf4aSJeff Kirsher {
2809e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_0, 0x00);
2810e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_MASK_0, 0xff);
2811e689cf4aSJeff Kirsher 	nw64(TCAM_CTL, (TCAM_CTL_RWC_TCAM_WRITE | index));
2812e689cf4aSJeff Kirsher 
2813e689cf4aSJeff Kirsher 	return tcam_wait_bit(np, TCAM_CTL_STAT);
2814e689cf4aSJeff Kirsher }
2815e689cf4aSJeff Kirsher 
2816e689cf4aSJeff Kirsher #if 0
2817e689cf4aSJeff Kirsher static int tcam_read(struct niu *np, int index,
2818e689cf4aSJeff Kirsher 		     u64 *key, u64 *mask)
2819e689cf4aSJeff Kirsher {
2820e689cf4aSJeff Kirsher 	int err;
2821e689cf4aSJeff Kirsher 
2822e689cf4aSJeff Kirsher 	nw64(TCAM_CTL, (TCAM_CTL_RWC_TCAM_READ | index));
2823e689cf4aSJeff Kirsher 	err = tcam_wait_bit(np, TCAM_CTL_STAT);
2824e689cf4aSJeff Kirsher 	if (!err) {
2825e689cf4aSJeff Kirsher 		key[0] = nr64(TCAM_KEY_0);
2826e689cf4aSJeff Kirsher 		key[1] = nr64(TCAM_KEY_1);
2827e689cf4aSJeff Kirsher 		key[2] = nr64(TCAM_KEY_2);
2828e689cf4aSJeff Kirsher 		key[3] = nr64(TCAM_KEY_3);
2829e689cf4aSJeff Kirsher 		mask[0] = nr64(TCAM_KEY_MASK_0);
2830e689cf4aSJeff Kirsher 		mask[1] = nr64(TCAM_KEY_MASK_1);
2831e689cf4aSJeff Kirsher 		mask[2] = nr64(TCAM_KEY_MASK_2);
2832e689cf4aSJeff Kirsher 		mask[3] = nr64(TCAM_KEY_MASK_3);
2833e689cf4aSJeff Kirsher 	}
2834e689cf4aSJeff Kirsher 	return err;
2835e689cf4aSJeff Kirsher }
2836e689cf4aSJeff Kirsher #endif
2837e689cf4aSJeff Kirsher 
tcam_write(struct niu * np,int index,u64 * key,u64 * mask)2838e689cf4aSJeff Kirsher static int tcam_write(struct niu *np, int index,
2839e689cf4aSJeff Kirsher 		      u64 *key, u64 *mask)
2840e689cf4aSJeff Kirsher {
2841e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_0, key[0]);
2842e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_1, key[1]);
2843e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_2, key[2]);
2844e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_3, key[3]);
2845e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_MASK_0, mask[0]);
2846e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_MASK_1, mask[1]);
2847e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_MASK_2, mask[2]);
2848e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_MASK_3, mask[3]);
2849e689cf4aSJeff Kirsher 	nw64(TCAM_CTL, (TCAM_CTL_RWC_TCAM_WRITE | index));
2850e689cf4aSJeff Kirsher 
2851e689cf4aSJeff Kirsher 	return tcam_wait_bit(np, TCAM_CTL_STAT);
2852e689cf4aSJeff Kirsher }
2853e689cf4aSJeff Kirsher 
2854e689cf4aSJeff Kirsher #if 0
2855e689cf4aSJeff Kirsher static int tcam_assoc_read(struct niu *np, int index, u64 *data)
2856e689cf4aSJeff Kirsher {
2857e689cf4aSJeff Kirsher 	int err;
2858e689cf4aSJeff Kirsher 
2859e689cf4aSJeff Kirsher 	nw64(TCAM_CTL, (TCAM_CTL_RWC_RAM_READ | index));
2860e689cf4aSJeff Kirsher 	err = tcam_wait_bit(np, TCAM_CTL_STAT);
2861e689cf4aSJeff Kirsher 	if (!err)
2862e689cf4aSJeff Kirsher 		*data = nr64(TCAM_KEY_1);
2863e689cf4aSJeff Kirsher 
2864e689cf4aSJeff Kirsher 	return err;
2865e689cf4aSJeff Kirsher }
2866e689cf4aSJeff Kirsher #endif
2867e689cf4aSJeff Kirsher 
tcam_assoc_write(struct niu * np,int index,u64 assoc_data)2868e689cf4aSJeff Kirsher static int tcam_assoc_write(struct niu *np, int index, u64 assoc_data)
2869e689cf4aSJeff Kirsher {
2870e689cf4aSJeff Kirsher 	nw64(TCAM_KEY_1, assoc_data);
2871e689cf4aSJeff Kirsher 	nw64(TCAM_CTL, (TCAM_CTL_RWC_RAM_WRITE | index));
2872e689cf4aSJeff Kirsher 
2873e689cf4aSJeff Kirsher 	return tcam_wait_bit(np, TCAM_CTL_STAT);
2874e689cf4aSJeff Kirsher }
2875e689cf4aSJeff Kirsher 
tcam_enable(struct niu * np,int on)2876e689cf4aSJeff Kirsher static void tcam_enable(struct niu *np, int on)
2877e689cf4aSJeff Kirsher {
2878e689cf4aSJeff Kirsher 	u64 val = nr64(FFLP_CFG_1);
2879e689cf4aSJeff Kirsher 
2880e689cf4aSJeff Kirsher 	if (on)
2881e689cf4aSJeff Kirsher 		val &= ~FFLP_CFG_1_TCAM_DIS;
2882e689cf4aSJeff Kirsher 	else
2883e689cf4aSJeff Kirsher 		val |= FFLP_CFG_1_TCAM_DIS;
2884e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, val);
2885e689cf4aSJeff Kirsher }
2886e689cf4aSJeff Kirsher 
tcam_set_lat_and_ratio(struct niu * np,u64 latency,u64 ratio)2887e689cf4aSJeff Kirsher static void tcam_set_lat_and_ratio(struct niu *np, u64 latency, u64 ratio)
2888e689cf4aSJeff Kirsher {
2889e689cf4aSJeff Kirsher 	u64 val = nr64(FFLP_CFG_1);
2890e689cf4aSJeff Kirsher 
2891e689cf4aSJeff Kirsher 	val &= ~(FFLP_CFG_1_FFLPINITDONE |
2892e689cf4aSJeff Kirsher 		 FFLP_CFG_1_CAMLAT |
2893e689cf4aSJeff Kirsher 		 FFLP_CFG_1_CAMRATIO);
2894e689cf4aSJeff Kirsher 	val |= (latency << FFLP_CFG_1_CAMLAT_SHIFT);
2895e689cf4aSJeff Kirsher 	val |= (ratio << FFLP_CFG_1_CAMRATIO_SHIFT);
2896e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, val);
2897e689cf4aSJeff Kirsher 
2898e689cf4aSJeff Kirsher 	val = nr64(FFLP_CFG_1);
2899e689cf4aSJeff Kirsher 	val |= FFLP_CFG_1_FFLPINITDONE;
2900e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, val);
2901e689cf4aSJeff Kirsher }
2902e689cf4aSJeff Kirsher 
tcam_user_eth_class_enable(struct niu * np,unsigned long class,int on)2903e689cf4aSJeff Kirsher static int tcam_user_eth_class_enable(struct niu *np, unsigned long class,
2904e689cf4aSJeff Kirsher 				      int on)
2905e689cf4aSJeff Kirsher {
2906e689cf4aSJeff Kirsher 	unsigned long reg;
2907e689cf4aSJeff Kirsher 	u64 val;
2908e689cf4aSJeff Kirsher 
2909e689cf4aSJeff Kirsher 	if (class < CLASS_CODE_ETHERTYPE1 ||
2910e689cf4aSJeff Kirsher 	    class > CLASS_CODE_ETHERTYPE2)
2911e689cf4aSJeff Kirsher 		return -EINVAL;
2912e689cf4aSJeff Kirsher 
2913e689cf4aSJeff Kirsher 	reg = L2_CLS(class - CLASS_CODE_ETHERTYPE1);
2914e689cf4aSJeff Kirsher 	val = nr64(reg);
2915e689cf4aSJeff Kirsher 	if (on)
2916e689cf4aSJeff Kirsher 		val |= L2_CLS_VLD;
2917e689cf4aSJeff Kirsher 	else
2918e689cf4aSJeff Kirsher 		val &= ~L2_CLS_VLD;
2919e689cf4aSJeff Kirsher 	nw64(reg, val);
2920e689cf4aSJeff Kirsher 
2921e689cf4aSJeff Kirsher 	return 0;
2922e689cf4aSJeff Kirsher }
2923e689cf4aSJeff Kirsher 
2924e689cf4aSJeff Kirsher #if 0
2925e689cf4aSJeff Kirsher static int tcam_user_eth_class_set(struct niu *np, unsigned long class,
2926e689cf4aSJeff Kirsher 				   u64 ether_type)
2927e689cf4aSJeff Kirsher {
2928e689cf4aSJeff Kirsher 	unsigned long reg;
2929e689cf4aSJeff Kirsher 	u64 val;
2930e689cf4aSJeff Kirsher 
2931e689cf4aSJeff Kirsher 	if (class < CLASS_CODE_ETHERTYPE1 ||
2932e689cf4aSJeff Kirsher 	    class > CLASS_CODE_ETHERTYPE2 ||
2933e689cf4aSJeff Kirsher 	    (ether_type & ~(u64)0xffff) != 0)
2934e689cf4aSJeff Kirsher 		return -EINVAL;
2935e689cf4aSJeff Kirsher 
2936e689cf4aSJeff Kirsher 	reg = L2_CLS(class - CLASS_CODE_ETHERTYPE1);
2937e689cf4aSJeff Kirsher 	val = nr64(reg);
2938e689cf4aSJeff Kirsher 	val &= ~L2_CLS_ETYPE;
2939e689cf4aSJeff Kirsher 	val |= (ether_type << L2_CLS_ETYPE_SHIFT);
2940e689cf4aSJeff Kirsher 	nw64(reg, val);
2941e689cf4aSJeff Kirsher 
2942e689cf4aSJeff Kirsher 	return 0;
2943e689cf4aSJeff Kirsher }
2944e689cf4aSJeff Kirsher #endif
2945e689cf4aSJeff Kirsher 
tcam_user_ip_class_enable(struct niu * np,unsigned long class,int on)2946e689cf4aSJeff Kirsher static int tcam_user_ip_class_enable(struct niu *np, unsigned long class,
2947e689cf4aSJeff Kirsher 				     int on)
2948e689cf4aSJeff Kirsher {
2949e689cf4aSJeff Kirsher 	unsigned long reg;
2950e689cf4aSJeff Kirsher 	u64 val;
2951e689cf4aSJeff Kirsher 
2952e689cf4aSJeff Kirsher 	if (class < CLASS_CODE_USER_PROG1 ||
2953e689cf4aSJeff Kirsher 	    class > CLASS_CODE_USER_PROG4)
2954e689cf4aSJeff Kirsher 		return -EINVAL;
2955e689cf4aSJeff Kirsher 
2956e689cf4aSJeff Kirsher 	reg = L3_CLS(class - CLASS_CODE_USER_PROG1);
2957e689cf4aSJeff Kirsher 	val = nr64(reg);
2958e689cf4aSJeff Kirsher 	if (on)
2959e689cf4aSJeff Kirsher 		val |= L3_CLS_VALID;
2960e689cf4aSJeff Kirsher 	else
2961e689cf4aSJeff Kirsher 		val &= ~L3_CLS_VALID;
2962e689cf4aSJeff Kirsher 	nw64(reg, val);
2963e689cf4aSJeff Kirsher 
2964e689cf4aSJeff Kirsher 	return 0;
2965e689cf4aSJeff Kirsher }
2966e689cf4aSJeff Kirsher 
tcam_user_ip_class_set(struct niu * np,unsigned long class,int ipv6,u64 protocol_id,u64 tos_mask,u64 tos_val)2967e689cf4aSJeff Kirsher static int tcam_user_ip_class_set(struct niu *np, unsigned long class,
2968e689cf4aSJeff Kirsher 				  int ipv6, u64 protocol_id,
2969e689cf4aSJeff Kirsher 				  u64 tos_mask, u64 tos_val)
2970e689cf4aSJeff Kirsher {
2971e689cf4aSJeff Kirsher 	unsigned long reg;
2972e689cf4aSJeff Kirsher 	u64 val;
2973e689cf4aSJeff Kirsher 
2974e689cf4aSJeff Kirsher 	if (class < CLASS_CODE_USER_PROG1 ||
2975e689cf4aSJeff Kirsher 	    class > CLASS_CODE_USER_PROG4 ||
2976e689cf4aSJeff Kirsher 	    (protocol_id & ~(u64)0xff) != 0 ||
2977e689cf4aSJeff Kirsher 	    (tos_mask & ~(u64)0xff) != 0 ||
2978e689cf4aSJeff Kirsher 	    (tos_val & ~(u64)0xff) != 0)
2979e689cf4aSJeff Kirsher 		return -EINVAL;
2980e689cf4aSJeff Kirsher 
2981e689cf4aSJeff Kirsher 	reg = L3_CLS(class - CLASS_CODE_USER_PROG1);
2982e689cf4aSJeff Kirsher 	val = nr64(reg);
2983e689cf4aSJeff Kirsher 	val &= ~(L3_CLS_IPVER | L3_CLS_PID |
2984e689cf4aSJeff Kirsher 		 L3_CLS_TOSMASK | L3_CLS_TOS);
2985e689cf4aSJeff Kirsher 	if (ipv6)
2986e689cf4aSJeff Kirsher 		val |= L3_CLS_IPVER;
2987e689cf4aSJeff Kirsher 	val |= (protocol_id << L3_CLS_PID_SHIFT);
2988e689cf4aSJeff Kirsher 	val |= (tos_mask << L3_CLS_TOSMASK_SHIFT);
2989e689cf4aSJeff Kirsher 	val |= (tos_val << L3_CLS_TOS_SHIFT);
2990e689cf4aSJeff Kirsher 	nw64(reg, val);
2991e689cf4aSJeff Kirsher 
2992e689cf4aSJeff Kirsher 	return 0;
2993e689cf4aSJeff Kirsher }
2994e689cf4aSJeff Kirsher 
tcam_early_init(struct niu * np)2995e689cf4aSJeff Kirsher static int tcam_early_init(struct niu *np)
2996e689cf4aSJeff Kirsher {
2997e689cf4aSJeff Kirsher 	unsigned long i;
2998e689cf4aSJeff Kirsher 	int err;
2999e689cf4aSJeff Kirsher 
3000e689cf4aSJeff Kirsher 	tcam_enable(np, 0);
3001e689cf4aSJeff Kirsher 	tcam_set_lat_and_ratio(np,
3002e689cf4aSJeff Kirsher 			       DEFAULT_TCAM_LATENCY,
3003e689cf4aSJeff Kirsher 			       DEFAULT_TCAM_ACCESS_RATIO);
3004e689cf4aSJeff Kirsher 	for (i = CLASS_CODE_ETHERTYPE1; i <= CLASS_CODE_ETHERTYPE2; i++) {
3005e689cf4aSJeff Kirsher 		err = tcam_user_eth_class_enable(np, i, 0);
3006e689cf4aSJeff Kirsher 		if (err)
3007e689cf4aSJeff Kirsher 			return err;
3008e689cf4aSJeff Kirsher 	}
3009e689cf4aSJeff Kirsher 	for (i = CLASS_CODE_USER_PROG1; i <= CLASS_CODE_USER_PROG4; i++) {
3010e689cf4aSJeff Kirsher 		err = tcam_user_ip_class_enable(np, i, 0);
3011e689cf4aSJeff Kirsher 		if (err)
3012e689cf4aSJeff Kirsher 			return err;
3013e689cf4aSJeff Kirsher 	}
3014e689cf4aSJeff Kirsher 
3015e689cf4aSJeff Kirsher 	return 0;
3016e689cf4aSJeff Kirsher }
3017e689cf4aSJeff Kirsher 
tcam_flush_all(struct niu * np)3018e689cf4aSJeff Kirsher static int tcam_flush_all(struct niu *np)
3019e689cf4aSJeff Kirsher {
3020e689cf4aSJeff Kirsher 	unsigned long i;
3021e689cf4aSJeff Kirsher 
3022e689cf4aSJeff Kirsher 	for (i = 0; i < np->parent->tcam_num_entries; i++) {
3023e689cf4aSJeff Kirsher 		int err = tcam_flush(np, i);
3024e689cf4aSJeff Kirsher 		if (err)
3025e689cf4aSJeff Kirsher 			return err;
3026e689cf4aSJeff Kirsher 	}
3027e689cf4aSJeff Kirsher 	return 0;
3028e689cf4aSJeff Kirsher }
3029e689cf4aSJeff Kirsher 
hash_addr_regval(unsigned long index,unsigned long num_entries)3030e689cf4aSJeff Kirsher static u64 hash_addr_regval(unsigned long index, unsigned long num_entries)
3031e689cf4aSJeff Kirsher {
3032e689cf4aSJeff Kirsher 	return (u64)index | (num_entries == 1 ? HASH_TBL_ADDR_AUTOINC : 0);
3033e689cf4aSJeff Kirsher }
3034e689cf4aSJeff Kirsher 
3035e689cf4aSJeff Kirsher #if 0
3036e689cf4aSJeff Kirsher static int hash_read(struct niu *np, unsigned long partition,
3037e689cf4aSJeff Kirsher 		     unsigned long index, unsigned long num_entries,
3038e689cf4aSJeff Kirsher 		     u64 *data)
3039e689cf4aSJeff Kirsher {
3040e689cf4aSJeff Kirsher 	u64 val = hash_addr_regval(index, num_entries);
3041e689cf4aSJeff Kirsher 	unsigned long i;
3042e689cf4aSJeff Kirsher 
3043e689cf4aSJeff Kirsher 	if (partition >= FCRAM_NUM_PARTITIONS ||
3044e689cf4aSJeff Kirsher 	    index + num_entries > FCRAM_SIZE)
3045e689cf4aSJeff Kirsher 		return -EINVAL;
3046e689cf4aSJeff Kirsher 
3047e689cf4aSJeff Kirsher 	nw64(HASH_TBL_ADDR(partition), val);
3048e689cf4aSJeff Kirsher 	for (i = 0; i < num_entries; i++)
3049e689cf4aSJeff Kirsher 		data[i] = nr64(HASH_TBL_DATA(partition));
3050e689cf4aSJeff Kirsher 
3051e689cf4aSJeff Kirsher 	return 0;
3052e689cf4aSJeff Kirsher }
3053e689cf4aSJeff Kirsher #endif
3054e689cf4aSJeff Kirsher 
hash_write(struct niu * np,unsigned long partition,unsigned long index,unsigned long num_entries,u64 * data)3055e689cf4aSJeff Kirsher static int hash_write(struct niu *np, unsigned long partition,
3056e689cf4aSJeff Kirsher 		      unsigned long index, unsigned long num_entries,
3057e689cf4aSJeff Kirsher 		      u64 *data)
3058e689cf4aSJeff Kirsher {
3059e689cf4aSJeff Kirsher 	u64 val = hash_addr_regval(index, num_entries);
3060e689cf4aSJeff Kirsher 	unsigned long i;
3061e689cf4aSJeff Kirsher 
3062e689cf4aSJeff Kirsher 	if (partition >= FCRAM_NUM_PARTITIONS ||
3063e689cf4aSJeff Kirsher 	    index + (num_entries * 8) > FCRAM_SIZE)
3064e689cf4aSJeff Kirsher 		return -EINVAL;
3065e689cf4aSJeff Kirsher 
3066e689cf4aSJeff Kirsher 	nw64(HASH_TBL_ADDR(partition), val);
3067e689cf4aSJeff Kirsher 	for (i = 0; i < num_entries; i++)
3068e689cf4aSJeff Kirsher 		nw64(HASH_TBL_DATA(partition), data[i]);
3069e689cf4aSJeff Kirsher 
3070e689cf4aSJeff Kirsher 	return 0;
3071e689cf4aSJeff Kirsher }
3072e689cf4aSJeff Kirsher 
fflp_reset(struct niu * np)3073e689cf4aSJeff Kirsher static void fflp_reset(struct niu *np)
3074e689cf4aSJeff Kirsher {
3075e689cf4aSJeff Kirsher 	u64 val;
3076e689cf4aSJeff Kirsher 
3077e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, FFLP_CFG_1_PIO_FIO_RST);
3078e689cf4aSJeff Kirsher 	udelay(10);
3079e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, 0);
3080e689cf4aSJeff Kirsher 
3081e689cf4aSJeff Kirsher 	val = FFLP_CFG_1_FCRAMOUTDR_NORMAL | FFLP_CFG_1_FFLPINITDONE;
3082e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, val);
3083e689cf4aSJeff Kirsher }
3084e689cf4aSJeff Kirsher 
fflp_set_timings(struct niu * np)3085e689cf4aSJeff Kirsher static void fflp_set_timings(struct niu *np)
3086e689cf4aSJeff Kirsher {
3087e689cf4aSJeff Kirsher 	u64 val = nr64(FFLP_CFG_1);
3088e689cf4aSJeff Kirsher 
3089e689cf4aSJeff Kirsher 	val &= ~FFLP_CFG_1_FFLPINITDONE;
3090e689cf4aSJeff Kirsher 	val |= (DEFAULT_FCRAMRATIO << FFLP_CFG_1_FCRAMRATIO_SHIFT);
3091e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, val);
3092e689cf4aSJeff Kirsher 
3093e689cf4aSJeff Kirsher 	val = nr64(FFLP_CFG_1);
3094e689cf4aSJeff Kirsher 	val |= FFLP_CFG_1_FFLPINITDONE;
3095e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, val);
3096e689cf4aSJeff Kirsher 
3097e689cf4aSJeff Kirsher 	val = nr64(FCRAM_REF_TMR);
3098e689cf4aSJeff Kirsher 	val &= ~(FCRAM_REF_TMR_MAX | FCRAM_REF_TMR_MIN);
3099e689cf4aSJeff Kirsher 	val |= (DEFAULT_FCRAM_REFRESH_MAX << FCRAM_REF_TMR_MAX_SHIFT);
3100e689cf4aSJeff Kirsher 	val |= (DEFAULT_FCRAM_REFRESH_MIN << FCRAM_REF_TMR_MIN_SHIFT);
3101e689cf4aSJeff Kirsher 	nw64(FCRAM_REF_TMR, val);
3102e689cf4aSJeff Kirsher }
3103e689cf4aSJeff Kirsher 
fflp_set_partition(struct niu * np,u64 partition,u64 mask,u64 base,int enable)3104e689cf4aSJeff Kirsher static int fflp_set_partition(struct niu *np, u64 partition,
3105e689cf4aSJeff Kirsher 			      u64 mask, u64 base, int enable)
3106e689cf4aSJeff Kirsher {
3107e689cf4aSJeff Kirsher 	unsigned long reg;
3108e689cf4aSJeff Kirsher 	u64 val;
3109e689cf4aSJeff Kirsher 
3110e689cf4aSJeff Kirsher 	if (partition >= FCRAM_NUM_PARTITIONS ||
3111e689cf4aSJeff Kirsher 	    (mask & ~(u64)0x1f) != 0 ||
3112e689cf4aSJeff Kirsher 	    (base & ~(u64)0x1f) != 0)
3113e689cf4aSJeff Kirsher 		return -EINVAL;
3114e689cf4aSJeff Kirsher 
3115e689cf4aSJeff Kirsher 	reg = FLW_PRT_SEL(partition);
3116e689cf4aSJeff Kirsher 
3117e689cf4aSJeff Kirsher 	val = nr64(reg);
3118e689cf4aSJeff Kirsher 	val &= ~(FLW_PRT_SEL_EXT | FLW_PRT_SEL_MASK | FLW_PRT_SEL_BASE);
3119e689cf4aSJeff Kirsher 	val |= (mask << FLW_PRT_SEL_MASK_SHIFT);
3120e689cf4aSJeff Kirsher 	val |= (base << FLW_PRT_SEL_BASE_SHIFT);
3121e689cf4aSJeff Kirsher 	if (enable)
3122e689cf4aSJeff Kirsher 		val |= FLW_PRT_SEL_EXT;
3123e689cf4aSJeff Kirsher 	nw64(reg, val);
3124e689cf4aSJeff Kirsher 
3125e689cf4aSJeff Kirsher 	return 0;
3126e689cf4aSJeff Kirsher }
3127e689cf4aSJeff Kirsher 
fflp_disable_all_partitions(struct niu * np)3128e689cf4aSJeff Kirsher static int fflp_disable_all_partitions(struct niu *np)
3129e689cf4aSJeff Kirsher {
3130e689cf4aSJeff Kirsher 	unsigned long i;
3131e689cf4aSJeff Kirsher 
3132e689cf4aSJeff Kirsher 	for (i = 0; i < FCRAM_NUM_PARTITIONS; i++) {
3133e689cf4aSJeff Kirsher 		int err = fflp_set_partition(np, 0, 0, 0, 0);
3134e689cf4aSJeff Kirsher 		if (err)
3135e689cf4aSJeff Kirsher 			return err;
3136e689cf4aSJeff Kirsher 	}
3137e689cf4aSJeff Kirsher 	return 0;
3138e689cf4aSJeff Kirsher }
3139e689cf4aSJeff Kirsher 
fflp_llcsnap_enable(struct niu * np,int on)3140e689cf4aSJeff Kirsher static void fflp_llcsnap_enable(struct niu *np, int on)
3141e689cf4aSJeff Kirsher {
3142e689cf4aSJeff Kirsher 	u64 val = nr64(FFLP_CFG_1);
3143e689cf4aSJeff Kirsher 
3144e689cf4aSJeff Kirsher 	if (on)
3145e689cf4aSJeff Kirsher 		val |= FFLP_CFG_1_LLCSNAP;
3146e689cf4aSJeff Kirsher 	else
3147e689cf4aSJeff Kirsher 		val &= ~FFLP_CFG_1_LLCSNAP;
3148e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, val);
3149e689cf4aSJeff Kirsher }
3150e689cf4aSJeff Kirsher 
fflp_errors_enable(struct niu * np,int on)3151e689cf4aSJeff Kirsher static void fflp_errors_enable(struct niu *np, int on)
3152e689cf4aSJeff Kirsher {
3153e689cf4aSJeff Kirsher 	u64 val = nr64(FFLP_CFG_1);
3154e689cf4aSJeff Kirsher 
3155e689cf4aSJeff Kirsher 	if (on)
3156e689cf4aSJeff Kirsher 		val &= ~FFLP_CFG_1_ERRORDIS;
3157e689cf4aSJeff Kirsher 	else
3158e689cf4aSJeff Kirsher 		val |= FFLP_CFG_1_ERRORDIS;
3159e689cf4aSJeff Kirsher 	nw64(FFLP_CFG_1, val);
3160e689cf4aSJeff Kirsher }
3161e689cf4aSJeff Kirsher 
fflp_hash_clear(struct niu * np)3162e689cf4aSJeff Kirsher static int fflp_hash_clear(struct niu *np)
3163e689cf4aSJeff Kirsher {
3164e689cf4aSJeff Kirsher 	struct fcram_hash_ipv4 ent;
3165e689cf4aSJeff Kirsher 	unsigned long i;
3166e689cf4aSJeff Kirsher 
3167e689cf4aSJeff Kirsher 	/* IPV4 hash entry with valid bit clear, rest is don't care.  */
3168e689cf4aSJeff Kirsher 	memset(&ent, 0, sizeof(ent));
3169e689cf4aSJeff Kirsher 	ent.header = HASH_HEADER_EXT;
3170e689cf4aSJeff Kirsher 
3171e689cf4aSJeff Kirsher 	for (i = 0; i < FCRAM_SIZE; i += sizeof(ent)) {
3172e689cf4aSJeff Kirsher 		int err = hash_write(np, 0, i, 1, (u64 *) &ent);
3173e689cf4aSJeff Kirsher 		if (err)
3174e689cf4aSJeff Kirsher 			return err;
3175e689cf4aSJeff Kirsher 	}
3176e689cf4aSJeff Kirsher 	return 0;
3177e689cf4aSJeff Kirsher }
3178e689cf4aSJeff Kirsher 
fflp_early_init(struct niu * np)3179e689cf4aSJeff Kirsher static int fflp_early_init(struct niu *np)
3180e689cf4aSJeff Kirsher {
3181e689cf4aSJeff Kirsher 	struct niu_parent *parent;
3182e689cf4aSJeff Kirsher 	unsigned long flags;
3183e689cf4aSJeff Kirsher 	int err;
3184e689cf4aSJeff Kirsher 
3185e689cf4aSJeff Kirsher 	niu_lock_parent(np, flags);
3186e689cf4aSJeff Kirsher 
3187e689cf4aSJeff Kirsher 	parent = np->parent;
3188e689cf4aSJeff Kirsher 	err = 0;
3189e689cf4aSJeff Kirsher 	if (!(parent->flags & PARENT_FLGS_CLS_HWINIT)) {
3190e689cf4aSJeff Kirsher 		if (np->parent->plat_type != PLAT_TYPE_NIU) {
3191e689cf4aSJeff Kirsher 			fflp_reset(np);
3192e689cf4aSJeff Kirsher 			fflp_set_timings(np);
3193e689cf4aSJeff Kirsher 			err = fflp_disable_all_partitions(np);
3194e689cf4aSJeff Kirsher 			if (err) {
3195e689cf4aSJeff Kirsher 				netif_printk(np, probe, KERN_DEBUG, np->dev,
3196e689cf4aSJeff Kirsher 					     "fflp_disable_all_partitions failed, err=%d\n",
3197e689cf4aSJeff Kirsher 					     err);
3198e689cf4aSJeff Kirsher 				goto out;
3199e689cf4aSJeff Kirsher 			}
3200e689cf4aSJeff Kirsher 		}
3201e689cf4aSJeff Kirsher 
3202e689cf4aSJeff Kirsher 		err = tcam_early_init(np);
3203e689cf4aSJeff Kirsher 		if (err) {
3204e689cf4aSJeff Kirsher 			netif_printk(np, probe, KERN_DEBUG, np->dev,
3205e689cf4aSJeff Kirsher 				     "tcam_early_init failed, err=%d\n", err);
3206e689cf4aSJeff Kirsher 			goto out;
3207e689cf4aSJeff Kirsher 		}
3208e689cf4aSJeff Kirsher 		fflp_llcsnap_enable(np, 1);
3209e689cf4aSJeff Kirsher 		fflp_errors_enable(np, 0);
3210e689cf4aSJeff Kirsher 		nw64(H1POLY, 0);
3211e689cf4aSJeff Kirsher 		nw64(H2POLY, 0);
3212e689cf4aSJeff Kirsher 
3213e689cf4aSJeff Kirsher 		err = tcam_flush_all(np);
3214e689cf4aSJeff Kirsher 		if (err) {
3215e689cf4aSJeff Kirsher 			netif_printk(np, probe, KERN_DEBUG, np->dev,
3216e689cf4aSJeff Kirsher 				     "tcam_flush_all failed, err=%d\n", err);
3217e689cf4aSJeff Kirsher 			goto out;
3218e689cf4aSJeff Kirsher 		}
3219e689cf4aSJeff Kirsher 		if (np->parent->plat_type != PLAT_TYPE_NIU) {
3220e689cf4aSJeff Kirsher 			err = fflp_hash_clear(np);
3221e689cf4aSJeff Kirsher 			if (err) {
3222e689cf4aSJeff Kirsher 				netif_printk(np, probe, KERN_DEBUG, np->dev,
3223e689cf4aSJeff Kirsher 					     "fflp_hash_clear failed, err=%d\n",
3224e689cf4aSJeff Kirsher 					     err);
3225e689cf4aSJeff Kirsher 				goto out;
3226e689cf4aSJeff Kirsher 			}
3227e689cf4aSJeff Kirsher 		}
3228e689cf4aSJeff Kirsher 
3229e689cf4aSJeff Kirsher 		vlan_tbl_clear(np);
3230e689cf4aSJeff Kirsher 
3231e689cf4aSJeff Kirsher 		parent->flags |= PARENT_FLGS_CLS_HWINIT;
3232e689cf4aSJeff Kirsher 	}
3233e689cf4aSJeff Kirsher out:
3234e689cf4aSJeff Kirsher 	niu_unlock_parent(np, flags);
3235e689cf4aSJeff Kirsher 	return err;
3236e689cf4aSJeff Kirsher }
3237e689cf4aSJeff Kirsher 
niu_set_flow_key(struct niu * np,unsigned long class_code,u64 key)3238e689cf4aSJeff Kirsher static int niu_set_flow_key(struct niu *np, unsigned long class_code, u64 key)
3239e689cf4aSJeff Kirsher {
3240e689cf4aSJeff Kirsher 	if (class_code < CLASS_CODE_USER_PROG1 ||
3241e689cf4aSJeff Kirsher 	    class_code > CLASS_CODE_SCTP_IPV6)
3242e689cf4aSJeff Kirsher 		return -EINVAL;
3243e689cf4aSJeff Kirsher 
3244e689cf4aSJeff Kirsher 	nw64(FLOW_KEY(class_code - CLASS_CODE_USER_PROG1), key);
3245e689cf4aSJeff Kirsher 	return 0;
3246e689cf4aSJeff Kirsher }
3247e689cf4aSJeff Kirsher 
niu_set_tcam_key(struct niu * np,unsigned long class_code,u64 key)3248e689cf4aSJeff Kirsher static int niu_set_tcam_key(struct niu *np, unsigned long class_code, u64 key)
3249e689cf4aSJeff Kirsher {
3250e689cf4aSJeff Kirsher 	if (class_code < CLASS_CODE_USER_PROG1 ||
3251e689cf4aSJeff Kirsher 	    class_code > CLASS_CODE_SCTP_IPV6)
3252e689cf4aSJeff Kirsher 		return -EINVAL;
3253e689cf4aSJeff Kirsher 
3254e689cf4aSJeff Kirsher 	nw64(TCAM_KEY(class_code - CLASS_CODE_USER_PROG1), key);
3255e689cf4aSJeff Kirsher 	return 0;
3256e689cf4aSJeff Kirsher }
3257e689cf4aSJeff Kirsher 
3258e689cf4aSJeff Kirsher /* Entries for the ports are interleaved in the TCAM */
tcam_get_index(struct niu * np,u16 idx)3259e689cf4aSJeff Kirsher static u16 tcam_get_index(struct niu *np, u16 idx)
3260e689cf4aSJeff Kirsher {
3261e689cf4aSJeff Kirsher 	/* One entry reserved for IP fragment rule */
3262e689cf4aSJeff Kirsher 	if (idx >= (np->clas.tcam_sz - 1))
3263e689cf4aSJeff Kirsher 		idx = 0;
3264e689cf4aSJeff Kirsher 	return np->clas.tcam_top + ((idx+1) * np->parent->num_ports);
3265e689cf4aSJeff Kirsher }
3266e689cf4aSJeff Kirsher 
tcam_get_size(struct niu * np)3267e689cf4aSJeff Kirsher static u16 tcam_get_size(struct niu *np)
3268e689cf4aSJeff Kirsher {
3269e689cf4aSJeff Kirsher 	/* One entry reserved for IP fragment rule */
3270e689cf4aSJeff Kirsher 	return np->clas.tcam_sz - 1;
3271e689cf4aSJeff Kirsher }
3272e689cf4aSJeff Kirsher 
tcam_get_valid_entry_cnt(struct niu * np)3273e689cf4aSJeff Kirsher static u16 tcam_get_valid_entry_cnt(struct niu *np)
3274e689cf4aSJeff Kirsher {
3275e689cf4aSJeff Kirsher 	/* One entry reserved for IP fragment rule */
3276e689cf4aSJeff Kirsher 	return np->clas.tcam_valid_entries - 1;
3277e689cf4aSJeff Kirsher }
3278e689cf4aSJeff Kirsher 
niu_rx_skb_append(struct sk_buff * skb,struct page * page,u32 offset,u32 size,u32 truesize)3279e689cf4aSJeff Kirsher static void niu_rx_skb_append(struct sk_buff *skb, struct page *page,
3280e7e5a403SEric Dumazet 			      u32 offset, u32 size, u32 truesize)
3281e689cf4aSJeff Kirsher {
3282e7e5a403SEric Dumazet 	skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, offset, size);
3283e689cf4aSJeff Kirsher 
3284e689cf4aSJeff Kirsher 	skb->len += size;
3285e689cf4aSJeff Kirsher 	skb->data_len += size;
3286e7e5a403SEric Dumazet 	skb->truesize += truesize;
3287e689cf4aSJeff Kirsher }
3288e689cf4aSJeff Kirsher 
niu_hash_rxaddr(struct rx_ring_info * rp,u64 a)3289e689cf4aSJeff Kirsher static unsigned int niu_hash_rxaddr(struct rx_ring_info *rp, u64 a)
3290e689cf4aSJeff Kirsher {
3291e689cf4aSJeff Kirsher 	a >>= PAGE_SHIFT;
3292e689cf4aSJeff Kirsher 	a ^= (a >> ilog2(MAX_RBR_RING_SIZE));
3293e689cf4aSJeff Kirsher 
3294e689cf4aSJeff Kirsher 	return a & (MAX_RBR_RING_SIZE - 1);
3295e689cf4aSJeff Kirsher }
3296e689cf4aSJeff Kirsher 
niu_find_rxpage(struct rx_ring_info * rp,u64 addr,struct page *** link)3297e689cf4aSJeff Kirsher static struct page *niu_find_rxpage(struct rx_ring_info *rp, u64 addr,
3298e689cf4aSJeff Kirsher 				    struct page ***link)
3299e689cf4aSJeff Kirsher {
3300e689cf4aSJeff Kirsher 	unsigned int h = niu_hash_rxaddr(rp, addr);
3301e689cf4aSJeff Kirsher 	struct page *p, **pp;
3302e689cf4aSJeff Kirsher 
3303e689cf4aSJeff Kirsher 	addr &= PAGE_MASK;
3304e689cf4aSJeff Kirsher 	pp = &rp->rxhash[h];
33052dcfe9e2SKees Cook 	for (; (p = *pp) != NULL; pp = &niu_next_page(p)) {
3306e689cf4aSJeff Kirsher 		if (p->index == addr) {
3307e689cf4aSJeff Kirsher 			*link = pp;
3308e689cf4aSJeff Kirsher 			goto found;
3309e689cf4aSJeff Kirsher 		}
3310e689cf4aSJeff Kirsher 	}
3311e689cf4aSJeff Kirsher 	BUG();
3312e689cf4aSJeff Kirsher 
3313e689cf4aSJeff Kirsher found:
3314e689cf4aSJeff Kirsher 	return p;
3315e689cf4aSJeff Kirsher }
3316e689cf4aSJeff Kirsher 
niu_hash_page(struct rx_ring_info * rp,struct page * page,u64 base)3317e689cf4aSJeff Kirsher static void niu_hash_page(struct rx_ring_info *rp, struct page *page, u64 base)
3318e689cf4aSJeff Kirsher {
3319e689cf4aSJeff Kirsher 	unsigned int h = niu_hash_rxaddr(rp, base);
3320e689cf4aSJeff Kirsher 
3321e689cf4aSJeff Kirsher 	page->index = base;
33222dcfe9e2SKees Cook 	niu_next_page(page) = rp->rxhash[h];
3323e689cf4aSJeff Kirsher 	rp->rxhash[h] = page;
3324e689cf4aSJeff Kirsher }
3325e689cf4aSJeff Kirsher 
niu_rbr_add_page(struct niu * np,struct rx_ring_info * rp,gfp_t mask,int start_index)3326e689cf4aSJeff Kirsher static int niu_rbr_add_page(struct niu *np, struct rx_ring_info *rp,
3327e689cf4aSJeff Kirsher 			    gfp_t mask, int start_index)
3328e689cf4aSJeff Kirsher {
3329e689cf4aSJeff Kirsher 	struct page *page;
3330e689cf4aSJeff Kirsher 	u64 addr;
3331e689cf4aSJeff Kirsher 	int i;
3332e689cf4aSJeff Kirsher 
3333e689cf4aSJeff Kirsher 	page = alloc_page(mask);
3334e689cf4aSJeff Kirsher 	if (!page)
3335e689cf4aSJeff Kirsher 		return -ENOMEM;
3336e689cf4aSJeff Kirsher 
3337e689cf4aSJeff Kirsher 	addr = np->ops->map_page(np->device, page, 0,
3338e689cf4aSJeff Kirsher 				 PAGE_SIZE, DMA_FROM_DEVICE);
3339ec2deec1SShuah Khan 	if (!addr) {
3340ec2deec1SShuah Khan 		__free_page(page);
3341ec2deec1SShuah Khan 		return -ENOMEM;
3342ec2deec1SShuah Khan 	}
3343e689cf4aSJeff Kirsher 
3344e689cf4aSJeff Kirsher 	niu_hash_page(rp, page, addr);
3345e689cf4aSJeff Kirsher 	if (rp->rbr_blocks_per_page > 1)
3346fe896d18SJoonsoo Kim 		page_ref_add(page, rp->rbr_blocks_per_page - 1);
3347e689cf4aSJeff Kirsher 
3348e689cf4aSJeff Kirsher 	for (i = 0; i < rp->rbr_blocks_per_page; i++) {
3349e689cf4aSJeff Kirsher 		__le32 *rbr = &rp->rbr[start_index + i];
3350e689cf4aSJeff Kirsher 
3351e689cf4aSJeff Kirsher 		*rbr = cpu_to_le32(addr >> RBR_DESCR_ADDR_SHIFT);
3352e689cf4aSJeff Kirsher 		addr += rp->rbr_block_size;
3353e689cf4aSJeff Kirsher 	}
3354e689cf4aSJeff Kirsher 
3355e689cf4aSJeff Kirsher 	return 0;
3356e689cf4aSJeff Kirsher }
3357e689cf4aSJeff Kirsher 
niu_rbr_refill(struct niu * np,struct rx_ring_info * rp,gfp_t mask)3358e689cf4aSJeff Kirsher static void niu_rbr_refill(struct niu *np, struct rx_ring_info *rp, gfp_t mask)
3359e689cf4aSJeff Kirsher {
3360e689cf4aSJeff Kirsher 	int index = rp->rbr_index;
3361e689cf4aSJeff Kirsher 
3362e689cf4aSJeff Kirsher 	rp->rbr_pending++;
3363e689cf4aSJeff Kirsher 	if ((rp->rbr_pending % rp->rbr_blocks_per_page) == 0) {
3364e689cf4aSJeff Kirsher 		int err = niu_rbr_add_page(np, rp, mask, index);
3365e689cf4aSJeff Kirsher 
3366e689cf4aSJeff Kirsher 		if (unlikely(err)) {
3367e689cf4aSJeff Kirsher 			rp->rbr_pending--;
3368e689cf4aSJeff Kirsher 			return;
3369e689cf4aSJeff Kirsher 		}
3370e689cf4aSJeff Kirsher 
3371e689cf4aSJeff Kirsher 		rp->rbr_index += rp->rbr_blocks_per_page;
3372e689cf4aSJeff Kirsher 		BUG_ON(rp->rbr_index > rp->rbr_table_size);
3373e689cf4aSJeff Kirsher 		if (rp->rbr_index == rp->rbr_table_size)
3374e689cf4aSJeff Kirsher 			rp->rbr_index = 0;
3375e689cf4aSJeff Kirsher 
3376e689cf4aSJeff Kirsher 		if (rp->rbr_pending >= rp->rbr_kick_thresh) {
3377e689cf4aSJeff Kirsher 			nw64(RBR_KICK(rp->rx_channel), rp->rbr_pending);
3378e689cf4aSJeff Kirsher 			rp->rbr_pending = 0;
3379e689cf4aSJeff Kirsher 		}
3380e689cf4aSJeff Kirsher 	}
3381e689cf4aSJeff Kirsher }
3382e689cf4aSJeff Kirsher 
niu_rx_pkt_ignore(struct niu * np,struct rx_ring_info * rp)3383e689cf4aSJeff Kirsher static int niu_rx_pkt_ignore(struct niu *np, struct rx_ring_info *rp)
3384e689cf4aSJeff Kirsher {
3385e689cf4aSJeff Kirsher 	unsigned int index = rp->rcr_index;
3386e689cf4aSJeff Kirsher 	int num_rcr = 0;
3387e689cf4aSJeff Kirsher 
3388e689cf4aSJeff Kirsher 	rp->rx_dropped++;
3389e689cf4aSJeff Kirsher 	while (1) {
3390e689cf4aSJeff Kirsher 		struct page *page, **link;
3391e689cf4aSJeff Kirsher 		u64 addr, val;
3392e689cf4aSJeff Kirsher 		u32 rcr_size;
3393e689cf4aSJeff Kirsher 
3394e689cf4aSJeff Kirsher 		num_rcr++;
3395e689cf4aSJeff Kirsher 
3396e689cf4aSJeff Kirsher 		val = le64_to_cpup(&rp->rcr[index]);
3397e689cf4aSJeff Kirsher 		addr = (val & RCR_ENTRY_PKT_BUF_ADDR) <<
3398e689cf4aSJeff Kirsher 			RCR_ENTRY_PKT_BUF_ADDR_SHIFT;
3399e689cf4aSJeff Kirsher 		page = niu_find_rxpage(rp, addr, &link);
3400e689cf4aSJeff Kirsher 
3401e689cf4aSJeff Kirsher 		rcr_size = rp->rbr_sizes[(val & RCR_ENTRY_PKTBUFSZ) >>
3402e689cf4aSJeff Kirsher 					 RCR_ENTRY_PKTBUFSZ_SHIFT];
3403e689cf4aSJeff Kirsher 		if ((page->index + PAGE_SIZE) - rcr_size == addr) {
34042dcfe9e2SKees Cook 			*link = niu_next_page(page);
3405e689cf4aSJeff Kirsher 			np->ops->unmap_page(np->device, page->index,
3406e689cf4aSJeff Kirsher 					    PAGE_SIZE, DMA_FROM_DEVICE);
3407e689cf4aSJeff Kirsher 			page->index = 0;
34082dcfe9e2SKees Cook 			niu_next_page(page) = NULL;
3409e689cf4aSJeff Kirsher 			__free_page(page);
3410e689cf4aSJeff Kirsher 			rp->rbr_refill_pending++;
3411e689cf4aSJeff Kirsher 		}
3412e689cf4aSJeff Kirsher 
3413e689cf4aSJeff Kirsher 		index = NEXT_RCR(rp, index);
3414e689cf4aSJeff Kirsher 		if (!(val & RCR_ENTRY_MULTI))
3415e689cf4aSJeff Kirsher 			break;
3416e689cf4aSJeff Kirsher 
3417e689cf4aSJeff Kirsher 	}
3418e689cf4aSJeff Kirsher 	rp->rcr_index = index;
3419e689cf4aSJeff Kirsher 
3420e689cf4aSJeff Kirsher 	return num_rcr;
3421e689cf4aSJeff Kirsher }
3422e689cf4aSJeff Kirsher 
niu_process_rx_pkt(struct napi_struct * napi,struct niu * np,struct rx_ring_info * rp)3423e689cf4aSJeff Kirsher static int niu_process_rx_pkt(struct napi_struct *napi, struct niu *np,
3424e689cf4aSJeff Kirsher 			      struct rx_ring_info *rp)
3425e689cf4aSJeff Kirsher {
3426e689cf4aSJeff Kirsher 	unsigned int index = rp->rcr_index;
3427e689cf4aSJeff Kirsher 	struct rx_pkt_hdr1 *rh;
3428e689cf4aSJeff Kirsher 	struct sk_buff *skb;
3429e689cf4aSJeff Kirsher 	int len, num_rcr;
3430e689cf4aSJeff Kirsher 
3431e689cf4aSJeff Kirsher 	skb = netdev_alloc_skb(np->dev, RX_SKB_ALLOC_SIZE);
3432e689cf4aSJeff Kirsher 	if (unlikely(!skb))
3433e689cf4aSJeff Kirsher 		return niu_rx_pkt_ignore(np, rp);
3434e689cf4aSJeff Kirsher 
3435e689cf4aSJeff Kirsher 	num_rcr = 0;
3436e689cf4aSJeff Kirsher 	while (1) {
3437e689cf4aSJeff Kirsher 		struct page *page, **link;
3438e689cf4aSJeff Kirsher 		u32 rcr_size, append_size;
3439e689cf4aSJeff Kirsher 		u64 addr, val, off;
3440e689cf4aSJeff Kirsher 
3441e689cf4aSJeff Kirsher 		num_rcr++;
3442e689cf4aSJeff Kirsher 
3443e689cf4aSJeff Kirsher 		val = le64_to_cpup(&rp->rcr[index]);
3444e689cf4aSJeff Kirsher 
3445e689cf4aSJeff Kirsher 		len = (val & RCR_ENTRY_L2_LEN) >>
3446e689cf4aSJeff Kirsher 			RCR_ENTRY_L2_LEN_SHIFT;
344714224923SRob Taglang 		append_size = len + ETH_HLEN + ETH_FCS_LEN;
3448e689cf4aSJeff Kirsher 
3449e689cf4aSJeff Kirsher 		addr = (val & RCR_ENTRY_PKT_BUF_ADDR) <<
3450e689cf4aSJeff Kirsher 			RCR_ENTRY_PKT_BUF_ADDR_SHIFT;
3451e689cf4aSJeff Kirsher 		page = niu_find_rxpage(rp, addr, &link);
3452e689cf4aSJeff Kirsher 
3453e689cf4aSJeff Kirsher 		rcr_size = rp->rbr_sizes[(val & RCR_ENTRY_PKTBUFSZ) >>
3454e689cf4aSJeff Kirsher 					 RCR_ENTRY_PKTBUFSZ_SHIFT];
3455e689cf4aSJeff Kirsher 
3456e689cf4aSJeff Kirsher 		off = addr & ~PAGE_MASK;
3457e689cf4aSJeff Kirsher 		if (num_rcr == 1) {
3458e689cf4aSJeff Kirsher 			int ptype;
3459e689cf4aSJeff Kirsher 
3460e689cf4aSJeff Kirsher 			ptype = (val >> RCR_ENTRY_PKT_TYPE_SHIFT);
3461e689cf4aSJeff Kirsher 			if ((ptype == RCR_PKT_TYPE_TCP ||
3462e689cf4aSJeff Kirsher 			     ptype == RCR_PKT_TYPE_UDP) &&
3463e689cf4aSJeff Kirsher 			    !(val & (RCR_ENTRY_NOPORT |
3464e689cf4aSJeff Kirsher 				     RCR_ENTRY_ERROR)))
3465e689cf4aSJeff Kirsher 				skb->ip_summed = CHECKSUM_UNNECESSARY;
3466e689cf4aSJeff Kirsher 			else
3467e689cf4aSJeff Kirsher 				skb_checksum_none_assert(skb);
3468e689cf4aSJeff Kirsher 		} else if (!(val & RCR_ENTRY_MULTI))
346914224923SRob Taglang 			append_size = append_size - skb->len;
3470e689cf4aSJeff Kirsher 
3471e7e5a403SEric Dumazet 		niu_rx_skb_append(skb, page, off, append_size, rcr_size);
3472e689cf4aSJeff Kirsher 		if ((page->index + rp->rbr_block_size) - rcr_size == addr) {
34732dcfe9e2SKees Cook 			*link = niu_next_page(page);
3474e689cf4aSJeff Kirsher 			np->ops->unmap_page(np->device, page->index,
3475e689cf4aSJeff Kirsher 					    PAGE_SIZE, DMA_FROM_DEVICE);
3476e689cf4aSJeff Kirsher 			page->index = 0;
34772dcfe9e2SKees Cook 			niu_next_page(page) = NULL;
3478e689cf4aSJeff Kirsher 			rp->rbr_refill_pending++;
3479e689cf4aSJeff Kirsher 		} else
3480e689cf4aSJeff Kirsher 			get_page(page);
3481e689cf4aSJeff Kirsher 
3482e689cf4aSJeff Kirsher 		index = NEXT_RCR(rp, index);
3483e689cf4aSJeff Kirsher 		if (!(val & RCR_ENTRY_MULTI))
3484e689cf4aSJeff Kirsher 			break;
3485e689cf4aSJeff Kirsher 
3486e689cf4aSJeff Kirsher 	}
3487e689cf4aSJeff Kirsher 	rp->rcr_index = index;
3488e689cf4aSJeff Kirsher 
3489e689cf4aSJeff Kirsher 	len += sizeof(*rh);
3490e689cf4aSJeff Kirsher 	len = min_t(int, len, sizeof(*rh) + VLAN_ETH_HLEN);
3491e689cf4aSJeff Kirsher 	__pskb_pull_tail(skb, len);
3492e689cf4aSJeff Kirsher 
3493e689cf4aSJeff Kirsher 	rh = (struct rx_pkt_hdr1 *) skb->data;
3494e689cf4aSJeff Kirsher 	if (np->dev->features & NETIF_F_RXHASH)
34953b21567fSTom Herbert 		skb_set_hash(skb,
34963b21567fSTom Herbert 			     ((u32)rh->hashval2_0 << 24 |
3497e689cf4aSJeff Kirsher 			      (u32)rh->hashval2_1 << 16 |
3498e689cf4aSJeff Kirsher 			      (u32)rh->hashval1_1 << 8 |
34993b21567fSTom Herbert 			      (u32)rh->hashval1_2 << 0),
35003b21567fSTom Herbert 			     PKT_HASH_TYPE_L3);
3501e689cf4aSJeff Kirsher 	skb_pull(skb, sizeof(*rh));
3502e689cf4aSJeff Kirsher 
3503e689cf4aSJeff Kirsher 	rp->rx_packets++;
3504e689cf4aSJeff Kirsher 	rp->rx_bytes += skb->len;
3505e689cf4aSJeff Kirsher 
3506e689cf4aSJeff Kirsher 	skb->protocol = eth_type_trans(skb, np->dev);
3507e689cf4aSJeff Kirsher 	skb_record_rx_queue(skb, rp->rx_channel);
3508e689cf4aSJeff Kirsher 	napi_gro_receive(napi, skb);
3509e689cf4aSJeff Kirsher 
3510e689cf4aSJeff Kirsher 	return num_rcr;
3511e689cf4aSJeff Kirsher }
3512e689cf4aSJeff Kirsher 
niu_rbr_fill(struct niu * np,struct rx_ring_info * rp,gfp_t mask)3513e689cf4aSJeff Kirsher static int niu_rbr_fill(struct niu *np, struct rx_ring_info *rp, gfp_t mask)
3514e689cf4aSJeff Kirsher {
3515e689cf4aSJeff Kirsher 	int blocks_per_page = rp->rbr_blocks_per_page;
3516e689cf4aSJeff Kirsher 	int err, index = rp->rbr_index;
3517e689cf4aSJeff Kirsher 
3518e689cf4aSJeff Kirsher 	err = 0;
3519e689cf4aSJeff Kirsher 	while (index < (rp->rbr_table_size - blocks_per_page)) {
3520e689cf4aSJeff Kirsher 		err = niu_rbr_add_page(np, rp, mask, index);
35219b70749eSShuah Khan 		if (unlikely(err))
3522e689cf4aSJeff Kirsher 			break;
3523e689cf4aSJeff Kirsher 
3524e689cf4aSJeff Kirsher 		index += blocks_per_page;
3525e689cf4aSJeff Kirsher 	}
3526e689cf4aSJeff Kirsher 
3527e689cf4aSJeff Kirsher 	rp->rbr_index = index;
3528e689cf4aSJeff Kirsher 	return err;
3529e689cf4aSJeff Kirsher }
3530e689cf4aSJeff Kirsher 
niu_rbr_free(struct niu * np,struct rx_ring_info * rp)3531e689cf4aSJeff Kirsher static void niu_rbr_free(struct niu *np, struct rx_ring_info *rp)
3532e689cf4aSJeff Kirsher {
3533e689cf4aSJeff Kirsher 	int i;
3534e689cf4aSJeff Kirsher 
3535e689cf4aSJeff Kirsher 	for (i = 0; i < MAX_RBR_RING_SIZE; i++) {
3536e689cf4aSJeff Kirsher 		struct page *page;
3537e689cf4aSJeff Kirsher 
3538e689cf4aSJeff Kirsher 		page = rp->rxhash[i];
3539e689cf4aSJeff Kirsher 		while (page) {
35402dcfe9e2SKees Cook 			struct page *next = niu_next_page(page);
3541e689cf4aSJeff Kirsher 			u64 base = page->index;
3542e689cf4aSJeff Kirsher 
3543e689cf4aSJeff Kirsher 			np->ops->unmap_page(np->device, base, PAGE_SIZE,
3544e689cf4aSJeff Kirsher 					    DMA_FROM_DEVICE);
3545e689cf4aSJeff Kirsher 			page->index = 0;
35462dcfe9e2SKees Cook 			niu_next_page(page) = NULL;
3547e689cf4aSJeff Kirsher 
3548e689cf4aSJeff Kirsher 			__free_page(page);
3549e689cf4aSJeff Kirsher 
3550e689cf4aSJeff Kirsher 			page = next;
3551e689cf4aSJeff Kirsher 		}
3552e689cf4aSJeff Kirsher 	}
3553e689cf4aSJeff Kirsher 
3554e689cf4aSJeff Kirsher 	for (i = 0; i < rp->rbr_table_size; i++)
3555e689cf4aSJeff Kirsher 		rp->rbr[i] = cpu_to_le32(0);
3556e689cf4aSJeff Kirsher 	rp->rbr_index = 0;
3557e689cf4aSJeff Kirsher }
3558e689cf4aSJeff Kirsher 
release_tx_packet(struct niu * np,struct tx_ring_info * rp,int idx)3559e689cf4aSJeff Kirsher static int release_tx_packet(struct niu *np, struct tx_ring_info *rp, int idx)
3560e689cf4aSJeff Kirsher {
3561e689cf4aSJeff Kirsher 	struct tx_buff_info *tb = &rp->tx_buffs[idx];
3562e689cf4aSJeff Kirsher 	struct sk_buff *skb = tb->skb;
3563e689cf4aSJeff Kirsher 	struct tx_pkt_hdr *tp;
3564e689cf4aSJeff Kirsher 	u64 tx_flags;
3565e689cf4aSJeff Kirsher 	int i, len;
3566e689cf4aSJeff Kirsher 
3567e689cf4aSJeff Kirsher 	tp = (struct tx_pkt_hdr *) skb->data;
3568e689cf4aSJeff Kirsher 	tx_flags = le64_to_cpup(&tp->flags);
3569e689cf4aSJeff Kirsher 
3570e689cf4aSJeff Kirsher 	rp->tx_packets++;
3571e689cf4aSJeff Kirsher 	rp->tx_bytes += (((tx_flags & TXHDR_LEN) >> TXHDR_LEN_SHIFT) -
3572e689cf4aSJeff Kirsher 			 ((tx_flags & TXHDR_PAD) / 2));
3573e689cf4aSJeff Kirsher 
3574e689cf4aSJeff Kirsher 	len = skb_headlen(skb);
3575e689cf4aSJeff Kirsher 	np->ops->unmap_single(np->device, tb->mapping,
3576e689cf4aSJeff Kirsher 			      len, DMA_TO_DEVICE);
3577e689cf4aSJeff Kirsher 
3578e689cf4aSJeff Kirsher 	if (le64_to_cpu(rp->descr[idx]) & TX_DESC_MARK)
3579e689cf4aSJeff Kirsher 		rp->mark_pending--;
3580e689cf4aSJeff Kirsher 
3581e689cf4aSJeff Kirsher 	tb->skb = NULL;
3582e689cf4aSJeff Kirsher 	do {
3583e689cf4aSJeff Kirsher 		idx = NEXT_TX(rp, idx);
3584e689cf4aSJeff Kirsher 		len -= MAX_TX_DESC_LEN;
3585e689cf4aSJeff Kirsher 	} while (len > 0);
3586e689cf4aSJeff Kirsher 
3587e689cf4aSJeff Kirsher 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3588e689cf4aSJeff Kirsher 		tb = &rp->tx_buffs[idx];
3589e689cf4aSJeff Kirsher 		BUG_ON(tb->skb != NULL);
3590e689cf4aSJeff Kirsher 		np->ops->unmap_page(np->device, tb->mapping,
35919e903e08SEric Dumazet 				    skb_frag_size(&skb_shinfo(skb)->frags[i]),
3592e689cf4aSJeff Kirsher 				    DMA_TO_DEVICE);
3593e689cf4aSJeff Kirsher 		idx = NEXT_TX(rp, idx);
3594e689cf4aSJeff Kirsher 	}
3595e689cf4aSJeff Kirsher 
3596e689cf4aSJeff Kirsher 	dev_kfree_skb(skb);
3597e689cf4aSJeff Kirsher 
3598e689cf4aSJeff Kirsher 	return idx;
3599e689cf4aSJeff Kirsher }
3600e689cf4aSJeff Kirsher 
3601e689cf4aSJeff Kirsher #define NIU_TX_WAKEUP_THRESH(rp)		((rp)->pending / 4)
3602e689cf4aSJeff Kirsher 
niu_tx_work(struct niu * np,struct tx_ring_info * rp)3603e689cf4aSJeff Kirsher static void niu_tx_work(struct niu *np, struct tx_ring_info *rp)
3604e689cf4aSJeff Kirsher {
3605e689cf4aSJeff Kirsher 	struct netdev_queue *txq;
3606e689cf4aSJeff Kirsher 	u16 pkt_cnt, tmp;
3607e689cf4aSJeff Kirsher 	int cons, index;
3608e689cf4aSJeff Kirsher 	u64 cs;
3609e689cf4aSJeff Kirsher 
3610e689cf4aSJeff Kirsher 	index = (rp - np->tx_rings);
3611e689cf4aSJeff Kirsher 	txq = netdev_get_tx_queue(np->dev, index);
3612e689cf4aSJeff Kirsher 
3613e689cf4aSJeff Kirsher 	cs = rp->tx_cs;
3614e689cf4aSJeff Kirsher 	if (unlikely(!(cs & (TX_CS_MK | TX_CS_MMK))))
3615e689cf4aSJeff Kirsher 		goto out;
3616e689cf4aSJeff Kirsher 
3617e689cf4aSJeff Kirsher 	tmp = pkt_cnt = (cs & TX_CS_PKT_CNT) >> TX_CS_PKT_CNT_SHIFT;
3618e689cf4aSJeff Kirsher 	pkt_cnt = (pkt_cnt - rp->last_pkt_cnt) &
3619e689cf4aSJeff Kirsher 		(TX_CS_PKT_CNT >> TX_CS_PKT_CNT_SHIFT);
3620e689cf4aSJeff Kirsher 
3621e689cf4aSJeff Kirsher 	rp->last_pkt_cnt = tmp;
3622e689cf4aSJeff Kirsher 
3623e689cf4aSJeff Kirsher 	cons = rp->cons;
3624e689cf4aSJeff Kirsher 
3625e689cf4aSJeff Kirsher 	netif_printk(np, tx_done, KERN_DEBUG, np->dev,
3626e689cf4aSJeff Kirsher 		     "%s() pkt_cnt[%u] cons[%d]\n", __func__, pkt_cnt, cons);
3627e689cf4aSJeff Kirsher 
36286a2b28efSDavid S. Miller 	while (pkt_cnt--)
3629e689cf4aSJeff Kirsher 		cons = release_tx_packet(np, rp, cons);
3630e689cf4aSJeff Kirsher 
3631e689cf4aSJeff Kirsher 	rp->cons = cons;
3632e689cf4aSJeff Kirsher 	smp_mb();
3633e689cf4aSJeff Kirsher 
3634e689cf4aSJeff Kirsher out:
3635e689cf4aSJeff Kirsher 	if (unlikely(netif_tx_queue_stopped(txq) &&
3636e689cf4aSJeff Kirsher 		     (niu_tx_avail(rp) > NIU_TX_WAKEUP_THRESH(rp)))) {
3637e689cf4aSJeff Kirsher 		__netif_tx_lock(txq, smp_processor_id());
3638e689cf4aSJeff Kirsher 		if (netif_tx_queue_stopped(txq) &&
3639e689cf4aSJeff Kirsher 		    (niu_tx_avail(rp) > NIU_TX_WAKEUP_THRESH(rp)))
3640e689cf4aSJeff Kirsher 			netif_tx_wake_queue(txq);
3641e689cf4aSJeff Kirsher 		__netif_tx_unlock(txq);
3642e689cf4aSJeff Kirsher 	}
3643e689cf4aSJeff Kirsher }
3644e689cf4aSJeff Kirsher 
niu_sync_rx_discard_stats(struct niu * np,struct rx_ring_info * rp,const int limit)3645e689cf4aSJeff Kirsher static inline void niu_sync_rx_discard_stats(struct niu *np,
3646e689cf4aSJeff Kirsher 					     struct rx_ring_info *rp,
3647e689cf4aSJeff Kirsher 					     const int limit)
3648e689cf4aSJeff Kirsher {
3649e689cf4aSJeff Kirsher 	/* This elaborate scheme is needed for reading the RX discard
3650e689cf4aSJeff Kirsher 	 * counters, as they are only 16-bit and can overflow quickly,
3651e689cf4aSJeff Kirsher 	 * and because the overflow indication bit is not usable as
3652e689cf4aSJeff Kirsher 	 * the counter value does not wrap, but remains at max value
3653e689cf4aSJeff Kirsher 	 * 0xFFFF.
3654e689cf4aSJeff Kirsher 	 *
3655e689cf4aSJeff Kirsher 	 * In theory and in practice counters can be lost in between
3656e689cf4aSJeff Kirsher 	 * reading nr64() and clearing the counter nw64().  For this
3657e689cf4aSJeff Kirsher 	 * reason, the number of counter clearings nw64() is
3658e689cf4aSJeff Kirsher 	 * limited/reduced though the limit parameter.
3659e689cf4aSJeff Kirsher 	 */
3660e689cf4aSJeff Kirsher 	int rx_channel = rp->rx_channel;
3661e689cf4aSJeff Kirsher 	u32 misc, wred;
3662e689cf4aSJeff Kirsher 
3663e689cf4aSJeff Kirsher 	/* RXMISC (Receive Miscellaneous Discard Count), covers the
3664e689cf4aSJeff Kirsher 	 * following discard events: IPP (Input Port Process),
3665e689cf4aSJeff Kirsher 	 * FFLP/TCAM, Full RCR (Receive Completion Ring) RBR (Receive
3666e689cf4aSJeff Kirsher 	 * Block Ring) prefetch buffer is empty.
3667e689cf4aSJeff Kirsher 	 */
3668e689cf4aSJeff Kirsher 	misc = nr64(RXMISC(rx_channel));
3669e689cf4aSJeff Kirsher 	if (unlikely((misc & RXMISC_COUNT) > limit)) {
3670e689cf4aSJeff Kirsher 		nw64(RXMISC(rx_channel), 0);
3671e689cf4aSJeff Kirsher 		rp->rx_errors += misc & RXMISC_COUNT;
3672e689cf4aSJeff Kirsher 
3673e689cf4aSJeff Kirsher 		if (unlikely(misc & RXMISC_OFLOW))
3674e689cf4aSJeff Kirsher 			dev_err(np->device, "rx-%d: Counter overflow RXMISC discard\n",
3675e689cf4aSJeff Kirsher 				rx_channel);
3676e689cf4aSJeff Kirsher 
3677e689cf4aSJeff Kirsher 		netif_printk(np, rx_err, KERN_DEBUG, np->dev,
3678e689cf4aSJeff Kirsher 			     "rx-%d: MISC drop=%u over=%u\n",
3679e689cf4aSJeff Kirsher 			     rx_channel, misc, misc-limit);
3680e689cf4aSJeff Kirsher 	}
3681e689cf4aSJeff Kirsher 
3682e689cf4aSJeff Kirsher 	/* WRED (Weighted Random Early Discard) by hardware */
3683e689cf4aSJeff Kirsher 	wred = nr64(RED_DIS_CNT(rx_channel));
3684e689cf4aSJeff Kirsher 	if (unlikely((wred & RED_DIS_CNT_COUNT) > limit)) {
3685e689cf4aSJeff Kirsher 		nw64(RED_DIS_CNT(rx_channel), 0);
3686e689cf4aSJeff Kirsher 		rp->rx_dropped += wred & RED_DIS_CNT_COUNT;
3687e689cf4aSJeff Kirsher 
3688e689cf4aSJeff Kirsher 		if (unlikely(wred & RED_DIS_CNT_OFLOW))
3689e689cf4aSJeff Kirsher 			dev_err(np->device, "rx-%d: Counter overflow WRED discard\n", rx_channel);
3690e689cf4aSJeff Kirsher 
3691e689cf4aSJeff Kirsher 		netif_printk(np, rx_err, KERN_DEBUG, np->dev,
3692e689cf4aSJeff Kirsher 			     "rx-%d: WRED drop=%u over=%u\n",
3693e689cf4aSJeff Kirsher 			     rx_channel, wred, wred-limit);
3694e689cf4aSJeff Kirsher 	}
3695e689cf4aSJeff Kirsher }
3696e689cf4aSJeff Kirsher 
niu_rx_work(struct napi_struct * napi,struct niu * np,struct rx_ring_info * rp,int budget)3697e689cf4aSJeff Kirsher static int niu_rx_work(struct napi_struct *napi, struct niu *np,
3698e689cf4aSJeff Kirsher 		       struct rx_ring_info *rp, int budget)
3699e689cf4aSJeff Kirsher {
3700e689cf4aSJeff Kirsher 	int qlen, rcr_done = 0, work_done = 0;
3701e689cf4aSJeff Kirsher 	struct rxdma_mailbox *mbox = rp->mbox;
3702e689cf4aSJeff Kirsher 	u64 stat;
3703e689cf4aSJeff Kirsher 
3704e689cf4aSJeff Kirsher #if 1
3705e689cf4aSJeff Kirsher 	stat = nr64(RX_DMA_CTL_STAT(rp->rx_channel));
3706e689cf4aSJeff Kirsher 	qlen = nr64(RCRSTAT_A(rp->rx_channel)) & RCRSTAT_A_QLEN;
3707e689cf4aSJeff Kirsher #else
3708e689cf4aSJeff Kirsher 	stat = le64_to_cpup(&mbox->rx_dma_ctl_stat);
3709e689cf4aSJeff Kirsher 	qlen = (le64_to_cpup(&mbox->rcrstat_a) & RCRSTAT_A_QLEN);
3710e689cf4aSJeff Kirsher #endif
3711e689cf4aSJeff Kirsher 	mbox->rx_dma_ctl_stat = 0;
3712e689cf4aSJeff Kirsher 	mbox->rcrstat_a = 0;
3713e689cf4aSJeff Kirsher 
3714e689cf4aSJeff Kirsher 	netif_printk(np, rx_status, KERN_DEBUG, np->dev,
3715e689cf4aSJeff Kirsher 		     "%s(chan[%d]), stat[%llx] qlen=%d\n",
3716e689cf4aSJeff Kirsher 		     __func__, rp->rx_channel, (unsigned long long)stat, qlen);
3717e689cf4aSJeff Kirsher 
3718e689cf4aSJeff Kirsher 	rcr_done = work_done = 0;
3719e689cf4aSJeff Kirsher 	qlen = min(qlen, budget);
3720e689cf4aSJeff Kirsher 	while (work_done < qlen) {
3721e689cf4aSJeff Kirsher 		rcr_done += niu_process_rx_pkt(napi, np, rp);
3722e689cf4aSJeff Kirsher 		work_done++;
3723e689cf4aSJeff Kirsher 	}
3724e689cf4aSJeff Kirsher 
3725e689cf4aSJeff Kirsher 	if (rp->rbr_refill_pending >= rp->rbr_kick_thresh) {
3726e689cf4aSJeff Kirsher 		unsigned int i;
3727e689cf4aSJeff Kirsher 
3728e689cf4aSJeff Kirsher 		for (i = 0; i < rp->rbr_refill_pending; i++)
3729e689cf4aSJeff Kirsher 			niu_rbr_refill(np, rp, GFP_ATOMIC);
3730e689cf4aSJeff Kirsher 		rp->rbr_refill_pending = 0;
3731e689cf4aSJeff Kirsher 	}
3732e689cf4aSJeff Kirsher 
3733e689cf4aSJeff Kirsher 	stat = (RX_DMA_CTL_STAT_MEX |
3734e689cf4aSJeff Kirsher 		((u64)work_done << RX_DMA_CTL_STAT_PKTREAD_SHIFT) |
3735e689cf4aSJeff Kirsher 		((u64)rcr_done << RX_DMA_CTL_STAT_PTRREAD_SHIFT));
3736e689cf4aSJeff Kirsher 
3737e689cf4aSJeff Kirsher 	nw64(RX_DMA_CTL_STAT(rp->rx_channel), stat);
3738e689cf4aSJeff Kirsher 
3739e689cf4aSJeff Kirsher 	/* Only sync discards stats when qlen indicate potential for drops */
3740e689cf4aSJeff Kirsher 	if (qlen > 10)
3741e689cf4aSJeff Kirsher 		niu_sync_rx_discard_stats(np, rp, 0x7FFF);
3742e689cf4aSJeff Kirsher 
3743e689cf4aSJeff Kirsher 	return work_done;
3744e689cf4aSJeff Kirsher }
3745e689cf4aSJeff Kirsher 
niu_poll_core(struct niu * np,struct niu_ldg * lp,int budget)3746e689cf4aSJeff Kirsher static int niu_poll_core(struct niu *np, struct niu_ldg *lp, int budget)
3747e689cf4aSJeff Kirsher {
3748e689cf4aSJeff Kirsher 	u64 v0 = lp->v0;
3749e689cf4aSJeff Kirsher 	u32 tx_vec = (v0 >> 32);
3750e689cf4aSJeff Kirsher 	u32 rx_vec = (v0 & 0xffffffff);
3751e689cf4aSJeff Kirsher 	int i, work_done = 0;
3752e689cf4aSJeff Kirsher 
3753e689cf4aSJeff Kirsher 	netif_printk(np, intr, KERN_DEBUG, np->dev,
3754e689cf4aSJeff Kirsher 		     "%s() v0[%016llx]\n", __func__, (unsigned long long)v0);
3755e689cf4aSJeff Kirsher 
3756e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_tx_rings; i++) {
3757e689cf4aSJeff Kirsher 		struct tx_ring_info *rp = &np->tx_rings[i];
3758e689cf4aSJeff Kirsher 		if (tx_vec & (1 << rp->tx_channel))
3759e689cf4aSJeff Kirsher 			niu_tx_work(np, rp);
3760e689cf4aSJeff Kirsher 		nw64(LD_IM0(LDN_TXDMA(rp->tx_channel)), 0);
3761e689cf4aSJeff Kirsher 	}
3762e689cf4aSJeff Kirsher 
3763e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_rx_rings; i++) {
3764e689cf4aSJeff Kirsher 		struct rx_ring_info *rp = &np->rx_rings[i];
3765e689cf4aSJeff Kirsher 
3766e689cf4aSJeff Kirsher 		if (rx_vec & (1 << rp->rx_channel)) {
3767e689cf4aSJeff Kirsher 			int this_work_done;
3768e689cf4aSJeff Kirsher 
3769e689cf4aSJeff Kirsher 			this_work_done = niu_rx_work(&lp->napi, np, rp,
3770e689cf4aSJeff Kirsher 						     budget);
3771e689cf4aSJeff Kirsher 
3772e689cf4aSJeff Kirsher 			budget -= this_work_done;
3773e689cf4aSJeff Kirsher 			work_done += this_work_done;
3774e689cf4aSJeff Kirsher 		}
3775e689cf4aSJeff Kirsher 		nw64(LD_IM0(LDN_RXDMA(rp->rx_channel)), 0);
3776e689cf4aSJeff Kirsher 	}
3777e689cf4aSJeff Kirsher 
3778e689cf4aSJeff Kirsher 	return work_done;
3779e689cf4aSJeff Kirsher }
3780e689cf4aSJeff Kirsher 
niu_poll(struct napi_struct * napi,int budget)3781e689cf4aSJeff Kirsher static int niu_poll(struct napi_struct *napi, int budget)
3782e689cf4aSJeff Kirsher {
3783e689cf4aSJeff Kirsher 	struct niu_ldg *lp = container_of(napi, struct niu_ldg, napi);
3784e689cf4aSJeff Kirsher 	struct niu *np = lp->np;
3785e689cf4aSJeff Kirsher 	int work_done;
3786e689cf4aSJeff Kirsher 
3787e689cf4aSJeff Kirsher 	work_done = niu_poll_core(np, lp, budget);
3788e689cf4aSJeff Kirsher 
3789e689cf4aSJeff Kirsher 	if (work_done < budget) {
37906ad20165SEric Dumazet 		napi_complete_done(napi, work_done);
3791e689cf4aSJeff Kirsher 		niu_ldg_rearm(np, lp, 1);
3792e689cf4aSJeff Kirsher 	}
3793e689cf4aSJeff Kirsher 	return work_done;
3794e689cf4aSJeff Kirsher }
3795e689cf4aSJeff Kirsher 
niu_log_rxchan_errors(struct niu * np,struct rx_ring_info * rp,u64 stat)3796e689cf4aSJeff Kirsher static void niu_log_rxchan_errors(struct niu *np, struct rx_ring_info *rp,
3797e689cf4aSJeff Kirsher 				  u64 stat)
3798e689cf4aSJeff Kirsher {
3799e689cf4aSJeff Kirsher 	netdev_err(np->dev, "RX channel %u errors ( ", rp->rx_channel);
3800e689cf4aSJeff Kirsher 
3801e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RBR_TMOUT)
3802e689cf4aSJeff Kirsher 		pr_cont("RBR_TMOUT ");
3803e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RSP_CNT_ERR)
3804e689cf4aSJeff Kirsher 		pr_cont("RSP_CNT ");
3805e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_BYTE_EN_BUS)
3806e689cf4aSJeff Kirsher 		pr_cont("BYTE_EN_BUS ");
3807e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RSP_DAT_ERR)
3808e689cf4aSJeff Kirsher 		pr_cont("RSP_DAT ");
3809e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RCR_ACK_ERR)
3810e689cf4aSJeff Kirsher 		pr_cont("RCR_ACK ");
3811e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RCR_SHA_PAR)
3812e689cf4aSJeff Kirsher 		pr_cont("RCR_SHA_PAR ");
3813e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RBR_PRE_PAR)
3814e689cf4aSJeff Kirsher 		pr_cont("RBR_PRE_PAR ");
3815e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_CONFIG_ERR)
3816e689cf4aSJeff Kirsher 		pr_cont("CONFIG ");
3817e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RCRINCON)
3818e689cf4aSJeff Kirsher 		pr_cont("RCRINCON ");
3819e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RCRFULL)
3820e689cf4aSJeff Kirsher 		pr_cont("RCRFULL ");
3821e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RBRFULL)
3822e689cf4aSJeff Kirsher 		pr_cont("RBRFULL ");
3823e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_RBRLOGPAGE)
3824e689cf4aSJeff Kirsher 		pr_cont("RBRLOGPAGE ");
3825e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_CFIGLOGPAGE)
3826e689cf4aSJeff Kirsher 		pr_cont("CFIGLOGPAGE ");
3827e689cf4aSJeff Kirsher 	if (stat & RX_DMA_CTL_STAT_DC_FIFO_ERR)
3828e689cf4aSJeff Kirsher 		pr_cont("DC_FIDO ");
3829e689cf4aSJeff Kirsher 
3830e689cf4aSJeff Kirsher 	pr_cont(")\n");
3831e689cf4aSJeff Kirsher }
3832e689cf4aSJeff Kirsher 
niu_rx_error(struct niu * np,struct rx_ring_info * rp)3833e689cf4aSJeff Kirsher static int niu_rx_error(struct niu *np, struct rx_ring_info *rp)
3834e689cf4aSJeff Kirsher {
3835e689cf4aSJeff Kirsher 	u64 stat = nr64(RX_DMA_CTL_STAT(rp->rx_channel));
3836e689cf4aSJeff Kirsher 	int err = 0;
3837e689cf4aSJeff Kirsher 
3838e689cf4aSJeff Kirsher 
3839e689cf4aSJeff Kirsher 	if (stat & (RX_DMA_CTL_STAT_CHAN_FATAL |
3840e689cf4aSJeff Kirsher 		    RX_DMA_CTL_STAT_PORT_FATAL))
3841e689cf4aSJeff Kirsher 		err = -EINVAL;
3842e689cf4aSJeff Kirsher 
3843e689cf4aSJeff Kirsher 	if (err) {
3844e689cf4aSJeff Kirsher 		netdev_err(np->dev, "RX channel %u error, stat[%llx]\n",
3845e689cf4aSJeff Kirsher 			   rp->rx_channel,
3846e689cf4aSJeff Kirsher 			   (unsigned long long) stat);
3847e689cf4aSJeff Kirsher 
3848e689cf4aSJeff Kirsher 		niu_log_rxchan_errors(np, rp, stat);
3849e689cf4aSJeff Kirsher 	}
3850e689cf4aSJeff Kirsher 
3851e689cf4aSJeff Kirsher 	nw64(RX_DMA_CTL_STAT(rp->rx_channel),
3852e689cf4aSJeff Kirsher 	     stat & RX_DMA_CTL_WRITE_CLEAR_ERRS);
3853e689cf4aSJeff Kirsher 
3854e689cf4aSJeff Kirsher 	return err;
3855e689cf4aSJeff Kirsher }
3856e689cf4aSJeff Kirsher 
niu_log_txchan_errors(struct niu * np,struct tx_ring_info * rp,u64 cs)3857e689cf4aSJeff Kirsher static void niu_log_txchan_errors(struct niu *np, struct tx_ring_info *rp,
3858e689cf4aSJeff Kirsher 				  u64 cs)
3859e689cf4aSJeff Kirsher {
3860e689cf4aSJeff Kirsher 	netdev_err(np->dev, "TX channel %u errors ( ", rp->tx_channel);
3861e689cf4aSJeff Kirsher 
3862e689cf4aSJeff Kirsher 	if (cs & TX_CS_MBOX_ERR)
3863e689cf4aSJeff Kirsher 		pr_cont("MBOX ");
3864e689cf4aSJeff Kirsher 	if (cs & TX_CS_PKT_SIZE_ERR)
3865e689cf4aSJeff Kirsher 		pr_cont("PKT_SIZE ");
3866e689cf4aSJeff Kirsher 	if (cs & TX_CS_TX_RING_OFLOW)
3867e689cf4aSJeff Kirsher 		pr_cont("TX_RING_OFLOW ");
3868e689cf4aSJeff Kirsher 	if (cs & TX_CS_PREF_BUF_PAR_ERR)
3869e689cf4aSJeff Kirsher 		pr_cont("PREF_BUF_PAR ");
3870e689cf4aSJeff Kirsher 	if (cs & TX_CS_NACK_PREF)
3871e689cf4aSJeff Kirsher 		pr_cont("NACK_PREF ");
3872e689cf4aSJeff Kirsher 	if (cs & TX_CS_NACK_PKT_RD)
3873e689cf4aSJeff Kirsher 		pr_cont("NACK_PKT_RD ");
3874e689cf4aSJeff Kirsher 	if (cs & TX_CS_CONF_PART_ERR)
3875e689cf4aSJeff Kirsher 		pr_cont("CONF_PART ");
3876e689cf4aSJeff Kirsher 	if (cs & TX_CS_PKT_PRT_ERR)
3877e689cf4aSJeff Kirsher 		pr_cont("PKT_PTR ");
3878e689cf4aSJeff Kirsher 
3879e689cf4aSJeff Kirsher 	pr_cont(")\n");
3880e689cf4aSJeff Kirsher }
3881e689cf4aSJeff Kirsher 
niu_tx_error(struct niu * np,struct tx_ring_info * rp)3882e689cf4aSJeff Kirsher static int niu_tx_error(struct niu *np, struct tx_ring_info *rp)
3883e689cf4aSJeff Kirsher {
3884e689cf4aSJeff Kirsher 	u64 cs, logh, logl;
3885e689cf4aSJeff Kirsher 
3886e689cf4aSJeff Kirsher 	cs = nr64(TX_CS(rp->tx_channel));
3887e689cf4aSJeff Kirsher 	logh = nr64(TX_RNG_ERR_LOGH(rp->tx_channel));
3888e689cf4aSJeff Kirsher 	logl = nr64(TX_RNG_ERR_LOGL(rp->tx_channel));
3889e689cf4aSJeff Kirsher 
3890e689cf4aSJeff Kirsher 	netdev_err(np->dev, "TX channel %u error, cs[%llx] logh[%llx] logl[%llx]\n",
3891e689cf4aSJeff Kirsher 		   rp->tx_channel,
3892e689cf4aSJeff Kirsher 		   (unsigned long long)cs,
3893e689cf4aSJeff Kirsher 		   (unsigned long long)logh,
3894e689cf4aSJeff Kirsher 		   (unsigned long long)logl);
3895e689cf4aSJeff Kirsher 
3896e689cf4aSJeff Kirsher 	niu_log_txchan_errors(np, rp, cs);
3897e689cf4aSJeff Kirsher 
3898e689cf4aSJeff Kirsher 	return -ENODEV;
3899e689cf4aSJeff Kirsher }
3900e689cf4aSJeff Kirsher 
niu_mif_interrupt(struct niu * np)3901e689cf4aSJeff Kirsher static int niu_mif_interrupt(struct niu *np)
3902e689cf4aSJeff Kirsher {
3903e689cf4aSJeff Kirsher 	u64 mif_status = nr64(MIF_STATUS);
3904e689cf4aSJeff Kirsher 	int phy_mdint = 0;
3905e689cf4aSJeff Kirsher 
3906e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC) {
3907e689cf4aSJeff Kirsher 		u64 xrxmac_stat = nr64_mac(XRXMAC_STATUS);
3908e689cf4aSJeff Kirsher 
3909e689cf4aSJeff Kirsher 		if (xrxmac_stat & XRXMAC_STATUS_PHY_MDINT)
3910e689cf4aSJeff Kirsher 			phy_mdint = 1;
3911e689cf4aSJeff Kirsher 	}
3912e689cf4aSJeff Kirsher 
3913e689cf4aSJeff Kirsher 	netdev_err(np->dev, "MIF interrupt, stat[%llx] phy_mdint(%d)\n",
3914e689cf4aSJeff Kirsher 		   (unsigned long long)mif_status, phy_mdint);
3915e689cf4aSJeff Kirsher 
3916e689cf4aSJeff Kirsher 	return -ENODEV;
3917e689cf4aSJeff Kirsher }
3918e689cf4aSJeff Kirsher 
niu_xmac_interrupt(struct niu * np)3919e689cf4aSJeff Kirsher static void niu_xmac_interrupt(struct niu *np)
3920e689cf4aSJeff Kirsher {
3921e689cf4aSJeff Kirsher 	struct niu_xmac_stats *mp = &np->mac_stats.xmac;
3922e689cf4aSJeff Kirsher 	u64 val;
3923e689cf4aSJeff Kirsher 
3924e689cf4aSJeff Kirsher 	val = nr64_mac(XTXMAC_STATUS);
3925e689cf4aSJeff Kirsher 	if (val & XTXMAC_STATUS_FRAME_CNT_EXP)
3926e689cf4aSJeff Kirsher 		mp->tx_frames += TXMAC_FRM_CNT_COUNT;
3927e689cf4aSJeff Kirsher 	if (val & XTXMAC_STATUS_BYTE_CNT_EXP)
3928e689cf4aSJeff Kirsher 		mp->tx_bytes += TXMAC_BYTE_CNT_COUNT;
3929e689cf4aSJeff Kirsher 	if (val & XTXMAC_STATUS_TXFIFO_XFR_ERR)
3930e689cf4aSJeff Kirsher 		mp->tx_fifo_errors++;
3931e689cf4aSJeff Kirsher 	if (val & XTXMAC_STATUS_TXMAC_OFLOW)
3932e689cf4aSJeff Kirsher 		mp->tx_overflow_errors++;
3933e689cf4aSJeff Kirsher 	if (val & XTXMAC_STATUS_MAX_PSIZE_ERR)
3934e689cf4aSJeff Kirsher 		mp->tx_max_pkt_size_errors++;
3935e689cf4aSJeff Kirsher 	if (val & XTXMAC_STATUS_TXMAC_UFLOW)
3936e689cf4aSJeff Kirsher 		mp->tx_underflow_errors++;
3937e689cf4aSJeff Kirsher 
3938e689cf4aSJeff Kirsher 	val = nr64_mac(XRXMAC_STATUS);
3939e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_LCL_FLT_STATUS)
3940e689cf4aSJeff Kirsher 		mp->rx_local_faults++;
3941e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RFLT_DET)
3942e689cf4aSJeff Kirsher 		mp->rx_remote_faults++;
3943e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_LFLT_CNT_EXP)
3944e689cf4aSJeff Kirsher 		mp->rx_link_faults += LINK_FAULT_CNT_COUNT;
3945e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_ALIGNERR_CNT_EXP)
3946e689cf4aSJeff Kirsher 		mp->rx_align_errors += RXMAC_ALIGN_ERR_CNT_COUNT;
3947e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXFRAG_CNT_EXP)
3948e689cf4aSJeff Kirsher 		mp->rx_frags += RXMAC_FRAG_CNT_COUNT;
3949e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXMULTF_CNT_EXP)
3950e689cf4aSJeff Kirsher 		mp->rx_mcasts += RXMAC_MC_FRM_CNT_COUNT;
3951e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXBCAST_CNT_EXP)
3952e689cf4aSJeff Kirsher 		mp->rx_bcasts += RXMAC_BC_FRM_CNT_COUNT;
3953e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXHIST1_CNT_EXP)
3954e689cf4aSJeff Kirsher 		mp->rx_hist_cnt1 += RXMAC_HIST_CNT1_COUNT;
3955e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXHIST2_CNT_EXP)
3956e689cf4aSJeff Kirsher 		mp->rx_hist_cnt2 += RXMAC_HIST_CNT2_COUNT;
3957e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXHIST3_CNT_EXP)
3958e689cf4aSJeff Kirsher 		mp->rx_hist_cnt3 += RXMAC_HIST_CNT3_COUNT;
3959e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXHIST4_CNT_EXP)
3960e689cf4aSJeff Kirsher 		mp->rx_hist_cnt4 += RXMAC_HIST_CNT4_COUNT;
3961e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXHIST5_CNT_EXP)
3962e689cf4aSJeff Kirsher 		mp->rx_hist_cnt5 += RXMAC_HIST_CNT5_COUNT;
3963e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXHIST6_CNT_EXP)
3964e689cf4aSJeff Kirsher 		mp->rx_hist_cnt6 += RXMAC_HIST_CNT6_COUNT;
3965e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXHIST7_CNT_EXP)
3966e689cf4aSJeff Kirsher 		mp->rx_hist_cnt7 += RXMAC_HIST_CNT7_COUNT;
3967e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXOCTET_CNT_EXP)
3968e689cf4aSJeff Kirsher 		mp->rx_octets += RXMAC_BT_CNT_COUNT;
3969e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_CVIOLERR_CNT_EXP)
3970e689cf4aSJeff Kirsher 		mp->rx_code_violations += RXMAC_CD_VIO_CNT_COUNT;
3971e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_LENERR_CNT_EXP)
3972e689cf4aSJeff Kirsher 		mp->rx_len_errors += RXMAC_MPSZER_CNT_COUNT;
3973e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_CRCERR_CNT_EXP)
3974e689cf4aSJeff Kirsher 		mp->rx_crc_errors += RXMAC_CRC_ER_CNT_COUNT;
3975e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXUFLOW)
3976e689cf4aSJeff Kirsher 		mp->rx_underflows++;
3977e689cf4aSJeff Kirsher 	if (val & XRXMAC_STATUS_RXOFLOW)
3978e689cf4aSJeff Kirsher 		mp->rx_overflows++;
3979e689cf4aSJeff Kirsher 
3980e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_FC_STAT);
3981e689cf4aSJeff Kirsher 	if (val & XMAC_FC_STAT_TX_MAC_NPAUSE)
3982e689cf4aSJeff Kirsher 		mp->pause_off_state++;
3983e689cf4aSJeff Kirsher 	if (val & XMAC_FC_STAT_TX_MAC_PAUSE)
3984e689cf4aSJeff Kirsher 		mp->pause_on_state++;
3985e689cf4aSJeff Kirsher 	if (val & XMAC_FC_STAT_RX_MAC_RPAUSE)
3986e689cf4aSJeff Kirsher 		mp->pause_received++;
3987e689cf4aSJeff Kirsher }
3988e689cf4aSJeff Kirsher 
niu_bmac_interrupt(struct niu * np)3989e689cf4aSJeff Kirsher static void niu_bmac_interrupt(struct niu *np)
3990e689cf4aSJeff Kirsher {
3991e689cf4aSJeff Kirsher 	struct niu_bmac_stats *mp = &np->mac_stats.bmac;
3992e689cf4aSJeff Kirsher 	u64 val;
3993e689cf4aSJeff Kirsher 
3994e689cf4aSJeff Kirsher 	val = nr64_mac(BTXMAC_STATUS);
3995e689cf4aSJeff Kirsher 	if (val & BTXMAC_STATUS_UNDERRUN)
3996e689cf4aSJeff Kirsher 		mp->tx_underflow_errors++;
3997e689cf4aSJeff Kirsher 	if (val & BTXMAC_STATUS_MAX_PKT_ERR)
3998e689cf4aSJeff Kirsher 		mp->tx_max_pkt_size_errors++;
3999e689cf4aSJeff Kirsher 	if (val & BTXMAC_STATUS_BYTE_CNT_EXP)
4000e689cf4aSJeff Kirsher 		mp->tx_bytes += BTXMAC_BYTE_CNT_COUNT;
4001e689cf4aSJeff Kirsher 	if (val & BTXMAC_STATUS_FRAME_CNT_EXP)
4002e689cf4aSJeff Kirsher 		mp->tx_frames += BTXMAC_FRM_CNT_COUNT;
4003e689cf4aSJeff Kirsher 
4004e689cf4aSJeff Kirsher 	val = nr64_mac(BRXMAC_STATUS);
4005e689cf4aSJeff Kirsher 	if (val & BRXMAC_STATUS_OVERFLOW)
4006e689cf4aSJeff Kirsher 		mp->rx_overflows++;
4007e689cf4aSJeff Kirsher 	if (val & BRXMAC_STATUS_FRAME_CNT_EXP)
4008e689cf4aSJeff Kirsher 		mp->rx_frames += BRXMAC_FRAME_CNT_COUNT;
4009e689cf4aSJeff Kirsher 	if (val & BRXMAC_STATUS_ALIGN_ERR_EXP)
4010e689cf4aSJeff Kirsher 		mp->rx_align_errors += BRXMAC_ALIGN_ERR_CNT_COUNT;
4011e689cf4aSJeff Kirsher 	if (val & BRXMAC_STATUS_CRC_ERR_EXP)
4012e689cf4aSJeff Kirsher 		mp->rx_crc_errors += BRXMAC_ALIGN_ERR_CNT_COUNT;
4013e689cf4aSJeff Kirsher 	if (val & BRXMAC_STATUS_LEN_ERR_EXP)
4014e689cf4aSJeff Kirsher 		mp->rx_len_errors += BRXMAC_CODE_VIOL_ERR_CNT_COUNT;
4015e689cf4aSJeff Kirsher 
4016e689cf4aSJeff Kirsher 	val = nr64_mac(BMAC_CTRL_STATUS);
4017e689cf4aSJeff Kirsher 	if (val & BMAC_CTRL_STATUS_NOPAUSE)
4018e689cf4aSJeff Kirsher 		mp->pause_off_state++;
4019e689cf4aSJeff Kirsher 	if (val & BMAC_CTRL_STATUS_PAUSE)
4020e689cf4aSJeff Kirsher 		mp->pause_on_state++;
4021e689cf4aSJeff Kirsher 	if (val & BMAC_CTRL_STATUS_PAUSE_RECV)
4022e689cf4aSJeff Kirsher 		mp->pause_received++;
4023e689cf4aSJeff Kirsher }
4024e689cf4aSJeff Kirsher 
niu_mac_interrupt(struct niu * np)4025e689cf4aSJeff Kirsher static int niu_mac_interrupt(struct niu *np)
4026e689cf4aSJeff Kirsher {
4027e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
4028e689cf4aSJeff Kirsher 		niu_xmac_interrupt(np);
4029e689cf4aSJeff Kirsher 	else
4030e689cf4aSJeff Kirsher 		niu_bmac_interrupt(np);
4031e689cf4aSJeff Kirsher 
4032e689cf4aSJeff Kirsher 	return 0;
4033e689cf4aSJeff Kirsher }
4034e689cf4aSJeff Kirsher 
niu_log_device_error(struct niu * np,u64 stat)4035e689cf4aSJeff Kirsher static void niu_log_device_error(struct niu *np, u64 stat)
4036e689cf4aSJeff Kirsher {
4037e689cf4aSJeff Kirsher 	netdev_err(np->dev, "Core device errors ( ");
4038e689cf4aSJeff Kirsher 
4039e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_META2)
4040e689cf4aSJeff Kirsher 		pr_cont("META2 ");
4041e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_META1)
4042e689cf4aSJeff Kirsher 		pr_cont("META1 ");
4043e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_PEU)
4044e689cf4aSJeff Kirsher 		pr_cont("PEU ");
4045e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_TXC)
4046e689cf4aSJeff Kirsher 		pr_cont("TXC ");
4047e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_RDMC)
4048e689cf4aSJeff Kirsher 		pr_cont("RDMC ");
4049e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_TDMC)
4050e689cf4aSJeff Kirsher 		pr_cont("TDMC ");
4051e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_ZCP)
4052e689cf4aSJeff Kirsher 		pr_cont("ZCP ");
4053e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_FFLP)
4054e689cf4aSJeff Kirsher 		pr_cont("FFLP ");
4055e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_IPP)
4056e689cf4aSJeff Kirsher 		pr_cont("IPP ");
4057e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_MAC)
4058e689cf4aSJeff Kirsher 		pr_cont("MAC ");
4059e689cf4aSJeff Kirsher 	if (stat & SYS_ERR_MASK_SMX)
4060e689cf4aSJeff Kirsher 		pr_cont("SMX ");
4061e689cf4aSJeff Kirsher 
4062e689cf4aSJeff Kirsher 	pr_cont(")\n");
4063e689cf4aSJeff Kirsher }
4064e689cf4aSJeff Kirsher 
niu_device_error(struct niu * np)4065e689cf4aSJeff Kirsher static int niu_device_error(struct niu *np)
4066e689cf4aSJeff Kirsher {
4067e689cf4aSJeff Kirsher 	u64 stat = nr64(SYS_ERR_STAT);
4068e689cf4aSJeff Kirsher 
4069e689cf4aSJeff Kirsher 	netdev_err(np->dev, "Core device error, stat[%llx]\n",
4070e689cf4aSJeff Kirsher 		   (unsigned long long)stat);
4071e689cf4aSJeff Kirsher 
4072e689cf4aSJeff Kirsher 	niu_log_device_error(np, stat);
4073e689cf4aSJeff Kirsher 
4074e689cf4aSJeff Kirsher 	return -ENODEV;
4075e689cf4aSJeff Kirsher }
4076e689cf4aSJeff Kirsher 
niu_slowpath_interrupt(struct niu * np,struct niu_ldg * lp,u64 v0,u64 v1,u64 v2)4077e689cf4aSJeff Kirsher static int niu_slowpath_interrupt(struct niu *np, struct niu_ldg *lp,
4078e689cf4aSJeff Kirsher 			      u64 v0, u64 v1, u64 v2)
4079e689cf4aSJeff Kirsher {
4080e689cf4aSJeff Kirsher 
4081e689cf4aSJeff Kirsher 	int i, err = 0;
4082e689cf4aSJeff Kirsher 
4083e689cf4aSJeff Kirsher 	lp->v0 = v0;
4084e689cf4aSJeff Kirsher 	lp->v1 = v1;
4085e689cf4aSJeff Kirsher 	lp->v2 = v2;
4086e689cf4aSJeff Kirsher 
4087e689cf4aSJeff Kirsher 	if (v1 & 0x00000000ffffffffULL) {
4088e689cf4aSJeff Kirsher 		u32 rx_vec = (v1 & 0xffffffff);
4089e689cf4aSJeff Kirsher 
4090e689cf4aSJeff Kirsher 		for (i = 0; i < np->num_rx_rings; i++) {
4091e689cf4aSJeff Kirsher 			struct rx_ring_info *rp = &np->rx_rings[i];
4092e689cf4aSJeff Kirsher 
4093e689cf4aSJeff Kirsher 			if (rx_vec & (1 << rp->rx_channel)) {
4094e689cf4aSJeff Kirsher 				int r = niu_rx_error(np, rp);
4095e689cf4aSJeff Kirsher 				if (r) {
4096e689cf4aSJeff Kirsher 					err = r;
4097e689cf4aSJeff Kirsher 				} else {
4098e689cf4aSJeff Kirsher 					if (!v0)
4099e689cf4aSJeff Kirsher 						nw64(RX_DMA_CTL_STAT(rp->rx_channel),
4100e689cf4aSJeff Kirsher 						     RX_DMA_CTL_STAT_MEX);
4101e689cf4aSJeff Kirsher 				}
4102e689cf4aSJeff Kirsher 			}
4103e689cf4aSJeff Kirsher 		}
4104e689cf4aSJeff Kirsher 	}
4105e689cf4aSJeff Kirsher 	if (v1 & 0x7fffffff00000000ULL) {
4106e689cf4aSJeff Kirsher 		u32 tx_vec = (v1 >> 32) & 0x7fffffff;
4107e689cf4aSJeff Kirsher 
4108e689cf4aSJeff Kirsher 		for (i = 0; i < np->num_tx_rings; i++) {
4109e689cf4aSJeff Kirsher 			struct tx_ring_info *rp = &np->tx_rings[i];
4110e689cf4aSJeff Kirsher 
4111e689cf4aSJeff Kirsher 			if (tx_vec & (1 << rp->tx_channel)) {
4112e689cf4aSJeff Kirsher 				int r = niu_tx_error(np, rp);
4113e689cf4aSJeff Kirsher 				if (r)
4114e689cf4aSJeff Kirsher 					err = r;
4115e689cf4aSJeff Kirsher 			}
4116e689cf4aSJeff Kirsher 		}
4117e689cf4aSJeff Kirsher 	}
4118e689cf4aSJeff Kirsher 	if ((v0 | v1) & 0x8000000000000000ULL) {
4119e689cf4aSJeff Kirsher 		int r = niu_mif_interrupt(np);
4120e689cf4aSJeff Kirsher 		if (r)
4121e689cf4aSJeff Kirsher 			err = r;
4122e689cf4aSJeff Kirsher 	}
4123e689cf4aSJeff Kirsher 	if (v2) {
4124e689cf4aSJeff Kirsher 		if (v2 & 0x01ef) {
4125e689cf4aSJeff Kirsher 			int r = niu_mac_interrupt(np);
4126e689cf4aSJeff Kirsher 			if (r)
4127e689cf4aSJeff Kirsher 				err = r;
4128e689cf4aSJeff Kirsher 		}
4129e689cf4aSJeff Kirsher 		if (v2 & 0x0210) {
4130e689cf4aSJeff Kirsher 			int r = niu_device_error(np);
4131e689cf4aSJeff Kirsher 			if (r)
4132e689cf4aSJeff Kirsher 				err = r;
4133e689cf4aSJeff Kirsher 		}
4134e689cf4aSJeff Kirsher 	}
4135e689cf4aSJeff Kirsher 
4136e689cf4aSJeff Kirsher 	if (err)
4137e689cf4aSJeff Kirsher 		niu_enable_interrupts(np, 0);
4138e689cf4aSJeff Kirsher 
4139e689cf4aSJeff Kirsher 	return err;
4140e689cf4aSJeff Kirsher }
4141e689cf4aSJeff Kirsher 
niu_rxchan_intr(struct niu * np,struct rx_ring_info * rp,int ldn)4142e689cf4aSJeff Kirsher static void niu_rxchan_intr(struct niu *np, struct rx_ring_info *rp,
4143e689cf4aSJeff Kirsher 			    int ldn)
4144e689cf4aSJeff Kirsher {
4145e689cf4aSJeff Kirsher 	struct rxdma_mailbox *mbox = rp->mbox;
4146e689cf4aSJeff Kirsher 	u64 stat_write, stat = le64_to_cpup(&mbox->rx_dma_ctl_stat);
4147e689cf4aSJeff Kirsher 
4148e689cf4aSJeff Kirsher 	stat_write = (RX_DMA_CTL_STAT_RCRTHRES |
4149e689cf4aSJeff Kirsher 		      RX_DMA_CTL_STAT_RCRTO);
4150e689cf4aSJeff Kirsher 	nw64(RX_DMA_CTL_STAT(rp->rx_channel), stat_write);
4151e689cf4aSJeff Kirsher 
4152e689cf4aSJeff Kirsher 	netif_printk(np, intr, KERN_DEBUG, np->dev,
4153e689cf4aSJeff Kirsher 		     "%s() stat[%llx]\n", __func__, (unsigned long long)stat);
4154e689cf4aSJeff Kirsher }
4155e689cf4aSJeff Kirsher 
niu_txchan_intr(struct niu * np,struct tx_ring_info * rp,int ldn)4156e689cf4aSJeff Kirsher static void niu_txchan_intr(struct niu *np, struct tx_ring_info *rp,
4157e689cf4aSJeff Kirsher 			    int ldn)
4158e689cf4aSJeff Kirsher {
4159e689cf4aSJeff Kirsher 	rp->tx_cs = nr64(TX_CS(rp->tx_channel));
4160e689cf4aSJeff Kirsher 
4161e689cf4aSJeff Kirsher 	netif_printk(np, intr, KERN_DEBUG, np->dev,
4162e689cf4aSJeff Kirsher 		     "%s() cs[%llx]\n", __func__, (unsigned long long)rp->tx_cs);
4163e689cf4aSJeff Kirsher }
4164e689cf4aSJeff Kirsher 
__niu_fastpath_interrupt(struct niu * np,int ldg,u64 v0)4165e689cf4aSJeff Kirsher static void __niu_fastpath_interrupt(struct niu *np, int ldg, u64 v0)
4166e689cf4aSJeff Kirsher {
4167e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
4168e689cf4aSJeff Kirsher 	u32 rx_vec, tx_vec;
4169e689cf4aSJeff Kirsher 	int i;
4170e689cf4aSJeff Kirsher 
4171e689cf4aSJeff Kirsher 	tx_vec = (v0 >> 32);
4172e689cf4aSJeff Kirsher 	rx_vec = (v0 & 0xffffffff);
4173e689cf4aSJeff Kirsher 
4174e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_rx_rings; i++) {
4175e689cf4aSJeff Kirsher 		struct rx_ring_info *rp = &np->rx_rings[i];
4176e689cf4aSJeff Kirsher 		int ldn = LDN_RXDMA(rp->rx_channel);
4177e689cf4aSJeff Kirsher 
4178e689cf4aSJeff Kirsher 		if (parent->ldg_map[ldn] != ldg)
4179e689cf4aSJeff Kirsher 			continue;
4180e689cf4aSJeff Kirsher 
4181e689cf4aSJeff Kirsher 		nw64(LD_IM0(ldn), LD_IM0_MASK);
4182e689cf4aSJeff Kirsher 		if (rx_vec & (1 << rp->rx_channel))
4183e689cf4aSJeff Kirsher 			niu_rxchan_intr(np, rp, ldn);
4184e689cf4aSJeff Kirsher 	}
4185e689cf4aSJeff Kirsher 
4186e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_tx_rings; i++) {
4187e689cf4aSJeff Kirsher 		struct tx_ring_info *rp = &np->tx_rings[i];
4188e689cf4aSJeff Kirsher 		int ldn = LDN_TXDMA(rp->tx_channel);
4189e689cf4aSJeff Kirsher 
4190e689cf4aSJeff Kirsher 		if (parent->ldg_map[ldn] != ldg)
4191e689cf4aSJeff Kirsher 			continue;
4192e689cf4aSJeff Kirsher 
4193e689cf4aSJeff Kirsher 		nw64(LD_IM0(ldn), LD_IM0_MASK);
4194e689cf4aSJeff Kirsher 		if (tx_vec & (1 << rp->tx_channel))
4195e689cf4aSJeff Kirsher 			niu_txchan_intr(np, rp, ldn);
4196e689cf4aSJeff Kirsher 	}
4197e689cf4aSJeff Kirsher }
4198e689cf4aSJeff Kirsher 
niu_schedule_napi(struct niu * np,struct niu_ldg * lp,u64 v0,u64 v1,u64 v2)4199e689cf4aSJeff Kirsher static void niu_schedule_napi(struct niu *np, struct niu_ldg *lp,
4200e689cf4aSJeff Kirsher 			      u64 v0, u64 v1, u64 v2)
4201e689cf4aSJeff Kirsher {
4202e689cf4aSJeff Kirsher 	if (likely(napi_schedule_prep(&lp->napi))) {
4203e689cf4aSJeff Kirsher 		lp->v0 = v0;
4204e689cf4aSJeff Kirsher 		lp->v1 = v1;
4205e689cf4aSJeff Kirsher 		lp->v2 = v2;
4206e689cf4aSJeff Kirsher 		__niu_fastpath_interrupt(np, lp->ldg_num, v0);
4207e689cf4aSJeff Kirsher 		__napi_schedule(&lp->napi);
4208e689cf4aSJeff Kirsher 	}
4209e689cf4aSJeff Kirsher }
4210e689cf4aSJeff Kirsher 
niu_interrupt(int irq,void * dev_id)4211e689cf4aSJeff Kirsher static irqreturn_t niu_interrupt(int irq, void *dev_id)
4212e689cf4aSJeff Kirsher {
4213e689cf4aSJeff Kirsher 	struct niu_ldg *lp = dev_id;
4214e689cf4aSJeff Kirsher 	struct niu *np = lp->np;
4215e689cf4aSJeff Kirsher 	int ldg = lp->ldg_num;
4216e689cf4aSJeff Kirsher 	unsigned long flags;
4217e689cf4aSJeff Kirsher 	u64 v0, v1, v2;
4218e689cf4aSJeff Kirsher 
4219e689cf4aSJeff Kirsher 	if (netif_msg_intr(np))
4220e689cf4aSJeff Kirsher 		printk(KERN_DEBUG KBUILD_MODNAME ": " "%s() ldg[%p](%d)",
4221e689cf4aSJeff Kirsher 		       __func__, lp, ldg);
4222e689cf4aSJeff Kirsher 
4223e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
4224e689cf4aSJeff Kirsher 
4225e689cf4aSJeff Kirsher 	v0 = nr64(LDSV0(ldg));
4226e689cf4aSJeff Kirsher 	v1 = nr64(LDSV1(ldg));
4227e689cf4aSJeff Kirsher 	v2 = nr64(LDSV2(ldg));
4228e689cf4aSJeff Kirsher 
4229e689cf4aSJeff Kirsher 	if (netif_msg_intr(np))
4230e689cf4aSJeff Kirsher 		pr_cont(" v0[%llx] v1[%llx] v2[%llx]\n",
4231e689cf4aSJeff Kirsher 		       (unsigned long long) v0,
4232e689cf4aSJeff Kirsher 		       (unsigned long long) v1,
4233e689cf4aSJeff Kirsher 		       (unsigned long long) v2);
4234e689cf4aSJeff Kirsher 
4235e689cf4aSJeff Kirsher 	if (unlikely(!v0 && !v1 && !v2)) {
4236e689cf4aSJeff Kirsher 		spin_unlock_irqrestore(&np->lock, flags);
4237e689cf4aSJeff Kirsher 		return IRQ_NONE;
4238e689cf4aSJeff Kirsher 	}
4239e689cf4aSJeff Kirsher 
4240e689cf4aSJeff Kirsher 	if (unlikely((v0 & ((u64)1 << LDN_MIF)) || v1 || v2)) {
4241e689cf4aSJeff Kirsher 		int err = niu_slowpath_interrupt(np, lp, v0, v1, v2);
4242e689cf4aSJeff Kirsher 		if (err)
4243e689cf4aSJeff Kirsher 			goto out;
4244e689cf4aSJeff Kirsher 	}
4245e689cf4aSJeff Kirsher 	if (likely(v0 & ~((u64)1 << LDN_MIF)))
4246e689cf4aSJeff Kirsher 		niu_schedule_napi(np, lp, v0, v1, v2);
4247e689cf4aSJeff Kirsher 	else
4248e689cf4aSJeff Kirsher 		niu_ldg_rearm(np, lp, 1);
4249e689cf4aSJeff Kirsher out:
4250e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
4251e689cf4aSJeff Kirsher 
4252e689cf4aSJeff Kirsher 	return IRQ_HANDLED;
4253e689cf4aSJeff Kirsher }
4254e689cf4aSJeff Kirsher 
niu_free_rx_ring_info(struct niu * np,struct rx_ring_info * rp)4255e689cf4aSJeff Kirsher static void niu_free_rx_ring_info(struct niu *np, struct rx_ring_info *rp)
4256e689cf4aSJeff Kirsher {
4257e689cf4aSJeff Kirsher 	if (rp->mbox) {
4258e689cf4aSJeff Kirsher 		np->ops->free_coherent(np->device,
4259e689cf4aSJeff Kirsher 				       sizeof(struct rxdma_mailbox),
4260e689cf4aSJeff Kirsher 				       rp->mbox, rp->mbox_dma);
4261e689cf4aSJeff Kirsher 		rp->mbox = NULL;
4262e689cf4aSJeff Kirsher 	}
4263e689cf4aSJeff Kirsher 	if (rp->rcr) {
4264e689cf4aSJeff Kirsher 		np->ops->free_coherent(np->device,
4265e689cf4aSJeff Kirsher 				       MAX_RCR_RING_SIZE * sizeof(__le64),
4266e689cf4aSJeff Kirsher 				       rp->rcr, rp->rcr_dma);
4267e689cf4aSJeff Kirsher 		rp->rcr = NULL;
4268e689cf4aSJeff Kirsher 		rp->rcr_table_size = 0;
4269e689cf4aSJeff Kirsher 		rp->rcr_index = 0;
4270e689cf4aSJeff Kirsher 	}
4271e689cf4aSJeff Kirsher 	if (rp->rbr) {
4272e689cf4aSJeff Kirsher 		niu_rbr_free(np, rp);
4273e689cf4aSJeff Kirsher 
4274e689cf4aSJeff Kirsher 		np->ops->free_coherent(np->device,
4275e689cf4aSJeff Kirsher 				       MAX_RBR_RING_SIZE * sizeof(__le32),
4276e689cf4aSJeff Kirsher 				       rp->rbr, rp->rbr_dma);
4277e689cf4aSJeff Kirsher 		rp->rbr = NULL;
4278e689cf4aSJeff Kirsher 		rp->rbr_table_size = 0;
4279e689cf4aSJeff Kirsher 		rp->rbr_index = 0;
4280e689cf4aSJeff Kirsher 	}
4281e689cf4aSJeff Kirsher 	kfree(rp->rxhash);
4282e689cf4aSJeff Kirsher 	rp->rxhash = NULL;
4283e689cf4aSJeff Kirsher }
4284e689cf4aSJeff Kirsher 
niu_free_tx_ring_info(struct niu * np,struct tx_ring_info * rp)4285e689cf4aSJeff Kirsher static void niu_free_tx_ring_info(struct niu *np, struct tx_ring_info *rp)
4286e689cf4aSJeff Kirsher {
4287e689cf4aSJeff Kirsher 	if (rp->mbox) {
4288e689cf4aSJeff Kirsher 		np->ops->free_coherent(np->device,
4289e689cf4aSJeff Kirsher 				       sizeof(struct txdma_mailbox),
4290e689cf4aSJeff Kirsher 				       rp->mbox, rp->mbox_dma);
4291e689cf4aSJeff Kirsher 		rp->mbox = NULL;
4292e689cf4aSJeff Kirsher 	}
4293e689cf4aSJeff Kirsher 	if (rp->descr) {
4294e689cf4aSJeff Kirsher 		int i;
4295e689cf4aSJeff Kirsher 
4296e689cf4aSJeff Kirsher 		for (i = 0; i < MAX_TX_RING_SIZE; i++) {
4297e689cf4aSJeff Kirsher 			if (rp->tx_buffs[i].skb)
4298e689cf4aSJeff Kirsher 				(void) release_tx_packet(np, rp, i);
4299e689cf4aSJeff Kirsher 		}
4300e689cf4aSJeff Kirsher 
4301e689cf4aSJeff Kirsher 		np->ops->free_coherent(np->device,
4302e689cf4aSJeff Kirsher 				       MAX_TX_RING_SIZE * sizeof(__le64),
4303e689cf4aSJeff Kirsher 				       rp->descr, rp->descr_dma);
4304e689cf4aSJeff Kirsher 		rp->descr = NULL;
4305e689cf4aSJeff Kirsher 		rp->pending = 0;
4306e689cf4aSJeff Kirsher 		rp->prod = 0;
4307e689cf4aSJeff Kirsher 		rp->cons = 0;
4308e689cf4aSJeff Kirsher 		rp->wrap_bit = 0;
4309e689cf4aSJeff Kirsher 	}
4310e689cf4aSJeff Kirsher }
4311e689cf4aSJeff Kirsher 
niu_free_channels(struct niu * np)4312e689cf4aSJeff Kirsher static void niu_free_channels(struct niu *np)
4313e689cf4aSJeff Kirsher {
4314e689cf4aSJeff Kirsher 	int i;
4315e689cf4aSJeff Kirsher 
4316e689cf4aSJeff Kirsher 	if (np->rx_rings) {
4317e689cf4aSJeff Kirsher 		for (i = 0; i < np->num_rx_rings; i++) {
4318e689cf4aSJeff Kirsher 			struct rx_ring_info *rp = &np->rx_rings[i];
4319e689cf4aSJeff Kirsher 
4320e689cf4aSJeff Kirsher 			niu_free_rx_ring_info(np, rp);
4321e689cf4aSJeff Kirsher 		}
4322e689cf4aSJeff Kirsher 		kfree(np->rx_rings);
4323e689cf4aSJeff Kirsher 		np->rx_rings = NULL;
4324e689cf4aSJeff Kirsher 		np->num_rx_rings = 0;
4325e689cf4aSJeff Kirsher 	}
4326e689cf4aSJeff Kirsher 
4327e689cf4aSJeff Kirsher 	if (np->tx_rings) {
4328e689cf4aSJeff Kirsher 		for (i = 0; i < np->num_tx_rings; i++) {
4329e689cf4aSJeff Kirsher 			struct tx_ring_info *rp = &np->tx_rings[i];
4330e689cf4aSJeff Kirsher 
4331e689cf4aSJeff Kirsher 			niu_free_tx_ring_info(np, rp);
4332e689cf4aSJeff Kirsher 		}
4333e689cf4aSJeff Kirsher 		kfree(np->tx_rings);
4334e689cf4aSJeff Kirsher 		np->tx_rings = NULL;
4335e689cf4aSJeff Kirsher 		np->num_tx_rings = 0;
4336e689cf4aSJeff Kirsher 	}
4337e689cf4aSJeff Kirsher }
4338e689cf4aSJeff Kirsher 
niu_alloc_rx_ring_info(struct niu * np,struct rx_ring_info * rp)4339e689cf4aSJeff Kirsher static int niu_alloc_rx_ring_info(struct niu *np,
4340e689cf4aSJeff Kirsher 				  struct rx_ring_info *rp)
4341e689cf4aSJeff Kirsher {
4342e689cf4aSJeff Kirsher 	BUILD_BUG_ON(sizeof(struct rxdma_mailbox) != 64);
4343e689cf4aSJeff Kirsher 
4344b2adaca9SJoe Perches 	rp->rxhash = kcalloc(MAX_RBR_RING_SIZE, sizeof(struct page *),
4345e689cf4aSJeff Kirsher 			     GFP_KERNEL);
4346e689cf4aSJeff Kirsher 	if (!rp->rxhash)
4347e689cf4aSJeff Kirsher 		return -ENOMEM;
4348e689cf4aSJeff Kirsher 
4349e689cf4aSJeff Kirsher 	rp->mbox = np->ops->alloc_coherent(np->device,
4350e689cf4aSJeff Kirsher 					   sizeof(struct rxdma_mailbox),
4351e689cf4aSJeff Kirsher 					   &rp->mbox_dma, GFP_KERNEL);
4352e689cf4aSJeff Kirsher 	if (!rp->mbox)
4353e689cf4aSJeff Kirsher 		return -ENOMEM;
4354e689cf4aSJeff Kirsher 	if ((unsigned long)rp->mbox & (64UL - 1)) {
4355e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Coherent alloc gives misaligned RXDMA mailbox %p\n",
4356e689cf4aSJeff Kirsher 			   rp->mbox);
4357e689cf4aSJeff Kirsher 		return -EINVAL;
4358e689cf4aSJeff Kirsher 	}
4359e689cf4aSJeff Kirsher 
4360e689cf4aSJeff Kirsher 	rp->rcr = np->ops->alloc_coherent(np->device,
4361e689cf4aSJeff Kirsher 					  MAX_RCR_RING_SIZE * sizeof(__le64),
4362e689cf4aSJeff Kirsher 					  &rp->rcr_dma, GFP_KERNEL);
4363e689cf4aSJeff Kirsher 	if (!rp->rcr)
4364e689cf4aSJeff Kirsher 		return -ENOMEM;
4365e689cf4aSJeff Kirsher 	if ((unsigned long)rp->rcr & (64UL - 1)) {
4366e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Coherent alloc gives misaligned RXDMA RCR table %p\n",
4367e689cf4aSJeff Kirsher 			   rp->rcr);
4368e689cf4aSJeff Kirsher 		return -EINVAL;
4369e689cf4aSJeff Kirsher 	}
4370e689cf4aSJeff Kirsher 	rp->rcr_table_size = MAX_RCR_RING_SIZE;
4371e689cf4aSJeff Kirsher 	rp->rcr_index = 0;
4372e689cf4aSJeff Kirsher 
4373e689cf4aSJeff Kirsher 	rp->rbr = np->ops->alloc_coherent(np->device,
4374e689cf4aSJeff Kirsher 					  MAX_RBR_RING_SIZE * sizeof(__le32),
4375e689cf4aSJeff Kirsher 					  &rp->rbr_dma, GFP_KERNEL);
4376e689cf4aSJeff Kirsher 	if (!rp->rbr)
4377e689cf4aSJeff Kirsher 		return -ENOMEM;
4378e689cf4aSJeff Kirsher 	if ((unsigned long)rp->rbr & (64UL - 1)) {
4379e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Coherent alloc gives misaligned RXDMA RBR table %p\n",
4380e689cf4aSJeff Kirsher 			   rp->rbr);
4381e689cf4aSJeff Kirsher 		return -EINVAL;
4382e689cf4aSJeff Kirsher 	}
4383e689cf4aSJeff Kirsher 	rp->rbr_table_size = MAX_RBR_RING_SIZE;
4384e689cf4aSJeff Kirsher 	rp->rbr_index = 0;
4385e689cf4aSJeff Kirsher 	rp->rbr_pending = 0;
4386e689cf4aSJeff Kirsher 
4387e689cf4aSJeff Kirsher 	return 0;
4388e689cf4aSJeff Kirsher }
4389e689cf4aSJeff Kirsher 
niu_set_max_burst(struct niu * np,struct tx_ring_info * rp)4390e689cf4aSJeff Kirsher static void niu_set_max_burst(struct niu *np, struct tx_ring_info *rp)
4391e689cf4aSJeff Kirsher {
4392e689cf4aSJeff Kirsher 	int mtu = np->dev->mtu;
4393e689cf4aSJeff Kirsher 
4394e689cf4aSJeff Kirsher 	/* These values are recommended by the HW designers for fair
4395e689cf4aSJeff Kirsher 	 * utilization of DRR amongst the rings.
4396e689cf4aSJeff Kirsher 	 */
4397e689cf4aSJeff Kirsher 	rp->max_burst = mtu + 32;
4398e689cf4aSJeff Kirsher 	if (rp->max_burst > 4096)
4399e689cf4aSJeff Kirsher 		rp->max_burst = 4096;
4400e689cf4aSJeff Kirsher }
4401e689cf4aSJeff Kirsher 
niu_alloc_tx_ring_info(struct niu * np,struct tx_ring_info * rp)4402e689cf4aSJeff Kirsher static int niu_alloc_tx_ring_info(struct niu *np,
4403e689cf4aSJeff Kirsher 				  struct tx_ring_info *rp)
4404e689cf4aSJeff Kirsher {
4405e689cf4aSJeff Kirsher 	BUILD_BUG_ON(sizeof(struct txdma_mailbox) != 64);
4406e689cf4aSJeff Kirsher 
4407e689cf4aSJeff Kirsher 	rp->mbox = np->ops->alloc_coherent(np->device,
4408e689cf4aSJeff Kirsher 					   sizeof(struct txdma_mailbox),
4409e689cf4aSJeff Kirsher 					   &rp->mbox_dma, GFP_KERNEL);
4410e689cf4aSJeff Kirsher 	if (!rp->mbox)
4411e689cf4aSJeff Kirsher 		return -ENOMEM;
4412e689cf4aSJeff Kirsher 	if ((unsigned long)rp->mbox & (64UL - 1)) {
4413e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Coherent alloc gives misaligned TXDMA mailbox %p\n",
4414e689cf4aSJeff Kirsher 			   rp->mbox);
4415e689cf4aSJeff Kirsher 		return -EINVAL;
4416e689cf4aSJeff Kirsher 	}
4417e689cf4aSJeff Kirsher 
4418e689cf4aSJeff Kirsher 	rp->descr = np->ops->alloc_coherent(np->device,
4419e689cf4aSJeff Kirsher 					    MAX_TX_RING_SIZE * sizeof(__le64),
4420e689cf4aSJeff Kirsher 					    &rp->descr_dma, GFP_KERNEL);
4421e689cf4aSJeff Kirsher 	if (!rp->descr)
4422e689cf4aSJeff Kirsher 		return -ENOMEM;
4423e689cf4aSJeff Kirsher 	if ((unsigned long)rp->descr & (64UL - 1)) {
4424e689cf4aSJeff Kirsher 		netdev_err(np->dev, "Coherent alloc gives misaligned TXDMA descr table %p\n",
4425e689cf4aSJeff Kirsher 			   rp->descr);
4426e689cf4aSJeff Kirsher 		return -EINVAL;
4427e689cf4aSJeff Kirsher 	}
4428e689cf4aSJeff Kirsher 
4429e689cf4aSJeff Kirsher 	rp->pending = MAX_TX_RING_SIZE;
4430e689cf4aSJeff Kirsher 	rp->prod = 0;
4431e689cf4aSJeff Kirsher 	rp->cons = 0;
4432e689cf4aSJeff Kirsher 	rp->wrap_bit = 0;
4433e689cf4aSJeff Kirsher 
4434e689cf4aSJeff Kirsher 	/* XXX make these configurable... XXX */
4435e689cf4aSJeff Kirsher 	rp->mark_freq = rp->pending / 4;
4436e689cf4aSJeff Kirsher 
4437e689cf4aSJeff Kirsher 	niu_set_max_burst(np, rp);
4438e689cf4aSJeff Kirsher 
4439e689cf4aSJeff Kirsher 	return 0;
4440e689cf4aSJeff Kirsher }
4441e689cf4aSJeff Kirsher 
niu_size_rbr(struct niu * np,struct rx_ring_info * rp)4442e689cf4aSJeff Kirsher static void niu_size_rbr(struct niu *np, struct rx_ring_info *rp)
4443e689cf4aSJeff Kirsher {
4444e689cf4aSJeff Kirsher 	u16 bss;
4445e689cf4aSJeff Kirsher 
4446e689cf4aSJeff Kirsher 	bss = min(PAGE_SHIFT, 15);
4447e689cf4aSJeff Kirsher 
4448e689cf4aSJeff Kirsher 	rp->rbr_block_size = 1 << bss;
4449e689cf4aSJeff Kirsher 	rp->rbr_blocks_per_page = 1 << (PAGE_SHIFT-bss);
4450e689cf4aSJeff Kirsher 
4451e689cf4aSJeff Kirsher 	rp->rbr_sizes[0] = 256;
4452e689cf4aSJeff Kirsher 	rp->rbr_sizes[1] = 1024;
4453e689cf4aSJeff Kirsher 	if (np->dev->mtu > ETH_DATA_LEN) {
4454e689cf4aSJeff Kirsher 		switch (PAGE_SIZE) {
4455e689cf4aSJeff Kirsher 		case 4 * 1024:
4456e689cf4aSJeff Kirsher 			rp->rbr_sizes[2] = 4096;
4457e689cf4aSJeff Kirsher 			break;
4458e689cf4aSJeff Kirsher 
4459e689cf4aSJeff Kirsher 		default:
4460e689cf4aSJeff Kirsher 			rp->rbr_sizes[2] = 8192;
4461e689cf4aSJeff Kirsher 			break;
4462e689cf4aSJeff Kirsher 		}
4463e689cf4aSJeff Kirsher 	} else {
4464e689cf4aSJeff Kirsher 		rp->rbr_sizes[2] = 2048;
4465e689cf4aSJeff Kirsher 	}
4466e689cf4aSJeff Kirsher 	rp->rbr_sizes[3] = rp->rbr_block_size;
4467e689cf4aSJeff Kirsher }
4468e689cf4aSJeff Kirsher 
niu_alloc_channels(struct niu * np)4469e689cf4aSJeff Kirsher static int niu_alloc_channels(struct niu *np)
4470e689cf4aSJeff Kirsher {
4471e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
4472e689cf4aSJeff Kirsher 	int first_rx_channel, first_tx_channel;
4473e689cf4aSJeff Kirsher 	int num_rx_rings, num_tx_rings;
4474e689cf4aSJeff Kirsher 	struct rx_ring_info *rx_rings;
4475e689cf4aSJeff Kirsher 	struct tx_ring_info *tx_rings;
4476e689cf4aSJeff Kirsher 	int i, port, err;
4477e689cf4aSJeff Kirsher 
4478e689cf4aSJeff Kirsher 	port = np->port;
4479e689cf4aSJeff Kirsher 	first_rx_channel = first_tx_channel = 0;
4480e689cf4aSJeff Kirsher 	for (i = 0; i < port; i++) {
4481e689cf4aSJeff Kirsher 		first_rx_channel += parent->rxchan_per_port[i];
4482e689cf4aSJeff Kirsher 		first_tx_channel += parent->txchan_per_port[i];
4483e689cf4aSJeff Kirsher 	}
4484e689cf4aSJeff Kirsher 
4485e689cf4aSJeff Kirsher 	num_rx_rings = parent->rxchan_per_port[port];
4486e689cf4aSJeff Kirsher 	num_tx_rings = parent->txchan_per_port[port];
4487e689cf4aSJeff Kirsher 
4488e689cf4aSJeff Kirsher 	rx_rings = kcalloc(num_rx_rings, sizeof(struct rx_ring_info),
4489e689cf4aSJeff Kirsher 			   GFP_KERNEL);
4490e689cf4aSJeff Kirsher 	err = -ENOMEM;
4491e689cf4aSJeff Kirsher 	if (!rx_rings)
4492e689cf4aSJeff Kirsher 		goto out_err;
4493e689cf4aSJeff Kirsher 
4494e689cf4aSJeff Kirsher 	np->num_rx_rings = num_rx_rings;
4495e689cf4aSJeff Kirsher 	smp_wmb();
4496e689cf4aSJeff Kirsher 	np->rx_rings = rx_rings;
4497e689cf4aSJeff Kirsher 
4498e689cf4aSJeff Kirsher 	netif_set_real_num_rx_queues(np->dev, num_rx_rings);
4499e689cf4aSJeff Kirsher 
4500e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_rx_rings; i++) {
4501e689cf4aSJeff Kirsher 		struct rx_ring_info *rp = &np->rx_rings[i];
4502e689cf4aSJeff Kirsher 
4503e689cf4aSJeff Kirsher 		rp->np = np;
4504e689cf4aSJeff Kirsher 		rp->rx_channel = first_rx_channel + i;
4505e689cf4aSJeff Kirsher 
4506e689cf4aSJeff Kirsher 		err = niu_alloc_rx_ring_info(np, rp);
4507e689cf4aSJeff Kirsher 		if (err)
4508e689cf4aSJeff Kirsher 			goto out_err;
4509e689cf4aSJeff Kirsher 
4510e689cf4aSJeff Kirsher 		niu_size_rbr(np, rp);
4511e689cf4aSJeff Kirsher 
4512e689cf4aSJeff Kirsher 		/* XXX better defaults, configurable, etc... XXX */
4513e689cf4aSJeff Kirsher 		rp->nonsyn_window = 64;
4514e689cf4aSJeff Kirsher 		rp->nonsyn_threshold = rp->rcr_table_size - 64;
4515e689cf4aSJeff Kirsher 		rp->syn_window = 64;
4516e689cf4aSJeff Kirsher 		rp->syn_threshold = rp->rcr_table_size - 64;
4517e689cf4aSJeff Kirsher 		rp->rcr_pkt_threshold = 16;
4518e689cf4aSJeff Kirsher 		rp->rcr_timeout = 8;
4519e689cf4aSJeff Kirsher 		rp->rbr_kick_thresh = RBR_REFILL_MIN;
4520e689cf4aSJeff Kirsher 		if (rp->rbr_kick_thresh < rp->rbr_blocks_per_page)
4521e689cf4aSJeff Kirsher 			rp->rbr_kick_thresh = rp->rbr_blocks_per_page;
4522e689cf4aSJeff Kirsher 
4523e689cf4aSJeff Kirsher 		err = niu_rbr_fill(np, rp, GFP_KERNEL);
4524e689cf4aSJeff Kirsher 		if (err)
45258ce07be7SHarshit Mogalapalli 			goto out_err;
4526e689cf4aSJeff Kirsher 	}
4527e689cf4aSJeff Kirsher 
4528e689cf4aSJeff Kirsher 	tx_rings = kcalloc(num_tx_rings, sizeof(struct tx_ring_info),
4529e689cf4aSJeff Kirsher 			   GFP_KERNEL);
4530e689cf4aSJeff Kirsher 	err = -ENOMEM;
4531e689cf4aSJeff Kirsher 	if (!tx_rings)
4532e689cf4aSJeff Kirsher 		goto out_err;
4533e689cf4aSJeff Kirsher 
4534e689cf4aSJeff Kirsher 	np->num_tx_rings = num_tx_rings;
4535e689cf4aSJeff Kirsher 	smp_wmb();
4536e689cf4aSJeff Kirsher 	np->tx_rings = tx_rings;
4537e689cf4aSJeff Kirsher 
4538e689cf4aSJeff Kirsher 	netif_set_real_num_tx_queues(np->dev, num_tx_rings);
4539e689cf4aSJeff Kirsher 
4540e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_tx_rings; i++) {
4541e689cf4aSJeff Kirsher 		struct tx_ring_info *rp = &np->tx_rings[i];
4542e689cf4aSJeff Kirsher 
4543e689cf4aSJeff Kirsher 		rp->np = np;
4544e689cf4aSJeff Kirsher 		rp->tx_channel = first_tx_channel + i;
4545e689cf4aSJeff Kirsher 
4546e689cf4aSJeff Kirsher 		err = niu_alloc_tx_ring_info(np, rp);
4547e689cf4aSJeff Kirsher 		if (err)
4548e689cf4aSJeff Kirsher 			goto out_err;
4549e689cf4aSJeff Kirsher 	}
4550e689cf4aSJeff Kirsher 
4551e689cf4aSJeff Kirsher 	return 0;
4552e689cf4aSJeff Kirsher 
4553e689cf4aSJeff Kirsher out_err:
4554e689cf4aSJeff Kirsher 	niu_free_channels(np);
4555e689cf4aSJeff Kirsher 	return err;
4556e689cf4aSJeff Kirsher }
4557e689cf4aSJeff Kirsher 
niu_tx_cs_sng_poll(struct niu * np,int channel)4558e689cf4aSJeff Kirsher static int niu_tx_cs_sng_poll(struct niu *np, int channel)
4559e689cf4aSJeff Kirsher {
4560e689cf4aSJeff Kirsher 	int limit = 1000;
4561e689cf4aSJeff Kirsher 
4562e689cf4aSJeff Kirsher 	while (--limit > 0) {
4563e689cf4aSJeff Kirsher 		u64 val = nr64(TX_CS(channel));
4564e689cf4aSJeff Kirsher 		if (val & TX_CS_SNG_STATE)
4565e689cf4aSJeff Kirsher 			return 0;
4566e689cf4aSJeff Kirsher 	}
4567e689cf4aSJeff Kirsher 	return -ENODEV;
4568e689cf4aSJeff Kirsher }
4569e689cf4aSJeff Kirsher 
niu_tx_channel_stop(struct niu * np,int channel)4570e689cf4aSJeff Kirsher static int niu_tx_channel_stop(struct niu *np, int channel)
4571e689cf4aSJeff Kirsher {
4572e689cf4aSJeff Kirsher 	u64 val = nr64(TX_CS(channel));
4573e689cf4aSJeff Kirsher 
4574e689cf4aSJeff Kirsher 	val |= TX_CS_STOP_N_GO;
4575e689cf4aSJeff Kirsher 	nw64(TX_CS(channel), val);
4576e689cf4aSJeff Kirsher 
4577e689cf4aSJeff Kirsher 	return niu_tx_cs_sng_poll(np, channel);
4578e689cf4aSJeff Kirsher }
4579e689cf4aSJeff Kirsher 
niu_tx_cs_reset_poll(struct niu * np,int channel)4580e689cf4aSJeff Kirsher static int niu_tx_cs_reset_poll(struct niu *np, int channel)
4581e689cf4aSJeff Kirsher {
4582e689cf4aSJeff Kirsher 	int limit = 1000;
4583e689cf4aSJeff Kirsher 
4584e689cf4aSJeff Kirsher 	while (--limit > 0) {
4585e689cf4aSJeff Kirsher 		u64 val = nr64(TX_CS(channel));
4586e689cf4aSJeff Kirsher 		if (!(val & TX_CS_RST))
4587e689cf4aSJeff Kirsher 			return 0;
4588e689cf4aSJeff Kirsher 	}
4589e689cf4aSJeff Kirsher 	return -ENODEV;
4590e689cf4aSJeff Kirsher }
4591e689cf4aSJeff Kirsher 
niu_tx_channel_reset(struct niu * np,int channel)4592e689cf4aSJeff Kirsher static int niu_tx_channel_reset(struct niu *np, int channel)
4593e689cf4aSJeff Kirsher {
4594e689cf4aSJeff Kirsher 	u64 val = nr64(TX_CS(channel));
4595e689cf4aSJeff Kirsher 	int err;
4596e689cf4aSJeff Kirsher 
4597e689cf4aSJeff Kirsher 	val |= TX_CS_RST;
4598e689cf4aSJeff Kirsher 	nw64(TX_CS(channel), val);
4599e689cf4aSJeff Kirsher 
4600e689cf4aSJeff Kirsher 	err = niu_tx_cs_reset_poll(np, channel);
4601e689cf4aSJeff Kirsher 	if (!err)
4602e689cf4aSJeff Kirsher 		nw64(TX_RING_KICK(channel), 0);
4603e689cf4aSJeff Kirsher 
4604e689cf4aSJeff Kirsher 	return err;
4605e689cf4aSJeff Kirsher }
4606e689cf4aSJeff Kirsher 
niu_tx_channel_lpage_init(struct niu * np,int channel)4607e689cf4aSJeff Kirsher static int niu_tx_channel_lpage_init(struct niu *np, int channel)
4608e689cf4aSJeff Kirsher {
4609e689cf4aSJeff Kirsher 	u64 val;
4610e689cf4aSJeff Kirsher 
4611e689cf4aSJeff Kirsher 	nw64(TX_LOG_MASK1(channel), 0);
4612e689cf4aSJeff Kirsher 	nw64(TX_LOG_VAL1(channel), 0);
4613e689cf4aSJeff Kirsher 	nw64(TX_LOG_MASK2(channel), 0);
4614e689cf4aSJeff Kirsher 	nw64(TX_LOG_VAL2(channel), 0);
4615e689cf4aSJeff Kirsher 	nw64(TX_LOG_PAGE_RELO1(channel), 0);
4616e689cf4aSJeff Kirsher 	nw64(TX_LOG_PAGE_RELO2(channel), 0);
4617e689cf4aSJeff Kirsher 	nw64(TX_LOG_PAGE_HDL(channel), 0);
4618e689cf4aSJeff Kirsher 
4619e689cf4aSJeff Kirsher 	val  = (u64)np->port << TX_LOG_PAGE_VLD_FUNC_SHIFT;
4620e689cf4aSJeff Kirsher 	val |= (TX_LOG_PAGE_VLD_PAGE0 | TX_LOG_PAGE_VLD_PAGE1);
4621e689cf4aSJeff Kirsher 	nw64(TX_LOG_PAGE_VLD(channel), val);
4622e689cf4aSJeff Kirsher 
4623e689cf4aSJeff Kirsher 	/* XXX TXDMA 32bit mode? XXX */
4624e689cf4aSJeff Kirsher 
4625e689cf4aSJeff Kirsher 	return 0;
4626e689cf4aSJeff Kirsher }
4627e689cf4aSJeff Kirsher 
niu_txc_enable_port(struct niu * np,int on)4628e689cf4aSJeff Kirsher static void niu_txc_enable_port(struct niu *np, int on)
4629e689cf4aSJeff Kirsher {
4630e689cf4aSJeff Kirsher 	unsigned long flags;
4631e689cf4aSJeff Kirsher 	u64 val, mask;
4632e689cf4aSJeff Kirsher 
4633e689cf4aSJeff Kirsher 	niu_lock_parent(np, flags);
4634e689cf4aSJeff Kirsher 	val = nr64(TXC_CONTROL);
4635e689cf4aSJeff Kirsher 	mask = (u64)1 << np->port;
4636e689cf4aSJeff Kirsher 	if (on) {
4637e689cf4aSJeff Kirsher 		val |= TXC_CONTROL_ENABLE | mask;
4638e689cf4aSJeff Kirsher 	} else {
4639e689cf4aSJeff Kirsher 		val &= ~mask;
4640e689cf4aSJeff Kirsher 		if ((val & ~TXC_CONTROL_ENABLE) == 0)
4641e689cf4aSJeff Kirsher 			val &= ~TXC_CONTROL_ENABLE;
4642e689cf4aSJeff Kirsher 	}
4643e689cf4aSJeff Kirsher 	nw64(TXC_CONTROL, val);
4644e689cf4aSJeff Kirsher 	niu_unlock_parent(np, flags);
4645e689cf4aSJeff Kirsher }
4646e689cf4aSJeff Kirsher 
niu_txc_set_imask(struct niu * np,u64 imask)4647e689cf4aSJeff Kirsher static void niu_txc_set_imask(struct niu *np, u64 imask)
4648e689cf4aSJeff Kirsher {
4649e689cf4aSJeff Kirsher 	unsigned long flags;
4650e689cf4aSJeff Kirsher 	u64 val;
4651e689cf4aSJeff Kirsher 
4652e689cf4aSJeff Kirsher 	niu_lock_parent(np, flags);
4653e689cf4aSJeff Kirsher 	val = nr64(TXC_INT_MASK);
4654e689cf4aSJeff Kirsher 	val &= ~TXC_INT_MASK_VAL(np->port);
4655e689cf4aSJeff Kirsher 	val |= (imask << TXC_INT_MASK_VAL_SHIFT(np->port));
4656e689cf4aSJeff Kirsher 	niu_unlock_parent(np, flags);
4657e689cf4aSJeff Kirsher }
4658e689cf4aSJeff Kirsher 
niu_txc_port_dma_enable(struct niu * np,int on)4659e689cf4aSJeff Kirsher static void niu_txc_port_dma_enable(struct niu *np, int on)
4660e689cf4aSJeff Kirsher {
4661e689cf4aSJeff Kirsher 	u64 val = 0;
4662e689cf4aSJeff Kirsher 
4663e689cf4aSJeff Kirsher 	if (on) {
4664e689cf4aSJeff Kirsher 		int i;
4665e689cf4aSJeff Kirsher 
4666e689cf4aSJeff Kirsher 		for (i = 0; i < np->num_tx_rings; i++)
4667e689cf4aSJeff Kirsher 			val |= (1 << np->tx_rings[i].tx_channel);
4668e689cf4aSJeff Kirsher 	}
4669e689cf4aSJeff Kirsher 	nw64(TXC_PORT_DMA(np->port), val);
4670e689cf4aSJeff Kirsher }
4671e689cf4aSJeff Kirsher 
niu_init_one_tx_channel(struct niu * np,struct tx_ring_info * rp)4672e689cf4aSJeff Kirsher static int niu_init_one_tx_channel(struct niu *np, struct tx_ring_info *rp)
4673e689cf4aSJeff Kirsher {
4674e689cf4aSJeff Kirsher 	int err, channel = rp->tx_channel;
4675e689cf4aSJeff Kirsher 	u64 val, ring_len;
4676e689cf4aSJeff Kirsher 
4677e689cf4aSJeff Kirsher 	err = niu_tx_channel_stop(np, channel);
4678e689cf4aSJeff Kirsher 	if (err)
4679e689cf4aSJeff Kirsher 		return err;
4680e689cf4aSJeff Kirsher 
4681e689cf4aSJeff Kirsher 	err = niu_tx_channel_reset(np, channel);
4682e689cf4aSJeff Kirsher 	if (err)
4683e689cf4aSJeff Kirsher 		return err;
4684e689cf4aSJeff Kirsher 
4685e689cf4aSJeff Kirsher 	err = niu_tx_channel_lpage_init(np, channel);
4686e689cf4aSJeff Kirsher 	if (err)
4687e689cf4aSJeff Kirsher 		return err;
4688e689cf4aSJeff Kirsher 
4689e689cf4aSJeff Kirsher 	nw64(TXC_DMA_MAX(channel), rp->max_burst);
4690e689cf4aSJeff Kirsher 	nw64(TX_ENT_MSK(channel), 0);
4691e689cf4aSJeff Kirsher 
4692e689cf4aSJeff Kirsher 	if (rp->descr_dma & ~(TX_RNG_CFIG_STADDR_BASE |
4693e689cf4aSJeff Kirsher 			      TX_RNG_CFIG_STADDR)) {
4694e689cf4aSJeff Kirsher 		netdev_err(np->dev, "TX ring channel %d DMA addr (%llx) is not aligned\n",
4695e689cf4aSJeff Kirsher 			   channel, (unsigned long long)rp->descr_dma);
4696e689cf4aSJeff Kirsher 		return -EINVAL;
4697e689cf4aSJeff Kirsher 	}
4698e689cf4aSJeff Kirsher 
4699e689cf4aSJeff Kirsher 	/* The length field in TX_RNG_CFIG is measured in 64-byte
4700e689cf4aSJeff Kirsher 	 * blocks.  rp->pending is the number of TX descriptors in
4701e689cf4aSJeff Kirsher 	 * our ring, 8 bytes each, thus we divide by 8 bytes more
4702e689cf4aSJeff Kirsher 	 * to get the proper value the chip wants.
4703e689cf4aSJeff Kirsher 	 */
4704e689cf4aSJeff Kirsher 	ring_len = (rp->pending / 8);
4705e689cf4aSJeff Kirsher 
4706e689cf4aSJeff Kirsher 	val = ((ring_len << TX_RNG_CFIG_LEN_SHIFT) |
4707e689cf4aSJeff Kirsher 	       rp->descr_dma);
4708e689cf4aSJeff Kirsher 	nw64(TX_RNG_CFIG(channel), val);
4709e689cf4aSJeff Kirsher 
4710e689cf4aSJeff Kirsher 	if (((rp->mbox_dma >> 32) & ~TXDMA_MBH_MBADDR) ||
4711e689cf4aSJeff Kirsher 	    ((u32)rp->mbox_dma & ~TXDMA_MBL_MBADDR)) {
4712e689cf4aSJeff Kirsher 		netdev_err(np->dev, "TX ring channel %d MBOX addr (%llx) has invalid bits\n",
4713e689cf4aSJeff Kirsher 			    channel, (unsigned long long)rp->mbox_dma);
4714e689cf4aSJeff Kirsher 		return -EINVAL;
4715e689cf4aSJeff Kirsher 	}
4716e689cf4aSJeff Kirsher 	nw64(TXDMA_MBH(channel), rp->mbox_dma >> 32);
4717e689cf4aSJeff Kirsher 	nw64(TXDMA_MBL(channel), rp->mbox_dma & TXDMA_MBL_MBADDR);
4718e689cf4aSJeff Kirsher 
4719e689cf4aSJeff Kirsher 	nw64(TX_CS(channel), 0);
4720e689cf4aSJeff Kirsher 
4721e689cf4aSJeff Kirsher 	rp->last_pkt_cnt = 0;
4722e689cf4aSJeff Kirsher 
4723e689cf4aSJeff Kirsher 	return 0;
4724e689cf4aSJeff Kirsher }
4725e689cf4aSJeff Kirsher 
niu_init_rdc_groups(struct niu * np)4726e689cf4aSJeff Kirsher static void niu_init_rdc_groups(struct niu *np)
4727e689cf4aSJeff Kirsher {
4728e689cf4aSJeff Kirsher 	struct niu_rdc_tables *tp = &np->parent->rdc_group_cfg[np->port];
4729e689cf4aSJeff Kirsher 	int i, first_table_num = tp->first_table_num;
4730e689cf4aSJeff Kirsher 
4731e689cf4aSJeff Kirsher 	for (i = 0; i < tp->num_tables; i++) {
4732e689cf4aSJeff Kirsher 		struct rdc_table *tbl = &tp->tables[i];
4733e689cf4aSJeff Kirsher 		int this_table = first_table_num + i;
4734e689cf4aSJeff Kirsher 		int slot;
4735e689cf4aSJeff Kirsher 
4736e689cf4aSJeff Kirsher 		for (slot = 0; slot < NIU_RDC_TABLE_SLOTS; slot++)
4737e689cf4aSJeff Kirsher 			nw64(RDC_TBL(this_table, slot),
4738e689cf4aSJeff Kirsher 			     tbl->rxdma_channel[slot]);
4739e689cf4aSJeff Kirsher 	}
4740e689cf4aSJeff Kirsher 
4741e689cf4aSJeff Kirsher 	nw64(DEF_RDC(np->port), np->parent->rdc_default[np->port]);
4742e689cf4aSJeff Kirsher }
4743e689cf4aSJeff Kirsher 
niu_init_drr_weight(struct niu * np)4744e689cf4aSJeff Kirsher static void niu_init_drr_weight(struct niu *np)
4745e689cf4aSJeff Kirsher {
4746e689cf4aSJeff Kirsher 	int type = phy_decode(np->parent->port_phy, np->port);
4747e689cf4aSJeff Kirsher 	u64 val;
4748e689cf4aSJeff Kirsher 
4749e689cf4aSJeff Kirsher 	switch (type) {
4750e689cf4aSJeff Kirsher 	case PORT_TYPE_10G:
4751e689cf4aSJeff Kirsher 		val = PT_DRR_WEIGHT_DEFAULT_10G;
4752e689cf4aSJeff Kirsher 		break;
4753e689cf4aSJeff Kirsher 
4754e689cf4aSJeff Kirsher 	case PORT_TYPE_1G:
4755e689cf4aSJeff Kirsher 	default:
4756e689cf4aSJeff Kirsher 		val = PT_DRR_WEIGHT_DEFAULT_1G;
4757e689cf4aSJeff Kirsher 		break;
4758e689cf4aSJeff Kirsher 	}
4759e689cf4aSJeff Kirsher 	nw64(PT_DRR_WT(np->port), val);
4760e689cf4aSJeff Kirsher }
4761e689cf4aSJeff Kirsher 
niu_init_hostinfo(struct niu * np)4762e689cf4aSJeff Kirsher static int niu_init_hostinfo(struct niu *np)
4763e689cf4aSJeff Kirsher {
4764e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
4765e689cf4aSJeff Kirsher 	struct niu_rdc_tables *tp = &parent->rdc_group_cfg[np->port];
4766e689cf4aSJeff Kirsher 	int i, err, num_alt = niu_num_alt_addr(np);
4767e689cf4aSJeff Kirsher 	int first_rdc_table = tp->first_table_num;
4768e689cf4aSJeff Kirsher 
4769e689cf4aSJeff Kirsher 	err = niu_set_primary_mac_rdc_table(np, first_rdc_table, 1);
4770e689cf4aSJeff Kirsher 	if (err)
4771e689cf4aSJeff Kirsher 		return err;
4772e689cf4aSJeff Kirsher 
4773e689cf4aSJeff Kirsher 	err = niu_set_multicast_mac_rdc_table(np, first_rdc_table, 1);
4774e689cf4aSJeff Kirsher 	if (err)
4775e689cf4aSJeff Kirsher 		return err;
4776e689cf4aSJeff Kirsher 
4777e689cf4aSJeff Kirsher 	for (i = 0; i < num_alt; i++) {
4778e689cf4aSJeff Kirsher 		err = niu_set_alt_mac_rdc_table(np, i, first_rdc_table, 1);
4779e689cf4aSJeff Kirsher 		if (err)
4780e689cf4aSJeff Kirsher 			return err;
4781e689cf4aSJeff Kirsher 	}
4782e689cf4aSJeff Kirsher 
4783e689cf4aSJeff Kirsher 	return 0;
4784e689cf4aSJeff Kirsher }
4785e689cf4aSJeff Kirsher 
niu_rx_channel_reset(struct niu * np,int channel)4786e689cf4aSJeff Kirsher static int niu_rx_channel_reset(struct niu *np, int channel)
4787e689cf4aSJeff Kirsher {
4788e689cf4aSJeff Kirsher 	return niu_set_and_wait_clear(np, RXDMA_CFIG1(channel),
4789e689cf4aSJeff Kirsher 				      RXDMA_CFIG1_RST, 1000, 10,
4790e689cf4aSJeff Kirsher 				      "RXDMA_CFIG1");
4791e689cf4aSJeff Kirsher }
4792e689cf4aSJeff Kirsher 
niu_rx_channel_lpage_init(struct niu * np,int channel)4793e689cf4aSJeff Kirsher static int niu_rx_channel_lpage_init(struct niu *np, int channel)
4794e689cf4aSJeff Kirsher {
4795e689cf4aSJeff Kirsher 	u64 val;
4796e689cf4aSJeff Kirsher 
4797e689cf4aSJeff Kirsher 	nw64(RX_LOG_MASK1(channel), 0);
4798e689cf4aSJeff Kirsher 	nw64(RX_LOG_VAL1(channel), 0);
4799e689cf4aSJeff Kirsher 	nw64(RX_LOG_MASK2(channel), 0);
4800e689cf4aSJeff Kirsher 	nw64(RX_LOG_VAL2(channel), 0);
4801e689cf4aSJeff Kirsher 	nw64(RX_LOG_PAGE_RELO1(channel), 0);
4802e689cf4aSJeff Kirsher 	nw64(RX_LOG_PAGE_RELO2(channel), 0);
4803e689cf4aSJeff Kirsher 	nw64(RX_LOG_PAGE_HDL(channel), 0);
4804e689cf4aSJeff Kirsher 
4805e689cf4aSJeff Kirsher 	val  = (u64)np->port << RX_LOG_PAGE_VLD_FUNC_SHIFT;
4806e689cf4aSJeff Kirsher 	val |= (RX_LOG_PAGE_VLD_PAGE0 | RX_LOG_PAGE_VLD_PAGE1);
4807e689cf4aSJeff Kirsher 	nw64(RX_LOG_PAGE_VLD(channel), val);
4808e689cf4aSJeff Kirsher 
4809e689cf4aSJeff Kirsher 	return 0;
4810e689cf4aSJeff Kirsher }
4811e689cf4aSJeff Kirsher 
niu_rx_channel_wred_init(struct niu * np,struct rx_ring_info * rp)4812e689cf4aSJeff Kirsher static void niu_rx_channel_wred_init(struct niu *np, struct rx_ring_info *rp)
4813e689cf4aSJeff Kirsher {
4814e689cf4aSJeff Kirsher 	u64 val;
4815e689cf4aSJeff Kirsher 
4816e689cf4aSJeff Kirsher 	val = (((u64)rp->nonsyn_window << RDC_RED_PARA_WIN_SHIFT) |
4817e689cf4aSJeff Kirsher 	       ((u64)rp->nonsyn_threshold << RDC_RED_PARA_THRE_SHIFT) |
4818e689cf4aSJeff Kirsher 	       ((u64)rp->syn_window << RDC_RED_PARA_WIN_SYN_SHIFT) |
4819e689cf4aSJeff Kirsher 	       ((u64)rp->syn_threshold << RDC_RED_PARA_THRE_SYN_SHIFT));
4820e689cf4aSJeff Kirsher 	nw64(RDC_RED_PARA(rp->rx_channel), val);
4821e689cf4aSJeff Kirsher }
4822e689cf4aSJeff Kirsher 
niu_compute_rbr_cfig_b(struct rx_ring_info * rp,u64 * ret)4823e689cf4aSJeff Kirsher static int niu_compute_rbr_cfig_b(struct rx_ring_info *rp, u64 *ret)
4824e689cf4aSJeff Kirsher {
4825e689cf4aSJeff Kirsher 	u64 val = 0;
4826e689cf4aSJeff Kirsher 
4827e689cf4aSJeff Kirsher 	*ret = 0;
4828e689cf4aSJeff Kirsher 	switch (rp->rbr_block_size) {
4829e689cf4aSJeff Kirsher 	case 4 * 1024:
4830e689cf4aSJeff Kirsher 		val |= (RBR_BLKSIZE_4K << RBR_CFIG_B_BLKSIZE_SHIFT);
4831e689cf4aSJeff Kirsher 		break;
4832e689cf4aSJeff Kirsher 	case 8 * 1024:
4833e689cf4aSJeff Kirsher 		val |= (RBR_BLKSIZE_8K << RBR_CFIG_B_BLKSIZE_SHIFT);
4834e689cf4aSJeff Kirsher 		break;
4835e689cf4aSJeff Kirsher 	case 16 * 1024:
4836e689cf4aSJeff Kirsher 		val |= (RBR_BLKSIZE_16K << RBR_CFIG_B_BLKSIZE_SHIFT);
4837e689cf4aSJeff Kirsher 		break;
4838e689cf4aSJeff Kirsher 	case 32 * 1024:
4839e689cf4aSJeff Kirsher 		val |= (RBR_BLKSIZE_32K << RBR_CFIG_B_BLKSIZE_SHIFT);
4840e689cf4aSJeff Kirsher 		break;
4841e689cf4aSJeff Kirsher 	default:
4842e689cf4aSJeff Kirsher 		return -EINVAL;
4843e689cf4aSJeff Kirsher 	}
4844e689cf4aSJeff Kirsher 	val |= RBR_CFIG_B_VLD2;
4845e689cf4aSJeff Kirsher 	switch (rp->rbr_sizes[2]) {
4846e689cf4aSJeff Kirsher 	case 2 * 1024:
4847e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ2_2K << RBR_CFIG_B_BUFSZ2_SHIFT);
4848e689cf4aSJeff Kirsher 		break;
4849e689cf4aSJeff Kirsher 	case 4 * 1024:
4850e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ2_4K << RBR_CFIG_B_BUFSZ2_SHIFT);
4851e689cf4aSJeff Kirsher 		break;
4852e689cf4aSJeff Kirsher 	case 8 * 1024:
4853e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ2_8K << RBR_CFIG_B_BUFSZ2_SHIFT);
4854e689cf4aSJeff Kirsher 		break;
4855e689cf4aSJeff Kirsher 	case 16 * 1024:
4856e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ2_16K << RBR_CFIG_B_BUFSZ2_SHIFT);
4857e689cf4aSJeff Kirsher 		break;
4858e689cf4aSJeff Kirsher 
4859e689cf4aSJeff Kirsher 	default:
4860e689cf4aSJeff Kirsher 		return -EINVAL;
4861e689cf4aSJeff Kirsher 	}
4862e689cf4aSJeff Kirsher 	val |= RBR_CFIG_B_VLD1;
4863e689cf4aSJeff Kirsher 	switch (rp->rbr_sizes[1]) {
4864e689cf4aSJeff Kirsher 	case 1 * 1024:
4865e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ1_1K << RBR_CFIG_B_BUFSZ1_SHIFT);
4866e689cf4aSJeff Kirsher 		break;
4867e689cf4aSJeff Kirsher 	case 2 * 1024:
4868e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ1_2K << RBR_CFIG_B_BUFSZ1_SHIFT);
4869e689cf4aSJeff Kirsher 		break;
4870e689cf4aSJeff Kirsher 	case 4 * 1024:
4871e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ1_4K << RBR_CFIG_B_BUFSZ1_SHIFT);
4872e689cf4aSJeff Kirsher 		break;
4873e689cf4aSJeff Kirsher 	case 8 * 1024:
4874e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ1_8K << RBR_CFIG_B_BUFSZ1_SHIFT);
4875e689cf4aSJeff Kirsher 		break;
4876e689cf4aSJeff Kirsher 
4877e689cf4aSJeff Kirsher 	default:
4878e689cf4aSJeff Kirsher 		return -EINVAL;
4879e689cf4aSJeff Kirsher 	}
4880e689cf4aSJeff Kirsher 	val |= RBR_CFIG_B_VLD0;
4881e689cf4aSJeff Kirsher 	switch (rp->rbr_sizes[0]) {
4882e689cf4aSJeff Kirsher 	case 256:
4883e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ0_256 << RBR_CFIG_B_BUFSZ0_SHIFT);
4884e689cf4aSJeff Kirsher 		break;
4885e689cf4aSJeff Kirsher 	case 512:
4886e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ0_512 << RBR_CFIG_B_BUFSZ0_SHIFT);
4887e689cf4aSJeff Kirsher 		break;
4888e689cf4aSJeff Kirsher 	case 1 * 1024:
4889e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ0_1K << RBR_CFIG_B_BUFSZ0_SHIFT);
4890e689cf4aSJeff Kirsher 		break;
4891e689cf4aSJeff Kirsher 	case 2 * 1024:
4892e689cf4aSJeff Kirsher 		val |= (RBR_BUFSZ0_2K << RBR_CFIG_B_BUFSZ0_SHIFT);
4893e689cf4aSJeff Kirsher 		break;
4894e689cf4aSJeff Kirsher 
4895e689cf4aSJeff Kirsher 	default:
4896e689cf4aSJeff Kirsher 		return -EINVAL;
4897e689cf4aSJeff Kirsher 	}
4898e689cf4aSJeff Kirsher 
4899e689cf4aSJeff Kirsher 	*ret = val;
4900e689cf4aSJeff Kirsher 	return 0;
4901e689cf4aSJeff Kirsher }
4902e689cf4aSJeff Kirsher 
niu_enable_rx_channel(struct niu * np,int channel,int on)4903e689cf4aSJeff Kirsher static int niu_enable_rx_channel(struct niu *np, int channel, int on)
4904e689cf4aSJeff Kirsher {
4905e689cf4aSJeff Kirsher 	u64 val = nr64(RXDMA_CFIG1(channel));
4906e689cf4aSJeff Kirsher 	int limit;
4907e689cf4aSJeff Kirsher 
4908e689cf4aSJeff Kirsher 	if (on)
4909e689cf4aSJeff Kirsher 		val |= RXDMA_CFIG1_EN;
4910e689cf4aSJeff Kirsher 	else
4911e689cf4aSJeff Kirsher 		val &= ~RXDMA_CFIG1_EN;
4912e689cf4aSJeff Kirsher 	nw64(RXDMA_CFIG1(channel), val);
4913e689cf4aSJeff Kirsher 
4914e689cf4aSJeff Kirsher 	limit = 1000;
4915e689cf4aSJeff Kirsher 	while (--limit > 0) {
4916e689cf4aSJeff Kirsher 		if (nr64(RXDMA_CFIG1(channel)) & RXDMA_CFIG1_QST)
4917e689cf4aSJeff Kirsher 			break;
4918e689cf4aSJeff Kirsher 		udelay(10);
4919e689cf4aSJeff Kirsher 	}
4920e689cf4aSJeff Kirsher 	if (limit <= 0)
4921e689cf4aSJeff Kirsher 		return -ENODEV;
4922e689cf4aSJeff Kirsher 	return 0;
4923e689cf4aSJeff Kirsher }
4924e689cf4aSJeff Kirsher 
niu_init_one_rx_channel(struct niu * np,struct rx_ring_info * rp)4925e689cf4aSJeff Kirsher static int niu_init_one_rx_channel(struct niu *np, struct rx_ring_info *rp)
4926e689cf4aSJeff Kirsher {
4927e689cf4aSJeff Kirsher 	int err, channel = rp->rx_channel;
4928e689cf4aSJeff Kirsher 	u64 val;
4929e689cf4aSJeff Kirsher 
4930e689cf4aSJeff Kirsher 	err = niu_rx_channel_reset(np, channel);
4931e689cf4aSJeff Kirsher 	if (err)
4932e689cf4aSJeff Kirsher 		return err;
4933e689cf4aSJeff Kirsher 
4934e689cf4aSJeff Kirsher 	err = niu_rx_channel_lpage_init(np, channel);
4935e689cf4aSJeff Kirsher 	if (err)
4936e689cf4aSJeff Kirsher 		return err;
4937e689cf4aSJeff Kirsher 
4938e689cf4aSJeff Kirsher 	niu_rx_channel_wred_init(np, rp);
4939e689cf4aSJeff Kirsher 
4940e689cf4aSJeff Kirsher 	nw64(RX_DMA_ENT_MSK(channel), RX_DMA_ENT_MSK_RBR_EMPTY);
4941e689cf4aSJeff Kirsher 	nw64(RX_DMA_CTL_STAT(channel),
4942e689cf4aSJeff Kirsher 	     (RX_DMA_CTL_STAT_MEX |
4943e689cf4aSJeff Kirsher 	      RX_DMA_CTL_STAT_RCRTHRES |
4944e689cf4aSJeff Kirsher 	      RX_DMA_CTL_STAT_RCRTO |
4945e689cf4aSJeff Kirsher 	      RX_DMA_CTL_STAT_RBR_EMPTY));
4946e689cf4aSJeff Kirsher 	nw64(RXDMA_CFIG1(channel), rp->mbox_dma >> 32);
4947e689cf4aSJeff Kirsher 	nw64(RXDMA_CFIG2(channel),
4948e689cf4aSJeff Kirsher 	     ((rp->mbox_dma & RXDMA_CFIG2_MBADDR_L) |
4949e689cf4aSJeff Kirsher 	      RXDMA_CFIG2_FULL_HDR));
4950e689cf4aSJeff Kirsher 	nw64(RBR_CFIG_A(channel),
4951e689cf4aSJeff Kirsher 	     ((u64)rp->rbr_table_size << RBR_CFIG_A_LEN_SHIFT) |
4952e689cf4aSJeff Kirsher 	     (rp->rbr_dma & (RBR_CFIG_A_STADDR_BASE | RBR_CFIG_A_STADDR)));
4953e689cf4aSJeff Kirsher 	err = niu_compute_rbr_cfig_b(rp, &val);
4954e689cf4aSJeff Kirsher 	if (err)
4955e689cf4aSJeff Kirsher 		return err;
4956e689cf4aSJeff Kirsher 	nw64(RBR_CFIG_B(channel), val);
4957e689cf4aSJeff Kirsher 	nw64(RCRCFIG_A(channel),
4958e689cf4aSJeff Kirsher 	     ((u64)rp->rcr_table_size << RCRCFIG_A_LEN_SHIFT) |
4959e689cf4aSJeff Kirsher 	     (rp->rcr_dma & (RCRCFIG_A_STADDR_BASE | RCRCFIG_A_STADDR)));
4960e689cf4aSJeff Kirsher 	nw64(RCRCFIG_B(channel),
4961e689cf4aSJeff Kirsher 	     ((u64)rp->rcr_pkt_threshold << RCRCFIG_B_PTHRES_SHIFT) |
4962e689cf4aSJeff Kirsher 	     RCRCFIG_B_ENTOUT |
4963e689cf4aSJeff Kirsher 	     ((u64)rp->rcr_timeout << RCRCFIG_B_TIMEOUT_SHIFT));
4964e689cf4aSJeff Kirsher 
4965e689cf4aSJeff Kirsher 	err = niu_enable_rx_channel(np, channel, 1);
4966e689cf4aSJeff Kirsher 	if (err)
4967e689cf4aSJeff Kirsher 		return err;
4968e689cf4aSJeff Kirsher 
4969e689cf4aSJeff Kirsher 	nw64(RBR_KICK(channel), rp->rbr_index);
4970e689cf4aSJeff Kirsher 
4971e689cf4aSJeff Kirsher 	val = nr64(RX_DMA_CTL_STAT(channel));
4972e689cf4aSJeff Kirsher 	val |= RX_DMA_CTL_STAT_RBR_EMPTY;
4973e689cf4aSJeff Kirsher 	nw64(RX_DMA_CTL_STAT(channel), val);
4974e689cf4aSJeff Kirsher 
4975e689cf4aSJeff Kirsher 	return 0;
4976e689cf4aSJeff Kirsher }
4977e689cf4aSJeff Kirsher 
niu_init_rx_channels(struct niu * np)4978e689cf4aSJeff Kirsher static int niu_init_rx_channels(struct niu *np)
4979e689cf4aSJeff Kirsher {
4980e689cf4aSJeff Kirsher 	unsigned long flags;
4981e689cf4aSJeff Kirsher 	u64 seed = jiffies_64;
4982e689cf4aSJeff Kirsher 	int err, i;
4983e689cf4aSJeff Kirsher 
4984e689cf4aSJeff Kirsher 	niu_lock_parent(np, flags);
4985e689cf4aSJeff Kirsher 	nw64(RX_DMA_CK_DIV, np->parent->rxdma_clock_divider);
4986e689cf4aSJeff Kirsher 	nw64(RED_RAN_INIT, RED_RAN_INIT_OPMODE | (seed & RED_RAN_INIT_VAL));
4987e689cf4aSJeff Kirsher 	niu_unlock_parent(np, flags);
4988e689cf4aSJeff Kirsher 
4989e689cf4aSJeff Kirsher 	/* XXX RXDMA 32bit mode? XXX */
4990e689cf4aSJeff Kirsher 
4991e689cf4aSJeff Kirsher 	niu_init_rdc_groups(np);
4992e689cf4aSJeff Kirsher 	niu_init_drr_weight(np);
4993e689cf4aSJeff Kirsher 
4994e689cf4aSJeff Kirsher 	err = niu_init_hostinfo(np);
4995e689cf4aSJeff Kirsher 	if (err)
4996e689cf4aSJeff Kirsher 		return err;
4997e689cf4aSJeff Kirsher 
4998e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_rx_rings; i++) {
4999e689cf4aSJeff Kirsher 		struct rx_ring_info *rp = &np->rx_rings[i];
5000e689cf4aSJeff Kirsher 
5001e689cf4aSJeff Kirsher 		err = niu_init_one_rx_channel(np, rp);
5002e689cf4aSJeff Kirsher 		if (err)
5003e689cf4aSJeff Kirsher 			return err;
5004e689cf4aSJeff Kirsher 	}
5005e689cf4aSJeff Kirsher 
5006e689cf4aSJeff Kirsher 	return 0;
5007e689cf4aSJeff Kirsher }
5008e689cf4aSJeff Kirsher 
niu_set_ip_frag_rule(struct niu * np)5009e689cf4aSJeff Kirsher static int niu_set_ip_frag_rule(struct niu *np)
5010e689cf4aSJeff Kirsher {
5011e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
5012e689cf4aSJeff Kirsher 	struct niu_classifier *cp = &np->clas;
5013e689cf4aSJeff Kirsher 	struct niu_tcam_entry *tp;
5014e689cf4aSJeff Kirsher 	int index, err;
5015e689cf4aSJeff Kirsher 
5016e689cf4aSJeff Kirsher 	index = cp->tcam_top;
5017e689cf4aSJeff Kirsher 	tp = &parent->tcam[index];
5018e689cf4aSJeff Kirsher 
5019e689cf4aSJeff Kirsher 	/* Note that the noport bit is the same in both ipv4 and
5020e689cf4aSJeff Kirsher 	 * ipv6 format TCAM entries.
5021e689cf4aSJeff Kirsher 	 */
5022e689cf4aSJeff Kirsher 	memset(tp, 0, sizeof(*tp));
5023e689cf4aSJeff Kirsher 	tp->key[1] = TCAM_V4KEY1_NOPORT;
5024e689cf4aSJeff Kirsher 	tp->key_mask[1] = TCAM_V4KEY1_NOPORT;
5025e689cf4aSJeff Kirsher 	tp->assoc_data = (TCAM_ASSOCDATA_TRES_USE_OFFSET |
5026e689cf4aSJeff Kirsher 			  ((u64)0 << TCAM_ASSOCDATA_OFFSET_SHIFT));
5027e689cf4aSJeff Kirsher 	err = tcam_write(np, index, tp->key, tp->key_mask);
5028e689cf4aSJeff Kirsher 	if (err)
5029e689cf4aSJeff Kirsher 		return err;
5030e689cf4aSJeff Kirsher 	err = tcam_assoc_write(np, index, tp->assoc_data);
5031e689cf4aSJeff Kirsher 	if (err)
5032e689cf4aSJeff Kirsher 		return err;
5033e689cf4aSJeff Kirsher 	tp->valid = 1;
5034e689cf4aSJeff Kirsher 	cp->tcam_valid_entries++;
5035e689cf4aSJeff Kirsher 
5036e689cf4aSJeff Kirsher 	return 0;
5037e689cf4aSJeff Kirsher }
5038e689cf4aSJeff Kirsher 
niu_init_classifier_hw(struct niu * np)5039e689cf4aSJeff Kirsher static int niu_init_classifier_hw(struct niu *np)
5040e689cf4aSJeff Kirsher {
5041e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
5042e689cf4aSJeff Kirsher 	struct niu_classifier *cp = &np->clas;
5043e689cf4aSJeff Kirsher 	int i, err;
5044e689cf4aSJeff Kirsher 
5045e689cf4aSJeff Kirsher 	nw64(H1POLY, cp->h1_init);
5046e689cf4aSJeff Kirsher 	nw64(H2POLY, cp->h2_init);
5047e689cf4aSJeff Kirsher 
5048e689cf4aSJeff Kirsher 	err = niu_init_hostinfo(np);
5049e689cf4aSJeff Kirsher 	if (err)
5050e689cf4aSJeff Kirsher 		return err;
5051e689cf4aSJeff Kirsher 
5052e689cf4aSJeff Kirsher 	for (i = 0; i < ENET_VLAN_TBL_NUM_ENTRIES; i++) {
5053e689cf4aSJeff Kirsher 		struct niu_vlan_rdc *vp = &cp->vlan_mappings[i];
5054e689cf4aSJeff Kirsher 
5055e689cf4aSJeff Kirsher 		vlan_tbl_write(np, i, np->port,
5056e689cf4aSJeff Kirsher 			       vp->vlan_pref, vp->rdc_num);
5057e689cf4aSJeff Kirsher 	}
5058e689cf4aSJeff Kirsher 
5059e689cf4aSJeff Kirsher 	for (i = 0; i < cp->num_alt_mac_mappings; i++) {
5060e689cf4aSJeff Kirsher 		struct niu_altmac_rdc *ap = &cp->alt_mac_mappings[i];
5061e689cf4aSJeff Kirsher 
5062e689cf4aSJeff Kirsher 		err = niu_set_alt_mac_rdc_table(np, ap->alt_mac_num,
5063e689cf4aSJeff Kirsher 						ap->rdc_num, ap->mac_pref);
5064e689cf4aSJeff Kirsher 		if (err)
5065e689cf4aSJeff Kirsher 			return err;
5066e689cf4aSJeff Kirsher 	}
5067e689cf4aSJeff Kirsher 
5068e689cf4aSJeff Kirsher 	for (i = CLASS_CODE_USER_PROG1; i <= CLASS_CODE_SCTP_IPV6; i++) {
5069e689cf4aSJeff Kirsher 		int index = i - CLASS_CODE_USER_PROG1;
5070e689cf4aSJeff Kirsher 
5071e689cf4aSJeff Kirsher 		err = niu_set_tcam_key(np, i, parent->tcam_key[index]);
5072e689cf4aSJeff Kirsher 		if (err)
5073e689cf4aSJeff Kirsher 			return err;
5074e689cf4aSJeff Kirsher 		err = niu_set_flow_key(np, i, parent->flow_key[index]);
5075e689cf4aSJeff Kirsher 		if (err)
5076e689cf4aSJeff Kirsher 			return err;
5077e689cf4aSJeff Kirsher 	}
5078e689cf4aSJeff Kirsher 
5079e689cf4aSJeff Kirsher 	err = niu_set_ip_frag_rule(np);
5080e689cf4aSJeff Kirsher 	if (err)
5081e689cf4aSJeff Kirsher 		return err;
5082e689cf4aSJeff Kirsher 
5083e689cf4aSJeff Kirsher 	tcam_enable(np, 1);
5084e689cf4aSJeff Kirsher 
5085e689cf4aSJeff Kirsher 	return 0;
5086e689cf4aSJeff Kirsher }
5087e689cf4aSJeff Kirsher 
niu_zcp_write(struct niu * np,int index,u64 * data)5088e689cf4aSJeff Kirsher static int niu_zcp_write(struct niu *np, int index, u64 *data)
5089e689cf4aSJeff Kirsher {
5090e689cf4aSJeff Kirsher 	nw64(ZCP_RAM_DATA0, data[0]);
5091e689cf4aSJeff Kirsher 	nw64(ZCP_RAM_DATA1, data[1]);
5092e689cf4aSJeff Kirsher 	nw64(ZCP_RAM_DATA2, data[2]);
5093e689cf4aSJeff Kirsher 	nw64(ZCP_RAM_DATA3, data[3]);
5094e689cf4aSJeff Kirsher 	nw64(ZCP_RAM_DATA4, data[4]);
5095e689cf4aSJeff Kirsher 	nw64(ZCP_RAM_BE, ZCP_RAM_BE_VAL);
5096e689cf4aSJeff Kirsher 	nw64(ZCP_RAM_ACC,
5097e689cf4aSJeff Kirsher 	     (ZCP_RAM_ACC_WRITE |
5098e689cf4aSJeff Kirsher 	      (0 << ZCP_RAM_ACC_ZFCID_SHIFT) |
5099e689cf4aSJeff Kirsher 	      (ZCP_RAM_SEL_CFIFO(np->port) << ZCP_RAM_ACC_RAM_SEL_SHIFT)));
5100e689cf4aSJeff Kirsher 
5101e689cf4aSJeff Kirsher 	return niu_wait_bits_clear(np, ZCP_RAM_ACC, ZCP_RAM_ACC_BUSY,
5102e689cf4aSJeff Kirsher 				   1000, 100);
5103e689cf4aSJeff Kirsher }
5104e689cf4aSJeff Kirsher 
niu_zcp_read(struct niu * np,int index,u64 * data)5105e689cf4aSJeff Kirsher static int niu_zcp_read(struct niu *np, int index, u64 *data)
5106e689cf4aSJeff Kirsher {
5107e689cf4aSJeff Kirsher 	int err;
5108e689cf4aSJeff Kirsher 
5109e689cf4aSJeff Kirsher 	err = niu_wait_bits_clear(np, ZCP_RAM_ACC, ZCP_RAM_ACC_BUSY,
5110e689cf4aSJeff Kirsher 				  1000, 100);
5111e689cf4aSJeff Kirsher 	if (err) {
5112e689cf4aSJeff Kirsher 		netdev_err(np->dev, "ZCP read busy won't clear, ZCP_RAM_ACC[%llx]\n",
5113e689cf4aSJeff Kirsher 			   (unsigned long long)nr64(ZCP_RAM_ACC));
5114e689cf4aSJeff Kirsher 		return err;
5115e689cf4aSJeff Kirsher 	}
5116e689cf4aSJeff Kirsher 
5117e689cf4aSJeff Kirsher 	nw64(ZCP_RAM_ACC,
5118e689cf4aSJeff Kirsher 	     (ZCP_RAM_ACC_READ |
5119e689cf4aSJeff Kirsher 	      (0 << ZCP_RAM_ACC_ZFCID_SHIFT) |
5120e689cf4aSJeff Kirsher 	      (ZCP_RAM_SEL_CFIFO(np->port) << ZCP_RAM_ACC_RAM_SEL_SHIFT)));
5121e689cf4aSJeff Kirsher 
5122e689cf4aSJeff Kirsher 	err = niu_wait_bits_clear(np, ZCP_RAM_ACC, ZCP_RAM_ACC_BUSY,
5123e689cf4aSJeff Kirsher 				  1000, 100);
5124e689cf4aSJeff Kirsher 	if (err) {
5125e689cf4aSJeff Kirsher 		netdev_err(np->dev, "ZCP read busy2 won't clear, ZCP_RAM_ACC[%llx]\n",
5126e689cf4aSJeff Kirsher 			   (unsigned long long)nr64(ZCP_RAM_ACC));
5127e689cf4aSJeff Kirsher 		return err;
5128e689cf4aSJeff Kirsher 	}
5129e689cf4aSJeff Kirsher 
5130e689cf4aSJeff Kirsher 	data[0] = nr64(ZCP_RAM_DATA0);
5131e689cf4aSJeff Kirsher 	data[1] = nr64(ZCP_RAM_DATA1);
5132e689cf4aSJeff Kirsher 	data[2] = nr64(ZCP_RAM_DATA2);
5133e689cf4aSJeff Kirsher 	data[3] = nr64(ZCP_RAM_DATA3);
5134e689cf4aSJeff Kirsher 	data[4] = nr64(ZCP_RAM_DATA4);
5135e689cf4aSJeff Kirsher 
5136e689cf4aSJeff Kirsher 	return 0;
5137e689cf4aSJeff Kirsher }
5138e689cf4aSJeff Kirsher 
niu_zcp_cfifo_reset(struct niu * np)5139e689cf4aSJeff Kirsher static void niu_zcp_cfifo_reset(struct niu *np)
5140e689cf4aSJeff Kirsher {
5141e689cf4aSJeff Kirsher 	u64 val = nr64(RESET_CFIFO);
5142e689cf4aSJeff Kirsher 
5143e689cf4aSJeff Kirsher 	val |= RESET_CFIFO_RST(np->port);
5144e689cf4aSJeff Kirsher 	nw64(RESET_CFIFO, val);
5145e689cf4aSJeff Kirsher 	udelay(10);
5146e689cf4aSJeff Kirsher 
5147e689cf4aSJeff Kirsher 	val &= ~RESET_CFIFO_RST(np->port);
5148e689cf4aSJeff Kirsher 	nw64(RESET_CFIFO, val);
5149e689cf4aSJeff Kirsher }
5150e689cf4aSJeff Kirsher 
niu_init_zcp(struct niu * np)5151e689cf4aSJeff Kirsher static int niu_init_zcp(struct niu *np)
5152e689cf4aSJeff Kirsher {
5153e689cf4aSJeff Kirsher 	u64 data[5], rbuf[5];
5154e689cf4aSJeff Kirsher 	int i, max, err;
5155e689cf4aSJeff Kirsher 
5156e689cf4aSJeff Kirsher 	if (np->parent->plat_type != PLAT_TYPE_NIU) {
5157e689cf4aSJeff Kirsher 		if (np->port == 0 || np->port == 1)
5158e689cf4aSJeff Kirsher 			max = ATLAS_P0_P1_CFIFO_ENTRIES;
5159e689cf4aSJeff Kirsher 		else
5160e689cf4aSJeff Kirsher 			max = ATLAS_P2_P3_CFIFO_ENTRIES;
5161e689cf4aSJeff Kirsher 	} else
5162e689cf4aSJeff Kirsher 		max = NIU_CFIFO_ENTRIES;
5163e689cf4aSJeff Kirsher 
5164e689cf4aSJeff Kirsher 	data[0] = 0;
5165e689cf4aSJeff Kirsher 	data[1] = 0;
5166e689cf4aSJeff Kirsher 	data[2] = 0;
5167e689cf4aSJeff Kirsher 	data[3] = 0;
5168e689cf4aSJeff Kirsher 	data[4] = 0;
5169e689cf4aSJeff Kirsher 
5170e689cf4aSJeff Kirsher 	for (i = 0; i < max; i++) {
5171e689cf4aSJeff Kirsher 		err = niu_zcp_write(np, i, data);
5172e689cf4aSJeff Kirsher 		if (err)
5173e689cf4aSJeff Kirsher 			return err;
5174e689cf4aSJeff Kirsher 		err = niu_zcp_read(np, i, rbuf);
5175e689cf4aSJeff Kirsher 		if (err)
5176e689cf4aSJeff Kirsher 			return err;
5177e689cf4aSJeff Kirsher 	}
5178e689cf4aSJeff Kirsher 
5179e689cf4aSJeff Kirsher 	niu_zcp_cfifo_reset(np);
5180e689cf4aSJeff Kirsher 	nw64(CFIFO_ECC(np->port), 0);
5181e689cf4aSJeff Kirsher 	nw64(ZCP_INT_STAT, ZCP_INT_STAT_ALL);
5182e689cf4aSJeff Kirsher 	(void) nr64(ZCP_INT_STAT);
5183e689cf4aSJeff Kirsher 	nw64(ZCP_INT_MASK, ZCP_INT_MASK_ALL);
5184e689cf4aSJeff Kirsher 
5185e689cf4aSJeff Kirsher 	return 0;
5186e689cf4aSJeff Kirsher }
5187e689cf4aSJeff Kirsher 
niu_ipp_write(struct niu * np,int index,u64 * data)5188e689cf4aSJeff Kirsher static void niu_ipp_write(struct niu *np, int index, u64 *data)
5189e689cf4aSJeff Kirsher {
5190e689cf4aSJeff Kirsher 	u64 val = nr64_ipp(IPP_CFIG);
5191e689cf4aSJeff Kirsher 
5192e689cf4aSJeff Kirsher 	nw64_ipp(IPP_CFIG, val | IPP_CFIG_DFIFO_PIO_W);
5193e689cf4aSJeff Kirsher 	nw64_ipp(IPP_DFIFO_WR_PTR, index);
5194e689cf4aSJeff Kirsher 	nw64_ipp(IPP_DFIFO_WR0, data[0]);
5195e689cf4aSJeff Kirsher 	nw64_ipp(IPP_DFIFO_WR1, data[1]);
5196e689cf4aSJeff Kirsher 	nw64_ipp(IPP_DFIFO_WR2, data[2]);
5197e689cf4aSJeff Kirsher 	nw64_ipp(IPP_DFIFO_WR3, data[3]);
5198e689cf4aSJeff Kirsher 	nw64_ipp(IPP_DFIFO_WR4, data[4]);
5199e689cf4aSJeff Kirsher 	nw64_ipp(IPP_CFIG, val & ~IPP_CFIG_DFIFO_PIO_W);
5200e689cf4aSJeff Kirsher }
5201e689cf4aSJeff Kirsher 
niu_ipp_read(struct niu * np,int index,u64 * data)5202e689cf4aSJeff Kirsher static void niu_ipp_read(struct niu *np, int index, u64 *data)
5203e689cf4aSJeff Kirsher {
5204e689cf4aSJeff Kirsher 	nw64_ipp(IPP_DFIFO_RD_PTR, index);
5205e689cf4aSJeff Kirsher 	data[0] = nr64_ipp(IPP_DFIFO_RD0);
5206e689cf4aSJeff Kirsher 	data[1] = nr64_ipp(IPP_DFIFO_RD1);
5207e689cf4aSJeff Kirsher 	data[2] = nr64_ipp(IPP_DFIFO_RD2);
5208e689cf4aSJeff Kirsher 	data[3] = nr64_ipp(IPP_DFIFO_RD3);
5209e689cf4aSJeff Kirsher 	data[4] = nr64_ipp(IPP_DFIFO_RD4);
5210e689cf4aSJeff Kirsher }
5211e689cf4aSJeff Kirsher 
niu_ipp_reset(struct niu * np)5212e689cf4aSJeff Kirsher static int niu_ipp_reset(struct niu *np)
5213e689cf4aSJeff Kirsher {
5214e689cf4aSJeff Kirsher 	return niu_set_and_wait_clear_ipp(np, IPP_CFIG, IPP_CFIG_SOFT_RST,
5215e689cf4aSJeff Kirsher 					  1000, 100, "IPP_CFIG");
5216e689cf4aSJeff Kirsher }
5217e689cf4aSJeff Kirsher 
niu_init_ipp(struct niu * np)5218e689cf4aSJeff Kirsher static int niu_init_ipp(struct niu *np)
5219e689cf4aSJeff Kirsher {
5220e689cf4aSJeff Kirsher 	u64 data[5], rbuf[5], val;
5221e689cf4aSJeff Kirsher 	int i, max, err;
5222e689cf4aSJeff Kirsher 
5223e689cf4aSJeff Kirsher 	if (np->parent->plat_type != PLAT_TYPE_NIU) {
5224e689cf4aSJeff Kirsher 		if (np->port == 0 || np->port == 1)
5225e689cf4aSJeff Kirsher 			max = ATLAS_P0_P1_DFIFO_ENTRIES;
5226e689cf4aSJeff Kirsher 		else
5227e689cf4aSJeff Kirsher 			max = ATLAS_P2_P3_DFIFO_ENTRIES;
5228e689cf4aSJeff Kirsher 	} else
5229e689cf4aSJeff Kirsher 		max = NIU_DFIFO_ENTRIES;
5230e689cf4aSJeff Kirsher 
5231e689cf4aSJeff Kirsher 	data[0] = 0;
5232e689cf4aSJeff Kirsher 	data[1] = 0;
5233e689cf4aSJeff Kirsher 	data[2] = 0;
5234e689cf4aSJeff Kirsher 	data[3] = 0;
5235e689cf4aSJeff Kirsher 	data[4] = 0;
5236e689cf4aSJeff Kirsher 
5237e689cf4aSJeff Kirsher 	for (i = 0; i < max; i++) {
5238e689cf4aSJeff Kirsher 		niu_ipp_write(np, i, data);
5239e689cf4aSJeff Kirsher 		niu_ipp_read(np, i, rbuf);
5240e689cf4aSJeff Kirsher 	}
5241e689cf4aSJeff Kirsher 
5242e689cf4aSJeff Kirsher 	(void) nr64_ipp(IPP_INT_STAT);
5243e689cf4aSJeff Kirsher 	(void) nr64_ipp(IPP_INT_STAT);
5244e689cf4aSJeff Kirsher 
5245e689cf4aSJeff Kirsher 	err = niu_ipp_reset(np);
5246e689cf4aSJeff Kirsher 	if (err)
5247e689cf4aSJeff Kirsher 		return err;
5248e689cf4aSJeff Kirsher 
5249e689cf4aSJeff Kirsher 	(void) nr64_ipp(IPP_PKT_DIS);
5250e689cf4aSJeff Kirsher 	(void) nr64_ipp(IPP_BAD_CS_CNT);
5251e689cf4aSJeff Kirsher 	(void) nr64_ipp(IPP_ECC);
5252e689cf4aSJeff Kirsher 
5253e689cf4aSJeff Kirsher 	(void) nr64_ipp(IPP_INT_STAT);
5254e689cf4aSJeff Kirsher 
5255e689cf4aSJeff Kirsher 	nw64_ipp(IPP_MSK, ~IPP_MSK_ALL);
5256e689cf4aSJeff Kirsher 
5257e689cf4aSJeff Kirsher 	val = nr64_ipp(IPP_CFIG);
5258e689cf4aSJeff Kirsher 	val &= ~IPP_CFIG_IP_MAX_PKT;
5259e689cf4aSJeff Kirsher 	val |= (IPP_CFIG_IPP_ENABLE |
5260e689cf4aSJeff Kirsher 		IPP_CFIG_DFIFO_ECC_EN |
5261e689cf4aSJeff Kirsher 		IPP_CFIG_DROP_BAD_CRC |
5262e689cf4aSJeff Kirsher 		IPP_CFIG_CKSUM_EN |
5263e689cf4aSJeff Kirsher 		(0x1ffff << IPP_CFIG_IP_MAX_PKT_SHIFT));
5264e689cf4aSJeff Kirsher 	nw64_ipp(IPP_CFIG, val);
5265e689cf4aSJeff Kirsher 
5266e689cf4aSJeff Kirsher 	return 0;
5267e689cf4aSJeff Kirsher }
5268e689cf4aSJeff Kirsher 
niu_handle_led(struct niu * np,int status)5269e689cf4aSJeff Kirsher static void niu_handle_led(struct niu *np, int status)
5270e689cf4aSJeff Kirsher {
5271e689cf4aSJeff Kirsher 	u64 val;
5272e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_CONFIG);
5273e689cf4aSJeff Kirsher 
5274e689cf4aSJeff Kirsher 	if ((np->flags & NIU_FLAGS_10G) != 0 &&
5275e689cf4aSJeff Kirsher 	    (np->flags & NIU_FLAGS_FIBER) != 0) {
5276e689cf4aSJeff Kirsher 		if (status) {
5277e689cf4aSJeff Kirsher 			val |= XMAC_CONFIG_LED_POLARITY;
5278e689cf4aSJeff Kirsher 			val &= ~XMAC_CONFIG_FORCE_LED_ON;
5279e689cf4aSJeff Kirsher 		} else {
5280e689cf4aSJeff Kirsher 			val |= XMAC_CONFIG_FORCE_LED_ON;
5281e689cf4aSJeff Kirsher 			val &= ~XMAC_CONFIG_LED_POLARITY;
5282e689cf4aSJeff Kirsher 		}
5283e689cf4aSJeff Kirsher 	}
5284e689cf4aSJeff Kirsher 
5285e689cf4aSJeff Kirsher 	nw64_mac(XMAC_CONFIG, val);
5286e689cf4aSJeff Kirsher }
5287e689cf4aSJeff Kirsher 
niu_init_xif_xmac(struct niu * np)5288e689cf4aSJeff Kirsher static void niu_init_xif_xmac(struct niu *np)
5289e689cf4aSJeff Kirsher {
5290e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
5291e689cf4aSJeff Kirsher 	u64 val;
5292e689cf4aSJeff Kirsher 
5293e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XCVR_SERDES) {
5294e689cf4aSJeff Kirsher 		val = nr64(MIF_CONFIG);
5295e689cf4aSJeff Kirsher 		val |= MIF_CONFIG_ATCA_GE;
5296e689cf4aSJeff Kirsher 		nw64(MIF_CONFIG, val);
5297e689cf4aSJeff Kirsher 	}
5298e689cf4aSJeff Kirsher 
5299e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_CONFIG);
5300e689cf4aSJeff Kirsher 	val &= ~XMAC_CONFIG_SEL_POR_CLK_SRC;
5301e689cf4aSJeff Kirsher 
5302e689cf4aSJeff Kirsher 	val |= XMAC_CONFIG_TX_OUTPUT_EN;
5303e689cf4aSJeff Kirsher 
5304e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_MAC) {
5305e689cf4aSJeff Kirsher 		val &= ~XMAC_CONFIG_SEL_POR_CLK_SRC;
5306e689cf4aSJeff Kirsher 		val |= XMAC_CONFIG_LOOPBACK;
5307e689cf4aSJeff Kirsher 	} else {
5308e689cf4aSJeff Kirsher 		val &= ~XMAC_CONFIG_LOOPBACK;
5309e689cf4aSJeff Kirsher 	}
5310e689cf4aSJeff Kirsher 
5311e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_10G) {
5312e689cf4aSJeff Kirsher 		val &= ~XMAC_CONFIG_LFS_DISABLE;
5313e689cf4aSJeff Kirsher 	} else {
5314e689cf4aSJeff Kirsher 		val |= XMAC_CONFIG_LFS_DISABLE;
5315e689cf4aSJeff Kirsher 		if (!(np->flags & NIU_FLAGS_FIBER) &&
5316e689cf4aSJeff Kirsher 		    !(np->flags & NIU_FLAGS_XCVR_SERDES))
5317e689cf4aSJeff Kirsher 			val |= XMAC_CONFIG_1G_PCS_BYPASS;
5318e689cf4aSJeff Kirsher 		else
5319e689cf4aSJeff Kirsher 			val &= ~XMAC_CONFIG_1G_PCS_BYPASS;
5320e689cf4aSJeff Kirsher 	}
5321e689cf4aSJeff Kirsher 
5322e689cf4aSJeff Kirsher 	val &= ~XMAC_CONFIG_10G_XPCS_BYPASS;
5323e689cf4aSJeff Kirsher 
5324e689cf4aSJeff Kirsher 	if (lp->active_speed == SPEED_100)
5325e689cf4aSJeff Kirsher 		val |= XMAC_CONFIG_SEL_CLK_25MHZ;
5326e689cf4aSJeff Kirsher 	else
5327e689cf4aSJeff Kirsher 		val &= ~XMAC_CONFIG_SEL_CLK_25MHZ;
5328e689cf4aSJeff Kirsher 
5329e689cf4aSJeff Kirsher 	nw64_mac(XMAC_CONFIG, val);
5330e689cf4aSJeff Kirsher 
5331e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_CONFIG);
5332e689cf4aSJeff Kirsher 	val &= ~XMAC_CONFIG_MODE_MASK;
5333e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_10G) {
5334e689cf4aSJeff Kirsher 		val |= XMAC_CONFIG_MODE_XGMII;
5335e689cf4aSJeff Kirsher 	} else {
5336e689cf4aSJeff Kirsher 		if (lp->active_speed == SPEED_1000)
5337e689cf4aSJeff Kirsher 			val |= XMAC_CONFIG_MODE_GMII;
5338e689cf4aSJeff Kirsher 		else
5339e689cf4aSJeff Kirsher 			val |= XMAC_CONFIG_MODE_MII;
5340e689cf4aSJeff Kirsher 	}
5341e689cf4aSJeff Kirsher 
5342e689cf4aSJeff Kirsher 	nw64_mac(XMAC_CONFIG, val);
5343e689cf4aSJeff Kirsher }
5344e689cf4aSJeff Kirsher 
niu_init_xif_bmac(struct niu * np)5345e689cf4aSJeff Kirsher static void niu_init_xif_bmac(struct niu *np)
5346e689cf4aSJeff Kirsher {
5347e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
5348e689cf4aSJeff Kirsher 	u64 val;
5349e689cf4aSJeff Kirsher 
5350e689cf4aSJeff Kirsher 	val = BMAC_XIF_CONFIG_TX_OUTPUT_EN;
5351e689cf4aSJeff Kirsher 
5352e689cf4aSJeff Kirsher 	if (lp->loopback_mode == LOOPBACK_MAC)
5353e689cf4aSJeff Kirsher 		val |= BMAC_XIF_CONFIG_MII_LOOPBACK;
5354e689cf4aSJeff Kirsher 	else
5355e689cf4aSJeff Kirsher 		val &= ~BMAC_XIF_CONFIG_MII_LOOPBACK;
5356e689cf4aSJeff Kirsher 
5357e689cf4aSJeff Kirsher 	if (lp->active_speed == SPEED_1000)
5358e689cf4aSJeff Kirsher 		val |= BMAC_XIF_CONFIG_GMII_MODE;
5359e689cf4aSJeff Kirsher 	else
5360e689cf4aSJeff Kirsher 		val &= ~BMAC_XIF_CONFIG_GMII_MODE;
5361e689cf4aSJeff Kirsher 
5362e689cf4aSJeff Kirsher 	val &= ~(BMAC_XIF_CONFIG_LINK_LED |
5363e689cf4aSJeff Kirsher 		 BMAC_XIF_CONFIG_LED_POLARITY);
5364e689cf4aSJeff Kirsher 
5365e689cf4aSJeff Kirsher 	if (!(np->flags & NIU_FLAGS_10G) &&
5366e689cf4aSJeff Kirsher 	    !(np->flags & NIU_FLAGS_FIBER) &&
5367e689cf4aSJeff Kirsher 	    lp->active_speed == SPEED_100)
5368e689cf4aSJeff Kirsher 		val |= BMAC_XIF_CONFIG_25MHZ_CLOCK;
5369e689cf4aSJeff Kirsher 	else
5370e689cf4aSJeff Kirsher 		val &= ~BMAC_XIF_CONFIG_25MHZ_CLOCK;
5371e689cf4aSJeff Kirsher 
5372e689cf4aSJeff Kirsher 	nw64_mac(BMAC_XIF_CONFIG, val);
5373e689cf4aSJeff Kirsher }
5374e689cf4aSJeff Kirsher 
niu_init_xif(struct niu * np)5375e689cf4aSJeff Kirsher static void niu_init_xif(struct niu *np)
5376e689cf4aSJeff Kirsher {
5377e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
5378e689cf4aSJeff Kirsher 		niu_init_xif_xmac(np);
5379e689cf4aSJeff Kirsher 	else
5380e689cf4aSJeff Kirsher 		niu_init_xif_bmac(np);
5381e689cf4aSJeff Kirsher }
5382e689cf4aSJeff Kirsher 
niu_pcs_mii_reset(struct niu * np)5383e689cf4aSJeff Kirsher static void niu_pcs_mii_reset(struct niu *np)
5384e689cf4aSJeff Kirsher {
5385e689cf4aSJeff Kirsher 	int limit = 1000;
5386e689cf4aSJeff Kirsher 	u64 val = nr64_pcs(PCS_MII_CTL);
5387e689cf4aSJeff Kirsher 	val |= PCS_MII_CTL_RST;
5388e689cf4aSJeff Kirsher 	nw64_pcs(PCS_MII_CTL, val);
5389e689cf4aSJeff Kirsher 	while ((--limit >= 0) && (val & PCS_MII_CTL_RST)) {
5390e689cf4aSJeff Kirsher 		udelay(100);
5391e689cf4aSJeff Kirsher 		val = nr64_pcs(PCS_MII_CTL);
5392e689cf4aSJeff Kirsher 	}
5393e689cf4aSJeff Kirsher }
5394e689cf4aSJeff Kirsher 
niu_xpcs_reset(struct niu * np)5395e689cf4aSJeff Kirsher static void niu_xpcs_reset(struct niu *np)
5396e689cf4aSJeff Kirsher {
5397e689cf4aSJeff Kirsher 	int limit = 1000;
5398e689cf4aSJeff Kirsher 	u64 val = nr64_xpcs(XPCS_CONTROL1);
5399e689cf4aSJeff Kirsher 	val |= XPCS_CONTROL1_RESET;
5400e689cf4aSJeff Kirsher 	nw64_xpcs(XPCS_CONTROL1, val);
5401e689cf4aSJeff Kirsher 	while ((--limit >= 0) && (val & XPCS_CONTROL1_RESET)) {
5402e689cf4aSJeff Kirsher 		udelay(100);
5403e689cf4aSJeff Kirsher 		val = nr64_xpcs(XPCS_CONTROL1);
5404e689cf4aSJeff Kirsher 	}
5405e689cf4aSJeff Kirsher }
5406e689cf4aSJeff Kirsher 
niu_init_pcs(struct niu * np)5407e689cf4aSJeff Kirsher static int niu_init_pcs(struct niu *np)
5408e689cf4aSJeff Kirsher {
5409e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
5410e689cf4aSJeff Kirsher 	u64 val;
5411e689cf4aSJeff Kirsher 
5412e689cf4aSJeff Kirsher 	switch (np->flags & (NIU_FLAGS_10G |
5413e689cf4aSJeff Kirsher 			     NIU_FLAGS_FIBER |
5414e689cf4aSJeff Kirsher 			     NIU_FLAGS_XCVR_SERDES)) {
5415e689cf4aSJeff Kirsher 	case NIU_FLAGS_FIBER:
5416e689cf4aSJeff Kirsher 		/* 1G fiber */
5417e689cf4aSJeff Kirsher 		nw64_pcs(PCS_CONF, PCS_CONF_MASK | PCS_CONF_ENABLE);
5418e689cf4aSJeff Kirsher 		nw64_pcs(PCS_DPATH_MODE, 0);
5419e689cf4aSJeff Kirsher 		niu_pcs_mii_reset(np);
5420e689cf4aSJeff Kirsher 		break;
5421e689cf4aSJeff Kirsher 
5422e689cf4aSJeff Kirsher 	case NIU_FLAGS_10G:
5423e689cf4aSJeff Kirsher 	case NIU_FLAGS_10G | NIU_FLAGS_FIBER:
5424e689cf4aSJeff Kirsher 	case NIU_FLAGS_10G | NIU_FLAGS_XCVR_SERDES:
5425e689cf4aSJeff Kirsher 		/* 10G SERDES */
5426e689cf4aSJeff Kirsher 		if (!(np->flags & NIU_FLAGS_XMAC))
5427e689cf4aSJeff Kirsher 			return -EINVAL;
5428e689cf4aSJeff Kirsher 
5429e689cf4aSJeff Kirsher 		/* 10G copper or fiber */
5430e689cf4aSJeff Kirsher 		val = nr64_mac(XMAC_CONFIG);
5431e689cf4aSJeff Kirsher 		val &= ~XMAC_CONFIG_10G_XPCS_BYPASS;
5432e689cf4aSJeff Kirsher 		nw64_mac(XMAC_CONFIG, val);
5433e689cf4aSJeff Kirsher 
5434e689cf4aSJeff Kirsher 		niu_xpcs_reset(np);
5435e689cf4aSJeff Kirsher 
5436e689cf4aSJeff Kirsher 		val = nr64_xpcs(XPCS_CONTROL1);
5437e689cf4aSJeff Kirsher 		if (lp->loopback_mode == LOOPBACK_PHY)
5438e689cf4aSJeff Kirsher 			val |= XPCS_CONTROL1_LOOPBACK;
5439e689cf4aSJeff Kirsher 		else
5440e689cf4aSJeff Kirsher 			val &= ~XPCS_CONTROL1_LOOPBACK;
5441e689cf4aSJeff Kirsher 		nw64_xpcs(XPCS_CONTROL1, val);
5442e689cf4aSJeff Kirsher 
5443e689cf4aSJeff Kirsher 		nw64_xpcs(XPCS_DESKEW_ERR_CNT, 0);
5444e689cf4aSJeff Kirsher 		(void) nr64_xpcs(XPCS_SYMERR_CNT01);
5445e689cf4aSJeff Kirsher 		(void) nr64_xpcs(XPCS_SYMERR_CNT23);
5446e689cf4aSJeff Kirsher 		break;
5447e689cf4aSJeff Kirsher 
5448e689cf4aSJeff Kirsher 
5449e689cf4aSJeff Kirsher 	case NIU_FLAGS_XCVR_SERDES:
5450e689cf4aSJeff Kirsher 		/* 1G SERDES */
5451e689cf4aSJeff Kirsher 		niu_pcs_mii_reset(np);
5452e689cf4aSJeff Kirsher 		nw64_pcs(PCS_CONF, PCS_CONF_MASK | PCS_CONF_ENABLE);
5453e689cf4aSJeff Kirsher 		nw64_pcs(PCS_DPATH_MODE, 0);
5454e689cf4aSJeff Kirsher 		break;
5455e689cf4aSJeff Kirsher 
5456e689cf4aSJeff Kirsher 	case 0:
5457e689cf4aSJeff Kirsher 		/* 1G copper */
5458e689cf4aSJeff Kirsher 	case NIU_FLAGS_XCVR_SERDES | NIU_FLAGS_FIBER:
5459e689cf4aSJeff Kirsher 		/* 1G RGMII FIBER */
5460e689cf4aSJeff Kirsher 		nw64_pcs(PCS_DPATH_MODE, PCS_DPATH_MODE_MII);
5461e689cf4aSJeff Kirsher 		niu_pcs_mii_reset(np);
5462e689cf4aSJeff Kirsher 		break;
5463e689cf4aSJeff Kirsher 
5464e689cf4aSJeff Kirsher 	default:
5465e689cf4aSJeff Kirsher 		return -EINVAL;
5466e689cf4aSJeff Kirsher 	}
5467e689cf4aSJeff Kirsher 
5468e689cf4aSJeff Kirsher 	return 0;
5469e689cf4aSJeff Kirsher }
5470e689cf4aSJeff Kirsher 
niu_reset_tx_xmac(struct niu * np)5471e689cf4aSJeff Kirsher static int niu_reset_tx_xmac(struct niu *np)
5472e689cf4aSJeff Kirsher {
5473e689cf4aSJeff Kirsher 	return niu_set_and_wait_clear_mac(np, XTXMAC_SW_RST,
5474e689cf4aSJeff Kirsher 					  (XTXMAC_SW_RST_REG_RS |
5475e689cf4aSJeff Kirsher 					   XTXMAC_SW_RST_SOFT_RST),
5476e689cf4aSJeff Kirsher 					  1000, 100, "XTXMAC_SW_RST");
5477e689cf4aSJeff Kirsher }
5478e689cf4aSJeff Kirsher 
niu_reset_tx_bmac(struct niu * np)5479e689cf4aSJeff Kirsher static int niu_reset_tx_bmac(struct niu *np)
5480e689cf4aSJeff Kirsher {
5481e689cf4aSJeff Kirsher 	int limit;
5482e689cf4aSJeff Kirsher 
5483e689cf4aSJeff Kirsher 	nw64_mac(BTXMAC_SW_RST, BTXMAC_SW_RST_RESET);
5484e689cf4aSJeff Kirsher 	limit = 1000;
5485e689cf4aSJeff Kirsher 	while (--limit >= 0) {
5486e689cf4aSJeff Kirsher 		if (!(nr64_mac(BTXMAC_SW_RST) & BTXMAC_SW_RST_RESET))
5487e689cf4aSJeff Kirsher 			break;
5488e689cf4aSJeff Kirsher 		udelay(100);
5489e689cf4aSJeff Kirsher 	}
5490e689cf4aSJeff Kirsher 	if (limit < 0) {
5491e689cf4aSJeff Kirsher 		dev_err(np->device, "Port %u TX BMAC would not reset, BTXMAC_SW_RST[%llx]\n",
5492e689cf4aSJeff Kirsher 			np->port,
5493e689cf4aSJeff Kirsher 			(unsigned long long) nr64_mac(BTXMAC_SW_RST));
5494e689cf4aSJeff Kirsher 		return -ENODEV;
5495e689cf4aSJeff Kirsher 	}
5496e689cf4aSJeff Kirsher 
5497e689cf4aSJeff Kirsher 	return 0;
5498e689cf4aSJeff Kirsher }
5499e689cf4aSJeff Kirsher 
niu_reset_tx_mac(struct niu * np)5500e689cf4aSJeff Kirsher static int niu_reset_tx_mac(struct niu *np)
5501e689cf4aSJeff Kirsher {
5502e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
5503e689cf4aSJeff Kirsher 		return niu_reset_tx_xmac(np);
5504e689cf4aSJeff Kirsher 	else
5505e689cf4aSJeff Kirsher 		return niu_reset_tx_bmac(np);
5506e689cf4aSJeff Kirsher }
5507e689cf4aSJeff Kirsher 
niu_init_tx_xmac(struct niu * np,u64 min,u64 max)5508e689cf4aSJeff Kirsher static void niu_init_tx_xmac(struct niu *np, u64 min, u64 max)
5509e689cf4aSJeff Kirsher {
5510e689cf4aSJeff Kirsher 	u64 val;
5511e689cf4aSJeff Kirsher 
5512e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_MIN);
5513e689cf4aSJeff Kirsher 	val &= ~(XMAC_MIN_TX_MIN_PKT_SIZE |
5514e689cf4aSJeff Kirsher 		 XMAC_MIN_RX_MIN_PKT_SIZE);
5515e689cf4aSJeff Kirsher 	val |= (min << XMAC_MIN_RX_MIN_PKT_SIZE_SHFT);
5516e689cf4aSJeff Kirsher 	val |= (min << XMAC_MIN_TX_MIN_PKT_SIZE_SHFT);
5517e689cf4aSJeff Kirsher 	nw64_mac(XMAC_MIN, val);
5518e689cf4aSJeff Kirsher 
5519e689cf4aSJeff Kirsher 	nw64_mac(XMAC_MAX, max);
5520e689cf4aSJeff Kirsher 
5521e689cf4aSJeff Kirsher 	nw64_mac(XTXMAC_STAT_MSK, ~(u64)0);
5522e689cf4aSJeff Kirsher 
5523e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_IPG);
5524e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_10G) {
5525e689cf4aSJeff Kirsher 		val &= ~XMAC_IPG_IPG_XGMII;
5526e689cf4aSJeff Kirsher 		val |= (IPG_12_15_XGMII << XMAC_IPG_IPG_XGMII_SHIFT);
5527e689cf4aSJeff Kirsher 	} else {
5528e689cf4aSJeff Kirsher 		val &= ~XMAC_IPG_IPG_MII_GMII;
5529e689cf4aSJeff Kirsher 		val |= (IPG_12_MII_GMII << XMAC_IPG_IPG_MII_GMII_SHIFT);
5530e689cf4aSJeff Kirsher 	}
5531e689cf4aSJeff Kirsher 	nw64_mac(XMAC_IPG, val);
5532e689cf4aSJeff Kirsher 
5533e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_CONFIG);
5534e689cf4aSJeff Kirsher 	val &= ~(XMAC_CONFIG_ALWAYS_NO_CRC |
5535e689cf4aSJeff Kirsher 		 XMAC_CONFIG_STRETCH_MODE |
5536e689cf4aSJeff Kirsher 		 XMAC_CONFIG_VAR_MIN_IPG_EN |
5537e689cf4aSJeff Kirsher 		 XMAC_CONFIG_TX_ENABLE);
5538e689cf4aSJeff Kirsher 	nw64_mac(XMAC_CONFIG, val);
5539e689cf4aSJeff Kirsher 
5540e689cf4aSJeff Kirsher 	nw64_mac(TXMAC_FRM_CNT, 0);
5541e689cf4aSJeff Kirsher 	nw64_mac(TXMAC_BYTE_CNT, 0);
5542e689cf4aSJeff Kirsher }
5543e689cf4aSJeff Kirsher 
niu_init_tx_bmac(struct niu * np,u64 min,u64 max)5544e689cf4aSJeff Kirsher static void niu_init_tx_bmac(struct niu *np, u64 min, u64 max)
5545e689cf4aSJeff Kirsher {
5546e689cf4aSJeff Kirsher 	u64 val;
5547e689cf4aSJeff Kirsher 
5548e689cf4aSJeff Kirsher 	nw64_mac(BMAC_MIN_FRAME, min);
5549e689cf4aSJeff Kirsher 	nw64_mac(BMAC_MAX_FRAME, max);
5550e689cf4aSJeff Kirsher 
5551e689cf4aSJeff Kirsher 	nw64_mac(BTXMAC_STATUS_MASK, ~(u64)0);
5552e689cf4aSJeff Kirsher 	nw64_mac(BMAC_CTRL_TYPE, 0x8808);
5553e689cf4aSJeff Kirsher 	nw64_mac(BMAC_PREAMBLE_SIZE, 7);
5554e689cf4aSJeff Kirsher 
5555e689cf4aSJeff Kirsher 	val = nr64_mac(BTXMAC_CONFIG);
5556e689cf4aSJeff Kirsher 	val &= ~(BTXMAC_CONFIG_FCS_DISABLE |
5557e689cf4aSJeff Kirsher 		 BTXMAC_CONFIG_ENABLE);
5558e689cf4aSJeff Kirsher 	nw64_mac(BTXMAC_CONFIG, val);
5559e689cf4aSJeff Kirsher }
5560e689cf4aSJeff Kirsher 
niu_init_tx_mac(struct niu * np)5561e689cf4aSJeff Kirsher static void niu_init_tx_mac(struct niu *np)
5562e689cf4aSJeff Kirsher {
5563e689cf4aSJeff Kirsher 	u64 min, max;
5564e689cf4aSJeff Kirsher 
5565e689cf4aSJeff Kirsher 	min = 64;
5566e689cf4aSJeff Kirsher 	if (np->dev->mtu > ETH_DATA_LEN)
5567e689cf4aSJeff Kirsher 		max = 9216;
5568e689cf4aSJeff Kirsher 	else
5569e689cf4aSJeff Kirsher 		max = 1522;
5570e689cf4aSJeff Kirsher 
5571e689cf4aSJeff Kirsher 	/* The XMAC_MIN register only accepts values for TX min which
5572e689cf4aSJeff Kirsher 	 * have the low 3 bits cleared.
5573e689cf4aSJeff Kirsher 	 */
5574e689cf4aSJeff Kirsher 	BUG_ON(min & 0x7);
5575e689cf4aSJeff Kirsher 
5576e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
5577e689cf4aSJeff Kirsher 		niu_init_tx_xmac(np, min, max);
5578e689cf4aSJeff Kirsher 	else
5579e689cf4aSJeff Kirsher 		niu_init_tx_bmac(np, min, max);
5580e689cf4aSJeff Kirsher }
5581e689cf4aSJeff Kirsher 
niu_reset_rx_xmac(struct niu * np)5582e689cf4aSJeff Kirsher static int niu_reset_rx_xmac(struct niu *np)
5583e689cf4aSJeff Kirsher {
5584e689cf4aSJeff Kirsher 	int limit;
5585e689cf4aSJeff Kirsher 
5586e689cf4aSJeff Kirsher 	nw64_mac(XRXMAC_SW_RST,
5587e689cf4aSJeff Kirsher 		 XRXMAC_SW_RST_REG_RS | XRXMAC_SW_RST_SOFT_RST);
5588e689cf4aSJeff Kirsher 	limit = 1000;
5589e689cf4aSJeff Kirsher 	while (--limit >= 0) {
5590e689cf4aSJeff Kirsher 		if (!(nr64_mac(XRXMAC_SW_RST) & (XRXMAC_SW_RST_REG_RS |
5591e689cf4aSJeff Kirsher 						 XRXMAC_SW_RST_SOFT_RST)))
5592e689cf4aSJeff Kirsher 			break;
5593e689cf4aSJeff Kirsher 		udelay(100);
5594e689cf4aSJeff Kirsher 	}
5595e689cf4aSJeff Kirsher 	if (limit < 0) {
5596e689cf4aSJeff Kirsher 		dev_err(np->device, "Port %u RX XMAC would not reset, XRXMAC_SW_RST[%llx]\n",
5597e689cf4aSJeff Kirsher 			np->port,
5598e689cf4aSJeff Kirsher 			(unsigned long long) nr64_mac(XRXMAC_SW_RST));
5599e689cf4aSJeff Kirsher 		return -ENODEV;
5600e689cf4aSJeff Kirsher 	}
5601e689cf4aSJeff Kirsher 
5602e689cf4aSJeff Kirsher 	return 0;
5603e689cf4aSJeff Kirsher }
5604e689cf4aSJeff Kirsher 
niu_reset_rx_bmac(struct niu * np)5605e689cf4aSJeff Kirsher static int niu_reset_rx_bmac(struct niu *np)
5606e689cf4aSJeff Kirsher {
5607e689cf4aSJeff Kirsher 	int limit;
5608e689cf4aSJeff Kirsher 
5609e689cf4aSJeff Kirsher 	nw64_mac(BRXMAC_SW_RST, BRXMAC_SW_RST_RESET);
5610e689cf4aSJeff Kirsher 	limit = 1000;
5611e689cf4aSJeff Kirsher 	while (--limit >= 0) {
5612e689cf4aSJeff Kirsher 		if (!(nr64_mac(BRXMAC_SW_RST) & BRXMAC_SW_RST_RESET))
5613e689cf4aSJeff Kirsher 			break;
5614e689cf4aSJeff Kirsher 		udelay(100);
5615e689cf4aSJeff Kirsher 	}
5616e689cf4aSJeff Kirsher 	if (limit < 0) {
5617e689cf4aSJeff Kirsher 		dev_err(np->device, "Port %u RX BMAC would not reset, BRXMAC_SW_RST[%llx]\n",
5618e689cf4aSJeff Kirsher 			np->port,
5619e689cf4aSJeff Kirsher 			(unsigned long long) nr64_mac(BRXMAC_SW_RST));
5620e689cf4aSJeff Kirsher 		return -ENODEV;
5621e689cf4aSJeff Kirsher 	}
5622e689cf4aSJeff Kirsher 
5623e689cf4aSJeff Kirsher 	return 0;
5624e689cf4aSJeff Kirsher }
5625e689cf4aSJeff Kirsher 
niu_reset_rx_mac(struct niu * np)5626e689cf4aSJeff Kirsher static int niu_reset_rx_mac(struct niu *np)
5627e689cf4aSJeff Kirsher {
5628e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
5629e689cf4aSJeff Kirsher 		return niu_reset_rx_xmac(np);
5630e689cf4aSJeff Kirsher 	else
5631e689cf4aSJeff Kirsher 		return niu_reset_rx_bmac(np);
5632e689cf4aSJeff Kirsher }
5633e689cf4aSJeff Kirsher 
niu_init_rx_xmac(struct niu * np)5634e689cf4aSJeff Kirsher static void niu_init_rx_xmac(struct niu *np)
5635e689cf4aSJeff Kirsher {
5636e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
5637e689cf4aSJeff Kirsher 	struct niu_rdc_tables *tp = &parent->rdc_group_cfg[np->port];
5638e689cf4aSJeff Kirsher 	int first_rdc_table = tp->first_table_num;
5639e689cf4aSJeff Kirsher 	unsigned long i;
5640e689cf4aSJeff Kirsher 	u64 val;
5641e689cf4aSJeff Kirsher 
5642e689cf4aSJeff Kirsher 	nw64_mac(XMAC_ADD_FILT0, 0);
5643e689cf4aSJeff Kirsher 	nw64_mac(XMAC_ADD_FILT1, 0);
5644e689cf4aSJeff Kirsher 	nw64_mac(XMAC_ADD_FILT2, 0);
5645e689cf4aSJeff Kirsher 	nw64_mac(XMAC_ADD_FILT12_MASK, 0);
5646e689cf4aSJeff Kirsher 	nw64_mac(XMAC_ADD_FILT00_MASK, 0);
5647e689cf4aSJeff Kirsher 	for (i = 0; i < MAC_NUM_HASH; i++)
5648e689cf4aSJeff Kirsher 		nw64_mac(XMAC_HASH_TBL(i), 0);
5649e689cf4aSJeff Kirsher 	nw64_mac(XRXMAC_STAT_MSK, ~(u64)0);
5650e689cf4aSJeff Kirsher 	niu_set_primary_mac_rdc_table(np, first_rdc_table, 1);
5651e689cf4aSJeff Kirsher 	niu_set_multicast_mac_rdc_table(np, first_rdc_table, 1);
5652e689cf4aSJeff Kirsher 
5653e689cf4aSJeff Kirsher 	val = nr64_mac(XMAC_CONFIG);
5654e689cf4aSJeff Kirsher 	val &= ~(XMAC_CONFIG_RX_MAC_ENABLE |
5655e689cf4aSJeff Kirsher 		 XMAC_CONFIG_PROMISCUOUS |
5656e689cf4aSJeff Kirsher 		 XMAC_CONFIG_PROMISC_GROUP |
5657e689cf4aSJeff Kirsher 		 XMAC_CONFIG_ERR_CHK_DIS |
5658e689cf4aSJeff Kirsher 		 XMAC_CONFIG_RX_CRC_CHK_DIS |
5659e689cf4aSJeff Kirsher 		 XMAC_CONFIG_RESERVED_MULTICAST |
5660e689cf4aSJeff Kirsher 		 XMAC_CONFIG_RX_CODEV_CHK_DIS |
5661e689cf4aSJeff Kirsher 		 XMAC_CONFIG_ADDR_FILTER_EN |
5662e689cf4aSJeff Kirsher 		 XMAC_CONFIG_RCV_PAUSE_ENABLE |
5663e689cf4aSJeff Kirsher 		 XMAC_CONFIG_STRIP_CRC |
5664e689cf4aSJeff Kirsher 		 XMAC_CONFIG_PASS_FLOW_CTRL |
5665e689cf4aSJeff Kirsher 		 XMAC_CONFIG_MAC2IPP_PKT_CNT_EN);
5666e689cf4aSJeff Kirsher 	val |= (XMAC_CONFIG_HASH_FILTER_EN);
5667e689cf4aSJeff Kirsher 	nw64_mac(XMAC_CONFIG, val);
5668e689cf4aSJeff Kirsher 
5669e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_BT_CNT, 0);
5670e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_BC_FRM_CNT, 0);
5671e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_MC_FRM_CNT, 0);
5672e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_FRAG_CNT, 0);
5673e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_HIST_CNT1, 0);
5674e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_HIST_CNT2, 0);
5675e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_HIST_CNT3, 0);
5676e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_HIST_CNT4, 0);
5677e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_HIST_CNT5, 0);
5678e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_HIST_CNT6, 0);
5679e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_HIST_CNT7, 0);
5680e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_MPSZER_CNT, 0);
5681e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_CRC_ER_CNT, 0);
5682e689cf4aSJeff Kirsher 	nw64_mac(RXMAC_CD_VIO_CNT, 0);
5683e689cf4aSJeff Kirsher 	nw64_mac(LINK_FAULT_CNT, 0);
5684e689cf4aSJeff Kirsher }
5685e689cf4aSJeff Kirsher 
niu_init_rx_bmac(struct niu * np)5686e689cf4aSJeff Kirsher static void niu_init_rx_bmac(struct niu *np)
5687e689cf4aSJeff Kirsher {
5688e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
5689e689cf4aSJeff Kirsher 	struct niu_rdc_tables *tp = &parent->rdc_group_cfg[np->port];
5690e689cf4aSJeff Kirsher 	int first_rdc_table = tp->first_table_num;
5691e689cf4aSJeff Kirsher 	unsigned long i;
5692e689cf4aSJeff Kirsher 	u64 val;
5693e689cf4aSJeff Kirsher 
5694e689cf4aSJeff Kirsher 	nw64_mac(BMAC_ADD_FILT0, 0);
5695e689cf4aSJeff Kirsher 	nw64_mac(BMAC_ADD_FILT1, 0);
5696e689cf4aSJeff Kirsher 	nw64_mac(BMAC_ADD_FILT2, 0);
5697e689cf4aSJeff Kirsher 	nw64_mac(BMAC_ADD_FILT12_MASK, 0);
5698e689cf4aSJeff Kirsher 	nw64_mac(BMAC_ADD_FILT00_MASK, 0);
5699e689cf4aSJeff Kirsher 	for (i = 0; i < MAC_NUM_HASH; i++)
5700e689cf4aSJeff Kirsher 		nw64_mac(BMAC_HASH_TBL(i), 0);
5701e689cf4aSJeff Kirsher 	niu_set_primary_mac_rdc_table(np, first_rdc_table, 1);
5702e689cf4aSJeff Kirsher 	niu_set_multicast_mac_rdc_table(np, first_rdc_table, 1);
5703e689cf4aSJeff Kirsher 	nw64_mac(BRXMAC_STATUS_MASK, ~(u64)0);
5704e689cf4aSJeff Kirsher 
5705e689cf4aSJeff Kirsher 	val = nr64_mac(BRXMAC_CONFIG);
5706e689cf4aSJeff Kirsher 	val &= ~(BRXMAC_CONFIG_ENABLE |
5707e689cf4aSJeff Kirsher 		 BRXMAC_CONFIG_STRIP_PAD |
5708e689cf4aSJeff Kirsher 		 BRXMAC_CONFIG_STRIP_FCS |
5709e689cf4aSJeff Kirsher 		 BRXMAC_CONFIG_PROMISC |
5710e689cf4aSJeff Kirsher 		 BRXMAC_CONFIG_PROMISC_GRP |
5711e689cf4aSJeff Kirsher 		 BRXMAC_CONFIG_ADDR_FILT_EN |
5712e689cf4aSJeff Kirsher 		 BRXMAC_CONFIG_DISCARD_DIS);
5713e689cf4aSJeff Kirsher 	val |= (BRXMAC_CONFIG_HASH_FILT_EN);
5714e689cf4aSJeff Kirsher 	nw64_mac(BRXMAC_CONFIG, val);
5715e689cf4aSJeff Kirsher 
5716e689cf4aSJeff Kirsher 	val = nr64_mac(BMAC_ADDR_CMPEN);
5717e689cf4aSJeff Kirsher 	val |= BMAC_ADDR_CMPEN_EN0;
5718e689cf4aSJeff Kirsher 	nw64_mac(BMAC_ADDR_CMPEN, val);
5719e689cf4aSJeff Kirsher }
5720e689cf4aSJeff Kirsher 
niu_init_rx_mac(struct niu * np)5721e689cf4aSJeff Kirsher static void niu_init_rx_mac(struct niu *np)
5722e689cf4aSJeff Kirsher {
5723e689cf4aSJeff Kirsher 	niu_set_primary_mac(np, np->dev->dev_addr);
5724e689cf4aSJeff Kirsher 
5725e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
5726e689cf4aSJeff Kirsher 		niu_init_rx_xmac(np);
5727e689cf4aSJeff Kirsher 	else
5728e689cf4aSJeff Kirsher 		niu_init_rx_bmac(np);
5729e689cf4aSJeff Kirsher }
5730e689cf4aSJeff Kirsher 
niu_enable_tx_xmac(struct niu * np,int on)5731e689cf4aSJeff Kirsher static void niu_enable_tx_xmac(struct niu *np, int on)
5732e689cf4aSJeff Kirsher {
5733e689cf4aSJeff Kirsher 	u64 val = nr64_mac(XMAC_CONFIG);
5734e689cf4aSJeff Kirsher 
5735e689cf4aSJeff Kirsher 	if (on)
5736e689cf4aSJeff Kirsher 		val |= XMAC_CONFIG_TX_ENABLE;
5737e689cf4aSJeff Kirsher 	else
5738e689cf4aSJeff Kirsher 		val &= ~XMAC_CONFIG_TX_ENABLE;
5739e689cf4aSJeff Kirsher 	nw64_mac(XMAC_CONFIG, val);
5740e689cf4aSJeff Kirsher }
5741e689cf4aSJeff Kirsher 
niu_enable_tx_bmac(struct niu * np,int on)5742e689cf4aSJeff Kirsher static void niu_enable_tx_bmac(struct niu *np, int on)
5743e689cf4aSJeff Kirsher {
5744e689cf4aSJeff Kirsher 	u64 val = nr64_mac(BTXMAC_CONFIG);
5745e689cf4aSJeff Kirsher 
5746e689cf4aSJeff Kirsher 	if (on)
5747e689cf4aSJeff Kirsher 		val |= BTXMAC_CONFIG_ENABLE;
5748e689cf4aSJeff Kirsher 	else
5749e689cf4aSJeff Kirsher 		val &= ~BTXMAC_CONFIG_ENABLE;
5750e689cf4aSJeff Kirsher 	nw64_mac(BTXMAC_CONFIG, val);
5751e689cf4aSJeff Kirsher }
5752e689cf4aSJeff Kirsher 
niu_enable_tx_mac(struct niu * np,int on)5753e689cf4aSJeff Kirsher static void niu_enable_tx_mac(struct niu *np, int on)
5754e689cf4aSJeff Kirsher {
5755e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
5756e689cf4aSJeff Kirsher 		niu_enable_tx_xmac(np, on);
5757e689cf4aSJeff Kirsher 	else
5758e689cf4aSJeff Kirsher 		niu_enable_tx_bmac(np, on);
5759e689cf4aSJeff Kirsher }
5760e689cf4aSJeff Kirsher 
niu_enable_rx_xmac(struct niu * np,int on)5761e689cf4aSJeff Kirsher static void niu_enable_rx_xmac(struct niu *np, int on)
5762e689cf4aSJeff Kirsher {
5763e689cf4aSJeff Kirsher 	u64 val = nr64_mac(XMAC_CONFIG);
5764e689cf4aSJeff Kirsher 
5765e689cf4aSJeff Kirsher 	val &= ~(XMAC_CONFIG_HASH_FILTER_EN |
5766e689cf4aSJeff Kirsher 		 XMAC_CONFIG_PROMISCUOUS);
5767e689cf4aSJeff Kirsher 
5768e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_MCAST)
5769e689cf4aSJeff Kirsher 		val |= XMAC_CONFIG_HASH_FILTER_EN;
5770e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_PROMISC)
5771e689cf4aSJeff Kirsher 		val |= XMAC_CONFIG_PROMISCUOUS;
5772e689cf4aSJeff Kirsher 
5773e689cf4aSJeff Kirsher 	if (on)
5774e689cf4aSJeff Kirsher 		val |= XMAC_CONFIG_RX_MAC_ENABLE;
5775e689cf4aSJeff Kirsher 	else
5776e689cf4aSJeff Kirsher 		val &= ~XMAC_CONFIG_RX_MAC_ENABLE;
5777e689cf4aSJeff Kirsher 	nw64_mac(XMAC_CONFIG, val);
5778e689cf4aSJeff Kirsher }
5779e689cf4aSJeff Kirsher 
niu_enable_rx_bmac(struct niu * np,int on)5780e689cf4aSJeff Kirsher static void niu_enable_rx_bmac(struct niu *np, int on)
5781e689cf4aSJeff Kirsher {
5782e689cf4aSJeff Kirsher 	u64 val = nr64_mac(BRXMAC_CONFIG);
5783e689cf4aSJeff Kirsher 
5784e689cf4aSJeff Kirsher 	val &= ~(BRXMAC_CONFIG_HASH_FILT_EN |
5785e689cf4aSJeff Kirsher 		 BRXMAC_CONFIG_PROMISC);
5786e689cf4aSJeff Kirsher 
5787e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_MCAST)
5788e689cf4aSJeff Kirsher 		val |= BRXMAC_CONFIG_HASH_FILT_EN;
5789e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_PROMISC)
5790e689cf4aSJeff Kirsher 		val |= BRXMAC_CONFIG_PROMISC;
5791e689cf4aSJeff Kirsher 
5792e689cf4aSJeff Kirsher 	if (on)
5793e689cf4aSJeff Kirsher 		val |= BRXMAC_CONFIG_ENABLE;
5794e689cf4aSJeff Kirsher 	else
5795e689cf4aSJeff Kirsher 		val &= ~BRXMAC_CONFIG_ENABLE;
5796e689cf4aSJeff Kirsher 	nw64_mac(BRXMAC_CONFIG, val);
5797e689cf4aSJeff Kirsher }
5798e689cf4aSJeff Kirsher 
niu_enable_rx_mac(struct niu * np,int on)5799e689cf4aSJeff Kirsher static void niu_enable_rx_mac(struct niu *np, int on)
5800e689cf4aSJeff Kirsher {
5801e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
5802e689cf4aSJeff Kirsher 		niu_enable_rx_xmac(np, on);
5803e689cf4aSJeff Kirsher 	else
5804e689cf4aSJeff Kirsher 		niu_enable_rx_bmac(np, on);
5805e689cf4aSJeff Kirsher }
5806e689cf4aSJeff Kirsher 
niu_init_mac(struct niu * np)5807e689cf4aSJeff Kirsher static int niu_init_mac(struct niu *np)
5808e689cf4aSJeff Kirsher {
5809e689cf4aSJeff Kirsher 	int err;
5810e689cf4aSJeff Kirsher 
5811e689cf4aSJeff Kirsher 	niu_init_xif(np);
5812e689cf4aSJeff Kirsher 	err = niu_init_pcs(np);
5813e689cf4aSJeff Kirsher 	if (err)
5814e689cf4aSJeff Kirsher 		return err;
5815e689cf4aSJeff Kirsher 
5816e689cf4aSJeff Kirsher 	err = niu_reset_tx_mac(np);
5817e689cf4aSJeff Kirsher 	if (err)
5818e689cf4aSJeff Kirsher 		return err;
5819e689cf4aSJeff Kirsher 	niu_init_tx_mac(np);
5820e689cf4aSJeff Kirsher 	err = niu_reset_rx_mac(np);
5821e689cf4aSJeff Kirsher 	if (err)
5822e689cf4aSJeff Kirsher 		return err;
5823e689cf4aSJeff Kirsher 	niu_init_rx_mac(np);
5824e689cf4aSJeff Kirsher 
5825e689cf4aSJeff Kirsher 	/* This looks hookey but the RX MAC reset we just did will
5826e689cf4aSJeff Kirsher 	 * undo some of the state we setup in niu_init_tx_mac() so we
5827e689cf4aSJeff Kirsher 	 * have to call it again.  In particular, the RX MAC reset will
5828e689cf4aSJeff Kirsher 	 * set the XMAC_MAX register back to it's default value.
5829e689cf4aSJeff Kirsher 	 */
5830e689cf4aSJeff Kirsher 	niu_init_tx_mac(np);
5831e689cf4aSJeff Kirsher 	niu_enable_tx_mac(np, 1);
5832e689cf4aSJeff Kirsher 
5833e689cf4aSJeff Kirsher 	niu_enable_rx_mac(np, 1);
5834e689cf4aSJeff Kirsher 
5835e689cf4aSJeff Kirsher 	return 0;
5836e689cf4aSJeff Kirsher }
5837e689cf4aSJeff Kirsher 
niu_stop_one_tx_channel(struct niu * np,struct tx_ring_info * rp)5838e689cf4aSJeff Kirsher static void niu_stop_one_tx_channel(struct niu *np, struct tx_ring_info *rp)
5839e689cf4aSJeff Kirsher {
5840e689cf4aSJeff Kirsher 	(void) niu_tx_channel_stop(np, rp->tx_channel);
5841e689cf4aSJeff Kirsher }
5842e689cf4aSJeff Kirsher 
niu_stop_tx_channels(struct niu * np)5843e689cf4aSJeff Kirsher static void niu_stop_tx_channels(struct niu *np)
5844e689cf4aSJeff Kirsher {
5845e689cf4aSJeff Kirsher 	int i;
5846e689cf4aSJeff Kirsher 
5847e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_tx_rings; i++) {
5848e689cf4aSJeff Kirsher 		struct tx_ring_info *rp = &np->tx_rings[i];
5849e689cf4aSJeff Kirsher 
5850e689cf4aSJeff Kirsher 		niu_stop_one_tx_channel(np, rp);
5851e689cf4aSJeff Kirsher 	}
5852e689cf4aSJeff Kirsher }
5853e689cf4aSJeff Kirsher 
niu_reset_one_tx_channel(struct niu * np,struct tx_ring_info * rp)5854e689cf4aSJeff Kirsher static void niu_reset_one_tx_channel(struct niu *np, struct tx_ring_info *rp)
5855e689cf4aSJeff Kirsher {
5856e689cf4aSJeff Kirsher 	(void) niu_tx_channel_reset(np, rp->tx_channel);
5857e689cf4aSJeff Kirsher }
5858e689cf4aSJeff Kirsher 
niu_reset_tx_channels(struct niu * np)5859e689cf4aSJeff Kirsher static void niu_reset_tx_channels(struct niu *np)
5860e689cf4aSJeff Kirsher {
5861e689cf4aSJeff Kirsher 	int i;
5862e689cf4aSJeff Kirsher 
5863e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_tx_rings; i++) {
5864e689cf4aSJeff Kirsher 		struct tx_ring_info *rp = &np->tx_rings[i];
5865e689cf4aSJeff Kirsher 
5866e689cf4aSJeff Kirsher 		niu_reset_one_tx_channel(np, rp);
5867e689cf4aSJeff Kirsher 	}
5868e689cf4aSJeff Kirsher }
5869e689cf4aSJeff Kirsher 
niu_stop_one_rx_channel(struct niu * np,struct rx_ring_info * rp)5870e689cf4aSJeff Kirsher static void niu_stop_one_rx_channel(struct niu *np, struct rx_ring_info *rp)
5871e689cf4aSJeff Kirsher {
5872e689cf4aSJeff Kirsher 	(void) niu_enable_rx_channel(np, rp->rx_channel, 0);
5873e689cf4aSJeff Kirsher }
5874e689cf4aSJeff Kirsher 
niu_stop_rx_channels(struct niu * np)5875e689cf4aSJeff Kirsher static void niu_stop_rx_channels(struct niu *np)
5876e689cf4aSJeff Kirsher {
5877e689cf4aSJeff Kirsher 	int i;
5878e689cf4aSJeff Kirsher 
5879e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_rx_rings; i++) {
5880e689cf4aSJeff Kirsher 		struct rx_ring_info *rp = &np->rx_rings[i];
5881e689cf4aSJeff Kirsher 
5882e689cf4aSJeff Kirsher 		niu_stop_one_rx_channel(np, rp);
5883e689cf4aSJeff Kirsher 	}
5884e689cf4aSJeff Kirsher }
5885e689cf4aSJeff Kirsher 
niu_reset_one_rx_channel(struct niu * np,struct rx_ring_info * rp)5886e689cf4aSJeff Kirsher static void niu_reset_one_rx_channel(struct niu *np, struct rx_ring_info *rp)
5887e689cf4aSJeff Kirsher {
5888e689cf4aSJeff Kirsher 	int channel = rp->rx_channel;
5889e689cf4aSJeff Kirsher 
5890e689cf4aSJeff Kirsher 	(void) niu_rx_channel_reset(np, channel);
5891e689cf4aSJeff Kirsher 	nw64(RX_DMA_ENT_MSK(channel), RX_DMA_ENT_MSK_ALL);
5892e689cf4aSJeff Kirsher 	nw64(RX_DMA_CTL_STAT(channel), 0);
5893e689cf4aSJeff Kirsher 	(void) niu_enable_rx_channel(np, channel, 0);
5894e689cf4aSJeff Kirsher }
5895e689cf4aSJeff Kirsher 
niu_reset_rx_channels(struct niu * np)5896e689cf4aSJeff Kirsher static void niu_reset_rx_channels(struct niu *np)
5897e689cf4aSJeff Kirsher {
5898e689cf4aSJeff Kirsher 	int i;
5899e689cf4aSJeff Kirsher 
5900e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_rx_rings; i++) {
5901e689cf4aSJeff Kirsher 		struct rx_ring_info *rp = &np->rx_rings[i];
5902e689cf4aSJeff Kirsher 
5903e689cf4aSJeff Kirsher 		niu_reset_one_rx_channel(np, rp);
5904e689cf4aSJeff Kirsher 	}
5905e689cf4aSJeff Kirsher }
5906e689cf4aSJeff Kirsher 
niu_disable_ipp(struct niu * np)5907e689cf4aSJeff Kirsher static void niu_disable_ipp(struct niu *np)
5908e689cf4aSJeff Kirsher {
5909e689cf4aSJeff Kirsher 	u64 rd, wr, val;
5910e689cf4aSJeff Kirsher 	int limit;
5911e689cf4aSJeff Kirsher 
5912e689cf4aSJeff Kirsher 	rd = nr64_ipp(IPP_DFIFO_RD_PTR);
5913e689cf4aSJeff Kirsher 	wr = nr64_ipp(IPP_DFIFO_WR_PTR);
5914e689cf4aSJeff Kirsher 	limit = 100;
5915e689cf4aSJeff Kirsher 	while (--limit >= 0 && (rd != wr)) {
5916e689cf4aSJeff Kirsher 		rd = nr64_ipp(IPP_DFIFO_RD_PTR);
5917e689cf4aSJeff Kirsher 		wr = nr64_ipp(IPP_DFIFO_WR_PTR);
5918e689cf4aSJeff Kirsher 	}
5919e689cf4aSJeff Kirsher 	if (limit < 0 &&
5920e689cf4aSJeff Kirsher 	    (rd != 0 && wr != 1)) {
5921e689cf4aSJeff Kirsher 		netdev_err(np->dev, "IPP would not quiesce, rd_ptr[%llx] wr_ptr[%llx]\n",
5922e689cf4aSJeff Kirsher 			   (unsigned long long)nr64_ipp(IPP_DFIFO_RD_PTR),
5923e689cf4aSJeff Kirsher 			   (unsigned long long)nr64_ipp(IPP_DFIFO_WR_PTR));
5924e689cf4aSJeff Kirsher 	}
5925e689cf4aSJeff Kirsher 
5926e689cf4aSJeff Kirsher 	val = nr64_ipp(IPP_CFIG);
5927e689cf4aSJeff Kirsher 	val &= ~(IPP_CFIG_IPP_ENABLE |
5928e689cf4aSJeff Kirsher 		 IPP_CFIG_DFIFO_ECC_EN |
5929e689cf4aSJeff Kirsher 		 IPP_CFIG_DROP_BAD_CRC |
5930e689cf4aSJeff Kirsher 		 IPP_CFIG_CKSUM_EN);
5931e689cf4aSJeff Kirsher 	nw64_ipp(IPP_CFIG, val);
5932e689cf4aSJeff Kirsher 
5933e689cf4aSJeff Kirsher 	(void) niu_ipp_reset(np);
5934e689cf4aSJeff Kirsher }
5935e689cf4aSJeff Kirsher 
niu_init_hw(struct niu * np)5936e689cf4aSJeff Kirsher static int niu_init_hw(struct niu *np)
5937e689cf4aSJeff Kirsher {
5938e689cf4aSJeff Kirsher 	int i, err;
5939e689cf4aSJeff Kirsher 
5940e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize TXC\n");
5941e689cf4aSJeff Kirsher 	niu_txc_enable_port(np, 1);
5942e689cf4aSJeff Kirsher 	niu_txc_port_dma_enable(np, 1);
5943e689cf4aSJeff Kirsher 	niu_txc_set_imask(np, 0);
5944e689cf4aSJeff Kirsher 
5945e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize TX channels\n");
5946e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_tx_rings; i++) {
5947e689cf4aSJeff Kirsher 		struct tx_ring_info *rp = &np->tx_rings[i];
5948e689cf4aSJeff Kirsher 
5949e689cf4aSJeff Kirsher 		err = niu_init_one_tx_channel(np, rp);
5950e689cf4aSJeff Kirsher 		if (err)
5951e689cf4aSJeff Kirsher 			return err;
5952e689cf4aSJeff Kirsher 	}
5953e689cf4aSJeff Kirsher 
5954e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize RX channels\n");
5955e689cf4aSJeff Kirsher 	err = niu_init_rx_channels(np);
5956e689cf4aSJeff Kirsher 	if (err)
5957e689cf4aSJeff Kirsher 		goto out_uninit_tx_channels;
5958e689cf4aSJeff Kirsher 
5959e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize classifier\n");
5960e689cf4aSJeff Kirsher 	err = niu_init_classifier_hw(np);
5961e689cf4aSJeff Kirsher 	if (err)
5962e689cf4aSJeff Kirsher 		goto out_uninit_rx_channels;
5963e689cf4aSJeff Kirsher 
5964e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize ZCP\n");
5965e689cf4aSJeff Kirsher 	err = niu_init_zcp(np);
5966e689cf4aSJeff Kirsher 	if (err)
5967e689cf4aSJeff Kirsher 		goto out_uninit_rx_channels;
5968e689cf4aSJeff Kirsher 
5969e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize IPP\n");
5970e689cf4aSJeff Kirsher 	err = niu_init_ipp(np);
5971e689cf4aSJeff Kirsher 	if (err)
5972e689cf4aSJeff Kirsher 		goto out_uninit_rx_channels;
5973e689cf4aSJeff Kirsher 
5974e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize MAC\n");
5975e689cf4aSJeff Kirsher 	err = niu_init_mac(np);
5976e689cf4aSJeff Kirsher 	if (err)
5977e689cf4aSJeff Kirsher 		goto out_uninit_ipp;
5978e689cf4aSJeff Kirsher 
5979e689cf4aSJeff Kirsher 	return 0;
5980e689cf4aSJeff Kirsher 
5981e689cf4aSJeff Kirsher out_uninit_ipp:
5982e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Uninit IPP\n");
5983e689cf4aSJeff Kirsher 	niu_disable_ipp(np);
5984e689cf4aSJeff Kirsher 
5985e689cf4aSJeff Kirsher out_uninit_rx_channels:
5986e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Uninit RX channels\n");
5987e689cf4aSJeff Kirsher 	niu_stop_rx_channels(np);
5988e689cf4aSJeff Kirsher 	niu_reset_rx_channels(np);
5989e689cf4aSJeff Kirsher 
5990e689cf4aSJeff Kirsher out_uninit_tx_channels:
5991e689cf4aSJeff Kirsher 	netif_printk(np, ifup, KERN_DEBUG, np->dev, "Uninit TX channels\n");
5992e689cf4aSJeff Kirsher 	niu_stop_tx_channels(np);
5993e689cf4aSJeff Kirsher 	niu_reset_tx_channels(np);
5994e689cf4aSJeff Kirsher 
5995e689cf4aSJeff Kirsher 	return err;
5996e689cf4aSJeff Kirsher }
5997e689cf4aSJeff Kirsher 
niu_stop_hw(struct niu * np)5998e689cf4aSJeff Kirsher static void niu_stop_hw(struct niu *np)
5999e689cf4aSJeff Kirsher {
6000e689cf4aSJeff Kirsher 	netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Disable interrupts\n");
6001e689cf4aSJeff Kirsher 	niu_enable_interrupts(np, 0);
6002e689cf4aSJeff Kirsher 
6003e689cf4aSJeff Kirsher 	netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Disable RX MAC\n");
6004e689cf4aSJeff Kirsher 	niu_enable_rx_mac(np, 0);
6005e689cf4aSJeff Kirsher 
6006e689cf4aSJeff Kirsher 	netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Disable IPP\n");
6007e689cf4aSJeff Kirsher 	niu_disable_ipp(np);
6008e689cf4aSJeff Kirsher 
6009e689cf4aSJeff Kirsher 	netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Stop TX channels\n");
6010e689cf4aSJeff Kirsher 	niu_stop_tx_channels(np);
6011e689cf4aSJeff Kirsher 
6012e689cf4aSJeff Kirsher 	netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Stop RX channels\n");
6013e689cf4aSJeff Kirsher 	niu_stop_rx_channels(np);
6014e689cf4aSJeff Kirsher 
6015e689cf4aSJeff Kirsher 	netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Reset TX channels\n");
6016e689cf4aSJeff Kirsher 	niu_reset_tx_channels(np);
6017e689cf4aSJeff Kirsher 
6018e689cf4aSJeff Kirsher 	netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Reset RX channels\n");
6019e689cf4aSJeff Kirsher 	niu_reset_rx_channels(np);
6020e689cf4aSJeff Kirsher }
6021e689cf4aSJeff Kirsher 
niu_set_irq_name(struct niu * np)6022e689cf4aSJeff Kirsher static void niu_set_irq_name(struct niu *np)
6023e689cf4aSJeff Kirsher {
6024e689cf4aSJeff Kirsher 	int port = np->port;
6025e689cf4aSJeff Kirsher 	int i, j = 1;
6026e689cf4aSJeff Kirsher 
6027e689cf4aSJeff Kirsher 	sprintf(np->irq_name[0], "%s:MAC", np->dev->name);
6028e689cf4aSJeff Kirsher 
6029e689cf4aSJeff Kirsher 	if (port == 0) {
6030e689cf4aSJeff Kirsher 		sprintf(np->irq_name[1], "%s:MIF", np->dev->name);
6031e689cf4aSJeff Kirsher 		sprintf(np->irq_name[2], "%s:SYSERR", np->dev->name);
6032e689cf4aSJeff Kirsher 		j = 3;
6033e689cf4aSJeff Kirsher 	}
6034e689cf4aSJeff Kirsher 
6035e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_ldg - j; i++) {
6036e689cf4aSJeff Kirsher 		if (i < np->num_rx_rings)
6037e689cf4aSJeff Kirsher 			sprintf(np->irq_name[i+j], "%s-rx-%d",
6038e689cf4aSJeff Kirsher 				np->dev->name, i);
6039e689cf4aSJeff Kirsher 		else if (i < np->num_tx_rings + np->num_rx_rings)
6040e689cf4aSJeff Kirsher 			sprintf(np->irq_name[i+j], "%s-tx-%d", np->dev->name,
6041e689cf4aSJeff Kirsher 				i - np->num_rx_rings);
6042e689cf4aSJeff Kirsher 	}
6043e689cf4aSJeff Kirsher }
6044e689cf4aSJeff Kirsher 
niu_request_irq(struct niu * np)6045e689cf4aSJeff Kirsher static int niu_request_irq(struct niu *np)
6046e689cf4aSJeff Kirsher {
6047e689cf4aSJeff Kirsher 	int i, j, err;
6048e689cf4aSJeff Kirsher 
6049e689cf4aSJeff Kirsher 	niu_set_irq_name(np);
6050e689cf4aSJeff Kirsher 
6051e689cf4aSJeff Kirsher 	err = 0;
6052e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_ldg; i++) {
6053e689cf4aSJeff Kirsher 		struct niu_ldg *lp = &np->ldg[i];
6054e689cf4aSJeff Kirsher 
6055e689cf4aSJeff Kirsher 		err = request_irq(lp->irq, niu_interrupt, IRQF_SHARED,
6056e689cf4aSJeff Kirsher 				  np->irq_name[i], lp);
6057e689cf4aSJeff Kirsher 		if (err)
6058e689cf4aSJeff Kirsher 			goto out_free_irqs;
6059e689cf4aSJeff Kirsher 
6060e689cf4aSJeff Kirsher 	}
6061e689cf4aSJeff Kirsher 
6062e689cf4aSJeff Kirsher 	return 0;
6063e689cf4aSJeff Kirsher 
6064e689cf4aSJeff Kirsher out_free_irqs:
6065e689cf4aSJeff Kirsher 	for (j = 0; j < i; j++) {
6066e689cf4aSJeff Kirsher 		struct niu_ldg *lp = &np->ldg[j];
6067e689cf4aSJeff Kirsher 
6068e689cf4aSJeff Kirsher 		free_irq(lp->irq, lp);
6069e689cf4aSJeff Kirsher 	}
6070e689cf4aSJeff Kirsher 	return err;
6071e689cf4aSJeff Kirsher }
6072e689cf4aSJeff Kirsher 
niu_free_irq(struct niu * np)6073e689cf4aSJeff Kirsher static void niu_free_irq(struct niu *np)
6074e689cf4aSJeff Kirsher {
6075e689cf4aSJeff Kirsher 	int i;
6076e689cf4aSJeff Kirsher 
6077e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_ldg; i++) {
6078e689cf4aSJeff Kirsher 		struct niu_ldg *lp = &np->ldg[i];
6079e689cf4aSJeff Kirsher 
6080e689cf4aSJeff Kirsher 		free_irq(lp->irq, lp);
6081e689cf4aSJeff Kirsher 	}
6082e689cf4aSJeff Kirsher }
6083e689cf4aSJeff Kirsher 
niu_enable_napi(struct niu * np)6084e689cf4aSJeff Kirsher static void niu_enable_napi(struct niu *np)
6085e689cf4aSJeff Kirsher {
6086e689cf4aSJeff Kirsher 	int i;
6087e689cf4aSJeff Kirsher 
6088e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_ldg; i++)
6089e689cf4aSJeff Kirsher 		napi_enable(&np->ldg[i].napi);
6090e689cf4aSJeff Kirsher }
6091e689cf4aSJeff Kirsher 
niu_disable_napi(struct niu * np)6092e689cf4aSJeff Kirsher static void niu_disable_napi(struct niu *np)
6093e689cf4aSJeff Kirsher {
6094e689cf4aSJeff Kirsher 	int i;
6095e689cf4aSJeff Kirsher 
6096e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_ldg; i++)
6097e689cf4aSJeff Kirsher 		napi_disable(&np->ldg[i].napi);
6098e689cf4aSJeff Kirsher }
6099e689cf4aSJeff Kirsher 
niu_open(struct net_device * dev)6100e689cf4aSJeff Kirsher static int niu_open(struct net_device *dev)
6101e689cf4aSJeff Kirsher {
6102e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6103e689cf4aSJeff Kirsher 	int err;
6104e689cf4aSJeff Kirsher 
6105e689cf4aSJeff Kirsher 	netif_carrier_off(dev);
6106e689cf4aSJeff Kirsher 
6107e689cf4aSJeff Kirsher 	err = niu_alloc_channels(np);
6108e689cf4aSJeff Kirsher 	if (err)
6109e689cf4aSJeff Kirsher 		goto out_err;
6110e689cf4aSJeff Kirsher 
6111e689cf4aSJeff Kirsher 	err = niu_enable_interrupts(np, 0);
6112e689cf4aSJeff Kirsher 	if (err)
6113e689cf4aSJeff Kirsher 		goto out_free_channels;
6114e689cf4aSJeff Kirsher 
6115e689cf4aSJeff Kirsher 	err = niu_request_irq(np);
6116e689cf4aSJeff Kirsher 	if (err)
6117e689cf4aSJeff Kirsher 		goto out_free_channels;
6118e689cf4aSJeff Kirsher 
6119e689cf4aSJeff Kirsher 	niu_enable_napi(np);
6120e689cf4aSJeff Kirsher 
6121e689cf4aSJeff Kirsher 	spin_lock_irq(&np->lock);
6122e689cf4aSJeff Kirsher 
6123e689cf4aSJeff Kirsher 	err = niu_init_hw(np);
6124e689cf4aSJeff Kirsher 	if (!err) {
61250822c5d9SKees Cook 		timer_setup(&np->timer, niu_timer, 0);
6126e689cf4aSJeff Kirsher 		np->timer.expires = jiffies + HZ;
6127e689cf4aSJeff Kirsher 
6128e689cf4aSJeff Kirsher 		err = niu_enable_interrupts(np, 1);
6129e689cf4aSJeff Kirsher 		if (err)
6130e689cf4aSJeff Kirsher 			niu_stop_hw(np);
6131e689cf4aSJeff Kirsher 	}
6132e689cf4aSJeff Kirsher 
6133e689cf4aSJeff Kirsher 	spin_unlock_irq(&np->lock);
6134e689cf4aSJeff Kirsher 
6135e689cf4aSJeff Kirsher 	if (err) {
6136e689cf4aSJeff Kirsher 		niu_disable_napi(np);
6137e689cf4aSJeff Kirsher 		goto out_free_irq;
6138e689cf4aSJeff Kirsher 	}
6139e689cf4aSJeff Kirsher 
6140e689cf4aSJeff Kirsher 	netif_tx_start_all_queues(dev);
6141e689cf4aSJeff Kirsher 
6142e689cf4aSJeff Kirsher 	if (np->link_config.loopback_mode != LOOPBACK_DISABLED)
6143e689cf4aSJeff Kirsher 		netif_carrier_on(dev);
6144e689cf4aSJeff Kirsher 
6145e689cf4aSJeff Kirsher 	add_timer(&np->timer);
6146e689cf4aSJeff Kirsher 
6147e689cf4aSJeff Kirsher 	return 0;
6148e689cf4aSJeff Kirsher 
6149e689cf4aSJeff Kirsher out_free_irq:
6150e689cf4aSJeff Kirsher 	niu_free_irq(np);
6151e689cf4aSJeff Kirsher 
6152e689cf4aSJeff Kirsher out_free_channels:
6153e689cf4aSJeff Kirsher 	niu_free_channels(np);
6154e689cf4aSJeff Kirsher 
6155e689cf4aSJeff Kirsher out_err:
6156e689cf4aSJeff Kirsher 	return err;
6157e689cf4aSJeff Kirsher }
6158e689cf4aSJeff Kirsher 
niu_full_shutdown(struct niu * np,struct net_device * dev)6159e689cf4aSJeff Kirsher static void niu_full_shutdown(struct niu *np, struct net_device *dev)
6160e689cf4aSJeff Kirsher {
6161e689cf4aSJeff Kirsher 	cancel_work_sync(&np->reset_task);
6162e689cf4aSJeff Kirsher 
6163e689cf4aSJeff Kirsher 	niu_disable_napi(np);
6164e689cf4aSJeff Kirsher 	netif_tx_stop_all_queues(dev);
6165e689cf4aSJeff Kirsher 
6166e689cf4aSJeff Kirsher 	del_timer_sync(&np->timer);
6167e689cf4aSJeff Kirsher 
6168e689cf4aSJeff Kirsher 	spin_lock_irq(&np->lock);
6169e689cf4aSJeff Kirsher 
6170e689cf4aSJeff Kirsher 	niu_stop_hw(np);
6171e689cf4aSJeff Kirsher 
6172e689cf4aSJeff Kirsher 	spin_unlock_irq(&np->lock);
6173e689cf4aSJeff Kirsher }
6174e689cf4aSJeff Kirsher 
niu_close(struct net_device * dev)6175e689cf4aSJeff Kirsher static int niu_close(struct net_device *dev)
6176e689cf4aSJeff Kirsher {
6177e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6178e689cf4aSJeff Kirsher 
6179e689cf4aSJeff Kirsher 	niu_full_shutdown(np, dev);
6180e689cf4aSJeff Kirsher 
6181e689cf4aSJeff Kirsher 	niu_free_irq(np);
6182e689cf4aSJeff Kirsher 
6183e689cf4aSJeff Kirsher 	niu_free_channels(np);
6184e689cf4aSJeff Kirsher 
6185e689cf4aSJeff Kirsher 	niu_handle_led(np, 0);
6186e689cf4aSJeff Kirsher 
6187e689cf4aSJeff Kirsher 	return 0;
6188e689cf4aSJeff Kirsher }
6189e689cf4aSJeff Kirsher 
niu_sync_xmac_stats(struct niu * np)6190e689cf4aSJeff Kirsher static void niu_sync_xmac_stats(struct niu *np)
6191e689cf4aSJeff Kirsher {
6192e689cf4aSJeff Kirsher 	struct niu_xmac_stats *mp = &np->mac_stats.xmac;
6193e689cf4aSJeff Kirsher 
6194e689cf4aSJeff Kirsher 	mp->tx_frames += nr64_mac(TXMAC_FRM_CNT);
6195e689cf4aSJeff Kirsher 	mp->tx_bytes += nr64_mac(TXMAC_BYTE_CNT);
6196e689cf4aSJeff Kirsher 
6197e689cf4aSJeff Kirsher 	mp->rx_link_faults += nr64_mac(LINK_FAULT_CNT);
6198e689cf4aSJeff Kirsher 	mp->rx_align_errors += nr64_mac(RXMAC_ALIGN_ERR_CNT);
6199e689cf4aSJeff Kirsher 	mp->rx_frags += nr64_mac(RXMAC_FRAG_CNT);
6200e689cf4aSJeff Kirsher 	mp->rx_mcasts += nr64_mac(RXMAC_MC_FRM_CNT);
6201e689cf4aSJeff Kirsher 	mp->rx_bcasts += nr64_mac(RXMAC_BC_FRM_CNT);
6202e689cf4aSJeff Kirsher 	mp->rx_hist_cnt1 += nr64_mac(RXMAC_HIST_CNT1);
6203e689cf4aSJeff Kirsher 	mp->rx_hist_cnt2 += nr64_mac(RXMAC_HIST_CNT2);
6204e689cf4aSJeff Kirsher 	mp->rx_hist_cnt3 += nr64_mac(RXMAC_HIST_CNT3);
6205e689cf4aSJeff Kirsher 	mp->rx_hist_cnt4 += nr64_mac(RXMAC_HIST_CNT4);
6206e689cf4aSJeff Kirsher 	mp->rx_hist_cnt5 += nr64_mac(RXMAC_HIST_CNT5);
6207e689cf4aSJeff Kirsher 	mp->rx_hist_cnt6 += nr64_mac(RXMAC_HIST_CNT6);
6208e689cf4aSJeff Kirsher 	mp->rx_hist_cnt7 += nr64_mac(RXMAC_HIST_CNT7);
6209e689cf4aSJeff Kirsher 	mp->rx_octets += nr64_mac(RXMAC_BT_CNT);
6210e689cf4aSJeff Kirsher 	mp->rx_code_violations += nr64_mac(RXMAC_CD_VIO_CNT);
6211e689cf4aSJeff Kirsher 	mp->rx_len_errors += nr64_mac(RXMAC_MPSZER_CNT);
6212e689cf4aSJeff Kirsher 	mp->rx_crc_errors += nr64_mac(RXMAC_CRC_ER_CNT);
6213e689cf4aSJeff Kirsher }
6214e689cf4aSJeff Kirsher 
niu_sync_bmac_stats(struct niu * np)6215e689cf4aSJeff Kirsher static void niu_sync_bmac_stats(struct niu *np)
6216e689cf4aSJeff Kirsher {
6217e689cf4aSJeff Kirsher 	struct niu_bmac_stats *mp = &np->mac_stats.bmac;
6218e689cf4aSJeff Kirsher 
6219e689cf4aSJeff Kirsher 	mp->tx_bytes += nr64_mac(BTXMAC_BYTE_CNT);
6220e689cf4aSJeff Kirsher 	mp->tx_frames += nr64_mac(BTXMAC_FRM_CNT);
6221e689cf4aSJeff Kirsher 
6222e689cf4aSJeff Kirsher 	mp->rx_frames += nr64_mac(BRXMAC_FRAME_CNT);
6223e689cf4aSJeff Kirsher 	mp->rx_align_errors += nr64_mac(BRXMAC_ALIGN_ERR_CNT);
6224e689cf4aSJeff Kirsher 	mp->rx_crc_errors += nr64_mac(BRXMAC_ALIGN_ERR_CNT);
6225e689cf4aSJeff Kirsher 	mp->rx_len_errors += nr64_mac(BRXMAC_CODE_VIOL_ERR_CNT);
6226e689cf4aSJeff Kirsher }
6227e689cf4aSJeff Kirsher 
niu_sync_mac_stats(struct niu * np)6228e689cf4aSJeff Kirsher static void niu_sync_mac_stats(struct niu *np)
6229e689cf4aSJeff Kirsher {
6230e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
6231e689cf4aSJeff Kirsher 		niu_sync_xmac_stats(np);
6232e689cf4aSJeff Kirsher 	else
6233e689cf4aSJeff Kirsher 		niu_sync_bmac_stats(np);
6234e689cf4aSJeff Kirsher }
6235e689cf4aSJeff Kirsher 
niu_get_rx_stats(struct niu * np,struct rtnl_link_stats64 * stats)6236e689cf4aSJeff Kirsher static void niu_get_rx_stats(struct niu *np,
6237e689cf4aSJeff Kirsher 			     struct rtnl_link_stats64 *stats)
6238e689cf4aSJeff Kirsher {
6239e689cf4aSJeff Kirsher 	u64 pkts, dropped, errors, bytes;
6240e689cf4aSJeff Kirsher 	struct rx_ring_info *rx_rings;
6241e689cf4aSJeff Kirsher 	int i;
6242e689cf4aSJeff Kirsher 
6243e689cf4aSJeff Kirsher 	pkts = dropped = errors = bytes = 0;
6244e689cf4aSJeff Kirsher 
62456aa7de05SMark Rutland 	rx_rings = READ_ONCE(np->rx_rings);
6246e689cf4aSJeff Kirsher 	if (!rx_rings)
6247e689cf4aSJeff Kirsher 		goto no_rings;
6248e689cf4aSJeff Kirsher 
6249e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_rx_rings; i++) {
6250e689cf4aSJeff Kirsher 		struct rx_ring_info *rp = &rx_rings[i];
6251e689cf4aSJeff Kirsher 
6252e689cf4aSJeff Kirsher 		niu_sync_rx_discard_stats(np, rp, 0);
6253e689cf4aSJeff Kirsher 
6254e689cf4aSJeff Kirsher 		pkts += rp->rx_packets;
6255e689cf4aSJeff Kirsher 		bytes += rp->rx_bytes;
6256e689cf4aSJeff Kirsher 		dropped += rp->rx_dropped;
6257e689cf4aSJeff Kirsher 		errors += rp->rx_errors;
6258e689cf4aSJeff Kirsher 	}
6259e689cf4aSJeff Kirsher 
6260e689cf4aSJeff Kirsher no_rings:
6261e689cf4aSJeff Kirsher 	stats->rx_packets = pkts;
6262e689cf4aSJeff Kirsher 	stats->rx_bytes = bytes;
6263e689cf4aSJeff Kirsher 	stats->rx_dropped = dropped;
6264e689cf4aSJeff Kirsher 	stats->rx_errors = errors;
6265e689cf4aSJeff Kirsher }
6266e689cf4aSJeff Kirsher 
niu_get_tx_stats(struct niu * np,struct rtnl_link_stats64 * stats)6267e689cf4aSJeff Kirsher static void niu_get_tx_stats(struct niu *np,
6268e689cf4aSJeff Kirsher 			     struct rtnl_link_stats64 *stats)
6269e689cf4aSJeff Kirsher {
6270e689cf4aSJeff Kirsher 	u64 pkts, errors, bytes;
6271e689cf4aSJeff Kirsher 	struct tx_ring_info *tx_rings;
6272e689cf4aSJeff Kirsher 	int i;
6273e689cf4aSJeff Kirsher 
6274e689cf4aSJeff Kirsher 	pkts = errors = bytes = 0;
6275e689cf4aSJeff Kirsher 
62766aa7de05SMark Rutland 	tx_rings = READ_ONCE(np->tx_rings);
6277e689cf4aSJeff Kirsher 	if (!tx_rings)
6278e689cf4aSJeff Kirsher 		goto no_rings;
6279e689cf4aSJeff Kirsher 
6280e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_tx_rings; i++) {
6281e689cf4aSJeff Kirsher 		struct tx_ring_info *rp = &tx_rings[i];
6282e689cf4aSJeff Kirsher 
6283e689cf4aSJeff Kirsher 		pkts += rp->tx_packets;
6284e689cf4aSJeff Kirsher 		bytes += rp->tx_bytes;
6285e689cf4aSJeff Kirsher 		errors += rp->tx_errors;
6286e689cf4aSJeff Kirsher 	}
6287e689cf4aSJeff Kirsher 
6288e689cf4aSJeff Kirsher no_rings:
6289e689cf4aSJeff Kirsher 	stats->tx_packets = pkts;
6290e689cf4aSJeff Kirsher 	stats->tx_bytes = bytes;
6291e689cf4aSJeff Kirsher 	stats->tx_errors = errors;
6292e689cf4aSJeff Kirsher }
6293e689cf4aSJeff Kirsher 
niu_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)6294bc1f4470Sstephen hemminger static void niu_get_stats(struct net_device *dev,
6295e689cf4aSJeff Kirsher 			  struct rtnl_link_stats64 *stats)
6296e689cf4aSJeff Kirsher {
6297e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6298e689cf4aSJeff Kirsher 
6299e689cf4aSJeff Kirsher 	if (netif_running(dev)) {
6300e689cf4aSJeff Kirsher 		niu_get_rx_stats(np, stats);
6301e689cf4aSJeff Kirsher 		niu_get_tx_stats(np, stats);
6302e689cf4aSJeff Kirsher 	}
6303e689cf4aSJeff Kirsher }
6304e689cf4aSJeff Kirsher 
niu_load_hash_xmac(struct niu * np,u16 * hash)6305e689cf4aSJeff Kirsher static void niu_load_hash_xmac(struct niu *np, u16 *hash)
6306e689cf4aSJeff Kirsher {
6307e689cf4aSJeff Kirsher 	int i;
6308e689cf4aSJeff Kirsher 
6309e689cf4aSJeff Kirsher 	for (i = 0; i < 16; i++)
6310e689cf4aSJeff Kirsher 		nw64_mac(XMAC_HASH_TBL(i), hash[i]);
6311e689cf4aSJeff Kirsher }
6312e689cf4aSJeff Kirsher 
niu_load_hash_bmac(struct niu * np,u16 * hash)6313e689cf4aSJeff Kirsher static void niu_load_hash_bmac(struct niu *np, u16 *hash)
6314e689cf4aSJeff Kirsher {
6315e689cf4aSJeff Kirsher 	int i;
6316e689cf4aSJeff Kirsher 
6317e689cf4aSJeff Kirsher 	for (i = 0; i < 16; i++)
6318e689cf4aSJeff Kirsher 		nw64_mac(BMAC_HASH_TBL(i), hash[i]);
6319e689cf4aSJeff Kirsher }
6320e689cf4aSJeff Kirsher 
niu_load_hash(struct niu * np,u16 * hash)6321e689cf4aSJeff Kirsher static void niu_load_hash(struct niu *np, u16 *hash)
6322e689cf4aSJeff Kirsher {
6323e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
6324e689cf4aSJeff Kirsher 		niu_load_hash_xmac(np, hash);
6325e689cf4aSJeff Kirsher 	else
6326e689cf4aSJeff Kirsher 		niu_load_hash_bmac(np, hash);
6327e689cf4aSJeff Kirsher }
6328e689cf4aSJeff Kirsher 
niu_set_rx_mode(struct net_device * dev)6329e689cf4aSJeff Kirsher static void niu_set_rx_mode(struct net_device *dev)
6330e689cf4aSJeff Kirsher {
6331e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6332e689cf4aSJeff Kirsher 	int i, alt_cnt, err;
6333e689cf4aSJeff Kirsher 	struct netdev_hw_addr *ha;
6334e689cf4aSJeff Kirsher 	unsigned long flags;
6335e689cf4aSJeff Kirsher 	u16 hash[16] = { 0, };
6336e689cf4aSJeff Kirsher 
6337e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
6338e689cf4aSJeff Kirsher 	niu_enable_rx_mac(np, 0);
6339e689cf4aSJeff Kirsher 
6340e689cf4aSJeff Kirsher 	np->flags &= ~(NIU_FLAGS_MCAST | NIU_FLAGS_PROMISC);
6341e689cf4aSJeff Kirsher 	if (dev->flags & IFF_PROMISC)
6342e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_PROMISC;
6343e689cf4aSJeff Kirsher 	if ((dev->flags & IFF_ALLMULTI) || (!netdev_mc_empty(dev)))
6344e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_MCAST;
6345e689cf4aSJeff Kirsher 
6346e689cf4aSJeff Kirsher 	alt_cnt = netdev_uc_count(dev);
6347e689cf4aSJeff Kirsher 	if (alt_cnt > niu_num_alt_addr(np)) {
6348e689cf4aSJeff Kirsher 		alt_cnt = 0;
6349e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_PROMISC;
6350e689cf4aSJeff Kirsher 	}
6351e689cf4aSJeff Kirsher 
6352e689cf4aSJeff Kirsher 	if (alt_cnt) {
6353e689cf4aSJeff Kirsher 		int index = 0;
6354e689cf4aSJeff Kirsher 
6355e689cf4aSJeff Kirsher 		netdev_for_each_uc_addr(ha, dev) {
6356e689cf4aSJeff Kirsher 			err = niu_set_alt_mac(np, index, ha->addr);
6357e689cf4aSJeff Kirsher 			if (err)
6358e689cf4aSJeff Kirsher 				netdev_warn(dev, "Error %d adding alt mac %d\n",
6359e689cf4aSJeff Kirsher 					    err, index);
6360e689cf4aSJeff Kirsher 			err = niu_enable_alt_mac(np, index, 1);
6361e689cf4aSJeff Kirsher 			if (err)
6362e689cf4aSJeff Kirsher 				netdev_warn(dev, "Error %d enabling alt mac %d\n",
6363e689cf4aSJeff Kirsher 					    err, index);
6364e689cf4aSJeff Kirsher 
6365e689cf4aSJeff Kirsher 			index++;
6366e689cf4aSJeff Kirsher 		}
6367e689cf4aSJeff Kirsher 	} else {
6368e689cf4aSJeff Kirsher 		int alt_start;
6369e689cf4aSJeff Kirsher 		if (np->flags & NIU_FLAGS_XMAC)
6370e689cf4aSJeff Kirsher 			alt_start = 0;
6371e689cf4aSJeff Kirsher 		else
6372e689cf4aSJeff Kirsher 			alt_start = 1;
6373e689cf4aSJeff Kirsher 		for (i = alt_start; i < niu_num_alt_addr(np); i++) {
6374e689cf4aSJeff Kirsher 			err = niu_enable_alt_mac(np, i, 0);
6375e689cf4aSJeff Kirsher 			if (err)
6376e689cf4aSJeff Kirsher 				netdev_warn(dev, "Error %d disabling alt mac %d\n",
6377e689cf4aSJeff Kirsher 					    err, i);
6378e689cf4aSJeff Kirsher 		}
6379e689cf4aSJeff Kirsher 	}
6380e689cf4aSJeff Kirsher 	if (dev->flags & IFF_ALLMULTI) {
6381e689cf4aSJeff Kirsher 		for (i = 0; i < 16; i++)
6382e689cf4aSJeff Kirsher 			hash[i] = 0xffff;
6383e689cf4aSJeff Kirsher 	} else if (!netdev_mc_empty(dev)) {
6384e689cf4aSJeff Kirsher 		netdev_for_each_mc_addr(ha, dev) {
6385e689cf4aSJeff Kirsher 			u32 crc = ether_crc_le(ETH_ALEN, ha->addr);
6386e689cf4aSJeff Kirsher 
6387e689cf4aSJeff Kirsher 			crc >>= 24;
6388e689cf4aSJeff Kirsher 			hash[crc >> 4] |= (1 << (15 - (crc & 0xf)));
6389e689cf4aSJeff Kirsher 		}
6390e689cf4aSJeff Kirsher 	}
6391e689cf4aSJeff Kirsher 
6392e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_MCAST)
6393e689cf4aSJeff Kirsher 		niu_load_hash(np, hash);
6394e689cf4aSJeff Kirsher 
6395e689cf4aSJeff Kirsher 	niu_enable_rx_mac(np, 1);
6396e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
6397e689cf4aSJeff Kirsher }
6398e689cf4aSJeff Kirsher 
niu_set_mac_addr(struct net_device * dev,void * p)6399e689cf4aSJeff Kirsher static int niu_set_mac_addr(struct net_device *dev, void *p)
6400e689cf4aSJeff Kirsher {
6401e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6402e689cf4aSJeff Kirsher 	struct sockaddr *addr = p;
6403e689cf4aSJeff Kirsher 	unsigned long flags;
6404e689cf4aSJeff Kirsher 
6405e689cf4aSJeff Kirsher 	if (!is_valid_ether_addr(addr->sa_data))
6406504f9b5aSDanny Kukawka 		return -EADDRNOTAVAIL;
6407e689cf4aSJeff Kirsher 
6408a96d317fSJakub Kicinski 	eth_hw_addr_set(dev, addr->sa_data);
6409e689cf4aSJeff Kirsher 
6410e689cf4aSJeff Kirsher 	if (!netif_running(dev))
6411e689cf4aSJeff Kirsher 		return 0;
6412e689cf4aSJeff Kirsher 
6413e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
6414e689cf4aSJeff Kirsher 	niu_enable_rx_mac(np, 0);
6415e689cf4aSJeff Kirsher 	niu_set_primary_mac(np, dev->dev_addr);
6416e689cf4aSJeff Kirsher 	niu_enable_rx_mac(np, 1);
6417e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
6418e689cf4aSJeff Kirsher 
6419e689cf4aSJeff Kirsher 	return 0;
6420e689cf4aSJeff Kirsher }
6421e689cf4aSJeff Kirsher 
niu_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)6422e689cf4aSJeff Kirsher static int niu_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6423e689cf4aSJeff Kirsher {
6424e689cf4aSJeff Kirsher 	return -EOPNOTSUPP;
6425e689cf4aSJeff Kirsher }
6426e689cf4aSJeff Kirsher 
niu_netif_stop(struct niu * np)6427e689cf4aSJeff Kirsher static void niu_netif_stop(struct niu *np)
6428e689cf4aSJeff Kirsher {
6429860e9538SFlorian Westphal 	netif_trans_update(np->dev);	/* prevent tx timeout */
6430e689cf4aSJeff Kirsher 
6431e689cf4aSJeff Kirsher 	niu_disable_napi(np);
6432e689cf4aSJeff Kirsher 
6433e689cf4aSJeff Kirsher 	netif_tx_disable(np->dev);
6434e689cf4aSJeff Kirsher }
6435e689cf4aSJeff Kirsher 
niu_netif_start(struct niu * np)6436e689cf4aSJeff Kirsher static void niu_netif_start(struct niu *np)
6437e689cf4aSJeff Kirsher {
6438e689cf4aSJeff Kirsher 	/* NOTE: unconditional netif_wake_queue is only appropriate
6439e689cf4aSJeff Kirsher 	 * so long as all callers are assured to have free tx slots
6440e689cf4aSJeff Kirsher 	 * (such as after niu_init_hw).
6441e689cf4aSJeff Kirsher 	 */
6442e689cf4aSJeff Kirsher 	netif_tx_wake_all_queues(np->dev);
6443e689cf4aSJeff Kirsher 
6444e689cf4aSJeff Kirsher 	niu_enable_napi(np);
6445e689cf4aSJeff Kirsher 
6446e689cf4aSJeff Kirsher 	niu_enable_interrupts(np, 1);
6447e689cf4aSJeff Kirsher }
6448e689cf4aSJeff Kirsher 
niu_reset_buffers(struct niu * np)6449e689cf4aSJeff Kirsher static void niu_reset_buffers(struct niu *np)
6450e689cf4aSJeff Kirsher {
6451e689cf4aSJeff Kirsher 	int i, j, k, err;
6452e689cf4aSJeff Kirsher 
6453e689cf4aSJeff Kirsher 	if (np->rx_rings) {
6454e689cf4aSJeff Kirsher 		for (i = 0; i < np->num_rx_rings; i++) {
6455e689cf4aSJeff Kirsher 			struct rx_ring_info *rp = &np->rx_rings[i];
6456e689cf4aSJeff Kirsher 
6457e689cf4aSJeff Kirsher 			for (j = 0, k = 0; j < MAX_RBR_RING_SIZE; j++) {
6458e689cf4aSJeff Kirsher 				struct page *page;
6459e689cf4aSJeff Kirsher 
6460e689cf4aSJeff Kirsher 				page = rp->rxhash[j];
6461e689cf4aSJeff Kirsher 				while (page) {
64622dcfe9e2SKees Cook 					struct page *next = niu_next_page(page);
6463e689cf4aSJeff Kirsher 					u64 base = page->index;
6464e689cf4aSJeff Kirsher 					base = base >> RBR_DESCR_ADDR_SHIFT;
6465e689cf4aSJeff Kirsher 					rp->rbr[k++] = cpu_to_le32(base);
6466e689cf4aSJeff Kirsher 					page = next;
6467e689cf4aSJeff Kirsher 				}
6468e689cf4aSJeff Kirsher 			}
6469e689cf4aSJeff Kirsher 			for (; k < MAX_RBR_RING_SIZE; k++) {
6470e689cf4aSJeff Kirsher 				err = niu_rbr_add_page(np, rp, GFP_ATOMIC, k);
6471e689cf4aSJeff Kirsher 				if (unlikely(err))
6472e689cf4aSJeff Kirsher 					break;
6473e689cf4aSJeff Kirsher 			}
6474e689cf4aSJeff Kirsher 
6475e689cf4aSJeff Kirsher 			rp->rbr_index = rp->rbr_table_size - 1;
6476e689cf4aSJeff Kirsher 			rp->rcr_index = 0;
6477e689cf4aSJeff Kirsher 			rp->rbr_pending = 0;
6478e689cf4aSJeff Kirsher 			rp->rbr_refill_pending = 0;
6479e689cf4aSJeff Kirsher 		}
6480e689cf4aSJeff Kirsher 	}
6481e689cf4aSJeff Kirsher 	if (np->tx_rings) {
6482e689cf4aSJeff Kirsher 		for (i = 0; i < np->num_tx_rings; i++) {
6483e689cf4aSJeff Kirsher 			struct tx_ring_info *rp = &np->tx_rings[i];
6484e689cf4aSJeff Kirsher 
6485e689cf4aSJeff Kirsher 			for (j = 0; j < MAX_TX_RING_SIZE; j++) {
6486e689cf4aSJeff Kirsher 				if (rp->tx_buffs[j].skb)
6487e689cf4aSJeff Kirsher 					(void) release_tx_packet(np, rp, j);
6488e689cf4aSJeff Kirsher 			}
6489e689cf4aSJeff Kirsher 
6490e689cf4aSJeff Kirsher 			rp->pending = MAX_TX_RING_SIZE;
6491e689cf4aSJeff Kirsher 			rp->prod = 0;
6492e689cf4aSJeff Kirsher 			rp->cons = 0;
6493e689cf4aSJeff Kirsher 			rp->wrap_bit = 0;
6494e689cf4aSJeff Kirsher 		}
6495e689cf4aSJeff Kirsher 	}
6496e689cf4aSJeff Kirsher }
6497e689cf4aSJeff Kirsher 
niu_reset_task(struct work_struct * work)6498e689cf4aSJeff Kirsher static void niu_reset_task(struct work_struct *work)
6499e689cf4aSJeff Kirsher {
6500e689cf4aSJeff Kirsher 	struct niu *np = container_of(work, struct niu, reset_task);
6501e689cf4aSJeff Kirsher 	unsigned long flags;
6502e689cf4aSJeff Kirsher 	int err;
6503e689cf4aSJeff Kirsher 
6504e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
6505e689cf4aSJeff Kirsher 	if (!netif_running(np->dev)) {
6506e689cf4aSJeff Kirsher 		spin_unlock_irqrestore(&np->lock, flags);
6507e689cf4aSJeff Kirsher 		return;
6508e689cf4aSJeff Kirsher 	}
6509e689cf4aSJeff Kirsher 
6510e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
6511e689cf4aSJeff Kirsher 
6512e689cf4aSJeff Kirsher 	del_timer_sync(&np->timer);
6513e689cf4aSJeff Kirsher 
6514e689cf4aSJeff Kirsher 	niu_netif_stop(np);
6515e689cf4aSJeff Kirsher 
6516e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
6517e689cf4aSJeff Kirsher 
6518e689cf4aSJeff Kirsher 	niu_stop_hw(np);
6519e689cf4aSJeff Kirsher 
6520e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
6521e689cf4aSJeff Kirsher 
6522e689cf4aSJeff Kirsher 	niu_reset_buffers(np);
6523e689cf4aSJeff Kirsher 
6524e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
6525e689cf4aSJeff Kirsher 
6526e689cf4aSJeff Kirsher 	err = niu_init_hw(np);
6527e689cf4aSJeff Kirsher 	if (!err) {
6528e689cf4aSJeff Kirsher 		np->timer.expires = jiffies + HZ;
6529e689cf4aSJeff Kirsher 		add_timer(&np->timer);
6530e689cf4aSJeff Kirsher 		niu_netif_start(np);
6531e689cf4aSJeff Kirsher 	}
6532e689cf4aSJeff Kirsher 
6533e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
6534e689cf4aSJeff Kirsher }
6535e689cf4aSJeff Kirsher 
niu_tx_timeout(struct net_device * dev,unsigned int txqueue)65360290bd29SMichael S. Tsirkin static void niu_tx_timeout(struct net_device *dev, unsigned int txqueue)
6537e689cf4aSJeff Kirsher {
6538e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6539e689cf4aSJeff Kirsher 
6540e689cf4aSJeff Kirsher 	dev_err(np->device, "%s: Transmit timed out, resetting\n",
6541e689cf4aSJeff Kirsher 		dev->name);
6542e689cf4aSJeff Kirsher 
6543e689cf4aSJeff Kirsher 	schedule_work(&np->reset_task);
6544e689cf4aSJeff Kirsher }
6545e689cf4aSJeff Kirsher 
niu_set_txd(struct tx_ring_info * rp,int index,u64 mapping,u64 len,u64 mark,u64 n_frags)6546e689cf4aSJeff Kirsher static void niu_set_txd(struct tx_ring_info *rp, int index,
6547e689cf4aSJeff Kirsher 			u64 mapping, u64 len, u64 mark,
6548e689cf4aSJeff Kirsher 			u64 n_frags)
6549e689cf4aSJeff Kirsher {
6550e689cf4aSJeff Kirsher 	__le64 *desc = &rp->descr[index];
6551e689cf4aSJeff Kirsher 
6552e689cf4aSJeff Kirsher 	*desc = cpu_to_le64(mark |
6553e689cf4aSJeff Kirsher 			    (n_frags << TX_DESC_NUM_PTR_SHIFT) |
6554e689cf4aSJeff Kirsher 			    (len << TX_DESC_TR_LEN_SHIFT) |
6555e689cf4aSJeff Kirsher 			    (mapping & TX_DESC_SAD));
6556e689cf4aSJeff Kirsher }
6557e689cf4aSJeff Kirsher 
niu_compute_tx_flags(struct sk_buff * skb,struct ethhdr * ehdr,u64 pad_bytes,u64 len)6558e689cf4aSJeff Kirsher static u64 niu_compute_tx_flags(struct sk_buff *skb, struct ethhdr *ehdr,
6559e689cf4aSJeff Kirsher 				u64 pad_bytes, u64 len)
6560e689cf4aSJeff Kirsher {
6561e689cf4aSJeff Kirsher 	u16 eth_proto, eth_proto_inner;
6562e689cf4aSJeff Kirsher 	u64 csum_bits, l3off, ihl, ret;
6563e689cf4aSJeff Kirsher 	u8 ip_proto;
6564e689cf4aSJeff Kirsher 	int ipv6;
6565e689cf4aSJeff Kirsher 
6566e689cf4aSJeff Kirsher 	eth_proto = be16_to_cpu(ehdr->h_proto);
6567e689cf4aSJeff Kirsher 	eth_proto_inner = eth_proto;
6568e689cf4aSJeff Kirsher 	if (eth_proto == ETH_P_8021Q) {
6569e689cf4aSJeff Kirsher 		struct vlan_ethhdr *vp = (struct vlan_ethhdr *) ehdr;
6570e689cf4aSJeff Kirsher 		__be16 val = vp->h_vlan_encapsulated_proto;
6571e689cf4aSJeff Kirsher 
6572e689cf4aSJeff Kirsher 		eth_proto_inner = be16_to_cpu(val);
6573e689cf4aSJeff Kirsher 	}
6574e689cf4aSJeff Kirsher 
6575e689cf4aSJeff Kirsher 	ipv6 = ihl = 0;
6576e689cf4aSJeff Kirsher 	switch (skb->protocol) {
6577e689cf4aSJeff Kirsher 	case cpu_to_be16(ETH_P_IP):
6578e689cf4aSJeff Kirsher 		ip_proto = ip_hdr(skb)->protocol;
6579e689cf4aSJeff Kirsher 		ihl = ip_hdr(skb)->ihl;
6580e689cf4aSJeff Kirsher 		break;
6581e689cf4aSJeff Kirsher 	case cpu_to_be16(ETH_P_IPV6):
6582e689cf4aSJeff Kirsher 		ip_proto = ipv6_hdr(skb)->nexthdr;
6583e689cf4aSJeff Kirsher 		ihl = (40 >> 2);
6584e689cf4aSJeff Kirsher 		ipv6 = 1;
6585e689cf4aSJeff Kirsher 		break;
6586e689cf4aSJeff Kirsher 	default:
6587e689cf4aSJeff Kirsher 		ip_proto = ihl = 0;
6588e689cf4aSJeff Kirsher 		break;
6589e689cf4aSJeff Kirsher 	}
6590e689cf4aSJeff Kirsher 
6591e689cf4aSJeff Kirsher 	csum_bits = TXHDR_CSUM_NONE;
6592e689cf4aSJeff Kirsher 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
6593e689cf4aSJeff Kirsher 		u64 start, stuff;
6594e689cf4aSJeff Kirsher 
6595e689cf4aSJeff Kirsher 		csum_bits = (ip_proto == IPPROTO_TCP ?
6596e689cf4aSJeff Kirsher 			     TXHDR_CSUM_TCP :
6597e689cf4aSJeff Kirsher 			     (ip_proto == IPPROTO_UDP ?
6598e689cf4aSJeff Kirsher 			      TXHDR_CSUM_UDP : TXHDR_CSUM_SCTP));
6599e689cf4aSJeff Kirsher 
6600e689cf4aSJeff Kirsher 		start = skb_checksum_start_offset(skb) -
6601e689cf4aSJeff Kirsher 			(pad_bytes + sizeof(struct tx_pkt_hdr));
6602e689cf4aSJeff Kirsher 		stuff = start + skb->csum_offset;
6603e689cf4aSJeff Kirsher 
6604e689cf4aSJeff Kirsher 		csum_bits |= (start / 2) << TXHDR_L4START_SHIFT;
6605e689cf4aSJeff Kirsher 		csum_bits |= (stuff / 2) << TXHDR_L4STUFF_SHIFT;
6606e689cf4aSJeff Kirsher 	}
6607e689cf4aSJeff Kirsher 
6608e689cf4aSJeff Kirsher 	l3off = skb_network_offset(skb) -
6609e689cf4aSJeff Kirsher 		(pad_bytes + sizeof(struct tx_pkt_hdr));
6610e689cf4aSJeff Kirsher 
6611e689cf4aSJeff Kirsher 	ret = (((pad_bytes / 2) << TXHDR_PAD_SHIFT) |
6612e689cf4aSJeff Kirsher 	       (len << TXHDR_LEN_SHIFT) |
6613e689cf4aSJeff Kirsher 	       ((l3off / 2) << TXHDR_L3START_SHIFT) |
6614e689cf4aSJeff Kirsher 	       (ihl << TXHDR_IHL_SHIFT) |
6615e5c5d22eSSimon Horman 	       ((eth_proto_inner < ETH_P_802_3_MIN) ? TXHDR_LLC : 0) |
6616e689cf4aSJeff Kirsher 	       ((eth_proto == ETH_P_8021Q) ? TXHDR_VLAN : 0) |
6617e689cf4aSJeff Kirsher 	       (ipv6 ? TXHDR_IP_VER : 0) |
6618e689cf4aSJeff Kirsher 	       csum_bits);
6619e689cf4aSJeff Kirsher 
6620e689cf4aSJeff Kirsher 	return ret;
6621e689cf4aSJeff Kirsher }
6622e689cf4aSJeff Kirsher 
niu_start_xmit(struct sk_buff * skb,struct net_device * dev)6623e689cf4aSJeff Kirsher static netdev_tx_t niu_start_xmit(struct sk_buff *skb,
6624e689cf4aSJeff Kirsher 				  struct net_device *dev)
6625e689cf4aSJeff Kirsher {
6626e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6627e689cf4aSJeff Kirsher 	unsigned long align, headroom;
6628e689cf4aSJeff Kirsher 	struct netdev_queue *txq;
6629e689cf4aSJeff Kirsher 	struct tx_ring_info *rp;
6630e689cf4aSJeff Kirsher 	struct tx_pkt_hdr *tp;
6631e689cf4aSJeff Kirsher 	unsigned int len, nfg;
6632e689cf4aSJeff Kirsher 	struct ethhdr *ehdr;
6633e689cf4aSJeff Kirsher 	int prod, i, tlen;
6634e689cf4aSJeff Kirsher 	u64 mapping, mrk;
6635e689cf4aSJeff Kirsher 
6636e689cf4aSJeff Kirsher 	i = skb_get_queue_mapping(skb);
6637e689cf4aSJeff Kirsher 	rp = &np->tx_rings[i];
6638e689cf4aSJeff Kirsher 	txq = netdev_get_tx_queue(dev, i);
6639e689cf4aSJeff Kirsher 
6640e689cf4aSJeff Kirsher 	if (niu_tx_avail(rp) <= (skb_shinfo(skb)->nr_frags + 1)) {
6641e689cf4aSJeff Kirsher 		netif_tx_stop_queue(txq);
6642e689cf4aSJeff Kirsher 		dev_err(np->device, "%s: BUG! Tx ring full when queue awake!\n", dev->name);
6643e689cf4aSJeff Kirsher 		rp->tx_errors++;
6644e689cf4aSJeff Kirsher 		return NETDEV_TX_BUSY;
6645e689cf4aSJeff Kirsher 	}
6646e689cf4aSJeff Kirsher 
664728f7936cSAlexander Duyck 	if (eth_skb_pad(skb))
6648e689cf4aSJeff Kirsher 		goto out;
6649e689cf4aSJeff Kirsher 
6650e689cf4aSJeff Kirsher 	len = sizeof(struct tx_pkt_hdr) + 15;
6651e689cf4aSJeff Kirsher 	if (skb_headroom(skb) < len) {
6652e689cf4aSJeff Kirsher 		struct sk_buff *skb_new;
6653e689cf4aSJeff Kirsher 
6654e689cf4aSJeff Kirsher 		skb_new = skb_realloc_headroom(skb, len);
665542288830SJiri Pirko 		if (!skb_new)
6656e689cf4aSJeff Kirsher 			goto out_drop;
6657e689cf4aSJeff Kirsher 		kfree_skb(skb);
6658e689cf4aSJeff Kirsher 		skb = skb_new;
6659e689cf4aSJeff Kirsher 	} else
6660e689cf4aSJeff Kirsher 		skb_orphan(skb);
6661e689cf4aSJeff Kirsher 
6662e689cf4aSJeff Kirsher 	align = ((unsigned long) skb->data & (16 - 1));
6663e689cf4aSJeff Kirsher 	headroom = align + sizeof(struct tx_pkt_hdr);
6664e689cf4aSJeff Kirsher 
6665e689cf4aSJeff Kirsher 	ehdr = (struct ethhdr *) skb->data;
6666d58ff351SJohannes Berg 	tp = skb_push(skb, headroom);
6667e689cf4aSJeff Kirsher 
6668e689cf4aSJeff Kirsher 	len = skb->len - sizeof(struct tx_pkt_hdr);
6669e689cf4aSJeff Kirsher 	tp->flags = cpu_to_le64(niu_compute_tx_flags(skb, ehdr, align, len));
6670e689cf4aSJeff Kirsher 	tp->resv = 0;
6671e689cf4aSJeff Kirsher 
6672e689cf4aSJeff Kirsher 	len = skb_headlen(skb);
6673e689cf4aSJeff Kirsher 	mapping = np->ops->map_single(np->device, skb->data,
6674e689cf4aSJeff Kirsher 				      len, DMA_TO_DEVICE);
6675e689cf4aSJeff Kirsher 
6676e689cf4aSJeff Kirsher 	prod = rp->prod;
6677e689cf4aSJeff Kirsher 
6678e689cf4aSJeff Kirsher 	rp->tx_buffs[prod].skb = skb;
6679e689cf4aSJeff Kirsher 	rp->tx_buffs[prod].mapping = mapping;
6680e689cf4aSJeff Kirsher 
6681e689cf4aSJeff Kirsher 	mrk = TX_DESC_SOP;
6682e689cf4aSJeff Kirsher 	if (++rp->mark_counter == rp->mark_freq) {
6683e689cf4aSJeff Kirsher 		rp->mark_counter = 0;
6684e689cf4aSJeff Kirsher 		mrk |= TX_DESC_MARK;
6685e689cf4aSJeff Kirsher 		rp->mark_pending++;
6686e689cf4aSJeff Kirsher 	}
6687e689cf4aSJeff Kirsher 
6688e689cf4aSJeff Kirsher 	tlen = len;
6689e689cf4aSJeff Kirsher 	nfg = skb_shinfo(skb)->nr_frags;
6690e689cf4aSJeff Kirsher 	while (tlen > 0) {
6691e689cf4aSJeff Kirsher 		tlen -= MAX_TX_DESC_LEN;
6692e689cf4aSJeff Kirsher 		nfg++;
6693e689cf4aSJeff Kirsher 	}
6694e689cf4aSJeff Kirsher 
6695e689cf4aSJeff Kirsher 	while (len > 0) {
6696e689cf4aSJeff Kirsher 		unsigned int this_len = len;
6697e689cf4aSJeff Kirsher 
6698e689cf4aSJeff Kirsher 		if (this_len > MAX_TX_DESC_LEN)
6699e689cf4aSJeff Kirsher 			this_len = MAX_TX_DESC_LEN;
6700e689cf4aSJeff Kirsher 
6701e689cf4aSJeff Kirsher 		niu_set_txd(rp, prod, mapping, this_len, mrk, nfg);
6702e689cf4aSJeff Kirsher 		mrk = nfg = 0;
6703e689cf4aSJeff Kirsher 
6704e689cf4aSJeff Kirsher 		prod = NEXT_TX(rp, prod);
6705e689cf4aSJeff Kirsher 		mapping += this_len;
6706e689cf4aSJeff Kirsher 		len -= this_len;
6707e689cf4aSJeff Kirsher 	}
6708e689cf4aSJeff Kirsher 
6709e689cf4aSJeff Kirsher 	for (i = 0; i <  skb_shinfo(skb)->nr_frags; i++) {
67109e903e08SEric Dumazet 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6711e689cf4aSJeff Kirsher 
67129e903e08SEric Dumazet 		len = skb_frag_size(frag);
6713134b413cSIan Campbell 		mapping = np->ops->map_page(np->device, skb_frag_page(frag),
6714b54c9d5bSJonathan Lemon 					    skb_frag_off(frag), len,
6715e689cf4aSJeff Kirsher 					    DMA_TO_DEVICE);
6716e689cf4aSJeff Kirsher 
6717e689cf4aSJeff Kirsher 		rp->tx_buffs[prod].skb = NULL;
6718e689cf4aSJeff Kirsher 		rp->tx_buffs[prod].mapping = mapping;
6719e689cf4aSJeff Kirsher 
6720e689cf4aSJeff Kirsher 		niu_set_txd(rp, prod, mapping, len, 0, 0);
6721e689cf4aSJeff Kirsher 
6722e689cf4aSJeff Kirsher 		prod = NEXT_TX(rp, prod);
6723e689cf4aSJeff Kirsher 	}
6724e689cf4aSJeff Kirsher 
6725e689cf4aSJeff Kirsher 	if (prod < rp->prod)
6726e689cf4aSJeff Kirsher 		rp->wrap_bit ^= TX_RING_KICK_WRAP;
6727e689cf4aSJeff Kirsher 	rp->prod = prod;
6728e689cf4aSJeff Kirsher 
6729e689cf4aSJeff Kirsher 	nw64(TX_RING_KICK(rp->tx_channel), rp->wrap_bit | (prod << 3));
6730e689cf4aSJeff Kirsher 
6731e689cf4aSJeff Kirsher 	if (unlikely(niu_tx_avail(rp) <= (MAX_SKB_FRAGS + 1))) {
6732e689cf4aSJeff Kirsher 		netif_tx_stop_queue(txq);
6733e689cf4aSJeff Kirsher 		if (niu_tx_avail(rp) > NIU_TX_WAKEUP_THRESH(rp))
6734e689cf4aSJeff Kirsher 			netif_tx_wake_queue(txq);
6735e689cf4aSJeff Kirsher 	}
6736e689cf4aSJeff Kirsher 
6737e689cf4aSJeff Kirsher out:
6738e689cf4aSJeff Kirsher 	return NETDEV_TX_OK;
6739e689cf4aSJeff Kirsher 
6740e689cf4aSJeff Kirsher out_drop:
6741e689cf4aSJeff Kirsher 	rp->tx_errors++;
6742e689cf4aSJeff Kirsher 	kfree_skb(skb);
6743e689cf4aSJeff Kirsher 	goto out;
6744e689cf4aSJeff Kirsher }
6745e689cf4aSJeff Kirsher 
niu_change_mtu(struct net_device * dev,int new_mtu)6746e689cf4aSJeff Kirsher static int niu_change_mtu(struct net_device *dev, int new_mtu)
6747e689cf4aSJeff Kirsher {
6748e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6749e689cf4aSJeff Kirsher 	int err, orig_jumbo, new_jumbo;
6750e689cf4aSJeff Kirsher 
6751e689cf4aSJeff Kirsher 	orig_jumbo = (dev->mtu > ETH_DATA_LEN);
6752e689cf4aSJeff Kirsher 	new_jumbo = (new_mtu > ETH_DATA_LEN);
6753e689cf4aSJeff Kirsher 
6754e689cf4aSJeff Kirsher 	dev->mtu = new_mtu;
6755e689cf4aSJeff Kirsher 
6756e689cf4aSJeff Kirsher 	if (!netif_running(dev) ||
6757e689cf4aSJeff Kirsher 	    (orig_jumbo == new_jumbo))
6758e689cf4aSJeff Kirsher 		return 0;
6759e689cf4aSJeff Kirsher 
6760e689cf4aSJeff Kirsher 	niu_full_shutdown(np, dev);
6761e689cf4aSJeff Kirsher 
6762e689cf4aSJeff Kirsher 	niu_free_channels(np);
6763e689cf4aSJeff Kirsher 
6764e689cf4aSJeff Kirsher 	niu_enable_napi(np);
6765e689cf4aSJeff Kirsher 
6766e689cf4aSJeff Kirsher 	err = niu_alloc_channels(np);
6767e689cf4aSJeff Kirsher 	if (err)
6768e689cf4aSJeff Kirsher 		return err;
6769e689cf4aSJeff Kirsher 
6770e689cf4aSJeff Kirsher 	spin_lock_irq(&np->lock);
6771e689cf4aSJeff Kirsher 
6772e689cf4aSJeff Kirsher 	err = niu_init_hw(np);
6773e689cf4aSJeff Kirsher 	if (!err) {
67740822c5d9SKees Cook 		timer_setup(&np->timer, niu_timer, 0);
6775e689cf4aSJeff Kirsher 		np->timer.expires = jiffies + HZ;
6776e689cf4aSJeff Kirsher 
6777e689cf4aSJeff Kirsher 		err = niu_enable_interrupts(np, 1);
6778e689cf4aSJeff Kirsher 		if (err)
6779e689cf4aSJeff Kirsher 			niu_stop_hw(np);
6780e689cf4aSJeff Kirsher 	}
6781e689cf4aSJeff Kirsher 
6782e689cf4aSJeff Kirsher 	spin_unlock_irq(&np->lock);
6783e689cf4aSJeff Kirsher 
6784e689cf4aSJeff Kirsher 	if (!err) {
6785e689cf4aSJeff Kirsher 		netif_tx_start_all_queues(dev);
6786e689cf4aSJeff Kirsher 		if (np->link_config.loopback_mode != LOOPBACK_DISABLED)
6787e689cf4aSJeff Kirsher 			netif_carrier_on(dev);
6788e689cf4aSJeff Kirsher 
6789e689cf4aSJeff Kirsher 		add_timer(&np->timer);
6790e689cf4aSJeff Kirsher 	}
6791e689cf4aSJeff Kirsher 
6792e689cf4aSJeff Kirsher 	return err;
6793e689cf4aSJeff Kirsher }
6794e689cf4aSJeff Kirsher 
niu_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)6795e689cf4aSJeff Kirsher static void niu_get_drvinfo(struct net_device *dev,
6796e689cf4aSJeff Kirsher 			    struct ethtool_drvinfo *info)
6797e689cf4aSJeff Kirsher {
6798e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6799e689cf4aSJeff Kirsher 	struct niu_vpd *vpd = &np->vpd;
6800e689cf4aSJeff Kirsher 
6801f029c781SWolfram Sang 	strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
6802f029c781SWolfram Sang 	strscpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
680323020ab3SRick Jones 	snprintf(info->fw_version, sizeof(info->fw_version), "%d.%d",
6804e689cf4aSJeff Kirsher 		vpd->fcode_major, vpd->fcode_minor);
6805e689cf4aSJeff Kirsher 	if (np->parent->plat_type != PLAT_TYPE_NIU)
6806f029c781SWolfram Sang 		strscpy(info->bus_info, pci_name(np->pdev),
680723020ab3SRick Jones 			sizeof(info->bus_info));
6808e689cf4aSJeff Kirsher }
6809e689cf4aSJeff Kirsher 
niu_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)6810d9722531SPhilippe Reynes static int niu_get_link_ksettings(struct net_device *dev,
6811d9722531SPhilippe Reynes 				  struct ethtool_link_ksettings *cmd)
6812e689cf4aSJeff Kirsher {
6813e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6814e689cf4aSJeff Kirsher 	struct niu_link_config *lp;
6815e689cf4aSJeff Kirsher 
6816e689cf4aSJeff Kirsher 	lp = &np->link_config;
6817e689cf4aSJeff Kirsher 
6818e689cf4aSJeff Kirsher 	memset(cmd, 0, sizeof(*cmd));
6819d9722531SPhilippe Reynes 	cmd->base.phy_address = np->phy_addr;
6820d9722531SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
6821d9722531SPhilippe Reynes 						lp->supported);
6822d9722531SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
6823d9722531SPhilippe Reynes 						lp->active_advertising);
6824d9722531SPhilippe Reynes 	cmd->base.autoneg = lp->active_autoneg;
6825d9722531SPhilippe Reynes 	cmd->base.speed = lp->active_speed;
6826d9722531SPhilippe Reynes 	cmd->base.duplex = lp->active_duplex;
6827d9722531SPhilippe Reynes 	cmd->base.port = (np->flags & NIU_FLAGS_FIBER) ? PORT_FIBRE : PORT_TP;
6828e689cf4aSJeff Kirsher 
6829e689cf4aSJeff Kirsher 	return 0;
6830e689cf4aSJeff Kirsher }
6831e689cf4aSJeff Kirsher 
niu_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)6832d9722531SPhilippe Reynes static int niu_set_link_ksettings(struct net_device *dev,
6833d9722531SPhilippe Reynes 				  const struct ethtool_link_ksettings *cmd)
6834e689cf4aSJeff Kirsher {
6835e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6836e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
6837e689cf4aSJeff Kirsher 
6838d9722531SPhilippe Reynes 	ethtool_convert_link_mode_to_legacy_u32(&lp->advertising,
6839d9722531SPhilippe Reynes 						cmd->link_modes.advertising);
6840d9722531SPhilippe Reynes 	lp->speed = cmd->base.speed;
6841d9722531SPhilippe Reynes 	lp->duplex = cmd->base.duplex;
6842d9722531SPhilippe Reynes 	lp->autoneg = cmd->base.autoneg;
6843e689cf4aSJeff Kirsher 	return niu_init_link(np);
6844e689cf4aSJeff Kirsher }
6845e689cf4aSJeff Kirsher 
niu_get_msglevel(struct net_device * dev)6846e689cf4aSJeff Kirsher static u32 niu_get_msglevel(struct net_device *dev)
6847e689cf4aSJeff Kirsher {
6848e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6849e689cf4aSJeff Kirsher 	return np->msg_enable;
6850e689cf4aSJeff Kirsher }
6851e689cf4aSJeff Kirsher 
niu_set_msglevel(struct net_device * dev,u32 value)6852e689cf4aSJeff Kirsher static void niu_set_msglevel(struct net_device *dev, u32 value)
6853e689cf4aSJeff Kirsher {
6854e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6855e689cf4aSJeff Kirsher 	np->msg_enable = value;
6856e689cf4aSJeff Kirsher }
6857e689cf4aSJeff Kirsher 
niu_nway_reset(struct net_device * dev)6858e689cf4aSJeff Kirsher static int niu_nway_reset(struct net_device *dev)
6859e689cf4aSJeff Kirsher {
6860e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6861e689cf4aSJeff Kirsher 
6862e689cf4aSJeff Kirsher 	if (np->link_config.autoneg)
6863e689cf4aSJeff Kirsher 		return niu_init_link(np);
6864e689cf4aSJeff Kirsher 
6865e689cf4aSJeff Kirsher 	return 0;
6866e689cf4aSJeff Kirsher }
6867e689cf4aSJeff Kirsher 
niu_get_eeprom_len(struct net_device * dev)6868e689cf4aSJeff Kirsher static int niu_get_eeprom_len(struct net_device *dev)
6869e689cf4aSJeff Kirsher {
6870e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6871e689cf4aSJeff Kirsher 
6872e689cf4aSJeff Kirsher 	return np->eeprom_len;
6873e689cf4aSJeff Kirsher }
6874e689cf4aSJeff Kirsher 
niu_get_eeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * data)6875e689cf4aSJeff Kirsher static int niu_get_eeprom(struct net_device *dev,
6876e689cf4aSJeff Kirsher 			  struct ethtool_eeprom *eeprom, u8 *data)
6877e689cf4aSJeff Kirsher {
6878e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
6879e689cf4aSJeff Kirsher 	u32 offset, len, val;
6880e689cf4aSJeff Kirsher 
6881e689cf4aSJeff Kirsher 	offset = eeprom->offset;
6882e689cf4aSJeff Kirsher 	len = eeprom->len;
6883e689cf4aSJeff Kirsher 
6884e689cf4aSJeff Kirsher 	if (offset + len < offset)
6885e689cf4aSJeff Kirsher 		return -EINVAL;
6886e689cf4aSJeff Kirsher 	if (offset >= np->eeprom_len)
6887e689cf4aSJeff Kirsher 		return -EINVAL;
6888e689cf4aSJeff Kirsher 	if (offset + len > np->eeprom_len)
6889e689cf4aSJeff Kirsher 		len = eeprom->len = np->eeprom_len - offset;
6890e689cf4aSJeff Kirsher 
6891e689cf4aSJeff Kirsher 	if (offset & 3) {
6892e689cf4aSJeff Kirsher 		u32 b_offset, b_count;
6893e689cf4aSJeff Kirsher 
6894e689cf4aSJeff Kirsher 		b_offset = offset & 3;
6895e689cf4aSJeff Kirsher 		b_count = 4 - b_offset;
6896e689cf4aSJeff Kirsher 		if (b_count > len)
6897e689cf4aSJeff Kirsher 			b_count = len;
6898e689cf4aSJeff Kirsher 
6899e689cf4aSJeff Kirsher 		val = nr64(ESPC_NCR((offset - b_offset) / 4));
6900e689cf4aSJeff Kirsher 		memcpy(data, ((char *)&val) + b_offset, b_count);
6901e689cf4aSJeff Kirsher 		data += b_count;
6902e689cf4aSJeff Kirsher 		len -= b_count;
6903e689cf4aSJeff Kirsher 		offset += b_count;
6904e689cf4aSJeff Kirsher 	}
6905e689cf4aSJeff Kirsher 	while (len >= 4) {
6906e689cf4aSJeff Kirsher 		val = nr64(ESPC_NCR(offset / 4));
6907e689cf4aSJeff Kirsher 		memcpy(data, &val, 4);
6908e689cf4aSJeff Kirsher 		data += 4;
6909e689cf4aSJeff Kirsher 		len -= 4;
6910e689cf4aSJeff Kirsher 		offset += 4;
6911e689cf4aSJeff Kirsher 	}
6912e689cf4aSJeff Kirsher 	if (len) {
6913e689cf4aSJeff Kirsher 		val = nr64(ESPC_NCR(offset / 4));
6914e689cf4aSJeff Kirsher 		memcpy(data, &val, len);
6915e689cf4aSJeff Kirsher 	}
6916e689cf4aSJeff Kirsher 	return 0;
6917e689cf4aSJeff Kirsher }
6918e689cf4aSJeff Kirsher 
niu_ethflow_to_l3proto(int flow_type,u8 * pid)6919e689cf4aSJeff Kirsher static void niu_ethflow_to_l3proto(int flow_type, u8 *pid)
6920e689cf4aSJeff Kirsher {
6921e689cf4aSJeff Kirsher 	switch (flow_type) {
6922e689cf4aSJeff Kirsher 	case TCP_V4_FLOW:
6923e689cf4aSJeff Kirsher 	case TCP_V6_FLOW:
6924e689cf4aSJeff Kirsher 		*pid = IPPROTO_TCP;
6925e689cf4aSJeff Kirsher 		break;
6926e689cf4aSJeff Kirsher 	case UDP_V4_FLOW:
6927e689cf4aSJeff Kirsher 	case UDP_V6_FLOW:
6928e689cf4aSJeff Kirsher 		*pid = IPPROTO_UDP;
6929e689cf4aSJeff Kirsher 		break;
6930e689cf4aSJeff Kirsher 	case SCTP_V4_FLOW:
6931e689cf4aSJeff Kirsher 	case SCTP_V6_FLOW:
6932e689cf4aSJeff Kirsher 		*pid = IPPROTO_SCTP;
6933e689cf4aSJeff Kirsher 		break;
6934e689cf4aSJeff Kirsher 	case AH_V4_FLOW:
6935e689cf4aSJeff Kirsher 	case AH_V6_FLOW:
6936e689cf4aSJeff Kirsher 		*pid = IPPROTO_AH;
6937e689cf4aSJeff Kirsher 		break;
6938e689cf4aSJeff Kirsher 	case ESP_V4_FLOW:
6939e689cf4aSJeff Kirsher 	case ESP_V6_FLOW:
6940e689cf4aSJeff Kirsher 		*pid = IPPROTO_ESP;
6941e689cf4aSJeff Kirsher 		break;
6942e689cf4aSJeff Kirsher 	default:
6943e689cf4aSJeff Kirsher 		*pid = 0;
6944e689cf4aSJeff Kirsher 		break;
6945e689cf4aSJeff Kirsher 	}
6946e689cf4aSJeff Kirsher }
6947e689cf4aSJeff Kirsher 
niu_class_to_ethflow(u64 class,int * flow_type)6948e689cf4aSJeff Kirsher static int niu_class_to_ethflow(u64 class, int *flow_type)
6949e689cf4aSJeff Kirsher {
6950e689cf4aSJeff Kirsher 	switch (class) {
6951e689cf4aSJeff Kirsher 	case CLASS_CODE_TCP_IPV4:
6952e689cf4aSJeff Kirsher 		*flow_type = TCP_V4_FLOW;
6953e689cf4aSJeff Kirsher 		break;
6954e689cf4aSJeff Kirsher 	case CLASS_CODE_UDP_IPV4:
6955e689cf4aSJeff Kirsher 		*flow_type = UDP_V4_FLOW;
6956e689cf4aSJeff Kirsher 		break;
6957e689cf4aSJeff Kirsher 	case CLASS_CODE_AH_ESP_IPV4:
6958e689cf4aSJeff Kirsher 		*flow_type = AH_V4_FLOW;
6959e689cf4aSJeff Kirsher 		break;
6960e689cf4aSJeff Kirsher 	case CLASS_CODE_SCTP_IPV4:
6961e689cf4aSJeff Kirsher 		*flow_type = SCTP_V4_FLOW;
6962e689cf4aSJeff Kirsher 		break;
6963e689cf4aSJeff Kirsher 	case CLASS_CODE_TCP_IPV6:
6964e689cf4aSJeff Kirsher 		*flow_type = TCP_V6_FLOW;
6965e689cf4aSJeff Kirsher 		break;
6966e689cf4aSJeff Kirsher 	case CLASS_CODE_UDP_IPV6:
6967e689cf4aSJeff Kirsher 		*flow_type = UDP_V6_FLOW;
6968e689cf4aSJeff Kirsher 		break;
6969e689cf4aSJeff Kirsher 	case CLASS_CODE_AH_ESP_IPV6:
6970e689cf4aSJeff Kirsher 		*flow_type = AH_V6_FLOW;
6971e689cf4aSJeff Kirsher 		break;
6972e689cf4aSJeff Kirsher 	case CLASS_CODE_SCTP_IPV6:
6973e689cf4aSJeff Kirsher 		*flow_type = SCTP_V6_FLOW;
6974e689cf4aSJeff Kirsher 		break;
6975e689cf4aSJeff Kirsher 	case CLASS_CODE_USER_PROG1:
6976e689cf4aSJeff Kirsher 	case CLASS_CODE_USER_PROG2:
6977e689cf4aSJeff Kirsher 	case CLASS_CODE_USER_PROG3:
6978e689cf4aSJeff Kirsher 	case CLASS_CODE_USER_PROG4:
6979e689cf4aSJeff Kirsher 		*flow_type = IP_USER_FLOW;
6980e689cf4aSJeff Kirsher 		break;
6981e689cf4aSJeff Kirsher 	default:
6982f55ea3d9SDan Carpenter 		return -EINVAL;
6983e689cf4aSJeff Kirsher 	}
6984e689cf4aSJeff Kirsher 
6985f55ea3d9SDan Carpenter 	return 0;
6986e689cf4aSJeff Kirsher }
6987e689cf4aSJeff Kirsher 
niu_ethflow_to_class(int flow_type,u64 * class)6988e689cf4aSJeff Kirsher static int niu_ethflow_to_class(int flow_type, u64 *class)
6989e689cf4aSJeff Kirsher {
6990e689cf4aSJeff Kirsher 	switch (flow_type) {
6991e689cf4aSJeff Kirsher 	case TCP_V4_FLOW:
6992e689cf4aSJeff Kirsher 		*class = CLASS_CODE_TCP_IPV4;
6993e689cf4aSJeff Kirsher 		break;
6994e689cf4aSJeff Kirsher 	case UDP_V4_FLOW:
6995e689cf4aSJeff Kirsher 		*class = CLASS_CODE_UDP_IPV4;
6996e689cf4aSJeff Kirsher 		break;
6997e689cf4aSJeff Kirsher 	case AH_ESP_V4_FLOW:
6998e689cf4aSJeff Kirsher 	case AH_V4_FLOW:
6999e689cf4aSJeff Kirsher 	case ESP_V4_FLOW:
7000e689cf4aSJeff Kirsher 		*class = CLASS_CODE_AH_ESP_IPV4;
7001e689cf4aSJeff Kirsher 		break;
7002e689cf4aSJeff Kirsher 	case SCTP_V4_FLOW:
7003e689cf4aSJeff Kirsher 		*class = CLASS_CODE_SCTP_IPV4;
7004e689cf4aSJeff Kirsher 		break;
7005e689cf4aSJeff Kirsher 	case TCP_V6_FLOW:
7006e689cf4aSJeff Kirsher 		*class = CLASS_CODE_TCP_IPV6;
7007e689cf4aSJeff Kirsher 		break;
7008e689cf4aSJeff Kirsher 	case UDP_V6_FLOW:
7009e689cf4aSJeff Kirsher 		*class = CLASS_CODE_UDP_IPV6;
7010e689cf4aSJeff Kirsher 		break;
7011e689cf4aSJeff Kirsher 	case AH_ESP_V6_FLOW:
7012e689cf4aSJeff Kirsher 	case AH_V6_FLOW:
7013e689cf4aSJeff Kirsher 	case ESP_V6_FLOW:
7014e689cf4aSJeff Kirsher 		*class = CLASS_CODE_AH_ESP_IPV6;
7015e689cf4aSJeff Kirsher 		break;
7016e689cf4aSJeff Kirsher 	case SCTP_V6_FLOW:
7017e689cf4aSJeff Kirsher 		*class = CLASS_CODE_SCTP_IPV6;
7018e689cf4aSJeff Kirsher 		break;
7019e689cf4aSJeff Kirsher 	default:
7020e689cf4aSJeff Kirsher 		return 0;
7021e689cf4aSJeff Kirsher 	}
7022e689cf4aSJeff Kirsher 
7023e689cf4aSJeff Kirsher 	return 1;
7024e689cf4aSJeff Kirsher }
7025e689cf4aSJeff Kirsher 
niu_flowkey_to_ethflow(u64 flow_key)7026e689cf4aSJeff Kirsher static u64 niu_flowkey_to_ethflow(u64 flow_key)
7027e689cf4aSJeff Kirsher {
7028e689cf4aSJeff Kirsher 	u64 ethflow = 0;
7029e689cf4aSJeff Kirsher 
7030e689cf4aSJeff Kirsher 	if (flow_key & FLOW_KEY_L2DA)
7031e689cf4aSJeff Kirsher 		ethflow |= RXH_L2DA;
7032e689cf4aSJeff Kirsher 	if (flow_key & FLOW_KEY_VLAN)
7033e689cf4aSJeff Kirsher 		ethflow |= RXH_VLAN;
7034e689cf4aSJeff Kirsher 	if (flow_key & FLOW_KEY_IPSA)
7035e689cf4aSJeff Kirsher 		ethflow |= RXH_IP_SRC;
7036e689cf4aSJeff Kirsher 	if (flow_key & FLOW_KEY_IPDA)
7037e689cf4aSJeff Kirsher 		ethflow |= RXH_IP_DST;
7038e689cf4aSJeff Kirsher 	if (flow_key & FLOW_KEY_PROTO)
7039e689cf4aSJeff Kirsher 		ethflow |= RXH_L3_PROTO;
7040e689cf4aSJeff Kirsher 	if (flow_key & (FLOW_KEY_L4_BYTE12 << FLOW_KEY_L4_0_SHIFT))
7041e689cf4aSJeff Kirsher 		ethflow |= RXH_L4_B_0_1;
7042e689cf4aSJeff Kirsher 	if (flow_key & (FLOW_KEY_L4_BYTE12 << FLOW_KEY_L4_1_SHIFT))
7043e689cf4aSJeff Kirsher 		ethflow |= RXH_L4_B_2_3;
7044e689cf4aSJeff Kirsher 
7045e689cf4aSJeff Kirsher 	return ethflow;
7046e689cf4aSJeff Kirsher 
7047e689cf4aSJeff Kirsher }
7048e689cf4aSJeff Kirsher 
niu_ethflow_to_flowkey(u64 ethflow,u64 * flow_key)7049e689cf4aSJeff Kirsher static int niu_ethflow_to_flowkey(u64 ethflow, u64 *flow_key)
7050e689cf4aSJeff Kirsher {
7051e689cf4aSJeff Kirsher 	u64 key = 0;
7052e689cf4aSJeff Kirsher 
7053e689cf4aSJeff Kirsher 	if (ethflow & RXH_L2DA)
7054e689cf4aSJeff Kirsher 		key |= FLOW_KEY_L2DA;
7055e689cf4aSJeff Kirsher 	if (ethflow & RXH_VLAN)
7056e689cf4aSJeff Kirsher 		key |= FLOW_KEY_VLAN;
7057e689cf4aSJeff Kirsher 	if (ethflow & RXH_IP_SRC)
7058e689cf4aSJeff Kirsher 		key |= FLOW_KEY_IPSA;
7059e689cf4aSJeff Kirsher 	if (ethflow & RXH_IP_DST)
7060e689cf4aSJeff Kirsher 		key |= FLOW_KEY_IPDA;
7061e689cf4aSJeff Kirsher 	if (ethflow & RXH_L3_PROTO)
7062e689cf4aSJeff Kirsher 		key |= FLOW_KEY_PROTO;
7063e689cf4aSJeff Kirsher 	if (ethflow & RXH_L4_B_0_1)
7064e689cf4aSJeff Kirsher 		key |= (FLOW_KEY_L4_BYTE12 << FLOW_KEY_L4_0_SHIFT);
7065e689cf4aSJeff Kirsher 	if (ethflow & RXH_L4_B_2_3)
7066e689cf4aSJeff Kirsher 		key |= (FLOW_KEY_L4_BYTE12 << FLOW_KEY_L4_1_SHIFT);
7067e689cf4aSJeff Kirsher 
7068e689cf4aSJeff Kirsher 	*flow_key = key;
7069e689cf4aSJeff Kirsher 
7070e689cf4aSJeff Kirsher 	return 1;
7071e689cf4aSJeff Kirsher 
7072e689cf4aSJeff Kirsher }
7073e689cf4aSJeff Kirsher 
niu_get_hash_opts(struct niu * np,struct ethtool_rxnfc * nfc)7074e689cf4aSJeff Kirsher static int niu_get_hash_opts(struct niu *np, struct ethtool_rxnfc *nfc)
7075e689cf4aSJeff Kirsher {
7076e689cf4aSJeff Kirsher 	u64 class;
7077e689cf4aSJeff Kirsher 
7078e689cf4aSJeff Kirsher 	nfc->data = 0;
7079e689cf4aSJeff Kirsher 
7080e689cf4aSJeff Kirsher 	if (!niu_ethflow_to_class(nfc->flow_type, &class))
7081e689cf4aSJeff Kirsher 		return -EINVAL;
7082e689cf4aSJeff Kirsher 
7083e689cf4aSJeff Kirsher 	if (np->parent->tcam_key[class - CLASS_CODE_USER_PROG1] &
7084e689cf4aSJeff Kirsher 	    TCAM_KEY_DISC)
7085e689cf4aSJeff Kirsher 		nfc->data = RXH_DISCARD;
7086e689cf4aSJeff Kirsher 	else
7087e689cf4aSJeff Kirsher 		nfc->data = niu_flowkey_to_ethflow(np->parent->flow_key[class -
7088e689cf4aSJeff Kirsher 						      CLASS_CODE_USER_PROG1]);
7089e689cf4aSJeff Kirsher 	return 0;
7090e689cf4aSJeff Kirsher }
7091e689cf4aSJeff Kirsher 
niu_get_ip4fs_from_tcam_key(struct niu_tcam_entry * tp,struct ethtool_rx_flow_spec * fsp)7092e689cf4aSJeff Kirsher static void niu_get_ip4fs_from_tcam_key(struct niu_tcam_entry *tp,
7093e689cf4aSJeff Kirsher 					struct ethtool_rx_flow_spec *fsp)
7094e689cf4aSJeff Kirsher {
7095e689cf4aSJeff Kirsher 	u32 tmp;
7096e689cf4aSJeff Kirsher 	u16 prt;
7097e689cf4aSJeff Kirsher 
7098e689cf4aSJeff Kirsher 	tmp = (tp->key[3] & TCAM_V4KEY3_SADDR) >> TCAM_V4KEY3_SADDR_SHIFT;
7099e689cf4aSJeff Kirsher 	fsp->h_u.tcp_ip4_spec.ip4src = cpu_to_be32(tmp);
7100e689cf4aSJeff Kirsher 
7101e689cf4aSJeff Kirsher 	tmp = (tp->key[3] & TCAM_V4KEY3_DADDR) >> TCAM_V4KEY3_DADDR_SHIFT;
7102e689cf4aSJeff Kirsher 	fsp->h_u.tcp_ip4_spec.ip4dst = cpu_to_be32(tmp);
7103e689cf4aSJeff Kirsher 
7104e689cf4aSJeff Kirsher 	tmp = (tp->key_mask[3] & TCAM_V4KEY3_SADDR) >> TCAM_V4KEY3_SADDR_SHIFT;
7105e689cf4aSJeff Kirsher 	fsp->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(tmp);
7106e689cf4aSJeff Kirsher 
7107e689cf4aSJeff Kirsher 	tmp = (tp->key_mask[3] & TCAM_V4KEY3_DADDR) >> TCAM_V4KEY3_DADDR_SHIFT;
7108e689cf4aSJeff Kirsher 	fsp->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(tmp);
7109e689cf4aSJeff Kirsher 
7110e689cf4aSJeff Kirsher 	fsp->h_u.tcp_ip4_spec.tos = (tp->key[2] & TCAM_V4KEY2_TOS) >>
7111e689cf4aSJeff Kirsher 		TCAM_V4KEY2_TOS_SHIFT;
7112e689cf4aSJeff Kirsher 	fsp->m_u.tcp_ip4_spec.tos = (tp->key_mask[2] & TCAM_V4KEY2_TOS) >>
7113e689cf4aSJeff Kirsher 		TCAM_V4KEY2_TOS_SHIFT;
7114e689cf4aSJeff Kirsher 
7115e689cf4aSJeff Kirsher 	switch (fsp->flow_type) {
7116e689cf4aSJeff Kirsher 	case TCP_V4_FLOW:
7117e689cf4aSJeff Kirsher 	case UDP_V4_FLOW:
7118e689cf4aSJeff Kirsher 	case SCTP_V4_FLOW:
7119e689cf4aSJeff Kirsher 		prt = ((tp->key[2] & TCAM_V4KEY2_PORT_SPI) >>
7120e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PORT_SPI_SHIFT) >> 16;
7121e689cf4aSJeff Kirsher 		fsp->h_u.tcp_ip4_spec.psrc = cpu_to_be16(prt);
7122e689cf4aSJeff Kirsher 
7123e689cf4aSJeff Kirsher 		prt = ((tp->key[2] & TCAM_V4KEY2_PORT_SPI) >>
7124e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PORT_SPI_SHIFT) & 0xffff;
7125e689cf4aSJeff Kirsher 		fsp->h_u.tcp_ip4_spec.pdst = cpu_to_be16(prt);
7126e689cf4aSJeff Kirsher 
7127e689cf4aSJeff Kirsher 		prt = ((tp->key_mask[2] & TCAM_V4KEY2_PORT_SPI) >>
7128e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PORT_SPI_SHIFT) >> 16;
7129e689cf4aSJeff Kirsher 		fsp->m_u.tcp_ip4_spec.psrc = cpu_to_be16(prt);
7130e689cf4aSJeff Kirsher 
7131e689cf4aSJeff Kirsher 		prt = ((tp->key_mask[2] & TCAM_V4KEY2_PORT_SPI) >>
7132e689cf4aSJeff Kirsher 			 TCAM_V4KEY2_PORT_SPI_SHIFT) & 0xffff;
7133e689cf4aSJeff Kirsher 		fsp->m_u.tcp_ip4_spec.pdst = cpu_to_be16(prt);
7134e689cf4aSJeff Kirsher 		break;
7135e689cf4aSJeff Kirsher 	case AH_V4_FLOW:
7136e689cf4aSJeff Kirsher 	case ESP_V4_FLOW:
7137e689cf4aSJeff Kirsher 		tmp = (tp->key[2] & TCAM_V4KEY2_PORT_SPI) >>
7138e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PORT_SPI_SHIFT;
7139e689cf4aSJeff Kirsher 		fsp->h_u.ah_ip4_spec.spi = cpu_to_be32(tmp);
7140e689cf4aSJeff Kirsher 
7141e689cf4aSJeff Kirsher 		tmp = (tp->key_mask[2] & TCAM_V4KEY2_PORT_SPI) >>
7142e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PORT_SPI_SHIFT;
7143e689cf4aSJeff Kirsher 		fsp->m_u.ah_ip4_spec.spi = cpu_to_be32(tmp);
7144e689cf4aSJeff Kirsher 		break;
7145e689cf4aSJeff Kirsher 	case IP_USER_FLOW:
7146e689cf4aSJeff Kirsher 		tmp = (tp->key[2] & TCAM_V4KEY2_PORT_SPI) >>
7147e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PORT_SPI_SHIFT;
7148e689cf4aSJeff Kirsher 		fsp->h_u.usr_ip4_spec.l4_4_bytes = cpu_to_be32(tmp);
7149e689cf4aSJeff Kirsher 
7150e689cf4aSJeff Kirsher 		tmp = (tp->key_mask[2] & TCAM_V4KEY2_PORT_SPI) >>
7151e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PORT_SPI_SHIFT;
7152e689cf4aSJeff Kirsher 		fsp->m_u.usr_ip4_spec.l4_4_bytes = cpu_to_be32(tmp);
7153e689cf4aSJeff Kirsher 
7154e689cf4aSJeff Kirsher 		fsp->h_u.usr_ip4_spec.proto =
7155e689cf4aSJeff Kirsher 			(tp->key[2] & TCAM_V4KEY2_PROTO) >>
7156e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PROTO_SHIFT;
7157e689cf4aSJeff Kirsher 		fsp->m_u.usr_ip4_spec.proto =
7158e689cf4aSJeff Kirsher 			(tp->key_mask[2] & TCAM_V4KEY2_PROTO) >>
7159e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PROTO_SHIFT;
7160e689cf4aSJeff Kirsher 
7161e689cf4aSJeff Kirsher 		fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
7162e689cf4aSJeff Kirsher 		break;
7163e689cf4aSJeff Kirsher 	default:
7164e689cf4aSJeff Kirsher 		break;
7165e689cf4aSJeff Kirsher 	}
7166e689cf4aSJeff Kirsher }
7167e689cf4aSJeff Kirsher 
niu_get_ethtool_tcam_entry(struct niu * np,struct ethtool_rxnfc * nfc)7168e689cf4aSJeff Kirsher static int niu_get_ethtool_tcam_entry(struct niu *np,
7169e689cf4aSJeff Kirsher 				      struct ethtool_rxnfc *nfc)
7170e689cf4aSJeff Kirsher {
7171e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
7172e689cf4aSJeff Kirsher 	struct niu_tcam_entry *tp;
7173e689cf4aSJeff Kirsher 	struct ethtool_rx_flow_spec *fsp = &nfc->fs;
7174e689cf4aSJeff Kirsher 	u16 idx;
7175e689cf4aSJeff Kirsher 	u64 class;
7176e689cf4aSJeff Kirsher 	int ret = 0;
7177e689cf4aSJeff Kirsher 
7178e689cf4aSJeff Kirsher 	idx = tcam_get_index(np, (u16)nfc->fs.location);
7179e689cf4aSJeff Kirsher 
7180e689cf4aSJeff Kirsher 	tp = &parent->tcam[idx];
7181e689cf4aSJeff Kirsher 	if (!tp->valid) {
7182e689cf4aSJeff Kirsher 		netdev_info(np->dev, "niu%d: entry [%d] invalid for idx[%d]\n",
7183e689cf4aSJeff Kirsher 			    parent->index, (u16)nfc->fs.location, idx);
7184e689cf4aSJeff Kirsher 		return -EINVAL;
7185e689cf4aSJeff Kirsher 	}
7186e689cf4aSJeff Kirsher 
7187e689cf4aSJeff Kirsher 	/* fill the flow spec entry */
7188e689cf4aSJeff Kirsher 	class = (tp->key[0] & TCAM_V4KEY0_CLASS_CODE) >>
7189e689cf4aSJeff Kirsher 		TCAM_V4KEY0_CLASS_CODE_SHIFT;
7190e689cf4aSJeff Kirsher 	ret = niu_class_to_ethflow(class, &fsp->flow_type);
7191e689cf4aSJeff Kirsher 	if (ret < 0) {
7192e689cf4aSJeff Kirsher 		netdev_info(np->dev, "niu%d: niu_class_to_ethflow failed\n",
7193e689cf4aSJeff Kirsher 			    parent->index);
7194e689cf4aSJeff Kirsher 		goto out;
7195e689cf4aSJeff Kirsher 	}
7196e689cf4aSJeff Kirsher 
7197e689cf4aSJeff Kirsher 	if (fsp->flow_type == AH_V4_FLOW || fsp->flow_type == AH_V6_FLOW) {
7198e689cf4aSJeff Kirsher 		u32 proto = (tp->key[2] & TCAM_V4KEY2_PROTO) >>
7199e689cf4aSJeff Kirsher 			TCAM_V4KEY2_PROTO_SHIFT;
7200e689cf4aSJeff Kirsher 		if (proto == IPPROTO_ESP) {
7201e689cf4aSJeff Kirsher 			if (fsp->flow_type == AH_V4_FLOW)
7202e689cf4aSJeff Kirsher 				fsp->flow_type = ESP_V4_FLOW;
7203e689cf4aSJeff Kirsher 			else
7204e689cf4aSJeff Kirsher 				fsp->flow_type = ESP_V6_FLOW;
7205e689cf4aSJeff Kirsher 		}
7206e689cf4aSJeff Kirsher 	}
7207e689cf4aSJeff Kirsher 
7208e689cf4aSJeff Kirsher 	switch (fsp->flow_type) {
7209e689cf4aSJeff Kirsher 	case TCP_V4_FLOW:
7210e689cf4aSJeff Kirsher 	case UDP_V4_FLOW:
7211e689cf4aSJeff Kirsher 	case SCTP_V4_FLOW:
7212e689cf4aSJeff Kirsher 	case AH_V4_FLOW:
7213e689cf4aSJeff Kirsher 	case ESP_V4_FLOW:
7214e689cf4aSJeff Kirsher 		niu_get_ip4fs_from_tcam_key(tp, fsp);
7215e689cf4aSJeff Kirsher 		break;
7216e689cf4aSJeff Kirsher 	case TCP_V6_FLOW:
7217e689cf4aSJeff Kirsher 	case UDP_V6_FLOW:
7218e689cf4aSJeff Kirsher 	case SCTP_V6_FLOW:
7219e689cf4aSJeff Kirsher 	case AH_V6_FLOW:
7220e689cf4aSJeff Kirsher 	case ESP_V6_FLOW:
7221e689cf4aSJeff Kirsher 		/* Not yet implemented */
7222e689cf4aSJeff Kirsher 		ret = -EINVAL;
7223e689cf4aSJeff Kirsher 		break;
7224e689cf4aSJeff Kirsher 	case IP_USER_FLOW:
7225e689cf4aSJeff Kirsher 		niu_get_ip4fs_from_tcam_key(tp, fsp);
7226e689cf4aSJeff Kirsher 		break;
7227e689cf4aSJeff Kirsher 	default:
7228e689cf4aSJeff Kirsher 		ret = -EINVAL;
7229e689cf4aSJeff Kirsher 		break;
7230e689cf4aSJeff Kirsher 	}
7231e689cf4aSJeff Kirsher 
7232e689cf4aSJeff Kirsher 	if (ret < 0)
7233e689cf4aSJeff Kirsher 		goto out;
7234e689cf4aSJeff Kirsher 
7235e689cf4aSJeff Kirsher 	if (tp->assoc_data & TCAM_ASSOCDATA_DISC)
7236e689cf4aSJeff Kirsher 		fsp->ring_cookie = RX_CLS_FLOW_DISC;
7237e689cf4aSJeff Kirsher 	else
7238e689cf4aSJeff Kirsher 		fsp->ring_cookie = (tp->assoc_data & TCAM_ASSOCDATA_OFFSET) >>
7239e689cf4aSJeff Kirsher 			TCAM_ASSOCDATA_OFFSET_SHIFT;
7240e689cf4aSJeff Kirsher 
7241e689cf4aSJeff Kirsher 	/* put the tcam size here */
7242e689cf4aSJeff Kirsher 	nfc->data = tcam_get_size(np);
7243e689cf4aSJeff Kirsher out:
7244e689cf4aSJeff Kirsher 	return ret;
7245e689cf4aSJeff Kirsher }
7246e689cf4aSJeff Kirsher 
niu_get_ethtool_tcam_all(struct niu * np,struct ethtool_rxnfc * nfc,u32 * rule_locs)7247e689cf4aSJeff Kirsher static int niu_get_ethtool_tcam_all(struct niu *np,
7248e689cf4aSJeff Kirsher 				    struct ethtool_rxnfc *nfc,
7249e689cf4aSJeff Kirsher 				    u32 *rule_locs)
7250e689cf4aSJeff Kirsher {
7251e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
7252e689cf4aSJeff Kirsher 	struct niu_tcam_entry *tp;
7253e689cf4aSJeff Kirsher 	int i, idx, cnt;
7254e689cf4aSJeff Kirsher 	unsigned long flags;
7255e689cf4aSJeff Kirsher 	int ret = 0;
7256e689cf4aSJeff Kirsher 
7257e689cf4aSJeff Kirsher 	/* put the tcam size here */
7258e689cf4aSJeff Kirsher 	nfc->data = tcam_get_size(np);
7259e689cf4aSJeff Kirsher 
7260e689cf4aSJeff Kirsher 	niu_lock_parent(np, flags);
7261e689cf4aSJeff Kirsher 	for (cnt = 0, i = 0; i < nfc->data; i++) {
7262e689cf4aSJeff Kirsher 		idx = tcam_get_index(np, i);
7263e689cf4aSJeff Kirsher 		tp = &parent->tcam[idx];
7264e689cf4aSJeff Kirsher 		if (!tp->valid)
7265e689cf4aSJeff Kirsher 			continue;
7266e689cf4aSJeff Kirsher 		if (cnt == nfc->rule_cnt) {
7267e689cf4aSJeff Kirsher 			ret = -EMSGSIZE;
7268e689cf4aSJeff Kirsher 			break;
7269e689cf4aSJeff Kirsher 		}
7270e689cf4aSJeff Kirsher 		rule_locs[cnt] = i;
7271e689cf4aSJeff Kirsher 		cnt++;
7272e689cf4aSJeff Kirsher 	}
7273e689cf4aSJeff Kirsher 	niu_unlock_parent(np, flags);
7274e689cf4aSJeff Kirsher 
7275473e64eeSBen Hutchings 	nfc->rule_cnt = cnt;
7276473e64eeSBen Hutchings 
7277e689cf4aSJeff Kirsher 	return ret;
7278e689cf4aSJeff Kirsher }
7279e689cf4aSJeff Kirsher 
niu_get_nfc(struct net_device * dev,struct ethtool_rxnfc * cmd,u32 * rule_locs)7280e689cf4aSJeff Kirsher static int niu_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
7281815c7db5SBen Hutchings 		       u32 *rule_locs)
7282e689cf4aSJeff Kirsher {
7283e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
7284e689cf4aSJeff Kirsher 	int ret = 0;
7285e689cf4aSJeff Kirsher 
7286e689cf4aSJeff Kirsher 	switch (cmd->cmd) {
7287e689cf4aSJeff Kirsher 	case ETHTOOL_GRXFH:
7288e689cf4aSJeff Kirsher 		ret = niu_get_hash_opts(np, cmd);
7289e689cf4aSJeff Kirsher 		break;
7290e689cf4aSJeff Kirsher 	case ETHTOOL_GRXRINGS:
7291e689cf4aSJeff Kirsher 		cmd->data = np->num_rx_rings;
7292e689cf4aSJeff Kirsher 		break;
7293e689cf4aSJeff Kirsher 	case ETHTOOL_GRXCLSRLCNT:
7294e689cf4aSJeff Kirsher 		cmd->rule_cnt = tcam_get_valid_entry_cnt(np);
7295e689cf4aSJeff Kirsher 		break;
7296e689cf4aSJeff Kirsher 	case ETHTOOL_GRXCLSRULE:
7297e689cf4aSJeff Kirsher 		ret = niu_get_ethtool_tcam_entry(np, cmd);
7298e689cf4aSJeff Kirsher 		break;
7299e689cf4aSJeff Kirsher 	case ETHTOOL_GRXCLSRLALL:
7300815c7db5SBen Hutchings 		ret = niu_get_ethtool_tcam_all(np, cmd, rule_locs);
7301e689cf4aSJeff Kirsher 		break;
7302e689cf4aSJeff Kirsher 	default:
7303e689cf4aSJeff Kirsher 		ret = -EINVAL;
7304e689cf4aSJeff Kirsher 		break;
7305e689cf4aSJeff Kirsher 	}
7306e689cf4aSJeff Kirsher 
7307e689cf4aSJeff Kirsher 	return ret;
7308e689cf4aSJeff Kirsher }
7309e689cf4aSJeff Kirsher 
niu_set_hash_opts(struct niu * np,struct ethtool_rxnfc * nfc)7310e689cf4aSJeff Kirsher static int niu_set_hash_opts(struct niu *np, struct ethtool_rxnfc *nfc)
7311e689cf4aSJeff Kirsher {
7312e689cf4aSJeff Kirsher 	u64 class;
7313e689cf4aSJeff Kirsher 	u64 flow_key = 0;
7314e689cf4aSJeff Kirsher 	unsigned long flags;
7315e689cf4aSJeff Kirsher 
7316e689cf4aSJeff Kirsher 	if (!niu_ethflow_to_class(nfc->flow_type, &class))
7317e689cf4aSJeff Kirsher 		return -EINVAL;
7318e689cf4aSJeff Kirsher 
7319e689cf4aSJeff Kirsher 	if (class < CLASS_CODE_USER_PROG1 ||
7320e689cf4aSJeff Kirsher 	    class > CLASS_CODE_SCTP_IPV6)
7321e689cf4aSJeff Kirsher 		return -EINVAL;
7322e689cf4aSJeff Kirsher 
7323e689cf4aSJeff Kirsher 	if (nfc->data & RXH_DISCARD) {
7324e689cf4aSJeff Kirsher 		niu_lock_parent(np, flags);
7325e689cf4aSJeff Kirsher 		flow_key = np->parent->tcam_key[class -
7326e689cf4aSJeff Kirsher 					       CLASS_CODE_USER_PROG1];
7327e689cf4aSJeff Kirsher 		flow_key |= TCAM_KEY_DISC;
7328e689cf4aSJeff Kirsher 		nw64(TCAM_KEY(class - CLASS_CODE_USER_PROG1), flow_key);
7329e689cf4aSJeff Kirsher 		np->parent->tcam_key[class - CLASS_CODE_USER_PROG1] = flow_key;
7330e689cf4aSJeff Kirsher 		niu_unlock_parent(np, flags);
7331e689cf4aSJeff Kirsher 		return 0;
7332e689cf4aSJeff Kirsher 	} else {
7333e689cf4aSJeff Kirsher 		/* Discard was set before, but is not set now */
7334e689cf4aSJeff Kirsher 		if (np->parent->tcam_key[class - CLASS_CODE_USER_PROG1] &
7335e689cf4aSJeff Kirsher 		    TCAM_KEY_DISC) {
7336e689cf4aSJeff Kirsher 			niu_lock_parent(np, flags);
7337e689cf4aSJeff Kirsher 			flow_key = np->parent->tcam_key[class -
7338e689cf4aSJeff Kirsher 					       CLASS_CODE_USER_PROG1];
7339e689cf4aSJeff Kirsher 			flow_key &= ~TCAM_KEY_DISC;
7340e689cf4aSJeff Kirsher 			nw64(TCAM_KEY(class - CLASS_CODE_USER_PROG1),
7341e689cf4aSJeff Kirsher 			     flow_key);
7342e689cf4aSJeff Kirsher 			np->parent->tcam_key[class - CLASS_CODE_USER_PROG1] =
7343e689cf4aSJeff Kirsher 				flow_key;
7344e689cf4aSJeff Kirsher 			niu_unlock_parent(np, flags);
7345e689cf4aSJeff Kirsher 		}
7346e689cf4aSJeff Kirsher 	}
7347e689cf4aSJeff Kirsher 
7348e689cf4aSJeff Kirsher 	if (!niu_ethflow_to_flowkey(nfc->data, &flow_key))
7349e689cf4aSJeff Kirsher 		return -EINVAL;
7350e689cf4aSJeff Kirsher 
7351e689cf4aSJeff Kirsher 	niu_lock_parent(np, flags);
7352e689cf4aSJeff Kirsher 	nw64(FLOW_KEY(class - CLASS_CODE_USER_PROG1), flow_key);
7353e689cf4aSJeff Kirsher 	np->parent->flow_key[class - CLASS_CODE_USER_PROG1] = flow_key;
7354e689cf4aSJeff Kirsher 	niu_unlock_parent(np, flags);
7355e689cf4aSJeff Kirsher 
7356e689cf4aSJeff Kirsher 	return 0;
7357e689cf4aSJeff Kirsher }
7358e689cf4aSJeff Kirsher 
niu_get_tcamkey_from_ip4fs(struct ethtool_rx_flow_spec * fsp,struct niu_tcam_entry * tp,int l2_rdc_tab,u64 class)7359e689cf4aSJeff Kirsher static void niu_get_tcamkey_from_ip4fs(struct ethtool_rx_flow_spec *fsp,
7360e689cf4aSJeff Kirsher 				       struct niu_tcam_entry *tp,
7361e689cf4aSJeff Kirsher 				       int l2_rdc_tab, u64 class)
7362e689cf4aSJeff Kirsher {
7363e689cf4aSJeff Kirsher 	u8 pid = 0;
7364e689cf4aSJeff Kirsher 	u32 sip, dip, sipm, dipm, spi, spim;
7365e689cf4aSJeff Kirsher 	u16 sport, dport, spm, dpm;
7366e689cf4aSJeff Kirsher 
7367e689cf4aSJeff Kirsher 	sip = be32_to_cpu(fsp->h_u.tcp_ip4_spec.ip4src);
7368e689cf4aSJeff Kirsher 	sipm = be32_to_cpu(fsp->m_u.tcp_ip4_spec.ip4src);
7369e689cf4aSJeff Kirsher 	dip = be32_to_cpu(fsp->h_u.tcp_ip4_spec.ip4dst);
7370e689cf4aSJeff Kirsher 	dipm = be32_to_cpu(fsp->m_u.tcp_ip4_spec.ip4dst);
7371e689cf4aSJeff Kirsher 
7372e689cf4aSJeff Kirsher 	tp->key[0] = class << TCAM_V4KEY0_CLASS_CODE_SHIFT;
7373e689cf4aSJeff Kirsher 	tp->key_mask[0] = TCAM_V4KEY0_CLASS_CODE;
7374e689cf4aSJeff Kirsher 	tp->key[1] = (u64)l2_rdc_tab << TCAM_V4KEY1_L2RDCNUM_SHIFT;
7375e689cf4aSJeff Kirsher 	tp->key_mask[1] = TCAM_V4KEY1_L2RDCNUM;
7376e689cf4aSJeff Kirsher 
7377e689cf4aSJeff Kirsher 	tp->key[3] = (u64)sip << TCAM_V4KEY3_SADDR_SHIFT;
7378e689cf4aSJeff Kirsher 	tp->key[3] |= dip;
7379e689cf4aSJeff Kirsher 
7380e689cf4aSJeff Kirsher 	tp->key_mask[3] = (u64)sipm << TCAM_V4KEY3_SADDR_SHIFT;
7381e689cf4aSJeff Kirsher 	tp->key_mask[3] |= dipm;
7382e689cf4aSJeff Kirsher 
7383e689cf4aSJeff Kirsher 	tp->key[2] |= ((u64)fsp->h_u.tcp_ip4_spec.tos <<
7384e689cf4aSJeff Kirsher 		       TCAM_V4KEY2_TOS_SHIFT);
7385e689cf4aSJeff Kirsher 	tp->key_mask[2] |= ((u64)fsp->m_u.tcp_ip4_spec.tos <<
7386e689cf4aSJeff Kirsher 			    TCAM_V4KEY2_TOS_SHIFT);
7387e689cf4aSJeff Kirsher 	switch (fsp->flow_type) {
7388e689cf4aSJeff Kirsher 	case TCP_V4_FLOW:
7389e689cf4aSJeff Kirsher 	case UDP_V4_FLOW:
7390e689cf4aSJeff Kirsher 	case SCTP_V4_FLOW:
7391e689cf4aSJeff Kirsher 		sport = be16_to_cpu(fsp->h_u.tcp_ip4_spec.psrc);
7392e689cf4aSJeff Kirsher 		spm = be16_to_cpu(fsp->m_u.tcp_ip4_spec.psrc);
7393e689cf4aSJeff Kirsher 		dport = be16_to_cpu(fsp->h_u.tcp_ip4_spec.pdst);
7394e689cf4aSJeff Kirsher 		dpm = be16_to_cpu(fsp->m_u.tcp_ip4_spec.pdst);
7395e689cf4aSJeff Kirsher 
7396e689cf4aSJeff Kirsher 		tp->key[2] |= (((u64)sport << 16) | dport);
7397e689cf4aSJeff Kirsher 		tp->key_mask[2] |= (((u64)spm << 16) | dpm);
7398e689cf4aSJeff Kirsher 		niu_ethflow_to_l3proto(fsp->flow_type, &pid);
7399e689cf4aSJeff Kirsher 		break;
7400e689cf4aSJeff Kirsher 	case AH_V4_FLOW:
7401e689cf4aSJeff Kirsher 	case ESP_V4_FLOW:
7402e689cf4aSJeff Kirsher 		spi = be32_to_cpu(fsp->h_u.ah_ip4_spec.spi);
7403e689cf4aSJeff Kirsher 		spim = be32_to_cpu(fsp->m_u.ah_ip4_spec.spi);
7404e689cf4aSJeff Kirsher 
7405e689cf4aSJeff Kirsher 		tp->key[2] |= spi;
7406e689cf4aSJeff Kirsher 		tp->key_mask[2] |= spim;
7407e689cf4aSJeff Kirsher 		niu_ethflow_to_l3proto(fsp->flow_type, &pid);
7408e689cf4aSJeff Kirsher 		break;
7409e689cf4aSJeff Kirsher 	case IP_USER_FLOW:
7410e689cf4aSJeff Kirsher 		spi = be32_to_cpu(fsp->h_u.usr_ip4_spec.l4_4_bytes);
7411e689cf4aSJeff Kirsher 		spim = be32_to_cpu(fsp->m_u.usr_ip4_spec.l4_4_bytes);
7412e689cf4aSJeff Kirsher 
7413e689cf4aSJeff Kirsher 		tp->key[2] |= spi;
7414e689cf4aSJeff Kirsher 		tp->key_mask[2] |= spim;
7415e689cf4aSJeff Kirsher 		pid = fsp->h_u.usr_ip4_spec.proto;
7416e689cf4aSJeff Kirsher 		break;
7417e689cf4aSJeff Kirsher 	default:
7418e689cf4aSJeff Kirsher 		break;
7419e689cf4aSJeff Kirsher 	}
7420e689cf4aSJeff Kirsher 
7421e689cf4aSJeff Kirsher 	tp->key[2] |= ((u64)pid << TCAM_V4KEY2_PROTO_SHIFT);
7422e689cf4aSJeff Kirsher 	if (pid) {
7423e689cf4aSJeff Kirsher 		tp->key_mask[2] |= TCAM_V4KEY2_PROTO;
7424e689cf4aSJeff Kirsher 	}
7425e689cf4aSJeff Kirsher }
7426e689cf4aSJeff Kirsher 
niu_add_ethtool_tcam_entry(struct niu * np,struct ethtool_rxnfc * nfc)7427e689cf4aSJeff Kirsher static int niu_add_ethtool_tcam_entry(struct niu *np,
7428e689cf4aSJeff Kirsher 				      struct ethtool_rxnfc *nfc)
7429e689cf4aSJeff Kirsher {
7430e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
7431e689cf4aSJeff Kirsher 	struct niu_tcam_entry *tp;
7432e689cf4aSJeff Kirsher 	struct ethtool_rx_flow_spec *fsp = &nfc->fs;
7433e689cf4aSJeff Kirsher 	struct niu_rdc_tables *rdc_table = &parent->rdc_group_cfg[np->port];
7434e689cf4aSJeff Kirsher 	int l2_rdc_table = rdc_table->first_table_num;
7435e689cf4aSJeff Kirsher 	u16 idx;
7436e689cf4aSJeff Kirsher 	u64 class;
7437e689cf4aSJeff Kirsher 	unsigned long flags;
7438e689cf4aSJeff Kirsher 	int err, ret;
7439e689cf4aSJeff Kirsher 
7440e689cf4aSJeff Kirsher 	ret = 0;
7441e689cf4aSJeff Kirsher 
7442e689cf4aSJeff Kirsher 	idx = nfc->fs.location;
7443e689cf4aSJeff Kirsher 	if (idx >= tcam_get_size(np))
7444e689cf4aSJeff Kirsher 		return -EINVAL;
7445e689cf4aSJeff Kirsher 
7446e689cf4aSJeff Kirsher 	if (fsp->flow_type == IP_USER_FLOW) {
7447e689cf4aSJeff Kirsher 		int i;
7448e689cf4aSJeff Kirsher 		int add_usr_cls = 0;
7449e689cf4aSJeff Kirsher 		struct ethtool_usrip4_spec *uspec = &fsp->h_u.usr_ip4_spec;
7450e689cf4aSJeff Kirsher 		struct ethtool_usrip4_spec *umask = &fsp->m_u.usr_ip4_spec;
7451e689cf4aSJeff Kirsher 
7452e689cf4aSJeff Kirsher 		if (uspec->ip_ver != ETH_RX_NFC_IP4)
7453e689cf4aSJeff Kirsher 			return -EINVAL;
7454e689cf4aSJeff Kirsher 
7455e689cf4aSJeff Kirsher 		niu_lock_parent(np, flags);
7456e689cf4aSJeff Kirsher 
7457e689cf4aSJeff Kirsher 		for (i = 0; i < NIU_L3_PROG_CLS; i++) {
7458e689cf4aSJeff Kirsher 			if (parent->l3_cls[i]) {
7459e689cf4aSJeff Kirsher 				if (uspec->proto == parent->l3_cls_pid[i]) {
7460e689cf4aSJeff Kirsher 					class = parent->l3_cls[i];
7461e689cf4aSJeff Kirsher 					parent->l3_cls_refcnt[i]++;
7462e689cf4aSJeff Kirsher 					add_usr_cls = 1;
7463e689cf4aSJeff Kirsher 					break;
7464e689cf4aSJeff Kirsher 				}
7465e689cf4aSJeff Kirsher 			} else {
7466e689cf4aSJeff Kirsher 				/* Program new user IP class */
7467e689cf4aSJeff Kirsher 				switch (i) {
7468e689cf4aSJeff Kirsher 				case 0:
7469e689cf4aSJeff Kirsher 					class = CLASS_CODE_USER_PROG1;
7470e689cf4aSJeff Kirsher 					break;
7471e689cf4aSJeff Kirsher 				case 1:
7472e689cf4aSJeff Kirsher 					class = CLASS_CODE_USER_PROG2;
7473e689cf4aSJeff Kirsher 					break;
7474e689cf4aSJeff Kirsher 				case 2:
7475e689cf4aSJeff Kirsher 					class = CLASS_CODE_USER_PROG3;
7476e689cf4aSJeff Kirsher 					break;
7477e689cf4aSJeff Kirsher 				case 3:
7478e689cf4aSJeff Kirsher 					class = CLASS_CODE_USER_PROG4;
7479e689cf4aSJeff Kirsher 					break;
7480e689cf4aSJeff Kirsher 				default:
748109073525SNathan Chancellor 					class = CLASS_CODE_UNRECOG;
7482e689cf4aSJeff Kirsher 					break;
7483e689cf4aSJeff Kirsher 				}
7484e689cf4aSJeff Kirsher 				ret = tcam_user_ip_class_set(np, class, 0,
7485e689cf4aSJeff Kirsher 							     uspec->proto,
7486e689cf4aSJeff Kirsher 							     uspec->tos,
7487e689cf4aSJeff Kirsher 							     umask->tos);
7488e689cf4aSJeff Kirsher 				if (ret)
7489e689cf4aSJeff Kirsher 					goto out;
7490e689cf4aSJeff Kirsher 
7491e689cf4aSJeff Kirsher 				ret = tcam_user_ip_class_enable(np, class, 1);
7492e689cf4aSJeff Kirsher 				if (ret)
7493e689cf4aSJeff Kirsher 					goto out;
7494e689cf4aSJeff Kirsher 				parent->l3_cls[i] = class;
7495e689cf4aSJeff Kirsher 				parent->l3_cls_pid[i] = uspec->proto;
7496e689cf4aSJeff Kirsher 				parent->l3_cls_refcnt[i]++;
7497e689cf4aSJeff Kirsher 				add_usr_cls = 1;
7498e689cf4aSJeff Kirsher 				break;
7499e689cf4aSJeff Kirsher 			}
7500e689cf4aSJeff Kirsher 		}
7501e689cf4aSJeff Kirsher 		if (!add_usr_cls) {
7502e689cf4aSJeff Kirsher 			netdev_info(np->dev, "niu%d: %s(): Could not find/insert class for pid %d\n",
7503e689cf4aSJeff Kirsher 				    parent->index, __func__, uspec->proto);
7504e689cf4aSJeff Kirsher 			ret = -EINVAL;
7505e689cf4aSJeff Kirsher 			goto out;
7506e689cf4aSJeff Kirsher 		}
7507e689cf4aSJeff Kirsher 		niu_unlock_parent(np, flags);
7508e689cf4aSJeff Kirsher 	} else {
7509e689cf4aSJeff Kirsher 		if (!niu_ethflow_to_class(fsp->flow_type, &class)) {
7510e689cf4aSJeff Kirsher 			return -EINVAL;
7511e689cf4aSJeff Kirsher 		}
7512e689cf4aSJeff Kirsher 	}
7513e689cf4aSJeff Kirsher 
7514e689cf4aSJeff Kirsher 	niu_lock_parent(np, flags);
7515e689cf4aSJeff Kirsher 
7516e689cf4aSJeff Kirsher 	idx = tcam_get_index(np, idx);
7517e689cf4aSJeff Kirsher 	tp = &parent->tcam[idx];
7518e689cf4aSJeff Kirsher 
7519e689cf4aSJeff Kirsher 	memset(tp, 0, sizeof(*tp));
7520e689cf4aSJeff Kirsher 
7521e689cf4aSJeff Kirsher 	/* fill in the tcam key and mask */
7522e689cf4aSJeff Kirsher 	switch (fsp->flow_type) {
7523e689cf4aSJeff Kirsher 	case TCP_V4_FLOW:
7524e689cf4aSJeff Kirsher 	case UDP_V4_FLOW:
7525e689cf4aSJeff Kirsher 	case SCTP_V4_FLOW:
7526e689cf4aSJeff Kirsher 	case AH_V4_FLOW:
7527e689cf4aSJeff Kirsher 	case ESP_V4_FLOW:
7528e689cf4aSJeff Kirsher 		niu_get_tcamkey_from_ip4fs(fsp, tp, l2_rdc_table, class);
7529e689cf4aSJeff Kirsher 		break;
7530e689cf4aSJeff Kirsher 	case TCP_V6_FLOW:
7531e689cf4aSJeff Kirsher 	case UDP_V6_FLOW:
7532e689cf4aSJeff Kirsher 	case SCTP_V6_FLOW:
7533e689cf4aSJeff Kirsher 	case AH_V6_FLOW:
7534e689cf4aSJeff Kirsher 	case ESP_V6_FLOW:
7535e689cf4aSJeff Kirsher 		/* Not yet implemented */
7536e689cf4aSJeff Kirsher 		netdev_info(np->dev, "niu%d: In %s(): flow %d for IPv6 not implemented\n",
7537e689cf4aSJeff Kirsher 			    parent->index, __func__, fsp->flow_type);
7538e689cf4aSJeff Kirsher 		ret = -EINVAL;
7539e689cf4aSJeff Kirsher 		goto out;
7540e689cf4aSJeff Kirsher 	case IP_USER_FLOW:
7541e689cf4aSJeff Kirsher 		niu_get_tcamkey_from_ip4fs(fsp, tp, l2_rdc_table, class);
7542e689cf4aSJeff Kirsher 		break;
7543e689cf4aSJeff Kirsher 	default:
7544e689cf4aSJeff Kirsher 		netdev_info(np->dev, "niu%d: In %s(): Unknown flow type %d\n",
7545e689cf4aSJeff Kirsher 			    parent->index, __func__, fsp->flow_type);
7546e689cf4aSJeff Kirsher 		ret = -EINVAL;
7547e689cf4aSJeff Kirsher 		goto out;
7548e689cf4aSJeff Kirsher 	}
7549e689cf4aSJeff Kirsher 
7550e689cf4aSJeff Kirsher 	/* fill in the assoc data */
7551e689cf4aSJeff Kirsher 	if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
7552e689cf4aSJeff Kirsher 		tp->assoc_data = TCAM_ASSOCDATA_DISC;
7553e689cf4aSJeff Kirsher 	} else {
7554e689cf4aSJeff Kirsher 		if (fsp->ring_cookie >= np->num_rx_rings) {
7555e689cf4aSJeff Kirsher 			netdev_info(np->dev, "niu%d: In %s(): Invalid RX ring %lld\n",
7556e689cf4aSJeff Kirsher 				    parent->index, __func__,
7557e689cf4aSJeff Kirsher 				    (long long)fsp->ring_cookie);
7558e689cf4aSJeff Kirsher 			ret = -EINVAL;
7559e689cf4aSJeff Kirsher 			goto out;
7560e689cf4aSJeff Kirsher 		}
7561e689cf4aSJeff Kirsher 		tp->assoc_data = (TCAM_ASSOCDATA_TRES_USE_OFFSET |
7562e689cf4aSJeff Kirsher 				  (fsp->ring_cookie <<
7563e689cf4aSJeff Kirsher 				   TCAM_ASSOCDATA_OFFSET_SHIFT));
7564e689cf4aSJeff Kirsher 	}
7565e689cf4aSJeff Kirsher 
7566e689cf4aSJeff Kirsher 	err = tcam_write(np, idx, tp->key, tp->key_mask);
7567e689cf4aSJeff Kirsher 	if (err) {
7568e689cf4aSJeff Kirsher 		ret = -EINVAL;
7569e689cf4aSJeff Kirsher 		goto out;
7570e689cf4aSJeff Kirsher 	}
7571e689cf4aSJeff Kirsher 	err = tcam_assoc_write(np, idx, tp->assoc_data);
7572e689cf4aSJeff Kirsher 	if (err) {
7573e689cf4aSJeff Kirsher 		ret = -EINVAL;
7574e689cf4aSJeff Kirsher 		goto out;
7575e689cf4aSJeff Kirsher 	}
7576e689cf4aSJeff Kirsher 
7577e689cf4aSJeff Kirsher 	/* validate the entry */
7578e689cf4aSJeff Kirsher 	tp->valid = 1;
7579e689cf4aSJeff Kirsher 	np->clas.tcam_valid_entries++;
7580e689cf4aSJeff Kirsher out:
7581e689cf4aSJeff Kirsher 	niu_unlock_parent(np, flags);
7582e689cf4aSJeff Kirsher 
7583e689cf4aSJeff Kirsher 	return ret;
7584e689cf4aSJeff Kirsher }
7585e689cf4aSJeff Kirsher 
niu_del_ethtool_tcam_entry(struct niu * np,u32 loc)7586e689cf4aSJeff Kirsher static int niu_del_ethtool_tcam_entry(struct niu *np, u32 loc)
7587e689cf4aSJeff Kirsher {
7588e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
7589e689cf4aSJeff Kirsher 	struct niu_tcam_entry *tp;
7590e689cf4aSJeff Kirsher 	u16 idx;
7591e689cf4aSJeff Kirsher 	unsigned long flags;
7592e689cf4aSJeff Kirsher 	u64 class;
7593e689cf4aSJeff Kirsher 	int ret = 0;
7594e689cf4aSJeff Kirsher 
7595e689cf4aSJeff Kirsher 	if (loc >= tcam_get_size(np))
7596e689cf4aSJeff Kirsher 		return -EINVAL;
7597e689cf4aSJeff Kirsher 
7598e689cf4aSJeff Kirsher 	niu_lock_parent(np, flags);
7599e689cf4aSJeff Kirsher 
7600e689cf4aSJeff Kirsher 	idx = tcam_get_index(np, loc);
7601e689cf4aSJeff Kirsher 	tp = &parent->tcam[idx];
7602e689cf4aSJeff Kirsher 
7603e689cf4aSJeff Kirsher 	/* if the entry is of a user defined class, then update*/
7604e689cf4aSJeff Kirsher 	class = (tp->key[0] & TCAM_V4KEY0_CLASS_CODE) >>
7605e689cf4aSJeff Kirsher 		TCAM_V4KEY0_CLASS_CODE_SHIFT;
7606e689cf4aSJeff Kirsher 
7607e689cf4aSJeff Kirsher 	if (class >= CLASS_CODE_USER_PROG1 && class <= CLASS_CODE_USER_PROG4) {
7608e689cf4aSJeff Kirsher 		int i;
7609e689cf4aSJeff Kirsher 		for (i = 0; i < NIU_L3_PROG_CLS; i++) {
7610e689cf4aSJeff Kirsher 			if (parent->l3_cls[i] == class) {
7611e689cf4aSJeff Kirsher 				parent->l3_cls_refcnt[i]--;
7612e689cf4aSJeff Kirsher 				if (!parent->l3_cls_refcnt[i]) {
7613e689cf4aSJeff Kirsher 					/* disable class */
7614e689cf4aSJeff Kirsher 					ret = tcam_user_ip_class_enable(np,
7615e689cf4aSJeff Kirsher 									class,
7616e689cf4aSJeff Kirsher 									0);
7617e689cf4aSJeff Kirsher 					if (ret)
7618e689cf4aSJeff Kirsher 						goto out;
7619e689cf4aSJeff Kirsher 					parent->l3_cls[i] = 0;
7620e689cf4aSJeff Kirsher 					parent->l3_cls_pid[i] = 0;
7621e689cf4aSJeff Kirsher 				}
7622e689cf4aSJeff Kirsher 				break;
7623e689cf4aSJeff Kirsher 			}
7624e689cf4aSJeff Kirsher 		}
7625e689cf4aSJeff Kirsher 		if (i == NIU_L3_PROG_CLS) {
7626e689cf4aSJeff Kirsher 			netdev_info(np->dev, "niu%d: In %s(): Usr class 0x%llx not found\n",
7627e689cf4aSJeff Kirsher 				    parent->index, __func__,
7628e689cf4aSJeff Kirsher 				    (unsigned long long)class);
7629e689cf4aSJeff Kirsher 			ret = -EINVAL;
7630e689cf4aSJeff Kirsher 			goto out;
7631e689cf4aSJeff Kirsher 		}
7632e689cf4aSJeff Kirsher 	}
7633e689cf4aSJeff Kirsher 
7634e689cf4aSJeff Kirsher 	ret = tcam_flush(np, idx);
7635e689cf4aSJeff Kirsher 	if (ret)
7636e689cf4aSJeff Kirsher 		goto out;
7637e689cf4aSJeff Kirsher 
7638e689cf4aSJeff Kirsher 	/* invalidate the entry */
7639e689cf4aSJeff Kirsher 	tp->valid = 0;
7640e689cf4aSJeff Kirsher 	np->clas.tcam_valid_entries--;
7641e689cf4aSJeff Kirsher out:
7642e689cf4aSJeff Kirsher 	niu_unlock_parent(np, flags);
7643e689cf4aSJeff Kirsher 
7644e689cf4aSJeff Kirsher 	return ret;
7645e689cf4aSJeff Kirsher }
7646e689cf4aSJeff Kirsher 
niu_set_nfc(struct net_device * dev,struct ethtool_rxnfc * cmd)7647e689cf4aSJeff Kirsher static int niu_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
7648e689cf4aSJeff Kirsher {
7649e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
7650e689cf4aSJeff Kirsher 	int ret = 0;
7651e689cf4aSJeff Kirsher 
7652e689cf4aSJeff Kirsher 	switch (cmd->cmd) {
7653e689cf4aSJeff Kirsher 	case ETHTOOL_SRXFH:
7654e689cf4aSJeff Kirsher 		ret = niu_set_hash_opts(np, cmd);
7655e689cf4aSJeff Kirsher 		break;
7656e689cf4aSJeff Kirsher 	case ETHTOOL_SRXCLSRLINS:
7657e689cf4aSJeff Kirsher 		ret = niu_add_ethtool_tcam_entry(np, cmd);
7658e689cf4aSJeff Kirsher 		break;
7659e689cf4aSJeff Kirsher 	case ETHTOOL_SRXCLSRLDEL:
7660e689cf4aSJeff Kirsher 		ret = niu_del_ethtool_tcam_entry(np, cmd->fs.location);
7661e689cf4aSJeff Kirsher 		break;
7662e689cf4aSJeff Kirsher 	default:
7663e689cf4aSJeff Kirsher 		ret = -EINVAL;
7664e689cf4aSJeff Kirsher 		break;
7665e689cf4aSJeff Kirsher 	}
7666e689cf4aSJeff Kirsher 
7667e689cf4aSJeff Kirsher 	return ret;
7668e689cf4aSJeff Kirsher }
7669e689cf4aSJeff Kirsher 
7670e689cf4aSJeff Kirsher static const struct {
7671e689cf4aSJeff Kirsher 	const char string[ETH_GSTRING_LEN];
7672e689cf4aSJeff Kirsher } niu_xmac_stat_keys[] = {
7673e689cf4aSJeff Kirsher 	{ "tx_frames" },
7674e689cf4aSJeff Kirsher 	{ "tx_bytes" },
7675e689cf4aSJeff Kirsher 	{ "tx_fifo_errors" },
7676e689cf4aSJeff Kirsher 	{ "tx_overflow_errors" },
7677e689cf4aSJeff Kirsher 	{ "tx_max_pkt_size_errors" },
7678e689cf4aSJeff Kirsher 	{ "tx_underflow_errors" },
7679e689cf4aSJeff Kirsher 	{ "rx_local_faults" },
7680e689cf4aSJeff Kirsher 	{ "rx_remote_faults" },
7681e689cf4aSJeff Kirsher 	{ "rx_link_faults" },
7682e689cf4aSJeff Kirsher 	{ "rx_align_errors" },
7683e689cf4aSJeff Kirsher 	{ "rx_frags" },
7684e689cf4aSJeff Kirsher 	{ "rx_mcasts" },
7685e689cf4aSJeff Kirsher 	{ "rx_bcasts" },
7686e689cf4aSJeff Kirsher 	{ "rx_hist_cnt1" },
7687e689cf4aSJeff Kirsher 	{ "rx_hist_cnt2" },
7688e689cf4aSJeff Kirsher 	{ "rx_hist_cnt3" },
7689e689cf4aSJeff Kirsher 	{ "rx_hist_cnt4" },
7690e689cf4aSJeff Kirsher 	{ "rx_hist_cnt5" },
7691e689cf4aSJeff Kirsher 	{ "rx_hist_cnt6" },
7692e689cf4aSJeff Kirsher 	{ "rx_hist_cnt7" },
7693e689cf4aSJeff Kirsher 	{ "rx_octets" },
7694e689cf4aSJeff Kirsher 	{ "rx_code_violations" },
7695e689cf4aSJeff Kirsher 	{ "rx_len_errors" },
7696e689cf4aSJeff Kirsher 	{ "rx_crc_errors" },
7697e689cf4aSJeff Kirsher 	{ "rx_underflows" },
7698e689cf4aSJeff Kirsher 	{ "rx_overflows" },
7699e689cf4aSJeff Kirsher 	{ "pause_off_state" },
7700e689cf4aSJeff Kirsher 	{ "pause_on_state" },
7701e689cf4aSJeff Kirsher 	{ "pause_received" },
7702e689cf4aSJeff Kirsher };
7703e689cf4aSJeff Kirsher 
7704e689cf4aSJeff Kirsher #define NUM_XMAC_STAT_KEYS	ARRAY_SIZE(niu_xmac_stat_keys)
7705e689cf4aSJeff Kirsher 
7706e689cf4aSJeff Kirsher static const struct {
7707e689cf4aSJeff Kirsher 	const char string[ETH_GSTRING_LEN];
7708e689cf4aSJeff Kirsher } niu_bmac_stat_keys[] = {
7709e689cf4aSJeff Kirsher 	{ "tx_underflow_errors" },
7710e689cf4aSJeff Kirsher 	{ "tx_max_pkt_size_errors" },
7711e689cf4aSJeff Kirsher 	{ "tx_bytes" },
7712e689cf4aSJeff Kirsher 	{ "tx_frames" },
7713e689cf4aSJeff Kirsher 	{ "rx_overflows" },
7714e689cf4aSJeff Kirsher 	{ "rx_frames" },
7715e689cf4aSJeff Kirsher 	{ "rx_align_errors" },
7716e689cf4aSJeff Kirsher 	{ "rx_crc_errors" },
7717e689cf4aSJeff Kirsher 	{ "rx_len_errors" },
7718e689cf4aSJeff Kirsher 	{ "pause_off_state" },
7719e689cf4aSJeff Kirsher 	{ "pause_on_state" },
7720e689cf4aSJeff Kirsher 	{ "pause_received" },
7721e689cf4aSJeff Kirsher };
7722e689cf4aSJeff Kirsher 
7723e689cf4aSJeff Kirsher #define NUM_BMAC_STAT_KEYS	ARRAY_SIZE(niu_bmac_stat_keys)
7724e689cf4aSJeff Kirsher 
7725e689cf4aSJeff Kirsher static const struct {
7726e689cf4aSJeff Kirsher 	const char string[ETH_GSTRING_LEN];
7727e689cf4aSJeff Kirsher } niu_rxchan_stat_keys[] = {
7728e689cf4aSJeff Kirsher 	{ "rx_channel" },
7729e689cf4aSJeff Kirsher 	{ "rx_packets" },
7730e689cf4aSJeff Kirsher 	{ "rx_bytes" },
7731e689cf4aSJeff Kirsher 	{ "rx_dropped" },
7732e689cf4aSJeff Kirsher 	{ "rx_errors" },
7733e689cf4aSJeff Kirsher };
7734e689cf4aSJeff Kirsher 
7735e689cf4aSJeff Kirsher #define NUM_RXCHAN_STAT_KEYS	ARRAY_SIZE(niu_rxchan_stat_keys)
7736e689cf4aSJeff Kirsher 
7737e689cf4aSJeff Kirsher static const struct {
7738e689cf4aSJeff Kirsher 	const char string[ETH_GSTRING_LEN];
7739e689cf4aSJeff Kirsher } niu_txchan_stat_keys[] = {
7740e689cf4aSJeff Kirsher 	{ "tx_channel" },
7741e689cf4aSJeff Kirsher 	{ "tx_packets" },
7742e689cf4aSJeff Kirsher 	{ "tx_bytes" },
7743e689cf4aSJeff Kirsher 	{ "tx_errors" },
7744e689cf4aSJeff Kirsher };
7745e689cf4aSJeff Kirsher 
7746e689cf4aSJeff Kirsher #define NUM_TXCHAN_STAT_KEYS	ARRAY_SIZE(niu_txchan_stat_keys)
7747e689cf4aSJeff Kirsher 
niu_get_strings(struct net_device * dev,u32 stringset,u8 * data)7748e689cf4aSJeff Kirsher static void niu_get_strings(struct net_device *dev, u32 stringset, u8 *data)
7749e689cf4aSJeff Kirsher {
7750e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
7751e689cf4aSJeff Kirsher 	int i;
7752e689cf4aSJeff Kirsher 
7753e689cf4aSJeff Kirsher 	if (stringset != ETH_SS_STATS)
7754e689cf4aSJeff Kirsher 		return;
7755e689cf4aSJeff Kirsher 
7756e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC) {
7757e689cf4aSJeff Kirsher 		memcpy(data, niu_xmac_stat_keys,
7758e689cf4aSJeff Kirsher 		       sizeof(niu_xmac_stat_keys));
7759e689cf4aSJeff Kirsher 		data += sizeof(niu_xmac_stat_keys);
7760e689cf4aSJeff Kirsher 	} else {
7761e689cf4aSJeff Kirsher 		memcpy(data, niu_bmac_stat_keys,
7762e689cf4aSJeff Kirsher 		       sizeof(niu_bmac_stat_keys));
7763e689cf4aSJeff Kirsher 		data += sizeof(niu_bmac_stat_keys);
7764e689cf4aSJeff Kirsher 	}
7765e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_rx_rings; i++) {
7766e689cf4aSJeff Kirsher 		memcpy(data, niu_rxchan_stat_keys,
7767e689cf4aSJeff Kirsher 		       sizeof(niu_rxchan_stat_keys));
7768e689cf4aSJeff Kirsher 		data += sizeof(niu_rxchan_stat_keys);
7769e689cf4aSJeff Kirsher 	}
7770e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_tx_rings; i++) {
7771e689cf4aSJeff Kirsher 		memcpy(data, niu_txchan_stat_keys,
7772e689cf4aSJeff Kirsher 		       sizeof(niu_txchan_stat_keys));
7773e689cf4aSJeff Kirsher 		data += sizeof(niu_txchan_stat_keys);
7774e689cf4aSJeff Kirsher 	}
7775e689cf4aSJeff Kirsher }
7776e689cf4aSJeff Kirsher 
niu_get_sset_count(struct net_device * dev,int stringset)7777e689cf4aSJeff Kirsher static int niu_get_sset_count(struct net_device *dev, int stringset)
7778e689cf4aSJeff Kirsher {
7779e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
7780e689cf4aSJeff Kirsher 
7781e689cf4aSJeff Kirsher 	if (stringset != ETH_SS_STATS)
7782e689cf4aSJeff Kirsher 		return -EINVAL;
7783e689cf4aSJeff Kirsher 
7784e689cf4aSJeff Kirsher 	return (np->flags & NIU_FLAGS_XMAC ?
7785e689cf4aSJeff Kirsher 		 NUM_XMAC_STAT_KEYS :
7786e689cf4aSJeff Kirsher 		 NUM_BMAC_STAT_KEYS) +
7787e689cf4aSJeff Kirsher 		(np->num_rx_rings * NUM_RXCHAN_STAT_KEYS) +
7788e689cf4aSJeff Kirsher 		(np->num_tx_rings * NUM_TXCHAN_STAT_KEYS);
7789e689cf4aSJeff Kirsher }
7790e689cf4aSJeff Kirsher 
niu_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)7791e689cf4aSJeff Kirsher static void niu_get_ethtool_stats(struct net_device *dev,
7792e689cf4aSJeff Kirsher 				  struct ethtool_stats *stats, u64 *data)
7793e689cf4aSJeff Kirsher {
7794e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
7795e689cf4aSJeff Kirsher 	int i;
7796e689cf4aSJeff Kirsher 
7797e689cf4aSJeff Kirsher 	niu_sync_mac_stats(np);
7798e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC) {
7799e689cf4aSJeff Kirsher 		memcpy(data, &np->mac_stats.xmac,
7800e689cf4aSJeff Kirsher 		       sizeof(struct niu_xmac_stats));
7801e689cf4aSJeff Kirsher 		data += (sizeof(struct niu_xmac_stats) / sizeof(u64));
7802e689cf4aSJeff Kirsher 	} else {
7803e689cf4aSJeff Kirsher 		memcpy(data, &np->mac_stats.bmac,
7804e689cf4aSJeff Kirsher 		       sizeof(struct niu_bmac_stats));
7805e689cf4aSJeff Kirsher 		data += (sizeof(struct niu_bmac_stats) / sizeof(u64));
7806e689cf4aSJeff Kirsher 	}
7807e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_rx_rings; i++) {
7808e689cf4aSJeff Kirsher 		struct rx_ring_info *rp = &np->rx_rings[i];
7809e689cf4aSJeff Kirsher 
7810e689cf4aSJeff Kirsher 		niu_sync_rx_discard_stats(np, rp, 0);
7811e689cf4aSJeff Kirsher 
7812e689cf4aSJeff Kirsher 		data[0] = rp->rx_channel;
7813e689cf4aSJeff Kirsher 		data[1] = rp->rx_packets;
7814e689cf4aSJeff Kirsher 		data[2] = rp->rx_bytes;
7815e689cf4aSJeff Kirsher 		data[3] = rp->rx_dropped;
7816e689cf4aSJeff Kirsher 		data[4] = rp->rx_errors;
7817e689cf4aSJeff Kirsher 		data += 5;
7818e689cf4aSJeff Kirsher 	}
7819e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_tx_rings; i++) {
7820e689cf4aSJeff Kirsher 		struct tx_ring_info *rp = &np->tx_rings[i];
7821e689cf4aSJeff Kirsher 
7822e689cf4aSJeff Kirsher 		data[0] = rp->tx_channel;
7823e689cf4aSJeff Kirsher 		data[1] = rp->tx_packets;
7824e689cf4aSJeff Kirsher 		data[2] = rp->tx_bytes;
7825e689cf4aSJeff Kirsher 		data[3] = rp->tx_errors;
7826e689cf4aSJeff Kirsher 		data += 4;
7827e689cf4aSJeff Kirsher 	}
7828e689cf4aSJeff Kirsher }
7829e689cf4aSJeff Kirsher 
niu_led_state_save(struct niu * np)7830e689cf4aSJeff Kirsher static u64 niu_led_state_save(struct niu *np)
7831e689cf4aSJeff Kirsher {
7832e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
7833e689cf4aSJeff Kirsher 		return nr64_mac(XMAC_CONFIG);
7834e689cf4aSJeff Kirsher 	else
7835e689cf4aSJeff Kirsher 		return nr64_mac(BMAC_XIF_CONFIG);
7836e689cf4aSJeff Kirsher }
7837e689cf4aSJeff Kirsher 
niu_led_state_restore(struct niu * np,u64 val)7838e689cf4aSJeff Kirsher static void niu_led_state_restore(struct niu *np, u64 val)
7839e689cf4aSJeff Kirsher {
7840e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC)
7841e689cf4aSJeff Kirsher 		nw64_mac(XMAC_CONFIG, val);
7842e689cf4aSJeff Kirsher 	else
7843e689cf4aSJeff Kirsher 		nw64_mac(BMAC_XIF_CONFIG, val);
7844e689cf4aSJeff Kirsher }
7845e689cf4aSJeff Kirsher 
niu_force_led(struct niu * np,int on)7846e689cf4aSJeff Kirsher static void niu_force_led(struct niu *np, int on)
7847e689cf4aSJeff Kirsher {
7848e689cf4aSJeff Kirsher 	u64 val, reg, bit;
7849e689cf4aSJeff Kirsher 
7850e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_XMAC) {
7851e689cf4aSJeff Kirsher 		reg = XMAC_CONFIG;
7852e689cf4aSJeff Kirsher 		bit = XMAC_CONFIG_FORCE_LED_ON;
7853e689cf4aSJeff Kirsher 	} else {
7854e689cf4aSJeff Kirsher 		reg = BMAC_XIF_CONFIG;
7855e689cf4aSJeff Kirsher 		bit = BMAC_XIF_CONFIG_LINK_LED;
7856e689cf4aSJeff Kirsher 	}
7857e689cf4aSJeff Kirsher 
7858e689cf4aSJeff Kirsher 	val = nr64_mac(reg);
7859e689cf4aSJeff Kirsher 	if (on)
7860e689cf4aSJeff Kirsher 		val |= bit;
7861e689cf4aSJeff Kirsher 	else
7862e689cf4aSJeff Kirsher 		val &= ~bit;
7863e689cf4aSJeff Kirsher 	nw64_mac(reg, val);
7864e689cf4aSJeff Kirsher }
7865e689cf4aSJeff Kirsher 
niu_set_phys_id(struct net_device * dev,enum ethtool_phys_id_state state)7866e689cf4aSJeff Kirsher static int niu_set_phys_id(struct net_device *dev,
7867e689cf4aSJeff Kirsher 			   enum ethtool_phys_id_state state)
7868e689cf4aSJeff Kirsher 
7869e689cf4aSJeff Kirsher {
7870e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
7871e689cf4aSJeff Kirsher 
7872e689cf4aSJeff Kirsher 	if (!netif_running(dev))
7873e689cf4aSJeff Kirsher 		return -EAGAIN;
7874e689cf4aSJeff Kirsher 
7875e689cf4aSJeff Kirsher 	switch (state) {
7876e689cf4aSJeff Kirsher 	case ETHTOOL_ID_ACTIVE:
7877e689cf4aSJeff Kirsher 		np->orig_led_state = niu_led_state_save(np);
7878e689cf4aSJeff Kirsher 		return 1;	/* cycle on/off once per second */
7879e689cf4aSJeff Kirsher 
7880e689cf4aSJeff Kirsher 	case ETHTOOL_ID_ON:
7881e689cf4aSJeff Kirsher 		niu_force_led(np, 1);
7882e689cf4aSJeff Kirsher 		break;
7883e689cf4aSJeff Kirsher 
7884e689cf4aSJeff Kirsher 	case ETHTOOL_ID_OFF:
7885e689cf4aSJeff Kirsher 		niu_force_led(np, 0);
7886e689cf4aSJeff Kirsher 		break;
7887e689cf4aSJeff Kirsher 
7888e689cf4aSJeff Kirsher 	case ETHTOOL_ID_INACTIVE:
7889e689cf4aSJeff Kirsher 		niu_led_state_restore(np, np->orig_led_state);
7890e689cf4aSJeff Kirsher 	}
7891e689cf4aSJeff Kirsher 
7892e689cf4aSJeff Kirsher 	return 0;
7893e689cf4aSJeff Kirsher }
7894e689cf4aSJeff Kirsher 
7895e689cf4aSJeff Kirsher static const struct ethtool_ops niu_ethtool_ops = {
7896e689cf4aSJeff Kirsher 	.get_drvinfo		= niu_get_drvinfo,
7897e689cf4aSJeff Kirsher 	.get_link		= ethtool_op_get_link,
7898e689cf4aSJeff Kirsher 	.get_msglevel		= niu_get_msglevel,
7899e689cf4aSJeff Kirsher 	.set_msglevel		= niu_set_msglevel,
7900e689cf4aSJeff Kirsher 	.nway_reset		= niu_nway_reset,
7901e689cf4aSJeff Kirsher 	.get_eeprom_len		= niu_get_eeprom_len,
7902e689cf4aSJeff Kirsher 	.get_eeprom		= niu_get_eeprom,
7903e689cf4aSJeff Kirsher 	.get_strings		= niu_get_strings,
7904e689cf4aSJeff Kirsher 	.get_sset_count		= niu_get_sset_count,
7905e689cf4aSJeff Kirsher 	.get_ethtool_stats	= niu_get_ethtool_stats,
7906e689cf4aSJeff Kirsher 	.set_phys_id		= niu_set_phys_id,
7907e689cf4aSJeff Kirsher 	.get_rxnfc		= niu_get_nfc,
7908e689cf4aSJeff Kirsher 	.set_rxnfc		= niu_set_nfc,
7909d9722531SPhilippe Reynes 	.get_link_ksettings	= niu_get_link_ksettings,
7910d9722531SPhilippe Reynes 	.set_link_ksettings	= niu_set_link_ksettings,
7911e689cf4aSJeff Kirsher };
7912e689cf4aSJeff Kirsher 
niu_ldg_assign_ldn(struct niu * np,struct niu_parent * parent,int ldg,int ldn)7913e689cf4aSJeff Kirsher static int niu_ldg_assign_ldn(struct niu *np, struct niu_parent *parent,
7914e689cf4aSJeff Kirsher 			      int ldg, int ldn)
7915e689cf4aSJeff Kirsher {
7916e689cf4aSJeff Kirsher 	if (ldg < NIU_LDG_MIN || ldg > NIU_LDG_MAX)
7917e689cf4aSJeff Kirsher 		return -EINVAL;
7918e689cf4aSJeff Kirsher 	if (ldn < 0 || ldn > LDN_MAX)
7919e689cf4aSJeff Kirsher 		return -EINVAL;
7920e689cf4aSJeff Kirsher 
7921e689cf4aSJeff Kirsher 	parent->ldg_map[ldn] = ldg;
7922e689cf4aSJeff Kirsher 
7923e689cf4aSJeff Kirsher 	if (np->parent->plat_type == PLAT_TYPE_NIU) {
7924e689cf4aSJeff Kirsher 		/* On N2 NIU, the ldn-->ldg assignments are setup and fixed by
7925e689cf4aSJeff Kirsher 		 * the firmware, and we're not supposed to change them.
7926e689cf4aSJeff Kirsher 		 * Validate the mapping, because if it's wrong we probably
7927e689cf4aSJeff Kirsher 		 * won't get any interrupts and that's painful to debug.
7928e689cf4aSJeff Kirsher 		 */
7929e689cf4aSJeff Kirsher 		if (nr64(LDG_NUM(ldn)) != ldg) {
793021c68644SColin Ian King 			dev_err(np->device, "Port %u, mismatched LDG assignment for ldn %d, should be %d is %llu\n",
7931e689cf4aSJeff Kirsher 				np->port, ldn, ldg,
7932e689cf4aSJeff Kirsher 				(unsigned long long) nr64(LDG_NUM(ldn)));
7933e689cf4aSJeff Kirsher 			return -EINVAL;
7934e689cf4aSJeff Kirsher 		}
7935e689cf4aSJeff Kirsher 	} else
7936e689cf4aSJeff Kirsher 		nw64(LDG_NUM(ldn), ldg);
7937e689cf4aSJeff Kirsher 
7938e689cf4aSJeff Kirsher 	return 0;
7939e689cf4aSJeff Kirsher }
7940e689cf4aSJeff Kirsher 
niu_set_ldg_timer_res(struct niu * np,int res)7941e689cf4aSJeff Kirsher static int niu_set_ldg_timer_res(struct niu *np, int res)
7942e689cf4aSJeff Kirsher {
7943e689cf4aSJeff Kirsher 	if (res < 0 || res > LDG_TIMER_RES_VAL)
7944e689cf4aSJeff Kirsher 		return -EINVAL;
7945e689cf4aSJeff Kirsher 
7946e689cf4aSJeff Kirsher 
7947e689cf4aSJeff Kirsher 	nw64(LDG_TIMER_RES, res);
7948e689cf4aSJeff Kirsher 
7949e689cf4aSJeff Kirsher 	return 0;
7950e689cf4aSJeff Kirsher }
7951e689cf4aSJeff Kirsher 
niu_set_ldg_sid(struct niu * np,int ldg,int func,int vector)7952e689cf4aSJeff Kirsher static int niu_set_ldg_sid(struct niu *np, int ldg, int func, int vector)
7953e689cf4aSJeff Kirsher {
7954e689cf4aSJeff Kirsher 	if ((ldg < NIU_LDG_MIN || ldg > NIU_LDG_MAX) ||
7955e689cf4aSJeff Kirsher 	    (func < 0 || func > 3) ||
7956e689cf4aSJeff Kirsher 	    (vector < 0 || vector > 0x1f))
7957e689cf4aSJeff Kirsher 		return -EINVAL;
7958e689cf4aSJeff Kirsher 
7959e689cf4aSJeff Kirsher 	nw64(SID(ldg), (func << SID_FUNC_SHIFT) | vector);
7960e689cf4aSJeff Kirsher 
7961e689cf4aSJeff Kirsher 	return 0;
7962e689cf4aSJeff Kirsher }
7963e689cf4aSJeff Kirsher 
niu_pci_eeprom_read(struct niu * np,u32 addr)7964f73d12bdSBill Pemberton static int niu_pci_eeprom_read(struct niu *np, u32 addr)
7965e689cf4aSJeff Kirsher {
7966e689cf4aSJeff Kirsher 	u64 frame, frame_base = (ESPC_PIO_STAT_READ_START |
7967e689cf4aSJeff Kirsher 				 (addr << ESPC_PIO_STAT_ADDR_SHIFT));
7968e689cf4aSJeff Kirsher 	int limit;
7969e689cf4aSJeff Kirsher 
7970e689cf4aSJeff Kirsher 	if (addr > (ESPC_PIO_STAT_ADDR >> ESPC_PIO_STAT_ADDR_SHIFT))
7971e689cf4aSJeff Kirsher 		return -EINVAL;
7972e689cf4aSJeff Kirsher 
7973e689cf4aSJeff Kirsher 	frame = frame_base;
7974e689cf4aSJeff Kirsher 	nw64(ESPC_PIO_STAT, frame);
7975e689cf4aSJeff Kirsher 	limit = 64;
7976e689cf4aSJeff Kirsher 	do {
7977e689cf4aSJeff Kirsher 		udelay(5);
7978e689cf4aSJeff Kirsher 		frame = nr64(ESPC_PIO_STAT);
7979e689cf4aSJeff Kirsher 		if (frame & ESPC_PIO_STAT_READ_END)
7980e689cf4aSJeff Kirsher 			break;
7981e689cf4aSJeff Kirsher 	} while (limit--);
7982e689cf4aSJeff Kirsher 	if (!(frame & ESPC_PIO_STAT_READ_END)) {
7983e689cf4aSJeff Kirsher 		dev_err(np->device, "EEPROM read timeout frame[%llx]\n",
7984e689cf4aSJeff Kirsher 			(unsigned long long) frame);
7985e689cf4aSJeff Kirsher 		return -ENODEV;
7986e689cf4aSJeff Kirsher 	}
7987e689cf4aSJeff Kirsher 
7988e689cf4aSJeff Kirsher 	frame = frame_base;
7989e689cf4aSJeff Kirsher 	nw64(ESPC_PIO_STAT, frame);
7990e689cf4aSJeff Kirsher 	limit = 64;
7991e689cf4aSJeff Kirsher 	do {
7992e689cf4aSJeff Kirsher 		udelay(5);
7993e689cf4aSJeff Kirsher 		frame = nr64(ESPC_PIO_STAT);
7994e689cf4aSJeff Kirsher 		if (frame & ESPC_PIO_STAT_READ_END)
7995e689cf4aSJeff Kirsher 			break;
7996e689cf4aSJeff Kirsher 	} while (limit--);
7997e689cf4aSJeff Kirsher 	if (!(frame & ESPC_PIO_STAT_READ_END)) {
7998e689cf4aSJeff Kirsher 		dev_err(np->device, "EEPROM read timeout frame[%llx]\n",
7999e689cf4aSJeff Kirsher 			(unsigned long long) frame);
8000e689cf4aSJeff Kirsher 		return -ENODEV;
8001e689cf4aSJeff Kirsher 	}
8002e689cf4aSJeff Kirsher 
8003e689cf4aSJeff Kirsher 	frame = nr64(ESPC_PIO_STAT);
8004e689cf4aSJeff Kirsher 	return (frame & ESPC_PIO_STAT_DATA) >> ESPC_PIO_STAT_DATA_SHIFT;
8005e689cf4aSJeff Kirsher }
8006e689cf4aSJeff Kirsher 
niu_pci_eeprom_read16(struct niu * np,u32 off)8007f73d12bdSBill Pemberton static int niu_pci_eeprom_read16(struct niu *np, u32 off)
8008e689cf4aSJeff Kirsher {
8009e689cf4aSJeff Kirsher 	int err = niu_pci_eeprom_read(np, off);
8010e689cf4aSJeff Kirsher 	u16 val;
8011e689cf4aSJeff Kirsher 
8012e689cf4aSJeff Kirsher 	if (err < 0)
8013e689cf4aSJeff Kirsher 		return err;
8014e689cf4aSJeff Kirsher 	val = (err << 8);
8015e689cf4aSJeff Kirsher 	err = niu_pci_eeprom_read(np, off + 1);
8016e689cf4aSJeff Kirsher 	if (err < 0)
8017e689cf4aSJeff Kirsher 		return err;
8018e689cf4aSJeff Kirsher 	val |= (err & 0xff);
8019e689cf4aSJeff Kirsher 
8020e689cf4aSJeff Kirsher 	return val;
8021e689cf4aSJeff Kirsher }
8022e689cf4aSJeff Kirsher 
niu_pci_eeprom_read16_swp(struct niu * np,u32 off)8023f73d12bdSBill Pemberton static int niu_pci_eeprom_read16_swp(struct niu *np, u32 off)
8024e689cf4aSJeff Kirsher {
8025e689cf4aSJeff Kirsher 	int err = niu_pci_eeprom_read(np, off);
8026e689cf4aSJeff Kirsher 	u16 val;
8027e689cf4aSJeff Kirsher 
8028e689cf4aSJeff Kirsher 	if (err < 0)
8029e689cf4aSJeff Kirsher 		return err;
8030e689cf4aSJeff Kirsher 
8031e689cf4aSJeff Kirsher 	val = (err & 0xff);
8032e689cf4aSJeff Kirsher 	err = niu_pci_eeprom_read(np, off + 1);
8033e689cf4aSJeff Kirsher 	if (err < 0)
8034e689cf4aSJeff Kirsher 		return err;
8035e689cf4aSJeff Kirsher 
8036e689cf4aSJeff Kirsher 	val |= (err & 0xff) << 8;
8037e689cf4aSJeff Kirsher 
8038e689cf4aSJeff Kirsher 	return val;
8039e689cf4aSJeff Kirsher }
8040e689cf4aSJeff Kirsher 
niu_pci_vpd_get_propname(struct niu * np,u32 off,char * namebuf,int namebuf_len)80411dd06ae8SGreg Kroah-Hartman static int niu_pci_vpd_get_propname(struct niu *np, u32 off, char *namebuf,
8042e689cf4aSJeff Kirsher 				    int namebuf_len)
8043e689cf4aSJeff Kirsher {
8044e689cf4aSJeff Kirsher 	int i;
8045e689cf4aSJeff Kirsher 
8046e689cf4aSJeff Kirsher 	for (i = 0; i < namebuf_len; i++) {
8047e689cf4aSJeff Kirsher 		int err = niu_pci_eeprom_read(np, off + i);
8048e689cf4aSJeff Kirsher 		if (err < 0)
8049e689cf4aSJeff Kirsher 			return err;
8050e689cf4aSJeff Kirsher 		*namebuf++ = err;
8051e689cf4aSJeff Kirsher 		if (!err)
8052e689cf4aSJeff Kirsher 			break;
8053e689cf4aSJeff Kirsher 	}
8054e689cf4aSJeff Kirsher 	if (i >= namebuf_len)
8055e689cf4aSJeff Kirsher 		return -EINVAL;
8056e689cf4aSJeff Kirsher 
8057e689cf4aSJeff Kirsher 	return i + 1;
8058e689cf4aSJeff Kirsher }
8059e689cf4aSJeff Kirsher 
niu_vpd_parse_version(struct niu * np)8060f73d12bdSBill Pemberton static void niu_vpd_parse_version(struct niu *np)
8061e689cf4aSJeff Kirsher {
8062e689cf4aSJeff Kirsher 	struct niu_vpd *vpd = &np->vpd;
8063e689cf4aSJeff Kirsher 	int len = strlen(vpd->version) + 1;
8064e689cf4aSJeff Kirsher 	const char *s = vpd->version;
8065e689cf4aSJeff Kirsher 	int i;
8066e689cf4aSJeff Kirsher 
8067e689cf4aSJeff Kirsher 	for (i = 0; i < len - 5; i++) {
8068e689cf4aSJeff Kirsher 		if (!strncmp(s + i, "FCode ", 6))
8069e689cf4aSJeff Kirsher 			break;
8070e689cf4aSJeff Kirsher 	}
8071e689cf4aSJeff Kirsher 	if (i >= len - 5)
8072e689cf4aSJeff Kirsher 		return;
8073e689cf4aSJeff Kirsher 
8074e689cf4aSJeff Kirsher 	s += i + 5;
8075e689cf4aSJeff Kirsher 	sscanf(s, "%d.%d", &vpd->fcode_major, &vpd->fcode_minor);
8076e689cf4aSJeff Kirsher 
8077e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8078e689cf4aSJeff Kirsher 		     "VPD_SCAN: FCODE major(%d) minor(%d)\n",
8079e689cf4aSJeff Kirsher 		     vpd->fcode_major, vpd->fcode_minor);
8080e689cf4aSJeff Kirsher 	if (vpd->fcode_major > NIU_VPD_MIN_MAJOR ||
8081e689cf4aSJeff Kirsher 	    (vpd->fcode_major == NIU_VPD_MIN_MAJOR &&
8082e689cf4aSJeff Kirsher 	     vpd->fcode_minor >= NIU_VPD_MIN_MINOR))
8083e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_VPD_VALID;
8084e689cf4aSJeff Kirsher }
8085e689cf4aSJeff Kirsher 
8086e689cf4aSJeff Kirsher /* ESPC_PIO_EN_ENABLE must be set */
niu_pci_vpd_scan_props(struct niu * np,u32 start,u32 end)80871dd06ae8SGreg Kroah-Hartman static int niu_pci_vpd_scan_props(struct niu *np, u32 start, u32 end)
8088e689cf4aSJeff Kirsher {
8089e689cf4aSJeff Kirsher 	unsigned int found_mask = 0;
8090e689cf4aSJeff Kirsher #define FOUND_MASK_MODEL	0x00000001
8091e689cf4aSJeff Kirsher #define FOUND_MASK_BMODEL	0x00000002
8092e689cf4aSJeff Kirsher #define FOUND_MASK_VERS		0x00000004
8093e689cf4aSJeff Kirsher #define FOUND_MASK_MAC		0x00000008
8094e689cf4aSJeff Kirsher #define FOUND_MASK_NMAC		0x00000010
8095e689cf4aSJeff Kirsher #define FOUND_MASK_PHY		0x00000020
8096e689cf4aSJeff Kirsher #define FOUND_MASK_ALL		0x0000003f
8097e689cf4aSJeff Kirsher 
8098e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8099e689cf4aSJeff Kirsher 		     "VPD_SCAN: start[%x] end[%x]\n", start, end);
8100e689cf4aSJeff Kirsher 	while (start < end) {
8101e689cf4aSJeff Kirsher 		int len, err, prop_len;
8102e689cf4aSJeff Kirsher 		char namebuf[64];
8103e689cf4aSJeff Kirsher 		u8 *prop_buf;
8104e689cf4aSJeff Kirsher 		int max_len;
8105e689cf4aSJeff Kirsher 
8106e689cf4aSJeff Kirsher 		if (found_mask == FOUND_MASK_ALL) {
8107e689cf4aSJeff Kirsher 			niu_vpd_parse_version(np);
8108e689cf4aSJeff Kirsher 			return 1;
8109e689cf4aSJeff Kirsher 		}
8110e689cf4aSJeff Kirsher 
8111e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read(np, start + 2);
8112e689cf4aSJeff Kirsher 		if (err < 0)
8113e689cf4aSJeff Kirsher 			return err;
8114e689cf4aSJeff Kirsher 		len = err;
8115e689cf4aSJeff Kirsher 		start += 3;
8116e689cf4aSJeff Kirsher 
8117e689cf4aSJeff Kirsher 		prop_len = niu_pci_eeprom_read(np, start + 4);
8118e6e33770SDu Cheng 		if (prop_len < 0)
8119e6e33770SDu Cheng 			return prop_len;
8120e689cf4aSJeff Kirsher 		err = niu_pci_vpd_get_propname(np, start + 5, namebuf, 64);
8121e689cf4aSJeff Kirsher 		if (err < 0)
8122e689cf4aSJeff Kirsher 			return err;
8123e689cf4aSJeff Kirsher 
8124e689cf4aSJeff Kirsher 		prop_buf = NULL;
8125e689cf4aSJeff Kirsher 		max_len = 0;
8126e689cf4aSJeff Kirsher 		if (!strcmp(namebuf, "model")) {
8127e689cf4aSJeff Kirsher 			prop_buf = np->vpd.model;
8128e689cf4aSJeff Kirsher 			max_len = NIU_VPD_MODEL_MAX;
8129e689cf4aSJeff Kirsher 			found_mask |= FOUND_MASK_MODEL;
8130e689cf4aSJeff Kirsher 		} else if (!strcmp(namebuf, "board-model")) {
8131e689cf4aSJeff Kirsher 			prop_buf = np->vpd.board_model;
8132e689cf4aSJeff Kirsher 			max_len = NIU_VPD_BD_MODEL_MAX;
8133e689cf4aSJeff Kirsher 			found_mask |= FOUND_MASK_BMODEL;
8134e689cf4aSJeff Kirsher 		} else if (!strcmp(namebuf, "version")) {
8135e689cf4aSJeff Kirsher 			prop_buf = np->vpd.version;
8136e689cf4aSJeff Kirsher 			max_len = NIU_VPD_VERSION_MAX;
8137e689cf4aSJeff Kirsher 			found_mask |= FOUND_MASK_VERS;
8138e689cf4aSJeff Kirsher 		} else if (!strcmp(namebuf, "local-mac-address")) {
8139e689cf4aSJeff Kirsher 			prop_buf = np->vpd.local_mac;
8140e689cf4aSJeff Kirsher 			max_len = ETH_ALEN;
8141e689cf4aSJeff Kirsher 			found_mask |= FOUND_MASK_MAC;
8142e689cf4aSJeff Kirsher 		} else if (!strcmp(namebuf, "num-mac-addresses")) {
8143e689cf4aSJeff Kirsher 			prop_buf = &np->vpd.mac_num;
8144e689cf4aSJeff Kirsher 			max_len = 1;
8145e689cf4aSJeff Kirsher 			found_mask |= FOUND_MASK_NMAC;
8146e689cf4aSJeff Kirsher 		} else if (!strcmp(namebuf, "phy-type")) {
8147e689cf4aSJeff Kirsher 			prop_buf = np->vpd.phy_type;
8148e689cf4aSJeff Kirsher 			max_len = NIU_VPD_PHY_TYPE_MAX;
8149e689cf4aSJeff Kirsher 			found_mask |= FOUND_MASK_PHY;
8150e689cf4aSJeff Kirsher 		}
8151e689cf4aSJeff Kirsher 
8152e689cf4aSJeff Kirsher 		if (max_len && prop_len > max_len) {
8153e689cf4aSJeff Kirsher 			dev_err(np->device, "Property '%s' length (%d) is too long\n", namebuf, prop_len);
8154e689cf4aSJeff Kirsher 			return -EINVAL;
8155e689cf4aSJeff Kirsher 		}
8156e689cf4aSJeff Kirsher 
8157e689cf4aSJeff Kirsher 		if (prop_buf) {
8158e689cf4aSJeff Kirsher 			u32 off = start + 5 + err;
8159e689cf4aSJeff Kirsher 			int i;
8160e689cf4aSJeff Kirsher 
8161e689cf4aSJeff Kirsher 			netif_printk(np, probe, KERN_DEBUG, np->dev,
8162e689cf4aSJeff Kirsher 				     "VPD_SCAN: Reading in property [%s] len[%d]\n",
8163e689cf4aSJeff Kirsher 				     namebuf, prop_len);
8164e6e33770SDu Cheng 			for (i = 0; i < prop_len; i++) {
8165e6e33770SDu Cheng 				err =  niu_pci_eeprom_read(np, off + i);
8166e6e33770SDu Cheng 				if (err < 0)
8167e6e33770SDu Cheng 					return err;
8168e6e33770SDu Cheng 				*prop_buf++ = err;
8169e6e33770SDu Cheng 			}
8170e689cf4aSJeff Kirsher 		}
8171e689cf4aSJeff Kirsher 
8172e689cf4aSJeff Kirsher 		start += len;
8173e689cf4aSJeff Kirsher 	}
8174e689cf4aSJeff Kirsher 
8175e689cf4aSJeff Kirsher 	return 0;
8176e689cf4aSJeff Kirsher }
8177e689cf4aSJeff Kirsher 
8178e689cf4aSJeff Kirsher /* ESPC_PIO_EN_ENABLE must be set */
niu_pci_vpd_fetch(struct niu * np,u32 start)8179e6e33770SDu Cheng static int niu_pci_vpd_fetch(struct niu *np, u32 start)
8180e689cf4aSJeff Kirsher {
8181e689cf4aSJeff Kirsher 	u32 offset;
8182e689cf4aSJeff Kirsher 	int err;
8183e689cf4aSJeff Kirsher 
8184e689cf4aSJeff Kirsher 	err = niu_pci_eeprom_read16_swp(np, start + 1);
8185e689cf4aSJeff Kirsher 	if (err < 0)
8186e6e33770SDu Cheng 		return err;
8187e689cf4aSJeff Kirsher 
8188e689cf4aSJeff Kirsher 	offset = err + 3;
8189e689cf4aSJeff Kirsher 
8190e689cf4aSJeff Kirsher 	while (start + offset < ESPC_EEPROM_SIZE) {
8191e689cf4aSJeff Kirsher 		u32 here = start + offset;
8192e689cf4aSJeff Kirsher 		u32 end;
8193e689cf4aSJeff Kirsher 
8194e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read(np, here);
8195e6e33770SDu Cheng 		if (err < 0)
8196e6e33770SDu Cheng 			return err;
8197e689cf4aSJeff Kirsher 		if (err != 0x90)
8198e6e33770SDu Cheng 			return -EINVAL;
8199e689cf4aSJeff Kirsher 
8200e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read16_swp(np, here + 1);
8201e689cf4aSJeff Kirsher 		if (err < 0)
8202e6e33770SDu Cheng 			return err;
8203e689cf4aSJeff Kirsher 
8204e689cf4aSJeff Kirsher 		here = start + offset + 3;
8205e689cf4aSJeff Kirsher 		end = start + offset + err;
8206e689cf4aSJeff Kirsher 
8207e689cf4aSJeff Kirsher 		offset += err;
8208e689cf4aSJeff Kirsher 
8209e689cf4aSJeff Kirsher 		err = niu_pci_vpd_scan_props(np, here, end);
8210e6e33770SDu Cheng 		if (err < 0)
8211e6e33770SDu Cheng 			return err;
821215bbf8bbSPaul Jakma 		/* ret == 1 is not an error */
8213e6e33770SDu Cheng 		if (err == 1)
821415bbf8bbSPaul Jakma 			return 0;
8215e689cf4aSJeff Kirsher 	}
8216e6e33770SDu Cheng 	return 0;
8217e689cf4aSJeff Kirsher }
8218e689cf4aSJeff Kirsher 
8219e689cf4aSJeff Kirsher /* ESPC_PIO_EN_ENABLE must be set */
niu_pci_vpd_offset(struct niu * np)8220f73d12bdSBill Pemberton static u32 niu_pci_vpd_offset(struct niu *np)
8221e689cf4aSJeff Kirsher {
8222e689cf4aSJeff Kirsher 	u32 start = 0, end = ESPC_EEPROM_SIZE, ret;
8223e689cf4aSJeff Kirsher 	int err;
8224e689cf4aSJeff Kirsher 
8225e689cf4aSJeff Kirsher 	while (start < end) {
8226e689cf4aSJeff Kirsher 		ret = start;
8227e689cf4aSJeff Kirsher 
8228e689cf4aSJeff Kirsher 		/* ROM header signature?  */
8229e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read16(np, start +  0);
8230e689cf4aSJeff Kirsher 		if (err != 0x55aa)
8231e689cf4aSJeff Kirsher 			return 0;
8232e689cf4aSJeff Kirsher 
8233e689cf4aSJeff Kirsher 		/* Apply offset to PCI data structure.  */
8234e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read16(np, start + 23);
8235e689cf4aSJeff Kirsher 		if (err < 0)
8236e689cf4aSJeff Kirsher 			return 0;
8237e689cf4aSJeff Kirsher 		start += err;
8238e689cf4aSJeff Kirsher 
8239e689cf4aSJeff Kirsher 		/* Check for "PCIR" signature.  */
8240e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read16(np, start +  0);
8241e689cf4aSJeff Kirsher 		if (err != 0x5043)
8242e689cf4aSJeff Kirsher 			return 0;
8243e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read16(np, start +  2);
8244e689cf4aSJeff Kirsher 		if (err != 0x4952)
8245e689cf4aSJeff Kirsher 			return 0;
8246e689cf4aSJeff Kirsher 
8247e689cf4aSJeff Kirsher 		/* Check for OBP image type.  */
8248e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read(np, start + 20);
8249e689cf4aSJeff Kirsher 		if (err < 0)
8250e689cf4aSJeff Kirsher 			return 0;
8251e689cf4aSJeff Kirsher 		if (err != 0x01) {
8252e689cf4aSJeff Kirsher 			err = niu_pci_eeprom_read(np, ret + 2);
8253e689cf4aSJeff Kirsher 			if (err < 0)
8254e689cf4aSJeff Kirsher 				return 0;
8255e689cf4aSJeff Kirsher 
8256e689cf4aSJeff Kirsher 			start = ret + (err * 512);
8257e689cf4aSJeff Kirsher 			continue;
8258e689cf4aSJeff Kirsher 		}
8259e689cf4aSJeff Kirsher 
8260e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read16_swp(np, start + 8);
8261e689cf4aSJeff Kirsher 		if (err < 0)
8262e689cf4aSJeff Kirsher 			return err;
8263e689cf4aSJeff Kirsher 		ret += err;
8264e689cf4aSJeff Kirsher 
8265e689cf4aSJeff Kirsher 		err = niu_pci_eeprom_read(np, ret + 0);
8266e689cf4aSJeff Kirsher 		if (err != 0x82)
8267e689cf4aSJeff Kirsher 			return 0;
8268e689cf4aSJeff Kirsher 
8269e689cf4aSJeff Kirsher 		return ret;
8270e689cf4aSJeff Kirsher 	}
8271e689cf4aSJeff Kirsher 
8272e689cf4aSJeff Kirsher 	return 0;
8273e689cf4aSJeff Kirsher }
8274e689cf4aSJeff Kirsher 
niu_phy_type_prop_decode(struct niu * np,const char * phy_prop)82751dd06ae8SGreg Kroah-Hartman static int niu_phy_type_prop_decode(struct niu *np, const char *phy_prop)
8276e689cf4aSJeff Kirsher {
8277e689cf4aSJeff Kirsher 	if (!strcmp(phy_prop, "mif")) {
8278e689cf4aSJeff Kirsher 		/* 1G copper, MII */
8279e689cf4aSJeff Kirsher 		np->flags &= ~(NIU_FLAGS_FIBER |
8280e689cf4aSJeff Kirsher 			       NIU_FLAGS_10G);
8281e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_MII;
8282e689cf4aSJeff Kirsher 	} else if (!strcmp(phy_prop, "xgf")) {
8283e689cf4aSJeff Kirsher 		/* 10G fiber, XPCS */
8284e689cf4aSJeff Kirsher 		np->flags |= (NIU_FLAGS_10G |
8285e689cf4aSJeff Kirsher 			      NIU_FLAGS_FIBER);
8286e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_XPCS;
8287e689cf4aSJeff Kirsher 	} else if (!strcmp(phy_prop, "pcs")) {
8288e689cf4aSJeff Kirsher 		/* 1G fiber, PCS */
8289e689cf4aSJeff Kirsher 		np->flags &= ~NIU_FLAGS_10G;
8290e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_FIBER;
8291e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_PCS;
8292e689cf4aSJeff Kirsher 	} else if (!strcmp(phy_prop, "xgc")) {
8293e689cf4aSJeff Kirsher 		/* 10G copper, XPCS */
8294e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_10G;
8295e689cf4aSJeff Kirsher 		np->flags &= ~NIU_FLAGS_FIBER;
8296e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_XPCS;
8297e689cf4aSJeff Kirsher 	} else if (!strcmp(phy_prop, "xgsd") || !strcmp(phy_prop, "gsd")) {
8298e689cf4aSJeff Kirsher 		/* 10G Serdes or 1G Serdes, default to 10G */
8299e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_10G;
8300e689cf4aSJeff Kirsher 		np->flags &= ~NIU_FLAGS_FIBER;
8301e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_XCVR_SERDES;
8302e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_XPCS;
8303e689cf4aSJeff Kirsher 	} else {
8304e689cf4aSJeff Kirsher 		return -EINVAL;
8305e689cf4aSJeff Kirsher 	}
8306e689cf4aSJeff Kirsher 	return 0;
8307e689cf4aSJeff Kirsher }
8308e689cf4aSJeff Kirsher 
niu_pci_vpd_get_nports(struct niu * np)8309e689cf4aSJeff Kirsher static int niu_pci_vpd_get_nports(struct niu *np)
8310e689cf4aSJeff Kirsher {
8311e689cf4aSJeff Kirsher 	int ports = 0;
8312e689cf4aSJeff Kirsher 
8313e689cf4aSJeff Kirsher 	if ((!strcmp(np->vpd.model, NIU_QGC_LP_MDL_STR)) ||
8314e689cf4aSJeff Kirsher 	    (!strcmp(np->vpd.model, NIU_QGC_PEM_MDL_STR)) ||
8315e689cf4aSJeff Kirsher 	    (!strcmp(np->vpd.model, NIU_MARAMBA_MDL_STR)) ||
8316e689cf4aSJeff Kirsher 	    (!strcmp(np->vpd.model, NIU_KIMI_MDL_STR)) ||
8317e689cf4aSJeff Kirsher 	    (!strcmp(np->vpd.model, NIU_ALONSO_MDL_STR))) {
8318e689cf4aSJeff Kirsher 		ports = 4;
8319e689cf4aSJeff Kirsher 	} else if ((!strcmp(np->vpd.model, NIU_2XGF_LP_MDL_STR)) ||
8320e689cf4aSJeff Kirsher 		   (!strcmp(np->vpd.model, NIU_2XGF_PEM_MDL_STR)) ||
8321e689cf4aSJeff Kirsher 		   (!strcmp(np->vpd.model, NIU_FOXXY_MDL_STR)) ||
8322e689cf4aSJeff Kirsher 		   (!strcmp(np->vpd.model, NIU_2XGF_MRVL_MDL_STR))) {
8323e689cf4aSJeff Kirsher 		ports = 2;
8324e689cf4aSJeff Kirsher 	}
8325e689cf4aSJeff Kirsher 
8326e689cf4aSJeff Kirsher 	return ports;
8327e689cf4aSJeff Kirsher }
8328e689cf4aSJeff Kirsher 
niu_pci_vpd_validate(struct niu * np)8329f73d12bdSBill Pemberton static void niu_pci_vpd_validate(struct niu *np)
8330e689cf4aSJeff Kirsher {
8331e689cf4aSJeff Kirsher 	struct net_device *dev = np->dev;
8332e689cf4aSJeff Kirsher 	struct niu_vpd *vpd = &np->vpd;
8333a7639279SJakub Kicinski 	u8 addr[ETH_ALEN];
8334e689cf4aSJeff Kirsher 	u8 val8;
8335e689cf4aSJeff Kirsher 
8336e689cf4aSJeff Kirsher 	if (!is_valid_ether_addr(&vpd->local_mac[0])) {
8337e689cf4aSJeff Kirsher 		dev_err(np->device, "VPD MAC invalid, falling back to SPROM\n");
8338e689cf4aSJeff Kirsher 
8339e689cf4aSJeff Kirsher 		np->flags &= ~NIU_FLAGS_VPD_VALID;
8340e689cf4aSJeff Kirsher 		return;
8341e689cf4aSJeff Kirsher 	}
8342e689cf4aSJeff Kirsher 
8343e689cf4aSJeff Kirsher 	if (!strcmp(np->vpd.model, NIU_ALONSO_MDL_STR) ||
8344e689cf4aSJeff Kirsher 	    !strcmp(np->vpd.model, NIU_KIMI_MDL_STR)) {
8345e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_10G;
8346e689cf4aSJeff Kirsher 		np->flags &= ~NIU_FLAGS_FIBER;
8347e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_XCVR_SERDES;
8348e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_PCS;
8349e689cf4aSJeff Kirsher 		if (np->port > 1) {
8350e689cf4aSJeff Kirsher 			np->flags |= NIU_FLAGS_FIBER;
8351e689cf4aSJeff Kirsher 			np->flags &= ~NIU_FLAGS_10G;
8352e689cf4aSJeff Kirsher 		}
8353e689cf4aSJeff Kirsher 		if (np->flags & NIU_FLAGS_10G)
8354e689cf4aSJeff Kirsher 			np->mac_xcvr = MAC_XCVR_XPCS;
8355e689cf4aSJeff Kirsher 	} else if (!strcmp(np->vpd.model, NIU_FOXXY_MDL_STR)) {
8356e689cf4aSJeff Kirsher 		np->flags |= (NIU_FLAGS_10G | NIU_FLAGS_FIBER |
8357e689cf4aSJeff Kirsher 			      NIU_FLAGS_HOTPLUG_PHY);
8358e689cf4aSJeff Kirsher 	} else if (niu_phy_type_prop_decode(np, np->vpd.phy_type)) {
8359e689cf4aSJeff Kirsher 		dev_err(np->device, "Illegal phy string [%s]\n",
8360e689cf4aSJeff Kirsher 			np->vpd.phy_type);
8361e689cf4aSJeff Kirsher 		dev_err(np->device, "Falling back to SPROM\n");
8362e689cf4aSJeff Kirsher 		np->flags &= ~NIU_FLAGS_VPD_VALID;
8363e689cf4aSJeff Kirsher 		return;
8364e689cf4aSJeff Kirsher 	}
8365e689cf4aSJeff Kirsher 
8366a7639279SJakub Kicinski 	ether_addr_copy(addr, vpd->local_mac);
8367e689cf4aSJeff Kirsher 
8368a7639279SJakub Kicinski 	val8 = addr[5];
8369a7639279SJakub Kicinski 	addr[5] += np->port;
8370a7639279SJakub Kicinski 	if (addr[5] < val8)
8371a7639279SJakub Kicinski 		addr[4]++;
8372a7639279SJakub Kicinski 
8373a7639279SJakub Kicinski 	eth_hw_addr_set(dev, addr);
8374e689cf4aSJeff Kirsher }
8375e689cf4aSJeff Kirsher 
niu_pci_probe_sprom(struct niu * np)8376f73d12bdSBill Pemberton static int niu_pci_probe_sprom(struct niu *np)
8377e689cf4aSJeff Kirsher {
8378e689cf4aSJeff Kirsher 	struct net_device *dev = np->dev;
8379a7639279SJakub Kicinski 	u8 addr[ETH_ALEN];
8380e689cf4aSJeff Kirsher 	int len, i;
8381e689cf4aSJeff Kirsher 	u64 val, sum;
8382e689cf4aSJeff Kirsher 	u8 val8;
8383e689cf4aSJeff Kirsher 
8384e689cf4aSJeff Kirsher 	val = (nr64(ESPC_VER_IMGSZ) & ESPC_VER_IMGSZ_IMGSZ);
8385e689cf4aSJeff Kirsher 	val >>= ESPC_VER_IMGSZ_IMGSZ_SHIFT;
8386e689cf4aSJeff Kirsher 	len = val / 4;
8387e689cf4aSJeff Kirsher 
8388e689cf4aSJeff Kirsher 	np->eeprom_len = len;
8389e689cf4aSJeff Kirsher 
8390e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8391e689cf4aSJeff Kirsher 		     "SPROM: Image size %llu\n", (unsigned long long)val);
8392e689cf4aSJeff Kirsher 
8393e689cf4aSJeff Kirsher 	sum = 0;
8394e689cf4aSJeff Kirsher 	for (i = 0; i < len; i++) {
8395e689cf4aSJeff Kirsher 		val = nr64(ESPC_NCR(i));
8396e689cf4aSJeff Kirsher 		sum += (val >>  0) & 0xff;
8397e689cf4aSJeff Kirsher 		sum += (val >>  8) & 0xff;
8398e689cf4aSJeff Kirsher 		sum += (val >> 16) & 0xff;
8399e689cf4aSJeff Kirsher 		sum += (val >> 24) & 0xff;
8400e689cf4aSJeff Kirsher 	}
8401e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8402e689cf4aSJeff Kirsher 		     "SPROM: Checksum %x\n", (int)(sum & 0xff));
8403e689cf4aSJeff Kirsher 	if ((sum & 0xff) != 0xab) {
8404e689cf4aSJeff Kirsher 		dev_err(np->device, "Bad SPROM checksum (%x, should be 0xab)\n", (int)(sum & 0xff));
8405e689cf4aSJeff Kirsher 		return -EINVAL;
8406e689cf4aSJeff Kirsher 	}
8407e689cf4aSJeff Kirsher 
8408e689cf4aSJeff Kirsher 	val = nr64(ESPC_PHY_TYPE);
8409e689cf4aSJeff Kirsher 	switch (np->port) {
8410e689cf4aSJeff Kirsher 	case 0:
8411e689cf4aSJeff Kirsher 		val8 = (val & ESPC_PHY_TYPE_PORT0) >>
8412e689cf4aSJeff Kirsher 			ESPC_PHY_TYPE_PORT0_SHIFT;
8413e689cf4aSJeff Kirsher 		break;
8414e689cf4aSJeff Kirsher 	case 1:
8415e689cf4aSJeff Kirsher 		val8 = (val & ESPC_PHY_TYPE_PORT1) >>
8416e689cf4aSJeff Kirsher 			ESPC_PHY_TYPE_PORT1_SHIFT;
8417e689cf4aSJeff Kirsher 		break;
8418e689cf4aSJeff Kirsher 	case 2:
8419e689cf4aSJeff Kirsher 		val8 = (val & ESPC_PHY_TYPE_PORT2) >>
8420e689cf4aSJeff Kirsher 			ESPC_PHY_TYPE_PORT2_SHIFT;
8421e689cf4aSJeff Kirsher 		break;
8422e689cf4aSJeff Kirsher 	case 3:
8423e689cf4aSJeff Kirsher 		val8 = (val & ESPC_PHY_TYPE_PORT3) >>
8424e689cf4aSJeff Kirsher 			ESPC_PHY_TYPE_PORT3_SHIFT;
8425e689cf4aSJeff Kirsher 		break;
8426e689cf4aSJeff Kirsher 	default:
8427e689cf4aSJeff Kirsher 		dev_err(np->device, "Bogus port number %u\n",
8428e689cf4aSJeff Kirsher 			np->port);
8429e689cf4aSJeff Kirsher 		return -EINVAL;
8430e689cf4aSJeff Kirsher 	}
8431e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8432e689cf4aSJeff Kirsher 		     "SPROM: PHY type %x\n", val8);
8433e689cf4aSJeff Kirsher 
8434e689cf4aSJeff Kirsher 	switch (val8) {
8435e689cf4aSJeff Kirsher 	case ESPC_PHY_TYPE_1G_COPPER:
8436e689cf4aSJeff Kirsher 		/* 1G copper, MII */
8437e689cf4aSJeff Kirsher 		np->flags &= ~(NIU_FLAGS_FIBER |
8438e689cf4aSJeff Kirsher 			       NIU_FLAGS_10G);
8439e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_MII;
8440e689cf4aSJeff Kirsher 		break;
8441e689cf4aSJeff Kirsher 
8442e689cf4aSJeff Kirsher 	case ESPC_PHY_TYPE_1G_FIBER:
8443e689cf4aSJeff Kirsher 		/* 1G fiber, PCS */
8444e689cf4aSJeff Kirsher 		np->flags &= ~NIU_FLAGS_10G;
8445e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_FIBER;
8446e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_PCS;
8447e689cf4aSJeff Kirsher 		break;
8448e689cf4aSJeff Kirsher 
8449e689cf4aSJeff Kirsher 	case ESPC_PHY_TYPE_10G_COPPER:
8450e689cf4aSJeff Kirsher 		/* 10G copper, XPCS */
8451e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_10G;
8452e689cf4aSJeff Kirsher 		np->flags &= ~NIU_FLAGS_FIBER;
8453e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_XPCS;
8454e689cf4aSJeff Kirsher 		break;
8455e689cf4aSJeff Kirsher 
8456e689cf4aSJeff Kirsher 	case ESPC_PHY_TYPE_10G_FIBER:
8457e689cf4aSJeff Kirsher 		/* 10G fiber, XPCS */
8458e689cf4aSJeff Kirsher 		np->flags |= (NIU_FLAGS_10G |
8459e689cf4aSJeff Kirsher 			      NIU_FLAGS_FIBER);
8460e689cf4aSJeff Kirsher 		np->mac_xcvr = MAC_XCVR_XPCS;
8461e689cf4aSJeff Kirsher 		break;
8462e689cf4aSJeff Kirsher 
8463e689cf4aSJeff Kirsher 	default:
8464e689cf4aSJeff Kirsher 		dev_err(np->device, "Bogus SPROM phy type %u\n", val8);
8465e689cf4aSJeff Kirsher 		return -EINVAL;
8466e689cf4aSJeff Kirsher 	}
8467e689cf4aSJeff Kirsher 
8468e689cf4aSJeff Kirsher 	val = nr64(ESPC_MAC_ADDR0);
8469e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8470e689cf4aSJeff Kirsher 		     "SPROM: MAC_ADDR0[%08llx]\n", (unsigned long long)val);
8471a7639279SJakub Kicinski 	addr[0] = (val >>  0) & 0xff;
8472a7639279SJakub Kicinski 	addr[1] = (val >>  8) & 0xff;
8473a7639279SJakub Kicinski 	addr[2] = (val >> 16) & 0xff;
8474a7639279SJakub Kicinski 	addr[3] = (val >> 24) & 0xff;
8475e689cf4aSJeff Kirsher 
8476e689cf4aSJeff Kirsher 	val = nr64(ESPC_MAC_ADDR1);
8477e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8478e689cf4aSJeff Kirsher 		     "SPROM: MAC_ADDR1[%08llx]\n", (unsigned long long)val);
8479a7639279SJakub Kicinski 	addr[4] = (val >>  0) & 0xff;
8480a7639279SJakub Kicinski 	addr[5] = (val >>  8) & 0xff;
8481e689cf4aSJeff Kirsher 
8482a7639279SJakub Kicinski 	if (!is_valid_ether_addr(addr)) {
8483e689cf4aSJeff Kirsher 		dev_err(np->device, "SPROM MAC address invalid [ %pM ]\n",
8484a7639279SJakub Kicinski 			addr);
8485e689cf4aSJeff Kirsher 		return -EINVAL;
8486e689cf4aSJeff Kirsher 	}
8487e689cf4aSJeff Kirsher 
8488a7639279SJakub Kicinski 	val8 = addr[5];
8489a7639279SJakub Kicinski 	addr[5] += np->port;
8490a7639279SJakub Kicinski 	if (addr[5] < val8)
8491a7639279SJakub Kicinski 		addr[4]++;
8492a7639279SJakub Kicinski 
8493a7639279SJakub Kicinski 	eth_hw_addr_set(dev, addr);
8494e689cf4aSJeff Kirsher 
8495e689cf4aSJeff Kirsher 	val = nr64(ESPC_MOD_STR_LEN);
8496e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8497e689cf4aSJeff Kirsher 		     "SPROM: MOD_STR_LEN[%llu]\n", (unsigned long long)val);
8498e689cf4aSJeff Kirsher 	if (val >= 8 * 4)
8499e689cf4aSJeff Kirsher 		return -EINVAL;
8500e689cf4aSJeff Kirsher 
8501e689cf4aSJeff Kirsher 	for (i = 0; i < val; i += 4) {
8502e689cf4aSJeff Kirsher 		u64 tmp = nr64(ESPC_NCR(5 + (i / 4)));
8503e689cf4aSJeff Kirsher 
8504e689cf4aSJeff Kirsher 		np->vpd.model[i + 3] = (tmp >>  0) & 0xff;
8505e689cf4aSJeff Kirsher 		np->vpd.model[i + 2] = (tmp >>  8) & 0xff;
8506e689cf4aSJeff Kirsher 		np->vpd.model[i + 1] = (tmp >> 16) & 0xff;
8507e689cf4aSJeff Kirsher 		np->vpd.model[i + 0] = (tmp >> 24) & 0xff;
8508e689cf4aSJeff Kirsher 	}
8509e689cf4aSJeff Kirsher 	np->vpd.model[val] = '\0';
8510e689cf4aSJeff Kirsher 
8511e689cf4aSJeff Kirsher 	val = nr64(ESPC_BD_MOD_STR_LEN);
8512e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8513e689cf4aSJeff Kirsher 		     "SPROM: BD_MOD_STR_LEN[%llu]\n", (unsigned long long)val);
8514e689cf4aSJeff Kirsher 	if (val >= 4 * 4)
8515e689cf4aSJeff Kirsher 		return -EINVAL;
8516e689cf4aSJeff Kirsher 
8517e689cf4aSJeff Kirsher 	for (i = 0; i < val; i += 4) {
8518e689cf4aSJeff Kirsher 		u64 tmp = nr64(ESPC_NCR(14 + (i / 4)));
8519e689cf4aSJeff Kirsher 
8520e689cf4aSJeff Kirsher 		np->vpd.board_model[i + 3] = (tmp >>  0) & 0xff;
8521e689cf4aSJeff Kirsher 		np->vpd.board_model[i + 2] = (tmp >>  8) & 0xff;
8522e689cf4aSJeff Kirsher 		np->vpd.board_model[i + 1] = (tmp >> 16) & 0xff;
8523e689cf4aSJeff Kirsher 		np->vpd.board_model[i + 0] = (tmp >> 24) & 0xff;
8524e689cf4aSJeff Kirsher 	}
8525e689cf4aSJeff Kirsher 	np->vpd.board_model[val] = '\0';
8526e689cf4aSJeff Kirsher 
8527e689cf4aSJeff Kirsher 	np->vpd.mac_num =
8528e689cf4aSJeff Kirsher 		nr64(ESPC_NUM_PORTS_MACS) & ESPC_NUM_PORTS_MACS_VAL;
8529e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
8530e689cf4aSJeff Kirsher 		     "SPROM: NUM_PORTS_MACS[%d]\n", np->vpd.mac_num);
8531e689cf4aSJeff Kirsher 
8532e689cf4aSJeff Kirsher 	return 0;
8533e689cf4aSJeff Kirsher }
8534e689cf4aSJeff Kirsher 
niu_get_and_validate_port(struct niu * np)8535f73d12bdSBill Pemberton static int niu_get_and_validate_port(struct niu *np)
8536e689cf4aSJeff Kirsher {
8537e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
8538e689cf4aSJeff Kirsher 
8539e689cf4aSJeff Kirsher 	if (np->port <= 1)
8540e689cf4aSJeff Kirsher 		np->flags |= NIU_FLAGS_XMAC;
8541e689cf4aSJeff Kirsher 
8542e689cf4aSJeff Kirsher 	if (!parent->num_ports) {
8543e689cf4aSJeff Kirsher 		if (parent->plat_type == PLAT_TYPE_NIU) {
8544e689cf4aSJeff Kirsher 			parent->num_ports = 2;
8545e689cf4aSJeff Kirsher 		} else {
8546e689cf4aSJeff Kirsher 			parent->num_ports = niu_pci_vpd_get_nports(np);
8547e689cf4aSJeff Kirsher 			if (!parent->num_ports) {
8548e689cf4aSJeff Kirsher 				/* Fall back to SPROM as last resort.
8549e689cf4aSJeff Kirsher 				 * This will fail on most cards.
8550e689cf4aSJeff Kirsher 				 */
8551e689cf4aSJeff Kirsher 				parent->num_ports = nr64(ESPC_NUM_PORTS_MACS) &
8552e689cf4aSJeff Kirsher 					ESPC_NUM_PORTS_MACS_VAL;
8553e689cf4aSJeff Kirsher 
8554e689cf4aSJeff Kirsher 				/* All of the current probing methods fail on
8555e689cf4aSJeff Kirsher 				 * Maramba on-board parts.
8556e689cf4aSJeff Kirsher 				 */
8557e689cf4aSJeff Kirsher 				if (!parent->num_ports)
8558e689cf4aSJeff Kirsher 					parent->num_ports = 4;
8559e689cf4aSJeff Kirsher 			}
8560e689cf4aSJeff Kirsher 		}
8561e689cf4aSJeff Kirsher 	}
8562e689cf4aSJeff Kirsher 
8563e689cf4aSJeff Kirsher 	if (np->port >= parent->num_ports)
8564e689cf4aSJeff Kirsher 		return -ENODEV;
8565e689cf4aSJeff Kirsher 
8566e689cf4aSJeff Kirsher 	return 0;
8567e689cf4aSJeff Kirsher }
8568e689cf4aSJeff Kirsher 
phy_record(struct niu_parent * parent,struct phy_probe_info * p,int dev_id_1,int dev_id_2,u8 phy_port,int type)85691dd06ae8SGreg Kroah-Hartman static int phy_record(struct niu_parent *parent, struct phy_probe_info *p,
85701dd06ae8SGreg Kroah-Hartman 		      int dev_id_1, int dev_id_2, u8 phy_port, int type)
8571e689cf4aSJeff Kirsher {
8572e689cf4aSJeff Kirsher 	u32 id = (dev_id_1 << 16) | dev_id_2;
8573e689cf4aSJeff Kirsher 	u8 idx;
8574e689cf4aSJeff Kirsher 
8575e689cf4aSJeff Kirsher 	if (dev_id_1 < 0 || dev_id_2 < 0)
8576e689cf4aSJeff Kirsher 		return 0;
8577e689cf4aSJeff Kirsher 	if (type == PHY_TYPE_PMA_PMD || type == PHY_TYPE_PCS) {
85781d214fa3SDavid S. Miller 		/* Because of the NIU_PHY_ID_MASK being applied, the 8704
857963ec3c83SDavid S. Miller 		 * test covers the 8706 as well.
858063ec3c83SDavid S. Miller 		 */
8581e689cf4aSJeff Kirsher 		if (((id & NIU_PHY_ID_MASK) != NIU_PHY_ID_BCM8704) &&
858263ec3c83SDavid S. Miller 		    ((id & NIU_PHY_ID_MASK) != NIU_PHY_ID_MRVL88X2011))
8583e689cf4aSJeff Kirsher 			return 0;
8584e689cf4aSJeff Kirsher 	} else {
8585e689cf4aSJeff Kirsher 		if ((id & NIU_PHY_ID_MASK) != NIU_PHY_ID_BCM5464R)
8586e689cf4aSJeff Kirsher 			return 0;
8587e689cf4aSJeff Kirsher 	}
8588e689cf4aSJeff Kirsher 
8589e689cf4aSJeff Kirsher 	pr_info("niu%d: Found PHY %08x type %s at phy_port %u\n",
8590e689cf4aSJeff Kirsher 		parent->index, id,
8591e689cf4aSJeff Kirsher 		type == PHY_TYPE_PMA_PMD ? "PMA/PMD" :
8592e689cf4aSJeff Kirsher 		type == PHY_TYPE_PCS ? "PCS" : "MII",
8593e689cf4aSJeff Kirsher 		phy_port);
8594e689cf4aSJeff Kirsher 
8595e689cf4aSJeff Kirsher 	if (p->cur[type] >= NIU_MAX_PORTS) {
8596e689cf4aSJeff Kirsher 		pr_err("Too many PHY ports\n");
8597e689cf4aSJeff Kirsher 		return -EINVAL;
8598e689cf4aSJeff Kirsher 	}
8599e689cf4aSJeff Kirsher 	idx = p->cur[type];
8600e689cf4aSJeff Kirsher 	p->phy_id[type][idx] = id;
8601e689cf4aSJeff Kirsher 	p->phy_port[type][idx] = phy_port;
8602e689cf4aSJeff Kirsher 	p->cur[type] = idx + 1;
8603e689cf4aSJeff Kirsher 	return 0;
8604e689cf4aSJeff Kirsher }
8605e689cf4aSJeff Kirsher 
port_has_10g(struct phy_probe_info * p,int port)8606f73d12bdSBill Pemberton static int port_has_10g(struct phy_probe_info *p, int port)
8607e689cf4aSJeff Kirsher {
8608e689cf4aSJeff Kirsher 	int i;
8609e689cf4aSJeff Kirsher 
8610e689cf4aSJeff Kirsher 	for (i = 0; i < p->cur[PHY_TYPE_PMA_PMD]; i++) {
8611e689cf4aSJeff Kirsher 		if (p->phy_port[PHY_TYPE_PMA_PMD][i] == port)
8612e689cf4aSJeff Kirsher 			return 1;
8613e689cf4aSJeff Kirsher 	}
8614e689cf4aSJeff Kirsher 	for (i = 0; i < p->cur[PHY_TYPE_PCS]; i++) {
8615e689cf4aSJeff Kirsher 		if (p->phy_port[PHY_TYPE_PCS][i] == port)
8616e689cf4aSJeff Kirsher 			return 1;
8617e689cf4aSJeff Kirsher 	}
8618e689cf4aSJeff Kirsher 
8619e689cf4aSJeff Kirsher 	return 0;
8620e689cf4aSJeff Kirsher }
8621e689cf4aSJeff Kirsher 
count_10g_ports(struct phy_probe_info * p,int * lowest)8622f73d12bdSBill Pemberton static int count_10g_ports(struct phy_probe_info *p, int *lowest)
8623e689cf4aSJeff Kirsher {
8624e689cf4aSJeff Kirsher 	int port, cnt;
8625e689cf4aSJeff Kirsher 
8626e689cf4aSJeff Kirsher 	cnt = 0;
8627e689cf4aSJeff Kirsher 	*lowest = 32;
8628e689cf4aSJeff Kirsher 	for (port = 8; port < 32; port++) {
8629e689cf4aSJeff Kirsher 		if (port_has_10g(p, port)) {
8630e689cf4aSJeff Kirsher 			if (!cnt)
8631e689cf4aSJeff Kirsher 				*lowest = port;
8632e689cf4aSJeff Kirsher 			cnt++;
8633e689cf4aSJeff Kirsher 		}
8634e689cf4aSJeff Kirsher 	}
8635e689cf4aSJeff Kirsher 
8636e689cf4aSJeff Kirsher 	return cnt;
8637e689cf4aSJeff Kirsher }
8638e689cf4aSJeff Kirsher 
count_1g_ports(struct phy_probe_info * p,int * lowest)8639f73d12bdSBill Pemberton static int count_1g_ports(struct phy_probe_info *p, int *lowest)
8640e689cf4aSJeff Kirsher {
8641e689cf4aSJeff Kirsher 	*lowest = 32;
8642e689cf4aSJeff Kirsher 	if (p->cur[PHY_TYPE_MII])
8643e689cf4aSJeff Kirsher 		*lowest = p->phy_port[PHY_TYPE_MII][0];
8644e689cf4aSJeff Kirsher 
8645e689cf4aSJeff Kirsher 	return p->cur[PHY_TYPE_MII];
8646e689cf4aSJeff Kirsher }
8647e689cf4aSJeff Kirsher 
niu_n2_divide_channels(struct niu_parent * parent)8648f73d12bdSBill Pemberton static void niu_n2_divide_channels(struct niu_parent *parent)
8649e689cf4aSJeff Kirsher {
8650e689cf4aSJeff Kirsher 	int num_ports = parent->num_ports;
8651e689cf4aSJeff Kirsher 	int i;
8652e689cf4aSJeff Kirsher 
8653e689cf4aSJeff Kirsher 	for (i = 0; i < num_ports; i++) {
8654e689cf4aSJeff Kirsher 		parent->rxchan_per_port[i] = (16 / num_ports);
8655e689cf4aSJeff Kirsher 		parent->txchan_per_port[i] = (16 / num_ports);
8656e689cf4aSJeff Kirsher 
8657e689cf4aSJeff Kirsher 		pr_info("niu%d: Port %u [%u RX chans] [%u TX chans]\n",
8658e689cf4aSJeff Kirsher 			parent->index, i,
8659e689cf4aSJeff Kirsher 			parent->rxchan_per_port[i],
8660e689cf4aSJeff Kirsher 			parent->txchan_per_port[i]);
8661e689cf4aSJeff Kirsher 	}
8662e689cf4aSJeff Kirsher }
8663e689cf4aSJeff Kirsher 
niu_divide_channels(struct niu_parent * parent,int num_10g,int num_1g)8664f73d12bdSBill Pemberton static void niu_divide_channels(struct niu_parent *parent,
8665e689cf4aSJeff Kirsher 				int num_10g, int num_1g)
8666e689cf4aSJeff Kirsher {
8667e689cf4aSJeff Kirsher 	int num_ports = parent->num_ports;
8668e689cf4aSJeff Kirsher 	int rx_chans_per_10g, rx_chans_per_1g;
8669e689cf4aSJeff Kirsher 	int tx_chans_per_10g, tx_chans_per_1g;
8670e689cf4aSJeff Kirsher 	int i, tot_rx, tot_tx;
8671e689cf4aSJeff Kirsher 
8672e689cf4aSJeff Kirsher 	if (!num_10g || !num_1g) {
8673e689cf4aSJeff Kirsher 		rx_chans_per_10g = rx_chans_per_1g =
8674e689cf4aSJeff Kirsher 			(NIU_NUM_RXCHAN / num_ports);
8675e689cf4aSJeff Kirsher 		tx_chans_per_10g = tx_chans_per_1g =
8676e689cf4aSJeff Kirsher 			(NIU_NUM_TXCHAN / num_ports);
8677e689cf4aSJeff Kirsher 	} else {
8678e689cf4aSJeff Kirsher 		rx_chans_per_1g = NIU_NUM_RXCHAN / 8;
8679e689cf4aSJeff Kirsher 		rx_chans_per_10g = (NIU_NUM_RXCHAN -
8680e689cf4aSJeff Kirsher 				    (rx_chans_per_1g * num_1g)) /
8681e689cf4aSJeff Kirsher 			num_10g;
8682e689cf4aSJeff Kirsher 
8683e689cf4aSJeff Kirsher 		tx_chans_per_1g = NIU_NUM_TXCHAN / 6;
8684e689cf4aSJeff Kirsher 		tx_chans_per_10g = (NIU_NUM_TXCHAN -
8685e689cf4aSJeff Kirsher 				    (tx_chans_per_1g * num_1g)) /
8686e689cf4aSJeff Kirsher 			num_10g;
8687e689cf4aSJeff Kirsher 	}
8688e689cf4aSJeff Kirsher 
8689e689cf4aSJeff Kirsher 	tot_rx = tot_tx = 0;
8690e689cf4aSJeff Kirsher 	for (i = 0; i < num_ports; i++) {
8691e689cf4aSJeff Kirsher 		int type = phy_decode(parent->port_phy, i);
8692e689cf4aSJeff Kirsher 
8693e689cf4aSJeff Kirsher 		if (type == PORT_TYPE_10G) {
8694e689cf4aSJeff Kirsher 			parent->rxchan_per_port[i] = rx_chans_per_10g;
8695e689cf4aSJeff Kirsher 			parent->txchan_per_port[i] = tx_chans_per_10g;
8696e689cf4aSJeff Kirsher 		} else {
8697e689cf4aSJeff Kirsher 			parent->rxchan_per_port[i] = rx_chans_per_1g;
8698e689cf4aSJeff Kirsher 			parent->txchan_per_port[i] = tx_chans_per_1g;
8699e689cf4aSJeff Kirsher 		}
8700e689cf4aSJeff Kirsher 		pr_info("niu%d: Port %u [%u RX chans] [%u TX chans]\n",
8701e689cf4aSJeff Kirsher 			parent->index, i,
8702e689cf4aSJeff Kirsher 			parent->rxchan_per_port[i],
8703e689cf4aSJeff Kirsher 			parent->txchan_per_port[i]);
8704e689cf4aSJeff Kirsher 		tot_rx += parent->rxchan_per_port[i];
8705e689cf4aSJeff Kirsher 		tot_tx += parent->txchan_per_port[i];
8706e689cf4aSJeff Kirsher 	}
8707e689cf4aSJeff Kirsher 
8708e689cf4aSJeff Kirsher 	if (tot_rx > NIU_NUM_RXCHAN) {
8709e689cf4aSJeff Kirsher 		pr_err("niu%d: Too many RX channels (%d), resetting to one per port\n",
8710e689cf4aSJeff Kirsher 		       parent->index, tot_rx);
8711e689cf4aSJeff Kirsher 		for (i = 0; i < num_ports; i++)
8712e689cf4aSJeff Kirsher 			parent->rxchan_per_port[i] = 1;
8713e689cf4aSJeff Kirsher 	}
8714e689cf4aSJeff Kirsher 	if (tot_tx > NIU_NUM_TXCHAN) {
8715e689cf4aSJeff Kirsher 		pr_err("niu%d: Too many TX channels (%d), resetting to one per port\n",
8716e689cf4aSJeff Kirsher 		       parent->index, tot_tx);
8717e689cf4aSJeff Kirsher 		for (i = 0; i < num_ports; i++)
8718e689cf4aSJeff Kirsher 			parent->txchan_per_port[i] = 1;
8719e689cf4aSJeff Kirsher 	}
8720e689cf4aSJeff Kirsher 	if (tot_rx < NIU_NUM_RXCHAN || tot_tx < NIU_NUM_TXCHAN) {
8721fe3881cfSJoe Perches 		pr_warn("niu%d: Driver bug, wasted channels, RX[%d] TX[%d]\n",
8722e689cf4aSJeff Kirsher 			parent->index, tot_rx, tot_tx);
8723e689cf4aSJeff Kirsher 	}
8724e689cf4aSJeff Kirsher }
8725e689cf4aSJeff Kirsher 
niu_divide_rdc_groups(struct niu_parent * parent,int num_10g,int num_1g)8726f73d12bdSBill Pemberton static void niu_divide_rdc_groups(struct niu_parent *parent,
8727e689cf4aSJeff Kirsher 				  int num_10g, int num_1g)
8728e689cf4aSJeff Kirsher {
8729e689cf4aSJeff Kirsher 	int i, num_ports = parent->num_ports;
8730e689cf4aSJeff Kirsher 	int rdc_group, rdc_groups_per_port;
8731e689cf4aSJeff Kirsher 	int rdc_channel_base;
8732e689cf4aSJeff Kirsher 
8733e689cf4aSJeff Kirsher 	rdc_group = 0;
8734e689cf4aSJeff Kirsher 	rdc_groups_per_port = NIU_NUM_RDC_TABLES / num_ports;
8735e689cf4aSJeff Kirsher 
8736e689cf4aSJeff Kirsher 	rdc_channel_base = 0;
8737e689cf4aSJeff Kirsher 
8738e689cf4aSJeff Kirsher 	for (i = 0; i < num_ports; i++) {
8739e689cf4aSJeff Kirsher 		struct niu_rdc_tables *tp = &parent->rdc_group_cfg[i];
8740e689cf4aSJeff Kirsher 		int grp, num_channels = parent->rxchan_per_port[i];
8741e689cf4aSJeff Kirsher 		int this_channel_offset;
8742e689cf4aSJeff Kirsher 
8743e689cf4aSJeff Kirsher 		tp->first_table_num = rdc_group;
8744e689cf4aSJeff Kirsher 		tp->num_tables = rdc_groups_per_port;
8745e689cf4aSJeff Kirsher 		this_channel_offset = 0;
8746e689cf4aSJeff Kirsher 		for (grp = 0; grp < tp->num_tables; grp++) {
8747e689cf4aSJeff Kirsher 			struct rdc_table *rt = &tp->tables[grp];
8748e689cf4aSJeff Kirsher 			int slot;
8749e689cf4aSJeff Kirsher 
8750e689cf4aSJeff Kirsher 			pr_info("niu%d: Port %d RDC tbl(%d) [ ",
8751e689cf4aSJeff Kirsher 				parent->index, i, tp->first_table_num + grp);
8752e689cf4aSJeff Kirsher 			for (slot = 0; slot < NIU_RDC_TABLE_SLOTS; slot++) {
8753e689cf4aSJeff Kirsher 				rt->rxdma_channel[slot] =
8754e689cf4aSJeff Kirsher 					rdc_channel_base + this_channel_offset;
8755e689cf4aSJeff Kirsher 
8756e689cf4aSJeff Kirsher 				pr_cont("%d ", rt->rxdma_channel[slot]);
8757e689cf4aSJeff Kirsher 
8758e689cf4aSJeff Kirsher 				if (++this_channel_offset == num_channels)
8759e689cf4aSJeff Kirsher 					this_channel_offset = 0;
8760e689cf4aSJeff Kirsher 			}
8761e689cf4aSJeff Kirsher 			pr_cont("]\n");
8762e689cf4aSJeff Kirsher 		}
8763e689cf4aSJeff Kirsher 
8764e689cf4aSJeff Kirsher 		parent->rdc_default[i] = rdc_channel_base;
8765e689cf4aSJeff Kirsher 
8766e689cf4aSJeff Kirsher 		rdc_channel_base += num_channels;
8767e689cf4aSJeff Kirsher 		rdc_group += rdc_groups_per_port;
8768e689cf4aSJeff Kirsher 	}
8769e689cf4aSJeff Kirsher }
8770e689cf4aSJeff Kirsher 
fill_phy_probe_info(struct niu * np,struct niu_parent * parent,struct phy_probe_info * info)87711dd06ae8SGreg Kroah-Hartman static int fill_phy_probe_info(struct niu *np, struct niu_parent *parent,
8772e689cf4aSJeff Kirsher 			       struct phy_probe_info *info)
8773e689cf4aSJeff Kirsher {
8774e689cf4aSJeff Kirsher 	unsigned long flags;
8775e689cf4aSJeff Kirsher 	int port, err;
8776e689cf4aSJeff Kirsher 
8777e689cf4aSJeff Kirsher 	memset(info, 0, sizeof(*info));
8778e689cf4aSJeff Kirsher 
8779e689cf4aSJeff Kirsher 	/* Port 0 to 7 are reserved for onboard Serdes, probe the rest.  */
8780e689cf4aSJeff Kirsher 	niu_lock_parent(np, flags);
8781e689cf4aSJeff Kirsher 	err = 0;
8782e689cf4aSJeff Kirsher 	for (port = 8; port < 32; port++) {
8783e689cf4aSJeff Kirsher 		int dev_id_1, dev_id_2;
8784e689cf4aSJeff Kirsher 
8785e689cf4aSJeff Kirsher 		dev_id_1 = mdio_read(np, port,
8786e689cf4aSJeff Kirsher 				     NIU_PMA_PMD_DEV_ADDR, MII_PHYSID1);
8787e689cf4aSJeff Kirsher 		dev_id_2 = mdio_read(np, port,
8788e689cf4aSJeff Kirsher 				     NIU_PMA_PMD_DEV_ADDR, MII_PHYSID2);
8789e689cf4aSJeff Kirsher 		err = phy_record(parent, info, dev_id_1, dev_id_2, port,
8790e689cf4aSJeff Kirsher 				 PHY_TYPE_PMA_PMD);
8791e689cf4aSJeff Kirsher 		if (err)
8792e689cf4aSJeff Kirsher 			break;
8793e689cf4aSJeff Kirsher 		dev_id_1 = mdio_read(np, port,
8794e689cf4aSJeff Kirsher 				     NIU_PCS_DEV_ADDR, MII_PHYSID1);
8795e689cf4aSJeff Kirsher 		dev_id_2 = mdio_read(np, port,
8796e689cf4aSJeff Kirsher 				     NIU_PCS_DEV_ADDR, MII_PHYSID2);
8797e689cf4aSJeff Kirsher 		err = phy_record(parent, info, dev_id_1, dev_id_2, port,
8798e689cf4aSJeff Kirsher 				 PHY_TYPE_PCS);
8799e689cf4aSJeff Kirsher 		if (err)
8800e689cf4aSJeff Kirsher 			break;
8801e689cf4aSJeff Kirsher 		dev_id_1 = mii_read(np, port, MII_PHYSID1);
8802e689cf4aSJeff Kirsher 		dev_id_2 = mii_read(np, port, MII_PHYSID2);
8803e689cf4aSJeff Kirsher 		err = phy_record(parent, info, dev_id_1, dev_id_2, port,
8804e689cf4aSJeff Kirsher 				 PHY_TYPE_MII);
8805e689cf4aSJeff Kirsher 		if (err)
8806e689cf4aSJeff Kirsher 			break;
8807e689cf4aSJeff Kirsher 	}
8808e689cf4aSJeff Kirsher 	niu_unlock_parent(np, flags);
8809e689cf4aSJeff Kirsher 
8810e689cf4aSJeff Kirsher 	return err;
8811e689cf4aSJeff Kirsher }
8812e689cf4aSJeff Kirsher 
walk_phys(struct niu * np,struct niu_parent * parent)8813f73d12bdSBill Pemberton static int walk_phys(struct niu *np, struct niu_parent *parent)
8814e689cf4aSJeff Kirsher {
8815e689cf4aSJeff Kirsher 	struct phy_probe_info *info = &parent->phy_probe_info;
8816e689cf4aSJeff Kirsher 	int lowest_10g, lowest_1g;
8817e689cf4aSJeff Kirsher 	int num_10g, num_1g;
8818e689cf4aSJeff Kirsher 	u32 val;
8819e689cf4aSJeff Kirsher 	int err;
8820e689cf4aSJeff Kirsher 
8821e689cf4aSJeff Kirsher 	num_10g = num_1g = 0;
8822e689cf4aSJeff Kirsher 
8823e689cf4aSJeff Kirsher 	if (!strcmp(np->vpd.model, NIU_ALONSO_MDL_STR) ||
8824e689cf4aSJeff Kirsher 	    !strcmp(np->vpd.model, NIU_KIMI_MDL_STR)) {
8825e689cf4aSJeff Kirsher 		num_10g = 0;
8826e689cf4aSJeff Kirsher 		num_1g = 2;
8827e689cf4aSJeff Kirsher 		parent->plat_type = PLAT_TYPE_ATCA_CP3220;
8828e689cf4aSJeff Kirsher 		parent->num_ports = 4;
8829e689cf4aSJeff Kirsher 		val = (phy_encode(PORT_TYPE_1G, 0) |
8830e689cf4aSJeff Kirsher 		       phy_encode(PORT_TYPE_1G, 1) |
8831e689cf4aSJeff Kirsher 		       phy_encode(PORT_TYPE_1G, 2) |
8832e689cf4aSJeff Kirsher 		       phy_encode(PORT_TYPE_1G, 3));
8833e689cf4aSJeff Kirsher 	} else if (!strcmp(np->vpd.model, NIU_FOXXY_MDL_STR)) {
8834e689cf4aSJeff Kirsher 		num_10g = 2;
8835e689cf4aSJeff Kirsher 		num_1g = 0;
8836e689cf4aSJeff Kirsher 		parent->num_ports = 2;
8837e689cf4aSJeff Kirsher 		val = (phy_encode(PORT_TYPE_10G, 0) |
8838e689cf4aSJeff Kirsher 		       phy_encode(PORT_TYPE_10G, 1));
8839e689cf4aSJeff Kirsher 	} else if ((np->flags & NIU_FLAGS_XCVR_SERDES) &&
8840e689cf4aSJeff Kirsher 		   (parent->plat_type == PLAT_TYPE_NIU)) {
8841e689cf4aSJeff Kirsher 		/* this is the Monza case */
8842e689cf4aSJeff Kirsher 		if (np->flags & NIU_FLAGS_10G) {
8843e689cf4aSJeff Kirsher 			val = (phy_encode(PORT_TYPE_10G, 0) |
8844e689cf4aSJeff Kirsher 			       phy_encode(PORT_TYPE_10G, 1));
8845e689cf4aSJeff Kirsher 		} else {
8846e689cf4aSJeff Kirsher 			val = (phy_encode(PORT_TYPE_1G, 0) |
8847e689cf4aSJeff Kirsher 			       phy_encode(PORT_TYPE_1G, 1));
8848e689cf4aSJeff Kirsher 		}
8849e689cf4aSJeff Kirsher 	} else {
8850e689cf4aSJeff Kirsher 		err = fill_phy_probe_info(np, parent, info);
8851e689cf4aSJeff Kirsher 		if (err)
8852e689cf4aSJeff Kirsher 			return err;
8853e689cf4aSJeff Kirsher 
8854e689cf4aSJeff Kirsher 		num_10g = count_10g_ports(info, &lowest_10g);
8855e689cf4aSJeff Kirsher 		num_1g = count_1g_ports(info, &lowest_1g);
8856e689cf4aSJeff Kirsher 
8857e689cf4aSJeff Kirsher 		switch ((num_10g << 4) | num_1g) {
8858e689cf4aSJeff Kirsher 		case 0x24:
8859e689cf4aSJeff Kirsher 			if (lowest_1g == 10)
8860e689cf4aSJeff Kirsher 				parent->plat_type = PLAT_TYPE_VF_P0;
8861e689cf4aSJeff Kirsher 			else if (lowest_1g == 26)
8862e689cf4aSJeff Kirsher 				parent->plat_type = PLAT_TYPE_VF_P1;
8863e689cf4aSJeff Kirsher 			else
8864e689cf4aSJeff Kirsher 				goto unknown_vg_1g_port;
8865e689cf4aSJeff Kirsher 
8866df561f66SGustavo A. R. Silva 			fallthrough;
8867e689cf4aSJeff Kirsher 		case 0x22:
8868e689cf4aSJeff Kirsher 			val = (phy_encode(PORT_TYPE_10G, 0) |
8869e689cf4aSJeff Kirsher 			       phy_encode(PORT_TYPE_10G, 1) |
8870e689cf4aSJeff Kirsher 			       phy_encode(PORT_TYPE_1G, 2) |
8871e689cf4aSJeff Kirsher 			       phy_encode(PORT_TYPE_1G, 3));
8872e689cf4aSJeff Kirsher 			break;
8873e689cf4aSJeff Kirsher 
8874e689cf4aSJeff Kirsher 		case 0x20:
8875e689cf4aSJeff Kirsher 			val = (phy_encode(PORT_TYPE_10G, 0) |
8876e689cf4aSJeff Kirsher 			       phy_encode(PORT_TYPE_10G, 1));
8877e689cf4aSJeff Kirsher 			break;
8878e689cf4aSJeff Kirsher 
8879e689cf4aSJeff Kirsher 		case 0x10:
8880e689cf4aSJeff Kirsher 			val = phy_encode(PORT_TYPE_10G, np->port);
8881e689cf4aSJeff Kirsher 			break;
8882e689cf4aSJeff Kirsher 
8883e689cf4aSJeff Kirsher 		case 0x14:
8884e689cf4aSJeff Kirsher 			if (lowest_1g == 10)
8885e689cf4aSJeff Kirsher 				parent->plat_type = PLAT_TYPE_VF_P0;
8886e689cf4aSJeff Kirsher 			else if (lowest_1g == 26)
8887e689cf4aSJeff Kirsher 				parent->plat_type = PLAT_TYPE_VF_P1;
8888e689cf4aSJeff Kirsher 			else
8889e689cf4aSJeff Kirsher 				goto unknown_vg_1g_port;
8890e689cf4aSJeff Kirsher 
8891df561f66SGustavo A. R. Silva 			fallthrough;
8892e689cf4aSJeff Kirsher 		case 0x13:
8893e689cf4aSJeff Kirsher 			if ((lowest_10g & 0x7) == 0)
8894e689cf4aSJeff Kirsher 				val = (phy_encode(PORT_TYPE_10G, 0) |
8895e689cf4aSJeff Kirsher 				       phy_encode(PORT_TYPE_1G, 1) |
8896e689cf4aSJeff Kirsher 				       phy_encode(PORT_TYPE_1G, 2) |
8897e689cf4aSJeff Kirsher 				       phy_encode(PORT_TYPE_1G, 3));
8898e689cf4aSJeff Kirsher 			else
8899e689cf4aSJeff Kirsher 				val = (phy_encode(PORT_TYPE_1G, 0) |
8900e689cf4aSJeff Kirsher 				       phy_encode(PORT_TYPE_10G, 1) |
8901e689cf4aSJeff Kirsher 				       phy_encode(PORT_TYPE_1G, 2) |
8902e689cf4aSJeff Kirsher 				       phy_encode(PORT_TYPE_1G, 3));
8903e689cf4aSJeff Kirsher 			break;
8904e689cf4aSJeff Kirsher 
8905e689cf4aSJeff Kirsher 		case 0x04:
8906e689cf4aSJeff Kirsher 			if (lowest_1g == 10)
8907e689cf4aSJeff Kirsher 				parent->plat_type = PLAT_TYPE_VF_P0;
8908e689cf4aSJeff Kirsher 			else if (lowest_1g == 26)
8909e689cf4aSJeff Kirsher 				parent->plat_type = PLAT_TYPE_VF_P1;
8910e689cf4aSJeff Kirsher 			else
8911e689cf4aSJeff Kirsher 				goto unknown_vg_1g_port;
8912e689cf4aSJeff Kirsher 
8913e689cf4aSJeff Kirsher 			val = (phy_encode(PORT_TYPE_1G, 0) |
8914e689cf4aSJeff Kirsher 			       phy_encode(PORT_TYPE_1G, 1) |
8915e689cf4aSJeff Kirsher 			       phy_encode(PORT_TYPE_1G, 2) |
8916e689cf4aSJeff Kirsher 			       phy_encode(PORT_TYPE_1G, 3));
8917e689cf4aSJeff Kirsher 			break;
8918e689cf4aSJeff Kirsher 
8919e689cf4aSJeff Kirsher 		default:
8920e689cf4aSJeff Kirsher 			pr_err("Unsupported port config 10G[%d] 1G[%d]\n",
8921e689cf4aSJeff Kirsher 			       num_10g, num_1g);
8922e689cf4aSJeff Kirsher 			return -EINVAL;
8923e689cf4aSJeff Kirsher 		}
8924e689cf4aSJeff Kirsher 	}
8925e689cf4aSJeff Kirsher 
8926e689cf4aSJeff Kirsher 	parent->port_phy = val;
8927e689cf4aSJeff Kirsher 
8928e689cf4aSJeff Kirsher 	if (parent->plat_type == PLAT_TYPE_NIU)
8929e689cf4aSJeff Kirsher 		niu_n2_divide_channels(parent);
8930e689cf4aSJeff Kirsher 	else
8931e689cf4aSJeff Kirsher 		niu_divide_channels(parent, num_10g, num_1g);
8932e689cf4aSJeff Kirsher 
8933e689cf4aSJeff Kirsher 	niu_divide_rdc_groups(parent, num_10g, num_1g);
8934e689cf4aSJeff Kirsher 
8935e689cf4aSJeff Kirsher 	return 0;
8936e689cf4aSJeff Kirsher 
8937e689cf4aSJeff Kirsher unknown_vg_1g_port:
8938e689cf4aSJeff Kirsher 	pr_err("Cannot identify platform type, 1gport=%d\n", lowest_1g);
8939e689cf4aSJeff Kirsher 	return -EINVAL;
8940e689cf4aSJeff Kirsher }
8941e689cf4aSJeff Kirsher 
niu_probe_ports(struct niu * np)8942f73d12bdSBill Pemberton static int niu_probe_ports(struct niu *np)
8943e689cf4aSJeff Kirsher {
8944e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
8945e689cf4aSJeff Kirsher 	int err, i;
8946e689cf4aSJeff Kirsher 
8947e689cf4aSJeff Kirsher 	if (parent->port_phy == PORT_PHY_UNKNOWN) {
8948e689cf4aSJeff Kirsher 		err = walk_phys(np, parent);
8949e689cf4aSJeff Kirsher 		if (err)
8950e689cf4aSJeff Kirsher 			return err;
8951e689cf4aSJeff Kirsher 
8952e689cf4aSJeff Kirsher 		niu_set_ldg_timer_res(np, 2);
8953e689cf4aSJeff Kirsher 		for (i = 0; i <= LDN_MAX; i++)
8954e689cf4aSJeff Kirsher 			niu_ldn_irq_enable(np, i, 0);
8955e689cf4aSJeff Kirsher 	}
8956e689cf4aSJeff Kirsher 
8957e689cf4aSJeff Kirsher 	if (parent->port_phy == PORT_PHY_INVALID)
8958e689cf4aSJeff Kirsher 		return -EINVAL;
8959e689cf4aSJeff Kirsher 
8960e689cf4aSJeff Kirsher 	return 0;
8961e689cf4aSJeff Kirsher }
8962e689cf4aSJeff Kirsher 
niu_classifier_swstate_init(struct niu * np)8963f73d12bdSBill Pemberton static int niu_classifier_swstate_init(struct niu *np)
8964e689cf4aSJeff Kirsher {
8965e689cf4aSJeff Kirsher 	struct niu_classifier *cp = &np->clas;
8966e689cf4aSJeff Kirsher 
8967e689cf4aSJeff Kirsher 	cp->tcam_top = (u16) np->port;
8968e689cf4aSJeff Kirsher 	cp->tcam_sz = np->parent->tcam_num_entries / np->parent->num_ports;
8969e689cf4aSJeff Kirsher 	cp->h1_init = 0xffffffff;
8970e689cf4aSJeff Kirsher 	cp->h2_init = 0xffff;
8971e689cf4aSJeff Kirsher 
8972e689cf4aSJeff Kirsher 	return fflp_early_init(np);
8973e689cf4aSJeff Kirsher }
8974e689cf4aSJeff Kirsher 
niu_link_config_init(struct niu * np)8975f73d12bdSBill Pemberton static void niu_link_config_init(struct niu *np)
8976e689cf4aSJeff Kirsher {
8977e689cf4aSJeff Kirsher 	struct niu_link_config *lp = &np->link_config;
8978e689cf4aSJeff Kirsher 
8979e689cf4aSJeff Kirsher 	lp->advertising = (ADVERTISED_10baseT_Half |
8980e689cf4aSJeff Kirsher 			   ADVERTISED_10baseT_Full |
8981e689cf4aSJeff Kirsher 			   ADVERTISED_100baseT_Half |
8982e689cf4aSJeff Kirsher 			   ADVERTISED_100baseT_Full |
8983e689cf4aSJeff Kirsher 			   ADVERTISED_1000baseT_Half |
8984e689cf4aSJeff Kirsher 			   ADVERTISED_1000baseT_Full |
8985e689cf4aSJeff Kirsher 			   ADVERTISED_10000baseT_Full |
8986e689cf4aSJeff Kirsher 			   ADVERTISED_Autoneg);
8987e689cf4aSJeff Kirsher 	lp->speed = lp->active_speed = SPEED_INVALID;
8988e689cf4aSJeff Kirsher 	lp->duplex = DUPLEX_FULL;
8989e689cf4aSJeff Kirsher 	lp->active_duplex = DUPLEX_INVALID;
8990e689cf4aSJeff Kirsher 	lp->autoneg = 1;
8991e689cf4aSJeff Kirsher #if 0
8992e689cf4aSJeff Kirsher 	lp->loopback_mode = LOOPBACK_MAC;
8993e689cf4aSJeff Kirsher 	lp->active_speed = SPEED_10000;
8994e689cf4aSJeff Kirsher 	lp->active_duplex = DUPLEX_FULL;
8995e689cf4aSJeff Kirsher #else
8996e689cf4aSJeff Kirsher 	lp->loopback_mode = LOOPBACK_DISABLED;
8997e689cf4aSJeff Kirsher #endif
8998e689cf4aSJeff Kirsher }
8999e689cf4aSJeff Kirsher 
niu_init_mac_ipp_pcs_base(struct niu * np)9000f73d12bdSBill Pemberton static int niu_init_mac_ipp_pcs_base(struct niu *np)
9001e689cf4aSJeff Kirsher {
9002e689cf4aSJeff Kirsher 	switch (np->port) {
9003e689cf4aSJeff Kirsher 	case 0:
9004e689cf4aSJeff Kirsher 		np->mac_regs = np->regs + XMAC_PORT0_OFF;
9005e689cf4aSJeff Kirsher 		np->ipp_off  = 0x00000;
9006e689cf4aSJeff Kirsher 		np->pcs_off  = 0x04000;
9007e689cf4aSJeff Kirsher 		np->xpcs_off = 0x02000;
9008e689cf4aSJeff Kirsher 		break;
9009e689cf4aSJeff Kirsher 
9010e689cf4aSJeff Kirsher 	case 1:
9011e689cf4aSJeff Kirsher 		np->mac_regs = np->regs + XMAC_PORT1_OFF;
9012e689cf4aSJeff Kirsher 		np->ipp_off  = 0x08000;
9013e689cf4aSJeff Kirsher 		np->pcs_off  = 0x0a000;
9014e689cf4aSJeff Kirsher 		np->xpcs_off = 0x08000;
9015e689cf4aSJeff Kirsher 		break;
9016e689cf4aSJeff Kirsher 
9017e689cf4aSJeff Kirsher 	case 2:
9018e689cf4aSJeff Kirsher 		np->mac_regs = np->regs + BMAC_PORT2_OFF;
9019e689cf4aSJeff Kirsher 		np->ipp_off  = 0x04000;
9020e689cf4aSJeff Kirsher 		np->pcs_off  = 0x0e000;
9021e689cf4aSJeff Kirsher 		np->xpcs_off = ~0UL;
9022e689cf4aSJeff Kirsher 		break;
9023e689cf4aSJeff Kirsher 
9024e689cf4aSJeff Kirsher 	case 3:
9025e689cf4aSJeff Kirsher 		np->mac_regs = np->regs + BMAC_PORT3_OFF;
9026e689cf4aSJeff Kirsher 		np->ipp_off  = 0x0c000;
9027e689cf4aSJeff Kirsher 		np->pcs_off  = 0x12000;
9028e689cf4aSJeff Kirsher 		np->xpcs_off = ~0UL;
9029e689cf4aSJeff Kirsher 		break;
9030e689cf4aSJeff Kirsher 
9031e689cf4aSJeff Kirsher 	default:
9032e689cf4aSJeff Kirsher 		dev_err(np->device, "Port %u is invalid, cannot compute MAC block offset\n", np->port);
9033e689cf4aSJeff Kirsher 		return -EINVAL;
9034e689cf4aSJeff Kirsher 	}
9035e689cf4aSJeff Kirsher 
9036e689cf4aSJeff Kirsher 	return 0;
9037e689cf4aSJeff Kirsher }
9038e689cf4aSJeff Kirsher 
niu_try_msix(struct niu * np,u8 * ldg_num_map)9039f73d12bdSBill Pemberton static void niu_try_msix(struct niu *np, u8 *ldg_num_map)
9040e689cf4aSJeff Kirsher {
9041e689cf4aSJeff Kirsher 	struct msix_entry msi_vec[NIU_NUM_LDG];
9042e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
9043e689cf4aSJeff Kirsher 	struct pci_dev *pdev = np->pdev;
90449e7df17eSAlexander Gordeev 	int i, num_irqs;
9045e689cf4aSJeff Kirsher 	u8 first_ldg;
9046e689cf4aSJeff Kirsher 
9047e689cf4aSJeff Kirsher 	first_ldg = (NIU_NUM_LDG / parent->num_ports) * np->port;
9048e689cf4aSJeff Kirsher 	for (i = 0; i < (NIU_NUM_LDG / parent->num_ports); i++)
9049e689cf4aSJeff Kirsher 		ldg_num_map[i] = first_ldg + i;
9050e689cf4aSJeff Kirsher 
9051e689cf4aSJeff Kirsher 	num_irqs = (parent->rxchan_per_port[np->port] +
9052e689cf4aSJeff Kirsher 		    parent->txchan_per_port[np->port] +
9053e689cf4aSJeff Kirsher 		    (np->port == 0 ? 3 : 1));
9054e689cf4aSJeff Kirsher 	BUG_ON(num_irqs > (NIU_NUM_LDG / parent->num_ports));
9055e689cf4aSJeff Kirsher 
9056e689cf4aSJeff Kirsher 	for (i = 0; i < num_irqs; i++) {
9057e689cf4aSJeff Kirsher 		msi_vec[i].vector = 0;
9058e689cf4aSJeff Kirsher 		msi_vec[i].entry = i;
9059e689cf4aSJeff Kirsher 	}
9060e689cf4aSJeff Kirsher 
90619e7df17eSAlexander Gordeev 	num_irqs = pci_enable_msix_range(pdev, msi_vec, 1, num_irqs);
90629e7df17eSAlexander Gordeev 	if (num_irqs < 0) {
9063e689cf4aSJeff Kirsher 		np->flags &= ~NIU_FLAGS_MSIX;
9064e689cf4aSJeff Kirsher 		return;
9065e689cf4aSJeff Kirsher 	}
9066e689cf4aSJeff Kirsher 
9067e689cf4aSJeff Kirsher 	np->flags |= NIU_FLAGS_MSIX;
9068e689cf4aSJeff Kirsher 	for (i = 0; i < num_irqs; i++)
9069e689cf4aSJeff Kirsher 		np->ldg[i].irq = msi_vec[i].vector;
9070e689cf4aSJeff Kirsher 	np->num_ldg = num_irqs;
9071e689cf4aSJeff Kirsher }
9072e689cf4aSJeff Kirsher 
niu_n2_irq_init(struct niu * np,u8 * ldg_num_map)9073f73d12bdSBill Pemberton static int niu_n2_irq_init(struct niu *np, u8 *ldg_num_map)
9074e689cf4aSJeff Kirsher {
9075e689cf4aSJeff Kirsher #ifdef CONFIG_SPARC64
9076e689cf4aSJeff Kirsher 	struct platform_device *op = np->op;
9077e689cf4aSJeff Kirsher 	const u32 *int_prop;
9078e689cf4aSJeff Kirsher 	int i;
9079e689cf4aSJeff Kirsher 
9080e689cf4aSJeff Kirsher 	int_prop = of_get_property(op->dev.of_node, "interrupts", NULL);
9081e689cf4aSJeff Kirsher 	if (!int_prop)
9082e689cf4aSJeff Kirsher 		return -ENODEV;
9083e689cf4aSJeff Kirsher 
9084e689cf4aSJeff Kirsher 	for (i = 0; i < op->archdata.num_irqs; i++) {
9085e689cf4aSJeff Kirsher 		ldg_num_map[i] = int_prop[i];
9086e689cf4aSJeff Kirsher 		np->ldg[i].irq = op->archdata.irqs[i];
9087e689cf4aSJeff Kirsher 	}
9088e689cf4aSJeff Kirsher 
9089e689cf4aSJeff Kirsher 	np->num_ldg = op->archdata.num_irqs;
9090e689cf4aSJeff Kirsher 
9091e689cf4aSJeff Kirsher 	return 0;
9092e689cf4aSJeff Kirsher #else
9093e689cf4aSJeff Kirsher 	return -EINVAL;
9094e689cf4aSJeff Kirsher #endif
9095e689cf4aSJeff Kirsher }
9096e689cf4aSJeff Kirsher 
niu_ldg_init(struct niu * np)9097f73d12bdSBill Pemberton static int niu_ldg_init(struct niu *np)
9098e689cf4aSJeff Kirsher {
9099e689cf4aSJeff Kirsher 	struct niu_parent *parent = np->parent;
9100e689cf4aSJeff Kirsher 	u8 ldg_num_map[NIU_NUM_LDG];
9101e689cf4aSJeff Kirsher 	int first_chan, num_chan;
9102e689cf4aSJeff Kirsher 	int i, err, ldg_rotor;
9103e689cf4aSJeff Kirsher 	u8 port;
9104e689cf4aSJeff Kirsher 
9105e689cf4aSJeff Kirsher 	np->num_ldg = 1;
9106e689cf4aSJeff Kirsher 	np->ldg[0].irq = np->dev->irq;
9107e689cf4aSJeff Kirsher 	if (parent->plat_type == PLAT_TYPE_NIU) {
9108e689cf4aSJeff Kirsher 		err = niu_n2_irq_init(np, ldg_num_map);
9109e689cf4aSJeff Kirsher 		if (err)
9110e689cf4aSJeff Kirsher 			return err;
9111e689cf4aSJeff Kirsher 	} else
9112e689cf4aSJeff Kirsher 		niu_try_msix(np, ldg_num_map);
9113e689cf4aSJeff Kirsher 
9114e689cf4aSJeff Kirsher 	port = np->port;
9115e689cf4aSJeff Kirsher 	for (i = 0; i < np->num_ldg; i++) {
9116e689cf4aSJeff Kirsher 		struct niu_ldg *lp = &np->ldg[i];
9117e689cf4aSJeff Kirsher 
9118b48b89f9SJakub Kicinski 		netif_napi_add(np->dev, &lp->napi, niu_poll);
9119e689cf4aSJeff Kirsher 
9120e689cf4aSJeff Kirsher 		lp->np = np;
9121e689cf4aSJeff Kirsher 		lp->ldg_num = ldg_num_map[i];
9122e689cf4aSJeff Kirsher 		lp->timer = 2; /* XXX */
9123e689cf4aSJeff Kirsher 
9124e689cf4aSJeff Kirsher 		/* On N2 NIU the firmware has setup the SID mappings so they go
9125e689cf4aSJeff Kirsher 		 * to the correct values that will route the LDG to the proper
9126e689cf4aSJeff Kirsher 		 * interrupt in the NCU interrupt table.
9127e689cf4aSJeff Kirsher 		 */
9128e689cf4aSJeff Kirsher 		if (np->parent->plat_type != PLAT_TYPE_NIU) {
9129e689cf4aSJeff Kirsher 			err = niu_set_ldg_sid(np, lp->ldg_num, port, i);
9130e689cf4aSJeff Kirsher 			if (err)
9131e689cf4aSJeff Kirsher 				return err;
9132e689cf4aSJeff Kirsher 		}
9133e689cf4aSJeff Kirsher 	}
9134e689cf4aSJeff Kirsher 
9135e689cf4aSJeff Kirsher 	/* We adopt the LDG assignment ordering used by the N2 NIU
9136e689cf4aSJeff Kirsher 	 * 'interrupt' properties because that simplifies a lot of
9137e689cf4aSJeff Kirsher 	 * things.  This ordering is:
9138e689cf4aSJeff Kirsher 	 *
9139e689cf4aSJeff Kirsher 	 *	MAC
9140e689cf4aSJeff Kirsher 	 *	MIF	(if port zero)
9141e689cf4aSJeff Kirsher 	 *	SYSERR	(if port zero)
9142e689cf4aSJeff Kirsher 	 *	RX channels
9143e689cf4aSJeff Kirsher 	 *	TX channels
9144e689cf4aSJeff Kirsher 	 */
9145e689cf4aSJeff Kirsher 
9146e689cf4aSJeff Kirsher 	ldg_rotor = 0;
9147e689cf4aSJeff Kirsher 
9148e689cf4aSJeff Kirsher 	err = niu_ldg_assign_ldn(np, parent, ldg_num_map[ldg_rotor],
9149e689cf4aSJeff Kirsher 				  LDN_MAC(port));
9150e689cf4aSJeff Kirsher 	if (err)
9151e689cf4aSJeff Kirsher 		return err;
9152e689cf4aSJeff Kirsher 
9153e689cf4aSJeff Kirsher 	ldg_rotor++;
9154e689cf4aSJeff Kirsher 	if (ldg_rotor == np->num_ldg)
9155e689cf4aSJeff Kirsher 		ldg_rotor = 0;
9156e689cf4aSJeff Kirsher 
9157e689cf4aSJeff Kirsher 	if (port == 0) {
9158e689cf4aSJeff Kirsher 		err = niu_ldg_assign_ldn(np, parent,
9159e689cf4aSJeff Kirsher 					 ldg_num_map[ldg_rotor],
9160e689cf4aSJeff Kirsher 					 LDN_MIF);
9161e689cf4aSJeff Kirsher 		if (err)
9162e689cf4aSJeff Kirsher 			return err;
9163e689cf4aSJeff Kirsher 
9164e689cf4aSJeff Kirsher 		ldg_rotor++;
9165e689cf4aSJeff Kirsher 		if (ldg_rotor == np->num_ldg)
9166e689cf4aSJeff Kirsher 			ldg_rotor = 0;
9167e689cf4aSJeff Kirsher 
9168e689cf4aSJeff Kirsher 		err = niu_ldg_assign_ldn(np, parent,
9169e689cf4aSJeff Kirsher 					 ldg_num_map[ldg_rotor],
9170e689cf4aSJeff Kirsher 					 LDN_DEVICE_ERROR);
9171e689cf4aSJeff Kirsher 		if (err)
9172e689cf4aSJeff Kirsher 			return err;
9173e689cf4aSJeff Kirsher 
9174e689cf4aSJeff Kirsher 		ldg_rotor++;
9175e689cf4aSJeff Kirsher 		if (ldg_rotor == np->num_ldg)
9176e689cf4aSJeff Kirsher 			ldg_rotor = 0;
9177e689cf4aSJeff Kirsher 
9178e689cf4aSJeff Kirsher 	}
9179e689cf4aSJeff Kirsher 
9180e689cf4aSJeff Kirsher 	first_chan = 0;
9181e689cf4aSJeff Kirsher 	for (i = 0; i < port; i++)
9182e689cf4aSJeff Kirsher 		first_chan += parent->rxchan_per_port[i];
9183e689cf4aSJeff Kirsher 	num_chan = parent->rxchan_per_port[port];
9184e689cf4aSJeff Kirsher 
9185e689cf4aSJeff Kirsher 	for (i = first_chan; i < (first_chan + num_chan); i++) {
9186e689cf4aSJeff Kirsher 		err = niu_ldg_assign_ldn(np, parent,
9187e689cf4aSJeff Kirsher 					 ldg_num_map[ldg_rotor],
9188e689cf4aSJeff Kirsher 					 LDN_RXDMA(i));
9189e689cf4aSJeff Kirsher 		if (err)
9190e689cf4aSJeff Kirsher 			return err;
9191e689cf4aSJeff Kirsher 		ldg_rotor++;
9192e689cf4aSJeff Kirsher 		if (ldg_rotor == np->num_ldg)
9193e689cf4aSJeff Kirsher 			ldg_rotor = 0;
9194e689cf4aSJeff Kirsher 	}
9195e689cf4aSJeff Kirsher 
9196e689cf4aSJeff Kirsher 	first_chan = 0;
9197e689cf4aSJeff Kirsher 	for (i = 0; i < port; i++)
9198e689cf4aSJeff Kirsher 		first_chan += parent->txchan_per_port[i];
9199e689cf4aSJeff Kirsher 	num_chan = parent->txchan_per_port[port];
9200e689cf4aSJeff Kirsher 	for (i = first_chan; i < (first_chan + num_chan); i++) {
9201e689cf4aSJeff Kirsher 		err = niu_ldg_assign_ldn(np, parent,
9202e689cf4aSJeff Kirsher 					 ldg_num_map[ldg_rotor],
9203e689cf4aSJeff Kirsher 					 LDN_TXDMA(i));
9204e689cf4aSJeff Kirsher 		if (err)
9205e689cf4aSJeff Kirsher 			return err;
9206e689cf4aSJeff Kirsher 		ldg_rotor++;
9207e689cf4aSJeff Kirsher 		if (ldg_rotor == np->num_ldg)
9208e689cf4aSJeff Kirsher 			ldg_rotor = 0;
9209e689cf4aSJeff Kirsher 	}
9210e689cf4aSJeff Kirsher 
9211e689cf4aSJeff Kirsher 	return 0;
9212e689cf4aSJeff Kirsher }
9213e689cf4aSJeff Kirsher 
niu_ldg_free(struct niu * np)9214f73d12bdSBill Pemberton static void niu_ldg_free(struct niu *np)
9215e689cf4aSJeff Kirsher {
9216e689cf4aSJeff Kirsher 	if (np->flags & NIU_FLAGS_MSIX)
9217e689cf4aSJeff Kirsher 		pci_disable_msix(np->pdev);
9218e689cf4aSJeff Kirsher }
9219e689cf4aSJeff Kirsher 
niu_get_of_props(struct niu * np)9220f73d12bdSBill Pemberton static int niu_get_of_props(struct niu *np)
9221e689cf4aSJeff Kirsher {
9222e689cf4aSJeff Kirsher #ifdef CONFIG_SPARC64
9223e689cf4aSJeff Kirsher 	struct net_device *dev = np->dev;
9224e689cf4aSJeff Kirsher 	struct device_node *dp;
9225e689cf4aSJeff Kirsher 	const char *phy_type;
9226e689cf4aSJeff Kirsher 	const u8 *mac_addr;
9227e689cf4aSJeff Kirsher 	const char *model;
9228e689cf4aSJeff Kirsher 	int prop_len;
9229e689cf4aSJeff Kirsher 
9230e689cf4aSJeff Kirsher 	if (np->parent->plat_type == PLAT_TYPE_NIU)
9231e689cf4aSJeff Kirsher 		dp = np->op->dev.of_node;
9232e689cf4aSJeff Kirsher 	else
9233e689cf4aSJeff Kirsher 		dp = pci_device_to_OF_node(np->pdev);
9234e689cf4aSJeff Kirsher 
9235451395f7SMartin Kaiser 	phy_type = of_get_property(dp, "phy-type", NULL);
9236e689cf4aSJeff Kirsher 	if (!phy_type) {
9237f7ce9103SRob Herring 		netdev_err(dev, "%pOF: OF node lacks phy-type property\n", dp);
9238e689cf4aSJeff Kirsher 		return -EINVAL;
9239e689cf4aSJeff Kirsher 	}
9240e689cf4aSJeff Kirsher 
9241e689cf4aSJeff Kirsher 	if (!strcmp(phy_type, "none"))
9242e689cf4aSJeff Kirsher 		return -ENODEV;
9243e689cf4aSJeff Kirsher 
9244e689cf4aSJeff Kirsher 	strcpy(np->vpd.phy_type, phy_type);
9245e689cf4aSJeff Kirsher 
9246e689cf4aSJeff Kirsher 	if (niu_phy_type_prop_decode(np, np->vpd.phy_type)) {
9247f7ce9103SRob Herring 		netdev_err(dev, "%pOF: Illegal phy string [%s]\n",
9248f7ce9103SRob Herring 			   dp, np->vpd.phy_type);
9249e689cf4aSJeff Kirsher 		return -EINVAL;
9250e689cf4aSJeff Kirsher 	}
9251e689cf4aSJeff Kirsher 
9252e689cf4aSJeff Kirsher 	mac_addr = of_get_property(dp, "local-mac-address", &prop_len);
9253e689cf4aSJeff Kirsher 	if (!mac_addr) {
9254f7ce9103SRob Herring 		netdev_err(dev, "%pOF: OF node lacks local-mac-address property\n",
9255f7ce9103SRob Herring 			   dp);
9256e689cf4aSJeff Kirsher 		return -EINVAL;
9257e689cf4aSJeff Kirsher 	}
9258e689cf4aSJeff Kirsher 	if (prop_len != dev->addr_len) {
9259f7ce9103SRob Herring 		netdev_err(dev, "%pOF: OF MAC address prop len (%d) is wrong\n",
9260f7ce9103SRob Herring 			   dp, prop_len);
9261e689cf4aSJeff Kirsher 	}
9262a05e4c0aSJakub Kicinski 	eth_hw_addr_set(dev, mac_addr);
9263aaeb6cdfSJiri Pirko 	if (!is_valid_ether_addr(&dev->dev_addr[0])) {
9264f7ce9103SRob Herring 		netdev_err(dev, "%pOF: OF MAC address is invalid\n", dp);
9265f7ce9103SRob Herring 		netdev_err(dev, "%pOF: [ %pM ]\n", dp, dev->dev_addr);
9266e689cf4aSJeff Kirsher 		return -EINVAL;
9267e689cf4aSJeff Kirsher 	}
9268e689cf4aSJeff Kirsher 
9269451395f7SMartin Kaiser 	model = of_get_property(dp, "model", NULL);
9270e689cf4aSJeff Kirsher 
9271e689cf4aSJeff Kirsher 	if (model)
9272e689cf4aSJeff Kirsher 		strcpy(np->vpd.model, model);
9273e689cf4aSJeff Kirsher 
92741a87e641SRob Herring 	if (of_property_read_bool(dp, "hot-swappable-phy")) {
9275e689cf4aSJeff Kirsher 		np->flags |= (NIU_FLAGS_10G | NIU_FLAGS_FIBER |
9276e689cf4aSJeff Kirsher 			NIU_FLAGS_HOTPLUG_PHY);
9277e689cf4aSJeff Kirsher 	}
9278e689cf4aSJeff Kirsher 
9279e689cf4aSJeff Kirsher 	return 0;
9280e689cf4aSJeff Kirsher #else
9281e689cf4aSJeff Kirsher 	return -EINVAL;
9282e689cf4aSJeff Kirsher #endif
9283e689cf4aSJeff Kirsher }
9284e689cf4aSJeff Kirsher 
niu_get_invariants(struct niu * np)9285f73d12bdSBill Pemberton static int niu_get_invariants(struct niu *np)
9286e689cf4aSJeff Kirsher {
9287e689cf4aSJeff Kirsher 	int err, have_props;
9288e689cf4aSJeff Kirsher 	u32 offset;
9289e689cf4aSJeff Kirsher 
9290e689cf4aSJeff Kirsher 	err = niu_get_of_props(np);
9291e689cf4aSJeff Kirsher 	if (err == -ENODEV)
9292e689cf4aSJeff Kirsher 		return err;
9293e689cf4aSJeff Kirsher 
9294e689cf4aSJeff Kirsher 	have_props = !err;
9295e689cf4aSJeff Kirsher 
9296e689cf4aSJeff Kirsher 	err = niu_init_mac_ipp_pcs_base(np);
9297e689cf4aSJeff Kirsher 	if (err)
9298e689cf4aSJeff Kirsher 		return err;
9299e689cf4aSJeff Kirsher 
9300e689cf4aSJeff Kirsher 	if (have_props) {
9301e689cf4aSJeff Kirsher 		err = niu_get_and_validate_port(np);
9302e689cf4aSJeff Kirsher 		if (err)
9303e689cf4aSJeff Kirsher 			return err;
9304e689cf4aSJeff Kirsher 
9305e689cf4aSJeff Kirsher 	} else  {
9306e689cf4aSJeff Kirsher 		if (np->parent->plat_type == PLAT_TYPE_NIU)
9307e689cf4aSJeff Kirsher 			return -EINVAL;
9308e689cf4aSJeff Kirsher 
9309e689cf4aSJeff Kirsher 		nw64(ESPC_PIO_EN, ESPC_PIO_EN_ENABLE);
9310e689cf4aSJeff Kirsher 		offset = niu_pci_vpd_offset(np);
9311e689cf4aSJeff Kirsher 		netif_printk(np, probe, KERN_DEBUG, np->dev,
9312e689cf4aSJeff Kirsher 			     "%s() VPD offset [%08x]\n", __func__, offset);
9313e6e33770SDu Cheng 		if (offset) {
9314e6e33770SDu Cheng 			err = niu_pci_vpd_fetch(np, offset);
9315e6e33770SDu Cheng 			if (err < 0)
9316e6e33770SDu Cheng 				return err;
9317e6e33770SDu Cheng 		}
9318e689cf4aSJeff Kirsher 		nw64(ESPC_PIO_EN, 0);
9319e689cf4aSJeff Kirsher 
9320e689cf4aSJeff Kirsher 		if (np->flags & NIU_FLAGS_VPD_VALID) {
9321e689cf4aSJeff Kirsher 			niu_pci_vpd_validate(np);
9322e689cf4aSJeff Kirsher 			err = niu_get_and_validate_port(np);
9323e689cf4aSJeff Kirsher 			if (err)
9324e689cf4aSJeff Kirsher 				return err;
9325e689cf4aSJeff Kirsher 		}
9326e689cf4aSJeff Kirsher 
9327e689cf4aSJeff Kirsher 		if (!(np->flags & NIU_FLAGS_VPD_VALID)) {
9328e689cf4aSJeff Kirsher 			err = niu_get_and_validate_port(np);
9329e689cf4aSJeff Kirsher 			if (err)
9330e689cf4aSJeff Kirsher 				return err;
9331e689cf4aSJeff Kirsher 			err = niu_pci_probe_sprom(np);
9332e689cf4aSJeff Kirsher 			if (err)
9333e689cf4aSJeff Kirsher 				return err;
9334e689cf4aSJeff Kirsher 		}
9335e689cf4aSJeff Kirsher 	}
9336e689cf4aSJeff Kirsher 
9337e689cf4aSJeff Kirsher 	err = niu_probe_ports(np);
9338e689cf4aSJeff Kirsher 	if (err)
9339e689cf4aSJeff Kirsher 		return err;
9340e689cf4aSJeff Kirsher 
9341e689cf4aSJeff Kirsher 	niu_ldg_init(np);
9342e689cf4aSJeff Kirsher 
9343e689cf4aSJeff Kirsher 	niu_classifier_swstate_init(np);
9344e689cf4aSJeff Kirsher 	niu_link_config_init(np);
9345e689cf4aSJeff Kirsher 
9346e689cf4aSJeff Kirsher 	err = niu_determine_phy_disposition(np);
9347e689cf4aSJeff Kirsher 	if (!err)
9348e689cf4aSJeff Kirsher 		err = niu_init_link(np);
9349e689cf4aSJeff Kirsher 
9350e689cf4aSJeff Kirsher 	return err;
9351e689cf4aSJeff Kirsher }
9352e689cf4aSJeff Kirsher 
9353e689cf4aSJeff Kirsher static LIST_HEAD(niu_parent_list);
9354e689cf4aSJeff Kirsher static DEFINE_MUTEX(niu_parent_lock);
9355e689cf4aSJeff Kirsher static int niu_parent_index;
9356e689cf4aSJeff Kirsher 
show_port_phy(struct device * dev,struct device_attribute * attr,char * buf)9357e689cf4aSJeff Kirsher static ssize_t show_port_phy(struct device *dev,
9358e689cf4aSJeff Kirsher 			     struct device_attribute *attr, char *buf)
9359e689cf4aSJeff Kirsher {
9360e689cf4aSJeff Kirsher 	struct platform_device *plat_dev = to_platform_device(dev);
9361df3f1c39SJingoo Han 	struct niu_parent *p = dev_get_platdata(&plat_dev->dev);
9362e689cf4aSJeff Kirsher 	u32 port_phy = p->port_phy;
9363e689cf4aSJeff Kirsher 	char *orig_buf = buf;
9364e689cf4aSJeff Kirsher 	int i;
9365e689cf4aSJeff Kirsher 
9366e689cf4aSJeff Kirsher 	if (port_phy == PORT_PHY_UNKNOWN ||
9367e689cf4aSJeff Kirsher 	    port_phy == PORT_PHY_INVALID)
9368e689cf4aSJeff Kirsher 		return 0;
9369e689cf4aSJeff Kirsher 
9370e689cf4aSJeff Kirsher 	for (i = 0; i < p->num_ports; i++) {
9371e689cf4aSJeff Kirsher 		const char *type_str;
9372e689cf4aSJeff Kirsher 		int type;
9373e689cf4aSJeff Kirsher 
9374e689cf4aSJeff Kirsher 		type = phy_decode(port_phy, i);
9375e689cf4aSJeff Kirsher 		if (type == PORT_TYPE_10G)
9376e689cf4aSJeff Kirsher 			type_str = "10G";
9377e689cf4aSJeff Kirsher 		else
9378e689cf4aSJeff Kirsher 			type_str = "1G";
9379e689cf4aSJeff Kirsher 		buf += sprintf(buf,
9380e689cf4aSJeff Kirsher 			       (i == 0) ? "%s" : " %s",
9381e689cf4aSJeff Kirsher 			       type_str);
9382e689cf4aSJeff Kirsher 	}
9383e689cf4aSJeff Kirsher 	buf += sprintf(buf, "\n");
9384e689cf4aSJeff Kirsher 	return buf - orig_buf;
9385e689cf4aSJeff Kirsher }
9386e689cf4aSJeff Kirsher 
show_plat_type(struct device * dev,struct device_attribute * attr,char * buf)9387e689cf4aSJeff Kirsher static ssize_t show_plat_type(struct device *dev,
9388e689cf4aSJeff Kirsher 			      struct device_attribute *attr, char *buf)
9389e689cf4aSJeff Kirsher {
9390e689cf4aSJeff Kirsher 	struct platform_device *plat_dev = to_platform_device(dev);
9391df3f1c39SJingoo Han 	struct niu_parent *p = dev_get_platdata(&plat_dev->dev);
9392e689cf4aSJeff Kirsher 	const char *type_str;
9393e689cf4aSJeff Kirsher 
9394e689cf4aSJeff Kirsher 	switch (p->plat_type) {
9395e689cf4aSJeff Kirsher 	case PLAT_TYPE_ATLAS:
9396e689cf4aSJeff Kirsher 		type_str = "atlas";
9397e689cf4aSJeff Kirsher 		break;
9398e689cf4aSJeff Kirsher 	case PLAT_TYPE_NIU:
9399e689cf4aSJeff Kirsher 		type_str = "niu";
9400e689cf4aSJeff Kirsher 		break;
9401e689cf4aSJeff Kirsher 	case PLAT_TYPE_VF_P0:
9402e689cf4aSJeff Kirsher 		type_str = "vf_p0";
9403e689cf4aSJeff Kirsher 		break;
9404e689cf4aSJeff Kirsher 	case PLAT_TYPE_VF_P1:
9405e689cf4aSJeff Kirsher 		type_str = "vf_p1";
9406e689cf4aSJeff Kirsher 		break;
9407e689cf4aSJeff Kirsher 	default:
9408e689cf4aSJeff Kirsher 		type_str = "unknown";
9409e689cf4aSJeff Kirsher 		break;
9410e689cf4aSJeff Kirsher 	}
9411e689cf4aSJeff Kirsher 
9412e689cf4aSJeff Kirsher 	return sprintf(buf, "%s\n", type_str);
9413e689cf4aSJeff Kirsher }
9414e689cf4aSJeff Kirsher 
__show_chan_per_port(struct device * dev,struct device_attribute * attr,char * buf,int rx)9415e689cf4aSJeff Kirsher static ssize_t __show_chan_per_port(struct device *dev,
9416e689cf4aSJeff Kirsher 				    struct device_attribute *attr, char *buf,
9417e689cf4aSJeff Kirsher 				    int rx)
9418e689cf4aSJeff Kirsher {
9419e689cf4aSJeff Kirsher 	struct platform_device *plat_dev = to_platform_device(dev);
9420df3f1c39SJingoo Han 	struct niu_parent *p = dev_get_platdata(&plat_dev->dev);
9421e689cf4aSJeff Kirsher 	char *orig_buf = buf;
9422e689cf4aSJeff Kirsher 	u8 *arr;
9423e689cf4aSJeff Kirsher 	int i;
9424e689cf4aSJeff Kirsher 
9425e689cf4aSJeff Kirsher 	arr = (rx ? p->rxchan_per_port : p->txchan_per_port);
9426e689cf4aSJeff Kirsher 
9427e689cf4aSJeff Kirsher 	for (i = 0; i < p->num_ports; i++) {
9428e689cf4aSJeff Kirsher 		buf += sprintf(buf,
9429e689cf4aSJeff Kirsher 			       (i == 0) ? "%d" : " %d",
9430e689cf4aSJeff Kirsher 			       arr[i]);
9431e689cf4aSJeff Kirsher 	}
9432e689cf4aSJeff Kirsher 	buf += sprintf(buf, "\n");
9433e689cf4aSJeff Kirsher 
9434e689cf4aSJeff Kirsher 	return buf - orig_buf;
9435e689cf4aSJeff Kirsher }
9436e689cf4aSJeff Kirsher 
show_rxchan_per_port(struct device * dev,struct device_attribute * attr,char * buf)9437e689cf4aSJeff Kirsher static ssize_t show_rxchan_per_port(struct device *dev,
9438e689cf4aSJeff Kirsher 				    struct device_attribute *attr, char *buf)
9439e689cf4aSJeff Kirsher {
9440e689cf4aSJeff Kirsher 	return __show_chan_per_port(dev, attr, buf, 1);
9441e689cf4aSJeff Kirsher }
9442e689cf4aSJeff Kirsher 
show_txchan_per_port(struct device * dev,struct device_attribute * attr,char * buf)9443e689cf4aSJeff Kirsher static ssize_t show_txchan_per_port(struct device *dev,
9444e689cf4aSJeff Kirsher 				    struct device_attribute *attr, char *buf)
9445e689cf4aSJeff Kirsher {
9446e689cf4aSJeff Kirsher 	return __show_chan_per_port(dev, attr, buf, 1);
9447e689cf4aSJeff Kirsher }
9448e689cf4aSJeff Kirsher 
show_num_ports(struct device * dev,struct device_attribute * attr,char * buf)9449e689cf4aSJeff Kirsher static ssize_t show_num_ports(struct device *dev,
9450e689cf4aSJeff Kirsher 			      struct device_attribute *attr, char *buf)
9451e689cf4aSJeff Kirsher {
9452e689cf4aSJeff Kirsher 	struct platform_device *plat_dev = to_platform_device(dev);
9453df3f1c39SJingoo Han 	struct niu_parent *p = dev_get_platdata(&plat_dev->dev);
9454e689cf4aSJeff Kirsher 
9455e689cf4aSJeff Kirsher 	return sprintf(buf, "%d\n", p->num_ports);
9456e689cf4aSJeff Kirsher }
9457e689cf4aSJeff Kirsher 
9458e689cf4aSJeff Kirsher static struct device_attribute niu_parent_attributes[] = {
9459d3757ba4SJoe Perches 	__ATTR(port_phy, 0444, show_port_phy, NULL),
9460d3757ba4SJoe Perches 	__ATTR(plat_type, 0444, show_plat_type, NULL),
9461d3757ba4SJoe Perches 	__ATTR(rxchan_per_port, 0444, show_rxchan_per_port, NULL),
9462d3757ba4SJoe Perches 	__ATTR(txchan_per_port, 0444, show_txchan_per_port, NULL),
9463d3757ba4SJoe Perches 	__ATTR(num_ports, 0444, show_num_ports, NULL),
9464e689cf4aSJeff Kirsher 	{}
9465e689cf4aSJeff Kirsher };
9466e689cf4aSJeff Kirsher 
niu_new_parent(struct niu * np,union niu_parent_id * id,u8 ptype)9467f73d12bdSBill Pemberton static struct niu_parent *niu_new_parent(struct niu *np,
94681dd06ae8SGreg Kroah-Hartman 					 union niu_parent_id *id, u8 ptype)
9469e689cf4aSJeff Kirsher {
9470e689cf4aSJeff Kirsher 	struct platform_device *plat_dev;
9471e689cf4aSJeff Kirsher 	struct niu_parent *p;
9472e689cf4aSJeff Kirsher 	int i;
9473e689cf4aSJeff Kirsher 
9474e689cf4aSJeff Kirsher 	plat_dev = platform_device_register_simple("niu-board", niu_parent_index,
9475e689cf4aSJeff Kirsher 						   NULL, 0);
9476e689cf4aSJeff Kirsher 	if (IS_ERR(plat_dev))
9477e689cf4aSJeff Kirsher 		return NULL;
9478e689cf4aSJeff Kirsher 
94793e1026b3SGreg Kroah-Hartman 	for (i = 0; niu_parent_attributes[i].attr.name; i++) {
9480e689cf4aSJeff Kirsher 		int err = device_create_file(&plat_dev->dev,
9481e689cf4aSJeff Kirsher 					     &niu_parent_attributes[i]);
9482e689cf4aSJeff Kirsher 		if (err)
9483e689cf4aSJeff Kirsher 			goto fail_unregister;
9484e689cf4aSJeff Kirsher 	}
9485e689cf4aSJeff Kirsher 
9486e689cf4aSJeff Kirsher 	p = kzalloc(sizeof(*p), GFP_KERNEL);
9487e689cf4aSJeff Kirsher 	if (!p)
9488e689cf4aSJeff Kirsher 		goto fail_unregister;
9489e689cf4aSJeff Kirsher 
9490e689cf4aSJeff Kirsher 	p->index = niu_parent_index++;
9491e689cf4aSJeff Kirsher 
9492e689cf4aSJeff Kirsher 	plat_dev->dev.platform_data = p;
9493e689cf4aSJeff Kirsher 	p->plat_dev = plat_dev;
9494e689cf4aSJeff Kirsher 
9495e689cf4aSJeff Kirsher 	memcpy(&p->id, id, sizeof(*id));
9496e689cf4aSJeff Kirsher 	p->plat_type = ptype;
9497e689cf4aSJeff Kirsher 	INIT_LIST_HEAD(&p->list);
9498e689cf4aSJeff Kirsher 	atomic_set(&p->refcnt, 0);
9499e689cf4aSJeff Kirsher 	list_add(&p->list, &niu_parent_list);
9500e689cf4aSJeff Kirsher 	spin_lock_init(&p->lock);
9501e689cf4aSJeff Kirsher 
9502e689cf4aSJeff Kirsher 	p->rxdma_clock_divider = 7500;
9503e689cf4aSJeff Kirsher 
9504e689cf4aSJeff Kirsher 	p->tcam_num_entries = NIU_PCI_TCAM_ENTRIES;
9505e689cf4aSJeff Kirsher 	if (p->plat_type == PLAT_TYPE_NIU)
9506e689cf4aSJeff Kirsher 		p->tcam_num_entries = NIU_NONPCI_TCAM_ENTRIES;
9507e689cf4aSJeff Kirsher 
9508e689cf4aSJeff Kirsher 	for (i = CLASS_CODE_USER_PROG1; i <= CLASS_CODE_SCTP_IPV6; i++) {
9509e689cf4aSJeff Kirsher 		int index = i - CLASS_CODE_USER_PROG1;
9510e689cf4aSJeff Kirsher 
9511e689cf4aSJeff Kirsher 		p->tcam_key[index] = TCAM_KEY_TSEL;
9512e689cf4aSJeff Kirsher 		p->flow_key[index] = (FLOW_KEY_IPSA |
9513e689cf4aSJeff Kirsher 				      FLOW_KEY_IPDA |
9514e689cf4aSJeff Kirsher 				      FLOW_KEY_PROTO |
9515e689cf4aSJeff Kirsher 				      (FLOW_KEY_L4_BYTE12 <<
9516e689cf4aSJeff Kirsher 				       FLOW_KEY_L4_0_SHIFT) |
9517e689cf4aSJeff Kirsher 				      (FLOW_KEY_L4_BYTE12 <<
9518e689cf4aSJeff Kirsher 				       FLOW_KEY_L4_1_SHIFT));
9519e689cf4aSJeff Kirsher 	}
9520e689cf4aSJeff Kirsher 
9521e689cf4aSJeff Kirsher 	for (i = 0; i < LDN_MAX + 1; i++)
9522e689cf4aSJeff Kirsher 		p->ldg_map[i] = LDG_INVALID;
9523e689cf4aSJeff Kirsher 
9524e689cf4aSJeff Kirsher 	return p;
9525e689cf4aSJeff Kirsher 
9526e689cf4aSJeff Kirsher fail_unregister:
9527e689cf4aSJeff Kirsher 	platform_device_unregister(plat_dev);
9528e689cf4aSJeff Kirsher 	return NULL;
9529e689cf4aSJeff Kirsher }
9530e689cf4aSJeff Kirsher 
niu_get_parent(struct niu * np,union niu_parent_id * id,u8 ptype)9531f73d12bdSBill Pemberton static struct niu_parent *niu_get_parent(struct niu *np,
95321dd06ae8SGreg Kroah-Hartman 					 union niu_parent_id *id, u8 ptype)
9533e689cf4aSJeff Kirsher {
9534e689cf4aSJeff Kirsher 	struct niu_parent *p, *tmp;
9535e689cf4aSJeff Kirsher 	int port = np->port;
9536e689cf4aSJeff Kirsher 
9537e689cf4aSJeff Kirsher 	mutex_lock(&niu_parent_lock);
9538e689cf4aSJeff Kirsher 	p = NULL;
9539e689cf4aSJeff Kirsher 	list_for_each_entry(tmp, &niu_parent_list, list) {
9540e689cf4aSJeff Kirsher 		if (!memcmp(id, &tmp->id, sizeof(*id))) {
9541e689cf4aSJeff Kirsher 			p = tmp;
9542e689cf4aSJeff Kirsher 			break;
9543e689cf4aSJeff Kirsher 		}
9544e689cf4aSJeff Kirsher 	}
9545e689cf4aSJeff Kirsher 	if (!p)
9546e689cf4aSJeff Kirsher 		p = niu_new_parent(np, id, ptype);
9547e689cf4aSJeff Kirsher 
9548e689cf4aSJeff Kirsher 	if (p) {
954973066f6cSArnd Bergmann 		char port_name[8];
9550e689cf4aSJeff Kirsher 		int err;
9551e689cf4aSJeff Kirsher 
9552e689cf4aSJeff Kirsher 		sprintf(port_name, "port%d", port);
9553e689cf4aSJeff Kirsher 		err = sysfs_create_link(&p->plat_dev->dev.kobj,
9554e689cf4aSJeff Kirsher 					&np->device->kobj,
9555e689cf4aSJeff Kirsher 					port_name);
9556e689cf4aSJeff Kirsher 		if (!err) {
9557e689cf4aSJeff Kirsher 			p->ports[port] = np;
9558e689cf4aSJeff Kirsher 			atomic_inc(&p->refcnt);
9559e689cf4aSJeff Kirsher 		}
9560e689cf4aSJeff Kirsher 	}
9561e689cf4aSJeff Kirsher 	mutex_unlock(&niu_parent_lock);
9562e689cf4aSJeff Kirsher 
9563e689cf4aSJeff Kirsher 	return p;
9564e689cf4aSJeff Kirsher }
9565e689cf4aSJeff Kirsher 
niu_put_parent(struct niu * np)9566e689cf4aSJeff Kirsher static void niu_put_parent(struct niu *np)
9567e689cf4aSJeff Kirsher {
9568e689cf4aSJeff Kirsher 	struct niu_parent *p = np->parent;
9569e689cf4aSJeff Kirsher 	u8 port = np->port;
957073066f6cSArnd Bergmann 	char port_name[8];
9571e689cf4aSJeff Kirsher 
9572e689cf4aSJeff Kirsher 	BUG_ON(!p || p->ports[port] != np);
9573e689cf4aSJeff Kirsher 
9574e689cf4aSJeff Kirsher 	netif_printk(np, probe, KERN_DEBUG, np->dev,
9575e689cf4aSJeff Kirsher 		     "%s() port[%u]\n", __func__, port);
9576e689cf4aSJeff Kirsher 
9577e689cf4aSJeff Kirsher 	sprintf(port_name, "port%d", port);
9578e689cf4aSJeff Kirsher 
9579e689cf4aSJeff Kirsher 	mutex_lock(&niu_parent_lock);
9580e689cf4aSJeff Kirsher 
9581e689cf4aSJeff Kirsher 	sysfs_remove_link(&p->plat_dev->dev.kobj, port_name);
9582e689cf4aSJeff Kirsher 
9583e689cf4aSJeff Kirsher 	p->ports[port] = NULL;
9584e689cf4aSJeff Kirsher 	np->parent = NULL;
9585e689cf4aSJeff Kirsher 
9586e689cf4aSJeff Kirsher 	if (atomic_dec_and_test(&p->refcnt)) {
9587e689cf4aSJeff Kirsher 		list_del(&p->list);
9588e689cf4aSJeff Kirsher 		platform_device_unregister(p->plat_dev);
9589e689cf4aSJeff Kirsher 	}
9590e689cf4aSJeff Kirsher 
9591e689cf4aSJeff Kirsher 	mutex_unlock(&niu_parent_lock);
9592e689cf4aSJeff Kirsher }
9593e689cf4aSJeff Kirsher 
niu_pci_alloc_coherent(struct device * dev,size_t size,u64 * handle,gfp_t flag)9594e689cf4aSJeff Kirsher static void *niu_pci_alloc_coherent(struct device *dev, size_t size,
9595e689cf4aSJeff Kirsher 				    u64 *handle, gfp_t flag)
9596e689cf4aSJeff Kirsher {
9597e689cf4aSJeff Kirsher 	dma_addr_t dh;
9598e689cf4aSJeff Kirsher 	void *ret;
9599e689cf4aSJeff Kirsher 
9600e689cf4aSJeff Kirsher 	ret = dma_alloc_coherent(dev, size, &dh, flag);
9601e689cf4aSJeff Kirsher 	if (ret)
9602e689cf4aSJeff Kirsher 		*handle = dh;
9603e689cf4aSJeff Kirsher 	return ret;
9604e689cf4aSJeff Kirsher }
9605e689cf4aSJeff Kirsher 
niu_pci_free_coherent(struct device * dev,size_t size,void * cpu_addr,u64 handle)9606e689cf4aSJeff Kirsher static void niu_pci_free_coherent(struct device *dev, size_t size,
9607e689cf4aSJeff Kirsher 				  void *cpu_addr, u64 handle)
9608e689cf4aSJeff Kirsher {
9609e689cf4aSJeff Kirsher 	dma_free_coherent(dev, size, cpu_addr, handle);
9610e689cf4aSJeff Kirsher }
9611e689cf4aSJeff Kirsher 
niu_pci_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction direction)9612e689cf4aSJeff Kirsher static u64 niu_pci_map_page(struct device *dev, struct page *page,
9613e689cf4aSJeff Kirsher 			    unsigned long offset, size_t size,
9614e689cf4aSJeff Kirsher 			    enum dma_data_direction direction)
9615e689cf4aSJeff Kirsher {
9616e689cf4aSJeff Kirsher 	return dma_map_page(dev, page, offset, size, direction);
9617e689cf4aSJeff Kirsher }
9618e689cf4aSJeff Kirsher 
niu_pci_unmap_page(struct device * dev,u64 dma_address,size_t size,enum dma_data_direction direction)9619e689cf4aSJeff Kirsher static void niu_pci_unmap_page(struct device *dev, u64 dma_address,
9620e689cf4aSJeff Kirsher 			       size_t size, enum dma_data_direction direction)
9621e689cf4aSJeff Kirsher {
9622e689cf4aSJeff Kirsher 	dma_unmap_page(dev, dma_address, size, direction);
9623e689cf4aSJeff Kirsher }
9624e689cf4aSJeff Kirsher 
niu_pci_map_single(struct device * dev,void * cpu_addr,size_t size,enum dma_data_direction direction)9625e689cf4aSJeff Kirsher static u64 niu_pci_map_single(struct device *dev, void *cpu_addr,
9626e689cf4aSJeff Kirsher 			      size_t size,
9627e689cf4aSJeff Kirsher 			      enum dma_data_direction direction)
9628e689cf4aSJeff Kirsher {
9629e689cf4aSJeff Kirsher 	return dma_map_single(dev, cpu_addr, size, direction);
9630e689cf4aSJeff Kirsher }
9631e689cf4aSJeff Kirsher 
niu_pci_unmap_single(struct device * dev,u64 dma_address,size_t size,enum dma_data_direction direction)9632e689cf4aSJeff Kirsher static void niu_pci_unmap_single(struct device *dev, u64 dma_address,
9633e689cf4aSJeff Kirsher 				 size_t size,
9634e689cf4aSJeff Kirsher 				 enum dma_data_direction direction)
9635e689cf4aSJeff Kirsher {
9636e689cf4aSJeff Kirsher 	dma_unmap_single(dev, dma_address, size, direction);
9637e689cf4aSJeff Kirsher }
9638e689cf4aSJeff Kirsher 
9639e689cf4aSJeff Kirsher static const struct niu_ops niu_pci_ops = {
9640e689cf4aSJeff Kirsher 	.alloc_coherent	= niu_pci_alloc_coherent,
9641e689cf4aSJeff Kirsher 	.free_coherent	= niu_pci_free_coherent,
9642e689cf4aSJeff Kirsher 	.map_page	= niu_pci_map_page,
9643e689cf4aSJeff Kirsher 	.unmap_page	= niu_pci_unmap_page,
9644e689cf4aSJeff Kirsher 	.map_single	= niu_pci_map_single,
9645e689cf4aSJeff Kirsher 	.unmap_single	= niu_pci_unmap_single,
9646e689cf4aSJeff Kirsher };
9647e689cf4aSJeff Kirsher 
niu_driver_version(void)9648f73d12bdSBill Pemberton static void niu_driver_version(void)
9649e689cf4aSJeff Kirsher {
9650e689cf4aSJeff Kirsher 	static int niu_version_printed;
9651e689cf4aSJeff Kirsher 
9652e689cf4aSJeff Kirsher 	if (niu_version_printed++ == 0)
9653e689cf4aSJeff Kirsher 		pr_info("%s", version);
9654e689cf4aSJeff Kirsher }
9655e689cf4aSJeff Kirsher 
niu_alloc_and_init(struct device * gen_dev,struct pci_dev * pdev,struct platform_device * op,const struct niu_ops * ops,u8 port)96561dd06ae8SGreg Kroah-Hartman static struct net_device *niu_alloc_and_init(struct device *gen_dev,
96571dd06ae8SGreg Kroah-Hartman 					     struct pci_dev *pdev,
96581dd06ae8SGreg Kroah-Hartman 					     struct platform_device *op,
96591dd06ae8SGreg Kroah-Hartman 					     const struct niu_ops *ops, u8 port)
9660e689cf4aSJeff Kirsher {
9661e689cf4aSJeff Kirsher 	struct net_device *dev;
9662e689cf4aSJeff Kirsher 	struct niu *np;
9663e689cf4aSJeff Kirsher 
9664e689cf4aSJeff Kirsher 	dev = alloc_etherdev_mq(sizeof(struct niu), NIU_NUM_TXCHAN);
966541de8d4cSJoe Perches 	if (!dev)
9666e689cf4aSJeff Kirsher 		return NULL;
9667e689cf4aSJeff Kirsher 
9668e689cf4aSJeff Kirsher 	SET_NETDEV_DEV(dev, gen_dev);
9669e689cf4aSJeff Kirsher 
9670e689cf4aSJeff Kirsher 	np = netdev_priv(dev);
9671e689cf4aSJeff Kirsher 	np->dev = dev;
9672e689cf4aSJeff Kirsher 	np->pdev = pdev;
9673e689cf4aSJeff Kirsher 	np->op = op;
9674e689cf4aSJeff Kirsher 	np->device = gen_dev;
9675e689cf4aSJeff Kirsher 	np->ops = ops;
9676e689cf4aSJeff Kirsher 
9677e689cf4aSJeff Kirsher 	np->msg_enable = niu_debug;
9678e689cf4aSJeff Kirsher 
9679e689cf4aSJeff Kirsher 	spin_lock_init(&np->lock);
9680e689cf4aSJeff Kirsher 	INIT_WORK(&np->reset_task, niu_reset_task);
9681e689cf4aSJeff Kirsher 
9682e689cf4aSJeff Kirsher 	np->port = port;
9683e689cf4aSJeff Kirsher 
9684e689cf4aSJeff Kirsher 	return dev;
9685e689cf4aSJeff Kirsher }
9686e689cf4aSJeff Kirsher 
9687e689cf4aSJeff Kirsher static const struct net_device_ops niu_netdev_ops = {
9688e689cf4aSJeff Kirsher 	.ndo_open		= niu_open,
9689e689cf4aSJeff Kirsher 	.ndo_stop		= niu_close,
9690e689cf4aSJeff Kirsher 	.ndo_start_xmit		= niu_start_xmit,
9691e689cf4aSJeff Kirsher 	.ndo_get_stats64	= niu_get_stats,
969201789349SJiri Pirko 	.ndo_set_rx_mode	= niu_set_rx_mode,
9693e689cf4aSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
9694e689cf4aSJeff Kirsher 	.ndo_set_mac_address	= niu_set_mac_addr,
9695a7605370SArnd Bergmann 	.ndo_eth_ioctl		= niu_ioctl,
9696e689cf4aSJeff Kirsher 	.ndo_tx_timeout		= niu_tx_timeout,
9697e689cf4aSJeff Kirsher 	.ndo_change_mtu		= niu_change_mtu,
9698e689cf4aSJeff Kirsher };
9699e689cf4aSJeff Kirsher 
niu_assign_netdev_ops(struct net_device * dev)9700f73d12bdSBill Pemberton static void niu_assign_netdev_ops(struct net_device *dev)
9701e689cf4aSJeff Kirsher {
9702e689cf4aSJeff Kirsher 	dev->netdev_ops = &niu_netdev_ops;
9703e689cf4aSJeff Kirsher 	dev->ethtool_ops = &niu_ethtool_ops;
9704e689cf4aSJeff Kirsher 	dev->watchdog_timeo = NIU_TX_TIMEOUT;
9705e689cf4aSJeff Kirsher }
9706e689cf4aSJeff Kirsher 
niu_device_announce(struct niu * np)9707f73d12bdSBill Pemberton static void niu_device_announce(struct niu *np)
9708e689cf4aSJeff Kirsher {
9709e689cf4aSJeff Kirsher 	struct net_device *dev = np->dev;
9710e689cf4aSJeff Kirsher 
9711e689cf4aSJeff Kirsher 	pr_info("%s: NIU Ethernet %pM\n", dev->name, dev->dev_addr);
9712e689cf4aSJeff Kirsher 
9713e689cf4aSJeff Kirsher 	if (np->parent->plat_type == PLAT_TYPE_ATCA_CP3220) {
9714e689cf4aSJeff Kirsher 		pr_info("%s: Port type[%s] mode[%s:%s] XCVR[%s] phy[%s]\n",
9715e689cf4aSJeff Kirsher 				dev->name,
9716e689cf4aSJeff Kirsher 				(np->flags & NIU_FLAGS_XMAC ? "XMAC" : "BMAC"),
9717e689cf4aSJeff Kirsher 				(np->flags & NIU_FLAGS_10G ? "10G" : "1G"),
9718e689cf4aSJeff Kirsher 				(np->flags & NIU_FLAGS_FIBER ? "RGMII FIBER" : "SERDES"),
9719e689cf4aSJeff Kirsher 				(np->mac_xcvr == MAC_XCVR_MII ? "MII" :
9720e689cf4aSJeff Kirsher 				 (np->mac_xcvr == MAC_XCVR_PCS ? "PCS" : "XPCS")),
9721e689cf4aSJeff Kirsher 				np->vpd.phy_type);
9722e689cf4aSJeff Kirsher 	} else {
9723e689cf4aSJeff Kirsher 		pr_info("%s: Port type[%s] mode[%s:%s] XCVR[%s] phy[%s]\n",
9724e689cf4aSJeff Kirsher 				dev->name,
9725e689cf4aSJeff Kirsher 				(np->flags & NIU_FLAGS_XMAC ? "XMAC" : "BMAC"),
9726e689cf4aSJeff Kirsher 				(np->flags & NIU_FLAGS_10G ? "10G" : "1G"),
9727e689cf4aSJeff Kirsher 				(np->flags & NIU_FLAGS_FIBER ? "FIBER" :
9728e689cf4aSJeff Kirsher 				 (np->flags & NIU_FLAGS_XCVR_SERDES ? "SERDES" :
9729e689cf4aSJeff Kirsher 				  "COPPER")),
9730e689cf4aSJeff Kirsher 				(np->mac_xcvr == MAC_XCVR_MII ? "MII" :
9731e689cf4aSJeff Kirsher 				 (np->mac_xcvr == MAC_XCVR_PCS ? "PCS" : "XPCS")),
9732e689cf4aSJeff Kirsher 				np->vpd.phy_type);
9733e689cf4aSJeff Kirsher 	}
9734e689cf4aSJeff Kirsher }
9735e689cf4aSJeff Kirsher 
niu_set_basic_features(struct net_device * dev)9736f73d12bdSBill Pemberton static void niu_set_basic_features(struct net_device *dev)
9737e689cf4aSJeff Kirsher {
9738e689cf4aSJeff Kirsher 	dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXHASH;
9739e689cf4aSJeff Kirsher 	dev->features |= dev->hw_features | NETIF_F_RXCSUM;
9740e689cf4aSJeff Kirsher }
9741e689cf4aSJeff Kirsher 
niu_pci_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)9742f73d12bdSBill Pemberton static int niu_pci_init_one(struct pci_dev *pdev,
9743e689cf4aSJeff Kirsher 			    const struct pci_device_id *ent)
9744e689cf4aSJeff Kirsher {
9745e689cf4aSJeff Kirsher 	union niu_parent_id parent_id;
9746e689cf4aSJeff Kirsher 	struct net_device *dev;
9747e689cf4aSJeff Kirsher 	struct niu *np;
974856cda129SJiang Liu 	int err;
9749e689cf4aSJeff Kirsher 
9750e689cf4aSJeff Kirsher 	niu_driver_version();
9751e689cf4aSJeff Kirsher 
9752e689cf4aSJeff Kirsher 	err = pci_enable_device(pdev);
9753e689cf4aSJeff Kirsher 	if (err) {
9754e689cf4aSJeff Kirsher 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
9755e689cf4aSJeff Kirsher 		return err;
9756e689cf4aSJeff Kirsher 	}
9757e689cf4aSJeff Kirsher 
9758e689cf4aSJeff Kirsher 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
9759e689cf4aSJeff Kirsher 	    !(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
9760e689cf4aSJeff Kirsher 		dev_err(&pdev->dev, "Cannot find proper PCI device base addresses, aborting\n");
9761e689cf4aSJeff Kirsher 		err = -ENODEV;
9762e689cf4aSJeff Kirsher 		goto err_out_disable_pdev;
9763e689cf4aSJeff Kirsher 	}
9764e689cf4aSJeff Kirsher 
9765e689cf4aSJeff Kirsher 	err = pci_request_regions(pdev, DRV_MODULE_NAME);
9766e689cf4aSJeff Kirsher 	if (err) {
9767e689cf4aSJeff Kirsher 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
9768e689cf4aSJeff Kirsher 		goto err_out_disable_pdev;
9769e689cf4aSJeff Kirsher 	}
9770e689cf4aSJeff Kirsher 
977156cda129SJiang Liu 	if (!pci_is_pcie(pdev)) {
9772e689cf4aSJeff Kirsher 		dev_err(&pdev->dev, "Cannot find PCI Express capability, aborting\n");
97738c65ef4bSPeter Senna Tschudin 		err = -ENODEV;
9774e689cf4aSJeff Kirsher 		goto err_out_free_res;
9775e689cf4aSJeff Kirsher 	}
9776e689cf4aSJeff Kirsher 
9777e689cf4aSJeff Kirsher 	dev = niu_alloc_and_init(&pdev->dev, pdev, NULL,
9778e689cf4aSJeff Kirsher 				 &niu_pci_ops, PCI_FUNC(pdev->devfn));
9779e689cf4aSJeff Kirsher 	if (!dev) {
9780e689cf4aSJeff Kirsher 		err = -ENOMEM;
9781e689cf4aSJeff Kirsher 		goto err_out_free_res;
9782e689cf4aSJeff Kirsher 	}
9783e689cf4aSJeff Kirsher 	np = netdev_priv(dev);
9784e689cf4aSJeff Kirsher 
9785e689cf4aSJeff Kirsher 	memset(&parent_id, 0, sizeof(parent_id));
9786e689cf4aSJeff Kirsher 	parent_id.pci.domain = pci_domain_nr(pdev->bus);
9787e689cf4aSJeff Kirsher 	parent_id.pci.bus = pdev->bus->number;
9788e689cf4aSJeff Kirsher 	parent_id.pci.device = PCI_SLOT(pdev->devfn);
9789e689cf4aSJeff Kirsher 
9790e689cf4aSJeff Kirsher 	np->parent = niu_get_parent(np, &parent_id,
9791e689cf4aSJeff Kirsher 				    PLAT_TYPE_ATLAS);
9792e689cf4aSJeff Kirsher 	if (!np->parent) {
9793e689cf4aSJeff Kirsher 		err = -ENOMEM;
9794e689cf4aSJeff Kirsher 		goto err_out_free_dev;
9795e689cf4aSJeff Kirsher 	}
9796e689cf4aSJeff Kirsher 
979756cda129SJiang Liu 	pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
979856cda129SJiang Liu 		PCI_EXP_DEVCTL_NOSNOOP_EN,
979956cda129SJiang Liu 		PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
980056cda129SJiang Liu 		PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE |
9801e689cf4aSJeff Kirsher 		PCI_EXP_DEVCTL_RELAX_EN);
9802e689cf4aSJeff Kirsher 
98039b0df250SChristophe JAILLET 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
98049b0df250SChristophe JAILLET 	if (!err)
9805e689cf4aSJeff Kirsher 		dev->features |= NETIF_F_HIGHDMA;
9806e689cf4aSJeff Kirsher 	if (err) {
98079b0df250SChristophe JAILLET 		err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
9808e689cf4aSJeff Kirsher 		if (err) {
9809e689cf4aSJeff Kirsher 			dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
9810e689cf4aSJeff Kirsher 			goto err_out_release_parent;
9811e689cf4aSJeff Kirsher 		}
9812e689cf4aSJeff Kirsher 	}
9813e689cf4aSJeff Kirsher 
9814e689cf4aSJeff Kirsher 	niu_set_basic_features(dev);
9815e689cf4aSJeff Kirsher 
981601789349SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT;
981701789349SJiri Pirko 
9818e689cf4aSJeff Kirsher 	np->regs = pci_ioremap_bar(pdev, 0);
9819e689cf4aSJeff Kirsher 	if (!np->regs) {
9820e689cf4aSJeff Kirsher 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
9821e689cf4aSJeff Kirsher 		err = -ENOMEM;
9822e689cf4aSJeff Kirsher 		goto err_out_release_parent;
9823e689cf4aSJeff Kirsher 	}
9824e689cf4aSJeff Kirsher 
9825e689cf4aSJeff Kirsher 	pci_set_master(pdev);
9826e689cf4aSJeff Kirsher 	pci_save_state(pdev);
9827e689cf4aSJeff Kirsher 
9828e689cf4aSJeff Kirsher 	dev->irq = pdev->irq;
9829e689cf4aSJeff Kirsher 
9830540bfe30SJarod Wilson 	/* MTU range: 68 - 9216 */
9831540bfe30SJarod Wilson 	dev->min_mtu = ETH_MIN_MTU;
9832540bfe30SJarod Wilson 	dev->max_mtu = NIU_MAX_MTU;
9833540bfe30SJarod Wilson 
9834e689cf4aSJeff Kirsher 	niu_assign_netdev_ops(dev);
9835e689cf4aSJeff Kirsher 
9836e689cf4aSJeff Kirsher 	err = niu_get_invariants(np);
9837e689cf4aSJeff Kirsher 	if (err) {
9838e689cf4aSJeff Kirsher 		if (err != -ENODEV)
9839e689cf4aSJeff Kirsher 			dev_err(&pdev->dev, "Problem fetching invariants of chip, aborting\n");
9840e689cf4aSJeff Kirsher 		goto err_out_iounmap;
9841e689cf4aSJeff Kirsher 	}
9842e689cf4aSJeff Kirsher 
9843e689cf4aSJeff Kirsher 	err = register_netdev(dev);
9844e689cf4aSJeff Kirsher 	if (err) {
9845e689cf4aSJeff Kirsher 		dev_err(&pdev->dev, "Cannot register net device, aborting\n");
9846e689cf4aSJeff Kirsher 		goto err_out_iounmap;
9847e689cf4aSJeff Kirsher 	}
9848e689cf4aSJeff Kirsher 
9849e689cf4aSJeff Kirsher 	pci_set_drvdata(pdev, dev);
9850e689cf4aSJeff Kirsher 
9851e689cf4aSJeff Kirsher 	niu_device_announce(np);
9852e689cf4aSJeff Kirsher 
9853e689cf4aSJeff Kirsher 	return 0;
9854e689cf4aSJeff Kirsher 
9855e689cf4aSJeff Kirsher err_out_iounmap:
9856e689cf4aSJeff Kirsher 	if (np->regs) {
9857e689cf4aSJeff Kirsher 		iounmap(np->regs);
9858e689cf4aSJeff Kirsher 		np->regs = NULL;
9859e689cf4aSJeff Kirsher 	}
9860e689cf4aSJeff Kirsher 
9861e689cf4aSJeff Kirsher err_out_release_parent:
9862e689cf4aSJeff Kirsher 	niu_put_parent(np);
9863e689cf4aSJeff Kirsher 
9864e689cf4aSJeff Kirsher err_out_free_dev:
9865e689cf4aSJeff Kirsher 	free_netdev(dev);
9866e689cf4aSJeff Kirsher 
9867e689cf4aSJeff Kirsher err_out_free_res:
9868e689cf4aSJeff Kirsher 	pci_release_regions(pdev);
9869e689cf4aSJeff Kirsher 
9870e689cf4aSJeff Kirsher err_out_disable_pdev:
9871e689cf4aSJeff Kirsher 	pci_disable_device(pdev);
9872e689cf4aSJeff Kirsher 
9873e689cf4aSJeff Kirsher 	return err;
9874e689cf4aSJeff Kirsher }
9875e689cf4aSJeff Kirsher 
niu_pci_remove_one(struct pci_dev * pdev)9876f73d12bdSBill Pemberton static void niu_pci_remove_one(struct pci_dev *pdev)
9877e689cf4aSJeff Kirsher {
9878e689cf4aSJeff Kirsher 	struct net_device *dev = pci_get_drvdata(pdev);
9879e689cf4aSJeff Kirsher 
9880e689cf4aSJeff Kirsher 	if (dev) {
9881e689cf4aSJeff Kirsher 		struct niu *np = netdev_priv(dev);
9882e689cf4aSJeff Kirsher 
9883e689cf4aSJeff Kirsher 		unregister_netdev(dev);
9884e689cf4aSJeff Kirsher 		if (np->regs) {
9885e689cf4aSJeff Kirsher 			iounmap(np->regs);
9886e689cf4aSJeff Kirsher 			np->regs = NULL;
9887e689cf4aSJeff Kirsher 		}
9888e689cf4aSJeff Kirsher 
9889e689cf4aSJeff Kirsher 		niu_ldg_free(np);
9890e689cf4aSJeff Kirsher 
9891e689cf4aSJeff Kirsher 		niu_put_parent(np);
9892e689cf4aSJeff Kirsher 
9893e689cf4aSJeff Kirsher 		free_netdev(dev);
9894e689cf4aSJeff Kirsher 		pci_release_regions(pdev);
9895e689cf4aSJeff Kirsher 		pci_disable_device(pdev);
9896e689cf4aSJeff Kirsher 	}
9897e689cf4aSJeff Kirsher }
9898e689cf4aSJeff Kirsher 
niu_suspend(struct device * dev_d)989986fc3f70SVaibhav Gupta static int __maybe_unused niu_suspend(struct device *dev_d)
9900e689cf4aSJeff Kirsher {
9901b0db0cc2SVaibhav Gupta 	struct net_device *dev = dev_get_drvdata(dev_d);
9902e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
9903e689cf4aSJeff Kirsher 	unsigned long flags;
9904e689cf4aSJeff Kirsher 
9905e689cf4aSJeff Kirsher 	if (!netif_running(dev))
9906e689cf4aSJeff Kirsher 		return 0;
9907e689cf4aSJeff Kirsher 
990843829731STejun Heo 	flush_work(&np->reset_task);
9909e689cf4aSJeff Kirsher 	niu_netif_stop(np);
9910e689cf4aSJeff Kirsher 
9911e689cf4aSJeff Kirsher 	del_timer_sync(&np->timer);
9912e689cf4aSJeff Kirsher 
9913e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
9914e689cf4aSJeff Kirsher 	niu_enable_interrupts(np, 0);
9915e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
9916e689cf4aSJeff Kirsher 
9917e689cf4aSJeff Kirsher 	netif_device_detach(dev);
9918e689cf4aSJeff Kirsher 
9919e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
9920e689cf4aSJeff Kirsher 	niu_stop_hw(np);
9921e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
9922e689cf4aSJeff Kirsher 
9923e689cf4aSJeff Kirsher 	return 0;
9924e689cf4aSJeff Kirsher }
9925e689cf4aSJeff Kirsher 
niu_resume(struct device * dev_d)992686fc3f70SVaibhav Gupta static int __maybe_unused niu_resume(struct device *dev_d)
9927e689cf4aSJeff Kirsher {
9928b0db0cc2SVaibhav Gupta 	struct net_device *dev = dev_get_drvdata(dev_d);
9929e689cf4aSJeff Kirsher 	struct niu *np = netdev_priv(dev);
9930e689cf4aSJeff Kirsher 	unsigned long flags;
9931e689cf4aSJeff Kirsher 	int err;
9932e689cf4aSJeff Kirsher 
9933e689cf4aSJeff Kirsher 	if (!netif_running(dev))
9934e689cf4aSJeff Kirsher 		return 0;
9935e689cf4aSJeff Kirsher 
9936e689cf4aSJeff Kirsher 	netif_device_attach(dev);
9937e689cf4aSJeff Kirsher 
9938e689cf4aSJeff Kirsher 	spin_lock_irqsave(&np->lock, flags);
9939e689cf4aSJeff Kirsher 
9940e689cf4aSJeff Kirsher 	err = niu_init_hw(np);
9941e689cf4aSJeff Kirsher 	if (!err) {
9942e689cf4aSJeff Kirsher 		np->timer.expires = jiffies + HZ;
9943e689cf4aSJeff Kirsher 		add_timer(&np->timer);
9944e689cf4aSJeff Kirsher 		niu_netif_start(np);
9945e689cf4aSJeff Kirsher 	}
9946e689cf4aSJeff Kirsher 
9947e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&np->lock, flags);
9948e689cf4aSJeff Kirsher 
9949e689cf4aSJeff Kirsher 	return err;
9950e689cf4aSJeff Kirsher }
9951e689cf4aSJeff Kirsher 
9952b0db0cc2SVaibhav Gupta static SIMPLE_DEV_PM_OPS(niu_pm_ops, niu_suspend, niu_resume);
9953b0db0cc2SVaibhav Gupta 
9954e689cf4aSJeff Kirsher static struct pci_driver niu_pci_driver = {
9955e689cf4aSJeff Kirsher 	.name		= DRV_MODULE_NAME,
9956e689cf4aSJeff Kirsher 	.id_table	= niu_pci_tbl,
9957e689cf4aSJeff Kirsher 	.probe		= niu_pci_init_one,
9958f73d12bdSBill Pemberton 	.remove		= niu_pci_remove_one,
9959b0db0cc2SVaibhav Gupta 	.driver.pm	= &niu_pm_ops,
9960e689cf4aSJeff Kirsher };
9961e689cf4aSJeff Kirsher 
9962e689cf4aSJeff Kirsher #ifdef CONFIG_SPARC64
niu_phys_alloc_coherent(struct device * dev,size_t size,u64 * dma_addr,gfp_t flag)9963e689cf4aSJeff Kirsher static void *niu_phys_alloc_coherent(struct device *dev, size_t size,
9964e689cf4aSJeff Kirsher 				     u64 *dma_addr, gfp_t flag)
9965e689cf4aSJeff Kirsher {
9966e689cf4aSJeff Kirsher 	unsigned long order = get_order(size);
9967e689cf4aSJeff Kirsher 	unsigned long page = __get_free_pages(flag, order);
9968e689cf4aSJeff Kirsher 
9969e689cf4aSJeff Kirsher 	if (page == 0UL)
9970e689cf4aSJeff Kirsher 		return NULL;
9971e689cf4aSJeff Kirsher 	memset((char *)page, 0, PAGE_SIZE << order);
9972e689cf4aSJeff Kirsher 	*dma_addr = __pa(page);
9973e689cf4aSJeff Kirsher 
9974e689cf4aSJeff Kirsher 	return (void *) page;
9975e689cf4aSJeff Kirsher }
9976e689cf4aSJeff Kirsher 
niu_phys_free_coherent(struct device * dev,size_t size,void * cpu_addr,u64 handle)9977e689cf4aSJeff Kirsher static void niu_phys_free_coherent(struct device *dev, size_t size,
9978e689cf4aSJeff Kirsher 				   void *cpu_addr, u64 handle)
9979e689cf4aSJeff Kirsher {
9980e689cf4aSJeff Kirsher 	unsigned long order = get_order(size);
9981e689cf4aSJeff Kirsher 
9982e689cf4aSJeff Kirsher 	free_pages((unsigned long) cpu_addr, order);
9983e689cf4aSJeff Kirsher }
9984e689cf4aSJeff Kirsher 
niu_phys_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction direction)9985e689cf4aSJeff Kirsher static u64 niu_phys_map_page(struct device *dev, struct page *page,
9986e689cf4aSJeff Kirsher 			     unsigned long offset, size_t size,
9987e689cf4aSJeff Kirsher 			     enum dma_data_direction direction)
9988e689cf4aSJeff Kirsher {
9989e689cf4aSJeff Kirsher 	return page_to_phys(page) + offset;
9990e689cf4aSJeff Kirsher }
9991e689cf4aSJeff Kirsher 
niu_phys_unmap_page(struct device * dev,u64 dma_address,size_t size,enum dma_data_direction direction)9992e689cf4aSJeff Kirsher static void niu_phys_unmap_page(struct device *dev, u64 dma_address,
9993e689cf4aSJeff Kirsher 				size_t size, enum dma_data_direction direction)
9994e689cf4aSJeff Kirsher {
9995e689cf4aSJeff Kirsher 	/* Nothing to do.  */
9996e689cf4aSJeff Kirsher }
9997e689cf4aSJeff Kirsher 
niu_phys_map_single(struct device * dev,void * cpu_addr,size_t size,enum dma_data_direction direction)9998e689cf4aSJeff Kirsher static u64 niu_phys_map_single(struct device *dev, void *cpu_addr,
9999e689cf4aSJeff Kirsher 			       size_t size,
10000e689cf4aSJeff Kirsher 			       enum dma_data_direction direction)
10001e689cf4aSJeff Kirsher {
10002e689cf4aSJeff Kirsher 	return __pa(cpu_addr);
10003e689cf4aSJeff Kirsher }
10004e689cf4aSJeff Kirsher 
niu_phys_unmap_single(struct device * dev,u64 dma_address,size_t size,enum dma_data_direction direction)10005e689cf4aSJeff Kirsher static void niu_phys_unmap_single(struct device *dev, u64 dma_address,
10006e689cf4aSJeff Kirsher 				  size_t size,
10007e689cf4aSJeff Kirsher 				  enum dma_data_direction direction)
10008e689cf4aSJeff Kirsher {
10009e689cf4aSJeff Kirsher 	/* Nothing to do.  */
10010e689cf4aSJeff Kirsher }
10011e689cf4aSJeff Kirsher 
10012e689cf4aSJeff Kirsher static const struct niu_ops niu_phys_ops = {
10013e689cf4aSJeff Kirsher 	.alloc_coherent	= niu_phys_alloc_coherent,
10014e689cf4aSJeff Kirsher 	.free_coherent	= niu_phys_free_coherent,
10015e689cf4aSJeff Kirsher 	.map_page	= niu_phys_map_page,
10016e689cf4aSJeff Kirsher 	.unmap_page	= niu_phys_unmap_page,
10017e689cf4aSJeff Kirsher 	.map_single	= niu_phys_map_single,
10018e689cf4aSJeff Kirsher 	.unmap_single	= niu_phys_unmap_single,
10019e689cf4aSJeff Kirsher };
10020e689cf4aSJeff Kirsher 
niu_of_probe(struct platform_device * op)10021f73d12bdSBill Pemberton static int niu_of_probe(struct platform_device *op)
10022e689cf4aSJeff Kirsher {
10023e689cf4aSJeff Kirsher 	union niu_parent_id parent_id;
10024e689cf4aSJeff Kirsher 	struct net_device *dev;
10025e689cf4aSJeff Kirsher 	struct niu *np;
10026e689cf4aSJeff Kirsher 	const u32 *reg;
10027e689cf4aSJeff Kirsher 	int err;
10028e689cf4aSJeff Kirsher 
10029e689cf4aSJeff Kirsher 	niu_driver_version();
10030e689cf4aSJeff Kirsher 
10031e689cf4aSJeff Kirsher 	reg = of_get_property(op->dev.of_node, "reg", NULL);
10032e689cf4aSJeff Kirsher 	if (!reg) {
10033f7ce9103SRob Herring 		dev_err(&op->dev, "%pOF: No 'reg' property, aborting\n",
10034f7ce9103SRob Herring 			op->dev.of_node);
10035e689cf4aSJeff Kirsher 		return -ENODEV;
10036e689cf4aSJeff Kirsher 	}
10037e689cf4aSJeff Kirsher 
10038e689cf4aSJeff Kirsher 	dev = niu_alloc_and_init(&op->dev, NULL, op,
10039e689cf4aSJeff Kirsher 				 &niu_phys_ops, reg[0] & 0x1);
10040e689cf4aSJeff Kirsher 	if (!dev) {
10041e689cf4aSJeff Kirsher 		err = -ENOMEM;
10042e689cf4aSJeff Kirsher 		goto err_out;
10043e689cf4aSJeff Kirsher 	}
10044e689cf4aSJeff Kirsher 	np = netdev_priv(dev);
10045e689cf4aSJeff Kirsher 
10046e689cf4aSJeff Kirsher 	memset(&parent_id, 0, sizeof(parent_id));
10047e689cf4aSJeff Kirsher 	parent_id.of = of_get_parent(op->dev.of_node);
10048e689cf4aSJeff Kirsher 
10049e689cf4aSJeff Kirsher 	np->parent = niu_get_parent(np, &parent_id,
10050e689cf4aSJeff Kirsher 				    PLAT_TYPE_NIU);
10051e689cf4aSJeff Kirsher 	if (!np->parent) {
10052e689cf4aSJeff Kirsher 		err = -ENOMEM;
10053e689cf4aSJeff Kirsher 		goto err_out_free_dev;
10054e689cf4aSJeff Kirsher 	}
10055e689cf4aSJeff Kirsher 
10056e689cf4aSJeff Kirsher 	niu_set_basic_features(dev);
10057e689cf4aSJeff Kirsher 
10058e689cf4aSJeff Kirsher 	np->regs = of_ioremap(&op->resource[1], 0,
10059e689cf4aSJeff Kirsher 			      resource_size(&op->resource[1]),
10060e689cf4aSJeff Kirsher 			      "niu regs");
10061e689cf4aSJeff Kirsher 	if (!np->regs) {
10062e689cf4aSJeff Kirsher 		dev_err(&op->dev, "Cannot map device registers, aborting\n");
10063e689cf4aSJeff Kirsher 		err = -ENOMEM;
10064e689cf4aSJeff Kirsher 		goto err_out_release_parent;
10065e689cf4aSJeff Kirsher 	}
10066e689cf4aSJeff Kirsher 
10067e689cf4aSJeff Kirsher 	np->vir_regs_1 = of_ioremap(&op->resource[2], 0,
10068e689cf4aSJeff Kirsher 				    resource_size(&op->resource[2]),
10069e689cf4aSJeff Kirsher 				    "niu vregs-1");
10070e689cf4aSJeff Kirsher 	if (!np->vir_regs_1) {
10071e689cf4aSJeff Kirsher 		dev_err(&op->dev, "Cannot map device vir registers 1, aborting\n");
10072e689cf4aSJeff Kirsher 		err = -ENOMEM;
10073e689cf4aSJeff Kirsher 		goto err_out_iounmap;
10074e689cf4aSJeff Kirsher 	}
10075e689cf4aSJeff Kirsher 
10076e689cf4aSJeff Kirsher 	np->vir_regs_2 = of_ioremap(&op->resource[3], 0,
10077e689cf4aSJeff Kirsher 				    resource_size(&op->resource[3]),
10078e689cf4aSJeff Kirsher 				    "niu vregs-2");
10079e689cf4aSJeff Kirsher 	if (!np->vir_regs_2) {
10080e689cf4aSJeff Kirsher 		dev_err(&op->dev, "Cannot map device vir registers 2, aborting\n");
10081e689cf4aSJeff Kirsher 		err = -ENOMEM;
10082e689cf4aSJeff Kirsher 		goto err_out_iounmap;
10083e689cf4aSJeff Kirsher 	}
10084e689cf4aSJeff Kirsher 
10085e689cf4aSJeff Kirsher 	niu_assign_netdev_ops(dev);
10086e689cf4aSJeff Kirsher 
10087e689cf4aSJeff Kirsher 	err = niu_get_invariants(np);
10088e689cf4aSJeff Kirsher 	if (err) {
10089e689cf4aSJeff Kirsher 		if (err != -ENODEV)
10090e689cf4aSJeff Kirsher 			dev_err(&op->dev, "Problem fetching invariants of chip, aborting\n");
10091e689cf4aSJeff Kirsher 		goto err_out_iounmap;
10092e689cf4aSJeff Kirsher 	}
10093e689cf4aSJeff Kirsher 
10094e689cf4aSJeff Kirsher 	err = register_netdev(dev);
10095e689cf4aSJeff Kirsher 	if (err) {
10096e689cf4aSJeff Kirsher 		dev_err(&op->dev, "Cannot register net device, aborting\n");
10097e689cf4aSJeff Kirsher 		goto err_out_iounmap;
10098e689cf4aSJeff Kirsher 	}
10099e689cf4aSJeff Kirsher 
101008513fbd8SJingoo Han 	platform_set_drvdata(op, dev);
10101e689cf4aSJeff Kirsher 
10102e689cf4aSJeff Kirsher 	niu_device_announce(np);
10103e689cf4aSJeff Kirsher 
10104e689cf4aSJeff Kirsher 	return 0;
10105e689cf4aSJeff Kirsher 
10106e689cf4aSJeff Kirsher err_out_iounmap:
10107e689cf4aSJeff Kirsher 	if (np->vir_regs_1) {
10108e689cf4aSJeff Kirsher 		of_iounmap(&op->resource[2], np->vir_regs_1,
10109e689cf4aSJeff Kirsher 			   resource_size(&op->resource[2]));
10110e689cf4aSJeff Kirsher 		np->vir_regs_1 = NULL;
10111e689cf4aSJeff Kirsher 	}
10112e689cf4aSJeff Kirsher 
10113e689cf4aSJeff Kirsher 	if (np->vir_regs_2) {
10114e689cf4aSJeff Kirsher 		of_iounmap(&op->resource[3], np->vir_regs_2,
10115e689cf4aSJeff Kirsher 			   resource_size(&op->resource[3]));
10116e689cf4aSJeff Kirsher 		np->vir_regs_2 = NULL;
10117e689cf4aSJeff Kirsher 	}
10118e689cf4aSJeff Kirsher 
10119e689cf4aSJeff Kirsher 	if (np->regs) {
10120e689cf4aSJeff Kirsher 		of_iounmap(&op->resource[1], np->regs,
10121e689cf4aSJeff Kirsher 			   resource_size(&op->resource[1]));
10122e689cf4aSJeff Kirsher 		np->regs = NULL;
10123e689cf4aSJeff Kirsher 	}
10124e689cf4aSJeff Kirsher 
10125e689cf4aSJeff Kirsher err_out_release_parent:
10126e689cf4aSJeff Kirsher 	niu_put_parent(np);
10127e689cf4aSJeff Kirsher 
10128e689cf4aSJeff Kirsher err_out_free_dev:
10129e689cf4aSJeff Kirsher 	free_netdev(dev);
10130e689cf4aSJeff Kirsher 
10131e689cf4aSJeff Kirsher err_out:
10132e689cf4aSJeff Kirsher 	return err;
10133e689cf4aSJeff Kirsher }
10134e689cf4aSJeff Kirsher 
niu_of_remove(struct platform_device * op)10135f73d12bdSBill Pemberton static int niu_of_remove(struct platform_device *op)
10136e689cf4aSJeff Kirsher {
101378513fbd8SJingoo Han 	struct net_device *dev = platform_get_drvdata(op);
10138e689cf4aSJeff Kirsher 
10139e689cf4aSJeff Kirsher 	if (dev) {
10140e689cf4aSJeff Kirsher 		struct niu *np = netdev_priv(dev);
10141e689cf4aSJeff Kirsher 
10142e689cf4aSJeff Kirsher 		unregister_netdev(dev);
10143e689cf4aSJeff Kirsher 
10144e689cf4aSJeff Kirsher 		if (np->vir_regs_1) {
10145e689cf4aSJeff Kirsher 			of_iounmap(&op->resource[2], np->vir_regs_1,
10146e689cf4aSJeff Kirsher 				   resource_size(&op->resource[2]));
10147e689cf4aSJeff Kirsher 			np->vir_regs_1 = NULL;
10148e689cf4aSJeff Kirsher 		}
10149e689cf4aSJeff Kirsher 
10150e689cf4aSJeff Kirsher 		if (np->vir_regs_2) {
10151e689cf4aSJeff Kirsher 			of_iounmap(&op->resource[3], np->vir_regs_2,
10152e689cf4aSJeff Kirsher 				   resource_size(&op->resource[3]));
10153e689cf4aSJeff Kirsher 			np->vir_regs_2 = NULL;
10154e689cf4aSJeff Kirsher 		}
10155e689cf4aSJeff Kirsher 
10156e689cf4aSJeff Kirsher 		if (np->regs) {
10157e689cf4aSJeff Kirsher 			of_iounmap(&op->resource[1], np->regs,
10158e689cf4aSJeff Kirsher 				   resource_size(&op->resource[1]));
10159e689cf4aSJeff Kirsher 			np->regs = NULL;
10160e689cf4aSJeff Kirsher 		}
10161e689cf4aSJeff Kirsher 
10162e689cf4aSJeff Kirsher 		niu_ldg_free(np);
10163e689cf4aSJeff Kirsher 
10164e689cf4aSJeff Kirsher 		niu_put_parent(np);
10165e689cf4aSJeff Kirsher 
10166e689cf4aSJeff Kirsher 		free_netdev(dev);
10167e689cf4aSJeff Kirsher 	}
10168e689cf4aSJeff Kirsher 	return 0;
10169e689cf4aSJeff Kirsher }
10170e689cf4aSJeff Kirsher 
10171e689cf4aSJeff Kirsher static const struct of_device_id niu_match[] = {
10172e689cf4aSJeff Kirsher 	{
10173e689cf4aSJeff Kirsher 		.name = "network",
10174e689cf4aSJeff Kirsher 		.compatible = "SUNW,niusl",
10175e689cf4aSJeff Kirsher 	},
10176e689cf4aSJeff Kirsher 	{},
10177e689cf4aSJeff Kirsher };
10178e689cf4aSJeff Kirsher MODULE_DEVICE_TABLE(of, niu_match);
10179e689cf4aSJeff Kirsher 
10180e689cf4aSJeff Kirsher static struct platform_driver niu_of_driver = {
10181e689cf4aSJeff Kirsher 	.driver = {
10182e689cf4aSJeff Kirsher 		.name = "niu",
10183e689cf4aSJeff Kirsher 		.of_match_table = niu_match,
10184e689cf4aSJeff Kirsher 	},
10185e689cf4aSJeff Kirsher 	.probe		= niu_of_probe,
10186f73d12bdSBill Pemberton 	.remove		= niu_of_remove,
10187e689cf4aSJeff Kirsher };
10188e689cf4aSJeff Kirsher 
10189e689cf4aSJeff Kirsher #endif /* CONFIG_SPARC64 */
10190e689cf4aSJeff Kirsher 
niu_init(void)10191e689cf4aSJeff Kirsher static int __init niu_init(void)
10192e689cf4aSJeff Kirsher {
10193e689cf4aSJeff Kirsher 	int err = 0;
10194e689cf4aSJeff Kirsher 
10195e689cf4aSJeff Kirsher 	BUILD_BUG_ON(PAGE_SIZE < 4 * 1024);
10196e689cf4aSJeff Kirsher 
101972dcfe9e2SKees Cook 	BUILD_BUG_ON(offsetof(struct page, mapping) !=
101982dcfe9e2SKees Cook 		     offsetof(union niu_page, next));
101992dcfe9e2SKees Cook 
10200e689cf4aSJeff Kirsher 	niu_debug = netif_msg_init(debug, NIU_MSG_DEFAULT);
10201e689cf4aSJeff Kirsher 
10202e689cf4aSJeff Kirsher #ifdef CONFIG_SPARC64
10203e689cf4aSJeff Kirsher 	err = platform_driver_register(&niu_of_driver);
10204e689cf4aSJeff Kirsher #endif
10205e689cf4aSJeff Kirsher 
10206e689cf4aSJeff Kirsher 	if (!err) {
10207e689cf4aSJeff Kirsher 		err = pci_register_driver(&niu_pci_driver);
10208e689cf4aSJeff Kirsher #ifdef CONFIG_SPARC64
10209e689cf4aSJeff Kirsher 		if (err)
10210e689cf4aSJeff Kirsher 			platform_driver_unregister(&niu_of_driver);
10211e689cf4aSJeff Kirsher #endif
10212e689cf4aSJeff Kirsher 	}
10213e689cf4aSJeff Kirsher 
10214e689cf4aSJeff Kirsher 	return err;
10215e689cf4aSJeff Kirsher }
10216e689cf4aSJeff Kirsher 
niu_exit(void)10217e689cf4aSJeff Kirsher static void __exit niu_exit(void)
10218e689cf4aSJeff Kirsher {
10219e689cf4aSJeff Kirsher 	pci_unregister_driver(&niu_pci_driver);
10220e689cf4aSJeff Kirsher #ifdef CONFIG_SPARC64
10221e689cf4aSJeff Kirsher 	platform_driver_unregister(&niu_of_driver);
10222e689cf4aSJeff Kirsher #endif
10223e689cf4aSJeff Kirsher }
10224e689cf4aSJeff Kirsher 
10225e689cf4aSJeff Kirsher module_init(niu_init);
10226e689cf4aSJeff Kirsher module_exit(niu_exit);
10227