gianfar_ethtool.c 49 KB

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