fealnx.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. /*
  2. Written 1998-2000 by Donald Becker.
  3. This software may be used and distributed according to the terms of
  4. the GNU General Public License (GPL), incorporated herein by reference.
  5. Drivers based on or derived from this code fall under the GPL and must
  6. retain the authorship, copyright and license notice. This file is not
  7. a complete program and may only be used when the entire operating
  8. system is licensed under the GPL.
  9. The author may be reached as becker@scyld.com, or C/O
  10. Scyld Computing Corporation
  11. 410 Severn Ave., Suite 210
  12. Annapolis MD 21403
  13. Support information and updates available at
  14. http://www.scyld.com/network/pci-skeleton.html
  15. Linux kernel updates:
  16. Version 2.51, Nov 17, 2001 (jgarzik):
  17. - Add ethtool support
  18. - Replace some MII-related magic numbers with constants
  19. */
  20. #define DRV_NAME "fealnx"
  21. #define DRV_VERSION "2.52"
  22. #define DRV_RELDATE "Sep-11-2006"
  23. static int debug; /* 1-> print debug message */
  24. static int max_interrupt_work = 20;
  25. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). */
  26. static int multicast_filter_limit = 32;
  27. /* Set the copy breakpoint for the copy-only-tiny-frames scheme. */
  28. /* Setting to > 1518 effectively disables this feature. */
  29. static int rx_copybreak;
  30. /* Used to pass the media type, etc. */
  31. /* Both 'options[]' and 'full_duplex[]' should exist for driver */
  32. /* interoperability. */
  33. /* The media type is usually passed in 'options[]'. */
  34. #define MAX_UNITS 8 /* More are supported, limit only on options */
  35. static int options[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
  36. static int full_duplex[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
  37. /* Operational parameters that are set at compile time. */
  38. /* Keep the ring sizes a power of two for compile efficiency. */
  39. /* The compiler will convert <unsigned>'%'<2^N> into a bit mask. */
  40. /* Making the Tx ring too large decreases the effectiveness of channel */
  41. /* bonding and packet priority. */
  42. /* There are no ill effects from too-large receive rings. */
  43. // 88-12-9 modify,
  44. // #define TX_RING_SIZE 16
  45. // #define RX_RING_SIZE 32
  46. #define TX_RING_SIZE 6
  47. #define RX_RING_SIZE 12
  48. #define TX_TOTAL_SIZE TX_RING_SIZE*sizeof(struct fealnx_desc)
  49. #define RX_TOTAL_SIZE RX_RING_SIZE*sizeof(struct fealnx_desc)
  50. /* Operational parameters that usually are not changed. */
  51. /* Time in jiffies before concluding the transmitter is hung. */
  52. #define TX_TIMEOUT (2*HZ)
  53. #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer. */
  54. /* Include files, designed to support most kernel versions 2.0.0 and later. */
  55. #include <linux/module.h>
  56. #include <linux/kernel.h>
  57. #include <linux/string.h>
  58. #include <linux/timer.h>
  59. #include <linux/errno.h>
  60. #include <linux/ioport.h>
  61. #include <linux/interrupt.h>
  62. #include <linux/pci.h>
  63. #include <linux/netdevice.h>
  64. #include <linux/etherdevice.h>
  65. #include <linux/skbuff.h>
  66. #include <linux/init.h>
  67. #include <linux/mii.h>
  68. #include <linux/ethtool.h>
  69. #include <linux/crc32.h>
  70. #include <linux/delay.h>
  71. #include <linux/bitops.h>
  72. #include <asm/processor.h> /* Processor type for cache alignment. */
  73. #include <asm/io.h>
  74. #include <linux/uaccess.h>
  75. #include <asm/byteorder.h>
  76. /* These identify the driver base version and may not be removed. */
  77. static const char version[] =
  78. KERN_INFO DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE "\n";
  79. /* This driver was written to use PCI memory space, however some x86 systems
  80. work only with I/O space accesses. */
  81. #ifndef __alpha__
  82. #define USE_IO_OPS
  83. #endif
  84. /* Kernel compatibility defines, some common to David Hinds' PCMCIA package. */
  85. /* This is only in the support-all-kernels source code. */
  86. #define RUN_AT(x) (jiffies + (x))
  87. MODULE_AUTHOR("Myson or whoever");
  88. MODULE_DESCRIPTION("Myson MTD-8xx 100/10M Ethernet PCI Adapter Driver");
  89. MODULE_LICENSE("GPL");
  90. module_param(max_interrupt_work, int, 0);
  91. module_param(debug, int, 0);
  92. module_param(rx_copybreak, int, 0);
  93. module_param(multicast_filter_limit, int, 0);
  94. module_param_array(options, int, NULL, 0);
  95. module_param_array(full_duplex, int, NULL, 0);
  96. MODULE_PARM_DESC(max_interrupt_work, "fealnx maximum events handled per interrupt");
  97. MODULE_PARM_DESC(debug, "fealnx enable debugging (0-1)");
  98. MODULE_PARM_DESC(rx_copybreak, "fealnx copy breakpoint for copy-only-tiny-frames");
  99. MODULE_PARM_DESC(multicast_filter_limit, "fealnx maximum number of filtered multicast addresses");
  100. MODULE_PARM_DESC(options, "fealnx: Bits 0-3: media type, bit 17: full duplex");
  101. MODULE_PARM_DESC(full_duplex, "fealnx full duplex setting(s) (1)");
  102. enum {
  103. MIN_REGION_SIZE = 136,
  104. };
  105. /* A chip capabilities table, matching the entries in pci_tbl[] above. */
  106. enum chip_capability_flags {
  107. HAS_MII_XCVR,
  108. HAS_CHIP_XCVR,
  109. };
  110. /* 89/6/13 add, */
  111. /* for different PHY */
  112. enum phy_type_flags {
  113. MysonPHY = 1,
  114. AhdocPHY = 2,
  115. SeeqPHY = 3,
  116. MarvellPHY = 4,
  117. Myson981 = 5,
  118. LevelOnePHY = 6,
  119. OtherPHY = 10,
  120. };
  121. struct chip_info {
  122. char *chip_name;
  123. int flags;
  124. };
  125. static const struct chip_info skel_netdrv_tbl[] = {
  126. { "100/10M Ethernet PCI Adapter", HAS_MII_XCVR },
  127. { "100/10M Ethernet PCI Adapter", HAS_CHIP_XCVR },
  128. { "1000/100/10M Ethernet PCI Adapter", HAS_MII_XCVR },
  129. };
  130. /* Offsets to the Command and Status Registers. */
  131. enum fealnx_offsets {
  132. PAR0 = 0x0, /* physical address 0-3 */
  133. PAR1 = 0x04, /* physical address 4-5 */
  134. MAR0 = 0x08, /* multicast address 0-3 */
  135. MAR1 = 0x0C, /* multicast address 4-7 */
  136. FAR0 = 0x10, /* flow-control address 0-3 */
  137. FAR1 = 0x14, /* flow-control address 4-5 */
  138. TCRRCR = 0x18, /* receive & transmit configuration */
  139. BCR = 0x1C, /* bus command */
  140. TXPDR = 0x20, /* transmit polling demand */
  141. RXPDR = 0x24, /* receive polling demand */
  142. RXCWP = 0x28, /* receive current word pointer */
  143. TXLBA = 0x2C, /* transmit list base address */
  144. RXLBA = 0x30, /* receive list base address */
  145. ISR = 0x34, /* interrupt status */
  146. IMR = 0x38, /* interrupt mask */
  147. FTH = 0x3C, /* flow control high/low threshold */
  148. MANAGEMENT = 0x40, /* bootrom/eeprom and mii management */
  149. TALLY = 0x44, /* tally counters for crc and mpa */
  150. TSR = 0x48, /* tally counter for transmit status */
  151. BMCRSR = 0x4c, /* basic mode control and status */
  152. PHYIDENTIFIER = 0x50, /* phy identifier */
  153. ANARANLPAR = 0x54, /* auto-negotiation advertisement and link
  154. partner ability */
  155. ANEROCR = 0x58, /* auto-negotiation expansion and pci conf. */
  156. BPREMRPSR = 0x5c, /* bypass & receive error mask and phy status */
  157. };
  158. /* Bits in the interrupt status/enable registers. */
  159. /* The bits in the Intr Status/Enable registers, mostly interrupt sources. */
  160. enum intr_status_bits {
  161. RFCON = 0x00020000, /* receive flow control xon packet */
  162. RFCOFF = 0x00010000, /* receive flow control xoff packet */
  163. LSCStatus = 0x00008000, /* link status change */
  164. ANCStatus = 0x00004000, /* autonegotiation completed */
  165. FBE = 0x00002000, /* fatal bus error */
  166. FBEMask = 0x00001800, /* mask bit12-11 */
  167. ParityErr = 0x00000000, /* parity error */
  168. TargetErr = 0x00001000, /* target abort */
  169. MasterErr = 0x00000800, /* master error */
  170. TUNF = 0x00000400, /* transmit underflow */
  171. ROVF = 0x00000200, /* receive overflow */
  172. ETI = 0x00000100, /* transmit early int */
  173. ERI = 0x00000080, /* receive early int */
  174. CNTOVF = 0x00000040, /* counter overflow */
  175. RBU = 0x00000020, /* receive buffer unavailable */
  176. TBU = 0x00000010, /* transmit buffer unavilable */
  177. TI = 0x00000008, /* transmit interrupt */
  178. RI = 0x00000004, /* receive interrupt */
  179. RxErr = 0x00000002, /* receive error */
  180. };
  181. /* Bits in the NetworkConfig register, W for writing, R for reading */
  182. /* FIXME: some names are invented by me. Marked with (name?) */
  183. /* If you have docs and know bit names, please fix 'em */
  184. enum rx_mode_bits {
  185. CR_W_ENH = 0x02000000, /* enhanced mode (name?) */
  186. CR_W_FD = 0x00100000, /* full duplex */
  187. CR_W_PS10 = 0x00080000, /* 10 mbit */
  188. CR_W_TXEN = 0x00040000, /* tx enable (name?) */
  189. CR_W_PS1000 = 0x00010000, /* 1000 mbit */
  190. /* CR_W_RXBURSTMASK= 0x00000e00, Im unsure about this */
  191. CR_W_RXMODEMASK = 0x000000e0,
  192. CR_W_PROM = 0x00000080, /* promiscuous mode */
  193. CR_W_AB = 0x00000040, /* accept broadcast */
  194. CR_W_AM = 0x00000020, /* accept mutlicast */
  195. CR_W_ARP = 0x00000008, /* receive runt pkt */
  196. CR_W_ALP = 0x00000004, /* receive long pkt */
  197. CR_W_SEP = 0x00000002, /* receive error pkt */
  198. CR_W_RXEN = 0x00000001, /* rx enable (unicast?) (name?) */
  199. CR_R_TXSTOP = 0x04000000, /* tx stopped (name?) */
  200. CR_R_FD = 0x00100000, /* full duplex detected */
  201. CR_R_PS10 = 0x00080000, /* 10 mbit detected */
  202. CR_R_RXSTOP = 0x00008000, /* rx stopped (name?) */
  203. };
  204. /* The Tulip Rx and Tx buffer descriptors. */
  205. struct fealnx_desc {
  206. s32 status;
  207. s32 control;
  208. u32 buffer;
  209. u32 next_desc;
  210. struct fealnx_desc *next_desc_logical;
  211. struct sk_buff *skbuff;
  212. u32 reserved1;
  213. u32 reserved2;
  214. };
  215. /* Bits in network_desc.status */
  216. enum rx_desc_status_bits {
  217. RXOWN = 0x80000000, /* own bit */
  218. FLNGMASK = 0x0fff0000, /* frame length */
  219. FLNGShift = 16,
  220. MARSTATUS = 0x00004000, /* multicast address received */
  221. BARSTATUS = 0x00002000, /* broadcast address received */
  222. PHYSTATUS = 0x00001000, /* physical address received */
  223. RXFSD = 0x00000800, /* first descriptor */
  224. RXLSD = 0x00000400, /* last descriptor */
  225. ErrorSummary = 0x80, /* error summary */
  226. RUNTPKT = 0x40, /* runt packet received */
  227. LONGPKT = 0x20, /* long packet received */
  228. FAE = 0x10, /* frame align error */
  229. CRC = 0x08, /* crc error */
  230. RXER = 0x04, /* receive error */
  231. };
  232. enum rx_desc_control_bits {
  233. RXIC = 0x00800000, /* interrupt control */
  234. RBSShift = 0,
  235. };
  236. enum tx_desc_status_bits {
  237. TXOWN = 0x80000000, /* own bit */
  238. JABTO = 0x00004000, /* jabber timeout */
  239. CSL = 0x00002000, /* carrier sense lost */
  240. LC = 0x00001000, /* late collision */
  241. EC = 0x00000800, /* excessive collision */
  242. UDF = 0x00000400, /* fifo underflow */
  243. DFR = 0x00000200, /* deferred */
  244. HF = 0x00000100, /* heartbeat fail */
  245. NCRMask = 0x000000ff, /* collision retry count */
  246. NCRShift = 0,
  247. };
  248. enum tx_desc_control_bits {
  249. TXIC = 0x80000000, /* interrupt control */
  250. ETIControl = 0x40000000, /* early transmit interrupt */
  251. TXLD = 0x20000000, /* last descriptor */
  252. TXFD = 0x10000000, /* first descriptor */
  253. CRCEnable = 0x08000000, /* crc control */
  254. PADEnable = 0x04000000, /* padding control */
  255. RetryTxLC = 0x02000000, /* retry late collision */
  256. PKTSMask = 0x3ff800, /* packet size bit21-11 */
  257. PKTSShift = 11,
  258. TBSMask = 0x000007ff, /* transmit buffer bit 10-0 */
  259. TBSShift = 0,
  260. };
  261. /* BootROM/EEPROM/MII Management Register */
  262. #define MASK_MIIR_MII_READ 0x00000000
  263. #define MASK_MIIR_MII_WRITE 0x00000008
  264. #define MASK_MIIR_MII_MDO 0x00000004
  265. #define MASK_MIIR_MII_MDI 0x00000002
  266. #define MASK_MIIR_MII_MDC 0x00000001
  267. /* ST+OP+PHYAD+REGAD+TA */
  268. #define OP_READ 0x6000 /* ST:01+OP:10+PHYAD+REGAD+TA:Z0 */
  269. #define OP_WRITE 0x5002 /* ST:01+OP:01+PHYAD+REGAD+TA:10 */
  270. /* ------------------------------------------------------------------------- */
  271. /* Constants for Myson PHY */
  272. /* ------------------------------------------------------------------------- */
  273. #define MysonPHYID 0xd0000302
  274. /* 89-7-27 add, (begin) */
  275. #define MysonPHYID0 0x0302
  276. #define StatusRegister 18
  277. #define SPEED100 0x0400 // bit10
  278. #define FULLMODE 0x0800 // bit11
  279. /* 89-7-27 add, (end) */
  280. /* ------------------------------------------------------------------------- */
  281. /* Constants for Seeq 80225 PHY */
  282. /* ------------------------------------------------------------------------- */
  283. #define SeeqPHYID0 0x0016
  284. #define MIIRegister18 18
  285. #define SPD_DET_100 0x80
  286. #define DPLX_DET_FULL 0x40
  287. /* ------------------------------------------------------------------------- */
  288. /* Constants for Ahdoc 101 PHY */
  289. /* ------------------------------------------------------------------------- */
  290. #define AhdocPHYID0 0x0022
  291. #define DiagnosticReg 18
  292. #define DPLX_FULL 0x0800
  293. #define Speed_100 0x0400
  294. /* 89/6/13 add, */
  295. /* -------------------------------------------------------------------------- */
  296. /* Constants */
  297. /* -------------------------------------------------------------------------- */
  298. #define MarvellPHYID0 0x0141
  299. #define LevelOnePHYID0 0x0013
  300. #define MII1000BaseTControlReg 9
  301. #define MII1000BaseTStatusReg 10
  302. #define SpecificReg 17
  303. /* for 1000BaseT Control Register */
  304. #define PHYAbletoPerform1000FullDuplex 0x0200
  305. #define PHYAbletoPerform1000HalfDuplex 0x0100
  306. #define PHY1000AbilityMask 0x300
  307. // for phy specific status register, marvell phy.
  308. #define SpeedMask 0x0c000
  309. #define Speed_1000M 0x08000
  310. #define Speed_100M 0x4000
  311. #define Speed_10M 0
  312. #define Full_Duplex 0x2000
  313. // 89/12/29 add, for phy specific status register, levelone phy, (begin)
  314. #define LXT1000_100M 0x08000
  315. #define LXT1000_1000M 0x0c000
  316. #define LXT1000_Full 0x200
  317. // 89/12/29 add, for phy specific status register, levelone phy, (end)
  318. /* for 3-in-1 case, BMCRSR register */
  319. #define LinkIsUp2 0x00040000
  320. /* for PHY */
  321. #define LinkIsUp 0x0004
  322. struct netdev_private {
  323. /* Descriptor rings first for alignment. */
  324. struct fealnx_desc *rx_ring;
  325. struct fealnx_desc *tx_ring;
  326. dma_addr_t rx_ring_dma;
  327. dma_addr_t tx_ring_dma;
  328. spinlock_t lock;
  329. /* Media monitoring timer. */
  330. struct timer_list timer;
  331. /* Reset timer */
  332. struct timer_list reset_timer;
  333. int reset_timer_armed;
  334. unsigned long crvalue_sv;
  335. unsigned long imrvalue_sv;
  336. /* Frequently used values: keep some adjacent for cache effect. */
  337. int flags;
  338. struct pci_dev *pci_dev;
  339. unsigned long crvalue;
  340. unsigned long bcrvalue;
  341. unsigned long imrvalue;
  342. struct fealnx_desc *cur_rx;
  343. struct fealnx_desc *lack_rxbuf;
  344. int really_rx_count;
  345. struct fealnx_desc *cur_tx;
  346. struct fealnx_desc *cur_tx_copy;
  347. int really_tx_count;
  348. int free_tx_count;
  349. unsigned int rx_buf_sz; /* Based on MTU+slack. */
  350. /* These values are keep track of the transceiver/media in use. */
  351. unsigned int linkok;
  352. unsigned int line_speed;
  353. unsigned int duplexmode;
  354. unsigned int default_port:4; /* Last dev->if_port value. */
  355. unsigned int PHYType;
  356. /* MII transceiver section. */
  357. int mii_cnt; /* MII device addresses. */
  358. unsigned char phys[2]; /* MII device addresses. */
  359. struct mii_if_info mii;
  360. void __iomem *mem;
  361. };
  362. static int mdio_read(struct net_device *dev, int phy_id, int location);
  363. static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
  364. static int netdev_open(struct net_device *dev);
  365. static void getlinktype(struct net_device *dev);
  366. static void getlinkstatus(struct net_device *dev);
  367. static void netdev_timer(struct timer_list *t);
  368. static void reset_timer(struct timer_list *t);
  369. static void fealnx_tx_timeout(struct net_device *dev);
  370. static void init_ring(struct net_device *dev);
  371. static netdev_tx_t start_tx(struct sk_buff *skb, struct net_device *dev);
  372. static irqreturn_t intr_handler(int irq, void *dev_instance);
  373. static int netdev_rx(struct net_device *dev);
  374. static void set_rx_mode(struct net_device *dev);
  375. static void __set_rx_mode(struct net_device *dev);
  376. static struct net_device_stats *get_stats(struct net_device *dev);
  377. static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  378. static const struct ethtool_ops netdev_ethtool_ops;
  379. static int netdev_close(struct net_device *dev);
  380. static void reset_rx_descriptors(struct net_device *dev);
  381. static void reset_tx_descriptors(struct net_device *dev);
  382. static void stop_nic_rx(void __iomem *ioaddr, long crvalue)
  383. {
  384. int delay = 0x1000;
  385. iowrite32(crvalue & ~(CR_W_RXEN), ioaddr + TCRRCR);
  386. while (--delay) {
  387. if ( (ioread32(ioaddr + TCRRCR) & CR_R_RXSTOP) == CR_R_RXSTOP)
  388. break;
  389. }
  390. }
  391. static void stop_nic_rxtx(void __iomem *ioaddr, long crvalue)
  392. {
  393. int delay = 0x1000;
  394. iowrite32(crvalue & ~(CR_W_RXEN+CR_W_TXEN), ioaddr + TCRRCR);
  395. while (--delay) {
  396. if ( (ioread32(ioaddr + TCRRCR) & (CR_R_RXSTOP+CR_R_TXSTOP))
  397. == (CR_R_RXSTOP+CR_R_TXSTOP) )
  398. break;
  399. }
  400. }
  401. static const struct net_device_ops netdev_ops = {
  402. .ndo_open = netdev_open,
  403. .ndo_stop = netdev_close,
  404. .ndo_start_xmit = start_tx,
  405. .ndo_get_stats = get_stats,
  406. .ndo_set_rx_mode = set_rx_mode,
  407. .ndo_do_ioctl = mii_ioctl,
  408. .ndo_tx_timeout = fealnx_tx_timeout,
  409. .ndo_set_mac_address = eth_mac_addr,
  410. .ndo_validate_addr = eth_validate_addr,
  411. };
  412. static int fealnx_init_one(struct pci_dev *pdev,
  413. const struct pci_device_id *ent)
  414. {
  415. struct netdev_private *np;
  416. int i, option, err, irq;
  417. static int card_idx = -1;
  418. char boardname[12];
  419. void __iomem *ioaddr;
  420. unsigned long len;
  421. unsigned int chip_id = ent->driver_data;
  422. struct net_device *dev;
  423. void *ring_space;
  424. dma_addr_t ring_dma;
  425. #ifdef USE_IO_OPS
  426. int bar = 0;
  427. #else
  428. int bar = 1;
  429. #endif
  430. /* when built into the kernel, we only print version if device is found */
  431. #ifndef MODULE
  432. static int printed_version;
  433. if (!printed_version++)
  434. printk(version);
  435. #endif
  436. card_idx++;
  437. sprintf(boardname, "fealnx%d", card_idx);
  438. option = card_idx < MAX_UNITS ? options[card_idx] : 0;
  439. i = pci_enable_device(pdev);
  440. if (i) return i;
  441. pci_set_master(pdev);
  442. len = pci_resource_len(pdev, bar);
  443. if (len < MIN_REGION_SIZE) {
  444. dev_err(&pdev->dev,
  445. "region size %ld too small, aborting\n", len);
  446. return -ENODEV;
  447. }
  448. i = pci_request_regions(pdev, boardname);
  449. if (i)
  450. return i;
  451. irq = pdev->irq;
  452. ioaddr = pci_iomap(pdev, bar, len);
  453. if (!ioaddr) {
  454. err = -ENOMEM;
  455. goto err_out_res;
  456. }
  457. dev = alloc_etherdev(sizeof(struct netdev_private));
  458. if (!dev) {
  459. err = -ENOMEM;
  460. goto err_out_unmap;
  461. }
  462. SET_NETDEV_DEV(dev, &pdev->dev);
  463. /* read ethernet id */
  464. for (i = 0; i < 6; ++i)
  465. dev->dev_addr[i] = ioread8(ioaddr + PAR0 + i);
  466. /* Reset the chip to erase previous misconfiguration. */
  467. iowrite32(0x00000001, ioaddr + BCR);
  468. /* Make certain the descriptor lists are aligned. */
  469. np = netdev_priv(dev);
  470. np->mem = ioaddr;
  471. spin_lock_init(&np->lock);
  472. np->pci_dev = pdev;
  473. np->flags = skel_netdrv_tbl[chip_id].flags;
  474. pci_set_drvdata(pdev, dev);
  475. np->mii.dev = dev;
  476. np->mii.mdio_read = mdio_read;
  477. np->mii.mdio_write = mdio_write;
  478. np->mii.phy_id_mask = 0x1f;
  479. np->mii.reg_num_mask = 0x1f;
  480. ring_space = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
  481. if (!ring_space) {
  482. err = -ENOMEM;
  483. goto err_out_free_dev;
  484. }
  485. np->rx_ring = ring_space;
  486. np->rx_ring_dma = ring_dma;
  487. ring_space = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
  488. if (!ring_space) {
  489. err = -ENOMEM;
  490. goto err_out_free_rx;
  491. }
  492. np->tx_ring = ring_space;
  493. np->tx_ring_dma = ring_dma;
  494. /* find the connected MII xcvrs */
  495. if (np->flags == HAS_MII_XCVR) {
  496. int phy, phy_idx = 0;
  497. for (phy = 1; phy < 32 && phy_idx < ARRAY_SIZE(np->phys);
  498. phy++) {
  499. int mii_status = mdio_read(dev, phy, 1);
  500. if (mii_status != 0xffff && mii_status != 0x0000) {
  501. np->phys[phy_idx++] = phy;
  502. dev_info(&pdev->dev,
  503. "MII PHY found at address %d, status "
  504. "0x%4.4x.\n", phy, mii_status);
  505. /* get phy type */
  506. {
  507. unsigned int data;
  508. data = mdio_read(dev, np->phys[0], 2);
  509. if (data == SeeqPHYID0)
  510. np->PHYType = SeeqPHY;
  511. else if (data == AhdocPHYID0)
  512. np->PHYType = AhdocPHY;
  513. else if (data == MarvellPHYID0)
  514. np->PHYType = MarvellPHY;
  515. else if (data == MysonPHYID0)
  516. np->PHYType = Myson981;
  517. else if (data == LevelOnePHYID0)
  518. np->PHYType = LevelOnePHY;
  519. else
  520. np->PHYType = OtherPHY;
  521. }
  522. }
  523. }
  524. np->mii_cnt = phy_idx;
  525. if (phy_idx == 0)
  526. dev_warn(&pdev->dev,
  527. "MII PHY not found -- this device may "
  528. "not operate correctly.\n");
  529. } else {
  530. np->phys[0] = 32;
  531. /* 89/6/23 add, (begin) */
  532. /* get phy type */
  533. if (ioread32(ioaddr + PHYIDENTIFIER) == MysonPHYID)
  534. np->PHYType = MysonPHY;
  535. else
  536. np->PHYType = OtherPHY;
  537. }
  538. np->mii.phy_id = np->phys[0];
  539. if (dev->mem_start)
  540. option = dev->mem_start;
  541. /* The lower four bits are the media type. */
  542. if (option > 0) {
  543. if (option & 0x200)
  544. np->mii.full_duplex = 1;
  545. np->default_port = option & 15;
  546. }
  547. if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
  548. np->mii.full_duplex = full_duplex[card_idx];
  549. if (np->mii.full_duplex) {
  550. dev_info(&pdev->dev, "Media type forced to Full Duplex.\n");
  551. /* 89/6/13 add, (begin) */
  552. // if (np->PHYType==MarvellPHY)
  553. if ((np->PHYType == MarvellPHY) || (np->PHYType == LevelOnePHY)) {
  554. unsigned int data;
  555. data = mdio_read(dev, np->phys[0], 9);
  556. data = (data & 0xfcff) | 0x0200;
  557. mdio_write(dev, np->phys[0], 9, data);
  558. }
  559. /* 89/6/13 add, (end) */
  560. if (np->flags == HAS_MII_XCVR)
  561. mdio_write(dev, np->phys[0], MII_ADVERTISE, ADVERTISE_FULL);
  562. else
  563. iowrite32(ADVERTISE_FULL, ioaddr + ANARANLPAR);
  564. np->mii.force_media = 1;
  565. }
  566. dev->netdev_ops = &netdev_ops;
  567. dev->ethtool_ops = &netdev_ethtool_ops;
  568. dev->watchdog_timeo = TX_TIMEOUT;
  569. err = register_netdev(dev);
  570. if (err)
  571. goto err_out_free_tx;
  572. printk(KERN_INFO "%s: %s at %p, %pM, IRQ %d.\n",
  573. dev->name, skel_netdrv_tbl[chip_id].chip_name, ioaddr,
  574. dev->dev_addr, irq);
  575. return 0;
  576. err_out_free_tx:
  577. pci_free_consistent(pdev, TX_TOTAL_SIZE, np->tx_ring, np->tx_ring_dma);
  578. err_out_free_rx:
  579. pci_free_consistent(pdev, RX_TOTAL_SIZE, np->rx_ring, np->rx_ring_dma);
  580. err_out_free_dev:
  581. free_netdev(dev);
  582. err_out_unmap:
  583. pci_iounmap(pdev, ioaddr);
  584. err_out_res:
  585. pci_release_regions(pdev);
  586. return err;
  587. }
  588. static void fealnx_remove_one(struct pci_dev *pdev)
  589. {
  590. struct net_device *dev = pci_get_drvdata(pdev);
  591. if (dev) {
  592. struct netdev_private *np = netdev_priv(dev);
  593. pci_free_consistent(pdev, TX_TOTAL_SIZE, np->tx_ring,
  594. np->tx_ring_dma);
  595. pci_free_consistent(pdev, RX_TOTAL_SIZE, np->rx_ring,
  596. np->rx_ring_dma);
  597. unregister_netdev(dev);
  598. pci_iounmap(pdev, np->mem);
  599. free_netdev(dev);
  600. pci_release_regions(pdev);
  601. } else
  602. printk(KERN_ERR "fealnx: remove for unknown device\n");
  603. }
  604. static ulong m80x_send_cmd_to_phy(void __iomem *miiport, int opcode, int phyad, int regad)
  605. {
  606. ulong miir;
  607. int i;
  608. unsigned int mask, data;
  609. /* enable MII output */
  610. miir = (ulong) ioread32(miiport);
  611. miir &= 0xfffffff0;
  612. miir |= MASK_MIIR_MII_WRITE + MASK_MIIR_MII_MDO;
  613. /* send 32 1's preamble */
  614. for (i = 0; i < 32; i++) {
  615. /* low MDC; MDO is already high (miir) */
  616. miir &= ~MASK_MIIR_MII_MDC;
  617. iowrite32(miir, miiport);
  618. /* high MDC */
  619. miir |= MASK_MIIR_MII_MDC;
  620. iowrite32(miir, miiport);
  621. }
  622. /* calculate ST+OP+PHYAD+REGAD+TA */
  623. data = opcode | (phyad << 7) | (regad << 2);
  624. /* sent out */
  625. mask = 0x8000;
  626. while (mask) {
  627. /* low MDC, prepare MDO */
  628. miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
  629. if (mask & data)
  630. miir |= MASK_MIIR_MII_MDO;
  631. iowrite32(miir, miiport);
  632. /* high MDC */
  633. miir |= MASK_MIIR_MII_MDC;
  634. iowrite32(miir, miiport);
  635. udelay(30);
  636. /* next */
  637. mask >>= 1;
  638. if (mask == 0x2 && opcode == OP_READ)
  639. miir &= ~MASK_MIIR_MII_WRITE;
  640. }
  641. return miir;
  642. }
  643. static int mdio_read(struct net_device *dev, int phyad, int regad)
  644. {
  645. struct netdev_private *np = netdev_priv(dev);
  646. void __iomem *miiport = np->mem + MANAGEMENT;
  647. ulong miir;
  648. unsigned int mask, data;
  649. miir = m80x_send_cmd_to_phy(miiport, OP_READ, phyad, regad);
  650. /* read data */
  651. mask = 0x8000;
  652. data = 0;
  653. while (mask) {
  654. /* low MDC */
  655. miir &= ~MASK_MIIR_MII_MDC;
  656. iowrite32(miir, miiport);
  657. /* read MDI */
  658. miir = ioread32(miiport);
  659. if (miir & MASK_MIIR_MII_MDI)
  660. data |= mask;
  661. /* high MDC, and wait */
  662. miir |= MASK_MIIR_MII_MDC;
  663. iowrite32(miir, miiport);
  664. udelay(30);
  665. /* next */
  666. mask >>= 1;
  667. }
  668. /* low MDC */
  669. miir &= ~MASK_MIIR_MII_MDC;
  670. iowrite32(miir, miiport);
  671. return data & 0xffff;
  672. }
  673. static void mdio_write(struct net_device *dev, int phyad, int regad, int data)
  674. {
  675. struct netdev_private *np = netdev_priv(dev);
  676. void __iomem *miiport = np->mem + MANAGEMENT;
  677. ulong miir;
  678. unsigned int mask;
  679. miir = m80x_send_cmd_to_phy(miiport, OP_WRITE, phyad, regad);
  680. /* write data */
  681. mask = 0x8000;
  682. while (mask) {
  683. /* low MDC, prepare MDO */
  684. miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
  685. if (mask & data)
  686. miir |= MASK_MIIR_MII_MDO;
  687. iowrite32(miir, miiport);
  688. /* high MDC */
  689. miir |= MASK_MIIR_MII_MDC;
  690. iowrite32(miir, miiport);
  691. /* next */
  692. mask >>= 1;
  693. }
  694. /* low MDC */
  695. miir &= ~MASK_MIIR_MII_MDC;
  696. iowrite32(miir, miiport);
  697. }
  698. static int netdev_open(struct net_device *dev)
  699. {
  700. struct netdev_private *np = netdev_priv(dev);
  701. void __iomem *ioaddr = np->mem;
  702. const int irq = np->pci_dev->irq;
  703. int rc, i;
  704. iowrite32(0x00000001, ioaddr + BCR); /* Reset */
  705. rc = request_irq(irq, intr_handler, IRQF_SHARED, dev->name, dev);
  706. if (rc)
  707. return -EAGAIN;
  708. for (i = 0; i < 3; i++)
  709. iowrite16(((unsigned short*)dev->dev_addr)[i],
  710. ioaddr + PAR0 + i*2);
  711. init_ring(dev);
  712. iowrite32(np->rx_ring_dma, ioaddr + RXLBA);
  713. iowrite32(np->tx_ring_dma, ioaddr + TXLBA);
  714. /* Initialize other registers. */
  715. /* Configure the PCI bus bursts and FIFO thresholds.
  716. 486: Set 8 longword burst.
  717. 586: no burst limit.
  718. Burst length 5:3
  719. 0 0 0 1
  720. 0 0 1 4
  721. 0 1 0 8
  722. 0 1 1 16
  723. 1 0 0 32
  724. 1 0 1 64
  725. 1 1 0 128
  726. 1 1 1 256
  727. Wait the specified 50 PCI cycles after a reset by initializing
  728. Tx and Rx queues and the address filter list.
  729. FIXME (Ueimor): optimistic for alpha + posted writes ? */
  730. np->bcrvalue = 0x10; /* little-endian, 8 burst length */
  731. #ifdef __BIG_ENDIAN
  732. np->bcrvalue |= 0x04; /* big-endian */
  733. #endif
  734. #if defined(__i386__) && !defined(MODULE)
  735. if (boot_cpu_data.x86 <= 4)
  736. np->crvalue = 0xa00;
  737. else
  738. #endif
  739. np->crvalue = 0xe00; /* rx 128 burst length */
  740. // 89/12/29 add,
  741. // 90/1/16 modify,
  742. // np->imrvalue=FBE|TUNF|CNTOVF|RBU|TI|RI;
  743. np->imrvalue = TUNF | CNTOVF | RBU | TI | RI;
  744. if (np->pci_dev->device == 0x891) {
  745. np->bcrvalue |= 0x200; /* set PROG bit */
  746. np->crvalue |= CR_W_ENH; /* set enhanced bit */
  747. np->imrvalue |= ETI;
  748. }
  749. iowrite32(np->bcrvalue, ioaddr + BCR);
  750. if (dev->if_port == 0)
  751. dev->if_port = np->default_port;
  752. iowrite32(0, ioaddr + RXPDR);
  753. // 89/9/1 modify,
  754. // np->crvalue = 0x00e40001; /* tx store and forward, tx/rx enable */
  755. np->crvalue |= 0x00e40001; /* tx store and forward, tx/rx enable */
  756. np->mii.full_duplex = np->mii.force_media;
  757. getlinkstatus(dev);
  758. if (np->linkok)
  759. getlinktype(dev);
  760. __set_rx_mode(dev);
  761. netif_start_queue(dev);
  762. /* Clear and Enable interrupts by setting the interrupt mask. */
  763. iowrite32(FBE | TUNF | CNTOVF | RBU | TI | RI, ioaddr + ISR);
  764. iowrite32(np->imrvalue, ioaddr + IMR);
  765. if (debug)
  766. printk(KERN_DEBUG "%s: Done netdev_open().\n", dev->name);
  767. /* Set the timer to check for link beat. */
  768. timer_setup(&np->timer, netdev_timer, 0);
  769. np->timer.expires = RUN_AT(3 * HZ);
  770. /* timer handler */
  771. add_timer(&np->timer);
  772. timer_setup(&np->reset_timer, reset_timer, 0);
  773. np->reset_timer_armed = 0;
  774. return rc;
  775. }
  776. static void getlinkstatus(struct net_device *dev)
  777. /* function: Routine will read MII Status Register to get link status. */
  778. /* input : dev... pointer to the adapter block. */
  779. /* output : none. */
  780. {
  781. struct netdev_private *np = netdev_priv(dev);
  782. unsigned int i, DelayTime = 0x1000;
  783. np->linkok = 0;
  784. if (np->PHYType == MysonPHY) {
  785. for (i = 0; i < DelayTime; ++i) {
  786. if (ioread32(np->mem + BMCRSR) & LinkIsUp2) {
  787. np->linkok = 1;
  788. return;
  789. }
  790. udelay(100);
  791. }
  792. } else {
  793. for (i = 0; i < DelayTime; ++i) {
  794. if (mdio_read(dev, np->phys[0], MII_BMSR) & BMSR_LSTATUS) {
  795. np->linkok = 1;
  796. return;
  797. }
  798. udelay(100);
  799. }
  800. }
  801. }
  802. static void getlinktype(struct net_device *dev)
  803. {
  804. struct netdev_private *np = netdev_priv(dev);
  805. if (np->PHYType == MysonPHY) { /* 3-in-1 case */
  806. if (ioread32(np->mem + TCRRCR) & CR_R_FD)
  807. np->duplexmode = 2; /* full duplex */
  808. else
  809. np->duplexmode = 1; /* half duplex */
  810. if (ioread32(np->mem + TCRRCR) & CR_R_PS10)
  811. np->line_speed = 1; /* 10M */
  812. else
  813. np->line_speed = 2; /* 100M */
  814. } else {
  815. if (np->PHYType == SeeqPHY) { /* this PHY is SEEQ 80225 */
  816. unsigned int data;
  817. data = mdio_read(dev, np->phys[0], MIIRegister18);
  818. if (data & SPD_DET_100)
  819. np->line_speed = 2; /* 100M */
  820. else
  821. np->line_speed = 1; /* 10M */
  822. if (data & DPLX_DET_FULL)
  823. np->duplexmode = 2; /* full duplex mode */
  824. else
  825. np->duplexmode = 1; /* half duplex mode */
  826. } else if (np->PHYType == AhdocPHY) {
  827. unsigned int data;
  828. data = mdio_read(dev, np->phys[0], DiagnosticReg);
  829. if (data & Speed_100)
  830. np->line_speed = 2; /* 100M */
  831. else
  832. np->line_speed = 1; /* 10M */
  833. if (data & DPLX_FULL)
  834. np->duplexmode = 2; /* full duplex mode */
  835. else
  836. np->duplexmode = 1; /* half duplex mode */
  837. }
  838. /* 89/6/13 add, (begin) */
  839. else if (np->PHYType == MarvellPHY) {
  840. unsigned int data;
  841. data = mdio_read(dev, np->phys[0], SpecificReg);
  842. if (data & Full_Duplex)
  843. np->duplexmode = 2; /* full duplex mode */
  844. else
  845. np->duplexmode = 1; /* half duplex mode */
  846. data &= SpeedMask;
  847. if (data == Speed_1000M)
  848. np->line_speed = 3; /* 1000M */
  849. else if (data == Speed_100M)
  850. np->line_speed = 2; /* 100M */
  851. else
  852. np->line_speed = 1; /* 10M */
  853. }
  854. /* 89/6/13 add, (end) */
  855. /* 89/7/27 add, (begin) */
  856. else if (np->PHYType == Myson981) {
  857. unsigned int data;
  858. data = mdio_read(dev, np->phys[0], StatusRegister);
  859. if (data & SPEED100)
  860. np->line_speed = 2;
  861. else
  862. np->line_speed = 1;
  863. if (data & FULLMODE)
  864. np->duplexmode = 2;
  865. else
  866. np->duplexmode = 1;
  867. }
  868. /* 89/7/27 add, (end) */
  869. /* 89/12/29 add */
  870. else if (np->PHYType == LevelOnePHY) {
  871. unsigned int data;
  872. data = mdio_read(dev, np->phys[0], SpecificReg);
  873. if (data & LXT1000_Full)
  874. np->duplexmode = 2; /* full duplex mode */
  875. else
  876. np->duplexmode = 1; /* half duplex mode */
  877. data &= SpeedMask;
  878. if (data == LXT1000_1000M)
  879. np->line_speed = 3; /* 1000M */
  880. else if (data == LXT1000_100M)
  881. np->line_speed = 2; /* 100M */
  882. else
  883. np->line_speed = 1; /* 10M */
  884. }
  885. np->crvalue &= (~CR_W_PS10) & (~CR_W_FD) & (~CR_W_PS1000);
  886. if (np->line_speed == 1)
  887. np->crvalue |= CR_W_PS10;
  888. else if (np->line_speed == 3)
  889. np->crvalue |= CR_W_PS1000;
  890. if (np->duplexmode == 2)
  891. np->crvalue |= CR_W_FD;
  892. }
  893. }
  894. /* Take lock before calling this */
  895. static void allocate_rx_buffers(struct net_device *dev)
  896. {
  897. struct netdev_private *np = netdev_priv(dev);
  898. /* allocate skb for rx buffers */
  899. while (np->really_rx_count != RX_RING_SIZE) {
  900. struct sk_buff *skb;
  901. skb = netdev_alloc_skb(dev, np->rx_buf_sz);
  902. if (skb == NULL)
  903. break; /* Better luck next round. */
  904. while (np->lack_rxbuf->skbuff)
  905. np->lack_rxbuf = np->lack_rxbuf->next_desc_logical;
  906. np->lack_rxbuf->skbuff = skb;
  907. np->lack_rxbuf->buffer = pci_map_single(np->pci_dev, skb->data,
  908. np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  909. np->lack_rxbuf->status = RXOWN;
  910. ++np->really_rx_count;
  911. }
  912. }
  913. static void netdev_timer(struct timer_list *t)
  914. {
  915. struct netdev_private *np = from_timer(np, t, timer);
  916. struct net_device *dev = np->mii.dev;
  917. void __iomem *ioaddr = np->mem;
  918. int old_crvalue = np->crvalue;
  919. unsigned int old_linkok = np->linkok;
  920. unsigned long flags;
  921. if (debug)
  922. printk(KERN_DEBUG "%s: Media selection timer tick, status %8.8x "
  923. "config %8.8x.\n", dev->name, ioread32(ioaddr + ISR),
  924. ioread32(ioaddr + TCRRCR));
  925. spin_lock_irqsave(&np->lock, flags);
  926. if (np->flags == HAS_MII_XCVR) {
  927. getlinkstatus(dev);
  928. if ((old_linkok == 0) && (np->linkok == 1)) { /* we need to detect the media type again */
  929. getlinktype(dev);
  930. if (np->crvalue != old_crvalue) {
  931. stop_nic_rxtx(ioaddr, np->crvalue);
  932. iowrite32(np->crvalue, ioaddr + TCRRCR);
  933. }
  934. }
  935. }
  936. allocate_rx_buffers(dev);
  937. spin_unlock_irqrestore(&np->lock, flags);
  938. np->timer.expires = RUN_AT(10 * HZ);
  939. add_timer(&np->timer);
  940. }
  941. /* Take lock before calling */
  942. /* Reset chip and disable rx, tx and interrupts */
  943. static void reset_and_disable_rxtx(struct net_device *dev)
  944. {
  945. struct netdev_private *np = netdev_priv(dev);
  946. void __iomem *ioaddr = np->mem;
  947. int delay=51;
  948. /* Reset the chip's Tx and Rx processes. */
  949. stop_nic_rxtx(ioaddr, 0);
  950. /* Disable interrupts by clearing the interrupt mask. */
  951. iowrite32(0, ioaddr + IMR);
  952. /* Reset the chip to erase previous misconfiguration. */
  953. iowrite32(0x00000001, ioaddr + BCR);
  954. /* Ueimor: wait for 50 PCI cycles (and flush posted writes btw).
  955. We surely wait too long (address+data phase). Who cares? */
  956. while (--delay) {
  957. ioread32(ioaddr + BCR);
  958. rmb();
  959. }
  960. }
  961. /* Take lock before calling */
  962. /* Restore chip after reset */
  963. static void enable_rxtx(struct net_device *dev)
  964. {
  965. struct netdev_private *np = netdev_priv(dev);
  966. void __iomem *ioaddr = np->mem;
  967. reset_rx_descriptors(dev);
  968. iowrite32(np->tx_ring_dma + ((char*)np->cur_tx - (char*)np->tx_ring),
  969. ioaddr + TXLBA);
  970. iowrite32(np->rx_ring_dma + ((char*)np->cur_rx - (char*)np->rx_ring),
  971. ioaddr + RXLBA);
  972. iowrite32(np->bcrvalue, ioaddr + BCR);
  973. iowrite32(0, ioaddr + RXPDR);
  974. __set_rx_mode(dev); /* changes np->crvalue, writes it into TCRRCR */
  975. /* Clear and Enable interrupts by setting the interrupt mask. */
  976. iowrite32(FBE | TUNF | CNTOVF | RBU | TI | RI, ioaddr + ISR);
  977. iowrite32(np->imrvalue, ioaddr + IMR);
  978. iowrite32(0, ioaddr + TXPDR);
  979. }
  980. static void reset_timer(struct timer_list *t)
  981. {
  982. struct netdev_private *np = from_timer(np, t, reset_timer);
  983. struct net_device *dev = np->mii.dev;
  984. unsigned long flags;
  985. printk(KERN_WARNING "%s: resetting tx and rx machinery\n", dev->name);
  986. spin_lock_irqsave(&np->lock, flags);
  987. np->crvalue = np->crvalue_sv;
  988. np->imrvalue = np->imrvalue_sv;
  989. reset_and_disable_rxtx(dev);
  990. /* works for me without this:
  991. reset_tx_descriptors(dev); */
  992. enable_rxtx(dev);
  993. netif_start_queue(dev); /* FIXME: or netif_wake_queue(dev); ? */
  994. np->reset_timer_armed = 0;
  995. spin_unlock_irqrestore(&np->lock, flags);
  996. }
  997. static void fealnx_tx_timeout(struct net_device *dev)
  998. {
  999. struct netdev_private *np = netdev_priv(dev);
  1000. void __iomem *ioaddr = np->mem;
  1001. unsigned long flags;
  1002. int i;
  1003. printk(KERN_WARNING
  1004. "%s: Transmit timed out, status %8.8x, resetting...\n",
  1005. dev->name, ioread32(ioaddr + ISR));
  1006. {
  1007. printk(KERN_DEBUG " Rx ring %p: ", np->rx_ring);
  1008. for (i = 0; i < RX_RING_SIZE; i++)
  1009. printk(KERN_CONT " %8.8x",
  1010. (unsigned int) np->rx_ring[i].status);
  1011. printk(KERN_CONT "\n");
  1012. printk(KERN_DEBUG " Tx ring %p: ", np->tx_ring);
  1013. for (i = 0; i < TX_RING_SIZE; i++)
  1014. printk(KERN_CONT " %4.4x", np->tx_ring[i].status);
  1015. printk(KERN_CONT "\n");
  1016. }
  1017. spin_lock_irqsave(&np->lock, flags);
  1018. reset_and_disable_rxtx(dev);
  1019. reset_tx_descriptors(dev);
  1020. enable_rxtx(dev);
  1021. spin_unlock_irqrestore(&np->lock, flags);
  1022. netif_trans_update(dev); /* prevent tx timeout */
  1023. dev->stats.tx_errors++;
  1024. netif_wake_queue(dev); /* or .._start_.. ?? */
  1025. }
  1026. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  1027. static void init_ring(struct net_device *dev)
  1028. {
  1029. struct netdev_private *np = netdev_priv(dev);
  1030. int i;
  1031. /* initialize rx variables */
  1032. np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
  1033. np->cur_rx = &np->rx_ring[0];
  1034. np->lack_rxbuf = np->rx_ring;
  1035. np->really_rx_count = 0;
  1036. /* initial rx descriptors. */
  1037. for (i = 0; i < RX_RING_SIZE; i++) {
  1038. np->rx_ring[i].status = 0;
  1039. np->rx_ring[i].control = np->rx_buf_sz << RBSShift;
  1040. np->rx_ring[i].next_desc = np->rx_ring_dma +
  1041. (i + 1)*sizeof(struct fealnx_desc);
  1042. np->rx_ring[i].next_desc_logical = &np->rx_ring[i + 1];
  1043. np->rx_ring[i].skbuff = NULL;
  1044. }
  1045. /* for the last rx descriptor */
  1046. np->rx_ring[i - 1].next_desc = np->rx_ring_dma;
  1047. np->rx_ring[i - 1].next_desc_logical = np->rx_ring;
  1048. /* allocate skb for rx buffers */
  1049. for (i = 0; i < RX_RING_SIZE; i++) {
  1050. struct sk_buff *skb = netdev_alloc_skb(dev, np->rx_buf_sz);
  1051. if (skb == NULL) {
  1052. np->lack_rxbuf = &np->rx_ring[i];
  1053. break;
  1054. }
  1055. ++np->really_rx_count;
  1056. np->rx_ring[i].skbuff = skb;
  1057. np->rx_ring[i].buffer = pci_map_single(np->pci_dev, skb->data,
  1058. np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1059. np->rx_ring[i].status = RXOWN;
  1060. np->rx_ring[i].control |= RXIC;
  1061. }
  1062. /* initialize tx variables */
  1063. np->cur_tx = &np->tx_ring[0];
  1064. np->cur_tx_copy = &np->tx_ring[0];
  1065. np->really_tx_count = 0;
  1066. np->free_tx_count = TX_RING_SIZE;
  1067. for (i = 0; i < TX_RING_SIZE; i++) {
  1068. np->tx_ring[i].status = 0;
  1069. /* do we need np->tx_ring[i].control = XXX; ?? */
  1070. np->tx_ring[i].next_desc = np->tx_ring_dma +
  1071. (i + 1)*sizeof(struct fealnx_desc);
  1072. np->tx_ring[i].next_desc_logical = &np->tx_ring[i + 1];
  1073. np->tx_ring[i].skbuff = NULL;
  1074. }
  1075. /* for the last tx descriptor */
  1076. np->tx_ring[i - 1].next_desc = np->tx_ring_dma;
  1077. np->tx_ring[i - 1].next_desc_logical = &np->tx_ring[0];
  1078. }
  1079. static netdev_tx_t start_tx(struct sk_buff *skb, struct net_device *dev)
  1080. {
  1081. struct netdev_private *np = netdev_priv(dev);
  1082. unsigned long flags;
  1083. spin_lock_irqsave(&np->lock, flags);
  1084. np->cur_tx_copy->skbuff = skb;
  1085. #define one_buffer
  1086. #define BPT 1022
  1087. #if defined(one_buffer)
  1088. np->cur_tx_copy->buffer = pci_map_single(np->pci_dev, skb->data,
  1089. skb->len, PCI_DMA_TODEVICE);
  1090. np->cur_tx_copy->control = TXIC | TXLD | TXFD | CRCEnable | PADEnable;
  1091. np->cur_tx_copy->control |= (skb->len << PKTSShift); /* pkt size */
  1092. np->cur_tx_copy->control |= (skb->len << TBSShift); /* buffer size */
  1093. // 89/12/29 add,
  1094. if (np->pci_dev->device == 0x891)
  1095. np->cur_tx_copy->control |= ETIControl | RetryTxLC;
  1096. np->cur_tx_copy->status = TXOWN;
  1097. np->cur_tx_copy = np->cur_tx_copy->next_desc_logical;
  1098. --np->free_tx_count;
  1099. #elif defined(two_buffer)
  1100. if (skb->len > BPT) {
  1101. struct fealnx_desc *next;
  1102. /* for the first descriptor */
  1103. np->cur_tx_copy->buffer = pci_map_single(np->pci_dev, skb->data,
  1104. BPT, PCI_DMA_TODEVICE);
  1105. np->cur_tx_copy->control = TXIC | TXFD | CRCEnable | PADEnable;
  1106. np->cur_tx_copy->control |= (skb->len << PKTSShift); /* pkt size */
  1107. np->cur_tx_copy->control |= (BPT << TBSShift); /* buffer size */
  1108. /* for the last descriptor */
  1109. next = np->cur_tx_copy->next_desc_logical;
  1110. next->skbuff = skb;
  1111. next->control = TXIC | TXLD | CRCEnable | PADEnable;
  1112. next->control |= (skb->len << PKTSShift); /* pkt size */
  1113. next->control |= ((skb->len - BPT) << TBSShift); /* buf size */
  1114. // 89/12/29 add,
  1115. if (np->pci_dev->device == 0x891)
  1116. np->cur_tx_copy->control |= ETIControl | RetryTxLC;
  1117. next->buffer = pci_map_single(ep->pci_dev, skb->data + BPT,
  1118. skb->len - BPT, PCI_DMA_TODEVICE);
  1119. next->status = TXOWN;
  1120. np->cur_tx_copy->status = TXOWN;
  1121. np->cur_tx_copy = next->next_desc_logical;
  1122. np->free_tx_count -= 2;
  1123. } else {
  1124. np->cur_tx_copy->buffer = pci_map_single(np->pci_dev, skb->data,
  1125. skb->len, PCI_DMA_TODEVICE);
  1126. np->cur_tx_copy->control = TXIC | TXLD | TXFD | CRCEnable | PADEnable;
  1127. np->cur_tx_copy->control |= (skb->len << PKTSShift); /* pkt size */
  1128. np->cur_tx_copy->control |= (skb->len << TBSShift); /* buffer size */
  1129. // 89/12/29 add,
  1130. if (np->pci_dev->device == 0x891)
  1131. np->cur_tx_copy->control |= ETIControl | RetryTxLC;
  1132. np->cur_tx_copy->status = TXOWN;
  1133. np->cur_tx_copy = np->cur_tx_copy->next_desc_logical;
  1134. --np->free_tx_count;
  1135. }
  1136. #endif
  1137. if (np->free_tx_count < 2)
  1138. netif_stop_queue(dev);
  1139. ++np->really_tx_count;
  1140. iowrite32(0, np->mem + TXPDR);
  1141. spin_unlock_irqrestore(&np->lock, flags);
  1142. return NETDEV_TX_OK;
  1143. }
  1144. /* Take lock before calling */
  1145. /* Chip probably hosed tx ring. Clean up. */
  1146. static void reset_tx_descriptors(struct net_device *dev)
  1147. {
  1148. struct netdev_private *np = netdev_priv(dev);
  1149. struct fealnx_desc *cur;
  1150. int i;
  1151. /* initialize tx variables */
  1152. np->cur_tx = &np->tx_ring[0];
  1153. np->cur_tx_copy = &np->tx_ring[0];
  1154. np->really_tx_count = 0;
  1155. np->free_tx_count = TX_RING_SIZE;
  1156. for (i = 0; i < TX_RING_SIZE; i++) {
  1157. cur = &np->tx_ring[i];
  1158. if (cur->skbuff) {
  1159. pci_unmap_single(np->pci_dev, cur->buffer,
  1160. cur->skbuff->len, PCI_DMA_TODEVICE);
  1161. dev_kfree_skb_any(cur->skbuff);
  1162. cur->skbuff = NULL;
  1163. }
  1164. cur->status = 0;
  1165. cur->control = 0; /* needed? */
  1166. /* probably not needed. We do it for purely paranoid reasons */
  1167. cur->next_desc = np->tx_ring_dma +
  1168. (i + 1)*sizeof(struct fealnx_desc);
  1169. cur->next_desc_logical = &np->tx_ring[i + 1];
  1170. }
  1171. /* for the last tx descriptor */
  1172. np->tx_ring[TX_RING_SIZE - 1].next_desc = np->tx_ring_dma;
  1173. np->tx_ring[TX_RING_SIZE - 1].next_desc_logical = &np->tx_ring[0];
  1174. }
  1175. /* Take lock and stop rx before calling this */
  1176. static void reset_rx_descriptors(struct net_device *dev)
  1177. {
  1178. struct netdev_private *np = netdev_priv(dev);
  1179. struct fealnx_desc *cur = np->cur_rx;
  1180. int i;
  1181. allocate_rx_buffers(dev);
  1182. for (i = 0; i < RX_RING_SIZE; i++) {
  1183. if (cur->skbuff)
  1184. cur->status = RXOWN;
  1185. cur = cur->next_desc_logical;
  1186. }
  1187. iowrite32(np->rx_ring_dma + ((char*)np->cur_rx - (char*)np->rx_ring),
  1188. np->mem + RXLBA);
  1189. }
  1190. /* The interrupt handler does all of the Rx thread work and cleans up
  1191. after the Tx thread. */
  1192. static irqreturn_t intr_handler(int irq, void *dev_instance)
  1193. {
  1194. struct net_device *dev = (struct net_device *) dev_instance;
  1195. struct netdev_private *np = netdev_priv(dev);
  1196. void __iomem *ioaddr = np->mem;
  1197. long boguscnt = max_interrupt_work;
  1198. unsigned int num_tx = 0;
  1199. int handled = 0;
  1200. spin_lock(&np->lock);
  1201. iowrite32(0, ioaddr + IMR);
  1202. do {
  1203. u32 intr_status = ioread32(ioaddr + ISR);
  1204. /* Acknowledge all of the current interrupt sources ASAP. */
  1205. iowrite32(intr_status, ioaddr + ISR);
  1206. if (debug)
  1207. printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n", dev->name,
  1208. intr_status);
  1209. if (!(intr_status & np->imrvalue))
  1210. break;
  1211. handled = 1;
  1212. // 90/1/16 delete,
  1213. //
  1214. // if (intr_status & FBE)
  1215. // { /* fatal error */
  1216. // stop_nic_tx(ioaddr, 0);
  1217. // stop_nic_rx(ioaddr, 0);
  1218. // break;
  1219. // };
  1220. if (intr_status & TUNF)
  1221. iowrite32(0, ioaddr + TXPDR);
  1222. if (intr_status & CNTOVF) {
  1223. /* missed pkts */
  1224. dev->stats.rx_missed_errors +=
  1225. ioread32(ioaddr + TALLY) & 0x7fff;
  1226. /* crc error */
  1227. dev->stats.rx_crc_errors +=
  1228. (ioread32(ioaddr + TALLY) & 0x7fff0000) >> 16;
  1229. }
  1230. if (intr_status & (RI | RBU)) {
  1231. if (intr_status & RI)
  1232. netdev_rx(dev);
  1233. else {
  1234. stop_nic_rx(ioaddr, np->crvalue);
  1235. reset_rx_descriptors(dev);
  1236. iowrite32(np->crvalue, ioaddr + TCRRCR);
  1237. }
  1238. }
  1239. while (np->really_tx_count) {
  1240. long tx_status = np->cur_tx->status;
  1241. long tx_control = np->cur_tx->control;
  1242. if (!(tx_control & TXLD)) { /* this pkt is combined by two tx descriptors */
  1243. struct fealnx_desc *next;
  1244. next = np->cur_tx->next_desc_logical;
  1245. tx_status = next->status;
  1246. tx_control = next->control;
  1247. }
  1248. if (tx_status & TXOWN)
  1249. break;
  1250. if (!(np->crvalue & CR_W_ENH)) {
  1251. if (tx_status & (CSL | LC | EC | UDF | HF)) {
  1252. dev->stats.tx_errors++;
  1253. if (tx_status & EC)
  1254. dev->stats.tx_aborted_errors++;
  1255. if (tx_status & CSL)
  1256. dev->stats.tx_carrier_errors++;
  1257. if (tx_status & LC)
  1258. dev->stats.tx_window_errors++;
  1259. if (tx_status & UDF)
  1260. dev->stats.tx_fifo_errors++;
  1261. if ((tx_status & HF) && np->mii.full_duplex == 0)
  1262. dev->stats.tx_heartbeat_errors++;
  1263. } else {
  1264. dev->stats.tx_bytes +=
  1265. ((tx_control & PKTSMask) >> PKTSShift);
  1266. dev->stats.collisions +=
  1267. ((tx_status & NCRMask) >> NCRShift);
  1268. dev->stats.tx_packets++;
  1269. }
  1270. } else {
  1271. dev->stats.tx_bytes +=
  1272. ((tx_control & PKTSMask) >> PKTSShift);
  1273. dev->stats.tx_packets++;
  1274. }
  1275. /* Free the original skb. */
  1276. pci_unmap_single(np->pci_dev, np->cur_tx->buffer,
  1277. np->cur_tx->skbuff->len, PCI_DMA_TODEVICE);
  1278. dev_kfree_skb_irq(np->cur_tx->skbuff);
  1279. np->cur_tx->skbuff = NULL;
  1280. --np->really_tx_count;
  1281. if (np->cur_tx->control & TXLD) {
  1282. np->cur_tx = np->cur_tx->next_desc_logical;
  1283. ++np->free_tx_count;
  1284. } else {
  1285. np->cur_tx = np->cur_tx->next_desc_logical;
  1286. np->cur_tx = np->cur_tx->next_desc_logical;
  1287. np->free_tx_count += 2;
  1288. }
  1289. num_tx++;
  1290. } /* end of for loop */
  1291. if (num_tx && np->free_tx_count >= 2)
  1292. netif_wake_queue(dev);
  1293. /* read transmit status for enhanced mode only */
  1294. if (np->crvalue & CR_W_ENH) {
  1295. long data;
  1296. data = ioread32(ioaddr + TSR);
  1297. dev->stats.tx_errors += (data & 0xff000000) >> 24;
  1298. dev->stats.tx_aborted_errors +=
  1299. (data & 0xff000000) >> 24;
  1300. dev->stats.tx_window_errors +=
  1301. (data & 0x00ff0000) >> 16;
  1302. dev->stats.collisions += (data & 0x0000ffff);
  1303. }
  1304. if (--boguscnt < 0) {
  1305. printk(KERN_WARNING "%s: Too much work at interrupt, "
  1306. "status=0x%4.4x.\n", dev->name, intr_status);
  1307. if (!np->reset_timer_armed) {
  1308. np->reset_timer_armed = 1;
  1309. np->reset_timer.expires = RUN_AT(HZ/2);
  1310. add_timer(&np->reset_timer);
  1311. stop_nic_rxtx(ioaddr, 0);
  1312. netif_stop_queue(dev);
  1313. /* or netif_tx_disable(dev); ?? */
  1314. /* Prevent other paths from enabling tx,rx,intrs */
  1315. np->crvalue_sv = np->crvalue;
  1316. np->imrvalue_sv = np->imrvalue;
  1317. np->crvalue &= ~(CR_W_TXEN | CR_W_RXEN); /* or simply = 0? */
  1318. np->imrvalue = 0;
  1319. }
  1320. break;
  1321. }
  1322. } while (1);
  1323. /* read the tally counters */
  1324. /* missed pkts */
  1325. dev->stats.rx_missed_errors += ioread32(ioaddr + TALLY) & 0x7fff;
  1326. /* crc error */
  1327. dev->stats.rx_crc_errors +=
  1328. (ioread32(ioaddr + TALLY) & 0x7fff0000) >> 16;
  1329. if (debug)
  1330. printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
  1331. dev->name, ioread32(ioaddr + ISR));
  1332. iowrite32(np->imrvalue, ioaddr + IMR);
  1333. spin_unlock(&np->lock);
  1334. return IRQ_RETVAL(handled);
  1335. }
  1336. /* This routine is logically part of the interrupt handler, but separated
  1337. for clarity and better register allocation. */
  1338. static int netdev_rx(struct net_device *dev)
  1339. {
  1340. struct netdev_private *np = netdev_priv(dev);
  1341. void __iomem *ioaddr = np->mem;
  1342. /* If EOP is set on the next entry, it's a new packet. Send it up. */
  1343. while (!(np->cur_rx->status & RXOWN) && np->cur_rx->skbuff) {
  1344. s32 rx_status = np->cur_rx->status;
  1345. if (np->really_rx_count == 0)
  1346. break;
  1347. if (debug)
  1348. printk(KERN_DEBUG " netdev_rx() status was %8.8x.\n", rx_status);
  1349. if ((!((rx_status & RXFSD) && (rx_status & RXLSD))) ||
  1350. (rx_status & ErrorSummary)) {
  1351. if (rx_status & ErrorSummary) { /* there was a fatal error */
  1352. if (debug)
  1353. printk(KERN_DEBUG
  1354. "%s: Receive error, Rx status %8.8x.\n",
  1355. dev->name, rx_status);
  1356. dev->stats.rx_errors++; /* end of a packet. */
  1357. if (rx_status & (LONGPKT | RUNTPKT))
  1358. dev->stats.rx_length_errors++;
  1359. if (rx_status & RXER)
  1360. dev->stats.rx_frame_errors++;
  1361. if (rx_status & CRC)
  1362. dev->stats.rx_crc_errors++;
  1363. } else {
  1364. int need_to_reset = 0;
  1365. int desno = 0;
  1366. if (rx_status & RXFSD) { /* this pkt is too long, over one rx buffer */
  1367. struct fealnx_desc *cur;
  1368. /* check this packet is received completely? */
  1369. cur = np->cur_rx;
  1370. while (desno <= np->really_rx_count) {
  1371. ++desno;
  1372. if ((!(cur->status & RXOWN)) &&
  1373. (cur->status & RXLSD))
  1374. break;
  1375. /* goto next rx descriptor */
  1376. cur = cur->next_desc_logical;
  1377. }
  1378. if (desno > np->really_rx_count)
  1379. need_to_reset = 1;
  1380. } else /* RXLSD did not find, something error */
  1381. need_to_reset = 1;
  1382. if (need_to_reset == 0) {
  1383. int i;
  1384. dev->stats.rx_length_errors++;
  1385. /* free all rx descriptors related this long pkt */
  1386. for (i = 0; i < desno; ++i) {
  1387. if (!np->cur_rx->skbuff) {
  1388. printk(KERN_DEBUG
  1389. "%s: I'm scared\n", dev->name);
  1390. break;
  1391. }
  1392. np->cur_rx->status = RXOWN;
  1393. np->cur_rx = np->cur_rx->next_desc_logical;
  1394. }
  1395. continue;
  1396. } else { /* rx error, need to reset this chip */
  1397. stop_nic_rx(ioaddr, np->crvalue);
  1398. reset_rx_descriptors(dev);
  1399. iowrite32(np->crvalue, ioaddr + TCRRCR);
  1400. }
  1401. break; /* exit the while loop */
  1402. }
  1403. } else { /* this received pkt is ok */
  1404. struct sk_buff *skb;
  1405. /* Omit the four octet CRC from the length. */
  1406. short pkt_len = ((rx_status & FLNGMASK) >> FLNGShift) - 4;
  1407. #ifndef final_version
  1408. if (debug)
  1409. printk(KERN_DEBUG " netdev_rx() normal Rx pkt length %d"
  1410. " status %x.\n", pkt_len, rx_status);
  1411. #endif
  1412. /* Check if the packet is long enough to accept without copying
  1413. to a minimally-sized skbuff. */
  1414. if (pkt_len < rx_copybreak &&
  1415. (skb = netdev_alloc_skb(dev, pkt_len + 2)) != NULL) {
  1416. skb_reserve(skb, 2); /* 16 byte align the IP header */
  1417. pci_dma_sync_single_for_cpu(np->pci_dev,
  1418. np->cur_rx->buffer,
  1419. np->rx_buf_sz,
  1420. PCI_DMA_FROMDEVICE);
  1421. /* Call copy + cksum if available. */
  1422. #if ! defined(__alpha__)
  1423. skb_copy_to_linear_data(skb,
  1424. np->cur_rx->skbuff->data, pkt_len);
  1425. skb_put(skb, pkt_len);
  1426. #else
  1427. skb_put_data(skb, np->cur_rx->skbuff->data,
  1428. pkt_len);
  1429. #endif
  1430. pci_dma_sync_single_for_device(np->pci_dev,
  1431. np->cur_rx->buffer,
  1432. np->rx_buf_sz,
  1433. PCI_DMA_FROMDEVICE);
  1434. } else {
  1435. pci_unmap_single(np->pci_dev,
  1436. np->cur_rx->buffer,
  1437. np->rx_buf_sz,
  1438. PCI_DMA_FROMDEVICE);
  1439. skb_put(skb = np->cur_rx->skbuff, pkt_len);
  1440. np->cur_rx->skbuff = NULL;
  1441. --np->really_rx_count;
  1442. }
  1443. skb->protocol = eth_type_trans(skb, dev);
  1444. netif_rx(skb);
  1445. dev->stats.rx_packets++;
  1446. dev->stats.rx_bytes += pkt_len;
  1447. }
  1448. np->cur_rx = np->cur_rx->next_desc_logical;
  1449. } /* end of while loop */
  1450. /* allocate skb for rx buffers */
  1451. allocate_rx_buffers(dev);
  1452. return 0;
  1453. }
  1454. static struct net_device_stats *get_stats(struct net_device *dev)
  1455. {
  1456. struct netdev_private *np = netdev_priv(dev);
  1457. void __iomem *ioaddr = np->mem;
  1458. /* The chip only need report frame silently dropped. */
  1459. if (netif_running(dev)) {
  1460. dev->stats.rx_missed_errors +=
  1461. ioread32(ioaddr + TALLY) & 0x7fff;
  1462. dev->stats.rx_crc_errors +=
  1463. (ioread32(ioaddr + TALLY) & 0x7fff0000) >> 16;
  1464. }
  1465. return &dev->stats;
  1466. }
  1467. /* for dev->set_multicast_list */
  1468. static void set_rx_mode(struct net_device *dev)
  1469. {
  1470. spinlock_t *lp = &((struct netdev_private *)netdev_priv(dev))->lock;
  1471. unsigned long flags;
  1472. spin_lock_irqsave(lp, flags);
  1473. __set_rx_mode(dev);
  1474. spin_unlock_irqrestore(lp, flags);
  1475. }
  1476. /* Take lock before calling */
  1477. static void __set_rx_mode(struct net_device *dev)
  1478. {
  1479. struct netdev_private *np = netdev_priv(dev);
  1480. void __iomem *ioaddr = np->mem;
  1481. u32 mc_filter[2]; /* Multicast hash filter */
  1482. u32 rx_mode;
  1483. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1484. memset(mc_filter, 0xff, sizeof(mc_filter));
  1485. rx_mode = CR_W_PROM | CR_W_AB | CR_W_AM;
  1486. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  1487. (dev->flags & IFF_ALLMULTI)) {
  1488. /* Too many to match, or accept all multicasts. */
  1489. memset(mc_filter, 0xff, sizeof(mc_filter));
  1490. rx_mode = CR_W_AB | CR_W_AM;
  1491. } else {
  1492. struct netdev_hw_addr *ha;
  1493. memset(mc_filter, 0, sizeof(mc_filter));
  1494. netdev_for_each_mc_addr(ha, dev) {
  1495. unsigned int bit;
  1496. bit = (ether_crc(ETH_ALEN, ha->addr) >> 26) ^ 0x3F;
  1497. mc_filter[bit >> 5] |= (1 << bit);
  1498. }
  1499. rx_mode = CR_W_AB | CR_W_AM;
  1500. }
  1501. stop_nic_rxtx(ioaddr, np->crvalue);
  1502. iowrite32(mc_filter[0], ioaddr + MAR0);
  1503. iowrite32(mc_filter[1], ioaddr + MAR1);
  1504. np->crvalue &= ~CR_W_RXMODEMASK;
  1505. np->crvalue |= rx_mode;
  1506. iowrite32(np->crvalue, ioaddr + TCRRCR);
  1507. }
  1508. static void netdev_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1509. {
  1510. struct netdev_private *np = netdev_priv(dev);
  1511. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1512. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1513. strlcpy(info->bus_info, pci_name(np->pci_dev), sizeof(info->bus_info));
  1514. }
  1515. static int netdev_get_link_ksettings(struct net_device *dev,
  1516. struct ethtool_link_ksettings *cmd)
  1517. {
  1518. struct netdev_private *np = netdev_priv(dev);
  1519. spin_lock_irq(&np->lock);
  1520. mii_ethtool_get_link_ksettings(&np->mii, cmd);
  1521. spin_unlock_irq(&np->lock);
  1522. return 0;
  1523. }
  1524. static int netdev_set_link_ksettings(struct net_device *dev,
  1525. const struct ethtool_link_ksettings *cmd)
  1526. {
  1527. struct netdev_private *np = netdev_priv(dev);
  1528. int rc;
  1529. spin_lock_irq(&np->lock);
  1530. rc = mii_ethtool_set_link_ksettings(&np->mii, cmd);
  1531. spin_unlock_irq(&np->lock);
  1532. return rc;
  1533. }
  1534. static int netdev_nway_reset(struct net_device *dev)
  1535. {
  1536. struct netdev_private *np = netdev_priv(dev);
  1537. return mii_nway_restart(&np->mii);
  1538. }
  1539. static u32 netdev_get_link(struct net_device *dev)
  1540. {
  1541. struct netdev_private *np = netdev_priv(dev);
  1542. return mii_link_ok(&np->mii);
  1543. }
  1544. static u32 netdev_get_msglevel(struct net_device *dev)
  1545. {
  1546. return debug;
  1547. }
  1548. static void netdev_set_msglevel(struct net_device *dev, u32 value)
  1549. {
  1550. debug = value;
  1551. }
  1552. static const struct ethtool_ops netdev_ethtool_ops = {
  1553. .get_drvinfo = netdev_get_drvinfo,
  1554. .nway_reset = netdev_nway_reset,
  1555. .get_link = netdev_get_link,
  1556. .get_msglevel = netdev_get_msglevel,
  1557. .set_msglevel = netdev_set_msglevel,
  1558. .get_link_ksettings = netdev_get_link_ksettings,
  1559. .set_link_ksettings = netdev_set_link_ksettings,
  1560. };
  1561. static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1562. {
  1563. struct netdev_private *np = netdev_priv(dev);
  1564. int rc;
  1565. if (!netif_running(dev))
  1566. return -EINVAL;
  1567. spin_lock_irq(&np->lock);
  1568. rc = generic_mii_ioctl(&np->mii, if_mii(rq), cmd, NULL);
  1569. spin_unlock_irq(&np->lock);
  1570. return rc;
  1571. }
  1572. static int netdev_close(struct net_device *dev)
  1573. {
  1574. struct netdev_private *np = netdev_priv(dev);
  1575. void __iomem *ioaddr = np->mem;
  1576. int i;
  1577. netif_stop_queue(dev);
  1578. /* Disable interrupts by clearing the interrupt mask. */
  1579. iowrite32(0x0000, ioaddr + IMR);
  1580. /* Stop the chip's Tx and Rx processes. */
  1581. stop_nic_rxtx(ioaddr, 0);
  1582. del_timer_sync(&np->timer);
  1583. del_timer_sync(&np->reset_timer);
  1584. free_irq(np->pci_dev->irq, dev);
  1585. /* Free all the skbuffs in the Rx queue. */
  1586. for (i = 0; i < RX_RING_SIZE; i++) {
  1587. struct sk_buff *skb = np->rx_ring[i].skbuff;
  1588. np->rx_ring[i].status = 0;
  1589. if (skb) {
  1590. pci_unmap_single(np->pci_dev, np->rx_ring[i].buffer,
  1591. np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1592. dev_kfree_skb(skb);
  1593. np->rx_ring[i].skbuff = NULL;
  1594. }
  1595. }
  1596. for (i = 0; i < TX_RING_SIZE; i++) {
  1597. struct sk_buff *skb = np->tx_ring[i].skbuff;
  1598. if (skb) {
  1599. pci_unmap_single(np->pci_dev, np->tx_ring[i].buffer,
  1600. skb->len, PCI_DMA_TODEVICE);
  1601. dev_kfree_skb(skb);
  1602. np->tx_ring[i].skbuff = NULL;
  1603. }
  1604. }
  1605. return 0;
  1606. }
  1607. static const struct pci_device_id fealnx_pci_tbl[] = {
  1608. {0x1516, 0x0800, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1609. {0x1516, 0x0803, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  1610. {0x1516, 0x0891, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
  1611. {} /* terminate list */
  1612. };
  1613. MODULE_DEVICE_TABLE(pci, fealnx_pci_tbl);
  1614. static struct pci_driver fealnx_driver = {
  1615. .name = "fealnx",
  1616. .id_table = fealnx_pci_tbl,
  1617. .probe = fealnx_init_one,
  1618. .remove = fealnx_remove_one,
  1619. };
  1620. static int __init fealnx_init(void)
  1621. {
  1622. /* when a module, this is printed whether or not devices are found in probe */
  1623. #ifdef MODULE
  1624. printk(version);
  1625. #endif
  1626. return pci_register_driver(&fealnx_driver);
  1627. }
  1628. static void __exit fealnx_exit(void)
  1629. {
  1630. pci_unregister_driver(&fealnx_driver);
  1631. }
  1632. module_init(fealnx_init);
  1633. module_exit(fealnx_exit);