3c589_cs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /* ======================================================================
  2. *
  3. * A PCMCIA ethernet driver for the 3com 3c589 card.
  4. *
  5. * Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
  6. *
  7. * 3c589_cs.c 1.162 2001/10/13 00:08:50
  8. *
  9. * The network driver code is based on Donald Becker's 3c589 code:
  10. *
  11. * Written 1994 by Donald Becker.
  12. * Copyright 1993 United States Government as represented by the
  13. * Director, National Security Agency. This software may be used and
  14. * distributed according to the terms of the GNU General Public License,
  15. * incorporated herein by reference.
  16. * Donald Becker may be reached at becker@scyld.com
  17. *
  18. * Updated for 2.5.x by Alan Cox <alan@lxorguk.ukuu.org.uk>
  19. *
  20. * ======================================================================
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #define DRV_NAME "3c589_cs"
  24. #define DRV_VERSION "1.162-ac"
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <linux/timer.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/in.h>
  33. #include <linux/delay.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/ioport.h>
  40. #include <linux/bitops.h>
  41. #include <linux/jiffies.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/io.h>
  44. #include <pcmcia/cistpl.h>
  45. #include <pcmcia/cisreg.h>
  46. #include <pcmcia/ciscode.h>
  47. #include <pcmcia/ds.h>
  48. /* To minimize the size of the driver source I only define operating
  49. * constants if they are used several times. You'll need the manual
  50. * if you want to understand driver details.
  51. */
  52. /* Offsets from base I/O address. */
  53. #define EL3_DATA 0x00
  54. #define EL3_TIMER 0x0a
  55. #define EL3_CMD 0x0e
  56. #define EL3_STATUS 0x0e
  57. #define EEPROM_READ 0x0080
  58. #define EEPROM_BUSY 0x8000
  59. #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
  60. /* The top five bits written to EL3_CMD are a command, the lower
  61. * 11 bits are the parameter, if applicable.
  62. */
  63. enum c509cmd {
  64. TotalReset = 0<<11,
  65. SelectWindow = 1<<11,
  66. StartCoax = 2<<11,
  67. RxDisable = 3<<11,
  68. RxEnable = 4<<11,
  69. RxReset = 5<<11,
  70. RxDiscard = 8<<11,
  71. TxEnable = 9<<11,
  72. TxDisable = 10<<11,
  73. TxReset = 11<<11,
  74. FakeIntr = 12<<11,
  75. AckIntr = 13<<11,
  76. SetIntrEnb = 14<<11,
  77. SetStatusEnb = 15<<11,
  78. SetRxFilter = 16<<11,
  79. SetRxThreshold = 17<<11,
  80. SetTxThreshold = 18<<11,
  81. SetTxStart = 19<<11,
  82. StatsEnable = 21<<11,
  83. StatsDisable = 22<<11,
  84. StopCoax = 23<<11
  85. };
  86. enum c509status {
  87. IntLatch = 0x0001,
  88. AdapterFailure = 0x0002,
  89. TxComplete = 0x0004,
  90. TxAvailable = 0x0008,
  91. RxComplete = 0x0010,
  92. RxEarly = 0x0020,
  93. IntReq = 0x0040,
  94. StatsFull = 0x0080,
  95. CmdBusy = 0x1000
  96. };
  97. /* The SetRxFilter command accepts the following classes: */
  98. enum RxFilter {
  99. RxStation = 1,
  100. RxMulticast = 2,
  101. RxBroadcast = 4,
  102. RxProm = 8
  103. };
  104. /* Register window 1 offsets, the window used in normal operation. */
  105. #define TX_FIFO 0x00
  106. #define RX_FIFO 0x00
  107. #define RX_STATUS 0x08
  108. #define TX_STATUS 0x0B
  109. #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */
  110. #define WN0_IRQ 0x08 /* Window 0: Set IRQ line in bits 12-15. */
  111. #define WN4_MEDIA 0x0A /* Window 4: Various transcvr/media bits. */
  112. #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
  113. #define MEDIA_LED 0x0001 /* Enable link light on 3C589E cards. */
  114. /* Time in jiffies before concluding Tx hung */
  115. #define TX_TIMEOUT ((400*HZ)/1000)
  116. struct el3_private {
  117. struct pcmcia_device *p_dev;
  118. /* For transceiver monitoring */
  119. struct timer_list media;
  120. u16 media_status;
  121. u16 fast_poll;
  122. unsigned long last_irq;
  123. spinlock_t lock;
  124. };
  125. static const char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
  126. /*====================================================================*/
  127. /* Module parameters */
  128. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  129. MODULE_DESCRIPTION("3Com 3c589 series PCMCIA ethernet driver");
  130. MODULE_LICENSE("GPL");
  131. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
  132. /* Special hook for setting if_port when module is loaded */
  133. INT_MODULE_PARM(if_port, 0);
  134. /*====================================================================*/
  135. static int tc589_config(struct pcmcia_device *link);
  136. static void tc589_release(struct pcmcia_device *link);
  137. static u16 read_eeprom(unsigned int ioaddr, int index);
  138. static void tc589_reset(struct net_device *dev);
  139. static void media_check(struct timer_list *t);
  140. static int el3_config(struct net_device *dev, struct ifmap *map);
  141. static int el3_open(struct net_device *dev);
  142. static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
  143. struct net_device *dev);
  144. static irqreturn_t el3_interrupt(int irq, void *dev_id);
  145. static void update_stats(struct net_device *dev);
  146. static struct net_device_stats *el3_get_stats(struct net_device *dev);
  147. static int el3_rx(struct net_device *dev);
  148. static int el3_close(struct net_device *dev);
  149. static void el3_tx_timeout(struct net_device *dev);
  150. static void set_rx_mode(struct net_device *dev);
  151. static void set_multicast_list(struct net_device *dev);
  152. static const struct ethtool_ops netdev_ethtool_ops;
  153. static void tc589_detach(struct pcmcia_device *p_dev);
  154. static const struct net_device_ops el3_netdev_ops = {
  155. .ndo_open = el3_open,
  156. .ndo_stop = el3_close,
  157. .ndo_start_xmit = el3_start_xmit,
  158. .ndo_tx_timeout = el3_tx_timeout,
  159. .ndo_set_config = el3_config,
  160. .ndo_get_stats = el3_get_stats,
  161. .ndo_set_rx_mode = set_multicast_list,
  162. .ndo_set_mac_address = eth_mac_addr,
  163. .ndo_validate_addr = eth_validate_addr,
  164. };
  165. static int tc589_probe(struct pcmcia_device *link)
  166. {
  167. struct el3_private *lp;
  168. struct net_device *dev;
  169. dev_dbg(&link->dev, "3c589_attach()\n");
  170. /* Create new ethernet device */
  171. dev = alloc_etherdev(sizeof(struct el3_private));
  172. if (!dev)
  173. return -ENOMEM;
  174. lp = netdev_priv(dev);
  175. link->priv = dev;
  176. lp->p_dev = link;
  177. spin_lock_init(&lp->lock);
  178. link->resource[0]->end = 16;
  179. link->resource[0]->flags |= IO_DATA_PATH_WIDTH_16;
  180. link->config_flags |= CONF_ENABLE_IRQ;
  181. link->config_index = 1;
  182. dev->netdev_ops = &el3_netdev_ops;
  183. dev->watchdog_timeo = TX_TIMEOUT;
  184. dev->ethtool_ops = &netdev_ethtool_ops;
  185. return tc589_config(link);
  186. }
  187. static void tc589_detach(struct pcmcia_device *link)
  188. {
  189. struct net_device *dev = link->priv;
  190. dev_dbg(&link->dev, "3c589_detach\n");
  191. unregister_netdev(dev);
  192. tc589_release(link);
  193. free_netdev(dev);
  194. } /* tc589_detach */
  195. static int tc589_config(struct pcmcia_device *link)
  196. {
  197. struct net_device *dev = link->priv;
  198. __be16 *phys_addr;
  199. int ret, i, j, multi = 0, fifo;
  200. unsigned int ioaddr;
  201. static const char * const ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
  202. u8 *buf;
  203. size_t len;
  204. dev_dbg(&link->dev, "3c589_config\n");
  205. phys_addr = (__be16 *)dev->dev_addr;
  206. /* Is this a 3c562? */
  207. if (link->manf_id != MANFID_3COM)
  208. dev_info(&link->dev, "hmmm, is this really a 3Com card??\n");
  209. multi = (link->card_id == PRODID_3COM_3C562);
  210. link->io_lines = 16;
  211. /* For the 3c562, the base address must be xx00-xx7f */
  212. for (i = j = 0; j < 0x400; j += 0x10) {
  213. if (multi && (j & 0x80))
  214. continue;
  215. link->resource[0]->start = j ^ 0x300;
  216. i = pcmcia_request_io(link);
  217. if (i == 0)
  218. break;
  219. }
  220. if (i != 0)
  221. goto failed;
  222. ret = pcmcia_request_irq(link, el3_interrupt);
  223. if (ret)
  224. goto failed;
  225. ret = pcmcia_enable_device(link);
  226. if (ret)
  227. goto failed;
  228. dev->irq = link->irq;
  229. dev->base_addr = link->resource[0]->start;
  230. ioaddr = dev->base_addr;
  231. EL3WINDOW(0);
  232. /* The 3c589 has an extra EEPROM for configuration info, including
  233. * the hardware address. The 3c562 puts the address in the CIS.
  234. */
  235. len = pcmcia_get_tuple(link, 0x88, &buf);
  236. if (buf && len >= 6) {
  237. for (i = 0; i < 3; i++)
  238. phys_addr[i] = htons(le16_to_cpu(buf[i*2]));
  239. kfree(buf);
  240. } else {
  241. kfree(buf); /* 0 < len < 6 */
  242. for (i = 0; i < 3; i++)
  243. phys_addr[i] = htons(read_eeprom(ioaddr, i));
  244. if (phys_addr[0] == htons(0x6060)) {
  245. dev_err(&link->dev, "IO port conflict at 0x%03lx-0x%03lx\n",
  246. dev->base_addr, dev->base_addr+15);
  247. goto failed;
  248. }
  249. }
  250. /* The address and resource configuration register aren't loaded from
  251. * the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version.
  252. */
  253. outw(0x3f00, ioaddr + 8);
  254. fifo = inl(ioaddr);
  255. /* The if_port symbol can be set when the module is loaded */
  256. if ((if_port >= 0) && (if_port <= 3))
  257. dev->if_port = if_port;
  258. else
  259. dev_err(&link->dev, "invalid if_port requested\n");
  260. SET_NETDEV_DEV(dev, &link->dev);
  261. if (register_netdev(dev) != 0) {
  262. dev_err(&link->dev, "register_netdev() failed\n");
  263. goto failed;
  264. }
  265. netdev_info(dev, "3Com 3c%s, io %#3lx, irq %d, hw_addr %pM\n",
  266. (multi ? "562" : "589"), dev->base_addr, dev->irq,
  267. dev->dev_addr);
  268. netdev_info(dev, " %dK FIFO split %s Rx:Tx, %s xcvr\n",
  269. (fifo & 7) ? 32 : 8, ram_split[(fifo >> 16) & 3],
  270. if_names[dev->if_port]);
  271. return 0;
  272. failed:
  273. tc589_release(link);
  274. return -ENODEV;
  275. } /* tc589_config */
  276. static void tc589_release(struct pcmcia_device *link)
  277. {
  278. pcmcia_disable_device(link);
  279. }
  280. static int tc589_suspend(struct pcmcia_device *link)
  281. {
  282. struct net_device *dev = link->priv;
  283. if (link->open)
  284. netif_device_detach(dev);
  285. return 0;
  286. }
  287. static int tc589_resume(struct pcmcia_device *link)
  288. {
  289. struct net_device *dev = link->priv;
  290. if (link->open) {
  291. tc589_reset(dev);
  292. netif_device_attach(dev);
  293. }
  294. return 0;
  295. }
  296. /*====================================================================*/
  297. /* Use this for commands that may take time to finish */
  298. static void tc589_wait_for_completion(struct net_device *dev, int cmd)
  299. {
  300. int i = 100;
  301. outw(cmd, dev->base_addr + EL3_CMD);
  302. while (--i > 0)
  303. if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000))
  304. break;
  305. if (i == 0)
  306. netdev_warn(dev, "command 0x%04x did not complete!\n", cmd);
  307. }
  308. /* Read a word from the EEPROM using the regular EEPROM access register.
  309. * Assume that we are in register window zero.
  310. */
  311. static u16 read_eeprom(unsigned int ioaddr, int index)
  312. {
  313. int i;
  314. outw(EEPROM_READ + index, ioaddr + 10);
  315. /* Reading the eeprom takes 162 us */
  316. for (i = 1620; i >= 0; i--)
  317. if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
  318. break;
  319. return inw(ioaddr + 12);
  320. }
  321. /* Set transceiver type, perhaps to something other than what the user
  322. * specified in dev->if_port.
  323. */
  324. static void tc589_set_xcvr(struct net_device *dev, int if_port)
  325. {
  326. struct el3_private *lp = netdev_priv(dev);
  327. unsigned int ioaddr = dev->base_addr;
  328. EL3WINDOW(0);
  329. switch (if_port) {
  330. case 0:
  331. case 1:
  332. outw(0, ioaddr + 6);
  333. break;
  334. case 2:
  335. outw(3<<14, ioaddr + 6);
  336. break;
  337. case 3:
  338. outw(1<<14, ioaddr + 6);
  339. break;
  340. }
  341. /* On PCMCIA, this just turns on the LED */
  342. outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
  343. /* 10baseT interface, enable link beat and jabber check. */
  344. EL3WINDOW(4);
  345. outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
  346. EL3WINDOW(1);
  347. if (if_port == 2)
  348. lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
  349. else
  350. lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
  351. }
  352. static void dump_status(struct net_device *dev)
  353. {
  354. unsigned int ioaddr = dev->base_addr;
  355. EL3WINDOW(1);
  356. netdev_info(dev, " irq status %04x, rx status %04x, tx status %02x tx free %04x\n",
  357. inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS),
  358. inb(ioaddr+TX_STATUS), inw(ioaddr+TX_FREE));
  359. EL3WINDOW(4);
  360. netdev_info(dev, " diagnostics: fifo %04x net %04x ethernet %04x media %04x\n",
  361. inw(ioaddr+0x04), inw(ioaddr+0x06), inw(ioaddr+0x08),
  362. inw(ioaddr+0x0a));
  363. EL3WINDOW(1);
  364. }
  365. /* Reset and restore all of the 3c589 registers. */
  366. static void tc589_reset(struct net_device *dev)
  367. {
  368. unsigned int ioaddr = dev->base_addr;
  369. int i;
  370. EL3WINDOW(0);
  371. outw(0x0001, ioaddr + 4); /* Activate board. */
  372. outw(0x3f00, ioaddr + 8); /* Set the IRQ line. */
  373. /* Set the station address in window 2. */
  374. EL3WINDOW(2);
  375. for (i = 0; i < 6; i++)
  376. outb(dev->dev_addr[i], ioaddr + i);
  377. tc589_set_xcvr(dev, dev->if_port);
  378. /* Switch to the stats window, and clear all stats by reading. */
  379. outw(StatsDisable, ioaddr + EL3_CMD);
  380. EL3WINDOW(6);
  381. for (i = 0; i < 9; i++)
  382. inb(ioaddr+i);
  383. inw(ioaddr + 10);
  384. inw(ioaddr + 12);
  385. /* Switch to register set 1 for normal use. */
  386. EL3WINDOW(1);
  387. set_rx_mode(dev);
  388. outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
  389. outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
  390. outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
  391. /* Allow status bits to be seen. */
  392. outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
  393. /* Ack all pending events, and set active indicator mask. */
  394. outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
  395. ioaddr + EL3_CMD);
  396. outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
  397. | AdapterFailure, ioaddr + EL3_CMD);
  398. }
  399. static void netdev_get_drvinfo(struct net_device *dev,
  400. struct ethtool_drvinfo *info)
  401. {
  402. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  403. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  404. snprintf(info->bus_info, sizeof(info->bus_info),
  405. "PCMCIA 0x%lx", dev->base_addr);
  406. }
  407. static const struct ethtool_ops netdev_ethtool_ops = {
  408. .get_drvinfo = netdev_get_drvinfo,
  409. };
  410. static int el3_config(struct net_device *dev, struct ifmap *map)
  411. {
  412. if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
  413. if (map->port <= 3) {
  414. dev->if_port = map->port;
  415. netdev_info(dev, "switched to %s port\n", if_names[dev->if_port]);
  416. tc589_set_xcvr(dev, dev->if_port);
  417. } else {
  418. return -EINVAL;
  419. }
  420. }
  421. return 0;
  422. }
  423. static int el3_open(struct net_device *dev)
  424. {
  425. struct el3_private *lp = netdev_priv(dev);
  426. struct pcmcia_device *link = lp->p_dev;
  427. if (!pcmcia_dev_present(link))
  428. return -ENODEV;
  429. link->open++;
  430. netif_start_queue(dev);
  431. tc589_reset(dev);
  432. timer_setup(&lp->media, media_check, 0);
  433. mod_timer(&lp->media, jiffies + HZ);
  434. dev_dbg(&link->dev, "%s: opened, status %4.4x.\n",
  435. dev->name, inw(dev->base_addr + EL3_STATUS));
  436. return 0;
  437. }
  438. static void el3_tx_timeout(struct net_device *dev)
  439. {
  440. unsigned int ioaddr = dev->base_addr;
  441. netdev_warn(dev, "Transmit timed out!\n");
  442. dump_status(dev);
  443. dev->stats.tx_errors++;
  444. netif_trans_update(dev); /* prevent tx timeout */
  445. /* Issue TX_RESET and TX_START commands. */
  446. tc589_wait_for_completion(dev, TxReset);
  447. outw(TxEnable, ioaddr + EL3_CMD);
  448. netif_wake_queue(dev);
  449. }
  450. static void pop_tx_status(struct net_device *dev)
  451. {
  452. unsigned int ioaddr = dev->base_addr;
  453. int i;
  454. /* Clear the Tx status stack. */
  455. for (i = 32; i > 0; i--) {
  456. u_char tx_status = inb(ioaddr + TX_STATUS);
  457. if (!(tx_status & 0x84))
  458. break;
  459. /* reset transmitter on jabber error or underrun */
  460. if (tx_status & 0x30)
  461. tc589_wait_for_completion(dev, TxReset);
  462. if (tx_status & 0x38) {
  463. netdev_dbg(dev, "transmit error: status 0x%02x\n", tx_status);
  464. outw(TxEnable, ioaddr + EL3_CMD);
  465. dev->stats.tx_aborted_errors++;
  466. }
  467. outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
  468. }
  469. }
  470. static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
  471. struct net_device *dev)
  472. {
  473. unsigned int ioaddr = dev->base_addr;
  474. struct el3_private *priv = netdev_priv(dev);
  475. unsigned long flags;
  476. netdev_dbg(dev, "el3_start_xmit(length = %ld) called, status %4.4x.\n",
  477. (long)skb->len, inw(ioaddr + EL3_STATUS));
  478. spin_lock_irqsave(&priv->lock, flags);
  479. dev->stats.tx_bytes += skb->len;
  480. /* Put out the doubleword header... */
  481. outw(skb->len, ioaddr + TX_FIFO);
  482. outw(0x00, ioaddr + TX_FIFO);
  483. /* ... and the packet rounded to a doubleword. */
  484. outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
  485. if (inw(ioaddr + TX_FREE) <= 1536) {
  486. netif_stop_queue(dev);
  487. /* Interrupt us when the FIFO has room for max-sized packet. */
  488. outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
  489. }
  490. pop_tx_status(dev);
  491. spin_unlock_irqrestore(&priv->lock, flags);
  492. dev_kfree_skb(skb);
  493. return NETDEV_TX_OK;
  494. }
  495. /* The EL3 interrupt handler. */
  496. static irqreturn_t el3_interrupt(int irq, void *dev_id)
  497. {
  498. struct net_device *dev = (struct net_device *) dev_id;
  499. struct el3_private *lp = netdev_priv(dev);
  500. unsigned int ioaddr;
  501. __u16 status;
  502. int i = 0, handled = 1;
  503. if (!netif_device_present(dev))
  504. return IRQ_NONE;
  505. ioaddr = dev->base_addr;
  506. netdev_dbg(dev, "interrupt, status %4.4x.\n", inw(ioaddr + EL3_STATUS));
  507. spin_lock(&lp->lock);
  508. while ((status = inw(ioaddr + EL3_STATUS)) &
  509. (IntLatch | RxComplete | StatsFull)) {
  510. if ((status & 0xe000) != 0x2000) {
  511. netdev_dbg(dev, "interrupt from dead card\n");
  512. handled = 0;
  513. break;
  514. }
  515. if (status & RxComplete)
  516. el3_rx(dev);
  517. if (status & TxAvailable) {
  518. netdev_dbg(dev, " TX room bit was handled.\n");
  519. /* There's room in the FIFO for a full-sized packet. */
  520. outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
  521. netif_wake_queue(dev);
  522. }
  523. if (status & TxComplete)
  524. pop_tx_status(dev);
  525. if (status & (AdapterFailure | RxEarly | StatsFull)) {
  526. /* Handle all uncommon interrupts. */
  527. if (status & StatsFull) /* Empty statistics. */
  528. update_stats(dev);
  529. if (status & RxEarly) {
  530. /* Rx early is unused. */
  531. el3_rx(dev);
  532. outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
  533. }
  534. if (status & AdapterFailure) {
  535. u16 fifo_diag;
  536. EL3WINDOW(4);
  537. fifo_diag = inw(ioaddr + 4);
  538. EL3WINDOW(1);
  539. netdev_warn(dev, "adapter failure, FIFO diagnostic register %04x.\n",
  540. fifo_diag);
  541. if (fifo_diag & 0x0400) {
  542. /* Tx overrun */
  543. tc589_wait_for_completion(dev, TxReset);
  544. outw(TxEnable, ioaddr + EL3_CMD);
  545. }
  546. if (fifo_diag & 0x2000) {
  547. /* Rx underrun */
  548. tc589_wait_for_completion(dev, RxReset);
  549. set_rx_mode(dev);
  550. outw(RxEnable, ioaddr + EL3_CMD);
  551. }
  552. outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
  553. }
  554. }
  555. if (++i > 10) {
  556. netdev_err(dev, "infinite loop in interrupt, status %4.4x.\n",
  557. status);
  558. /* Clear all interrupts */
  559. outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
  560. break;
  561. }
  562. /* Acknowledge the IRQ. */
  563. outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
  564. }
  565. lp->last_irq = jiffies;
  566. spin_unlock(&lp->lock);
  567. netdev_dbg(dev, "exiting interrupt, status %4.4x.\n",
  568. inw(ioaddr + EL3_STATUS));
  569. return IRQ_RETVAL(handled);
  570. }
  571. static void media_check(struct timer_list *t)
  572. {
  573. struct el3_private *lp = from_timer(lp, t, media);
  574. struct net_device *dev = lp->p_dev->priv;
  575. unsigned int ioaddr = dev->base_addr;
  576. u16 media, errs;
  577. unsigned long flags;
  578. if (!netif_device_present(dev))
  579. goto reschedule;
  580. /* Check for pending interrupt with expired latency timer: with
  581. * this, we can limp along even if the interrupt is blocked
  582. */
  583. if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
  584. (inb(ioaddr + EL3_TIMER) == 0xff)) {
  585. if (!lp->fast_poll)
  586. netdev_warn(dev, "interrupt(s) dropped!\n");
  587. local_irq_save(flags);
  588. el3_interrupt(dev->irq, dev);
  589. local_irq_restore(flags);
  590. lp->fast_poll = HZ;
  591. }
  592. if (lp->fast_poll) {
  593. lp->fast_poll--;
  594. lp->media.expires = jiffies + HZ/100;
  595. add_timer(&lp->media);
  596. return;
  597. }
  598. /* lp->lock guards the EL3 window. Window should always be 1 except
  599. * when the lock is held
  600. */
  601. spin_lock_irqsave(&lp->lock, flags);
  602. EL3WINDOW(4);
  603. media = inw(ioaddr+WN4_MEDIA) & 0xc810;
  604. /* Ignore collisions unless we've had no irq's recently */
  605. if (time_before(jiffies, lp->last_irq + HZ)) {
  606. media &= ~0x0010;
  607. } else {
  608. /* Try harder to detect carrier errors */
  609. EL3WINDOW(6);
  610. outw(StatsDisable, ioaddr + EL3_CMD);
  611. errs = inb(ioaddr + 0);
  612. outw(StatsEnable, ioaddr + EL3_CMD);
  613. dev->stats.tx_carrier_errors += errs;
  614. if (errs || (lp->media_status & 0x0010))
  615. media |= 0x0010;
  616. }
  617. if (media != lp->media_status) {
  618. if ((media & lp->media_status & 0x8000) &&
  619. ((lp->media_status ^ media) & 0x0800))
  620. netdev_info(dev, "%s link beat\n",
  621. (lp->media_status & 0x0800 ? "lost" : "found"));
  622. else if ((media & lp->media_status & 0x4000) &&
  623. ((lp->media_status ^ media) & 0x0010))
  624. netdev_info(dev, "coax cable %s\n",
  625. (lp->media_status & 0x0010 ? "ok" : "problem"));
  626. if (dev->if_port == 0) {
  627. if (media & 0x8000) {
  628. if (media & 0x0800)
  629. netdev_info(dev, "flipped to 10baseT\n");
  630. else
  631. tc589_set_xcvr(dev, 2);
  632. } else if (media & 0x4000) {
  633. if (media & 0x0010)
  634. tc589_set_xcvr(dev, 1);
  635. else
  636. netdev_info(dev, "flipped to 10base2\n");
  637. }
  638. }
  639. lp->media_status = media;
  640. }
  641. EL3WINDOW(1);
  642. spin_unlock_irqrestore(&lp->lock, flags);
  643. reschedule:
  644. lp->media.expires = jiffies + HZ;
  645. add_timer(&lp->media);
  646. }
  647. static struct net_device_stats *el3_get_stats(struct net_device *dev)
  648. {
  649. struct el3_private *lp = netdev_priv(dev);
  650. unsigned long flags;
  651. struct pcmcia_device *link = lp->p_dev;
  652. if (pcmcia_dev_present(link)) {
  653. spin_lock_irqsave(&lp->lock, flags);
  654. update_stats(dev);
  655. spin_unlock_irqrestore(&lp->lock, flags);
  656. }
  657. return &dev->stats;
  658. }
  659. /* Update statistics. We change to register window 6, so this should be run
  660. * single-threaded if the device is active. This is expected to be a rare
  661. * operation, and it's simpler for the rest of the driver to assume that
  662. * window 1 is always valid rather than use a special window-state variable.
  663. *
  664. * Caller must hold the lock for this
  665. */
  666. static void update_stats(struct net_device *dev)
  667. {
  668. unsigned int ioaddr = dev->base_addr;
  669. netdev_dbg(dev, "updating the statistics.\n");
  670. /* Turn off statistics updates while reading. */
  671. outw(StatsDisable, ioaddr + EL3_CMD);
  672. /* Switch to the stats window, and read everything. */
  673. EL3WINDOW(6);
  674. dev->stats.tx_carrier_errors += inb(ioaddr + 0);
  675. dev->stats.tx_heartbeat_errors += inb(ioaddr + 1);
  676. /* Multiple collisions. */
  677. inb(ioaddr + 2);
  678. dev->stats.collisions += inb(ioaddr + 3);
  679. dev->stats.tx_window_errors += inb(ioaddr + 4);
  680. dev->stats.rx_fifo_errors += inb(ioaddr + 5);
  681. dev->stats.tx_packets += inb(ioaddr + 6);
  682. /* Rx packets */
  683. inb(ioaddr + 7);
  684. /* Tx deferrals */
  685. inb(ioaddr + 8);
  686. /* Rx octets */
  687. inw(ioaddr + 10);
  688. /* Tx octets */
  689. inw(ioaddr + 12);
  690. /* Back to window 1, and turn statistics back on. */
  691. EL3WINDOW(1);
  692. outw(StatsEnable, ioaddr + EL3_CMD);
  693. }
  694. static int el3_rx(struct net_device *dev)
  695. {
  696. unsigned int ioaddr = dev->base_addr;
  697. int worklimit = 32;
  698. short rx_status;
  699. netdev_dbg(dev, "in rx_packet(), status %4.4x, rx_status %4.4x.\n",
  700. inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
  701. while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
  702. worklimit > 0) {
  703. worklimit--;
  704. if (rx_status & 0x4000) { /* Error, update stats. */
  705. short error = rx_status & 0x3800;
  706. dev->stats.rx_errors++;
  707. switch (error) {
  708. case 0x0000:
  709. dev->stats.rx_over_errors++;
  710. break;
  711. case 0x0800:
  712. dev->stats.rx_length_errors++;
  713. break;
  714. case 0x1000:
  715. dev->stats.rx_frame_errors++;
  716. break;
  717. case 0x1800:
  718. dev->stats.rx_length_errors++;
  719. break;
  720. case 0x2000:
  721. dev->stats.rx_frame_errors++;
  722. break;
  723. case 0x2800:
  724. dev->stats.rx_crc_errors++;
  725. break;
  726. }
  727. } else {
  728. short pkt_len = rx_status & 0x7ff;
  729. struct sk_buff *skb;
  730. skb = netdev_alloc_skb(dev, pkt_len + 5);
  731. netdev_dbg(dev, " Receiving packet size %d status %4.4x.\n",
  732. pkt_len, rx_status);
  733. if (skb != NULL) {
  734. skb_reserve(skb, 2);
  735. insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
  736. (pkt_len+3)>>2);
  737. skb->protocol = eth_type_trans(skb, dev);
  738. netif_rx(skb);
  739. dev->stats.rx_packets++;
  740. dev->stats.rx_bytes += pkt_len;
  741. } else {
  742. netdev_dbg(dev, "couldn't allocate a sk_buff of size %d.\n",
  743. pkt_len);
  744. dev->stats.rx_dropped++;
  745. }
  746. }
  747. /* Pop the top of the Rx FIFO */
  748. tc589_wait_for_completion(dev, RxDiscard);
  749. }
  750. if (worklimit == 0)
  751. netdev_warn(dev, "too much work in el3_rx!\n");
  752. return 0;
  753. }
  754. static void set_rx_mode(struct net_device *dev)
  755. {
  756. unsigned int ioaddr = dev->base_addr;
  757. u16 opts = SetRxFilter | RxStation | RxBroadcast;
  758. if (dev->flags & IFF_PROMISC)
  759. opts |= RxMulticast | RxProm;
  760. else if (!netdev_mc_empty(dev) || (dev->flags & IFF_ALLMULTI))
  761. opts |= RxMulticast;
  762. outw(opts, ioaddr + EL3_CMD);
  763. }
  764. static void set_multicast_list(struct net_device *dev)
  765. {
  766. struct el3_private *priv = netdev_priv(dev);
  767. unsigned long flags;
  768. spin_lock_irqsave(&priv->lock, flags);
  769. set_rx_mode(dev);
  770. spin_unlock_irqrestore(&priv->lock, flags);
  771. }
  772. static int el3_close(struct net_device *dev)
  773. {
  774. struct el3_private *lp = netdev_priv(dev);
  775. struct pcmcia_device *link = lp->p_dev;
  776. unsigned int ioaddr = dev->base_addr;
  777. dev_dbg(&link->dev, "%s: shutting down ethercard.\n", dev->name);
  778. if (pcmcia_dev_present(link)) {
  779. /* Turn off statistics ASAP. We update dev->stats below. */
  780. outw(StatsDisable, ioaddr + EL3_CMD);
  781. /* Disable the receiver and transmitter. */
  782. outw(RxDisable, ioaddr + EL3_CMD);
  783. outw(TxDisable, ioaddr + EL3_CMD);
  784. if (dev->if_port == 2)
  785. /* Turn off thinnet power. Green! */
  786. outw(StopCoax, ioaddr + EL3_CMD);
  787. else if (dev->if_port == 1) {
  788. /* Disable link beat and jabber */
  789. EL3WINDOW(4);
  790. outw(0, ioaddr + WN4_MEDIA);
  791. }
  792. /* Switching back to window 0 disables the IRQ. */
  793. EL3WINDOW(0);
  794. /* But we explicitly zero the IRQ line select anyway. */
  795. outw(0x0f00, ioaddr + WN0_IRQ);
  796. /* Check if the card still exists */
  797. if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
  798. update_stats(dev);
  799. }
  800. link->open--;
  801. netif_stop_queue(dev);
  802. del_timer_sync(&lp->media);
  803. return 0;
  804. }
  805. static const struct pcmcia_device_id tc589_ids[] = {
  806. PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0101, 0x0562),
  807. PCMCIA_MFC_DEVICE_PROD_ID1(0, "Motorola MARQUIS", 0xf03e4e77),
  808. PCMCIA_DEVICE_MANF_CARD(0x0101, 0x0589),
  809. PCMCIA_DEVICE_PROD_ID12("Farallon", "ENet", 0x58d93fc4, 0x992c2202),
  810. PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x0035, "cis/3CXEM556.cis"),
  811. PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x003d, "cis/3CXEM556.cis"),
  812. PCMCIA_DEVICE_NULL,
  813. };
  814. MODULE_DEVICE_TABLE(pcmcia, tc589_ids);
  815. static struct pcmcia_driver tc589_driver = {
  816. .owner = THIS_MODULE,
  817. .name = "3c589_cs",
  818. .probe = tc589_probe,
  819. .remove = tc589_detach,
  820. .id_table = tc589_ids,
  821. .suspend = tc589_suspend,
  822. .resume = tc589_resume,
  823. };
  824. module_pcmcia_driver(tc589_driver);