gianfar_ethtool.c 39 KB

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