pcnet32.c 80 KB

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