ni5010.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /* ni5010.c: A network driver for the MiCom-Interlan NI5010 ethercard.
  2. *
  3. * Copyright 1996,1997,2006 Jan-Pascal van Best and Andreas Mohr.
  4. *
  5. * This software may be used and distributed according to the terms
  6. * of the GNU General Public License, incorporated herein by reference.
  7. *
  8. * The authors may be reached as:
  9. * janpascal@vanbest.org andi@lisas.de
  10. *
  11. * Sources:
  12. * Donald Becker's "skeleton.c"
  13. * Crynwr ni5010 packet driver
  14. *
  15. * Changes:
  16. * v0.0: First test version
  17. * v0.1: First working version
  18. * v0.2:
  19. * v0.3->v0.90: Now demand setting io and irq when loading as module
  20. * 970430 v0.91: modified for Linux 2.1.14
  21. * v0.92: Implemented Andreas' (better) NI5010 probe
  22. * 970503 v0.93: Fixed auto-irq failure on warm reboot (JB)
  23. * 970623 v1.00: First kernel version (AM)
  24. * 970814 v1.01: Added detection of onboard receive buffer size (AM)
  25. * 060611 v1.02: slight cleanup: email addresses, driver modernization.
  26. * Bugs:
  27. * - not SMP-safe (no locking of I/O accesses)
  28. * - Note that you have to patch ifconfig for the new /proc/net/dev
  29. * format. It gives incorrect stats otherwise.
  30. *
  31. * To do:
  32. * Fix all bugs :-)
  33. * Move some stuff to chipset_init()
  34. * Handle xmt errors other than collisions
  35. * Complete merge with Andreas' driver
  36. * Implement ring buffers (Is this useful? You can't squeeze
  37. * too many packet in a 2k buffer!)
  38. * Implement DMA (Again, is this useful? Some docs say DMA is
  39. * slower than programmed I/O)
  40. *
  41. * Compile with:
  42. * gcc -O2 -fomit-frame-pointer -m486 -D__KERNEL__ \
  43. * -DMODULE -c ni5010.c
  44. *
  45. * Insert with e.g.:
  46. * insmod ni5010.ko io=0x300 irq=5
  47. */
  48. #include <linux/module.h>
  49. #include <linux/kernel.h>
  50. #include <linux/string.h>
  51. #include <linux/errno.h>
  52. #include <linux/ioport.h>
  53. #include <linux/slab.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/delay.h>
  56. #include <linux/init.h>
  57. #include <linux/bitops.h>
  58. #include <asm/io.h>
  59. #include <asm/dma.h>
  60. #include <linux/netdevice.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/skbuff.h>
  63. #include "ni5010.h"
  64. static const char boardname[] = "NI5010";
  65. static char version[] __initdata =
  66. "ni5010.c: v1.02 20060611 Jan-Pascal van Best and Andreas Mohr\n";
  67. /* bufsize_rcv == 0 means autoprobing */
  68. static unsigned int bufsize_rcv;
  69. #define JUMPERED_INTERRUPTS /* IRQ line jumpered on board */
  70. #undef JUMPERED_DMA /* No DMA used */
  71. #undef FULL_IODETECT /* Only detect in portlist */
  72. #ifndef FULL_IODETECT
  73. /* A zero-terminated list of I/O addresses to be probed. */
  74. static unsigned int ports[] __initdata =
  75. { 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0 };
  76. #endif
  77. /* Use 0 for production, 1 for verification, >2 for debug */
  78. #ifndef NI5010_DEBUG
  79. #define NI5010_DEBUG 0
  80. #endif
  81. /* Information that needs to be kept for each board. */
  82. struct ni5010_local {
  83. struct net_device_stats stats;
  84. int o_pkt_size;
  85. spinlock_t lock;
  86. };
  87. /* Index to functions, as function prototypes. */
  88. static int ni5010_probe1(struct net_device *dev, int ioaddr);
  89. static int ni5010_open(struct net_device *dev);
  90. static int ni5010_send_packet(struct sk_buff *skb, struct net_device *dev);
  91. static irqreturn_t ni5010_interrupt(int irq, void *dev_id);
  92. static void ni5010_rx(struct net_device *dev);
  93. static void ni5010_timeout(struct net_device *dev);
  94. static int ni5010_close(struct net_device *dev);
  95. static struct net_device_stats *ni5010_get_stats(struct net_device *dev);
  96. static void ni5010_set_multicast_list(struct net_device *dev);
  97. static void reset_receiver(struct net_device *dev);
  98. static int process_xmt_interrupt(struct net_device *dev);
  99. #define tx_done(dev) 1
  100. static void hardware_send_packet(struct net_device *dev, char *buf, int length, int pad);
  101. static void chipset_init(struct net_device *dev, int startp);
  102. static void dump_packet(void *buf, int len);
  103. static void ni5010_show_registers(struct net_device *dev);
  104. static int io;
  105. static int irq;
  106. struct net_device * __init ni5010_probe(int unit)
  107. {
  108. struct net_device *dev = alloc_etherdev(sizeof(struct ni5010_local));
  109. int *port;
  110. int err = 0;
  111. if (!dev)
  112. return ERR_PTR(-ENOMEM);
  113. if (unit >= 0) {
  114. sprintf(dev->name, "eth%d", unit);
  115. netdev_boot_setup_check(dev);
  116. io = dev->base_addr;
  117. irq = dev->irq;
  118. }
  119. PRINTK2((KERN_DEBUG "%s: Entering ni5010_probe\n", dev->name));
  120. SET_MODULE_OWNER(dev);
  121. if (io > 0x1ff) { /* Check a single specified location. */
  122. err = ni5010_probe1(dev, io);
  123. } else if (io != 0) { /* Don't probe at all. */
  124. err = -ENXIO;
  125. } else {
  126. #ifdef FULL_IODETECT
  127. for (io=0x200; io<0x400 && ni5010_probe1(dev, io) ; io+=0x20)
  128. ;
  129. if (io == 0x400)
  130. err = -ENODEV;
  131. #else
  132. for (port = ports; *port && ni5010_probe1(dev, *port); port++)
  133. ;
  134. if (!*port)
  135. err = -ENODEV;
  136. #endif /* FULL_IODETECT */
  137. }
  138. if (err)
  139. goto out;
  140. err = register_netdev(dev);
  141. if (err)
  142. goto out1;
  143. return dev;
  144. out1:
  145. release_region(dev->base_addr, NI5010_IO_EXTENT);
  146. out:
  147. free_netdev(dev);
  148. return ERR_PTR(err);
  149. }
  150. static inline int rd_port(int ioaddr)
  151. {
  152. inb(IE_RBUF);
  153. return inb(IE_SAPROM);
  154. }
  155. static void __init trigger_irq(int ioaddr)
  156. {
  157. outb(0x00, EDLC_RESET); /* Clear EDLC hold RESET state */
  158. outb(0x00, IE_RESET); /* Board reset */
  159. outb(0x00, EDLC_XMASK); /* Disable all Xmt interrupts */
  160. outb(0x00, EDLC_RMASK); /* Disable all Rcv interrupt */
  161. outb(0xff, EDLC_XCLR); /* Clear all pending Xmt interrupts */
  162. outb(0xff, EDLC_RCLR); /* Clear all pending Rcv interrupts */
  163. /*
  164. * Transmit packet mode: Ignore parity, Power xcvr,
  165. * Enable loopback
  166. */
  167. outb(XMD_IG_PAR | XMD_T_MODE | XMD_LBC, EDLC_XMODE);
  168. outb(RMD_BROADCAST, EDLC_RMODE); /* Receive normal&broadcast */
  169. outb(XM_ALL, EDLC_XMASK); /* Enable all Xmt interrupts */
  170. udelay(50); /* FIXME: Necessary? */
  171. outb(MM_EN_XMT|MM_MUX, IE_MMODE); /* Start transmission */
  172. }
  173. /*
  174. * This is the real probe routine. Linux has a history of friendly device
  175. * probes on the ISA bus. A good device probes avoids doing writes, and
  176. * verifies that the correct device exists and functions.
  177. */
  178. static int __init ni5010_probe1(struct net_device *dev, int ioaddr)
  179. {
  180. static unsigned version_printed;
  181. struct ni5010_local *lp;
  182. int i;
  183. unsigned int data = 0;
  184. int boguscount = 40;
  185. int err = -ENODEV;
  186. dev->base_addr = ioaddr;
  187. dev->irq = irq;
  188. if (!request_region(ioaddr, NI5010_IO_EXTENT, boardname))
  189. return -EBUSY;
  190. /*
  191. * This is no "official" probe method, I've rather tested which
  192. * probe works best with my seven NI5010 cards
  193. * (they have very different serial numbers)
  194. * Suggestions or failure reports are very, very welcome !
  195. * But I think it is a relatively good probe method
  196. * since it doesn't use any "outb"
  197. * It should be nearly 100% reliable !
  198. * well-known WARNING: this probe method (like many others)
  199. * will hang the system if a NE2000 card region is probed !
  200. *
  201. * - Andreas
  202. */
  203. PRINTK2((KERN_DEBUG "%s: entering ni5010_probe1(%#3x)\n",
  204. dev->name, ioaddr));
  205. if (inb(ioaddr+0) == 0xff)
  206. goto out;
  207. while ( (rd_port(ioaddr) & rd_port(ioaddr) & rd_port(ioaddr) &
  208. rd_port(ioaddr) & rd_port(ioaddr) & rd_port(ioaddr)) != 0xff)
  209. {
  210. if (boguscount-- == 0)
  211. goto out;
  212. }
  213. PRINTK2((KERN_DEBUG "%s: I/O #1 passed!\n", dev->name));
  214. for (i=0; i<32; i++)
  215. if ( (data = rd_port(ioaddr)) != 0xff) break;
  216. if (data==0xff)
  217. goto out;
  218. PRINTK2((KERN_DEBUG "%s: I/O #2 passed!\n", dev->name));
  219. if ((data != SA_ADDR0) || (rd_port(ioaddr) != SA_ADDR1) ||
  220. (rd_port(ioaddr) != SA_ADDR2))
  221. goto out;
  222. for (i=0; i<4; i++)
  223. rd_port(ioaddr);
  224. if ( (rd_port(ioaddr) != NI5010_MAGICVAL1) ||
  225. (rd_port(ioaddr) != NI5010_MAGICVAL2) )
  226. goto out;
  227. PRINTK2((KERN_DEBUG "%s: I/O #3 passed!\n", dev->name));
  228. if (NI5010_DEBUG && version_printed++ == 0)
  229. printk(KERN_INFO "%s", version);
  230. printk("NI5010 ethercard probe at 0x%x: ", ioaddr);
  231. dev->base_addr = ioaddr;
  232. for (i=0; i<6; i++) {
  233. outw(i, IE_GP);
  234. printk("%2.2x ", dev->dev_addr[i] = inb(IE_SAPROM));
  235. }
  236. PRINTK2((KERN_DEBUG "%s: I/O #4 passed!\n", dev->name));
  237. #ifdef JUMPERED_INTERRUPTS
  238. if (dev->irq == 0xff)
  239. ;
  240. else if (dev->irq < 2) {
  241. unsigned long irq_mask;
  242. PRINTK2((KERN_DEBUG "%s: I/O #5 passed!\n", dev->name));
  243. irq_mask = probe_irq_on();
  244. trigger_irq(ioaddr);
  245. mdelay(20);
  246. dev->irq = probe_irq_off(irq_mask);
  247. PRINTK2((KERN_DEBUG "%s: I/O #6 passed!\n", dev->name));
  248. if (dev->irq == 0) {
  249. err = -EAGAIN;
  250. printk(KERN_WARNING "%s: no IRQ found!\n", dev->name);
  251. goto out;
  252. }
  253. PRINTK2((KERN_DEBUG "%s: I/O #7 passed!\n", dev->name));
  254. } else if (dev->irq == 2) {
  255. dev->irq = 9;
  256. }
  257. #endif /* JUMPERED_INTERRUPTS */
  258. PRINTK2((KERN_DEBUG "%s: I/O #9 passed!\n", dev->name));
  259. /* DMA is not supported (yet?), so no use detecting it */
  260. lp = netdev_priv(dev);
  261. spin_lock_init(&lp->lock);
  262. PRINTK2((KERN_DEBUG "%s: I/O #10 passed!\n", dev->name));
  263. /* get the size of the onboard receive buffer
  264. * higher addresses than bufsize are wrapped into real buffer
  265. * i.e. data for offs. 0x801 is written to 0x1 with a 2K onboard buffer
  266. */
  267. if (!bufsize_rcv) {
  268. outb(1, IE_MMODE); /* Put Rcv buffer on system bus */
  269. outw(0, IE_GP); /* Point GP at start of packet */
  270. outb(0, IE_RBUF); /* set buffer byte 0 to 0 */
  271. for (i = 1; i < 0xff; i++) {
  272. outw(i << 8, IE_GP); /* Point GP at packet size to be tested */
  273. outb(i, IE_RBUF);
  274. outw(0x0, IE_GP); /* Point GP at start of packet */
  275. data = inb(IE_RBUF);
  276. if (data == i) break;
  277. }
  278. bufsize_rcv = i << 8;
  279. outw(0, IE_GP); /* Point GP at start of packet */
  280. outb(0, IE_RBUF); /* set buffer byte 0 to 0 again */
  281. }
  282. printk("-> bufsize rcv/xmt=%d/%d\n", bufsize_rcv, NI5010_BUFSIZE);
  283. memset(dev->priv, 0, sizeof(struct ni5010_local));
  284. dev->open = ni5010_open;
  285. dev->stop = ni5010_close;
  286. dev->hard_start_xmit = ni5010_send_packet;
  287. dev->get_stats = ni5010_get_stats;
  288. dev->set_multicast_list = ni5010_set_multicast_list;
  289. dev->tx_timeout = ni5010_timeout;
  290. dev->watchdog_timeo = HZ/20;
  291. dev->flags &= ~IFF_MULTICAST; /* Multicast doesn't work */
  292. /* Shut up the ni5010 */
  293. outb(0, EDLC_RMASK); /* Mask all receive interrupts */
  294. outb(0, EDLC_XMASK); /* Mask all xmit interrupts */
  295. outb(0xff, EDLC_RCLR); /* Kill all pending rcv interrupts */
  296. outb(0xff, EDLC_XCLR); /* Kill all pending xmt interrupts */
  297. printk(KERN_INFO "%s: NI5010 found at 0x%x, using IRQ %d", dev->name, ioaddr, dev->irq);
  298. if (dev->dma)
  299. printk(" & DMA %d", dev->dma);
  300. printk(".\n");
  301. return 0;
  302. out:
  303. release_region(dev->base_addr, NI5010_IO_EXTENT);
  304. return err;
  305. }
  306. /*
  307. * Open/initialize the board. This is called (in the current kernel)
  308. * sometime after booting when the 'ifconfig' program is run.
  309. *
  310. * This routine should set everything up anew at each open, even
  311. * registers that "should" only need to be set once at boot, so that
  312. * there is a non-reboot way to recover if something goes wrong.
  313. */
  314. static int ni5010_open(struct net_device *dev)
  315. {
  316. int ioaddr = dev->base_addr;
  317. int i;
  318. PRINTK2((KERN_DEBUG "%s: entering ni5010_open()\n", dev->name));
  319. if (request_irq(dev->irq, &ni5010_interrupt, 0, boardname, dev)) {
  320. printk(KERN_WARNING "%s: Cannot get irq %#2x\n", dev->name, dev->irq);
  321. return -EAGAIN;
  322. }
  323. PRINTK3((KERN_DEBUG "%s: passed open() #1\n", dev->name));
  324. /*
  325. * Always allocate the DMA channel after the IRQ,
  326. * and clean up on failure.
  327. */
  328. #ifdef JUMPERED_DMA
  329. if (request_dma(dev->dma, cardname)) {
  330. printk(KERN_WARNING "%s: Cannot get dma %#2x\n", dev->name, dev->dma);
  331. free_irq(dev->irq, NULL);
  332. return -EAGAIN;
  333. }
  334. #endif /* JUMPERED_DMA */
  335. PRINTK3((KERN_DEBUG "%s: passed open() #2\n", dev->name));
  336. /* Reset the hardware here. Don't forget to set the station address. */
  337. outb(RS_RESET, EDLC_RESET); /* Hold up EDLC_RESET while configing board */
  338. outb(0, IE_RESET); /* Hardware reset of ni5010 board */
  339. outb(XMD_LBC, EDLC_XMODE); /* Only loopback xmits */
  340. PRINTK3((KERN_DEBUG "%s: passed open() #3\n", dev->name));
  341. /* Set the station address */
  342. for(i = 0;i < 6; i++) {
  343. outb(dev->dev_addr[i], EDLC_ADDR + i);
  344. }
  345. PRINTK3((KERN_DEBUG "%s: Initialising ni5010\n", dev->name));
  346. outb(0, EDLC_XMASK); /* No xmit interrupts for now */
  347. outb(XMD_IG_PAR | XMD_T_MODE | XMD_LBC, EDLC_XMODE);
  348. /* Normal packet xmit mode */
  349. outb(0xff, EDLC_XCLR); /* Clear all pending xmit interrupts */
  350. outb(RMD_BROADCAST, EDLC_RMODE);
  351. /* Receive broadcast and normal packets */
  352. reset_receiver(dev); /* Ready ni5010 for receiving packets */
  353. outb(0, EDLC_RESET); /* Un-reset the ni5010 */
  354. netif_start_queue(dev);
  355. if (NI5010_DEBUG) ni5010_show_registers(dev);
  356. PRINTK((KERN_DEBUG "%s: open successful\n", dev->name));
  357. return 0;
  358. }
  359. static void reset_receiver(struct net_device *dev)
  360. {
  361. int ioaddr = dev->base_addr;
  362. PRINTK3((KERN_DEBUG "%s: resetting receiver\n", dev->name));
  363. outw(0, IE_GP); /* Receive packet at start of buffer */
  364. outb(0xff, EDLC_RCLR); /* Clear all pending rcv interrupts */
  365. outb(0, IE_MMODE); /* Put EDLC to rcv buffer */
  366. outb(MM_EN_RCV, IE_MMODE); /* Enable rcv */
  367. outb(0xff, EDLC_RMASK); /* Enable all rcv interrupts */
  368. }
  369. static void ni5010_timeout(struct net_device *dev)
  370. {
  371. printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
  372. tx_done(dev) ? "IRQ conflict" : "network cable problem");
  373. /* Try to restart the adaptor. */
  374. /* FIXME: Give it a real kick here */
  375. chipset_init(dev, 1);
  376. dev->trans_start = jiffies;
  377. netif_wake_queue(dev);
  378. }
  379. static int ni5010_send_packet(struct sk_buff *skb, struct net_device *dev)
  380. {
  381. int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
  382. PRINTK2((KERN_DEBUG "%s: entering ni5010_send_packet\n", dev->name));
  383. /*
  384. * Block sending
  385. */
  386. netif_stop_queue(dev);
  387. hardware_send_packet(dev, (unsigned char *)skb->data, skb->len, length-skb->len);
  388. dev->trans_start = jiffies;
  389. dev_kfree_skb (skb);
  390. return 0;
  391. }
  392. /*
  393. * The typical workload of the driver:
  394. * Handle the network interface interrupts.
  395. */
  396. static irqreturn_t ni5010_interrupt(int irq, void *dev_id)
  397. {
  398. struct net_device *dev = dev_id;
  399. struct ni5010_local *lp;
  400. int ioaddr, status;
  401. int xmit_was_error = 0;
  402. PRINTK2((KERN_DEBUG "%s: entering ni5010_interrupt\n", dev->name));
  403. ioaddr = dev->base_addr;
  404. lp = netdev_priv(dev);
  405. spin_lock(&lp->lock);
  406. status = inb(IE_ISTAT);
  407. PRINTK3((KERN_DEBUG "%s: IE_ISTAT = %#02x\n", dev->name, status));
  408. if ((status & IS_R_INT) == 0) ni5010_rx(dev);
  409. if ((status & IS_X_INT) == 0) {
  410. xmit_was_error = process_xmt_interrupt(dev);
  411. }
  412. if ((status & IS_DMA_INT) == 0) {
  413. PRINTK((KERN_DEBUG "%s: DMA complete (?)\n", dev->name));
  414. outb(0, IE_DMA_RST); /* Reset DMA int */
  415. }
  416. if (!xmit_was_error)
  417. reset_receiver(dev);
  418. spin_unlock(&lp->lock);
  419. return IRQ_HANDLED;
  420. }
  421. static void dump_packet(void *buf, int len)
  422. {
  423. int i;
  424. printk(KERN_DEBUG "Packet length = %#4x\n", len);
  425. for (i = 0; i < len; i++){
  426. if (i % 16 == 0) printk(KERN_DEBUG "%#4.4x", i);
  427. if (i % 2 == 0) printk(" ");
  428. printk("%2.2x", ((unsigned char *)buf)[i]);
  429. if (i % 16 == 15) printk("\n");
  430. }
  431. printk("\n");
  432. return;
  433. }
  434. /* We have a good packet, get it out of the buffer. */
  435. static void ni5010_rx(struct net_device *dev)
  436. {
  437. struct ni5010_local *lp = netdev_priv(dev);
  438. int ioaddr = dev->base_addr;
  439. unsigned char rcv_stat;
  440. struct sk_buff *skb;
  441. int i_pkt_size;
  442. PRINTK2((KERN_DEBUG "%s: entering ni5010_rx()\n", dev->name));
  443. rcv_stat = inb(EDLC_RSTAT);
  444. PRINTK3((KERN_DEBUG "%s: EDLC_RSTAT = %#2x\n", dev->name, rcv_stat));
  445. if ( (rcv_stat & RS_VALID_BITS) != RS_PKT_OK) {
  446. PRINTK((KERN_INFO "%s: receive error.\n", dev->name));
  447. lp->stats.rx_errors++;
  448. if (rcv_stat & RS_RUNT) lp->stats.rx_length_errors++;
  449. if (rcv_stat & RS_ALIGN) lp->stats.rx_frame_errors++;
  450. if (rcv_stat & RS_CRC_ERR) lp->stats.rx_crc_errors++;
  451. if (rcv_stat & RS_OFLW) lp->stats.rx_fifo_errors++;
  452. outb(0xff, EDLC_RCLR); /* Clear the interrupt */
  453. return;
  454. }
  455. outb(0xff, EDLC_RCLR); /* Clear the interrupt */
  456. i_pkt_size = inw(IE_RCNT);
  457. if (i_pkt_size > ETH_FRAME_LEN || i_pkt_size < 10 ) {
  458. PRINTK((KERN_DEBUG "%s: Packet size error, packet size = %#4.4x\n",
  459. dev->name, i_pkt_size));
  460. lp->stats.rx_errors++;
  461. lp->stats.rx_length_errors++;
  462. return;
  463. }
  464. /* Malloc up new buffer. */
  465. skb = dev_alloc_skb(i_pkt_size + 3);
  466. if (skb == NULL) {
  467. printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
  468. lp->stats.rx_dropped++;
  469. return;
  470. }
  471. skb->dev = dev;
  472. skb_reserve(skb, 2);
  473. /* Read packet into buffer */
  474. outb(MM_MUX, IE_MMODE); /* Rcv buffer to system bus */
  475. outw(0, IE_GP); /* Seek to beginning of packet */
  476. insb(IE_RBUF, skb_put(skb, i_pkt_size), i_pkt_size);
  477. if (NI5010_DEBUG >= 4)
  478. dump_packet(skb->data, skb->len);
  479. skb->protocol = eth_type_trans(skb,dev);
  480. netif_rx(skb);
  481. dev->last_rx = jiffies;
  482. lp->stats.rx_packets++;
  483. lp->stats.rx_bytes += i_pkt_size;
  484. PRINTK2((KERN_DEBUG "%s: Received packet, size=%#4.4x\n",
  485. dev->name, i_pkt_size));
  486. }
  487. static int process_xmt_interrupt(struct net_device *dev)
  488. {
  489. struct ni5010_local *lp = netdev_priv(dev);
  490. int ioaddr = dev->base_addr;
  491. int xmit_stat;
  492. PRINTK2((KERN_DEBUG "%s: entering process_xmt_interrupt\n", dev->name));
  493. xmit_stat = inb(EDLC_XSTAT);
  494. PRINTK3((KERN_DEBUG "%s: EDLC_XSTAT = %2.2x\n", dev->name, xmit_stat));
  495. outb(0, EDLC_XMASK); /* Disable xmit IRQ's */
  496. outb(0xff, EDLC_XCLR); /* Clear all pending xmit IRQ's */
  497. if (xmit_stat & XS_COLL){
  498. PRINTK((KERN_DEBUG "%s: collision detected, retransmitting\n",
  499. dev->name));
  500. outw(NI5010_BUFSIZE - lp->o_pkt_size, IE_GP);
  501. /* outb(0, IE_MMODE); */ /* xmt buf on sysbus FIXME: needed ? */
  502. outb(MM_EN_XMT | MM_MUX, IE_MMODE);
  503. outb(XM_ALL, EDLC_XMASK); /* Enable xmt IRQ's */
  504. lp->stats.collisions++;
  505. return 1;
  506. }
  507. /* FIXME: handle other xmt error conditions */
  508. lp->stats.tx_packets++;
  509. lp->stats.tx_bytes += lp->o_pkt_size;
  510. netif_wake_queue(dev);
  511. PRINTK2((KERN_DEBUG "%s: sent packet, size=%#4.4x\n",
  512. dev->name, lp->o_pkt_size));
  513. return 0;
  514. }
  515. /* The inverse routine to ni5010_open(). */
  516. static int ni5010_close(struct net_device *dev)
  517. {
  518. int ioaddr = dev->base_addr;
  519. PRINTK2((KERN_DEBUG "%s: entering ni5010_close\n", dev->name));
  520. #ifdef JUMPERED_INTERRUPTS
  521. free_irq(dev->irq, NULL);
  522. #endif
  523. /* Put card in held-RESET state */
  524. outb(0, IE_MMODE);
  525. outb(RS_RESET, EDLC_RESET);
  526. netif_stop_queue(dev);
  527. PRINTK((KERN_DEBUG "%s: %s closed down\n", dev->name, boardname));
  528. return 0;
  529. }
  530. /* Get the current statistics. This may be called with the card open or
  531. closed. */
  532. static struct net_device_stats *ni5010_get_stats(struct net_device *dev)
  533. {
  534. struct ni5010_local *lp = netdev_priv(dev);
  535. PRINTK2((KERN_DEBUG "%s: entering ni5010_get_stats\n", dev->name));
  536. if (NI5010_DEBUG) ni5010_show_registers(dev);
  537. /* cli(); */
  538. /* Update the statistics from the device registers. */
  539. /* We do this in the interrupt handler */
  540. /* sti(); */
  541. return &lp->stats;
  542. }
  543. /* Set or clear the multicast filter for this adaptor.
  544. num_addrs == -1 Promiscuous mode, receive all packets
  545. num_addrs == 0 Normal mode, clear multicast list
  546. num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  547. best-effort filtering.
  548. */
  549. static void ni5010_set_multicast_list(struct net_device *dev)
  550. {
  551. short ioaddr = dev->base_addr;
  552. PRINTK2((KERN_DEBUG "%s: entering set_multicast_list\n", dev->name));
  553. if (dev->flags&IFF_PROMISC || dev->flags&IFF_ALLMULTI) {
  554. dev->flags |= IFF_PROMISC;
  555. outb(RMD_PROMISC, EDLC_RMODE); /* Enable promiscuous mode */
  556. PRINTK((KERN_DEBUG "%s: Entering promiscuous mode\n", dev->name));
  557. } else if (dev->mc_list) {
  558. /* Sorry, multicast not supported */
  559. PRINTK((KERN_DEBUG "%s: No multicast, entering broadcast mode\n", dev->name));
  560. outb(RMD_BROADCAST, EDLC_RMODE);
  561. } else {
  562. PRINTK((KERN_DEBUG "%s: Entering broadcast mode\n", dev->name));
  563. outb(RMD_BROADCAST, EDLC_RMODE); /* Disable promiscuous mode, use normal mode */
  564. }
  565. }
  566. static void hardware_send_packet(struct net_device *dev, char *buf, int length, int pad)
  567. {
  568. struct ni5010_local *lp = netdev_priv(dev);
  569. int ioaddr = dev->base_addr;
  570. unsigned long flags;
  571. unsigned int buf_offs;
  572. PRINTK2((KERN_DEBUG "%s: entering hardware_send_packet\n", dev->name));
  573. if (length > ETH_FRAME_LEN) {
  574. PRINTK((KERN_WARNING "%s: packet too large, not possible\n",
  575. dev->name));
  576. return;
  577. }
  578. if (NI5010_DEBUG) ni5010_show_registers(dev);
  579. if (inb(IE_ISTAT) & IS_EN_XMT) {
  580. PRINTK((KERN_WARNING "%s: sending packet while already transmitting, not possible\n",
  581. dev->name));
  582. return;
  583. }
  584. if (NI5010_DEBUG > 3) dump_packet(buf, length);
  585. buf_offs = NI5010_BUFSIZE - length - pad;
  586. spin_lock_irqsave(&lp->lock, flags);
  587. lp->o_pkt_size = length + pad;
  588. outb(0, EDLC_RMASK); /* Mask all receive interrupts */
  589. outb(0, IE_MMODE); /* Put Xmit buffer on system bus */
  590. outb(0xff, EDLC_RCLR); /* Clear out pending rcv interrupts */
  591. outw(buf_offs, IE_GP); /* Point GP at start of packet */
  592. outsb(IE_XBUF, buf, length); /* Put data in buffer */
  593. while(pad--)
  594. outb(0, IE_XBUF);
  595. outw(buf_offs, IE_GP); /* Rewrite where packet starts */
  596. /* should work without that outb() (Crynwr used it) */
  597. /*outb(MM_MUX, IE_MMODE);*/ /* Xmt buffer to EDLC bus */
  598. outb(MM_EN_XMT | MM_MUX, IE_MMODE); /* Begin transmission */
  599. outb(XM_ALL, EDLC_XMASK); /* Cause interrupt after completion or fail */
  600. spin_unlock_irqrestore(&lp->lock, flags);
  601. netif_wake_queue(dev);
  602. if (NI5010_DEBUG) ni5010_show_registers(dev);
  603. }
  604. static void chipset_init(struct net_device *dev, int startp)
  605. {
  606. /* FIXME: Move some stuff here */
  607. PRINTK3((KERN_DEBUG "%s: doing NOTHING in chipset_init\n", dev->name));
  608. }
  609. static void ni5010_show_registers(struct net_device *dev)
  610. {
  611. int ioaddr = dev->base_addr;
  612. PRINTK3((KERN_DEBUG "%s: XSTAT %#2.2x\n", dev->name, inb(EDLC_XSTAT)));
  613. PRINTK3((KERN_DEBUG "%s: XMASK %#2.2x\n", dev->name, inb(EDLC_XMASK)));
  614. PRINTK3((KERN_DEBUG "%s: RSTAT %#2.2x\n", dev->name, inb(EDLC_RSTAT)));
  615. PRINTK3((KERN_DEBUG "%s: RMASK %#2.2x\n", dev->name, inb(EDLC_RMASK)));
  616. PRINTK3((KERN_DEBUG "%s: RMODE %#2.2x\n", dev->name, inb(EDLC_RMODE)));
  617. PRINTK3((KERN_DEBUG "%s: XMODE %#2.2x\n", dev->name, inb(EDLC_XMODE)));
  618. PRINTK3((KERN_DEBUG "%s: ISTAT %#2.2x\n", dev->name, inb(IE_ISTAT)));
  619. }
  620. #ifdef MODULE
  621. static struct net_device *dev_ni5010;
  622. module_param(io, int, 0);
  623. module_param(irq, int, 0);
  624. MODULE_PARM_DESC(io, "ni5010 I/O base address");
  625. MODULE_PARM_DESC(irq, "ni5010 IRQ number");
  626. static int __init ni5010_init_module(void)
  627. {
  628. PRINTK2((KERN_DEBUG "%s: entering init_module\n", boardname));
  629. /*
  630. if(io <= 0 || irq == 0){
  631. printk(KERN_WARNING "%s: Autoprobing not allowed for modules.\n", boardname);
  632. printk(KERN_WARNING "%s: Set symbols 'io' and 'irq'\n", boardname);
  633. return -EINVAL;
  634. }
  635. */
  636. if (io <= 0){
  637. printk(KERN_WARNING "%s: Autoprobing for modules is hazardous, trying anyway..\n", boardname);
  638. }
  639. PRINTK2((KERN_DEBUG "%s: init_module irq=%#2x, io=%#3x\n", boardname, irq, io));
  640. dev_ni5010 = ni5010_probe(-1);
  641. if (IS_ERR(dev_ni5010))
  642. return PTR_ERR(dev_ni5010);
  643. return 0;
  644. }
  645. static void __exit ni5010_cleanup_module(void)
  646. {
  647. PRINTK2((KERN_DEBUG "%s: entering cleanup_module\n", boardname));
  648. unregister_netdev(dev_ni5010);
  649. release_region(dev_ni5010->base_addr, NI5010_IO_EXTENT);
  650. free_netdev(dev_ni5010);
  651. }
  652. module_init(ni5010_init_module);
  653. module_exit(ni5010_cleanup_module);
  654. #endif /* MODULE */
  655. MODULE_LICENSE("GPL");
  656. /*
  657. * Local variables:
  658. * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c ni5010.c"
  659. * version-control: t
  660. * kept-new-versions: 5
  661. * tab-width: 4
  662. * End:
  663. */