pcnet32.c 82 KB

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