gianfar_ethtool.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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 "gianfar.h"
  42. #define GFAR_MAX_COAL_USECS 0xffff
  43. #define GFAR_MAX_COAL_FRAMES 0xff
  44. static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
  45. u64 *buf);
  46. static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
  47. static int gfar_gcoalesce(struct net_device *dev,
  48. struct ethtool_coalesce *cvals);
  49. static int gfar_scoalesce(struct net_device *dev,
  50. struct ethtool_coalesce *cvals);
  51. static void gfar_gringparam(struct net_device *dev,
  52. struct ethtool_ringparam *rvals);
  53. static int gfar_sringparam(struct net_device *dev,
  54. struct ethtool_ringparam *rvals);
  55. static void gfar_gdrvinfo(struct net_device *dev,
  56. struct ethtool_drvinfo *drvinfo);
  57. static const char stat_gstrings[][ETH_GSTRING_LEN] = {
  58. /* extra stats */
  59. "rx-allocation-errors",
  60. "rx-large-frame-errors",
  61. "rx-short-frame-errors",
  62. "rx-non-octet-errors",
  63. "rx-crc-errors",
  64. "rx-overrun-errors",
  65. "rx-busy-errors",
  66. "rx-babbling-errors",
  67. "rx-truncated-frames",
  68. "ethernet-bus-error",
  69. "tx-babbling-errors",
  70. "tx-underrun-errors",
  71. "tx-timeout-errors",
  72. /* rmon stats */
  73. "tx-rx-64-frames",
  74. "tx-rx-65-127-frames",
  75. "tx-rx-128-255-frames",
  76. "tx-rx-256-511-frames",
  77. "tx-rx-512-1023-frames",
  78. "tx-rx-1024-1518-frames",
  79. "tx-rx-1519-1522-good-vlan",
  80. "rx-bytes",
  81. "rx-packets",
  82. "rx-fcs-errors",
  83. "receive-multicast-packet",
  84. "receive-broadcast-packet",
  85. "rx-control-frame-packets",
  86. "rx-pause-frame-packets",
  87. "rx-unknown-op-code",
  88. "rx-alignment-error",
  89. "rx-frame-length-error",
  90. "rx-code-error",
  91. "rx-carrier-sense-error",
  92. "rx-undersize-packets",
  93. "rx-oversize-packets",
  94. "rx-fragmented-frames",
  95. "rx-jabber-frames",
  96. "rx-dropped-frames",
  97. "tx-byte-counter",
  98. "tx-packets",
  99. "tx-multicast-packets",
  100. "tx-broadcast-packets",
  101. "tx-pause-control-frames",
  102. "tx-deferral-packets",
  103. "tx-excessive-deferral-packets",
  104. "tx-single-collision-packets",
  105. "tx-multiple-collision-packets",
  106. "tx-late-collision-packets",
  107. "tx-excessive-collision-packets",
  108. "tx-total-collision",
  109. "reserved",
  110. "tx-dropped-frames",
  111. "tx-jabber-frames",
  112. "tx-fcs-errors",
  113. "tx-control-frames",
  114. "tx-oversize-frames",
  115. "tx-undersize-frames",
  116. "tx-fragmented-frames",
  117. };
  118. /* Fill in a buffer with the strings which correspond to the
  119. * stats */
  120. static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
  121. {
  122. struct gfar_private *priv = netdev_priv(dev);
  123. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
  124. memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
  125. else
  126. memcpy(buf, stat_gstrings,
  127. GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
  128. }
  129. /* Fill in an array of 64-bit statistics from various sources.
  130. * This array will be appended to the end of the ethtool_stats
  131. * structure, and returned to user space
  132. */
  133. static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
  134. u64 *buf)
  135. {
  136. int i;
  137. struct gfar_private *priv = netdev_priv(dev);
  138. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  139. atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
  140. for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
  141. buf[i] = atomic64_read(&extra[i]);
  142. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  143. u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
  144. for (; i < GFAR_STATS_LEN; i++, rmon++)
  145. buf[i] = (u64) gfar_read(rmon);
  146. }
  147. }
  148. static int gfar_sset_count(struct net_device *dev, int sset)
  149. {
  150. struct gfar_private *priv = netdev_priv(dev);
  151. switch (sset) {
  152. case ETH_SS_STATS:
  153. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
  154. return GFAR_STATS_LEN;
  155. else
  156. return GFAR_EXTRA_STATS_LEN;
  157. default:
  158. return -EOPNOTSUPP;
  159. }
  160. }
  161. /* Fills in the drvinfo structure with some basic info */
  162. static void gfar_gdrvinfo(struct net_device *dev,
  163. struct ethtool_drvinfo *drvinfo)
  164. {
  165. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  166. strlcpy(drvinfo->version, gfar_driver_version,
  167. sizeof(drvinfo->version));
  168. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  169. strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
  170. }
  171. /* Return the length of the register structure */
  172. static int gfar_reglen(struct net_device *dev)
  173. {
  174. return sizeof (struct gfar);
  175. }
  176. /* Return a dump of the GFAR register space */
  177. static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  178. void *regbuf)
  179. {
  180. int i;
  181. struct gfar_private *priv = netdev_priv(dev);
  182. u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
  183. u32 *buf = (u32 *) regbuf;
  184. for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
  185. buf[i] = gfar_read(&theregs[i]);
  186. }
  187. /* Convert microseconds to ethernet clock ticks, which changes
  188. * depending on what speed the controller is running at */
  189. static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
  190. unsigned int usecs)
  191. {
  192. struct net_device *ndev = priv->ndev;
  193. struct phy_device *phydev = ndev->phydev;
  194. unsigned int count;
  195. /* The timer is different, depending on the interface speed */
  196. switch (phydev->speed) {
  197. case SPEED_1000:
  198. count = GFAR_GBIT_TIME;
  199. break;
  200. case SPEED_100:
  201. count = GFAR_100_TIME;
  202. break;
  203. case SPEED_10:
  204. default:
  205. count = GFAR_10_TIME;
  206. break;
  207. }
  208. /* Make sure we return a number greater than 0
  209. * if usecs > 0 */
  210. return (usecs * 1000 + count - 1) / count;
  211. }
  212. /* Convert ethernet clock ticks to microseconds */
  213. static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
  214. unsigned int ticks)
  215. {
  216. struct net_device *ndev = priv->ndev;
  217. struct phy_device *phydev = ndev->phydev;
  218. unsigned int count;
  219. /* The timer is different, depending on the interface speed */
  220. switch (phydev->speed) {
  221. case SPEED_1000:
  222. count = GFAR_GBIT_TIME;
  223. break;
  224. case SPEED_100:
  225. count = GFAR_100_TIME;
  226. break;
  227. case SPEED_10:
  228. default:
  229. count = GFAR_10_TIME;
  230. break;
  231. }
  232. /* Make sure we return a number greater than 0 */
  233. /* if ticks is > 0 */
  234. return (ticks * count) / 1000;
  235. }
  236. /* Get the coalescing parameters, and put them in the cvals
  237. * structure. */
  238. static int gfar_gcoalesce(struct net_device *dev,
  239. struct ethtool_coalesce *cvals)
  240. {
  241. struct gfar_private *priv = netdev_priv(dev);
  242. struct gfar_priv_rx_q *rx_queue = NULL;
  243. struct gfar_priv_tx_q *tx_queue = NULL;
  244. unsigned long rxtime;
  245. unsigned long rxcount;
  246. unsigned long txtime;
  247. unsigned long txcount;
  248. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
  249. return -EOPNOTSUPP;
  250. if (!dev->phydev)
  251. return -ENODEV;
  252. rx_queue = priv->rx_queue[0];
  253. tx_queue = priv->tx_queue[0];
  254. rxtime = get_ictt_value(rx_queue->rxic);
  255. rxcount = get_icft_value(rx_queue->rxic);
  256. txtime = get_ictt_value(tx_queue->txic);
  257. txcount = get_icft_value(tx_queue->txic);
  258. cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
  259. cvals->rx_max_coalesced_frames = rxcount;
  260. cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
  261. cvals->tx_max_coalesced_frames = txcount;
  262. cvals->use_adaptive_rx_coalesce = 0;
  263. cvals->use_adaptive_tx_coalesce = 0;
  264. cvals->pkt_rate_low = 0;
  265. cvals->rx_coalesce_usecs_low = 0;
  266. cvals->rx_max_coalesced_frames_low = 0;
  267. cvals->tx_coalesce_usecs_low = 0;
  268. cvals->tx_max_coalesced_frames_low = 0;
  269. /* When the packet rate is below pkt_rate_high but above
  270. * pkt_rate_low (both measured in packets per second) the
  271. * normal {rx,tx}_* coalescing parameters are used.
  272. */
  273. /* When the packet rate is (measured in packets per second)
  274. * is above pkt_rate_high, the {rx,tx}_*_high parameters are
  275. * used.
  276. */
  277. cvals->pkt_rate_high = 0;
  278. cvals->rx_coalesce_usecs_high = 0;
  279. cvals->rx_max_coalesced_frames_high = 0;
  280. cvals->tx_coalesce_usecs_high = 0;
  281. cvals->tx_max_coalesced_frames_high = 0;
  282. /* How often to do adaptive coalescing packet rate sampling,
  283. * measured in seconds. Must not be zero.
  284. */
  285. cvals->rate_sample_interval = 0;
  286. return 0;
  287. }
  288. /* Change the coalescing values.
  289. * Both cvals->*_usecs and cvals->*_frames have to be > 0
  290. * in order for coalescing to be active
  291. */
  292. static int gfar_scoalesce(struct net_device *dev,
  293. struct ethtool_coalesce *cvals)
  294. {
  295. struct gfar_private *priv = netdev_priv(dev);
  296. int i, err = 0;
  297. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
  298. return -EOPNOTSUPP;
  299. if (!dev->phydev)
  300. return -ENODEV;
  301. /* Check the bounds of the values */
  302. if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
  303. netdev_info(dev, "Coalescing is limited to %d microseconds\n",
  304. GFAR_MAX_COAL_USECS);
  305. return -EINVAL;
  306. }
  307. if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
  308. netdev_info(dev, "Coalescing is limited to %d frames\n",
  309. GFAR_MAX_COAL_FRAMES);
  310. return -EINVAL;
  311. }
  312. /* Check the bounds of the values */
  313. if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
  314. netdev_info(dev, "Coalescing is limited to %d microseconds\n",
  315. GFAR_MAX_COAL_USECS);
  316. return -EINVAL;
  317. }
  318. if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
  319. netdev_info(dev, "Coalescing is limited to %d frames\n",
  320. GFAR_MAX_COAL_FRAMES);
  321. return -EINVAL;
  322. }
  323. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  324. cpu_relax();
  325. /* Set up rx coalescing */
  326. if ((cvals->rx_coalesce_usecs == 0) ||
  327. (cvals->rx_max_coalesced_frames == 0)) {
  328. for (i = 0; i < priv->num_rx_queues; i++)
  329. priv->rx_queue[i]->rxcoalescing = 0;
  330. } else {
  331. for (i = 0; i < priv->num_rx_queues; i++)
  332. priv->rx_queue[i]->rxcoalescing = 1;
  333. }
  334. for (i = 0; i < priv->num_rx_queues; i++) {
  335. priv->rx_queue[i]->rxic = mk_ic_value(
  336. cvals->rx_max_coalesced_frames,
  337. gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
  338. }
  339. /* Set up tx coalescing */
  340. if ((cvals->tx_coalesce_usecs == 0) ||
  341. (cvals->tx_max_coalesced_frames == 0)) {
  342. for (i = 0; i < priv->num_tx_queues; i++)
  343. priv->tx_queue[i]->txcoalescing = 0;
  344. } else {
  345. for (i = 0; i < priv->num_tx_queues; i++)
  346. priv->tx_queue[i]->txcoalescing = 1;
  347. }
  348. for (i = 0; i < priv->num_tx_queues; i++) {
  349. priv->tx_queue[i]->txic = mk_ic_value(
  350. cvals->tx_max_coalesced_frames,
  351. gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
  352. }
  353. if (dev->flags & IFF_UP) {
  354. stop_gfar(dev);
  355. err = startup_gfar(dev);
  356. } else {
  357. gfar_mac_reset(priv);
  358. }
  359. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  360. return err;
  361. }
  362. /* Fills in rvals with the current ring parameters. Currently,
  363. * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
  364. * jumbo are ignored by the driver */
  365. static void gfar_gringparam(struct net_device *dev,
  366. struct ethtool_ringparam *rvals)
  367. {
  368. struct gfar_private *priv = netdev_priv(dev);
  369. struct gfar_priv_tx_q *tx_queue = NULL;
  370. struct gfar_priv_rx_q *rx_queue = NULL;
  371. tx_queue = priv->tx_queue[0];
  372. rx_queue = priv->rx_queue[0];
  373. rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
  374. rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
  375. rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
  376. rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
  377. /* Values changeable by the user. The valid values are
  378. * in the range 1 to the "*_max_pending" counterpart above.
  379. */
  380. rvals->rx_pending = rx_queue->rx_ring_size;
  381. rvals->rx_mini_pending = rx_queue->rx_ring_size;
  382. rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
  383. rvals->tx_pending = tx_queue->tx_ring_size;
  384. }
  385. /* Change the current ring parameters, stopping the controller if
  386. * necessary so that we don't mess things up while we're in motion.
  387. */
  388. static int gfar_sringparam(struct net_device *dev,
  389. struct ethtool_ringparam *rvals)
  390. {
  391. struct gfar_private *priv = netdev_priv(dev);
  392. int err = 0, i;
  393. if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
  394. return -EINVAL;
  395. if (!is_power_of_2(rvals->rx_pending)) {
  396. netdev_err(dev, "Ring sizes must be a power of 2\n");
  397. return -EINVAL;
  398. }
  399. if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
  400. return -EINVAL;
  401. if (!is_power_of_2(rvals->tx_pending)) {
  402. netdev_err(dev, "Ring sizes must be a power of 2\n");
  403. return -EINVAL;
  404. }
  405. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  406. cpu_relax();
  407. if (dev->flags & IFF_UP)
  408. stop_gfar(dev);
  409. /* Change the sizes */
  410. for (i = 0; i < priv->num_rx_queues; i++)
  411. priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
  412. for (i = 0; i < priv->num_tx_queues; i++)
  413. priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
  414. /* Rebuild the rings with the new size */
  415. if (dev->flags & IFF_UP)
  416. err = startup_gfar(dev);
  417. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  418. return err;
  419. }
  420. static void gfar_gpauseparam(struct net_device *dev,
  421. struct ethtool_pauseparam *epause)
  422. {
  423. struct gfar_private *priv = netdev_priv(dev);
  424. epause->autoneg = !!priv->pause_aneg_en;
  425. epause->rx_pause = !!priv->rx_pause_en;
  426. epause->tx_pause = !!priv->tx_pause_en;
  427. }
  428. static int gfar_spauseparam(struct net_device *dev,
  429. struct ethtool_pauseparam *epause)
  430. {
  431. struct gfar_private *priv = netdev_priv(dev);
  432. struct phy_device *phydev = dev->phydev;
  433. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  434. u32 oldadv, newadv;
  435. if (!phydev)
  436. return -ENODEV;
  437. if (!(phydev->supported & SUPPORTED_Pause) ||
  438. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  439. (epause->rx_pause != epause->tx_pause)))
  440. return -EINVAL;
  441. priv->rx_pause_en = priv->tx_pause_en = 0;
  442. if (epause->rx_pause) {
  443. priv->rx_pause_en = 1;
  444. if (epause->tx_pause) {
  445. priv->tx_pause_en = 1;
  446. /* FLOW_CTRL_RX & TX */
  447. newadv = ADVERTISED_Pause;
  448. } else /* FLOW_CTLR_RX */
  449. newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
  450. } else if (epause->tx_pause) {
  451. priv->tx_pause_en = 1;
  452. /* FLOW_CTLR_TX */
  453. newadv = ADVERTISED_Asym_Pause;
  454. } else
  455. newadv = 0;
  456. if (epause->autoneg)
  457. priv->pause_aneg_en = 1;
  458. else
  459. priv->pause_aneg_en = 0;
  460. oldadv = phydev->advertising &
  461. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  462. if (oldadv != newadv) {
  463. phydev->advertising &=
  464. ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  465. phydev->advertising |= newadv;
  466. if (phydev->autoneg)
  467. /* inform link partner of our
  468. * new flow ctrl settings
  469. */
  470. return phy_start_aneg(phydev);
  471. if (!epause->autoneg) {
  472. u32 tempval;
  473. tempval = gfar_read(&regs->maccfg1);
  474. tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
  475. priv->tx_actual_en = 0;
  476. if (priv->tx_pause_en) {
  477. priv->tx_actual_en = 1;
  478. tempval |= MACCFG1_TX_FLOW;
  479. }
  480. if (priv->rx_pause_en)
  481. tempval |= MACCFG1_RX_FLOW;
  482. gfar_write(&regs->maccfg1, tempval);
  483. }
  484. }
  485. return 0;
  486. }
  487. int gfar_set_features(struct net_device *dev, netdev_features_t features)
  488. {
  489. netdev_features_t changed = dev->features ^ features;
  490. struct gfar_private *priv = netdev_priv(dev);
  491. int err = 0;
  492. if (!(changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  493. NETIF_F_RXCSUM)))
  494. return 0;
  495. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  496. cpu_relax();
  497. dev->features = features;
  498. if (dev->flags & IFF_UP) {
  499. /* Now we take down the rings to rebuild them */
  500. stop_gfar(dev);
  501. err = startup_gfar(dev);
  502. } else {
  503. gfar_mac_reset(priv);
  504. }
  505. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  506. return err;
  507. }
  508. static uint32_t gfar_get_msglevel(struct net_device *dev)
  509. {
  510. struct gfar_private *priv = netdev_priv(dev);
  511. return priv->msg_enable;
  512. }
  513. static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
  514. {
  515. struct gfar_private *priv = netdev_priv(dev);
  516. priv->msg_enable = data;
  517. }
  518. #ifdef CONFIG_PM
  519. static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  520. {
  521. struct gfar_private *priv = netdev_priv(dev);
  522. wol->supported = 0;
  523. wol->wolopts = 0;
  524. if (priv->wol_supported & GFAR_WOL_MAGIC)
  525. wol->supported |= WAKE_MAGIC;
  526. if (priv->wol_supported & GFAR_WOL_FILER_UCAST)
  527. wol->supported |= WAKE_UCAST;
  528. if (priv->wol_opts & GFAR_WOL_MAGIC)
  529. wol->wolopts |= WAKE_MAGIC;
  530. if (priv->wol_opts & GFAR_WOL_FILER_UCAST)
  531. wol->wolopts |= WAKE_UCAST;
  532. }
  533. static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  534. {
  535. struct gfar_private *priv = netdev_priv(dev);
  536. u16 wol_opts = 0;
  537. int err;
  538. if (!priv->wol_supported && wol->wolopts)
  539. return -EINVAL;
  540. if (wol->wolopts & ~(WAKE_MAGIC | WAKE_UCAST))
  541. return -EINVAL;
  542. if (wol->wolopts & WAKE_MAGIC) {
  543. wol_opts |= GFAR_WOL_MAGIC;
  544. } else {
  545. if (wol->wolopts & WAKE_UCAST)
  546. wol_opts |= GFAR_WOL_FILER_UCAST;
  547. }
  548. wol_opts &= priv->wol_supported;
  549. priv->wol_opts = 0;
  550. err = device_set_wakeup_enable(priv->dev, wol_opts);
  551. if (err)
  552. return err;
  553. priv->wol_opts = wol_opts;
  554. return 0;
  555. }
  556. #endif
  557. static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
  558. {
  559. u32 fcr = 0x0, fpr = FPR_FILER_MASK;
  560. if (ethflow & RXH_L2DA) {
  561. fcr = RQFCR_PID_DAH | RQFCR_CMP_NOMATCH |
  562. RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
  563. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  564. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  565. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  566. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  567. fcr = RQFCR_PID_DAL | RQFCR_CMP_NOMATCH |
  568. RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
  569. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  570. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  571. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  572. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  573. }
  574. if (ethflow & RXH_VLAN) {
  575. fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  576. RQFCR_AND | RQFCR_HASHTBL_0;
  577. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  578. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  579. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  580. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  581. }
  582. if (ethflow & RXH_IP_SRC) {
  583. fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  584. RQFCR_AND | RQFCR_HASHTBL_0;
  585. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  586. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  587. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  588. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  589. }
  590. if (ethflow & (RXH_IP_DST)) {
  591. fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  592. RQFCR_AND | RQFCR_HASHTBL_0;
  593. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  594. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  595. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  596. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  597. }
  598. if (ethflow & RXH_L3_PROTO) {
  599. fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  600. RQFCR_AND | RQFCR_HASHTBL_0;
  601. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  602. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  603. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  604. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  605. }
  606. if (ethflow & RXH_L4_B_0_1) {
  607. fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  608. RQFCR_AND | RQFCR_HASHTBL_0;
  609. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  610. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  611. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  612. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  613. }
  614. if (ethflow & RXH_L4_B_2_3) {
  615. fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  616. RQFCR_AND | RQFCR_HASHTBL_0;
  617. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  618. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  619. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  620. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  621. }
  622. }
  623. static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
  624. u64 class)
  625. {
  626. unsigned int last_rule_idx = priv->cur_filer_idx;
  627. unsigned int cmp_rqfpr;
  628. unsigned int *local_rqfpr;
  629. unsigned int *local_rqfcr;
  630. int i = 0x0, k = 0x0;
  631. int j = MAX_FILER_IDX, l = 0x0;
  632. int ret = 1;
  633. local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
  634. GFP_KERNEL);
  635. local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
  636. GFP_KERNEL);
  637. if (!local_rqfpr || !local_rqfcr) {
  638. ret = 0;
  639. goto err;
  640. }
  641. switch (class) {
  642. case TCP_V4_FLOW:
  643. cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
  644. break;
  645. case UDP_V4_FLOW:
  646. cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
  647. break;
  648. case TCP_V6_FLOW:
  649. cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
  650. break;
  651. case UDP_V6_FLOW:
  652. cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
  653. break;
  654. default:
  655. netdev_err(priv->ndev,
  656. "Right now this class is not supported\n");
  657. ret = 0;
  658. goto err;
  659. }
  660. for (i = 0; i < MAX_FILER_IDX + 1; i++) {
  661. local_rqfpr[j] = priv->ftp_rqfpr[i];
  662. local_rqfcr[j] = priv->ftp_rqfcr[i];
  663. j--;
  664. if ((priv->ftp_rqfcr[i] ==
  665. (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
  666. (priv->ftp_rqfpr[i] == cmp_rqfpr))
  667. break;
  668. }
  669. if (i == MAX_FILER_IDX + 1) {
  670. netdev_err(priv->ndev,
  671. "No parse rule found, can't create hash rules\n");
  672. ret = 0;
  673. goto err;
  674. }
  675. /* If a match was found, then it begins the starting of a cluster rule
  676. * if it was already programmed, we need to overwrite these rules
  677. */
  678. for (l = i+1; l < MAX_FILER_IDX; l++) {
  679. if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
  680. !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
  681. priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
  682. RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
  683. priv->ftp_rqfpr[l] = FPR_FILER_MASK;
  684. gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
  685. priv->ftp_rqfpr[l]);
  686. break;
  687. }
  688. if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
  689. (priv->ftp_rqfcr[l] & RQFCR_AND))
  690. continue;
  691. else {
  692. local_rqfpr[j] = priv->ftp_rqfpr[l];
  693. local_rqfcr[j] = priv->ftp_rqfcr[l];
  694. j--;
  695. }
  696. }
  697. priv->cur_filer_idx = l - 1;
  698. last_rule_idx = l;
  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. int gfar_phc_index = -1;
  1313. EXPORT_SYMBOL(gfar_phc_index);
  1314. static int gfar_get_ts_info(struct net_device *dev,
  1315. struct ethtool_ts_info *info)
  1316. {
  1317. struct gfar_private *priv = netdev_priv(dev);
  1318. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
  1319. info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
  1320. SOF_TIMESTAMPING_SOFTWARE;
  1321. info->phc_index = -1;
  1322. return 0;
  1323. }
  1324. info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
  1325. SOF_TIMESTAMPING_RX_HARDWARE |
  1326. SOF_TIMESTAMPING_RAW_HARDWARE;
  1327. info->phc_index = gfar_phc_index;
  1328. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  1329. (1 << HWTSTAMP_TX_ON);
  1330. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  1331. (1 << HWTSTAMP_FILTER_ALL);
  1332. return 0;
  1333. }
  1334. const struct ethtool_ops gfar_ethtool_ops = {
  1335. .get_drvinfo = gfar_gdrvinfo,
  1336. .get_regs_len = gfar_reglen,
  1337. .get_regs = gfar_get_regs,
  1338. .get_link = ethtool_op_get_link,
  1339. .get_coalesce = gfar_gcoalesce,
  1340. .set_coalesce = gfar_scoalesce,
  1341. .get_ringparam = gfar_gringparam,
  1342. .set_ringparam = gfar_sringparam,
  1343. .get_pauseparam = gfar_gpauseparam,
  1344. .set_pauseparam = gfar_spauseparam,
  1345. .get_strings = gfar_gstrings,
  1346. .get_sset_count = gfar_sset_count,
  1347. .get_ethtool_stats = gfar_fill_stats,
  1348. .get_msglevel = gfar_get_msglevel,
  1349. .set_msglevel = gfar_set_msglevel,
  1350. #ifdef CONFIG_PM
  1351. .get_wol = gfar_get_wol,
  1352. .set_wol = gfar_set_wol,
  1353. #endif
  1354. .set_rxnfc = gfar_set_nfc,
  1355. .get_rxnfc = gfar_get_nfc,
  1356. .get_ts_info = gfar_get_ts_info,
  1357. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1358. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1359. };