1 #define VERSION "0.23" 2 /* ns83820.c by Benjamin LaHaise with contributions. 3 * 4 * Questions/comments/discussion to linux-ns83820@kvack.org. 5 * 6 * $Revision: 1.34.2.23 $ 7 * 8 * Copyright 2001 Benjamin LaHaise. 9 * Copyright 2001, 2002 Red Hat. 10 * 11 * Mmmm, chocolate vanilla mocha... 12 * 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation; either version 2 of the License, or 17 * (at your option) any later version. 18 * 19 * This program is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU General Public License for more details. 23 * 24 * You should have received a copy of the GNU General Public License 25 * along with this program; if not, see <http://www.gnu.org/licenses/>. 26 * 27 * 28 * ChangeLog 29 * ========= 30 * 20010414 0.1 - created 31 * 20010622 0.2 - basic rx and tx. 32 * 20010711 0.3 - added duplex and link state detection support. 33 * 20010713 0.4 - zero copy, no hangs. 34 * 0.5 - 64 bit dma support (davem will hate me for this) 35 * - disable jumbo frames to avoid tx hangs 36 * - work around tx deadlocks on my 1.02 card via 37 * fiddling with TXCFG 38 * 20010810 0.6 - use pci dma api for ringbuffers, work on ia64 39 * 20010816 0.7 - misc cleanups 40 * 20010826 0.8 - fix critical zero copy bugs 41 * 0.9 - internal experiment 42 * 20010827 0.10 - fix ia64 unaligned access. 43 * 20010906 0.11 - accept all packets with checksum errors as 44 * otherwise fragments get lost 45 * - fix >> 32 bugs 46 * 0.12 - add statistics counters 47 * - add allmulti/promisc support 48 * 20011009 0.13 - hotplug support, other smaller pci api cleanups 49 * 20011204 0.13a - optical transceiver support added 50 * by Michael Clark <michael@metaparadigm.com> 51 * 20011205 0.13b - call register_netdev earlier in initialization 52 * suppress duplicate link status messages 53 * 20011117 0.14 - ethtool GDRVINFO, GLINK support from jgarzik 54 * 20011204 0.15 get ppc (big endian) working 55 * 20011218 0.16 various cleanups 56 * 20020310 0.17 speedups 57 * 20020610 0.18 - actually use the pci dma api for highmem 58 * - remove pci latency register fiddling 59 * 0.19 - better bist support 60 * - add ihr and reset_phy parameters 61 * - gmii bus probing 62 * - fix missed txok introduced during performance 63 * tuning 64 * 0.20 - fix stupid RFEN thinko. i am such a smurf. 65 * 20040828 0.21 - add hardware vlan accleration 66 * by Neil Horman <nhorman@redhat.com> 67 * 20050406 0.22 - improved DAC ifdefs from Andi Kleen 68 * - removal of dead code from Adrian Bunk 69 * - fix half duplex collision behaviour 70 * Driver Overview 71 * =============== 72 * 73 * This driver was originally written for the National Semiconductor 74 * 83820 chip, a 10/100/1000 Mbps 64 bit PCI ethernet NIC. Hopefully 75 * this code will turn out to be a) clean, b) correct, and c) fast. 76 * With that in mind, I'm aiming to split the code up as much as 77 * reasonably possible. At present there are X major sections that 78 * break down into a) packet receive, b) packet transmit, c) link 79 * management, d) initialization and configuration. Where possible, 80 * these code paths are designed to run in parallel. 81 * 82 * This driver has been tested and found to work with the following 83 * cards (in no particular order): 84 * 85 * Cameo SOHO-GA2000T SOHO-GA2500T 86 * D-Link DGE-500T 87 * PureData PDP8023Z-TG 88 * SMC SMC9452TX SMC9462TX 89 * Netgear GA621 90 * 91 * Special thanks to SMC for providing hardware to test this driver on. 92 * 93 * Reports of success or failure would be greatly appreciated. 94 */ 95 //#define dprintk printk 96 #define dprintk(x...) do { } while (0) 97 98 #include <linux/module.h> 99 #include <linux/moduleparam.h> 100 #include <linux/types.h> 101 #include <linux/pci.h> 102 #include <linux/dma-mapping.h> 103 #include <linux/netdevice.h> 104 #include <linux/etherdevice.h> 105 #include <linux/delay.h> 106 #include <linux/workqueue.h> 107 #include <linux/init.h> 108 #include <linux/interrupt.h> 109 #include <linux/ip.h> /* for iph */ 110 #include <linux/in.h> /* for IPPROTO_... */ 111 #include <linux/compiler.h> 112 #include <linux/prefetch.h> 113 #include <linux/ethtool.h> 114 #include <linux/sched.h> 115 #include <linux/timer.h> 116 #include <linux/if_vlan.h> 117 #include <linux/rtnetlink.h> 118 #include <linux/jiffies.h> 119 #include <linux/slab.h> 120 121 #include <asm/io.h> 122 #include <linux/uaccess.h> 123 124 #define DRV_NAME "ns83820" 125 126 /* Global parameters. See module_param near the bottom. */ 127 static int ihr = 2; 128 static int reset_phy = 0; 129 static int lnksts = 0; /* CFG_LNKSTS bit polarity */ 130 131 /* Dprintk is used for more interesting debug events */ 132 #undef Dprintk 133 #define Dprintk dprintk 134 135 /* tunables */ 136 #define RX_BUF_SIZE 1500 /* 8192 */ 137 #if IS_ENABLED(CONFIG_VLAN_8021Q) 138 #define NS83820_VLAN_ACCEL_SUPPORT 139 #endif 140 141 /* Must not exceed ~65000. */ 142 #define NR_RX_DESC 64 143 #define NR_TX_DESC 128 144 145 /* not tunable */ 146 #define REAL_RX_BUF_SIZE (RX_BUF_SIZE + 14) /* rx/tx mac addr + type */ 147 148 #define MIN_TX_DESC_FREE 8 149 150 /* register defines */ 151 #define CFGCS 0x04 152 153 #define CR_TXE 0x00000001 154 #define CR_TXD 0x00000002 155 /* Ramit : Here's a tip, don't do a RXD immediately followed by an RXE 156 * The Receive engine skips one descriptor and moves 157 * onto the next one!! */ 158 #define CR_RXE 0x00000004 159 #define CR_RXD 0x00000008 160 #define CR_TXR 0x00000010 161 #define CR_RXR 0x00000020 162 #define CR_SWI 0x00000080 163 #define CR_RST 0x00000100 164 165 #define PTSCR_EEBIST_FAIL 0x00000001 166 #define PTSCR_EEBIST_EN 0x00000002 167 #define PTSCR_EELOAD_EN 0x00000004 168 #define PTSCR_RBIST_FAIL 0x000001b8 169 #define PTSCR_RBIST_DONE 0x00000200 170 #define PTSCR_RBIST_EN 0x00000400 171 #define PTSCR_RBIST_RST 0x00002000 172 173 #define MEAR_EEDI 0x00000001 174 #define MEAR_EEDO 0x00000002 175 #define MEAR_EECLK 0x00000004 176 #define MEAR_EESEL 0x00000008 177 #define MEAR_MDIO 0x00000010 178 #define MEAR_MDDIR 0x00000020 179 #define MEAR_MDC 0x00000040 180 181 #define ISR_TXDESC3 0x40000000 182 #define ISR_TXDESC2 0x20000000 183 #define ISR_TXDESC1 0x10000000 184 #define ISR_TXDESC0 0x08000000 185 #define ISR_RXDESC3 0x04000000 186 #define ISR_RXDESC2 0x02000000 187 #define ISR_RXDESC1 0x01000000 188 #define ISR_RXDESC0 0x00800000 189 #define ISR_TXRCMP 0x00400000 190 #define ISR_RXRCMP 0x00200000 191 #define ISR_DPERR 0x00100000 192 #define ISR_SSERR 0x00080000 193 #define ISR_RMABT 0x00040000 194 #define ISR_RTABT 0x00020000 195 #define ISR_RXSOVR 0x00010000 196 #define ISR_HIBINT 0x00008000 197 #define ISR_PHY 0x00004000 198 #define ISR_PME 0x00002000 199 #define ISR_SWI 0x00001000 200 #define ISR_MIB 0x00000800 201 #define ISR_TXURN 0x00000400 202 #define ISR_TXIDLE 0x00000200 203 #define ISR_TXERR 0x00000100 204 #define ISR_TXDESC 0x00000080 205 #define ISR_TXOK 0x00000040 206 #define ISR_RXORN 0x00000020 207 #define ISR_RXIDLE 0x00000010 208 #define ISR_RXEARLY 0x00000008 209 #define ISR_RXERR 0x00000004 210 #define ISR_RXDESC 0x00000002 211 #define ISR_RXOK 0x00000001 212 213 #define TXCFG_CSI 0x80000000 214 #define TXCFG_HBI 0x40000000 215 #define TXCFG_MLB 0x20000000 216 #define TXCFG_ATP 0x10000000 217 #define TXCFG_ECRETRY 0x00800000 218 #define TXCFG_BRST_DIS 0x00080000 219 #define TXCFG_MXDMA1024 0x00000000 220 #define TXCFG_MXDMA512 0x00700000 221 #define TXCFG_MXDMA256 0x00600000 222 #define TXCFG_MXDMA128 0x00500000 223 #define TXCFG_MXDMA64 0x00400000 224 #define TXCFG_MXDMA32 0x00300000 225 #define TXCFG_MXDMA16 0x00200000 226 #define TXCFG_MXDMA8 0x00100000 227 228 #define CFG_LNKSTS 0x80000000 229 #define CFG_SPDSTS 0x60000000 230 #define CFG_SPDSTS1 0x40000000 231 #define CFG_SPDSTS0 0x20000000 232 #define CFG_DUPSTS 0x10000000 233 #define CFG_TBI_EN 0x01000000 234 #define CFG_MODE_1000 0x00400000 235 /* Ramit : Dont' ever use AUTO_1000, it never works and is buggy. 236 * Read the Phy response and then configure the MAC accordingly */ 237 #define CFG_AUTO_1000 0x00200000 238 #define CFG_PINT_CTL 0x001c0000 239 #define CFG_PINT_DUPSTS 0x00100000 240 #define CFG_PINT_LNKSTS 0x00080000 241 #define CFG_PINT_SPDSTS 0x00040000 242 #define CFG_TMRTEST 0x00020000 243 #define CFG_MRM_DIS 0x00010000 244 #define CFG_MWI_DIS 0x00008000 245 #define CFG_T64ADDR 0x00004000 246 #define CFG_PCI64_DET 0x00002000 247 #define CFG_DATA64_EN 0x00001000 248 #define CFG_M64ADDR 0x00000800 249 #define CFG_PHY_RST 0x00000400 250 #define CFG_PHY_DIS 0x00000200 251 #define CFG_EXTSTS_EN 0x00000100 252 #define CFG_REQALG 0x00000080 253 #define CFG_SB 0x00000040 254 #define CFG_POW 0x00000020 255 #define CFG_EXD 0x00000010 256 #define CFG_PESEL 0x00000008 257 #define CFG_BROM_DIS 0x00000004 258 #define CFG_EXT_125 0x00000002 259 #define CFG_BEM 0x00000001 260 261 #define EXTSTS_UDPPKT 0x00200000 262 #define EXTSTS_TCPPKT 0x00080000 263 #define EXTSTS_IPPKT 0x00020000 264 #define EXTSTS_VPKT 0x00010000 265 #define EXTSTS_VTG_MASK 0x0000ffff 266 267 #define SPDSTS_POLARITY (CFG_SPDSTS1 | CFG_SPDSTS0 | CFG_DUPSTS | (lnksts ? CFG_LNKSTS : 0)) 268 269 #define MIBC_MIBS 0x00000008 270 #define MIBC_ACLR 0x00000004 271 #define MIBC_FRZ 0x00000002 272 #define MIBC_WRN 0x00000001 273 274 #define PCR_PSEN (1 << 31) 275 #define PCR_PS_MCAST (1 << 30) 276 #define PCR_PS_DA (1 << 29) 277 #define PCR_STHI_8 (3 << 23) 278 #define PCR_STLO_4 (1 << 23) 279 #define PCR_FFHI_8K (3 << 21) 280 #define PCR_FFLO_4K (1 << 21) 281 #define PCR_PAUSE_CNT 0xFFFE 282 283 #define RXCFG_AEP 0x80000000 284 #define RXCFG_ARP 0x40000000 285 #define RXCFG_STRIPCRC 0x20000000 286 #define RXCFG_RX_FD 0x10000000 287 #define RXCFG_ALP 0x08000000 288 #define RXCFG_AIRL 0x04000000 289 #define RXCFG_MXDMA512 0x00700000 290 #define RXCFG_DRTH 0x0000003e 291 #define RXCFG_DRTH0 0x00000002 292 293 #define RFCR_RFEN 0x80000000 294 #define RFCR_AAB 0x40000000 295 #define RFCR_AAM 0x20000000 296 #define RFCR_AAU 0x10000000 297 #define RFCR_APM 0x08000000 298 #define RFCR_APAT 0x07800000 299 #define RFCR_APAT3 0x04000000 300 #define RFCR_APAT2 0x02000000 301 #define RFCR_APAT1 0x01000000 302 #define RFCR_APAT0 0x00800000 303 #define RFCR_AARP 0x00400000 304 #define RFCR_MHEN 0x00200000 305 #define RFCR_UHEN 0x00100000 306 #define RFCR_ULM 0x00080000 307 308 #define VRCR_RUDPE 0x00000080 309 #define VRCR_RTCPE 0x00000040 310 #define VRCR_RIPE 0x00000020 311 #define VRCR_IPEN 0x00000010 312 #define VRCR_DUTF 0x00000008 313 #define VRCR_DVTF 0x00000004 314 #define VRCR_VTREN 0x00000002 315 #define VRCR_VTDEN 0x00000001 316 317 #define VTCR_PPCHK 0x00000008 318 #define VTCR_GCHK 0x00000004 319 #define VTCR_VPPTI 0x00000002 320 #define VTCR_VGTI 0x00000001 321 322 #define CR 0x00 323 #define CFG 0x04 324 #define MEAR 0x08 325 #define PTSCR 0x0c 326 #define ISR 0x10 327 #define IMR 0x14 328 #define IER 0x18 329 #define IHR 0x1c 330 #define TXDP 0x20 331 #define TXDP_HI 0x24 332 #define TXCFG 0x28 333 #define GPIOR 0x2c 334 #define RXDP 0x30 335 #define RXDP_HI 0x34 336 #define RXCFG 0x38 337 #define PQCR 0x3c 338 #define WCSR 0x40 339 #define PCR 0x44 340 #define RFCR 0x48 341 #define RFDR 0x4c 342 343 #define SRR 0x58 344 345 #define VRCR 0xbc 346 #define VTCR 0xc0 347 #define VDR 0xc4 348 #define CCSR 0xcc 349 350 #define TBICR 0xe0 351 #define TBISR 0xe4 352 #define TANAR 0xe8 353 #define TANLPAR 0xec 354 #define TANER 0xf0 355 #define TESR 0xf4 356 357 #define TBICR_MR_AN_ENABLE 0x00001000 358 #define TBICR_MR_RESTART_AN 0x00000200 359 360 #define TBISR_MR_LINK_STATUS 0x00000020 361 #define TBISR_MR_AN_COMPLETE 0x00000004 362 363 #define TANAR_PS2 0x00000100 364 #define TANAR_PS1 0x00000080 365 #define TANAR_HALF_DUP 0x00000040 366 #define TANAR_FULL_DUP 0x00000020 367 368 #define GPIOR_GP5_OE 0x00000200 369 #define GPIOR_GP4_OE 0x00000100 370 #define GPIOR_GP3_OE 0x00000080 371 #define GPIOR_GP2_OE 0x00000040 372 #define GPIOR_GP1_OE 0x00000020 373 #define GPIOR_GP3_OUT 0x00000004 374 #define GPIOR_GP1_OUT 0x00000001 375 376 #define LINK_AUTONEGOTIATE 0x01 377 #define LINK_DOWN 0x02 378 #define LINK_UP 0x04 379 380 #define HW_ADDR_LEN sizeof(dma_addr_t) 381 #define desc_addr_set(desc, addr) \ 382 do { \ 383 ((desc)[0] = cpu_to_le32(addr)); \ 384 if (HW_ADDR_LEN == 8) \ 385 (desc)[1] = cpu_to_le32(((u64)addr) >> 32); \ 386 } while(0) 387 #define desc_addr_get(desc) \ 388 (le32_to_cpu((desc)[0]) | \ 389 (HW_ADDR_LEN == 8 ? ((dma_addr_t)le32_to_cpu((desc)[1]))<<32 : 0)) 390 391 #define DESC_LINK 0 392 #define DESC_BUFPTR (DESC_LINK + HW_ADDR_LEN/4) 393 #define DESC_CMDSTS (DESC_BUFPTR + HW_ADDR_LEN/4) 394 #define DESC_EXTSTS (DESC_CMDSTS + 4/4) 395 396 #define CMDSTS_OWN 0x80000000 397 #define CMDSTS_MORE 0x40000000 398 #define CMDSTS_INTR 0x20000000 399 #define CMDSTS_ERR 0x10000000 400 #define CMDSTS_OK 0x08000000 401 #define CMDSTS_RUNT 0x00200000 402 #define CMDSTS_LEN_MASK 0x0000ffff 403 404 #define CMDSTS_DEST_MASK 0x01800000 405 #define CMDSTS_DEST_SELF 0x00800000 406 #define CMDSTS_DEST_MULTI 0x01000000 407 408 #define DESC_SIZE 8 /* Should be cache line sized */ 409 410 struct rx_info { 411 spinlock_t lock; 412 int up; 413 unsigned long idle; 414 415 struct sk_buff *skbs[NR_RX_DESC]; 416 417 __le32 *next_rx_desc; 418 u16 next_rx, next_empty; 419 420 __le32 *descs; 421 dma_addr_t phy_descs; 422 }; 423 424 425 struct ns83820 { 426 u8 __iomem *base; 427 428 struct pci_dev *pci_dev; 429 struct net_device *ndev; 430 431 struct rx_info rx_info; 432 struct tasklet_struct rx_tasklet; 433 434 unsigned ihr; 435 struct work_struct tq_refill; 436 437 /* protects everything below. irqsave when using. */ 438 spinlock_t misc_lock; 439 440 u32 CFG_cache; 441 442 u32 MEAR_cache; 443 u32 IMR_cache; 444 445 unsigned linkstate; 446 447 spinlock_t tx_lock; 448 449 u16 tx_done_idx; 450 u16 tx_idx; 451 volatile u16 tx_free_idx; /* idx of free desc chain */ 452 u16 tx_intr_idx; 453 454 atomic_t nr_tx_skbs; 455 struct sk_buff *tx_skbs[NR_TX_DESC]; 456 457 char pad[16] __attribute__((aligned(16))); 458 __le32 *tx_descs; 459 dma_addr_t tx_phy_descs; 460 461 struct timer_list tx_watchdog; 462 }; 463 464 static inline struct ns83820 *PRIV(struct net_device *dev) 465 { 466 return netdev_priv(dev); 467 } 468 469 #define __kick_rx(dev) writel(CR_RXE, dev->base + CR) 470 471 static inline void kick_rx(struct net_device *ndev) 472 { 473 struct ns83820 *dev = PRIV(ndev); 474 dprintk("kick_rx: maybe kicking\n"); 475 if (test_and_clear_bit(0, &dev->rx_info.idle)) { 476 dprintk("actually kicking\n"); 477 writel(dev->rx_info.phy_descs + 478 (4 * DESC_SIZE * dev->rx_info.next_rx), 479 dev->base + RXDP); 480 if (dev->rx_info.next_rx == dev->rx_info.next_empty) 481 printk(KERN_DEBUG "%s: uh-oh: next_rx == next_empty???\n", 482 ndev->name); 483 __kick_rx(dev); 484 } 485 } 486 487 //free = (tx_done_idx + NR_TX_DESC-2 - free_idx) % NR_TX_DESC 488 #define start_tx_okay(dev) \ 489 (((NR_TX_DESC-2 + dev->tx_done_idx - dev->tx_free_idx) % NR_TX_DESC) > MIN_TX_DESC_FREE) 490 491 /* Packet Receiver 492 * 493 * The hardware supports linked lists of receive descriptors for 494 * which ownership is transferred back and forth by means of an 495 * ownership bit. While the hardware does support the use of a 496 * ring for receive descriptors, we only make use of a chain in 497 * an attempt to reduce bus traffic under heavy load scenarios. 498 * This will also make bugs a bit more obvious. The current code 499 * only makes use of a single rx chain; I hope to implement 500 * priority based rx for version 1.0. Goal: even under overload 501 * conditions, still route realtime traffic with as low jitter as 502 * possible. 503 */ 504 static inline void build_rx_desc(struct ns83820 *dev, __le32 *desc, dma_addr_t link, dma_addr_t buf, u32 cmdsts, u32 extsts) 505 { 506 desc_addr_set(desc + DESC_LINK, link); 507 desc_addr_set(desc + DESC_BUFPTR, buf); 508 desc[DESC_EXTSTS] = cpu_to_le32(extsts); 509 mb(); 510 desc[DESC_CMDSTS] = cpu_to_le32(cmdsts); 511 } 512 513 #define nr_rx_empty(dev) ((NR_RX_DESC-2 + dev->rx_info.next_rx - dev->rx_info.next_empty) % NR_RX_DESC) 514 static inline int ns83820_add_rx_skb(struct ns83820 *dev, struct sk_buff *skb) 515 { 516 unsigned next_empty; 517 u32 cmdsts; 518 __le32 *sg; 519 dma_addr_t buf; 520 521 next_empty = dev->rx_info.next_empty; 522 523 /* don't overrun last rx marker */ 524 if (unlikely(nr_rx_empty(dev) <= 2)) { 525 kfree_skb(skb); 526 return 1; 527 } 528 529 #if 0 530 dprintk("next_empty[%d] nr_used[%d] next_rx[%d]\n", 531 dev->rx_info.next_empty, 532 dev->rx_info.nr_used, 533 dev->rx_info.next_rx 534 ); 535 #endif 536 537 sg = dev->rx_info.descs + (next_empty * DESC_SIZE); 538 BUG_ON(NULL != dev->rx_info.skbs[next_empty]); 539 dev->rx_info.skbs[next_empty] = skb; 540 541 dev->rx_info.next_empty = (next_empty + 1) % NR_RX_DESC; 542 cmdsts = REAL_RX_BUF_SIZE | CMDSTS_INTR; 543 buf = pci_map_single(dev->pci_dev, skb->data, 544 REAL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE); 545 build_rx_desc(dev, sg, 0, buf, cmdsts, 0); 546 /* update link of previous rx */ 547 if (likely(next_empty != dev->rx_info.next_rx)) 548 dev->rx_info.descs[((NR_RX_DESC + next_empty - 1) % NR_RX_DESC) * DESC_SIZE] = cpu_to_le32(dev->rx_info.phy_descs + (next_empty * DESC_SIZE * 4)); 549 550 return 0; 551 } 552 553 static inline int rx_refill(struct net_device *ndev, gfp_t gfp) 554 { 555 struct ns83820 *dev = PRIV(ndev); 556 unsigned i; 557 unsigned long flags = 0; 558 559 if (unlikely(nr_rx_empty(dev) <= 2)) 560 return 0; 561 562 dprintk("rx_refill(%p)\n", ndev); 563 if (gfp == GFP_ATOMIC) 564 spin_lock_irqsave(&dev->rx_info.lock, flags); 565 for (i=0; i<NR_RX_DESC; i++) { 566 struct sk_buff *skb; 567 long res; 568 569 /* extra 16 bytes for alignment */ 570 skb = __netdev_alloc_skb(ndev, REAL_RX_BUF_SIZE+16, gfp); 571 if (unlikely(!skb)) 572 break; 573 574 skb_reserve(skb, skb->data - PTR_ALIGN(skb->data, 16)); 575 if (gfp != GFP_ATOMIC) 576 spin_lock_irqsave(&dev->rx_info.lock, flags); 577 res = ns83820_add_rx_skb(dev, skb); 578 if (gfp != GFP_ATOMIC) 579 spin_unlock_irqrestore(&dev->rx_info.lock, flags); 580 if (res) { 581 i = 1; 582 break; 583 } 584 } 585 if (gfp == GFP_ATOMIC) 586 spin_unlock_irqrestore(&dev->rx_info.lock, flags); 587 588 return i ? 0 : -ENOMEM; 589 } 590 591 static void rx_refill_atomic(struct net_device *ndev) 592 { 593 rx_refill(ndev, GFP_ATOMIC); 594 } 595 596 /* REFILL */ 597 static inline void queue_refill(struct work_struct *work) 598 { 599 struct ns83820 *dev = container_of(work, struct ns83820, tq_refill); 600 struct net_device *ndev = dev->ndev; 601 602 rx_refill(ndev, GFP_KERNEL); 603 if (dev->rx_info.up) 604 kick_rx(ndev); 605 } 606 607 static inline void clear_rx_desc(struct ns83820 *dev, unsigned i) 608 { 609 build_rx_desc(dev, dev->rx_info.descs + (DESC_SIZE * i), 0, 0, CMDSTS_OWN, 0); 610 } 611 612 static void phy_intr(struct net_device *ndev) 613 { 614 struct ns83820 *dev = PRIV(ndev); 615 static const char *speeds[] = { "10", "100", "1000", "1000(?)", "1000F" }; 616 u32 cfg, new_cfg; 617 u32 tbisr, tanar, tanlpar; 618 int speed, fullduplex, newlinkstate; 619 620 cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY; 621 622 if (dev->CFG_cache & CFG_TBI_EN) { 623 /* we have an optical transceiver */ 624 tbisr = readl(dev->base + TBISR); 625 tanar = readl(dev->base + TANAR); 626 tanlpar = readl(dev->base + TANLPAR); 627 dprintk("phy_intr: tbisr=%08x, tanar=%08x, tanlpar=%08x\n", 628 tbisr, tanar, tanlpar); 629 630 if ( (fullduplex = (tanlpar & TANAR_FULL_DUP) && 631 (tanar & TANAR_FULL_DUP)) ) { 632 633 /* both of us are full duplex */ 634 writel(readl(dev->base + TXCFG) 635 | TXCFG_CSI | TXCFG_HBI | TXCFG_ATP, 636 dev->base + TXCFG); 637 writel(readl(dev->base + RXCFG) | RXCFG_RX_FD, 638 dev->base + RXCFG); 639 /* Light up full duplex LED */ 640 writel(readl(dev->base + GPIOR) | GPIOR_GP1_OUT, 641 dev->base + GPIOR); 642 643 } else if (((tanlpar & TANAR_HALF_DUP) && 644 (tanar & TANAR_HALF_DUP)) || 645 ((tanlpar & TANAR_FULL_DUP) && 646 (tanar & TANAR_HALF_DUP)) || 647 ((tanlpar & TANAR_HALF_DUP) && 648 (tanar & TANAR_FULL_DUP))) { 649 650 /* one or both of us are half duplex */ 651 writel((readl(dev->base + TXCFG) 652 & ~(TXCFG_CSI | TXCFG_HBI)) | TXCFG_ATP, 653 dev->base + TXCFG); 654 writel(readl(dev->base + RXCFG) & ~RXCFG_RX_FD, 655 dev->base + RXCFG); 656 /* Turn off full duplex LED */ 657 writel(readl(dev->base + GPIOR) & ~GPIOR_GP1_OUT, 658 dev->base + GPIOR); 659 } 660 661 speed = 4; /* 1000F */ 662 663 } else { 664 /* we have a copper transceiver */ 665 new_cfg = dev->CFG_cache & ~(CFG_SB | CFG_MODE_1000 | CFG_SPDSTS); 666 667 if (cfg & CFG_SPDSTS1) 668 new_cfg |= CFG_MODE_1000; 669 else 670 new_cfg &= ~CFG_MODE_1000; 671 672 speed = ((cfg / CFG_SPDSTS0) & 3); 673 fullduplex = (cfg & CFG_DUPSTS); 674 675 if (fullduplex) { 676 new_cfg |= CFG_SB; 677 writel(readl(dev->base + TXCFG) 678 | TXCFG_CSI | TXCFG_HBI, 679 dev->base + TXCFG); 680 writel(readl(dev->base + RXCFG) | RXCFG_RX_FD, 681 dev->base + RXCFG); 682 } else { 683 writel(readl(dev->base + TXCFG) 684 & ~(TXCFG_CSI | TXCFG_HBI), 685 dev->base + TXCFG); 686 writel(readl(dev->base + RXCFG) & ~(RXCFG_RX_FD), 687 dev->base + RXCFG); 688 } 689 690 if ((cfg & CFG_LNKSTS) && 691 ((new_cfg ^ dev->CFG_cache) != 0)) { 692 writel(new_cfg, dev->base + CFG); 693 dev->CFG_cache = new_cfg; 694 } 695 696 dev->CFG_cache &= ~CFG_SPDSTS; 697 dev->CFG_cache |= cfg & CFG_SPDSTS; 698 } 699 700 newlinkstate = (cfg & CFG_LNKSTS) ? LINK_UP : LINK_DOWN; 701 702 if (newlinkstate & LINK_UP && 703 dev->linkstate != newlinkstate) { 704 netif_start_queue(ndev); 705 netif_wake_queue(ndev); 706 printk(KERN_INFO "%s: link now %s mbps, %s duplex and up.\n", 707 ndev->name, 708 speeds[speed], 709 fullduplex ? "full" : "half"); 710 } else if (newlinkstate & LINK_DOWN && 711 dev->linkstate != newlinkstate) { 712 netif_stop_queue(ndev); 713 printk(KERN_INFO "%s: link now down.\n", ndev->name); 714 } 715 716 dev->linkstate = newlinkstate; 717 } 718 719 static int ns83820_setup_rx(struct net_device *ndev) 720 { 721 struct ns83820 *dev = PRIV(ndev); 722 unsigned i; 723 int ret; 724 725 dprintk("ns83820_setup_rx(%p)\n", ndev); 726 727 dev->rx_info.idle = 1; 728 dev->rx_info.next_rx = 0; 729 dev->rx_info.next_rx_desc = dev->rx_info.descs; 730 dev->rx_info.next_empty = 0; 731 732 for (i=0; i<NR_RX_DESC; i++) 733 clear_rx_desc(dev, i); 734 735 writel(0, dev->base + RXDP_HI); 736 writel(dev->rx_info.phy_descs, dev->base + RXDP); 737 738 ret = rx_refill(ndev, GFP_KERNEL); 739 if (!ret) { 740 dprintk("starting receiver\n"); 741 /* prevent the interrupt handler from stomping on us */ 742 spin_lock_irq(&dev->rx_info.lock); 743 744 writel(0x0001, dev->base + CCSR); 745 writel(0, dev->base + RFCR); 746 writel(0x7fc00000, dev->base + RFCR); 747 writel(0xffc00000, dev->base + RFCR); 748 749 dev->rx_info.up = 1; 750 751 phy_intr(ndev); 752 753 /* Okay, let it rip */ 754 spin_lock(&dev->misc_lock); 755 dev->IMR_cache |= ISR_PHY; 756 dev->IMR_cache |= ISR_RXRCMP; 757 //dev->IMR_cache |= ISR_RXERR; 758 //dev->IMR_cache |= ISR_RXOK; 759 dev->IMR_cache |= ISR_RXORN; 760 dev->IMR_cache |= ISR_RXSOVR; 761 dev->IMR_cache |= ISR_RXDESC; 762 dev->IMR_cache |= ISR_RXIDLE; 763 dev->IMR_cache |= ISR_TXDESC; 764 dev->IMR_cache |= ISR_TXIDLE; 765 766 writel(dev->IMR_cache, dev->base + IMR); 767 writel(1, dev->base + IER); 768 spin_unlock(&dev->misc_lock); 769 770 kick_rx(ndev); 771 772 spin_unlock_irq(&dev->rx_info.lock); 773 } 774 return ret; 775 } 776 777 static void ns83820_cleanup_rx(struct ns83820 *dev) 778 { 779 unsigned i; 780 unsigned long flags; 781 782 dprintk("ns83820_cleanup_rx(%p)\n", dev); 783 784 /* disable receive interrupts */ 785 spin_lock_irqsave(&dev->misc_lock, flags); 786 dev->IMR_cache &= ~(ISR_RXOK | ISR_RXDESC | ISR_RXERR | ISR_RXEARLY | ISR_RXIDLE); 787 writel(dev->IMR_cache, dev->base + IMR); 788 spin_unlock_irqrestore(&dev->misc_lock, flags); 789 790 /* synchronize with the interrupt handler and kill it */ 791 dev->rx_info.up = 0; 792 synchronize_irq(dev->pci_dev->irq); 793 794 /* touch the pci bus... */ 795 readl(dev->base + IMR); 796 797 /* assumes the transmitter is already disabled and reset */ 798 writel(0, dev->base + RXDP_HI); 799 writel(0, dev->base + RXDP); 800 801 for (i=0; i<NR_RX_DESC; i++) { 802 struct sk_buff *skb = dev->rx_info.skbs[i]; 803 dev->rx_info.skbs[i] = NULL; 804 clear_rx_desc(dev, i); 805 kfree_skb(skb); 806 } 807 } 808 809 static void ns83820_rx_kick(struct net_device *ndev) 810 { 811 struct ns83820 *dev = PRIV(ndev); 812 /*if (nr_rx_empty(dev) >= NR_RX_DESC/4)*/ { 813 if (dev->rx_info.up) { 814 rx_refill_atomic(ndev); 815 kick_rx(ndev); 816 } 817 } 818 819 if (dev->rx_info.up && nr_rx_empty(dev) > NR_RX_DESC*3/4) 820 schedule_work(&dev->tq_refill); 821 else 822 kick_rx(ndev); 823 if (dev->rx_info.idle) 824 printk(KERN_DEBUG "%s: BAD\n", ndev->name); 825 } 826 827 /* rx_irq 828 * 829 */ 830 static void rx_irq(struct net_device *ndev) 831 { 832 struct ns83820 *dev = PRIV(ndev); 833 struct rx_info *info = &dev->rx_info; 834 unsigned next_rx; 835 int rx_rc, len; 836 u32 cmdsts; 837 __le32 *desc; 838 unsigned long flags; 839 int nr = 0; 840 841 dprintk("rx_irq(%p)\n", ndev); 842 dprintk("rxdp: %08x, descs: %08lx next_rx[%d]: %p next_empty[%d]: %p\n", 843 readl(dev->base + RXDP), 844 (long)(dev->rx_info.phy_descs), 845 (int)dev->rx_info.next_rx, 846 (dev->rx_info.descs + (DESC_SIZE * dev->rx_info.next_rx)), 847 (int)dev->rx_info.next_empty, 848 (dev->rx_info.descs + (DESC_SIZE * dev->rx_info.next_empty)) 849 ); 850 851 spin_lock_irqsave(&info->lock, flags); 852 if (!info->up) 853 goto out; 854 855 dprintk("walking descs\n"); 856 next_rx = info->next_rx; 857 desc = info->next_rx_desc; 858 while ((CMDSTS_OWN & (cmdsts = le32_to_cpu(desc[DESC_CMDSTS]))) && 859 (cmdsts != CMDSTS_OWN)) { 860 struct sk_buff *skb; 861 u32 extsts = le32_to_cpu(desc[DESC_EXTSTS]); 862 dma_addr_t bufptr = desc_addr_get(desc + DESC_BUFPTR); 863 864 dprintk("cmdsts: %08x\n", cmdsts); 865 dprintk("link: %08x\n", cpu_to_le32(desc[DESC_LINK])); 866 dprintk("extsts: %08x\n", extsts); 867 868 skb = info->skbs[next_rx]; 869 info->skbs[next_rx] = NULL; 870 info->next_rx = (next_rx + 1) % NR_RX_DESC; 871 872 mb(); 873 clear_rx_desc(dev, next_rx); 874 875 pci_unmap_single(dev->pci_dev, bufptr, 876 RX_BUF_SIZE, PCI_DMA_FROMDEVICE); 877 len = cmdsts & CMDSTS_LEN_MASK; 878 #ifdef NS83820_VLAN_ACCEL_SUPPORT 879 /* NH: As was mentioned below, this chip is kinda 880 * brain dead about vlan tag stripping. Frames 881 * that are 64 bytes with a vlan header appended 882 * like arp frames, or pings, are flagged as Runts 883 * when the tag is stripped and hardware. This 884 * also means that the OK bit in the descriptor 885 * is cleared when the frame comes in so we have 886 * to do a specific length check here to make sure 887 * the frame would have been ok, had we not stripped 888 * the tag. 889 */ 890 if (likely((CMDSTS_OK & cmdsts) || 891 ((cmdsts & CMDSTS_RUNT) && len >= 56))) { 892 #else 893 if (likely(CMDSTS_OK & cmdsts)) { 894 #endif 895 skb_put(skb, len); 896 if (unlikely(!skb)) 897 goto netdev_mangle_me_harder_failed; 898 if (cmdsts & CMDSTS_DEST_MULTI) 899 ndev->stats.multicast++; 900 ndev->stats.rx_packets++; 901 ndev->stats.rx_bytes += len; 902 if ((extsts & 0x002a0000) && !(extsts & 0x00540000)) { 903 skb->ip_summed = CHECKSUM_UNNECESSARY; 904 } else { 905 skb_checksum_none_assert(skb); 906 } 907 skb->protocol = eth_type_trans(skb, ndev); 908 #ifdef NS83820_VLAN_ACCEL_SUPPORT 909 if(extsts & EXTSTS_VPKT) { 910 unsigned short tag; 911 912 tag = ntohs(extsts & EXTSTS_VTG_MASK); 913 __vlan_hwaccel_put_tag(skb, htons(ETH_P_IPV6), tag); 914 } 915 #endif 916 rx_rc = netif_rx(skb); 917 if (NET_RX_DROP == rx_rc) { 918 netdev_mangle_me_harder_failed: 919 ndev->stats.rx_dropped++; 920 } 921 } else { 922 dev_kfree_skb_irq(skb); 923 } 924 925 nr++; 926 next_rx = info->next_rx; 927 desc = info->descs + (DESC_SIZE * next_rx); 928 } 929 info->next_rx = next_rx; 930 info->next_rx_desc = info->descs + (DESC_SIZE * next_rx); 931 932 out: 933 if (0 && !nr) { 934 Dprintk("dazed: cmdsts_f: %08x\n", cmdsts); 935 } 936 937 spin_unlock_irqrestore(&info->lock, flags); 938 } 939 940 static void rx_action(unsigned long _dev) 941 { 942 struct net_device *ndev = (void *)_dev; 943 struct ns83820 *dev = PRIV(ndev); 944 rx_irq(ndev); 945 writel(ihr, dev->base + IHR); 946 947 spin_lock_irq(&dev->misc_lock); 948 dev->IMR_cache |= ISR_RXDESC; 949 writel(dev->IMR_cache, dev->base + IMR); 950 spin_unlock_irq(&dev->misc_lock); 951 952 rx_irq(ndev); 953 ns83820_rx_kick(ndev); 954 } 955 956 /* Packet Transmit code 957 */ 958 static inline void kick_tx(struct ns83820 *dev) 959 { 960 dprintk("kick_tx(%p): tx_idx=%d free_idx=%d\n", 961 dev, dev->tx_idx, dev->tx_free_idx); 962 writel(CR_TXE, dev->base + CR); 963 } 964 965 /* No spinlock needed on the transmit irq path as the interrupt handler is 966 * serialized. 967 */ 968 static void do_tx_done(struct net_device *ndev) 969 { 970 struct ns83820 *dev = PRIV(ndev); 971 u32 cmdsts, tx_done_idx; 972 __le32 *desc; 973 974 dprintk("do_tx_done(%p)\n", ndev); 975 tx_done_idx = dev->tx_done_idx; 976 desc = dev->tx_descs + (tx_done_idx * DESC_SIZE); 977 978 dprintk("tx_done_idx=%d free_idx=%d cmdsts=%08x\n", 979 tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS])); 980 while ((tx_done_idx != dev->tx_free_idx) && 981 !(CMDSTS_OWN & (cmdsts = le32_to_cpu(desc[DESC_CMDSTS]))) ) { 982 struct sk_buff *skb; 983 unsigned len; 984 dma_addr_t addr; 985 986 if (cmdsts & CMDSTS_ERR) 987 ndev->stats.tx_errors++; 988 if (cmdsts & CMDSTS_OK) 989 ndev->stats.tx_packets++; 990 if (cmdsts & CMDSTS_OK) 991 ndev->stats.tx_bytes += cmdsts & 0xffff; 992 993 dprintk("tx_done_idx=%d free_idx=%d cmdsts=%08x\n", 994 tx_done_idx, dev->tx_free_idx, cmdsts); 995 skb = dev->tx_skbs[tx_done_idx]; 996 dev->tx_skbs[tx_done_idx] = NULL; 997 dprintk("done(%p)\n", skb); 998 999 len = cmdsts & CMDSTS_LEN_MASK; 1000 addr = desc_addr_get(desc + DESC_BUFPTR); 1001 if (skb) { 1002 pci_unmap_single(dev->pci_dev, 1003 addr, 1004 len, 1005 PCI_DMA_TODEVICE); 1006 dev_kfree_skb_irq(skb); 1007 atomic_dec(&dev->nr_tx_skbs); 1008 } else 1009 pci_unmap_page(dev->pci_dev, 1010 addr, 1011 len, 1012 PCI_DMA_TODEVICE); 1013 1014 tx_done_idx = (tx_done_idx + 1) % NR_TX_DESC; 1015 dev->tx_done_idx = tx_done_idx; 1016 desc[DESC_CMDSTS] = cpu_to_le32(0); 1017 mb(); 1018 desc = dev->tx_descs + (tx_done_idx * DESC_SIZE); 1019 } 1020 1021 /* Allow network stack to resume queueing packets after we've 1022 * finished transmitting at least 1/4 of the packets in the queue. 1023 */ 1024 if (netif_queue_stopped(ndev) && start_tx_okay(dev)) { 1025 dprintk("start_queue(%p)\n", ndev); 1026 netif_start_queue(ndev); 1027 netif_wake_queue(ndev); 1028 } 1029 } 1030 1031 static void ns83820_cleanup_tx(struct ns83820 *dev) 1032 { 1033 unsigned i; 1034 1035 for (i=0; i<NR_TX_DESC; i++) { 1036 struct sk_buff *skb = dev->tx_skbs[i]; 1037 dev->tx_skbs[i] = NULL; 1038 if (skb) { 1039 __le32 *desc = dev->tx_descs + (i * DESC_SIZE); 1040 pci_unmap_single(dev->pci_dev, 1041 desc_addr_get(desc + DESC_BUFPTR), 1042 le32_to_cpu(desc[DESC_CMDSTS]) & CMDSTS_LEN_MASK, 1043 PCI_DMA_TODEVICE); 1044 dev_kfree_skb_irq(skb); 1045 atomic_dec(&dev->nr_tx_skbs); 1046 } 1047 } 1048 1049 memset(dev->tx_descs, 0, NR_TX_DESC * DESC_SIZE * 4); 1050 } 1051 1052 /* transmit routine. This code relies on the network layer serializing 1053 * its calls in, but will run happily in parallel with the interrupt 1054 * handler. This code currently has provisions for fragmenting tx buffers 1055 * while trying to track down a bug in either the zero copy code or 1056 * the tx fifo (hence the MAX_FRAG_LEN). 1057 */ 1058 static netdev_tx_t ns83820_hard_start_xmit(struct sk_buff *skb, 1059 struct net_device *ndev) 1060 { 1061 struct ns83820 *dev = PRIV(ndev); 1062 u32 free_idx, cmdsts, extsts; 1063 int nr_free, nr_frags; 1064 unsigned tx_done_idx, last_idx; 1065 dma_addr_t buf; 1066 unsigned len; 1067 skb_frag_t *frag; 1068 int stopped = 0; 1069 int do_intr = 0; 1070 volatile __le32 *first_desc; 1071 1072 dprintk("ns83820_hard_start_xmit\n"); 1073 1074 nr_frags = skb_shinfo(skb)->nr_frags; 1075 again: 1076 if (unlikely(dev->CFG_cache & CFG_LNKSTS)) { 1077 netif_stop_queue(ndev); 1078 if (unlikely(dev->CFG_cache & CFG_LNKSTS)) 1079 return NETDEV_TX_BUSY; 1080 netif_start_queue(ndev); 1081 } 1082 1083 last_idx = free_idx = dev->tx_free_idx; 1084 tx_done_idx = dev->tx_done_idx; 1085 nr_free = (tx_done_idx + NR_TX_DESC-2 - free_idx) % NR_TX_DESC; 1086 nr_free -= 1; 1087 if (nr_free <= nr_frags) { 1088 dprintk("stop_queue - not enough(%p)\n", ndev); 1089 netif_stop_queue(ndev); 1090 1091 /* Check again: we may have raced with a tx done irq */ 1092 if (dev->tx_done_idx != tx_done_idx) { 1093 dprintk("restart queue(%p)\n", ndev); 1094 netif_start_queue(ndev); 1095 goto again; 1096 } 1097 return NETDEV_TX_BUSY; 1098 } 1099 1100 if (free_idx == dev->tx_intr_idx) { 1101 do_intr = 1; 1102 dev->tx_intr_idx = (dev->tx_intr_idx + NR_TX_DESC/4) % NR_TX_DESC; 1103 } 1104 1105 nr_free -= nr_frags; 1106 if (nr_free < MIN_TX_DESC_FREE) { 1107 dprintk("stop_queue - last entry(%p)\n", ndev); 1108 netif_stop_queue(ndev); 1109 stopped = 1; 1110 } 1111 1112 frag = skb_shinfo(skb)->frags; 1113 if (!nr_frags) 1114 frag = NULL; 1115 extsts = 0; 1116 if (skb->ip_summed == CHECKSUM_PARTIAL) { 1117 extsts |= EXTSTS_IPPKT; 1118 if (IPPROTO_TCP == ip_hdr(skb)->protocol) 1119 extsts |= EXTSTS_TCPPKT; 1120 else if (IPPROTO_UDP == ip_hdr(skb)->protocol) 1121 extsts |= EXTSTS_UDPPKT; 1122 } 1123 1124 #ifdef NS83820_VLAN_ACCEL_SUPPORT 1125 if (skb_vlan_tag_present(skb)) { 1126 /* fetch the vlan tag info out of the 1127 * ancillary data if the vlan code 1128 * is using hw vlan acceleration 1129 */ 1130 short tag = skb_vlan_tag_get(skb); 1131 extsts |= (EXTSTS_VPKT | htons(tag)); 1132 } 1133 #endif 1134 1135 len = skb->len; 1136 if (nr_frags) 1137 len -= skb->data_len; 1138 buf = pci_map_single(dev->pci_dev, skb->data, len, PCI_DMA_TODEVICE); 1139 1140 first_desc = dev->tx_descs + (free_idx * DESC_SIZE); 1141 1142 for (;;) { 1143 volatile __le32 *desc = dev->tx_descs + (free_idx * DESC_SIZE); 1144 1145 dprintk("frag[%3u]: %4u @ 0x%08Lx\n", free_idx, len, 1146 (unsigned long long)buf); 1147 last_idx = free_idx; 1148 free_idx = (free_idx + 1) % NR_TX_DESC; 1149 desc[DESC_LINK] = cpu_to_le32(dev->tx_phy_descs + (free_idx * DESC_SIZE * 4)); 1150 desc_addr_set(desc + DESC_BUFPTR, buf); 1151 desc[DESC_EXTSTS] = cpu_to_le32(extsts); 1152 1153 cmdsts = ((nr_frags) ? CMDSTS_MORE : do_intr ? CMDSTS_INTR : 0); 1154 cmdsts |= (desc == first_desc) ? 0 : CMDSTS_OWN; 1155 cmdsts |= len; 1156 desc[DESC_CMDSTS] = cpu_to_le32(cmdsts); 1157 1158 if (!nr_frags) 1159 break; 1160 1161 buf = skb_frag_dma_map(&dev->pci_dev->dev, frag, 0, 1162 skb_frag_size(frag), DMA_TO_DEVICE); 1163 dprintk("frag: buf=%08Lx page=%08lx offset=%08lx\n", 1164 (long long)buf, (long) page_to_pfn(frag->page), 1165 frag->page_offset); 1166 len = skb_frag_size(frag); 1167 frag++; 1168 nr_frags--; 1169 } 1170 dprintk("done pkt\n"); 1171 1172 spin_lock_irq(&dev->tx_lock); 1173 dev->tx_skbs[last_idx] = skb; 1174 first_desc[DESC_CMDSTS] |= cpu_to_le32(CMDSTS_OWN); 1175 dev->tx_free_idx = free_idx; 1176 atomic_inc(&dev->nr_tx_skbs); 1177 spin_unlock_irq(&dev->tx_lock); 1178 1179 kick_tx(dev); 1180 1181 /* Check again: we may have raced with a tx done irq */ 1182 if (stopped && (dev->tx_done_idx != tx_done_idx) && start_tx_okay(dev)) 1183 netif_start_queue(ndev); 1184 1185 return NETDEV_TX_OK; 1186 } 1187 1188 static void ns83820_update_stats(struct ns83820 *dev) 1189 { 1190 struct net_device *ndev = dev->ndev; 1191 u8 __iomem *base = dev->base; 1192 1193 /* the DP83820 will freeze counters, so we need to read all of them */ 1194 ndev->stats.rx_errors += readl(base + 0x60) & 0xffff; 1195 ndev->stats.rx_crc_errors += readl(base + 0x64) & 0xffff; 1196 ndev->stats.rx_missed_errors += readl(base + 0x68) & 0xffff; 1197 ndev->stats.rx_frame_errors += readl(base + 0x6c) & 0xffff; 1198 /*ndev->stats.rx_symbol_errors +=*/ readl(base + 0x70); 1199 ndev->stats.rx_length_errors += readl(base + 0x74) & 0xffff; 1200 ndev->stats.rx_length_errors += readl(base + 0x78) & 0xffff; 1201 /*ndev->stats.rx_badopcode_errors += */ readl(base + 0x7c); 1202 /*ndev->stats.rx_pause_count += */ readl(base + 0x80); 1203 /*ndev->stats.tx_pause_count += */ readl(base + 0x84); 1204 ndev->stats.tx_carrier_errors += readl(base + 0x88) & 0xff; 1205 } 1206 1207 static struct net_device_stats *ns83820_get_stats(struct net_device *ndev) 1208 { 1209 struct ns83820 *dev = PRIV(ndev); 1210 1211 /* somewhat overkill */ 1212 spin_lock_irq(&dev->misc_lock); 1213 ns83820_update_stats(dev); 1214 spin_unlock_irq(&dev->misc_lock); 1215 1216 return &ndev->stats; 1217 } 1218 1219 /* Let ethtool retrieve info */ 1220 static int ns83820_get_link_ksettings(struct net_device *ndev, 1221 struct ethtool_link_ksettings *cmd) 1222 { 1223 struct ns83820 *dev = PRIV(ndev); 1224 u32 cfg, tanar, tbicr; 1225 int fullduplex = 0; 1226 u32 supported; 1227 1228 /* 1229 * Here's the list of available ethtool commands from other drivers: 1230 * cmd->advertising = 1231 * ethtool_cmd_speed_set(cmd, ...) 1232 * cmd->duplex = 1233 * cmd->port = 0; 1234 * cmd->phy_address = 1235 * cmd->transceiver = 0; 1236 * cmd->autoneg = 1237 * cmd->maxtxpkt = 0; 1238 * cmd->maxrxpkt = 0; 1239 */ 1240 1241 /* read current configuration */ 1242 cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY; 1243 tanar = readl(dev->base + TANAR); 1244 tbicr = readl(dev->base + TBICR); 1245 1246 fullduplex = (cfg & CFG_DUPSTS) ? 1 : 0; 1247 1248 supported = SUPPORTED_Autoneg; 1249 1250 if (dev->CFG_cache & CFG_TBI_EN) { 1251 /* we have optical interface */ 1252 supported |= SUPPORTED_1000baseT_Half | 1253 SUPPORTED_1000baseT_Full | 1254 SUPPORTED_FIBRE; 1255 cmd->base.port = PORT_FIBRE; 1256 } else { 1257 /* we have copper */ 1258 supported |= SUPPORTED_10baseT_Half | 1259 SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half | 1260 SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Half | 1261 SUPPORTED_1000baseT_Full | 1262 SUPPORTED_MII; 1263 cmd->base.port = PORT_MII; 1264 } 1265 1266 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 1267 supported); 1268 1269 cmd->base.duplex = fullduplex ? DUPLEX_FULL : DUPLEX_HALF; 1270 switch (cfg / CFG_SPDSTS0 & 3) { 1271 case 2: 1272 cmd->base.speed = SPEED_1000; 1273 break; 1274 case 1: 1275 cmd->base.speed = SPEED_100; 1276 break; 1277 default: 1278 cmd->base.speed = SPEED_10; 1279 break; 1280 } 1281 cmd->base.autoneg = (tbicr & TBICR_MR_AN_ENABLE) 1282 ? AUTONEG_ENABLE : AUTONEG_DISABLE; 1283 return 0; 1284 } 1285 1286 /* Let ethool change settings*/ 1287 static int ns83820_set_link_ksettings(struct net_device *ndev, 1288 const struct ethtool_link_ksettings *cmd) 1289 { 1290 struct ns83820 *dev = PRIV(ndev); 1291 u32 cfg, tanar; 1292 int have_optical = 0; 1293 int fullduplex = 0; 1294 1295 /* read current configuration */ 1296 cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY; 1297 tanar = readl(dev->base + TANAR); 1298 1299 if (dev->CFG_cache & CFG_TBI_EN) { 1300 /* we have optical */ 1301 have_optical = 1; 1302 fullduplex = (tanar & TANAR_FULL_DUP); 1303 1304 } else { 1305 /* we have copper */ 1306 fullduplex = cfg & CFG_DUPSTS; 1307 } 1308 1309 spin_lock_irq(&dev->misc_lock); 1310 spin_lock(&dev->tx_lock); 1311 1312 /* Set duplex */ 1313 if (cmd->base.duplex != fullduplex) { 1314 if (have_optical) { 1315 /*set full duplex*/ 1316 if (cmd->base.duplex == DUPLEX_FULL) { 1317 /* force full duplex */ 1318 writel(readl(dev->base + TXCFG) 1319 | TXCFG_CSI | TXCFG_HBI | TXCFG_ATP, 1320 dev->base + TXCFG); 1321 writel(readl(dev->base + RXCFG) | RXCFG_RX_FD, 1322 dev->base + RXCFG); 1323 /* Light up full duplex LED */ 1324 writel(readl(dev->base + GPIOR) | GPIOR_GP1_OUT, 1325 dev->base + GPIOR); 1326 } else { 1327 /*TODO: set half duplex */ 1328 } 1329 1330 } else { 1331 /*we have copper*/ 1332 /* TODO: Set duplex for copper cards */ 1333 } 1334 printk(KERN_INFO "%s: Duplex set via ethtool\n", 1335 ndev->name); 1336 } 1337 1338 /* Set autonegotiation */ 1339 if (1) { 1340 if (cmd->base.autoneg == AUTONEG_ENABLE) { 1341 /* restart auto negotiation */ 1342 writel(TBICR_MR_AN_ENABLE | TBICR_MR_RESTART_AN, 1343 dev->base + TBICR); 1344 writel(TBICR_MR_AN_ENABLE, dev->base + TBICR); 1345 dev->linkstate = LINK_AUTONEGOTIATE; 1346 1347 printk(KERN_INFO "%s: autoneg enabled via ethtool\n", 1348 ndev->name); 1349 } else { 1350 /* disable auto negotiation */ 1351 writel(0x00000000, dev->base + TBICR); 1352 } 1353 1354 printk(KERN_INFO "%s: autoneg %s via ethtool\n", ndev->name, 1355 cmd->base.autoneg ? "ENABLED" : "DISABLED"); 1356 } 1357 1358 phy_intr(ndev); 1359 spin_unlock(&dev->tx_lock); 1360 spin_unlock_irq(&dev->misc_lock); 1361 1362 return 0; 1363 } 1364 /* end ethtool get/set support -df */ 1365 1366 static void ns83820_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *info) 1367 { 1368 struct ns83820 *dev = PRIV(ndev); 1369 strlcpy(info->driver, "ns83820", sizeof(info->driver)); 1370 strlcpy(info->version, VERSION, sizeof(info->version)); 1371 strlcpy(info->bus_info, pci_name(dev->pci_dev), sizeof(info->bus_info)); 1372 } 1373 1374 static u32 ns83820_get_link(struct net_device *ndev) 1375 { 1376 struct ns83820 *dev = PRIV(ndev); 1377 u32 cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY; 1378 return cfg & CFG_LNKSTS ? 1 : 0; 1379 } 1380 1381 static const struct ethtool_ops ops = { 1382 .get_drvinfo = ns83820_get_drvinfo, 1383 .get_link = ns83820_get_link, 1384 .get_link_ksettings = ns83820_get_link_ksettings, 1385 .set_link_ksettings = ns83820_set_link_ksettings, 1386 }; 1387 1388 static inline void ns83820_disable_interrupts(struct ns83820 *dev) 1389 { 1390 writel(0, dev->base + IMR); 1391 writel(0, dev->base + IER); 1392 readl(dev->base + IER); 1393 } 1394 1395 /* this function is called in irq context from the ISR */ 1396 static void ns83820_mib_isr(struct ns83820 *dev) 1397 { 1398 unsigned long flags; 1399 spin_lock_irqsave(&dev->misc_lock, flags); 1400 ns83820_update_stats(dev); 1401 spin_unlock_irqrestore(&dev->misc_lock, flags); 1402 } 1403 1404 static void ns83820_do_isr(struct net_device *ndev, u32 isr); 1405 static irqreturn_t ns83820_irq(int foo, void *data) 1406 { 1407 struct net_device *ndev = data; 1408 struct ns83820 *dev = PRIV(ndev); 1409 u32 isr; 1410 dprintk("ns83820_irq(%p)\n", ndev); 1411 1412 dev->ihr = 0; 1413 1414 isr = readl(dev->base + ISR); 1415 dprintk("irq: %08x\n", isr); 1416 ns83820_do_isr(ndev, isr); 1417 return IRQ_HANDLED; 1418 } 1419 1420 static void ns83820_do_isr(struct net_device *ndev, u32 isr) 1421 { 1422 struct ns83820 *dev = PRIV(ndev); 1423 unsigned long flags; 1424 1425 #ifdef DEBUG 1426 if (isr & ~(ISR_PHY | ISR_RXDESC | ISR_RXEARLY | ISR_RXOK | ISR_RXERR | ISR_TXIDLE | ISR_TXOK | ISR_TXDESC)) 1427 Dprintk("odd isr? 0x%08x\n", isr); 1428 #endif 1429 1430 if (ISR_RXIDLE & isr) { 1431 dev->rx_info.idle = 1; 1432 Dprintk("oh dear, we are idle\n"); 1433 ns83820_rx_kick(ndev); 1434 } 1435 1436 if ((ISR_RXDESC | ISR_RXOK) & isr) { 1437 prefetch(dev->rx_info.next_rx_desc); 1438 1439 spin_lock_irqsave(&dev->misc_lock, flags); 1440 dev->IMR_cache &= ~(ISR_RXDESC | ISR_RXOK); 1441 writel(dev->IMR_cache, dev->base + IMR); 1442 spin_unlock_irqrestore(&dev->misc_lock, flags); 1443 1444 tasklet_schedule(&dev->rx_tasklet); 1445 //rx_irq(ndev); 1446 //writel(4, dev->base + IHR); 1447 } 1448 1449 if ((ISR_RXIDLE | ISR_RXORN | ISR_RXDESC | ISR_RXOK | ISR_RXERR) & isr) 1450 ns83820_rx_kick(ndev); 1451 1452 if (unlikely(ISR_RXSOVR & isr)) { 1453 //printk("overrun: rxsovr\n"); 1454 ndev->stats.rx_fifo_errors++; 1455 } 1456 1457 if (unlikely(ISR_RXORN & isr)) { 1458 //printk("overrun: rxorn\n"); 1459 ndev->stats.rx_fifo_errors++; 1460 } 1461 1462 if ((ISR_RXRCMP & isr) && dev->rx_info.up) 1463 writel(CR_RXE, dev->base + CR); 1464 1465 if (ISR_TXIDLE & isr) { 1466 u32 txdp; 1467 txdp = readl(dev->base + TXDP); 1468 dprintk("txdp: %08x\n", txdp); 1469 txdp -= dev->tx_phy_descs; 1470 dev->tx_idx = txdp / (DESC_SIZE * 4); 1471 if (dev->tx_idx >= NR_TX_DESC) { 1472 printk(KERN_ALERT "%s: BUG -- txdp out of range\n", ndev->name); 1473 dev->tx_idx = 0; 1474 } 1475 /* The may have been a race between a pci originated read 1476 * and the descriptor update from the cpu. Just in case, 1477 * kick the transmitter if the hardware thinks it is on a 1478 * different descriptor than we are. 1479 */ 1480 if (dev->tx_idx != dev->tx_free_idx) 1481 kick_tx(dev); 1482 } 1483 1484 /* Defer tx ring processing until more than a minimum amount of 1485 * work has accumulated 1486 */ 1487 if ((ISR_TXDESC | ISR_TXIDLE | ISR_TXOK | ISR_TXERR) & isr) { 1488 spin_lock_irqsave(&dev->tx_lock, flags); 1489 do_tx_done(ndev); 1490 spin_unlock_irqrestore(&dev->tx_lock, flags); 1491 1492 /* Disable TxOk if there are no outstanding tx packets. 1493 */ 1494 if ((dev->tx_done_idx == dev->tx_free_idx) && 1495 (dev->IMR_cache & ISR_TXOK)) { 1496 spin_lock_irqsave(&dev->misc_lock, flags); 1497 dev->IMR_cache &= ~ISR_TXOK; 1498 writel(dev->IMR_cache, dev->base + IMR); 1499 spin_unlock_irqrestore(&dev->misc_lock, flags); 1500 } 1501 } 1502 1503 /* The TxIdle interrupt can come in before the transmit has 1504 * completed. Normally we reap packets off of the combination 1505 * of TxDesc and TxIdle and leave TxOk disabled (since it 1506 * occurs on every packet), but when no further irqs of this 1507 * nature are expected, we must enable TxOk. 1508 */ 1509 if ((ISR_TXIDLE & isr) && (dev->tx_done_idx != dev->tx_free_idx)) { 1510 spin_lock_irqsave(&dev->misc_lock, flags); 1511 dev->IMR_cache |= ISR_TXOK; 1512 writel(dev->IMR_cache, dev->base + IMR); 1513 spin_unlock_irqrestore(&dev->misc_lock, flags); 1514 } 1515 1516 /* MIB interrupt: one of the statistics counters is about to overflow */ 1517 if (unlikely(ISR_MIB & isr)) 1518 ns83820_mib_isr(dev); 1519 1520 /* PHY: Link up/down/negotiation state change */ 1521 if (unlikely(ISR_PHY & isr)) 1522 phy_intr(ndev); 1523 1524 #if 0 /* Still working on the interrupt mitigation strategy */ 1525 if (dev->ihr) 1526 writel(dev->ihr, dev->base + IHR); 1527 #endif 1528 } 1529 1530 static void ns83820_do_reset(struct ns83820 *dev, u32 which) 1531 { 1532 Dprintk("resetting chip...\n"); 1533 writel(which, dev->base + CR); 1534 do { 1535 schedule(); 1536 } while (readl(dev->base + CR) & which); 1537 Dprintk("okay!\n"); 1538 } 1539 1540 static int ns83820_stop(struct net_device *ndev) 1541 { 1542 struct ns83820 *dev = PRIV(ndev); 1543 1544 /* FIXME: protect against interrupt handler? */ 1545 del_timer_sync(&dev->tx_watchdog); 1546 1547 ns83820_disable_interrupts(dev); 1548 1549 dev->rx_info.up = 0; 1550 synchronize_irq(dev->pci_dev->irq); 1551 1552 ns83820_do_reset(dev, CR_RST); 1553 1554 synchronize_irq(dev->pci_dev->irq); 1555 1556 spin_lock_irq(&dev->misc_lock); 1557 dev->IMR_cache &= ~(ISR_TXURN | ISR_TXIDLE | ISR_TXERR | ISR_TXDESC | ISR_TXOK); 1558 spin_unlock_irq(&dev->misc_lock); 1559 1560 ns83820_cleanup_rx(dev); 1561 ns83820_cleanup_tx(dev); 1562 1563 return 0; 1564 } 1565 1566 static void ns83820_tx_timeout(struct net_device *ndev) 1567 { 1568 struct ns83820 *dev = PRIV(ndev); 1569 u32 tx_done_idx; 1570 __le32 *desc; 1571 unsigned long flags; 1572 1573 spin_lock_irqsave(&dev->tx_lock, flags); 1574 1575 tx_done_idx = dev->tx_done_idx; 1576 desc = dev->tx_descs + (tx_done_idx * DESC_SIZE); 1577 1578 printk(KERN_INFO "%s: tx_timeout: tx_done_idx=%d free_idx=%d cmdsts=%08x\n", 1579 ndev->name, 1580 tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS])); 1581 1582 #if defined(DEBUG) 1583 { 1584 u32 isr; 1585 isr = readl(dev->base + ISR); 1586 printk("irq: %08x imr: %08x\n", isr, dev->IMR_cache); 1587 ns83820_do_isr(ndev, isr); 1588 } 1589 #endif 1590 1591 do_tx_done(ndev); 1592 1593 tx_done_idx = dev->tx_done_idx; 1594 desc = dev->tx_descs + (tx_done_idx * DESC_SIZE); 1595 1596 printk(KERN_INFO "%s: after: tx_done_idx=%d free_idx=%d cmdsts=%08x\n", 1597 ndev->name, 1598 tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS])); 1599 1600 spin_unlock_irqrestore(&dev->tx_lock, flags); 1601 } 1602 1603 static void ns83820_tx_watch(unsigned long data) 1604 { 1605 struct net_device *ndev = (void *)data; 1606 struct ns83820 *dev = PRIV(ndev); 1607 1608 #if defined(DEBUG) 1609 printk("ns83820_tx_watch: %u %u %d\n", 1610 dev->tx_done_idx, dev->tx_free_idx, atomic_read(&dev->nr_tx_skbs) 1611 ); 1612 #endif 1613 1614 if (time_after(jiffies, dev_trans_start(ndev) + 1*HZ) && 1615 dev->tx_done_idx != dev->tx_free_idx) { 1616 printk(KERN_DEBUG "%s: ns83820_tx_watch: %u %u %d\n", 1617 ndev->name, 1618 dev->tx_done_idx, dev->tx_free_idx, 1619 atomic_read(&dev->nr_tx_skbs)); 1620 ns83820_tx_timeout(ndev); 1621 } 1622 1623 mod_timer(&dev->tx_watchdog, jiffies + 2*HZ); 1624 } 1625 1626 static int ns83820_open(struct net_device *ndev) 1627 { 1628 struct ns83820 *dev = PRIV(ndev); 1629 unsigned i; 1630 u32 desc; 1631 int ret; 1632 1633 dprintk("ns83820_open\n"); 1634 1635 writel(0, dev->base + PQCR); 1636 1637 ret = ns83820_setup_rx(ndev); 1638 if (ret) 1639 goto failed; 1640 1641 memset(dev->tx_descs, 0, 4 * NR_TX_DESC * DESC_SIZE); 1642 for (i=0; i<NR_TX_DESC; i++) { 1643 dev->tx_descs[(i * DESC_SIZE) + DESC_LINK] 1644 = cpu_to_le32( 1645 dev->tx_phy_descs 1646 + ((i+1) % NR_TX_DESC) * DESC_SIZE * 4); 1647 } 1648 1649 dev->tx_idx = 0; 1650 dev->tx_done_idx = 0; 1651 desc = dev->tx_phy_descs; 1652 writel(0, dev->base + TXDP_HI); 1653 writel(desc, dev->base + TXDP); 1654 1655 init_timer(&dev->tx_watchdog); 1656 dev->tx_watchdog.data = (unsigned long)ndev; 1657 dev->tx_watchdog.function = ns83820_tx_watch; 1658 mod_timer(&dev->tx_watchdog, jiffies + 2*HZ); 1659 1660 netif_start_queue(ndev); /* FIXME: wait for phy to come up */ 1661 1662 return 0; 1663 1664 failed: 1665 ns83820_stop(ndev); 1666 return ret; 1667 } 1668 1669 static void ns83820_getmac(struct ns83820 *dev, u8 *mac) 1670 { 1671 unsigned i; 1672 for (i=0; i<3; i++) { 1673 u32 data; 1674 1675 /* Read from the perfect match memory: this is loaded by 1676 * the chip from the EEPROM via the EELOAD self test. 1677 */ 1678 writel(i*2, dev->base + RFCR); 1679 data = readl(dev->base + RFDR); 1680 1681 *mac++ = data; 1682 *mac++ = data >> 8; 1683 } 1684 } 1685 1686 static void ns83820_set_multicast(struct net_device *ndev) 1687 { 1688 struct ns83820 *dev = PRIV(ndev); 1689 u8 __iomem *rfcr = dev->base + RFCR; 1690 u32 and_mask = 0xffffffff; 1691 u32 or_mask = 0; 1692 u32 val; 1693 1694 if (ndev->flags & IFF_PROMISC) 1695 or_mask |= RFCR_AAU | RFCR_AAM; 1696 else 1697 and_mask &= ~(RFCR_AAU | RFCR_AAM); 1698 1699 if (ndev->flags & IFF_ALLMULTI || netdev_mc_count(ndev)) 1700 or_mask |= RFCR_AAM; 1701 else 1702 and_mask &= ~RFCR_AAM; 1703 1704 spin_lock_irq(&dev->misc_lock); 1705 val = (readl(rfcr) & and_mask) | or_mask; 1706 /* Ramit : RFCR Write Fix doc says RFEN must be 0 modify other bits */ 1707 writel(val & ~RFCR_RFEN, rfcr); 1708 writel(val, rfcr); 1709 spin_unlock_irq(&dev->misc_lock); 1710 } 1711 1712 static void ns83820_run_bist(struct net_device *ndev, const char *name, u32 enable, u32 done, u32 fail) 1713 { 1714 struct ns83820 *dev = PRIV(ndev); 1715 int timed_out = 0; 1716 unsigned long start; 1717 u32 status; 1718 int loops = 0; 1719 1720 dprintk("%s: start %s\n", ndev->name, name); 1721 1722 start = jiffies; 1723 1724 writel(enable, dev->base + PTSCR); 1725 for (;;) { 1726 loops++; 1727 status = readl(dev->base + PTSCR); 1728 if (!(status & enable)) 1729 break; 1730 if (status & done) 1731 break; 1732 if (status & fail) 1733 break; 1734 if (time_after_eq(jiffies, start + HZ)) { 1735 timed_out = 1; 1736 break; 1737 } 1738 schedule_timeout_uninterruptible(1); 1739 } 1740 1741 if (status & fail) 1742 printk(KERN_INFO "%s: %s failed! (0x%08x & 0x%08x)\n", 1743 ndev->name, name, status, fail); 1744 else if (timed_out) 1745 printk(KERN_INFO "%s: run_bist %s timed out! (%08x)\n", 1746 ndev->name, name, status); 1747 1748 dprintk("%s: done %s in %d loops\n", ndev->name, name, loops); 1749 } 1750 1751 #ifdef PHY_CODE_IS_FINISHED 1752 static void ns83820_mii_write_bit(struct ns83820 *dev, int bit) 1753 { 1754 /* drive MDC low */ 1755 dev->MEAR_cache &= ~MEAR_MDC; 1756 writel(dev->MEAR_cache, dev->base + MEAR); 1757 readl(dev->base + MEAR); 1758 1759 /* enable output, set bit */ 1760 dev->MEAR_cache |= MEAR_MDDIR; 1761 if (bit) 1762 dev->MEAR_cache |= MEAR_MDIO; 1763 else 1764 dev->MEAR_cache &= ~MEAR_MDIO; 1765 1766 /* set the output bit */ 1767 writel(dev->MEAR_cache, dev->base + MEAR); 1768 readl(dev->base + MEAR); 1769 1770 /* Wait. Max clock rate is 2.5MHz, this way we come in under 1MHz */ 1771 udelay(1); 1772 1773 /* drive MDC high causing the data bit to be latched */ 1774 dev->MEAR_cache |= MEAR_MDC; 1775 writel(dev->MEAR_cache, dev->base + MEAR); 1776 readl(dev->base + MEAR); 1777 1778 /* Wait again... */ 1779 udelay(1); 1780 } 1781 1782 static int ns83820_mii_read_bit(struct ns83820 *dev) 1783 { 1784 int bit; 1785 1786 /* drive MDC low, disable output */ 1787 dev->MEAR_cache &= ~MEAR_MDC; 1788 dev->MEAR_cache &= ~MEAR_MDDIR; 1789 writel(dev->MEAR_cache, dev->base + MEAR); 1790 readl(dev->base + MEAR); 1791 1792 /* Wait. Max clock rate is 2.5MHz, this way we come in under 1MHz */ 1793 udelay(1); 1794 1795 /* drive MDC high causing the data bit to be latched */ 1796 bit = (readl(dev->base + MEAR) & MEAR_MDIO) ? 1 : 0; 1797 dev->MEAR_cache |= MEAR_MDC; 1798 writel(dev->MEAR_cache, dev->base + MEAR); 1799 1800 /* Wait again... */ 1801 udelay(1); 1802 1803 return bit; 1804 } 1805 1806 static unsigned ns83820_mii_read_reg(struct ns83820 *dev, unsigned phy, unsigned reg) 1807 { 1808 unsigned data = 0; 1809 int i; 1810 1811 /* read some garbage so that we eventually sync up */ 1812 for (i=0; i<64; i++) 1813 ns83820_mii_read_bit(dev); 1814 1815 ns83820_mii_write_bit(dev, 0); /* start */ 1816 ns83820_mii_write_bit(dev, 1); 1817 ns83820_mii_write_bit(dev, 1); /* opcode read */ 1818 ns83820_mii_write_bit(dev, 0); 1819 1820 /* write out the phy address: 5 bits, msb first */ 1821 for (i=0; i<5; i++) 1822 ns83820_mii_write_bit(dev, phy & (0x10 >> i)); 1823 1824 /* write out the register address, 5 bits, msb first */ 1825 for (i=0; i<5; i++) 1826 ns83820_mii_write_bit(dev, reg & (0x10 >> i)); 1827 1828 ns83820_mii_read_bit(dev); /* turn around cycles */ 1829 ns83820_mii_read_bit(dev); 1830 1831 /* read in the register data, 16 bits msb first */ 1832 for (i=0; i<16; i++) { 1833 data <<= 1; 1834 data |= ns83820_mii_read_bit(dev); 1835 } 1836 1837 return data; 1838 } 1839 1840 static unsigned ns83820_mii_write_reg(struct ns83820 *dev, unsigned phy, unsigned reg, unsigned data) 1841 { 1842 int i; 1843 1844 /* read some garbage so that we eventually sync up */ 1845 for (i=0; i<64; i++) 1846 ns83820_mii_read_bit(dev); 1847 1848 ns83820_mii_write_bit(dev, 0); /* start */ 1849 ns83820_mii_write_bit(dev, 1); 1850 ns83820_mii_write_bit(dev, 0); /* opcode read */ 1851 ns83820_mii_write_bit(dev, 1); 1852 1853 /* write out the phy address: 5 bits, msb first */ 1854 for (i=0; i<5; i++) 1855 ns83820_mii_write_bit(dev, phy & (0x10 >> i)); 1856 1857 /* write out the register address, 5 bits, msb first */ 1858 for (i=0; i<5; i++) 1859 ns83820_mii_write_bit(dev, reg & (0x10 >> i)); 1860 1861 ns83820_mii_read_bit(dev); /* turn around cycles */ 1862 ns83820_mii_read_bit(dev); 1863 1864 /* read in the register data, 16 bits msb first */ 1865 for (i=0; i<16; i++) 1866 ns83820_mii_write_bit(dev, (data >> (15 - i)) & 1); 1867 1868 return data; 1869 } 1870 1871 static void ns83820_probe_phy(struct net_device *ndev) 1872 { 1873 struct ns83820 *dev = PRIV(ndev); 1874 static int first; 1875 int i; 1876 #define MII_PHYIDR1 0x02 1877 #define MII_PHYIDR2 0x03 1878 1879 #if 0 1880 if (!first) { 1881 unsigned tmp; 1882 ns83820_mii_read_reg(dev, 1, 0x09); 1883 ns83820_mii_write_reg(dev, 1, 0x10, 0x0d3e); 1884 1885 tmp = ns83820_mii_read_reg(dev, 1, 0x00); 1886 ns83820_mii_write_reg(dev, 1, 0x00, tmp | 0x8000); 1887 udelay(1300); 1888 ns83820_mii_read_reg(dev, 1, 0x09); 1889 } 1890 #endif 1891 first = 1; 1892 1893 for (i=1; i<2; i++) { 1894 int j; 1895 unsigned a, b; 1896 a = ns83820_mii_read_reg(dev, i, MII_PHYIDR1); 1897 b = ns83820_mii_read_reg(dev, i, MII_PHYIDR2); 1898 1899 //printk("%s: phy %d: 0x%04x 0x%04x\n", 1900 // ndev->name, i, a, b); 1901 1902 for (j=0; j<0x16; j+=4) { 1903 dprintk("%s: [0x%02x] %04x %04x %04x %04x\n", 1904 ndev->name, j, 1905 ns83820_mii_read_reg(dev, i, 0 + j), 1906 ns83820_mii_read_reg(dev, i, 1 + j), 1907 ns83820_mii_read_reg(dev, i, 2 + j), 1908 ns83820_mii_read_reg(dev, i, 3 + j) 1909 ); 1910 } 1911 } 1912 { 1913 unsigned a, b; 1914 /* read firmware version: memory addr is 0x8402 and 0x8403 */ 1915 ns83820_mii_write_reg(dev, 1, 0x16, 0x000d); 1916 ns83820_mii_write_reg(dev, 1, 0x1e, 0x810e); 1917 a = ns83820_mii_read_reg(dev, 1, 0x1d); 1918 1919 ns83820_mii_write_reg(dev, 1, 0x16, 0x000d); 1920 ns83820_mii_write_reg(dev, 1, 0x1e, 0x810e); 1921 b = ns83820_mii_read_reg(dev, 1, 0x1d); 1922 dprintk("version: 0x%04x 0x%04x\n", a, b); 1923 } 1924 } 1925 #endif 1926 1927 static const struct net_device_ops netdev_ops = { 1928 .ndo_open = ns83820_open, 1929 .ndo_stop = ns83820_stop, 1930 .ndo_start_xmit = ns83820_hard_start_xmit, 1931 .ndo_get_stats = ns83820_get_stats, 1932 .ndo_set_rx_mode = ns83820_set_multicast, 1933 .ndo_validate_addr = eth_validate_addr, 1934 .ndo_set_mac_address = eth_mac_addr, 1935 .ndo_tx_timeout = ns83820_tx_timeout, 1936 }; 1937 1938 static int ns83820_init_one(struct pci_dev *pci_dev, 1939 const struct pci_device_id *id) 1940 { 1941 struct net_device *ndev; 1942 struct ns83820 *dev; 1943 long addr; 1944 int err; 1945 int using_dac = 0; 1946 1947 /* See if we can set the dma mask early on; failure is fatal. */ 1948 if (sizeof(dma_addr_t) == 8 && 1949 !pci_set_dma_mask(pci_dev, DMA_BIT_MASK(64))) { 1950 using_dac = 1; 1951 } else if (!pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32))) { 1952 using_dac = 0; 1953 } else { 1954 dev_warn(&pci_dev->dev, "pci_set_dma_mask failed!\n"); 1955 return -ENODEV; 1956 } 1957 1958 ndev = alloc_etherdev(sizeof(struct ns83820)); 1959 err = -ENOMEM; 1960 if (!ndev) 1961 goto out; 1962 1963 dev = PRIV(ndev); 1964 dev->ndev = ndev; 1965 1966 spin_lock_init(&dev->rx_info.lock); 1967 spin_lock_init(&dev->tx_lock); 1968 spin_lock_init(&dev->misc_lock); 1969 dev->pci_dev = pci_dev; 1970 1971 SET_NETDEV_DEV(ndev, &pci_dev->dev); 1972 1973 INIT_WORK(&dev->tq_refill, queue_refill); 1974 tasklet_init(&dev->rx_tasklet, rx_action, (unsigned long)ndev); 1975 1976 err = pci_enable_device(pci_dev); 1977 if (err) { 1978 dev_info(&pci_dev->dev, "pci_enable_dev failed: %d\n", err); 1979 goto out_free; 1980 } 1981 1982 pci_set_master(pci_dev); 1983 addr = pci_resource_start(pci_dev, 1); 1984 dev->base = ioremap_nocache(addr, PAGE_SIZE); 1985 dev->tx_descs = pci_alloc_consistent(pci_dev, 1986 4 * DESC_SIZE * NR_TX_DESC, &dev->tx_phy_descs); 1987 dev->rx_info.descs = pci_alloc_consistent(pci_dev, 1988 4 * DESC_SIZE * NR_RX_DESC, &dev->rx_info.phy_descs); 1989 err = -ENOMEM; 1990 if (!dev->base || !dev->tx_descs || !dev->rx_info.descs) 1991 goto out_disable; 1992 1993 dprintk("%p: %08lx %p: %08lx\n", 1994 dev->tx_descs, (long)dev->tx_phy_descs, 1995 dev->rx_info.descs, (long)dev->rx_info.phy_descs); 1996 1997 ns83820_disable_interrupts(dev); 1998 1999 dev->IMR_cache = 0; 2000 2001 err = request_irq(pci_dev->irq, ns83820_irq, IRQF_SHARED, 2002 DRV_NAME, ndev); 2003 if (err) { 2004 dev_info(&pci_dev->dev, "unable to register irq %d, err %d\n", 2005 pci_dev->irq, err); 2006 goto out_disable; 2007 } 2008 2009 /* 2010 * FIXME: we are holding rtnl_lock() over obscenely long area only 2011 * because some of the setup code uses dev->name. It's Wrong(tm) - 2012 * we should be using driver-specific names for all that stuff. 2013 * For now that will do, but we really need to come back and kill 2014 * most of the dev_alloc_name() users later. 2015 */ 2016 rtnl_lock(); 2017 err = dev_alloc_name(ndev, ndev->name); 2018 if (err < 0) { 2019 dev_info(&pci_dev->dev, "unable to get netdev name: %d\n", err); 2020 goto out_free_irq; 2021 } 2022 2023 printk("%s: ns83820.c: 0x22c: %08x, subsystem: %04x:%04x\n", 2024 ndev->name, le32_to_cpu(readl(dev->base + 0x22c)), 2025 pci_dev->subsystem_vendor, pci_dev->subsystem_device); 2026 2027 ndev->netdev_ops = &netdev_ops; 2028 ndev->ethtool_ops = &ops; 2029 ndev->watchdog_timeo = 5 * HZ; 2030 pci_set_drvdata(pci_dev, ndev); 2031 2032 ns83820_do_reset(dev, CR_RST); 2033 2034 /* Must reset the ram bist before running it */ 2035 writel(PTSCR_RBIST_RST, dev->base + PTSCR); 2036 ns83820_run_bist(ndev, "sram bist", PTSCR_RBIST_EN, 2037 PTSCR_RBIST_DONE, PTSCR_RBIST_FAIL); 2038 ns83820_run_bist(ndev, "eeprom bist", PTSCR_EEBIST_EN, 0, 2039 PTSCR_EEBIST_FAIL); 2040 ns83820_run_bist(ndev, "eeprom load", PTSCR_EELOAD_EN, 0, 0); 2041 2042 /* I love config registers */ 2043 dev->CFG_cache = readl(dev->base + CFG); 2044 2045 if ((dev->CFG_cache & CFG_PCI64_DET)) { 2046 printk(KERN_INFO "%s: detected 64 bit PCI data bus.\n", 2047 ndev->name); 2048 /*dev->CFG_cache |= CFG_DATA64_EN;*/ 2049 if (!(dev->CFG_cache & CFG_DATA64_EN)) 2050 printk(KERN_INFO "%s: EEPROM did not enable 64 bit bus. Disabled.\n", 2051 ndev->name); 2052 } else 2053 dev->CFG_cache &= ~(CFG_DATA64_EN); 2054 2055 dev->CFG_cache &= (CFG_TBI_EN | CFG_MRM_DIS | CFG_MWI_DIS | 2056 CFG_T64ADDR | CFG_DATA64_EN | CFG_EXT_125 | 2057 CFG_M64ADDR); 2058 dev->CFG_cache |= CFG_PINT_DUPSTS | CFG_PINT_LNKSTS | CFG_PINT_SPDSTS | 2059 CFG_EXTSTS_EN | CFG_EXD | CFG_PESEL; 2060 dev->CFG_cache |= CFG_REQALG; 2061 dev->CFG_cache |= CFG_POW; 2062 dev->CFG_cache |= CFG_TMRTEST; 2063 2064 /* When compiled with 64 bit addressing, we must always enable 2065 * the 64 bit descriptor format. 2066 */ 2067 if (sizeof(dma_addr_t) == 8) 2068 dev->CFG_cache |= CFG_M64ADDR; 2069 if (using_dac) 2070 dev->CFG_cache |= CFG_T64ADDR; 2071 2072 /* Big endian mode does not seem to do what the docs suggest */ 2073 dev->CFG_cache &= ~CFG_BEM; 2074 2075 /* setup optical transceiver if we have one */ 2076 if (dev->CFG_cache & CFG_TBI_EN) { 2077 printk(KERN_INFO "%s: enabling optical transceiver\n", 2078 ndev->name); 2079 writel(readl(dev->base + GPIOR) | 0x3e8, dev->base + GPIOR); 2080 2081 /* setup auto negotiation feature advertisement */ 2082 writel(readl(dev->base + TANAR) 2083 | TANAR_HALF_DUP | TANAR_FULL_DUP, 2084 dev->base + TANAR); 2085 2086 /* start auto negotiation */ 2087 writel(TBICR_MR_AN_ENABLE | TBICR_MR_RESTART_AN, 2088 dev->base + TBICR); 2089 writel(TBICR_MR_AN_ENABLE, dev->base + TBICR); 2090 dev->linkstate = LINK_AUTONEGOTIATE; 2091 2092 dev->CFG_cache |= CFG_MODE_1000; 2093 } 2094 2095 writel(dev->CFG_cache, dev->base + CFG); 2096 dprintk("CFG: %08x\n", dev->CFG_cache); 2097 2098 if (reset_phy) { 2099 printk(KERN_INFO "%s: resetting phy\n", ndev->name); 2100 writel(dev->CFG_cache | CFG_PHY_RST, dev->base + CFG); 2101 msleep(10); 2102 writel(dev->CFG_cache, dev->base + CFG); 2103 } 2104 2105 #if 0 /* Huh? This sets the PCI latency register. Should be done via 2106 * the PCI layer. FIXME. 2107 */ 2108 if (readl(dev->base + SRR)) 2109 writel(readl(dev->base+0x20c) | 0xfe00, dev->base + 0x20c); 2110 #endif 2111 2112 /* Note! The DMA burst size interacts with packet 2113 * transmission, such that the largest packet that 2114 * can be transmitted is 8192 - FLTH - burst size. 2115 * If only the transmit fifo was larger... 2116 */ 2117 /* Ramit : 1024 DMA is not a good idea, it ends up banging 2118 * some DELL and COMPAQ SMP systems */ 2119 writel(TXCFG_CSI | TXCFG_HBI | TXCFG_ATP | TXCFG_MXDMA512 2120 | ((1600 / 32) * 0x100), 2121 dev->base + TXCFG); 2122 2123 /* Flush the interrupt holdoff timer */ 2124 writel(0x000, dev->base + IHR); 2125 writel(0x100, dev->base + IHR); 2126 writel(0x000, dev->base + IHR); 2127 2128 /* Set Rx to full duplex, don't accept runt, errored, long or length 2129 * range errored packets. Use 512 byte DMA. 2130 */ 2131 /* Ramit : 1024 DMA is not a good idea, it ends up banging 2132 * some DELL and COMPAQ SMP systems 2133 * Turn on ALP, only we are accpeting Jumbo Packets */ 2134 writel(RXCFG_AEP | RXCFG_ARP | RXCFG_AIRL | RXCFG_RX_FD 2135 | RXCFG_STRIPCRC 2136 //| RXCFG_ALP 2137 | (RXCFG_MXDMA512) | 0, dev->base + RXCFG); 2138 2139 /* Disable priority queueing */ 2140 writel(0, dev->base + PQCR); 2141 2142 /* Enable IP checksum validation and detetion of VLAN headers. 2143 * Note: do not set the reject options as at least the 0x102 2144 * revision of the chip does not properly accept IP fragments 2145 * at least for UDP. 2146 */ 2147 /* Ramit : Be sure to turn on RXCFG_ARP if VLAN's are enabled, since 2148 * the MAC it calculates the packetsize AFTER stripping the VLAN 2149 * header, and if a VLAN Tagged packet of 64 bytes is received (like 2150 * a ping with a VLAN header) then the card, strips the 4 byte VLAN 2151 * tag and then checks the packet size, so if RXCFG_ARP is not enabled, 2152 * it discrards it!. These guys...... 2153 * also turn on tag stripping if hardware acceleration is enabled 2154 */ 2155 #ifdef NS83820_VLAN_ACCEL_SUPPORT 2156 #define VRCR_INIT_VALUE (VRCR_IPEN|VRCR_VTDEN|VRCR_VTREN) 2157 #else 2158 #define VRCR_INIT_VALUE (VRCR_IPEN|VRCR_VTDEN) 2159 #endif 2160 writel(VRCR_INIT_VALUE, dev->base + VRCR); 2161 2162 /* Enable per-packet TCP/UDP/IP checksumming 2163 * and per packet vlan tag insertion if 2164 * vlan hardware acceleration is enabled 2165 */ 2166 #ifdef NS83820_VLAN_ACCEL_SUPPORT 2167 #define VTCR_INIT_VALUE (VTCR_PPCHK|VTCR_VPPTI) 2168 #else 2169 #define VTCR_INIT_VALUE VTCR_PPCHK 2170 #endif 2171 writel(VTCR_INIT_VALUE, dev->base + VTCR); 2172 2173 /* Ramit : Enable async and sync pause frames */ 2174 /* writel(0, dev->base + PCR); */ 2175 writel((PCR_PS_MCAST | PCR_PS_DA | PCR_PSEN | PCR_FFLO_4K | 2176 PCR_FFHI_8K | PCR_STLO_4 | PCR_STHI_8 | PCR_PAUSE_CNT), 2177 dev->base + PCR); 2178 2179 /* Disable Wake On Lan */ 2180 writel(0, dev->base + WCSR); 2181 2182 ns83820_getmac(dev, ndev->dev_addr); 2183 2184 /* Yes, we support dumb IP checksum on transmit */ 2185 ndev->features |= NETIF_F_SG; 2186 ndev->features |= NETIF_F_IP_CSUM; 2187 2188 ndev->min_mtu = 0; 2189 2190 #ifdef NS83820_VLAN_ACCEL_SUPPORT 2191 /* We also support hardware vlan acceleration */ 2192 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX; 2193 #endif 2194 2195 if (using_dac) { 2196 printk(KERN_INFO "%s: using 64 bit addressing.\n", 2197 ndev->name); 2198 ndev->features |= NETIF_F_HIGHDMA; 2199 } 2200 2201 printk(KERN_INFO "%s: ns83820 v" VERSION ": DP83820 v%u.%u: %pM io=0x%08lx irq=%d f=%s\n", 2202 ndev->name, 2203 (unsigned)readl(dev->base + SRR) >> 8, 2204 (unsigned)readl(dev->base + SRR) & 0xff, 2205 ndev->dev_addr, addr, pci_dev->irq, 2206 (ndev->features & NETIF_F_HIGHDMA) ? "h,sg" : "sg" 2207 ); 2208 2209 #ifdef PHY_CODE_IS_FINISHED 2210 ns83820_probe_phy(ndev); 2211 #endif 2212 2213 err = register_netdevice(ndev); 2214 if (err) { 2215 printk(KERN_INFO "ns83820: unable to register netdev: %d\n", err); 2216 goto out_cleanup; 2217 } 2218 rtnl_unlock(); 2219 2220 return 0; 2221 2222 out_cleanup: 2223 ns83820_disable_interrupts(dev); /* paranoia */ 2224 out_free_irq: 2225 rtnl_unlock(); 2226 free_irq(pci_dev->irq, ndev); 2227 out_disable: 2228 if (dev->base) 2229 iounmap(dev->base); 2230 pci_free_consistent(pci_dev, 4 * DESC_SIZE * NR_TX_DESC, dev->tx_descs, dev->tx_phy_descs); 2231 pci_free_consistent(pci_dev, 4 * DESC_SIZE * NR_RX_DESC, dev->rx_info.descs, dev->rx_info.phy_descs); 2232 pci_disable_device(pci_dev); 2233 out_free: 2234 free_netdev(ndev); 2235 out: 2236 return err; 2237 } 2238 2239 static void ns83820_remove_one(struct pci_dev *pci_dev) 2240 { 2241 struct net_device *ndev = pci_get_drvdata(pci_dev); 2242 struct ns83820 *dev = PRIV(ndev); /* ok even if NULL */ 2243 2244 if (!ndev) /* paranoia */ 2245 return; 2246 2247 ns83820_disable_interrupts(dev); /* paranoia */ 2248 2249 unregister_netdev(ndev); 2250 free_irq(dev->pci_dev->irq, ndev); 2251 iounmap(dev->base); 2252 pci_free_consistent(dev->pci_dev, 4 * DESC_SIZE * NR_TX_DESC, 2253 dev->tx_descs, dev->tx_phy_descs); 2254 pci_free_consistent(dev->pci_dev, 4 * DESC_SIZE * NR_RX_DESC, 2255 dev->rx_info.descs, dev->rx_info.phy_descs); 2256 pci_disable_device(dev->pci_dev); 2257 free_netdev(ndev); 2258 } 2259 2260 static const struct pci_device_id ns83820_pci_tbl[] = { 2261 { 0x100b, 0x0022, PCI_ANY_ID, PCI_ANY_ID, 0, .driver_data = 0, }, 2262 { 0, }, 2263 }; 2264 2265 static struct pci_driver driver = { 2266 .name = "ns83820", 2267 .id_table = ns83820_pci_tbl, 2268 .probe = ns83820_init_one, 2269 .remove = ns83820_remove_one, 2270 #if 0 /* FIXME: implement */ 2271 .suspend = , 2272 .resume = , 2273 #endif 2274 }; 2275 2276 2277 static int __init ns83820_init(void) 2278 { 2279 printk(KERN_INFO "ns83820.c: National Semiconductor DP83820 10/100/1000 driver.\n"); 2280 return pci_register_driver(&driver); 2281 } 2282 2283 static void __exit ns83820_exit(void) 2284 { 2285 pci_unregister_driver(&driver); 2286 } 2287 2288 MODULE_AUTHOR("Benjamin LaHaise <bcrl@kvack.org>"); 2289 MODULE_DESCRIPTION("National Semiconductor DP83820 10/100/1000 driver"); 2290 MODULE_LICENSE("GPL"); 2291 2292 MODULE_DEVICE_TABLE(pci, ns83820_pci_tbl); 2293 2294 module_param(lnksts, int, 0); 2295 MODULE_PARM_DESC(lnksts, "Polarity of LNKSTS bit"); 2296 2297 module_param(ihr, int, 0); 2298 MODULE_PARM_DESC(ihr, "Time in 100 us increments to delay interrupts (range 0-127)"); 2299 2300 module_param(reset_phy, int, 0); 2301 MODULE_PARM_DESC(reset_phy, "Set to 1 to reset the PHY on startup"); 2302 2303 module_init(ns83820_init); 2304 module_exit(ns83820_exit); 2305