ena_ethtool.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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(suspend),
  55. ENA_STAT_GLOBAL_ENTRY(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(bad_req_id),
  74. ENA_STAT_TX_ENTRY(llq_buffer_copy),
  75. ENA_STAT_TX_ENTRY(missed_tx),
  76. };
  77. static const struct ena_stats ena_stats_rx_strings[] = {
  78. ENA_STAT_RX_ENTRY(cnt),
  79. ENA_STAT_RX_ENTRY(bytes),
  80. ENA_STAT_RX_ENTRY(rx_copybreak_pkt),
  81. ENA_STAT_RX_ENTRY(csum_good),
  82. ENA_STAT_RX_ENTRY(refil_partial),
  83. ENA_STAT_RX_ENTRY(bad_csum),
  84. ENA_STAT_RX_ENTRY(page_alloc_fail),
  85. ENA_STAT_RX_ENTRY(skb_alloc_fail),
  86. ENA_STAT_RX_ENTRY(dma_mapping_err),
  87. ENA_STAT_RX_ENTRY(bad_desc_num),
  88. ENA_STAT_RX_ENTRY(bad_req_id),
  89. ENA_STAT_RX_ENTRY(empty_rx_ring),
  90. ENA_STAT_RX_ENTRY(csum_unchecked),
  91. };
  92. static const struct ena_stats ena_stats_ena_com_strings[] = {
  93. ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
  94. ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
  95. ENA_STAT_ENA_COM_ENTRY(completed_cmd),
  96. ENA_STAT_ENA_COM_ENTRY(out_of_space),
  97. ENA_STAT_ENA_COM_ENTRY(no_completion),
  98. };
  99. #define ENA_STATS_ARRAY_GLOBAL ARRAY_SIZE(ena_stats_global_strings)
  100. #define ENA_STATS_ARRAY_TX ARRAY_SIZE(ena_stats_tx_strings)
  101. #define ENA_STATS_ARRAY_RX ARRAY_SIZE(ena_stats_rx_strings)
  102. #define ENA_STATS_ARRAY_ENA_COM ARRAY_SIZE(ena_stats_ena_com_strings)
  103. static void ena_safe_update_stat(u64 *src, u64 *dst,
  104. struct u64_stats_sync *syncp)
  105. {
  106. unsigned int start;
  107. do {
  108. start = u64_stats_fetch_begin_irq(syncp);
  109. *(dst) = *src;
  110. } while (u64_stats_fetch_retry_irq(syncp, start));
  111. }
  112. static void ena_queue_stats(struct ena_adapter *adapter, u64 **data)
  113. {
  114. const struct ena_stats *ena_stats;
  115. struct ena_ring *ring;
  116. u64 *ptr;
  117. int i, j;
  118. for (i = 0; i < adapter->num_queues; i++) {
  119. /* Tx stats */
  120. ring = &adapter->tx_ring[i];
  121. for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
  122. ena_stats = &ena_stats_tx_strings[j];
  123. ptr = (u64 *)((uintptr_t)&ring->tx_stats +
  124. (uintptr_t)ena_stats->stat_offset);
  125. ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
  126. }
  127. /* Rx stats */
  128. ring = &adapter->rx_ring[i];
  129. for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
  130. ena_stats = &ena_stats_rx_strings[j];
  131. ptr = (u64 *)((uintptr_t)&ring->rx_stats +
  132. (uintptr_t)ena_stats->stat_offset);
  133. ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
  134. }
  135. }
  136. }
  137. static void ena_dev_admin_queue_stats(struct ena_adapter *adapter, u64 **data)
  138. {
  139. const struct ena_stats *ena_stats;
  140. u32 *ptr;
  141. int i;
  142. for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
  143. ena_stats = &ena_stats_ena_com_strings[i];
  144. ptr = (u32 *)((uintptr_t)&adapter->ena_dev->admin_queue.stats +
  145. (uintptr_t)ena_stats->stat_offset);
  146. *(*data)++ = *ptr;
  147. }
  148. }
  149. static void ena_get_ethtool_stats(struct net_device *netdev,
  150. struct ethtool_stats *stats,
  151. u64 *data)
  152. {
  153. struct ena_adapter *adapter = netdev_priv(netdev);
  154. const struct ena_stats *ena_stats;
  155. u64 *ptr;
  156. int i;
  157. for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
  158. ena_stats = &ena_stats_global_strings[i];
  159. ptr = (u64 *)((uintptr_t)&adapter->dev_stats +
  160. (uintptr_t)ena_stats->stat_offset);
  161. ena_safe_update_stat(ptr, data++, &adapter->syncp);
  162. }
  163. ena_queue_stats(adapter, &data);
  164. ena_dev_admin_queue_stats(adapter, &data);
  165. }
  166. int ena_get_sset_count(struct net_device *netdev, int sset)
  167. {
  168. struct ena_adapter *adapter = netdev_priv(netdev);
  169. if (sset != ETH_SS_STATS)
  170. return -EOPNOTSUPP;
  171. return adapter->num_queues * (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX)
  172. + ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
  173. }
  174. static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
  175. {
  176. const struct ena_stats *ena_stats;
  177. int i, j;
  178. for (i = 0; i < adapter->num_queues; i++) {
  179. /* Tx stats */
  180. for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
  181. ena_stats = &ena_stats_tx_strings[j];
  182. snprintf(*data, ETH_GSTRING_LEN,
  183. "queue_%u_tx_%s", i, ena_stats->name);
  184. (*data) += ETH_GSTRING_LEN;
  185. }
  186. /* Rx stats */
  187. for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
  188. ena_stats = &ena_stats_rx_strings[j];
  189. snprintf(*data, ETH_GSTRING_LEN,
  190. "queue_%u_rx_%s", i, ena_stats->name);
  191. (*data) += ETH_GSTRING_LEN;
  192. }
  193. }
  194. }
  195. static void ena_com_dev_strings(u8 **data)
  196. {
  197. const struct ena_stats *ena_stats;
  198. int i;
  199. for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
  200. ena_stats = &ena_stats_ena_com_strings[i];
  201. snprintf(*data, ETH_GSTRING_LEN,
  202. "ena_admin_q_%s", ena_stats->name);
  203. (*data) += ETH_GSTRING_LEN;
  204. }
  205. }
  206. static void ena_get_strings(struct net_device *netdev, u32 sset, u8 *data)
  207. {
  208. struct ena_adapter *adapter = netdev_priv(netdev);
  209. const struct ena_stats *ena_stats;
  210. int i;
  211. if (sset != ETH_SS_STATS)
  212. return;
  213. for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
  214. ena_stats = &ena_stats_global_strings[i];
  215. memcpy(data, ena_stats->name, ETH_GSTRING_LEN);
  216. data += ETH_GSTRING_LEN;
  217. }
  218. ena_queue_strings(adapter, &data);
  219. ena_com_dev_strings(&data);
  220. }
  221. static int ena_get_link_ksettings(struct net_device *netdev,
  222. struct ethtool_link_ksettings *link_ksettings)
  223. {
  224. struct ena_adapter *adapter = netdev_priv(netdev);
  225. struct ena_com_dev *ena_dev = adapter->ena_dev;
  226. struct ena_admin_get_feature_link_desc *link;
  227. struct ena_admin_get_feat_resp feat_resp;
  228. int rc;
  229. rc = ena_com_get_link_params(ena_dev, &feat_resp);
  230. if (rc)
  231. return rc;
  232. link = &feat_resp.u.link;
  233. link_ksettings->base.speed = link->speed;
  234. if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
  235. ethtool_link_ksettings_add_link_mode(link_ksettings,
  236. supported, Autoneg);
  237. ethtool_link_ksettings_add_link_mode(link_ksettings,
  238. supported, Autoneg);
  239. }
  240. link_ksettings->base.autoneg =
  241. (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
  242. AUTONEG_ENABLE : AUTONEG_DISABLE;
  243. link_ksettings->base.duplex = DUPLEX_FULL;
  244. return 0;
  245. }
  246. static int ena_get_coalesce(struct net_device *net_dev,
  247. struct ethtool_coalesce *coalesce)
  248. {
  249. struct ena_adapter *adapter = netdev_priv(net_dev);
  250. struct ena_com_dev *ena_dev = adapter->ena_dev;
  251. if (!ena_com_interrupt_moderation_supported(ena_dev)) {
  252. /* the devie doesn't support interrupt moderation */
  253. return -EOPNOTSUPP;
  254. }
  255. coalesce->tx_coalesce_usecs =
  256. ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) *
  257. ena_dev->intr_delay_resolution;
  258. if (!ena_com_get_adaptive_moderation_enabled(ena_dev))
  259. coalesce->rx_coalesce_usecs =
  260. ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
  261. * ena_dev->intr_delay_resolution;
  262. coalesce->use_adaptive_rx_coalesce =
  263. ena_com_get_adaptive_moderation_enabled(ena_dev);
  264. return 0;
  265. }
  266. static void ena_update_tx_rings_intr_moderation(struct ena_adapter *adapter)
  267. {
  268. unsigned int val;
  269. int i;
  270. val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
  271. for (i = 0; i < adapter->num_queues; i++)
  272. adapter->tx_ring[i].smoothed_interval = val;
  273. }
  274. static void ena_update_rx_rings_intr_moderation(struct ena_adapter *adapter)
  275. {
  276. unsigned int val;
  277. int i;
  278. val = ena_com_get_nonadaptive_moderation_interval_rx(adapter->ena_dev);
  279. for (i = 0; i < adapter->num_queues; i++)
  280. adapter->rx_ring[i].smoothed_interval = val;
  281. }
  282. static int ena_set_coalesce(struct net_device *net_dev,
  283. struct ethtool_coalesce *coalesce)
  284. {
  285. struct ena_adapter *adapter = netdev_priv(net_dev);
  286. struct ena_com_dev *ena_dev = adapter->ena_dev;
  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. rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
  293. coalesce->tx_coalesce_usecs);
  294. if (rc)
  295. return rc;
  296. ena_update_tx_rings_intr_moderation(adapter);
  297. if (coalesce->use_adaptive_rx_coalesce) {
  298. if (!ena_com_get_adaptive_moderation_enabled(ena_dev))
  299. ena_com_enable_adaptive_moderation(ena_dev);
  300. return 0;
  301. }
  302. rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
  303. coalesce->rx_coalesce_usecs);
  304. if (rc)
  305. return rc;
  306. ena_update_rx_rings_intr_moderation(adapter);
  307. if (!coalesce->use_adaptive_rx_coalesce) {
  308. if (ena_com_get_adaptive_moderation_enabled(ena_dev))
  309. ena_com_disable_adaptive_moderation(ena_dev);
  310. }
  311. return 0;
  312. }
  313. static u32 ena_get_msglevel(struct net_device *netdev)
  314. {
  315. struct ena_adapter *adapter = netdev_priv(netdev);
  316. return adapter->msg_enable;
  317. }
  318. static void ena_set_msglevel(struct net_device *netdev, u32 value)
  319. {
  320. struct ena_adapter *adapter = netdev_priv(netdev);
  321. adapter->msg_enable = value;
  322. }
  323. static void ena_get_drvinfo(struct net_device *dev,
  324. struct ethtool_drvinfo *info)
  325. {
  326. struct ena_adapter *adapter = netdev_priv(dev);
  327. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  328. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  329. strlcpy(info->bus_info, pci_name(adapter->pdev),
  330. sizeof(info->bus_info));
  331. }
  332. static void ena_get_ringparam(struct net_device *netdev,
  333. struct ethtool_ringparam *ring)
  334. {
  335. struct ena_adapter *adapter = netdev_priv(netdev);
  336. ring->tx_max_pending = adapter->max_tx_ring_size;
  337. ring->rx_max_pending = adapter->max_rx_ring_size;
  338. ring->tx_pending = adapter->tx_ring[0].ring_size;
  339. ring->rx_pending = adapter->rx_ring[0].ring_size;
  340. }
  341. static int ena_set_ringparam(struct net_device *netdev,
  342. struct ethtool_ringparam *ring)
  343. {
  344. struct ena_adapter *adapter = netdev_priv(netdev);
  345. u32 new_tx_size, new_rx_size;
  346. new_tx_size = ring->tx_pending < ENA_MIN_RING_SIZE ?
  347. ENA_MIN_RING_SIZE : ring->tx_pending;
  348. new_tx_size = rounddown_pow_of_two(new_tx_size);
  349. new_rx_size = ring->rx_pending < ENA_MIN_RING_SIZE ?
  350. ENA_MIN_RING_SIZE : ring->rx_pending;
  351. new_rx_size = rounddown_pow_of_two(new_rx_size);
  352. if (new_tx_size == adapter->requested_tx_ring_size &&
  353. new_rx_size == adapter->requested_rx_ring_size)
  354. return 0;
  355. return ena_update_queue_sizes(adapter, new_tx_size, new_rx_size);
  356. }
  357. static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
  358. {
  359. u32 data = 0;
  360. if (hash_fields & ENA_ADMIN_RSS_L2_DA)
  361. data |= RXH_L2DA;
  362. if (hash_fields & ENA_ADMIN_RSS_L3_DA)
  363. data |= RXH_IP_DST;
  364. if (hash_fields & ENA_ADMIN_RSS_L3_SA)
  365. data |= RXH_IP_SRC;
  366. if (hash_fields & ENA_ADMIN_RSS_L4_DP)
  367. data |= RXH_L4_B_2_3;
  368. if (hash_fields & ENA_ADMIN_RSS_L4_SP)
  369. data |= RXH_L4_B_0_1;
  370. return data;
  371. }
  372. static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
  373. {
  374. u16 data = 0;
  375. if (hash_fields & RXH_L2DA)
  376. data |= ENA_ADMIN_RSS_L2_DA;
  377. if (hash_fields & RXH_IP_DST)
  378. data |= ENA_ADMIN_RSS_L3_DA;
  379. if (hash_fields & RXH_IP_SRC)
  380. data |= ENA_ADMIN_RSS_L3_SA;
  381. if (hash_fields & RXH_L4_B_2_3)
  382. data |= ENA_ADMIN_RSS_L4_DP;
  383. if (hash_fields & RXH_L4_B_0_1)
  384. data |= ENA_ADMIN_RSS_L4_SP;
  385. return data;
  386. }
  387. static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
  388. struct ethtool_rxnfc *cmd)
  389. {
  390. enum ena_admin_flow_hash_proto proto;
  391. u16 hash_fields;
  392. int rc;
  393. cmd->data = 0;
  394. switch (cmd->flow_type) {
  395. case TCP_V4_FLOW:
  396. proto = ENA_ADMIN_RSS_TCP4;
  397. break;
  398. case UDP_V4_FLOW:
  399. proto = ENA_ADMIN_RSS_UDP4;
  400. break;
  401. case TCP_V6_FLOW:
  402. proto = ENA_ADMIN_RSS_TCP6;
  403. break;
  404. case UDP_V6_FLOW:
  405. proto = ENA_ADMIN_RSS_UDP6;
  406. break;
  407. case IPV4_FLOW:
  408. proto = ENA_ADMIN_RSS_IP4;
  409. break;
  410. case IPV6_FLOW:
  411. proto = ENA_ADMIN_RSS_IP6;
  412. break;
  413. case ETHER_FLOW:
  414. proto = ENA_ADMIN_RSS_NOT_IP;
  415. break;
  416. case AH_V4_FLOW:
  417. case ESP_V4_FLOW:
  418. case AH_V6_FLOW:
  419. case ESP_V6_FLOW:
  420. case SCTP_V4_FLOW:
  421. case AH_ESP_V4_FLOW:
  422. return -EOPNOTSUPP;
  423. default:
  424. return -EINVAL;
  425. }
  426. rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
  427. if (rc)
  428. return rc;
  429. cmd->data = ena_flow_hash_to_flow_type(hash_fields);
  430. return 0;
  431. }
  432. static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
  433. struct ethtool_rxnfc *cmd)
  434. {
  435. enum ena_admin_flow_hash_proto proto;
  436. u16 hash_fields;
  437. switch (cmd->flow_type) {
  438. case TCP_V4_FLOW:
  439. proto = ENA_ADMIN_RSS_TCP4;
  440. break;
  441. case UDP_V4_FLOW:
  442. proto = ENA_ADMIN_RSS_UDP4;
  443. break;
  444. case TCP_V6_FLOW:
  445. proto = ENA_ADMIN_RSS_TCP6;
  446. break;
  447. case UDP_V6_FLOW:
  448. proto = ENA_ADMIN_RSS_UDP6;
  449. break;
  450. case IPV4_FLOW:
  451. proto = ENA_ADMIN_RSS_IP4;
  452. break;
  453. case IPV6_FLOW:
  454. proto = ENA_ADMIN_RSS_IP6;
  455. break;
  456. case ETHER_FLOW:
  457. proto = ENA_ADMIN_RSS_NOT_IP;
  458. break;
  459. case AH_V4_FLOW:
  460. case ESP_V4_FLOW:
  461. case AH_V6_FLOW:
  462. case ESP_V6_FLOW:
  463. case SCTP_V4_FLOW:
  464. case AH_ESP_V4_FLOW:
  465. return -EOPNOTSUPP;
  466. default:
  467. return -EINVAL;
  468. }
  469. hash_fields = ena_flow_data_to_flow_hash(cmd->data);
  470. return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
  471. }
  472. static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
  473. {
  474. struct ena_adapter *adapter = netdev_priv(netdev);
  475. int rc = 0;
  476. switch (info->cmd) {
  477. case ETHTOOL_SRXFH:
  478. rc = ena_set_rss_hash(adapter->ena_dev, info);
  479. break;
  480. case ETHTOOL_SRXCLSRLDEL:
  481. case ETHTOOL_SRXCLSRLINS:
  482. default:
  483. netif_err(adapter, drv, netdev,
  484. "Command parameter %d is not supported\n", info->cmd);
  485. rc = -EOPNOTSUPP;
  486. }
  487. return rc;
  488. }
  489. static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
  490. u32 *rules)
  491. {
  492. struct ena_adapter *adapter = netdev_priv(netdev);
  493. int rc = 0;
  494. switch (info->cmd) {
  495. case ETHTOOL_GRXRINGS:
  496. info->data = adapter->num_queues;
  497. rc = 0;
  498. break;
  499. case ETHTOOL_GRXFH:
  500. rc = ena_get_rss_hash(adapter->ena_dev, info);
  501. break;
  502. case ETHTOOL_GRXCLSRLCNT:
  503. case ETHTOOL_GRXCLSRULE:
  504. case ETHTOOL_GRXCLSRLALL:
  505. default:
  506. netif_err(adapter, drv, netdev,
  507. "Command parameter %d is not supported\n", info->cmd);
  508. rc = -EOPNOTSUPP;
  509. }
  510. return rc;
  511. }
  512. static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
  513. {
  514. return ENA_RX_RSS_TABLE_SIZE;
  515. }
  516. static u32 ena_get_rxfh_key_size(struct net_device *netdev)
  517. {
  518. return ENA_HASH_KEY_SIZE;
  519. }
  520. static int ena_indirection_table_get(struct ena_adapter *adapter, u32 *indir)
  521. {
  522. struct ena_com_dev *ena_dev = adapter->ena_dev;
  523. int i, rc;
  524. if (!indir)
  525. return 0;
  526. rc = ena_com_indirect_table_get(ena_dev, indir);
  527. if (rc)
  528. return rc;
  529. /* Our internal representation of the indices is: even indices
  530. * for Tx and uneven indices for Rx. We need to convert the Rx
  531. * indices to be consecutive
  532. */
  533. for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++)
  534. indir[i] = ENA_IO_RXQ_IDX_TO_COMBINED_IDX(indir[i]);
  535. return rc;
  536. }
  537. static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
  538. u8 *hfunc)
  539. {
  540. struct ena_adapter *adapter = netdev_priv(netdev);
  541. enum ena_admin_hash_functions ena_func;
  542. u8 func;
  543. int rc;
  544. rc = ena_indirection_table_get(adapter, indir);
  545. if (rc)
  546. return rc;
  547. /* We call this function in order to check if the device
  548. * supports getting/setting the hash function.
  549. */
  550. rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func, key);
  551. if (rc) {
  552. if (rc == -EOPNOTSUPP) {
  553. key = NULL;
  554. hfunc = NULL;
  555. rc = 0;
  556. }
  557. return rc;
  558. }
  559. if (rc)
  560. return rc;
  561. switch (ena_func) {
  562. case ENA_ADMIN_TOEPLITZ:
  563. func = ETH_RSS_HASH_TOP;
  564. break;
  565. case ENA_ADMIN_CRC32:
  566. func = ETH_RSS_HASH_CRC32;
  567. break;
  568. default:
  569. netif_err(adapter, drv, netdev,
  570. "Command parameter is not supported\n");
  571. return -EOPNOTSUPP;
  572. }
  573. if (hfunc)
  574. *hfunc = func;
  575. return rc;
  576. }
  577. static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
  578. const u8 *key, const u8 hfunc)
  579. {
  580. struct ena_adapter *adapter = netdev_priv(netdev);
  581. struct ena_com_dev *ena_dev = adapter->ena_dev;
  582. enum ena_admin_hash_functions func;
  583. int rc, i;
  584. if (indir) {
  585. for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
  586. rc = ena_com_indirect_table_fill_entry(ena_dev,
  587. i,
  588. ENA_IO_RXQ_IDX(indir[i]));
  589. if (unlikely(rc)) {
  590. netif_err(adapter, drv, netdev,
  591. "Cannot fill indirect table (index is too large)\n");
  592. return rc;
  593. }
  594. }
  595. rc = ena_com_indirect_table_set(ena_dev);
  596. if (rc) {
  597. netif_err(adapter, drv, netdev,
  598. "Cannot set indirect table\n");
  599. return rc == -EPERM ? -EOPNOTSUPP : rc;
  600. }
  601. }
  602. switch (hfunc) {
  603. case ETH_RSS_HASH_NO_CHANGE:
  604. func = ena_com_get_current_hash_function(ena_dev);
  605. break;
  606. case ETH_RSS_HASH_TOP:
  607. func = ENA_ADMIN_TOEPLITZ;
  608. break;
  609. case ETH_RSS_HASH_CRC32:
  610. func = ENA_ADMIN_CRC32;
  611. break;
  612. default:
  613. netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
  614. hfunc);
  615. return -EOPNOTSUPP;
  616. }
  617. if (key) {
  618. rc = ena_com_fill_hash_function(ena_dev, func, key,
  619. ENA_HASH_KEY_SIZE,
  620. 0xFFFFFFFF);
  621. if (unlikely(rc)) {
  622. netif_err(adapter, drv, netdev, "Cannot fill key\n");
  623. return rc == -EPERM ? -EOPNOTSUPP : rc;
  624. }
  625. }
  626. return 0;
  627. }
  628. static void ena_get_channels(struct net_device *netdev,
  629. struct ethtool_channels *channels)
  630. {
  631. struct ena_adapter *adapter = netdev_priv(netdev);
  632. channels->max_rx = adapter->num_queues;
  633. channels->max_tx = adapter->num_queues;
  634. channels->max_other = 0;
  635. channels->max_combined = 0;
  636. channels->rx_count = adapter->num_queues;
  637. channels->tx_count = adapter->num_queues;
  638. channels->other_count = 0;
  639. channels->combined_count = 0;
  640. }
  641. static int ena_get_tunable(struct net_device *netdev,
  642. const struct ethtool_tunable *tuna, void *data)
  643. {
  644. struct ena_adapter *adapter = netdev_priv(netdev);
  645. int ret = 0;
  646. switch (tuna->id) {
  647. case ETHTOOL_RX_COPYBREAK:
  648. *(u32 *)data = adapter->rx_copybreak;
  649. break;
  650. default:
  651. ret = -EINVAL;
  652. break;
  653. }
  654. return ret;
  655. }
  656. static int ena_set_tunable(struct net_device *netdev,
  657. const struct ethtool_tunable *tuna,
  658. const void *data)
  659. {
  660. struct ena_adapter *adapter = netdev_priv(netdev);
  661. int ret = 0;
  662. u32 len;
  663. switch (tuna->id) {
  664. case ETHTOOL_RX_COPYBREAK:
  665. len = *(u32 *)data;
  666. if (len > adapter->netdev->mtu) {
  667. ret = -EINVAL;
  668. break;
  669. }
  670. adapter->rx_copybreak = len;
  671. break;
  672. default:
  673. ret = -EINVAL;
  674. break;
  675. }
  676. return ret;
  677. }
  678. static const struct ethtool_ops ena_ethtool_ops = {
  679. .get_link_ksettings = ena_get_link_ksettings,
  680. .get_drvinfo = ena_get_drvinfo,
  681. .get_msglevel = ena_get_msglevel,
  682. .set_msglevel = ena_set_msglevel,
  683. .get_link = ethtool_op_get_link,
  684. .get_coalesce = ena_get_coalesce,
  685. .set_coalesce = ena_set_coalesce,
  686. .get_ringparam = ena_get_ringparam,
  687. .set_ringparam = ena_set_ringparam,
  688. .get_sset_count = ena_get_sset_count,
  689. .get_strings = ena_get_strings,
  690. .get_ethtool_stats = ena_get_ethtool_stats,
  691. .get_rxnfc = ena_get_rxnfc,
  692. .set_rxnfc = ena_set_rxnfc,
  693. .get_rxfh_indir_size = ena_get_rxfh_indir_size,
  694. .get_rxfh_key_size = ena_get_rxfh_key_size,
  695. .get_rxfh = ena_get_rxfh,
  696. .set_rxfh = ena_set_rxfh,
  697. .get_channels = ena_get_channels,
  698. .get_tunable = ena_get_tunable,
  699. .set_tunable = ena_set_tunable,
  700. .get_ts_info = ethtool_op_get_ts_info,
  701. };
  702. void ena_set_ethtool_ops(struct net_device *netdev)
  703. {
  704. netdev->ethtool_ops = &ena_ethtool_ops;
  705. }
  706. static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
  707. {
  708. struct net_device *netdev = adapter->netdev;
  709. u8 *strings_buf;
  710. u64 *data_buf;
  711. int strings_num;
  712. int i, rc;
  713. strings_num = ena_get_sset_count(netdev, ETH_SS_STATS);
  714. if (strings_num <= 0) {
  715. netif_err(adapter, drv, netdev, "Can't get stats num\n");
  716. return;
  717. }
  718. strings_buf = devm_kcalloc(&adapter->pdev->dev,
  719. ETH_GSTRING_LEN, strings_num,
  720. GFP_ATOMIC);
  721. if (!strings_buf) {
  722. netif_err(adapter, drv, netdev,
  723. "failed to alloc strings_buf\n");
  724. return;
  725. }
  726. data_buf = devm_kcalloc(&adapter->pdev->dev,
  727. strings_num, sizeof(u64),
  728. GFP_ATOMIC);
  729. if (!data_buf) {
  730. netif_err(adapter, drv, netdev,
  731. "failed to allocate data buf\n");
  732. devm_kfree(&adapter->pdev->dev, strings_buf);
  733. return;
  734. }
  735. ena_get_strings(netdev, ETH_SS_STATS, strings_buf);
  736. ena_get_ethtool_stats(netdev, NULL, data_buf);
  737. /* If there is a buffer, dump stats, otherwise print them to dmesg */
  738. if (buf)
  739. for (i = 0; i < strings_num; i++) {
  740. rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
  741. "%s %llu\n",
  742. strings_buf + i * ETH_GSTRING_LEN,
  743. data_buf[i]);
  744. buf += rc;
  745. }
  746. else
  747. for (i = 0; i < strings_num; i++)
  748. netif_err(adapter, drv, netdev, "%s: %llu\n",
  749. strings_buf + i * ETH_GSTRING_LEN,
  750. data_buf[i]);
  751. devm_kfree(&adapter->pdev->dev, strings_buf);
  752. devm_kfree(&adapter->pdev->dev, data_buf);
  753. }
  754. void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
  755. {
  756. if (!buf)
  757. return;
  758. ena_dump_stats_ex(adapter, buf);
  759. }
  760. void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
  761. {
  762. ena_dump_stats_ex(adapter, NULL);
  763. }