Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1 | /* Silan SC92031 PCI Fast Ethernet Adapter driver |
| 2 | * |
| 3 | * Based on vendor drivers: |
| 4 | * Silan Fast Ethernet Netcard Driver: |
| 5 | * MODULE_AUTHOR ("gaoyonghong"); |
| 6 | * MODULE_DESCRIPTION ("SILAN Fast Ethernet driver"); |
| 7 | * MODULE_LICENSE("GPL"); |
| 8 | * 8139D Fast Ethernet driver: |
| 9 | * (C) 2002 by gaoyonghong |
| 10 | * MODULE_AUTHOR ("gaoyonghong"); |
| 11 | * MODULE_DESCRIPTION ("Rsltek 8139D PCI Fast Ethernet Adapter driver"); |
| 12 | * MODULE_LICENSE("GPL"); |
| 13 | * Both are almost identical and seem to be based on pci-skeleton.c |
| 14 | * |
| 15 | * Rewritten for 2.6 by Cesar Eduardo Barros |
| 16 | */ |
| 17 | |
| 18 | /* Note about set_mac_address: I don't know how to change the hardware |
| 19 | * matching, so you need to enable IFF_PROMISC when using it. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/pci.h> |
| 26 | #include <linux/dma-mapping.h> |
| 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/etherdevice.h> |
| 29 | #include <linux/ethtool.h> |
| 30 | #include <linux/crc32.h> |
| 31 | |
| 32 | #include <asm/irq.h> |
| 33 | |
| 34 | #define PCI_VENDOR_ID_SILAN 0x1904 |
| 35 | #define PCI_DEVICE_ID_SILAN_SC92031 0x2031 |
| 36 | #define PCI_DEVICE_ID_SILAN_8139D 0x8139 |
| 37 | |
| 38 | #define SC92031_NAME "sc92031" |
| 39 | #define SC92031_DESCRIPTION "Silan SC92031 PCI Fast Ethernet Adapter driver" |
| 40 | #define SC92031_VERSION "2.0c" |
| 41 | |
| 42 | /* BAR 0 is MMIO, BAR 1 is PIO */ |
| 43 | #ifndef SC92031_USE_BAR |
| 44 | #define SC92031_USE_BAR 0 |
| 45 | #endif |
| 46 | |
| 47 | /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). */ |
| 48 | static int multicast_filter_limit = 64; |
| 49 | module_param(multicast_filter_limit, int, 0); |
| 50 | MODULE_PARM_DESC(multicast_filter_limit, |
| 51 | "Maximum number of filtered multicast addresses"); |
| 52 | |
| 53 | static int media; |
| 54 | module_param(media, int, 0); |
| 55 | MODULE_PARM_DESC(media, "Media type (0x00 = autodetect," |
| 56 | " 0x01 = 10M half, 0x02 = 10M full," |
| 57 | " 0x04 = 100M half, 0x08 = 100M full)"); |
| 58 | |
| 59 | /* Size of the in-memory receive ring. */ |
| 60 | #define RX_BUF_LEN_IDX 3 /* 0==8K, 1==16K, 2==32K, 3==64K ,4==128K*/ |
| 61 | #define RX_BUF_LEN (8192 << RX_BUF_LEN_IDX) |
| 62 | |
| 63 | /* Number of Tx descriptor registers. */ |
| 64 | #define NUM_TX_DESC 4 |
| 65 | |
| 66 | /* max supported ethernet frame size -- must be at least (dev->mtu+14+4).*/ |
| 67 | #define MAX_ETH_FRAME_SIZE 1536 |
| 68 | |
| 69 | /* Size of the Tx bounce buffers -- must be at least (dev->mtu+14+4). */ |
| 70 | #define TX_BUF_SIZE MAX_ETH_FRAME_SIZE |
| 71 | #define TX_BUF_TOT_LEN (TX_BUF_SIZE * NUM_TX_DESC) |
| 72 | |
| 73 | /* The following settings are log_2(bytes)-4: 0 == 16 bytes .. 6==1024, 7==end of packet. */ |
| 74 | #define RX_FIFO_THRESH 7 /* Rx buffer level before first PCI xfer. */ |
| 75 | |
| 76 | /* Time in jiffies before concluding the transmitter is hung. */ |
| 77 | #define TX_TIMEOUT (4*HZ) |
| 78 | |
| 79 | #define SILAN_STATS_NUM 2 /* number of ETHTOOL_GSTATS */ |
| 80 | |
| 81 | /* media options */ |
| 82 | #define AUTOSELECT 0x00 |
| 83 | #define M10_HALF 0x01 |
| 84 | #define M10_FULL 0x02 |
| 85 | #define M100_HALF 0x04 |
| 86 | #define M100_FULL 0x08 |
| 87 | |
| 88 | /* Symbolic offsets to registers. */ |
| 89 | enum silan_registers { |
| 90 | Config0 = 0x00, // Config0 |
| 91 | Config1 = 0x04, // Config1 |
| 92 | RxBufWPtr = 0x08, // Rx buffer writer poiter |
| 93 | IntrStatus = 0x0C, // Interrupt status |
| 94 | IntrMask = 0x10, // Interrupt mask |
| 95 | RxbufAddr = 0x14, // Rx buffer start address |
| 96 | RxBufRPtr = 0x18, // Rx buffer read pointer |
| 97 | Txstatusall = 0x1C, // Transmit status of all descriptors |
| 98 | TxStatus0 = 0x20, // Transmit status (Four 32bit registers). |
| 99 | TxAddr0 = 0x30, // Tx descriptors (also four 32bit). |
| 100 | RxConfig = 0x40, // Rx configuration |
| 101 | MAC0 = 0x44, // Ethernet hardware address. |
| 102 | MAR0 = 0x4C, // Multicast filter. |
| 103 | RxStatus0 = 0x54, // Rx status |
| 104 | TxConfig = 0x5C, // Tx configuration |
| 105 | PhyCtrl = 0x60, // physical control |
| 106 | FlowCtrlConfig = 0x64, // flow control |
| 107 | Miicmd0 = 0x68, // Mii command0 register |
| 108 | Miicmd1 = 0x6C, // Mii command1 register |
| 109 | Miistatus = 0x70, // Mii status register |
| 110 | Timercnt = 0x74, // Timer counter register |
| 111 | TimerIntr = 0x78, // Timer interrupt register |
| 112 | PMConfig = 0x7C, // Power Manager configuration |
| 113 | CRC0 = 0x80, // Power Manager CRC ( Two 32bit regisers) |
| 114 | Wakeup0 = 0x88, // power Manager wakeup( Eight 64bit regiser) |
| 115 | LSBCRC0 = 0xC8, // power Manager LSBCRC(Two 32bit regiser) |
| 116 | TestD0 = 0xD0, |
| 117 | TestD4 = 0xD4, |
| 118 | TestD8 = 0xD8, |
| 119 | }; |
| 120 | |
| 121 | #define MII_BMCR 0 // Basic mode control register |
| 122 | #define MII_BMSR 1 // Basic mode status register |
| 123 | #define MII_JAB 16 |
| 124 | #define MII_OutputStatus 24 |
| 125 | |
| 126 | #define BMCR_FULLDPLX 0x0100 // Full duplex |
| 127 | #define BMCR_ANRESTART 0x0200 // Auto negotiation restart |
| 128 | #define BMCR_ANENABLE 0x1000 // Enable auto negotiation |
| 129 | #define BMCR_SPEED100 0x2000 // Select 100Mbps |
| 130 | #define BMSR_LSTATUS 0x0004 // Link status |
| 131 | #define PHY_16_JAB_ENB 0x1000 |
| 132 | #define PHY_16_PORT_ENB 0x1 |
| 133 | |
| 134 | enum IntrStatusBits { |
| 135 | LinkFail = 0x80000000, |
| 136 | LinkOK = 0x40000000, |
| 137 | TimeOut = 0x20000000, |
| 138 | RxOverflow = 0x0040, |
| 139 | RxOK = 0x0020, |
| 140 | TxOK = 0x0001, |
| 141 | IntrBits = LinkFail|LinkOK|TimeOut|RxOverflow|RxOK|TxOK, |
| 142 | }; |
| 143 | |
| 144 | enum TxStatusBits { |
| 145 | TxCarrierLost = 0x20000000, |
| 146 | TxAborted = 0x10000000, |
| 147 | TxOutOfWindow = 0x08000000, |
| 148 | TxNccShift = 22, |
| 149 | EarlyTxThresShift = 16, |
| 150 | TxStatOK = 0x8000, |
| 151 | TxUnderrun = 0x4000, |
| 152 | TxOwn = 0x2000, |
| 153 | }; |
| 154 | |
| 155 | enum RxStatusBits { |
| 156 | RxStatesOK = 0x80000, |
| 157 | RxBadAlign = 0x40000, |
| 158 | RxHugeFrame = 0x20000, |
| 159 | RxSmallFrame = 0x10000, |
| 160 | RxCRCOK = 0x8000, |
| 161 | RxCrlFrame = 0x4000, |
| 162 | Rx_Broadcast = 0x2000, |
| 163 | Rx_Multicast = 0x1000, |
| 164 | RxAddrMatch = 0x0800, |
| 165 | MiiErr = 0x0400, |
| 166 | }; |
| 167 | |
| 168 | enum RxConfigBits { |
| 169 | RxFullDx = 0x80000000, |
| 170 | RxEnb = 0x40000000, |
| 171 | RxSmall = 0x20000000, |
| 172 | RxHuge = 0x10000000, |
| 173 | RxErr = 0x08000000, |
| 174 | RxAllphys = 0x04000000, |
| 175 | RxMulticast = 0x02000000, |
| 176 | RxBroadcast = 0x01000000, |
| 177 | RxLoopBack = (1 << 23) | (1 << 22), |
| 178 | LowThresholdShift = 12, |
| 179 | HighThresholdShift = 2, |
| 180 | }; |
| 181 | |
| 182 | enum TxConfigBits { |
| 183 | TxFullDx = 0x80000000, |
| 184 | TxEnb = 0x40000000, |
| 185 | TxEnbPad = 0x20000000, |
| 186 | TxEnbHuge = 0x10000000, |
| 187 | TxEnbFCS = 0x08000000, |
| 188 | TxNoBackOff = 0x04000000, |
| 189 | TxEnbPrem = 0x02000000, |
| 190 | TxCareLostCrs = 0x1000000, |
| 191 | TxExdCollNum = 0xf00000, |
| 192 | TxDataRate = 0x80000, |
| 193 | }; |
| 194 | |
| 195 | enum PhyCtrlconfigbits { |
| 196 | PhyCtrlAne = 0x80000000, |
| 197 | PhyCtrlSpd100 = 0x40000000, |
| 198 | PhyCtrlSpd10 = 0x20000000, |
| 199 | PhyCtrlPhyBaseAddr = 0x1f000000, |
| 200 | PhyCtrlDux = 0x800000, |
| 201 | PhyCtrlReset = 0x400000, |
| 202 | }; |
| 203 | |
| 204 | enum FlowCtrlConfigBits { |
| 205 | FlowCtrlFullDX = 0x80000000, |
| 206 | FlowCtrlEnb = 0x40000000, |
| 207 | }; |
| 208 | |
| 209 | enum Config0Bits { |
| 210 | Cfg0_Reset = 0x80000000, |
| 211 | Cfg0_Anaoff = 0x40000000, |
| 212 | Cfg0_LDPS = 0x20000000, |
| 213 | }; |
| 214 | |
| 215 | enum Config1Bits { |
| 216 | Cfg1_EarlyRx = 1 << 31, |
| 217 | Cfg1_EarlyTx = 1 << 30, |
| 218 | |
| 219 | //rx buffer size |
| 220 | Cfg1_Rcv8K = 0x0, |
| 221 | Cfg1_Rcv16K = 0x1, |
| 222 | Cfg1_Rcv32K = 0x3, |
| 223 | Cfg1_Rcv64K = 0x7, |
| 224 | Cfg1_Rcv128K = 0xf, |
| 225 | }; |
| 226 | |
| 227 | enum MiiCmd0Bits { |
| 228 | Mii_Divider = 0x20000000, |
| 229 | Mii_WRITE = 0x400000, |
| 230 | Mii_READ = 0x200000, |
| 231 | Mii_SCAN = 0x100000, |
| 232 | Mii_Tamod = 0x80000, |
| 233 | Mii_Drvmod = 0x40000, |
| 234 | Mii_mdc = 0x20000, |
| 235 | Mii_mdoen = 0x10000, |
| 236 | Mii_mdo = 0x8000, |
| 237 | Mii_mdi = 0x4000, |
| 238 | }; |
| 239 | |
| 240 | enum MiiStatusBits { |
| 241 | Mii_StatusBusy = 0x80000000, |
| 242 | }; |
| 243 | |
| 244 | enum PMConfigBits { |
| 245 | PM_Enable = 1 << 31, |
| 246 | PM_LongWF = 1 << 30, |
| 247 | PM_Magic = 1 << 29, |
| 248 | PM_LANWake = 1 << 28, |
| 249 | PM_LWPTN = (1 << 27 | 1<< 26), |
| 250 | PM_LinkUp = 1 << 25, |
| 251 | PM_WakeUp = 1 << 24, |
| 252 | }; |
| 253 | |
| 254 | /* Locking rules: |
| 255 | * priv->lock protects most of the fields of priv and most of the |
| 256 | * hardware registers. It does not have to protect against softirqs |
| 257 | * between sc92031_disable_interrupts and sc92031_enable_interrupts; |
| 258 | * it also does not need to be used in ->open and ->stop while the |
| 259 | * device interrupts are off. |
| 260 | * Not having to protect against softirqs is very useful due to heavy |
| 261 | * use of mdelay() at _sc92031_reset. |
| 262 | * Functions prefixed with _sc92031_ must be called with the lock held; |
| 263 | * functions prefixed with sc92031_ must be called without the lock held. |
| 264 | * Use mmiowb() before unlocking if the hardware was written to. |
| 265 | */ |
| 266 | |
| 267 | /* Locking rules for the interrupt: |
| 268 | * - the interrupt and the tasklet never run at the same time |
| 269 | * - neither run between sc92031_disable_interrupts and |
| 270 | * sc92031_enable_interrupt |
| 271 | */ |
| 272 | |
| 273 | struct sc92031_priv { |
| 274 | spinlock_t lock; |
| 275 | /* iomap.h cookie */ |
| 276 | void __iomem *port_base; |
| 277 | /* pci device structure */ |
| 278 | struct pci_dev *pdev; |
| 279 | /* tasklet */ |
| 280 | struct tasklet_struct tasklet; |
| 281 | |
| 282 | /* CPU address of rx ring */ |
| 283 | void *rx_ring; |
| 284 | /* PCI address of rx ring */ |
| 285 | dma_addr_t rx_ring_dma_addr; |
| 286 | /* PCI address of rx ring read pointer */ |
| 287 | dma_addr_t rx_ring_tail; |
| 288 | |
| 289 | /* tx ring write index */ |
| 290 | unsigned tx_head; |
| 291 | /* tx ring read index */ |
| 292 | unsigned tx_tail; |
| 293 | /* CPU address of tx bounce buffer */ |
| 294 | void *tx_bufs; |
| 295 | /* PCI address of tx bounce buffer */ |
| 296 | dma_addr_t tx_bufs_dma_addr; |
| 297 | |
| 298 | /* copies of some hardware registers */ |
| 299 | u32 intr_status; |
| 300 | atomic_t intr_mask; |
| 301 | u32 rx_config; |
| 302 | u32 tx_config; |
| 303 | u32 pm_config; |
| 304 | |
| 305 | /* copy of some flags from dev->flags */ |
| 306 | unsigned int mc_flags; |
| 307 | |
| 308 | /* for ETHTOOL_GSTATS */ |
| 309 | u64 tx_timeouts; |
| 310 | u64 rx_loss; |
| 311 | |
| 312 | /* for dev->get_stats */ |
| 313 | long rx_value; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | /* I don't know which registers can be safely read; however, I can guess |
| 317 | * MAC0 is one of them. */ |
| 318 | static inline void _sc92031_dummy_read(void __iomem *port_base) |
| 319 | { |
| 320 | ioread32(port_base + MAC0); |
| 321 | } |
| 322 | |
| 323 | static u32 _sc92031_mii_wait(void __iomem *port_base) |
| 324 | { |
| 325 | u32 mii_status; |
| 326 | |
| 327 | do { |
| 328 | udelay(10); |
| 329 | mii_status = ioread32(port_base + Miistatus); |
| 330 | } while (mii_status & Mii_StatusBusy); |
| 331 | |
| 332 | return mii_status; |
| 333 | } |
| 334 | |
| 335 | static u32 _sc92031_mii_cmd(void __iomem *port_base, u32 cmd0, u32 cmd1) |
| 336 | { |
| 337 | iowrite32(Mii_Divider, port_base + Miicmd0); |
| 338 | |
| 339 | _sc92031_mii_wait(port_base); |
| 340 | |
| 341 | iowrite32(cmd1, port_base + Miicmd1); |
| 342 | iowrite32(Mii_Divider | cmd0, port_base + Miicmd0); |
| 343 | |
| 344 | return _sc92031_mii_wait(port_base); |
| 345 | } |
| 346 | |
| 347 | static void _sc92031_mii_scan(void __iomem *port_base) |
| 348 | { |
| 349 | _sc92031_mii_cmd(port_base, Mii_SCAN, 0x1 << 6); |
| 350 | } |
| 351 | |
| 352 | static u16 _sc92031_mii_read(void __iomem *port_base, unsigned reg) |
| 353 | { |
| 354 | return _sc92031_mii_cmd(port_base, Mii_READ, reg << 6) >> 13; |
| 355 | } |
| 356 | |
| 357 | static void _sc92031_mii_write(void __iomem *port_base, unsigned reg, u16 val) |
| 358 | { |
| 359 | _sc92031_mii_cmd(port_base, Mii_WRITE, (reg << 6) | ((u32)val << 11)); |
| 360 | } |
| 361 | |
| 362 | static void sc92031_disable_interrupts(struct net_device *dev) |
| 363 | { |
| 364 | struct sc92031_priv *priv = netdev_priv(dev); |
| 365 | void __iomem *port_base = priv->port_base; |
| 366 | |
| 367 | /* tell the tasklet/interrupt not to enable interrupts */ |
| 368 | atomic_set(&priv->intr_mask, 0); |
| 369 | wmb(); |
| 370 | |
| 371 | /* stop interrupts */ |
| 372 | iowrite32(0, port_base + IntrMask); |
| 373 | _sc92031_dummy_read(port_base); |
| 374 | mmiowb(); |
| 375 | |
| 376 | /* wait for any concurrent interrupt/tasklet to finish */ |
| 377 | synchronize_irq(dev->irq); |
| 378 | tasklet_disable(&priv->tasklet); |
| 379 | } |
| 380 | |
| 381 | static void sc92031_enable_interrupts(struct net_device *dev) |
| 382 | { |
| 383 | struct sc92031_priv *priv = netdev_priv(dev); |
| 384 | void __iomem *port_base = priv->port_base; |
| 385 | |
| 386 | tasklet_enable(&priv->tasklet); |
| 387 | |
| 388 | atomic_set(&priv->intr_mask, IntrBits); |
| 389 | wmb(); |
| 390 | |
| 391 | iowrite32(IntrBits, port_base + IntrMask); |
| 392 | mmiowb(); |
| 393 | } |
| 394 | |
| 395 | static void _sc92031_disable_tx_rx(struct net_device *dev) |
| 396 | { |
| 397 | struct sc92031_priv *priv = netdev_priv(dev); |
| 398 | void __iomem *port_base = priv->port_base; |
| 399 | |
| 400 | priv->rx_config &= ~RxEnb; |
| 401 | priv->tx_config &= ~TxEnb; |
| 402 | iowrite32(priv->rx_config, port_base + RxConfig); |
| 403 | iowrite32(priv->tx_config, port_base + TxConfig); |
| 404 | } |
| 405 | |
| 406 | static void _sc92031_enable_tx_rx(struct net_device *dev) |
| 407 | { |
| 408 | struct sc92031_priv *priv = netdev_priv(dev); |
| 409 | void __iomem *port_base = priv->port_base; |
| 410 | |
| 411 | priv->rx_config |= RxEnb; |
| 412 | priv->tx_config |= TxEnb; |
| 413 | iowrite32(priv->rx_config, port_base + RxConfig); |
| 414 | iowrite32(priv->tx_config, port_base + TxConfig); |
| 415 | } |
| 416 | |
| 417 | static void _sc92031_tx_clear(struct net_device *dev) |
| 418 | { |
| 419 | struct sc92031_priv *priv = netdev_priv(dev); |
| 420 | |
| 421 | while (priv->tx_head - priv->tx_tail > 0) { |
| 422 | priv->tx_tail++; |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 423 | dev->stats.tx_dropped++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 424 | } |
| 425 | priv->tx_head = priv->tx_tail = 0; |
| 426 | } |
| 427 | |
| 428 | static void _sc92031_set_mar(struct net_device *dev) |
| 429 | { |
| 430 | struct sc92031_priv *priv = netdev_priv(dev); |
| 431 | void __iomem *port_base = priv->port_base; |
| 432 | u32 mar0 = 0, mar1 = 0; |
| 433 | |
| 434 | if ((dev->flags & IFF_PROMISC) |
| 435 | || dev->mc_count > multicast_filter_limit |
| 436 | || (dev->flags & IFF_ALLMULTI)) |
| 437 | mar0 = mar1 = 0xffffffff; |
| 438 | else if (dev->flags & IFF_MULTICAST) { |
| 439 | struct dev_mc_list *mc_list; |
| 440 | |
| 441 | for (mc_list = dev->mc_list; mc_list; mc_list = mc_list->next) { |
| 442 | u32 crc; |
| 443 | unsigned bit = 0; |
| 444 | |
| 445 | crc = ~ether_crc(ETH_ALEN, mc_list->dmi_addr); |
| 446 | crc >>= 24; |
| 447 | |
| 448 | if (crc & 0x01) bit |= 0x02; |
| 449 | if (crc & 0x02) bit |= 0x01; |
| 450 | if (crc & 0x10) bit |= 0x20; |
| 451 | if (crc & 0x20) bit |= 0x10; |
| 452 | if (crc & 0x40) bit |= 0x08; |
| 453 | if (crc & 0x80) bit |= 0x04; |
| 454 | |
| 455 | if (bit > 31) |
| 456 | mar0 |= 0x1 << (bit - 32); |
| 457 | else |
| 458 | mar1 |= 0x1 << bit; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | iowrite32(mar0, port_base + MAR0); |
| 463 | iowrite32(mar1, port_base + MAR0 + 4); |
| 464 | } |
| 465 | |
| 466 | static void _sc92031_set_rx_config(struct net_device *dev) |
| 467 | { |
| 468 | struct sc92031_priv *priv = netdev_priv(dev); |
| 469 | void __iomem *port_base = priv->port_base; |
| 470 | unsigned int old_mc_flags; |
| 471 | u32 rx_config_bits = 0; |
| 472 | |
| 473 | old_mc_flags = priv->mc_flags; |
| 474 | |
| 475 | if (dev->flags & IFF_PROMISC) |
| 476 | rx_config_bits |= RxSmall | RxHuge | RxErr | RxBroadcast |
| 477 | | RxMulticast | RxAllphys; |
| 478 | |
| 479 | if (dev->flags & (IFF_ALLMULTI | IFF_MULTICAST)) |
| 480 | rx_config_bits |= RxMulticast; |
| 481 | |
| 482 | if (dev->flags & IFF_BROADCAST) |
| 483 | rx_config_bits |= RxBroadcast; |
| 484 | |
| 485 | priv->rx_config &= ~(RxSmall | RxHuge | RxErr | RxBroadcast |
| 486 | | RxMulticast | RxAllphys); |
| 487 | priv->rx_config |= rx_config_bits; |
| 488 | |
| 489 | priv->mc_flags = dev->flags & (IFF_PROMISC | IFF_ALLMULTI |
| 490 | | IFF_MULTICAST | IFF_BROADCAST); |
| 491 | |
| 492 | if (netif_carrier_ok(dev) && priv->mc_flags != old_mc_flags) |
| 493 | iowrite32(priv->rx_config, port_base + RxConfig); |
| 494 | } |
| 495 | |
| 496 | static bool _sc92031_check_media(struct net_device *dev) |
| 497 | { |
| 498 | struct sc92031_priv *priv = netdev_priv(dev); |
| 499 | void __iomem *port_base = priv->port_base; |
| 500 | u16 bmsr; |
| 501 | |
| 502 | bmsr = _sc92031_mii_read(port_base, MII_BMSR); |
| 503 | rmb(); |
| 504 | if (bmsr & BMSR_LSTATUS) { |
| 505 | bool speed_100, duplex_full; |
| 506 | u32 flow_ctrl_config = 0; |
| 507 | u16 output_status = _sc92031_mii_read(port_base, |
| 508 | MII_OutputStatus); |
| 509 | _sc92031_mii_scan(port_base); |
| 510 | |
| 511 | speed_100 = output_status & 0x2; |
| 512 | duplex_full = output_status & 0x4; |
| 513 | |
| 514 | /* Initial Tx/Rx configuration */ |
| 515 | priv->rx_config = (0x40 << LowThresholdShift) | (0x1c0 << HighThresholdShift); |
| 516 | priv->tx_config = 0x48800000; |
| 517 | |
| 518 | /* NOTE: vendor driver had dead code here to enable tx padding */ |
| 519 | |
| 520 | if (!speed_100) |
| 521 | priv->tx_config |= 0x80000; |
| 522 | |
| 523 | // configure rx mode |
| 524 | _sc92031_set_rx_config(dev); |
| 525 | |
| 526 | if (duplex_full) { |
| 527 | priv->rx_config |= RxFullDx; |
| 528 | priv->tx_config |= TxFullDx; |
| 529 | flow_ctrl_config = FlowCtrlFullDX | FlowCtrlEnb; |
| 530 | } else { |
| 531 | priv->rx_config &= ~RxFullDx; |
| 532 | priv->tx_config &= ~TxFullDx; |
| 533 | } |
| 534 | |
| 535 | _sc92031_set_mar(dev); |
| 536 | _sc92031_set_rx_config(dev); |
| 537 | _sc92031_enable_tx_rx(dev); |
| 538 | iowrite32(flow_ctrl_config, port_base + FlowCtrlConfig); |
| 539 | |
| 540 | netif_carrier_on(dev); |
| 541 | |
| 542 | if (printk_ratelimit()) |
| 543 | printk(KERN_INFO "%s: link up, %sMbps, %s-duplex\n", |
| 544 | dev->name, |
| 545 | speed_100 ? "100" : "10", |
| 546 | duplex_full ? "full" : "half"); |
| 547 | return true; |
| 548 | } else { |
| 549 | _sc92031_mii_scan(port_base); |
| 550 | |
| 551 | netif_carrier_off(dev); |
| 552 | |
| 553 | _sc92031_disable_tx_rx(dev); |
| 554 | |
| 555 | if (printk_ratelimit()) |
| 556 | printk(KERN_INFO "%s: link down\n", dev->name); |
| 557 | return false; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | static void _sc92031_phy_reset(struct net_device *dev) |
| 562 | { |
| 563 | struct sc92031_priv *priv = netdev_priv(dev); |
| 564 | void __iomem *port_base = priv->port_base; |
| 565 | u32 phy_ctrl; |
| 566 | |
| 567 | phy_ctrl = ioread32(port_base + PhyCtrl); |
| 568 | phy_ctrl &= ~(PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10); |
| 569 | phy_ctrl |= PhyCtrlAne | PhyCtrlReset; |
| 570 | |
| 571 | switch (media) { |
| 572 | default: |
| 573 | case AUTOSELECT: |
| 574 | phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10; |
| 575 | break; |
| 576 | case M10_HALF: |
| 577 | phy_ctrl |= PhyCtrlSpd10; |
| 578 | break; |
| 579 | case M10_FULL: |
| 580 | phy_ctrl |= PhyCtrlDux | PhyCtrlSpd10; |
| 581 | break; |
| 582 | case M100_HALF: |
| 583 | phy_ctrl |= PhyCtrlSpd100; |
| 584 | break; |
| 585 | case M100_FULL: |
| 586 | phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100; |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | iowrite32(phy_ctrl, port_base + PhyCtrl); |
| 591 | mdelay(10); |
| 592 | |
| 593 | phy_ctrl &= ~PhyCtrlReset; |
| 594 | iowrite32(phy_ctrl, port_base + PhyCtrl); |
| 595 | mdelay(1); |
| 596 | |
| 597 | _sc92031_mii_write(port_base, MII_JAB, |
| 598 | PHY_16_JAB_ENB | PHY_16_PORT_ENB); |
| 599 | _sc92031_mii_scan(port_base); |
| 600 | |
| 601 | netif_carrier_off(dev); |
| 602 | netif_stop_queue(dev); |
| 603 | } |
| 604 | |
| 605 | static void _sc92031_reset(struct net_device *dev) |
| 606 | { |
| 607 | struct sc92031_priv *priv = netdev_priv(dev); |
| 608 | void __iomem *port_base = priv->port_base; |
| 609 | |
| 610 | /* disable PM */ |
| 611 | iowrite32(0, port_base + PMConfig); |
| 612 | |
| 613 | /* soft reset the chip */ |
| 614 | iowrite32(Cfg0_Reset, port_base + Config0); |
| 615 | mdelay(200); |
| 616 | |
| 617 | iowrite32(0, port_base + Config0); |
| 618 | mdelay(10); |
| 619 | |
| 620 | /* disable interrupts */ |
| 621 | iowrite32(0, port_base + IntrMask); |
| 622 | |
| 623 | /* clear multicast address */ |
| 624 | iowrite32(0, port_base + MAR0); |
| 625 | iowrite32(0, port_base + MAR0 + 4); |
| 626 | |
| 627 | /* init rx ring */ |
| 628 | iowrite32(priv->rx_ring_dma_addr, port_base + RxbufAddr); |
| 629 | priv->rx_ring_tail = priv->rx_ring_dma_addr; |
| 630 | |
| 631 | /* init tx ring */ |
| 632 | _sc92031_tx_clear(dev); |
| 633 | |
| 634 | /* clear old register values */ |
| 635 | priv->intr_status = 0; |
| 636 | atomic_set(&priv->intr_mask, 0); |
| 637 | priv->rx_config = 0; |
| 638 | priv->tx_config = 0; |
| 639 | priv->mc_flags = 0; |
| 640 | |
| 641 | /* configure rx buffer size */ |
| 642 | /* NOTE: vendor driver had dead code here to enable early tx/rx */ |
| 643 | iowrite32(Cfg1_Rcv64K, port_base + Config1); |
| 644 | |
| 645 | _sc92031_phy_reset(dev); |
| 646 | _sc92031_check_media(dev); |
| 647 | |
| 648 | /* calculate rx fifo overflow */ |
| 649 | priv->rx_value = 0; |
| 650 | |
| 651 | /* enable PM */ |
| 652 | iowrite32(priv->pm_config, port_base + PMConfig); |
| 653 | |
| 654 | /* clear intr register */ |
| 655 | ioread32(port_base + IntrStatus); |
| 656 | } |
| 657 | |
| 658 | static void _sc92031_tx_tasklet(struct net_device *dev) |
| 659 | { |
| 660 | struct sc92031_priv *priv = netdev_priv(dev); |
| 661 | void __iomem *port_base = priv->port_base; |
| 662 | |
| 663 | unsigned old_tx_tail; |
| 664 | unsigned entry; |
| 665 | u32 tx_status; |
| 666 | |
| 667 | old_tx_tail = priv->tx_tail; |
| 668 | while (priv->tx_head - priv->tx_tail > 0) { |
| 669 | entry = priv->tx_tail % NUM_TX_DESC; |
| 670 | tx_status = ioread32(port_base + TxStatus0 + entry * 4); |
| 671 | |
| 672 | if (!(tx_status & (TxStatOK | TxUnderrun | TxAborted))) |
| 673 | break; |
| 674 | |
| 675 | priv->tx_tail++; |
| 676 | |
| 677 | if (tx_status & TxStatOK) { |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 678 | dev->stats.tx_bytes += tx_status & 0x1fff; |
| 679 | dev->stats.tx_packets++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 680 | /* Note: TxCarrierLost is always asserted at 100mbps. */ |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 681 | dev->stats.collisions += (tx_status >> 22) & 0xf; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | if (tx_status & (TxOutOfWindow | TxAborted)) { |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 685 | dev->stats.tx_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 686 | |
| 687 | if (tx_status & TxAborted) |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 688 | dev->stats.tx_aborted_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 689 | |
| 690 | if (tx_status & TxCarrierLost) |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 691 | dev->stats.tx_carrier_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 692 | |
| 693 | if (tx_status & TxOutOfWindow) |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 694 | dev->stats.tx_window_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | if (tx_status & TxUnderrun) |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 698 | dev->stats.tx_fifo_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | if (priv->tx_tail != old_tx_tail) |
| 702 | if (netif_queue_stopped(dev)) |
| 703 | netif_wake_queue(dev); |
| 704 | } |
| 705 | |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 706 | static void _sc92031_rx_tasklet_error(struct net_device *dev, |
| 707 | u32 rx_status, unsigned rx_size) |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 708 | { |
| 709 | if(rx_size > (MAX_ETH_FRAME_SIZE + 4) || rx_size < 16) { |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 710 | dev->stats.rx_errors++; |
| 711 | dev->stats.rx_length_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | if (!(rx_status & RxStatesOK)) { |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 715 | dev->stats.rx_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 716 | |
| 717 | if (rx_status & (RxHugeFrame | RxSmallFrame)) |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 718 | dev->stats.rx_length_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 719 | |
| 720 | if (rx_status & RxBadAlign) |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 721 | dev->stats.rx_frame_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 722 | |
| 723 | if (!(rx_status & RxCRCOK)) |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 724 | dev->stats.rx_crc_errors++; |
| 725 | } else { |
| 726 | struct sc92031_priv *priv = netdev_priv(dev); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 727 | priv->rx_loss++; |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 728 | } |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | static void _sc92031_rx_tasklet(struct net_device *dev) |
| 732 | { |
| 733 | struct sc92031_priv *priv = netdev_priv(dev); |
| 734 | void __iomem *port_base = priv->port_base; |
| 735 | |
| 736 | dma_addr_t rx_ring_head; |
| 737 | unsigned rx_len; |
| 738 | unsigned rx_ring_offset; |
| 739 | void *rx_ring = priv->rx_ring; |
| 740 | |
| 741 | rx_ring_head = ioread32(port_base + RxBufWPtr); |
| 742 | rmb(); |
| 743 | |
| 744 | /* rx_ring_head is only 17 bits in the RxBufWPtr register. |
| 745 | * we need to change it to 32 bits physical address |
| 746 | */ |
| 747 | rx_ring_head &= (dma_addr_t)(RX_BUF_LEN - 1); |
| 748 | rx_ring_head |= priv->rx_ring_dma_addr & ~(dma_addr_t)(RX_BUF_LEN - 1); |
| 749 | if (rx_ring_head < priv->rx_ring_dma_addr) |
| 750 | rx_ring_head += RX_BUF_LEN; |
| 751 | |
| 752 | if (rx_ring_head >= priv->rx_ring_tail) |
| 753 | rx_len = rx_ring_head - priv->rx_ring_tail; |
| 754 | else |
| 755 | rx_len = RX_BUF_LEN - (priv->rx_ring_tail - rx_ring_head); |
| 756 | |
| 757 | if (!rx_len) |
| 758 | return; |
| 759 | |
| 760 | if (unlikely(rx_len > RX_BUF_LEN)) { |
| 761 | if (printk_ratelimit()) |
| 762 | printk(KERN_ERR "%s: rx packets length > rx buffer\n", |
| 763 | dev->name); |
| 764 | return; |
| 765 | } |
| 766 | |
| 767 | rx_ring_offset = (priv->rx_ring_tail - priv->rx_ring_dma_addr) % RX_BUF_LEN; |
| 768 | |
| 769 | while (rx_len) { |
| 770 | u32 rx_status; |
| 771 | unsigned rx_size, rx_size_align, pkt_size; |
| 772 | struct sk_buff *skb; |
| 773 | |
| 774 | rx_status = le32_to_cpup((__le32 *)(rx_ring + rx_ring_offset)); |
| 775 | rmb(); |
| 776 | |
| 777 | rx_size = rx_status >> 20; |
| 778 | rx_size_align = (rx_size + 3) & ~3; // for 4 bytes aligned |
| 779 | pkt_size = rx_size - 4; // Omit the four octet CRC from the length. |
| 780 | |
| 781 | rx_ring_offset = (rx_ring_offset + 4) % RX_BUF_LEN; |
| 782 | |
| 783 | if (unlikely(rx_status == 0 |
| 784 | || rx_size > (MAX_ETH_FRAME_SIZE + 4) |
| 785 | || rx_size < 16 |
| 786 | || !(rx_status & RxStatesOK))) { |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 787 | _sc92031_rx_tasklet_error(dev, rx_status, rx_size); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 788 | break; |
| 789 | } |
| 790 | |
| 791 | if (unlikely(rx_size_align + 4 > rx_len)) { |
| 792 | if (printk_ratelimit()) |
| 793 | printk(KERN_ERR "%s: rx_len is too small\n", dev->name); |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | rx_len -= rx_size_align + 4; |
| 798 | |
Stephen Hemminger | 2723b01 | 2008-04-02 10:13:12 -0700 | [diff] [blame] | 799 | skb = netdev_alloc_skb(dev, pkt_size + NET_IP_ALIGN); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 800 | if (unlikely(!skb)) { |
| 801 | if (printk_ratelimit()) |
| 802 | printk(KERN_ERR "%s: Couldn't allocate a skb_buff for a packet of size %u\n", |
| 803 | dev->name, pkt_size); |
| 804 | goto next; |
| 805 | } |
| 806 | |
| 807 | skb_reserve(skb, NET_IP_ALIGN); |
| 808 | |
| 809 | if ((rx_ring_offset + pkt_size) > RX_BUF_LEN) { |
| 810 | memcpy(skb_put(skb, RX_BUF_LEN - rx_ring_offset), |
| 811 | rx_ring + rx_ring_offset, RX_BUF_LEN - rx_ring_offset); |
| 812 | memcpy(skb_put(skb, pkt_size - (RX_BUF_LEN - rx_ring_offset)), |
| 813 | rx_ring, pkt_size - (RX_BUF_LEN - rx_ring_offset)); |
| 814 | } else { |
| 815 | memcpy(skb_put(skb, pkt_size), rx_ring + rx_ring_offset, pkt_size); |
| 816 | } |
| 817 | |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 818 | skb->protocol = eth_type_trans(skb, dev); |
| 819 | dev->last_rx = jiffies; |
| 820 | netif_rx(skb); |
| 821 | |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 822 | dev->stats.rx_bytes += pkt_size; |
| 823 | dev->stats.rx_packets++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 824 | |
| 825 | if (rx_status & Rx_Multicast) |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 826 | dev->stats.multicast++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 827 | |
| 828 | next: |
| 829 | rx_ring_offset = (rx_ring_offset + rx_size_align) % RX_BUF_LEN; |
| 830 | } |
| 831 | mb(); |
| 832 | |
| 833 | priv->rx_ring_tail = rx_ring_head; |
| 834 | iowrite32(priv->rx_ring_tail, port_base + RxBufRPtr); |
| 835 | } |
| 836 | |
| 837 | static void _sc92031_link_tasklet(struct net_device *dev) |
| 838 | { |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 839 | if (_sc92031_check_media(dev)) |
| 840 | netif_wake_queue(dev); |
| 841 | else { |
| 842 | netif_stop_queue(dev); |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 843 | dev->stats.tx_carrier_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
| 847 | static void sc92031_tasklet(unsigned long data) |
| 848 | { |
| 849 | struct net_device *dev = (struct net_device *)data; |
| 850 | struct sc92031_priv *priv = netdev_priv(dev); |
| 851 | void __iomem *port_base = priv->port_base; |
| 852 | u32 intr_status, intr_mask; |
| 853 | |
| 854 | intr_status = priv->intr_status; |
| 855 | |
| 856 | spin_lock(&priv->lock); |
| 857 | |
| 858 | if (unlikely(!netif_running(dev))) |
| 859 | goto out; |
| 860 | |
| 861 | if (intr_status & TxOK) |
| 862 | _sc92031_tx_tasklet(dev); |
| 863 | |
| 864 | if (intr_status & RxOK) |
| 865 | _sc92031_rx_tasklet(dev); |
| 866 | |
| 867 | if (intr_status & RxOverflow) |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 868 | dev->stats.rx_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 869 | |
| 870 | if (intr_status & TimeOut) { |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 871 | dev->stats.rx_errors++; |
| 872 | dev->stats.rx_length_errors++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | if (intr_status & (LinkFail | LinkOK)) |
| 876 | _sc92031_link_tasklet(dev); |
| 877 | |
| 878 | out: |
| 879 | intr_mask = atomic_read(&priv->intr_mask); |
| 880 | rmb(); |
| 881 | |
| 882 | iowrite32(intr_mask, port_base + IntrMask); |
| 883 | mmiowb(); |
| 884 | |
| 885 | spin_unlock(&priv->lock); |
| 886 | } |
| 887 | |
| 888 | static irqreturn_t sc92031_interrupt(int irq, void *dev_id) |
| 889 | { |
| 890 | struct net_device *dev = dev_id; |
| 891 | struct sc92031_priv *priv = netdev_priv(dev); |
| 892 | void __iomem *port_base = priv->port_base; |
| 893 | u32 intr_status, intr_mask; |
| 894 | |
| 895 | /* mask interrupts before clearing IntrStatus */ |
| 896 | iowrite32(0, port_base + IntrMask); |
| 897 | _sc92031_dummy_read(port_base); |
| 898 | |
| 899 | intr_status = ioread32(port_base + IntrStatus); |
| 900 | if (unlikely(intr_status == 0xffffffff)) |
| 901 | return IRQ_NONE; // hardware has gone missing |
| 902 | |
| 903 | intr_status &= IntrBits; |
| 904 | if (!intr_status) |
| 905 | goto out_none; |
| 906 | |
| 907 | priv->intr_status = intr_status; |
| 908 | tasklet_schedule(&priv->tasklet); |
| 909 | |
| 910 | return IRQ_HANDLED; |
| 911 | |
| 912 | out_none: |
| 913 | intr_mask = atomic_read(&priv->intr_mask); |
| 914 | rmb(); |
| 915 | |
| 916 | iowrite32(intr_mask, port_base + IntrMask); |
| 917 | mmiowb(); |
| 918 | |
| 919 | return IRQ_NONE; |
| 920 | } |
| 921 | |
| 922 | static struct net_device_stats *sc92031_get_stats(struct net_device *dev) |
| 923 | { |
| 924 | struct sc92031_priv *priv = netdev_priv(dev); |
| 925 | void __iomem *port_base = priv->port_base; |
| 926 | |
| 927 | // FIXME I do not understand what is this trying to do. |
| 928 | if (netif_running(dev)) { |
| 929 | int temp; |
| 930 | |
| 931 | spin_lock_bh(&priv->lock); |
| 932 | |
| 933 | /* Update the error count. */ |
| 934 | temp = (ioread32(port_base + RxStatus0) >> 16) & 0xffff; |
| 935 | |
| 936 | if (temp == 0xffff) { |
| 937 | priv->rx_value += temp; |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 938 | dev->stats.rx_fifo_errors = priv->rx_value; |
| 939 | } else |
| 940 | dev->stats.rx_fifo_errors = temp + priv->rx_value; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 941 | |
| 942 | spin_unlock_bh(&priv->lock); |
| 943 | } |
| 944 | |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 945 | return &dev->stats; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | static int sc92031_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 949 | { |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 950 | struct sc92031_priv *priv = netdev_priv(dev); |
| 951 | void __iomem *port_base = priv->port_base; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 952 | unsigned len; |
| 953 | unsigned entry; |
| 954 | u32 tx_status; |
| 955 | |
| 956 | if (unlikely(skb->len > TX_BUF_SIZE)) { |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 957 | dev->stats.tx_dropped++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 958 | goto out; |
| 959 | } |
| 960 | |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 961 | spin_lock(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 962 | |
| 963 | if (unlikely(!netif_carrier_ok(dev))) { |
Stephen Hemminger | 9c28eae | 2008-04-02 10:11:20 -0700 | [diff] [blame] | 964 | dev->stats.tx_dropped++; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 965 | goto out_unlock; |
| 966 | } |
| 967 | |
| 968 | BUG_ON(priv->tx_head - priv->tx_tail >= NUM_TX_DESC); |
| 969 | |
| 970 | entry = priv->tx_head++ % NUM_TX_DESC; |
| 971 | |
| 972 | skb_copy_and_csum_dev(skb, priv->tx_bufs + entry * TX_BUF_SIZE); |
| 973 | |
| 974 | len = skb->len; |
Cesar Eduardo Barros | 6f94f70 | 2008-05-29 21:58:36 -0300 | [diff] [blame] | 975 | if (len < ETH_ZLEN) { |
Gerrit Renker | 5a0a92e | 2008-05-17 08:35:36 +0100 | [diff] [blame] | 976 | memset(priv->tx_bufs + entry * TX_BUF_SIZE + len, |
| 977 | 0, ETH_ZLEN - len); |
| 978 | len = ETH_ZLEN; |
| 979 | } |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 980 | |
| 981 | wmb(); |
| 982 | |
| 983 | if (len < 100) |
| 984 | tx_status = len; |
| 985 | else if (len < 300) |
| 986 | tx_status = 0x30000 | len; |
| 987 | else |
| 988 | tx_status = 0x50000 | len; |
| 989 | |
| 990 | iowrite32(priv->tx_bufs_dma_addr + entry * TX_BUF_SIZE, |
| 991 | port_base + TxAddr0 + entry * 4); |
| 992 | iowrite32(tx_status, port_base + TxStatus0 + entry * 4); |
| 993 | mmiowb(); |
| 994 | |
| 995 | dev->trans_start = jiffies; |
| 996 | |
| 997 | if (priv->tx_head - priv->tx_tail >= NUM_TX_DESC) |
| 998 | netif_stop_queue(dev); |
| 999 | |
| 1000 | out_unlock: |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 1001 | spin_unlock(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1002 | |
| 1003 | out: |
| 1004 | dev_kfree_skb(skb); |
| 1005 | |
Stephen Hemminger | 26a17b7 | 2008-04-02 10:11:11 -0700 | [diff] [blame] | 1006 | return NETDEV_TX_OK; |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static int sc92031_open(struct net_device *dev) |
| 1010 | { |
| 1011 | int err; |
| 1012 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1013 | struct pci_dev *pdev = priv->pdev; |
| 1014 | |
| 1015 | priv->rx_ring = pci_alloc_consistent(pdev, RX_BUF_LEN, |
| 1016 | &priv->rx_ring_dma_addr); |
| 1017 | if (unlikely(!priv->rx_ring)) { |
| 1018 | err = -ENOMEM; |
| 1019 | goto out_alloc_rx_ring; |
| 1020 | } |
| 1021 | |
| 1022 | priv->tx_bufs = pci_alloc_consistent(pdev, TX_BUF_TOT_LEN, |
| 1023 | &priv->tx_bufs_dma_addr); |
| 1024 | if (unlikely(!priv->tx_bufs)) { |
| 1025 | err = -ENOMEM; |
| 1026 | goto out_alloc_tx_bufs; |
| 1027 | } |
| 1028 | priv->tx_head = priv->tx_tail = 0; |
| 1029 | |
| 1030 | err = request_irq(pdev->irq, sc92031_interrupt, |
Thomas Gleixner | 2db6346 | 2007-02-14 00:33:20 -0800 | [diff] [blame] | 1031 | IRQF_SHARED, dev->name, dev); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1032 | if (unlikely(err < 0)) |
| 1033 | goto out_request_irq; |
| 1034 | |
| 1035 | priv->pm_config = 0; |
| 1036 | |
| 1037 | /* Interrupts already disabled by sc92031_stop or sc92031_probe */ |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 1038 | spin_lock_bh(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1039 | |
| 1040 | _sc92031_reset(dev); |
| 1041 | mmiowb(); |
| 1042 | |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 1043 | spin_unlock_bh(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1044 | sc92031_enable_interrupts(dev); |
| 1045 | |
| 1046 | if (netif_carrier_ok(dev)) |
| 1047 | netif_start_queue(dev); |
| 1048 | else |
| 1049 | netif_tx_disable(dev); |
| 1050 | |
| 1051 | return 0; |
| 1052 | |
| 1053 | out_request_irq: |
| 1054 | pci_free_consistent(pdev, TX_BUF_TOT_LEN, priv->tx_bufs, |
| 1055 | priv->tx_bufs_dma_addr); |
| 1056 | out_alloc_tx_bufs: |
| 1057 | pci_free_consistent(pdev, RX_BUF_LEN, priv->rx_ring, |
| 1058 | priv->rx_ring_dma_addr); |
| 1059 | out_alloc_rx_ring: |
| 1060 | return err; |
| 1061 | } |
| 1062 | |
| 1063 | static int sc92031_stop(struct net_device *dev) |
| 1064 | { |
| 1065 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1066 | struct pci_dev *pdev = priv->pdev; |
| 1067 | |
| 1068 | netif_tx_disable(dev); |
| 1069 | |
| 1070 | /* Disable interrupts, stop Tx and Rx. */ |
| 1071 | sc92031_disable_interrupts(dev); |
| 1072 | |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 1073 | spin_lock_bh(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1074 | |
| 1075 | _sc92031_disable_tx_rx(dev); |
| 1076 | _sc92031_tx_clear(dev); |
| 1077 | mmiowb(); |
| 1078 | |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 1079 | spin_unlock_bh(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1080 | |
| 1081 | free_irq(pdev->irq, dev); |
| 1082 | pci_free_consistent(pdev, TX_BUF_TOT_LEN, priv->tx_bufs, |
| 1083 | priv->tx_bufs_dma_addr); |
| 1084 | pci_free_consistent(pdev, RX_BUF_LEN, priv->rx_ring, |
| 1085 | priv->rx_ring_dma_addr); |
| 1086 | |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | static void sc92031_set_multicast_list(struct net_device *dev) |
| 1091 | { |
| 1092 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1093 | |
| 1094 | spin_lock_bh(&priv->lock); |
| 1095 | |
| 1096 | _sc92031_set_mar(dev); |
| 1097 | _sc92031_set_rx_config(dev); |
| 1098 | mmiowb(); |
| 1099 | |
| 1100 | spin_unlock_bh(&priv->lock); |
| 1101 | } |
| 1102 | |
| 1103 | static void sc92031_tx_timeout(struct net_device *dev) |
| 1104 | { |
| 1105 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1106 | |
| 1107 | /* Disable interrupts by clearing the interrupt mask.*/ |
| 1108 | sc92031_disable_interrupts(dev); |
| 1109 | |
| 1110 | spin_lock(&priv->lock); |
| 1111 | |
| 1112 | priv->tx_timeouts++; |
| 1113 | |
| 1114 | _sc92031_reset(dev); |
| 1115 | mmiowb(); |
| 1116 | |
| 1117 | spin_unlock(&priv->lock); |
| 1118 | |
| 1119 | /* enable interrupts */ |
| 1120 | sc92031_enable_interrupts(dev); |
| 1121 | |
| 1122 | if (netif_carrier_ok(dev)) |
| 1123 | netif_wake_queue(dev); |
| 1124 | } |
| 1125 | |
| 1126 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1127 | static void sc92031_poll_controller(struct net_device *dev) |
| 1128 | { |
| 1129 | disable_irq(dev->irq); |
| 1130 | if (sc92031_interrupt(dev->irq, dev) != IRQ_NONE) |
| 1131 | sc92031_tasklet((unsigned long)dev); |
| 1132 | enable_irq(dev->irq); |
| 1133 | } |
| 1134 | #endif |
| 1135 | |
| 1136 | static int sc92031_ethtool_get_settings(struct net_device *dev, |
| 1137 | struct ethtool_cmd *cmd) |
| 1138 | { |
| 1139 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1140 | void __iomem *port_base = priv->port_base; |
| 1141 | u8 phy_address; |
| 1142 | u32 phy_ctrl; |
| 1143 | u16 output_status; |
| 1144 | |
| 1145 | spin_lock_bh(&priv->lock); |
| 1146 | |
| 1147 | phy_address = ioread32(port_base + Miicmd1) >> 27; |
| 1148 | phy_ctrl = ioread32(port_base + PhyCtrl); |
| 1149 | |
| 1150 | output_status = _sc92031_mii_read(port_base, MII_OutputStatus); |
| 1151 | _sc92031_mii_scan(port_base); |
| 1152 | mmiowb(); |
| 1153 | |
| 1154 | spin_unlock_bh(&priv->lock); |
| 1155 | |
| 1156 | cmd->supported = SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
| 1157 | | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
| 1158 | | SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII; |
| 1159 | |
| 1160 | cmd->advertising = ADVERTISED_TP | ADVERTISED_MII; |
| 1161 | |
| 1162 | if ((phy_ctrl & (PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10)) |
| 1163 | == (PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10)) |
| 1164 | cmd->advertising |= ADVERTISED_Autoneg; |
| 1165 | |
| 1166 | if ((phy_ctrl & PhyCtrlSpd10) == PhyCtrlSpd10) |
| 1167 | cmd->advertising |= ADVERTISED_10baseT_Half; |
| 1168 | |
| 1169 | if ((phy_ctrl & (PhyCtrlSpd10 | PhyCtrlDux)) |
| 1170 | == (PhyCtrlSpd10 | PhyCtrlDux)) |
| 1171 | cmd->advertising |= ADVERTISED_10baseT_Full; |
| 1172 | |
| 1173 | if ((phy_ctrl & PhyCtrlSpd100) == PhyCtrlSpd100) |
| 1174 | cmd->advertising |= ADVERTISED_100baseT_Half; |
| 1175 | |
| 1176 | if ((phy_ctrl & (PhyCtrlSpd100 | PhyCtrlDux)) |
| 1177 | == (PhyCtrlSpd100 | PhyCtrlDux)) |
| 1178 | cmd->advertising |= ADVERTISED_100baseT_Full; |
| 1179 | |
| 1180 | if (phy_ctrl & PhyCtrlAne) |
| 1181 | cmd->advertising |= ADVERTISED_Autoneg; |
| 1182 | |
| 1183 | cmd->speed = (output_status & 0x2) ? SPEED_100 : SPEED_10; |
| 1184 | cmd->duplex = (output_status & 0x4) ? DUPLEX_FULL : DUPLEX_HALF; |
| 1185 | cmd->port = PORT_MII; |
| 1186 | cmd->phy_address = phy_address; |
| 1187 | cmd->transceiver = XCVR_INTERNAL; |
| 1188 | cmd->autoneg = (phy_ctrl & PhyCtrlAne) ? AUTONEG_ENABLE : AUTONEG_DISABLE; |
| 1189 | |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | static int sc92031_ethtool_set_settings(struct net_device *dev, |
| 1194 | struct ethtool_cmd *cmd) |
| 1195 | { |
| 1196 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1197 | void __iomem *port_base = priv->port_base; |
| 1198 | u32 phy_ctrl; |
| 1199 | u32 old_phy_ctrl; |
| 1200 | |
| 1201 | if (!(cmd->speed == SPEED_10 || cmd->speed == SPEED_100)) |
| 1202 | return -EINVAL; |
| 1203 | if (!(cmd->duplex == DUPLEX_HALF || cmd->duplex == DUPLEX_FULL)) |
| 1204 | return -EINVAL; |
| 1205 | if (!(cmd->port == PORT_MII)) |
| 1206 | return -EINVAL; |
| 1207 | if (!(cmd->phy_address == 0x1f)) |
| 1208 | return -EINVAL; |
| 1209 | if (!(cmd->transceiver == XCVR_INTERNAL)) |
| 1210 | return -EINVAL; |
| 1211 | if (!(cmd->autoneg == AUTONEG_DISABLE || cmd->autoneg == AUTONEG_ENABLE)) |
| 1212 | return -EINVAL; |
| 1213 | |
| 1214 | if (cmd->autoneg == AUTONEG_ENABLE) { |
| 1215 | if (!(cmd->advertising & (ADVERTISED_Autoneg |
| 1216 | | ADVERTISED_100baseT_Full |
| 1217 | | ADVERTISED_100baseT_Half |
| 1218 | | ADVERTISED_10baseT_Full |
| 1219 | | ADVERTISED_10baseT_Half))) |
| 1220 | return -EINVAL; |
| 1221 | |
| 1222 | phy_ctrl = PhyCtrlAne; |
| 1223 | |
| 1224 | // FIXME: I'm not sure what the original code was trying to do |
| 1225 | if (cmd->advertising & ADVERTISED_Autoneg) |
| 1226 | phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10; |
| 1227 | if (cmd->advertising & ADVERTISED_100baseT_Full) |
| 1228 | phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100; |
| 1229 | if (cmd->advertising & ADVERTISED_100baseT_Half) |
| 1230 | phy_ctrl |= PhyCtrlSpd100; |
| 1231 | if (cmd->advertising & ADVERTISED_10baseT_Full) |
| 1232 | phy_ctrl |= PhyCtrlSpd10 | PhyCtrlDux; |
| 1233 | if (cmd->advertising & ADVERTISED_10baseT_Half) |
| 1234 | phy_ctrl |= PhyCtrlSpd10; |
| 1235 | } else { |
| 1236 | // FIXME: Whole branch guessed |
| 1237 | phy_ctrl = 0; |
| 1238 | |
| 1239 | if (cmd->speed == SPEED_10) |
| 1240 | phy_ctrl |= PhyCtrlSpd10; |
| 1241 | else /* cmd->speed == SPEED_100 */ |
| 1242 | phy_ctrl |= PhyCtrlSpd100; |
| 1243 | |
| 1244 | if (cmd->duplex == DUPLEX_FULL) |
| 1245 | phy_ctrl |= PhyCtrlDux; |
| 1246 | } |
| 1247 | |
| 1248 | spin_lock_bh(&priv->lock); |
| 1249 | |
| 1250 | old_phy_ctrl = ioread32(port_base + PhyCtrl); |
| 1251 | phy_ctrl |= old_phy_ctrl & ~(PhyCtrlAne | PhyCtrlDux |
| 1252 | | PhyCtrlSpd100 | PhyCtrlSpd10); |
| 1253 | if (phy_ctrl != old_phy_ctrl) |
| 1254 | iowrite32(phy_ctrl, port_base + PhyCtrl); |
| 1255 | |
| 1256 | spin_unlock_bh(&priv->lock); |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static void sc92031_ethtool_get_drvinfo(struct net_device *dev, |
| 1262 | struct ethtool_drvinfo *drvinfo) |
| 1263 | { |
| 1264 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1265 | struct pci_dev *pdev = priv->pdev; |
| 1266 | |
| 1267 | strcpy(drvinfo->driver, SC92031_NAME); |
| 1268 | strcpy(drvinfo->version, SC92031_VERSION); |
| 1269 | strcpy(drvinfo->bus_info, pci_name(pdev)); |
| 1270 | } |
| 1271 | |
| 1272 | static void sc92031_ethtool_get_wol(struct net_device *dev, |
| 1273 | struct ethtool_wolinfo *wolinfo) |
| 1274 | { |
| 1275 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1276 | void __iomem *port_base = priv->port_base; |
| 1277 | u32 pm_config; |
| 1278 | |
| 1279 | spin_lock_bh(&priv->lock); |
| 1280 | pm_config = ioread32(port_base + PMConfig); |
| 1281 | spin_unlock_bh(&priv->lock); |
| 1282 | |
| 1283 | // FIXME: Guessed |
| 1284 | wolinfo->supported = WAKE_PHY | WAKE_MAGIC |
| 1285 | | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST; |
| 1286 | wolinfo->wolopts = 0; |
| 1287 | |
| 1288 | if (pm_config & PM_LinkUp) |
| 1289 | wolinfo->wolopts |= WAKE_PHY; |
| 1290 | |
| 1291 | if (pm_config & PM_Magic) |
| 1292 | wolinfo->wolopts |= WAKE_MAGIC; |
| 1293 | |
| 1294 | if (pm_config & PM_WakeUp) |
| 1295 | // FIXME: Guessed |
| 1296 | wolinfo->wolopts |= WAKE_UCAST | WAKE_MCAST | WAKE_BCAST; |
| 1297 | } |
| 1298 | |
| 1299 | static int sc92031_ethtool_set_wol(struct net_device *dev, |
| 1300 | struct ethtool_wolinfo *wolinfo) |
| 1301 | { |
| 1302 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1303 | void __iomem *port_base = priv->port_base; |
| 1304 | u32 pm_config; |
| 1305 | |
| 1306 | spin_lock_bh(&priv->lock); |
| 1307 | |
| 1308 | pm_config = ioread32(port_base + PMConfig) |
| 1309 | & ~(PM_LinkUp | PM_Magic | PM_WakeUp); |
| 1310 | |
| 1311 | if (wolinfo->wolopts & WAKE_PHY) |
| 1312 | pm_config |= PM_LinkUp; |
| 1313 | |
| 1314 | if (wolinfo->wolopts & WAKE_MAGIC) |
| 1315 | pm_config |= PM_Magic; |
| 1316 | |
| 1317 | // FIXME: Guessed |
| 1318 | if (wolinfo->wolopts & (WAKE_UCAST | WAKE_MCAST | WAKE_BCAST)) |
| 1319 | pm_config |= PM_WakeUp; |
| 1320 | |
| 1321 | priv->pm_config = pm_config; |
| 1322 | iowrite32(pm_config, port_base + PMConfig); |
| 1323 | mmiowb(); |
| 1324 | |
| 1325 | spin_unlock_bh(&priv->lock); |
| 1326 | |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | static int sc92031_ethtool_nway_reset(struct net_device *dev) |
| 1331 | { |
| 1332 | int err = 0; |
| 1333 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1334 | void __iomem *port_base = priv->port_base; |
| 1335 | u16 bmcr; |
| 1336 | |
| 1337 | spin_lock_bh(&priv->lock); |
| 1338 | |
| 1339 | bmcr = _sc92031_mii_read(port_base, MII_BMCR); |
| 1340 | if (!(bmcr & BMCR_ANENABLE)) { |
| 1341 | err = -EINVAL; |
| 1342 | goto out; |
| 1343 | } |
| 1344 | |
| 1345 | _sc92031_mii_write(port_base, MII_BMCR, bmcr | BMCR_ANRESTART); |
| 1346 | |
| 1347 | out: |
| 1348 | _sc92031_mii_scan(port_base); |
| 1349 | mmiowb(); |
| 1350 | |
| 1351 | spin_unlock_bh(&priv->lock); |
| 1352 | |
| 1353 | return err; |
| 1354 | } |
| 1355 | |
| 1356 | static const char sc92031_ethtool_stats_strings[SILAN_STATS_NUM][ETH_GSTRING_LEN] = { |
| 1357 | "tx_timeout", |
| 1358 | "rx_loss", |
| 1359 | }; |
| 1360 | |
| 1361 | static void sc92031_ethtool_get_strings(struct net_device *dev, |
| 1362 | u32 stringset, u8 *data) |
| 1363 | { |
| 1364 | if (stringset == ETH_SS_STATS) |
| 1365 | memcpy(data, sc92031_ethtool_stats_strings, |
| 1366 | SILAN_STATS_NUM * ETH_GSTRING_LEN); |
| 1367 | } |
| 1368 | |
Jeff Garzik | b9f2c04 | 2007-10-03 18:07:32 -0700 | [diff] [blame] | 1369 | static int sc92031_ethtool_get_sset_count(struct net_device *dev, int sset) |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1370 | { |
Jeff Garzik | b9f2c04 | 2007-10-03 18:07:32 -0700 | [diff] [blame] | 1371 | switch (sset) { |
| 1372 | case ETH_SS_STATS: |
| 1373 | return SILAN_STATS_NUM; |
| 1374 | default: |
| 1375 | return -EOPNOTSUPP; |
| 1376 | } |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | static void sc92031_ethtool_get_ethtool_stats(struct net_device *dev, |
| 1380 | struct ethtool_stats *stats, u64 *data) |
| 1381 | { |
| 1382 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1383 | |
| 1384 | spin_lock_bh(&priv->lock); |
| 1385 | data[0] = priv->tx_timeouts; |
| 1386 | data[1] = priv->rx_loss; |
| 1387 | spin_unlock_bh(&priv->lock); |
| 1388 | } |
| 1389 | |
| 1390 | static struct ethtool_ops sc92031_ethtool_ops = { |
| 1391 | .get_settings = sc92031_ethtool_get_settings, |
| 1392 | .set_settings = sc92031_ethtool_set_settings, |
| 1393 | .get_drvinfo = sc92031_ethtool_get_drvinfo, |
| 1394 | .get_wol = sc92031_ethtool_get_wol, |
| 1395 | .set_wol = sc92031_ethtool_set_wol, |
| 1396 | .nway_reset = sc92031_ethtool_nway_reset, |
| 1397 | .get_link = ethtool_op_get_link, |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1398 | .get_strings = sc92031_ethtool_get_strings, |
Jeff Garzik | b9f2c04 | 2007-10-03 18:07:32 -0700 | [diff] [blame] | 1399 | .get_sset_count = sc92031_ethtool_get_sset_count, |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1400 | .get_ethtool_stats = sc92031_ethtool_get_ethtool_stats, |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1401 | }; |
| 1402 | |
| 1403 | static int __devinit sc92031_probe(struct pci_dev *pdev, |
| 1404 | const struct pci_device_id *id) |
| 1405 | { |
| 1406 | int err; |
| 1407 | void __iomem* port_base; |
| 1408 | struct net_device *dev; |
| 1409 | struct sc92031_priv *priv; |
| 1410 | u32 mac0, mac1; |
| 1411 | |
| 1412 | err = pci_enable_device(pdev); |
| 1413 | if (unlikely(err < 0)) |
| 1414 | goto out_enable_device; |
| 1415 | |
| 1416 | pci_set_master(pdev); |
| 1417 | |
| 1418 | err = pci_set_dma_mask(pdev, DMA_32BIT_MASK); |
| 1419 | if (unlikely(err < 0)) |
| 1420 | goto out_set_dma_mask; |
| 1421 | |
| 1422 | err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK); |
| 1423 | if (unlikely(err < 0)) |
| 1424 | goto out_set_dma_mask; |
| 1425 | |
| 1426 | err = pci_request_regions(pdev, SC92031_NAME); |
| 1427 | if (unlikely(err < 0)) |
| 1428 | goto out_request_regions; |
| 1429 | |
| 1430 | port_base = pci_iomap(pdev, SC92031_USE_BAR, 0); |
| 1431 | if (unlikely(!port_base)) { |
| 1432 | err = -EIO; |
| 1433 | goto out_iomap; |
| 1434 | } |
| 1435 | |
| 1436 | dev = alloc_etherdev(sizeof(struct sc92031_priv)); |
| 1437 | if (unlikely(!dev)) { |
| 1438 | err = -ENOMEM; |
| 1439 | goto out_alloc_etherdev; |
| 1440 | } |
| 1441 | |
| 1442 | pci_set_drvdata(pdev, dev); |
Stephen Hemminger | 5a81f14 | 2008-04-10 21:30:38 -0700 | [diff] [blame] | 1443 | SET_NETDEV_DEV(dev, &pdev->dev); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1444 | |
| 1445 | #if SC92031_USE_BAR == 0 |
| 1446 | dev->mem_start = pci_resource_start(pdev, SC92031_USE_BAR); |
| 1447 | dev->mem_end = pci_resource_end(pdev, SC92031_USE_BAR); |
| 1448 | #elif SC92031_USE_BAR == 1 |
| 1449 | dev->base_addr = pci_resource_start(pdev, SC92031_USE_BAR); |
| 1450 | #endif |
| 1451 | dev->irq = pdev->irq; |
| 1452 | |
| 1453 | /* faked with skb_copy_and_csum_dev */ |
| 1454 | dev->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA; |
| 1455 | |
| 1456 | dev->get_stats = sc92031_get_stats; |
| 1457 | dev->ethtool_ops = &sc92031_ethtool_ops; |
| 1458 | dev->hard_start_xmit = sc92031_start_xmit; |
| 1459 | dev->watchdog_timeo = TX_TIMEOUT; |
| 1460 | dev->open = sc92031_open; |
| 1461 | dev->stop = sc92031_stop; |
| 1462 | dev->set_multicast_list = sc92031_set_multicast_list; |
| 1463 | dev->tx_timeout = sc92031_tx_timeout; |
| 1464 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1465 | dev->poll_controller = sc92031_poll_controller; |
| 1466 | #endif |
| 1467 | |
| 1468 | priv = netdev_priv(dev); |
| 1469 | spin_lock_init(&priv->lock); |
| 1470 | priv->port_base = port_base; |
| 1471 | priv->pdev = pdev; |
| 1472 | tasklet_init(&priv->tasklet, sc92031_tasklet, (unsigned long)dev); |
| 1473 | /* Fudge tasklet count so the call to sc92031_enable_interrupts at |
| 1474 | * sc92031_open will work correctly */ |
| 1475 | tasklet_disable_nosync(&priv->tasklet); |
| 1476 | |
| 1477 | /* PCI PM Wakeup */ |
| 1478 | iowrite32((~PM_LongWF & ~PM_LWPTN) | PM_Enable, port_base + PMConfig); |
| 1479 | |
| 1480 | mac0 = ioread32(port_base + MAC0); |
| 1481 | mac1 = ioread32(port_base + MAC0 + 4); |
| 1482 | dev->dev_addr[0] = dev->perm_addr[0] = mac0 >> 24; |
| 1483 | dev->dev_addr[1] = dev->perm_addr[1] = mac0 >> 16; |
| 1484 | dev->dev_addr[2] = dev->perm_addr[2] = mac0 >> 8; |
| 1485 | dev->dev_addr[3] = dev->perm_addr[3] = mac0; |
| 1486 | dev->dev_addr[4] = dev->perm_addr[4] = mac1 >> 8; |
| 1487 | dev->dev_addr[5] = dev->perm_addr[5] = mac1; |
| 1488 | |
| 1489 | err = register_netdev(dev); |
| 1490 | if (err < 0) |
| 1491 | goto out_register_netdev; |
| 1492 | |
| 1493 | return 0; |
| 1494 | |
| 1495 | out_register_netdev: |
| 1496 | free_netdev(dev); |
| 1497 | out_alloc_etherdev: |
| 1498 | pci_iounmap(pdev, port_base); |
| 1499 | out_iomap: |
| 1500 | pci_release_regions(pdev); |
| 1501 | out_request_regions: |
| 1502 | out_set_dma_mask: |
| 1503 | pci_disable_device(pdev); |
| 1504 | out_enable_device: |
| 1505 | return err; |
| 1506 | } |
| 1507 | |
| 1508 | static void __devexit sc92031_remove(struct pci_dev *pdev) |
| 1509 | { |
| 1510 | struct net_device *dev = pci_get_drvdata(pdev); |
| 1511 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1512 | void __iomem* port_base = priv->port_base; |
| 1513 | |
| 1514 | unregister_netdev(dev); |
| 1515 | free_netdev(dev); |
| 1516 | pci_iounmap(pdev, port_base); |
| 1517 | pci_release_regions(pdev); |
| 1518 | pci_disable_device(pdev); |
| 1519 | } |
| 1520 | |
| 1521 | static int sc92031_suspend(struct pci_dev *pdev, pm_message_t state) |
| 1522 | { |
| 1523 | struct net_device *dev = pci_get_drvdata(pdev); |
| 1524 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1525 | |
| 1526 | pci_save_state(pdev); |
| 1527 | |
| 1528 | if (!netif_running(dev)) |
| 1529 | goto out; |
| 1530 | |
| 1531 | netif_device_detach(dev); |
| 1532 | |
| 1533 | /* Disable interrupts, stop Tx and Rx. */ |
| 1534 | sc92031_disable_interrupts(dev); |
| 1535 | |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 1536 | spin_lock_bh(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1537 | |
| 1538 | _sc92031_disable_tx_rx(dev); |
| 1539 | _sc92031_tx_clear(dev); |
| 1540 | mmiowb(); |
| 1541 | |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 1542 | spin_unlock_bh(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1543 | |
| 1544 | out: |
| 1545 | pci_set_power_state(pdev, pci_choose_state(pdev, state)); |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | static int sc92031_resume(struct pci_dev *pdev) |
| 1551 | { |
| 1552 | struct net_device *dev = pci_get_drvdata(pdev); |
| 1553 | struct sc92031_priv *priv = netdev_priv(dev); |
| 1554 | |
| 1555 | pci_restore_state(pdev); |
| 1556 | pci_set_power_state(pdev, PCI_D0); |
| 1557 | |
| 1558 | if (!netif_running(dev)) |
| 1559 | goto out; |
| 1560 | |
| 1561 | /* Interrupts already disabled by sc92031_suspend */ |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 1562 | spin_lock_bh(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1563 | |
| 1564 | _sc92031_reset(dev); |
| 1565 | mmiowb(); |
| 1566 | |
Herbert Xu | 699784b | 2007-04-09 11:51:15 -0700 | [diff] [blame] | 1567 | spin_unlock_bh(&priv->lock); |
Cesar Eduardo Barros | bf34570 | 2006-12-19 13:08:47 -0800 | [diff] [blame] | 1568 | sc92031_enable_interrupts(dev); |
| 1569 | |
| 1570 | netif_device_attach(dev); |
| 1571 | |
| 1572 | if (netif_carrier_ok(dev)) |
| 1573 | netif_wake_queue(dev); |
| 1574 | else |
| 1575 | netif_tx_disable(dev); |
| 1576 | |
| 1577 | out: |
| 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | static struct pci_device_id sc92031_pci_device_id_table[] __devinitdata = { |
| 1582 | { PCI_DEVICE(PCI_VENDOR_ID_SILAN, PCI_DEVICE_ID_SILAN_SC92031) }, |
| 1583 | { PCI_DEVICE(PCI_VENDOR_ID_SILAN, PCI_DEVICE_ID_SILAN_8139D) }, |
| 1584 | { 0, } |
| 1585 | }; |
| 1586 | MODULE_DEVICE_TABLE(pci, sc92031_pci_device_id_table); |
| 1587 | |
| 1588 | static struct pci_driver sc92031_pci_driver = { |
| 1589 | .name = SC92031_NAME, |
| 1590 | .id_table = sc92031_pci_device_id_table, |
| 1591 | .probe = sc92031_probe, |
| 1592 | .remove = __devexit_p(sc92031_remove), |
| 1593 | .suspend = sc92031_suspend, |
| 1594 | .resume = sc92031_resume, |
| 1595 | }; |
| 1596 | |
| 1597 | static int __init sc92031_init(void) |
| 1598 | { |
| 1599 | printk(KERN_INFO SC92031_DESCRIPTION " " SC92031_VERSION "\n"); |
| 1600 | return pci_register_driver(&sc92031_pci_driver); |
| 1601 | } |
| 1602 | |
| 1603 | static void __exit sc92031_exit(void) |
| 1604 | { |
| 1605 | pci_unregister_driver(&sc92031_pci_driver); |
| 1606 | } |
| 1607 | |
| 1608 | module_init(sc92031_init); |
| 1609 | module_exit(sc92031_exit); |
| 1610 | |
| 1611 | MODULE_LICENSE("GPL"); |
| 1612 | MODULE_AUTHOR("Cesar Eduardo Barros <cesarb@cesarb.net>"); |
| 1613 | MODULE_DESCRIPTION(SC92031_DESCRIPTION); |
| 1614 | MODULE_VERSION(SC92031_VERSION); |