eepro100.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. /* drivers/net/eepro100.c: An Intel i82557-559 Ethernet driver for Linux. */
  2. /*
  3. Written 1996-1999 by Donald Becker.
  4. The driver also contains updates by different kernel developers
  5. (see incomplete list below).
  6. Current maintainer is Andrey V. Savochkin <saw@saw.sw.com.sg>.
  7. Please use this email address and linux-kernel mailing list for bug reports.
  8. This software may be used and distributed according to the terms
  9. of the GNU General Public License, incorporated herein by reference.
  10. This driver is for the Intel EtherExpress Pro100 (Speedo3) design.
  11. It should work with all i82557/558/559 boards.
  12. Version history:
  13. 1998 Apr - 2000 Feb Andrey V. Savochkin <saw@saw.sw.com.sg>
  14. Serious fixes for multicast filter list setting, TX timeout routine;
  15. RX ring refilling logic; other stuff
  16. 2000 Feb Jeff Garzik <jgarzik@pobox.com>
  17. Convert to new PCI driver interface
  18. 2000 Mar 24 Dragan Stancevic <visitor@valinux.com>
  19. Disabled FC and ER, to avoid lockups when when we get FCP interrupts.
  20. 2000 Jul 17 Goutham Rao <goutham.rao@intel.com>
  21. PCI DMA API fixes, adding pci_dma_sync_single calls where neccesary
  22. 2000 Aug 31 David Mosberger <davidm@hpl.hp.com>
  23. rx_align support: enables rx DMA without causing unaligned accesses.
  24. */
  25. static const char * const version =
  26. "eepro100.c:v1.09j-t 9/29/99 Donald Becker http://www.scyld.com/network/eepro100.html\n"
  27. "eepro100.c: $Revision: 1.1.1.1 $ 2000/11/17 Modified by Andrey V. Savochkin <saw@saw.sw.com.sg> and others\n";
  28. /* A few user-configurable values that apply to all boards.
  29. First set is undocumented and spelled per Intel recommendations. */
  30. static int congenb /* = 0 */; /* Enable congestion control in the DP83840. */
  31. static int txfifo = 8; /* Tx FIFO threshold in 4 byte units, 0-15 */
  32. static int rxfifo = 8; /* Rx FIFO threshold, default 32 bytes. */
  33. /* Tx/Rx DMA burst length, 0-127, 0 == no preemption, tx==128 -> disabled. */
  34. static int txdmacount = 128;
  35. static int rxdmacount /* = 0 */;
  36. #if defined(__ia64__) || defined(__alpha__) || defined(__sparc__) || defined(__mips__) || \
  37. defined(__arm__)
  38. /* align rx buffers to 2 bytes so that IP header is aligned */
  39. # define rx_align(skb) skb_reserve((skb), 2)
  40. # define RxFD_ALIGNMENT __attribute__ ((aligned (2), packed))
  41. #else
  42. # define rx_align(skb)
  43. # define RxFD_ALIGNMENT
  44. #endif
  45. /* Set the copy breakpoint for the copy-only-tiny-buffer Rx method.
  46. Lower values use more memory, but are faster. */
  47. static int rx_copybreak = 200;
  48. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  49. static int max_interrupt_work = 20;
  50. /* Maximum number of multicast addresses to filter (vs. rx-all-multicast) */
  51. static int multicast_filter_limit = 64;
  52. /* 'options' is used to pass a transceiver override or full-duplex flag
  53. e.g. "options=16" for FD, "options=32" for 100mbps-only. */
  54. static int full_duplex[] = {-1, -1, -1, -1, -1, -1, -1, -1};
  55. static int options[] = {-1, -1, -1, -1, -1, -1, -1, -1};
  56. /* A few values that may be tweaked. */
  57. /* The ring sizes should be a power of two for efficiency. */
  58. #define TX_RING_SIZE 64
  59. #define RX_RING_SIZE 64
  60. /* How much slots multicast filter setup may take.
  61. Do not descrease without changing set_rx_mode() implementaion. */
  62. #define TX_MULTICAST_SIZE 2
  63. #define TX_MULTICAST_RESERV (TX_MULTICAST_SIZE*2)
  64. /* Actual number of TX packets queued, must be
  65. <= TX_RING_SIZE-TX_MULTICAST_RESERV. */
  66. #define TX_QUEUE_LIMIT (TX_RING_SIZE-TX_MULTICAST_RESERV)
  67. /* Hysteresis marking queue as no longer full. */
  68. #define TX_QUEUE_UNFULL (TX_QUEUE_LIMIT-4)
  69. /* Operational parameters that usually are not changed. */
  70. /* Time in jiffies before concluding the transmitter is hung. */
  71. #define TX_TIMEOUT (2*HZ)
  72. /* Size of an pre-allocated Rx buffer: <Ethernet MTU> + slack.*/
  73. #define PKT_BUF_SZ 1536
  74. #include <linux/module.h>
  75. #include <linux/kernel.h>
  76. #include <linux/string.h>
  77. #include <linux/errno.h>
  78. #include <linux/ioport.h>
  79. #include <linux/slab.h>
  80. #include <linux/interrupt.h>
  81. #include <linux/timer.h>
  82. #include <linux/pci.h>
  83. #include <linux/spinlock.h>
  84. #include <linux/init.h>
  85. #include <linux/mii.h>
  86. #include <linux/delay.h>
  87. #include <linux/bitops.h>
  88. #include <asm/io.h>
  89. #include <asm/uaccess.h>
  90. #include <asm/irq.h>
  91. #include <linux/netdevice.h>
  92. #include <linux/etherdevice.h>
  93. #include <linux/rtnetlink.h>
  94. #include <linux/skbuff.h>
  95. #include <linux/ethtool.h>
  96. static int use_io;
  97. static int debug = -1;
  98. #define DEBUG_DEFAULT (NETIF_MSG_DRV | \
  99. NETIF_MSG_HW | \
  100. NETIF_MSG_RX_ERR | \
  101. NETIF_MSG_TX_ERR)
  102. #define DEBUG ((debug >= 0) ? (1<<debug)-1 : DEBUG_DEFAULT)
  103. MODULE_AUTHOR("Maintainer: Andrey V. Savochkin <saw@saw.sw.com.sg>");
  104. MODULE_DESCRIPTION("Intel i82557/i82558/i82559 PCI EtherExpressPro driver");
  105. MODULE_LICENSE("GPL");
  106. module_param(use_io, int, 0);
  107. module_param(debug, int, 0);
  108. module_param_array(options, int, NULL, 0);
  109. module_param_array(full_duplex, int, NULL, 0);
  110. module_param(congenb, int, 0);
  111. module_param(txfifo, int, 0);
  112. module_param(rxfifo, int, 0);
  113. module_param(txdmacount, int, 0);
  114. module_param(rxdmacount, int, 0);
  115. module_param(rx_copybreak, int, 0);
  116. module_param(max_interrupt_work, int, 0);
  117. module_param(multicast_filter_limit, int, 0);
  118. MODULE_PARM_DESC(debug, "debug level (0-6)");
  119. MODULE_PARM_DESC(options, "Bits 0-3: transceiver type, bit 4: full duplex, bit 5: 100Mbps");
  120. MODULE_PARM_DESC(full_duplex, "full duplex setting(s) (1)");
  121. MODULE_PARM_DESC(congenb, "Enable congestion control (1)");
  122. MODULE_PARM_DESC(txfifo, "Tx FIFO threshold in 4 byte units, (0-15)");
  123. MODULE_PARM_DESC(rxfifo, "Rx FIFO threshold in 4 byte units, (0-15)");
  124. MODULE_PARM_DESC(txdmacount, "Tx DMA burst length; 128 - disable (0-128)");
  125. MODULE_PARM_DESC(rxdmacount, "Rx DMA burst length; 128 - disable (0-128)");
  126. MODULE_PARM_DESC(rx_copybreak, "copy breakpoint for copy-only-tiny-frames");
  127. MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
  128. MODULE_PARM_DESC(multicast_filter_limit, "maximum number of filtered multicast addresses");
  129. #define RUN_AT(x) (jiffies + (x))
  130. #define netdevice_start(dev)
  131. #define netdevice_stop(dev)
  132. #define netif_set_tx_timeout(dev, tf, tm) \
  133. do { \
  134. (dev)->tx_timeout = (tf); \
  135. (dev)->watchdog_timeo = (tm); \
  136. } while(0)
  137. /*
  138. Theory of Operation
  139. I. Board Compatibility
  140. This device driver is designed for the Intel i82557 "Speedo3" chip, Intel's
  141. single-chip fast Ethernet controller for PCI, as used on the Intel
  142. EtherExpress Pro 100 adapter.
  143. II. Board-specific settings
  144. PCI bus devices are configured by the system at boot time, so no jumpers
  145. need to be set on the board. The system BIOS should be set to assign the
  146. PCI INTA signal to an otherwise unused system IRQ line. While it's
  147. possible to share PCI interrupt lines, it negatively impacts performance and
  148. only recent kernels support it.
  149. III. Driver operation
  150. IIIA. General
  151. The Speedo3 is very similar to other Intel network chips, that is to say
  152. "apparently designed on a different planet". This chips retains the complex
  153. Rx and Tx descriptors and multiple buffers pointers as previous chips, but
  154. also has simplified Tx and Rx buffer modes. This driver uses the "flexible"
  155. Tx mode, but in a simplified lower-overhead manner: it associates only a
  156. single buffer descriptor with each frame descriptor.
  157. Despite the extra space overhead in each receive skbuff, the driver must use
  158. the simplified Rx buffer mode to assure that only a single data buffer is
  159. associated with each RxFD. The driver implements this by reserving space
  160. for the Rx descriptor at the head of each Rx skbuff.
  161. The Speedo-3 has receive and command unit base addresses that are added to
  162. almost all descriptor pointers. The driver sets these to zero, so that all
  163. pointer fields are absolute addresses.
  164. The System Control Block (SCB) of some previous Intel chips exists on the
  165. chip in both PCI I/O and memory space. This driver uses the I/O space
  166. registers, but might switch to memory mapped mode to better support non-x86
  167. processors.
  168. IIIB. Transmit structure
  169. The driver must use the complex Tx command+descriptor mode in order to
  170. have a indirect pointer to the skbuff data section. Each Tx command block
  171. (TxCB) is associated with two immediately appended Tx Buffer Descriptor
  172. (TxBD). A fixed ring of these TxCB+TxBD pairs are kept as part of the
  173. speedo_private data structure for each adapter instance.
  174. The newer i82558 explicitly supports this structure, and can read the two
  175. TxBDs in the same PCI burst as the TxCB.
  176. This ring structure is used for all normal transmit packets, but the
  177. transmit packet descriptors aren't long enough for most non-Tx commands such
  178. as CmdConfigure. This is complicated by the possibility that the chip has
  179. already loaded the link address in the previous descriptor. So for these
  180. commands we convert the next free descriptor on the ring to a NoOp, and point
  181. that descriptor's link to the complex command.
  182. An additional complexity of these non-transmit commands are that they may be
  183. added asynchronous to the normal transmit queue, so we disable interrupts
  184. whenever the Tx descriptor ring is manipulated.
  185. A notable aspect of these special configure commands is that they do
  186. work with the normal Tx ring entry scavenge method. The Tx ring scavenge
  187. is done at interrupt time using the 'dirty_tx' index, and checking for the
  188. command-complete bit. While the setup frames may have the NoOp command on the
  189. Tx ring marked as complete, but not have completed the setup command, this
  190. is not a problem. The tx_ring entry can be still safely reused, as the
  191. tx_skbuff[] entry is always empty for config_cmd and mc_setup frames.
  192. Commands may have bits set e.g. CmdSuspend in the command word to either
  193. suspend or stop the transmit/command unit. This driver always flags the last
  194. command with CmdSuspend, erases the CmdSuspend in the previous command, and
  195. then issues a CU_RESUME.
  196. Note: Watch out for the potential race condition here: imagine
  197. erasing the previous suspend
  198. the chip processes the previous command
  199. the chip processes the final command, and suspends
  200. doing the CU_RESUME
  201. the chip processes the next-yet-valid post-final-command.
  202. So blindly sending a CU_RESUME is only safe if we do it immediately after
  203. after erasing the previous CmdSuspend, without the possibility of an
  204. intervening delay. Thus the resume command is always within the
  205. interrupts-disabled region. This is a timing dependence, but handling this
  206. condition in a timing-independent way would considerably complicate the code.
  207. Note: In previous generation Intel chips, restarting the command unit was a
  208. notoriously slow process. This is presumably no longer true.
  209. IIIC. Receive structure
  210. Because of the bus-master support on the Speedo3 this driver uses the new
  211. SKBUFF_RX_COPYBREAK scheme, rather than a fixed intermediate receive buffer.
  212. This scheme allocates full-sized skbuffs as receive buffers. The value
  213. SKBUFF_RX_COPYBREAK is used as the copying breakpoint: it is chosen to
  214. trade-off the memory wasted by passing the full-sized skbuff to the queue
  215. layer for all frames vs. the copying cost of copying a frame to a
  216. correctly-sized skbuff.
  217. For small frames the copying cost is negligible (esp. considering that we
  218. are pre-loading the cache with immediately useful header information), so we
  219. allocate a new, minimally-sized skbuff. For large frames the copying cost
  220. is non-trivial, and the larger copy might flush the cache of useful data, so
  221. we pass up the skbuff the packet was received into.
  222. IV. Notes
  223. Thanks to Steve Williams of Intel for arranging the non-disclosure agreement
  224. that stated that I could disclose the information. But I still resent
  225. having to sign an Intel NDA when I'm helping Intel sell their own product!
  226. */
  227. static int speedo_found1(struct pci_dev *pdev, void __iomem *ioaddr, int fnd_cnt, int acpi_idle_state);
  228. /* Offsets to the various registers.
  229. All accesses need not be longword aligned. */
  230. enum speedo_offsets {
  231. SCBStatus = 0, SCBCmd = 2, /* Rx/Command Unit command and status. */
  232. SCBIntmask = 3,
  233. SCBPointer = 4, /* General purpose pointer. */
  234. SCBPort = 8, /* Misc. commands and operands. */
  235. SCBflash = 12, SCBeeprom = 14, /* EEPROM and flash memory control. */
  236. SCBCtrlMDI = 16, /* MDI interface control. */
  237. SCBEarlyRx = 20, /* Early receive byte count. */
  238. };
  239. /* Commands that can be put in a command list entry. */
  240. enum commands {
  241. CmdNOp = 0, CmdIASetup = 0x10000, CmdConfigure = 0x20000,
  242. CmdMulticastList = 0x30000, CmdTx = 0x40000, CmdTDR = 0x50000,
  243. CmdDump = 0x60000, CmdDiagnose = 0x70000,
  244. CmdSuspend = 0x40000000, /* Suspend after completion. */
  245. CmdIntr = 0x20000000, /* Interrupt after completion. */
  246. CmdTxFlex = 0x00080000, /* Use "Flexible mode" for CmdTx command. */
  247. };
  248. /* Clear CmdSuspend (1<<30) avoiding interference with the card access to the
  249. status bits. Previous driver versions used separate 16 bit fields for
  250. commands and statuses. --SAW
  251. */
  252. #if defined(__alpha__)
  253. # define clear_suspend(cmd) clear_bit(30, &(cmd)->cmd_status);
  254. #else
  255. # if defined(__LITTLE_ENDIAN)
  256. # define clear_suspend(cmd) ((__u16 *)&(cmd)->cmd_status)[1] &= ~0x4000
  257. # elif defined(__BIG_ENDIAN)
  258. # define clear_suspend(cmd) ((__u16 *)&(cmd)->cmd_status)[1] &= ~0x0040
  259. # else
  260. # error Unsupported byteorder
  261. # endif
  262. #endif
  263. enum SCBCmdBits {
  264. SCBMaskCmdDone=0x8000, SCBMaskRxDone=0x4000, SCBMaskCmdIdle=0x2000,
  265. SCBMaskRxSuspend=0x1000, SCBMaskEarlyRx=0x0800, SCBMaskFlowCtl=0x0400,
  266. SCBTriggerIntr=0x0200, SCBMaskAll=0x0100,
  267. /* The rest are Rx and Tx commands. */
  268. CUStart=0x0010, CUResume=0x0020, CUStatsAddr=0x0040, CUShowStats=0x0050,
  269. CUCmdBase=0x0060, /* CU Base address (set to zero) . */
  270. CUDumpStats=0x0070, /* Dump then reset stats counters. */
  271. RxStart=0x0001, RxResume=0x0002, RxAbort=0x0004, RxAddrLoad=0x0006,
  272. RxResumeNoResources=0x0007,
  273. };
  274. enum SCBPort_cmds {
  275. PortReset=0, PortSelfTest=1, PortPartialReset=2, PortDump=3,
  276. };
  277. /* The Speedo3 Rx and Tx frame/buffer descriptors. */
  278. struct descriptor { /* A generic descriptor. */
  279. volatile s32 cmd_status; /* All command and status fields. */
  280. u32 link; /* struct descriptor * */
  281. unsigned char params[0];
  282. };
  283. /* The Speedo3 Rx and Tx buffer descriptors. */
  284. struct RxFD { /* Receive frame descriptor. */
  285. volatile s32 status;
  286. u32 link; /* struct RxFD * */
  287. u32 rx_buf_addr; /* void * */
  288. u32 count;
  289. } RxFD_ALIGNMENT;
  290. /* Selected elements of the Tx/RxFD.status word. */
  291. enum RxFD_bits {
  292. RxComplete=0x8000, RxOK=0x2000,
  293. RxErrCRC=0x0800, RxErrAlign=0x0400, RxErrTooBig=0x0200, RxErrSymbol=0x0010,
  294. RxEth2Type=0x0020, RxNoMatch=0x0004, RxNoIAMatch=0x0002,
  295. TxUnderrun=0x1000, StatusComplete=0x8000,
  296. };
  297. #define CONFIG_DATA_SIZE 22
  298. struct TxFD { /* Transmit frame descriptor set. */
  299. s32 status;
  300. u32 link; /* void * */
  301. u32 tx_desc_addr; /* Always points to the tx_buf_addr element. */
  302. s32 count; /* # of TBD (=1), Tx start thresh., etc. */
  303. /* This constitutes two "TBD" entries -- we only use one. */
  304. #define TX_DESCR_BUF_OFFSET 16
  305. u32 tx_buf_addr0; /* void *, frame to be transmitted. */
  306. s32 tx_buf_size0; /* Length of Tx frame. */
  307. u32 tx_buf_addr1; /* void *, frame to be transmitted. */
  308. s32 tx_buf_size1; /* Length of Tx frame. */
  309. /* the structure must have space for at least CONFIG_DATA_SIZE starting
  310. * from tx_desc_addr field */
  311. };
  312. /* Multicast filter setting block. --SAW */
  313. struct speedo_mc_block {
  314. struct speedo_mc_block *next;
  315. unsigned int tx;
  316. dma_addr_t frame_dma;
  317. unsigned int len;
  318. struct descriptor frame __attribute__ ((__aligned__(16)));
  319. };
  320. /* Elements of the dump_statistics block. This block must be lword aligned. */
  321. struct speedo_stats {
  322. u32 tx_good_frames;
  323. u32 tx_coll16_errs;
  324. u32 tx_late_colls;
  325. u32 tx_underruns;
  326. u32 tx_lost_carrier;
  327. u32 tx_deferred;
  328. u32 tx_one_colls;
  329. u32 tx_multi_colls;
  330. u32 tx_total_colls;
  331. u32 rx_good_frames;
  332. u32 rx_crc_errs;
  333. u32 rx_align_errs;
  334. u32 rx_resource_errs;
  335. u32 rx_overrun_errs;
  336. u32 rx_colls_errs;
  337. u32 rx_runt_errs;
  338. u32 done_marker;
  339. };
  340. enum Rx_ring_state_bits {
  341. RrNoMem=1, RrPostponed=2, RrNoResources=4, RrOOMReported=8,
  342. };
  343. /* Do not change the position (alignment) of the first few elements!
  344. The later elements are grouped for cache locality.
  345. Unfortunately, all the positions have been shifted since there.
  346. A new re-alignment is required. 2000/03/06 SAW */
  347. struct speedo_private {
  348. void __iomem *regs;
  349. struct TxFD *tx_ring; /* Commands (usually CmdTxPacket). */
  350. struct RxFD *rx_ringp[RX_RING_SIZE]; /* Rx descriptor, used as ring. */
  351. /* The addresses of a Tx/Rx-in-place packets/buffers. */
  352. struct sk_buff *tx_skbuff[TX_RING_SIZE];
  353. struct sk_buff *rx_skbuff[RX_RING_SIZE];
  354. /* Mapped addresses of the rings. */
  355. dma_addr_t tx_ring_dma;
  356. #define TX_RING_ELEM_DMA(sp, n) ((sp)->tx_ring_dma + (n)*sizeof(struct TxFD))
  357. dma_addr_t rx_ring_dma[RX_RING_SIZE];
  358. struct descriptor *last_cmd; /* Last command sent. */
  359. unsigned int cur_tx, dirty_tx; /* The ring entries to be free()ed. */
  360. spinlock_t lock; /* Group with Tx control cache line. */
  361. u32 tx_threshold; /* The value for txdesc.count. */
  362. struct RxFD *last_rxf; /* Last filled RX buffer. */
  363. dma_addr_t last_rxf_dma;
  364. unsigned int cur_rx, dirty_rx; /* The next free ring entry */
  365. long last_rx_time; /* Last Rx, in jiffies, to handle Rx hang. */
  366. struct net_device_stats stats;
  367. struct speedo_stats *lstats;
  368. dma_addr_t lstats_dma;
  369. int chip_id;
  370. struct pci_dev *pdev;
  371. struct timer_list timer; /* Media selection timer. */
  372. struct speedo_mc_block *mc_setup_head; /* Multicast setup frame list head. */
  373. struct speedo_mc_block *mc_setup_tail; /* Multicast setup frame list tail. */
  374. long in_interrupt; /* Word-aligned dev->interrupt */
  375. unsigned char acpi_pwr;
  376. signed char rx_mode; /* Current PROMISC/ALLMULTI setting. */
  377. unsigned int tx_full:1; /* The Tx queue is full. */
  378. unsigned int flow_ctrl:1; /* Use 802.3x flow control. */
  379. unsigned int rx_bug:1; /* Work around receiver hang errata. */
  380. unsigned char default_port:8; /* Last dev->if_port value. */
  381. unsigned char rx_ring_state; /* RX ring status flags. */
  382. unsigned short phy[2]; /* PHY media interfaces available. */
  383. unsigned short partner; /* Link partner caps. */
  384. struct mii_if_info mii_if; /* MII API hooks, info */
  385. u32 msg_enable; /* debug message level */
  386. };
  387. /* The parameters for a CmdConfigure operation.
  388. There are so many options that it would be difficult to document each bit.
  389. We mostly use the default or recommended settings. */
  390. static const char i82557_config_cmd[CONFIG_DATA_SIZE] = {
  391. 22, 0x08, 0, 0, 0, 0, 0x32, 0x03, 1, /* 1=Use MII 0=Use AUI */
  392. 0, 0x2E, 0, 0x60, 0,
  393. 0xf2, 0x48, 0, 0x40, 0xf2, 0x80, /* 0x40=Force full-duplex */
  394. 0x3f, 0x05, };
  395. static const char i82558_config_cmd[CONFIG_DATA_SIZE] = {
  396. 22, 0x08, 0, 1, 0, 0, 0x22, 0x03, 1, /* 1=Use MII 0=Use AUI */
  397. 0, 0x2E, 0, 0x60, 0x08, 0x88,
  398. 0x68, 0, 0x40, 0xf2, 0x84, /* Disable FC */
  399. 0x31, 0x05, };
  400. /* PHY media interface chips. */
  401. static const char * const phys[] = {
  402. "None", "i82553-A/B", "i82553-C", "i82503",
  403. "DP83840", "80c240", "80c24", "i82555",
  404. "unknown-8", "unknown-9", "DP83840A", "unknown-11",
  405. "unknown-12", "unknown-13", "unknown-14", "unknown-15", };
  406. enum phy_chips { NonSuchPhy=0, I82553AB, I82553C, I82503, DP83840, S80C240,
  407. S80C24, I82555, DP83840A=10, };
  408. static const char is_mii[] = { 0, 1, 1, 0, 1, 1, 0, 1 };
  409. #define EE_READ_CMD (6)
  410. static int eepro100_init_one(struct pci_dev *pdev,
  411. const struct pci_device_id *ent);
  412. static int do_eeprom_cmd(void __iomem *ioaddr, int cmd, int cmd_len);
  413. static int mdio_read(struct net_device *dev, int phy_id, int location);
  414. static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
  415. static int speedo_open(struct net_device *dev);
  416. static void speedo_resume(struct net_device *dev);
  417. static void speedo_timer(unsigned long data);
  418. static void speedo_init_rx_ring(struct net_device *dev);
  419. static void speedo_tx_timeout(struct net_device *dev);
  420. static int speedo_start_xmit(struct sk_buff *skb, struct net_device *dev);
  421. static void speedo_refill_rx_buffers(struct net_device *dev, int force);
  422. static int speedo_rx(struct net_device *dev);
  423. static void speedo_tx_buffer_gc(struct net_device *dev);
  424. static irqreturn_t speedo_interrupt(int irq, void *dev_instance);
  425. static int speedo_close(struct net_device *dev);
  426. static struct net_device_stats *speedo_get_stats(struct net_device *dev);
  427. static int speedo_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  428. static void set_rx_mode(struct net_device *dev);
  429. static void speedo_show_state(struct net_device *dev);
  430. static const struct ethtool_ops ethtool_ops;
  431. #ifdef honor_default_port
  432. /* Optional driver feature to allow forcing the transceiver setting.
  433. Not recommended. */
  434. static int mii_ctrl[8] = { 0x3300, 0x3100, 0x0000, 0x0100,
  435. 0x2000, 0x2100, 0x0400, 0x3100};
  436. #endif
  437. /* How to wait for the command unit to accept a command.
  438. Typically this takes 0 ticks. */
  439. static inline unsigned char wait_for_cmd_done(struct net_device *dev,
  440. struct speedo_private *sp)
  441. {
  442. int wait = 1000;
  443. void __iomem *cmd_ioaddr = sp->regs + SCBCmd;
  444. unsigned char r;
  445. do {
  446. udelay(1);
  447. r = ioread8(cmd_ioaddr);
  448. } while(r && --wait >= 0);
  449. if (wait < 0)
  450. printk(KERN_ALERT "%s: wait_for_cmd_done timeout!\n", dev->name);
  451. return r;
  452. }
  453. static int __devinit eepro100_init_one (struct pci_dev *pdev,
  454. const struct pci_device_id *ent)
  455. {
  456. void __iomem *ioaddr;
  457. int irq, pci_bar;
  458. int acpi_idle_state = 0, pm;
  459. static int cards_found /* = 0 */;
  460. unsigned long pci_base;
  461. #ifndef MODULE
  462. /* when built-in, we only print version if device is found */
  463. static int did_version;
  464. if (did_version++ == 0)
  465. printk(version);
  466. #endif
  467. /* save power state before pci_enable_device overwrites it */
  468. pm = pci_find_capability(pdev, PCI_CAP_ID_PM);
  469. if (pm) {
  470. u16 pwr_command;
  471. pci_read_config_word(pdev, pm + PCI_PM_CTRL, &pwr_command);
  472. acpi_idle_state = pwr_command & PCI_PM_CTRL_STATE_MASK;
  473. }
  474. if (pci_enable_device(pdev))
  475. goto err_out_free_mmio_region;
  476. pci_set_master(pdev);
  477. if (!request_region(pci_resource_start(pdev, 1),
  478. pci_resource_len(pdev, 1), "eepro100")) {
  479. dev_err(&pdev->dev, "eepro100: cannot reserve I/O ports\n");
  480. goto err_out_none;
  481. }
  482. if (!request_mem_region(pci_resource_start(pdev, 0),
  483. pci_resource_len(pdev, 0), "eepro100")) {
  484. dev_err(&pdev->dev, "eepro100: cannot reserve MMIO region\n");
  485. goto err_out_free_pio_region;
  486. }
  487. irq = pdev->irq;
  488. pci_bar = use_io ? 1 : 0;
  489. pci_base = pci_resource_start(pdev, pci_bar);
  490. if (DEBUG & NETIF_MSG_PROBE)
  491. printk("Found Intel i82557 PCI Speedo at %#lx, IRQ %d.\n",
  492. pci_base, irq);
  493. ioaddr = pci_iomap(pdev, pci_bar, 0);
  494. if (!ioaddr) {
  495. dev_err(&pdev->dev, "eepro100: cannot remap IO\n");
  496. goto err_out_free_mmio_region;
  497. }
  498. if (speedo_found1(pdev, ioaddr, cards_found, acpi_idle_state) == 0)
  499. cards_found++;
  500. else
  501. goto err_out_iounmap;
  502. return 0;
  503. err_out_iounmap: ;
  504. pci_iounmap(pdev, ioaddr);
  505. err_out_free_mmio_region:
  506. release_mem_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  507. err_out_free_pio_region:
  508. release_region(pci_resource_start(pdev, 1), pci_resource_len(pdev, 1));
  509. err_out_none:
  510. return -ENODEV;
  511. }
  512. #ifdef CONFIG_NET_POLL_CONTROLLER
  513. /*
  514. * Polling 'interrupt' - used by things like netconsole to send skbs
  515. * without having to re-enable interrupts. It's not called while
  516. * the interrupt routine is executing.
  517. */
  518. static void poll_speedo (struct net_device *dev)
  519. {
  520. /* disable_irq is not very nice, but with the funny lockless design
  521. we have no other choice. */
  522. disable_irq(dev->irq);
  523. speedo_interrupt (dev->irq, dev);
  524. enable_irq(dev->irq);
  525. }
  526. #endif
  527. static int __devinit speedo_found1(struct pci_dev *pdev,
  528. void __iomem *ioaddr, int card_idx, int acpi_idle_state)
  529. {
  530. struct net_device *dev;
  531. struct speedo_private *sp;
  532. const char *product;
  533. int i, option;
  534. u16 eeprom[0x100];
  535. int size;
  536. void *tx_ring_space;
  537. dma_addr_t tx_ring_dma;
  538. size = TX_RING_SIZE * sizeof(struct TxFD) + sizeof(struct speedo_stats);
  539. tx_ring_space = pci_alloc_consistent(pdev, size, &tx_ring_dma);
  540. if (tx_ring_space == NULL)
  541. return -1;
  542. dev = alloc_etherdev(sizeof(struct speedo_private));
  543. if (dev == NULL) {
  544. printk(KERN_ERR "eepro100: Could not allocate ethernet device.\n");
  545. pci_free_consistent(pdev, size, tx_ring_space, tx_ring_dma);
  546. return -1;
  547. }
  548. SET_MODULE_OWNER(dev);
  549. SET_NETDEV_DEV(dev, &pdev->dev);
  550. if (dev->mem_start > 0)
  551. option = dev->mem_start;
  552. else if (card_idx >= 0 && options[card_idx] >= 0)
  553. option = options[card_idx];
  554. else
  555. option = 0;
  556. rtnl_lock();
  557. if (dev_alloc_name(dev, dev->name) < 0)
  558. goto err_free_unlock;
  559. /* Read the station address EEPROM before doing the reset.
  560. Nominally his should even be done before accepting the device, but
  561. then we wouldn't have a device name with which to report the error.
  562. The size test is for 6 bit vs. 8 bit address serial EEPROMs.
  563. */
  564. {
  565. void __iomem *iobase;
  566. int read_cmd, ee_size;
  567. u16 sum;
  568. int j;
  569. /* Use IO only to avoid postponed writes and satisfy EEPROM timing
  570. requirements. */
  571. iobase = pci_iomap(pdev, 1, pci_resource_len(pdev, 1));
  572. if (!iobase)
  573. goto err_free_unlock;
  574. if ((do_eeprom_cmd(iobase, EE_READ_CMD << 24, 27) & 0xffe0000)
  575. == 0xffe0000) {
  576. ee_size = 0x100;
  577. read_cmd = EE_READ_CMD << 24;
  578. } else {
  579. ee_size = 0x40;
  580. read_cmd = EE_READ_CMD << 22;
  581. }
  582. for (j = 0, i = 0, sum = 0; i < ee_size; i++) {
  583. u16 value = do_eeprom_cmd(iobase, read_cmd | (i << 16), 27);
  584. eeprom[i] = value;
  585. sum += value;
  586. if (i < 3) {
  587. dev->dev_addr[j++] = value;
  588. dev->dev_addr[j++] = value >> 8;
  589. }
  590. }
  591. if (sum != 0xBABA)
  592. printk(KERN_WARNING "%s: Invalid EEPROM checksum %#4.4x, "
  593. "check settings before activating this device!\n",
  594. dev->name, sum);
  595. /* Don't unregister_netdev(dev); as the EEPro may actually be
  596. usable, especially if the MAC address is set later.
  597. On the other hand, it may be unusable if MDI data is corrupted. */
  598. pci_iounmap(pdev, iobase);
  599. }
  600. /* Reset the chip: stop Tx and Rx processes and clear counters.
  601. This takes less than 10usec and will easily finish before the next
  602. action. */
  603. iowrite32(PortReset, ioaddr + SCBPort);
  604. ioread32(ioaddr + SCBPort);
  605. udelay(10);
  606. if (eeprom[3] & 0x0100)
  607. product = "OEM i82557/i82558 10/100 Ethernet";
  608. else
  609. product = pci_name(pdev);
  610. printk(KERN_INFO "%s: %s, ", dev->name, product);
  611. for (i = 0; i < 5; i++)
  612. printk("%2.2X:", dev->dev_addr[i]);
  613. printk("%2.2X, ", dev->dev_addr[i]);
  614. printk("IRQ %d.\n", pdev->irq);
  615. sp = netdev_priv(dev);
  616. /* we must initialize this early, for mdio_{read,write} */
  617. sp->regs = ioaddr;
  618. #if 1 || defined(kernel_bloat)
  619. /* OK, this is pure kernel bloat. I don't like it when other drivers
  620. waste non-pageable kernel space to emit similar messages, but I need
  621. them for bug reports. */
  622. {
  623. const char *connectors[] = {" RJ45", " BNC", " AUI", " MII"};
  624. /* The self-test results must be paragraph aligned. */
  625. volatile s32 *self_test_results;
  626. int boguscnt = 16000; /* Timeout for set-test. */
  627. if ((eeprom[3] & 0x03) != 0x03)
  628. printk(KERN_INFO " Receiver lock-up bug exists -- enabling"
  629. " work-around.\n");
  630. printk(KERN_INFO " Board assembly %4.4x%2.2x-%3.3d, Physical"
  631. " connectors present:",
  632. eeprom[8], eeprom[9]>>8, eeprom[9] & 0xff);
  633. for (i = 0; i < 4; i++)
  634. if (eeprom[5] & (1<<i))
  635. printk(connectors[i]);
  636. printk("\n"KERN_INFO" Primary interface chip %s PHY #%d.\n",
  637. phys[(eeprom[6]>>8)&15], eeprom[6] & 0x1f);
  638. if (eeprom[7] & 0x0700)
  639. printk(KERN_INFO " Secondary interface chip %s.\n",
  640. phys[(eeprom[7]>>8)&7]);
  641. if (((eeprom[6]>>8) & 0x3f) == DP83840
  642. || ((eeprom[6]>>8) & 0x3f) == DP83840A) {
  643. int mdi_reg23 = mdio_read(dev, eeprom[6] & 0x1f, 23) | 0x0422;
  644. if (congenb)
  645. mdi_reg23 |= 0x0100;
  646. printk(KERN_INFO" DP83840 specific setup, setting register 23 to %4.4x.\n",
  647. mdi_reg23);
  648. mdio_write(dev, eeprom[6] & 0x1f, 23, mdi_reg23);
  649. }
  650. if ((option >= 0) && (option & 0x70)) {
  651. printk(KERN_INFO " Forcing %dMbs %s-duplex operation.\n",
  652. (option & 0x20 ? 100 : 10),
  653. (option & 0x10 ? "full" : "half"));
  654. mdio_write(dev, eeprom[6] & 0x1f, MII_BMCR,
  655. ((option & 0x20) ? 0x2000 : 0) | /* 100mbps? */
  656. ((option & 0x10) ? 0x0100 : 0)); /* Full duplex? */
  657. }
  658. /* Perform a system self-test. */
  659. self_test_results = (s32*) ((((long) tx_ring_space) + 15) & ~0xf);
  660. self_test_results[0] = 0;
  661. self_test_results[1] = -1;
  662. iowrite32(tx_ring_dma | PortSelfTest, ioaddr + SCBPort);
  663. do {
  664. udelay(10);
  665. } while (self_test_results[1] == -1 && --boguscnt >= 0);
  666. if (boguscnt < 0) { /* Test optimized out. */
  667. printk(KERN_ERR "Self test failed, status %8.8x:\n"
  668. KERN_ERR " Failure to initialize the i82557.\n"
  669. KERN_ERR " Verify that the card is a bus-master"
  670. " capable slot.\n",
  671. self_test_results[1]);
  672. } else
  673. printk(KERN_INFO " General self-test: %s.\n"
  674. KERN_INFO " Serial sub-system self-test: %s.\n"
  675. KERN_INFO " Internal registers self-test: %s.\n"
  676. KERN_INFO " ROM checksum self-test: %s (%#8.8x).\n",
  677. self_test_results[1] & 0x1000 ? "failed" : "passed",
  678. self_test_results[1] & 0x0020 ? "failed" : "passed",
  679. self_test_results[1] & 0x0008 ? "failed" : "passed",
  680. self_test_results[1] & 0x0004 ? "failed" : "passed",
  681. self_test_results[0]);
  682. }
  683. #endif /* kernel_bloat */
  684. iowrite32(PortReset, ioaddr + SCBPort);
  685. ioread32(ioaddr + SCBPort);
  686. udelay(10);
  687. /* Return the chip to its original power state. */
  688. pci_set_power_state(pdev, acpi_idle_state);
  689. pci_set_drvdata (pdev, dev);
  690. SET_NETDEV_DEV(dev, &pdev->dev);
  691. dev->irq = pdev->irq;
  692. sp->pdev = pdev;
  693. sp->msg_enable = DEBUG;
  694. sp->acpi_pwr = acpi_idle_state;
  695. sp->tx_ring = tx_ring_space;
  696. sp->tx_ring_dma = tx_ring_dma;
  697. sp->lstats = (struct speedo_stats *)(sp->tx_ring + TX_RING_SIZE);
  698. sp->lstats_dma = TX_RING_ELEM_DMA(sp, TX_RING_SIZE);
  699. init_timer(&sp->timer); /* used in ioctl() */
  700. spin_lock_init(&sp->lock);
  701. sp->mii_if.full_duplex = option >= 0 && (option & 0x10) ? 1 : 0;
  702. if (card_idx >= 0) {
  703. if (full_duplex[card_idx] >= 0)
  704. sp->mii_if.full_duplex = full_duplex[card_idx];
  705. }
  706. sp->default_port = option >= 0 ? (option & 0x0f) : 0;
  707. sp->phy[0] = eeprom[6];
  708. sp->phy[1] = eeprom[7];
  709. sp->mii_if.phy_id = eeprom[6] & 0x1f;
  710. sp->mii_if.phy_id_mask = 0x1f;
  711. sp->mii_if.reg_num_mask = 0x1f;
  712. sp->mii_if.dev = dev;
  713. sp->mii_if.mdio_read = mdio_read;
  714. sp->mii_if.mdio_write = mdio_write;
  715. sp->rx_bug = (eeprom[3] & 0x03) == 3 ? 0 : 1;
  716. if (((pdev->device > 0x1030 && (pdev->device < 0x103F)))
  717. || (pdev->device == 0x2449) || (pdev->device == 0x2459)
  718. || (pdev->device == 0x245D)) {
  719. sp->chip_id = 1;
  720. }
  721. if (sp->rx_bug)
  722. printk(KERN_INFO " Receiver lock-up workaround activated.\n");
  723. /* The Speedo-specific entries in the device structure. */
  724. dev->open = &speedo_open;
  725. dev->hard_start_xmit = &speedo_start_xmit;
  726. netif_set_tx_timeout(dev, &speedo_tx_timeout, TX_TIMEOUT);
  727. dev->stop = &speedo_close;
  728. dev->get_stats = &speedo_get_stats;
  729. dev->set_multicast_list = &set_rx_mode;
  730. dev->do_ioctl = &speedo_ioctl;
  731. SET_ETHTOOL_OPS(dev, &ethtool_ops);
  732. #ifdef CONFIG_NET_POLL_CONTROLLER
  733. dev->poll_controller = &poll_speedo;
  734. #endif
  735. if (register_netdevice(dev))
  736. goto err_free_unlock;
  737. rtnl_unlock();
  738. return 0;
  739. err_free_unlock:
  740. rtnl_unlock();
  741. free_netdev(dev);
  742. return -1;
  743. }
  744. static void do_slow_command(struct net_device *dev, struct speedo_private *sp, int cmd)
  745. {
  746. void __iomem *cmd_ioaddr = sp->regs + SCBCmd;
  747. int wait = 0;
  748. do
  749. if (ioread8(cmd_ioaddr) == 0) break;
  750. while(++wait <= 200);
  751. if (wait > 100)
  752. printk(KERN_ERR "Command %4.4x never accepted (%d polls)!\n",
  753. ioread8(cmd_ioaddr), wait);
  754. iowrite8(cmd, cmd_ioaddr);
  755. for (wait = 0; wait <= 100; wait++)
  756. if (ioread8(cmd_ioaddr) == 0) return;
  757. for (; wait <= 20000; wait++)
  758. if (ioread8(cmd_ioaddr) == 0) return;
  759. else udelay(1);
  760. printk(KERN_ERR "Command %4.4x was not accepted after %d polls!"
  761. " Current status %8.8x.\n",
  762. cmd, wait, ioread32(sp->regs + SCBStatus));
  763. }
  764. /* Serial EEPROM section.
  765. A "bit" grungy, but we work our way through bit-by-bit :->. */
  766. /* EEPROM_Ctrl bits. */
  767. #define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
  768. #define EE_CS 0x02 /* EEPROM chip select. */
  769. #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
  770. #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
  771. #define EE_ENB (0x4800 | EE_CS)
  772. #define EE_WRITE_0 0x4802
  773. #define EE_WRITE_1 0x4806
  774. #define EE_OFFSET SCBeeprom
  775. /* The fixes for the code were kindly provided by Dragan Stancevic
  776. <visitor@valinux.com> to strictly follow Intel specifications of EEPROM
  777. access timing.
  778. The publicly available sheet 64486302 (sec. 3.1) specifies 1us access
  779. interval for serial EEPROM. However, it looks like that there is an
  780. additional requirement dictating larger udelay's in the code below.
  781. 2000/05/24 SAW */
  782. static int __devinit do_eeprom_cmd(void __iomem *ioaddr, int cmd, int cmd_len)
  783. {
  784. unsigned retval = 0;
  785. void __iomem *ee_addr = ioaddr + SCBeeprom;
  786. iowrite16(EE_ENB, ee_addr); udelay(2);
  787. iowrite16(EE_ENB | EE_SHIFT_CLK, ee_addr); udelay(2);
  788. /* Shift the command bits out. */
  789. do {
  790. short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
  791. iowrite16(dataval, ee_addr); udelay(2);
  792. iowrite16(dataval | EE_SHIFT_CLK, ee_addr); udelay(2);
  793. retval = (retval << 1) | ((ioread16(ee_addr) & EE_DATA_READ) ? 1 : 0);
  794. } while (--cmd_len >= 0);
  795. iowrite16(EE_ENB, ee_addr); udelay(2);
  796. /* Terminate the EEPROM access. */
  797. iowrite16(EE_ENB & ~EE_CS, ee_addr);
  798. return retval;
  799. }
  800. static int mdio_read(struct net_device *dev, int phy_id, int location)
  801. {
  802. struct speedo_private *sp = netdev_priv(dev);
  803. void __iomem *ioaddr = sp->regs;
  804. int val, boguscnt = 64*10; /* <64 usec. to complete, typ 27 ticks */
  805. iowrite32(0x08000000 | (location<<16) | (phy_id<<21), ioaddr + SCBCtrlMDI);
  806. do {
  807. val = ioread32(ioaddr + SCBCtrlMDI);
  808. if (--boguscnt < 0) {
  809. printk(KERN_ERR " mdio_read() timed out with val = %8.8x.\n", val);
  810. break;
  811. }
  812. } while (! (val & 0x10000000));
  813. return val & 0xffff;
  814. }
  815. static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
  816. {
  817. struct speedo_private *sp = netdev_priv(dev);
  818. void __iomem *ioaddr = sp->regs;
  819. int val, boguscnt = 64*10; /* <64 usec. to complete, typ 27 ticks */
  820. iowrite32(0x04000000 | (location<<16) | (phy_id<<21) | value,
  821. ioaddr + SCBCtrlMDI);
  822. do {
  823. val = ioread32(ioaddr + SCBCtrlMDI);
  824. if (--boguscnt < 0) {
  825. printk(KERN_ERR" mdio_write() timed out with val = %8.8x.\n", val);
  826. break;
  827. }
  828. } while (! (val & 0x10000000));
  829. }
  830. static int
  831. speedo_open(struct net_device *dev)
  832. {
  833. struct speedo_private *sp = netdev_priv(dev);
  834. void __iomem *ioaddr = sp->regs;
  835. int retval;
  836. if (netif_msg_ifup(sp))
  837. printk(KERN_DEBUG "%s: speedo_open() irq %d.\n", dev->name, dev->irq);
  838. pci_set_power_state(sp->pdev, PCI_D0);
  839. /* Set up the Tx queue early.. */
  840. sp->cur_tx = 0;
  841. sp->dirty_tx = 0;
  842. sp->last_cmd = NULL;
  843. sp->tx_full = 0;
  844. sp->in_interrupt = 0;
  845. /* .. we can safely take handler calls during init. */
  846. retval = request_irq(dev->irq, &speedo_interrupt, IRQF_SHARED, dev->name, dev);
  847. if (retval) {
  848. return retval;
  849. }
  850. dev->if_port = sp->default_port;
  851. #ifdef oh_no_you_dont_unless_you_honour_the_options_passed_in_to_us
  852. /* Retrigger negotiation to reset previous errors. */
  853. if ((sp->phy[0] & 0x8000) == 0) {
  854. int phy_addr = sp->phy[0] & 0x1f ;
  855. /* Use 0x3300 for restarting NWay, other values to force xcvr:
  856. 0x0000 10-HD
  857. 0x0100 10-FD
  858. 0x2000 100-HD
  859. 0x2100 100-FD
  860. */
  861. #ifdef honor_default_port
  862. mdio_write(dev, phy_addr, MII_BMCR, mii_ctrl[dev->default_port & 7]);
  863. #else
  864. mdio_write(dev, phy_addr, MII_BMCR, 0x3300);
  865. #endif
  866. }
  867. #endif
  868. speedo_init_rx_ring(dev);
  869. /* Fire up the hardware. */
  870. iowrite16(SCBMaskAll, ioaddr + SCBCmd);
  871. speedo_resume(dev);
  872. netdevice_start(dev);
  873. netif_start_queue(dev);
  874. /* Setup the chip and configure the multicast list. */
  875. sp->mc_setup_head = NULL;
  876. sp->mc_setup_tail = NULL;
  877. sp->flow_ctrl = sp->partner = 0;
  878. sp->rx_mode = -1; /* Invalid -> always reset the mode. */
  879. set_rx_mode(dev);
  880. if ((sp->phy[0] & 0x8000) == 0)
  881. sp->mii_if.advertising = mdio_read(dev, sp->phy[0] & 0x1f, MII_ADVERTISE);
  882. mii_check_link(&sp->mii_if);
  883. if (netif_msg_ifup(sp)) {
  884. printk(KERN_DEBUG "%s: Done speedo_open(), status %8.8x.\n",
  885. dev->name, ioread16(ioaddr + SCBStatus));
  886. }
  887. /* Set the timer. The timer serves a dual purpose:
  888. 1) to monitor the media interface (e.g. link beat) and perhaps switch
  889. to an alternate media type
  890. 2) to monitor Rx activity, and restart the Rx process if the receiver
  891. hangs. */
  892. sp->timer.expires = RUN_AT((24*HZ)/10); /* 2.4 sec. */
  893. sp->timer.data = (unsigned long)dev;
  894. sp->timer.function = &speedo_timer; /* timer handler */
  895. add_timer(&sp->timer);
  896. /* No need to wait for the command unit to accept here. */
  897. if ((sp->phy[0] & 0x8000) == 0)
  898. mdio_read(dev, sp->phy[0] & 0x1f, MII_BMCR);
  899. return 0;
  900. }
  901. /* Start the chip hardware after a full reset. */
  902. static void speedo_resume(struct net_device *dev)
  903. {
  904. struct speedo_private *sp = netdev_priv(dev);
  905. void __iomem *ioaddr = sp->regs;
  906. /* Start with a Tx threshold of 256 (0x..20.... 8 byte units). */
  907. sp->tx_threshold = 0x01208000;
  908. /* Set the segment registers to '0'. */
  909. if (wait_for_cmd_done(dev, sp) != 0) {
  910. iowrite32(PortPartialReset, ioaddr + SCBPort);
  911. udelay(10);
  912. }
  913. iowrite32(0, ioaddr + SCBPointer);
  914. ioread32(ioaddr + SCBPointer); /* Flush to PCI. */
  915. udelay(10); /* Bogus, but it avoids the bug. */
  916. /* Note: these next two operations can take a while. */
  917. do_slow_command(dev, sp, RxAddrLoad);
  918. do_slow_command(dev, sp, CUCmdBase);
  919. /* Load the statistics block and rx ring addresses. */
  920. iowrite32(sp->lstats_dma, ioaddr + SCBPointer);
  921. ioread32(ioaddr + SCBPointer); /* Flush to PCI */
  922. iowrite8(CUStatsAddr, ioaddr + SCBCmd);
  923. sp->lstats->done_marker = 0;
  924. wait_for_cmd_done(dev, sp);
  925. if (sp->rx_ringp[sp->cur_rx % RX_RING_SIZE] == NULL) {
  926. if (netif_msg_rx_err(sp))
  927. printk(KERN_DEBUG "%s: NULL cur_rx in speedo_resume().\n",
  928. dev->name);
  929. } else {
  930. iowrite32(sp->rx_ring_dma[sp->cur_rx % RX_RING_SIZE],
  931. ioaddr + SCBPointer);
  932. ioread32(ioaddr + SCBPointer); /* Flush to PCI */
  933. }
  934. /* Note: RxStart should complete instantly. */
  935. do_slow_command(dev, sp, RxStart);
  936. do_slow_command(dev, sp, CUDumpStats);
  937. /* Fill the first command with our physical address. */
  938. {
  939. struct descriptor *ias_cmd;
  940. ias_cmd =
  941. (struct descriptor *)&sp->tx_ring[sp->cur_tx++ % TX_RING_SIZE];
  942. /* Avoid a bug(?!) here by marking the command already completed. */
  943. ias_cmd->cmd_status = cpu_to_le32((CmdSuspend | CmdIASetup) | 0xa000);
  944. ias_cmd->link =
  945. cpu_to_le32(TX_RING_ELEM_DMA(sp, sp->cur_tx % TX_RING_SIZE));
  946. memcpy(ias_cmd->params, dev->dev_addr, 6);
  947. if (sp->last_cmd)
  948. clear_suspend(sp->last_cmd);
  949. sp->last_cmd = ias_cmd;
  950. }
  951. /* Start the chip's Tx process and unmask interrupts. */
  952. iowrite32(TX_RING_ELEM_DMA(sp, sp->dirty_tx % TX_RING_SIZE),
  953. ioaddr + SCBPointer);
  954. /* We are not ACK-ing FCP and ER in the interrupt handler yet so they should
  955. remain masked --Dragan */
  956. iowrite16(CUStart | SCBMaskEarlyRx | SCBMaskFlowCtl, ioaddr + SCBCmd);
  957. }
  958. /*
  959. * Sometimes the receiver stops making progress. This routine knows how to
  960. * get it going again, without losing packets or being otherwise nasty like
  961. * a chip reset would be. Previously the driver had a whole sequence
  962. * of if RxSuspended, if it's no buffers do one thing, if it's no resources,
  963. * do another, etc. But those things don't really matter. Separate logic
  964. * in the ISR provides for allocating buffers--the other half of operation
  965. * is just making sure the receiver is active. speedo_rx_soft_reset does that.
  966. * This problem with the old, more involved algorithm is shown up under
  967. * ping floods on the order of 60K packets/second on a 100Mbps fdx network.
  968. */
  969. static void
  970. speedo_rx_soft_reset(struct net_device *dev)
  971. {
  972. struct speedo_private *sp = netdev_priv(dev);
  973. struct RxFD *rfd;
  974. void __iomem *ioaddr;
  975. ioaddr = sp->regs;
  976. if (wait_for_cmd_done(dev, sp) != 0) {
  977. printk("%s: previous command stalled\n", dev->name);
  978. return;
  979. }
  980. /*
  981. * Put the hardware into a known state.
  982. */
  983. iowrite8(RxAbort, ioaddr + SCBCmd);
  984. rfd = sp->rx_ringp[sp->cur_rx % RX_RING_SIZE];
  985. rfd->rx_buf_addr = 0xffffffff;
  986. if (wait_for_cmd_done(dev, sp) != 0) {
  987. printk("%s: RxAbort command stalled\n", dev->name);
  988. return;
  989. }
  990. iowrite32(sp->rx_ring_dma[sp->cur_rx % RX_RING_SIZE],
  991. ioaddr + SCBPointer);
  992. iowrite8(RxStart, ioaddr + SCBCmd);
  993. }
  994. /* Media monitoring and control. */
  995. static void speedo_timer(unsigned long data)
  996. {
  997. struct net_device *dev = (struct net_device *)data;
  998. struct speedo_private *sp = netdev_priv(dev);
  999. void __iomem *ioaddr = sp->regs;
  1000. int phy_num = sp->phy[0] & 0x1f;
  1001. /* We have MII and lost link beat. */
  1002. if ((sp->phy[0] & 0x8000) == 0) {
  1003. int partner = mdio_read(dev, phy_num, MII_LPA);
  1004. if (partner != sp->partner) {
  1005. int flow_ctrl = sp->mii_if.advertising & partner & 0x0400 ? 1 : 0;
  1006. if (netif_msg_link(sp)) {
  1007. printk(KERN_DEBUG "%s: Link status change.\n", dev->name);
  1008. printk(KERN_DEBUG "%s: Old partner %x, new %x, adv %x.\n",
  1009. dev->name, sp->partner, partner, sp->mii_if.advertising);
  1010. }
  1011. sp->partner = partner;
  1012. if (flow_ctrl != sp->flow_ctrl) {
  1013. sp->flow_ctrl = flow_ctrl;
  1014. sp->rx_mode = -1; /* Trigger a reload. */
  1015. }
  1016. }
  1017. }
  1018. mii_check_link(&sp->mii_if);
  1019. if (netif_msg_timer(sp)) {
  1020. printk(KERN_DEBUG "%s: Media control tick, status %4.4x.\n",
  1021. dev->name, ioread16(ioaddr + SCBStatus));
  1022. }
  1023. if (sp->rx_mode < 0 ||
  1024. (sp->rx_bug && jiffies - sp->last_rx_time > 2*HZ)) {
  1025. /* We haven't received a packet in a Long Time. We might have been
  1026. bitten by the receiver hang bug. This can be cleared by sending
  1027. a set multicast list command. */
  1028. if (netif_msg_timer(sp))
  1029. printk(KERN_DEBUG "%s: Sending a multicast list set command"
  1030. " from a timer routine,"
  1031. " m=%d, j=%ld, l=%ld.\n",
  1032. dev->name, sp->rx_mode, jiffies, sp->last_rx_time);
  1033. set_rx_mode(dev);
  1034. }
  1035. /* We must continue to monitor the media. */
  1036. sp->timer.expires = RUN_AT(2*HZ); /* 2.0 sec. */
  1037. add_timer(&sp->timer);
  1038. }
  1039. static void speedo_show_state(struct net_device *dev)
  1040. {
  1041. struct speedo_private *sp = netdev_priv(dev);
  1042. int i;
  1043. if (netif_msg_pktdata(sp)) {
  1044. printk(KERN_DEBUG "%s: Tx ring dump, Tx queue %u / %u:\n",
  1045. dev->name, sp->cur_tx, sp->dirty_tx);
  1046. for (i = 0; i < TX_RING_SIZE; i++)
  1047. printk(KERN_DEBUG "%s: %c%c%2d %8.8x.\n", dev->name,
  1048. i == sp->dirty_tx % TX_RING_SIZE ? '*' : ' ',
  1049. i == sp->cur_tx % TX_RING_SIZE ? '=' : ' ',
  1050. i, sp->tx_ring[i].status);
  1051. printk(KERN_DEBUG "%s: Printing Rx ring"
  1052. " (next to receive into %u, dirty index %u).\n",
  1053. dev->name, sp->cur_rx, sp->dirty_rx);
  1054. for (i = 0; i < RX_RING_SIZE; i++)
  1055. printk(KERN_DEBUG "%s: %c%c%c%2d %8.8x.\n", dev->name,
  1056. sp->rx_ringp[i] == sp->last_rxf ? 'l' : ' ',
  1057. i == sp->dirty_rx % RX_RING_SIZE ? '*' : ' ',
  1058. i == sp->cur_rx % RX_RING_SIZE ? '=' : ' ',
  1059. i, (sp->rx_ringp[i] != NULL) ?
  1060. (unsigned)sp->rx_ringp[i]->status : 0);
  1061. }
  1062. #if 0
  1063. {
  1064. void __iomem *ioaddr = sp->regs;
  1065. int phy_num = sp->phy[0] & 0x1f;
  1066. for (i = 0; i < 16; i++) {
  1067. /* FIXME: what does it mean? --SAW */
  1068. if (i == 6) i = 21;
  1069. printk(KERN_DEBUG "%s: PHY index %d register %d is %4.4x.\n",
  1070. dev->name, phy_num, i, mdio_read(dev, phy_num, i));
  1071. }
  1072. }
  1073. #endif
  1074. }
  1075. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  1076. static void
  1077. speedo_init_rx_ring(struct net_device *dev)
  1078. {
  1079. struct speedo_private *sp = netdev_priv(dev);
  1080. struct RxFD *rxf, *last_rxf = NULL;
  1081. dma_addr_t last_rxf_dma = 0 /* to shut up the compiler */;
  1082. int i;
  1083. sp->cur_rx = 0;
  1084. for (i = 0; i < RX_RING_SIZE; i++) {
  1085. struct sk_buff *skb;
  1086. skb = dev_alloc_skb(PKT_BUF_SZ + sizeof(struct RxFD));
  1087. if (skb)
  1088. rx_align(skb); /* Align IP on 16 byte boundary */
  1089. sp->rx_skbuff[i] = skb;
  1090. if (skb == NULL)
  1091. break; /* OK. Just initially short of Rx bufs. */
  1092. skb->dev = dev; /* Mark as being used by this device. */
  1093. rxf = (struct RxFD *)skb->data;
  1094. sp->rx_ringp[i] = rxf;
  1095. sp->rx_ring_dma[i] =
  1096. pci_map_single(sp->pdev, rxf,
  1097. PKT_BUF_SZ + sizeof(struct RxFD), PCI_DMA_BIDIRECTIONAL);
  1098. skb_reserve(skb, sizeof(struct RxFD));
  1099. if (last_rxf) {
  1100. last_rxf->link = cpu_to_le32(sp->rx_ring_dma[i]);
  1101. pci_dma_sync_single_for_device(sp->pdev, last_rxf_dma,
  1102. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1103. }
  1104. last_rxf = rxf;
  1105. last_rxf_dma = sp->rx_ring_dma[i];
  1106. rxf->status = cpu_to_le32(0x00000001); /* '1' is flag value only. */
  1107. rxf->link = 0; /* None yet. */
  1108. /* This field unused by i82557. */
  1109. rxf->rx_buf_addr = 0xffffffff;
  1110. rxf->count = cpu_to_le32(PKT_BUF_SZ << 16);
  1111. pci_dma_sync_single_for_device(sp->pdev, sp->rx_ring_dma[i],
  1112. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1113. }
  1114. sp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  1115. /* Mark the last entry as end-of-list. */
  1116. last_rxf->status = cpu_to_le32(0xC0000002); /* '2' is flag value only. */
  1117. pci_dma_sync_single_for_device(sp->pdev, sp->rx_ring_dma[RX_RING_SIZE-1],
  1118. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1119. sp->last_rxf = last_rxf;
  1120. sp->last_rxf_dma = last_rxf_dma;
  1121. }
  1122. static void speedo_purge_tx(struct net_device *dev)
  1123. {
  1124. struct speedo_private *sp = netdev_priv(dev);
  1125. int entry;
  1126. while ((int)(sp->cur_tx - sp->dirty_tx) > 0) {
  1127. entry = sp->dirty_tx % TX_RING_SIZE;
  1128. if (sp->tx_skbuff[entry]) {
  1129. sp->stats.tx_errors++;
  1130. pci_unmap_single(sp->pdev,
  1131. le32_to_cpu(sp->tx_ring[entry].tx_buf_addr0),
  1132. sp->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
  1133. dev_kfree_skb_irq(sp->tx_skbuff[entry]);
  1134. sp->tx_skbuff[entry] = NULL;
  1135. }
  1136. sp->dirty_tx++;
  1137. }
  1138. while (sp->mc_setup_head != NULL) {
  1139. struct speedo_mc_block *t;
  1140. if (netif_msg_tx_err(sp))
  1141. printk(KERN_DEBUG "%s: freeing mc frame.\n", dev->name);
  1142. pci_unmap_single(sp->pdev, sp->mc_setup_head->frame_dma,
  1143. sp->mc_setup_head->len, PCI_DMA_TODEVICE);
  1144. t = sp->mc_setup_head->next;
  1145. kfree(sp->mc_setup_head);
  1146. sp->mc_setup_head = t;
  1147. }
  1148. sp->mc_setup_tail = NULL;
  1149. sp->tx_full = 0;
  1150. netif_wake_queue(dev);
  1151. }
  1152. static void reset_mii(struct net_device *dev)
  1153. {
  1154. struct speedo_private *sp = netdev_priv(dev);
  1155. /* Reset the MII transceiver, suggested by Fred Young @ scalable.com. */
  1156. if ((sp->phy[0] & 0x8000) == 0) {
  1157. int phy_addr = sp->phy[0] & 0x1f;
  1158. int advertising = mdio_read(dev, phy_addr, MII_ADVERTISE);
  1159. int mii_bmcr = mdio_read(dev, phy_addr, MII_BMCR);
  1160. mdio_write(dev, phy_addr, MII_BMCR, 0x0400);
  1161. mdio_write(dev, phy_addr, MII_BMSR, 0x0000);
  1162. mdio_write(dev, phy_addr, MII_ADVERTISE, 0x0000);
  1163. mdio_write(dev, phy_addr, MII_BMCR, 0x8000);
  1164. #ifdef honor_default_port
  1165. mdio_write(dev, phy_addr, MII_BMCR, mii_ctrl[dev->default_port & 7]);
  1166. #else
  1167. mdio_read(dev, phy_addr, MII_BMCR);
  1168. mdio_write(dev, phy_addr, MII_BMCR, mii_bmcr);
  1169. mdio_write(dev, phy_addr, MII_ADVERTISE, advertising);
  1170. #endif
  1171. }
  1172. }
  1173. static void speedo_tx_timeout(struct net_device *dev)
  1174. {
  1175. struct speedo_private *sp = netdev_priv(dev);
  1176. void __iomem *ioaddr = sp->regs;
  1177. int status = ioread16(ioaddr + SCBStatus);
  1178. unsigned long flags;
  1179. if (netif_msg_tx_err(sp)) {
  1180. printk(KERN_WARNING "%s: Transmit timed out: status %4.4x "
  1181. " %4.4x at %d/%d command %8.8x.\n",
  1182. dev->name, status, ioread16(ioaddr + SCBCmd),
  1183. sp->dirty_tx, sp->cur_tx,
  1184. sp->tx_ring[sp->dirty_tx % TX_RING_SIZE].status);
  1185. }
  1186. speedo_show_state(dev);
  1187. #if 0
  1188. if ((status & 0x00C0) != 0x0080
  1189. && (status & 0x003C) == 0x0010) {
  1190. /* Only the command unit has stopped. */
  1191. printk(KERN_WARNING "%s: Trying to restart the transmitter...\n",
  1192. dev->name);
  1193. iowrite32(TX_RING_ELEM_DMA(sp, dirty_tx % TX_RING_SIZE]),
  1194. ioaddr + SCBPointer);
  1195. iowrite16(CUStart, ioaddr + SCBCmd);
  1196. reset_mii(dev);
  1197. } else {
  1198. #else
  1199. {
  1200. #endif
  1201. del_timer_sync(&sp->timer);
  1202. /* Reset the Tx and Rx units. */
  1203. iowrite32(PortReset, ioaddr + SCBPort);
  1204. /* We may get spurious interrupts here. But I don't think that they
  1205. may do much harm. 1999/12/09 SAW */
  1206. udelay(10);
  1207. /* Disable interrupts. */
  1208. iowrite16(SCBMaskAll, ioaddr + SCBCmd);
  1209. synchronize_irq(dev->irq);
  1210. speedo_tx_buffer_gc(dev);
  1211. /* Free as much as possible.
  1212. It helps to recover from a hang because of out-of-memory.
  1213. It also simplifies speedo_resume() in case TX ring is full or
  1214. close-to-be full. */
  1215. speedo_purge_tx(dev);
  1216. speedo_refill_rx_buffers(dev, 1);
  1217. spin_lock_irqsave(&sp->lock, flags);
  1218. speedo_resume(dev);
  1219. sp->rx_mode = -1;
  1220. dev->trans_start = jiffies;
  1221. spin_unlock_irqrestore(&sp->lock, flags);
  1222. set_rx_mode(dev); /* it takes the spinlock itself --SAW */
  1223. /* Reset MII transceiver. Do it before starting the timer to serialize
  1224. mdio_xxx operations. Yes, it's a paranoya :-) 2000/05/09 SAW */
  1225. reset_mii(dev);
  1226. sp->timer.expires = RUN_AT(2*HZ);
  1227. add_timer(&sp->timer);
  1228. }
  1229. return;
  1230. }
  1231. static int
  1232. speedo_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1233. {
  1234. struct speedo_private *sp = netdev_priv(dev);
  1235. void __iomem *ioaddr = sp->regs;
  1236. int entry;
  1237. /* Prevent interrupts from changing the Tx ring from underneath us. */
  1238. unsigned long flags;
  1239. spin_lock_irqsave(&sp->lock, flags);
  1240. /* Check if there are enough space. */
  1241. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  1242. printk(KERN_ERR "%s: incorrect tbusy state, fixed.\n", dev->name);
  1243. netif_stop_queue(dev);
  1244. sp->tx_full = 1;
  1245. spin_unlock_irqrestore(&sp->lock, flags);
  1246. return 1;
  1247. }
  1248. /* Calculate the Tx descriptor entry. */
  1249. entry = sp->cur_tx++ % TX_RING_SIZE;
  1250. sp->tx_skbuff[entry] = skb;
  1251. sp->tx_ring[entry].status =
  1252. cpu_to_le32(CmdSuspend | CmdTx | CmdTxFlex);
  1253. if (!(entry & ((TX_RING_SIZE>>2)-1)))
  1254. sp->tx_ring[entry].status |= cpu_to_le32(CmdIntr);
  1255. sp->tx_ring[entry].link =
  1256. cpu_to_le32(TX_RING_ELEM_DMA(sp, sp->cur_tx % TX_RING_SIZE));
  1257. sp->tx_ring[entry].tx_desc_addr =
  1258. cpu_to_le32(TX_RING_ELEM_DMA(sp, entry) + TX_DESCR_BUF_OFFSET);
  1259. /* The data region is always in one buffer descriptor. */
  1260. sp->tx_ring[entry].count = cpu_to_le32(sp->tx_threshold);
  1261. sp->tx_ring[entry].tx_buf_addr0 =
  1262. cpu_to_le32(pci_map_single(sp->pdev, skb->data,
  1263. skb->len, PCI_DMA_TODEVICE));
  1264. sp->tx_ring[entry].tx_buf_size0 = cpu_to_le32(skb->len);
  1265. /* workaround for hardware bug on 10 mbit half duplex */
  1266. if ((sp->partner == 0) && (sp->chip_id == 1)) {
  1267. wait_for_cmd_done(dev, sp);
  1268. iowrite8(0 , ioaddr + SCBCmd);
  1269. udelay(1);
  1270. }
  1271. /* Trigger the command unit resume. */
  1272. wait_for_cmd_done(dev, sp);
  1273. clear_suspend(sp->last_cmd);
  1274. /* We want the time window between clearing suspend flag on the previous
  1275. command and resuming CU to be as small as possible.
  1276. Interrupts in between are very undesired. --SAW */
  1277. iowrite8(CUResume, ioaddr + SCBCmd);
  1278. sp->last_cmd = (struct descriptor *)&sp->tx_ring[entry];
  1279. /* Leave room for set_rx_mode(). If there is no more space than reserved
  1280. for multicast filter mark the ring as full. */
  1281. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  1282. netif_stop_queue(dev);
  1283. sp->tx_full = 1;
  1284. }
  1285. spin_unlock_irqrestore(&sp->lock, flags);
  1286. dev->trans_start = jiffies;
  1287. return 0;
  1288. }
  1289. static void speedo_tx_buffer_gc(struct net_device *dev)
  1290. {
  1291. unsigned int dirty_tx;
  1292. struct speedo_private *sp = netdev_priv(dev);
  1293. dirty_tx = sp->dirty_tx;
  1294. while ((int)(sp->cur_tx - dirty_tx) > 0) {
  1295. int entry = dirty_tx % TX_RING_SIZE;
  1296. int status = le32_to_cpu(sp->tx_ring[entry].status);
  1297. if (netif_msg_tx_done(sp))
  1298. printk(KERN_DEBUG " scavenge candidate %d status %4.4x.\n",
  1299. entry, status);
  1300. if ((status & StatusComplete) == 0)
  1301. break; /* It still hasn't been processed. */
  1302. if (status & TxUnderrun)
  1303. if (sp->tx_threshold < 0x01e08000) {
  1304. if (netif_msg_tx_err(sp))
  1305. printk(KERN_DEBUG "%s: TX underrun, threshold adjusted.\n",
  1306. dev->name);
  1307. sp->tx_threshold += 0x00040000;
  1308. }
  1309. /* Free the original skb. */
  1310. if (sp->tx_skbuff[entry]) {
  1311. sp->stats.tx_packets++; /* Count only user packets. */
  1312. sp->stats.tx_bytes += sp->tx_skbuff[entry]->len;
  1313. pci_unmap_single(sp->pdev,
  1314. le32_to_cpu(sp->tx_ring[entry].tx_buf_addr0),
  1315. sp->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
  1316. dev_kfree_skb_irq(sp->tx_skbuff[entry]);
  1317. sp->tx_skbuff[entry] = NULL;
  1318. }
  1319. dirty_tx++;
  1320. }
  1321. if (netif_msg_tx_err(sp) && (int)(sp->cur_tx - dirty_tx) > TX_RING_SIZE) {
  1322. printk(KERN_ERR "out-of-sync dirty pointer, %d vs. %d,"
  1323. " full=%d.\n",
  1324. dirty_tx, sp->cur_tx, sp->tx_full);
  1325. dirty_tx += TX_RING_SIZE;
  1326. }
  1327. while (sp->mc_setup_head != NULL
  1328. && (int)(dirty_tx - sp->mc_setup_head->tx - 1) > 0) {
  1329. struct speedo_mc_block *t;
  1330. if (netif_msg_tx_err(sp))
  1331. printk(KERN_DEBUG "%s: freeing mc frame.\n", dev->name);
  1332. pci_unmap_single(sp->pdev, sp->mc_setup_head->frame_dma,
  1333. sp->mc_setup_head->len, PCI_DMA_TODEVICE);
  1334. t = sp->mc_setup_head->next;
  1335. kfree(sp->mc_setup_head);
  1336. sp->mc_setup_head = t;
  1337. }
  1338. if (sp->mc_setup_head == NULL)
  1339. sp->mc_setup_tail = NULL;
  1340. sp->dirty_tx = dirty_tx;
  1341. }
  1342. /* The interrupt handler does all of the Rx thread work and cleans up
  1343. after the Tx thread. */
  1344. static irqreturn_t speedo_interrupt(int irq, void *dev_instance)
  1345. {
  1346. struct net_device *dev = (struct net_device *)dev_instance;
  1347. struct speedo_private *sp;
  1348. void __iomem *ioaddr;
  1349. long boguscnt = max_interrupt_work;
  1350. unsigned short status;
  1351. unsigned int handled = 0;
  1352. sp = netdev_priv(dev);
  1353. ioaddr = sp->regs;
  1354. #ifndef final_version
  1355. /* A lock to prevent simultaneous entry on SMP machines. */
  1356. if (test_and_set_bit(0, (void*)&sp->in_interrupt)) {
  1357. printk(KERN_ERR"%s: SMP simultaneous entry of an interrupt handler.\n",
  1358. dev->name);
  1359. sp->in_interrupt = 0; /* Avoid halting machine. */
  1360. return IRQ_NONE;
  1361. }
  1362. #endif
  1363. do {
  1364. status = ioread16(ioaddr + SCBStatus);
  1365. /* Acknowledge all of the current interrupt sources ASAP. */
  1366. /* Will change from 0xfc00 to 0xff00 when we start handling
  1367. FCP and ER interrupts --Dragan */
  1368. iowrite16(status & 0xfc00, ioaddr + SCBStatus);
  1369. if (netif_msg_intr(sp))
  1370. printk(KERN_DEBUG "%s: interrupt status=%#4.4x.\n",
  1371. dev->name, status);
  1372. if ((status & 0xfc00) == 0)
  1373. break;
  1374. handled = 1;
  1375. if ((status & 0x5000) || /* Packet received, or Rx error. */
  1376. (sp->rx_ring_state&(RrNoMem|RrPostponed)) == RrPostponed)
  1377. /* Need to gather the postponed packet. */
  1378. speedo_rx(dev);
  1379. /* Always check if all rx buffers are allocated. --SAW */
  1380. speedo_refill_rx_buffers(dev, 0);
  1381. spin_lock(&sp->lock);
  1382. /*
  1383. * The chip may have suspended reception for various reasons.
  1384. * Check for that, and re-prime it should this be the case.
  1385. */
  1386. switch ((status >> 2) & 0xf) {
  1387. case 0: /* Idle */
  1388. break;
  1389. case 1: /* Suspended */
  1390. case 2: /* No resources (RxFDs) */
  1391. case 9: /* Suspended with no more RBDs */
  1392. case 10: /* No resources due to no RBDs */
  1393. case 12: /* Ready with no RBDs */
  1394. speedo_rx_soft_reset(dev);
  1395. break;
  1396. case 3: case 5: case 6: case 7: case 8:
  1397. case 11: case 13: case 14: case 15:
  1398. /* these are all reserved values */
  1399. break;
  1400. }
  1401. /* User interrupt, Command/Tx unit interrupt or CU not active. */
  1402. if (status & 0xA400) {
  1403. speedo_tx_buffer_gc(dev);
  1404. if (sp->tx_full
  1405. && (int)(sp->cur_tx - sp->dirty_tx) < TX_QUEUE_UNFULL) {
  1406. /* The ring is no longer full. */
  1407. sp->tx_full = 0;
  1408. netif_wake_queue(dev); /* Attention: under a spinlock. --SAW */
  1409. }
  1410. }
  1411. spin_unlock(&sp->lock);
  1412. if (--boguscnt < 0) {
  1413. printk(KERN_ERR "%s: Too much work at interrupt, status=0x%4.4x.\n",
  1414. dev->name, status);
  1415. /* Clear all interrupt sources. */
  1416. /* Will change from 0xfc00 to 0xff00 when we start handling
  1417. FCP and ER interrupts --Dragan */
  1418. iowrite16(0xfc00, ioaddr + SCBStatus);
  1419. break;
  1420. }
  1421. } while (1);
  1422. if (netif_msg_intr(sp))
  1423. printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
  1424. dev->name, ioread16(ioaddr + SCBStatus));
  1425. clear_bit(0, (void*)&sp->in_interrupt);
  1426. return IRQ_RETVAL(handled);
  1427. }
  1428. static inline struct RxFD *speedo_rx_alloc(struct net_device *dev, int entry)
  1429. {
  1430. struct speedo_private *sp = netdev_priv(dev);
  1431. struct RxFD *rxf;
  1432. struct sk_buff *skb;
  1433. /* Get a fresh skbuff to replace the consumed one. */
  1434. skb = dev_alloc_skb(PKT_BUF_SZ + sizeof(struct RxFD));
  1435. if (skb)
  1436. rx_align(skb); /* Align IP on 16 byte boundary */
  1437. sp->rx_skbuff[entry] = skb;
  1438. if (skb == NULL) {
  1439. sp->rx_ringp[entry] = NULL;
  1440. return NULL;
  1441. }
  1442. rxf = sp->rx_ringp[entry] = (struct RxFD *)skb->data;
  1443. sp->rx_ring_dma[entry] =
  1444. pci_map_single(sp->pdev, rxf,
  1445. PKT_BUF_SZ + sizeof(struct RxFD), PCI_DMA_FROMDEVICE);
  1446. skb->dev = dev;
  1447. skb_reserve(skb, sizeof(struct RxFD));
  1448. rxf->rx_buf_addr = 0xffffffff;
  1449. pci_dma_sync_single_for_device(sp->pdev, sp->rx_ring_dma[entry],
  1450. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1451. return rxf;
  1452. }
  1453. static inline void speedo_rx_link(struct net_device *dev, int entry,
  1454. struct RxFD *rxf, dma_addr_t rxf_dma)
  1455. {
  1456. struct speedo_private *sp = netdev_priv(dev);
  1457. rxf->status = cpu_to_le32(0xC0000001); /* '1' for driver use only. */
  1458. rxf->link = 0; /* None yet. */
  1459. rxf->count = cpu_to_le32(PKT_BUF_SZ << 16);
  1460. sp->last_rxf->link = cpu_to_le32(rxf_dma);
  1461. sp->last_rxf->status &= cpu_to_le32(~0xC0000000);
  1462. pci_dma_sync_single_for_device(sp->pdev, sp->last_rxf_dma,
  1463. sizeof(struct RxFD), PCI_DMA_TODEVICE);
  1464. sp->last_rxf = rxf;
  1465. sp->last_rxf_dma = rxf_dma;
  1466. }
  1467. static int speedo_refill_rx_buf(struct net_device *dev, int force)
  1468. {
  1469. struct speedo_private *sp = netdev_priv(dev);
  1470. int entry;
  1471. struct RxFD *rxf;
  1472. entry = sp->dirty_rx % RX_RING_SIZE;
  1473. if (sp->rx_skbuff[entry] == NULL) {
  1474. rxf = speedo_rx_alloc(dev, entry);
  1475. if (rxf == NULL) {
  1476. unsigned int forw;
  1477. int forw_entry;
  1478. if (netif_msg_rx_err(sp) || !(sp->rx_ring_state & RrOOMReported)) {
  1479. printk(KERN_WARNING "%s: can't fill rx buffer (force %d)!\n",
  1480. dev->name, force);
  1481. sp->rx_ring_state |= RrOOMReported;
  1482. }
  1483. speedo_show_state(dev);
  1484. if (!force)
  1485. return -1; /* Better luck next time! */
  1486. /* Borrow an skb from one of next entries. */
  1487. for (forw = sp->dirty_rx + 1; forw != sp->cur_rx; forw++)
  1488. if (sp->rx_skbuff[forw % RX_RING_SIZE] != NULL)
  1489. break;
  1490. if (forw == sp->cur_rx)
  1491. return -1;
  1492. forw_entry = forw % RX_RING_SIZE;
  1493. sp->rx_skbuff[entry] = sp->rx_skbuff[forw_entry];
  1494. sp->rx_skbuff[forw_entry] = NULL;
  1495. rxf = sp->rx_ringp[forw_entry];
  1496. sp->rx_ringp[forw_entry] = NULL;
  1497. sp->rx_ringp[entry] = rxf;
  1498. }
  1499. } else {
  1500. rxf = sp->rx_ringp[entry];
  1501. }
  1502. speedo_rx_link(dev, entry, rxf, sp->rx_ring_dma[entry]);
  1503. sp->dirty_rx++;
  1504. sp->rx_ring_state &= ~(RrNoMem|RrOOMReported); /* Mark the progress. */
  1505. return 0;
  1506. }
  1507. static void speedo_refill_rx_buffers(struct net_device *dev, int force)
  1508. {
  1509. struct speedo_private *sp = netdev_priv(dev);
  1510. /* Refill the RX ring. */
  1511. while ((int)(sp->cur_rx - sp->dirty_rx) > 0 &&
  1512. speedo_refill_rx_buf(dev, force) != -1);
  1513. }
  1514. static int
  1515. speedo_rx(struct net_device *dev)
  1516. {
  1517. struct speedo_private *sp = netdev_priv(dev);
  1518. int entry = sp->cur_rx % RX_RING_SIZE;
  1519. int rx_work_limit = sp->dirty_rx + RX_RING_SIZE - sp->cur_rx;
  1520. int alloc_ok = 1;
  1521. int npkts = 0;
  1522. if (netif_msg_intr(sp))
  1523. printk(KERN_DEBUG " In speedo_rx().\n");
  1524. /* If we own the next entry, it's a new packet. Send it up. */
  1525. while (sp->rx_ringp[entry] != NULL) {
  1526. int status;
  1527. int pkt_len;
  1528. pci_dma_sync_single_for_cpu(sp->pdev, sp->rx_ring_dma[entry],
  1529. sizeof(struct RxFD), PCI_DMA_FROMDEVICE);
  1530. status = le32_to_cpu(sp->rx_ringp[entry]->status);
  1531. pkt_len = le32_to_cpu(sp->rx_ringp[entry]->count) & 0x3fff;
  1532. if (!(status & RxComplete))
  1533. break;
  1534. if (--rx_work_limit < 0)
  1535. break;
  1536. /* Check for a rare out-of-memory case: the current buffer is
  1537. the last buffer allocated in the RX ring. --SAW */
  1538. if (sp->last_rxf == sp->rx_ringp[entry]) {
  1539. /* Postpone the packet. It'll be reaped at an interrupt when this
  1540. packet is no longer the last packet in the ring. */
  1541. if (netif_msg_rx_err(sp))
  1542. printk(KERN_DEBUG "%s: RX packet postponed!\n",
  1543. dev->name);
  1544. sp->rx_ring_state |= RrPostponed;
  1545. break;
  1546. }
  1547. if (netif_msg_rx_status(sp))
  1548. printk(KERN_DEBUG " speedo_rx() status %8.8x len %d.\n", status,
  1549. pkt_len);
  1550. if ((status & (RxErrTooBig|RxOK|0x0f90)) != RxOK) {
  1551. if (status & RxErrTooBig)
  1552. printk(KERN_ERR "%s: Ethernet frame overran the Rx buffer, "
  1553. "status %8.8x!\n", dev->name, status);
  1554. else if (! (status & RxOK)) {
  1555. /* There was a fatal error. This *should* be impossible. */
  1556. sp->stats.rx_errors++;
  1557. printk(KERN_ERR "%s: Anomalous event in speedo_rx(), "
  1558. "status %8.8x.\n",
  1559. dev->name, status);
  1560. }
  1561. } else {
  1562. struct sk_buff *skb;
  1563. /* Check if the packet is long enough to just accept without
  1564. copying to a properly sized skbuff. */
  1565. if (pkt_len < rx_copybreak
  1566. && (skb = dev_alloc_skb(pkt_len + 2)) != 0) {
  1567. skb->dev = dev;
  1568. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  1569. /* 'skb_put()' points to the start of sk_buff data area. */
  1570. pci_dma_sync_single_for_cpu(sp->pdev, sp->rx_ring_dma[entry],
  1571. sizeof(struct RxFD) + pkt_len,
  1572. PCI_DMA_FROMDEVICE);
  1573. #if 1 || USE_IP_CSUM
  1574. /* Packet is in one chunk -- we can copy + cksum. */
  1575. eth_copy_and_sum(skb, sp->rx_skbuff[entry]->data, pkt_len, 0);
  1576. skb_put(skb, pkt_len);
  1577. #else
  1578. memcpy(skb_put(skb, pkt_len), sp->rx_skbuff[entry]->data,
  1579. pkt_len);
  1580. #endif
  1581. pci_dma_sync_single_for_device(sp->pdev, sp->rx_ring_dma[entry],
  1582. sizeof(struct RxFD) + pkt_len,
  1583. PCI_DMA_FROMDEVICE);
  1584. npkts++;
  1585. } else {
  1586. /* Pass up the already-filled skbuff. */
  1587. skb = sp->rx_skbuff[entry];
  1588. if (skb == NULL) {
  1589. printk(KERN_ERR "%s: Inconsistent Rx descriptor chain.\n",
  1590. dev->name);
  1591. break;
  1592. }
  1593. sp->rx_skbuff[entry] = NULL;
  1594. skb_put(skb, pkt_len);
  1595. npkts++;
  1596. sp->rx_ringp[entry] = NULL;
  1597. pci_unmap_single(sp->pdev, sp->rx_ring_dma[entry],
  1598. PKT_BUF_SZ + sizeof(struct RxFD),
  1599. PCI_DMA_FROMDEVICE);
  1600. }
  1601. skb->protocol = eth_type_trans(skb, dev);
  1602. netif_rx(skb);
  1603. dev->last_rx = jiffies;
  1604. sp->stats.rx_packets++;
  1605. sp->stats.rx_bytes += pkt_len;
  1606. }
  1607. entry = (++sp->cur_rx) % RX_RING_SIZE;
  1608. sp->rx_ring_state &= ~RrPostponed;
  1609. /* Refill the recently taken buffers.
  1610. Do it one-by-one to handle traffic bursts better. */
  1611. if (alloc_ok && speedo_refill_rx_buf(dev, 0) == -1)
  1612. alloc_ok = 0;
  1613. }
  1614. /* Try hard to refill the recently taken buffers. */
  1615. speedo_refill_rx_buffers(dev, 1);
  1616. if (npkts)
  1617. sp->last_rx_time = jiffies;
  1618. return 0;
  1619. }
  1620. static int
  1621. speedo_close(struct net_device *dev)
  1622. {
  1623. struct speedo_private *sp = netdev_priv(dev);
  1624. void __iomem *ioaddr = sp->regs;
  1625. int i;
  1626. netdevice_stop(dev);
  1627. netif_stop_queue(dev);
  1628. if (netif_msg_ifdown(sp))
  1629. printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x.\n",
  1630. dev->name, ioread16(ioaddr + SCBStatus));
  1631. /* Shut off the media monitoring timer. */
  1632. del_timer_sync(&sp->timer);
  1633. iowrite16(SCBMaskAll, ioaddr + SCBCmd);
  1634. /* Shutting down the chip nicely fails to disable flow control. So.. */
  1635. iowrite32(PortPartialReset, ioaddr + SCBPort);
  1636. ioread32(ioaddr + SCBPort); /* flush posted write */
  1637. /*
  1638. * The chip requires a 10 microsecond quiet period. Wait here!
  1639. */
  1640. udelay(10);
  1641. free_irq(dev->irq, dev);
  1642. speedo_show_state(dev);
  1643. /* Free all the skbuffs in the Rx and Tx queues. */
  1644. for (i = 0; i < RX_RING_SIZE; i++) {
  1645. struct sk_buff *skb = sp->rx_skbuff[i];
  1646. sp->rx_skbuff[i] = NULL;
  1647. /* Clear the Rx descriptors. */
  1648. if (skb) {
  1649. pci_unmap_single(sp->pdev,
  1650. sp->rx_ring_dma[i],
  1651. PKT_BUF_SZ + sizeof(struct RxFD), PCI_DMA_FROMDEVICE);
  1652. dev_kfree_skb(skb);
  1653. }
  1654. }
  1655. for (i = 0; i < TX_RING_SIZE; i++) {
  1656. struct sk_buff *skb = sp->tx_skbuff[i];
  1657. sp->tx_skbuff[i] = NULL;
  1658. /* Clear the Tx descriptors. */
  1659. if (skb) {
  1660. pci_unmap_single(sp->pdev,
  1661. le32_to_cpu(sp->tx_ring[i].tx_buf_addr0),
  1662. skb->len, PCI_DMA_TODEVICE);
  1663. dev_kfree_skb(skb);
  1664. }
  1665. }
  1666. /* Free multicast setting blocks. */
  1667. for (i = 0; sp->mc_setup_head != NULL; i++) {
  1668. struct speedo_mc_block *t;
  1669. t = sp->mc_setup_head->next;
  1670. kfree(sp->mc_setup_head);
  1671. sp->mc_setup_head = t;
  1672. }
  1673. sp->mc_setup_tail = NULL;
  1674. if (netif_msg_ifdown(sp))
  1675. printk(KERN_DEBUG "%s: %d multicast blocks dropped.\n", dev->name, i);
  1676. pci_set_power_state(sp->pdev, PCI_D2);
  1677. return 0;
  1678. }
  1679. /* The Speedo-3 has an especially awkward and unusable method of getting
  1680. statistics out of the chip. It takes an unpredictable length of time
  1681. for the dump-stats command to complete. To avoid a busy-wait loop we
  1682. update the stats with the previous dump results, and then trigger a
  1683. new dump.
  1684. Oh, and incoming frames are dropped while executing dump-stats!
  1685. */
  1686. static struct net_device_stats *
  1687. speedo_get_stats(struct net_device *dev)
  1688. {
  1689. struct speedo_private *sp = netdev_priv(dev);
  1690. void __iomem *ioaddr = sp->regs;
  1691. /* Update only if the previous dump finished. */
  1692. if (sp->lstats->done_marker == le32_to_cpu(0xA007)) {
  1693. sp->stats.tx_aborted_errors += le32_to_cpu(sp->lstats->tx_coll16_errs);
  1694. sp->stats.tx_window_errors += le32_to_cpu(sp->lstats->tx_late_colls);
  1695. sp->stats.tx_fifo_errors += le32_to_cpu(sp->lstats->tx_underruns);
  1696. sp->stats.tx_fifo_errors += le32_to_cpu(sp->lstats->tx_lost_carrier);
  1697. /*sp->stats.tx_deferred += le32_to_cpu(sp->lstats->tx_deferred);*/
  1698. sp->stats.collisions += le32_to_cpu(sp->lstats->tx_total_colls);
  1699. sp->stats.rx_crc_errors += le32_to_cpu(sp->lstats->rx_crc_errs);
  1700. sp->stats.rx_frame_errors += le32_to_cpu(sp->lstats->rx_align_errs);
  1701. sp->stats.rx_over_errors += le32_to_cpu(sp->lstats->rx_resource_errs);
  1702. sp->stats.rx_fifo_errors += le32_to_cpu(sp->lstats->rx_overrun_errs);
  1703. sp->stats.rx_length_errors += le32_to_cpu(sp->lstats->rx_runt_errs);
  1704. sp->lstats->done_marker = 0x0000;
  1705. if (netif_running(dev)) {
  1706. unsigned long flags;
  1707. /* Take a spinlock to make wait_for_cmd_done and sending the
  1708. command atomic. --SAW */
  1709. spin_lock_irqsave(&sp->lock, flags);
  1710. wait_for_cmd_done(dev, sp);
  1711. iowrite8(CUDumpStats, ioaddr + SCBCmd);
  1712. spin_unlock_irqrestore(&sp->lock, flags);
  1713. }
  1714. }
  1715. return &sp->stats;
  1716. }
  1717. static void speedo_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1718. {
  1719. struct speedo_private *sp = netdev_priv(dev);
  1720. strncpy(info->driver, "eepro100", sizeof(info->driver)-1);
  1721. strncpy(info->version, version, sizeof(info->version)-1);
  1722. if (sp->pdev)
  1723. strcpy(info->bus_info, pci_name(sp->pdev));
  1724. }
  1725. static int speedo_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1726. {
  1727. struct speedo_private *sp = netdev_priv(dev);
  1728. spin_lock_irq(&sp->lock);
  1729. mii_ethtool_gset(&sp->mii_if, ecmd);
  1730. spin_unlock_irq(&sp->lock);
  1731. return 0;
  1732. }
  1733. static int speedo_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1734. {
  1735. struct speedo_private *sp = netdev_priv(dev);
  1736. int res;
  1737. spin_lock_irq(&sp->lock);
  1738. res = mii_ethtool_sset(&sp->mii_if, ecmd);
  1739. spin_unlock_irq(&sp->lock);
  1740. return res;
  1741. }
  1742. static int speedo_nway_reset(struct net_device *dev)
  1743. {
  1744. struct speedo_private *sp = netdev_priv(dev);
  1745. return mii_nway_restart(&sp->mii_if);
  1746. }
  1747. static u32 speedo_get_link(struct net_device *dev)
  1748. {
  1749. struct speedo_private *sp = netdev_priv(dev);
  1750. return mii_link_ok(&sp->mii_if);
  1751. }
  1752. static u32 speedo_get_msglevel(struct net_device *dev)
  1753. {
  1754. struct speedo_private *sp = netdev_priv(dev);
  1755. return sp->msg_enable;
  1756. }
  1757. static void speedo_set_msglevel(struct net_device *dev, u32 v)
  1758. {
  1759. struct speedo_private *sp = netdev_priv(dev);
  1760. sp->msg_enable = v;
  1761. }
  1762. static const struct ethtool_ops ethtool_ops = {
  1763. .get_drvinfo = speedo_get_drvinfo,
  1764. .get_settings = speedo_get_settings,
  1765. .set_settings = speedo_set_settings,
  1766. .nway_reset = speedo_nway_reset,
  1767. .get_link = speedo_get_link,
  1768. .get_msglevel = speedo_get_msglevel,
  1769. .set_msglevel = speedo_set_msglevel,
  1770. };
  1771. static int speedo_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1772. {
  1773. struct speedo_private *sp = netdev_priv(dev);
  1774. struct mii_ioctl_data *data = if_mii(rq);
  1775. int phy = sp->phy[0] & 0x1f;
  1776. int saved_acpi;
  1777. int t;
  1778. switch(cmd) {
  1779. case SIOCGMIIPHY: /* Get address of MII PHY in use. */
  1780. data->phy_id = phy;
  1781. case SIOCGMIIREG: /* Read MII PHY register. */
  1782. /* FIXME: these operations need to be serialized with MDIO
  1783. access from the timeout handler.
  1784. They are currently serialized only with MDIO access from the
  1785. timer routine. 2000/05/09 SAW */
  1786. saved_acpi = pci_set_power_state(sp->pdev, PCI_D0);
  1787. t = del_timer_sync(&sp->timer);
  1788. data->val_out = mdio_read(dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
  1789. if (t)
  1790. add_timer(&sp->timer); /* may be set to the past --SAW */
  1791. pci_set_power_state(sp->pdev, saved_acpi);
  1792. return 0;
  1793. case SIOCSMIIREG: /* Write MII PHY register. */
  1794. if (!capable(CAP_NET_ADMIN))
  1795. return -EPERM;
  1796. saved_acpi = pci_set_power_state(sp->pdev, PCI_D0);
  1797. t = del_timer_sync(&sp->timer);
  1798. mdio_write(dev, data->phy_id, data->reg_num, data->val_in);
  1799. if (t)
  1800. add_timer(&sp->timer); /* may be set to the past --SAW */
  1801. pci_set_power_state(sp->pdev, saved_acpi);
  1802. return 0;
  1803. default:
  1804. return -EOPNOTSUPP;
  1805. }
  1806. }
  1807. /* Set or clear the multicast filter for this adaptor.
  1808. This is very ugly with Intel chips -- we usually have to execute an
  1809. entire configuration command, plus process a multicast command.
  1810. This is complicated. We must put a large configuration command and
  1811. an arbitrarily-sized multicast command in the transmit list.
  1812. To minimize the disruption -- the previous command might have already
  1813. loaded the link -- we convert the current command block, normally a Tx
  1814. command, into a no-op and link it to the new command.
  1815. */
  1816. static void set_rx_mode(struct net_device *dev)
  1817. {
  1818. struct speedo_private *sp = netdev_priv(dev);
  1819. void __iomem *ioaddr = sp->regs;
  1820. struct descriptor *last_cmd;
  1821. char new_rx_mode;
  1822. unsigned long flags;
  1823. int entry, i;
  1824. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1825. new_rx_mode = 3;
  1826. } else if ((dev->flags & IFF_ALLMULTI) ||
  1827. dev->mc_count > multicast_filter_limit) {
  1828. new_rx_mode = 1;
  1829. } else
  1830. new_rx_mode = 0;
  1831. if (netif_msg_rx_status(sp))
  1832. printk(KERN_DEBUG "%s: set_rx_mode %d -> %d\n", dev->name,
  1833. sp->rx_mode, new_rx_mode);
  1834. if ((int)(sp->cur_tx - sp->dirty_tx) > TX_RING_SIZE - TX_MULTICAST_SIZE) {
  1835. /* The Tx ring is full -- don't add anything! Hope the mode will be
  1836. * set again later. */
  1837. sp->rx_mode = -1;
  1838. return;
  1839. }
  1840. if (new_rx_mode != sp->rx_mode) {
  1841. u8 *config_cmd_data;
  1842. spin_lock_irqsave(&sp->lock, flags);
  1843. entry = sp->cur_tx++ % TX_RING_SIZE;
  1844. last_cmd = sp->last_cmd;
  1845. sp->last_cmd = (struct descriptor *)&sp->tx_ring[entry];
  1846. sp->tx_skbuff[entry] = NULL; /* Redundant. */
  1847. sp->tx_ring[entry].status = cpu_to_le32(CmdSuspend | CmdConfigure);
  1848. sp->tx_ring[entry].link =
  1849. cpu_to_le32(TX_RING_ELEM_DMA(sp, (entry + 1) % TX_RING_SIZE));
  1850. config_cmd_data = (void *)&sp->tx_ring[entry].tx_desc_addr;
  1851. /* Construct a full CmdConfig frame. */
  1852. memcpy(config_cmd_data, i82558_config_cmd, CONFIG_DATA_SIZE);
  1853. config_cmd_data[1] = (txfifo << 4) | rxfifo;
  1854. config_cmd_data[4] = rxdmacount;
  1855. config_cmd_data[5] = txdmacount + 0x80;
  1856. config_cmd_data[15] |= (new_rx_mode & 2) ? 1 : 0;
  1857. /* 0x80 doesn't disable FC 0x84 does.
  1858. Disable Flow control since we are not ACK-ing any FC interrupts
  1859. for now. --Dragan */
  1860. config_cmd_data[19] = 0x84;
  1861. config_cmd_data[19] |= sp->mii_if.full_duplex ? 0x40 : 0;
  1862. config_cmd_data[21] = (new_rx_mode & 1) ? 0x0D : 0x05;
  1863. if (sp->phy[0] & 0x8000) { /* Use the AUI port instead. */
  1864. config_cmd_data[15] |= 0x80;
  1865. config_cmd_data[8] = 0;
  1866. }
  1867. /* Trigger the command unit resume. */
  1868. wait_for_cmd_done(dev, sp);
  1869. clear_suspend(last_cmd);
  1870. iowrite8(CUResume, ioaddr + SCBCmd);
  1871. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  1872. netif_stop_queue(dev);
  1873. sp->tx_full = 1;
  1874. }
  1875. spin_unlock_irqrestore(&sp->lock, flags);
  1876. }
  1877. if (new_rx_mode == 0 && dev->mc_count < 4) {
  1878. /* The simple case of 0-3 multicast list entries occurs often, and
  1879. fits within one tx_ring[] entry. */
  1880. struct dev_mc_list *mclist;
  1881. u16 *setup_params, *eaddrs;
  1882. spin_lock_irqsave(&sp->lock, flags);
  1883. entry = sp->cur_tx++ % TX_RING_SIZE;
  1884. last_cmd = sp->last_cmd;
  1885. sp->last_cmd = (struct descriptor *)&sp->tx_ring[entry];
  1886. sp->tx_skbuff[entry] = NULL;
  1887. sp->tx_ring[entry].status = cpu_to_le32(CmdSuspend | CmdMulticastList);
  1888. sp->tx_ring[entry].link =
  1889. cpu_to_le32(TX_RING_ELEM_DMA(sp, (entry + 1) % TX_RING_SIZE));
  1890. sp->tx_ring[entry].tx_desc_addr = 0; /* Really MC list count. */
  1891. setup_params = (u16 *)&sp->tx_ring[entry].tx_desc_addr;
  1892. *setup_params++ = cpu_to_le16(dev->mc_count*6);
  1893. /* Fill in the multicast addresses. */
  1894. for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
  1895. i++, mclist = mclist->next) {
  1896. eaddrs = (u16 *)mclist->dmi_addr;
  1897. *setup_params++ = *eaddrs++;
  1898. *setup_params++ = *eaddrs++;
  1899. *setup_params++ = *eaddrs++;
  1900. }
  1901. wait_for_cmd_done(dev, sp);
  1902. clear_suspend(last_cmd);
  1903. /* Immediately trigger the command unit resume. */
  1904. iowrite8(CUResume, ioaddr + SCBCmd);
  1905. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  1906. netif_stop_queue(dev);
  1907. sp->tx_full = 1;
  1908. }
  1909. spin_unlock_irqrestore(&sp->lock, flags);
  1910. } else if (new_rx_mode == 0) {
  1911. struct dev_mc_list *mclist;
  1912. u16 *setup_params, *eaddrs;
  1913. struct speedo_mc_block *mc_blk;
  1914. struct descriptor *mc_setup_frm;
  1915. int i;
  1916. mc_blk = kmalloc(sizeof(*mc_blk) + 2 + multicast_filter_limit*6,
  1917. GFP_ATOMIC);
  1918. if (mc_blk == NULL) {
  1919. printk(KERN_ERR "%s: Failed to allocate a setup frame.\n",
  1920. dev->name);
  1921. sp->rx_mode = -1; /* We failed, try again. */
  1922. return;
  1923. }
  1924. mc_blk->next = NULL;
  1925. mc_blk->len = 2 + multicast_filter_limit*6;
  1926. mc_blk->frame_dma =
  1927. pci_map_single(sp->pdev, &mc_blk->frame, mc_blk->len,
  1928. PCI_DMA_TODEVICE);
  1929. mc_setup_frm = &mc_blk->frame;
  1930. /* Fill the setup frame. */
  1931. if (netif_msg_ifup(sp))
  1932. printk(KERN_DEBUG "%s: Constructing a setup frame at %p.\n",
  1933. dev->name, mc_setup_frm);
  1934. mc_setup_frm->cmd_status =
  1935. cpu_to_le32(CmdSuspend | CmdIntr | CmdMulticastList);
  1936. /* Link set below. */
  1937. setup_params = (u16 *)&mc_setup_frm->params;
  1938. *setup_params++ = cpu_to_le16(dev->mc_count*6);
  1939. /* Fill in the multicast addresses. */
  1940. for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
  1941. i++, mclist = mclist->next) {
  1942. eaddrs = (u16 *)mclist->dmi_addr;
  1943. *setup_params++ = *eaddrs++;
  1944. *setup_params++ = *eaddrs++;
  1945. *setup_params++ = *eaddrs++;
  1946. }
  1947. /* Disable interrupts while playing with the Tx Cmd list. */
  1948. spin_lock_irqsave(&sp->lock, flags);
  1949. if (sp->mc_setup_tail)
  1950. sp->mc_setup_tail->next = mc_blk;
  1951. else
  1952. sp->mc_setup_head = mc_blk;
  1953. sp->mc_setup_tail = mc_blk;
  1954. mc_blk->tx = sp->cur_tx;
  1955. entry = sp->cur_tx++ % TX_RING_SIZE;
  1956. last_cmd = sp->last_cmd;
  1957. sp->last_cmd = mc_setup_frm;
  1958. /* Change the command to a NoOp, pointing to the CmdMulti command. */
  1959. sp->tx_skbuff[entry] = NULL;
  1960. sp->tx_ring[entry].status = cpu_to_le32(CmdNOp);
  1961. sp->tx_ring[entry].link = cpu_to_le32(mc_blk->frame_dma);
  1962. /* Set the link in the setup frame. */
  1963. mc_setup_frm->link =
  1964. cpu_to_le32(TX_RING_ELEM_DMA(sp, (entry + 1) % TX_RING_SIZE));
  1965. pci_dma_sync_single_for_device(sp->pdev, mc_blk->frame_dma,
  1966. mc_blk->len, PCI_DMA_TODEVICE);
  1967. wait_for_cmd_done(dev, sp);
  1968. clear_suspend(last_cmd);
  1969. /* Immediately trigger the command unit resume. */
  1970. iowrite8(CUResume, ioaddr + SCBCmd);
  1971. if ((int)(sp->cur_tx - sp->dirty_tx) >= TX_QUEUE_LIMIT) {
  1972. netif_stop_queue(dev);
  1973. sp->tx_full = 1;
  1974. }
  1975. spin_unlock_irqrestore(&sp->lock, flags);
  1976. if (netif_msg_rx_status(sp))
  1977. printk(" CmdMCSetup frame length %d in entry %d.\n",
  1978. dev->mc_count, entry);
  1979. }
  1980. sp->rx_mode = new_rx_mode;
  1981. }
  1982. #ifdef CONFIG_PM
  1983. static int eepro100_suspend(struct pci_dev *pdev, pm_message_t state)
  1984. {
  1985. struct net_device *dev = pci_get_drvdata (pdev);
  1986. struct speedo_private *sp = netdev_priv(dev);
  1987. void __iomem *ioaddr = sp->regs;
  1988. pci_save_state(pdev);
  1989. if (!netif_running(dev))
  1990. return 0;
  1991. del_timer_sync(&sp->timer);
  1992. netif_device_detach(dev);
  1993. iowrite32(PortPartialReset, ioaddr + SCBPort);
  1994. /* XXX call pci_set_power_state ()? */
  1995. pci_disable_device(pdev);
  1996. pci_set_power_state (pdev, PCI_D3hot);
  1997. return 0;
  1998. }
  1999. static int eepro100_resume(struct pci_dev *pdev)
  2000. {
  2001. struct net_device *dev = pci_get_drvdata (pdev);
  2002. struct speedo_private *sp = netdev_priv(dev);
  2003. void __iomem *ioaddr = sp->regs;
  2004. pci_set_power_state(pdev, PCI_D0);
  2005. pci_restore_state(pdev);
  2006. pci_enable_device(pdev);
  2007. pci_set_master(pdev);
  2008. if (!netif_running(dev))
  2009. return 0;
  2010. /* I'm absolutely uncertain if this part of code may work.
  2011. The problems are:
  2012. - correct hardware reinitialization;
  2013. - correct driver behavior between different steps of the
  2014. reinitialization;
  2015. - serialization with other driver calls.
  2016. 2000/03/08 SAW */
  2017. iowrite16(SCBMaskAll, ioaddr + SCBCmd);
  2018. speedo_resume(dev);
  2019. netif_device_attach(dev);
  2020. sp->rx_mode = -1;
  2021. sp->flow_ctrl = sp->partner = 0;
  2022. set_rx_mode(dev);
  2023. sp->timer.expires = RUN_AT(2*HZ);
  2024. add_timer(&sp->timer);
  2025. return 0;
  2026. }
  2027. #endif /* CONFIG_PM */
  2028. static void __devexit eepro100_remove_one (struct pci_dev *pdev)
  2029. {
  2030. struct net_device *dev = pci_get_drvdata (pdev);
  2031. struct speedo_private *sp = netdev_priv(dev);
  2032. unregister_netdev(dev);
  2033. release_region(pci_resource_start(pdev, 1), pci_resource_len(pdev, 1));
  2034. release_mem_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  2035. pci_iounmap(pdev, sp->regs);
  2036. pci_free_consistent(pdev, TX_RING_SIZE * sizeof(struct TxFD)
  2037. + sizeof(struct speedo_stats),
  2038. sp->tx_ring, sp->tx_ring_dma);
  2039. pci_disable_device(pdev);
  2040. free_netdev(dev);
  2041. }
  2042. static struct pci_device_id eepro100_pci_tbl[] = {
  2043. { PCI_VENDOR_ID_INTEL, 0x1229, PCI_ANY_ID, PCI_ANY_ID, },
  2044. { PCI_VENDOR_ID_INTEL, 0x1209, PCI_ANY_ID, PCI_ANY_ID, },
  2045. { PCI_VENDOR_ID_INTEL, 0x1029, PCI_ANY_ID, PCI_ANY_ID, },
  2046. { PCI_VENDOR_ID_INTEL, 0x1030, PCI_ANY_ID, PCI_ANY_ID, },
  2047. { PCI_VENDOR_ID_INTEL, 0x1031, PCI_ANY_ID, PCI_ANY_ID, },
  2048. { PCI_VENDOR_ID_INTEL, 0x1032, PCI_ANY_ID, PCI_ANY_ID, },
  2049. { PCI_VENDOR_ID_INTEL, 0x1033, PCI_ANY_ID, PCI_ANY_ID, },
  2050. { PCI_VENDOR_ID_INTEL, 0x1034, PCI_ANY_ID, PCI_ANY_ID, },
  2051. { PCI_VENDOR_ID_INTEL, 0x1035, PCI_ANY_ID, PCI_ANY_ID, },
  2052. { PCI_VENDOR_ID_INTEL, 0x1036, PCI_ANY_ID, PCI_ANY_ID, },
  2053. { PCI_VENDOR_ID_INTEL, 0x1037, PCI_ANY_ID, PCI_ANY_ID, },
  2054. { PCI_VENDOR_ID_INTEL, 0x1038, PCI_ANY_ID, PCI_ANY_ID, },
  2055. { PCI_VENDOR_ID_INTEL, 0x1039, PCI_ANY_ID, PCI_ANY_ID, },
  2056. { PCI_VENDOR_ID_INTEL, 0x103A, PCI_ANY_ID, PCI_ANY_ID, },
  2057. { PCI_VENDOR_ID_INTEL, 0x103B, PCI_ANY_ID, PCI_ANY_ID, },
  2058. { PCI_VENDOR_ID_INTEL, 0x103C, PCI_ANY_ID, PCI_ANY_ID, },
  2059. { PCI_VENDOR_ID_INTEL, 0x103D, PCI_ANY_ID, PCI_ANY_ID, },
  2060. { PCI_VENDOR_ID_INTEL, 0x103E, PCI_ANY_ID, PCI_ANY_ID, },
  2061. { PCI_VENDOR_ID_INTEL, 0x1050, PCI_ANY_ID, PCI_ANY_ID, },
  2062. { PCI_VENDOR_ID_INTEL, 0x1059, PCI_ANY_ID, PCI_ANY_ID, },
  2063. { PCI_VENDOR_ID_INTEL, 0x1227, PCI_ANY_ID, PCI_ANY_ID, },
  2064. { PCI_VENDOR_ID_INTEL, 0x2449, PCI_ANY_ID, PCI_ANY_ID, },
  2065. { PCI_VENDOR_ID_INTEL, 0x2459, PCI_ANY_ID, PCI_ANY_ID, },
  2066. { PCI_VENDOR_ID_INTEL, 0x245D, PCI_ANY_ID, PCI_ANY_ID, },
  2067. { PCI_VENDOR_ID_INTEL, 0x5200, PCI_ANY_ID, PCI_ANY_ID, },
  2068. { PCI_VENDOR_ID_INTEL, 0x5201, PCI_ANY_ID, PCI_ANY_ID, },
  2069. { 0,}
  2070. };
  2071. MODULE_DEVICE_TABLE(pci, eepro100_pci_tbl);
  2072. static struct pci_driver eepro100_driver = {
  2073. .name = "eepro100",
  2074. .id_table = eepro100_pci_tbl,
  2075. .probe = eepro100_init_one,
  2076. .remove = __devexit_p(eepro100_remove_one),
  2077. #ifdef CONFIG_PM
  2078. .suspend = eepro100_suspend,
  2079. .resume = eepro100_resume,
  2080. #endif /* CONFIG_PM */
  2081. };
  2082. static int __init eepro100_init_module(void)
  2083. {
  2084. #ifdef MODULE
  2085. printk(version);
  2086. #endif
  2087. return pci_register_driver(&eepro100_driver);
  2088. }
  2089. static void __exit eepro100_cleanup_module(void)
  2090. {
  2091. pci_unregister_driver(&eepro100_driver);
  2092. }
  2093. module_init(eepro100_init_module);
  2094. module_exit(eepro100_cleanup_module);
  2095. /*
  2096. * Local variables:
  2097. * compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -c eepro100.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
  2098. * c-indent-level: 4
  2099. * c-basic-offset: 4
  2100. * tab-width: 4
  2101. * End:
  2102. */