sunlance.c 42 KB

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