pcnet32.c 84 KB

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