3c501.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /* 3c501.c: A 3Com 3c501 Ethernet driver for Linux. */
  2. /*
  3. Written 1992,1993,1994 Donald Becker
  4. Copyright 1993 United States Government as represented by the
  5. Director, National Security Agency. This software may be used and
  6. distributed according to the terms of the GNU General Public License,
  7. incorporated herein by reference.
  8. This is a device driver for the 3Com Etherlink 3c501.
  9. Do not purchase this card, even as a joke. It's performance is horrible,
  10. and it breaks in many ways.
  11. The original author may be reached as becker@scyld.com, or C/O
  12. Scyld Computing Corporation
  13. 410 Severn Ave., Suite 210
  14. Annapolis MD 21403
  15. Fixed (again!) the missing interrupt locking on TX/RX shifting.
  16. Alan Cox <Alan.Cox@linux.org>
  17. Removed calls to init_etherdev since they are no longer needed, and
  18. cleaned up modularization just a bit. The driver still allows only
  19. the default address for cards when loaded as a module, but that's
  20. really less braindead than anyone using a 3c501 board. :)
  21. 19950208 (invid@msen.com)
  22. Added traps for interrupts hitting the window as we clear and TX load
  23. the board. Now getting 150K/second FTP with a 3c501 card. Still playing
  24. with a TX-TX optimisation to see if we can touch 180-200K/second as seems
  25. theoretically maximum.
  26. 19950402 Alan Cox <Alan.Cox@linux.org>
  27. Cleaned up for 2.3.x because we broke SMP now.
  28. 20000208 Alan Cox <alan@redhat.com>
  29. Check up pass for 2.5. Nothing significant changed
  30. 20021009 Alan Cox <alan@redhat.com>
  31. Fixed zero fill corner case
  32. 20030104 Alan Cox <alan@redhat.com>
  33. For the avoidance of doubt the "preferred form" of this code is one which
  34. is in an open non patent encumbered format. Where cryptographic key signing
  35. forms part of the process of creating an executable the information
  36. including keys needed to generate an equivalently functional executable
  37. are deemed to be part of the source code.
  38. */
  39. /**
  40. * DOC: 3c501 Card Notes
  41. *
  42. * Some notes on this thing if you have to hack it. [Alan]
  43. *
  44. * Some documentation is available from 3Com. Due to the boards age
  45. * standard responses when you ask for this will range from 'be serious'
  46. * to 'give it to a museum'. The documentation is incomplete and mostly
  47. * of historical interest anyway.
  48. *
  49. * The basic system is a single buffer which can be used to receive or
  50. * transmit a packet. A third command mode exists when you are setting
  51. * things up.
  52. *
  53. * If it's transmitting it's not receiving and vice versa. In fact the
  54. * time to get the board back into useful state after an operation is
  55. * quite large.
  56. *
  57. * The driver works by keeping the board in receive mode waiting for a
  58. * packet to arrive. When one arrives it is copied out of the buffer
  59. * and delivered to the kernel. The card is reloaded and off we go.
  60. *
  61. * When transmitting lp->txing is set and the card is reset (from
  62. * receive mode) [possibly losing a packet just received] to command
  63. * mode. A packet is loaded and transmit mode triggered. The interrupt
  64. * handler runs different code for transmit interrupts and can handle
  65. * returning to receive mode or retransmissions (yes you have to help
  66. * out with those too).
  67. *
  68. * DOC: Problems
  69. *
  70. * There are a wide variety of undocumented error returns from the card
  71. * and you basically have to kick the board and pray if they turn up. Most
  72. * only occur under extreme load or if you do something the board doesn't
  73. * like (eg touching a register at the wrong time).
  74. *
  75. * The driver is less efficient than it could be. It switches through
  76. * receive mode even if more transmits are queued. If this worries you buy
  77. * a real Ethernet card.
  78. *
  79. * The combination of slow receive restart and no real multicast
  80. * filter makes the board unusable with a kernel compiled for IP
  81. * multicasting in a real multicast environment. That's down to the board,
  82. * but even with no multicast programs running a multicast IP kernel is
  83. * in group 224.0.0.1 and you will therefore be listening to all multicasts.
  84. * One nv conference running over that Ethernet and you can give up.
  85. *
  86. */
  87. #define DRV_NAME "3c501"
  88. #define DRV_VERSION "2002/10/09"
  89. static const char version[] =
  90. DRV_NAME ".c: " DRV_VERSION " Alan Cox (alan@redhat.com).\n";
  91. /*
  92. * Braindamage remaining:
  93. * The 3c501 board.
  94. */
  95. #include <linux/module.h>
  96. #include <linux/kernel.h>
  97. #include <linux/fcntl.h>
  98. #include <linux/ioport.h>
  99. #include <linux/interrupt.h>
  100. #include <linux/slab.h>
  101. #include <linux/string.h>
  102. #include <linux/errno.h>
  103. #include <linux/spinlock.h>
  104. #include <linux/ethtool.h>
  105. #include <linux/delay.h>
  106. #include <linux/bitops.h>
  107. #include <asm/uaccess.h>
  108. #include <asm/io.h>
  109. #include <linux/netdevice.h>
  110. #include <linux/etherdevice.h>
  111. #include <linux/skbuff.h>
  112. #include <linux/init.h>
  113. #include "3c501.h"
  114. /*
  115. * The boilerplate probe code.
  116. */
  117. static int io=0x280;
  118. static int irq=5;
  119. static int mem_start;
  120. /**
  121. * el1_probe: - probe for a 3c501
  122. * @dev: The device structure passed in to probe.
  123. *
  124. * This can be called from two places. The network layer will probe using
  125. * a device structure passed in with the probe information completed. For a
  126. * modular driver we use #init_module to fill in our own structure and probe
  127. * for it.
  128. *
  129. * Returns 0 on success. ENXIO if asked not to probe and ENODEV if asked to
  130. * probe and failing to find anything.
  131. */
  132. struct net_device * __init el1_probe(int unit)
  133. {
  134. struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
  135. static unsigned ports[] = { 0x280, 0x300, 0};
  136. unsigned *port;
  137. int err = 0;
  138. if (!dev)
  139. return ERR_PTR(-ENOMEM);
  140. if (unit >= 0) {
  141. sprintf(dev->name, "eth%d", unit);
  142. netdev_boot_setup_check(dev);
  143. io = dev->base_addr;
  144. irq = dev->irq;
  145. mem_start = dev->mem_start & 7;
  146. }
  147. SET_MODULE_OWNER(dev);
  148. if (io > 0x1ff) { /* Check a single specified location. */
  149. err = el1_probe1(dev, io);
  150. } else if (io != 0) {
  151. err = -ENXIO; /* Don't probe at all. */
  152. } else {
  153. for (port = ports; *port && el1_probe1(dev, *port); port++)
  154. ;
  155. if (!*port)
  156. err = -ENODEV;
  157. }
  158. if (err)
  159. goto out;
  160. err = register_netdev(dev);
  161. if (err)
  162. goto out1;
  163. return dev;
  164. out1:
  165. release_region(dev->base_addr, EL1_IO_EXTENT);
  166. out:
  167. free_netdev(dev);
  168. return ERR_PTR(err);
  169. }
  170. /**
  171. * el1_probe1:
  172. * @dev: The device structure to use
  173. * @ioaddr: An I/O address to probe at.
  174. *
  175. * The actual probe. This is iterated over by #el1_probe in order to
  176. * check all the applicable device locations.
  177. *
  178. * Returns 0 for a success, in which case the device is activated,
  179. * EAGAIN if the IRQ is in use by another driver, and ENODEV if the
  180. * board cannot be found.
  181. */
  182. static int __init el1_probe1(struct net_device *dev, int ioaddr)
  183. {
  184. struct net_local *lp;
  185. const char *mname; /* Vendor name */
  186. unsigned char station_addr[6];
  187. int autoirq = 0;
  188. int i;
  189. /*
  190. * Reserve I/O resource for exclusive use by this driver
  191. */
  192. if (!request_region(ioaddr, EL1_IO_EXTENT, DRV_NAME))
  193. return -ENODEV;
  194. /*
  195. * Read the station address PROM data from the special port.
  196. */
  197. for (i = 0; i < 6; i++)
  198. {
  199. outw(i, ioaddr + EL1_DATAPTR);
  200. station_addr[i] = inb(ioaddr + EL1_SAPROM);
  201. }
  202. /*
  203. * Check the first three octets of the S.A. for 3Com's prefix, or
  204. * for the Sager NP943 prefix.
  205. */
  206. if (station_addr[0] == 0x02 && station_addr[1] == 0x60
  207. && station_addr[2] == 0x8c)
  208. {
  209. mname = "3c501";
  210. } else if (station_addr[0] == 0x00 && station_addr[1] == 0x80
  211. && station_addr[2] == 0xC8)
  212. {
  213. mname = "NP943";
  214. }
  215. else {
  216. release_region(ioaddr, EL1_IO_EXTENT);
  217. return -ENODEV;
  218. }
  219. /*
  220. * We auto-IRQ by shutting off the interrupt line and letting it float
  221. * high.
  222. */
  223. dev->irq = irq;
  224. if (dev->irq < 2)
  225. {
  226. unsigned long irq_mask;
  227. irq_mask = probe_irq_on();
  228. inb(RX_STATUS); /* Clear pending interrupts. */
  229. inb(TX_STATUS);
  230. outb(AX_LOOP + 1, AX_CMD);
  231. outb(0x00, AX_CMD);
  232. mdelay(20);
  233. autoirq = probe_irq_off(irq_mask);
  234. if (autoirq == 0)
  235. {
  236. printk(KERN_WARNING "%s probe at %#x failed to detect IRQ line.\n",
  237. mname, ioaddr);
  238. release_region(ioaddr, EL1_IO_EXTENT);
  239. return -EAGAIN;
  240. }
  241. }
  242. outb(AX_RESET+AX_LOOP, AX_CMD); /* Loopback mode. */
  243. dev->base_addr = ioaddr;
  244. memcpy(dev->dev_addr, station_addr, ETH_ALEN);
  245. if (mem_start & 0xf)
  246. el_debug = mem_start & 0x7;
  247. if (autoirq)
  248. dev->irq = autoirq;
  249. printk(KERN_INFO "%s: %s EtherLink at %#lx, using %sIRQ %d.\n", dev->name, mname, dev->base_addr,
  250. autoirq ? "auto":"assigned ", dev->irq);
  251. #ifdef CONFIG_IP_MULTICAST
  252. printk(KERN_WARNING "WARNING: Use of the 3c501 in a multicast kernel is NOT recommended.\n");
  253. #endif
  254. if (el_debug)
  255. printk(KERN_DEBUG "%s", version);
  256. memset(dev->priv, 0, sizeof(struct net_local));
  257. lp = netdev_priv(dev);
  258. spin_lock_init(&lp->lock);
  259. /*
  260. * The EL1-specific entries in the device structure.
  261. */
  262. dev->open = &el_open;
  263. dev->hard_start_xmit = &el_start_xmit;
  264. dev->tx_timeout = &el_timeout;
  265. dev->watchdog_timeo = HZ;
  266. dev->stop = &el1_close;
  267. dev->get_stats = &el1_get_stats;
  268. dev->set_multicast_list = &set_multicast_list;
  269. dev->ethtool_ops = &netdev_ethtool_ops;
  270. return 0;
  271. }
  272. /**
  273. * el1_open:
  274. * @dev: device that is being opened
  275. *
  276. * When an ifconfig is issued which changes the device flags to include
  277. * IFF_UP this function is called. It is only called when the change
  278. * occurs, not when the interface remains up. #el1_close will be called
  279. * when it goes down.
  280. *
  281. * Returns 0 for a successful open, or -EAGAIN if someone has run off
  282. * with our interrupt line.
  283. */
  284. static int el_open(struct net_device *dev)
  285. {
  286. int retval;
  287. int ioaddr = dev->base_addr;
  288. struct net_local *lp = netdev_priv(dev);
  289. unsigned long flags;
  290. if (el_debug > 2)
  291. printk(KERN_DEBUG "%s: Doing el_open()...", dev->name);
  292. if ((retval = request_irq(dev->irq, &el_interrupt, 0, dev->name, dev)))
  293. return retval;
  294. spin_lock_irqsave(&lp->lock, flags);
  295. el_reset(dev);
  296. spin_unlock_irqrestore(&lp->lock, flags);
  297. lp->txing = 0; /* Board in RX mode */
  298. outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */
  299. netif_start_queue(dev);
  300. return 0;
  301. }
  302. /**
  303. * el_timeout:
  304. * @dev: The 3c501 card that has timed out
  305. *
  306. * Attempt to restart the board. This is basically a mixture of extreme
  307. * violence and prayer
  308. *
  309. */
  310. static void el_timeout(struct net_device *dev)
  311. {
  312. struct net_local *lp = netdev_priv(dev);
  313. int ioaddr = dev->base_addr;
  314. if (el_debug)
  315. printk (KERN_DEBUG "%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
  316. dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS));
  317. lp->stats.tx_errors++;
  318. outb(TX_NORM, TX_CMD);
  319. outb(RX_NORM, RX_CMD);
  320. outb(AX_OFF, AX_CMD); /* Just trigger a false interrupt. */
  321. outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */
  322. lp->txing = 0; /* Ripped back in to RX */
  323. netif_wake_queue(dev);
  324. }
  325. /**
  326. * el_start_xmit:
  327. * @skb: The packet that is queued to be sent
  328. * @dev: The 3c501 card we want to throw it down
  329. *
  330. * Attempt to send a packet to a 3c501 card. There are some interesting
  331. * catches here because the 3c501 is an extremely old and therefore
  332. * stupid piece of technology.
  333. *
  334. * If we are handling an interrupt on the other CPU we cannot load a packet
  335. * as we may still be attempting to retrieve the last RX packet buffer.
  336. *
  337. * When a transmit times out we dump the card into control mode and just
  338. * start again. It happens enough that it isnt worth logging.
  339. *
  340. * We avoid holding the spin locks when doing the packet load to the board.
  341. * The device is very slow, and its DMA mode is even slower. If we held the
  342. * lock while loading 1500 bytes onto the controller we would drop a lot of
  343. * serial port characters. This requires we do extra locking, but we have
  344. * no real choice.
  345. */
  346. static int el_start_xmit(struct sk_buff *skb, struct net_device *dev)
  347. {
  348. struct net_local *lp = netdev_priv(dev);
  349. int ioaddr = dev->base_addr;
  350. unsigned long flags;
  351. /*
  352. * Avoid incoming interrupts between us flipping txing and flipping
  353. * mode as the driver assumes txing is a faithful indicator of card
  354. * state
  355. */
  356. spin_lock_irqsave(&lp->lock, flags);
  357. /*
  358. * Avoid timer-based retransmission conflicts.
  359. */
  360. netif_stop_queue(dev);
  361. do
  362. {
  363. int len = skb->len;
  364. int pad = 0;
  365. int gp_start;
  366. unsigned char *buf = skb->data;
  367. if (len < ETH_ZLEN)
  368. pad = ETH_ZLEN - len;
  369. gp_start = 0x800 - ( len + pad );
  370. lp->tx_pkt_start = gp_start;
  371. lp->collisions = 0;
  372. lp->stats.tx_bytes += skb->len;
  373. /*
  374. * Command mode with status cleared should [in theory]
  375. * mean no more interrupts can be pending on the card.
  376. */
  377. outb_p(AX_SYS, AX_CMD);
  378. inb_p(RX_STATUS);
  379. inb_p(TX_STATUS);
  380. lp->loading = 1;
  381. lp->txing = 1;
  382. /*
  383. * Turn interrupts back on while we spend a pleasant afternoon
  384. * loading bytes into the board
  385. */
  386. spin_unlock_irqrestore(&lp->lock, flags);
  387. outw(0x00, RX_BUF_CLR); /* Set rx packet area to 0. */
  388. outw(gp_start, GP_LOW); /* aim - packet will be loaded into buffer start */
  389. outsb(DATAPORT,buf,len); /* load buffer (usual thing each byte increments the pointer) */
  390. if (pad) {
  391. while(pad--) /* Zero fill buffer tail */
  392. outb(0, DATAPORT);
  393. }
  394. outw(gp_start, GP_LOW); /* the board reuses the same register */
  395. if(lp->loading != 2)
  396. {
  397. outb(AX_XMIT, AX_CMD); /* fire ... Trigger xmit. */
  398. lp->loading=0;
  399. dev->trans_start = jiffies;
  400. if (el_debug > 2)
  401. printk(KERN_DEBUG " queued xmit.\n");
  402. dev_kfree_skb (skb);
  403. return 0;
  404. }
  405. /* A receive upset our load, despite our best efforts */
  406. if(el_debug>2)
  407. printk(KERN_DEBUG "%s: burped during tx load.\n", dev->name);
  408. spin_lock_irqsave(&lp->lock, flags);
  409. }
  410. while(1);
  411. }
  412. /**
  413. * el_interrupt:
  414. * @irq: Interrupt number
  415. * @dev_id: The 3c501 that burped
  416. *
  417. * Handle the ether interface interrupts. The 3c501 needs a lot more
  418. * hand holding than most cards. In particular we get a transmit interrupt
  419. * with a collision error because the board firmware isnt capable of rewinding
  420. * its own transmit buffer pointers. It can however count to 16 for us.
  421. *
  422. * On the receive side the card is also very dumb. It has no buffering to
  423. * speak of. We simply pull the packet out of its PIO buffer (which is slow)
  424. * and queue it for the kernel. Then we reset the card for the next packet.
  425. *
  426. * We sometimes get surprise interrupts late both because the SMP IRQ delivery
  427. * is message passing and because the card sometimes seems to deliver late. I
  428. * think if it is part way through a receive and the mode is changed it carries
  429. * on receiving and sends us an interrupt. We have to band aid all these cases
  430. * to get a sensible 150kBytes/second performance. Even then you want a small
  431. * TCP window.
  432. */
  433. static irqreturn_t el_interrupt(int irq, void *dev_id)
  434. {
  435. struct net_device *dev = dev_id;
  436. struct net_local *lp;
  437. int ioaddr;
  438. int axsr; /* Aux. status reg. */
  439. ioaddr = dev->base_addr;
  440. lp = netdev_priv(dev);
  441. spin_lock(&lp->lock);
  442. /*
  443. * What happened ?
  444. */
  445. axsr = inb(AX_STATUS);
  446. /*
  447. * Log it
  448. */
  449. if (el_debug > 3)
  450. printk(KERN_DEBUG "%s: el_interrupt() aux=%#02x", dev->name, axsr);
  451. if(lp->loading==1 && !lp->txing)
  452. printk(KERN_WARNING "%s: Inconsistent state loading while not in tx\n",
  453. dev->name);
  454. if (lp->txing)
  455. {
  456. /*
  457. * Board in transmit mode. May be loading. If we are
  458. * loading we shouldn't have got this.
  459. */
  460. int txsr = inb(TX_STATUS);
  461. if(lp->loading==1)
  462. {
  463. if(el_debug > 2)
  464. {
  465. printk(KERN_DEBUG "%s: Interrupt while loading [", dev->name);
  466. printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x]\n", txsr, inw(GP_LOW),inw(RX_LOW));
  467. }
  468. lp->loading=2; /* Force a reload */
  469. spin_unlock(&lp->lock);
  470. goto out;
  471. }
  472. if (el_debug > 6)
  473. printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x", txsr, inw(GP_LOW),inw(RX_LOW));
  474. if ((axsr & 0x80) && (txsr & TX_READY) == 0)
  475. {
  476. /*
  477. * FIXME: is there a logic to whether to keep on trying or
  478. * reset immediately ?
  479. */
  480. if(el_debug>1)
  481. printk(KERN_DEBUG "%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x"
  482. " gp=%03x rp=%03x.\n", dev->name, txsr, axsr,
  483. inw(ioaddr + EL1_DATAPTR), inw(ioaddr + EL1_RXPTR));
  484. lp->txing = 0;
  485. netif_wake_queue(dev);
  486. }
  487. else if (txsr & TX_16COLLISIONS)
  488. {
  489. /*
  490. * Timed out
  491. */
  492. if (el_debug)
  493. printk (KERN_DEBUG "%s: Transmit failed 16 times, Ethernet jammed?\n",dev->name);
  494. outb(AX_SYS, AX_CMD);
  495. lp->txing = 0;
  496. lp->stats.tx_aborted_errors++;
  497. netif_wake_queue(dev);
  498. }
  499. else if (txsr & TX_COLLISION)
  500. {
  501. /*
  502. * Retrigger xmit.
  503. */
  504. if (el_debug > 6)
  505. printk(KERN_DEBUG " retransmitting after a collision.\n");
  506. /*
  507. * Poor little chip can't reset its own start pointer
  508. */
  509. outb(AX_SYS, AX_CMD);
  510. outw(lp->tx_pkt_start, GP_LOW);
  511. outb(AX_XMIT, AX_CMD);
  512. lp->stats.collisions++;
  513. spin_unlock(&lp->lock);
  514. goto out;
  515. }
  516. else
  517. {
  518. /*
  519. * It worked.. we will now fall through and receive
  520. */
  521. lp->stats.tx_packets++;
  522. if (el_debug > 6)
  523. printk(KERN_DEBUG " Tx succeeded %s\n",
  524. (txsr & TX_RDY) ? "." : "but tx is busy!");
  525. /*
  526. * This is safe the interrupt is atomic WRT itself.
  527. */
  528. lp->txing = 0;
  529. netif_wake_queue(dev); /* In case more to transmit */
  530. }
  531. }
  532. else
  533. {
  534. /*
  535. * In receive mode.
  536. */
  537. int rxsr = inb(RX_STATUS);
  538. if (el_debug > 5)
  539. printk(KERN_DEBUG " rxsr=%02x txsr=%02x rp=%04x", rxsr, inb(TX_STATUS),inw(RX_LOW));
  540. /*
  541. * Just reading rx_status fixes most errors.
  542. */
  543. if (rxsr & RX_MISSED)
  544. lp->stats.rx_missed_errors++;
  545. else if (rxsr & RX_RUNT)
  546. { /* Handled to avoid board lock-up. */
  547. lp->stats.rx_length_errors++;
  548. if (el_debug > 5)
  549. printk(KERN_DEBUG " runt.\n");
  550. }
  551. else if (rxsr & RX_GOOD)
  552. {
  553. /*
  554. * Receive worked.
  555. */
  556. el_receive(dev);
  557. }
  558. else
  559. {
  560. /*
  561. * Nothing? Something is broken!
  562. */
  563. if (el_debug > 2)
  564. printk(KERN_DEBUG "%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
  565. dev->name, rxsr);
  566. el_reset(dev);
  567. }
  568. if (el_debug > 3)
  569. printk(KERN_DEBUG ".\n");
  570. }
  571. /*
  572. * Move into receive mode
  573. */
  574. outb(AX_RX, AX_CMD);
  575. outw(0x00, RX_BUF_CLR);
  576. inb(RX_STATUS); /* Be certain that interrupts are cleared. */
  577. inb(TX_STATUS);
  578. spin_unlock(&lp->lock);
  579. out:
  580. return IRQ_HANDLED;
  581. }
  582. /**
  583. * el_receive:
  584. * @dev: Device to pull the packets from
  585. *
  586. * We have a good packet. Well, not really "good", just mostly not broken.
  587. * We must check everything to see if it is good. In particular we occasionally
  588. * get wild packet sizes from the card. If the packet seems sane we PIO it
  589. * off the card and queue it for the protocol layers.
  590. */
  591. static void el_receive(struct net_device *dev)
  592. {
  593. struct net_local *lp = netdev_priv(dev);
  594. int ioaddr = dev->base_addr;
  595. int pkt_len;
  596. struct sk_buff *skb;
  597. pkt_len = inw(RX_LOW);
  598. if (el_debug > 4)
  599. printk(KERN_DEBUG " el_receive %d.\n", pkt_len);
  600. if ((pkt_len < 60) || (pkt_len > 1536))
  601. {
  602. if (el_debug)
  603. printk(KERN_DEBUG "%s: bogus packet, length=%d\n", dev->name, pkt_len);
  604. lp->stats.rx_over_errors++;
  605. return;
  606. }
  607. /*
  608. * Command mode so we can empty the buffer
  609. */
  610. outb(AX_SYS, AX_CMD);
  611. skb = dev_alloc_skb(pkt_len+2);
  612. /*
  613. * Start of frame
  614. */
  615. outw(0x00, GP_LOW);
  616. if (skb == NULL)
  617. {
  618. printk(KERN_INFO "%s: Memory squeeze, dropping packet.\n", dev->name);
  619. lp->stats.rx_dropped++;
  620. return;
  621. }
  622. else
  623. {
  624. skb_reserve(skb,2); /* Force 16 byte alignment */
  625. skb->dev = dev;
  626. /*
  627. * The read increments through the bytes. The interrupt
  628. * handler will fix the pointer when it returns to
  629. * receive mode.
  630. */
  631. insb(DATAPORT, skb_put(skb,pkt_len), pkt_len);
  632. skb->protocol=eth_type_trans(skb,dev);
  633. netif_rx(skb);
  634. dev->last_rx = jiffies;
  635. lp->stats.rx_packets++;
  636. lp->stats.rx_bytes+=pkt_len;
  637. }
  638. return;
  639. }
  640. /**
  641. * el_reset: Reset a 3c501 card
  642. * @dev: The 3c501 card about to get zapped
  643. *
  644. * Even resetting a 3c501 isnt simple. When you activate reset it loses all
  645. * its configuration. You must hold the lock when doing this. The function
  646. * cannot take the lock itself as it is callable from the irq handler.
  647. */
  648. static void el_reset(struct net_device *dev)
  649. {
  650. struct net_local *lp = netdev_priv(dev);
  651. int ioaddr = dev->base_addr;
  652. if (el_debug> 2)
  653. printk(KERN_INFO "3c501 reset...");
  654. outb(AX_RESET, AX_CMD); /* Reset the chip */
  655. outb(AX_LOOP, AX_CMD); /* Aux control, irq and loopback enabled */
  656. {
  657. int i;
  658. for (i = 0; i < 6; i++) /* Set the station address. */
  659. outb(dev->dev_addr[i], ioaddr + i);
  660. }
  661. outw(0, RX_BUF_CLR); /* Set rx packet area to 0. */
  662. outb(TX_NORM, TX_CMD); /* tx irq on done, collision */
  663. outb(RX_NORM, RX_CMD); /* Set Rx commands. */
  664. inb(RX_STATUS); /* Clear status. */
  665. inb(TX_STATUS);
  666. lp->txing = 0;
  667. }
  668. /**
  669. * el1_close:
  670. * @dev: 3c501 card to shut down
  671. *
  672. * Close a 3c501 card. The IFF_UP flag has been cleared by the user via
  673. * the SIOCSIFFLAGS ioctl. We stop any further transmissions being queued,
  674. * and then disable the interrupts. Finally we reset the chip. The effects
  675. * of the rest will be cleaned up by #el1_open. Always returns 0 indicating
  676. * a success.
  677. */
  678. static int el1_close(struct net_device *dev)
  679. {
  680. int ioaddr = dev->base_addr;
  681. if (el_debug > 2)
  682. printk(KERN_INFO "%s: Shutting down Ethernet card at %#x.\n", dev->name, ioaddr);
  683. netif_stop_queue(dev);
  684. /*
  685. * Free and disable the IRQ.
  686. */
  687. free_irq(dev->irq, dev);
  688. outb(AX_RESET, AX_CMD); /* Reset the chip */
  689. return 0;
  690. }
  691. /**
  692. * el1_get_stats:
  693. * @dev: The card to get the statistics for
  694. *
  695. * In smarter devices this function is needed to pull statistics off the
  696. * board itself. The 3c501 has no hardware statistics. We maintain them all
  697. * so they are by definition always up to date.
  698. *
  699. * Returns the statistics for the card from the card private data
  700. */
  701. static struct net_device_stats *el1_get_stats(struct net_device *dev)
  702. {
  703. struct net_local *lp = netdev_priv(dev);
  704. return &lp->stats;
  705. }
  706. /**
  707. * set_multicast_list:
  708. * @dev: The device to adjust
  709. *
  710. * Set or clear the multicast filter for this adaptor to use the best-effort
  711. * filtering supported. The 3c501 supports only three modes of filtering.
  712. * It always receives broadcasts and packets for itself. You can choose to
  713. * optionally receive all packets, or all multicast packets on top of this.
  714. */
  715. static void set_multicast_list(struct net_device *dev)
  716. {
  717. int ioaddr = dev->base_addr;
  718. if(dev->flags&IFF_PROMISC)
  719. {
  720. outb(RX_PROM, RX_CMD);
  721. inb(RX_STATUS);
  722. }
  723. else if (dev->mc_list || dev->flags&IFF_ALLMULTI)
  724. {
  725. outb(RX_MULT, RX_CMD); /* Multicast or all multicast is the same */
  726. inb(RX_STATUS); /* Clear status. */
  727. }
  728. else
  729. {
  730. outb(RX_NORM, RX_CMD);
  731. inb(RX_STATUS);
  732. }
  733. }
  734. static void netdev_get_drvinfo(struct net_device *dev,
  735. struct ethtool_drvinfo *info)
  736. {
  737. strcpy(info->driver, DRV_NAME);
  738. strcpy(info->version, DRV_VERSION);
  739. sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
  740. }
  741. static u32 netdev_get_msglevel(struct net_device *dev)
  742. {
  743. return debug;
  744. }
  745. static void netdev_set_msglevel(struct net_device *dev, u32 level)
  746. {
  747. debug = level;
  748. }
  749. static const struct ethtool_ops netdev_ethtool_ops = {
  750. .get_drvinfo = netdev_get_drvinfo,
  751. .get_msglevel = netdev_get_msglevel,
  752. .set_msglevel = netdev_set_msglevel,
  753. };
  754. #ifdef MODULE
  755. static struct net_device *dev_3c501;
  756. module_param(io, int, 0);
  757. module_param(irq, int, 0);
  758. MODULE_PARM_DESC(io, "EtherLink I/O base address");
  759. MODULE_PARM_DESC(irq, "EtherLink IRQ number");
  760. /**
  761. * init_module:
  762. *
  763. * When the driver is loaded as a module this function is called. We fake up
  764. * a device structure with the base I/O and interrupt set as if it were being
  765. * called from Space.c. This minimises the extra code that would otherwise
  766. * be required.
  767. *
  768. * Returns 0 for success or -EIO if a card is not found. Returning an error
  769. * here also causes the module to be unloaded
  770. */
  771. int __init init_module(void)
  772. {
  773. dev_3c501 = el1_probe(-1);
  774. if (IS_ERR(dev_3c501))
  775. return PTR_ERR(dev_3c501);
  776. return 0;
  777. }
  778. /**
  779. * cleanup_module:
  780. *
  781. * The module is being unloaded. We unhook our network device from the system
  782. * and then free up the resources we took when the card was found.
  783. */
  784. void __exit cleanup_module(void)
  785. {
  786. struct net_device *dev = dev_3c501;
  787. unregister_netdev(dev);
  788. release_region(dev->base_addr, EL1_IO_EXTENT);
  789. free_netdev(dev);
  790. }
  791. #endif /* MODULE */
  792. MODULE_AUTHOR("Donald Becker, Alan Cox");
  793. MODULE_DESCRIPTION("Support for the ancient 3Com 3c501 ethernet card");
  794. MODULE_LICENSE("GPL");