pcnet32.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987
  1. /* pcnet32.c: An AMD PCnet32 ethernet driver for linux. */
  2. /*
  3. * Copyright 1996-1999 Thomas Bogendoerfer
  4. *
  5. * Derived from the lance driver written 1993,1994,1995 by Donald Becker.
  6. *
  7. * Copyright 1993 United States Government as represented by the
  8. * Director, National Security Agency.
  9. *
  10. * This software may be used and distributed according to the terms
  11. * of the GNU General Public License, incorporated herein by reference.
  12. *
  13. * This driver is for PCnet32 and PCnetPCI based ethercards
  14. */
  15. /**************************************************************************
  16. * 23 Oct, 2000.
  17. * Fixed a few bugs, related to running the controller in 32bit mode.
  18. *
  19. * Carsten Langgaard, carstenl@mips.com
  20. * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
  21. *
  22. *************************************************************************/
  23. #define DRV_NAME "pcnet32"
  24. #define DRV_VERSION "1.32"
  25. #define DRV_RELDATE "18.Mar.2006"
  26. #define PFX DRV_NAME ": "
  27. static const char *const version =
  28. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " tsbogend@alpha.franken.de\n";
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/string.h>
  32. #include <linux/errno.h>
  33. #include <linux/ioport.h>
  34. #include <linux/slab.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/pci.h>
  37. #include <linux/delay.h>
  38. #include <linux/init.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/mii.h>
  41. #include <linux/crc32.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/etherdevice.h>
  44. #include <linux/skbuff.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/bitops.h>
  48. #include <asm/dma.h>
  49. #include <asm/io.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/irq.h>
  52. /*
  53. * PCI device identifiers for "new style" Linux PCI Device Drivers
  54. */
  55. static struct pci_device_id pcnet32_pci_tbl[] = {
  56. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE_HOME), },
  57. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE), },
  58. /*
  59. * Adapters that were sold with IBM's RS/6000 or pSeries hardware have
  60. * the incorrect vendor id.
  61. */
  62. { PCI_DEVICE(PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_AMD_LANCE),
  63. .class = (PCI_CLASS_NETWORK_ETHERNET << 8), .class_mask = 0xffff00, },
  64. { } /* terminate list */
  65. };
  66. MODULE_DEVICE_TABLE(pci, pcnet32_pci_tbl);
  67. static int cards_found;
  68. /*
  69. * VLB I/O addresses
  70. */
  71. static unsigned int pcnet32_portlist[] __initdata =
  72. { 0x300, 0x320, 0x340, 0x360, 0 };
  73. static int pcnet32_debug = 0;
  74. static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
  75. static int pcnet32vlb; /* check for VLB cards ? */
  76. static struct net_device *pcnet32_dev;
  77. static int max_interrupt_work = 2;
  78. static int rx_copybreak = 200;
  79. #define PCNET32_PORT_AUI 0x00
  80. #define PCNET32_PORT_10BT 0x01
  81. #define PCNET32_PORT_GPSI 0x02
  82. #define PCNET32_PORT_MII 0x03
  83. #define PCNET32_PORT_PORTSEL 0x03
  84. #define PCNET32_PORT_ASEL 0x04
  85. #define PCNET32_PORT_100 0x40
  86. #define PCNET32_PORT_FD 0x80
  87. #define PCNET32_DMA_MASK 0xffffffff
  88. #define PCNET32_WATCHDOG_TIMEOUT (jiffies + (2 * HZ))
  89. #define PCNET32_BLINK_TIMEOUT (jiffies + (HZ/4))
  90. /*
  91. * table to translate option values from tulip
  92. * to internal options
  93. */
  94. static const unsigned char options_mapping[] = {
  95. PCNET32_PORT_ASEL, /* 0 Auto-select */
  96. PCNET32_PORT_AUI, /* 1 BNC/AUI */
  97. PCNET32_PORT_AUI, /* 2 AUI/BNC */
  98. PCNET32_PORT_ASEL, /* 3 not supported */
  99. PCNET32_PORT_10BT | PCNET32_PORT_FD, /* 4 10baseT-FD */
  100. PCNET32_PORT_ASEL, /* 5 not supported */
  101. PCNET32_PORT_ASEL, /* 6 not supported */
  102. PCNET32_PORT_ASEL, /* 7 not supported */
  103. PCNET32_PORT_ASEL, /* 8 not supported */
  104. PCNET32_PORT_MII, /* 9 MII 10baseT */
  105. PCNET32_PORT_MII | PCNET32_PORT_FD, /* 10 MII 10baseT-FD */
  106. PCNET32_PORT_MII, /* 11 MII (autosel) */
  107. PCNET32_PORT_10BT, /* 12 10BaseT */
  108. PCNET32_PORT_MII | PCNET32_PORT_100, /* 13 MII 100BaseTx */
  109. /* 14 MII 100BaseTx-FD */
  110. PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD,
  111. PCNET32_PORT_ASEL /* 15 not supported */
  112. };
  113. static const char pcnet32_gstrings_test[][ETH_GSTRING_LEN] = {
  114. "Loopback test (offline)"
  115. };
  116. #define PCNET32_TEST_LEN (sizeof(pcnet32_gstrings_test) / ETH_GSTRING_LEN)
  117. #define PCNET32_NUM_REGS 136
  118. #define MAX_UNITS 8 /* More are supported, limit only on options */
  119. static int options[MAX_UNITS];
  120. static int full_duplex[MAX_UNITS];
  121. static int homepna[MAX_UNITS];
  122. /*
  123. * Theory of Operation
  124. *
  125. * This driver uses the same software structure as the normal lance
  126. * driver. So look for a verbose description in lance.c. The differences
  127. * to the normal lance driver is the use of the 32bit mode of PCnet32
  128. * and PCnetPCI chips. Because these chips are 32bit chips, there is no
  129. * 16MB limitation and we don't need bounce buffers.
  130. */
  131. /*
  132. * Set the number of Tx and Rx buffers, using Log_2(# buffers).
  133. * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
  134. * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
  135. */
  136. #ifndef PCNET32_LOG_TX_BUFFERS
  137. #define PCNET32_LOG_TX_BUFFERS 4
  138. #define PCNET32_LOG_RX_BUFFERS 5
  139. #define PCNET32_LOG_MAX_TX_BUFFERS 9 /* 2^9 == 512 */
  140. #define PCNET32_LOG_MAX_RX_BUFFERS 9
  141. #endif
  142. #define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
  143. #define TX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_TX_BUFFERS))
  144. #define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
  145. #define RX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_RX_BUFFERS))
  146. #define PKT_BUF_SZ 1544
  147. /* Offsets from base I/O address. */
  148. #define PCNET32_WIO_RDP 0x10
  149. #define PCNET32_WIO_RAP 0x12
  150. #define PCNET32_WIO_RESET 0x14
  151. #define PCNET32_WIO_BDP 0x16
  152. #define PCNET32_DWIO_RDP 0x10
  153. #define PCNET32_DWIO_RAP 0x14
  154. #define PCNET32_DWIO_RESET 0x18
  155. #define PCNET32_DWIO_BDP 0x1C
  156. #define PCNET32_TOTAL_SIZE 0x20
  157. #define CSR0 0
  158. #define CSR0_INIT 0x1
  159. #define CSR0_START 0x2
  160. #define CSR0_STOP 0x4
  161. #define CSR0_TXPOLL 0x8
  162. #define CSR0_INTEN 0x40
  163. #define CSR0_IDON 0x0100
  164. #define CSR0_NORMAL (CSR0_START | CSR0_INTEN)
  165. #define PCNET32_INIT_LOW 1
  166. #define PCNET32_INIT_HIGH 2
  167. #define CSR3 3
  168. #define CSR4 4
  169. #define CSR5 5
  170. #define CSR5_SUSPEND 0x0001
  171. #define CSR15 15
  172. #define PCNET32_MC_FILTER 8
  173. /* The PCNET32 Rx and Tx ring descriptors. */
  174. struct pcnet32_rx_head {
  175. u32 base;
  176. s16 buf_length;
  177. s16 status;
  178. u32 msg_length;
  179. u32 reserved;
  180. };
  181. struct pcnet32_tx_head {
  182. u32 base;
  183. s16 length;
  184. s16 status;
  185. u32 misc;
  186. u32 reserved;
  187. };
  188. /* The PCNET32 32-Bit initialization block, described in databook. */
  189. struct pcnet32_init_block {
  190. u16 mode;
  191. u16 tlen_rlen;
  192. u8 phys_addr[6];
  193. u16 reserved;
  194. u32 filter[2];
  195. /* Receive and transmit ring base, along with extra bits. */
  196. u32 rx_ring;
  197. u32 tx_ring;
  198. };
  199. /* PCnet32 access functions */
  200. struct pcnet32_access {
  201. u16 (*read_csr) (unsigned long, int);
  202. void (*write_csr) (unsigned long, int, u16);
  203. u16 (*read_bcr) (unsigned long, int);
  204. void (*write_bcr) (unsigned long, int, u16);
  205. u16 (*read_rap) (unsigned long);
  206. void (*write_rap) (unsigned long, u16);
  207. void (*reset) (unsigned long);
  208. };
  209. /*
  210. * The first field of pcnet32_private is read by the ethernet device
  211. * so the structure should be allocated using pci_alloc_consistent().
  212. */
  213. struct pcnet32_private {
  214. struct pcnet32_init_block init_block;
  215. /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
  216. struct pcnet32_rx_head *rx_ring;
  217. struct pcnet32_tx_head *tx_ring;
  218. dma_addr_t dma_addr;/* DMA address of beginning of this
  219. object, returned by pci_alloc_consistent */
  220. struct pci_dev *pci_dev;
  221. const char *name;
  222. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  223. struct sk_buff **tx_skbuff;
  224. struct sk_buff **rx_skbuff;
  225. dma_addr_t *tx_dma_addr;
  226. dma_addr_t *rx_dma_addr;
  227. struct pcnet32_access a;
  228. spinlock_t lock; /* Guard lock */
  229. unsigned int cur_rx, cur_tx; /* The next free ring entry */
  230. unsigned int rx_ring_size; /* current rx ring size */
  231. unsigned int tx_ring_size; /* current tx ring size */
  232. unsigned int rx_mod_mask; /* rx ring modular mask */
  233. unsigned int tx_mod_mask; /* tx ring modular mask */
  234. unsigned short rx_len_bits;
  235. unsigned short tx_len_bits;
  236. dma_addr_t rx_ring_dma_addr;
  237. dma_addr_t tx_ring_dma_addr;
  238. unsigned int dirty_rx, /* ring entries to be freed. */
  239. dirty_tx;
  240. struct net_device_stats stats;
  241. char tx_full;
  242. char phycount; /* number of phys found */
  243. int options;
  244. unsigned int shared_irq:1, /* shared irq possible */
  245. dxsuflo:1, /* disable transmit stop on uflo */
  246. mii:1; /* mii port available */
  247. struct net_device *next;
  248. struct mii_if_info mii_if;
  249. struct timer_list watchdog_timer;
  250. struct timer_list blink_timer;
  251. u32 msg_enable; /* debug message level */
  252. /* each bit indicates an available PHY */
  253. u32 phymask;
  254. };
  255. static int pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
  256. static int pcnet32_probe1(unsigned long, int, struct pci_dev *);
  257. static int pcnet32_open(struct net_device *);
  258. static int pcnet32_init_ring(struct net_device *);
  259. static int pcnet32_start_xmit(struct sk_buff *, struct net_device *);
  260. static int pcnet32_rx(struct net_device *);
  261. static void pcnet32_tx_timeout(struct net_device *dev);
  262. static irqreturn_t pcnet32_interrupt(int, void *, struct pt_regs *);
  263. static int pcnet32_close(struct net_device *);
  264. static struct net_device_stats *pcnet32_get_stats(struct net_device *);
  265. static void pcnet32_load_multicast(struct net_device *dev);
  266. static void pcnet32_set_multicast_list(struct net_device *);
  267. static int pcnet32_ioctl(struct net_device *, struct ifreq *, int);
  268. static void pcnet32_watchdog(struct net_device *);
  269. static int mdio_read(struct net_device *dev, int phy_id, int reg_num);
  270. static void mdio_write(struct net_device *dev, int phy_id, int reg_num,
  271. int val);
  272. static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits);
  273. static void pcnet32_ethtool_test(struct net_device *dev,
  274. struct ethtool_test *eth_test, u64 * data);
  275. static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1);
  276. static int pcnet32_phys_id(struct net_device *dev, u32 data);
  277. static void pcnet32_led_blink_callback(struct net_device *dev);
  278. static int pcnet32_get_regs_len(struct net_device *dev);
  279. static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  280. void *ptr);
  281. static void pcnet32_purge_tx_ring(struct net_device *dev);
  282. static int pcnet32_alloc_ring(struct net_device *dev, char *name);
  283. static void pcnet32_free_ring(struct net_device *dev);
  284. static void pcnet32_check_media(struct net_device *dev, int verbose);
  285. static u16 pcnet32_wio_read_csr(unsigned long addr, int index)
  286. {
  287. outw(index, addr + PCNET32_WIO_RAP);
  288. return inw(addr + PCNET32_WIO_RDP);
  289. }
  290. static void pcnet32_wio_write_csr(unsigned long addr, int index, u16 val)
  291. {
  292. outw(index, addr + PCNET32_WIO_RAP);
  293. outw(val, addr + PCNET32_WIO_RDP);
  294. }
  295. static u16 pcnet32_wio_read_bcr(unsigned long addr, int index)
  296. {
  297. outw(index, addr + PCNET32_WIO_RAP);
  298. return inw(addr + PCNET32_WIO_BDP);
  299. }
  300. static void pcnet32_wio_write_bcr(unsigned long addr, int index, u16 val)
  301. {
  302. outw(index, addr + PCNET32_WIO_RAP);
  303. outw(val, addr + PCNET32_WIO_BDP);
  304. }
  305. static u16 pcnet32_wio_read_rap(unsigned long addr)
  306. {
  307. return inw(addr + PCNET32_WIO_RAP);
  308. }
  309. static void pcnet32_wio_write_rap(unsigned long addr, u16 val)
  310. {
  311. outw(val, addr + PCNET32_WIO_RAP);
  312. }
  313. static void pcnet32_wio_reset(unsigned long addr)
  314. {
  315. inw(addr + PCNET32_WIO_RESET);
  316. }
  317. static int pcnet32_wio_check(unsigned long addr)
  318. {
  319. outw(88, addr + PCNET32_WIO_RAP);
  320. return (inw(addr + PCNET32_WIO_RAP) == 88);
  321. }
  322. static struct pcnet32_access pcnet32_wio = {
  323. .read_csr = pcnet32_wio_read_csr,
  324. .write_csr = pcnet32_wio_write_csr,
  325. .read_bcr = pcnet32_wio_read_bcr,
  326. .write_bcr = pcnet32_wio_write_bcr,
  327. .read_rap = pcnet32_wio_read_rap,
  328. .write_rap = pcnet32_wio_write_rap,
  329. .reset = pcnet32_wio_reset
  330. };
  331. static u16 pcnet32_dwio_read_csr(unsigned long addr, int index)
  332. {
  333. outl(index, addr + PCNET32_DWIO_RAP);
  334. return (inl(addr + PCNET32_DWIO_RDP) & 0xffff);
  335. }
  336. static void pcnet32_dwio_write_csr(unsigned long addr, int index, u16 val)
  337. {
  338. outl(index, addr + PCNET32_DWIO_RAP);
  339. outl(val, addr + PCNET32_DWIO_RDP);
  340. }
  341. static u16 pcnet32_dwio_read_bcr(unsigned long addr, int index)
  342. {
  343. outl(index, addr + PCNET32_DWIO_RAP);
  344. return (inl(addr + PCNET32_DWIO_BDP) & 0xffff);
  345. }
  346. static void pcnet32_dwio_write_bcr(unsigned long addr, int index, u16 val)
  347. {
  348. outl(index, addr + PCNET32_DWIO_RAP);
  349. outl(val, addr + PCNET32_DWIO_BDP);
  350. }
  351. static u16 pcnet32_dwio_read_rap(unsigned long addr)
  352. {
  353. return (inl(addr + PCNET32_DWIO_RAP) & 0xffff);
  354. }
  355. static void pcnet32_dwio_write_rap(unsigned long addr, u16 val)
  356. {
  357. outl(val, addr + PCNET32_DWIO_RAP);
  358. }
  359. static void pcnet32_dwio_reset(unsigned long addr)
  360. {
  361. inl(addr + PCNET32_DWIO_RESET);
  362. }
  363. static int pcnet32_dwio_check(unsigned long addr)
  364. {
  365. outl(88, addr + PCNET32_DWIO_RAP);
  366. return ((inl(addr + PCNET32_DWIO_RAP) & 0xffff) == 88);
  367. }
  368. static struct pcnet32_access pcnet32_dwio = {
  369. .read_csr = pcnet32_dwio_read_csr,
  370. .write_csr = pcnet32_dwio_write_csr,
  371. .read_bcr = pcnet32_dwio_read_bcr,
  372. .write_bcr = pcnet32_dwio_write_bcr,
  373. .read_rap = pcnet32_dwio_read_rap,
  374. .write_rap = pcnet32_dwio_write_rap,
  375. .reset = pcnet32_dwio_reset
  376. };
  377. static void pcnet32_netif_stop(struct net_device *dev)
  378. {
  379. dev->trans_start = jiffies;
  380. netif_poll_disable(dev);
  381. netif_tx_disable(dev);
  382. }
  383. static void pcnet32_netif_start(struct net_device *dev)
  384. {
  385. netif_wake_queue(dev);
  386. netif_poll_enable(dev);
  387. }
  388. /*
  389. * Allocate space for the new sized tx ring.
  390. * Free old resources
  391. * Save new resources.
  392. * Any failure keeps old resources.
  393. * Must be called with lp->lock held.
  394. */
  395. static void pcnet32_realloc_tx_ring(struct net_device *dev,
  396. struct pcnet32_private *lp,
  397. unsigned int size)
  398. {
  399. dma_addr_t new_ring_dma_addr;
  400. dma_addr_t *new_dma_addr_list;
  401. struct pcnet32_tx_head *new_tx_ring;
  402. struct sk_buff **new_skb_list;
  403. pcnet32_purge_tx_ring(dev);
  404. new_tx_ring = pci_alloc_consistent(lp->pci_dev,
  405. sizeof(struct pcnet32_tx_head) *
  406. (1 << size),
  407. &new_ring_dma_addr);
  408. if (new_tx_ring == NULL) {
  409. if (netif_msg_drv(lp))
  410. printk("\n" KERN_ERR
  411. "%s: Consistent memory allocation failed.\n",
  412. dev->name);
  413. return;
  414. }
  415. memset(new_tx_ring, 0, sizeof(struct pcnet32_tx_head) * (1 << size));
  416. new_dma_addr_list = kcalloc((1 << size), sizeof(dma_addr_t),
  417. GFP_ATOMIC);
  418. if (!new_dma_addr_list) {
  419. if (netif_msg_drv(lp))
  420. printk("\n" KERN_ERR
  421. "%s: Memory allocation failed.\n", dev->name);
  422. goto free_new_tx_ring;
  423. }
  424. new_skb_list = kcalloc((1 << size), sizeof(struct sk_buff *),
  425. GFP_ATOMIC);
  426. if (!new_skb_list) {
  427. if (netif_msg_drv(lp))
  428. printk("\n" KERN_ERR
  429. "%s: Memory allocation failed.\n", dev->name);
  430. goto free_new_lists;
  431. }
  432. kfree(lp->tx_skbuff);
  433. kfree(lp->tx_dma_addr);
  434. pci_free_consistent(lp->pci_dev,
  435. sizeof(struct pcnet32_tx_head) *
  436. lp->tx_ring_size, lp->tx_ring,
  437. lp->tx_ring_dma_addr);
  438. lp->tx_ring_size = (1 << size);
  439. lp->tx_mod_mask = lp->tx_ring_size - 1;
  440. lp->tx_len_bits = (size << 12);
  441. lp->tx_ring = new_tx_ring;
  442. lp->tx_ring_dma_addr = new_ring_dma_addr;
  443. lp->tx_dma_addr = new_dma_addr_list;
  444. lp->tx_skbuff = new_skb_list;
  445. return;
  446. free_new_lists:
  447. kfree(new_dma_addr_list);
  448. free_new_tx_ring:
  449. pci_free_consistent(lp->pci_dev,
  450. sizeof(struct pcnet32_tx_head) *
  451. (1 << size),
  452. new_tx_ring,
  453. new_ring_dma_addr);
  454. return;
  455. }
  456. /*
  457. * Allocate space for the new sized rx ring.
  458. * Re-use old receive buffers.
  459. * alloc extra buffers
  460. * free unneeded buffers
  461. * free unneeded buffers
  462. * Save new resources.
  463. * Any failure keeps old resources.
  464. * Must be called with lp->lock held.
  465. */
  466. static void pcnet32_realloc_rx_ring(struct net_device *dev,
  467. struct pcnet32_private *lp,
  468. unsigned int size)
  469. {
  470. dma_addr_t new_ring_dma_addr;
  471. dma_addr_t *new_dma_addr_list;
  472. struct pcnet32_rx_head *new_rx_ring;
  473. struct sk_buff **new_skb_list;
  474. int new, overlap;
  475. new_rx_ring = pci_alloc_consistent(lp->pci_dev,
  476. sizeof(struct pcnet32_rx_head) *
  477. (1 << size),
  478. &new_ring_dma_addr);
  479. if (new_rx_ring == NULL) {
  480. if (netif_msg_drv(lp))
  481. printk("\n" KERN_ERR
  482. "%s: Consistent memory allocation failed.\n",
  483. dev->name);
  484. return;
  485. }
  486. memset(new_rx_ring, 0, sizeof(struct pcnet32_rx_head) * (1 << size));
  487. new_dma_addr_list = kcalloc((1 << size), sizeof(dma_addr_t),
  488. GFP_ATOMIC);
  489. if (!new_dma_addr_list) {
  490. if (netif_msg_drv(lp))
  491. printk("\n" KERN_ERR
  492. "%s: Memory allocation failed.\n", dev->name);
  493. goto free_new_rx_ring;
  494. }
  495. new_skb_list = kcalloc((1 << size), sizeof(struct sk_buff *),
  496. GFP_ATOMIC);
  497. if (!new_skb_list) {
  498. if (netif_msg_drv(lp))
  499. printk("\n" KERN_ERR
  500. "%s: Memory allocation failed.\n", dev->name);
  501. goto free_new_lists;
  502. }
  503. /* first copy the current receive buffers */
  504. overlap = min(size, lp->rx_ring_size);
  505. for (new = 0; new < overlap; new++) {
  506. new_rx_ring[new] = lp->rx_ring[new];
  507. new_dma_addr_list[new] = lp->rx_dma_addr[new];
  508. new_skb_list[new] = lp->rx_skbuff[new];
  509. }
  510. /* now allocate any new buffers needed */
  511. for (; new < size; new++ ) {
  512. struct sk_buff *rx_skbuff;
  513. new_skb_list[new] = dev_alloc_skb(PKT_BUF_SZ);
  514. if (!(rx_skbuff = new_skb_list[new])) {
  515. /* keep the original lists and buffers */
  516. if (netif_msg_drv(lp))
  517. printk(KERN_ERR
  518. "%s: pcnet32_realloc_rx_ring dev_alloc_skb failed.\n",
  519. dev->name);
  520. goto free_all_new;
  521. }
  522. skb_reserve(rx_skbuff, 2);
  523. new_dma_addr_list[new] =
  524. pci_map_single(lp->pci_dev, rx_skbuff->data,
  525. PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
  526. new_rx_ring[new].base = (u32) le32_to_cpu(new_dma_addr_list[new]);
  527. new_rx_ring[new].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
  528. new_rx_ring[new].status = le16_to_cpu(0x8000);
  529. }
  530. /* and free any unneeded buffers */
  531. for (; new < lp->rx_ring_size; new++) {
  532. if (lp->rx_skbuff[new]) {
  533. pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[new],
  534. PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
  535. dev_kfree_skb(lp->rx_skbuff[new]);
  536. }
  537. }
  538. kfree(lp->rx_skbuff);
  539. kfree(lp->rx_dma_addr);
  540. pci_free_consistent(lp->pci_dev,
  541. sizeof(struct pcnet32_rx_head) *
  542. lp->rx_ring_size, lp->rx_ring,
  543. lp->rx_ring_dma_addr);
  544. lp->rx_ring_size = (1 << size);
  545. lp->rx_mod_mask = lp->rx_ring_size - 1;
  546. lp->rx_len_bits = (size << 4);
  547. lp->rx_ring = new_rx_ring;
  548. lp->rx_ring_dma_addr = new_ring_dma_addr;
  549. lp->rx_dma_addr = new_dma_addr_list;
  550. lp->rx_skbuff = new_skb_list;
  551. return;
  552. free_all_new:
  553. for (; --new >= lp->rx_ring_size; ) {
  554. if (new_skb_list[new]) {
  555. pci_unmap_single(lp->pci_dev, new_dma_addr_list[new],
  556. PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
  557. dev_kfree_skb(new_skb_list[new]);
  558. }
  559. }
  560. kfree(new_skb_list);
  561. free_new_lists:
  562. kfree(new_dma_addr_list);
  563. free_new_rx_ring:
  564. pci_free_consistent(lp->pci_dev,
  565. sizeof(struct pcnet32_rx_head) *
  566. (1 << size),
  567. new_rx_ring,
  568. new_ring_dma_addr);
  569. return;
  570. }
  571. #ifdef CONFIG_NET_POLL_CONTROLLER
  572. static void pcnet32_poll_controller(struct net_device *dev)
  573. {
  574. disable_irq(dev->irq);
  575. pcnet32_interrupt(0, dev, NULL);
  576. enable_irq(dev->irq);
  577. }
  578. #endif
  579. static int pcnet32_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  580. {
  581. struct pcnet32_private *lp = dev->priv;
  582. unsigned long flags;
  583. int r = -EOPNOTSUPP;
  584. if (lp->mii) {
  585. spin_lock_irqsave(&lp->lock, flags);
  586. mii_ethtool_gset(&lp->mii_if, cmd);
  587. spin_unlock_irqrestore(&lp->lock, flags);
  588. r = 0;
  589. }
  590. return r;
  591. }
  592. static int pcnet32_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  593. {
  594. struct pcnet32_private *lp = dev->priv;
  595. unsigned long flags;
  596. int r = -EOPNOTSUPP;
  597. if (lp->mii) {
  598. spin_lock_irqsave(&lp->lock, flags);
  599. r = mii_ethtool_sset(&lp->mii_if, cmd);
  600. spin_unlock_irqrestore(&lp->lock, flags);
  601. }
  602. return r;
  603. }
  604. static void pcnet32_get_drvinfo(struct net_device *dev,
  605. struct ethtool_drvinfo *info)
  606. {
  607. struct pcnet32_private *lp = dev->priv;
  608. strcpy(info->driver, DRV_NAME);
  609. strcpy(info->version, DRV_VERSION);
  610. if (lp->pci_dev)
  611. strcpy(info->bus_info, pci_name(lp->pci_dev));
  612. else
  613. sprintf(info->bus_info, "VLB 0x%lx", dev->base_addr);
  614. }
  615. static u32 pcnet32_get_link(struct net_device *dev)
  616. {
  617. struct pcnet32_private *lp = dev->priv;
  618. unsigned long flags;
  619. int r;
  620. spin_lock_irqsave(&lp->lock, flags);
  621. if (lp->mii) {
  622. r = mii_link_ok(&lp->mii_if);
  623. } else {
  624. ulong ioaddr = dev->base_addr; /* card base I/O address */
  625. r = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
  626. }
  627. spin_unlock_irqrestore(&lp->lock, flags);
  628. return r;
  629. }
  630. static u32 pcnet32_get_msglevel(struct net_device *dev)
  631. {
  632. struct pcnet32_private *lp = dev->priv;
  633. return lp->msg_enable;
  634. }
  635. static void pcnet32_set_msglevel(struct net_device *dev, u32 value)
  636. {
  637. struct pcnet32_private *lp = dev->priv;
  638. lp->msg_enable = value;
  639. }
  640. static int pcnet32_nway_reset(struct net_device *dev)
  641. {
  642. struct pcnet32_private *lp = dev->priv;
  643. unsigned long flags;
  644. int r = -EOPNOTSUPP;
  645. if (lp->mii) {
  646. spin_lock_irqsave(&lp->lock, flags);
  647. r = mii_nway_restart(&lp->mii_if);
  648. spin_unlock_irqrestore(&lp->lock, flags);
  649. }
  650. return r;
  651. }
  652. static void pcnet32_get_ringparam(struct net_device *dev,
  653. struct ethtool_ringparam *ering)
  654. {
  655. struct pcnet32_private *lp = dev->priv;
  656. ering->tx_max_pending = TX_MAX_RING_SIZE;
  657. ering->tx_pending = lp->tx_ring_size;
  658. ering->rx_max_pending = RX_MAX_RING_SIZE;
  659. ering->rx_pending = lp->rx_ring_size;
  660. }
  661. static int pcnet32_set_ringparam(struct net_device *dev,
  662. struct ethtool_ringparam *ering)
  663. {
  664. struct pcnet32_private *lp = dev->priv;
  665. unsigned long flags;
  666. unsigned int size;
  667. ulong ioaddr = dev->base_addr;
  668. int i;
  669. if (ering->rx_mini_pending || ering->rx_jumbo_pending)
  670. return -EINVAL;
  671. if (netif_running(dev))
  672. pcnet32_netif_stop(dev);
  673. spin_lock_irqsave(&lp->lock, flags);
  674. lp->a.write_csr(ioaddr, CSR0, CSR0_STOP); /* stop the chip */
  675. size = min(ering->tx_pending, (unsigned int)TX_MAX_RING_SIZE);
  676. /* set the minimum ring size to 4, to allow the loopback test to work
  677. * unchanged.
  678. */
  679. for (i = 2; i <= PCNET32_LOG_MAX_TX_BUFFERS; i++) {
  680. if (size <= (1 << i))
  681. break;
  682. }
  683. if ((1 << i) != lp->tx_ring_size)
  684. pcnet32_realloc_tx_ring(dev, lp, i);
  685. size = min(ering->rx_pending, (unsigned int)RX_MAX_RING_SIZE);
  686. for (i = 2; i <= PCNET32_LOG_MAX_RX_BUFFERS; i++) {
  687. if (size <= (1 << i))
  688. break;
  689. }
  690. if ((1 << i) != lp->rx_ring_size)
  691. pcnet32_realloc_rx_ring(dev, lp, i);
  692. dev->weight = lp->rx_ring_size / 2;
  693. if (netif_running(dev)) {
  694. pcnet32_netif_start(dev);
  695. pcnet32_restart(dev, CSR0_NORMAL);
  696. }
  697. spin_unlock_irqrestore(&lp->lock, flags);
  698. if (netif_msg_drv(lp))
  699. printk(KERN_INFO
  700. "%s: Ring Param Settings: RX: %d, TX: %d\n", dev->name,
  701. lp->rx_ring_size, lp->tx_ring_size);
  702. return 0;
  703. }
  704. static void pcnet32_get_strings(struct net_device *dev, u32 stringset,
  705. u8 * data)
  706. {
  707. memcpy(data, pcnet32_gstrings_test, sizeof(pcnet32_gstrings_test));
  708. }
  709. static int pcnet32_self_test_count(struct net_device *dev)
  710. {
  711. return PCNET32_TEST_LEN;
  712. }
  713. static void pcnet32_ethtool_test(struct net_device *dev,
  714. struct ethtool_test *test, u64 * data)
  715. {
  716. struct pcnet32_private *lp = dev->priv;
  717. int rc;
  718. if (test->flags == ETH_TEST_FL_OFFLINE) {
  719. rc = pcnet32_loopback_test(dev, data);
  720. if (rc) {
  721. if (netif_msg_hw(lp))
  722. printk(KERN_DEBUG "%s: Loopback test failed.\n",
  723. dev->name);
  724. test->flags |= ETH_TEST_FL_FAILED;
  725. } else if (netif_msg_hw(lp))
  726. printk(KERN_DEBUG "%s: Loopback test passed.\n",
  727. dev->name);
  728. } else if (netif_msg_hw(lp))
  729. printk(KERN_DEBUG
  730. "%s: No tests to run (specify 'Offline' on ethtool).",
  731. dev->name);
  732. } /* end pcnet32_ethtool_test */
  733. static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1)
  734. {
  735. struct pcnet32_private *lp = dev->priv;
  736. struct pcnet32_access *a = &lp->a; /* access to registers */
  737. ulong ioaddr = dev->base_addr; /* card base I/O address */
  738. struct sk_buff *skb; /* sk buff */
  739. int x, i; /* counters */
  740. int numbuffs = 4; /* number of TX/RX buffers and descs */
  741. u16 status = 0x8300; /* TX ring status */
  742. u16 teststatus; /* test of ring status */
  743. int rc; /* return code */
  744. int size; /* size of packets */
  745. unsigned char *packet; /* source packet data */
  746. static const int data_len = 60; /* length of source packets */
  747. unsigned long flags;
  748. unsigned long ticks;
  749. *data1 = 1; /* status of test, default to fail */
  750. rc = 1; /* default to fail */
  751. if (netif_running(dev))
  752. pcnet32_close(dev);
  753. spin_lock_irqsave(&lp->lock, flags);
  754. /* Reset the PCNET32 */
  755. lp->a.reset(ioaddr);
  756. /* switch pcnet32 to 32bit mode */
  757. lp->a.write_bcr(ioaddr, 20, 2);
  758. lp->init_block.mode =
  759. le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  760. lp->init_block.filter[0] = 0;
  761. lp->init_block.filter[1] = 0;
  762. /* purge & init rings but don't actually restart */
  763. pcnet32_restart(dev, 0x0000);
  764. lp->a.write_csr(ioaddr, 0, 0x0004); /* Set STOP bit */
  765. /* Initialize Transmit buffers. */
  766. size = data_len + 15;
  767. for (x = 0; x < numbuffs; x++) {
  768. if (!(skb = dev_alloc_skb(size))) {
  769. if (netif_msg_hw(lp))
  770. printk(KERN_DEBUG
  771. "%s: Cannot allocate skb at line: %d!\n",
  772. dev->name, __LINE__);
  773. goto clean_up;
  774. } else {
  775. packet = skb->data;
  776. skb_put(skb, size); /* create space for data */
  777. lp->tx_skbuff[x] = skb;
  778. lp->tx_ring[x].length = le16_to_cpu(-skb->len);
  779. lp->tx_ring[x].misc = 0;
  780. /* put DA and SA into the skb */
  781. for (i = 0; i < 6; i++)
  782. *packet++ = dev->dev_addr[i];
  783. for (i = 0; i < 6; i++)
  784. *packet++ = dev->dev_addr[i];
  785. /* type */
  786. *packet++ = 0x08;
  787. *packet++ = 0x06;
  788. /* packet number */
  789. *packet++ = x;
  790. /* fill packet with data */
  791. for (i = 0; i < data_len; i++)
  792. *packet++ = i;
  793. lp->tx_dma_addr[x] =
  794. pci_map_single(lp->pci_dev, skb->data, skb->len,
  795. PCI_DMA_TODEVICE);
  796. lp->tx_ring[x].base =
  797. (u32) le32_to_cpu(lp->tx_dma_addr[x]);
  798. wmb(); /* Make sure owner changes after all others are visible */
  799. lp->tx_ring[x].status = le16_to_cpu(status);
  800. }
  801. }
  802. x = a->read_bcr(ioaddr, 32); /* set internal loopback in BSR32 */
  803. x = x | 0x0002;
  804. a->write_bcr(ioaddr, 32, x);
  805. lp->a.write_csr(ioaddr, 15, 0x0044); /* set int loopback in CSR15 */
  806. teststatus = le16_to_cpu(0x8000);
  807. lp->a.write_csr(ioaddr, 0, 0x0002); /* Set STRT bit */
  808. /* Check status of descriptors */
  809. for (x = 0; x < numbuffs; x++) {
  810. ticks = 0;
  811. rmb();
  812. while ((lp->rx_ring[x].status & teststatus) && (ticks < 200)) {
  813. spin_unlock_irqrestore(&lp->lock, flags);
  814. mdelay(1);
  815. spin_lock_irqsave(&lp->lock, flags);
  816. rmb();
  817. ticks++;
  818. }
  819. if (ticks == 200) {
  820. if (netif_msg_hw(lp))
  821. printk("%s: Desc %d failed to reset!\n",
  822. dev->name, x);
  823. break;
  824. }
  825. }
  826. lp->a.write_csr(ioaddr, 0, 0x0004); /* Set STOP bit */
  827. wmb();
  828. if (netif_msg_hw(lp) && netif_msg_pktdata(lp)) {
  829. printk(KERN_DEBUG "%s: RX loopback packets:\n", dev->name);
  830. for (x = 0; x < numbuffs; x++) {
  831. printk(KERN_DEBUG "%s: Packet %d:\n", dev->name, x);
  832. skb = lp->rx_skbuff[x];
  833. for (i = 0; i < size; i++) {
  834. printk("%02x ", *(skb->data + i));
  835. }
  836. printk("\n");
  837. }
  838. }
  839. x = 0;
  840. rc = 0;
  841. while (x < numbuffs && !rc) {
  842. skb = lp->rx_skbuff[x];
  843. packet = lp->tx_skbuff[x]->data;
  844. for (i = 0; i < size; i++) {
  845. if (*(skb->data + i) != packet[i]) {
  846. if (netif_msg_hw(lp))
  847. printk(KERN_DEBUG
  848. "%s: Error in compare! %2x - %02x %02x\n",
  849. dev->name, i, *(skb->data + i),
  850. packet[i]);
  851. rc = 1;
  852. break;
  853. }
  854. }
  855. x++;
  856. }
  857. if (!rc) {
  858. *data1 = 0;
  859. }
  860. clean_up:
  861. pcnet32_purge_tx_ring(dev);
  862. x = a->read_csr(ioaddr, 15) & 0xFFFF;
  863. a->write_csr(ioaddr, 15, (x & ~0x0044)); /* reset bits 6 and 2 */
  864. x = a->read_bcr(ioaddr, 32); /* reset internal loopback */
  865. x = x & ~0x0002;
  866. a->write_bcr(ioaddr, 32, x);
  867. spin_unlock_irqrestore(&lp->lock, flags);
  868. if (netif_running(dev)) {
  869. pcnet32_open(dev);
  870. } else {
  871. lp->a.write_bcr(ioaddr, 20, 4); /* return to 16bit mode */
  872. }
  873. return (rc);
  874. } /* end pcnet32_loopback_test */
  875. static void pcnet32_led_blink_callback(struct net_device *dev)
  876. {
  877. struct pcnet32_private *lp = dev->priv;
  878. struct pcnet32_access *a = &lp->a;
  879. ulong ioaddr = dev->base_addr;
  880. unsigned long flags;
  881. int i;
  882. spin_lock_irqsave(&lp->lock, flags);
  883. for (i = 4; i < 8; i++) {
  884. a->write_bcr(ioaddr, i, a->read_bcr(ioaddr, i) ^ 0x4000);
  885. }
  886. spin_unlock_irqrestore(&lp->lock, flags);
  887. mod_timer(&lp->blink_timer, PCNET32_BLINK_TIMEOUT);
  888. }
  889. static int pcnet32_phys_id(struct net_device *dev, u32 data)
  890. {
  891. struct pcnet32_private *lp = dev->priv;
  892. struct pcnet32_access *a = &lp->a;
  893. ulong ioaddr = dev->base_addr;
  894. unsigned long flags;
  895. int i, regs[4];
  896. if (!lp->blink_timer.function) {
  897. init_timer(&lp->blink_timer);
  898. lp->blink_timer.function = (void *)pcnet32_led_blink_callback;
  899. lp->blink_timer.data = (unsigned long)dev;
  900. }
  901. /* Save the current value of the bcrs */
  902. spin_lock_irqsave(&lp->lock, flags);
  903. for (i = 4; i < 8; i++) {
  904. regs[i - 4] = a->read_bcr(ioaddr, i);
  905. }
  906. spin_unlock_irqrestore(&lp->lock, flags);
  907. mod_timer(&lp->blink_timer, jiffies);
  908. set_current_state(TASK_INTERRUPTIBLE);
  909. if ((!data) || (data > (u32) (MAX_SCHEDULE_TIMEOUT / HZ)))
  910. data = (u32) (MAX_SCHEDULE_TIMEOUT / HZ);
  911. msleep_interruptible(data * 1000);
  912. del_timer_sync(&lp->blink_timer);
  913. /* Restore the original value of the bcrs */
  914. spin_lock_irqsave(&lp->lock, flags);
  915. for (i = 4; i < 8; i++) {
  916. a->write_bcr(ioaddr, i, regs[i - 4]);
  917. }
  918. spin_unlock_irqrestore(&lp->lock, flags);
  919. return 0;
  920. }
  921. #define PCNET32_REGS_PER_PHY 32
  922. #define PCNET32_MAX_PHYS 32
  923. static int pcnet32_get_regs_len(struct net_device *dev)
  924. {
  925. struct pcnet32_private *lp = dev->priv;
  926. int j = lp->phycount * PCNET32_REGS_PER_PHY;
  927. return ((PCNET32_NUM_REGS + j) * sizeof(u16));
  928. }
  929. static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  930. void *ptr)
  931. {
  932. int i, csr0;
  933. u16 *buff = ptr;
  934. struct pcnet32_private *lp = dev->priv;
  935. struct pcnet32_access *a = &lp->a;
  936. ulong ioaddr = dev->base_addr;
  937. int ticks;
  938. unsigned long flags;
  939. spin_lock_irqsave(&lp->lock, flags);
  940. csr0 = a->read_csr(ioaddr, 0);
  941. if (!(csr0 & 0x0004)) { /* If not stopped */
  942. /* set SUSPEND (SPND) - CSR5 bit 0 */
  943. a->write_csr(ioaddr, 5, 0x0001);
  944. /* poll waiting for bit to be set */
  945. ticks = 0;
  946. while (!(a->read_csr(ioaddr, 5) & 0x0001)) {
  947. spin_unlock_irqrestore(&lp->lock, flags);
  948. mdelay(1);
  949. spin_lock_irqsave(&lp->lock, flags);
  950. ticks++;
  951. if (ticks > 200) {
  952. if (netif_msg_hw(lp))
  953. printk(KERN_DEBUG
  954. "%s: Error getting into suspend!\n",
  955. dev->name);
  956. break;
  957. }
  958. }
  959. }
  960. /* read address PROM */
  961. for (i = 0; i < 16; i += 2)
  962. *buff++ = inw(ioaddr + i);
  963. /* read control and status registers */
  964. for (i = 0; i < 90; i++) {
  965. *buff++ = a->read_csr(ioaddr, i);
  966. }
  967. *buff++ = a->read_csr(ioaddr, 112);
  968. *buff++ = a->read_csr(ioaddr, 114);
  969. /* read bus configuration registers */
  970. for (i = 0; i < 30; i++) {
  971. *buff++ = a->read_bcr(ioaddr, i);
  972. }
  973. *buff++ = 0; /* skip bcr30 so as not to hang 79C976 */
  974. for (i = 31; i < 36; i++) {
  975. *buff++ = a->read_bcr(ioaddr, i);
  976. }
  977. /* read mii phy registers */
  978. if (lp->mii) {
  979. int j;
  980. for (j = 0; j < PCNET32_MAX_PHYS; j++) {
  981. if (lp->phymask & (1 << j)) {
  982. for (i = 0; i < PCNET32_REGS_PER_PHY; i++) {
  983. lp->a.write_bcr(ioaddr, 33,
  984. (j << 5) | i);
  985. *buff++ = lp->a.read_bcr(ioaddr, 34);
  986. }
  987. }
  988. }
  989. }
  990. if (!(csr0 & 0x0004)) { /* If not stopped */
  991. /* clear SUSPEND (SPND) - CSR5 bit 0 */
  992. a->write_csr(ioaddr, 5, 0x0000);
  993. }
  994. spin_unlock_irqrestore(&lp->lock, flags);
  995. }
  996. static struct ethtool_ops pcnet32_ethtool_ops = {
  997. .get_settings = pcnet32_get_settings,
  998. .set_settings = pcnet32_set_settings,
  999. .get_drvinfo = pcnet32_get_drvinfo,
  1000. .get_msglevel = pcnet32_get_msglevel,
  1001. .set_msglevel = pcnet32_set_msglevel,
  1002. .nway_reset = pcnet32_nway_reset,
  1003. .get_link = pcnet32_get_link,
  1004. .get_ringparam = pcnet32_get_ringparam,
  1005. .set_ringparam = pcnet32_set_ringparam,
  1006. .get_tx_csum = ethtool_op_get_tx_csum,
  1007. .get_sg = ethtool_op_get_sg,
  1008. .get_tso = ethtool_op_get_tso,
  1009. .get_strings = pcnet32_get_strings,
  1010. .self_test_count = pcnet32_self_test_count,
  1011. .self_test = pcnet32_ethtool_test,
  1012. .phys_id = pcnet32_phys_id,
  1013. .get_regs_len = pcnet32_get_regs_len,
  1014. .get_regs = pcnet32_get_regs,
  1015. .get_perm_addr = ethtool_op_get_perm_addr,
  1016. };
  1017. /* only probes for non-PCI devices, the rest are handled by
  1018. * pci_register_driver via pcnet32_probe_pci */
  1019. static void __devinit pcnet32_probe_vlbus(unsigned int *pcnet32_portlist)
  1020. {
  1021. unsigned int *port, ioaddr;
  1022. /* search for PCnet32 VLB cards at known addresses */
  1023. for (port = pcnet32_portlist; (ioaddr = *port); port++) {
  1024. if (request_region
  1025. (ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_vlbus")) {
  1026. /* check if there is really a pcnet chip on that ioaddr */
  1027. if ((inb(ioaddr + 14) == 0x57)
  1028. && (inb(ioaddr + 15) == 0x57)) {
  1029. pcnet32_probe1(ioaddr, 0, NULL);
  1030. } else {
  1031. release_region(ioaddr, PCNET32_TOTAL_SIZE);
  1032. }
  1033. }
  1034. }
  1035. }
  1036. static int __devinit
  1037. pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
  1038. {
  1039. unsigned long ioaddr;
  1040. int err;
  1041. err = pci_enable_device(pdev);
  1042. if (err < 0) {
  1043. if (pcnet32_debug & NETIF_MSG_PROBE)
  1044. printk(KERN_ERR PFX
  1045. "failed to enable device -- err=%d\n", err);
  1046. return err;
  1047. }
  1048. pci_set_master(pdev);
  1049. ioaddr = pci_resource_start(pdev, 0);
  1050. if (!ioaddr) {
  1051. if (pcnet32_debug & NETIF_MSG_PROBE)
  1052. printk(KERN_ERR PFX
  1053. "card has no PCI IO resources, aborting\n");
  1054. return -ENODEV;
  1055. }
  1056. if (!pci_dma_supported(pdev, PCNET32_DMA_MASK)) {
  1057. if (pcnet32_debug & NETIF_MSG_PROBE)
  1058. printk(KERN_ERR PFX
  1059. "architecture does not support 32bit PCI busmaster DMA\n");
  1060. return -ENODEV;
  1061. }
  1062. if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_pci") ==
  1063. NULL) {
  1064. if (pcnet32_debug & NETIF_MSG_PROBE)
  1065. printk(KERN_ERR PFX
  1066. "io address range already allocated\n");
  1067. return -EBUSY;
  1068. }
  1069. err = pcnet32_probe1(ioaddr, 1, pdev);
  1070. if (err < 0) {
  1071. pci_disable_device(pdev);
  1072. }
  1073. return err;
  1074. }
  1075. /* pcnet32_probe1
  1076. * Called from both pcnet32_probe_vlbus and pcnet_probe_pci.
  1077. * pdev will be NULL when called from pcnet32_probe_vlbus.
  1078. */
  1079. static int __devinit
  1080. pcnet32_probe1(unsigned long ioaddr, int shared, struct pci_dev *pdev)
  1081. {
  1082. struct pcnet32_private *lp;
  1083. dma_addr_t lp_dma_addr;
  1084. int i, media;
  1085. int fdx, mii, fset, dxsuflo;
  1086. int chip_version;
  1087. char *chipname;
  1088. struct net_device *dev;
  1089. struct pcnet32_access *a = NULL;
  1090. u8 promaddr[6];
  1091. int ret = -ENODEV;
  1092. /* reset the chip */
  1093. pcnet32_wio_reset(ioaddr);
  1094. /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
  1095. if (pcnet32_wio_read_csr(ioaddr, 0) == 4 && pcnet32_wio_check(ioaddr)) {
  1096. a = &pcnet32_wio;
  1097. } else {
  1098. pcnet32_dwio_reset(ioaddr);
  1099. if (pcnet32_dwio_read_csr(ioaddr, 0) == 4
  1100. && pcnet32_dwio_check(ioaddr)) {
  1101. a = &pcnet32_dwio;
  1102. } else
  1103. goto err_release_region;
  1104. }
  1105. chip_version =
  1106. a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr, 89) << 16);
  1107. if ((pcnet32_debug & NETIF_MSG_PROBE) && (pcnet32_debug & NETIF_MSG_HW))
  1108. printk(KERN_INFO " PCnet chip version is %#x.\n",
  1109. chip_version);
  1110. if ((chip_version & 0xfff) != 0x003) {
  1111. if (pcnet32_debug & NETIF_MSG_PROBE)
  1112. printk(KERN_INFO PFX "Unsupported chip version.\n");
  1113. goto err_release_region;
  1114. }
  1115. /* initialize variables */
  1116. fdx = mii = fset = dxsuflo = 0;
  1117. chip_version = (chip_version >> 12) & 0xffff;
  1118. switch (chip_version) {
  1119. case 0x2420:
  1120. chipname = "PCnet/PCI 79C970"; /* PCI */
  1121. break;
  1122. case 0x2430:
  1123. if (shared)
  1124. chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
  1125. else
  1126. chipname = "PCnet/32 79C965"; /* 486/VL bus */
  1127. break;
  1128. case 0x2621:
  1129. chipname = "PCnet/PCI II 79C970A"; /* PCI */
  1130. fdx = 1;
  1131. break;
  1132. case 0x2623:
  1133. chipname = "PCnet/FAST 79C971"; /* PCI */
  1134. fdx = 1;
  1135. mii = 1;
  1136. fset = 1;
  1137. break;
  1138. case 0x2624:
  1139. chipname = "PCnet/FAST+ 79C972"; /* PCI */
  1140. fdx = 1;
  1141. mii = 1;
  1142. fset = 1;
  1143. break;
  1144. case 0x2625:
  1145. chipname = "PCnet/FAST III 79C973"; /* PCI */
  1146. fdx = 1;
  1147. mii = 1;
  1148. break;
  1149. case 0x2626:
  1150. chipname = "PCnet/Home 79C978"; /* PCI */
  1151. fdx = 1;
  1152. /*
  1153. * This is based on specs published at www.amd.com. This section
  1154. * assumes that a card with a 79C978 wants to go into standard
  1155. * ethernet mode. The 79C978 can also go into 1Mb HomePNA mode,
  1156. * and the module option homepna=1 can select this instead.
  1157. */
  1158. media = a->read_bcr(ioaddr, 49);
  1159. media &= ~3; /* default to 10Mb ethernet */
  1160. if (cards_found < MAX_UNITS && homepna[cards_found])
  1161. media |= 1; /* switch to home wiring mode */
  1162. if (pcnet32_debug & NETIF_MSG_PROBE)
  1163. printk(KERN_DEBUG PFX "media set to %sMbit mode.\n",
  1164. (media & 1) ? "1" : "10");
  1165. a->write_bcr(ioaddr, 49, media);
  1166. break;
  1167. case 0x2627:
  1168. chipname = "PCnet/FAST III 79C975"; /* PCI */
  1169. fdx = 1;
  1170. mii = 1;
  1171. break;
  1172. case 0x2628:
  1173. chipname = "PCnet/PRO 79C976";
  1174. fdx = 1;
  1175. mii = 1;
  1176. break;
  1177. default:
  1178. if (pcnet32_debug & NETIF_MSG_PROBE)
  1179. printk(KERN_INFO PFX
  1180. "PCnet version %#x, no PCnet32 chip.\n",
  1181. chip_version);
  1182. goto err_release_region;
  1183. }
  1184. /*
  1185. * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
  1186. * starting until the packet is loaded. Strike one for reliability, lose
  1187. * one for latency - although on PCI this isnt a big loss. Older chips
  1188. * have FIFO's smaller than a packet, so you can't do this.
  1189. * Turn on BCR18:BurstRdEn and BCR18:BurstWrEn.
  1190. */
  1191. if (fset) {
  1192. a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0860));
  1193. a->write_csr(ioaddr, 80,
  1194. (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
  1195. dxsuflo = 1;
  1196. }
  1197. dev = alloc_etherdev(0);
  1198. if (!dev) {
  1199. if (pcnet32_debug & NETIF_MSG_PROBE)
  1200. printk(KERN_ERR PFX "Memory allocation failed.\n");
  1201. ret = -ENOMEM;
  1202. goto err_release_region;
  1203. }
  1204. SET_NETDEV_DEV(dev, &pdev->dev);
  1205. if (pcnet32_debug & NETIF_MSG_PROBE)
  1206. printk(KERN_INFO PFX "%s at %#3lx,", chipname, ioaddr);
  1207. /* In most chips, after a chip reset, the ethernet address is read from the
  1208. * station address PROM at the base address and programmed into the
  1209. * "Physical Address Registers" CSR12-14.
  1210. * As a precautionary measure, we read the PROM values and complain if
  1211. * they disagree with the CSRs. If they miscompare, and the PROM addr
  1212. * is valid, then the PROM addr is used.
  1213. */
  1214. for (i = 0; i < 3; i++) {
  1215. unsigned int val;
  1216. val = a->read_csr(ioaddr, i + 12) & 0x0ffff;
  1217. /* There may be endianness issues here. */
  1218. dev->dev_addr[2 * i] = val & 0x0ff;
  1219. dev->dev_addr[2 * i + 1] = (val >> 8) & 0x0ff;
  1220. }
  1221. /* read PROM address and compare with CSR address */
  1222. for (i = 0; i < 6; i++)
  1223. promaddr[i] = inb(ioaddr + i);
  1224. if (memcmp(promaddr, dev->dev_addr, 6)
  1225. || !is_valid_ether_addr(dev->dev_addr)) {
  1226. if (is_valid_ether_addr(promaddr)) {
  1227. if (pcnet32_debug & NETIF_MSG_PROBE) {
  1228. printk(" warning: CSR address invalid,\n");
  1229. printk(KERN_INFO
  1230. " using instead PROM address of");
  1231. }
  1232. memcpy(dev->dev_addr, promaddr, 6);
  1233. }
  1234. }
  1235. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  1236. /* if the ethernet address is not valid, force to 00:00:00:00:00:00 */
  1237. if (!is_valid_ether_addr(dev->perm_addr))
  1238. memset(dev->dev_addr, 0, sizeof(dev->dev_addr));
  1239. if (pcnet32_debug & NETIF_MSG_PROBE) {
  1240. for (i = 0; i < 6; i++)
  1241. printk(" %2.2x", dev->dev_addr[i]);
  1242. /* Version 0x2623 and 0x2624 */
  1243. if (((chip_version + 1) & 0xfffe) == 0x2624) {
  1244. i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
  1245. printk("\n" KERN_INFO " tx_start_pt(0x%04x):", i);
  1246. switch (i >> 10) {
  1247. case 0:
  1248. printk(" 20 bytes,");
  1249. break;
  1250. case 1:
  1251. printk(" 64 bytes,");
  1252. break;
  1253. case 2:
  1254. printk(" 128 bytes,");
  1255. break;
  1256. case 3:
  1257. printk("~220 bytes,");
  1258. break;
  1259. }
  1260. i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
  1261. printk(" BCR18(%x):", i & 0xffff);
  1262. if (i & (1 << 5))
  1263. printk("BurstWrEn ");
  1264. if (i & (1 << 6))
  1265. printk("BurstRdEn ");
  1266. if (i & (1 << 7))
  1267. printk("DWordIO ");
  1268. if (i & (1 << 11))
  1269. printk("NoUFlow ");
  1270. i = a->read_bcr(ioaddr, 25);
  1271. printk("\n" KERN_INFO " SRAMSIZE=0x%04x,", i << 8);
  1272. i = a->read_bcr(ioaddr, 26);
  1273. printk(" SRAM_BND=0x%04x,", i << 8);
  1274. i = a->read_bcr(ioaddr, 27);
  1275. if (i & (1 << 14))
  1276. printk("LowLatRx");
  1277. }
  1278. }
  1279. dev->base_addr = ioaddr;
  1280. /* pci_alloc_consistent returns page-aligned memory, so we do not have to check the alignment */
  1281. if ((lp =
  1282. pci_alloc_consistent(pdev, sizeof(*lp), &lp_dma_addr)) == NULL) {
  1283. if (pcnet32_debug & NETIF_MSG_PROBE)
  1284. printk(KERN_ERR PFX
  1285. "Consistent memory allocation failed.\n");
  1286. ret = -ENOMEM;
  1287. goto err_free_netdev;
  1288. }
  1289. memset(lp, 0, sizeof(*lp));
  1290. lp->dma_addr = lp_dma_addr;
  1291. lp->pci_dev = pdev;
  1292. spin_lock_init(&lp->lock);
  1293. SET_MODULE_OWNER(dev);
  1294. SET_NETDEV_DEV(dev, &pdev->dev);
  1295. dev->priv = lp;
  1296. lp->name = chipname;
  1297. lp->shared_irq = shared;
  1298. lp->tx_ring_size = TX_RING_SIZE; /* default tx ring size */
  1299. lp->rx_ring_size = RX_RING_SIZE; /* default rx ring size */
  1300. lp->tx_mod_mask = lp->tx_ring_size - 1;
  1301. lp->rx_mod_mask = lp->rx_ring_size - 1;
  1302. lp->tx_len_bits = (PCNET32_LOG_TX_BUFFERS << 12);
  1303. lp->rx_len_bits = (PCNET32_LOG_RX_BUFFERS << 4);
  1304. lp->mii_if.full_duplex = fdx;
  1305. lp->mii_if.phy_id_mask = 0x1f;
  1306. lp->mii_if.reg_num_mask = 0x1f;
  1307. lp->dxsuflo = dxsuflo;
  1308. lp->mii = mii;
  1309. lp->msg_enable = pcnet32_debug;
  1310. if ((cards_found >= MAX_UNITS)
  1311. || (options[cards_found] > sizeof(options_mapping)))
  1312. lp->options = PCNET32_PORT_ASEL;
  1313. else
  1314. lp->options = options_mapping[options[cards_found]];
  1315. lp->mii_if.dev = dev;
  1316. lp->mii_if.mdio_read = mdio_read;
  1317. lp->mii_if.mdio_write = mdio_write;
  1318. if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
  1319. ((cards_found >= MAX_UNITS) || full_duplex[cards_found]))
  1320. lp->options |= PCNET32_PORT_FD;
  1321. if (!a) {
  1322. if (pcnet32_debug & NETIF_MSG_PROBE)
  1323. printk(KERN_ERR PFX "No access methods\n");
  1324. ret = -ENODEV;
  1325. goto err_free_consistent;
  1326. }
  1327. lp->a = *a;
  1328. /* prior to register_netdev, dev->name is not yet correct */
  1329. if (pcnet32_alloc_ring(dev, pci_name(lp->pci_dev))) {
  1330. ret = -ENOMEM;
  1331. goto err_free_ring;
  1332. }
  1333. /* detect special T1/E1 WAN card by checking for MAC address */
  1334. if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0
  1335. && dev->dev_addr[2] == 0x75)
  1336. lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
  1337. lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
  1338. lp->init_block.tlen_rlen =
  1339. le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
  1340. for (i = 0; i < 6; i++)
  1341. lp->init_block.phys_addr[i] = dev->dev_addr[i];
  1342. lp->init_block.filter[0] = 0x00000000;
  1343. lp->init_block.filter[1] = 0x00000000;
  1344. lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
  1345. lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
  1346. /* switch pcnet32 to 32bit mode */
  1347. a->write_bcr(ioaddr, 20, 2);
  1348. a->write_csr(ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private,
  1349. init_block)) & 0xffff);
  1350. a->write_csr(ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private,
  1351. init_block)) >> 16);
  1352. if (pdev) { /* use the IRQ provided by PCI */
  1353. dev->irq = pdev->irq;
  1354. if (pcnet32_debug & NETIF_MSG_PROBE)
  1355. printk(" assigned IRQ %d.\n", dev->irq);
  1356. } else {
  1357. unsigned long irq_mask = probe_irq_on();
  1358. /*
  1359. * To auto-IRQ we enable the initialization-done and DMA error
  1360. * interrupts. For ISA boards we get a DMA error, but VLB and PCI
  1361. * boards will work.
  1362. */
  1363. /* Trigger an initialization just for the interrupt. */
  1364. a->write_csr(ioaddr, 0, 0x41);
  1365. mdelay(1);
  1366. dev->irq = probe_irq_off(irq_mask);
  1367. if (!dev->irq) {
  1368. if (pcnet32_debug & NETIF_MSG_PROBE)
  1369. printk(", failed to detect IRQ line.\n");
  1370. ret = -ENODEV;
  1371. goto err_free_ring;
  1372. }
  1373. if (pcnet32_debug & NETIF_MSG_PROBE)
  1374. printk(", probed IRQ %d.\n", dev->irq);
  1375. }
  1376. /* Set the mii phy_id so that we can query the link state */
  1377. if (lp->mii) {
  1378. /* lp->phycount and lp->phymask are set to 0 by memset above */
  1379. lp->mii_if.phy_id = ((lp->a.read_bcr(ioaddr, 33)) >> 5) & 0x1f;
  1380. /* scan for PHYs */
  1381. for (i = 0; i < PCNET32_MAX_PHYS; i++) {
  1382. unsigned short id1, id2;
  1383. id1 = mdio_read(dev, i, MII_PHYSID1);
  1384. if (id1 == 0xffff)
  1385. continue;
  1386. id2 = mdio_read(dev, i, MII_PHYSID2);
  1387. if (id2 == 0xffff)
  1388. continue;
  1389. if (i == 31 && ((chip_version + 1) & 0xfffe) == 0x2624)
  1390. continue; /* 79C971 & 79C972 have phantom phy at id 31 */
  1391. lp->phycount++;
  1392. lp->phymask |= (1 << i);
  1393. lp->mii_if.phy_id = i;
  1394. if (pcnet32_debug & NETIF_MSG_PROBE)
  1395. printk(KERN_INFO PFX
  1396. "Found PHY %04x:%04x at address %d.\n",
  1397. id1, id2, i);
  1398. }
  1399. lp->a.write_bcr(ioaddr, 33, (lp->mii_if.phy_id) << 5);
  1400. if (lp->phycount > 1) {
  1401. lp->options |= PCNET32_PORT_MII;
  1402. }
  1403. }
  1404. init_timer(&lp->watchdog_timer);
  1405. lp->watchdog_timer.data = (unsigned long)dev;
  1406. lp->watchdog_timer.function = (void *)&pcnet32_watchdog;
  1407. /* The PCNET32-specific entries in the device structure. */
  1408. dev->open = &pcnet32_open;
  1409. dev->hard_start_xmit = &pcnet32_start_xmit;
  1410. dev->stop = &pcnet32_close;
  1411. dev->get_stats = &pcnet32_get_stats;
  1412. dev->set_multicast_list = &pcnet32_set_multicast_list;
  1413. dev->do_ioctl = &pcnet32_ioctl;
  1414. dev->ethtool_ops = &pcnet32_ethtool_ops;
  1415. dev->tx_timeout = pcnet32_tx_timeout;
  1416. dev->watchdog_timeo = (5 * HZ);
  1417. #ifdef CONFIG_NET_POLL_CONTROLLER
  1418. dev->poll_controller = pcnet32_poll_controller;
  1419. #endif
  1420. /* Fill in the generic fields of the device structure. */
  1421. if (register_netdev(dev))
  1422. goto err_free_ring;
  1423. if (pdev) {
  1424. pci_set_drvdata(pdev, dev);
  1425. } else {
  1426. lp->next = pcnet32_dev;
  1427. pcnet32_dev = dev;
  1428. }
  1429. if (pcnet32_debug & NETIF_MSG_PROBE)
  1430. printk(KERN_INFO "%s: registered as %s\n", dev->name, lp->name);
  1431. cards_found++;
  1432. /* enable LED writes */
  1433. a->write_bcr(ioaddr, 2, a->read_bcr(ioaddr, 2) | 0x1000);
  1434. return 0;
  1435. err_free_ring:
  1436. pcnet32_free_ring(dev);
  1437. err_free_consistent:
  1438. pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
  1439. err_free_netdev:
  1440. free_netdev(dev);
  1441. err_release_region:
  1442. release_region(ioaddr, PCNET32_TOTAL_SIZE);
  1443. return ret;
  1444. }
  1445. /* if any allocation fails, caller must also call pcnet32_free_ring */
  1446. static int pcnet32_alloc_ring(struct net_device *dev, char *name)
  1447. {
  1448. struct pcnet32_private *lp = dev->priv;
  1449. lp->tx_ring = pci_alloc_consistent(lp->pci_dev,
  1450. sizeof(struct pcnet32_tx_head) *
  1451. lp->tx_ring_size,
  1452. &lp->tx_ring_dma_addr);
  1453. if (lp->tx_ring == NULL) {
  1454. if (netif_msg_drv(lp))
  1455. printk("\n" KERN_ERR PFX
  1456. "%s: Consistent memory allocation failed.\n",
  1457. name);
  1458. return -ENOMEM;
  1459. }
  1460. lp->rx_ring = pci_alloc_consistent(lp->pci_dev,
  1461. sizeof(struct pcnet32_rx_head) *
  1462. lp->rx_ring_size,
  1463. &lp->rx_ring_dma_addr);
  1464. if (lp->rx_ring == NULL) {
  1465. if (netif_msg_drv(lp))
  1466. printk("\n" KERN_ERR PFX
  1467. "%s: Consistent memory allocation failed.\n",
  1468. name);
  1469. return -ENOMEM;
  1470. }
  1471. lp->tx_dma_addr = kcalloc(lp->tx_ring_size, sizeof(dma_addr_t),
  1472. GFP_ATOMIC);
  1473. if (!lp->tx_dma_addr) {
  1474. if (netif_msg_drv(lp))
  1475. printk("\n" KERN_ERR PFX
  1476. "%s: Memory allocation failed.\n", name);
  1477. return -ENOMEM;
  1478. }
  1479. lp->rx_dma_addr = kcalloc(lp->rx_ring_size, sizeof(dma_addr_t),
  1480. GFP_ATOMIC);
  1481. if (!lp->rx_dma_addr) {
  1482. if (netif_msg_drv(lp))
  1483. printk("\n" KERN_ERR PFX
  1484. "%s: Memory allocation failed.\n", name);
  1485. return -ENOMEM;
  1486. }
  1487. lp->tx_skbuff = kcalloc(lp->tx_ring_size, sizeof(struct sk_buff *),
  1488. GFP_ATOMIC);
  1489. if (!lp->tx_skbuff) {
  1490. if (netif_msg_drv(lp))
  1491. printk("\n" KERN_ERR PFX
  1492. "%s: Memory allocation failed.\n", name);
  1493. return -ENOMEM;
  1494. }
  1495. lp->rx_skbuff = kcalloc(lp->rx_ring_size, sizeof(struct sk_buff *),
  1496. GFP_ATOMIC);
  1497. if (!lp->rx_skbuff) {
  1498. if (netif_msg_drv(lp))
  1499. printk("\n" KERN_ERR PFX
  1500. "%s: Memory allocation failed.\n", name);
  1501. return -ENOMEM;
  1502. }
  1503. return 0;
  1504. }
  1505. static void pcnet32_free_ring(struct net_device *dev)
  1506. {
  1507. struct pcnet32_private *lp = dev->priv;
  1508. kfree(lp->tx_skbuff);
  1509. lp->tx_skbuff = NULL;
  1510. kfree(lp->rx_skbuff);
  1511. lp->rx_skbuff = NULL;
  1512. kfree(lp->tx_dma_addr);
  1513. lp->tx_dma_addr = NULL;
  1514. kfree(lp->rx_dma_addr);
  1515. lp->rx_dma_addr = NULL;
  1516. if (lp->tx_ring) {
  1517. pci_free_consistent(lp->pci_dev,
  1518. sizeof(struct pcnet32_tx_head) *
  1519. lp->tx_ring_size, lp->tx_ring,
  1520. lp->tx_ring_dma_addr);
  1521. lp->tx_ring = NULL;
  1522. }
  1523. if (lp->rx_ring) {
  1524. pci_free_consistent(lp->pci_dev,
  1525. sizeof(struct pcnet32_rx_head) *
  1526. lp->rx_ring_size, lp->rx_ring,
  1527. lp->rx_ring_dma_addr);
  1528. lp->rx_ring = NULL;
  1529. }
  1530. }
  1531. static int pcnet32_open(struct net_device *dev)
  1532. {
  1533. struct pcnet32_private *lp = dev->priv;
  1534. unsigned long ioaddr = dev->base_addr;
  1535. u16 val;
  1536. int i;
  1537. int rc;
  1538. unsigned long flags;
  1539. if (request_irq(dev->irq, &pcnet32_interrupt,
  1540. lp->shared_irq ? IRQF_SHARED : 0, dev->name,
  1541. (void *)dev)) {
  1542. return -EAGAIN;
  1543. }
  1544. spin_lock_irqsave(&lp->lock, flags);
  1545. /* Check for a valid station address */
  1546. if (!is_valid_ether_addr(dev->dev_addr)) {
  1547. rc = -EINVAL;
  1548. goto err_free_irq;
  1549. }
  1550. /* Reset the PCNET32 */
  1551. lp->a.reset(ioaddr);
  1552. /* switch pcnet32 to 32bit mode */
  1553. lp->a.write_bcr(ioaddr, 20, 2);
  1554. if (netif_msg_ifup(lp))
  1555. printk(KERN_DEBUG
  1556. "%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
  1557. dev->name, dev->irq, (u32) (lp->tx_ring_dma_addr),
  1558. (u32) (lp->rx_ring_dma_addr),
  1559. (u32) (lp->dma_addr +
  1560. offsetof(struct pcnet32_private, init_block)));
  1561. /* set/reset autoselect bit */
  1562. val = lp->a.read_bcr(ioaddr, 2) & ~2;
  1563. if (lp->options & PCNET32_PORT_ASEL)
  1564. val |= 2;
  1565. lp->a.write_bcr(ioaddr, 2, val);
  1566. /* handle full duplex setting */
  1567. if (lp->mii_if.full_duplex) {
  1568. val = lp->a.read_bcr(ioaddr, 9) & ~3;
  1569. if (lp->options & PCNET32_PORT_FD) {
  1570. val |= 1;
  1571. if (lp->options == (PCNET32_PORT_FD | PCNET32_PORT_AUI))
  1572. val |= 2;
  1573. } else if (lp->options & PCNET32_PORT_ASEL) {
  1574. /* workaround of xSeries250, turn on for 79C975 only */
  1575. i = ((lp->a.read_csr(ioaddr, 88) |
  1576. (lp->a.
  1577. read_csr(ioaddr, 89) << 16)) >> 12) & 0xffff;
  1578. if (i == 0x2627)
  1579. val |= 3;
  1580. }
  1581. lp->a.write_bcr(ioaddr, 9, val);
  1582. }
  1583. /* set/reset GPSI bit in test register */
  1584. val = lp->a.read_csr(ioaddr, 124) & ~0x10;
  1585. if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
  1586. val |= 0x10;
  1587. lp->a.write_csr(ioaddr, 124, val);
  1588. /* Allied Telesyn AT 2700/2701 FX are 100Mbit only and do not negotiate */
  1589. if (lp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_AT &&
  1590. (lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2700FX ||
  1591. lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2701FX)) {
  1592. if (lp->options & PCNET32_PORT_ASEL) {
  1593. lp->options = PCNET32_PORT_FD | PCNET32_PORT_100;
  1594. if (netif_msg_link(lp))
  1595. printk(KERN_DEBUG
  1596. "%s: Setting 100Mb-Full Duplex.\n",
  1597. dev->name);
  1598. }
  1599. }
  1600. if (lp->phycount < 2) {
  1601. /*
  1602. * 24 Jun 2004 according AMD, in order to change the PHY,
  1603. * DANAS (or DISPM for 79C976) must be set; then select the speed,
  1604. * duplex, and/or enable auto negotiation, and clear DANAS
  1605. */
  1606. if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
  1607. lp->a.write_bcr(ioaddr, 32,
  1608. lp->a.read_bcr(ioaddr, 32) | 0x0080);
  1609. /* disable Auto Negotiation, set 10Mpbs, HD */
  1610. val = lp->a.read_bcr(ioaddr, 32) & ~0xb8;
  1611. if (lp->options & PCNET32_PORT_FD)
  1612. val |= 0x10;
  1613. if (lp->options & PCNET32_PORT_100)
  1614. val |= 0x08;
  1615. lp->a.write_bcr(ioaddr, 32, val);
  1616. } else {
  1617. if (lp->options & PCNET32_PORT_ASEL) {
  1618. lp->a.write_bcr(ioaddr, 32,
  1619. lp->a.read_bcr(ioaddr,
  1620. 32) | 0x0080);
  1621. /* enable auto negotiate, setup, disable fd */
  1622. val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
  1623. val |= 0x20;
  1624. lp->a.write_bcr(ioaddr, 32, val);
  1625. }
  1626. }
  1627. } else {
  1628. int first_phy = -1;
  1629. u16 bmcr;
  1630. u32 bcr9;
  1631. struct ethtool_cmd ecmd;
  1632. /*
  1633. * There is really no good other way to handle multiple PHYs
  1634. * other than turning off all automatics
  1635. */
  1636. val = lp->a.read_bcr(ioaddr, 2);
  1637. lp->a.write_bcr(ioaddr, 2, val & ~2);
  1638. val = lp->a.read_bcr(ioaddr, 32);
  1639. lp->a.write_bcr(ioaddr, 32, val & ~(1 << 7)); /* stop MII manager */
  1640. if (!(lp->options & PCNET32_PORT_ASEL)) {
  1641. /* setup ecmd */
  1642. ecmd.port = PORT_MII;
  1643. ecmd.transceiver = XCVR_INTERNAL;
  1644. ecmd.autoneg = AUTONEG_DISABLE;
  1645. ecmd.speed =
  1646. lp->
  1647. options & PCNET32_PORT_100 ? SPEED_100 : SPEED_10;
  1648. bcr9 = lp->a.read_bcr(ioaddr, 9);
  1649. if (lp->options & PCNET32_PORT_FD) {
  1650. ecmd.duplex = DUPLEX_FULL;
  1651. bcr9 |= (1 << 0);
  1652. } else {
  1653. ecmd.duplex = DUPLEX_HALF;
  1654. bcr9 |= ~(1 << 0);
  1655. }
  1656. lp->a.write_bcr(ioaddr, 9, bcr9);
  1657. }
  1658. for (i = 0; i < PCNET32_MAX_PHYS; i++) {
  1659. if (lp->phymask & (1 << i)) {
  1660. /* isolate all but the first PHY */
  1661. bmcr = mdio_read(dev, i, MII_BMCR);
  1662. if (first_phy == -1) {
  1663. first_phy = i;
  1664. mdio_write(dev, i, MII_BMCR,
  1665. bmcr & ~BMCR_ISOLATE);
  1666. } else {
  1667. mdio_write(dev, i, MII_BMCR,
  1668. bmcr | BMCR_ISOLATE);
  1669. }
  1670. /* use mii_ethtool_sset to setup PHY */
  1671. lp->mii_if.phy_id = i;
  1672. ecmd.phy_address = i;
  1673. if (lp->options & PCNET32_PORT_ASEL) {
  1674. mii_ethtool_gset(&lp->mii_if, &ecmd);
  1675. ecmd.autoneg = AUTONEG_ENABLE;
  1676. }
  1677. mii_ethtool_sset(&lp->mii_if, &ecmd);
  1678. }
  1679. }
  1680. lp->mii_if.phy_id = first_phy;
  1681. if (netif_msg_link(lp))
  1682. printk(KERN_INFO "%s: Using PHY number %d.\n",
  1683. dev->name, first_phy);
  1684. }
  1685. #ifdef DO_DXSUFLO
  1686. if (lp->dxsuflo) { /* Disable transmit stop on underflow */
  1687. val = lp->a.read_csr(ioaddr, 3);
  1688. val |= 0x40;
  1689. lp->a.write_csr(ioaddr, 3, val);
  1690. }
  1691. #endif
  1692. lp->init_block.mode =
  1693. le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  1694. pcnet32_load_multicast(dev);
  1695. if (pcnet32_init_ring(dev)) {
  1696. rc = -ENOMEM;
  1697. goto err_free_ring;
  1698. }
  1699. /* Re-initialize the PCNET32, and start it when done. */
  1700. lp->a.write_csr(ioaddr, 1, (lp->dma_addr +
  1701. offsetof(struct pcnet32_private,
  1702. init_block)) & 0xffff);
  1703. lp->a.write_csr(ioaddr, 2,
  1704. (lp->dma_addr +
  1705. offsetof(struct pcnet32_private, init_block)) >> 16);
  1706. lp->a.write_csr(ioaddr, 4, 0x0915);
  1707. lp->a.write_csr(ioaddr, 0, 0x0001);
  1708. netif_start_queue(dev);
  1709. /* Print the link status and start the watchdog */
  1710. pcnet32_check_media(dev, 1);
  1711. mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
  1712. i = 0;
  1713. while (i++ < 100)
  1714. if (lp->a.read_csr(ioaddr, 0) & 0x0100)
  1715. break;
  1716. /*
  1717. * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
  1718. * reports that doing so triggers a bug in the '974.
  1719. */
  1720. lp->a.write_csr(ioaddr, 0, 0x0042);
  1721. if (netif_msg_ifup(lp))
  1722. printk(KERN_DEBUG
  1723. "%s: pcnet32 open after %d ticks, init block %#x csr0 %4.4x.\n",
  1724. dev->name, i,
  1725. (u32) (lp->dma_addr +
  1726. offsetof(struct pcnet32_private, init_block)),
  1727. lp->a.read_csr(ioaddr, 0));
  1728. spin_unlock_irqrestore(&lp->lock, flags);
  1729. return 0; /* Always succeed */
  1730. err_free_ring:
  1731. /* free any allocated skbuffs */
  1732. for (i = 0; i < lp->rx_ring_size; i++) {
  1733. lp->rx_ring[i].status = 0;
  1734. if (lp->rx_skbuff[i]) {
  1735. pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i],
  1736. PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
  1737. dev_kfree_skb(lp->rx_skbuff[i]);
  1738. }
  1739. lp->rx_skbuff[i] = NULL;
  1740. lp->rx_dma_addr[i] = 0;
  1741. }
  1742. /*
  1743. * Switch back to 16bit mode to avoid problems with dumb
  1744. * DOS packet driver after a warm reboot
  1745. */
  1746. lp->a.write_bcr(ioaddr, 20, 4);
  1747. err_free_irq:
  1748. spin_unlock_irqrestore(&lp->lock, flags);
  1749. free_irq(dev->irq, dev);
  1750. return rc;
  1751. }
  1752. /*
  1753. * The LANCE has been halted for one reason or another (busmaster memory
  1754. * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
  1755. * etc.). Modern LANCE variants always reload their ring-buffer
  1756. * configuration when restarted, so we must reinitialize our ring
  1757. * context before restarting. As part of this reinitialization,
  1758. * find all packets still on the Tx ring and pretend that they had been
  1759. * sent (in effect, drop the packets on the floor) - the higher-level
  1760. * protocols will time out and retransmit. It'd be better to shuffle
  1761. * these skbs to a temp list and then actually re-Tx them after
  1762. * restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
  1763. */
  1764. static void pcnet32_purge_tx_ring(struct net_device *dev)
  1765. {
  1766. struct pcnet32_private *lp = dev->priv;
  1767. int i;
  1768. for (i = 0; i < lp->tx_ring_size; i++) {
  1769. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  1770. wmb(); /* Make sure adapter sees owner change */
  1771. if (lp->tx_skbuff[i]) {
  1772. pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
  1773. lp->tx_skbuff[i]->len,
  1774. PCI_DMA_TODEVICE);
  1775. dev_kfree_skb_any(lp->tx_skbuff[i]);
  1776. }
  1777. lp->tx_skbuff[i] = NULL;
  1778. lp->tx_dma_addr[i] = 0;
  1779. }
  1780. }
  1781. /* Initialize the PCNET32 Rx and Tx rings. */
  1782. static int pcnet32_init_ring(struct net_device *dev)
  1783. {
  1784. struct pcnet32_private *lp = dev->priv;
  1785. int i;
  1786. lp->tx_full = 0;
  1787. lp->cur_rx = lp->cur_tx = 0;
  1788. lp->dirty_rx = lp->dirty_tx = 0;
  1789. for (i = 0; i < lp->rx_ring_size; i++) {
  1790. struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
  1791. if (rx_skbuff == NULL) {
  1792. if (!
  1793. (rx_skbuff = lp->rx_skbuff[i] =
  1794. dev_alloc_skb(PKT_BUF_SZ))) {
  1795. /* there is not much, we can do at this point */
  1796. if (pcnet32_debug & NETIF_MSG_DRV)
  1797. printk(KERN_ERR
  1798. "%s: pcnet32_init_ring dev_alloc_skb failed.\n",
  1799. dev->name);
  1800. return -1;
  1801. }
  1802. skb_reserve(rx_skbuff, 2);
  1803. }
  1804. rmb();
  1805. if (lp->rx_dma_addr[i] == 0)
  1806. lp->rx_dma_addr[i] =
  1807. pci_map_single(lp->pci_dev, rx_skbuff->data,
  1808. PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
  1809. lp->rx_ring[i].base = (u32) le32_to_cpu(lp->rx_dma_addr[i]);
  1810. lp->rx_ring[i].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
  1811. wmb(); /* Make sure owner changes after all others are visible */
  1812. lp->rx_ring[i].status = le16_to_cpu(0x8000);
  1813. }
  1814. /* The Tx buffer address is filled in as needed, but we do need to clear
  1815. * the upper ownership bit. */
  1816. for (i = 0; i < lp->tx_ring_size; i++) {
  1817. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  1818. wmb(); /* Make sure adapter sees owner change */
  1819. lp->tx_ring[i].base = 0;
  1820. lp->tx_dma_addr[i] = 0;
  1821. }
  1822. lp->init_block.tlen_rlen =
  1823. le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
  1824. for (i = 0; i < 6; i++)
  1825. lp->init_block.phys_addr[i] = dev->dev_addr[i];
  1826. lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
  1827. lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
  1828. wmb(); /* Make sure all changes are visible */
  1829. return 0;
  1830. }
  1831. /* the pcnet32 has been issued a stop or reset. Wait for the stop bit
  1832. * then flush the pending transmit operations, re-initialize the ring,
  1833. * and tell the chip to initialize.
  1834. */
  1835. static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
  1836. {
  1837. struct pcnet32_private *lp = dev->priv;
  1838. unsigned long ioaddr = dev->base_addr;
  1839. int i;
  1840. /* wait for stop */
  1841. for (i = 0; i < 100; i++)
  1842. if (lp->a.read_csr(ioaddr, 0) & 0x0004)
  1843. break;
  1844. if (i >= 100 && netif_msg_drv(lp))
  1845. printk(KERN_ERR
  1846. "%s: pcnet32_restart timed out waiting for stop.\n",
  1847. dev->name);
  1848. pcnet32_purge_tx_ring(dev);
  1849. if (pcnet32_init_ring(dev))
  1850. return;
  1851. /* ReInit Ring */
  1852. lp->a.write_csr(ioaddr, 0, 1);
  1853. i = 0;
  1854. while (i++ < 1000)
  1855. if (lp->a.read_csr(ioaddr, 0) & 0x0100)
  1856. break;
  1857. lp->a.write_csr(ioaddr, 0, csr0_bits);
  1858. }
  1859. static void pcnet32_tx_timeout(struct net_device *dev)
  1860. {
  1861. struct pcnet32_private *lp = dev->priv;
  1862. unsigned long ioaddr = dev->base_addr, flags;
  1863. spin_lock_irqsave(&lp->lock, flags);
  1864. /* Transmitter timeout, serious problems. */
  1865. if (pcnet32_debug & NETIF_MSG_DRV)
  1866. printk(KERN_ERR
  1867. "%s: transmit timed out, status %4.4x, resetting.\n",
  1868. dev->name, lp->a.read_csr(ioaddr, 0));
  1869. lp->a.write_csr(ioaddr, 0, 0x0004);
  1870. lp->stats.tx_errors++;
  1871. if (netif_msg_tx_err(lp)) {
  1872. int i;
  1873. printk(KERN_DEBUG
  1874. " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
  1875. lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
  1876. lp->cur_rx);
  1877. for (i = 0; i < lp->rx_ring_size; i++)
  1878. printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
  1879. le32_to_cpu(lp->rx_ring[i].base),
  1880. (-le16_to_cpu(lp->rx_ring[i].buf_length)) &
  1881. 0xffff, le32_to_cpu(lp->rx_ring[i].msg_length),
  1882. le16_to_cpu(lp->rx_ring[i].status));
  1883. for (i = 0; i < lp->tx_ring_size; i++)
  1884. printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
  1885. le32_to_cpu(lp->tx_ring[i].base),
  1886. (-le16_to_cpu(lp->tx_ring[i].length)) & 0xffff,
  1887. le32_to_cpu(lp->tx_ring[i].misc),
  1888. le16_to_cpu(lp->tx_ring[i].status));
  1889. printk("\n");
  1890. }
  1891. pcnet32_restart(dev, 0x0042);
  1892. dev->trans_start = jiffies;
  1893. netif_wake_queue(dev);
  1894. spin_unlock_irqrestore(&lp->lock, flags);
  1895. }
  1896. static int pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1897. {
  1898. struct pcnet32_private *lp = dev->priv;
  1899. unsigned long ioaddr = dev->base_addr;
  1900. u16 status;
  1901. int entry;
  1902. unsigned long flags;
  1903. spin_lock_irqsave(&lp->lock, flags);
  1904. if (netif_msg_tx_queued(lp)) {
  1905. printk(KERN_DEBUG
  1906. "%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
  1907. dev->name, lp->a.read_csr(ioaddr, 0));
  1908. }
  1909. /* Default status -- will not enable Successful-TxDone
  1910. * interrupt when that option is available to us.
  1911. */
  1912. status = 0x8300;
  1913. /* Fill in a Tx ring entry */
  1914. /* Mask to ring buffer boundary. */
  1915. entry = lp->cur_tx & lp->tx_mod_mask;
  1916. /* Caution: the write order is important here, set the status
  1917. * with the "ownership" bits last. */
  1918. lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
  1919. lp->tx_ring[entry].misc = 0x00000000;
  1920. lp->tx_skbuff[entry] = skb;
  1921. lp->tx_dma_addr[entry] =
  1922. pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
  1923. lp->tx_ring[entry].base = (u32) le32_to_cpu(lp->tx_dma_addr[entry]);
  1924. wmb(); /* Make sure owner changes after all others are visible */
  1925. lp->tx_ring[entry].status = le16_to_cpu(status);
  1926. lp->cur_tx++;
  1927. lp->stats.tx_bytes += skb->len;
  1928. /* Trigger an immediate send poll. */
  1929. lp->a.write_csr(ioaddr, 0, 0x0048);
  1930. dev->trans_start = jiffies;
  1931. if (lp->tx_ring[(entry + 1) & lp->tx_mod_mask].base != 0) {
  1932. lp->tx_full = 1;
  1933. netif_stop_queue(dev);
  1934. }
  1935. spin_unlock_irqrestore(&lp->lock, flags);
  1936. return 0;
  1937. }
  1938. /* The PCNET32 interrupt handler. */
  1939. static irqreturn_t
  1940. pcnet32_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1941. {
  1942. struct net_device *dev = dev_id;
  1943. struct pcnet32_private *lp;
  1944. unsigned long ioaddr;
  1945. u16 csr0, rap;
  1946. int boguscnt = max_interrupt_work;
  1947. int must_restart;
  1948. if (!dev) {
  1949. if (pcnet32_debug & NETIF_MSG_INTR)
  1950. printk(KERN_DEBUG "%s(): irq %d for unknown device\n",
  1951. __FUNCTION__, irq);
  1952. return IRQ_NONE;
  1953. }
  1954. ioaddr = dev->base_addr;
  1955. lp = dev->priv;
  1956. spin_lock(&lp->lock);
  1957. rap = lp->a.read_rap(ioaddr);
  1958. while ((csr0 = lp->a.read_csr(ioaddr, 0)) & 0x8f00 && --boguscnt >= 0) {
  1959. if (csr0 == 0xffff) {
  1960. break; /* PCMCIA remove happened */
  1961. }
  1962. /* Acknowledge all of the current interrupt sources ASAP. */
  1963. lp->a.write_csr(ioaddr, 0, csr0 & ~0x004f);
  1964. must_restart = 0;
  1965. if (netif_msg_intr(lp))
  1966. printk(KERN_DEBUG
  1967. "%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
  1968. dev->name, csr0, lp->a.read_csr(ioaddr, 0));
  1969. if (csr0 & 0x0400) /* Rx interrupt */
  1970. pcnet32_rx(dev);
  1971. if (csr0 & 0x0200) { /* Tx-done interrupt */
  1972. unsigned int dirty_tx = lp->dirty_tx;
  1973. int delta;
  1974. while (dirty_tx != lp->cur_tx) {
  1975. int entry = dirty_tx & lp->tx_mod_mask;
  1976. int status =
  1977. (short)le16_to_cpu(lp->tx_ring[entry].
  1978. status);
  1979. if (status < 0)
  1980. break; /* It still hasn't been Txed */
  1981. lp->tx_ring[entry].base = 0;
  1982. if (status & 0x4000) {
  1983. /* There was an major error, log it. */
  1984. int err_status =
  1985. le32_to_cpu(lp->tx_ring[entry].
  1986. misc);
  1987. lp->stats.tx_errors++;
  1988. if (netif_msg_tx_err(lp))
  1989. printk(KERN_ERR
  1990. "%s: Tx error status=%04x err_status=%08x\n",
  1991. dev->name, status,
  1992. err_status);
  1993. if (err_status & 0x04000000)
  1994. lp->stats.tx_aborted_errors++;
  1995. if (err_status & 0x08000000)
  1996. lp->stats.tx_carrier_errors++;
  1997. if (err_status & 0x10000000)
  1998. lp->stats.tx_window_errors++;
  1999. #ifndef DO_DXSUFLO
  2000. if (err_status & 0x40000000) {
  2001. lp->stats.tx_fifo_errors++;
  2002. /* Ackk! On FIFO errors the Tx unit is turned off! */
  2003. /* Remove this verbosity later! */
  2004. if (netif_msg_tx_err(lp))
  2005. printk(KERN_ERR
  2006. "%s: Tx FIFO error! CSR0=%4.4x\n",
  2007. dev->name, csr0);
  2008. must_restart = 1;
  2009. }
  2010. #else
  2011. if (err_status & 0x40000000) {
  2012. lp->stats.tx_fifo_errors++;
  2013. if (!lp->dxsuflo) { /* If controller doesn't recover ... */
  2014. /* Ackk! On FIFO errors the Tx unit is turned off! */
  2015. /* Remove this verbosity later! */
  2016. if (netif_msg_tx_err
  2017. (lp))
  2018. printk(KERN_ERR
  2019. "%s: Tx FIFO error! CSR0=%4.4x\n",
  2020. dev->
  2021. name,
  2022. csr0);
  2023. must_restart = 1;
  2024. }
  2025. }
  2026. #endif
  2027. } else {
  2028. if (status & 0x1800)
  2029. lp->stats.collisions++;
  2030. lp->stats.tx_packets++;
  2031. }
  2032. /* We must free the original skb */
  2033. if (lp->tx_skbuff[entry]) {
  2034. pci_unmap_single(lp->pci_dev,
  2035. lp->tx_dma_addr[entry],
  2036. lp->tx_skbuff[entry]->
  2037. len, PCI_DMA_TODEVICE);
  2038. dev_kfree_skb_irq(lp->tx_skbuff[entry]);
  2039. lp->tx_skbuff[entry] = NULL;
  2040. lp->tx_dma_addr[entry] = 0;
  2041. }
  2042. dirty_tx++;
  2043. }
  2044. delta =
  2045. (lp->cur_tx - dirty_tx) & (lp->tx_mod_mask +
  2046. lp->tx_ring_size);
  2047. if (delta > lp->tx_ring_size) {
  2048. if (netif_msg_drv(lp))
  2049. printk(KERN_ERR
  2050. "%s: out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
  2051. dev->name, dirty_tx, lp->cur_tx,
  2052. lp->tx_full);
  2053. dirty_tx += lp->tx_ring_size;
  2054. delta -= lp->tx_ring_size;
  2055. }
  2056. if (lp->tx_full &&
  2057. netif_queue_stopped(dev) &&
  2058. delta < lp->tx_ring_size - 2) {
  2059. /* The ring is no longer full, clear tbusy. */
  2060. lp->tx_full = 0;
  2061. netif_wake_queue(dev);
  2062. }
  2063. lp->dirty_tx = dirty_tx;
  2064. }
  2065. /* Log misc errors. */
  2066. if (csr0 & 0x4000)
  2067. lp->stats.tx_errors++; /* Tx babble. */
  2068. if (csr0 & 0x1000) {
  2069. /*
  2070. * this happens when our receive ring is full. This shouldn't
  2071. * be a problem as we will see normal rx interrupts for the frames
  2072. * in the receive ring. But there are some PCI chipsets (I can
  2073. * reproduce this on SP3G with Intel saturn chipset) which have
  2074. * sometimes problems and will fill up the receive ring with
  2075. * error descriptors. In this situation we don't get a rx
  2076. * interrupt, but a missed frame interrupt sooner or later.
  2077. * So we try to clean up our receive ring here.
  2078. */
  2079. pcnet32_rx(dev);
  2080. lp->stats.rx_errors++; /* Missed a Rx frame. */
  2081. }
  2082. if (csr0 & 0x0800) {
  2083. if (netif_msg_drv(lp))
  2084. printk(KERN_ERR
  2085. "%s: Bus master arbitration failure, status %4.4x.\n",
  2086. dev->name, csr0);
  2087. /* unlike for the lance, there is no restart needed */
  2088. }
  2089. if (must_restart) {
  2090. /* reset the chip to clear the error condition, then restart */
  2091. lp->a.reset(ioaddr);
  2092. lp->a.write_csr(ioaddr, 4, 0x0915);
  2093. pcnet32_restart(dev, 0x0002);
  2094. netif_wake_queue(dev);
  2095. }
  2096. }
  2097. /* Set interrupt enable. */
  2098. lp->a.write_csr(ioaddr, 0, 0x0040);
  2099. lp->a.write_rap(ioaddr, rap);
  2100. if (netif_msg_intr(lp))
  2101. printk(KERN_DEBUG "%s: exiting interrupt, csr0=%#4.4x.\n",
  2102. dev->name, lp->a.read_csr(ioaddr, 0));
  2103. spin_unlock(&lp->lock);
  2104. return IRQ_HANDLED;
  2105. }
  2106. static int pcnet32_rx(struct net_device *dev)
  2107. {
  2108. struct pcnet32_private *lp = dev->priv;
  2109. int entry = lp->cur_rx & lp->rx_mod_mask;
  2110. int boguscnt = lp->rx_ring_size / 2;
  2111. /* If we own the next entry, it's a new packet. Send it up. */
  2112. while ((short)le16_to_cpu(lp->rx_ring[entry].status) >= 0) {
  2113. int status = (short)le16_to_cpu(lp->rx_ring[entry].status) >> 8;
  2114. if (status != 0x03) { /* There was an error. */
  2115. /*
  2116. * There is a tricky error noted by John Murphy,
  2117. * <murf@perftech.com> to Russ Nelson: Even with full-sized
  2118. * buffers it's possible for a jabber packet to use two
  2119. * buffers, with only the last correctly noting the error.
  2120. */
  2121. if (status & 0x01) /* Only count a general error at the */
  2122. lp->stats.rx_errors++; /* end of a packet. */
  2123. if (status & 0x20)
  2124. lp->stats.rx_frame_errors++;
  2125. if (status & 0x10)
  2126. lp->stats.rx_over_errors++;
  2127. if (status & 0x08)
  2128. lp->stats.rx_crc_errors++;
  2129. if (status & 0x04)
  2130. lp->stats.rx_fifo_errors++;
  2131. lp->rx_ring[entry].status &= le16_to_cpu(0x03ff);
  2132. } else {
  2133. /* Malloc up new buffer, compatible with net-2e. */
  2134. short pkt_len =
  2135. (le32_to_cpu(lp->rx_ring[entry].msg_length) & 0xfff)
  2136. - 4;
  2137. struct sk_buff *skb;
  2138. /* Discard oversize frames. */
  2139. if (unlikely(pkt_len > PKT_BUF_SZ - 2)) {
  2140. if (netif_msg_drv(lp))
  2141. printk(KERN_ERR
  2142. "%s: Impossible packet size %d!\n",
  2143. dev->name, pkt_len);
  2144. lp->stats.rx_errors++;
  2145. } else if (pkt_len < 60) {
  2146. if (netif_msg_rx_err(lp))
  2147. printk(KERN_ERR "%s: Runt packet!\n",
  2148. dev->name);
  2149. lp->stats.rx_errors++;
  2150. } else {
  2151. int rx_in_place = 0;
  2152. if (pkt_len > rx_copybreak) {
  2153. struct sk_buff *newskb;
  2154. if ((newskb =
  2155. dev_alloc_skb(PKT_BUF_SZ))) {
  2156. skb_reserve(newskb, 2);
  2157. skb = lp->rx_skbuff[entry];
  2158. pci_unmap_single(lp->pci_dev,
  2159. lp->
  2160. rx_dma_addr
  2161. [entry],
  2162. PKT_BUF_SZ - 2,
  2163. PCI_DMA_FROMDEVICE);
  2164. skb_put(skb, pkt_len);
  2165. lp->rx_skbuff[entry] = newskb;
  2166. newskb->dev = dev;
  2167. lp->rx_dma_addr[entry] =
  2168. pci_map_single(lp->pci_dev,
  2169. newskb->data,
  2170. PKT_BUF_SZ -
  2171. 2,
  2172. PCI_DMA_FROMDEVICE);
  2173. lp->rx_ring[entry].base =
  2174. le32_to_cpu(lp->
  2175. rx_dma_addr
  2176. [entry]);
  2177. rx_in_place = 1;
  2178. } else
  2179. skb = NULL;
  2180. } else {
  2181. skb = dev_alloc_skb(pkt_len + 2);
  2182. }
  2183. if (skb == NULL) {
  2184. int i;
  2185. if (netif_msg_drv(lp))
  2186. printk(KERN_ERR
  2187. "%s: Memory squeeze, deferring packet.\n",
  2188. dev->name);
  2189. for (i = 0; i < lp->rx_ring_size; i++)
  2190. if ((short)
  2191. le16_to_cpu(lp->
  2192. rx_ring[(entry +
  2193. i)
  2194. & lp->
  2195. rx_mod_mask].
  2196. status) < 0)
  2197. break;
  2198. if (i > lp->rx_ring_size - 2) {
  2199. lp->stats.rx_dropped++;
  2200. lp->rx_ring[entry].status |=
  2201. le16_to_cpu(0x8000);
  2202. wmb(); /* Make sure adapter sees owner change */
  2203. lp->cur_rx++;
  2204. }
  2205. break;
  2206. }
  2207. skb->dev = dev;
  2208. if (!rx_in_place) {
  2209. skb_reserve(skb, 2); /* 16 byte align */
  2210. skb_put(skb, pkt_len); /* Make room */
  2211. pci_dma_sync_single_for_cpu(lp->pci_dev,
  2212. lp->
  2213. rx_dma_addr
  2214. [entry],
  2215. PKT_BUF_SZ -
  2216. 2,
  2217. PCI_DMA_FROMDEVICE);
  2218. eth_copy_and_sum(skb,
  2219. (unsigned char *)(lp->
  2220. rx_skbuff
  2221. [entry]->
  2222. data),
  2223. pkt_len, 0);
  2224. pci_dma_sync_single_for_device(lp->
  2225. pci_dev,
  2226. lp->
  2227. rx_dma_addr
  2228. [entry],
  2229. PKT_BUF_SZ
  2230. - 2,
  2231. PCI_DMA_FROMDEVICE);
  2232. }
  2233. lp->stats.rx_bytes += skb->len;
  2234. skb->protocol = eth_type_trans(skb, dev);
  2235. netif_rx(skb);
  2236. dev->last_rx = jiffies;
  2237. lp->stats.rx_packets++;
  2238. }
  2239. }
  2240. /*
  2241. * The docs say that the buffer length isn't touched, but Andrew Boyd
  2242. * of QNX reports that some revs of the 79C965 clear it.
  2243. */
  2244. lp->rx_ring[entry].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
  2245. wmb(); /* Make sure owner changes after all others are visible */
  2246. lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
  2247. entry = (++lp->cur_rx) & lp->rx_mod_mask;
  2248. if (--boguscnt <= 0)
  2249. break; /* don't stay in loop forever */
  2250. }
  2251. return 0;
  2252. }
  2253. static int pcnet32_close(struct net_device *dev)
  2254. {
  2255. unsigned long ioaddr = dev->base_addr;
  2256. struct pcnet32_private *lp = dev->priv;
  2257. int i;
  2258. unsigned long flags;
  2259. del_timer_sync(&lp->watchdog_timer);
  2260. netif_stop_queue(dev);
  2261. spin_lock_irqsave(&lp->lock, flags);
  2262. lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
  2263. if (netif_msg_ifdown(lp))
  2264. printk(KERN_DEBUG
  2265. "%s: Shutting down ethercard, status was %2.2x.\n",
  2266. dev->name, lp->a.read_csr(ioaddr, 0));
  2267. /* We stop the PCNET32 here -- it occasionally polls memory if we don't. */
  2268. lp->a.write_csr(ioaddr, 0, 0x0004);
  2269. /*
  2270. * Switch back to 16bit mode to avoid problems with dumb
  2271. * DOS packet driver after a warm reboot
  2272. */
  2273. lp->a.write_bcr(ioaddr, 20, 4);
  2274. spin_unlock_irqrestore(&lp->lock, flags);
  2275. free_irq(dev->irq, dev);
  2276. spin_lock_irqsave(&lp->lock, flags);
  2277. /* free all allocated skbuffs */
  2278. for (i = 0; i < lp->rx_ring_size; i++) {
  2279. lp->rx_ring[i].status = 0;
  2280. wmb(); /* Make sure adapter sees owner change */
  2281. if (lp->rx_skbuff[i]) {
  2282. pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i],
  2283. PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
  2284. dev_kfree_skb(lp->rx_skbuff[i]);
  2285. }
  2286. lp->rx_skbuff[i] = NULL;
  2287. lp->rx_dma_addr[i] = 0;
  2288. }
  2289. for (i = 0; i < lp->tx_ring_size; i++) {
  2290. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  2291. wmb(); /* Make sure adapter sees owner change */
  2292. if (lp->tx_skbuff[i]) {
  2293. pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
  2294. lp->tx_skbuff[i]->len,
  2295. PCI_DMA_TODEVICE);
  2296. dev_kfree_skb(lp->tx_skbuff[i]);
  2297. }
  2298. lp->tx_skbuff[i] = NULL;
  2299. lp->tx_dma_addr[i] = 0;
  2300. }
  2301. spin_unlock_irqrestore(&lp->lock, flags);
  2302. return 0;
  2303. }
  2304. static struct net_device_stats *pcnet32_get_stats(struct net_device *dev)
  2305. {
  2306. struct pcnet32_private *lp = dev->priv;
  2307. unsigned long ioaddr = dev->base_addr;
  2308. u16 saved_addr;
  2309. unsigned long flags;
  2310. spin_lock_irqsave(&lp->lock, flags);
  2311. saved_addr = lp->a.read_rap(ioaddr);
  2312. lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
  2313. lp->a.write_rap(ioaddr, saved_addr);
  2314. spin_unlock_irqrestore(&lp->lock, flags);
  2315. return &lp->stats;
  2316. }
  2317. /* taken from the sunlance driver, which it took from the depca driver */
  2318. static void pcnet32_load_multicast(struct net_device *dev)
  2319. {
  2320. struct pcnet32_private *lp = dev->priv;
  2321. volatile struct pcnet32_init_block *ib = &lp->init_block;
  2322. volatile u16 *mcast_table = (u16 *) & ib->filter;
  2323. struct dev_mc_list *dmi = dev->mc_list;
  2324. char *addrs;
  2325. int i;
  2326. u32 crc;
  2327. /* set all multicast bits */
  2328. if (dev->flags & IFF_ALLMULTI) {
  2329. ib->filter[0] = 0xffffffff;
  2330. ib->filter[1] = 0xffffffff;
  2331. return;
  2332. }
  2333. /* clear the multicast filter */
  2334. ib->filter[0] = 0;
  2335. ib->filter[1] = 0;
  2336. /* Add addresses */
  2337. for (i = 0; i < dev->mc_count; i++) {
  2338. addrs = dmi->dmi_addr;
  2339. dmi = dmi->next;
  2340. /* multicast address? */
  2341. if (!(*addrs & 1))
  2342. continue;
  2343. crc = ether_crc_le(6, addrs);
  2344. crc = crc >> 26;
  2345. mcast_table[crc >> 4] =
  2346. le16_to_cpu(le16_to_cpu(mcast_table[crc >> 4]) |
  2347. (1 << (crc & 0xf)));
  2348. }
  2349. return;
  2350. }
  2351. /*
  2352. * Set or clear the multicast filter for this adaptor.
  2353. */
  2354. static void pcnet32_set_multicast_list(struct net_device *dev)
  2355. {
  2356. unsigned long ioaddr = dev->base_addr, flags;
  2357. struct pcnet32_private *lp = dev->priv;
  2358. spin_lock_irqsave(&lp->lock, flags);
  2359. if (dev->flags & IFF_PROMISC) {
  2360. /* Log any net taps. */
  2361. if (netif_msg_hw(lp))
  2362. printk(KERN_INFO "%s: Promiscuous mode enabled.\n",
  2363. dev->name);
  2364. lp->init_block.mode =
  2365. le16_to_cpu(0x8000 | (lp->options & PCNET32_PORT_PORTSEL) <<
  2366. 7);
  2367. } else {
  2368. lp->init_block.mode =
  2369. le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  2370. pcnet32_load_multicast(dev);
  2371. }
  2372. lp->a.write_csr(ioaddr, 0, 0x0004); /* Temporarily stop the lance. */
  2373. pcnet32_restart(dev, 0x0042); /* Resume normal operation */
  2374. netif_wake_queue(dev);
  2375. spin_unlock_irqrestore(&lp->lock, flags);
  2376. }
  2377. /* This routine assumes that the lp->lock is held */
  2378. static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
  2379. {
  2380. struct pcnet32_private *lp = dev->priv;
  2381. unsigned long ioaddr = dev->base_addr;
  2382. u16 val_out;
  2383. if (!lp->mii)
  2384. return 0;
  2385. lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  2386. val_out = lp->a.read_bcr(ioaddr, 34);
  2387. return val_out;
  2388. }
  2389. /* This routine assumes that the lp->lock is held */
  2390. static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
  2391. {
  2392. struct pcnet32_private *lp = dev->priv;
  2393. unsigned long ioaddr = dev->base_addr;
  2394. if (!lp->mii)
  2395. return;
  2396. lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  2397. lp->a.write_bcr(ioaddr, 34, val);
  2398. }
  2399. static int pcnet32_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2400. {
  2401. struct pcnet32_private *lp = dev->priv;
  2402. int rc;
  2403. unsigned long flags;
  2404. /* SIOC[GS]MIIxxx ioctls */
  2405. if (lp->mii) {
  2406. spin_lock_irqsave(&lp->lock, flags);
  2407. rc = generic_mii_ioctl(&lp->mii_if, if_mii(rq), cmd, NULL);
  2408. spin_unlock_irqrestore(&lp->lock, flags);
  2409. } else {
  2410. rc = -EOPNOTSUPP;
  2411. }
  2412. return rc;
  2413. }
  2414. static int pcnet32_check_otherphy(struct net_device *dev)
  2415. {
  2416. struct pcnet32_private *lp = dev->priv;
  2417. struct mii_if_info mii = lp->mii_if;
  2418. u16 bmcr;
  2419. int i;
  2420. for (i = 0; i < PCNET32_MAX_PHYS; i++) {
  2421. if (i == lp->mii_if.phy_id)
  2422. continue; /* skip active phy */
  2423. if (lp->phymask & (1 << i)) {
  2424. mii.phy_id = i;
  2425. if (mii_link_ok(&mii)) {
  2426. /* found PHY with active link */
  2427. if (netif_msg_link(lp))
  2428. printk(KERN_INFO
  2429. "%s: Using PHY number %d.\n",
  2430. dev->name, i);
  2431. /* isolate inactive phy */
  2432. bmcr =
  2433. mdio_read(dev, lp->mii_if.phy_id, MII_BMCR);
  2434. mdio_write(dev, lp->mii_if.phy_id, MII_BMCR,
  2435. bmcr | BMCR_ISOLATE);
  2436. /* de-isolate new phy */
  2437. bmcr = mdio_read(dev, i, MII_BMCR);
  2438. mdio_write(dev, i, MII_BMCR,
  2439. bmcr & ~BMCR_ISOLATE);
  2440. /* set new phy address */
  2441. lp->mii_if.phy_id = i;
  2442. return 1;
  2443. }
  2444. }
  2445. }
  2446. return 0;
  2447. }
  2448. /*
  2449. * Show the status of the media. Similar to mii_check_media however it
  2450. * correctly shows the link speed for all (tested) pcnet32 variants.
  2451. * Devices with no mii just report link state without speed.
  2452. *
  2453. * Caller is assumed to hold and release the lp->lock.
  2454. */
  2455. static void pcnet32_check_media(struct net_device *dev, int verbose)
  2456. {
  2457. struct pcnet32_private *lp = dev->priv;
  2458. int curr_link;
  2459. int prev_link = netif_carrier_ok(dev) ? 1 : 0;
  2460. u32 bcr9;
  2461. if (lp->mii) {
  2462. curr_link = mii_link_ok(&lp->mii_if);
  2463. } else {
  2464. ulong ioaddr = dev->base_addr; /* card base I/O address */
  2465. curr_link = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
  2466. }
  2467. if (!curr_link) {
  2468. if (prev_link || verbose) {
  2469. netif_carrier_off(dev);
  2470. if (netif_msg_link(lp))
  2471. printk(KERN_INFO "%s: link down\n", dev->name);
  2472. }
  2473. if (lp->phycount > 1) {
  2474. curr_link = pcnet32_check_otherphy(dev);
  2475. prev_link = 0;
  2476. }
  2477. } else if (verbose || !prev_link) {
  2478. netif_carrier_on(dev);
  2479. if (lp->mii) {
  2480. if (netif_msg_link(lp)) {
  2481. struct ethtool_cmd ecmd;
  2482. mii_ethtool_gset(&lp->mii_if, &ecmd);
  2483. printk(KERN_INFO
  2484. "%s: link up, %sMbps, %s-duplex\n",
  2485. dev->name,
  2486. (ecmd.speed == SPEED_100) ? "100" : "10",
  2487. (ecmd.duplex ==
  2488. DUPLEX_FULL) ? "full" : "half");
  2489. }
  2490. bcr9 = lp->a.read_bcr(dev->base_addr, 9);
  2491. if ((bcr9 & (1 << 0)) != lp->mii_if.full_duplex) {
  2492. if (lp->mii_if.full_duplex)
  2493. bcr9 |= (1 << 0);
  2494. else
  2495. bcr9 &= ~(1 << 0);
  2496. lp->a.write_bcr(dev->base_addr, 9, bcr9);
  2497. }
  2498. } else {
  2499. if (netif_msg_link(lp))
  2500. printk(KERN_INFO "%s: link up\n", dev->name);
  2501. }
  2502. }
  2503. }
  2504. /*
  2505. * Check for loss of link and link establishment.
  2506. * Can not use mii_check_media because it does nothing if mode is forced.
  2507. */
  2508. static void pcnet32_watchdog(struct net_device *dev)
  2509. {
  2510. struct pcnet32_private *lp = dev->priv;
  2511. unsigned long flags;
  2512. /* Print the link status if it has changed */
  2513. spin_lock_irqsave(&lp->lock, flags);
  2514. pcnet32_check_media(dev, 0);
  2515. spin_unlock_irqrestore(&lp->lock, flags);
  2516. mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
  2517. }
  2518. static void __devexit pcnet32_remove_one(struct pci_dev *pdev)
  2519. {
  2520. struct net_device *dev = pci_get_drvdata(pdev);
  2521. if (dev) {
  2522. struct pcnet32_private *lp = dev->priv;
  2523. unregister_netdev(dev);
  2524. pcnet32_free_ring(dev);
  2525. release_region(dev->base_addr, PCNET32_TOTAL_SIZE);
  2526. pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
  2527. free_netdev(dev);
  2528. pci_disable_device(pdev);
  2529. pci_set_drvdata(pdev, NULL);
  2530. }
  2531. }
  2532. static struct pci_driver pcnet32_driver = {
  2533. .name = DRV_NAME,
  2534. .probe = pcnet32_probe_pci,
  2535. .remove = __devexit_p(pcnet32_remove_one),
  2536. .id_table = pcnet32_pci_tbl,
  2537. };
  2538. /* An additional parameter that may be passed in... */
  2539. static int debug = -1;
  2540. static int tx_start_pt = -1;
  2541. static int pcnet32_have_pci;
  2542. module_param(debug, int, 0);
  2543. MODULE_PARM_DESC(debug, DRV_NAME " debug level");
  2544. module_param(max_interrupt_work, int, 0);
  2545. MODULE_PARM_DESC(max_interrupt_work,
  2546. DRV_NAME " maximum events handled per interrupt");
  2547. module_param(rx_copybreak, int, 0);
  2548. MODULE_PARM_DESC(rx_copybreak,
  2549. DRV_NAME " copy breakpoint for copy-only-tiny-frames");
  2550. module_param(tx_start_pt, int, 0);
  2551. MODULE_PARM_DESC(tx_start_pt, DRV_NAME " transmit start point (0-3)");
  2552. module_param(pcnet32vlb, int, 0);
  2553. MODULE_PARM_DESC(pcnet32vlb, DRV_NAME " Vesa local bus (VLB) support (0/1)");
  2554. module_param_array(options, int, NULL, 0);
  2555. MODULE_PARM_DESC(options, DRV_NAME " initial option setting(s) (0-15)");
  2556. module_param_array(full_duplex, int, NULL, 0);
  2557. MODULE_PARM_DESC(full_duplex, DRV_NAME " full duplex setting(s) (1)");
  2558. /* Module Parameter for HomePNA cards added by Patrick Simmons, 2004 */
  2559. module_param_array(homepna, int, NULL, 0);
  2560. MODULE_PARM_DESC(homepna,
  2561. DRV_NAME
  2562. " mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet");
  2563. MODULE_AUTHOR("Thomas Bogendoerfer");
  2564. MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
  2565. MODULE_LICENSE("GPL");
  2566. #define PCNET32_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  2567. static int __init pcnet32_init_module(void)
  2568. {
  2569. printk(KERN_INFO "%s", version);
  2570. pcnet32_debug = netif_msg_init(debug, PCNET32_MSG_DEFAULT);
  2571. if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
  2572. tx_start = tx_start_pt;
  2573. /* find the PCI devices */
  2574. if (!pci_module_init(&pcnet32_driver))
  2575. pcnet32_have_pci = 1;
  2576. /* should we find any remaining VLbus devices ? */
  2577. if (pcnet32vlb)
  2578. pcnet32_probe_vlbus(pcnet32_portlist);
  2579. if (cards_found && (pcnet32_debug & NETIF_MSG_PROBE))
  2580. printk(KERN_INFO PFX "%d cards_found.\n", cards_found);
  2581. return (pcnet32_have_pci + cards_found) ? 0 : -ENODEV;
  2582. }
  2583. static void __exit pcnet32_cleanup_module(void)
  2584. {
  2585. struct net_device *next_dev;
  2586. while (pcnet32_dev) {
  2587. struct pcnet32_private *lp = pcnet32_dev->priv;
  2588. next_dev = lp->next;
  2589. unregister_netdev(pcnet32_dev);
  2590. pcnet32_free_ring(pcnet32_dev);
  2591. release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
  2592. pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
  2593. free_netdev(pcnet32_dev);
  2594. pcnet32_dev = next_dev;
  2595. }
  2596. if (pcnet32_have_pci)
  2597. pci_unregister_driver(&pcnet32_driver);
  2598. }
  2599. module_init(pcnet32_init_module);
  2600. module_exit(pcnet32_cleanup_module);
  2601. /*
  2602. * Local variables:
  2603. * c-indent-level: 4
  2604. * tab-width: 8
  2605. * End:
  2606. */