GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / net / ethernet / tundra / tsi108_eth.c
1 /*******************************************************************************
2
3   Copyright(c) 2006 Tundra Semiconductor Corporation.
4
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of the GNU General Public License as published by the Free
7   Software Foundation; either version 2 of the License, or (at your option)
8   any later version.
9
10   This program is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc., 59
17   Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 *******************************************************************************/
20
21 /* This driver is based on the driver code originally developed
22  * for the Intel IOC80314 (ForestLake) Gigabit Ethernet by
23  * scott.wood@timesys.com  * Copyright (C) 2003 TimeSys Corporation
24  *
25  * Currently changes from original version are:
26  * - porting to Tsi108-based platform and kernel 2.6 (kong.lai@tundra.com)
27  * - modifications to handle two ports independently and support for
28  *   additional PHY devices (alexandre.bounine@tundra.com)
29  * - Get hardware information from platform device. (tie-fei.zang@freescale.com)
30  *
31  */
32
33 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/interrupt.h>
36 #include <linux/net.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/ethtool.h>
40 #include <linux/skbuff.h>
41 #include <linux/spinlock.h>
42 #include <linux/delay.h>
43 #include <linux/crc32.h>
44 #include <linux/mii.h>
45 #include <linux/device.h>
46 #include <linux/pci.h>
47 #include <linux/rtnetlink.h>
48 #include <linux/timer.h>
49 #include <linux/platform_device.h>
50 #include <linux/gfp.h>
51
52 #include <asm/io.h>
53 #include <asm/tsi108.h>
54
55 #include "tsi108_eth.h"
56
57 #define MII_READ_DELAY 10000    /* max link wait time in msec */
58
59 #define TSI108_RXRING_LEN     256
60
61 /* NOTE: The driver currently does not support receiving packets
62  * larger than the buffer size, so don't decrease this (unless you
63  * want to add such support).
64  */
65 #define TSI108_RXBUF_SIZE     1536
66
67 #define TSI108_TXRING_LEN     256
68
69 #define TSI108_TX_INT_FREQ    64
70
71 /* Check the phy status every half a second. */
72 #define CHECK_PHY_INTERVAL (HZ/2)
73
74 static int tsi108_init_one(struct platform_device *pdev);
75 static int tsi108_ether_remove(struct platform_device *pdev);
76
77 struct tsi108_prv_data {
78         void  __iomem *regs;    /* Base of normal regs */
79         void  __iomem *phyregs; /* Base of register bank used for PHY access */
80
81         struct net_device *dev;
82         struct napi_struct napi;
83
84         unsigned int phy;               /* Index of PHY for this interface */
85         unsigned int irq_num;
86         unsigned int id;
87         unsigned int phy_type;
88
89         struct timer_list timer;/* Timer that triggers the check phy function */
90         unsigned int rxtail;    /* Next entry in rxring to read */
91         unsigned int rxhead;    /* Next entry in rxring to give a new buffer */
92         unsigned int rxfree;    /* Number of free, allocated RX buffers */
93
94         unsigned int rxpending; /* Non-zero if there are still descriptors
95                                  * to be processed from a previous descriptor
96                                  * interrupt condition that has been cleared */
97
98         unsigned int txtail;    /* Next TX descriptor to check status on */
99         unsigned int txhead;    /* Next TX descriptor to use */
100
101         /* Number of free TX descriptors.  This could be calculated from
102          * rxhead and rxtail if one descriptor were left unused to disambiguate
103          * full and empty conditions, but it's simpler to just keep track
104          * explicitly. */
105
106         unsigned int txfree;
107
108         unsigned int phy_ok;            /* The PHY is currently powered on. */
109
110         /* PHY status (duplex is 1 for half, 2 for full,
111          * so that the default 0 indicates that neither has
112          * yet been configured). */
113
114         unsigned int link_up;
115         unsigned int speed;
116         unsigned int duplex;
117
118         tx_desc *txring;
119         rx_desc *rxring;
120         struct sk_buff *txskbs[TSI108_TXRING_LEN];
121         struct sk_buff *rxskbs[TSI108_RXRING_LEN];
122
123         dma_addr_t txdma, rxdma;
124
125         /* txlock nests in misclock and phy_lock */
126
127         spinlock_t txlock, misclock;
128
129         /* stats is used to hold the upper bits of each hardware counter,
130          * and tmpstats is used to hold the full values for returning
131          * to the caller of get_stats().  They must be separate in case
132          * an overflow interrupt occurs before the stats are consumed.
133          */
134
135         struct net_device_stats stats;
136         struct net_device_stats tmpstats;
137
138         /* These stats are kept separate in hardware, thus require individual
139          * fields for handling carry.  They are combined in get_stats.
140          */
141
142         unsigned long rx_fcs;   /* Add to rx_frame_errors */
143         unsigned long rx_short_fcs;     /* Add to rx_frame_errors */
144         unsigned long rx_long_fcs;      /* Add to rx_frame_errors */
145         unsigned long rx_underruns;     /* Add to rx_length_errors */
146         unsigned long rx_overruns;      /* Add to rx_length_errors */
147
148         unsigned long tx_coll_abort;    /* Add to tx_aborted_errors/collisions */
149         unsigned long tx_pause_drop;    /* Add to tx_aborted_errors */
150
151         unsigned long mc_hash[16];
152         u32 msg_enable;                 /* debug message level */
153         struct mii_if_info mii_if;
154         unsigned int init_media;
155
156         struct platform_device *pdev;
157 };
158
159 /* Structure for a device driver */
160
161 static struct platform_driver tsi_eth_driver = {
162         .probe = tsi108_init_one,
163         .remove = tsi108_ether_remove,
164         .driver = {
165                 .name = "tsi-ethernet",
166         },
167 };
168
169 static void tsi108_timed_checker(struct timer_list *t);
170
171 #ifdef DEBUG
172 static void dump_eth_one(struct net_device *dev)
173 {
174         struct tsi108_prv_data *data = netdev_priv(dev);
175
176         printk("Dumping %s...\n", dev->name);
177         printk("intstat %x intmask %x phy_ok %d"
178                " link %d speed %d duplex %d\n",
179                TSI_READ(TSI108_EC_INTSTAT),
180                TSI_READ(TSI108_EC_INTMASK), data->phy_ok,
181                data->link_up, data->speed, data->duplex);
182
183         printk("TX: head %d, tail %d, free %d, stat %x, estat %x, err %x\n",
184                data->txhead, data->txtail, data->txfree,
185                TSI_READ(TSI108_EC_TXSTAT),
186                TSI_READ(TSI108_EC_TXESTAT),
187                TSI_READ(TSI108_EC_TXERR));
188
189         printk("RX: head %d, tail %d, free %d, stat %x,"
190                " estat %x, err %x, pending %d\n\n",
191                data->rxhead, data->rxtail, data->rxfree,
192                TSI_READ(TSI108_EC_RXSTAT),
193                TSI_READ(TSI108_EC_RXESTAT),
194                TSI_READ(TSI108_EC_RXERR), data->rxpending);
195 }
196 #endif
197
198 /* Synchronization is needed between the thread and up/down events.
199  * Note that the PHY is accessed through the same registers for both
200  * interfaces, so this can't be made interface-specific.
201  */
202
203 static DEFINE_SPINLOCK(phy_lock);
204
205 static int tsi108_read_mii(struct tsi108_prv_data *data, int reg)
206 {
207         unsigned i;
208
209         TSI_WRITE_PHY(TSI108_MAC_MII_ADDR,
210                                 (data->phy << TSI108_MAC_MII_ADDR_PHY) |
211                                 (reg << TSI108_MAC_MII_ADDR_REG));
212         TSI_WRITE_PHY(TSI108_MAC_MII_CMD, 0);
213         TSI_WRITE_PHY(TSI108_MAC_MII_CMD, TSI108_MAC_MII_CMD_READ);
214         for (i = 0; i < 100; i++) {
215                 if (!(TSI_READ_PHY(TSI108_MAC_MII_IND) &
216                       (TSI108_MAC_MII_IND_NOTVALID | TSI108_MAC_MII_IND_BUSY)))
217                         break;
218                 udelay(10);
219         }
220
221         if (i == 100)
222                 return 0xffff;
223         else
224                 return TSI_READ_PHY(TSI108_MAC_MII_DATAIN);
225 }
226
227 static void tsi108_write_mii(struct tsi108_prv_data *data,
228                                 int reg, u16 val)
229 {
230         unsigned i = 100;
231         TSI_WRITE_PHY(TSI108_MAC_MII_ADDR,
232                                 (data->phy << TSI108_MAC_MII_ADDR_PHY) |
233                                 (reg << TSI108_MAC_MII_ADDR_REG));
234         TSI_WRITE_PHY(TSI108_MAC_MII_DATAOUT, val);
235         while (i--) {
236                 if(!(TSI_READ_PHY(TSI108_MAC_MII_IND) &
237                         TSI108_MAC_MII_IND_BUSY))
238                         break;
239                 udelay(10);
240         }
241 }
242
243 static int tsi108_mdio_read(struct net_device *dev, int addr, int reg)
244 {
245         struct tsi108_prv_data *data = netdev_priv(dev);
246         return tsi108_read_mii(data, reg);
247 }
248
249 static void tsi108_mdio_write(struct net_device *dev, int addr, int reg, int val)
250 {
251         struct tsi108_prv_data *data = netdev_priv(dev);
252         tsi108_write_mii(data, reg, val);
253 }
254
255 static inline void tsi108_write_tbi(struct tsi108_prv_data *data,
256                                         int reg, u16 val)
257 {
258         unsigned i = 1000;
259         TSI_WRITE(TSI108_MAC_MII_ADDR,
260                              (0x1e << TSI108_MAC_MII_ADDR_PHY)
261                              | (reg << TSI108_MAC_MII_ADDR_REG));
262         TSI_WRITE(TSI108_MAC_MII_DATAOUT, val);
263         while(i--) {
264                 if(!(TSI_READ(TSI108_MAC_MII_IND) & TSI108_MAC_MII_IND_BUSY))
265                         return;
266                 udelay(10);
267         }
268         printk(KERN_ERR "%s function time out\n", __func__);
269 }
270
271 static int mii_speed(struct mii_if_info *mii)
272 {
273         int advert, lpa, val, media;
274         int lpa2 = 0;
275         int speed;
276
277         if (!mii_link_ok(mii))
278                 return 0;
279
280         val = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_BMSR);
281         if ((val & BMSR_ANEGCOMPLETE) == 0)
282                 return 0;
283
284         advert = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_ADVERTISE);
285         lpa = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_LPA);
286         media = mii_nway_result(advert & lpa);
287
288         if (mii->supports_gmii)
289                 lpa2 = mii->mdio_read(mii->dev, mii->phy_id, MII_STAT1000);
290
291         speed = lpa2 & (LPA_1000FULL | LPA_1000HALF) ? 1000 :
292                         (media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? 100 : 10);
293         return speed;
294 }
295
296 static void tsi108_check_phy(struct net_device *dev)
297 {
298         struct tsi108_prv_data *data = netdev_priv(dev);
299         u32 mac_cfg2_reg, portctrl_reg;
300         u32 duplex;
301         u32 speed;
302         unsigned long flags;
303
304         spin_lock_irqsave(&phy_lock, flags);
305
306         if (!data->phy_ok)
307                 goto out;
308
309         duplex = mii_check_media(&data->mii_if, netif_msg_link(data), data->init_media);
310         data->init_media = 0;
311
312         if (netif_carrier_ok(dev)) {
313
314                 speed = mii_speed(&data->mii_if);
315
316                 if ((speed != data->speed) || duplex) {
317
318                         mac_cfg2_reg = TSI_READ(TSI108_MAC_CFG2);
319                         portctrl_reg = TSI_READ(TSI108_EC_PORTCTRL);
320
321                         mac_cfg2_reg &= ~TSI108_MAC_CFG2_IFACE_MASK;
322
323                         if (speed == 1000) {
324                                 mac_cfg2_reg |= TSI108_MAC_CFG2_GIG;
325                                 portctrl_reg &= ~TSI108_EC_PORTCTRL_NOGIG;
326                         } else {
327                                 mac_cfg2_reg |= TSI108_MAC_CFG2_NOGIG;
328                                 portctrl_reg |= TSI108_EC_PORTCTRL_NOGIG;
329                         }
330
331                         data->speed = speed;
332
333                         if (data->mii_if.full_duplex) {
334                                 mac_cfg2_reg |= TSI108_MAC_CFG2_FULLDUPLEX;
335                                 portctrl_reg &= ~TSI108_EC_PORTCTRL_HALFDUPLEX;
336                                 data->duplex = 2;
337                         } else {
338                                 mac_cfg2_reg &= ~TSI108_MAC_CFG2_FULLDUPLEX;
339                                 portctrl_reg |= TSI108_EC_PORTCTRL_HALFDUPLEX;
340                                 data->duplex = 1;
341                         }
342
343                         TSI_WRITE(TSI108_MAC_CFG2, mac_cfg2_reg);
344                         TSI_WRITE(TSI108_EC_PORTCTRL, portctrl_reg);
345                 }
346
347                 if (data->link_up == 0) {
348                         /* The manual says it can take 3-4 usecs for the speed change
349                          * to take effect.
350                          */
351                         udelay(5);
352
353                         spin_lock(&data->txlock);
354                         if (is_valid_ether_addr(dev->dev_addr) && data->txfree)
355                                 netif_wake_queue(dev);
356
357                         data->link_up = 1;
358                         spin_unlock(&data->txlock);
359                 }
360         } else {
361                 if (data->link_up == 1) {
362                         netif_stop_queue(dev);
363                         data->link_up = 0;
364                         printk(KERN_NOTICE "%s : link is down\n", dev->name);
365                 }
366
367                 goto out;
368         }
369
370
371 out:
372         spin_unlock_irqrestore(&phy_lock, flags);
373 }
374
375 static inline void
376 tsi108_stat_carry_one(int carry, int carry_bit, int carry_shift,
377                       unsigned long *upper)
378 {
379         if (carry & carry_bit)
380                 *upper += carry_shift;
381 }
382
383 static void tsi108_stat_carry(struct net_device *dev)
384 {
385         struct tsi108_prv_data *data = netdev_priv(dev);
386         unsigned long flags;
387         u32 carry1, carry2;
388
389         spin_lock_irqsave(&data->misclock, flags);
390
391         carry1 = TSI_READ(TSI108_STAT_CARRY1);
392         carry2 = TSI_READ(TSI108_STAT_CARRY2);
393
394         TSI_WRITE(TSI108_STAT_CARRY1, carry1);
395         TSI_WRITE(TSI108_STAT_CARRY2, carry2);
396
397         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXBYTES,
398                               TSI108_STAT_RXBYTES_CARRY, &data->stats.rx_bytes);
399
400         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXPKTS,
401                               TSI108_STAT_RXPKTS_CARRY,
402                               &data->stats.rx_packets);
403
404         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXFCS,
405                               TSI108_STAT_RXFCS_CARRY, &data->rx_fcs);
406
407         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXMCAST,
408                               TSI108_STAT_RXMCAST_CARRY,
409                               &data->stats.multicast);
410
411         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXALIGN,
412                               TSI108_STAT_RXALIGN_CARRY,
413                               &data->stats.rx_frame_errors);
414
415         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXLENGTH,
416                               TSI108_STAT_RXLENGTH_CARRY,
417                               &data->stats.rx_length_errors);
418
419         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXRUNT,
420                               TSI108_STAT_RXRUNT_CARRY, &data->rx_underruns);
421
422         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXJUMBO,
423                               TSI108_STAT_RXJUMBO_CARRY, &data->rx_overruns);
424
425         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXFRAG,
426                               TSI108_STAT_RXFRAG_CARRY, &data->rx_short_fcs);
427
428         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXJABBER,
429                               TSI108_STAT_RXJABBER_CARRY, &data->rx_long_fcs);
430
431         tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXDROP,
432                               TSI108_STAT_RXDROP_CARRY,
433                               &data->stats.rx_missed_errors);
434
435         tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXBYTES,
436                               TSI108_STAT_TXBYTES_CARRY, &data->stats.tx_bytes);
437
438         tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXPKTS,
439                               TSI108_STAT_TXPKTS_CARRY,
440                               &data->stats.tx_packets);
441
442         tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXEXDEF,
443                               TSI108_STAT_TXEXDEF_CARRY,
444                               &data->stats.tx_aborted_errors);
445
446         tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXEXCOL,
447                               TSI108_STAT_TXEXCOL_CARRY, &data->tx_coll_abort);
448
449         tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXTCOL,
450                               TSI108_STAT_TXTCOL_CARRY,
451                               &data->stats.collisions);
452
453         tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXPAUSE,
454                               TSI108_STAT_TXPAUSEDROP_CARRY,
455                               &data->tx_pause_drop);
456
457         spin_unlock_irqrestore(&data->misclock, flags);
458 }
459
460 /* Read a stat counter atomically with respect to carries.
461  * data->misclock must be held.
462  */
463 static inline unsigned long
464 tsi108_read_stat(struct tsi108_prv_data * data, int reg, int carry_bit,
465                  int carry_shift, unsigned long *upper)
466 {
467         int carryreg;
468         unsigned long val;
469
470         if (reg < 0xb0)
471                 carryreg = TSI108_STAT_CARRY1;
472         else
473                 carryreg = TSI108_STAT_CARRY2;
474
475       again:
476         val = TSI_READ(reg) | *upper;
477
478         /* Check to see if it overflowed, but the interrupt hasn't
479          * been serviced yet.  If so, handle the carry here, and
480          * try again.
481          */
482
483         if (unlikely(TSI_READ(carryreg) & carry_bit)) {
484                 *upper += carry_shift;
485                 TSI_WRITE(carryreg, carry_bit);
486                 goto again;
487         }
488
489         return val;
490 }
491
492 static struct net_device_stats *tsi108_get_stats(struct net_device *dev)
493 {
494         unsigned long excol;
495
496         struct tsi108_prv_data *data = netdev_priv(dev);
497         spin_lock_irq(&data->misclock);
498
499         data->tmpstats.rx_packets =
500             tsi108_read_stat(data, TSI108_STAT_RXPKTS,
501                              TSI108_STAT_CARRY1_RXPKTS,
502                              TSI108_STAT_RXPKTS_CARRY, &data->stats.rx_packets);
503
504         data->tmpstats.tx_packets =
505             tsi108_read_stat(data, TSI108_STAT_TXPKTS,
506                              TSI108_STAT_CARRY2_TXPKTS,
507                              TSI108_STAT_TXPKTS_CARRY, &data->stats.tx_packets);
508
509         data->tmpstats.rx_bytes =
510             tsi108_read_stat(data, TSI108_STAT_RXBYTES,
511                              TSI108_STAT_CARRY1_RXBYTES,
512                              TSI108_STAT_RXBYTES_CARRY, &data->stats.rx_bytes);
513
514         data->tmpstats.tx_bytes =
515             tsi108_read_stat(data, TSI108_STAT_TXBYTES,
516                              TSI108_STAT_CARRY2_TXBYTES,
517                              TSI108_STAT_TXBYTES_CARRY, &data->stats.tx_bytes);
518
519         data->tmpstats.multicast =
520             tsi108_read_stat(data, TSI108_STAT_RXMCAST,
521                              TSI108_STAT_CARRY1_RXMCAST,
522                              TSI108_STAT_RXMCAST_CARRY, &data->stats.multicast);
523
524         excol = tsi108_read_stat(data, TSI108_STAT_TXEXCOL,
525                                  TSI108_STAT_CARRY2_TXEXCOL,
526                                  TSI108_STAT_TXEXCOL_CARRY,
527                                  &data->tx_coll_abort);
528
529         data->tmpstats.collisions =
530             tsi108_read_stat(data, TSI108_STAT_TXTCOL,
531                              TSI108_STAT_CARRY2_TXTCOL,
532                              TSI108_STAT_TXTCOL_CARRY, &data->stats.collisions);
533
534         data->tmpstats.collisions += excol;
535
536         data->tmpstats.rx_length_errors =
537             tsi108_read_stat(data, TSI108_STAT_RXLENGTH,
538                              TSI108_STAT_CARRY1_RXLENGTH,
539                              TSI108_STAT_RXLENGTH_CARRY,
540                              &data->stats.rx_length_errors);
541
542         data->tmpstats.rx_length_errors +=
543             tsi108_read_stat(data, TSI108_STAT_RXRUNT,
544                              TSI108_STAT_CARRY1_RXRUNT,
545                              TSI108_STAT_RXRUNT_CARRY, &data->rx_underruns);
546
547         data->tmpstats.rx_length_errors +=
548             tsi108_read_stat(data, TSI108_STAT_RXJUMBO,
549                              TSI108_STAT_CARRY1_RXJUMBO,
550                              TSI108_STAT_RXJUMBO_CARRY, &data->rx_overruns);
551
552         data->tmpstats.rx_frame_errors =
553             tsi108_read_stat(data, TSI108_STAT_RXALIGN,
554                              TSI108_STAT_CARRY1_RXALIGN,
555                              TSI108_STAT_RXALIGN_CARRY,
556                              &data->stats.rx_frame_errors);
557
558         data->tmpstats.rx_frame_errors +=
559             tsi108_read_stat(data, TSI108_STAT_RXFCS,
560                              TSI108_STAT_CARRY1_RXFCS, TSI108_STAT_RXFCS_CARRY,
561                              &data->rx_fcs);
562
563         data->tmpstats.rx_frame_errors +=
564             tsi108_read_stat(data, TSI108_STAT_RXFRAG,
565                              TSI108_STAT_CARRY1_RXFRAG,
566                              TSI108_STAT_RXFRAG_CARRY, &data->rx_short_fcs);
567
568         data->tmpstats.rx_missed_errors =
569             tsi108_read_stat(data, TSI108_STAT_RXDROP,
570                              TSI108_STAT_CARRY1_RXDROP,
571                              TSI108_STAT_RXDROP_CARRY,
572                              &data->stats.rx_missed_errors);
573
574         /* These three are maintained by software. */
575         data->tmpstats.rx_fifo_errors = data->stats.rx_fifo_errors;
576         data->tmpstats.rx_crc_errors = data->stats.rx_crc_errors;
577
578         data->tmpstats.tx_aborted_errors =
579             tsi108_read_stat(data, TSI108_STAT_TXEXDEF,
580                              TSI108_STAT_CARRY2_TXEXDEF,
581                              TSI108_STAT_TXEXDEF_CARRY,
582                              &data->stats.tx_aborted_errors);
583
584         data->tmpstats.tx_aborted_errors +=
585             tsi108_read_stat(data, TSI108_STAT_TXPAUSEDROP,
586                              TSI108_STAT_CARRY2_TXPAUSE,
587                              TSI108_STAT_TXPAUSEDROP_CARRY,
588                              &data->tx_pause_drop);
589
590         data->tmpstats.tx_aborted_errors += excol;
591
592         data->tmpstats.tx_errors = data->tmpstats.tx_aborted_errors;
593         data->tmpstats.rx_errors = data->tmpstats.rx_length_errors +
594             data->tmpstats.rx_crc_errors +
595             data->tmpstats.rx_frame_errors +
596             data->tmpstats.rx_fifo_errors + data->tmpstats.rx_missed_errors;
597
598         spin_unlock_irq(&data->misclock);
599         return &data->tmpstats;
600 }
601
602 static void tsi108_restart_rx(struct tsi108_prv_data * data, struct net_device *dev)
603 {
604         TSI_WRITE(TSI108_EC_RXQ_PTRHIGH,
605                              TSI108_EC_RXQ_PTRHIGH_VALID);
606
607         TSI_WRITE(TSI108_EC_RXCTRL, TSI108_EC_RXCTRL_GO
608                              | TSI108_EC_RXCTRL_QUEUE0);
609 }
610
611 static void tsi108_restart_tx(struct tsi108_prv_data * data)
612 {
613         TSI_WRITE(TSI108_EC_TXQ_PTRHIGH,
614                              TSI108_EC_TXQ_PTRHIGH_VALID);
615
616         TSI_WRITE(TSI108_EC_TXCTRL, TSI108_EC_TXCTRL_IDLEINT |
617                              TSI108_EC_TXCTRL_GO | TSI108_EC_TXCTRL_QUEUE0);
618 }
619
620 /* txlock must be held by caller, with IRQs disabled, and
621  * with permission to re-enable them when the lock is dropped.
622  */
623 static void tsi108_complete_tx(struct net_device *dev)
624 {
625         struct tsi108_prv_data *data = netdev_priv(dev);
626         int tx;
627         struct sk_buff *skb;
628         int release = 0;
629
630         while (!data->txfree || data->txhead != data->txtail) {
631                 tx = data->txtail;
632
633                 if (data->txring[tx].misc & TSI108_TX_OWN)
634                         break;
635
636                 skb = data->txskbs[tx];
637
638                 if (!(data->txring[tx].misc & TSI108_TX_OK))
639                         printk("%s: bad tx packet, misc %x\n",
640                                dev->name, data->txring[tx].misc);
641
642                 data->txtail = (data->txtail + 1) % TSI108_TXRING_LEN;
643                 data->txfree++;
644
645                 if (data->txring[tx].misc & TSI108_TX_EOF) {
646                         dev_kfree_skb_any(skb);
647                         release++;
648                 }
649         }
650
651         if (release) {
652                 if (is_valid_ether_addr(dev->dev_addr) && data->link_up)
653                         netif_wake_queue(dev);
654         }
655 }
656
657 static int tsi108_send_packet(struct sk_buff * skb, struct net_device *dev)
658 {
659         struct tsi108_prv_data *data = netdev_priv(dev);
660         int frags = skb_shinfo(skb)->nr_frags + 1;
661         int i;
662
663         if (!data->phy_ok && net_ratelimit())
664                 printk(KERN_ERR "%s: Transmit while PHY is down!\n", dev->name);
665
666         if (!data->link_up) {
667                 printk(KERN_ERR "%s: Transmit while link is down!\n",
668                        dev->name);
669                 netif_stop_queue(dev);
670                 return NETDEV_TX_BUSY;
671         }
672
673         if (data->txfree < MAX_SKB_FRAGS + 1) {
674                 netif_stop_queue(dev);
675
676                 if (net_ratelimit())
677                         printk(KERN_ERR "%s: Transmit with full tx ring!\n",
678                                dev->name);
679                 return NETDEV_TX_BUSY;
680         }
681
682         if (data->txfree - frags < MAX_SKB_FRAGS + 1) {
683                 netif_stop_queue(dev);
684         }
685
686         spin_lock_irq(&data->txlock);
687
688         for (i = 0; i < frags; i++) {
689                 int misc = 0;
690                 int tx = data->txhead;
691
692                 /* This is done to mark every TSI108_TX_INT_FREQ tx buffers with
693                  * the interrupt bit.  TX descriptor-complete interrupts are
694                  * enabled when the queue fills up, and masked when there is
695                  * still free space.  This way, when saturating the outbound
696                  * link, the tx interrupts are kept to a reasonable level.
697                  * When the queue is not full, reclamation of skbs still occurs
698                  * as new packets are transmitted, or on a queue-empty
699                  * interrupt.
700                  */
701
702                 if ((tx % TSI108_TX_INT_FREQ == 0) &&
703                     ((TSI108_TXRING_LEN - data->txfree) >= TSI108_TX_INT_FREQ))
704                         misc = TSI108_TX_INT;
705
706                 data->txskbs[tx] = skb;
707
708                 if (i == 0) {
709                         data->txring[tx].buf0 = dma_map_single(&data->pdev->dev,
710                                         skb->data, skb_headlen(skb),
711                                         DMA_TO_DEVICE);
712                         data->txring[tx].len = skb_headlen(skb);
713                         misc |= TSI108_TX_SOF;
714                 } else {
715                         const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
716
717                         data->txring[tx].buf0 =
718                                 skb_frag_dma_map(&data->pdev->dev, frag,
719                                                 0, skb_frag_size(frag),
720                                                 DMA_TO_DEVICE);
721                         data->txring[tx].len = skb_frag_size(frag);
722                 }
723
724                 if (i == frags - 1)
725                         misc |= TSI108_TX_EOF;
726
727                 if (netif_msg_pktdata(data)) {
728                         int i;
729                         printk("%s: Tx Frame contents (%d)\n", dev->name,
730                                skb->len);
731                         for (i = 0; i < skb->len; i++)
732                                 printk(" %2.2x", skb->data[i]);
733                         printk(".\n");
734                 }
735                 data->txring[tx].misc = misc | TSI108_TX_OWN;
736
737                 data->txhead = (data->txhead + 1) % TSI108_TXRING_LEN;
738                 data->txfree--;
739         }
740
741         tsi108_complete_tx(dev);
742
743         /* This must be done after the check for completed tx descriptors,
744          * so that the tail pointer is correct.
745          */
746
747         if (!(TSI_READ(TSI108_EC_TXSTAT) & TSI108_EC_TXSTAT_QUEUE0))
748                 tsi108_restart_tx(data);
749
750         spin_unlock_irq(&data->txlock);
751         return NETDEV_TX_OK;
752 }
753
754 static int tsi108_complete_rx(struct net_device *dev, int budget)
755 {
756         struct tsi108_prv_data *data = netdev_priv(dev);
757         int done = 0;
758
759         while (data->rxfree && done != budget) {
760                 int rx = data->rxtail;
761                 struct sk_buff *skb;
762
763                 if (data->rxring[rx].misc & TSI108_RX_OWN)
764                         break;
765
766                 skb = data->rxskbs[rx];
767                 data->rxtail = (data->rxtail + 1) % TSI108_RXRING_LEN;
768                 data->rxfree--;
769                 done++;
770
771                 if (data->rxring[rx].misc & TSI108_RX_BAD) {
772                         spin_lock_irq(&data->misclock);
773
774                         if (data->rxring[rx].misc & TSI108_RX_CRC)
775                                 data->stats.rx_crc_errors++;
776                         if (data->rxring[rx].misc & TSI108_RX_OVER)
777                                 data->stats.rx_fifo_errors++;
778
779                         spin_unlock_irq(&data->misclock);
780
781                         dev_kfree_skb_any(skb);
782                         continue;
783                 }
784                 if (netif_msg_pktdata(data)) {
785                         int i;
786                         printk("%s: Rx Frame contents (%d)\n",
787                                dev->name, data->rxring[rx].len);
788                         for (i = 0; i < data->rxring[rx].len; i++)
789                                 printk(" %2.2x", skb->data[i]);
790                         printk(".\n");
791                 }
792
793                 skb_put(skb, data->rxring[rx].len);
794                 skb->protocol = eth_type_trans(skb, dev);
795                 netif_receive_skb(skb);
796         }
797
798         return done;
799 }
800
801 static int tsi108_refill_rx(struct net_device *dev, int budget)
802 {
803         struct tsi108_prv_data *data = netdev_priv(dev);
804         int done = 0;
805
806         while (data->rxfree != TSI108_RXRING_LEN && done != budget) {
807                 int rx = data->rxhead;
808                 struct sk_buff *skb;
809
810                 skb = netdev_alloc_skb_ip_align(dev, TSI108_RXBUF_SIZE);
811                 data->rxskbs[rx] = skb;
812                 if (!skb)
813                         break;
814
815                 data->rxring[rx].buf0 = dma_map_single(&data->pdev->dev,
816                                 skb->data, TSI108_RX_SKB_SIZE,
817                                 DMA_FROM_DEVICE);
818
819                 /* Sometimes the hardware sets blen to zero after packet
820                  * reception, even though the manual says that it's only ever
821                  * modified by the driver.
822                  */
823
824                 data->rxring[rx].blen = TSI108_RX_SKB_SIZE;
825                 data->rxring[rx].misc = TSI108_RX_OWN | TSI108_RX_INT;
826
827                 data->rxhead = (data->rxhead + 1) % TSI108_RXRING_LEN;
828                 data->rxfree++;
829                 done++;
830         }
831
832         if (done != 0 && !(TSI_READ(TSI108_EC_RXSTAT) &
833                            TSI108_EC_RXSTAT_QUEUE0))
834                 tsi108_restart_rx(data, dev);
835
836         return done;
837 }
838
839 static int tsi108_poll(struct napi_struct *napi, int budget)
840 {
841         struct tsi108_prv_data *data = container_of(napi, struct tsi108_prv_data, napi);
842         struct net_device *dev = data->dev;
843         u32 estat = TSI_READ(TSI108_EC_RXESTAT);
844         u32 intstat = TSI_READ(TSI108_EC_INTSTAT);
845         int num_received = 0, num_filled = 0;
846
847         intstat &= TSI108_INT_RXQUEUE0 | TSI108_INT_RXTHRESH |
848             TSI108_INT_RXOVERRUN | TSI108_INT_RXERROR | TSI108_INT_RXWAIT;
849
850         TSI_WRITE(TSI108_EC_RXESTAT, estat);
851         TSI_WRITE(TSI108_EC_INTSTAT, intstat);
852
853         if (data->rxpending || (estat & TSI108_EC_RXESTAT_Q0_DESCINT))
854                 num_received = tsi108_complete_rx(dev, budget);
855
856         /* This should normally fill no more slots than the number of
857          * packets received in tsi108_complete_rx().  The exception
858          * is when we previously ran out of memory for RX SKBs.  In that
859          * case, it's helpful to obey the budget, not only so that the
860          * CPU isn't hogged, but so that memory (which may still be low)
861          * is not hogged by one device.
862          *
863          * A work unit is considered to be two SKBs to allow us to catch
864          * up when the ring has shrunk due to out-of-memory but we're
865          * still removing the full budget's worth of packets each time.
866          */
867
868         if (data->rxfree < TSI108_RXRING_LEN)
869                 num_filled = tsi108_refill_rx(dev, budget * 2);
870
871         if (intstat & TSI108_INT_RXERROR) {
872                 u32 err = TSI_READ(TSI108_EC_RXERR);
873                 TSI_WRITE(TSI108_EC_RXERR, err);
874
875                 if (err) {
876                         if (net_ratelimit())
877                                 printk(KERN_DEBUG "%s: RX error %x\n",
878                                        dev->name, err);
879
880                         if (!(TSI_READ(TSI108_EC_RXSTAT) &
881                               TSI108_EC_RXSTAT_QUEUE0))
882                                 tsi108_restart_rx(data, dev);
883                 }
884         }
885
886         if (intstat & TSI108_INT_RXOVERRUN) {
887                 spin_lock_irq(&data->misclock);
888                 data->stats.rx_fifo_errors++;
889                 spin_unlock_irq(&data->misclock);
890         }
891
892         if (num_received < budget) {
893                 data->rxpending = 0;
894                 napi_complete_done(napi, num_received);
895
896                 TSI_WRITE(TSI108_EC_INTMASK,
897                                      TSI_READ(TSI108_EC_INTMASK)
898                                      & ~(TSI108_INT_RXQUEUE0
899                                          | TSI108_INT_RXTHRESH |
900                                          TSI108_INT_RXOVERRUN |
901                                          TSI108_INT_RXERROR |
902                                          TSI108_INT_RXWAIT));
903         } else {
904                 data->rxpending = 1;
905         }
906
907         return num_received;
908 }
909
910 static void tsi108_rx_int(struct net_device *dev)
911 {
912         struct tsi108_prv_data *data = netdev_priv(dev);
913
914         /* A race could cause dev to already be scheduled, so it's not an
915          * error if that happens (and interrupts shouldn't be re-masked,
916          * because that can cause harmful races, if poll has already
917          * unmasked them but not cleared LINK_STATE_SCHED).
918          *
919          * This can happen if this code races with tsi108_poll(), which masks
920          * the interrupts after tsi108_irq_one() read the mask, but before
921          * napi_schedule is called.  It could also happen due to calls
922          * from tsi108_check_rxring().
923          */
924
925         if (napi_schedule_prep(&data->napi)) {
926                 /* Mask, rather than ack, the receive interrupts.  The ack
927                  * will happen in tsi108_poll().
928                  */
929
930                 TSI_WRITE(TSI108_EC_INTMASK,
931                                      TSI_READ(TSI108_EC_INTMASK) |
932                                      TSI108_INT_RXQUEUE0
933                                      | TSI108_INT_RXTHRESH |
934                                      TSI108_INT_RXOVERRUN | TSI108_INT_RXERROR |
935                                      TSI108_INT_RXWAIT);
936                 __napi_schedule(&data->napi);
937         } else {
938                 if (!netif_running(dev)) {
939                         /* This can happen if an interrupt occurs while the
940                          * interface is being brought down, as the START
941                          * bit is cleared before the stop function is called.
942                          *
943                          * In this case, the interrupts must be masked, or
944                          * they will continue indefinitely.
945                          *
946                          * There's a race here if the interface is brought down
947                          * and then up in rapid succession, as the device could
948                          * be made running after the above check and before
949                          * the masking below.  This will only happen if the IRQ
950                          * thread has a lower priority than the task brining
951                          * up the interface.  Fixing this race would likely
952                          * require changes in generic code.
953                          */
954
955                         TSI_WRITE(TSI108_EC_INTMASK,
956                                              TSI_READ
957                                              (TSI108_EC_INTMASK) |
958                                              TSI108_INT_RXQUEUE0 |
959                                              TSI108_INT_RXTHRESH |
960                                              TSI108_INT_RXOVERRUN |
961                                              TSI108_INT_RXERROR |
962                                              TSI108_INT_RXWAIT);
963                 }
964         }
965 }
966
967 /* If the RX ring has run out of memory, try periodically
968  * to allocate some more, as otherwise poll would never
969  * get called (apart from the initial end-of-queue condition).
970  *
971  * This is called once per second (by default) from the thread.
972  */
973
974 static void tsi108_check_rxring(struct net_device *dev)
975 {
976         struct tsi108_prv_data *data = netdev_priv(dev);
977
978         /* A poll is scheduled, as opposed to caling tsi108_refill_rx
979          * directly, so as to keep the receive path single-threaded
980          * (and thus not needing a lock).
981          */
982
983         if (netif_running(dev) && data->rxfree < TSI108_RXRING_LEN / 4)
984                 tsi108_rx_int(dev);
985 }
986
987 static void tsi108_tx_int(struct net_device *dev)
988 {
989         struct tsi108_prv_data *data = netdev_priv(dev);
990         u32 estat = TSI_READ(TSI108_EC_TXESTAT);
991
992         TSI_WRITE(TSI108_EC_TXESTAT, estat);
993         TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_TXQUEUE0 |
994                              TSI108_INT_TXIDLE | TSI108_INT_TXERROR);
995         if (estat & TSI108_EC_TXESTAT_Q0_ERR) {
996                 u32 err = TSI_READ(TSI108_EC_TXERR);
997                 TSI_WRITE(TSI108_EC_TXERR, err);
998
999                 if (err && net_ratelimit())
1000                         printk(KERN_ERR "%s: TX error %x\n", dev->name, err);
1001         }
1002
1003         if (estat & (TSI108_EC_TXESTAT_Q0_DESCINT | TSI108_EC_TXESTAT_Q0_EOQ)) {
1004                 spin_lock(&data->txlock);
1005                 tsi108_complete_tx(dev);
1006                 spin_unlock(&data->txlock);
1007         }
1008 }
1009
1010
1011 static irqreturn_t tsi108_irq(int irq, void *dev_id)
1012 {
1013         struct net_device *dev = dev_id;
1014         struct tsi108_prv_data *data = netdev_priv(dev);
1015         u32 stat = TSI_READ(TSI108_EC_INTSTAT);
1016
1017         if (!(stat & TSI108_INT_ANY))
1018                 return IRQ_NONE;        /* Not our interrupt */
1019
1020         stat &= ~TSI_READ(TSI108_EC_INTMASK);
1021
1022         if (stat & (TSI108_INT_TXQUEUE0 | TSI108_INT_TXIDLE |
1023                     TSI108_INT_TXERROR))
1024                 tsi108_tx_int(dev);
1025         if (stat & (TSI108_INT_RXQUEUE0 | TSI108_INT_RXTHRESH |
1026                     TSI108_INT_RXWAIT | TSI108_INT_RXOVERRUN |
1027                     TSI108_INT_RXERROR))
1028                 tsi108_rx_int(dev);
1029
1030         if (stat & TSI108_INT_SFN) {
1031                 if (net_ratelimit())
1032                         printk(KERN_DEBUG "%s: SFN error\n", dev->name);
1033                 TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_SFN);
1034         }
1035
1036         if (stat & TSI108_INT_STATCARRY) {
1037                 tsi108_stat_carry(dev);
1038                 TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_STATCARRY);
1039         }
1040
1041         return IRQ_HANDLED;
1042 }
1043
1044 static void tsi108_stop_ethernet(struct net_device *dev)
1045 {
1046         struct tsi108_prv_data *data = netdev_priv(dev);
1047         int i = 1000;
1048         /* Disable all TX and RX queues ... */
1049         TSI_WRITE(TSI108_EC_TXCTRL, 0);
1050         TSI_WRITE(TSI108_EC_RXCTRL, 0);
1051
1052         /* ...and wait for them to become idle */
1053         while(i--) {
1054                 if(!(TSI_READ(TSI108_EC_TXSTAT) & TSI108_EC_TXSTAT_ACTIVE))
1055                         break;
1056                 udelay(10);
1057         }
1058         i = 1000;
1059         while(i--){
1060                 if(!(TSI_READ(TSI108_EC_RXSTAT) & TSI108_EC_RXSTAT_ACTIVE))
1061                         return;
1062                 udelay(10);
1063         }
1064         printk(KERN_ERR "%s function time out\n", __func__);
1065 }
1066
1067 static void tsi108_reset_ether(struct tsi108_prv_data * data)
1068 {
1069         TSI_WRITE(TSI108_MAC_CFG1, TSI108_MAC_CFG1_SOFTRST);
1070         udelay(100);
1071         TSI_WRITE(TSI108_MAC_CFG1, 0);
1072
1073         TSI_WRITE(TSI108_EC_PORTCTRL, TSI108_EC_PORTCTRL_STATRST);
1074         udelay(100);
1075         TSI_WRITE(TSI108_EC_PORTCTRL,
1076                              TSI_READ(TSI108_EC_PORTCTRL) &
1077                              ~TSI108_EC_PORTCTRL_STATRST);
1078
1079         TSI_WRITE(TSI108_EC_TXCFG, TSI108_EC_TXCFG_RST);
1080         udelay(100);
1081         TSI_WRITE(TSI108_EC_TXCFG,
1082                              TSI_READ(TSI108_EC_TXCFG) &
1083                              ~TSI108_EC_TXCFG_RST);
1084
1085         TSI_WRITE(TSI108_EC_RXCFG, TSI108_EC_RXCFG_RST);
1086         udelay(100);
1087         TSI_WRITE(TSI108_EC_RXCFG,
1088                              TSI_READ(TSI108_EC_RXCFG) &
1089                              ~TSI108_EC_RXCFG_RST);
1090
1091         TSI_WRITE(TSI108_MAC_MII_MGMT_CFG,
1092                              TSI_READ(TSI108_MAC_MII_MGMT_CFG) |
1093                              TSI108_MAC_MII_MGMT_RST);
1094         udelay(100);
1095         TSI_WRITE(TSI108_MAC_MII_MGMT_CFG,
1096                              (TSI_READ(TSI108_MAC_MII_MGMT_CFG) &
1097                              ~(TSI108_MAC_MII_MGMT_RST |
1098                                TSI108_MAC_MII_MGMT_CLK)) | 0x07);
1099 }
1100
1101 static int tsi108_get_mac(struct net_device *dev)
1102 {
1103         struct tsi108_prv_data *data = netdev_priv(dev);
1104         u32 word1 = TSI_READ(TSI108_MAC_ADDR1);
1105         u32 word2 = TSI_READ(TSI108_MAC_ADDR2);
1106
1107         /* Note that the octets are reversed from what the manual says,
1108          * producing an even weirder ordering...
1109          */
1110         if (word2 == 0 && word1 == 0) {
1111                 dev->dev_addr[0] = 0x00;
1112                 dev->dev_addr[1] = 0x06;
1113                 dev->dev_addr[2] = 0xd2;
1114                 dev->dev_addr[3] = 0x00;
1115                 dev->dev_addr[4] = 0x00;
1116                 if (0x8 == data->phy)
1117                         dev->dev_addr[5] = 0x01;
1118                 else
1119                         dev->dev_addr[5] = 0x02;
1120
1121                 word2 = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 24);
1122
1123                 word1 = (dev->dev_addr[2] << 0) | (dev->dev_addr[3] << 8) |
1124                     (dev->dev_addr[4] << 16) | (dev->dev_addr[5] << 24);
1125
1126                 TSI_WRITE(TSI108_MAC_ADDR1, word1);
1127                 TSI_WRITE(TSI108_MAC_ADDR2, word2);
1128         } else {
1129                 dev->dev_addr[0] = (word2 >> 16) & 0xff;
1130                 dev->dev_addr[1] = (word2 >> 24) & 0xff;
1131                 dev->dev_addr[2] = (word1 >> 0) & 0xff;
1132                 dev->dev_addr[3] = (word1 >> 8) & 0xff;
1133                 dev->dev_addr[4] = (word1 >> 16) & 0xff;
1134                 dev->dev_addr[5] = (word1 >> 24) & 0xff;
1135         }
1136
1137         if (!is_valid_ether_addr(dev->dev_addr)) {
1138                 printk(KERN_ERR
1139                        "%s: Invalid MAC address. word1: %08x, word2: %08x\n",
1140                        dev->name, word1, word2);
1141                 return -EINVAL;
1142         }
1143
1144         return 0;
1145 }
1146
1147 static int tsi108_set_mac(struct net_device *dev, void *addr)
1148 {
1149         struct tsi108_prv_data *data = netdev_priv(dev);
1150         u32 word1, word2;
1151         int i;
1152
1153         if (!is_valid_ether_addr(addr))
1154                 return -EADDRNOTAVAIL;
1155
1156         for (i = 0; i < 6; i++)
1157                 /* +2 is for the offset of the HW addr type */
1158                 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1159
1160         word2 = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 24);
1161
1162         word1 = (dev->dev_addr[2] << 0) | (dev->dev_addr[3] << 8) |
1163             (dev->dev_addr[4] << 16) | (dev->dev_addr[5] << 24);
1164
1165         spin_lock_irq(&data->misclock);
1166         TSI_WRITE(TSI108_MAC_ADDR1, word1);
1167         TSI_WRITE(TSI108_MAC_ADDR2, word2);
1168         spin_lock(&data->txlock);
1169
1170         if (data->txfree && data->link_up)
1171                 netif_wake_queue(dev);
1172
1173         spin_unlock(&data->txlock);
1174         spin_unlock_irq(&data->misclock);
1175         return 0;
1176 }
1177
1178 /* Protected by dev->xmit_lock. */
1179 static void tsi108_set_rx_mode(struct net_device *dev)
1180 {
1181         struct tsi108_prv_data *data = netdev_priv(dev);
1182         u32 rxcfg = TSI_READ(TSI108_EC_RXCFG);
1183
1184         if (dev->flags & IFF_PROMISC) {
1185                 rxcfg &= ~(TSI108_EC_RXCFG_UC_HASH | TSI108_EC_RXCFG_MC_HASH);
1186                 rxcfg |= TSI108_EC_RXCFG_UFE | TSI108_EC_RXCFG_MFE;
1187                 goto out;
1188         }
1189
1190         rxcfg &= ~(TSI108_EC_RXCFG_UFE | TSI108_EC_RXCFG_MFE);
1191
1192         if (dev->flags & IFF_ALLMULTI || !netdev_mc_empty(dev)) {
1193                 int i;
1194                 struct netdev_hw_addr *ha;
1195                 rxcfg |= TSI108_EC_RXCFG_MFE | TSI108_EC_RXCFG_MC_HASH;
1196
1197                 memset(data->mc_hash, 0, sizeof(data->mc_hash));
1198
1199                 netdev_for_each_mc_addr(ha, dev) {
1200                         u32 hash, crc;
1201
1202                         crc = ether_crc(6, ha->addr);
1203                         hash = crc >> 23;
1204                         __set_bit(hash, &data->mc_hash[0]);
1205                 }
1206
1207                 TSI_WRITE(TSI108_EC_HASHADDR,
1208                                      TSI108_EC_HASHADDR_AUTOINC |
1209                                      TSI108_EC_HASHADDR_MCAST);
1210
1211                 for (i = 0; i < 16; i++) {
1212                         /* The manual says that the hardware may drop
1213                          * back-to-back writes to the data register.
1214                          */
1215                         udelay(1);
1216                         TSI_WRITE(TSI108_EC_HASHDATA,
1217                                              data->mc_hash[i]);
1218                 }
1219         }
1220
1221       out:
1222         TSI_WRITE(TSI108_EC_RXCFG, rxcfg);
1223 }
1224
1225 static void tsi108_init_phy(struct net_device *dev)
1226 {
1227         struct tsi108_prv_data *data = netdev_priv(dev);
1228         u32 i = 0;
1229         u16 phyval = 0;
1230         unsigned long flags;
1231
1232         spin_lock_irqsave(&phy_lock, flags);
1233
1234         tsi108_write_mii(data, MII_BMCR, BMCR_RESET);
1235         while (--i) {
1236                 if(!(tsi108_read_mii(data, MII_BMCR) & BMCR_RESET))
1237                         break;
1238                 udelay(10);
1239         }
1240         if (i == 0)
1241                 printk(KERN_ERR "%s function time out\n", __func__);
1242
1243         if (data->phy_type == TSI108_PHY_BCM54XX) {
1244                 tsi108_write_mii(data, 0x09, 0x0300);
1245                 tsi108_write_mii(data, 0x10, 0x1020);
1246                 tsi108_write_mii(data, 0x1c, 0x8c00);
1247         }
1248
1249         tsi108_write_mii(data,
1250                          MII_BMCR,
1251                          BMCR_ANENABLE | BMCR_ANRESTART);
1252         while (tsi108_read_mii(data, MII_BMCR) & BMCR_ANRESTART)
1253                 cpu_relax();
1254
1255         /* Set G/MII mode and receive clock select in TBI control #2.  The
1256          * second port won't work if this isn't done, even though we don't
1257          * use TBI mode.
1258          */
1259
1260         tsi108_write_tbi(data, 0x11, 0x30);
1261
1262         /* FIXME: It seems to take more than 2 back-to-back reads to the
1263          * PHY_STAT register before the link up status bit is set.
1264          */
1265
1266         data->link_up = 0;
1267
1268         while (!((phyval = tsi108_read_mii(data, MII_BMSR)) &
1269                  BMSR_LSTATUS)) {
1270                 if (i++ > (MII_READ_DELAY / 10)) {
1271                         break;
1272                 }
1273                 spin_unlock_irqrestore(&phy_lock, flags);
1274                 msleep(10);
1275                 spin_lock_irqsave(&phy_lock, flags);
1276         }
1277
1278         data->mii_if.supports_gmii = mii_check_gmii_support(&data->mii_if);
1279         printk(KERN_DEBUG "PHY_STAT reg contains %08x\n", phyval);
1280         data->phy_ok = 1;
1281         data->init_media = 1;
1282         spin_unlock_irqrestore(&phy_lock, flags);
1283 }
1284
1285 static void tsi108_kill_phy(struct net_device *dev)
1286 {
1287         struct tsi108_prv_data *data = netdev_priv(dev);
1288         unsigned long flags;
1289
1290         spin_lock_irqsave(&phy_lock, flags);
1291         tsi108_write_mii(data, MII_BMCR, BMCR_PDOWN);
1292         data->phy_ok = 0;
1293         spin_unlock_irqrestore(&phy_lock, flags);
1294 }
1295
1296 static int tsi108_open(struct net_device *dev)
1297 {
1298         int i;
1299         struct tsi108_prv_data *data = netdev_priv(dev);
1300         unsigned int rxring_size = TSI108_RXRING_LEN * sizeof(rx_desc);
1301         unsigned int txring_size = TSI108_TXRING_LEN * sizeof(tx_desc);
1302
1303         i = request_irq(data->irq_num, tsi108_irq, 0, dev->name, dev);
1304         if (i != 0) {
1305                 printk(KERN_ERR "tsi108_eth%d: Could not allocate IRQ%d.\n",
1306                        data->id, data->irq_num);
1307                 return i;
1308         } else {
1309                 dev->irq = data->irq_num;
1310                 printk(KERN_NOTICE
1311                        "tsi108_open : Port %d Assigned IRQ %d to %s\n",
1312                        data->id, dev->irq, dev->name);
1313         }
1314
1315         data->rxring = dma_zalloc_coherent(&data->pdev->dev, rxring_size,
1316                         &data->rxdma, GFP_KERNEL);
1317         if (!data->rxring)
1318                 return -ENOMEM;
1319
1320         data->txring = dma_zalloc_coherent(&data->pdev->dev, txring_size,
1321                         &data->txdma, GFP_KERNEL);
1322         if (!data->txring) {
1323                 dma_free_coherent(&data->pdev->dev, rxring_size, data->rxring,
1324                                     data->rxdma);
1325                 return -ENOMEM;
1326         }
1327
1328         for (i = 0; i < TSI108_RXRING_LEN; i++) {
1329                 data->rxring[i].next0 = data->rxdma + (i + 1) * sizeof(rx_desc);
1330                 data->rxring[i].blen = TSI108_RXBUF_SIZE;
1331                 data->rxring[i].vlan = 0;
1332         }
1333
1334         data->rxring[TSI108_RXRING_LEN - 1].next0 = data->rxdma;
1335
1336         data->rxtail = 0;
1337         data->rxhead = 0;
1338
1339         for (i = 0; i < TSI108_RXRING_LEN; i++) {
1340                 struct sk_buff *skb;
1341
1342                 skb = netdev_alloc_skb_ip_align(dev, TSI108_RXBUF_SIZE);
1343                 if (!skb) {
1344                         /* Bah.  No memory for now, but maybe we'll get
1345                          * some more later.
1346                          * For now, we'll live with the smaller ring.
1347                          */
1348                         printk(KERN_WARNING
1349                                "%s: Could only allocate %d receive skb(s).\n",
1350                                dev->name, i);
1351                         data->rxhead = i;
1352                         break;
1353                 }
1354
1355                 data->rxskbs[i] = skb;
1356                 data->rxring[i].buf0 = virt_to_phys(data->rxskbs[i]->data);
1357                 data->rxring[i].misc = TSI108_RX_OWN | TSI108_RX_INT;
1358         }
1359
1360         data->rxfree = i;
1361         TSI_WRITE(TSI108_EC_RXQ_PTRLOW, data->rxdma);
1362
1363         for (i = 0; i < TSI108_TXRING_LEN; i++) {
1364                 data->txring[i].next0 = data->txdma + (i + 1) * sizeof(tx_desc);
1365                 data->txring[i].misc = 0;
1366         }
1367
1368         data->txring[TSI108_TXRING_LEN - 1].next0 = data->txdma;
1369         data->txtail = 0;
1370         data->txhead = 0;
1371         data->txfree = TSI108_TXRING_LEN;
1372         TSI_WRITE(TSI108_EC_TXQ_PTRLOW, data->txdma);
1373         tsi108_init_phy(dev);
1374
1375         napi_enable(&data->napi);
1376
1377         timer_setup(&data->timer, tsi108_timed_checker, 0);
1378         mod_timer(&data->timer, jiffies + 1);
1379
1380         tsi108_restart_rx(data, dev);
1381
1382         TSI_WRITE(TSI108_EC_INTSTAT, ~0);
1383
1384         TSI_WRITE(TSI108_EC_INTMASK,
1385                              ~(TSI108_INT_TXQUEUE0 | TSI108_INT_RXERROR |
1386                                TSI108_INT_RXTHRESH | TSI108_INT_RXQUEUE0 |
1387                                TSI108_INT_RXOVERRUN | TSI108_INT_RXWAIT |
1388                                TSI108_INT_SFN | TSI108_INT_STATCARRY));
1389
1390         TSI_WRITE(TSI108_MAC_CFG1,
1391                              TSI108_MAC_CFG1_RXEN | TSI108_MAC_CFG1_TXEN);
1392         netif_start_queue(dev);
1393         return 0;
1394 }
1395
1396 static int tsi108_close(struct net_device *dev)
1397 {
1398         struct tsi108_prv_data *data = netdev_priv(dev);
1399
1400         netif_stop_queue(dev);
1401         napi_disable(&data->napi);
1402
1403         del_timer_sync(&data->timer);
1404
1405         tsi108_stop_ethernet(dev);
1406         tsi108_kill_phy(dev);
1407         TSI_WRITE(TSI108_EC_INTMASK, ~0);
1408         TSI_WRITE(TSI108_MAC_CFG1, 0);
1409
1410         /* Check for any pending TX packets, and drop them. */
1411
1412         while (!data->txfree || data->txhead != data->txtail) {
1413                 int tx = data->txtail;
1414                 struct sk_buff *skb;
1415                 skb = data->txskbs[tx];
1416                 data->txtail = (data->txtail + 1) % TSI108_TXRING_LEN;
1417                 data->txfree++;
1418                 dev_kfree_skb(skb);
1419         }
1420
1421         free_irq(data->irq_num, dev);
1422
1423         /* Discard the RX ring. */
1424
1425         while (data->rxfree) {
1426                 int rx = data->rxtail;
1427                 struct sk_buff *skb;
1428
1429                 skb = data->rxskbs[rx];
1430                 data->rxtail = (data->rxtail + 1) % TSI108_RXRING_LEN;
1431                 data->rxfree--;
1432                 dev_kfree_skb(skb);
1433         }
1434
1435         dma_free_coherent(&data->pdev->dev,
1436                             TSI108_RXRING_LEN * sizeof(rx_desc),
1437                             data->rxring, data->rxdma);
1438         dma_free_coherent(&data->pdev->dev,
1439                             TSI108_TXRING_LEN * sizeof(tx_desc),
1440                             data->txring, data->txdma);
1441
1442         return 0;
1443 }
1444
1445 static void tsi108_init_mac(struct net_device *dev)
1446 {
1447         struct tsi108_prv_data *data = netdev_priv(dev);
1448
1449         TSI_WRITE(TSI108_MAC_CFG2, TSI108_MAC_CFG2_DFLT_PREAMBLE |
1450                              TSI108_MAC_CFG2_PADCRC);
1451
1452         TSI_WRITE(TSI108_EC_TXTHRESH,
1453                              (192 << TSI108_EC_TXTHRESH_STARTFILL) |
1454                              (192 << TSI108_EC_TXTHRESH_STOPFILL));
1455
1456         TSI_WRITE(TSI108_STAT_CARRYMASK1,
1457                              ~(TSI108_STAT_CARRY1_RXBYTES |
1458                                TSI108_STAT_CARRY1_RXPKTS |
1459                                TSI108_STAT_CARRY1_RXFCS |
1460                                TSI108_STAT_CARRY1_RXMCAST |
1461                                TSI108_STAT_CARRY1_RXALIGN |
1462                                TSI108_STAT_CARRY1_RXLENGTH |
1463                                TSI108_STAT_CARRY1_RXRUNT |
1464                                TSI108_STAT_CARRY1_RXJUMBO |
1465                                TSI108_STAT_CARRY1_RXFRAG |
1466                                TSI108_STAT_CARRY1_RXJABBER |
1467                                TSI108_STAT_CARRY1_RXDROP));
1468
1469         TSI_WRITE(TSI108_STAT_CARRYMASK2,
1470                              ~(TSI108_STAT_CARRY2_TXBYTES |
1471                                TSI108_STAT_CARRY2_TXPKTS |
1472                                TSI108_STAT_CARRY2_TXEXDEF |
1473                                TSI108_STAT_CARRY2_TXEXCOL |
1474                                TSI108_STAT_CARRY2_TXTCOL |
1475                                TSI108_STAT_CARRY2_TXPAUSE));
1476
1477         TSI_WRITE(TSI108_EC_PORTCTRL, TSI108_EC_PORTCTRL_STATEN);
1478         TSI_WRITE(TSI108_MAC_CFG1, 0);
1479
1480         TSI_WRITE(TSI108_EC_RXCFG,
1481                              TSI108_EC_RXCFG_SE | TSI108_EC_RXCFG_BFE);
1482
1483         TSI_WRITE(TSI108_EC_TXQ_CFG, TSI108_EC_TXQ_CFG_DESC_INT |
1484                              TSI108_EC_TXQ_CFG_EOQ_OWN_INT |
1485                              TSI108_EC_TXQ_CFG_WSWP | (TSI108_PBM_PORT <<
1486                                                 TSI108_EC_TXQ_CFG_SFNPORT));
1487
1488         TSI_WRITE(TSI108_EC_RXQ_CFG, TSI108_EC_RXQ_CFG_DESC_INT |
1489                              TSI108_EC_RXQ_CFG_EOQ_OWN_INT |
1490                              TSI108_EC_RXQ_CFG_WSWP | (TSI108_PBM_PORT <<
1491                                                 TSI108_EC_RXQ_CFG_SFNPORT));
1492
1493         TSI_WRITE(TSI108_EC_TXQ_BUFCFG,
1494                              TSI108_EC_TXQ_BUFCFG_BURST256 |
1495                              TSI108_EC_TXQ_BUFCFG_BSWP | (TSI108_PBM_PORT <<
1496                                                 TSI108_EC_TXQ_BUFCFG_SFNPORT));
1497
1498         TSI_WRITE(TSI108_EC_RXQ_BUFCFG,
1499                              TSI108_EC_RXQ_BUFCFG_BURST256 |
1500                              TSI108_EC_RXQ_BUFCFG_BSWP | (TSI108_PBM_PORT <<
1501                                                 TSI108_EC_RXQ_BUFCFG_SFNPORT));
1502
1503         TSI_WRITE(TSI108_EC_INTMASK, ~0);
1504 }
1505
1506 static int tsi108_get_link_ksettings(struct net_device *dev,
1507                                      struct ethtool_link_ksettings *cmd)
1508 {
1509         struct tsi108_prv_data *data = netdev_priv(dev);
1510         unsigned long flags;
1511
1512         spin_lock_irqsave(&data->txlock, flags);
1513         mii_ethtool_get_link_ksettings(&data->mii_if, cmd);
1514         spin_unlock_irqrestore(&data->txlock, flags);
1515
1516         return 0;
1517 }
1518
1519 static int tsi108_set_link_ksettings(struct net_device *dev,
1520                                      const struct ethtool_link_ksettings *cmd)
1521 {
1522         struct tsi108_prv_data *data = netdev_priv(dev);
1523         unsigned long flags;
1524         int rc;
1525
1526         spin_lock_irqsave(&data->txlock, flags);
1527         rc = mii_ethtool_set_link_ksettings(&data->mii_if, cmd);
1528         spin_unlock_irqrestore(&data->txlock, flags);
1529
1530         return rc;
1531 }
1532
1533 static int tsi108_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1534 {
1535         struct tsi108_prv_data *data = netdev_priv(dev);
1536         if (!netif_running(dev))
1537                 return -EINVAL;
1538         return generic_mii_ioctl(&data->mii_if, if_mii(rq), cmd, NULL);
1539 }
1540
1541 static const struct ethtool_ops tsi108_ethtool_ops = {
1542         .get_link       = ethtool_op_get_link,
1543         .get_link_ksettings     = tsi108_get_link_ksettings,
1544         .set_link_ksettings     = tsi108_set_link_ksettings,
1545 };
1546
1547 static const struct net_device_ops tsi108_netdev_ops = {
1548         .ndo_open               = tsi108_open,
1549         .ndo_stop               = tsi108_close,
1550         .ndo_start_xmit         = tsi108_send_packet,
1551         .ndo_set_rx_mode        = tsi108_set_rx_mode,
1552         .ndo_get_stats          = tsi108_get_stats,
1553         .ndo_do_ioctl           = tsi108_do_ioctl,
1554         .ndo_set_mac_address    = tsi108_set_mac,
1555         .ndo_validate_addr      = eth_validate_addr,
1556 };
1557
1558 static int
1559 tsi108_init_one(struct platform_device *pdev)
1560 {
1561         struct net_device *dev = NULL;
1562         struct tsi108_prv_data *data = NULL;
1563         hw_info *einfo;
1564         int err = 0;
1565
1566         einfo = dev_get_platdata(&pdev->dev);
1567
1568         if (NULL == einfo) {
1569                 printk(KERN_ERR "tsi-eth %d: Missing additional data!\n",
1570                        pdev->id);
1571                 return -ENODEV;
1572         }
1573
1574         /* Create an ethernet device instance */
1575
1576         dev = alloc_etherdev(sizeof(struct tsi108_prv_data));
1577         if (!dev)
1578                 return -ENOMEM;
1579
1580         printk("tsi108_eth%d: probe...\n", pdev->id);
1581         data = netdev_priv(dev);
1582         data->dev = dev;
1583         data->pdev = pdev;
1584
1585         pr_debug("tsi108_eth%d:regs:phyresgs:phy:irq_num=0x%x:0x%x:0x%x:0x%x\n",
1586                         pdev->id, einfo->regs, einfo->phyregs,
1587                         einfo->phy, einfo->irq_num);
1588
1589         data->regs = ioremap(einfo->regs, 0x400);
1590         if (NULL == data->regs) {
1591                 err = -ENOMEM;
1592                 goto regs_fail;
1593         }
1594
1595         data->phyregs = ioremap(einfo->phyregs, 0x400);
1596         if (NULL == data->phyregs) {
1597                 err = -ENOMEM;
1598                 goto phyregs_fail;
1599         }
1600 /* MII setup */
1601         data->mii_if.dev = dev;
1602         data->mii_if.mdio_read = tsi108_mdio_read;
1603         data->mii_if.mdio_write = tsi108_mdio_write;
1604         data->mii_if.phy_id = einfo->phy;
1605         data->mii_if.phy_id_mask = 0x1f;
1606         data->mii_if.reg_num_mask = 0x1f;
1607
1608         data->phy = einfo->phy;
1609         data->phy_type = einfo->phy_type;
1610         data->irq_num = einfo->irq_num;
1611         data->id = pdev->id;
1612         netif_napi_add(dev, &data->napi, tsi108_poll, 64);
1613         dev->netdev_ops = &tsi108_netdev_ops;
1614         dev->ethtool_ops = &tsi108_ethtool_ops;
1615
1616         /* Apparently, the Linux networking code won't use scatter-gather
1617          * if the hardware doesn't do checksums.  However, it's faster
1618          * to checksum in place and use SG, as (among other reasons)
1619          * the cache won't be dirtied (which then has to be flushed
1620          * before DMA).  The checksumming is done by the driver (via
1621          * a new function skb_csum_dev() in net/core/skbuff.c).
1622          */
1623
1624         dev->features = NETIF_F_HIGHDMA;
1625
1626         spin_lock_init(&data->txlock);
1627         spin_lock_init(&data->misclock);
1628
1629         tsi108_reset_ether(data);
1630         tsi108_kill_phy(dev);
1631
1632         if ((err = tsi108_get_mac(dev)) != 0) {
1633                 printk(KERN_ERR "%s: Invalid MAC address.  Please correct.\n",
1634                        dev->name);
1635                 goto register_fail;
1636         }
1637
1638         tsi108_init_mac(dev);
1639         err = register_netdev(dev);
1640         if (err) {
1641                 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
1642                                 dev->name);
1643                 goto register_fail;
1644         }
1645
1646         platform_set_drvdata(pdev, dev);
1647         printk(KERN_INFO "%s: Tsi108 Gigabit Ethernet, MAC: %pM\n",
1648                dev->name, dev->dev_addr);
1649 #ifdef DEBUG
1650         data->msg_enable = DEBUG;
1651         dump_eth_one(dev);
1652 #endif
1653
1654         return 0;
1655
1656 register_fail:
1657         iounmap(data->phyregs);
1658
1659 phyregs_fail:
1660         iounmap(data->regs);
1661
1662 regs_fail:
1663         free_netdev(dev);
1664         return err;
1665 }
1666
1667 /* There's no way to either get interrupts from the PHY when
1668  * something changes, or to have the Tsi108 automatically communicate
1669  * with the PHY to reconfigure itself.
1670  *
1671  * Thus, we have to do it using a timer.
1672  */
1673
1674 static void tsi108_timed_checker(struct timer_list *t)
1675 {
1676         struct tsi108_prv_data *data = from_timer(data, t, timer);
1677         struct net_device *dev = data->dev;
1678
1679         tsi108_check_phy(dev);
1680         tsi108_check_rxring(dev);
1681         mod_timer(&data->timer, jiffies + CHECK_PHY_INTERVAL);
1682 }
1683
1684 static int tsi108_ether_remove(struct platform_device *pdev)
1685 {
1686         struct net_device *dev = platform_get_drvdata(pdev);
1687         struct tsi108_prv_data *priv = netdev_priv(dev);
1688
1689         unregister_netdev(dev);
1690         tsi108_stop_ethernet(dev);
1691         iounmap(priv->regs);
1692         iounmap(priv->phyregs);
1693         free_netdev(dev);
1694
1695         return 0;
1696 }
1697 module_platform_driver(tsi_eth_driver);
1698
1699 MODULE_AUTHOR("Tundra Semiconductor Corporation");
1700 MODULE_DESCRIPTION("Tsi108 Gigabit Ethernet driver");
1701 MODULE_LICENSE("GPL");
1702 MODULE_ALIAS("platform:tsi-ethernet");