ether1.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * linux/drivers/acorn/net/ether1.c
  3. *
  4. * Copyright (C) 1996-2000 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Acorn ether1 driver (82586 chip) for Acorn machines
  11. *
  12. * We basically keep two queues in the cards memory - one for transmit
  13. * and one for receive. Each has a head and a tail. The head is where
  14. * we/the chip adds packets to be transmitted/received, and the tail
  15. * is where the transmitter has got to/where the receiver will stop.
  16. * Both of these queues are circular, and since the chip is running
  17. * all the time, we have to be careful when we modify the pointers etc
  18. * so that the buffer memory contents is valid all the time.
  19. *
  20. * Change log:
  21. * 1.00 RMK Released
  22. * 1.01 RMK 19/03/1996 Transfers the last odd byte onto/off of the card now.
  23. * 1.02 RMK 25/05/1997 Added code to restart RU if it goes not ready
  24. * 1.03 RMK 14/09/1997 Cleaned up the handling of a reset during the TX interrupt.
  25. * Should prevent lockup.
  26. * 1.04 RMK 17/09/1997 Added more info when initialsation of chip goes wrong.
  27. * TDR now only reports failure when chip reports non-zero
  28. * TDR time-distance.
  29. * 1.05 RMK 31/12/1997 Removed calls to dev_tint for 2.1
  30. * 1.06 RMK 10/02/2000 Updated for 2.3.43
  31. * 1.07 RMK 13/05/2000 Updated for 2.3.99-pre8
  32. */
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/types.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/ioport.h>
  39. #include <linux/in.h>
  40. #include <linux/slab.h>
  41. #include <linux/string.h>
  42. #include <linux/errno.h>
  43. #include <linux/device.h>
  44. #include <linux/init.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/etherdevice.h>
  47. #include <linux/skbuff.h>
  48. #include <linux/bitops.h>
  49. #include <asm/io.h>
  50. #include <asm/dma.h>
  51. #include <asm/ecard.h>
  52. #define __ETHER1_C
  53. #include "ether1.h"
  54. static unsigned int net_debug = NET_DEBUG;
  55. #define BUFFER_SIZE 0x10000
  56. #define TX_AREA_START 0x00100
  57. #define TX_AREA_END 0x05000
  58. #define RX_AREA_START 0x05000
  59. #define RX_AREA_END 0x0fc00
  60. static int ether1_open(struct net_device *dev);
  61. static netdev_tx_t ether1_sendpacket(struct sk_buff *skb,
  62. struct net_device *dev);
  63. static irqreturn_t ether1_interrupt(int irq, void *dev_id);
  64. static int ether1_close(struct net_device *dev);
  65. static void ether1_setmulticastlist(struct net_device *dev);
  66. static void ether1_timeout(struct net_device *dev);
  67. /* ------------------------------------------------------------------------- */
  68. static char version[] = "ether1 ethernet driver (c) 2000 Russell King v1.07\n";
  69. #define BUS_16 16
  70. #define BUS_8 8
  71. /* ------------------------------------------------------------------------- */
  72. #define DISABLEIRQS 1
  73. #define NORMALIRQS 0
  74. #define ether1_readw(dev, addr, type, offset, svflgs) ether1_inw_p (dev, addr + (int)(&((type *)0)->offset), svflgs)
  75. #define ether1_writew(dev, val, addr, type, offset, svflgs) ether1_outw_p (dev, val, addr + (int)(&((type *)0)->offset), svflgs)
  76. static inline unsigned short
  77. ether1_inw_p (struct net_device *dev, int addr, int svflgs)
  78. {
  79. unsigned long flags;
  80. unsigned short ret;
  81. if (svflgs)
  82. local_irq_save (flags);
  83. writeb(addr >> 12, REG_PAGE);
  84. ret = readw(ETHER1_RAM + ((addr & 4095) << 1));
  85. if (svflgs)
  86. local_irq_restore (flags);
  87. return ret;
  88. }
  89. static inline void
  90. ether1_outw_p (struct net_device *dev, unsigned short val, int addr, int svflgs)
  91. {
  92. unsigned long flags;
  93. if (svflgs)
  94. local_irq_save (flags);
  95. writeb(addr >> 12, REG_PAGE);
  96. writew(val, ETHER1_RAM + ((addr & 4095) << 1));
  97. if (svflgs)
  98. local_irq_restore (flags);
  99. }
  100. /*
  101. * Some inline assembler to allow fast transfers on to/off of the card.
  102. * Since this driver depends on some features presented by the ARM
  103. * specific architecture, and that you can't configure this driver
  104. * without specifiing ARM mode, this is not a problem.
  105. *
  106. * This routine is essentially an optimised memcpy from the card's
  107. * onboard RAM to kernel memory.
  108. */
  109. static void
  110. ether1_writebuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
  111. {
  112. unsigned int page, thislen, offset;
  113. void __iomem *addr;
  114. offset = start & 4095;
  115. page = start >> 12;
  116. addr = ETHER1_RAM + (offset << 1);
  117. if (offset + length > 4096)
  118. thislen = 4096 - offset;
  119. else
  120. thislen = length;
  121. do {
  122. int used;
  123. writeb(page, REG_PAGE);
  124. length -= thislen;
  125. __asm__ __volatile__(
  126. "subs %3, %3, #2\n\
  127. bmi 2f\n\
  128. 1: ldr %0, [%1], #2\n\
  129. mov %0, %0, lsl #16\n\
  130. orr %0, %0, %0, lsr #16\n\
  131. str %0, [%2], #4\n\
  132. subs %3, %3, #2\n\
  133. bmi 2f\n\
  134. ldr %0, [%1], #2\n\
  135. mov %0, %0, lsl #16\n\
  136. orr %0, %0, %0, lsr #16\n\
  137. str %0, [%2], #4\n\
  138. subs %3, %3, #2\n\
  139. bmi 2f\n\
  140. ldr %0, [%1], #2\n\
  141. mov %0, %0, lsl #16\n\
  142. orr %0, %0, %0, lsr #16\n\
  143. str %0, [%2], #4\n\
  144. subs %3, %3, #2\n\
  145. bmi 2f\n\
  146. ldr %0, [%1], #2\n\
  147. mov %0, %0, lsl #16\n\
  148. orr %0, %0, %0, lsr #16\n\
  149. str %0, [%2], #4\n\
  150. subs %3, %3, #2\n\
  151. bpl 1b\n\
  152. 2: adds %3, %3, #1\n\
  153. ldreqb %0, [%1]\n\
  154. streqb %0, [%2]"
  155. : "=&r" (used), "=&r" (data)
  156. : "r" (addr), "r" (thislen), "1" (data));
  157. addr = ETHER1_RAM;
  158. thislen = length;
  159. if (thislen > 4096)
  160. thislen = 4096;
  161. page++;
  162. } while (thislen);
  163. }
  164. static void
  165. ether1_readbuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
  166. {
  167. unsigned int page, thislen, offset;
  168. void __iomem *addr;
  169. offset = start & 4095;
  170. page = start >> 12;
  171. addr = ETHER1_RAM + (offset << 1);
  172. if (offset + length > 4096)
  173. thislen = 4096 - offset;
  174. else
  175. thislen = length;
  176. do {
  177. int used;
  178. writeb(page, REG_PAGE);
  179. length -= thislen;
  180. __asm__ __volatile__(
  181. "subs %3, %3, #2\n\
  182. bmi 2f\n\
  183. 1: ldr %0, [%2], #4\n\
  184. strb %0, [%1], #1\n\
  185. mov %0, %0, lsr #8\n\
  186. strb %0, [%1], #1\n\
  187. subs %3, %3, #2\n\
  188. bmi 2f\n\
  189. ldr %0, [%2], #4\n\
  190. strb %0, [%1], #1\n\
  191. mov %0, %0, lsr #8\n\
  192. strb %0, [%1], #1\n\
  193. subs %3, %3, #2\n\
  194. bmi 2f\n\
  195. ldr %0, [%2], #4\n\
  196. strb %0, [%1], #1\n\
  197. mov %0, %0, lsr #8\n\
  198. strb %0, [%1], #1\n\
  199. subs %3, %3, #2\n\
  200. bmi 2f\n\
  201. ldr %0, [%2], #4\n\
  202. strb %0, [%1], #1\n\
  203. mov %0, %0, lsr #8\n\
  204. strb %0, [%1], #1\n\
  205. subs %3, %3, #2\n\
  206. bpl 1b\n\
  207. 2: adds %3, %3, #1\n\
  208. ldreqb %0, [%2]\n\
  209. streqb %0, [%1]"
  210. : "=&r" (used), "=&r" (data)
  211. : "r" (addr), "r" (thislen), "1" (data));
  212. addr = ETHER1_RAM;
  213. thislen = length;
  214. if (thislen > 4096)
  215. thislen = 4096;
  216. page++;
  217. } while (thislen);
  218. }
  219. static int
  220. ether1_ramtest(struct net_device *dev, unsigned char byte)
  221. {
  222. unsigned char *buffer = kmalloc (BUFFER_SIZE, GFP_KERNEL);
  223. int i, ret = BUFFER_SIZE;
  224. int max_errors = 15;
  225. int bad = -1;
  226. int bad_start = 0;
  227. if (!buffer)
  228. return 1;
  229. memset (buffer, byte, BUFFER_SIZE);
  230. ether1_writebuffer (dev, buffer, 0, BUFFER_SIZE);
  231. memset (buffer, byte ^ 0xff, BUFFER_SIZE);
  232. ether1_readbuffer (dev, buffer, 0, BUFFER_SIZE);
  233. for (i = 0; i < BUFFER_SIZE; i++) {
  234. if (buffer[i] != byte) {
  235. if (max_errors >= 0 && bad != buffer[i]) {
  236. if (bad != -1)
  237. printk ("\n");
  238. printk (KERN_CRIT "%s: RAM failed with (%02X instead of %02X) at 0x%04X",
  239. dev->name, buffer[i], byte, i);
  240. ret = -ENODEV;
  241. max_errors --;
  242. bad = buffer[i];
  243. bad_start = i;
  244. }
  245. } else {
  246. if (bad != -1) {
  247. if (bad_start == i - 1)
  248. printk ("\n");
  249. else
  250. printk (" - 0x%04X\n", i - 1);
  251. bad = -1;
  252. }
  253. }
  254. }
  255. if (bad != -1)
  256. printk (" - 0x%04X\n", BUFFER_SIZE);
  257. kfree (buffer);
  258. return ret;
  259. }
  260. static int
  261. ether1_reset (struct net_device *dev)
  262. {
  263. writeb(CTRL_RST|CTRL_ACK, REG_CONTROL);
  264. return BUS_16;
  265. }
  266. static int
  267. ether1_init_2(struct net_device *dev)
  268. {
  269. int i;
  270. dev->mem_start = 0;
  271. i = ether1_ramtest (dev, 0x5a);
  272. if (i > 0)
  273. i = ether1_ramtest (dev, 0x1e);
  274. if (i <= 0)
  275. return -ENODEV;
  276. dev->mem_end = i;
  277. return 0;
  278. }
  279. /*
  280. * These are the structures that are loaded into the ether RAM card to
  281. * initialise the 82586
  282. */
  283. /* at 0x0100 */
  284. #define NOP_ADDR (TX_AREA_START)
  285. #define NOP_SIZE (0x06)
  286. static nop_t init_nop = {
  287. 0,
  288. CMD_NOP,
  289. NOP_ADDR
  290. };
  291. /* at 0x003a */
  292. #define TDR_ADDR (0x003a)
  293. #define TDR_SIZE (0x08)
  294. static tdr_t init_tdr = {
  295. 0,
  296. CMD_TDR | CMD_INTR,
  297. NOP_ADDR,
  298. 0
  299. };
  300. /* at 0x002e */
  301. #define MC_ADDR (0x002e)
  302. #define MC_SIZE (0x0c)
  303. static mc_t init_mc = {
  304. 0,
  305. CMD_SETMULTICAST,
  306. TDR_ADDR,
  307. 0,
  308. { { 0, } }
  309. };
  310. /* at 0x0022 */
  311. #define SA_ADDR (0x0022)
  312. #define SA_SIZE (0x0c)
  313. static sa_t init_sa = {
  314. 0,
  315. CMD_SETADDRESS,
  316. MC_ADDR,
  317. { 0, }
  318. };
  319. /* at 0x0010 */
  320. #define CFG_ADDR (0x0010)
  321. #define CFG_SIZE (0x12)
  322. static cfg_t init_cfg = {
  323. 0,
  324. CMD_CONFIG,
  325. SA_ADDR,
  326. 8,
  327. 8,
  328. CFG8_SRDY,
  329. CFG9_PREAMB8 | CFG9_ADDRLENBUF | CFG9_ADDRLEN(6),
  330. 0,
  331. 0x60,
  332. 0,
  333. CFG13_RETRY(15) | CFG13_SLOTH(2),
  334. 0,
  335. };
  336. /* at 0x0000 */
  337. #define SCB_ADDR (0x0000)
  338. #define SCB_SIZE (0x10)
  339. static scb_t init_scb = {
  340. 0,
  341. SCB_CMDACKRNR | SCB_CMDACKCNA | SCB_CMDACKFR | SCB_CMDACKCX,
  342. CFG_ADDR,
  343. RX_AREA_START,
  344. 0,
  345. 0,
  346. 0,
  347. 0
  348. };
  349. /* at 0xffee */
  350. #define ISCP_ADDR (0xffee)
  351. #define ISCP_SIZE (0x08)
  352. static iscp_t init_iscp = {
  353. 1,
  354. SCB_ADDR,
  355. 0x0000,
  356. 0x0000
  357. };
  358. /* at 0xfff6 */
  359. #define SCP_ADDR (0xfff6)
  360. #define SCP_SIZE (0x0a)
  361. static scp_t init_scp = {
  362. SCP_SY_16BBUS,
  363. { 0, 0 },
  364. ISCP_ADDR,
  365. 0
  366. };
  367. #define RFD_SIZE (0x16)
  368. static rfd_t init_rfd = {
  369. 0,
  370. 0,
  371. 0,
  372. 0,
  373. { 0, },
  374. { 0, },
  375. 0
  376. };
  377. #define RBD_SIZE (0x0a)
  378. static rbd_t init_rbd = {
  379. 0,
  380. 0,
  381. 0,
  382. 0,
  383. ETH_FRAME_LEN + 8
  384. };
  385. #define TX_SIZE (0x08)
  386. #define TBD_SIZE (0x08)
  387. static int
  388. ether1_init_for_open (struct net_device *dev)
  389. {
  390. int i, status, addr, next, next2;
  391. int failures = 0;
  392. unsigned long timeout;
  393. writeb(CTRL_RST|CTRL_ACK, REG_CONTROL);
  394. for (i = 0; i < 6; i++)
  395. init_sa.sa_addr[i] = dev->dev_addr[i];
  396. /* load data structures into ether1 RAM */
  397. ether1_writebuffer (dev, &init_scp, SCP_ADDR, SCP_SIZE);
  398. ether1_writebuffer (dev, &init_iscp, ISCP_ADDR, ISCP_SIZE);
  399. ether1_writebuffer (dev, &init_scb, SCB_ADDR, SCB_SIZE);
  400. ether1_writebuffer (dev, &init_cfg, CFG_ADDR, CFG_SIZE);
  401. ether1_writebuffer (dev, &init_sa, SA_ADDR, SA_SIZE);
  402. ether1_writebuffer (dev, &init_mc, MC_ADDR, MC_SIZE);
  403. ether1_writebuffer (dev, &init_tdr, TDR_ADDR, TDR_SIZE);
  404. ether1_writebuffer (dev, &init_nop, NOP_ADDR, NOP_SIZE);
  405. if (ether1_readw(dev, CFG_ADDR, cfg_t, cfg_command, NORMALIRQS) != CMD_CONFIG) {
  406. printk (KERN_ERR "%s: detected either RAM fault or compiler bug\n",
  407. dev->name);
  408. return 1;
  409. }
  410. /*
  411. * setup circularly linked list of { rfd, rbd, buffer }, with
  412. * all rfds circularly linked, rbds circularly linked.
  413. * First rfd is linked to scp, first rbd is linked to first
  414. * rfd. Last rbd has a suspend command.
  415. */
  416. addr = RX_AREA_START;
  417. do {
  418. next = addr + RFD_SIZE + RBD_SIZE + ETH_FRAME_LEN + 10;
  419. next2 = next + RFD_SIZE + RBD_SIZE + ETH_FRAME_LEN + 10;
  420. if (next2 >= RX_AREA_END) {
  421. next = RX_AREA_START;
  422. init_rfd.rfd_command = RFD_CMDEL | RFD_CMDSUSPEND;
  423. priv(dev)->rx_tail = addr;
  424. } else
  425. init_rfd.rfd_command = 0;
  426. if (addr == RX_AREA_START)
  427. init_rfd.rfd_rbdoffset = addr + RFD_SIZE;
  428. else
  429. init_rfd.rfd_rbdoffset = 0;
  430. init_rfd.rfd_link = next;
  431. init_rbd.rbd_link = next + RFD_SIZE;
  432. init_rbd.rbd_bufl = addr + RFD_SIZE + RBD_SIZE;
  433. ether1_writebuffer (dev, &init_rfd, addr, RFD_SIZE);
  434. ether1_writebuffer (dev, &init_rbd, addr + RFD_SIZE, RBD_SIZE);
  435. addr = next;
  436. } while (next2 < RX_AREA_END);
  437. priv(dev)->tx_link = NOP_ADDR;
  438. priv(dev)->tx_head = NOP_ADDR + NOP_SIZE;
  439. priv(dev)->tx_tail = TDR_ADDR;
  440. priv(dev)->rx_head = RX_AREA_START;
  441. /* release reset & give 586 a prod */
  442. priv(dev)->resetting = 1;
  443. priv(dev)->initialising = 1;
  444. writeb(CTRL_RST, REG_CONTROL);
  445. writeb(0, REG_CONTROL);
  446. writeb(CTRL_CA, REG_CONTROL);
  447. /* 586 should now unset iscp.busy */
  448. timeout = jiffies + HZ/2;
  449. while (ether1_readw(dev, ISCP_ADDR, iscp_t, iscp_busy, DISABLEIRQS) == 1) {
  450. if (time_after(jiffies, timeout)) {
  451. printk (KERN_WARNING "%s: can't initialise 82586: iscp is busy\n", dev->name);
  452. return 1;
  453. }
  454. }
  455. /* check status of commands that we issued */
  456. timeout += HZ/10;
  457. while (((status = ether1_readw(dev, CFG_ADDR, cfg_t, cfg_status, DISABLEIRQS))
  458. & STAT_COMPLETE) == 0) {
  459. if (time_after(jiffies, timeout))
  460. break;
  461. }
  462. if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
  463. printk (KERN_WARNING "%s: can't initialise 82586: config status %04X\n", dev->name, status);
  464. printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
  465. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
  466. ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
  467. ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
  468. ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
  469. failures += 1;
  470. }
  471. timeout += HZ/10;
  472. while (((status = ether1_readw(dev, SA_ADDR, sa_t, sa_status, DISABLEIRQS))
  473. & STAT_COMPLETE) == 0) {
  474. if (time_after(jiffies, timeout))
  475. break;
  476. }
  477. if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
  478. printk (KERN_WARNING "%s: can't initialise 82586: set address status %04X\n", dev->name, status);
  479. printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
  480. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
  481. ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
  482. ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
  483. ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
  484. failures += 1;
  485. }
  486. timeout += HZ/10;
  487. while (((status = ether1_readw(dev, MC_ADDR, mc_t, mc_status, DISABLEIRQS))
  488. & STAT_COMPLETE) == 0) {
  489. if (time_after(jiffies, timeout))
  490. break;
  491. }
  492. if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
  493. printk (KERN_WARNING "%s: can't initialise 82586: set multicast status %04X\n", dev->name, status);
  494. printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
  495. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
  496. ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
  497. ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
  498. ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
  499. failures += 1;
  500. }
  501. timeout += HZ;
  502. while (((status = ether1_readw(dev, TDR_ADDR, tdr_t, tdr_status, DISABLEIRQS))
  503. & STAT_COMPLETE) == 0) {
  504. if (time_after(jiffies, timeout))
  505. break;
  506. }
  507. if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
  508. printk (KERN_WARNING "%s: can't tdr (ignored)\n", dev->name);
  509. printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
  510. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
  511. ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
  512. ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
  513. ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
  514. } else {
  515. status = ether1_readw(dev, TDR_ADDR, tdr_t, tdr_result, DISABLEIRQS);
  516. if (status & TDR_XCVRPROB)
  517. printk (KERN_WARNING "%s: i/f failed tdr: transceiver problem\n", dev->name);
  518. else if ((status & (TDR_SHORT|TDR_OPEN)) && (status & TDR_TIME)) {
  519. #ifdef FANCY
  520. printk (KERN_WARNING "%s: i/f failed tdr: cable %s %d.%d us away\n", dev->name,
  521. status & TDR_SHORT ? "short" : "open", (status & TDR_TIME) / 10,
  522. (status & TDR_TIME) % 10);
  523. #else
  524. printk (KERN_WARNING "%s: i/f failed tdr: cable %s %d clks away\n", dev->name,
  525. status & TDR_SHORT ? "short" : "open", (status & TDR_TIME));
  526. #endif
  527. }
  528. }
  529. if (failures)
  530. ether1_reset (dev);
  531. return failures ? 1 : 0;
  532. }
  533. /* ------------------------------------------------------------------------- */
  534. static int
  535. ether1_txalloc (struct net_device *dev, int size)
  536. {
  537. int start, tail;
  538. size = (size + 1) & ~1;
  539. tail = priv(dev)->tx_tail;
  540. if (priv(dev)->tx_head + size > TX_AREA_END) {
  541. if (tail > priv(dev)->tx_head)
  542. return -1;
  543. start = TX_AREA_START;
  544. if (start + size > tail)
  545. return -1;
  546. priv(dev)->tx_head = start + size;
  547. } else {
  548. if (priv(dev)->tx_head < tail && (priv(dev)->tx_head + size) > tail)
  549. return -1;
  550. start = priv(dev)->tx_head;
  551. priv(dev)->tx_head += size;
  552. }
  553. return start;
  554. }
  555. static int
  556. ether1_open (struct net_device *dev)
  557. {
  558. if (request_irq(dev->irq, ether1_interrupt, 0, "ether1", dev))
  559. return -EAGAIN;
  560. if (ether1_init_for_open (dev)) {
  561. free_irq (dev->irq, dev);
  562. return -EAGAIN;
  563. }
  564. netif_start_queue(dev);
  565. return 0;
  566. }
  567. static void
  568. ether1_timeout(struct net_device *dev)
  569. {
  570. printk(KERN_WARNING "%s: transmit timeout, network cable problem?\n",
  571. dev->name);
  572. printk(KERN_WARNING "%s: resetting device\n", dev->name);
  573. ether1_reset (dev);
  574. if (ether1_init_for_open (dev))
  575. printk (KERN_ERR "%s: unable to restart interface\n", dev->name);
  576. dev->stats.tx_errors++;
  577. netif_wake_queue(dev);
  578. }
  579. static netdev_tx_t
  580. ether1_sendpacket (struct sk_buff *skb, struct net_device *dev)
  581. {
  582. int tmp, tst, nopaddr, txaddr, tbdaddr, dataddr;
  583. unsigned long flags;
  584. tx_t tx;
  585. tbd_t tbd;
  586. nop_t nop;
  587. if (priv(dev)->restart) {
  588. printk(KERN_WARNING "%s: resetting device\n", dev->name);
  589. ether1_reset(dev);
  590. if (ether1_init_for_open(dev))
  591. printk(KERN_ERR "%s: unable to restart interface\n", dev->name);
  592. else
  593. priv(dev)->restart = 0;
  594. }
  595. if (skb->len < ETH_ZLEN) {
  596. if (skb_padto(skb, ETH_ZLEN))
  597. goto out;
  598. }
  599. /*
  600. * insert packet followed by a nop
  601. */
  602. txaddr = ether1_txalloc (dev, TX_SIZE);
  603. tbdaddr = ether1_txalloc (dev, TBD_SIZE);
  604. dataddr = ether1_txalloc (dev, skb->len);
  605. nopaddr = ether1_txalloc (dev, NOP_SIZE);
  606. tx.tx_status = 0;
  607. tx.tx_command = CMD_TX | CMD_INTR;
  608. tx.tx_link = nopaddr;
  609. tx.tx_tbdoffset = tbdaddr;
  610. tbd.tbd_opts = TBD_EOL | skb->len;
  611. tbd.tbd_link = I82586_NULL;
  612. tbd.tbd_bufl = dataddr;
  613. tbd.tbd_bufh = 0;
  614. nop.nop_status = 0;
  615. nop.nop_command = CMD_NOP;
  616. nop.nop_link = nopaddr;
  617. local_irq_save(flags);
  618. ether1_writebuffer (dev, &tx, txaddr, TX_SIZE);
  619. ether1_writebuffer (dev, &tbd, tbdaddr, TBD_SIZE);
  620. ether1_writebuffer (dev, skb->data, dataddr, skb->len);
  621. ether1_writebuffer (dev, &nop, nopaddr, NOP_SIZE);
  622. tmp = priv(dev)->tx_link;
  623. priv(dev)->tx_link = nopaddr;
  624. /* now reset the previous nop pointer */
  625. ether1_writew(dev, txaddr, tmp, nop_t, nop_link, NORMALIRQS);
  626. local_irq_restore(flags);
  627. /* handle transmit */
  628. /* check to see if we have room for a full sized ether frame */
  629. tmp = priv(dev)->tx_head;
  630. tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
  631. priv(dev)->tx_head = tmp;
  632. dev_kfree_skb (skb);
  633. if (tst == -1)
  634. netif_stop_queue(dev);
  635. out:
  636. return NETDEV_TX_OK;
  637. }
  638. static void
  639. ether1_xmit_done (struct net_device *dev)
  640. {
  641. nop_t nop;
  642. int caddr, tst;
  643. caddr = priv(dev)->tx_tail;
  644. again:
  645. ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
  646. switch (nop.nop_command & CMD_MASK) {
  647. case CMD_TDR:
  648. /* special case */
  649. if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
  650. != (unsigned short)I82586_NULL) {
  651. ether1_writew(dev, SCB_CMDCUCSTART | SCB_CMDRXSTART, SCB_ADDR, scb_t,
  652. scb_command, NORMALIRQS);
  653. writeb(CTRL_CA, REG_CONTROL);
  654. }
  655. priv(dev)->tx_tail = NOP_ADDR;
  656. return;
  657. case CMD_NOP:
  658. if (nop.nop_link == caddr) {
  659. if (priv(dev)->initialising == 0)
  660. printk (KERN_WARNING "%s: strange command complete with no tx command!\n", dev->name);
  661. else
  662. priv(dev)->initialising = 0;
  663. return;
  664. }
  665. if (caddr == nop.nop_link)
  666. return;
  667. caddr = nop.nop_link;
  668. goto again;
  669. case CMD_TX:
  670. if (nop.nop_status & STAT_COMPLETE)
  671. break;
  672. printk (KERN_ERR "%s: strange command complete without completed command\n", dev->name);
  673. priv(dev)->restart = 1;
  674. return;
  675. default:
  676. printk (KERN_WARNING "%s: strange command %d complete! (offset %04X)", dev->name,
  677. nop.nop_command & CMD_MASK, caddr);
  678. priv(dev)->restart = 1;
  679. return;
  680. }
  681. while (nop.nop_status & STAT_COMPLETE) {
  682. if (nop.nop_status & STAT_OK) {
  683. dev->stats.tx_packets++;
  684. dev->stats.collisions += (nop.nop_status & STAT_COLLISIONS);
  685. } else {
  686. dev->stats.tx_errors++;
  687. if (nop.nop_status & STAT_COLLAFTERTX)
  688. dev->stats.collisions++;
  689. if (nop.nop_status & STAT_NOCARRIER)
  690. dev->stats.tx_carrier_errors++;
  691. if (nop.nop_status & STAT_TXLOSTCTS)
  692. printk (KERN_WARNING "%s: cts lost\n", dev->name);
  693. if (nop.nop_status & STAT_TXSLOWDMA)
  694. dev->stats.tx_fifo_errors++;
  695. if (nop.nop_status & STAT_COLLEXCESSIVE)
  696. dev->stats.collisions += 16;
  697. }
  698. if (nop.nop_link == caddr) {
  699. printk (KERN_ERR "%s: tx buffer chaining error: tx command points to itself\n", dev->name);
  700. break;
  701. }
  702. caddr = nop.nop_link;
  703. ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
  704. if ((nop.nop_command & CMD_MASK) != CMD_NOP) {
  705. printk (KERN_ERR "%s: tx buffer chaining error: no nop after tx command\n", dev->name);
  706. break;
  707. }
  708. if (caddr == nop.nop_link)
  709. break;
  710. caddr = nop.nop_link;
  711. ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
  712. if ((nop.nop_command & CMD_MASK) != CMD_TX) {
  713. printk (KERN_ERR "%s: tx buffer chaining error: no tx command after nop\n", dev->name);
  714. break;
  715. }
  716. }
  717. priv(dev)->tx_tail = caddr;
  718. caddr = priv(dev)->tx_head;
  719. tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
  720. priv(dev)->tx_head = caddr;
  721. if (tst != -1)
  722. netif_wake_queue(dev);
  723. }
  724. static void
  725. ether1_recv_done (struct net_device *dev)
  726. {
  727. int status;
  728. int nexttail, rbdaddr;
  729. rbd_t rbd;
  730. do {
  731. status = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_status, NORMALIRQS);
  732. if ((status & RFD_COMPLETE) == 0)
  733. break;
  734. rbdaddr = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_rbdoffset, NORMALIRQS);
  735. ether1_readbuffer (dev, &rbd, rbdaddr, RBD_SIZE);
  736. if ((rbd.rbd_status & (RBD_EOF | RBD_ACNTVALID)) == (RBD_EOF | RBD_ACNTVALID)) {
  737. int length = rbd.rbd_status & RBD_ACNT;
  738. struct sk_buff *skb;
  739. length = (length + 1) & ~1;
  740. skb = netdev_alloc_skb(dev, length + 2);
  741. if (skb) {
  742. skb_reserve (skb, 2);
  743. ether1_readbuffer (dev, skb_put (skb, length), rbd.rbd_bufl, length);
  744. skb->protocol = eth_type_trans (skb, dev);
  745. netif_rx (skb);
  746. dev->stats.rx_packets++;
  747. } else
  748. dev->stats.rx_dropped++;
  749. } else {
  750. printk(KERN_WARNING "%s: %s\n", dev->name,
  751. (rbd.rbd_status & RBD_EOF) ? "oversized packet" : "acnt not valid");
  752. dev->stats.rx_dropped++;
  753. }
  754. nexttail = ether1_readw(dev, priv(dev)->rx_tail, rfd_t, rfd_link, NORMALIRQS);
  755. /* nexttail should be rx_head */
  756. if (nexttail != priv(dev)->rx_head)
  757. printk(KERN_ERR "%s: receiver buffer chaining error (%04X != %04X)\n",
  758. dev->name, nexttail, priv(dev)->rx_head);
  759. ether1_writew(dev, RFD_CMDEL | RFD_CMDSUSPEND, nexttail, rfd_t, rfd_command, NORMALIRQS);
  760. ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_command, NORMALIRQS);
  761. ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_status, NORMALIRQS);
  762. ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_rbdoffset, NORMALIRQS);
  763. priv(dev)->rx_tail = nexttail;
  764. priv(dev)->rx_head = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_link, NORMALIRQS);
  765. } while (1);
  766. }
  767. static irqreturn_t
  768. ether1_interrupt (int irq, void *dev_id)
  769. {
  770. struct net_device *dev = (struct net_device *)dev_id;
  771. int status;
  772. status = ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS);
  773. if (status) {
  774. ether1_writew(dev, status & (SCB_STRNR | SCB_STCNA | SCB_STFR | SCB_STCX),
  775. SCB_ADDR, scb_t, scb_command, NORMALIRQS);
  776. writeb(CTRL_CA | CTRL_ACK, REG_CONTROL);
  777. if (status & SCB_STCX) {
  778. ether1_xmit_done (dev);
  779. }
  780. if (status & SCB_STCNA) {
  781. if (priv(dev)->resetting == 0)
  782. printk (KERN_WARNING "%s: CU went not ready ???\n", dev->name);
  783. else
  784. priv(dev)->resetting += 1;
  785. if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
  786. != (unsigned short)I82586_NULL) {
  787. ether1_writew(dev, SCB_CMDCUCSTART, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
  788. writeb(CTRL_CA, REG_CONTROL);
  789. }
  790. if (priv(dev)->resetting == 2)
  791. priv(dev)->resetting = 0;
  792. }
  793. if (status & SCB_STFR) {
  794. ether1_recv_done (dev);
  795. }
  796. if (status & SCB_STRNR) {
  797. if (ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS) & SCB_STRXSUSP) {
  798. printk (KERN_WARNING "%s: RU went not ready: RU suspended\n", dev->name);
  799. ether1_writew(dev, SCB_CMDRXRESUME, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
  800. writeb(CTRL_CA, REG_CONTROL);
  801. dev->stats.rx_dropped++; /* we suspended due to lack of buffer space */
  802. } else
  803. printk(KERN_WARNING "%s: RU went not ready: %04X\n", dev->name,
  804. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS));
  805. printk (KERN_WARNING "RU ptr = %04X\n", ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset,
  806. NORMALIRQS));
  807. }
  808. } else
  809. writeb(CTRL_ACK, REG_CONTROL);
  810. return IRQ_HANDLED;
  811. }
  812. static int
  813. ether1_close (struct net_device *dev)
  814. {
  815. ether1_reset (dev);
  816. free_irq(dev->irq, dev);
  817. return 0;
  818. }
  819. /*
  820. * Set or clear the multicast filter for this adaptor.
  821. * num_addrs == -1 Promiscuous mode, receive all packets.
  822. * num_addrs == 0 Normal mode, clear multicast list.
  823. * num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  824. * best-effort filtering.
  825. */
  826. static void
  827. ether1_setmulticastlist (struct net_device *dev)
  828. {
  829. }
  830. /* ------------------------------------------------------------------------- */
  831. static void ether1_banner(void)
  832. {
  833. static unsigned int version_printed = 0;
  834. if (net_debug && version_printed++ == 0)
  835. printk(KERN_INFO "%s", version);
  836. }
  837. static const struct net_device_ops ether1_netdev_ops = {
  838. .ndo_open = ether1_open,
  839. .ndo_stop = ether1_close,
  840. .ndo_start_xmit = ether1_sendpacket,
  841. .ndo_set_rx_mode = ether1_setmulticastlist,
  842. .ndo_tx_timeout = ether1_timeout,
  843. .ndo_validate_addr = eth_validate_addr,
  844. .ndo_set_mac_address = eth_mac_addr,
  845. };
  846. static int
  847. ether1_probe(struct expansion_card *ec, const struct ecard_id *id)
  848. {
  849. struct net_device *dev;
  850. int i, ret = 0;
  851. ether1_banner();
  852. ret = ecard_request_resources(ec);
  853. if (ret)
  854. goto out;
  855. dev = alloc_etherdev(sizeof(struct ether1_priv));
  856. if (!dev) {
  857. ret = -ENOMEM;
  858. goto release;
  859. }
  860. SET_NETDEV_DEV(dev, &ec->dev);
  861. dev->irq = ec->irq;
  862. priv(dev)->base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
  863. if (!priv(dev)->base) {
  864. ret = -ENOMEM;
  865. goto free;
  866. }
  867. if ((priv(dev)->bus_type = ether1_reset(dev)) == 0) {
  868. ret = -ENODEV;
  869. goto free;
  870. }
  871. for (i = 0; i < 6; i++)
  872. dev->dev_addr[i] = readb(IDPROM_ADDRESS + (i << 2));
  873. if (ether1_init_2(dev)) {
  874. ret = -ENODEV;
  875. goto free;
  876. }
  877. dev->netdev_ops = &ether1_netdev_ops;
  878. dev->watchdog_timeo = 5 * HZ / 100;
  879. ret = register_netdev(dev);
  880. if (ret)
  881. goto free;
  882. printk(KERN_INFO "%s: ether1 in slot %d, %pM\n",
  883. dev->name, ec->slot_no, dev->dev_addr);
  884. ecard_set_drvdata(ec, dev);
  885. return 0;
  886. free:
  887. free_netdev(dev);
  888. release:
  889. ecard_release_resources(ec);
  890. out:
  891. return ret;
  892. }
  893. static void ether1_remove(struct expansion_card *ec)
  894. {
  895. struct net_device *dev = ecard_get_drvdata(ec);
  896. ecard_set_drvdata(ec, NULL);
  897. unregister_netdev(dev);
  898. free_netdev(dev);
  899. ecard_release_resources(ec);
  900. }
  901. static const struct ecard_id ether1_ids[] = {
  902. { MANU_ACORN, PROD_ACORN_ETHER1 },
  903. { 0xffff, 0xffff }
  904. };
  905. static struct ecard_driver ether1_driver = {
  906. .probe = ether1_probe,
  907. .remove = ether1_remove,
  908. .id_table = ether1_ids,
  909. .drv = {
  910. .name = "ether1",
  911. },
  912. };
  913. static int __init ether1_init(void)
  914. {
  915. return ecard_register_driver(&ether1_driver);
  916. }
  917. static void __exit ether1_exit(void)
  918. {
  919. ecard_remove_driver(&ether1_driver);
  920. }
  921. module_init(ether1_init);
  922. module_exit(ether1_exit);
  923. MODULE_LICENSE("GPL");