pcnet32.c 83 KB

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