lib82596.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. /* lasi_82596.c -- driver for the intel 82596 ethernet controller, as
  2. munged into HPPA boxen .
  3. This driver is based upon 82596.c, original credits are below...
  4. but there were too many hoops which HP wants jumped through to
  5. keep this code in there in a sane manner.
  6. 3 primary sources of the mess --
  7. 1) hppa needs *lots* of cacheline flushing to keep this kind of
  8. MMIO running.
  9. 2) The 82596 needs to see all of its pointers as their physical
  10. address. Thus virt_to_bus/bus_to_virt are *everywhere*.
  11. 3) The implementation HP is using seems to be significantly pickier
  12. about when and how the command and RX units are started. some
  13. command ordering was changed.
  14. Examination of the mach driver leads one to believe that there
  15. might be a saner way to pull this off... anyone who feels like a
  16. full rewrite can be my guest.
  17. Split 02/13/2000 Sam Creasey (sammy@oh.verio.com)
  18. 02/01/2000 Initial modifications for parisc by Helge Deller (deller@gmx.de)
  19. 03/02/2000 changes for better/correct(?) cache-flushing (deller)
  20. */
  21. /* 82596.c: A generic 82596 ethernet driver for linux. */
  22. /*
  23. Based on Apricot.c
  24. Written 1994 by Mark Evans.
  25. This driver is for the Apricot 82596 bus-master interface
  26. Modularised 12/94 Mark Evans
  27. Modified to support the 82596 ethernet chips on 680x0 VME boards.
  28. by Richard Hirst <richard@sleepie.demon.co.uk>
  29. Renamed to be 82596.c
  30. 980825: Changed to receive directly in to sk_buffs which are
  31. allocated at open() time. Eliminates copy on incoming frames
  32. (small ones are still copied). Shared data now held in a
  33. non-cached page, so we can run on 68060 in copyback mode.
  34. TBD:
  35. * look at deferring rx frames rather than discarding (as per tulip)
  36. * handle tx ring full as per tulip
  37. * performance test to tune rx_copybreak
  38. Most of my modifications relate to the braindead big-endian
  39. implementation by Intel. When the i596 is operating in
  40. 'big-endian' mode, it thinks a 32 bit value of 0x12345678
  41. should be stored as 0x56781234. This is a real pain, when
  42. you have linked lists which are shared by the 680x0 and the
  43. i596.
  44. Driver skeleton
  45. Written 1993 by Donald Becker.
  46. Copyright 1993 United States Government as represented by the Director,
  47. National Security Agency. This software may only be used and distributed
  48. according to the terms of the GNU General Public License as modified by SRC,
  49. incorporated herein by reference.
  50. The author may be reached as becker@scyld.com, or C/O
  51. Scyld Computing Corporation, 410 Severn Ave., Suite 210, Annapolis MD 21403
  52. */
  53. #include <linux/module.h>
  54. #include <linux/kernel.h>
  55. #include <linux/string.h>
  56. #include <linux/errno.h>
  57. #include <linux/ioport.h>
  58. #include <linux/interrupt.h>
  59. #include <linux/delay.h>
  60. #include <linux/netdevice.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/skbuff.h>
  63. #include <linux/types.h>
  64. #include <linux/bitops.h>
  65. #include <linux/dma-mapping.h>
  66. #include <linux/io.h>
  67. #include <linux/irq.h>
  68. #include <linux/gfp.h>
  69. /* DEBUG flags
  70. */
  71. #define DEB_INIT 0x0001
  72. #define DEB_PROBE 0x0002
  73. #define DEB_SERIOUS 0x0004
  74. #define DEB_ERRORS 0x0008
  75. #define DEB_MULTI 0x0010
  76. #define DEB_TDR 0x0020
  77. #define DEB_OPEN 0x0040
  78. #define DEB_RESET 0x0080
  79. #define DEB_ADDCMD 0x0100
  80. #define DEB_STATUS 0x0200
  81. #define DEB_STARTTX 0x0400
  82. #define DEB_RXADDR 0x0800
  83. #define DEB_TXADDR 0x1000
  84. #define DEB_RXFRAME 0x2000
  85. #define DEB_INTS 0x4000
  86. #define DEB_STRUCT 0x8000
  87. #define DEB_ANY 0xffff
  88. #define DEB(x, y) if (i596_debug & (x)) { y; }
  89. /*
  90. * The MPU_PORT command allows direct access to the 82596. With PORT access
  91. * the following commands are available (p5-18). The 32-bit port command
  92. * must be word-swapped with the most significant word written first.
  93. * This only applies to VME boards.
  94. */
  95. #define PORT_RESET 0x00 /* reset 82596 */
  96. #define PORT_SELFTEST 0x01 /* selftest */
  97. #define PORT_ALTSCP 0x02 /* alternate SCB address */
  98. #define PORT_ALTDUMP 0x03 /* Alternate DUMP address */
  99. static int i596_debug = (DEB_SERIOUS|DEB_PROBE);
  100. /* Copy frames shorter than rx_copybreak, otherwise pass on up in
  101. * a full sized sk_buff. Value of 100 stolen from tulip.c (!alpha).
  102. */
  103. static int rx_copybreak = 100;
  104. #define PKT_BUF_SZ 1536
  105. #define MAX_MC_CNT 64
  106. #define ISCP_BUSY 0x0001
  107. #define I596_NULL ((u32)0xffffffff)
  108. #define CMD_EOL 0x8000 /* The last command of the list, stop. */
  109. #define CMD_SUSP 0x4000 /* Suspend after doing cmd. */
  110. #define CMD_INTR 0x2000 /* Interrupt after doing cmd. */
  111. #define CMD_FLEX 0x0008 /* Enable flexible memory model */
  112. enum commands {
  113. CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
  114. CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7
  115. };
  116. #define STAT_C 0x8000 /* Set to 0 after execution */
  117. #define STAT_B 0x4000 /* Command being executed */
  118. #define STAT_OK 0x2000 /* Command executed ok */
  119. #define STAT_A 0x1000 /* Command aborted */
  120. #define CUC_START 0x0100
  121. #define CUC_RESUME 0x0200
  122. #define CUC_SUSPEND 0x0300
  123. #define CUC_ABORT 0x0400
  124. #define RX_START 0x0010
  125. #define RX_RESUME 0x0020
  126. #define RX_SUSPEND 0x0030
  127. #define RX_ABORT 0x0040
  128. #define TX_TIMEOUT (HZ/20)
  129. struct i596_reg {
  130. unsigned short porthi;
  131. unsigned short portlo;
  132. u32 ca;
  133. };
  134. #define EOF 0x8000
  135. #define SIZE_MASK 0x3fff
  136. struct i596_tbd {
  137. unsigned short size;
  138. unsigned short pad;
  139. u32 next;
  140. u32 data;
  141. u32 cache_pad[5]; /* Total 32 bytes... */
  142. };
  143. /* The command structure has two 'next' pointers; v_next is the address of
  144. * the next command as seen by the CPU, b_next is the address of the next
  145. * command as seen by the 82596. The b_next pointer, as used by the 82596
  146. * always references the status field of the next command, rather than the
  147. * v_next field, because the 82596 is unaware of v_next. It may seem more
  148. * logical to put v_next at the end of the structure, but we cannot do that
  149. * because the 82596 expects other fields to be there, depending on command
  150. * type.
  151. */
  152. struct i596_cmd {
  153. struct i596_cmd *v_next; /* Address from CPUs viewpoint */
  154. unsigned short status;
  155. unsigned short command;
  156. u32 b_next; /* Address from i596 viewpoint */
  157. };
  158. struct tx_cmd {
  159. struct i596_cmd cmd;
  160. u32 tbd;
  161. unsigned short size;
  162. unsigned short pad;
  163. struct sk_buff *skb; /* So we can free it after tx */
  164. dma_addr_t dma_addr;
  165. #ifdef __LP64__
  166. u32 cache_pad[6]; /* Total 64 bytes... */
  167. #else
  168. u32 cache_pad[1]; /* Total 32 bytes... */
  169. #endif
  170. };
  171. struct tdr_cmd {
  172. struct i596_cmd cmd;
  173. unsigned short status;
  174. unsigned short pad;
  175. };
  176. struct mc_cmd {
  177. struct i596_cmd cmd;
  178. short mc_cnt;
  179. char mc_addrs[MAX_MC_CNT*6];
  180. };
  181. struct sa_cmd {
  182. struct i596_cmd cmd;
  183. char eth_addr[8];
  184. };
  185. struct cf_cmd {
  186. struct i596_cmd cmd;
  187. char i596_config[16];
  188. };
  189. struct i596_rfd {
  190. unsigned short stat;
  191. unsigned short cmd;
  192. u32 b_next; /* Address from i596 viewpoint */
  193. u32 rbd;
  194. unsigned short count;
  195. unsigned short size;
  196. struct i596_rfd *v_next; /* Address from CPUs viewpoint */
  197. struct i596_rfd *v_prev;
  198. #ifndef __LP64__
  199. u32 cache_pad[2]; /* Total 32 bytes... */
  200. #endif
  201. };
  202. struct i596_rbd {
  203. /* hardware data */
  204. unsigned short count;
  205. unsigned short zero1;
  206. u32 b_next;
  207. u32 b_data; /* Address from i596 viewpoint */
  208. unsigned short size;
  209. unsigned short zero2;
  210. /* driver data */
  211. struct sk_buff *skb;
  212. struct i596_rbd *v_next;
  213. u32 b_addr; /* This rbd addr from i596 view */
  214. unsigned char *v_data; /* Address from CPUs viewpoint */
  215. /* Total 32 bytes... */
  216. #ifdef __LP64__
  217. u32 cache_pad[4];
  218. #endif
  219. };
  220. /* These values as chosen so struct i596_dma fits in one page... */
  221. #define TX_RING_SIZE 32
  222. #define RX_RING_SIZE 16
  223. struct i596_scb {
  224. unsigned short status;
  225. unsigned short command;
  226. u32 cmd;
  227. u32 rfd;
  228. u32 crc_err;
  229. u32 align_err;
  230. u32 resource_err;
  231. u32 over_err;
  232. u32 rcvdt_err;
  233. u32 short_err;
  234. unsigned short t_on;
  235. unsigned short t_off;
  236. };
  237. struct i596_iscp {
  238. u32 stat;
  239. u32 scb;
  240. };
  241. struct i596_scp {
  242. u32 sysbus;
  243. u32 pad;
  244. u32 iscp;
  245. };
  246. struct i596_dma {
  247. struct i596_scp scp __attribute__((aligned(32)));
  248. volatile struct i596_iscp iscp __attribute__((aligned(32)));
  249. volatile struct i596_scb scb __attribute__((aligned(32)));
  250. struct sa_cmd sa_cmd __attribute__((aligned(32)));
  251. struct cf_cmd cf_cmd __attribute__((aligned(32)));
  252. struct tdr_cmd tdr_cmd __attribute__((aligned(32)));
  253. struct mc_cmd mc_cmd __attribute__((aligned(32)));
  254. struct i596_rfd rfds[RX_RING_SIZE] __attribute__((aligned(32)));
  255. struct i596_rbd rbds[RX_RING_SIZE] __attribute__((aligned(32)));
  256. struct tx_cmd tx_cmds[TX_RING_SIZE] __attribute__((aligned(32)));
  257. struct i596_tbd tbds[TX_RING_SIZE] __attribute__((aligned(32)));
  258. };
  259. struct i596_private {
  260. struct i596_dma *dma;
  261. u32 stat;
  262. int last_restart;
  263. struct i596_rfd *rfd_head;
  264. struct i596_rbd *rbd_head;
  265. struct i596_cmd *cmd_tail;
  266. struct i596_cmd *cmd_head;
  267. int cmd_backlog;
  268. u32 last_cmd;
  269. int next_tx_cmd;
  270. int options;
  271. spinlock_t lock; /* serialize access to chip */
  272. dma_addr_t dma_addr;
  273. void __iomem *mpu_port;
  274. void __iomem *ca;
  275. };
  276. static const char init_setup[] =
  277. {
  278. 0x8E, /* length, prefetch on */
  279. 0xC8, /* fifo to 8, monitor off */
  280. 0x80, /* don't save bad frames */
  281. 0x2E, /* No source address insertion, 8 byte preamble */
  282. 0x00, /* priority and backoff defaults */
  283. 0x60, /* interframe spacing */
  284. 0x00, /* slot time LSB */
  285. 0xf2, /* slot time and retries */
  286. 0x00, /* promiscuous mode */
  287. 0x00, /* collision detect */
  288. 0x40, /* minimum frame length */
  289. 0xff,
  290. 0x00,
  291. 0x7f /* *multi IA */ };
  292. static int i596_open(struct net_device *dev);
  293. static int i596_start_xmit(struct sk_buff *skb, struct net_device *dev);
  294. static irqreturn_t i596_interrupt(int irq, void *dev_id);
  295. static int i596_close(struct net_device *dev);
  296. static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd);
  297. static void i596_tx_timeout (struct net_device *dev);
  298. static void print_eth(unsigned char *buf, char *str);
  299. static void set_multicast_list(struct net_device *dev);
  300. static inline void ca(struct net_device *dev);
  301. static void mpu_port(struct net_device *dev, int c, dma_addr_t x);
  302. static int rx_ring_size = RX_RING_SIZE;
  303. static int ticks_limit = 100;
  304. static int max_cmd_backlog = TX_RING_SIZE-1;
  305. #ifdef CONFIG_NET_POLL_CONTROLLER
  306. static void i596_poll_controller(struct net_device *dev);
  307. #endif
  308. static inline int wait_istat(struct net_device *dev, struct i596_dma *dma, int delcnt, char *str)
  309. {
  310. DMA_INV(dev, &(dma->iscp), sizeof(struct i596_iscp));
  311. while (--delcnt && dma->iscp.stat) {
  312. udelay(10);
  313. DMA_INV(dev, &(dma->iscp), sizeof(struct i596_iscp));
  314. }
  315. if (!delcnt) {
  316. printk(KERN_ERR "%s: %s, iscp.stat %04x, didn't clear\n",
  317. dev->name, str, SWAP16(dma->iscp.stat));
  318. return -1;
  319. } else
  320. return 0;
  321. }
  322. static inline int wait_cmd(struct net_device *dev, struct i596_dma *dma, int delcnt, char *str)
  323. {
  324. DMA_INV(dev, &(dma->scb), sizeof(struct i596_scb));
  325. while (--delcnt && dma->scb.command) {
  326. udelay(10);
  327. DMA_INV(dev, &(dma->scb), sizeof(struct i596_scb));
  328. }
  329. if (!delcnt) {
  330. printk(KERN_ERR "%s: %s, status %4.4x, cmd %4.4x.\n",
  331. dev->name, str,
  332. SWAP16(dma->scb.status),
  333. SWAP16(dma->scb.command));
  334. return -1;
  335. } else
  336. return 0;
  337. }
  338. static void i596_display_data(struct net_device *dev)
  339. {
  340. struct i596_private *lp = netdev_priv(dev);
  341. struct i596_dma *dma = lp->dma;
  342. struct i596_cmd *cmd;
  343. struct i596_rfd *rfd;
  344. struct i596_rbd *rbd;
  345. printk(KERN_DEBUG "lp and scp at %p, .sysbus = %08x, .iscp = %08x\n",
  346. &dma->scp, dma->scp.sysbus, SWAP32(dma->scp.iscp));
  347. printk(KERN_DEBUG "iscp at %p, iscp.stat = %08x, .scb = %08x\n",
  348. &dma->iscp, SWAP32(dma->iscp.stat), SWAP32(dma->iscp.scb));
  349. printk(KERN_DEBUG "scb at %p, scb.status = %04x, .command = %04x,"
  350. " .cmd = %08x, .rfd = %08x\n",
  351. &dma->scb, SWAP16(dma->scb.status), SWAP16(dma->scb.command),
  352. SWAP16(dma->scb.cmd), SWAP32(dma->scb.rfd));
  353. printk(KERN_DEBUG " errors: crc %x, align %x, resource %x,"
  354. " over %x, rcvdt %x, short %x\n",
  355. SWAP32(dma->scb.crc_err), SWAP32(dma->scb.align_err),
  356. SWAP32(dma->scb.resource_err), SWAP32(dma->scb.over_err),
  357. SWAP32(dma->scb.rcvdt_err), SWAP32(dma->scb.short_err));
  358. cmd = lp->cmd_head;
  359. while (cmd != NULL) {
  360. printk(KERN_DEBUG
  361. "cmd at %p, .status = %04x, .command = %04x,"
  362. " .b_next = %08x\n",
  363. cmd, SWAP16(cmd->status), SWAP16(cmd->command),
  364. SWAP32(cmd->b_next));
  365. cmd = cmd->v_next;
  366. }
  367. rfd = lp->rfd_head;
  368. printk(KERN_DEBUG "rfd_head = %p\n", rfd);
  369. do {
  370. printk(KERN_DEBUG
  371. " %p .stat %04x, .cmd %04x, b_next %08x, rbd %08x,"
  372. " count %04x\n",
  373. rfd, SWAP16(rfd->stat), SWAP16(rfd->cmd),
  374. SWAP32(rfd->b_next), SWAP32(rfd->rbd),
  375. SWAP16(rfd->count));
  376. rfd = rfd->v_next;
  377. } while (rfd != lp->rfd_head);
  378. rbd = lp->rbd_head;
  379. printk(KERN_DEBUG "rbd_head = %p\n", rbd);
  380. do {
  381. printk(KERN_DEBUG
  382. " %p .count %04x, b_next %08x, b_data %08x,"
  383. " size %04x\n",
  384. rbd, SWAP16(rbd->count), SWAP32(rbd->b_next),
  385. SWAP32(rbd->b_data), SWAP16(rbd->size));
  386. rbd = rbd->v_next;
  387. } while (rbd != lp->rbd_head);
  388. DMA_INV(dev, dma, sizeof(struct i596_dma));
  389. }
  390. #define virt_to_dma(lp, v) ((lp)->dma_addr + (dma_addr_t)((unsigned long)(v)-(unsigned long)((lp)->dma)))
  391. static inline int init_rx_bufs(struct net_device *dev)
  392. {
  393. struct i596_private *lp = netdev_priv(dev);
  394. struct i596_dma *dma = lp->dma;
  395. int i;
  396. struct i596_rfd *rfd;
  397. struct i596_rbd *rbd;
  398. /* First build the Receive Buffer Descriptor List */
  399. for (i = 0, rbd = dma->rbds; i < rx_ring_size; i++, rbd++) {
  400. dma_addr_t dma_addr;
  401. struct sk_buff *skb;
  402. skb = netdev_alloc_skb_ip_align(dev, PKT_BUF_SZ);
  403. if (skb == NULL)
  404. return -1;
  405. dma_addr = dma_map_single(dev->dev.parent, skb->data,
  406. PKT_BUF_SZ, DMA_FROM_DEVICE);
  407. rbd->v_next = rbd+1;
  408. rbd->b_next = SWAP32(virt_to_dma(lp, rbd+1));
  409. rbd->b_addr = SWAP32(virt_to_dma(lp, rbd));
  410. rbd->skb = skb;
  411. rbd->v_data = skb->data;
  412. rbd->b_data = SWAP32(dma_addr);
  413. rbd->size = SWAP16(PKT_BUF_SZ);
  414. }
  415. lp->rbd_head = dma->rbds;
  416. rbd = dma->rbds + rx_ring_size - 1;
  417. rbd->v_next = dma->rbds;
  418. rbd->b_next = SWAP32(virt_to_dma(lp, dma->rbds));
  419. /* Now build the Receive Frame Descriptor List */
  420. for (i = 0, rfd = dma->rfds; i < rx_ring_size; i++, rfd++) {
  421. rfd->rbd = I596_NULL;
  422. rfd->v_next = rfd+1;
  423. rfd->v_prev = rfd-1;
  424. rfd->b_next = SWAP32(virt_to_dma(lp, rfd+1));
  425. rfd->cmd = SWAP16(CMD_FLEX);
  426. }
  427. lp->rfd_head = dma->rfds;
  428. dma->scb.rfd = SWAP32(virt_to_dma(lp, dma->rfds));
  429. rfd = dma->rfds;
  430. rfd->rbd = SWAP32(virt_to_dma(lp, lp->rbd_head));
  431. rfd->v_prev = dma->rfds + rx_ring_size - 1;
  432. rfd = dma->rfds + rx_ring_size - 1;
  433. rfd->v_next = dma->rfds;
  434. rfd->b_next = SWAP32(virt_to_dma(lp, dma->rfds));
  435. rfd->cmd = SWAP16(CMD_EOL|CMD_FLEX);
  436. DMA_WBACK_INV(dev, dma, sizeof(struct i596_dma));
  437. return 0;
  438. }
  439. static inline void remove_rx_bufs(struct net_device *dev)
  440. {
  441. struct i596_private *lp = netdev_priv(dev);
  442. struct i596_rbd *rbd;
  443. int i;
  444. for (i = 0, rbd = lp->dma->rbds; i < rx_ring_size; i++, rbd++) {
  445. if (rbd->skb == NULL)
  446. break;
  447. dma_unmap_single(dev->dev.parent,
  448. (dma_addr_t)SWAP32(rbd->b_data),
  449. PKT_BUF_SZ, DMA_FROM_DEVICE);
  450. dev_kfree_skb(rbd->skb);
  451. }
  452. }
  453. static void rebuild_rx_bufs(struct net_device *dev)
  454. {
  455. struct i596_private *lp = netdev_priv(dev);
  456. struct i596_dma *dma = lp->dma;
  457. int i;
  458. /* Ensure rx frame/buffer descriptors are tidy */
  459. for (i = 0; i < rx_ring_size; i++) {
  460. dma->rfds[i].rbd = I596_NULL;
  461. dma->rfds[i].cmd = SWAP16(CMD_FLEX);
  462. }
  463. dma->rfds[rx_ring_size-1].cmd = SWAP16(CMD_EOL|CMD_FLEX);
  464. lp->rfd_head = dma->rfds;
  465. dma->scb.rfd = SWAP32(virt_to_dma(lp, dma->rfds));
  466. lp->rbd_head = dma->rbds;
  467. dma->rfds[0].rbd = SWAP32(virt_to_dma(lp, dma->rbds));
  468. DMA_WBACK_INV(dev, dma, sizeof(struct i596_dma));
  469. }
  470. static int init_i596_mem(struct net_device *dev)
  471. {
  472. struct i596_private *lp = netdev_priv(dev);
  473. struct i596_dma *dma = lp->dma;
  474. unsigned long flags;
  475. mpu_port(dev, PORT_RESET, 0);
  476. udelay(100); /* Wait 100us - seems to help */
  477. /* change the scp address */
  478. lp->last_cmd = jiffies;
  479. dma->scp.sysbus = SYSBUS;
  480. dma->scp.iscp = SWAP32(virt_to_dma(lp, &(dma->iscp)));
  481. dma->iscp.scb = SWAP32(virt_to_dma(lp, &(dma->scb)));
  482. dma->iscp.stat = SWAP32(ISCP_BUSY);
  483. lp->cmd_backlog = 0;
  484. lp->cmd_head = NULL;
  485. dma->scb.cmd = I596_NULL;
  486. DEB(DEB_INIT, printk(KERN_DEBUG "%s: starting i82596.\n", dev->name));
  487. DMA_WBACK(dev, &(dma->scp), sizeof(struct i596_scp));
  488. DMA_WBACK(dev, &(dma->iscp), sizeof(struct i596_iscp));
  489. DMA_WBACK(dev, &(dma->scb), sizeof(struct i596_scb));
  490. mpu_port(dev, PORT_ALTSCP, virt_to_dma(lp, &dma->scp));
  491. ca(dev);
  492. if (wait_istat(dev, dma, 1000, "initialization timed out"))
  493. goto failed;
  494. DEB(DEB_INIT, printk(KERN_DEBUG
  495. "%s: i82596 initialization successful\n",
  496. dev->name));
  497. if (request_irq(dev->irq, i596_interrupt, 0, "i82596", dev)) {
  498. printk(KERN_ERR "%s: IRQ %d not free\n", dev->name, dev->irq);
  499. goto failed;
  500. }
  501. /* Ensure rx frame/buffer descriptors are tidy */
  502. rebuild_rx_bufs(dev);
  503. dma->scb.command = 0;
  504. DMA_WBACK(dev, &(dma->scb), sizeof(struct i596_scb));
  505. DEB(DEB_INIT, printk(KERN_DEBUG
  506. "%s: queuing CmdConfigure\n", dev->name));
  507. memcpy(dma->cf_cmd.i596_config, init_setup, 14);
  508. dma->cf_cmd.cmd.command = SWAP16(CmdConfigure);
  509. DMA_WBACK(dev, &(dma->cf_cmd), sizeof(struct cf_cmd));
  510. i596_add_cmd(dev, &dma->cf_cmd.cmd);
  511. DEB(DEB_INIT, printk(KERN_DEBUG "%s: queuing CmdSASetup\n", dev->name));
  512. memcpy(dma->sa_cmd.eth_addr, dev->dev_addr, ETH_ALEN);
  513. dma->sa_cmd.cmd.command = SWAP16(CmdSASetup);
  514. DMA_WBACK(dev, &(dma->sa_cmd), sizeof(struct sa_cmd));
  515. i596_add_cmd(dev, &dma->sa_cmd.cmd);
  516. DEB(DEB_INIT, printk(KERN_DEBUG "%s: queuing CmdTDR\n", dev->name));
  517. dma->tdr_cmd.cmd.command = SWAP16(CmdTDR);
  518. DMA_WBACK(dev, &(dma->tdr_cmd), sizeof(struct tdr_cmd));
  519. i596_add_cmd(dev, &dma->tdr_cmd.cmd);
  520. spin_lock_irqsave (&lp->lock, flags);
  521. if (wait_cmd(dev, dma, 1000, "timed out waiting to issue RX_START")) {
  522. spin_unlock_irqrestore (&lp->lock, flags);
  523. goto failed_free_irq;
  524. }
  525. DEB(DEB_INIT, printk(KERN_DEBUG "%s: Issuing RX_START\n", dev->name));
  526. dma->scb.command = SWAP16(RX_START);
  527. dma->scb.rfd = SWAP32(virt_to_dma(lp, dma->rfds));
  528. DMA_WBACK(dev, &(dma->scb), sizeof(struct i596_scb));
  529. ca(dev);
  530. spin_unlock_irqrestore (&lp->lock, flags);
  531. if (wait_cmd(dev, dma, 1000, "RX_START not processed"))
  532. goto failed_free_irq;
  533. DEB(DEB_INIT, printk(KERN_DEBUG
  534. "%s: Receive unit started OK\n", dev->name));
  535. return 0;
  536. failed_free_irq:
  537. free_irq(dev->irq, dev);
  538. failed:
  539. printk(KERN_ERR "%s: Failed to initialise 82596\n", dev->name);
  540. mpu_port(dev, PORT_RESET, 0);
  541. return -1;
  542. }
  543. static inline int i596_rx(struct net_device *dev)
  544. {
  545. struct i596_private *lp = netdev_priv(dev);
  546. struct i596_rfd *rfd;
  547. struct i596_rbd *rbd;
  548. int frames = 0;
  549. DEB(DEB_RXFRAME, printk(KERN_DEBUG
  550. "i596_rx(), rfd_head %p, rbd_head %p\n",
  551. lp->rfd_head, lp->rbd_head));
  552. rfd = lp->rfd_head; /* Ref next frame to check */
  553. DMA_INV(dev, rfd, sizeof(struct i596_rfd));
  554. while (rfd->stat & SWAP16(STAT_C)) { /* Loop while complete frames */
  555. if (rfd->rbd == I596_NULL)
  556. rbd = NULL;
  557. else if (rfd->rbd == lp->rbd_head->b_addr) {
  558. rbd = lp->rbd_head;
  559. DMA_INV(dev, rbd, sizeof(struct i596_rbd));
  560. } else {
  561. printk(KERN_ERR "%s: rbd chain broken!\n", dev->name);
  562. /* XXX Now what? */
  563. rbd = NULL;
  564. }
  565. DEB(DEB_RXFRAME, printk(KERN_DEBUG
  566. " rfd %p, rfd.rbd %08x, rfd.stat %04x\n",
  567. rfd, rfd->rbd, rfd->stat));
  568. if (rbd != NULL && (rfd->stat & SWAP16(STAT_OK))) {
  569. /* a good frame */
  570. int pkt_len = SWAP16(rbd->count) & 0x3fff;
  571. struct sk_buff *skb = rbd->skb;
  572. int rx_in_place = 0;
  573. DEB(DEB_RXADDR, print_eth(rbd->v_data, "received"));
  574. frames++;
  575. /* Check if the packet is long enough to just accept
  576. * without copying to a properly sized skbuff.
  577. */
  578. if (pkt_len > rx_copybreak) {
  579. struct sk_buff *newskb;
  580. dma_addr_t dma_addr;
  581. dma_unmap_single(dev->dev.parent,
  582. (dma_addr_t)SWAP32(rbd->b_data),
  583. PKT_BUF_SZ, DMA_FROM_DEVICE);
  584. /* Get fresh skbuff to replace filled one. */
  585. newskb = netdev_alloc_skb_ip_align(dev,
  586. PKT_BUF_SZ);
  587. if (newskb == NULL) {
  588. skb = NULL; /* drop pkt */
  589. goto memory_squeeze;
  590. }
  591. /* Pass up the skb already on the Rx ring. */
  592. skb_put(skb, pkt_len);
  593. rx_in_place = 1;
  594. rbd->skb = newskb;
  595. dma_addr = dma_map_single(dev->dev.parent,
  596. newskb->data,
  597. PKT_BUF_SZ,
  598. DMA_FROM_DEVICE);
  599. rbd->v_data = newskb->data;
  600. rbd->b_data = SWAP32(dma_addr);
  601. DMA_WBACK_INV(dev, rbd, sizeof(struct i596_rbd));
  602. } else {
  603. skb = netdev_alloc_skb_ip_align(dev, pkt_len);
  604. }
  605. memory_squeeze:
  606. if (skb == NULL) {
  607. /* XXX tulip.c can defer packets here!! */
  608. dev->stats.rx_dropped++;
  609. } else {
  610. if (!rx_in_place) {
  611. /* 16 byte align the data fields */
  612. dma_sync_single_for_cpu(dev->dev.parent,
  613. (dma_addr_t)SWAP32(rbd->b_data),
  614. PKT_BUF_SZ, DMA_FROM_DEVICE);
  615. memcpy(skb_put(skb, pkt_len), rbd->v_data, pkt_len);
  616. dma_sync_single_for_device(dev->dev.parent,
  617. (dma_addr_t)SWAP32(rbd->b_data),
  618. PKT_BUF_SZ, DMA_FROM_DEVICE);
  619. }
  620. skb->len = pkt_len;
  621. skb->protocol = eth_type_trans(skb, dev);
  622. netif_rx(skb);
  623. dev->stats.rx_packets++;
  624. dev->stats.rx_bytes += pkt_len;
  625. }
  626. } else {
  627. DEB(DEB_ERRORS, printk(KERN_DEBUG
  628. "%s: Error, rfd.stat = 0x%04x\n",
  629. dev->name, rfd->stat));
  630. dev->stats.rx_errors++;
  631. if (rfd->stat & SWAP16(0x0100))
  632. dev->stats.collisions++;
  633. if (rfd->stat & SWAP16(0x8000))
  634. dev->stats.rx_length_errors++;
  635. if (rfd->stat & SWAP16(0x0001))
  636. dev->stats.rx_over_errors++;
  637. if (rfd->stat & SWAP16(0x0002))
  638. dev->stats.rx_fifo_errors++;
  639. if (rfd->stat & SWAP16(0x0004))
  640. dev->stats.rx_frame_errors++;
  641. if (rfd->stat & SWAP16(0x0008))
  642. dev->stats.rx_crc_errors++;
  643. if (rfd->stat & SWAP16(0x0010))
  644. dev->stats.rx_length_errors++;
  645. }
  646. /* Clear the buffer descriptor count and EOF + F flags */
  647. if (rbd != NULL && (rbd->count & SWAP16(0x4000))) {
  648. rbd->count = 0;
  649. lp->rbd_head = rbd->v_next;
  650. DMA_WBACK_INV(dev, rbd, sizeof(struct i596_rbd));
  651. }
  652. /* Tidy the frame descriptor, marking it as end of list */
  653. rfd->rbd = I596_NULL;
  654. rfd->stat = 0;
  655. rfd->cmd = SWAP16(CMD_EOL|CMD_FLEX);
  656. rfd->count = 0;
  657. /* Update record of next frame descriptor to process */
  658. lp->dma->scb.rfd = rfd->b_next;
  659. lp->rfd_head = rfd->v_next;
  660. DMA_WBACK_INV(dev, rfd, sizeof(struct i596_rfd));
  661. /* Remove end-of-list from old end descriptor */
  662. rfd->v_prev->cmd = SWAP16(CMD_FLEX);
  663. DMA_WBACK_INV(dev, rfd->v_prev, sizeof(struct i596_rfd));
  664. rfd = lp->rfd_head;
  665. DMA_INV(dev, rfd, sizeof(struct i596_rfd));
  666. }
  667. DEB(DEB_RXFRAME, printk(KERN_DEBUG "frames %d\n", frames));
  668. return 0;
  669. }
  670. static inline void i596_cleanup_cmd(struct net_device *dev, struct i596_private *lp)
  671. {
  672. struct i596_cmd *ptr;
  673. while (lp->cmd_head != NULL) {
  674. ptr = lp->cmd_head;
  675. lp->cmd_head = ptr->v_next;
  676. lp->cmd_backlog--;
  677. switch (SWAP16(ptr->command) & 0x7) {
  678. case CmdTx:
  679. {
  680. struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
  681. struct sk_buff *skb = tx_cmd->skb;
  682. dma_unmap_single(dev->dev.parent,
  683. tx_cmd->dma_addr,
  684. skb->len, DMA_TO_DEVICE);
  685. dev_kfree_skb(skb);
  686. dev->stats.tx_errors++;
  687. dev->stats.tx_aborted_errors++;
  688. ptr->v_next = NULL;
  689. ptr->b_next = I596_NULL;
  690. tx_cmd->cmd.command = 0; /* Mark as free */
  691. break;
  692. }
  693. default:
  694. ptr->v_next = NULL;
  695. ptr->b_next = I596_NULL;
  696. }
  697. DMA_WBACK_INV(dev, ptr, sizeof(struct i596_cmd));
  698. }
  699. wait_cmd(dev, lp->dma, 100, "i596_cleanup_cmd timed out");
  700. lp->dma->scb.cmd = I596_NULL;
  701. DMA_WBACK(dev, &(lp->dma->scb), sizeof(struct i596_scb));
  702. }
  703. static inline void i596_reset(struct net_device *dev, struct i596_private *lp)
  704. {
  705. unsigned long flags;
  706. DEB(DEB_RESET, printk(KERN_DEBUG "i596_reset\n"));
  707. spin_lock_irqsave (&lp->lock, flags);
  708. wait_cmd(dev, lp->dma, 100, "i596_reset timed out");
  709. netif_stop_queue(dev);
  710. /* FIXME: this command might cause an lpmc */
  711. lp->dma->scb.command = SWAP16(CUC_ABORT | RX_ABORT);
  712. DMA_WBACK(dev, &(lp->dma->scb), sizeof(struct i596_scb));
  713. ca(dev);
  714. /* wait for shutdown */
  715. wait_cmd(dev, lp->dma, 1000, "i596_reset 2 timed out");
  716. spin_unlock_irqrestore (&lp->lock, flags);
  717. i596_cleanup_cmd(dev, lp);
  718. i596_rx(dev);
  719. netif_start_queue(dev);
  720. init_i596_mem(dev);
  721. }
  722. static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd)
  723. {
  724. struct i596_private *lp = netdev_priv(dev);
  725. struct i596_dma *dma = lp->dma;
  726. unsigned long flags;
  727. DEB(DEB_ADDCMD, printk(KERN_DEBUG "i596_add_cmd cmd_head %p\n",
  728. lp->cmd_head));
  729. cmd->status = 0;
  730. cmd->command |= SWAP16(CMD_EOL | CMD_INTR);
  731. cmd->v_next = NULL;
  732. cmd->b_next = I596_NULL;
  733. DMA_WBACK(dev, cmd, sizeof(struct i596_cmd));
  734. spin_lock_irqsave (&lp->lock, flags);
  735. if (lp->cmd_head != NULL) {
  736. lp->cmd_tail->v_next = cmd;
  737. lp->cmd_tail->b_next = SWAP32(virt_to_dma(lp, &cmd->status));
  738. DMA_WBACK(dev, lp->cmd_tail, sizeof(struct i596_cmd));
  739. } else {
  740. lp->cmd_head = cmd;
  741. wait_cmd(dev, dma, 100, "i596_add_cmd timed out");
  742. dma->scb.cmd = SWAP32(virt_to_dma(lp, &cmd->status));
  743. dma->scb.command = SWAP16(CUC_START);
  744. DMA_WBACK(dev, &(dma->scb), sizeof(struct i596_scb));
  745. ca(dev);
  746. }
  747. lp->cmd_tail = cmd;
  748. lp->cmd_backlog++;
  749. spin_unlock_irqrestore (&lp->lock, flags);
  750. if (lp->cmd_backlog > max_cmd_backlog) {
  751. unsigned long tickssofar = jiffies - lp->last_cmd;
  752. if (tickssofar < ticks_limit)
  753. return;
  754. printk(KERN_ERR
  755. "%s: command unit timed out, status resetting.\n",
  756. dev->name);
  757. #if 1
  758. i596_reset(dev, lp);
  759. #endif
  760. }
  761. }
  762. static int i596_open(struct net_device *dev)
  763. {
  764. DEB(DEB_OPEN, printk(KERN_DEBUG
  765. "%s: i596_open() irq %d.\n", dev->name, dev->irq));
  766. if (init_rx_bufs(dev)) {
  767. printk(KERN_ERR "%s: Failed to init rx bufs\n", dev->name);
  768. return -EAGAIN;
  769. }
  770. if (init_i596_mem(dev)) {
  771. printk(KERN_ERR "%s: Failed to init memory\n", dev->name);
  772. goto out_remove_rx_bufs;
  773. }
  774. netif_start_queue(dev);
  775. return 0;
  776. out_remove_rx_bufs:
  777. remove_rx_bufs(dev);
  778. return -EAGAIN;
  779. }
  780. static void i596_tx_timeout (struct net_device *dev)
  781. {
  782. struct i596_private *lp = netdev_priv(dev);
  783. /* Transmitter timeout, serious problems. */
  784. DEB(DEB_ERRORS, printk(KERN_DEBUG
  785. "%s: transmit timed out, status resetting.\n",
  786. dev->name));
  787. dev->stats.tx_errors++;
  788. /* Try to restart the adaptor */
  789. if (lp->last_restart == dev->stats.tx_packets) {
  790. DEB(DEB_ERRORS, printk(KERN_DEBUG "Resetting board.\n"));
  791. /* Shutdown and restart */
  792. i596_reset (dev, lp);
  793. } else {
  794. /* Issue a channel attention signal */
  795. DEB(DEB_ERRORS, printk(KERN_DEBUG "Kicking board.\n"));
  796. lp->dma->scb.command = SWAP16(CUC_START | RX_START);
  797. DMA_WBACK_INV(dev, &(lp->dma->scb), sizeof(struct i596_scb));
  798. ca (dev);
  799. lp->last_restart = dev->stats.tx_packets;
  800. }
  801. dev->trans_start = jiffies; /* prevent tx timeout */
  802. netif_wake_queue (dev);
  803. }
  804. static int i596_start_xmit(struct sk_buff *skb, struct net_device *dev)
  805. {
  806. struct i596_private *lp = netdev_priv(dev);
  807. struct tx_cmd *tx_cmd;
  808. struct i596_tbd *tbd;
  809. short length = skb->len;
  810. DEB(DEB_STARTTX, printk(KERN_DEBUG
  811. "%s: i596_start_xmit(%x,%p) called\n",
  812. dev->name, skb->len, skb->data));
  813. if (length < ETH_ZLEN) {
  814. if (skb_padto(skb, ETH_ZLEN))
  815. return NETDEV_TX_OK;
  816. length = ETH_ZLEN;
  817. }
  818. netif_stop_queue(dev);
  819. tx_cmd = lp->dma->tx_cmds + lp->next_tx_cmd;
  820. tbd = lp->dma->tbds + lp->next_tx_cmd;
  821. if (tx_cmd->cmd.command) {
  822. DEB(DEB_ERRORS, printk(KERN_DEBUG
  823. "%s: xmit ring full, dropping packet.\n",
  824. dev->name));
  825. dev->stats.tx_dropped++;
  826. dev_kfree_skb_any(skb);
  827. } else {
  828. if (++lp->next_tx_cmd == TX_RING_SIZE)
  829. lp->next_tx_cmd = 0;
  830. tx_cmd->tbd = SWAP32(virt_to_dma(lp, tbd));
  831. tbd->next = I596_NULL;
  832. tx_cmd->cmd.command = SWAP16(CMD_FLEX | CmdTx);
  833. tx_cmd->skb = skb;
  834. tx_cmd->pad = 0;
  835. tx_cmd->size = 0;
  836. tbd->pad = 0;
  837. tbd->size = SWAP16(EOF | length);
  838. tx_cmd->dma_addr = dma_map_single(dev->dev.parent, skb->data,
  839. skb->len, DMA_TO_DEVICE);
  840. tbd->data = SWAP32(tx_cmd->dma_addr);
  841. DEB(DEB_TXADDR, print_eth(skb->data, "tx-queued"));
  842. DMA_WBACK_INV(dev, tx_cmd, sizeof(struct tx_cmd));
  843. DMA_WBACK_INV(dev, tbd, sizeof(struct i596_tbd));
  844. i596_add_cmd(dev, &tx_cmd->cmd);
  845. dev->stats.tx_packets++;
  846. dev->stats.tx_bytes += length;
  847. }
  848. netif_start_queue(dev);
  849. return NETDEV_TX_OK;
  850. }
  851. static void print_eth(unsigned char *add, char *str)
  852. {
  853. printk(KERN_DEBUG "i596 0x%p, %pM --> %pM %02X%02X, %s\n",
  854. add, add + 6, add, add[12], add[13], str);
  855. }
  856. static const struct net_device_ops i596_netdev_ops = {
  857. .ndo_open = i596_open,
  858. .ndo_stop = i596_close,
  859. .ndo_start_xmit = i596_start_xmit,
  860. .ndo_set_rx_mode = set_multicast_list,
  861. .ndo_tx_timeout = i596_tx_timeout,
  862. .ndo_change_mtu = eth_change_mtu,
  863. .ndo_validate_addr = eth_validate_addr,
  864. .ndo_set_mac_address = eth_mac_addr,
  865. #ifdef CONFIG_NET_POLL_CONTROLLER
  866. .ndo_poll_controller = i596_poll_controller,
  867. #endif
  868. };
  869. static int i82596_probe(struct net_device *dev)
  870. {
  871. int i;
  872. struct i596_private *lp = netdev_priv(dev);
  873. struct i596_dma *dma;
  874. /* This lot is ensure things have been cache line aligned. */
  875. BUILD_BUG_ON(sizeof(struct i596_rfd) != 32);
  876. BUILD_BUG_ON(sizeof(struct i596_rbd) & 31);
  877. BUILD_BUG_ON(sizeof(struct tx_cmd) & 31);
  878. BUILD_BUG_ON(sizeof(struct i596_tbd) != 32);
  879. #ifndef __LP64__
  880. BUILD_BUG_ON(sizeof(struct i596_dma) > 4096);
  881. #endif
  882. if (!dev->base_addr || !dev->irq)
  883. return -ENODEV;
  884. dma = (struct i596_dma *) DMA_ALLOC(dev->dev.parent,
  885. sizeof(struct i596_dma), &lp->dma_addr, GFP_KERNEL);
  886. if (!dma) {
  887. printk(KERN_ERR "%s: Couldn't get shared memory\n", __FILE__);
  888. return -ENOMEM;
  889. }
  890. dev->netdev_ops = &i596_netdev_ops;
  891. dev->watchdog_timeo = TX_TIMEOUT;
  892. memset(dma, 0, sizeof(struct i596_dma));
  893. lp->dma = dma;
  894. dma->scb.command = 0;
  895. dma->scb.cmd = I596_NULL;
  896. dma->scb.rfd = I596_NULL;
  897. spin_lock_init(&lp->lock);
  898. DMA_WBACK_INV(dev, dma, sizeof(struct i596_dma));
  899. i = register_netdev(dev);
  900. if (i) {
  901. DMA_FREE(dev->dev.parent, sizeof(struct i596_dma),
  902. (void *)dma, lp->dma_addr);
  903. return i;
  904. }
  905. DEB(DEB_PROBE, printk(KERN_INFO "%s: 82596 at %#3lx, %pM IRQ %d.\n",
  906. dev->name, dev->base_addr, dev->dev_addr,
  907. dev->irq));
  908. DEB(DEB_INIT, printk(KERN_INFO
  909. "%s: dma at 0x%p (%d bytes), lp->scb at 0x%p\n",
  910. dev->name, dma, (int)sizeof(struct i596_dma),
  911. &dma->scb));
  912. return 0;
  913. }
  914. #ifdef CONFIG_NET_POLL_CONTROLLER
  915. static void i596_poll_controller(struct net_device *dev)
  916. {
  917. disable_irq(dev->irq);
  918. i596_interrupt(dev->irq, dev);
  919. enable_irq(dev->irq);
  920. }
  921. #endif
  922. static irqreturn_t i596_interrupt(int irq, void *dev_id)
  923. {
  924. struct net_device *dev = dev_id;
  925. struct i596_private *lp;
  926. struct i596_dma *dma;
  927. unsigned short status, ack_cmd = 0;
  928. lp = netdev_priv(dev);
  929. dma = lp->dma;
  930. spin_lock (&lp->lock);
  931. wait_cmd(dev, dma, 100, "i596 interrupt, timeout");
  932. status = SWAP16(dma->scb.status);
  933. DEB(DEB_INTS, printk(KERN_DEBUG
  934. "%s: i596 interrupt, IRQ %d, status %4.4x.\n",
  935. dev->name, dev->irq, status));
  936. ack_cmd = status & 0xf000;
  937. if (!ack_cmd) {
  938. DEB(DEB_ERRORS, printk(KERN_DEBUG
  939. "%s: interrupt with no events\n",
  940. dev->name));
  941. spin_unlock (&lp->lock);
  942. return IRQ_NONE;
  943. }
  944. if ((status & 0x8000) || (status & 0x2000)) {
  945. struct i596_cmd *ptr;
  946. if ((status & 0x8000))
  947. DEB(DEB_INTS,
  948. printk(KERN_DEBUG
  949. "%s: i596 interrupt completed command.\n",
  950. dev->name));
  951. if ((status & 0x2000))
  952. DEB(DEB_INTS,
  953. printk(KERN_DEBUG
  954. "%s: i596 interrupt command unit inactive %x.\n",
  955. dev->name, status & 0x0700));
  956. while (lp->cmd_head != NULL) {
  957. DMA_INV(dev, lp->cmd_head, sizeof(struct i596_cmd));
  958. if (!(lp->cmd_head->status & SWAP16(STAT_C)))
  959. break;
  960. ptr = lp->cmd_head;
  961. DEB(DEB_STATUS,
  962. printk(KERN_DEBUG
  963. "cmd_head->status = %04x, ->command = %04x\n",
  964. SWAP16(lp->cmd_head->status),
  965. SWAP16(lp->cmd_head->command)));
  966. lp->cmd_head = ptr->v_next;
  967. lp->cmd_backlog--;
  968. switch (SWAP16(ptr->command) & 0x7) {
  969. case CmdTx:
  970. {
  971. struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
  972. struct sk_buff *skb = tx_cmd->skb;
  973. if (ptr->status & SWAP16(STAT_OK)) {
  974. DEB(DEB_TXADDR,
  975. print_eth(skb->data, "tx-done"));
  976. } else {
  977. dev->stats.tx_errors++;
  978. if (ptr->status & SWAP16(0x0020))
  979. dev->stats.collisions++;
  980. if (!(ptr->status & SWAP16(0x0040)))
  981. dev->stats.tx_heartbeat_errors++;
  982. if (ptr->status & SWAP16(0x0400))
  983. dev->stats.tx_carrier_errors++;
  984. if (ptr->status & SWAP16(0x0800))
  985. dev->stats.collisions++;
  986. if (ptr->status & SWAP16(0x1000))
  987. dev->stats.tx_aborted_errors++;
  988. }
  989. dma_unmap_single(dev->dev.parent,
  990. tx_cmd->dma_addr,
  991. skb->len, DMA_TO_DEVICE);
  992. dev_kfree_skb_irq(skb);
  993. tx_cmd->cmd.command = 0; /* Mark free */
  994. break;
  995. }
  996. case CmdTDR:
  997. {
  998. unsigned short status = SWAP16(((struct tdr_cmd *)ptr)->status);
  999. if (status & 0x8000) {
  1000. DEB(DEB_ANY,
  1001. printk(KERN_DEBUG "%s: link ok.\n",
  1002. dev->name));
  1003. } else {
  1004. if (status & 0x4000)
  1005. printk(KERN_ERR
  1006. "%s: Transceiver problem.\n",
  1007. dev->name);
  1008. if (status & 0x2000)
  1009. printk(KERN_ERR
  1010. "%s: Termination problem.\n",
  1011. dev->name);
  1012. if (status & 0x1000)
  1013. printk(KERN_ERR
  1014. "%s: Short circuit.\n",
  1015. dev->name);
  1016. DEB(DEB_TDR,
  1017. printk(KERN_DEBUG "%s: Time %d.\n",
  1018. dev->name, status & 0x07ff));
  1019. }
  1020. break;
  1021. }
  1022. case CmdConfigure:
  1023. /*
  1024. * Zap command so set_multicast_list() know
  1025. * it is free
  1026. */
  1027. ptr->command = 0;
  1028. break;
  1029. }
  1030. ptr->v_next = NULL;
  1031. ptr->b_next = I596_NULL;
  1032. DMA_WBACK(dev, ptr, sizeof(struct i596_cmd));
  1033. lp->last_cmd = jiffies;
  1034. }
  1035. /* This mess is arranging that only the last of any outstanding
  1036. * commands has the interrupt bit set. Should probably really
  1037. * only add to the cmd queue when the CU is stopped.
  1038. */
  1039. ptr = lp->cmd_head;
  1040. while ((ptr != NULL) && (ptr != lp->cmd_tail)) {
  1041. struct i596_cmd *prev = ptr;
  1042. ptr->command &= SWAP16(0x1fff);
  1043. ptr = ptr->v_next;
  1044. DMA_WBACK_INV(dev, prev, sizeof(struct i596_cmd));
  1045. }
  1046. if (lp->cmd_head != NULL)
  1047. ack_cmd |= CUC_START;
  1048. dma->scb.cmd = SWAP32(virt_to_dma(lp, &lp->cmd_head->status));
  1049. DMA_WBACK_INV(dev, &dma->scb, sizeof(struct i596_scb));
  1050. }
  1051. if ((status & 0x1000) || (status & 0x4000)) {
  1052. if ((status & 0x4000))
  1053. DEB(DEB_INTS,
  1054. printk(KERN_DEBUG
  1055. "%s: i596 interrupt received a frame.\n",
  1056. dev->name));
  1057. i596_rx(dev);
  1058. /* Only RX_START if stopped - RGH 07-07-96 */
  1059. if (status & 0x1000) {
  1060. if (netif_running(dev)) {
  1061. DEB(DEB_ERRORS,
  1062. printk(KERN_DEBUG
  1063. "%s: i596 interrupt receive unit inactive, status 0x%x\n",
  1064. dev->name, status));
  1065. ack_cmd |= RX_START;
  1066. dev->stats.rx_errors++;
  1067. dev->stats.rx_fifo_errors++;
  1068. rebuild_rx_bufs(dev);
  1069. }
  1070. }
  1071. }
  1072. wait_cmd(dev, dma, 100, "i596 interrupt, timeout");
  1073. dma->scb.command = SWAP16(ack_cmd);
  1074. DMA_WBACK(dev, &dma->scb, sizeof(struct i596_scb));
  1075. /* DANGER: I suspect that some kind of interrupt
  1076. acknowledgement aside from acking the 82596 might be needed
  1077. here... but it's running acceptably without */
  1078. ca(dev);
  1079. wait_cmd(dev, dma, 100, "i596 interrupt, exit timeout");
  1080. DEB(DEB_INTS, printk(KERN_DEBUG "%s: exiting interrupt.\n", dev->name));
  1081. spin_unlock (&lp->lock);
  1082. return IRQ_HANDLED;
  1083. }
  1084. static int i596_close(struct net_device *dev)
  1085. {
  1086. struct i596_private *lp = netdev_priv(dev);
  1087. unsigned long flags;
  1088. netif_stop_queue(dev);
  1089. DEB(DEB_INIT,
  1090. printk(KERN_DEBUG
  1091. "%s: Shutting down ethercard, status was %4.4x.\n",
  1092. dev->name, SWAP16(lp->dma->scb.status)));
  1093. spin_lock_irqsave(&lp->lock, flags);
  1094. wait_cmd(dev, lp->dma, 100, "close1 timed out");
  1095. lp->dma->scb.command = SWAP16(CUC_ABORT | RX_ABORT);
  1096. DMA_WBACK(dev, &lp->dma->scb, sizeof(struct i596_scb));
  1097. ca(dev);
  1098. wait_cmd(dev, lp->dma, 100, "close2 timed out");
  1099. spin_unlock_irqrestore(&lp->lock, flags);
  1100. DEB(DEB_STRUCT, i596_display_data(dev));
  1101. i596_cleanup_cmd(dev, lp);
  1102. free_irq(dev->irq, dev);
  1103. remove_rx_bufs(dev);
  1104. return 0;
  1105. }
  1106. /*
  1107. * Set or clear the multicast filter for this adaptor.
  1108. */
  1109. static void set_multicast_list(struct net_device *dev)
  1110. {
  1111. struct i596_private *lp = netdev_priv(dev);
  1112. struct i596_dma *dma = lp->dma;
  1113. int config = 0, cnt;
  1114. DEB(DEB_MULTI,
  1115. printk(KERN_DEBUG
  1116. "%s: set multicast list, %d entries, promisc %s, allmulti %s\n",
  1117. dev->name, netdev_mc_count(dev),
  1118. dev->flags & IFF_PROMISC ? "ON" : "OFF",
  1119. dev->flags & IFF_ALLMULTI ? "ON" : "OFF"));
  1120. if ((dev->flags & IFF_PROMISC) &&
  1121. !(dma->cf_cmd.i596_config[8] & 0x01)) {
  1122. dma->cf_cmd.i596_config[8] |= 0x01;
  1123. config = 1;
  1124. }
  1125. if (!(dev->flags & IFF_PROMISC) &&
  1126. (dma->cf_cmd.i596_config[8] & 0x01)) {
  1127. dma->cf_cmd.i596_config[8] &= ~0x01;
  1128. config = 1;
  1129. }
  1130. if ((dev->flags & IFF_ALLMULTI) &&
  1131. (dma->cf_cmd.i596_config[11] & 0x20)) {
  1132. dma->cf_cmd.i596_config[11] &= ~0x20;
  1133. config = 1;
  1134. }
  1135. if (!(dev->flags & IFF_ALLMULTI) &&
  1136. !(dma->cf_cmd.i596_config[11] & 0x20)) {
  1137. dma->cf_cmd.i596_config[11] |= 0x20;
  1138. config = 1;
  1139. }
  1140. if (config) {
  1141. if (dma->cf_cmd.cmd.command)
  1142. printk(KERN_INFO
  1143. "%s: config change request already queued\n",
  1144. dev->name);
  1145. else {
  1146. dma->cf_cmd.cmd.command = SWAP16(CmdConfigure);
  1147. DMA_WBACK_INV(dev, &dma->cf_cmd, sizeof(struct cf_cmd));
  1148. i596_add_cmd(dev, &dma->cf_cmd.cmd);
  1149. }
  1150. }
  1151. cnt = netdev_mc_count(dev);
  1152. if (cnt > MAX_MC_CNT) {
  1153. cnt = MAX_MC_CNT;
  1154. printk(KERN_NOTICE "%s: Only %d multicast addresses supported",
  1155. dev->name, cnt);
  1156. }
  1157. if (!netdev_mc_empty(dev)) {
  1158. struct netdev_hw_addr *ha;
  1159. unsigned char *cp;
  1160. struct mc_cmd *cmd;
  1161. cmd = &dma->mc_cmd;
  1162. cmd->cmd.command = SWAP16(CmdMulticastList);
  1163. cmd->mc_cnt = SWAP16(netdev_mc_count(dev) * 6);
  1164. cp = cmd->mc_addrs;
  1165. netdev_for_each_mc_addr(ha, dev) {
  1166. if (!cnt--)
  1167. break;
  1168. memcpy(cp, ha->addr, ETH_ALEN);
  1169. if (i596_debug > 1)
  1170. DEB(DEB_MULTI,
  1171. printk(KERN_DEBUG
  1172. "%s: Adding address %pM\n",
  1173. dev->name, cp));
  1174. cp += ETH_ALEN;
  1175. }
  1176. DMA_WBACK_INV(dev, &dma->mc_cmd, sizeof(struct mc_cmd));
  1177. i596_add_cmd(dev, &cmd->cmd);
  1178. }
  1179. }