sunlance.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. /* $Id: sunlance.c,v 1.112 2002/01/15 06:48:55 davem Exp $
  2. * lance.c: Linux/Sparc/Lance driver
  3. *
  4. * Written 1995, 1996 by Miguel de Icaza
  5. * Sources:
  6. * The Linux depca driver
  7. * The Linux lance driver.
  8. * The Linux skeleton driver.
  9. * The NetBSD Sparc/Lance driver.
  10. * Theo de Raadt (deraadt@openbsd.org)
  11. * NCR92C990 Lan Controller manual
  12. *
  13. * 1.4:
  14. * Added support to run with a ledma on the Sun4m
  15. *
  16. * 1.5:
  17. * Added multiple card detection.
  18. *
  19. * 4/17/96: Burst sizes and tpe selection on sun4m by Eddie C. Dost
  20. * (ecd@skynet.be)
  21. *
  22. * 5/15/96: auto carrier detection on sun4m by Eddie C. Dost
  23. * (ecd@skynet.be)
  24. *
  25. * 5/17/96: lebuffer on scsi/ether cards now work David S. Miller
  26. * (davem@caip.rutgers.edu)
  27. *
  28. * 5/29/96: override option 'tpe-link-test?', if it is 'false', as
  29. * this disables auto carrier detection on sun4m. Eddie C. Dost
  30. * (ecd@skynet.be)
  31. *
  32. * 1.7:
  33. * 6/26/96: Bug fix for multiple ledmas, miguel.
  34. *
  35. * 1.8:
  36. * Stole multicast code from depca.c, fixed lance_tx.
  37. *
  38. * 1.9:
  39. * 8/21/96: Fixed the multicast code (Pedro Roque)
  40. *
  41. * 8/28/96: Send fake packet in lance_open() if auto_select is true,
  42. * so we can detect the carrier loss condition in time.
  43. * Eddie C. Dost (ecd@skynet.be)
  44. *
  45. * 9/15/96: Align rx_buf so that eth_copy_and_sum() won't cause an
  46. * MNA trap during chksum_partial_copy(). (ecd@skynet.be)
  47. *
  48. * 11/17/96: Handle LE_C0_MERR in lance_interrupt(). (ecd@skynet.be)
  49. *
  50. * 12/22/96: Don't loop forever in lance_rx() on incomplete packets.
  51. * This was the sun4c killer. Shit, stupid bug.
  52. * (ecd@skynet.be)
  53. *
  54. * 1.10:
  55. * 1/26/97: Modularize driver. (ecd@skynet.be)
  56. *
  57. * 1.11:
  58. * 12/27/97: Added sun4d support. (jj@sunsite.mff.cuni.cz)
  59. *
  60. * 1.12:
  61. * 11/3/99: Fixed SMP race in lance_start_xmit found by davem.
  62. * Anton Blanchard (anton@progsoc.uts.edu.au)
  63. * 2.00: 11/9/99: Massive overhaul and port to new SBUS driver interfaces.
  64. * David S. Miller (davem@redhat.com)
  65. * 2.01:
  66. * 11/08/01: Use library crc32 functions (Matt_Domsch@dell.com)
  67. *
  68. */
  69. #undef DEBUG_DRIVER
  70. static char lancestr[] = "LANCE";
  71. #include <linux/module.h>
  72. #include <linux/kernel.h>
  73. #include <linux/types.h>
  74. #include <linux/fcntl.h>
  75. #include <linux/interrupt.h>
  76. #include <linux/ioport.h>
  77. #include <linux/in.h>
  78. #include <linux/slab.h>
  79. #include <linux/string.h>
  80. #include <linux/delay.h>
  81. #include <linux/init.h>
  82. #include <linux/crc32.h>
  83. #include <linux/errno.h>
  84. #include <linux/socket.h> /* Used for the temporal inet entries and routing */
  85. #include <linux/route.h>
  86. #include <linux/netdevice.h>
  87. #include <linux/etherdevice.h>
  88. #include <linux/skbuff.h>
  89. #include <linux/ethtool.h>
  90. #include <linux/bitops.h>
  91. #include <asm/system.h>
  92. #include <asm/io.h>
  93. #include <asm/dma.h>
  94. #include <asm/pgtable.h>
  95. #include <asm/byteorder.h> /* Used by the checksum routines */
  96. #include <asm/idprom.h>
  97. #include <asm/sbus.h>
  98. #include <asm/openprom.h>
  99. #include <asm/oplib.h>
  100. #include <asm/auxio.h> /* For tpe-link-test? setting */
  101. #include <asm/irq.h>
  102. #define DRV_NAME "sunlance"
  103. #define DRV_VERSION "2.02"
  104. #define DRV_RELDATE "8/24/03"
  105. #define DRV_AUTHOR "Miguel de Icaza (miguel@nuclecu.unam.mx)"
  106. static char version[] =
  107. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
  108. MODULE_VERSION(DRV_VERSION);
  109. MODULE_AUTHOR(DRV_AUTHOR);
  110. MODULE_DESCRIPTION("Sun Lance ethernet driver");
  111. MODULE_LICENSE("GPL");
  112. /* Define: 2^4 Tx buffers and 2^4 Rx buffers */
  113. #ifndef LANCE_LOG_TX_BUFFERS
  114. #define LANCE_LOG_TX_BUFFERS 4
  115. #define LANCE_LOG_RX_BUFFERS 4
  116. #endif
  117. #define LE_CSR0 0
  118. #define LE_CSR1 1
  119. #define LE_CSR2 2
  120. #define LE_CSR3 3
  121. #define LE_MO_PROM 0x8000 /* Enable promiscuous mode */
  122. #define LE_C0_ERR 0x8000 /* Error: set if BAB, SQE, MISS or ME is set */
  123. #define LE_C0_BABL 0x4000 /* BAB: Babble: tx timeout. */
  124. #define LE_C0_CERR 0x2000 /* SQE: Signal quality error */
  125. #define LE_C0_MISS 0x1000 /* MISS: Missed a packet */
  126. #define LE_C0_MERR 0x0800 /* ME: Memory error */
  127. #define LE_C0_RINT 0x0400 /* Received interrupt */
  128. #define LE_C0_TINT 0x0200 /* Transmitter Interrupt */
  129. #define LE_C0_IDON 0x0100 /* IFIN: Init finished. */
  130. #define LE_C0_INTR 0x0080 /* Interrupt or error */
  131. #define LE_C0_INEA 0x0040 /* Interrupt enable */
  132. #define LE_C0_RXON 0x0020 /* Receiver on */
  133. #define LE_C0_TXON 0x0010 /* Transmitter on */
  134. #define LE_C0_TDMD 0x0008 /* Transmitter demand */
  135. #define LE_C0_STOP 0x0004 /* Stop the card */
  136. #define LE_C0_STRT 0x0002 /* Start the card */
  137. #define LE_C0_INIT 0x0001 /* Init the card */
  138. #define LE_C3_BSWP 0x4 /* SWAP */
  139. #define LE_C3_ACON 0x2 /* ALE Control */
  140. #define LE_C3_BCON 0x1 /* Byte control */
  141. /* Receive message descriptor 1 */
  142. #define LE_R1_OWN 0x80 /* Who owns the entry */
  143. #define LE_R1_ERR 0x40 /* Error: if FRA, OFL, CRC or BUF is set */
  144. #define LE_R1_FRA 0x20 /* FRA: Frame error */
  145. #define LE_R1_OFL 0x10 /* OFL: Frame overflow */
  146. #define LE_R1_CRC 0x08 /* CRC error */
  147. #define LE_R1_BUF 0x04 /* BUF: Buffer error */
  148. #define LE_R1_SOP 0x02 /* Start of packet */
  149. #define LE_R1_EOP 0x01 /* End of packet */
  150. #define LE_R1_POK 0x03 /* Packet is complete: SOP + EOP */
  151. #define LE_T1_OWN 0x80 /* Lance owns the packet */
  152. #define LE_T1_ERR 0x40 /* Error summary */
  153. #define LE_T1_EMORE 0x10 /* Error: more than one retry needed */
  154. #define LE_T1_EONE 0x08 /* Error: one retry needed */
  155. #define LE_T1_EDEF 0x04 /* Error: deferred */
  156. #define LE_T1_SOP 0x02 /* Start of packet */
  157. #define LE_T1_EOP 0x01 /* End of packet */
  158. #define LE_T1_POK 0x03 /* Packet is complete: SOP + EOP */
  159. #define LE_T3_BUF 0x8000 /* Buffer error */
  160. #define LE_T3_UFL 0x4000 /* Error underflow */
  161. #define LE_T3_LCOL 0x1000 /* Error late collision */
  162. #define LE_T3_CLOS 0x0800 /* Error carrier loss */
  163. #define LE_T3_RTY 0x0400 /* Error retry */
  164. #define LE_T3_TDR 0x03ff /* Time Domain Reflectometry counter */
  165. #define TX_RING_SIZE (1 << (LANCE_LOG_TX_BUFFERS))
  166. #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
  167. #define TX_RING_LEN_BITS ((LANCE_LOG_TX_BUFFERS) << 29)
  168. #define TX_NEXT(__x) (((__x)+1) & TX_RING_MOD_MASK)
  169. #define RX_RING_SIZE (1 << (LANCE_LOG_RX_BUFFERS))
  170. #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
  171. #define RX_RING_LEN_BITS ((LANCE_LOG_RX_BUFFERS) << 29)
  172. #define RX_NEXT(__x) (((__x)+1) & RX_RING_MOD_MASK)
  173. #define PKT_BUF_SZ 1544
  174. #define RX_BUFF_SIZE PKT_BUF_SZ
  175. #define TX_BUFF_SIZE PKT_BUF_SZ
  176. struct lance_rx_desc {
  177. u16 rmd0; /* low address of packet */
  178. u8 rmd1_bits; /* descriptor bits */
  179. u8 rmd1_hadr; /* high address of packet */
  180. s16 length; /* This length is 2s complement (negative)!
  181. * Buffer length
  182. */
  183. u16 mblength; /* This is the actual number of bytes received */
  184. };
  185. struct lance_tx_desc {
  186. u16 tmd0; /* low address of packet */
  187. u8 tmd1_bits; /* descriptor bits */
  188. u8 tmd1_hadr; /* high address of packet */
  189. s16 length; /* Length is 2s complement (negative)! */
  190. u16 misc;
  191. };
  192. /* The LANCE initialization block, described in databook. */
  193. /* On the Sparc, this block should be on a DMA region */
  194. struct lance_init_block {
  195. u16 mode; /* Pre-set mode (reg. 15) */
  196. u8 phys_addr[6]; /* Physical ethernet address */
  197. u32 filter[2]; /* Multicast filter. */
  198. /* Receive and transmit ring base, along with extra bits. */
  199. u16 rx_ptr; /* receive descriptor addr */
  200. u16 rx_len; /* receive len and high addr */
  201. u16 tx_ptr; /* transmit descriptor addr */
  202. u16 tx_len; /* transmit len and high addr */
  203. /* The Tx and Rx ring entries must aligned on 8-byte boundaries. */
  204. struct lance_rx_desc brx_ring[RX_RING_SIZE];
  205. struct lance_tx_desc btx_ring[TX_RING_SIZE];
  206. u8 tx_buf [TX_RING_SIZE][TX_BUFF_SIZE];
  207. u8 pad[2]; /* align rx_buf for copy_and_sum(). */
  208. u8 rx_buf [RX_RING_SIZE][RX_BUFF_SIZE];
  209. };
  210. #define libdesc_offset(rt, elem) \
  211. ((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem])))))
  212. #define libbuff_offset(rt, elem) \
  213. ((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem][0])))))
  214. struct lance_private {
  215. void __iomem *lregs; /* Lance RAP/RDP regs. */
  216. void __iomem *dregs; /* DMA controller regs. */
  217. struct lance_init_block __iomem *init_block_iomem;
  218. struct lance_init_block *init_block_mem;
  219. spinlock_t lock;
  220. int rx_new, tx_new;
  221. int rx_old, tx_old;
  222. struct net_device_stats stats;
  223. struct sbus_dma *ledma; /* If set this points to ledma */
  224. char tpe; /* cable-selection is TPE */
  225. char auto_select; /* cable-selection by carrier */
  226. char burst_sizes; /* ledma SBus burst sizes */
  227. char pio_buffer; /* init block in PIO space? */
  228. unsigned short busmaster_regval;
  229. void (*init_ring)(struct net_device *);
  230. void (*rx)(struct net_device *);
  231. void (*tx)(struct net_device *);
  232. char *name;
  233. dma_addr_t init_block_dvma;
  234. struct net_device *dev; /* Backpointer */
  235. struct sbus_dev *sdev;
  236. struct timer_list multicast_timer;
  237. };
  238. #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
  239. lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:\
  240. lp->tx_old - lp->tx_new-1)
  241. /* Lance registers. */
  242. #define RDP 0x00UL /* register data port */
  243. #define RAP 0x02UL /* register address port */
  244. #define LANCE_REG_SIZE 0x04UL
  245. #define STOP_LANCE(__lp) \
  246. do { void __iomem *__base = (__lp)->lregs; \
  247. sbus_writew(LE_CSR0, __base + RAP); \
  248. sbus_writew(LE_C0_STOP, __base + RDP); \
  249. } while (0)
  250. int sparc_lance_debug = 2;
  251. /* The Lance uses 24 bit addresses */
  252. /* On the Sun4c the DVMA will provide the remaining bytes for us */
  253. /* On the Sun4m we have to instruct the ledma to provide them */
  254. /* Even worse, on scsi/ether SBUS cards, the init block and the
  255. * transmit/receive buffers are addresses as offsets from absolute
  256. * zero on the lebuffer PIO area. -DaveM
  257. */
  258. #define LANCE_ADDR(x) ((long)(x) & ~0xff000000)
  259. /* Load the CSR registers */
  260. static void load_csrs(struct lance_private *lp)
  261. {
  262. u32 leptr;
  263. if (lp->pio_buffer)
  264. leptr = 0;
  265. else
  266. leptr = LANCE_ADDR(lp->init_block_dvma);
  267. sbus_writew(LE_CSR1, lp->lregs + RAP);
  268. sbus_writew(leptr & 0xffff, lp->lregs + RDP);
  269. sbus_writew(LE_CSR2, lp->lregs + RAP);
  270. sbus_writew(leptr >> 16, lp->lregs + RDP);
  271. sbus_writew(LE_CSR3, lp->lregs + RAP);
  272. sbus_writew(lp->busmaster_regval, lp->lregs + RDP);
  273. /* Point back to csr0 */
  274. sbus_writew(LE_CSR0, lp->lregs + RAP);
  275. }
  276. /* Setup the Lance Rx and Tx rings */
  277. static void lance_init_ring_dvma(struct net_device *dev)
  278. {
  279. struct lance_private *lp = netdev_priv(dev);
  280. struct lance_init_block *ib = lp->init_block_mem;
  281. dma_addr_t aib = lp->init_block_dvma;
  282. __u32 leptr;
  283. int i;
  284. /* Lock out other processes while setting up hardware */
  285. netif_stop_queue(dev);
  286. lp->rx_new = lp->tx_new = 0;
  287. lp->rx_old = lp->tx_old = 0;
  288. /* Copy the ethernet address to the lance init block
  289. * Note that on the sparc you need to swap the ethernet address.
  290. */
  291. ib->phys_addr [0] = dev->dev_addr [1];
  292. ib->phys_addr [1] = dev->dev_addr [0];
  293. ib->phys_addr [2] = dev->dev_addr [3];
  294. ib->phys_addr [3] = dev->dev_addr [2];
  295. ib->phys_addr [4] = dev->dev_addr [5];
  296. ib->phys_addr [5] = dev->dev_addr [4];
  297. /* Setup the Tx ring entries */
  298. for (i = 0; i <= TX_RING_SIZE; i++) {
  299. leptr = LANCE_ADDR(aib + libbuff_offset(tx_buf, i));
  300. ib->btx_ring [i].tmd0 = leptr;
  301. ib->btx_ring [i].tmd1_hadr = leptr >> 16;
  302. ib->btx_ring [i].tmd1_bits = 0;
  303. ib->btx_ring [i].length = 0xf000; /* The ones required by tmd2 */
  304. ib->btx_ring [i].misc = 0;
  305. }
  306. /* Setup the Rx ring entries */
  307. for (i = 0; i < RX_RING_SIZE; i++) {
  308. leptr = LANCE_ADDR(aib + libbuff_offset(rx_buf, i));
  309. ib->brx_ring [i].rmd0 = leptr;
  310. ib->brx_ring [i].rmd1_hadr = leptr >> 16;
  311. ib->brx_ring [i].rmd1_bits = LE_R1_OWN;
  312. ib->brx_ring [i].length = -RX_BUFF_SIZE | 0xf000;
  313. ib->brx_ring [i].mblength = 0;
  314. }
  315. /* Setup the initialization block */
  316. /* Setup rx descriptor pointer */
  317. leptr = LANCE_ADDR(aib + libdesc_offset(brx_ring, 0));
  318. ib->rx_len = (LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16);
  319. ib->rx_ptr = leptr;
  320. /* Setup tx descriptor pointer */
  321. leptr = LANCE_ADDR(aib + libdesc_offset(btx_ring, 0));
  322. ib->tx_len = (LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16);
  323. ib->tx_ptr = leptr;
  324. }
  325. static void lance_init_ring_pio(struct net_device *dev)
  326. {
  327. struct lance_private *lp = netdev_priv(dev);
  328. struct lance_init_block __iomem *ib = lp->init_block_iomem;
  329. u32 leptr;
  330. int i;
  331. /* Lock out other processes while setting up hardware */
  332. netif_stop_queue(dev);
  333. lp->rx_new = lp->tx_new = 0;
  334. lp->rx_old = lp->tx_old = 0;
  335. /* Copy the ethernet address to the lance init block
  336. * Note that on the sparc you need to swap the ethernet address.
  337. */
  338. sbus_writeb(dev->dev_addr[1], &ib->phys_addr[0]);
  339. sbus_writeb(dev->dev_addr[0], &ib->phys_addr[1]);
  340. sbus_writeb(dev->dev_addr[3], &ib->phys_addr[2]);
  341. sbus_writeb(dev->dev_addr[2], &ib->phys_addr[3]);
  342. sbus_writeb(dev->dev_addr[5], &ib->phys_addr[4]);
  343. sbus_writeb(dev->dev_addr[4], &ib->phys_addr[5]);
  344. /* Setup the Tx ring entries */
  345. for (i = 0; i <= TX_RING_SIZE; i++) {
  346. leptr = libbuff_offset(tx_buf, i);
  347. sbus_writew(leptr, &ib->btx_ring [i].tmd0);
  348. sbus_writeb(leptr >> 16,&ib->btx_ring [i].tmd1_hadr);
  349. sbus_writeb(0, &ib->btx_ring [i].tmd1_bits);
  350. /* The ones required by tmd2 */
  351. sbus_writew(0xf000, &ib->btx_ring [i].length);
  352. sbus_writew(0, &ib->btx_ring [i].misc);
  353. }
  354. /* Setup the Rx ring entries */
  355. for (i = 0; i < RX_RING_SIZE; i++) {
  356. leptr = libbuff_offset(rx_buf, i);
  357. sbus_writew(leptr, &ib->brx_ring [i].rmd0);
  358. sbus_writeb(leptr >> 16,&ib->brx_ring [i].rmd1_hadr);
  359. sbus_writeb(LE_R1_OWN, &ib->brx_ring [i].rmd1_bits);
  360. sbus_writew(-RX_BUFF_SIZE|0xf000,
  361. &ib->brx_ring [i].length);
  362. sbus_writew(0, &ib->brx_ring [i].mblength);
  363. }
  364. /* Setup the initialization block */
  365. /* Setup rx descriptor pointer */
  366. leptr = libdesc_offset(brx_ring, 0);
  367. sbus_writew((LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16),
  368. &ib->rx_len);
  369. sbus_writew(leptr, &ib->rx_ptr);
  370. /* Setup tx descriptor pointer */
  371. leptr = libdesc_offset(btx_ring, 0);
  372. sbus_writew((LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16),
  373. &ib->tx_len);
  374. sbus_writew(leptr, &ib->tx_ptr);
  375. }
  376. static void init_restart_ledma(struct lance_private *lp)
  377. {
  378. u32 csr = sbus_readl(lp->dregs + DMA_CSR);
  379. if (!(csr & DMA_HNDL_ERROR)) {
  380. /* E-Cache draining */
  381. while (sbus_readl(lp->dregs + DMA_CSR) & DMA_FIFO_ISDRAIN)
  382. barrier();
  383. }
  384. csr = sbus_readl(lp->dregs + DMA_CSR);
  385. csr &= ~DMA_E_BURSTS;
  386. if (lp->burst_sizes & DMA_BURST32)
  387. csr |= DMA_E_BURST32;
  388. else
  389. csr |= DMA_E_BURST16;
  390. csr |= (DMA_DSBL_RD_DRN | DMA_DSBL_WR_INV | DMA_FIFO_INV);
  391. if (lp->tpe)
  392. csr |= DMA_EN_ENETAUI;
  393. else
  394. csr &= ~DMA_EN_ENETAUI;
  395. udelay(20);
  396. sbus_writel(csr, lp->dregs + DMA_CSR);
  397. udelay(200);
  398. }
  399. static int init_restart_lance(struct lance_private *lp)
  400. {
  401. u16 regval = 0;
  402. int i;
  403. if (lp->dregs)
  404. init_restart_ledma(lp);
  405. sbus_writew(LE_CSR0, lp->lregs + RAP);
  406. sbus_writew(LE_C0_INIT, lp->lregs + RDP);
  407. /* Wait for the lance to complete initialization */
  408. for (i = 0; i < 100; i++) {
  409. regval = sbus_readw(lp->lregs + RDP);
  410. if (regval & (LE_C0_ERR | LE_C0_IDON))
  411. break;
  412. barrier();
  413. }
  414. if (i == 100 || (regval & LE_C0_ERR)) {
  415. printk(KERN_ERR "LANCE unopened after %d ticks, csr0=%4.4x.\n",
  416. i, regval);
  417. if (lp->dregs)
  418. printk("dcsr=%8.8x\n", sbus_readl(lp->dregs + DMA_CSR));
  419. return -1;
  420. }
  421. /* Clear IDON by writing a "1", enable interrupts and start lance */
  422. sbus_writew(LE_C0_IDON, lp->lregs + RDP);
  423. sbus_writew(LE_C0_INEA | LE_C0_STRT, lp->lregs + RDP);
  424. if (lp->dregs) {
  425. u32 csr = sbus_readl(lp->dregs + DMA_CSR);
  426. csr |= DMA_INT_ENAB;
  427. sbus_writel(csr, lp->dregs + DMA_CSR);
  428. }
  429. return 0;
  430. }
  431. static void lance_rx_dvma(struct net_device *dev)
  432. {
  433. struct lance_private *lp = netdev_priv(dev);
  434. struct lance_init_block *ib = lp->init_block_mem;
  435. struct lance_rx_desc *rd;
  436. u8 bits;
  437. int len, entry = lp->rx_new;
  438. struct sk_buff *skb;
  439. for (rd = &ib->brx_ring [entry];
  440. !((bits = rd->rmd1_bits) & LE_R1_OWN);
  441. rd = &ib->brx_ring [entry]) {
  442. /* We got an incomplete frame? */
  443. if ((bits & LE_R1_POK) != LE_R1_POK) {
  444. lp->stats.rx_over_errors++;
  445. lp->stats.rx_errors++;
  446. } else if (bits & LE_R1_ERR) {
  447. /* Count only the end frame as a rx error,
  448. * not the beginning
  449. */
  450. if (bits & LE_R1_BUF) lp->stats.rx_fifo_errors++;
  451. if (bits & LE_R1_CRC) lp->stats.rx_crc_errors++;
  452. if (bits & LE_R1_OFL) lp->stats.rx_over_errors++;
  453. if (bits & LE_R1_FRA) lp->stats.rx_frame_errors++;
  454. if (bits & LE_R1_EOP) lp->stats.rx_errors++;
  455. } else {
  456. len = (rd->mblength & 0xfff) - 4;
  457. skb = dev_alloc_skb(len + 2);
  458. if (skb == NULL) {
  459. printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n",
  460. dev->name);
  461. lp->stats.rx_dropped++;
  462. rd->mblength = 0;
  463. rd->rmd1_bits = LE_R1_OWN;
  464. lp->rx_new = RX_NEXT(entry);
  465. return;
  466. }
  467. lp->stats.rx_bytes += len;
  468. skb_reserve(skb, 2); /* 16 byte align */
  469. skb_put(skb, len); /* make room */
  470. eth_copy_and_sum(skb,
  471. (unsigned char *)&(ib->rx_buf [entry][0]),
  472. len, 0);
  473. skb->protocol = eth_type_trans(skb, dev);
  474. netif_rx(skb);
  475. dev->last_rx = jiffies;
  476. lp->stats.rx_packets++;
  477. }
  478. /* Return the packet to the pool */
  479. rd->mblength = 0;
  480. rd->rmd1_bits = LE_R1_OWN;
  481. entry = RX_NEXT(entry);
  482. }
  483. lp->rx_new = entry;
  484. }
  485. static void lance_tx_dvma(struct net_device *dev)
  486. {
  487. struct lance_private *lp = netdev_priv(dev);
  488. struct lance_init_block *ib = lp->init_block_mem;
  489. int i, j;
  490. spin_lock(&lp->lock);
  491. j = lp->tx_old;
  492. for (i = j; i != lp->tx_new; i = j) {
  493. struct lance_tx_desc *td = &ib->btx_ring [i];
  494. u8 bits = td->tmd1_bits;
  495. /* If we hit a packet not owned by us, stop */
  496. if (bits & LE_T1_OWN)
  497. break;
  498. if (bits & LE_T1_ERR) {
  499. u16 status = td->misc;
  500. lp->stats.tx_errors++;
  501. if (status & LE_T3_RTY) lp->stats.tx_aborted_errors++;
  502. if (status & LE_T3_LCOL) lp->stats.tx_window_errors++;
  503. if (status & LE_T3_CLOS) {
  504. lp->stats.tx_carrier_errors++;
  505. if (lp->auto_select) {
  506. lp->tpe = 1 - lp->tpe;
  507. printk(KERN_NOTICE "%s: Carrier Lost, trying %s\n",
  508. dev->name, lp->tpe?"TPE":"AUI");
  509. STOP_LANCE(lp);
  510. lp->init_ring(dev);
  511. load_csrs(lp);
  512. init_restart_lance(lp);
  513. goto out;
  514. }
  515. }
  516. /* Buffer errors and underflows turn off the
  517. * transmitter, restart the adapter.
  518. */
  519. if (status & (LE_T3_BUF|LE_T3_UFL)) {
  520. lp->stats.tx_fifo_errors++;
  521. printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
  522. dev->name);
  523. STOP_LANCE(lp);
  524. lp->init_ring(dev);
  525. load_csrs(lp);
  526. init_restart_lance(lp);
  527. goto out;
  528. }
  529. } else if ((bits & LE_T1_POK) == LE_T1_POK) {
  530. /*
  531. * So we don't count the packet more than once.
  532. */
  533. td->tmd1_bits = bits & ~(LE_T1_POK);
  534. /* One collision before packet was sent. */
  535. if (bits & LE_T1_EONE)
  536. lp->stats.collisions++;
  537. /* More than one collision, be optimistic. */
  538. if (bits & LE_T1_EMORE)
  539. lp->stats.collisions += 2;
  540. lp->stats.tx_packets++;
  541. }
  542. j = TX_NEXT(j);
  543. }
  544. lp->tx_old = j;
  545. out:
  546. if (netif_queue_stopped(dev) &&
  547. TX_BUFFS_AVAIL > 0)
  548. netif_wake_queue(dev);
  549. spin_unlock(&lp->lock);
  550. }
  551. static void lance_piocopy_to_skb(struct sk_buff *skb, void __iomem *piobuf, int len)
  552. {
  553. u16 *p16 = (u16 *) skb->data;
  554. u32 *p32;
  555. u8 *p8;
  556. void __iomem *pbuf = piobuf;
  557. /* We know here that both src and dest are on a 16bit boundary. */
  558. *p16++ = sbus_readw(pbuf);
  559. p32 = (u32 *) p16;
  560. pbuf += 2;
  561. len -= 2;
  562. while (len >= 4) {
  563. *p32++ = sbus_readl(pbuf);
  564. pbuf += 4;
  565. len -= 4;
  566. }
  567. p8 = (u8 *) p32;
  568. if (len >= 2) {
  569. p16 = (u16 *) p32;
  570. *p16++ = sbus_readw(pbuf);
  571. pbuf += 2;
  572. len -= 2;
  573. p8 = (u8 *) p16;
  574. }
  575. if (len >= 1)
  576. *p8 = sbus_readb(pbuf);
  577. }
  578. static void lance_rx_pio(struct net_device *dev)
  579. {
  580. struct lance_private *lp = netdev_priv(dev);
  581. struct lance_init_block __iomem *ib = lp->init_block_iomem;
  582. struct lance_rx_desc __iomem *rd;
  583. unsigned char bits;
  584. int len, entry;
  585. struct sk_buff *skb;
  586. entry = lp->rx_new;
  587. for (rd = &ib->brx_ring [entry];
  588. !((bits = sbus_readb(&rd->rmd1_bits)) & LE_R1_OWN);
  589. rd = &ib->brx_ring [entry]) {
  590. /* We got an incomplete frame? */
  591. if ((bits & LE_R1_POK) != LE_R1_POK) {
  592. lp->stats.rx_over_errors++;
  593. lp->stats.rx_errors++;
  594. } else if (bits & LE_R1_ERR) {
  595. /* Count only the end frame as a rx error,
  596. * not the beginning
  597. */
  598. if (bits & LE_R1_BUF) lp->stats.rx_fifo_errors++;
  599. if (bits & LE_R1_CRC) lp->stats.rx_crc_errors++;
  600. if (bits & LE_R1_OFL) lp->stats.rx_over_errors++;
  601. if (bits & LE_R1_FRA) lp->stats.rx_frame_errors++;
  602. if (bits & LE_R1_EOP) lp->stats.rx_errors++;
  603. } else {
  604. len = (sbus_readw(&rd->mblength) & 0xfff) - 4;
  605. skb = dev_alloc_skb(len + 2);
  606. if (skb == NULL) {
  607. printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n",
  608. dev->name);
  609. lp->stats.rx_dropped++;
  610. sbus_writew(0, &rd->mblength);
  611. sbus_writeb(LE_R1_OWN, &rd->rmd1_bits);
  612. lp->rx_new = RX_NEXT(entry);
  613. return;
  614. }
  615. lp->stats.rx_bytes += len;
  616. skb_reserve (skb, 2); /* 16 byte align */
  617. skb_put(skb, len); /* make room */
  618. lance_piocopy_to_skb(skb, &(ib->rx_buf[entry][0]), len);
  619. skb->protocol = eth_type_trans(skb, dev);
  620. netif_rx(skb);
  621. dev->last_rx = jiffies;
  622. lp->stats.rx_packets++;
  623. }
  624. /* Return the packet to the pool */
  625. sbus_writew(0, &rd->mblength);
  626. sbus_writeb(LE_R1_OWN, &rd->rmd1_bits);
  627. entry = RX_NEXT(entry);
  628. }
  629. lp->rx_new = entry;
  630. }
  631. static void lance_tx_pio(struct net_device *dev)
  632. {
  633. struct lance_private *lp = netdev_priv(dev);
  634. struct lance_init_block __iomem *ib = lp->init_block_iomem;
  635. int i, j;
  636. spin_lock(&lp->lock);
  637. j = lp->tx_old;
  638. for (i = j; i != lp->tx_new; i = j) {
  639. struct lance_tx_desc __iomem *td = &ib->btx_ring [i];
  640. u8 bits = sbus_readb(&td->tmd1_bits);
  641. /* If we hit a packet not owned by us, stop */
  642. if (bits & LE_T1_OWN)
  643. break;
  644. if (bits & LE_T1_ERR) {
  645. u16 status = sbus_readw(&td->misc);
  646. lp->stats.tx_errors++;
  647. if (status & LE_T3_RTY) lp->stats.tx_aborted_errors++;
  648. if (status & LE_T3_LCOL) lp->stats.tx_window_errors++;
  649. if (status & LE_T3_CLOS) {
  650. lp->stats.tx_carrier_errors++;
  651. if (lp->auto_select) {
  652. lp->tpe = 1 - lp->tpe;
  653. printk(KERN_NOTICE "%s: Carrier Lost, trying %s\n",
  654. dev->name, lp->tpe?"TPE":"AUI");
  655. STOP_LANCE(lp);
  656. lp->init_ring(dev);
  657. load_csrs(lp);
  658. init_restart_lance(lp);
  659. goto out;
  660. }
  661. }
  662. /* Buffer errors and underflows turn off the
  663. * transmitter, restart the adapter.
  664. */
  665. if (status & (LE_T3_BUF|LE_T3_UFL)) {
  666. lp->stats.tx_fifo_errors++;
  667. printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
  668. dev->name);
  669. STOP_LANCE(lp);
  670. lp->init_ring(dev);
  671. load_csrs(lp);
  672. init_restart_lance(lp);
  673. goto out;
  674. }
  675. } else if ((bits & LE_T1_POK) == LE_T1_POK) {
  676. /*
  677. * So we don't count the packet more than once.
  678. */
  679. sbus_writeb(bits & ~(LE_T1_POK), &td->tmd1_bits);
  680. /* One collision before packet was sent. */
  681. if (bits & LE_T1_EONE)
  682. lp->stats.collisions++;
  683. /* More than one collision, be optimistic. */
  684. if (bits & LE_T1_EMORE)
  685. lp->stats.collisions += 2;
  686. lp->stats.tx_packets++;
  687. }
  688. j = TX_NEXT(j);
  689. }
  690. lp->tx_old = j;
  691. if (netif_queue_stopped(dev) &&
  692. TX_BUFFS_AVAIL > 0)
  693. netif_wake_queue(dev);
  694. out:
  695. spin_unlock(&lp->lock);
  696. }
  697. static irqreturn_t lance_interrupt(int irq, void *dev_id)
  698. {
  699. struct net_device *dev = dev_id;
  700. struct lance_private *lp = netdev_priv(dev);
  701. int csr0;
  702. sbus_writew(LE_CSR0, lp->lregs + RAP);
  703. csr0 = sbus_readw(lp->lregs + RDP);
  704. /* Acknowledge all the interrupt sources ASAP */
  705. sbus_writew(csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT),
  706. lp->lregs + RDP);
  707. if ((csr0 & LE_C0_ERR) != 0) {
  708. /* Clear the error condition */
  709. sbus_writew((LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
  710. LE_C0_CERR | LE_C0_MERR),
  711. lp->lregs + RDP);
  712. }
  713. if (csr0 & LE_C0_RINT)
  714. lp->rx(dev);
  715. if (csr0 & LE_C0_TINT)
  716. lp->tx(dev);
  717. if (csr0 & LE_C0_BABL)
  718. lp->stats.tx_errors++;
  719. if (csr0 & LE_C0_MISS)
  720. lp->stats.rx_errors++;
  721. if (csr0 & LE_C0_MERR) {
  722. if (lp->dregs) {
  723. u32 addr = sbus_readl(lp->dregs + DMA_ADDR);
  724. printk(KERN_ERR "%s: Memory error, status %04x, addr %06x\n",
  725. dev->name, csr0, addr & 0xffffff);
  726. } else {
  727. printk(KERN_ERR "%s: Memory error, status %04x\n",
  728. dev->name, csr0);
  729. }
  730. sbus_writew(LE_C0_STOP, lp->lregs + RDP);
  731. if (lp->dregs) {
  732. u32 dma_csr = sbus_readl(lp->dregs + DMA_CSR);
  733. dma_csr |= DMA_FIFO_INV;
  734. sbus_writel(dma_csr, lp->dregs + DMA_CSR);
  735. }
  736. lp->init_ring(dev);
  737. load_csrs(lp);
  738. init_restart_lance(lp);
  739. netif_wake_queue(dev);
  740. }
  741. sbus_writew(LE_C0_INEA, lp->lregs + RDP);
  742. return IRQ_HANDLED;
  743. }
  744. /* Build a fake network packet and send it to ourselves. */
  745. static void build_fake_packet(struct lance_private *lp)
  746. {
  747. struct net_device *dev = lp->dev;
  748. int i, entry;
  749. entry = lp->tx_new & TX_RING_MOD_MASK;
  750. if (lp->pio_buffer) {
  751. struct lance_init_block __iomem *ib = lp->init_block_iomem;
  752. u16 __iomem *packet = (u16 __iomem *) &(ib->tx_buf[entry][0]);
  753. struct ethhdr __iomem *eth = (struct ethhdr __iomem *) packet;
  754. for (i = 0; i < (ETH_ZLEN / sizeof(u16)); i++)
  755. sbus_writew(0, &packet[i]);
  756. for (i = 0; i < 6; i++) {
  757. sbus_writeb(dev->dev_addr[i], &eth->h_dest[i]);
  758. sbus_writeb(dev->dev_addr[i], &eth->h_source[i]);
  759. }
  760. sbus_writew((-ETH_ZLEN) | 0xf000, &ib->btx_ring[entry].length);
  761. sbus_writew(0, &ib->btx_ring[entry].misc);
  762. sbus_writeb(LE_T1_POK|LE_T1_OWN, &ib->btx_ring[entry].tmd1_bits);
  763. } else {
  764. struct lance_init_block *ib = lp->init_block_mem;
  765. u16 *packet = (u16 *) &(ib->tx_buf[entry][0]);
  766. struct ethhdr *eth = (struct ethhdr *) packet;
  767. memset(packet, 0, ETH_ZLEN);
  768. for (i = 0; i < 6; i++) {
  769. eth->h_dest[i] = dev->dev_addr[i];
  770. eth->h_source[i] = dev->dev_addr[i];
  771. }
  772. ib->btx_ring[entry].length = (-ETH_ZLEN) | 0xf000;
  773. ib->btx_ring[entry].misc = 0;
  774. ib->btx_ring[entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN);
  775. }
  776. lp->tx_new = TX_NEXT(entry);
  777. }
  778. struct net_device *last_dev;
  779. static int lance_open(struct net_device *dev)
  780. {
  781. struct lance_private *lp = netdev_priv(dev);
  782. int status = 0;
  783. last_dev = dev;
  784. STOP_LANCE(lp);
  785. if (request_irq(dev->irq, &lance_interrupt, IRQF_SHARED,
  786. lancestr, (void *) dev)) {
  787. printk(KERN_ERR "Lance: Can't get irq %d\n", dev->irq);
  788. return -EAGAIN;
  789. }
  790. /* On the 4m, setup the ledma to provide the upper bits for buffers */
  791. if (lp->dregs) {
  792. u32 regval = lp->init_block_dvma & 0xff000000;
  793. sbus_writel(regval, lp->dregs + DMA_TEST);
  794. }
  795. /* Set mode and clear multicast filter only at device open,
  796. * so that lance_init_ring() called at any error will not
  797. * forget multicast filters.
  798. *
  799. * BTW it is common bug in all lance drivers! --ANK
  800. */
  801. if (lp->pio_buffer) {
  802. struct lance_init_block __iomem *ib = lp->init_block_iomem;
  803. sbus_writew(0, &ib->mode);
  804. sbus_writel(0, &ib->filter[0]);
  805. sbus_writel(0, &ib->filter[1]);
  806. } else {
  807. struct lance_init_block *ib = lp->init_block_mem;
  808. ib->mode = 0;
  809. ib->filter [0] = 0;
  810. ib->filter [1] = 0;
  811. }
  812. lp->init_ring(dev);
  813. load_csrs(lp);
  814. netif_start_queue(dev);
  815. status = init_restart_lance(lp);
  816. if (!status && lp->auto_select) {
  817. build_fake_packet(lp);
  818. sbus_writew(LE_C0_INEA | LE_C0_TDMD, lp->lregs + RDP);
  819. }
  820. return status;
  821. }
  822. static int lance_close(struct net_device *dev)
  823. {
  824. struct lance_private *lp = netdev_priv(dev);
  825. netif_stop_queue(dev);
  826. del_timer_sync(&lp->multicast_timer);
  827. STOP_LANCE(lp);
  828. free_irq(dev->irq, (void *) dev);
  829. return 0;
  830. }
  831. static int lance_reset(struct net_device *dev)
  832. {
  833. struct lance_private *lp = netdev_priv(dev);
  834. int status;
  835. STOP_LANCE(lp);
  836. /* On the 4m, reset the dma too */
  837. if (lp->dregs) {
  838. u32 csr, addr;
  839. printk(KERN_ERR "resetting ledma\n");
  840. csr = sbus_readl(lp->dregs + DMA_CSR);
  841. sbus_writel(csr | DMA_RST_ENET, lp->dregs + DMA_CSR);
  842. udelay(200);
  843. sbus_writel(csr & ~DMA_RST_ENET, lp->dregs + DMA_CSR);
  844. addr = lp->init_block_dvma & 0xff000000;
  845. sbus_writel(addr, lp->dregs + DMA_TEST);
  846. }
  847. lp->init_ring(dev);
  848. load_csrs(lp);
  849. dev->trans_start = jiffies;
  850. status = init_restart_lance(lp);
  851. return status;
  852. }
  853. static void lance_piocopy_from_skb(void __iomem *dest, unsigned char *src, int len)
  854. {
  855. void __iomem *piobuf = dest;
  856. u32 *p32;
  857. u16 *p16;
  858. u8 *p8;
  859. switch ((unsigned long)src & 0x3) {
  860. case 0:
  861. p32 = (u32 *) src;
  862. while (len >= 4) {
  863. sbus_writel(*p32, piobuf);
  864. p32++;
  865. piobuf += 4;
  866. len -= 4;
  867. }
  868. src = (char *) p32;
  869. break;
  870. case 1:
  871. case 3:
  872. p8 = (u8 *) src;
  873. while (len >= 4) {
  874. u32 val;
  875. val = p8[0] << 24;
  876. val |= p8[1] << 16;
  877. val |= p8[2] << 8;
  878. val |= p8[3];
  879. sbus_writel(val, piobuf);
  880. p8 += 4;
  881. piobuf += 4;
  882. len -= 4;
  883. }
  884. src = (char *) p8;
  885. break;
  886. case 2:
  887. p16 = (u16 *) src;
  888. while (len >= 4) {
  889. u32 val = p16[0]<<16 | p16[1];
  890. sbus_writel(val, piobuf);
  891. p16 += 2;
  892. piobuf += 4;
  893. len -= 4;
  894. }
  895. src = (char *) p16;
  896. break;
  897. };
  898. if (len >= 2) {
  899. u16 val = src[0] << 8 | src[1];
  900. sbus_writew(val, piobuf);
  901. src += 2;
  902. piobuf += 2;
  903. len -= 2;
  904. }
  905. if (len >= 1)
  906. sbus_writeb(src[0], piobuf);
  907. }
  908. static void lance_piozero(void __iomem *dest, int len)
  909. {
  910. void __iomem *piobuf = dest;
  911. if ((unsigned long)piobuf & 1) {
  912. sbus_writeb(0, piobuf);
  913. piobuf += 1;
  914. len -= 1;
  915. if (len == 0)
  916. return;
  917. }
  918. if (len == 1) {
  919. sbus_writeb(0, piobuf);
  920. return;
  921. }
  922. if ((unsigned long)piobuf & 2) {
  923. sbus_writew(0, piobuf);
  924. piobuf += 2;
  925. len -= 2;
  926. if (len == 0)
  927. return;
  928. }
  929. while (len >= 4) {
  930. sbus_writel(0, piobuf);
  931. piobuf += 4;
  932. len -= 4;
  933. }
  934. if (len >= 2) {
  935. sbus_writew(0, piobuf);
  936. piobuf += 2;
  937. len -= 2;
  938. }
  939. if (len >= 1)
  940. sbus_writeb(0, piobuf);
  941. }
  942. static void lance_tx_timeout(struct net_device *dev)
  943. {
  944. struct lance_private *lp = netdev_priv(dev);
  945. printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n",
  946. dev->name, sbus_readw(lp->lregs + RDP));
  947. lance_reset(dev);
  948. netif_wake_queue(dev);
  949. }
  950. static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
  951. {
  952. struct lance_private *lp = netdev_priv(dev);
  953. int entry, skblen, len;
  954. skblen = skb->len;
  955. len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
  956. spin_lock_irq(&lp->lock);
  957. lp->stats.tx_bytes += len;
  958. entry = lp->tx_new & TX_RING_MOD_MASK;
  959. if (lp->pio_buffer) {
  960. struct lance_init_block __iomem *ib = lp->init_block_iomem;
  961. sbus_writew((-len) | 0xf000, &ib->btx_ring[entry].length);
  962. sbus_writew(0, &ib->btx_ring[entry].misc);
  963. lance_piocopy_from_skb(&ib->tx_buf[entry][0], skb->data, skblen);
  964. if (len != skblen)
  965. lance_piozero(&ib->tx_buf[entry][skblen], len - skblen);
  966. sbus_writeb(LE_T1_POK | LE_T1_OWN, &ib->btx_ring[entry].tmd1_bits);
  967. } else {
  968. struct lance_init_block *ib = lp->init_block_mem;
  969. ib->btx_ring [entry].length = (-len) | 0xf000;
  970. ib->btx_ring [entry].misc = 0;
  971. skb_copy_from_linear_data(skb, &ib->tx_buf [entry][0], skblen);
  972. if (len != skblen)
  973. memset((char *) &ib->tx_buf [entry][skblen], 0, len - skblen);
  974. ib->btx_ring [entry].tmd1_bits = (LE_T1_POK | LE_T1_OWN);
  975. }
  976. lp->tx_new = TX_NEXT(entry);
  977. if (TX_BUFFS_AVAIL <= 0)
  978. netif_stop_queue(dev);
  979. /* Kick the lance: transmit now */
  980. sbus_writew(LE_C0_INEA | LE_C0_TDMD, lp->lregs + RDP);
  981. /* Read back CSR to invalidate the E-Cache.
  982. * This is needed, because DMA_DSBL_WR_INV is set.
  983. */
  984. if (lp->dregs)
  985. sbus_readw(lp->lregs + RDP);
  986. spin_unlock_irq(&lp->lock);
  987. dev->trans_start = jiffies;
  988. dev_kfree_skb(skb);
  989. return 0;
  990. }
  991. static struct net_device_stats *lance_get_stats(struct net_device *dev)
  992. {
  993. struct lance_private *lp = netdev_priv(dev);
  994. return &lp->stats;
  995. }
  996. /* taken from the depca driver */
  997. static void lance_load_multicast(struct net_device *dev)
  998. {
  999. struct lance_private *lp = netdev_priv(dev);
  1000. struct dev_mc_list *dmi = dev->mc_list;
  1001. char *addrs;
  1002. int i;
  1003. u32 crc;
  1004. u32 val;
  1005. /* set all multicast bits */
  1006. if (dev->flags & IFF_ALLMULTI)
  1007. val = ~0;
  1008. else
  1009. val = 0;
  1010. if (lp->pio_buffer) {
  1011. struct lance_init_block __iomem *ib = lp->init_block_iomem;
  1012. sbus_writel(val, &ib->filter[0]);
  1013. sbus_writel(val, &ib->filter[1]);
  1014. } else {
  1015. struct lance_init_block *ib = lp->init_block_mem;
  1016. ib->filter [0] = val;
  1017. ib->filter [1] = val;
  1018. }
  1019. if (dev->flags & IFF_ALLMULTI)
  1020. return;
  1021. /* Add addresses */
  1022. for (i = 0; i < dev->mc_count; i++) {
  1023. addrs = dmi->dmi_addr;
  1024. dmi = dmi->next;
  1025. /* multicast address? */
  1026. if (!(*addrs & 1))
  1027. continue;
  1028. crc = ether_crc_le(6, addrs);
  1029. crc = crc >> 26;
  1030. if (lp->pio_buffer) {
  1031. struct lance_init_block __iomem *ib = lp->init_block_iomem;
  1032. u16 __iomem *mcast_table = (u16 __iomem *) &ib->filter;
  1033. u16 tmp = sbus_readw(&mcast_table[crc>>4]);
  1034. tmp |= 1 << (crc & 0xf);
  1035. sbus_writew(tmp, &mcast_table[crc>>4]);
  1036. } else {
  1037. struct lance_init_block *ib = lp->init_block_mem;
  1038. u16 *mcast_table = (u16 *) &ib->filter;
  1039. mcast_table [crc >> 4] |= 1 << (crc & 0xf);
  1040. }
  1041. }
  1042. }
  1043. static void lance_set_multicast(struct net_device *dev)
  1044. {
  1045. struct lance_private *lp = netdev_priv(dev);
  1046. struct lance_init_block *ib_mem = lp->init_block_mem;
  1047. struct lance_init_block __iomem *ib_iomem = lp->init_block_iomem;
  1048. u16 mode;
  1049. if (!netif_running(dev))
  1050. return;
  1051. if (lp->tx_old != lp->tx_new) {
  1052. mod_timer(&lp->multicast_timer, jiffies + 4);
  1053. netif_wake_queue(dev);
  1054. return;
  1055. }
  1056. netif_stop_queue(dev);
  1057. STOP_LANCE(lp);
  1058. lp->init_ring(dev);
  1059. if (lp->pio_buffer)
  1060. mode = sbus_readw(&ib_iomem->mode);
  1061. else
  1062. mode = ib_mem->mode;
  1063. if (dev->flags & IFF_PROMISC) {
  1064. mode |= LE_MO_PROM;
  1065. if (lp->pio_buffer)
  1066. sbus_writew(mode, &ib_iomem->mode);
  1067. else
  1068. ib_mem->mode = mode;
  1069. } else {
  1070. mode &= ~LE_MO_PROM;
  1071. if (lp->pio_buffer)
  1072. sbus_writew(mode, &ib_iomem->mode);
  1073. else
  1074. ib_mem->mode = mode;
  1075. lance_load_multicast(dev);
  1076. }
  1077. load_csrs(lp);
  1078. init_restart_lance(lp);
  1079. netif_wake_queue(dev);
  1080. }
  1081. static void lance_set_multicast_retry(unsigned long _opaque)
  1082. {
  1083. struct net_device *dev = (struct net_device *) _opaque;
  1084. lance_set_multicast(dev);
  1085. }
  1086. static void lance_free_hwresources(struct lance_private *lp)
  1087. {
  1088. if (lp->lregs)
  1089. sbus_iounmap(lp->lregs, LANCE_REG_SIZE);
  1090. if (lp->init_block_iomem) {
  1091. sbus_iounmap(lp->init_block_iomem,
  1092. sizeof(struct lance_init_block));
  1093. } else if (lp->init_block_mem) {
  1094. sbus_free_consistent(lp->sdev,
  1095. sizeof(struct lance_init_block),
  1096. lp->init_block_mem,
  1097. lp->init_block_dvma);
  1098. }
  1099. }
  1100. /* Ethtool support... */
  1101. static void sparc_lance_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1102. {
  1103. struct lance_private *lp = netdev_priv(dev);
  1104. strcpy(info->driver, "sunlance");
  1105. strcpy(info->version, "2.02");
  1106. sprintf(info->bus_info, "SBUS:%d",
  1107. lp->sdev->slot);
  1108. }
  1109. static u32 sparc_lance_get_link(struct net_device *dev)
  1110. {
  1111. /* We really do not keep track of this, but this
  1112. * is better than not reporting anything at all.
  1113. */
  1114. return 1;
  1115. }
  1116. static const struct ethtool_ops sparc_lance_ethtool_ops = {
  1117. .get_drvinfo = sparc_lance_get_drvinfo,
  1118. .get_link = sparc_lance_get_link,
  1119. };
  1120. static int __devinit sparc_lance_probe_one(struct sbus_dev *sdev,
  1121. struct sbus_dma *ledma,
  1122. struct sbus_dev *lebuffer)
  1123. {
  1124. static unsigned version_printed;
  1125. struct net_device *dev;
  1126. struct lance_private *lp;
  1127. int i;
  1128. dev = alloc_etherdev(sizeof(struct lance_private) + 8);
  1129. if (!dev)
  1130. return -ENOMEM;
  1131. lp = netdev_priv(dev);
  1132. memset(lp, 0, sizeof(*lp));
  1133. if (sparc_lance_debug && version_printed++ == 0)
  1134. printk (KERN_INFO "%s", version);
  1135. spin_lock_init(&lp->lock);
  1136. /* Copy the IDPROM ethernet address to the device structure, later we
  1137. * will copy the address in the device structure to the lance
  1138. * initialization block.
  1139. */
  1140. for (i = 0; i < 6; i++)
  1141. dev->dev_addr[i] = idprom->id_ethaddr[i];
  1142. /* Get the IO region */
  1143. lp->lregs = sbus_ioremap(&sdev->resource[0], 0,
  1144. LANCE_REG_SIZE, lancestr);
  1145. if (!lp->lregs) {
  1146. printk(KERN_ERR "SunLance: Cannot map registers.\n");
  1147. goto fail;
  1148. }
  1149. lp->sdev = sdev;
  1150. if (lebuffer) {
  1151. /* sanity check */
  1152. if (lebuffer->resource[0].start & 7) {
  1153. printk(KERN_ERR "SunLance: ERROR: Rx and Tx rings not on even boundary.\n");
  1154. goto fail;
  1155. }
  1156. lp->init_block_iomem =
  1157. sbus_ioremap(&lebuffer->resource[0], 0,
  1158. sizeof(struct lance_init_block), "lebuffer");
  1159. if (!lp->init_block_iomem) {
  1160. printk(KERN_ERR "SunLance: Cannot map PIO buffer.\n");
  1161. goto fail;
  1162. }
  1163. lp->init_block_dvma = 0;
  1164. lp->pio_buffer = 1;
  1165. lp->init_ring = lance_init_ring_pio;
  1166. lp->rx = lance_rx_pio;
  1167. lp->tx = lance_tx_pio;
  1168. } else {
  1169. lp->init_block_mem =
  1170. sbus_alloc_consistent(sdev, sizeof(struct lance_init_block),
  1171. &lp->init_block_dvma);
  1172. if (!lp->init_block_mem || lp->init_block_dvma == 0) {
  1173. printk(KERN_ERR "SunLance: Cannot allocate consistent DMA memory.\n");
  1174. goto fail;
  1175. }
  1176. lp->pio_buffer = 0;
  1177. lp->init_ring = lance_init_ring_dvma;
  1178. lp->rx = lance_rx_dvma;
  1179. lp->tx = lance_tx_dvma;
  1180. }
  1181. lp->busmaster_regval = prom_getintdefault(sdev->prom_node,
  1182. "busmaster-regval",
  1183. (LE_C3_BSWP | LE_C3_ACON |
  1184. LE_C3_BCON));
  1185. lp->name = lancestr;
  1186. lp->ledma = ledma;
  1187. lp->burst_sizes = 0;
  1188. if (lp->ledma) {
  1189. char prop[6];
  1190. unsigned int sbmask;
  1191. u32 csr;
  1192. /* Find burst-size property for ledma */
  1193. lp->burst_sizes = prom_getintdefault(ledma->sdev->prom_node,
  1194. "burst-sizes", 0);
  1195. /* ledma may be capable of fast bursts, but sbus may not. */
  1196. sbmask = prom_getintdefault(ledma->sdev->bus->prom_node,
  1197. "burst-sizes", DMA_BURSTBITS);
  1198. lp->burst_sizes &= sbmask;
  1199. /* Get the cable-selection property */
  1200. memset(prop, 0, sizeof(prop));
  1201. prom_getstring(ledma->sdev->prom_node, "cable-selection",
  1202. prop, sizeof(prop));
  1203. if (prop[0] == 0) {
  1204. int topnd, nd;
  1205. printk(KERN_INFO "SunLance: using auto-carrier-detection.\n");
  1206. /* Is this found at /options .attributes in all
  1207. * Prom versions? XXX
  1208. */
  1209. topnd = prom_getchild(prom_root_node);
  1210. nd = prom_searchsiblings(topnd, "options");
  1211. if (!nd)
  1212. goto no_link_test;
  1213. if (!prom_node_has_property(nd, "tpe-link-test?"))
  1214. goto no_link_test;
  1215. memset(prop, 0, sizeof(prop));
  1216. prom_getstring(nd, "tpe-link-test?", prop,
  1217. sizeof(prop));
  1218. if (strcmp(prop, "true")) {
  1219. printk(KERN_NOTICE "SunLance: warning: overriding option "
  1220. "'tpe-link-test?'\n");
  1221. printk(KERN_NOTICE "SunLance: warning: mail any problems "
  1222. "to ecd@skynet.be\n");
  1223. auxio_set_lte(AUXIO_LTE_ON);
  1224. }
  1225. no_link_test:
  1226. lp->auto_select = 1;
  1227. lp->tpe = 0;
  1228. } else if (!strcmp(prop, "aui")) {
  1229. lp->auto_select = 0;
  1230. lp->tpe = 0;
  1231. } else {
  1232. lp->auto_select = 0;
  1233. lp->tpe = 1;
  1234. }
  1235. lp->dregs = ledma->regs;
  1236. /* Reset ledma */
  1237. csr = sbus_readl(lp->dregs + DMA_CSR);
  1238. sbus_writel(csr | DMA_RST_ENET, lp->dregs + DMA_CSR);
  1239. udelay(200);
  1240. sbus_writel(csr & ~DMA_RST_ENET, lp->dregs + DMA_CSR);
  1241. } else
  1242. lp->dregs = NULL;
  1243. lp->dev = dev;
  1244. SET_MODULE_OWNER(dev);
  1245. SET_NETDEV_DEV(dev, &sdev->ofdev.dev);
  1246. dev->open = &lance_open;
  1247. dev->stop = &lance_close;
  1248. dev->hard_start_xmit = &lance_start_xmit;
  1249. dev->tx_timeout = &lance_tx_timeout;
  1250. dev->watchdog_timeo = 5*HZ;
  1251. dev->get_stats = &lance_get_stats;
  1252. dev->set_multicast_list = &lance_set_multicast;
  1253. dev->ethtool_ops = &sparc_lance_ethtool_ops;
  1254. dev->irq = sdev->irqs[0];
  1255. dev->dma = 0;
  1256. /* We cannot sleep if the chip is busy during a
  1257. * multicast list update event, because such events
  1258. * can occur from interrupts (ex. IPv6). So we
  1259. * use a timer to try again later when necessary. -DaveM
  1260. */
  1261. init_timer(&lp->multicast_timer);
  1262. lp->multicast_timer.data = (unsigned long) dev;
  1263. lp->multicast_timer.function = &lance_set_multicast_retry;
  1264. if (register_netdev(dev)) {
  1265. printk(KERN_ERR "SunLance: Cannot register device.\n");
  1266. goto fail;
  1267. }
  1268. dev_set_drvdata(&sdev->ofdev.dev, lp);
  1269. printk(KERN_INFO "%s: LANCE ", dev->name);
  1270. for (i = 0; i < 6; i++)
  1271. printk("%2.2x%c", dev->dev_addr[i],
  1272. i == 5 ? ' ': ':');
  1273. printk("\n");
  1274. return 0;
  1275. fail:
  1276. lance_free_hwresources(lp);
  1277. free_netdev(dev);
  1278. return -ENODEV;
  1279. }
  1280. /* On 4m, find the associated dma for the lance chip */
  1281. static struct sbus_dma * __devinit find_ledma(struct sbus_dev *sdev)
  1282. {
  1283. struct sbus_dma *p;
  1284. for_each_dvma(p) {
  1285. if (p->sdev == sdev)
  1286. return p;
  1287. }
  1288. return NULL;
  1289. }
  1290. #ifdef CONFIG_SUN4
  1291. #include <asm/sun4paddr.h>
  1292. #include <asm/machines.h>
  1293. /* Find all the lance cards on the system and initialize them */
  1294. static struct sbus_dev sun4_sdev;
  1295. static int __devinit sparc_lance_init(void)
  1296. {
  1297. if ((idprom->id_machtype == (SM_SUN4|SM_4_330)) ||
  1298. (idprom->id_machtype == (SM_SUN4|SM_4_470))) {
  1299. memset(&sun4_sdev, 0, sizeof(struct sbus_dev));
  1300. sun4_sdev.reg_addrs[0].phys_addr = sun4_eth_physaddr;
  1301. sun4_sdev.irqs[0] = 6;
  1302. return sparc_lance_probe_one(&sun4_sdev, NULL, NULL);
  1303. }
  1304. return -ENODEV;
  1305. }
  1306. static int __exit sunlance_sun4_remove(void)
  1307. {
  1308. struct lance_private *lp = dev_get_drvdata(&sun4_sdev.ofdev.dev);
  1309. struct net_device *net_dev = lp->dev;
  1310. unregister_netdev(net_dev);
  1311. lance_free_hwresources(lp);
  1312. free_netdev(net_dev);
  1313. dev_set_drvdata(&sun4_sdev.ofdev.dev, NULL);
  1314. return 0;
  1315. }
  1316. #else /* !CONFIG_SUN4 */
  1317. static int __devinit sunlance_sbus_probe(struct of_device *dev, const struct of_device_id *match)
  1318. {
  1319. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  1320. int err;
  1321. if (sdev->parent) {
  1322. struct of_device *parent = &sdev->parent->ofdev;
  1323. if (!strcmp(parent->node->name, "ledma")) {
  1324. struct sbus_dma *ledma = find_ledma(to_sbus_device(&parent->dev));
  1325. err = sparc_lance_probe_one(sdev, ledma, NULL);
  1326. } else if (!strcmp(parent->node->name, "lebuffer")) {
  1327. err = sparc_lance_probe_one(sdev, NULL, to_sbus_device(&parent->dev));
  1328. } else
  1329. err = sparc_lance_probe_one(sdev, NULL, NULL);
  1330. } else
  1331. err = sparc_lance_probe_one(sdev, NULL, NULL);
  1332. return err;
  1333. }
  1334. static int __devexit sunlance_sbus_remove(struct of_device *dev)
  1335. {
  1336. struct lance_private *lp = dev_get_drvdata(&dev->dev);
  1337. struct net_device *net_dev = lp->dev;
  1338. unregister_netdev(net_dev);
  1339. lance_free_hwresources(lp);
  1340. free_netdev(net_dev);
  1341. dev_set_drvdata(&dev->dev, NULL);
  1342. return 0;
  1343. }
  1344. static struct of_device_id sunlance_sbus_match[] = {
  1345. {
  1346. .name = "le",
  1347. },
  1348. {},
  1349. };
  1350. MODULE_DEVICE_TABLE(of, sunlance_sbus_match);
  1351. static struct of_platform_driver sunlance_sbus_driver = {
  1352. .name = "sunlance",
  1353. .match_table = sunlance_sbus_match,
  1354. .probe = sunlance_sbus_probe,
  1355. .remove = __devexit_p(sunlance_sbus_remove),
  1356. };
  1357. /* Find all the lance cards on the system and initialize them */
  1358. static int __init sparc_lance_init(void)
  1359. {
  1360. return of_register_driver(&sunlance_sbus_driver, &sbus_bus_type);
  1361. }
  1362. #endif /* !CONFIG_SUN4 */
  1363. static void __exit sparc_lance_exit(void)
  1364. {
  1365. #ifdef CONFIG_SUN4
  1366. sunlance_sun4_remove();
  1367. #else
  1368. of_unregister_driver(&sunlance_sbus_driver);
  1369. #endif
  1370. }
  1371. module_init(sparc_lance_init);
  1372. module_exit(sparc_lance_exit);