pcnet32.c 84 KB

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