pcnet32.c 83 KB

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