blob: 57395b9587dc8efe068fd8f0a35895269f666318 [file] [log] [blame]
Francois Romieu1202d6f2007-09-17 17:13:55 -07001/*
2 * ipg.c: Device Driver for the IP1000 Gigabit Ethernet Adapter
3 *
4 * Copyright (C) 2003, 2007 IC Plus Corp
5 *
6 * Original Author:
7 *
8 * Craig Rich
9 * Sundance Technology, Inc.
10 * www.sundanceti.com
11 * craig_rich@sundanceti.com
12 *
13 * Current Maintainer:
14 *
15 * Sorbica Shieh.
16 * http://www.icplus.com.tw
17 * sorbica@icplus.com.tw
18 *
19 * Jesse Huang
20 * http://www.icplus.com.tw
21 * jesse@icplus.com.tw
22 */
23#include <linux/crc32.h>
24#include <linux/ethtool.h>
25#include <linux/mii.h>
26#include <linux/mutex.h>
27
trem1dad9392007-10-02 14:04:38 -070028#include <asm/div64.h>
29
Francois Romieu1202d6f2007-09-17 17:13:55 -070030#define IPG_RX_RING_BYTES (sizeof(struct ipg_rx) * IPG_RFDLIST_LENGTH)
31#define IPG_TX_RING_BYTES (sizeof(struct ipg_tx) * IPG_TFDLIST_LENGTH)
32#define IPG_RESET_MASK \
33 (IPG_AC_GLOBAL_RESET | IPG_AC_RX_RESET | IPG_AC_TX_RESET | \
34 IPG_AC_DMA | IPG_AC_FIFO | IPG_AC_NETWORK | IPG_AC_HOST | \
35 IPG_AC_AUTO_INIT)
36
Pekka Enberg8da5bb72007-11-30 09:55:43 +020037#define ipg_w32(val32, reg) iowrite32((val32), ioaddr + (reg))
38#define ipg_w16(val16, reg) iowrite16((val16), ioaddr + (reg))
39#define ipg_w8(val8, reg) iowrite8((val8), ioaddr + (reg))
Francois Romieu1202d6f2007-09-17 17:13:55 -070040
41#define ipg_r32(reg) ioread32(ioaddr + (reg))
42#define ipg_r16(reg) ioread16(ioaddr + (reg))
43#define ipg_r8(reg) ioread8(ioaddr + (reg))
44
45#define JUMBO_FRAME_4k_ONLY
46enum {
47 netdev_io_size = 128
48};
49
50#include "ipg.h"
51#define DRV_NAME "ipg"
52
53MODULE_AUTHOR("IC Plus Corp. 2003");
Pekka Enberg1662e4b2007-11-30 09:54:06 +020054MODULE_DESCRIPTION("IC Plus IP1000 Gigabit Ethernet Adapter Linux Driver");
Francois Romieu1202d6f2007-09-17 17:13:55 -070055MODULE_LICENSE("GPL");
56
Pekka Enberg8da5bb72007-11-30 09:55:43 +020057/*
58 * Variable record -- index by leading revision/length
59 * Revision/Length(=N*4), Address1, Data1, Address2, Data2,...,AddressN,DataN
60 */
Adrian Bunk96fd74b2007-10-24 18:23:19 +020061static unsigned short DefaultPhyParam[] = {
Pekka Enberg8da5bb72007-11-30 09:55:43 +020062 /* 11/12/03 IP1000A v1-3 rev=0x40 */
Adrian Bunk96fd74b2007-10-24 18:23:19 +020063 /*--------------------------------------------------------------------------
64 (0x4000|(15*4)), 31, 0x0001, 27, 0x01e0, 31, 0x0002, 22, 0x85bd, 24, 0xfff2,
65 27, 0x0c10, 28, 0x0c10, 29, 0x2c10, 31, 0x0003, 23, 0x92f6,
66 31, 0x0000, 23, 0x003d, 30, 0x00de, 20, 0x20e7, 9, 0x0700,
67 --------------------------------------------------------------------------*/
Pekka Enberg8da5bb72007-11-30 09:55:43 +020068 /* 12/17/03 IP1000A v1-4 rev=0x40 */
Adrian Bunk96fd74b2007-10-24 18:23:19 +020069 (0x4000 | (07 * 4)), 31, 0x0001, 27, 0x01e0, 31, 0x0002, 27, 0xeb8e, 31,
70 0x0000,
71 30, 0x005e, 9, 0x0700,
Pekka Enberg8da5bb72007-11-30 09:55:43 +020072 /* 01/09/04 IP1000A v1-5 rev=0x41 */
Adrian Bunk96fd74b2007-10-24 18:23:19 +020073 (0x4100 | (07 * 4)), 31, 0x0001, 27, 0x01e0, 31, 0x0002, 27, 0xeb8e, 31,
74 0x0000,
75 30, 0x005e, 9, 0x0700,
76 0x0000
77};
78
Francois Romieu1202d6f2007-09-17 17:13:55 -070079static const char *ipg_brand_name[] = {
80 "IC PLUS IP1000 1000/100/10 based NIC",
81 "Sundance Technology ST2021 based NIC",
82 "Tamarack Microelectronics TC9020/9021 based NIC",
83 "Tamarack Microelectronics TC9020/9021 based NIC",
84 "D-Link NIC",
85 "D-Link NIC IP1000A"
86};
87
88static struct pci_device_id ipg_pci_tbl[] __devinitdata = {
89 { PCI_VDEVICE(SUNDANCE, 0x1023), 0 },
90 { PCI_VDEVICE(SUNDANCE, 0x2021), 1 },
91 { PCI_VDEVICE(SUNDANCE, 0x1021), 2 },
92 { PCI_VDEVICE(DLINK, 0x9021), 3 },
93 { PCI_VDEVICE(DLINK, 0x4000), 4 },
94 { PCI_VDEVICE(DLINK, 0x4020), 5 },
95 { 0, }
96};
97
98MODULE_DEVICE_TABLE(pci, ipg_pci_tbl);
99
100static inline void __iomem *ipg_ioaddr(struct net_device *dev)
101{
102 struct ipg_nic_private *sp = netdev_priv(dev);
103 return sp->ioaddr;
104}
105
106#ifdef IPG_DEBUG
107static void ipg_dump_rfdlist(struct net_device *dev)
108{
109 struct ipg_nic_private *sp = netdev_priv(dev);
110 void __iomem *ioaddr = sp->ioaddr;
111 unsigned int i;
112 u32 offset;
113
114 IPG_DEBUG_MSG("_dump_rfdlist\n");
115
116 printk(KERN_INFO "rx_current = %2.2x\n", sp->rx_current);
117 printk(KERN_INFO "rx_dirty = %2.2x\n", sp->rx_dirty);
118 printk(KERN_INFO "RFDList start address = %16.16lx\n",
119 (unsigned long) sp->rxd_map);
120 printk(KERN_INFO "RFDListPtr register = %8.8x%8.8x\n",
121 ipg_r32(IPG_RFDLISTPTR1), ipg_r32(IPG_RFDLISTPTR0));
122
123 for (i = 0; i < IPG_RFDLIST_LENGTH; i++) {
124 offset = (u32) &sp->rxd[i].next_desc - (u32) sp->rxd;
125 printk(KERN_INFO "%2.2x %4.4x RFDNextPtr = %16.16lx\n", i,
126 offset, (unsigned long) sp->rxd[i].next_desc);
127 offset = (u32) &sp->rxd[i].rfs - (u32) sp->rxd;
128 printk(KERN_INFO "%2.2x %4.4x RFS = %16.16lx\n", i,
129 offset, (unsigned long) sp->rxd[i].rfs);
130 offset = (u32) &sp->rxd[i].frag_info - (u32) sp->rxd;
131 printk(KERN_INFO "%2.2x %4.4x frag_info = %16.16lx\n", i,
132 offset, (unsigned long) sp->rxd[i].frag_info);
133 }
134}
135
136static void ipg_dump_tfdlist(struct net_device *dev)
137{
138 struct ipg_nic_private *sp = netdev_priv(dev);
139 void __iomem *ioaddr = sp->ioaddr;
140 unsigned int i;
141 u32 offset;
142
143 IPG_DEBUG_MSG("_dump_tfdlist\n");
144
145 printk(KERN_INFO "tx_current = %2.2x\n", sp->tx_current);
146 printk(KERN_INFO "tx_dirty = %2.2x\n", sp->tx_dirty);
147 printk(KERN_INFO "TFDList start address = %16.16lx\n",
148 (unsigned long) sp->txd_map);
149 printk(KERN_INFO "TFDListPtr register = %8.8x%8.8x\n",
150 ipg_r32(IPG_TFDLISTPTR1), ipg_r32(IPG_TFDLISTPTR0));
151
152 for (i = 0; i < IPG_TFDLIST_LENGTH; i++) {
153 offset = (u32) &sp->txd[i].next_desc - (u32) sp->txd;
154 printk(KERN_INFO "%2.2x %4.4x TFDNextPtr = %16.16lx\n", i,
155 offset, (unsigned long) sp->txd[i].next_desc);
156
157 offset = (u32) &sp->txd[i].tfc - (u32) sp->txd;
158 printk(KERN_INFO "%2.2x %4.4x TFC = %16.16lx\n", i,
159 offset, (unsigned long) sp->txd[i].tfc);
160 offset = (u32) &sp->txd[i].frag_info - (u32) sp->txd;
161 printk(KERN_INFO "%2.2x %4.4x frag_info = %16.16lx\n", i,
162 offset, (unsigned long) sp->txd[i].frag_info);
163 }
164}
165#endif
166
167static void ipg_write_phy_ctl(void __iomem *ioaddr, u8 data)
168{
169 ipg_w8(IPG_PC_RSVD_MASK & data, PHY_CTRL);
170 ndelay(IPG_PC_PHYCTRLWAIT_NS);
171}
172
173static void ipg_drive_phy_ctl_low_high(void __iomem *ioaddr, u8 data)
174{
175 ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_LO | data);
176 ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_HI | data);
177}
178
179static void send_three_state(void __iomem *ioaddr, u8 phyctrlpolarity)
180{
181 phyctrlpolarity |= (IPG_PC_MGMTDATA & 0) | IPG_PC_MGMTDIR;
182
183 ipg_drive_phy_ctl_low_high(ioaddr, phyctrlpolarity);
184}
185
186static void send_end(void __iomem *ioaddr, u8 phyctrlpolarity)
187{
188 ipg_w8((IPG_PC_MGMTCLK_LO | (IPG_PC_MGMTDATA & 0) | IPG_PC_MGMTDIR |
189 phyctrlpolarity) & IPG_PC_RSVD_MASK, PHY_CTRL);
190}
191
Pekka Enberg8da5bb72007-11-30 09:55:43 +0200192static u16 read_phy_bit(void __iomem *ioaddr, u8 phyctrlpolarity)
Francois Romieu1202d6f2007-09-17 17:13:55 -0700193{
194 u16 bit_data;
195
196 ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_LO | phyctrlpolarity);
197
198 bit_data = ((ipg_r8(PHY_CTRL) & IPG_PC_MGMTDATA) >> 1) & 1;
199
200 ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_HI | phyctrlpolarity);
201
202 return bit_data;
203}
204
205/*
206 * Read a register from the Physical Layer device located
207 * on the IPG NIC, using the IPG PHYCTRL register.
208 */
Pekka Enberg8da5bb72007-11-30 09:55:43 +0200209static int mdio_read(struct net_device *dev, int phy_id, int phy_reg)
Francois Romieu1202d6f2007-09-17 17:13:55 -0700210{
211 void __iomem *ioaddr = ipg_ioaddr(dev);
212 /*
213 * The GMII mangement frame structure for a read is as follows:
214 *
215 * |Preamble|st|op|phyad|regad|ta| data |idle|
216 * |< 32 1s>|01|10|AAAAA|RRRRR|z0|DDDDDDDDDDDDDDDD|z |
217 *
218 * <32 1s> = 32 consecutive logic 1 values
219 * A = bit of Physical Layer device address (MSB first)
220 * R = bit of register address (MSB first)
221 * z = High impedance state
222 * D = bit of read data (MSB first)
223 *
224 * Transmission order is 'Preamble' field first, bits transmitted
225 * left to right (first to last).
226 */
227 struct {
228 u32 field;
229 unsigned int len;
230 } p[] = {
231 { GMII_PREAMBLE, 32 }, /* Preamble */
232 { GMII_ST, 2 }, /* ST */
233 { GMII_READ, 2 }, /* OP */
234 { phy_id, 5 }, /* PHYAD */
235 { phy_reg, 5 }, /* REGAD */
236 { 0x0000, 2 }, /* TA */
237 { 0x0000, 16 }, /* DATA */
238 { 0x0000, 1 } /* IDLE */
239 };
240 unsigned int i, j;
241 u8 polarity, data;
242
243 polarity = ipg_r8(PHY_CTRL);
244 polarity &= (IPG_PC_DUPLEX_POLARITY | IPG_PC_LINK_POLARITY);
245
246 /* Create the Preamble, ST, OP, PHYAD, and REGAD field. */
247 for (j = 0; j < 5; j++) {
248 for (i = 0; i < p[j].len; i++) {
249 /* For each variable length field, the MSB must be
250 * transmitted first. Rotate through the field bits,
251 * starting with the MSB, and move each bit into the
252 * the 1st (2^1) bit position (this is the bit position
253 * corresponding to the MgmtData bit of the PhyCtrl
254 * register for the IPG).
255 *
256 * Example: ST = 01;
257 *
258 * First write a '0' to bit 1 of the PhyCtrl
259 * register, then write a '1' to bit 1 of the
260 * PhyCtrl register.
261 *
262 * To do this, right shift the MSB of ST by the value:
263 * [field length - 1 - #ST bits already written]
264 * then left shift this result by 1.
265 */
266 data = (p[j].field >> (p[j].len - 1 - i)) << 1;
267 data &= IPG_PC_MGMTDATA;
268 data |= polarity | IPG_PC_MGMTDIR;
269
270 ipg_drive_phy_ctl_low_high(ioaddr, data);
271 }
272 }
273
274 send_three_state(ioaddr, polarity);
275
276 read_phy_bit(ioaddr, polarity);
277
278 /*
279 * For a read cycle, the bits for the next two fields (TA and
280 * DATA) are driven by the PHY (the IPG reads these bits).
281 */
282 for (i = 0; i < p[6].len; i++) {
283 p[6].field |=
284 (read_phy_bit(ioaddr, polarity) << (p[6].len - 1 - i));
285 }
286
287 send_three_state(ioaddr, polarity);
288 send_three_state(ioaddr, polarity);
289 send_three_state(ioaddr, polarity);
290 send_end(ioaddr, polarity);
291
292 /* Return the value of the DATA field. */
293 return p[6].field;
294}
295
296/*
297 * Write to a register from the Physical Layer device located
298 * on the IPG NIC, using the IPG PHYCTRL register.
299 */
300static void mdio_write(struct net_device *dev, int phy_id, int phy_reg, int val)
301{
302 void __iomem *ioaddr = ipg_ioaddr(dev);
303 /*
304 * The GMII mangement frame structure for a read is as follows:
305 *
306 * |Preamble|st|op|phyad|regad|ta| data |idle|
307 * |< 32 1s>|01|10|AAAAA|RRRRR|z0|DDDDDDDDDDDDDDDD|z |
308 *
309 * <32 1s> = 32 consecutive logic 1 values
310 * A = bit of Physical Layer device address (MSB first)
311 * R = bit of register address (MSB first)
312 * z = High impedance state
313 * D = bit of write data (MSB first)
314 *
315 * Transmission order is 'Preamble' field first, bits transmitted
316 * left to right (first to last).
317 */
318 struct {
319 u32 field;
320 unsigned int len;
321 } p[] = {
322 { GMII_PREAMBLE, 32 }, /* Preamble */
323 { GMII_ST, 2 }, /* ST */
324 { GMII_WRITE, 2 }, /* OP */
325 { phy_id, 5 }, /* PHYAD */
326 { phy_reg, 5 }, /* REGAD */
327 { 0x0002, 2 }, /* TA */
328 { val & 0xffff, 16 }, /* DATA */
329 { 0x0000, 1 } /* IDLE */
330 };
331 unsigned int i, j;
332 u8 polarity, data;
333
334 polarity = ipg_r8(PHY_CTRL);
335 polarity &= (IPG_PC_DUPLEX_POLARITY | IPG_PC_LINK_POLARITY);
336
337 /* Create the Preamble, ST, OP, PHYAD, and REGAD field. */
338 for (j = 0; j < 7; j++) {
339 for (i = 0; i < p[j].len; i++) {
340 /* For each variable length field, the MSB must be
341 * transmitted first. Rotate through the field bits,
342 * starting with the MSB, and move each bit into the
343 * the 1st (2^1) bit position (this is the bit position
344 * corresponding to the MgmtData bit of the PhyCtrl
345 * register for the IPG).
346 *
347 * Example: ST = 01;
348 *
349 * First write a '0' to bit 1 of the PhyCtrl
350 * register, then write a '1' to bit 1 of the
351 * PhyCtrl register.
352 *
353 * To do this, right shift the MSB of ST by the value:
354 * [field length - 1 - #ST bits already written]
355 * then left shift this result by 1.
356 */
357 data = (p[j].field >> (p[j].len - 1 - i)) << 1;
358 data &= IPG_PC_MGMTDATA;
359 data |= polarity | IPG_PC_MGMTDIR;
360
361 ipg_drive_phy_ctl_low_high(ioaddr, data);
362 }
363 }
364
365 /* The last cycle is a tri-state, so read from the PHY. */
366 for (j = 7; j < 8; j++) {
367 for (i = 0; i < p[j].len; i++) {
368 ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_LO | polarity);
369
370 p[j].field |= ((ipg_r8(PHY_CTRL) &
371 IPG_PC_MGMTDATA) >> 1) << (p[j].len - 1 - i);
372
373 ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_HI | polarity);
374 }
375 }
376}
377
Francois Romieu1202d6f2007-09-17 17:13:55 -0700378static void ipg_set_led_mode(struct net_device *dev)
379{
380 struct ipg_nic_private *sp = netdev_priv(dev);
381 void __iomem *ioaddr = sp->ioaddr;
382 u32 mode;
383
384 mode = ipg_r32(ASIC_CTRL);
385 mode &= ~(IPG_AC_LED_MODE_BIT_1 | IPG_AC_LED_MODE | IPG_AC_LED_SPEED);
386
Pekka Enberg9893ba12008-01-24 02:01:17 -0800387 if ((sp->led_mode & 0x03) > 1)
Francois Romieu1202d6f2007-09-17 17:13:55 -0700388 mode |= IPG_AC_LED_MODE_BIT_1; /* Write Asic Control Bit 29 */
389
Pekka Enberg9893ba12008-01-24 02:01:17 -0800390 if ((sp->led_mode & 0x01) == 1)
Francois Romieu1202d6f2007-09-17 17:13:55 -0700391 mode |= IPG_AC_LED_MODE; /* Write Asic Control Bit 14 */
392
Pekka Enberg9893ba12008-01-24 02:01:17 -0800393 if ((sp->led_mode & 0x08) == 8)
Francois Romieu1202d6f2007-09-17 17:13:55 -0700394 mode |= IPG_AC_LED_SPEED; /* Write Asic Control Bit 27 */
395
396 ipg_w32(mode, ASIC_CTRL);
397}
398
Francois Romieu1202d6f2007-09-17 17:13:55 -0700399static void ipg_set_phy_set(struct net_device *dev)
400{
401 struct ipg_nic_private *sp = netdev_priv(dev);
402 void __iomem *ioaddr = sp->ioaddr;
403 int physet;
404
405 physet = ipg_r8(PHY_SET);
406 physet &= ~(IPG_PS_MEM_LENB9B | IPG_PS_MEM_LEN9 | IPG_PS_NON_COMPDET);
Pekka Enberg9893ba12008-01-24 02:01:17 -0800407 physet |= ((sp->led_mode & 0x70) >> 4);
Francois Romieu1202d6f2007-09-17 17:13:55 -0700408 ipg_w8(physet, PHY_SET);
409}
410
411static int ipg_reset(struct net_device *dev, u32 resetflags)
412{
413 /* Assert functional resets via the IPG AsicCtrl
414 * register as specified by the 'resetflags' input
415 * parameter.
416 */
Pekka Enbergdea4a872007-11-30 09:54:51 +0200417 void __iomem *ioaddr = ipg_ioaddr(dev);
Francois Romieu1202d6f2007-09-17 17:13:55 -0700418 unsigned int timeout_count = 0;
419
420 IPG_DEBUG_MSG("_reset\n");
421
422 ipg_w32(ipg_r32(ASIC_CTRL) | resetflags, ASIC_CTRL);
423
424 /* Delay added to account for problem with 10Mbps reset. */
425 mdelay(IPG_AC_RESETWAIT);
426
427 while (IPG_AC_RESET_BUSY & ipg_r32(ASIC_CTRL)) {
428 mdelay(IPG_AC_RESETWAIT);
429 if (++timeout_count > IPG_AC_RESET_TIMEOUT)
430 return -ETIME;
431 }
Pekka Enbergdea4a872007-11-30 09:54:51 +0200432 /* Set LED Mode in Asic Control */
Francois Romieu1202d6f2007-09-17 17:13:55 -0700433 ipg_set_led_mode(dev);
434
Pekka Enbergdea4a872007-11-30 09:54:51 +0200435 /* Set PHYSet Register Value */
Francois Romieu1202d6f2007-09-17 17:13:55 -0700436 ipg_set_phy_set(dev);
437 return 0;
438}
439
440/* Find the GMII PHY address. */
441static int ipg_find_phyaddr(struct net_device *dev)
442{
443 unsigned int phyaddr, i;
444
445 for (i = 0; i < 32; i++) {
446 u32 status;
447
448 /* Search for the correct PHY address among 32 possible. */
449 phyaddr = (IPG_NIC_PHY_ADDRESS + i) % 32;
450
451 /* 10/22/03 Grace change verify from GMII_PHY_STATUS to
452 GMII_PHY_ID1
453 */
454
455 status = mdio_read(dev, phyaddr, MII_BMSR);
456
457 if ((status != 0xFFFF) && (status != 0))
458 return phyaddr;
459 }
460
461 return 0x1f;
462}
463
464/*
465 * Configure IPG based on result of IEEE 802.3 PHY
466 * auto-negotiation.
467 */
468static int ipg_config_autoneg(struct net_device *dev)
469{
470 struct ipg_nic_private *sp = netdev_priv(dev);
471 void __iomem *ioaddr = sp->ioaddr;
472 unsigned int txflowcontrol;
473 unsigned int rxflowcontrol;
474 unsigned int fullduplex;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700475 u32 mac_ctrl_val;
476 u32 asicctrl;
477 u8 phyctrl;
478
479 IPG_DEBUG_MSG("_config_autoneg\n");
480
481 asicctrl = ipg_r32(ASIC_CTRL);
482 phyctrl = ipg_r8(PHY_CTRL);
483 mac_ctrl_val = ipg_r32(MAC_CTRL);
484
485 /* Set flags for use in resolving auto-negotation, assuming
486 * non-1000Mbps, half duplex, no flow control.
487 */
488 fullduplex = 0;
489 txflowcontrol = 0;
490 rxflowcontrol = 0;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700491
492 /* To accomodate a problem in 10Mbps operation,
493 * set a global flag if PHY running in 10Mbps mode.
494 */
495 sp->tenmbpsmode = 0;
496
497 printk(KERN_INFO "%s: Link speed = ", dev->name);
498
499 /* Determine actual speed of operation. */
500 switch (phyctrl & IPG_PC_LINK_SPEED) {
501 case IPG_PC_LINK_SPEED_10MBPS:
502 printk("10Mbps.\n");
503 printk(KERN_INFO "%s: 10Mbps operational mode enabled.\n",
504 dev->name);
505 sp->tenmbpsmode = 1;
506 break;
507 case IPG_PC_LINK_SPEED_100MBPS:
508 printk("100Mbps.\n");
509 break;
510 case IPG_PC_LINK_SPEED_1000MBPS:
511 printk("1000Mbps.\n");
Francois Romieu1202d6f2007-09-17 17:13:55 -0700512 break;
513 default:
514 printk("undefined!\n");
515 return 0;
516 }
517
518 if (phyctrl & IPG_PC_DUPLEX_STATUS) {
519 fullduplex = 1;
520 txflowcontrol = 1;
521 rxflowcontrol = 1;
522 }
523
524 /* Configure full duplex, and flow control. */
525 if (fullduplex == 1) {
526 /* Configure IPG for full duplex operation. */
527 printk(KERN_INFO "%s: setting full duplex, ", dev->name);
528
529 mac_ctrl_val |= IPG_MC_DUPLEX_SELECT_FD;
530
531 if (txflowcontrol == 1) {
532 printk("TX flow control");
533 mac_ctrl_val |= IPG_MC_TX_FLOW_CONTROL_ENABLE;
534 } else {
535 printk("no TX flow control");
536 mac_ctrl_val &= ~IPG_MC_TX_FLOW_CONTROL_ENABLE;
537 }
538
539 if (rxflowcontrol == 1) {
540 printk(", RX flow control.");
541 mac_ctrl_val |= IPG_MC_RX_FLOW_CONTROL_ENABLE;
542 } else {
543 printk(", no RX flow control.");
544 mac_ctrl_val &= ~IPG_MC_RX_FLOW_CONTROL_ENABLE;
545 }
546
547 printk("\n");
548 } else {
549 /* Configure IPG for half duplex operation. */
Pekka Enberg8da5bb72007-11-30 09:55:43 +0200550 printk(KERN_INFO "%s: setting half duplex, "
Francois Romieu1202d6f2007-09-17 17:13:55 -0700551 "no TX flow control, no RX flow control.\n", dev->name);
552
553 mac_ctrl_val &= ~IPG_MC_DUPLEX_SELECT_FD &
554 ~IPG_MC_TX_FLOW_CONTROL_ENABLE &
555 ~IPG_MC_RX_FLOW_CONTROL_ENABLE;
556 }
557 ipg_w32(mac_ctrl_val, MAC_CTRL);
558 return 0;
559}
560
561/* Determine and configure multicast operation and set
562 * receive mode for IPG.
563 */
564static void ipg_nic_set_multicast_list(struct net_device *dev)
565{
566 void __iomem *ioaddr = ipg_ioaddr(dev);
567 struct dev_mc_list *mc_list_ptr;
568 unsigned int hashindex;
569 u32 hashtable[2];
570 u8 receivemode;
571
572 IPG_DEBUG_MSG("_nic_set_multicast_list\n");
573
574 receivemode = IPG_RM_RECEIVEUNICAST | IPG_RM_RECEIVEBROADCAST;
575
576 if (dev->flags & IFF_PROMISC) {
577 /* NIC to be configured in promiscuous mode. */
578 receivemode = IPG_RM_RECEIVEALLFRAMES;
579 } else if ((dev->flags & IFF_ALLMULTI) ||
Roel Kluin07612482008-06-09 16:33:50 -0700580 ((dev->flags & IFF_MULTICAST) &&
Francois Romieu1202d6f2007-09-17 17:13:55 -0700581 (dev->mc_count > IPG_MULTICAST_HASHTABLE_SIZE))) {
582 /* NIC to be configured to receive all multicast
583 * frames. */
584 receivemode |= IPG_RM_RECEIVEMULTICAST;
Roel Kluin07612482008-06-09 16:33:50 -0700585 } else if ((dev->flags & IFF_MULTICAST) && (dev->mc_count > 0)) {
Francois Romieu1202d6f2007-09-17 17:13:55 -0700586 /* NIC to be configured to receive selected
587 * multicast addresses. */
588 receivemode |= IPG_RM_RECEIVEMULTICASTHASH;
589 }
590
591 /* Calculate the bits to set for the 64 bit, IPG HASHTABLE.
592 * The IPG applies a cyclic-redundancy-check (the same CRC
593 * used to calculate the frame data FCS) to the destination
594 * address all incoming multicast frames whose destination
595 * address has the multicast bit set. The least significant
596 * 6 bits of the CRC result are used as an addressing index
597 * into the hash table. If the value of the bit addressed by
598 * this index is a 1, the frame is passed to the host system.
599 */
600
601 /* Clear hashtable. */
602 hashtable[0] = 0x00000000;
603 hashtable[1] = 0x00000000;
604
605 /* Cycle through all multicast addresses to filter. */
606 for (mc_list_ptr = dev->mc_list;
607 mc_list_ptr != NULL; mc_list_ptr = mc_list_ptr->next) {
608 /* Calculate CRC result for each multicast address. */
609 hashindex = crc32_le(0xffffffff, mc_list_ptr->dmi_addr,
610 ETH_ALEN);
611
612 /* Use only the least significant 6 bits. */
613 hashindex = hashindex & 0x3F;
614
615 /* Within "hashtable", set bit number "hashindex"
616 * to a logic 1.
617 */
618 set_bit(hashindex, (void *)hashtable);
619 }
620
621 /* Write the value of the hashtable, to the 4, 16 bit
622 * HASHTABLE IPG registers.
623 */
624 ipg_w32(hashtable[0], HASHTABLE_0);
625 ipg_w32(hashtable[1], HASHTABLE_1);
626
627 ipg_w8(IPG_RM_RSVD_MASK & receivemode, RECEIVE_MODE);
628
629 IPG_DEBUG_MSG("ReceiveMode = %x\n", ipg_r8(RECEIVE_MODE));
630}
631
632static int ipg_io_config(struct net_device *dev)
633{
Pekka Enbergda02b232008-06-23 14:36:35 +0300634 struct ipg_nic_private *sp = netdev_priv(dev);
Francois Romieu1202d6f2007-09-17 17:13:55 -0700635 void __iomem *ioaddr = ipg_ioaddr(dev);
636 u32 origmacctrl;
637 u32 restoremacctrl;
638
639 IPG_DEBUG_MSG("_io_config\n");
640
641 origmacctrl = ipg_r32(MAC_CTRL);
642
643 restoremacctrl = origmacctrl | IPG_MC_STATISTICS_ENABLE;
644
645 /* Based on compilation option, determine if FCS is to be
646 * stripped on receive frames by IPG.
647 */
648 if (!IPG_STRIP_FCS_ON_RX)
649 restoremacctrl |= IPG_MC_RCV_FCS;
650
651 /* Determine if transmitter and/or receiver are
652 * enabled so we may restore MACCTRL correctly.
653 */
654 if (origmacctrl & IPG_MC_TX_ENABLED)
655 restoremacctrl |= IPG_MC_TX_ENABLE;
656
657 if (origmacctrl & IPG_MC_RX_ENABLED)
658 restoremacctrl |= IPG_MC_RX_ENABLE;
659
660 /* Transmitter and receiver must be disabled before setting
661 * IFSSelect.
662 */
663 ipg_w32((origmacctrl & (IPG_MC_RX_DISABLE | IPG_MC_TX_DISABLE)) &
664 IPG_MC_RSVD_MASK, MAC_CTRL);
665
666 /* Now that transmitter and receiver are disabled, write
667 * to IFSSelect.
668 */
669 ipg_w32((origmacctrl & IPG_MC_IFS_96BIT) & IPG_MC_RSVD_MASK, MAC_CTRL);
670
671 /* Set RECEIVEMODE register. */
672 ipg_nic_set_multicast_list(dev);
673
Pekka Enbergda02b232008-06-23 14:36:35 +0300674 ipg_w16(sp->max_rxframe_size, MAX_FRAME_SIZE);
Francois Romieu1202d6f2007-09-17 17:13:55 -0700675
676 ipg_w8(IPG_RXDMAPOLLPERIOD_VALUE, RX_DMA_POLL_PERIOD);
677 ipg_w8(IPG_RXDMAURGENTTHRESH_VALUE, RX_DMA_URGENT_THRESH);
678 ipg_w8(IPG_RXDMABURSTTHRESH_VALUE, RX_DMA_BURST_THRESH);
679 ipg_w8(IPG_TXDMAPOLLPERIOD_VALUE, TX_DMA_POLL_PERIOD);
680 ipg_w8(IPG_TXDMAURGENTTHRESH_VALUE, TX_DMA_URGENT_THRESH);
681 ipg_w8(IPG_TXDMABURSTTHRESH_VALUE, TX_DMA_BURST_THRESH);
682 ipg_w16((IPG_IE_HOST_ERROR | IPG_IE_TX_DMA_COMPLETE |
683 IPG_IE_TX_COMPLETE | IPG_IE_INT_REQUESTED |
684 IPG_IE_UPDATE_STATS | IPG_IE_LINK_EVENT |
685 IPG_IE_RX_DMA_COMPLETE | IPG_IE_RX_DMA_PRIORITY), INT_ENABLE);
686 ipg_w16(IPG_FLOWONTHRESH_VALUE, FLOW_ON_THRESH);
687 ipg_w16(IPG_FLOWOFFTHRESH_VALUE, FLOW_OFF_THRESH);
688
689 /* IPG multi-frag frame bug workaround.
690 * Per silicon revision B3 eratta.
691 */
692 ipg_w16(ipg_r16(DEBUG_CTRL) | 0x0200, DEBUG_CTRL);
693
694 /* IPG TX poll now bug workaround.
695 * Per silicon revision B3 eratta.
696 */
697 ipg_w16(ipg_r16(DEBUG_CTRL) | 0x0010, DEBUG_CTRL);
698
699 /* IPG RX poll now bug workaround.
700 * Per silicon revision B3 eratta.
701 */
702 ipg_w16(ipg_r16(DEBUG_CTRL) | 0x0020, DEBUG_CTRL);
703
704 /* Now restore MACCTRL to original setting. */
705 ipg_w32(IPG_MC_RSVD_MASK & restoremacctrl, MAC_CTRL);
706
707 /* Disable unused RMON statistics. */
708 ipg_w32(IPG_RZ_ALL, RMON_STATISTICS_MASK);
709
710 /* Disable unused MIB statistics. */
711 ipg_w32(IPG_SM_MACCONTROLFRAMESXMTD | IPG_SM_MACCONTROLFRAMESRCVD |
712 IPG_SM_BCSTOCTETXMTOK_BCSTFRAMESXMTDOK | IPG_SM_TXJUMBOFRAMES |
713 IPG_SM_MCSTOCTETXMTOK_MCSTFRAMESXMTDOK | IPG_SM_RXJUMBOFRAMES |
714 IPG_SM_BCSTOCTETRCVDOK_BCSTFRAMESRCVDOK |
715 IPG_SM_UDPCHECKSUMERRORS | IPG_SM_TCPCHECKSUMERRORS |
716 IPG_SM_IPCHECKSUMERRORS, STATISTICS_MASK);
717
718 return 0;
719}
720
721/*
722 * Create a receive buffer within system memory and update
723 * NIC private structure appropriately.
724 */
725static int ipg_get_rxbuff(struct net_device *dev, int entry)
726{
727 struct ipg_nic_private *sp = netdev_priv(dev);
728 struct ipg_rx *rxfd = sp->rxd + entry;
729 struct sk_buff *skb;
730 u64 rxfragsize;
731
732 IPG_DEBUG_MSG("_get_rxbuff\n");
733
Pekka Enberg39f20582008-06-23 14:36:18 +0300734 skb = netdev_alloc_skb(dev, sp->rxsupport_size + NET_IP_ALIGN);
Francois Romieu1202d6f2007-09-17 17:13:55 -0700735 if (!skb) {
Pekka Enberg9893ba12008-01-24 02:01:17 -0800736 sp->rx_buff[entry] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700737 return -ENOMEM;
738 }
739
740 /* Adjust the data start location within the buffer to
741 * align IP address field to a 16 byte boundary.
742 */
743 skb_reserve(skb, NET_IP_ALIGN);
744
745 /* Associate the receive buffer with the IPG NIC. */
746 skb->dev = dev;
747
748 /* Save the address of the sk_buff structure. */
Pekka Enberg9893ba12008-01-24 02:01:17 -0800749 sp->rx_buff[entry] = skb;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700750
751 rxfd->frag_info = cpu_to_le64(pci_map_single(sp->pdev, skb->data,
752 sp->rx_buf_sz, PCI_DMA_FROMDEVICE));
753
754 /* Set the RFD fragment length. */
Pekka Enberg18a9cdb2008-06-23 14:36:00 +0300755 rxfragsize = sp->rxfrag_size;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700756 rxfd->frag_info |= cpu_to_le64((rxfragsize << 48) & IPG_RFI_FRAGLEN);
757
758 return 0;
759}
760
761static int init_rfdlist(struct net_device *dev)
762{
763 struct ipg_nic_private *sp = netdev_priv(dev);
764 void __iomem *ioaddr = sp->ioaddr;
765 unsigned int i;
766
767 IPG_DEBUG_MSG("_init_rfdlist\n");
768
769 for (i = 0; i < IPG_RFDLIST_LENGTH; i++) {
770 struct ipg_rx *rxfd = sp->rxd + i;
771
Pekka Enberg9893ba12008-01-24 02:01:17 -0800772 if (sp->rx_buff[i]) {
Francois Romieu1202d6f2007-09-17 17:13:55 -0700773 pci_unmap_single(sp->pdev,
Al Viro325a8072007-10-14 19:41:29 +0100774 le64_to_cpu(rxfd->frag_info) & ~IPG_RFI_FRAGLEN,
Francois Romieu1202d6f2007-09-17 17:13:55 -0700775 sp->rx_buf_sz, PCI_DMA_FROMDEVICE);
Pekka Enberg9893ba12008-01-24 02:01:17 -0800776 dev_kfree_skb_irq(sp->rx_buff[i]);
777 sp->rx_buff[i] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700778 }
779
780 /* Clear out the RFS field. */
781 rxfd->rfs = 0x0000000000000000;
782
783 if (ipg_get_rxbuff(dev, i) < 0) {
784 /*
785 * A receive buffer was not ready, break the
786 * RFD list here.
787 */
788 IPG_DEBUG_MSG("Cannot allocate Rx buffer.\n");
789
790 /* Just in case we cannot allocate a single RFD.
791 * Should not occur.
792 */
793 if (i == 0) {
794 printk(KERN_ERR "%s: No memory available"
795 " for RFD list.\n", dev->name);
796 return -ENOMEM;
797 }
798 }
799
800 rxfd->next_desc = cpu_to_le64(sp->rxd_map +
801 sizeof(struct ipg_rx)*(i + 1));
802 }
803 sp->rxd[i - 1].next_desc = cpu_to_le64(sp->rxd_map);
804
805 sp->rx_current = 0;
806 sp->rx_dirty = 0;
807
808 /* Write the location of the RFDList to the IPG. */
809 ipg_w32((u32) sp->rxd_map, RFD_LIST_PTR_0);
810 ipg_w32(0x00000000, RFD_LIST_PTR_1);
811
812 return 0;
813}
814
815static void init_tfdlist(struct net_device *dev)
816{
817 struct ipg_nic_private *sp = netdev_priv(dev);
818 void __iomem *ioaddr = sp->ioaddr;
819 unsigned int i;
820
821 IPG_DEBUG_MSG("_init_tfdlist\n");
822
823 for (i = 0; i < IPG_TFDLIST_LENGTH; i++) {
824 struct ipg_tx *txfd = sp->txd + i;
825
826 txfd->tfc = cpu_to_le64(IPG_TFC_TFDDONE);
827
Pekka Enberg9893ba12008-01-24 02:01:17 -0800828 if (sp->tx_buff[i]) {
829 dev_kfree_skb_irq(sp->tx_buff[i]);
830 sp->tx_buff[i] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700831 }
832
833 txfd->next_desc = cpu_to_le64(sp->txd_map +
834 sizeof(struct ipg_tx)*(i + 1));
835 }
836 sp->txd[i - 1].next_desc = cpu_to_le64(sp->txd_map);
837
838 sp->tx_current = 0;
839 sp->tx_dirty = 0;
840
841 /* Write the location of the TFDList to the IPG. */
842 IPG_DDEBUG_MSG("Starting TFDListPtr = %8.8x\n",
843 (u32) sp->txd_map);
844 ipg_w32((u32) sp->txd_map, TFD_LIST_PTR_0);
845 ipg_w32(0x00000000, TFD_LIST_PTR_1);
846
Pekka Enberg9893ba12008-01-24 02:01:17 -0800847 sp->reset_current_tfd = 1;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700848}
849
850/*
851 * Free all transmit buffers which have already been transfered
852 * via DMA to the IPG.
853 */
854static void ipg_nic_txfree(struct net_device *dev)
855{
856 struct ipg_nic_private *sp = netdev_priv(dev);
Francois Romieu0da1b992008-01-10 23:40:59 +0100857 unsigned int released, pending, dirty;
trem1dad9392007-10-02 14:04:38 -0700858
Francois Romieu1202d6f2007-09-17 17:13:55 -0700859 IPG_DEBUG_MSG("_nic_txfree\n");
860
861 pending = sp->tx_current - sp->tx_dirty;
Francois Romieu0da1b992008-01-10 23:40:59 +0100862 dirty = sp->tx_dirty % IPG_TFDLIST_LENGTH;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700863
864 for (released = 0; released < pending; released++) {
Pekka Enberg9893ba12008-01-24 02:01:17 -0800865 struct sk_buff *skb = sp->tx_buff[dirty];
Francois Romieu1202d6f2007-09-17 17:13:55 -0700866 struct ipg_tx *txfd = sp->txd + dirty;
867
868 IPG_DEBUG_MSG("TFC = %16.16lx\n", (unsigned long) txfd->tfc);
869
870 /* Look at each TFD's TFC field beginning
871 * at the last freed TFD up to the current TFD.
872 * If the TFDDone bit is set, free the associated
873 * buffer.
874 */
Francois Romieu0da1b992008-01-10 23:40:59 +0100875 if (!(txfd->tfc & cpu_to_le64(IPG_TFC_TFDDONE)))
876 break;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700877
878 /* Free the transmit buffer. */
879 if (skb) {
880 pci_unmap_single(sp->pdev,
Al Viro325a8072007-10-14 19:41:29 +0100881 le64_to_cpu(txfd->frag_info) & ~IPG_TFI_FRAGLEN,
Francois Romieu1202d6f2007-09-17 17:13:55 -0700882 skb->len, PCI_DMA_TODEVICE);
883
Pekka Enberg85d68a52007-11-30 09:53:51 +0200884 dev_kfree_skb_irq(skb);
Francois Romieu1202d6f2007-09-17 17:13:55 -0700885
Pekka Enberg9893ba12008-01-24 02:01:17 -0800886 sp->tx_buff[dirty] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700887 }
Francois Romieu0da1b992008-01-10 23:40:59 +0100888 dirty = (dirty + 1) % IPG_TFDLIST_LENGTH;
Francois Romieu1202d6f2007-09-17 17:13:55 -0700889 }
890
891 sp->tx_dirty += released;
892
893 if (netif_queue_stopped(dev) &&
894 (sp->tx_current != (sp->tx_dirty + IPG_TFDLIST_LENGTH))) {
895 netif_wake_queue(dev);
896 }
897}
898
899static void ipg_tx_timeout(struct net_device *dev)
900{
901 struct ipg_nic_private *sp = netdev_priv(dev);
902 void __iomem *ioaddr = sp->ioaddr;
903
904 ipg_reset(dev, IPG_AC_TX_RESET | IPG_AC_DMA | IPG_AC_NETWORK |
905 IPG_AC_FIFO);
906
907 spin_lock_irq(&sp->lock);
908
909 /* Re-configure after DMA reset. */
910 if (ipg_io_config(dev) < 0) {
911 printk(KERN_INFO "%s: Error during re-configuration.\n",
912 dev->name);
913 }
914
915 init_tfdlist(dev);
916
917 spin_unlock_irq(&sp->lock);
918
919 ipg_w32((ipg_r32(MAC_CTRL) | IPG_MC_TX_ENABLE) & IPG_MC_RSVD_MASK,
920 MAC_CTRL);
921}
922
923/*
924 * For TxComplete interrupts, free all transmit
925 * buffers which have already been transfered via DMA
926 * to the IPG.
927 */
928static void ipg_nic_txcleanup(struct net_device *dev)
929{
930 struct ipg_nic_private *sp = netdev_priv(dev);
931 void __iomem *ioaddr = sp->ioaddr;
932 unsigned int i;
933
934 IPG_DEBUG_MSG("_nic_txcleanup\n");
935
936 for (i = 0; i < IPG_TFDLIST_LENGTH; i++) {
937 /* Reading the TXSTATUS register clears the
938 * TX_COMPLETE interrupt.
939 */
940 u32 txstatusdword = ipg_r32(TX_STATUS);
941
942 IPG_DEBUG_MSG("TxStatus = %8.8x\n", txstatusdword);
943
944 /* Check for Transmit errors. Error bits only valid if
945 * TX_COMPLETE bit in the TXSTATUS register is a 1.
946 */
947 if (!(txstatusdword & IPG_TS_TX_COMPLETE))
948 break;
949
950 /* If in 10Mbps mode, indicate transmit is ready. */
951 if (sp->tenmbpsmode) {
952 netif_wake_queue(dev);
953 }
954
955 /* Transmit error, increment stat counters. */
956 if (txstatusdword & IPG_TS_TX_ERROR) {
957 IPG_DEBUG_MSG("Transmit error.\n");
958 sp->stats.tx_errors++;
959 }
960
961 /* Late collision, re-enable transmitter. */
962 if (txstatusdword & IPG_TS_LATE_COLLISION) {
963 IPG_DEBUG_MSG("Late collision on transmit.\n");
964 ipg_w32((ipg_r32(MAC_CTRL) | IPG_MC_TX_ENABLE) &
965 IPG_MC_RSVD_MASK, MAC_CTRL);
966 }
967
968 /* Maximum collisions, re-enable transmitter. */
969 if (txstatusdword & IPG_TS_TX_MAX_COLL) {
970 IPG_DEBUG_MSG("Maximum collisions on transmit.\n");
971 ipg_w32((ipg_r32(MAC_CTRL) | IPG_MC_TX_ENABLE) &
972 IPG_MC_RSVD_MASK, MAC_CTRL);
973 }
974
975 /* Transmit underrun, reset and re-enable
976 * transmitter.
977 */
978 if (txstatusdword & IPG_TS_TX_UNDERRUN) {
979 IPG_DEBUG_MSG("Transmitter underrun.\n");
980 sp->stats.tx_fifo_errors++;
981 ipg_reset(dev, IPG_AC_TX_RESET | IPG_AC_DMA |
982 IPG_AC_NETWORK | IPG_AC_FIFO);
983
984 /* Re-configure after DMA reset. */
985 if (ipg_io_config(dev) < 0) {
986 printk(KERN_INFO
987 "%s: Error during re-configuration.\n",
988 dev->name);
989 }
990 init_tfdlist(dev);
991
992 ipg_w32((ipg_r32(MAC_CTRL) | IPG_MC_TX_ENABLE) &
993 IPG_MC_RSVD_MASK, MAC_CTRL);
994 }
995 }
996
997 ipg_nic_txfree(dev);
998}
999
1000/* Provides statistical information about the IPG NIC. */
Adrian Bunk96fd74b2007-10-24 18:23:19 +02001001static struct net_device_stats *ipg_nic_get_stats(struct net_device *dev)
Francois Romieu1202d6f2007-09-17 17:13:55 -07001002{
1003 struct ipg_nic_private *sp = netdev_priv(dev);
1004 void __iomem *ioaddr = sp->ioaddr;
1005 u16 temp1;
1006 u16 temp2;
1007
1008 IPG_DEBUG_MSG("_nic_get_stats\n");
1009
1010 /* Check to see if the NIC has been initialized via nic_open,
1011 * before trying to read statistic registers.
1012 */
1013 if (!test_bit(__LINK_STATE_START, &dev->state))
1014 return &sp->stats;
1015
1016 sp->stats.rx_packets += ipg_r32(IPG_FRAMESRCVDOK);
1017 sp->stats.tx_packets += ipg_r32(IPG_FRAMESXMTDOK);
1018 sp->stats.rx_bytes += ipg_r32(IPG_OCTETRCVOK);
1019 sp->stats.tx_bytes += ipg_r32(IPG_OCTETXMTOK);
1020 temp1 = ipg_r16(IPG_FRAMESLOSTRXERRORS);
1021 sp->stats.rx_errors += temp1;
1022 sp->stats.rx_missed_errors += temp1;
1023 temp1 = ipg_r32(IPG_SINGLECOLFRAMES) + ipg_r32(IPG_MULTICOLFRAMES) +
1024 ipg_r32(IPG_LATECOLLISIONS);
1025 temp2 = ipg_r16(IPG_CARRIERSENSEERRORS);
1026 sp->stats.collisions += temp1;
1027 sp->stats.tx_dropped += ipg_r16(IPG_FRAMESABORTXSCOLLS);
1028 sp->stats.tx_errors += ipg_r16(IPG_FRAMESWEXDEFERRAL) +
1029 ipg_r32(IPG_FRAMESWDEFERREDXMT) + temp1 + temp2;
1030 sp->stats.multicast += ipg_r32(IPG_MCSTOCTETRCVDOK);
1031
1032 /* detailed tx_errors */
1033 sp->stats.tx_carrier_errors += temp2;
1034
1035 /* detailed rx_errors */
1036 sp->stats.rx_length_errors += ipg_r16(IPG_INRANGELENGTHERRORS) +
1037 ipg_r16(IPG_FRAMETOOLONGERRRORS);
1038 sp->stats.rx_crc_errors += ipg_r16(IPG_FRAMECHECKSEQERRORS);
1039
1040 /* Unutilized IPG statistic registers. */
1041 ipg_r32(IPG_MCSTFRAMESRCVDOK);
1042
1043 return &sp->stats;
1044}
1045
1046/* Restore used receive buffers. */
1047static int ipg_nic_rxrestore(struct net_device *dev)
1048{
1049 struct ipg_nic_private *sp = netdev_priv(dev);
1050 const unsigned int curr = sp->rx_current;
1051 unsigned int dirty = sp->rx_dirty;
1052
1053 IPG_DEBUG_MSG("_nic_rxrestore\n");
1054
1055 for (dirty = sp->rx_dirty; curr - dirty > 0; dirty++) {
1056 unsigned int entry = dirty % IPG_RFDLIST_LENGTH;
1057
1058 /* rx_copybreak may poke hole here and there. */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001059 if (sp->rx_buff[entry])
Francois Romieu1202d6f2007-09-17 17:13:55 -07001060 continue;
1061
1062 /* Generate a new receive buffer to replace the
1063 * current buffer (which will be released by the
1064 * Linux system).
1065 */
1066 if (ipg_get_rxbuff(dev, entry) < 0) {
1067 IPG_DEBUG_MSG("Cannot allocate new Rx buffer.\n");
1068
1069 break;
1070 }
1071
1072 /* Reset the RFS field. */
1073 sp->rxd[entry].rfs = 0x0000000000000000;
1074 }
1075 sp->rx_dirty = dirty;
1076
1077 return 0;
1078}
1079
Francois Romieu1202d6f2007-09-17 17:13:55 -07001080/* use jumboindex and jumbosize to control jumbo frame status
Pekka Enberg8da5bb72007-11-30 09:55:43 +02001081 * initial status is jumboindex=-1 and jumbosize=0
1082 * 1. jumboindex = -1 and jumbosize=0 : previous jumbo frame has been done.
1083 * 2. jumboindex != -1 and jumbosize != 0 : jumbo frame is not over size and receiving
1084 * 3. jumboindex = -1 and jumbosize != 0 : jumbo frame is over size, already dump
1085 * previous receiving and need to continue dumping the current one
1086 */
Francois Romieu1202d6f2007-09-17 17:13:55 -07001087enum {
Pekka Enberg9893ba12008-01-24 02:01:17 -08001088 NORMAL_PACKET,
1089 ERROR_PACKET
Francois Romieu1202d6f2007-09-17 17:13:55 -07001090};
1091
1092enum {
Pekka Enberg9893ba12008-01-24 02:01:17 -08001093 FRAME_NO_START_NO_END = 0,
1094 FRAME_WITH_START = 1,
1095 FRAME_WITH_END = 10,
1096 FRAME_WITH_START_WITH_END = 11
Francois Romieu1202d6f2007-09-17 17:13:55 -07001097};
1098
Pekka Enberg024f4d882008-06-23 14:35:16 +03001099static void ipg_nic_rx_free_skb(struct net_device *dev)
Francois Romieu1202d6f2007-09-17 17:13:55 -07001100{
1101 struct ipg_nic_private *sp = netdev_priv(dev);
1102 unsigned int entry = sp->rx_current % IPG_RFDLIST_LENGTH;
1103
Pekka Enberg9893ba12008-01-24 02:01:17 -08001104 if (sp->rx_buff[entry]) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001105 struct ipg_rx *rxfd = sp->rxd + entry;
1106
1107 pci_unmap_single(sp->pdev,
1108 le64_to_cpu(rxfd->frag_info & ~IPG_RFI_FRAGLEN),
1109 sp->rx_buf_sz, PCI_DMA_FROMDEVICE);
Pekka Enberg9893ba12008-01-24 02:01:17 -08001110 dev_kfree_skb_irq(sp->rx_buff[entry]);
1111 sp->rx_buff[entry] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001112 }
1113}
1114
Pekka Enberg024f4d882008-06-23 14:35:16 +03001115static int ipg_nic_rx_check_frame_type(struct net_device *dev)
Francois Romieu1202d6f2007-09-17 17:13:55 -07001116{
1117 struct ipg_nic_private *sp = netdev_priv(dev);
1118 struct ipg_rx *rxfd = sp->rxd + (sp->rx_current % IPG_RFDLIST_LENGTH);
Pekka Enberg9893ba12008-01-24 02:01:17 -08001119 int type = FRAME_NO_START_NO_END;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001120
1121 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_FRAMESTART)
Pekka Enberg9893ba12008-01-24 02:01:17 -08001122 type += FRAME_WITH_START;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001123 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_FRAMEEND)
Pekka Enberg9893ba12008-01-24 02:01:17 -08001124 type += FRAME_WITH_END;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001125 return type;
1126}
1127
Pekka Enberg024f4d882008-06-23 14:35:16 +03001128static int ipg_nic_rx_check_error(struct net_device *dev)
Francois Romieu1202d6f2007-09-17 17:13:55 -07001129{
1130 struct ipg_nic_private *sp = netdev_priv(dev);
1131 unsigned int entry = sp->rx_current % IPG_RFDLIST_LENGTH;
1132 struct ipg_rx *rxfd = sp->rxd + entry;
1133
1134 if (IPG_DROP_ON_RX_ETH_ERRORS && (le64_to_cpu(rxfd->rfs) &
1135 (IPG_RFS_RXFIFOOVERRUN | IPG_RFS_RXRUNTFRAME |
1136 IPG_RFS_RXALIGNMENTERROR | IPG_RFS_RXFCSERROR |
1137 IPG_RFS_RXOVERSIZEDFRAME | IPG_RFS_RXLENGTHERROR))) {
1138 IPG_DEBUG_MSG("Rx error, RFS = %16.16lx\n",
1139 (unsigned long) rxfd->rfs);
1140
1141 /* Increment general receive error statistic. */
1142 sp->stats.rx_errors++;
1143
1144 /* Increment detailed receive error statistics. */
1145 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_RXFIFOOVERRUN) {
1146 IPG_DEBUG_MSG("RX FIFO overrun occured.\n");
1147
1148 sp->stats.rx_fifo_errors++;
1149 }
1150
1151 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_RXRUNTFRAME) {
1152 IPG_DEBUG_MSG("RX runt occured.\n");
1153 sp->stats.rx_length_errors++;
1154 }
1155
1156 /* Do nothing for IPG_RFS_RXOVERSIZEDFRAME,
1157 * error count handled by a IPG statistic register.
1158 */
1159
1160 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_RXALIGNMENTERROR) {
1161 IPG_DEBUG_MSG("RX alignment error occured.\n");
1162 sp->stats.rx_frame_errors++;
1163 }
1164
1165 /* Do nothing for IPG_RFS_RXFCSERROR, error count
1166 * handled by a IPG statistic register.
1167 */
1168
1169 /* Free the memory associated with the RX
1170 * buffer since it is erroneous and we will
1171 * not pass it to higher layer processes.
1172 */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001173 if (sp->rx_buff[entry]) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001174 pci_unmap_single(sp->pdev,
1175 le64_to_cpu(rxfd->frag_info & ~IPG_RFI_FRAGLEN),
1176 sp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1177
Pekka Enberg9893ba12008-01-24 02:01:17 -08001178 dev_kfree_skb_irq(sp->rx_buff[entry]);
1179 sp->rx_buff[entry] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001180 }
Pekka Enberg9893ba12008-01-24 02:01:17 -08001181 return ERROR_PACKET;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001182 }
Pekka Enberg9893ba12008-01-24 02:01:17 -08001183 return NORMAL_PACKET;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001184}
1185
1186static void ipg_nic_rx_with_start_and_end(struct net_device *dev,
1187 struct ipg_nic_private *sp,
1188 struct ipg_rx *rxfd, unsigned entry)
1189{
Pekka Enberg9893ba12008-01-24 02:01:17 -08001190 struct ipg_jumbo *jumbo = &sp->jumbo;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001191 struct sk_buff *skb;
1192 int framelen;
1193
Pekka Enberg9893ba12008-01-24 02:01:17 -08001194 if (jumbo->found_start) {
Pekka Enberg85d68a52007-11-30 09:53:51 +02001195 dev_kfree_skb_irq(jumbo->skb);
Pekka Enberg9893ba12008-01-24 02:01:17 -08001196 jumbo->found_start = 0;
1197 jumbo->current_size = 0;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001198 jumbo->skb = NULL;
1199 }
1200
Pekka Enberg8da5bb72007-11-30 09:55:43 +02001201 /* 1: found error, 0 no error */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001202 if (ipg_nic_rx_check_error(dev) != NORMAL_PACKET)
Francois Romieu1202d6f2007-09-17 17:13:55 -07001203 return;
1204
Pekka Enberg9893ba12008-01-24 02:01:17 -08001205 skb = sp->rx_buff[entry];
Francois Romieu1202d6f2007-09-17 17:13:55 -07001206 if (!skb)
1207 return;
1208
Pekka Enberg8da5bb72007-11-30 09:55:43 +02001209 /* accept this frame and send to upper layer */
Francois Romieu1202d6f2007-09-17 17:13:55 -07001210 framelen = le64_to_cpu(rxfd->rfs) & IPG_RFS_RXFRAMELEN;
Pekka Enberg18a9cdb2008-06-23 14:36:00 +03001211 if (framelen > sp->rxfrag_size)
1212 framelen = sp->rxfrag_size;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001213
1214 skb_put(skb, framelen);
1215 skb->protocol = eth_type_trans(skb, dev);
1216 skb->ip_summed = CHECKSUM_NONE;
1217 netif_rx(skb);
1218 dev->last_rx = jiffies;
Pekka Enberg9893ba12008-01-24 02:01:17 -08001219 sp->rx_buff[entry] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001220}
1221
1222static void ipg_nic_rx_with_start(struct net_device *dev,
1223 struct ipg_nic_private *sp,
1224 struct ipg_rx *rxfd, unsigned entry)
1225{
Pekka Enberg9893ba12008-01-24 02:01:17 -08001226 struct ipg_jumbo *jumbo = &sp->jumbo;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001227 struct pci_dev *pdev = sp->pdev;
1228 struct sk_buff *skb;
1229
Pekka Enberg8da5bb72007-11-30 09:55:43 +02001230 /* 1: found error, 0 no error */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001231 if (ipg_nic_rx_check_error(dev) != NORMAL_PACKET)
Francois Romieu1202d6f2007-09-17 17:13:55 -07001232 return;
1233
Pekka Enberg8da5bb72007-11-30 09:55:43 +02001234 /* accept this frame and send to upper layer */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001235 skb = sp->rx_buff[entry];
Francois Romieu1202d6f2007-09-17 17:13:55 -07001236 if (!skb)
1237 return;
1238
Pekka Enberg9893ba12008-01-24 02:01:17 -08001239 if (jumbo->found_start)
Pekka Enberg85d68a52007-11-30 09:53:51 +02001240 dev_kfree_skb_irq(jumbo->skb);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001241
1242 pci_unmap_single(pdev, le64_to_cpu(rxfd->frag_info & ~IPG_RFI_FRAGLEN),
1243 sp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1244
Pekka Enberg18a9cdb2008-06-23 14:36:00 +03001245 skb_put(skb, sp->rxfrag_size);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001246
Pekka Enberg9893ba12008-01-24 02:01:17 -08001247 jumbo->found_start = 1;
Pekka Enberg18a9cdb2008-06-23 14:36:00 +03001248 jumbo->current_size = sp->rxfrag_size;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001249 jumbo->skb = skb;
1250
Pekka Enberg9893ba12008-01-24 02:01:17 -08001251 sp->rx_buff[entry] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001252 dev->last_rx = jiffies;
1253}
1254
1255static void ipg_nic_rx_with_end(struct net_device *dev,
1256 struct ipg_nic_private *sp,
1257 struct ipg_rx *rxfd, unsigned entry)
1258{
Pekka Enberg9893ba12008-01-24 02:01:17 -08001259 struct ipg_jumbo *jumbo = &sp->jumbo;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001260
Pekka Enberg8da5bb72007-11-30 09:55:43 +02001261 /* 1: found error, 0 no error */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001262 if (ipg_nic_rx_check_error(dev) == NORMAL_PACKET) {
1263 struct sk_buff *skb = sp->rx_buff[entry];
Francois Romieu1202d6f2007-09-17 17:13:55 -07001264
1265 if (!skb)
1266 return;
1267
Pekka Enberg9893ba12008-01-24 02:01:17 -08001268 if (jumbo->found_start) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001269 int framelen, endframelen;
1270
1271 framelen = le64_to_cpu(rxfd->rfs) & IPG_RFS_RXFRAMELEN;
1272
Pekka Enbergecfecfb2008-06-23 14:34:29 +03001273 endframelen = framelen - jumbo->current_size;
Pekka Enberg39f20582008-06-23 14:36:18 +03001274 if (framelen > sp->rxsupport_size)
Pekka Enberg85d68a52007-11-30 09:53:51 +02001275 dev_kfree_skb_irq(jumbo->skb);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001276 else {
Pekka Enbergecfecfb2008-06-23 14:34:29 +03001277 memcpy(skb_put(jumbo->skb, endframelen),
1278 skb->data, endframelen);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001279
1280 jumbo->skb->protocol =
1281 eth_type_trans(jumbo->skb, dev);
1282
1283 jumbo->skb->ip_summed = CHECKSUM_NONE;
1284 netif_rx(jumbo->skb);
1285 }
1286 }
1287
1288 dev->last_rx = jiffies;
Pekka Enberg9893ba12008-01-24 02:01:17 -08001289 jumbo->found_start = 0;
1290 jumbo->current_size = 0;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001291 jumbo->skb = NULL;
1292
1293 ipg_nic_rx_free_skb(dev);
1294 } else {
Pekka Enberg85d68a52007-11-30 09:53:51 +02001295 dev_kfree_skb_irq(jumbo->skb);
Pekka Enberg9893ba12008-01-24 02:01:17 -08001296 jumbo->found_start = 0;
1297 jumbo->current_size = 0;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001298 jumbo->skb = NULL;
1299 }
1300}
1301
1302static void ipg_nic_rx_no_start_no_end(struct net_device *dev,
1303 struct ipg_nic_private *sp,
1304 struct ipg_rx *rxfd, unsigned entry)
1305{
Pekka Enberg9893ba12008-01-24 02:01:17 -08001306 struct ipg_jumbo *jumbo = &sp->jumbo;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001307
Pekka Enberg8da5bb72007-11-30 09:55:43 +02001308 /* 1: found error, 0 no error */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001309 if (ipg_nic_rx_check_error(dev) == NORMAL_PACKET) {
1310 struct sk_buff *skb = sp->rx_buff[entry];
Francois Romieu1202d6f2007-09-17 17:13:55 -07001311
1312 if (skb) {
Pekka Enberg9893ba12008-01-24 02:01:17 -08001313 if (jumbo->found_start) {
Pekka Enberg18a9cdb2008-06-23 14:36:00 +03001314 jumbo->current_size += sp->rxfrag_size;
Pekka Enberg39f20582008-06-23 14:36:18 +03001315 if (jumbo->current_size <= sp->rxsupport_size) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001316 memcpy(skb_put(jumbo->skb,
Pekka Enberg18a9cdb2008-06-23 14:36:00 +03001317 sp->rxfrag_size),
1318 skb->data, sp->rxfrag_size);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001319 }
1320 }
1321 dev->last_rx = jiffies;
1322 ipg_nic_rx_free_skb(dev);
1323 }
1324 } else {
Pekka Enberg85d68a52007-11-30 09:53:51 +02001325 dev_kfree_skb_irq(jumbo->skb);
Pekka Enberg9893ba12008-01-24 02:01:17 -08001326 jumbo->found_start = 0;
1327 jumbo->current_size = 0;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001328 jumbo->skb = NULL;
1329 }
1330}
1331
Pekka Enberg024f4d882008-06-23 14:35:16 +03001332static int ipg_nic_rx_jumbo(struct net_device *dev)
Francois Romieu1202d6f2007-09-17 17:13:55 -07001333{
1334 struct ipg_nic_private *sp = netdev_priv(dev);
1335 unsigned int curr = sp->rx_current;
1336 void __iomem *ioaddr = sp->ioaddr;
1337 unsigned int i;
1338
1339 IPG_DEBUG_MSG("_nic_rx\n");
1340
1341 for (i = 0; i < IPG_MAXRFDPROCESS_COUNT; i++, curr++) {
1342 unsigned int entry = curr % IPG_RFDLIST_LENGTH;
1343 struct ipg_rx *rxfd = sp->rxd + entry;
1344
1345 if (!(rxfd->rfs & le64_to_cpu(IPG_RFS_RFDDONE)))
1346 break;
1347
1348 switch (ipg_nic_rx_check_frame_type(dev)) {
Pekka Enberg9893ba12008-01-24 02:01:17 -08001349 case FRAME_WITH_START_WITH_END:
Pekka Enbergecfecfb2008-06-23 14:34:29 +03001350 ipg_nic_rx_with_start_and_end(dev, sp, rxfd, entry);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001351 break;
Pekka Enberg9893ba12008-01-24 02:01:17 -08001352 case FRAME_WITH_START:
Pekka Enbergecfecfb2008-06-23 14:34:29 +03001353 ipg_nic_rx_with_start(dev, sp, rxfd, entry);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001354 break;
Pekka Enberg9893ba12008-01-24 02:01:17 -08001355 case FRAME_WITH_END:
Pekka Enbergecfecfb2008-06-23 14:34:29 +03001356 ipg_nic_rx_with_end(dev, sp, rxfd, entry);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001357 break;
Pekka Enberg9893ba12008-01-24 02:01:17 -08001358 case FRAME_NO_START_NO_END:
Pekka Enbergecfecfb2008-06-23 14:34:29 +03001359 ipg_nic_rx_no_start_no_end(dev, sp, rxfd, entry);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001360 break;
1361 }
1362 }
1363
1364 sp->rx_current = curr;
1365
1366 if (i == IPG_MAXRFDPROCESS_COUNT) {
1367 /* There are more RFDs to process, however the
1368 * allocated amount of RFD processing time has
1369 * expired. Assert Interrupt Requested to make
1370 * sure we come back to process the remaining RFDs.
1371 */
1372 ipg_w32(ipg_r32(ASIC_CTRL) | IPG_AC_INT_REQUEST, ASIC_CTRL);
1373 }
1374
1375 ipg_nic_rxrestore(dev);
1376
1377 return 0;
1378}
1379
Francois Romieu1202d6f2007-09-17 17:13:55 -07001380static int ipg_nic_rx(struct net_device *dev)
1381{
1382 /* Transfer received Ethernet frames to higher network layers. */
1383 struct ipg_nic_private *sp = netdev_priv(dev);
1384 unsigned int curr = sp->rx_current;
1385 void __iomem *ioaddr = sp->ioaddr;
1386 struct ipg_rx *rxfd;
1387 unsigned int i;
1388
1389 IPG_DEBUG_MSG("_nic_rx\n");
1390
1391#define __RFS_MASK \
1392 cpu_to_le64(IPG_RFS_RFDDONE | IPG_RFS_FRAMESTART | IPG_RFS_FRAMEEND)
1393
1394 for (i = 0; i < IPG_MAXRFDPROCESS_COUNT; i++, curr++) {
1395 unsigned int entry = curr % IPG_RFDLIST_LENGTH;
Pekka Enberg9893ba12008-01-24 02:01:17 -08001396 struct sk_buff *skb = sp->rx_buff[entry];
Francois Romieu1202d6f2007-09-17 17:13:55 -07001397 unsigned int framelen;
1398
1399 rxfd = sp->rxd + entry;
1400
1401 if (((rxfd->rfs & __RFS_MASK) != __RFS_MASK) || !skb)
1402 break;
1403
1404 /* Get received frame length. */
1405 framelen = le64_to_cpu(rxfd->rfs) & IPG_RFS_RXFRAMELEN;
1406
1407 /* Check for jumbo frame arrival with too small
1408 * RXFRAG_SIZE.
1409 */
Pekka Enberg18a9cdb2008-06-23 14:36:00 +03001410 if (framelen > sp->rxfrag_size) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001411 IPG_DEBUG_MSG
1412 ("RFS FrameLen > allocated fragment size.\n");
1413
Pekka Enberg18a9cdb2008-06-23 14:36:00 +03001414 framelen = sp->rxfrag_size;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001415 }
1416
Al Viro325a8072007-10-14 19:41:29 +01001417 if ((IPG_DROP_ON_RX_ETH_ERRORS && (le64_to_cpu(rxfd->rfs) &
Francois Romieu1202d6f2007-09-17 17:13:55 -07001418 (IPG_RFS_RXFIFOOVERRUN | IPG_RFS_RXRUNTFRAME |
1419 IPG_RFS_RXALIGNMENTERROR | IPG_RFS_RXFCSERROR |
Al Viro325a8072007-10-14 19:41:29 +01001420 IPG_RFS_RXOVERSIZEDFRAME | IPG_RFS_RXLENGTHERROR)))) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001421
1422 IPG_DEBUG_MSG("Rx error, RFS = %16.16lx\n",
1423 (unsigned long int) rxfd->rfs);
1424
1425 /* Increment general receive error statistic. */
1426 sp->stats.rx_errors++;
1427
1428 /* Increment detailed receive error statistics. */
Al Viro325a8072007-10-14 19:41:29 +01001429 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_RXFIFOOVERRUN) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001430 IPG_DEBUG_MSG("RX FIFO overrun occured.\n");
1431 sp->stats.rx_fifo_errors++;
1432 }
1433
Al Viro325a8072007-10-14 19:41:29 +01001434 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_RXRUNTFRAME) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001435 IPG_DEBUG_MSG("RX runt occured.\n");
1436 sp->stats.rx_length_errors++;
1437 }
1438
Al Viro325a8072007-10-14 19:41:29 +01001439 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_RXOVERSIZEDFRAME) ;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001440 /* Do nothing, error count handled by a IPG
1441 * statistic register.
1442 */
1443
Al Viro325a8072007-10-14 19:41:29 +01001444 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_RXALIGNMENTERROR) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001445 IPG_DEBUG_MSG("RX alignment error occured.\n");
1446 sp->stats.rx_frame_errors++;
1447 }
1448
Al Viro325a8072007-10-14 19:41:29 +01001449 if (le64_to_cpu(rxfd->rfs) & IPG_RFS_RXFCSERROR) ;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001450 /* Do nothing, error count handled by a IPG
1451 * statistic register.
1452 */
1453
1454 /* Free the memory associated with the RX
1455 * buffer since it is erroneous and we will
1456 * not pass it to higher layer processes.
1457 */
1458 if (skb) {
Al Viro325a8072007-10-14 19:41:29 +01001459 __le64 info = rxfd->frag_info;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001460
1461 pci_unmap_single(sp->pdev,
Al Viro325a8072007-10-14 19:41:29 +01001462 le64_to_cpu(info) & ~IPG_RFI_FRAGLEN,
Francois Romieu1202d6f2007-09-17 17:13:55 -07001463 sp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1464
Pekka Enberg85d68a52007-11-30 09:53:51 +02001465 dev_kfree_skb_irq(skb);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001466 }
1467 } else {
1468
1469 /* Adjust the new buffer length to accomodate the size
1470 * of the received frame.
1471 */
1472 skb_put(skb, framelen);
1473
1474 /* Set the buffer's protocol field to Ethernet. */
1475 skb->protocol = eth_type_trans(skb, dev);
1476
Pekka Enberg6d3b2cb2007-11-30 09:54:33 +02001477 /* The IPG encountered an error with (or
1478 * there were no) IP/TCP/UDP checksums.
1479 * This may or may not indicate an invalid
1480 * IP/TCP/UDP frame was received. Let the
1481 * upper layer decide.
Francois Romieu1202d6f2007-09-17 17:13:55 -07001482 */
Pekka Enberg6d3b2cb2007-11-30 09:54:33 +02001483 skb->ip_summed = CHECKSUM_NONE;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001484
1485 /* Hand off frame for higher layer processing.
1486 * The function netif_rx() releases the sk_buff
1487 * when processing completes.
1488 */
1489 netif_rx(skb);
1490
1491 /* Record frame receive time (jiffies = Linux
1492 * kernel current time stamp).
1493 */
1494 dev->last_rx = jiffies;
1495 }
1496
1497 /* Assure RX buffer is not reused by IPG. */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001498 sp->rx_buff[entry] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001499 }
1500
1501 /*
1502 * If there are more RFDs to proces and the allocated amount of RFD
1503 * processing time has expired, assert Interrupt Requested to make
1504 * sure we come back to process the remaining RFDs.
1505 */
1506 if (i == IPG_MAXRFDPROCESS_COUNT)
1507 ipg_w32(ipg_r32(ASIC_CTRL) | IPG_AC_INT_REQUEST, ASIC_CTRL);
1508
1509#ifdef IPG_DEBUG
1510 /* Check if the RFD list contained no receive frame data. */
1511 if (!i)
1512 sp->EmptyRFDListCount++;
1513#endif
Al Viro325a8072007-10-14 19:41:29 +01001514 while ((le64_to_cpu(rxfd->rfs) & IPG_RFS_RFDDONE) &&
1515 !((le64_to_cpu(rxfd->rfs) & IPG_RFS_FRAMESTART) &&
1516 (le64_to_cpu(rxfd->rfs) & IPG_RFS_FRAMEEND))) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001517 unsigned int entry = curr++ % IPG_RFDLIST_LENGTH;
1518
1519 rxfd = sp->rxd + entry;
1520
1521 IPG_DEBUG_MSG("Frame requires multiple RFDs.\n");
1522
1523 /* An unexpected event, additional code needed to handle
1524 * properly. So for the time being, just disregard the
1525 * frame.
1526 */
1527
1528 /* Free the memory associated with the RX
1529 * buffer since it is erroneous and we will
1530 * not pass it to higher layer processes.
1531 */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001532 if (sp->rx_buff[entry]) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001533 pci_unmap_single(sp->pdev,
Al Viro325a8072007-10-14 19:41:29 +01001534 le64_to_cpu(rxfd->frag_info) & ~IPG_RFI_FRAGLEN,
Francois Romieu1202d6f2007-09-17 17:13:55 -07001535 sp->rx_buf_sz, PCI_DMA_FROMDEVICE);
Pekka Enberg9893ba12008-01-24 02:01:17 -08001536 dev_kfree_skb_irq(sp->rx_buff[entry]);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001537 }
1538
1539 /* Assure RX buffer is not reused by IPG. */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001540 sp->rx_buff[entry] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001541 }
1542
1543 sp->rx_current = curr;
1544
1545 /* Check to see if there are a minimum number of used
1546 * RFDs before restoring any (should improve performance.)
1547 */
1548 if ((curr - sp->rx_dirty) >= IPG_MINUSEDRFDSTOFREE)
1549 ipg_nic_rxrestore(dev);
1550
1551 return 0;
1552}
Francois Romieu1202d6f2007-09-17 17:13:55 -07001553
1554static void ipg_reset_after_host_error(struct work_struct *work)
1555{
1556 struct ipg_nic_private *sp =
1557 container_of(work, struct ipg_nic_private, task.work);
1558 struct net_device *dev = sp->dev;
1559
1560 IPG_DDEBUG_MSG("DMACtrl = %8.8x\n", ioread32(sp->ioaddr + IPG_DMACTRL));
1561
1562 /*
1563 * Acknowledge HostError interrupt by resetting
1564 * IPG DMA and HOST.
1565 */
1566 ipg_reset(dev, IPG_AC_GLOBAL_RESET | IPG_AC_HOST | IPG_AC_DMA);
1567
1568 init_rfdlist(dev);
1569 init_tfdlist(dev);
1570
1571 if (ipg_io_config(dev) < 0) {
1572 printk(KERN_INFO "%s: Cannot recover from PCI error.\n",
1573 dev->name);
1574 schedule_delayed_work(&sp->task, HZ);
1575 }
1576}
1577
1578static irqreturn_t ipg_interrupt_handler(int irq, void *dev_inst)
1579{
1580 struct net_device *dev = dev_inst;
1581 struct ipg_nic_private *sp = netdev_priv(dev);
1582 void __iomem *ioaddr = sp->ioaddr;
1583 unsigned int handled = 0;
1584 u16 status;
1585
1586 IPG_DEBUG_MSG("_interrupt_handler\n");
1587
Pekka Enberg024f4d882008-06-23 14:35:16 +03001588 if (sp->is_jumbo)
1589 ipg_nic_rxrestore(dev);
1590
Francois Romieu227bc242008-01-10 23:25:30 +01001591 spin_lock(&sp->lock);
1592
Francois Romieu1202d6f2007-09-17 17:13:55 -07001593 /* Get interrupt source information, and acknowledge
1594 * some (i.e. TxDMAComplete, RxDMAComplete, RxEarly,
1595 * IntRequested, MacControlFrame, LinkEvent) interrupts
1596 * if issued. Also, all IPG interrupts are disabled by
1597 * reading IntStatusAck.
1598 */
1599 status = ipg_r16(INT_STATUS_ACK);
1600
1601 IPG_DEBUG_MSG("IntStatusAck = %4.4x\n", status);
1602
1603 /* Shared IRQ of remove event. */
1604 if (!(status & IPG_IS_RSVD_MASK))
1605 goto out_enable;
1606
1607 handled = 1;
1608
1609 if (unlikely(!netif_running(dev)))
Francois Romieu227bc242008-01-10 23:25:30 +01001610 goto out_unlock;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001611
1612 /* If RFDListEnd interrupt, restore all used RFDs. */
1613 if (status & IPG_IS_RFD_LIST_END) {
1614 IPG_DEBUG_MSG("RFDListEnd Interrupt.\n");
1615
1616 /* The RFD list end indicates an RFD was encountered
1617 * with a 0 NextPtr, or with an RFDDone bit set to 1
1618 * (indicating the RFD is not read for use by the
1619 * IPG.) Try to restore all RFDs.
1620 */
1621 ipg_nic_rxrestore(dev);
1622
1623#ifdef IPG_DEBUG
1624 /* Increment the RFDlistendCount counter. */
1625 sp->RFDlistendCount++;
1626#endif
1627 }
1628
1629 /* If RFDListEnd, RxDMAPriority, RxDMAComplete, or
1630 * IntRequested interrupt, process received frames. */
1631 if ((status & IPG_IS_RX_DMA_PRIORITY) ||
1632 (status & IPG_IS_RFD_LIST_END) ||
1633 (status & IPG_IS_RX_DMA_COMPLETE) ||
1634 (status & IPG_IS_INT_REQUESTED)) {
1635#ifdef IPG_DEBUG
1636 /* Increment the RFD list checked counter if interrupted
1637 * only to check the RFD list. */
1638 if (status & (~(IPG_IS_RX_DMA_PRIORITY | IPG_IS_RFD_LIST_END |
1639 IPG_IS_RX_DMA_COMPLETE | IPG_IS_INT_REQUESTED) &
1640 (IPG_IS_HOST_ERROR | IPG_IS_TX_DMA_COMPLETE |
1641 IPG_IS_LINK_EVENT | IPG_IS_TX_COMPLETE |
1642 IPG_IS_UPDATE_STATS)))
1643 sp->RFDListCheckedCount++;
1644#endif
1645
Pekka Enberg024f4d882008-06-23 14:35:16 +03001646 if (sp->is_jumbo)
1647 ipg_nic_rx_jumbo(dev);
1648 else
1649 ipg_nic_rx(dev);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001650 }
1651
1652 /* If TxDMAComplete interrupt, free used TFDs. */
1653 if (status & IPG_IS_TX_DMA_COMPLETE)
1654 ipg_nic_txfree(dev);
1655
1656 /* TxComplete interrupts indicate one of numerous actions.
1657 * Determine what action to take based on TXSTATUS register.
1658 */
1659 if (status & IPG_IS_TX_COMPLETE)
1660 ipg_nic_txcleanup(dev);
1661
1662 /* If UpdateStats interrupt, update Linux Ethernet statistics */
1663 if (status & IPG_IS_UPDATE_STATS)
1664 ipg_nic_get_stats(dev);
1665
1666 /* If HostError interrupt, reset IPG. */
1667 if (status & IPG_IS_HOST_ERROR) {
1668 IPG_DDEBUG_MSG("HostError Interrupt\n");
1669
1670 schedule_delayed_work(&sp->task, 0);
1671 }
1672
1673 /* If LinkEvent interrupt, resolve autonegotiation. */
1674 if (status & IPG_IS_LINK_EVENT) {
1675 if (ipg_config_autoneg(dev) < 0)
1676 printk(KERN_INFO "%s: Auto-negotiation error.\n",
1677 dev->name);
1678 }
1679
1680 /* If MACCtrlFrame interrupt, do nothing. */
1681 if (status & IPG_IS_MAC_CTRL_FRAME)
1682 IPG_DEBUG_MSG("MACCtrlFrame interrupt.\n");
1683
1684 /* If RxComplete interrupt, do nothing. */
1685 if (status & IPG_IS_RX_COMPLETE)
1686 IPG_DEBUG_MSG("RxComplete interrupt.\n");
1687
1688 /* If RxEarly interrupt, do nothing. */
1689 if (status & IPG_IS_RX_EARLY)
1690 IPG_DEBUG_MSG("RxEarly interrupt.\n");
1691
1692out_enable:
1693 /* Re-enable IPG interrupts. */
1694 ipg_w16(IPG_IE_TX_DMA_COMPLETE | IPG_IE_RX_DMA_COMPLETE |
1695 IPG_IE_HOST_ERROR | IPG_IE_INT_REQUESTED | IPG_IE_TX_COMPLETE |
1696 IPG_IE_LINK_EVENT | IPG_IE_UPDATE_STATS, INT_ENABLE);
Francois Romieu227bc242008-01-10 23:25:30 +01001697out_unlock:
Francois Romieu1202d6f2007-09-17 17:13:55 -07001698 spin_unlock(&sp->lock);
Francois Romieu227bc242008-01-10 23:25:30 +01001699
Francois Romieu1202d6f2007-09-17 17:13:55 -07001700 return IRQ_RETVAL(handled);
1701}
1702
1703static void ipg_rx_clear(struct ipg_nic_private *sp)
1704{
1705 unsigned int i;
1706
1707 for (i = 0; i < IPG_RFDLIST_LENGTH; i++) {
Pekka Enberg9893ba12008-01-24 02:01:17 -08001708 if (sp->rx_buff[i]) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001709 struct ipg_rx *rxfd = sp->rxd + i;
1710
Pekka Enberg9893ba12008-01-24 02:01:17 -08001711 dev_kfree_skb_irq(sp->rx_buff[i]);
1712 sp->rx_buff[i] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001713 pci_unmap_single(sp->pdev,
Al Viro325a8072007-10-14 19:41:29 +01001714 le64_to_cpu(rxfd->frag_info) & ~IPG_RFI_FRAGLEN,
Francois Romieu1202d6f2007-09-17 17:13:55 -07001715 sp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1716 }
1717 }
1718}
1719
1720static void ipg_tx_clear(struct ipg_nic_private *sp)
1721{
1722 unsigned int i;
1723
1724 for (i = 0; i < IPG_TFDLIST_LENGTH; i++) {
Pekka Enberg9893ba12008-01-24 02:01:17 -08001725 if (sp->tx_buff[i]) {
Francois Romieu1202d6f2007-09-17 17:13:55 -07001726 struct ipg_tx *txfd = sp->txd + i;
1727
1728 pci_unmap_single(sp->pdev,
Al Viro325a8072007-10-14 19:41:29 +01001729 le64_to_cpu(txfd->frag_info) & ~IPG_TFI_FRAGLEN,
Pekka Enberg9893ba12008-01-24 02:01:17 -08001730 sp->tx_buff[i]->len, PCI_DMA_TODEVICE);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001731
Pekka Enberg9893ba12008-01-24 02:01:17 -08001732 dev_kfree_skb_irq(sp->tx_buff[i]);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001733
Pekka Enberg9893ba12008-01-24 02:01:17 -08001734 sp->tx_buff[i] = NULL;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001735 }
1736 }
1737}
1738
1739static int ipg_nic_open(struct net_device *dev)
1740{
1741 struct ipg_nic_private *sp = netdev_priv(dev);
1742 void __iomem *ioaddr = sp->ioaddr;
1743 struct pci_dev *pdev = sp->pdev;
1744 int rc;
1745
1746 IPG_DEBUG_MSG("_nic_open\n");
1747
Pekka Enberg39f20582008-06-23 14:36:18 +03001748 sp->rx_buf_sz = sp->rxsupport_size;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001749
1750 /* Check for interrupt line conflicts, and request interrupt
1751 * line for IPG.
1752 *
1753 * IMPORTANT: Disable IPG interrupts prior to registering
1754 * IRQ.
1755 */
1756 ipg_w16(0x0000, INT_ENABLE);
1757
1758 /* Register the interrupt line to be used by the IPG within
1759 * the Linux system.
1760 */
1761 rc = request_irq(pdev->irq, &ipg_interrupt_handler, IRQF_SHARED,
1762 dev->name, dev);
1763 if (rc < 0) {
1764 printk(KERN_INFO "%s: Error when requesting interrupt.\n",
1765 dev->name);
1766 goto out;
1767 }
1768
1769 dev->irq = pdev->irq;
1770
1771 rc = -ENOMEM;
1772
1773 sp->rxd = dma_alloc_coherent(&pdev->dev, IPG_RX_RING_BYTES,
1774 &sp->rxd_map, GFP_KERNEL);
1775 if (!sp->rxd)
1776 goto err_free_irq_0;
1777
1778 sp->txd = dma_alloc_coherent(&pdev->dev, IPG_TX_RING_BYTES,
1779 &sp->txd_map, GFP_KERNEL);
1780 if (!sp->txd)
1781 goto err_free_rx_1;
1782
1783 rc = init_rfdlist(dev);
1784 if (rc < 0) {
1785 printk(KERN_INFO "%s: Error during configuration.\n",
1786 dev->name);
1787 goto err_free_tx_2;
1788 }
1789
1790 init_tfdlist(dev);
1791
1792 rc = ipg_io_config(dev);
1793 if (rc < 0) {
1794 printk(KERN_INFO "%s: Error during configuration.\n",
1795 dev->name);
1796 goto err_release_tfdlist_3;
1797 }
1798
1799 /* Resolve autonegotiation. */
1800 if (ipg_config_autoneg(dev) < 0)
1801 printk(KERN_INFO "%s: Auto-negotiation error.\n", dev->name);
1802
Francois Romieu1202d6f2007-09-17 17:13:55 -07001803 /* initialize JUMBO Frame control variable */
Pekka Enberg9893ba12008-01-24 02:01:17 -08001804 sp->jumbo.found_start = 0;
1805 sp->jumbo.current_size = 0;
Pekka Enberge8399fe2008-06-23 14:34:50 +03001806 sp->jumbo.skb = NULL;
Pekka Enberg83042952008-06-23 14:35:37 +03001807
1808 if (IPG_TXFRAG_SIZE)
1809 dev->mtu = IPG_TXFRAG_SIZE;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001810
1811 /* Enable transmit and receive operation of the IPG. */
1812 ipg_w32((ipg_r32(MAC_CTRL) | IPG_MC_RX_ENABLE | IPG_MC_TX_ENABLE) &
1813 IPG_MC_RSVD_MASK, MAC_CTRL);
1814
1815 netif_start_queue(dev);
1816out:
1817 return rc;
1818
1819err_release_tfdlist_3:
1820 ipg_tx_clear(sp);
1821 ipg_rx_clear(sp);
1822err_free_tx_2:
1823 dma_free_coherent(&pdev->dev, IPG_TX_RING_BYTES, sp->txd, sp->txd_map);
1824err_free_rx_1:
1825 dma_free_coherent(&pdev->dev, IPG_RX_RING_BYTES, sp->rxd, sp->rxd_map);
1826err_free_irq_0:
1827 free_irq(pdev->irq, dev);
1828 goto out;
1829}
1830
1831static int ipg_nic_stop(struct net_device *dev)
1832{
1833 struct ipg_nic_private *sp = netdev_priv(dev);
1834 void __iomem *ioaddr = sp->ioaddr;
1835 struct pci_dev *pdev = sp->pdev;
1836
1837 IPG_DEBUG_MSG("_nic_stop\n");
1838
1839 netif_stop_queue(dev);
1840
1841 IPG_DDEBUG_MSG("RFDlistendCount = %i\n", sp->RFDlistendCount);
1842 IPG_DDEBUG_MSG("RFDListCheckedCount = %i\n", sp->rxdCheckedCount);
1843 IPG_DDEBUG_MSG("EmptyRFDListCount = %i\n", sp->EmptyRFDListCount);
1844 IPG_DUMPTFDLIST(dev);
1845
1846 do {
1847 (void) ipg_r16(INT_STATUS_ACK);
1848
1849 ipg_reset(dev, IPG_AC_GLOBAL_RESET | IPG_AC_HOST | IPG_AC_DMA);
1850
1851 synchronize_irq(pdev->irq);
1852 } while (ipg_r16(INT_ENABLE) & IPG_IE_RSVD_MASK);
1853
1854 ipg_rx_clear(sp);
1855
1856 ipg_tx_clear(sp);
1857
1858 pci_free_consistent(pdev, IPG_RX_RING_BYTES, sp->rxd, sp->rxd_map);
1859 pci_free_consistent(pdev, IPG_TX_RING_BYTES, sp->txd, sp->txd_map);
1860
1861 free_irq(pdev->irq, dev);
1862
1863 return 0;
1864}
1865
1866static int ipg_nic_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1867{
1868 struct ipg_nic_private *sp = netdev_priv(dev);
1869 void __iomem *ioaddr = sp->ioaddr;
1870 unsigned int entry = sp->tx_current % IPG_TFDLIST_LENGTH;
1871 unsigned long flags;
1872 struct ipg_tx *txfd;
1873
1874 IPG_DDEBUG_MSG("_nic_hard_start_xmit\n");
1875
1876 /* If in 10Mbps mode, stop the transmit queue so
1877 * no more transmit frames are accepted.
1878 */
1879 if (sp->tenmbpsmode)
1880 netif_stop_queue(dev);
1881
Pekka Enberg9893ba12008-01-24 02:01:17 -08001882 if (sp->reset_current_tfd) {
1883 sp->reset_current_tfd = 0;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001884 entry = 0;
1885 }
1886
1887 txfd = sp->txd + entry;
1888
Pekka Enberg9893ba12008-01-24 02:01:17 -08001889 sp->tx_buff[entry] = skb;
Francois Romieu1202d6f2007-09-17 17:13:55 -07001890
1891 /* Clear all TFC fields, except TFDDONE. */
1892 txfd->tfc = cpu_to_le64(IPG_TFC_TFDDONE);
1893
1894 /* Specify the TFC field within the TFD. */
1895 txfd->tfc |= cpu_to_le64(IPG_TFC_WORDALIGNDISABLED |
Al Viro48f5fec2008-03-16 22:22:14 +00001896 (IPG_TFC_FRAMEID & sp->tx_current) |
Francois Romieu1202d6f2007-09-17 17:13:55 -07001897 (IPG_TFC_FRAGCOUNT & (1 << 24)));
Al Viro48f5fec2008-03-16 22:22:14 +00001898 /*
1899 * 16--17 (WordAlign) <- 3 (disable),
1900 * 0--15 (FrameId) <- sp->tx_current,
1901 * 24--27 (FragCount) <- 1
1902 */
Francois Romieu1202d6f2007-09-17 17:13:55 -07001903
1904 /* Request TxComplete interrupts at an interval defined
1905 * by the constant IPG_FRAMESBETWEENTXCOMPLETES.
1906 * Request TxComplete interrupt for every frame
1907 * if in 10Mbps mode to accomodate problem with 10Mbps
1908 * processing.
1909 */
1910 if (sp->tenmbpsmode)
1911 txfd->tfc |= cpu_to_le64(IPG_TFC_TXINDICATE);
Francois Romieu47cccd72008-01-10 23:53:15 +01001912 txfd->tfc |= cpu_to_le64(IPG_TFC_TXDMAINDICATE);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001913 /* Based on compilation option, determine if FCS is to be
1914 * appended to transmit frame by IPG.
1915 */
1916 if (!(IPG_APPEND_FCS_ON_TX))
1917 txfd->tfc |= cpu_to_le64(IPG_TFC_FCSAPPENDDISABLE);
1918
1919 /* Based on compilation option, determine if IP, TCP and/or
1920 * UDP checksums are to be added to transmit frame by IPG.
1921 */
1922 if (IPG_ADD_IPCHECKSUM_ON_TX)
1923 txfd->tfc |= cpu_to_le64(IPG_TFC_IPCHECKSUMENABLE);
1924
1925 if (IPG_ADD_TCPCHECKSUM_ON_TX)
1926 txfd->tfc |= cpu_to_le64(IPG_TFC_TCPCHECKSUMENABLE);
1927
1928 if (IPG_ADD_UDPCHECKSUM_ON_TX)
1929 txfd->tfc |= cpu_to_le64(IPG_TFC_UDPCHECKSUMENABLE);
1930
1931 /* Based on compilation option, determine if VLAN tag info is to be
1932 * inserted into transmit frame by IPG.
1933 */
1934 if (IPG_INSERT_MANUAL_VLAN_TAG) {
1935 txfd->tfc |= cpu_to_le64(IPG_TFC_VLANTAGINSERT |
1936 ((u64) IPG_MANUAL_VLAN_VID << 32) |
1937 ((u64) IPG_MANUAL_VLAN_CFI << 44) |
1938 ((u64) IPG_MANUAL_VLAN_USERPRIORITY << 45));
1939 }
1940
1941 /* The fragment start location within system memory is defined
1942 * by the sk_buff structure's data field. The physical address
1943 * of this location within the system's virtual memory space
1944 * is determined using the IPG_HOST2BUS_MAP function.
1945 */
1946 txfd->frag_info = cpu_to_le64(pci_map_single(sp->pdev, skb->data,
1947 skb->len, PCI_DMA_TODEVICE));
1948
1949 /* The length of the fragment within system memory is defined by
1950 * the sk_buff structure's len field.
1951 */
1952 txfd->frag_info |= cpu_to_le64(IPG_TFI_FRAGLEN &
1953 ((u64) (skb->len & 0xffff) << 48));
1954
1955 /* Clear the TFDDone bit last to indicate the TFD is ready
1956 * for transfer to the IPG.
1957 */
1958 txfd->tfc &= cpu_to_le64(~IPG_TFC_TFDDONE);
1959
1960 spin_lock_irqsave(&sp->lock, flags);
1961
1962 sp->tx_current++;
1963
1964 mmiowb();
1965
1966 ipg_w32(IPG_DC_TX_DMA_POLL_NOW, DMA_CTRL);
1967
1968 if (sp->tx_current == (sp->tx_dirty + IPG_TFDLIST_LENGTH))
Francois Romieudafdec72008-01-10 23:45:05 +01001969 netif_stop_queue(dev);
Francois Romieu1202d6f2007-09-17 17:13:55 -07001970
1971 spin_unlock_irqrestore(&sp->lock, flags);
1972
1973 return NETDEV_TX_OK;
1974}
1975
1976static void ipg_set_phy_default_param(unsigned char rev,
1977 struct net_device *dev, int phy_address)
1978{
1979 unsigned short length;
1980 unsigned char revision;
1981 unsigned short *phy_param;
1982 unsigned short address, value;
1983
1984 phy_param = &DefaultPhyParam[0];
1985 length = *phy_param & 0x00FF;
1986 revision = (unsigned char)((*phy_param) >> 8);
1987 phy_param++;
1988 while (length != 0) {
1989 if (rev == revision) {
1990 while (length > 1) {
1991 address = *phy_param;
1992 value = *(phy_param + 1);
1993 phy_param += 2;
1994 mdio_write(dev, phy_address, address, value);
1995 length -= 4;
1996 }
1997 break;
1998 } else {
1999 phy_param += length / 2;
2000 length = *phy_param & 0x00FF;
2001 revision = (unsigned char)((*phy_param) >> 8);
2002 phy_param++;
2003 }
2004 }
2005}
2006
Francois Romieu1202d6f2007-09-17 17:13:55 -07002007static int read_eeprom(struct net_device *dev, int eep_addr)
2008{
2009 void __iomem *ioaddr = ipg_ioaddr(dev);
2010 unsigned int i;
2011 int ret = 0;
2012 u16 value;
2013
2014 value = IPG_EC_EEPROM_READOPCODE | (eep_addr & 0xff);
2015 ipg_w16(value, EEPROM_CTRL);
2016
2017 for (i = 0; i < 1000; i++) {
2018 u16 data;
2019
2020 mdelay(10);
2021 data = ipg_r16(EEPROM_CTRL);
2022 if (!(data & IPG_EC_EEPROM_BUSY)) {
2023 ret = ipg_r16(EEPROM_DATA);
2024 break;
2025 }
2026 }
2027 return ret;
2028}
2029
2030static void ipg_init_mii(struct net_device *dev)
2031{
2032 struct ipg_nic_private *sp = netdev_priv(dev);
2033 struct mii_if_info *mii_if = &sp->mii_if;
2034 int phyaddr;
2035
2036 mii_if->dev = dev;
2037 mii_if->mdio_read = mdio_read;
2038 mii_if->mdio_write = mdio_write;
2039 mii_if->phy_id_mask = 0x1f;
2040 mii_if->reg_num_mask = 0x1f;
2041
2042 mii_if->phy_id = phyaddr = ipg_find_phyaddr(dev);
2043
2044 if (phyaddr != 0x1f) {
2045 u16 mii_phyctrl, mii_1000cr;
2046 u8 revisionid = 0;
2047
2048 mii_1000cr = mdio_read(dev, phyaddr, MII_CTRL1000);
2049 mii_1000cr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF |
2050 GMII_PHY_1000BASETCONTROL_PreferMaster;
2051 mdio_write(dev, phyaddr, MII_CTRL1000, mii_1000cr);
2052
2053 mii_phyctrl = mdio_read(dev, phyaddr, MII_BMCR);
2054
2055 /* Set default phyparam */
2056 pci_read_config_byte(sp->pdev, PCI_REVISION_ID, &revisionid);
2057 ipg_set_phy_default_param(revisionid, dev, phyaddr);
2058
2059 /* Reset PHY */
2060 mii_phyctrl |= BMCR_RESET | BMCR_ANRESTART;
2061 mdio_write(dev, phyaddr, MII_BMCR, mii_phyctrl);
2062
2063 }
2064}
2065
2066static int ipg_hw_init(struct net_device *dev)
2067{
2068 struct ipg_nic_private *sp = netdev_priv(dev);
2069 void __iomem *ioaddr = sp->ioaddr;
2070 unsigned int i;
2071 int rc;
2072
Pekka Enbergdea4a872007-11-30 09:54:51 +02002073 /* Read/Write and Reset EEPROM Value */
Francois Romieu1202d6f2007-09-17 17:13:55 -07002074 /* Read LED Mode Configuration from EEPROM */
Pekka Enberg9893ba12008-01-24 02:01:17 -08002075 sp->led_mode = read_eeprom(dev, 6);
Francois Romieu1202d6f2007-09-17 17:13:55 -07002076
2077 /* Reset all functions within the IPG. Do not assert
2078 * RST_OUT as not compatible with some PHYs.
2079 */
2080 rc = ipg_reset(dev, IPG_RESET_MASK);
2081 if (rc < 0)
2082 goto out;
2083
2084 ipg_init_mii(dev);
2085
2086 /* Read MAC Address from EEPROM */
2087 for (i = 0; i < 3; i++)
2088 sp->station_addr[i] = read_eeprom(dev, 16 + i);
2089
2090 for (i = 0; i < 3; i++)
2091 ipg_w16(sp->station_addr[i], STATION_ADDRESS_0 + 2*i);
2092
2093 /* Set station address in ethernet_device structure. */
2094 dev->dev_addr[0] = ipg_r16(STATION_ADDRESS_0) & 0x00ff;
2095 dev->dev_addr[1] = (ipg_r16(STATION_ADDRESS_0) & 0xff00) >> 8;
2096 dev->dev_addr[2] = ipg_r16(STATION_ADDRESS_1) & 0x00ff;
2097 dev->dev_addr[3] = (ipg_r16(STATION_ADDRESS_1) & 0xff00) >> 8;
2098 dev->dev_addr[4] = ipg_r16(STATION_ADDRESS_2) & 0x00ff;
2099 dev->dev_addr[5] = (ipg_r16(STATION_ADDRESS_2) & 0xff00) >> 8;
2100out:
2101 return rc;
2102}
2103
2104static int ipg_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2105{
2106 struct ipg_nic_private *sp = netdev_priv(dev);
2107 int rc;
2108
2109 mutex_lock(&sp->mii_mutex);
2110 rc = generic_mii_ioctl(&sp->mii_if, if_mii(ifr), cmd, NULL);
2111 mutex_unlock(&sp->mii_mutex);
2112
2113 return rc;
2114}
2115
2116static int ipg_nic_change_mtu(struct net_device *dev, int new_mtu)
2117{
Pekka Enbergda02b232008-06-23 14:36:35 +03002118 struct ipg_nic_private *sp = netdev_priv(dev);
2119
Francois Romieu1202d6f2007-09-17 17:13:55 -07002120 /* Function to accomodate changes to Maximum Transfer Unit
2121 * (or MTU) of IPG NIC. Cannot use default function since
2122 * the default will not allow for MTU > 1500 bytes.
2123 */
2124
2125 IPG_DEBUG_MSG("_nic_change_mtu\n");
2126
2127 /* Check that the new MTU value is between 68 (14 byte header, 46
2128 * byte payload, 4 byte FCS) and IPG_MAX_RXFRAME_SIZE, which
2129 * corresponds to the MAXFRAMESIZE register in the IPG.
2130 */
Pekka Enbergda02b232008-06-23 14:36:35 +03002131 if ((new_mtu < 68) || (new_mtu > sp->max_rxframe_size))
Francois Romieu1202d6f2007-09-17 17:13:55 -07002132 return -EINVAL;
2133
2134 dev->mtu = new_mtu;
2135
2136 return 0;
2137}
2138
2139static int ipg_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2140{
2141 struct ipg_nic_private *sp = netdev_priv(dev);
2142 int rc;
2143
2144 mutex_lock(&sp->mii_mutex);
2145 rc = mii_ethtool_gset(&sp->mii_if, cmd);
2146 mutex_unlock(&sp->mii_mutex);
2147
2148 return rc;
2149}
2150
2151static int ipg_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2152{
2153 struct ipg_nic_private *sp = netdev_priv(dev);
2154 int rc;
2155
2156 mutex_lock(&sp->mii_mutex);
2157 rc = mii_ethtool_sset(&sp->mii_if, cmd);
2158 mutex_unlock(&sp->mii_mutex);
2159
2160 return rc;
2161}
2162
2163static int ipg_nway_reset(struct net_device *dev)
2164{
2165 struct ipg_nic_private *sp = netdev_priv(dev);
2166 int rc;
2167
2168 mutex_lock(&sp->mii_mutex);
2169 rc = mii_nway_restart(&sp->mii_if);
2170 mutex_unlock(&sp->mii_mutex);
2171
2172 return rc;
2173}
2174
2175static struct ethtool_ops ipg_ethtool_ops = {
2176 .get_settings = ipg_get_settings,
2177 .set_settings = ipg_set_settings,
2178 .nway_reset = ipg_nway_reset,
2179};
2180
Adrian Bunkef312242007-12-11 23:20:30 +01002181static void __devexit ipg_remove(struct pci_dev *pdev)
Francois Romieu1202d6f2007-09-17 17:13:55 -07002182{
2183 struct net_device *dev = pci_get_drvdata(pdev);
2184 struct ipg_nic_private *sp = netdev_priv(dev);
2185
2186 IPG_DEBUG_MSG("_remove\n");
2187
2188 /* Un-register Ethernet device. */
2189 unregister_netdev(dev);
2190
2191 pci_iounmap(pdev, sp->ioaddr);
2192
2193 pci_release_regions(pdev);
2194
2195 free_netdev(dev);
2196 pci_disable_device(pdev);
2197 pci_set_drvdata(pdev, NULL);
2198}
2199
2200static int __devinit ipg_probe(struct pci_dev *pdev,
2201 const struct pci_device_id *id)
2202{
2203 unsigned int i = id->driver_data;
2204 struct ipg_nic_private *sp;
2205 struct net_device *dev;
2206 void __iomem *ioaddr;
2207 int rc;
2208
2209 rc = pci_enable_device(pdev);
2210 if (rc < 0)
2211 goto out;
2212
2213 printk(KERN_INFO "%s: %s\n", pci_name(pdev), ipg_brand_name[i]);
2214
2215 pci_set_master(pdev);
2216
2217 rc = pci_set_dma_mask(pdev, DMA_40BIT_MASK);
2218 if (rc < 0) {
2219 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2220 if (rc < 0) {
2221 printk(KERN_ERR "%s: DMA config failed.\n",
2222 pci_name(pdev));
2223 goto err_disable_0;
2224 }
2225 }
2226
2227 /*
2228 * Initialize net device.
2229 */
2230 dev = alloc_etherdev(sizeof(struct ipg_nic_private));
2231 if (!dev) {
2232 printk(KERN_ERR "%s: alloc_etherdev failed\n", pci_name(pdev));
2233 rc = -ENOMEM;
2234 goto err_disable_0;
2235 }
2236
2237 sp = netdev_priv(dev);
2238 spin_lock_init(&sp->lock);
2239 mutex_init(&sp->mii_mutex);
2240
Pekka Enberg024f4d882008-06-23 14:35:16 +03002241 sp->is_jumbo = IPG_JUMBO;
Pekka Enberg18a9cdb2008-06-23 14:36:00 +03002242 sp->rxfrag_size = IPG_RXFRAG_SIZE;
Pekka Enberg39f20582008-06-23 14:36:18 +03002243 sp->rxsupport_size = IPG_RXSUPPORT_SIZE;
Pekka Enbergda02b232008-06-23 14:36:35 +03002244 sp->max_rxframe_size = IPG_MAX_RXFRAME_SIZE;
Pekka Enberg024f4d882008-06-23 14:35:16 +03002245
Francois Romieu1202d6f2007-09-17 17:13:55 -07002246 /* Declare IPG NIC functions for Ethernet device methods.
2247 */
2248 dev->open = &ipg_nic_open;
2249 dev->stop = &ipg_nic_stop;
2250 dev->hard_start_xmit = &ipg_nic_hard_start_xmit;
2251 dev->get_stats = &ipg_nic_get_stats;
2252 dev->set_multicast_list = &ipg_nic_set_multicast_list;
2253 dev->do_ioctl = ipg_ioctl;
2254 dev->tx_timeout = ipg_tx_timeout;
2255 dev->change_mtu = &ipg_nic_change_mtu;
2256
2257 SET_NETDEV_DEV(dev, &pdev->dev);
2258 SET_ETHTOOL_OPS(dev, &ipg_ethtool_ops);
2259
2260 rc = pci_request_regions(pdev, DRV_NAME);
2261 if (rc)
2262 goto err_free_dev_1;
2263
2264 ioaddr = pci_iomap(pdev, 1, pci_resource_len(pdev, 1));
2265 if (!ioaddr) {
2266 printk(KERN_ERR "%s cannot map MMIO\n", pci_name(pdev));
2267 rc = -EIO;
2268 goto err_release_regions_2;
2269 }
2270
2271 /* Save the pointer to the PCI device information. */
2272 sp->ioaddr = ioaddr;
2273 sp->pdev = pdev;
2274 sp->dev = dev;
2275
2276 INIT_DELAYED_WORK(&sp->task, ipg_reset_after_host_error);
2277
2278 pci_set_drvdata(pdev, dev);
2279
2280 rc = ipg_hw_init(dev);
2281 if (rc < 0)
2282 goto err_unmap_3;
2283
2284 rc = register_netdev(dev);
2285 if (rc < 0)
2286 goto err_unmap_3;
2287
2288 printk(KERN_INFO "Ethernet device registered as: %s\n", dev->name);
2289out:
2290 return rc;
2291
2292err_unmap_3:
2293 pci_iounmap(pdev, ioaddr);
2294err_release_regions_2:
2295 pci_release_regions(pdev);
2296err_free_dev_1:
2297 free_netdev(dev);
2298err_disable_0:
2299 pci_disable_device(pdev);
2300 goto out;
2301}
2302
2303static struct pci_driver ipg_pci_driver = {
2304 .name = IPG_DRIVER_NAME,
2305 .id_table = ipg_pci_tbl,
2306 .probe = ipg_probe,
2307 .remove = __devexit_p(ipg_remove),
2308};
2309
2310static int __init ipg_init_module(void)
2311{
2312 return pci_register_driver(&ipg_pci_driver);
2313}
2314
2315static void __exit ipg_exit_module(void)
2316{
2317 pci_unregister_driver(&ipg_pci_driver);
2318}
2319
2320module_init(ipg_init_module);
2321module_exit(ipg_exit_module);