fealnx.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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 <asm/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(unsigned long data);
  368. static void reset_timer(unsigned long data);
  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_change_mtu = eth_change_mtu,
  410. .ndo_set_mac_address = eth_mac_addr,
  411. .ndo_validate_addr = eth_validate_addr,
  412. };
  413. static int fealnx_init_one(struct pci_dev *pdev,
  414. const struct pci_device_id *ent)
  415. {
  416. struct netdev_private *np;
  417. int i, option, err, irq;
  418. static int card_idx = -1;
  419. char boardname[12];
  420. void __iomem *ioaddr;
  421. unsigned long len;
  422. unsigned int chip_id = ent->driver_data;
  423. struct net_device *dev;
  424. void *ring_space;
  425. dma_addr_t ring_dma;
  426. #ifdef USE_IO_OPS
  427. int bar = 0;
  428. #else
  429. int bar = 1;
  430. #endif
  431. /* when built into the kernel, we only print version if device is found */
  432. #ifndef MODULE
  433. static int printed_version;
  434. if (!printed_version++)
  435. printk(version);
  436. #endif
  437. card_idx++;
  438. sprintf(boardname, "fealnx%d", card_idx);
  439. option = card_idx < MAX_UNITS ? options[card_idx] : 0;
  440. i = pci_enable_device(pdev);
  441. if (i) return i;
  442. pci_set_master(pdev);
  443. len = pci_resource_len(pdev, bar);
  444. if (len < MIN_REGION_SIZE) {
  445. dev_err(&pdev->dev,
  446. "region size %ld too small, aborting\n", len);
  447. return -ENODEV;
  448. }
  449. i = pci_request_regions(pdev, boardname);
  450. if (i)
  451. return i;
  452. irq = pdev->irq;
  453. ioaddr = pci_iomap(pdev, bar, len);
  454. if (!ioaddr) {
  455. err = -ENOMEM;
  456. goto err_out_res;
  457. }
  458. dev = alloc_etherdev(sizeof(struct netdev_private));
  459. if (!dev) {
  460. err = -ENOMEM;
  461. goto err_out_unmap;
  462. }
  463. SET_NETDEV_DEV(dev, &pdev->dev);
  464. /* read ethernet id */
  465. for (i = 0; i < 6; ++i)
  466. dev->dev_addr[i] = ioread8(ioaddr + PAR0 + i);
  467. /* Reset the chip to erase previous misconfiguration. */
  468. iowrite32(0x00000001, ioaddr + BCR);
  469. /* Make certain the descriptor lists are aligned. */
  470. np = netdev_priv(dev);
  471. np->mem = ioaddr;
  472. spin_lock_init(&np->lock);
  473. np->pci_dev = pdev;
  474. np->flags = skel_netdrv_tbl[chip_id].flags;
  475. pci_set_drvdata(pdev, dev);
  476. np->mii.dev = dev;
  477. np->mii.mdio_read = mdio_read;
  478. np->mii.mdio_write = mdio_write;
  479. np->mii.phy_id_mask = 0x1f;
  480. np->mii.reg_num_mask = 0x1f;
  481. ring_space = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
  482. if (!ring_space) {
  483. err = -ENOMEM;
  484. goto err_out_free_dev;
  485. }
  486. np->rx_ring = ring_space;
  487. np->rx_ring_dma = ring_dma;
  488. ring_space = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
  489. if (!ring_space) {
  490. err = -ENOMEM;
  491. goto err_out_free_rx;
  492. }
  493. np->tx_ring = ring_space;
  494. np->tx_ring_dma = ring_dma;
  495. /* find the connected MII xcvrs */
  496. if (np->flags == HAS_MII_XCVR) {
  497. int phy, phy_idx = 0;
  498. for (phy = 1; phy < 32 && phy_idx < ARRAY_SIZE(np->phys);
  499. phy++) {
  500. int mii_status = mdio_read(dev, phy, 1);
  501. if (mii_status != 0xffff && mii_status != 0x0000) {
  502. np->phys[phy_idx++] = phy;
  503. dev_info(&pdev->dev,
  504. "MII PHY found at address %d, status "
  505. "0x%4.4x.\n", phy, mii_status);
  506. /* get phy type */
  507. {
  508. unsigned int data;
  509. data = mdio_read(dev, np->phys[0], 2);
  510. if (data == SeeqPHYID0)
  511. np->PHYType = SeeqPHY;
  512. else if (data == AhdocPHYID0)
  513. np->PHYType = AhdocPHY;
  514. else if (data == MarvellPHYID0)
  515. np->PHYType = MarvellPHY;
  516. else if (data == MysonPHYID0)
  517. np->PHYType = Myson981;
  518. else if (data == LevelOnePHYID0)
  519. np->PHYType = LevelOnePHY;
  520. else
  521. np->PHYType = OtherPHY;
  522. }
  523. }
  524. }
  525. np->mii_cnt = phy_idx;
  526. if (phy_idx == 0)
  527. dev_warn(&pdev->dev,
  528. "MII PHY not found -- this device may "
  529. "not operate correctly.\n");
  530. } else {
  531. np->phys[0] = 32;
  532. /* 89/6/23 add, (begin) */
  533. /* get phy type */
  534. if (ioread32(ioaddr + PHYIDENTIFIER) == MysonPHYID)
  535. np->PHYType = MysonPHY;
  536. else
  537. np->PHYType = OtherPHY;
  538. }
  539. np->mii.phy_id = np->phys[0];
  540. if (dev->mem_start)
  541. option = dev->mem_start;
  542. /* The lower four bits are the media type. */
  543. if (option > 0) {
  544. if (option & 0x200)
  545. np->mii.full_duplex = 1;
  546. np->default_port = option & 15;
  547. }
  548. if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
  549. np->mii.full_duplex = full_duplex[card_idx];
  550. if (np->mii.full_duplex) {
  551. dev_info(&pdev->dev, "Media type forced to Full Duplex.\n");
  552. /* 89/6/13 add, (begin) */
  553. // if (np->PHYType==MarvellPHY)
  554. if ((np->PHYType == MarvellPHY) || (np->PHYType == LevelOnePHY)) {
  555. unsigned int data;
  556. data = mdio_read(dev, np->phys[0], 9);
  557. data = (data & 0xfcff) | 0x0200;
  558. mdio_write(dev, np->phys[0], 9, data);
  559. }
  560. /* 89/6/13 add, (end) */
  561. if (np->flags == HAS_MII_XCVR)
  562. mdio_write(dev, np->phys[0], MII_ADVERTISE, ADVERTISE_FULL);
  563. else
  564. iowrite32(ADVERTISE_FULL, ioaddr + ANARANLPAR);
  565. np->mii.force_media = 1;
  566. }
  567. dev->netdev_ops = &netdev_ops;
  568. dev->ethtool_ops = &netdev_ethtool_ops;
  569. dev->watchdog_timeo = TX_TIMEOUT;
  570. err = register_netdev(dev);
  571. if (err)
  572. goto err_out_free_tx;
  573. printk(KERN_INFO "%s: %s at %p, %pM, IRQ %d.\n",
  574. dev->name, skel_netdrv_tbl[chip_id].chip_name, ioaddr,
  575. dev->dev_addr, irq);
  576. return 0;
  577. err_out_free_tx:
  578. pci_free_consistent(pdev, TX_TOTAL_SIZE, np->tx_ring, np->tx_ring_dma);
  579. err_out_free_rx:
  580. pci_free_consistent(pdev, RX_TOTAL_SIZE, np->rx_ring, np->rx_ring_dma);
  581. err_out_free_dev:
  582. free_netdev(dev);
  583. err_out_unmap:
  584. pci_iounmap(pdev, ioaddr);
  585. err_out_res:
  586. pci_release_regions(pdev);
  587. return err;
  588. }
  589. static void fealnx_remove_one(struct pci_dev *pdev)
  590. {
  591. struct net_device *dev = pci_get_drvdata(pdev);
  592. if (dev) {
  593. struct netdev_private *np = netdev_priv(dev);
  594. pci_free_consistent(pdev, TX_TOTAL_SIZE, np->tx_ring,
  595. np->tx_ring_dma);
  596. pci_free_consistent(pdev, RX_TOTAL_SIZE, np->rx_ring,
  597. np->rx_ring_dma);
  598. unregister_netdev(dev);
  599. pci_iounmap(pdev, np->mem);
  600. free_netdev(dev);
  601. pci_release_regions(pdev);
  602. } else
  603. printk(KERN_ERR "fealnx: remove for unknown device\n");
  604. }
  605. static ulong m80x_send_cmd_to_phy(void __iomem *miiport, int opcode, int phyad, int regad)
  606. {
  607. ulong miir;
  608. int i;
  609. unsigned int mask, data;
  610. /* enable MII output */
  611. miir = (ulong) ioread32(miiport);
  612. miir &= 0xfffffff0;
  613. miir |= MASK_MIIR_MII_WRITE + MASK_MIIR_MII_MDO;
  614. /* send 32 1's preamble */
  615. for (i = 0; i < 32; i++) {
  616. /* low MDC; MDO is already high (miir) */
  617. miir &= ~MASK_MIIR_MII_MDC;
  618. iowrite32(miir, miiport);
  619. /* high MDC */
  620. miir |= MASK_MIIR_MII_MDC;
  621. iowrite32(miir, miiport);
  622. }
  623. /* calculate ST+OP+PHYAD+REGAD+TA */
  624. data = opcode | (phyad << 7) | (regad << 2);
  625. /* sent out */
  626. mask = 0x8000;
  627. while (mask) {
  628. /* low MDC, prepare MDO */
  629. miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
  630. if (mask & data)
  631. miir |= MASK_MIIR_MII_MDO;
  632. iowrite32(miir, miiport);
  633. /* high MDC */
  634. miir |= MASK_MIIR_MII_MDC;
  635. iowrite32(miir, miiport);
  636. udelay(30);
  637. /* next */
  638. mask >>= 1;
  639. if (mask == 0x2 && opcode == OP_READ)
  640. miir &= ~MASK_MIIR_MII_WRITE;
  641. }
  642. return miir;
  643. }
  644. static int mdio_read(struct net_device *dev, int phyad, int regad)
  645. {
  646. struct netdev_private *np = netdev_priv(dev);
  647. void __iomem *miiport = np->mem + MANAGEMENT;
  648. ulong miir;
  649. unsigned int mask, data;
  650. miir = m80x_send_cmd_to_phy(miiport, OP_READ, phyad, regad);
  651. /* read data */
  652. mask = 0x8000;
  653. data = 0;
  654. while (mask) {
  655. /* low MDC */
  656. miir &= ~MASK_MIIR_MII_MDC;
  657. iowrite32(miir, miiport);
  658. /* read MDI */
  659. miir = ioread32(miiport);
  660. if (miir & MASK_MIIR_MII_MDI)
  661. data |= mask;
  662. /* high MDC, and wait */
  663. miir |= MASK_MIIR_MII_MDC;
  664. iowrite32(miir, miiport);
  665. udelay(30);
  666. /* next */
  667. mask >>= 1;
  668. }
  669. /* low MDC */
  670. miir &= ~MASK_MIIR_MII_MDC;
  671. iowrite32(miir, miiport);
  672. return data & 0xffff;
  673. }
  674. static void mdio_write(struct net_device *dev, int phyad, int regad, int data)
  675. {
  676. struct netdev_private *np = netdev_priv(dev);
  677. void __iomem *miiport = np->mem + MANAGEMENT;
  678. ulong miir;
  679. unsigned int mask;
  680. miir = m80x_send_cmd_to_phy(miiport, OP_WRITE, phyad, regad);
  681. /* write data */
  682. mask = 0x8000;
  683. while (mask) {
  684. /* low MDC, prepare MDO */
  685. miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
  686. if (mask & data)
  687. miir |= MASK_MIIR_MII_MDO;
  688. iowrite32(miir, miiport);
  689. /* high MDC */
  690. miir |= MASK_MIIR_MII_MDC;
  691. iowrite32(miir, miiport);
  692. /* next */
  693. mask >>= 1;
  694. }
  695. /* low MDC */
  696. miir &= ~MASK_MIIR_MII_MDC;
  697. iowrite32(miir, miiport);
  698. }
  699. static int netdev_open(struct net_device *dev)
  700. {
  701. struct netdev_private *np = netdev_priv(dev);
  702. void __iomem *ioaddr = np->mem;
  703. const int irq = np->pci_dev->irq;
  704. int rc, i;
  705. iowrite32(0x00000001, ioaddr + BCR); /* Reset */
  706. rc = request_irq(irq, intr_handler, IRQF_SHARED, dev->name, dev);
  707. if (rc)
  708. return -EAGAIN;
  709. for (i = 0; i < 3; i++)
  710. iowrite16(((unsigned short*)dev->dev_addr)[i],
  711. ioaddr + PAR0 + i*2);
  712. init_ring(dev);
  713. iowrite32(np->rx_ring_dma, ioaddr + RXLBA);
  714. iowrite32(np->tx_ring_dma, ioaddr + TXLBA);
  715. /* Initialize other registers. */
  716. /* Configure the PCI bus bursts and FIFO thresholds.
  717. 486: Set 8 longword burst.
  718. 586: no burst limit.
  719. Burst length 5:3
  720. 0 0 0 1
  721. 0 0 1 4
  722. 0 1 0 8
  723. 0 1 1 16
  724. 1 0 0 32
  725. 1 0 1 64
  726. 1 1 0 128
  727. 1 1 1 256
  728. Wait the specified 50 PCI cycles after a reset by initializing
  729. Tx and Rx queues and the address filter list.
  730. FIXME (Ueimor): optimistic for alpha + posted writes ? */
  731. np->bcrvalue = 0x10; /* little-endian, 8 burst length */
  732. #ifdef __BIG_ENDIAN
  733. np->bcrvalue |= 0x04; /* big-endian */
  734. #endif
  735. #if defined(__i386__) && !defined(MODULE)
  736. if (boot_cpu_data.x86 <= 4)
  737. np->crvalue = 0xa00;
  738. else
  739. #endif
  740. np->crvalue = 0xe00; /* rx 128 burst length */
  741. // 89/12/29 add,
  742. // 90/1/16 modify,
  743. // np->imrvalue=FBE|TUNF|CNTOVF|RBU|TI|RI;
  744. np->imrvalue = TUNF | CNTOVF | RBU | TI | RI;
  745. if (np->pci_dev->device == 0x891) {
  746. np->bcrvalue |= 0x200; /* set PROG bit */
  747. np->crvalue |= CR_W_ENH; /* set enhanced bit */
  748. np->imrvalue |= ETI;
  749. }
  750. iowrite32(np->bcrvalue, ioaddr + BCR);
  751. if (dev->if_port == 0)
  752. dev->if_port = np->default_port;
  753. iowrite32(0, ioaddr + RXPDR);
  754. // 89/9/1 modify,
  755. // np->crvalue = 0x00e40001; /* tx store and forward, tx/rx enable */
  756. np->crvalue |= 0x00e40001; /* tx store and forward, tx/rx enable */
  757. np->mii.full_duplex = np->mii.force_media;
  758. getlinkstatus(dev);
  759. if (np->linkok)
  760. getlinktype(dev);
  761. __set_rx_mode(dev);
  762. netif_start_queue(dev);
  763. /* Clear and Enable interrupts by setting the interrupt mask. */
  764. iowrite32(FBE | TUNF | CNTOVF | RBU | TI | RI, ioaddr + ISR);
  765. iowrite32(np->imrvalue, ioaddr + IMR);
  766. if (debug)
  767. printk(KERN_DEBUG "%s: Done netdev_open().\n", dev->name);
  768. /* Set the timer to check for link beat. */
  769. init_timer(&np->timer);
  770. np->timer.expires = RUN_AT(3 * HZ);
  771. np->timer.data = (unsigned long) dev;
  772. np->timer.function = netdev_timer;
  773. /* timer handler */
  774. add_timer(&np->timer);
  775. init_timer(&np->reset_timer);
  776. np->reset_timer.data = (unsigned long) dev;
  777. np->reset_timer.function = reset_timer;
  778. np->reset_timer_armed = 0;
  779. return rc;
  780. }
  781. static void getlinkstatus(struct net_device *dev)
  782. /* function: Routine will read MII Status Register to get link status. */
  783. /* input : dev... pointer to the adapter block. */
  784. /* output : none. */
  785. {
  786. struct netdev_private *np = netdev_priv(dev);
  787. unsigned int i, DelayTime = 0x1000;
  788. np->linkok = 0;
  789. if (np->PHYType == MysonPHY) {
  790. for (i = 0; i < DelayTime; ++i) {
  791. if (ioread32(np->mem + BMCRSR) & LinkIsUp2) {
  792. np->linkok = 1;
  793. return;
  794. }
  795. udelay(100);
  796. }
  797. } else {
  798. for (i = 0; i < DelayTime; ++i) {
  799. if (mdio_read(dev, np->phys[0], MII_BMSR) & BMSR_LSTATUS) {
  800. np->linkok = 1;
  801. return;
  802. }
  803. udelay(100);
  804. }
  805. }
  806. }
  807. static void getlinktype(struct net_device *dev)
  808. {
  809. struct netdev_private *np = netdev_priv(dev);
  810. if (np->PHYType == MysonPHY) { /* 3-in-1 case */
  811. if (ioread32(np->mem + TCRRCR) & CR_R_FD)
  812. np->duplexmode = 2; /* full duplex */
  813. else
  814. np->duplexmode = 1; /* half duplex */
  815. if (ioread32(np->mem + TCRRCR) & CR_R_PS10)
  816. np->line_speed = 1; /* 10M */
  817. else
  818. np->line_speed = 2; /* 100M */
  819. } else {
  820. if (np->PHYType == SeeqPHY) { /* this PHY is SEEQ 80225 */
  821. unsigned int data;
  822. data = mdio_read(dev, np->phys[0], MIIRegister18);
  823. if (data & SPD_DET_100)
  824. np->line_speed = 2; /* 100M */
  825. else
  826. np->line_speed = 1; /* 10M */
  827. if (data & DPLX_DET_FULL)
  828. np->duplexmode = 2; /* full duplex mode */
  829. else
  830. np->duplexmode = 1; /* half duplex mode */
  831. } else if (np->PHYType == AhdocPHY) {
  832. unsigned int data;
  833. data = mdio_read(dev, np->phys[0], DiagnosticReg);
  834. if (data & Speed_100)
  835. np->line_speed = 2; /* 100M */
  836. else
  837. np->line_speed = 1; /* 10M */
  838. if (data & DPLX_FULL)
  839. np->duplexmode = 2; /* full duplex mode */
  840. else
  841. np->duplexmode = 1; /* half duplex mode */
  842. }
  843. /* 89/6/13 add, (begin) */
  844. else if (np->PHYType == MarvellPHY) {
  845. unsigned int data;
  846. data = mdio_read(dev, np->phys[0], SpecificReg);
  847. if (data & Full_Duplex)
  848. np->duplexmode = 2; /* full duplex mode */
  849. else
  850. np->duplexmode = 1; /* half duplex mode */
  851. data &= SpeedMask;
  852. if (data == Speed_1000M)
  853. np->line_speed = 3; /* 1000M */
  854. else if (data == Speed_100M)
  855. np->line_speed = 2; /* 100M */
  856. else
  857. np->line_speed = 1; /* 10M */
  858. }
  859. /* 89/6/13 add, (end) */
  860. /* 89/7/27 add, (begin) */
  861. else if (np->PHYType == Myson981) {
  862. unsigned int data;
  863. data = mdio_read(dev, np->phys[0], StatusRegister);
  864. if (data & SPEED100)
  865. np->line_speed = 2;
  866. else
  867. np->line_speed = 1;
  868. if (data & FULLMODE)
  869. np->duplexmode = 2;
  870. else
  871. np->duplexmode = 1;
  872. }
  873. /* 89/7/27 add, (end) */
  874. /* 89/12/29 add */
  875. else if (np->PHYType == LevelOnePHY) {
  876. unsigned int data;
  877. data = mdio_read(dev, np->phys[0], SpecificReg);
  878. if (data & LXT1000_Full)
  879. np->duplexmode = 2; /* full duplex mode */
  880. else
  881. np->duplexmode = 1; /* half duplex mode */
  882. data &= SpeedMask;
  883. if (data == LXT1000_1000M)
  884. np->line_speed = 3; /* 1000M */
  885. else if (data == LXT1000_100M)
  886. np->line_speed = 2; /* 100M */
  887. else
  888. np->line_speed = 1; /* 10M */
  889. }
  890. np->crvalue &= (~CR_W_PS10) & (~CR_W_FD) & (~CR_W_PS1000);
  891. if (np->line_speed == 1)
  892. np->crvalue |= CR_W_PS10;
  893. else if (np->line_speed == 3)
  894. np->crvalue |= CR_W_PS1000;
  895. if (np->duplexmode == 2)
  896. np->crvalue |= CR_W_FD;
  897. }
  898. }
  899. /* Take lock before calling this */
  900. static void allocate_rx_buffers(struct net_device *dev)
  901. {
  902. struct netdev_private *np = netdev_priv(dev);
  903. /* allocate skb for rx buffers */
  904. while (np->really_rx_count != RX_RING_SIZE) {
  905. struct sk_buff *skb;
  906. skb = netdev_alloc_skb(dev, np->rx_buf_sz);
  907. if (skb == NULL)
  908. break; /* Better luck next round. */
  909. while (np->lack_rxbuf->skbuff)
  910. np->lack_rxbuf = np->lack_rxbuf->next_desc_logical;
  911. np->lack_rxbuf->skbuff = skb;
  912. np->lack_rxbuf->buffer = pci_map_single(np->pci_dev, skb->data,
  913. np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  914. np->lack_rxbuf->status = RXOWN;
  915. ++np->really_rx_count;
  916. }
  917. }
  918. static void netdev_timer(unsigned long data)
  919. {
  920. struct net_device *dev = (struct net_device *) data;
  921. struct netdev_private *np = netdev_priv(dev);
  922. void __iomem *ioaddr = np->mem;
  923. int old_crvalue = np->crvalue;
  924. unsigned int old_linkok = np->linkok;
  925. unsigned long flags;
  926. if (debug)
  927. printk(KERN_DEBUG "%s: Media selection timer tick, status %8.8x "
  928. "config %8.8x.\n", dev->name, ioread32(ioaddr + ISR),
  929. ioread32(ioaddr + TCRRCR));
  930. spin_lock_irqsave(&np->lock, flags);
  931. if (np->flags == HAS_MII_XCVR) {
  932. getlinkstatus(dev);
  933. if ((old_linkok == 0) && (np->linkok == 1)) { /* we need to detect the media type again */
  934. getlinktype(dev);
  935. if (np->crvalue != old_crvalue) {
  936. stop_nic_rxtx(ioaddr, np->crvalue);
  937. iowrite32(np->crvalue, ioaddr + TCRRCR);
  938. }
  939. }
  940. }
  941. allocate_rx_buffers(dev);
  942. spin_unlock_irqrestore(&np->lock, flags);
  943. np->timer.expires = RUN_AT(10 * HZ);
  944. add_timer(&np->timer);
  945. }
  946. /* Take lock before calling */
  947. /* Reset chip and disable rx, tx and interrupts */
  948. static void reset_and_disable_rxtx(struct net_device *dev)
  949. {
  950. struct netdev_private *np = netdev_priv(dev);
  951. void __iomem *ioaddr = np->mem;
  952. int delay=51;
  953. /* Reset the chip's Tx and Rx processes. */
  954. stop_nic_rxtx(ioaddr, 0);
  955. /* Disable interrupts by clearing the interrupt mask. */
  956. iowrite32(0, ioaddr + IMR);
  957. /* Reset the chip to erase previous misconfiguration. */
  958. iowrite32(0x00000001, ioaddr + BCR);
  959. /* Ueimor: wait for 50 PCI cycles (and flush posted writes btw).
  960. We surely wait too long (address+data phase). Who cares? */
  961. while (--delay) {
  962. ioread32(ioaddr + BCR);
  963. rmb();
  964. }
  965. }
  966. /* Take lock before calling */
  967. /* Restore chip after reset */
  968. static void enable_rxtx(struct net_device *dev)
  969. {
  970. struct netdev_private *np = netdev_priv(dev);
  971. void __iomem *ioaddr = np->mem;
  972. reset_rx_descriptors(dev);
  973. iowrite32(np->tx_ring_dma + ((char*)np->cur_tx - (char*)np->tx_ring),
  974. ioaddr + TXLBA);
  975. iowrite32(np->rx_ring_dma + ((char*)np->cur_rx - (char*)np->rx_ring),
  976. ioaddr + RXLBA);
  977. iowrite32(np->bcrvalue, ioaddr + BCR);
  978. iowrite32(0, ioaddr + RXPDR);
  979. __set_rx_mode(dev); /* changes np->crvalue, writes it into TCRRCR */
  980. /* Clear and Enable interrupts by setting the interrupt mask. */
  981. iowrite32(FBE | TUNF | CNTOVF | RBU | TI | RI, ioaddr + ISR);
  982. iowrite32(np->imrvalue, ioaddr + IMR);
  983. iowrite32(0, ioaddr + TXPDR);
  984. }
  985. static void reset_timer(unsigned long data)
  986. {
  987. struct net_device *dev = (struct net_device *) data;
  988. struct netdev_private *np = netdev_priv(dev);
  989. unsigned long flags;
  990. printk(KERN_WARNING "%s: resetting tx and rx machinery\n", dev->name);
  991. spin_lock_irqsave(&np->lock, flags);
  992. np->crvalue = np->crvalue_sv;
  993. np->imrvalue = np->imrvalue_sv;
  994. reset_and_disable_rxtx(dev);
  995. /* works for me without this:
  996. reset_tx_descriptors(dev); */
  997. enable_rxtx(dev);
  998. netif_start_queue(dev); /* FIXME: or netif_wake_queue(dev); ? */
  999. np->reset_timer_armed = 0;
  1000. spin_unlock_irqrestore(&np->lock, flags);
  1001. }
  1002. static void fealnx_tx_timeout(struct net_device *dev)
  1003. {
  1004. struct netdev_private *np = netdev_priv(dev);
  1005. void __iomem *ioaddr = np->mem;
  1006. unsigned long flags;
  1007. int i;
  1008. printk(KERN_WARNING
  1009. "%s: Transmit timed out, status %8.8x, resetting...\n",
  1010. dev->name, ioread32(ioaddr + ISR));
  1011. {
  1012. printk(KERN_DEBUG " Rx ring %p: ", np->rx_ring);
  1013. for (i = 0; i < RX_RING_SIZE; i++)
  1014. printk(KERN_CONT " %8.8x",
  1015. (unsigned int) np->rx_ring[i].status);
  1016. printk(KERN_CONT "\n");
  1017. printk(KERN_DEBUG " Tx ring %p: ", np->tx_ring);
  1018. for (i = 0; i < TX_RING_SIZE; i++)
  1019. printk(KERN_CONT " %4.4x", np->tx_ring[i].status);
  1020. printk(KERN_CONT "\n");
  1021. }
  1022. spin_lock_irqsave(&np->lock, flags);
  1023. reset_and_disable_rxtx(dev);
  1024. reset_tx_descriptors(dev);
  1025. enable_rxtx(dev);
  1026. spin_unlock_irqrestore(&np->lock, flags);
  1027. dev->trans_start = jiffies; /* prevent tx timeout */
  1028. dev->stats.tx_errors++;
  1029. netif_wake_queue(dev); /* or .._start_.. ?? */
  1030. }
  1031. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  1032. static void init_ring(struct net_device *dev)
  1033. {
  1034. struct netdev_private *np = netdev_priv(dev);
  1035. int i;
  1036. /* initialize rx variables */
  1037. np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
  1038. np->cur_rx = &np->rx_ring[0];
  1039. np->lack_rxbuf = np->rx_ring;
  1040. np->really_rx_count = 0;
  1041. /* initial rx descriptors. */
  1042. for (i = 0; i < RX_RING_SIZE; i++) {
  1043. np->rx_ring[i].status = 0;
  1044. np->rx_ring[i].control = np->rx_buf_sz << RBSShift;
  1045. np->rx_ring[i].next_desc = np->rx_ring_dma +
  1046. (i + 1)*sizeof(struct fealnx_desc);
  1047. np->rx_ring[i].next_desc_logical = &np->rx_ring[i + 1];
  1048. np->rx_ring[i].skbuff = NULL;
  1049. }
  1050. /* for the last rx descriptor */
  1051. np->rx_ring[i - 1].next_desc = np->rx_ring_dma;
  1052. np->rx_ring[i - 1].next_desc_logical = np->rx_ring;
  1053. /* allocate skb for rx buffers */
  1054. for (i = 0; i < RX_RING_SIZE; i++) {
  1055. struct sk_buff *skb = netdev_alloc_skb(dev, np->rx_buf_sz);
  1056. if (skb == NULL) {
  1057. np->lack_rxbuf = &np->rx_ring[i];
  1058. break;
  1059. }
  1060. ++np->really_rx_count;
  1061. np->rx_ring[i].skbuff = skb;
  1062. np->rx_ring[i].buffer = pci_map_single(np->pci_dev, skb->data,
  1063. np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1064. np->rx_ring[i].status = RXOWN;
  1065. np->rx_ring[i].control |= RXIC;
  1066. }
  1067. /* initialize tx variables */
  1068. np->cur_tx = &np->tx_ring[0];
  1069. np->cur_tx_copy = &np->tx_ring[0];
  1070. np->really_tx_count = 0;
  1071. np->free_tx_count = TX_RING_SIZE;
  1072. for (i = 0; i < TX_RING_SIZE; i++) {
  1073. np->tx_ring[i].status = 0;
  1074. /* do we need np->tx_ring[i].control = XXX; ?? */
  1075. np->tx_ring[i].next_desc = np->tx_ring_dma +
  1076. (i + 1)*sizeof(struct fealnx_desc);
  1077. np->tx_ring[i].next_desc_logical = &np->tx_ring[i + 1];
  1078. np->tx_ring[i].skbuff = NULL;
  1079. }
  1080. /* for the last tx descriptor */
  1081. np->tx_ring[i - 1].next_desc = np->tx_ring_dma;
  1082. np->tx_ring[i - 1].next_desc_logical = &np->tx_ring[0];
  1083. }
  1084. static netdev_tx_t start_tx(struct sk_buff *skb, struct net_device *dev)
  1085. {
  1086. struct netdev_private *np = netdev_priv(dev);
  1087. unsigned long flags;
  1088. spin_lock_irqsave(&np->lock, flags);
  1089. np->cur_tx_copy->skbuff = skb;
  1090. #define one_buffer
  1091. #define BPT 1022
  1092. #if defined(one_buffer)
  1093. np->cur_tx_copy->buffer = pci_map_single(np->pci_dev, skb->data,
  1094. skb->len, PCI_DMA_TODEVICE);
  1095. np->cur_tx_copy->control = TXIC | TXLD | TXFD | CRCEnable | PADEnable;
  1096. np->cur_tx_copy->control |= (skb->len << PKTSShift); /* pkt size */
  1097. np->cur_tx_copy->control |= (skb->len << TBSShift); /* buffer size */
  1098. // 89/12/29 add,
  1099. if (np->pci_dev->device == 0x891)
  1100. np->cur_tx_copy->control |= ETIControl | RetryTxLC;
  1101. np->cur_tx_copy->status = TXOWN;
  1102. np->cur_tx_copy = np->cur_tx_copy->next_desc_logical;
  1103. --np->free_tx_count;
  1104. #elif defined(two_buffer)
  1105. if (skb->len > BPT) {
  1106. struct fealnx_desc *next;
  1107. /* for the first descriptor */
  1108. np->cur_tx_copy->buffer = pci_map_single(np->pci_dev, skb->data,
  1109. BPT, PCI_DMA_TODEVICE);
  1110. np->cur_tx_copy->control = TXIC | TXFD | CRCEnable | PADEnable;
  1111. np->cur_tx_copy->control |= (skb->len << PKTSShift); /* pkt size */
  1112. np->cur_tx_copy->control |= (BPT << TBSShift); /* buffer size */
  1113. /* for the last descriptor */
  1114. next = np->cur_tx_copy->next_desc_logical;
  1115. next->skbuff = skb;
  1116. next->control = TXIC | TXLD | CRCEnable | PADEnable;
  1117. next->control |= (skb->len << PKTSShift); /* pkt size */
  1118. next->control |= ((skb->len - BPT) << TBSShift); /* buf size */
  1119. // 89/12/29 add,
  1120. if (np->pci_dev->device == 0x891)
  1121. np->cur_tx_copy->control |= ETIControl | RetryTxLC;
  1122. next->buffer = pci_map_single(ep->pci_dev, skb->data + BPT,
  1123. skb->len - BPT, PCI_DMA_TODEVICE);
  1124. next->status = TXOWN;
  1125. np->cur_tx_copy->status = TXOWN;
  1126. np->cur_tx_copy = next->next_desc_logical;
  1127. np->free_tx_count -= 2;
  1128. } else {
  1129. np->cur_tx_copy->buffer = pci_map_single(np->pci_dev, skb->data,
  1130. skb->len, PCI_DMA_TODEVICE);
  1131. np->cur_tx_copy->control = TXIC | TXLD | TXFD | CRCEnable | PADEnable;
  1132. np->cur_tx_copy->control |= (skb->len << PKTSShift); /* pkt size */
  1133. np->cur_tx_copy->control |= (skb->len << TBSShift); /* buffer size */
  1134. // 89/12/29 add,
  1135. if (np->pci_dev->device == 0x891)
  1136. np->cur_tx_copy->control |= ETIControl | RetryTxLC;
  1137. np->cur_tx_copy->status = TXOWN;
  1138. np->cur_tx_copy = np->cur_tx_copy->next_desc_logical;
  1139. --np->free_tx_count;
  1140. }
  1141. #endif
  1142. if (np->free_tx_count < 2)
  1143. netif_stop_queue(dev);
  1144. ++np->really_tx_count;
  1145. iowrite32(0, np->mem + TXPDR);
  1146. spin_unlock_irqrestore(&np->lock, flags);
  1147. return NETDEV_TX_OK;
  1148. }
  1149. /* Take lock before calling */
  1150. /* Chip probably hosed tx ring. Clean up. */
  1151. static void reset_tx_descriptors(struct net_device *dev)
  1152. {
  1153. struct netdev_private *np = netdev_priv(dev);
  1154. struct fealnx_desc *cur;
  1155. int i;
  1156. /* initialize tx variables */
  1157. np->cur_tx = &np->tx_ring[0];
  1158. np->cur_tx_copy = &np->tx_ring[0];
  1159. np->really_tx_count = 0;
  1160. np->free_tx_count = TX_RING_SIZE;
  1161. for (i = 0; i < TX_RING_SIZE; i++) {
  1162. cur = &np->tx_ring[i];
  1163. if (cur->skbuff) {
  1164. pci_unmap_single(np->pci_dev, cur->buffer,
  1165. cur->skbuff->len, PCI_DMA_TODEVICE);
  1166. dev_kfree_skb_any(cur->skbuff);
  1167. cur->skbuff = NULL;
  1168. }
  1169. cur->status = 0;
  1170. cur->control = 0; /* needed? */
  1171. /* probably not needed. We do it for purely paranoid reasons */
  1172. cur->next_desc = np->tx_ring_dma +
  1173. (i + 1)*sizeof(struct fealnx_desc);
  1174. cur->next_desc_logical = &np->tx_ring[i + 1];
  1175. }
  1176. /* for the last tx descriptor */
  1177. np->tx_ring[TX_RING_SIZE - 1].next_desc = np->tx_ring_dma;
  1178. np->tx_ring[TX_RING_SIZE - 1].next_desc_logical = &np->tx_ring[0];
  1179. }
  1180. /* Take lock and stop rx before calling this */
  1181. static void reset_rx_descriptors(struct net_device *dev)
  1182. {
  1183. struct netdev_private *np = netdev_priv(dev);
  1184. struct fealnx_desc *cur = np->cur_rx;
  1185. int i;
  1186. allocate_rx_buffers(dev);
  1187. for (i = 0; i < RX_RING_SIZE; i++) {
  1188. if (cur->skbuff)
  1189. cur->status = RXOWN;
  1190. cur = cur->next_desc_logical;
  1191. }
  1192. iowrite32(np->rx_ring_dma + ((char*)np->cur_rx - (char*)np->rx_ring),
  1193. np->mem + RXLBA);
  1194. }
  1195. /* The interrupt handler does all of the Rx thread work and cleans up
  1196. after the Tx thread. */
  1197. static irqreturn_t intr_handler(int irq, void *dev_instance)
  1198. {
  1199. struct net_device *dev = (struct net_device *) dev_instance;
  1200. struct netdev_private *np = netdev_priv(dev);
  1201. void __iomem *ioaddr = np->mem;
  1202. long boguscnt = max_interrupt_work;
  1203. unsigned int num_tx = 0;
  1204. int handled = 0;
  1205. spin_lock(&np->lock);
  1206. iowrite32(0, ioaddr + IMR);
  1207. do {
  1208. u32 intr_status = ioread32(ioaddr + ISR);
  1209. /* Acknowledge all of the current interrupt sources ASAP. */
  1210. iowrite32(intr_status, ioaddr + ISR);
  1211. if (debug)
  1212. printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n", dev->name,
  1213. intr_status);
  1214. if (!(intr_status & np->imrvalue))
  1215. break;
  1216. handled = 1;
  1217. // 90/1/16 delete,
  1218. //
  1219. // if (intr_status & FBE)
  1220. // { /* fatal error */
  1221. // stop_nic_tx(ioaddr, 0);
  1222. // stop_nic_rx(ioaddr, 0);
  1223. // break;
  1224. // };
  1225. if (intr_status & TUNF)
  1226. iowrite32(0, ioaddr + TXPDR);
  1227. if (intr_status & CNTOVF) {
  1228. /* missed pkts */
  1229. dev->stats.rx_missed_errors +=
  1230. ioread32(ioaddr + TALLY) & 0x7fff;
  1231. /* crc error */
  1232. dev->stats.rx_crc_errors +=
  1233. (ioread32(ioaddr + TALLY) & 0x7fff0000) >> 16;
  1234. }
  1235. if (intr_status & (RI | RBU)) {
  1236. if (intr_status & RI)
  1237. netdev_rx(dev);
  1238. else {
  1239. stop_nic_rx(ioaddr, np->crvalue);
  1240. reset_rx_descriptors(dev);
  1241. iowrite32(np->crvalue, ioaddr + TCRRCR);
  1242. }
  1243. }
  1244. while (np->really_tx_count) {
  1245. long tx_status = np->cur_tx->status;
  1246. long tx_control = np->cur_tx->control;
  1247. if (!(tx_control & TXLD)) { /* this pkt is combined by two tx descriptors */
  1248. struct fealnx_desc *next;
  1249. next = np->cur_tx->next_desc_logical;
  1250. tx_status = next->status;
  1251. tx_control = next->control;
  1252. }
  1253. if (tx_status & TXOWN)
  1254. break;
  1255. if (!(np->crvalue & CR_W_ENH)) {
  1256. if (tx_status & (CSL | LC | EC | UDF | HF)) {
  1257. dev->stats.tx_errors++;
  1258. if (tx_status & EC)
  1259. dev->stats.tx_aborted_errors++;
  1260. if (tx_status & CSL)
  1261. dev->stats.tx_carrier_errors++;
  1262. if (tx_status & LC)
  1263. dev->stats.tx_window_errors++;
  1264. if (tx_status & UDF)
  1265. dev->stats.tx_fifo_errors++;
  1266. if ((tx_status & HF) && np->mii.full_duplex == 0)
  1267. dev->stats.tx_heartbeat_errors++;
  1268. } else {
  1269. dev->stats.tx_bytes +=
  1270. ((tx_control & PKTSMask) >> PKTSShift);
  1271. dev->stats.collisions +=
  1272. ((tx_status & NCRMask) >> NCRShift);
  1273. dev->stats.tx_packets++;
  1274. }
  1275. } else {
  1276. dev->stats.tx_bytes +=
  1277. ((tx_control & PKTSMask) >> PKTSShift);
  1278. dev->stats.tx_packets++;
  1279. }
  1280. /* Free the original skb. */
  1281. pci_unmap_single(np->pci_dev, np->cur_tx->buffer,
  1282. np->cur_tx->skbuff->len, PCI_DMA_TODEVICE);
  1283. dev_kfree_skb_irq(np->cur_tx->skbuff);
  1284. np->cur_tx->skbuff = NULL;
  1285. --np->really_tx_count;
  1286. if (np->cur_tx->control & TXLD) {
  1287. np->cur_tx = np->cur_tx->next_desc_logical;
  1288. ++np->free_tx_count;
  1289. } else {
  1290. np->cur_tx = np->cur_tx->next_desc_logical;
  1291. np->cur_tx = np->cur_tx->next_desc_logical;
  1292. np->free_tx_count += 2;
  1293. }
  1294. num_tx++;
  1295. } /* end of for loop */
  1296. if (num_tx && np->free_tx_count >= 2)
  1297. netif_wake_queue(dev);
  1298. /* read transmit status for enhanced mode only */
  1299. if (np->crvalue & CR_W_ENH) {
  1300. long data;
  1301. data = ioread32(ioaddr + TSR);
  1302. dev->stats.tx_errors += (data & 0xff000000) >> 24;
  1303. dev->stats.tx_aborted_errors +=
  1304. (data & 0xff000000) >> 24;
  1305. dev->stats.tx_window_errors +=
  1306. (data & 0x00ff0000) >> 16;
  1307. dev->stats.collisions += (data & 0x0000ffff);
  1308. }
  1309. if (--boguscnt < 0) {
  1310. printk(KERN_WARNING "%s: Too much work at interrupt, "
  1311. "status=0x%4.4x.\n", dev->name, intr_status);
  1312. if (!np->reset_timer_armed) {
  1313. np->reset_timer_armed = 1;
  1314. np->reset_timer.expires = RUN_AT(HZ/2);
  1315. add_timer(&np->reset_timer);
  1316. stop_nic_rxtx(ioaddr, 0);
  1317. netif_stop_queue(dev);
  1318. /* or netif_tx_disable(dev); ?? */
  1319. /* Prevent other paths from enabling tx,rx,intrs */
  1320. np->crvalue_sv = np->crvalue;
  1321. np->imrvalue_sv = np->imrvalue;
  1322. np->crvalue &= ~(CR_W_TXEN | CR_W_RXEN); /* or simply = 0? */
  1323. np->imrvalue = 0;
  1324. }
  1325. break;
  1326. }
  1327. } while (1);
  1328. /* read the tally counters */
  1329. /* missed pkts */
  1330. dev->stats.rx_missed_errors += ioread32(ioaddr + TALLY) & 0x7fff;
  1331. /* crc error */
  1332. dev->stats.rx_crc_errors +=
  1333. (ioread32(ioaddr + TALLY) & 0x7fff0000) >> 16;
  1334. if (debug)
  1335. printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
  1336. dev->name, ioread32(ioaddr + ISR));
  1337. iowrite32(np->imrvalue, ioaddr + IMR);
  1338. spin_unlock(&np->lock);
  1339. return IRQ_RETVAL(handled);
  1340. }
  1341. /* This routine is logically part of the interrupt handler, but separated
  1342. for clarity and better register allocation. */
  1343. static int netdev_rx(struct net_device *dev)
  1344. {
  1345. struct netdev_private *np = netdev_priv(dev);
  1346. void __iomem *ioaddr = np->mem;
  1347. /* If EOP is set on the next entry, it's a new packet. Send it up. */
  1348. while (!(np->cur_rx->status & RXOWN) && np->cur_rx->skbuff) {
  1349. s32 rx_status = np->cur_rx->status;
  1350. if (np->really_rx_count == 0)
  1351. break;
  1352. if (debug)
  1353. printk(KERN_DEBUG " netdev_rx() status was %8.8x.\n", rx_status);
  1354. if ((!((rx_status & RXFSD) && (rx_status & RXLSD))) ||
  1355. (rx_status & ErrorSummary)) {
  1356. if (rx_status & ErrorSummary) { /* there was a fatal error */
  1357. if (debug)
  1358. printk(KERN_DEBUG
  1359. "%s: Receive error, Rx status %8.8x.\n",
  1360. dev->name, rx_status);
  1361. dev->stats.rx_errors++; /* end of a packet. */
  1362. if (rx_status & (LONGPKT | RUNTPKT))
  1363. dev->stats.rx_length_errors++;
  1364. if (rx_status & RXER)
  1365. dev->stats.rx_frame_errors++;
  1366. if (rx_status & CRC)
  1367. dev->stats.rx_crc_errors++;
  1368. } else {
  1369. int need_to_reset = 0;
  1370. int desno = 0;
  1371. if (rx_status & RXFSD) { /* this pkt is too long, over one rx buffer */
  1372. struct fealnx_desc *cur;
  1373. /* check this packet is received completely? */
  1374. cur = np->cur_rx;
  1375. while (desno <= np->really_rx_count) {
  1376. ++desno;
  1377. if ((!(cur->status & RXOWN)) &&
  1378. (cur->status & RXLSD))
  1379. break;
  1380. /* goto next rx descriptor */
  1381. cur = cur->next_desc_logical;
  1382. }
  1383. if (desno > np->really_rx_count)
  1384. need_to_reset = 1;
  1385. } else /* RXLSD did not find, something error */
  1386. need_to_reset = 1;
  1387. if (need_to_reset == 0) {
  1388. int i;
  1389. dev->stats.rx_length_errors++;
  1390. /* free all rx descriptors related this long pkt */
  1391. for (i = 0; i < desno; ++i) {
  1392. if (!np->cur_rx->skbuff) {
  1393. printk(KERN_DEBUG
  1394. "%s: I'm scared\n", dev->name);
  1395. break;
  1396. }
  1397. np->cur_rx->status = RXOWN;
  1398. np->cur_rx = np->cur_rx->next_desc_logical;
  1399. }
  1400. continue;
  1401. } else { /* rx error, need to reset this chip */
  1402. stop_nic_rx(ioaddr, np->crvalue);
  1403. reset_rx_descriptors(dev);
  1404. iowrite32(np->crvalue, ioaddr + TCRRCR);
  1405. }
  1406. break; /* exit the while loop */
  1407. }
  1408. } else { /* this received pkt is ok */
  1409. struct sk_buff *skb;
  1410. /* Omit the four octet CRC from the length. */
  1411. short pkt_len = ((rx_status & FLNGMASK) >> FLNGShift) - 4;
  1412. #ifndef final_version
  1413. if (debug)
  1414. printk(KERN_DEBUG " netdev_rx() normal Rx pkt length %d"
  1415. " status %x.\n", pkt_len, rx_status);
  1416. #endif
  1417. /* Check if the packet is long enough to accept without copying
  1418. to a minimally-sized skbuff. */
  1419. if (pkt_len < rx_copybreak &&
  1420. (skb = netdev_alloc_skb(dev, pkt_len + 2)) != NULL) {
  1421. skb_reserve(skb, 2); /* 16 byte align the IP header */
  1422. pci_dma_sync_single_for_cpu(np->pci_dev,
  1423. np->cur_rx->buffer,
  1424. np->rx_buf_sz,
  1425. PCI_DMA_FROMDEVICE);
  1426. /* Call copy + cksum if available. */
  1427. #if ! defined(__alpha__)
  1428. skb_copy_to_linear_data(skb,
  1429. np->cur_rx->skbuff->data, pkt_len);
  1430. skb_put(skb, pkt_len);
  1431. #else
  1432. memcpy(skb_put(skb, pkt_len),
  1433. np->cur_rx->skbuff->data, pkt_len);
  1434. #endif
  1435. pci_dma_sync_single_for_device(np->pci_dev,
  1436. np->cur_rx->buffer,
  1437. np->rx_buf_sz,
  1438. PCI_DMA_FROMDEVICE);
  1439. } else {
  1440. pci_unmap_single(np->pci_dev,
  1441. np->cur_rx->buffer,
  1442. np->rx_buf_sz,
  1443. PCI_DMA_FROMDEVICE);
  1444. skb_put(skb = np->cur_rx->skbuff, pkt_len);
  1445. np->cur_rx->skbuff = NULL;
  1446. --np->really_rx_count;
  1447. }
  1448. skb->protocol = eth_type_trans(skb, dev);
  1449. netif_rx(skb);
  1450. dev->stats.rx_packets++;
  1451. dev->stats.rx_bytes += pkt_len;
  1452. }
  1453. np->cur_rx = np->cur_rx->next_desc_logical;
  1454. } /* end of while loop */
  1455. /* allocate skb for rx buffers */
  1456. allocate_rx_buffers(dev);
  1457. return 0;
  1458. }
  1459. static struct net_device_stats *get_stats(struct net_device *dev)
  1460. {
  1461. struct netdev_private *np = netdev_priv(dev);
  1462. void __iomem *ioaddr = np->mem;
  1463. /* The chip only need report frame silently dropped. */
  1464. if (netif_running(dev)) {
  1465. dev->stats.rx_missed_errors +=
  1466. ioread32(ioaddr + TALLY) & 0x7fff;
  1467. dev->stats.rx_crc_errors +=
  1468. (ioread32(ioaddr + TALLY) & 0x7fff0000) >> 16;
  1469. }
  1470. return &dev->stats;
  1471. }
  1472. /* for dev->set_multicast_list */
  1473. static void set_rx_mode(struct net_device *dev)
  1474. {
  1475. spinlock_t *lp = &((struct netdev_private *)netdev_priv(dev))->lock;
  1476. unsigned long flags;
  1477. spin_lock_irqsave(lp, flags);
  1478. __set_rx_mode(dev);
  1479. spin_unlock_irqrestore(lp, flags);
  1480. }
  1481. /* Take lock before calling */
  1482. static void __set_rx_mode(struct net_device *dev)
  1483. {
  1484. struct netdev_private *np = netdev_priv(dev);
  1485. void __iomem *ioaddr = np->mem;
  1486. u32 mc_filter[2]; /* Multicast hash filter */
  1487. u32 rx_mode;
  1488. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1489. memset(mc_filter, 0xff, sizeof(mc_filter));
  1490. rx_mode = CR_W_PROM | CR_W_AB | CR_W_AM;
  1491. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  1492. (dev->flags & IFF_ALLMULTI)) {
  1493. /* Too many to match, or accept all multicasts. */
  1494. memset(mc_filter, 0xff, sizeof(mc_filter));
  1495. rx_mode = CR_W_AB | CR_W_AM;
  1496. } else {
  1497. struct netdev_hw_addr *ha;
  1498. memset(mc_filter, 0, sizeof(mc_filter));
  1499. netdev_for_each_mc_addr(ha, dev) {
  1500. unsigned int bit;
  1501. bit = (ether_crc(ETH_ALEN, ha->addr) >> 26) ^ 0x3F;
  1502. mc_filter[bit >> 5] |= (1 << bit);
  1503. }
  1504. rx_mode = CR_W_AB | CR_W_AM;
  1505. }
  1506. stop_nic_rxtx(ioaddr, np->crvalue);
  1507. iowrite32(mc_filter[0], ioaddr + MAR0);
  1508. iowrite32(mc_filter[1], ioaddr + MAR1);
  1509. np->crvalue &= ~CR_W_RXMODEMASK;
  1510. np->crvalue |= rx_mode;
  1511. iowrite32(np->crvalue, ioaddr + TCRRCR);
  1512. }
  1513. static void netdev_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1514. {
  1515. struct netdev_private *np = netdev_priv(dev);
  1516. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1517. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1518. strlcpy(info->bus_info, pci_name(np->pci_dev), sizeof(info->bus_info));
  1519. }
  1520. static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1521. {
  1522. struct netdev_private *np = netdev_priv(dev);
  1523. int rc;
  1524. spin_lock_irq(&np->lock);
  1525. rc = mii_ethtool_gset(&np->mii, cmd);
  1526. spin_unlock_irq(&np->lock);
  1527. return rc;
  1528. }
  1529. static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1530. {
  1531. struct netdev_private *np = netdev_priv(dev);
  1532. int rc;
  1533. spin_lock_irq(&np->lock);
  1534. rc = mii_ethtool_sset(&np->mii, cmd);
  1535. spin_unlock_irq(&np->lock);
  1536. return rc;
  1537. }
  1538. static int netdev_nway_reset(struct net_device *dev)
  1539. {
  1540. struct netdev_private *np = netdev_priv(dev);
  1541. return mii_nway_restart(&np->mii);
  1542. }
  1543. static u32 netdev_get_link(struct net_device *dev)
  1544. {
  1545. struct netdev_private *np = netdev_priv(dev);
  1546. return mii_link_ok(&np->mii);
  1547. }
  1548. static u32 netdev_get_msglevel(struct net_device *dev)
  1549. {
  1550. return debug;
  1551. }
  1552. static void netdev_set_msglevel(struct net_device *dev, u32 value)
  1553. {
  1554. debug = value;
  1555. }
  1556. static const struct ethtool_ops netdev_ethtool_ops = {
  1557. .get_drvinfo = netdev_get_drvinfo,
  1558. .get_settings = netdev_get_settings,
  1559. .set_settings = netdev_set_settings,
  1560. .nway_reset = netdev_nway_reset,
  1561. .get_link = netdev_get_link,
  1562. .get_msglevel = netdev_get_msglevel,
  1563. .set_msglevel = netdev_set_msglevel,
  1564. };
  1565. static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1566. {
  1567. struct netdev_private *np = netdev_priv(dev);
  1568. int rc;
  1569. if (!netif_running(dev))
  1570. return -EINVAL;
  1571. spin_lock_irq(&np->lock);
  1572. rc = generic_mii_ioctl(&np->mii, if_mii(rq), cmd, NULL);
  1573. spin_unlock_irq(&np->lock);
  1574. return rc;
  1575. }
  1576. static int netdev_close(struct net_device *dev)
  1577. {
  1578. struct netdev_private *np = netdev_priv(dev);
  1579. void __iomem *ioaddr = np->mem;
  1580. int i;
  1581. netif_stop_queue(dev);
  1582. /* Disable interrupts by clearing the interrupt mask. */
  1583. iowrite32(0x0000, ioaddr + IMR);
  1584. /* Stop the chip's Tx and Rx processes. */
  1585. stop_nic_rxtx(ioaddr, 0);
  1586. del_timer_sync(&np->timer);
  1587. del_timer_sync(&np->reset_timer);
  1588. free_irq(np->pci_dev->irq, dev);
  1589. /* Free all the skbuffs in the Rx queue. */
  1590. for (i = 0; i < RX_RING_SIZE; i++) {
  1591. struct sk_buff *skb = np->rx_ring[i].skbuff;
  1592. np->rx_ring[i].status = 0;
  1593. if (skb) {
  1594. pci_unmap_single(np->pci_dev, np->rx_ring[i].buffer,
  1595. np->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1596. dev_kfree_skb(skb);
  1597. np->rx_ring[i].skbuff = NULL;
  1598. }
  1599. }
  1600. for (i = 0; i < TX_RING_SIZE; i++) {
  1601. struct sk_buff *skb = np->tx_ring[i].skbuff;
  1602. if (skb) {
  1603. pci_unmap_single(np->pci_dev, np->tx_ring[i].buffer,
  1604. skb->len, PCI_DMA_TODEVICE);
  1605. dev_kfree_skb(skb);
  1606. np->tx_ring[i].skbuff = NULL;
  1607. }
  1608. }
  1609. return 0;
  1610. }
  1611. static const struct pci_device_id fealnx_pci_tbl[] = {
  1612. {0x1516, 0x0800, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1613. {0x1516, 0x0803, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  1614. {0x1516, 0x0891, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
  1615. {} /* terminate list */
  1616. };
  1617. MODULE_DEVICE_TABLE(pci, fealnx_pci_tbl);
  1618. static struct pci_driver fealnx_driver = {
  1619. .name = "fealnx",
  1620. .id_table = fealnx_pci_tbl,
  1621. .probe = fealnx_init_one,
  1622. .remove = fealnx_remove_one,
  1623. };
  1624. static int __init fealnx_init(void)
  1625. {
  1626. /* when a module, this is printed whether or not devices are found in probe */
  1627. #ifdef MODULE
  1628. printk(version);
  1629. #endif
  1630. return pci_register_driver(&fealnx_driver);
  1631. }
  1632. static void __exit fealnx_exit(void)
  1633. {
  1634. pci_unregister_driver(&fealnx_driver);
  1635. }
  1636. module_init(fealnx_init);
  1637. module_exit(fealnx_exit);