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