sk_mca.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:30k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* 
  2. net-3-driver for the SKNET MCA-based cards
  3. This is an extension to the Linux operating system, and is covered by the
  4. same GNU General Public License that covers that work.
  5. Copyright 1999 by Alfred Arnold (alfred@ccac.rwth-aachen.de, aarnold@elsa.de)
  6. This driver is based both on the 3C523 driver and the SK_G16 driver.
  7. paper sources:
  8.   'PC Hardware: Aufbau, Funktionsweise, Programmierung' by 
  9.   Hans-Peter Messmer for the basic Microchannel stuff
  10.   
  11.   'Linux Geraetetreiber' by Allesandro Rubini, Kalle Dalheimer
  12.   for help on Ethernet driver programming
  13.   'Ethernet/IEEE 802.3 Family 1992 World Network Data Book/Handbook' by AMD
  14.   for documentation on the AM7990 LANCE
  15.   'SKNET Personal Technisches Manual', Version 1.2 by Schneider&Koch
  16.   for documentation on the Junior board
  17.   'SK-NET MC2+ Technical Manual", Version 1.1 by Schneider&Koch for
  18.   documentation on the MC2 bord
  19.   
  20.   A big thank you to the S&K support for providing me so quickly with
  21.   documentation!
  22.   Also see http://www.syskonnect.com/
  23.   Missing things:
  24.   -> set debug level via ioctl instead of compile-time switches
  25.   -> I didn't follow the development of the 2.1.x kernels, so my
  26.      assumptions about which things changed with which kernel version 
  27.      are probably nonsense
  28. History:
  29.   May 16th, 1999
  30.    startup
  31.   May 22st, 1999
  32. added private structure, methods
  33.         begun building data structures in RAM
  34.   May 23nd, 1999
  35. can receive frames, send frames
  36.   May 24th, 1999
  37.         modularized intialization of LANCE
  38.         loadable as module
  39. still Tx problem :-(
  40.   May 26th, 1999
  41.    MC2 works
  42.    support for multiple devices
  43.    display media type for MC2+
  44.   May 28th, 1999
  45. fixed problem in GetLANCE leaving interrupts turned off
  46.         increase TX queue to 4 packets to improve send performance
  47.   May 29th, 1999
  48. a few corrections in statistics, caught rcvr overruns 
  49.         reinitialization of LANCE/board in critical situations
  50.         MCA info implemented
  51. implemented LANCE multicast filter
  52.   Jun 6th, 1999
  53. additions for Linux 2.2
  54.   Dec 25th, 1999
  55.    unfortunately there seem to be newer MC2+ boards that react
  56.    on IRQ 3/5/9/10 instead of 3/5/10/11, so we have to autoprobe
  57.    in questionable cases...
  58.   Dec 28th, 1999
  59. integrated patches from David Weinehall & Bill Wendling for 2.3
  60. kernels (isa_...functions).  Things are defined in a way that
  61.         it still works with 2.0.x 8-)
  62.   Dec 30th, 1999
  63. added handling of the remaining interrupt conditions.  That
  64.         should cure the spurious hangs.
  65.   Jan 30th, 2000
  66. newer kernels automatically probe more than one board, so the
  67. 'startslot' as a variable is also needed here
  68.   June 1st, 2000
  69. added changes for recent 2.3 kernels
  70.  *************************************************************************/
  71. #include <linux/kernel.h>
  72. #include <linux/sched.h>
  73. #include <linux/string.h>
  74. #include <linux/errno.h>
  75. #include <linux/ioport.h>
  76. #include <linux/slab.h>
  77. #include <linux/interrupt.h>
  78. #include <linux/delay.h>
  79. #include <linux/time.h>
  80. #include <linux/mca.h>
  81. #include <linux/init.h>
  82. #include <asm/processor.h>
  83. #include <asm/bitops.h>
  84. #include <asm/io.h>
  85. #include <linux/module.h>
  86. #include <linux/version.h>
  87. #include <linux/netdevice.h>
  88. #include <linux/etherdevice.h>
  89. #include <linux/skbuff.h>
  90. #define _SK_MCA_DRIVER_
  91. #include "sk_mca.h"
  92. /* ------------------------------------------------------------------------
  93.  * global static data - not more since we can handle multiple boards and
  94.  * have to pack all state info into the device struct!
  95.  * ------------------------------------------------------------------------ */
  96. static char *MediaNames[Media_Count] =
  97.     { "10Base2", "10BaseT", "10Base5", "Unknown" };
  98. static unsigned char poly[] =
  99.     { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0,
  100. 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0
  101. };
  102. /* ------------------------------------------------------------------------
  103.  * private subfunctions
  104.  * ------------------------------------------------------------------------ */
  105. /* dump parts of shared memory - only needed during debugging */
  106. #ifdef DEBUG
  107. static void dumpmem(struct SKMCA_NETDEV *dev, u32 start, u32 len)
  108. {
  109. int z;
  110. for (z = 0; z < len; z++) {
  111. if ((z & 15) == 0)
  112. printk("%04x:", z);
  113. printk(" %02x", SKMCA_READB(dev->mem_start + start + z));
  114. if ((z & 15) == 15)
  115. printk("n");
  116. }
  117. }
  118. /* print exact time - ditto */
  119. static void PrTime(void)
  120. {
  121. struct timeval tv;
  122. do_gettimeofday(&tv);
  123. printk("%9d:%06d: ", tv.tv_sec, tv.tv_usec);
  124. }
  125. #endif
  126. /* deduce resources out of POS registers */
  127. static void __init getaddrs(int slot, int junior, int *base, int *irq,
  128.      skmca_medium * medium)
  129. {
  130. u_char pos0, pos1, pos2;
  131. if (junior) {
  132. pos0 = mca_read_stored_pos(slot, 2);
  133. *base = ((pos0 & 0x0e) << 13) + 0xc0000;
  134. *irq = ((pos0 & 0x10) >> 4) + 10;
  135. *medium = Media_Unknown;
  136. } else {
  137. /* reset POS 104 Bits 0+1 so the shared memory region goes to the
  138.    configured area between 640K and 1M.  Afterwards, enable the MC2.
  139.    I really don't know what rode SK to do this... */
  140. mca_write_pos(slot, 4,
  141.       mca_read_stored_pos(slot, 4) & 0xfc);
  142. mca_write_pos(slot, 2,
  143.       mca_read_stored_pos(slot, 2) | 0x01);
  144. pos1 = mca_read_stored_pos(slot, 3);
  145. pos2 = mca_read_stored_pos(slot, 4);
  146. *base = ((pos1 & 0x07) << 14) + 0xc0000;
  147. switch (pos2 & 0x0c) {
  148. case 0:
  149. *irq = 3;
  150. break;
  151. case 4:
  152. *irq = 5;
  153. break;
  154. case 8:
  155. *irq = -10;
  156. break;
  157. case 12:
  158. *irq = -11;
  159. break;
  160. }
  161. *medium = (pos2 >> 6) & 3;
  162. }
  163. }
  164. /* check for both cards:
  165.    When the MC2 is turned off, it was configured for more than 15MB RAM,
  166.    is disabled and won't get detected using the standard probe.  We
  167.    therefore have to scan the slots manually :-( */
  168. static int __init dofind(int *junior, int firstslot)
  169. {
  170. int slot;
  171. unsigned int id;
  172. for (slot = firstslot; slot < MCA_MAX_SLOT_NR; slot++) {
  173. id = mca_read_stored_pos(slot, 0)
  174.     + (((unsigned int) mca_read_stored_pos(slot, 1)) << 8);
  175. *junior = 0;
  176. if (id == SKNET_MCA_ID)
  177. return slot;
  178. *junior = 1;
  179. if (id == SKNET_JUNIOR_MCA_ID)
  180. return slot;
  181. }
  182. return MCA_NOTFOUND;
  183. }
  184. /* reset the whole board */
  185. static void ResetBoard(struct SKMCA_NETDEV *dev)
  186. {
  187. skmca_priv *priv = (skmca_priv *) dev->priv;
  188. SKMCA_WRITEB(CTRL_RESET_ON, priv->ctrladdr);
  189. udelay(10);
  190. SKMCA_WRITEB(CTRL_RESET_OFF, priv->ctrladdr);
  191. }
  192. /* wait for LANCE interface to become not busy */
  193. static int WaitLANCE(struct SKMCA_NETDEV *dev)
  194. {
  195. skmca_priv *priv = (skmca_priv *) dev->priv;
  196. int t = 0;
  197. while ((SKMCA_READB(priv->ctrladdr) & STAT_IO_BUSY) ==
  198.        STAT_IO_BUSY) {
  199. udelay(1);
  200. if (++t > 1000) {
  201. printk("%s: LANCE access timeout", dev->name);
  202. return 0;
  203. }
  204. }
  205. return 1;
  206. }
  207. /* set LANCE register - must be atomic */
  208. static void SetLANCE(struct SKMCA_NETDEV *dev, u16 addr, u16 value)
  209. {
  210. skmca_priv *priv = (skmca_priv *) dev->priv;
  211. unsigned long flags;
  212. /* disable interrupts */
  213. save_flags(flags);
  214. cli();
  215. /* wait until no transfer is pending */
  216. WaitLANCE(dev);
  217. /* transfer register address to RAP */
  218. SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP,
  219.      priv->ctrladdr);
  220. SKMCA_WRITEW(addr, priv->ioregaddr);
  221. SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
  222. udelay(1);
  223. WaitLANCE(dev);
  224. /* transfer data to register */
  225. SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_DATA,
  226.      priv->ctrladdr);
  227. SKMCA_WRITEW(value, priv->ioregaddr);
  228. SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
  229. udelay(1);
  230. WaitLANCE(dev);
  231. /* reenable interrupts */
  232. restore_flags(flags);
  233. }
  234. /* get LANCE register */
  235. static u16 GetLANCE(struct SKMCA_NETDEV *dev, u16 addr)
  236. {
  237. skmca_priv *priv = (skmca_priv *) dev->priv;
  238. unsigned long flags;
  239. unsigned int res;
  240. /* disable interrupts */
  241. save_flags(flags);
  242. cli();
  243. /* wait until no transfer is pending */
  244. WaitLANCE(dev);
  245. /* transfer register address to RAP */
  246. SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP,
  247.      priv->ctrladdr);
  248. SKMCA_WRITEW(addr, priv->ioregaddr);
  249. SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
  250. udelay(1);
  251. WaitLANCE(dev);
  252. /* transfer data from register */
  253. SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_READ | CTRL_ADR_DATA,
  254.      priv->ctrladdr);
  255. SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
  256. udelay(1);
  257. WaitLANCE(dev);
  258. res = SKMCA_READW(priv->ioregaddr);
  259. /* reenable interrupts */
  260. restore_flags(flags);
  261. return res;
  262. }
  263. /* build up descriptors in shared RAM */
  264. static void InitDscrs(struct SKMCA_NETDEV *dev)
  265. {
  266. u32 bufaddr;
  267. /* Set up Tx descriptors. The board has only 16K RAM so bits 16..23
  268.    are always 0. */
  269. bufaddr = RAM_DATABASE;
  270. {
  271. LANCE_TxDescr descr;
  272. int z;
  273. for (z = 0; z < TXCOUNT; z++) {
  274. descr.LowAddr = bufaddr;
  275. descr.Flags = 0;
  276. descr.Len = 0xf000;
  277. descr.Status = 0;
  278. SKMCA_TOIO(dev->mem_start + RAM_TXBASE +
  279.    (z * sizeof(LANCE_TxDescr)), &descr,
  280.    sizeof(LANCE_TxDescr));
  281. SKMCA_SETIO(dev->mem_start + bufaddr, 0,
  282.     RAM_BUFSIZE);
  283. bufaddr += RAM_BUFSIZE;
  284. }
  285. }
  286. /* do the same for the Rx descriptors */
  287. {
  288. LANCE_RxDescr descr;
  289. int z;
  290. for (z = 0; z < RXCOUNT; z++) {
  291. descr.LowAddr = bufaddr;
  292. descr.Flags = RXDSCR_FLAGS_OWN;
  293. descr.MaxLen = -RAM_BUFSIZE;
  294. descr.Len = 0;
  295. SKMCA_TOIO(dev->mem_start + RAM_RXBASE +
  296.    (z * sizeof(LANCE_RxDescr)), &descr,
  297.    sizeof(LANCE_RxDescr));
  298. SKMCA_SETIO(dev->mem_start + bufaddr, 0,
  299.     RAM_BUFSIZE);
  300. bufaddr += RAM_BUFSIZE;
  301. }
  302. }
  303. }
  304. /* calculate the hash bit position for a given multicast address
  305.    taken more or less directly from the AMD datasheet... */
  306. static void UpdateCRC(unsigned char *CRC, int bit)
  307. {
  308. int j;
  309. /* shift CRC one bit */
  310. memmove(CRC + 1, CRC, 32 * sizeof(unsigned char));
  311. CRC[0] = 0;
  312. /* if bit XOR controlbit = 1, set CRC = CRC XOR polynomial */
  313. if (bit ^ CRC[32])
  314. for (j = 0; j < 32; j++)
  315. CRC[j] ^= poly[j];
  316. }
  317. static unsigned int GetHash(char *address)
  318. {
  319. unsigned char CRC[33];
  320. int i, byte, hashcode;
  321. /* a multicast address has bit 0 in the first byte set */
  322. if ((address[0] & 1) == 0)
  323. return -1;
  324. /* initialize CRC */
  325. memset(CRC, 1, sizeof(CRC));
  326. /* loop through address bits */
  327. for (byte = 0; byte < 6; byte++)
  328. for (i = 0; i < 8; i++)
  329. UpdateCRC(CRC, (address[byte] >> i) & 1);
  330. /* hashcode is the 6 least significant bits of the CRC */
  331. hashcode = 0;
  332. for (i = 0; i < 6; i++)
  333. hashcode = (hashcode << 1) + CRC[i];
  334. return hashcode;
  335. }
  336. /* feed ready-built initialization block into LANCE */
  337. static void InitLANCE(struct SKMCA_NETDEV *dev)
  338. {
  339. skmca_priv *priv = (skmca_priv *) dev->priv;
  340. /* build up descriptors. */
  341. InitDscrs(dev);
  342. /* next RX descriptor to be read is the first one.  Since the LANCE
  343.    will start from the beginning after initialization, we have to 
  344.    reset out pointers too. */
  345. priv->nextrx = 0;
  346. /* no TX descriptors active */
  347. priv->nexttxput = priv->nexttxdone = priv->txbusy = 0;
  348. /* set up the LANCE bus control register - constant for SKnet boards */
  349. SetLANCE(dev, LANCE_CSR3,
  350.  CSR3_BSWAP_OFF | CSR3_ALE_LOW | CSR3_BCON_HOLD);
  351. /* write address of initialization block into LANCE */
  352. SetLANCE(dev, LANCE_CSR1, RAM_INITBASE & 0xffff);
  353. SetLANCE(dev, LANCE_CSR2, (RAM_INITBASE >> 16) & 0xff);
  354. /* we don't get ready until the LANCE has read the init block */
  355. #if (LINUX_VERSION_CODE >= 0x02032a)
  356. netif_stop_queue(dev);
  357. #else
  358. dev->tbusy = 1;
  359. #endif
  360. /* let LANCE read the initialization block.  LANCE is ready
  361.    when we receive the corresponding interrupt. */
  362. SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_INIT);
  363. }
  364. /* stop the LANCE so we can reinitialize it */
  365. static void StopLANCE(struct SKMCA_NETDEV *dev)
  366. {
  367. /* can't take frames any more */
  368. #if (LINUX_VERSION_CODE >= 0x02032a)
  369. netif_stop_queue(dev);
  370. #else
  371. dev->tbusy = 1;
  372. #endif
  373. /* disable interrupts, stop it */
  374. SetLANCE(dev, LANCE_CSR0, CSR0_STOP);
  375. }
  376. /* initialize card and LANCE for proper operation */
  377. static void InitBoard(struct SKMCA_NETDEV *dev)
  378. {
  379. LANCE_InitBlock block;
  380. /* Lay out the shared RAM - first we create the init block for the LANCE.
  381.    We do not overwrite it later because we need it again when we switch
  382.    promiscous mode on/off. */
  383. block.Mode = 0;
  384. if (dev->flags & IFF_PROMISC)
  385. block.Mode |= LANCE_INIT_PROM;
  386. memcpy(block.PAdr, dev->dev_addr, 6);
  387. memset(block.LAdrF, 0, sizeof(block.LAdrF));
  388. block.RdrP = (RAM_RXBASE & 0xffffff) | (LRXCOUNT << 29);
  389. block.TdrP = (RAM_TXBASE & 0xffffff) | (LTXCOUNT << 29);
  390. SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block, sizeof(block));
  391. /* initialize LANCE. Implicitly sets up other structures in RAM. */
  392. InitLANCE(dev);
  393. }
  394. /* deinitialize card and LANCE */
  395. static void DeinitBoard(struct SKMCA_NETDEV *dev)
  396. {
  397. /* stop LANCE */
  398. StopLANCE(dev);
  399. /* reset board */
  400. ResetBoard(dev);
  401. }
  402. /* probe for device's irq */
  403. static int __init ProbeIRQ(struct SKMCA_NETDEV *dev)
  404. {
  405. unsigned long imaskval, njiffies, irq;
  406. u16 csr0val;
  407. /* enable all interrupts */
  408. imaskval = probe_irq_on();
  409. /* initialize the board. Wait for interrupt 'Initialization done'. */
  410. ResetBoard(dev);
  411. InitBoard(dev);
  412. njiffies = jiffies + 100;
  413. do {
  414. csr0val = GetLANCE(dev, LANCE_CSR0);
  415. }
  416. while (((csr0val & CSR0_IDON) == 0) && (jiffies != njiffies));
  417. /* turn of interrupts again */
  418. irq = probe_irq_off(imaskval);
  419. /* if we found something, ack the interrupt */
  420. if (irq)
  421. SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_IDON);
  422. /* back to idle state */
  423. DeinitBoard(dev);
  424. return irq;
  425. }
  426. /* ------------------------------------------------------------------------
  427.  * interrupt handler(s)
  428.  * ------------------------------------------------------------------------ */
  429. /* LANCE has read initialization block -> start it */
  430. static u16 irqstart_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
  431. {
  432. /* now we're ready to transmit */
  433. #if (LINUX_VERSION_CODE >= 0x02032a)
  434. netif_wake_queue(dev);
  435. #else
  436. dev->tbusy = 0;
  437. #endif
  438. /* reset IDON bit, start LANCE */
  439. SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_IDON | CSR0_STRT);
  440. return GetLANCE(dev, LANCE_CSR0);
  441. }
  442. /* did we loose blocks due to a FIFO overrun ? */
  443. static u16 irqmiss_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
  444. {
  445. skmca_priv *priv = (skmca_priv *) dev->priv;
  446. /* update statistics */
  447. priv->stat.rx_fifo_errors++;
  448. /* reset MISS bit */
  449. SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_MISS);
  450. return GetLANCE(dev, LANCE_CSR0);
  451. }
  452. /* receive interrupt */
  453. static u16 irqrx_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
  454. {
  455. skmca_priv *priv = (skmca_priv *) dev->priv;
  456. LANCE_RxDescr descr;
  457. unsigned int descraddr;
  458. /* run through queue until we reach a descriptor we do not own */
  459. descraddr = RAM_RXBASE + (priv->nextrx * sizeof(LANCE_RxDescr));
  460. while (1) {
  461. /* read descriptor */
  462. SKMCA_FROMIO(&descr, dev->mem_start + descraddr,
  463.      sizeof(LANCE_RxDescr));
  464. /* if we reach a descriptor we do not own, we're done */
  465. if ((descr.Flags & RXDSCR_FLAGS_OWN) != 0)
  466. break;
  467. #ifdef DEBUG
  468. PrTime();
  469. printk("Receive packet on descr %d len %dn", priv->nextrx,
  470.        descr.Len);
  471. #endif
  472. /* erroneous packet ? */
  473. if ((descr.Flags & RXDSCR_FLAGS_ERR) != 0) {
  474. priv->stat.rx_errors++;
  475. if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
  476. priv->stat.rx_crc_errors++;
  477. else if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
  478. priv->stat.rx_frame_errors++;
  479. else if ((descr.Flags & RXDSCR_FLAGS_OFLO) != 0)
  480. priv->stat.rx_fifo_errors++;
  481. }
  482. /* good packet ? */
  483. else {
  484. struct sk_buff *skb;
  485. skb = dev_alloc_skb(descr.Len + 2);
  486. if (skb == NULL)
  487. priv->stat.rx_dropped++;
  488. else {
  489. SKMCA_FROMIO(skb_put(skb, descr.Len),
  490.      dev->mem_start +
  491.      descr.LowAddr, descr.Len);
  492. skb->dev = dev;
  493. skb->protocol = eth_type_trans(skb, dev);
  494. skb->ip_summed = CHECKSUM_NONE;
  495. priv->stat.rx_packets++;
  496. #if LINUX_VERSION_CODE >= 0x020119 /* byte counters for >= 2.1.25 */
  497. priv->stat.rx_bytes += descr.Len;
  498. #endif
  499. netif_rx(skb);
  500. dev->last_rx = jiffies;
  501. }
  502. }
  503. /* give descriptor back to LANCE */
  504. descr.Len = 0;
  505. descr.Flags |= RXDSCR_FLAGS_OWN;
  506. /* update descriptor in shared RAM */
  507. SKMCA_TOIO(dev->mem_start + descraddr, &descr,
  508.    sizeof(LANCE_RxDescr));
  509. /* go to next descriptor */
  510. priv->nextrx++;
  511. descraddr += sizeof(LANCE_RxDescr);
  512. if (priv->nextrx >= RXCOUNT) {
  513. priv->nextrx = 0;
  514. descraddr = RAM_RXBASE;
  515. }
  516. }
  517. /* reset RINT bit */
  518. SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_RINT);
  519. return GetLANCE(dev, LANCE_CSR0);
  520. }
  521. /* transmit interrupt */
  522. static u16 irqtx_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
  523. {
  524. skmca_priv *priv = (skmca_priv *) dev->priv;
  525. LANCE_TxDescr descr;
  526. unsigned int descraddr;
  527. /* check descriptors at most until no busy one is left */
  528. descraddr =
  529.     RAM_TXBASE + (priv->nexttxdone * sizeof(LANCE_TxDescr));
  530. while (priv->txbusy > 0) {
  531. /* read descriptor */
  532. SKMCA_FROMIO(&descr, dev->mem_start + descraddr,
  533.      sizeof(LANCE_TxDescr));
  534. /* if the LANCE still owns this one, we've worked out all sent packets */
  535. if ((descr.Flags & TXDSCR_FLAGS_OWN) != 0)
  536. break;
  537. #ifdef DEBUG
  538. PrTime();
  539. printk("Send packet done on descr %dn", priv->nexttxdone);
  540. #endif
  541. /* update statistics */
  542. if ((descr.Flags & TXDSCR_FLAGS_ERR) == 0) {
  543. priv->stat.tx_packets++;
  544. #if LINUX_VERSION_CODE >= 0x020119 /* byte counters for >= 2.1.25 */
  545. priv->stat.tx_bytes++;
  546. #endif
  547. } else {
  548. priv->stat.tx_errors++;
  549. if ((descr.Status & TXDSCR_STATUS_UFLO) != 0) {
  550. priv->stat.tx_fifo_errors++;
  551. InitLANCE(dev);
  552. }
  553. else
  554.     if ((descr.Status & TXDSCR_STATUS_LCOL) !=
  555. 0) priv->stat.tx_window_errors++;
  556. else if ((descr.Status & TXDSCR_STATUS_LCAR) != 0)
  557. priv->stat.tx_carrier_errors++;
  558. else if ((descr.Status & TXDSCR_STATUS_RTRY) != 0)
  559. priv->stat.tx_aborted_errors++;
  560. }
  561. /* go to next descriptor */
  562. priv->nexttxdone++;
  563. descraddr += sizeof(LANCE_TxDescr);
  564. if (priv->nexttxdone >= TXCOUNT) {
  565. priv->nexttxdone = 0;
  566. descraddr = RAM_TXBASE;
  567. }
  568. priv->txbusy--;
  569. }
  570. /* reset TX interrupt bit */
  571. SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_TINT);
  572. oldcsr0 = GetLANCE(dev, LANCE_CSR0);
  573. /* at least one descriptor is freed.  Therefore we can accept
  574.    a new one */
  575. /* inform upper layers we're in business again */
  576. #if (LINUX_VERSION_CODE >= 0x02032a)
  577. netif_wake_queue(dev);
  578. #else
  579. dev->tbusy = 0;
  580. mark_bh(NET_BH);
  581. #endif
  582. return oldcsr0;
  583. }
  584. /* general interrupt entry */
  585. static void irq_handler(int irq, void *device, struct pt_regs *regs)
  586. {
  587. struct SKMCA_NETDEV *dev = (struct SKMCA_NETDEV *) device;
  588. u16 csr0val;
  589. /* read CSR0 to get interrupt cause */
  590. csr0val = GetLANCE(dev, LANCE_CSR0);
  591. /* in case we're not meant... */
  592. if ((csr0val & CSR0_INTR) == 0)
  593. return;
  594. #if (LINUX_VERSION_CODE >= 0x02032a)
  595. #if 0
  596. set_bit(LINK_STATE_RXSEM, &dev->state);
  597. #endif
  598. #else
  599. dev->interrupt = 1;
  600. #endif
  601. /* loop through the interrupt bits until everything is clear */
  602. do {
  603. if ((csr0val & CSR0_IDON) != 0)
  604. csr0val = irqstart_handler(dev, csr0val);
  605. if ((csr0val & CSR0_RINT) != 0)
  606. csr0val = irqrx_handler(dev, csr0val);
  607. if ((csr0val & CSR0_MISS) != 0)
  608. csr0val = irqmiss_handler(dev, csr0val);
  609. if ((csr0val & CSR0_TINT) != 0)
  610. csr0val = irqtx_handler(dev, csr0val);
  611. if ((csr0val & CSR0_MERR) != 0) {
  612. SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_MERR);
  613. csr0val = GetLANCE(dev, LANCE_CSR0);
  614. }
  615. if ((csr0val & CSR0_BABL) != 0) {
  616. SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_BABL);
  617. csr0val = GetLANCE(dev, LANCE_CSR0);
  618. }
  619. }
  620. while ((csr0val & CSR0_INTR) != 0);
  621. #if (LINUX_VERSION_CODE >= 0x02032a)
  622. #if 0
  623. clear_bit(LINK_STATE_RXSEM, &dev->state);
  624. #endif
  625. #else
  626. dev->interrupt = 0;
  627. #endif
  628. }
  629. /* ------------------------------------------------------------------------
  630.  * driver methods
  631.  * ------------------------------------------------------------------------ */
  632. /* MCA info */
  633. static int skmca_getinfo(char *buf, int slot, void *d)
  634. {
  635. int len = 0, i;
  636. struct SKMCA_NETDEV *dev = (struct SKMCA_NETDEV *) d;
  637. skmca_priv *priv;
  638. /* can't say anything about an uninitialized device... */
  639. if (dev == NULL)
  640. return len;
  641. if (dev->priv == NULL)
  642. return len;
  643. priv = (skmca_priv *) dev->priv;
  644. /* print info */
  645. len += sprintf(buf + len, "IRQ: %dn", priv->realirq);
  646. len += sprintf(buf + len, "Memory: %#lx-%#lxn", dev->mem_start,
  647.        dev->mem_end - 1);
  648. len +=
  649.     sprintf(buf + len, "Transceiver: %sn",
  650.     MediaNames[priv->medium]);
  651. len += sprintf(buf + len, "Device: %sn", dev->name);
  652. len += sprintf(buf + len, "MAC address:");
  653. for (i = 0; i < 6; i++)
  654. len += sprintf(buf + len, " %02x", dev->dev_addr[i]);
  655. buf[len++] = 'n';
  656. buf[len] = 0;
  657. return len;
  658. }
  659. /* open driver.  Means also initialization and start of LANCE */
  660. static int skmca_open(struct SKMCA_NETDEV *dev)
  661. {
  662. int result;
  663. skmca_priv *priv = (skmca_priv *) dev->priv;
  664. /* register resources - only necessary for IRQ */
  665. result =
  666.     request_irq(priv->realirq, irq_handler,
  667. SA_SHIRQ | SA_SAMPLE_RANDOM, "sk_mca", dev);
  668. if (result != 0) {
  669. printk("%s: failed to register irq %dn", dev->name,
  670.        dev->irq);
  671. return result;
  672. }
  673. dev->irq = priv->realirq;
  674. /* set up the card and LANCE */
  675. InitBoard(dev);
  676. /* set up flags */
  677. #if (LINUX_VERSION_CODE >= 0x02032a)
  678. netif_start_queue(dev);
  679. #else
  680. dev->interrupt = 0;
  681. dev->tbusy = 0;
  682. dev->start = 0;
  683. MOD_INC_USE_COUNT;
  684. #endif
  685. return 0;
  686. }
  687. /* close driver.  Shut down board and free allocated resources */
  688. static int skmca_close(struct SKMCA_NETDEV *dev)
  689. {
  690. /* turn off board */
  691. DeinitBoard(dev);
  692. /* release resources */
  693. if (dev->irq != 0)
  694. free_irq(dev->irq, dev);
  695. dev->irq = 0;
  696. #if (LINUX_VERSION_CODE < 0x02032a)
  697. MOD_DEC_USE_COUNT;
  698. #endif
  699. return 0;
  700. }
  701. /* transmit a block. */
  702. static int skmca_tx(struct sk_buff *skb, struct SKMCA_NETDEV *dev)
  703. {
  704. skmca_priv *priv = (skmca_priv *) dev->priv;
  705. LANCE_TxDescr descr;
  706. unsigned int address;
  707. int tmplen, retval = 0;
  708. unsigned long flags;
  709. /* if we get called with a NULL descriptor, the Ethernet layer thinks 
  710.    our card is stuck an we should reset it.  We'll do this completely: */
  711. if (skb == NULL) {
  712. DeinitBoard(dev);
  713. InitBoard(dev);
  714. return 0; /* don't try to free the block here ;-) */
  715. }
  716. /* is there space in the Tx queue ? If no, the upper layer gave us a
  717.    packet in spite of us not being ready and is really in trouble.
  718.    We'll do the dropping for him: */
  719. if (priv->txbusy >= TXCOUNT) {
  720. priv->stat.tx_dropped++;
  721. retval = -EIO;
  722. goto tx_done;
  723. }
  724. /* get TX descriptor */
  725. address = RAM_TXBASE + (priv->nexttxput * sizeof(LANCE_TxDescr));
  726. SKMCA_FROMIO(&descr, dev->mem_start + address,
  727.      sizeof(LANCE_TxDescr));
  728. /* enter packet length as 2s complement - assure minimum length */
  729. tmplen = skb->len;
  730. if (tmplen < 60)
  731. tmplen = 60;
  732. descr.Len = 65536 - tmplen;
  733. /* copy filler into RAM - in case we're filling up... 
  734.    we're filling a bit more than necessary, but that doesn't harm
  735.    since the buffer is far larger... */
  736. if (tmplen > skb->len) {
  737. char *fill = "NetBSD is a nice OS too! ";
  738. unsigned int destoffs = 0, l = strlen(fill);
  739. while (destoffs < tmplen) {
  740. SKMCA_TOIO(dev->mem_start + descr.LowAddr +
  741.    destoffs, fill, l);
  742. destoffs += l;
  743. }
  744. }
  745. /* do the real data copying */
  746. SKMCA_TOIO(dev->mem_start + descr.LowAddr, skb->data, skb->len);
  747. /* hand descriptor over to LANCE - this is the first and last chunk */
  748. descr.Flags =
  749.     TXDSCR_FLAGS_OWN | TXDSCR_FLAGS_STP | TXDSCR_FLAGS_ENP;
  750. #ifdef DEBUG
  751. PrTime();
  752. printk("Send packet on descr %d len %dn", priv->nexttxput,
  753.        skb->len);
  754. #endif
  755. /* one more descriptor busy */
  756. save_flags(flags);
  757. cli();
  758. priv->nexttxput++;
  759. if (priv->nexttxput >= TXCOUNT)
  760. priv->nexttxput = 0;
  761. priv->txbusy++;
  762. /* are we saturated ? */
  763. if (priv->txbusy >= TXCOUNT)
  764. #if (LINUX_VERSION_CODE >= 0x02032a)
  765. netif_stop_queue(dev);
  766. #else
  767. dev->tbusy = 1;
  768. #endif
  769. /* write descriptor back to RAM */
  770. SKMCA_TOIO(dev->mem_start + address, &descr,
  771.    sizeof(LANCE_TxDescr));
  772. /* if no descriptors were active, give the LANCE a hint to read it
  773.    immediately */
  774. if (priv->txbusy == 0)
  775. SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_TDMD);
  776. restore_flags(flags);
  777.       tx_done:
  778. /* When did that change exactly ? */
  779. #if LINUX_VERSION_CODE >= 0x020200
  780. dev_kfree_skb(skb);
  781. #else
  782. dev_kfree_skb(skb, FREE_WRITE);
  783. #endif
  784. return retval;
  785. }
  786. /* return pointer to Ethernet statistics */
  787. static struct net_device_stats *skmca_stats(struct SKMCA_NETDEV *dev)
  788. {
  789. skmca_priv *priv = (skmca_priv *) dev->priv;
  790. return &(priv->stat);
  791. }
  792. /* we don't support runtime reconfiguration, since an MCA card can
  793.    be unambigously identified by its POS registers. */
  794. static int skmca_config(struct SKMCA_NETDEV *dev, struct ifmap *map)
  795. {
  796. return 0;
  797. }
  798. /* switch receiver mode.  We use the LANCE's multicast filter to prefilter
  799.    multicast addresses. */
  800. static void skmca_set_multicast_list(struct SKMCA_NETDEV *dev)
  801. {
  802. LANCE_InitBlock block;
  803. /* first stop the LANCE... */
  804. StopLANCE(dev);
  805. /* ...then modify the initialization block... */
  806. SKMCA_FROMIO(&block, dev->mem_start + RAM_INITBASE, sizeof(block));
  807. if (dev->flags & IFF_PROMISC)
  808. block.Mode |= LANCE_INIT_PROM;
  809. else
  810. block.Mode &= ~LANCE_INIT_PROM;
  811. if (dev->flags & IFF_ALLMULTI) { /* get all multicasts */
  812. memset(block.LAdrF, 8, 0xff);
  813. } else { /* get selected/no multicasts */
  814. struct dev_mc_list *mptr;
  815. int code;
  816. memset(block.LAdrF, 8, 0x00);
  817. for (mptr = dev->mc_list; mptr != NULL; mptr = mptr->next) {
  818. code = GetHash(mptr->dmi_addr);
  819. block.LAdrF[(code >> 3) & 7] |= 1 << (code & 7);
  820. }
  821. }
  822. SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block, sizeof(block));
  823. /* ...then reinit LANCE with the correct flags */
  824. InitLANCE(dev);
  825. }
  826. /* ------------------------------------------------------------------------
  827.  * hardware check
  828.  * ------------------------------------------------------------------------ */
  829. static int startslot; /* counts through slots when probing multiple devices */
  830. int __init skmca_probe(struct SKMCA_NETDEV *dev)
  831. {
  832. int force_detect = 0;
  833. int junior, slot, i;
  834. int base = 0, irq = 0;
  835. skmca_priv *priv;
  836. skmca_medium medium;
  837. /* can't work without an MCA bus ;-) */
  838. if (MCA_bus == 0)
  839. return -ENODEV;
  840. SET_MODULE_OWNER(dev);
  841. /* start address of 1 --> forced detection */
  842. if (dev->mem_start == 1)
  843. force_detect = 1;
  844. /* search through slots */
  845. if (dev != NULL) {
  846. base = dev->mem_start;
  847. irq = dev->irq;
  848. }
  849. slot = dofind(&junior, startslot);
  850. while (slot != -1) {
  851. /* deduce card addresses */
  852. getaddrs(slot, junior, &base, &irq, &medium);
  853. #if LINUX_VERSION_CODE >= 0x020300
  854. /* slot already in use ? */
  855. if (mca_is_adapter_used(slot)) {
  856. slot = dofind(&junior, slot + 1);
  857. continue;
  858. }
  859. #endif
  860. /* were we looking for something different ? */
  861. if ((dev->irq != 0) || (dev->mem_start != 0)) {
  862. if ((dev->irq != 0) && (dev->irq != irq)) {
  863. slot = dofind(&junior, slot + 1);
  864. continue;
  865. }
  866. if ((dev->mem_start != 0)
  867.     && (dev->mem_start != base)) {
  868. slot = dofind(&junior, slot + 1);
  869. continue;
  870. }
  871. }
  872. /* found something that matches */
  873. break;
  874. }
  875. /* nothing found ? */
  876. if (slot == -1)
  877. return ((base != 0) || (irq != 0)) ? ENXIO : ENODEV;
  878. /* make procfs entries */
  879. if (junior)
  880. mca_set_adapter_name(slot,
  881.      "SKNET junior MC2 Ethernet Adapter");
  882. else
  883. mca_set_adapter_name(slot, "SKNET MC2+ Ethernet Adapter");
  884. mca_set_adapter_procfn(slot, (MCA_ProcFn) skmca_getinfo, dev);
  885. #if LINUX_VERSION_CODE >= 0x020200
  886. mca_mark_as_used(slot);
  887. #endif
  888. /* announce success */
  889. printk("%s: SKNet %s adapter found in slot %dn", dev->name,
  890.        junior ? "Junior MC2" : "MC2+", slot + 1);
  891. /* allocate structure */
  892. priv = dev->priv =
  893.     (skmca_priv *) kmalloc(sizeof(skmca_priv), GFP_KERNEL);
  894. if (!priv)
  895. return -ENOMEM;
  896. priv->slot = slot;
  897. priv->macbase = base + 0x3fc0;
  898. priv->ioregaddr = base + 0x3ff0;
  899. priv->ctrladdr = base + 0x3ff2;
  900. priv->cmdaddr = base + 0x3ff3;
  901. priv->medium = medium;
  902. memset(&(priv->stat), 0, sizeof(struct net_device_stats));
  903. /* set base + irq for this device (irq not allocated so far) */
  904. dev->irq = 0;
  905. dev->mem_start = base;
  906. dev->mem_end = base + 0x4000;
  907. /* autoprobe ? */
  908. if (irq < 0) {
  909. int nirq;
  910. printk
  911.     ("%s: ambigous POS bit combination, must probe for IRQ...n",
  912.      dev->name);
  913. nirq = ProbeIRQ(dev);
  914. if (nirq <= 0)
  915. printk("%s: IRQ probe failed, assuming IRQ %d",
  916.        dev->name, priv->realirq = -irq);
  917. else
  918. priv->realirq = nirq;
  919. } else
  920. priv->realirq = irq;
  921. /* set methods */
  922. dev->open = skmca_open;
  923. dev->stop = skmca_close;
  924. dev->set_config = skmca_config;
  925. dev->hard_start_xmit = skmca_tx;
  926. dev->do_ioctl = NULL;
  927. dev->get_stats = skmca_stats;
  928. dev->set_multicast_list = skmca_set_multicast_list;
  929. dev->flags |= IFF_MULTICAST;
  930. /* generic setup */
  931. ether_setup(dev);
  932. /* copy out MAC address */
  933. for (i = 0; i < 6; i++)
  934. dev->dev_addr[i] = SKMCA_READB(priv->macbase + (i << 1));
  935. /* print config */
  936. printk("%s: IRQ %d, memory %#lx-%#lx, "
  937.        "MAC address %02x:%02x:%02x:%02x:%02x:%02x.n",
  938.        dev->name, priv->realirq, dev->mem_start, dev->mem_end - 1,
  939.        dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
  940.        dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
  941. printk("%s: %s mediumn", dev->name, MediaNames[priv->medium]);
  942. /* reset board */
  943. ResetBoard(dev);
  944. startslot = slot + 1;
  945. return 0;
  946. }
  947. /* ------------------------------------------------------------------------
  948.  * modularization support
  949.  * ------------------------------------------------------------------------ */
  950. #ifdef MODULE
  951. MODULE_LICENSE("GPL");
  952. #define DEVMAX 5
  953. #if (LINUX_VERSION_CODE >= 0x020369)
  954. static struct SKMCA_NETDEV moddevs[DEVMAX] =
  955.     { {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
  956. {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
  957. {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
  958. {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
  959. {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe}
  960. };
  961. #else
  962. static char NameSpace[8 * DEVMAX];
  963. static struct SKMCA_NETDEV moddevs[DEVMAX] =
  964.     { {NameSpace + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
  965. {NameSpace + 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
  966. {NameSpace + 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
  967. {NameSpace + 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
  968. {NameSpace + 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe}
  969. };
  970. #endif
  971. int irq;
  972. int io;
  973. int init_module(void)
  974. {
  975. int z, res;
  976. startslot = 0;
  977. for (z = 0; z < DEVMAX; z++) {
  978. strcpy(moddevs[z].name, "     ");
  979. res = register_netdev(moddevs + z);
  980. if (res != 0)
  981. return (z > 0) ? 0 : -EIO;
  982. }
  983. return 0;
  984. }
  985. void cleanup_module(void)
  986. {
  987. struct SKMCA_NETDEV *dev;
  988. skmca_priv *priv;
  989. int z;
  990. if (MOD_IN_USE) {
  991. printk("cannot unload, module in usen");
  992. return;
  993. }
  994. for (z = 0; z < DEVMAX; z++) {
  995. dev = moddevs + z;
  996. if (dev->priv != NULL) {
  997. priv = (skmca_priv *) dev->priv;
  998. DeinitBoard(dev);
  999. if (dev->irq != 0)
  1000. free_irq(dev->irq, dev);
  1001. dev->irq = 0;
  1002. unregister_netdev(dev);
  1003. #if LINUX_VERSION_CODE >= 0x020200
  1004. mca_mark_as_unused(priv->slot);
  1005. #endif
  1006. mca_set_adapter_procfn(priv->slot, NULL, NULL);
  1007. kfree(dev->priv);
  1008. dev->priv = NULL;
  1009. }
  1010. }
  1011. }
  1012. #endif /* MODULE */