fs_enet-main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  488. /*
  489. * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
  490. */
  491. static struct sk_buff *tx_skb_align_workaround(struct net_device *dev,
  492. struct sk_buff *skb)
  493. {
  494. struct sk_buff *new_skb;
  495. struct fs_enet_private *fep = netdev_priv(dev);
  496. /* Alloc new skb */
  497. new_skb = dev_alloc_skb(skb->len + 4);
  498. if (!new_skb) {
  499. if (net_ratelimit()) {
  500. dev_warn(fep->dev,
  501. "Memory squeeze, dropping tx packet.\n");
  502. }
  503. return NULL;
  504. }
  505. /* Make sure new skb is properly aligned */
  506. skb_align(new_skb, 4);
  507. /* Copy data to new skb ... */
  508. skb_copy_from_linear_data(skb, new_skb->data, skb->len);
  509. skb_put(new_skb, skb->len);
  510. /* ... and free an old one */
  511. dev_kfree_skb_any(skb);
  512. return new_skb;
  513. }
  514. #endif
  515. static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  516. {
  517. struct fs_enet_private *fep = netdev_priv(dev);
  518. cbd_t __iomem *bdp;
  519. int curidx;
  520. u16 sc;
  521. unsigned long flags;
  522. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  523. if (((unsigned long)skb->data) & 0x3) {
  524. skb = tx_skb_align_workaround(dev, skb);
  525. if (!skb) {
  526. /*
  527. * We have lost packet due to memory allocation error
  528. * in tx_skb_align_workaround(). Hopefully original
  529. * skb is still valid, so try transmit it later.
  530. */
  531. return NETDEV_TX_BUSY;
  532. }
  533. }
  534. #endif
  535. spin_lock_irqsave(&fep->tx_lock, flags);
  536. /*
  537. * Fill in a Tx ring entry
  538. */
  539. bdp = fep->cur_tx;
  540. if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
  541. netif_stop_queue(dev);
  542. spin_unlock_irqrestore(&fep->tx_lock, flags);
  543. /*
  544. * Ooops. All transmit buffers are full. Bail out.
  545. * This should not happen, since the tx queue should be stopped.
  546. */
  547. dev_warn(fep->dev, "tx queue full!.\n");
  548. return NETDEV_TX_BUSY;
  549. }
  550. curidx = bdp - fep->tx_bd_base;
  551. /*
  552. * Clear all of the status flags.
  553. */
  554. CBDC_SC(bdp, BD_ENET_TX_STATS);
  555. /*
  556. * Save skb pointer.
  557. */
  558. fep->tx_skbuff[curidx] = skb;
  559. fep->stats.tx_bytes += skb->len;
  560. /*
  561. * Push the data cache so the CPM does not get stale memory data.
  562. */
  563. CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
  564. skb->data, skb->len, DMA_TO_DEVICE));
  565. CBDW_DATLEN(bdp, skb->len);
  566. /*
  567. * If this was the last BD in the ring, start at the beginning again.
  568. */
  569. if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
  570. fep->cur_tx++;
  571. else
  572. fep->cur_tx = fep->tx_bd_base;
  573. if (!--fep->tx_free)
  574. netif_stop_queue(dev);
  575. /* Trigger transmission start */
  576. sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
  577. BD_ENET_TX_LAST | BD_ENET_TX_TC;
  578. /* note that while FEC does not have this bit
  579. * it marks it as available for software use
  580. * yay for hw reuse :) */
  581. if (skb->len <= 60)
  582. sc |= BD_ENET_TX_PAD;
  583. CBDS_SC(bdp, sc);
  584. (*fep->ops->tx_kickstart)(dev);
  585. spin_unlock_irqrestore(&fep->tx_lock, flags);
  586. return NETDEV_TX_OK;
  587. }
  588. static void fs_timeout(struct net_device *dev)
  589. {
  590. struct fs_enet_private *fep = netdev_priv(dev);
  591. unsigned long flags;
  592. int wake = 0;
  593. fep->stats.tx_errors++;
  594. spin_lock_irqsave(&fep->lock, flags);
  595. if (dev->flags & IFF_UP) {
  596. phy_stop(fep->phydev);
  597. (*fep->ops->stop)(dev);
  598. (*fep->ops->restart)(dev);
  599. phy_start(fep->phydev);
  600. }
  601. phy_start(fep->phydev);
  602. wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
  603. spin_unlock_irqrestore(&fep->lock, flags);
  604. if (wake)
  605. netif_wake_queue(dev);
  606. }
  607. /*-----------------------------------------------------------------------------
  608. * generic link-change handler - should be sufficient for most cases
  609. *-----------------------------------------------------------------------------*/
  610. static void generic_adjust_link(struct net_device *dev)
  611. {
  612. struct fs_enet_private *fep = netdev_priv(dev);
  613. struct phy_device *phydev = fep->phydev;
  614. int new_state = 0;
  615. if (phydev->link) {
  616. /* adjust to duplex mode */
  617. if (phydev->duplex != fep->oldduplex) {
  618. new_state = 1;
  619. fep->oldduplex = phydev->duplex;
  620. }
  621. if (phydev->speed != fep->oldspeed) {
  622. new_state = 1;
  623. fep->oldspeed = phydev->speed;
  624. }
  625. if (!fep->oldlink) {
  626. new_state = 1;
  627. fep->oldlink = 1;
  628. }
  629. if (new_state)
  630. fep->ops->restart(dev);
  631. } else if (fep->oldlink) {
  632. new_state = 1;
  633. fep->oldlink = 0;
  634. fep->oldspeed = 0;
  635. fep->oldduplex = -1;
  636. }
  637. if (new_state && netif_msg_link(fep))
  638. phy_print_status(phydev);
  639. }
  640. static void fs_adjust_link(struct net_device *dev)
  641. {
  642. struct fs_enet_private *fep = netdev_priv(dev);
  643. unsigned long flags;
  644. spin_lock_irqsave(&fep->lock, flags);
  645. if(fep->ops->adjust_link)
  646. fep->ops->adjust_link(dev);
  647. else
  648. generic_adjust_link(dev);
  649. spin_unlock_irqrestore(&fep->lock, flags);
  650. }
  651. static int fs_init_phy(struct net_device *dev)
  652. {
  653. struct fs_enet_private *fep = netdev_priv(dev);
  654. struct phy_device *phydev;
  655. fep->oldlink = 0;
  656. fep->oldspeed = 0;
  657. fep->oldduplex = -1;
  658. phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0,
  659. PHY_INTERFACE_MODE_MII);
  660. if (!phydev) {
  661. phydev = of_phy_connect_fixed_link(dev, &fs_adjust_link,
  662. PHY_INTERFACE_MODE_MII);
  663. }
  664. if (!phydev) {
  665. dev_err(&dev->dev, "Could not attach to PHY\n");
  666. return -ENODEV;
  667. }
  668. fep->phydev = phydev;
  669. return 0;
  670. }
  671. static int fs_enet_open(struct net_device *dev)
  672. {
  673. struct fs_enet_private *fep = netdev_priv(dev);
  674. int r;
  675. int err;
  676. /* to initialize the fep->cur_rx,... */
  677. /* not doing this, will cause a crash in fs_enet_rx_napi */
  678. fs_init_bds(fep->ndev);
  679. if (fep->fpi->use_napi)
  680. napi_enable(&fep->napi);
  681. /* Install our interrupt handler. */
  682. r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
  683. "fs_enet-mac", dev);
  684. if (r != 0) {
  685. dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
  686. if (fep->fpi->use_napi)
  687. napi_disable(&fep->napi);
  688. return -EINVAL;
  689. }
  690. err = fs_init_phy(dev);
  691. if (err) {
  692. free_irq(fep->interrupt, dev);
  693. if (fep->fpi->use_napi)
  694. napi_disable(&fep->napi);
  695. return err;
  696. }
  697. phy_start(fep->phydev);
  698. netif_start_queue(dev);
  699. return 0;
  700. }
  701. static int fs_enet_close(struct net_device *dev)
  702. {
  703. struct fs_enet_private *fep = netdev_priv(dev);
  704. unsigned long flags;
  705. netif_stop_queue(dev);
  706. netif_carrier_off(dev);
  707. if (fep->fpi->use_napi)
  708. napi_disable(&fep->napi);
  709. phy_stop(fep->phydev);
  710. spin_lock_irqsave(&fep->lock, flags);
  711. spin_lock(&fep->tx_lock);
  712. (*fep->ops->stop)(dev);
  713. spin_unlock(&fep->tx_lock);
  714. spin_unlock_irqrestore(&fep->lock, flags);
  715. /* release any irqs */
  716. phy_disconnect(fep->phydev);
  717. fep->phydev = NULL;
  718. free_irq(fep->interrupt, dev);
  719. return 0;
  720. }
  721. static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
  722. {
  723. struct fs_enet_private *fep = netdev_priv(dev);
  724. return &fep->stats;
  725. }
  726. /*************************************************************************/
  727. static void fs_get_drvinfo(struct net_device *dev,
  728. struct ethtool_drvinfo *info)
  729. {
  730. strcpy(info->driver, DRV_MODULE_NAME);
  731. strcpy(info->version, DRV_MODULE_VERSION);
  732. }
  733. static int fs_get_regs_len(struct net_device *dev)
  734. {
  735. struct fs_enet_private *fep = netdev_priv(dev);
  736. return (*fep->ops->get_regs_len)(dev);
  737. }
  738. static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  739. void *p)
  740. {
  741. struct fs_enet_private *fep = netdev_priv(dev);
  742. unsigned long flags;
  743. int r, len;
  744. len = regs->len;
  745. spin_lock_irqsave(&fep->lock, flags);
  746. r = (*fep->ops->get_regs)(dev, p, &len);
  747. spin_unlock_irqrestore(&fep->lock, flags);
  748. if (r == 0)
  749. regs->version = 0;
  750. }
  751. static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  752. {
  753. struct fs_enet_private *fep = netdev_priv(dev);
  754. if (!fep->phydev)
  755. return -ENODEV;
  756. return phy_ethtool_gset(fep->phydev, cmd);
  757. }
  758. static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  759. {
  760. struct fs_enet_private *fep = netdev_priv(dev);
  761. if (!fep->phydev)
  762. return -ENODEV;
  763. return phy_ethtool_sset(fep->phydev, cmd);
  764. }
  765. static int fs_nway_reset(struct net_device *dev)
  766. {
  767. return 0;
  768. }
  769. static u32 fs_get_msglevel(struct net_device *dev)
  770. {
  771. struct fs_enet_private *fep = netdev_priv(dev);
  772. return fep->msg_enable;
  773. }
  774. static void fs_set_msglevel(struct net_device *dev, u32 value)
  775. {
  776. struct fs_enet_private *fep = netdev_priv(dev);
  777. fep->msg_enable = value;
  778. }
  779. static const struct ethtool_ops fs_ethtool_ops = {
  780. .get_drvinfo = fs_get_drvinfo,
  781. .get_regs_len = fs_get_regs_len,
  782. .get_settings = fs_get_settings,
  783. .set_settings = fs_set_settings,
  784. .nway_reset = fs_nway_reset,
  785. .get_link = ethtool_op_get_link,
  786. .get_msglevel = fs_get_msglevel,
  787. .set_msglevel = fs_set_msglevel,
  788. .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
  789. .set_sg = ethtool_op_set_sg,
  790. .get_regs = fs_get_regs,
  791. };
  792. static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  793. {
  794. struct fs_enet_private *fep = netdev_priv(dev);
  795. if (!netif_running(dev))
  796. return -EINVAL;
  797. return phy_mii_ioctl(fep->phydev, rq, cmd);
  798. }
  799. extern int fs_mii_connect(struct net_device *dev);
  800. extern void fs_mii_disconnect(struct net_device *dev);
  801. /**************************************************************************************/
  802. #ifdef CONFIG_FS_ENET_HAS_FEC
  803. #define IS_FEC(match) ((match)->data == &fs_fec_ops)
  804. #else
  805. #define IS_FEC(match) 0
  806. #endif
  807. static const struct net_device_ops fs_enet_netdev_ops = {
  808. .ndo_open = fs_enet_open,
  809. .ndo_stop = fs_enet_close,
  810. .ndo_get_stats = fs_enet_get_stats,
  811. .ndo_start_xmit = fs_enet_start_xmit,
  812. .ndo_tx_timeout = fs_timeout,
  813. .ndo_set_multicast_list = fs_set_multicast_list,
  814. .ndo_do_ioctl = fs_ioctl,
  815. .ndo_validate_addr = eth_validate_addr,
  816. .ndo_set_mac_address = eth_mac_addr,
  817. .ndo_change_mtu = eth_change_mtu,
  818. #ifdef CONFIG_NET_POLL_CONTROLLER
  819. .ndo_poll_controller = fs_enet_netpoll,
  820. #endif
  821. };
  822. static int __devinit fs_enet_probe(struct of_device *ofdev,
  823. const struct of_device_id *match)
  824. {
  825. struct net_device *ndev;
  826. struct fs_enet_private *fep;
  827. struct fs_platform_info *fpi;
  828. const u32 *data;
  829. const u8 *mac_addr;
  830. int privsize, len, ret = -ENODEV;
  831. fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
  832. if (!fpi)
  833. return -ENOMEM;
  834. if (!IS_FEC(match)) {
  835. data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
  836. if (!data || len != 4)
  837. goto out_free_fpi;
  838. fpi->cp_command = *data;
  839. }
  840. fpi->rx_ring = 32;
  841. fpi->tx_ring = 32;
  842. fpi->rx_copybreak = 240;
  843. fpi->use_napi = 1;
  844. fpi->napi_weight = 17;
  845. fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
  846. if ((!fpi->phy_node) && (!of_get_property(ofdev->dev.of_node, "fixed-link",
  847. NULL)))
  848. goto out_free_fpi;
  849. privsize = sizeof(*fep) +
  850. sizeof(struct sk_buff **) *
  851. (fpi->rx_ring + fpi->tx_ring);
  852. ndev = alloc_etherdev(privsize);
  853. if (!ndev) {
  854. ret = -ENOMEM;
  855. goto out_free_fpi;
  856. }
  857. SET_NETDEV_DEV(ndev, &ofdev->dev);
  858. dev_set_drvdata(&ofdev->dev, ndev);
  859. fep = netdev_priv(ndev);
  860. fep->dev = &ofdev->dev;
  861. fep->ndev = ndev;
  862. fep->fpi = fpi;
  863. fep->ops = match->data;
  864. ret = fep->ops->setup_data(ndev);
  865. if (ret)
  866. goto out_free_dev;
  867. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  868. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  869. spin_lock_init(&fep->lock);
  870. spin_lock_init(&fep->tx_lock);
  871. mac_addr = of_get_mac_address(ofdev->dev.of_node);
  872. if (mac_addr)
  873. memcpy(ndev->dev_addr, mac_addr, 6);
  874. ret = fep->ops->allocate_bd(ndev);
  875. if (ret)
  876. goto out_cleanup_data;
  877. fep->rx_bd_base = fep->ring_base;
  878. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  879. fep->tx_ring = fpi->tx_ring;
  880. fep->rx_ring = fpi->rx_ring;
  881. ndev->netdev_ops = &fs_enet_netdev_ops;
  882. ndev->watchdog_timeo = 2 * HZ;
  883. if (fpi->use_napi)
  884. netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
  885. fpi->napi_weight);
  886. ndev->ethtool_ops = &fs_ethtool_ops;
  887. init_timer(&fep->phy_timer_list);
  888. netif_carrier_off(ndev);
  889. ret = register_netdev(ndev);
  890. if (ret)
  891. goto out_free_bd;
  892. pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
  893. return 0;
  894. out_free_bd:
  895. fep->ops->free_bd(ndev);
  896. out_cleanup_data:
  897. fep->ops->cleanup_data(ndev);
  898. out_free_dev:
  899. free_netdev(ndev);
  900. dev_set_drvdata(&ofdev->dev, NULL);
  901. of_node_put(fpi->phy_node);
  902. out_free_fpi:
  903. kfree(fpi);
  904. return ret;
  905. }
  906. static int fs_enet_remove(struct of_device *ofdev)
  907. {
  908. struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
  909. struct fs_enet_private *fep = netdev_priv(ndev);
  910. unregister_netdev(ndev);
  911. fep->ops->free_bd(ndev);
  912. fep->ops->cleanup_data(ndev);
  913. dev_set_drvdata(fep->dev, NULL);
  914. of_node_put(fep->fpi->phy_node);
  915. free_netdev(ndev);
  916. return 0;
  917. }
  918. static struct of_device_id fs_enet_match[] = {
  919. #ifdef CONFIG_FS_ENET_HAS_SCC
  920. {
  921. .compatible = "fsl,cpm1-scc-enet",
  922. .data = (void *)&fs_scc_ops,
  923. },
  924. {
  925. .compatible = "fsl,cpm2-scc-enet",
  926. .data = (void *)&fs_scc_ops,
  927. },
  928. #endif
  929. #ifdef CONFIG_FS_ENET_HAS_FCC
  930. {
  931. .compatible = "fsl,cpm2-fcc-enet",
  932. .data = (void *)&fs_fcc_ops,
  933. },
  934. #endif
  935. #ifdef CONFIG_FS_ENET_HAS_FEC
  936. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  937. {
  938. .compatible = "fsl,mpc5121-fec",
  939. .data = (void *)&fs_fec_ops,
  940. },
  941. #else
  942. {
  943. .compatible = "fsl,pq1-fec-enet",
  944. .data = (void *)&fs_fec_ops,
  945. },
  946. #endif
  947. #endif
  948. {}
  949. };
  950. MODULE_DEVICE_TABLE(of, fs_enet_match);
  951. static struct of_platform_driver fs_enet_driver = {
  952. .driver = {
  953. .owner = THIS_MODULE,
  954. .name = "fs_enet",
  955. .of_match_table = fs_enet_match,
  956. },
  957. .probe = fs_enet_probe,
  958. .remove = fs_enet_remove,
  959. };
  960. static int __init fs_init(void)
  961. {
  962. return of_register_platform_driver(&fs_enet_driver);
  963. }
  964. static void __exit fs_cleanup(void)
  965. {
  966. of_unregister_platform_driver(&fs_enet_driver);
  967. }
  968. #ifdef CONFIG_NET_POLL_CONTROLLER
  969. static void fs_enet_netpoll(struct net_device *dev)
  970. {
  971. disable_irq(dev->irq);
  972. fs_enet_interrupt(dev->irq, dev);
  973. enable_irq(dev->irq);
  974. }
  975. #endif
  976. /**************************************************************************************/
  977. module_init(fs_init);
  978. module_exit(fs_cleanup);