gianfar_ethtool.c 50 KB

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