gianfar_ethtool.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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. priv->tx_actual_en = 0;
  497. if (priv->tx_pause_en) {
  498. priv->tx_actual_en = 1;
  499. tempval |= MACCFG1_TX_FLOW;
  500. }
  501. if (priv->rx_pause_en)
  502. tempval |= MACCFG1_RX_FLOW;
  503. gfar_write(&regs->maccfg1, tempval);
  504. }
  505. }
  506. return 0;
  507. }
  508. int gfar_set_features(struct net_device *dev, netdev_features_t features)
  509. {
  510. netdev_features_t changed = dev->features ^ features;
  511. struct gfar_private *priv = netdev_priv(dev);
  512. int err = 0;
  513. if (!(changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  514. NETIF_F_RXCSUM)))
  515. return 0;
  516. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  517. cpu_relax();
  518. dev->features = features;
  519. if (dev->flags & IFF_UP) {
  520. /* Now we take down the rings to rebuild them */
  521. stop_gfar(dev);
  522. err = startup_gfar(dev);
  523. } else {
  524. gfar_mac_reset(priv);
  525. }
  526. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  527. return err;
  528. }
  529. static uint32_t gfar_get_msglevel(struct net_device *dev)
  530. {
  531. struct gfar_private *priv = netdev_priv(dev);
  532. return priv->msg_enable;
  533. }
  534. static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
  535. {
  536. struct gfar_private *priv = netdev_priv(dev);
  537. priv->msg_enable = data;
  538. }
  539. #ifdef CONFIG_PM
  540. static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  541. {
  542. struct gfar_private *priv = netdev_priv(dev);
  543. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
  544. wol->supported = WAKE_MAGIC;
  545. wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
  546. } else {
  547. wol->supported = wol->wolopts = 0;
  548. }
  549. }
  550. static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  551. {
  552. struct gfar_private *priv = netdev_priv(dev);
  553. unsigned long flags;
  554. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
  555. wol->wolopts != 0)
  556. return -EINVAL;
  557. if (wol->wolopts & ~WAKE_MAGIC)
  558. return -EINVAL;
  559. device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
  560. spin_lock_irqsave(&priv->bflock, flags);
  561. priv->wol_en = !!device_may_wakeup(&dev->dev);
  562. spin_unlock_irqrestore(&priv->bflock, flags);
  563. return 0;
  564. }
  565. #endif
  566. static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
  567. {
  568. u32 fcr = 0x0, fpr = FPR_FILER_MASK;
  569. if (ethflow & RXH_L2DA) {
  570. fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
  571. RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
  572. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  573. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  574. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  575. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  576. fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
  577. RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
  578. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  579. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  580. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  581. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  582. }
  583. if (ethflow & RXH_VLAN) {
  584. fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  585. RQFCR_AND | RQFCR_HASHTBL_0;
  586. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  587. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  588. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  589. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  590. }
  591. if (ethflow & RXH_IP_SRC) {
  592. fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  593. RQFCR_AND | RQFCR_HASHTBL_0;
  594. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  595. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  596. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  597. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  598. }
  599. if (ethflow & (RXH_IP_DST)) {
  600. fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  601. RQFCR_AND | RQFCR_HASHTBL_0;
  602. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  603. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  604. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  605. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  606. }
  607. if (ethflow & RXH_L3_PROTO) {
  608. fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  609. RQFCR_AND | RQFCR_HASHTBL_0;
  610. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  611. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  612. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  613. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  614. }
  615. if (ethflow & RXH_L4_B_0_1) {
  616. fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  617. RQFCR_AND | RQFCR_HASHTBL_0;
  618. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  619. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  620. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  621. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  622. }
  623. if (ethflow & RXH_L4_B_2_3) {
  624. fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
  625. RQFCR_AND | RQFCR_HASHTBL_0;
  626. priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
  627. priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
  628. gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
  629. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  630. }
  631. }
  632. static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
  633. u64 class)
  634. {
  635. unsigned int last_rule_idx = priv->cur_filer_idx;
  636. unsigned int cmp_rqfpr;
  637. unsigned int *local_rqfpr;
  638. unsigned int *local_rqfcr;
  639. int i = 0x0, k = 0x0;
  640. int j = MAX_FILER_IDX, l = 0x0;
  641. int ret = 1;
  642. local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
  643. GFP_KERNEL);
  644. local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
  645. GFP_KERNEL);
  646. if (!local_rqfpr || !local_rqfcr) {
  647. ret = 0;
  648. goto err;
  649. }
  650. switch (class) {
  651. case TCP_V4_FLOW:
  652. cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
  653. break;
  654. case UDP_V4_FLOW:
  655. cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
  656. break;
  657. case TCP_V6_FLOW:
  658. cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
  659. break;
  660. case UDP_V6_FLOW:
  661. cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
  662. break;
  663. default:
  664. netdev_err(priv->ndev,
  665. "Right now this class is not supported\n");
  666. ret = 0;
  667. goto err;
  668. }
  669. for (i = 0; i < MAX_FILER_IDX + 1; i++) {
  670. local_rqfpr[j] = priv->ftp_rqfpr[i];
  671. local_rqfcr[j] = priv->ftp_rqfcr[i];
  672. j--;
  673. if ((priv->ftp_rqfcr[i] ==
  674. (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
  675. (priv->ftp_rqfpr[i] == cmp_rqfpr))
  676. break;
  677. }
  678. if (i == MAX_FILER_IDX + 1) {
  679. netdev_err(priv->ndev,
  680. "No parse rule found, can't create hash rules\n");
  681. ret = 0;
  682. goto err;
  683. }
  684. /* If a match was found, then it begins the starting of a cluster rule
  685. * if it was already programmed, we need to overwrite these rules
  686. */
  687. for (l = i+1; l < MAX_FILER_IDX; l++) {
  688. if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
  689. !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
  690. priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
  691. RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
  692. priv->ftp_rqfpr[l] = FPR_FILER_MASK;
  693. gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
  694. priv->ftp_rqfpr[l]);
  695. break;
  696. }
  697. if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
  698. (priv->ftp_rqfcr[l] & RQFCR_AND))
  699. continue;
  700. else {
  701. local_rqfpr[j] = priv->ftp_rqfpr[l];
  702. local_rqfcr[j] = priv->ftp_rqfcr[l];
  703. j--;
  704. }
  705. }
  706. priv->cur_filer_idx = l - 1;
  707. last_rule_idx = l;
  708. /* hash rules */
  709. ethflow_to_filer_rules(priv, ethflow);
  710. /* Write back the popped out rules again */
  711. for (k = j+1; k < MAX_FILER_IDX; k++) {
  712. priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
  713. priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
  714. gfar_write_filer(priv, priv->cur_filer_idx,
  715. local_rqfcr[k], local_rqfpr[k]);
  716. if (!priv->cur_filer_idx)
  717. break;
  718. priv->cur_filer_idx = priv->cur_filer_idx - 1;
  719. }
  720. err:
  721. kfree(local_rqfcr);
  722. kfree(local_rqfpr);
  723. return ret;
  724. }
  725. static int gfar_set_hash_opts(struct gfar_private *priv,
  726. struct ethtool_rxnfc *cmd)
  727. {
  728. /* write the filer rules here */
  729. if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
  730. return -EINVAL;
  731. return 0;
  732. }
  733. static int gfar_check_filer_hardware(struct gfar_private *priv)
  734. {
  735. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  736. u32 i;
  737. /* Check if we are in FIFO mode */
  738. i = gfar_read(&regs->ecntrl);
  739. i &= ECNTRL_FIFM;
  740. if (i == ECNTRL_FIFM) {
  741. netdev_notice(priv->ndev, "Interface in FIFO mode\n");
  742. i = gfar_read(&regs->rctrl);
  743. i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
  744. if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
  745. netdev_info(priv->ndev,
  746. "Receive Queue Filtering enabled\n");
  747. } else {
  748. netdev_warn(priv->ndev,
  749. "Receive Queue Filtering disabled\n");
  750. return -EOPNOTSUPP;
  751. }
  752. }
  753. /* Or in standard mode */
  754. else {
  755. i = gfar_read(&regs->rctrl);
  756. i &= RCTRL_PRSDEP_MASK;
  757. if (i == RCTRL_PRSDEP_MASK) {
  758. netdev_info(priv->ndev,
  759. "Receive Queue Filtering enabled\n");
  760. } else {
  761. netdev_warn(priv->ndev,
  762. "Receive Queue Filtering disabled\n");
  763. return -EOPNOTSUPP;
  764. }
  765. }
  766. /* Sets the properties for arbitrary filer rule
  767. * to the first 4 Layer 4 Bytes
  768. */
  769. gfar_write(&regs->rbifx, 0xC0C1C2C3);
  770. return 0;
  771. }
  772. static int gfar_comp_asc(const void *a, const void *b)
  773. {
  774. return memcmp(a, b, 4);
  775. }
  776. static int gfar_comp_desc(const void *a, const void *b)
  777. {
  778. return -memcmp(a, b, 4);
  779. }
  780. static void gfar_swap(void *a, void *b, int size)
  781. {
  782. u32 *_a = a;
  783. u32 *_b = b;
  784. swap(_a[0], _b[0]);
  785. swap(_a[1], _b[1]);
  786. swap(_a[2], _b[2]);
  787. swap(_a[3], _b[3]);
  788. }
  789. /* Write a mask to filer cache */
  790. static void gfar_set_mask(u32 mask, struct filer_table *tab)
  791. {
  792. tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
  793. tab->fe[tab->index].prop = mask;
  794. tab->index++;
  795. }
  796. /* Sets parse bits (e.g. IP or TCP) */
  797. static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
  798. {
  799. gfar_set_mask(mask, tab);
  800. tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
  801. RQFCR_AND;
  802. tab->fe[tab->index].prop = value;
  803. tab->index++;
  804. }
  805. static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
  806. struct filer_table *tab)
  807. {
  808. gfar_set_mask(mask, tab);
  809. tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
  810. tab->fe[tab->index].prop = value;
  811. tab->index++;
  812. }
  813. /* For setting a tuple of value and mask of type flag
  814. * Example:
  815. * IP-Src = 10.0.0.0/255.0.0.0
  816. * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
  817. *
  818. * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
  819. * For a don't care mask it gives us a 0
  820. *
  821. * The check if don't care and the mask adjustment if mask=0 is done for VLAN
  822. * and MAC stuff on an upper level (due to missing information on this level).
  823. * For these guys we can discard them if they are value=0 and mask=0.
  824. *
  825. * Further the all masks are one-padded for better hardware efficiency.
  826. */
  827. static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
  828. struct filer_table *tab)
  829. {
  830. switch (flag) {
  831. /* 3bit */
  832. case RQFCR_PID_PRI:
  833. if (!(value | mask))
  834. return;
  835. mask |= RQFCR_PID_PRI_MASK;
  836. break;
  837. /* 8bit */
  838. case RQFCR_PID_L4P:
  839. case RQFCR_PID_TOS:
  840. if (!~(mask | RQFCR_PID_L4P_MASK))
  841. return;
  842. if (!mask)
  843. mask = ~0;
  844. else
  845. mask |= RQFCR_PID_L4P_MASK;
  846. break;
  847. /* 12bit */
  848. case RQFCR_PID_VID:
  849. if (!(value | mask))
  850. return;
  851. mask |= RQFCR_PID_VID_MASK;
  852. break;
  853. /* 16bit */
  854. case RQFCR_PID_DPT:
  855. case RQFCR_PID_SPT:
  856. case RQFCR_PID_ETY:
  857. if (!~(mask | RQFCR_PID_PORT_MASK))
  858. return;
  859. if (!mask)
  860. mask = ~0;
  861. else
  862. mask |= RQFCR_PID_PORT_MASK;
  863. break;
  864. /* 24bit */
  865. case RQFCR_PID_DAH:
  866. case RQFCR_PID_DAL:
  867. case RQFCR_PID_SAH:
  868. case RQFCR_PID_SAL:
  869. if (!(value | mask))
  870. return;
  871. mask |= RQFCR_PID_MAC_MASK;
  872. break;
  873. /* for all real 32bit masks */
  874. default:
  875. if (!~mask)
  876. return;
  877. if (!mask)
  878. mask = ~0;
  879. break;
  880. }
  881. gfar_set_general_attribute(value, mask, flag, tab);
  882. }
  883. /* Translates value and mask for UDP, TCP or SCTP */
  884. static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
  885. struct ethtool_tcpip4_spec *mask,
  886. struct filer_table *tab)
  887. {
  888. gfar_set_attribute(be32_to_cpu(value->ip4src),
  889. be32_to_cpu(mask->ip4src),
  890. RQFCR_PID_SIA, tab);
  891. gfar_set_attribute(be32_to_cpu(value->ip4dst),
  892. be32_to_cpu(mask->ip4dst),
  893. RQFCR_PID_DIA, tab);
  894. gfar_set_attribute(be16_to_cpu(value->pdst),
  895. be16_to_cpu(mask->pdst),
  896. RQFCR_PID_DPT, tab);
  897. gfar_set_attribute(be16_to_cpu(value->psrc),
  898. be16_to_cpu(mask->psrc),
  899. RQFCR_PID_SPT, tab);
  900. gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
  901. }
  902. /* Translates value and mask for RAW-IP4 */
  903. static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
  904. struct ethtool_usrip4_spec *mask,
  905. struct filer_table *tab)
  906. {
  907. gfar_set_attribute(be32_to_cpu(value->ip4src),
  908. be32_to_cpu(mask->ip4src),
  909. RQFCR_PID_SIA, tab);
  910. gfar_set_attribute(be32_to_cpu(value->ip4dst),
  911. be32_to_cpu(mask->ip4dst),
  912. RQFCR_PID_DIA, tab);
  913. gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
  914. gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
  915. gfar_set_attribute(be32_to_cpu(value->l4_4_bytes),
  916. be32_to_cpu(mask->l4_4_bytes),
  917. RQFCR_PID_ARB, tab);
  918. }
  919. /* Translates value and mask for ETHER spec */
  920. static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
  921. struct filer_table *tab)
  922. {
  923. u32 upper_temp_mask = 0;
  924. u32 lower_temp_mask = 0;
  925. /* Source address */
  926. if (!is_broadcast_ether_addr(mask->h_source)) {
  927. if (is_zero_ether_addr(mask->h_source)) {
  928. upper_temp_mask = 0xFFFFFFFF;
  929. lower_temp_mask = 0xFFFFFFFF;
  930. } else {
  931. upper_temp_mask = mask->h_source[0] << 16 |
  932. mask->h_source[1] << 8 |
  933. mask->h_source[2];
  934. lower_temp_mask = mask->h_source[3] << 16 |
  935. mask->h_source[4] << 8 |
  936. mask->h_source[5];
  937. }
  938. /* Upper 24bit */
  939. gfar_set_attribute(value->h_source[0] << 16 |
  940. value->h_source[1] << 8 |
  941. value->h_source[2],
  942. upper_temp_mask, RQFCR_PID_SAH, tab);
  943. /* And the same for the lower part */
  944. gfar_set_attribute(value->h_source[3] << 16 |
  945. value->h_source[4] << 8 |
  946. value->h_source[5],
  947. lower_temp_mask, RQFCR_PID_SAL, tab);
  948. }
  949. /* Destination address */
  950. if (!is_broadcast_ether_addr(mask->h_dest)) {
  951. /* Special for destination is limited broadcast */
  952. if ((is_broadcast_ether_addr(value->h_dest) &&
  953. is_zero_ether_addr(mask->h_dest))) {
  954. gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
  955. } else {
  956. if (is_zero_ether_addr(mask->h_dest)) {
  957. upper_temp_mask = 0xFFFFFFFF;
  958. lower_temp_mask = 0xFFFFFFFF;
  959. } else {
  960. upper_temp_mask = mask->h_dest[0] << 16 |
  961. mask->h_dest[1] << 8 |
  962. mask->h_dest[2];
  963. lower_temp_mask = mask->h_dest[3] << 16 |
  964. mask->h_dest[4] << 8 |
  965. mask->h_dest[5];
  966. }
  967. /* Upper 24bit */
  968. gfar_set_attribute(value->h_dest[0] << 16 |
  969. value->h_dest[1] << 8 |
  970. value->h_dest[2],
  971. upper_temp_mask, RQFCR_PID_DAH, tab);
  972. /* And the same for the lower part */
  973. gfar_set_attribute(value->h_dest[3] << 16 |
  974. value->h_dest[4] << 8 |
  975. value->h_dest[5],
  976. lower_temp_mask, RQFCR_PID_DAL, tab);
  977. }
  978. }
  979. gfar_set_attribute(be16_to_cpu(value->h_proto),
  980. be16_to_cpu(mask->h_proto),
  981. RQFCR_PID_ETY, tab);
  982. }
  983. static inline u32 vlan_tci_vid(struct ethtool_rx_flow_spec *rule)
  984. {
  985. return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_VID_MASK;
  986. }
  987. static inline u32 vlan_tci_vidm(struct ethtool_rx_flow_spec *rule)
  988. {
  989. return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_VID_MASK;
  990. }
  991. static inline u32 vlan_tci_cfi(struct ethtool_rx_flow_spec *rule)
  992. {
  993. return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_CFI_MASK;
  994. }
  995. static inline u32 vlan_tci_cfim(struct ethtool_rx_flow_spec *rule)
  996. {
  997. return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_CFI_MASK;
  998. }
  999. static inline u32 vlan_tci_prio(struct ethtool_rx_flow_spec *rule)
  1000. {
  1001. return (be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_PRIO_MASK) >>
  1002. VLAN_PRIO_SHIFT;
  1003. }
  1004. static inline u32 vlan_tci_priom(struct ethtool_rx_flow_spec *rule)
  1005. {
  1006. return (be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_PRIO_MASK) >>
  1007. VLAN_PRIO_SHIFT;
  1008. }
  1009. /* Convert a rule to binary filter format of gianfar */
  1010. static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
  1011. struct filer_table *tab)
  1012. {
  1013. u32 vlan = 0, vlan_mask = 0;
  1014. u32 id = 0, id_mask = 0;
  1015. u32 cfi = 0, cfi_mask = 0;
  1016. u32 prio = 0, prio_mask = 0;
  1017. u32 old_index = tab->index;
  1018. /* Check if vlan is wanted */
  1019. if ((rule->flow_type & FLOW_EXT) &&
  1020. (rule->m_ext.vlan_tci != cpu_to_be16(0xFFFF))) {
  1021. if (!rule->m_ext.vlan_tci)
  1022. rule->m_ext.vlan_tci = cpu_to_be16(0xFFFF);
  1023. vlan = RQFPR_VLN;
  1024. vlan_mask = RQFPR_VLN;
  1025. /* Separate the fields */
  1026. id = vlan_tci_vid(rule);
  1027. id_mask = vlan_tci_vidm(rule);
  1028. cfi = vlan_tci_cfi(rule);
  1029. cfi_mask = vlan_tci_cfim(rule);
  1030. prio = vlan_tci_prio(rule);
  1031. prio_mask = vlan_tci_priom(rule);
  1032. if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
  1033. vlan |= RQFPR_CFI;
  1034. vlan_mask |= RQFPR_CFI;
  1035. } else if (cfi != VLAN_TAG_PRESENT &&
  1036. cfi_mask == VLAN_TAG_PRESENT) {
  1037. vlan_mask |= RQFPR_CFI;
  1038. }
  1039. }
  1040. switch (rule->flow_type & ~FLOW_EXT) {
  1041. case TCP_V4_FLOW:
  1042. gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
  1043. RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
  1044. gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
  1045. &rule->m_u.tcp_ip4_spec, tab);
  1046. break;
  1047. case UDP_V4_FLOW:
  1048. gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
  1049. RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
  1050. gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
  1051. &rule->m_u.udp_ip4_spec, tab);
  1052. break;
  1053. case SCTP_V4_FLOW:
  1054. gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
  1055. tab);
  1056. gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
  1057. gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
  1058. (struct ethtool_tcpip4_spec *)&rule->m_u,
  1059. tab);
  1060. break;
  1061. case IP_USER_FLOW:
  1062. gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
  1063. tab);
  1064. gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
  1065. (struct ethtool_usrip4_spec *) &rule->m_u,
  1066. tab);
  1067. break;
  1068. case ETHER_FLOW:
  1069. if (vlan)
  1070. gfar_set_parse_bits(vlan, vlan_mask, tab);
  1071. gfar_set_ether((struct ethhdr *) &rule->h_u,
  1072. (struct ethhdr *) &rule->m_u, tab);
  1073. break;
  1074. default:
  1075. return -1;
  1076. }
  1077. /* Set the vlan attributes in the end */
  1078. if (vlan) {
  1079. gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
  1080. gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
  1081. }
  1082. /* If there has been nothing written till now, it must be a default */
  1083. if (tab->index == old_index) {
  1084. gfar_set_mask(0xFFFFFFFF, tab);
  1085. tab->fe[tab->index].ctrl = 0x20;
  1086. tab->fe[tab->index].prop = 0x0;
  1087. tab->index++;
  1088. }
  1089. /* Remove last AND */
  1090. tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
  1091. /* Specify which queue to use or to drop */
  1092. if (rule->ring_cookie == RX_CLS_FLOW_DISC)
  1093. tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
  1094. else
  1095. tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
  1096. /* Only big enough entries can be clustered */
  1097. if (tab->index > (old_index + 2)) {
  1098. tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
  1099. tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
  1100. }
  1101. /* In rare cases the cache can be full while there is
  1102. * free space in hw
  1103. */
  1104. if (tab->index > MAX_FILER_CACHE_IDX - 1)
  1105. return -EBUSY;
  1106. return 0;
  1107. }
  1108. /* Copy size filer entries */
  1109. static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
  1110. struct gfar_filer_entry src[0], s32 size)
  1111. {
  1112. while (size > 0) {
  1113. size--;
  1114. dst[size].ctrl = src[size].ctrl;
  1115. dst[size].prop = src[size].prop;
  1116. }
  1117. }
  1118. /* Delete the contents of the filer-table between start and end
  1119. * and collapse them
  1120. */
  1121. static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
  1122. {
  1123. int length;
  1124. if (end > MAX_FILER_CACHE_IDX || end < begin)
  1125. return -EINVAL;
  1126. end++;
  1127. length = end - begin;
  1128. /* Copy */
  1129. while (end < tab->index) {
  1130. tab->fe[begin].ctrl = tab->fe[end].ctrl;
  1131. tab->fe[begin++].prop = tab->fe[end++].prop;
  1132. }
  1133. /* Fill up with don't cares */
  1134. while (begin < tab->index) {
  1135. tab->fe[begin].ctrl = 0x60;
  1136. tab->fe[begin].prop = 0xFFFFFFFF;
  1137. begin++;
  1138. }
  1139. tab->index -= length;
  1140. return 0;
  1141. }
  1142. /* Make space on the wanted location */
  1143. static int gfar_expand_filer_entries(u32 begin, u32 length,
  1144. struct filer_table *tab)
  1145. {
  1146. if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX ||
  1147. begin > MAX_FILER_CACHE_IDX)
  1148. return -EINVAL;
  1149. gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
  1150. tab->index - length + 1);
  1151. tab->index += length;
  1152. return 0;
  1153. }
  1154. static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
  1155. {
  1156. for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
  1157. start++) {
  1158. if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
  1159. (RQFCR_AND | RQFCR_CLE))
  1160. return start;
  1161. }
  1162. return -1;
  1163. }
  1164. static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
  1165. {
  1166. for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
  1167. start++) {
  1168. if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
  1169. (RQFCR_CLE))
  1170. return start;
  1171. }
  1172. return -1;
  1173. }
  1174. /* Uses hardwares clustering option to reduce
  1175. * the number of filer table entries
  1176. */
  1177. static void gfar_cluster_filer(struct filer_table *tab)
  1178. {
  1179. s32 i = -1, j, iend, jend;
  1180. while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
  1181. j = i;
  1182. while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
  1183. /* The cluster entries self and the previous one
  1184. * (a mask) must be identical!
  1185. */
  1186. if (tab->fe[i].ctrl != tab->fe[j].ctrl)
  1187. break;
  1188. if (tab->fe[i].prop != tab->fe[j].prop)
  1189. break;
  1190. if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
  1191. break;
  1192. if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
  1193. break;
  1194. iend = gfar_get_next_cluster_end(i, tab);
  1195. jend = gfar_get_next_cluster_end(j, tab);
  1196. if (jend == -1 || iend == -1)
  1197. break;
  1198. /* First we make some free space, where our cluster
  1199. * element should be. Then we copy it there and finally
  1200. * delete in from its old location.
  1201. */
  1202. if (gfar_expand_filer_entries(iend, (jend - j), tab) ==
  1203. -EINVAL)
  1204. break;
  1205. gfar_copy_filer_entries(&(tab->fe[iend + 1]),
  1206. &(tab->fe[jend + 1]), jend - j);
  1207. if (gfar_trim_filer_entries(jend - 1,
  1208. jend + (jend - j),
  1209. tab) == -EINVAL)
  1210. return;
  1211. /* Mask out cluster bit */
  1212. tab->fe[iend].ctrl &= ~(RQFCR_CLE);
  1213. }
  1214. }
  1215. }
  1216. /* Swaps the masked bits of a1<>a2 and b1<>b2 */
  1217. static void gfar_swap_bits(struct gfar_filer_entry *a1,
  1218. struct gfar_filer_entry *a2,
  1219. struct gfar_filer_entry *b1,
  1220. struct gfar_filer_entry *b2, u32 mask)
  1221. {
  1222. u32 temp[4];
  1223. temp[0] = a1->ctrl & mask;
  1224. temp[1] = a2->ctrl & mask;
  1225. temp[2] = b1->ctrl & mask;
  1226. temp[3] = b2->ctrl & mask;
  1227. a1->ctrl &= ~mask;
  1228. a2->ctrl &= ~mask;
  1229. b1->ctrl &= ~mask;
  1230. b2->ctrl &= ~mask;
  1231. a1->ctrl |= temp[1];
  1232. a2->ctrl |= temp[0];
  1233. b1->ctrl |= temp[3];
  1234. b2->ctrl |= temp[2];
  1235. }
  1236. /* Generate a list consisting of masks values with their start and
  1237. * end of validity and block as indicator for parts belonging
  1238. * together (glued by ANDs) in mask_table
  1239. */
  1240. static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
  1241. struct filer_table *tab)
  1242. {
  1243. u32 i, and_index = 0, block_index = 1;
  1244. for (i = 0; i < tab->index; i++) {
  1245. /* LSByte of control = 0 sets a mask */
  1246. if (!(tab->fe[i].ctrl & 0xF)) {
  1247. mask_table[and_index].mask = tab->fe[i].prop;
  1248. mask_table[and_index].start = i;
  1249. mask_table[and_index].block = block_index;
  1250. if (and_index >= 1)
  1251. mask_table[and_index - 1].end = i - 1;
  1252. and_index++;
  1253. }
  1254. /* cluster starts and ends will be separated because they should
  1255. * hold their position
  1256. */
  1257. if (tab->fe[i].ctrl & RQFCR_CLE)
  1258. block_index++;
  1259. /* A not set AND indicates the end of a depended block */
  1260. if (!(tab->fe[i].ctrl & RQFCR_AND))
  1261. block_index++;
  1262. }
  1263. mask_table[and_index - 1].end = i - 1;
  1264. return and_index;
  1265. }
  1266. /* Sorts the entries of mask_table by the values of the masks.
  1267. * Important: The 0xFF80 flags of the first and last entry of a
  1268. * block must hold their position (which queue, CLusterEnable, ReJEct,
  1269. * AND)
  1270. */
  1271. static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
  1272. struct filer_table *temp_table, u32 and_index)
  1273. {
  1274. /* Pointer to compare function (_asc or _desc) */
  1275. int (*gfar_comp)(const void *, const void *);
  1276. u32 i, size = 0, start = 0, prev = 1;
  1277. u32 old_first, old_last, new_first, new_last;
  1278. gfar_comp = &gfar_comp_desc;
  1279. for (i = 0; i < and_index; i++) {
  1280. if (prev != mask_table[i].block) {
  1281. old_first = mask_table[start].start + 1;
  1282. old_last = mask_table[i - 1].end;
  1283. sort(mask_table + start, size,
  1284. sizeof(struct gfar_mask_entry),
  1285. gfar_comp, &gfar_swap);
  1286. /* Toggle order for every block. This makes the
  1287. * thing more efficient!
  1288. */
  1289. if (gfar_comp == gfar_comp_desc)
  1290. gfar_comp = &gfar_comp_asc;
  1291. else
  1292. gfar_comp = &gfar_comp_desc;
  1293. new_first = mask_table[start].start + 1;
  1294. new_last = mask_table[i - 1].end;
  1295. gfar_swap_bits(&temp_table->fe[new_first],
  1296. &temp_table->fe[old_first],
  1297. &temp_table->fe[new_last],
  1298. &temp_table->fe[old_last],
  1299. RQFCR_QUEUE | RQFCR_CLE |
  1300. RQFCR_RJE | RQFCR_AND);
  1301. start = i;
  1302. size = 0;
  1303. }
  1304. size++;
  1305. prev = mask_table[i].block;
  1306. }
  1307. }
  1308. /* Reduces the number of masks needed in the filer table to save entries
  1309. * This is done by sorting the masks of a depended block. A depended block is
  1310. * identified by gluing ANDs or CLE. The sorting order toggles after every
  1311. * block. Of course entries in scope of a mask must change their location with
  1312. * it.
  1313. */
  1314. static int gfar_optimize_filer_masks(struct filer_table *tab)
  1315. {
  1316. struct filer_table *temp_table;
  1317. struct gfar_mask_entry *mask_table;
  1318. u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
  1319. s32 ret = 0;
  1320. /* We need a copy of the filer table because
  1321. * we want to change its order
  1322. */
  1323. temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
  1324. if (temp_table == NULL)
  1325. return -ENOMEM;
  1326. mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
  1327. sizeof(struct gfar_mask_entry), GFP_KERNEL);
  1328. if (mask_table == NULL) {
  1329. ret = -ENOMEM;
  1330. goto end;
  1331. }
  1332. and_index = gfar_generate_mask_table(mask_table, tab);
  1333. gfar_sort_mask_table(mask_table, temp_table, and_index);
  1334. /* Now we can copy the data from our duplicated filer table to
  1335. * the real one in the order the mask table says
  1336. */
  1337. for (i = 0; i < and_index; i++) {
  1338. size = mask_table[i].end - mask_table[i].start + 1;
  1339. gfar_copy_filer_entries(&(tab->fe[j]),
  1340. &(temp_table->fe[mask_table[i].start]), size);
  1341. j += size;
  1342. }
  1343. /* And finally we just have to check for duplicated masks and drop the
  1344. * second ones
  1345. */
  1346. for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
  1347. if (tab->fe[i].ctrl == 0x80) {
  1348. previous_mask = i++;
  1349. break;
  1350. }
  1351. }
  1352. for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
  1353. if (tab->fe[i].ctrl == 0x80) {
  1354. if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
  1355. /* Two identical ones found!
  1356. * So drop the second one!
  1357. */
  1358. gfar_trim_filer_entries(i, i, tab);
  1359. } else
  1360. /* Not identical! */
  1361. previous_mask = i;
  1362. }
  1363. }
  1364. kfree(mask_table);
  1365. end: kfree(temp_table);
  1366. return ret;
  1367. }
  1368. /* Write the bit-pattern from software's buffer to hardware registers */
  1369. static int gfar_write_filer_table(struct gfar_private *priv,
  1370. struct filer_table *tab)
  1371. {
  1372. u32 i = 0;
  1373. if (tab->index > MAX_FILER_IDX - 1)
  1374. return -EBUSY;
  1375. /* Fill regular entries */
  1376. for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].prop);
  1377. i++)
  1378. gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
  1379. /* Fill the rest with fall-troughs */
  1380. for (; i < MAX_FILER_IDX - 1; i++)
  1381. gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
  1382. /* Last entry must be default accept
  1383. * because that's what people expect
  1384. */
  1385. gfar_write_filer(priv, i, 0x20, 0x0);
  1386. return 0;
  1387. }
  1388. static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
  1389. struct gfar_private *priv)
  1390. {
  1391. if (flow->flow_type & FLOW_EXT) {
  1392. if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
  1393. netdev_warn(priv->ndev,
  1394. "User-specific data not supported!\n");
  1395. if (~flow->m_ext.vlan_etype)
  1396. netdev_warn(priv->ndev,
  1397. "VLAN-etype not supported!\n");
  1398. }
  1399. if (flow->flow_type == IP_USER_FLOW)
  1400. if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
  1401. netdev_warn(priv->ndev,
  1402. "IP-Version differing from IPv4 not supported!\n");
  1403. return 0;
  1404. }
  1405. static int gfar_process_filer_changes(struct gfar_private *priv)
  1406. {
  1407. struct ethtool_flow_spec_container *j;
  1408. struct filer_table *tab;
  1409. s32 i = 0;
  1410. s32 ret = 0;
  1411. /* So index is set to zero, too! */
  1412. tab = kzalloc(sizeof(*tab), GFP_KERNEL);
  1413. if (tab == NULL)
  1414. return -ENOMEM;
  1415. /* Now convert the existing filer data from flow_spec into
  1416. * filer tables binary format
  1417. */
  1418. list_for_each_entry(j, &priv->rx_list.list, list) {
  1419. ret = gfar_convert_to_filer(&j->fs, tab);
  1420. if (ret == -EBUSY) {
  1421. netdev_err(priv->ndev,
  1422. "Rule not added: No free space!\n");
  1423. goto end;
  1424. }
  1425. if (ret == -1) {
  1426. netdev_err(priv->ndev,
  1427. "Rule not added: Unsupported Flow-type!\n");
  1428. goto end;
  1429. }
  1430. }
  1431. i = tab->index;
  1432. /* Optimizations to save entries */
  1433. gfar_cluster_filer(tab);
  1434. gfar_optimize_filer_masks(tab);
  1435. pr_debug("\tSummary:\n"
  1436. "\tData on hardware: %d\n"
  1437. "\tCompression rate: %d%%\n",
  1438. tab->index, 100 - (100 * tab->index) / i);
  1439. /* Write everything to hardware */
  1440. ret = gfar_write_filer_table(priv, tab);
  1441. if (ret == -EBUSY) {
  1442. netdev_err(priv->ndev, "Rule not added: No free space!\n");
  1443. goto end;
  1444. }
  1445. end:
  1446. kfree(tab);
  1447. return ret;
  1448. }
  1449. static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
  1450. {
  1451. u32 i = 0;
  1452. for (i = 0; i < sizeof(flow->m_u); i++)
  1453. flow->m_u.hdata[i] ^= 0xFF;
  1454. flow->m_ext.vlan_etype ^= cpu_to_be16(0xFFFF);
  1455. flow->m_ext.vlan_tci ^= cpu_to_be16(0xFFFF);
  1456. flow->m_ext.data[0] ^= cpu_to_be32(~0);
  1457. flow->m_ext.data[1] ^= cpu_to_be32(~0);
  1458. }
  1459. static int gfar_add_cls(struct gfar_private *priv,
  1460. struct ethtool_rx_flow_spec *flow)
  1461. {
  1462. struct ethtool_flow_spec_container *temp, *comp;
  1463. int ret = 0;
  1464. temp = kmalloc(sizeof(*temp), GFP_KERNEL);
  1465. if (temp == NULL)
  1466. return -ENOMEM;
  1467. memcpy(&temp->fs, flow, sizeof(temp->fs));
  1468. gfar_invert_masks(&temp->fs);
  1469. ret = gfar_check_capability(&temp->fs, priv);
  1470. if (ret)
  1471. goto clean_mem;
  1472. /* Link in the new element at the right @location */
  1473. if (list_empty(&priv->rx_list.list)) {
  1474. ret = gfar_check_filer_hardware(priv);
  1475. if (ret != 0)
  1476. goto clean_mem;
  1477. list_add(&temp->list, &priv->rx_list.list);
  1478. goto process;
  1479. } else {
  1480. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1481. if (comp->fs.location > flow->location) {
  1482. list_add_tail(&temp->list, &comp->list);
  1483. goto process;
  1484. }
  1485. if (comp->fs.location == flow->location) {
  1486. netdev_err(priv->ndev,
  1487. "Rule not added: ID %d not free!\n",
  1488. flow->location);
  1489. ret = -EBUSY;
  1490. goto clean_mem;
  1491. }
  1492. }
  1493. list_add_tail(&temp->list, &priv->rx_list.list);
  1494. }
  1495. process:
  1496. ret = gfar_process_filer_changes(priv);
  1497. if (ret)
  1498. goto clean_list;
  1499. priv->rx_list.count++;
  1500. return ret;
  1501. clean_list:
  1502. list_del(&temp->list);
  1503. clean_mem:
  1504. kfree(temp);
  1505. return ret;
  1506. }
  1507. static int gfar_del_cls(struct gfar_private *priv, u32 loc)
  1508. {
  1509. struct ethtool_flow_spec_container *comp;
  1510. u32 ret = -EINVAL;
  1511. if (list_empty(&priv->rx_list.list))
  1512. return ret;
  1513. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1514. if (comp->fs.location == loc) {
  1515. list_del(&comp->list);
  1516. kfree(comp);
  1517. priv->rx_list.count--;
  1518. gfar_process_filer_changes(priv);
  1519. ret = 0;
  1520. break;
  1521. }
  1522. }
  1523. return ret;
  1524. }
  1525. static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
  1526. {
  1527. struct ethtool_flow_spec_container *comp;
  1528. u32 ret = -EINVAL;
  1529. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1530. if (comp->fs.location == cmd->fs.location) {
  1531. memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
  1532. gfar_invert_masks(&cmd->fs);
  1533. ret = 0;
  1534. break;
  1535. }
  1536. }
  1537. return ret;
  1538. }
  1539. static int gfar_get_cls_all(struct gfar_private *priv,
  1540. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  1541. {
  1542. struct ethtool_flow_spec_container *comp;
  1543. u32 i = 0;
  1544. list_for_each_entry(comp, &priv->rx_list.list, list) {
  1545. if (i == cmd->rule_cnt)
  1546. return -EMSGSIZE;
  1547. rule_locs[i] = comp->fs.location;
  1548. i++;
  1549. }
  1550. cmd->data = MAX_FILER_IDX;
  1551. cmd->rule_cnt = i;
  1552. return 0;
  1553. }
  1554. static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
  1555. {
  1556. struct gfar_private *priv = netdev_priv(dev);
  1557. int ret = 0;
  1558. if (test_bit(GFAR_RESETTING, &priv->state))
  1559. return -EBUSY;
  1560. mutex_lock(&priv->rx_queue_access);
  1561. switch (cmd->cmd) {
  1562. case ETHTOOL_SRXFH:
  1563. ret = gfar_set_hash_opts(priv, cmd);
  1564. break;
  1565. case ETHTOOL_SRXCLSRLINS:
  1566. if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
  1567. cmd->fs.ring_cookie >= priv->num_rx_queues) ||
  1568. cmd->fs.location >= MAX_FILER_IDX) {
  1569. ret = -EINVAL;
  1570. break;
  1571. }
  1572. ret = gfar_add_cls(priv, &cmd->fs);
  1573. break;
  1574. case ETHTOOL_SRXCLSRLDEL:
  1575. ret = gfar_del_cls(priv, cmd->fs.location);
  1576. break;
  1577. default:
  1578. ret = -EINVAL;
  1579. }
  1580. mutex_unlock(&priv->rx_queue_access);
  1581. return ret;
  1582. }
  1583. static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
  1584. u32 *rule_locs)
  1585. {
  1586. struct gfar_private *priv = netdev_priv(dev);
  1587. int ret = 0;
  1588. switch (cmd->cmd) {
  1589. case ETHTOOL_GRXRINGS:
  1590. cmd->data = priv->num_rx_queues;
  1591. break;
  1592. case ETHTOOL_GRXCLSRLCNT:
  1593. cmd->rule_cnt = priv->rx_list.count;
  1594. break;
  1595. case ETHTOOL_GRXCLSRULE:
  1596. ret = gfar_get_cls(priv, cmd);
  1597. break;
  1598. case ETHTOOL_GRXCLSRLALL:
  1599. ret = gfar_get_cls_all(priv, cmd, rule_locs);
  1600. break;
  1601. default:
  1602. ret = -EINVAL;
  1603. break;
  1604. }
  1605. return ret;
  1606. }
  1607. int gfar_phc_index = -1;
  1608. EXPORT_SYMBOL(gfar_phc_index);
  1609. static int gfar_get_ts_info(struct net_device *dev,
  1610. struct ethtool_ts_info *info)
  1611. {
  1612. struct gfar_private *priv = netdev_priv(dev);
  1613. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
  1614. info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
  1615. SOF_TIMESTAMPING_SOFTWARE;
  1616. info->phc_index = -1;
  1617. return 0;
  1618. }
  1619. info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
  1620. SOF_TIMESTAMPING_RX_HARDWARE |
  1621. SOF_TIMESTAMPING_RAW_HARDWARE;
  1622. info->phc_index = gfar_phc_index;
  1623. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  1624. (1 << HWTSTAMP_TX_ON);
  1625. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  1626. (1 << HWTSTAMP_FILTER_ALL);
  1627. return 0;
  1628. }
  1629. const struct ethtool_ops gfar_ethtool_ops = {
  1630. .get_settings = gfar_gsettings,
  1631. .set_settings = gfar_ssettings,
  1632. .get_drvinfo = gfar_gdrvinfo,
  1633. .get_regs_len = gfar_reglen,
  1634. .get_regs = gfar_get_regs,
  1635. .get_link = ethtool_op_get_link,
  1636. .get_coalesce = gfar_gcoalesce,
  1637. .set_coalesce = gfar_scoalesce,
  1638. .get_ringparam = gfar_gringparam,
  1639. .set_ringparam = gfar_sringparam,
  1640. .get_pauseparam = gfar_gpauseparam,
  1641. .set_pauseparam = gfar_spauseparam,
  1642. .get_strings = gfar_gstrings,
  1643. .get_sset_count = gfar_sset_count,
  1644. .get_ethtool_stats = gfar_fill_stats,
  1645. .get_msglevel = gfar_get_msglevel,
  1646. .set_msglevel = gfar_set_msglevel,
  1647. #ifdef CONFIG_PM
  1648. .get_wol = gfar_get_wol,
  1649. .set_wol = gfar_set_wol,
  1650. #endif
  1651. .set_rxnfc = gfar_set_nfc,
  1652. .get_rxnfc = gfar_get_nfc,
  1653. .get_ts_info = gfar_get_ts_info,
  1654. };