pcnet32.c 82 KB

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