pcnet32.c 85 KB

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