ena_ethtool.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * Copyright 2015 Amazon.com, Inc. or its affiliates.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/pci.h>
  33. #include "ena_netdev.h"
  34. struct ena_stats {
  35. char name[ETH_GSTRING_LEN];
  36. int stat_offset;
  37. };
  38. #define ENA_STAT_ENA_COM_ENTRY(stat) { \
  39. .name = #stat, \
  40. .stat_offset = offsetof(struct ena_com_stats_admin, stat) \
  41. }
  42. #define ENA_STAT_ENTRY(stat, stat_type) { \
  43. .name = #stat, \
  44. .stat_offset = offsetof(struct ena_stats_##stat_type, stat) \
  45. }
  46. #define ENA_STAT_RX_ENTRY(stat) \
  47. ENA_STAT_ENTRY(stat, rx)
  48. #define ENA_STAT_TX_ENTRY(stat) \
  49. ENA_STAT_ENTRY(stat, tx)
  50. #define ENA_STAT_GLOBAL_ENTRY(stat) \
  51. ENA_STAT_ENTRY(stat, dev)
  52. static const struct ena_stats ena_stats_global_strings[] = {
  53. ENA_STAT_GLOBAL_ENTRY(tx_timeout),
  54. ENA_STAT_GLOBAL_ENTRY(io_suspend),
  55. ENA_STAT_GLOBAL_ENTRY(io_resume),
  56. ENA_STAT_GLOBAL_ENTRY(wd_expired),
  57. ENA_STAT_GLOBAL_ENTRY(interface_up),
  58. ENA_STAT_GLOBAL_ENTRY(interface_down),
  59. ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
  60. };
  61. static const struct ena_stats ena_stats_tx_strings[] = {
  62. ENA_STAT_TX_ENTRY(cnt),
  63. ENA_STAT_TX_ENTRY(bytes),
  64. ENA_STAT_TX_ENTRY(queue_stop),
  65. ENA_STAT_TX_ENTRY(queue_wakeup),
  66. ENA_STAT_TX_ENTRY(dma_mapping_err),
  67. ENA_STAT_TX_ENTRY(linearize),
  68. ENA_STAT_TX_ENTRY(linearize_failed),
  69. ENA_STAT_TX_ENTRY(napi_comp),
  70. ENA_STAT_TX_ENTRY(tx_poll),
  71. ENA_STAT_TX_ENTRY(doorbells),
  72. ENA_STAT_TX_ENTRY(prepare_ctx_err),
  73. ENA_STAT_TX_ENTRY(missing_tx_comp),
  74. ENA_STAT_TX_ENTRY(bad_req_id),
  75. };
  76. static const struct ena_stats ena_stats_rx_strings[] = {
  77. ENA_STAT_RX_ENTRY(cnt),
  78. ENA_STAT_RX_ENTRY(bytes),
  79. ENA_STAT_RX_ENTRY(refil_partial),
  80. ENA_STAT_RX_ENTRY(bad_csum),
  81. ENA_STAT_RX_ENTRY(page_alloc_fail),
  82. ENA_STAT_RX_ENTRY(skb_alloc_fail),
  83. ENA_STAT_RX_ENTRY(dma_mapping_err),
  84. ENA_STAT_RX_ENTRY(bad_desc_num),
  85. ENA_STAT_RX_ENTRY(rx_copybreak_pkt),
  86. };
  87. static const struct ena_stats ena_stats_ena_com_strings[] = {
  88. ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
  89. ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
  90. ENA_STAT_ENA_COM_ENTRY(completed_cmd),
  91. ENA_STAT_ENA_COM_ENTRY(out_of_space),
  92. ENA_STAT_ENA_COM_ENTRY(no_completion),
  93. };
  94. #define ENA_STATS_ARRAY_GLOBAL ARRAY_SIZE(ena_stats_global_strings)
  95. #define ENA_STATS_ARRAY_TX ARRAY_SIZE(ena_stats_tx_strings)
  96. #define ENA_STATS_ARRAY_RX ARRAY_SIZE(ena_stats_rx_strings)
  97. #define ENA_STATS_ARRAY_ENA_COM ARRAY_SIZE(ena_stats_ena_com_strings)
  98. static void ena_safe_update_stat(u64 *src, u64 *dst,
  99. struct u64_stats_sync *syncp)
  100. {
  101. unsigned int start;
  102. do {
  103. start = u64_stats_fetch_begin_irq(syncp);
  104. *(dst) = *src;
  105. } while (u64_stats_fetch_retry_irq(syncp, start));
  106. }
  107. static void ena_queue_stats(struct ena_adapter *adapter, u64 **data)
  108. {
  109. const struct ena_stats *ena_stats;
  110. struct ena_ring *ring;
  111. u64 *ptr;
  112. int i, j;
  113. for (i = 0; i < adapter->num_queues; i++) {
  114. /* Tx stats */
  115. ring = &adapter->tx_ring[i];
  116. for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
  117. ena_stats = &ena_stats_tx_strings[j];
  118. ptr = (u64 *)((uintptr_t)&ring->tx_stats +
  119. (uintptr_t)ena_stats->stat_offset);
  120. ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
  121. }
  122. /* Rx stats */
  123. ring = &adapter->rx_ring[i];
  124. for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
  125. ena_stats = &ena_stats_rx_strings[j];
  126. ptr = (u64 *)((uintptr_t)&ring->rx_stats +
  127. (uintptr_t)ena_stats->stat_offset);
  128. ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
  129. }
  130. }
  131. }
  132. static void ena_dev_admin_queue_stats(struct ena_adapter *adapter, u64 **data)
  133. {
  134. const struct ena_stats *ena_stats;
  135. u32 *ptr;
  136. int i;
  137. for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
  138. ena_stats = &ena_stats_ena_com_strings[i];
  139. ptr = (u32 *)((uintptr_t)&adapter->ena_dev->admin_queue.stats +
  140. (uintptr_t)ena_stats->stat_offset);
  141. *(*data)++ = *ptr;
  142. }
  143. }
  144. static void ena_get_ethtool_stats(struct net_device *netdev,
  145. struct ethtool_stats *stats,
  146. u64 *data)
  147. {
  148. struct ena_adapter *adapter = netdev_priv(netdev);
  149. const struct ena_stats *ena_stats;
  150. u64 *ptr;
  151. int i;
  152. for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
  153. ena_stats = &ena_stats_global_strings[i];
  154. ptr = (u64 *)((uintptr_t)&adapter->dev_stats +
  155. (uintptr_t)ena_stats->stat_offset);
  156. ena_safe_update_stat(ptr, data++, &adapter->syncp);
  157. }
  158. ena_queue_stats(adapter, &data);
  159. ena_dev_admin_queue_stats(adapter, &data);
  160. }
  161. int ena_get_sset_count(struct net_device *netdev, int sset)
  162. {
  163. struct ena_adapter *adapter = netdev_priv(netdev);
  164. if (sset != ETH_SS_STATS)
  165. return -EOPNOTSUPP;
  166. return adapter->num_queues * (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX)
  167. + ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
  168. }
  169. static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
  170. {
  171. const struct ena_stats *ena_stats;
  172. int i, j;
  173. for (i = 0; i < adapter->num_queues; i++) {
  174. /* Tx stats */
  175. for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
  176. ena_stats = &ena_stats_tx_strings[j];
  177. snprintf(*data, ETH_GSTRING_LEN,
  178. "queue_%u_tx_%s", i, ena_stats->name);
  179. (*data) += ETH_GSTRING_LEN;
  180. }
  181. /* Rx stats */
  182. for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
  183. ena_stats = &ena_stats_rx_strings[j];
  184. snprintf(*data, ETH_GSTRING_LEN,
  185. "queue_%u_rx_%s", i, ena_stats->name);
  186. (*data) += ETH_GSTRING_LEN;
  187. }
  188. }
  189. }
  190. static void ena_com_dev_strings(u8 **data)
  191. {
  192. const struct ena_stats *ena_stats;
  193. int i;
  194. for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
  195. ena_stats = &ena_stats_ena_com_strings[i];
  196. snprintf(*data, ETH_GSTRING_LEN,
  197. "ena_admin_q_%s", ena_stats->name);
  198. (*data) += ETH_GSTRING_LEN;
  199. }
  200. }
  201. static void ena_get_strings(struct net_device *netdev, u32 sset, u8 *data)
  202. {
  203. struct ena_adapter *adapter = netdev_priv(netdev);
  204. const struct ena_stats *ena_stats;
  205. int i;
  206. if (sset != ETH_SS_STATS)
  207. return;
  208. for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
  209. ena_stats = &ena_stats_global_strings[i];
  210. memcpy(data, ena_stats->name, ETH_GSTRING_LEN);
  211. data += ETH_GSTRING_LEN;
  212. }
  213. ena_queue_strings(adapter, &data);
  214. ena_com_dev_strings(&data);
  215. }
  216. static int ena_get_link_ksettings(struct net_device *netdev,
  217. struct ethtool_link_ksettings *link_ksettings)
  218. {
  219. struct ena_adapter *adapter = netdev_priv(netdev);
  220. struct ena_com_dev *ena_dev = adapter->ena_dev;
  221. struct ena_admin_get_feature_link_desc *link;
  222. struct ena_admin_get_feat_resp feat_resp;
  223. int rc;
  224. rc = ena_com_get_link_params(ena_dev, &feat_resp);
  225. if (rc)
  226. return rc;
  227. link = &feat_resp.u.link;
  228. link_ksettings->base.speed = link->speed;
  229. if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
  230. ethtool_link_ksettings_add_link_mode(link_ksettings,
  231. supported, Autoneg);
  232. ethtool_link_ksettings_add_link_mode(link_ksettings,
  233. supported, Autoneg);
  234. }
  235. link_ksettings->base.autoneg =
  236. (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
  237. AUTONEG_ENABLE : AUTONEG_DISABLE;
  238. link_ksettings->base.duplex = DUPLEX_FULL;
  239. return 0;
  240. }
  241. static int ena_get_coalesce(struct net_device *net_dev,
  242. struct ethtool_coalesce *coalesce)
  243. {
  244. struct ena_adapter *adapter = netdev_priv(net_dev);
  245. struct ena_com_dev *ena_dev = adapter->ena_dev;
  246. struct ena_intr_moder_entry intr_moder_entry;
  247. if (!ena_com_interrupt_moderation_supported(ena_dev)) {
  248. /* the devie doesn't support interrupt moderation */
  249. return -EOPNOTSUPP;
  250. }
  251. coalesce->tx_coalesce_usecs =
  252. ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) /
  253. ena_dev->intr_delay_resolution;
  254. if (!ena_com_get_adaptive_moderation_enabled(ena_dev)) {
  255. coalesce->rx_coalesce_usecs =
  256. ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
  257. / ena_dev->intr_delay_resolution;
  258. } else {
  259. ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
  260. coalesce->rx_coalesce_usecs_low = intr_moder_entry.intr_moder_interval;
  261. coalesce->rx_max_coalesced_frames_low = intr_moder_entry.pkts_per_interval;
  262. ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
  263. coalesce->rx_coalesce_usecs = intr_moder_entry.intr_moder_interval;
  264. coalesce->rx_max_coalesced_frames = intr_moder_entry.pkts_per_interval;
  265. ena_com_get_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
  266. coalesce->rx_coalesce_usecs_high = intr_moder_entry.intr_moder_interval;
  267. coalesce->rx_max_coalesced_frames_high = intr_moder_entry.pkts_per_interval;
  268. }
  269. coalesce->use_adaptive_rx_coalesce =
  270. ena_com_get_adaptive_moderation_enabled(ena_dev);
  271. return 0;
  272. }
  273. static void ena_update_tx_rings_intr_moderation(struct ena_adapter *adapter)
  274. {
  275. unsigned int val;
  276. int i;
  277. val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
  278. for (i = 0; i < adapter->num_queues; i++)
  279. adapter->tx_ring[i].smoothed_interval = val;
  280. }
  281. static int ena_set_coalesce(struct net_device *net_dev,
  282. struct ethtool_coalesce *coalesce)
  283. {
  284. struct ena_adapter *adapter = netdev_priv(net_dev);
  285. struct ena_com_dev *ena_dev = adapter->ena_dev;
  286. struct ena_intr_moder_entry intr_moder_entry;
  287. int rc;
  288. if (!ena_com_interrupt_moderation_supported(ena_dev)) {
  289. /* the devie doesn't support interrupt moderation */
  290. return -EOPNOTSUPP;
  291. }
  292. if (coalesce->rx_coalesce_usecs_irq ||
  293. coalesce->rx_max_coalesced_frames_irq ||
  294. coalesce->tx_coalesce_usecs_irq ||
  295. coalesce->tx_max_coalesced_frames ||
  296. coalesce->tx_max_coalesced_frames_irq ||
  297. coalesce->stats_block_coalesce_usecs ||
  298. coalesce->use_adaptive_tx_coalesce ||
  299. coalesce->pkt_rate_low ||
  300. coalesce->tx_coalesce_usecs_low ||
  301. coalesce->tx_max_coalesced_frames_low ||
  302. coalesce->pkt_rate_high ||
  303. coalesce->tx_coalesce_usecs_high ||
  304. coalesce->tx_max_coalesced_frames_high ||
  305. coalesce->rate_sample_interval)
  306. return -EINVAL;
  307. rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
  308. coalesce->tx_coalesce_usecs);
  309. if (rc)
  310. return rc;
  311. ena_update_tx_rings_intr_moderation(adapter);
  312. if (ena_com_get_adaptive_moderation_enabled(ena_dev)) {
  313. if (!coalesce->use_adaptive_rx_coalesce) {
  314. ena_com_disable_adaptive_moderation(ena_dev);
  315. rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
  316. coalesce->rx_coalesce_usecs);
  317. return rc;
  318. }
  319. } else { /* was in non-adaptive mode */
  320. if (coalesce->use_adaptive_rx_coalesce) {
  321. ena_com_enable_adaptive_moderation(ena_dev);
  322. } else {
  323. rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
  324. coalesce->rx_coalesce_usecs);
  325. return rc;
  326. }
  327. }
  328. intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_low;
  329. intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_low;
  330. intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
  331. ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_LOWEST, &intr_moder_entry);
  332. intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs;
  333. intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames;
  334. intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
  335. ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_MID, &intr_moder_entry);
  336. intr_moder_entry.intr_moder_interval = coalesce->rx_coalesce_usecs_high;
  337. intr_moder_entry.pkts_per_interval = coalesce->rx_max_coalesced_frames_high;
  338. intr_moder_entry.bytes_per_interval = ENA_INTR_BYTE_COUNT_NOT_SUPPORTED;
  339. ena_com_init_intr_moderation_entry(adapter->ena_dev, ENA_INTR_MODER_HIGHEST, &intr_moder_entry);
  340. return 0;
  341. }
  342. static u32 ena_get_msglevel(struct net_device *netdev)
  343. {
  344. struct ena_adapter *adapter = netdev_priv(netdev);
  345. return adapter->msg_enable;
  346. }
  347. static void ena_set_msglevel(struct net_device *netdev, u32 value)
  348. {
  349. struct ena_adapter *adapter = netdev_priv(netdev);
  350. adapter->msg_enable = value;
  351. }
  352. static void ena_get_drvinfo(struct net_device *dev,
  353. struct ethtool_drvinfo *info)
  354. {
  355. struct ena_adapter *adapter = netdev_priv(dev);
  356. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  357. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  358. strlcpy(info->bus_info, pci_name(adapter->pdev),
  359. sizeof(info->bus_info));
  360. }
  361. static void ena_get_ringparam(struct net_device *netdev,
  362. struct ethtool_ringparam *ring)
  363. {
  364. struct ena_adapter *adapter = netdev_priv(netdev);
  365. struct ena_ring *tx_ring = &adapter->tx_ring[0];
  366. struct ena_ring *rx_ring = &adapter->rx_ring[0];
  367. ring->rx_max_pending = rx_ring->ring_size;
  368. ring->tx_max_pending = tx_ring->ring_size;
  369. ring->rx_pending = rx_ring->ring_size;
  370. ring->tx_pending = tx_ring->ring_size;
  371. }
  372. static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
  373. {
  374. u32 data = 0;
  375. if (hash_fields & ENA_ADMIN_RSS_L2_DA)
  376. data |= RXH_L2DA;
  377. if (hash_fields & ENA_ADMIN_RSS_L3_DA)
  378. data |= RXH_IP_DST;
  379. if (hash_fields & ENA_ADMIN_RSS_L3_SA)
  380. data |= RXH_IP_SRC;
  381. if (hash_fields & ENA_ADMIN_RSS_L4_DP)
  382. data |= RXH_L4_B_2_3;
  383. if (hash_fields & ENA_ADMIN_RSS_L4_SP)
  384. data |= RXH_L4_B_0_1;
  385. return data;
  386. }
  387. static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
  388. {
  389. u16 data = 0;
  390. if (hash_fields & RXH_L2DA)
  391. data |= ENA_ADMIN_RSS_L2_DA;
  392. if (hash_fields & RXH_IP_DST)
  393. data |= ENA_ADMIN_RSS_L3_DA;
  394. if (hash_fields & RXH_IP_SRC)
  395. data |= ENA_ADMIN_RSS_L3_SA;
  396. if (hash_fields & RXH_L4_B_2_3)
  397. data |= ENA_ADMIN_RSS_L4_DP;
  398. if (hash_fields & RXH_L4_B_0_1)
  399. data |= ENA_ADMIN_RSS_L4_SP;
  400. return data;
  401. }
  402. static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
  403. struct ethtool_rxnfc *cmd)
  404. {
  405. enum ena_admin_flow_hash_proto proto;
  406. u16 hash_fields;
  407. int rc;
  408. cmd->data = 0;
  409. switch (cmd->flow_type) {
  410. case TCP_V4_FLOW:
  411. proto = ENA_ADMIN_RSS_TCP4;
  412. break;
  413. case UDP_V4_FLOW:
  414. proto = ENA_ADMIN_RSS_UDP4;
  415. break;
  416. case TCP_V6_FLOW:
  417. proto = ENA_ADMIN_RSS_TCP6;
  418. break;
  419. case UDP_V6_FLOW:
  420. proto = ENA_ADMIN_RSS_UDP6;
  421. break;
  422. case IPV4_FLOW:
  423. proto = ENA_ADMIN_RSS_IP4;
  424. break;
  425. case IPV6_FLOW:
  426. proto = ENA_ADMIN_RSS_IP6;
  427. break;
  428. case ETHER_FLOW:
  429. proto = ENA_ADMIN_RSS_NOT_IP;
  430. break;
  431. case AH_V4_FLOW:
  432. case ESP_V4_FLOW:
  433. case AH_V6_FLOW:
  434. case ESP_V6_FLOW:
  435. case SCTP_V4_FLOW:
  436. case AH_ESP_V4_FLOW:
  437. return -EOPNOTSUPP;
  438. default:
  439. return -EINVAL;
  440. }
  441. rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
  442. if (rc) {
  443. /* If device don't have permission, return unsupported */
  444. if (rc == -EPERM)
  445. rc = -EOPNOTSUPP;
  446. return rc;
  447. }
  448. cmd->data = ena_flow_hash_to_flow_type(hash_fields);
  449. return 0;
  450. }
  451. static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
  452. struct ethtool_rxnfc *cmd)
  453. {
  454. enum ena_admin_flow_hash_proto proto;
  455. u16 hash_fields;
  456. switch (cmd->flow_type) {
  457. case TCP_V4_FLOW:
  458. proto = ENA_ADMIN_RSS_TCP4;
  459. break;
  460. case UDP_V4_FLOW:
  461. proto = ENA_ADMIN_RSS_UDP4;
  462. break;
  463. case TCP_V6_FLOW:
  464. proto = ENA_ADMIN_RSS_TCP6;
  465. break;
  466. case UDP_V6_FLOW:
  467. proto = ENA_ADMIN_RSS_UDP6;
  468. break;
  469. case IPV4_FLOW:
  470. proto = ENA_ADMIN_RSS_IP4;
  471. break;
  472. case IPV6_FLOW:
  473. proto = ENA_ADMIN_RSS_IP6;
  474. break;
  475. case ETHER_FLOW:
  476. proto = ENA_ADMIN_RSS_NOT_IP;
  477. break;
  478. case AH_V4_FLOW:
  479. case ESP_V4_FLOW:
  480. case AH_V6_FLOW:
  481. case ESP_V6_FLOW:
  482. case SCTP_V4_FLOW:
  483. case AH_ESP_V4_FLOW:
  484. return -EOPNOTSUPP;
  485. default:
  486. return -EINVAL;
  487. }
  488. hash_fields = ena_flow_data_to_flow_hash(cmd->data);
  489. return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
  490. }
  491. static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
  492. {
  493. struct ena_adapter *adapter = netdev_priv(netdev);
  494. int rc = 0;
  495. switch (info->cmd) {
  496. case ETHTOOL_SRXFH:
  497. rc = ena_set_rss_hash(adapter->ena_dev, info);
  498. break;
  499. case ETHTOOL_SRXCLSRLDEL:
  500. case ETHTOOL_SRXCLSRLINS:
  501. default:
  502. netif_err(adapter, drv, netdev,
  503. "Command parameter %d is not supported\n", info->cmd);
  504. rc = -EOPNOTSUPP;
  505. }
  506. return (rc == -EPERM) ? -EOPNOTSUPP : rc;
  507. }
  508. static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
  509. u32 *rules)
  510. {
  511. struct ena_adapter *adapter = netdev_priv(netdev);
  512. int rc = 0;
  513. switch (info->cmd) {
  514. case ETHTOOL_GRXRINGS:
  515. info->data = adapter->num_queues;
  516. rc = 0;
  517. break;
  518. case ETHTOOL_GRXFH:
  519. rc = ena_get_rss_hash(adapter->ena_dev, info);
  520. break;
  521. case ETHTOOL_GRXCLSRLCNT:
  522. case ETHTOOL_GRXCLSRULE:
  523. case ETHTOOL_GRXCLSRLALL:
  524. default:
  525. netif_err(adapter, drv, netdev,
  526. "Command parameter %d is not supported\n", info->cmd);
  527. rc = -EOPNOTSUPP;
  528. }
  529. return (rc == -EPERM) ? -EOPNOTSUPP : rc;
  530. }
  531. static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
  532. {
  533. return ENA_RX_RSS_TABLE_SIZE;
  534. }
  535. static u32 ena_get_rxfh_key_size(struct net_device *netdev)
  536. {
  537. return ENA_HASH_KEY_SIZE;
  538. }
  539. static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
  540. u8 *hfunc)
  541. {
  542. struct ena_adapter *adapter = netdev_priv(netdev);
  543. enum ena_admin_hash_functions ena_func;
  544. u8 func;
  545. int rc;
  546. rc = ena_com_indirect_table_get(adapter->ena_dev, indir);
  547. if (rc)
  548. return rc;
  549. rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func, key);
  550. if (rc)
  551. return rc;
  552. switch (ena_func) {
  553. case ENA_ADMIN_TOEPLITZ:
  554. func = ETH_RSS_HASH_TOP;
  555. break;
  556. case ENA_ADMIN_CRC32:
  557. func = ETH_RSS_HASH_XOR;
  558. break;
  559. default:
  560. netif_err(adapter, drv, netdev,
  561. "Command parameter is not supported\n");
  562. return -EOPNOTSUPP;
  563. }
  564. if (hfunc)
  565. *hfunc = func;
  566. return rc;
  567. }
  568. static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
  569. const u8 *key, const u8 hfunc)
  570. {
  571. struct ena_adapter *adapter = netdev_priv(netdev);
  572. struct ena_com_dev *ena_dev = adapter->ena_dev;
  573. enum ena_admin_hash_functions func;
  574. int rc, i;
  575. if (indir) {
  576. for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
  577. rc = ena_com_indirect_table_fill_entry(ena_dev,
  578. ENA_IO_RXQ_IDX(indir[i]),
  579. i);
  580. if (unlikely(rc)) {
  581. netif_err(adapter, drv, netdev,
  582. "Cannot fill indirect table (index is too large)\n");
  583. return rc;
  584. }
  585. }
  586. rc = ena_com_indirect_table_set(ena_dev);
  587. if (rc) {
  588. netif_err(adapter, drv, netdev,
  589. "Cannot set indirect table\n");
  590. return rc == -EPERM ? -EOPNOTSUPP : rc;
  591. }
  592. }
  593. switch (hfunc) {
  594. case ETH_RSS_HASH_TOP:
  595. func = ENA_ADMIN_TOEPLITZ;
  596. break;
  597. case ETH_RSS_HASH_XOR:
  598. func = ENA_ADMIN_CRC32;
  599. break;
  600. default:
  601. netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
  602. hfunc);
  603. return -EOPNOTSUPP;
  604. }
  605. if (key) {
  606. rc = ena_com_fill_hash_function(ena_dev, func, key,
  607. ENA_HASH_KEY_SIZE,
  608. 0xFFFFFFFF);
  609. if (unlikely(rc)) {
  610. netif_err(adapter, drv, netdev, "Cannot fill key\n");
  611. return rc == -EPERM ? -EOPNOTSUPP : rc;
  612. }
  613. }
  614. return 0;
  615. }
  616. static void ena_get_channels(struct net_device *netdev,
  617. struct ethtool_channels *channels)
  618. {
  619. struct ena_adapter *adapter = netdev_priv(netdev);
  620. channels->max_rx = ENA_MAX_NUM_IO_QUEUES;
  621. channels->max_tx = ENA_MAX_NUM_IO_QUEUES;
  622. channels->max_other = 0;
  623. channels->max_combined = 0;
  624. channels->rx_count = adapter->num_queues;
  625. channels->tx_count = adapter->num_queues;
  626. channels->other_count = 0;
  627. channels->combined_count = 0;
  628. }
  629. static int ena_get_tunable(struct net_device *netdev,
  630. const struct ethtool_tunable *tuna, void *data)
  631. {
  632. struct ena_adapter *adapter = netdev_priv(netdev);
  633. int ret = 0;
  634. switch (tuna->id) {
  635. case ETHTOOL_RX_COPYBREAK:
  636. *(u32 *)data = adapter->rx_copybreak;
  637. break;
  638. default:
  639. ret = -EINVAL;
  640. break;
  641. }
  642. return ret;
  643. }
  644. static int ena_set_tunable(struct net_device *netdev,
  645. const struct ethtool_tunable *tuna,
  646. const void *data)
  647. {
  648. struct ena_adapter *adapter = netdev_priv(netdev);
  649. int ret = 0;
  650. u32 len;
  651. switch (tuna->id) {
  652. case ETHTOOL_RX_COPYBREAK:
  653. len = *(u32 *)data;
  654. if (len > adapter->netdev->mtu) {
  655. ret = -EINVAL;
  656. break;
  657. }
  658. adapter->rx_copybreak = len;
  659. break;
  660. default:
  661. ret = -EINVAL;
  662. break;
  663. }
  664. return ret;
  665. }
  666. static const struct ethtool_ops ena_ethtool_ops = {
  667. .get_link_ksettings = ena_get_link_ksettings,
  668. .get_drvinfo = ena_get_drvinfo,
  669. .get_msglevel = ena_get_msglevel,
  670. .set_msglevel = ena_set_msglevel,
  671. .get_link = ethtool_op_get_link,
  672. .get_coalesce = ena_get_coalesce,
  673. .set_coalesce = ena_set_coalesce,
  674. .get_ringparam = ena_get_ringparam,
  675. .get_sset_count = ena_get_sset_count,
  676. .get_strings = ena_get_strings,
  677. .get_ethtool_stats = ena_get_ethtool_stats,
  678. .get_rxnfc = ena_get_rxnfc,
  679. .set_rxnfc = ena_set_rxnfc,
  680. .get_rxfh_indir_size = ena_get_rxfh_indir_size,
  681. .get_rxfh_key_size = ena_get_rxfh_key_size,
  682. .get_rxfh = ena_get_rxfh,
  683. .set_rxfh = ena_set_rxfh,
  684. .get_channels = ena_get_channels,
  685. .get_tunable = ena_get_tunable,
  686. .set_tunable = ena_set_tunable,
  687. };
  688. void ena_set_ethtool_ops(struct net_device *netdev)
  689. {
  690. netdev->ethtool_ops = &ena_ethtool_ops;
  691. }
  692. static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
  693. {
  694. struct net_device *netdev = adapter->netdev;
  695. u8 *strings_buf;
  696. u64 *data_buf;
  697. int strings_num;
  698. int i, rc;
  699. strings_num = ena_get_sset_count(netdev, ETH_SS_STATS);
  700. if (strings_num <= 0) {
  701. netif_err(adapter, drv, netdev, "Can't get stats num\n");
  702. return;
  703. }
  704. strings_buf = devm_kzalloc(&adapter->pdev->dev,
  705. strings_num * ETH_GSTRING_LEN,
  706. GFP_ATOMIC);
  707. if (!strings_buf) {
  708. netif_err(adapter, drv, netdev,
  709. "failed to alloc strings_buf\n");
  710. return;
  711. }
  712. data_buf = devm_kzalloc(&adapter->pdev->dev,
  713. strings_num * sizeof(u64),
  714. GFP_ATOMIC);
  715. if (!data_buf) {
  716. netif_err(adapter, drv, netdev,
  717. "failed to allocate data buf\n");
  718. devm_kfree(&adapter->pdev->dev, strings_buf);
  719. return;
  720. }
  721. ena_get_strings(netdev, ETH_SS_STATS, strings_buf);
  722. ena_get_ethtool_stats(netdev, NULL, data_buf);
  723. /* If there is a buffer, dump stats, otherwise print them to dmesg */
  724. if (buf)
  725. for (i = 0; i < strings_num; i++) {
  726. rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
  727. "%s %llu\n",
  728. strings_buf + i * ETH_GSTRING_LEN,
  729. data_buf[i]);
  730. buf += rc;
  731. }
  732. else
  733. for (i = 0; i < strings_num; i++)
  734. netif_err(adapter, drv, netdev, "%s: %llu\n",
  735. strings_buf + i * ETH_GSTRING_LEN,
  736. data_buf[i]);
  737. devm_kfree(&adapter->pdev->dev, strings_buf);
  738. devm_kfree(&adapter->pdev->dev, data_buf);
  739. }
  740. void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
  741. {
  742. if (!buf)
  743. return;
  744. ena_dump_stats_ex(adapter, buf);
  745. }
  746. void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
  747. {
  748. ena_dump_stats_ex(adapter, NULL);
  749. }