i40e_ethtool.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * The full GNU General Public License is included in this distribution in
  20. * the file called "COPYING".
  21. *
  22. * Contact Information:
  23. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  24. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25. *
  26. ******************************************************************************/
  27. /* ethtool support for i40e */
  28. #include "i40e.h"
  29. #include "i40e_diag.h"
  30. struct i40e_stats {
  31. char stat_string[ETH_GSTRING_LEN];
  32. int sizeof_stat;
  33. int stat_offset;
  34. };
  35. #define I40E_STAT(_type, _name, _stat) { \
  36. .stat_string = _name, \
  37. .sizeof_stat = FIELD_SIZEOF(_type, _stat), \
  38. .stat_offset = offsetof(_type, _stat) \
  39. }
  40. #define I40E_NETDEV_STAT(_net_stat) \
  41. I40E_STAT(struct net_device_stats, #_net_stat, _net_stat)
  42. #define I40E_PF_STAT(_name, _stat) \
  43. I40E_STAT(struct i40e_pf, _name, _stat)
  44. #define I40E_VSI_STAT(_name, _stat) \
  45. I40E_STAT(struct i40e_vsi, _name, _stat)
  46. static const struct i40e_stats i40e_gstrings_net_stats[] = {
  47. I40E_NETDEV_STAT(rx_packets),
  48. I40E_NETDEV_STAT(tx_packets),
  49. I40E_NETDEV_STAT(rx_bytes),
  50. I40E_NETDEV_STAT(tx_bytes),
  51. I40E_NETDEV_STAT(rx_errors),
  52. I40E_NETDEV_STAT(tx_errors),
  53. I40E_NETDEV_STAT(rx_dropped),
  54. I40E_NETDEV_STAT(tx_dropped),
  55. I40E_NETDEV_STAT(multicast),
  56. I40E_NETDEV_STAT(collisions),
  57. I40E_NETDEV_STAT(rx_length_errors),
  58. I40E_NETDEV_STAT(rx_crc_errors),
  59. };
  60. /* These PF_STATs might look like duplicates of some NETDEV_STATs,
  61. * but they are separate. This device supports Virtualization, and
  62. * as such might have several netdevs supporting VMDq and FCoE going
  63. * through a single port. The NETDEV_STATs are for individual netdevs
  64. * seen at the top of the stack, and the PF_STATs are for the physical
  65. * function at the bottom of the stack hosting those netdevs.
  66. *
  67. * The PF_STATs are appended to the netdev stats only when ethtool -S
  68. * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
  69. */
  70. static struct i40e_stats i40e_gstrings_stats[] = {
  71. I40E_PF_STAT("rx_bytes", stats.eth.rx_bytes),
  72. I40E_PF_STAT("tx_bytes", stats.eth.tx_bytes),
  73. I40E_PF_STAT("rx_errors", stats.eth.rx_errors),
  74. I40E_PF_STAT("tx_errors", stats.eth.tx_errors),
  75. I40E_PF_STAT("rx_dropped", stats.eth.rx_discards),
  76. I40E_PF_STAT("tx_dropped", stats.eth.tx_discards),
  77. I40E_PF_STAT("tx_dropped_link_down", stats.tx_dropped_link_down),
  78. I40E_PF_STAT("crc_errors", stats.crc_errors),
  79. I40E_PF_STAT("illegal_bytes", stats.illegal_bytes),
  80. I40E_PF_STAT("mac_local_faults", stats.mac_local_faults),
  81. I40E_PF_STAT("mac_remote_faults", stats.mac_remote_faults),
  82. I40E_PF_STAT("rx_length_errors", stats.rx_length_errors),
  83. I40E_PF_STAT("link_xon_rx", stats.link_xon_rx),
  84. I40E_PF_STAT("link_xoff_rx", stats.link_xoff_rx),
  85. I40E_PF_STAT("link_xon_tx", stats.link_xon_tx),
  86. I40E_PF_STAT("link_xoff_tx", stats.link_xoff_tx),
  87. I40E_PF_STAT("rx_size_64", stats.rx_size_64),
  88. I40E_PF_STAT("rx_size_127", stats.rx_size_127),
  89. I40E_PF_STAT("rx_size_255", stats.rx_size_255),
  90. I40E_PF_STAT("rx_size_511", stats.rx_size_511),
  91. I40E_PF_STAT("rx_size_1023", stats.rx_size_1023),
  92. I40E_PF_STAT("rx_size_1522", stats.rx_size_1522),
  93. I40E_PF_STAT("rx_size_big", stats.rx_size_big),
  94. I40E_PF_STAT("tx_size_64", stats.tx_size_64),
  95. I40E_PF_STAT("tx_size_127", stats.tx_size_127),
  96. I40E_PF_STAT("tx_size_255", stats.tx_size_255),
  97. I40E_PF_STAT("tx_size_511", stats.tx_size_511),
  98. I40E_PF_STAT("tx_size_1023", stats.tx_size_1023),
  99. I40E_PF_STAT("tx_size_1522", stats.tx_size_1522),
  100. I40E_PF_STAT("tx_size_big", stats.tx_size_big),
  101. I40E_PF_STAT("rx_undersize", stats.rx_undersize),
  102. I40E_PF_STAT("rx_fragments", stats.rx_fragments),
  103. I40E_PF_STAT("rx_oversize", stats.rx_oversize),
  104. I40E_PF_STAT("rx_jabber", stats.rx_jabber),
  105. I40E_PF_STAT("VF_admin_queue_requests", vf_aq_requests),
  106. };
  107. #define I40E_QUEUE_STATS_LEN(n) \
  108. ((((struct i40e_netdev_priv *)netdev_priv((n)))->vsi->num_queue_pairs + \
  109. ((struct i40e_netdev_priv *)netdev_priv((n)))->vsi->num_queue_pairs) * 2)
  110. #define I40E_GLOBAL_STATS_LEN ARRAY_SIZE(i40e_gstrings_stats)
  111. #define I40E_NETDEV_STATS_LEN ARRAY_SIZE(i40e_gstrings_net_stats)
  112. #define I40E_VSI_STATS_LEN(n) (I40E_NETDEV_STATS_LEN + \
  113. I40E_QUEUE_STATS_LEN((n)))
  114. #define I40E_PFC_STATS_LEN ( \
  115. (FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_rx) + \
  116. FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_rx) + \
  117. FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_tx) + \
  118. FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_tx) + \
  119. FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_2_xoff)) \
  120. / sizeof(u64))
  121. #define I40E_PF_STATS_LEN(n) (I40E_GLOBAL_STATS_LEN + \
  122. I40E_PFC_STATS_LEN + \
  123. I40E_VSI_STATS_LEN((n)))
  124. enum i40e_ethtool_test_id {
  125. I40E_ETH_TEST_REG = 0,
  126. I40E_ETH_TEST_EEPROM,
  127. I40E_ETH_TEST_INTR,
  128. I40E_ETH_TEST_LOOPBACK,
  129. I40E_ETH_TEST_LINK,
  130. };
  131. static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
  132. "Register test (offline)",
  133. "Eeprom test (offline)",
  134. "Interrupt test (offline)",
  135. "Loopback test (offline)",
  136. "Link test (on/offline)"
  137. };
  138. #define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
  139. /**
  140. * i40e_get_settings - Get Link Speed and Duplex settings
  141. * @netdev: network interface device structure
  142. * @ecmd: ethtool command
  143. *
  144. * Reports speed/duplex settings based on media_type
  145. **/
  146. static int i40e_get_settings(struct net_device *netdev,
  147. struct ethtool_cmd *ecmd)
  148. {
  149. struct i40e_netdev_priv *np = netdev_priv(netdev);
  150. struct i40e_pf *pf = np->vsi->back;
  151. struct i40e_hw *hw = &pf->hw;
  152. struct i40e_link_status *hw_link_info = &hw->phy.link_info;
  153. bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
  154. u32 link_speed = hw_link_info->link_speed;
  155. /* hardware is either in 40G mode or 10G mode
  156. * NOTE: this section initializes supported and advertising
  157. */
  158. switch (hw_link_info->phy_type) {
  159. case I40E_PHY_TYPE_40GBASE_CR4:
  160. case I40E_PHY_TYPE_40GBASE_CR4_CU:
  161. ecmd->supported = SUPPORTED_40000baseCR4_Full;
  162. ecmd->advertising = ADVERTISED_40000baseCR4_Full;
  163. break;
  164. case I40E_PHY_TYPE_40GBASE_KR4:
  165. ecmd->supported = SUPPORTED_40000baseKR4_Full;
  166. ecmd->advertising = ADVERTISED_40000baseKR4_Full;
  167. break;
  168. case I40E_PHY_TYPE_40GBASE_SR4:
  169. ecmd->supported = SUPPORTED_40000baseSR4_Full;
  170. ecmd->advertising = ADVERTISED_40000baseSR4_Full;
  171. break;
  172. case I40E_PHY_TYPE_40GBASE_LR4:
  173. ecmd->supported = SUPPORTED_40000baseLR4_Full;
  174. ecmd->advertising = ADVERTISED_40000baseLR4_Full;
  175. break;
  176. case I40E_PHY_TYPE_10GBASE_KX4:
  177. ecmd->supported = SUPPORTED_10000baseKX4_Full;
  178. ecmd->advertising = ADVERTISED_10000baseKX4_Full;
  179. break;
  180. case I40E_PHY_TYPE_10GBASE_KR:
  181. ecmd->supported = SUPPORTED_10000baseKR_Full;
  182. ecmd->advertising = ADVERTISED_10000baseKR_Full;
  183. break;
  184. case I40E_PHY_TYPE_10GBASE_T:
  185. default:
  186. ecmd->supported = SUPPORTED_10000baseT_Full;
  187. ecmd->advertising = ADVERTISED_10000baseT_Full;
  188. break;
  189. }
  190. /* for now just say autoneg all the time */
  191. ecmd->supported |= SUPPORTED_Autoneg;
  192. if (hw->phy.media_type == I40E_MEDIA_TYPE_BACKPLANE) {
  193. ecmd->supported |= SUPPORTED_Backplane;
  194. ecmd->advertising |= ADVERTISED_Backplane;
  195. ecmd->port = PORT_NONE;
  196. } else if (hw->phy.media_type == I40E_MEDIA_TYPE_BASET) {
  197. ecmd->supported |= SUPPORTED_TP;
  198. ecmd->advertising |= ADVERTISED_TP;
  199. ecmd->port = PORT_TP;
  200. } else {
  201. ecmd->supported |= SUPPORTED_FIBRE;
  202. ecmd->advertising |= ADVERTISED_FIBRE;
  203. ecmd->port = PORT_FIBRE;
  204. }
  205. ecmd->transceiver = XCVR_EXTERNAL;
  206. if (link_up) {
  207. switch (link_speed) {
  208. case I40E_LINK_SPEED_40GB:
  209. /* need a SPEED_40000 in ethtool.h */
  210. ethtool_cmd_speed_set(ecmd, 40000);
  211. break;
  212. case I40E_LINK_SPEED_10GB:
  213. ethtool_cmd_speed_set(ecmd, SPEED_10000);
  214. break;
  215. default:
  216. break;
  217. }
  218. ecmd->duplex = DUPLEX_FULL;
  219. } else {
  220. ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
  221. ecmd->duplex = DUPLEX_UNKNOWN;
  222. }
  223. return 0;
  224. }
  225. /**
  226. * i40e_get_pauseparam - Get Flow Control status
  227. * Return tx/rx-pause status
  228. **/
  229. static void i40e_get_pauseparam(struct net_device *netdev,
  230. struct ethtool_pauseparam *pause)
  231. {
  232. struct i40e_netdev_priv *np = netdev_priv(netdev);
  233. struct i40e_pf *pf = np->vsi->back;
  234. struct i40e_hw *hw = &pf->hw;
  235. struct i40e_link_status *hw_link_info = &hw->phy.link_info;
  236. pause->autoneg =
  237. ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
  238. AUTONEG_ENABLE : AUTONEG_DISABLE);
  239. pause->rx_pause = 0;
  240. pause->tx_pause = 0;
  241. if (hw_link_info->an_info & I40E_AQ_LINK_PAUSE_RX)
  242. pause->rx_pause = 1;
  243. if (hw_link_info->an_info & I40E_AQ_LINK_PAUSE_TX)
  244. pause->tx_pause = 1;
  245. }
  246. static u32 i40e_get_msglevel(struct net_device *netdev)
  247. {
  248. struct i40e_netdev_priv *np = netdev_priv(netdev);
  249. struct i40e_pf *pf = np->vsi->back;
  250. return pf->msg_enable;
  251. }
  252. static void i40e_set_msglevel(struct net_device *netdev, u32 data)
  253. {
  254. struct i40e_netdev_priv *np = netdev_priv(netdev);
  255. struct i40e_pf *pf = np->vsi->back;
  256. if (I40E_DEBUG_USER & data)
  257. pf->hw.debug_mask = data;
  258. pf->msg_enable = data;
  259. }
  260. static int i40e_get_regs_len(struct net_device *netdev)
  261. {
  262. int reg_count = 0;
  263. int i;
  264. for (i = 0; i40e_reg_list[i].offset != 0; i++)
  265. reg_count += i40e_reg_list[i].elements;
  266. return reg_count * sizeof(u32);
  267. }
  268. static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
  269. void *p)
  270. {
  271. struct i40e_netdev_priv *np = netdev_priv(netdev);
  272. struct i40e_pf *pf = np->vsi->back;
  273. struct i40e_hw *hw = &pf->hw;
  274. u32 *reg_buf = p;
  275. int i, j, ri;
  276. u32 reg;
  277. /* Tell ethtool which driver-version-specific regs output we have.
  278. *
  279. * At some point, if we have ethtool doing special formatting of
  280. * this data, it will rely on this version number to know how to
  281. * interpret things. Hence, this needs to be updated if/when the
  282. * diags register table is changed.
  283. */
  284. regs->version = 1;
  285. /* loop through the diags reg table for what to print */
  286. ri = 0;
  287. for (i = 0; i40e_reg_list[i].offset != 0; i++) {
  288. for (j = 0; j < i40e_reg_list[i].elements; j++) {
  289. reg = i40e_reg_list[i].offset
  290. + (j * i40e_reg_list[i].stride);
  291. reg_buf[ri++] = rd32(hw, reg);
  292. }
  293. }
  294. }
  295. static int i40e_get_eeprom(struct net_device *netdev,
  296. struct ethtool_eeprom *eeprom, u8 *bytes)
  297. {
  298. struct i40e_netdev_priv *np = netdev_priv(netdev);
  299. struct i40e_hw *hw = &np->vsi->back->hw;
  300. int first_word, last_word;
  301. u16 i, eeprom_len;
  302. u16 *eeprom_buff;
  303. int ret_val = 0;
  304. if (eeprom->len == 0)
  305. return -EINVAL;
  306. eeprom->magic = hw->vendor_id | (hw->device_id << 16);
  307. first_word = eeprom->offset >> 1;
  308. last_word = (eeprom->offset + eeprom->len - 1) >> 1;
  309. eeprom_len = last_word - first_word + 1;
  310. eeprom_buff = kmalloc(sizeof(u16) * eeprom_len, GFP_KERNEL);
  311. if (!eeprom_buff)
  312. return -ENOMEM;
  313. ret_val = i40e_read_nvm_buffer(hw, first_word, &eeprom_len,
  314. eeprom_buff);
  315. if (eeprom_len == 0) {
  316. kfree(eeprom_buff);
  317. return -EACCES;
  318. }
  319. /* Device's eeprom is always little-endian, word addressable */
  320. for (i = 0; i < eeprom_len; i++)
  321. le16_to_cpus(&eeprom_buff[i]);
  322. memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
  323. kfree(eeprom_buff);
  324. return ret_val;
  325. }
  326. static int i40e_get_eeprom_len(struct net_device *netdev)
  327. {
  328. struct i40e_netdev_priv *np = netdev_priv(netdev);
  329. struct i40e_hw *hw = &np->vsi->back->hw;
  330. return hw->nvm.sr_size * 2;
  331. }
  332. static void i40e_get_drvinfo(struct net_device *netdev,
  333. struct ethtool_drvinfo *drvinfo)
  334. {
  335. struct i40e_netdev_priv *np = netdev_priv(netdev);
  336. struct i40e_vsi *vsi = np->vsi;
  337. struct i40e_pf *pf = vsi->back;
  338. strlcpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
  339. strlcpy(drvinfo->version, i40e_driver_version_str,
  340. sizeof(drvinfo->version));
  341. strlcpy(drvinfo->fw_version, i40e_fw_version_str(&pf->hw),
  342. sizeof(drvinfo->fw_version));
  343. strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
  344. sizeof(drvinfo->bus_info));
  345. }
  346. static void i40e_get_ringparam(struct net_device *netdev,
  347. struct ethtool_ringparam *ring)
  348. {
  349. struct i40e_netdev_priv *np = netdev_priv(netdev);
  350. struct i40e_pf *pf = np->vsi->back;
  351. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  352. ring->rx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
  353. ring->tx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
  354. ring->rx_mini_max_pending = 0;
  355. ring->rx_jumbo_max_pending = 0;
  356. ring->rx_pending = vsi->rx_rings[0]->count;
  357. ring->tx_pending = vsi->tx_rings[0]->count;
  358. ring->rx_mini_pending = 0;
  359. ring->rx_jumbo_pending = 0;
  360. }
  361. static int i40e_set_ringparam(struct net_device *netdev,
  362. struct ethtool_ringparam *ring)
  363. {
  364. struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
  365. struct i40e_netdev_priv *np = netdev_priv(netdev);
  366. struct i40e_vsi *vsi = np->vsi;
  367. struct i40e_pf *pf = vsi->back;
  368. u32 new_rx_count, new_tx_count;
  369. int i, err = 0;
  370. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  371. return -EINVAL;
  372. new_tx_count = clamp_t(u32, ring->tx_pending,
  373. I40E_MIN_NUM_DESCRIPTORS,
  374. I40E_MAX_NUM_DESCRIPTORS);
  375. new_tx_count = ALIGN(new_tx_count, I40E_REQ_DESCRIPTOR_MULTIPLE);
  376. new_rx_count = clamp_t(u32, ring->rx_pending,
  377. I40E_MIN_NUM_DESCRIPTORS,
  378. I40E_MAX_NUM_DESCRIPTORS);
  379. new_rx_count = ALIGN(new_rx_count, I40E_REQ_DESCRIPTOR_MULTIPLE);
  380. /* if nothing to do return success */
  381. if ((new_tx_count == vsi->tx_rings[0]->count) &&
  382. (new_rx_count == vsi->rx_rings[0]->count))
  383. return 0;
  384. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  385. usleep_range(1000, 2000);
  386. if (!netif_running(vsi->netdev)) {
  387. /* simple case - set for the next time the netdev is started */
  388. for (i = 0; i < vsi->num_queue_pairs; i++) {
  389. vsi->tx_rings[i]->count = new_tx_count;
  390. vsi->rx_rings[i]->count = new_rx_count;
  391. }
  392. goto done;
  393. }
  394. /* We can't just free everything and then setup again,
  395. * because the ISRs in MSI-X mode get passed pointers
  396. * to the Tx and Rx ring structs.
  397. */
  398. /* alloc updated Tx resources */
  399. if (new_tx_count != vsi->tx_rings[0]->count) {
  400. netdev_info(netdev,
  401. "Changing Tx descriptor count from %d to %d.\n",
  402. vsi->tx_rings[0]->count, new_tx_count);
  403. tx_rings = kcalloc(vsi->alloc_queue_pairs,
  404. sizeof(struct i40e_ring), GFP_KERNEL);
  405. if (!tx_rings) {
  406. err = -ENOMEM;
  407. goto done;
  408. }
  409. for (i = 0; i < vsi->num_queue_pairs; i++) {
  410. /* clone ring and setup updated count */
  411. tx_rings[i] = *vsi->tx_rings[i];
  412. tx_rings[i].count = new_tx_count;
  413. err = i40e_setup_tx_descriptors(&tx_rings[i]);
  414. if (err) {
  415. while (i) {
  416. i--;
  417. i40e_free_tx_resources(&tx_rings[i]);
  418. }
  419. kfree(tx_rings);
  420. tx_rings = NULL;
  421. goto done;
  422. }
  423. }
  424. }
  425. /* alloc updated Rx resources */
  426. if (new_rx_count != vsi->rx_rings[0]->count) {
  427. netdev_info(netdev,
  428. "Changing Rx descriptor count from %d to %d\n",
  429. vsi->rx_rings[0]->count, new_rx_count);
  430. rx_rings = kcalloc(vsi->alloc_queue_pairs,
  431. sizeof(struct i40e_ring), GFP_KERNEL);
  432. if (!rx_rings) {
  433. err = -ENOMEM;
  434. goto free_tx;
  435. }
  436. for (i = 0; i < vsi->num_queue_pairs; i++) {
  437. /* clone ring and setup updated count */
  438. rx_rings[i] = *vsi->rx_rings[i];
  439. rx_rings[i].count = new_rx_count;
  440. err = i40e_setup_rx_descriptors(&rx_rings[i]);
  441. if (err) {
  442. while (i) {
  443. i--;
  444. i40e_free_rx_resources(&rx_rings[i]);
  445. }
  446. kfree(rx_rings);
  447. rx_rings = NULL;
  448. goto free_tx;
  449. }
  450. }
  451. }
  452. /* Bring interface down, copy in the new ring info,
  453. * then restore the interface
  454. */
  455. i40e_down(vsi);
  456. if (tx_rings) {
  457. for (i = 0; i < vsi->num_queue_pairs; i++) {
  458. i40e_free_tx_resources(vsi->tx_rings[i]);
  459. *vsi->tx_rings[i] = tx_rings[i];
  460. }
  461. kfree(tx_rings);
  462. tx_rings = NULL;
  463. }
  464. if (rx_rings) {
  465. for (i = 0; i < vsi->num_queue_pairs; i++) {
  466. i40e_free_rx_resources(vsi->rx_rings[i]);
  467. *vsi->rx_rings[i] = rx_rings[i];
  468. }
  469. kfree(rx_rings);
  470. rx_rings = NULL;
  471. }
  472. i40e_up(vsi);
  473. free_tx:
  474. /* error cleanup if the Rx allocations failed after getting Tx */
  475. if (tx_rings) {
  476. for (i = 0; i < vsi->num_queue_pairs; i++)
  477. i40e_free_tx_resources(&tx_rings[i]);
  478. kfree(tx_rings);
  479. tx_rings = NULL;
  480. }
  481. done:
  482. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  483. return err;
  484. }
  485. static int i40e_get_sset_count(struct net_device *netdev, int sset)
  486. {
  487. struct i40e_netdev_priv *np = netdev_priv(netdev);
  488. struct i40e_vsi *vsi = np->vsi;
  489. struct i40e_pf *pf = vsi->back;
  490. switch (sset) {
  491. case ETH_SS_TEST:
  492. return I40E_TEST_LEN;
  493. case ETH_SS_STATS:
  494. if (vsi == pf->vsi[pf->lan_vsi])
  495. return I40E_PF_STATS_LEN(netdev);
  496. else
  497. return I40E_VSI_STATS_LEN(netdev);
  498. default:
  499. return -EOPNOTSUPP;
  500. }
  501. }
  502. static void i40e_get_ethtool_stats(struct net_device *netdev,
  503. struct ethtool_stats *stats, u64 *data)
  504. {
  505. struct i40e_netdev_priv *np = netdev_priv(netdev);
  506. struct i40e_vsi *vsi = np->vsi;
  507. struct i40e_pf *pf = vsi->back;
  508. int i = 0;
  509. char *p;
  510. int j;
  511. struct rtnl_link_stats64 *net_stats = i40e_get_vsi_stats_struct(vsi);
  512. unsigned int start;
  513. i40e_update_stats(vsi);
  514. for (j = 0; j < I40E_NETDEV_STATS_LEN; j++) {
  515. p = (char *)net_stats + i40e_gstrings_net_stats[j].stat_offset;
  516. data[i++] = (i40e_gstrings_net_stats[j].sizeof_stat ==
  517. sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
  518. }
  519. rcu_read_lock();
  520. for (j = 0; j < vsi->num_queue_pairs; j++, i += 4) {
  521. struct i40e_ring *tx_ring = ACCESS_ONCE(vsi->tx_rings[j]);
  522. struct i40e_ring *rx_ring;
  523. if (!tx_ring)
  524. continue;
  525. /* process Tx ring statistics */
  526. do {
  527. start = u64_stats_fetch_begin_bh(&tx_ring->syncp);
  528. data[i] = tx_ring->stats.packets;
  529. data[i + 1] = tx_ring->stats.bytes;
  530. } while (u64_stats_fetch_retry_bh(&tx_ring->syncp, start));
  531. /* Rx ring is the 2nd half of the queue pair */
  532. rx_ring = &tx_ring[1];
  533. do {
  534. start = u64_stats_fetch_begin_bh(&rx_ring->syncp);
  535. data[i + 2] = rx_ring->stats.packets;
  536. data[i + 3] = rx_ring->stats.bytes;
  537. } while (u64_stats_fetch_retry_bh(&rx_ring->syncp, start));
  538. }
  539. rcu_read_unlock();
  540. if (vsi == pf->vsi[pf->lan_vsi]) {
  541. for (j = 0; j < I40E_GLOBAL_STATS_LEN; j++) {
  542. p = (char *)pf + i40e_gstrings_stats[j].stat_offset;
  543. data[i++] = (i40e_gstrings_stats[j].sizeof_stat ==
  544. sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
  545. }
  546. for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
  547. data[i++] = pf->stats.priority_xon_tx[j];
  548. data[i++] = pf->stats.priority_xoff_tx[j];
  549. }
  550. for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
  551. data[i++] = pf->stats.priority_xon_rx[j];
  552. data[i++] = pf->stats.priority_xoff_rx[j];
  553. }
  554. for (j = 0; j < I40E_MAX_USER_PRIORITY; j++)
  555. data[i++] = pf->stats.priority_xon_2_xoff[j];
  556. }
  557. }
  558. static void i40e_get_strings(struct net_device *netdev, u32 stringset,
  559. u8 *data)
  560. {
  561. struct i40e_netdev_priv *np = netdev_priv(netdev);
  562. struct i40e_vsi *vsi = np->vsi;
  563. struct i40e_pf *pf = vsi->back;
  564. char *p = (char *)data;
  565. int i;
  566. switch (stringset) {
  567. case ETH_SS_TEST:
  568. for (i = 0; i < I40E_TEST_LEN; i++) {
  569. memcpy(data, i40e_gstrings_test[i], ETH_GSTRING_LEN);
  570. data += ETH_GSTRING_LEN;
  571. }
  572. break;
  573. case ETH_SS_STATS:
  574. for (i = 0; i < I40E_NETDEV_STATS_LEN; i++) {
  575. snprintf(p, ETH_GSTRING_LEN, "%s",
  576. i40e_gstrings_net_stats[i].stat_string);
  577. p += ETH_GSTRING_LEN;
  578. }
  579. for (i = 0; i < vsi->num_queue_pairs; i++) {
  580. snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_packets", i);
  581. p += ETH_GSTRING_LEN;
  582. snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_bytes", i);
  583. p += ETH_GSTRING_LEN;
  584. snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_packets", i);
  585. p += ETH_GSTRING_LEN;
  586. snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_bytes", i);
  587. p += ETH_GSTRING_LEN;
  588. }
  589. if (vsi == pf->vsi[pf->lan_vsi]) {
  590. for (i = 0; i < I40E_GLOBAL_STATS_LEN; i++) {
  591. snprintf(p, ETH_GSTRING_LEN, "port.%s",
  592. i40e_gstrings_stats[i].stat_string);
  593. p += ETH_GSTRING_LEN;
  594. }
  595. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  596. snprintf(p, ETH_GSTRING_LEN,
  597. "port.tx_priority_%u_xon", i);
  598. p += ETH_GSTRING_LEN;
  599. snprintf(p, ETH_GSTRING_LEN,
  600. "port.tx_priority_%u_xoff", i);
  601. p += ETH_GSTRING_LEN;
  602. }
  603. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  604. snprintf(p, ETH_GSTRING_LEN,
  605. "port.rx_priority_%u_xon", i);
  606. p += ETH_GSTRING_LEN;
  607. snprintf(p, ETH_GSTRING_LEN,
  608. "port.rx_priority_%u_xoff", i);
  609. p += ETH_GSTRING_LEN;
  610. }
  611. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  612. snprintf(p, ETH_GSTRING_LEN,
  613. "port.rx_priority_%u_xon_2_xoff", i);
  614. p += ETH_GSTRING_LEN;
  615. }
  616. }
  617. /* BUG_ON(p - data != I40E_STATS_LEN * ETH_GSTRING_LEN); */
  618. break;
  619. }
  620. }
  621. static int i40e_get_ts_info(struct net_device *dev,
  622. struct ethtool_ts_info *info)
  623. {
  624. return ethtool_op_get_ts_info(dev, info);
  625. }
  626. static int i40e_link_test(struct i40e_pf *pf, u64 *data)
  627. {
  628. if (i40e_get_link_status(&pf->hw))
  629. *data = 0;
  630. else
  631. *data = 1;
  632. return *data;
  633. }
  634. static int i40e_reg_test(struct i40e_pf *pf, u64 *data)
  635. {
  636. i40e_status ret;
  637. ret = i40e_diag_reg_test(&pf->hw);
  638. *data = ret;
  639. return ret;
  640. }
  641. static int i40e_eeprom_test(struct i40e_pf *pf, u64 *data)
  642. {
  643. i40e_status ret;
  644. ret = i40e_diag_eeprom_test(&pf->hw);
  645. *data = ret;
  646. return ret;
  647. }
  648. static int i40e_intr_test(struct i40e_pf *pf, u64 *data)
  649. {
  650. *data = -ENOSYS;
  651. return *data;
  652. }
  653. static int i40e_loopback_test(struct i40e_pf *pf, u64 *data)
  654. {
  655. *data = -ENOSYS;
  656. return *data;
  657. }
  658. static void i40e_diag_test(struct net_device *netdev,
  659. struct ethtool_test *eth_test, u64 *data)
  660. {
  661. struct i40e_netdev_priv *np = netdev_priv(netdev);
  662. struct i40e_pf *pf = np->vsi->back;
  663. set_bit(__I40E_TESTING, &pf->state);
  664. if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
  665. /* Offline tests */
  666. netdev_info(netdev, "offline testing starting\n");
  667. /* Link test performed before hardware reset
  668. * so autoneg doesn't interfere with test result
  669. */
  670. netdev_info(netdev, "link test starting\n");
  671. if (i40e_link_test(pf, &data[I40E_ETH_TEST_LINK]))
  672. eth_test->flags |= ETH_TEST_FL_FAILED;
  673. netdev_info(netdev, "register test starting\n");
  674. if (i40e_reg_test(pf, &data[I40E_ETH_TEST_REG]))
  675. eth_test->flags |= ETH_TEST_FL_FAILED;
  676. i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
  677. netdev_info(netdev, "eeprom test starting\n");
  678. if (i40e_eeprom_test(pf, &data[I40E_ETH_TEST_EEPROM]))
  679. eth_test->flags |= ETH_TEST_FL_FAILED;
  680. i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
  681. netdev_info(netdev, "interrupt test starting\n");
  682. if (i40e_intr_test(pf, &data[I40E_ETH_TEST_INTR]))
  683. eth_test->flags |= ETH_TEST_FL_FAILED;
  684. i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
  685. netdev_info(netdev, "loopback test starting\n");
  686. if (i40e_loopback_test(pf, &data[I40E_ETH_TEST_LOOPBACK]))
  687. eth_test->flags |= ETH_TEST_FL_FAILED;
  688. } else {
  689. netdev_info(netdev, "online test starting\n");
  690. /* Online tests */
  691. if (i40e_link_test(pf, &data[I40E_ETH_TEST_LINK]))
  692. eth_test->flags |= ETH_TEST_FL_FAILED;
  693. /* Offline only tests, not run in online; pass by default */
  694. data[I40E_ETH_TEST_REG] = 0;
  695. data[I40E_ETH_TEST_EEPROM] = 0;
  696. data[I40E_ETH_TEST_INTR] = 0;
  697. data[I40E_ETH_TEST_LOOPBACK] = 0;
  698. clear_bit(__I40E_TESTING, &pf->state);
  699. }
  700. }
  701. static void i40e_get_wol(struct net_device *netdev,
  702. struct ethtool_wolinfo *wol)
  703. {
  704. wol->supported = 0;
  705. wol->wolopts = 0;
  706. }
  707. static int i40e_nway_reset(struct net_device *netdev)
  708. {
  709. /* restart autonegotiation */
  710. struct i40e_netdev_priv *np = netdev_priv(netdev);
  711. struct i40e_pf *pf = np->vsi->back;
  712. struct i40e_hw *hw = &pf->hw;
  713. i40e_status ret = 0;
  714. ret = i40e_aq_set_link_restart_an(hw, NULL);
  715. if (ret) {
  716. netdev_info(netdev, "link restart failed, aq_err=%d\n",
  717. pf->hw.aq.asq_last_status);
  718. return -EIO;
  719. }
  720. return 0;
  721. }
  722. static int i40e_set_phys_id(struct net_device *netdev,
  723. enum ethtool_phys_id_state state)
  724. {
  725. struct i40e_netdev_priv *np = netdev_priv(netdev);
  726. struct i40e_pf *pf = np->vsi->back;
  727. struct i40e_hw *hw = &pf->hw;
  728. int blink_freq = 2;
  729. switch (state) {
  730. case ETHTOOL_ID_ACTIVE:
  731. pf->led_status = i40e_led_get(hw);
  732. return blink_freq;
  733. case ETHTOOL_ID_ON:
  734. i40e_led_set(hw, 0xF);
  735. break;
  736. case ETHTOOL_ID_OFF:
  737. i40e_led_set(hw, 0x0);
  738. break;
  739. case ETHTOOL_ID_INACTIVE:
  740. i40e_led_set(hw, pf->led_status);
  741. break;
  742. }
  743. return 0;
  744. }
  745. /* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
  746. * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
  747. * 125us (8000 interrupts per second) == ITR(62)
  748. */
  749. static int i40e_get_coalesce(struct net_device *netdev,
  750. struct ethtool_coalesce *ec)
  751. {
  752. struct i40e_netdev_priv *np = netdev_priv(netdev);
  753. struct i40e_vsi *vsi = np->vsi;
  754. ec->tx_max_coalesced_frames_irq = vsi->work_limit;
  755. ec->rx_max_coalesced_frames_irq = vsi->work_limit;
  756. if (ITR_IS_DYNAMIC(vsi->rx_itr_setting))
  757. ec->rx_coalesce_usecs = 1;
  758. else
  759. ec->rx_coalesce_usecs = vsi->rx_itr_setting;
  760. if (ITR_IS_DYNAMIC(vsi->tx_itr_setting))
  761. ec->tx_coalesce_usecs = 1;
  762. else
  763. ec->tx_coalesce_usecs = vsi->tx_itr_setting;
  764. return 0;
  765. }
  766. static int i40e_set_coalesce(struct net_device *netdev,
  767. struct ethtool_coalesce *ec)
  768. {
  769. struct i40e_netdev_priv *np = netdev_priv(netdev);
  770. struct i40e_q_vector *q_vector;
  771. struct i40e_vsi *vsi = np->vsi;
  772. struct i40e_pf *pf = vsi->back;
  773. struct i40e_hw *hw = &pf->hw;
  774. u16 vector;
  775. int i;
  776. if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
  777. vsi->work_limit = ec->tx_max_coalesced_frames_irq;
  778. switch (ec->rx_coalesce_usecs) {
  779. case 0:
  780. vsi->rx_itr_setting = 0;
  781. break;
  782. case 1:
  783. vsi->rx_itr_setting = (I40E_ITR_DYNAMIC |
  784. ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
  785. break;
  786. default:
  787. if ((ec->rx_coalesce_usecs < (I40E_MIN_ITR << 1)) ||
  788. (ec->rx_coalesce_usecs > (I40E_MAX_ITR << 1)))
  789. return -EINVAL;
  790. vsi->rx_itr_setting = ec->rx_coalesce_usecs;
  791. break;
  792. }
  793. switch (ec->tx_coalesce_usecs) {
  794. case 0:
  795. vsi->tx_itr_setting = 0;
  796. break;
  797. case 1:
  798. vsi->tx_itr_setting = (I40E_ITR_DYNAMIC |
  799. ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
  800. break;
  801. default:
  802. if ((ec->tx_coalesce_usecs < (I40E_MIN_ITR << 1)) ||
  803. (ec->tx_coalesce_usecs > (I40E_MAX_ITR << 1)))
  804. return -EINVAL;
  805. vsi->tx_itr_setting = ec->tx_coalesce_usecs;
  806. break;
  807. }
  808. vector = vsi->base_vector;
  809. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  810. q_vector = vsi->q_vectors[i];
  811. q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
  812. wr32(hw, I40E_PFINT_ITRN(0, vector - 1), q_vector->rx.itr);
  813. q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
  814. wr32(hw, I40E_PFINT_ITRN(1, vector - 1), q_vector->tx.itr);
  815. i40e_flush(hw);
  816. }
  817. return 0;
  818. }
  819. /**
  820. * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
  821. * @pf: pointer to the physical function struct
  822. * @cmd: ethtool rxnfc command
  823. *
  824. * Returns Success if the flow is supported, else Invalid Input.
  825. **/
  826. static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
  827. {
  828. cmd->data = 0;
  829. /* Report default options for RSS on i40e */
  830. switch (cmd->flow_type) {
  831. case TCP_V4_FLOW:
  832. case UDP_V4_FLOW:
  833. cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
  834. /* fall through to add IP fields */
  835. case SCTP_V4_FLOW:
  836. case AH_ESP_V4_FLOW:
  837. case AH_V4_FLOW:
  838. case ESP_V4_FLOW:
  839. case IPV4_FLOW:
  840. cmd->data |= RXH_IP_SRC | RXH_IP_DST;
  841. break;
  842. case TCP_V6_FLOW:
  843. case UDP_V6_FLOW:
  844. cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
  845. /* fall through to add IP fields */
  846. case SCTP_V6_FLOW:
  847. case AH_ESP_V6_FLOW:
  848. case AH_V6_FLOW:
  849. case ESP_V6_FLOW:
  850. case IPV6_FLOW:
  851. cmd->data |= RXH_IP_SRC | RXH_IP_DST;
  852. break;
  853. default:
  854. return -EINVAL;
  855. }
  856. return 0;
  857. }
  858. /**
  859. * i40e_get_rxnfc - command to get RX flow classification rules
  860. * @netdev: network interface device structure
  861. * @cmd: ethtool rxnfc command
  862. *
  863. * Returns Success if the command is supported.
  864. **/
  865. static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
  866. u32 *rule_locs)
  867. {
  868. struct i40e_netdev_priv *np = netdev_priv(netdev);
  869. struct i40e_vsi *vsi = np->vsi;
  870. struct i40e_pf *pf = vsi->back;
  871. int ret = -EOPNOTSUPP;
  872. switch (cmd->cmd) {
  873. case ETHTOOL_GRXRINGS:
  874. cmd->data = vsi->alloc_queue_pairs;
  875. ret = 0;
  876. break;
  877. case ETHTOOL_GRXFH:
  878. ret = i40e_get_rss_hash_opts(pf, cmd);
  879. break;
  880. case ETHTOOL_GRXCLSRLCNT:
  881. ret = 0;
  882. break;
  883. case ETHTOOL_GRXCLSRULE:
  884. ret = 0;
  885. break;
  886. case ETHTOOL_GRXCLSRLALL:
  887. cmd->data = 500;
  888. ret = 0;
  889. default:
  890. break;
  891. }
  892. return ret;
  893. }
  894. /**
  895. * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
  896. * @pf: pointer to the physical function struct
  897. * @cmd: ethtool rxnfc command
  898. *
  899. * Returns Success if the flow input set is supported.
  900. **/
  901. static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
  902. {
  903. struct i40e_hw *hw = &pf->hw;
  904. u64 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
  905. ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
  906. /* RSS does not support anything other than hashing
  907. * to queues on src and dst IPs and ports
  908. */
  909. if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
  910. RXH_L4_B_0_1 | RXH_L4_B_2_3))
  911. return -EINVAL;
  912. /* We need at least the IP SRC and DEST fields for hashing */
  913. if (!(nfc->data & RXH_IP_SRC) ||
  914. !(nfc->data & RXH_IP_DST))
  915. return -EINVAL;
  916. switch (nfc->flow_type) {
  917. case TCP_V4_FLOW:
  918. switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  919. case 0:
  920. hena &= ~((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
  921. break;
  922. case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
  923. hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
  924. break;
  925. default:
  926. return -EINVAL;
  927. }
  928. break;
  929. case TCP_V6_FLOW:
  930. switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  931. case 0:
  932. hena &= ~((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
  933. break;
  934. case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
  935. hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
  936. break;
  937. default:
  938. return -EINVAL;
  939. }
  940. break;
  941. case UDP_V4_FLOW:
  942. switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  943. case 0:
  944. hena &=
  945. ~(((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
  946. ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |
  947. ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4));
  948. break;
  949. case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
  950. hena |=
  951. (((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
  952. ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |
  953. ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4));
  954. break;
  955. default:
  956. return -EINVAL;
  957. }
  958. break;
  959. case UDP_V6_FLOW:
  960. switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  961. case 0:
  962. hena &=
  963. ~(((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
  964. ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |
  965. ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6));
  966. break;
  967. case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
  968. hena |=
  969. (((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
  970. ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |
  971. ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6));
  972. break;
  973. default:
  974. return -EINVAL;
  975. }
  976. break;
  977. case AH_ESP_V4_FLOW:
  978. case AH_V4_FLOW:
  979. case ESP_V4_FLOW:
  980. case SCTP_V4_FLOW:
  981. if ((nfc->data & RXH_L4_B_0_1) ||
  982. (nfc->data & RXH_L4_B_2_3))
  983. return -EINVAL;
  984. hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
  985. break;
  986. case AH_ESP_V6_FLOW:
  987. case AH_V6_FLOW:
  988. case ESP_V6_FLOW:
  989. case SCTP_V6_FLOW:
  990. if ((nfc->data & RXH_L4_B_0_1) ||
  991. (nfc->data & RXH_L4_B_2_3))
  992. return -EINVAL;
  993. hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
  994. break;
  995. case IPV4_FLOW:
  996. hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
  997. ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4);
  998. break;
  999. case IPV6_FLOW:
  1000. hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
  1001. ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6);
  1002. break;
  1003. default:
  1004. return -EINVAL;
  1005. }
  1006. wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
  1007. wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  1008. i40e_flush(hw);
  1009. return 0;
  1010. }
  1011. #define IP_HEADER_OFFSET 14
  1012. /**
  1013. * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 Flow Director filters for
  1014. * a specific flow spec
  1015. * @vsi: pointer to the targeted VSI
  1016. * @fd_data: the flow director data required from the FDir descriptor
  1017. * @ethtool_rx_flow_spec: the flow spec
  1018. * @add: true adds a filter, false removes it
  1019. *
  1020. * Returns 0 if the filters were successfully added or removed
  1021. **/
  1022. static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
  1023. struct i40e_fdir_data *fd_data,
  1024. struct ethtool_rx_flow_spec *fsp, bool add)
  1025. {
  1026. struct i40e_pf *pf = vsi->back;
  1027. struct udphdr *udp;
  1028. struct iphdr *ip;
  1029. bool err = false;
  1030. int ret;
  1031. int i;
  1032. ip = (struct iphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET);
  1033. udp = (struct udphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET
  1034. + sizeof(struct iphdr));
  1035. ip->saddr = fsp->h_u.tcp_ip4_spec.ip4src;
  1036. ip->daddr = fsp->h_u.tcp_ip4_spec.ip4dst;
  1037. udp->source = fsp->h_u.tcp_ip4_spec.psrc;
  1038. udp->dest = fsp->h_u.tcp_ip4_spec.pdst;
  1039. for (i = I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP;
  1040. i <= I40E_FILTER_PCTYPE_NONF_IPV4_UDP; i++) {
  1041. fd_data->pctype = i;
  1042. ret = i40e_program_fdir_filter(fd_data, pf, add);
  1043. if (ret) {
  1044. dev_info(&pf->pdev->dev,
  1045. "Filter command send failed for PCTYPE %d (ret = %d)\n",
  1046. fd_data->pctype, ret);
  1047. err = true;
  1048. } else {
  1049. dev_info(&pf->pdev->dev,
  1050. "Filter OK for PCTYPE %d (ret = %d)\n",
  1051. fd_data->pctype, ret);
  1052. }
  1053. }
  1054. return err ? -EOPNOTSUPP : 0;
  1055. }
  1056. /**
  1057. * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 Flow Director filters for
  1058. * a specific flow spec
  1059. * @vsi: pointer to the targeted VSI
  1060. * @fd_data: the flow director data required from the FDir descriptor
  1061. * @ethtool_rx_flow_spec: the flow spec
  1062. * @add: true adds a filter, false removes it
  1063. *
  1064. * Returns 0 if the filters were successfully added or removed
  1065. **/
  1066. static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
  1067. struct i40e_fdir_data *fd_data,
  1068. struct ethtool_rx_flow_spec *fsp, bool add)
  1069. {
  1070. struct i40e_pf *pf = vsi->back;
  1071. struct tcphdr *tcp;
  1072. struct iphdr *ip;
  1073. bool err = false;
  1074. int ret;
  1075. ip = (struct iphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET);
  1076. tcp = (struct tcphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET
  1077. + sizeof(struct iphdr));
  1078. ip->daddr = fsp->h_u.tcp_ip4_spec.ip4dst;
  1079. tcp->dest = fsp->h_u.tcp_ip4_spec.pdst;
  1080. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN;
  1081. ret = i40e_program_fdir_filter(fd_data, pf, add);
  1082. if (ret) {
  1083. dev_info(&pf->pdev->dev,
  1084. "Filter command send failed for PCTYPE %d (ret = %d)\n",
  1085. fd_data->pctype, ret);
  1086. err = true;
  1087. } else {
  1088. dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d (ret = %d)\n",
  1089. fd_data->pctype, ret);
  1090. }
  1091. ip->saddr = fsp->h_u.tcp_ip4_spec.ip4src;
  1092. tcp->source = fsp->h_u.tcp_ip4_spec.psrc;
  1093. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  1094. ret = i40e_program_fdir_filter(fd_data, pf, add);
  1095. if (ret) {
  1096. dev_info(&pf->pdev->dev,
  1097. "Filter command send failed for PCTYPE %d (ret = %d)\n",
  1098. fd_data->pctype, ret);
  1099. err = true;
  1100. } else {
  1101. dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d (ret = %d)\n",
  1102. fd_data->pctype, ret);
  1103. }
  1104. return err ? -EOPNOTSUPP : 0;
  1105. }
  1106. /**
  1107. * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
  1108. * a specific flow spec
  1109. * @vsi: pointer to the targeted VSI
  1110. * @fd_data: the flow director data required from the FDir descriptor
  1111. * @ethtool_rx_flow_spec: the flow spec
  1112. * @add: true adds a filter, false removes it
  1113. *
  1114. * Returns 0 if the filters were successfully added or removed
  1115. **/
  1116. static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
  1117. struct i40e_fdir_data *fd_data,
  1118. struct ethtool_rx_flow_spec *fsp, bool add)
  1119. {
  1120. return -EOPNOTSUPP;
  1121. }
  1122. /**
  1123. * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
  1124. * a specific flow spec
  1125. * @vsi: pointer to the targeted VSI
  1126. * @fd_data: the flow director data required for the FDir descriptor
  1127. * @fsp: the ethtool flow spec
  1128. * @add: true adds a filter, false removes it
  1129. *
  1130. * Returns 0 if the filters were successfully added or removed
  1131. **/
  1132. static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
  1133. struct i40e_fdir_data *fd_data,
  1134. struct ethtool_rx_flow_spec *fsp, bool add)
  1135. {
  1136. struct i40e_pf *pf = vsi->back;
  1137. struct iphdr *ip;
  1138. bool err = false;
  1139. int ret;
  1140. int i;
  1141. ip = (struct iphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET);
  1142. ip->saddr = fsp->h_u.usr_ip4_spec.ip4src;
  1143. ip->daddr = fsp->h_u.usr_ip4_spec.ip4dst;
  1144. ip->protocol = fsp->h_u.usr_ip4_spec.proto;
  1145. for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
  1146. i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
  1147. fd_data->pctype = i;
  1148. ret = i40e_program_fdir_filter(fd_data, pf, add);
  1149. if (ret) {
  1150. dev_info(&pf->pdev->dev,
  1151. "Filter command send failed for PCTYPE %d (ret = %d)\n",
  1152. fd_data->pctype, ret);
  1153. err = true;
  1154. } else {
  1155. dev_info(&pf->pdev->dev,
  1156. "Filter OK for PCTYPE %d (ret = %d)\n",
  1157. fd_data->pctype, ret);
  1158. }
  1159. }
  1160. return err ? -EOPNOTSUPP : 0;
  1161. }
  1162. /**
  1163. * i40e_add_del_fdir_ethtool - Add/Remove Flow Director filters for
  1164. * a specific flow spec based on their protocol
  1165. * @vsi: pointer to the targeted VSI
  1166. * @cmd: command to get or set RX flow classification rules
  1167. * @add: true adds a filter, false removes it
  1168. *
  1169. * Returns 0 if the filters were successfully added or removed
  1170. **/
  1171. static int i40e_add_del_fdir_ethtool(struct i40e_vsi *vsi,
  1172. struct ethtool_rxnfc *cmd, bool add)
  1173. {
  1174. struct i40e_fdir_data fd_data;
  1175. int ret = -EINVAL;
  1176. struct i40e_pf *pf;
  1177. struct ethtool_rx_flow_spec *fsp =
  1178. (struct ethtool_rx_flow_spec *)&cmd->fs;
  1179. if (!vsi)
  1180. return -EINVAL;
  1181. pf = vsi->back;
  1182. if ((fsp->ring_cookie != RX_CLS_FLOW_DISC) &&
  1183. (fsp->ring_cookie >= vsi->num_queue_pairs))
  1184. return -EINVAL;
  1185. /* Populate the Flow Director that we have at the moment
  1186. * and allocate the raw packet buffer for the calling functions
  1187. */
  1188. fd_data.raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_LOOKUP,
  1189. GFP_KERNEL);
  1190. if (!fd_data.raw_packet) {
  1191. dev_info(&pf->pdev->dev, "Could not allocate memory\n");
  1192. return -ENOMEM;
  1193. }
  1194. fd_data.q_index = fsp->ring_cookie;
  1195. fd_data.flex_off = 0;
  1196. fd_data.pctype = 0;
  1197. fd_data.dest_vsi = vsi->id;
  1198. fd_data.dest_ctl = 0;
  1199. fd_data.fd_status = 0;
  1200. fd_data.cnt_index = 0;
  1201. fd_data.fd_id = 0;
  1202. switch (fsp->flow_type & ~FLOW_EXT) {
  1203. case TCP_V4_FLOW:
  1204. ret = i40e_add_del_fdir_tcpv4(vsi, &fd_data, fsp, add);
  1205. break;
  1206. case UDP_V4_FLOW:
  1207. ret = i40e_add_del_fdir_udpv4(vsi, &fd_data, fsp, add);
  1208. break;
  1209. case SCTP_V4_FLOW:
  1210. ret = i40e_add_del_fdir_sctpv4(vsi, &fd_data, fsp, add);
  1211. break;
  1212. case IPV4_FLOW:
  1213. ret = i40e_add_del_fdir_ipv4(vsi, &fd_data, fsp, add);
  1214. break;
  1215. case IP_USER_FLOW:
  1216. switch (fsp->h_u.usr_ip4_spec.proto) {
  1217. case IPPROTO_TCP:
  1218. ret = i40e_add_del_fdir_tcpv4(vsi, &fd_data, fsp, add);
  1219. break;
  1220. case IPPROTO_UDP:
  1221. ret = i40e_add_del_fdir_udpv4(vsi, &fd_data, fsp, add);
  1222. break;
  1223. case IPPROTO_SCTP:
  1224. ret = i40e_add_del_fdir_sctpv4(vsi, &fd_data, fsp, add);
  1225. break;
  1226. default:
  1227. ret = i40e_add_del_fdir_ipv4(vsi, &fd_data, fsp, add);
  1228. break;
  1229. }
  1230. break;
  1231. default:
  1232. dev_info(&pf->pdev->dev, "Could not specify spec type\n");
  1233. ret = -EINVAL;
  1234. }
  1235. kfree(fd_data.raw_packet);
  1236. fd_data.raw_packet = NULL;
  1237. return ret;
  1238. }
  1239. /**
  1240. * i40e_set_rxnfc - command to set RX flow classification rules
  1241. * @netdev: network interface device structure
  1242. * @cmd: ethtool rxnfc command
  1243. *
  1244. * Returns Success if the command is supported.
  1245. **/
  1246. static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
  1247. {
  1248. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1249. struct i40e_vsi *vsi = np->vsi;
  1250. struct i40e_pf *pf = vsi->back;
  1251. int ret = -EOPNOTSUPP;
  1252. switch (cmd->cmd) {
  1253. case ETHTOOL_SRXFH:
  1254. ret = i40e_set_rss_hash_opt(pf, cmd);
  1255. break;
  1256. case ETHTOOL_SRXCLSRLINS:
  1257. ret = i40e_add_del_fdir_ethtool(vsi, cmd, true);
  1258. break;
  1259. case ETHTOOL_SRXCLSRLDEL:
  1260. ret = i40e_add_del_fdir_ethtool(vsi, cmd, false);
  1261. break;
  1262. default:
  1263. break;
  1264. }
  1265. return ret;
  1266. }
  1267. static const struct ethtool_ops i40e_ethtool_ops = {
  1268. .get_settings = i40e_get_settings,
  1269. .get_drvinfo = i40e_get_drvinfo,
  1270. .get_regs_len = i40e_get_regs_len,
  1271. .get_regs = i40e_get_regs,
  1272. .nway_reset = i40e_nway_reset,
  1273. .get_link = ethtool_op_get_link,
  1274. .get_wol = i40e_get_wol,
  1275. .get_eeprom_len = i40e_get_eeprom_len,
  1276. .get_eeprom = i40e_get_eeprom,
  1277. .get_ringparam = i40e_get_ringparam,
  1278. .set_ringparam = i40e_set_ringparam,
  1279. .get_pauseparam = i40e_get_pauseparam,
  1280. .get_msglevel = i40e_get_msglevel,
  1281. .set_msglevel = i40e_set_msglevel,
  1282. .get_rxnfc = i40e_get_rxnfc,
  1283. .set_rxnfc = i40e_set_rxnfc,
  1284. .self_test = i40e_diag_test,
  1285. .get_strings = i40e_get_strings,
  1286. .set_phys_id = i40e_set_phys_id,
  1287. .get_sset_count = i40e_get_sset_count,
  1288. .get_ethtool_stats = i40e_get_ethtool_stats,
  1289. .get_coalesce = i40e_get_coalesce,
  1290. .set_coalesce = i40e_set_coalesce,
  1291. .get_ts_info = i40e_get_ts_info,
  1292. };
  1293. void i40e_set_ethtool_ops(struct net_device *netdev)
  1294. {
  1295. SET_ETHTOOL_OPS(netdev, &i40e_ethtool_ops);
  1296. }