gianfar_ethtool.c 47 KB

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