lib8390.c 35 KB

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