gianfar_ethtool.c 41 KB

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