3c589_cs.c 25 KB

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