lib8390.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /* 8390.c: A general NS8390 ethernet driver core for linux. */
  2. /*
  3. Written 1992-94 by Donald Becker.
  4. Copyright 1993 United States Government as represented by the
  5. Director, National Security Agency.
  6. This software may be used and distributed according to the terms
  7. of the GNU General Public License, incorporated herein by reference.
  8. The author may be reached as becker@scyld.com, or C/O
  9. Scyld Computing Corporation
  10. 410 Severn Ave., Suite 210
  11. Annapolis MD 21403
  12. This is the chip-specific code for many 8390-based ethernet adaptors.
  13. This is not a complete driver, it must be combined with board-specific
  14. code such as ne.c, wd.c, 3c503.c, etc.
  15. Seeing how at least eight drivers use this code, (not counting the
  16. PCMCIA ones either) it is easy to break some card by what seems like
  17. a simple innocent change. Please contact me or Donald if you think
  18. you have found something that needs changing. -- PG
  19. Changelog:
  20. Paul Gortmaker : remove set_bit lock, other cleanups.
  21. Paul Gortmaker : add ei_get_8390_hdr() so we can pass skb's to
  22. ei_block_input() for eth_io_copy_and_sum().
  23. Paul Gortmaker : exchange static int ei_pingpong for a #define,
  24. also add better Tx error handling.
  25. Paul Gortmaker : rewrite Rx overrun handling as per NS specs.
  26. Alexey Kuznetsov : use the 8390's six bit hash multicast filter.
  27. Paul Gortmaker : tweak ANK's above multicast changes a bit.
  28. Paul Gortmaker : update packet statistics for v2.1.x
  29. Alan Cox : support arbitary stupid port mappings on the
  30. 68K Macintosh. Support >16bit I/O spaces
  31. Paul Gortmaker : add kmod support for auto-loading of the 8390
  32. module by all drivers that require it.
  33. Alan Cox : Spinlocking work, added 'BUG_83C690'
  34. Paul Gortmaker : Separate out Tx timeout code from Tx path.
  35. Paul Gortmaker : Remove old unused single Tx buffer code.
  36. Hayato Fujiwara : Add m32r support.
  37. Paul Gortmaker : use skb_padto() instead of stack scratch area
  38. Sources:
  39. The National Semiconductor LAN Databook, and the 3Com 3c503 databook.
  40. */
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/jiffies.h>
  44. #include <linux/fs.h>
  45. #include <linux/types.h>
  46. #include <linux/string.h>
  47. #include <linux/bitops.h>
  48. #include <asm/system.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/io.h>
  51. #include <asm/irq.h>
  52. #include <linux/delay.h>
  53. #include <linux/errno.h>
  54. #include <linux/fcntl.h>
  55. #include <linux/in.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/init.h>
  58. #include <linux/crc32.h>
  59. #include <linux/netdevice.h>
  60. #include <linux/etherdevice.h>
  61. #define NS8390_CORE
  62. #include "8390.h"
  63. #define BUG_83C690
  64. /* These are the operational function interfaces to board-specific
  65. routines.
  66. void reset_8390(struct net_device *dev)
  67. Resets the board associated with DEV, including a hardware reset of
  68. the 8390. This is only called when there is a transmit timeout, and
  69. it is always followed by 8390_init().
  70. void block_output(struct net_device *dev, int count, const unsigned char *buf,
  71. int start_page)
  72. Write the COUNT bytes of BUF to the packet buffer at START_PAGE. The
  73. "page" value uses the 8390's 256-byte pages.
  74. void get_8390_hdr(struct net_device *dev, struct e8390_hdr *hdr, int ring_page)
  75. Read the 4 byte, page aligned 8390 header. *If* there is a
  76. subsequent read, it will be of the rest of the packet.
  77. void block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
  78. Read COUNT bytes from the packet buffer into the skb data area. Start
  79. reading from RING_OFFSET, the address as the 8390 sees it. This will always
  80. follow the read of the 8390 header.
  81. */
  82. #define ei_reset_8390 (ei_local->reset_8390)
  83. #define ei_block_output (ei_local->block_output)
  84. #define ei_block_input (ei_local->block_input)
  85. #define ei_get_8390_hdr (ei_local->get_8390_hdr)
  86. /* use 0 for production, 1 for verification, >2 for debug */
  87. #ifndef ei_debug
  88. int ei_debug = 1;
  89. #endif
  90. /* Index to functions. */
  91. static void ei_tx_intr(struct net_device *dev);
  92. static void ei_tx_err(struct net_device *dev);
  93. static void ei_tx_timeout(struct net_device *dev);
  94. static void ei_receive(struct net_device *dev);
  95. static void ei_rx_overrun(struct net_device *dev);
  96. /* Routines generic to NS8390-based boards. */
  97. static void NS8390_trigger_send(struct net_device *dev, unsigned int length,
  98. int start_page);
  99. static void set_multicast_list(struct net_device *dev);
  100. static void do_set_multicast_list(struct net_device *dev);
  101. static void __NS8390_init(struct net_device *dev, int startp);
  102. /*
  103. * SMP and the 8390 setup.
  104. *
  105. * The 8390 isnt exactly designed to be multithreaded on RX/TX. There is
  106. * a page register that controls bank and packet buffer access. We guard
  107. * this with ei_local->page_lock. Nobody should assume or set the page other
  108. * than zero when the lock is not held. Lock holders must restore page 0
  109. * before unlocking. Even pure readers must take the lock to protect in
  110. * page 0.
  111. *
  112. * To make life difficult the chip can also be very slow. We therefore can't
  113. * just use spinlocks. For the longer lockups we disable the irq the device
  114. * sits on and hold the lock. We must hold the lock because there is a dual
  115. * processor case other than interrupts (get stats/set multicast list in
  116. * parallel with each other and transmit).
  117. *
  118. * Note: in theory we can just disable the irq on the card _but_ there is
  119. * a latency on SMP irq delivery. So we can easily go "disable irq" "sync irqs"
  120. * enter lock, take the queued irq. So we waddle instead of flying.
  121. *
  122. * Finally by special arrangement for the purpose of being generally
  123. * annoying the transmit function is called bh atomic. That places
  124. * restrictions on the user context callers as disable_irq won't save
  125. * them.
  126. */
  127. /**
  128. * ei_open - Open/initialize the board.
  129. * @dev: network device to initialize
  130. *
  131. * This routine goes all-out, setting everything
  132. * up anew at each open, even though many of these registers should only
  133. * need to be set once at boot.
  134. */
  135. static int __ei_open(struct net_device *dev)
  136. {
  137. unsigned long flags;
  138. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  139. /* The card I/O part of the driver (e.g. 3c503) can hook a Tx timeout
  140. wrapper that does e.g. media check & then calls ei_tx_timeout. */
  141. if (dev->tx_timeout == NULL)
  142. dev->tx_timeout = ei_tx_timeout;
  143. if (dev->watchdog_timeo <= 0)
  144. dev->watchdog_timeo = TX_TIMEOUT;
  145. /*
  146. * Grab the page lock so we own the register set, then call
  147. * the init function.
  148. */
  149. spin_lock_irqsave(&ei_local->page_lock, flags);
  150. __NS8390_init(dev, 1);
  151. /* Set the flag before we drop the lock, That way the IRQ arrives
  152. after its set and we get no silly warnings */
  153. netif_start_queue(dev);
  154. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  155. ei_local->irqlock = 0;
  156. return 0;
  157. }
  158. /**
  159. * ei_close - shut down network device
  160. * @dev: network device to close
  161. *
  162. * Opposite of ei_open(). Only used when "ifconfig <devname> down" is done.
  163. */
  164. static int __ei_close(struct net_device *dev)
  165. {
  166. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  167. unsigned long flags;
  168. /*
  169. * Hold the page lock during close
  170. */
  171. spin_lock_irqsave(&ei_local->page_lock, flags);
  172. __NS8390_init(dev, 0);
  173. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  174. netif_stop_queue(dev);
  175. return 0;
  176. }
  177. /**
  178. * ei_tx_timeout - handle transmit time out condition
  179. * @dev: network device which has apparently fallen asleep
  180. *
  181. * Called by kernel when device never acknowledges a transmit has
  182. * completed (or failed) - i.e. never posted a Tx related interrupt.
  183. */
  184. static void ei_tx_timeout(struct net_device *dev)
  185. {
  186. unsigned long e8390_base = dev->base_addr;
  187. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  188. int txsr, isr, tickssofar = jiffies - dev->trans_start;
  189. unsigned long flags;
  190. #if defined(CONFIG_M32R) && defined(CONFIG_SMP)
  191. unsigned long icucr;
  192. local_irq_save(flags);
  193. icucr = inl(M32R_ICU_CR1_PORTL);
  194. icucr |= M32R_ICUCR_ISMOD11;
  195. outl(icucr, M32R_ICU_CR1_PORTL);
  196. local_irq_restore(flags);
  197. #endif
  198. ei_local->stat.tx_errors++;
  199. spin_lock_irqsave(&ei_local->page_lock, flags);
  200. txsr = ei_inb(e8390_base+EN0_TSR);
  201. isr = ei_inb(e8390_base+EN0_ISR);
  202. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  203. printk(KERN_DEBUG "%s: Tx timed out, %s TSR=%#2x, ISR=%#2x, t=%d.\n",
  204. dev->name, (txsr & ENTSR_ABT) ? "excess collisions." :
  205. (isr) ? "lost interrupt?" : "cable problem?", txsr, isr, tickssofar);
  206. if (!isr && !ei_local->stat.tx_packets)
  207. {
  208. /* The 8390 probably hasn't gotten on the cable yet. */
  209. ei_local->interface_num ^= 1; /* Try a different xcvr. */
  210. }
  211. /* Ugly but a reset can be slow, yet must be protected */
  212. disable_irq_nosync_lockdep(dev->irq);
  213. spin_lock(&ei_local->page_lock);
  214. /* Try to restart the card. Perhaps the user has fixed something. */
  215. ei_reset_8390(dev);
  216. __NS8390_init(dev, 1);
  217. spin_unlock(&ei_local->page_lock);
  218. enable_irq_lockdep(dev->irq);
  219. netif_wake_queue(dev);
  220. }
  221. /**
  222. * ei_start_xmit - begin packet transmission
  223. * @skb: packet to be sent
  224. * @dev: network device to which packet is sent
  225. *
  226. * Sends a packet to an 8390 network device.
  227. */
  228. static int ei_start_xmit(struct sk_buff *skb, struct net_device *dev)
  229. {
  230. unsigned long e8390_base = dev->base_addr;
  231. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  232. int send_length = skb->len, output_page;
  233. unsigned long flags;
  234. char buf[ETH_ZLEN];
  235. char *data = skb->data;
  236. if (skb->len < ETH_ZLEN) {
  237. memset(buf, 0, ETH_ZLEN); /* more efficient than doing just the needed bits */
  238. memcpy(buf, data, skb->len);
  239. send_length = ETH_ZLEN;
  240. data = buf;
  241. }
  242. /* Mask interrupts from the ethercard.
  243. SMP: We have to grab the lock here otherwise the IRQ handler
  244. on another CPU can flip window and race the IRQ mask set. We end
  245. up trashing the mcast filter not disabling irqs if we don't lock */
  246. spin_lock_irqsave(&ei_local->page_lock, flags);
  247. ei_outb_p(0x00, e8390_base + EN0_IMR);
  248. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  249. /*
  250. * Slow phase with lock held.
  251. */
  252. disable_irq_nosync_lockdep_irqsave(dev->irq, &flags);
  253. spin_lock(&ei_local->page_lock);
  254. ei_local->irqlock = 1;
  255. /*
  256. * We have two Tx slots available for use. Find the first free
  257. * slot, and then perform some sanity checks. With two Tx bufs,
  258. * you get very close to transmitting back-to-back packets. With
  259. * only one Tx buf, the transmitter sits idle while you reload the
  260. * card, leaving a substantial gap between each transmitted packet.
  261. */
  262. if (ei_local->tx1 == 0)
  263. {
  264. output_page = ei_local->tx_start_page;
  265. ei_local->tx1 = send_length;
  266. if (ei_debug && ei_local->tx2 > 0)
  267. printk(KERN_DEBUG "%s: idle transmitter tx2=%d, lasttx=%d, txing=%d.\n",
  268. dev->name, ei_local->tx2, ei_local->lasttx, ei_local->txing);
  269. }
  270. else if (ei_local->tx2 == 0)
  271. {
  272. output_page = ei_local->tx_start_page + TX_PAGES/2;
  273. ei_local->tx2 = send_length;
  274. if (ei_debug && ei_local->tx1 > 0)
  275. printk(KERN_DEBUG "%s: idle transmitter, tx1=%d, lasttx=%d, txing=%d.\n",
  276. dev->name, ei_local->tx1, ei_local->lasttx, ei_local->txing);
  277. }
  278. else
  279. { /* We should never get here. */
  280. if (ei_debug)
  281. printk(KERN_DEBUG "%s: No Tx buffers free! tx1=%d tx2=%d last=%d\n",
  282. dev->name, ei_local->tx1, ei_local->tx2, ei_local->lasttx);
  283. ei_local->irqlock = 0;
  284. netif_stop_queue(dev);
  285. ei_outb_p(ENISR_ALL, e8390_base + EN0_IMR);
  286. spin_unlock(&ei_local->page_lock);
  287. enable_irq_lockdep_irqrestore(dev->irq, &flags);
  288. ei_local->stat.tx_errors++;
  289. return 1;
  290. }
  291. /*
  292. * Okay, now upload the packet and trigger a send if the transmitter
  293. * isn't already sending. If it is busy, the interrupt handler will
  294. * trigger the send later, upon receiving a Tx done interrupt.
  295. */
  296. ei_block_output(dev, send_length, data, output_page);
  297. if (! ei_local->txing)
  298. {
  299. ei_local->txing = 1;
  300. NS8390_trigger_send(dev, send_length, output_page);
  301. dev->trans_start = jiffies;
  302. if (output_page == ei_local->tx_start_page)
  303. {
  304. ei_local->tx1 = -1;
  305. ei_local->lasttx = -1;
  306. }
  307. else
  308. {
  309. ei_local->tx2 = -1;
  310. ei_local->lasttx = -2;
  311. }
  312. }
  313. else ei_local->txqueue++;
  314. if (ei_local->tx1 && ei_local->tx2)
  315. netif_stop_queue(dev);
  316. else
  317. netif_start_queue(dev);
  318. /* Turn 8390 interrupts back on. */
  319. ei_local->irqlock = 0;
  320. ei_outb_p(ENISR_ALL, e8390_base + EN0_IMR);
  321. spin_unlock(&ei_local->page_lock);
  322. enable_irq_lockdep_irqrestore(dev->irq, &flags);
  323. dev_kfree_skb (skb);
  324. ei_local->stat.tx_bytes += send_length;
  325. return 0;
  326. }
  327. /**
  328. * ei_interrupt - handle the interrupts from an 8390
  329. * @irq: interrupt number
  330. * @dev_id: a pointer to the net_device
  331. *
  332. * Handle the ether interface interrupts. We pull packets from
  333. * the 8390 via the card specific functions and fire them at the networking
  334. * stack. We also handle transmit completions and wake the transmit path if
  335. * necessary. We also update the counters and do other housekeeping as
  336. * needed.
  337. */
  338. static irqreturn_t __ei_interrupt(int irq, void *dev_id)
  339. {
  340. struct net_device *dev = dev_id;
  341. unsigned long e8390_base = dev->base_addr;
  342. int interrupts, nr_serviced = 0;
  343. struct ei_device *ei_local = netdev_priv(dev);
  344. /*
  345. * Protect the irq test too.
  346. */
  347. spin_lock(&ei_local->page_lock);
  348. if (ei_local->irqlock)
  349. {
  350. #if 1 /* This might just be an interrupt for a PCI device sharing this line */
  351. /* The "irqlock" check is only for testing. */
  352. printk(ei_local->irqlock
  353. ? "%s: Interrupted while interrupts are masked! isr=%#2x imr=%#2x.\n"
  354. : "%s: Reentering the interrupt handler! isr=%#2x imr=%#2x.\n",
  355. dev->name, ei_inb_p(e8390_base + EN0_ISR),
  356. ei_inb_p(e8390_base + EN0_IMR));
  357. #endif
  358. spin_unlock(&ei_local->page_lock);
  359. return IRQ_NONE;
  360. }
  361. /* Change to page 0 and read the intr status reg. */
  362. ei_outb_p(E8390_NODMA+E8390_PAGE0, e8390_base + E8390_CMD);
  363. if (ei_debug > 3)
  364. printk(KERN_DEBUG "%s: interrupt(isr=%#2.2x).\n", dev->name,
  365. ei_inb_p(e8390_base + EN0_ISR));
  366. /* !!Assumption!! -- we stay in page 0. Don't break this. */
  367. while ((interrupts = ei_inb_p(e8390_base + EN0_ISR)) != 0
  368. && ++nr_serviced < MAX_SERVICE)
  369. {
  370. if (!netif_running(dev)) {
  371. printk(KERN_WARNING "%s: interrupt from stopped card\n", dev->name);
  372. /* rmk - acknowledge the interrupts */
  373. ei_outb_p(interrupts, e8390_base + EN0_ISR);
  374. interrupts = 0;
  375. break;
  376. }
  377. if (interrupts & ENISR_OVER)
  378. ei_rx_overrun(dev);
  379. else if (interrupts & (ENISR_RX+ENISR_RX_ERR))
  380. {
  381. /* Got a good (?) packet. */
  382. ei_receive(dev);
  383. }
  384. /* Push the next to-transmit packet through. */
  385. if (interrupts & ENISR_TX)
  386. ei_tx_intr(dev);
  387. else if (interrupts & ENISR_TX_ERR)
  388. ei_tx_err(dev);
  389. if (interrupts & ENISR_COUNTERS)
  390. {
  391. ei_local->stat.rx_frame_errors += ei_inb_p(e8390_base + EN0_COUNTER0);
  392. ei_local->stat.rx_crc_errors += ei_inb_p(e8390_base + EN0_COUNTER1);
  393. ei_local->stat.rx_missed_errors+= ei_inb_p(e8390_base + EN0_COUNTER2);
  394. ei_outb_p(ENISR_COUNTERS, e8390_base + EN0_ISR); /* Ack intr. */
  395. }
  396. /* Ignore any RDC interrupts that make it back to here. */
  397. if (interrupts & ENISR_RDC)
  398. {
  399. ei_outb_p(ENISR_RDC, e8390_base + EN0_ISR);
  400. }
  401. ei_outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base + E8390_CMD);
  402. }
  403. if (interrupts && ei_debug)
  404. {
  405. ei_outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base + E8390_CMD);
  406. if (nr_serviced >= MAX_SERVICE)
  407. {
  408. /* 0xFF is valid for a card removal */
  409. if(interrupts!=0xFF)
  410. printk(KERN_WARNING "%s: Too much work at interrupt, status %#2.2x\n",
  411. dev->name, interrupts);
  412. ei_outb_p(ENISR_ALL, e8390_base + EN0_ISR); /* Ack. most intrs. */
  413. } else {
  414. printk(KERN_WARNING "%s: unknown interrupt %#2x\n", dev->name, interrupts);
  415. ei_outb_p(0xff, e8390_base + EN0_ISR); /* Ack. all intrs. */
  416. }
  417. }
  418. spin_unlock(&ei_local->page_lock);
  419. return IRQ_RETVAL(nr_serviced > 0);
  420. }
  421. #ifdef CONFIG_NET_POLL_CONTROLLER
  422. static void __ei_poll(struct net_device *dev)
  423. {
  424. disable_irq_lockdep(dev->irq);
  425. __ei_interrupt(dev->irq, dev);
  426. enable_irq_lockdep(dev->irq);
  427. }
  428. #endif
  429. /**
  430. * ei_tx_err - handle transmitter error
  431. * @dev: network device which threw the exception
  432. *
  433. * A transmitter error has happened. Most likely excess collisions (which
  434. * is a fairly normal condition). If the error is one where the Tx will
  435. * have been aborted, we try and send another one right away, instead of
  436. * letting the failed packet sit and collect dust in the Tx buffer. This
  437. * is a much better solution as it avoids kernel based Tx timeouts, and
  438. * an unnecessary card reset.
  439. *
  440. * Called with lock held.
  441. */
  442. static void ei_tx_err(struct net_device *dev)
  443. {
  444. unsigned long e8390_base = dev->base_addr;
  445. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  446. unsigned char txsr = ei_inb_p(e8390_base+EN0_TSR);
  447. unsigned char tx_was_aborted = txsr & (ENTSR_ABT+ENTSR_FU);
  448. #ifdef VERBOSE_ERROR_DUMP
  449. printk(KERN_DEBUG "%s: transmitter error (%#2x): ", dev->name, txsr);
  450. if (txsr & ENTSR_ABT)
  451. printk("excess-collisions ");
  452. if (txsr & ENTSR_ND)
  453. printk("non-deferral ");
  454. if (txsr & ENTSR_CRS)
  455. printk("lost-carrier ");
  456. if (txsr & ENTSR_FU)
  457. printk("FIFO-underrun ");
  458. if (txsr & ENTSR_CDH)
  459. printk("lost-heartbeat ");
  460. printk("\n");
  461. #endif
  462. ei_outb_p(ENISR_TX_ERR, e8390_base + EN0_ISR); /* Ack intr. */
  463. if (tx_was_aborted)
  464. ei_tx_intr(dev);
  465. else
  466. {
  467. ei_local->stat.tx_errors++;
  468. if (txsr & ENTSR_CRS) ei_local->stat.tx_carrier_errors++;
  469. if (txsr & ENTSR_CDH) ei_local->stat.tx_heartbeat_errors++;
  470. if (txsr & ENTSR_OWC) ei_local->stat.tx_window_errors++;
  471. }
  472. }
  473. /**
  474. * ei_tx_intr - transmit interrupt handler
  475. * @dev: network device for which tx intr is handled
  476. *
  477. * We have finished a transmit: check for errors and then trigger the next
  478. * packet to be sent. Called with lock held.
  479. */
  480. static void ei_tx_intr(struct net_device *dev)
  481. {
  482. unsigned long e8390_base = dev->base_addr;
  483. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  484. int status = ei_inb(e8390_base + EN0_TSR);
  485. ei_outb_p(ENISR_TX, e8390_base + EN0_ISR); /* Ack intr. */
  486. /*
  487. * There are two Tx buffers, see which one finished, and trigger
  488. * the send of another one if it exists.
  489. */
  490. ei_local->txqueue--;
  491. if (ei_local->tx1 < 0)
  492. {
  493. if (ei_local->lasttx != 1 && ei_local->lasttx != -1)
  494. printk(KERN_ERR "%s: bogus last_tx_buffer %d, tx1=%d.\n",
  495. ei_local->name, ei_local->lasttx, ei_local->tx1);
  496. ei_local->tx1 = 0;
  497. if (ei_local->tx2 > 0)
  498. {
  499. ei_local->txing = 1;
  500. NS8390_trigger_send(dev, ei_local->tx2, ei_local->tx_start_page + 6);
  501. dev->trans_start = jiffies;
  502. ei_local->tx2 = -1,
  503. ei_local->lasttx = 2;
  504. }
  505. else ei_local->lasttx = 20, ei_local->txing = 0;
  506. }
  507. else if (ei_local->tx2 < 0)
  508. {
  509. if (ei_local->lasttx != 2 && ei_local->lasttx != -2)
  510. printk("%s: bogus last_tx_buffer %d, tx2=%d.\n",
  511. ei_local->name, ei_local->lasttx, ei_local->tx2);
  512. ei_local->tx2 = 0;
  513. if (ei_local->tx1 > 0)
  514. {
  515. ei_local->txing = 1;
  516. NS8390_trigger_send(dev, ei_local->tx1, ei_local->tx_start_page);
  517. dev->trans_start = jiffies;
  518. ei_local->tx1 = -1;
  519. ei_local->lasttx = 1;
  520. }
  521. else
  522. ei_local->lasttx = 10, ei_local->txing = 0;
  523. }
  524. // else printk(KERN_WARNING "%s: unexpected TX-done interrupt, lasttx=%d.\n",
  525. // dev->name, ei_local->lasttx);
  526. /* Minimize Tx latency: update the statistics after we restart TXing. */
  527. if (status & ENTSR_COL)
  528. ei_local->stat.collisions++;
  529. if (status & ENTSR_PTX)
  530. ei_local->stat.tx_packets++;
  531. else
  532. {
  533. ei_local->stat.tx_errors++;
  534. if (status & ENTSR_ABT)
  535. {
  536. ei_local->stat.tx_aborted_errors++;
  537. ei_local->stat.collisions += 16;
  538. }
  539. if (status & ENTSR_CRS)
  540. ei_local->stat.tx_carrier_errors++;
  541. if (status & ENTSR_FU)
  542. ei_local->stat.tx_fifo_errors++;
  543. if (status & ENTSR_CDH)
  544. ei_local->stat.tx_heartbeat_errors++;
  545. if (status & ENTSR_OWC)
  546. ei_local->stat.tx_window_errors++;
  547. }
  548. netif_wake_queue(dev);
  549. }
  550. /**
  551. * ei_receive - receive some packets
  552. * @dev: network device with which receive will be run
  553. *
  554. * We have a good packet(s), get it/them out of the buffers.
  555. * Called with lock held.
  556. */
  557. static void ei_receive(struct net_device *dev)
  558. {
  559. unsigned long e8390_base = dev->base_addr;
  560. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  561. unsigned char rxing_page, this_frame, next_frame;
  562. unsigned short current_offset;
  563. int rx_pkt_count = 0;
  564. struct e8390_pkt_hdr rx_frame;
  565. int num_rx_pages = ei_local->stop_page-ei_local->rx_start_page;
  566. while (++rx_pkt_count < 10)
  567. {
  568. int pkt_len, pkt_stat;
  569. /* Get the rx page (incoming packet pointer). */
  570. ei_outb_p(E8390_NODMA+E8390_PAGE1, e8390_base + E8390_CMD);
  571. rxing_page = ei_inb_p(e8390_base + EN1_CURPAG);
  572. ei_outb_p(E8390_NODMA+E8390_PAGE0, e8390_base + E8390_CMD);
  573. /* Remove one frame from the ring. Boundary is always a page behind. */
  574. this_frame = ei_inb_p(e8390_base + EN0_BOUNDARY) + 1;
  575. if (this_frame >= ei_local->stop_page)
  576. this_frame = ei_local->rx_start_page;
  577. /* Someday we'll omit the previous, iff we never get this message.
  578. (There is at least one clone claimed to have a problem.)
  579. Keep quiet if it looks like a card removal. One problem here
  580. is that some clones crash in roughly the same way.
  581. */
  582. if (ei_debug > 0 && this_frame != ei_local->current_page && (this_frame!=0x0 || rxing_page!=0xFF))
  583. printk(KERN_ERR "%s: mismatched read page pointers %2x vs %2x.\n",
  584. dev->name, this_frame, ei_local->current_page);
  585. if (this_frame == rxing_page) /* Read all the frames? */
  586. break; /* Done for now */
  587. current_offset = this_frame << 8;
  588. ei_get_8390_hdr(dev, &rx_frame, this_frame);
  589. pkt_len = rx_frame.count - sizeof(struct e8390_pkt_hdr);
  590. pkt_stat = rx_frame.status;
  591. next_frame = this_frame + 1 + ((pkt_len+4)>>8);
  592. /* Check for bogosity warned by 3c503 book: the status byte is never
  593. written. This happened a lot during testing! This code should be
  594. cleaned up someday. */
  595. if (rx_frame.next != next_frame
  596. && rx_frame.next != next_frame + 1
  597. && rx_frame.next != next_frame - num_rx_pages
  598. && rx_frame.next != next_frame + 1 - num_rx_pages) {
  599. ei_local->current_page = rxing_page;
  600. ei_outb(ei_local->current_page-1, e8390_base+EN0_BOUNDARY);
  601. ei_local->stat.rx_errors++;
  602. continue;
  603. }
  604. if (pkt_len < 60 || pkt_len > 1518)
  605. {
  606. if (ei_debug)
  607. printk(KERN_DEBUG "%s: bogus packet size: %d, status=%#2x nxpg=%#2x.\n",
  608. dev->name, rx_frame.count, rx_frame.status,
  609. rx_frame.next);
  610. ei_local->stat.rx_errors++;
  611. ei_local->stat.rx_length_errors++;
  612. }
  613. else if ((pkt_stat & 0x0F) == ENRSR_RXOK)
  614. {
  615. struct sk_buff *skb;
  616. skb = dev_alloc_skb(pkt_len+2);
  617. if (skb == NULL)
  618. {
  619. if (ei_debug > 1)
  620. printk(KERN_DEBUG "%s: Couldn't allocate a sk_buff of size %d.\n",
  621. dev->name, pkt_len);
  622. ei_local->stat.rx_dropped++;
  623. break;
  624. }
  625. else
  626. {
  627. skb_reserve(skb,2); /* IP headers on 16 byte boundaries */
  628. skb->dev = dev;
  629. skb_put(skb, pkt_len); /* Make room */
  630. ei_block_input(dev, pkt_len, skb, current_offset + sizeof(rx_frame));
  631. skb->protocol=eth_type_trans(skb,dev);
  632. netif_rx(skb);
  633. dev->last_rx = jiffies;
  634. ei_local->stat.rx_packets++;
  635. ei_local->stat.rx_bytes += pkt_len;
  636. if (pkt_stat & ENRSR_PHY)
  637. ei_local->stat.multicast++;
  638. }
  639. }
  640. else
  641. {
  642. if (ei_debug)
  643. printk(KERN_DEBUG "%s: bogus packet: status=%#2x nxpg=%#2x size=%d\n",
  644. dev->name, rx_frame.status, rx_frame.next,
  645. rx_frame.count);
  646. ei_local->stat.rx_errors++;
  647. /* NB: The NIC counts CRC, frame and missed errors. */
  648. if (pkt_stat & ENRSR_FO)
  649. ei_local->stat.rx_fifo_errors++;
  650. }
  651. next_frame = rx_frame.next;
  652. /* This _should_ never happen: it's here for avoiding bad clones. */
  653. if (next_frame >= ei_local->stop_page) {
  654. printk("%s: next frame inconsistency, %#2x\n", dev->name,
  655. next_frame);
  656. next_frame = ei_local->rx_start_page;
  657. }
  658. ei_local->current_page = next_frame;
  659. ei_outb_p(next_frame-1, e8390_base+EN0_BOUNDARY);
  660. }
  661. /* We used to also ack ENISR_OVER here, but that would sometimes mask
  662. a real overrun, leaving the 8390 in a stopped state with rec'vr off. */
  663. ei_outb_p(ENISR_RX+ENISR_RX_ERR, e8390_base+EN0_ISR);
  664. return;
  665. }
  666. /**
  667. * ei_rx_overrun - handle receiver overrun
  668. * @dev: network device which threw exception
  669. *
  670. * We have a receiver overrun: we have to kick the 8390 to get it started
  671. * again. Problem is that you have to kick it exactly as NS prescribes in
  672. * the updated datasheets, or "the NIC may act in an unpredictable manner."
  673. * This includes causing "the NIC to defer indefinitely when it is stopped
  674. * on a busy network." Ugh.
  675. * Called with lock held. Don't call this with the interrupts off or your
  676. * computer will hate you - it takes 10ms or so.
  677. */
  678. static void ei_rx_overrun(struct net_device *dev)
  679. {
  680. unsigned long e8390_base = dev->base_addr;
  681. unsigned char was_txing, must_resend = 0;
  682. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  683. /*
  684. * Record whether a Tx was in progress and then issue the
  685. * stop command.
  686. */
  687. was_txing = ei_inb_p(e8390_base+E8390_CMD) & E8390_TRANS;
  688. ei_outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD);
  689. if (ei_debug > 1)
  690. printk(KERN_DEBUG "%s: Receiver overrun.\n", dev->name);
  691. ei_local->stat.rx_over_errors++;
  692. /*
  693. * Wait a full Tx time (1.2ms) + some guard time, NS says 1.6ms total.
  694. * Early datasheets said to poll the reset bit, but now they say that
  695. * it "is not a reliable indicator and subsequently should be ignored."
  696. * We wait at least 10ms.
  697. */
  698. mdelay(10);
  699. /*
  700. * Reset RBCR[01] back to zero as per magic incantation.
  701. */
  702. ei_outb_p(0x00, e8390_base+EN0_RCNTLO);
  703. ei_outb_p(0x00, e8390_base+EN0_RCNTHI);
  704. /*
  705. * See if any Tx was interrupted or not. According to NS, this
  706. * step is vital, and skipping it will cause no end of havoc.
  707. */
  708. if (was_txing)
  709. {
  710. unsigned char tx_completed = ei_inb_p(e8390_base+EN0_ISR) & (ENISR_TX+ENISR_TX_ERR);
  711. if (!tx_completed)
  712. must_resend = 1;
  713. }
  714. /*
  715. * Have to enter loopback mode and then restart the NIC before
  716. * you are allowed to slurp packets up off the ring.
  717. */
  718. ei_outb_p(E8390_TXOFF, e8390_base + EN0_TXCR);
  719. ei_outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START, e8390_base + E8390_CMD);
  720. /*
  721. * Clear the Rx ring of all the debris, and ack the interrupt.
  722. */
  723. ei_receive(dev);
  724. ei_outb_p(ENISR_OVER, e8390_base+EN0_ISR);
  725. /*
  726. * Leave loopback mode, and resend any packet that got stopped.
  727. */
  728. ei_outb_p(E8390_TXCONFIG, e8390_base + EN0_TXCR);
  729. if (must_resend)
  730. ei_outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START + E8390_TRANS, e8390_base + E8390_CMD);
  731. }
  732. /*
  733. * Collect the stats. This is called unlocked and from several contexts.
  734. */
  735. static struct net_device_stats *get_stats(struct net_device *dev)
  736. {
  737. unsigned long ioaddr = dev->base_addr;
  738. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  739. unsigned long flags;
  740. /* If the card is stopped, just return the present stats. */
  741. if (!netif_running(dev))
  742. return &ei_local->stat;
  743. spin_lock_irqsave(&ei_local->page_lock,flags);
  744. /* Read the counter registers, assuming we are in page 0. */
  745. ei_local->stat.rx_frame_errors += ei_inb_p(ioaddr + EN0_COUNTER0);
  746. ei_local->stat.rx_crc_errors += ei_inb_p(ioaddr + EN0_COUNTER1);
  747. ei_local->stat.rx_missed_errors+= ei_inb_p(ioaddr + EN0_COUNTER2);
  748. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  749. return &ei_local->stat;
  750. }
  751. /*
  752. * Form the 64 bit 8390 multicast table from the linked list of addresses
  753. * associated with this dev structure.
  754. */
  755. static inline void make_mc_bits(u8 *bits, struct net_device *dev)
  756. {
  757. struct dev_mc_list *dmi;
  758. for (dmi=dev->mc_list; dmi; dmi=dmi->next)
  759. {
  760. u32 crc;
  761. if (dmi->dmi_addrlen != ETH_ALEN)
  762. {
  763. printk(KERN_INFO "%s: invalid multicast address length given.\n", dev->name);
  764. continue;
  765. }
  766. crc = ether_crc(ETH_ALEN, dmi->dmi_addr);
  767. /*
  768. * The 8390 uses the 6 most significant bits of the
  769. * CRC to index the multicast table.
  770. */
  771. bits[crc>>29] |= (1<<((crc>>26)&7));
  772. }
  773. }
  774. /**
  775. * do_set_multicast_list - set/clear multicast filter
  776. * @dev: net device for which multicast filter is adjusted
  777. *
  778. * Set or clear the multicast filter for this adaptor. May be called
  779. * from a BH in 2.1.x. Must be called with lock held.
  780. */
  781. static void do_set_multicast_list(struct net_device *dev)
  782. {
  783. unsigned long e8390_base = dev->base_addr;
  784. int i;
  785. struct ei_device *ei_local = (struct ei_device*)netdev_priv(dev);
  786. if (!(dev->flags&(IFF_PROMISC|IFF_ALLMULTI)))
  787. {
  788. memset(ei_local->mcfilter, 0, 8);
  789. if (dev->mc_list)
  790. make_mc_bits(ei_local->mcfilter, dev);
  791. }
  792. else
  793. memset(ei_local->mcfilter, 0xFF, 8); /* mcast set to accept-all */
  794. /*
  795. * DP8390 manuals don't specify any magic sequence for altering
  796. * the multicast regs on an already running card. To be safe, we
  797. * ensure multicast mode is off prior to loading up the new hash
  798. * table. If this proves to be not enough, we can always resort
  799. * to stopping the NIC, loading the table and then restarting.
  800. *
  801. * Bug Alert! The MC regs on the SMC 83C690 (SMC Elite and SMC
  802. * Elite16) appear to be write-only. The NS 8390 data sheet lists
  803. * them as r/w so this is a bug. The SMC 83C790 (SMC Ultra and
  804. * Ultra32 EISA) appears to have this bug fixed.
  805. */
  806. if (netif_running(dev))
  807. ei_outb_p(E8390_RXCONFIG, e8390_base + EN0_RXCR);
  808. ei_outb_p(E8390_NODMA + E8390_PAGE1, e8390_base + E8390_CMD);
  809. for(i = 0; i < 8; i++)
  810. {
  811. ei_outb_p(ei_local->mcfilter[i], e8390_base + EN1_MULT_SHIFT(i));
  812. #ifndef BUG_83C690
  813. if(ei_inb_p(e8390_base + EN1_MULT_SHIFT(i))!=ei_local->mcfilter[i])
  814. printk(KERN_ERR "Multicast filter read/write mismap %d\n",i);
  815. #endif
  816. }
  817. ei_outb_p(E8390_NODMA + E8390_PAGE0, e8390_base + E8390_CMD);
  818. if(dev->flags&IFF_PROMISC)
  819. ei_outb_p(E8390_RXCONFIG | 0x18, e8390_base + EN0_RXCR);
  820. else if(dev->flags&IFF_ALLMULTI || dev->mc_list)
  821. ei_outb_p(E8390_RXCONFIG | 0x08, e8390_base + EN0_RXCR);
  822. else
  823. ei_outb_p(E8390_RXCONFIG, e8390_base + EN0_RXCR);
  824. }
  825. /*
  826. * Called without lock held. This is invoked from user context and may
  827. * be parallel to just about everything else. Its also fairly quick and
  828. * not called too often. Must protect against both bh and irq users
  829. */
  830. static void set_multicast_list(struct net_device *dev)
  831. {
  832. unsigned long flags;
  833. struct ei_device *ei_local = (struct ei_device*)netdev_priv(dev);
  834. spin_lock_irqsave(&ei_local->page_lock, flags);
  835. do_set_multicast_list(dev);
  836. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  837. }
  838. /**
  839. * ethdev_setup - init rest of 8390 device struct
  840. * @dev: network device structure to init
  841. *
  842. * Initialize the rest of the 8390 device structure. Do NOT __init
  843. * this, as it is used by 8390 based modular drivers too.
  844. */
  845. static void ethdev_setup(struct net_device *dev)
  846. {
  847. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  848. if (ei_debug > 1)
  849. printk(version);
  850. dev->hard_start_xmit = &ei_start_xmit;
  851. dev->get_stats = get_stats;
  852. dev->set_multicast_list = &set_multicast_list;
  853. ether_setup(dev);
  854. spin_lock_init(&ei_local->page_lock);
  855. }
  856. /**
  857. * alloc_ei_netdev - alloc_etherdev counterpart for 8390
  858. * @size: extra bytes to allocate
  859. *
  860. * Allocate 8390-specific net_device.
  861. */
  862. static struct net_device *____alloc_ei_netdev(int size)
  863. {
  864. return alloc_netdev(sizeof(struct ei_device) + size, "eth%d",
  865. ethdev_setup);
  866. }
  867. /* This page of functions should be 8390 generic */
  868. /* Follow National Semi's recommendations for initializing the "NIC". */
  869. /**
  870. * NS8390_init - initialize 8390 hardware
  871. * @dev: network device to initialize
  872. * @startp: boolean. non-zero value to initiate chip processing
  873. *
  874. * Must be called with lock held.
  875. */
  876. static void __NS8390_init(struct net_device *dev, int startp)
  877. {
  878. unsigned long e8390_base = dev->base_addr;
  879. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  880. int i;
  881. int endcfg = ei_local->word16
  882. ? (0x48 | ENDCFG_WTS | (ei_local->bigendian ? ENDCFG_BOS : 0))
  883. : 0x48;
  884. if(sizeof(struct e8390_pkt_hdr)!=4)
  885. panic("8390.c: header struct mispacked\n");
  886. /* Follow National Semi's recommendations for initing the DP83902. */
  887. ei_outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD); /* 0x21 */
  888. ei_outb_p(endcfg, e8390_base + EN0_DCFG); /* 0x48 or 0x49 */
  889. /* Clear the remote byte count registers. */
  890. ei_outb_p(0x00, e8390_base + EN0_RCNTLO);
  891. ei_outb_p(0x00, e8390_base + EN0_RCNTHI);
  892. /* Set to monitor and loopback mode -- this is vital!. */
  893. ei_outb_p(E8390_RXOFF, e8390_base + EN0_RXCR); /* 0x20 */
  894. ei_outb_p(E8390_TXOFF, e8390_base + EN0_TXCR); /* 0x02 */
  895. /* Set the transmit page and receive ring. */
  896. ei_outb_p(ei_local->tx_start_page, e8390_base + EN0_TPSR);
  897. ei_local->tx1 = ei_local->tx2 = 0;
  898. ei_outb_p(ei_local->rx_start_page, e8390_base + EN0_STARTPG);
  899. ei_outb_p(ei_local->stop_page-1, e8390_base + EN0_BOUNDARY); /* 3c503 says 0x3f,NS0x26*/
  900. ei_local->current_page = ei_local->rx_start_page; /* assert boundary+1 */
  901. ei_outb_p(ei_local->stop_page, e8390_base + EN0_STOPPG);
  902. /* Clear the pending interrupts and mask. */
  903. ei_outb_p(0xFF, e8390_base + EN0_ISR);
  904. ei_outb_p(0x00, e8390_base + EN0_IMR);
  905. /* Copy the station address into the DS8390 registers. */
  906. ei_outb_p(E8390_NODMA + E8390_PAGE1 + E8390_STOP, e8390_base+E8390_CMD); /* 0x61 */
  907. for(i = 0; i < 6; i++)
  908. {
  909. ei_outb_p(dev->dev_addr[i], e8390_base + EN1_PHYS_SHIFT(i));
  910. if (ei_debug > 1 && ei_inb_p(e8390_base + EN1_PHYS_SHIFT(i))!=dev->dev_addr[i])
  911. printk(KERN_ERR "Hw. address read/write mismap %d\n",i);
  912. }
  913. ei_outb_p(ei_local->rx_start_page, e8390_base + EN1_CURPAG);
  914. ei_outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD);
  915. netif_start_queue(dev);
  916. ei_local->tx1 = ei_local->tx2 = 0;
  917. ei_local->txing = 0;
  918. if (startp)
  919. {
  920. ei_outb_p(0xff, e8390_base + EN0_ISR);
  921. ei_outb_p(ENISR_ALL, e8390_base + EN0_IMR);
  922. ei_outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base+E8390_CMD);
  923. ei_outb_p(E8390_TXCONFIG, e8390_base + EN0_TXCR); /* xmit on. */
  924. /* 3c503 TechMan says rxconfig only after the NIC is started. */
  925. ei_outb_p(E8390_RXCONFIG, e8390_base + EN0_RXCR); /* rx on, */
  926. do_set_multicast_list(dev); /* (re)load the mcast table */
  927. }
  928. }
  929. /* Trigger a transmit start, assuming the length is valid.
  930. Always called with the page lock held */
  931. static void NS8390_trigger_send(struct net_device *dev, unsigned int length,
  932. int start_page)
  933. {
  934. unsigned long e8390_base = dev->base_addr;
  935. struct ei_device *ei_local __attribute((unused)) = (struct ei_device *) netdev_priv(dev);
  936. ei_outb_p(E8390_NODMA+E8390_PAGE0, e8390_base+E8390_CMD);
  937. if (ei_inb_p(e8390_base + E8390_CMD) & E8390_TRANS)
  938. {
  939. printk(KERN_WARNING "%s: trigger_send() called with the transmitter busy.\n",
  940. dev->name);
  941. return;
  942. }
  943. ei_outb_p(length & 0xff, e8390_base + EN0_TCNTLO);
  944. ei_outb_p(length >> 8, e8390_base + EN0_TCNTHI);
  945. ei_outb_p(start_page, e8390_base + EN0_TPSR);
  946. ei_outb_p(E8390_NODMA+E8390_TRANS+E8390_START, e8390_base+E8390_CMD);
  947. }