gianfar_ethtool.c 41 KB

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