fs_enet-main.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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 platform_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_put;
  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. out_put:
  902. of_node_put(fpi->phy_node);
  903. out_free_fpi:
  904. kfree(fpi);
  905. return ret;
  906. }
  907. static int fs_enet_remove(struct platform_device *ofdev)
  908. {
  909. struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
  910. struct fs_enet_private *fep = netdev_priv(ndev);
  911. unregister_netdev(ndev);
  912. fep->ops->free_bd(ndev);
  913. fep->ops->cleanup_data(ndev);
  914. dev_set_drvdata(fep->dev, NULL);
  915. of_node_put(fep->fpi->phy_node);
  916. free_netdev(ndev);
  917. return 0;
  918. }
  919. static struct of_device_id fs_enet_match[] = {
  920. #ifdef CONFIG_FS_ENET_HAS_SCC
  921. {
  922. .compatible = "fsl,cpm1-scc-enet",
  923. .data = (void *)&fs_scc_ops,
  924. },
  925. {
  926. .compatible = "fsl,cpm2-scc-enet",
  927. .data = (void *)&fs_scc_ops,
  928. },
  929. #endif
  930. #ifdef CONFIG_FS_ENET_HAS_FCC
  931. {
  932. .compatible = "fsl,cpm2-fcc-enet",
  933. .data = (void *)&fs_fcc_ops,
  934. },
  935. #endif
  936. #ifdef CONFIG_FS_ENET_HAS_FEC
  937. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  938. {
  939. .compatible = "fsl,mpc5121-fec",
  940. .data = (void *)&fs_fec_ops,
  941. },
  942. #else
  943. {
  944. .compatible = "fsl,pq1-fec-enet",
  945. .data = (void *)&fs_fec_ops,
  946. },
  947. #endif
  948. #endif
  949. {}
  950. };
  951. MODULE_DEVICE_TABLE(of, fs_enet_match);
  952. static struct of_platform_driver fs_enet_driver = {
  953. .driver = {
  954. .owner = THIS_MODULE,
  955. .name = "fs_enet",
  956. .of_match_table = fs_enet_match,
  957. },
  958. .probe = fs_enet_probe,
  959. .remove = fs_enet_remove,
  960. };
  961. static int __init fs_init(void)
  962. {
  963. return of_register_platform_driver(&fs_enet_driver);
  964. }
  965. static void __exit fs_cleanup(void)
  966. {
  967. of_unregister_platform_driver(&fs_enet_driver);
  968. }
  969. #ifdef CONFIG_NET_POLL_CONTROLLER
  970. static void fs_enet_netpoll(struct net_device *dev)
  971. {
  972. disable_irq(dev->irq);
  973. fs_enet_interrupt(dev->irq, dev);
  974. enable_irq(dev->irq);
  975. }
  976. #endif
  977. /**************************************************************************************/
  978. module_init(fs_init);
  979. module_exit(fs_cleanup);