xref: /linux/drivers/net/ethernet/micrel/ksz884x.c (revision 787343ad3d321fc987e36715433050df88353465)
1bcc9736cSJeff Kirsher /**
2bcc9736cSJeff Kirsher  * drivers/net/ksx884x.c - Micrel KSZ8841/2 PCI Ethernet driver
3bcc9736cSJeff Kirsher  *
4bcc9736cSJeff Kirsher  * Copyright (c) 2009-2010 Micrel, Inc.
5bcc9736cSJeff Kirsher  * 	Tristram Ha <Tristram.Ha@micrel.com>
6bcc9736cSJeff Kirsher  *
7bcc9736cSJeff Kirsher  * This program is free software; you can redistribute it and/or modify
8bcc9736cSJeff Kirsher  * it under the terms of the GNU General Public License version 2 as
9bcc9736cSJeff Kirsher  * published by the Free Software Foundation.
10bcc9736cSJeff Kirsher  *
11bcc9736cSJeff Kirsher  * This program is distributed in the hope that it will be useful,
12bcc9736cSJeff Kirsher  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13bcc9736cSJeff Kirsher  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14bcc9736cSJeff Kirsher  * GNU General Public License for more details.
15bcc9736cSJeff Kirsher  */
16bcc9736cSJeff Kirsher 
17bcc9736cSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18bcc9736cSJeff Kirsher 
19bcc9736cSJeff Kirsher #include <linux/init.h>
20bcc9736cSJeff Kirsher #include <linux/interrupt.h>
21bcc9736cSJeff Kirsher #include <linux/kernel.h>
22bcc9736cSJeff Kirsher #include <linux/module.h>
23bcc9736cSJeff Kirsher #include <linux/ioport.h>
24bcc9736cSJeff Kirsher #include <linux/pci.h>
25bcc9736cSJeff Kirsher #include <linux/proc_fs.h>
26bcc9736cSJeff Kirsher #include <linux/mii.h>
27bcc9736cSJeff Kirsher #include <linux/platform_device.h>
28bcc9736cSJeff Kirsher #include <linux/ethtool.h>
29bcc9736cSJeff Kirsher #include <linux/etherdevice.h>
30bcc9736cSJeff Kirsher #include <linux/in.h>
31bcc9736cSJeff Kirsher #include <linux/ip.h>
32bcc9736cSJeff Kirsher #include <linux/if_vlan.h>
33bcc9736cSJeff Kirsher #include <linux/crc32.h>
34bcc9736cSJeff Kirsher #include <linux/sched.h>
35bcc9736cSJeff Kirsher #include <linux/slab.h>
36bcc9736cSJeff Kirsher 
37bcc9736cSJeff Kirsher 
38bcc9736cSJeff Kirsher /* DMA Registers */
39bcc9736cSJeff Kirsher 
40bcc9736cSJeff Kirsher #define KS_DMA_TX_CTRL			0x0000
41bcc9736cSJeff Kirsher #define DMA_TX_ENABLE			0x00000001
42bcc9736cSJeff Kirsher #define DMA_TX_CRC_ENABLE		0x00000002
43bcc9736cSJeff Kirsher #define DMA_TX_PAD_ENABLE		0x00000004
44bcc9736cSJeff Kirsher #define DMA_TX_LOOPBACK			0x00000100
45bcc9736cSJeff Kirsher #define DMA_TX_FLOW_ENABLE		0x00000200
46bcc9736cSJeff Kirsher #define DMA_TX_CSUM_IP			0x00010000
47bcc9736cSJeff Kirsher #define DMA_TX_CSUM_TCP			0x00020000
48bcc9736cSJeff Kirsher #define DMA_TX_CSUM_UDP			0x00040000
49bcc9736cSJeff Kirsher #define DMA_TX_BURST_SIZE		0x3F000000
50bcc9736cSJeff Kirsher 
51bcc9736cSJeff Kirsher #define KS_DMA_RX_CTRL			0x0004
52bcc9736cSJeff Kirsher #define DMA_RX_ENABLE			0x00000001
53bcc9736cSJeff Kirsher #define KS884X_DMA_RX_MULTICAST		0x00000002
54bcc9736cSJeff Kirsher #define DMA_RX_PROMISCUOUS		0x00000004
55bcc9736cSJeff Kirsher #define DMA_RX_ERROR			0x00000008
56bcc9736cSJeff Kirsher #define DMA_RX_UNICAST			0x00000010
57bcc9736cSJeff Kirsher #define DMA_RX_ALL_MULTICAST		0x00000020
58bcc9736cSJeff Kirsher #define DMA_RX_BROADCAST		0x00000040
59bcc9736cSJeff Kirsher #define DMA_RX_FLOW_ENABLE		0x00000200
60bcc9736cSJeff Kirsher #define DMA_RX_CSUM_IP			0x00010000
61bcc9736cSJeff Kirsher #define DMA_RX_CSUM_TCP			0x00020000
62bcc9736cSJeff Kirsher #define DMA_RX_CSUM_UDP			0x00040000
63bcc9736cSJeff Kirsher #define DMA_RX_BURST_SIZE		0x3F000000
64bcc9736cSJeff Kirsher 
65bcc9736cSJeff Kirsher #define DMA_BURST_SHIFT			24
66bcc9736cSJeff Kirsher #define DMA_BURST_DEFAULT		8
67bcc9736cSJeff Kirsher 
68bcc9736cSJeff Kirsher #define KS_DMA_TX_START			0x0008
69bcc9736cSJeff Kirsher #define KS_DMA_RX_START			0x000C
70bcc9736cSJeff Kirsher #define DMA_START			0x00000001
71bcc9736cSJeff Kirsher 
72bcc9736cSJeff Kirsher #define KS_DMA_TX_ADDR			0x0010
73bcc9736cSJeff Kirsher #define KS_DMA_RX_ADDR			0x0014
74bcc9736cSJeff Kirsher 
75bcc9736cSJeff Kirsher #define DMA_ADDR_LIST_MASK		0xFFFFFFFC
76bcc9736cSJeff Kirsher #define DMA_ADDR_LIST_SHIFT		2
77bcc9736cSJeff Kirsher 
78bcc9736cSJeff Kirsher /* MTR0 */
79bcc9736cSJeff Kirsher #define KS884X_MULTICAST_0_OFFSET	0x0020
80bcc9736cSJeff Kirsher #define KS884X_MULTICAST_1_OFFSET	0x0021
81bcc9736cSJeff Kirsher #define KS884X_MULTICAST_2_OFFSET	0x0022
82bcc9736cSJeff Kirsher #define KS884x_MULTICAST_3_OFFSET	0x0023
83bcc9736cSJeff Kirsher /* MTR1 */
84bcc9736cSJeff Kirsher #define KS884X_MULTICAST_4_OFFSET	0x0024
85bcc9736cSJeff Kirsher #define KS884X_MULTICAST_5_OFFSET	0x0025
86bcc9736cSJeff Kirsher #define KS884X_MULTICAST_6_OFFSET	0x0026
87bcc9736cSJeff Kirsher #define KS884X_MULTICAST_7_OFFSET	0x0027
88bcc9736cSJeff Kirsher 
89bcc9736cSJeff Kirsher /* Interrupt Registers */
90bcc9736cSJeff Kirsher 
91bcc9736cSJeff Kirsher /* INTEN */
92bcc9736cSJeff Kirsher #define KS884X_INTERRUPTS_ENABLE	0x0028
93bcc9736cSJeff Kirsher /* INTST */
94bcc9736cSJeff Kirsher #define KS884X_INTERRUPTS_STATUS	0x002C
95bcc9736cSJeff Kirsher 
96bcc9736cSJeff Kirsher #define KS884X_INT_RX_STOPPED		0x02000000
97bcc9736cSJeff Kirsher #define KS884X_INT_TX_STOPPED		0x04000000
98bcc9736cSJeff Kirsher #define KS884X_INT_RX_OVERRUN		0x08000000
99bcc9736cSJeff Kirsher #define KS884X_INT_TX_EMPTY		0x10000000
100bcc9736cSJeff Kirsher #define KS884X_INT_RX			0x20000000
101bcc9736cSJeff Kirsher #define KS884X_INT_TX			0x40000000
102bcc9736cSJeff Kirsher #define KS884X_INT_PHY			0x80000000
103bcc9736cSJeff Kirsher 
104bcc9736cSJeff Kirsher #define KS884X_INT_RX_MASK		\
105bcc9736cSJeff Kirsher 	(KS884X_INT_RX | KS884X_INT_RX_OVERRUN)
106bcc9736cSJeff Kirsher #define KS884X_INT_TX_MASK		\
107bcc9736cSJeff Kirsher 	(KS884X_INT_TX | KS884X_INT_TX_EMPTY)
108bcc9736cSJeff Kirsher #define KS884X_INT_MASK	(KS884X_INT_RX | KS884X_INT_TX | KS884X_INT_PHY)
109bcc9736cSJeff Kirsher 
110bcc9736cSJeff Kirsher /* MAC Additional Station Address */
111bcc9736cSJeff Kirsher 
112bcc9736cSJeff Kirsher /* MAAL0 */
113bcc9736cSJeff Kirsher #define KS_ADD_ADDR_0_LO		0x0080
114bcc9736cSJeff Kirsher /* MAAH0 */
115bcc9736cSJeff Kirsher #define KS_ADD_ADDR_0_HI		0x0084
116bcc9736cSJeff Kirsher /* MAAL1 */
117bcc9736cSJeff Kirsher #define KS_ADD_ADDR_1_LO		0x0088
118bcc9736cSJeff Kirsher /* MAAH1 */
119bcc9736cSJeff Kirsher #define KS_ADD_ADDR_1_HI		0x008C
120bcc9736cSJeff Kirsher /* MAAL2 */
121bcc9736cSJeff Kirsher #define KS_ADD_ADDR_2_LO		0x0090
122bcc9736cSJeff Kirsher /* MAAH2 */
123bcc9736cSJeff Kirsher #define KS_ADD_ADDR_2_HI		0x0094
124bcc9736cSJeff Kirsher /* MAAL3 */
125bcc9736cSJeff Kirsher #define KS_ADD_ADDR_3_LO		0x0098
126bcc9736cSJeff Kirsher /* MAAH3 */
127bcc9736cSJeff Kirsher #define KS_ADD_ADDR_3_HI		0x009C
128bcc9736cSJeff Kirsher /* MAAL4 */
129bcc9736cSJeff Kirsher #define KS_ADD_ADDR_4_LO		0x00A0
130bcc9736cSJeff Kirsher /* MAAH4 */
131bcc9736cSJeff Kirsher #define KS_ADD_ADDR_4_HI		0x00A4
132bcc9736cSJeff Kirsher /* MAAL5 */
133bcc9736cSJeff Kirsher #define KS_ADD_ADDR_5_LO		0x00A8
134bcc9736cSJeff Kirsher /* MAAH5 */
135bcc9736cSJeff Kirsher #define KS_ADD_ADDR_5_HI		0x00AC
136bcc9736cSJeff Kirsher /* MAAL6 */
137bcc9736cSJeff Kirsher #define KS_ADD_ADDR_6_LO		0x00B0
138bcc9736cSJeff Kirsher /* MAAH6 */
139bcc9736cSJeff Kirsher #define KS_ADD_ADDR_6_HI		0x00B4
140bcc9736cSJeff Kirsher /* MAAL7 */
141bcc9736cSJeff Kirsher #define KS_ADD_ADDR_7_LO		0x00B8
142bcc9736cSJeff Kirsher /* MAAH7 */
143bcc9736cSJeff Kirsher #define KS_ADD_ADDR_7_HI		0x00BC
144bcc9736cSJeff Kirsher /* MAAL8 */
145bcc9736cSJeff Kirsher #define KS_ADD_ADDR_8_LO		0x00C0
146bcc9736cSJeff Kirsher /* MAAH8 */
147bcc9736cSJeff Kirsher #define KS_ADD_ADDR_8_HI		0x00C4
148bcc9736cSJeff Kirsher /* MAAL9 */
149bcc9736cSJeff Kirsher #define KS_ADD_ADDR_9_LO		0x00C8
150bcc9736cSJeff Kirsher /* MAAH9 */
151bcc9736cSJeff Kirsher #define KS_ADD_ADDR_9_HI		0x00CC
152bcc9736cSJeff Kirsher /* MAAL10 */
153bcc9736cSJeff Kirsher #define KS_ADD_ADDR_A_LO		0x00D0
154bcc9736cSJeff Kirsher /* MAAH10 */
155bcc9736cSJeff Kirsher #define KS_ADD_ADDR_A_HI		0x00D4
156bcc9736cSJeff Kirsher /* MAAL11 */
157bcc9736cSJeff Kirsher #define KS_ADD_ADDR_B_LO		0x00D8
158bcc9736cSJeff Kirsher /* MAAH11 */
159bcc9736cSJeff Kirsher #define KS_ADD_ADDR_B_HI		0x00DC
160bcc9736cSJeff Kirsher /* MAAL12 */
161bcc9736cSJeff Kirsher #define KS_ADD_ADDR_C_LO		0x00E0
162bcc9736cSJeff Kirsher /* MAAH12 */
163bcc9736cSJeff Kirsher #define KS_ADD_ADDR_C_HI		0x00E4
164bcc9736cSJeff Kirsher /* MAAL13 */
165bcc9736cSJeff Kirsher #define KS_ADD_ADDR_D_LO		0x00E8
166bcc9736cSJeff Kirsher /* MAAH13 */
167bcc9736cSJeff Kirsher #define KS_ADD_ADDR_D_HI		0x00EC
168bcc9736cSJeff Kirsher /* MAAL14 */
169bcc9736cSJeff Kirsher #define KS_ADD_ADDR_E_LO		0x00F0
170bcc9736cSJeff Kirsher /* MAAH14 */
171bcc9736cSJeff Kirsher #define KS_ADD_ADDR_E_HI		0x00F4
172bcc9736cSJeff Kirsher /* MAAL15 */
173bcc9736cSJeff Kirsher #define KS_ADD_ADDR_F_LO		0x00F8
174bcc9736cSJeff Kirsher /* MAAH15 */
175bcc9736cSJeff Kirsher #define KS_ADD_ADDR_F_HI		0x00FC
176bcc9736cSJeff Kirsher 
177bcc9736cSJeff Kirsher #define ADD_ADDR_HI_MASK		0x0000FFFF
178bcc9736cSJeff Kirsher #define ADD_ADDR_ENABLE			0x80000000
179bcc9736cSJeff Kirsher #define ADD_ADDR_INCR			8
180bcc9736cSJeff Kirsher 
181bcc9736cSJeff Kirsher /* Miscellaneous Registers */
182bcc9736cSJeff Kirsher 
183bcc9736cSJeff Kirsher /* MARL */
184bcc9736cSJeff Kirsher #define KS884X_ADDR_0_OFFSET		0x0200
185bcc9736cSJeff Kirsher #define KS884X_ADDR_1_OFFSET		0x0201
186bcc9736cSJeff Kirsher /* MARM */
187bcc9736cSJeff Kirsher #define KS884X_ADDR_2_OFFSET		0x0202
188bcc9736cSJeff Kirsher #define KS884X_ADDR_3_OFFSET		0x0203
189bcc9736cSJeff Kirsher /* MARH */
190bcc9736cSJeff Kirsher #define KS884X_ADDR_4_OFFSET		0x0204
191bcc9736cSJeff Kirsher #define KS884X_ADDR_5_OFFSET		0x0205
192bcc9736cSJeff Kirsher 
193bcc9736cSJeff Kirsher /* OBCR */
194bcc9736cSJeff Kirsher #define KS884X_BUS_CTRL_OFFSET		0x0210
195bcc9736cSJeff Kirsher 
196bcc9736cSJeff Kirsher #define BUS_SPEED_125_MHZ		0x0000
197bcc9736cSJeff Kirsher #define BUS_SPEED_62_5_MHZ		0x0001
198bcc9736cSJeff Kirsher #define BUS_SPEED_41_66_MHZ		0x0002
199bcc9736cSJeff Kirsher #define BUS_SPEED_25_MHZ		0x0003
200bcc9736cSJeff Kirsher 
201bcc9736cSJeff Kirsher /* EEPCR */
202bcc9736cSJeff Kirsher #define KS884X_EEPROM_CTRL_OFFSET	0x0212
203bcc9736cSJeff Kirsher 
204bcc9736cSJeff Kirsher #define EEPROM_CHIP_SELECT		0x0001
205bcc9736cSJeff Kirsher #define EEPROM_SERIAL_CLOCK		0x0002
206bcc9736cSJeff Kirsher #define EEPROM_DATA_OUT			0x0004
207bcc9736cSJeff Kirsher #define EEPROM_DATA_IN			0x0008
208bcc9736cSJeff Kirsher #define EEPROM_ACCESS_ENABLE		0x0010
209bcc9736cSJeff Kirsher 
210bcc9736cSJeff Kirsher /* MBIR */
211bcc9736cSJeff Kirsher #define KS884X_MEM_INFO_OFFSET		0x0214
212bcc9736cSJeff Kirsher 
213bcc9736cSJeff Kirsher #define RX_MEM_TEST_FAILED		0x0008
214bcc9736cSJeff Kirsher #define RX_MEM_TEST_FINISHED		0x0010
215bcc9736cSJeff Kirsher #define TX_MEM_TEST_FAILED		0x0800
216bcc9736cSJeff Kirsher #define TX_MEM_TEST_FINISHED		0x1000
217bcc9736cSJeff Kirsher 
218bcc9736cSJeff Kirsher /* GCR */
219bcc9736cSJeff Kirsher #define KS884X_GLOBAL_CTRL_OFFSET	0x0216
220bcc9736cSJeff Kirsher #define GLOBAL_SOFTWARE_RESET		0x0001
221bcc9736cSJeff Kirsher 
222bcc9736cSJeff Kirsher #define KS8841_POWER_MANAGE_OFFSET	0x0218
223bcc9736cSJeff Kirsher 
224bcc9736cSJeff Kirsher /* WFCR */
225bcc9736cSJeff Kirsher #define KS8841_WOL_CTRL_OFFSET		0x021A
226bcc9736cSJeff Kirsher #define KS8841_WOL_MAGIC_ENABLE		0x0080
227bcc9736cSJeff Kirsher #define KS8841_WOL_FRAME3_ENABLE	0x0008
228bcc9736cSJeff Kirsher #define KS8841_WOL_FRAME2_ENABLE	0x0004
229bcc9736cSJeff Kirsher #define KS8841_WOL_FRAME1_ENABLE	0x0002
230bcc9736cSJeff Kirsher #define KS8841_WOL_FRAME0_ENABLE	0x0001
231bcc9736cSJeff Kirsher 
232bcc9736cSJeff Kirsher /* WF0 */
233bcc9736cSJeff Kirsher #define KS8841_WOL_FRAME_CRC_OFFSET	0x0220
234bcc9736cSJeff Kirsher #define KS8841_WOL_FRAME_BYTE0_OFFSET	0x0224
235bcc9736cSJeff Kirsher #define KS8841_WOL_FRAME_BYTE2_OFFSET	0x0228
236bcc9736cSJeff Kirsher 
237bcc9736cSJeff Kirsher /* IACR */
238bcc9736cSJeff Kirsher #define KS884X_IACR_P			0x04A0
239bcc9736cSJeff Kirsher #define KS884X_IACR_OFFSET		KS884X_IACR_P
240bcc9736cSJeff Kirsher 
241bcc9736cSJeff Kirsher /* IADR1 */
242bcc9736cSJeff Kirsher #define KS884X_IADR1_P			0x04A2
243bcc9736cSJeff Kirsher #define KS884X_IADR2_P			0x04A4
244bcc9736cSJeff Kirsher #define KS884X_IADR3_P			0x04A6
245bcc9736cSJeff Kirsher #define KS884X_IADR4_P			0x04A8
246bcc9736cSJeff Kirsher #define KS884X_IADR5_P			0x04AA
247bcc9736cSJeff Kirsher 
248bcc9736cSJeff Kirsher #define KS884X_ACC_CTRL_SEL_OFFSET	KS884X_IACR_P
249bcc9736cSJeff Kirsher #define KS884X_ACC_CTRL_INDEX_OFFSET	(KS884X_ACC_CTRL_SEL_OFFSET + 1)
250bcc9736cSJeff Kirsher 
251bcc9736cSJeff Kirsher #define KS884X_ACC_DATA_0_OFFSET	KS884X_IADR4_P
252bcc9736cSJeff Kirsher #define KS884X_ACC_DATA_1_OFFSET	(KS884X_ACC_DATA_0_OFFSET + 1)
253bcc9736cSJeff Kirsher #define KS884X_ACC_DATA_2_OFFSET	KS884X_IADR5_P
254bcc9736cSJeff Kirsher #define KS884X_ACC_DATA_3_OFFSET	(KS884X_ACC_DATA_2_OFFSET + 1)
255bcc9736cSJeff Kirsher #define KS884X_ACC_DATA_4_OFFSET	KS884X_IADR2_P
256bcc9736cSJeff Kirsher #define KS884X_ACC_DATA_5_OFFSET	(KS884X_ACC_DATA_4_OFFSET + 1)
257bcc9736cSJeff Kirsher #define KS884X_ACC_DATA_6_OFFSET	KS884X_IADR3_P
258bcc9736cSJeff Kirsher #define KS884X_ACC_DATA_7_OFFSET	(KS884X_ACC_DATA_6_OFFSET + 1)
259bcc9736cSJeff Kirsher #define KS884X_ACC_DATA_8_OFFSET	KS884X_IADR1_P
260bcc9736cSJeff Kirsher 
261bcc9736cSJeff Kirsher /* P1MBCR */
262bcc9736cSJeff Kirsher #define KS884X_P1MBCR_P			0x04D0
263bcc9736cSJeff Kirsher #define KS884X_P1MBSR_P			0x04D2
264bcc9736cSJeff Kirsher #define KS884X_PHY1ILR_P		0x04D4
265bcc9736cSJeff Kirsher #define KS884X_PHY1IHR_P		0x04D6
266bcc9736cSJeff Kirsher #define KS884X_P1ANAR_P			0x04D8
267bcc9736cSJeff Kirsher #define KS884X_P1ANLPR_P		0x04DA
268bcc9736cSJeff Kirsher 
269bcc9736cSJeff Kirsher /* P2MBCR */
270bcc9736cSJeff Kirsher #define KS884X_P2MBCR_P			0x04E0
271bcc9736cSJeff Kirsher #define KS884X_P2MBSR_P			0x04E2
272bcc9736cSJeff Kirsher #define KS884X_PHY2ILR_P		0x04E4
273bcc9736cSJeff Kirsher #define KS884X_PHY2IHR_P		0x04E6
274bcc9736cSJeff Kirsher #define KS884X_P2ANAR_P			0x04E8
275bcc9736cSJeff Kirsher #define KS884X_P2ANLPR_P		0x04EA
276bcc9736cSJeff Kirsher 
277bcc9736cSJeff Kirsher #define KS884X_PHY_1_CTRL_OFFSET	KS884X_P1MBCR_P
278bcc9736cSJeff Kirsher #define PHY_CTRL_INTERVAL		(KS884X_P2MBCR_P - KS884X_P1MBCR_P)
279bcc9736cSJeff Kirsher 
280bcc9736cSJeff Kirsher #define KS884X_PHY_CTRL_OFFSET		0x00
281bcc9736cSJeff Kirsher 
282bcc9736cSJeff Kirsher /* Mode Control Register */
283bcc9736cSJeff Kirsher #define PHY_REG_CTRL			0
284bcc9736cSJeff Kirsher 
285bcc9736cSJeff Kirsher #define PHY_RESET			0x8000
286bcc9736cSJeff Kirsher #define PHY_LOOPBACK			0x4000
287bcc9736cSJeff Kirsher #define PHY_SPEED_100MBIT		0x2000
288bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_ENABLE		0x1000
289bcc9736cSJeff Kirsher #define PHY_POWER_DOWN			0x0800
290bcc9736cSJeff Kirsher #define PHY_MII_DISABLE			0x0400
291bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_RESTART		0x0200
292bcc9736cSJeff Kirsher #define PHY_FULL_DUPLEX			0x0100
293bcc9736cSJeff Kirsher #define PHY_COLLISION_TEST		0x0080
294bcc9736cSJeff Kirsher #define PHY_HP_MDIX			0x0020
295bcc9736cSJeff Kirsher #define PHY_FORCE_MDIX			0x0010
296bcc9736cSJeff Kirsher #define PHY_AUTO_MDIX_DISABLE		0x0008
297bcc9736cSJeff Kirsher #define PHY_REMOTE_FAULT_DISABLE	0x0004
298bcc9736cSJeff Kirsher #define PHY_TRANSMIT_DISABLE		0x0002
299bcc9736cSJeff Kirsher #define PHY_LED_DISABLE			0x0001
300bcc9736cSJeff Kirsher 
301bcc9736cSJeff Kirsher #define KS884X_PHY_STATUS_OFFSET	0x02
302bcc9736cSJeff Kirsher 
303bcc9736cSJeff Kirsher /* Mode Status Register */
304bcc9736cSJeff Kirsher #define PHY_REG_STATUS			1
305bcc9736cSJeff Kirsher 
306bcc9736cSJeff Kirsher #define PHY_100BT4_CAPABLE		0x8000
307bcc9736cSJeff Kirsher #define PHY_100BTX_FD_CAPABLE		0x4000
308bcc9736cSJeff Kirsher #define PHY_100BTX_CAPABLE		0x2000
309bcc9736cSJeff Kirsher #define PHY_10BT_FD_CAPABLE		0x1000
310bcc9736cSJeff Kirsher #define PHY_10BT_CAPABLE		0x0800
311bcc9736cSJeff Kirsher #define PHY_MII_SUPPRESS_CAPABLE	0x0040
312bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_ACKNOWLEDGE	0x0020
313bcc9736cSJeff Kirsher #define PHY_REMOTE_FAULT		0x0010
314bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_CAPABLE		0x0008
315bcc9736cSJeff Kirsher #define PHY_LINK_STATUS			0x0004
316bcc9736cSJeff Kirsher #define PHY_JABBER_DETECT		0x0002
317bcc9736cSJeff Kirsher #define PHY_EXTENDED_CAPABILITY		0x0001
318bcc9736cSJeff Kirsher 
319bcc9736cSJeff Kirsher #define KS884X_PHY_ID_1_OFFSET		0x04
320bcc9736cSJeff Kirsher #define KS884X_PHY_ID_2_OFFSET		0x06
321bcc9736cSJeff Kirsher 
322bcc9736cSJeff Kirsher /* PHY Identifier Registers */
323bcc9736cSJeff Kirsher #define PHY_REG_ID_1			2
324bcc9736cSJeff Kirsher #define PHY_REG_ID_2			3
325bcc9736cSJeff Kirsher 
326bcc9736cSJeff Kirsher #define KS884X_PHY_AUTO_NEG_OFFSET	0x08
327bcc9736cSJeff Kirsher 
328bcc9736cSJeff Kirsher /* Auto-Negotiation Advertisement Register */
329bcc9736cSJeff Kirsher #define PHY_REG_AUTO_NEGOTIATION	4
330bcc9736cSJeff Kirsher 
331bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_NEXT_PAGE		0x8000
332bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_REMOTE_FAULT	0x2000
333bcc9736cSJeff Kirsher /* Not supported. */
334bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_ASYM_PAUSE		0x0800
335bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_SYM_PAUSE		0x0400
336bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_100BT4		0x0200
337bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_100BTX_FD		0x0100
338bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_100BTX		0x0080
339bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_10BT_FD		0x0040
340bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_10BT		0x0020
341bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_SELECTOR		0x001F
342bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_802_3		0x0001
343bcc9736cSJeff Kirsher 
344bcc9736cSJeff Kirsher #define PHY_AUTO_NEG_PAUSE  (PHY_AUTO_NEG_SYM_PAUSE | PHY_AUTO_NEG_ASYM_PAUSE)
345bcc9736cSJeff Kirsher 
346bcc9736cSJeff Kirsher #define KS884X_PHY_REMOTE_CAP_OFFSET	0x0A
347bcc9736cSJeff Kirsher 
348bcc9736cSJeff Kirsher /* Auto-Negotiation Link Partner Ability Register */
349bcc9736cSJeff Kirsher #define PHY_REG_REMOTE_CAPABILITY	5
350bcc9736cSJeff Kirsher 
351bcc9736cSJeff Kirsher #define PHY_REMOTE_NEXT_PAGE		0x8000
352bcc9736cSJeff Kirsher #define PHY_REMOTE_ACKNOWLEDGE		0x4000
353bcc9736cSJeff Kirsher #define PHY_REMOTE_REMOTE_FAULT		0x2000
354bcc9736cSJeff Kirsher #define PHY_REMOTE_SYM_PAUSE		0x0400
355bcc9736cSJeff Kirsher #define PHY_REMOTE_100BTX_FD		0x0100
356bcc9736cSJeff Kirsher #define PHY_REMOTE_100BTX		0x0080
357bcc9736cSJeff Kirsher #define PHY_REMOTE_10BT_FD		0x0040
358bcc9736cSJeff Kirsher #define PHY_REMOTE_10BT			0x0020
359bcc9736cSJeff Kirsher 
360bcc9736cSJeff Kirsher /* P1VCT */
361bcc9736cSJeff Kirsher #define KS884X_P1VCT_P			0x04F0
362bcc9736cSJeff Kirsher #define KS884X_P1PHYCTRL_P		0x04F2
363bcc9736cSJeff Kirsher 
364bcc9736cSJeff Kirsher /* P2VCT */
365bcc9736cSJeff Kirsher #define KS884X_P2VCT_P			0x04F4
366bcc9736cSJeff Kirsher #define KS884X_P2PHYCTRL_P		0x04F6
367bcc9736cSJeff Kirsher 
368bcc9736cSJeff Kirsher #define KS884X_PHY_SPECIAL_OFFSET	KS884X_P1VCT_P
369bcc9736cSJeff Kirsher #define PHY_SPECIAL_INTERVAL		(KS884X_P2VCT_P - KS884X_P1VCT_P)
370bcc9736cSJeff Kirsher 
371bcc9736cSJeff Kirsher #define KS884X_PHY_LINK_MD_OFFSET	0x00
372bcc9736cSJeff Kirsher 
373bcc9736cSJeff Kirsher #define PHY_START_CABLE_DIAG		0x8000
374bcc9736cSJeff Kirsher #define PHY_CABLE_DIAG_RESULT		0x6000
375bcc9736cSJeff Kirsher #define PHY_CABLE_STAT_NORMAL		0x0000
376bcc9736cSJeff Kirsher #define PHY_CABLE_STAT_OPEN		0x2000
377bcc9736cSJeff Kirsher #define PHY_CABLE_STAT_SHORT		0x4000
378bcc9736cSJeff Kirsher #define PHY_CABLE_STAT_FAILED		0x6000
379bcc9736cSJeff Kirsher #define PHY_CABLE_10M_SHORT		0x1000
380bcc9736cSJeff Kirsher #define PHY_CABLE_FAULT_COUNTER		0x01FF
381bcc9736cSJeff Kirsher 
382bcc9736cSJeff Kirsher #define KS884X_PHY_PHY_CTRL_OFFSET	0x02
383bcc9736cSJeff Kirsher 
384bcc9736cSJeff Kirsher #define PHY_STAT_REVERSED_POLARITY	0x0020
385bcc9736cSJeff Kirsher #define PHY_STAT_MDIX			0x0010
386bcc9736cSJeff Kirsher #define PHY_FORCE_LINK			0x0008
387bcc9736cSJeff Kirsher #define PHY_POWER_SAVING_DISABLE	0x0004
388bcc9736cSJeff Kirsher #define PHY_REMOTE_LOOPBACK		0x0002
389bcc9736cSJeff Kirsher 
390bcc9736cSJeff Kirsher /* SIDER */
391bcc9736cSJeff Kirsher #define KS884X_SIDER_P			0x0400
392bcc9736cSJeff Kirsher #define KS884X_CHIP_ID_OFFSET		KS884X_SIDER_P
393bcc9736cSJeff Kirsher #define KS884X_FAMILY_ID_OFFSET		(KS884X_CHIP_ID_OFFSET + 1)
394bcc9736cSJeff Kirsher 
395bcc9736cSJeff Kirsher #define REG_FAMILY_ID			0x88
396bcc9736cSJeff Kirsher 
397bcc9736cSJeff Kirsher #define REG_CHIP_ID_41			0x8810
398bcc9736cSJeff Kirsher #define REG_CHIP_ID_42			0x8800
399bcc9736cSJeff Kirsher 
400bcc9736cSJeff Kirsher #define KS884X_CHIP_ID_MASK_41		0xFF10
401bcc9736cSJeff Kirsher #define KS884X_CHIP_ID_MASK		0xFFF0
402bcc9736cSJeff Kirsher #define KS884X_CHIP_ID_SHIFT		4
403bcc9736cSJeff Kirsher #define KS884X_REVISION_MASK		0x000E
404bcc9736cSJeff Kirsher #define KS884X_REVISION_SHIFT		1
405bcc9736cSJeff Kirsher #define KS8842_START			0x0001
406bcc9736cSJeff Kirsher 
407bcc9736cSJeff Kirsher #define CHIP_IP_41_M			0x8810
408bcc9736cSJeff Kirsher #define CHIP_IP_42_M			0x8800
409bcc9736cSJeff Kirsher #define CHIP_IP_61_M			0x8890
410bcc9736cSJeff Kirsher #define CHIP_IP_62_M			0x8880
411bcc9736cSJeff Kirsher 
412bcc9736cSJeff Kirsher #define CHIP_IP_41_P			0x8850
413bcc9736cSJeff Kirsher #define CHIP_IP_42_P			0x8840
414bcc9736cSJeff Kirsher #define CHIP_IP_61_P			0x88D0
415bcc9736cSJeff Kirsher #define CHIP_IP_62_P			0x88C0
416bcc9736cSJeff Kirsher 
417bcc9736cSJeff Kirsher /* SGCR1 */
418bcc9736cSJeff Kirsher #define KS8842_SGCR1_P			0x0402
419bcc9736cSJeff Kirsher #define KS8842_SWITCH_CTRL_1_OFFSET	KS8842_SGCR1_P
420bcc9736cSJeff Kirsher 
421bcc9736cSJeff Kirsher #define SWITCH_PASS_ALL			0x8000
422bcc9736cSJeff Kirsher #define SWITCH_TX_FLOW_CTRL		0x2000
423bcc9736cSJeff Kirsher #define SWITCH_RX_FLOW_CTRL		0x1000
424bcc9736cSJeff Kirsher #define SWITCH_CHECK_LENGTH		0x0800
425bcc9736cSJeff Kirsher #define SWITCH_AGING_ENABLE		0x0400
426bcc9736cSJeff Kirsher #define SWITCH_FAST_AGING		0x0200
427bcc9736cSJeff Kirsher #define SWITCH_AGGR_BACKOFF		0x0100
428bcc9736cSJeff Kirsher #define SWITCH_PASS_PAUSE		0x0008
429bcc9736cSJeff Kirsher #define SWITCH_LINK_AUTO_AGING		0x0001
430bcc9736cSJeff Kirsher 
431bcc9736cSJeff Kirsher /* SGCR2 */
432bcc9736cSJeff Kirsher #define KS8842_SGCR2_P			0x0404
433bcc9736cSJeff Kirsher #define KS8842_SWITCH_CTRL_2_OFFSET	KS8842_SGCR2_P
434bcc9736cSJeff Kirsher 
435bcc9736cSJeff Kirsher #define SWITCH_VLAN_ENABLE		0x8000
436bcc9736cSJeff Kirsher #define SWITCH_IGMP_SNOOP		0x4000
437bcc9736cSJeff Kirsher #define IPV6_MLD_SNOOP_ENABLE		0x2000
438bcc9736cSJeff Kirsher #define IPV6_MLD_SNOOP_OPTION		0x1000
439bcc9736cSJeff Kirsher #define PRIORITY_SCHEME_SELECT		0x0800
440bcc9736cSJeff Kirsher #define SWITCH_MIRROR_RX_TX		0x0100
441bcc9736cSJeff Kirsher #define UNICAST_VLAN_BOUNDARY		0x0080
442bcc9736cSJeff Kirsher #define MULTICAST_STORM_DISABLE		0x0040
443bcc9736cSJeff Kirsher #define SWITCH_BACK_PRESSURE		0x0020
444bcc9736cSJeff Kirsher #define FAIR_FLOW_CTRL			0x0010
445bcc9736cSJeff Kirsher #define NO_EXC_COLLISION_DROP		0x0008
446bcc9736cSJeff Kirsher #define SWITCH_HUGE_PACKET		0x0004
447bcc9736cSJeff Kirsher #define SWITCH_LEGAL_PACKET		0x0002
448bcc9736cSJeff Kirsher #define SWITCH_BUF_RESERVE		0x0001
449bcc9736cSJeff Kirsher 
450bcc9736cSJeff Kirsher /* SGCR3 */
451bcc9736cSJeff Kirsher #define KS8842_SGCR3_P			0x0406
452bcc9736cSJeff Kirsher #define KS8842_SWITCH_CTRL_3_OFFSET	KS8842_SGCR3_P
453bcc9736cSJeff Kirsher 
454bcc9736cSJeff Kirsher #define BROADCAST_STORM_RATE_LO		0xFF00
455bcc9736cSJeff Kirsher #define SWITCH_REPEATER			0x0080
456bcc9736cSJeff Kirsher #define SWITCH_HALF_DUPLEX		0x0040
457bcc9736cSJeff Kirsher #define SWITCH_FLOW_CTRL		0x0020
458bcc9736cSJeff Kirsher #define SWITCH_10_MBIT			0x0010
459bcc9736cSJeff Kirsher #define SWITCH_REPLACE_NULL_VID		0x0008
460bcc9736cSJeff Kirsher #define BROADCAST_STORM_RATE_HI		0x0007
461bcc9736cSJeff Kirsher 
462bcc9736cSJeff Kirsher #define BROADCAST_STORM_RATE		0x07FF
463bcc9736cSJeff Kirsher 
464bcc9736cSJeff Kirsher /* SGCR4 */
465bcc9736cSJeff Kirsher #define KS8842_SGCR4_P			0x0408
466bcc9736cSJeff Kirsher 
467bcc9736cSJeff Kirsher /* SGCR5 */
468bcc9736cSJeff Kirsher #define KS8842_SGCR5_P			0x040A
469bcc9736cSJeff Kirsher #define KS8842_SWITCH_CTRL_5_OFFSET	KS8842_SGCR5_P
470bcc9736cSJeff Kirsher 
471bcc9736cSJeff Kirsher #define LED_MODE			0x8200
472bcc9736cSJeff Kirsher #define LED_SPEED_DUPLEX_ACT		0x0000
473bcc9736cSJeff Kirsher #define LED_SPEED_DUPLEX_LINK_ACT	0x8000
474bcc9736cSJeff Kirsher #define LED_DUPLEX_10_100		0x0200
475bcc9736cSJeff Kirsher 
476bcc9736cSJeff Kirsher /* SGCR6 */
477bcc9736cSJeff Kirsher #define KS8842_SGCR6_P			0x0410
478bcc9736cSJeff Kirsher #define KS8842_SWITCH_CTRL_6_OFFSET	KS8842_SGCR6_P
479bcc9736cSJeff Kirsher 
480bcc9736cSJeff Kirsher #define KS8842_PRIORITY_MASK		3
481bcc9736cSJeff Kirsher #define KS8842_PRIORITY_SHIFT		2
482bcc9736cSJeff Kirsher 
483bcc9736cSJeff Kirsher /* SGCR7 */
484bcc9736cSJeff Kirsher #define KS8842_SGCR7_P			0x0412
485bcc9736cSJeff Kirsher #define KS8842_SWITCH_CTRL_7_OFFSET	KS8842_SGCR7_P
486bcc9736cSJeff Kirsher 
487bcc9736cSJeff Kirsher #define SWITCH_UNK_DEF_PORT_ENABLE	0x0008
488bcc9736cSJeff Kirsher #define SWITCH_UNK_DEF_PORT_3		0x0004
489bcc9736cSJeff Kirsher #define SWITCH_UNK_DEF_PORT_2		0x0002
490bcc9736cSJeff Kirsher #define SWITCH_UNK_DEF_PORT_1		0x0001
491bcc9736cSJeff Kirsher 
492bcc9736cSJeff Kirsher /* MACAR1 */
493bcc9736cSJeff Kirsher #define KS8842_MACAR1_P			0x0470
494bcc9736cSJeff Kirsher #define KS8842_MACAR2_P			0x0472
495bcc9736cSJeff Kirsher #define KS8842_MACAR3_P			0x0474
496bcc9736cSJeff Kirsher #define KS8842_MAC_ADDR_1_OFFSET	KS8842_MACAR1_P
497bcc9736cSJeff Kirsher #define KS8842_MAC_ADDR_0_OFFSET	(KS8842_MAC_ADDR_1_OFFSET + 1)
498bcc9736cSJeff Kirsher #define KS8842_MAC_ADDR_3_OFFSET	KS8842_MACAR2_P
499bcc9736cSJeff Kirsher #define KS8842_MAC_ADDR_2_OFFSET	(KS8842_MAC_ADDR_3_OFFSET + 1)
500bcc9736cSJeff Kirsher #define KS8842_MAC_ADDR_5_OFFSET	KS8842_MACAR3_P
501bcc9736cSJeff Kirsher #define KS8842_MAC_ADDR_4_OFFSET	(KS8842_MAC_ADDR_5_OFFSET + 1)
502bcc9736cSJeff Kirsher 
503bcc9736cSJeff Kirsher /* TOSR1 */
504bcc9736cSJeff Kirsher #define KS8842_TOSR1_P			0x0480
505bcc9736cSJeff Kirsher #define KS8842_TOSR2_P			0x0482
506bcc9736cSJeff Kirsher #define KS8842_TOSR3_P			0x0484
507bcc9736cSJeff Kirsher #define KS8842_TOSR4_P			0x0486
508bcc9736cSJeff Kirsher #define KS8842_TOSR5_P			0x0488
509bcc9736cSJeff Kirsher #define KS8842_TOSR6_P			0x048A
510bcc9736cSJeff Kirsher #define KS8842_TOSR7_P			0x0490
511bcc9736cSJeff Kirsher #define KS8842_TOSR8_P			0x0492
512bcc9736cSJeff Kirsher #define KS8842_TOS_1_OFFSET		KS8842_TOSR1_P
513bcc9736cSJeff Kirsher #define KS8842_TOS_2_OFFSET		KS8842_TOSR2_P
514bcc9736cSJeff Kirsher #define KS8842_TOS_3_OFFSET		KS8842_TOSR3_P
515bcc9736cSJeff Kirsher #define KS8842_TOS_4_OFFSET		KS8842_TOSR4_P
516bcc9736cSJeff Kirsher #define KS8842_TOS_5_OFFSET		KS8842_TOSR5_P
517bcc9736cSJeff Kirsher #define KS8842_TOS_6_OFFSET		KS8842_TOSR6_P
518bcc9736cSJeff Kirsher 
519bcc9736cSJeff Kirsher #define KS8842_TOS_7_OFFSET		KS8842_TOSR7_P
520bcc9736cSJeff Kirsher #define KS8842_TOS_8_OFFSET		KS8842_TOSR8_P
521bcc9736cSJeff Kirsher 
522bcc9736cSJeff Kirsher /* P1CR1 */
523bcc9736cSJeff Kirsher #define KS8842_P1CR1_P			0x0500
524bcc9736cSJeff Kirsher #define KS8842_P1CR2_P			0x0502
525bcc9736cSJeff Kirsher #define KS8842_P1VIDR_P			0x0504
526bcc9736cSJeff Kirsher #define KS8842_P1CR3_P			0x0506
527bcc9736cSJeff Kirsher #define KS8842_P1IRCR_P			0x0508
528bcc9736cSJeff Kirsher #define KS8842_P1ERCR_P			0x050A
529bcc9736cSJeff Kirsher #define KS884X_P1SCSLMD_P		0x0510
530bcc9736cSJeff Kirsher #define KS884X_P1CR4_P			0x0512
531bcc9736cSJeff Kirsher #define KS884X_P1SR_P			0x0514
532bcc9736cSJeff Kirsher 
533bcc9736cSJeff Kirsher /* P2CR1 */
534bcc9736cSJeff Kirsher #define KS8842_P2CR1_P			0x0520
535bcc9736cSJeff Kirsher #define KS8842_P2CR2_P			0x0522
536bcc9736cSJeff Kirsher #define KS8842_P2VIDR_P			0x0524
537bcc9736cSJeff Kirsher #define KS8842_P2CR3_P			0x0526
538bcc9736cSJeff Kirsher #define KS8842_P2IRCR_P			0x0528
539bcc9736cSJeff Kirsher #define KS8842_P2ERCR_P			0x052A
540bcc9736cSJeff Kirsher #define KS884X_P2SCSLMD_P		0x0530
541bcc9736cSJeff Kirsher #define KS884X_P2CR4_P			0x0532
542bcc9736cSJeff Kirsher #define KS884X_P2SR_P			0x0534
543bcc9736cSJeff Kirsher 
544bcc9736cSJeff Kirsher /* P3CR1 */
545bcc9736cSJeff Kirsher #define KS8842_P3CR1_P			0x0540
546bcc9736cSJeff Kirsher #define KS8842_P3CR2_P			0x0542
547bcc9736cSJeff Kirsher #define KS8842_P3VIDR_P			0x0544
548bcc9736cSJeff Kirsher #define KS8842_P3CR3_P			0x0546
549bcc9736cSJeff Kirsher #define KS8842_P3IRCR_P			0x0548
550bcc9736cSJeff Kirsher #define KS8842_P3ERCR_P			0x054A
551bcc9736cSJeff Kirsher 
552bcc9736cSJeff Kirsher #define KS8842_PORT_1_CTRL_1		KS8842_P1CR1_P
553bcc9736cSJeff Kirsher #define KS8842_PORT_2_CTRL_1		KS8842_P2CR1_P
554bcc9736cSJeff Kirsher #define KS8842_PORT_3_CTRL_1		KS8842_P3CR1_P
555bcc9736cSJeff Kirsher 
556bcc9736cSJeff Kirsher #define PORT_CTRL_ADDR(port, addr)		\
557bcc9736cSJeff Kirsher 	(addr = KS8842_PORT_1_CTRL_1 + (port) *	\
558bcc9736cSJeff Kirsher 		(KS8842_PORT_2_CTRL_1 - KS8842_PORT_1_CTRL_1))
559bcc9736cSJeff Kirsher 
560bcc9736cSJeff Kirsher #define KS8842_PORT_CTRL_1_OFFSET	0x00
561bcc9736cSJeff Kirsher 
562bcc9736cSJeff Kirsher #define PORT_BROADCAST_STORM		0x0080
563bcc9736cSJeff Kirsher #define PORT_DIFFSERV_ENABLE		0x0040
564bcc9736cSJeff Kirsher #define PORT_802_1P_ENABLE		0x0020
565bcc9736cSJeff Kirsher #define PORT_BASED_PRIORITY_MASK	0x0018
566bcc9736cSJeff Kirsher #define PORT_BASED_PRIORITY_BASE	0x0003
567bcc9736cSJeff Kirsher #define PORT_BASED_PRIORITY_SHIFT	3
568bcc9736cSJeff Kirsher #define PORT_BASED_PRIORITY_0		0x0000
569bcc9736cSJeff Kirsher #define PORT_BASED_PRIORITY_1		0x0008
570bcc9736cSJeff Kirsher #define PORT_BASED_PRIORITY_2		0x0010
571bcc9736cSJeff Kirsher #define PORT_BASED_PRIORITY_3		0x0018
572bcc9736cSJeff Kirsher #define PORT_INSERT_TAG			0x0004
573bcc9736cSJeff Kirsher #define PORT_REMOVE_TAG			0x0002
574bcc9736cSJeff Kirsher #define PORT_PRIO_QUEUE_ENABLE		0x0001
575bcc9736cSJeff Kirsher 
576bcc9736cSJeff Kirsher #define KS8842_PORT_CTRL_2_OFFSET	0x02
577bcc9736cSJeff Kirsher 
578bcc9736cSJeff Kirsher #define PORT_INGRESS_VLAN_FILTER	0x4000
579bcc9736cSJeff Kirsher #define PORT_DISCARD_NON_VID		0x2000
580bcc9736cSJeff Kirsher #define PORT_FORCE_FLOW_CTRL		0x1000
581bcc9736cSJeff Kirsher #define PORT_BACK_PRESSURE		0x0800
582bcc9736cSJeff Kirsher #define PORT_TX_ENABLE			0x0400
583bcc9736cSJeff Kirsher #define PORT_RX_ENABLE			0x0200
584bcc9736cSJeff Kirsher #define PORT_LEARN_DISABLE		0x0100
585bcc9736cSJeff Kirsher #define PORT_MIRROR_SNIFFER		0x0080
586bcc9736cSJeff Kirsher #define PORT_MIRROR_RX			0x0040
587bcc9736cSJeff Kirsher #define PORT_MIRROR_TX			0x0020
588bcc9736cSJeff Kirsher #define PORT_USER_PRIORITY_CEILING	0x0008
589bcc9736cSJeff Kirsher #define PORT_VLAN_MEMBERSHIP		0x0007
590bcc9736cSJeff Kirsher 
591bcc9736cSJeff Kirsher #define KS8842_PORT_CTRL_VID_OFFSET	0x04
592bcc9736cSJeff Kirsher 
593bcc9736cSJeff Kirsher #define PORT_DEFAULT_VID		0x0001
594bcc9736cSJeff Kirsher 
595bcc9736cSJeff Kirsher #define KS8842_PORT_CTRL_3_OFFSET	0x06
596bcc9736cSJeff Kirsher 
597bcc9736cSJeff Kirsher #define PORT_INGRESS_LIMIT_MODE		0x000C
598bcc9736cSJeff Kirsher #define PORT_INGRESS_ALL		0x0000
599bcc9736cSJeff Kirsher #define PORT_INGRESS_UNICAST		0x0004
600bcc9736cSJeff Kirsher #define PORT_INGRESS_MULTICAST		0x0008
601bcc9736cSJeff Kirsher #define PORT_INGRESS_BROADCAST		0x000C
602bcc9736cSJeff Kirsher #define PORT_COUNT_IFG			0x0002
603bcc9736cSJeff Kirsher #define PORT_COUNT_PREAMBLE		0x0001
604bcc9736cSJeff Kirsher 
605bcc9736cSJeff Kirsher #define KS8842_PORT_IN_RATE_OFFSET	0x08
606bcc9736cSJeff Kirsher #define KS8842_PORT_OUT_RATE_OFFSET	0x0A
607bcc9736cSJeff Kirsher 
608bcc9736cSJeff Kirsher #define PORT_PRIORITY_RATE		0x0F
609bcc9736cSJeff Kirsher #define PORT_PRIORITY_RATE_SHIFT	4
610bcc9736cSJeff Kirsher 
611bcc9736cSJeff Kirsher #define KS884X_PORT_LINK_MD		0x10
612bcc9736cSJeff Kirsher 
613bcc9736cSJeff Kirsher #define PORT_CABLE_10M_SHORT		0x8000
614bcc9736cSJeff Kirsher #define PORT_CABLE_DIAG_RESULT		0x6000
615bcc9736cSJeff Kirsher #define PORT_CABLE_STAT_NORMAL		0x0000
616bcc9736cSJeff Kirsher #define PORT_CABLE_STAT_OPEN		0x2000
617bcc9736cSJeff Kirsher #define PORT_CABLE_STAT_SHORT		0x4000
618bcc9736cSJeff Kirsher #define PORT_CABLE_STAT_FAILED		0x6000
619bcc9736cSJeff Kirsher #define PORT_START_CABLE_DIAG		0x1000
620bcc9736cSJeff Kirsher #define PORT_FORCE_LINK			0x0800
621bcc9736cSJeff Kirsher #define PORT_POWER_SAVING_DISABLE	0x0400
622bcc9736cSJeff Kirsher #define PORT_PHY_REMOTE_LOOPBACK	0x0200
623bcc9736cSJeff Kirsher #define PORT_CABLE_FAULT_COUNTER	0x01FF
624bcc9736cSJeff Kirsher 
625bcc9736cSJeff Kirsher #define KS884X_PORT_CTRL_4_OFFSET	0x12
626bcc9736cSJeff Kirsher 
627bcc9736cSJeff Kirsher #define PORT_LED_OFF			0x8000
628bcc9736cSJeff Kirsher #define PORT_TX_DISABLE			0x4000
629bcc9736cSJeff Kirsher #define PORT_AUTO_NEG_RESTART		0x2000
630bcc9736cSJeff Kirsher #define PORT_REMOTE_FAULT_DISABLE	0x1000
631bcc9736cSJeff Kirsher #define PORT_POWER_DOWN			0x0800
632bcc9736cSJeff Kirsher #define PORT_AUTO_MDIX_DISABLE		0x0400
633bcc9736cSJeff Kirsher #define PORT_FORCE_MDIX			0x0200
634bcc9736cSJeff Kirsher #define PORT_LOOPBACK			0x0100
635bcc9736cSJeff Kirsher #define PORT_AUTO_NEG_ENABLE		0x0080
636bcc9736cSJeff Kirsher #define PORT_FORCE_100_MBIT		0x0040
637bcc9736cSJeff Kirsher #define PORT_FORCE_FULL_DUPLEX		0x0020
638bcc9736cSJeff Kirsher #define PORT_AUTO_NEG_SYM_PAUSE		0x0010
639bcc9736cSJeff Kirsher #define PORT_AUTO_NEG_100BTX_FD		0x0008
640bcc9736cSJeff Kirsher #define PORT_AUTO_NEG_100BTX		0x0004
641bcc9736cSJeff Kirsher #define PORT_AUTO_NEG_10BT_FD		0x0002
642bcc9736cSJeff Kirsher #define PORT_AUTO_NEG_10BT		0x0001
643bcc9736cSJeff Kirsher 
644bcc9736cSJeff Kirsher #define KS884X_PORT_STATUS_OFFSET	0x14
645bcc9736cSJeff Kirsher 
646bcc9736cSJeff Kirsher #define PORT_HP_MDIX			0x8000
647bcc9736cSJeff Kirsher #define PORT_REVERSED_POLARITY		0x2000
648bcc9736cSJeff Kirsher #define PORT_RX_FLOW_CTRL		0x0800
649bcc9736cSJeff Kirsher #define PORT_TX_FLOW_CTRL		0x1000
650bcc9736cSJeff Kirsher #define PORT_STATUS_SPEED_100MBIT	0x0400
651bcc9736cSJeff Kirsher #define PORT_STATUS_FULL_DUPLEX		0x0200
652bcc9736cSJeff Kirsher #define PORT_REMOTE_FAULT		0x0100
653bcc9736cSJeff Kirsher #define PORT_MDIX_STATUS		0x0080
654bcc9736cSJeff Kirsher #define PORT_AUTO_NEG_COMPLETE		0x0040
655bcc9736cSJeff Kirsher #define PORT_STATUS_LINK_GOOD		0x0020
656bcc9736cSJeff Kirsher #define PORT_REMOTE_SYM_PAUSE		0x0010
657bcc9736cSJeff Kirsher #define PORT_REMOTE_100BTX_FD		0x0008
658bcc9736cSJeff Kirsher #define PORT_REMOTE_100BTX		0x0004
659bcc9736cSJeff Kirsher #define PORT_REMOTE_10BT_FD		0x0002
660bcc9736cSJeff Kirsher #define PORT_REMOTE_10BT		0x0001
661bcc9736cSJeff Kirsher 
662bcc9736cSJeff Kirsher /*
663bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_ADDR		00-0000FFFF-FFFFFFFF
664bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_FWD_PORTS	00-00070000-00000000
665bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_VALID		00-00080000-00000000
666bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_OVERRIDE	00-00100000-00000000
667bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_USE_FID	00-00200000-00000000
668bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_FID		00-03C00000-00000000
669bcc9736cSJeff Kirsher */
670bcc9736cSJeff Kirsher 
671bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_ADDR		0x0000FFFF
672bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_FWD_PORTS	0x00070000
673bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_VALID		0x00080000
674bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_OVERRIDE	0x00100000
675bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_USE_FID	0x00200000
676bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_FID		0x03C00000
677bcc9736cSJeff Kirsher 
678bcc9736cSJeff Kirsher #define STATIC_MAC_FWD_PORTS_SHIFT	16
679bcc9736cSJeff Kirsher #define STATIC_MAC_FID_SHIFT		22
680bcc9736cSJeff Kirsher 
681bcc9736cSJeff Kirsher /*
682bcc9736cSJeff Kirsher #define VLAN_TABLE_VID			00-00000000-00000FFF
683bcc9736cSJeff Kirsher #define VLAN_TABLE_FID			00-00000000-0000F000
684bcc9736cSJeff Kirsher #define VLAN_TABLE_MEMBERSHIP		00-00000000-00070000
685bcc9736cSJeff Kirsher #define VLAN_TABLE_VALID		00-00000000-00080000
686bcc9736cSJeff Kirsher */
687bcc9736cSJeff Kirsher 
688bcc9736cSJeff Kirsher #define VLAN_TABLE_VID			0x00000FFF
689bcc9736cSJeff Kirsher #define VLAN_TABLE_FID			0x0000F000
690bcc9736cSJeff Kirsher #define VLAN_TABLE_MEMBERSHIP		0x00070000
691bcc9736cSJeff Kirsher #define VLAN_TABLE_VALID		0x00080000
692bcc9736cSJeff Kirsher 
693bcc9736cSJeff Kirsher #define VLAN_TABLE_FID_SHIFT		12
694bcc9736cSJeff Kirsher #define VLAN_TABLE_MEMBERSHIP_SHIFT	16
695bcc9736cSJeff Kirsher 
696bcc9736cSJeff Kirsher /*
697bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_ADDR		00-0000FFFF-FFFFFFFF
698bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_FID		00-000F0000-00000000
699bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_SRC_PORT	00-00300000-00000000
700bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_TIMESTAMP	00-00C00000-00000000
701bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_ENTRIES	03-FF000000-00000000
702bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_MAC_EMPTY	04-00000000-00000000
703bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_RESERVED	78-00000000-00000000
704bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_NOT_READY	80-00000000-00000000
705bcc9736cSJeff Kirsher */
706bcc9736cSJeff Kirsher 
707bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_ADDR		0x0000FFFF
708bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_FID		0x000F0000
709bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_SRC_PORT	0x00300000
710bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_TIMESTAMP	0x00C00000
711bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_ENTRIES	0xFF000000
712bcc9736cSJeff Kirsher 
713bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_ENTRIES_H	0x03
714bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_MAC_EMPTY	0x04
715bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_RESERVED	0x78
716bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TABLE_NOT_READY	0x80
717bcc9736cSJeff Kirsher 
718bcc9736cSJeff Kirsher #define DYNAMIC_MAC_FID_SHIFT		16
719bcc9736cSJeff Kirsher #define DYNAMIC_MAC_SRC_PORT_SHIFT	20
720bcc9736cSJeff Kirsher #define DYNAMIC_MAC_TIMESTAMP_SHIFT	22
721bcc9736cSJeff Kirsher #define DYNAMIC_MAC_ENTRIES_SHIFT	24
722bcc9736cSJeff Kirsher #define DYNAMIC_MAC_ENTRIES_H_SHIFT	8
723bcc9736cSJeff Kirsher 
724bcc9736cSJeff Kirsher /*
725bcc9736cSJeff Kirsher #define MIB_COUNTER_VALUE		00-00000000-3FFFFFFF
726bcc9736cSJeff Kirsher #define MIB_COUNTER_VALID		00-00000000-40000000
727bcc9736cSJeff Kirsher #define MIB_COUNTER_OVERFLOW		00-00000000-80000000
728bcc9736cSJeff Kirsher */
729bcc9736cSJeff Kirsher 
730bcc9736cSJeff Kirsher #define MIB_COUNTER_VALUE		0x3FFFFFFF
731bcc9736cSJeff Kirsher #define MIB_COUNTER_VALID		0x40000000
732bcc9736cSJeff Kirsher #define MIB_COUNTER_OVERFLOW		0x80000000
733bcc9736cSJeff Kirsher 
734bcc9736cSJeff Kirsher #define MIB_PACKET_DROPPED		0x0000FFFF
735bcc9736cSJeff Kirsher 
736bcc9736cSJeff Kirsher #define KS_MIB_PACKET_DROPPED_TX_0	0x100
737bcc9736cSJeff Kirsher #define KS_MIB_PACKET_DROPPED_TX_1	0x101
738bcc9736cSJeff Kirsher #define KS_MIB_PACKET_DROPPED_TX	0x102
739bcc9736cSJeff Kirsher #define KS_MIB_PACKET_DROPPED_RX_0	0x103
740bcc9736cSJeff Kirsher #define KS_MIB_PACKET_DROPPED_RX_1	0x104
741bcc9736cSJeff Kirsher #define KS_MIB_PACKET_DROPPED_RX	0x105
742bcc9736cSJeff Kirsher 
743bcc9736cSJeff Kirsher /* Change default LED mode. */
744bcc9736cSJeff Kirsher #define SET_DEFAULT_LED			LED_SPEED_DUPLEX_ACT
745bcc9736cSJeff Kirsher 
746bcc9736cSJeff Kirsher #define MAC_ADDR_LEN			6
747bcc9736cSJeff Kirsher #define MAC_ADDR_ORDER(i)		(MAC_ADDR_LEN - 1 - (i))
748bcc9736cSJeff Kirsher 
749bcc9736cSJeff Kirsher #define MAX_ETHERNET_BODY_SIZE		1500
750bcc9736cSJeff Kirsher #define ETHERNET_HEADER_SIZE		14
751bcc9736cSJeff Kirsher 
752bcc9736cSJeff Kirsher #define MAX_ETHERNET_PACKET_SIZE	\
753bcc9736cSJeff Kirsher 	(MAX_ETHERNET_BODY_SIZE + ETHERNET_HEADER_SIZE)
754bcc9736cSJeff Kirsher 
755bcc9736cSJeff Kirsher #define REGULAR_RX_BUF_SIZE		(MAX_ETHERNET_PACKET_SIZE + 4)
756bcc9736cSJeff Kirsher #define MAX_RX_BUF_SIZE			(1912 + 4)
757bcc9736cSJeff Kirsher 
758bcc9736cSJeff Kirsher #define ADDITIONAL_ENTRIES		16
759bcc9736cSJeff Kirsher #define MAX_MULTICAST_LIST		32
760bcc9736cSJeff Kirsher 
761bcc9736cSJeff Kirsher #define HW_MULTICAST_SIZE		8
762bcc9736cSJeff Kirsher 
763bcc9736cSJeff Kirsher #define HW_TO_DEV_PORT(port)		(port - 1)
764bcc9736cSJeff Kirsher 
765bcc9736cSJeff Kirsher enum {
766bcc9736cSJeff Kirsher 	media_connected,
767bcc9736cSJeff Kirsher 	media_disconnected
768bcc9736cSJeff Kirsher };
769bcc9736cSJeff Kirsher 
770bcc9736cSJeff Kirsher enum {
771bcc9736cSJeff Kirsher 	OID_COUNTER_UNKOWN,
772bcc9736cSJeff Kirsher 
773bcc9736cSJeff Kirsher 	OID_COUNTER_FIRST,
774bcc9736cSJeff Kirsher 
775bcc9736cSJeff Kirsher 	/* total transmit errors */
776bcc9736cSJeff Kirsher 	OID_COUNTER_XMIT_ERROR,
777bcc9736cSJeff Kirsher 
778bcc9736cSJeff Kirsher 	/* total receive errors */
779bcc9736cSJeff Kirsher 	OID_COUNTER_RCV_ERROR,
780bcc9736cSJeff Kirsher 
781bcc9736cSJeff Kirsher 	OID_COUNTER_LAST
782bcc9736cSJeff Kirsher };
783bcc9736cSJeff Kirsher 
784bcc9736cSJeff Kirsher /*
785bcc9736cSJeff Kirsher  * Hardware descriptor definitions
786bcc9736cSJeff Kirsher  */
787bcc9736cSJeff Kirsher 
788bcc9736cSJeff Kirsher #define DESC_ALIGNMENT			16
789bcc9736cSJeff Kirsher #define BUFFER_ALIGNMENT		8
790bcc9736cSJeff Kirsher 
791bcc9736cSJeff Kirsher #define NUM_OF_RX_DESC			64
792bcc9736cSJeff Kirsher #define NUM_OF_TX_DESC			64
793bcc9736cSJeff Kirsher 
794bcc9736cSJeff Kirsher #define KS_DESC_RX_FRAME_LEN		0x000007FF
795bcc9736cSJeff Kirsher #define KS_DESC_RX_FRAME_TYPE		0x00008000
796bcc9736cSJeff Kirsher #define KS_DESC_RX_ERROR_CRC		0x00010000
797bcc9736cSJeff Kirsher #define KS_DESC_RX_ERROR_RUNT		0x00020000
798bcc9736cSJeff Kirsher #define KS_DESC_RX_ERROR_TOO_LONG	0x00040000
799bcc9736cSJeff Kirsher #define KS_DESC_RX_ERROR_PHY		0x00080000
800bcc9736cSJeff Kirsher #define KS884X_DESC_RX_PORT_MASK	0x00300000
801bcc9736cSJeff Kirsher #define KS_DESC_RX_MULTICAST		0x01000000
802bcc9736cSJeff Kirsher #define KS_DESC_RX_ERROR		0x02000000
803bcc9736cSJeff Kirsher #define KS_DESC_RX_ERROR_CSUM_UDP	0x04000000
804bcc9736cSJeff Kirsher #define KS_DESC_RX_ERROR_CSUM_TCP	0x08000000
805bcc9736cSJeff Kirsher #define KS_DESC_RX_ERROR_CSUM_IP	0x10000000
806bcc9736cSJeff Kirsher #define KS_DESC_RX_LAST			0x20000000
807bcc9736cSJeff Kirsher #define KS_DESC_RX_FIRST		0x40000000
808bcc9736cSJeff Kirsher #define KS_DESC_RX_ERROR_COND		\
809bcc9736cSJeff Kirsher 	(KS_DESC_RX_ERROR_CRC |		\
810bcc9736cSJeff Kirsher 	KS_DESC_RX_ERROR_RUNT |		\
811bcc9736cSJeff Kirsher 	KS_DESC_RX_ERROR_PHY |		\
812bcc9736cSJeff Kirsher 	KS_DESC_RX_ERROR_TOO_LONG)
813bcc9736cSJeff Kirsher 
814bcc9736cSJeff Kirsher #define KS_DESC_HW_OWNED		0x80000000
815bcc9736cSJeff Kirsher 
816bcc9736cSJeff Kirsher #define KS_DESC_BUF_SIZE		0x000007FF
817bcc9736cSJeff Kirsher #define KS884X_DESC_TX_PORT_MASK	0x00300000
818bcc9736cSJeff Kirsher #define KS_DESC_END_OF_RING		0x02000000
819bcc9736cSJeff Kirsher #define KS_DESC_TX_CSUM_GEN_UDP		0x04000000
820bcc9736cSJeff Kirsher #define KS_DESC_TX_CSUM_GEN_TCP		0x08000000
821bcc9736cSJeff Kirsher #define KS_DESC_TX_CSUM_GEN_IP		0x10000000
822bcc9736cSJeff Kirsher #define KS_DESC_TX_LAST			0x20000000
823bcc9736cSJeff Kirsher #define KS_DESC_TX_FIRST		0x40000000
824bcc9736cSJeff Kirsher #define KS_DESC_TX_INTERRUPT		0x80000000
825bcc9736cSJeff Kirsher 
826bcc9736cSJeff Kirsher #define KS_DESC_PORT_SHIFT		20
827bcc9736cSJeff Kirsher 
828bcc9736cSJeff Kirsher #define KS_DESC_RX_MASK			(KS_DESC_BUF_SIZE)
829bcc9736cSJeff Kirsher 
830bcc9736cSJeff Kirsher #define KS_DESC_TX_MASK			\
831bcc9736cSJeff Kirsher 	(KS_DESC_TX_INTERRUPT |		\
832bcc9736cSJeff Kirsher 	KS_DESC_TX_FIRST |		\
833bcc9736cSJeff Kirsher 	KS_DESC_TX_LAST |		\
834bcc9736cSJeff Kirsher 	KS_DESC_TX_CSUM_GEN_IP |	\
835bcc9736cSJeff Kirsher 	KS_DESC_TX_CSUM_GEN_TCP |	\
836bcc9736cSJeff Kirsher 	KS_DESC_TX_CSUM_GEN_UDP |	\
837bcc9736cSJeff Kirsher 	KS_DESC_BUF_SIZE)
838bcc9736cSJeff Kirsher 
839bcc9736cSJeff Kirsher struct ksz_desc_rx_stat {
840bcc9736cSJeff Kirsher #ifdef __BIG_ENDIAN_BITFIELD
841bcc9736cSJeff Kirsher 	u32 hw_owned:1;
842bcc9736cSJeff Kirsher 	u32 first_desc:1;
843bcc9736cSJeff Kirsher 	u32 last_desc:1;
844bcc9736cSJeff Kirsher 	u32 csum_err_ip:1;
845bcc9736cSJeff Kirsher 	u32 csum_err_tcp:1;
846bcc9736cSJeff Kirsher 	u32 csum_err_udp:1;
847bcc9736cSJeff Kirsher 	u32 error:1;
848bcc9736cSJeff Kirsher 	u32 multicast:1;
849bcc9736cSJeff Kirsher 	u32 src_port:4;
850bcc9736cSJeff Kirsher 	u32 err_phy:1;
851bcc9736cSJeff Kirsher 	u32 err_too_long:1;
852bcc9736cSJeff Kirsher 	u32 err_runt:1;
853bcc9736cSJeff Kirsher 	u32 err_crc:1;
854bcc9736cSJeff Kirsher 	u32 frame_type:1;
855bcc9736cSJeff Kirsher 	u32 reserved1:4;
856bcc9736cSJeff Kirsher 	u32 frame_len:11;
857bcc9736cSJeff Kirsher #else
858bcc9736cSJeff Kirsher 	u32 frame_len:11;
859bcc9736cSJeff Kirsher 	u32 reserved1:4;
860bcc9736cSJeff Kirsher 	u32 frame_type:1;
861bcc9736cSJeff Kirsher 	u32 err_crc:1;
862bcc9736cSJeff Kirsher 	u32 err_runt:1;
863bcc9736cSJeff Kirsher 	u32 err_too_long:1;
864bcc9736cSJeff Kirsher 	u32 err_phy:1;
865bcc9736cSJeff Kirsher 	u32 src_port:4;
866bcc9736cSJeff Kirsher 	u32 multicast:1;
867bcc9736cSJeff Kirsher 	u32 error:1;
868bcc9736cSJeff Kirsher 	u32 csum_err_udp:1;
869bcc9736cSJeff Kirsher 	u32 csum_err_tcp:1;
870bcc9736cSJeff Kirsher 	u32 csum_err_ip:1;
871bcc9736cSJeff Kirsher 	u32 last_desc:1;
872bcc9736cSJeff Kirsher 	u32 first_desc:1;
873bcc9736cSJeff Kirsher 	u32 hw_owned:1;
874bcc9736cSJeff Kirsher #endif
875bcc9736cSJeff Kirsher };
876bcc9736cSJeff Kirsher 
877bcc9736cSJeff Kirsher struct ksz_desc_tx_stat {
878bcc9736cSJeff Kirsher #ifdef __BIG_ENDIAN_BITFIELD
879bcc9736cSJeff Kirsher 	u32 hw_owned:1;
880bcc9736cSJeff Kirsher 	u32 reserved1:31;
881bcc9736cSJeff Kirsher #else
882bcc9736cSJeff Kirsher 	u32 reserved1:31;
883bcc9736cSJeff Kirsher 	u32 hw_owned:1;
884bcc9736cSJeff Kirsher #endif
885bcc9736cSJeff Kirsher };
886bcc9736cSJeff Kirsher 
887bcc9736cSJeff Kirsher struct ksz_desc_rx_buf {
888bcc9736cSJeff Kirsher #ifdef __BIG_ENDIAN_BITFIELD
889bcc9736cSJeff Kirsher 	u32 reserved4:6;
890bcc9736cSJeff Kirsher 	u32 end_of_ring:1;
891bcc9736cSJeff Kirsher 	u32 reserved3:14;
892bcc9736cSJeff Kirsher 	u32 buf_size:11;
893bcc9736cSJeff Kirsher #else
894bcc9736cSJeff Kirsher 	u32 buf_size:11;
895bcc9736cSJeff Kirsher 	u32 reserved3:14;
896bcc9736cSJeff Kirsher 	u32 end_of_ring:1;
897bcc9736cSJeff Kirsher 	u32 reserved4:6;
898bcc9736cSJeff Kirsher #endif
899bcc9736cSJeff Kirsher };
900bcc9736cSJeff Kirsher 
901bcc9736cSJeff Kirsher struct ksz_desc_tx_buf {
902bcc9736cSJeff Kirsher #ifdef __BIG_ENDIAN_BITFIELD
903bcc9736cSJeff Kirsher 	u32 intr:1;
904bcc9736cSJeff Kirsher 	u32 first_seg:1;
905bcc9736cSJeff Kirsher 	u32 last_seg:1;
906bcc9736cSJeff Kirsher 	u32 csum_gen_ip:1;
907bcc9736cSJeff Kirsher 	u32 csum_gen_tcp:1;
908bcc9736cSJeff Kirsher 	u32 csum_gen_udp:1;
909bcc9736cSJeff Kirsher 	u32 end_of_ring:1;
910bcc9736cSJeff Kirsher 	u32 reserved4:1;
911bcc9736cSJeff Kirsher 	u32 dest_port:4;
912bcc9736cSJeff Kirsher 	u32 reserved3:9;
913bcc9736cSJeff Kirsher 	u32 buf_size:11;
914bcc9736cSJeff Kirsher #else
915bcc9736cSJeff Kirsher 	u32 buf_size:11;
916bcc9736cSJeff Kirsher 	u32 reserved3:9;
917bcc9736cSJeff Kirsher 	u32 dest_port:4;
918bcc9736cSJeff Kirsher 	u32 reserved4:1;
919bcc9736cSJeff Kirsher 	u32 end_of_ring:1;
920bcc9736cSJeff Kirsher 	u32 csum_gen_udp:1;
921bcc9736cSJeff Kirsher 	u32 csum_gen_tcp:1;
922bcc9736cSJeff Kirsher 	u32 csum_gen_ip:1;
923bcc9736cSJeff Kirsher 	u32 last_seg:1;
924bcc9736cSJeff Kirsher 	u32 first_seg:1;
925bcc9736cSJeff Kirsher 	u32 intr:1;
926bcc9736cSJeff Kirsher #endif
927bcc9736cSJeff Kirsher };
928bcc9736cSJeff Kirsher 
929bcc9736cSJeff Kirsher union desc_stat {
930bcc9736cSJeff Kirsher 	struct ksz_desc_rx_stat rx;
931bcc9736cSJeff Kirsher 	struct ksz_desc_tx_stat tx;
932bcc9736cSJeff Kirsher 	u32 data;
933bcc9736cSJeff Kirsher };
934bcc9736cSJeff Kirsher 
935bcc9736cSJeff Kirsher union desc_buf {
936bcc9736cSJeff Kirsher 	struct ksz_desc_rx_buf rx;
937bcc9736cSJeff Kirsher 	struct ksz_desc_tx_buf tx;
938bcc9736cSJeff Kirsher 	u32 data;
939bcc9736cSJeff Kirsher };
940bcc9736cSJeff Kirsher 
941bcc9736cSJeff Kirsher /**
942bcc9736cSJeff Kirsher  * struct ksz_hw_desc - Hardware descriptor data structure
943bcc9736cSJeff Kirsher  * @ctrl:	Descriptor control value.
944bcc9736cSJeff Kirsher  * @buf:	Descriptor buffer value.
945bcc9736cSJeff Kirsher  * @addr:	Physical address of memory buffer.
946bcc9736cSJeff Kirsher  * @next:	Pointer to next hardware descriptor.
947bcc9736cSJeff Kirsher  */
948bcc9736cSJeff Kirsher struct ksz_hw_desc {
949bcc9736cSJeff Kirsher 	union desc_stat ctrl;
950bcc9736cSJeff Kirsher 	union desc_buf buf;
951bcc9736cSJeff Kirsher 	u32 addr;
952bcc9736cSJeff Kirsher 	u32 next;
953bcc9736cSJeff Kirsher };
954bcc9736cSJeff Kirsher 
955bcc9736cSJeff Kirsher /**
956bcc9736cSJeff Kirsher  * struct ksz_sw_desc - Software descriptor data structure
957bcc9736cSJeff Kirsher  * @ctrl:	Descriptor control value.
958bcc9736cSJeff Kirsher  * @buf:	Descriptor buffer value.
959bcc9736cSJeff Kirsher  * @buf_size:	Current buffers size value in hardware descriptor.
960bcc9736cSJeff Kirsher  */
961bcc9736cSJeff Kirsher struct ksz_sw_desc {
962bcc9736cSJeff Kirsher 	union desc_stat ctrl;
963bcc9736cSJeff Kirsher 	union desc_buf buf;
964bcc9736cSJeff Kirsher 	u32 buf_size;
965bcc9736cSJeff Kirsher };
966bcc9736cSJeff Kirsher 
967bcc9736cSJeff Kirsher /**
968bcc9736cSJeff Kirsher  * struct ksz_dma_buf - OS dependent DMA buffer data structure
969bcc9736cSJeff Kirsher  * @skb:	Associated socket buffer.
970bcc9736cSJeff Kirsher  * @dma:	Associated physical DMA address.
971bcc9736cSJeff Kirsher  * len:		Actual len used.
972bcc9736cSJeff Kirsher  */
973bcc9736cSJeff Kirsher struct ksz_dma_buf {
974bcc9736cSJeff Kirsher 	struct sk_buff *skb;
975bcc9736cSJeff Kirsher 	dma_addr_t dma;
976bcc9736cSJeff Kirsher 	int len;
977bcc9736cSJeff Kirsher };
978bcc9736cSJeff Kirsher 
979bcc9736cSJeff Kirsher /**
980bcc9736cSJeff Kirsher  * struct ksz_desc - Descriptor structure
981bcc9736cSJeff Kirsher  * @phw:	Hardware descriptor pointer to uncached physical memory.
982bcc9736cSJeff Kirsher  * @sw:		Cached memory to hold hardware descriptor values for
983bcc9736cSJeff Kirsher  * 		manipulation.
984bcc9736cSJeff Kirsher  * @dma_buf:	Operating system dependent data structure to hold physical
985bcc9736cSJeff Kirsher  * 		memory buffer allocation information.
986bcc9736cSJeff Kirsher  */
987bcc9736cSJeff Kirsher struct ksz_desc {
988bcc9736cSJeff Kirsher 	struct ksz_hw_desc *phw;
989bcc9736cSJeff Kirsher 	struct ksz_sw_desc sw;
990bcc9736cSJeff Kirsher 	struct ksz_dma_buf dma_buf;
991bcc9736cSJeff Kirsher };
992bcc9736cSJeff Kirsher 
993bcc9736cSJeff Kirsher #define DMA_BUFFER(desc)  ((struct ksz_dma_buf *)(&(desc)->dma_buf))
994bcc9736cSJeff Kirsher 
995bcc9736cSJeff Kirsher /**
996bcc9736cSJeff Kirsher  * struct ksz_desc_info - Descriptor information data structure
997bcc9736cSJeff Kirsher  * @ring:	First descriptor in the ring.
998bcc9736cSJeff Kirsher  * @cur:	Current descriptor being manipulated.
999bcc9736cSJeff Kirsher  * @ring_virt:	First hardware descriptor in the ring.
1000bcc9736cSJeff Kirsher  * @ring_phys:	The physical address of the first descriptor of the ring.
1001bcc9736cSJeff Kirsher  * @size:	Size of hardware descriptor.
1002bcc9736cSJeff Kirsher  * @alloc:	Number of descriptors allocated.
1003bcc9736cSJeff Kirsher  * @avail:	Number of descriptors available for use.
1004bcc9736cSJeff Kirsher  * @last:	Index for last descriptor released to hardware.
1005bcc9736cSJeff Kirsher  * @next:	Index for next descriptor available for use.
1006bcc9736cSJeff Kirsher  * @mask:	Mask for index wrapping.
1007bcc9736cSJeff Kirsher  */
1008bcc9736cSJeff Kirsher struct ksz_desc_info {
1009bcc9736cSJeff Kirsher 	struct ksz_desc *ring;
1010bcc9736cSJeff Kirsher 	struct ksz_desc *cur;
1011bcc9736cSJeff Kirsher 	struct ksz_hw_desc *ring_virt;
1012bcc9736cSJeff Kirsher 	u32 ring_phys;
1013bcc9736cSJeff Kirsher 	int size;
1014bcc9736cSJeff Kirsher 	int alloc;
1015bcc9736cSJeff Kirsher 	int avail;
1016bcc9736cSJeff Kirsher 	int last;
1017bcc9736cSJeff Kirsher 	int next;
1018bcc9736cSJeff Kirsher 	int mask;
1019bcc9736cSJeff Kirsher };
1020bcc9736cSJeff Kirsher 
1021bcc9736cSJeff Kirsher /*
1022bcc9736cSJeff Kirsher  * KSZ8842 switch definitions
1023bcc9736cSJeff Kirsher  */
1024bcc9736cSJeff Kirsher 
1025bcc9736cSJeff Kirsher enum {
1026bcc9736cSJeff Kirsher 	TABLE_STATIC_MAC = 0,
1027bcc9736cSJeff Kirsher 	TABLE_VLAN,
1028bcc9736cSJeff Kirsher 	TABLE_DYNAMIC_MAC,
1029bcc9736cSJeff Kirsher 	TABLE_MIB
1030bcc9736cSJeff Kirsher };
1031bcc9736cSJeff Kirsher 
1032bcc9736cSJeff Kirsher #define LEARNED_MAC_TABLE_ENTRIES	1024
1033bcc9736cSJeff Kirsher #define STATIC_MAC_TABLE_ENTRIES	8
1034bcc9736cSJeff Kirsher 
1035bcc9736cSJeff Kirsher /**
1036bcc9736cSJeff Kirsher  * struct ksz_mac_table - Static MAC table data structure
1037bcc9736cSJeff Kirsher  * @mac_addr:	MAC address to filter.
1038bcc9736cSJeff Kirsher  * @vid:	VID value.
1039bcc9736cSJeff Kirsher  * @fid:	FID value.
1040bcc9736cSJeff Kirsher  * @ports:	Port membership.
1041bcc9736cSJeff Kirsher  * @override:	Override setting.
1042bcc9736cSJeff Kirsher  * @use_fid:	FID use setting.
1043bcc9736cSJeff Kirsher  * @valid:	Valid setting indicating the entry is being used.
1044bcc9736cSJeff Kirsher  */
1045bcc9736cSJeff Kirsher struct ksz_mac_table {
1046bcc9736cSJeff Kirsher 	u8 mac_addr[MAC_ADDR_LEN];
1047bcc9736cSJeff Kirsher 	u16 vid;
1048bcc9736cSJeff Kirsher 	u8 fid;
1049bcc9736cSJeff Kirsher 	u8 ports;
1050bcc9736cSJeff Kirsher 	u8 override:1;
1051bcc9736cSJeff Kirsher 	u8 use_fid:1;
1052bcc9736cSJeff Kirsher 	u8 valid:1;
1053bcc9736cSJeff Kirsher };
1054bcc9736cSJeff Kirsher 
1055bcc9736cSJeff Kirsher #define VLAN_TABLE_ENTRIES		16
1056bcc9736cSJeff Kirsher 
1057bcc9736cSJeff Kirsher /**
1058bcc9736cSJeff Kirsher  * struct ksz_vlan_table - VLAN table data structure
1059bcc9736cSJeff Kirsher  * @vid:	VID value.
1060bcc9736cSJeff Kirsher  * @fid:	FID value.
1061bcc9736cSJeff Kirsher  * @member:	Port membership.
1062bcc9736cSJeff Kirsher  */
1063bcc9736cSJeff Kirsher struct ksz_vlan_table {
1064bcc9736cSJeff Kirsher 	u16 vid;
1065bcc9736cSJeff Kirsher 	u8 fid;
1066bcc9736cSJeff Kirsher 	u8 member;
1067bcc9736cSJeff Kirsher };
1068bcc9736cSJeff Kirsher 
1069bcc9736cSJeff Kirsher #define DIFFSERV_ENTRIES		64
1070bcc9736cSJeff Kirsher #define PRIO_802_1P_ENTRIES		8
1071bcc9736cSJeff Kirsher #define PRIO_QUEUES			4
1072bcc9736cSJeff Kirsher 
1073bcc9736cSJeff Kirsher #define SWITCH_PORT_NUM			2
1074bcc9736cSJeff Kirsher #define TOTAL_PORT_NUM			(SWITCH_PORT_NUM + 1)
1075bcc9736cSJeff Kirsher #define HOST_MASK			(1 << SWITCH_PORT_NUM)
1076bcc9736cSJeff Kirsher #define PORT_MASK			7
1077bcc9736cSJeff Kirsher 
1078bcc9736cSJeff Kirsher #define MAIN_PORT			0
1079bcc9736cSJeff Kirsher #define OTHER_PORT			1
1080bcc9736cSJeff Kirsher #define HOST_PORT			SWITCH_PORT_NUM
1081bcc9736cSJeff Kirsher 
1082bcc9736cSJeff Kirsher #define PORT_COUNTER_NUM		0x20
1083bcc9736cSJeff Kirsher #define TOTAL_PORT_COUNTER_NUM		(PORT_COUNTER_NUM + 2)
1084bcc9736cSJeff Kirsher 
1085bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_LO_PRIORITY	0x00
1086bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_HI_PRIORITY	0x01
1087bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_UNDERSIZE	0x02
1088bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_FRAGMENT		0x03
1089bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_OVERSIZE		0x04
1090bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_JABBER		0x05
1091bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_SYMBOL_ERR	0x06
1092bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_CRC_ERR		0x07
1093bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_ALIGNMENT_ERR	0x08
1094bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_CTRL_8808	0x09
1095bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_PAUSE		0x0A
1096bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_BROADCAST	0x0B
1097bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_MULTICAST	0x0C
1098bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_UNICAST		0x0D
1099bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_OCTET_64		0x0E
1100bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_OCTET_65_127	0x0F
1101bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_OCTET_128_255	0x10
1102bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_OCTET_256_511	0x11
1103bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_OCTET_512_1023	0x12
1104bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_OCTET_1024_1522	0x13
1105bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_LO_PRIORITY	0x14
1106bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_HI_PRIORITY	0x15
1107bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_LATE_COLLISION	0x16
1108bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_PAUSE		0x17
1109bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_BROADCAST	0x18
1110bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_MULTICAST	0x19
1111bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_UNICAST		0x1A
1112bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_DEFERRED		0x1B
1113bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_TOTAL_COLLISION	0x1C
1114bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_EXCESS_COLLISION	0x1D
1115bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_SINGLE_COLLISION	0x1E
1116bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_MULTI_COLLISION	0x1F
1117bcc9736cSJeff Kirsher 
1118bcc9736cSJeff Kirsher #define MIB_COUNTER_RX_DROPPED_PACKET	0x20
1119bcc9736cSJeff Kirsher #define MIB_COUNTER_TX_DROPPED_PACKET	0x21
1120bcc9736cSJeff Kirsher 
1121bcc9736cSJeff Kirsher /**
1122bcc9736cSJeff Kirsher  * struct ksz_port_mib - Port MIB data structure
1123bcc9736cSJeff Kirsher  * @cnt_ptr:	Current pointer to MIB counter index.
1124bcc9736cSJeff Kirsher  * @link_down:	Indication the link has just gone down.
1125bcc9736cSJeff Kirsher  * @state:	Connection status of the port.
1126bcc9736cSJeff Kirsher  * @mib_start:	The starting counter index.  Some ports do not start at 0.
1127bcc9736cSJeff Kirsher  * @counter:	64-bit MIB counter value.
1128bcc9736cSJeff Kirsher  * @dropped:	Temporary buffer to remember last read packet dropped values.
1129bcc9736cSJeff Kirsher  *
1130bcc9736cSJeff Kirsher  * MIB counters needs to be read periodically so that counters do not get
1131bcc9736cSJeff Kirsher  * overflowed and give incorrect values.  A right balance is needed to
1132bcc9736cSJeff Kirsher  * satisfy this condition and not waste too much CPU time.
1133bcc9736cSJeff Kirsher  *
1134bcc9736cSJeff Kirsher  * It is pointless to read MIB counters when the port is disconnected.  The
1135bcc9736cSJeff Kirsher  * @state provides the connection status so that MIB counters are read only
1136bcc9736cSJeff Kirsher  * when the port is connected.  The @link_down indicates the port is just
1137bcc9736cSJeff Kirsher  * disconnected so that all MIB counters are read one last time to update the
1138bcc9736cSJeff Kirsher  * information.
1139bcc9736cSJeff Kirsher  */
1140bcc9736cSJeff Kirsher struct ksz_port_mib {
1141bcc9736cSJeff Kirsher 	u8 cnt_ptr;
1142bcc9736cSJeff Kirsher 	u8 link_down;
1143bcc9736cSJeff Kirsher 	u8 state;
1144bcc9736cSJeff Kirsher 	u8 mib_start;
1145bcc9736cSJeff Kirsher 
1146bcc9736cSJeff Kirsher 	u64 counter[TOTAL_PORT_COUNTER_NUM];
1147bcc9736cSJeff Kirsher 	u32 dropped[2];
1148bcc9736cSJeff Kirsher };
1149bcc9736cSJeff Kirsher 
1150bcc9736cSJeff Kirsher /**
1151bcc9736cSJeff Kirsher  * struct ksz_port_cfg - Port configuration data structure
1152bcc9736cSJeff Kirsher  * @vid:	VID value.
1153bcc9736cSJeff Kirsher  * @member:	Port membership.
1154bcc9736cSJeff Kirsher  * @port_prio:	Port priority.
1155bcc9736cSJeff Kirsher  * @rx_rate:	Receive priority rate.
1156bcc9736cSJeff Kirsher  * @tx_rate:	Transmit priority rate.
1157bcc9736cSJeff Kirsher  * @stp_state:	Current Spanning Tree Protocol state.
1158bcc9736cSJeff Kirsher  */
1159bcc9736cSJeff Kirsher struct ksz_port_cfg {
1160bcc9736cSJeff Kirsher 	u16 vid;
1161bcc9736cSJeff Kirsher 	u8 member;
1162bcc9736cSJeff Kirsher 	u8 port_prio;
1163bcc9736cSJeff Kirsher 	u32 rx_rate[PRIO_QUEUES];
1164bcc9736cSJeff Kirsher 	u32 tx_rate[PRIO_QUEUES];
1165bcc9736cSJeff Kirsher 	int stp_state;
1166bcc9736cSJeff Kirsher };
1167bcc9736cSJeff Kirsher 
1168bcc9736cSJeff Kirsher /**
1169bcc9736cSJeff Kirsher  * struct ksz_switch - KSZ8842 switch data structure
1170bcc9736cSJeff Kirsher  * @mac_table:	MAC table entries information.
1171bcc9736cSJeff Kirsher  * @vlan_table:	VLAN table entries information.
1172bcc9736cSJeff Kirsher  * @port_cfg:	Port configuration information.
1173bcc9736cSJeff Kirsher  * @diffserv:	DiffServ priority settings.  Possible values from 6-bit of ToS
1174bcc9736cSJeff Kirsher  * 		(bit7 ~ bit2) field.
1175bcc9736cSJeff Kirsher  * @p_802_1p:	802.1P priority settings.  Possible values from 3-bit of 802.1p
1176bcc9736cSJeff Kirsher  * 		Tag priority field.
1177bcc9736cSJeff Kirsher  * @br_addr:	Bridge address.  Used for STP.
1178bcc9736cSJeff Kirsher  * @other_addr:	Other MAC address.  Used for multiple network device mode.
1179bcc9736cSJeff Kirsher  * @broad_per:	Broadcast storm percentage.
1180bcc9736cSJeff Kirsher  * @member:	Current port membership.  Used for STP.
1181bcc9736cSJeff Kirsher  */
1182bcc9736cSJeff Kirsher struct ksz_switch {
1183bcc9736cSJeff Kirsher 	struct ksz_mac_table mac_table[STATIC_MAC_TABLE_ENTRIES];
1184bcc9736cSJeff Kirsher 	struct ksz_vlan_table vlan_table[VLAN_TABLE_ENTRIES];
1185bcc9736cSJeff Kirsher 	struct ksz_port_cfg port_cfg[TOTAL_PORT_NUM];
1186bcc9736cSJeff Kirsher 
1187bcc9736cSJeff Kirsher 	u8 diffserv[DIFFSERV_ENTRIES];
1188bcc9736cSJeff Kirsher 	u8 p_802_1p[PRIO_802_1P_ENTRIES];
1189bcc9736cSJeff Kirsher 
1190bcc9736cSJeff Kirsher 	u8 br_addr[MAC_ADDR_LEN];
1191bcc9736cSJeff Kirsher 	u8 other_addr[MAC_ADDR_LEN];
1192bcc9736cSJeff Kirsher 
1193bcc9736cSJeff Kirsher 	u8 broad_per;
1194bcc9736cSJeff Kirsher 	u8 member;
1195bcc9736cSJeff Kirsher };
1196bcc9736cSJeff Kirsher 
1197bcc9736cSJeff Kirsher #define TX_RATE_UNIT			10000
1198bcc9736cSJeff Kirsher 
1199bcc9736cSJeff Kirsher /**
1200bcc9736cSJeff Kirsher  * struct ksz_port_info - Port information data structure
1201bcc9736cSJeff Kirsher  * @state:	Connection status of the port.
1202bcc9736cSJeff Kirsher  * @tx_rate:	Transmit rate divided by 10000 to get Mbit.
1203bcc9736cSJeff Kirsher  * @duplex:	Duplex mode.
1204bcc9736cSJeff Kirsher  * @advertised:	Advertised auto-negotiation setting.  Used to determine link.
1205bcc9736cSJeff Kirsher  * @partner:	Auto-negotiation partner setting.  Used to determine link.
1206bcc9736cSJeff Kirsher  * @port_id:	Port index to access actual hardware register.
1207bcc9736cSJeff Kirsher  * @pdev:	Pointer to OS dependent network device.
1208bcc9736cSJeff Kirsher  */
1209bcc9736cSJeff Kirsher struct ksz_port_info {
1210bcc9736cSJeff Kirsher 	uint state;
1211bcc9736cSJeff Kirsher 	uint tx_rate;
1212bcc9736cSJeff Kirsher 	u8 duplex;
1213bcc9736cSJeff Kirsher 	u8 advertised;
1214bcc9736cSJeff Kirsher 	u8 partner;
1215bcc9736cSJeff Kirsher 	u8 port_id;
1216bcc9736cSJeff Kirsher 	void *pdev;
1217bcc9736cSJeff Kirsher };
1218bcc9736cSJeff Kirsher 
1219bcc9736cSJeff Kirsher #define MAX_TX_HELD_SIZE		52000
1220bcc9736cSJeff Kirsher 
1221bcc9736cSJeff Kirsher /* Hardware features and bug fixes. */
1222bcc9736cSJeff Kirsher #define LINK_INT_WORKING		(1 << 0)
1223bcc9736cSJeff Kirsher #define SMALL_PACKET_TX_BUG		(1 << 1)
1224bcc9736cSJeff Kirsher #define HALF_DUPLEX_SIGNAL_BUG		(1 << 2)
1225bcc9736cSJeff Kirsher #define RX_HUGE_FRAME			(1 << 4)
1226bcc9736cSJeff Kirsher #define STP_SUPPORT			(1 << 8)
1227bcc9736cSJeff Kirsher 
1228bcc9736cSJeff Kirsher /* Software overrides. */
1229bcc9736cSJeff Kirsher #define PAUSE_FLOW_CTRL			(1 << 0)
1230bcc9736cSJeff Kirsher #define FAST_AGING			(1 << 1)
1231bcc9736cSJeff Kirsher 
1232bcc9736cSJeff Kirsher /**
1233bcc9736cSJeff Kirsher  * struct ksz_hw - KSZ884X hardware data structure
1234bcc9736cSJeff Kirsher  * @io:			Virtual address assigned.
1235bcc9736cSJeff Kirsher  * @ksz_switch:		Pointer to KSZ8842 switch.
1236bcc9736cSJeff Kirsher  * @port_info:		Port information.
1237bcc9736cSJeff Kirsher  * @port_mib:		Port MIB information.
1238bcc9736cSJeff Kirsher  * @dev_count:		Number of network devices this hardware supports.
1239bcc9736cSJeff Kirsher  * @dst_ports:		Destination ports in switch for transmission.
1240bcc9736cSJeff Kirsher  * @id:			Hardware ID.  Used for display only.
1241bcc9736cSJeff Kirsher  * @mib_cnt:		Number of MIB counters this hardware has.
1242bcc9736cSJeff Kirsher  * @mib_port_cnt:	Number of ports with MIB counters.
1243bcc9736cSJeff Kirsher  * @tx_cfg:		Cached transmit control settings.
1244bcc9736cSJeff Kirsher  * @rx_cfg:		Cached receive control settings.
1245bcc9736cSJeff Kirsher  * @intr_mask:		Current interrupt mask.
1246bcc9736cSJeff Kirsher  * @intr_set:		Current interrup set.
1247bcc9736cSJeff Kirsher  * @intr_blocked:	Interrupt blocked.
1248bcc9736cSJeff Kirsher  * @rx_desc_info:	Receive descriptor information.
1249bcc9736cSJeff Kirsher  * @tx_desc_info:	Transmit descriptor information.
1250bcc9736cSJeff Kirsher  * @tx_int_cnt:		Transmit interrupt count.  Used for TX optimization.
1251bcc9736cSJeff Kirsher  * @tx_int_mask:	Transmit interrupt mask.  Used for TX optimization.
1252bcc9736cSJeff Kirsher  * @tx_size:		Transmit data size.  Used for TX optimization.
1253bcc9736cSJeff Kirsher  * 			The maximum is defined by MAX_TX_HELD_SIZE.
1254bcc9736cSJeff Kirsher  * @perm_addr:		Permanent MAC address.
1255bcc9736cSJeff Kirsher  * @override_addr:	Overrided MAC address.
1256bcc9736cSJeff Kirsher  * @address:		Additional MAC address entries.
1257bcc9736cSJeff Kirsher  * @addr_list_size:	Additional MAC address list size.
1258bcc9736cSJeff Kirsher  * @mac_override:	Indication of MAC address overrided.
1259bcc9736cSJeff Kirsher  * @promiscuous:	Counter to keep track of promiscuous mode set.
1260bcc9736cSJeff Kirsher  * @all_multi:		Counter to keep track of all multicast mode set.
1261bcc9736cSJeff Kirsher  * @multi_list:		Multicast address entries.
1262bcc9736cSJeff Kirsher  * @multi_bits:		Cached multicast hash table settings.
1263bcc9736cSJeff Kirsher  * @multi_list_size:	Multicast address list size.
1264bcc9736cSJeff Kirsher  * @enabled:		Indication of hardware enabled.
1265bcc9736cSJeff Kirsher  * @rx_stop:		Indication of receive process stop.
1266bcc9736cSJeff Kirsher  * @features:		Hardware features to enable.
1267bcc9736cSJeff Kirsher  * @overrides:		Hardware features to override.
1268bcc9736cSJeff Kirsher  * @parent:		Pointer to parent, network device private structure.
1269bcc9736cSJeff Kirsher  */
1270bcc9736cSJeff Kirsher struct ksz_hw {
1271bcc9736cSJeff Kirsher 	void __iomem *io;
1272bcc9736cSJeff Kirsher 
1273bcc9736cSJeff Kirsher 	struct ksz_switch *ksz_switch;
1274bcc9736cSJeff Kirsher 	struct ksz_port_info port_info[SWITCH_PORT_NUM];
1275bcc9736cSJeff Kirsher 	struct ksz_port_mib port_mib[TOTAL_PORT_NUM];
1276bcc9736cSJeff Kirsher 	int dev_count;
1277bcc9736cSJeff Kirsher 	int dst_ports;
1278bcc9736cSJeff Kirsher 	int id;
1279bcc9736cSJeff Kirsher 	int mib_cnt;
1280bcc9736cSJeff Kirsher 	int mib_port_cnt;
1281bcc9736cSJeff Kirsher 
1282bcc9736cSJeff Kirsher 	u32 tx_cfg;
1283bcc9736cSJeff Kirsher 	u32 rx_cfg;
1284bcc9736cSJeff Kirsher 	u32 intr_mask;
1285bcc9736cSJeff Kirsher 	u32 intr_set;
1286bcc9736cSJeff Kirsher 	uint intr_blocked;
1287bcc9736cSJeff Kirsher 
1288bcc9736cSJeff Kirsher 	struct ksz_desc_info rx_desc_info;
1289bcc9736cSJeff Kirsher 	struct ksz_desc_info tx_desc_info;
1290bcc9736cSJeff Kirsher 
1291bcc9736cSJeff Kirsher 	int tx_int_cnt;
1292bcc9736cSJeff Kirsher 	int tx_int_mask;
1293bcc9736cSJeff Kirsher 	int tx_size;
1294bcc9736cSJeff Kirsher 
1295bcc9736cSJeff Kirsher 	u8 perm_addr[MAC_ADDR_LEN];
1296bcc9736cSJeff Kirsher 	u8 override_addr[MAC_ADDR_LEN];
1297bcc9736cSJeff Kirsher 	u8 address[ADDITIONAL_ENTRIES][MAC_ADDR_LEN];
1298bcc9736cSJeff Kirsher 	u8 addr_list_size;
1299bcc9736cSJeff Kirsher 	u8 mac_override;
1300bcc9736cSJeff Kirsher 	u8 promiscuous;
1301bcc9736cSJeff Kirsher 	u8 all_multi;
1302bcc9736cSJeff Kirsher 	u8 multi_list[MAX_MULTICAST_LIST][MAC_ADDR_LEN];
1303bcc9736cSJeff Kirsher 	u8 multi_bits[HW_MULTICAST_SIZE];
1304bcc9736cSJeff Kirsher 	u8 multi_list_size;
1305bcc9736cSJeff Kirsher 
1306bcc9736cSJeff Kirsher 	u8 enabled;
1307bcc9736cSJeff Kirsher 	u8 rx_stop;
1308bcc9736cSJeff Kirsher 	u8 reserved2[1];
1309bcc9736cSJeff Kirsher 
1310bcc9736cSJeff Kirsher 	uint features;
1311bcc9736cSJeff Kirsher 	uint overrides;
1312bcc9736cSJeff Kirsher 
1313bcc9736cSJeff Kirsher 	void *parent;
1314bcc9736cSJeff Kirsher };
1315bcc9736cSJeff Kirsher 
1316bcc9736cSJeff Kirsher enum {
1317bcc9736cSJeff Kirsher 	PHY_NO_FLOW_CTRL,
1318bcc9736cSJeff Kirsher 	PHY_FLOW_CTRL,
1319bcc9736cSJeff Kirsher 	PHY_TX_ONLY,
1320bcc9736cSJeff Kirsher 	PHY_RX_ONLY
1321bcc9736cSJeff Kirsher };
1322bcc9736cSJeff Kirsher 
1323bcc9736cSJeff Kirsher /**
1324bcc9736cSJeff Kirsher  * struct ksz_port - Virtual port data structure
1325bcc9736cSJeff Kirsher  * @duplex:		Duplex mode setting.  1 for half duplex, 2 for full
1326bcc9736cSJeff Kirsher  * 			duplex, and 0 for auto, which normally results in full
1327bcc9736cSJeff Kirsher  * 			duplex.
1328bcc9736cSJeff Kirsher  * @speed:		Speed setting.  10 for 10 Mbit, 100 for 100 Mbit, and
1329bcc9736cSJeff Kirsher  * 			0 for auto, which normally results in 100 Mbit.
1330bcc9736cSJeff Kirsher  * @force_link:		Force link setting.  0 for auto-negotiation, and 1 for
1331bcc9736cSJeff Kirsher  * 			force.
1332bcc9736cSJeff Kirsher  * @flow_ctrl:		Flow control setting.  PHY_NO_FLOW_CTRL for no flow
1333bcc9736cSJeff Kirsher  * 			control, and PHY_FLOW_CTRL for flow control.
1334bcc9736cSJeff Kirsher  * 			PHY_TX_ONLY and PHY_RX_ONLY are not supported for 100
1335bcc9736cSJeff Kirsher  * 			Mbit PHY.
1336bcc9736cSJeff Kirsher  * @first_port:		Index of first port this port supports.
1337bcc9736cSJeff Kirsher  * @mib_port_cnt:	Number of ports with MIB counters.
1338bcc9736cSJeff Kirsher  * @port_cnt:		Number of ports this port supports.
1339bcc9736cSJeff Kirsher  * @counter:		Port statistics counter.
1340bcc9736cSJeff Kirsher  * @hw:			Pointer to hardware structure.
1341bcc9736cSJeff Kirsher  * @linked:		Pointer to port information linked to this port.
1342bcc9736cSJeff Kirsher  */
1343bcc9736cSJeff Kirsher struct ksz_port {
1344bcc9736cSJeff Kirsher 	u8 duplex;
1345bcc9736cSJeff Kirsher 	u8 speed;
1346bcc9736cSJeff Kirsher 	u8 force_link;
1347bcc9736cSJeff Kirsher 	u8 flow_ctrl;
1348bcc9736cSJeff Kirsher 
1349bcc9736cSJeff Kirsher 	int first_port;
1350bcc9736cSJeff Kirsher 	int mib_port_cnt;
1351bcc9736cSJeff Kirsher 	int port_cnt;
1352bcc9736cSJeff Kirsher 	u64 counter[OID_COUNTER_LAST];
1353bcc9736cSJeff Kirsher 
1354bcc9736cSJeff Kirsher 	struct ksz_hw *hw;
1355bcc9736cSJeff Kirsher 	struct ksz_port_info *linked;
1356bcc9736cSJeff Kirsher };
1357bcc9736cSJeff Kirsher 
1358bcc9736cSJeff Kirsher /**
1359bcc9736cSJeff Kirsher  * struct ksz_timer_info - Timer information data structure
1360bcc9736cSJeff Kirsher  * @timer:	Kernel timer.
1361bcc9736cSJeff Kirsher  * @cnt:	Running timer counter.
1362bcc9736cSJeff Kirsher  * @max:	Number of times to run timer; -1 for infinity.
1363bcc9736cSJeff Kirsher  * @period:	Timer period in jiffies.
1364bcc9736cSJeff Kirsher  */
1365bcc9736cSJeff Kirsher struct ksz_timer_info {
1366bcc9736cSJeff Kirsher 	struct timer_list timer;
1367bcc9736cSJeff Kirsher 	int cnt;
1368bcc9736cSJeff Kirsher 	int max;
1369bcc9736cSJeff Kirsher 	int period;
1370bcc9736cSJeff Kirsher };
1371bcc9736cSJeff Kirsher 
1372bcc9736cSJeff Kirsher /**
1373bcc9736cSJeff Kirsher  * struct ksz_shared_mem - OS dependent shared memory data structure
1374bcc9736cSJeff Kirsher  * @dma_addr:	Physical DMA address allocated.
1375bcc9736cSJeff Kirsher  * @alloc_size:	Allocation size.
1376bcc9736cSJeff Kirsher  * @phys:	Actual physical address used.
1377bcc9736cSJeff Kirsher  * @alloc_virt:	Virtual address allocated.
1378bcc9736cSJeff Kirsher  * @virt:	Actual virtual address used.
1379bcc9736cSJeff Kirsher  */
1380bcc9736cSJeff Kirsher struct ksz_shared_mem {
1381bcc9736cSJeff Kirsher 	dma_addr_t dma_addr;
1382bcc9736cSJeff Kirsher 	uint alloc_size;
1383bcc9736cSJeff Kirsher 	uint phys;
1384bcc9736cSJeff Kirsher 	u8 *alloc_virt;
1385bcc9736cSJeff Kirsher 	u8 *virt;
1386bcc9736cSJeff Kirsher };
1387bcc9736cSJeff Kirsher 
1388bcc9736cSJeff Kirsher /**
1389bcc9736cSJeff Kirsher  * struct ksz_counter_info - OS dependent counter information data structure
1390bcc9736cSJeff Kirsher  * @counter:	Wait queue to wakeup after counters are read.
1391bcc9736cSJeff Kirsher  * @time:	Next time in jiffies to read counter.
1392bcc9736cSJeff Kirsher  * @read:	Indication of counters read in full or not.
1393bcc9736cSJeff Kirsher  */
1394bcc9736cSJeff Kirsher struct ksz_counter_info {
1395bcc9736cSJeff Kirsher 	wait_queue_head_t counter;
1396bcc9736cSJeff Kirsher 	unsigned long time;
1397bcc9736cSJeff Kirsher 	int read;
1398bcc9736cSJeff Kirsher };
1399bcc9736cSJeff Kirsher 
1400bcc9736cSJeff Kirsher /**
1401bcc9736cSJeff Kirsher  * struct dev_info - Network device information data structure
1402bcc9736cSJeff Kirsher  * @dev:		Pointer to network device.
1403bcc9736cSJeff Kirsher  * @pdev:		Pointer to PCI device.
1404bcc9736cSJeff Kirsher  * @hw:			Hardware structure.
1405bcc9736cSJeff Kirsher  * @desc_pool:		Physical memory used for descriptor pool.
1406bcc9736cSJeff Kirsher  * @hwlock:		Spinlock to prevent hardware from accessing.
1407bcc9736cSJeff Kirsher  * @lock:		Mutex lock to prevent device from accessing.
1408bcc9736cSJeff Kirsher  * @dev_rcv:		Receive process function used.
1409bcc9736cSJeff Kirsher  * @last_skb:		Socket buffer allocated for descriptor rx fragments.
1410bcc9736cSJeff Kirsher  * @skb_index:		Buffer index for receiving fragments.
1411bcc9736cSJeff Kirsher  * @skb_len:		Buffer length for receiving fragments.
1412bcc9736cSJeff Kirsher  * @mib_read:		Workqueue to read MIB counters.
1413bcc9736cSJeff Kirsher  * @mib_timer_info:	Timer to read MIB counters.
1414bcc9736cSJeff Kirsher  * @counter:		Used for MIB reading.
1415bcc9736cSJeff Kirsher  * @mtu:		Current MTU used.  The default is REGULAR_RX_BUF_SIZE;
1416bcc9736cSJeff Kirsher  * 			the maximum is MAX_RX_BUF_SIZE.
1417bcc9736cSJeff Kirsher  * @opened:		Counter to keep track of device open.
1418bcc9736cSJeff Kirsher  * @rx_tasklet:		Receive processing tasklet.
1419bcc9736cSJeff Kirsher  * @tx_tasklet:		Transmit processing tasklet.
1420bcc9736cSJeff Kirsher  * @wol_enable:		Wake-on-LAN enable set by ethtool.
1421bcc9736cSJeff Kirsher  * @wol_support:	Wake-on-LAN support used by ethtool.
1422bcc9736cSJeff Kirsher  * @pme_wait:		Used for KSZ8841 power management.
1423bcc9736cSJeff Kirsher  */
1424bcc9736cSJeff Kirsher struct dev_info {
1425bcc9736cSJeff Kirsher 	struct net_device *dev;
1426bcc9736cSJeff Kirsher 	struct pci_dev *pdev;
1427bcc9736cSJeff Kirsher 
1428bcc9736cSJeff Kirsher 	struct ksz_hw hw;
1429bcc9736cSJeff Kirsher 	struct ksz_shared_mem desc_pool;
1430bcc9736cSJeff Kirsher 
1431bcc9736cSJeff Kirsher 	spinlock_t hwlock;
1432bcc9736cSJeff Kirsher 	struct mutex lock;
1433bcc9736cSJeff Kirsher 
1434bcc9736cSJeff Kirsher 	int (*dev_rcv)(struct dev_info *);
1435bcc9736cSJeff Kirsher 
1436bcc9736cSJeff Kirsher 	struct sk_buff *last_skb;
1437bcc9736cSJeff Kirsher 	int skb_index;
1438bcc9736cSJeff Kirsher 	int skb_len;
1439bcc9736cSJeff Kirsher 
1440bcc9736cSJeff Kirsher 	struct work_struct mib_read;
1441bcc9736cSJeff Kirsher 	struct ksz_timer_info mib_timer_info;
1442bcc9736cSJeff Kirsher 	struct ksz_counter_info counter[TOTAL_PORT_NUM];
1443bcc9736cSJeff Kirsher 
1444bcc9736cSJeff Kirsher 	int mtu;
1445bcc9736cSJeff Kirsher 	int opened;
1446bcc9736cSJeff Kirsher 
1447bcc9736cSJeff Kirsher 	struct tasklet_struct rx_tasklet;
1448bcc9736cSJeff Kirsher 	struct tasklet_struct tx_tasklet;
1449bcc9736cSJeff Kirsher 
1450bcc9736cSJeff Kirsher 	int wol_enable;
1451bcc9736cSJeff Kirsher 	int wol_support;
1452bcc9736cSJeff Kirsher 	unsigned long pme_wait;
1453bcc9736cSJeff Kirsher };
1454bcc9736cSJeff Kirsher 
1455bcc9736cSJeff Kirsher /**
1456bcc9736cSJeff Kirsher  * struct dev_priv - Network device private data structure
1457bcc9736cSJeff Kirsher  * @adapter:		Adapter device information.
1458bcc9736cSJeff Kirsher  * @port:		Port information.
1459bcc9736cSJeff Kirsher  * @monitor_time_info:	Timer to monitor ports.
1460bcc9736cSJeff Kirsher  * @proc_sem:		Semaphore for proc accessing.
1461bcc9736cSJeff Kirsher  * @id:			Device ID.
1462bcc9736cSJeff Kirsher  * @mii_if:		MII interface information.
1463bcc9736cSJeff Kirsher  * @advertising:	Temporary variable to store advertised settings.
1464bcc9736cSJeff Kirsher  * @msg_enable:		The message flags controlling driver output.
1465bcc9736cSJeff Kirsher  * @media_state:	The connection status of the device.
1466bcc9736cSJeff Kirsher  * @multicast:		The all multicast state of the device.
1467bcc9736cSJeff Kirsher  * @promiscuous:	The promiscuous state of the device.
1468bcc9736cSJeff Kirsher  */
1469bcc9736cSJeff Kirsher struct dev_priv {
1470bcc9736cSJeff Kirsher 	struct dev_info *adapter;
1471bcc9736cSJeff Kirsher 	struct ksz_port port;
1472bcc9736cSJeff Kirsher 	struct ksz_timer_info monitor_timer_info;
1473bcc9736cSJeff Kirsher 
1474bcc9736cSJeff Kirsher 	struct semaphore proc_sem;
1475bcc9736cSJeff Kirsher 	int id;
1476bcc9736cSJeff Kirsher 
1477bcc9736cSJeff Kirsher 	struct mii_if_info mii_if;
1478bcc9736cSJeff Kirsher 	u32 advertising;
1479bcc9736cSJeff Kirsher 
1480bcc9736cSJeff Kirsher 	u32 msg_enable;
1481bcc9736cSJeff Kirsher 	int media_state;
1482bcc9736cSJeff Kirsher 	int multicast;
1483bcc9736cSJeff Kirsher 	int promiscuous;
1484bcc9736cSJeff Kirsher };
1485bcc9736cSJeff Kirsher 
1486bcc9736cSJeff Kirsher #define DRV_NAME		"KSZ884X PCI"
1487bcc9736cSJeff Kirsher #define DEVICE_NAME		"KSZ884x PCI"
1488bcc9736cSJeff Kirsher #define DRV_VERSION		"1.0.0"
1489bcc9736cSJeff Kirsher #define DRV_RELDATE		"Feb 8, 2010"
1490bcc9736cSJeff Kirsher 
1491bcc9736cSJeff Kirsher static char version[] __devinitdata =
1492bcc9736cSJeff Kirsher 	"Micrel " DEVICE_NAME " " DRV_VERSION " (" DRV_RELDATE ")";
1493bcc9736cSJeff Kirsher 
1494bcc9736cSJeff Kirsher static u8 DEFAULT_MAC_ADDRESS[] = { 0x00, 0x10, 0xA1, 0x88, 0x42, 0x01 };
1495bcc9736cSJeff Kirsher 
1496bcc9736cSJeff Kirsher /*
1497bcc9736cSJeff Kirsher  * Interrupt processing primary routines
1498bcc9736cSJeff Kirsher  */
1499bcc9736cSJeff Kirsher 
1500bcc9736cSJeff Kirsher static inline void hw_ack_intr(struct ksz_hw *hw, uint interrupt)
1501bcc9736cSJeff Kirsher {
1502bcc9736cSJeff Kirsher 	writel(interrupt, hw->io + KS884X_INTERRUPTS_STATUS);
1503bcc9736cSJeff Kirsher }
1504bcc9736cSJeff Kirsher 
1505bcc9736cSJeff Kirsher static inline void hw_dis_intr(struct ksz_hw *hw)
1506bcc9736cSJeff Kirsher {
1507bcc9736cSJeff Kirsher 	hw->intr_blocked = hw->intr_mask;
1508bcc9736cSJeff Kirsher 	writel(0, hw->io + KS884X_INTERRUPTS_ENABLE);
1509bcc9736cSJeff Kirsher 	hw->intr_set = readl(hw->io + KS884X_INTERRUPTS_ENABLE);
1510bcc9736cSJeff Kirsher }
1511bcc9736cSJeff Kirsher 
1512bcc9736cSJeff Kirsher static inline void hw_set_intr(struct ksz_hw *hw, uint interrupt)
1513bcc9736cSJeff Kirsher {
1514bcc9736cSJeff Kirsher 	hw->intr_set = interrupt;
1515bcc9736cSJeff Kirsher 	writel(interrupt, hw->io + KS884X_INTERRUPTS_ENABLE);
1516bcc9736cSJeff Kirsher }
1517bcc9736cSJeff Kirsher 
1518bcc9736cSJeff Kirsher static inline void hw_ena_intr(struct ksz_hw *hw)
1519bcc9736cSJeff Kirsher {
1520bcc9736cSJeff Kirsher 	hw->intr_blocked = 0;
1521bcc9736cSJeff Kirsher 	hw_set_intr(hw, hw->intr_mask);
1522bcc9736cSJeff Kirsher }
1523bcc9736cSJeff Kirsher 
1524bcc9736cSJeff Kirsher static inline void hw_dis_intr_bit(struct ksz_hw *hw, uint bit)
1525bcc9736cSJeff Kirsher {
1526bcc9736cSJeff Kirsher 	hw->intr_mask &= ~(bit);
1527bcc9736cSJeff Kirsher }
1528bcc9736cSJeff Kirsher 
1529bcc9736cSJeff Kirsher static inline void hw_turn_off_intr(struct ksz_hw *hw, uint interrupt)
1530bcc9736cSJeff Kirsher {
1531bcc9736cSJeff Kirsher 	u32 read_intr;
1532bcc9736cSJeff Kirsher 
1533bcc9736cSJeff Kirsher 	read_intr = readl(hw->io + KS884X_INTERRUPTS_ENABLE);
1534bcc9736cSJeff Kirsher 	hw->intr_set = read_intr & ~interrupt;
1535bcc9736cSJeff Kirsher 	writel(hw->intr_set, hw->io + KS884X_INTERRUPTS_ENABLE);
1536bcc9736cSJeff Kirsher 	hw_dis_intr_bit(hw, interrupt);
1537bcc9736cSJeff Kirsher }
1538bcc9736cSJeff Kirsher 
1539bcc9736cSJeff Kirsher /**
1540bcc9736cSJeff Kirsher  * hw_turn_on_intr - turn on specified interrupts
1541bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1542bcc9736cSJeff Kirsher  * @bit:	The interrupt bits to be on.
1543bcc9736cSJeff Kirsher  *
1544bcc9736cSJeff Kirsher  * This routine turns on the specified interrupts in the interrupt mask so that
1545bcc9736cSJeff Kirsher  * those interrupts will be enabled.
1546bcc9736cSJeff Kirsher  */
1547bcc9736cSJeff Kirsher static void hw_turn_on_intr(struct ksz_hw *hw, u32 bit)
1548bcc9736cSJeff Kirsher {
1549bcc9736cSJeff Kirsher 	hw->intr_mask |= bit;
1550bcc9736cSJeff Kirsher 
1551bcc9736cSJeff Kirsher 	if (!hw->intr_blocked)
1552bcc9736cSJeff Kirsher 		hw_set_intr(hw, hw->intr_mask);
1553bcc9736cSJeff Kirsher }
1554bcc9736cSJeff Kirsher 
1555bcc9736cSJeff Kirsher static inline void hw_ena_intr_bit(struct ksz_hw *hw, uint interrupt)
1556bcc9736cSJeff Kirsher {
1557bcc9736cSJeff Kirsher 	u32 read_intr;
1558bcc9736cSJeff Kirsher 
1559bcc9736cSJeff Kirsher 	read_intr = readl(hw->io + KS884X_INTERRUPTS_ENABLE);
1560bcc9736cSJeff Kirsher 	hw->intr_set = read_intr | interrupt;
1561bcc9736cSJeff Kirsher 	writel(hw->intr_set, hw->io + KS884X_INTERRUPTS_ENABLE);
1562bcc9736cSJeff Kirsher }
1563bcc9736cSJeff Kirsher 
1564bcc9736cSJeff Kirsher static inline void hw_read_intr(struct ksz_hw *hw, uint *status)
1565bcc9736cSJeff Kirsher {
1566bcc9736cSJeff Kirsher 	*status = readl(hw->io + KS884X_INTERRUPTS_STATUS);
1567bcc9736cSJeff Kirsher 	*status = *status & hw->intr_set;
1568bcc9736cSJeff Kirsher }
1569bcc9736cSJeff Kirsher 
1570bcc9736cSJeff Kirsher static inline void hw_restore_intr(struct ksz_hw *hw, uint interrupt)
1571bcc9736cSJeff Kirsher {
1572bcc9736cSJeff Kirsher 	if (interrupt)
1573bcc9736cSJeff Kirsher 		hw_ena_intr(hw);
1574bcc9736cSJeff Kirsher }
1575bcc9736cSJeff Kirsher 
1576bcc9736cSJeff Kirsher /**
1577bcc9736cSJeff Kirsher  * hw_block_intr - block hardware interrupts
1578bcc9736cSJeff Kirsher  *
1579bcc9736cSJeff Kirsher  * This function blocks all interrupts of the hardware and returns the current
1580bcc9736cSJeff Kirsher  * interrupt enable mask so that interrupts can be restored later.
1581bcc9736cSJeff Kirsher  *
1582bcc9736cSJeff Kirsher  * Return the current interrupt enable mask.
1583bcc9736cSJeff Kirsher  */
1584bcc9736cSJeff Kirsher static uint hw_block_intr(struct ksz_hw *hw)
1585bcc9736cSJeff Kirsher {
1586bcc9736cSJeff Kirsher 	uint interrupt = 0;
1587bcc9736cSJeff Kirsher 
1588bcc9736cSJeff Kirsher 	if (!hw->intr_blocked) {
1589bcc9736cSJeff Kirsher 		hw_dis_intr(hw);
1590bcc9736cSJeff Kirsher 		interrupt = hw->intr_blocked;
1591bcc9736cSJeff Kirsher 	}
1592bcc9736cSJeff Kirsher 	return interrupt;
1593bcc9736cSJeff Kirsher }
1594bcc9736cSJeff Kirsher 
1595bcc9736cSJeff Kirsher /*
1596bcc9736cSJeff Kirsher  * Hardware descriptor routines
1597bcc9736cSJeff Kirsher  */
1598bcc9736cSJeff Kirsher 
1599bcc9736cSJeff Kirsher static inline void reset_desc(struct ksz_desc *desc, union desc_stat status)
1600bcc9736cSJeff Kirsher {
1601bcc9736cSJeff Kirsher 	status.rx.hw_owned = 0;
1602bcc9736cSJeff Kirsher 	desc->phw->ctrl.data = cpu_to_le32(status.data);
1603bcc9736cSJeff Kirsher }
1604bcc9736cSJeff Kirsher 
1605bcc9736cSJeff Kirsher static inline void release_desc(struct ksz_desc *desc)
1606bcc9736cSJeff Kirsher {
1607bcc9736cSJeff Kirsher 	desc->sw.ctrl.tx.hw_owned = 1;
1608bcc9736cSJeff Kirsher 	if (desc->sw.buf_size != desc->sw.buf.data) {
1609bcc9736cSJeff Kirsher 		desc->sw.buf_size = desc->sw.buf.data;
1610bcc9736cSJeff Kirsher 		desc->phw->buf.data = cpu_to_le32(desc->sw.buf.data);
1611bcc9736cSJeff Kirsher 	}
1612bcc9736cSJeff Kirsher 	desc->phw->ctrl.data = cpu_to_le32(desc->sw.ctrl.data);
1613bcc9736cSJeff Kirsher }
1614bcc9736cSJeff Kirsher 
1615bcc9736cSJeff Kirsher static void get_rx_pkt(struct ksz_desc_info *info, struct ksz_desc **desc)
1616bcc9736cSJeff Kirsher {
1617bcc9736cSJeff Kirsher 	*desc = &info->ring[info->last];
1618bcc9736cSJeff Kirsher 	info->last++;
1619bcc9736cSJeff Kirsher 	info->last &= info->mask;
1620bcc9736cSJeff Kirsher 	info->avail--;
1621bcc9736cSJeff Kirsher 	(*desc)->sw.buf.data &= ~KS_DESC_RX_MASK;
1622bcc9736cSJeff Kirsher }
1623bcc9736cSJeff Kirsher 
1624bcc9736cSJeff Kirsher static inline void set_rx_buf(struct ksz_desc *desc, u32 addr)
1625bcc9736cSJeff Kirsher {
1626bcc9736cSJeff Kirsher 	desc->phw->addr = cpu_to_le32(addr);
1627bcc9736cSJeff Kirsher }
1628bcc9736cSJeff Kirsher 
1629bcc9736cSJeff Kirsher static inline void set_rx_len(struct ksz_desc *desc, u32 len)
1630bcc9736cSJeff Kirsher {
1631bcc9736cSJeff Kirsher 	desc->sw.buf.rx.buf_size = len;
1632bcc9736cSJeff Kirsher }
1633bcc9736cSJeff Kirsher 
1634bcc9736cSJeff Kirsher static inline void get_tx_pkt(struct ksz_desc_info *info,
1635bcc9736cSJeff Kirsher 	struct ksz_desc **desc)
1636bcc9736cSJeff Kirsher {
1637bcc9736cSJeff Kirsher 	*desc = &info->ring[info->next];
1638bcc9736cSJeff Kirsher 	info->next++;
1639bcc9736cSJeff Kirsher 	info->next &= info->mask;
1640bcc9736cSJeff Kirsher 	info->avail--;
1641bcc9736cSJeff Kirsher 	(*desc)->sw.buf.data &= ~KS_DESC_TX_MASK;
1642bcc9736cSJeff Kirsher }
1643bcc9736cSJeff Kirsher 
1644bcc9736cSJeff Kirsher static inline void set_tx_buf(struct ksz_desc *desc, u32 addr)
1645bcc9736cSJeff Kirsher {
1646bcc9736cSJeff Kirsher 	desc->phw->addr = cpu_to_le32(addr);
1647bcc9736cSJeff Kirsher }
1648bcc9736cSJeff Kirsher 
1649bcc9736cSJeff Kirsher static inline void set_tx_len(struct ksz_desc *desc, u32 len)
1650bcc9736cSJeff Kirsher {
1651bcc9736cSJeff Kirsher 	desc->sw.buf.tx.buf_size = len;
1652bcc9736cSJeff Kirsher }
1653bcc9736cSJeff Kirsher 
1654bcc9736cSJeff Kirsher /* Switch functions */
1655bcc9736cSJeff Kirsher 
1656bcc9736cSJeff Kirsher #define TABLE_READ			0x10
1657bcc9736cSJeff Kirsher #define TABLE_SEL_SHIFT			2
1658bcc9736cSJeff Kirsher 
1659bcc9736cSJeff Kirsher #define HW_DELAY(hw, reg)			\
1660bcc9736cSJeff Kirsher 	do {					\
1661bcc9736cSJeff Kirsher 		u16 dummy;			\
1662bcc9736cSJeff Kirsher 		dummy = readw(hw->io + reg);	\
1663bcc9736cSJeff Kirsher 	} while (0)
1664bcc9736cSJeff Kirsher 
1665bcc9736cSJeff Kirsher /**
1666bcc9736cSJeff Kirsher  * sw_r_table - read 4 bytes of data from switch table
1667bcc9736cSJeff Kirsher  * @hw:		The hardware instance.
1668bcc9736cSJeff Kirsher  * @table:	The table selector.
1669bcc9736cSJeff Kirsher  * @addr:	The address of the table entry.
1670bcc9736cSJeff Kirsher  * @data:	Buffer to store the read data.
1671bcc9736cSJeff Kirsher  *
1672bcc9736cSJeff Kirsher  * This routine reads 4 bytes of data from the table of the switch.
1673bcc9736cSJeff Kirsher  * Hardware interrupts are disabled to minimize corruption of read data.
1674bcc9736cSJeff Kirsher  */
1675bcc9736cSJeff Kirsher static void sw_r_table(struct ksz_hw *hw, int table, u16 addr, u32 *data)
1676bcc9736cSJeff Kirsher {
1677bcc9736cSJeff Kirsher 	u16 ctrl_addr;
1678bcc9736cSJeff Kirsher 	uint interrupt;
1679bcc9736cSJeff Kirsher 
1680bcc9736cSJeff Kirsher 	ctrl_addr = (((table << TABLE_SEL_SHIFT) | TABLE_READ) << 8) | addr;
1681bcc9736cSJeff Kirsher 
1682bcc9736cSJeff Kirsher 	interrupt = hw_block_intr(hw);
1683bcc9736cSJeff Kirsher 
1684bcc9736cSJeff Kirsher 	writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
1685bcc9736cSJeff Kirsher 	HW_DELAY(hw, KS884X_IACR_OFFSET);
1686bcc9736cSJeff Kirsher 	*data = readl(hw->io + KS884X_ACC_DATA_0_OFFSET);
1687bcc9736cSJeff Kirsher 
1688bcc9736cSJeff Kirsher 	hw_restore_intr(hw, interrupt);
1689bcc9736cSJeff Kirsher }
1690bcc9736cSJeff Kirsher 
1691bcc9736cSJeff Kirsher /**
1692bcc9736cSJeff Kirsher  * sw_w_table_64 - write 8 bytes of data to the switch table
1693bcc9736cSJeff Kirsher  * @hw:		The hardware instance.
1694bcc9736cSJeff Kirsher  * @table:	The table selector.
1695bcc9736cSJeff Kirsher  * @addr:	The address of the table entry.
1696bcc9736cSJeff Kirsher  * @data_hi:	The high part of data to be written (bit63 ~ bit32).
1697bcc9736cSJeff Kirsher  * @data_lo:	The low part of data to be written (bit31 ~ bit0).
1698bcc9736cSJeff Kirsher  *
1699bcc9736cSJeff Kirsher  * This routine writes 8 bytes of data to the table of the switch.
1700bcc9736cSJeff Kirsher  * Hardware interrupts are disabled to minimize corruption of written data.
1701bcc9736cSJeff Kirsher  */
1702bcc9736cSJeff Kirsher static void sw_w_table_64(struct ksz_hw *hw, int table, u16 addr, u32 data_hi,
1703bcc9736cSJeff Kirsher 	u32 data_lo)
1704bcc9736cSJeff Kirsher {
1705bcc9736cSJeff Kirsher 	u16 ctrl_addr;
1706bcc9736cSJeff Kirsher 	uint interrupt;
1707bcc9736cSJeff Kirsher 
1708bcc9736cSJeff Kirsher 	ctrl_addr = ((table << TABLE_SEL_SHIFT) << 8) | addr;
1709bcc9736cSJeff Kirsher 
1710bcc9736cSJeff Kirsher 	interrupt = hw_block_intr(hw);
1711bcc9736cSJeff Kirsher 
1712bcc9736cSJeff Kirsher 	writel(data_hi, hw->io + KS884X_ACC_DATA_4_OFFSET);
1713bcc9736cSJeff Kirsher 	writel(data_lo, hw->io + KS884X_ACC_DATA_0_OFFSET);
1714bcc9736cSJeff Kirsher 
1715bcc9736cSJeff Kirsher 	writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
1716bcc9736cSJeff Kirsher 	HW_DELAY(hw, KS884X_IACR_OFFSET);
1717bcc9736cSJeff Kirsher 
1718bcc9736cSJeff Kirsher 	hw_restore_intr(hw, interrupt);
1719bcc9736cSJeff Kirsher }
1720bcc9736cSJeff Kirsher 
1721bcc9736cSJeff Kirsher /**
1722bcc9736cSJeff Kirsher  * sw_w_sta_mac_table - write to the static MAC table
1723bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1724bcc9736cSJeff Kirsher  * @addr:	The address of the table entry.
1725bcc9736cSJeff Kirsher  * @mac_addr:	The MAC address.
1726bcc9736cSJeff Kirsher  * @ports:	The port members.
1727bcc9736cSJeff Kirsher  * @override:	The flag to override the port receive/transmit settings.
1728bcc9736cSJeff Kirsher  * @valid:	The flag to indicate entry is valid.
1729bcc9736cSJeff Kirsher  * @use_fid:	The flag to indicate the FID is valid.
1730bcc9736cSJeff Kirsher  * @fid:	The FID value.
1731bcc9736cSJeff Kirsher  *
1732bcc9736cSJeff Kirsher  * This routine writes an entry of the static MAC table of the switch.  It
1733bcc9736cSJeff Kirsher  * calls sw_w_table_64() to write the data.
1734bcc9736cSJeff Kirsher  */
1735bcc9736cSJeff Kirsher static void sw_w_sta_mac_table(struct ksz_hw *hw, u16 addr, u8 *mac_addr,
1736bcc9736cSJeff Kirsher 	u8 ports, int override, int valid, int use_fid, u8 fid)
1737bcc9736cSJeff Kirsher {
1738bcc9736cSJeff Kirsher 	u32 data_hi;
1739bcc9736cSJeff Kirsher 	u32 data_lo;
1740bcc9736cSJeff Kirsher 
1741bcc9736cSJeff Kirsher 	data_lo = ((u32) mac_addr[2] << 24) |
1742bcc9736cSJeff Kirsher 		((u32) mac_addr[3] << 16) |
1743bcc9736cSJeff Kirsher 		((u32) mac_addr[4] << 8) | mac_addr[5];
1744bcc9736cSJeff Kirsher 	data_hi = ((u32) mac_addr[0] << 8) | mac_addr[1];
1745bcc9736cSJeff Kirsher 	data_hi |= (u32) ports << STATIC_MAC_FWD_PORTS_SHIFT;
1746bcc9736cSJeff Kirsher 
1747bcc9736cSJeff Kirsher 	if (override)
1748bcc9736cSJeff Kirsher 		data_hi |= STATIC_MAC_TABLE_OVERRIDE;
1749bcc9736cSJeff Kirsher 	if (use_fid) {
1750bcc9736cSJeff Kirsher 		data_hi |= STATIC_MAC_TABLE_USE_FID;
1751bcc9736cSJeff Kirsher 		data_hi |= (u32) fid << STATIC_MAC_FID_SHIFT;
1752bcc9736cSJeff Kirsher 	}
1753bcc9736cSJeff Kirsher 	if (valid)
1754bcc9736cSJeff Kirsher 		data_hi |= STATIC_MAC_TABLE_VALID;
1755bcc9736cSJeff Kirsher 
1756bcc9736cSJeff Kirsher 	sw_w_table_64(hw, TABLE_STATIC_MAC, addr, data_hi, data_lo);
1757bcc9736cSJeff Kirsher }
1758bcc9736cSJeff Kirsher 
1759bcc9736cSJeff Kirsher /**
1760bcc9736cSJeff Kirsher  * sw_r_vlan_table - read from the VLAN table
1761bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1762bcc9736cSJeff Kirsher  * @addr:	The address of the table entry.
1763bcc9736cSJeff Kirsher  * @vid:	Buffer to store the VID.
1764bcc9736cSJeff Kirsher  * @fid:	Buffer to store the VID.
1765bcc9736cSJeff Kirsher  * @member:	Buffer to store the port membership.
1766bcc9736cSJeff Kirsher  *
1767bcc9736cSJeff Kirsher  * This function reads an entry of the VLAN table of the switch.  It calls
1768bcc9736cSJeff Kirsher  * sw_r_table() to get the data.
1769bcc9736cSJeff Kirsher  *
1770bcc9736cSJeff Kirsher  * Return 0 if the entry is valid; otherwise -1.
1771bcc9736cSJeff Kirsher  */
1772bcc9736cSJeff Kirsher static int sw_r_vlan_table(struct ksz_hw *hw, u16 addr, u16 *vid, u8 *fid,
1773bcc9736cSJeff Kirsher 	u8 *member)
1774bcc9736cSJeff Kirsher {
1775bcc9736cSJeff Kirsher 	u32 data;
1776bcc9736cSJeff Kirsher 
1777bcc9736cSJeff Kirsher 	sw_r_table(hw, TABLE_VLAN, addr, &data);
1778bcc9736cSJeff Kirsher 	if (data & VLAN_TABLE_VALID) {
1779bcc9736cSJeff Kirsher 		*vid = (u16)(data & VLAN_TABLE_VID);
1780bcc9736cSJeff Kirsher 		*fid = (u8)((data & VLAN_TABLE_FID) >> VLAN_TABLE_FID_SHIFT);
1781bcc9736cSJeff Kirsher 		*member = (u8)((data & VLAN_TABLE_MEMBERSHIP) >>
1782bcc9736cSJeff Kirsher 			VLAN_TABLE_MEMBERSHIP_SHIFT);
1783bcc9736cSJeff Kirsher 		return 0;
1784bcc9736cSJeff Kirsher 	}
1785bcc9736cSJeff Kirsher 	return -1;
1786bcc9736cSJeff Kirsher }
1787bcc9736cSJeff Kirsher 
1788bcc9736cSJeff Kirsher /**
1789bcc9736cSJeff Kirsher  * port_r_mib_cnt - read MIB counter
1790bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1791bcc9736cSJeff Kirsher  * @port:	The port index.
1792bcc9736cSJeff Kirsher  * @addr:	The address of the counter.
1793bcc9736cSJeff Kirsher  * @cnt:	Buffer to store the counter.
1794bcc9736cSJeff Kirsher  *
1795bcc9736cSJeff Kirsher  * This routine reads a MIB counter of the port.
1796bcc9736cSJeff Kirsher  * Hardware interrupts are disabled to minimize corruption of read data.
1797bcc9736cSJeff Kirsher  */
1798bcc9736cSJeff Kirsher static void port_r_mib_cnt(struct ksz_hw *hw, int port, u16 addr, u64 *cnt)
1799bcc9736cSJeff Kirsher {
1800bcc9736cSJeff Kirsher 	u32 data;
1801bcc9736cSJeff Kirsher 	u16 ctrl_addr;
1802bcc9736cSJeff Kirsher 	uint interrupt;
1803bcc9736cSJeff Kirsher 	int timeout;
1804bcc9736cSJeff Kirsher 
1805bcc9736cSJeff Kirsher 	ctrl_addr = addr + PORT_COUNTER_NUM * port;
1806bcc9736cSJeff Kirsher 
1807bcc9736cSJeff Kirsher 	interrupt = hw_block_intr(hw);
1808bcc9736cSJeff Kirsher 
1809bcc9736cSJeff Kirsher 	ctrl_addr |= (((TABLE_MIB << TABLE_SEL_SHIFT) | TABLE_READ) << 8);
1810bcc9736cSJeff Kirsher 	writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
1811bcc9736cSJeff Kirsher 	HW_DELAY(hw, KS884X_IACR_OFFSET);
1812bcc9736cSJeff Kirsher 
1813bcc9736cSJeff Kirsher 	for (timeout = 100; timeout > 0; timeout--) {
1814bcc9736cSJeff Kirsher 		data = readl(hw->io + KS884X_ACC_DATA_0_OFFSET);
1815bcc9736cSJeff Kirsher 
1816bcc9736cSJeff Kirsher 		if (data & MIB_COUNTER_VALID) {
1817bcc9736cSJeff Kirsher 			if (data & MIB_COUNTER_OVERFLOW)
1818bcc9736cSJeff Kirsher 				*cnt += MIB_COUNTER_VALUE + 1;
1819bcc9736cSJeff Kirsher 			*cnt += data & MIB_COUNTER_VALUE;
1820bcc9736cSJeff Kirsher 			break;
1821bcc9736cSJeff Kirsher 		}
1822bcc9736cSJeff Kirsher 	}
1823bcc9736cSJeff Kirsher 
1824bcc9736cSJeff Kirsher 	hw_restore_intr(hw, interrupt);
1825bcc9736cSJeff Kirsher }
1826bcc9736cSJeff Kirsher 
1827bcc9736cSJeff Kirsher /**
1828bcc9736cSJeff Kirsher  * port_r_mib_pkt - read dropped packet counts
1829bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1830bcc9736cSJeff Kirsher  * @port:	The port index.
1831bcc9736cSJeff Kirsher  * @cnt:	Buffer to store the receive and transmit dropped packet counts.
1832bcc9736cSJeff Kirsher  *
1833bcc9736cSJeff Kirsher  * This routine reads the dropped packet counts of the port.
1834bcc9736cSJeff Kirsher  * Hardware interrupts are disabled to minimize corruption of read data.
1835bcc9736cSJeff Kirsher  */
1836bcc9736cSJeff Kirsher static void port_r_mib_pkt(struct ksz_hw *hw, int port, u32 *last, u64 *cnt)
1837bcc9736cSJeff Kirsher {
1838bcc9736cSJeff Kirsher 	u32 cur;
1839bcc9736cSJeff Kirsher 	u32 data;
1840bcc9736cSJeff Kirsher 	u16 ctrl_addr;
1841bcc9736cSJeff Kirsher 	uint interrupt;
1842bcc9736cSJeff Kirsher 	int index;
1843bcc9736cSJeff Kirsher 
1844bcc9736cSJeff Kirsher 	index = KS_MIB_PACKET_DROPPED_RX_0 + port;
1845bcc9736cSJeff Kirsher 	do {
1846bcc9736cSJeff Kirsher 		interrupt = hw_block_intr(hw);
1847bcc9736cSJeff Kirsher 
1848bcc9736cSJeff Kirsher 		ctrl_addr = (u16) index;
1849bcc9736cSJeff Kirsher 		ctrl_addr |= (((TABLE_MIB << TABLE_SEL_SHIFT) | TABLE_READ)
1850bcc9736cSJeff Kirsher 			<< 8);
1851bcc9736cSJeff Kirsher 		writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
1852bcc9736cSJeff Kirsher 		HW_DELAY(hw, KS884X_IACR_OFFSET);
1853bcc9736cSJeff Kirsher 		data = readl(hw->io + KS884X_ACC_DATA_0_OFFSET);
1854bcc9736cSJeff Kirsher 
1855bcc9736cSJeff Kirsher 		hw_restore_intr(hw, interrupt);
1856bcc9736cSJeff Kirsher 
1857bcc9736cSJeff Kirsher 		data &= MIB_PACKET_DROPPED;
1858bcc9736cSJeff Kirsher 		cur = *last;
1859bcc9736cSJeff Kirsher 		if (data != cur) {
1860bcc9736cSJeff Kirsher 			*last = data;
1861bcc9736cSJeff Kirsher 			if (data < cur)
1862bcc9736cSJeff Kirsher 				data += MIB_PACKET_DROPPED + 1;
1863bcc9736cSJeff Kirsher 			data -= cur;
1864bcc9736cSJeff Kirsher 			*cnt += data;
1865bcc9736cSJeff Kirsher 		}
1866bcc9736cSJeff Kirsher 		++last;
1867bcc9736cSJeff Kirsher 		++cnt;
1868bcc9736cSJeff Kirsher 		index -= KS_MIB_PACKET_DROPPED_TX -
1869bcc9736cSJeff Kirsher 			KS_MIB_PACKET_DROPPED_TX_0 + 1;
1870bcc9736cSJeff Kirsher 	} while (index >= KS_MIB_PACKET_DROPPED_TX_0 + port);
1871bcc9736cSJeff Kirsher }
1872bcc9736cSJeff Kirsher 
1873bcc9736cSJeff Kirsher /**
1874bcc9736cSJeff Kirsher  * port_r_cnt - read MIB counters periodically
1875bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1876bcc9736cSJeff Kirsher  * @port:	The port index.
1877bcc9736cSJeff Kirsher  *
1878bcc9736cSJeff Kirsher  * This routine is used to read the counters of the port periodically to avoid
1879bcc9736cSJeff Kirsher  * counter overflow.  The hardware should be acquired first before calling this
1880bcc9736cSJeff Kirsher  * routine.
1881bcc9736cSJeff Kirsher  *
1882bcc9736cSJeff Kirsher  * Return non-zero when not all counters not read.
1883bcc9736cSJeff Kirsher  */
1884bcc9736cSJeff Kirsher static int port_r_cnt(struct ksz_hw *hw, int port)
1885bcc9736cSJeff Kirsher {
1886bcc9736cSJeff Kirsher 	struct ksz_port_mib *mib = &hw->port_mib[port];
1887bcc9736cSJeff Kirsher 
1888bcc9736cSJeff Kirsher 	if (mib->mib_start < PORT_COUNTER_NUM)
1889bcc9736cSJeff Kirsher 		while (mib->cnt_ptr < PORT_COUNTER_NUM) {
1890bcc9736cSJeff Kirsher 			port_r_mib_cnt(hw, port, mib->cnt_ptr,
1891bcc9736cSJeff Kirsher 				&mib->counter[mib->cnt_ptr]);
1892bcc9736cSJeff Kirsher 			++mib->cnt_ptr;
1893bcc9736cSJeff Kirsher 		}
1894bcc9736cSJeff Kirsher 	if (hw->mib_cnt > PORT_COUNTER_NUM)
1895bcc9736cSJeff Kirsher 		port_r_mib_pkt(hw, port, mib->dropped,
1896bcc9736cSJeff Kirsher 			&mib->counter[PORT_COUNTER_NUM]);
1897bcc9736cSJeff Kirsher 	mib->cnt_ptr = 0;
1898bcc9736cSJeff Kirsher 	return 0;
1899bcc9736cSJeff Kirsher }
1900bcc9736cSJeff Kirsher 
1901bcc9736cSJeff Kirsher /**
1902bcc9736cSJeff Kirsher  * port_init_cnt - initialize MIB counter values
1903bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1904bcc9736cSJeff Kirsher  * @port:	The port index.
1905bcc9736cSJeff Kirsher  *
1906bcc9736cSJeff Kirsher  * This routine is used to initialize all counters to zero if the hardware
1907bcc9736cSJeff Kirsher  * cannot do it after reset.
1908bcc9736cSJeff Kirsher  */
1909bcc9736cSJeff Kirsher static void port_init_cnt(struct ksz_hw *hw, int port)
1910bcc9736cSJeff Kirsher {
1911bcc9736cSJeff Kirsher 	struct ksz_port_mib *mib = &hw->port_mib[port];
1912bcc9736cSJeff Kirsher 
1913bcc9736cSJeff Kirsher 	mib->cnt_ptr = 0;
1914bcc9736cSJeff Kirsher 	if (mib->mib_start < PORT_COUNTER_NUM)
1915bcc9736cSJeff Kirsher 		do {
1916bcc9736cSJeff Kirsher 			port_r_mib_cnt(hw, port, mib->cnt_ptr,
1917bcc9736cSJeff Kirsher 				&mib->counter[mib->cnt_ptr]);
1918bcc9736cSJeff Kirsher 			++mib->cnt_ptr;
1919bcc9736cSJeff Kirsher 		} while (mib->cnt_ptr < PORT_COUNTER_NUM);
1920bcc9736cSJeff Kirsher 	if (hw->mib_cnt > PORT_COUNTER_NUM)
1921bcc9736cSJeff Kirsher 		port_r_mib_pkt(hw, port, mib->dropped,
1922bcc9736cSJeff Kirsher 			&mib->counter[PORT_COUNTER_NUM]);
1923bcc9736cSJeff Kirsher 	memset((void *) mib->counter, 0, sizeof(u64) * TOTAL_PORT_COUNTER_NUM);
1924bcc9736cSJeff Kirsher 	mib->cnt_ptr = 0;
1925bcc9736cSJeff Kirsher }
1926bcc9736cSJeff Kirsher 
1927bcc9736cSJeff Kirsher /*
1928bcc9736cSJeff Kirsher  * Port functions
1929bcc9736cSJeff Kirsher  */
1930bcc9736cSJeff Kirsher 
1931bcc9736cSJeff Kirsher /**
1932bcc9736cSJeff Kirsher  * port_chk - check port register bits
1933bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1934bcc9736cSJeff Kirsher  * @port:	The port index.
1935bcc9736cSJeff Kirsher  * @offset:	The offset of the port register.
1936bcc9736cSJeff Kirsher  * @bits:	The data bits to check.
1937bcc9736cSJeff Kirsher  *
1938bcc9736cSJeff Kirsher  * This function checks whether the specified bits of the port register are set
1939bcc9736cSJeff Kirsher  * or not.
1940bcc9736cSJeff Kirsher  *
1941bcc9736cSJeff Kirsher  * Return 0 if the bits are not set.
1942bcc9736cSJeff Kirsher  */
1943bcc9736cSJeff Kirsher static int port_chk(struct ksz_hw *hw, int port, int offset, u16 bits)
1944bcc9736cSJeff Kirsher {
1945bcc9736cSJeff Kirsher 	u32 addr;
1946bcc9736cSJeff Kirsher 	u16 data;
1947bcc9736cSJeff Kirsher 
1948bcc9736cSJeff Kirsher 	PORT_CTRL_ADDR(port, addr);
1949bcc9736cSJeff Kirsher 	addr += offset;
1950bcc9736cSJeff Kirsher 	data = readw(hw->io + addr);
1951bcc9736cSJeff Kirsher 	return (data & bits) == bits;
1952bcc9736cSJeff Kirsher }
1953bcc9736cSJeff Kirsher 
1954bcc9736cSJeff Kirsher /**
1955bcc9736cSJeff Kirsher  * port_cfg - set port register bits
1956bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1957bcc9736cSJeff Kirsher  * @port:	The port index.
1958bcc9736cSJeff Kirsher  * @offset:	The offset of the port register.
1959bcc9736cSJeff Kirsher  * @bits:	The data bits to set.
1960bcc9736cSJeff Kirsher  * @set:	The flag indicating whether the bits are to be set or not.
1961bcc9736cSJeff Kirsher  *
1962bcc9736cSJeff Kirsher  * This routine sets or resets the specified bits of the port register.
1963bcc9736cSJeff Kirsher  */
1964bcc9736cSJeff Kirsher static void port_cfg(struct ksz_hw *hw, int port, int offset, u16 bits,
1965bcc9736cSJeff Kirsher 	int set)
1966bcc9736cSJeff Kirsher {
1967bcc9736cSJeff Kirsher 	u32 addr;
1968bcc9736cSJeff Kirsher 	u16 data;
1969bcc9736cSJeff Kirsher 
1970bcc9736cSJeff Kirsher 	PORT_CTRL_ADDR(port, addr);
1971bcc9736cSJeff Kirsher 	addr += offset;
1972bcc9736cSJeff Kirsher 	data = readw(hw->io + addr);
1973bcc9736cSJeff Kirsher 	if (set)
1974bcc9736cSJeff Kirsher 		data |= bits;
1975bcc9736cSJeff Kirsher 	else
1976bcc9736cSJeff Kirsher 		data &= ~bits;
1977bcc9736cSJeff Kirsher 	writew(data, hw->io + addr);
1978bcc9736cSJeff Kirsher }
1979bcc9736cSJeff Kirsher 
1980bcc9736cSJeff Kirsher /**
1981bcc9736cSJeff Kirsher  * port_chk_shift - check port bit
1982bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
1983bcc9736cSJeff Kirsher  * @port:	The port index.
1984bcc9736cSJeff Kirsher  * @offset:	The offset of the register.
1985bcc9736cSJeff Kirsher  * @shift:	Number of bits to shift.
1986bcc9736cSJeff Kirsher  *
1987bcc9736cSJeff Kirsher  * This function checks whether the specified port is set in the register or
1988bcc9736cSJeff Kirsher  * not.
1989bcc9736cSJeff Kirsher  *
1990bcc9736cSJeff Kirsher  * Return 0 if the port is not set.
1991bcc9736cSJeff Kirsher  */
1992bcc9736cSJeff Kirsher static int port_chk_shift(struct ksz_hw *hw, int port, u32 addr, int shift)
1993bcc9736cSJeff Kirsher {
1994bcc9736cSJeff Kirsher 	u16 data;
1995bcc9736cSJeff Kirsher 	u16 bit = 1 << port;
1996bcc9736cSJeff Kirsher 
1997bcc9736cSJeff Kirsher 	data = readw(hw->io + addr);
1998bcc9736cSJeff Kirsher 	data >>= shift;
1999bcc9736cSJeff Kirsher 	return (data & bit) == bit;
2000bcc9736cSJeff Kirsher }
2001bcc9736cSJeff Kirsher 
2002bcc9736cSJeff Kirsher /**
2003bcc9736cSJeff Kirsher  * port_cfg_shift - set port bit
2004bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2005bcc9736cSJeff Kirsher  * @port:	The port index.
2006bcc9736cSJeff Kirsher  * @offset:	The offset of the register.
2007bcc9736cSJeff Kirsher  * @shift:	Number of bits to shift.
2008bcc9736cSJeff Kirsher  * @set:	The flag indicating whether the port is to be set or not.
2009bcc9736cSJeff Kirsher  *
2010bcc9736cSJeff Kirsher  * This routine sets or resets the specified port in the register.
2011bcc9736cSJeff Kirsher  */
2012bcc9736cSJeff Kirsher static void port_cfg_shift(struct ksz_hw *hw, int port, u32 addr, int shift,
2013bcc9736cSJeff Kirsher 	int set)
2014bcc9736cSJeff Kirsher {
2015bcc9736cSJeff Kirsher 	u16 data;
2016bcc9736cSJeff Kirsher 	u16 bits = 1 << port;
2017bcc9736cSJeff Kirsher 
2018bcc9736cSJeff Kirsher 	data = readw(hw->io + addr);
2019bcc9736cSJeff Kirsher 	bits <<= shift;
2020bcc9736cSJeff Kirsher 	if (set)
2021bcc9736cSJeff Kirsher 		data |= bits;
2022bcc9736cSJeff Kirsher 	else
2023bcc9736cSJeff Kirsher 		data &= ~bits;
2024bcc9736cSJeff Kirsher 	writew(data, hw->io + addr);
2025bcc9736cSJeff Kirsher }
2026bcc9736cSJeff Kirsher 
2027bcc9736cSJeff Kirsher /**
2028bcc9736cSJeff Kirsher  * port_r8 - read byte from port register
2029bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2030bcc9736cSJeff Kirsher  * @port:	The port index.
2031bcc9736cSJeff Kirsher  * @offset:	The offset of the port register.
2032bcc9736cSJeff Kirsher  * @data:	Buffer to store the data.
2033bcc9736cSJeff Kirsher  *
2034bcc9736cSJeff Kirsher  * This routine reads a byte from the port register.
2035bcc9736cSJeff Kirsher  */
2036bcc9736cSJeff Kirsher static void port_r8(struct ksz_hw *hw, int port, int offset, u8 *data)
2037bcc9736cSJeff Kirsher {
2038bcc9736cSJeff Kirsher 	u32 addr;
2039bcc9736cSJeff Kirsher 
2040bcc9736cSJeff Kirsher 	PORT_CTRL_ADDR(port, addr);
2041bcc9736cSJeff Kirsher 	addr += offset;
2042bcc9736cSJeff Kirsher 	*data = readb(hw->io + addr);
2043bcc9736cSJeff Kirsher }
2044bcc9736cSJeff Kirsher 
2045bcc9736cSJeff Kirsher /**
2046bcc9736cSJeff Kirsher  * port_r16 - read word from port register.
2047bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2048bcc9736cSJeff Kirsher  * @port:	The port index.
2049bcc9736cSJeff Kirsher  * @offset:	The offset of the port register.
2050bcc9736cSJeff Kirsher  * @data:	Buffer to store the data.
2051bcc9736cSJeff Kirsher  *
2052bcc9736cSJeff Kirsher  * This routine reads a word from the port register.
2053bcc9736cSJeff Kirsher  */
2054bcc9736cSJeff Kirsher static void port_r16(struct ksz_hw *hw, int port, int offset, u16 *data)
2055bcc9736cSJeff Kirsher {
2056bcc9736cSJeff Kirsher 	u32 addr;
2057bcc9736cSJeff Kirsher 
2058bcc9736cSJeff Kirsher 	PORT_CTRL_ADDR(port, addr);
2059bcc9736cSJeff Kirsher 	addr += offset;
2060bcc9736cSJeff Kirsher 	*data = readw(hw->io + addr);
2061bcc9736cSJeff Kirsher }
2062bcc9736cSJeff Kirsher 
2063bcc9736cSJeff Kirsher /**
2064bcc9736cSJeff Kirsher  * port_w16 - write word to port register.
2065bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2066bcc9736cSJeff Kirsher  * @port:	The port index.
2067bcc9736cSJeff Kirsher  * @offset:	The offset of the port register.
2068bcc9736cSJeff Kirsher  * @data:	Data to write.
2069bcc9736cSJeff Kirsher  *
2070bcc9736cSJeff Kirsher  * This routine writes a word to the port register.
2071bcc9736cSJeff Kirsher  */
2072bcc9736cSJeff Kirsher static void port_w16(struct ksz_hw *hw, int port, int offset, u16 data)
2073bcc9736cSJeff Kirsher {
2074bcc9736cSJeff Kirsher 	u32 addr;
2075bcc9736cSJeff Kirsher 
2076bcc9736cSJeff Kirsher 	PORT_CTRL_ADDR(port, addr);
2077bcc9736cSJeff Kirsher 	addr += offset;
2078bcc9736cSJeff Kirsher 	writew(data, hw->io + addr);
2079bcc9736cSJeff Kirsher }
2080bcc9736cSJeff Kirsher 
2081bcc9736cSJeff Kirsher /**
2082bcc9736cSJeff Kirsher  * sw_chk - check switch register bits
2083bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2084bcc9736cSJeff Kirsher  * @addr:	The address of the switch register.
2085bcc9736cSJeff Kirsher  * @bits:	The data bits to check.
2086bcc9736cSJeff Kirsher  *
2087bcc9736cSJeff Kirsher  * This function checks whether the specified bits of the switch register are
2088bcc9736cSJeff Kirsher  * set or not.
2089bcc9736cSJeff Kirsher  *
2090bcc9736cSJeff Kirsher  * Return 0 if the bits are not set.
2091bcc9736cSJeff Kirsher  */
2092bcc9736cSJeff Kirsher static int sw_chk(struct ksz_hw *hw, u32 addr, u16 bits)
2093bcc9736cSJeff Kirsher {
2094bcc9736cSJeff Kirsher 	u16 data;
2095bcc9736cSJeff Kirsher 
2096bcc9736cSJeff Kirsher 	data = readw(hw->io + addr);
2097bcc9736cSJeff Kirsher 	return (data & bits) == bits;
2098bcc9736cSJeff Kirsher }
2099bcc9736cSJeff Kirsher 
2100bcc9736cSJeff Kirsher /**
2101bcc9736cSJeff Kirsher  * sw_cfg - set switch register bits
2102bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2103bcc9736cSJeff Kirsher  * @addr:	The address of the switch register.
2104bcc9736cSJeff Kirsher  * @bits:	The data bits to set.
2105bcc9736cSJeff Kirsher  * @set:	The flag indicating whether the bits are to be set or not.
2106bcc9736cSJeff Kirsher  *
2107bcc9736cSJeff Kirsher  * This function sets or resets the specified bits of the switch register.
2108bcc9736cSJeff Kirsher  */
2109bcc9736cSJeff Kirsher static void sw_cfg(struct ksz_hw *hw, u32 addr, u16 bits, int set)
2110bcc9736cSJeff Kirsher {
2111bcc9736cSJeff Kirsher 	u16 data;
2112bcc9736cSJeff Kirsher 
2113bcc9736cSJeff Kirsher 	data = readw(hw->io + addr);
2114bcc9736cSJeff Kirsher 	if (set)
2115bcc9736cSJeff Kirsher 		data |= bits;
2116bcc9736cSJeff Kirsher 	else
2117bcc9736cSJeff Kirsher 		data &= ~bits;
2118bcc9736cSJeff Kirsher 	writew(data, hw->io + addr);
2119bcc9736cSJeff Kirsher }
2120bcc9736cSJeff Kirsher 
2121bcc9736cSJeff Kirsher /* Bandwidth */
2122bcc9736cSJeff Kirsher 
2123bcc9736cSJeff Kirsher static inline void port_cfg_broad_storm(struct ksz_hw *hw, int p, int set)
2124bcc9736cSJeff Kirsher {
2125bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2126bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_BROADCAST_STORM, set);
2127bcc9736cSJeff Kirsher }
2128bcc9736cSJeff Kirsher 
2129bcc9736cSJeff Kirsher static inline int port_chk_broad_storm(struct ksz_hw *hw, int p)
2130bcc9736cSJeff Kirsher {
2131bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2132bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_BROADCAST_STORM);
2133bcc9736cSJeff Kirsher }
2134bcc9736cSJeff Kirsher 
2135bcc9736cSJeff Kirsher /* Driver set switch broadcast storm protection at 10% rate. */
2136bcc9736cSJeff Kirsher #define BROADCAST_STORM_PROTECTION_RATE	10
2137bcc9736cSJeff Kirsher 
2138bcc9736cSJeff Kirsher /* 148,800 frames * 67 ms / 100 */
2139bcc9736cSJeff Kirsher #define BROADCAST_STORM_VALUE		9969
2140bcc9736cSJeff Kirsher 
2141bcc9736cSJeff Kirsher /**
2142bcc9736cSJeff Kirsher  * sw_cfg_broad_storm - configure broadcast storm threshold
2143bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2144bcc9736cSJeff Kirsher  * @percent:	Broadcast storm threshold in percent of transmit rate.
2145bcc9736cSJeff Kirsher  *
2146bcc9736cSJeff Kirsher  * This routine configures the broadcast storm threshold of the switch.
2147bcc9736cSJeff Kirsher  */
2148bcc9736cSJeff Kirsher static void sw_cfg_broad_storm(struct ksz_hw *hw, u8 percent)
2149bcc9736cSJeff Kirsher {
2150bcc9736cSJeff Kirsher 	u16 data;
2151bcc9736cSJeff Kirsher 	u32 value = ((u32) BROADCAST_STORM_VALUE * (u32) percent / 100);
2152bcc9736cSJeff Kirsher 
2153bcc9736cSJeff Kirsher 	if (value > BROADCAST_STORM_RATE)
2154bcc9736cSJeff Kirsher 		value = BROADCAST_STORM_RATE;
2155bcc9736cSJeff Kirsher 
2156bcc9736cSJeff Kirsher 	data = readw(hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2157bcc9736cSJeff Kirsher 	data &= ~(BROADCAST_STORM_RATE_LO | BROADCAST_STORM_RATE_HI);
2158bcc9736cSJeff Kirsher 	data |= ((value & 0x00FF) << 8) | ((value & 0xFF00) >> 8);
2159bcc9736cSJeff Kirsher 	writew(data, hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2160bcc9736cSJeff Kirsher }
2161bcc9736cSJeff Kirsher 
2162bcc9736cSJeff Kirsher /**
2163bcc9736cSJeff Kirsher  * sw_get_board_storm - get broadcast storm threshold
2164bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2165bcc9736cSJeff Kirsher  * @percent:	Buffer to store the broadcast storm threshold percentage.
2166bcc9736cSJeff Kirsher  *
2167bcc9736cSJeff Kirsher  * This routine retrieves the broadcast storm threshold of the switch.
2168bcc9736cSJeff Kirsher  */
2169bcc9736cSJeff Kirsher static void sw_get_broad_storm(struct ksz_hw *hw, u8 *percent)
2170bcc9736cSJeff Kirsher {
2171bcc9736cSJeff Kirsher 	int num;
2172bcc9736cSJeff Kirsher 	u16 data;
2173bcc9736cSJeff Kirsher 
2174bcc9736cSJeff Kirsher 	data = readw(hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2175bcc9736cSJeff Kirsher 	num = (data & BROADCAST_STORM_RATE_HI);
2176bcc9736cSJeff Kirsher 	num <<= 8;
2177bcc9736cSJeff Kirsher 	num |= (data & BROADCAST_STORM_RATE_LO) >> 8;
2178bcc9736cSJeff Kirsher 	num = (num * 100 + BROADCAST_STORM_VALUE / 2) / BROADCAST_STORM_VALUE;
2179bcc9736cSJeff Kirsher 	*percent = (u8) num;
2180bcc9736cSJeff Kirsher }
2181bcc9736cSJeff Kirsher 
2182bcc9736cSJeff Kirsher /**
2183bcc9736cSJeff Kirsher  * sw_dis_broad_storm - disable broadstorm
2184bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2185bcc9736cSJeff Kirsher  * @port:	The port index.
2186bcc9736cSJeff Kirsher  *
2187bcc9736cSJeff Kirsher  * This routine disables the broadcast storm limit function of the switch.
2188bcc9736cSJeff Kirsher  */
2189bcc9736cSJeff Kirsher static void sw_dis_broad_storm(struct ksz_hw *hw, int port)
2190bcc9736cSJeff Kirsher {
2191bcc9736cSJeff Kirsher 	port_cfg_broad_storm(hw, port, 0);
2192bcc9736cSJeff Kirsher }
2193bcc9736cSJeff Kirsher 
2194bcc9736cSJeff Kirsher /**
2195bcc9736cSJeff Kirsher  * sw_ena_broad_storm - enable broadcast storm
2196bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2197bcc9736cSJeff Kirsher  * @port:	The port index.
2198bcc9736cSJeff Kirsher  *
2199bcc9736cSJeff Kirsher  * This routine enables the broadcast storm limit function of the switch.
2200bcc9736cSJeff Kirsher  */
2201bcc9736cSJeff Kirsher static void sw_ena_broad_storm(struct ksz_hw *hw, int port)
2202bcc9736cSJeff Kirsher {
2203bcc9736cSJeff Kirsher 	sw_cfg_broad_storm(hw, hw->ksz_switch->broad_per);
2204bcc9736cSJeff Kirsher 	port_cfg_broad_storm(hw, port, 1);
2205bcc9736cSJeff Kirsher }
2206bcc9736cSJeff Kirsher 
2207bcc9736cSJeff Kirsher /**
2208bcc9736cSJeff Kirsher  * sw_init_broad_storm - initialize broadcast storm
2209bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2210bcc9736cSJeff Kirsher  *
2211bcc9736cSJeff Kirsher  * This routine initializes the broadcast storm limit function of the switch.
2212bcc9736cSJeff Kirsher  */
2213bcc9736cSJeff Kirsher static void sw_init_broad_storm(struct ksz_hw *hw)
2214bcc9736cSJeff Kirsher {
2215bcc9736cSJeff Kirsher 	int port;
2216bcc9736cSJeff Kirsher 
2217bcc9736cSJeff Kirsher 	hw->ksz_switch->broad_per = 1;
2218bcc9736cSJeff Kirsher 	sw_cfg_broad_storm(hw, hw->ksz_switch->broad_per);
2219bcc9736cSJeff Kirsher 	for (port = 0; port < TOTAL_PORT_NUM; port++)
2220bcc9736cSJeff Kirsher 		sw_dis_broad_storm(hw, port);
2221bcc9736cSJeff Kirsher 	sw_cfg(hw, KS8842_SWITCH_CTRL_2_OFFSET, MULTICAST_STORM_DISABLE, 1);
2222bcc9736cSJeff Kirsher }
2223bcc9736cSJeff Kirsher 
2224bcc9736cSJeff Kirsher /**
2225bcc9736cSJeff Kirsher  * hw_cfg_broad_storm - configure broadcast storm
2226bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2227bcc9736cSJeff Kirsher  * @percent:	Broadcast storm threshold in percent of transmit rate.
2228bcc9736cSJeff Kirsher  *
2229bcc9736cSJeff Kirsher  * This routine configures the broadcast storm threshold of the switch.
2230bcc9736cSJeff Kirsher  * It is called by user functions.  The hardware should be acquired first.
2231bcc9736cSJeff Kirsher  */
2232bcc9736cSJeff Kirsher static void hw_cfg_broad_storm(struct ksz_hw *hw, u8 percent)
2233bcc9736cSJeff Kirsher {
2234bcc9736cSJeff Kirsher 	if (percent > 100)
2235bcc9736cSJeff Kirsher 		percent = 100;
2236bcc9736cSJeff Kirsher 
2237bcc9736cSJeff Kirsher 	sw_cfg_broad_storm(hw, percent);
2238bcc9736cSJeff Kirsher 	sw_get_broad_storm(hw, &percent);
2239bcc9736cSJeff Kirsher 	hw->ksz_switch->broad_per = percent;
2240bcc9736cSJeff Kirsher }
2241bcc9736cSJeff Kirsher 
2242bcc9736cSJeff Kirsher /**
2243bcc9736cSJeff Kirsher  * sw_dis_prio_rate - disable switch priority rate
2244bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2245bcc9736cSJeff Kirsher  * @port:	The port index.
2246bcc9736cSJeff Kirsher  *
2247bcc9736cSJeff Kirsher  * This routine disables the priority rate function of the switch.
2248bcc9736cSJeff Kirsher  */
2249bcc9736cSJeff Kirsher static void sw_dis_prio_rate(struct ksz_hw *hw, int port)
2250bcc9736cSJeff Kirsher {
2251bcc9736cSJeff Kirsher 	u32 addr;
2252bcc9736cSJeff Kirsher 
2253bcc9736cSJeff Kirsher 	PORT_CTRL_ADDR(port, addr);
2254bcc9736cSJeff Kirsher 	addr += KS8842_PORT_IN_RATE_OFFSET;
2255bcc9736cSJeff Kirsher 	writel(0, hw->io + addr);
2256bcc9736cSJeff Kirsher }
2257bcc9736cSJeff Kirsher 
2258bcc9736cSJeff Kirsher /**
2259bcc9736cSJeff Kirsher  * sw_init_prio_rate - initialize switch prioirty rate
2260bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2261bcc9736cSJeff Kirsher  *
2262bcc9736cSJeff Kirsher  * This routine initializes the priority rate function of the switch.
2263bcc9736cSJeff Kirsher  */
2264bcc9736cSJeff Kirsher static void sw_init_prio_rate(struct ksz_hw *hw)
2265bcc9736cSJeff Kirsher {
2266bcc9736cSJeff Kirsher 	int port;
2267bcc9736cSJeff Kirsher 	int prio;
2268bcc9736cSJeff Kirsher 	struct ksz_switch *sw = hw->ksz_switch;
2269bcc9736cSJeff Kirsher 
2270bcc9736cSJeff Kirsher 	for (port = 0; port < TOTAL_PORT_NUM; port++) {
2271bcc9736cSJeff Kirsher 		for (prio = 0; prio < PRIO_QUEUES; prio++) {
2272bcc9736cSJeff Kirsher 			sw->port_cfg[port].rx_rate[prio] =
2273bcc9736cSJeff Kirsher 			sw->port_cfg[port].tx_rate[prio] = 0;
2274bcc9736cSJeff Kirsher 		}
2275bcc9736cSJeff Kirsher 		sw_dis_prio_rate(hw, port);
2276bcc9736cSJeff Kirsher 	}
2277bcc9736cSJeff Kirsher }
2278bcc9736cSJeff Kirsher 
2279bcc9736cSJeff Kirsher /* Communication */
2280bcc9736cSJeff Kirsher 
2281bcc9736cSJeff Kirsher static inline void port_cfg_back_pressure(struct ksz_hw *hw, int p, int set)
2282bcc9736cSJeff Kirsher {
2283bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2284bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_BACK_PRESSURE, set);
2285bcc9736cSJeff Kirsher }
2286bcc9736cSJeff Kirsher 
2287bcc9736cSJeff Kirsher static inline void port_cfg_force_flow_ctrl(struct ksz_hw *hw, int p, int set)
2288bcc9736cSJeff Kirsher {
2289bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2290bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_FORCE_FLOW_CTRL, set);
2291bcc9736cSJeff Kirsher }
2292bcc9736cSJeff Kirsher 
2293bcc9736cSJeff Kirsher static inline int port_chk_back_pressure(struct ksz_hw *hw, int p)
2294bcc9736cSJeff Kirsher {
2295bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2296bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_BACK_PRESSURE);
2297bcc9736cSJeff Kirsher }
2298bcc9736cSJeff Kirsher 
2299bcc9736cSJeff Kirsher static inline int port_chk_force_flow_ctrl(struct ksz_hw *hw, int p)
2300bcc9736cSJeff Kirsher {
2301bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2302bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_FORCE_FLOW_CTRL);
2303bcc9736cSJeff Kirsher }
2304bcc9736cSJeff Kirsher 
2305bcc9736cSJeff Kirsher /* Spanning Tree */
2306bcc9736cSJeff Kirsher 
2307bcc9736cSJeff Kirsher static inline void port_cfg_dis_learn(struct ksz_hw *hw, int p, int set)
2308bcc9736cSJeff Kirsher {
2309bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2310bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_LEARN_DISABLE, set);
2311bcc9736cSJeff Kirsher }
2312bcc9736cSJeff Kirsher 
2313bcc9736cSJeff Kirsher static inline void port_cfg_rx(struct ksz_hw *hw, int p, int set)
2314bcc9736cSJeff Kirsher {
2315bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2316bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_RX_ENABLE, set);
2317bcc9736cSJeff Kirsher }
2318bcc9736cSJeff Kirsher 
2319bcc9736cSJeff Kirsher static inline void port_cfg_tx(struct ksz_hw *hw, int p, int set)
2320bcc9736cSJeff Kirsher {
2321bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2322bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_TX_ENABLE, set);
2323bcc9736cSJeff Kirsher }
2324bcc9736cSJeff Kirsher 
2325bcc9736cSJeff Kirsher static inline void sw_cfg_fast_aging(struct ksz_hw *hw, int set)
2326bcc9736cSJeff Kirsher {
2327bcc9736cSJeff Kirsher 	sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET, SWITCH_FAST_AGING, set);
2328bcc9736cSJeff Kirsher }
2329bcc9736cSJeff Kirsher 
2330bcc9736cSJeff Kirsher static inline void sw_flush_dyn_mac_table(struct ksz_hw *hw)
2331bcc9736cSJeff Kirsher {
2332bcc9736cSJeff Kirsher 	if (!(hw->overrides & FAST_AGING)) {
2333bcc9736cSJeff Kirsher 		sw_cfg_fast_aging(hw, 1);
2334bcc9736cSJeff Kirsher 		mdelay(1);
2335bcc9736cSJeff Kirsher 		sw_cfg_fast_aging(hw, 0);
2336bcc9736cSJeff Kirsher 	}
2337bcc9736cSJeff Kirsher }
2338bcc9736cSJeff Kirsher 
2339bcc9736cSJeff Kirsher /* VLAN */
2340bcc9736cSJeff Kirsher 
2341bcc9736cSJeff Kirsher static inline void port_cfg_ins_tag(struct ksz_hw *hw, int p, int insert)
2342bcc9736cSJeff Kirsher {
2343bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2344bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_INSERT_TAG, insert);
2345bcc9736cSJeff Kirsher }
2346bcc9736cSJeff Kirsher 
2347bcc9736cSJeff Kirsher static inline void port_cfg_rmv_tag(struct ksz_hw *hw, int p, int remove)
2348bcc9736cSJeff Kirsher {
2349bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2350bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_REMOVE_TAG, remove);
2351bcc9736cSJeff Kirsher }
2352bcc9736cSJeff Kirsher 
2353bcc9736cSJeff Kirsher static inline int port_chk_ins_tag(struct ksz_hw *hw, int p)
2354bcc9736cSJeff Kirsher {
2355bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2356bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_INSERT_TAG);
2357bcc9736cSJeff Kirsher }
2358bcc9736cSJeff Kirsher 
2359bcc9736cSJeff Kirsher static inline int port_chk_rmv_tag(struct ksz_hw *hw, int p)
2360bcc9736cSJeff Kirsher {
2361bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2362bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_REMOVE_TAG);
2363bcc9736cSJeff Kirsher }
2364bcc9736cSJeff Kirsher 
2365bcc9736cSJeff Kirsher static inline void port_cfg_dis_non_vid(struct ksz_hw *hw, int p, int set)
2366bcc9736cSJeff Kirsher {
2367bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2368bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_DISCARD_NON_VID, set);
2369bcc9736cSJeff Kirsher }
2370bcc9736cSJeff Kirsher 
2371bcc9736cSJeff Kirsher static inline void port_cfg_in_filter(struct ksz_hw *hw, int p, int set)
2372bcc9736cSJeff Kirsher {
2373bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2374bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_INGRESS_VLAN_FILTER, set);
2375bcc9736cSJeff Kirsher }
2376bcc9736cSJeff Kirsher 
2377bcc9736cSJeff Kirsher static inline int port_chk_dis_non_vid(struct ksz_hw *hw, int p)
2378bcc9736cSJeff Kirsher {
2379bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2380bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_DISCARD_NON_VID);
2381bcc9736cSJeff Kirsher }
2382bcc9736cSJeff Kirsher 
2383bcc9736cSJeff Kirsher static inline int port_chk_in_filter(struct ksz_hw *hw, int p)
2384bcc9736cSJeff Kirsher {
2385bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2386bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_INGRESS_VLAN_FILTER);
2387bcc9736cSJeff Kirsher }
2388bcc9736cSJeff Kirsher 
2389bcc9736cSJeff Kirsher /* Mirroring */
2390bcc9736cSJeff Kirsher 
2391bcc9736cSJeff Kirsher static inline void port_cfg_mirror_sniffer(struct ksz_hw *hw, int p, int set)
2392bcc9736cSJeff Kirsher {
2393bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2394bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_MIRROR_SNIFFER, set);
2395bcc9736cSJeff Kirsher }
2396bcc9736cSJeff Kirsher 
2397bcc9736cSJeff Kirsher static inline void port_cfg_mirror_rx(struct ksz_hw *hw, int p, int set)
2398bcc9736cSJeff Kirsher {
2399bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2400bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_MIRROR_RX, set);
2401bcc9736cSJeff Kirsher }
2402bcc9736cSJeff Kirsher 
2403bcc9736cSJeff Kirsher static inline void port_cfg_mirror_tx(struct ksz_hw *hw, int p, int set)
2404bcc9736cSJeff Kirsher {
2405bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2406bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_MIRROR_TX, set);
2407bcc9736cSJeff Kirsher }
2408bcc9736cSJeff Kirsher 
2409bcc9736cSJeff Kirsher static inline void sw_cfg_mirror_rx_tx(struct ksz_hw *hw, int set)
2410bcc9736cSJeff Kirsher {
2411bcc9736cSJeff Kirsher 	sw_cfg(hw, KS8842_SWITCH_CTRL_2_OFFSET, SWITCH_MIRROR_RX_TX, set);
2412bcc9736cSJeff Kirsher }
2413bcc9736cSJeff Kirsher 
2414bcc9736cSJeff Kirsher static void sw_init_mirror(struct ksz_hw *hw)
2415bcc9736cSJeff Kirsher {
2416bcc9736cSJeff Kirsher 	int port;
2417bcc9736cSJeff Kirsher 
2418bcc9736cSJeff Kirsher 	for (port = 0; port < TOTAL_PORT_NUM; port++) {
2419bcc9736cSJeff Kirsher 		port_cfg_mirror_sniffer(hw, port, 0);
2420bcc9736cSJeff Kirsher 		port_cfg_mirror_rx(hw, port, 0);
2421bcc9736cSJeff Kirsher 		port_cfg_mirror_tx(hw, port, 0);
2422bcc9736cSJeff Kirsher 	}
2423bcc9736cSJeff Kirsher 	sw_cfg_mirror_rx_tx(hw, 0);
2424bcc9736cSJeff Kirsher }
2425bcc9736cSJeff Kirsher 
2426bcc9736cSJeff Kirsher static inline void sw_cfg_unk_def_deliver(struct ksz_hw *hw, int set)
2427bcc9736cSJeff Kirsher {
2428bcc9736cSJeff Kirsher 	sw_cfg(hw, KS8842_SWITCH_CTRL_7_OFFSET,
2429bcc9736cSJeff Kirsher 		SWITCH_UNK_DEF_PORT_ENABLE, set);
2430bcc9736cSJeff Kirsher }
2431bcc9736cSJeff Kirsher 
2432bcc9736cSJeff Kirsher static inline int sw_cfg_chk_unk_def_deliver(struct ksz_hw *hw)
2433bcc9736cSJeff Kirsher {
2434bcc9736cSJeff Kirsher 	return sw_chk(hw, KS8842_SWITCH_CTRL_7_OFFSET,
2435bcc9736cSJeff Kirsher 		SWITCH_UNK_DEF_PORT_ENABLE);
2436bcc9736cSJeff Kirsher }
2437bcc9736cSJeff Kirsher 
2438bcc9736cSJeff Kirsher static inline void sw_cfg_unk_def_port(struct ksz_hw *hw, int port, int set)
2439bcc9736cSJeff Kirsher {
2440bcc9736cSJeff Kirsher 	port_cfg_shift(hw, port, KS8842_SWITCH_CTRL_7_OFFSET, 0, set);
2441bcc9736cSJeff Kirsher }
2442bcc9736cSJeff Kirsher 
2443bcc9736cSJeff Kirsher static inline int sw_chk_unk_def_port(struct ksz_hw *hw, int port)
2444bcc9736cSJeff Kirsher {
2445bcc9736cSJeff Kirsher 	return port_chk_shift(hw, port, KS8842_SWITCH_CTRL_7_OFFSET, 0);
2446bcc9736cSJeff Kirsher }
2447bcc9736cSJeff Kirsher 
2448bcc9736cSJeff Kirsher /* Priority */
2449bcc9736cSJeff Kirsher 
2450bcc9736cSJeff Kirsher static inline void port_cfg_diffserv(struct ksz_hw *hw, int p, int set)
2451bcc9736cSJeff Kirsher {
2452bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2453bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_DIFFSERV_ENABLE, set);
2454bcc9736cSJeff Kirsher }
2455bcc9736cSJeff Kirsher 
2456bcc9736cSJeff Kirsher static inline void port_cfg_802_1p(struct ksz_hw *hw, int p, int set)
2457bcc9736cSJeff Kirsher {
2458bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2459bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_802_1P_ENABLE, set);
2460bcc9736cSJeff Kirsher }
2461bcc9736cSJeff Kirsher 
2462bcc9736cSJeff Kirsher static inline void port_cfg_replace_vid(struct ksz_hw *hw, int p, int set)
2463bcc9736cSJeff Kirsher {
2464bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2465bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_USER_PRIORITY_CEILING, set);
2466bcc9736cSJeff Kirsher }
2467bcc9736cSJeff Kirsher 
2468bcc9736cSJeff Kirsher static inline void port_cfg_prio(struct ksz_hw *hw, int p, int set)
2469bcc9736cSJeff Kirsher {
2470bcc9736cSJeff Kirsher 	port_cfg(hw, p,
2471bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_PRIO_QUEUE_ENABLE, set);
2472bcc9736cSJeff Kirsher }
2473bcc9736cSJeff Kirsher 
2474bcc9736cSJeff Kirsher static inline int port_chk_diffserv(struct ksz_hw *hw, int p)
2475bcc9736cSJeff Kirsher {
2476bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2477bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_DIFFSERV_ENABLE);
2478bcc9736cSJeff Kirsher }
2479bcc9736cSJeff Kirsher 
2480bcc9736cSJeff Kirsher static inline int port_chk_802_1p(struct ksz_hw *hw, int p)
2481bcc9736cSJeff Kirsher {
2482bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2483bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_802_1P_ENABLE);
2484bcc9736cSJeff Kirsher }
2485bcc9736cSJeff Kirsher 
2486bcc9736cSJeff Kirsher static inline int port_chk_replace_vid(struct ksz_hw *hw, int p)
2487bcc9736cSJeff Kirsher {
2488bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2489bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_2_OFFSET, PORT_USER_PRIORITY_CEILING);
2490bcc9736cSJeff Kirsher }
2491bcc9736cSJeff Kirsher 
2492bcc9736cSJeff Kirsher static inline int port_chk_prio(struct ksz_hw *hw, int p)
2493bcc9736cSJeff Kirsher {
2494bcc9736cSJeff Kirsher 	return port_chk(hw, p,
2495bcc9736cSJeff Kirsher 		KS8842_PORT_CTRL_1_OFFSET, PORT_PRIO_QUEUE_ENABLE);
2496bcc9736cSJeff Kirsher }
2497bcc9736cSJeff Kirsher 
2498bcc9736cSJeff Kirsher /**
2499bcc9736cSJeff Kirsher  * sw_dis_diffserv - disable switch DiffServ priority
2500bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2501bcc9736cSJeff Kirsher  * @port:	The port index.
2502bcc9736cSJeff Kirsher  *
2503bcc9736cSJeff Kirsher  * This routine disables the DiffServ priority function of the switch.
2504bcc9736cSJeff Kirsher  */
2505bcc9736cSJeff Kirsher static void sw_dis_diffserv(struct ksz_hw *hw, int port)
2506bcc9736cSJeff Kirsher {
2507bcc9736cSJeff Kirsher 	port_cfg_diffserv(hw, port, 0);
2508bcc9736cSJeff Kirsher }
2509bcc9736cSJeff Kirsher 
2510bcc9736cSJeff Kirsher /**
2511bcc9736cSJeff Kirsher  * sw_dis_802_1p - disable switch 802.1p priority
2512bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2513bcc9736cSJeff Kirsher  * @port:	The port index.
2514bcc9736cSJeff Kirsher  *
2515bcc9736cSJeff Kirsher  * This routine disables the 802.1p priority function of the switch.
2516bcc9736cSJeff Kirsher  */
2517bcc9736cSJeff Kirsher static void sw_dis_802_1p(struct ksz_hw *hw, int port)
2518bcc9736cSJeff Kirsher {
2519bcc9736cSJeff Kirsher 	port_cfg_802_1p(hw, port, 0);
2520bcc9736cSJeff Kirsher }
2521bcc9736cSJeff Kirsher 
2522bcc9736cSJeff Kirsher /**
2523bcc9736cSJeff Kirsher  * sw_cfg_replace_null_vid -
2524bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2525bcc9736cSJeff Kirsher  * @set:	The flag to disable or enable.
2526bcc9736cSJeff Kirsher  *
2527bcc9736cSJeff Kirsher  */
2528bcc9736cSJeff Kirsher static void sw_cfg_replace_null_vid(struct ksz_hw *hw, int set)
2529bcc9736cSJeff Kirsher {
2530bcc9736cSJeff Kirsher 	sw_cfg(hw, KS8842_SWITCH_CTRL_3_OFFSET, SWITCH_REPLACE_NULL_VID, set);
2531bcc9736cSJeff Kirsher }
2532bcc9736cSJeff Kirsher 
2533bcc9736cSJeff Kirsher /**
2534bcc9736cSJeff Kirsher  * sw_cfg_replace_vid - enable switch 802.10 priority re-mapping
2535bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2536bcc9736cSJeff Kirsher  * @port:	The port index.
2537bcc9736cSJeff Kirsher  * @set:	The flag to disable or enable.
2538bcc9736cSJeff Kirsher  *
2539bcc9736cSJeff Kirsher  * This routine enables the 802.1p priority re-mapping function of the switch.
2540bcc9736cSJeff Kirsher  * That allows 802.1p priority field to be replaced with the port's default
2541bcc9736cSJeff Kirsher  * tag's priority value if the ingress packet's 802.1p priority has a higher
2542bcc9736cSJeff Kirsher  * priority than port's default tag's priority.
2543bcc9736cSJeff Kirsher  */
2544bcc9736cSJeff Kirsher static void sw_cfg_replace_vid(struct ksz_hw *hw, int port, int set)
2545bcc9736cSJeff Kirsher {
2546bcc9736cSJeff Kirsher 	port_cfg_replace_vid(hw, port, set);
2547bcc9736cSJeff Kirsher }
2548bcc9736cSJeff Kirsher 
2549bcc9736cSJeff Kirsher /**
2550bcc9736cSJeff Kirsher  * sw_cfg_port_based - configure switch port based priority
2551bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2552bcc9736cSJeff Kirsher  * @port:	The port index.
2553bcc9736cSJeff Kirsher  * @prio:	The priority to set.
2554bcc9736cSJeff Kirsher  *
2555bcc9736cSJeff Kirsher  * This routine configures the port based priority of the switch.
2556bcc9736cSJeff Kirsher  */
2557bcc9736cSJeff Kirsher static void sw_cfg_port_based(struct ksz_hw *hw, int port, u8 prio)
2558bcc9736cSJeff Kirsher {
2559bcc9736cSJeff Kirsher 	u16 data;
2560bcc9736cSJeff Kirsher 
2561bcc9736cSJeff Kirsher 	if (prio > PORT_BASED_PRIORITY_BASE)
2562bcc9736cSJeff Kirsher 		prio = PORT_BASED_PRIORITY_BASE;
2563bcc9736cSJeff Kirsher 
2564bcc9736cSJeff Kirsher 	hw->ksz_switch->port_cfg[port].port_prio = prio;
2565bcc9736cSJeff Kirsher 
2566bcc9736cSJeff Kirsher 	port_r16(hw, port, KS8842_PORT_CTRL_1_OFFSET, &data);
2567bcc9736cSJeff Kirsher 	data &= ~PORT_BASED_PRIORITY_MASK;
2568bcc9736cSJeff Kirsher 	data |= prio << PORT_BASED_PRIORITY_SHIFT;
2569bcc9736cSJeff Kirsher 	port_w16(hw, port, KS8842_PORT_CTRL_1_OFFSET, data);
2570bcc9736cSJeff Kirsher }
2571bcc9736cSJeff Kirsher 
2572bcc9736cSJeff Kirsher /**
2573bcc9736cSJeff Kirsher  * sw_dis_multi_queue - disable transmit multiple queues
2574bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2575bcc9736cSJeff Kirsher  * @port:	The port index.
2576bcc9736cSJeff Kirsher  *
2577bcc9736cSJeff Kirsher  * This routine disables the transmit multiple queues selection of the switch
2578bcc9736cSJeff Kirsher  * port.  Only single transmit queue on the port.
2579bcc9736cSJeff Kirsher  */
2580bcc9736cSJeff Kirsher static void sw_dis_multi_queue(struct ksz_hw *hw, int port)
2581bcc9736cSJeff Kirsher {
2582bcc9736cSJeff Kirsher 	port_cfg_prio(hw, port, 0);
2583bcc9736cSJeff Kirsher }
2584bcc9736cSJeff Kirsher 
2585bcc9736cSJeff Kirsher /**
2586bcc9736cSJeff Kirsher  * sw_init_prio - initialize switch priority
2587bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2588bcc9736cSJeff Kirsher  *
2589bcc9736cSJeff Kirsher  * This routine initializes the switch QoS priority functions.
2590bcc9736cSJeff Kirsher  */
2591bcc9736cSJeff Kirsher static void sw_init_prio(struct ksz_hw *hw)
2592bcc9736cSJeff Kirsher {
2593bcc9736cSJeff Kirsher 	int port;
2594bcc9736cSJeff Kirsher 	int tos;
2595bcc9736cSJeff Kirsher 	struct ksz_switch *sw = hw->ksz_switch;
2596bcc9736cSJeff Kirsher 
2597bcc9736cSJeff Kirsher 	/*
2598bcc9736cSJeff Kirsher 	 * Init all the 802.1p tag priority value to be assigned to different
2599bcc9736cSJeff Kirsher 	 * priority queue.
2600bcc9736cSJeff Kirsher 	 */
2601bcc9736cSJeff Kirsher 	sw->p_802_1p[0] = 0;
2602bcc9736cSJeff Kirsher 	sw->p_802_1p[1] = 0;
2603bcc9736cSJeff Kirsher 	sw->p_802_1p[2] = 1;
2604bcc9736cSJeff Kirsher 	sw->p_802_1p[3] = 1;
2605bcc9736cSJeff Kirsher 	sw->p_802_1p[4] = 2;
2606bcc9736cSJeff Kirsher 	sw->p_802_1p[5] = 2;
2607bcc9736cSJeff Kirsher 	sw->p_802_1p[6] = 3;
2608bcc9736cSJeff Kirsher 	sw->p_802_1p[7] = 3;
2609bcc9736cSJeff Kirsher 
2610bcc9736cSJeff Kirsher 	/*
2611bcc9736cSJeff Kirsher 	 * Init all the DiffServ priority value to be assigned to priority
2612bcc9736cSJeff Kirsher 	 * queue 0.
2613bcc9736cSJeff Kirsher 	 */
2614bcc9736cSJeff Kirsher 	for (tos = 0; tos < DIFFSERV_ENTRIES; tos++)
2615bcc9736cSJeff Kirsher 		sw->diffserv[tos] = 0;
2616bcc9736cSJeff Kirsher 
2617bcc9736cSJeff Kirsher 	/* All QoS functions disabled. */
2618bcc9736cSJeff Kirsher 	for (port = 0; port < TOTAL_PORT_NUM; port++) {
2619bcc9736cSJeff Kirsher 		sw_dis_multi_queue(hw, port);
2620bcc9736cSJeff Kirsher 		sw_dis_diffserv(hw, port);
2621bcc9736cSJeff Kirsher 		sw_dis_802_1p(hw, port);
2622bcc9736cSJeff Kirsher 		sw_cfg_replace_vid(hw, port, 0);
2623bcc9736cSJeff Kirsher 
2624bcc9736cSJeff Kirsher 		sw->port_cfg[port].port_prio = 0;
2625bcc9736cSJeff Kirsher 		sw_cfg_port_based(hw, port, sw->port_cfg[port].port_prio);
2626bcc9736cSJeff Kirsher 	}
2627bcc9736cSJeff Kirsher 	sw_cfg_replace_null_vid(hw, 0);
2628bcc9736cSJeff Kirsher }
2629bcc9736cSJeff Kirsher 
2630bcc9736cSJeff Kirsher /**
2631bcc9736cSJeff Kirsher  * port_get_def_vid - get port default VID.
2632bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2633bcc9736cSJeff Kirsher  * @port:	The port index.
2634bcc9736cSJeff Kirsher  * @vid:	Buffer to store the VID.
2635bcc9736cSJeff Kirsher  *
2636bcc9736cSJeff Kirsher  * This routine retrieves the default VID of the port.
2637bcc9736cSJeff Kirsher  */
2638bcc9736cSJeff Kirsher static void port_get_def_vid(struct ksz_hw *hw, int port, u16 *vid)
2639bcc9736cSJeff Kirsher {
2640bcc9736cSJeff Kirsher 	u32 addr;
2641bcc9736cSJeff Kirsher 
2642bcc9736cSJeff Kirsher 	PORT_CTRL_ADDR(port, addr);
2643bcc9736cSJeff Kirsher 	addr += KS8842_PORT_CTRL_VID_OFFSET;
2644bcc9736cSJeff Kirsher 	*vid = readw(hw->io + addr);
2645bcc9736cSJeff Kirsher }
2646bcc9736cSJeff Kirsher 
2647bcc9736cSJeff Kirsher /**
2648bcc9736cSJeff Kirsher  * sw_init_vlan - initialize switch VLAN
2649bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2650bcc9736cSJeff Kirsher  *
2651bcc9736cSJeff Kirsher  * This routine initializes the VLAN function of the switch.
2652bcc9736cSJeff Kirsher  */
2653bcc9736cSJeff Kirsher static void sw_init_vlan(struct ksz_hw *hw)
2654bcc9736cSJeff Kirsher {
2655bcc9736cSJeff Kirsher 	int port;
2656bcc9736cSJeff Kirsher 	int entry;
2657bcc9736cSJeff Kirsher 	struct ksz_switch *sw = hw->ksz_switch;
2658bcc9736cSJeff Kirsher 
2659bcc9736cSJeff Kirsher 	/* Read 16 VLAN entries from device's VLAN table. */
2660bcc9736cSJeff Kirsher 	for (entry = 0; entry < VLAN_TABLE_ENTRIES; entry++) {
2661bcc9736cSJeff Kirsher 		sw_r_vlan_table(hw, entry,
2662bcc9736cSJeff Kirsher 			&sw->vlan_table[entry].vid,
2663bcc9736cSJeff Kirsher 			&sw->vlan_table[entry].fid,
2664bcc9736cSJeff Kirsher 			&sw->vlan_table[entry].member);
2665bcc9736cSJeff Kirsher 	}
2666bcc9736cSJeff Kirsher 
2667bcc9736cSJeff Kirsher 	for (port = 0; port < TOTAL_PORT_NUM; port++) {
2668bcc9736cSJeff Kirsher 		port_get_def_vid(hw, port, &sw->port_cfg[port].vid);
2669bcc9736cSJeff Kirsher 		sw->port_cfg[port].member = PORT_MASK;
2670bcc9736cSJeff Kirsher 	}
2671bcc9736cSJeff Kirsher }
2672bcc9736cSJeff Kirsher 
2673bcc9736cSJeff Kirsher /**
2674bcc9736cSJeff Kirsher  * sw_cfg_port_base_vlan - configure port-based VLAN membership
2675bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2676bcc9736cSJeff Kirsher  * @port:	The port index.
2677bcc9736cSJeff Kirsher  * @member:	The port-based VLAN membership.
2678bcc9736cSJeff Kirsher  *
2679bcc9736cSJeff Kirsher  * This routine configures the port-based VLAN membership of the port.
2680bcc9736cSJeff Kirsher  */
2681bcc9736cSJeff Kirsher static void sw_cfg_port_base_vlan(struct ksz_hw *hw, int port, u8 member)
2682bcc9736cSJeff Kirsher {
2683bcc9736cSJeff Kirsher 	u32 addr;
2684bcc9736cSJeff Kirsher 	u8 data;
2685bcc9736cSJeff Kirsher 
2686bcc9736cSJeff Kirsher 	PORT_CTRL_ADDR(port, addr);
2687bcc9736cSJeff Kirsher 	addr += KS8842_PORT_CTRL_2_OFFSET;
2688bcc9736cSJeff Kirsher 
2689bcc9736cSJeff Kirsher 	data = readb(hw->io + addr);
2690bcc9736cSJeff Kirsher 	data &= ~PORT_VLAN_MEMBERSHIP;
2691bcc9736cSJeff Kirsher 	data |= (member & PORT_MASK);
2692bcc9736cSJeff Kirsher 	writeb(data, hw->io + addr);
2693bcc9736cSJeff Kirsher 
2694bcc9736cSJeff Kirsher 	hw->ksz_switch->port_cfg[port].member = member;
2695bcc9736cSJeff Kirsher }
2696bcc9736cSJeff Kirsher 
2697bcc9736cSJeff Kirsher /**
2698bcc9736cSJeff Kirsher  * sw_get_addr - get the switch MAC address.
2699bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2700bcc9736cSJeff Kirsher  * @mac_addr:	Buffer to store the MAC address.
2701bcc9736cSJeff Kirsher  *
2702bcc9736cSJeff Kirsher  * This function retrieves the MAC address of the switch.
2703bcc9736cSJeff Kirsher  */
2704bcc9736cSJeff Kirsher static inline void sw_get_addr(struct ksz_hw *hw, u8 *mac_addr)
2705bcc9736cSJeff Kirsher {
2706bcc9736cSJeff Kirsher 	int i;
2707bcc9736cSJeff Kirsher 
2708bcc9736cSJeff Kirsher 	for (i = 0; i < 6; i += 2) {
2709bcc9736cSJeff Kirsher 		mac_addr[i] = readb(hw->io + KS8842_MAC_ADDR_0_OFFSET + i);
2710bcc9736cSJeff Kirsher 		mac_addr[1 + i] = readb(hw->io + KS8842_MAC_ADDR_1_OFFSET + i);
2711bcc9736cSJeff Kirsher 	}
2712bcc9736cSJeff Kirsher }
2713bcc9736cSJeff Kirsher 
2714bcc9736cSJeff Kirsher /**
2715bcc9736cSJeff Kirsher  * sw_set_addr - configure switch MAC address
2716bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2717bcc9736cSJeff Kirsher  * @mac_addr:	The MAC address.
2718bcc9736cSJeff Kirsher  *
2719bcc9736cSJeff Kirsher  * This function configures the MAC address of the switch.
2720bcc9736cSJeff Kirsher  */
2721bcc9736cSJeff Kirsher static void sw_set_addr(struct ksz_hw *hw, u8 *mac_addr)
2722bcc9736cSJeff Kirsher {
2723bcc9736cSJeff Kirsher 	int i;
2724bcc9736cSJeff Kirsher 
2725bcc9736cSJeff Kirsher 	for (i = 0; i < 6; i += 2) {
2726bcc9736cSJeff Kirsher 		writeb(mac_addr[i], hw->io + KS8842_MAC_ADDR_0_OFFSET + i);
2727bcc9736cSJeff Kirsher 		writeb(mac_addr[1 + i], hw->io + KS8842_MAC_ADDR_1_OFFSET + i);
2728bcc9736cSJeff Kirsher 	}
2729bcc9736cSJeff Kirsher }
2730bcc9736cSJeff Kirsher 
2731bcc9736cSJeff Kirsher /**
2732bcc9736cSJeff Kirsher  * sw_set_global_ctrl - set switch global control
2733bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2734bcc9736cSJeff Kirsher  *
2735bcc9736cSJeff Kirsher  * This routine sets the global control of the switch function.
2736bcc9736cSJeff Kirsher  */
2737bcc9736cSJeff Kirsher static void sw_set_global_ctrl(struct ksz_hw *hw)
2738bcc9736cSJeff Kirsher {
2739bcc9736cSJeff Kirsher 	u16 data;
2740bcc9736cSJeff Kirsher 
2741bcc9736cSJeff Kirsher 	/* Enable switch MII flow control. */
2742bcc9736cSJeff Kirsher 	data = readw(hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2743bcc9736cSJeff Kirsher 	data |= SWITCH_FLOW_CTRL;
2744bcc9736cSJeff Kirsher 	writew(data, hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2745bcc9736cSJeff Kirsher 
2746bcc9736cSJeff Kirsher 	data = readw(hw->io + KS8842_SWITCH_CTRL_1_OFFSET);
2747bcc9736cSJeff Kirsher 
2748bcc9736cSJeff Kirsher 	/* Enable aggressive back off algorithm in half duplex mode. */
2749bcc9736cSJeff Kirsher 	data |= SWITCH_AGGR_BACKOFF;
2750bcc9736cSJeff Kirsher 
2751bcc9736cSJeff Kirsher 	/* Enable automatic fast aging when link changed detected. */
2752bcc9736cSJeff Kirsher 	data |= SWITCH_AGING_ENABLE;
2753bcc9736cSJeff Kirsher 	data |= SWITCH_LINK_AUTO_AGING;
2754bcc9736cSJeff Kirsher 
2755bcc9736cSJeff Kirsher 	if (hw->overrides & FAST_AGING)
2756bcc9736cSJeff Kirsher 		data |= SWITCH_FAST_AGING;
2757bcc9736cSJeff Kirsher 	else
2758bcc9736cSJeff Kirsher 		data &= ~SWITCH_FAST_AGING;
2759bcc9736cSJeff Kirsher 	writew(data, hw->io + KS8842_SWITCH_CTRL_1_OFFSET);
2760bcc9736cSJeff Kirsher 
2761bcc9736cSJeff Kirsher 	data = readw(hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
2762bcc9736cSJeff Kirsher 
2763bcc9736cSJeff Kirsher 	/* Enable no excessive collision drop. */
2764bcc9736cSJeff Kirsher 	data |= NO_EXC_COLLISION_DROP;
2765bcc9736cSJeff Kirsher 	writew(data, hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
2766bcc9736cSJeff Kirsher }
2767bcc9736cSJeff Kirsher 
2768bcc9736cSJeff Kirsher enum {
2769bcc9736cSJeff Kirsher 	STP_STATE_DISABLED = 0,
2770bcc9736cSJeff Kirsher 	STP_STATE_LISTENING,
2771bcc9736cSJeff Kirsher 	STP_STATE_LEARNING,
2772bcc9736cSJeff Kirsher 	STP_STATE_FORWARDING,
2773bcc9736cSJeff Kirsher 	STP_STATE_BLOCKED,
2774bcc9736cSJeff Kirsher 	STP_STATE_SIMPLE
2775bcc9736cSJeff Kirsher };
2776bcc9736cSJeff Kirsher 
2777bcc9736cSJeff Kirsher /**
2778bcc9736cSJeff Kirsher  * port_set_stp_state - configure port spanning tree state
2779bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2780bcc9736cSJeff Kirsher  * @port:	The port index.
2781bcc9736cSJeff Kirsher  * @state:	The spanning tree state.
2782bcc9736cSJeff Kirsher  *
2783bcc9736cSJeff Kirsher  * This routine configures the spanning tree state of the port.
2784bcc9736cSJeff Kirsher  */
2785bcc9736cSJeff Kirsher static void port_set_stp_state(struct ksz_hw *hw, int port, int state)
2786bcc9736cSJeff Kirsher {
2787bcc9736cSJeff Kirsher 	u16 data;
2788bcc9736cSJeff Kirsher 
2789bcc9736cSJeff Kirsher 	port_r16(hw, port, KS8842_PORT_CTRL_2_OFFSET, &data);
2790bcc9736cSJeff Kirsher 	switch (state) {
2791bcc9736cSJeff Kirsher 	case STP_STATE_DISABLED:
2792bcc9736cSJeff Kirsher 		data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE);
2793bcc9736cSJeff Kirsher 		data |= PORT_LEARN_DISABLE;
2794bcc9736cSJeff Kirsher 		break;
2795bcc9736cSJeff Kirsher 	case STP_STATE_LISTENING:
2796bcc9736cSJeff Kirsher /*
2797bcc9736cSJeff Kirsher  * No need to turn on transmit because of port direct mode.
2798bcc9736cSJeff Kirsher  * Turning on receive is required if static MAC table is not setup.
2799bcc9736cSJeff Kirsher  */
2800bcc9736cSJeff Kirsher 		data &= ~PORT_TX_ENABLE;
2801bcc9736cSJeff Kirsher 		data |= PORT_RX_ENABLE;
2802bcc9736cSJeff Kirsher 		data |= PORT_LEARN_DISABLE;
2803bcc9736cSJeff Kirsher 		break;
2804bcc9736cSJeff Kirsher 	case STP_STATE_LEARNING:
2805bcc9736cSJeff Kirsher 		data &= ~PORT_TX_ENABLE;
2806bcc9736cSJeff Kirsher 		data |= PORT_RX_ENABLE;
2807bcc9736cSJeff Kirsher 		data &= ~PORT_LEARN_DISABLE;
2808bcc9736cSJeff Kirsher 		break;
2809bcc9736cSJeff Kirsher 	case STP_STATE_FORWARDING:
2810bcc9736cSJeff Kirsher 		data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
2811bcc9736cSJeff Kirsher 		data &= ~PORT_LEARN_DISABLE;
2812bcc9736cSJeff Kirsher 		break;
2813bcc9736cSJeff Kirsher 	case STP_STATE_BLOCKED:
2814bcc9736cSJeff Kirsher /*
2815bcc9736cSJeff Kirsher  * Need to setup static MAC table with override to keep receiving BPDU
2816bcc9736cSJeff Kirsher  * messages.  See sw_init_stp routine.
2817bcc9736cSJeff Kirsher  */
2818bcc9736cSJeff Kirsher 		data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE);
2819bcc9736cSJeff Kirsher 		data |= PORT_LEARN_DISABLE;
2820bcc9736cSJeff Kirsher 		break;
2821bcc9736cSJeff Kirsher 	case STP_STATE_SIMPLE:
2822bcc9736cSJeff Kirsher 		data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
2823bcc9736cSJeff Kirsher 		data |= PORT_LEARN_DISABLE;
2824bcc9736cSJeff Kirsher 		break;
2825bcc9736cSJeff Kirsher 	}
2826bcc9736cSJeff Kirsher 	port_w16(hw, port, KS8842_PORT_CTRL_2_OFFSET, data);
2827bcc9736cSJeff Kirsher 	hw->ksz_switch->port_cfg[port].stp_state = state;
2828bcc9736cSJeff Kirsher }
2829bcc9736cSJeff Kirsher 
2830bcc9736cSJeff Kirsher #define STP_ENTRY			0
2831bcc9736cSJeff Kirsher #define BROADCAST_ENTRY			1
2832bcc9736cSJeff Kirsher #define BRIDGE_ADDR_ENTRY		2
2833bcc9736cSJeff Kirsher #define IPV6_ADDR_ENTRY			3
2834bcc9736cSJeff Kirsher 
2835bcc9736cSJeff Kirsher /**
2836bcc9736cSJeff Kirsher  * sw_clr_sta_mac_table - clear static MAC table
2837bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2838bcc9736cSJeff Kirsher  *
2839bcc9736cSJeff Kirsher  * This routine clears the static MAC table.
2840bcc9736cSJeff Kirsher  */
2841bcc9736cSJeff Kirsher static void sw_clr_sta_mac_table(struct ksz_hw *hw)
2842bcc9736cSJeff Kirsher {
2843bcc9736cSJeff Kirsher 	struct ksz_mac_table *entry;
2844bcc9736cSJeff Kirsher 	int i;
2845bcc9736cSJeff Kirsher 
2846bcc9736cSJeff Kirsher 	for (i = 0; i < STATIC_MAC_TABLE_ENTRIES; i++) {
2847bcc9736cSJeff Kirsher 		entry = &hw->ksz_switch->mac_table[i];
2848bcc9736cSJeff Kirsher 		sw_w_sta_mac_table(hw, i,
2849bcc9736cSJeff Kirsher 			entry->mac_addr, entry->ports,
2850bcc9736cSJeff Kirsher 			entry->override, 0,
2851bcc9736cSJeff Kirsher 			entry->use_fid, entry->fid);
2852bcc9736cSJeff Kirsher 	}
2853bcc9736cSJeff Kirsher }
2854bcc9736cSJeff Kirsher 
2855bcc9736cSJeff Kirsher /**
2856bcc9736cSJeff Kirsher  * sw_init_stp - initialize switch spanning tree support
2857bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2858bcc9736cSJeff Kirsher  *
2859bcc9736cSJeff Kirsher  * This routine initializes the spanning tree support of the switch.
2860bcc9736cSJeff Kirsher  */
2861bcc9736cSJeff Kirsher static void sw_init_stp(struct ksz_hw *hw)
2862bcc9736cSJeff Kirsher {
2863bcc9736cSJeff Kirsher 	struct ksz_mac_table *entry;
2864bcc9736cSJeff Kirsher 
2865bcc9736cSJeff Kirsher 	entry = &hw->ksz_switch->mac_table[STP_ENTRY];
2866bcc9736cSJeff Kirsher 	entry->mac_addr[0] = 0x01;
2867bcc9736cSJeff Kirsher 	entry->mac_addr[1] = 0x80;
2868bcc9736cSJeff Kirsher 	entry->mac_addr[2] = 0xC2;
2869bcc9736cSJeff Kirsher 	entry->mac_addr[3] = 0x00;
2870bcc9736cSJeff Kirsher 	entry->mac_addr[4] = 0x00;
2871bcc9736cSJeff Kirsher 	entry->mac_addr[5] = 0x00;
2872bcc9736cSJeff Kirsher 	entry->ports = HOST_MASK;
2873bcc9736cSJeff Kirsher 	entry->override = 1;
2874bcc9736cSJeff Kirsher 	entry->valid = 1;
2875bcc9736cSJeff Kirsher 	sw_w_sta_mac_table(hw, STP_ENTRY,
2876bcc9736cSJeff Kirsher 		entry->mac_addr, entry->ports,
2877bcc9736cSJeff Kirsher 		entry->override, entry->valid,
2878bcc9736cSJeff Kirsher 		entry->use_fid, entry->fid);
2879bcc9736cSJeff Kirsher }
2880bcc9736cSJeff Kirsher 
2881bcc9736cSJeff Kirsher /**
2882bcc9736cSJeff Kirsher  * sw_block_addr - block certain packets from the host port
2883bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2884bcc9736cSJeff Kirsher  *
2885bcc9736cSJeff Kirsher  * This routine blocks certain packets from reaching to the host port.
2886bcc9736cSJeff Kirsher  */
2887bcc9736cSJeff Kirsher static void sw_block_addr(struct ksz_hw *hw)
2888bcc9736cSJeff Kirsher {
2889bcc9736cSJeff Kirsher 	struct ksz_mac_table *entry;
2890bcc9736cSJeff Kirsher 	int i;
2891bcc9736cSJeff Kirsher 
2892bcc9736cSJeff Kirsher 	for (i = BROADCAST_ENTRY; i <= IPV6_ADDR_ENTRY; i++) {
2893bcc9736cSJeff Kirsher 		entry = &hw->ksz_switch->mac_table[i];
2894bcc9736cSJeff Kirsher 		entry->valid = 0;
2895bcc9736cSJeff Kirsher 		sw_w_sta_mac_table(hw, i,
2896bcc9736cSJeff Kirsher 			entry->mac_addr, entry->ports,
2897bcc9736cSJeff Kirsher 			entry->override, entry->valid,
2898bcc9736cSJeff Kirsher 			entry->use_fid, entry->fid);
2899bcc9736cSJeff Kirsher 	}
2900bcc9736cSJeff Kirsher }
2901bcc9736cSJeff Kirsher 
2902bcc9736cSJeff Kirsher #define PHY_LINK_SUPPORT		\
2903bcc9736cSJeff Kirsher 	(PHY_AUTO_NEG_ASYM_PAUSE |	\
2904bcc9736cSJeff Kirsher 	PHY_AUTO_NEG_SYM_PAUSE |	\
2905bcc9736cSJeff Kirsher 	PHY_AUTO_NEG_100BT4 |		\
2906bcc9736cSJeff Kirsher 	PHY_AUTO_NEG_100BTX_FD |	\
2907bcc9736cSJeff Kirsher 	PHY_AUTO_NEG_100BTX |		\
2908bcc9736cSJeff Kirsher 	PHY_AUTO_NEG_10BT_FD |		\
2909bcc9736cSJeff Kirsher 	PHY_AUTO_NEG_10BT)
2910bcc9736cSJeff Kirsher 
2911bcc9736cSJeff Kirsher static inline void hw_r_phy_ctrl(struct ksz_hw *hw, int phy, u16 *data)
2912bcc9736cSJeff Kirsher {
2913bcc9736cSJeff Kirsher 	*data = readw(hw->io + phy + KS884X_PHY_CTRL_OFFSET);
2914bcc9736cSJeff Kirsher }
2915bcc9736cSJeff Kirsher 
2916bcc9736cSJeff Kirsher static inline void hw_w_phy_ctrl(struct ksz_hw *hw, int phy, u16 data)
2917bcc9736cSJeff Kirsher {
2918bcc9736cSJeff Kirsher 	writew(data, hw->io + phy + KS884X_PHY_CTRL_OFFSET);
2919bcc9736cSJeff Kirsher }
2920bcc9736cSJeff Kirsher 
2921bcc9736cSJeff Kirsher static inline void hw_r_phy_link_stat(struct ksz_hw *hw, int phy, u16 *data)
2922bcc9736cSJeff Kirsher {
2923bcc9736cSJeff Kirsher 	*data = readw(hw->io + phy + KS884X_PHY_STATUS_OFFSET);
2924bcc9736cSJeff Kirsher }
2925bcc9736cSJeff Kirsher 
2926bcc9736cSJeff Kirsher static inline void hw_r_phy_auto_neg(struct ksz_hw *hw, int phy, u16 *data)
2927bcc9736cSJeff Kirsher {
2928bcc9736cSJeff Kirsher 	*data = readw(hw->io + phy + KS884X_PHY_AUTO_NEG_OFFSET);
2929bcc9736cSJeff Kirsher }
2930bcc9736cSJeff Kirsher 
2931bcc9736cSJeff Kirsher static inline void hw_w_phy_auto_neg(struct ksz_hw *hw, int phy, u16 data)
2932bcc9736cSJeff Kirsher {
2933bcc9736cSJeff Kirsher 	writew(data, hw->io + phy + KS884X_PHY_AUTO_NEG_OFFSET);
2934bcc9736cSJeff Kirsher }
2935bcc9736cSJeff Kirsher 
2936bcc9736cSJeff Kirsher static inline void hw_r_phy_rem_cap(struct ksz_hw *hw, int phy, u16 *data)
2937bcc9736cSJeff Kirsher {
2938bcc9736cSJeff Kirsher 	*data = readw(hw->io + phy + KS884X_PHY_REMOTE_CAP_OFFSET);
2939bcc9736cSJeff Kirsher }
2940bcc9736cSJeff Kirsher 
2941bcc9736cSJeff Kirsher static inline void hw_r_phy_crossover(struct ksz_hw *hw, int phy, u16 *data)
2942bcc9736cSJeff Kirsher {
2943bcc9736cSJeff Kirsher 	*data = readw(hw->io + phy + KS884X_PHY_CTRL_OFFSET);
2944bcc9736cSJeff Kirsher }
2945bcc9736cSJeff Kirsher 
2946bcc9736cSJeff Kirsher static inline void hw_w_phy_crossover(struct ksz_hw *hw, int phy, u16 data)
2947bcc9736cSJeff Kirsher {
2948bcc9736cSJeff Kirsher 	writew(data, hw->io + phy + KS884X_PHY_CTRL_OFFSET);
2949bcc9736cSJeff Kirsher }
2950bcc9736cSJeff Kirsher 
2951bcc9736cSJeff Kirsher static inline void hw_r_phy_polarity(struct ksz_hw *hw, int phy, u16 *data)
2952bcc9736cSJeff Kirsher {
2953bcc9736cSJeff Kirsher 	*data = readw(hw->io + phy + KS884X_PHY_PHY_CTRL_OFFSET);
2954bcc9736cSJeff Kirsher }
2955bcc9736cSJeff Kirsher 
2956bcc9736cSJeff Kirsher static inline void hw_w_phy_polarity(struct ksz_hw *hw, int phy, u16 data)
2957bcc9736cSJeff Kirsher {
2958bcc9736cSJeff Kirsher 	writew(data, hw->io + phy + KS884X_PHY_PHY_CTRL_OFFSET);
2959bcc9736cSJeff Kirsher }
2960bcc9736cSJeff Kirsher 
2961bcc9736cSJeff Kirsher static inline void hw_r_phy_link_md(struct ksz_hw *hw, int phy, u16 *data)
2962bcc9736cSJeff Kirsher {
2963bcc9736cSJeff Kirsher 	*data = readw(hw->io + phy + KS884X_PHY_LINK_MD_OFFSET);
2964bcc9736cSJeff Kirsher }
2965bcc9736cSJeff Kirsher 
2966bcc9736cSJeff Kirsher static inline void hw_w_phy_link_md(struct ksz_hw *hw, int phy, u16 data)
2967bcc9736cSJeff Kirsher {
2968bcc9736cSJeff Kirsher 	writew(data, hw->io + phy + KS884X_PHY_LINK_MD_OFFSET);
2969bcc9736cSJeff Kirsher }
2970bcc9736cSJeff Kirsher 
2971bcc9736cSJeff Kirsher /**
2972bcc9736cSJeff Kirsher  * hw_r_phy - read data from PHY register
2973bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2974bcc9736cSJeff Kirsher  * @port:	Port to read.
2975bcc9736cSJeff Kirsher  * @reg:	PHY register to read.
2976bcc9736cSJeff Kirsher  * @val:	Buffer to store the read data.
2977bcc9736cSJeff Kirsher  *
2978bcc9736cSJeff Kirsher  * This routine reads data from the PHY register.
2979bcc9736cSJeff Kirsher  */
2980bcc9736cSJeff Kirsher static void hw_r_phy(struct ksz_hw *hw, int port, u16 reg, u16 *val)
2981bcc9736cSJeff Kirsher {
2982bcc9736cSJeff Kirsher 	int phy;
2983bcc9736cSJeff Kirsher 
2984bcc9736cSJeff Kirsher 	phy = KS884X_PHY_1_CTRL_OFFSET + port * PHY_CTRL_INTERVAL + reg;
2985bcc9736cSJeff Kirsher 	*val = readw(hw->io + phy);
2986bcc9736cSJeff Kirsher }
2987bcc9736cSJeff Kirsher 
2988bcc9736cSJeff Kirsher /**
2989bcc9736cSJeff Kirsher  * port_w_phy - write data to PHY register
2990bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
2991bcc9736cSJeff Kirsher  * @port:	Port to write.
2992bcc9736cSJeff Kirsher  * @reg:	PHY register to write.
2993bcc9736cSJeff Kirsher  * @val:	Word data to write.
2994bcc9736cSJeff Kirsher  *
2995bcc9736cSJeff Kirsher  * This routine writes data to the PHY register.
2996bcc9736cSJeff Kirsher  */
2997bcc9736cSJeff Kirsher static void hw_w_phy(struct ksz_hw *hw, int port, u16 reg, u16 val)
2998bcc9736cSJeff Kirsher {
2999bcc9736cSJeff Kirsher 	int phy;
3000bcc9736cSJeff Kirsher 
3001bcc9736cSJeff Kirsher 	phy = KS884X_PHY_1_CTRL_OFFSET + port * PHY_CTRL_INTERVAL + reg;
3002bcc9736cSJeff Kirsher 	writew(val, hw->io + phy);
3003bcc9736cSJeff Kirsher }
3004bcc9736cSJeff Kirsher 
3005bcc9736cSJeff Kirsher /*
3006bcc9736cSJeff Kirsher  * EEPROM access functions
3007bcc9736cSJeff Kirsher  */
3008bcc9736cSJeff Kirsher 
3009bcc9736cSJeff Kirsher #define AT93C_CODE			0
3010bcc9736cSJeff Kirsher #define AT93C_WR_OFF			0x00
3011bcc9736cSJeff Kirsher #define AT93C_WR_ALL			0x10
3012bcc9736cSJeff Kirsher #define AT93C_ER_ALL			0x20
3013bcc9736cSJeff Kirsher #define AT93C_WR_ON			0x30
3014bcc9736cSJeff Kirsher 
3015bcc9736cSJeff Kirsher #define AT93C_WRITE			1
3016bcc9736cSJeff Kirsher #define AT93C_READ			2
3017bcc9736cSJeff Kirsher #define AT93C_ERASE			3
3018bcc9736cSJeff Kirsher 
3019bcc9736cSJeff Kirsher #define EEPROM_DELAY			4
3020bcc9736cSJeff Kirsher 
3021bcc9736cSJeff Kirsher static inline void drop_gpio(struct ksz_hw *hw, u8 gpio)
3022bcc9736cSJeff Kirsher {
3023bcc9736cSJeff Kirsher 	u16 data;
3024bcc9736cSJeff Kirsher 
3025bcc9736cSJeff Kirsher 	data = readw(hw->io + KS884X_EEPROM_CTRL_OFFSET);
3026bcc9736cSJeff Kirsher 	data &= ~gpio;
3027bcc9736cSJeff Kirsher 	writew(data, hw->io + KS884X_EEPROM_CTRL_OFFSET);
3028bcc9736cSJeff Kirsher }
3029bcc9736cSJeff Kirsher 
3030bcc9736cSJeff Kirsher static inline void raise_gpio(struct ksz_hw *hw, u8 gpio)
3031bcc9736cSJeff Kirsher {
3032bcc9736cSJeff Kirsher 	u16 data;
3033bcc9736cSJeff Kirsher 
3034bcc9736cSJeff Kirsher 	data = readw(hw->io + KS884X_EEPROM_CTRL_OFFSET);
3035bcc9736cSJeff Kirsher 	data |= gpio;
3036bcc9736cSJeff Kirsher 	writew(data, hw->io + KS884X_EEPROM_CTRL_OFFSET);
3037bcc9736cSJeff Kirsher }
3038bcc9736cSJeff Kirsher 
3039bcc9736cSJeff Kirsher static inline u8 state_gpio(struct ksz_hw *hw, u8 gpio)
3040bcc9736cSJeff Kirsher {
3041bcc9736cSJeff Kirsher 	u16 data;
3042bcc9736cSJeff Kirsher 
3043bcc9736cSJeff Kirsher 	data = readw(hw->io + KS884X_EEPROM_CTRL_OFFSET);
3044bcc9736cSJeff Kirsher 	return (u8)(data & gpio);
3045bcc9736cSJeff Kirsher }
3046bcc9736cSJeff Kirsher 
3047bcc9736cSJeff Kirsher static void eeprom_clk(struct ksz_hw *hw)
3048bcc9736cSJeff Kirsher {
3049bcc9736cSJeff Kirsher 	raise_gpio(hw, EEPROM_SERIAL_CLOCK);
3050bcc9736cSJeff Kirsher 	udelay(EEPROM_DELAY);
3051bcc9736cSJeff Kirsher 	drop_gpio(hw, EEPROM_SERIAL_CLOCK);
3052bcc9736cSJeff Kirsher 	udelay(EEPROM_DELAY);
3053bcc9736cSJeff Kirsher }
3054bcc9736cSJeff Kirsher 
3055bcc9736cSJeff Kirsher static u16 spi_r(struct ksz_hw *hw)
3056bcc9736cSJeff Kirsher {
3057bcc9736cSJeff Kirsher 	int i;
3058bcc9736cSJeff Kirsher 	u16 temp = 0;
3059bcc9736cSJeff Kirsher 
3060bcc9736cSJeff Kirsher 	for (i = 15; i >= 0; i--) {
3061bcc9736cSJeff Kirsher 		raise_gpio(hw, EEPROM_SERIAL_CLOCK);
3062bcc9736cSJeff Kirsher 		udelay(EEPROM_DELAY);
3063bcc9736cSJeff Kirsher 
3064bcc9736cSJeff Kirsher 		temp |= (state_gpio(hw, EEPROM_DATA_IN)) ? 1 << i : 0;
3065bcc9736cSJeff Kirsher 
3066bcc9736cSJeff Kirsher 		drop_gpio(hw, EEPROM_SERIAL_CLOCK);
3067bcc9736cSJeff Kirsher 		udelay(EEPROM_DELAY);
3068bcc9736cSJeff Kirsher 	}
3069bcc9736cSJeff Kirsher 	return temp;
3070bcc9736cSJeff Kirsher }
3071bcc9736cSJeff Kirsher 
3072bcc9736cSJeff Kirsher static void spi_w(struct ksz_hw *hw, u16 data)
3073bcc9736cSJeff Kirsher {
3074bcc9736cSJeff Kirsher 	int i;
3075bcc9736cSJeff Kirsher 
3076bcc9736cSJeff Kirsher 	for (i = 15; i >= 0; i--) {
3077bcc9736cSJeff Kirsher 		(data & (0x01 << i)) ? raise_gpio(hw, EEPROM_DATA_OUT) :
3078bcc9736cSJeff Kirsher 			drop_gpio(hw, EEPROM_DATA_OUT);
3079bcc9736cSJeff Kirsher 		eeprom_clk(hw);
3080bcc9736cSJeff Kirsher 	}
3081bcc9736cSJeff Kirsher }
3082bcc9736cSJeff Kirsher 
3083bcc9736cSJeff Kirsher static void spi_reg(struct ksz_hw *hw, u8 data, u8 reg)
3084bcc9736cSJeff Kirsher {
3085bcc9736cSJeff Kirsher 	int i;
3086bcc9736cSJeff Kirsher 
3087bcc9736cSJeff Kirsher 	/* Initial start bit */
3088bcc9736cSJeff Kirsher 	raise_gpio(hw, EEPROM_DATA_OUT);
3089bcc9736cSJeff Kirsher 	eeprom_clk(hw);
3090bcc9736cSJeff Kirsher 
3091bcc9736cSJeff Kirsher 	/* AT93C operation */
3092bcc9736cSJeff Kirsher 	for (i = 1; i >= 0; i--) {
3093bcc9736cSJeff Kirsher 		(data & (0x01 << i)) ? raise_gpio(hw, EEPROM_DATA_OUT) :
3094bcc9736cSJeff Kirsher 			drop_gpio(hw, EEPROM_DATA_OUT);
3095bcc9736cSJeff Kirsher 		eeprom_clk(hw);
3096bcc9736cSJeff Kirsher 	}
3097bcc9736cSJeff Kirsher 
3098bcc9736cSJeff Kirsher 	/* Address location */
3099bcc9736cSJeff Kirsher 	for (i = 5; i >= 0; i--) {
3100bcc9736cSJeff Kirsher 		(reg & (0x01 << i)) ? raise_gpio(hw, EEPROM_DATA_OUT) :
3101bcc9736cSJeff Kirsher 			drop_gpio(hw, EEPROM_DATA_OUT);
3102bcc9736cSJeff Kirsher 		eeprom_clk(hw);
3103bcc9736cSJeff Kirsher 	}
3104bcc9736cSJeff Kirsher }
3105bcc9736cSJeff Kirsher 
3106bcc9736cSJeff Kirsher #define EEPROM_DATA_RESERVED		0
3107bcc9736cSJeff Kirsher #define EEPROM_DATA_MAC_ADDR_0		1
3108bcc9736cSJeff Kirsher #define EEPROM_DATA_MAC_ADDR_1		2
3109bcc9736cSJeff Kirsher #define EEPROM_DATA_MAC_ADDR_2		3
3110bcc9736cSJeff Kirsher #define EEPROM_DATA_SUBSYS_ID		4
3111bcc9736cSJeff Kirsher #define EEPROM_DATA_SUBSYS_VEN_ID	5
3112bcc9736cSJeff Kirsher #define EEPROM_DATA_PM_CAP		6
3113bcc9736cSJeff Kirsher 
3114bcc9736cSJeff Kirsher /* User defined EEPROM data */
3115bcc9736cSJeff Kirsher #define EEPROM_DATA_OTHER_MAC_ADDR	9
3116bcc9736cSJeff Kirsher 
3117bcc9736cSJeff Kirsher /**
3118bcc9736cSJeff Kirsher  * eeprom_read - read from AT93C46 EEPROM
3119bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3120bcc9736cSJeff Kirsher  * @reg:	The register offset.
3121bcc9736cSJeff Kirsher  *
3122bcc9736cSJeff Kirsher  * This function reads a word from the AT93C46 EEPROM.
3123bcc9736cSJeff Kirsher  *
3124bcc9736cSJeff Kirsher  * Return the data value.
3125bcc9736cSJeff Kirsher  */
3126bcc9736cSJeff Kirsher static u16 eeprom_read(struct ksz_hw *hw, u8 reg)
3127bcc9736cSJeff Kirsher {
3128bcc9736cSJeff Kirsher 	u16 data;
3129bcc9736cSJeff Kirsher 
3130bcc9736cSJeff Kirsher 	raise_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
3131bcc9736cSJeff Kirsher 
3132bcc9736cSJeff Kirsher 	spi_reg(hw, AT93C_READ, reg);
3133bcc9736cSJeff Kirsher 	data = spi_r(hw);
3134bcc9736cSJeff Kirsher 
3135bcc9736cSJeff Kirsher 	drop_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
3136bcc9736cSJeff Kirsher 
3137bcc9736cSJeff Kirsher 	return data;
3138bcc9736cSJeff Kirsher }
3139bcc9736cSJeff Kirsher 
3140bcc9736cSJeff Kirsher /**
3141bcc9736cSJeff Kirsher  * eeprom_write - write to AT93C46 EEPROM
3142bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3143bcc9736cSJeff Kirsher  * @reg:	The register offset.
3144bcc9736cSJeff Kirsher  * @data:	The data value.
3145bcc9736cSJeff Kirsher  *
3146bcc9736cSJeff Kirsher  * This procedure writes a word to the AT93C46 EEPROM.
3147bcc9736cSJeff Kirsher  */
3148bcc9736cSJeff Kirsher static void eeprom_write(struct ksz_hw *hw, u8 reg, u16 data)
3149bcc9736cSJeff Kirsher {
3150bcc9736cSJeff Kirsher 	int timeout;
3151bcc9736cSJeff Kirsher 
3152bcc9736cSJeff Kirsher 	raise_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
3153bcc9736cSJeff Kirsher 
3154bcc9736cSJeff Kirsher 	/* Enable write. */
3155bcc9736cSJeff Kirsher 	spi_reg(hw, AT93C_CODE, AT93C_WR_ON);
3156bcc9736cSJeff Kirsher 	drop_gpio(hw, EEPROM_CHIP_SELECT);
3157bcc9736cSJeff Kirsher 	udelay(1);
3158bcc9736cSJeff Kirsher 
3159bcc9736cSJeff Kirsher 	/* Erase the register. */
3160bcc9736cSJeff Kirsher 	raise_gpio(hw, EEPROM_CHIP_SELECT);
3161bcc9736cSJeff Kirsher 	spi_reg(hw, AT93C_ERASE, reg);
3162bcc9736cSJeff Kirsher 	drop_gpio(hw, EEPROM_CHIP_SELECT);
3163bcc9736cSJeff Kirsher 	udelay(1);
3164bcc9736cSJeff Kirsher 
3165bcc9736cSJeff Kirsher 	/* Check operation complete. */
3166bcc9736cSJeff Kirsher 	raise_gpio(hw, EEPROM_CHIP_SELECT);
3167bcc9736cSJeff Kirsher 	timeout = 8;
3168bcc9736cSJeff Kirsher 	mdelay(2);
3169bcc9736cSJeff Kirsher 	do {
3170bcc9736cSJeff Kirsher 		mdelay(1);
3171bcc9736cSJeff Kirsher 	} while (!state_gpio(hw, EEPROM_DATA_IN) && --timeout);
3172bcc9736cSJeff Kirsher 	drop_gpio(hw, EEPROM_CHIP_SELECT);
3173bcc9736cSJeff Kirsher 	udelay(1);
3174bcc9736cSJeff Kirsher 
3175bcc9736cSJeff Kirsher 	/* Write the register. */
3176bcc9736cSJeff Kirsher 	raise_gpio(hw, EEPROM_CHIP_SELECT);
3177bcc9736cSJeff Kirsher 	spi_reg(hw, AT93C_WRITE, reg);
3178bcc9736cSJeff Kirsher 	spi_w(hw, data);
3179bcc9736cSJeff Kirsher 	drop_gpio(hw, EEPROM_CHIP_SELECT);
3180bcc9736cSJeff Kirsher 	udelay(1);
3181bcc9736cSJeff Kirsher 
3182bcc9736cSJeff Kirsher 	/* Check operation complete. */
3183bcc9736cSJeff Kirsher 	raise_gpio(hw, EEPROM_CHIP_SELECT);
3184bcc9736cSJeff Kirsher 	timeout = 8;
3185bcc9736cSJeff Kirsher 	mdelay(2);
3186bcc9736cSJeff Kirsher 	do {
3187bcc9736cSJeff Kirsher 		mdelay(1);
3188bcc9736cSJeff Kirsher 	} while (!state_gpio(hw, EEPROM_DATA_IN) && --timeout);
3189bcc9736cSJeff Kirsher 	drop_gpio(hw, EEPROM_CHIP_SELECT);
3190bcc9736cSJeff Kirsher 	udelay(1);
3191bcc9736cSJeff Kirsher 
3192bcc9736cSJeff Kirsher 	/* Disable write. */
3193bcc9736cSJeff Kirsher 	raise_gpio(hw, EEPROM_CHIP_SELECT);
3194bcc9736cSJeff Kirsher 	spi_reg(hw, AT93C_CODE, AT93C_WR_OFF);
3195bcc9736cSJeff Kirsher 
3196bcc9736cSJeff Kirsher 	drop_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
3197bcc9736cSJeff Kirsher }
3198bcc9736cSJeff Kirsher 
3199bcc9736cSJeff Kirsher /*
3200bcc9736cSJeff Kirsher  * Link detection routines
3201bcc9736cSJeff Kirsher  */
3202bcc9736cSJeff Kirsher 
3203bcc9736cSJeff Kirsher static u16 advertised_flow_ctrl(struct ksz_port *port, u16 ctrl)
3204bcc9736cSJeff Kirsher {
3205bcc9736cSJeff Kirsher 	ctrl &= ~PORT_AUTO_NEG_SYM_PAUSE;
3206bcc9736cSJeff Kirsher 	switch (port->flow_ctrl) {
3207bcc9736cSJeff Kirsher 	case PHY_FLOW_CTRL:
3208bcc9736cSJeff Kirsher 		ctrl |= PORT_AUTO_NEG_SYM_PAUSE;
3209bcc9736cSJeff Kirsher 		break;
3210bcc9736cSJeff Kirsher 	/* Not supported. */
3211bcc9736cSJeff Kirsher 	case PHY_TX_ONLY:
3212bcc9736cSJeff Kirsher 	case PHY_RX_ONLY:
3213bcc9736cSJeff Kirsher 	default:
3214bcc9736cSJeff Kirsher 		break;
3215bcc9736cSJeff Kirsher 	}
3216bcc9736cSJeff Kirsher 	return ctrl;
3217bcc9736cSJeff Kirsher }
3218bcc9736cSJeff Kirsher 
3219bcc9736cSJeff Kirsher static void set_flow_ctrl(struct ksz_hw *hw, int rx, int tx)
3220bcc9736cSJeff Kirsher {
3221bcc9736cSJeff Kirsher 	u32 rx_cfg;
3222bcc9736cSJeff Kirsher 	u32 tx_cfg;
3223bcc9736cSJeff Kirsher 
3224bcc9736cSJeff Kirsher 	rx_cfg = hw->rx_cfg;
3225bcc9736cSJeff Kirsher 	tx_cfg = hw->tx_cfg;
3226bcc9736cSJeff Kirsher 	if (rx)
3227bcc9736cSJeff Kirsher 		hw->rx_cfg |= DMA_RX_FLOW_ENABLE;
3228bcc9736cSJeff Kirsher 	else
3229bcc9736cSJeff Kirsher 		hw->rx_cfg &= ~DMA_RX_FLOW_ENABLE;
3230bcc9736cSJeff Kirsher 	if (tx)
3231bcc9736cSJeff Kirsher 		hw->tx_cfg |= DMA_TX_FLOW_ENABLE;
3232bcc9736cSJeff Kirsher 	else
3233bcc9736cSJeff Kirsher 		hw->tx_cfg &= ~DMA_TX_FLOW_ENABLE;
3234bcc9736cSJeff Kirsher 	if (hw->enabled) {
3235bcc9736cSJeff Kirsher 		if (rx_cfg != hw->rx_cfg)
3236bcc9736cSJeff Kirsher 			writel(hw->rx_cfg, hw->io + KS_DMA_RX_CTRL);
3237bcc9736cSJeff Kirsher 		if (tx_cfg != hw->tx_cfg)
3238bcc9736cSJeff Kirsher 			writel(hw->tx_cfg, hw->io + KS_DMA_TX_CTRL);
3239bcc9736cSJeff Kirsher 	}
3240bcc9736cSJeff Kirsher }
3241bcc9736cSJeff Kirsher 
3242bcc9736cSJeff Kirsher static void determine_flow_ctrl(struct ksz_hw *hw, struct ksz_port *port,
3243bcc9736cSJeff Kirsher 	u16 local, u16 remote)
3244bcc9736cSJeff Kirsher {
3245bcc9736cSJeff Kirsher 	int rx;
3246bcc9736cSJeff Kirsher 	int tx;
3247bcc9736cSJeff Kirsher 
3248bcc9736cSJeff Kirsher 	if (hw->overrides & PAUSE_FLOW_CTRL)
3249bcc9736cSJeff Kirsher 		return;
3250bcc9736cSJeff Kirsher 
3251bcc9736cSJeff Kirsher 	rx = tx = 0;
3252bcc9736cSJeff Kirsher 	if (port->force_link)
3253bcc9736cSJeff Kirsher 		rx = tx = 1;
3254bcc9736cSJeff Kirsher 	if (remote & PHY_AUTO_NEG_SYM_PAUSE) {
3255bcc9736cSJeff Kirsher 		if (local & PHY_AUTO_NEG_SYM_PAUSE) {
3256bcc9736cSJeff Kirsher 			rx = tx = 1;
3257bcc9736cSJeff Kirsher 		} else if ((remote & PHY_AUTO_NEG_ASYM_PAUSE) &&
3258bcc9736cSJeff Kirsher 				(local & PHY_AUTO_NEG_PAUSE) ==
3259bcc9736cSJeff Kirsher 				PHY_AUTO_NEG_ASYM_PAUSE) {
3260bcc9736cSJeff Kirsher 			tx = 1;
3261bcc9736cSJeff Kirsher 		}
3262bcc9736cSJeff Kirsher 	} else if (remote & PHY_AUTO_NEG_ASYM_PAUSE) {
3263bcc9736cSJeff Kirsher 		if ((local & PHY_AUTO_NEG_PAUSE) == PHY_AUTO_NEG_PAUSE)
3264bcc9736cSJeff Kirsher 			rx = 1;
3265bcc9736cSJeff Kirsher 	}
3266bcc9736cSJeff Kirsher 	if (!hw->ksz_switch)
3267bcc9736cSJeff Kirsher 		set_flow_ctrl(hw, rx, tx);
3268bcc9736cSJeff Kirsher }
3269bcc9736cSJeff Kirsher 
3270bcc9736cSJeff Kirsher static inline void port_cfg_change(struct ksz_hw *hw, struct ksz_port *port,
3271bcc9736cSJeff Kirsher 	struct ksz_port_info *info, u16 link_status)
3272bcc9736cSJeff Kirsher {
3273bcc9736cSJeff Kirsher 	if ((hw->features & HALF_DUPLEX_SIGNAL_BUG) &&
3274bcc9736cSJeff Kirsher 			!(hw->overrides & PAUSE_FLOW_CTRL)) {
3275bcc9736cSJeff Kirsher 		u32 cfg = hw->tx_cfg;
3276bcc9736cSJeff Kirsher 
3277bcc9736cSJeff Kirsher 		/* Disable flow control in the half duplex mode. */
3278bcc9736cSJeff Kirsher 		if (1 == info->duplex)
3279bcc9736cSJeff Kirsher 			hw->tx_cfg &= ~DMA_TX_FLOW_ENABLE;
3280bcc9736cSJeff Kirsher 		if (hw->enabled && cfg != hw->tx_cfg)
3281bcc9736cSJeff Kirsher 			writel(hw->tx_cfg, hw->io + KS_DMA_TX_CTRL);
3282bcc9736cSJeff Kirsher 	}
3283bcc9736cSJeff Kirsher }
3284bcc9736cSJeff Kirsher 
3285bcc9736cSJeff Kirsher /**
3286bcc9736cSJeff Kirsher  * port_get_link_speed - get current link status
3287bcc9736cSJeff Kirsher  * @port: 	The port instance.
3288bcc9736cSJeff Kirsher  *
3289bcc9736cSJeff Kirsher  * This routine reads PHY registers to determine the current link status of the
3290bcc9736cSJeff Kirsher  * switch ports.
3291bcc9736cSJeff Kirsher  */
3292bcc9736cSJeff Kirsher static void port_get_link_speed(struct ksz_port *port)
3293bcc9736cSJeff Kirsher {
3294bcc9736cSJeff Kirsher 	uint interrupt;
3295bcc9736cSJeff Kirsher 	struct ksz_port_info *info;
3296bcc9736cSJeff Kirsher 	struct ksz_port_info *linked = NULL;
3297bcc9736cSJeff Kirsher 	struct ksz_hw *hw = port->hw;
3298bcc9736cSJeff Kirsher 	u16 data;
3299bcc9736cSJeff Kirsher 	u16 status;
3300bcc9736cSJeff Kirsher 	u8 local;
3301bcc9736cSJeff Kirsher 	u8 remote;
3302bcc9736cSJeff Kirsher 	int i;
3303bcc9736cSJeff Kirsher 	int p;
3304bcc9736cSJeff Kirsher 	int change = 0;
3305bcc9736cSJeff Kirsher 
3306bcc9736cSJeff Kirsher 	interrupt = hw_block_intr(hw);
3307bcc9736cSJeff Kirsher 
3308bcc9736cSJeff Kirsher 	for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
3309bcc9736cSJeff Kirsher 		info = &hw->port_info[p];
3310bcc9736cSJeff Kirsher 		port_r16(hw, p, KS884X_PORT_CTRL_4_OFFSET, &data);
3311bcc9736cSJeff Kirsher 		port_r16(hw, p, KS884X_PORT_STATUS_OFFSET, &status);
3312bcc9736cSJeff Kirsher 
3313bcc9736cSJeff Kirsher 		/*
3314bcc9736cSJeff Kirsher 		 * Link status is changing all the time even when there is no
3315bcc9736cSJeff Kirsher 		 * cable connection!
3316bcc9736cSJeff Kirsher 		 */
3317bcc9736cSJeff Kirsher 		remote = status & (PORT_AUTO_NEG_COMPLETE |
3318bcc9736cSJeff Kirsher 			PORT_STATUS_LINK_GOOD);
3319bcc9736cSJeff Kirsher 		local = (u8) data;
3320bcc9736cSJeff Kirsher 
3321bcc9736cSJeff Kirsher 		/* No change to status. */
3322bcc9736cSJeff Kirsher 		if (local == info->advertised && remote == info->partner)
3323bcc9736cSJeff Kirsher 			continue;
3324bcc9736cSJeff Kirsher 
3325bcc9736cSJeff Kirsher 		info->advertised = local;
3326bcc9736cSJeff Kirsher 		info->partner = remote;
3327bcc9736cSJeff Kirsher 		if (status & PORT_STATUS_LINK_GOOD) {
3328bcc9736cSJeff Kirsher 
3329bcc9736cSJeff Kirsher 			/* Remember the first linked port. */
3330bcc9736cSJeff Kirsher 			if (!linked)
3331bcc9736cSJeff Kirsher 				linked = info;
3332bcc9736cSJeff Kirsher 
3333bcc9736cSJeff Kirsher 			info->tx_rate = 10 * TX_RATE_UNIT;
3334bcc9736cSJeff Kirsher 			if (status & PORT_STATUS_SPEED_100MBIT)
3335bcc9736cSJeff Kirsher 				info->tx_rate = 100 * TX_RATE_UNIT;
3336bcc9736cSJeff Kirsher 
3337bcc9736cSJeff Kirsher 			info->duplex = 1;
3338bcc9736cSJeff Kirsher 			if (status & PORT_STATUS_FULL_DUPLEX)
3339bcc9736cSJeff Kirsher 				info->duplex = 2;
3340bcc9736cSJeff Kirsher 
3341bcc9736cSJeff Kirsher 			if (media_connected != info->state) {
3342bcc9736cSJeff Kirsher 				hw_r_phy(hw, p, KS884X_PHY_AUTO_NEG_OFFSET,
3343bcc9736cSJeff Kirsher 					&data);
3344bcc9736cSJeff Kirsher 				hw_r_phy(hw, p, KS884X_PHY_REMOTE_CAP_OFFSET,
3345bcc9736cSJeff Kirsher 					&status);
3346bcc9736cSJeff Kirsher 				determine_flow_ctrl(hw, port, data, status);
3347bcc9736cSJeff Kirsher 				if (hw->ksz_switch) {
3348bcc9736cSJeff Kirsher 					port_cfg_back_pressure(hw, p,
3349bcc9736cSJeff Kirsher 						(1 == info->duplex));
3350bcc9736cSJeff Kirsher 				}
3351bcc9736cSJeff Kirsher 				change |= 1 << i;
3352bcc9736cSJeff Kirsher 				port_cfg_change(hw, port, info, status);
3353bcc9736cSJeff Kirsher 			}
3354bcc9736cSJeff Kirsher 			info->state = media_connected;
3355bcc9736cSJeff Kirsher 		} else {
3356bcc9736cSJeff Kirsher 			if (media_disconnected != info->state) {
3357bcc9736cSJeff Kirsher 				change |= 1 << i;
3358bcc9736cSJeff Kirsher 
3359bcc9736cSJeff Kirsher 				/* Indicate the link just goes down. */
3360bcc9736cSJeff Kirsher 				hw->port_mib[p].link_down = 1;
3361bcc9736cSJeff Kirsher 			}
3362bcc9736cSJeff Kirsher 			info->state = media_disconnected;
3363bcc9736cSJeff Kirsher 		}
3364bcc9736cSJeff Kirsher 		hw->port_mib[p].state = (u8) info->state;
3365bcc9736cSJeff Kirsher 	}
3366bcc9736cSJeff Kirsher 
3367bcc9736cSJeff Kirsher 	if (linked && media_disconnected == port->linked->state)
3368bcc9736cSJeff Kirsher 		port->linked = linked;
3369bcc9736cSJeff Kirsher 
3370bcc9736cSJeff Kirsher 	hw_restore_intr(hw, interrupt);
3371bcc9736cSJeff Kirsher }
3372bcc9736cSJeff Kirsher 
3373bcc9736cSJeff Kirsher #define PHY_RESET_TIMEOUT		10
3374bcc9736cSJeff Kirsher 
3375bcc9736cSJeff Kirsher /**
3376bcc9736cSJeff Kirsher  * port_set_link_speed - set port speed
3377bcc9736cSJeff Kirsher  * @port: 	The port instance.
3378bcc9736cSJeff Kirsher  *
3379bcc9736cSJeff Kirsher  * This routine sets the link speed of the switch ports.
3380bcc9736cSJeff Kirsher  */
3381bcc9736cSJeff Kirsher static void port_set_link_speed(struct ksz_port *port)
3382bcc9736cSJeff Kirsher {
3383bcc9736cSJeff Kirsher 	struct ksz_port_info *info;
3384bcc9736cSJeff Kirsher 	struct ksz_hw *hw = port->hw;
3385bcc9736cSJeff Kirsher 	u16 data;
3386bcc9736cSJeff Kirsher 	u16 cfg;
3387bcc9736cSJeff Kirsher 	u8 status;
3388bcc9736cSJeff Kirsher 	int i;
3389bcc9736cSJeff Kirsher 	int p;
3390bcc9736cSJeff Kirsher 
3391bcc9736cSJeff Kirsher 	for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
3392bcc9736cSJeff Kirsher 		info = &hw->port_info[p];
3393bcc9736cSJeff Kirsher 
3394bcc9736cSJeff Kirsher 		port_r16(hw, p, KS884X_PORT_CTRL_4_OFFSET, &data);
3395bcc9736cSJeff Kirsher 		port_r8(hw, p, KS884X_PORT_STATUS_OFFSET, &status);
3396bcc9736cSJeff Kirsher 
3397bcc9736cSJeff Kirsher 		cfg = 0;
3398bcc9736cSJeff Kirsher 		if (status & PORT_STATUS_LINK_GOOD)
3399bcc9736cSJeff Kirsher 			cfg = data;
3400bcc9736cSJeff Kirsher 
3401bcc9736cSJeff Kirsher 		data |= PORT_AUTO_NEG_ENABLE;
3402bcc9736cSJeff Kirsher 		data = advertised_flow_ctrl(port, data);
3403bcc9736cSJeff Kirsher 
3404bcc9736cSJeff Kirsher 		data |= PORT_AUTO_NEG_100BTX_FD | PORT_AUTO_NEG_100BTX |
3405bcc9736cSJeff Kirsher 			PORT_AUTO_NEG_10BT_FD | PORT_AUTO_NEG_10BT;
3406bcc9736cSJeff Kirsher 
3407bcc9736cSJeff Kirsher 		/* Check if manual configuration is specified by the user. */
3408bcc9736cSJeff Kirsher 		if (port->speed || port->duplex) {
3409bcc9736cSJeff Kirsher 			if (10 == port->speed)
3410bcc9736cSJeff Kirsher 				data &= ~(PORT_AUTO_NEG_100BTX_FD |
3411bcc9736cSJeff Kirsher 					PORT_AUTO_NEG_100BTX);
3412bcc9736cSJeff Kirsher 			else if (100 == port->speed)
3413bcc9736cSJeff Kirsher 				data &= ~(PORT_AUTO_NEG_10BT_FD |
3414bcc9736cSJeff Kirsher 					PORT_AUTO_NEG_10BT);
3415bcc9736cSJeff Kirsher 			if (1 == port->duplex)
3416bcc9736cSJeff Kirsher 				data &= ~(PORT_AUTO_NEG_100BTX_FD |
3417bcc9736cSJeff Kirsher 					PORT_AUTO_NEG_10BT_FD);
3418bcc9736cSJeff Kirsher 			else if (2 == port->duplex)
3419bcc9736cSJeff Kirsher 				data &= ~(PORT_AUTO_NEG_100BTX |
3420bcc9736cSJeff Kirsher 					PORT_AUTO_NEG_10BT);
3421bcc9736cSJeff Kirsher 		}
3422bcc9736cSJeff Kirsher 		if (data != cfg) {
3423bcc9736cSJeff Kirsher 			data |= PORT_AUTO_NEG_RESTART;
3424bcc9736cSJeff Kirsher 			port_w16(hw, p, KS884X_PORT_CTRL_4_OFFSET, data);
3425bcc9736cSJeff Kirsher 		}
3426bcc9736cSJeff Kirsher 	}
3427bcc9736cSJeff Kirsher }
3428bcc9736cSJeff Kirsher 
3429bcc9736cSJeff Kirsher /**
3430bcc9736cSJeff Kirsher  * port_force_link_speed - force port speed
3431bcc9736cSJeff Kirsher  * @port: 	The port instance.
3432bcc9736cSJeff Kirsher  *
3433bcc9736cSJeff Kirsher  * This routine forces the link speed of the switch ports.
3434bcc9736cSJeff Kirsher  */
3435bcc9736cSJeff Kirsher static void port_force_link_speed(struct ksz_port *port)
3436bcc9736cSJeff Kirsher {
3437bcc9736cSJeff Kirsher 	struct ksz_hw *hw = port->hw;
3438bcc9736cSJeff Kirsher 	u16 data;
3439bcc9736cSJeff Kirsher 	int i;
3440bcc9736cSJeff Kirsher 	int phy;
3441bcc9736cSJeff Kirsher 	int p;
3442bcc9736cSJeff Kirsher 
3443bcc9736cSJeff Kirsher 	for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
3444bcc9736cSJeff Kirsher 		phy = KS884X_PHY_1_CTRL_OFFSET + p * PHY_CTRL_INTERVAL;
3445bcc9736cSJeff Kirsher 		hw_r_phy_ctrl(hw, phy, &data);
3446bcc9736cSJeff Kirsher 
3447bcc9736cSJeff Kirsher 		data &= ~PHY_AUTO_NEG_ENABLE;
3448bcc9736cSJeff Kirsher 
3449bcc9736cSJeff Kirsher 		if (10 == port->speed)
3450bcc9736cSJeff Kirsher 			data &= ~PHY_SPEED_100MBIT;
3451bcc9736cSJeff Kirsher 		else if (100 == port->speed)
3452bcc9736cSJeff Kirsher 			data |= PHY_SPEED_100MBIT;
3453bcc9736cSJeff Kirsher 		if (1 == port->duplex)
3454bcc9736cSJeff Kirsher 			data &= ~PHY_FULL_DUPLEX;
3455bcc9736cSJeff Kirsher 		else if (2 == port->duplex)
3456bcc9736cSJeff Kirsher 			data |= PHY_FULL_DUPLEX;
3457bcc9736cSJeff Kirsher 		hw_w_phy_ctrl(hw, phy, data);
3458bcc9736cSJeff Kirsher 	}
3459bcc9736cSJeff Kirsher }
3460bcc9736cSJeff Kirsher 
3461bcc9736cSJeff Kirsher static void port_set_power_saving(struct ksz_port *port, int enable)
3462bcc9736cSJeff Kirsher {
3463bcc9736cSJeff Kirsher 	struct ksz_hw *hw = port->hw;
3464bcc9736cSJeff Kirsher 	int i;
3465bcc9736cSJeff Kirsher 	int p;
3466bcc9736cSJeff Kirsher 
3467bcc9736cSJeff Kirsher 	for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++)
3468bcc9736cSJeff Kirsher 		port_cfg(hw, p,
3469bcc9736cSJeff Kirsher 			KS884X_PORT_CTRL_4_OFFSET, PORT_POWER_DOWN, enable);
3470bcc9736cSJeff Kirsher }
3471bcc9736cSJeff Kirsher 
3472bcc9736cSJeff Kirsher /*
3473bcc9736cSJeff Kirsher  * KSZ8841 power management functions
3474bcc9736cSJeff Kirsher  */
3475bcc9736cSJeff Kirsher 
3476bcc9736cSJeff Kirsher /**
3477bcc9736cSJeff Kirsher  * hw_chk_wol_pme_status - check PMEN pin
3478bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3479bcc9736cSJeff Kirsher  *
3480bcc9736cSJeff Kirsher  * This function is used to check PMEN pin is asserted.
3481bcc9736cSJeff Kirsher  *
3482bcc9736cSJeff Kirsher  * Return 1 if PMEN pin is asserted; otherwise, 0.
3483bcc9736cSJeff Kirsher  */
3484bcc9736cSJeff Kirsher static int hw_chk_wol_pme_status(struct ksz_hw *hw)
3485bcc9736cSJeff Kirsher {
3486bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = container_of(hw, struct dev_info, hw);
3487bcc9736cSJeff Kirsher 	struct pci_dev *pdev = hw_priv->pdev;
3488bcc9736cSJeff Kirsher 	u16 data;
3489bcc9736cSJeff Kirsher 
3490bcc9736cSJeff Kirsher 	if (!pdev->pm_cap)
3491bcc9736cSJeff Kirsher 		return 0;
3492bcc9736cSJeff Kirsher 	pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &data);
3493bcc9736cSJeff Kirsher 	return (data & PCI_PM_CTRL_PME_STATUS) == PCI_PM_CTRL_PME_STATUS;
3494bcc9736cSJeff Kirsher }
3495bcc9736cSJeff Kirsher 
3496bcc9736cSJeff Kirsher /**
3497bcc9736cSJeff Kirsher  * hw_clr_wol_pme_status - clear PMEN pin
3498bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3499bcc9736cSJeff Kirsher  *
3500bcc9736cSJeff Kirsher  * This routine is used to clear PME_Status to deassert PMEN pin.
3501bcc9736cSJeff Kirsher  */
3502bcc9736cSJeff Kirsher static void hw_clr_wol_pme_status(struct ksz_hw *hw)
3503bcc9736cSJeff Kirsher {
3504bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = container_of(hw, struct dev_info, hw);
3505bcc9736cSJeff Kirsher 	struct pci_dev *pdev = hw_priv->pdev;
3506bcc9736cSJeff Kirsher 	u16 data;
3507bcc9736cSJeff Kirsher 
3508bcc9736cSJeff Kirsher 	if (!pdev->pm_cap)
3509bcc9736cSJeff Kirsher 		return;
3510bcc9736cSJeff Kirsher 
3511bcc9736cSJeff Kirsher 	/* Clear PME_Status to deassert PMEN pin. */
3512bcc9736cSJeff Kirsher 	pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &data);
3513bcc9736cSJeff Kirsher 	data |= PCI_PM_CTRL_PME_STATUS;
3514bcc9736cSJeff Kirsher 	pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, data);
3515bcc9736cSJeff Kirsher }
3516bcc9736cSJeff Kirsher 
3517bcc9736cSJeff Kirsher /**
3518bcc9736cSJeff Kirsher  * hw_cfg_wol_pme - enable or disable Wake-on-LAN
3519bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3520bcc9736cSJeff Kirsher  * @set:	The flag indicating whether to enable or disable.
3521bcc9736cSJeff Kirsher  *
3522bcc9736cSJeff Kirsher  * This routine is used to enable or disable Wake-on-LAN.
3523bcc9736cSJeff Kirsher  */
3524bcc9736cSJeff Kirsher static void hw_cfg_wol_pme(struct ksz_hw *hw, int set)
3525bcc9736cSJeff Kirsher {
3526bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = container_of(hw, struct dev_info, hw);
3527bcc9736cSJeff Kirsher 	struct pci_dev *pdev = hw_priv->pdev;
3528bcc9736cSJeff Kirsher 	u16 data;
3529bcc9736cSJeff Kirsher 
3530bcc9736cSJeff Kirsher 	if (!pdev->pm_cap)
3531bcc9736cSJeff Kirsher 		return;
3532bcc9736cSJeff Kirsher 	pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &data);
3533bcc9736cSJeff Kirsher 	data &= ~PCI_PM_CTRL_STATE_MASK;
3534bcc9736cSJeff Kirsher 	if (set)
3535bcc9736cSJeff Kirsher 		data |= PCI_PM_CTRL_PME_ENABLE | PCI_D3hot;
3536bcc9736cSJeff Kirsher 	else
3537bcc9736cSJeff Kirsher 		data &= ~PCI_PM_CTRL_PME_ENABLE;
3538bcc9736cSJeff Kirsher 	pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, data);
3539bcc9736cSJeff Kirsher }
3540bcc9736cSJeff Kirsher 
3541bcc9736cSJeff Kirsher /**
3542bcc9736cSJeff Kirsher  * hw_cfg_wol - configure Wake-on-LAN features
3543bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3544bcc9736cSJeff Kirsher  * @frame:	The pattern frame bit.
3545bcc9736cSJeff Kirsher  * @set:	The flag indicating whether to enable or disable.
3546bcc9736cSJeff Kirsher  *
3547bcc9736cSJeff Kirsher  * This routine is used to enable or disable certain Wake-on-LAN features.
3548bcc9736cSJeff Kirsher  */
3549bcc9736cSJeff Kirsher static void hw_cfg_wol(struct ksz_hw *hw, u16 frame, int set)
3550bcc9736cSJeff Kirsher {
3551bcc9736cSJeff Kirsher 	u16 data;
3552bcc9736cSJeff Kirsher 
3553bcc9736cSJeff Kirsher 	data = readw(hw->io + KS8841_WOL_CTRL_OFFSET);
3554bcc9736cSJeff Kirsher 	if (set)
3555bcc9736cSJeff Kirsher 		data |= frame;
3556bcc9736cSJeff Kirsher 	else
3557bcc9736cSJeff Kirsher 		data &= ~frame;
3558bcc9736cSJeff Kirsher 	writew(data, hw->io + KS8841_WOL_CTRL_OFFSET);
3559bcc9736cSJeff Kirsher }
3560bcc9736cSJeff Kirsher 
3561bcc9736cSJeff Kirsher /**
3562bcc9736cSJeff Kirsher  * hw_set_wol_frame - program Wake-on-LAN pattern
3563bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3564bcc9736cSJeff Kirsher  * @i:		The frame index.
3565bcc9736cSJeff Kirsher  * @mask_size:	The size of the mask.
3566bcc9736cSJeff Kirsher  * @mask:	Mask to ignore certain bytes in the pattern.
3567bcc9736cSJeff Kirsher  * @frame_size:	The size of the frame.
3568bcc9736cSJeff Kirsher  * @pattern:	The frame data.
3569bcc9736cSJeff Kirsher  *
3570bcc9736cSJeff Kirsher  * This routine is used to program Wake-on-LAN pattern.
3571bcc9736cSJeff Kirsher  */
3572bcc9736cSJeff Kirsher static void hw_set_wol_frame(struct ksz_hw *hw, int i, uint mask_size,
3573bcc9736cSJeff Kirsher 	const u8 *mask, uint frame_size, const u8 *pattern)
3574bcc9736cSJeff Kirsher {
3575bcc9736cSJeff Kirsher 	int bits;
3576bcc9736cSJeff Kirsher 	int from;
3577bcc9736cSJeff Kirsher 	int len;
3578bcc9736cSJeff Kirsher 	int to;
3579bcc9736cSJeff Kirsher 	u32 crc;
3580bcc9736cSJeff Kirsher 	u8 data[64];
3581bcc9736cSJeff Kirsher 	u8 val = 0;
3582bcc9736cSJeff Kirsher 
3583bcc9736cSJeff Kirsher 	if (frame_size > mask_size * 8)
3584bcc9736cSJeff Kirsher 		frame_size = mask_size * 8;
3585bcc9736cSJeff Kirsher 	if (frame_size > 64)
3586bcc9736cSJeff Kirsher 		frame_size = 64;
3587bcc9736cSJeff Kirsher 
3588bcc9736cSJeff Kirsher 	i *= 0x10;
3589bcc9736cSJeff Kirsher 	writel(0, hw->io + KS8841_WOL_FRAME_BYTE0_OFFSET + i);
3590bcc9736cSJeff Kirsher 	writel(0, hw->io + KS8841_WOL_FRAME_BYTE2_OFFSET + i);
3591bcc9736cSJeff Kirsher 
3592bcc9736cSJeff Kirsher 	bits = len = from = to = 0;
3593bcc9736cSJeff Kirsher 	do {
3594bcc9736cSJeff Kirsher 		if (bits) {
3595bcc9736cSJeff Kirsher 			if ((val & 1))
3596bcc9736cSJeff Kirsher 				data[to++] = pattern[from];
3597bcc9736cSJeff Kirsher 			val >>= 1;
3598bcc9736cSJeff Kirsher 			++from;
3599bcc9736cSJeff Kirsher 			--bits;
3600bcc9736cSJeff Kirsher 		} else {
3601bcc9736cSJeff Kirsher 			val = mask[len];
3602bcc9736cSJeff Kirsher 			writeb(val, hw->io + KS8841_WOL_FRAME_BYTE0_OFFSET + i
3603bcc9736cSJeff Kirsher 				+ len);
3604bcc9736cSJeff Kirsher 			++len;
3605bcc9736cSJeff Kirsher 			if (val)
3606bcc9736cSJeff Kirsher 				bits = 8;
3607bcc9736cSJeff Kirsher 			else
3608bcc9736cSJeff Kirsher 				from += 8;
3609bcc9736cSJeff Kirsher 		}
3610bcc9736cSJeff Kirsher 	} while (from < (int) frame_size);
3611bcc9736cSJeff Kirsher 	if (val) {
3612bcc9736cSJeff Kirsher 		bits = mask[len - 1];
3613bcc9736cSJeff Kirsher 		val <<= (from % 8);
3614bcc9736cSJeff Kirsher 		bits &= ~val;
3615bcc9736cSJeff Kirsher 		writeb(bits, hw->io + KS8841_WOL_FRAME_BYTE0_OFFSET + i + len -
3616bcc9736cSJeff Kirsher 			1);
3617bcc9736cSJeff Kirsher 	}
3618bcc9736cSJeff Kirsher 	crc = ether_crc(to, data);
3619bcc9736cSJeff Kirsher 	writel(crc, hw->io + KS8841_WOL_FRAME_CRC_OFFSET + i);
3620bcc9736cSJeff Kirsher }
3621bcc9736cSJeff Kirsher 
3622bcc9736cSJeff Kirsher /**
3623bcc9736cSJeff Kirsher  * hw_add_wol_arp - add ARP pattern
3624bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3625bcc9736cSJeff Kirsher  * @ip_addr:	The IPv4 address assigned to the device.
3626bcc9736cSJeff Kirsher  *
3627bcc9736cSJeff Kirsher  * This routine is used to add ARP pattern for waking up the host.
3628bcc9736cSJeff Kirsher  */
3629bcc9736cSJeff Kirsher static void hw_add_wol_arp(struct ksz_hw *hw, const u8 *ip_addr)
3630bcc9736cSJeff Kirsher {
3631bcc9736cSJeff Kirsher 	static const u8 mask[6] = { 0x3F, 0xF0, 0x3F, 0x00, 0xC0, 0x03 };
3632bcc9736cSJeff Kirsher 	u8 pattern[42] = {
3633bcc9736cSJeff Kirsher 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3634bcc9736cSJeff Kirsher 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3635bcc9736cSJeff Kirsher 		0x08, 0x06,
3636bcc9736cSJeff Kirsher 		0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01,
3637bcc9736cSJeff Kirsher 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3638bcc9736cSJeff Kirsher 		0x00, 0x00, 0x00, 0x00,
3639bcc9736cSJeff Kirsher 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3640bcc9736cSJeff Kirsher 		0x00, 0x00, 0x00, 0x00 };
3641bcc9736cSJeff Kirsher 
3642bcc9736cSJeff Kirsher 	memcpy(&pattern[38], ip_addr, 4);
3643bcc9736cSJeff Kirsher 	hw_set_wol_frame(hw, 3, 6, mask, 42, pattern);
3644bcc9736cSJeff Kirsher }
3645bcc9736cSJeff Kirsher 
3646bcc9736cSJeff Kirsher /**
3647bcc9736cSJeff Kirsher  * hw_add_wol_bcast - add broadcast pattern
3648bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3649bcc9736cSJeff Kirsher  *
3650bcc9736cSJeff Kirsher  * This routine is used to add broadcast pattern for waking up the host.
3651bcc9736cSJeff Kirsher  */
3652bcc9736cSJeff Kirsher static void hw_add_wol_bcast(struct ksz_hw *hw)
3653bcc9736cSJeff Kirsher {
3654bcc9736cSJeff Kirsher 	static const u8 mask[] = { 0x3F };
3655bcc9736cSJeff Kirsher 	static const u8 pattern[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3656bcc9736cSJeff Kirsher 
3657bcc9736cSJeff Kirsher 	hw_set_wol_frame(hw, 2, 1, mask, MAC_ADDR_LEN, pattern);
3658bcc9736cSJeff Kirsher }
3659bcc9736cSJeff Kirsher 
3660bcc9736cSJeff Kirsher /**
3661bcc9736cSJeff Kirsher  * hw_add_wol_mcast - add multicast pattern
3662bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3663bcc9736cSJeff Kirsher  *
3664bcc9736cSJeff Kirsher  * This routine is used to add multicast pattern for waking up the host.
3665bcc9736cSJeff Kirsher  *
3666bcc9736cSJeff Kirsher  * It is assumed the multicast packet is the ICMPv6 neighbor solicitation used
3667bcc9736cSJeff Kirsher  * by IPv6 ping command.  Note that multicast packets are filtred through the
3668bcc9736cSJeff Kirsher  * multicast hash table, so not all multicast packets can wake up the host.
3669bcc9736cSJeff Kirsher  */
3670bcc9736cSJeff Kirsher static void hw_add_wol_mcast(struct ksz_hw *hw)
3671bcc9736cSJeff Kirsher {
3672bcc9736cSJeff Kirsher 	static const u8 mask[] = { 0x3F };
3673bcc9736cSJeff Kirsher 	u8 pattern[] = { 0x33, 0x33, 0xFF, 0x00, 0x00, 0x00 };
3674bcc9736cSJeff Kirsher 
3675bcc9736cSJeff Kirsher 	memcpy(&pattern[3], &hw->override_addr[3], 3);
3676bcc9736cSJeff Kirsher 	hw_set_wol_frame(hw, 1, 1, mask, 6, pattern);
3677bcc9736cSJeff Kirsher }
3678bcc9736cSJeff Kirsher 
3679bcc9736cSJeff Kirsher /**
3680bcc9736cSJeff Kirsher  * hw_add_wol_ucast - add unicast pattern
3681bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3682bcc9736cSJeff Kirsher  *
3683bcc9736cSJeff Kirsher  * This routine is used to add unicast pattern to wakeup the host.
3684bcc9736cSJeff Kirsher  *
3685bcc9736cSJeff Kirsher  * It is assumed the unicast packet is directed to the device, as the hardware
3686bcc9736cSJeff Kirsher  * can only receive them in normal case.
3687bcc9736cSJeff Kirsher  */
3688bcc9736cSJeff Kirsher static void hw_add_wol_ucast(struct ksz_hw *hw)
3689bcc9736cSJeff Kirsher {
3690bcc9736cSJeff Kirsher 	static const u8 mask[] = { 0x3F };
3691bcc9736cSJeff Kirsher 
3692bcc9736cSJeff Kirsher 	hw_set_wol_frame(hw, 0, 1, mask, MAC_ADDR_LEN, hw->override_addr);
3693bcc9736cSJeff Kirsher }
3694bcc9736cSJeff Kirsher 
3695bcc9736cSJeff Kirsher /**
3696bcc9736cSJeff Kirsher  * hw_enable_wol - enable Wake-on-LAN
3697bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3698bcc9736cSJeff Kirsher  * @wol_enable:	The Wake-on-LAN settings.
3699bcc9736cSJeff Kirsher  * @net_addr:	The IPv4 address assigned to the device.
3700bcc9736cSJeff Kirsher  *
3701bcc9736cSJeff Kirsher  * This routine is used to enable Wake-on-LAN depending on driver settings.
3702bcc9736cSJeff Kirsher  */
3703bcc9736cSJeff Kirsher static void hw_enable_wol(struct ksz_hw *hw, u32 wol_enable, const u8 *net_addr)
3704bcc9736cSJeff Kirsher {
3705bcc9736cSJeff Kirsher 	hw_cfg_wol(hw, KS8841_WOL_MAGIC_ENABLE, (wol_enable & WAKE_MAGIC));
3706bcc9736cSJeff Kirsher 	hw_cfg_wol(hw, KS8841_WOL_FRAME0_ENABLE, (wol_enable & WAKE_UCAST));
3707bcc9736cSJeff Kirsher 	hw_add_wol_ucast(hw);
3708bcc9736cSJeff Kirsher 	hw_cfg_wol(hw, KS8841_WOL_FRAME1_ENABLE, (wol_enable & WAKE_MCAST));
3709bcc9736cSJeff Kirsher 	hw_add_wol_mcast(hw);
3710bcc9736cSJeff Kirsher 	hw_cfg_wol(hw, KS8841_WOL_FRAME2_ENABLE, (wol_enable & WAKE_BCAST));
3711bcc9736cSJeff Kirsher 	hw_cfg_wol(hw, KS8841_WOL_FRAME3_ENABLE, (wol_enable & WAKE_ARP));
3712bcc9736cSJeff Kirsher 	hw_add_wol_arp(hw, net_addr);
3713bcc9736cSJeff Kirsher }
3714bcc9736cSJeff Kirsher 
3715bcc9736cSJeff Kirsher /**
3716bcc9736cSJeff Kirsher  * hw_init - check driver is correct for the hardware
3717bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3718bcc9736cSJeff Kirsher  *
3719bcc9736cSJeff Kirsher  * This function checks the hardware is correct for this driver and sets the
3720bcc9736cSJeff Kirsher  * hardware up for proper initialization.
3721bcc9736cSJeff Kirsher  *
3722bcc9736cSJeff Kirsher  * Return number of ports or 0 if not right.
3723bcc9736cSJeff Kirsher  */
3724bcc9736cSJeff Kirsher static int hw_init(struct ksz_hw *hw)
3725bcc9736cSJeff Kirsher {
3726bcc9736cSJeff Kirsher 	int rc = 0;
3727bcc9736cSJeff Kirsher 	u16 data;
3728bcc9736cSJeff Kirsher 	u16 revision;
3729bcc9736cSJeff Kirsher 
3730bcc9736cSJeff Kirsher 	/* Set bus speed to 125MHz. */
3731bcc9736cSJeff Kirsher 	writew(BUS_SPEED_125_MHZ, hw->io + KS884X_BUS_CTRL_OFFSET);
3732bcc9736cSJeff Kirsher 
3733bcc9736cSJeff Kirsher 	/* Check KSZ884x chip ID. */
3734bcc9736cSJeff Kirsher 	data = readw(hw->io + KS884X_CHIP_ID_OFFSET);
3735bcc9736cSJeff Kirsher 
3736bcc9736cSJeff Kirsher 	revision = (data & KS884X_REVISION_MASK) >> KS884X_REVISION_SHIFT;
3737bcc9736cSJeff Kirsher 	data &= KS884X_CHIP_ID_MASK_41;
3738bcc9736cSJeff Kirsher 	if (REG_CHIP_ID_41 == data)
3739bcc9736cSJeff Kirsher 		rc = 1;
3740bcc9736cSJeff Kirsher 	else if (REG_CHIP_ID_42 == data)
3741bcc9736cSJeff Kirsher 		rc = 2;
3742bcc9736cSJeff Kirsher 	else
3743bcc9736cSJeff Kirsher 		return 0;
3744bcc9736cSJeff Kirsher 
3745bcc9736cSJeff Kirsher 	/* Setup hardware features or bug workarounds. */
3746bcc9736cSJeff Kirsher 	if (revision <= 1) {
3747bcc9736cSJeff Kirsher 		hw->features |= SMALL_PACKET_TX_BUG;
3748bcc9736cSJeff Kirsher 		if (1 == rc)
3749bcc9736cSJeff Kirsher 			hw->features |= HALF_DUPLEX_SIGNAL_BUG;
3750bcc9736cSJeff Kirsher 	}
3751bcc9736cSJeff Kirsher 	return rc;
3752bcc9736cSJeff Kirsher }
3753bcc9736cSJeff Kirsher 
3754bcc9736cSJeff Kirsher /**
3755bcc9736cSJeff Kirsher  * hw_reset - reset the hardware
3756bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3757bcc9736cSJeff Kirsher  *
3758bcc9736cSJeff Kirsher  * This routine resets the hardware.
3759bcc9736cSJeff Kirsher  */
3760bcc9736cSJeff Kirsher static void hw_reset(struct ksz_hw *hw)
3761bcc9736cSJeff Kirsher {
3762bcc9736cSJeff Kirsher 	writew(GLOBAL_SOFTWARE_RESET, hw->io + KS884X_GLOBAL_CTRL_OFFSET);
3763bcc9736cSJeff Kirsher 
3764bcc9736cSJeff Kirsher 	/* Wait for device to reset. */
3765bcc9736cSJeff Kirsher 	mdelay(10);
3766bcc9736cSJeff Kirsher 
3767bcc9736cSJeff Kirsher 	/* Write 0 to clear device reset. */
3768bcc9736cSJeff Kirsher 	writew(0, hw->io + KS884X_GLOBAL_CTRL_OFFSET);
3769bcc9736cSJeff Kirsher }
3770bcc9736cSJeff Kirsher 
3771bcc9736cSJeff Kirsher /**
3772bcc9736cSJeff Kirsher  * hw_setup - setup the hardware
3773bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3774bcc9736cSJeff Kirsher  *
3775bcc9736cSJeff Kirsher  * This routine setup the hardware for proper operation.
3776bcc9736cSJeff Kirsher  */
3777bcc9736cSJeff Kirsher static void hw_setup(struct ksz_hw *hw)
3778bcc9736cSJeff Kirsher {
3779bcc9736cSJeff Kirsher #if SET_DEFAULT_LED
3780bcc9736cSJeff Kirsher 	u16 data;
3781bcc9736cSJeff Kirsher 
3782bcc9736cSJeff Kirsher 	/* Change default LED mode. */
3783bcc9736cSJeff Kirsher 	data = readw(hw->io + KS8842_SWITCH_CTRL_5_OFFSET);
3784bcc9736cSJeff Kirsher 	data &= ~LED_MODE;
3785bcc9736cSJeff Kirsher 	data |= SET_DEFAULT_LED;
3786bcc9736cSJeff Kirsher 	writew(data, hw->io + KS8842_SWITCH_CTRL_5_OFFSET);
3787bcc9736cSJeff Kirsher #endif
3788bcc9736cSJeff Kirsher 
3789bcc9736cSJeff Kirsher 	/* Setup transmit control. */
3790bcc9736cSJeff Kirsher 	hw->tx_cfg = (DMA_TX_PAD_ENABLE | DMA_TX_CRC_ENABLE |
3791bcc9736cSJeff Kirsher 		(DMA_BURST_DEFAULT << DMA_BURST_SHIFT) | DMA_TX_ENABLE);
3792bcc9736cSJeff Kirsher 
3793bcc9736cSJeff Kirsher 	/* Setup receive control. */
3794bcc9736cSJeff Kirsher 	hw->rx_cfg = (DMA_RX_BROADCAST | DMA_RX_UNICAST |
3795bcc9736cSJeff Kirsher 		(DMA_BURST_DEFAULT << DMA_BURST_SHIFT) | DMA_RX_ENABLE);
3796bcc9736cSJeff Kirsher 	hw->rx_cfg |= KS884X_DMA_RX_MULTICAST;
3797bcc9736cSJeff Kirsher 
3798bcc9736cSJeff Kirsher 	/* Hardware cannot handle UDP packet in IP fragments. */
3799bcc9736cSJeff Kirsher 	hw->rx_cfg |= (DMA_RX_CSUM_TCP | DMA_RX_CSUM_IP);
3800bcc9736cSJeff Kirsher 
3801bcc9736cSJeff Kirsher 	if (hw->all_multi)
3802bcc9736cSJeff Kirsher 		hw->rx_cfg |= DMA_RX_ALL_MULTICAST;
3803bcc9736cSJeff Kirsher 	if (hw->promiscuous)
3804bcc9736cSJeff Kirsher 		hw->rx_cfg |= DMA_RX_PROMISCUOUS;
3805bcc9736cSJeff Kirsher }
3806bcc9736cSJeff Kirsher 
3807bcc9736cSJeff Kirsher /**
3808bcc9736cSJeff Kirsher  * hw_setup_intr - setup interrupt mask
3809bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3810bcc9736cSJeff Kirsher  *
3811bcc9736cSJeff Kirsher  * This routine setup the interrupt mask for proper operation.
3812bcc9736cSJeff Kirsher  */
3813bcc9736cSJeff Kirsher static void hw_setup_intr(struct ksz_hw *hw)
3814bcc9736cSJeff Kirsher {
3815bcc9736cSJeff Kirsher 	hw->intr_mask = KS884X_INT_MASK | KS884X_INT_RX_OVERRUN;
3816bcc9736cSJeff Kirsher }
3817bcc9736cSJeff Kirsher 
3818bcc9736cSJeff Kirsher static void ksz_check_desc_num(struct ksz_desc_info *info)
3819bcc9736cSJeff Kirsher {
3820bcc9736cSJeff Kirsher #define MIN_DESC_SHIFT  2
3821bcc9736cSJeff Kirsher 
3822bcc9736cSJeff Kirsher 	int alloc = info->alloc;
3823bcc9736cSJeff Kirsher 	int shift;
3824bcc9736cSJeff Kirsher 
3825bcc9736cSJeff Kirsher 	shift = 0;
3826bcc9736cSJeff Kirsher 	while (!(alloc & 1)) {
3827bcc9736cSJeff Kirsher 		shift++;
3828bcc9736cSJeff Kirsher 		alloc >>= 1;
3829bcc9736cSJeff Kirsher 	}
3830bcc9736cSJeff Kirsher 	if (alloc != 1 || shift < MIN_DESC_SHIFT) {
3831bcc9736cSJeff Kirsher 		pr_alert("Hardware descriptor numbers not right!\n");
3832bcc9736cSJeff Kirsher 		while (alloc) {
3833bcc9736cSJeff Kirsher 			shift++;
3834bcc9736cSJeff Kirsher 			alloc >>= 1;
3835bcc9736cSJeff Kirsher 		}
3836bcc9736cSJeff Kirsher 		if (shift < MIN_DESC_SHIFT)
3837bcc9736cSJeff Kirsher 			shift = MIN_DESC_SHIFT;
3838bcc9736cSJeff Kirsher 		alloc = 1 << shift;
3839bcc9736cSJeff Kirsher 		info->alloc = alloc;
3840bcc9736cSJeff Kirsher 	}
3841bcc9736cSJeff Kirsher 	info->mask = info->alloc - 1;
3842bcc9736cSJeff Kirsher }
3843bcc9736cSJeff Kirsher 
3844bcc9736cSJeff Kirsher static void hw_init_desc(struct ksz_desc_info *desc_info, int transmit)
3845bcc9736cSJeff Kirsher {
3846bcc9736cSJeff Kirsher 	int i;
3847bcc9736cSJeff Kirsher 	u32 phys = desc_info->ring_phys;
3848bcc9736cSJeff Kirsher 	struct ksz_hw_desc *desc = desc_info->ring_virt;
3849bcc9736cSJeff Kirsher 	struct ksz_desc *cur = desc_info->ring;
3850bcc9736cSJeff Kirsher 	struct ksz_desc *previous = NULL;
3851bcc9736cSJeff Kirsher 
3852bcc9736cSJeff Kirsher 	for (i = 0; i < desc_info->alloc; i++) {
3853bcc9736cSJeff Kirsher 		cur->phw = desc++;
3854bcc9736cSJeff Kirsher 		phys += desc_info->size;
3855bcc9736cSJeff Kirsher 		previous = cur++;
3856bcc9736cSJeff Kirsher 		previous->phw->next = cpu_to_le32(phys);
3857bcc9736cSJeff Kirsher 	}
3858bcc9736cSJeff Kirsher 	previous->phw->next = cpu_to_le32(desc_info->ring_phys);
3859bcc9736cSJeff Kirsher 	previous->sw.buf.rx.end_of_ring = 1;
3860bcc9736cSJeff Kirsher 	previous->phw->buf.data = cpu_to_le32(previous->sw.buf.data);
3861bcc9736cSJeff Kirsher 
3862bcc9736cSJeff Kirsher 	desc_info->avail = desc_info->alloc;
3863bcc9736cSJeff Kirsher 	desc_info->last = desc_info->next = 0;
3864bcc9736cSJeff Kirsher 
3865bcc9736cSJeff Kirsher 	desc_info->cur = desc_info->ring;
3866bcc9736cSJeff Kirsher }
3867bcc9736cSJeff Kirsher 
3868bcc9736cSJeff Kirsher /**
3869bcc9736cSJeff Kirsher  * hw_set_desc_base - set descriptor base addresses
3870bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3871bcc9736cSJeff Kirsher  * @tx_addr:	The transmit descriptor base.
3872bcc9736cSJeff Kirsher  * @rx_addr:	The receive descriptor base.
3873bcc9736cSJeff Kirsher  *
3874bcc9736cSJeff Kirsher  * This routine programs the descriptor base addresses after reset.
3875bcc9736cSJeff Kirsher  */
3876bcc9736cSJeff Kirsher static void hw_set_desc_base(struct ksz_hw *hw, u32 tx_addr, u32 rx_addr)
3877bcc9736cSJeff Kirsher {
3878bcc9736cSJeff Kirsher 	/* Set base address of Tx/Rx descriptors. */
3879bcc9736cSJeff Kirsher 	writel(tx_addr, hw->io + KS_DMA_TX_ADDR);
3880bcc9736cSJeff Kirsher 	writel(rx_addr, hw->io + KS_DMA_RX_ADDR);
3881bcc9736cSJeff Kirsher }
3882bcc9736cSJeff Kirsher 
3883bcc9736cSJeff Kirsher static void hw_reset_pkts(struct ksz_desc_info *info)
3884bcc9736cSJeff Kirsher {
3885bcc9736cSJeff Kirsher 	info->cur = info->ring;
3886bcc9736cSJeff Kirsher 	info->avail = info->alloc;
3887bcc9736cSJeff Kirsher 	info->last = info->next = 0;
3888bcc9736cSJeff Kirsher }
3889bcc9736cSJeff Kirsher 
3890bcc9736cSJeff Kirsher static inline void hw_resume_rx(struct ksz_hw *hw)
3891bcc9736cSJeff Kirsher {
3892bcc9736cSJeff Kirsher 	writel(DMA_START, hw->io + KS_DMA_RX_START);
3893bcc9736cSJeff Kirsher }
3894bcc9736cSJeff Kirsher 
3895bcc9736cSJeff Kirsher /**
3896bcc9736cSJeff Kirsher  * hw_start_rx - start receiving
3897bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3898bcc9736cSJeff Kirsher  *
3899bcc9736cSJeff Kirsher  * This routine starts the receive function of the hardware.
3900bcc9736cSJeff Kirsher  */
3901bcc9736cSJeff Kirsher static void hw_start_rx(struct ksz_hw *hw)
3902bcc9736cSJeff Kirsher {
3903bcc9736cSJeff Kirsher 	writel(hw->rx_cfg, hw->io + KS_DMA_RX_CTRL);
3904bcc9736cSJeff Kirsher 
3905bcc9736cSJeff Kirsher 	/* Notify when the receive stops. */
3906bcc9736cSJeff Kirsher 	hw->intr_mask |= KS884X_INT_RX_STOPPED;
3907bcc9736cSJeff Kirsher 
3908bcc9736cSJeff Kirsher 	writel(DMA_START, hw->io + KS_DMA_RX_START);
3909bcc9736cSJeff Kirsher 	hw_ack_intr(hw, KS884X_INT_RX_STOPPED);
3910bcc9736cSJeff Kirsher 	hw->rx_stop++;
3911bcc9736cSJeff Kirsher 
3912bcc9736cSJeff Kirsher 	/* Variable overflows. */
3913bcc9736cSJeff Kirsher 	if (0 == hw->rx_stop)
3914bcc9736cSJeff Kirsher 		hw->rx_stop = 2;
3915bcc9736cSJeff Kirsher }
3916bcc9736cSJeff Kirsher 
3917bcc9736cSJeff Kirsher /*
3918bcc9736cSJeff Kirsher  * hw_stop_rx - stop receiving
3919bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3920bcc9736cSJeff Kirsher  *
3921bcc9736cSJeff Kirsher  * This routine stops the receive function of the hardware.
3922bcc9736cSJeff Kirsher  */
3923bcc9736cSJeff Kirsher static void hw_stop_rx(struct ksz_hw *hw)
3924bcc9736cSJeff Kirsher {
3925bcc9736cSJeff Kirsher 	hw->rx_stop = 0;
3926bcc9736cSJeff Kirsher 	hw_turn_off_intr(hw, KS884X_INT_RX_STOPPED);
3927bcc9736cSJeff Kirsher 	writel((hw->rx_cfg & ~DMA_RX_ENABLE), hw->io + KS_DMA_RX_CTRL);
3928bcc9736cSJeff Kirsher }
3929bcc9736cSJeff Kirsher 
3930bcc9736cSJeff Kirsher /**
3931bcc9736cSJeff Kirsher  * hw_start_tx - start transmitting
3932bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3933bcc9736cSJeff Kirsher  *
3934bcc9736cSJeff Kirsher  * This routine starts the transmit function of the hardware.
3935bcc9736cSJeff Kirsher  */
3936bcc9736cSJeff Kirsher static void hw_start_tx(struct ksz_hw *hw)
3937bcc9736cSJeff Kirsher {
3938bcc9736cSJeff Kirsher 	writel(hw->tx_cfg, hw->io + KS_DMA_TX_CTRL);
3939bcc9736cSJeff Kirsher }
3940bcc9736cSJeff Kirsher 
3941bcc9736cSJeff Kirsher /**
3942bcc9736cSJeff Kirsher  * hw_stop_tx - stop transmitting
3943bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3944bcc9736cSJeff Kirsher  *
3945bcc9736cSJeff Kirsher  * This routine stops the transmit function of the hardware.
3946bcc9736cSJeff Kirsher  */
3947bcc9736cSJeff Kirsher static void hw_stop_tx(struct ksz_hw *hw)
3948bcc9736cSJeff Kirsher {
3949bcc9736cSJeff Kirsher 	writel((hw->tx_cfg & ~DMA_TX_ENABLE), hw->io + KS_DMA_TX_CTRL);
3950bcc9736cSJeff Kirsher }
3951bcc9736cSJeff Kirsher 
3952bcc9736cSJeff Kirsher /**
3953bcc9736cSJeff Kirsher  * hw_disable - disable hardware
3954bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3955bcc9736cSJeff Kirsher  *
3956bcc9736cSJeff Kirsher  * This routine disables the hardware.
3957bcc9736cSJeff Kirsher  */
3958bcc9736cSJeff Kirsher static void hw_disable(struct ksz_hw *hw)
3959bcc9736cSJeff Kirsher {
3960bcc9736cSJeff Kirsher 	hw_stop_rx(hw);
3961bcc9736cSJeff Kirsher 	hw_stop_tx(hw);
3962bcc9736cSJeff Kirsher 	hw->enabled = 0;
3963bcc9736cSJeff Kirsher }
3964bcc9736cSJeff Kirsher 
3965bcc9736cSJeff Kirsher /**
3966bcc9736cSJeff Kirsher  * hw_enable - enable hardware
3967bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3968bcc9736cSJeff Kirsher  *
3969bcc9736cSJeff Kirsher  * This routine enables the hardware.
3970bcc9736cSJeff Kirsher  */
3971bcc9736cSJeff Kirsher static void hw_enable(struct ksz_hw *hw)
3972bcc9736cSJeff Kirsher {
3973bcc9736cSJeff Kirsher 	hw_start_tx(hw);
3974bcc9736cSJeff Kirsher 	hw_start_rx(hw);
3975bcc9736cSJeff Kirsher 	hw->enabled = 1;
3976bcc9736cSJeff Kirsher }
3977bcc9736cSJeff Kirsher 
3978bcc9736cSJeff Kirsher /**
3979bcc9736cSJeff Kirsher  * hw_alloc_pkt - allocate enough descriptors for transmission
3980bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
3981bcc9736cSJeff Kirsher  * @length:	The length of the packet.
3982bcc9736cSJeff Kirsher  * @physical:	Number of descriptors required.
3983bcc9736cSJeff Kirsher  *
3984bcc9736cSJeff Kirsher  * This function allocates descriptors for transmission.
3985bcc9736cSJeff Kirsher  *
3986bcc9736cSJeff Kirsher  * Return 0 if not successful; 1 for buffer copy; or number of descriptors.
3987bcc9736cSJeff Kirsher  */
3988bcc9736cSJeff Kirsher static int hw_alloc_pkt(struct ksz_hw *hw, int length, int physical)
3989bcc9736cSJeff Kirsher {
3990bcc9736cSJeff Kirsher 	/* Always leave one descriptor free. */
3991bcc9736cSJeff Kirsher 	if (hw->tx_desc_info.avail <= 1)
3992bcc9736cSJeff Kirsher 		return 0;
3993bcc9736cSJeff Kirsher 
3994bcc9736cSJeff Kirsher 	/* Allocate a descriptor for transmission and mark it current. */
3995bcc9736cSJeff Kirsher 	get_tx_pkt(&hw->tx_desc_info, &hw->tx_desc_info.cur);
3996bcc9736cSJeff Kirsher 	hw->tx_desc_info.cur->sw.buf.tx.first_seg = 1;
3997bcc9736cSJeff Kirsher 
3998bcc9736cSJeff Kirsher 	/* Keep track of number of transmit descriptors used so far. */
3999bcc9736cSJeff Kirsher 	++hw->tx_int_cnt;
4000bcc9736cSJeff Kirsher 	hw->tx_size += length;
4001bcc9736cSJeff Kirsher 
4002bcc9736cSJeff Kirsher 	/* Cannot hold on too much data. */
4003bcc9736cSJeff Kirsher 	if (hw->tx_size >= MAX_TX_HELD_SIZE)
4004bcc9736cSJeff Kirsher 		hw->tx_int_cnt = hw->tx_int_mask + 1;
4005bcc9736cSJeff Kirsher 
4006bcc9736cSJeff Kirsher 	if (physical > hw->tx_desc_info.avail)
4007bcc9736cSJeff Kirsher 		return 1;
4008bcc9736cSJeff Kirsher 
4009bcc9736cSJeff Kirsher 	return hw->tx_desc_info.avail;
4010bcc9736cSJeff Kirsher }
4011bcc9736cSJeff Kirsher 
4012bcc9736cSJeff Kirsher /**
4013bcc9736cSJeff Kirsher  * hw_send_pkt - mark packet for transmission
4014bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
4015bcc9736cSJeff Kirsher  *
4016bcc9736cSJeff Kirsher  * This routine marks the packet for transmission in PCI version.
4017bcc9736cSJeff Kirsher  */
4018bcc9736cSJeff Kirsher static void hw_send_pkt(struct ksz_hw *hw)
4019bcc9736cSJeff Kirsher {
4020bcc9736cSJeff Kirsher 	struct ksz_desc *cur = hw->tx_desc_info.cur;
4021bcc9736cSJeff Kirsher 
4022bcc9736cSJeff Kirsher 	cur->sw.buf.tx.last_seg = 1;
4023bcc9736cSJeff Kirsher 
4024bcc9736cSJeff Kirsher 	/* Interrupt only after specified number of descriptors used. */
4025bcc9736cSJeff Kirsher 	if (hw->tx_int_cnt > hw->tx_int_mask) {
4026bcc9736cSJeff Kirsher 		cur->sw.buf.tx.intr = 1;
4027bcc9736cSJeff Kirsher 		hw->tx_int_cnt = 0;
4028bcc9736cSJeff Kirsher 		hw->tx_size = 0;
4029bcc9736cSJeff Kirsher 	}
4030bcc9736cSJeff Kirsher 
4031bcc9736cSJeff Kirsher 	/* KSZ8842 supports port directed transmission. */
4032bcc9736cSJeff Kirsher 	cur->sw.buf.tx.dest_port = hw->dst_ports;
4033bcc9736cSJeff Kirsher 
4034bcc9736cSJeff Kirsher 	release_desc(cur);
4035bcc9736cSJeff Kirsher 
4036bcc9736cSJeff Kirsher 	writel(0, hw->io + KS_DMA_TX_START);
4037bcc9736cSJeff Kirsher }
4038bcc9736cSJeff Kirsher 
4039bcc9736cSJeff Kirsher static int empty_addr(u8 *addr)
4040bcc9736cSJeff Kirsher {
4041bcc9736cSJeff Kirsher 	u32 *addr1 = (u32 *) addr;
4042bcc9736cSJeff Kirsher 	u16 *addr2 = (u16 *) &addr[4];
4043bcc9736cSJeff Kirsher 
4044bcc9736cSJeff Kirsher 	return 0 == *addr1 && 0 == *addr2;
4045bcc9736cSJeff Kirsher }
4046bcc9736cSJeff Kirsher 
4047bcc9736cSJeff Kirsher /**
4048bcc9736cSJeff Kirsher  * hw_set_addr - set MAC address
4049bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
4050bcc9736cSJeff Kirsher  *
4051bcc9736cSJeff Kirsher  * This routine programs the MAC address of the hardware when the address is
4052bcc9736cSJeff Kirsher  * overrided.
4053bcc9736cSJeff Kirsher  */
4054bcc9736cSJeff Kirsher static void hw_set_addr(struct ksz_hw *hw)
4055bcc9736cSJeff Kirsher {
4056bcc9736cSJeff Kirsher 	int i;
4057bcc9736cSJeff Kirsher 
4058bcc9736cSJeff Kirsher 	for (i = 0; i < MAC_ADDR_LEN; i++)
4059bcc9736cSJeff Kirsher 		writeb(hw->override_addr[MAC_ADDR_ORDER(i)],
4060bcc9736cSJeff Kirsher 			hw->io + KS884X_ADDR_0_OFFSET + i);
4061bcc9736cSJeff Kirsher 
4062bcc9736cSJeff Kirsher 	sw_set_addr(hw, hw->override_addr);
4063bcc9736cSJeff Kirsher }
4064bcc9736cSJeff Kirsher 
4065bcc9736cSJeff Kirsher /**
4066bcc9736cSJeff Kirsher  * hw_read_addr - read MAC address
4067bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
4068bcc9736cSJeff Kirsher  *
4069bcc9736cSJeff Kirsher  * This routine retrieves the MAC address of the hardware.
4070bcc9736cSJeff Kirsher  */
4071bcc9736cSJeff Kirsher static void hw_read_addr(struct ksz_hw *hw)
4072bcc9736cSJeff Kirsher {
4073bcc9736cSJeff Kirsher 	int i;
4074bcc9736cSJeff Kirsher 
4075bcc9736cSJeff Kirsher 	for (i = 0; i < MAC_ADDR_LEN; i++)
4076bcc9736cSJeff Kirsher 		hw->perm_addr[MAC_ADDR_ORDER(i)] = readb(hw->io +
4077bcc9736cSJeff Kirsher 			KS884X_ADDR_0_OFFSET + i);
4078bcc9736cSJeff Kirsher 
4079bcc9736cSJeff Kirsher 	if (!hw->mac_override) {
4080bcc9736cSJeff Kirsher 		memcpy(hw->override_addr, hw->perm_addr, MAC_ADDR_LEN);
4081bcc9736cSJeff Kirsher 		if (empty_addr(hw->override_addr)) {
4082bcc9736cSJeff Kirsher 			memcpy(hw->perm_addr, DEFAULT_MAC_ADDRESS,
4083bcc9736cSJeff Kirsher 				MAC_ADDR_LEN);
4084bcc9736cSJeff Kirsher 			memcpy(hw->override_addr, DEFAULT_MAC_ADDRESS,
4085bcc9736cSJeff Kirsher 				MAC_ADDR_LEN);
4086bcc9736cSJeff Kirsher 			hw->override_addr[5] += hw->id;
4087bcc9736cSJeff Kirsher 			hw_set_addr(hw);
4088bcc9736cSJeff Kirsher 		}
4089bcc9736cSJeff Kirsher 	}
4090bcc9736cSJeff Kirsher }
4091bcc9736cSJeff Kirsher 
4092bcc9736cSJeff Kirsher static void hw_ena_add_addr(struct ksz_hw *hw, int index, u8 *mac_addr)
4093bcc9736cSJeff Kirsher {
4094bcc9736cSJeff Kirsher 	int i;
4095bcc9736cSJeff Kirsher 	u32 mac_addr_lo;
4096bcc9736cSJeff Kirsher 	u32 mac_addr_hi;
4097bcc9736cSJeff Kirsher 
4098bcc9736cSJeff Kirsher 	mac_addr_hi = 0;
4099bcc9736cSJeff Kirsher 	for (i = 0; i < 2; i++) {
4100bcc9736cSJeff Kirsher 		mac_addr_hi <<= 8;
4101bcc9736cSJeff Kirsher 		mac_addr_hi |= mac_addr[i];
4102bcc9736cSJeff Kirsher 	}
4103bcc9736cSJeff Kirsher 	mac_addr_hi |= ADD_ADDR_ENABLE;
4104bcc9736cSJeff Kirsher 	mac_addr_lo = 0;
4105bcc9736cSJeff Kirsher 	for (i = 2; i < 6; i++) {
4106bcc9736cSJeff Kirsher 		mac_addr_lo <<= 8;
4107bcc9736cSJeff Kirsher 		mac_addr_lo |= mac_addr[i];
4108bcc9736cSJeff Kirsher 	}
4109bcc9736cSJeff Kirsher 	index *= ADD_ADDR_INCR;
4110bcc9736cSJeff Kirsher 
4111bcc9736cSJeff Kirsher 	writel(mac_addr_lo, hw->io + index + KS_ADD_ADDR_0_LO);
4112bcc9736cSJeff Kirsher 	writel(mac_addr_hi, hw->io + index + KS_ADD_ADDR_0_HI);
4113bcc9736cSJeff Kirsher }
4114bcc9736cSJeff Kirsher 
4115bcc9736cSJeff Kirsher static void hw_set_add_addr(struct ksz_hw *hw)
4116bcc9736cSJeff Kirsher {
4117bcc9736cSJeff Kirsher 	int i;
4118bcc9736cSJeff Kirsher 
4119bcc9736cSJeff Kirsher 	for (i = 0; i < ADDITIONAL_ENTRIES; i++) {
4120bcc9736cSJeff Kirsher 		if (empty_addr(hw->address[i]))
4121bcc9736cSJeff Kirsher 			writel(0, hw->io + ADD_ADDR_INCR * i +
4122bcc9736cSJeff Kirsher 				KS_ADD_ADDR_0_HI);
4123bcc9736cSJeff Kirsher 		else
4124bcc9736cSJeff Kirsher 			hw_ena_add_addr(hw, i, hw->address[i]);
4125bcc9736cSJeff Kirsher 	}
4126bcc9736cSJeff Kirsher }
4127bcc9736cSJeff Kirsher 
4128bcc9736cSJeff Kirsher static int hw_add_addr(struct ksz_hw *hw, u8 *mac_addr)
4129bcc9736cSJeff Kirsher {
4130bcc9736cSJeff Kirsher 	int i;
4131bcc9736cSJeff Kirsher 	int j = ADDITIONAL_ENTRIES;
4132bcc9736cSJeff Kirsher 
4133bcc9736cSJeff Kirsher 	if (!memcmp(hw->override_addr, mac_addr, MAC_ADDR_LEN))
4134bcc9736cSJeff Kirsher 		return 0;
4135bcc9736cSJeff Kirsher 	for (i = 0; i < hw->addr_list_size; i++) {
4136bcc9736cSJeff Kirsher 		if (!memcmp(hw->address[i], mac_addr, MAC_ADDR_LEN))
4137bcc9736cSJeff Kirsher 			return 0;
4138bcc9736cSJeff Kirsher 		if (ADDITIONAL_ENTRIES == j && empty_addr(hw->address[i]))
4139bcc9736cSJeff Kirsher 			j = i;
4140bcc9736cSJeff Kirsher 	}
4141bcc9736cSJeff Kirsher 	if (j < ADDITIONAL_ENTRIES) {
4142bcc9736cSJeff Kirsher 		memcpy(hw->address[j], mac_addr, MAC_ADDR_LEN);
4143bcc9736cSJeff Kirsher 		hw_ena_add_addr(hw, j, hw->address[j]);
4144bcc9736cSJeff Kirsher 		return 0;
4145bcc9736cSJeff Kirsher 	}
4146bcc9736cSJeff Kirsher 	return -1;
4147bcc9736cSJeff Kirsher }
4148bcc9736cSJeff Kirsher 
4149bcc9736cSJeff Kirsher static int hw_del_addr(struct ksz_hw *hw, u8 *mac_addr)
4150bcc9736cSJeff Kirsher {
4151bcc9736cSJeff Kirsher 	int i;
4152bcc9736cSJeff Kirsher 
4153bcc9736cSJeff Kirsher 	for (i = 0; i < hw->addr_list_size; i++) {
4154bcc9736cSJeff Kirsher 		if (!memcmp(hw->address[i], mac_addr, MAC_ADDR_LEN)) {
4155bcc9736cSJeff Kirsher 			memset(hw->address[i], 0, MAC_ADDR_LEN);
4156bcc9736cSJeff Kirsher 			writel(0, hw->io + ADD_ADDR_INCR * i +
4157bcc9736cSJeff Kirsher 				KS_ADD_ADDR_0_HI);
4158bcc9736cSJeff Kirsher 			return 0;
4159bcc9736cSJeff Kirsher 		}
4160bcc9736cSJeff Kirsher 	}
4161bcc9736cSJeff Kirsher 	return -1;
4162bcc9736cSJeff Kirsher }
4163bcc9736cSJeff Kirsher 
4164bcc9736cSJeff Kirsher /**
4165bcc9736cSJeff Kirsher  * hw_clr_multicast - clear multicast addresses
4166bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
4167bcc9736cSJeff Kirsher  *
4168bcc9736cSJeff Kirsher  * This routine removes all multicast addresses set in the hardware.
4169bcc9736cSJeff Kirsher  */
4170bcc9736cSJeff Kirsher static void hw_clr_multicast(struct ksz_hw *hw)
4171bcc9736cSJeff Kirsher {
4172bcc9736cSJeff Kirsher 	int i;
4173bcc9736cSJeff Kirsher 
4174bcc9736cSJeff Kirsher 	for (i = 0; i < HW_MULTICAST_SIZE; i++) {
4175bcc9736cSJeff Kirsher 		hw->multi_bits[i] = 0;
4176bcc9736cSJeff Kirsher 
4177bcc9736cSJeff Kirsher 		writeb(0, hw->io + KS884X_MULTICAST_0_OFFSET + i);
4178bcc9736cSJeff Kirsher 	}
4179bcc9736cSJeff Kirsher }
4180bcc9736cSJeff Kirsher 
4181bcc9736cSJeff Kirsher /**
4182bcc9736cSJeff Kirsher  * hw_set_grp_addr - set multicast addresses
4183bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
4184bcc9736cSJeff Kirsher  *
4185bcc9736cSJeff Kirsher  * This routine programs multicast addresses for the hardware to accept those
4186bcc9736cSJeff Kirsher  * addresses.
4187bcc9736cSJeff Kirsher  */
4188bcc9736cSJeff Kirsher static void hw_set_grp_addr(struct ksz_hw *hw)
4189bcc9736cSJeff Kirsher {
4190bcc9736cSJeff Kirsher 	int i;
4191bcc9736cSJeff Kirsher 	int index;
4192bcc9736cSJeff Kirsher 	int position;
4193bcc9736cSJeff Kirsher 	int value;
4194bcc9736cSJeff Kirsher 
4195bcc9736cSJeff Kirsher 	memset(hw->multi_bits, 0, sizeof(u8) * HW_MULTICAST_SIZE);
4196bcc9736cSJeff Kirsher 
4197bcc9736cSJeff Kirsher 	for (i = 0; i < hw->multi_list_size; i++) {
4198bcc9736cSJeff Kirsher 		position = (ether_crc(6, hw->multi_list[i]) >> 26) & 0x3f;
4199bcc9736cSJeff Kirsher 		index = position >> 3;
4200bcc9736cSJeff Kirsher 		value = 1 << (position & 7);
4201bcc9736cSJeff Kirsher 		hw->multi_bits[index] |= (u8) value;
4202bcc9736cSJeff Kirsher 	}
4203bcc9736cSJeff Kirsher 
4204bcc9736cSJeff Kirsher 	for (i = 0; i < HW_MULTICAST_SIZE; i++)
4205bcc9736cSJeff Kirsher 		writeb(hw->multi_bits[i], hw->io + KS884X_MULTICAST_0_OFFSET +
4206bcc9736cSJeff Kirsher 			i);
4207bcc9736cSJeff Kirsher }
4208bcc9736cSJeff Kirsher 
4209bcc9736cSJeff Kirsher /**
4210bcc9736cSJeff Kirsher  * hw_set_multicast - enable or disable all multicast receiving
4211bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
4212bcc9736cSJeff Kirsher  * @multicast:	To turn on or off the all multicast feature.
4213bcc9736cSJeff Kirsher  *
4214bcc9736cSJeff Kirsher  * This routine enables/disables the hardware to accept all multicast packets.
4215bcc9736cSJeff Kirsher  */
4216bcc9736cSJeff Kirsher static void hw_set_multicast(struct ksz_hw *hw, u8 multicast)
4217bcc9736cSJeff Kirsher {
4218bcc9736cSJeff Kirsher 	/* Stop receiving for reconfiguration. */
4219bcc9736cSJeff Kirsher 	hw_stop_rx(hw);
4220bcc9736cSJeff Kirsher 
4221bcc9736cSJeff Kirsher 	if (multicast)
4222bcc9736cSJeff Kirsher 		hw->rx_cfg |= DMA_RX_ALL_MULTICAST;
4223bcc9736cSJeff Kirsher 	else
4224bcc9736cSJeff Kirsher 		hw->rx_cfg &= ~DMA_RX_ALL_MULTICAST;
4225bcc9736cSJeff Kirsher 
4226bcc9736cSJeff Kirsher 	if (hw->enabled)
4227bcc9736cSJeff Kirsher 		hw_start_rx(hw);
4228bcc9736cSJeff Kirsher }
4229bcc9736cSJeff Kirsher 
4230bcc9736cSJeff Kirsher /**
4231bcc9736cSJeff Kirsher  * hw_set_promiscuous - enable or disable promiscuous receiving
4232bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
4233bcc9736cSJeff Kirsher  * @prom:	To turn on or off the promiscuous feature.
4234bcc9736cSJeff Kirsher  *
4235bcc9736cSJeff Kirsher  * This routine enables/disables the hardware to accept all packets.
4236bcc9736cSJeff Kirsher  */
4237bcc9736cSJeff Kirsher static void hw_set_promiscuous(struct ksz_hw *hw, u8 prom)
4238bcc9736cSJeff Kirsher {
4239bcc9736cSJeff Kirsher 	/* Stop receiving for reconfiguration. */
4240bcc9736cSJeff Kirsher 	hw_stop_rx(hw);
4241bcc9736cSJeff Kirsher 
4242bcc9736cSJeff Kirsher 	if (prom)
4243bcc9736cSJeff Kirsher 		hw->rx_cfg |= DMA_RX_PROMISCUOUS;
4244bcc9736cSJeff Kirsher 	else
4245bcc9736cSJeff Kirsher 		hw->rx_cfg &= ~DMA_RX_PROMISCUOUS;
4246bcc9736cSJeff Kirsher 
4247bcc9736cSJeff Kirsher 	if (hw->enabled)
4248bcc9736cSJeff Kirsher 		hw_start_rx(hw);
4249bcc9736cSJeff Kirsher }
4250bcc9736cSJeff Kirsher 
4251bcc9736cSJeff Kirsher /**
4252bcc9736cSJeff Kirsher  * sw_enable - enable the switch
4253bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
4254bcc9736cSJeff Kirsher  * @enable:	The flag to enable or disable the switch
4255bcc9736cSJeff Kirsher  *
4256bcc9736cSJeff Kirsher  * This routine is used to enable/disable the switch in KSZ8842.
4257bcc9736cSJeff Kirsher  */
4258bcc9736cSJeff Kirsher static void sw_enable(struct ksz_hw *hw, int enable)
4259bcc9736cSJeff Kirsher {
4260bcc9736cSJeff Kirsher 	int port;
4261bcc9736cSJeff Kirsher 
4262bcc9736cSJeff Kirsher 	for (port = 0; port < SWITCH_PORT_NUM; port++) {
4263bcc9736cSJeff Kirsher 		if (hw->dev_count > 1) {
4264bcc9736cSJeff Kirsher 			/* Set port-base vlan membership with host port. */
4265bcc9736cSJeff Kirsher 			sw_cfg_port_base_vlan(hw, port,
4266bcc9736cSJeff Kirsher 				HOST_MASK | (1 << port));
4267bcc9736cSJeff Kirsher 			port_set_stp_state(hw, port, STP_STATE_DISABLED);
4268bcc9736cSJeff Kirsher 		} else {
4269bcc9736cSJeff Kirsher 			sw_cfg_port_base_vlan(hw, port, PORT_MASK);
4270bcc9736cSJeff Kirsher 			port_set_stp_state(hw, port, STP_STATE_FORWARDING);
4271bcc9736cSJeff Kirsher 		}
4272bcc9736cSJeff Kirsher 	}
4273bcc9736cSJeff Kirsher 	if (hw->dev_count > 1)
4274bcc9736cSJeff Kirsher 		port_set_stp_state(hw, SWITCH_PORT_NUM, STP_STATE_SIMPLE);
4275bcc9736cSJeff Kirsher 	else
4276bcc9736cSJeff Kirsher 		port_set_stp_state(hw, SWITCH_PORT_NUM, STP_STATE_FORWARDING);
4277bcc9736cSJeff Kirsher 
4278bcc9736cSJeff Kirsher 	if (enable)
4279bcc9736cSJeff Kirsher 		enable = KS8842_START;
4280bcc9736cSJeff Kirsher 	writew(enable, hw->io + KS884X_CHIP_ID_OFFSET);
4281bcc9736cSJeff Kirsher }
4282bcc9736cSJeff Kirsher 
4283bcc9736cSJeff Kirsher /**
4284bcc9736cSJeff Kirsher  * sw_setup - setup the switch
4285bcc9736cSJeff Kirsher  * @hw: 	The hardware instance.
4286bcc9736cSJeff Kirsher  *
4287bcc9736cSJeff Kirsher  * This routine setup the hardware switch engine for default operation.
4288bcc9736cSJeff Kirsher  */
4289bcc9736cSJeff Kirsher static void sw_setup(struct ksz_hw *hw)
4290bcc9736cSJeff Kirsher {
4291bcc9736cSJeff Kirsher 	int port;
4292bcc9736cSJeff Kirsher 
4293bcc9736cSJeff Kirsher 	sw_set_global_ctrl(hw);
4294bcc9736cSJeff Kirsher 
4295bcc9736cSJeff Kirsher 	/* Enable switch broadcast storm protection at 10% percent rate. */
4296bcc9736cSJeff Kirsher 	sw_init_broad_storm(hw);
4297bcc9736cSJeff Kirsher 	hw_cfg_broad_storm(hw, BROADCAST_STORM_PROTECTION_RATE);
4298bcc9736cSJeff Kirsher 	for (port = 0; port < SWITCH_PORT_NUM; port++)
4299bcc9736cSJeff Kirsher 		sw_ena_broad_storm(hw, port);
4300bcc9736cSJeff Kirsher 
4301bcc9736cSJeff Kirsher 	sw_init_prio(hw);
4302bcc9736cSJeff Kirsher 
4303bcc9736cSJeff Kirsher 	sw_init_mirror(hw);
4304bcc9736cSJeff Kirsher 
4305bcc9736cSJeff Kirsher 	sw_init_prio_rate(hw);
4306bcc9736cSJeff Kirsher 
4307bcc9736cSJeff Kirsher 	sw_init_vlan(hw);
4308bcc9736cSJeff Kirsher 
4309bcc9736cSJeff Kirsher 	if (hw->features & STP_SUPPORT)
4310bcc9736cSJeff Kirsher 		sw_init_stp(hw);
4311bcc9736cSJeff Kirsher 	if (!sw_chk(hw, KS8842_SWITCH_CTRL_1_OFFSET,
4312bcc9736cSJeff Kirsher 			SWITCH_TX_FLOW_CTRL | SWITCH_RX_FLOW_CTRL))
4313bcc9736cSJeff Kirsher 		hw->overrides |= PAUSE_FLOW_CTRL;
4314bcc9736cSJeff Kirsher 	sw_enable(hw, 1);
4315bcc9736cSJeff Kirsher }
4316bcc9736cSJeff Kirsher 
4317bcc9736cSJeff Kirsher /**
4318bcc9736cSJeff Kirsher  * ksz_start_timer - start kernel timer
4319bcc9736cSJeff Kirsher  * @info:	Kernel timer information.
4320bcc9736cSJeff Kirsher  * @time:	The time tick.
4321bcc9736cSJeff Kirsher  *
4322bcc9736cSJeff Kirsher  * This routine starts the kernel timer after the specified time tick.
4323bcc9736cSJeff Kirsher  */
4324bcc9736cSJeff Kirsher static void ksz_start_timer(struct ksz_timer_info *info, int time)
4325bcc9736cSJeff Kirsher {
4326bcc9736cSJeff Kirsher 	info->cnt = 0;
4327bcc9736cSJeff Kirsher 	info->timer.expires = jiffies + time;
4328bcc9736cSJeff Kirsher 	add_timer(&info->timer);
4329bcc9736cSJeff Kirsher 
4330bcc9736cSJeff Kirsher 	/* infinity */
4331bcc9736cSJeff Kirsher 	info->max = -1;
4332bcc9736cSJeff Kirsher }
4333bcc9736cSJeff Kirsher 
4334bcc9736cSJeff Kirsher /**
4335bcc9736cSJeff Kirsher  * ksz_stop_timer - stop kernel timer
4336bcc9736cSJeff Kirsher  * @info:	Kernel timer information.
4337bcc9736cSJeff Kirsher  *
4338bcc9736cSJeff Kirsher  * This routine stops the kernel timer.
4339bcc9736cSJeff Kirsher  */
4340bcc9736cSJeff Kirsher static void ksz_stop_timer(struct ksz_timer_info *info)
4341bcc9736cSJeff Kirsher {
4342bcc9736cSJeff Kirsher 	if (info->max) {
4343bcc9736cSJeff Kirsher 		info->max = 0;
4344bcc9736cSJeff Kirsher 		del_timer_sync(&info->timer);
4345bcc9736cSJeff Kirsher 	}
4346bcc9736cSJeff Kirsher }
4347bcc9736cSJeff Kirsher 
4348bcc9736cSJeff Kirsher static void ksz_init_timer(struct ksz_timer_info *info, int period,
4349bcc9736cSJeff Kirsher 	void (*function)(unsigned long), void *data)
4350bcc9736cSJeff Kirsher {
4351bcc9736cSJeff Kirsher 	info->max = 0;
4352bcc9736cSJeff Kirsher 	info->period = period;
4353bcc9736cSJeff Kirsher 	init_timer(&info->timer);
4354bcc9736cSJeff Kirsher 	info->timer.function = function;
4355bcc9736cSJeff Kirsher 	info->timer.data = (unsigned long) data;
4356bcc9736cSJeff Kirsher }
4357bcc9736cSJeff Kirsher 
4358bcc9736cSJeff Kirsher static void ksz_update_timer(struct ksz_timer_info *info)
4359bcc9736cSJeff Kirsher {
4360bcc9736cSJeff Kirsher 	++info->cnt;
4361bcc9736cSJeff Kirsher 	if (info->max > 0) {
4362bcc9736cSJeff Kirsher 		if (info->cnt < info->max) {
4363bcc9736cSJeff Kirsher 			info->timer.expires = jiffies + info->period;
4364bcc9736cSJeff Kirsher 			add_timer(&info->timer);
4365bcc9736cSJeff Kirsher 		} else
4366bcc9736cSJeff Kirsher 			info->max = 0;
4367bcc9736cSJeff Kirsher 	} else if (info->max < 0) {
4368bcc9736cSJeff Kirsher 		info->timer.expires = jiffies + info->period;
4369bcc9736cSJeff Kirsher 		add_timer(&info->timer);
4370bcc9736cSJeff Kirsher 	}
4371bcc9736cSJeff Kirsher }
4372bcc9736cSJeff Kirsher 
4373bcc9736cSJeff Kirsher /**
4374bcc9736cSJeff Kirsher  * ksz_alloc_soft_desc - allocate software descriptors
4375bcc9736cSJeff Kirsher  * @desc_info:	Descriptor information structure.
4376bcc9736cSJeff Kirsher  * @transmit:	Indication that descriptors are for transmit.
4377bcc9736cSJeff Kirsher  *
4378bcc9736cSJeff Kirsher  * This local function allocates software descriptors for manipulation in
4379bcc9736cSJeff Kirsher  * memory.
4380bcc9736cSJeff Kirsher  *
4381bcc9736cSJeff Kirsher  * Return 0 if successful.
4382bcc9736cSJeff Kirsher  */
4383bcc9736cSJeff Kirsher static int ksz_alloc_soft_desc(struct ksz_desc_info *desc_info, int transmit)
4384bcc9736cSJeff Kirsher {
4385bcc9736cSJeff Kirsher 	desc_info->ring = kmalloc(sizeof(struct ksz_desc) * desc_info->alloc,
4386bcc9736cSJeff Kirsher 		GFP_KERNEL);
4387bcc9736cSJeff Kirsher 	if (!desc_info->ring)
4388bcc9736cSJeff Kirsher 		return 1;
4389bcc9736cSJeff Kirsher 	memset((void *) desc_info->ring, 0,
4390bcc9736cSJeff Kirsher 		sizeof(struct ksz_desc) * desc_info->alloc);
4391bcc9736cSJeff Kirsher 	hw_init_desc(desc_info, transmit);
4392bcc9736cSJeff Kirsher 	return 0;
4393bcc9736cSJeff Kirsher }
4394bcc9736cSJeff Kirsher 
4395bcc9736cSJeff Kirsher /**
4396bcc9736cSJeff Kirsher  * ksz_alloc_desc - allocate hardware descriptors
4397bcc9736cSJeff Kirsher  * @adapter:	Adapter information structure.
4398bcc9736cSJeff Kirsher  *
4399bcc9736cSJeff Kirsher  * This local function allocates hardware descriptors for receiving and
4400bcc9736cSJeff Kirsher  * transmitting.
4401bcc9736cSJeff Kirsher  *
4402bcc9736cSJeff Kirsher  * Return 0 if successful.
4403bcc9736cSJeff Kirsher  */
4404bcc9736cSJeff Kirsher static int ksz_alloc_desc(struct dev_info *adapter)
4405bcc9736cSJeff Kirsher {
4406bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &adapter->hw;
4407bcc9736cSJeff Kirsher 	int offset;
4408bcc9736cSJeff Kirsher 
4409bcc9736cSJeff Kirsher 	/* Allocate memory for RX & TX descriptors. */
4410bcc9736cSJeff Kirsher 	adapter->desc_pool.alloc_size =
4411bcc9736cSJeff Kirsher 		hw->rx_desc_info.size * hw->rx_desc_info.alloc +
4412bcc9736cSJeff Kirsher 		hw->tx_desc_info.size * hw->tx_desc_info.alloc +
4413bcc9736cSJeff Kirsher 		DESC_ALIGNMENT;
4414bcc9736cSJeff Kirsher 
4415bcc9736cSJeff Kirsher 	adapter->desc_pool.alloc_virt =
4416bcc9736cSJeff Kirsher 		pci_alloc_consistent(
4417bcc9736cSJeff Kirsher 			adapter->pdev, adapter->desc_pool.alloc_size,
4418bcc9736cSJeff Kirsher 			&adapter->desc_pool.dma_addr);
4419bcc9736cSJeff Kirsher 	if (adapter->desc_pool.alloc_virt == NULL) {
4420bcc9736cSJeff Kirsher 		adapter->desc_pool.alloc_size = 0;
4421bcc9736cSJeff Kirsher 		return 1;
4422bcc9736cSJeff Kirsher 	}
4423bcc9736cSJeff Kirsher 	memset(adapter->desc_pool.alloc_virt, 0, adapter->desc_pool.alloc_size);
4424bcc9736cSJeff Kirsher 
4425bcc9736cSJeff Kirsher 	/* Align to the next cache line boundary. */
4426bcc9736cSJeff Kirsher 	offset = (((ulong) adapter->desc_pool.alloc_virt % DESC_ALIGNMENT) ?
4427bcc9736cSJeff Kirsher 		(DESC_ALIGNMENT -
4428bcc9736cSJeff Kirsher 		((ulong) adapter->desc_pool.alloc_virt % DESC_ALIGNMENT)) : 0);
4429bcc9736cSJeff Kirsher 	adapter->desc_pool.virt = adapter->desc_pool.alloc_virt + offset;
4430bcc9736cSJeff Kirsher 	adapter->desc_pool.phys = adapter->desc_pool.dma_addr + offset;
4431bcc9736cSJeff Kirsher 
4432bcc9736cSJeff Kirsher 	/* Allocate receive/transmit descriptors. */
4433bcc9736cSJeff Kirsher 	hw->rx_desc_info.ring_virt = (struct ksz_hw_desc *)
4434bcc9736cSJeff Kirsher 		adapter->desc_pool.virt;
4435bcc9736cSJeff Kirsher 	hw->rx_desc_info.ring_phys = adapter->desc_pool.phys;
4436bcc9736cSJeff Kirsher 	offset = hw->rx_desc_info.alloc * hw->rx_desc_info.size;
4437bcc9736cSJeff Kirsher 	hw->tx_desc_info.ring_virt = (struct ksz_hw_desc *)
4438bcc9736cSJeff Kirsher 		(adapter->desc_pool.virt + offset);
4439bcc9736cSJeff Kirsher 	hw->tx_desc_info.ring_phys = adapter->desc_pool.phys + offset;
4440bcc9736cSJeff Kirsher 
4441bcc9736cSJeff Kirsher 	if (ksz_alloc_soft_desc(&hw->rx_desc_info, 0))
4442bcc9736cSJeff Kirsher 		return 1;
4443bcc9736cSJeff Kirsher 	if (ksz_alloc_soft_desc(&hw->tx_desc_info, 1))
4444bcc9736cSJeff Kirsher 		return 1;
4445bcc9736cSJeff Kirsher 
4446bcc9736cSJeff Kirsher 	return 0;
4447bcc9736cSJeff Kirsher }
4448bcc9736cSJeff Kirsher 
4449bcc9736cSJeff Kirsher /**
4450bcc9736cSJeff Kirsher  * free_dma_buf - release DMA buffer resources
4451bcc9736cSJeff Kirsher  * @adapter:	Adapter information structure.
4452bcc9736cSJeff Kirsher  *
4453bcc9736cSJeff Kirsher  * This routine is just a helper function to release the DMA buffer resources.
4454bcc9736cSJeff Kirsher  */
4455bcc9736cSJeff Kirsher static void free_dma_buf(struct dev_info *adapter, struct ksz_dma_buf *dma_buf,
4456bcc9736cSJeff Kirsher 	int direction)
4457bcc9736cSJeff Kirsher {
4458bcc9736cSJeff Kirsher 	pci_unmap_single(adapter->pdev, dma_buf->dma, dma_buf->len, direction);
4459bcc9736cSJeff Kirsher 	dev_kfree_skb(dma_buf->skb);
4460bcc9736cSJeff Kirsher 	dma_buf->skb = NULL;
4461bcc9736cSJeff Kirsher 	dma_buf->dma = 0;
4462bcc9736cSJeff Kirsher }
4463bcc9736cSJeff Kirsher 
4464bcc9736cSJeff Kirsher /**
4465bcc9736cSJeff Kirsher  * ksz_init_rx_buffers - initialize receive descriptors
4466bcc9736cSJeff Kirsher  * @adapter:	Adapter information structure.
4467bcc9736cSJeff Kirsher  *
4468bcc9736cSJeff Kirsher  * This routine initializes DMA buffers for receiving.
4469bcc9736cSJeff Kirsher  */
4470bcc9736cSJeff Kirsher static void ksz_init_rx_buffers(struct dev_info *adapter)
4471bcc9736cSJeff Kirsher {
4472bcc9736cSJeff Kirsher 	int i;
4473bcc9736cSJeff Kirsher 	struct ksz_desc *desc;
4474bcc9736cSJeff Kirsher 	struct ksz_dma_buf *dma_buf;
4475bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &adapter->hw;
4476bcc9736cSJeff Kirsher 	struct ksz_desc_info *info = &hw->rx_desc_info;
4477bcc9736cSJeff Kirsher 
4478bcc9736cSJeff Kirsher 	for (i = 0; i < hw->rx_desc_info.alloc; i++) {
4479bcc9736cSJeff Kirsher 		get_rx_pkt(info, &desc);
4480bcc9736cSJeff Kirsher 
4481bcc9736cSJeff Kirsher 		dma_buf = DMA_BUFFER(desc);
4482bcc9736cSJeff Kirsher 		if (dma_buf->skb && dma_buf->len != adapter->mtu)
4483bcc9736cSJeff Kirsher 			free_dma_buf(adapter, dma_buf, PCI_DMA_FROMDEVICE);
4484bcc9736cSJeff Kirsher 		dma_buf->len = adapter->mtu;
4485bcc9736cSJeff Kirsher 		if (!dma_buf->skb)
4486bcc9736cSJeff Kirsher 			dma_buf->skb = alloc_skb(dma_buf->len, GFP_ATOMIC);
4487bcc9736cSJeff Kirsher 		if (dma_buf->skb && !dma_buf->dma) {
4488bcc9736cSJeff Kirsher 			dma_buf->skb->dev = adapter->dev;
4489bcc9736cSJeff Kirsher 			dma_buf->dma = pci_map_single(
4490bcc9736cSJeff Kirsher 				adapter->pdev,
4491bcc9736cSJeff Kirsher 				skb_tail_pointer(dma_buf->skb),
4492bcc9736cSJeff Kirsher 				dma_buf->len,
4493bcc9736cSJeff Kirsher 				PCI_DMA_FROMDEVICE);
4494bcc9736cSJeff Kirsher 		}
4495bcc9736cSJeff Kirsher 
4496bcc9736cSJeff Kirsher 		/* Set descriptor. */
4497bcc9736cSJeff Kirsher 		set_rx_buf(desc, dma_buf->dma);
4498bcc9736cSJeff Kirsher 		set_rx_len(desc, dma_buf->len);
4499bcc9736cSJeff Kirsher 		release_desc(desc);
4500bcc9736cSJeff Kirsher 	}
4501bcc9736cSJeff Kirsher }
4502bcc9736cSJeff Kirsher 
4503bcc9736cSJeff Kirsher /**
4504bcc9736cSJeff Kirsher  * ksz_alloc_mem - allocate memory for hardware descriptors
4505bcc9736cSJeff Kirsher  * @adapter:	Adapter information structure.
4506bcc9736cSJeff Kirsher  *
4507bcc9736cSJeff Kirsher  * This function allocates memory for use by hardware descriptors for receiving
4508bcc9736cSJeff Kirsher  * and transmitting.
4509bcc9736cSJeff Kirsher  *
4510bcc9736cSJeff Kirsher  * Return 0 if successful.
4511bcc9736cSJeff Kirsher  */
4512bcc9736cSJeff Kirsher static int ksz_alloc_mem(struct dev_info *adapter)
4513bcc9736cSJeff Kirsher {
4514bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &adapter->hw;
4515bcc9736cSJeff Kirsher 
4516bcc9736cSJeff Kirsher 	/* Determine the number of receive and transmit descriptors. */
4517bcc9736cSJeff Kirsher 	hw->rx_desc_info.alloc = NUM_OF_RX_DESC;
4518bcc9736cSJeff Kirsher 	hw->tx_desc_info.alloc = NUM_OF_TX_DESC;
4519bcc9736cSJeff Kirsher 
4520bcc9736cSJeff Kirsher 	/* Determine how many descriptors to skip transmit interrupt. */
4521bcc9736cSJeff Kirsher 	hw->tx_int_cnt = 0;
4522bcc9736cSJeff Kirsher 	hw->tx_int_mask = NUM_OF_TX_DESC / 4;
4523bcc9736cSJeff Kirsher 	if (hw->tx_int_mask > 8)
4524bcc9736cSJeff Kirsher 		hw->tx_int_mask = 8;
4525bcc9736cSJeff Kirsher 	while (hw->tx_int_mask) {
4526bcc9736cSJeff Kirsher 		hw->tx_int_cnt++;
4527bcc9736cSJeff Kirsher 		hw->tx_int_mask >>= 1;
4528bcc9736cSJeff Kirsher 	}
4529bcc9736cSJeff Kirsher 	if (hw->tx_int_cnt) {
4530bcc9736cSJeff Kirsher 		hw->tx_int_mask = (1 << (hw->tx_int_cnt - 1)) - 1;
4531bcc9736cSJeff Kirsher 		hw->tx_int_cnt = 0;
4532bcc9736cSJeff Kirsher 	}
4533bcc9736cSJeff Kirsher 
4534bcc9736cSJeff Kirsher 	/* Determine the descriptor size. */
4535bcc9736cSJeff Kirsher 	hw->rx_desc_info.size =
4536bcc9736cSJeff Kirsher 		(((sizeof(struct ksz_hw_desc) + DESC_ALIGNMENT - 1) /
4537bcc9736cSJeff Kirsher 		DESC_ALIGNMENT) * DESC_ALIGNMENT);
4538bcc9736cSJeff Kirsher 	hw->tx_desc_info.size =
4539bcc9736cSJeff Kirsher 		(((sizeof(struct ksz_hw_desc) + DESC_ALIGNMENT - 1) /
4540bcc9736cSJeff Kirsher 		DESC_ALIGNMENT) * DESC_ALIGNMENT);
4541bcc9736cSJeff Kirsher 	if (hw->rx_desc_info.size != sizeof(struct ksz_hw_desc))
4542bcc9736cSJeff Kirsher 		pr_alert("Hardware descriptor size not right!\n");
4543bcc9736cSJeff Kirsher 	ksz_check_desc_num(&hw->rx_desc_info);
4544bcc9736cSJeff Kirsher 	ksz_check_desc_num(&hw->tx_desc_info);
4545bcc9736cSJeff Kirsher 
4546bcc9736cSJeff Kirsher 	/* Allocate descriptors. */
4547bcc9736cSJeff Kirsher 	if (ksz_alloc_desc(adapter))
4548bcc9736cSJeff Kirsher 		return 1;
4549bcc9736cSJeff Kirsher 
4550bcc9736cSJeff Kirsher 	return 0;
4551bcc9736cSJeff Kirsher }
4552bcc9736cSJeff Kirsher 
4553bcc9736cSJeff Kirsher /**
4554bcc9736cSJeff Kirsher  * ksz_free_desc - free software and hardware descriptors
4555bcc9736cSJeff Kirsher  * @adapter:	Adapter information structure.
4556bcc9736cSJeff Kirsher  *
4557bcc9736cSJeff Kirsher  * This local routine frees the software and hardware descriptors allocated by
4558bcc9736cSJeff Kirsher  * ksz_alloc_desc().
4559bcc9736cSJeff Kirsher  */
4560bcc9736cSJeff Kirsher static void ksz_free_desc(struct dev_info *adapter)
4561bcc9736cSJeff Kirsher {
4562bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &adapter->hw;
4563bcc9736cSJeff Kirsher 
4564bcc9736cSJeff Kirsher 	/* Reset descriptor. */
4565bcc9736cSJeff Kirsher 	hw->rx_desc_info.ring_virt = NULL;
4566bcc9736cSJeff Kirsher 	hw->tx_desc_info.ring_virt = NULL;
4567bcc9736cSJeff Kirsher 	hw->rx_desc_info.ring_phys = 0;
4568bcc9736cSJeff Kirsher 	hw->tx_desc_info.ring_phys = 0;
4569bcc9736cSJeff Kirsher 
4570bcc9736cSJeff Kirsher 	/* Free memory. */
4571bcc9736cSJeff Kirsher 	if (adapter->desc_pool.alloc_virt)
4572bcc9736cSJeff Kirsher 		pci_free_consistent(
4573bcc9736cSJeff Kirsher 			adapter->pdev,
4574bcc9736cSJeff Kirsher 			adapter->desc_pool.alloc_size,
4575bcc9736cSJeff Kirsher 			adapter->desc_pool.alloc_virt,
4576bcc9736cSJeff Kirsher 			adapter->desc_pool.dma_addr);
4577bcc9736cSJeff Kirsher 
4578bcc9736cSJeff Kirsher 	/* Reset resource pool. */
4579bcc9736cSJeff Kirsher 	adapter->desc_pool.alloc_size = 0;
4580bcc9736cSJeff Kirsher 	adapter->desc_pool.alloc_virt = NULL;
4581bcc9736cSJeff Kirsher 
4582bcc9736cSJeff Kirsher 	kfree(hw->rx_desc_info.ring);
4583bcc9736cSJeff Kirsher 	hw->rx_desc_info.ring = NULL;
4584bcc9736cSJeff Kirsher 	kfree(hw->tx_desc_info.ring);
4585bcc9736cSJeff Kirsher 	hw->tx_desc_info.ring = NULL;
4586bcc9736cSJeff Kirsher }
4587bcc9736cSJeff Kirsher 
4588bcc9736cSJeff Kirsher /**
4589bcc9736cSJeff Kirsher  * ksz_free_buffers - free buffers used in the descriptors
4590bcc9736cSJeff Kirsher  * @adapter:	Adapter information structure.
4591bcc9736cSJeff Kirsher  * @desc_info:	Descriptor information structure.
4592bcc9736cSJeff Kirsher  *
4593bcc9736cSJeff Kirsher  * This local routine frees buffers used in the DMA buffers.
4594bcc9736cSJeff Kirsher  */
4595bcc9736cSJeff Kirsher static void ksz_free_buffers(struct dev_info *adapter,
4596bcc9736cSJeff Kirsher 	struct ksz_desc_info *desc_info, int direction)
4597bcc9736cSJeff Kirsher {
4598bcc9736cSJeff Kirsher 	int i;
4599bcc9736cSJeff Kirsher 	struct ksz_dma_buf *dma_buf;
4600bcc9736cSJeff Kirsher 	struct ksz_desc *desc = desc_info->ring;
4601bcc9736cSJeff Kirsher 
4602bcc9736cSJeff Kirsher 	for (i = 0; i < desc_info->alloc; i++) {
4603bcc9736cSJeff Kirsher 		dma_buf = DMA_BUFFER(desc);
4604bcc9736cSJeff Kirsher 		if (dma_buf->skb)
4605bcc9736cSJeff Kirsher 			free_dma_buf(adapter, dma_buf, direction);
4606bcc9736cSJeff Kirsher 		desc++;
4607bcc9736cSJeff Kirsher 	}
4608bcc9736cSJeff Kirsher }
4609bcc9736cSJeff Kirsher 
4610bcc9736cSJeff Kirsher /**
4611bcc9736cSJeff Kirsher  * ksz_free_mem - free all resources used by descriptors
4612bcc9736cSJeff Kirsher  * @adapter:	Adapter information structure.
4613bcc9736cSJeff Kirsher  *
4614bcc9736cSJeff Kirsher  * This local routine frees all the resources allocated by ksz_alloc_mem().
4615bcc9736cSJeff Kirsher  */
4616bcc9736cSJeff Kirsher static void ksz_free_mem(struct dev_info *adapter)
4617bcc9736cSJeff Kirsher {
4618bcc9736cSJeff Kirsher 	/* Free transmit buffers. */
4619bcc9736cSJeff Kirsher 	ksz_free_buffers(adapter, &adapter->hw.tx_desc_info,
4620bcc9736cSJeff Kirsher 		PCI_DMA_TODEVICE);
4621bcc9736cSJeff Kirsher 
4622bcc9736cSJeff Kirsher 	/* Free receive buffers. */
4623bcc9736cSJeff Kirsher 	ksz_free_buffers(adapter, &adapter->hw.rx_desc_info,
4624bcc9736cSJeff Kirsher 		PCI_DMA_FROMDEVICE);
4625bcc9736cSJeff Kirsher 
4626bcc9736cSJeff Kirsher 	/* Free descriptors. */
4627bcc9736cSJeff Kirsher 	ksz_free_desc(adapter);
4628bcc9736cSJeff Kirsher }
4629bcc9736cSJeff Kirsher 
4630bcc9736cSJeff Kirsher static void get_mib_counters(struct ksz_hw *hw, int first, int cnt,
4631bcc9736cSJeff Kirsher 	u64 *counter)
4632bcc9736cSJeff Kirsher {
4633bcc9736cSJeff Kirsher 	int i;
4634bcc9736cSJeff Kirsher 	int mib;
4635bcc9736cSJeff Kirsher 	int port;
4636bcc9736cSJeff Kirsher 	struct ksz_port_mib *port_mib;
4637bcc9736cSJeff Kirsher 
4638bcc9736cSJeff Kirsher 	memset(counter, 0, sizeof(u64) * TOTAL_PORT_COUNTER_NUM);
4639bcc9736cSJeff Kirsher 	for (i = 0, port = first; i < cnt; i++, port++) {
4640bcc9736cSJeff Kirsher 		port_mib = &hw->port_mib[port];
4641bcc9736cSJeff Kirsher 		for (mib = port_mib->mib_start; mib < hw->mib_cnt; mib++)
4642bcc9736cSJeff Kirsher 			counter[mib] += port_mib->counter[mib];
4643bcc9736cSJeff Kirsher 	}
4644bcc9736cSJeff Kirsher }
4645bcc9736cSJeff Kirsher 
4646bcc9736cSJeff Kirsher /**
4647bcc9736cSJeff Kirsher  * send_packet - send packet
4648bcc9736cSJeff Kirsher  * @skb:	Socket buffer.
4649bcc9736cSJeff Kirsher  * @dev:	Network device.
4650bcc9736cSJeff Kirsher  *
4651bcc9736cSJeff Kirsher  * This routine is used to send a packet out to the network.
4652bcc9736cSJeff Kirsher  */
4653bcc9736cSJeff Kirsher static void send_packet(struct sk_buff *skb, struct net_device *dev)
4654bcc9736cSJeff Kirsher {
4655bcc9736cSJeff Kirsher 	struct ksz_desc *desc;
4656bcc9736cSJeff Kirsher 	struct ksz_desc *first;
4657bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
4658bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
4659bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
4660bcc9736cSJeff Kirsher 	struct ksz_desc_info *info = &hw->tx_desc_info;
4661bcc9736cSJeff Kirsher 	struct ksz_dma_buf *dma_buf;
4662bcc9736cSJeff Kirsher 	int len;
4663bcc9736cSJeff Kirsher 	int last_frag = skb_shinfo(skb)->nr_frags;
4664bcc9736cSJeff Kirsher 
4665bcc9736cSJeff Kirsher 	/*
4666bcc9736cSJeff Kirsher 	 * KSZ8842 with multiple device interfaces needs to be told which port
4667bcc9736cSJeff Kirsher 	 * to send.
4668bcc9736cSJeff Kirsher 	 */
4669bcc9736cSJeff Kirsher 	if (hw->dev_count > 1)
4670bcc9736cSJeff Kirsher 		hw->dst_ports = 1 << priv->port.first_port;
4671bcc9736cSJeff Kirsher 
4672bcc9736cSJeff Kirsher 	/* Hardware will pad the length to 60. */
4673bcc9736cSJeff Kirsher 	len = skb->len;
4674bcc9736cSJeff Kirsher 
4675bcc9736cSJeff Kirsher 	/* Remember the very first descriptor. */
4676bcc9736cSJeff Kirsher 	first = info->cur;
4677bcc9736cSJeff Kirsher 	desc = first;
4678bcc9736cSJeff Kirsher 
4679bcc9736cSJeff Kirsher 	dma_buf = DMA_BUFFER(desc);
4680bcc9736cSJeff Kirsher 	if (last_frag) {
4681bcc9736cSJeff Kirsher 		int frag;
4682bcc9736cSJeff Kirsher 		skb_frag_t *this_frag;
4683bcc9736cSJeff Kirsher 
4684bcc9736cSJeff Kirsher 		dma_buf->len = skb_headlen(skb);
4685bcc9736cSJeff Kirsher 
4686bcc9736cSJeff Kirsher 		dma_buf->dma = pci_map_single(
4687bcc9736cSJeff Kirsher 			hw_priv->pdev, skb->data, dma_buf->len,
4688bcc9736cSJeff Kirsher 			PCI_DMA_TODEVICE);
4689bcc9736cSJeff Kirsher 		set_tx_buf(desc, dma_buf->dma);
4690bcc9736cSJeff Kirsher 		set_tx_len(desc, dma_buf->len);
4691bcc9736cSJeff Kirsher 
4692bcc9736cSJeff Kirsher 		frag = 0;
4693bcc9736cSJeff Kirsher 		do {
4694bcc9736cSJeff Kirsher 			this_frag = &skb_shinfo(skb)->frags[frag];
4695bcc9736cSJeff Kirsher 
4696bcc9736cSJeff Kirsher 			/* Get a new descriptor. */
4697bcc9736cSJeff Kirsher 			get_tx_pkt(info, &desc);
4698bcc9736cSJeff Kirsher 
4699bcc9736cSJeff Kirsher 			/* Keep track of descriptors used so far. */
4700bcc9736cSJeff Kirsher 			++hw->tx_int_cnt;
4701bcc9736cSJeff Kirsher 
4702bcc9736cSJeff Kirsher 			dma_buf = DMA_BUFFER(desc);
4703bcc9736cSJeff Kirsher 			dma_buf->len = this_frag->size;
4704bcc9736cSJeff Kirsher 
4705bcc9736cSJeff Kirsher 			dma_buf->dma = pci_map_single(
4706bcc9736cSJeff Kirsher 				hw_priv->pdev,
4707*787343adSIan Campbell 				skb_frag_address(this_frag),
4708bcc9736cSJeff Kirsher 				dma_buf->len,
4709bcc9736cSJeff Kirsher 				PCI_DMA_TODEVICE);
4710bcc9736cSJeff Kirsher 			set_tx_buf(desc, dma_buf->dma);
4711bcc9736cSJeff Kirsher 			set_tx_len(desc, dma_buf->len);
4712bcc9736cSJeff Kirsher 
4713bcc9736cSJeff Kirsher 			frag++;
4714bcc9736cSJeff Kirsher 			if (frag == last_frag)
4715bcc9736cSJeff Kirsher 				break;
4716bcc9736cSJeff Kirsher 
4717bcc9736cSJeff Kirsher 			/* Do not release the last descriptor here. */
4718bcc9736cSJeff Kirsher 			release_desc(desc);
4719bcc9736cSJeff Kirsher 		} while (1);
4720bcc9736cSJeff Kirsher 
4721bcc9736cSJeff Kirsher 		/* current points to the last descriptor. */
4722bcc9736cSJeff Kirsher 		info->cur = desc;
4723bcc9736cSJeff Kirsher 
4724bcc9736cSJeff Kirsher 		/* Release the first descriptor. */
4725bcc9736cSJeff Kirsher 		release_desc(first);
4726bcc9736cSJeff Kirsher 	} else {
4727bcc9736cSJeff Kirsher 		dma_buf->len = len;
4728bcc9736cSJeff Kirsher 
4729bcc9736cSJeff Kirsher 		dma_buf->dma = pci_map_single(
4730bcc9736cSJeff Kirsher 			hw_priv->pdev, skb->data, dma_buf->len,
4731bcc9736cSJeff Kirsher 			PCI_DMA_TODEVICE);
4732bcc9736cSJeff Kirsher 		set_tx_buf(desc, dma_buf->dma);
4733bcc9736cSJeff Kirsher 		set_tx_len(desc, dma_buf->len);
4734bcc9736cSJeff Kirsher 	}
4735bcc9736cSJeff Kirsher 
4736bcc9736cSJeff Kirsher 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
4737bcc9736cSJeff Kirsher 		(desc)->sw.buf.tx.csum_gen_tcp = 1;
4738bcc9736cSJeff Kirsher 		(desc)->sw.buf.tx.csum_gen_udp = 1;
4739bcc9736cSJeff Kirsher 	}
4740bcc9736cSJeff Kirsher 
4741bcc9736cSJeff Kirsher 	/*
4742bcc9736cSJeff Kirsher 	 * The last descriptor holds the packet so that it can be returned to
4743bcc9736cSJeff Kirsher 	 * network subsystem after all descriptors are transmitted.
4744bcc9736cSJeff Kirsher 	 */
4745bcc9736cSJeff Kirsher 	dma_buf->skb = skb;
4746bcc9736cSJeff Kirsher 
4747bcc9736cSJeff Kirsher 	hw_send_pkt(hw);
4748bcc9736cSJeff Kirsher 
4749bcc9736cSJeff Kirsher 	/* Update transmit statistics. */
4750bcc9736cSJeff Kirsher 	dev->stats.tx_packets++;
4751bcc9736cSJeff Kirsher 	dev->stats.tx_bytes += len;
4752bcc9736cSJeff Kirsher }
4753bcc9736cSJeff Kirsher 
4754bcc9736cSJeff Kirsher /**
4755bcc9736cSJeff Kirsher  * transmit_cleanup - clean up transmit descriptors
4756bcc9736cSJeff Kirsher  * @dev:	Network device.
4757bcc9736cSJeff Kirsher  *
4758bcc9736cSJeff Kirsher  * This routine is called to clean up the transmitted buffers.
4759bcc9736cSJeff Kirsher  */
4760bcc9736cSJeff Kirsher static void transmit_cleanup(struct dev_info *hw_priv, int normal)
4761bcc9736cSJeff Kirsher {
4762bcc9736cSJeff Kirsher 	int last;
4763bcc9736cSJeff Kirsher 	union desc_stat status;
4764bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
4765bcc9736cSJeff Kirsher 	struct ksz_desc_info *info = &hw->tx_desc_info;
4766bcc9736cSJeff Kirsher 	struct ksz_desc *desc;
4767bcc9736cSJeff Kirsher 	struct ksz_dma_buf *dma_buf;
4768bcc9736cSJeff Kirsher 	struct net_device *dev = NULL;
4769bcc9736cSJeff Kirsher 
4770bcc9736cSJeff Kirsher 	spin_lock(&hw_priv->hwlock);
4771bcc9736cSJeff Kirsher 	last = info->last;
4772bcc9736cSJeff Kirsher 
4773bcc9736cSJeff Kirsher 	while (info->avail < info->alloc) {
4774bcc9736cSJeff Kirsher 		/* Get next descriptor which is not hardware owned. */
4775bcc9736cSJeff Kirsher 		desc = &info->ring[last];
4776bcc9736cSJeff Kirsher 		status.data = le32_to_cpu(desc->phw->ctrl.data);
4777bcc9736cSJeff Kirsher 		if (status.tx.hw_owned) {
4778bcc9736cSJeff Kirsher 			if (normal)
4779bcc9736cSJeff Kirsher 				break;
4780bcc9736cSJeff Kirsher 			else
4781bcc9736cSJeff Kirsher 				reset_desc(desc, status);
4782bcc9736cSJeff Kirsher 		}
4783bcc9736cSJeff Kirsher 
4784bcc9736cSJeff Kirsher 		dma_buf = DMA_BUFFER(desc);
4785bcc9736cSJeff Kirsher 		pci_unmap_single(
4786bcc9736cSJeff Kirsher 			hw_priv->pdev, dma_buf->dma, dma_buf->len,
4787bcc9736cSJeff Kirsher 			PCI_DMA_TODEVICE);
4788bcc9736cSJeff Kirsher 
4789bcc9736cSJeff Kirsher 		/* This descriptor contains the last buffer in the packet. */
4790bcc9736cSJeff Kirsher 		if (dma_buf->skb) {
4791bcc9736cSJeff Kirsher 			dev = dma_buf->skb->dev;
4792bcc9736cSJeff Kirsher 
4793bcc9736cSJeff Kirsher 			/* Release the packet back to network subsystem. */
4794bcc9736cSJeff Kirsher 			dev_kfree_skb_irq(dma_buf->skb);
4795bcc9736cSJeff Kirsher 			dma_buf->skb = NULL;
4796bcc9736cSJeff Kirsher 		}
4797bcc9736cSJeff Kirsher 
4798bcc9736cSJeff Kirsher 		/* Free the transmitted descriptor. */
4799bcc9736cSJeff Kirsher 		last++;
4800bcc9736cSJeff Kirsher 		last &= info->mask;
4801bcc9736cSJeff Kirsher 		info->avail++;
4802bcc9736cSJeff Kirsher 	}
4803bcc9736cSJeff Kirsher 	info->last = last;
4804bcc9736cSJeff Kirsher 	spin_unlock(&hw_priv->hwlock);
4805bcc9736cSJeff Kirsher 
4806bcc9736cSJeff Kirsher 	/* Notify the network subsystem that the packet has been sent. */
4807bcc9736cSJeff Kirsher 	if (dev)
4808bcc9736cSJeff Kirsher 		dev->trans_start = jiffies;
4809bcc9736cSJeff Kirsher }
4810bcc9736cSJeff Kirsher 
4811bcc9736cSJeff Kirsher /**
4812bcc9736cSJeff Kirsher  * transmit_done - transmit done processing
4813bcc9736cSJeff Kirsher  * @dev:	Network device.
4814bcc9736cSJeff Kirsher  *
4815bcc9736cSJeff Kirsher  * This routine is called when the transmit interrupt is triggered, indicating
4816bcc9736cSJeff Kirsher  * either a packet is sent successfully or there are transmit errors.
4817bcc9736cSJeff Kirsher  */
4818bcc9736cSJeff Kirsher static void tx_done(struct dev_info *hw_priv)
4819bcc9736cSJeff Kirsher {
4820bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
4821bcc9736cSJeff Kirsher 	int port;
4822bcc9736cSJeff Kirsher 
4823bcc9736cSJeff Kirsher 	transmit_cleanup(hw_priv, 1);
4824bcc9736cSJeff Kirsher 
4825bcc9736cSJeff Kirsher 	for (port = 0; port < hw->dev_count; port++) {
4826bcc9736cSJeff Kirsher 		struct net_device *dev = hw->port_info[port].pdev;
4827bcc9736cSJeff Kirsher 
4828bcc9736cSJeff Kirsher 		if (netif_running(dev) && netif_queue_stopped(dev))
4829bcc9736cSJeff Kirsher 			netif_wake_queue(dev);
4830bcc9736cSJeff Kirsher 	}
4831bcc9736cSJeff Kirsher }
4832bcc9736cSJeff Kirsher 
4833bcc9736cSJeff Kirsher static inline void copy_old_skb(struct sk_buff *old, struct sk_buff *skb)
4834bcc9736cSJeff Kirsher {
4835bcc9736cSJeff Kirsher 	skb->dev = old->dev;
4836bcc9736cSJeff Kirsher 	skb->protocol = old->protocol;
4837bcc9736cSJeff Kirsher 	skb->ip_summed = old->ip_summed;
4838bcc9736cSJeff Kirsher 	skb->csum = old->csum;
4839bcc9736cSJeff Kirsher 	skb_set_network_header(skb, ETH_HLEN);
4840bcc9736cSJeff Kirsher 
4841bcc9736cSJeff Kirsher 	dev_kfree_skb(old);
4842bcc9736cSJeff Kirsher }
4843bcc9736cSJeff Kirsher 
4844bcc9736cSJeff Kirsher /**
4845bcc9736cSJeff Kirsher  * netdev_tx - send out packet
4846bcc9736cSJeff Kirsher  * @skb:	Socket buffer.
4847bcc9736cSJeff Kirsher  * @dev:	Network device.
4848bcc9736cSJeff Kirsher  *
4849bcc9736cSJeff Kirsher  * This function is used by the upper network layer to send out a packet.
4850bcc9736cSJeff Kirsher  *
4851bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code indicating failure.
4852bcc9736cSJeff Kirsher  */
4853bcc9736cSJeff Kirsher static netdev_tx_t netdev_tx(struct sk_buff *skb, struct net_device *dev)
4854bcc9736cSJeff Kirsher {
4855bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
4856bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
4857bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
4858bcc9736cSJeff Kirsher 	int left;
4859bcc9736cSJeff Kirsher 	int num = 1;
4860bcc9736cSJeff Kirsher 	int rc = 0;
4861bcc9736cSJeff Kirsher 
4862bcc9736cSJeff Kirsher 	if (hw->features & SMALL_PACKET_TX_BUG) {
4863bcc9736cSJeff Kirsher 		struct sk_buff *org_skb = skb;
4864bcc9736cSJeff Kirsher 
4865bcc9736cSJeff Kirsher 		if (skb->len <= 48) {
4866bcc9736cSJeff Kirsher 			if (skb_end_pointer(skb) - skb->data >= 50) {
4867bcc9736cSJeff Kirsher 				memset(&skb->data[skb->len], 0, 50 - skb->len);
4868bcc9736cSJeff Kirsher 				skb->len = 50;
4869bcc9736cSJeff Kirsher 			} else {
4870bcc9736cSJeff Kirsher 				skb = dev_alloc_skb(50);
4871bcc9736cSJeff Kirsher 				if (!skb)
4872bcc9736cSJeff Kirsher 					return NETDEV_TX_BUSY;
4873bcc9736cSJeff Kirsher 				memcpy(skb->data, org_skb->data, org_skb->len);
4874bcc9736cSJeff Kirsher 				memset(&skb->data[org_skb->len], 0,
4875bcc9736cSJeff Kirsher 					50 - org_skb->len);
4876bcc9736cSJeff Kirsher 				skb->len = 50;
4877bcc9736cSJeff Kirsher 				copy_old_skb(org_skb, skb);
4878bcc9736cSJeff Kirsher 			}
4879bcc9736cSJeff Kirsher 		}
4880bcc9736cSJeff Kirsher 	}
4881bcc9736cSJeff Kirsher 
4882bcc9736cSJeff Kirsher 	spin_lock_irq(&hw_priv->hwlock);
4883bcc9736cSJeff Kirsher 
4884bcc9736cSJeff Kirsher 	num = skb_shinfo(skb)->nr_frags + 1;
4885bcc9736cSJeff Kirsher 	left = hw_alloc_pkt(hw, skb->len, num);
4886bcc9736cSJeff Kirsher 	if (left) {
4887bcc9736cSJeff Kirsher 		if (left < num ||
4888bcc9736cSJeff Kirsher 				((CHECKSUM_PARTIAL == skb->ip_summed) &&
4889bcc9736cSJeff Kirsher 				(ETH_P_IPV6 == htons(skb->protocol)))) {
4890bcc9736cSJeff Kirsher 			struct sk_buff *org_skb = skb;
4891bcc9736cSJeff Kirsher 
4892bcc9736cSJeff Kirsher 			skb = dev_alloc_skb(org_skb->len);
4893bcc9736cSJeff Kirsher 			if (!skb) {
4894bcc9736cSJeff Kirsher 				rc = NETDEV_TX_BUSY;
4895bcc9736cSJeff Kirsher 				goto unlock;
4896bcc9736cSJeff Kirsher 			}
4897bcc9736cSJeff Kirsher 			skb_copy_and_csum_dev(org_skb, skb->data);
4898bcc9736cSJeff Kirsher 			org_skb->ip_summed = CHECKSUM_NONE;
4899bcc9736cSJeff Kirsher 			skb->len = org_skb->len;
4900bcc9736cSJeff Kirsher 			copy_old_skb(org_skb, skb);
4901bcc9736cSJeff Kirsher 		}
4902bcc9736cSJeff Kirsher 		send_packet(skb, dev);
4903bcc9736cSJeff Kirsher 		if (left <= num)
4904bcc9736cSJeff Kirsher 			netif_stop_queue(dev);
4905bcc9736cSJeff Kirsher 	} else {
4906bcc9736cSJeff Kirsher 		/* Stop the transmit queue until packet is allocated. */
4907bcc9736cSJeff Kirsher 		netif_stop_queue(dev);
4908bcc9736cSJeff Kirsher 		rc = NETDEV_TX_BUSY;
4909bcc9736cSJeff Kirsher 	}
4910bcc9736cSJeff Kirsher unlock:
4911bcc9736cSJeff Kirsher 	spin_unlock_irq(&hw_priv->hwlock);
4912bcc9736cSJeff Kirsher 
4913bcc9736cSJeff Kirsher 	return rc;
4914bcc9736cSJeff Kirsher }
4915bcc9736cSJeff Kirsher 
4916bcc9736cSJeff Kirsher /**
4917bcc9736cSJeff Kirsher  * netdev_tx_timeout - transmit timeout processing
4918bcc9736cSJeff Kirsher  * @dev:	Network device.
4919bcc9736cSJeff Kirsher  *
4920bcc9736cSJeff Kirsher  * This routine is called when the transmit timer expires.  That indicates the
4921bcc9736cSJeff Kirsher  * hardware is not running correctly because transmit interrupts are not
4922bcc9736cSJeff Kirsher  * triggered to free up resources so that the transmit routine can continue
4923bcc9736cSJeff Kirsher  * sending out packets.  The hardware is reset to correct the problem.
4924bcc9736cSJeff Kirsher  */
4925bcc9736cSJeff Kirsher static void netdev_tx_timeout(struct net_device *dev)
4926bcc9736cSJeff Kirsher {
4927bcc9736cSJeff Kirsher 	static unsigned long last_reset;
4928bcc9736cSJeff Kirsher 
4929bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
4930bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
4931bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
4932bcc9736cSJeff Kirsher 	int port;
4933bcc9736cSJeff Kirsher 
4934bcc9736cSJeff Kirsher 	if (hw->dev_count > 1) {
4935bcc9736cSJeff Kirsher 		/*
4936bcc9736cSJeff Kirsher 		 * Only reset the hardware if time between calls is long
4937bcc9736cSJeff Kirsher 		 * enough.
4938bcc9736cSJeff Kirsher 		 */
4939bcc9736cSJeff Kirsher 		if (jiffies - last_reset <= dev->watchdog_timeo)
4940bcc9736cSJeff Kirsher 			hw_priv = NULL;
4941bcc9736cSJeff Kirsher 	}
4942bcc9736cSJeff Kirsher 
4943bcc9736cSJeff Kirsher 	last_reset = jiffies;
4944bcc9736cSJeff Kirsher 	if (hw_priv) {
4945bcc9736cSJeff Kirsher 		hw_dis_intr(hw);
4946bcc9736cSJeff Kirsher 		hw_disable(hw);
4947bcc9736cSJeff Kirsher 
4948bcc9736cSJeff Kirsher 		transmit_cleanup(hw_priv, 0);
4949bcc9736cSJeff Kirsher 		hw_reset_pkts(&hw->rx_desc_info);
4950bcc9736cSJeff Kirsher 		hw_reset_pkts(&hw->tx_desc_info);
4951bcc9736cSJeff Kirsher 		ksz_init_rx_buffers(hw_priv);
4952bcc9736cSJeff Kirsher 
4953bcc9736cSJeff Kirsher 		hw_reset(hw);
4954bcc9736cSJeff Kirsher 
4955bcc9736cSJeff Kirsher 		hw_set_desc_base(hw,
4956bcc9736cSJeff Kirsher 			hw->tx_desc_info.ring_phys,
4957bcc9736cSJeff Kirsher 			hw->rx_desc_info.ring_phys);
4958bcc9736cSJeff Kirsher 		hw_set_addr(hw);
4959bcc9736cSJeff Kirsher 		if (hw->all_multi)
4960bcc9736cSJeff Kirsher 			hw_set_multicast(hw, hw->all_multi);
4961bcc9736cSJeff Kirsher 		else if (hw->multi_list_size)
4962bcc9736cSJeff Kirsher 			hw_set_grp_addr(hw);
4963bcc9736cSJeff Kirsher 
4964bcc9736cSJeff Kirsher 		if (hw->dev_count > 1) {
4965bcc9736cSJeff Kirsher 			hw_set_add_addr(hw);
4966bcc9736cSJeff Kirsher 			for (port = 0; port < SWITCH_PORT_NUM; port++) {
4967bcc9736cSJeff Kirsher 				struct net_device *port_dev;
4968bcc9736cSJeff Kirsher 
4969bcc9736cSJeff Kirsher 				port_set_stp_state(hw, port,
4970bcc9736cSJeff Kirsher 					STP_STATE_DISABLED);
4971bcc9736cSJeff Kirsher 
4972bcc9736cSJeff Kirsher 				port_dev = hw->port_info[port].pdev;
4973bcc9736cSJeff Kirsher 				if (netif_running(port_dev))
4974bcc9736cSJeff Kirsher 					port_set_stp_state(hw, port,
4975bcc9736cSJeff Kirsher 						STP_STATE_SIMPLE);
4976bcc9736cSJeff Kirsher 			}
4977bcc9736cSJeff Kirsher 		}
4978bcc9736cSJeff Kirsher 
4979bcc9736cSJeff Kirsher 		hw_enable(hw);
4980bcc9736cSJeff Kirsher 		hw_ena_intr(hw);
4981bcc9736cSJeff Kirsher 	}
4982bcc9736cSJeff Kirsher 
4983bcc9736cSJeff Kirsher 	dev->trans_start = jiffies;
4984bcc9736cSJeff Kirsher 	netif_wake_queue(dev);
4985bcc9736cSJeff Kirsher }
4986bcc9736cSJeff Kirsher 
4987bcc9736cSJeff Kirsher static inline void csum_verified(struct sk_buff *skb)
4988bcc9736cSJeff Kirsher {
4989bcc9736cSJeff Kirsher 	unsigned short protocol;
4990bcc9736cSJeff Kirsher 	struct iphdr *iph;
4991bcc9736cSJeff Kirsher 
4992bcc9736cSJeff Kirsher 	protocol = skb->protocol;
4993bcc9736cSJeff Kirsher 	skb_reset_network_header(skb);
4994bcc9736cSJeff Kirsher 	iph = (struct iphdr *) skb_network_header(skb);
4995bcc9736cSJeff Kirsher 	if (protocol == htons(ETH_P_8021Q)) {
4996bcc9736cSJeff Kirsher 		protocol = iph->tot_len;
4997bcc9736cSJeff Kirsher 		skb_set_network_header(skb, VLAN_HLEN);
4998bcc9736cSJeff Kirsher 		iph = (struct iphdr *) skb_network_header(skb);
4999bcc9736cSJeff Kirsher 	}
5000bcc9736cSJeff Kirsher 	if (protocol == htons(ETH_P_IP)) {
5001bcc9736cSJeff Kirsher 		if (iph->protocol == IPPROTO_TCP)
5002bcc9736cSJeff Kirsher 			skb->ip_summed = CHECKSUM_UNNECESSARY;
5003bcc9736cSJeff Kirsher 	}
5004bcc9736cSJeff Kirsher }
5005bcc9736cSJeff Kirsher 
5006bcc9736cSJeff Kirsher static inline int rx_proc(struct net_device *dev, struct ksz_hw* hw,
5007bcc9736cSJeff Kirsher 	struct ksz_desc *desc, union desc_stat status)
5008bcc9736cSJeff Kirsher {
5009bcc9736cSJeff Kirsher 	int packet_len;
5010bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5011bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5012bcc9736cSJeff Kirsher 	struct ksz_dma_buf *dma_buf;
5013bcc9736cSJeff Kirsher 	struct sk_buff *skb;
5014bcc9736cSJeff Kirsher 	int rx_status;
5015bcc9736cSJeff Kirsher 
5016bcc9736cSJeff Kirsher 	/* Received length includes 4-byte CRC. */
5017bcc9736cSJeff Kirsher 	packet_len = status.rx.frame_len - 4;
5018bcc9736cSJeff Kirsher 
5019bcc9736cSJeff Kirsher 	dma_buf = DMA_BUFFER(desc);
5020bcc9736cSJeff Kirsher 	pci_dma_sync_single_for_cpu(
5021bcc9736cSJeff Kirsher 		hw_priv->pdev, dma_buf->dma, packet_len + 4,
5022bcc9736cSJeff Kirsher 		PCI_DMA_FROMDEVICE);
5023bcc9736cSJeff Kirsher 
5024bcc9736cSJeff Kirsher 	do {
5025bcc9736cSJeff Kirsher 		/* skb->data != skb->head */
5026bcc9736cSJeff Kirsher 		skb = dev_alloc_skb(packet_len + 2);
5027bcc9736cSJeff Kirsher 		if (!skb) {
5028bcc9736cSJeff Kirsher 			dev->stats.rx_dropped++;
5029bcc9736cSJeff Kirsher 			return -ENOMEM;
5030bcc9736cSJeff Kirsher 		}
5031bcc9736cSJeff Kirsher 
5032bcc9736cSJeff Kirsher 		/*
5033bcc9736cSJeff Kirsher 		 * Align socket buffer in 4-byte boundary for better
5034bcc9736cSJeff Kirsher 		 * performance.
5035bcc9736cSJeff Kirsher 		 */
5036bcc9736cSJeff Kirsher 		skb_reserve(skb, 2);
5037bcc9736cSJeff Kirsher 
5038bcc9736cSJeff Kirsher 		memcpy(skb_put(skb, packet_len),
5039bcc9736cSJeff Kirsher 			dma_buf->skb->data, packet_len);
5040bcc9736cSJeff Kirsher 	} while (0);
5041bcc9736cSJeff Kirsher 
5042bcc9736cSJeff Kirsher 	skb->protocol = eth_type_trans(skb, dev);
5043bcc9736cSJeff Kirsher 
5044bcc9736cSJeff Kirsher 	if (hw->rx_cfg & (DMA_RX_CSUM_UDP | DMA_RX_CSUM_TCP))
5045bcc9736cSJeff Kirsher 		csum_verified(skb);
5046bcc9736cSJeff Kirsher 
5047bcc9736cSJeff Kirsher 	/* Update receive statistics. */
5048bcc9736cSJeff Kirsher 	dev->stats.rx_packets++;
5049bcc9736cSJeff Kirsher 	dev->stats.rx_bytes += packet_len;
5050bcc9736cSJeff Kirsher 
5051bcc9736cSJeff Kirsher 	/* Notify upper layer for received packet. */
5052bcc9736cSJeff Kirsher 	rx_status = netif_rx(skb);
5053bcc9736cSJeff Kirsher 
5054bcc9736cSJeff Kirsher 	return 0;
5055bcc9736cSJeff Kirsher }
5056bcc9736cSJeff Kirsher 
5057bcc9736cSJeff Kirsher static int dev_rcv_packets(struct dev_info *hw_priv)
5058bcc9736cSJeff Kirsher {
5059bcc9736cSJeff Kirsher 	int next;
5060bcc9736cSJeff Kirsher 	union desc_stat status;
5061bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5062bcc9736cSJeff Kirsher 	struct net_device *dev = hw->port_info[0].pdev;
5063bcc9736cSJeff Kirsher 	struct ksz_desc_info *info = &hw->rx_desc_info;
5064bcc9736cSJeff Kirsher 	int left = info->alloc;
5065bcc9736cSJeff Kirsher 	struct ksz_desc *desc;
5066bcc9736cSJeff Kirsher 	int received = 0;
5067bcc9736cSJeff Kirsher 
5068bcc9736cSJeff Kirsher 	next = info->next;
5069bcc9736cSJeff Kirsher 	while (left--) {
5070bcc9736cSJeff Kirsher 		/* Get next descriptor which is not hardware owned. */
5071bcc9736cSJeff Kirsher 		desc = &info->ring[next];
5072bcc9736cSJeff Kirsher 		status.data = le32_to_cpu(desc->phw->ctrl.data);
5073bcc9736cSJeff Kirsher 		if (status.rx.hw_owned)
5074bcc9736cSJeff Kirsher 			break;
5075bcc9736cSJeff Kirsher 
5076bcc9736cSJeff Kirsher 		/* Status valid only when last descriptor bit is set. */
5077bcc9736cSJeff Kirsher 		if (status.rx.last_desc && status.rx.first_desc) {
5078bcc9736cSJeff Kirsher 			if (rx_proc(dev, hw, desc, status))
5079bcc9736cSJeff Kirsher 				goto release_packet;
5080bcc9736cSJeff Kirsher 			received++;
5081bcc9736cSJeff Kirsher 		}
5082bcc9736cSJeff Kirsher 
5083bcc9736cSJeff Kirsher release_packet:
5084bcc9736cSJeff Kirsher 		release_desc(desc);
5085bcc9736cSJeff Kirsher 		next++;
5086bcc9736cSJeff Kirsher 		next &= info->mask;
5087bcc9736cSJeff Kirsher 	}
5088bcc9736cSJeff Kirsher 	info->next = next;
5089bcc9736cSJeff Kirsher 
5090bcc9736cSJeff Kirsher 	return received;
5091bcc9736cSJeff Kirsher }
5092bcc9736cSJeff Kirsher 
5093bcc9736cSJeff Kirsher static int port_rcv_packets(struct dev_info *hw_priv)
5094bcc9736cSJeff Kirsher {
5095bcc9736cSJeff Kirsher 	int next;
5096bcc9736cSJeff Kirsher 	union desc_stat status;
5097bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5098bcc9736cSJeff Kirsher 	struct net_device *dev = hw->port_info[0].pdev;
5099bcc9736cSJeff Kirsher 	struct ksz_desc_info *info = &hw->rx_desc_info;
5100bcc9736cSJeff Kirsher 	int left = info->alloc;
5101bcc9736cSJeff Kirsher 	struct ksz_desc *desc;
5102bcc9736cSJeff Kirsher 	int received = 0;
5103bcc9736cSJeff Kirsher 
5104bcc9736cSJeff Kirsher 	next = info->next;
5105bcc9736cSJeff Kirsher 	while (left--) {
5106bcc9736cSJeff Kirsher 		/* Get next descriptor which is not hardware owned. */
5107bcc9736cSJeff Kirsher 		desc = &info->ring[next];
5108bcc9736cSJeff Kirsher 		status.data = le32_to_cpu(desc->phw->ctrl.data);
5109bcc9736cSJeff Kirsher 		if (status.rx.hw_owned)
5110bcc9736cSJeff Kirsher 			break;
5111bcc9736cSJeff Kirsher 
5112bcc9736cSJeff Kirsher 		if (hw->dev_count > 1) {
5113bcc9736cSJeff Kirsher 			/* Get received port number. */
5114bcc9736cSJeff Kirsher 			int p = HW_TO_DEV_PORT(status.rx.src_port);
5115bcc9736cSJeff Kirsher 
5116bcc9736cSJeff Kirsher 			dev = hw->port_info[p].pdev;
5117bcc9736cSJeff Kirsher 			if (!netif_running(dev))
5118bcc9736cSJeff Kirsher 				goto release_packet;
5119bcc9736cSJeff Kirsher 		}
5120bcc9736cSJeff Kirsher 
5121bcc9736cSJeff Kirsher 		/* Status valid only when last descriptor bit is set. */
5122bcc9736cSJeff Kirsher 		if (status.rx.last_desc && status.rx.first_desc) {
5123bcc9736cSJeff Kirsher 			if (rx_proc(dev, hw, desc, status))
5124bcc9736cSJeff Kirsher 				goto release_packet;
5125bcc9736cSJeff Kirsher 			received++;
5126bcc9736cSJeff Kirsher 		}
5127bcc9736cSJeff Kirsher 
5128bcc9736cSJeff Kirsher release_packet:
5129bcc9736cSJeff Kirsher 		release_desc(desc);
5130bcc9736cSJeff Kirsher 		next++;
5131bcc9736cSJeff Kirsher 		next &= info->mask;
5132bcc9736cSJeff Kirsher 	}
5133bcc9736cSJeff Kirsher 	info->next = next;
5134bcc9736cSJeff Kirsher 
5135bcc9736cSJeff Kirsher 	return received;
5136bcc9736cSJeff Kirsher }
5137bcc9736cSJeff Kirsher 
5138bcc9736cSJeff Kirsher static int dev_rcv_special(struct dev_info *hw_priv)
5139bcc9736cSJeff Kirsher {
5140bcc9736cSJeff Kirsher 	int next;
5141bcc9736cSJeff Kirsher 	union desc_stat status;
5142bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5143bcc9736cSJeff Kirsher 	struct net_device *dev = hw->port_info[0].pdev;
5144bcc9736cSJeff Kirsher 	struct ksz_desc_info *info = &hw->rx_desc_info;
5145bcc9736cSJeff Kirsher 	int left = info->alloc;
5146bcc9736cSJeff Kirsher 	struct ksz_desc *desc;
5147bcc9736cSJeff Kirsher 	int received = 0;
5148bcc9736cSJeff Kirsher 
5149bcc9736cSJeff Kirsher 	next = info->next;
5150bcc9736cSJeff Kirsher 	while (left--) {
5151bcc9736cSJeff Kirsher 		/* Get next descriptor which is not hardware owned. */
5152bcc9736cSJeff Kirsher 		desc = &info->ring[next];
5153bcc9736cSJeff Kirsher 		status.data = le32_to_cpu(desc->phw->ctrl.data);
5154bcc9736cSJeff Kirsher 		if (status.rx.hw_owned)
5155bcc9736cSJeff Kirsher 			break;
5156bcc9736cSJeff Kirsher 
5157bcc9736cSJeff Kirsher 		if (hw->dev_count > 1) {
5158bcc9736cSJeff Kirsher 			/* Get received port number. */
5159bcc9736cSJeff Kirsher 			int p = HW_TO_DEV_PORT(status.rx.src_port);
5160bcc9736cSJeff Kirsher 
5161bcc9736cSJeff Kirsher 			dev = hw->port_info[p].pdev;
5162bcc9736cSJeff Kirsher 			if (!netif_running(dev))
5163bcc9736cSJeff Kirsher 				goto release_packet;
5164bcc9736cSJeff Kirsher 		}
5165bcc9736cSJeff Kirsher 
5166bcc9736cSJeff Kirsher 		/* Status valid only when last descriptor bit is set. */
5167bcc9736cSJeff Kirsher 		if (status.rx.last_desc && status.rx.first_desc) {
5168bcc9736cSJeff Kirsher 			/*
5169bcc9736cSJeff Kirsher 			 * Receive without error.  With receive errors
5170bcc9736cSJeff Kirsher 			 * disabled, packets with receive errors will be
5171bcc9736cSJeff Kirsher 			 * dropped, so no need to check the error bit.
5172bcc9736cSJeff Kirsher 			 */
5173bcc9736cSJeff Kirsher 			if (!status.rx.error || (status.data &
5174bcc9736cSJeff Kirsher 					KS_DESC_RX_ERROR_COND) ==
5175bcc9736cSJeff Kirsher 					KS_DESC_RX_ERROR_TOO_LONG) {
5176bcc9736cSJeff Kirsher 				if (rx_proc(dev, hw, desc, status))
5177bcc9736cSJeff Kirsher 					goto release_packet;
5178bcc9736cSJeff Kirsher 				received++;
5179bcc9736cSJeff Kirsher 			} else {
5180bcc9736cSJeff Kirsher 				struct dev_priv *priv = netdev_priv(dev);
5181bcc9736cSJeff Kirsher 
5182bcc9736cSJeff Kirsher 				/* Update receive error statistics. */
5183bcc9736cSJeff Kirsher 				priv->port.counter[OID_COUNTER_RCV_ERROR]++;
5184bcc9736cSJeff Kirsher 			}
5185bcc9736cSJeff Kirsher 		}
5186bcc9736cSJeff Kirsher 
5187bcc9736cSJeff Kirsher release_packet:
5188bcc9736cSJeff Kirsher 		release_desc(desc);
5189bcc9736cSJeff Kirsher 		next++;
5190bcc9736cSJeff Kirsher 		next &= info->mask;
5191bcc9736cSJeff Kirsher 	}
5192bcc9736cSJeff Kirsher 	info->next = next;
5193bcc9736cSJeff Kirsher 
5194bcc9736cSJeff Kirsher 	return received;
5195bcc9736cSJeff Kirsher }
5196bcc9736cSJeff Kirsher 
5197bcc9736cSJeff Kirsher static void rx_proc_task(unsigned long data)
5198bcc9736cSJeff Kirsher {
5199bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = (struct dev_info *) data;
5200bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5201bcc9736cSJeff Kirsher 
5202bcc9736cSJeff Kirsher 	if (!hw->enabled)
5203bcc9736cSJeff Kirsher 		return;
5204bcc9736cSJeff Kirsher 	if (unlikely(!hw_priv->dev_rcv(hw_priv))) {
5205bcc9736cSJeff Kirsher 
5206bcc9736cSJeff Kirsher 		/* In case receive process is suspended because of overrun. */
5207bcc9736cSJeff Kirsher 		hw_resume_rx(hw);
5208bcc9736cSJeff Kirsher 
5209bcc9736cSJeff Kirsher 		/* tasklets are interruptible. */
5210bcc9736cSJeff Kirsher 		spin_lock_irq(&hw_priv->hwlock);
5211bcc9736cSJeff Kirsher 		hw_turn_on_intr(hw, KS884X_INT_RX_MASK);
5212bcc9736cSJeff Kirsher 		spin_unlock_irq(&hw_priv->hwlock);
5213bcc9736cSJeff Kirsher 	} else {
5214bcc9736cSJeff Kirsher 		hw_ack_intr(hw, KS884X_INT_RX);
5215bcc9736cSJeff Kirsher 		tasklet_schedule(&hw_priv->rx_tasklet);
5216bcc9736cSJeff Kirsher 	}
5217bcc9736cSJeff Kirsher }
5218bcc9736cSJeff Kirsher 
5219bcc9736cSJeff Kirsher static void tx_proc_task(unsigned long data)
5220bcc9736cSJeff Kirsher {
5221bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = (struct dev_info *) data;
5222bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5223bcc9736cSJeff Kirsher 
5224bcc9736cSJeff Kirsher 	hw_ack_intr(hw, KS884X_INT_TX_MASK);
5225bcc9736cSJeff Kirsher 
5226bcc9736cSJeff Kirsher 	tx_done(hw_priv);
5227bcc9736cSJeff Kirsher 
5228bcc9736cSJeff Kirsher 	/* tasklets are interruptible. */
5229bcc9736cSJeff Kirsher 	spin_lock_irq(&hw_priv->hwlock);
5230bcc9736cSJeff Kirsher 	hw_turn_on_intr(hw, KS884X_INT_TX);
5231bcc9736cSJeff Kirsher 	spin_unlock_irq(&hw_priv->hwlock);
5232bcc9736cSJeff Kirsher }
5233bcc9736cSJeff Kirsher 
5234bcc9736cSJeff Kirsher static inline void handle_rx_stop(struct ksz_hw *hw)
5235bcc9736cSJeff Kirsher {
5236bcc9736cSJeff Kirsher 	/* Receive just has been stopped. */
5237bcc9736cSJeff Kirsher 	if (0 == hw->rx_stop)
5238bcc9736cSJeff Kirsher 		hw->intr_mask &= ~KS884X_INT_RX_STOPPED;
5239bcc9736cSJeff Kirsher 	else if (hw->rx_stop > 1) {
5240bcc9736cSJeff Kirsher 		if (hw->enabled && (hw->rx_cfg & DMA_RX_ENABLE)) {
5241bcc9736cSJeff Kirsher 			hw_start_rx(hw);
5242bcc9736cSJeff Kirsher 		} else {
5243bcc9736cSJeff Kirsher 			hw->intr_mask &= ~KS884X_INT_RX_STOPPED;
5244bcc9736cSJeff Kirsher 			hw->rx_stop = 0;
5245bcc9736cSJeff Kirsher 		}
5246bcc9736cSJeff Kirsher 	} else
5247bcc9736cSJeff Kirsher 		/* Receive just has been started. */
5248bcc9736cSJeff Kirsher 		hw->rx_stop++;
5249bcc9736cSJeff Kirsher }
5250bcc9736cSJeff Kirsher 
5251bcc9736cSJeff Kirsher /**
5252bcc9736cSJeff Kirsher  * netdev_intr - interrupt handling
5253bcc9736cSJeff Kirsher  * @irq:	Interrupt number.
5254bcc9736cSJeff Kirsher  * @dev_id:	Network device.
5255bcc9736cSJeff Kirsher  *
5256bcc9736cSJeff Kirsher  * This function is called by upper network layer to signal interrupt.
5257bcc9736cSJeff Kirsher  *
5258bcc9736cSJeff Kirsher  * Return IRQ_HANDLED if interrupt is handled.
5259bcc9736cSJeff Kirsher  */
5260bcc9736cSJeff Kirsher static irqreturn_t netdev_intr(int irq, void *dev_id)
5261bcc9736cSJeff Kirsher {
5262bcc9736cSJeff Kirsher 	uint int_enable = 0;
5263bcc9736cSJeff Kirsher 	struct net_device *dev = (struct net_device *) dev_id;
5264bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5265bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5266bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5267bcc9736cSJeff Kirsher 
5268bcc9736cSJeff Kirsher 	hw_read_intr(hw, &int_enable);
5269bcc9736cSJeff Kirsher 
5270bcc9736cSJeff Kirsher 	/* Not our interrupt! */
5271bcc9736cSJeff Kirsher 	if (!int_enable)
5272bcc9736cSJeff Kirsher 		return IRQ_NONE;
5273bcc9736cSJeff Kirsher 
5274bcc9736cSJeff Kirsher 	do {
5275bcc9736cSJeff Kirsher 		hw_ack_intr(hw, int_enable);
5276bcc9736cSJeff Kirsher 		int_enable &= hw->intr_mask;
5277bcc9736cSJeff Kirsher 
5278bcc9736cSJeff Kirsher 		if (unlikely(int_enable & KS884X_INT_TX_MASK)) {
5279bcc9736cSJeff Kirsher 			hw_dis_intr_bit(hw, KS884X_INT_TX_MASK);
5280bcc9736cSJeff Kirsher 			tasklet_schedule(&hw_priv->tx_tasklet);
5281bcc9736cSJeff Kirsher 		}
5282bcc9736cSJeff Kirsher 
5283bcc9736cSJeff Kirsher 		if (likely(int_enable & KS884X_INT_RX)) {
5284bcc9736cSJeff Kirsher 			hw_dis_intr_bit(hw, KS884X_INT_RX);
5285bcc9736cSJeff Kirsher 			tasklet_schedule(&hw_priv->rx_tasklet);
5286bcc9736cSJeff Kirsher 		}
5287bcc9736cSJeff Kirsher 
5288bcc9736cSJeff Kirsher 		if (unlikely(int_enable & KS884X_INT_RX_OVERRUN)) {
5289bcc9736cSJeff Kirsher 			dev->stats.rx_fifo_errors++;
5290bcc9736cSJeff Kirsher 			hw_resume_rx(hw);
5291bcc9736cSJeff Kirsher 		}
5292bcc9736cSJeff Kirsher 
5293bcc9736cSJeff Kirsher 		if (unlikely(int_enable & KS884X_INT_PHY)) {
5294bcc9736cSJeff Kirsher 			struct ksz_port *port = &priv->port;
5295bcc9736cSJeff Kirsher 
5296bcc9736cSJeff Kirsher 			hw->features |= LINK_INT_WORKING;
5297bcc9736cSJeff Kirsher 			port_get_link_speed(port);
5298bcc9736cSJeff Kirsher 		}
5299bcc9736cSJeff Kirsher 
5300bcc9736cSJeff Kirsher 		if (unlikely(int_enable & KS884X_INT_RX_STOPPED)) {
5301bcc9736cSJeff Kirsher 			handle_rx_stop(hw);
5302bcc9736cSJeff Kirsher 			break;
5303bcc9736cSJeff Kirsher 		}
5304bcc9736cSJeff Kirsher 
5305bcc9736cSJeff Kirsher 		if (unlikely(int_enable & KS884X_INT_TX_STOPPED)) {
5306bcc9736cSJeff Kirsher 			u32 data;
5307bcc9736cSJeff Kirsher 
5308bcc9736cSJeff Kirsher 			hw->intr_mask &= ~KS884X_INT_TX_STOPPED;
5309bcc9736cSJeff Kirsher 			pr_info("Tx stopped\n");
5310bcc9736cSJeff Kirsher 			data = readl(hw->io + KS_DMA_TX_CTRL);
5311bcc9736cSJeff Kirsher 			if (!(data & DMA_TX_ENABLE))
5312bcc9736cSJeff Kirsher 				pr_info("Tx disabled\n");
5313bcc9736cSJeff Kirsher 			break;
5314bcc9736cSJeff Kirsher 		}
5315bcc9736cSJeff Kirsher 	} while (0);
5316bcc9736cSJeff Kirsher 
5317bcc9736cSJeff Kirsher 	hw_ena_intr(hw);
5318bcc9736cSJeff Kirsher 
5319bcc9736cSJeff Kirsher 	return IRQ_HANDLED;
5320bcc9736cSJeff Kirsher }
5321bcc9736cSJeff Kirsher 
5322bcc9736cSJeff Kirsher /*
5323bcc9736cSJeff Kirsher  * Linux network device functions
5324bcc9736cSJeff Kirsher  */
5325bcc9736cSJeff Kirsher 
5326bcc9736cSJeff Kirsher static unsigned long next_jiffies;
5327bcc9736cSJeff Kirsher 
5328bcc9736cSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
5329bcc9736cSJeff Kirsher static void netdev_netpoll(struct net_device *dev)
5330bcc9736cSJeff Kirsher {
5331bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5332bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5333bcc9736cSJeff Kirsher 
5334bcc9736cSJeff Kirsher 	hw_dis_intr(&hw_priv->hw);
5335bcc9736cSJeff Kirsher 	netdev_intr(dev->irq, dev);
5336bcc9736cSJeff Kirsher }
5337bcc9736cSJeff Kirsher #endif
5338bcc9736cSJeff Kirsher 
5339bcc9736cSJeff Kirsher static void bridge_change(struct ksz_hw *hw)
5340bcc9736cSJeff Kirsher {
5341bcc9736cSJeff Kirsher 	int port;
5342bcc9736cSJeff Kirsher 	u8  member;
5343bcc9736cSJeff Kirsher 	struct ksz_switch *sw = hw->ksz_switch;
5344bcc9736cSJeff Kirsher 
5345bcc9736cSJeff Kirsher 	/* No ports in forwarding state. */
5346bcc9736cSJeff Kirsher 	if (!sw->member) {
5347bcc9736cSJeff Kirsher 		port_set_stp_state(hw, SWITCH_PORT_NUM, STP_STATE_SIMPLE);
5348bcc9736cSJeff Kirsher 		sw_block_addr(hw);
5349bcc9736cSJeff Kirsher 	}
5350bcc9736cSJeff Kirsher 	for (port = 0; port < SWITCH_PORT_NUM; port++) {
5351bcc9736cSJeff Kirsher 		if (STP_STATE_FORWARDING == sw->port_cfg[port].stp_state)
5352bcc9736cSJeff Kirsher 			member = HOST_MASK | sw->member;
5353bcc9736cSJeff Kirsher 		else
5354bcc9736cSJeff Kirsher 			member = HOST_MASK | (1 << port);
5355bcc9736cSJeff Kirsher 		if (member != sw->port_cfg[port].member)
5356bcc9736cSJeff Kirsher 			sw_cfg_port_base_vlan(hw, port, member);
5357bcc9736cSJeff Kirsher 	}
5358bcc9736cSJeff Kirsher }
5359bcc9736cSJeff Kirsher 
5360bcc9736cSJeff Kirsher /**
5361bcc9736cSJeff Kirsher  * netdev_close - close network device
5362bcc9736cSJeff Kirsher  * @dev:	Network device.
5363bcc9736cSJeff Kirsher  *
5364bcc9736cSJeff Kirsher  * This function process the close operation of network device.  This is caused
5365bcc9736cSJeff Kirsher  * by the user command "ifconfig ethX down."
5366bcc9736cSJeff Kirsher  *
5367bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code indicating failure.
5368bcc9736cSJeff Kirsher  */
5369bcc9736cSJeff Kirsher static int netdev_close(struct net_device *dev)
5370bcc9736cSJeff Kirsher {
5371bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5372bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5373bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
5374bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5375bcc9736cSJeff Kirsher 	int pi;
5376bcc9736cSJeff Kirsher 
5377bcc9736cSJeff Kirsher 	netif_stop_queue(dev);
5378bcc9736cSJeff Kirsher 
5379bcc9736cSJeff Kirsher 	ksz_stop_timer(&priv->monitor_timer_info);
5380bcc9736cSJeff Kirsher 
5381bcc9736cSJeff Kirsher 	/* Need to shut the port manually in multiple device interfaces mode. */
5382bcc9736cSJeff Kirsher 	if (hw->dev_count > 1) {
5383bcc9736cSJeff Kirsher 		port_set_stp_state(hw, port->first_port, STP_STATE_DISABLED);
5384bcc9736cSJeff Kirsher 
5385bcc9736cSJeff Kirsher 		/* Port is closed.  Need to change bridge setting. */
5386bcc9736cSJeff Kirsher 		if (hw->features & STP_SUPPORT) {
5387bcc9736cSJeff Kirsher 			pi = 1 << port->first_port;
5388bcc9736cSJeff Kirsher 			if (hw->ksz_switch->member & pi) {
5389bcc9736cSJeff Kirsher 				hw->ksz_switch->member &= ~pi;
5390bcc9736cSJeff Kirsher 				bridge_change(hw);
5391bcc9736cSJeff Kirsher 			}
5392bcc9736cSJeff Kirsher 		}
5393bcc9736cSJeff Kirsher 	}
5394bcc9736cSJeff Kirsher 	if (port->first_port > 0)
5395bcc9736cSJeff Kirsher 		hw_del_addr(hw, dev->dev_addr);
5396bcc9736cSJeff Kirsher 	if (!hw_priv->wol_enable)
5397bcc9736cSJeff Kirsher 		port_set_power_saving(port, true);
5398bcc9736cSJeff Kirsher 
5399bcc9736cSJeff Kirsher 	if (priv->multicast)
5400bcc9736cSJeff Kirsher 		--hw->all_multi;
5401bcc9736cSJeff Kirsher 	if (priv->promiscuous)
5402bcc9736cSJeff Kirsher 		--hw->promiscuous;
5403bcc9736cSJeff Kirsher 
5404bcc9736cSJeff Kirsher 	hw_priv->opened--;
5405bcc9736cSJeff Kirsher 	if (!(hw_priv->opened)) {
5406bcc9736cSJeff Kirsher 		ksz_stop_timer(&hw_priv->mib_timer_info);
5407bcc9736cSJeff Kirsher 		flush_work(&hw_priv->mib_read);
5408bcc9736cSJeff Kirsher 
5409bcc9736cSJeff Kirsher 		hw_dis_intr(hw);
5410bcc9736cSJeff Kirsher 		hw_disable(hw);
5411bcc9736cSJeff Kirsher 		hw_clr_multicast(hw);
5412bcc9736cSJeff Kirsher 
5413bcc9736cSJeff Kirsher 		/* Delay for receive task to stop scheduling itself. */
5414bcc9736cSJeff Kirsher 		msleep(2000 / HZ);
5415bcc9736cSJeff Kirsher 
5416bcc9736cSJeff Kirsher 		tasklet_disable(&hw_priv->rx_tasklet);
5417bcc9736cSJeff Kirsher 		tasklet_disable(&hw_priv->tx_tasklet);
5418bcc9736cSJeff Kirsher 		free_irq(dev->irq, hw_priv->dev);
5419bcc9736cSJeff Kirsher 
5420bcc9736cSJeff Kirsher 		transmit_cleanup(hw_priv, 0);
5421bcc9736cSJeff Kirsher 		hw_reset_pkts(&hw->rx_desc_info);
5422bcc9736cSJeff Kirsher 		hw_reset_pkts(&hw->tx_desc_info);
5423bcc9736cSJeff Kirsher 
5424bcc9736cSJeff Kirsher 		/* Clean out static MAC table when the switch is shutdown. */
5425bcc9736cSJeff Kirsher 		if (hw->features & STP_SUPPORT)
5426bcc9736cSJeff Kirsher 			sw_clr_sta_mac_table(hw);
5427bcc9736cSJeff Kirsher 	}
5428bcc9736cSJeff Kirsher 
5429bcc9736cSJeff Kirsher 	return 0;
5430bcc9736cSJeff Kirsher }
5431bcc9736cSJeff Kirsher 
5432bcc9736cSJeff Kirsher static void hw_cfg_huge_frame(struct dev_info *hw_priv, struct ksz_hw *hw)
5433bcc9736cSJeff Kirsher {
5434bcc9736cSJeff Kirsher 	if (hw->ksz_switch) {
5435bcc9736cSJeff Kirsher 		u32 data;
5436bcc9736cSJeff Kirsher 
5437bcc9736cSJeff Kirsher 		data = readw(hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
5438bcc9736cSJeff Kirsher 		if (hw->features & RX_HUGE_FRAME)
5439bcc9736cSJeff Kirsher 			data |= SWITCH_HUGE_PACKET;
5440bcc9736cSJeff Kirsher 		else
5441bcc9736cSJeff Kirsher 			data &= ~SWITCH_HUGE_PACKET;
5442bcc9736cSJeff Kirsher 		writew(data, hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
5443bcc9736cSJeff Kirsher 	}
5444bcc9736cSJeff Kirsher 	if (hw->features & RX_HUGE_FRAME) {
5445bcc9736cSJeff Kirsher 		hw->rx_cfg |= DMA_RX_ERROR;
5446bcc9736cSJeff Kirsher 		hw_priv->dev_rcv = dev_rcv_special;
5447bcc9736cSJeff Kirsher 	} else {
5448bcc9736cSJeff Kirsher 		hw->rx_cfg &= ~DMA_RX_ERROR;
5449bcc9736cSJeff Kirsher 		if (hw->dev_count > 1)
5450bcc9736cSJeff Kirsher 			hw_priv->dev_rcv = port_rcv_packets;
5451bcc9736cSJeff Kirsher 		else
5452bcc9736cSJeff Kirsher 			hw_priv->dev_rcv = dev_rcv_packets;
5453bcc9736cSJeff Kirsher 	}
5454bcc9736cSJeff Kirsher }
5455bcc9736cSJeff Kirsher 
5456bcc9736cSJeff Kirsher static int prepare_hardware(struct net_device *dev)
5457bcc9736cSJeff Kirsher {
5458bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5459bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5460bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5461bcc9736cSJeff Kirsher 	int rc = 0;
5462bcc9736cSJeff Kirsher 
5463bcc9736cSJeff Kirsher 	/* Remember the network device that requests interrupts. */
5464bcc9736cSJeff Kirsher 	hw_priv->dev = dev;
5465bcc9736cSJeff Kirsher 	rc = request_irq(dev->irq, netdev_intr, IRQF_SHARED, dev->name, dev);
5466bcc9736cSJeff Kirsher 	if (rc)
5467bcc9736cSJeff Kirsher 		return rc;
5468bcc9736cSJeff Kirsher 	tasklet_enable(&hw_priv->rx_tasklet);
5469bcc9736cSJeff Kirsher 	tasklet_enable(&hw_priv->tx_tasklet);
5470bcc9736cSJeff Kirsher 
5471bcc9736cSJeff Kirsher 	hw->promiscuous = 0;
5472bcc9736cSJeff Kirsher 	hw->all_multi = 0;
5473bcc9736cSJeff Kirsher 	hw->multi_list_size = 0;
5474bcc9736cSJeff Kirsher 
5475bcc9736cSJeff Kirsher 	hw_reset(hw);
5476bcc9736cSJeff Kirsher 
5477bcc9736cSJeff Kirsher 	hw_set_desc_base(hw,
5478bcc9736cSJeff Kirsher 		hw->tx_desc_info.ring_phys, hw->rx_desc_info.ring_phys);
5479bcc9736cSJeff Kirsher 	hw_set_addr(hw);
5480bcc9736cSJeff Kirsher 	hw_cfg_huge_frame(hw_priv, hw);
5481bcc9736cSJeff Kirsher 	ksz_init_rx_buffers(hw_priv);
5482bcc9736cSJeff Kirsher 	return 0;
5483bcc9736cSJeff Kirsher }
5484bcc9736cSJeff Kirsher 
5485bcc9736cSJeff Kirsher static void set_media_state(struct net_device *dev, int media_state)
5486bcc9736cSJeff Kirsher {
5487bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5488bcc9736cSJeff Kirsher 
5489bcc9736cSJeff Kirsher 	if (media_state == priv->media_state)
5490bcc9736cSJeff Kirsher 		netif_carrier_on(dev);
5491bcc9736cSJeff Kirsher 	else
5492bcc9736cSJeff Kirsher 		netif_carrier_off(dev);
5493bcc9736cSJeff Kirsher 	netif_info(priv, link, dev, "link %s\n",
5494bcc9736cSJeff Kirsher 		   media_state == priv->media_state ? "on" : "off");
5495bcc9736cSJeff Kirsher }
5496bcc9736cSJeff Kirsher 
5497bcc9736cSJeff Kirsher /**
5498bcc9736cSJeff Kirsher  * netdev_open - open network device
5499bcc9736cSJeff Kirsher  * @dev:	Network device.
5500bcc9736cSJeff Kirsher  *
5501bcc9736cSJeff Kirsher  * This function process the open operation of network device.  This is caused
5502bcc9736cSJeff Kirsher  * by the user command "ifconfig ethX up."
5503bcc9736cSJeff Kirsher  *
5504bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code indicating failure.
5505bcc9736cSJeff Kirsher  */
5506bcc9736cSJeff Kirsher static int netdev_open(struct net_device *dev)
5507bcc9736cSJeff Kirsher {
5508bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5509bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5510bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5511bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
5512bcc9736cSJeff Kirsher 	int i;
5513bcc9736cSJeff Kirsher 	int p;
5514bcc9736cSJeff Kirsher 	int rc = 0;
5515bcc9736cSJeff Kirsher 
5516bcc9736cSJeff Kirsher 	priv->multicast = 0;
5517bcc9736cSJeff Kirsher 	priv->promiscuous = 0;
5518bcc9736cSJeff Kirsher 
5519bcc9736cSJeff Kirsher 	/* Reset device statistics. */
5520bcc9736cSJeff Kirsher 	memset(&dev->stats, 0, sizeof(struct net_device_stats));
5521bcc9736cSJeff Kirsher 	memset((void *) port->counter, 0,
5522bcc9736cSJeff Kirsher 		(sizeof(u64) * OID_COUNTER_LAST));
5523bcc9736cSJeff Kirsher 
5524bcc9736cSJeff Kirsher 	if (!(hw_priv->opened)) {
5525bcc9736cSJeff Kirsher 		rc = prepare_hardware(dev);
5526bcc9736cSJeff Kirsher 		if (rc)
5527bcc9736cSJeff Kirsher 			return rc;
5528bcc9736cSJeff Kirsher 		for (i = 0; i < hw->mib_port_cnt; i++) {
5529bcc9736cSJeff Kirsher 			if (next_jiffies < jiffies)
5530bcc9736cSJeff Kirsher 				next_jiffies = jiffies + HZ * 2;
5531bcc9736cSJeff Kirsher 			else
5532bcc9736cSJeff Kirsher 				next_jiffies += HZ * 1;
5533bcc9736cSJeff Kirsher 			hw_priv->counter[i].time = next_jiffies;
5534bcc9736cSJeff Kirsher 			hw->port_mib[i].state = media_disconnected;
5535bcc9736cSJeff Kirsher 			port_init_cnt(hw, i);
5536bcc9736cSJeff Kirsher 		}
5537bcc9736cSJeff Kirsher 		if (hw->ksz_switch)
5538bcc9736cSJeff Kirsher 			hw->port_mib[HOST_PORT].state = media_connected;
5539bcc9736cSJeff Kirsher 		else {
5540bcc9736cSJeff Kirsher 			hw_add_wol_bcast(hw);
5541bcc9736cSJeff Kirsher 			hw_cfg_wol_pme(hw, 0);
5542bcc9736cSJeff Kirsher 			hw_clr_wol_pme_status(&hw_priv->hw);
5543bcc9736cSJeff Kirsher 		}
5544bcc9736cSJeff Kirsher 	}
5545bcc9736cSJeff Kirsher 	port_set_power_saving(port, false);
5546bcc9736cSJeff Kirsher 
5547bcc9736cSJeff Kirsher 	for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
5548bcc9736cSJeff Kirsher 		/*
5549bcc9736cSJeff Kirsher 		 * Initialize to invalid value so that link detection
5550bcc9736cSJeff Kirsher 		 * is done.
5551bcc9736cSJeff Kirsher 		 */
5552bcc9736cSJeff Kirsher 		hw->port_info[p].partner = 0xFF;
5553bcc9736cSJeff Kirsher 		hw->port_info[p].state = media_disconnected;
5554bcc9736cSJeff Kirsher 	}
5555bcc9736cSJeff Kirsher 
5556bcc9736cSJeff Kirsher 	/* Need to open the port in multiple device interfaces mode. */
5557bcc9736cSJeff Kirsher 	if (hw->dev_count > 1) {
5558bcc9736cSJeff Kirsher 		port_set_stp_state(hw, port->first_port, STP_STATE_SIMPLE);
5559bcc9736cSJeff Kirsher 		if (port->first_port > 0)
5560bcc9736cSJeff Kirsher 			hw_add_addr(hw, dev->dev_addr);
5561bcc9736cSJeff Kirsher 	}
5562bcc9736cSJeff Kirsher 
5563bcc9736cSJeff Kirsher 	port_get_link_speed(port);
5564bcc9736cSJeff Kirsher 	if (port->force_link)
5565bcc9736cSJeff Kirsher 		port_force_link_speed(port);
5566bcc9736cSJeff Kirsher 	else
5567bcc9736cSJeff Kirsher 		port_set_link_speed(port);
5568bcc9736cSJeff Kirsher 
5569bcc9736cSJeff Kirsher 	if (!(hw_priv->opened)) {
5570bcc9736cSJeff Kirsher 		hw_setup_intr(hw);
5571bcc9736cSJeff Kirsher 		hw_enable(hw);
5572bcc9736cSJeff Kirsher 		hw_ena_intr(hw);
5573bcc9736cSJeff Kirsher 
5574bcc9736cSJeff Kirsher 		if (hw->mib_port_cnt)
5575bcc9736cSJeff Kirsher 			ksz_start_timer(&hw_priv->mib_timer_info,
5576bcc9736cSJeff Kirsher 				hw_priv->mib_timer_info.period);
5577bcc9736cSJeff Kirsher 	}
5578bcc9736cSJeff Kirsher 
5579bcc9736cSJeff Kirsher 	hw_priv->opened++;
5580bcc9736cSJeff Kirsher 
5581bcc9736cSJeff Kirsher 	ksz_start_timer(&priv->monitor_timer_info,
5582bcc9736cSJeff Kirsher 		priv->monitor_timer_info.period);
5583bcc9736cSJeff Kirsher 
5584bcc9736cSJeff Kirsher 	priv->media_state = port->linked->state;
5585bcc9736cSJeff Kirsher 
5586bcc9736cSJeff Kirsher 	set_media_state(dev, media_connected);
5587bcc9736cSJeff Kirsher 	netif_start_queue(dev);
5588bcc9736cSJeff Kirsher 
5589bcc9736cSJeff Kirsher 	return 0;
5590bcc9736cSJeff Kirsher }
5591bcc9736cSJeff Kirsher 
5592bcc9736cSJeff Kirsher /* RX errors = rx_errors */
5593bcc9736cSJeff Kirsher /* RX dropped = rx_dropped */
5594bcc9736cSJeff Kirsher /* RX overruns = rx_fifo_errors */
5595bcc9736cSJeff Kirsher /* RX frame = rx_crc_errors + rx_frame_errors + rx_length_errors */
5596bcc9736cSJeff Kirsher /* TX errors = tx_errors */
5597bcc9736cSJeff Kirsher /* TX dropped = tx_dropped */
5598bcc9736cSJeff Kirsher /* TX overruns = tx_fifo_errors */
5599bcc9736cSJeff Kirsher /* TX carrier = tx_aborted_errors + tx_carrier_errors + tx_window_errors */
5600bcc9736cSJeff Kirsher /* collisions = collisions */
5601bcc9736cSJeff Kirsher 
5602bcc9736cSJeff Kirsher /**
5603bcc9736cSJeff Kirsher  * netdev_query_statistics - query network device statistics
5604bcc9736cSJeff Kirsher  * @dev:	Network device.
5605bcc9736cSJeff Kirsher  *
5606bcc9736cSJeff Kirsher  * This function returns the statistics of the network device.  The device
5607bcc9736cSJeff Kirsher  * needs not be opened.
5608bcc9736cSJeff Kirsher  *
5609bcc9736cSJeff Kirsher  * Return network device statistics.
5610bcc9736cSJeff Kirsher  */
5611bcc9736cSJeff Kirsher static struct net_device_stats *netdev_query_statistics(struct net_device *dev)
5612bcc9736cSJeff Kirsher {
5613bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5614bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
5615bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &priv->adapter->hw;
5616bcc9736cSJeff Kirsher 	struct ksz_port_mib *mib;
5617bcc9736cSJeff Kirsher 	int i;
5618bcc9736cSJeff Kirsher 	int p;
5619bcc9736cSJeff Kirsher 
5620bcc9736cSJeff Kirsher 	dev->stats.rx_errors = port->counter[OID_COUNTER_RCV_ERROR];
5621bcc9736cSJeff Kirsher 	dev->stats.tx_errors = port->counter[OID_COUNTER_XMIT_ERROR];
5622bcc9736cSJeff Kirsher 
5623bcc9736cSJeff Kirsher 	/* Reset to zero to add count later. */
5624bcc9736cSJeff Kirsher 	dev->stats.multicast = 0;
5625bcc9736cSJeff Kirsher 	dev->stats.collisions = 0;
5626bcc9736cSJeff Kirsher 	dev->stats.rx_length_errors = 0;
5627bcc9736cSJeff Kirsher 	dev->stats.rx_crc_errors = 0;
5628bcc9736cSJeff Kirsher 	dev->stats.rx_frame_errors = 0;
5629bcc9736cSJeff Kirsher 	dev->stats.tx_window_errors = 0;
5630bcc9736cSJeff Kirsher 
5631bcc9736cSJeff Kirsher 	for (i = 0, p = port->first_port; i < port->mib_port_cnt; i++, p++) {
5632bcc9736cSJeff Kirsher 		mib = &hw->port_mib[p];
5633bcc9736cSJeff Kirsher 
5634bcc9736cSJeff Kirsher 		dev->stats.multicast += (unsigned long)
5635bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_RX_MULTICAST];
5636bcc9736cSJeff Kirsher 
5637bcc9736cSJeff Kirsher 		dev->stats.collisions += (unsigned long)
5638bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_TX_TOTAL_COLLISION];
5639bcc9736cSJeff Kirsher 
5640bcc9736cSJeff Kirsher 		dev->stats.rx_length_errors += (unsigned long)(
5641bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_RX_UNDERSIZE] +
5642bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_RX_FRAGMENT] +
5643bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_RX_OVERSIZE] +
5644bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_RX_JABBER]);
5645bcc9736cSJeff Kirsher 		dev->stats.rx_crc_errors += (unsigned long)
5646bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_RX_CRC_ERR];
5647bcc9736cSJeff Kirsher 		dev->stats.rx_frame_errors += (unsigned long)(
5648bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_RX_ALIGNMENT_ERR] +
5649bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_RX_SYMBOL_ERR]);
5650bcc9736cSJeff Kirsher 
5651bcc9736cSJeff Kirsher 		dev->stats.tx_window_errors += (unsigned long)
5652bcc9736cSJeff Kirsher 			mib->counter[MIB_COUNTER_TX_LATE_COLLISION];
5653bcc9736cSJeff Kirsher 	}
5654bcc9736cSJeff Kirsher 
5655bcc9736cSJeff Kirsher 	return &dev->stats;
5656bcc9736cSJeff Kirsher }
5657bcc9736cSJeff Kirsher 
5658bcc9736cSJeff Kirsher /**
5659bcc9736cSJeff Kirsher  * netdev_set_mac_address - set network device MAC address
5660bcc9736cSJeff Kirsher  * @dev:	Network device.
5661bcc9736cSJeff Kirsher  * @addr:	Buffer of MAC address.
5662bcc9736cSJeff Kirsher  *
5663bcc9736cSJeff Kirsher  * This function is used to set the MAC address of the network device.
5664bcc9736cSJeff Kirsher  *
5665bcc9736cSJeff Kirsher  * Return 0 to indicate success.
5666bcc9736cSJeff Kirsher  */
5667bcc9736cSJeff Kirsher static int netdev_set_mac_address(struct net_device *dev, void *addr)
5668bcc9736cSJeff Kirsher {
5669bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5670bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5671bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5672bcc9736cSJeff Kirsher 	struct sockaddr *mac = addr;
5673bcc9736cSJeff Kirsher 	uint interrupt;
5674bcc9736cSJeff Kirsher 
5675bcc9736cSJeff Kirsher 	if (priv->port.first_port > 0)
5676bcc9736cSJeff Kirsher 		hw_del_addr(hw, dev->dev_addr);
5677bcc9736cSJeff Kirsher 	else {
5678bcc9736cSJeff Kirsher 		hw->mac_override = 1;
5679bcc9736cSJeff Kirsher 		memcpy(hw->override_addr, mac->sa_data, MAC_ADDR_LEN);
5680bcc9736cSJeff Kirsher 	}
5681bcc9736cSJeff Kirsher 
5682bcc9736cSJeff Kirsher 	memcpy(dev->dev_addr, mac->sa_data, MAX_ADDR_LEN);
5683bcc9736cSJeff Kirsher 
5684bcc9736cSJeff Kirsher 	interrupt = hw_block_intr(hw);
5685bcc9736cSJeff Kirsher 
5686bcc9736cSJeff Kirsher 	if (priv->port.first_port > 0)
5687bcc9736cSJeff Kirsher 		hw_add_addr(hw, dev->dev_addr);
5688bcc9736cSJeff Kirsher 	else
5689bcc9736cSJeff Kirsher 		hw_set_addr(hw);
5690bcc9736cSJeff Kirsher 	hw_restore_intr(hw, interrupt);
5691bcc9736cSJeff Kirsher 
5692bcc9736cSJeff Kirsher 	return 0;
5693bcc9736cSJeff Kirsher }
5694bcc9736cSJeff Kirsher 
5695bcc9736cSJeff Kirsher static void dev_set_promiscuous(struct net_device *dev, struct dev_priv *priv,
5696bcc9736cSJeff Kirsher 	struct ksz_hw *hw, int promiscuous)
5697bcc9736cSJeff Kirsher {
5698bcc9736cSJeff Kirsher 	if (promiscuous != priv->promiscuous) {
5699bcc9736cSJeff Kirsher 		u8 prev_state = hw->promiscuous;
5700bcc9736cSJeff Kirsher 
5701bcc9736cSJeff Kirsher 		if (promiscuous)
5702bcc9736cSJeff Kirsher 			++hw->promiscuous;
5703bcc9736cSJeff Kirsher 		else
5704bcc9736cSJeff Kirsher 			--hw->promiscuous;
5705bcc9736cSJeff Kirsher 		priv->promiscuous = promiscuous;
5706bcc9736cSJeff Kirsher 
5707bcc9736cSJeff Kirsher 		/* Turn on/off promiscuous mode. */
5708bcc9736cSJeff Kirsher 		if (hw->promiscuous <= 1 && prev_state <= 1)
5709bcc9736cSJeff Kirsher 			hw_set_promiscuous(hw, hw->promiscuous);
5710bcc9736cSJeff Kirsher 
5711bcc9736cSJeff Kirsher 		/*
5712bcc9736cSJeff Kirsher 		 * Port is not in promiscuous mode, meaning it is released
5713bcc9736cSJeff Kirsher 		 * from the bridge.
5714bcc9736cSJeff Kirsher 		 */
5715bcc9736cSJeff Kirsher 		if ((hw->features & STP_SUPPORT) && !promiscuous &&
5716bcc9736cSJeff Kirsher 		    (dev->priv_flags & IFF_BRIDGE_PORT)) {
5717bcc9736cSJeff Kirsher 			struct ksz_switch *sw = hw->ksz_switch;
5718bcc9736cSJeff Kirsher 			int port = priv->port.first_port;
5719bcc9736cSJeff Kirsher 
5720bcc9736cSJeff Kirsher 			port_set_stp_state(hw, port, STP_STATE_DISABLED);
5721bcc9736cSJeff Kirsher 			port = 1 << port;
5722bcc9736cSJeff Kirsher 			if (sw->member & port) {
5723bcc9736cSJeff Kirsher 				sw->member &= ~port;
5724bcc9736cSJeff Kirsher 				bridge_change(hw);
5725bcc9736cSJeff Kirsher 			}
5726bcc9736cSJeff Kirsher 		}
5727bcc9736cSJeff Kirsher 	}
5728bcc9736cSJeff Kirsher }
5729bcc9736cSJeff Kirsher 
5730bcc9736cSJeff Kirsher static void dev_set_multicast(struct dev_priv *priv, struct ksz_hw *hw,
5731bcc9736cSJeff Kirsher 	int multicast)
5732bcc9736cSJeff Kirsher {
5733bcc9736cSJeff Kirsher 	if (multicast != priv->multicast) {
5734bcc9736cSJeff Kirsher 		u8 all_multi = hw->all_multi;
5735bcc9736cSJeff Kirsher 
5736bcc9736cSJeff Kirsher 		if (multicast)
5737bcc9736cSJeff Kirsher 			++hw->all_multi;
5738bcc9736cSJeff Kirsher 		else
5739bcc9736cSJeff Kirsher 			--hw->all_multi;
5740bcc9736cSJeff Kirsher 		priv->multicast = multicast;
5741bcc9736cSJeff Kirsher 
5742bcc9736cSJeff Kirsher 		/* Turn on/off all multicast mode. */
5743bcc9736cSJeff Kirsher 		if (hw->all_multi <= 1 && all_multi <= 1)
5744bcc9736cSJeff Kirsher 			hw_set_multicast(hw, hw->all_multi);
5745bcc9736cSJeff Kirsher 	}
5746bcc9736cSJeff Kirsher }
5747bcc9736cSJeff Kirsher 
5748bcc9736cSJeff Kirsher /**
5749bcc9736cSJeff Kirsher  * netdev_set_rx_mode
5750bcc9736cSJeff Kirsher  * @dev:	Network device.
5751bcc9736cSJeff Kirsher  *
5752bcc9736cSJeff Kirsher  * This routine is used to set multicast addresses or put the network device
5753bcc9736cSJeff Kirsher  * into promiscuous mode.
5754bcc9736cSJeff Kirsher  */
5755bcc9736cSJeff Kirsher static void netdev_set_rx_mode(struct net_device *dev)
5756bcc9736cSJeff Kirsher {
5757bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5758bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5759bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5760bcc9736cSJeff Kirsher 	struct netdev_hw_addr *ha;
5761bcc9736cSJeff Kirsher 	int multicast = (dev->flags & IFF_ALLMULTI);
5762bcc9736cSJeff Kirsher 
5763bcc9736cSJeff Kirsher 	dev_set_promiscuous(dev, priv, hw, (dev->flags & IFF_PROMISC));
5764bcc9736cSJeff Kirsher 
5765bcc9736cSJeff Kirsher 	if (hw_priv->hw.dev_count > 1)
5766bcc9736cSJeff Kirsher 		multicast |= (dev->flags & IFF_MULTICAST);
5767bcc9736cSJeff Kirsher 	dev_set_multicast(priv, hw, multicast);
5768bcc9736cSJeff Kirsher 
5769bcc9736cSJeff Kirsher 	/* Cannot use different hashes in multiple device interfaces mode. */
5770bcc9736cSJeff Kirsher 	if (hw_priv->hw.dev_count > 1)
5771bcc9736cSJeff Kirsher 		return;
5772bcc9736cSJeff Kirsher 
5773bcc9736cSJeff Kirsher 	if ((dev->flags & IFF_MULTICAST) && !netdev_mc_empty(dev)) {
5774bcc9736cSJeff Kirsher 		int i = 0;
5775bcc9736cSJeff Kirsher 
5776bcc9736cSJeff Kirsher 		/* List too big to support so turn on all multicast mode. */
5777bcc9736cSJeff Kirsher 		if (netdev_mc_count(dev) > MAX_MULTICAST_LIST) {
5778bcc9736cSJeff Kirsher 			if (MAX_MULTICAST_LIST != hw->multi_list_size) {
5779bcc9736cSJeff Kirsher 				hw->multi_list_size = MAX_MULTICAST_LIST;
5780bcc9736cSJeff Kirsher 				++hw->all_multi;
5781bcc9736cSJeff Kirsher 				hw_set_multicast(hw, hw->all_multi);
5782bcc9736cSJeff Kirsher 			}
5783bcc9736cSJeff Kirsher 			return;
5784bcc9736cSJeff Kirsher 		}
5785bcc9736cSJeff Kirsher 
5786bcc9736cSJeff Kirsher 		netdev_for_each_mc_addr(ha, dev) {
5787bcc9736cSJeff Kirsher 			if (i >= MAX_MULTICAST_LIST)
5788bcc9736cSJeff Kirsher 				break;
5789bcc9736cSJeff Kirsher 			memcpy(hw->multi_list[i++], ha->addr, MAC_ADDR_LEN);
5790bcc9736cSJeff Kirsher 		}
5791bcc9736cSJeff Kirsher 		hw->multi_list_size = (u8) i;
5792bcc9736cSJeff Kirsher 		hw_set_grp_addr(hw);
5793bcc9736cSJeff Kirsher 	} else {
5794bcc9736cSJeff Kirsher 		if (MAX_MULTICAST_LIST == hw->multi_list_size) {
5795bcc9736cSJeff Kirsher 			--hw->all_multi;
5796bcc9736cSJeff Kirsher 			hw_set_multicast(hw, hw->all_multi);
5797bcc9736cSJeff Kirsher 		}
5798bcc9736cSJeff Kirsher 		hw->multi_list_size = 0;
5799bcc9736cSJeff Kirsher 		hw_clr_multicast(hw);
5800bcc9736cSJeff Kirsher 	}
5801bcc9736cSJeff Kirsher }
5802bcc9736cSJeff Kirsher 
5803bcc9736cSJeff Kirsher static int netdev_change_mtu(struct net_device *dev, int new_mtu)
5804bcc9736cSJeff Kirsher {
5805bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5806bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5807bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5808bcc9736cSJeff Kirsher 	int hw_mtu;
5809bcc9736cSJeff Kirsher 
5810bcc9736cSJeff Kirsher 	if (netif_running(dev))
5811bcc9736cSJeff Kirsher 		return -EBUSY;
5812bcc9736cSJeff Kirsher 
5813bcc9736cSJeff Kirsher 	/* Cannot use different MTU in multiple device interfaces mode. */
5814bcc9736cSJeff Kirsher 	if (hw->dev_count > 1)
5815bcc9736cSJeff Kirsher 		if (dev != hw_priv->dev)
5816bcc9736cSJeff Kirsher 			return 0;
5817bcc9736cSJeff Kirsher 	if (new_mtu < 60)
5818bcc9736cSJeff Kirsher 		return -EINVAL;
5819bcc9736cSJeff Kirsher 
5820bcc9736cSJeff Kirsher 	if (dev->mtu != new_mtu) {
5821bcc9736cSJeff Kirsher 		hw_mtu = new_mtu + ETHERNET_HEADER_SIZE + 4;
5822bcc9736cSJeff Kirsher 		if (hw_mtu > MAX_RX_BUF_SIZE)
5823bcc9736cSJeff Kirsher 			return -EINVAL;
5824bcc9736cSJeff Kirsher 		if (hw_mtu > REGULAR_RX_BUF_SIZE) {
5825bcc9736cSJeff Kirsher 			hw->features |= RX_HUGE_FRAME;
5826bcc9736cSJeff Kirsher 			hw_mtu = MAX_RX_BUF_SIZE;
5827bcc9736cSJeff Kirsher 		} else {
5828bcc9736cSJeff Kirsher 			hw->features &= ~RX_HUGE_FRAME;
5829bcc9736cSJeff Kirsher 			hw_mtu = REGULAR_RX_BUF_SIZE;
5830bcc9736cSJeff Kirsher 		}
5831bcc9736cSJeff Kirsher 		hw_mtu = (hw_mtu + 3) & ~3;
5832bcc9736cSJeff Kirsher 		hw_priv->mtu = hw_mtu;
5833bcc9736cSJeff Kirsher 		dev->mtu = new_mtu;
5834bcc9736cSJeff Kirsher 	}
5835bcc9736cSJeff Kirsher 	return 0;
5836bcc9736cSJeff Kirsher }
5837bcc9736cSJeff Kirsher 
5838bcc9736cSJeff Kirsher /**
5839bcc9736cSJeff Kirsher  * netdev_ioctl - I/O control processing
5840bcc9736cSJeff Kirsher  * @dev:	Network device.
5841bcc9736cSJeff Kirsher  * @ifr:	Interface request structure.
5842bcc9736cSJeff Kirsher  * @cmd:	I/O control code.
5843bcc9736cSJeff Kirsher  *
5844bcc9736cSJeff Kirsher  * This function is used to process I/O control calls.
5845bcc9736cSJeff Kirsher  *
5846bcc9736cSJeff Kirsher  * Return 0 to indicate success.
5847bcc9736cSJeff Kirsher  */
5848bcc9736cSJeff Kirsher static int netdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5849bcc9736cSJeff Kirsher {
5850bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5851bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5852bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
5853bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
5854bcc9736cSJeff Kirsher 	int rc;
5855bcc9736cSJeff Kirsher 	int result = 0;
5856bcc9736cSJeff Kirsher 	struct mii_ioctl_data *data = if_mii(ifr);
5857bcc9736cSJeff Kirsher 
5858bcc9736cSJeff Kirsher 	if (down_interruptible(&priv->proc_sem))
5859bcc9736cSJeff Kirsher 		return -ERESTARTSYS;
5860bcc9736cSJeff Kirsher 
5861bcc9736cSJeff Kirsher 	/* assume success */
5862bcc9736cSJeff Kirsher 	rc = 0;
5863bcc9736cSJeff Kirsher 	switch (cmd) {
5864bcc9736cSJeff Kirsher 	/* Get address of MII PHY in use. */
5865bcc9736cSJeff Kirsher 	case SIOCGMIIPHY:
5866bcc9736cSJeff Kirsher 		data->phy_id = priv->id;
5867bcc9736cSJeff Kirsher 
5868bcc9736cSJeff Kirsher 		/* Fallthrough... */
5869bcc9736cSJeff Kirsher 
5870bcc9736cSJeff Kirsher 	/* Read MII PHY register. */
5871bcc9736cSJeff Kirsher 	case SIOCGMIIREG:
5872bcc9736cSJeff Kirsher 		if (data->phy_id != priv->id || data->reg_num >= 6)
5873bcc9736cSJeff Kirsher 			result = -EIO;
5874bcc9736cSJeff Kirsher 		else
5875bcc9736cSJeff Kirsher 			hw_r_phy(hw, port->linked->port_id, data->reg_num,
5876bcc9736cSJeff Kirsher 				&data->val_out);
5877bcc9736cSJeff Kirsher 		break;
5878bcc9736cSJeff Kirsher 
5879bcc9736cSJeff Kirsher 	/* Write MII PHY register. */
5880bcc9736cSJeff Kirsher 	case SIOCSMIIREG:
5881bcc9736cSJeff Kirsher 		if (!capable(CAP_NET_ADMIN))
5882bcc9736cSJeff Kirsher 			result = -EPERM;
5883bcc9736cSJeff Kirsher 		else if (data->phy_id != priv->id || data->reg_num >= 6)
5884bcc9736cSJeff Kirsher 			result = -EIO;
5885bcc9736cSJeff Kirsher 		else
5886bcc9736cSJeff Kirsher 			hw_w_phy(hw, port->linked->port_id, data->reg_num,
5887bcc9736cSJeff Kirsher 				data->val_in);
5888bcc9736cSJeff Kirsher 		break;
5889bcc9736cSJeff Kirsher 
5890bcc9736cSJeff Kirsher 	default:
5891bcc9736cSJeff Kirsher 		result = -EOPNOTSUPP;
5892bcc9736cSJeff Kirsher 	}
5893bcc9736cSJeff Kirsher 
5894bcc9736cSJeff Kirsher 	up(&priv->proc_sem);
5895bcc9736cSJeff Kirsher 
5896bcc9736cSJeff Kirsher 	return result;
5897bcc9736cSJeff Kirsher }
5898bcc9736cSJeff Kirsher 
5899bcc9736cSJeff Kirsher /*
5900bcc9736cSJeff Kirsher  * MII support
5901bcc9736cSJeff Kirsher  */
5902bcc9736cSJeff Kirsher 
5903bcc9736cSJeff Kirsher /**
5904bcc9736cSJeff Kirsher  * mdio_read - read PHY register
5905bcc9736cSJeff Kirsher  * @dev:	Network device.
5906bcc9736cSJeff Kirsher  * @phy_id:	The PHY id.
5907bcc9736cSJeff Kirsher  * @reg_num:	The register number.
5908bcc9736cSJeff Kirsher  *
5909bcc9736cSJeff Kirsher  * This function returns the PHY register value.
5910bcc9736cSJeff Kirsher  *
5911bcc9736cSJeff Kirsher  * Return the register value.
5912bcc9736cSJeff Kirsher  */
5913bcc9736cSJeff Kirsher static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
5914bcc9736cSJeff Kirsher {
5915bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5916bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
5917bcc9736cSJeff Kirsher 	struct ksz_hw *hw = port->hw;
5918bcc9736cSJeff Kirsher 	u16 val_out;
5919bcc9736cSJeff Kirsher 
5920bcc9736cSJeff Kirsher 	hw_r_phy(hw, port->linked->port_id, reg_num << 1, &val_out);
5921bcc9736cSJeff Kirsher 	return val_out;
5922bcc9736cSJeff Kirsher }
5923bcc9736cSJeff Kirsher 
5924bcc9736cSJeff Kirsher /**
5925bcc9736cSJeff Kirsher  * mdio_write - set PHY register
5926bcc9736cSJeff Kirsher  * @dev:	Network device.
5927bcc9736cSJeff Kirsher  * @phy_id:	The PHY id.
5928bcc9736cSJeff Kirsher  * @reg_num:	The register number.
5929bcc9736cSJeff Kirsher  * @val:	The register value.
5930bcc9736cSJeff Kirsher  *
5931bcc9736cSJeff Kirsher  * This procedure sets the PHY register value.
5932bcc9736cSJeff Kirsher  */
5933bcc9736cSJeff Kirsher static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
5934bcc9736cSJeff Kirsher {
5935bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5936bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
5937bcc9736cSJeff Kirsher 	struct ksz_hw *hw = port->hw;
5938bcc9736cSJeff Kirsher 	int i;
5939bcc9736cSJeff Kirsher 	int pi;
5940bcc9736cSJeff Kirsher 
5941bcc9736cSJeff Kirsher 	for (i = 0, pi = port->first_port; i < port->port_cnt; i++, pi++)
5942bcc9736cSJeff Kirsher 		hw_w_phy(hw, pi, reg_num << 1, val);
5943bcc9736cSJeff Kirsher }
5944bcc9736cSJeff Kirsher 
5945bcc9736cSJeff Kirsher /*
5946bcc9736cSJeff Kirsher  * ethtool support
5947bcc9736cSJeff Kirsher  */
5948bcc9736cSJeff Kirsher 
5949bcc9736cSJeff Kirsher #define EEPROM_SIZE			0x40
5950bcc9736cSJeff Kirsher 
5951bcc9736cSJeff Kirsher static u16 eeprom_data[EEPROM_SIZE] = { 0 };
5952bcc9736cSJeff Kirsher 
5953bcc9736cSJeff Kirsher #define ADVERTISED_ALL			\
5954bcc9736cSJeff Kirsher 	(ADVERTISED_10baseT_Half |	\
5955bcc9736cSJeff Kirsher 	ADVERTISED_10baseT_Full |	\
5956bcc9736cSJeff Kirsher 	ADVERTISED_100baseT_Half |	\
5957bcc9736cSJeff Kirsher 	ADVERTISED_100baseT_Full)
5958bcc9736cSJeff Kirsher 
5959bcc9736cSJeff Kirsher /* These functions use the MII functions in mii.c. */
5960bcc9736cSJeff Kirsher 
5961bcc9736cSJeff Kirsher /**
5962bcc9736cSJeff Kirsher  * netdev_get_settings - get network device settings
5963bcc9736cSJeff Kirsher  * @dev:	Network device.
5964bcc9736cSJeff Kirsher  * @cmd:	Ethtool command.
5965bcc9736cSJeff Kirsher  *
5966bcc9736cSJeff Kirsher  * This function queries the PHY and returns its state in the ethtool command.
5967bcc9736cSJeff Kirsher  *
5968bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code.
5969bcc9736cSJeff Kirsher  */
5970bcc9736cSJeff Kirsher static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
5971bcc9736cSJeff Kirsher {
5972bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5973bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5974bcc9736cSJeff Kirsher 
5975bcc9736cSJeff Kirsher 	mutex_lock(&hw_priv->lock);
5976bcc9736cSJeff Kirsher 	mii_ethtool_gset(&priv->mii_if, cmd);
5977bcc9736cSJeff Kirsher 	cmd->advertising |= SUPPORTED_TP;
5978bcc9736cSJeff Kirsher 	mutex_unlock(&hw_priv->lock);
5979bcc9736cSJeff Kirsher 
5980bcc9736cSJeff Kirsher 	/* Save advertised settings for workaround in next function. */
5981bcc9736cSJeff Kirsher 	priv->advertising = cmd->advertising;
5982bcc9736cSJeff Kirsher 	return 0;
5983bcc9736cSJeff Kirsher }
5984bcc9736cSJeff Kirsher 
5985bcc9736cSJeff Kirsher /**
5986bcc9736cSJeff Kirsher  * netdev_set_settings - set network device settings
5987bcc9736cSJeff Kirsher  * @dev:	Network device.
5988bcc9736cSJeff Kirsher  * @cmd:	Ethtool command.
5989bcc9736cSJeff Kirsher  *
5990bcc9736cSJeff Kirsher  * This function sets the PHY according to the ethtool command.
5991bcc9736cSJeff Kirsher  *
5992bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code.
5993bcc9736cSJeff Kirsher  */
5994bcc9736cSJeff Kirsher static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
5995bcc9736cSJeff Kirsher {
5996bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
5997bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
5998bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
5999bcc9736cSJeff Kirsher 	u32 speed = ethtool_cmd_speed(cmd);
6000bcc9736cSJeff Kirsher 	int rc;
6001bcc9736cSJeff Kirsher 
6002bcc9736cSJeff Kirsher 	/*
6003bcc9736cSJeff Kirsher 	 * ethtool utility does not change advertised setting if auto
6004bcc9736cSJeff Kirsher 	 * negotiation is not specified explicitly.
6005bcc9736cSJeff Kirsher 	 */
6006bcc9736cSJeff Kirsher 	if (cmd->autoneg && priv->advertising == cmd->advertising) {
6007bcc9736cSJeff Kirsher 		cmd->advertising |= ADVERTISED_ALL;
6008bcc9736cSJeff Kirsher 		if (10 == speed)
6009bcc9736cSJeff Kirsher 			cmd->advertising &=
6010bcc9736cSJeff Kirsher 				~(ADVERTISED_100baseT_Full |
6011bcc9736cSJeff Kirsher 				ADVERTISED_100baseT_Half);
6012bcc9736cSJeff Kirsher 		else if (100 == speed)
6013bcc9736cSJeff Kirsher 			cmd->advertising &=
6014bcc9736cSJeff Kirsher 				~(ADVERTISED_10baseT_Full |
6015bcc9736cSJeff Kirsher 				ADVERTISED_10baseT_Half);
6016bcc9736cSJeff Kirsher 		if (0 == cmd->duplex)
6017bcc9736cSJeff Kirsher 			cmd->advertising &=
6018bcc9736cSJeff Kirsher 				~(ADVERTISED_100baseT_Full |
6019bcc9736cSJeff Kirsher 				ADVERTISED_10baseT_Full);
6020bcc9736cSJeff Kirsher 		else if (1 == cmd->duplex)
6021bcc9736cSJeff Kirsher 			cmd->advertising &=
6022bcc9736cSJeff Kirsher 				~(ADVERTISED_100baseT_Half |
6023bcc9736cSJeff Kirsher 				ADVERTISED_10baseT_Half);
6024bcc9736cSJeff Kirsher 	}
6025bcc9736cSJeff Kirsher 	mutex_lock(&hw_priv->lock);
6026bcc9736cSJeff Kirsher 	if (cmd->autoneg &&
6027bcc9736cSJeff Kirsher 			(cmd->advertising & ADVERTISED_ALL) ==
6028bcc9736cSJeff Kirsher 			ADVERTISED_ALL) {
6029bcc9736cSJeff Kirsher 		port->duplex = 0;
6030bcc9736cSJeff Kirsher 		port->speed = 0;
6031bcc9736cSJeff Kirsher 		port->force_link = 0;
6032bcc9736cSJeff Kirsher 	} else {
6033bcc9736cSJeff Kirsher 		port->duplex = cmd->duplex + 1;
6034bcc9736cSJeff Kirsher 		if (1000 != speed)
6035bcc9736cSJeff Kirsher 			port->speed = speed;
6036bcc9736cSJeff Kirsher 		if (cmd->autoneg)
6037bcc9736cSJeff Kirsher 			port->force_link = 0;
6038bcc9736cSJeff Kirsher 		else
6039bcc9736cSJeff Kirsher 			port->force_link = 1;
6040bcc9736cSJeff Kirsher 	}
6041bcc9736cSJeff Kirsher 	rc = mii_ethtool_sset(&priv->mii_if, cmd);
6042bcc9736cSJeff Kirsher 	mutex_unlock(&hw_priv->lock);
6043bcc9736cSJeff Kirsher 	return rc;
6044bcc9736cSJeff Kirsher }
6045bcc9736cSJeff Kirsher 
6046bcc9736cSJeff Kirsher /**
6047bcc9736cSJeff Kirsher  * netdev_nway_reset - restart auto-negotiation
6048bcc9736cSJeff Kirsher  * @dev:	Network device.
6049bcc9736cSJeff Kirsher  *
6050bcc9736cSJeff Kirsher  * This function restarts the PHY for auto-negotiation.
6051bcc9736cSJeff Kirsher  *
6052bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code.
6053bcc9736cSJeff Kirsher  */
6054bcc9736cSJeff Kirsher static int netdev_nway_reset(struct net_device *dev)
6055bcc9736cSJeff Kirsher {
6056bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6057bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6058bcc9736cSJeff Kirsher 	int rc;
6059bcc9736cSJeff Kirsher 
6060bcc9736cSJeff Kirsher 	mutex_lock(&hw_priv->lock);
6061bcc9736cSJeff Kirsher 	rc = mii_nway_restart(&priv->mii_if);
6062bcc9736cSJeff Kirsher 	mutex_unlock(&hw_priv->lock);
6063bcc9736cSJeff Kirsher 	return rc;
6064bcc9736cSJeff Kirsher }
6065bcc9736cSJeff Kirsher 
6066bcc9736cSJeff Kirsher /**
6067bcc9736cSJeff Kirsher  * netdev_get_link - get network device link status
6068bcc9736cSJeff Kirsher  * @dev:	Network device.
6069bcc9736cSJeff Kirsher  *
6070bcc9736cSJeff Kirsher  * This function gets the link status from the PHY.
6071bcc9736cSJeff Kirsher  *
6072bcc9736cSJeff Kirsher  * Return true if PHY is linked and false otherwise.
6073bcc9736cSJeff Kirsher  */
6074bcc9736cSJeff Kirsher static u32 netdev_get_link(struct net_device *dev)
6075bcc9736cSJeff Kirsher {
6076bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6077bcc9736cSJeff Kirsher 	int rc;
6078bcc9736cSJeff Kirsher 
6079bcc9736cSJeff Kirsher 	rc = mii_link_ok(&priv->mii_if);
6080bcc9736cSJeff Kirsher 	return rc;
6081bcc9736cSJeff Kirsher }
6082bcc9736cSJeff Kirsher 
6083bcc9736cSJeff Kirsher /**
6084bcc9736cSJeff Kirsher  * netdev_get_drvinfo - get network driver information
6085bcc9736cSJeff Kirsher  * @dev:	Network device.
6086bcc9736cSJeff Kirsher  * @info:	Ethtool driver info data structure.
6087bcc9736cSJeff Kirsher  *
6088bcc9736cSJeff Kirsher  * This procedure returns the driver information.
6089bcc9736cSJeff Kirsher  */
6090bcc9736cSJeff Kirsher static void netdev_get_drvinfo(struct net_device *dev,
6091bcc9736cSJeff Kirsher 	struct ethtool_drvinfo *info)
6092bcc9736cSJeff Kirsher {
6093bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6094bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6095bcc9736cSJeff Kirsher 
6096bcc9736cSJeff Kirsher 	strcpy(info->driver, DRV_NAME);
6097bcc9736cSJeff Kirsher 	strcpy(info->version, DRV_VERSION);
6098bcc9736cSJeff Kirsher 	strcpy(info->bus_info, pci_name(hw_priv->pdev));
6099bcc9736cSJeff Kirsher }
6100bcc9736cSJeff Kirsher 
6101bcc9736cSJeff Kirsher /**
6102bcc9736cSJeff Kirsher  * netdev_get_regs_len - get length of register dump
6103bcc9736cSJeff Kirsher  * @dev:	Network device.
6104bcc9736cSJeff Kirsher  *
6105bcc9736cSJeff Kirsher  * This function returns the length of the register dump.
6106bcc9736cSJeff Kirsher  *
6107bcc9736cSJeff Kirsher  * Return length of the register dump.
6108bcc9736cSJeff Kirsher  */
6109bcc9736cSJeff Kirsher static struct hw_regs {
6110bcc9736cSJeff Kirsher 	int start;
6111bcc9736cSJeff Kirsher 	int end;
6112bcc9736cSJeff Kirsher } hw_regs_range[] = {
6113bcc9736cSJeff Kirsher 	{ KS_DMA_TX_CTRL,	KS884X_INTERRUPTS_STATUS },
6114bcc9736cSJeff Kirsher 	{ KS_ADD_ADDR_0_LO,	KS_ADD_ADDR_F_HI },
6115bcc9736cSJeff Kirsher 	{ KS884X_ADDR_0_OFFSET,	KS8841_WOL_FRAME_BYTE2_OFFSET },
6116bcc9736cSJeff Kirsher 	{ KS884X_SIDER_P,	KS8842_SGCR7_P },
6117bcc9736cSJeff Kirsher 	{ KS8842_MACAR1_P,	KS8842_TOSR8_P },
6118bcc9736cSJeff Kirsher 	{ KS884X_P1MBCR_P,	KS8842_P3ERCR_P },
6119bcc9736cSJeff Kirsher 	{ 0, 0 }
6120bcc9736cSJeff Kirsher };
6121bcc9736cSJeff Kirsher 
6122bcc9736cSJeff Kirsher static int netdev_get_regs_len(struct net_device *dev)
6123bcc9736cSJeff Kirsher {
6124bcc9736cSJeff Kirsher 	struct hw_regs *range = hw_regs_range;
6125bcc9736cSJeff Kirsher 	int regs_len = 0x10 * sizeof(u32);
6126bcc9736cSJeff Kirsher 
6127bcc9736cSJeff Kirsher 	while (range->end > range->start) {
6128bcc9736cSJeff Kirsher 		regs_len += (range->end - range->start + 3) / 4 * 4;
6129bcc9736cSJeff Kirsher 		range++;
6130bcc9736cSJeff Kirsher 	}
6131bcc9736cSJeff Kirsher 	return regs_len;
6132bcc9736cSJeff Kirsher }
6133bcc9736cSJeff Kirsher 
6134bcc9736cSJeff Kirsher /**
6135bcc9736cSJeff Kirsher  * netdev_get_regs - get register dump
6136bcc9736cSJeff Kirsher  * @dev:	Network device.
6137bcc9736cSJeff Kirsher  * @regs:	Ethtool registers data structure.
6138bcc9736cSJeff Kirsher  * @ptr:	Buffer to store the register values.
6139bcc9736cSJeff Kirsher  *
6140bcc9736cSJeff Kirsher  * This procedure dumps the register values in the provided buffer.
6141bcc9736cSJeff Kirsher  */
6142bcc9736cSJeff Kirsher static void netdev_get_regs(struct net_device *dev, struct ethtool_regs *regs,
6143bcc9736cSJeff Kirsher 	void *ptr)
6144bcc9736cSJeff Kirsher {
6145bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6146bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6147bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6148bcc9736cSJeff Kirsher 	int *buf = (int *) ptr;
6149bcc9736cSJeff Kirsher 	struct hw_regs *range = hw_regs_range;
6150bcc9736cSJeff Kirsher 	int len;
6151bcc9736cSJeff Kirsher 
6152bcc9736cSJeff Kirsher 	mutex_lock(&hw_priv->lock);
6153bcc9736cSJeff Kirsher 	regs->version = 0;
6154bcc9736cSJeff Kirsher 	for (len = 0; len < 0x40; len += 4) {
6155bcc9736cSJeff Kirsher 		pci_read_config_dword(hw_priv->pdev, len, buf);
6156bcc9736cSJeff Kirsher 		buf++;
6157bcc9736cSJeff Kirsher 	}
6158bcc9736cSJeff Kirsher 	while (range->end > range->start) {
6159bcc9736cSJeff Kirsher 		for (len = range->start; len < range->end; len += 4) {
6160bcc9736cSJeff Kirsher 			*buf = readl(hw->io + len);
6161bcc9736cSJeff Kirsher 			buf++;
6162bcc9736cSJeff Kirsher 		}
6163bcc9736cSJeff Kirsher 		range++;
6164bcc9736cSJeff Kirsher 	}
6165bcc9736cSJeff Kirsher 	mutex_unlock(&hw_priv->lock);
6166bcc9736cSJeff Kirsher }
6167bcc9736cSJeff Kirsher 
6168bcc9736cSJeff Kirsher #define WOL_SUPPORT			\
6169bcc9736cSJeff Kirsher 	(WAKE_PHY | WAKE_MAGIC |	\
6170bcc9736cSJeff Kirsher 	WAKE_UCAST | WAKE_MCAST |	\
6171bcc9736cSJeff Kirsher 	WAKE_BCAST | WAKE_ARP)
6172bcc9736cSJeff Kirsher 
6173bcc9736cSJeff Kirsher /**
6174bcc9736cSJeff Kirsher  * netdev_get_wol - get Wake-on-LAN support
6175bcc9736cSJeff Kirsher  * @dev:	Network device.
6176bcc9736cSJeff Kirsher  * @wol:	Ethtool Wake-on-LAN data structure.
6177bcc9736cSJeff Kirsher  *
6178bcc9736cSJeff Kirsher  * This procedure returns Wake-on-LAN support.
6179bcc9736cSJeff Kirsher  */
6180bcc9736cSJeff Kirsher static void netdev_get_wol(struct net_device *dev,
6181bcc9736cSJeff Kirsher 	struct ethtool_wolinfo *wol)
6182bcc9736cSJeff Kirsher {
6183bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6184bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6185bcc9736cSJeff Kirsher 
6186bcc9736cSJeff Kirsher 	wol->supported = hw_priv->wol_support;
6187bcc9736cSJeff Kirsher 	wol->wolopts = hw_priv->wol_enable;
6188bcc9736cSJeff Kirsher 	memset(&wol->sopass, 0, sizeof(wol->sopass));
6189bcc9736cSJeff Kirsher }
6190bcc9736cSJeff Kirsher 
6191bcc9736cSJeff Kirsher /**
6192bcc9736cSJeff Kirsher  * netdev_set_wol - set Wake-on-LAN support
6193bcc9736cSJeff Kirsher  * @dev:	Network device.
6194bcc9736cSJeff Kirsher  * @wol:	Ethtool Wake-on-LAN data structure.
6195bcc9736cSJeff Kirsher  *
6196bcc9736cSJeff Kirsher  * This function sets Wake-on-LAN support.
6197bcc9736cSJeff Kirsher  *
6198bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code.
6199bcc9736cSJeff Kirsher  */
6200bcc9736cSJeff Kirsher static int netdev_set_wol(struct net_device *dev,
6201bcc9736cSJeff Kirsher 	struct ethtool_wolinfo *wol)
6202bcc9736cSJeff Kirsher {
6203bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6204bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6205bcc9736cSJeff Kirsher 
6206bcc9736cSJeff Kirsher 	/* Need to find a way to retrieve the device IP address. */
6207bcc9736cSJeff Kirsher 	static const u8 net_addr[] = { 192, 168, 1, 1 };
6208bcc9736cSJeff Kirsher 
6209bcc9736cSJeff Kirsher 	if (wol->wolopts & ~hw_priv->wol_support)
6210bcc9736cSJeff Kirsher 		return -EINVAL;
6211bcc9736cSJeff Kirsher 
6212bcc9736cSJeff Kirsher 	hw_priv->wol_enable = wol->wolopts;
6213bcc9736cSJeff Kirsher 
6214bcc9736cSJeff Kirsher 	/* Link wakeup cannot really be disabled. */
6215bcc9736cSJeff Kirsher 	if (wol->wolopts)
6216bcc9736cSJeff Kirsher 		hw_priv->wol_enable |= WAKE_PHY;
6217bcc9736cSJeff Kirsher 	hw_enable_wol(&hw_priv->hw, hw_priv->wol_enable, net_addr);
6218bcc9736cSJeff Kirsher 	return 0;
6219bcc9736cSJeff Kirsher }
6220bcc9736cSJeff Kirsher 
6221bcc9736cSJeff Kirsher /**
6222bcc9736cSJeff Kirsher  * netdev_get_msglevel - get debug message level
6223bcc9736cSJeff Kirsher  * @dev:	Network device.
6224bcc9736cSJeff Kirsher  *
6225bcc9736cSJeff Kirsher  * This function returns current debug message level.
6226bcc9736cSJeff Kirsher  *
6227bcc9736cSJeff Kirsher  * Return current debug message flags.
6228bcc9736cSJeff Kirsher  */
6229bcc9736cSJeff Kirsher static u32 netdev_get_msglevel(struct net_device *dev)
6230bcc9736cSJeff Kirsher {
6231bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6232bcc9736cSJeff Kirsher 
6233bcc9736cSJeff Kirsher 	return priv->msg_enable;
6234bcc9736cSJeff Kirsher }
6235bcc9736cSJeff Kirsher 
6236bcc9736cSJeff Kirsher /**
6237bcc9736cSJeff Kirsher  * netdev_set_msglevel - set debug message level
6238bcc9736cSJeff Kirsher  * @dev:	Network device.
6239bcc9736cSJeff Kirsher  * @value:	Debug message flags.
6240bcc9736cSJeff Kirsher  *
6241bcc9736cSJeff Kirsher  * This procedure sets debug message level.
6242bcc9736cSJeff Kirsher  */
6243bcc9736cSJeff Kirsher static void netdev_set_msglevel(struct net_device *dev, u32 value)
6244bcc9736cSJeff Kirsher {
6245bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6246bcc9736cSJeff Kirsher 
6247bcc9736cSJeff Kirsher 	priv->msg_enable = value;
6248bcc9736cSJeff Kirsher }
6249bcc9736cSJeff Kirsher 
6250bcc9736cSJeff Kirsher /**
6251bcc9736cSJeff Kirsher  * netdev_get_eeprom_len - get EEPROM length
6252bcc9736cSJeff Kirsher  * @dev:	Network device.
6253bcc9736cSJeff Kirsher  *
6254bcc9736cSJeff Kirsher  * This function returns the length of the EEPROM.
6255bcc9736cSJeff Kirsher  *
6256bcc9736cSJeff Kirsher  * Return length of the EEPROM.
6257bcc9736cSJeff Kirsher  */
6258bcc9736cSJeff Kirsher static int netdev_get_eeprom_len(struct net_device *dev)
6259bcc9736cSJeff Kirsher {
6260bcc9736cSJeff Kirsher 	return EEPROM_SIZE * 2;
6261bcc9736cSJeff Kirsher }
6262bcc9736cSJeff Kirsher 
6263bcc9736cSJeff Kirsher /**
6264bcc9736cSJeff Kirsher  * netdev_get_eeprom - get EEPROM data
6265bcc9736cSJeff Kirsher  * @dev:	Network device.
6266bcc9736cSJeff Kirsher  * @eeprom:	Ethtool EEPROM data structure.
6267bcc9736cSJeff Kirsher  * @data:	Buffer to store the EEPROM data.
6268bcc9736cSJeff Kirsher  *
6269bcc9736cSJeff Kirsher  * This function dumps the EEPROM data in the provided buffer.
6270bcc9736cSJeff Kirsher  *
6271bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code.
6272bcc9736cSJeff Kirsher  */
6273bcc9736cSJeff Kirsher #define EEPROM_MAGIC			0x10A18842
6274bcc9736cSJeff Kirsher 
6275bcc9736cSJeff Kirsher static int netdev_get_eeprom(struct net_device *dev,
6276bcc9736cSJeff Kirsher 	struct ethtool_eeprom *eeprom, u8 *data)
6277bcc9736cSJeff Kirsher {
6278bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6279bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6280bcc9736cSJeff Kirsher 	u8 *eeprom_byte = (u8 *) eeprom_data;
6281bcc9736cSJeff Kirsher 	int i;
6282bcc9736cSJeff Kirsher 	int len;
6283bcc9736cSJeff Kirsher 
6284bcc9736cSJeff Kirsher 	len = (eeprom->offset + eeprom->len + 1) / 2;
6285bcc9736cSJeff Kirsher 	for (i = eeprom->offset / 2; i < len; i++)
6286bcc9736cSJeff Kirsher 		eeprom_data[i] = eeprom_read(&hw_priv->hw, i);
6287bcc9736cSJeff Kirsher 	eeprom->magic = EEPROM_MAGIC;
6288bcc9736cSJeff Kirsher 	memcpy(data, &eeprom_byte[eeprom->offset], eeprom->len);
6289bcc9736cSJeff Kirsher 
6290bcc9736cSJeff Kirsher 	return 0;
6291bcc9736cSJeff Kirsher }
6292bcc9736cSJeff Kirsher 
6293bcc9736cSJeff Kirsher /**
6294bcc9736cSJeff Kirsher  * netdev_set_eeprom - write EEPROM data
6295bcc9736cSJeff Kirsher  * @dev:	Network device.
6296bcc9736cSJeff Kirsher  * @eeprom:	Ethtool EEPROM data structure.
6297bcc9736cSJeff Kirsher  * @data:	Data buffer.
6298bcc9736cSJeff Kirsher  *
6299bcc9736cSJeff Kirsher  * This function modifies the EEPROM data one byte at a time.
6300bcc9736cSJeff Kirsher  *
6301bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code.
6302bcc9736cSJeff Kirsher  */
6303bcc9736cSJeff Kirsher static int netdev_set_eeprom(struct net_device *dev,
6304bcc9736cSJeff Kirsher 	struct ethtool_eeprom *eeprom, u8 *data)
6305bcc9736cSJeff Kirsher {
6306bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6307bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6308bcc9736cSJeff Kirsher 	u16 eeprom_word[EEPROM_SIZE];
6309bcc9736cSJeff Kirsher 	u8 *eeprom_byte = (u8 *) eeprom_word;
6310bcc9736cSJeff Kirsher 	int i;
6311bcc9736cSJeff Kirsher 	int len;
6312bcc9736cSJeff Kirsher 
6313bcc9736cSJeff Kirsher 	if (eeprom->magic != EEPROM_MAGIC)
6314bcc9736cSJeff Kirsher 		return -EINVAL;
6315bcc9736cSJeff Kirsher 
6316bcc9736cSJeff Kirsher 	len = (eeprom->offset + eeprom->len + 1) / 2;
6317bcc9736cSJeff Kirsher 	for (i = eeprom->offset / 2; i < len; i++)
6318bcc9736cSJeff Kirsher 		eeprom_data[i] = eeprom_read(&hw_priv->hw, i);
6319bcc9736cSJeff Kirsher 	memcpy(eeprom_word, eeprom_data, EEPROM_SIZE * 2);
6320bcc9736cSJeff Kirsher 	memcpy(&eeprom_byte[eeprom->offset], data, eeprom->len);
6321bcc9736cSJeff Kirsher 	for (i = 0; i < EEPROM_SIZE; i++)
6322bcc9736cSJeff Kirsher 		if (eeprom_word[i] != eeprom_data[i]) {
6323bcc9736cSJeff Kirsher 			eeprom_data[i] = eeprom_word[i];
6324bcc9736cSJeff Kirsher 			eeprom_write(&hw_priv->hw, i, eeprom_data[i]);
6325bcc9736cSJeff Kirsher 	}
6326bcc9736cSJeff Kirsher 
6327bcc9736cSJeff Kirsher 	return 0;
6328bcc9736cSJeff Kirsher }
6329bcc9736cSJeff Kirsher 
6330bcc9736cSJeff Kirsher /**
6331bcc9736cSJeff Kirsher  * netdev_get_pauseparam - get flow control parameters
6332bcc9736cSJeff Kirsher  * @dev:	Network device.
6333bcc9736cSJeff Kirsher  * @pause:	Ethtool PAUSE settings data structure.
6334bcc9736cSJeff Kirsher  *
6335bcc9736cSJeff Kirsher  * This procedure returns the PAUSE control flow settings.
6336bcc9736cSJeff Kirsher  */
6337bcc9736cSJeff Kirsher static void netdev_get_pauseparam(struct net_device *dev,
6338bcc9736cSJeff Kirsher 	struct ethtool_pauseparam *pause)
6339bcc9736cSJeff Kirsher {
6340bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6341bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6342bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6343bcc9736cSJeff Kirsher 
6344bcc9736cSJeff Kirsher 	pause->autoneg = (hw->overrides & PAUSE_FLOW_CTRL) ? 0 : 1;
6345bcc9736cSJeff Kirsher 	if (!hw->ksz_switch) {
6346bcc9736cSJeff Kirsher 		pause->rx_pause =
6347bcc9736cSJeff Kirsher 			(hw->rx_cfg & DMA_RX_FLOW_ENABLE) ? 1 : 0;
6348bcc9736cSJeff Kirsher 		pause->tx_pause =
6349bcc9736cSJeff Kirsher 			(hw->tx_cfg & DMA_TX_FLOW_ENABLE) ? 1 : 0;
6350bcc9736cSJeff Kirsher 	} else {
6351bcc9736cSJeff Kirsher 		pause->rx_pause =
6352bcc9736cSJeff Kirsher 			(sw_chk(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6353bcc9736cSJeff Kirsher 				SWITCH_RX_FLOW_CTRL)) ? 1 : 0;
6354bcc9736cSJeff Kirsher 		pause->tx_pause =
6355bcc9736cSJeff Kirsher 			(sw_chk(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6356bcc9736cSJeff Kirsher 				SWITCH_TX_FLOW_CTRL)) ? 1 : 0;
6357bcc9736cSJeff Kirsher 	}
6358bcc9736cSJeff Kirsher }
6359bcc9736cSJeff Kirsher 
6360bcc9736cSJeff Kirsher /**
6361bcc9736cSJeff Kirsher  * netdev_set_pauseparam - set flow control parameters
6362bcc9736cSJeff Kirsher  * @dev:	Network device.
6363bcc9736cSJeff Kirsher  * @pause:	Ethtool PAUSE settings data structure.
6364bcc9736cSJeff Kirsher  *
6365bcc9736cSJeff Kirsher  * This function sets the PAUSE control flow settings.
6366bcc9736cSJeff Kirsher  * Not implemented yet.
6367bcc9736cSJeff Kirsher  *
6368bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code.
6369bcc9736cSJeff Kirsher  */
6370bcc9736cSJeff Kirsher static int netdev_set_pauseparam(struct net_device *dev,
6371bcc9736cSJeff Kirsher 	struct ethtool_pauseparam *pause)
6372bcc9736cSJeff Kirsher {
6373bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6374bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6375bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6376bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
6377bcc9736cSJeff Kirsher 
6378bcc9736cSJeff Kirsher 	mutex_lock(&hw_priv->lock);
6379bcc9736cSJeff Kirsher 	if (pause->autoneg) {
6380bcc9736cSJeff Kirsher 		if (!pause->rx_pause && !pause->tx_pause)
6381bcc9736cSJeff Kirsher 			port->flow_ctrl = PHY_NO_FLOW_CTRL;
6382bcc9736cSJeff Kirsher 		else
6383bcc9736cSJeff Kirsher 			port->flow_ctrl = PHY_FLOW_CTRL;
6384bcc9736cSJeff Kirsher 		hw->overrides &= ~PAUSE_FLOW_CTRL;
6385bcc9736cSJeff Kirsher 		port->force_link = 0;
6386bcc9736cSJeff Kirsher 		if (hw->ksz_switch) {
6387bcc9736cSJeff Kirsher 			sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6388bcc9736cSJeff Kirsher 				SWITCH_RX_FLOW_CTRL, 1);
6389bcc9736cSJeff Kirsher 			sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6390bcc9736cSJeff Kirsher 				SWITCH_TX_FLOW_CTRL, 1);
6391bcc9736cSJeff Kirsher 		}
6392bcc9736cSJeff Kirsher 		port_set_link_speed(port);
6393bcc9736cSJeff Kirsher 	} else {
6394bcc9736cSJeff Kirsher 		hw->overrides |= PAUSE_FLOW_CTRL;
6395bcc9736cSJeff Kirsher 		if (hw->ksz_switch) {
6396bcc9736cSJeff Kirsher 			sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6397bcc9736cSJeff Kirsher 				SWITCH_RX_FLOW_CTRL, pause->rx_pause);
6398bcc9736cSJeff Kirsher 			sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6399bcc9736cSJeff Kirsher 				SWITCH_TX_FLOW_CTRL, pause->tx_pause);
6400bcc9736cSJeff Kirsher 		} else
6401bcc9736cSJeff Kirsher 			set_flow_ctrl(hw, pause->rx_pause, pause->tx_pause);
6402bcc9736cSJeff Kirsher 	}
6403bcc9736cSJeff Kirsher 	mutex_unlock(&hw_priv->lock);
6404bcc9736cSJeff Kirsher 
6405bcc9736cSJeff Kirsher 	return 0;
6406bcc9736cSJeff Kirsher }
6407bcc9736cSJeff Kirsher 
6408bcc9736cSJeff Kirsher /**
6409bcc9736cSJeff Kirsher  * netdev_get_ringparam - get tx/rx ring parameters
6410bcc9736cSJeff Kirsher  * @dev:	Network device.
6411bcc9736cSJeff Kirsher  * @pause:	Ethtool RING settings data structure.
6412bcc9736cSJeff Kirsher  *
6413bcc9736cSJeff Kirsher  * This procedure returns the TX/RX ring settings.
6414bcc9736cSJeff Kirsher  */
6415bcc9736cSJeff Kirsher static void netdev_get_ringparam(struct net_device *dev,
6416bcc9736cSJeff Kirsher 	struct ethtool_ringparam *ring)
6417bcc9736cSJeff Kirsher {
6418bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6419bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6420bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6421bcc9736cSJeff Kirsher 
6422bcc9736cSJeff Kirsher 	ring->tx_max_pending = (1 << 9);
6423bcc9736cSJeff Kirsher 	ring->tx_pending = hw->tx_desc_info.alloc;
6424bcc9736cSJeff Kirsher 	ring->rx_max_pending = (1 << 9);
6425bcc9736cSJeff Kirsher 	ring->rx_pending = hw->rx_desc_info.alloc;
6426bcc9736cSJeff Kirsher }
6427bcc9736cSJeff Kirsher 
6428bcc9736cSJeff Kirsher #define STATS_LEN			(TOTAL_PORT_COUNTER_NUM)
6429bcc9736cSJeff Kirsher 
6430bcc9736cSJeff Kirsher static struct {
6431bcc9736cSJeff Kirsher 	char string[ETH_GSTRING_LEN];
6432bcc9736cSJeff Kirsher } ethtool_stats_keys[STATS_LEN] = {
6433bcc9736cSJeff Kirsher 	{ "rx_lo_priority_octets" },
6434bcc9736cSJeff Kirsher 	{ "rx_hi_priority_octets" },
6435bcc9736cSJeff Kirsher 	{ "rx_undersize_packets" },
6436bcc9736cSJeff Kirsher 	{ "rx_fragments" },
6437bcc9736cSJeff Kirsher 	{ "rx_oversize_packets" },
6438bcc9736cSJeff Kirsher 	{ "rx_jabbers" },
6439bcc9736cSJeff Kirsher 	{ "rx_symbol_errors" },
6440bcc9736cSJeff Kirsher 	{ "rx_crc_errors" },
6441bcc9736cSJeff Kirsher 	{ "rx_align_errors" },
6442bcc9736cSJeff Kirsher 	{ "rx_mac_ctrl_packets" },
6443bcc9736cSJeff Kirsher 	{ "rx_pause_packets" },
6444bcc9736cSJeff Kirsher 	{ "rx_bcast_packets" },
6445bcc9736cSJeff Kirsher 	{ "rx_mcast_packets" },
6446bcc9736cSJeff Kirsher 	{ "rx_ucast_packets" },
6447bcc9736cSJeff Kirsher 	{ "rx_64_or_less_octet_packets" },
6448bcc9736cSJeff Kirsher 	{ "rx_65_to_127_octet_packets" },
6449bcc9736cSJeff Kirsher 	{ "rx_128_to_255_octet_packets" },
6450bcc9736cSJeff Kirsher 	{ "rx_256_to_511_octet_packets" },
6451bcc9736cSJeff Kirsher 	{ "rx_512_to_1023_octet_packets" },
6452bcc9736cSJeff Kirsher 	{ "rx_1024_to_1522_octet_packets" },
6453bcc9736cSJeff Kirsher 
6454bcc9736cSJeff Kirsher 	{ "tx_lo_priority_octets" },
6455bcc9736cSJeff Kirsher 	{ "tx_hi_priority_octets" },
6456bcc9736cSJeff Kirsher 	{ "tx_late_collisions" },
6457bcc9736cSJeff Kirsher 	{ "tx_pause_packets" },
6458bcc9736cSJeff Kirsher 	{ "tx_bcast_packets" },
6459bcc9736cSJeff Kirsher 	{ "tx_mcast_packets" },
6460bcc9736cSJeff Kirsher 	{ "tx_ucast_packets" },
6461bcc9736cSJeff Kirsher 	{ "tx_deferred" },
6462bcc9736cSJeff Kirsher 	{ "tx_total_collisions" },
6463bcc9736cSJeff Kirsher 	{ "tx_excessive_collisions" },
6464bcc9736cSJeff Kirsher 	{ "tx_single_collisions" },
6465bcc9736cSJeff Kirsher 	{ "tx_mult_collisions" },
6466bcc9736cSJeff Kirsher 
6467bcc9736cSJeff Kirsher 	{ "rx_discards" },
6468bcc9736cSJeff Kirsher 	{ "tx_discards" },
6469bcc9736cSJeff Kirsher };
6470bcc9736cSJeff Kirsher 
6471bcc9736cSJeff Kirsher /**
6472bcc9736cSJeff Kirsher  * netdev_get_strings - get statistics identity strings
6473bcc9736cSJeff Kirsher  * @dev:	Network device.
6474bcc9736cSJeff Kirsher  * @stringset:	String set identifier.
6475bcc9736cSJeff Kirsher  * @buf:	Buffer to store the strings.
6476bcc9736cSJeff Kirsher  *
6477bcc9736cSJeff Kirsher  * This procedure returns the strings used to identify the statistics.
6478bcc9736cSJeff Kirsher  */
6479bcc9736cSJeff Kirsher static void netdev_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
6480bcc9736cSJeff Kirsher {
6481bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6482bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6483bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6484bcc9736cSJeff Kirsher 
6485bcc9736cSJeff Kirsher 	if (ETH_SS_STATS == stringset)
6486bcc9736cSJeff Kirsher 		memcpy(buf, &ethtool_stats_keys,
6487bcc9736cSJeff Kirsher 			ETH_GSTRING_LEN * hw->mib_cnt);
6488bcc9736cSJeff Kirsher }
6489bcc9736cSJeff Kirsher 
6490bcc9736cSJeff Kirsher /**
6491bcc9736cSJeff Kirsher  * netdev_get_sset_count - get statistics size
6492bcc9736cSJeff Kirsher  * @dev:	Network device.
6493bcc9736cSJeff Kirsher  * @sset:	The statistics set number.
6494bcc9736cSJeff Kirsher  *
6495bcc9736cSJeff Kirsher  * This function returns the size of the statistics to be reported.
6496bcc9736cSJeff Kirsher  *
6497bcc9736cSJeff Kirsher  * Return size of the statistics to be reported.
6498bcc9736cSJeff Kirsher  */
6499bcc9736cSJeff Kirsher static int netdev_get_sset_count(struct net_device *dev, int sset)
6500bcc9736cSJeff Kirsher {
6501bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6502bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6503bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6504bcc9736cSJeff Kirsher 
6505bcc9736cSJeff Kirsher 	switch (sset) {
6506bcc9736cSJeff Kirsher 	case ETH_SS_STATS:
6507bcc9736cSJeff Kirsher 		return hw->mib_cnt;
6508bcc9736cSJeff Kirsher 	default:
6509bcc9736cSJeff Kirsher 		return -EOPNOTSUPP;
6510bcc9736cSJeff Kirsher 	}
6511bcc9736cSJeff Kirsher }
6512bcc9736cSJeff Kirsher 
6513bcc9736cSJeff Kirsher /**
6514bcc9736cSJeff Kirsher  * netdev_get_ethtool_stats - get network device statistics
6515bcc9736cSJeff Kirsher  * @dev:	Network device.
6516bcc9736cSJeff Kirsher  * @stats:	Ethtool statistics data structure.
6517bcc9736cSJeff Kirsher  * @data:	Buffer to store the statistics.
6518bcc9736cSJeff Kirsher  *
6519bcc9736cSJeff Kirsher  * This procedure returns the statistics.
6520bcc9736cSJeff Kirsher  */
6521bcc9736cSJeff Kirsher static void netdev_get_ethtool_stats(struct net_device *dev,
6522bcc9736cSJeff Kirsher 	struct ethtool_stats *stats, u64 *data)
6523bcc9736cSJeff Kirsher {
6524bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6525bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6526bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6527bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
6528bcc9736cSJeff Kirsher 	int n_stats = stats->n_stats;
6529bcc9736cSJeff Kirsher 	int i;
6530bcc9736cSJeff Kirsher 	int n;
6531bcc9736cSJeff Kirsher 	int p;
6532bcc9736cSJeff Kirsher 	int rc;
6533bcc9736cSJeff Kirsher 	u64 counter[TOTAL_PORT_COUNTER_NUM];
6534bcc9736cSJeff Kirsher 
6535bcc9736cSJeff Kirsher 	mutex_lock(&hw_priv->lock);
6536bcc9736cSJeff Kirsher 	n = SWITCH_PORT_NUM;
6537bcc9736cSJeff Kirsher 	for (i = 0, p = port->first_port; i < port->mib_port_cnt; i++, p++) {
6538bcc9736cSJeff Kirsher 		if (media_connected == hw->port_mib[p].state) {
6539bcc9736cSJeff Kirsher 			hw_priv->counter[p].read = 1;
6540bcc9736cSJeff Kirsher 
6541bcc9736cSJeff Kirsher 			/* Remember first port that requests read. */
6542bcc9736cSJeff Kirsher 			if (n == SWITCH_PORT_NUM)
6543bcc9736cSJeff Kirsher 				n = p;
6544bcc9736cSJeff Kirsher 		}
6545bcc9736cSJeff Kirsher 	}
6546bcc9736cSJeff Kirsher 	mutex_unlock(&hw_priv->lock);
6547bcc9736cSJeff Kirsher 
6548bcc9736cSJeff Kirsher 	if (n < SWITCH_PORT_NUM)
6549bcc9736cSJeff Kirsher 		schedule_work(&hw_priv->mib_read);
6550bcc9736cSJeff Kirsher 
6551bcc9736cSJeff Kirsher 	if (1 == port->mib_port_cnt && n < SWITCH_PORT_NUM) {
6552bcc9736cSJeff Kirsher 		p = n;
6553bcc9736cSJeff Kirsher 		rc = wait_event_interruptible_timeout(
6554bcc9736cSJeff Kirsher 			hw_priv->counter[p].counter,
6555bcc9736cSJeff Kirsher 			2 == hw_priv->counter[p].read,
6556bcc9736cSJeff Kirsher 			HZ * 1);
6557bcc9736cSJeff Kirsher 	} else
6558bcc9736cSJeff Kirsher 		for (i = 0, p = n; i < port->mib_port_cnt - n; i++, p++) {
6559bcc9736cSJeff Kirsher 			if (0 == i) {
6560bcc9736cSJeff Kirsher 				rc = wait_event_interruptible_timeout(
6561bcc9736cSJeff Kirsher 					hw_priv->counter[p].counter,
6562bcc9736cSJeff Kirsher 					2 == hw_priv->counter[p].read,
6563bcc9736cSJeff Kirsher 					HZ * 2);
6564bcc9736cSJeff Kirsher 			} else if (hw->port_mib[p].cnt_ptr) {
6565bcc9736cSJeff Kirsher 				rc = wait_event_interruptible_timeout(
6566bcc9736cSJeff Kirsher 					hw_priv->counter[p].counter,
6567bcc9736cSJeff Kirsher 					2 == hw_priv->counter[p].read,
6568bcc9736cSJeff Kirsher 					HZ * 1);
6569bcc9736cSJeff Kirsher 			}
6570bcc9736cSJeff Kirsher 		}
6571bcc9736cSJeff Kirsher 
6572bcc9736cSJeff Kirsher 	get_mib_counters(hw, port->first_port, port->mib_port_cnt, counter);
6573bcc9736cSJeff Kirsher 	n = hw->mib_cnt;
6574bcc9736cSJeff Kirsher 	if (n > n_stats)
6575bcc9736cSJeff Kirsher 		n = n_stats;
6576bcc9736cSJeff Kirsher 	n_stats -= n;
6577bcc9736cSJeff Kirsher 	for (i = 0; i < n; i++)
6578bcc9736cSJeff Kirsher 		*data++ = counter[i];
6579bcc9736cSJeff Kirsher }
6580bcc9736cSJeff Kirsher 
6581bcc9736cSJeff Kirsher /**
6582bcc9736cSJeff Kirsher  * netdev_set_features - set receive checksum support
6583bcc9736cSJeff Kirsher  * @dev:	Network device.
6584bcc9736cSJeff Kirsher  * @features:	New device features (offloads).
6585bcc9736cSJeff Kirsher  *
6586bcc9736cSJeff Kirsher  * This function sets receive checksum support setting.
6587bcc9736cSJeff Kirsher  *
6588bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code.
6589bcc9736cSJeff Kirsher  */
6590bcc9736cSJeff Kirsher static int netdev_set_features(struct net_device *dev, u32 features)
6591bcc9736cSJeff Kirsher {
6592bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6593bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6594bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6595bcc9736cSJeff Kirsher 
6596bcc9736cSJeff Kirsher 	mutex_lock(&hw_priv->lock);
6597bcc9736cSJeff Kirsher 
6598bcc9736cSJeff Kirsher 	/* see note in hw_setup() */
6599bcc9736cSJeff Kirsher 	if (features & NETIF_F_RXCSUM)
6600bcc9736cSJeff Kirsher 		hw->rx_cfg |= DMA_RX_CSUM_TCP | DMA_RX_CSUM_IP;
6601bcc9736cSJeff Kirsher 	else
6602bcc9736cSJeff Kirsher 		hw->rx_cfg &= ~(DMA_RX_CSUM_TCP | DMA_RX_CSUM_IP);
6603bcc9736cSJeff Kirsher 
6604bcc9736cSJeff Kirsher 	if (hw->enabled)
6605bcc9736cSJeff Kirsher 		writel(hw->rx_cfg, hw->io + KS_DMA_RX_CTRL);
6606bcc9736cSJeff Kirsher 
6607bcc9736cSJeff Kirsher 	mutex_unlock(&hw_priv->lock);
6608bcc9736cSJeff Kirsher 
6609bcc9736cSJeff Kirsher 	return 0;
6610bcc9736cSJeff Kirsher }
6611bcc9736cSJeff Kirsher 
6612bcc9736cSJeff Kirsher static struct ethtool_ops netdev_ethtool_ops = {
6613bcc9736cSJeff Kirsher 	.get_settings		= netdev_get_settings,
6614bcc9736cSJeff Kirsher 	.set_settings		= netdev_set_settings,
6615bcc9736cSJeff Kirsher 	.nway_reset		= netdev_nway_reset,
6616bcc9736cSJeff Kirsher 	.get_link		= netdev_get_link,
6617bcc9736cSJeff Kirsher 	.get_drvinfo		= netdev_get_drvinfo,
6618bcc9736cSJeff Kirsher 	.get_regs_len		= netdev_get_regs_len,
6619bcc9736cSJeff Kirsher 	.get_regs		= netdev_get_regs,
6620bcc9736cSJeff Kirsher 	.get_wol		= netdev_get_wol,
6621bcc9736cSJeff Kirsher 	.set_wol		= netdev_set_wol,
6622bcc9736cSJeff Kirsher 	.get_msglevel		= netdev_get_msglevel,
6623bcc9736cSJeff Kirsher 	.set_msglevel		= netdev_set_msglevel,
6624bcc9736cSJeff Kirsher 	.get_eeprom_len		= netdev_get_eeprom_len,
6625bcc9736cSJeff Kirsher 	.get_eeprom		= netdev_get_eeprom,
6626bcc9736cSJeff Kirsher 	.set_eeprom		= netdev_set_eeprom,
6627bcc9736cSJeff Kirsher 	.get_pauseparam		= netdev_get_pauseparam,
6628bcc9736cSJeff Kirsher 	.set_pauseparam		= netdev_set_pauseparam,
6629bcc9736cSJeff Kirsher 	.get_ringparam		= netdev_get_ringparam,
6630bcc9736cSJeff Kirsher 	.get_strings		= netdev_get_strings,
6631bcc9736cSJeff Kirsher 	.get_sset_count		= netdev_get_sset_count,
6632bcc9736cSJeff Kirsher 	.get_ethtool_stats	= netdev_get_ethtool_stats,
6633bcc9736cSJeff Kirsher };
6634bcc9736cSJeff Kirsher 
6635bcc9736cSJeff Kirsher /*
6636bcc9736cSJeff Kirsher  * Hardware monitoring
6637bcc9736cSJeff Kirsher  */
6638bcc9736cSJeff Kirsher 
6639bcc9736cSJeff Kirsher static void update_link(struct net_device *dev, struct dev_priv *priv,
6640bcc9736cSJeff Kirsher 	struct ksz_port *port)
6641bcc9736cSJeff Kirsher {
6642bcc9736cSJeff Kirsher 	if (priv->media_state != port->linked->state) {
6643bcc9736cSJeff Kirsher 		priv->media_state = port->linked->state;
6644bcc9736cSJeff Kirsher 		if (netif_running(dev))
6645bcc9736cSJeff Kirsher 			set_media_state(dev, media_connected);
6646bcc9736cSJeff Kirsher 	}
6647bcc9736cSJeff Kirsher }
6648bcc9736cSJeff Kirsher 
6649bcc9736cSJeff Kirsher static void mib_read_work(struct work_struct *work)
6650bcc9736cSJeff Kirsher {
6651bcc9736cSJeff Kirsher 	struct dev_info *hw_priv =
6652bcc9736cSJeff Kirsher 		container_of(work, struct dev_info, mib_read);
6653bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6654bcc9736cSJeff Kirsher 	struct ksz_port_mib *mib;
6655bcc9736cSJeff Kirsher 	int i;
6656bcc9736cSJeff Kirsher 
6657bcc9736cSJeff Kirsher 	next_jiffies = jiffies;
6658bcc9736cSJeff Kirsher 	for (i = 0; i < hw->mib_port_cnt; i++) {
6659bcc9736cSJeff Kirsher 		mib = &hw->port_mib[i];
6660bcc9736cSJeff Kirsher 
6661bcc9736cSJeff Kirsher 		/* Reading MIB counters or requested to read. */
6662bcc9736cSJeff Kirsher 		if (mib->cnt_ptr || 1 == hw_priv->counter[i].read) {
6663bcc9736cSJeff Kirsher 
6664bcc9736cSJeff Kirsher 			/* Need to process receive interrupt. */
6665bcc9736cSJeff Kirsher 			if (port_r_cnt(hw, i))
6666bcc9736cSJeff Kirsher 				break;
6667bcc9736cSJeff Kirsher 			hw_priv->counter[i].read = 0;
6668bcc9736cSJeff Kirsher 
6669bcc9736cSJeff Kirsher 			/* Finish reading counters. */
6670bcc9736cSJeff Kirsher 			if (0 == mib->cnt_ptr) {
6671bcc9736cSJeff Kirsher 				hw_priv->counter[i].read = 2;
6672bcc9736cSJeff Kirsher 				wake_up_interruptible(
6673bcc9736cSJeff Kirsher 					&hw_priv->counter[i].counter);
6674bcc9736cSJeff Kirsher 			}
6675bcc9736cSJeff Kirsher 		} else if (jiffies >= hw_priv->counter[i].time) {
6676bcc9736cSJeff Kirsher 			/* Only read MIB counters when the port is connected. */
6677bcc9736cSJeff Kirsher 			if (media_connected == mib->state)
6678bcc9736cSJeff Kirsher 				hw_priv->counter[i].read = 1;
6679bcc9736cSJeff Kirsher 			next_jiffies += HZ * 1 * hw->mib_port_cnt;
6680bcc9736cSJeff Kirsher 			hw_priv->counter[i].time = next_jiffies;
6681bcc9736cSJeff Kirsher 
6682bcc9736cSJeff Kirsher 		/* Port is just disconnected. */
6683bcc9736cSJeff Kirsher 		} else if (mib->link_down) {
6684bcc9736cSJeff Kirsher 			mib->link_down = 0;
6685bcc9736cSJeff Kirsher 
6686bcc9736cSJeff Kirsher 			/* Read counters one last time after link is lost. */
6687bcc9736cSJeff Kirsher 			hw_priv->counter[i].read = 1;
6688bcc9736cSJeff Kirsher 		}
6689bcc9736cSJeff Kirsher 	}
6690bcc9736cSJeff Kirsher }
6691bcc9736cSJeff Kirsher 
6692bcc9736cSJeff Kirsher static void mib_monitor(unsigned long ptr)
6693bcc9736cSJeff Kirsher {
6694bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = (struct dev_info *) ptr;
6695bcc9736cSJeff Kirsher 
6696bcc9736cSJeff Kirsher 	mib_read_work(&hw_priv->mib_read);
6697bcc9736cSJeff Kirsher 
6698bcc9736cSJeff Kirsher 	/* This is used to verify Wake-on-LAN is working. */
6699bcc9736cSJeff Kirsher 	if (hw_priv->pme_wait) {
6700bcc9736cSJeff Kirsher 		if (hw_priv->pme_wait <= jiffies) {
6701bcc9736cSJeff Kirsher 			hw_clr_wol_pme_status(&hw_priv->hw);
6702bcc9736cSJeff Kirsher 			hw_priv->pme_wait = 0;
6703bcc9736cSJeff Kirsher 		}
6704bcc9736cSJeff Kirsher 	} else if (hw_chk_wol_pme_status(&hw_priv->hw)) {
6705bcc9736cSJeff Kirsher 
6706bcc9736cSJeff Kirsher 		/* PME is asserted.  Wait 2 seconds to clear it. */
6707bcc9736cSJeff Kirsher 		hw_priv->pme_wait = jiffies + HZ * 2;
6708bcc9736cSJeff Kirsher 	}
6709bcc9736cSJeff Kirsher 
6710bcc9736cSJeff Kirsher 	ksz_update_timer(&hw_priv->mib_timer_info);
6711bcc9736cSJeff Kirsher }
6712bcc9736cSJeff Kirsher 
6713bcc9736cSJeff Kirsher /**
6714bcc9736cSJeff Kirsher  * dev_monitor - periodic monitoring
6715bcc9736cSJeff Kirsher  * @ptr:	Network device pointer.
6716bcc9736cSJeff Kirsher  *
6717bcc9736cSJeff Kirsher  * This routine is run in a kernel timer to monitor the network device.
6718bcc9736cSJeff Kirsher  */
6719bcc9736cSJeff Kirsher static void dev_monitor(unsigned long ptr)
6720bcc9736cSJeff Kirsher {
6721bcc9736cSJeff Kirsher 	struct net_device *dev = (struct net_device *) ptr;
6722bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6723bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = priv->adapter;
6724bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
6725bcc9736cSJeff Kirsher 	struct ksz_port *port = &priv->port;
6726bcc9736cSJeff Kirsher 
6727bcc9736cSJeff Kirsher 	if (!(hw->features & LINK_INT_WORKING))
6728bcc9736cSJeff Kirsher 		port_get_link_speed(port);
6729bcc9736cSJeff Kirsher 	update_link(dev, priv, port);
6730bcc9736cSJeff Kirsher 
6731bcc9736cSJeff Kirsher 	ksz_update_timer(&priv->monitor_timer_info);
6732bcc9736cSJeff Kirsher }
6733bcc9736cSJeff Kirsher 
6734bcc9736cSJeff Kirsher /*
6735bcc9736cSJeff Kirsher  * Linux network device interface functions
6736bcc9736cSJeff Kirsher  */
6737bcc9736cSJeff Kirsher 
6738bcc9736cSJeff Kirsher /* Driver exported variables */
6739bcc9736cSJeff Kirsher 
6740bcc9736cSJeff Kirsher static int msg_enable;
6741bcc9736cSJeff Kirsher 
6742bcc9736cSJeff Kirsher static char *macaddr = ":";
6743bcc9736cSJeff Kirsher static char *mac1addr = ":";
6744bcc9736cSJeff Kirsher 
6745bcc9736cSJeff Kirsher /*
6746bcc9736cSJeff Kirsher  * This enables multiple network device mode for KSZ8842, which contains a
6747bcc9736cSJeff Kirsher  * switch with two physical ports.  Some users like to take control of the
6748bcc9736cSJeff Kirsher  * ports for running Spanning Tree Protocol.  The driver will create an
6749bcc9736cSJeff Kirsher  * additional eth? device for the other port.
6750bcc9736cSJeff Kirsher  *
6751bcc9736cSJeff Kirsher  * Some limitations are the network devices cannot have different MTU and
6752bcc9736cSJeff Kirsher  * multicast hash tables.
6753bcc9736cSJeff Kirsher  */
6754bcc9736cSJeff Kirsher static int multi_dev;
6755bcc9736cSJeff Kirsher 
6756bcc9736cSJeff Kirsher /*
6757bcc9736cSJeff Kirsher  * As most users select multiple network device mode to use Spanning Tree
6758bcc9736cSJeff Kirsher  * Protocol, this enables a feature in which most unicast and multicast packets
6759bcc9736cSJeff Kirsher  * are forwarded inside the switch and not passed to the host.  Only packets
6760bcc9736cSJeff Kirsher  * that need the host's attention are passed to it.  This prevents the host
6761bcc9736cSJeff Kirsher  * wasting CPU time to examine each and every incoming packets and do the
6762bcc9736cSJeff Kirsher  * forwarding itself.
6763bcc9736cSJeff Kirsher  *
6764bcc9736cSJeff Kirsher  * As the hack requires the private bridge header, the driver cannot compile
6765bcc9736cSJeff Kirsher  * with just the kernel headers.
6766bcc9736cSJeff Kirsher  *
6767bcc9736cSJeff Kirsher  * Enabling STP support also turns on multiple network device mode.
6768bcc9736cSJeff Kirsher  */
6769bcc9736cSJeff Kirsher static int stp;
6770bcc9736cSJeff Kirsher 
6771bcc9736cSJeff Kirsher /*
6772bcc9736cSJeff Kirsher  * This enables fast aging in the KSZ8842 switch.  Not sure what situation
6773bcc9736cSJeff Kirsher  * needs that.  However, fast aging is used to flush the dynamic MAC table when
6774bcc9736cSJeff Kirsher  * STP suport is enabled.
6775bcc9736cSJeff Kirsher  */
6776bcc9736cSJeff Kirsher static int fast_aging;
6777bcc9736cSJeff Kirsher 
6778bcc9736cSJeff Kirsher /**
6779bcc9736cSJeff Kirsher  * netdev_init - initialize network device.
6780bcc9736cSJeff Kirsher  * @dev:	Network device.
6781bcc9736cSJeff Kirsher  *
6782bcc9736cSJeff Kirsher  * This function initializes the network device.
6783bcc9736cSJeff Kirsher  *
6784bcc9736cSJeff Kirsher  * Return 0 if successful; otherwise an error code indicating failure.
6785bcc9736cSJeff Kirsher  */
6786bcc9736cSJeff Kirsher static int __init netdev_init(struct net_device *dev)
6787bcc9736cSJeff Kirsher {
6788bcc9736cSJeff Kirsher 	struct dev_priv *priv = netdev_priv(dev);
6789bcc9736cSJeff Kirsher 
6790bcc9736cSJeff Kirsher 	/* 500 ms timeout */
6791bcc9736cSJeff Kirsher 	ksz_init_timer(&priv->monitor_timer_info, 500 * HZ / 1000,
6792bcc9736cSJeff Kirsher 		dev_monitor, dev);
6793bcc9736cSJeff Kirsher 
6794bcc9736cSJeff Kirsher 	/* 500 ms timeout */
6795bcc9736cSJeff Kirsher 	dev->watchdog_timeo = HZ / 2;
6796bcc9736cSJeff Kirsher 
6797bcc9736cSJeff Kirsher 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_RXCSUM;
6798bcc9736cSJeff Kirsher 
6799bcc9736cSJeff Kirsher 	/*
6800bcc9736cSJeff Kirsher 	 * Hardware does not really support IPv6 checksum generation, but
6801bcc9736cSJeff Kirsher 	 * driver actually runs faster with this on.
6802bcc9736cSJeff Kirsher 	 */
6803bcc9736cSJeff Kirsher 	dev->hw_features |= NETIF_F_IPV6_CSUM;
6804bcc9736cSJeff Kirsher 
6805bcc9736cSJeff Kirsher 	dev->features |= dev->hw_features;
6806bcc9736cSJeff Kirsher 
6807bcc9736cSJeff Kirsher 	sema_init(&priv->proc_sem, 1);
6808bcc9736cSJeff Kirsher 
6809bcc9736cSJeff Kirsher 	priv->mii_if.phy_id_mask = 0x1;
6810bcc9736cSJeff Kirsher 	priv->mii_if.reg_num_mask = 0x7;
6811bcc9736cSJeff Kirsher 	priv->mii_if.dev = dev;
6812bcc9736cSJeff Kirsher 	priv->mii_if.mdio_read = mdio_read;
6813bcc9736cSJeff Kirsher 	priv->mii_if.mdio_write = mdio_write;
6814bcc9736cSJeff Kirsher 	priv->mii_if.phy_id = priv->port.first_port + 1;
6815bcc9736cSJeff Kirsher 
6816bcc9736cSJeff Kirsher 	priv->msg_enable = netif_msg_init(msg_enable,
6817bcc9736cSJeff Kirsher 		(NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK));
6818bcc9736cSJeff Kirsher 
6819bcc9736cSJeff Kirsher 	return 0;
6820bcc9736cSJeff Kirsher }
6821bcc9736cSJeff Kirsher 
6822bcc9736cSJeff Kirsher static const struct net_device_ops netdev_ops = {
6823bcc9736cSJeff Kirsher 	.ndo_init		= netdev_init,
6824bcc9736cSJeff Kirsher 	.ndo_open		= netdev_open,
6825bcc9736cSJeff Kirsher 	.ndo_stop		= netdev_close,
6826bcc9736cSJeff Kirsher 	.ndo_get_stats		= netdev_query_statistics,
6827bcc9736cSJeff Kirsher 	.ndo_start_xmit		= netdev_tx,
6828bcc9736cSJeff Kirsher 	.ndo_tx_timeout		= netdev_tx_timeout,
6829bcc9736cSJeff Kirsher 	.ndo_change_mtu		= netdev_change_mtu,
6830bcc9736cSJeff Kirsher 	.ndo_set_features	= netdev_set_features,
6831bcc9736cSJeff Kirsher 	.ndo_set_mac_address	= netdev_set_mac_address,
6832bcc9736cSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
6833bcc9736cSJeff Kirsher 	.ndo_do_ioctl		= netdev_ioctl,
6834bcc9736cSJeff Kirsher 	.ndo_set_rx_mode	= netdev_set_rx_mode,
6835bcc9736cSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
6836bcc9736cSJeff Kirsher 	.ndo_poll_controller	= netdev_netpoll,
6837bcc9736cSJeff Kirsher #endif
6838bcc9736cSJeff Kirsher };
6839bcc9736cSJeff Kirsher 
6840bcc9736cSJeff Kirsher static void netdev_free(struct net_device *dev)
6841bcc9736cSJeff Kirsher {
6842bcc9736cSJeff Kirsher 	if (dev->watchdog_timeo)
6843bcc9736cSJeff Kirsher 		unregister_netdev(dev);
6844bcc9736cSJeff Kirsher 
6845bcc9736cSJeff Kirsher 	free_netdev(dev);
6846bcc9736cSJeff Kirsher }
6847bcc9736cSJeff Kirsher 
6848bcc9736cSJeff Kirsher struct platform_info {
6849bcc9736cSJeff Kirsher 	struct dev_info dev_info;
6850bcc9736cSJeff Kirsher 	struct net_device *netdev[SWITCH_PORT_NUM];
6851bcc9736cSJeff Kirsher };
6852bcc9736cSJeff Kirsher 
6853bcc9736cSJeff Kirsher static int net_device_present;
6854bcc9736cSJeff Kirsher 
6855bcc9736cSJeff Kirsher static void get_mac_addr(struct dev_info *hw_priv, u8 *macaddr, int port)
6856bcc9736cSJeff Kirsher {
6857bcc9736cSJeff Kirsher 	int i;
6858bcc9736cSJeff Kirsher 	int j;
6859bcc9736cSJeff Kirsher 	int got_num;
6860bcc9736cSJeff Kirsher 	int num;
6861bcc9736cSJeff Kirsher 
6862bcc9736cSJeff Kirsher 	i = j = num = got_num = 0;
6863bcc9736cSJeff Kirsher 	while (j < MAC_ADDR_LEN) {
6864bcc9736cSJeff Kirsher 		if (macaddr[i]) {
6865bcc9736cSJeff Kirsher 			int digit;
6866bcc9736cSJeff Kirsher 
6867bcc9736cSJeff Kirsher 			got_num = 1;
6868bcc9736cSJeff Kirsher 			digit = hex_to_bin(macaddr[i]);
6869bcc9736cSJeff Kirsher 			if (digit >= 0)
6870bcc9736cSJeff Kirsher 				num = num * 16 + digit;
6871bcc9736cSJeff Kirsher 			else if (':' == macaddr[i])
6872bcc9736cSJeff Kirsher 				got_num = 2;
6873bcc9736cSJeff Kirsher 			else
6874bcc9736cSJeff Kirsher 				break;
6875bcc9736cSJeff Kirsher 		} else if (got_num)
6876bcc9736cSJeff Kirsher 			got_num = 2;
6877bcc9736cSJeff Kirsher 		else
6878bcc9736cSJeff Kirsher 			break;
6879bcc9736cSJeff Kirsher 		if (2 == got_num) {
6880bcc9736cSJeff Kirsher 			if (MAIN_PORT == port) {
6881bcc9736cSJeff Kirsher 				hw_priv->hw.override_addr[j++] = (u8) num;
6882bcc9736cSJeff Kirsher 				hw_priv->hw.override_addr[5] +=
6883bcc9736cSJeff Kirsher 					hw_priv->hw.id;
6884bcc9736cSJeff Kirsher 			} else {
6885bcc9736cSJeff Kirsher 				hw_priv->hw.ksz_switch->other_addr[j++] =
6886bcc9736cSJeff Kirsher 					(u8) num;
6887bcc9736cSJeff Kirsher 				hw_priv->hw.ksz_switch->other_addr[5] +=
6888bcc9736cSJeff Kirsher 					hw_priv->hw.id;
6889bcc9736cSJeff Kirsher 			}
6890bcc9736cSJeff Kirsher 			num = got_num = 0;
6891bcc9736cSJeff Kirsher 		}
6892bcc9736cSJeff Kirsher 		i++;
6893bcc9736cSJeff Kirsher 	}
6894bcc9736cSJeff Kirsher 	if (MAC_ADDR_LEN == j) {
6895bcc9736cSJeff Kirsher 		if (MAIN_PORT == port)
6896bcc9736cSJeff Kirsher 			hw_priv->hw.mac_override = 1;
6897bcc9736cSJeff Kirsher 	}
6898bcc9736cSJeff Kirsher }
6899bcc9736cSJeff Kirsher 
6900bcc9736cSJeff Kirsher #define KS884X_DMA_MASK			(~0x0UL)
6901bcc9736cSJeff Kirsher 
6902bcc9736cSJeff Kirsher static void read_other_addr(struct ksz_hw *hw)
6903bcc9736cSJeff Kirsher {
6904bcc9736cSJeff Kirsher 	int i;
6905bcc9736cSJeff Kirsher 	u16 data[3];
6906bcc9736cSJeff Kirsher 	struct ksz_switch *sw = hw->ksz_switch;
6907bcc9736cSJeff Kirsher 
6908bcc9736cSJeff Kirsher 	for (i = 0; i < 3; i++)
6909bcc9736cSJeff Kirsher 		data[i] = eeprom_read(hw, i + EEPROM_DATA_OTHER_MAC_ADDR);
6910bcc9736cSJeff Kirsher 	if ((data[0] || data[1] || data[2]) && data[0] != 0xffff) {
6911bcc9736cSJeff Kirsher 		sw->other_addr[5] = (u8) data[0];
6912bcc9736cSJeff Kirsher 		sw->other_addr[4] = (u8)(data[0] >> 8);
6913bcc9736cSJeff Kirsher 		sw->other_addr[3] = (u8) data[1];
6914bcc9736cSJeff Kirsher 		sw->other_addr[2] = (u8)(data[1] >> 8);
6915bcc9736cSJeff Kirsher 		sw->other_addr[1] = (u8) data[2];
6916bcc9736cSJeff Kirsher 		sw->other_addr[0] = (u8)(data[2] >> 8);
6917bcc9736cSJeff Kirsher 	}
6918bcc9736cSJeff Kirsher }
6919bcc9736cSJeff Kirsher 
6920bcc9736cSJeff Kirsher #ifndef PCI_VENDOR_ID_MICREL_KS
6921bcc9736cSJeff Kirsher #define PCI_VENDOR_ID_MICREL_KS		0x16c6
6922bcc9736cSJeff Kirsher #endif
6923bcc9736cSJeff Kirsher 
6924bcc9736cSJeff Kirsher static int __devinit pcidev_init(struct pci_dev *pdev,
6925bcc9736cSJeff Kirsher 	const struct pci_device_id *id)
6926bcc9736cSJeff Kirsher {
6927bcc9736cSJeff Kirsher 	struct net_device *dev;
6928bcc9736cSJeff Kirsher 	struct dev_priv *priv;
6929bcc9736cSJeff Kirsher 	struct dev_info *hw_priv;
6930bcc9736cSJeff Kirsher 	struct ksz_hw *hw;
6931bcc9736cSJeff Kirsher 	struct platform_info *info;
6932bcc9736cSJeff Kirsher 	struct ksz_port *port;
6933bcc9736cSJeff Kirsher 	unsigned long reg_base;
6934bcc9736cSJeff Kirsher 	unsigned long reg_len;
6935bcc9736cSJeff Kirsher 	int cnt;
6936bcc9736cSJeff Kirsher 	int i;
6937bcc9736cSJeff Kirsher 	int mib_port_count;
6938bcc9736cSJeff Kirsher 	int pi;
6939bcc9736cSJeff Kirsher 	int port_count;
6940bcc9736cSJeff Kirsher 	int result;
6941bcc9736cSJeff Kirsher 	char banner[sizeof(version)];
6942bcc9736cSJeff Kirsher 	struct ksz_switch *sw = NULL;
6943bcc9736cSJeff Kirsher 
6944bcc9736cSJeff Kirsher 	result = pci_enable_device(pdev);
6945bcc9736cSJeff Kirsher 	if (result)
6946bcc9736cSJeff Kirsher 		return result;
6947bcc9736cSJeff Kirsher 
6948bcc9736cSJeff Kirsher 	result = -ENODEV;
6949bcc9736cSJeff Kirsher 
6950bcc9736cSJeff Kirsher 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) ||
6951bcc9736cSJeff Kirsher 			pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
6952bcc9736cSJeff Kirsher 		return result;
6953bcc9736cSJeff Kirsher 
6954bcc9736cSJeff Kirsher 	reg_base = pci_resource_start(pdev, 0);
6955bcc9736cSJeff Kirsher 	reg_len = pci_resource_len(pdev, 0);
6956bcc9736cSJeff Kirsher 	if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0)
6957bcc9736cSJeff Kirsher 		return result;
6958bcc9736cSJeff Kirsher 
6959bcc9736cSJeff Kirsher 	if (!request_mem_region(reg_base, reg_len, DRV_NAME))
6960bcc9736cSJeff Kirsher 		return result;
6961bcc9736cSJeff Kirsher 	pci_set_master(pdev);
6962bcc9736cSJeff Kirsher 
6963bcc9736cSJeff Kirsher 	result = -ENOMEM;
6964bcc9736cSJeff Kirsher 
6965bcc9736cSJeff Kirsher 	info = kzalloc(sizeof(struct platform_info), GFP_KERNEL);
6966bcc9736cSJeff Kirsher 	if (!info)
6967bcc9736cSJeff Kirsher 		goto pcidev_init_dev_err;
6968bcc9736cSJeff Kirsher 
6969bcc9736cSJeff Kirsher 	hw_priv = &info->dev_info;
6970bcc9736cSJeff Kirsher 	hw_priv->pdev = pdev;
6971bcc9736cSJeff Kirsher 
6972bcc9736cSJeff Kirsher 	hw = &hw_priv->hw;
6973bcc9736cSJeff Kirsher 
6974bcc9736cSJeff Kirsher 	hw->io = ioremap(reg_base, reg_len);
6975bcc9736cSJeff Kirsher 	if (!hw->io)
6976bcc9736cSJeff Kirsher 		goto pcidev_init_io_err;
6977bcc9736cSJeff Kirsher 
6978bcc9736cSJeff Kirsher 	cnt = hw_init(hw);
6979bcc9736cSJeff Kirsher 	if (!cnt) {
6980bcc9736cSJeff Kirsher 		if (msg_enable & NETIF_MSG_PROBE)
6981bcc9736cSJeff Kirsher 			pr_alert("chip not detected\n");
6982bcc9736cSJeff Kirsher 		result = -ENODEV;
6983bcc9736cSJeff Kirsher 		goto pcidev_init_alloc_err;
6984bcc9736cSJeff Kirsher 	}
6985bcc9736cSJeff Kirsher 
6986bcc9736cSJeff Kirsher 	snprintf(banner, sizeof(banner), "%s", version);
6987bcc9736cSJeff Kirsher 	banner[13] = cnt + '0';		/* Replace x in "Micrel KSZ884x" */
6988bcc9736cSJeff Kirsher 	dev_info(&hw_priv->pdev->dev, "%s\n", banner);
6989bcc9736cSJeff Kirsher 	dev_dbg(&hw_priv->pdev->dev, "Mem = %p; IRQ = %d\n", hw->io, pdev->irq);
6990bcc9736cSJeff Kirsher 
6991bcc9736cSJeff Kirsher 	/* Assume device is KSZ8841. */
6992bcc9736cSJeff Kirsher 	hw->dev_count = 1;
6993bcc9736cSJeff Kirsher 	port_count = 1;
6994bcc9736cSJeff Kirsher 	mib_port_count = 1;
6995bcc9736cSJeff Kirsher 	hw->addr_list_size = 0;
6996bcc9736cSJeff Kirsher 	hw->mib_cnt = PORT_COUNTER_NUM;
6997bcc9736cSJeff Kirsher 	hw->mib_port_cnt = 1;
6998bcc9736cSJeff Kirsher 
6999bcc9736cSJeff Kirsher 	/* KSZ8842 has a switch with multiple ports. */
7000bcc9736cSJeff Kirsher 	if (2 == cnt) {
7001bcc9736cSJeff Kirsher 		if (fast_aging)
7002bcc9736cSJeff Kirsher 			hw->overrides |= FAST_AGING;
7003bcc9736cSJeff Kirsher 
7004bcc9736cSJeff Kirsher 		hw->mib_cnt = TOTAL_PORT_COUNTER_NUM;
7005bcc9736cSJeff Kirsher 
7006bcc9736cSJeff Kirsher 		/* Multiple network device interfaces are required. */
7007bcc9736cSJeff Kirsher 		if (multi_dev) {
7008bcc9736cSJeff Kirsher 			hw->dev_count = SWITCH_PORT_NUM;
7009bcc9736cSJeff Kirsher 			hw->addr_list_size = SWITCH_PORT_NUM - 1;
7010bcc9736cSJeff Kirsher 		}
7011bcc9736cSJeff Kirsher 
7012bcc9736cSJeff Kirsher 		/* Single network device has multiple ports. */
7013bcc9736cSJeff Kirsher 		if (1 == hw->dev_count) {
7014bcc9736cSJeff Kirsher 			port_count = SWITCH_PORT_NUM;
7015bcc9736cSJeff Kirsher 			mib_port_count = SWITCH_PORT_NUM;
7016bcc9736cSJeff Kirsher 		}
7017bcc9736cSJeff Kirsher 		hw->mib_port_cnt = TOTAL_PORT_NUM;
7018bcc9736cSJeff Kirsher 		hw->ksz_switch = kzalloc(sizeof(struct ksz_switch), GFP_KERNEL);
7019bcc9736cSJeff Kirsher 		if (!hw->ksz_switch)
7020bcc9736cSJeff Kirsher 			goto pcidev_init_alloc_err;
7021bcc9736cSJeff Kirsher 
7022bcc9736cSJeff Kirsher 		sw = hw->ksz_switch;
7023bcc9736cSJeff Kirsher 	}
7024bcc9736cSJeff Kirsher 	for (i = 0; i < hw->mib_port_cnt; i++)
7025bcc9736cSJeff Kirsher 		hw->port_mib[i].mib_start = 0;
7026bcc9736cSJeff Kirsher 
7027bcc9736cSJeff Kirsher 	hw->parent = hw_priv;
7028bcc9736cSJeff Kirsher 
7029bcc9736cSJeff Kirsher 	/* Default MTU is 1500. */
7030bcc9736cSJeff Kirsher 	hw_priv->mtu = (REGULAR_RX_BUF_SIZE + 3) & ~3;
7031bcc9736cSJeff Kirsher 
7032bcc9736cSJeff Kirsher 	if (ksz_alloc_mem(hw_priv))
7033bcc9736cSJeff Kirsher 		goto pcidev_init_mem_err;
7034bcc9736cSJeff Kirsher 
7035bcc9736cSJeff Kirsher 	hw_priv->hw.id = net_device_present;
7036bcc9736cSJeff Kirsher 
7037bcc9736cSJeff Kirsher 	spin_lock_init(&hw_priv->hwlock);
7038bcc9736cSJeff Kirsher 	mutex_init(&hw_priv->lock);
7039bcc9736cSJeff Kirsher 
7040bcc9736cSJeff Kirsher 	/* tasklet is enabled. */
7041bcc9736cSJeff Kirsher 	tasklet_init(&hw_priv->rx_tasklet, rx_proc_task,
7042bcc9736cSJeff Kirsher 		(unsigned long) hw_priv);
7043bcc9736cSJeff Kirsher 	tasklet_init(&hw_priv->tx_tasklet, tx_proc_task,
7044bcc9736cSJeff Kirsher 		(unsigned long) hw_priv);
7045bcc9736cSJeff Kirsher 
7046bcc9736cSJeff Kirsher 	/* tasklet_enable will decrement the atomic counter. */
7047bcc9736cSJeff Kirsher 	tasklet_disable(&hw_priv->rx_tasklet);
7048bcc9736cSJeff Kirsher 	tasklet_disable(&hw_priv->tx_tasklet);
7049bcc9736cSJeff Kirsher 
7050bcc9736cSJeff Kirsher 	for (i = 0; i < TOTAL_PORT_NUM; i++)
7051bcc9736cSJeff Kirsher 		init_waitqueue_head(&hw_priv->counter[i].counter);
7052bcc9736cSJeff Kirsher 
7053bcc9736cSJeff Kirsher 	if (macaddr[0] != ':')
7054bcc9736cSJeff Kirsher 		get_mac_addr(hw_priv, macaddr, MAIN_PORT);
7055bcc9736cSJeff Kirsher 
7056bcc9736cSJeff Kirsher 	/* Read MAC address and initialize override address if not overrided. */
7057bcc9736cSJeff Kirsher 	hw_read_addr(hw);
7058bcc9736cSJeff Kirsher 
7059bcc9736cSJeff Kirsher 	/* Multiple device interfaces mode requires a second MAC address. */
7060bcc9736cSJeff Kirsher 	if (hw->dev_count > 1) {
7061bcc9736cSJeff Kirsher 		memcpy(sw->other_addr, hw->override_addr, MAC_ADDR_LEN);
7062bcc9736cSJeff Kirsher 		read_other_addr(hw);
7063bcc9736cSJeff Kirsher 		if (mac1addr[0] != ':')
7064bcc9736cSJeff Kirsher 			get_mac_addr(hw_priv, mac1addr, OTHER_PORT);
7065bcc9736cSJeff Kirsher 	}
7066bcc9736cSJeff Kirsher 
7067bcc9736cSJeff Kirsher 	hw_setup(hw);
7068bcc9736cSJeff Kirsher 	if (hw->ksz_switch)
7069bcc9736cSJeff Kirsher 		sw_setup(hw);
7070bcc9736cSJeff Kirsher 	else {
7071bcc9736cSJeff Kirsher 		hw_priv->wol_support = WOL_SUPPORT;
7072bcc9736cSJeff Kirsher 		hw_priv->wol_enable = 0;
7073bcc9736cSJeff Kirsher 	}
7074bcc9736cSJeff Kirsher 
7075bcc9736cSJeff Kirsher 	INIT_WORK(&hw_priv->mib_read, mib_read_work);
7076bcc9736cSJeff Kirsher 
7077bcc9736cSJeff Kirsher 	/* 500 ms timeout */
7078bcc9736cSJeff Kirsher 	ksz_init_timer(&hw_priv->mib_timer_info, 500 * HZ / 1000,
7079bcc9736cSJeff Kirsher 		mib_monitor, hw_priv);
7080bcc9736cSJeff Kirsher 
7081bcc9736cSJeff Kirsher 	for (i = 0; i < hw->dev_count; i++) {
7082bcc9736cSJeff Kirsher 		dev = alloc_etherdev(sizeof(struct dev_priv));
7083bcc9736cSJeff Kirsher 		if (!dev)
7084bcc9736cSJeff Kirsher 			goto pcidev_init_reg_err;
7085bcc9736cSJeff Kirsher 		info->netdev[i] = dev;
7086bcc9736cSJeff Kirsher 
7087bcc9736cSJeff Kirsher 		priv = netdev_priv(dev);
7088bcc9736cSJeff Kirsher 		priv->adapter = hw_priv;
7089bcc9736cSJeff Kirsher 		priv->id = net_device_present++;
7090bcc9736cSJeff Kirsher 
7091bcc9736cSJeff Kirsher 		port = &priv->port;
7092bcc9736cSJeff Kirsher 		port->port_cnt = port_count;
7093bcc9736cSJeff Kirsher 		port->mib_port_cnt = mib_port_count;
7094bcc9736cSJeff Kirsher 		port->first_port = i;
7095bcc9736cSJeff Kirsher 		port->flow_ctrl = PHY_FLOW_CTRL;
7096bcc9736cSJeff Kirsher 
7097bcc9736cSJeff Kirsher 		port->hw = hw;
7098bcc9736cSJeff Kirsher 		port->linked = &hw->port_info[port->first_port];
7099bcc9736cSJeff Kirsher 
7100bcc9736cSJeff Kirsher 		for (cnt = 0, pi = i; cnt < port_count; cnt++, pi++) {
7101bcc9736cSJeff Kirsher 			hw->port_info[pi].port_id = pi;
7102bcc9736cSJeff Kirsher 			hw->port_info[pi].pdev = dev;
7103bcc9736cSJeff Kirsher 			hw->port_info[pi].state = media_disconnected;
7104bcc9736cSJeff Kirsher 		}
7105bcc9736cSJeff Kirsher 
7106bcc9736cSJeff Kirsher 		dev->mem_start = (unsigned long) hw->io;
7107bcc9736cSJeff Kirsher 		dev->mem_end = dev->mem_start + reg_len - 1;
7108bcc9736cSJeff Kirsher 		dev->irq = pdev->irq;
7109bcc9736cSJeff Kirsher 		if (MAIN_PORT == i)
7110bcc9736cSJeff Kirsher 			memcpy(dev->dev_addr, hw_priv->hw.override_addr,
7111bcc9736cSJeff Kirsher 				MAC_ADDR_LEN);
7112bcc9736cSJeff Kirsher 		else {
7113bcc9736cSJeff Kirsher 			memcpy(dev->dev_addr, sw->other_addr,
7114bcc9736cSJeff Kirsher 				MAC_ADDR_LEN);
7115bcc9736cSJeff Kirsher 			if (!memcmp(sw->other_addr, hw->override_addr,
7116bcc9736cSJeff Kirsher 					MAC_ADDR_LEN))
7117bcc9736cSJeff Kirsher 				dev->dev_addr[5] += port->first_port;
7118bcc9736cSJeff Kirsher 		}
7119bcc9736cSJeff Kirsher 
7120bcc9736cSJeff Kirsher 		dev->netdev_ops = &netdev_ops;
7121bcc9736cSJeff Kirsher 		SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
7122bcc9736cSJeff Kirsher 		if (register_netdev(dev))
7123bcc9736cSJeff Kirsher 			goto pcidev_init_reg_err;
7124bcc9736cSJeff Kirsher 		port_set_power_saving(port, true);
7125bcc9736cSJeff Kirsher 	}
7126bcc9736cSJeff Kirsher 
7127bcc9736cSJeff Kirsher 	pci_dev_get(hw_priv->pdev);
7128bcc9736cSJeff Kirsher 	pci_set_drvdata(pdev, info);
7129bcc9736cSJeff Kirsher 	return 0;
7130bcc9736cSJeff Kirsher 
7131bcc9736cSJeff Kirsher pcidev_init_reg_err:
7132bcc9736cSJeff Kirsher 	for (i = 0; i < hw->dev_count; i++) {
7133bcc9736cSJeff Kirsher 		if (info->netdev[i]) {
7134bcc9736cSJeff Kirsher 			netdev_free(info->netdev[i]);
7135bcc9736cSJeff Kirsher 			info->netdev[i] = NULL;
7136bcc9736cSJeff Kirsher 		}
7137bcc9736cSJeff Kirsher 	}
7138bcc9736cSJeff Kirsher 
7139bcc9736cSJeff Kirsher pcidev_init_mem_err:
7140bcc9736cSJeff Kirsher 	ksz_free_mem(hw_priv);
7141bcc9736cSJeff Kirsher 	kfree(hw->ksz_switch);
7142bcc9736cSJeff Kirsher 
7143bcc9736cSJeff Kirsher pcidev_init_alloc_err:
7144bcc9736cSJeff Kirsher 	iounmap(hw->io);
7145bcc9736cSJeff Kirsher 
7146bcc9736cSJeff Kirsher pcidev_init_io_err:
7147bcc9736cSJeff Kirsher 	kfree(info);
7148bcc9736cSJeff Kirsher 
7149bcc9736cSJeff Kirsher pcidev_init_dev_err:
7150bcc9736cSJeff Kirsher 	release_mem_region(reg_base, reg_len);
7151bcc9736cSJeff Kirsher 
7152bcc9736cSJeff Kirsher 	return result;
7153bcc9736cSJeff Kirsher }
7154bcc9736cSJeff Kirsher 
7155bcc9736cSJeff Kirsher static void pcidev_exit(struct pci_dev *pdev)
7156bcc9736cSJeff Kirsher {
7157bcc9736cSJeff Kirsher 	int i;
7158bcc9736cSJeff Kirsher 	struct platform_info *info = pci_get_drvdata(pdev);
7159bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = &info->dev_info;
7160bcc9736cSJeff Kirsher 
7161bcc9736cSJeff Kirsher 	pci_set_drvdata(pdev, NULL);
7162bcc9736cSJeff Kirsher 
7163bcc9736cSJeff Kirsher 	release_mem_region(pci_resource_start(pdev, 0),
7164bcc9736cSJeff Kirsher 		pci_resource_len(pdev, 0));
7165bcc9736cSJeff Kirsher 	for (i = 0; i < hw_priv->hw.dev_count; i++) {
7166bcc9736cSJeff Kirsher 		if (info->netdev[i])
7167bcc9736cSJeff Kirsher 			netdev_free(info->netdev[i]);
7168bcc9736cSJeff Kirsher 	}
7169bcc9736cSJeff Kirsher 	if (hw_priv->hw.io)
7170bcc9736cSJeff Kirsher 		iounmap(hw_priv->hw.io);
7171bcc9736cSJeff Kirsher 	ksz_free_mem(hw_priv);
7172bcc9736cSJeff Kirsher 	kfree(hw_priv->hw.ksz_switch);
7173bcc9736cSJeff Kirsher 	pci_dev_put(hw_priv->pdev);
7174bcc9736cSJeff Kirsher 	kfree(info);
7175bcc9736cSJeff Kirsher }
7176bcc9736cSJeff Kirsher 
7177bcc9736cSJeff Kirsher #ifdef CONFIG_PM
7178bcc9736cSJeff Kirsher static int pcidev_resume(struct pci_dev *pdev)
7179bcc9736cSJeff Kirsher {
7180bcc9736cSJeff Kirsher 	int i;
7181bcc9736cSJeff Kirsher 	struct platform_info *info = pci_get_drvdata(pdev);
7182bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = &info->dev_info;
7183bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
7184bcc9736cSJeff Kirsher 
7185bcc9736cSJeff Kirsher 	pci_set_power_state(pdev, PCI_D0);
7186bcc9736cSJeff Kirsher 	pci_restore_state(pdev);
7187bcc9736cSJeff Kirsher 	pci_enable_wake(pdev, PCI_D0, 0);
7188bcc9736cSJeff Kirsher 
7189bcc9736cSJeff Kirsher 	if (hw_priv->wol_enable)
7190bcc9736cSJeff Kirsher 		hw_cfg_wol_pme(hw, 0);
7191bcc9736cSJeff Kirsher 	for (i = 0; i < hw->dev_count; i++) {
7192bcc9736cSJeff Kirsher 		if (info->netdev[i]) {
7193bcc9736cSJeff Kirsher 			struct net_device *dev = info->netdev[i];
7194bcc9736cSJeff Kirsher 
7195bcc9736cSJeff Kirsher 			if (netif_running(dev)) {
7196bcc9736cSJeff Kirsher 				netdev_open(dev);
7197bcc9736cSJeff Kirsher 				netif_device_attach(dev);
7198bcc9736cSJeff Kirsher 			}
7199bcc9736cSJeff Kirsher 		}
7200bcc9736cSJeff Kirsher 	}
7201bcc9736cSJeff Kirsher 	return 0;
7202bcc9736cSJeff Kirsher }
7203bcc9736cSJeff Kirsher 
7204bcc9736cSJeff Kirsher static int pcidev_suspend(struct pci_dev *pdev, pm_message_t state)
7205bcc9736cSJeff Kirsher {
7206bcc9736cSJeff Kirsher 	int i;
7207bcc9736cSJeff Kirsher 	struct platform_info *info = pci_get_drvdata(pdev);
7208bcc9736cSJeff Kirsher 	struct dev_info *hw_priv = &info->dev_info;
7209bcc9736cSJeff Kirsher 	struct ksz_hw *hw = &hw_priv->hw;
7210bcc9736cSJeff Kirsher 
7211bcc9736cSJeff Kirsher 	/* Need to find a way to retrieve the device IP address. */
7212bcc9736cSJeff Kirsher 	static const u8 net_addr[] = { 192, 168, 1, 1 };
7213bcc9736cSJeff Kirsher 
7214bcc9736cSJeff Kirsher 	for (i = 0; i < hw->dev_count; i++) {
7215bcc9736cSJeff Kirsher 		if (info->netdev[i]) {
7216bcc9736cSJeff Kirsher 			struct net_device *dev = info->netdev[i];
7217bcc9736cSJeff Kirsher 
7218bcc9736cSJeff Kirsher 			if (netif_running(dev)) {
7219bcc9736cSJeff Kirsher 				netif_device_detach(dev);
7220bcc9736cSJeff Kirsher 				netdev_close(dev);
7221bcc9736cSJeff Kirsher 			}
7222bcc9736cSJeff Kirsher 		}
7223bcc9736cSJeff Kirsher 	}
7224bcc9736cSJeff Kirsher 	if (hw_priv->wol_enable) {
7225bcc9736cSJeff Kirsher 		hw_enable_wol(hw, hw_priv->wol_enable, net_addr);
7226bcc9736cSJeff Kirsher 		hw_cfg_wol_pme(hw, 1);
7227bcc9736cSJeff Kirsher 	}
7228bcc9736cSJeff Kirsher 
7229bcc9736cSJeff Kirsher 	pci_save_state(pdev);
7230bcc9736cSJeff Kirsher 	pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
7231bcc9736cSJeff Kirsher 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
7232bcc9736cSJeff Kirsher 	return 0;
7233bcc9736cSJeff Kirsher }
7234bcc9736cSJeff Kirsher #endif
7235bcc9736cSJeff Kirsher 
7236bcc9736cSJeff Kirsher static char pcidev_name[] = "ksz884xp";
7237bcc9736cSJeff Kirsher 
7238bcc9736cSJeff Kirsher static struct pci_device_id pcidev_table[] = {
7239bcc9736cSJeff Kirsher 	{ PCI_VENDOR_ID_MICREL_KS, 0x8841,
7240bcc9736cSJeff Kirsher 		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
7241bcc9736cSJeff Kirsher 	{ PCI_VENDOR_ID_MICREL_KS, 0x8842,
7242bcc9736cSJeff Kirsher 		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
7243bcc9736cSJeff Kirsher 	{ 0 }
7244bcc9736cSJeff Kirsher };
7245bcc9736cSJeff Kirsher 
7246bcc9736cSJeff Kirsher MODULE_DEVICE_TABLE(pci, pcidev_table);
7247bcc9736cSJeff Kirsher 
7248bcc9736cSJeff Kirsher static struct pci_driver pci_device_driver = {
7249bcc9736cSJeff Kirsher #ifdef CONFIG_PM
7250bcc9736cSJeff Kirsher 	.suspend	= pcidev_suspend,
7251bcc9736cSJeff Kirsher 	.resume		= pcidev_resume,
7252bcc9736cSJeff Kirsher #endif
7253bcc9736cSJeff Kirsher 	.name		= pcidev_name,
7254bcc9736cSJeff Kirsher 	.id_table	= pcidev_table,
7255bcc9736cSJeff Kirsher 	.probe		= pcidev_init,
7256bcc9736cSJeff Kirsher 	.remove		= pcidev_exit
7257bcc9736cSJeff Kirsher };
7258bcc9736cSJeff Kirsher 
7259bcc9736cSJeff Kirsher static int __init ksz884x_init_module(void)
7260bcc9736cSJeff Kirsher {
7261bcc9736cSJeff Kirsher 	return pci_register_driver(&pci_device_driver);
7262bcc9736cSJeff Kirsher }
7263bcc9736cSJeff Kirsher 
7264bcc9736cSJeff Kirsher static void __exit ksz884x_cleanup_module(void)
7265bcc9736cSJeff Kirsher {
7266bcc9736cSJeff Kirsher 	pci_unregister_driver(&pci_device_driver);
7267bcc9736cSJeff Kirsher }
7268bcc9736cSJeff Kirsher 
7269bcc9736cSJeff Kirsher module_init(ksz884x_init_module);
7270bcc9736cSJeff Kirsher module_exit(ksz884x_cleanup_module);
7271bcc9736cSJeff Kirsher 
7272bcc9736cSJeff Kirsher MODULE_DESCRIPTION("KSZ8841/2 PCI network driver");
7273bcc9736cSJeff Kirsher MODULE_AUTHOR("Tristram Ha <Tristram.Ha@micrel.com>");
7274bcc9736cSJeff Kirsher MODULE_LICENSE("GPL");
7275bcc9736cSJeff Kirsher 
7276bcc9736cSJeff Kirsher module_param_named(message, msg_enable, int, 0);
7277bcc9736cSJeff Kirsher MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
7278bcc9736cSJeff Kirsher 
7279bcc9736cSJeff Kirsher module_param(macaddr, charp, 0);
7280bcc9736cSJeff Kirsher module_param(mac1addr, charp, 0);
7281bcc9736cSJeff Kirsher module_param(fast_aging, int, 0);
7282bcc9736cSJeff Kirsher module_param(multi_dev, int, 0);
7283bcc9736cSJeff Kirsher module_param(stp, int, 0);
7284bcc9736cSJeff Kirsher MODULE_PARM_DESC(macaddr, "MAC address");
7285bcc9736cSJeff Kirsher MODULE_PARM_DESC(mac1addr, "Second MAC address");
7286bcc9736cSJeff Kirsher MODULE_PARM_DESC(fast_aging, "Fast aging");
7287bcc9736cSJeff Kirsher MODULE_PARM_DESC(multi_dev, "Multiple device interfaces");
7288bcc9736cSJeff Kirsher MODULE_PARM_DESC(stp, "STP support");
7289