fs_enet-main.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
  3. *
  4. * Copyright (c) 2003 Intracom S.A.
  5. * by Pantelis Antoniou <panto@intracom.gr>
  6. *
  7. * 2005 (c) MontaVista Software, Inc.
  8. * Vitaly Bordug <vbordug@ru.mvista.com>
  9. *
  10. * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
  11. * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
  12. *
  13. * This file is licensed under the terms of the GNU General Public License
  14. * version 2. This program is licensed "as is" without any warranty of any
  15. * kind, whether express or implied.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/errno.h>
  23. #include <linux/ioport.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/mii.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/bitops.h>
  35. #include <linux/fs.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/phy.h>
  38. #include <linux/of.h>
  39. #include <linux/of_mdio.h>
  40. #include <linux/of_platform.h>
  41. #include <linux/of_gpio.h>
  42. #include <linux/vmalloc.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/irq.h>
  45. #include <asm/uaccess.h>
  46. #include "fs_enet.h"
  47. /*************************************************/
  48. MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
  49. MODULE_DESCRIPTION("Freescale Ethernet Driver");
  50. MODULE_LICENSE("GPL");
  51. MODULE_VERSION(DRV_MODULE_VERSION);
  52. static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
  53. module_param(fs_enet_debug, int, 0);
  54. MODULE_PARM_DESC(fs_enet_debug,
  55. "Freescale bitmapped debugging message enable value");
  56. #ifdef CONFIG_NET_POLL_CONTROLLER
  57. static void fs_enet_netpoll(struct net_device *dev);
  58. #endif
  59. static void fs_set_multicast_list(struct net_device *dev)
  60. {
  61. struct fs_enet_private *fep = netdev_priv(dev);
  62. (*fep->ops->set_multicast_list)(dev);
  63. }
  64. static void skb_align(struct sk_buff *skb, int align)
  65. {
  66. int off = ((unsigned long)skb->data) & (align - 1);
  67. if (off)
  68. skb_reserve(skb, align - off);
  69. }
  70. /* NAPI receive function */
  71. static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
  72. {
  73. struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
  74. struct net_device *dev = fep->ndev;
  75. const struct fs_platform_info *fpi = fep->fpi;
  76. cbd_t __iomem *bdp;
  77. struct sk_buff *skb, *skbn, *skbt;
  78. int received = 0;
  79. u16 pkt_len, sc;
  80. int curidx;
  81. /*
  82. * First, grab all of the stats for the incoming packet.
  83. * These get messed up if we get called due to a busy condition.
  84. */
  85. bdp = fep->cur_rx;
  86. /* clear RX status bits for napi*/
  87. (*fep->ops->napi_clear_rx_event)(dev);
  88. while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
  89. curidx = bdp - fep->rx_bd_base;
  90. /*
  91. * Since we have allocated space to hold a complete frame,
  92. * the last indicator should be set.
  93. */
  94. if ((sc & BD_ENET_RX_LAST) == 0)
  95. dev_warn(fep->dev, "rcv is not +last\n");
  96. /*
  97. * Check for errors.
  98. */
  99. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
  100. BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  101. fep->stats.rx_errors++;
  102. /* Frame too long or too short. */
  103. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
  104. fep->stats.rx_length_errors++;
  105. /* Frame alignment */
  106. if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  107. fep->stats.rx_frame_errors++;
  108. /* CRC Error */
  109. if (sc & BD_ENET_RX_CR)
  110. fep->stats.rx_crc_errors++;
  111. /* FIFO overrun */
  112. if (sc & BD_ENET_RX_OV)
  113. fep->stats.rx_crc_errors++;
  114. skb = fep->rx_skbuff[curidx];
  115. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  116. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  117. DMA_FROM_DEVICE);
  118. skbn = skb;
  119. } else {
  120. skb = fep->rx_skbuff[curidx];
  121. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  122. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  123. DMA_FROM_DEVICE);
  124. /*
  125. * Process the incoming frame.
  126. */
  127. fep->stats.rx_packets++;
  128. pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
  129. fep->stats.rx_bytes += pkt_len + 4;
  130. if (pkt_len <= fpi->rx_copybreak) {
  131. /* +2 to make IP header L1 cache aligned */
  132. skbn = dev_alloc_skb(pkt_len + 2);
  133. if (skbn != NULL) {
  134. skb_reserve(skbn, 2); /* align IP header */
  135. skb_copy_from_linear_data(skb,
  136. skbn->data, pkt_len);
  137. /* swap */
  138. skbt = skb;
  139. skb = skbn;
  140. skbn = skbt;
  141. }
  142. } else {
  143. skbn = dev_alloc_skb(ENET_RX_FRSIZE);
  144. if (skbn)
  145. skb_align(skbn, ENET_RX_ALIGN);
  146. }
  147. if (skbn != NULL) {
  148. skb_put(skb, pkt_len); /* Make room */
  149. skb->protocol = eth_type_trans(skb, dev);
  150. received++;
  151. netif_receive_skb(skb);
  152. } else {
  153. dev_warn(fep->dev,
  154. "Memory squeeze, dropping packet.\n");
  155. fep->stats.rx_dropped++;
  156. skbn = skb;
  157. }
  158. }
  159. fep->rx_skbuff[curidx] = skbn;
  160. CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
  161. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  162. DMA_FROM_DEVICE));
  163. CBDW_DATLEN(bdp, 0);
  164. CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
  165. /*
  166. * Update BD pointer to next entry.
  167. */
  168. if ((sc & BD_ENET_RX_WRAP) == 0)
  169. bdp++;
  170. else
  171. bdp = fep->rx_bd_base;
  172. (*fep->ops->rx_bd_done)(dev);
  173. if (received >= budget)
  174. break;
  175. }
  176. fep->cur_rx = bdp;
  177. if (received < budget) {
  178. /* done */
  179. napi_complete(napi);
  180. (*fep->ops->napi_enable_rx)(dev);
  181. }
  182. return received;
  183. }
  184. /* non NAPI receive function */
  185. static int fs_enet_rx_non_napi(struct net_device *dev)
  186. {
  187. struct fs_enet_private *fep = netdev_priv(dev);
  188. const struct fs_platform_info *fpi = fep->fpi;
  189. cbd_t __iomem *bdp;
  190. struct sk_buff *skb, *skbn, *skbt;
  191. int received = 0;
  192. u16 pkt_len, sc;
  193. int curidx;
  194. /*
  195. * First, grab all of the stats for the incoming packet.
  196. * These get messed up if we get called due to a busy condition.
  197. */
  198. bdp = fep->cur_rx;
  199. while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
  200. curidx = bdp - fep->rx_bd_base;
  201. /*
  202. * Since we have allocated space to hold a complete frame,
  203. * the last indicator should be set.
  204. */
  205. if ((sc & BD_ENET_RX_LAST) == 0)
  206. dev_warn(fep->dev, "rcv is not +last\n");
  207. /*
  208. * Check for errors.
  209. */
  210. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
  211. BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  212. fep->stats.rx_errors++;
  213. /* Frame too long or too short. */
  214. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
  215. fep->stats.rx_length_errors++;
  216. /* Frame alignment */
  217. if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  218. fep->stats.rx_frame_errors++;
  219. /* CRC Error */
  220. if (sc & BD_ENET_RX_CR)
  221. fep->stats.rx_crc_errors++;
  222. /* FIFO overrun */
  223. if (sc & BD_ENET_RX_OV)
  224. fep->stats.rx_crc_errors++;
  225. skb = fep->rx_skbuff[curidx];
  226. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  227. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  228. DMA_FROM_DEVICE);
  229. skbn = skb;
  230. } else {
  231. skb = fep->rx_skbuff[curidx];
  232. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  233. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  234. DMA_FROM_DEVICE);
  235. /*
  236. * Process the incoming frame.
  237. */
  238. fep->stats.rx_packets++;
  239. pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
  240. fep->stats.rx_bytes += pkt_len + 4;
  241. if (pkt_len <= fpi->rx_copybreak) {
  242. /* +2 to make IP header L1 cache aligned */
  243. skbn = dev_alloc_skb(pkt_len + 2);
  244. if (skbn != NULL) {
  245. skb_reserve(skbn, 2); /* align IP header */
  246. skb_copy_from_linear_data(skb,
  247. skbn->data, pkt_len);
  248. /* swap */
  249. skbt = skb;
  250. skb = skbn;
  251. skbn = skbt;
  252. }
  253. } else {
  254. skbn = dev_alloc_skb(ENET_RX_FRSIZE);
  255. if (skbn)
  256. skb_align(skbn, ENET_RX_ALIGN);
  257. }
  258. if (skbn != NULL) {
  259. skb_put(skb, pkt_len); /* Make room */
  260. skb->protocol = eth_type_trans(skb, dev);
  261. received++;
  262. netif_rx(skb);
  263. } else {
  264. dev_warn(fep->dev,
  265. "Memory squeeze, dropping packet.\n");
  266. fep->stats.rx_dropped++;
  267. skbn = skb;
  268. }
  269. }
  270. fep->rx_skbuff[curidx] = skbn;
  271. CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
  272. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  273. DMA_FROM_DEVICE));
  274. CBDW_DATLEN(bdp, 0);
  275. CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
  276. /*
  277. * Update BD pointer to next entry.
  278. */
  279. if ((sc & BD_ENET_RX_WRAP) == 0)
  280. bdp++;
  281. else
  282. bdp = fep->rx_bd_base;
  283. (*fep->ops->rx_bd_done)(dev);
  284. }
  285. fep->cur_rx = bdp;
  286. return 0;
  287. }
  288. static void fs_enet_tx(struct net_device *dev)
  289. {
  290. struct fs_enet_private *fep = netdev_priv(dev);
  291. cbd_t __iomem *bdp;
  292. struct sk_buff *skb;
  293. int dirtyidx, do_wake, do_restart;
  294. u16 sc;
  295. spin_lock(&fep->tx_lock);
  296. bdp = fep->dirty_tx;
  297. do_wake = do_restart = 0;
  298. while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
  299. dirtyidx = bdp - fep->tx_bd_base;
  300. if (fep->tx_free == fep->tx_ring)
  301. break;
  302. skb = fep->tx_skbuff[dirtyidx];
  303. /*
  304. * Check for errors.
  305. */
  306. if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  307. BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
  308. if (sc & BD_ENET_TX_HB) /* No heartbeat */
  309. fep->stats.tx_heartbeat_errors++;
  310. if (sc & BD_ENET_TX_LC) /* Late collision */
  311. fep->stats.tx_window_errors++;
  312. if (sc & BD_ENET_TX_RL) /* Retrans limit */
  313. fep->stats.tx_aborted_errors++;
  314. if (sc & BD_ENET_TX_UN) /* Underrun */
  315. fep->stats.tx_fifo_errors++;
  316. if (sc & BD_ENET_TX_CSL) /* Carrier lost */
  317. fep->stats.tx_carrier_errors++;
  318. if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
  319. fep->stats.tx_errors++;
  320. do_restart = 1;
  321. }
  322. } else
  323. fep->stats.tx_packets++;
  324. if (sc & BD_ENET_TX_READY) {
  325. dev_warn(fep->dev,
  326. "HEY! Enet xmit interrupt and TX_READY.\n");
  327. }
  328. /*
  329. * Deferred means some collisions occurred during transmit,
  330. * but we eventually sent the packet OK.
  331. */
  332. if (sc & BD_ENET_TX_DEF)
  333. fep->stats.collisions++;
  334. /* unmap */
  335. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  336. skb->len, DMA_TO_DEVICE);
  337. /*
  338. * Free the sk buffer associated with this last transmit.
  339. */
  340. dev_kfree_skb_irq(skb);
  341. fep->tx_skbuff[dirtyidx] = NULL;
  342. /*
  343. * Update pointer to next buffer descriptor to be transmitted.
  344. */
  345. if ((sc & BD_ENET_TX_WRAP) == 0)
  346. bdp++;
  347. else
  348. bdp = fep->tx_bd_base;
  349. /*
  350. * Since we have freed up a buffer, the ring is no longer
  351. * full.
  352. */
  353. if (!fep->tx_free++)
  354. do_wake = 1;
  355. }
  356. fep->dirty_tx = bdp;
  357. if (do_restart)
  358. (*fep->ops->tx_restart)(dev);
  359. spin_unlock(&fep->tx_lock);
  360. if (do_wake)
  361. netif_wake_queue(dev);
  362. }
  363. /*
  364. * The interrupt handler.
  365. * This is called from the MPC core interrupt.
  366. */
  367. static irqreturn_t
  368. fs_enet_interrupt(int irq, void *dev_id)
  369. {
  370. struct net_device *dev = dev_id;
  371. struct fs_enet_private *fep;
  372. const struct fs_platform_info *fpi;
  373. u32 int_events;
  374. u32 int_clr_events;
  375. int nr, napi_ok;
  376. int handled;
  377. fep = netdev_priv(dev);
  378. fpi = fep->fpi;
  379. nr = 0;
  380. while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
  381. nr++;
  382. int_clr_events = int_events;
  383. if (fpi->use_napi)
  384. int_clr_events &= ~fep->ev_napi_rx;
  385. (*fep->ops->clear_int_events)(dev, int_clr_events);
  386. if (int_events & fep->ev_err)
  387. (*fep->ops->ev_error)(dev, int_events);
  388. if (int_events & fep->ev_rx) {
  389. if (!fpi->use_napi)
  390. fs_enet_rx_non_napi(dev);
  391. else {
  392. napi_ok = napi_schedule_prep(&fep->napi);
  393. (*fep->ops->napi_disable_rx)(dev);
  394. (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
  395. /* NOTE: it is possible for FCCs in NAPI mode */
  396. /* to submit a spurious interrupt while in poll */
  397. if (napi_ok)
  398. __napi_schedule(&fep->napi);
  399. }
  400. }
  401. if (int_events & fep->ev_tx)
  402. fs_enet_tx(dev);
  403. }
  404. handled = nr > 0;
  405. return IRQ_RETVAL(handled);
  406. }
  407. void fs_init_bds(struct net_device *dev)
  408. {
  409. struct fs_enet_private *fep = netdev_priv(dev);
  410. cbd_t __iomem *bdp;
  411. struct sk_buff *skb;
  412. int i;
  413. fs_cleanup_bds(dev);
  414. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  415. fep->tx_free = fep->tx_ring;
  416. fep->cur_rx = fep->rx_bd_base;
  417. /*
  418. * Initialize the receive buffer descriptors.
  419. */
  420. for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
  421. skb = dev_alloc_skb(ENET_RX_FRSIZE);
  422. if (skb == NULL) {
  423. dev_warn(fep->dev,
  424. "Memory squeeze, unable to allocate skb\n");
  425. break;
  426. }
  427. skb_align(skb, ENET_RX_ALIGN);
  428. fep->rx_skbuff[i] = skb;
  429. CBDW_BUFADDR(bdp,
  430. dma_map_single(fep->dev, skb->data,
  431. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  432. DMA_FROM_DEVICE));
  433. CBDW_DATLEN(bdp, 0); /* zero */
  434. CBDW_SC(bdp, BD_ENET_RX_EMPTY |
  435. ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
  436. }
  437. /*
  438. * if we failed, fillup remainder
  439. */
  440. for (; i < fep->rx_ring; i++, bdp++) {
  441. fep->rx_skbuff[i] = NULL;
  442. CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
  443. }
  444. /*
  445. * ...and the same for transmit.
  446. */
  447. for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
  448. fep->tx_skbuff[i] = NULL;
  449. CBDW_BUFADDR(bdp, 0);
  450. CBDW_DATLEN(bdp, 0);
  451. CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
  452. }
  453. }
  454. void fs_cleanup_bds(struct net_device *dev)
  455. {
  456. struct fs_enet_private *fep = netdev_priv(dev);
  457. struct sk_buff *skb;
  458. cbd_t __iomem *bdp;
  459. int i;
  460. /*
  461. * Reset SKB transmit buffers.
  462. */
  463. for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
  464. if ((skb = fep->tx_skbuff[i]) == NULL)
  465. continue;
  466. /* unmap */
  467. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  468. skb->len, DMA_TO_DEVICE);
  469. fep->tx_skbuff[i] = NULL;
  470. dev_kfree_skb(skb);
  471. }
  472. /*
  473. * Reset SKB receive buffers
  474. */
  475. for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
  476. if ((skb = fep->rx_skbuff[i]) == NULL)
  477. continue;
  478. /* unmap */
  479. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  480. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  481. DMA_FROM_DEVICE);
  482. fep->rx_skbuff[i] = NULL;
  483. dev_kfree_skb(skb);
  484. }
  485. }
  486. /**********************************************************************************/
  487. static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  488. {
  489. struct fs_enet_private *fep = netdev_priv(dev);
  490. cbd_t __iomem *bdp;
  491. int curidx;
  492. u16 sc;
  493. unsigned long flags;
  494. spin_lock_irqsave(&fep->tx_lock, flags);
  495. /*
  496. * Fill in a Tx ring entry
  497. */
  498. bdp = fep->cur_tx;
  499. if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
  500. netif_stop_queue(dev);
  501. spin_unlock_irqrestore(&fep->tx_lock, flags);
  502. /*
  503. * Ooops. All transmit buffers are full. Bail out.
  504. * This should not happen, since the tx queue should be stopped.
  505. */
  506. dev_warn(fep->dev, "tx queue full!.\n");
  507. return NETDEV_TX_BUSY;
  508. }
  509. curidx = bdp - fep->tx_bd_base;
  510. /*
  511. * Clear all of the status flags.
  512. */
  513. CBDC_SC(bdp, BD_ENET_TX_STATS);
  514. /*
  515. * Save skb pointer.
  516. */
  517. fep->tx_skbuff[curidx] = skb;
  518. fep->stats.tx_bytes += skb->len;
  519. /*
  520. * Push the data cache so the CPM does not get stale memory data.
  521. */
  522. CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
  523. skb->data, skb->len, DMA_TO_DEVICE));
  524. CBDW_DATLEN(bdp, skb->len);
  525. dev->trans_start = jiffies;
  526. /*
  527. * If this was the last BD in the ring, start at the beginning again.
  528. */
  529. if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
  530. fep->cur_tx++;
  531. else
  532. fep->cur_tx = fep->tx_bd_base;
  533. if (!--fep->tx_free)
  534. netif_stop_queue(dev);
  535. /* Trigger transmission start */
  536. sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
  537. BD_ENET_TX_LAST | BD_ENET_TX_TC;
  538. /* note that while FEC does not have this bit
  539. * it marks it as available for software use
  540. * yay for hw reuse :) */
  541. if (skb->len <= 60)
  542. sc |= BD_ENET_TX_PAD;
  543. CBDS_SC(bdp, sc);
  544. (*fep->ops->tx_kickstart)(dev);
  545. spin_unlock_irqrestore(&fep->tx_lock, flags);
  546. return NETDEV_TX_OK;
  547. }
  548. static void fs_timeout(struct net_device *dev)
  549. {
  550. struct fs_enet_private *fep = netdev_priv(dev);
  551. unsigned long flags;
  552. int wake = 0;
  553. fep->stats.tx_errors++;
  554. spin_lock_irqsave(&fep->lock, flags);
  555. if (dev->flags & IFF_UP) {
  556. phy_stop(fep->phydev);
  557. (*fep->ops->stop)(dev);
  558. (*fep->ops->restart)(dev);
  559. phy_start(fep->phydev);
  560. }
  561. phy_start(fep->phydev);
  562. wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
  563. spin_unlock_irqrestore(&fep->lock, flags);
  564. if (wake)
  565. netif_wake_queue(dev);
  566. }
  567. /*-----------------------------------------------------------------------------
  568. * generic link-change handler - should be sufficient for most cases
  569. *-----------------------------------------------------------------------------*/
  570. static void generic_adjust_link(struct net_device *dev)
  571. {
  572. struct fs_enet_private *fep = netdev_priv(dev);
  573. struct phy_device *phydev = fep->phydev;
  574. int new_state = 0;
  575. if (phydev->link) {
  576. /* adjust to duplex mode */
  577. if (phydev->duplex != fep->oldduplex) {
  578. new_state = 1;
  579. fep->oldduplex = phydev->duplex;
  580. }
  581. if (phydev->speed != fep->oldspeed) {
  582. new_state = 1;
  583. fep->oldspeed = phydev->speed;
  584. }
  585. if (!fep->oldlink) {
  586. new_state = 1;
  587. fep->oldlink = 1;
  588. }
  589. if (new_state)
  590. fep->ops->restart(dev);
  591. } else if (fep->oldlink) {
  592. new_state = 1;
  593. fep->oldlink = 0;
  594. fep->oldspeed = 0;
  595. fep->oldduplex = -1;
  596. }
  597. if (new_state && netif_msg_link(fep))
  598. phy_print_status(phydev);
  599. }
  600. static void fs_adjust_link(struct net_device *dev)
  601. {
  602. struct fs_enet_private *fep = netdev_priv(dev);
  603. unsigned long flags;
  604. spin_lock_irqsave(&fep->lock, flags);
  605. if(fep->ops->adjust_link)
  606. fep->ops->adjust_link(dev);
  607. else
  608. generic_adjust_link(dev);
  609. spin_unlock_irqrestore(&fep->lock, flags);
  610. }
  611. static int fs_init_phy(struct net_device *dev)
  612. {
  613. struct fs_enet_private *fep = netdev_priv(dev);
  614. struct phy_device *phydev;
  615. fep->oldlink = 0;
  616. fep->oldspeed = 0;
  617. fep->oldduplex = -1;
  618. phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0,
  619. PHY_INTERFACE_MODE_MII);
  620. if (!phydev) {
  621. phydev = of_phy_connect_fixed_link(dev, &fs_adjust_link,
  622. PHY_INTERFACE_MODE_MII);
  623. }
  624. if (!phydev) {
  625. dev_err(&dev->dev, "Could not attach to PHY\n");
  626. return -ENODEV;
  627. }
  628. fep->phydev = phydev;
  629. return 0;
  630. }
  631. static int fs_enet_open(struct net_device *dev)
  632. {
  633. struct fs_enet_private *fep = netdev_priv(dev);
  634. int r;
  635. int err;
  636. /* to initialize the fep->cur_rx,... */
  637. /* not doing this, will cause a crash in fs_enet_rx_napi */
  638. fs_init_bds(fep->ndev);
  639. if (fep->fpi->use_napi)
  640. napi_enable(&fep->napi);
  641. /* Install our interrupt handler. */
  642. r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
  643. "fs_enet-mac", dev);
  644. if (r != 0) {
  645. dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
  646. if (fep->fpi->use_napi)
  647. napi_disable(&fep->napi);
  648. return -EINVAL;
  649. }
  650. err = fs_init_phy(dev);
  651. if (err) {
  652. free_irq(fep->interrupt, dev);
  653. if (fep->fpi->use_napi)
  654. napi_disable(&fep->napi);
  655. return err;
  656. }
  657. phy_start(fep->phydev);
  658. netif_start_queue(dev);
  659. return 0;
  660. }
  661. static int fs_enet_close(struct net_device *dev)
  662. {
  663. struct fs_enet_private *fep = netdev_priv(dev);
  664. unsigned long flags;
  665. netif_stop_queue(dev);
  666. netif_carrier_off(dev);
  667. if (fep->fpi->use_napi)
  668. napi_disable(&fep->napi);
  669. phy_stop(fep->phydev);
  670. spin_lock_irqsave(&fep->lock, flags);
  671. spin_lock(&fep->tx_lock);
  672. (*fep->ops->stop)(dev);
  673. spin_unlock(&fep->tx_lock);
  674. spin_unlock_irqrestore(&fep->lock, flags);
  675. /* release any irqs */
  676. phy_disconnect(fep->phydev);
  677. fep->phydev = NULL;
  678. free_irq(fep->interrupt, dev);
  679. return 0;
  680. }
  681. static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
  682. {
  683. struct fs_enet_private *fep = netdev_priv(dev);
  684. return &fep->stats;
  685. }
  686. /*************************************************************************/
  687. static void fs_get_drvinfo(struct net_device *dev,
  688. struct ethtool_drvinfo *info)
  689. {
  690. strcpy(info->driver, DRV_MODULE_NAME);
  691. strcpy(info->version, DRV_MODULE_VERSION);
  692. }
  693. static int fs_get_regs_len(struct net_device *dev)
  694. {
  695. struct fs_enet_private *fep = netdev_priv(dev);
  696. return (*fep->ops->get_regs_len)(dev);
  697. }
  698. static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  699. void *p)
  700. {
  701. struct fs_enet_private *fep = netdev_priv(dev);
  702. unsigned long flags;
  703. int r, len;
  704. len = regs->len;
  705. spin_lock_irqsave(&fep->lock, flags);
  706. r = (*fep->ops->get_regs)(dev, p, &len);
  707. spin_unlock_irqrestore(&fep->lock, flags);
  708. if (r == 0)
  709. regs->version = 0;
  710. }
  711. static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  712. {
  713. struct fs_enet_private *fep = netdev_priv(dev);
  714. if (!fep->phydev)
  715. return -ENODEV;
  716. return phy_ethtool_gset(fep->phydev, cmd);
  717. }
  718. static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  719. {
  720. struct fs_enet_private *fep = netdev_priv(dev);
  721. if (!fep->phydev)
  722. return -ENODEV;
  723. return phy_ethtool_sset(fep->phydev, cmd);
  724. }
  725. static int fs_nway_reset(struct net_device *dev)
  726. {
  727. return 0;
  728. }
  729. static u32 fs_get_msglevel(struct net_device *dev)
  730. {
  731. struct fs_enet_private *fep = netdev_priv(dev);
  732. return fep->msg_enable;
  733. }
  734. static void fs_set_msglevel(struct net_device *dev, u32 value)
  735. {
  736. struct fs_enet_private *fep = netdev_priv(dev);
  737. fep->msg_enable = value;
  738. }
  739. static const struct ethtool_ops fs_ethtool_ops = {
  740. .get_drvinfo = fs_get_drvinfo,
  741. .get_regs_len = fs_get_regs_len,
  742. .get_settings = fs_get_settings,
  743. .set_settings = fs_set_settings,
  744. .nway_reset = fs_nway_reset,
  745. .get_link = ethtool_op_get_link,
  746. .get_msglevel = fs_get_msglevel,
  747. .set_msglevel = fs_set_msglevel,
  748. .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
  749. .set_sg = ethtool_op_set_sg,
  750. .get_regs = fs_get_regs,
  751. };
  752. static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  753. {
  754. struct fs_enet_private *fep = netdev_priv(dev);
  755. struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
  756. if (!netif_running(dev))
  757. return -EINVAL;
  758. return phy_mii_ioctl(fep->phydev, mii, cmd);
  759. }
  760. extern int fs_mii_connect(struct net_device *dev);
  761. extern void fs_mii_disconnect(struct net_device *dev);
  762. /**************************************************************************************/
  763. #ifdef CONFIG_FS_ENET_HAS_FEC
  764. #define IS_FEC(match) ((match)->data == &fs_fec_ops)
  765. #else
  766. #define IS_FEC(match) 0
  767. #endif
  768. static const struct net_device_ops fs_enet_netdev_ops = {
  769. .ndo_open = fs_enet_open,
  770. .ndo_stop = fs_enet_close,
  771. .ndo_get_stats = fs_enet_get_stats,
  772. .ndo_start_xmit = fs_enet_start_xmit,
  773. .ndo_tx_timeout = fs_timeout,
  774. .ndo_set_multicast_list = fs_set_multicast_list,
  775. .ndo_do_ioctl = fs_ioctl,
  776. .ndo_validate_addr = eth_validate_addr,
  777. .ndo_set_mac_address = eth_mac_addr,
  778. .ndo_change_mtu = eth_change_mtu,
  779. #ifdef CONFIG_NET_POLL_CONTROLLER
  780. .ndo_poll_controller = fs_enet_netpoll,
  781. #endif
  782. };
  783. static int __devinit fs_enet_probe(struct of_device *ofdev,
  784. const struct of_device_id *match)
  785. {
  786. struct net_device *ndev;
  787. struct fs_enet_private *fep;
  788. struct fs_platform_info *fpi;
  789. const u32 *data;
  790. const u8 *mac_addr;
  791. int privsize, len, ret = -ENODEV;
  792. fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
  793. if (!fpi)
  794. return -ENOMEM;
  795. if (!IS_FEC(match)) {
  796. data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
  797. if (!data || len != 4)
  798. goto out_free_fpi;
  799. fpi->cp_command = *data;
  800. }
  801. fpi->rx_ring = 32;
  802. fpi->tx_ring = 32;
  803. fpi->rx_copybreak = 240;
  804. fpi->use_napi = 1;
  805. fpi->napi_weight = 17;
  806. fpi->phy_node = of_parse_phandle(ofdev->node, "phy-handle", 0);
  807. if ((!fpi->phy_node) && (!of_get_property(ofdev->node, "fixed-link",
  808. NULL)))
  809. goto out_free_fpi;
  810. privsize = sizeof(*fep) +
  811. sizeof(struct sk_buff **) *
  812. (fpi->rx_ring + fpi->tx_ring);
  813. ndev = alloc_etherdev(privsize);
  814. if (!ndev) {
  815. ret = -ENOMEM;
  816. goto out_free_fpi;
  817. }
  818. SET_NETDEV_DEV(ndev, &ofdev->dev);
  819. dev_set_drvdata(&ofdev->dev, ndev);
  820. fep = netdev_priv(ndev);
  821. fep->dev = &ofdev->dev;
  822. fep->ndev = ndev;
  823. fep->fpi = fpi;
  824. fep->ops = match->data;
  825. ret = fep->ops->setup_data(ndev);
  826. if (ret)
  827. goto out_free_dev;
  828. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  829. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  830. spin_lock_init(&fep->lock);
  831. spin_lock_init(&fep->tx_lock);
  832. mac_addr = of_get_mac_address(ofdev->node);
  833. if (mac_addr)
  834. memcpy(ndev->dev_addr, mac_addr, 6);
  835. ret = fep->ops->allocate_bd(ndev);
  836. if (ret)
  837. goto out_cleanup_data;
  838. fep->rx_bd_base = fep->ring_base;
  839. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  840. fep->tx_ring = fpi->tx_ring;
  841. fep->rx_ring = fpi->rx_ring;
  842. ndev->netdev_ops = &fs_enet_netdev_ops;
  843. ndev->watchdog_timeo = 2 * HZ;
  844. if (fpi->use_napi)
  845. netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
  846. fpi->napi_weight);
  847. ndev->ethtool_ops = &fs_ethtool_ops;
  848. init_timer(&fep->phy_timer_list);
  849. netif_carrier_off(ndev);
  850. ret = register_netdev(ndev);
  851. if (ret)
  852. goto out_free_bd;
  853. pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
  854. return 0;
  855. out_free_bd:
  856. fep->ops->free_bd(ndev);
  857. out_cleanup_data:
  858. fep->ops->cleanup_data(ndev);
  859. out_free_dev:
  860. free_netdev(ndev);
  861. dev_set_drvdata(&ofdev->dev, NULL);
  862. of_node_put(fpi->phy_node);
  863. out_free_fpi:
  864. kfree(fpi);
  865. return ret;
  866. }
  867. static int fs_enet_remove(struct of_device *ofdev)
  868. {
  869. struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
  870. struct fs_enet_private *fep = netdev_priv(ndev);
  871. unregister_netdev(ndev);
  872. fep->ops->free_bd(ndev);
  873. fep->ops->cleanup_data(ndev);
  874. dev_set_drvdata(fep->dev, NULL);
  875. of_node_put(fep->fpi->phy_node);
  876. free_netdev(ndev);
  877. return 0;
  878. }
  879. static struct of_device_id fs_enet_match[] = {
  880. #ifdef CONFIG_FS_ENET_HAS_SCC
  881. {
  882. .compatible = "fsl,cpm1-scc-enet",
  883. .data = (void *)&fs_scc_ops,
  884. },
  885. {
  886. .compatible = "fsl,cpm2-scc-enet",
  887. .data = (void *)&fs_scc_ops,
  888. },
  889. #endif
  890. #ifdef CONFIG_FS_ENET_HAS_FCC
  891. {
  892. .compatible = "fsl,cpm2-fcc-enet",
  893. .data = (void *)&fs_fcc_ops,
  894. },
  895. #endif
  896. #ifdef CONFIG_FS_ENET_HAS_FEC
  897. {
  898. .compatible = "fsl,pq1-fec-enet",
  899. .data = (void *)&fs_fec_ops,
  900. },
  901. #endif
  902. {}
  903. };
  904. MODULE_DEVICE_TABLE(of, fs_enet_match);
  905. static struct of_platform_driver fs_enet_driver = {
  906. .name = "fs_enet",
  907. .match_table = fs_enet_match,
  908. .probe = fs_enet_probe,
  909. .remove = fs_enet_remove,
  910. };
  911. static int __init fs_init(void)
  912. {
  913. return of_register_platform_driver(&fs_enet_driver);
  914. }
  915. static void __exit fs_cleanup(void)
  916. {
  917. of_unregister_platform_driver(&fs_enet_driver);
  918. }
  919. #ifdef CONFIG_NET_POLL_CONTROLLER
  920. static void fs_enet_netpoll(struct net_device *dev)
  921. {
  922. disable_irq(dev->irq);
  923. fs_enet_interrupt(dev->irq, dev);
  924. enable_irq(dev->irq);
  925. }
  926. #endif
  927. /**************************************************************************************/
  928. module_init(fs_init);
  929. module_exit(fs_cleanup);