pcnet32.c 81 KB

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