3c509.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. /* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
  2. /*
  3. Written 1993-2000 by Donald Becker.
  4. Copyright 1994-2000 by Donald Becker.
  5. Copyright 1993 United States Government as represented by the
  6. Director, National Security Agency. This software may be used and
  7. distributed according to the terms of the GNU General Public License,
  8. incorporated herein by reference.
  9. This driver is for the 3Com EtherLinkIII series.
  10. The author may be reached as becker@scyld.com, or C/O
  11. Scyld Computing Corporation
  12. 410 Severn Ave., Suite 210
  13. Annapolis MD 21403
  14. Known limitations:
  15. Because of the way 3c509 ISA detection works it's difficult to predict
  16. a priori which of several ISA-mode cards will be detected first.
  17. This driver does not use predictive interrupt mode, resulting in higher
  18. packet latency but lower overhead. If interrupts are disabled for an
  19. unusually long time it could also result in missed packets, but in
  20. practice this rarely happens.
  21. FIXES:
  22. Alan Cox: Removed the 'Unexpected interrupt' bug.
  23. Michael Meskes: Upgraded to Donald Becker's version 1.07.
  24. Alan Cox: Increased the eeprom delay. Regardless of
  25. what the docs say some people definitely
  26. get problems with lower (but in card spec)
  27. delays
  28. v1.10 4/21/97 Fixed module code so that multiple cards may be detected,
  29. other cleanups. -djb
  30. Andrea Arcangeli: Upgraded to Donald Becker's version 1.12.
  31. Rick Payne: Fixed SMP race condition
  32. v1.13 9/8/97 Made 'max_interrupt_work' an insmod-settable variable -djb
  33. v1.14 10/15/97 Avoided waiting..discard message for fast machines -djb
  34. v1.15 1/31/98 Faster recovery for Tx errors. -djb
  35. v1.16 2/3/98 Different ID port handling to avoid sound cards. -djb
  36. v1.18 12Mar2001 Andrew Morton <andrewm@uow.edu.au>
  37. - Avoid bogus detect of 3c590's (Andrzej Krzysztofowicz)
  38. - Reviewed against 1.18 from scyld.com
  39. v1.18a 17Nov2001 Jeff Garzik <jgarzik@pobox.com>
  40. - ethtool support
  41. v1.18b 1Mar2002 Zwane Mwaikambo <zwane@commfireservices.com>
  42. - Power Management support
  43. v1.18c 1Mar2002 David Ruggiero <jdr@farfalle.com>
  44. - Full duplex support
  45. v1.19 16Oct2002 Zwane Mwaikambo <zwane@linuxpower.ca>
  46. - Additional ethtool features
  47. v1.19a 28Oct2002 Davud Ruggiero <jdr@farfalle.com>
  48. - Increase *read_eeprom udelay to workaround oops with 2 cards.
  49. v1.19b 08Nov2002 Marc Zyngier <maz@wild-wind.fr.eu.org>
  50. - Introduce driver model for EISA cards.
  51. */
  52. #define DRV_NAME "3c509"
  53. #define DRV_VERSION "1.19b"
  54. #define DRV_RELDATE "08Nov2002"
  55. /* A few values that may be tweaked. */
  56. /* Time in jiffies before concluding the transmitter is hung. */
  57. #define TX_TIMEOUT (400*HZ/1000)
  58. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  59. static int max_interrupt_work = 10;
  60. #include <linux/module.h>
  61. #ifdef CONFIG_MCA
  62. #include <linux/mca.h>
  63. #endif
  64. #include <linux/isapnp.h>
  65. #include <linux/string.h>
  66. #include <linux/interrupt.h>
  67. #include <linux/errno.h>
  68. #include <linux/in.h>
  69. #include <linux/slab.h>
  70. #include <linux/ioport.h>
  71. #include <linux/init.h>
  72. #include <linux/netdevice.h>
  73. #include <linux/etherdevice.h>
  74. #include <linux/pm.h>
  75. #include <linux/pm_legacy.h>
  76. #include <linux/skbuff.h>
  77. #include <linux/delay.h> /* for udelay() */
  78. #include <linux/spinlock.h>
  79. #include <linux/ethtool.h>
  80. #include <linux/device.h>
  81. #include <linux/eisa.h>
  82. #include <linux/bitops.h>
  83. #include <asm/uaccess.h>
  84. #include <asm/io.h>
  85. #include <asm/irq.h>
  86. static char versionA[] __initdata = DRV_NAME ".c:" DRV_VERSION " " DRV_RELDATE " becker@scyld.com\n";
  87. static char versionB[] __initdata = "http://www.scyld.com/network/3c509.html\n";
  88. #if defined(CONFIG_PM) && (defined(CONFIG_MCA) || defined(CONFIG_EISA))
  89. #define EL3_SUSPEND
  90. #endif
  91. #ifdef EL3_DEBUG
  92. static int el3_debug = EL3_DEBUG;
  93. #else
  94. static int el3_debug = 2;
  95. #endif
  96. /* Used to do a global count of all the cards in the system. Must be
  97. * a global variable so that the mca/eisa probe routines can increment
  98. * it */
  99. static int el3_cards = 0;
  100. /* To minimize the size of the driver source I only define operating
  101. constants if they are used several times. You'll need the manual
  102. anyway if you want to understand driver details. */
  103. /* Offsets from base I/O address. */
  104. #define EL3_DATA 0x00
  105. #define EL3_CMD 0x0e
  106. #define EL3_STATUS 0x0e
  107. #define EEPROM_READ 0x80
  108. #define EL3_IO_EXTENT 16
  109. #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
  110. /* The top five bits written to EL3_CMD are a command, the lower
  111. 11 bits are the parameter, if applicable. */
  112. enum c509cmd {
  113. TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
  114. RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
  115. TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
  116. FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
  117. SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
  118. SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
  119. StatsDisable = 22<<11, StopCoax = 23<<11, PowerUp = 27<<11,
  120. PowerDown = 28<<11, PowerAuto = 29<<11};
  121. enum c509status {
  122. IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
  123. TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
  124. IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
  125. /* The SetRxFilter command accepts the following classes: */
  126. enum RxFilter {
  127. RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
  128. /* Register window 1 offsets, the window used in normal operation. */
  129. #define TX_FIFO 0x00
  130. #define RX_FIFO 0x00
  131. #define RX_STATUS 0x08
  132. #define TX_STATUS 0x0B
  133. #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */
  134. #define WN0_CONF_CTRL 0x04 /* Window 0: Configuration control register */
  135. #define WN0_ADDR_CONF 0x06 /* Window 0: Address configuration register */
  136. #define WN0_IRQ 0x08 /* Window 0: Set IRQ line in bits 12-15. */
  137. #define WN4_MEDIA 0x0A /* Window 4: Various transcvr/media bits. */
  138. #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
  139. #define WN4_NETDIAG 0x06 /* Window 4: Net diagnostic */
  140. #define FD_ENABLE 0x8000 /* Enable full-duplex ("external loopback") */
  141. /*
  142. * Must be a power of two (we use a binary and in the
  143. * circular queue)
  144. */
  145. #define SKB_QUEUE_SIZE 64
  146. struct el3_private {
  147. struct net_device_stats stats;
  148. struct net_device *next_dev;
  149. spinlock_t lock;
  150. /* skb send-queue */
  151. int head, size;
  152. struct sk_buff *queue[SKB_QUEUE_SIZE];
  153. enum {
  154. EL3_MCA,
  155. EL3_PNP,
  156. EL3_EISA,
  157. } type; /* type of device */
  158. struct device *dev;
  159. };
  160. static int id_port __initdata = 0x110; /* Start with 0x110 to avoid new sound cards.*/
  161. static struct net_device *el3_root_dev;
  162. static ushort id_read_eeprom(int index);
  163. static ushort read_eeprom(int ioaddr, int index);
  164. static int el3_open(struct net_device *dev);
  165. static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
  166. static irqreturn_t el3_interrupt(int irq, void *dev_id);
  167. static void update_stats(struct net_device *dev);
  168. static struct net_device_stats *el3_get_stats(struct net_device *dev);
  169. static int el3_rx(struct net_device *dev);
  170. static int el3_close(struct net_device *dev);
  171. static void set_multicast_list(struct net_device *dev);
  172. static void el3_tx_timeout (struct net_device *dev);
  173. static void el3_down(struct net_device *dev);
  174. static void el3_up(struct net_device *dev);
  175. static const struct ethtool_ops ethtool_ops;
  176. #ifdef EL3_SUSPEND
  177. static int el3_suspend(struct device *, pm_message_t);
  178. static int el3_resume(struct device *);
  179. #else
  180. #define el3_suspend NULL
  181. #define el3_resume NULL
  182. #endif
  183. /* generic device remove for all device types */
  184. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  185. static int el3_device_remove (struct device *device);
  186. #endif
  187. #ifdef CONFIG_NET_POLL_CONTROLLER
  188. static void el3_poll_controller(struct net_device *dev);
  189. #endif
  190. #ifdef CONFIG_EISA
  191. static struct eisa_device_id el3_eisa_ids[] = {
  192. { "TCM5092" },
  193. { "TCM5093" },
  194. { "TCM5095" },
  195. { "" }
  196. };
  197. MODULE_DEVICE_TABLE(eisa, el3_eisa_ids);
  198. static int el3_eisa_probe (struct device *device);
  199. static struct eisa_driver el3_eisa_driver = {
  200. .id_table = el3_eisa_ids,
  201. .driver = {
  202. .name = "3c509",
  203. .probe = el3_eisa_probe,
  204. .remove = __devexit_p (el3_device_remove),
  205. .suspend = el3_suspend,
  206. .resume = el3_resume,
  207. }
  208. };
  209. #endif
  210. #ifdef CONFIG_MCA
  211. static int el3_mca_probe(struct device *dev);
  212. static short el3_mca_adapter_ids[] __initdata = {
  213. 0x627c,
  214. 0x627d,
  215. 0x62db,
  216. 0x62f6,
  217. 0x62f7,
  218. 0x0000
  219. };
  220. static char *el3_mca_adapter_names[] __initdata = {
  221. "3Com 3c529 EtherLink III (10base2)",
  222. "3Com 3c529 EtherLink III (10baseT)",
  223. "3Com 3c529 EtherLink III (test mode)",
  224. "3Com 3c529 EtherLink III (TP or coax)",
  225. "3Com 3c529 EtherLink III (TP)",
  226. NULL
  227. };
  228. static struct mca_driver el3_mca_driver = {
  229. .id_table = el3_mca_adapter_ids,
  230. .driver = {
  231. .name = "3c529",
  232. .bus = &mca_bus_type,
  233. .probe = el3_mca_probe,
  234. .remove = __devexit_p(el3_device_remove),
  235. .suspend = el3_suspend,
  236. .resume = el3_resume,
  237. },
  238. };
  239. #endif /* CONFIG_MCA */
  240. #if defined(__ISAPNP__)
  241. static struct isapnp_device_id el3_isapnp_adapters[] __initdata = {
  242. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  243. ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5090),
  244. (long) "3Com Etherlink III (TP)" },
  245. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  246. ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5091),
  247. (long) "3Com Etherlink III" },
  248. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  249. ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5094),
  250. (long) "3Com Etherlink III (combo)" },
  251. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  252. ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5095),
  253. (long) "3Com Etherlink III (TPO)" },
  254. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  255. ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5098),
  256. (long) "3Com Etherlink III (TPC)" },
  257. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  258. ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f7),
  259. (long) "3Com Etherlink III compatible" },
  260. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  261. ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f8),
  262. (long) "3Com Etherlink III compatible" },
  263. { } /* terminate list */
  264. };
  265. static u16 el3_isapnp_phys_addr[8][3];
  266. static int nopnp;
  267. #endif /* __ISAPNP__ */
  268. /* With the driver model introduction for EISA devices, both init
  269. * and cleanup have been split :
  270. * - EISA devices probe/remove starts in el3_eisa_probe/el3_device_remove
  271. * - MCA/ISA still use el3_probe
  272. *
  273. * Both call el3_common_init/el3_common_remove. */
  274. static int __init el3_common_init(struct net_device *dev)
  275. {
  276. struct el3_private *lp = netdev_priv(dev);
  277. short i;
  278. int err;
  279. spin_lock_init(&lp->lock);
  280. if (dev->mem_start & 0x05) { /* xcvr codes 1/3/4/12 */
  281. dev->if_port = (dev->mem_start & 0x0f);
  282. } else { /* xcvr codes 0/8 */
  283. /* use eeprom value, but save user's full-duplex selection */
  284. dev->if_port |= (dev->mem_start & 0x08);
  285. }
  286. /* The EL3-specific entries in the device structure. */
  287. dev->open = &el3_open;
  288. dev->hard_start_xmit = &el3_start_xmit;
  289. dev->stop = &el3_close;
  290. dev->get_stats = &el3_get_stats;
  291. dev->set_multicast_list = &set_multicast_list;
  292. dev->tx_timeout = el3_tx_timeout;
  293. dev->watchdog_timeo = TX_TIMEOUT;
  294. #ifdef CONFIG_NET_POLL_CONTROLLER
  295. dev->poll_controller = el3_poll_controller;
  296. #endif
  297. SET_ETHTOOL_OPS(dev, &ethtool_ops);
  298. err = register_netdev(dev);
  299. if (err) {
  300. printk(KERN_ERR "Failed to register 3c5x9 at %#3.3lx, IRQ %d.\n",
  301. dev->base_addr, dev->irq);
  302. release_region(dev->base_addr, EL3_IO_EXTENT);
  303. return err;
  304. }
  305. {
  306. const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
  307. printk("%s: 3c5x9 found at %#3.3lx, %s port, address ",
  308. dev->name, dev->base_addr,
  309. if_names[(dev->if_port & 0x03)]);
  310. }
  311. /* Read in the station address. */
  312. for (i = 0; i < 6; i++)
  313. printk(" %2.2x", dev->dev_addr[i]);
  314. printk(", IRQ %d.\n", dev->irq);
  315. if (el3_debug > 0)
  316. printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
  317. return 0;
  318. }
  319. static void el3_common_remove (struct net_device *dev)
  320. {
  321. struct el3_private *lp = netdev_priv(dev);
  322. (void) lp; /* Keep gcc quiet... */
  323. #if defined(__ISAPNP__)
  324. if (lp->type == EL3_PNP)
  325. pnp_device_detach(to_pnp_dev(lp->dev));
  326. #endif
  327. unregister_netdev (dev);
  328. release_region(dev->base_addr, EL3_IO_EXTENT);
  329. free_netdev (dev);
  330. }
  331. static int __init el3_probe(int card_idx)
  332. {
  333. struct net_device *dev;
  334. struct el3_private *lp;
  335. short lrs_state = 0xff, i;
  336. int ioaddr, irq, if_port;
  337. u16 phys_addr[3];
  338. static int current_tag;
  339. int err = -ENODEV;
  340. #if defined(__ISAPNP__)
  341. static int pnp_cards;
  342. struct pnp_dev *idev = NULL;
  343. if (nopnp == 1)
  344. goto no_pnp;
  345. for (i=0; el3_isapnp_adapters[i].vendor != 0; i++) {
  346. int j;
  347. while ((idev = pnp_find_dev(NULL,
  348. el3_isapnp_adapters[i].vendor,
  349. el3_isapnp_adapters[i].function,
  350. idev))) {
  351. if (pnp_device_attach(idev) < 0)
  352. continue;
  353. if (pnp_activate_dev(idev) < 0) {
  354. __again:
  355. pnp_device_detach(idev);
  356. continue;
  357. }
  358. if (!pnp_port_valid(idev, 0) || !pnp_irq_valid(idev, 0))
  359. goto __again;
  360. ioaddr = pnp_port_start(idev, 0);
  361. if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509 PnP")) {
  362. pnp_device_detach(idev);
  363. return -EBUSY;
  364. }
  365. irq = pnp_irq(idev, 0);
  366. if (el3_debug > 3)
  367. printk ("ISAPnP reports %s at i/o 0x%x, irq %d\n",
  368. (char*) el3_isapnp_adapters[i].driver_data, ioaddr, irq);
  369. EL3WINDOW(0);
  370. for (j = 0; j < 3; j++)
  371. el3_isapnp_phys_addr[pnp_cards][j] =
  372. phys_addr[j] =
  373. htons(read_eeprom(ioaddr, j));
  374. if_port = read_eeprom(ioaddr, 8) >> 14;
  375. dev = alloc_etherdev(sizeof (struct el3_private));
  376. if (!dev) {
  377. release_region(ioaddr, EL3_IO_EXTENT);
  378. pnp_device_detach(idev);
  379. return -ENOMEM;
  380. }
  381. SET_MODULE_OWNER(dev);
  382. SET_NETDEV_DEV(dev, &idev->dev);
  383. pnp_cards++;
  384. netdev_boot_setup_check(dev);
  385. goto found;
  386. }
  387. }
  388. no_pnp:
  389. #endif /* __ISAPNP__ */
  390. /* Select an open I/O location at 0x1*0 to do contention select. */
  391. for ( ; id_port < 0x200; id_port += 0x10) {
  392. if (!request_region(id_port, 1, "3c509"))
  393. continue;
  394. outb(0x00, id_port);
  395. outb(0xff, id_port);
  396. if (inb(id_port) & 0x01){
  397. release_region(id_port, 1);
  398. break;
  399. } else
  400. release_region(id_port, 1);
  401. }
  402. if (id_port >= 0x200) {
  403. /* Rare -- do we really need a warning? */
  404. printk(" WARNING: No I/O port available for 3c509 activation.\n");
  405. return -ENODEV;
  406. }
  407. /* Next check for all ISA bus boards by sending the ID sequence to the
  408. ID_PORT. We find cards past the first by setting the 'current_tag'
  409. on cards as they are found. Cards with their tag set will not
  410. respond to subsequent ID sequences. */
  411. outb(0x00, id_port);
  412. outb(0x00, id_port);
  413. for(i = 0; i < 255; i++) {
  414. outb(lrs_state, id_port);
  415. lrs_state <<= 1;
  416. lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
  417. }
  418. /* For the first probe, clear all board's tag registers. */
  419. if (current_tag == 0)
  420. outb(0xd0, id_port);
  421. else /* Otherwise kill off already-found boards. */
  422. outb(0xd8, id_port);
  423. if (id_read_eeprom(7) != 0x6d50) {
  424. return -ENODEV;
  425. }
  426. /* Read in EEPROM data, which does contention-select.
  427. Only the lowest address board will stay "on-line".
  428. 3Com got the byte order backwards. */
  429. for (i = 0; i < 3; i++) {
  430. phys_addr[i] = htons(id_read_eeprom(i));
  431. }
  432. #if defined(__ISAPNP__)
  433. if (nopnp == 0) {
  434. /* The ISA PnP 3c509 cards respond to the ID sequence.
  435. This check is needed in order not to register them twice. */
  436. for (i = 0; i < pnp_cards; i++) {
  437. if (phys_addr[0] == el3_isapnp_phys_addr[i][0] &&
  438. phys_addr[1] == el3_isapnp_phys_addr[i][1] &&
  439. phys_addr[2] == el3_isapnp_phys_addr[i][2])
  440. {
  441. if (el3_debug > 3)
  442. printk("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
  443. phys_addr[0] & 0xff, phys_addr[0] >> 8,
  444. phys_addr[1] & 0xff, phys_addr[1] >> 8,
  445. phys_addr[2] & 0xff, phys_addr[2] >> 8);
  446. /* Set the adaptor tag so that the next card can be found. */
  447. outb(0xd0 + ++current_tag, id_port);
  448. goto no_pnp;
  449. }
  450. }
  451. }
  452. #endif /* __ISAPNP__ */
  453. {
  454. unsigned int iobase = id_read_eeprom(8);
  455. if_port = iobase >> 14;
  456. ioaddr = 0x200 + ((iobase & 0x1f) << 4);
  457. }
  458. irq = id_read_eeprom(9) >> 12;
  459. dev = alloc_etherdev(sizeof (struct el3_private));
  460. if (!dev)
  461. return -ENOMEM;
  462. SET_MODULE_OWNER(dev);
  463. netdev_boot_setup_check(dev);
  464. /* Set passed-in IRQ or I/O Addr. */
  465. if (dev->irq > 1 && dev->irq < 16)
  466. irq = dev->irq;
  467. if (dev->base_addr) {
  468. if (dev->mem_end == 0x3c509 /* Magic key */
  469. && dev->base_addr >= 0x200 && dev->base_addr <= 0x3e0)
  470. ioaddr = dev->base_addr & 0x3f0;
  471. else if (dev->base_addr != ioaddr)
  472. goto out;
  473. }
  474. if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509")) {
  475. err = -EBUSY;
  476. goto out;
  477. }
  478. /* Set the adaptor tag so that the next card can be found. */
  479. outb(0xd0 + ++current_tag, id_port);
  480. /* Activate the adaptor at the EEPROM location. */
  481. outb((ioaddr >> 4) | 0xe0, id_port);
  482. EL3WINDOW(0);
  483. if (inw(ioaddr) != 0x6d50)
  484. goto out1;
  485. /* Free the interrupt so that some other card can use it. */
  486. outw(0x0f00, ioaddr + WN0_IRQ);
  487. #if defined(__ISAPNP__)
  488. found: /* PNP jumps here... */
  489. #endif /* __ISAPNP__ */
  490. memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
  491. dev->base_addr = ioaddr;
  492. dev->irq = irq;
  493. dev->if_port = if_port;
  494. lp = netdev_priv(dev);
  495. #if defined(__ISAPNP__)
  496. lp->dev = &idev->dev;
  497. #endif
  498. err = el3_common_init(dev);
  499. if (err)
  500. goto out1;
  501. el3_cards++;
  502. lp->next_dev = el3_root_dev;
  503. el3_root_dev = dev;
  504. return 0;
  505. out1:
  506. #if defined(__ISAPNP__)
  507. if (idev)
  508. pnp_device_detach(idev);
  509. #endif
  510. out:
  511. free_netdev(dev);
  512. return err;
  513. }
  514. #ifdef CONFIG_MCA
  515. static int __init el3_mca_probe(struct device *device)
  516. {
  517. /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch,
  518. * heavily modified by Chris Beauregard
  519. * (cpbeaure@csclub.uwaterloo.ca) to support standard MCA
  520. * probing.
  521. *
  522. * redone for multi-card detection by ZP Gu (zpg@castle.net)
  523. * now works as a module */
  524. struct el3_private *lp;
  525. short i;
  526. int ioaddr, irq, if_port;
  527. u16 phys_addr[3];
  528. struct net_device *dev = NULL;
  529. u_char pos4, pos5;
  530. struct mca_device *mdev = to_mca_device(device);
  531. int slot = mdev->slot;
  532. int err;
  533. pos4 = mca_device_read_stored_pos(mdev, 4);
  534. pos5 = mca_device_read_stored_pos(mdev, 5);
  535. ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
  536. irq = pos5 & 0x0f;
  537. printk("3c529: found %s at slot %d\n",
  538. el3_mca_adapter_names[mdev->index], slot + 1);
  539. /* claim the slot */
  540. strncpy(mdev->name, el3_mca_adapter_names[mdev->index],
  541. sizeof(mdev->name));
  542. mca_device_set_claim(mdev, 1);
  543. if_port = pos4 & 0x03;
  544. irq = mca_device_transform_irq(mdev, irq);
  545. ioaddr = mca_device_transform_ioport(mdev, ioaddr);
  546. if (el3_debug > 2) {
  547. printk("3c529: irq %d ioaddr 0x%x ifport %d\n", irq, ioaddr, if_port);
  548. }
  549. EL3WINDOW(0);
  550. for (i = 0; i < 3; i++) {
  551. phys_addr[i] = htons(read_eeprom(ioaddr, i));
  552. }
  553. dev = alloc_etherdev(sizeof (struct el3_private));
  554. if (dev == NULL) {
  555. release_region(ioaddr, EL3_IO_EXTENT);
  556. return -ENOMEM;
  557. }
  558. SET_MODULE_OWNER(dev);
  559. netdev_boot_setup_check(dev);
  560. memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
  561. dev->base_addr = ioaddr;
  562. dev->irq = irq;
  563. dev->if_port = if_port;
  564. lp = netdev_priv(dev);
  565. lp->dev = device;
  566. lp->type = EL3_MCA;
  567. device->driver_data = dev;
  568. err = el3_common_init(dev);
  569. if (err) {
  570. device->driver_data = NULL;
  571. free_netdev(dev);
  572. return -ENOMEM;
  573. }
  574. el3_cards++;
  575. return 0;
  576. }
  577. #endif /* CONFIG_MCA */
  578. #ifdef CONFIG_EISA
  579. static int __init el3_eisa_probe (struct device *device)
  580. {
  581. struct el3_private *lp;
  582. short i;
  583. int ioaddr, irq, if_port;
  584. u16 phys_addr[3];
  585. struct net_device *dev = NULL;
  586. struct eisa_device *edev;
  587. int err;
  588. /* Yeepee, The driver framework is calling us ! */
  589. edev = to_eisa_device (device);
  590. ioaddr = edev->base_addr;
  591. if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509"))
  592. return -EBUSY;
  593. /* Change the register set to the configuration window 0. */
  594. outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
  595. irq = inw(ioaddr + WN0_IRQ) >> 12;
  596. if_port = inw(ioaddr + 6)>>14;
  597. for (i = 0; i < 3; i++)
  598. phys_addr[i] = htons(read_eeprom(ioaddr, i));
  599. /* Restore the "Product ID" to the EEPROM read register. */
  600. read_eeprom(ioaddr, 3);
  601. dev = alloc_etherdev(sizeof (struct el3_private));
  602. if (dev == NULL) {
  603. release_region(ioaddr, EL3_IO_EXTENT);
  604. return -ENOMEM;
  605. }
  606. SET_MODULE_OWNER(dev);
  607. netdev_boot_setup_check(dev);
  608. memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
  609. dev->base_addr = ioaddr;
  610. dev->irq = irq;
  611. dev->if_port = if_port;
  612. lp = netdev_priv(dev);
  613. lp->dev = device;
  614. lp->type = EL3_EISA;
  615. eisa_set_drvdata (edev, dev);
  616. err = el3_common_init(dev);
  617. if (err) {
  618. eisa_set_drvdata (edev, NULL);
  619. free_netdev(dev);
  620. return err;
  621. }
  622. el3_cards++;
  623. return 0;
  624. }
  625. #endif
  626. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  627. /* This remove works for all device types.
  628. *
  629. * The net dev must be stored in the driver_data field */
  630. static int __devexit el3_device_remove (struct device *device)
  631. {
  632. struct net_device *dev;
  633. dev = device->driver_data;
  634. el3_common_remove (dev);
  635. return 0;
  636. }
  637. #endif
  638. /* Read a word from the EEPROM using the regular EEPROM access register.
  639. Assume that we are in register window zero.
  640. */
  641. static ushort read_eeprom(int ioaddr, int index)
  642. {
  643. outw(EEPROM_READ + index, ioaddr + 10);
  644. /* Pause for at least 162 us. for the read to take place.
  645. Some chips seem to require much longer */
  646. mdelay(2);
  647. return inw(ioaddr + 12);
  648. }
  649. /* Read a word from the EEPROM when in the ISA ID probe state. */
  650. static ushort __init id_read_eeprom(int index)
  651. {
  652. int bit, word = 0;
  653. /* Issue read command, and pause for at least 162 us. for it to complete.
  654. Assume extra-fast 16Mhz bus. */
  655. outb(EEPROM_READ + index, id_port);
  656. /* Pause for at least 162 us. for the read to take place. */
  657. /* Some chips seem to require much longer */
  658. mdelay(4);
  659. for (bit = 15; bit >= 0; bit--)
  660. word = (word << 1) + (inb(id_port) & 0x01);
  661. if (el3_debug > 3)
  662. printk(" 3c509 EEPROM word %d %#4.4x.\n", index, word);
  663. return word;
  664. }
  665. static int
  666. el3_open(struct net_device *dev)
  667. {
  668. int ioaddr = dev->base_addr;
  669. int i;
  670. outw(TxReset, ioaddr + EL3_CMD);
  671. outw(RxReset, ioaddr + EL3_CMD);
  672. outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
  673. i = request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev);
  674. if (i)
  675. return i;
  676. EL3WINDOW(0);
  677. if (el3_debug > 3)
  678. printk("%s: Opening, IRQ %d status@%x %4.4x.\n", dev->name,
  679. dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
  680. el3_up(dev);
  681. if (el3_debug > 3)
  682. printk("%s: Opened 3c509 IRQ %d status %4.4x.\n",
  683. dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
  684. return 0;
  685. }
  686. static void
  687. el3_tx_timeout (struct net_device *dev)
  688. {
  689. struct el3_private *lp = netdev_priv(dev);
  690. int ioaddr = dev->base_addr;
  691. /* Transmitter timeout, serious problems. */
  692. printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
  693. "Tx FIFO room %d.\n",
  694. dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
  695. inw(ioaddr + TX_FREE));
  696. lp->stats.tx_errors++;
  697. dev->trans_start = jiffies;
  698. /* Issue TX_RESET and TX_START commands. */
  699. outw(TxReset, ioaddr + EL3_CMD);
  700. outw(TxEnable, ioaddr + EL3_CMD);
  701. netif_wake_queue(dev);
  702. }
  703. static int
  704. el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
  705. {
  706. struct el3_private *lp = netdev_priv(dev);
  707. int ioaddr = dev->base_addr;
  708. unsigned long flags;
  709. netif_stop_queue (dev);
  710. lp->stats.tx_bytes += skb->len;
  711. if (el3_debug > 4) {
  712. printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
  713. dev->name, skb->len, inw(ioaddr + EL3_STATUS));
  714. }
  715. #if 0
  716. #ifndef final_version
  717. { /* Error-checking code, delete someday. */
  718. ushort status = inw(ioaddr + EL3_STATUS);
  719. if (status & 0x0001 /* IRQ line active, missed one. */
  720. && inw(ioaddr + EL3_STATUS) & 1) { /* Make sure. */
  721. printk("%s: Missed interrupt, status then %04x now %04x"
  722. " Tx %2.2x Rx %4.4x.\n", dev->name, status,
  723. inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
  724. inw(ioaddr + RX_STATUS));
  725. /* Fake interrupt trigger by masking, acknowledge interrupts. */
  726. outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
  727. outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
  728. ioaddr + EL3_CMD);
  729. outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
  730. }
  731. }
  732. #endif
  733. #endif
  734. /*
  735. * We lock the driver against other processors. Note
  736. * we don't need to lock versus the IRQ as we suspended
  737. * that. This means that we lose the ability to take
  738. * an RX during a TX upload. That sucks a bit with SMP
  739. * on an original 3c509 (2K buffer)
  740. *
  741. * Using disable_irq stops us crapping on other
  742. * time sensitive devices.
  743. */
  744. spin_lock_irqsave(&lp->lock, flags);
  745. /* Put out the doubleword header... */
  746. outw(skb->len, ioaddr + TX_FIFO);
  747. outw(0x00, ioaddr + TX_FIFO);
  748. /* ... and the packet rounded to a doubleword. */
  749. outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
  750. dev->trans_start = jiffies;
  751. if (inw(ioaddr + TX_FREE) > 1536)
  752. netif_start_queue(dev);
  753. else
  754. /* Interrupt us when the FIFO has room for max-sized packet. */
  755. outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
  756. spin_unlock_irqrestore(&lp->lock, flags);
  757. dev_kfree_skb (skb);
  758. /* Clear the Tx status stack. */
  759. {
  760. short tx_status;
  761. int i = 4;
  762. while (--i > 0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
  763. if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
  764. if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
  765. if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
  766. outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
  767. }
  768. }
  769. return 0;
  770. }
  771. /* The EL3 interrupt handler. */
  772. static irqreturn_t
  773. el3_interrupt(int irq, void *dev_id)
  774. {
  775. struct net_device *dev = dev_id;
  776. struct el3_private *lp;
  777. int ioaddr, status;
  778. int i = max_interrupt_work;
  779. lp = netdev_priv(dev);
  780. spin_lock(&lp->lock);
  781. ioaddr = dev->base_addr;
  782. if (el3_debug > 4) {
  783. status = inw(ioaddr + EL3_STATUS);
  784. printk("%s: interrupt, status %4.4x.\n", dev->name, status);
  785. }
  786. while ((status = inw(ioaddr + EL3_STATUS)) &
  787. (IntLatch | RxComplete | StatsFull)) {
  788. if (status & RxComplete)
  789. el3_rx(dev);
  790. if (status & TxAvailable) {
  791. if (el3_debug > 5)
  792. printk(" TX room bit was handled.\n");
  793. /* There's room in the FIFO for a full-sized packet. */
  794. outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
  795. netif_wake_queue (dev);
  796. }
  797. if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
  798. /* Handle all uncommon interrupts. */
  799. if (status & StatsFull) /* Empty statistics. */
  800. update_stats(dev);
  801. if (status & RxEarly) { /* Rx early is unused. */
  802. el3_rx(dev);
  803. outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
  804. }
  805. if (status & TxComplete) { /* Really Tx error. */
  806. struct el3_private *lp = netdev_priv(dev);
  807. short tx_status;
  808. int i = 4;
  809. while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
  810. if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
  811. if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
  812. if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
  813. outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
  814. }
  815. }
  816. if (status & AdapterFailure) {
  817. /* Adapter failure requires Rx reset and reinit. */
  818. outw(RxReset, ioaddr + EL3_CMD);
  819. /* Set the Rx filter to the current state. */
  820. outw(SetRxFilter | RxStation | RxBroadcast
  821. | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
  822. | (dev->flags & IFF_PROMISC ? RxProm : 0),
  823. ioaddr + EL3_CMD);
  824. outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
  825. outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
  826. }
  827. }
  828. if (--i < 0) {
  829. printk("%s: Infinite loop in interrupt, status %4.4x.\n",
  830. dev->name, status);
  831. /* Clear all interrupts. */
  832. outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
  833. break;
  834. }
  835. /* Acknowledge the IRQ. */
  836. outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
  837. }
  838. if (el3_debug > 4) {
  839. printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
  840. inw(ioaddr + EL3_STATUS));
  841. }
  842. spin_unlock(&lp->lock);
  843. return IRQ_HANDLED;
  844. }
  845. #ifdef CONFIG_NET_POLL_CONTROLLER
  846. /*
  847. * Polling receive - used by netconsole and other diagnostic tools
  848. * to allow network i/o with interrupts disabled.
  849. */
  850. static void el3_poll_controller(struct net_device *dev)
  851. {
  852. disable_irq(dev->irq);
  853. el3_interrupt(dev->irq, dev);
  854. enable_irq(dev->irq);
  855. }
  856. #endif
  857. static struct net_device_stats *
  858. el3_get_stats(struct net_device *dev)
  859. {
  860. struct el3_private *lp = netdev_priv(dev);
  861. unsigned long flags;
  862. /*
  863. * This is fast enough not to bother with disable IRQ
  864. * stuff.
  865. */
  866. spin_lock_irqsave(&lp->lock, flags);
  867. update_stats(dev);
  868. spin_unlock_irqrestore(&lp->lock, flags);
  869. return &lp->stats;
  870. }
  871. /* Update statistics. We change to register window 6, so this should be run
  872. single-threaded if the device is active. This is expected to be a rare
  873. operation, and it's simpler for the rest of the driver to assume that
  874. window 1 is always valid rather than use a special window-state variable.
  875. */
  876. static void update_stats(struct net_device *dev)
  877. {
  878. struct el3_private *lp = netdev_priv(dev);
  879. int ioaddr = dev->base_addr;
  880. if (el3_debug > 5)
  881. printk(" Updating the statistics.\n");
  882. /* Turn off statistics updates while reading. */
  883. outw(StatsDisable, ioaddr + EL3_CMD);
  884. /* Switch to the stats window, and read everything. */
  885. EL3WINDOW(6);
  886. lp->stats.tx_carrier_errors += inb(ioaddr + 0);
  887. lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
  888. /* Multiple collisions. */ inb(ioaddr + 2);
  889. lp->stats.collisions += inb(ioaddr + 3);
  890. lp->stats.tx_window_errors += inb(ioaddr + 4);
  891. lp->stats.rx_fifo_errors += inb(ioaddr + 5);
  892. lp->stats.tx_packets += inb(ioaddr + 6);
  893. /* Rx packets */ inb(ioaddr + 7);
  894. /* Tx deferrals */ inb(ioaddr + 8);
  895. inw(ioaddr + 10); /* Total Rx and Tx octets. */
  896. inw(ioaddr + 12);
  897. /* Back to window 1, and turn statistics back on. */
  898. EL3WINDOW(1);
  899. outw(StatsEnable, ioaddr + EL3_CMD);
  900. return;
  901. }
  902. static int
  903. el3_rx(struct net_device *dev)
  904. {
  905. struct el3_private *lp = netdev_priv(dev);
  906. int ioaddr = dev->base_addr;
  907. short rx_status;
  908. if (el3_debug > 5)
  909. printk(" In rx_packet(), status %4.4x, rx_status %4.4x.\n",
  910. inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
  911. while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
  912. if (rx_status & 0x4000) { /* Error, update stats. */
  913. short error = rx_status & 0x3800;
  914. outw(RxDiscard, ioaddr + EL3_CMD);
  915. lp->stats.rx_errors++;
  916. switch (error) {
  917. case 0x0000: lp->stats.rx_over_errors++; break;
  918. case 0x0800: lp->stats.rx_length_errors++; break;
  919. case 0x1000: lp->stats.rx_frame_errors++; break;
  920. case 0x1800: lp->stats.rx_length_errors++; break;
  921. case 0x2000: lp->stats.rx_frame_errors++; break;
  922. case 0x2800: lp->stats.rx_crc_errors++; break;
  923. }
  924. } else {
  925. short pkt_len = rx_status & 0x7ff;
  926. struct sk_buff *skb;
  927. skb = dev_alloc_skb(pkt_len+5);
  928. lp->stats.rx_bytes += pkt_len;
  929. if (el3_debug > 4)
  930. printk("Receiving packet size %d status %4.4x.\n",
  931. pkt_len, rx_status);
  932. if (skb != NULL) {
  933. skb->dev = dev;
  934. skb_reserve(skb, 2); /* Align IP on 16 byte */
  935. /* 'skb->data' points to the start of sk_buff data area. */
  936. insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
  937. (pkt_len + 3) >> 2);
  938. outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
  939. skb->protocol = eth_type_trans(skb,dev);
  940. netif_rx(skb);
  941. dev->last_rx = jiffies;
  942. lp->stats.rx_packets++;
  943. continue;
  944. }
  945. outw(RxDiscard, ioaddr + EL3_CMD);
  946. lp->stats.rx_dropped++;
  947. if (el3_debug)
  948. printk("%s: Couldn't allocate a sk_buff of size %d.\n",
  949. dev->name, pkt_len);
  950. }
  951. inw(ioaddr + EL3_STATUS); /* Delay. */
  952. while (inw(ioaddr + EL3_STATUS) & 0x1000)
  953. printk(KERN_DEBUG " Waiting for 3c509 to discard packet, status %x.\n",
  954. inw(ioaddr + EL3_STATUS) );
  955. }
  956. return 0;
  957. }
  958. /*
  959. * Set or clear the multicast filter for this adaptor.
  960. */
  961. static void
  962. set_multicast_list(struct net_device *dev)
  963. {
  964. unsigned long flags;
  965. struct el3_private *lp = netdev_priv(dev);
  966. int ioaddr = dev->base_addr;
  967. if (el3_debug > 1) {
  968. static int old;
  969. if (old != dev->mc_count) {
  970. old = dev->mc_count;
  971. printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
  972. }
  973. }
  974. spin_lock_irqsave(&lp->lock, flags);
  975. if (dev->flags&IFF_PROMISC) {
  976. outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
  977. ioaddr + EL3_CMD);
  978. }
  979. else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
  980. outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
  981. }
  982. else
  983. outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
  984. spin_unlock_irqrestore(&lp->lock, flags);
  985. }
  986. static int
  987. el3_close(struct net_device *dev)
  988. {
  989. int ioaddr = dev->base_addr;
  990. struct el3_private *lp = netdev_priv(dev);
  991. if (el3_debug > 2)
  992. printk("%s: Shutting down ethercard.\n", dev->name);
  993. el3_down(dev);
  994. free_irq(dev->irq, dev);
  995. /* Switching back to window 0 disables the IRQ. */
  996. EL3WINDOW(0);
  997. if (lp->type != EL3_EISA) {
  998. /* But we explicitly zero the IRQ line select anyway. Don't do
  999. * it on EISA cards, it prevents the module from getting an
  1000. * IRQ after unload+reload... */
  1001. outw(0x0f00, ioaddr + WN0_IRQ);
  1002. }
  1003. return 0;
  1004. }
  1005. static int
  1006. el3_link_ok(struct net_device *dev)
  1007. {
  1008. int ioaddr = dev->base_addr;
  1009. u16 tmp;
  1010. EL3WINDOW(4);
  1011. tmp = inw(ioaddr + WN4_MEDIA);
  1012. EL3WINDOW(1);
  1013. return tmp & (1<<11);
  1014. }
  1015. static int
  1016. el3_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
  1017. {
  1018. u16 tmp;
  1019. int ioaddr = dev->base_addr;
  1020. EL3WINDOW(0);
  1021. /* obtain current transceiver via WN4_MEDIA? */
  1022. tmp = inw(ioaddr + WN0_ADDR_CONF);
  1023. ecmd->transceiver = XCVR_INTERNAL;
  1024. switch (tmp >> 14) {
  1025. case 0:
  1026. ecmd->port = PORT_TP;
  1027. break;
  1028. case 1:
  1029. ecmd->port = PORT_AUI;
  1030. ecmd->transceiver = XCVR_EXTERNAL;
  1031. break;
  1032. case 3:
  1033. ecmd->port = PORT_BNC;
  1034. default:
  1035. break;
  1036. }
  1037. ecmd->duplex = DUPLEX_HALF;
  1038. ecmd->supported = 0;
  1039. tmp = inw(ioaddr + WN0_CONF_CTRL);
  1040. if (tmp & (1<<13))
  1041. ecmd->supported |= SUPPORTED_AUI;
  1042. if (tmp & (1<<12))
  1043. ecmd->supported |= SUPPORTED_BNC;
  1044. if (tmp & (1<<9)) {
  1045. ecmd->supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half |
  1046. SUPPORTED_10baseT_Full; /* hmm... */
  1047. EL3WINDOW(4);
  1048. tmp = inw(ioaddr + WN4_NETDIAG);
  1049. if (tmp & FD_ENABLE)
  1050. ecmd->duplex = DUPLEX_FULL;
  1051. }
  1052. ecmd->speed = SPEED_10;
  1053. EL3WINDOW(1);
  1054. return 0;
  1055. }
  1056. static int
  1057. el3_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
  1058. {
  1059. u16 tmp;
  1060. int ioaddr = dev->base_addr;
  1061. if (ecmd->speed != SPEED_10)
  1062. return -EINVAL;
  1063. if ((ecmd->duplex != DUPLEX_HALF) && (ecmd->duplex != DUPLEX_FULL))
  1064. return -EINVAL;
  1065. if ((ecmd->transceiver != XCVR_INTERNAL) && (ecmd->transceiver != XCVR_EXTERNAL))
  1066. return -EINVAL;
  1067. /* change XCVR type */
  1068. EL3WINDOW(0);
  1069. tmp = inw(ioaddr + WN0_ADDR_CONF);
  1070. switch (ecmd->port) {
  1071. case PORT_TP:
  1072. tmp &= ~(3<<14);
  1073. dev->if_port = 0;
  1074. break;
  1075. case PORT_AUI:
  1076. tmp |= (1<<14);
  1077. dev->if_port = 1;
  1078. break;
  1079. case PORT_BNC:
  1080. tmp |= (3<<14);
  1081. dev->if_port = 3;
  1082. break;
  1083. default:
  1084. return -EINVAL;
  1085. }
  1086. outw(tmp, ioaddr + WN0_ADDR_CONF);
  1087. if (dev->if_port == 3) {
  1088. /* fire up the DC-DC convertor if BNC gets enabled */
  1089. tmp = inw(ioaddr + WN0_ADDR_CONF);
  1090. if (tmp & (3 << 14)) {
  1091. outw(StartCoax, ioaddr + EL3_CMD);
  1092. udelay(800);
  1093. } else
  1094. return -EIO;
  1095. }
  1096. EL3WINDOW(4);
  1097. tmp = inw(ioaddr + WN4_NETDIAG);
  1098. if (ecmd->duplex == DUPLEX_FULL)
  1099. tmp |= FD_ENABLE;
  1100. else
  1101. tmp &= ~FD_ENABLE;
  1102. outw(tmp, ioaddr + WN4_NETDIAG);
  1103. EL3WINDOW(1);
  1104. return 0;
  1105. }
  1106. static void el3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1107. {
  1108. strcpy(info->driver, DRV_NAME);
  1109. strcpy(info->version, DRV_VERSION);
  1110. }
  1111. static int el3_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1112. {
  1113. struct el3_private *lp = netdev_priv(dev);
  1114. int ret;
  1115. spin_lock_irq(&lp->lock);
  1116. ret = el3_netdev_get_ecmd(dev, ecmd);
  1117. spin_unlock_irq(&lp->lock);
  1118. return ret;
  1119. }
  1120. static int el3_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1121. {
  1122. struct el3_private *lp = netdev_priv(dev);
  1123. int ret;
  1124. spin_lock_irq(&lp->lock);
  1125. ret = el3_netdev_set_ecmd(dev, ecmd);
  1126. spin_unlock_irq(&lp->lock);
  1127. return ret;
  1128. }
  1129. static u32 el3_get_link(struct net_device *dev)
  1130. {
  1131. struct el3_private *lp = netdev_priv(dev);
  1132. u32 ret;
  1133. spin_lock_irq(&lp->lock);
  1134. ret = el3_link_ok(dev);
  1135. spin_unlock_irq(&lp->lock);
  1136. return ret;
  1137. }
  1138. static u32 el3_get_msglevel(struct net_device *dev)
  1139. {
  1140. return el3_debug;
  1141. }
  1142. static void el3_set_msglevel(struct net_device *dev, u32 v)
  1143. {
  1144. el3_debug = v;
  1145. }
  1146. static const struct ethtool_ops ethtool_ops = {
  1147. .get_drvinfo = el3_get_drvinfo,
  1148. .get_settings = el3_get_settings,
  1149. .set_settings = el3_set_settings,
  1150. .get_link = el3_get_link,
  1151. .get_msglevel = el3_get_msglevel,
  1152. .set_msglevel = el3_set_msglevel,
  1153. };
  1154. static void
  1155. el3_down(struct net_device *dev)
  1156. {
  1157. int ioaddr = dev->base_addr;
  1158. netif_stop_queue(dev);
  1159. /* Turn off statistics ASAP. We update lp->stats below. */
  1160. outw(StatsDisable, ioaddr + EL3_CMD);
  1161. /* Disable the receiver and transmitter. */
  1162. outw(RxDisable, ioaddr + EL3_CMD);
  1163. outw(TxDisable, ioaddr + EL3_CMD);
  1164. if (dev->if_port == 3)
  1165. /* Turn off thinnet power. Green! */
  1166. outw(StopCoax, ioaddr + EL3_CMD);
  1167. else if (dev->if_port == 0) {
  1168. /* Disable link beat and jabber, if_port may change here next open(). */
  1169. EL3WINDOW(4);
  1170. outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
  1171. }
  1172. outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
  1173. update_stats(dev);
  1174. }
  1175. static void
  1176. el3_up(struct net_device *dev)
  1177. {
  1178. int i, sw_info, net_diag;
  1179. int ioaddr = dev->base_addr;
  1180. /* Activating the board required and does no harm otherwise */
  1181. outw(0x0001, ioaddr + 4);
  1182. /* Set the IRQ line. */
  1183. outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
  1184. /* Set the station address in window 2 each time opened. */
  1185. EL3WINDOW(2);
  1186. for (i = 0; i < 6; i++)
  1187. outb(dev->dev_addr[i], ioaddr + i);
  1188. if ((dev->if_port & 0x03) == 3) /* BNC interface */
  1189. /* Start the thinnet transceiver. We should really wait 50ms...*/
  1190. outw(StartCoax, ioaddr + EL3_CMD);
  1191. else if ((dev->if_port & 0x03) == 0) { /* 10baseT interface */
  1192. /* Combine secondary sw_info word (the adapter level) and primary
  1193. sw_info word (duplex setting plus other useless bits) */
  1194. EL3WINDOW(0);
  1195. sw_info = (read_eeprom(ioaddr, 0x14) & 0x400f) |
  1196. (read_eeprom(ioaddr, 0x0d) & 0xBff0);
  1197. EL3WINDOW(4);
  1198. net_diag = inw(ioaddr + WN4_NETDIAG);
  1199. net_diag = (net_diag | FD_ENABLE); /* temporarily assume full-duplex will be set */
  1200. printk("%s: ", dev->name);
  1201. switch (dev->if_port & 0x0c) {
  1202. case 12:
  1203. /* force full-duplex mode if 3c5x9b */
  1204. if (sw_info & 0x000f) {
  1205. printk("Forcing 3c5x9b full-duplex mode");
  1206. break;
  1207. }
  1208. case 8:
  1209. /* set full-duplex mode based on eeprom config setting */
  1210. if ((sw_info & 0x000f) && (sw_info & 0x8000)) {
  1211. printk("Setting 3c5x9b full-duplex mode (from EEPROM configuration bit)");
  1212. break;
  1213. }
  1214. default:
  1215. /* xcvr=(0 || 4) OR user has an old 3c5x9 non "B" model */
  1216. printk("Setting 3c5x9/3c5x9B half-duplex mode");
  1217. net_diag = (net_diag & ~FD_ENABLE); /* disable full duplex */
  1218. }
  1219. outw(net_diag, ioaddr + WN4_NETDIAG);
  1220. printk(" if_port: %d, sw_info: %4.4x\n", dev->if_port, sw_info);
  1221. if (el3_debug > 3)
  1222. printk("%s: 3c5x9 net diag word is now: %4.4x.\n", dev->name, net_diag);
  1223. /* Enable link beat and jabber check. */
  1224. outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
  1225. }
  1226. /* Switch to the stats window, and clear all stats by reading. */
  1227. outw(StatsDisable, ioaddr + EL3_CMD);
  1228. EL3WINDOW(6);
  1229. for (i = 0; i < 9; i++)
  1230. inb(ioaddr + i);
  1231. inw(ioaddr + 10);
  1232. inw(ioaddr + 12);
  1233. /* Switch to register set 1 for normal use. */
  1234. EL3WINDOW(1);
  1235. /* Accept b-case and phys addr only. */
  1236. outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
  1237. outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
  1238. outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
  1239. outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
  1240. /* Allow status bits to be seen. */
  1241. outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
  1242. /* Ack all pending events, and set active indicator mask. */
  1243. outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
  1244. ioaddr + EL3_CMD);
  1245. outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
  1246. ioaddr + EL3_CMD);
  1247. netif_start_queue(dev);
  1248. }
  1249. /* Power Management support functions */
  1250. #ifdef EL3_SUSPEND
  1251. static int
  1252. el3_suspend(struct device *pdev, pm_message_t state)
  1253. {
  1254. unsigned long flags;
  1255. struct net_device *dev;
  1256. struct el3_private *lp;
  1257. int ioaddr;
  1258. dev = pdev->driver_data;
  1259. lp = netdev_priv(dev);
  1260. ioaddr = dev->base_addr;
  1261. spin_lock_irqsave(&lp->lock, flags);
  1262. if (netif_running(dev))
  1263. netif_device_detach(dev);
  1264. el3_down(dev);
  1265. outw(PowerDown, ioaddr + EL3_CMD);
  1266. spin_unlock_irqrestore(&lp->lock, flags);
  1267. return 0;
  1268. }
  1269. static int
  1270. el3_resume(struct device *pdev)
  1271. {
  1272. unsigned long flags;
  1273. struct net_device *dev;
  1274. struct el3_private *lp;
  1275. int ioaddr;
  1276. dev = pdev->driver_data;
  1277. lp = netdev_priv(dev);
  1278. ioaddr = dev->base_addr;
  1279. spin_lock_irqsave(&lp->lock, flags);
  1280. outw(PowerUp, ioaddr + EL3_CMD);
  1281. el3_up(dev);
  1282. if (netif_running(dev))
  1283. netif_device_attach(dev);
  1284. spin_unlock_irqrestore(&lp->lock, flags);
  1285. return 0;
  1286. }
  1287. #endif /* EL3_SUSPEND */
  1288. /* Parameters that may be passed into the module. */
  1289. static int debug = -1;
  1290. static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
  1291. static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  1292. module_param(debug,int, 0);
  1293. module_param_array(irq, int, NULL, 0);
  1294. module_param_array(xcvr, int, NULL, 0);
  1295. module_param(max_interrupt_work, int, 0);
  1296. MODULE_PARM_DESC(debug, "debug level (0-6)");
  1297. MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
  1298. MODULE_PARM_DESC(xcvr,"transceiver(s) (0=internal, 1=external)");
  1299. MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
  1300. #if defined(__ISAPNP__)
  1301. module_param(nopnp, int, 0);
  1302. MODULE_PARM_DESC(nopnp, "disable ISA PnP support (0-1)");
  1303. MODULE_DEVICE_TABLE(isapnp, el3_isapnp_adapters);
  1304. #endif /* __ISAPNP__ */
  1305. MODULE_DESCRIPTION("3Com Etherlink III (3c509, 3c509B) ISA/PnP ethernet driver");
  1306. MODULE_LICENSE("GPL");
  1307. static int __init el3_init_module(void)
  1308. {
  1309. int ret = 0;
  1310. el3_cards = 0;
  1311. if (debug >= 0)
  1312. el3_debug = debug;
  1313. el3_root_dev = NULL;
  1314. while (el3_probe(el3_cards) == 0) {
  1315. if (irq[el3_cards] > 1)
  1316. el3_root_dev->irq = irq[el3_cards];
  1317. if (xcvr[el3_cards] >= 0)
  1318. el3_root_dev->if_port = xcvr[el3_cards];
  1319. el3_cards++;
  1320. }
  1321. #ifdef CONFIG_EISA
  1322. ret = eisa_driver_register(&el3_eisa_driver);
  1323. #endif
  1324. #ifdef CONFIG_MCA
  1325. {
  1326. int err = mca_register_driver(&el3_mca_driver);
  1327. if (ret == 0)
  1328. ret = err;
  1329. }
  1330. #endif
  1331. return ret;
  1332. }
  1333. static void __exit el3_cleanup_module(void)
  1334. {
  1335. struct net_device *next_dev;
  1336. while (el3_root_dev) {
  1337. struct el3_private *lp = netdev_priv(el3_root_dev);
  1338. next_dev = lp->next_dev;
  1339. el3_common_remove (el3_root_dev);
  1340. el3_root_dev = next_dev;
  1341. }
  1342. #ifdef CONFIG_EISA
  1343. eisa_driver_unregister (&el3_eisa_driver);
  1344. #endif
  1345. #ifdef CONFIG_MCA
  1346. mca_unregister_driver(&el3_mca_driver);
  1347. #endif
  1348. }
  1349. module_init (el3_init_module);
  1350. module_exit (el3_cleanup_module);