TOMOYO: Use GFP_NOFS rather than GFP_KERNEL.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / ksz884x.c
blob6c5327af1bf920e28f7291c7ce9bb47aac8eafde
1 /**
2 * drivers/net/ksx884x.c - Micrel KSZ8841/2 PCI Ethernet driver
4 * Copyright (c) 2009-2010 Micrel, Inc.
5 * Tristram Ha <Tristram.Ha@micrel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/version.h>
21 #include <linux/ioport.h>
22 #include <linux/pci.h>
23 #include <linux/proc_fs.h>
24 #include <linux/mii.h>
25 #include <linux/platform_device.h>
26 #include <linux/ethtool.h>
27 #include <linux/etherdevice.h>
28 #include <linux/in.h>
29 #include <linux/ip.h>
30 #include <linux/if_vlan.h>
31 #include <linux/crc32.h>
32 #include <linux/sched.h>
35 /* DMA Registers */
37 #define KS_DMA_TX_CTRL 0x0000
38 #define DMA_TX_ENABLE 0x00000001
39 #define DMA_TX_CRC_ENABLE 0x00000002
40 #define DMA_TX_PAD_ENABLE 0x00000004
41 #define DMA_TX_LOOPBACK 0x00000100
42 #define DMA_TX_FLOW_ENABLE 0x00000200
43 #define DMA_TX_CSUM_IP 0x00010000
44 #define DMA_TX_CSUM_TCP 0x00020000
45 #define DMA_TX_CSUM_UDP 0x00040000
46 #define DMA_TX_BURST_SIZE 0x3F000000
48 #define KS_DMA_RX_CTRL 0x0004
49 #define DMA_RX_ENABLE 0x00000001
50 #define KS884X_DMA_RX_MULTICAST 0x00000002
51 #define DMA_RX_PROMISCUOUS 0x00000004
52 #define DMA_RX_ERROR 0x00000008
53 #define DMA_RX_UNICAST 0x00000010
54 #define DMA_RX_ALL_MULTICAST 0x00000020
55 #define DMA_RX_BROADCAST 0x00000040
56 #define DMA_RX_FLOW_ENABLE 0x00000200
57 #define DMA_RX_CSUM_IP 0x00010000
58 #define DMA_RX_CSUM_TCP 0x00020000
59 #define DMA_RX_CSUM_UDP 0x00040000
60 #define DMA_RX_BURST_SIZE 0x3F000000
62 #define DMA_BURST_SHIFT 24
63 #define DMA_BURST_DEFAULT 8
65 #define KS_DMA_TX_START 0x0008
66 #define KS_DMA_RX_START 0x000C
67 #define DMA_START 0x00000001
69 #define KS_DMA_TX_ADDR 0x0010
70 #define KS_DMA_RX_ADDR 0x0014
72 #define DMA_ADDR_LIST_MASK 0xFFFFFFFC
73 #define DMA_ADDR_LIST_SHIFT 2
75 /* MTR0 */
76 #define KS884X_MULTICAST_0_OFFSET 0x0020
77 #define KS884X_MULTICAST_1_OFFSET 0x0021
78 #define KS884X_MULTICAST_2_OFFSET 0x0022
79 #define KS884x_MULTICAST_3_OFFSET 0x0023
80 /* MTR1 */
81 #define KS884X_MULTICAST_4_OFFSET 0x0024
82 #define KS884X_MULTICAST_5_OFFSET 0x0025
83 #define KS884X_MULTICAST_6_OFFSET 0x0026
84 #define KS884X_MULTICAST_7_OFFSET 0x0027
86 /* Interrupt Registers */
88 /* INTEN */
89 #define KS884X_INTERRUPTS_ENABLE 0x0028
90 /* INTST */
91 #define KS884X_INTERRUPTS_STATUS 0x002C
93 #define KS884X_INT_RX_STOPPED 0x02000000
94 #define KS884X_INT_TX_STOPPED 0x04000000
95 #define KS884X_INT_RX_OVERRUN 0x08000000
96 #define KS884X_INT_TX_EMPTY 0x10000000
97 #define KS884X_INT_RX 0x20000000
98 #define KS884X_INT_TX 0x40000000
99 #define KS884X_INT_PHY 0x80000000
101 #define KS884X_INT_RX_MASK \
102 (KS884X_INT_RX | KS884X_INT_RX_OVERRUN)
103 #define KS884X_INT_TX_MASK \
104 (KS884X_INT_TX | KS884X_INT_TX_EMPTY)
105 #define KS884X_INT_MASK (KS884X_INT_RX | KS884X_INT_TX | KS884X_INT_PHY)
107 /* MAC Additional Station Address */
109 /* MAAL0 */
110 #define KS_ADD_ADDR_0_LO 0x0080
111 /* MAAH0 */
112 #define KS_ADD_ADDR_0_HI 0x0084
113 /* MAAL1 */
114 #define KS_ADD_ADDR_1_LO 0x0088
115 /* MAAH1 */
116 #define KS_ADD_ADDR_1_HI 0x008C
117 /* MAAL2 */
118 #define KS_ADD_ADDR_2_LO 0x0090
119 /* MAAH2 */
120 #define KS_ADD_ADDR_2_HI 0x0094
121 /* MAAL3 */
122 #define KS_ADD_ADDR_3_LO 0x0098
123 /* MAAH3 */
124 #define KS_ADD_ADDR_3_HI 0x009C
125 /* MAAL4 */
126 #define KS_ADD_ADDR_4_LO 0x00A0
127 /* MAAH4 */
128 #define KS_ADD_ADDR_4_HI 0x00A4
129 /* MAAL5 */
130 #define KS_ADD_ADDR_5_LO 0x00A8
131 /* MAAH5 */
132 #define KS_ADD_ADDR_5_HI 0x00AC
133 /* MAAL6 */
134 #define KS_ADD_ADDR_6_LO 0x00B0
135 /* MAAH6 */
136 #define KS_ADD_ADDR_6_HI 0x00B4
137 /* MAAL7 */
138 #define KS_ADD_ADDR_7_LO 0x00B8
139 /* MAAH7 */
140 #define KS_ADD_ADDR_7_HI 0x00BC
141 /* MAAL8 */
142 #define KS_ADD_ADDR_8_LO 0x00C0
143 /* MAAH8 */
144 #define KS_ADD_ADDR_8_HI 0x00C4
145 /* MAAL9 */
146 #define KS_ADD_ADDR_9_LO 0x00C8
147 /* MAAH9 */
148 #define KS_ADD_ADDR_9_HI 0x00CC
149 /* MAAL10 */
150 #define KS_ADD_ADDR_A_LO 0x00D0
151 /* MAAH10 */
152 #define KS_ADD_ADDR_A_HI 0x00D4
153 /* MAAL11 */
154 #define KS_ADD_ADDR_B_LO 0x00D8
155 /* MAAH11 */
156 #define KS_ADD_ADDR_B_HI 0x00DC
157 /* MAAL12 */
158 #define KS_ADD_ADDR_C_LO 0x00E0
159 /* MAAH12 */
160 #define KS_ADD_ADDR_C_HI 0x00E4
161 /* MAAL13 */
162 #define KS_ADD_ADDR_D_LO 0x00E8
163 /* MAAH13 */
164 #define KS_ADD_ADDR_D_HI 0x00EC
165 /* MAAL14 */
166 #define KS_ADD_ADDR_E_LO 0x00F0
167 /* MAAH14 */
168 #define KS_ADD_ADDR_E_HI 0x00F4
169 /* MAAL15 */
170 #define KS_ADD_ADDR_F_LO 0x00F8
171 /* MAAH15 */
172 #define KS_ADD_ADDR_F_HI 0x00FC
174 #define ADD_ADDR_HI_MASK 0x0000FFFF
175 #define ADD_ADDR_ENABLE 0x80000000
176 #define ADD_ADDR_INCR 8
178 /* Miscellaneous Registers */
180 /* MARL */
181 #define KS884X_ADDR_0_OFFSET 0x0200
182 #define KS884X_ADDR_1_OFFSET 0x0201
183 /* MARM */
184 #define KS884X_ADDR_2_OFFSET 0x0202
185 #define KS884X_ADDR_3_OFFSET 0x0203
186 /* MARH */
187 #define KS884X_ADDR_4_OFFSET 0x0204
188 #define KS884X_ADDR_5_OFFSET 0x0205
190 /* OBCR */
191 #define KS884X_BUS_CTRL_OFFSET 0x0210
193 #define BUS_SPEED_125_MHZ 0x0000
194 #define BUS_SPEED_62_5_MHZ 0x0001
195 #define BUS_SPEED_41_66_MHZ 0x0002
196 #define BUS_SPEED_25_MHZ 0x0003
198 /* EEPCR */
199 #define KS884X_EEPROM_CTRL_OFFSET 0x0212
201 #define EEPROM_CHIP_SELECT 0x0001
202 #define EEPROM_SERIAL_CLOCK 0x0002
203 #define EEPROM_DATA_OUT 0x0004
204 #define EEPROM_DATA_IN 0x0008
205 #define EEPROM_ACCESS_ENABLE 0x0010
207 /* MBIR */
208 #define KS884X_MEM_INFO_OFFSET 0x0214
210 #define RX_MEM_TEST_FAILED 0x0008
211 #define RX_MEM_TEST_FINISHED 0x0010
212 #define TX_MEM_TEST_FAILED 0x0800
213 #define TX_MEM_TEST_FINISHED 0x1000
215 /* GCR */
216 #define KS884X_GLOBAL_CTRL_OFFSET 0x0216
217 #define GLOBAL_SOFTWARE_RESET 0x0001
219 #define KS8841_POWER_MANAGE_OFFSET 0x0218
221 /* WFCR */
222 #define KS8841_WOL_CTRL_OFFSET 0x021A
223 #define KS8841_WOL_MAGIC_ENABLE 0x0080
224 #define KS8841_WOL_FRAME3_ENABLE 0x0008
225 #define KS8841_WOL_FRAME2_ENABLE 0x0004
226 #define KS8841_WOL_FRAME1_ENABLE 0x0002
227 #define KS8841_WOL_FRAME0_ENABLE 0x0001
229 /* WF0 */
230 #define KS8841_WOL_FRAME_CRC_OFFSET 0x0220
231 #define KS8841_WOL_FRAME_BYTE0_OFFSET 0x0224
232 #define KS8841_WOL_FRAME_BYTE2_OFFSET 0x0228
234 /* IACR */
235 #define KS884X_IACR_P 0x04A0
236 #define KS884X_IACR_OFFSET KS884X_IACR_P
238 /* IADR1 */
239 #define KS884X_IADR1_P 0x04A2
240 #define KS884X_IADR2_P 0x04A4
241 #define KS884X_IADR3_P 0x04A6
242 #define KS884X_IADR4_P 0x04A8
243 #define KS884X_IADR5_P 0x04AA
245 #define KS884X_ACC_CTRL_SEL_OFFSET KS884X_IACR_P
246 #define KS884X_ACC_CTRL_INDEX_OFFSET (KS884X_ACC_CTRL_SEL_OFFSET + 1)
248 #define KS884X_ACC_DATA_0_OFFSET KS884X_IADR4_P
249 #define KS884X_ACC_DATA_1_OFFSET (KS884X_ACC_DATA_0_OFFSET + 1)
250 #define KS884X_ACC_DATA_2_OFFSET KS884X_IADR5_P
251 #define KS884X_ACC_DATA_3_OFFSET (KS884X_ACC_DATA_2_OFFSET + 1)
252 #define KS884X_ACC_DATA_4_OFFSET KS884X_IADR2_P
253 #define KS884X_ACC_DATA_5_OFFSET (KS884X_ACC_DATA_4_OFFSET + 1)
254 #define KS884X_ACC_DATA_6_OFFSET KS884X_IADR3_P
255 #define KS884X_ACC_DATA_7_OFFSET (KS884X_ACC_DATA_6_OFFSET + 1)
256 #define KS884X_ACC_DATA_8_OFFSET KS884X_IADR1_P
258 /* P1MBCR */
259 #define KS884X_P1MBCR_P 0x04D0
260 #define KS884X_P1MBSR_P 0x04D2
261 #define KS884X_PHY1ILR_P 0x04D4
262 #define KS884X_PHY1IHR_P 0x04D6
263 #define KS884X_P1ANAR_P 0x04D8
264 #define KS884X_P1ANLPR_P 0x04DA
266 /* P2MBCR */
267 #define KS884X_P2MBCR_P 0x04E0
268 #define KS884X_P2MBSR_P 0x04E2
269 #define KS884X_PHY2ILR_P 0x04E4
270 #define KS884X_PHY2IHR_P 0x04E6
271 #define KS884X_P2ANAR_P 0x04E8
272 #define KS884X_P2ANLPR_P 0x04EA
274 #define KS884X_PHY_1_CTRL_OFFSET KS884X_P1MBCR_P
275 #define PHY_CTRL_INTERVAL (KS884X_P2MBCR_P - KS884X_P1MBCR_P)
277 #define KS884X_PHY_CTRL_OFFSET 0x00
279 /* Mode Control Register */
280 #define PHY_REG_CTRL 0
282 #define PHY_RESET 0x8000
283 #define PHY_LOOPBACK 0x4000
284 #define PHY_SPEED_100MBIT 0x2000
285 #define PHY_AUTO_NEG_ENABLE 0x1000
286 #define PHY_POWER_DOWN 0x0800
287 #define PHY_MII_DISABLE 0x0400
288 #define PHY_AUTO_NEG_RESTART 0x0200
289 #define PHY_FULL_DUPLEX 0x0100
290 #define PHY_COLLISION_TEST 0x0080
291 #define PHY_HP_MDIX 0x0020
292 #define PHY_FORCE_MDIX 0x0010
293 #define PHY_AUTO_MDIX_DISABLE 0x0008
294 #define PHY_REMOTE_FAULT_DISABLE 0x0004
295 #define PHY_TRANSMIT_DISABLE 0x0002
296 #define PHY_LED_DISABLE 0x0001
298 #define KS884X_PHY_STATUS_OFFSET 0x02
300 /* Mode Status Register */
301 #define PHY_REG_STATUS 1
303 #define PHY_100BT4_CAPABLE 0x8000
304 #define PHY_100BTX_FD_CAPABLE 0x4000
305 #define PHY_100BTX_CAPABLE 0x2000
306 #define PHY_10BT_FD_CAPABLE 0x1000
307 #define PHY_10BT_CAPABLE 0x0800
308 #define PHY_MII_SUPPRESS_CAPABLE 0x0040
309 #define PHY_AUTO_NEG_ACKNOWLEDGE 0x0020
310 #define PHY_REMOTE_FAULT 0x0010
311 #define PHY_AUTO_NEG_CAPABLE 0x0008
312 #define PHY_LINK_STATUS 0x0004
313 #define PHY_JABBER_DETECT 0x0002
314 #define PHY_EXTENDED_CAPABILITY 0x0001
316 #define KS884X_PHY_ID_1_OFFSET 0x04
317 #define KS884X_PHY_ID_2_OFFSET 0x06
319 /* PHY Identifier Registers */
320 #define PHY_REG_ID_1 2
321 #define PHY_REG_ID_2 3
323 #define KS884X_PHY_AUTO_NEG_OFFSET 0x08
325 /* Auto-Negotiation Advertisement Register */
326 #define PHY_REG_AUTO_NEGOTIATION 4
328 #define PHY_AUTO_NEG_NEXT_PAGE 0x8000
329 #define PHY_AUTO_NEG_REMOTE_FAULT 0x2000
330 /* Not supported. */
331 #define PHY_AUTO_NEG_ASYM_PAUSE 0x0800
332 #define PHY_AUTO_NEG_SYM_PAUSE 0x0400
333 #define PHY_AUTO_NEG_100BT4 0x0200
334 #define PHY_AUTO_NEG_100BTX_FD 0x0100
335 #define PHY_AUTO_NEG_100BTX 0x0080
336 #define PHY_AUTO_NEG_10BT_FD 0x0040
337 #define PHY_AUTO_NEG_10BT 0x0020
338 #define PHY_AUTO_NEG_SELECTOR 0x001F
339 #define PHY_AUTO_NEG_802_3 0x0001
341 #define PHY_AUTO_NEG_PAUSE (PHY_AUTO_NEG_SYM_PAUSE | PHY_AUTO_NEG_ASYM_PAUSE)
343 #define KS884X_PHY_REMOTE_CAP_OFFSET 0x0A
345 /* Auto-Negotiation Link Partner Ability Register */
346 #define PHY_REG_REMOTE_CAPABILITY 5
348 #define PHY_REMOTE_NEXT_PAGE 0x8000
349 #define PHY_REMOTE_ACKNOWLEDGE 0x4000
350 #define PHY_REMOTE_REMOTE_FAULT 0x2000
351 #define PHY_REMOTE_SYM_PAUSE 0x0400
352 #define PHY_REMOTE_100BTX_FD 0x0100
353 #define PHY_REMOTE_100BTX 0x0080
354 #define PHY_REMOTE_10BT_FD 0x0040
355 #define PHY_REMOTE_10BT 0x0020
357 /* P1VCT */
358 #define KS884X_P1VCT_P 0x04F0
359 #define KS884X_P1PHYCTRL_P 0x04F2
361 /* P2VCT */
362 #define KS884X_P2VCT_P 0x04F4
363 #define KS884X_P2PHYCTRL_P 0x04F6
365 #define KS884X_PHY_SPECIAL_OFFSET KS884X_P1VCT_P
366 #define PHY_SPECIAL_INTERVAL (KS884X_P2VCT_P - KS884X_P1VCT_P)
368 #define KS884X_PHY_LINK_MD_OFFSET 0x00
370 #define PHY_START_CABLE_DIAG 0x8000
371 #define PHY_CABLE_DIAG_RESULT 0x6000
372 #define PHY_CABLE_STAT_NORMAL 0x0000
373 #define PHY_CABLE_STAT_OPEN 0x2000
374 #define PHY_CABLE_STAT_SHORT 0x4000
375 #define PHY_CABLE_STAT_FAILED 0x6000
376 #define PHY_CABLE_10M_SHORT 0x1000
377 #define PHY_CABLE_FAULT_COUNTER 0x01FF
379 #define KS884X_PHY_PHY_CTRL_OFFSET 0x02
381 #define PHY_STAT_REVERSED_POLARITY 0x0020
382 #define PHY_STAT_MDIX 0x0010
383 #define PHY_FORCE_LINK 0x0008
384 #define PHY_POWER_SAVING_DISABLE 0x0004
385 #define PHY_REMOTE_LOOPBACK 0x0002
387 /* SIDER */
388 #define KS884X_SIDER_P 0x0400
389 #define KS884X_CHIP_ID_OFFSET KS884X_SIDER_P
390 #define KS884X_FAMILY_ID_OFFSET (KS884X_CHIP_ID_OFFSET + 1)
392 #define REG_FAMILY_ID 0x88
394 #define REG_CHIP_ID_41 0x8810
395 #define REG_CHIP_ID_42 0x8800
397 #define KS884X_CHIP_ID_MASK_41 0xFF10
398 #define KS884X_CHIP_ID_MASK 0xFFF0
399 #define KS884X_CHIP_ID_SHIFT 4
400 #define KS884X_REVISION_MASK 0x000E
401 #define KS884X_REVISION_SHIFT 1
402 #define KS8842_START 0x0001
404 #define CHIP_IP_41_M 0x8810
405 #define CHIP_IP_42_M 0x8800
406 #define CHIP_IP_61_M 0x8890
407 #define CHIP_IP_62_M 0x8880
409 #define CHIP_IP_41_P 0x8850
410 #define CHIP_IP_42_P 0x8840
411 #define CHIP_IP_61_P 0x88D0
412 #define CHIP_IP_62_P 0x88C0
414 /* SGCR1 */
415 #define KS8842_SGCR1_P 0x0402
416 #define KS8842_SWITCH_CTRL_1_OFFSET KS8842_SGCR1_P
418 #define SWITCH_PASS_ALL 0x8000
419 #define SWITCH_TX_FLOW_CTRL 0x2000
420 #define SWITCH_RX_FLOW_CTRL 0x1000
421 #define SWITCH_CHECK_LENGTH 0x0800
422 #define SWITCH_AGING_ENABLE 0x0400
423 #define SWITCH_FAST_AGING 0x0200
424 #define SWITCH_AGGR_BACKOFF 0x0100
425 #define SWITCH_PASS_PAUSE 0x0008
426 #define SWITCH_LINK_AUTO_AGING 0x0001
428 /* SGCR2 */
429 #define KS8842_SGCR2_P 0x0404
430 #define KS8842_SWITCH_CTRL_2_OFFSET KS8842_SGCR2_P
432 #define SWITCH_VLAN_ENABLE 0x8000
433 #define SWITCH_IGMP_SNOOP 0x4000
434 #define IPV6_MLD_SNOOP_ENABLE 0x2000
435 #define IPV6_MLD_SNOOP_OPTION 0x1000
436 #define PRIORITY_SCHEME_SELECT 0x0800
437 #define SWITCH_MIRROR_RX_TX 0x0100
438 #define UNICAST_VLAN_BOUNDARY 0x0080
439 #define MULTICAST_STORM_DISABLE 0x0040
440 #define SWITCH_BACK_PRESSURE 0x0020
441 #define FAIR_FLOW_CTRL 0x0010
442 #define NO_EXC_COLLISION_DROP 0x0008
443 #define SWITCH_HUGE_PACKET 0x0004
444 #define SWITCH_LEGAL_PACKET 0x0002
445 #define SWITCH_BUF_RESERVE 0x0001
447 /* SGCR3 */
448 #define KS8842_SGCR3_P 0x0406
449 #define KS8842_SWITCH_CTRL_3_OFFSET KS8842_SGCR3_P
451 #define BROADCAST_STORM_RATE_LO 0xFF00
452 #define SWITCH_REPEATER 0x0080
453 #define SWITCH_HALF_DUPLEX 0x0040
454 #define SWITCH_FLOW_CTRL 0x0020
455 #define SWITCH_10_MBIT 0x0010
456 #define SWITCH_REPLACE_NULL_VID 0x0008
457 #define BROADCAST_STORM_RATE_HI 0x0007
459 #define BROADCAST_STORM_RATE 0x07FF
461 /* SGCR4 */
462 #define KS8842_SGCR4_P 0x0408
464 /* SGCR5 */
465 #define KS8842_SGCR5_P 0x040A
466 #define KS8842_SWITCH_CTRL_5_OFFSET KS8842_SGCR5_P
468 #define LED_MODE 0x8200
469 #define LED_SPEED_DUPLEX_ACT 0x0000
470 #define LED_SPEED_DUPLEX_LINK_ACT 0x8000
471 #define LED_DUPLEX_10_100 0x0200
473 /* SGCR6 */
474 #define KS8842_SGCR6_P 0x0410
475 #define KS8842_SWITCH_CTRL_6_OFFSET KS8842_SGCR6_P
477 #define KS8842_PRIORITY_MASK 3
478 #define KS8842_PRIORITY_SHIFT 2
480 /* SGCR7 */
481 #define KS8842_SGCR7_P 0x0412
482 #define KS8842_SWITCH_CTRL_7_OFFSET KS8842_SGCR7_P
484 #define SWITCH_UNK_DEF_PORT_ENABLE 0x0008
485 #define SWITCH_UNK_DEF_PORT_3 0x0004
486 #define SWITCH_UNK_DEF_PORT_2 0x0002
487 #define SWITCH_UNK_DEF_PORT_1 0x0001
489 /* MACAR1 */
490 #define KS8842_MACAR1_P 0x0470
491 #define KS8842_MACAR2_P 0x0472
492 #define KS8842_MACAR3_P 0x0474
493 #define KS8842_MAC_ADDR_1_OFFSET KS8842_MACAR1_P
494 #define KS8842_MAC_ADDR_0_OFFSET (KS8842_MAC_ADDR_1_OFFSET + 1)
495 #define KS8842_MAC_ADDR_3_OFFSET KS8842_MACAR2_P
496 #define KS8842_MAC_ADDR_2_OFFSET (KS8842_MAC_ADDR_3_OFFSET + 1)
497 #define KS8842_MAC_ADDR_5_OFFSET KS8842_MACAR3_P
498 #define KS8842_MAC_ADDR_4_OFFSET (KS8842_MAC_ADDR_5_OFFSET + 1)
500 /* TOSR1 */
501 #define KS8842_TOSR1_P 0x0480
502 #define KS8842_TOSR2_P 0x0482
503 #define KS8842_TOSR3_P 0x0484
504 #define KS8842_TOSR4_P 0x0486
505 #define KS8842_TOSR5_P 0x0488
506 #define KS8842_TOSR6_P 0x048A
507 #define KS8842_TOSR7_P 0x0490
508 #define KS8842_TOSR8_P 0x0492
509 #define KS8842_TOS_1_OFFSET KS8842_TOSR1_P
510 #define KS8842_TOS_2_OFFSET KS8842_TOSR2_P
511 #define KS8842_TOS_3_OFFSET KS8842_TOSR3_P
512 #define KS8842_TOS_4_OFFSET KS8842_TOSR4_P
513 #define KS8842_TOS_5_OFFSET KS8842_TOSR5_P
514 #define KS8842_TOS_6_OFFSET KS8842_TOSR6_P
516 #define KS8842_TOS_7_OFFSET KS8842_TOSR7_P
517 #define KS8842_TOS_8_OFFSET KS8842_TOSR8_P
519 /* P1CR1 */
520 #define KS8842_P1CR1_P 0x0500
521 #define KS8842_P1CR2_P 0x0502
522 #define KS8842_P1VIDR_P 0x0504
523 #define KS8842_P1CR3_P 0x0506
524 #define KS8842_P1IRCR_P 0x0508
525 #define KS8842_P1ERCR_P 0x050A
526 #define KS884X_P1SCSLMD_P 0x0510
527 #define KS884X_P1CR4_P 0x0512
528 #define KS884X_P1SR_P 0x0514
530 /* P2CR1 */
531 #define KS8842_P2CR1_P 0x0520
532 #define KS8842_P2CR2_P 0x0522
533 #define KS8842_P2VIDR_P 0x0524
534 #define KS8842_P2CR3_P 0x0526
535 #define KS8842_P2IRCR_P 0x0528
536 #define KS8842_P2ERCR_P 0x052A
537 #define KS884X_P2SCSLMD_P 0x0530
538 #define KS884X_P2CR4_P 0x0532
539 #define KS884X_P2SR_P 0x0534
541 /* P3CR1 */
542 #define KS8842_P3CR1_P 0x0540
543 #define KS8842_P3CR2_P 0x0542
544 #define KS8842_P3VIDR_P 0x0544
545 #define KS8842_P3CR3_P 0x0546
546 #define KS8842_P3IRCR_P 0x0548
547 #define KS8842_P3ERCR_P 0x054A
549 #define KS8842_PORT_1_CTRL_1 KS8842_P1CR1_P
550 #define KS8842_PORT_2_CTRL_1 KS8842_P2CR1_P
551 #define KS8842_PORT_3_CTRL_1 KS8842_P3CR1_P
553 #define PORT_CTRL_ADDR(port, addr) \
554 (addr = KS8842_PORT_1_CTRL_1 + (port) * \
555 (KS8842_PORT_2_CTRL_1 - KS8842_PORT_1_CTRL_1))
557 #define KS8842_PORT_CTRL_1_OFFSET 0x00
559 #define PORT_BROADCAST_STORM 0x0080
560 #define PORT_DIFFSERV_ENABLE 0x0040
561 #define PORT_802_1P_ENABLE 0x0020
562 #define PORT_BASED_PRIORITY_MASK 0x0018
563 #define PORT_BASED_PRIORITY_BASE 0x0003
564 #define PORT_BASED_PRIORITY_SHIFT 3
565 #define PORT_BASED_PRIORITY_0 0x0000
566 #define PORT_BASED_PRIORITY_1 0x0008
567 #define PORT_BASED_PRIORITY_2 0x0010
568 #define PORT_BASED_PRIORITY_3 0x0018
569 #define PORT_INSERT_TAG 0x0004
570 #define PORT_REMOVE_TAG 0x0002
571 #define PORT_PRIO_QUEUE_ENABLE 0x0001
573 #define KS8842_PORT_CTRL_2_OFFSET 0x02
575 #define PORT_INGRESS_VLAN_FILTER 0x4000
576 #define PORT_DISCARD_NON_VID 0x2000
577 #define PORT_FORCE_FLOW_CTRL 0x1000
578 #define PORT_BACK_PRESSURE 0x0800
579 #define PORT_TX_ENABLE 0x0400
580 #define PORT_RX_ENABLE 0x0200
581 #define PORT_LEARN_DISABLE 0x0100
582 #define PORT_MIRROR_SNIFFER 0x0080
583 #define PORT_MIRROR_RX 0x0040
584 #define PORT_MIRROR_TX 0x0020
585 #define PORT_USER_PRIORITY_CEILING 0x0008
586 #define PORT_VLAN_MEMBERSHIP 0x0007
588 #define KS8842_PORT_CTRL_VID_OFFSET 0x04
590 #define PORT_DEFAULT_VID 0x0001
592 #define KS8842_PORT_CTRL_3_OFFSET 0x06
594 #define PORT_INGRESS_LIMIT_MODE 0x000C
595 #define PORT_INGRESS_ALL 0x0000
596 #define PORT_INGRESS_UNICAST 0x0004
597 #define PORT_INGRESS_MULTICAST 0x0008
598 #define PORT_INGRESS_BROADCAST 0x000C
599 #define PORT_COUNT_IFG 0x0002
600 #define PORT_COUNT_PREAMBLE 0x0001
602 #define KS8842_PORT_IN_RATE_OFFSET 0x08
603 #define KS8842_PORT_OUT_RATE_OFFSET 0x0A
605 #define PORT_PRIORITY_RATE 0x0F
606 #define PORT_PRIORITY_RATE_SHIFT 4
608 #define KS884X_PORT_LINK_MD 0x10
610 #define PORT_CABLE_10M_SHORT 0x8000
611 #define PORT_CABLE_DIAG_RESULT 0x6000
612 #define PORT_CABLE_STAT_NORMAL 0x0000
613 #define PORT_CABLE_STAT_OPEN 0x2000
614 #define PORT_CABLE_STAT_SHORT 0x4000
615 #define PORT_CABLE_STAT_FAILED 0x6000
616 #define PORT_START_CABLE_DIAG 0x1000
617 #define PORT_FORCE_LINK 0x0800
618 #define PORT_POWER_SAVING_DISABLE 0x0400
619 #define PORT_PHY_REMOTE_LOOPBACK 0x0200
620 #define PORT_CABLE_FAULT_COUNTER 0x01FF
622 #define KS884X_PORT_CTRL_4_OFFSET 0x12
624 #define PORT_LED_OFF 0x8000
625 #define PORT_TX_DISABLE 0x4000
626 #define PORT_AUTO_NEG_RESTART 0x2000
627 #define PORT_REMOTE_FAULT_DISABLE 0x1000
628 #define PORT_POWER_DOWN 0x0800
629 #define PORT_AUTO_MDIX_DISABLE 0x0400
630 #define PORT_FORCE_MDIX 0x0200
631 #define PORT_LOOPBACK 0x0100
632 #define PORT_AUTO_NEG_ENABLE 0x0080
633 #define PORT_FORCE_100_MBIT 0x0040
634 #define PORT_FORCE_FULL_DUPLEX 0x0020
635 #define PORT_AUTO_NEG_SYM_PAUSE 0x0010
636 #define PORT_AUTO_NEG_100BTX_FD 0x0008
637 #define PORT_AUTO_NEG_100BTX 0x0004
638 #define PORT_AUTO_NEG_10BT_FD 0x0002
639 #define PORT_AUTO_NEG_10BT 0x0001
641 #define KS884X_PORT_STATUS_OFFSET 0x14
643 #define PORT_HP_MDIX 0x8000
644 #define PORT_REVERSED_POLARITY 0x2000
645 #define PORT_RX_FLOW_CTRL 0x0800
646 #define PORT_TX_FLOW_CTRL 0x1000
647 #define PORT_STATUS_SPEED_100MBIT 0x0400
648 #define PORT_STATUS_FULL_DUPLEX 0x0200
649 #define PORT_REMOTE_FAULT 0x0100
650 #define PORT_MDIX_STATUS 0x0080
651 #define PORT_AUTO_NEG_COMPLETE 0x0040
652 #define PORT_STATUS_LINK_GOOD 0x0020
653 #define PORT_REMOTE_SYM_PAUSE 0x0010
654 #define PORT_REMOTE_100BTX_FD 0x0008
655 #define PORT_REMOTE_100BTX 0x0004
656 #define PORT_REMOTE_10BT_FD 0x0002
657 #define PORT_REMOTE_10BT 0x0001
660 #define STATIC_MAC_TABLE_ADDR 00-0000FFFF-FFFFFFFF
661 #define STATIC_MAC_TABLE_FWD_PORTS 00-00070000-00000000
662 #define STATIC_MAC_TABLE_VALID 00-00080000-00000000
663 #define STATIC_MAC_TABLE_OVERRIDE 00-00100000-00000000
664 #define STATIC_MAC_TABLE_USE_FID 00-00200000-00000000
665 #define STATIC_MAC_TABLE_FID 00-03C00000-00000000
668 #define STATIC_MAC_TABLE_ADDR 0x0000FFFF
669 #define STATIC_MAC_TABLE_FWD_PORTS 0x00070000
670 #define STATIC_MAC_TABLE_VALID 0x00080000
671 #define STATIC_MAC_TABLE_OVERRIDE 0x00100000
672 #define STATIC_MAC_TABLE_USE_FID 0x00200000
673 #define STATIC_MAC_TABLE_FID 0x03C00000
675 #define STATIC_MAC_FWD_PORTS_SHIFT 16
676 #define STATIC_MAC_FID_SHIFT 22
679 #define VLAN_TABLE_VID 00-00000000-00000FFF
680 #define VLAN_TABLE_FID 00-00000000-0000F000
681 #define VLAN_TABLE_MEMBERSHIP 00-00000000-00070000
682 #define VLAN_TABLE_VALID 00-00000000-00080000
685 #define VLAN_TABLE_VID 0x00000FFF
686 #define VLAN_TABLE_FID 0x0000F000
687 #define VLAN_TABLE_MEMBERSHIP 0x00070000
688 #define VLAN_TABLE_VALID 0x00080000
690 #define VLAN_TABLE_FID_SHIFT 12
691 #define VLAN_TABLE_MEMBERSHIP_SHIFT 16
694 #define DYNAMIC_MAC_TABLE_ADDR 00-0000FFFF-FFFFFFFF
695 #define DYNAMIC_MAC_TABLE_FID 00-000F0000-00000000
696 #define DYNAMIC_MAC_TABLE_SRC_PORT 00-00300000-00000000
697 #define DYNAMIC_MAC_TABLE_TIMESTAMP 00-00C00000-00000000
698 #define DYNAMIC_MAC_TABLE_ENTRIES 03-FF000000-00000000
699 #define DYNAMIC_MAC_TABLE_MAC_EMPTY 04-00000000-00000000
700 #define DYNAMIC_MAC_TABLE_RESERVED 78-00000000-00000000
701 #define DYNAMIC_MAC_TABLE_NOT_READY 80-00000000-00000000
704 #define DYNAMIC_MAC_TABLE_ADDR 0x0000FFFF
705 #define DYNAMIC_MAC_TABLE_FID 0x000F0000
706 #define DYNAMIC_MAC_TABLE_SRC_PORT 0x00300000
707 #define DYNAMIC_MAC_TABLE_TIMESTAMP 0x00C00000
708 #define DYNAMIC_MAC_TABLE_ENTRIES 0xFF000000
710 #define DYNAMIC_MAC_TABLE_ENTRIES_H 0x03
711 #define DYNAMIC_MAC_TABLE_MAC_EMPTY 0x04
712 #define DYNAMIC_MAC_TABLE_RESERVED 0x78
713 #define DYNAMIC_MAC_TABLE_NOT_READY 0x80
715 #define DYNAMIC_MAC_FID_SHIFT 16
716 #define DYNAMIC_MAC_SRC_PORT_SHIFT 20
717 #define DYNAMIC_MAC_TIMESTAMP_SHIFT 22
718 #define DYNAMIC_MAC_ENTRIES_SHIFT 24
719 #define DYNAMIC_MAC_ENTRIES_H_SHIFT 8
722 #define MIB_COUNTER_VALUE 00-00000000-3FFFFFFF
723 #define MIB_COUNTER_VALID 00-00000000-40000000
724 #define MIB_COUNTER_OVERFLOW 00-00000000-80000000
727 #define MIB_COUNTER_VALUE 0x3FFFFFFF
728 #define MIB_COUNTER_VALID 0x40000000
729 #define MIB_COUNTER_OVERFLOW 0x80000000
731 #define MIB_PACKET_DROPPED 0x0000FFFF
733 #define KS_MIB_PACKET_DROPPED_TX_0 0x100
734 #define KS_MIB_PACKET_DROPPED_TX_1 0x101
735 #define KS_MIB_PACKET_DROPPED_TX 0x102
736 #define KS_MIB_PACKET_DROPPED_RX_0 0x103
737 #define KS_MIB_PACKET_DROPPED_RX_1 0x104
738 #define KS_MIB_PACKET_DROPPED_RX 0x105
740 /* Change default LED mode. */
741 #define SET_DEFAULT_LED LED_SPEED_DUPLEX_ACT
743 #define MAC_ADDR_LEN 6
744 #define MAC_ADDR_ORDER(i) (MAC_ADDR_LEN - 1 - (i))
746 #define MAX_ETHERNET_BODY_SIZE 1500
747 #define ETHERNET_HEADER_SIZE 14
749 #define MAX_ETHERNET_PACKET_SIZE \
750 (MAX_ETHERNET_BODY_SIZE + ETHERNET_HEADER_SIZE)
752 #define REGULAR_RX_BUF_SIZE (MAX_ETHERNET_PACKET_SIZE + 4)
753 #define MAX_RX_BUF_SIZE (1912 + 4)
755 #define ADDITIONAL_ENTRIES 16
756 #define MAX_MULTICAST_LIST 32
758 #define HW_MULTICAST_SIZE 8
760 #define HW_TO_DEV_PORT(port) (port - 1)
762 enum {
763 media_connected,
764 media_disconnected
767 enum {
768 OID_COUNTER_UNKOWN,
770 OID_COUNTER_FIRST,
772 /* total transmit errors */
773 OID_COUNTER_XMIT_ERROR,
775 /* total receive errors */
776 OID_COUNTER_RCV_ERROR,
778 OID_COUNTER_LAST
782 * Hardware descriptor definitions
785 #define DESC_ALIGNMENT 16
786 #define BUFFER_ALIGNMENT 8
788 #define NUM_OF_RX_DESC 64
789 #define NUM_OF_TX_DESC 64
791 #define KS_DESC_RX_FRAME_LEN 0x000007FF
792 #define KS_DESC_RX_FRAME_TYPE 0x00008000
793 #define KS_DESC_RX_ERROR_CRC 0x00010000
794 #define KS_DESC_RX_ERROR_RUNT 0x00020000
795 #define KS_DESC_RX_ERROR_TOO_LONG 0x00040000
796 #define KS_DESC_RX_ERROR_PHY 0x00080000
797 #define KS884X_DESC_RX_PORT_MASK 0x00300000
798 #define KS_DESC_RX_MULTICAST 0x01000000
799 #define KS_DESC_RX_ERROR 0x02000000
800 #define KS_DESC_RX_ERROR_CSUM_UDP 0x04000000
801 #define KS_DESC_RX_ERROR_CSUM_TCP 0x08000000
802 #define KS_DESC_RX_ERROR_CSUM_IP 0x10000000
803 #define KS_DESC_RX_LAST 0x20000000
804 #define KS_DESC_RX_FIRST 0x40000000
805 #define KS_DESC_RX_ERROR_COND \
806 (KS_DESC_RX_ERROR_CRC | \
807 KS_DESC_RX_ERROR_RUNT | \
808 KS_DESC_RX_ERROR_PHY | \
809 KS_DESC_RX_ERROR_TOO_LONG)
811 #define KS_DESC_HW_OWNED 0x80000000
813 #define KS_DESC_BUF_SIZE 0x000007FF
814 #define KS884X_DESC_TX_PORT_MASK 0x00300000
815 #define KS_DESC_END_OF_RING 0x02000000
816 #define KS_DESC_TX_CSUM_GEN_UDP 0x04000000
817 #define KS_DESC_TX_CSUM_GEN_TCP 0x08000000
818 #define KS_DESC_TX_CSUM_GEN_IP 0x10000000
819 #define KS_DESC_TX_LAST 0x20000000
820 #define KS_DESC_TX_FIRST 0x40000000
821 #define KS_DESC_TX_INTERRUPT 0x80000000
823 #define KS_DESC_PORT_SHIFT 20
825 #define KS_DESC_RX_MASK (KS_DESC_BUF_SIZE)
827 #define KS_DESC_TX_MASK \
828 (KS_DESC_TX_INTERRUPT | \
829 KS_DESC_TX_FIRST | \
830 KS_DESC_TX_LAST | \
831 KS_DESC_TX_CSUM_GEN_IP | \
832 KS_DESC_TX_CSUM_GEN_TCP | \
833 KS_DESC_TX_CSUM_GEN_UDP | \
834 KS_DESC_BUF_SIZE)
836 struct ksz_desc_rx_stat {
837 #ifdef __BIG_ENDIAN_BITFIELD
838 u32 hw_owned:1;
839 u32 first_desc:1;
840 u32 last_desc:1;
841 u32 csum_err_ip:1;
842 u32 csum_err_tcp:1;
843 u32 csum_err_udp:1;
844 u32 error:1;
845 u32 multicast:1;
846 u32 src_port:4;
847 u32 err_phy:1;
848 u32 err_too_long:1;
849 u32 err_runt:1;
850 u32 err_crc:1;
851 u32 frame_type:1;
852 u32 reserved1:4;
853 u32 frame_len:11;
854 #else
855 u32 frame_len:11;
856 u32 reserved1:4;
857 u32 frame_type:1;
858 u32 err_crc:1;
859 u32 err_runt:1;
860 u32 err_too_long:1;
861 u32 err_phy:1;
862 u32 src_port:4;
863 u32 multicast:1;
864 u32 error:1;
865 u32 csum_err_udp:1;
866 u32 csum_err_tcp:1;
867 u32 csum_err_ip:1;
868 u32 last_desc:1;
869 u32 first_desc:1;
870 u32 hw_owned:1;
871 #endif
874 struct ksz_desc_tx_stat {
875 #ifdef __BIG_ENDIAN_BITFIELD
876 u32 hw_owned:1;
877 u32 reserved1:31;
878 #else
879 u32 reserved1:31;
880 u32 hw_owned:1;
881 #endif
884 struct ksz_desc_rx_buf {
885 #ifdef __BIG_ENDIAN_BITFIELD
886 u32 reserved4:6;
887 u32 end_of_ring:1;
888 u32 reserved3:14;
889 u32 buf_size:11;
890 #else
891 u32 buf_size:11;
892 u32 reserved3:14;
893 u32 end_of_ring:1;
894 u32 reserved4:6;
895 #endif
898 struct ksz_desc_tx_buf {
899 #ifdef __BIG_ENDIAN_BITFIELD
900 u32 intr:1;
901 u32 first_seg:1;
902 u32 last_seg:1;
903 u32 csum_gen_ip:1;
904 u32 csum_gen_tcp:1;
905 u32 csum_gen_udp:1;
906 u32 end_of_ring:1;
907 u32 reserved4:1;
908 u32 dest_port:4;
909 u32 reserved3:9;
910 u32 buf_size:11;
911 #else
912 u32 buf_size:11;
913 u32 reserved3:9;
914 u32 dest_port:4;
915 u32 reserved4:1;
916 u32 end_of_ring:1;
917 u32 csum_gen_udp:1;
918 u32 csum_gen_tcp:1;
919 u32 csum_gen_ip:1;
920 u32 last_seg:1;
921 u32 first_seg:1;
922 u32 intr:1;
923 #endif
926 union desc_stat {
927 struct ksz_desc_rx_stat rx;
928 struct ksz_desc_tx_stat tx;
929 u32 data;
932 union desc_buf {
933 struct ksz_desc_rx_buf rx;
934 struct ksz_desc_tx_buf tx;
935 u32 data;
939 * struct ksz_hw_desc - Hardware descriptor data structure
940 * @ctrl: Descriptor control value.
941 * @buf: Descriptor buffer value.
942 * @addr: Physical address of memory buffer.
943 * @next: Pointer to next hardware descriptor.
945 struct ksz_hw_desc {
946 union desc_stat ctrl;
947 union desc_buf buf;
948 u32 addr;
949 u32 next;
953 * struct ksz_sw_desc - Software descriptor data structure
954 * @ctrl: Descriptor control value.
955 * @buf: Descriptor buffer value.
956 * @buf_size: Current buffers size value in hardware descriptor.
958 struct ksz_sw_desc {
959 union desc_stat ctrl;
960 union desc_buf buf;
961 u32 buf_size;
965 * struct ksz_dma_buf - OS dependent DMA buffer data structure
966 * @skb: Associated socket buffer.
967 * @dma: Associated physical DMA address.
968 * len: Actual len used.
970 struct ksz_dma_buf {
971 struct sk_buff *skb;
972 dma_addr_t dma;
973 int len;
977 * struct ksz_desc - Descriptor structure
978 * @phw: Hardware descriptor pointer to uncached physical memory.
979 * @sw: Cached memory to hold hardware descriptor values for
980 * manipulation.
981 * @dma_buf: Operating system dependent data structure to hold physical
982 * memory buffer allocation information.
984 struct ksz_desc {
985 struct ksz_hw_desc *phw;
986 struct ksz_sw_desc sw;
987 struct ksz_dma_buf dma_buf;
990 #define DMA_BUFFER(desc) ((struct ksz_dma_buf *)(&(desc)->dma_buf))
993 * struct ksz_desc_info - Descriptor information data structure
994 * @ring: First descriptor in the ring.
995 * @cur: Current descriptor being manipulated.
996 * @ring_virt: First hardware descriptor in the ring.
997 * @ring_phys: The physical address of the first descriptor of the ring.
998 * @size: Size of hardware descriptor.
999 * @alloc: Number of descriptors allocated.
1000 * @avail: Number of descriptors available for use.
1001 * @last: Index for last descriptor released to hardware.
1002 * @next: Index for next descriptor available for use.
1003 * @mask: Mask for index wrapping.
1005 struct ksz_desc_info {
1006 struct ksz_desc *ring;
1007 struct ksz_desc *cur;
1008 struct ksz_hw_desc *ring_virt;
1009 u32 ring_phys;
1010 int size;
1011 int alloc;
1012 int avail;
1013 int last;
1014 int next;
1015 int mask;
1019 * KSZ8842 switch definitions
1022 enum {
1023 TABLE_STATIC_MAC = 0,
1024 TABLE_VLAN,
1025 TABLE_DYNAMIC_MAC,
1026 TABLE_MIB
1029 #define LEARNED_MAC_TABLE_ENTRIES 1024
1030 #define STATIC_MAC_TABLE_ENTRIES 8
1033 * struct ksz_mac_table - Static MAC table data structure
1034 * @mac_addr: MAC address to filter.
1035 * @vid: VID value.
1036 * @fid: FID value.
1037 * @ports: Port membership.
1038 * @override: Override setting.
1039 * @use_fid: FID use setting.
1040 * @valid: Valid setting indicating the entry is being used.
1042 struct ksz_mac_table {
1043 u8 mac_addr[MAC_ADDR_LEN];
1044 u16 vid;
1045 u8 fid;
1046 u8 ports;
1047 u8 override:1;
1048 u8 use_fid:1;
1049 u8 valid:1;
1052 #define VLAN_TABLE_ENTRIES 16
1055 * struct ksz_vlan_table - VLAN table data structure
1056 * @vid: VID value.
1057 * @fid: FID value.
1058 * @member: Port membership.
1060 struct ksz_vlan_table {
1061 u16 vid;
1062 u8 fid;
1063 u8 member;
1066 #define DIFFSERV_ENTRIES 64
1067 #define PRIO_802_1P_ENTRIES 8
1068 #define PRIO_QUEUES 4
1070 #define SWITCH_PORT_NUM 2
1071 #define TOTAL_PORT_NUM (SWITCH_PORT_NUM + 1)
1072 #define HOST_MASK (1 << SWITCH_PORT_NUM)
1073 #define PORT_MASK 7
1075 #define MAIN_PORT 0
1076 #define OTHER_PORT 1
1077 #define HOST_PORT SWITCH_PORT_NUM
1079 #define PORT_COUNTER_NUM 0x20
1080 #define TOTAL_PORT_COUNTER_NUM (PORT_COUNTER_NUM + 2)
1082 #define MIB_COUNTER_RX_LO_PRIORITY 0x00
1083 #define MIB_COUNTER_RX_HI_PRIORITY 0x01
1084 #define MIB_COUNTER_RX_UNDERSIZE 0x02
1085 #define MIB_COUNTER_RX_FRAGMENT 0x03
1086 #define MIB_COUNTER_RX_OVERSIZE 0x04
1087 #define MIB_COUNTER_RX_JABBER 0x05
1088 #define MIB_COUNTER_RX_SYMBOL_ERR 0x06
1089 #define MIB_COUNTER_RX_CRC_ERR 0x07
1090 #define MIB_COUNTER_RX_ALIGNMENT_ERR 0x08
1091 #define MIB_COUNTER_RX_CTRL_8808 0x09
1092 #define MIB_COUNTER_RX_PAUSE 0x0A
1093 #define MIB_COUNTER_RX_BROADCAST 0x0B
1094 #define MIB_COUNTER_RX_MULTICAST 0x0C
1095 #define MIB_COUNTER_RX_UNICAST 0x0D
1096 #define MIB_COUNTER_RX_OCTET_64 0x0E
1097 #define MIB_COUNTER_RX_OCTET_65_127 0x0F
1098 #define MIB_COUNTER_RX_OCTET_128_255 0x10
1099 #define MIB_COUNTER_RX_OCTET_256_511 0x11
1100 #define MIB_COUNTER_RX_OCTET_512_1023 0x12
1101 #define MIB_COUNTER_RX_OCTET_1024_1522 0x13
1102 #define MIB_COUNTER_TX_LO_PRIORITY 0x14
1103 #define MIB_COUNTER_TX_HI_PRIORITY 0x15
1104 #define MIB_COUNTER_TX_LATE_COLLISION 0x16
1105 #define MIB_COUNTER_TX_PAUSE 0x17
1106 #define MIB_COUNTER_TX_BROADCAST 0x18
1107 #define MIB_COUNTER_TX_MULTICAST 0x19
1108 #define MIB_COUNTER_TX_UNICAST 0x1A
1109 #define MIB_COUNTER_TX_DEFERRED 0x1B
1110 #define MIB_COUNTER_TX_TOTAL_COLLISION 0x1C
1111 #define MIB_COUNTER_TX_EXCESS_COLLISION 0x1D
1112 #define MIB_COUNTER_TX_SINGLE_COLLISION 0x1E
1113 #define MIB_COUNTER_TX_MULTI_COLLISION 0x1F
1115 #define MIB_COUNTER_RX_DROPPED_PACKET 0x20
1116 #define MIB_COUNTER_TX_DROPPED_PACKET 0x21
1119 * struct ksz_port_mib - Port MIB data structure
1120 * @cnt_ptr: Current pointer to MIB counter index.
1121 * @link_down: Indication the link has just gone down.
1122 * @state: Connection status of the port.
1123 * @mib_start: The starting counter index. Some ports do not start at 0.
1124 * @counter: 64-bit MIB counter value.
1125 * @dropped: Temporary buffer to remember last read packet dropped values.
1127 * MIB counters needs to be read periodically so that counters do not get
1128 * overflowed and give incorrect values. A right balance is needed to
1129 * satisfy this condition and not waste too much CPU time.
1131 * It is pointless to read MIB counters when the port is disconnected. The
1132 * @state provides the connection status so that MIB counters are read only
1133 * when the port is connected. The @link_down indicates the port is just
1134 * disconnected so that all MIB counters are read one last time to update the
1135 * information.
1137 struct ksz_port_mib {
1138 u8 cnt_ptr;
1139 u8 link_down;
1140 u8 state;
1141 u8 mib_start;
1143 u64 counter[TOTAL_PORT_COUNTER_NUM];
1144 u32 dropped[2];
1148 * struct ksz_port_cfg - Port configuration data structure
1149 * @vid: VID value.
1150 * @member: Port membership.
1151 * @port_prio: Port priority.
1152 * @rx_rate: Receive priority rate.
1153 * @tx_rate: Transmit priority rate.
1154 * @stp_state: Current Spanning Tree Protocol state.
1156 struct ksz_port_cfg {
1157 u16 vid;
1158 u8 member;
1159 u8 port_prio;
1160 u32 rx_rate[PRIO_QUEUES];
1161 u32 tx_rate[PRIO_QUEUES];
1162 int stp_state;
1166 * struct ksz_switch - KSZ8842 switch data structure
1167 * @mac_table: MAC table entries information.
1168 * @vlan_table: VLAN table entries information.
1169 * @port_cfg: Port configuration information.
1170 * @diffserv: DiffServ priority settings. Possible values from 6-bit of ToS
1171 * (bit7 ~ bit2) field.
1172 * @p_802_1p: 802.1P priority settings. Possible values from 3-bit of 802.1p
1173 * Tag priority field.
1174 * @br_addr: Bridge address. Used for STP.
1175 * @other_addr: Other MAC address. Used for multiple network device mode.
1176 * @broad_per: Broadcast storm percentage.
1177 * @member: Current port membership. Used for STP.
1179 struct ksz_switch {
1180 struct ksz_mac_table mac_table[STATIC_MAC_TABLE_ENTRIES];
1181 struct ksz_vlan_table vlan_table[VLAN_TABLE_ENTRIES];
1182 struct ksz_port_cfg port_cfg[TOTAL_PORT_NUM];
1184 u8 diffserv[DIFFSERV_ENTRIES];
1185 u8 p_802_1p[PRIO_802_1P_ENTRIES];
1187 u8 br_addr[MAC_ADDR_LEN];
1188 u8 other_addr[MAC_ADDR_LEN];
1190 u8 broad_per;
1191 u8 member;
1194 #define TX_RATE_UNIT 10000
1197 * struct ksz_port_info - Port information data structure
1198 * @state: Connection status of the port.
1199 * @tx_rate: Transmit rate divided by 10000 to get Mbit.
1200 * @duplex: Duplex mode.
1201 * @advertised: Advertised auto-negotiation setting. Used to determine link.
1202 * @partner: Auto-negotiation partner setting. Used to determine link.
1203 * @port_id: Port index to access actual hardware register.
1204 * @pdev: Pointer to OS dependent network device.
1206 struct ksz_port_info {
1207 uint state;
1208 uint tx_rate;
1209 u8 duplex;
1210 u8 advertised;
1211 u8 partner;
1212 u8 port_id;
1213 void *pdev;
1216 #define MAX_TX_HELD_SIZE 52000
1218 /* Hardware features and bug fixes. */
1219 #define LINK_INT_WORKING (1 << 0)
1220 #define SMALL_PACKET_TX_BUG (1 << 1)
1221 #define HALF_DUPLEX_SIGNAL_BUG (1 << 2)
1222 #define IPV6_CSUM_GEN_HACK (1 << 3)
1223 #define RX_HUGE_FRAME (1 << 4)
1224 #define STP_SUPPORT (1 << 8)
1226 /* Software overrides. */
1227 #define PAUSE_FLOW_CTRL (1 << 0)
1228 #define FAST_AGING (1 << 1)
1231 * struct ksz_hw - KSZ884X hardware data structure
1232 * @io: Virtual address assigned.
1233 * @ksz_switch: Pointer to KSZ8842 switch.
1234 * @port_info: Port information.
1235 * @port_mib: Port MIB information.
1236 * @dev_count: Number of network devices this hardware supports.
1237 * @dst_ports: Destination ports in switch for transmission.
1238 * @id: Hardware ID. Used for display only.
1239 * @mib_cnt: Number of MIB counters this hardware has.
1240 * @mib_port_cnt: Number of ports with MIB counters.
1241 * @tx_cfg: Cached transmit control settings.
1242 * @rx_cfg: Cached receive control settings.
1243 * @intr_mask: Current interrupt mask.
1244 * @intr_set: Current interrup set.
1245 * @intr_blocked: Interrupt blocked.
1246 * @rx_desc_info: Receive descriptor information.
1247 * @tx_desc_info: Transmit descriptor information.
1248 * @tx_int_cnt: Transmit interrupt count. Used for TX optimization.
1249 * @tx_int_mask: Transmit interrupt mask. Used for TX optimization.
1250 * @tx_size: Transmit data size. Used for TX optimization.
1251 * The maximum is defined by MAX_TX_HELD_SIZE.
1252 * @perm_addr: Permanent MAC address.
1253 * @override_addr: Overrided MAC address.
1254 * @address: Additional MAC address entries.
1255 * @addr_list_size: Additional MAC address list size.
1256 * @mac_override: Indication of MAC address overrided.
1257 * @promiscuous: Counter to keep track of promiscuous mode set.
1258 * @all_multi: Counter to keep track of all multicast mode set.
1259 * @multi_list: Multicast address entries.
1260 * @multi_bits: Cached multicast hash table settings.
1261 * @multi_list_size: Multicast address list size.
1262 * @enabled: Indication of hardware enabled.
1263 * @rx_stop: Indication of receive process stop.
1264 * @features: Hardware features to enable.
1265 * @overrides: Hardware features to override.
1266 * @parent: Pointer to parent, network device private structure.
1268 struct ksz_hw {
1269 void __iomem *io;
1271 struct ksz_switch *ksz_switch;
1272 struct ksz_port_info port_info[SWITCH_PORT_NUM];
1273 struct ksz_port_mib port_mib[TOTAL_PORT_NUM];
1274 int dev_count;
1275 int dst_ports;
1276 int id;
1277 int mib_cnt;
1278 int mib_port_cnt;
1280 u32 tx_cfg;
1281 u32 rx_cfg;
1282 u32 intr_mask;
1283 u32 intr_set;
1284 uint intr_blocked;
1286 struct ksz_desc_info rx_desc_info;
1287 struct ksz_desc_info tx_desc_info;
1289 int tx_int_cnt;
1290 int tx_int_mask;
1291 int tx_size;
1293 u8 perm_addr[MAC_ADDR_LEN];
1294 u8 override_addr[MAC_ADDR_LEN];
1295 u8 address[ADDITIONAL_ENTRIES][MAC_ADDR_LEN];
1296 u8 addr_list_size;
1297 u8 mac_override;
1298 u8 promiscuous;
1299 u8 all_multi;
1300 u8 multi_list[MAX_MULTICAST_LIST][MAC_ADDR_LEN];
1301 u8 multi_bits[HW_MULTICAST_SIZE];
1302 u8 multi_list_size;
1304 u8 enabled;
1305 u8 rx_stop;
1306 u8 reserved2[1];
1308 uint features;
1309 uint overrides;
1311 void *parent;
1314 enum {
1315 PHY_NO_FLOW_CTRL,
1316 PHY_FLOW_CTRL,
1317 PHY_TX_ONLY,
1318 PHY_RX_ONLY
1322 * struct ksz_port - Virtual port data structure
1323 * @duplex: Duplex mode setting. 1 for half duplex, 2 for full
1324 * duplex, and 0 for auto, which normally results in full
1325 * duplex.
1326 * @speed: Speed setting. 10 for 10 Mbit, 100 for 100 Mbit, and
1327 * 0 for auto, which normally results in 100 Mbit.
1328 * @force_link: Force link setting. 0 for auto-negotiation, and 1 for
1329 * force.
1330 * @flow_ctrl: Flow control setting. PHY_NO_FLOW_CTRL for no flow
1331 * control, and PHY_FLOW_CTRL for flow control.
1332 * PHY_TX_ONLY and PHY_RX_ONLY are not supported for 100
1333 * Mbit PHY.
1334 * @first_port: Index of first port this port supports.
1335 * @mib_port_cnt: Number of ports with MIB counters.
1336 * @port_cnt: Number of ports this port supports.
1337 * @counter: Port statistics counter.
1338 * @hw: Pointer to hardware structure.
1339 * @linked: Pointer to port information linked to this port.
1341 struct ksz_port {
1342 u8 duplex;
1343 u8 speed;
1344 u8 force_link;
1345 u8 flow_ctrl;
1347 int first_port;
1348 int mib_port_cnt;
1349 int port_cnt;
1350 u64 counter[OID_COUNTER_LAST];
1352 struct ksz_hw *hw;
1353 struct ksz_port_info *linked;
1357 * struct ksz_timer_info - Timer information data structure
1358 * @timer: Kernel timer.
1359 * @cnt: Running timer counter.
1360 * @max: Number of times to run timer; -1 for infinity.
1361 * @period: Timer period in jiffies.
1363 struct ksz_timer_info {
1364 struct timer_list timer;
1365 int cnt;
1366 int max;
1367 int period;
1371 * struct ksz_shared_mem - OS dependent shared memory data structure
1372 * @dma_addr: Physical DMA address allocated.
1373 * @alloc_size: Allocation size.
1374 * @phys: Actual physical address used.
1375 * @alloc_virt: Virtual address allocated.
1376 * @virt: Actual virtual address used.
1378 struct ksz_shared_mem {
1379 dma_addr_t dma_addr;
1380 uint alloc_size;
1381 uint phys;
1382 u8 *alloc_virt;
1383 u8 *virt;
1387 * struct ksz_counter_info - OS dependent counter information data structure
1388 * @counter: Wait queue to wakeup after counters are read.
1389 * @time: Next time in jiffies to read counter.
1390 * @read: Indication of counters read in full or not.
1392 struct ksz_counter_info {
1393 wait_queue_head_t counter;
1394 unsigned long time;
1395 int read;
1399 * struct dev_info - Network device information data structure
1400 * @dev: Pointer to network device.
1401 * @pdev: Pointer to PCI device.
1402 * @hw: Hardware structure.
1403 * @desc_pool: Physical memory used for descriptor pool.
1404 * @hwlock: Spinlock to prevent hardware from accessing.
1405 * @lock: Mutex lock to prevent device from accessing.
1406 * @dev_rcv: Receive process function used.
1407 * @last_skb: Socket buffer allocated for descriptor rx fragments.
1408 * @skb_index: Buffer index for receiving fragments.
1409 * @skb_len: Buffer length for receiving fragments.
1410 * @mib_read: Workqueue to read MIB counters.
1411 * @mib_timer_info: Timer to read MIB counters.
1412 * @counter: Used for MIB reading.
1413 * @mtu: Current MTU used. The default is REGULAR_RX_BUF_SIZE;
1414 * the maximum is MAX_RX_BUF_SIZE.
1415 * @opened: Counter to keep track of device open.
1416 * @rx_tasklet: Receive processing tasklet.
1417 * @tx_tasklet: Transmit processing tasklet.
1418 * @wol_enable: Wake-on-LAN enable set by ethtool.
1419 * @wol_support: Wake-on-LAN support used by ethtool.
1420 * @pme_wait: Used for KSZ8841 power management.
1422 struct dev_info {
1423 struct net_device *dev;
1424 struct pci_dev *pdev;
1426 struct ksz_hw hw;
1427 struct ksz_shared_mem desc_pool;
1429 spinlock_t hwlock;
1430 struct mutex lock;
1432 int (*dev_rcv)(struct dev_info *);
1434 struct sk_buff *last_skb;
1435 int skb_index;
1436 int skb_len;
1438 struct work_struct mib_read;
1439 struct ksz_timer_info mib_timer_info;
1440 struct ksz_counter_info counter[TOTAL_PORT_NUM];
1442 int mtu;
1443 int opened;
1445 struct tasklet_struct rx_tasklet;
1446 struct tasklet_struct tx_tasklet;
1448 int wol_enable;
1449 int wol_support;
1450 unsigned long pme_wait;
1454 * struct dev_priv - Network device private data structure
1455 * @adapter: Adapter device information.
1456 * @port: Port information.
1457 * @monitor_time_info: Timer to monitor ports.
1458 * @stats: Network statistics.
1459 * @proc_sem: Semaphore for proc accessing.
1460 * @id: Device ID.
1461 * @mii_if: MII interface information.
1462 * @advertising: Temporary variable to store advertised settings.
1463 * @msg_enable: The message flags controlling driver output.
1464 * @media_state: The connection status of the device.
1465 * @multicast: The all multicast state of the device.
1466 * @promiscuous: The promiscuous state of the device.
1468 struct dev_priv {
1469 struct dev_info *adapter;
1470 struct ksz_port port;
1471 struct ksz_timer_info monitor_timer_info;
1472 struct net_device_stats stats;
1474 struct semaphore proc_sem;
1475 int id;
1477 struct mii_if_info mii_if;
1478 u32 advertising;
1480 u32 msg_enable;
1481 int media_state;
1482 int multicast;
1483 int promiscuous;
1486 #define ks_info(_ks, _msg...) dev_info(&(_ks)->pdev->dev, _msg)
1487 #define ks_warn(_ks, _msg...) dev_warn(&(_ks)->pdev->dev, _msg)
1488 #define ks_dbg(_ks, _msg...) dev_dbg(&(_ks)->pdev->dev, _msg)
1489 #define ks_err(_ks, _msg...) dev_err(&(_ks)->pdev->dev, _msg)
1491 #define DRV_NAME "KSZ884X PCI"
1492 #define DEVICE_NAME "KSZ884x PCI"
1493 #define DRV_VERSION "1.0.0"
1494 #define DRV_RELDATE "Feb 8, 2010"
1496 static char version[] __devinitdata =
1497 "Micrel " DEVICE_NAME " " DRV_VERSION " (" DRV_RELDATE ")";
1499 static u8 DEFAULT_MAC_ADDRESS[] = { 0x00, 0x10, 0xA1, 0x88, 0x42, 0x01 };
1502 * Interrupt processing primary routines
1505 static inline void hw_ack_intr(struct ksz_hw *hw, uint interrupt)
1507 writel(interrupt, hw->io + KS884X_INTERRUPTS_STATUS);
1510 static inline void hw_dis_intr(struct ksz_hw *hw)
1512 hw->intr_blocked = hw->intr_mask;
1513 writel(0, hw->io + KS884X_INTERRUPTS_ENABLE);
1514 hw->intr_set = readl(hw->io + KS884X_INTERRUPTS_ENABLE);
1517 static inline void hw_set_intr(struct ksz_hw *hw, uint interrupt)
1519 hw->intr_set = interrupt;
1520 writel(interrupt, hw->io + KS884X_INTERRUPTS_ENABLE);
1523 static inline void hw_ena_intr(struct ksz_hw *hw)
1525 hw->intr_blocked = 0;
1526 hw_set_intr(hw, hw->intr_mask);
1529 static inline void hw_dis_intr_bit(struct ksz_hw *hw, uint bit)
1531 hw->intr_mask &= ~(bit);
1534 static inline void hw_turn_off_intr(struct ksz_hw *hw, uint interrupt)
1536 u32 read_intr;
1538 read_intr = readl(hw->io + KS884X_INTERRUPTS_ENABLE);
1539 hw->intr_set = read_intr & ~interrupt;
1540 writel(hw->intr_set, hw->io + KS884X_INTERRUPTS_ENABLE);
1541 hw_dis_intr_bit(hw, interrupt);
1545 * hw_turn_on_intr - turn on specified interrupts
1546 * @hw: The hardware instance.
1547 * @bit: The interrupt bits to be on.
1549 * This routine turns on the specified interrupts in the interrupt mask so that
1550 * those interrupts will be enabled.
1552 static void hw_turn_on_intr(struct ksz_hw *hw, u32 bit)
1554 hw->intr_mask |= bit;
1556 if (!hw->intr_blocked)
1557 hw_set_intr(hw, hw->intr_mask);
1560 static inline void hw_ena_intr_bit(struct ksz_hw *hw, uint interrupt)
1562 u32 read_intr;
1564 read_intr = readl(hw->io + KS884X_INTERRUPTS_ENABLE);
1565 hw->intr_set = read_intr | interrupt;
1566 writel(hw->intr_set, hw->io + KS884X_INTERRUPTS_ENABLE);
1569 static inline void hw_read_intr(struct ksz_hw *hw, uint *status)
1571 *status = readl(hw->io + KS884X_INTERRUPTS_STATUS);
1572 *status = *status & hw->intr_set;
1575 static inline void hw_restore_intr(struct ksz_hw *hw, uint interrupt)
1577 if (interrupt)
1578 hw_ena_intr(hw);
1582 * hw_block_intr - block hardware interrupts
1584 * This function blocks all interrupts of the hardware and returns the current
1585 * interrupt enable mask so that interrupts can be restored later.
1587 * Return the current interrupt enable mask.
1589 static uint hw_block_intr(struct ksz_hw *hw)
1591 uint interrupt = 0;
1593 if (!hw->intr_blocked) {
1594 hw_dis_intr(hw);
1595 interrupt = hw->intr_blocked;
1597 return interrupt;
1601 * Hardware descriptor routines
1604 static inline void reset_desc(struct ksz_desc *desc, union desc_stat status)
1606 status.rx.hw_owned = 0;
1607 desc->phw->ctrl.data = cpu_to_le32(status.data);
1610 static inline void release_desc(struct ksz_desc *desc)
1612 desc->sw.ctrl.tx.hw_owned = 1;
1613 if (desc->sw.buf_size != desc->sw.buf.data) {
1614 desc->sw.buf_size = desc->sw.buf.data;
1615 desc->phw->buf.data = cpu_to_le32(desc->sw.buf.data);
1617 desc->phw->ctrl.data = cpu_to_le32(desc->sw.ctrl.data);
1620 static void get_rx_pkt(struct ksz_desc_info *info, struct ksz_desc **desc)
1622 *desc = &info->ring[info->last];
1623 info->last++;
1624 info->last &= info->mask;
1625 info->avail--;
1626 (*desc)->sw.buf.data &= ~KS_DESC_RX_MASK;
1629 static inline void set_rx_buf(struct ksz_desc *desc, u32 addr)
1631 desc->phw->addr = cpu_to_le32(addr);
1634 static inline void set_rx_len(struct ksz_desc *desc, u32 len)
1636 desc->sw.buf.rx.buf_size = len;
1639 static inline void get_tx_pkt(struct ksz_desc_info *info,
1640 struct ksz_desc **desc)
1642 *desc = &info->ring[info->next];
1643 info->next++;
1644 info->next &= info->mask;
1645 info->avail--;
1646 (*desc)->sw.buf.data &= ~KS_DESC_TX_MASK;
1649 static inline void set_tx_buf(struct ksz_desc *desc, u32 addr)
1651 desc->phw->addr = cpu_to_le32(addr);
1654 static inline void set_tx_len(struct ksz_desc *desc, u32 len)
1656 desc->sw.buf.tx.buf_size = len;
1659 /* Switch functions */
1661 #define TABLE_READ 0x10
1662 #define TABLE_SEL_SHIFT 2
1664 #define HW_DELAY(hw, reg) \
1665 do { \
1666 u16 dummy; \
1667 dummy = readw(hw->io + reg); \
1668 } while (0)
1671 * sw_r_table - read 4 bytes of data from switch table
1672 * @hw: The hardware instance.
1673 * @table: The table selector.
1674 * @addr: The address of the table entry.
1675 * @data: Buffer to store the read data.
1677 * This routine reads 4 bytes of data from the table of the switch.
1678 * Hardware interrupts are disabled to minimize corruption of read data.
1680 static void sw_r_table(struct ksz_hw *hw, int table, u16 addr, u32 *data)
1682 u16 ctrl_addr;
1683 uint interrupt;
1685 ctrl_addr = (((table << TABLE_SEL_SHIFT) | TABLE_READ) << 8) | addr;
1687 interrupt = hw_block_intr(hw);
1689 writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
1690 HW_DELAY(hw, KS884X_IACR_OFFSET);
1691 *data = readl(hw->io + KS884X_ACC_DATA_0_OFFSET);
1693 hw_restore_intr(hw, interrupt);
1697 * sw_w_table_64 - write 8 bytes of data to the switch table
1698 * @hw: The hardware instance.
1699 * @table: The table selector.
1700 * @addr: The address of the table entry.
1701 * @data_hi: The high part of data to be written (bit63 ~ bit32).
1702 * @data_lo: The low part of data to be written (bit31 ~ bit0).
1704 * This routine writes 8 bytes of data to the table of the switch.
1705 * Hardware interrupts are disabled to minimize corruption of written data.
1707 static void sw_w_table_64(struct ksz_hw *hw, int table, u16 addr, u32 data_hi,
1708 u32 data_lo)
1710 u16 ctrl_addr;
1711 uint interrupt;
1713 ctrl_addr = ((table << TABLE_SEL_SHIFT) << 8) | addr;
1715 interrupt = hw_block_intr(hw);
1717 writel(data_hi, hw->io + KS884X_ACC_DATA_4_OFFSET);
1718 writel(data_lo, hw->io + KS884X_ACC_DATA_0_OFFSET);
1720 writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
1721 HW_DELAY(hw, KS884X_IACR_OFFSET);
1723 hw_restore_intr(hw, interrupt);
1727 * sw_w_sta_mac_table - write to the static MAC table
1728 * @hw: The hardware instance.
1729 * @addr: The address of the table entry.
1730 * @mac_addr: The MAC address.
1731 * @ports: The port members.
1732 * @override: The flag to override the port receive/transmit settings.
1733 * @valid: The flag to indicate entry is valid.
1734 * @use_fid: The flag to indicate the FID is valid.
1735 * @fid: The FID value.
1737 * This routine writes an entry of the static MAC table of the switch. It
1738 * calls sw_w_table_64() to write the data.
1740 static void sw_w_sta_mac_table(struct ksz_hw *hw, u16 addr, u8 *mac_addr,
1741 u8 ports, int override, int valid, int use_fid, u8 fid)
1743 u32 data_hi;
1744 u32 data_lo;
1746 data_lo = ((u32) mac_addr[2] << 24) |
1747 ((u32) mac_addr[3] << 16) |
1748 ((u32) mac_addr[4] << 8) | mac_addr[5];
1749 data_hi = ((u32) mac_addr[0] << 8) | mac_addr[1];
1750 data_hi |= (u32) ports << STATIC_MAC_FWD_PORTS_SHIFT;
1752 if (override)
1753 data_hi |= STATIC_MAC_TABLE_OVERRIDE;
1754 if (use_fid) {
1755 data_hi |= STATIC_MAC_TABLE_USE_FID;
1756 data_hi |= (u32) fid << STATIC_MAC_FID_SHIFT;
1758 if (valid)
1759 data_hi |= STATIC_MAC_TABLE_VALID;
1761 sw_w_table_64(hw, TABLE_STATIC_MAC, addr, data_hi, data_lo);
1765 * sw_r_vlan_table - read from the VLAN table
1766 * @hw: The hardware instance.
1767 * @addr: The address of the table entry.
1768 * @vid: Buffer to store the VID.
1769 * @fid: Buffer to store the VID.
1770 * @member: Buffer to store the port membership.
1772 * This function reads an entry of the VLAN table of the switch. It calls
1773 * sw_r_table() to get the data.
1775 * Return 0 if the entry is valid; otherwise -1.
1777 static int sw_r_vlan_table(struct ksz_hw *hw, u16 addr, u16 *vid, u8 *fid,
1778 u8 *member)
1780 u32 data;
1782 sw_r_table(hw, TABLE_VLAN, addr, &data);
1783 if (data & VLAN_TABLE_VALID) {
1784 *vid = (u16)(data & VLAN_TABLE_VID);
1785 *fid = (u8)((data & VLAN_TABLE_FID) >> VLAN_TABLE_FID_SHIFT);
1786 *member = (u8)((data & VLAN_TABLE_MEMBERSHIP) >>
1787 VLAN_TABLE_MEMBERSHIP_SHIFT);
1788 return 0;
1790 return -1;
1794 * port_r_mib_cnt - read MIB counter
1795 * @hw: The hardware instance.
1796 * @port: The port index.
1797 * @addr: The address of the counter.
1798 * @cnt: Buffer to store the counter.
1800 * This routine reads a MIB counter of the port.
1801 * Hardware interrupts are disabled to minimize corruption of read data.
1803 static void port_r_mib_cnt(struct ksz_hw *hw, int port, u16 addr, u64 *cnt)
1805 u32 data;
1806 u16 ctrl_addr;
1807 uint interrupt;
1808 int timeout;
1810 ctrl_addr = addr + PORT_COUNTER_NUM * port;
1812 interrupt = hw_block_intr(hw);
1814 ctrl_addr |= (((TABLE_MIB << TABLE_SEL_SHIFT) | TABLE_READ) << 8);
1815 writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
1816 HW_DELAY(hw, KS884X_IACR_OFFSET);
1818 for (timeout = 100; timeout > 0; timeout--) {
1819 data = readl(hw->io + KS884X_ACC_DATA_0_OFFSET);
1821 if (data & MIB_COUNTER_VALID) {
1822 if (data & MIB_COUNTER_OVERFLOW)
1823 *cnt += MIB_COUNTER_VALUE + 1;
1824 *cnt += data & MIB_COUNTER_VALUE;
1825 break;
1829 hw_restore_intr(hw, interrupt);
1833 * port_r_mib_pkt - read dropped packet counts
1834 * @hw: The hardware instance.
1835 * @port: The port index.
1836 * @cnt: Buffer to store the receive and transmit dropped packet counts.
1838 * This routine reads the dropped packet counts of the port.
1839 * Hardware interrupts are disabled to minimize corruption of read data.
1841 static void port_r_mib_pkt(struct ksz_hw *hw, int port, u32 *last, u64 *cnt)
1843 u32 cur;
1844 u32 data;
1845 u16 ctrl_addr;
1846 uint interrupt;
1847 int index;
1849 index = KS_MIB_PACKET_DROPPED_RX_0 + port;
1850 do {
1851 interrupt = hw_block_intr(hw);
1853 ctrl_addr = (u16) index;
1854 ctrl_addr |= (((TABLE_MIB << TABLE_SEL_SHIFT) | TABLE_READ)
1855 << 8);
1856 writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
1857 HW_DELAY(hw, KS884X_IACR_OFFSET);
1858 data = readl(hw->io + KS884X_ACC_DATA_0_OFFSET);
1860 hw_restore_intr(hw, interrupt);
1862 data &= MIB_PACKET_DROPPED;
1863 cur = *last;
1864 if (data != cur) {
1865 *last = data;
1866 if (data < cur)
1867 data += MIB_PACKET_DROPPED + 1;
1868 data -= cur;
1869 *cnt += data;
1871 ++last;
1872 ++cnt;
1873 index -= KS_MIB_PACKET_DROPPED_TX -
1874 KS_MIB_PACKET_DROPPED_TX_0 + 1;
1875 } while (index >= KS_MIB_PACKET_DROPPED_TX_0 + port);
1879 * port_r_cnt - read MIB counters periodically
1880 * @hw: The hardware instance.
1881 * @port: The port index.
1883 * This routine is used to read the counters of the port periodically to avoid
1884 * counter overflow. The hardware should be acquired first before calling this
1885 * routine.
1887 * Return non-zero when not all counters not read.
1889 static int port_r_cnt(struct ksz_hw *hw, int port)
1891 struct ksz_port_mib *mib = &hw->port_mib[port];
1893 if (mib->mib_start < PORT_COUNTER_NUM)
1894 while (mib->cnt_ptr < PORT_COUNTER_NUM) {
1895 port_r_mib_cnt(hw, port, mib->cnt_ptr,
1896 &mib->counter[mib->cnt_ptr]);
1897 ++mib->cnt_ptr;
1899 if (hw->mib_cnt > PORT_COUNTER_NUM)
1900 port_r_mib_pkt(hw, port, mib->dropped,
1901 &mib->counter[PORT_COUNTER_NUM]);
1902 mib->cnt_ptr = 0;
1903 return 0;
1907 * port_init_cnt - initialize MIB counter values
1908 * @hw: The hardware instance.
1909 * @port: The port index.
1911 * This routine is used to initialize all counters to zero if the hardware
1912 * cannot do it after reset.
1914 static void port_init_cnt(struct ksz_hw *hw, int port)
1916 struct ksz_port_mib *mib = &hw->port_mib[port];
1918 mib->cnt_ptr = 0;
1919 if (mib->mib_start < PORT_COUNTER_NUM)
1920 do {
1921 port_r_mib_cnt(hw, port, mib->cnt_ptr,
1922 &mib->counter[mib->cnt_ptr]);
1923 ++mib->cnt_ptr;
1924 } while (mib->cnt_ptr < PORT_COUNTER_NUM);
1925 if (hw->mib_cnt > PORT_COUNTER_NUM)
1926 port_r_mib_pkt(hw, port, mib->dropped,
1927 &mib->counter[PORT_COUNTER_NUM]);
1928 memset((void *) mib->counter, 0, sizeof(u64) * TOTAL_PORT_COUNTER_NUM);
1929 mib->cnt_ptr = 0;
1933 * Port functions
1937 * port_chk - check port register bits
1938 * @hw: The hardware instance.
1939 * @port: The port index.
1940 * @offset: The offset of the port register.
1941 * @bits: The data bits to check.
1943 * This function checks whether the specified bits of the port register are set
1944 * or not.
1946 * Return 0 if the bits are not set.
1948 static int port_chk(struct ksz_hw *hw, int port, int offset, u16 bits)
1950 u32 addr;
1951 u16 data;
1953 PORT_CTRL_ADDR(port, addr);
1954 addr += offset;
1955 data = readw(hw->io + addr);
1956 return (data & bits) == bits;
1960 * port_cfg - set port register bits
1961 * @hw: The hardware instance.
1962 * @port: The port index.
1963 * @offset: The offset of the port register.
1964 * @bits: The data bits to set.
1965 * @set: The flag indicating whether the bits are to be set or not.
1967 * This routine sets or resets the specified bits of the port register.
1969 static void port_cfg(struct ksz_hw *hw, int port, int offset, u16 bits,
1970 int set)
1972 u32 addr;
1973 u16 data;
1975 PORT_CTRL_ADDR(port, addr);
1976 addr += offset;
1977 data = readw(hw->io + addr);
1978 if (set)
1979 data |= bits;
1980 else
1981 data &= ~bits;
1982 writew(data, hw->io + addr);
1986 * port_chk_shift - check port bit
1987 * @hw: The hardware instance.
1988 * @port: The port index.
1989 * @offset: The offset of the register.
1990 * @shift: Number of bits to shift.
1992 * This function checks whether the specified port is set in the register or
1993 * not.
1995 * Return 0 if the port is not set.
1997 static int port_chk_shift(struct ksz_hw *hw, int port, u32 addr, int shift)
1999 u16 data;
2000 u16 bit = 1 << port;
2002 data = readw(hw->io + addr);
2003 data >>= shift;
2004 return (data & bit) == bit;
2008 * port_cfg_shift - set port bit
2009 * @hw: The hardware instance.
2010 * @port: The port index.
2011 * @offset: The offset of the register.
2012 * @shift: Number of bits to shift.
2013 * @set: The flag indicating whether the port is to be set or not.
2015 * This routine sets or resets the specified port in the register.
2017 static void port_cfg_shift(struct ksz_hw *hw, int port, u32 addr, int shift,
2018 int set)
2020 u16 data;
2021 u16 bits = 1 << port;
2023 data = readw(hw->io + addr);
2024 bits <<= shift;
2025 if (set)
2026 data |= bits;
2027 else
2028 data &= ~bits;
2029 writew(data, hw->io + addr);
2033 * port_r8 - read byte from port register
2034 * @hw: The hardware instance.
2035 * @port: The port index.
2036 * @offset: The offset of the port register.
2037 * @data: Buffer to store the data.
2039 * This routine reads a byte from the port register.
2041 static void port_r8(struct ksz_hw *hw, int port, int offset, u8 *data)
2043 u32 addr;
2045 PORT_CTRL_ADDR(port, addr);
2046 addr += offset;
2047 *data = readb(hw->io + addr);
2051 * port_r16 - read word from port register.
2052 * @hw: The hardware instance.
2053 * @port: The port index.
2054 * @offset: The offset of the port register.
2055 * @data: Buffer to store the data.
2057 * This routine reads a word from the port register.
2059 static void port_r16(struct ksz_hw *hw, int port, int offset, u16 *data)
2061 u32 addr;
2063 PORT_CTRL_ADDR(port, addr);
2064 addr += offset;
2065 *data = readw(hw->io + addr);
2069 * port_w16 - write word to port register.
2070 * @hw: The hardware instance.
2071 * @port: The port index.
2072 * @offset: The offset of the port register.
2073 * @data: Data to write.
2075 * This routine writes a word to the port register.
2077 static void port_w16(struct ksz_hw *hw, int port, int offset, u16 data)
2079 u32 addr;
2081 PORT_CTRL_ADDR(port, addr);
2082 addr += offset;
2083 writew(data, hw->io + addr);
2087 * sw_chk - check switch register bits
2088 * @hw: The hardware instance.
2089 * @addr: The address of the switch register.
2090 * @bits: The data bits to check.
2092 * This function checks whether the specified bits of the switch register are
2093 * set or not.
2095 * Return 0 if the bits are not set.
2097 static int sw_chk(struct ksz_hw *hw, u32 addr, u16 bits)
2099 u16 data;
2101 data = readw(hw->io + addr);
2102 return (data & bits) == bits;
2106 * sw_cfg - set switch register bits
2107 * @hw: The hardware instance.
2108 * @addr: The address of the switch register.
2109 * @bits: The data bits to set.
2110 * @set: The flag indicating whether the bits are to be set or not.
2112 * This function sets or resets the specified bits of the switch register.
2114 static void sw_cfg(struct ksz_hw *hw, u32 addr, u16 bits, int set)
2116 u16 data;
2118 data = readw(hw->io + addr);
2119 if (set)
2120 data |= bits;
2121 else
2122 data &= ~bits;
2123 writew(data, hw->io + addr);
2126 /* Bandwidth */
2128 static inline void port_cfg_broad_storm(struct ksz_hw *hw, int p, int set)
2130 port_cfg(hw, p,
2131 KS8842_PORT_CTRL_1_OFFSET, PORT_BROADCAST_STORM, set);
2134 static inline int port_chk_broad_storm(struct ksz_hw *hw, int p)
2136 return port_chk(hw, p,
2137 KS8842_PORT_CTRL_1_OFFSET, PORT_BROADCAST_STORM);
2140 /* Driver set switch broadcast storm protection at 10% rate. */
2141 #define BROADCAST_STORM_PROTECTION_RATE 10
2143 /* 148,800 frames * 67 ms / 100 */
2144 #define BROADCAST_STORM_VALUE 9969
2147 * sw_cfg_broad_storm - configure broadcast storm threshold
2148 * @hw: The hardware instance.
2149 * @percent: Broadcast storm threshold in percent of transmit rate.
2151 * This routine configures the broadcast storm threshold of the switch.
2153 static void sw_cfg_broad_storm(struct ksz_hw *hw, u8 percent)
2155 u16 data;
2156 u32 value = ((u32) BROADCAST_STORM_VALUE * (u32) percent / 100);
2158 if (value > BROADCAST_STORM_RATE)
2159 value = BROADCAST_STORM_RATE;
2161 data = readw(hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2162 data &= ~(BROADCAST_STORM_RATE_LO | BROADCAST_STORM_RATE_HI);
2163 data |= ((value & 0x00FF) << 8) | ((value & 0xFF00) >> 8);
2164 writew(data, hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2168 * sw_get_board_storm - get broadcast storm threshold
2169 * @hw: The hardware instance.
2170 * @percent: Buffer to store the broadcast storm threshold percentage.
2172 * This routine retrieves the broadcast storm threshold of the switch.
2174 static void sw_get_broad_storm(struct ksz_hw *hw, u8 *percent)
2176 int num;
2177 u16 data;
2179 data = readw(hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2180 num = (data & BROADCAST_STORM_RATE_HI);
2181 num <<= 8;
2182 num |= (data & BROADCAST_STORM_RATE_LO) >> 8;
2183 num = (num * 100 + BROADCAST_STORM_VALUE / 2) / BROADCAST_STORM_VALUE;
2184 *percent = (u8) num;
2188 * sw_dis_broad_storm - disable broadstorm
2189 * @hw: The hardware instance.
2190 * @port: The port index.
2192 * This routine disables the broadcast storm limit function of the switch.
2194 static void sw_dis_broad_storm(struct ksz_hw *hw, int port)
2196 port_cfg_broad_storm(hw, port, 0);
2200 * sw_ena_broad_storm - enable broadcast storm
2201 * @hw: The hardware instance.
2202 * @port: The port index.
2204 * This routine enables the broadcast storm limit function of the switch.
2206 static void sw_ena_broad_storm(struct ksz_hw *hw, int port)
2208 sw_cfg_broad_storm(hw, hw->ksz_switch->broad_per);
2209 port_cfg_broad_storm(hw, port, 1);
2213 * sw_init_broad_storm - initialize broadcast storm
2214 * @hw: The hardware instance.
2216 * This routine initializes the broadcast storm limit function of the switch.
2218 static void sw_init_broad_storm(struct ksz_hw *hw)
2220 int port;
2222 hw->ksz_switch->broad_per = 1;
2223 sw_cfg_broad_storm(hw, hw->ksz_switch->broad_per);
2224 for (port = 0; port < TOTAL_PORT_NUM; port++)
2225 sw_dis_broad_storm(hw, port);
2226 sw_cfg(hw, KS8842_SWITCH_CTRL_2_OFFSET, MULTICAST_STORM_DISABLE, 1);
2230 * hw_cfg_broad_storm - configure broadcast storm
2231 * @hw: The hardware instance.
2232 * @percent: Broadcast storm threshold in percent of transmit rate.
2234 * This routine configures the broadcast storm threshold of the switch.
2235 * It is called by user functions. The hardware should be acquired first.
2237 static void hw_cfg_broad_storm(struct ksz_hw *hw, u8 percent)
2239 if (percent > 100)
2240 percent = 100;
2242 sw_cfg_broad_storm(hw, percent);
2243 sw_get_broad_storm(hw, &percent);
2244 hw->ksz_switch->broad_per = percent;
2248 * sw_dis_prio_rate - disable switch priority rate
2249 * @hw: The hardware instance.
2250 * @port: The port index.
2252 * This routine disables the priority rate function of the switch.
2254 static void sw_dis_prio_rate(struct ksz_hw *hw, int port)
2256 u32 addr;
2258 PORT_CTRL_ADDR(port, addr);
2259 addr += KS8842_PORT_IN_RATE_OFFSET;
2260 writel(0, hw->io + addr);
2264 * sw_init_prio_rate - initialize switch prioirty rate
2265 * @hw: The hardware instance.
2267 * This routine initializes the priority rate function of the switch.
2269 static void sw_init_prio_rate(struct ksz_hw *hw)
2271 int port;
2272 int prio;
2273 struct ksz_switch *sw = hw->ksz_switch;
2275 for (port = 0; port < TOTAL_PORT_NUM; port++) {
2276 for (prio = 0; prio < PRIO_QUEUES; prio++) {
2277 sw->port_cfg[port].rx_rate[prio] =
2278 sw->port_cfg[port].tx_rate[prio] = 0;
2280 sw_dis_prio_rate(hw, port);
2284 /* Communication */
2286 static inline void port_cfg_back_pressure(struct ksz_hw *hw, int p, int set)
2288 port_cfg(hw, p,
2289 KS8842_PORT_CTRL_2_OFFSET, PORT_BACK_PRESSURE, set);
2292 static inline void port_cfg_force_flow_ctrl(struct ksz_hw *hw, int p, int set)
2294 port_cfg(hw, p,
2295 KS8842_PORT_CTRL_2_OFFSET, PORT_FORCE_FLOW_CTRL, set);
2298 static inline int port_chk_back_pressure(struct ksz_hw *hw, int p)
2300 return port_chk(hw, p,
2301 KS8842_PORT_CTRL_2_OFFSET, PORT_BACK_PRESSURE);
2304 static inline int port_chk_force_flow_ctrl(struct ksz_hw *hw, int p)
2306 return port_chk(hw, p,
2307 KS8842_PORT_CTRL_2_OFFSET, PORT_FORCE_FLOW_CTRL);
2310 /* Spanning Tree */
2312 static inline void port_cfg_dis_learn(struct ksz_hw *hw, int p, int set)
2314 port_cfg(hw, p,
2315 KS8842_PORT_CTRL_2_OFFSET, PORT_LEARN_DISABLE, set);
2318 static inline void port_cfg_rx(struct ksz_hw *hw, int p, int set)
2320 port_cfg(hw, p,
2321 KS8842_PORT_CTRL_2_OFFSET, PORT_RX_ENABLE, set);
2324 static inline void port_cfg_tx(struct ksz_hw *hw, int p, int set)
2326 port_cfg(hw, p,
2327 KS8842_PORT_CTRL_2_OFFSET, PORT_TX_ENABLE, set);
2330 static inline void sw_cfg_fast_aging(struct ksz_hw *hw, int set)
2332 sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET, SWITCH_FAST_AGING, set);
2335 static inline void sw_flush_dyn_mac_table(struct ksz_hw *hw)
2337 if (!(hw->overrides & FAST_AGING)) {
2338 sw_cfg_fast_aging(hw, 1);
2339 mdelay(1);
2340 sw_cfg_fast_aging(hw, 0);
2344 /* VLAN */
2346 static inline void port_cfg_ins_tag(struct ksz_hw *hw, int p, int insert)
2348 port_cfg(hw, p,
2349 KS8842_PORT_CTRL_1_OFFSET, PORT_INSERT_TAG, insert);
2352 static inline void port_cfg_rmv_tag(struct ksz_hw *hw, int p, int remove)
2354 port_cfg(hw, p,
2355 KS8842_PORT_CTRL_1_OFFSET, PORT_REMOVE_TAG, remove);
2358 static inline int port_chk_ins_tag(struct ksz_hw *hw, int p)
2360 return port_chk(hw, p,
2361 KS8842_PORT_CTRL_1_OFFSET, PORT_INSERT_TAG);
2364 static inline int port_chk_rmv_tag(struct ksz_hw *hw, int p)
2366 return port_chk(hw, p,
2367 KS8842_PORT_CTRL_1_OFFSET, PORT_REMOVE_TAG);
2370 static inline void port_cfg_dis_non_vid(struct ksz_hw *hw, int p, int set)
2372 port_cfg(hw, p,
2373 KS8842_PORT_CTRL_2_OFFSET, PORT_DISCARD_NON_VID, set);
2376 static inline void port_cfg_in_filter(struct ksz_hw *hw, int p, int set)
2378 port_cfg(hw, p,
2379 KS8842_PORT_CTRL_2_OFFSET, PORT_INGRESS_VLAN_FILTER, set);
2382 static inline int port_chk_dis_non_vid(struct ksz_hw *hw, int p)
2384 return port_chk(hw, p,
2385 KS8842_PORT_CTRL_2_OFFSET, PORT_DISCARD_NON_VID);
2388 static inline int port_chk_in_filter(struct ksz_hw *hw, int p)
2390 return port_chk(hw, p,
2391 KS8842_PORT_CTRL_2_OFFSET, PORT_INGRESS_VLAN_FILTER);
2394 /* Mirroring */
2396 static inline void port_cfg_mirror_sniffer(struct ksz_hw *hw, int p, int set)
2398 port_cfg(hw, p,
2399 KS8842_PORT_CTRL_2_OFFSET, PORT_MIRROR_SNIFFER, set);
2402 static inline void port_cfg_mirror_rx(struct ksz_hw *hw, int p, int set)
2404 port_cfg(hw, p,
2405 KS8842_PORT_CTRL_2_OFFSET, PORT_MIRROR_RX, set);
2408 static inline void port_cfg_mirror_tx(struct ksz_hw *hw, int p, int set)
2410 port_cfg(hw, p,
2411 KS8842_PORT_CTRL_2_OFFSET, PORT_MIRROR_TX, set);
2414 static inline void sw_cfg_mirror_rx_tx(struct ksz_hw *hw, int set)
2416 sw_cfg(hw, KS8842_SWITCH_CTRL_2_OFFSET, SWITCH_MIRROR_RX_TX, set);
2419 static void sw_init_mirror(struct ksz_hw *hw)
2421 int port;
2423 for (port = 0; port < TOTAL_PORT_NUM; port++) {
2424 port_cfg_mirror_sniffer(hw, port, 0);
2425 port_cfg_mirror_rx(hw, port, 0);
2426 port_cfg_mirror_tx(hw, port, 0);
2428 sw_cfg_mirror_rx_tx(hw, 0);
2431 static inline void sw_cfg_unk_def_deliver(struct ksz_hw *hw, int set)
2433 sw_cfg(hw, KS8842_SWITCH_CTRL_7_OFFSET,
2434 SWITCH_UNK_DEF_PORT_ENABLE, set);
2437 static inline int sw_cfg_chk_unk_def_deliver(struct ksz_hw *hw)
2439 return sw_chk(hw, KS8842_SWITCH_CTRL_7_OFFSET,
2440 SWITCH_UNK_DEF_PORT_ENABLE);
2443 static inline void sw_cfg_unk_def_port(struct ksz_hw *hw, int port, int set)
2445 port_cfg_shift(hw, port, KS8842_SWITCH_CTRL_7_OFFSET, 0, set);
2448 static inline int sw_chk_unk_def_port(struct ksz_hw *hw, int port)
2450 return port_chk_shift(hw, port, KS8842_SWITCH_CTRL_7_OFFSET, 0);
2453 /* Priority */
2455 static inline void port_cfg_diffserv(struct ksz_hw *hw, int p, int set)
2457 port_cfg(hw, p,
2458 KS8842_PORT_CTRL_1_OFFSET, PORT_DIFFSERV_ENABLE, set);
2461 static inline void port_cfg_802_1p(struct ksz_hw *hw, int p, int set)
2463 port_cfg(hw, p,
2464 KS8842_PORT_CTRL_1_OFFSET, PORT_802_1P_ENABLE, set);
2467 static inline void port_cfg_replace_vid(struct ksz_hw *hw, int p, int set)
2469 port_cfg(hw, p,
2470 KS8842_PORT_CTRL_2_OFFSET, PORT_USER_PRIORITY_CEILING, set);
2473 static inline void port_cfg_prio(struct ksz_hw *hw, int p, int set)
2475 port_cfg(hw, p,
2476 KS8842_PORT_CTRL_1_OFFSET, PORT_PRIO_QUEUE_ENABLE, set);
2479 static inline int port_chk_diffserv(struct ksz_hw *hw, int p)
2481 return port_chk(hw, p,
2482 KS8842_PORT_CTRL_1_OFFSET, PORT_DIFFSERV_ENABLE);
2485 static inline int port_chk_802_1p(struct ksz_hw *hw, int p)
2487 return port_chk(hw, p,
2488 KS8842_PORT_CTRL_1_OFFSET, PORT_802_1P_ENABLE);
2491 static inline int port_chk_replace_vid(struct ksz_hw *hw, int p)
2493 return port_chk(hw, p,
2494 KS8842_PORT_CTRL_2_OFFSET, PORT_USER_PRIORITY_CEILING);
2497 static inline int port_chk_prio(struct ksz_hw *hw, int p)
2499 return port_chk(hw, p,
2500 KS8842_PORT_CTRL_1_OFFSET, PORT_PRIO_QUEUE_ENABLE);
2504 * sw_dis_diffserv - disable switch DiffServ priority
2505 * @hw: The hardware instance.
2506 * @port: The port index.
2508 * This routine disables the DiffServ priority function of the switch.
2510 static void sw_dis_diffserv(struct ksz_hw *hw, int port)
2512 port_cfg_diffserv(hw, port, 0);
2516 * sw_dis_802_1p - disable switch 802.1p priority
2517 * @hw: The hardware instance.
2518 * @port: The port index.
2520 * This routine disables the 802.1p priority function of the switch.
2522 static void sw_dis_802_1p(struct ksz_hw *hw, int port)
2524 port_cfg_802_1p(hw, port, 0);
2528 * sw_cfg_replace_null_vid -
2529 * @hw: The hardware instance.
2530 * @set: The flag to disable or enable.
2533 static void sw_cfg_replace_null_vid(struct ksz_hw *hw, int set)
2535 sw_cfg(hw, KS8842_SWITCH_CTRL_3_OFFSET, SWITCH_REPLACE_NULL_VID, set);
2539 * sw_cfg_replace_vid - enable switch 802.10 priority re-mapping
2540 * @hw: The hardware instance.
2541 * @port: The port index.
2542 * @set: The flag to disable or enable.
2544 * This routine enables the 802.1p priority re-mapping function of the switch.
2545 * That allows 802.1p priority field to be replaced with the port's default
2546 * tag's priority value if the ingress packet's 802.1p priority has a higher
2547 * priority than port's default tag's priority.
2549 static void sw_cfg_replace_vid(struct ksz_hw *hw, int port, int set)
2551 port_cfg_replace_vid(hw, port, set);
2555 * sw_cfg_port_based - configure switch port based priority
2556 * @hw: The hardware instance.
2557 * @port: The port index.
2558 * @prio: The priority to set.
2560 * This routine configures the port based priority of the switch.
2562 static void sw_cfg_port_based(struct ksz_hw *hw, int port, u8 prio)
2564 u16 data;
2566 if (prio > PORT_BASED_PRIORITY_BASE)
2567 prio = PORT_BASED_PRIORITY_BASE;
2569 hw->ksz_switch->port_cfg[port].port_prio = prio;
2571 port_r16(hw, port, KS8842_PORT_CTRL_1_OFFSET, &data);
2572 data &= ~PORT_BASED_PRIORITY_MASK;
2573 data |= prio << PORT_BASED_PRIORITY_SHIFT;
2574 port_w16(hw, port, KS8842_PORT_CTRL_1_OFFSET, data);
2578 * sw_dis_multi_queue - disable transmit multiple queues
2579 * @hw: The hardware instance.
2580 * @port: The port index.
2582 * This routine disables the transmit multiple queues selection of the switch
2583 * port. Only single transmit queue on the port.
2585 static void sw_dis_multi_queue(struct ksz_hw *hw, int port)
2587 port_cfg_prio(hw, port, 0);
2591 * sw_init_prio - initialize switch priority
2592 * @hw: The hardware instance.
2594 * This routine initializes the switch QoS priority functions.
2596 static void sw_init_prio(struct ksz_hw *hw)
2598 int port;
2599 int tos;
2600 struct ksz_switch *sw = hw->ksz_switch;
2603 * Init all the 802.1p tag priority value to be assigned to different
2604 * priority queue.
2606 sw->p_802_1p[0] = 0;
2607 sw->p_802_1p[1] = 0;
2608 sw->p_802_1p[2] = 1;
2609 sw->p_802_1p[3] = 1;
2610 sw->p_802_1p[4] = 2;
2611 sw->p_802_1p[5] = 2;
2612 sw->p_802_1p[6] = 3;
2613 sw->p_802_1p[7] = 3;
2616 * Init all the DiffServ priority value to be assigned to priority
2617 * queue 0.
2619 for (tos = 0; tos < DIFFSERV_ENTRIES; tos++)
2620 sw->diffserv[tos] = 0;
2622 /* All QoS functions disabled. */
2623 for (port = 0; port < TOTAL_PORT_NUM; port++) {
2624 sw_dis_multi_queue(hw, port);
2625 sw_dis_diffserv(hw, port);
2626 sw_dis_802_1p(hw, port);
2627 sw_cfg_replace_vid(hw, port, 0);
2629 sw->port_cfg[port].port_prio = 0;
2630 sw_cfg_port_based(hw, port, sw->port_cfg[port].port_prio);
2632 sw_cfg_replace_null_vid(hw, 0);
2636 * port_get_def_vid - get port default VID.
2637 * @hw: The hardware instance.
2638 * @port: The port index.
2639 * @vid: Buffer to store the VID.
2641 * This routine retrieves the default VID of the port.
2643 static void port_get_def_vid(struct ksz_hw *hw, int port, u16 *vid)
2645 u32 addr;
2647 PORT_CTRL_ADDR(port, addr);
2648 addr += KS8842_PORT_CTRL_VID_OFFSET;
2649 *vid = readw(hw->io + addr);
2653 * sw_init_vlan - initialize switch VLAN
2654 * @hw: The hardware instance.
2656 * This routine initializes the VLAN function of the switch.
2658 static void sw_init_vlan(struct ksz_hw *hw)
2660 int port;
2661 int entry;
2662 struct ksz_switch *sw = hw->ksz_switch;
2664 /* Read 16 VLAN entries from device's VLAN table. */
2665 for (entry = 0; entry < VLAN_TABLE_ENTRIES; entry++) {
2666 sw_r_vlan_table(hw, entry,
2667 &sw->vlan_table[entry].vid,
2668 &sw->vlan_table[entry].fid,
2669 &sw->vlan_table[entry].member);
2672 for (port = 0; port < TOTAL_PORT_NUM; port++) {
2673 port_get_def_vid(hw, port, &sw->port_cfg[port].vid);
2674 sw->port_cfg[port].member = PORT_MASK;
2679 * sw_cfg_port_base_vlan - configure port-based VLAN membership
2680 * @hw: The hardware instance.
2681 * @port: The port index.
2682 * @member: The port-based VLAN membership.
2684 * This routine configures the port-based VLAN membership of the port.
2686 static void sw_cfg_port_base_vlan(struct ksz_hw *hw, int port, u8 member)
2688 u32 addr;
2689 u8 data;
2691 PORT_CTRL_ADDR(port, addr);
2692 addr += KS8842_PORT_CTRL_2_OFFSET;
2694 data = readb(hw->io + addr);
2695 data &= ~PORT_VLAN_MEMBERSHIP;
2696 data |= (member & PORT_MASK);
2697 writeb(data, hw->io + addr);
2699 hw->ksz_switch->port_cfg[port].member = member;
2703 * sw_get_addr - get the switch MAC address.
2704 * @hw: The hardware instance.
2705 * @mac_addr: Buffer to store the MAC address.
2707 * This function retrieves the MAC address of the switch.
2709 static inline void sw_get_addr(struct ksz_hw *hw, u8 *mac_addr)
2711 int i;
2713 for (i = 0; i < 6; i += 2) {
2714 mac_addr[i] = readb(hw->io + KS8842_MAC_ADDR_0_OFFSET + i);
2715 mac_addr[1 + i] = readb(hw->io + KS8842_MAC_ADDR_1_OFFSET + i);
2720 * sw_set_addr - configure switch MAC address
2721 * @hw: The hardware instance.
2722 * @mac_addr: The MAC address.
2724 * This function configures the MAC address of the switch.
2726 static void sw_set_addr(struct ksz_hw *hw, u8 *mac_addr)
2728 int i;
2730 for (i = 0; i < 6; i += 2) {
2731 writeb(mac_addr[i], hw->io + KS8842_MAC_ADDR_0_OFFSET + i);
2732 writeb(mac_addr[1 + i], hw->io + KS8842_MAC_ADDR_1_OFFSET + i);
2737 * sw_set_global_ctrl - set switch global control
2738 * @hw: The hardware instance.
2740 * This routine sets the global control of the switch function.
2742 static void sw_set_global_ctrl(struct ksz_hw *hw)
2744 u16 data;
2746 /* Enable switch MII flow control. */
2747 data = readw(hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2748 data |= SWITCH_FLOW_CTRL;
2749 writew(data, hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
2751 data = readw(hw->io + KS8842_SWITCH_CTRL_1_OFFSET);
2753 /* Enable aggressive back off algorithm in half duplex mode. */
2754 data |= SWITCH_AGGR_BACKOFF;
2756 /* Enable automatic fast aging when link changed detected. */
2757 data |= SWITCH_AGING_ENABLE;
2758 data |= SWITCH_LINK_AUTO_AGING;
2760 if (hw->overrides & FAST_AGING)
2761 data |= SWITCH_FAST_AGING;
2762 else
2763 data &= ~SWITCH_FAST_AGING;
2764 writew(data, hw->io + KS8842_SWITCH_CTRL_1_OFFSET);
2766 data = readw(hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
2768 /* Enable no excessive collision drop. */
2769 data |= NO_EXC_COLLISION_DROP;
2770 writew(data, hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
2773 enum {
2774 STP_STATE_DISABLED = 0,
2775 STP_STATE_LISTENING,
2776 STP_STATE_LEARNING,
2777 STP_STATE_FORWARDING,
2778 STP_STATE_BLOCKED,
2779 STP_STATE_SIMPLE
2783 * port_set_stp_state - configure port spanning tree state
2784 * @hw: The hardware instance.
2785 * @port: The port index.
2786 * @state: The spanning tree state.
2788 * This routine configures the spanning tree state of the port.
2790 static void port_set_stp_state(struct ksz_hw *hw, int port, int state)
2792 u16 data;
2794 port_r16(hw, port, KS8842_PORT_CTRL_2_OFFSET, &data);
2795 switch (state) {
2796 case STP_STATE_DISABLED:
2797 data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE);
2798 data |= PORT_LEARN_DISABLE;
2799 break;
2800 case STP_STATE_LISTENING:
2802 * No need to turn on transmit because of port direct mode.
2803 * Turning on receive is required if static MAC table is not setup.
2805 data &= ~PORT_TX_ENABLE;
2806 data |= PORT_RX_ENABLE;
2807 data |= PORT_LEARN_DISABLE;
2808 break;
2809 case STP_STATE_LEARNING:
2810 data &= ~PORT_TX_ENABLE;
2811 data |= PORT_RX_ENABLE;
2812 data &= ~PORT_LEARN_DISABLE;
2813 break;
2814 case STP_STATE_FORWARDING:
2815 data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
2816 data &= ~PORT_LEARN_DISABLE;
2817 break;
2818 case STP_STATE_BLOCKED:
2820 * Need to setup static MAC table with override to keep receiving BPDU
2821 * messages. See sw_init_stp routine.
2823 data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE);
2824 data |= PORT_LEARN_DISABLE;
2825 break;
2826 case STP_STATE_SIMPLE:
2827 data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
2828 data |= PORT_LEARN_DISABLE;
2829 break;
2831 port_w16(hw, port, KS8842_PORT_CTRL_2_OFFSET, data);
2832 hw->ksz_switch->port_cfg[port].stp_state = state;
2835 #define STP_ENTRY 0
2836 #define BROADCAST_ENTRY 1
2837 #define BRIDGE_ADDR_ENTRY 2
2838 #define IPV6_ADDR_ENTRY 3
2841 * sw_clr_sta_mac_table - clear static MAC table
2842 * @hw: The hardware instance.
2844 * This routine clears the static MAC table.
2846 static void sw_clr_sta_mac_table(struct ksz_hw *hw)
2848 struct ksz_mac_table *entry;
2849 int i;
2851 for (i = 0; i < STATIC_MAC_TABLE_ENTRIES; i++) {
2852 entry = &hw->ksz_switch->mac_table[i];
2853 sw_w_sta_mac_table(hw, i,
2854 entry->mac_addr, entry->ports,
2855 entry->override, 0,
2856 entry->use_fid, entry->fid);
2861 * sw_init_stp - initialize switch spanning tree support
2862 * @hw: The hardware instance.
2864 * This routine initializes the spanning tree support of the switch.
2866 static void sw_init_stp(struct ksz_hw *hw)
2868 struct ksz_mac_table *entry;
2870 entry = &hw->ksz_switch->mac_table[STP_ENTRY];
2871 entry->mac_addr[0] = 0x01;
2872 entry->mac_addr[1] = 0x80;
2873 entry->mac_addr[2] = 0xC2;
2874 entry->mac_addr[3] = 0x00;
2875 entry->mac_addr[4] = 0x00;
2876 entry->mac_addr[5] = 0x00;
2877 entry->ports = HOST_MASK;
2878 entry->override = 1;
2879 entry->valid = 1;
2880 sw_w_sta_mac_table(hw, STP_ENTRY,
2881 entry->mac_addr, entry->ports,
2882 entry->override, entry->valid,
2883 entry->use_fid, entry->fid);
2887 * sw_block_addr - block certain packets from the host port
2888 * @hw: The hardware instance.
2890 * This routine blocks certain packets from reaching to the host port.
2892 static void sw_block_addr(struct ksz_hw *hw)
2894 struct ksz_mac_table *entry;
2895 int i;
2897 for (i = BROADCAST_ENTRY; i <= IPV6_ADDR_ENTRY; i++) {
2898 entry = &hw->ksz_switch->mac_table[i];
2899 entry->valid = 0;
2900 sw_w_sta_mac_table(hw, i,
2901 entry->mac_addr, entry->ports,
2902 entry->override, entry->valid,
2903 entry->use_fid, entry->fid);
2907 #define PHY_LINK_SUPPORT \
2908 (PHY_AUTO_NEG_ASYM_PAUSE | \
2909 PHY_AUTO_NEG_SYM_PAUSE | \
2910 PHY_AUTO_NEG_100BT4 | \
2911 PHY_AUTO_NEG_100BTX_FD | \
2912 PHY_AUTO_NEG_100BTX | \
2913 PHY_AUTO_NEG_10BT_FD | \
2914 PHY_AUTO_NEG_10BT)
2916 static inline void hw_r_phy_ctrl(struct ksz_hw *hw, int phy, u16 *data)
2918 *data = readw(hw->io + phy + KS884X_PHY_CTRL_OFFSET);
2921 static inline void hw_w_phy_ctrl(struct ksz_hw *hw, int phy, u16 data)
2923 writew(data, hw->io + phy + KS884X_PHY_CTRL_OFFSET);
2926 static inline void hw_r_phy_link_stat(struct ksz_hw *hw, int phy, u16 *data)
2928 *data = readw(hw->io + phy + KS884X_PHY_STATUS_OFFSET);
2931 static inline void hw_r_phy_auto_neg(struct ksz_hw *hw, int phy, u16 *data)
2933 *data = readw(hw->io + phy + KS884X_PHY_AUTO_NEG_OFFSET);
2936 static inline void hw_w_phy_auto_neg(struct ksz_hw *hw, int phy, u16 data)
2938 writew(data, hw->io + phy + KS884X_PHY_AUTO_NEG_OFFSET);
2941 static inline void hw_r_phy_rem_cap(struct ksz_hw *hw, int phy, u16 *data)
2943 *data = readw(hw->io + phy + KS884X_PHY_REMOTE_CAP_OFFSET);
2946 static inline void hw_r_phy_crossover(struct ksz_hw *hw, int phy, u16 *data)
2948 *data = readw(hw->io + phy + KS884X_PHY_CTRL_OFFSET);
2951 static inline void hw_w_phy_crossover(struct ksz_hw *hw, int phy, u16 data)
2953 writew(data, hw->io + phy + KS884X_PHY_CTRL_OFFSET);
2956 static inline void hw_r_phy_polarity(struct ksz_hw *hw, int phy, u16 *data)
2958 *data = readw(hw->io + phy + KS884X_PHY_PHY_CTRL_OFFSET);
2961 static inline void hw_w_phy_polarity(struct ksz_hw *hw, int phy, u16 data)
2963 writew(data, hw->io + phy + KS884X_PHY_PHY_CTRL_OFFSET);
2966 static inline void hw_r_phy_link_md(struct ksz_hw *hw, int phy, u16 *data)
2968 *data = readw(hw->io + phy + KS884X_PHY_LINK_MD_OFFSET);
2971 static inline void hw_w_phy_link_md(struct ksz_hw *hw, int phy, u16 data)
2973 writew(data, hw->io + phy + KS884X_PHY_LINK_MD_OFFSET);
2977 * hw_r_phy - read data from PHY register
2978 * @hw: The hardware instance.
2979 * @port: Port to read.
2980 * @reg: PHY register to read.
2981 * @val: Buffer to store the read data.
2983 * This routine reads data from the PHY register.
2985 static void hw_r_phy(struct ksz_hw *hw, int port, u16 reg, u16 *val)
2987 int phy;
2989 phy = KS884X_PHY_1_CTRL_OFFSET + port * PHY_CTRL_INTERVAL + reg;
2990 *val = readw(hw->io + phy);
2994 * port_w_phy - write data to PHY register
2995 * @hw: The hardware instance.
2996 * @port: Port to write.
2997 * @reg: PHY register to write.
2998 * @val: Word data to write.
3000 * This routine writes data to the PHY register.
3002 static void hw_w_phy(struct ksz_hw *hw, int port, u16 reg, u16 val)
3004 int phy;
3006 phy = KS884X_PHY_1_CTRL_OFFSET + port * PHY_CTRL_INTERVAL + reg;
3007 writew(val, hw->io + phy);
3011 * EEPROM access functions
3014 #define AT93C_CODE 0
3015 #define AT93C_WR_OFF 0x00
3016 #define AT93C_WR_ALL 0x10
3017 #define AT93C_ER_ALL 0x20
3018 #define AT93C_WR_ON 0x30
3020 #define AT93C_WRITE 1
3021 #define AT93C_READ 2
3022 #define AT93C_ERASE 3
3024 #define EEPROM_DELAY 4
3026 static inline void drop_gpio(struct ksz_hw *hw, u8 gpio)
3028 u16 data;
3030 data = readw(hw->io + KS884X_EEPROM_CTRL_OFFSET);
3031 data &= ~gpio;
3032 writew(data, hw->io + KS884X_EEPROM_CTRL_OFFSET);
3035 static inline void raise_gpio(struct ksz_hw *hw, u8 gpio)
3037 u16 data;
3039 data = readw(hw->io + KS884X_EEPROM_CTRL_OFFSET);
3040 data |= gpio;
3041 writew(data, hw->io + KS884X_EEPROM_CTRL_OFFSET);
3044 static inline u8 state_gpio(struct ksz_hw *hw, u8 gpio)
3046 u16 data;
3048 data = readw(hw->io + KS884X_EEPROM_CTRL_OFFSET);
3049 return (u8)(data & gpio);
3052 static void eeprom_clk(struct ksz_hw *hw)
3054 raise_gpio(hw, EEPROM_SERIAL_CLOCK);
3055 udelay(EEPROM_DELAY);
3056 drop_gpio(hw, EEPROM_SERIAL_CLOCK);
3057 udelay(EEPROM_DELAY);
3060 static u16 spi_r(struct ksz_hw *hw)
3062 int i;
3063 u16 temp = 0;
3065 for (i = 15; i >= 0; i--) {
3066 raise_gpio(hw, EEPROM_SERIAL_CLOCK);
3067 udelay(EEPROM_DELAY);
3069 temp |= (state_gpio(hw, EEPROM_DATA_IN)) ? 1 << i : 0;
3071 drop_gpio(hw, EEPROM_SERIAL_CLOCK);
3072 udelay(EEPROM_DELAY);
3074 return temp;
3077 static void spi_w(struct ksz_hw *hw, u16 data)
3079 int i;
3081 for (i = 15; i >= 0; i--) {
3082 (data & (0x01 << i)) ? raise_gpio(hw, EEPROM_DATA_OUT) :
3083 drop_gpio(hw, EEPROM_DATA_OUT);
3084 eeprom_clk(hw);
3088 static void spi_reg(struct ksz_hw *hw, u8 data, u8 reg)
3090 int i;
3092 /* Initial start bit */
3093 raise_gpio(hw, EEPROM_DATA_OUT);
3094 eeprom_clk(hw);
3096 /* AT93C operation */
3097 for (i = 1; i >= 0; i--) {
3098 (data & (0x01 << i)) ? raise_gpio(hw, EEPROM_DATA_OUT) :
3099 drop_gpio(hw, EEPROM_DATA_OUT);
3100 eeprom_clk(hw);
3103 /* Address location */
3104 for (i = 5; i >= 0; i--) {
3105 (reg & (0x01 << i)) ? raise_gpio(hw, EEPROM_DATA_OUT) :
3106 drop_gpio(hw, EEPROM_DATA_OUT);
3107 eeprom_clk(hw);
3111 #define EEPROM_DATA_RESERVED 0
3112 #define EEPROM_DATA_MAC_ADDR_0 1
3113 #define EEPROM_DATA_MAC_ADDR_1 2
3114 #define EEPROM_DATA_MAC_ADDR_2 3
3115 #define EEPROM_DATA_SUBSYS_ID 4
3116 #define EEPROM_DATA_SUBSYS_VEN_ID 5
3117 #define EEPROM_DATA_PM_CAP 6
3119 /* User defined EEPROM data */
3120 #define EEPROM_DATA_OTHER_MAC_ADDR 9
3123 * eeprom_read - read from AT93C46 EEPROM
3124 * @hw: The hardware instance.
3125 * @reg: The register offset.
3127 * This function reads a word from the AT93C46 EEPROM.
3129 * Return the data value.
3131 static u16 eeprom_read(struct ksz_hw *hw, u8 reg)
3133 u16 data;
3135 raise_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
3137 spi_reg(hw, AT93C_READ, reg);
3138 data = spi_r(hw);
3140 drop_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
3142 return data;
3146 * eeprom_write - write to AT93C46 EEPROM
3147 * @hw: The hardware instance.
3148 * @reg: The register offset.
3149 * @data: The data value.
3151 * This procedure writes a word to the AT93C46 EEPROM.
3153 static void eeprom_write(struct ksz_hw *hw, u8 reg, u16 data)
3155 int timeout;
3157 raise_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
3159 /* Enable write. */
3160 spi_reg(hw, AT93C_CODE, AT93C_WR_ON);
3161 drop_gpio(hw, EEPROM_CHIP_SELECT);
3162 udelay(1);
3164 /* Erase the register. */
3165 raise_gpio(hw, EEPROM_CHIP_SELECT);
3166 spi_reg(hw, AT93C_ERASE, reg);
3167 drop_gpio(hw, EEPROM_CHIP_SELECT);
3168 udelay(1);
3170 /* Check operation complete. */
3171 raise_gpio(hw, EEPROM_CHIP_SELECT);
3172 timeout = 8;
3173 mdelay(2);
3174 do {
3175 mdelay(1);
3176 } while (!state_gpio(hw, EEPROM_DATA_IN) && --timeout);
3177 drop_gpio(hw, EEPROM_CHIP_SELECT);
3178 udelay(1);
3180 /* Write the register. */
3181 raise_gpio(hw, EEPROM_CHIP_SELECT);
3182 spi_reg(hw, AT93C_WRITE, reg);
3183 spi_w(hw, data);
3184 drop_gpio(hw, EEPROM_CHIP_SELECT);
3185 udelay(1);
3187 /* Check operation complete. */
3188 raise_gpio(hw, EEPROM_CHIP_SELECT);
3189 timeout = 8;
3190 mdelay(2);
3191 do {
3192 mdelay(1);
3193 } while (!state_gpio(hw, EEPROM_DATA_IN) && --timeout);
3194 drop_gpio(hw, EEPROM_CHIP_SELECT);
3195 udelay(1);
3197 /* Disable write. */
3198 raise_gpio(hw, EEPROM_CHIP_SELECT);
3199 spi_reg(hw, AT93C_CODE, AT93C_WR_OFF);
3201 drop_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
3205 * Link detection routines
3208 static u16 advertised_flow_ctrl(struct ksz_port *port, u16 ctrl)
3210 ctrl &= ~PORT_AUTO_NEG_SYM_PAUSE;
3211 switch (port->flow_ctrl) {
3212 case PHY_FLOW_CTRL:
3213 ctrl |= PORT_AUTO_NEG_SYM_PAUSE;
3214 break;
3215 /* Not supported. */
3216 case PHY_TX_ONLY:
3217 case PHY_RX_ONLY:
3218 default:
3219 break;
3221 return ctrl;
3224 static void set_flow_ctrl(struct ksz_hw *hw, int rx, int tx)
3226 u32 rx_cfg;
3227 u32 tx_cfg;
3229 rx_cfg = hw->rx_cfg;
3230 tx_cfg = hw->tx_cfg;
3231 if (rx)
3232 hw->rx_cfg |= DMA_RX_FLOW_ENABLE;
3233 else
3234 hw->rx_cfg &= ~DMA_RX_FLOW_ENABLE;
3235 if (tx)
3236 hw->tx_cfg |= DMA_TX_FLOW_ENABLE;
3237 else
3238 hw->tx_cfg &= ~DMA_TX_FLOW_ENABLE;
3239 if (hw->enabled) {
3240 if (rx_cfg != hw->rx_cfg)
3241 writel(hw->rx_cfg, hw->io + KS_DMA_RX_CTRL);
3242 if (tx_cfg != hw->tx_cfg)
3243 writel(hw->tx_cfg, hw->io + KS_DMA_TX_CTRL);
3247 static void determine_flow_ctrl(struct ksz_hw *hw, struct ksz_port *port,
3248 u16 local, u16 remote)
3250 int rx;
3251 int tx;
3253 if (hw->overrides & PAUSE_FLOW_CTRL)
3254 return;
3256 rx = tx = 0;
3257 if (port->force_link)
3258 rx = tx = 1;
3259 if (remote & PHY_AUTO_NEG_SYM_PAUSE) {
3260 if (local & PHY_AUTO_NEG_SYM_PAUSE) {
3261 rx = tx = 1;
3262 } else if ((remote & PHY_AUTO_NEG_ASYM_PAUSE) &&
3263 (local & PHY_AUTO_NEG_PAUSE) ==
3264 PHY_AUTO_NEG_ASYM_PAUSE) {
3265 tx = 1;
3267 } else if (remote & PHY_AUTO_NEG_ASYM_PAUSE) {
3268 if ((local & PHY_AUTO_NEG_PAUSE) == PHY_AUTO_NEG_PAUSE)
3269 rx = 1;
3271 if (!hw->ksz_switch)
3272 set_flow_ctrl(hw, rx, tx);
3275 static inline void port_cfg_change(struct ksz_hw *hw, struct ksz_port *port,
3276 struct ksz_port_info *info, u16 link_status)
3278 if ((hw->features & HALF_DUPLEX_SIGNAL_BUG) &&
3279 !(hw->overrides & PAUSE_FLOW_CTRL)) {
3280 u32 cfg = hw->tx_cfg;
3282 /* Disable flow control in the half duplex mode. */
3283 if (1 == info->duplex)
3284 hw->tx_cfg &= ~DMA_TX_FLOW_ENABLE;
3285 if (hw->enabled && cfg != hw->tx_cfg)
3286 writel(hw->tx_cfg, hw->io + KS_DMA_TX_CTRL);
3291 * port_get_link_speed - get current link status
3292 * @port: The port instance.
3294 * This routine reads PHY registers to determine the current link status of the
3295 * switch ports.
3297 static void port_get_link_speed(struct ksz_port *port)
3299 uint interrupt;
3300 struct ksz_port_info *info;
3301 struct ksz_port_info *linked = NULL;
3302 struct ksz_hw *hw = port->hw;
3303 u16 data;
3304 u16 status;
3305 u8 local;
3306 u8 remote;
3307 int i;
3308 int p;
3309 int change = 0;
3311 interrupt = hw_block_intr(hw);
3313 for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
3314 info = &hw->port_info[p];
3315 port_r16(hw, p, KS884X_PORT_CTRL_4_OFFSET, &data);
3316 port_r16(hw, p, KS884X_PORT_STATUS_OFFSET, &status);
3319 * Link status is changing all the time even when there is no
3320 * cable connection!
3322 remote = status & (PORT_AUTO_NEG_COMPLETE |
3323 PORT_STATUS_LINK_GOOD);
3324 local = (u8) data;
3326 /* No change to status. */
3327 if (local == info->advertised && remote == info->partner)
3328 continue;
3330 info->advertised = local;
3331 info->partner = remote;
3332 if (status & PORT_STATUS_LINK_GOOD) {
3334 /* Remember the first linked port. */
3335 if (!linked)
3336 linked = info;
3338 info->tx_rate = 10 * TX_RATE_UNIT;
3339 if (status & PORT_STATUS_SPEED_100MBIT)
3340 info->tx_rate = 100 * TX_RATE_UNIT;
3342 info->duplex = 1;
3343 if (status & PORT_STATUS_FULL_DUPLEX)
3344 info->duplex = 2;
3346 if (media_connected != info->state) {
3347 hw_r_phy(hw, p, KS884X_PHY_AUTO_NEG_OFFSET,
3348 &data);
3349 hw_r_phy(hw, p, KS884X_PHY_REMOTE_CAP_OFFSET,
3350 &status);
3351 determine_flow_ctrl(hw, port, data, status);
3352 if (hw->ksz_switch) {
3353 port_cfg_back_pressure(hw, p,
3354 (1 == info->duplex));
3356 change |= 1 << i;
3357 port_cfg_change(hw, port, info, status);
3359 info->state = media_connected;
3360 } else {
3361 if (media_disconnected != info->state) {
3362 change |= 1 << i;
3364 /* Indicate the link just goes down. */
3365 hw->port_mib[p].link_down = 1;
3367 info->state = media_disconnected;
3369 hw->port_mib[p].state = (u8) info->state;
3372 if (linked && media_disconnected == port->linked->state)
3373 port->linked = linked;
3375 hw_restore_intr(hw, interrupt);
3378 #define PHY_RESET_TIMEOUT 10
3381 * port_set_link_speed - set port speed
3382 * @port: The port instance.
3384 * This routine sets the link speed of the switch ports.
3386 static void port_set_link_speed(struct ksz_port *port)
3388 struct ksz_port_info *info;
3389 struct ksz_hw *hw = port->hw;
3390 u16 data;
3391 u16 cfg;
3392 u8 status;
3393 int i;
3394 int p;
3396 for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
3397 info = &hw->port_info[p];
3399 port_r16(hw, p, KS884X_PORT_CTRL_4_OFFSET, &data);
3400 port_r8(hw, p, KS884X_PORT_STATUS_OFFSET, &status);
3402 cfg = 0;
3403 if (status & PORT_STATUS_LINK_GOOD)
3404 cfg = data;
3406 data |= PORT_AUTO_NEG_ENABLE;
3407 data = advertised_flow_ctrl(port, data);
3409 data |= PORT_AUTO_NEG_100BTX_FD | PORT_AUTO_NEG_100BTX |
3410 PORT_AUTO_NEG_10BT_FD | PORT_AUTO_NEG_10BT;
3412 /* Check if manual configuration is specified by the user. */
3413 if (port->speed || port->duplex) {
3414 if (10 == port->speed)
3415 data &= ~(PORT_AUTO_NEG_100BTX_FD |
3416 PORT_AUTO_NEG_100BTX);
3417 else if (100 == port->speed)
3418 data &= ~(PORT_AUTO_NEG_10BT_FD |
3419 PORT_AUTO_NEG_10BT);
3420 if (1 == port->duplex)
3421 data &= ~(PORT_AUTO_NEG_100BTX_FD |
3422 PORT_AUTO_NEG_10BT_FD);
3423 else if (2 == port->duplex)
3424 data &= ~(PORT_AUTO_NEG_100BTX |
3425 PORT_AUTO_NEG_10BT);
3427 if (data != cfg) {
3428 data |= PORT_AUTO_NEG_RESTART;
3429 port_w16(hw, p, KS884X_PORT_CTRL_4_OFFSET, data);
3435 * port_force_link_speed - force port speed
3436 * @port: The port instance.
3438 * This routine forces the link speed of the switch ports.
3440 static void port_force_link_speed(struct ksz_port *port)
3442 struct ksz_hw *hw = port->hw;
3443 u16 data;
3444 int i;
3445 int phy;
3446 int p;
3448 for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
3449 phy = KS884X_PHY_1_CTRL_OFFSET + p * PHY_CTRL_INTERVAL;
3450 hw_r_phy_ctrl(hw, phy, &data);
3452 data &= ~PHY_AUTO_NEG_ENABLE;
3454 if (10 == port->speed)
3455 data &= ~PHY_SPEED_100MBIT;
3456 else if (100 == port->speed)
3457 data |= PHY_SPEED_100MBIT;
3458 if (1 == port->duplex)
3459 data &= ~PHY_FULL_DUPLEX;
3460 else if (2 == port->duplex)
3461 data |= PHY_FULL_DUPLEX;
3462 hw_w_phy_ctrl(hw, phy, data);
3466 static void port_set_power_saving(struct ksz_port *port, int enable)
3468 struct ksz_hw *hw = port->hw;
3469 int i;
3470 int p;
3472 for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++)
3473 port_cfg(hw, p,
3474 KS884X_PORT_CTRL_4_OFFSET, PORT_POWER_DOWN, enable);
3478 * KSZ8841 power management functions
3482 * hw_chk_wol_pme_status - check PMEN pin
3483 * @hw: The hardware instance.
3485 * This function is used to check PMEN pin is asserted.
3487 * Return 1 if PMEN pin is asserted; otherwise, 0.
3489 static int hw_chk_wol_pme_status(struct ksz_hw *hw)
3491 struct dev_info *hw_priv = container_of(hw, struct dev_info, hw);
3492 struct pci_dev *pdev = hw_priv->pdev;
3493 u16 data;
3495 if (!pdev->pm_cap)
3496 return 0;
3497 pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &data);
3498 return (data & PCI_PM_CTRL_PME_STATUS) == PCI_PM_CTRL_PME_STATUS;
3502 * hw_clr_wol_pme_status - clear PMEN pin
3503 * @hw: The hardware instance.
3505 * This routine is used to clear PME_Status to deassert PMEN pin.
3507 static void hw_clr_wol_pme_status(struct ksz_hw *hw)
3509 struct dev_info *hw_priv = container_of(hw, struct dev_info, hw);
3510 struct pci_dev *pdev = hw_priv->pdev;
3511 u16 data;
3513 if (!pdev->pm_cap)
3514 return;
3516 /* Clear PME_Status to deassert PMEN pin. */
3517 pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &data);
3518 data |= PCI_PM_CTRL_PME_STATUS;
3519 pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, data);
3523 * hw_cfg_wol_pme - enable or disable Wake-on-LAN
3524 * @hw: The hardware instance.
3525 * @set: The flag indicating whether to enable or disable.
3527 * This routine is used to enable or disable Wake-on-LAN.
3529 static void hw_cfg_wol_pme(struct ksz_hw *hw, int set)
3531 struct dev_info *hw_priv = container_of(hw, struct dev_info, hw);
3532 struct pci_dev *pdev = hw_priv->pdev;
3533 u16 data;
3535 if (!pdev->pm_cap)
3536 return;
3537 pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &data);
3538 data &= ~PCI_PM_CTRL_STATE_MASK;
3539 if (set)
3540 data |= PCI_PM_CTRL_PME_ENABLE | PCI_D3hot;
3541 else
3542 data &= ~PCI_PM_CTRL_PME_ENABLE;
3543 pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, data);
3547 * hw_cfg_wol - configure Wake-on-LAN features
3548 * @hw: The hardware instance.
3549 * @frame: The pattern frame bit.
3550 * @set: The flag indicating whether to enable or disable.
3552 * This routine is used to enable or disable certain Wake-on-LAN features.
3554 static void hw_cfg_wol(struct ksz_hw *hw, u16 frame, int set)
3556 u16 data;
3558 data = readw(hw->io + KS8841_WOL_CTRL_OFFSET);
3559 if (set)
3560 data |= frame;
3561 else
3562 data &= ~frame;
3563 writew(data, hw->io + KS8841_WOL_CTRL_OFFSET);
3567 * hw_set_wol_frame - program Wake-on-LAN pattern
3568 * @hw: The hardware instance.
3569 * @i: The frame index.
3570 * @mask_size: The size of the mask.
3571 * @mask: Mask to ignore certain bytes in the pattern.
3572 * @frame_size: The size of the frame.
3573 * @pattern: The frame data.
3575 * This routine is used to program Wake-on-LAN pattern.
3577 static void hw_set_wol_frame(struct ksz_hw *hw, int i, uint mask_size,
3578 u8 *mask, uint frame_size, u8 *pattern)
3580 int bits;
3581 int from;
3582 int len;
3583 int to;
3584 u32 crc;
3585 u8 data[64];
3586 u8 val = 0;
3588 if (frame_size > mask_size * 8)
3589 frame_size = mask_size * 8;
3590 if (frame_size > 64)
3591 frame_size = 64;
3593 i *= 0x10;
3594 writel(0, hw->io + KS8841_WOL_FRAME_BYTE0_OFFSET + i);
3595 writel(0, hw->io + KS8841_WOL_FRAME_BYTE2_OFFSET + i);
3597 bits = len = from = to = 0;
3598 do {
3599 if (bits) {
3600 if ((val & 1))
3601 data[to++] = pattern[from];
3602 val >>= 1;
3603 ++from;
3604 --bits;
3605 } else {
3606 val = mask[len];
3607 writeb(val, hw->io + KS8841_WOL_FRAME_BYTE0_OFFSET + i
3608 + len);
3609 ++len;
3610 if (val)
3611 bits = 8;
3612 else
3613 from += 8;
3615 } while (from < (int) frame_size);
3616 if (val) {
3617 bits = mask[len - 1];
3618 val <<= (from % 8);
3619 bits &= ~val;
3620 writeb(bits, hw->io + KS8841_WOL_FRAME_BYTE0_OFFSET + i + len -
3623 crc = ether_crc(to, data);
3624 writel(crc, hw->io + KS8841_WOL_FRAME_CRC_OFFSET + i);
3628 * hw_add_wol_arp - add ARP pattern
3629 * @hw: The hardware instance.
3630 * @ip_addr: The IPv4 address assigned to the device.
3632 * This routine is used to add ARP pattern for waking up the host.
3634 static void hw_add_wol_arp(struct ksz_hw *hw, u8 *ip_addr)
3636 u8 mask[6] = { 0x3F, 0xF0, 0x3F, 0x00, 0xC0, 0x03 };
3637 u8 pattern[42] = {
3638 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3639 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3640 0x08, 0x06,
3641 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01,
3642 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3643 0x00, 0x00, 0x00, 0x00,
3644 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3645 0x00, 0x00, 0x00, 0x00 };
3647 memcpy(&pattern[38], ip_addr, 4);
3648 hw_set_wol_frame(hw, 3, 6, mask, 42, pattern);
3652 * hw_add_wol_bcast - add broadcast pattern
3653 * @hw: The hardware instance.
3655 * This routine is used to add broadcast pattern for waking up the host.
3657 static void hw_add_wol_bcast(struct ksz_hw *hw)
3659 u8 mask[] = { 0x3F };
3660 u8 pattern[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3662 hw_set_wol_frame(hw, 2, 1, mask, MAC_ADDR_LEN, pattern);
3666 * hw_add_wol_mcast - add multicast pattern
3667 * @hw: The hardware instance.
3669 * This routine is used to add multicast pattern for waking up the host.
3671 * It is assumed the multicast packet is the ICMPv6 neighbor solicitation used
3672 * by IPv6 ping command. Note that multicast packets are filtred through the
3673 * multicast hash table, so not all multicast packets can wake up the host.
3675 static void hw_add_wol_mcast(struct ksz_hw *hw)
3677 u8 mask[] = { 0x3F };
3678 u8 pattern[] = { 0x33, 0x33, 0xFF, 0x00, 0x00, 0x00 };
3680 memcpy(&pattern[3], &hw->override_addr[3], 3);
3681 hw_set_wol_frame(hw, 1, 1, mask, 6, pattern);
3685 * hw_add_wol_ucast - add unicast pattern
3686 * @hw: The hardware instance.
3688 * This routine is used to add unicast pattern to wakeup the host.
3690 * It is assumed the unicast packet is directed to the device, as the hardware
3691 * can only receive them in normal case.
3693 static void hw_add_wol_ucast(struct ksz_hw *hw)
3695 u8 mask[] = { 0x3F };
3697 hw_set_wol_frame(hw, 0, 1, mask, MAC_ADDR_LEN, hw->override_addr);
3701 * hw_enable_wol - enable Wake-on-LAN
3702 * @hw: The hardware instance.
3703 * @wol_enable: The Wake-on-LAN settings.
3704 * @net_addr: The IPv4 address assigned to the device.
3706 * This routine is used to enable Wake-on-LAN depending on driver settings.
3708 static void hw_enable_wol(struct ksz_hw *hw, u32 wol_enable, u8 *net_addr)
3710 hw_cfg_wol(hw, KS8841_WOL_MAGIC_ENABLE, (wol_enable & WAKE_MAGIC));
3711 hw_cfg_wol(hw, KS8841_WOL_FRAME0_ENABLE, (wol_enable & WAKE_UCAST));
3712 hw_add_wol_ucast(hw);
3713 hw_cfg_wol(hw, KS8841_WOL_FRAME1_ENABLE, (wol_enable & WAKE_MCAST));
3714 hw_add_wol_mcast(hw);
3715 hw_cfg_wol(hw, KS8841_WOL_FRAME2_ENABLE, (wol_enable & WAKE_BCAST));
3716 hw_cfg_wol(hw, KS8841_WOL_FRAME3_ENABLE, (wol_enable & WAKE_ARP));
3717 hw_add_wol_arp(hw, net_addr);
3721 * hw_init - check driver is correct for the hardware
3722 * @hw: The hardware instance.
3724 * This function checks the hardware is correct for this driver and sets the
3725 * hardware up for proper initialization.
3727 * Return number of ports or 0 if not right.
3729 static int hw_init(struct ksz_hw *hw)
3731 int rc = 0;
3732 u16 data;
3733 u16 revision;
3735 /* Set bus speed to 125MHz. */
3736 writew(BUS_SPEED_125_MHZ, hw->io + KS884X_BUS_CTRL_OFFSET);
3738 /* Check KSZ884x chip ID. */
3739 data = readw(hw->io + KS884X_CHIP_ID_OFFSET);
3741 revision = (data & KS884X_REVISION_MASK) >> KS884X_REVISION_SHIFT;
3742 data &= KS884X_CHIP_ID_MASK_41;
3743 if (REG_CHIP_ID_41 == data)
3744 rc = 1;
3745 else if (REG_CHIP_ID_42 == data)
3746 rc = 2;
3747 else
3748 return 0;
3750 /* Setup hardware features or bug workarounds. */
3751 if (revision <= 1) {
3752 hw->features |= SMALL_PACKET_TX_BUG;
3753 if (1 == rc)
3754 hw->features |= HALF_DUPLEX_SIGNAL_BUG;
3756 hw->features |= IPV6_CSUM_GEN_HACK;
3757 return rc;
3761 * hw_reset - reset the hardware
3762 * @hw: The hardware instance.
3764 * This routine resets the hardware.
3766 static void hw_reset(struct ksz_hw *hw)
3768 writew(GLOBAL_SOFTWARE_RESET, hw->io + KS884X_GLOBAL_CTRL_OFFSET);
3770 /* Wait for device to reset. */
3771 mdelay(10);
3773 /* Write 0 to clear device reset. */
3774 writew(0, hw->io + KS884X_GLOBAL_CTRL_OFFSET);
3778 * hw_setup - setup the hardware
3779 * @hw: The hardware instance.
3781 * This routine setup the hardware for proper operation.
3783 static void hw_setup(struct ksz_hw *hw)
3785 #if SET_DEFAULT_LED
3786 u16 data;
3788 /* Change default LED mode. */
3789 data = readw(hw->io + KS8842_SWITCH_CTRL_5_OFFSET);
3790 data &= ~LED_MODE;
3791 data |= SET_DEFAULT_LED;
3792 writew(data, hw->io + KS8842_SWITCH_CTRL_5_OFFSET);
3793 #endif
3795 /* Setup transmit control. */
3796 hw->tx_cfg = (DMA_TX_PAD_ENABLE | DMA_TX_CRC_ENABLE |
3797 (DMA_BURST_DEFAULT << DMA_BURST_SHIFT) | DMA_TX_ENABLE);
3799 /* Setup receive control. */
3800 hw->rx_cfg = (DMA_RX_BROADCAST | DMA_RX_UNICAST |
3801 (DMA_BURST_DEFAULT << DMA_BURST_SHIFT) | DMA_RX_ENABLE);
3802 hw->rx_cfg |= KS884X_DMA_RX_MULTICAST;
3804 /* Hardware cannot handle UDP packet in IP fragments. */
3805 hw->rx_cfg |= (DMA_RX_CSUM_TCP | DMA_RX_CSUM_IP);
3807 if (hw->all_multi)
3808 hw->rx_cfg |= DMA_RX_ALL_MULTICAST;
3809 if (hw->promiscuous)
3810 hw->rx_cfg |= DMA_RX_PROMISCUOUS;
3814 * hw_setup_intr - setup interrupt mask
3815 * @hw: The hardware instance.
3817 * This routine setup the interrupt mask for proper operation.
3819 static void hw_setup_intr(struct ksz_hw *hw)
3821 hw->intr_mask = KS884X_INT_MASK | KS884X_INT_RX_OVERRUN;
3824 static void ksz_check_desc_num(struct ksz_desc_info *info)
3826 #define MIN_DESC_SHIFT 2
3828 int alloc = info->alloc;
3829 int shift;
3831 shift = 0;
3832 while (!(alloc & 1)) {
3833 shift++;
3834 alloc >>= 1;
3836 if (alloc != 1 || shift < MIN_DESC_SHIFT) {
3837 printk(KERN_ALERT "Hardware descriptor numbers not right!\n");
3838 while (alloc) {
3839 shift++;
3840 alloc >>= 1;
3842 if (shift < MIN_DESC_SHIFT)
3843 shift = MIN_DESC_SHIFT;
3844 alloc = 1 << shift;
3845 info->alloc = alloc;
3847 info->mask = info->alloc - 1;
3850 static void hw_init_desc(struct ksz_desc_info *desc_info, int transmit)
3852 int i;
3853 u32 phys = desc_info->ring_phys;
3854 struct ksz_hw_desc *desc = desc_info->ring_virt;
3855 struct ksz_desc *cur = desc_info->ring;
3856 struct ksz_desc *previous = NULL;
3858 for (i = 0; i < desc_info->alloc; i++) {
3859 cur->phw = desc++;
3860 phys += desc_info->size;
3861 previous = cur++;
3862 previous->phw->next = cpu_to_le32(phys);
3864 previous->phw->next = cpu_to_le32(desc_info->ring_phys);
3865 previous->sw.buf.rx.end_of_ring = 1;
3866 previous->phw->buf.data = cpu_to_le32(previous->sw.buf.data);
3868 desc_info->avail = desc_info->alloc;
3869 desc_info->last = desc_info->next = 0;
3871 desc_info->cur = desc_info->ring;
3875 * hw_set_desc_base - set descriptor base addresses
3876 * @hw: The hardware instance.
3877 * @tx_addr: The transmit descriptor base.
3878 * @rx_addr: The receive descriptor base.
3880 * This routine programs the descriptor base addresses after reset.
3882 static void hw_set_desc_base(struct ksz_hw *hw, u32 tx_addr, u32 rx_addr)
3884 /* Set base address of Tx/Rx descriptors. */
3885 writel(tx_addr, hw->io + KS_DMA_TX_ADDR);
3886 writel(rx_addr, hw->io + KS_DMA_RX_ADDR);
3889 static void hw_reset_pkts(struct ksz_desc_info *info)
3891 info->cur = info->ring;
3892 info->avail = info->alloc;
3893 info->last = info->next = 0;
3896 static inline void hw_resume_rx(struct ksz_hw *hw)
3898 writel(DMA_START, hw->io + KS_DMA_RX_START);
3902 * hw_start_rx - start receiving
3903 * @hw: The hardware instance.
3905 * This routine starts the receive function of the hardware.
3907 static void hw_start_rx(struct ksz_hw *hw)
3909 writel(hw->rx_cfg, hw->io + KS_DMA_RX_CTRL);
3911 /* Notify when the receive stops. */
3912 hw->intr_mask |= KS884X_INT_RX_STOPPED;
3914 writel(DMA_START, hw->io + KS_DMA_RX_START);
3915 hw_ack_intr(hw, KS884X_INT_RX_STOPPED);
3916 hw->rx_stop++;
3918 /* Variable overflows. */
3919 if (0 == hw->rx_stop)
3920 hw->rx_stop = 2;
3924 * hw_stop_rx - stop receiving
3925 * @hw: The hardware instance.
3927 * This routine stops the receive function of the hardware.
3929 static void hw_stop_rx(struct ksz_hw *hw)
3931 hw->rx_stop = 0;
3932 hw_turn_off_intr(hw, KS884X_INT_RX_STOPPED);
3933 writel((hw->rx_cfg & ~DMA_RX_ENABLE), hw->io + KS_DMA_RX_CTRL);
3937 * hw_start_tx - start transmitting
3938 * @hw: The hardware instance.
3940 * This routine starts the transmit function of the hardware.
3942 static void hw_start_tx(struct ksz_hw *hw)
3944 writel(hw->tx_cfg, hw->io + KS_DMA_TX_CTRL);
3948 * hw_stop_tx - stop transmitting
3949 * @hw: The hardware instance.
3951 * This routine stops the transmit function of the hardware.
3953 static void hw_stop_tx(struct ksz_hw *hw)
3955 writel((hw->tx_cfg & ~DMA_TX_ENABLE), hw->io + KS_DMA_TX_CTRL);
3959 * hw_disable - disable hardware
3960 * @hw: The hardware instance.
3962 * This routine disables the hardware.
3964 static void hw_disable(struct ksz_hw *hw)
3966 hw_stop_rx(hw);
3967 hw_stop_tx(hw);
3968 hw->enabled = 0;
3972 * hw_enable - enable hardware
3973 * @hw: The hardware instance.
3975 * This routine enables the hardware.
3977 static void hw_enable(struct ksz_hw *hw)
3979 hw_start_tx(hw);
3980 hw_start_rx(hw);
3981 hw->enabled = 1;
3985 * hw_alloc_pkt - allocate enough descriptors for transmission
3986 * @hw: The hardware instance.
3987 * @length: The length of the packet.
3988 * @physical: Number of descriptors required.
3990 * This function allocates descriptors for transmission.
3992 * Return 0 if not successful; 1 for buffer copy; or number of descriptors.
3994 static int hw_alloc_pkt(struct ksz_hw *hw, int length, int physical)
3996 /* Always leave one descriptor free. */
3997 if (hw->tx_desc_info.avail <= 1)
3998 return 0;
4000 /* Allocate a descriptor for transmission and mark it current. */
4001 get_tx_pkt(&hw->tx_desc_info, &hw->tx_desc_info.cur);
4002 hw->tx_desc_info.cur->sw.buf.tx.first_seg = 1;
4004 /* Keep track of number of transmit descriptors used so far. */
4005 ++hw->tx_int_cnt;
4006 hw->tx_size += length;
4008 /* Cannot hold on too much data. */
4009 if (hw->tx_size >= MAX_TX_HELD_SIZE)
4010 hw->tx_int_cnt = hw->tx_int_mask + 1;
4012 if (physical > hw->tx_desc_info.avail)
4013 return 1;
4015 return hw->tx_desc_info.avail;
4019 * hw_send_pkt - mark packet for transmission
4020 * @hw: The hardware instance.
4022 * This routine marks the packet for transmission in PCI version.
4024 static void hw_send_pkt(struct ksz_hw *hw)
4026 struct ksz_desc *cur = hw->tx_desc_info.cur;
4028 cur->sw.buf.tx.last_seg = 1;
4030 /* Interrupt only after specified number of descriptors used. */
4031 if (hw->tx_int_cnt > hw->tx_int_mask) {
4032 cur->sw.buf.tx.intr = 1;
4033 hw->tx_int_cnt = 0;
4034 hw->tx_size = 0;
4037 /* KSZ8842 supports port directed transmission. */
4038 cur->sw.buf.tx.dest_port = hw->dst_ports;
4040 release_desc(cur);
4042 writel(0, hw->io + KS_DMA_TX_START);
4045 static int empty_addr(u8 *addr)
4047 u32 *addr1 = (u32 *) addr;
4048 u16 *addr2 = (u16 *) &addr[4];
4050 return 0 == *addr1 && 0 == *addr2;
4054 * hw_set_addr - set MAC address
4055 * @hw: The hardware instance.
4057 * This routine programs the MAC address of the hardware when the address is
4058 * overrided.
4060 static void hw_set_addr(struct ksz_hw *hw)
4062 int i;
4064 for (i = 0; i < MAC_ADDR_LEN; i++)
4065 writeb(hw->override_addr[MAC_ADDR_ORDER(i)],
4066 hw->io + KS884X_ADDR_0_OFFSET + i);
4068 sw_set_addr(hw, hw->override_addr);
4072 * hw_read_addr - read MAC address
4073 * @hw: The hardware instance.
4075 * This routine retrieves the MAC address of the hardware.
4077 static void hw_read_addr(struct ksz_hw *hw)
4079 int i;
4081 for (i = 0; i < MAC_ADDR_LEN; i++)
4082 hw->perm_addr[MAC_ADDR_ORDER(i)] = readb(hw->io +
4083 KS884X_ADDR_0_OFFSET + i);
4085 if (!hw->mac_override) {
4086 memcpy(hw->override_addr, hw->perm_addr, MAC_ADDR_LEN);
4087 if (empty_addr(hw->override_addr)) {
4088 memcpy(hw->perm_addr, DEFAULT_MAC_ADDRESS,
4089 MAC_ADDR_LEN);
4090 memcpy(hw->override_addr, DEFAULT_MAC_ADDRESS,
4091 MAC_ADDR_LEN);
4092 hw->override_addr[5] += hw->id;
4093 hw_set_addr(hw);
4098 static void hw_ena_add_addr(struct ksz_hw *hw, int index, u8 *mac_addr)
4100 int i;
4101 u32 mac_addr_lo;
4102 u32 mac_addr_hi;
4104 mac_addr_hi = 0;
4105 for (i = 0; i < 2; i++) {
4106 mac_addr_hi <<= 8;
4107 mac_addr_hi |= mac_addr[i];
4109 mac_addr_hi |= ADD_ADDR_ENABLE;
4110 mac_addr_lo = 0;
4111 for (i = 2; i < 6; i++) {
4112 mac_addr_lo <<= 8;
4113 mac_addr_lo |= mac_addr[i];
4115 index *= ADD_ADDR_INCR;
4117 writel(mac_addr_lo, hw->io + index + KS_ADD_ADDR_0_LO);
4118 writel(mac_addr_hi, hw->io + index + KS_ADD_ADDR_0_HI);
4121 static void hw_set_add_addr(struct ksz_hw *hw)
4123 int i;
4125 for (i = 0; i < ADDITIONAL_ENTRIES; i++) {
4126 if (empty_addr(hw->address[i]))
4127 writel(0, hw->io + ADD_ADDR_INCR * i +
4128 KS_ADD_ADDR_0_HI);
4129 else
4130 hw_ena_add_addr(hw, i, hw->address[i]);
4134 static int hw_add_addr(struct ksz_hw *hw, u8 *mac_addr)
4136 int i;
4137 int j = ADDITIONAL_ENTRIES;
4139 if (!memcmp(hw->override_addr, mac_addr, MAC_ADDR_LEN))
4140 return 0;
4141 for (i = 0; i < hw->addr_list_size; i++) {
4142 if (!memcmp(hw->address[i], mac_addr, MAC_ADDR_LEN))
4143 return 0;
4144 if (ADDITIONAL_ENTRIES == j && empty_addr(hw->address[i]))
4145 j = i;
4147 if (j < ADDITIONAL_ENTRIES) {
4148 memcpy(hw->address[j], mac_addr, MAC_ADDR_LEN);
4149 hw_ena_add_addr(hw, j, hw->address[j]);
4150 return 0;
4152 return -1;
4155 static int hw_del_addr(struct ksz_hw *hw, u8 *mac_addr)
4157 int i;
4159 for (i = 0; i < hw->addr_list_size; i++) {
4160 if (!memcmp(hw->address[i], mac_addr, MAC_ADDR_LEN)) {
4161 memset(hw->address[i], 0, MAC_ADDR_LEN);
4162 writel(0, hw->io + ADD_ADDR_INCR * i +
4163 KS_ADD_ADDR_0_HI);
4164 return 0;
4167 return -1;
4171 * hw_clr_multicast - clear multicast addresses
4172 * @hw: The hardware instance.
4174 * This routine removes all multicast addresses set in the hardware.
4176 static void hw_clr_multicast(struct ksz_hw *hw)
4178 int i;
4180 for (i = 0; i < HW_MULTICAST_SIZE; i++) {
4181 hw->multi_bits[i] = 0;
4183 writeb(0, hw->io + KS884X_MULTICAST_0_OFFSET + i);
4188 * hw_set_grp_addr - set multicast addresses
4189 * @hw: The hardware instance.
4191 * This routine programs multicast addresses for the hardware to accept those
4192 * addresses.
4194 static void hw_set_grp_addr(struct ksz_hw *hw)
4196 int i;
4197 int index;
4198 int position;
4199 int value;
4201 memset(hw->multi_bits, 0, sizeof(u8) * HW_MULTICAST_SIZE);
4203 for (i = 0; i < hw->multi_list_size; i++) {
4204 position = (ether_crc(6, hw->multi_list[i]) >> 26) & 0x3f;
4205 index = position >> 3;
4206 value = 1 << (position & 7);
4207 hw->multi_bits[index] |= (u8) value;
4210 for (i = 0; i < HW_MULTICAST_SIZE; i++)
4211 writeb(hw->multi_bits[i], hw->io + KS884X_MULTICAST_0_OFFSET +
4216 * hw_set_multicast - enable or disable all multicast receiving
4217 * @hw: The hardware instance.
4218 * @multicast: To turn on or off the all multicast feature.
4220 * This routine enables/disables the hardware to accept all multicast packets.
4222 static void hw_set_multicast(struct ksz_hw *hw, u8 multicast)
4224 /* Stop receiving for reconfiguration. */
4225 hw_stop_rx(hw);
4227 if (multicast)
4228 hw->rx_cfg |= DMA_RX_ALL_MULTICAST;
4229 else
4230 hw->rx_cfg &= ~DMA_RX_ALL_MULTICAST;
4232 if (hw->enabled)
4233 hw_start_rx(hw);
4237 * hw_set_promiscuous - enable or disable promiscuous receiving
4238 * @hw: The hardware instance.
4239 * @prom: To turn on or off the promiscuous feature.
4241 * This routine enables/disables the hardware to accept all packets.
4243 static void hw_set_promiscuous(struct ksz_hw *hw, u8 prom)
4245 /* Stop receiving for reconfiguration. */
4246 hw_stop_rx(hw);
4248 if (prom)
4249 hw->rx_cfg |= DMA_RX_PROMISCUOUS;
4250 else
4251 hw->rx_cfg &= ~DMA_RX_PROMISCUOUS;
4253 if (hw->enabled)
4254 hw_start_rx(hw);
4258 * sw_enable - enable the switch
4259 * @hw: The hardware instance.
4260 * @enable: The flag to enable or disable the switch
4262 * This routine is used to enable/disable the switch in KSZ8842.
4264 static void sw_enable(struct ksz_hw *hw, int enable)
4266 int port;
4268 for (port = 0; port < SWITCH_PORT_NUM; port++) {
4269 if (hw->dev_count > 1) {
4270 /* Set port-base vlan membership with host port. */
4271 sw_cfg_port_base_vlan(hw, port,
4272 HOST_MASK | (1 << port));
4273 port_set_stp_state(hw, port, STP_STATE_DISABLED);
4274 } else {
4275 sw_cfg_port_base_vlan(hw, port, PORT_MASK);
4276 port_set_stp_state(hw, port, STP_STATE_FORWARDING);
4279 if (hw->dev_count > 1)
4280 port_set_stp_state(hw, SWITCH_PORT_NUM, STP_STATE_SIMPLE);
4281 else
4282 port_set_stp_state(hw, SWITCH_PORT_NUM, STP_STATE_FORWARDING);
4284 if (enable)
4285 enable = KS8842_START;
4286 writew(enable, hw->io + KS884X_CHIP_ID_OFFSET);
4290 * sw_setup - setup the switch
4291 * @hw: The hardware instance.
4293 * This routine setup the hardware switch engine for default operation.
4295 static void sw_setup(struct ksz_hw *hw)
4297 int port;
4299 sw_set_global_ctrl(hw);
4301 /* Enable switch broadcast storm protection at 10% percent rate. */
4302 sw_init_broad_storm(hw);
4303 hw_cfg_broad_storm(hw, BROADCAST_STORM_PROTECTION_RATE);
4304 for (port = 0; port < SWITCH_PORT_NUM; port++)
4305 sw_ena_broad_storm(hw, port);
4307 sw_init_prio(hw);
4309 sw_init_mirror(hw);
4311 sw_init_prio_rate(hw);
4313 sw_init_vlan(hw);
4315 if (hw->features & STP_SUPPORT)
4316 sw_init_stp(hw);
4317 if (!sw_chk(hw, KS8842_SWITCH_CTRL_1_OFFSET,
4318 SWITCH_TX_FLOW_CTRL | SWITCH_RX_FLOW_CTRL))
4319 hw->overrides |= PAUSE_FLOW_CTRL;
4320 sw_enable(hw, 1);
4324 * ksz_start_timer - start kernel timer
4325 * @info: Kernel timer information.
4326 * @time: The time tick.
4328 * This routine starts the kernel timer after the specified time tick.
4330 static void ksz_start_timer(struct ksz_timer_info *info, int time)
4332 info->cnt = 0;
4333 info->timer.expires = jiffies + time;
4334 add_timer(&info->timer);
4336 /* infinity */
4337 info->max = -1;
4341 * ksz_stop_timer - stop kernel timer
4342 * @info: Kernel timer information.
4344 * This routine stops the kernel timer.
4346 static void ksz_stop_timer(struct ksz_timer_info *info)
4348 if (info->max) {
4349 info->max = 0;
4350 del_timer_sync(&info->timer);
4354 static void ksz_init_timer(struct ksz_timer_info *info, int period,
4355 void (*function)(unsigned long), void *data)
4357 info->max = 0;
4358 info->period = period;
4359 init_timer(&info->timer);
4360 info->timer.function = function;
4361 info->timer.data = (unsigned long) data;
4364 static void ksz_update_timer(struct ksz_timer_info *info)
4366 ++info->cnt;
4367 if (info->max > 0) {
4368 if (info->cnt < info->max) {
4369 info->timer.expires = jiffies + info->period;
4370 add_timer(&info->timer);
4371 } else
4372 info->max = 0;
4373 } else if (info->max < 0) {
4374 info->timer.expires = jiffies + info->period;
4375 add_timer(&info->timer);
4380 * ksz_alloc_soft_desc - allocate software descriptors
4381 * @desc_info: Descriptor information structure.
4382 * @transmit: Indication that descriptors are for transmit.
4384 * This local function allocates software descriptors for manipulation in
4385 * memory.
4387 * Return 0 if successful.
4389 static int ksz_alloc_soft_desc(struct ksz_desc_info *desc_info, int transmit)
4391 desc_info->ring = kmalloc(sizeof(struct ksz_desc) * desc_info->alloc,
4392 GFP_KERNEL);
4393 if (!desc_info->ring)
4394 return 1;
4395 memset((void *) desc_info->ring, 0,
4396 sizeof(struct ksz_desc) * desc_info->alloc);
4397 hw_init_desc(desc_info, transmit);
4398 return 0;
4402 * ksz_alloc_desc - allocate hardware descriptors
4403 * @adapter: Adapter information structure.
4405 * This local function allocates hardware descriptors for receiving and
4406 * transmitting.
4408 * Return 0 if successful.
4410 static int ksz_alloc_desc(struct dev_info *adapter)
4412 struct ksz_hw *hw = &adapter->hw;
4413 int offset;
4415 /* Allocate memory for RX & TX descriptors. */
4416 adapter->desc_pool.alloc_size =
4417 hw->rx_desc_info.size * hw->rx_desc_info.alloc +
4418 hw->tx_desc_info.size * hw->tx_desc_info.alloc +
4419 DESC_ALIGNMENT;
4421 adapter->desc_pool.alloc_virt =
4422 pci_alloc_consistent(
4423 adapter->pdev, adapter->desc_pool.alloc_size,
4424 &adapter->desc_pool.dma_addr);
4425 if (adapter->desc_pool.alloc_virt == NULL) {
4426 adapter->desc_pool.alloc_size = 0;
4427 return 1;
4429 memset(adapter->desc_pool.alloc_virt, 0, adapter->desc_pool.alloc_size);
4431 /* Align to the next cache line boundary. */
4432 offset = (((ulong) adapter->desc_pool.alloc_virt % DESC_ALIGNMENT) ?
4433 (DESC_ALIGNMENT -
4434 ((ulong) adapter->desc_pool.alloc_virt % DESC_ALIGNMENT)) : 0);
4435 adapter->desc_pool.virt = adapter->desc_pool.alloc_virt + offset;
4436 adapter->desc_pool.phys = adapter->desc_pool.dma_addr + offset;
4438 /* Allocate receive/transmit descriptors. */
4439 hw->rx_desc_info.ring_virt = (struct ksz_hw_desc *)
4440 adapter->desc_pool.virt;
4441 hw->rx_desc_info.ring_phys = adapter->desc_pool.phys;
4442 offset = hw->rx_desc_info.alloc * hw->rx_desc_info.size;
4443 hw->tx_desc_info.ring_virt = (struct ksz_hw_desc *)
4444 (adapter->desc_pool.virt + offset);
4445 hw->tx_desc_info.ring_phys = adapter->desc_pool.phys + offset;
4447 if (ksz_alloc_soft_desc(&hw->rx_desc_info, 0))
4448 return 1;
4449 if (ksz_alloc_soft_desc(&hw->tx_desc_info, 1))
4450 return 1;
4452 return 0;
4456 * free_dma_buf - release DMA buffer resources
4457 * @adapter: Adapter information structure.
4459 * This routine is just a helper function to release the DMA buffer resources.
4461 static void free_dma_buf(struct dev_info *adapter, struct ksz_dma_buf *dma_buf,
4462 int direction)
4464 pci_unmap_single(adapter->pdev, dma_buf->dma, dma_buf->len, direction);
4465 dev_kfree_skb(dma_buf->skb);
4466 dma_buf->skb = NULL;
4467 dma_buf->dma = 0;
4471 * ksz_init_rx_buffers - initialize receive descriptors
4472 * @adapter: Adapter information structure.
4474 * This routine initializes DMA buffers for receiving.
4476 static void ksz_init_rx_buffers(struct dev_info *adapter)
4478 int i;
4479 struct ksz_desc *desc;
4480 struct ksz_dma_buf *dma_buf;
4481 struct ksz_hw *hw = &adapter->hw;
4482 struct ksz_desc_info *info = &hw->rx_desc_info;
4484 for (i = 0; i < hw->rx_desc_info.alloc; i++) {
4485 get_rx_pkt(info, &desc);
4487 dma_buf = DMA_BUFFER(desc);
4488 if (dma_buf->skb && dma_buf->len != adapter->mtu)
4489 free_dma_buf(adapter, dma_buf, PCI_DMA_FROMDEVICE);
4490 dma_buf->len = adapter->mtu;
4491 if (!dma_buf->skb)
4492 dma_buf->skb = alloc_skb(dma_buf->len, GFP_ATOMIC);
4493 if (dma_buf->skb && !dma_buf->dma) {
4494 dma_buf->skb->dev = adapter->dev;
4495 dma_buf->dma = pci_map_single(
4496 adapter->pdev,
4497 skb_tail_pointer(dma_buf->skb),
4498 dma_buf->len,
4499 PCI_DMA_FROMDEVICE);
4502 /* Set descriptor. */
4503 set_rx_buf(desc, dma_buf->dma);
4504 set_rx_len(desc, dma_buf->len);
4505 release_desc(desc);
4510 * ksz_alloc_mem - allocate memory for hardware descriptors
4511 * @adapter: Adapter information structure.
4513 * This function allocates memory for use by hardware descriptors for receiving
4514 * and transmitting.
4516 * Return 0 if successful.
4518 static int ksz_alloc_mem(struct dev_info *adapter)
4520 struct ksz_hw *hw = &adapter->hw;
4522 /* Determine the number of receive and transmit descriptors. */
4523 hw->rx_desc_info.alloc = NUM_OF_RX_DESC;
4524 hw->tx_desc_info.alloc = NUM_OF_TX_DESC;
4526 /* Determine how many descriptors to skip transmit interrupt. */
4527 hw->tx_int_cnt = 0;
4528 hw->tx_int_mask = NUM_OF_TX_DESC / 4;
4529 if (hw->tx_int_mask > 8)
4530 hw->tx_int_mask = 8;
4531 while (hw->tx_int_mask) {
4532 hw->tx_int_cnt++;
4533 hw->tx_int_mask >>= 1;
4535 if (hw->tx_int_cnt) {
4536 hw->tx_int_mask = (1 << (hw->tx_int_cnt - 1)) - 1;
4537 hw->tx_int_cnt = 0;
4540 /* Determine the descriptor size. */
4541 hw->rx_desc_info.size =
4542 (((sizeof(struct ksz_hw_desc) + DESC_ALIGNMENT - 1) /
4543 DESC_ALIGNMENT) * DESC_ALIGNMENT);
4544 hw->tx_desc_info.size =
4545 (((sizeof(struct ksz_hw_desc) + DESC_ALIGNMENT - 1) /
4546 DESC_ALIGNMENT) * DESC_ALIGNMENT);
4547 if (hw->rx_desc_info.size != sizeof(struct ksz_hw_desc))
4548 printk(KERN_ALERT
4549 "Hardware descriptor size not right!\n");
4550 ksz_check_desc_num(&hw->rx_desc_info);
4551 ksz_check_desc_num(&hw->tx_desc_info);
4553 /* Allocate descriptors. */
4554 if (ksz_alloc_desc(adapter))
4555 return 1;
4557 return 0;
4561 * ksz_free_desc - free software and hardware descriptors
4562 * @adapter: Adapter information structure.
4564 * This local routine frees the software and hardware descriptors allocated by
4565 * ksz_alloc_desc().
4567 static void ksz_free_desc(struct dev_info *adapter)
4569 struct ksz_hw *hw = &adapter->hw;
4571 /* Reset descriptor. */
4572 hw->rx_desc_info.ring_virt = NULL;
4573 hw->tx_desc_info.ring_virt = NULL;
4574 hw->rx_desc_info.ring_phys = 0;
4575 hw->tx_desc_info.ring_phys = 0;
4577 /* Free memory. */
4578 if (adapter->desc_pool.alloc_virt)
4579 pci_free_consistent(
4580 adapter->pdev,
4581 adapter->desc_pool.alloc_size,
4582 adapter->desc_pool.alloc_virt,
4583 adapter->desc_pool.dma_addr);
4585 /* Reset resource pool. */
4586 adapter->desc_pool.alloc_size = 0;
4587 adapter->desc_pool.alloc_virt = NULL;
4589 kfree(hw->rx_desc_info.ring);
4590 hw->rx_desc_info.ring = NULL;
4591 kfree(hw->tx_desc_info.ring);
4592 hw->tx_desc_info.ring = NULL;
4596 * ksz_free_buffers - free buffers used in the descriptors
4597 * @adapter: Adapter information structure.
4598 * @desc_info: Descriptor information structure.
4600 * This local routine frees buffers used in the DMA buffers.
4602 static void ksz_free_buffers(struct dev_info *adapter,
4603 struct ksz_desc_info *desc_info, int direction)
4605 int i;
4606 struct ksz_dma_buf *dma_buf;
4607 struct ksz_desc *desc = desc_info->ring;
4609 for (i = 0; i < desc_info->alloc; i++) {
4610 dma_buf = DMA_BUFFER(desc);
4611 if (dma_buf->skb)
4612 free_dma_buf(adapter, dma_buf, direction);
4613 desc++;
4618 * ksz_free_mem - free all resources used by descriptors
4619 * @adapter: Adapter information structure.
4621 * This local routine frees all the resources allocated by ksz_alloc_mem().
4623 static void ksz_free_mem(struct dev_info *adapter)
4625 /* Free transmit buffers. */
4626 ksz_free_buffers(adapter, &adapter->hw.tx_desc_info,
4627 PCI_DMA_TODEVICE);
4629 /* Free receive buffers. */
4630 ksz_free_buffers(adapter, &adapter->hw.rx_desc_info,
4631 PCI_DMA_FROMDEVICE);
4633 /* Free descriptors. */
4634 ksz_free_desc(adapter);
4637 static void get_mib_counters(struct ksz_hw *hw, int first, int cnt,
4638 u64 *counter)
4640 int i;
4641 int mib;
4642 int port;
4643 struct ksz_port_mib *port_mib;
4645 memset(counter, 0, sizeof(u64) * TOTAL_PORT_COUNTER_NUM);
4646 for (i = 0, port = first; i < cnt; i++, port++) {
4647 port_mib = &hw->port_mib[port];
4648 for (mib = port_mib->mib_start; mib < hw->mib_cnt; mib++)
4649 counter[mib] += port_mib->counter[mib];
4654 * send_packet - send packet
4655 * @skb: Socket buffer.
4656 * @dev: Network device.
4658 * This routine is used to send a packet out to the network.
4660 static void send_packet(struct sk_buff *skb, struct net_device *dev)
4662 struct ksz_desc *desc;
4663 struct ksz_desc *first;
4664 struct dev_priv *priv = netdev_priv(dev);
4665 struct dev_info *hw_priv = priv->adapter;
4666 struct ksz_hw *hw = &hw_priv->hw;
4667 struct ksz_desc_info *info = &hw->tx_desc_info;
4668 struct ksz_dma_buf *dma_buf;
4669 int len;
4670 int last_frag = skb_shinfo(skb)->nr_frags;
4673 * KSZ8842 with multiple device interfaces needs to be told which port
4674 * to send.
4676 if (hw->dev_count > 1)
4677 hw->dst_ports = 1 << priv->port.first_port;
4679 /* Hardware will pad the length to 60. */
4680 len = skb->len;
4682 /* Remember the very first descriptor. */
4683 first = info->cur;
4684 desc = first;
4686 dma_buf = DMA_BUFFER(desc);
4687 if (last_frag) {
4688 int frag;
4689 skb_frag_t *this_frag;
4691 dma_buf->len = skb->len - skb->data_len;
4693 dma_buf->dma = pci_map_single(
4694 hw_priv->pdev, skb->data, dma_buf->len,
4695 PCI_DMA_TODEVICE);
4696 set_tx_buf(desc, dma_buf->dma);
4697 set_tx_len(desc, dma_buf->len);
4699 frag = 0;
4700 do {
4701 this_frag = &skb_shinfo(skb)->frags[frag];
4703 /* Get a new descriptor. */
4704 get_tx_pkt(info, &desc);
4706 /* Keep track of descriptors used so far. */
4707 ++hw->tx_int_cnt;
4709 dma_buf = DMA_BUFFER(desc);
4710 dma_buf->len = this_frag->size;
4712 dma_buf->dma = pci_map_single(
4713 hw_priv->pdev,
4714 page_address(this_frag->page) +
4715 this_frag->page_offset,
4716 dma_buf->len,
4717 PCI_DMA_TODEVICE);
4718 set_tx_buf(desc, dma_buf->dma);
4719 set_tx_len(desc, dma_buf->len);
4721 frag++;
4722 if (frag == last_frag)
4723 break;
4725 /* Do not release the last descriptor here. */
4726 release_desc(desc);
4727 } while (1);
4729 /* current points to the last descriptor. */
4730 info->cur = desc;
4732 /* Release the first descriptor. */
4733 release_desc(first);
4734 } else {
4735 dma_buf->len = len;
4737 dma_buf->dma = pci_map_single(
4738 hw_priv->pdev, skb->data, dma_buf->len,
4739 PCI_DMA_TODEVICE);
4740 set_tx_buf(desc, dma_buf->dma);
4741 set_tx_len(desc, dma_buf->len);
4744 if (skb->ip_summed == CHECKSUM_PARTIAL) {
4745 (desc)->sw.buf.tx.csum_gen_tcp = 1;
4746 (desc)->sw.buf.tx.csum_gen_udp = 1;
4750 * The last descriptor holds the packet so that it can be returned to
4751 * network subsystem after all descriptors are transmitted.
4753 dma_buf->skb = skb;
4755 hw_send_pkt(hw);
4757 /* Update transmit statistics. */
4758 priv->stats.tx_packets++;
4759 priv->stats.tx_bytes += len;
4763 * transmit_cleanup - clean up transmit descriptors
4764 * @dev: Network device.
4766 * This routine is called to clean up the transmitted buffers.
4768 static void transmit_cleanup(struct dev_info *hw_priv, int normal)
4770 int last;
4771 union desc_stat status;
4772 struct ksz_hw *hw = &hw_priv->hw;
4773 struct ksz_desc_info *info = &hw->tx_desc_info;
4774 struct ksz_desc *desc;
4775 struct ksz_dma_buf *dma_buf;
4776 struct net_device *dev = NULL;
4778 spin_lock(&hw_priv->hwlock);
4779 last = info->last;
4781 while (info->avail < info->alloc) {
4782 /* Get next descriptor which is not hardware owned. */
4783 desc = &info->ring[last];
4784 status.data = le32_to_cpu(desc->phw->ctrl.data);
4785 if (status.tx.hw_owned) {
4786 if (normal)
4787 break;
4788 else
4789 reset_desc(desc, status);
4792 dma_buf = DMA_BUFFER(desc);
4793 pci_unmap_single(
4794 hw_priv->pdev, dma_buf->dma, dma_buf->len,
4795 PCI_DMA_TODEVICE);
4797 /* This descriptor contains the last buffer in the packet. */
4798 if (dma_buf->skb) {
4799 dev = dma_buf->skb->dev;
4801 /* Release the packet back to network subsystem. */
4802 dev_kfree_skb_irq(dma_buf->skb);
4803 dma_buf->skb = NULL;
4806 /* Free the transmitted descriptor. */
4807 last++;
4808 last &= info->mask;
4809 info->avail++;
4811 info->last = last;
4812 spin_unlock(&hw_priv->hwlock);
4814 /* Notify the network subsystem that the packet has been sent. */
4815 if (dev)
4816 dev->trans_start = jiffies;
4820 * transmit_done - transmit done processing
4821 * @dev: Network device.
4823 * This routine is called when the transmit interrupt is triggered, indicating
4824 * either a packet is sent successfully or there are transmit errors.
4826 static void tx_done(struct dev_info *hw_priv)
4828 struct ksz_hw *hw = &hw_priv->hw;
4829 int port;
4831 transmit_cleanup(hw_priv, 1);
4833 for (port = 0; port < hw->dev_count; port++) {
4834 struct net_device *dev = hw->port_info[port].pdev;
4836 if (netif_running(dev) && netif_queue_stopped(dev))
4837 netif_wake_queue(dev);
4841 static inline void copy_old_skb(struct sk_buff *old, struct sk_buff *skb)
4843 skb->dev = old->dev;
4844 skb->protocol = old->protocol;
4845 skb->ip_summed = old->ip_summed;
4846 skb->csum = old->csum;
4847 skb_set_network_header(skb, ETH_HLEN);
4849 dev_kfree_skb(old);
4853 * netdev_tx - send out packet
4854 * @skb: Socket buffer.
4855 * @dev: Network device.
4857 * This function is used by the upper network layer to send out a packet.
4859 * Return 0 if successful; otherwise an error code indicating failure.
4861 static int netdev_tx(struct sk_buff *skb, struct net_device *dev)
4863 struct dev_priv *priv = netdev_priv(dev);
4864 struct dev_info *hw_priv = priv->adapter;
4865 struct ksz_hw *hw = &hw_priv->hw;
4866 int left;
4867 int num = 1;
4868 int rc = 0;
4870 if (hw->features & SMALL_PACKET_TX_BUG) {
4871 struct sk_buff *org_skb = skb;
4873 if (skb->len <= 48) {
4874 if (skb_end_pointer(skb) - skb->data >= 50) {
4875 memset(&skb->data[skb->len], 0, 50 - skb->len);
4876 skb->len = 50;
4877 } else {
4878 skb = dev_alloc_skb(50);
4879 if (!skb)
4880 return NETDEV_TX_BUSY;
4881 memcpy(skb->data, org_skb->data, org_skb->len);
4882 memset(&skb->data[org_skb->len], 0,
4883 50 - org_skb->len);
4884 skb->len = 50;
4885 copy_old_skb(org_skb, skb);
4890 spin_lock_irq(&hw_priv->hwlock);
4892 num = skb_shinfo(skb)->nr_frags + 1;
4893 left = hw_alloc_pkt(hw, skb->len, num);
4894 if (left) {
4895 if (left < num ||
4896 ((hw->features & IPV6_CSUM_GEN_HACK) &&
4897 (CHECKSUM_PARTIAL == skb->ip_summed) &&
4898 (ETH_P_IPV6 == htons(skb->protocol)))) {
4899 struct sk_buff *org_skb = skb;
4901 skb = dev_alloc_skb(org_skb->len);
4902 if (!skb) {
4903 rc = NETDEV_TX_BUSY;
4904 goto unlock;
4906 skb_copy_and_csum_dev(org_skb, skb->data);
4907 org_skb->ip_summed = 0;
4908 skb->len = org_skb->len;
4909 copy_old_skb(org_skb, skb);
4911 send_packet(skb, dev);
4912 if (left <= num)
4913 netif_stop_queue(dev);
4914 } else {
4915 /* Stop the transmit queue until packet is allocated. */
4916 netif_stop_queue(dev);
4917 rc = NETDEV_TX_BUSY;
4919 unlock:
4920 spin_unlock_irq(&hw_priv->hwlock);
4922 return rc;
4926 * netdev_tx_timeout - transmit timeout processing
4927 * @dev: Network device.
4929 * This routine is called when the transmit timer expires. That indicates the
4930 * hardware is not running correctly because transmit interrupts are not
4931 * triggered to free up resources so that the transmit routine can continue
4932 * sending out packets. The hardware is reset to correct the problem.
4934 static void netdev_tx_timeout(struct net_device *dev)
4936 static unsigned long last_reset;
4938 struct dev_priv *priv = netdev_priv(dev);
4939 struct dev_info *hw_priv = priv->adapter;
4940 struct ksz_hw *hw = &hw_priv->hw;
4941 int port;
4943 if (hw->dev_count > 1) {
4945 * Only reset the hardware if time between calls is long
4946 * enough.
4948 if (jiffies - last_reset <= dev->watchdog_timeo)
4949 hw_priv = NULL;
4952 last_reset = jiffies;
4953 if (hw_priv) {
4954 hw_dis_intr(hw);
4955 hw_disable(hw);
4957 transmit_cleanup(hw_priv, 0);
4958 hw_reset_pkts(&hw->rx_desc_info);
4959 hw_reset_pkts(&hw->tx_desc_info);
4960 ksz_init_rx_buffers(hw_priv);
4962 hw_reset(hw);
4964 hw_set_desc_base(hw,
4965 hw->tx_desc_info.ring_phys,
4966 hw->rx_desc_info.ring_phys);
4967 hw_set_addr(hw);
4968 if (hw->all_multi)
4969 hw_set_multicast(hw, hw->all_multi);
4970 else if (hw->multi_list_size)
4971 hw_set_grp_addr(hw);
4973 if (hw->dev_count > 1) {
4974 hw_set_add_addr(hw);
4975 for (port = 0; port < SWITCH_PORT_NUM; port++) {
4976 struct net_device *port_dev;
4978 port_set_stp_state(hw, port,
4979 STP_STATE_DISABLED);
4981 port_dev = hw->port_info[port].pdev;
4982 if (netif_running(port_dev))
4983 port_set_stp_state(hw, port,
4984 STP_STATE_SIMPLE);
4988 hw_enable(hw);
4989 hw_ena_intr(hw);
4992 dev->trans_start = jiffies;
4993 netif_wake_queue(dev);
4996 static inline void csum_verified(struct sk_buff *skb)
4998 unsigned short protocol;
4999 struct iphdr *iph;
5001 protocol = skb->protocol;
5002 skb_reset_network_header(skb);
5003 iph = (struct iphdr *) skb_network_header(skb);
5004 if (protocol == htons(ETH_P_8021Q)) {
5005 protocol = iph->tot_len;
5006 skb_set_network_header(skb, VLAN_HLEN);
5007 iph = (struct iphdr *) skb_network_header(skb);
5009 if (protocol == htons(ETH_P_IP)) {
5010 if (iph->protocol == IPPROTO_TCP)
5011 skb->ip_summed = CHECKSUM_UNNECESSARY;
5015 static inline int rx_proc(struct net_device *dev, struct ksz_hw* hw,
5016 struct ksz_desc *desc, union desc_stat status)
5018 int packet_len;
5019 struct dev_priv *priv = netdev_priv(dev);
5020 struct dev_info *hw_priv = priv->adapter;
5021 struct ksz_dma_buf *dma_buf;
5022 struct sk_buff *skb;
5023 int rx_status;
5025 /* Received length includes 4-byte CRC. */
5026 packet_len = status.rx.frame_len - 4;
5028 dma_buf = DMA_BUFFER(desc);
5029 pci_dma_sync_single_for_cpu(
5030 hw_priv->pdev, dma_buf->dma, packet_len + 4,
5031 PCI_DMA_FROMDEVICE);
5033 do {
5034 /* skb->data != skb->head */
5035 skb = dev_alloc_skb(packet_len + 2);
5036 if (!skb) {
5037 priv->stats.rx_dropped++;
5038 return -ENOMEM;
5042 * Align socket buffer in 4-byte boundary for better
5043 * performance.
5045 skb_reserve(skb, 2);
5047 memcpy(skb_put(skb, packet_len),
5048 dma_buf->skb->data, packet_len);
5049 } while (0);
5051 skb->dev = dev;
5053 skb->protocol = eth_type_trans(skb, dev);
5055 if (hw->rx_cfg & (DMA_RX_CSUM_UDP | DMA_RX_CSUM_TCP))
5056 csum_verified(skb);
5058 /* Update receive statistics. */
5059 priv->stats.rx_packets++;
5060 priv->stats.rx_bytes += packet_len;
5062 /* Notify upper layer for received packet. */
5063 dev->last_rx = jiffies;
5065 rx_status = netif_rx(skb);
5067 return 0;
5070 static int dev_rcv_packets(struct dev_info *hw_priv)
5072 int next;
5073 union desc_stat status;
5074 struct ksz_hw *hw = &hw_priv->hw;
5075 struct net_device *dev = hw->port_info[0].pdev;
5076 struct ksz_desc_info *info = &hw->rx_desc_info;
5077 int left = info->alloc;
5078 struct ksz_desc *desc;
5079 int received = 0;
5081 next = info->next;
5082 while (left--) {
5083 /* Get next descriptor which is not hardware owned. */
5084 desc = &info->ring[next];
5085 status.data = le32_to_cpu(desc->phw->ctrl.data);
5086 if (status.rx.hw_owned)
5087 break;
5089 /* Status valid only when last descriptor bit is set. */
5090 if (status.rx.last_desc && status.rx.first_desc) {
5091 if (rx_proc(dev, hw, desc, status))
5092 goto release_packet;
5093 received++;
5096 release_packet:
5097 release_desc(desc);
5098 next++;
5099 next &= info->mask;
5101 info->next = next;
5103 return received;
5106 static int port_rcv_packets(struct dev_info *hw_priv)
5108 int next;
5109 union desc_stat status;
5110 struct ksz_hw *hw = &hw_priv->hw;
5111 struct net_device *dev = hw->port_info[0].pdev;
5112 struct ksz_desc_info *info = &hw->rx_desc_info;
5113 int left = info->alloc;
5114 struct ksz_desc *desc;
5115 int received = 0;
5117 next = info->next;
5118 while (left--) {
5119 /* Get next descriptor which is not hardware owned. */
5120 desc = &info->ring[next];
5121 status.data = le32_to_cpu(desc->phw->ctrl.data);
5122 if (status.rx.hw_owned)
5123 break;
5125 if (hw->dev_count > 1) {
5126 /* Get received port number. */
5127 int p = HW_TO_DEV_PORT(status.rx.src_port);
5129 dev = hw->port_info[p].pdev;
5130 if (!netif_running(dev))
5131 goto release_packet;
5134 /* Status valid only when last descriptor bit is set. */
5135 if (status.rx.last_desc && status.rx.first_desc) {
5136 if (rx_proc(dev, hw, desc, status))
5137 goto release_packet;
5138 received++;
5141 release_packet:
5142 release_desc(desc);
5143 next++;
5144 next &= info->mask;
5146 info->next = next;
5148 return received;
5151 static int dev_rcv_special(struct dev_info *hw_priv)
5153 int next;
5154 union desc_stat status;
5155 struct ksz_hw *hw = &hw_priv->hw;
5156 struct net_device *dev = hw->port_info[0].pdev;
5157 struct ksz_desc_info *info = &hw->rx_desc_info;
5158 int left = info->alloc;
5159 struct ksz_desc *desc;
5160 int received = 0;
5162 next = info->next;
5163 while (left--) {
5164 /* Get next descriptor which is not hardware owned. */
5165 desc = &info->ring[next];
5166 status.data = le32_to_cpu(desc->phw->ctrl.data);
5167 if (status.rx.hw_owned)
5168 break;
5170 if (hw->dev_count > 1) {
5171 /* Get received port number. */
5172 int p = HW_TO_DEV_PORT(status.rx.src_port);
5174 dev = hw->port_info[p].pdev;
5175 if (!netif_running(dev))
5176 goto release_packet;
5179 /* Status valid only when last descriptor bit is set. */
5180 if (status.rx.last_desc && status.rx.first_desc) {
5182 * Receive without error. With receive errors
5183 * disabled, packets with receive errors will be
5184 * dropped, so no need to check the error bit.
5186 if (!status.rx.error || (status.data &
5187 KS_DESC_RX_ERROR_COND) ==
5188 KS_DESC_RX_ERROR_TOO_LONG) {
5189 if (rx_proc(dev, hw, desc, status))
5190 goto release_packet;
5191 received++;
5192 } else {
5193 struct dev_priv *priv = netdev_priv(dev);
5195 /* Update receive error statistics. */
5196 priv->port.counter[OID_COUNTER_RCV_ERROR]++;
5200 release_packet:
5201 release_desc(desc);
5202 next++;
5203 next &= info->mask;
5205 info->next = next;
5207 return received;
5210 static void rx_proc_task(unsigned long data)
5212 struct dev_info *hw_priv = (struct dev_info *) data;
5213 struct ksz_hw *hw = &hw_priv->hw;
5215 if (!hw->enabled)
5216 return;
5217 if (unlikely(!hw_priv->dev_rcv(hw_priv))) {
5219 /* In case receive process is suspended because of overrun. */
5220 hw_resume_rx(hw);
5222 /* tasklets are interruptible. */
5223 spin_lock_irq(&hw_priv->hwlock);
5224 hw_turn_on_intr(hw, KS884X_INT_RX_MASK);
5225 spin_unlock_irq(&hw_priv->hwlock);
5226 } else {
5227 hw_ack_intr(hw, KS884X_INT_RX);
5228 tasklet_schedule(&hw_priv->rx_tasklet);
5232 static void tx_proc_task(unsigned long data)
5234 struct dev_info *hw_priv = (struct dev_info *) data;
5235 struct ksz_hw *hw = &hw_priv->hw;
5237 hw_ack_intr(hw, KS884X_INT_TX_MASK);
5239 tx_done(hw_priv);
5241 /* tasklets are interruptible. */
5242 spin_lock_irq(&hw_priv->hwlock);
5243 hw_turn_on_intr(hw, KS884X_INT_TX);
5244 spin_unlock_irq(&hw_priv->hwlock);
5247 static inline void handle_rx_stop(struct ksz_hw *hw)
5249 /* Receive just has been stopped. */
5250 if (0 == hw->rx_stop)
5251 hw->intr_mask &= ~KS884X_INT_RX_STOPPED;
5252 else if (hw->rx_stop > 1) {
5253 if (hw->enabled && (hw->rx_cfg & DMA_RX_ENABLE)) {
5254 hw_start_rx(hw);
5255 } else {
5256 hw->intr_mask &= ~KS884X_INT_RX_STOPPED;
5257 hw->rx_stop = 0;
5259 } else
5260 /* Receive just has been started. */
5261 hw->rx_stop++;
5265 * netdev_intr - interrupt handling
5266 * @irq: Interrupt number.
5267 * @dev_id: Network device.
5269 * This function is called by upper network layer to signal interrupt.
5271 * Return IRQ_HANDLED if interrupt is handled.
5273 static irqreturn_t netdev_intr(int irq, void *dev_id)
5275 uint int_enable = 0;
5276 struct net_device *dev = (struct net_device *) dev_id;
5277 struct dev_priv *priv = netdev_priv(dev);
5278 struct dev_info *hw_priv = priv->adapter;
5279 struct ksz_hw *hw = &hw_priv->hw;
5281 hw_read_intr(hw, &int_enable);
5283 /* Not our interrupt! */
5284 if (!int_enable)
5285 return IRQ_NONE;
5287 do {
5288 hw_ack_intr(hw, int_enable);
5289 int_enable &= hw->intr_mask;
5291 if (unlikely(int_enable & KS884X_INT_TX_MASK)) {
5292 hw_dis_intr_bit(hw, KS884X_INT_TX_MASK);
5293 tasklet_schedule(&hw_priv->tx_tasklet);
5296 if (likely(int_enable & KS884X_INT_RX)) {
5297 hw_dis_intr_bit(hw, KS884X_INT_RX);
5298 tasklet_schedule(&hw_priv->rx_tasklet);
5301 if (unlikely(int_enable & KS884X_INT_RX_OVERRUN)) {
5302 priv->stats.rx_fifo_errors++;
5303 hw_resume_rx(hw);
5306 if (unlikely(int_enable & KS884X_INT_PHY)) {
5307 struct ksz_port *port = &priv->port;
5309 hw->features |= LINK_INT_WORKING;
5310 port_get_link_speed(port);
5313 if (unlikely(int_enable & KS884X_INT_RX_STOPPED)) {
5314 handle_rx_stop(hw);
5315 break;
5318 if (unlikely(int_enable & KS884X_INT_TX_STOPPED)) {
5319 u32 data;
5321 hw->intr_mask &= ~KS884X_INT_TX_STOPPED;
5322 printk(KERN_INFO "Tx stopped\n");
5323 data = readl(hw->io + KS_DMA_TX_CTRL);
5324 if (!(data & DMA_TX_ENABLE))
5325 printk(KERN_INFO "Tx disabled\n");
5326 break;
5328 } while (0);
5330 hw_ena_intr(hw);
5332 return IRQ_HANDLED;
5336 * Linux network device functions
5339 static unsigned long next_jiffies;
5341 #ifdef CONFIG_NET_POLL_CONTROLLER
5342 static void netdev_netpoll(struct net_device *dev)
5344 struct dev_priv *priv = netdev_priv(dev);
5345 struct dev_info *hw_priv = priv->adapter;
5347 hw_dis_intr(&hw_priv->hw);
5348 netdev_intr(dev->irq, dev);
5350 #endif
5352 static void bridge_change(struct ksz_hw *hw)
5354 int port;
5355 u8 member;
5356 struct ksz_switch *sw = hw->ksz_switch;
5358 /* No ports in forwarding state. */
5359 if (!sw->member) {
5360 port_set_stp_state(hw, SWITCH_PORT_NUM, STP_STATE_SIMPLE);
5361 sw_block_addr(hw);
5363 for (port = 0; port < SWITCH_PORT_NUM; port++) {
5364 if (STP_STATE_FORWARDING == sw->port_cfg[port].stp_state)
5365 member = HOST_MASK | sw->member;
5366 else
5367 member = HOST_MASK | (1 << port);
5368 if (member != sw->port_cfg[port].member)
5369 sw_cfg_port_base_vlan(hw, port, member);
5374 * netdev_close - close network device
5375 * @dev: Network device.
5377 * This function process the close operation of network device. This is caused
5378 * by the user command "ifconfig ethX down."
5380 * Return 0 if successful; otherwise an error code indicating failure.
5382 static int netdev_close(struct net_device *dev)
5384 struct dev_priv *priv = netdev_priv(dev);
5385 struct dev_info *hw_priv = priv->adapter;
5386 struct ksz_port *port = &priv->port;
5387 struct ksz_hw *hw = &hw_priv->hw;
5388 int pi;
5390 netif_stop_queue(dev);
5392 ksz_stop_timer(&priv->monitor_timer_info);
5394 /* Need to shut the port manually in multiple device interfaces mode. */
5395 if (hw->dev_count > 1) {
5396 port_set_stp_state(hw, port->first_port, STP_STATE_DISABLED);
5398 /* Port is closed. Need to change bridge setting. */
5399 if (hw->features & STP_SUPPORT) {
5400 pi = 1 << port->first_port;
5401 if (hw->ksz_switch->member & pi) {
5402 hw->ksz_switch->member &= ~pi;
5403 bridge_change(hw);
5407 if (port->first_port > 0)
5408 hw_del_addr(hw, dev->dev_addr);
5409 if (!hw_priv->wol_enable)
5410 port_set_power_saving(port, true);
5412 if (priv->multicast)
5413 --hw->all_multi;
5414 if (priv->promiscuous)
5415 --hw->promiscuous;
5417 hw_priv->opened--;
5418 if (!(hw_priv->opened)) {
5419 ksz_stop_timer(&hw_priv->mib_timer_info);
5420 flush_work(&hw_priv->mib_read);
5422 hw_dis_intr(hw);
5423 hw_disable(hw);
5424 hw_clr_multicast(hw);
5426 /* Delay for receive task to stop scheduling itself. */
5427 msleep(2000 / HZ);
5429 tasklet_disable(&hw_priv->rx_tasklet);
5430 tasklet_disable(&hw_priv->tx_tasklet);
5431 free_irq(dev->irq, hw_priv->dev);
5433 transmit_cleanup(hw_priv, 0);
5434 hw_reset_pkts(&hw->rx_desc_info);
5435 hw_reset_pkts(&hw->tx_desc_info);
5437 /* Clean out static MAC table when the switch is shutdown. */
5438 if (hw->features & STP_SUPPORT)
5439 sw_clr_sta_mac_table(hw);
5442 return 0;
5445 static void hw_cfg_huge_frame(struct dev_info *hw_priv, struct ksz_hw *hw)
5447 if (hw->ksz_switch) {
5448 u32 data;
5450 data = readw(hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
5451 if (hw->features & RX_HUGE_FRAME)
5452 data |= SWITCH_HUGE_PACKET;
5453 else
5454 data &= ~SWITCH_HUGE_PACKET;
5455 writew(data, hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
5457 if (hw->features & RX_HUGE_FRAME) {
5458 hw->rx_cfg |= DMA_RX_ERROR;
5459 hw_priv->dev_rcv = dev_rcv_special;
5460 } else {
5461 hw->rx_cfg &= ~DMA_RX_ERROR;
5462 if (hw->dev_count > 1)
5463 hw_priv->dev_rcv = port_rcv_packets;
5464 else
5465 hw_priv->dev_rcv = dev_rcv_packets;
5469 static int prepare_hardware(struct net_device *dev)
5471 struct dev_priv *priv = netdev_priv(dev);
5472 struct dev_info *hw_priv = priv->adapter;
5473 struct ksz_hw *hw = &hw_priv->hw;
5474 int rc = 0;
5476 /* Remember the network device that requests interrupts. */
5477 hw_priv->dev = dev;
5478 rc = request_irq(dev->irq, netdev_intr, IRQF_SHARED, dev->name, dev);
5479 if (rc)
5480 return rc;
5481 tasklet_enable(&hw_priv->rx_tasklet);
5482 tasklet_enable(&hw_priv->tx_tasklet);
5484 hw->promiscuous = 0;
5485 hw->all_multi = 0;
5486 hw->multi_list_size = 0;
5488 hw_reset(hw);
5490 hw_set_desc_base(hw,
5491 hw->tx_desc_info.ring_phys, hw->rx_desc_info.ring_phys);
5492 hw_set_addr(hw);
5493 hw_cfg_huge_frame(hw_priv, hw);
5494 ksz_init_rx_buffers(hw_priv);
5495 return 0;
5499 * netdev_open - open network device
5500 * @dev: Network device.
5502 * This function process the open operation of network device. This is caused
5503 * by the user command "ifconfig ethX up."
5505 * Return 0 if successful; otherwise an error code indicating failure.
5507 static int netdev_open(struct net_device *dev)
5509 struct dev_priv *priv = netdev_priv(dev);
5510 struct dev_info *hw_priv = priv->adapter;
5511 struct ksz_hw *hw = &hw_priv->hw;
5512 struct ksz_port *port = &priv->port;
5513 int i;
5514 int p;
5515 int rc = 0;
5517 priv->multicast = 0;
5518 priv->promiscuous = 0;
5520 /* Reset device statistics. */
5521 memset(&priv->stats, 0, sizeof(struct net_device_stats));
5522 memset((void *) port->counter, 0,
5523 (sizeof(u64) * OID_COUNTER_LAST));
5525 if (!(hw_priv->opened)) {
5526 rc = prepare_hardware(dev);
5527 if (rc)
5528 return rc;
5529 for (i = 0; i < hw->mib_port_cnt; i++) {
5530 if (next_jiffies < jiffies)
5531 next_jiffies = jiffies + HZ * 2;
5532 else
5533 next_jiffies += HZ * 1;
5534 hw_priv->counter[i].time = next_jiffies;
5535 hw->port_mib[i].state = media_disconnected;
5536 port_init_cnt(hw, i);
5538 if (hw->ksz_switch)
5539 hw->port_mib[HOST_PORT].state = media_connected;
5540 else {
5541 hw_add_wol_bcast(hw);
5542 hw_cfg_wol_pme(hw, 0);
5543 hw_clr_wol_pme_status(&hw_priv->hw);
5546 port_set_power_saving(port, false);
5548 for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
5550 * Initialize to invalid value so that link detection
5551 * is done.
5553 hw->port_info[p].partner = 0xFF;
5554 hw->port_info[p].state = media_disconnected;
5557 /* Need to open the port in multiple device interfaces mode. */
5558 if (hw->dev_count > 1) {
5559 port_set_stp_state(hw, port->first_port, STP_STATE_SIMPLE);
5560 if (port->first_port > 0)
5561 hw_add_addr(hw, dev->dev_addr);
5564 port_get_link_speed(port);
5565 if (port->force_link)
5566 port_force_link_speed(port);
5567 else
5568 port_set_link_speed(port);
5570 if (!(hw_priv->opened)) {
5571 hw_setup_intr(hw);
5572 hw_enable(hw);
5573 hw_ena_intr(hw);
5575 if (hw->mib_port_cnt)
5576 ksz_start_timer(&hw_priv->mib_timer_info,
5577 hw_priv->mib_timer_info.period);
5580 hw_priv->opened++;
5582 ksz_start_timer(&priv->monitor_timer_info,
5583 priv->monitor_timer_info.period);
5585 priv->media_state = port->linked->state;
5587 if (media_connected == priv->media_state)
5588 netif_carrier_on(dev);
5589 else
5590 netif_carrier_off(dev);
5591 if (netif_msg_link(priv))
5592 printk(KERN_INFO "%s link %s\n", dev->name,
5593 (media_connected == priv->media_state ?
5594 "on" : "off"));
5596 netif_start_queue(dev);
5598 return 0;
5601 /* RX errors = rx_errors */
5602 /* RX dropped = rx_dropped */
5603 /* RX overruns = rx_fifo_errors */
5604 /* RX frame = rx_crc_errors + rx_frame_errors + rx_length_errors */
5605 /* TX errors = tx_errors */
5606 /* TX dropped = tx_dropped */
5607 /* TX overruns = tx_fifo_errors */
5608 /* TX carrier = tx_aborted_errors + tx_carrier_errors + tx_window_errors */
5609 /* collisions = collisions */
5612 * netdev_query_statistics - query network device statistics
5613 * @dev: Network device.
5615 * This function returns the statistics of the network device. The device
5616 * needs not be opened.
5618 * Return network device statistics.
5620 static struct net_device_stats *netdev_query_statistics(struct net_device *dev)
5622 struct dev_priv *priv = netdev_priv(dev);
5623 struct ksz_port *port = &priv->port;
5624 struct ksz_hw *hw = &priv->adapter->hw;
5625 struct ksz_port_mib *mib;
5626 int i;
5627 int p;
5629 priv->stats.rx_errors = port->counter[OID_COUNTER_RCV_ERROR];
5630 priv->stats.tx_errors = port->counter[OID_COUNTER_XMIT_ERROR];
5632 /* Reset to zero to add count later. */
5633 priv->stats.multicast = 0;
5634 priv->stats.collisions = 0;
5635 priv->stats.rx_length_errors = 0;
5636 priv->stats.rx_crc_errors = 0;
5637 priv->stats.rx_frame_errors = 0;
5638 priv->stats.tx_window_errors = 0;
5640 for (i = 0, p = port->first_port; i < port->mib_port_cnt; i++, p++) {
5641 mib = &hw->port_mib[p];
5643 priv->stats.multicast += (unsigned long)
5644 mib->counter[MIB_COUNTER_RX_MULTICAST];
5646 priv->stats.collisions += (unsigned long)
5647 mib->counter[MIB_COUNTER_TX_TOTAL_COLLISION];
5649 priv->stats.rx_length_errors += (unsigned long)(
5650 mib->counter[MIB_COUNTER_RX_UNDERSIZE] +
5651 mib->counter[MIB_COUNTER_RX_FRAGMENT] +
5652 mib->counter[MIB_COUNTER_RX_OVERSIZE] +
5653 mib->counter[MIB_COUNTER_RX_JABBER]);
5654 priv->stats.rx_crc_errors += (unsigned long)
5655 mib->counter[MIB_COUNTER_RX_CRC_ERR];
5656 priv->stats.rx_frame_errors += (unsigned long)(
5657 mib->counter[MIB_COUNTER_RX_ALIGNMENT_ERR] +
5658 mib->counter[MIB_COUNTER_RX_SYMBOL_ERR]);
5660 priv->stats.tx_window_errors += (unsigned long)
5661 mib->counter[MIB_COUNTER_TX_LATE_COLLISION];
5664 return &priv->stats;
5668 * netdev_set_mac_address - set network device MAC address
5669 * @dev: Network device.
5670 * @addr: Buffer of MAC address.
5672 * This function is used to set the MAC address of the network device.
5674 * Return 0 to indicate success.
5676 static int netdev_set_mac_address(struct net_device *dev, void *addr)
5678 struct dev_priv *priv = netdev_priv(dev);
5679 struct dev_info *hw_priv = priv->adapter;
5680 struct ksz_hw *hw = &hw_priv->hw;
5681 struct sockaddr *mac = addr;
5682 uint interrupt;
5684 if (priv->port.first_port > 0)
5685 hw_del_addr(hw, dev->dev_addr);
5686 else {
5687 hw->mac_override = 1;
5688 memcpy(hw->override_addr, mac->sa_data, MAC_ADDR_LEN);
5691 memcpy(dev->dev_addr, mac->sa_data, MAX_ADDR_LEN);
5693 interrupt = hw_block_intr(hw);
5695 if (priv->port.first_port > 0)
5696 hw_add_addr(hw, dev->dev_addr);
5697 else
5698 hw_set_addr(hw);
5699 hw_restore_intr(hw, interrupt);
5701 return 0;
5704 static void dev_set_promiscuous(struct net_device *dev, struct dev_priv *priv,
5705 struct ksz_hw *hw, int promiscuous)
5707 if (promiscuous != priv->promiscuous) {
5708 u8 prev_state = hw->promiscuous;
5710 if (promiscuous)
5711 ++hw->promiscuous;
5712 else
5713 --hw->promiscuous;
5714 priv->promiscuous = promiscuous;
5716 /* Turn on/off promiscuous mode. */
5717 if (hw->promiscuous <= 1 && prev_state <= 1)
5718 hw_set_promiscuous(hw, hw->promiscuous);
5721 * Port is not in promiscuous mode, meaning it is released
5722 * from the bridge.
5724 if ((hw->features & STP_SUPPORT) && !promiscuous &&
5725 dev->br_port) {
5726 struct ksz_switch *sw = hw->ksz_switch;
5727 int port = priv->port.first_port;
5729 port_set_stp_state(hw, port, STP_STATE_DISABLED);
5730 port = 1 << port;
5731 if (sw->member & port) {
5732 sw->member &= ~port;
5733 bridge_change(hw);
5739 static void dev_set_multicast(struct dev_priv *priv, struct ksz_hw *hw,
5740 int multicast)
5742 if (multicast != priv->multicast) {
5743 u8 all_multi = hw->all_multi;
5745 if (multicast)
5746 ++hw->all_multi;
5747 else
5748 --hw->all_multi;
5749 priv->multicast = multicast;
5751 /* Turn on/off all multicast mode. */
5752 if (hw->all_multi <= 1 && all_multi <= 1)
5753 hw_set_multicast(hw, hw->all_multi);
5758 * netdev_set_rx_mode
5759 * @dev: Network device.
5761 * This routine is used to set multicast addresses or put the network device
5762 * into promiscuous mode.
5764 static void netdev_set_rx_mode(struct net_device *dev)
5766 struct dev_priv *priv = netdev_priv(dev);
5767 struct dev_info *hw_priv = priv->adapter;
5768 struct ksz_hw *hw = &hw_priv->hw;
5769 struct dev_mc_list *mc_ptr;
5770 int multicast = (dev->flags & IFF_ALLMULTI);
5772 dev_set_promiscuous(dev, priv, hw, (dev->flags & IFF_PROMISC));
5774 if (hw_priv->hw.dev_count > 1)
5775 multicast |= (dev->flags & IFF_MULTICAST);
5776 dev_set_multicast(priv, hw, multicast);
5778 /* Cannot use different hashes in multiple device interfaces mode. */
5779 if (hw_priv->hw.dev_count > 1)
5780 return;
5782 if ((dev->flags & IFF_MULTICAST) && !netdev_mc_empty(dev)) {
5783 int i = 0;
5785 /* List too big to support so turn on all multicast mode. */
5786 if (dev->mc_count > MAX_MULTICAST_LIST) {
5787 if (MAX_MULTICAST_LIST != hw->multi_list_size) {
5788 hw->multi_list_size = MAX_MULTICAST_LIST;
5789 ++hw->all_multi;
5790 hw_set_multicast(hw, hw->all_multi);
5792 return;
5795 netdev_for_each_mc_addr(mc_ptr, dev) {
5796 if (!(*mc_ptr->dmi_addr & 1))
5797 continue;
5798 if (i >= MAX_MULTICAST_LIST)
5799 break;
5800 memcpy(hw->multi_list[i++], mc_ptr->dmi_addr,
5801 MAC_ADDR_LEN);
5803 hw->multi_list_size = (u8) i;
5804 hw_set_grp_addr(hw);
5805 } else {
5806 if (MAX_MULTICAST_LIST == hw->multi_list_size) {
5807 --hw->all_multi;
5808 hw_set_multicast(hw, hw->all_multi);
5810 hw->multi_list_size = 0;
5811 hw_clr_multicast(hw);
5815 static int netdev_change_mtu(struct net_device *dev, int new_mtu)
5817 struct dev_priv *priv = netdev_priv(dev);
5818 struct dev_info *hw_priv = priv->adapter;
5819 struct ksz_hw *hw = &hw_priv->hw;
5820 int hw_mtu;
5822 if (netif_running(dev))
5823 return -EBUSY;
5825 /* Cannot use different MTU in multiple device interfaces mode. */
5826 if (hw->dev_count > 1)
5827 if (dev != hw_priv->dev)
5828 return 0;
5829 if (new_mtu < 60)
5830 return -EINVAL;
5832 if (dev->mtu != new_mtu) {
5833 hw_mtu = new_mtu + ETHERNET_HEADER_SIZE + 4;
5834 if (hw_mtu > MAX_RX_BUF_SIZE)
5835 return -EINVAL;
5836 if (hw_mtu > REGULAR_RX_BUF_SIZE) {
5837 hw->features |= RX_HUGE_FRAME;
5838 hw_mtu = MAX_RX_BUF_SIZE;
5839 } else {
5840 hw->features &= ~RX_HUGE_FRAME;
5841 hw_mtu = REGULAR_RX_BUF_SIZE;
5843 hw_mtu = (hw_mtu + 3) & ~3;
5844 hw_priv->mtu = hw_mtu;
5845 dev->mtu = new_mtu;
5847 return 0;
5851 * netdev_ioctl - I/O control processing
5852 * @dev: Network device.
5853 * @ifr: Interface request structure.
5854 * @cmd: I/O control code.
5856 * This function is used to process I/O control calls.
5858 * Return 0 to indicate success.
5860 static int netdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5862 struct dev_priv *priv = netdev_priv(dev);
5863 struct dev_info *hw_priv = priv->adapter;
5864 struct ksz_hw *hw = &hw_priv->hw;
5865 struct ksz_port *port = &priv->port;
5866 int rc;
5867 int result = 0;
5868 struct mii_ioctl_data *data = if_mii(ifr);
5870 if (down_interruptible(&priv->proc_sem))
5871 return -ERESTARTSYS;
5873 /* assume success */
5874 rc = 0;
5875 switch (cmd) {
5876 /* Get address of MII PHY in use. */
5877 case SIOCGMIIPHY:
5878 data->phy_id = priv->id;
5880 /* Fallthrough... */
5882 /* Read MII PHY register. */
5883 case SIOCGMIIREG:
5884 if (data->phy_id != priv->id || data->reg_num >= 6)
5885 result = -EIO;
5886 else
5887 hw_r_phy(hw, port->linked->port_id, data->reg_num,
5888 &data->val_out);
5889 break;
5891 /* Write MII PHY register. */
5892 case SIOCSMIIREG:
5893 if (!capable(CAP_NET_ADMIN))
5894 result = -EPERM;
5895 else if (data->phy_id != priv->id || data->reg_num >= 6)
5896 result = -EIO;
5897 else
5898 hw_w_phy(hw, port->linked->port_id, data->reg_num,
5899 data->val_in);
5900 break;
5902 default:
5903 result = -EOPNOTSUPP;
5906 up(&priv->proc_sem);
5908 return result;
5912 * MII support
5916 * mdio_read - read PHY register
5917 * @dev: Network device.
5918 * @phy_id: The PHY id.
5919 * @reg_num: The register number.
5921 * This function returns the PHY register value.
5923 * Return the register value.
5925 static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
5927 struct dev_priv *priv = netdev_priv(dev);
5928 struct ksz_port *port = &priv->port;
5929 struct ksz_hw *hw = port->hw;
5930 u16 val_out;
5932 hw_r_phy(hw, port->linked->port_id, reg_num << 1, &val_out);
5933 return val_out;
5937 * mdio_write - set PHY register
5938 * @dev: Network device.
5939 * @phy_id: The PHY id.
5940 * @reg_num: The register number.
5941 * @val: The register value.
5943 * This procedure sets the PHY register value.
5945 static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
5947 struct dev_priv *priv = netdev_priv(dev);
5948 struct ksz_port *port = &priv->port;
5949 struct ksz_hw *hw = port->hw;
5950 int i;
5951 int pi;
5953 for (i = 0, pi = port->first_port; i < port->port_cnt; i++, pi++)
5954 hw_w_phy(hw, pi, reg_num << 1, val);
5958 * ethtool support
5961 #define EEPROM_SIZE 0x40
5963 static u16 eeprom_data[EEPROM_SIZE] = { 0 };
5965 #define ADVERTISED_ALL \
5966 (ADVERTISED_10baseT_Half | \
5967 ADVERTISED_10baseT_Full | \
5968 ADVERTISED_100baseT_Half | \
5969 ADVERTISED_100baseT_Full)
5971 /* These functions use the MII functions in mii.c. */
5974 * netdev_get_settings - get network device settings
5975 * @dev: Network device.
5976 * @cmd: Ethtool command.
5978 * This function queries the PHY and returns its state in the ethtool command.
5980 * Return 0 if successful; otherwise an error code.
5982 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
5984 struct dev_priv *priv = netdev_priv(dev);
5985 struct dev_info *hw_priv = priv->adapter;
5987 mutex_lock(&hw_priv->lock);
5988 mii_ethtool_gset(&priv->mii_if, cmd);
5989 cmd->advertising |= SUPPORTED_TP;
5990 mutex_unlock(&hw_priv->lock);
5992 /* Save advertised settings for workaround in next function. */
5993 priv->advertising = cmd->advertising;
5994 return 0;
5998 * netdev_set_settings - set network device settings
5999 * @dev: Network device.
6000 * @cmd: Ethtool command.
6002 * This function sets the PHY according to the ethtool command.
6004 * Return 0 if successful; otherwise an error code.
6006 static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
6008 struct dev_priv *priv = netdev_priv(dev);
6009 struct dev_info *hw_priv = priv->adapter;
6010 struct ksz_port *port = &priv->port;
6011 int rc;
6014 * ethtool utility does not change advertised setting if auto
6015 * negotiation is not specified explicitly.
6017 if (cmd->autoneg && priv->advertising == cmd->advertising) {
6018 cmd->advertising |= ADVERTISED_ALL;
6019 if (10 == cmd->speed)
6020 cmd->advertising &=
6021 ~(ADVERTISED_100baseT_Full |
6022 ADVERTISED_100baseT_Half);
6023 else if (100 == cmd->speed)
6024 cmd->advertising &=
6025 ~(ADVERTISED_10baseT_Full |
6026 ADVERTISED_10baseT_Half);
6027 if (0 == cmd->duplex)
6028 cmd->advertising &=
6029 ~(ADVERTISED_100baseT_Full |
6030 ADVERTISED_10baseT_Full);
6031 else if (1 == cmd->duplex)
6032 cmd->advertising &=
6033 ~(ADVERTISED_100baseT_Half |
6034 ADVERTISED_10baseT_Half);
6036 mutex_lock(&hw_priv->lock);
6037 if (cmd->autoneg &&
6038 (cmd->advertising & ADVERTISED_ALL) ==
6039 ADVERTISED_ALL) {
6040 port->duplex = 0;
6041 port->speed = 0;
6042 port->force_link = 0;
6043 } else {
6044 port->duplex = cmd->duplex + 1;
6045 if (cmd->speed != 1000)
6046 port->speed = cmd->speed;
6047 if (cmd->autoneg)
6048 port->force_link = 0;
6049 else
6050 port->force_link = 1;
6052 rc = mii_ethtool_sset(&priv->mii_if, cmd);
6053 mutex_unlock(&hw_priv->lock);
6054 return rc;
6058 * netdev_nway_reset - restart auto-negotiation
6059 * @dev: Network device.
6061 * This function restarts the PHY for auto-negotiation.
6063 * Return 0 if successful; otherwise an error code.
6065 static int netdev_nway_reset(struct net_device *dev)
6067 struct dev_priv *priv = netdev_priv(dev);
6068 struct dev_info *hw_priv = priv->adapter;
6069 int rc;
6071 mutex_lock(&hw_priv->lock);
6072 rc = mii_nway_restart(&priv->mii_if);
6073 mutex_unlock(&hw_priv->lock);
6074 return rc;
6078 * netdev_get_link - get network device link status
6079 * @dev: Network device.
6081 * This function gets the link status from the PHY.
6083 * Return true if PHY is linked and false otherwise.
6085 static u32 netdev_get_link(struct net_device *dev)
6087 struct dev_priv *priv = netdev_priv(dev);
6088 int rc;
6090 rc = mii_link_ok(&priv->mii_if);
6091 return rc;
6095 * netdev_get_drvinfo - get network driver information
6096 * @dev: Network device.
6097 * @info: Ethtool driver info data structure.
6099 * This procedure returns the driver information.
6101 static void netdev_get_drvinfo(struct net_device *dev,
6102 struct ethtool_drvinfo *info)
6104 struct dev_priv *priv = netdev_priv(dev);
6105 struct dev_info *hw_priv = priv->adapter;
6107 strcpy(info->driver, DRV_NAME);
6108 strcpy(info->version, DRV_VERSION);
6109 strcpy(info->bus_info, pci_name(hw_priv->pdev));
6113 * netdev_get_regs_len - get length of register dump
6114 * @dev: Network device.
6116 * This function returns the length of the register dump.
6118 * Return length of the register dump.
6120 static struct hw_regs {
6121 int start;
6122 int end;
6123 } hw_regs_range[] = {
6124 { KS_DMA_TX_CTRL, KS884X_INTERRUPTS_STATUS },
6125 { KS_ADD_ADDR_0_LO, KS_ADD_ADDR_F_HI },
6126 { KS884X_ADDR_0_OFFSET, KS8841_WOL_FRAME_BYTE2_OFFSET },
6127 { KS884X_SIDER_P, KS8842_SGCR7_P },
6128 { KS8842_MACAR1_P, KS8842_TOSR8_P },
6129 { KS884X_P1MBCR_P, KS8842_P3ERCR_P },
6130 { 0, 0 }
6133 static int netdev_get_regs_len(struct net_device *dev)
6135 struct hw_regs *range = hw_regs_range;
6136 int regs_len = 0x10 * sizeof(u32);
6138 while (range->end > range->start) {
6139 regs_len += (range->end - range->start + 3) / 4 * 4;
6140 range++;
6142 return regs_len;
6146 * netdev_get_regs - get register dump
6147 * @dev: Network device.
6148 * @regs: Ethtool registers data structure.
6149 * @ptr: Buffer to store the register values.
6151 * This procedure dumps the register values in the provided buffer.
6153 static void netdev_get_regs(struct net_device *dev, struct ethtool_regs *regs,
6154 void *ptr)
6156 struct dev_priv *priv = netdev_priv(dev);
6157 struct dev_info *hw_priv = priv->adapter;
6158 struct ksz_hw *hw = &hw_priv->hw;
6159 int *buf = (int *) ptr;
6160 struct hw_regs *range = hw_regs_range;
6161 int len;
6163 mutex_lock(&hw_priv->lock);
6164 regs->version = 0;
6165 for (len = 0; len < 0x40; len += 4) {
6166 pci_read_config_dword(hw_priv->pdev, len, buf);
6167 buf++;
6169 while (range->end > range->start) {
6170 for (len = range->start; len < range->end; len += 4) {
6171 *buf = readl(hw->io + len);
6172 buf++;
6174 range++;
6176 mutex_unlock(&hw_priv->lock);
6179 #define WOL_SUPPORT \
6180 (WAKE_PHY | WAKE_MAGIC | \
6181 WAKE_UCAST | WAKE_MCAST | \
6182 WAKE_BCAST | WAKE_ARP)
6185 * netdev_get_wol - get Wake-on-LAN support
6186 * @dev: Network device.
6187 * @wol: Ethtool Wake-on-LAN data structure.
6189 * This procedure returns Wake-on-LAN support.
6191 static void netdev_get_wol(struct net_device *dev,
6192 struct ethtool_wolinfo *wol)
6194 struct dev_priv *priv = netdev_priv(dev);
6195 struct dev_info *hw_priv = priv->adapter;
6197 wol->supported = hw_priv->wol_support;
6198 wol->wolopts = hw_priv->wol_enable;
6199 memset(&wol->sopass, 0, sizeof(wol->sopass));
6203 * netdev_set_wol - set Wake-on-LAN support
6204 * @dev: Network device.
6205 * @wol: Ethtool Wake-on-LAN data structure.
6207 * This function sets Wake-on-LAN support.
6209 * Return 0 if successful; otherwise an error code.
6211 static int netdev_set_wol(struct net_device *dev,
6212 struct ethtool_wolinfo *wol)
6214 struct dev_priv *priv = netdev_priv(dev);
6215 struct dev_info *hw_priv = priv->adapter;
6217 /* Need to find a way to retrieve the device IP address. */
6218 u8 net_addr[] = { 192, 168, 1, 1 };
6220 if (wol->wolopts & ~hw_priv->wol_support)
6221 return -EINVAL;
6223 hw_priv->wol_enable = wol->wolopts;
6225 /* Link wakeup cannot really be disabled. */
6226 if (wol->wolopts)
6227 hw_priv->wol_enable |= WAKE_PHY;
6228 hw_enable_wol(&hw_priv->hw, hw_priv->wol_enable, net_addr);
6229 return 0;
6233 * netdev_get_msglevel - get debug message level
6234 * @dev: Network device.
6236 * This function returns current debug message level.
6238 * Return current debug message flags.
6240 static u32 netdev_get_msglevel(struct net_device *dev)
6242 struct dev_priv *priv = netdev_priv(dev);
6244 return priv->msg_enable;
6248 * netdev_set_msglevel - set debug message level
6249 * @dev: Network device.
6250 * @value: Debug message flags.
6252 * This procedure sets debug message level.
6254 static void netdev_set_msglevel(struct net_device *dev, u32 value)
6256 struct dev_priv *priv = netdev_priv(dev);
6258 priv->msg_enable = value;
6262 * netdev_get_eeprom_len - get EEPROM length
6263 * @dev: Network device.
6265 * This function returns the length of the EEPROM.
6267 * Return length of the EEPROM.
6269 static int netdev_get_eeprom_len(struct net_device *dev)
6271 return EEPROM_SIZE * 2;
6275 * netdev_get_eeprom - get EEPROM data
6276 * @dev: Network device.
6277 * @eeprom: Ethtool EEPROM data structure.
6278 * @data: Buffer to store the EEPROM data.
6280 * This function dumps the EEPROM data in the provided buffer.
6282 * Return 0 if successful; otherwise an error code.
6284 #define EEPROM_MAGIC 0x10A18842
6286 static int netdev_get_eeprom(struct net_device *dev,
6287 struct ethtool_eeprom *eeprom, u8 *data)
6289 struct dev_priv *priv = netdev_priv(dev);
6290 struct dev_info *hw_priv = priv->adapter;
6291 u8 *eeprom_byte = (u8 *) eeprom_data;
6292 int i;
6293 int len;
6295 len = (eeprom->offset + eeprom->len + 1) / 2;
6296 for (i = eeprom->offset / 2; i < len; i++)
6297 eeprom_data[i] = eeprom_read(&hw_priv->hw, i);
6298 eeprom->magic = EEPROM_MAGIC;
6299 memcpy(data, &eeprom_byte[eeprom->offset], eeprom->len);
6301 return 0;
6305 * netdev_set_eeprom - write EEPROM data
6306 * @dev: Network device.
6307 * @eeprom: Ethtool EEPROM data structure.
6308 * @data: Data buffer.
6310 * This function modifies the EEPROM data one byte at a time.
6312 * Return 0 if successful; otherwise an error code.
6314 static int netdev_set_eeprom(struct net_device *dev,
6315 struct ethtool_eeprom *eeprom, u8 *data)
6317 struct dev_priv *priv = netdev_priv(dev);
6318 struct dev_info *hw_priv = priv->adapter;
6319 u16 eeprom_word[EEPROM_SIZE];
6320 u8 *eeprom_byte = (u8 *) eeprom_word;
6321 int i;
6322 int len;
6324 if (eeprom->magic != EEPROM_MAGIC)
6325 return -EINVAL;
6327 len = (eeprom->offset + eeprom->len + 1) / 2;
6328 for (i = eeprom->offset / 2; i < len; i++)
6329 eeprom_data[i] = eeprom_read(&hw_priv->hw, i);
6330 memcpy(eeprom_word, eeprom_data, EEPROM_SIZE * 2);
6331 memcpy(&eeprom_byte[eeprom->offset], data, eeprom->len);
6332 for (i = 0; i < EEPROM_SIZE; i++)
6333 if (eeprom_word[i] != eeprom_data[i]) {
6334 eeprom_data[i] = eeprom_word[i];
6335 eeprom_write(&hw_priv->hw, i, eeprom_data[i]);
6338 return 0;
6342 * netdev_get_pauseparam - get flow control parameters
6343 * @dev: Network device.
6344 * @pause: Ethtool PAUSE settings data structure.
6346 * This procedure returns the PAUSE control flow settings.
6348 static void netdev_get_pauseparam(struct net_device *dev,
6349 struct ethtool_pauseparam *pause)
6351 struct dev_priv *priv = netdev_priv(dev);
6352 struct dev_info *hw_priv = priv->adapter;
6353 struct ksz_hw *hw = &hw_priv->hw;
6355 pause->autoneg = (hw->overrides & PAUSE_FLOW_CTRL) ? 0 : 1;
6356 if (!hw->ksz_switch) {
6357 pause->rx_pause =
6358 (hw->rx_cfg & DMA_RX_FLOW_ENABLE) ? 1 : 0;
6359 pause->tx_pause =
6360 (hw->tx_cfg & DMA_TX_FLOW_ENABLE) ? 1 : 0;
6361 } else {
6362 pause->rx_pause =
6363 (sw_chk(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6364 SWITCH_RX_FLOW_CTRL)) ? 1 : 0;
6365 pause->tx_pause =
6366 (sw_chk(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6367 SWITCH_TX_FLOW_CTRL)) ? 1 : 0;
6372 * netdev_set_pauseparam - set flow control parameters
6373 * @dev: Network device.
6374 * @pause: Ethtool PAUSE settings data structure.
6376 * This function sets the PAUSE control flow settings.
6377 * Not implemented yet.
6379 * Return 0 if successful; otherwise an error code.
6381 static int netdev_set_pauseparam(struct net_device *dev,
6382 struct ethtool_pauseparam *pause)
6384 struct dev_priv *priv = netdev_priv(dev);
6385 struct dev_info *hw_priv = priv->adapter;
6386 struct ksz_hw *hw = &hw_priv->hw;
6387 struct ksz_port *port = &priv->port;
6389 mutex_lock(&hw_priv->lock);
6390 if (pause->autoneg) {
6391 if (!pause->rx_pause && !pause->tx_pause)
6392 port->flow_ctrl = PHY_NO_FLOW_CTRL;
6393 else
6394 port->flow_ctrl = PHY_FLOW_CTRL;
6395 hw->overrides &= ~PAUSE_FLOW_CTRL;
6396 port->force_link = 0;
6397 if (hw->ksz_switch) {
6398 sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6399 SWITCH_RX_FLOW_CTRL, 1);
6400 sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6401 SWITCH_TX_FLOW_CTRL, 1);
6403 port_set_link_speed(port);
6404 } else {
6405 hw->overrides |= PAUSE_FLOW_CTRL;
6406 if (hw->ksz_switch) {
6407 sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6408 SWITCH_RX_FLOW_CTRL, pause->rx_pause);
6409 sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
6410 SWITCH_TX_FLOW_CTRL, pause->tx_pause);
6411 } else
6412 set_flow_ctrl(hw, pause->rx_pause, pause->tx_pause);
6414 mutex_unlock(&hw_priv->lock);
6416 return 0;
6420 * netdev_get_ringparam - get tx/rx ring parameters
6421 * @dev: Network device.
6422 * @pause: Ethtool RING settings data structure.
6424 * This procedure returns the TX/RX ring settings.
6426 static void netdev_get_ringparam(struct net_device *dev,
6427 struct ethtool_ringparam *ring)
6429 struct dev_priv *priv = netdev_priv(dev);
6430 struct dev_info *hw_priv = priv->adapter;
6431 struct ksz_hw *hw = &hw_priv->hw;
6433 ring->tx_max_pending = (1 << 9);
6434 ring->tx_pending = hw->tx_desc_info.alloc;
6435 ring->rx_max_pending = (1 << 9);
6436 ring->rx_pending = hw->rx_desc_info.alloc;
6439 #define STATS_LEN (TOTAL_PORT_COUNTER_NUM)
6441 static struct {
6442 char string[ETH_GSTRING_LEN];
6443 } ethtool_stats_keys[STATS_LEN] = {
6444 { "rx_lo_priority_octets" },
6445 { "rx_hi_priority_octets" },
6446 { "rx_undersize_packets" },
6447 { "rx_fragments" },
6448 { "rx_oversize_packets" },
6449 { "rx_jabbers" },
6450 { "rx_symbol_errors" },
6451 { "rx_crc_errors" },
6452 { "rx_align_errors" },
6453 { "rx_mac_ctrl_packets" },
6454 { "rx_pause_packets" },
6455 { "rx_bcast_packets" },
6456 { "rx_mcast_packets" },
6457 { "rx_ucast_packets" },
6458 { "rx_64_or_less_octet_packets" },
6459 { "rx_65_to_127_octet_packets" },
6460 { "rx_128_to_255_octet_packets" },
6461 { "rx_256_to_511_octet_packets" },
6462 { "rx_512_to_1023_octet_packets" },
6463 { "rx_1024_to_1522_octet_packets" },
6465 { "tx_lo_priority_octets" },
6466 { "tx_hi_priority_octets" },
6467 { "tx_late_collisions" },
6468 { "tx_pause_packets" },
6469 { "tx_bcast_packets" },
6470 { "tx_mcast_packets" },
6471 { "tx_ucast_packets" },
6472 { "tx_deferred" },
6473 { "tx_total_collisions" },
6474 { "tx_excessive_collisions" },
6475 { "tx_single_collisions" },
6476 { "tx_mult_collisions" },
6478 { "rx_discards" },
6479 { "tx_discards" },
6483 * netdev_get_strings - get statistics identity strings
6484 * @dev: Network device.
6485 * @stringset: String set identifier.
6486 * @buf: Buffer to store the strings.
6488 * This procedure returns the strings used to identify the statistics.
6490 static void netdev_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
6492 struct dev_priv *priv = netdev_priv(dev);
6493 struct dev_info *hw_priv = priv->adapter;
6494 struct ksz_hw *hw = &hw_priv->hw;
6496 if (ETH_SS_STATS == stringset)
6497 memcpy(buf, &ethtool_stats_keys,
6498 ETH_GSTRING_LEN * hw->mib_cnt);
6502 * netdev_get_sset_count - get statistics size
6503 * @dev: Network device.
6504 * @sset: The statistics set number.
6506 * This function returns the size of the statistics to be reported.
6508 * Return size of the statistics to be reported.
6510 static int netdev_get_sset_count(struct net_device *dev, int sset)
6512 struct dev_priv *priv = netdev_priv(dev);
6513 struct dev_info *hw_priv = priv->adapter;
6514 struct ksz_hw *hw = &hw_priv->hw;
6516 switch (sset) {
6517 case ETH_SS_STATS:
6518 return hw->mib_cnt;
6519 default:
6520 return -EOPNOTSUPP;
6525 * netdev_get_ethtool_stats - get network device statistics
6526 * @dev: Network device.
6527 * @stats: Ethtool statistics data structure.
6528 * @data: Buffer to store the statistics.
6530 * This procedure returns the statistics.
6532 static void netdev_get_ethtool_stats(struct net_device *dev,
6533 struct ethtool_stats *stats, u64 *data)
6535 struct dev_priv *priv = netdev_priv(dev);
6536 struct dev_info *hw_priv = priv->adapter;
6537 struct ksz_hw *hw = &hw_priv->hw;
6538 struct ksz_port *port = &priv->port;
6539 int n_stats = stats->n_stats;
6540 int i;
6541 int n;
6542 int p;
6543 int rc;
6544 u64 counter[TOTAL_PORT_COUNTER_NUM];
6546 mutex_lock(&hw_priv->lock);
6547 n = SWITCH_PORT_NUM;
6548 for (i = 0, p = port->first_port; i < port->mib_port_cnt; i++, p++) {
6549 if (media_connected == hw->port_mib[p].state) {
6550 hw_priv->counter[p].read = 1;
6552 /* Remember first port that requests read. */
6553 if (n == SWITCH_PORT_NUM)
6554 n = p;
6557 mutex_unlock(&hw_priv->lock);
6559 if (n < SWITCH_PORT_NUM)
6560 schedule_work(&hw_priv->mib_read);
6562 if (1 == port->mib_port_cnt && n < SWITCH_PORT_NUM) {
6563 p = n;
6564 rc = wait_event_interruptible_timeout(
6565 hw_priv->counter[p].counter,
6566 2 == hw_priv->counter[p].read,
6567 HZ * 1);
6568 } else
6569 for (i = 0, p = n; i < port->mib_port_cnt - n; i++, p++) {
6570 if (0 == i) {
6571 rc = wait_event_interruptible_timeout(
6572 hw_priv->counter[p].counter,
6573 2 == hw_priv->counter[p].read,
6574 HZ * 2);
6575 } else if (hw->port_mib[p].cnt_ptr) {
6576 rc = wait_event_interruptible_timeout(
6577 hw_priv->counter[p].counter,
6578 2 == hw_priv->counter[p].read,
6579 HZ * 1);
6583 get_mib_counters(hw, port->first_port, port->mib_port_cnt, counter);
6584 n = hw->mib_cnt;
6585 if (n > n_stats)
6586 n = n_stats;
6587 n_stats -= n;
6588 for (i = 0; i < n; i++)
6589 *data++ = counter[i];
6593 * netdev_get_rx_csum - get receive checksum support
6594 * @dev: Network device.
6596 * This function gets receive checksum support setting.
6598 * Return true if receive checksum is enabled; false otherwise.
6600 static u32 netdev_get_rx_csum(struct net_device *dev)
6602 struct dev_priv *priv = netdev_priv(dev);
6603 struct dev_info *hw_priv = priv->adapter;
6604 struct ksz_hw *hw = &hw_priv->hw;
6606 return hw->rx_cfg &
6607 (DMA_RX_CSUM_UDP |
6608 DMA_RX_CSUM_TCP |
6609 DMA_RX_CSUM_IP);
6613 * netdev_set_rx_csum - set receive checksum support
6614 * @dev: Network device.
6615 * @data: Zero to disable receive checksum support.
6617 * This function sets receive checksum support setting.
6619 * Return 0 if successful; otherwise an error code.
6621 static int netdev_set_rx_csum(struct net_device *dev, u32 data)
6623 struct dev_priv *priv = netdev_priv(dev);
6624 struct dev_info *hw_priv = priv->adapter;
6625 struct ksz_hw *hw = &hw_priv->hw;
6626 u32 new_setting = hw->rx_cfg;
6628 if (data)
6629 new_setting |=
6630 (DMA_RX_CSUM_UDP | DMA_RX_CSUM_TCP |
6631 DMA_RX_CSUM_IP);
6632 else
6633 new_setting &=
6634 ~(DMA_RX_CSUM_UDP | DMA_RX_CSUM_TCP |
6635 DMA_RX_CSUM_IP);
6636 new_setting &= ~DMA_RX_CSUM_UDP;
6637 mutex_lock(&hw_priv->lock);
6638 if (new_setting != hw->rx_cfg) {
6639 hw->rx_cfg = new_setting;
6640 if (hw->enabled)
6641 writel(hw->rx_cfg, hw->io + KS_DMA_RX_CTRL);
6643 mutex_unlock(&hw_priv->lock);
6644 return 0;
6647 static struct ethtool_ops netdev_ethtool_ops = {
6648 .get_settings = netdev_get_settings,
6649 .set_settings = netdev_set_settings,
6650 .nway_reset = netdev_nway_reset,
6651 .get_link = netdev_get_link,
6652 .get_drvinfo = netdev_get_drvinfo,
6653 .get_regs_len = netdev_get_regs_len,
6654 .get_regs = netdev_get_regs,
6655 .get_wol = netdev_get_wol,
6656 .set_wol = netdev_set_wol,
6657 .get_msglevel = netdev_get_msglevel,
6658 .set_msglevel = netdev_set_msglevel,
6659 .get_eeprom_len = netdev_get_eeprom_len,
6660 .get_eeprom = netdev_get_eeprom,
6661 .set_eeprom = netdev_set_eeprom,
6662 .get_pauseparam = netdev_get_pauseparam,
6663 .set_pauseparam = netdev_set_pauseparam,
6664 .get_ringparam = netdev_get_ringparam,
6665 .get_strings = netdev_get_strings,
6666 .get_sset_count = netdev_get_sset_count,
6667 .get_ethtool_stats = netdev_get_ethtool_stats,
6668 .get_rx_csum = netdev_get_rx_csum,
6669 .set_rx_csum = netdev_set_rx_csum,
6670 .get_tx_csum = ethtool_op_get_tx_csum,
6671 .set_tx_csum = ethtool_op_set_tx_csum,
6672 .get_sg = ethtool_op_get_sg,
6673 .set_sg = ethtool_op_set_sg,
6677 * Hardware monitoring
6680 static void update_link(struct net_device *dev, struct dev_priv *priv,
6681 struct ksz_port *port)
6683 if (priv->media_state != port->linked->state) {
6684 priv->media_state = port->linked->state;
6685 if (netif_running(dev)) {
6686 if (media_connected == priv->media_state)
6687 netif_carrier_on(dev);
6688 else
6689 netif_carrier_off(dev);
6690 if (netif_msg_link(priv))
6691 printk(KERN_INFO "%s link %s\n", dev->name,
6692 (media_connected == priv->media_state ?
6693 "on" : "off"));
6698 static void mib_read_work(struct work_struct *work)
6700 struct dev_info *hw_priv =
6701 container_of(work, struct dev_info, mib_read);
6702 struct ksz_hw *hw = &hw_priv->hw;
6703 struct ksz_port_mib *mib;
6704 int i;
6706 next_jiffies = jiffies;
6707 for (i = 0; i < hw->mib_port_cnt; i++) {
6708 mib = &hw->port_mib[i];
6710 /* Reading MIB counters or requested to read. */
6711 if (mib->cnt_ptr || 1 == hw_priv->counter[i].read) {
6713 /* Need to process receive interrupt. */
6714 if (port_r_cnt(hw, i))
6715 break;
6716 hw_priv->counter[i].read = 0;
6718 /* Finish reading counters. */
6719 if (0 == mib->cnt_ptr) {
6720 hw_priv->counter[i].read = 2;
6721 wake_up_interruptible(
6722 &hw_priv->counter[i].counter);
6724 } else if (jiffies >= hw_priv->counter[i].time) {
6725 /* Only read MIB counters when the port is connected. */
6726 if (media_connected == mib->state)
6727 hw_priv->counter[i].read = 1;
6728 next_jiffies += HZ * 1 * hw->mib_port_cnt;
6729 hw_priv->counter[i].time = next_jiffies;
6731 /* Port is just disconnected. */
6732 } else if (mib->link_down) {
6733 mib->link_down = 0;
6735 /* Read counters one last time after link is lost. */
6736 hw_priv->counter[i].read = 1;
6741 static void mib_monitor(unsigned long ptr)
6743 struct dev_info *hw_priv = (struct dev_info *) ptr;
6745 mib_read_work(&hw_priv->mib_read);
6747 /* This is used to verify Wake-on-LAN is working. */
6748 if (hw_priv->pme_wait) {
6749 if (hw_priv->pme_wait <= jiffies) {
6750 hw_clr_wol_pme_status(&hw_priv->hw);
6751 hw_priv->pme_wait = 0;
6753 } else if (hw_chk_wol_pme_status(&hw_priv->hw)) {
6755 /* PME is asserted. Wait 2 seconds to clear it. */
6756 hw_priv->pme_wait = jiffies + HZ * 2;
6759 ksz_update_timer(&hw_priv->mib_timer_info);
6763 * dev_monitor - periodic monitoring
6764 * @ptr: Network device pointer.
6766 * This routine is run in a kernel timer to monitor the network device.
6768 static void dev_monitor(unsigned long ptr)
6770 struct net_device *dev = (struct net_device *) ptr;
6771 struct dev_priv *priv = netdev_priv(dev);
6772 struct dev_info *hw_priv = priv->adapter;
6773 struct ksz_hw *hw = &hw_priv->hw;
6774 struct ksz_port *port = &priv->port;
6776 if (!(hw->features & LINK_INT_WORKING))
6777 port_get_link_speed(port);
6778 update_link(dev, priv, port);
6780 ksz_update_timer(&priv->monitor_timer_info);
6784 * Linux network device interface functions
6787 /* Driver exported variables */
6789 static int msg_enable;
6791 static char *macaddr = ":";
6792 static char *mac1addr = ":";
6795 * This enables multiple network device mode for KSZ8842, which contains a
6796 * switch with two physical ports. Some users like to take control of the
6797 * ports for running Spanning Tree Protocol. The driver will create an
6798 * additional eth? device for the other port.
6800 * Some limitations are the network devices cannot have different MTU and
6801 * multicast hash tables.
6803 static int multi_dev;
6806 * As most users select multiple network device mode to use Spanning Tree
6807 * Protocol, this enables a feature in which most unicast and multicast packets
6808 * are forwarded inside the switch and not passed to the host. Only packets
6809 * that need the host's attention are passed to it. This prevents the host
6810 * wasting CPU time to examine each and every incoming packets and do the
6811 * forwarding itself.
6813 * As the hack requires the private bridge header, the driver cannot compile
6814 * with just the kernel headers.
6816 * Enabling STP support also turns on multiple network device mode.
6818 static int stp;
6821 * This enables fast aging in the KSZ8842 switch. Not sure what situation
6822 * needs that. However, fast aging is used to flush the dynamic MAC table when
6823 * STP suport is enabled.
6825 static int fast_aging;
6828 * netdev_init - initalize network device.
6829 * @dev: Network device.
6831 * This function initializes the network device.
6833 * Return 0 if successful; otherwise an error code indicating failure.
6835 static int __init netdev_init(struct net_device *dev)
6837 struct dev_priv *priv = netdev_priv(dev);
6839 /* 500 ms timeout */
6840 ksz_init_timer(&priv->monitor_timer_info, 500 * HZ / 1000,
6841 dev_monitor, dev);
6843 /* 500 ms timeout */
6844 dev->watchdog_timeo = HZ / 2;
6846 dev->features |= NETIF_F_IP_CSUM;
6849 * Hardware does not really support IPv6 checksum generation, but
6850 * driver actually runs faster with this on. Refer IPV6_CSUM_GEN_HACK.
6852 dev->features |= NETIF_F_IPV6_CSUM;
6853 dev->features |= NETIF_F_SG;
6855 sema_init(&priv->proc_sem, 1);
6857 priv->mii_if.phy_id_mask = 0x1;
6858 priv->mii_if.reg_num_mask = 0x7;
6859 priv->mii_if.dev = dev;
6860 priv->mii_if.mdio_read = mdio_read;
6861 priv->mii_if.mdio_write = mdio_write;
6862 priv->mii_if.phy_id = priv->port.first_port + 1;
6864 priv->msg_enable = netif_msg_init(msg_enable,
6865 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK));
6867 return 0;
6870 static const struct net_device_ops netdev_ops = {
6871 .ndo_init = netdev_init,
6872 .ndo_open = netdev_open,
6873 .ndo_stop = netdev_close,
6874 .ndo_get_stats = netdev_query_statistics,
6875 .ndo_start_xmit = netdev_tx,
6876 .ndo_tx_timeout = netdev_tx_timeout,
6877 .ndo_change_mtu = netdev_change_mtu,
6878 .ndo_set_mac_address = netdev_set_mac_address,
6879 .ndo_do_ioctl = netdev_ioctl,
6880 .ndo_set_rx_mode = netdev_set_rx_mode,
6881 #ifdef CONFIG_NET_POLL_CONTROLLER
6882 .ndo_poll_controller = netdev_netpoll,
6883 #endif
6886 static void netdev_free(struct net_device *dev)
6888 if (dev->watchdog_timeo)
6889 unregister_netdev(dev);
6891 free_netdev(dev);
6894 struct platform_info {
6895 struct dev_info dev_info;
6896 struct net_device *netdev[SWITCH_PORT_NUM];
6899 static int net_device_present;
6901 static void get_mac_addr(struct dev_info *hw_priv, u8 *macaddr, int port)
6903 int i;
6904 int j;
6905 int got_num;
6906 int num;
6908 i = j = num = got_num = 0;
6909 while (j < MAC_ADDR_LEN) {
6910 if (macaddr[i]) {
6911 got_num = 1;
6912 if ('0' <= macaddr[i] && macaddr[i] <= '9')
6913 num = num * 16 + macaddr[i] - '0';
6914 else if ('A' <= macaddr[i] && macaddr[i] <= 'F')
6915 num = num * 16 + 10 + macaddr[i] - 'A';
6916 else if ('a' <= macaddr[i] && macaddr[i] <= 'f')
6917 num = num * 16 + 10 + macaddr[i] - 'a';
6918 else if (':' == macaddr[i])
6919 got_num = 2;
6920 else
6921 break;
6922 } else if (got_num)
6923 got_num = 2;
6924 else
6925 break;
6926 if (2 == got_num) {
6927 if (MAIN_PORT == port) {
6928 hw_priv->hw.override_addr[j++] = (u8) num;
6929 hw_priv->hw.override_addr[5] +=
6930 hw_priv->hw.id;
6931 } else {
6932 hw_priv->hw.ksz_switch->other_addr[j++] =
6933 (u8) num;
6934 hw_priv->hw.ksz_switch->other_addr[5] +=
6935 hw_priv->hw.id;
6937 num = got_num = 0;
6939 i++;
6941 if (MAC_ADDR_LEN == j) {
6942 if (MAIN_PORT == port)
6943 hw_priv->hw.mac_override = 1;
6947 #define KS884X_DMA_MASK (~0x0UL)
6949 static void read_other_addr(struct ksz_hw *hw)
6951 int i;
6952 u16 data[3];
6953 struct ksz_switch *sw = hw->ksz_switch;
6955 for (i = 0; i < 3; i++)
6956 data[i] = eeprom_read(hw, i + EEPROM_DATA_OTHER_MAC_ADDR);
6957 if ((data[0] || data[1] || data[2]) && data[0] != 0xffff) {
6958 sw->other_addr[5] = (u8) data[0];
6959 sw->other_addr[4] = (u8)(data[0] >> 8);
6960 sw->other_addr[3] = (u8) data[1];
6961 sw->other_addr[2] = (u8)(data[1] >> 8);
6962 sw->other_addr[1] = (u8) data[2];
6963 sw->other_addr[0] = (u8)(data[2] >> 8);
6967 #ifndef PCI_VENDOR_ID_MICREL_KS
6968 #define PCI_VENDOR_ID_MICREL_KS 0x16c6
6969 #endif
6971 static int __init pcidev_init(struct pci_dev *pdev,
6972 const struct pci_device_id *id)
6974 struct net_device *dev;
6975 struct dev_priv *priv;
6976 struct dev_info *hw_priv;
6977 struct ksz_hw *hw;
6978 struct platform_info *info;
6979 struct ksz_port *port;
6980 unsigned long reg_base;
6981 unsigned long reg_len;
6982 int cnt;
6983 int i;
6984 int mib_port_count;
6985 int pi;
6986 int port_count;
6987 int result;
6988 char banner[80];
6989 struct ksz_switch *sw = NULL;
6991 result = pci_enable_device(pdev);
6992 if (result)
6993 return result;
6995 result = -ENODEV;
6997 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) ||
6998 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
6999 return result;
7001 reg_base = pci_resource_start(pdev, 0);
7002 reg_len = pci_resource_len(pdev, 0);
7003 if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0)
7004 return result;
7006 if (!request_mem_region(reg_base, reg_len, DRV_NAME))
7007 return result;
7008 pci_set_master(pdev);
7010 result = -ENOMEM;
7012 info = kmalloc(sizeof(struct platform_info), GFP_KERNEL);
7013 if (!info)
7014 goto pcidev_init_dev_err;
7015 memset(info, 0, sizeof(struct platform_info));
7017 hw_priv = &info->dev_info;
7018 hw_priv->pdev = pdev;
7020 hw = &hw_priv->hw;
7022 hw->io = ioremap(reg_base, reg_len);
7023 if (!hw->io)
7024 goto pcidev_init_io_err;
7026 cnt = hw_init(hw);
7027 if (!cnt) {
7028 if (msg_enable & NETIF_MSG_PROBE)
7029 printk(KERN_ALERT "chip not detected\n");
7030 result = -ENODEV;
7031 goto pcidev_init_alloc_err;
7034 sprintf(banner, "%s\n", version);
7035 banner[13] = cnt + '0';
7036 ks_info(hw_priv, "%s", banner);
7037 ks_dbg(hw_priv, "Mem = %p; IRQ = %d\n", hw->io, pdev->irq);
7039 /* Assume device is KSZ8841. */
7040 hw->dev_count = 1;
7041 port_count = 1;
7042 mib_port_count = 1;
7043 hw->addr_list_size = 0;
7044 hw->mib_cnt = PORT_COUNTER_NUM;
7045 hw->mib_port_cnt = 1;
7047 /* KSZ8842 has a switch with multiple ports. */
7048 if (2 == cnt) {
7049 if (fast_aging)
7050 hw->overrides |= FAST_AGING;
7052 hw->mib_cnt = TOTAL_PORT_COUNTER_NUM;
7054 /* Multiple network device interfaces are required. */
7055 if (multi_dev) {
7056 hw->dev_count = SWITCH_PORT_NUM;
7057 hw->addr_list_size = SWITCH_PORT_NUM - 1;
7060 /* Single network device has multiple ports. */
7061 if (1 == hw->dev_count) {
7062 port_count = SWITCH_PORT_NUM;
7063 mib_port_count = SWITCH_PORT_NUM;
7065 hw->mib_port_cnt = TOTAL_PORT_NUM;
7066 hw->ksz_switch = kmalloc(sizeof(struct ksz_switch), GFP_KERNEL);
7067 if (!hw->ksz_switch)
7068 goto pcidev_init_alloc_err;
7069 memset(hw->ksz_switch, 0, sizeof(struct ksz_switch));
7071 sw = hw->ksz_switch;
7073 for (i = 0; i < hw->mib_port_cnt; i++)
7074 hw->port_mib[i].mib_start = 0;
7076 hw->parent = hw_priv;
7078 /* Default MTU is 1500. */
7079 hw_priv->mtu = (REGULAR_RX_BUF_SIZE + 3) & ~3;
7081 if (ksz_alloc_mem(hw_priv))
7082 goto pcidev_init_mem_err;
7084 hw_priv->hw.id = net_device_present;
7086 spin_lock_init(&hw_priv->hwlock);
7087 mutex_init(&hw_priv->lock);
7089 /* tasklet is enabled. */
7090 tasklet_init(&hw_priv->rx_tasklet, rx_proc_task,
7091 (unsigned long) hw_priv);
7092 tasklet_init(&hw_priv->tx_tasklet, tx_proc_task,
7093 (unsigned long) hw_priv);
7095 /* tasklet_enable will decrement the atomic counter. */
7096 tasklet_disable(&hw_priv->rx_tasklet);
7097 tasklet_disable(&hw_priv->tx_tasklet);
7099 for (i = 0; i < TOTAL_PORT_NUM; i++)
7100 init_waitqueue_head(&hw_priv->counter[i].counter);
7102 if (macaddr[0] != ':')
7103 get_mac_addr(hw_priv, macaddr, MAIN_PORT);
7105 /* Read MAC address and initialize override address if not overrided. */
7106 hw_read_addr(hw);
7108 /* Multiple device interfaces mode requires a second MAC address. */
7109 if (hw->dev_count > 1) {
7110 memcpy(sw->other_addr, hw->override_addr, MAC_ADDR_LEN);
7111 read_other_addr(hw);
7112 if (mac1addr[0] != ':')
7113 get_mac_addr(hw_priv, mac1addr, OTHER_PORT);
7116 hw_setup(hw);
7117 if (hw->ksz_switch)
7118 sw_setup(hw);
7119 else {
7120 hw_priv->wol_support = WOL_SUPPORT;
7121 hw_priv->wol_enable = 0;
7124 INIT_WORK(&hw_priv->mib_read, mib_read_work);
7126 /* 500 ms timeout */
7127 ksz_init_timer(&hw_priv->mib_timer_info, 500 * HZ / 1000,
7128 mib_monitor, hw_priv);
7130 for (i = 0; i < hw->dev_count; i++) {
7131 dev = alloc_etherdev(sizeof(struct dev_priv));
7132 if (!dev)
7133 goto pcidev_init_reg_err;
7134 info->netdev[i] = dev;
7136 priv = netdev_priv(dev);
7137 priv->adapter = hw_priv;
7138 priv->id = net_device_present++;
7140 port = &priv->port;
7141 port->port_cnt = port_count;
7142 port->mib_port_cnt = mib_port_count;
7143 port->first_port = i;
7144 port->flow_ctrl = PHY_FLOW_CTRL;
7146 port->hw = hw;
7147 port->linked = &hw->port_info[port->first_port];
7149 for (cnt = 0, pi = i; cnt < port_count; cnt++, pi++) {
7150 hw->port_info[pi].port_id = pi;
7151 hw->port_info[pi].pdev = dev;
7152 hw->port_info[pi].state = media_disconnected;
7155 dev->mem_start = (unsigned long) hw->io;
7156 dev->mem_end = dev->mem_start + reg_len - 1;
7157 dev->irq = pdev->irq;
7158 if (MAIN_PORT == i)
7159 memcpy(dev->dev_addr, hw_priv->hw.override_addr,
7160 MAC_ADDR_LEN);
7161 else {
7162 memcpy(dev->dev_addr, sw->other_addr,
7163 MAC_ADDR_LEN);
7164 if (!memcmp(sw->other_addr, hw->override_addr,
7165 MAC_ADDR_LEN))
7166 dev->dev_addr[5] += port->first_port;
7169 dev->netdev_ops = &netdev_ops;
7170 SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
7171 if (register_netdev(dev))
7172 goto pcidev_init_reg_err;
7173 port_set_power_saving(port, true);
7176 pci_dev_get(hw_priv->pdev);
7177 pci_set_drvdata(pdev, info);
7178 return 0;
7180 pcidev_init_reg_err:
7181 for (i = 0; i < hw->dev_count; i++) {
7182 if (info->netdev[i]) {
7183 netdev_free(info->netdev[i]);
7184 info->netdev[i] = NULL;
7188 pcidev_init_mem_err:
7189 ksz_free_mem(hw_priv);
7190 kfree(hw->ksz_switch);
7192 pcidev_init_alloc_err:
7193 iounmap(hw->io);
7195 pcidev_init_io_err:
7196 kfree(info);
7198 pcidev_init_dev_err:
7199 release_mem_region(reg_base, reg_len);
7201 return result;
7204 static void pcidev_exit(struct pci_dev *pdev)
7206 int i;
7207 struct platform_info *info = pci_get_drvdata(pdev);
7208 struct dev_info *hw_priv = &info->dev_info;
7210 pci_set_drvdata(pdev, NULL);
7212 release_mem_region(pci_resource_start(pdev, 0),
7213 pci_resource_len(pdev, 0));
7214 for (i = 0; i < hw_priv->hw.dev_count; i++) {
7215 if (info->netdev[i])
7216 netdev_free(info->netdev[i]);
7218 if (hw_priv->hw.io)
7219 iounmap(hw_priv->hw.io);
7220 ksz_free_mem(hw_priv);
7221 kfree(hw_priv->hw.ksz_switch);
7222 pci_dev_put(hw_priv->pdev);
7223 kfree(info);
7226 #ifdef CONFIG_PM
7227 static int pcidev_resume(struct pci_dev *pdev)
7229 int i;
7230 struct platform_info *info = pci_get_drvdata(pdev);
7231 struct dev_info *hw_priv = &info->dev_info;
7232 struct ksz_hw *hw = &hw_priv->hw;
7234 pci_set_power_state(pdev, PCI_D0);
7235 pci_restore_state(pdev);
7236 pci_enable_wake(pdev, PCI_D0, 0);
7238 if (hw_priv->wol_enable)
7239 hw_cfg_wol_pme(hw, 0);
7240 for (i = 0; i < hw->dev_count; i++) {
7241 if (info->netdev[i]) {
7242 struct net_device *dev = info->netdev[i];
7244 if (netif_running(dev)) {
7245 netdev_open(dev);
7246 netif_device_attach(dev);
7250 return 0;
7253 static int pcidev_suspend(struct pci_dev *pdev, pm_message_t state)
7255 int i;
7256 struct platform_info *info = pci_get_drvdata(pdev);
7257 struct dev_info *hw_priv = &info->dev_info;
7258 struct ksz_hw *hw = &hw_priv->hw;
7260 /* Need to find a way to retrieve the device IP address. */
7261 u8 net_addr[] = { 192, 168, 1, 1 };
7263 for (i = 0; i < hw->dev_count; i++) {
7264 if (info->netdev[i]) {
7265 struct net_device *dev = info->netdev[i];
7267 if (netif_running(dev)) {
7268 netif_device_detach(dev);
7269 netdev_close(dev);
7273 if (hw_priv->wol_enable) {
7274 hw_enable_wol(hw, hw_priv->wol_enable, net_addr);
7275 hw_cfg_wol_pme(hw, 1);
7278 pci_save_state(pdev);
7279 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
7280 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7281 return 0;
7283 #endif
7285 static char pcidev_name[] = "ksz884xp";
7287 static struct pci_device_id pcidev_table[] = {
7288 { PCI_VENDOR_ID_MICREL_KS, 0x8841,
7289 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
7290 { PCI_VENDOR_ID_MICREL_KS, 0x8842,
7291 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
7292 { 0 }
7295 MODULE_DEVICE_TABLE(pci, pcidev_table);
7297 static struct pci_driver pci_device_driver = {
7298 #ifdef CONFIG_PM
7299 .suspend = pcidev_suspend,
7300 .resume = pcidev_resume,
7301 #endif
7302 .name = pcidev_name,
7303 .id_table = pcidev_table,
7304 .probe = pcidev_init,
7305 .remove = pcidev_exit
7308 static int __init ksz884x_init_module(void)
7310 return pci_register_driver(&pci_device_driver);
7313 static void __exit ksz884x_cleanup_module(void)
7315 pci_unregister_driver(&pci_device_driver);
7318 module_init(ksz884x_init_module);
7319 module_exit(ksz884x_cleanup_module);
7321 MODULE_DESCRIPTION("KSZ8841/2 PCI network driver");
7322 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@micrel.com>");
7323 MODULE_LICENSE("GPL");
7325 module_param_named(message, msg_enable, int, 0);
7326 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
7328 module_param(macaddr, charp, 0);
7329 module_param(mac1addr, charp, 0);
7330 module_param(fast_aging, int, 0);
7331 module_param(multi_dev, int, 0);
7332 module_param(stp, int, 0);
7333 MODULE_PARM_DESC(macaddr, "MAC address");
7334 MODULE_PARM_DESC(mac1addr, "Second MAC address");
7335 MODULE_PARM_DESC(fast_aging, "Fast aging");
7336 MODULE_PARM_DESC(multi_dev, "Multiple device interfaces");
7337 MODULE_PARM_DESC(stp, "STP support");