pcnet32.c 82 KB

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