ioc3-eth.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Driver for SGI's IOC3 based Ethernet cards as found in the PCI card.
  3. *
  4. * Copyright (C) 1999, 2000, 01, 03, 06 Ralf Baechle
  5. * Copyright (C) 1995, 1999, 2000, 2001 by Silicon Graphics, Inc.
  6. *
  7. * References:
  8. * o IOC3 ASIC specification 4.51, 1996-04-18
  9. * o IEEE 802.3 specification, 2000 edition
  10. * o DP38840A Specification, National Semiconductor, March 1997
  11. *
  12. * To do:
  13. *
  14. * o Use prefetching for large packets. What is a good lower limit for
  15. * prefetching?
  16. * o Use hardware checksums.
  17. * o Which PHYs might possibly be attached to the IOC3 in real live,
  18. * which workarounds are required for them? Do we ever have Lucent's?
  19. * o For the 2.5 branch kill the mii-tool ioctls.
  20. */
  21. #define IOC3_NAME "ioc3-eth"
  22. #define IOC3_VERSION "2.6.3-4"
  23. #include <linux/delay.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/errno.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/crc16.h>
  30. #include <linux/crc32.h>
  31. #include <linux/mii.h>
  32. #include <linux/in.h>
  33. #include <linux/io.h>
  34. #include <linux/ip.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/gfp.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/ethtool.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/nvmem-consumer.h>
  45. #include <net/ip.h>
  46. #include <asm/sn/ioc3.h>
  47. #include <asm/pci/bridge.h>
  48. #define CRC16_INIT 0
  49. #define CRC16_VALID 0xb001
  50. /* Number of RX buffers. This is tunable in the range of 16 <= x < 512.
  51. * The value must be a power of two.
  52. */
  53. #define RX_BUFFS 64
  54. #define RX_RING_ENTRIES 512 /* fixed in hardware */
  55. #define RX_RING_MASK (RX_RING_ENTRIES - 1)
  56. #define RX_RING_SIZE (RX_RING_ENTRIES * sizeof(u64))
  57. /* 128 TX buffers (not tunable) */
  58. #define TX_RING_ENTRIES 128
  59. #define TX_RING_MASK (TX_RING_ENTRIES - 1)
  60. #define TX_RING_SIZE (TX_RING_ENTRIES * sizeof(struct ioc3_etxd))
  61. /* IOC3 does dma transfers in 128 byte blocks */
  62. #define IOC3_DMA_XFER_LEN 128UL
  63. /* Every RX buffer starts with 8 byte descriptor data */
  64. #define RX_OFFSET (sizeof(struct ioc3_erxbuf) + NET_IP_ALIGN)
  65. #define RX_BUF_SIZE (13 * IOC3_DMA_XFER_LEN)
  66. #define ETCSR_FD ((21 << ETCSR_IPGR2_SHIFT) | (21 << ETCSR_IPGR1_SHIFT) | 21)
  67. #define ETCSR_HD ((17 << ETCSR_IPGR2_SHIFT) | (11 << ETCSR_IPGR1_SHIFT) | 21)
  68. /* Private per NIC data of the driver. */
  69. struct ioc3_private {
  70. struct ioc3_ethregs *regs;
  71. struct device *dma_dev;
  72. u32 *ssram;
  73. unsigned long *rxr; /* pointer to receiver ring */
  74. void *tx_ring;
  75. struct ioc3_etxd *txr;
  76. dma_addr_t rxr_dma;
  77. dma_addr_t txr_dma;
  78. struct sk_buff *rx_skbs[RX_RING_ENTRIES];
  79. struct sk_buff *tx_skbs[TX_RING_ENTRIES];
  80. int rx_ci; /* RX consumer index */
  81. int rx_pi; /* RX producer index */
  82. int tx_ci; /* TX consumer index */
  83. int tx_pi; /* TX producer index */
  84. int txqlen;
  85. u32 emcr, ehar_h, ehar_l;
  86. spinlock_t ioc3_lock;
  87. struct mii_if_info mii;
  88. /* Members used by autonegotiation */
  89. struct timer_list ioc3_timer;
  90. };
  91. static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  92. static void ioc3_set_multicast_list(struct net_device *dev);
  93. static netdev_tx_t ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev);
  94. static void ioc3_timeout(struct net_device *dev, unsigned int txqueue);
  95. static inline unsigned int ioc3_hash(const unsigned char *addr);
  96. static void ioc3_start(struct ioc3_private *ip);
  97. static inline void ioc3_stop(struct ioc3_private *ip);
  98. static void ioc3_init(struct net_device *dev);
  99. static int ioc3_alloc_rx_bufs(struct net_device *dev);
  100. static void ioc3_free_rx_bufs(struct ioc3_private *ip);
  101. static inline void ioc3_clean_tx_ring(struct ioc3_private *ip);
  102. static const struct ethtool_ops ioc3_ethtool_ops;
  103. static inline unsigned long aligned_rx_skb_addr(unsigned long addr)
  104. {
  105. return (~addr + 1) & (IOC3_DMA_XFER_LEN - 1UL);
  106. }
  107. static inline int ioc3_alloc_skb(struct ioc3_private *ip, struct sk_buff **skb,
  108. struct ioc3_erxbuf **rxb, dma_addr_t *rxb_dma)
  109. {
  110. struct sk_buff *new_skb;
  111. dma_addr_t d;
  112. int offset;
  113. new_skb = alloc_skb(RX_BUF_SIZE + IOC3_DMA_XFER_LEN - 1, GFP_ATOMIC);
  114. if (!new_skb)
  115. return -ENOMEM;
  116. /* ensure buffer is aligned to IOC3_DMA_XFER_LEN */
  117. offset = aligned_rx_skb_addr((unsigned long)new_skb->data);
  118. if (offset)
  119. skb_reserve(new_skb, offset);
  120. d = dma_map_single(ip->dma_dev, new_skb->data,
  121. RX_BUF_SIZE, DMA_FROM_DEVICE);
  122. if (dma_mapping_error(ip->dma_dev, d)) {
  123. dev_kfree_skb_any(new_skb);
  124. return -ENOMEM;
  125. }
  126. *rxb_dma = d;
  127. *rxb = (struct ioc3_erxbuf *)new_skb->data;
  128. skb_reserve(new_skb, RX_OFFSET);
  129. *skb = new_skb;
  130. return 0;
  131. }
  132. #ifdef CONFIG_PCI_XTALK_BRIDGE
  133. static inline unsigned long ioc3_map(dma_addr_t addr, unsigned long attr)
  134. {
  135. return (addr & ~PCI64_ATTR_BAR) | attr;
  136. }
  137. #define ERBAR_VAL (ERBAR_BARRIER_BIT << ERBAR_RXBARR_SHIFT)
  138. #else
  139. static inline unsigned long ioc3_map(dma_addr_t addr, unsigned long attr)
  140. {
  141. return addr;
  142. }
  143. #define ERBAR_VAL 0
  144. #endif
  145. static int ioc3eth_nvmem_match(struct device *dev, const void *data)
  146. {
  147. const char *name = dev_name(dev);
  148. const char *prefix = data;
  149. int prefix_len;
  150. prefix_len = strlen(prefix);
  151. if (strlen(name) < (prefix_len + 3))
  152. return 0;
  153. if (memcmp(prefix, name, prefix_len) != 0)
  154. return 0;
  155. /* found nvmem device which is attached to our ioc3
  156. * now check for one wire family code 09, 89 and 91
  157. */
  158. if (memcmp(name + prefix_len, "09-", 3) == 0)
  159. return 1;
  160. if (memcmp(name + prefix_len, "89-", 3) == 0)
  161. return 1;
  162. if (memcmp(name + prefix_len, "91-", 3) == 0)
  163. return 1;
  164. return 0;
  165. }
  166. static int ioc3eth_get_mac_addr(struct resource *res, u8 mac_addr[6])
  167. {
  168. struct nvmem_device *nvmem;
  169. char prefix[24];
  170. u8 prom[16];
  171. int ret;
  172. int i;
  173. snprintf(prefix, sizeof(prefix), "ioc3-%012llx-",
  174. res->start & ~0xffff);
  175. nvmem = nvmem_device_find(prefix, ioc3eth_nvmem_match);
  176. if (IS_ERR(nvmem))
  177. return PTR_ERR(nvmem);
  178. ret = nvmem_device_read(nvmem, 0, 16, prom);
  179. nvmem_device_put(nvmem);
  180. if (ret < 0)
  181. return ret;
  182. /* check, if content is valid */
  183. if (prom[0] != 0x0a ||
  184. crc16(CRC16_INIT, prom, 13) != CRC16_VALID)
  185. return -EINVAL;
  186. for (i = 0; i < 6; i++)
  187. mac_addr[i] = prom[10 - i];
  188. return 0;
  189. }
  190. static void __ioc3_set_mac_address(struct net_device *dev)
  191. {
  192. struct ioc3_private *ip = netdev_priv(dev);
  193. writel((dev->dev_addr[5] << 8) |
  194. dev->dev_addr[4],
  195. &ip->regs->emar_h);
  196. writel((dev->dev_addr[3] << 24) |
  197. (dev->dev_addr[2] << 16) |
  198. (dev->dev_addr[1] << 8) |
  199. dev->dev_addr[0],
  200. &ip->regs->emar_l);
  201. }
  202. static int ioc3_set_mac_address(struct net_device *dev, void *addr)
  203. {
  204. struct ioc3_private *ip = netdev_priv(dev);
  205. struct sockaddr *sa = addr;
  206. eth_hw_addr_set(dev, sa->sa_data);
  207. spin_lock_irq(&ip->ioc3_lock);
  208. __ioc3_set_mac_address(dev);
  209. spin_unlock_irq(&ip->ioc3_lock);
  210. return 0;
  211. }
  212. /* Caller must hold the ioc3_lock ever for MII readers. This is also
  213. * used to protect the transmitter side but it's low contention.
  214. */
  215. static int ioc3_mdio_read(struct net_device *dev, int phy, int reg)
  216. {
  217. struct ioc3_private *ip = netdev_priv(dev);
  218. struct ioc3_ethregs *regs = ip->regs;
  219. while (readl(&regs->micr) & MICR_BUSY)
  220. ;
  221. writel((phy << MICR_PHYADDR_SHIFT) | reg | MICR_READTRIG,
  222. &regs->micr);
  223. while (readl(&regs->micr) & MICR_BUSY)
  224. ;
  225. return readl(&regs->midr_r) & MIDR_DATA_MASK;
  226. }
  227. static void ioc3_mdio_write(struct net_device *dev, int phy, int reg, int data)
  228. {
  229. struct ioc3_private *ip = netdev_priv(dev);
  230. struct ioc3_ethregs *regs = ip->regs;
  231. while (readl(&regs->micr) & MICR_BUSY)
  232. ;
  233. writel(data, &regs->midr_w);
  234. writel((phy << MICR_PHYADDR_SHIFT) | reg, &regs->micr);
  235. while (readl(&regs->micr) & MICR_BUSY)
  236. ;
  237. }
  238. static int ioc3_mii_init(struct ioc3_private *ip);
  239. static struct net_device_stats *ioc3_get_stats(struct net_device *dev)
  240. {
  241. struct ioc3_private *ip = netdev_priv(dev);
  242. struct ioc3_ethregs *regs = ip->regs;
  243. dev->stats.collisions += readl(&regs->etcdc) & ETCDC_COLLCNT_MASK;
  244. return &dev->stats;
  245. }
  246. static void ioc3_tcpudp_checksum(struct sk_buff *skb, u32 hwsum, int len)
  247. {
  248. struct ethhdr *eh = eth_hdr(skb);
  249. unsigned int proto;
  250. unsigned char *cp;
  251. struct iphdr *ih;
  252. u32 csum, ehsum;
  253. u16 *ew;
  254. /* Did hardware handle the checksum at all? The cases we can handle
  255. * are:
  256. *
  257. * - TCP and UDP checksums of IPv4 only.
  258. * - IPv6 would be doable but we keep that for later ...
  259. * - Only unfragmented packets. Did somebody already tell you
  260. * fragmentation is evil?
  261. * - don't care about packet size. Worst case when processing a
  262. * malformed packet we'll try to access the packet at ip header +
  263. * 64 bytes which is still inside the skb. Even in the unlikely
  264. * case where the checksum is right the higher layers will still
  265. * drop the packet as appropriate.
  266. */
  267. if (eh->h_proto != htons(ETH_P_IP))
  268. return;
  269. ih = (struct iphdr *)((char *)eh + ETH_HLEN);
  270. if (ip_is_fragment(ih))
  271. return;
  272. proto = ih->protocol;
  273. if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
  274. return;
  275. /* Same as tx - compute csum of pseudo header */
  276. csum = hwsum +
  277. (ih->tot_len - (ih->ihl << 2)) +
  278. htons((u16)ih->protocol) +
  279. (ih->saddr >> 16) + (ih->saddr & 0xffff) +
  280. (ih->daddr >> 16) + (ih->daddr & 0xffff);
  281. /* Sum up ethernet dest addr, src addr and protocol */
  282. ew = (u16 *)eh;
  283. ehsum = ew[0] + ew[1] + ew[2] + ew[3] + ew[4] + ew[5] + ew[6];
  284. ehsum = (ehsum & 0xffff) + (ehsum >> 16);
  285. ehsum = (ehsum & 0xffff) + (ehsum >> 16);
  286. csum += 0xffff ^ ehsum;
  287. /* In the next step we also subtract the 1's complement
  288. * checksum of the trailing ethernet CRC.
  289. */
  290. cp = (char *)eh + len; /* points at trailing CRC */
  291. if (len & 1) {
  292. csum += 0xffff ^ (u16)((cp[1] << 8) | cp[0]);
  293. csum += 0xffff ^ (u16)((cp[3] << 8) | cp[2]);
  294. } else {
  295. csum += 0xffff ^ (u16)((cp[0] << 8) | cp[1]);
  296. csum += 0xffff ^ (u16)((cp[2] << 8) | cp[3]);
  297. }
  298. csum = (csum & 0xffff) + (csum >> 16);
  299. csum = (csum & 0xffff) + (csum >> 16);
  300. if (csum == 0xffff)
  301. skb->ip_summed = CHECKSUM_UNNECESSARY;
  302. }
  303. static inline void ioc3_rx(struct net_device *dev)
  304. {
  305. struct ioc3_private *ip = netdev_priv(dev);
  306. struct sk_buff *skb, *new_skb;
  307. int rx_entry, n_entry, len;
  308. struct ioc3_erxbuf *rxb;
  309. unsigned long *rxr;
  310. dma_addr_t d;
  311. u32 w0, err;
  312. rxr = ip->rxr; /* Ring base */
  313. rx_entry = ip->rx_ci; /* RX consume index */
  314. n_entry = ip->rx_pi;
  315. skb = ip->rx_skbs[rx_entry];
  316. rxb = (struct ioc3_erxbuf *)(skb->data - RX_OFFSET);
  317. w0 = be32_to_cpu(rxb->w0);
  318. while (w0 & ERXBUF_V) {
  319. err = be32_to_cpu(rxb->err); /* It's valid ... */
  320. if (err & ERXBUF_GOODPKT) {
  321. len = ((w0 >> ERXBUF_BYTECNT_SHIFT) & 0x7ff) - 4;
  322. skb_put(skb, len);
  323. skb->protocol = eth_type_trans(skb, dev);
  324. if (ioc3_alloc_skb(ip, &new_skb, &rxb, &d)) {
  325. /* Ouch, drop packet and just recycle packet
  326. * to keep the ring filled.
  327. */
  328. dev->stats.rx_dropped++;
  329. new_skb = skb;
  330. d = rxr[rx_entry];
  331. goto next;
  332. }
  333. if (likely(dev->features & NETIF_F_RXCSUM))
  334. ioc3_tcpudp_checksum(skb,
  335. w0 & ERXBUF_IPCKSUM_MASK,
  336. len);
  337. dma_unmap_single(ip->dma_dev, rxr[rx_entry],
  338. RX_BUF_SIZE, DMA_FROM_DEVICE);
  339. netif_rx(skb);
  340. ip->rx_skbs[rx_entry] = NULL; /* Poison */
  341. dev->stats.rx_packets++; /* Statistics */
  342. dev->stats.rx_bytes += len;
  343. } else {
  344. /* The frame is invalid and the skb never
  345. * reached the network layer so we can just
  346. * recycle it.
  347. */
  348. new_skb = skb;
  349. d = rxr[rx_entry];
  350. dev->stats.rx_errors++;
  351. }
  352. if (err & ERXBUF_CRCERR) /* Statistics */
  353. dev->stats.rx_crc_errors++;
  354. if (err & ERXBUF_FRAMERR)
  355. dev->stats.rx_frame_errors++;
  356. next:
  357. ip->rx_skbs[n_entry] = new_skb;
  358. rxr[n_entry] = cpu_to_be64(ioc3_map(d, PCI64_ATTR_BAR));
  359. rxb->w0 = 0; /* Clear valid flag */
  360. n_entry = (n_entry + 1) & RX_RING_MASK; /* Update erpir */
  361. /* Now go on to the next ring entry. */
  362. rx_entry = (rx_entry + 1) & RX_RING_MASK;
  363. skb = ip->rx_skbs[rx_entry];
  364. rxb = (struct ioc3_erxbuf *)(skb->data - RX_OFFSET);
  365. w0 = be32_to_cpu(rxb->w0);
  366. }
  367. writel((n_entry << 3) | ERPIR_ARM, &ip->regs->erpir);
  368. ip->rx_pi = n_entry;
  369. ip->rx_ci = rx_entry;
  370. }
  371. static inline void ioc3_tx(struct net_device *dev)
  372. {
  373. struct ioc3_private *ip = netdev_priv(dev);
  374. struct ioc3_ethregs *regs = ip->regs;
  375. unsigned long packets, bytes;
  376. int tx_entry, o_entry;
  377. struct sk_buff *skb;
  378. u32 etcir;
  379. spin_lock(&ip->ioc3_lock);
  380. etcir = readl(&regs->etcir);
  381. tx_entry = (etcir >> 7) & TX_RING_MASK;
  382. o_entry = ip->tx_ci;
  383. packets = 0;
  384. bytes = 0;
  385. while (o_entry != tx_entry) {
  386. packets++;
  387. skb = ip->tx_skbs[o_entry];
  388. bytes += skb->len;
  389. dev_consume_skb_irq(skb);
  390. ip->tx_skbs[o_entry] = NULL;
  391. o_entry = (o_entry + 1) & TX_RING_MASK; /* Next */
  392. etcir = readl(&regs->etcir); /* More pkts sent? */
  393. tx_entry = (etcir >> 7) & TX_RING_MASK;
  394. }
  395. dev->stats.tx_packets += packets;
  396. dev->stats.tx_bytes += bytes;
  397. ip->txqlen -= packets;
  398. if (netif_queue_stopped(dev) && ip->txqlen < TX_RING_ENTRIES)
  399. netif_wake_queue(dev);
  400. ip->tx_ci = o_entry;
  401. spin_unlock(&ip->ioc3_lock);
  402. }
  403. /* Deal with fatal IOC3 errors. This condition might be caused by a hard or
  404. * software problems, so we should try to recover
  405. * more gracefully if this ever happens. In theory we might be flooded
  406. * with such error interrupts if something really goes wrong, so we might
  407. * also consider to take the interface down.
  408. */
  409. static void ioc3_error(struct net_device *dev, u32 eisr)
  410. {
  411. struct ioc3_private *ip = netdev_priv(dev);
  412. spin_lock(&ip->ioc3_lock);
  413. if (eisr & EISR_RXOFLO)
  414. net_err_ratelimited("%s: RX overflow.\n", dev->name);
  415. if (eisr & EISR_RXBUFOFLO)
  416. net_err_ratelimited("%s: RX buffer overflow.\n", dev->name);
  417. if (eisr & EISR_RXMEMERR)
  418. net_err_ratelimited("%s: RX PCI error.\n", dev->name);
  419. if (eisr & EISR_RXPARERR)
  420. net_err_ratelimited("%s: RX SSRAM parity error.\n", dev->name);
  421. if (eisr & EISR_TXBUFUFLO)
  422. net_err_ratelimited("%s: TX buffer underflow.\n", dev->name);
  423. if (eisr & EISR_TXMEMERR)
  424. net_err_ratelimited("%s: TX PCI error.\n", dev->name);
  425. ioc3_stop(ip);
  426. ioc3_free_rx_bufs(ip);
  427. ioc3_clean_tx_ring(ip);
  428. ioc3_init(dev);
  429. if (ioc3_alloc_rx_bufs(dev)) {
  430. netdev_err(dev, "%s: rx buffer allocation failed\n", __func__);
  431. spin_unlock(&ip->ioc3_lock);
  432. return;
  433. }
  434. ioc3_start(ip);
  435. ioc3_mii_init(ip);
  436. netif_wake_queue(dev);
  437. spin_unlock(&ip->ioc3_lock);
  438. }
  439. /* The interrupt handler does all of the Rx thread work and cleans up
  440. * after the Tx thread.
  441. */
  442. static irqreturn_t ioc3_interrupt(int irq, void *dev_id)
  443. {
  444. struct ioc3_private *ip = netdev_priv(dev_id);
  445. struct ioc3_ethregs *regs = ip->regs;
  446. u32 eisr;
  447. eisr = readl(&regs->eisr);
  448. writel(eisr, &regs->eisr);
  449. readl(&regs->eisr); /* Flush */
  450. if (eisr & (EISR_RXOFLO | EISR_RXBUFOFLO | EISR_RXMEMERR |
  451. EISR_RXPARERR | EISR_TXBUFUFLO | EISR_TXMEMERR))
  452. ioc3_error(dev_id, eisr);
  453. if (eisr & EISR_RXTIMERINT)
  454. ioc3_rx(dev_id);
  455. if (eisr & EISR_TXEXPLICIT)
  456. ioc3_tx(dev_id);
  457. return IRQ_HANDLED;
  458. }
  459. static inline void ioc3_setup_duplex(struct ioc3_private *ip)
  460. {
  461. struct ioc3_ethregs *regs = ip->regs;
  462. spin_lock_irq(&ip->ioc3_lock);
  463. if (ip->mii.full_duplex) {
  464. writel(ETCSR_FD, &regs->etcsr);
  465. ip->emcr |= EMCR_DUPLEX;
  466. } else {
  467. writel(ETCSR_HD, &regs->etcsr);
  468. ip->emcr &= ~EMCR_DUPLEX;
  469. }
  470. writel(ip->emcr, &regs->emcr);
  471. spin_unlock_irq(&ip->ioc3_lock);
  472. }
  473. static void ioc3_timer(struct timer_list *t)
  474. {
  475. struct ioc3_private *ip = from_timer(ip, t, ioc3_timer);
  476. /* Print the link status if it has changed */
  477. mii_check_media(&ip->mii, 1, 0);
  478. ioc3_setup_duplex(ip);
  479. ip->ioc3_timer.expires = jiffies + ((12 * HZ) / 10); /* 1.2s */
  480. add_timer(&ip->ioc3_timer);
  481. }
  482. /* Try to find a PHY. There is no apparent relation between the MII addresses
  483. * in the SGI documentation and what we find in reality, so we simply probe
  484. * for the PHY.
  485. */
  486. static int ioc3_mii_init(struct ioc3_private *ip)
  487. {
  488. u16 word;
  489. int i;
  490. for (i = 0; i < 32; i++) {
  491. word = ioc3_mdio_read(ip->mii.dev, i, MII_PHYSID1);
  492. if (word != 0xffff && word != 0x0000) {
  493. ip->mii.phy_id = i;
  494. return 0;
  495. }
  496. }
  497. ip->mii.phy_id = -1;
  498. return -ENODEV;
  499. }
  500. static void ioc3_mii_start(struct ioc3_private *ip)
  501. {
  502. ip->ioc3_timer.expires = jiffies + (12 * HZ) / 10; /* 1.2 sec. */
  503. add_timer(&ip->ioc3_timer);
  504. }
  505. static inline void ioc3_tx_unmap(struct ioc3_private *ip, int entry)
  506. {
  507. struct ioc3_etxd *desc;
  508. u32 cmd, bufcnt, len;
  509. desc = &ip->txr[entry];
  510. cmd = be32_to_cpu(desc->cmd);
  511. bufcnt = be32_to_cpu(desc->bufcnt);
  512. if (cmd & ETXD_B1V) {
  513. len = (bufcnt & ETXD_B1CNT_MASK) >> ETXD_B1CNT_SHIFT;
  514. dma_unmap_single(ip->dma_dev, be64_to_cpu(desc->p1),
  515. len, DMA_TO_DEVICE);
  516. }
  517. if (cmd & ETXD_B2V) {
  518. len = (bufcnt & ETXD_B2CNT_MASK) >> ETXD_B2CNT_SHIFT;
  519. dma_unmap_single(ip->dma_dev, be64_to_cpu(desc->p2),
  520. len, DMA_TO_DEVICE);
  521. }
  522. }
  523. static inline void ioc3_clean_tx_ring(struct ioc3_private *ip)
  524. {
  525. struct sk_buff *skb;
  526. int i;
  527. for (i = 0; i < TX_RING_ENTRIES; i++) {
  528. skb = ip->tx_skbs[i];
  529. if (skb) {
  530. ioc3_tx_unmap(ip, i);
  531. ip->tx_skbs[i] = NULL;
  532. dev_kfree_skb_any(skb);
  533. }
  534. ip->txr[i].cmd = 0;
  535. }
  536. ip->tx_pi = 0;
  537. ip->tx_ci = 0;
  538. }
  539. static void ioc3_free_rx_bufs(struct ioc3_private *ip)
  540. {
  541. int rx_entry, n_entry;
  542. struct sk_buff *skb;
  543. n_entry = ip->rx_ci;
  544. rx_entry = ip->rx_pi;
  545. while (n_entry != rx_entry) {
  546. skb = ip->rx_skbs[n_entry];
  547. if (skb) {
  548. dma_unmap_single(ip->dma_dev,
  549. be64_to_cpu(ip->rxr[n_entry]),
  550. RX_BUF_SIZE, DMA_FROM_DEVICE);
  551. dev_kfree_skb_any(skb);
  552. }
  553. n_entry = (n_entry + 1) & RX_RING_MASK;
  554. }
  555. }
  556. static int ioc3_alloc_rx_bufs(struct net_device *dev)
  557. {
  558. struct ioc3_private *ip = netdev_priv(dev);
  559. struct ioc3_erxbuf *rxb;
  560. dma_addr_t d;
  561. int i;
  562. /* Now the rx buffers. The RX ring may be larger but
  563. * we only allocate 16 buffers for now. Need to tune
  564. * this for performance and memory later.
  565. */
  566. for (i = 0; i < RX_BUFFS; i++) {
  567. if (ioc3_alloc_skb(ip, &ip->rx_skbs[i], &rxb, &d))
  568. return -ENOMEM;
  569. rxb->w0 = 0; /* Clear valid flag */
  570. ip->rxr[i] = cpu_to_be64(ioc3_map(d, PCI64_ATTR_BAR));
  571. }
  572. ip->rx_ci = 0;
  573. ip->rx_pi = RX_BUFFS;
  574. return 0;
  575. }
  576. static inline void ioc3_ssram_disc(struct ioc3_private *ip)
  577. {
  578. struct ioc3_ethregs *regs = ip->regs;
  579. u32 *ssram0 = &ip->ssram[0x0000];
  580. u32 *ssram1 = &ip->ssram[0x4000];
  581. u32 pattern = 0x5555;
  582. /* Assume the larger size SSRAM and enable parity checking */
  583. writel(readl(&regs->emcr) | (EMCR_BUFSIZ | EMCR_RAMPAR), &regs->emcr);
  584. readl(&regs->emcr); /* Flush */
  585. writel(pattern, ssram0);
  586. writel(~pattern & IOC3_SSRAM_DM, ssram1);
  587. if ((readl(ssram0) & IOC3_SSRAM_DM) != pattern ||
  588. (readl(ssram1) & IOC3_SSRAM_DM) != (~pattern & IOC3_SSRAM_DM)) {
  589. /* set ssram size to 64 KB */
  590. ip->emcr |= EMCR_RAMPAR;
  591. writel(readl(&regs->emcr) & ~EMCR_BUFSIZ, &regs->emcr);
  592. } else {
  593. ip->emcr |= EMCR_BUFSIZ | EMCR_RAMPAR;
  594. }
  595. }
  596. static void ioc3_init(struct net_device *dev)
  597. {
  598. struct ioc3_private *ip = netdev_priv(dev);
  599. struct ioc3_ethregs *regs = ip->regs;
  600. del_timer_sync(&ip->ioc3_timer); /* Kill if running */
  601. writel(EMCR_RST, &regs->emcr); /* Reset */
  602. readl(&regs->emcr); /* Flush WB */
  603. udelay(4); /* Give it time ... */
  604. writel(0, &regs->emcr);
  605. readl(&regs->emcr);
  606. /* Misc registers */
  607. writel(ERBAR_VAL, &regs->erbar);
  608. readl(&regs->etcdc); /* Clear on read */
  609. writel(15, &regs->ercsr); /* RX low watermark */
  610. writel(0, &regs->ertr); /* Interrupt immediately */
  611. __ioc3_set_mac_address(dev);
  612. writel(ip->ehar_h, &regs->ehar_h);
  613. writel(ip->ehar_l, &regs->ehar_l);
  614. writel(42, &regs->ersr); /* XXX should be random */
  615. }
  616. static void ioc3_start(struct ioc3_private *ip)
  617. {
  618. struct ioc3_ethregs *regs = ip->regs;
  619. unsigned long ring;
  620. /* Now the rx ring base, consume & produce registers. */
  621. ring = ioc3_map(ip->rxr_dma, PCI64_ATTR_PREC);
  622. writel(ring >> 32, &regs->erbr_h);
  623. writel(ring & 0xffffffff, &regs->erbr_l);
  624. writel(ip->rx_ci << 3, &regs->ercir);
  625. writel((ip->rx_pi << 3) | ERPIR_ARM, &regs->erpir);
  626. ring = ioc3_map(ip->txr_dma, PCI64_ATTR_PREC);
  627. ip->txqlen = 0; /* nothing queued */
  628. /* Now the tx ring base, consume & produce registers. */
  629. writel(ring >> 32, &regs->etbr_h);
  630. writel(ring & 0xffffffff, &regs->etbr_l);
  631. writel(ip->tx_pi << 7, &regs->etpir);
  632. writel(ip->tx_ci << 7, &regs->etcir);
  633. readl(&regs->etcir); /* Flush */
  634. ip->emcr |= ((RX_OFFSET / 2) << EMCR_RXOFF_SHIFT) | EMCR_TXDMAEN |
  635. EMCR_TXEN | EMCR_RXDMAEN | EMCR_RXEN | EMCR_PADEN;
  636. writel(ip->emcr, &regs->emcr);
  637. writel(EISR_RXTIMERINT | EISR_RXOFLO | EISR_RXBUFOFLO |
  638. EISR_RXMEMERR | EISR_RXPARERR | EISR_TXBUFUFLO |
  639. EISR_TXEXPLICIT | EISR_TXMEMERR, &regs->eier);
  640. readl(&regs->eier);
  641. }
  642. static inline void ioc3_stop(struct ioc3_private *ip)
  643. {
  644. struct ioc3_ethregs *regs = ip->regs;
  645. writel(0, &regs->emcr); /* Shutup */
  646. writel(0, &regs->eier); /* Disable interrupts */
  647. readl(&regs->eier); /* Flush */
  648. }
  649. static int ioc3_open(struct net_device *dev)
  650. {
  651. struct ioc3_private *ip = netdev_priv(dev);
  652. ip->ehar_h = 0;
  653. ip->ehar_l = 0;
  654. ioc3_init(dev);
  655. if (ioc3_alloc_rx_bufs(dev)) {
  656. netdev_err(dev, "%s: rx buffer allocation failed\n", __func__);
  657. return -ENOMEM;
  658. }
  659. ioc3_start(ip);
  660. ioc3_mii_start(ip);
  661. netif_start_queue(dev);
  662. return 0;
  663. }
  664. static int ioc3_close(struct net_device *dev)
  665. {
  666. struct ioc3_private *ip = netdev_priv(dev);
  667. del_timer_sync(&ip->ioc3_timer);
  668. netif_stop_queue(dev);
  669. ioc3_stop(ip);
  670. ioc3_free_rx_bufs(ip);
  671. ioc3_clean_tx_ring(ip);
  672. return 0;
  673. }
  674. static const struct net_device_ops ioc3_netdev_ops = {
  675. .ndo_open = ioc3_open,
  676. .ndo_stop = ioc3_close,
  677. .ndo_start_xmit = ioc3_start_xmit,
  678. .ndo_tx_timeout = ioc3_timeout,
  679. .ndo_get_stats = ioc3_get_stats,
  680. .ndo_set_rx_mode = ioc3_set_multicast_list,
  681. .ndo_eth_ioctl = ioc3_ioctl,
  682. .ndo_validate_addr = eth_validate_addr,
  683. .ndo_set_mac_address = ioc3_set_mac_address,
  684. };
  685. static int ioc3eth_probe(struct platform_device *pdev)
  686. {
  687. u32 sw_physid1, sw_physid2, vendor, model, rev;
  688. struct ioc3_private *ip;
  689. struct net_device *dev;
  690. struct resource *regs;
  691. u8 mac_addr[6];
  692. int err;
  693. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  694. if (!regs) {
  695. dev_err(&pdev->dev, "Invalid resource\n");
  696. return -EINVAL;
  697. }
  698. /* get mac addr from one wire prom */
  699. if (ioc3eth_get_mac_addr(regs, mac_addr))
  700. return -EPROBE_DEFER; /* not available yet */
  701. dev = alloc_etherdev(sizeof(struct ioc3_private));
  702. if (!dev)
  703. return -ENOMEM;
  704. SET_NETDEV_DEV(dev, &pdev->dev);
  705. ip = netdev_priv(dev);
  706. ip->dma_dev = pdev->dev.parent;
  707. ip->regs = devm_platform_ioremap_resource(pdev, 0);
  708. if (IS_ERR(ip->regs)) {
  709. err = PTR_ERR(ip->regs);
  710. goto out_free;
  711. }
  712. ip->ssram = devm_platform_ioremap_resource(pdev, 1);
  713. if (IS_ERR(ip->ssram)) {
  714. err = PTR_ERR(ip->ssram);
  715. goto out_free;
  716. }
  717. dev->irq = platform_get_irq(pdev, 0);
  718. if (dev->irq < 0) {
  719. err = dev->irq;
  720. goto out_free;
  721. }
  722. if (devm_request_irq(&pdev->dev, dev->irq, ioc3_interrupt,
  723. IRQF_SHARED, "ioc3-eth", dev)) {
  724. dev_err(&pdev->dev, "Can't get irq %d\n", dev->irq);
  725. err = -ENODEV;
  726. goto out_free;
  727. }
  728. spin_lock_init(&ip->ioc3_lock);
  729. timer_setup(&ip->ioc3_timer, ioc3_timer, 0);
  730. ioc3_stop(ip);
  731. /* Allocate rx ring. 4kb = 512 entries, must be 4kb aligned */
  732. ip->rxr = dma_alloc_coherent(ip->dma_dev, RX_RING_SIZE, &ip->rxr_dma,
  733. GFP_KERNEL);
  734. if (!ip->rxr) {
  735. pr_err("ioc3-eth: rx ring allocation failed\n");
  736. err = -ENOMEM;
  737. goto out_stop;
  738. }
  739. /* Allocate tx rings. 16kb = 128 bufs, must be 16kb aligned */
  740. ip->tx_ring = dma_alloc_coherent(ip->dma_dev, TX_RING_SIZE + SZ_16K - 1,
  741. &ip->txr_dma, GFP_KERNEL);
  742. if (!ip->tx_ring) {
  743. pr_err("ioc3-eth: tx ring allocation failed\n");
  744. err = -ENOMEM;
  745. goto out_stop;
  746. }
  747. /* Align TX ring */
  748. ip->txr = PTR_ALIGN(ip->tx_ring, SZ_16K);
  749. ip->txr_dma = ALIGN(ip->txr_dma, SZ_16K);
  750. ioc3_init(dev);
  751. ip->mii.phy_id_mask = 0x1f;
  752. ip->mii.reg_num_mask = 0x1f;
  753. ip->mii.dev = dev;
  754. ip->mii.mdio_read = ioc3_mdio_read;
  755. ip->mii.mdio_write = ioc3_mdio_write;
  756. ioc3_mii_init(ip);
  757. if (ip->mii.phy_id == -1) {
  758. netdev_err(dev, "Didn't find a PHY, goodbye.\n");
  759. err = -ENODEV;
  760. goto out_stop;
  761. }
  762. ioc3_mii_start(ip);
  763. ioc3_ssram_disc(ip);
  764. eth_hw_addr_set(dev, mac_addr);
  765. /* The IOC3-specific entries in the device structure. */
  766. dev->watchdog_timeo = 5 * HZ;
  767. dev->netdev_ops = &ioc3_netdev_ops;
  768. dev->ethtool_ops = &ioc3_ethtool_ops;
  769. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  770. dev->features = NETIF_F_IP_CSUM | NETIF_F_HIGHDMA;
  771. sw_physid1 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID1);
  772. sw_physid2 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID2);
  773. err = register_netdev(dev);
  774. if (err)
  775. goto out_stop;
  776. mii_check_media(&ip->mii, 1, 1);
  777. ioc3_setup_duplex(ip);
  778. vendor = (sw_physid1 << 12) | (sw_physid2 >> 4);
  779. model = (sw_physid2 >> 4) & 0x3f;
  780. rev = sw_physid2 & 0xf;
  781. netdev_info(dev, "Using PHY %d, vendor 0x%x, model %d, rev %d.\n",
  782. ip->mii.phy_id, vendor, model, rev);
  783. netdev_info(dev, "IOC3 SSRAM has %d kbyte.\n",
  784. ip->emcr & EMCR_BUFSIZ ? 128 : 64);
  785. return 0;
  786. out_stop:
  787. del_timer_sync(&ip->ioc3_timer);
  788. if (ip->rxr)
  789. dma_free_coherent(ip->dma_dev, RX_RING_SIZE, ip->rxr,
  790. ip->rxr_dma);
  791. if (ip->tx_ring)
  792. dma_free_coherent(ip->dma_dev, TX_RING_SIZE + SZ_16K - 1, ip->tx_ring,
  793. ip->txr_dma);
  794. out_free:
  795. free_netdev(dev);
  796. return err;
  797. }
  798. static int ioc3eth_remove(struct platform_device *pdev)
  799. {
  800. struct net_device *dev = platform_get_drvdata(pdev);
  801. struct ioc3_private *ip = netdev_priv(dev);
  802. dma_free_coherent(ip->dma_dev, RX_RING_SIZE, ip->rxr, ip->rxr_dma);
  803. dma_free_coherent(ip->dma_dev, TX_RING_SIZE + SZ_16K - 1, ip->tx_ring, ip->txr_dma);
  804. unregister_netdev(dev);
  805. del_timer_sync(&ip->ioc3_timer);
  806. free_netdev(dev);
  807. return 0;
  808. }
  809. static netdev_tx_t ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev)
  810. {
  811. struct ioc3_private *ip = netdev_priv(dev);
  812. struct ioc3_etxd *desc;
  813. unsigned long data;
  814. unsigned int len;
  815. int produce;
  816. u32 w0 = 0;
  817. /* IOC3 has a fairly simple minded checksumming hardware which simply
  818. * adds up the 1's complement checksum for the entire packet and
  819. * inserts it at an offset which can be specified in the descriptor
  820. * into the transmit packet. This means we have to compensate for the
  821. * MAC header which should not be summed and the TCP/UDP pseudo headers
  822. * manually.
  823. */
  824. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  825. const struct iphdr *ih = ip_hdr(skb);
  826. const int proto = ntohs(ih->protocol);
  827. unsigned int csoff;
  828. u32 csum, ehsum;
  829. u16 *eh;
  830. /* The MAC header. skb->mac seem the logic approach
  831. * to find the MAC header - except it's a NULL pointer ...
  832. */
  833. eh = (u16 *)skb->data;
  834. /* Sum up dest addr, src addr and protocol */
  835. ehsum = eh[0] + eh[1] + eh[2] + eh[3] + eh[4] + eh[5] + eh[6];
  836. /* Skip IP header; it's sum is always zero and was
  837. * already filled in by ip_output.c
  838. */
  839. csum = csum_tcpudp_nofold(ih->saddr, ih->daddr,
  840. ih->tot_len - (ih->ihl << 2),
  841. proto, csum_fold(ehsum));
  842. csum = (csum & 0xffff) + (csum >> 16); /* Fold again */
  843. csum = (csum & 0xffff) + (csum >> 16);
  844. csoff = ETH_HLEN + (ih->ihl << 2);
  845. if (proto == IPPROTO_UDP) {
  846. csoff += offsetof(struct udphdr, check);
  847. udp_hdr(skb)->check = csum;
  848. }
  849. if (proto == IPPROTO_TCP) {
  850. csoff += offsetof(struct tcphdr, check);
  851. tcp_hdr(skb)->check = csum;
  852. }
  853. w0 = ETXD_DOCHECKSUM | (csoff << ETXD_CHKOFF_SHIFT);
  854. }
  855. spin_lock_irq(&ip->ioc3_lock);
  856. data = (unsigned long)skb->data;
  857. len = skb->len;
  858. produce = ip->tx_pi;
  859. desc = &ip->txr[produce];
  860. if (len <= 104) {
  861. /* Short packet, let's copy it directly into the ring. */
  862. skb_copy_from_linear_data(skb, desc->data, skb->len);
  863. if (len < ETH_ZLEN) {
  864. /* Very short packet, pad with zeros at the end. */
  865. memset(desc->data + len, 0, ETH_ZLEN - len);
  866. len = ETH_ZLEN;
  867. }
  868. desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_D0V | w0);
  869. desc->bufcnt = cpu_to_be32(len);
  870. } else if ((data ^ (data + len - 1)) & 0x4000) {
  871. unsigned long b2 = (data | 0x3fffUL) + 1UL;
  872. unsigned long s1 = b2 - data;
  873. unsigned long s2 = data + len - b2;
  874. dma_addr_t d1, d2;
  875. desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE |
  876. ETXD_B1V | ETXD_B2V | w0);
  877. desc->bufcnt = cpu_to_be32((s1 << ETXD_B1CNT_SHIFT) |
  878. (s2 << ETXD_B2CNT_SHIFT));
  879. d1 = dma_map_single(ip->dma_dev, skb->data, s1, DMA_TO_DEVICE);
  880. if (dma_mapping_error(ip->dma_dev, d1))
  881. goto drop_packet;
  882. d2 = dma_map_single(ip->dma_dev, (void *)b2, s1, DMA_TO_DEVICE);
  883. if (dma_mapping_error(ip->dma_dev, d2)) {
  884. dma_unmap_single(ip->dma_dev, d1, len, DMA_TO_DEVICE);
  885. goto drop_packet;
  886. }
  887. desc->p1 = cpu_to_be64(ioc3_map(d1, PCI64_ATTR_PREF));
  888. desc->p2 = cpu_to_be64(ioc3_map(d2, PCI64_ATTR_PREF));
  889. } else {
  890. dma_addr_t d;
  891. /* Normal sized packet that doesn't cross a page boundary. */
  892. desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_B1V | w0);
  893. desc->bufcnt = cpu_to_be32(len << ETXD_B1CNT_SHIFT);
  894. d = dma_map_single(ip->dma_dev, skb->data, len, DMA_TO_DEVICE);
  895. if (dma_mapping_error(ip->dma_dev, d))
  896. goto drop_packet;
  897. desc->p1 = cpu_to_be64(ioc3_map(d, PCI64_ATTR_PREF));
  898. }
  899. mb(); /* make sure all descriptor changes are visible */
  900. ip->tx_skbs[produce] = skb; /* Remember skb */
  901. produce = (produce + 1) & TX_RING_MASK;
  902. ip->tx_pi = produce;
  903. writel(produce << 7, &ip->regs->etpir); /* Fire ... */
  904. ip->txqlen++;
  905. if (ip->txqlen >= (TX_RING_ENTRIES - 1))
  906. netif_stop_queue(dev);
  907. spin_unlock_irq(&ip->ioc3_lock);
  908. return NETDEV_TX_OK;
  909. drop_packet:
  910. dev_kfree_skb_any(skb);
  911. dev->stats.tx_dropped++;
  912. spin_unlock_irq(&ip->ioc3_lock);
  913. return NETDEV_TX_OK;
  914. }
  915. static void ioc3_timeout(struct net_device *dev, unsigned int txqueue)
  916. {
  917. struct ioc3_private *ip = netdev_priv(dev);
  918. netdev_err(dev, "transmit timed out, resetting\n");
  919. spin_lock_irq(&ip->ioc3_lock);
  920. ioc3_stop(ip);
  921. ioc3_free_rx_bufs(ip);
  922. ioc3_clean_tx_ring(ip);
  923. ioc3_init(dev);
  924. if (ioc3_alloc_rx_bufs(dev)) {
  925. netdev_err(dev, "%s: rx buffer allocation failed\n", __func__);
  926. spin_unlock_irq(&ip->ioc3_lock);
  927. return;
  928. }
  929. ioc3_start(ip);
  930. ioc3_mii_init(ip);
  931. ioc3_mii_start(ip);
  932. spin_unlock_irq(&ip->ioc3_lock);
  933. netif_wake_queue(dev);
  934. }
  935. /* Given a multicast ethernet address, this routine calculates the
  936. * address's bit index in the logical address filter mask
  937. */
  938. static inline unsigned int ioc3_hash(const unsigned char *addr)
  939. {
  940. unsigned int temp = 0;
  941. int bits;
  942. u32 crc;
  943. crc = ether_crc_le(ETH_ALEN, addr);
  944. crc &= 0x3f; /* bit reverse lowest 6 bits for hash index */
  945. for (bits = 6; --bits >= 0; ) {
  946. temp <<= 1;
  947. temp |= (crc & 0x1);
  948. crc >>= 1;
  949. }
  950. return temp;
  951. }
  952. static void ioc3_get_drvinfo(struct net_device *dev,
  953. struct ethtool_drvinfo *info)
  954. {
  955. strscpy(info->driver, IOC3_NAME, sizeof(info->driver));
  956. strscpy(info->version, IOC3_VERSION, sizeof(info->version));
  957. strscpy(info->bus_info, pci_name(to_pci_dev(dev->dev.parent)),
  958. sizeof(info->bus_info));
  959. }
  960. static int ioc3_get_link_ksettings(struct net_device *dev,
  961. struct ethtool_link_ksettings *cmd)
  962. {
  963. struct ioc3_private *ip = netdev_priv(dev);
  964. spin_lock_irq(&ip->ioc3_lock);
  965. mii_ethtool_get_link_ksettings(&ip->mii, cmd);
  966. spin_unlock_irq(&ip->ioc3_lock);
  967. return 0;
  968. }
  969. static int ioc3_set_link_ksettings(struct net_device *dev,
  970. const struct ethtool_link_ksettings *cmd)
  971. {
  972. struct ioc3_private *ip = netdev_priv(dev);
  973. int rc;
  974. spin_lock_irq(&ip->ioc3_lock);
  975. rc = mii_ethtool_set_link_ksettings(&ip->mii, cmd);
  976. spin_unlock_irq(&ip->ioc3_lock);
  977. return rc;
  978. }
  979. static int ioc3_nway_reset(struct net_device *dev)
  980. {
  981. struct ioc3_private *ip = netdev_priv(dev);
  982. int rc;
  983. spin_lock_irq(&ip->ioc3_lock);
  984. rc = mii_nway_restart(&ip->mii);
  985. spin_unlock_irq(&ip->ioc3_lock);
  986. return rc;
  987. }
  988. static u32 ioc3_get_link(struct net_device *dev)
  989. {
  990. struct ioc3_private *ip = netdev_priv(dev);
  991. int rc;
  992. spin_lock_irq(&ip->ioc3_lock);
  993. rc = mii_link_ok(&ip->mii);
  994. spin_unlock_irq(&ip->ioc3_lock);
  995. return rc;
  996. }
  997. static const struct ethtool_ops ioc3_ethtool_ops = {
  998. .get_drvinfo = ioc3_get_drvinfo,
  999. .nway_reset = ioc3_nway_reset,
  1000. .get_link = ioc3_get_link,
  1001. .get_link_ksettings = ioc3_get_link_ksettings,
  1002. .set_link_ksettings = ioc3_set_link_ksettings,
  1003. };
  1004. static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1005. {
  1006. struct ioc3_private *ip = netdev_priv(dev);
  1007. int rc;
  1008. spin_lock_irq(&ip->ioc3_lock);
  1009. rc = generic_mii_ioctl(&ip->mii, if_mii(rq), cmd, NULL);
  1010. spin_unlock_irq(&ip->ioc3_lock);
  1011. return rc;
  1012. }
  1013. static void ioc3_set_multicast_list(struct net_device *dev)
  1014. {
  1015. struct ioc3_private *ip = netdev_priv(dev);
  1016. struct ioc3_ethregs *regs = ip->regs;
  1017. struct netdev_hw_addr *ha;
  1018. u64 ehar = 0;
  1019. spin_lock_irq(&ip->ioc3_lock);
  1020. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1021. ip->emcr |= EMCR_PROMISC;
  1022. writel(ip->emcr, &regs->emcr);
  1023. readl(&regs->emcr);
  1024. } else {
  1025. ip->emcr &= ~EMCR_PROMISC;
  1026. writel(ip->emcr, &regs->emcr); /* Clear promiscuous. */
  1027. readl(&regs->emcr);
  1028. if ((dev->flags & IFF_ALLMULTI) ||
  1029. (netdev_mc_count(dev) > 64)) {
  1030. /* Too many for hashing to make sense or we want all
  1031. * multicast packets anyway, so skip computing all the
  1032. * hashes and just accept all packets.
  1033. */
  1034. ip->ehar_h = 0xffffffff;
  1035. ip->ehar_l = 0xffffffff;
  1036. } else {
  1037. netdev_for_each_mc_addr(ha, dev) {
  1038. ehar |= (1UL << ioc3_hash(ha->addr));
  1039. }
  1040. ip->ehar_h = ehar >> 32;
  1041. ip->ehar_l = ehar & 0xffffffff;
  1042. }
  1043. writel(ip->ehar_h, &regs->ehar_h);
  1044. writel(ip->ehar_l, &regs->ehar_l);
  1045. }
  1046. spin_unlock_irq(&ip->ioc3_lock);
  1047. }
  1048. static struct platform_driver ioc3eth_driver = {
  1049. .probe = ioc3eth_probe,
  1050. .remove = ioc3eth_remove,
  1051. .driver = {
  1052. .name = "ioc3-eth",
  1053. }
  1054. };
  1055. module_platform_driver(ioc3eth_driver);
  1056. MODULE_AUTHOR("Ralf Baechle <[email protected]>");
  1057. MODULE_DESCRIPTION("SGI IOC3 Ethernet driver");
  1058. MODULE_LICENSE("GPL");