gianfar_ethtool.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. /*
  2. * drivers/net/ethernet/freescale/gianfar_ethtool.c
  3. *
  4. * Gianfar Ethernet Driver
  5. * Ethtool support for Gianfar Enet
  6. * Based on e1000 ethtool support
  7. *
  8. * Author: Andy Fleming
  9. * Maintainer: Kumar Gala
  10. * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
  11. *
  12. * Copyright 2003-2006, 2008-2009, 2011 Freescale Semiconductor, Inc.
  13. *
  14. * This software may be used and distributed according to
  15. * the terms of the GNU Public License, Version 2, incorporated herein
  16. * by reference.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/string.h>
  21. #include <linux/errno.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/net_tstamp.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/mm.h>
  30. #include <asm/io.h>
  31. #include <asm/irq.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/module.h>
  34. #include <linux/crc32.h>
  35. #include <asm/types.h>
  36. #include <linux/ethtool.h>
  37. #include <linux/mii.h>
  38. #include <linux/phy.h>
  39. #include <linux/sort.h>
  40. #include <linux/if_vlan.h>
  41. #include <linux/of_platform.h>
  42. #include <linux/fsl/ptp_qoriq.h>
  43. #include "gianfar.h"
  44. #define GFAR_MAX_COAL_USECS 0xffff
  45. #define GFAR_MAX_COAL_FRAMES 0xff
  46. static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
  47. u64 *buf);
  48. static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
  49. static int gfar_gcoalesce(struct net_device *dev,
  50. struct ethtool_coalesce *cvals);
  51. static int gfar_scoalesce(struct net_device *dev,
  52. struct ethtool_coalesce *cvals);
  53. static void gfar_gringparam(struct net_device *dev,
  54. struct ethtool_ringparam *rvals);
  55. static int gfar_sringparam(struct net_device *dev,
  56. struct ethtool_ringparam *rvals);
  57. static void gfar_gdrvinfo(struct net_device *dev,
  58. struct ethtool_drvinfo *drvinfo);
  59. static const char stat_gstrings[][ETH_GSTRING_LEN] = {
  60. /* extra stats */
  61. "rx-allocation-errors",
  62. "rx-large-frame-errors",
  63. "rx-short-frame-errors",
  64. "rx-non-octet-errors",
  65. "rx-crc-errors",
  66. "rx-overrun-errors",
  67. "rx-busy-errors",
  68. "rx-babbling-errors",
  69. "rx-truncated-frames",
  70. "ethernet-bus-error",
  71. "tx-babbling-errors",
  72. "tx-underrun-errors",
  73. "tx-timeout-errors",
  74. /* rmon stats */
  75. "tx-rx-64-frames",
  76. "tx-rx-65-127-frames",
  77. "tx-rx-128-255-frames",
  78. "tx-rx-256-511-frames",
  79. "tx-rx-512-1023-frames",
  80. "tx-rx-1024-1518-frames",
  81. "tx-rx-1519-1522-good-vlan",
  82. "rx-bytes",
  83. "rx-packets",
  84. "rx-fcs-errors",
  85. "receive-multicast-packet",
  86. "receive-broadcast-packet",
  87. "rx-control-frame-packets",
  88. "rx-pause-frame-packets",
  89. "rx-unknown-op-code",
  90. "rx-alignment-error",
  91. "rx-frame-length-error",
  92. "rx-code-error",
  93. "rx-carrier-sense-error",
  94. "rx-undersize-packets",
  95. "rx-oversize-packets",
  96. "rx-fragmented-frames",
  97. "rx-jabber-frames",
  98. "rx-dropped-frames",
  99. "tx-byte-counter",
  100. "tx-packets",
  101. "tx-multicast-packets",
  102. "tx-broadcast-packets",
  103. "tx-pause-control-frames",
  104. "tx-deferral-packets",
  105. "tx-excessive-deferral-packets",
  106. "tx-single-collision-packets",
  107. "tx-multiple-collision-packets",
  108. "tx-late-collision-packets",
  109. "tx-excessive-collision-packets",
  110. "tx-total-collision",
  111. "reserved",
  112. "tx-dropped-frames",
  113. "tx-jabber-frames",
  114. "tx-fcs-errors",
  115. "tx-control-frames",
  116. "tx-oversize-frames",
  117. "tx-undersize-frames",
  118. "tx-fragmented-frames",
  119. };
  120. /* Fill in a buffer with the strings which correspond to the
  121. * stats */
  122. static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
  123. {
  124. struct gfar_private *priv = netdev_priv(dev);
  125. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
  126. memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
  127. else
  128. memcpy(buf, stat_gstrings,
  129. GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
  130. }
  131. /* Fill in an array of 64-bit statistics from various sources.
  132. * This array will be appended to the end of the ethtool_stats
  133. * structure, and returned to user space
  134. */
  135. static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
  136. u64 *buf)
  137. {
  138. int i;
  139. struct gfar_private *priv = netdev_priv(dev);
  140. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  141. atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
  142. for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
  143. buf[i] = atomic64_read(&extra[i]);
  144. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  145. u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
  146. for (; i < GFAR_STATS_LEN; i++, rmon++)
  147. buf[i] = (u64) gfar_read(rmon);
  148. }
  149. }
  150. static int gfar_sset_count(struct net_device *dev, int sset)
  151. {
  152. struct gfar_private *priv = netdev_priv(dev);
  153. switch (sset) {
  154. case ETH_SS_STATS:
  155. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
  156. return GFAR_STATS_LEN;
  157. else
  158. return GFAR_EXTRA_STATS_LEN;
  159. default:
  160. return -EOPNOTSUPP;
  161. }
  162. }
  163. /* Fills in the drvinfo structure with some basic info */
  164. static void gfar_gdrvinfo(struct net_device *dev,
  165. struct ethtool_drvinfo *drvinfo)
  166. {
  167. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  168. strlcpy(drvinfo->version, gfar_driver_version,
  169. sizeof(drvinfo->version));
  170. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  171. strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
  172. }
  173. /* Return the length of the register structure */
  174. static int gfar_reglen(struct net_device *dev)
  175. {
  176. return sizeof (struct gfar);
  177. }
  178. /* Return a dump of the GFAR register space */
  179. static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  180. void *regbuf)
  181. {
  182. int i;
  183. struct gfar_private *priv = netdev_priv(dev);
  184. u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
  185. u32 *buf = (u32 *) regbuf;
  186. for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
  187. buf[i] = gfar_read(&theregs[i]);
  188. }
  189. /* Convert microseconds to ethernet clock ticks, which changes
  190. * depending on what speed the controller is running at */
  191. static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
  192. unsigned int usecs)
  193. {
  194. struct net_device *ndev = priv->ndev;
  195. struct phy_device *phydev = ndev->phydev;
  196. unsigned int count;
  197. /* The timer is different, depending on the interface speed */
  198. switch (phydev->speed) {
  199. case SPEED_1000:
  200. count = GFAR_GBIT_TIME;
  201. break;
  202. case SPEED_100:
  203. count = GFAR_100_TIME;
  204. break;
  205. case SPEED_10:
  206. default:
  207. count = GFAR_10_TIME;
  208. break;
  209. }
  210. /* Make sure we return a number greater than 0
  211. * if usecs > 0 */
  212. return (usecs * 1000 + count - 1) / count;
  213. }
  214. /* Convert ethernet clock ticks to microseconds */
  215. static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
  216. unsigned int ticks)
  217. {
  218. struct net_device *ndev = priv->ndev;
  219. struct phy_device *phydev = ndev->phydev;
  220. unsigned int count;
  221. /* The timer is different, depending on the interface speed */
  222. switch (phydev->speed) {
  223. case SPEED_1000:
  224. count = GFAR_GBIT_TIME;
  225. break;
  226. case SPEED_100:
  227. count = GFAR_100_TIME;
  228. break;
  229. case SPEED_10:
  230. default:
  231. count = GFAR_10_TIME;
  232. break;
  233. }
  234. /* Make sure we return a number greater than 0 */
  235. /* if ticks is > 0 */
  236. return (ticks * count) / 1000;
  237. }
  238. /* Get the coalescing parameters, and put them in the cvals
  239. * structure. */
  240. static int gfar_gcoalesce(struct net_device *dev,
  241. struct ethtool_coalesce *cvals)
  242. {
  243. struct gfar_private *priv = netdev_priv(dev);
  244. struct gfar_priv_rx_q *rx_queue = NULL;
  245. struct gfar_priv_tx_q *tx_queue = NULL;
  246. unsigned long rxtime;
  247. unsigned long rxcount;
  248. unsigned long txtime;
  249. unsigned long txcount;
  250. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
  251. return -EOPNOTSUPP;
  252. if (!dev->phydev)
  253. return -ENODEV;
  254. rx_queue = priv->rx_queue[0];
  255. tx_queue = priv->tx_queue[0];
  256. rxtime = get_ictt_value(rx_queue->rxic);
  257. rxcount = get_icft_value(rx_queue->rxic);
  258. txtime = get_ictt_value(tx_queue->txic);
  259. txcount = get_icft_value(tx_queue->txic);
  260. cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
  261. cvals->rx_max_coalesced_frames = rxcount;
  262. cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
  263. cvals->tx_max_coalesced_frames = txcount;
  264. cvals->use_adaptive_rx_coalesce = 0;
  265. cvals->use_adaptive_tx_coalesce = 0;
  266. cvals->pkt_rate_low = 0;
  267. cvals->rx_coalesce_usecs_low = 0;
  268. cvals->rx_max_coalesced_frames_low = 0;
  269. cvals->tx_coalesce_usecs_low = 0;
  270. cvals->tx_max_coalesced_frames_low = 0;
  271. /* When the packet rate is below pkt_rate_high but above
  272. * pkt_rate_low (both measured in packets per second) the
  273. * normal {rx,tx}_* coalescing parameters are used.
  274. */
  275. /* When the packet rate is (measured in packets per second)
  276. * is above pkt_rate_high, the {rx,tx}_*_high parameters are
  277. * used.
  278. */
  279. cvals->pkt_rate_high = 0;
  280. cvals->rx_coalesce_usecs_high = 0;
  281. cvals->rx_max_coalesced_frames_high = 0;
  282. cvals->tx_coalesce_usecs_high = 0;
  283. cvals->tx_max_coalesced_frames_high = 0;
  284. /* How often to do adaptive coalescing packet rate sampling,
  285. * measured in seconds. Must not be zero.
  286. */
  287. cvals->rate_sample_interval = 0;
  288. return 0;
  289. }
  290. /* Change the coalescing values.
  291. * Both cvals->*_usecs and cvals->*_frames have to be > 0
  292. * in order for coalescing to be active
  293. */
  294. static int gfar_scoalesce(struct net_device *dev,
  295. struct ethtool_coalesce *cvals)
  296. {
  297. struct gfar_private *priv = netdev_priv(dev);
  298. int i, err = 0;
  299. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
  300. return -EOPNOTSUPP;
  301. if (!dev->phydev)
  302. return -ENODEV;
  303. /* Check the bounds of the values */
  304. if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
  305. netdev_info(dev, "Coalescing is limited to %d microseconds\n",
  306. GFAR_MAX_COAL_USECS);
  307. return -EINVAL;
  308. }
  309. if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
  310. netdev_info(dev, "Coalescing is limited to %d frames\n",
  311. GFAR_MAX_COAL_FRAMES);
  312. return -EINVAL;
  313. }
  314. /* Check the bounds of the values */
  315. if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
  316. netdev_info(dev, "Coalescing is limited to %d microseconds\n",
  317. GFAR_MAX_COAL_USECS);
  318. return -EINVAL;
  319. }
  320. if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
  321. netdev_info(dev, "Coalescing is limited to %d frames\n",
  322. GFAR_MAX_COAL_FRAMES);
  323. return -EINVAL;
  324. }
  325. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  326. cpu_relax();
  327. /* Set up rx coalescing */
  328. if ((cvals->rx_coalesce_usecs == 0) ||
  329. (cvals->rx_max_coalesced_frames == 0)) {
  330. for (i = 0; i < priv->num_rx_queues; i++)
  331. priv->rx_queue[i]->rxcoalescing = 0;
  332. } else {
  333. for (i = 0; i < priv->num_rx_queues; i++)
  334. priv->rx_queue[i]->rxcoalescing = 1;
  335. }
  336. for (i = 0; i < priv->num_rx_queues; i++) {
  337. priv->rx_queue[i]->rxic = mk_ic_value(
  338. cvals->rx_max_coalesced_frames,
  339. gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
  340. }
  341. /* Set up tx coalescing */
  342. if ((cvals->tx_coalesce_usecs == 0) ||
  343. (cvals->tx_max_coalesced_frames == 0)) {
  344. for (i = 0; i < priv->num_tx_queues; i++)
  345. priv->tx_queue[i]->txcoalescing = 0;
  346. } else {
  347. for (i = 0; i < priv->num_tx_queues; i++)
  348. priv->tx_queue[i]->txcoalescing = 1;
  349. }
  350. for (i = 0; i < priv->num_tx_queues; i++) {
  351. priv->tx_queue[i]->txic = mk_ic_value(
  352. cvals->tx_max_coalesced_frames,
  353. gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
  354. }
  355. if (dev->flags & IFF_UP) {
  356. stop_gfar(dev);
  357. err = startup_gfar(dev);
  358. } else {
  359. gfar_mac_reset(priv);
  360. }
  361. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  362. return err;
  363. }
  364. /* Fills in rvals with the current ring parameters. Currently,
  365. * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
  366. * jumbo are ignored by the driver */
  367. static void gfar_gringparam(struct net_device *dev,
  368. struct ethtool_ringparam *rvals)
  369. {
  370. struct gfar_private *priv = netdev_priv(dev);
  371. struct gfar_priv_tx_q *tx_queue = NULL;
  372. struct gfar_priv_rx_q *rx_queue = NULL;
  373. tx_queue = priv->tx_queue[0];
  374. rx_queue = priv->rx_queue[0];
  375. rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
  376. rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
  377. rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
  378. rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
  379. /* Values changeable by the user. The valid values are
  380. * in the range 1 to the "*_max_pending" counterpart above.
  381. */
  382. rvals->rx_pending = rx_queue->rx_ring_size;
  383. rvals->rx_mini_pending = rx_queue->rx_ring_size;
  384. rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
  385. rvals->tx_pending = tx_queue->tx_ring_size;
  386. }
  387. /* Change the current ring parameters, stopping the controller if
  388. * necessary so that we don't mess things up while we're in motion.
  389. */
  390. static int gfar_sringparam(struct net_device *dev,
  391. struct ethtool_ringparam *rvals)
  392. {
  393. struct gfar_private *priv = netdev_priv(dev);
  394. int err = 0, i;
  395. if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
  396. return -EINVAL;
  397. if (!is_power_of_2(rvals->rx_pending)) {
  398. netdev_err(dev, "Ring sizes must be a power of 2\n");
  399. return -EINVAL;
  400. }
  401. if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
  402. return -EINVAL;
  403. if (!is_power_of_2(rvals->tx_pending)) {
  404. netdev_err(dev, "Ring sizes must be a power of 2\n");
  405. return -EINVAL;
  406. }
  407. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  408. cpu_relax();
  409. if (dev->flags & IFF_UP)
  410. stop_gfar(dev);
  411. /* Change the sizes */
  412. for (i = 0; i < priv->num_rx_queues; i++)
  413. priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
  414. for (i = 0; i < priv->num_tx_queues; i++)
  415. priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
  416. /* Rebuild the rings with the new size */
  417. if (dev->flags & IFF_UP)
  418. err = startup_gfar(dev);
  419. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  420. return err;
  421. }
  422. static void gfar_gpauseparam(struct net_device *dev,
  423. struct ethtool_pauseparam *epause)
  424. {
  425. struct gfar_private *priv = netdev_priv(dev);
  426. epause->autoneg = !!priv->pause_aneg_en;
  427. epause->rx_pause = !!priv->rx_pause_en;
  428. epause->tx_pause = !!priv->tx_pause_en;
  429. }
  430. static int gfar_spauseparam(struct net_device *dev,
  431. struct ethtool_pauseparam *epause)
  432. {
  433. struct gfar_private *priv = netdev_priv(dev);
  434. struct phy_device *phydev = dev->phydev;
  435. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  436. u32 oldadv, newadv;
  437. if (!phydev)
  438. return -ENODEV;
  439. if (!(phydev->supported & SUPPORTED_Pause) ||
  440. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  441. (epause->rx_pause != epause->tx_pause)))
  442. return -EINVAL;
  443. priv->rx_pause_en = priv->tx_pause_en = 0;
  444. if (epause->rx_pause) {
  445. priv->rx_pause_en = 1;
  446. if (epause->tx_pause) {
  447. priv->tx_pause_en = 1;
  448. /* FLOW_CTRL_RX & TX */
  449. newadv = ADVERTISED_Pause;
  450. } else /* FLOW_CTLR_RX */
  451. newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
  452. } else if (epause->tx_pause) {
  453. priv->tx_pause_en = 1;
  454. /* FLOW_CTLR_TX */
  455. newadv = ADVERTISED_Asym_Pause;
  456. } else
  457. newadv = 0;
  458. if (epause->autoneg)
  459. priv->pause_aneg_en = 1;
  460. else
  461. priv->pause_aneg_en = 0;
  462. oldadv = phydev->advertising &
  463. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  464. if (oldadv != newadv) {
  465. phydev->advertising &=
  466. ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  467. phydev->advertising |= newadv;
  468. if (phydev->autoneg)
  469. /* inform link partner of our
  470. * new flow ctrl settings
  471. */
  472. return phy_start_aneg(phydev);
  473. if (!epause->autoneg) {
  474. u32 tempval;
  475. tempval = gfar_read(&regs->maccfg1);
  476. tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
  477. priv->tx_actual_en = 0;
  478. if (priv->tx_pause_en) {
  479. priv->tx_actual_en = 1;
  480. tempval |= MACCFG1_TX_FLOW;
  481. }
  482. if (priv->rx_pause_en)
  483. tempval |= MACCFG1_RX_FLOW;
  484. gfar_write(&regs->maccfg1, tempval);
  485. }
  486. }
  487. return 0;
  488. }
  489. int gfar_set_features(struct net_device *dev, netdev_features_t features)
  490. {
  491. netdev_features_t changed = dev->features ^ features;
  492. struct gfar_private *priv = netdev_priv(dev);
  493. int err = 0;
  494. if (!(changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  495. NETIF_F_RXCSUM)))
  496. return 0;
  497. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  498. cpu_relax();
  499. dev->features = features;
  500. if (dev->flags & IFF_UP) {
  501. /* Now we take down the rings to rebuild them */
  502. stop_gfar(dev);
  503. err = startup_gfar(dev);
  504. } else {
  505. gfar_mac_reset(priv);
  506. }
  507. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  508. return err;
  509. }
  510. static uint32_t gfar_get_msglevel(struct net_device *dev)
  511. {
  512. struct gfar_private *priv = netdev_priv(dev);
  513. return priv->msg_enable;
  514. }
  515. static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
  516. {
  517. struct gfar_private *priv = netdev_priv(dev);
  518. priv->msg_enable = data;
  519. }
  520. #ifdef CONFIG_PM
  521. static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  522. {
  523. struct gfar_private *priv = netdev_priv(dev);
  524. wol->supported = 0;
  525. wol->wolopts = 0;
  526. if (priv->wol_supported & GFAR_WOL_MAGIC)
  527. wol->supported |= WAKE_MAGIC;
  528. if (priv->wol_supported & GFAR_WOL_FILER_UCAST)
  529. wol->supported |= WAKE_UCAST;
  530. if (priv->wol_opts & GFAR_WOL_MAGIC)
  531. wol->wolopts |= WAKE_MAGIC;
  532. if (priv->wol_opts & GFAR_WOL_FILER_UCAST)
  533. wol->wolopts |= WAKE_UCAST;
  534. }
  535. static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  536. {
  537. struct gfar_private *priv = netdev_priv(dev);
  538. u16 wol_opts = 0;
  539. int err;
  540. if (!priv->wol_supported && wol->wolopts)
  541. return -EINVAL;
  542. if (wol->wolopts & ~(WAKE_MAGIC | WAKE_UCAST))
  543. return -EINVAL;
  544. if (wol->wolopts & WAKE_MAGIC) {
  545. wol_opts |= GFAR_WOL_MAGIC;
  546. } else {
  547. if (wol->wolopts & WAKE_UCAST)
  548. wol_opts |= GFAR_WOL_FILER_UCAST;
  549. }
  550. wol_opts &= priv->wol_supported;
  551. priv->wol_opts = 0;
  552. err = device_set_wakeup_enable(priv->dev, wol_opts);
  553. if (err)
  554. return err;
  555. priv->wol_opts = wol_opts;
  556. return 0;
  557. }
  558. #endif
  559. static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
  560. {
  561. u32 fcr = 0x0, fpr = FPR_FILER_MASK;
  562. if (ethflow & RXH_L2DA) {
  563. fcr = RQFCR_PID_DAH | RQFCR_CMP_NOMATCH |
  564. RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
  565. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  566. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  567. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  568. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  569. fcr = RQFCR_PID_DAL | RQFCR_CMP_NOMATCH |
  570. RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
  571. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  572. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  573. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  574. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  575. }
  576. if (ethflow & RXH_VLAN) {
  577. fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  578. RQFCR_AND | RQFCR_HASHTBL_0;
  579. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  580. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  581. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  582. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  583. }
  584. if (ethflow & RXH_IP_SRC) {
  585. fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  586. RQFCR_AND | RQFCR_HASHTBL_0;
  587. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  588. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  589. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  590. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  591. }
  592. if (ethflow & (RXH_IP_DST)) {
  593. fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  594. RQFCR_AND | RQFCR_HASHTBL_0;
  595. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  596. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  597. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  598. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  599. }
  600. if (ethflow & RXH_L3_PROTO) {
  601. fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  602. RQFCR_AND | RQFCR_HASHTBL_0;
  603. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  604. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  605. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  606. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  607. }
  608. if (ethflow & RXH_L4_B_0_1) {
  609. fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  610. RQFCR_AND | RQFCR_HASHTBL_0;
  611. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  612. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  613. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  614. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  615. }
  616. if (ethflow & RXH_L4_B_2_3) {
  617. fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  618. RQFCR_AND | RQFCR_HASHTBL_0;
  619. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  620. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  621. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  622. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  623. }
  624. }
  625. static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
  626. u64 class)
  627. {
  628. unsigned int cmp_rqfpr;
  629. unsigned int *local_rqfpr;
  630. unsigned int *local_rqfcr;
  631. int i = 0x0, k = 0x0;
  632. int j = MAX_FILER_IDX, l = 0x0;
  633. int ret = 1;
  634. local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
  635. GFP_KERNEL);
  636. local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
  637. GFP_KERNEL);
  638. if (!local_rqfpr || !local_rqfcr) {
  639. ret = 0;
  640. goto err;
  641. }
  642. switch (class) {
  643. case TCP_V4_FLOW:
  644. cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
  645. break;
  646. case UDP_V4_FLOW:
  647. cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
  648. break;
  649. case TCP_V6_FLOW:
  650. cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
  651. break;
  652. case UDP_V6_FLOW:
  653. cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
  654. break;
  655. default:
  656. netdev_err(priv->ndev,
  657. "Right now this class is not supported\n");
  658. ret = 0;
  659. goto err;
  660. }
  661. for (i = 0; i < MAX_FILER_IDX + 1; i++) {
  662. local_rqfpr[j] = priv->ftp_rqfpr[i];
  663. local_rqfcr[j] = priv->ftp_rqfcr[i];
  664. j--;
  665. if ((priv->ftp_rqfcr[i] ==
  666. (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
  667. (priv->ftp_rqfpr[i] == cmp_rqfpr))
  668. break;
  669. }
  670. if (i == MAX_FILER_IDX + 1) {
  671. netdev_err(priv->ndev,
  672. "No parse rule found, can't create hash rules\n");
  673. ret = 0;
  674. goto err;
  675. }
  676. /* If a match was found, then it begins the starting of a cluster rule
  677. * if it was already programmed, we need to overwrite these rules
  678. */
  679. for (l = i+1; l < MAX_FILER_IDX; l++) {
  680. if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
  681. !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
  682. priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
  683. RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
  684. priv->ftp_rqfpr[l] = FPR_FILER_MASK;
  685. gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
  686. priv->ftp_rqfpr[l]);
  687. break;
  688. }
  689. if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
  690. (priv->ftp_rqfcr[l] & RQFCR_AND))
  691. continue;
  692. else {
  693. local_rqfpr[j] = priv->ftp_rqfpr[l];
  694. local_rqfcr[j] = priv->ftp_rqfcr[l];
  695. j--;
  696. }
  697. }
  698. priv->cur_filer_idx = l - 1;
  699. /* hash rules */
  700. ethflow_to_filer_rules(priv, ethflow);
  701. /* Write back the popped out rules again */
  702. for (k = j+1; k < MAX_FILER_IDX; k++) {
  703. priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
  704. priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
  705. gfar_write_filer(priv, priv->cur_filer_idx,
  706. local_rqfcr[k], local_rqfpr[k]);
  707. if (!priv->cur_filer_idx)
  708. break;
  709. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  710. }
  711. err:
  712. kfree(local_rqfcr);
  713. kfree(local_rqfpr);
  714. return ret;
  715. }
  716. static int gfar_set_hash_opts(struct gfar_private *priv,
  717. struct ethtool_rxnfc *cmd)
  718. {
  719. /* write the filer rules here */
  720. if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
  721. return -EINVAL;
  722. return 0;
  723. }
  724. static int gfar_check_filer_hardware(struct gfar_private *priv)
  725. {
  726. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  727. u32 i;
  728. /* Check if we are in FIFO mode */
  729. i = gfar_read(&regs->ecntrl);
  730. i &= ECNTRL_FIFM;
  731. if (i == ECNTRL_FIFM) {
  732. netdev_notice(priv->ndev, "Interface in FIFO mode\n");
  733. i = gfar_read(&regs->rctrl);
  734. i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
  735. if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
  736. netdev_info(priv->ndev,
  737. "Receive Queue Filtering enabled\n");
  738. } else {
  739. netdev_warn(priv->ndev,
  740. "Receive Queue Filtering disabled\n");
  741. return -EOPNOTSUPP;
  742. }
  743. }
  744. /* Or in standard mode */
  745. else {
  746. i = gfar_read(&regs->rctrl);
  747. i &= RCTRL_PRSDEP_MASK;
  748. if (i == RCTRL_PRSDEP_MASK) {
  749. netdev_info(priv->ndev,
  750. "Receive Queue Filtering enabled\n");
  751. } else {
  752. netdev_warn(priv->ndev,
  753. "Receive Queue Filtering disabled\n");
  754. return -EOPNOTSUPP;
  755. }
  756. }
  757. /* Sets the properties for arbitrary filer rule
  758. * to the first 4 Layer 4 Bytes
  759. */
  760. gfar_write(&regs->rbifx, 0xC0C1C2C3);
  761. return 0;
  762. }
  763. /* Write a mask to filer cache */
  764. static void gfar_set_mask(u32 mask, struct filer_table *tab)
  765. {
  766. tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
  767. tab->fe[tab->index].prop = mask;
  768. tab->index++;
  769. }
  770. /* Sets parse bits (e.g. IP or TCP) */
  771. static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
  772. {
  773. gfar_set_mask(mask, tab);
  774. tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
  775. RQFCR_AND;
  776. tab->fe[tab->index].prop = value;
  777. tab->index++;
  778. }
  779. static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
  780. struct filer_table *tab)
  781. {
  782. gfar_set_mask(mask, tab);
  783. tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
  784. tab->fe[tab->index].prop = value;
  785. tab->index++;
  786. }
  787. /* For setting a tuple of value and mask of type flag
  788. * Example:
  789. * IP-Src = 10.0.0.0/255.0.0.0
  790. * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
  791. *
  792. * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
  793. * For a don't care mask it gives us a 0
  794. *
  795. * The check if don't care and the mask adjustment if mask=0 is done for VLAN
  796. * and MAC stuff on an upper level (due to missing information on this level).
  797. * For these guys we can discard them if they are value=0 and mask=0.
  798. *
  799. * Further the all masks are one-padded for better hardware efficiency.
  800. */
  801. static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
  802. struct filer_table *tab)
  803. {
  804. switch (flag) {
  805. /* 3bit */
  806. case RQFCR_PID_PRI:
  807. if (!(value | mask))
  808. return;
  809. mask |= RQFCR_PID_PRI_MASK;
  810. break;
  811. /* 8bit */
  812. case RQFCR_PID_L4P:
  813. case RQFCR_PID_TOS:
  814. if (!~(mask | RQFCR_PID_L4P_MASK))
  815. return;
  816. if (!mask)
  817. mask = ~0;
  818. else
  819. mask |= RQFCR_PID_L4P_MASK;
  820. break;
  821. /* 12bit */
  822. case RQFCR_PID_VID:
  823. if (!(value | mask))
  824. return;
  825. mask |= RQFCR_PID_VID_MASK;
  826. break;
  827. /* 16bit */
  828. case RQFCR_PID_DPT:
  829. case RQFCR_PID_SPT:
  830. case RQFCR_PID_ETY:
  831. if (!~(mask | RQFCR_PID_PORT_MASK))
  832. return;
  833. if (!mask)
  834. mask = ~0;
  835. else
  836. mask |= RQFCR_PID_PORT_MASK;
  837. break;
  838. /* 24bit */
  839. case RQFCR_PID_DAH:
  840. case RQFCR_PID_DAL:
  841. case RQFCR_PID_SAH:
  842. case RQFCR_PID_SAL:
  843. if (!(value | mask))
  844. return;
  845. mask |= RQFCR_PID_MAC_MASK;
  846. break;
  847. /* for all real 32bit masks */
  848. default:
  849. if (!~mask)
  850. return;
  851. if (!mask)
  852. mask = ~0;
  853. break;
  854. }
  855. gfar_set_general_attribute(value, mask, flag, tab);
  856. }
  857. /* Translates value and mask for UDP, TCP or SCTP */
  858. static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
  859. struct ethtool_tcpip4_spec *mask,
  860. struct filer_table *tab)
  861. {
  862. gfar_set_attribute(be32_to_cpu(value->ip4src),
  863. be32_to_cpu(mask->ip4src),
  864. RQFCR_PID_SIA, tab);
  865. gfar_set_attribute(be32_to_cpu(value->ip4dst),
  866. be32_to_cpu(mask->ip4dst),
  867. RQFCR_PID_DIA, tab);
  868. gfar_set_attribute(be16_to_cpu(value->pdst),
  869. be16_to_cpu(mask->pdst),
  870. RQFCR_PID_DPT, tab);
  871. gfar_set_attribute(be16_to_cpu(value->psrc),
  872. be16_to_cpu(mask->psrc),
  873. RQFCR_PID_SPT, tab);
  874. gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
  875. }
  876. /* Translates value and mask for RAW-IP4 */
  877. static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
  878. struct ethtool_usrip4_spec *mask,
  879. struct filer_table *tab)
  880. {
  881. gfar_set_attribute(be32_to_cpu(value->ip4src),
  882. be32_to_cpu(mask->ip4src),
  883. RQFCR_PID_SIA, tab);
  884. gfar_set_attribute(be32_to_cpu(value->ip4dst),
  885. be32_to_cpu(mask->ip4dst),
  886. RQFCR_PID_DIA, tab);
  887. gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
  888. gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
  889. gfar_set_attribute(be32_to_cpu(value->l4_4_bytes),
  890. be32_to_cpu(mask->l4_4_bytes),
  891. RQFCR_PID_ARB, tab);
  892. }
  893. /* Translates value and mask for ETHER spec */
  894. static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
  895. struct filer_table *tab)
  896. {
  897. u32 upper_temp_mask = 0;
  898. u32 lower_temp_mask = 0;
  899. /* Source address */
  900. if (!is_broadcast_ether_addr(mask->h_source)) {
  901. if (is_zero_ether_addr(mask->h_source)) {
  902. upper_temp_mask = 0xFFFFFFFF;
  903. lower_temp_mask = 0xFFFFFFFF;
  904. } else {
  905. upper_temp_mask = mask->h_source[0] << 16 |
  906. mask->h_source[1] << 8 |
  907. mask->h_source[2];
  908. lower_temp_mask = mask->h_source[3] << 16 |
  909. mask->h_source[4] << 8 |
  910. mask->h_source[5];
  911. }
  912. /* Upper 24bit */
  913. gfar_set_attribute(value->h_source[0] << 16 |
  914. value->h_source[1] << 8 |
  915. value->h_source[2],
  916. upper_temp_mask, RQFCR_PID_SAH, tab);
  917. /* And the same for the lower part */
  918. gfar_set_attribute(value->h_source[3] << 16 |
  919. value->h_source[4] << 8 |
  920. value->h_source[5],
  921. lower_temp_mask, RQFCR_PID_SAL, tab);
  922. }
  923. /* Destination address */
  924. if (!is_broadcast_ether_addr(mask->h_dest)) {
  925. /* Special for destination is limited broadcast */
  926. if ((is_broadcast_ether_addr(value->h_dest) &&
  927. is_zero_ether_addr(mask->h_dest))) {
  928. gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
  929. } else {
  930. if (is_zero_ether_addr(mask->h_dest)) {
  931. upper_temp_mask = 0xFFFFFFFF;
  932. lower_temp_mask = 0xFFFFFFFF;
  933. } else {
  934. upper_temp_mask = mask->h_dest[0] << 16 |
  935. mask->h_dest[1] << 8 |
  936. mask->h_dest[2];
  937. lower_temp_mask = mask->h_dest[3] << 16 |
  938. mask->h_dest[4] << 8 |
  939. mask->h_dest[5];
  940. }
  941. /* Upper 24bit */
  942. gfar_set_attribute(value->h_dest[0] << 16 |
  943. value->h_dest[1] << 8 |
  944. value->h_dest[2],
  945. upper_temp_mask, RQFCR_PID_DAH, tab);
  946. /* And the same for the lower part */
  947. gfar_set_attribute(value->h_dest[3] << 16 |
  948. value->h_dest[4] << 8 |
  949. value->h_dest[5],
  950. lower_temp_mask, RQFCR_PID_DAL, tab);
  951. }
  952. }
  953. gfar_set_attribute(be16_to_cpu(value->h_proto),
  954. be16_to_cpu(mask->h_proto),
  955. RQFCR_PID_ETY, tab);
  956. }
  957. static inline u32 vlan_tci_vid(struct ethtool_rx_flow_spec *rule)
  958. {
  959. return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_VID_MASK;
  960. }
  961. static inline u32 vlan_tci_vidm(struct ethtool_rx_flow_spec *rule)
  962. {
  963. return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_VID_MASK;
  964. }
  965. static inline u32 vlan_tci_cfi(struct ethtool_rx_flow_spec *rule)
  966. {
  967. return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_CFI_MASK;
  968. }
  969. static inline u32 vlan_tci_cfim(struct ethtool_rx_flow_spec *rule)
  970. {
  971. return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_CFI_MASK;
  972. }
  973. static inline u32 vlan_tci_prio(struct ethtool_rx_flow_spec *rule)
  974. {
  975. return (be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_PRIO_MASK) >>
  976. VLAN_PRIO_SHIFT;
  977. }
  978. static inline u32 vlan_tci_priom(struct ethtool_rx_flow_spec *rule)
  979. {
  980. return (be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_PRIO_MASK) >>
  981. VLAN_PRIO_SHIFT;
  982. }
  983. /* Convert a rule to binary filter format of gianfar */
  984. static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
  985. struct filer_table *tab)
  986. {
  987. u32 vlan = 0, vlan_mask = 0;
  988. u32 id = 0, id_mask = 0;
  989. u32 cfi = 0, cfi_mask = 0;
  990. u32 prio = 0, prio_mask = 0;
  991. u32 old_index = tab->index;
  992. /* Check if vlan is wanted */
  993. if ((rule->flow_type & FLOW_EXT) &&
  994. (rule->m_ext.vlan_tci != cpu_to_be16(0xFFFF))) {
  995. if (!rule->m_ext.vlan_tci)
  996. rule->m_ext.vlan_tci = cpu_to_be16(0xFFFF);
  997. vlan = RQFPR_VLN;
  998. vlan_mask = RQFPR_VLN;
  999. /* Separate the fields */
  1000. id = vlan_tci_vid(rule);
  1001. id_mask = vlan_tci_vidm(rule);
  1002. cfi = vlan_tci_cfi(rule);
  1003. cfi_mask = vlan_tci_cfim(rule);
  1004. prio = vlan_tci_prio(rule);
  1005. prio_mask = vlan_tci_priom(rule);
  1006. if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
  1007. vlan |= RQFPR_CFI;
  1008. vlan_mask |= RQFPR_CFI;
  1009. } else if (cfi != VLAN_TAG_PRESENT &&
  1010. cfi_mask == VLAN_TAG_PRESENT) {
  1011. vlan_mask |= RQFPR_CFI;
  1012. }
  1013. }
  1014. switch (rule->flow_type & ~FLOW_EXT) {
  1015. case TCP_V4_FLOW:
  1016. gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
  1017. RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
  1018. gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
  1019. &rule->m_u.tcp_ip4_spec, tab);
  1020. break;
  1021. case UDP_V4_FLOW:
  1022. gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
  1023. RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
  1024. gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
  1025. &rule->m_u.udp_ip4_spec, tab);
  1026. break;
  1027. case SCTP_V4_FLOW:
  1028. gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
  1029. tab);
  1030. gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
  1031. gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
  1032. (struct ethtool_tcpip4_spec *)&rule->m_u,
  1033. tab);
  1034. break;
  1035. case IP_USER_FLOW:
  1036. gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
  1037. tab);
  1038. gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
  1039. (struct ethtool_usrip4_spec *) &rule->m_u,
  1040. tab);
  1041. break;
  1042. case ETHER_FLOW:
  1043. if (vlan)
  1044. gfar_set_parse_bits(vlan, vlan_mask, tab);
  1045. gfar_set_ether((struct ethhdr *) &rule->h_u,
  1046. (struct ethhdr *) &rule->m_u, tab);
  1047. break;
  1048. default:
  1049. return -1;
  1050. }
  1051. /* Set the vlan attributes in the end */
  1052. if (vlan) {
  1053. gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
  1054. gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
  1055. }
  1056. /* If there has been nothing written till now, it must be a default */
  1057. if (tab->index == old_index) {
  1058. gfar_set_mask(0xFFFFFFFF, tab);
  1059. tab->fe[tab->index].ctrl = 0x20;
  1060. tab->fe[tab->index].prop = 0x0;
  1061. tab->index++;
  1062. }
  1063. /* Remove last AND */
  1064. tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
  1065. /* Specify which queue to use or to drop */
  1066. if (rule->ring_cookie == RX_CLS_FLOW_DISC)
  1067. tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
  1068. else
  1069. tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
  1070. /* Only big enough entries can be clustered */
  1071. if (tab->index > (old_index + 2)) {
  1072. tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
  1073. tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
  1074. }
  1075. /* In rare cases the cache can be full while there is
  1076. * free space in hw
  1077. */
  1078. if (tab->index > MAX_FILER_CACHE_IDX - 1)
  1079. return -EBUSY;
  1080. return 0;
  1081. }
  1082. /* Write the bit-pattern from software's buffer to hardware registers */
  1083. static int gfar_write_filer_table(struct gfar_private *priv,
  1084. struct filer_table *tab)
  1085. {
  1086. u32 i = 0;
  1087. if (tab->index > MAX_FILER_IDX - 1)
  1088. return -EBUSY;
  1089. /* Fill regular entries */
  1090. for (; i < MAX_FILER_IDX && (tab->fe[i].ctrl | tab->fe[i].prop); i++)
  1091. gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
  1092. /* Fill the rest with fall-troughs */
  1093. for (; i < MAX_FILER_IDX; i++)
  1094. gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
  1095. /* Last entry must be default accept
  1096. * because that's what people expect
  1097. */
  1098. gfar_write_filer(priv, i, 0x20, 0x0);
  1099. return 0;
  1100. }
  1101. static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
  1102. struct gfar_private *priv)
  1103. {
  1104. if (flow->flow_type & FLOW_EXT) {
  1105. if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
  1106. netdev_warn(priv->ndev,
  1107. "User-specific data not supported!\n");
  1108. if (~flow->m_ext.vlan_etype)
  1109. netdev_warn(priv->ndev,
  1110. "VLAN-etype not supported!\n");
  1111. }
  1112. if (flow->flow_type == IP_USER_FLOW)
  1113. if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
  1114. netdev_warn(priv->ndev,
  1115. "IP-Version differing from IPv4 not supported!\n");
  1116. return 0;
  1117. }
  1118. static int gfar_process_filer_changes(struct gfar_private *priv)
  1119. {
  1120. struct ethtool_flow_spec_container *j;
  1121. struct filer_table *tab;
  1122. s32 ret = 0;
  1123. /* So index is set to zero, too! */
  1124. tab = kzalloc(sizeof(*tab), GFP_KERNEL);
  1125. if (tab == NULL)
  1126. return -ENOMEM;
  1127. /* Now convert the existing filer data from flow_spec into
  1128. * filer tables binary format
  1129. */
  1130. list_for_each_entry(j, &priv->rx_list.list, list) {
  1131. ret = gfar_convert_to_filer(&j->fs, tab);
  1132. if (ret == -EBUSY) {
  1133. netdev_err(priv->ndev,
  1134. "Rule not added: No free space!\n");
  1135. goto end;
  1136. }
  1137. if (ret == -1) {
  1138. netdev_err(priv->ndev,
  1139. "Rule not added: Unsupported Flow-type!\n");
  1140. goto end;
  1141. }
  1142. }
  1143. /* Write everything to hardware */
  1144. ret = gfar_write_filer_table(priv, tab);
  1145. if (ret == -EBUSY) {
  1146. netdev_err(priv->ndev, "Rule not added: No free space!\n");
  1147. goto end;
  1148. }
  1149. end:
  1150. kfree(tab);
  1151. return ret;
  1152. }
  1153. static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
  1154. {
  1155. u32 i = 0;
  1156. for (i = 0; i < sizeof(flow->m_u); i++)
  1157. flow->m_u.hdata[i] ^= 0xFF;
  1158. flow->m_ext.vlan_etype ^= cpu_to_be16(0xFFFF);
  1159. flow->m_ext.vlan_tci ^= cpu_to_be16(0xFFFF);
  1160. flow->m_ext.data[0] ^= cpu_to_be32(~0);
  1161. flow->m_ext.data[1] ^= cpu_to_be32(~0);
  1162. }
  1163. static int gfar_add_cls(struct gfar_private *priv,
  1164. struct ethtool_rx_flow_spec *flow)
  1165. {
  1166. struct ethtool_flow_spec_container *temp, *comp;
  1167. int ret = 0;
  1168. temp = kmalloc(sizeof(*temp), GFP_KERNEL);
  1169. if (temp == NULL)
  1170. return -ENOMEM;
  1171. memcpy(&temp->fs, flow, sizeof(temp->fs));
  1172. gfar_invert_masks(&temp->fs);
  1173. ret = gfar_check_capability(&temp->fs, priv);
  1174. if (ret)
  1175. goto clean_mem;
  1176. /* Link in the new element at the right @location */
  1177. if (list_empty(&priv->rx_list.list)) {
  1178. ret = gfar_check_filer_hardware(priv);
  1179. if (ret != 0)
  1180. goto clean_mem;
  1181. list_add(&temp->list, &priv->rx_list.list);
  1182. goto process;
  1183. } else {
  1184. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1185. if (comp->fs.location > flow->location) {
  1186. list_add_tail(&temp->list, &comp->list);
  1187. goto process;
  1188. }
  1189. if (comp->fs.location == flow->location) {
  1190. netdev_err(priv->ndev,
  1191. "Rule not added: ID %d not free!\n",
  1192. flow->location);
  1193. ret = -EBUSY;
  1194. goto clean_mem;
  1195. }
  1196. }
  1197. list_add_tail(&temp->list, &priv->rx_list.list);
  1198. }
  1199. process:
  1200. priv->rx_list.count++;
  1201. ret = gfar_process_filer_changes(priv);
  1202. if (ret)
  1203. goto clean_list;
  1204. return ret;
  1205. clean_list:
  1206. priv->rx_list.count--;
  1207. list_del(&temp->list);
  1208. clean_mem:
  1209. kfree(temp);
  1210. return ret;
  1211. }
  1212. static int gfar_del_cls(struct gfar_private *priv, u32 loc)
  1213. {
  1214. struct ethtool_flow_spec_container *comp;
  1215. u32 ret = -EINVAL;
  1216. if (list_empty(&priv->rx_list.list))
  1217. return ret;
  1218. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1219. if (comp->fs.location == loc) {
  1220. list_del(&comp->list);
  1221. kfree(comp);
  1222. priv->rx_list.count--;
  1223. gfar_process_filer_changes(priv);
  1224. ret = 0;
  1225. break;
  1226. }
  1227. }
  1228. return ret;
  1229. }
  1230. static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
  1231. {
  1232. struct ethtool_flow_spec_container *comp;
  1233. u32 ret = -EINVAL;
  1234. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1235. if (comp->fs.location == cmd->fs.location) {
  1236. memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
  1237. gfar_invert_masks(&cmd->fs);
  1238. ret = 0;
  1239. break;
  1240. }
  1241. }
  1242. return ret;
  1243. }
  1244. static int gfar_get_cls_all(struct gfar_private *priv,
  1245. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  1246. {
  1247. struct ethtool_flow_spec_container *comp;
  1248. u32 i = 0;
  1249. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1250. if (i == cmd->rule_cnt)
  1251. return -EMSGSIZE;
  1252. rule_locs[i] = comp->fs.location;
  1253. i++;
  1254. }
  1255. cmd->data = MAX_FILER_IDX;
  1256. cmd->rule_cnt = i;
  1257. return 0;
  1258. }
  1259. static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
  1260. {
  1261. struct gfar_private *priv = netdev_priv(dev);
  1262. int ret = 0;
  1263. if (test_bit(GFAR_RESETTING, &priv->state))
  1264. return -EBUSY;
  1265. mutex_lock(&priv->rx_queue_access);
  1266. switch (cmd->cmd) {
  1267. case ETHTOOL_SRXFH:
  1268. ret = gfar_set_hash_opts(priv, cmd);
  1269. break;
  1270. case ETHTOOL_SRXCLSRLINS:
  1271. if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
  1272. cmd->fs.ring_cookie >= priv->num_rx_queues) ||
  1273. cmd->fs.location >= MAX_FILER_IDX) {
  1274. ret = -EINVAL;
  1275. break;
  1276. }
  1277. ret = gfar_add_cls(priv, &cmd->fs);
  1278. break;
  1279. case ETHTOOL_SRXCLSRLDEL:
  1280. ret = gfar_del_cls(priv, cmd->fs.location);
  1281. break;
  1282. default:
  1283. ret = -EINVAL;
  1284. }
  1285. mutex_unlock(&priv->rx_queue_access);
  1286. return ret;
  1287. }
  1288. static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
  1289. u32 *rule_locs)
  1290. {
  1291. struct gfar_private *priv = netdev_priv(dev);
  1292. int ret = 0;
  1293. switch (cmd->cmd) {
  1294. case ETHTOOL_GRXRINGS:
  1295. cmd->data = priv->num_rx_queues;
  1296. break;
  1297. case ETHTOOL_GRXCLSRLCNT:
  1298. cmd->rule_cnt = priv->rx_list.count;
  1299. break;
  1300. case ETHTOOL_GRXCLSRULE:
  1301. ret = gfar_get_cls(priv, cmd);
  1302. break;
  1303. case ETHTOOL_GRXCLSRLALL:
  1304. ret = gfar_get_cls_all(priv, cmd, rule_locs);
  1305. break;
  1306. default:
  1307. ret = -EINVAL;
  1308. break;
  1309. }
  1310. return ret;
  1311. }
  1312. static int gfar_get_ts_info(struct net_device *dev,
  1313. struct ethtool_ts_info *info)
  1314. {
  1315. struct gfar_private *priv = netdev_priv(dev);
  1316. struct platform_device *ptp_dev;
  1317. struct device_node *ptp_node;
  1318. struct qoriq_ptp *ptp = NULL;
  1319. info->phc_index = -1;
  1320. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
  1321. info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
  1322. SOF_TIMESTAMPING_SOFTWARE;
  1323. return 0;
  1324. }
  1325. ptp_node = of_find_compatible_node(NULL, NULL, "fsl,etsec-ptp");
  1326. if (ptp_node) {
  1327. ptp_dev = of_find_device_by_node(ptp_node);
  1328. if (ptp_dev)
  1329. ptp = platform_get_drvdata(ptp_dev);
  1330. }
  1331. if (ptp)
  1332. info->phc_index = ptp->phc_index;
  1333. info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
  1334. SOF_TIMESTAMPING_RX_HARDWARE |
  1335. SOF_TIMESTAMPING_RAW_HARDWARE;
  1336. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  1337. (1 << HWTSTAMP_TX_ON);
  1338. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  1339. (1 << HWTSTAMP_FILTER_ALL);
  1340. return 0;
  1341. }
  1342. const struct ethtool_ops gfar_ethtool_ops = {
  1343. .get_drvinfo = gfar_gdrvinfo,
  1344. .get_regs_len = gfar_reglen,
  1345. .get_regs = gfar_get_regs,
  1346. .get_link = ethtool_op_get_link,
  1347. .get_coalesce = gfar_gcoalesce,
  1348. .set_coalesce = gfar_scoalesce,
  1349. .get_ringparam = gfar_gringparam,
  1350. .set_ringparam = gfar_sringparam,
  1351. .get_pauseparam = gfar_gpauseparam,
  1352. .set_pauseparam = gfar_spauseparam,
  1353. .get_strings = gfar_gstrings,
  1354. .get_sset_count = gfar_sset_count,
  1355. .get_ethtool_stats = gfar_fill_stats,
  1356. .get_msglevel = gfar_get_msglevel,
  1357. .set_msglevel = gfar_set_msglevel,
  1358. #ifdef CONFIG_PM
  1359. .get_wol = gfar_get_wol,
  1360. .set_wol = gfar_set_wol,
  1361. #endif
  1362. .set_rxnfc = gfar_set_nfc,
  1363. .get_rxnfc = gfar_get_nfc,
  1364. .get_ts_info = gfar_get_ts_info,
  1365. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1366. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1367. };