pcnet32.c 80 KB

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