pcnet32.c 82 KB

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