gianfar_ethtool.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  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. "rx-large-frame-errors",
  59. "rx-short-frame-errors",
  60. "rx-non-octet-errors",
  61. "rx-crc-errors",
  62. "rx-overrun-errors",
  63. "rx-busy-errors",
  64. "rx-babbling-errors",
  65. "rx-truncated-frames",
  66. "ethernet-bus-error",
  67. "tx-babbling-errors",
  68. "tx-underrun-errors",
  69. "rx-skb-missing-errors",
  70. "tx-timeout-errors",
  71. "tx-rx-64-frames",
  72. "tx-rx-65-127-frames",
  73. "tx-rx-128-255-frames",
  74. "tx-rx-256-511-frames",
  75. "tx-rx-512-1023-frames",
  76. "tx-rx-1024-1518-frames",
  77. "tx-rx-1519-1522-good-vlan",
  78. "rx-bytes",
  79. "rx-packets",
  80. "rx-fcs-errors",
  81. "receive-multicast-packet",
  82. "receive-broadcast-packet",
  83. "rx-control-frame-packets",
  84. "rx-pause-frame-packets",
  85. "rx-unknown-op-code",
  86. "rx-alignment-error",
  87. "rx-frame-length-error",
  88. "rx-code-error",
  89. "rx-carrier-sense-error",
  90. "rx-undersize-packets",
  91. "rx-oversize-packets",
  92. "rx-fragmented-frames",
  93. "rx-jabber-frames",
  94. "rx-dropped-frames",
  95. "tx-byte-counter",
  96. "tx-packets",
  97. "tx-multicast-packets",
  98. "tx-broadcast-packets",
  99. "tx-pause-control-frames",
  100. "tx-deferral-packets",
  101. "tx-excessive-deferral-packets",
  102. "tx-single-collision-packets",
  103. "tx-multiple-collision-packets",
  104. "tx-late-collision-packets",
  105. "tx-excessive-collision-packets",
  106. "tx-total-collision",
  107. "reserved",
  108. "tx-dropped-frames",
  109. "tx-jabber-frames",
  110. "tx-fcs-errors",
  111. "tx-control-frames",
  112. "tx-oversize-frames",
  113. "tx-undersize-frames",
  114. "tx-fragmented-frames",
  115. };
  116. /* Fill in a buffer with the strings which correspond to the
  117. * stats */
  118. static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
  119. {
  120. struct gfar_private *priv = netdev_priv(dev);
  121. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
  122. memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
  123. else
  124. memcpy(buf, stat_gstrings,
  125. GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
  126. }
  127. /* Fill in an array of 64-bit statistics from various sources.
  128. * This array will be appended to the end of the ethtool_stats
  129. * structure, and returned to user space
  130. */
  131. static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
  132. u64 *buf)
  133. {
  134. int i;
  135. struct gfar_private *priv = netdev_priv(dev);
  136. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  137. atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
  138. for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
  139. buf[i] = atomic64_read(&extra[i]);
  140. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  141. u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
  142. for (; i < GFAR_STATS_LEN; i++, rmon++)
  143. buf[i] = (u64) gfar_read(rmon);
  144. }
  145. }
  146. static int gfar_sset_count(struct net_device *dev, int sset)
  147. {
  148. struct gfar_private *priv = netdev_priv(dev);
  149. switch (sset) {
  150. case ETH_SS_STATS:
  151. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
  152. return GFAR_STATS_LEN;
  153. else
  154. return GFAR_EXTRA_STATS_LEN;
  155. default:
  156. return -EOPNOTSUPP;
  157. }
  158. }
  159. /* Fills in the drvinfo structure with some basic info */
  160. static void gfar_gdrvinfo(struct net_device *dev,
  161. struct ethtool_drvinfo *drvinfo)
  162. {
  163. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  164. strlcpy(drvinfo->version, gfar_driver_version,
  165. sizeof(drvinfo->version));
  166. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  167. strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
  168. drvinfo->regdump_len = 0;
  169. drvinfo->eedump_len = 0;
  170. }
  171. static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
  172. {
  173. struct gfar_private *priv = netdev_priv(dev);
  174. struct phy_device *phydev = priv->phydev;
  175. if (NULL == phydev)
  176. return -ENODEV;
  177. return phy_ethtool_sset(phydev, cmd);
  178. }
  179. /* Return the current settings in the ethtool_cmd structure */
  180. static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  181. {
  182. struct gfar_private *priv = netdev_priv(dev);
  183. struct phy_device *phydev = priv->phydev;
  184. struct gfar_priv_rx_q *rx_queue = NULL;
  185. struct gfar_priv_tx_q *tx_queue = NULL;
  186. if (NULL == phydev)
  187. return -ENODEV;
  188. tx_queue = priv->tx_queue[0];
  189. rx_queue = priv->rx_queue[0];
  190. /* etsec-1.7 and older versions have only one txic
  191. * and rxic regs although they support multiple queues */
  192. cmd->maxtxpkt = get_icft_value(tx_queue->txic);
  193. cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
  194. return phy_ethtool_gset(phydev, cmd);
  195. }
  196. /* Return the length of the register structure */
  197. static int gfar_reglen(struct net_device *dev)
  198. {
  199. return sizeof (struct gfar);
  200. }
  201. /* Return a dump of the GFAR register space */
  202. static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  203. void *regbuf)
  204. {
  205. int i;
  206. struct gfar_private *priv = netdev_priv(dev);
  207. u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
  208. u32 *buf = (u32 *) regbuf;
  209. for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
  210. buf[i] = gfar_read(&theregs[i]);
  211. }
  212. /* Convert microseconds to ethernet clock ticks, which changes
  213. * depending on what speed the controller is running at */
  214. static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
  215. unsigned int usecs)
  216. {
  217. unsigned int count;
  218. /* The timer is different, depending on the interface speed */
  219. switch (priv->phydev->speed) {
  220. case SPEED_1000:
  221. count = GFAR_GBIT_TIME;
  222. break;
  223. case SPEED_100:
  224. count = GFAR_100_TIME;
  225. break;
  226. case SPEED_10:
  227. default:
  228. count = GFAR_10_TIME;
  229. break;
  230. }
  231. /* Make sure we return a number greater than 0
  232. * if usecs > 0 */
  233. return (usecs * 1000 + count - 1) / count;
  234. }
  235. /* Convert ethernet clock ticks to microseconds */
  236. static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
  237. unsigned int ticks)
  238. {
  239. unsigned int count;
  240. /* The timer is different, depending on the interface speed */
  241. switch (priv->phydev->speed) {
  242. case SPEED_1000:
  243. count = GFAR_GBIT_TIME;
  244. break;
  245. case SPEED_100:
  246. count = GFAR_100_TIME;
  247. break;
  248. case SPEED_10:
  249. default:
  250. count = GFAR_10_TIME;
  251. break;
  252. }
  253. /* Make sure we return a number greater than 0 */
  254. /* if ticks is > 0 */
  255. return (ticks * count) / 1000;
  256. }
  257. /* Get the coalescing parameters, and put them in the cvals
  258. * structure. */
  259. static int gfar_gcoalesce(struct net_device *dev,
  260. struct ethtool_coalesce *cvals)
  261. {
  262. struct gfar_private *priv = netdev_priv(dev);
  263. struct gfar_priv_rx_q *rx_queue = NULL;
  264. struct gfar_priv_tx_q *tx_queue = NULL;
  265. unsigned long rxtime;
  266. unsigned long rxcount;
  267. unsigned long txtime;
  268. unsigned long txcount;
  269. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
  270. return -EOPNOTSUPP;
  271. if (NULL == priv->phydev)
  272. return -ENODEV;
  273. rx_queue = priv->rx_queue[0];
  274. tx_queue = priv->tx_queue[0];
  275. rxtime = get_ictt_value(rx_queue->rxic);
  276. rxcount = get_icft_value(rx_queue->rxic);
  277. txtime = get_ictt_value(tx_queue->txic);
  278. txcount = get_icft_value(tx_queue->txic);
  279. cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
  280. cvals->rx_max_coalesced_frames = rxcount;
  281. cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
  282. cvals->tx_max_coalesced_frames = txcount;
  283. cvals->use_adaptive_rx_coalesce = 0;
  284. cvals->use_adaptive_tx_coalesce = 0;
  285. cvals->pkt_rate_low = 0;
  286. cvals->rx_coalesce_usecs_low = 0;
  287. cvals->rx_max_coalesced_frames_low = 0;
  288. cvals->tx_coalesce_usecs_low = 0;
  289. cvals->tx_max_coalesced_frames_low = 0;
  290. /* When the packet rate is below pkt_rate_high but above
  291. * pkt_rate_low (both measured in packets per second) the
  292. * normal {rx,tx}_* coalescing parameters are used.
  293. */
  294. /* When the packet rate is (measured in packets per second)
  295. * is above pkt_rate_high, the {rx,tx}_*_high parameters are
  296. * used.
  297. */
  298. cvals->pkt_rate_high = 0;
  299. cvals->rx_coalesce_usecs_high = 0;
  300. cvals->rx_max_coalesced_frames_high = 0;
  301. cvals->tx_coalesce_usecs_high = 0;
  302. cvals->tx_max_coalesced_frames_high = 0;
  303. /* How often to do adaptive coalescing packet rate sampling,
  304. * measured in seconds. Must not be zero.
  305. */
  306. cvals->rate_sample_interval = 0;
  307. return 0;
  308. }
  309. /* Change the coalescing values.
  310. * Both cvals->*_usecs and cvals->*_frames have to be > 0
  311. * in order for coalescing to be active
  312. */
  313. static int gfar_scoalesce(struct net_device *dev,
  314. struct ethtool_coalesce *cvals)
  315. {
  316. struct gfar_private *priv = netdev_priv(dev);
  317. int i, err = 0;
  318. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
  319. return -EOPNOTSUPP;
  320. if (NULL == priv->phydev)
  321. return -ENODEV;
  322. /* Check the bounds of the values */
  323. if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
  324. netdev_info(dev, "Coalescing is limited to %d microseconds\n",
  325. GFAR_MAX_COAL_USECS);
  326. return -EINVAL;
  327. }
  328. if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
  329. netdev_info(dev, "Coalescing is limited to %d frames\n",
  330. GFAR_MAX_COAL_FRAMES);
  331. return -EINVAL;
  332. }
  333. /* Check the bounds of the values */
  334. if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
  335. netdev_info(dev, "Coalescing is limited to %d microseconds\n",
  336. GFAR_MAX_COAL_USECS);
  337. return -EINVAL;
  338. }
  339. if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
  340. netdev_info(dev, "Coalescing is limited to %d frames\n",
  341. GFAR_MAX_COAL_FRAMES);
  342. return -EINVAL;
  343. }
  344. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  345. cpu_relax();
  346. /* Set up rx coalescing */
  347. if ((cvals->rx_coalesce_usecs == 0) ||
  348. (cvals->rx_max_coalesced_frames == 0)) {
  349. for (i = 0; i < priv->num_rx_queues; i++)
  350. priv->rx_queue[i]->rxcoalescing = 0;
  351. } else {
  352. for (i = 0; i < priv->num_rx_queues; i++)
  353. priv->rx_queue[i]->rxcoalescing = 1;
  354. }
  355. for (i = 0; i < priv->num_rx_queues; i++) {
  356. priv->rx_queue[i]->rxic = mk_ic_value(
  357. cvals->rx_max_coalesced_frames,
  358. gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
  359. }
  360. /* Set up tx coalescing */
  361. if ((cvals->tx_coalesce_usecs == 0) ||
  362. (cvals->tx_max_coalesced_frames == 0)) {
  363. for (i = 0; i < priv->num_tx_queues; i++)
  364. priv->tx_queue[i]->txcoalescing = 0;
  365. } else {
  366. for (i = 0; i < priv->num_tx_queues; i++)
  367. priv->tx_queue[i]->txcoalescing = 1;
  368. }
  369. for (i = 0; i < priv->num_tx_queues; i++) {
  370. priv->tx_queue[i]->txic = mk_ic_value(
  371. cvals->tx_max_coalesced_frames,
  372. gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
  373. }
  374. if (dev->flags & IFF_UP) {
  375. stop_gfar(dev);
  376. err = startup_gfar(dev);
  377. } else {
  378. gfar_mac_reset(priv);
  379. }
  380. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  381. return err;
  382. }
  383. /* Fills in rvals with the current ring parameters. Currently,
  384. * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
  385. * jumbo are ignored by the driver */
  386. static void gfar_gringparam(struct net_device *dev,
  387. struct ethtool_ringparam *rvals)
  388. {
  389. struct gfar_private *priv = netdev_priv(dev);
  390. struct gfar_priv_tx_q *tx_queue = NULL;
  391. struct gfar_priv_rx_q *rx_queue = NULL;
  392. tx_queue = priv->tx_queue[0];
  393. rx_queue = priv->rx_queue[0];
  394. rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
  395. rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
  396. rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
  397. rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
  398. /* Values changeable by the user. The valid values are
  399. * in the range 1 to the "*_max_pending" counterpart above.
  400. */
  401. rvals->rx_pending = rx_queue->rx_ring_size;
  402. rvals->rx_mini_pending = rx_queue->rx_ring_size;
  403. rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
  404. rvals->tx_pending = tx_queue->tx_ring_size;
  405. }
  406. /* Change the current ring parameters, stopping the controller if
  407. * necessary so that we don't mess things up while we're in motion.
  408. */
  409. static int gfar_sringparam(struct net_device *dev,
  410. struct ethtool_ringparam *rvals)
  411. {
  412. struct gfar_private *priv = netdev_priv(dev);
  413. int err = 0, i;
  414. if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
  415. return -EINVAL;
  416. if (!is_power_of_2(rvals->rx_pending)) {
  417. netdev_err(dev, "Ring sizes must be a power of 2\n");
  418. return -EINVAL;
  419. }
  420. if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
  421. return -EINVAL;
  422. if (!is_power_of_2(rvals->tx_pending)) {
  423. netdev_err(dev, "Ring sizes must be a power of 2\n");
  424. return -EINVAL;
  425. }
  426. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  427. cpu_relax();
  428. if (dev->flags & IFF_UP)
  429. stop_gfar(dev);
  430. /* Change the sizes */
  431. for (i = 0; i < priv->num_rx_queues; i++)
  432. priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
  433. for (i = 0; i < priv->num_tx_queues; i++)
  434. priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
  435. /* Rebuild the rings with the new size */
  436. if (dev->flags & IFF_UP)
  437. err = startup_gfar(dev);
  438. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  439. return err;
  440. }
  441. static void gfar_gpauseparam(struct net_device *dev,
  442. struct ethtool_pauseparam *epause)
  443. {
  444. struct gfar_private *priv = netdev_priv(dev);
  445. epause->autoneg = !!priv->pause_aneg_en;
  446. epause->rx_pause = !!priv->rx_pause_en;
  447. epause->tx_pause = !!priv->tx_pause_en;
  448. }
  449. static int gfar_spauseparam(struct net_device *dev,
  450. struct ethtool_pauseparam *epause)
  451. {
  452. struct gfar_private *priv = netdev_priv(dev);
  453. struct phy_device *phydev = priv->phydev;
  454. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  455. u32 oldadv, newadv;
  456. if (!phydev)
  457. return -ENODEV;
  458. if (!(phydev->supported & SUPPORTED_Pause) ||
  459. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  460. (epause->rx_pause != epause->tx_pause)))
  461. return -EINVAL;
  462. priv->rx_pause_en = priv->tx_pause_en = 0;
  463. if (epause->rx_pause) {
  464. priv->rx_pause_en = 1;
  465. if (epause->tx_pause) {
  466. priv->tx_pause_en = 1;
  467. /* FLOW_CTRL_RX & TX */
  468. newadv = ADVERTISED_Pause;
  469. } else /* FLOW_CTLR_RX */
  470. newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
  471. } else if (epause->tx_pause) {
  472. priv->tx_pause_en = 1;
  473. /* FLOW_CTLR_TX */
  474. newadv = ADVERTISED_Asym_Pause;
  475. } else
  476. newadv = 0;
  477. if (epause->autoneg)
  478. priv->pause_aneg_en = 1;
  479. else
  480. priv->pause_aneg_en = 0;
  481. oldadv = phydev->advertising &
  482. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  483. if (oldadv != newadv) {
  484. phydev->advertising &=
  485. ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  486. phydev->advertising |= newadv;
  487. if (phydev->autoneg)
  488. /* inform link partner of our
  489. * new flow ctrl settings
  490. */
  491. return phy_start_aneg(phydev);
  492. if (!epause->autoneg) {
  493. u32 tempval;
  494. tempval = gfar_read(&regs->maccfg1);
  495. tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
  496. if (priv->tx_pause_en)
  497. tempval |= MACCFG1_TX_FLOW;
  498. if (priv->rx_pause_en)
  499. tempval |= MACCFG1_RX_FLOW;
  500. gfar_write(&regs->maccfg1, tempval);
  501. }
  502. }
  503. return 0;
  504. }
  505. int gfar_set_features(struct net_device *dev, netdev_features_t features)
  506. {
  507. netdev_features_t changed = dev->features ^ features;
  508. struct gfar_private *priv = netdev_priv(dev);
  509. int err = 0;
  510. if (!(changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  511. NETIF_F_RXCSUM)))
  512. return 0;
  513. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  514. cpu_relax();
  515. dev->features = features;
  516. if (dev->flags & IFF_UP) {
  517. /* Now we take down the rings to rebuild them */
  518. stop_gfar(dev);
  519. err = startup_gfar(dev);
  520. } else {
  521. gfar_mac_reset(priv);
  522. }
  523. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  524. return err;
  525. }
  526. static uint32_t gfar_get_msglevel(struct net_device *dev)
  527. {
  528. struct gfar_private *priv = netdev_priv(dev);
  529. return priv->msg_enable;
  530. }
  531. static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
  532. {
  533. struct gfar_private *priv = netdev_priv(dev);
  534. priv->msg_enable = data;
  535. }
  536. #ifdef CONFIG_PM
  537. static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  538. {
  539. struct gfar_private *priv = netdev_priv(dev);
  540. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
  541. wol->supported = WAKE_MAGIC;
  542. wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
  543. } else {
  544. wol->supported = wol->wolopts = 0;
  545. }
  546. }
  547. static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  548. {
  549. struct gfar_private *priv = netdev_priv(dev);
  550. unsigned long flags;
  551. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
  552. wol->wolopts != 0)
  553. return -EINVAL;
  554. if (wol->wolopts & ~WAKE_MAGIC)
  555. return -EINVAL;
  556. device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
  557. spin_lock_irqsave(&priv->bflock, flags);
  558. priv->wol_en = !!device_may_wakeup(&dev->dev);
  559. spin_unlock_irqrestore(&priv->bflock, flags);
  560. return 0;
  561. }
  562. #endif
  563. static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
  564. {
  565. u32 fcr = 0x0, fpr = FPR_FILER_MASK;
  566. if (ethflow & RXH_L2DA) {
  567. fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
  568. RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
  569. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  570. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  571. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  572. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  573. fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
  574. RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
  575. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  576. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  577. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  578. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  579. }
  580. if (ethflow & RXH_VLAN) {
  581. fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  582. RQFCR_AND | RQFCR_HASHTBL_0;
  583. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  584. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  585. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  586. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  587. }
  588. if (ethflow & RXH_IP_SRC) {
  589. fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  590. RQFCR_AND | RQFCR_HASHTBL_0;
  591. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  592. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  593. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  594. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  595. }
  596. if (ethflow & (RXH_IP_DST)) {
  597. fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  598. RQFCR_AND | RQFCR_HASHTBL_0;
  599. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  600. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  601. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  602. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  603. }
  604. if (ethflow & RXH_L3_PROTO) {
  605. fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  606. RQFCR_AND | RQFCR_HASHTBL_0;
  607. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  608. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  609. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  610. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  611. }
  612. if (ethflow & RXH_L4_B_0_1) {
  613. fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  614. RQFCR_AND | RQFCR_HASHTBL_0;
  615. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  616. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  617. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  618. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  619. }
  620. if (ethflow & RXH_L4_B_2_3) {
  621. fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  622. RQFCR_AND | RQFCR_HASHTBL_0;
  623. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  624. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  625. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  626. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  627. }
  628. }
  629. static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
  630. u64 class)
  631. {
  632. unsigned int last_rule_idx = priv->cur_filer_idx;
  633. unsigned int cmp_rqfpr;
  634. unsigned int *local_rqfpr;
  635. unsigned int *local_rqfcr;
  636. int i = 0x0, k = 0x0;
  637. int j = MAX_FILER_IDX, l = 0x0;
  638. int ret = 1;
  639. local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
  640. GFP_KERNEL);
  641. local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
  642. GFP_KERNEL);
  643. if (!local_rqfpr || !local_rqfcr) {
  644. ret = 0;
  645. goto err;
  646. }
  647. switch (class) {
  648. case TCP_V4_FLOW:
  649. cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
  650. break;
  651. case UDP_V4_FLOW:
  652. cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
  653. break;
  654. case TCP_V6_FLOW:
  655. cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
  656. break;
  657. case UDP_V6_FLOW:
  658. cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
  659. break;
  660. default:
  661. netdev_err(priv->ndev,
  662. "Right now this class is not supported\n");
  663. ret = 0;
  664. goto err;
  665. }
  666. for (i = 0; i < MAX_FILER_IDX + 1; i++) {
  667. local_rqfpr[j] = priv->ftp_rqfpr[i];
  668. local_rqfcr[j] = priv->ftp_rqfcr[i];
  669. j--;
  670. if ((priv->ftp_rqfcr[i] ==
  671. (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
  672. (priv->ftp_rqfpr[i] == cmp_rqfpr))
  673. break;
  674. }
  675. if (i == MAX_FILER_IDX + 1) {
  676. netdev_err(priv->ndev,
  677. "No parse rule found, can't create hash rules\n");
  678. ret = 0;
  679. goto err;
  680. }
  681. /* If a match was found, then it begins the starting of a cluster rule
  682. * if it was already programmed, we need to overwrite these rules
  683. */
  684. for (l = i+1; l < MAX_FILER_IDX; l++) {
  685. if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
  686. !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
  687. priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
  688. RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
  689. priv->ftp_rqfpr[l] = FPR_FILER_MASK;
  690. gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
  691. priv->ftp_rqfpr[l]);
  692. break;
  693. }
  694. if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
  695. (priv->ftp_rqfcr[l] & RQFCR_AND))
  696. continue;
  697. else {
  698. local_rqfpr[j] = priv->ftp_rqfpr[l];
  699. local_rqfcr[j] = priv->ftp_rqfcr[l];
  700. j--;
  701. }
  702. }
  703. priv->cur_filer_idx = l - 1;
  704. last_rule_idx = l;
  705. /* hash rules */
  706. ethflow_to_filer_rules(priv, ethflow);
  707. /* Write back the popped out rules again */
  708. for (k = j+1; k < MAX_FILER_IDX; k++) {
  709. priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
  710. priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
  711. gfar_write_filer(priv, priv->cur_filer_idx,
  712. local_rqfcr[k], local_rqfpr[k]);
  713. if (!priv->cur_filer_idx)
  714. break;
  715. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  716. }
  717. err:
  718. kfree(local_rqfcr);
  719. kfree(local_rqfpr);
  720. return ret;
  721. }
  722. static int gfar_set_hash_opts(struct gfar_private *priv,
  723. struct ethtool_rxnfc *cmd)
  724. {
  725. /* write the filer rules here */
  726. if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
  727. return -EINVAL;
  728. return 0;
  729. }
  730. static int gfar_check_filer_hardware(struct gfar_private *priv)
  731. {
  732. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  733. u32 i;
  734. /* Check if we are in FIFO mode */
  735. i = gfar_read(&regs->ecntrl);
  736. i &= ECNTRL_FIFM;
  737. if (i == ECNTRL_FIFM) {
  738. netdev_notice(priv->ndev, "Interface in FIFO mode\n");
  739. i = gfar_read(&regs->rctrl);
  740. i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
  741. if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
  742. netdev_info(priv->ndev,
  743. "Receive Queue Filtering enabled\n");
  744. } else {
  745. netdev_warn(priv->ndev,
  746. "Receive Queue Filtering disabled\n");
  747. return -EOPNOTSUPP;
  748. }
  749. }
  750. /* Or in standard mode */
  751. else {
  752. i = gfar_read(&regs->rctrl);
  753. i &= RCTRL_PRSDEP_MASK;
  754. if (i == RCTRL_PRSDEP_MASK) {
  755. netdev_info(priv->ndev,
  756. "Receive Queue Filtering enabled\n");
  757. } else {
  758. netdev_warn(priv->ndev,
  759. "Receive Queue Filtering disabled\n");
  760. return -EOPNOTSUPP;
  761. }
  762. }
  763. /* Sets the properties for arbitrary filer rule
  764. * to the first 4 Layer 4 Bytes
  765. */
  766. gfar_write(&regs->rbifx, 0xC0C1C2C3);
  767. return 0;
  768. }
  769. static int gfar_comp_asc(const void *a, const void *b)
  770. {
  771. return memcmp(a, b, 4);
  772. }
  773. static int gfar_comp_desc(const void *a, const void *b)
  774. {
  775. return -memcmp(a, b, 4);
  776. }
  777. static void gfar_swap(void *a, void *b, int size)
  778. {
  779. u32 *_a = a;
  780. u32 *_b = b;
  781. swap(_a[0], _b[0]);
  782. swap(_a[1], _b[1]);
  783. swap(_a[2], _b[2]);
  784. swap(_a[3], _b[3]);
  785. }
  786. /* Write a mask to filer cache */
  787. static void gfar_set_mask(u32 mask, struct filer_table *tab)
  788. {
  789. tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
  790. tab->fe[tab->index].prop = mask;
  791. tab->index++;
  792. }
  793. /* Sets parse bits (e.g. IP or TCP) */
  794. static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
  795. {
  796. gfar_set_mask(mask, tab);
  797. tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
  798. RQFCR_AND;
  799. tab->fe[tab->index].prop = value;
  800. tab->index++;
  801. }
  802. static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
  803. struct filer_table *tab)
  804. {
  805. gfar_set_mask(mask, tab);
  806. tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
  807. tab->fe[tab->index].prop = value;
  808. tab->index++;
  809. }
  810. /* For setting a tuple of value and mask of type flag
  811. * Example:
  812. * IP-Src = 10.0.0.0/255.0.0.0
  813. * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
  814. *
  815. * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
  816. * For a don't care mask it gives us a 0
  817. *
  818. * The check if don't care and the mask adjustment if mask=0 is done for VLAN
  819. * and MAC stuff on an upper level (due to missing information on this level).
  820. * For these guys we can discard them if they are value=0 and mask=0.
  821. *
  822. * Further the all masks are one-padded for better hardware efficiency.
  823. */
  824. static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
  825. struct filer_table *tab)
  826. {
  827. switch (flag) {
  828. /* 3bit */
  829. case RQFCR_PID_PRI:
  830. if (!(value | mask))
  831. return;
  832. mask |= RQFCR_PID_PRI_MASK;
  833. break;
  834. /* 8bit */
  835. case RQFCR_PID_L4P:
  836. case RQFCR_PID_TOS:
  837. if (!~(mask | RQFCR_PID_L4P_MASK))
  838. return;
  839. if (!mask)
  840. mask = ~0;
  841. else
  842. mask |= RQFCR_PID_L4P_MASK;
  843. break;
  844. /* 12bit */
  845. case RQFCR_PID_VID:
  846. if (!(value | mask))
  847. return;
  848. mask |= RQFCR_PID_VID_MASK;
  849. break;
  850. /* 16bit */
  851. case RQFCR_PID_DPT:
  852. case RQFCR_PID_SPT:
  853. case RQFCR_PID_ETY:
  854. if (!~(mask | RQFCR_PID_PORT_MASK))
  855. return;
  856. if (!mask)
  857. mask = ~0;
  858. else
  859. mask |= RQFCR_PID_PORT_MASK;
  860. break;
  861. /* 24bit */
  862. case RQFCR_PID_DAH:
  863. case RQFCR_PID_DAL:
  864. case RQFCR_PID_SAH:
  865. case RQFCR_PID_SAL:
  866. if (!(value | mask))
  867. return;
  868. mask |= RQFCR_PID_MAC_MASK;
  869. break;
  870. /* for all real 32bit masks */
  871. default:
  872. if (!~mask)
  873. return;
  874. if (!mask)
  875. mask = ~0;
  876. break;
  877. }
  878. gfar_set_general_attribute(value, mask, flag, tab);
  879. }
  880. /* Translates value and mask for UDP, TCP or SCTP */
  881. static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
  882. struct ethtool_tcpip4_spec *mask,
  883. struct filer_table *tab)
  884. {
  885. gfar_set_attribute(be32_to_cpu(value->ip4src),
  886. be32_to_cpu(mask->ip4src),
  887. RQFCR_PID_SIA, tab);
  888. gfar_set_attribute(be32_to_cpu(value->ip4dst),
  889. be32_to_cpu(mask->ip4dst),
  890. RQFCR_PID_DIA, tab);
  891. gfar_set_attribute(be16_to_cpu(value->pdst),
  892. be16_to_cpu(mask->pdst),
  893. RQFCR_PID_DPT, tab);
  894. gfar_set_attribute(be16_to_cpu(value->psrc),
  895. be16_to_cpu(mask->psrc),
  896. RQFCR_PID_SPT, tab);
  897. gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
  898. }
  899. /* Translates value and mask for RAW-IP4 */
  900. static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
  901. struct ethtool_usrip4_spec *mask,
  902. struct filer_table *tab)
  903. {
  904. gfar_set_attribute(be32_to_cpu(value->ip4src),
  905. be32_to_cpu(mask->ip4src),
  906. RQFCR_PID_SIA, tab);
  907. gfar_set_attribute(be32_to_cpu(value->ip4dst),
  908. be32_to_cpu(mask->ip4dst),
  909. RQFCR_PID_DIA, tab);
  910. gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
  911. gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
  912. gfar_set_attribute(be32_to_cpu(value->l4_4_bytes),
  913. be32_to_cpu(mask->l4_4_bytes),
  914. RQFCR_PID_ARB, tab);
  915. }
  916. /* Translates value and mask for ETHER spec */
  917. static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
  918. struct filer_table *tab)
  919. {
  920. u32 upper_temp_mask = 0;
  921. u32 lower_temp_mask = 0;
  922. /* Source address */
  923. if (!is_broadcast_ether_addr(mask->h_source)) {
  924. if (is_zero_ether_addr(mask->h_source)) {
  925. upper_temp_mask = 0xFFFFFFFF;
  926. lower_temp_mask = 0xFFFFFFFF;
  927. } else {
  928. upper_temp_mask = mask->h_source[0] << 16 |
  929. mask->h_source[1] << 8 |
  930. mask->h_source[2];
  931. lower_temp_mask = mask->h_source[3] << 16 |
  932. mask->h_source[4] << 8 |
  933. mask->h_source[5];
  934. }
  935. /* Upper 24bit */
  936. gfar_set_attribute(value->h_source[0] << 16 |
  937. value->h_source[1] << 8 |
  938. value->h_source[2],
  939. upper_temp_mask, RQFCR_PID_SAH, tab);
  940. /* And the same for the lower part */
  941. gfar_set_attribute(value->h_source[3] << 16 |
  942. value->h_source[4] << 8 |
  943. value->h_source[5],
  944. lower_temp_mask, RQFCR_PID_SAL, tab);
  945. }
  946. /* Destination address */
  947. if (!is_broadcast_ether_addr(mask->h_dest)) {
  948. /* Special for destination is limited broadcast */
  949. if ((is_broadcast_ether_addr(value->h_dest) &&
  950. is_zero_ether_addr(mask->h_dest))) {
  951. gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
  952. } else {
  953. if (is_zero_ether_addr(mask->h_dest)) {
  954. upper_temp_mask = 0xFFFFFFFF;
  955. lower_temp_mask = 0xFFFFFFFF;
  956. } else {
  957. upper_temp_mask = mask->h_dest[0] << 16 |
  958. mask->h_dest[1] << 8 |
  959. mask->h_dest[2];
  960. lower_temp_mask = mask->h_dest[3] << 16 |
  961. mask->h_dest[4] << 8 |
  962. mask->h_dest[5];
  963. }
  964. /* Upper 24bit */
  965. gfar_set_attribute(value->h_dest[0] << 16 |
  966. value->h_dest[1] << 8 |
  967. value->h_dest[2],
  968. upper_temp_mask, RQFCR_PID_DAH, tab);
  969. /* And the same for the lower part */
  970. gfar_set_attribute(value->h_dest[3] << 16 |
  971. value->h_dest[4] << 8 |
  972. value->h_dest[5],
  973. lower_temp_mask, RQFCR_PID_DAL, tab);
  974. }
  975. }
  976. gfar_set_attribute(be16_to_cpu(value->h_proto),
  977. be16_to_cpu(mask->h_proto),
  978. RQFCR_PID_ETY, tab);
  979. }
  980. static inline u32 vlan_tci_vid(struct ethtool_rx_flow_spec *rule)
  981. {
  982. return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_VID_MASK;
  983. }
  984. static inline u32 vlan_tci_vidm(struct ethtool_rx_flow_spec *rule)
  985. {
  986. return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_VID_MASK;
  987. }
  988. static inline u32 vlan_tci_cfi(struct ethtool_rx_flow_spec *rule)
  989. {
  990. return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_CFI_MASK;
  991. }
  992. static inline u32 vlan_tci_cfim(struct ethtool_rx_flow_spec *rule)
  993. {
  994. return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_CFI_MASK;
  995. }
  996. static inline u32 vlan_tci_prio(struct ethtool_rx_flow_spec *rule)
  997. {
  998. return (be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_PRIO_MASK) >>
  999. VLAN_PRIO_SHIFT;
  1000. }
  1001. static inline u32 vlan_tci_priom(struct ethtool_rx_flow_spec *rule)
  1002. {
  1003. return (be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_PRIO_MASK) >>
  1004. VLAN_PRIO_SHIFT;
  1005. }
  1006. /* Convert a rule to binary filter format of gianfar */
  1007. static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
  1008. struct filer_table *tab)
  1009. {
  1010. u32 vlan = 0, vlan_mask = 0;
  1011. u32 id = 0, id_mask = 0;
  1012. u32 cfi = 0, cfi_mask = 0;
  1013. u32 prio = 0, prio_mask = 0;
  1014. u32 old_index = tab->index;
  1015. /* Check if vlan is wanted */
  1016. if ((rule->flow_type & FLOW_EXT) &&
  1017. (rule->m_ext.vlan_tci != cpu_to_be16(0xFFFF))) {
  1018. if (!rule->m_ext.vlan_tci)
  1019. rule->m_ext.vlan_tci = cpu_to_be16(0xFFFF);
  1020. vlan = RQFPR_VLN;
  1021. vlan_mask = RQFPR_VLN;
  1022. /* Separate the fields */
  1023. id = vlan_tci_vid(rule);
  1024. id_mask = vlan_tci_vidm(rule);
  1025. cfi = vlan_tci_cfi(rule);
  1026. cfi_mask = vlan_tci_cfim(rule);
  1027. prio = vlan_tci_prio(rule);
  1028. prio_mask = vlan_tci_priom(rule);
  1029. if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
  1030. vlan |= RQFPR_CFI;
  1031. vlan_mask |= RQFPR_CFI;
  1032. } else if (cfi != VLAN_TAG_PRESENT &&
  1033. cfi_mask == VLAN_TAG_PRESENT) {
  1034. vlan_mask |= RQFPR_CFI;
  1035. }
  1036. }
  1037. switch (rule->flow_type & ~FLOW_EXT) {
  1038. case TCP_V4_FLOW:
  1039. gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
  1040. RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
  1041. gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
  1042. &rule->m_u.tcp_ip4_spec, tab);
  1043. break;
  1044. case UDP_V4_FLOW:
  1045. gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
  1046. RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
  1047. gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
  1048. &rule->m_u.udp_ip4_spec, tab);
  1049. break;
  1050. case SCTP_V4_FLOW:
  1051. gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
  1052. tab);
  1053. gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
  1054. gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
  1055. (struct ethtool_tcpip4_spec *)&rule->m_u,
  1056. tab);
  1057. break;
  1058. case IP_USER_FLOW:
  1059. gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
  1060. tab);
  1061. gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
  1062. (struct ethtool_usrip4_spec *) &rule->m_u,
  1063. tab);
  1064. break;
  1065. case ETHER_FLOW:
  1066. if (vlan)
  1067. gfar_set_parse_bits(vlan, vlan_mask, tab);
  1068. gfar_set_ether((struct ethhdr *) &rule->h_u,
  1069. (struct ethhdr *) &rule->m_u, tab);
  1070. break;
  1071. default:
  1072. return -1;
  1073. }
  1074. /* Set the vlan attributes in the end */
  1075. if (vlan) {
  1076. gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
  1077. gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
  1078. }
  1079. /* If there has been nothing written till now, it must be a default */
  1080. if (tab->index == old_index) {
  1081. gfar_set_mask(0xFFFFFFFF, tab);
  1082. tab->fe[tab->index].ctrl = 0x20;
  1083. tab->fe[tab->index].prop = 0x0;
  1084. tab->index++;
  1085. }
  1086. /* Remove last AND */
  1087. tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
  1088. /* Specify which queue to use or to drop */
  1089. if (rule->ring_cookie == RX_CLS_FLOW_DISC)
  1090. tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
  1091. else
  1092. tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
  1093. /* Only big enough entries can be clustered */
  1094. if (tab->index > (old_index + 2)) {
  1095. tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
  1096. tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
  1097. }
  1098. /* In rare cases the cache can be full while there is
  1099. * free space in hw
  1100. */
  1101. if (tab->index > MAX_FILER_CACHE_IDX - 1)
  1102. return -EBUSY;
  1103. return 0;
  1104. }
  1105. /* Copy size filer entries */
  1106. static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
  1107. struct gfar_filer_entry src[0], s32 size)
  1108. {
  1109. while (size > 0) {
  1110. size--;
  1111. dst[size].ctrl = src[size].ctrl;
  1112. dst[size].prop = src[size].prop;
  1113. }
  1114. }
  1115. /* Delete the contents of the filer-table between start and end
  1116. * and collapse them
  1117. */
  1118. static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
  1119. {
  1120. int length;
  1121. if (end > MAX_FILER_CACHE_IDX || end < begin)
  1122. return -EINVAL;
  1123. end++;
  1124. length = end - begin;
  1125. /* Copy */
  1126. while (end < tab->index) {
  1127. tab->fe[begin].ctrl = tab->fe[end].ctrl;
  1128. tab->fe[begin++].prop = tab->fe[end++].prop;
  1129. }
  1130. /* Fill up with don't cares */
  1131. while (begin < tab->index) {
  1132. tab->fe[begin].ctrl = 0x60;
  1133. tab->fe[begin].prop = 0xFFFFFFFF;
  1134. begin++;
  1135. }
  1136. tab->index -= length;
  1137. return 0;
  1138. }
  1139. /* Make space on the wanted location */
  1140. static int gfar_expand_filer_entries(u32 begin, u32 length,
  1141. struct filer_table *tab)
  1142. {
  1143. if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX ||
  1144. begin > MAX_FILER_CACHE_IDX)
  1145. return -EINVAL;
  1146. gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
  1147. tab->index - length + 1);
  1148. tab->index += length;
  1149. return 0;
  1150. }
  1151. static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
  1152. {
  1153. for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
  1154. start++) {
  1155. if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
  1156. (RQFCR_AND | RQFCR_CLE))
  1157. return start;
  1158. }
  1159. return -1;
  1160. }
  1161. static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
  1162. {
  1163. for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
  1164. start++) {
  1165. if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
  1166. (RQFCR_CLE))
  1167. return start;
  1168. }
  1169. return -1;
  1170. }
  1171. /* Uses hardwares clustering option to reduce
  1172. * the number of filer table entries
  1173. */
  1174. static void gfar_cluster_filer(struct filer_table *tab)
  1175. {
  1176. s32 i = -1, j, iend, jend;
  1177. while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
  1178. j = i;
  1179. while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
  1180. /* The cluster entries self and the previous one
  1181. * (a mask) must be identical!
  1182. */
  1183. if (tab->fe[i].ctrl != tab->fe[j].ctrl)
  1184. break;
  1185. if (tab->fe[i].prop != tab->fe[j].prop)
  1186. break;
  1187. if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
  1188. break;
  1189. if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
  1190. break;
  1191. iend = gfar_get_next_cluster_end(i, tab);
  1192. jend = gfar_get_next_cluster_end(j, tab);
  1193. if (jend == -1 || iend == -1)
  1194. break;
  1195. /* First we make some free space, where our cluster
  1196. * element should be. Then we copy it there and finally
  1197. * delete in from its old location.
  1198. */
  1199. if (gfar_expand_filer_entries(iend, (jend - j), tab) ==
  1200. -EINVAL)
  1201. break;
  1202. gfar_copy_filer_entries(&(tab->fe[iend + 1]),
  1203. &(tab->fe[jend + 1]), jend - j);
  1204. if (gfar_trim_filer_entries(jend - 1,
  1205. jend + (jend - j),
  1206. tab) == -EINVAL)
  1207. return;
  1208. /* Mask out cluster bit */
  1209. tab->fe[iend].ctrl &= ~(RQFCR_CLE);
  1210. }
  1211. }
  1212. }
  1213. /* Swaps the masked bits of a1<>a2 and b1<>b2 */
  1214. static void gfar_swap_bits(struct gfar_filer_entry *a1,
  1215. struct gfar_filer_entry *a2,
  1216. struct gfar_filer_entry *b1,
  1217. struct gfar_filer_entry *b2, u32 mask)
  1218. {
  1219. u32 temp[4];
  1220. temp[0] = a1->ctrl & mask;
  1221. temp[1] = a2->ctrl & mask;
  1222. temp[2] = b1->ctrl & mask;
  1223. temp[3] = b2->ctrl & mask;
  1224. a1->ctrl &= ~mask;
  1225. a2->ctrl &= ~mask;
  1226. b1->ctrl &= ~mask;
  1227. b2->ctrl &= ~mask;
  1228. a1->ctrl |= temp[1];
  1229. a2->ctrl |= temp[0];
  1230. b1->ctrl |= temp[3];
  1231. b2->ctrl |= temp[2];
  1232. }
  1233. /* Generate a list consisting of masks values with their start and
  1234. * end of validity and block as indicator for parts belonging
  1235. * together (glued by ANDs) in mask_table
  1236. */
  1237. static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
  1238. struct filer_table *tab)
  1239. {
  1240. u32 i, and_index = 0, block_index = 1;
  1241. for (i = 0; i < tab->index; i++) {
  1242. /* LSByte of control = 0 sets a mask */
  1243. if (!(tab->fe[i].ctrl & 0xF)) {
  1244. mask_table[and_index].mask = tab->fe[i].prop;
  1245. mask_table[and_index].start = i;
  1246. mask_table[and_index].block = block_index;
  1247. if (and_index >= 1)
  1248. mask_table[and_index - 1].end = i - 1;
  1249. and_index++;
  1250. }
  1251. /* cluster starts and ends will be separated because they should
  1252. * hold their position
  1253. */
  1254. if (tab->fe[i].ctrl & RQFCR_CLE)
  1255. block_index++;
  1256. /* A not set AND indicates the end of a depended block */
  1257. if (!(tab->fe[i].ctrl & RQFCR_AND))
  1258. block_index++;
  1259. }
  1260. mask_table[and_index - 1].end = i - 1;
  1261. return and_index;
  1262. }
  1263. /* Sorts the entries of mask_table by the values of the masks.
  1264. * Important: The 0xFF80 flags of the first and last entry of a
  1265. * block must hold their position (which queue, CLusterEnable, ReJEct,
  1266. * AND)
  1267. */
  1268. static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
  1269. struct filer_table *temp_table, u32 and_index)
  1270. {
  1271. /* Pointer to compare function (_asc or _desc) */
  1272. int (*gfar_comp)(const void *, const void *);
  1273. u32 i, size = 0, start = 0, prev = 1;
  1274. u32 old_first, old_last, new_first, new_last;
  1275. gfar_comp = &gfar_comp_desc;
  1276. for (i = 0; i < and_index; i++) {
  1277. if (prev != mask_table[i].block) {
  1278. old_first = mask_table[start].start + 1;
  1279. old_last = mask_table[i - 1].end;
  1280. sort(mask_table + start, size,
  1281. sizeof(struct gfar_mask_entry),
  1282. gfar_comp, &gfar_swap);
  1283. /* Toggle order for every block. This makes the
  1284. * thing more efficient!
  1285. */
  1286. if (gfar_comp == gfar_comp_desc)
  1287. gfar_comp = &gfar_comp_asc;
  1288. else
  1289. gfar_comp = &gfar_comp_desc;
  1290. new_first = mask_table[start].start + 1;
  1291. new_last = mask_table[i - 1].end;
  1292. gfar_swap_bits(&temp_table->fe[new_first],
  1293. &temp_table->fe[old_first],
  1294. &temp_table->fe[new_last],
  1295. &temp_table->fe[old_last],
  1296. RQFCR_QUEUE | RQFCR_CLE |
  1297. RQFCR_RJE | RQFCR_AND);
  1298. start = i;
  1299. size = 0;
  1300. }
  1301. size++;
  1302. prev = mask_table[i].block;
  1303. }
  1304. }
  1305. /* Reduces the number of masks needed in the filer table to save entries
  1306. * This is done by sorting the masks of a depended block. A depended block is
  1307. * identified by gluing ANDs or CLE. The sorting order toggles after every
  1308. * block. Of course entries in scope of a mask must change their location with
  1309. * it.
  1310. */
  1311. static int gfar_optimize_filer_masks(struct filer_table *tab)
  1312. {
  1313. struct filer_table *temp_table;
  1314. struct gfar_mask_entry *mask_table;
  1315. u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
  1316. s32 ret = 0;
  1317. /* We need a copy of the filer table because
  1318. * we want to change its order
  1319. */
  1320. temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
  1321. if (temp_table == NULL)
  1322. return -ENOMEM;
  1323. mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
  1324. sizeof(struct gfar_mask_entry), GFP_KERNEL);
  1325. if (mask_table == NULL) {
  1326. ret = -ENOMEM;
  1327. goto end;
  1328. }
  1329. and_index = gfar_generate_mask_table(mask_table, tab);
  1330. gfar_sort_mask_table(mask_table, temp_table, and_index);
  1331. /* Now we can copy the data from our duplicated filer table to
  1332. * the real one in the order the mask table says
  1333. */
  1334. for (i = 0; i < and_index; i++) {
  1335. size = mask_table[i].end - mask_table[i].start + 1;
  1336. gfar_copy_filer_entries(&(tab->fe[j]),
  1337. &(temp_table->fe[mask_table[i].start]), size);
  1338. j += size;
  1339. }
  1340. /* And finally we just have to check for duplicated masks and drop the
  1341. * second ones
  1342. */
  1343. for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
  1344. if (tab->fe[i].ctrl == 0x80) {
  1345. previous_mask = i++;
  1346. break;
  1347. }
  1348. }
  1349. for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
  1350. if (tab->fe[i].ctrl == 0x80) {
  1351. if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
  1352. /* Two identical ones found!
  1353. * So drop the second one!
  1354. */
  1355. gfar_trim_filer_entries(i, i, tab);
  1356. } else
  1357. /* Not identical! */
  1358. previous_mask = i;
  1359. }
  1360. }
  1361. kfree(mask_table);
  1362. end: kfree(temp_table);
  1363. return ret;
  1364. }
  1365. /* Write the bit-pattern from software's buffer to hardware registers */
  1366. static int gfar_write_filer_table(struct gfar_private *priv,
  1367. struct filer_table *tab)
  1368. {
  1369. u32 i = 0;
  1370. if (tab->index > MAX_FILER_IDX - 1)
  1371. return -EBUSY;
  1372. /* Fill regular entries */
  1373. for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].ctrl);
  1374. i++)
  1375. gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
  1376. /* Fill the rest with fall-troughs */
  1377. for (; i < MAX_FILER_IDX - 1; i++)
  1378. gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
  1379. /* Last entry must be default accept
  1380. * because that's what people expect
  1381. */
  1382. gfar_write_filer(priv, i, 0x20, 0x0);
  1383. return 0;
  1384. }
  1385. static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
  1386. struct gfar_private *priv)
  1387. {
  1388. if (flow->flow_type & FLOW_EXT) {
  1389. if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
  1390. netdev_warn(priv->ndev,
  1391. "User-specific data not supported!\n");
  1392. if (~flow->m_ext.vlan_etype)
  1393. netdev_warn(priv->ndev,
  1394. "VLAN-etype not supported!\n");
  1395. }
  1396. if (flow->flow_type == IP_USER_FLOW)
  1397. if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
  1398. netdev_warn(priv->ndev,
  1399. "IP-Version differing from IPv4 not supported!\n");
  1400. return 0;
  1401. }
  1402. static int gfar_process_filer_changes(struct gfar_private *priv)
  1403. {
  1404. struct ethtool_flow_spec_container *j;
  1405. struct filer_table *tab;
  1406. s32 i = 0;
  1407. s32 ret = 0;
  1408. /* So index is set to zero, too! */
  1409. tab = kzalloc(sizeof(*tab), GFP_KERNEL);
  1410. if (tab == NULL)
  1411. return -ENOMEM;
  1412. /* Now convert the existing filer data from flow_spec into
  1413. * filer tables binary format
  1414. */
  1415. list_for_each_entry(j, &priv->rx_list.list, list) {
  1416. ret = gfar_convert_to_filer(&j->fs, tab);
  1417. if (ret == -EBUSY) {
  1418. netdev_err(priv->ndev,
  1419. "Rule not added: No free space!\n");
  1420. goto end;
  1421. }
  1422. if (ret == -1) {
  1423. netdev_err(priv->ndev,
  1424. "Rule not added: Unsupported Flow-type!\n");
  1425. goto end;
  1426. }
  1427. }
  1428. i = tab->index;
  1429. /* Optimizations to save entries */
  1430. gfar_cluster_filer(tab);
  1431. gfar_optimize_filer_masks(tab);
  1432. pr_debug("\tSummary:\n"
  1433. "\tData on hardware: %d\n"
  1434. "\tCompression rate: %d%%\n",
  1435. tab->index, 100 - (100 * tab->index) / i);
  1436. /* Write everything to hardware */
  1437. ret = gfar_write_filer_table(priv, tab);
  1438. if (ret == -EBUSY) {
  1439. netdev_err(priv->ndev, "Rule not added: No free space!\n");
  1440. goto end;
  1441. }
  1442. end:
  1443. kfree(tab);
  1444. return ret;
  1445. }
  1446. static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
  1447. {
  1448. u32 i = 0;
  1449. for (i = 0; i < sizeof(flow->m_u); i++)
  1450. flow->m_u.hdata[i] ^= 0xFF;
  1451. flow->m_ext.vlan_etype ^= cpu_to_be16(0xFFFF);
  1452. flow->m_ext.vlan_tci ^= cpu_to_be16(0xFFFF);
  1453. flow->m_ext.data[0] ^= cpu_to_be32(~0);
  1454. flow->m_ext.data[1] ^= cpu_to_be32(~0);
  1455. }
  1456. static int gfar_add_cls(struct gfar_private *priv,
  1457. struct ethtool_rx_flow_spec *flow)
  1458. {
  1459. struct ethtool_flow_spec_container *temp, *comp;
  1460. int ret = 0;
  1461. temp = kmalloc(sizeof(*temp), GFP_KERNEL);
  1462. if (temp == NULL)
  1463. return -ENOMEM;
  1464. memcpy(&temp->fs, flow, sizeof(temp->fs));
  1465. gfar_invert_masks(&temp->fs);
  1466. ret = gfar_check_capability(&temp->fs, priv);
  1467. if (ret)
  1468. goto clean_mem;
  1469. /* Link in the new element at the right @location */
  1470. if (list_empty(&priv->rx_list.list)) {
  1471. ret = gfar_check_filer_hardware(priv);
  1472. if (ret != 0)
  1473. goto clean_mem;
  1474. list_add(&temp->list, &priv->rx_list.list);
  1475. goto process;
  1476. } else {
  1477. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1478. if (comp->fs.location > flow->location) {
  1479. list_add_tail(&temp->list, &comp->list);
  1480. goto process;
  1481. }
  1482. if (comp->fs.location == flow->location) {
  1483. netdev_err(priv->ndev,
  1484. "Rule not added: ID %d not free!\n",
  1485. flow->location);
  1486. ret = -EBUSY;
  1487. goto clean_mem;
  1488. }
  1489. }
  1490. list_add_tail(&temp->list, &priv->rx_list.list);
  1491. }
  1492. process:
  1493. ret = gfar_process_filer_changes(priv);
  1494. if (ret)
  1495. goto clean_list;
  1496. priv->rx_list.count++;
  1497. return ret;
  1498. clean_list:
  1499. list_del(&temp->list);
  1500. clean_mem:
  1501. kfree(temp);
  1502. return ret;
  1503. }
  1504. static int gfar_del_cls(struct gfar_private *priv, u32 loc)
  1505. {
  1506. struct ethtool_flow_spec_container *comp;
  1507. u32 ret = -EINVAL;
  1508. if (list_empty(&priv->rx_list.list))
  1509. return ret;
  1510. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1511. if (comp->fs.location == loc) {
  1512. list_del(&comp->list);
  1513. kfree(comp);
  1514. priv->rx_list.count--;
  1515. gfar_process_filer_changes(priv);
  1516. ret = 0;
  1517. break;
  1518. }
  1519. }
  1520. return ret;
  1521. }
  1522. static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
  1523. {
  1524. struct ethtool_flow_spec_container *comp;
  1525. u32 ret = -EINVAL;
  1526. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1527. if (comp->fs.location == cmd->fs.location) {
  1528. memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
  1529. gfar_invert_masks(&cmd->fs);
  1530. ret = 0;
  1531. break;
  1532. }
  1533. }
  1534. return ret;
  1535. }
  1536. static int gfar_get_cls_all(struct gfar_private *priv,
  1537. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  1538. {
  1539. struct ethtool_flow_spec_container *comp;
  1540. u32 i = 0;
  1541. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1542. if (i == cmd->rule_cnt)
  1543. return -EMSGSIZE;
  1544. rule_locs[i] = comp->fs.location;
  1545. i++;
  1546. }
  1547. cmd->data = MAX_FILER_IDX;
  1548. cmd->rule_cnt = i;
  1549. return 0;
  1550. }
  1551. static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
  1552. {
  1553. struct gfar_private *priv = netdev_priv(dev);
  1554. int ret = 0;
  1555. if (test_bit(GFAR_RESETTING, &priv->state))
  1556. return -EBUSY;
  1557. mutex_lock(&priv->rx_queue_access);
  1558. switch (cmd->cmd) {
  1559. case ETHTOOL_SRXFH:
  1560. ret = gfar_set_hash_opts(priv, cmd);
  1561. break;
  1562. case ETHTOOL_SRXCLSRLINS:
  1563. if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
  1564. cmd->fs.ring_cookie >= priv->num_rx_queues) ||
  1565. cmd->fs.location >= MAX_FILER_IDX) {
  1566. ret = -EINVAL;
  1567. break;
  1568. }
  1569. ret = gfar_add_cls(priv, &cmd->fs);
  1570. break;
  1571. case ETHTOOL_SRXCLSRLDEL:
  1572. ret = gfar_del_cls(priv, cmd->fs.location);
  1573. break;
  1574. default:
  1575. ret = -EINVAL;
  1576. }
  1577. mutex_unlock(&priv->rx_queue_access);
  1578. return ret;
  1579. }
  1580. static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
  1581. u32 *rule_locs)
  1582. {
  1583. struct gfar_private *priv = netdev_priv(dev);
  1584. int ret = 0;
  1585. switch (cmd->cmd) {
  1586. case ETHTOOL_GRXRINGS:
  1587. cmd->data = priv->num_rx_queues;
  1588. break;
  1589. case ETHTOOL_GRXCLSRLCNT:
  1590. cmd->rule_cnt = priv->rx_list.count;
  1591. break;
  1592. case ETHTOOL_GRXCLSRULE:
  1593. ret = gfar_get_cls(priv, cmd);
  1594. break;
  1595. case ETHTOOL_GRXCLSRLALL:
  1596. ret = gfar_get_cls_all(priv, cmd, rule_locs);
  1597. break;
  1598. default:
  1599. ret = -EINVAL;
  1600. break;
  1601. }
  1602. return ret;
  1603. }
  1604. int gfar_phc_index = -1;
  1605. EXPORT_SYMBOL(gfar_phc_index);
  1606. static int gfar_get_ts_info(struct net_device *dev,
  1607. struct ethtool_ts_info *info)
  1608. {
  1609. struct gfar_private *priv = netdev_priv(dev);
  1610. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
  1611. info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
  1612. SOF_TIMESTAMPING_SOFTWARE;
  1613. info->phc_index = -1;
  1614. return 0;
  1615. }
  1616. info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
  1617. SOF_TIMESTAMPING_RX_HARDWARE |
  1618. SOF_TIMESTAMPING_RAW_HARDWARE;
  1619. info->phc_index = gfar_phc_index;
  1620. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  1621. (1 << HWTSTAMP_TX_ON);
  1622. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  1623. (1 << HWTSTAMP_FILTER_ALL);
  1624. return 0;
  1625. }
  1626. const struct ethtool_ops gfar_ethtool_ops = {
  1627. .get_settings = gfar_gsettings,
  1628. .set_settings = gfar_ssettings,
  1629. .get_drvinfo = gfar_gdrvinfo,
  1630. .get_regs_len = gfar_reglen,
  1631. .get_regs = gfar_get_regs,
  1632. .get_link = ethtool_op_get_link,
  1633. .get_coalesce = gfar_gcoalesce,
  1634. .set_coalesce = gfar_scoalesce,
  1635. .get_ringparam = gfar_gringparam,
  1636. .set_ringparam = gfar_sringparam,
  1637. .get_pauseparam = gfar_gpauseparam,
  1638. .set_pauseparam = gfar_spauseparam,
  1639. .get_strings = gfar_gstrings,
  1640. .get_sset_count = gfar_sset_count,
  1641. .get_ethtool_stats = gfar_fill_stats,
  1642. .get_msglevel = gfar_get_msglevel,
  1643. .set_msglevel = gfar_set_msglevel,
  1644. #ifdef CONFIG_PM
  1645. .get_wol = gfar_get_wol,
  1646. .set_wol = gfar_set_wol,
  1647. #endif
  1648. .set_rxnfc = gfar_set_nfc,
  1649. .get_rxnfc = gfar_get_nfc,
  1650. .get_ts_info = gfar_get_ts_info,
  1651. };