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