enic_ethtool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /**
  2. * Copyright 2013 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. *
  17. */
  18. #include <linux/netdevice.h>
  19. #include <linux/ethtool.h>
  20. #include "enic_res.h"
  21. #include "enic.h"
  22. #include "enic_dev.h"
  23. #include "enic_clsf.h"
  24. #include "vnic_rss.h"
  25. #include "vnic_stats.h"
  26. struct enic_stat {
  27. char name[ETH_GSTRING_LEN];
  28. unsigned int index;
  29. };
  30. #define ENIC_TX_STAT(stat) { \
  31. .name = #stat, \
  32. .index = offsetof(struct vnic_tx_stats, stat) / sizeof(u64) \
  33. }
  34. #define ENIC_RX_STAT(stat) { \
  35. .name = #stat, \
  36. .index = offsetof(struct vnic_rx_stats, stat) / sizeof(u64) \
  37. }
  38. #define ENIC_GEN_STAT(stat) { \
  39. .name = #stat, \
  40. .index = offsetof(struct vnic_gen_stats, stat) / sizeof(u64)\
  41. }
  42. static const struct enic_stat enic_tx_stats[] = {
  43. ENIC_TX_STAT(tx_frames_ok),
  44. ENIC_TX_STAT(tx_unicast_frames_ok),
  45. ENIC_TX_STAT(tx_multicast_frames_ok),
  46. ENIC_TX_STAT(tx_broadcast_frames_ok),
  47. ENIC_TX_STAT(tx_bytes_ok),
  48. ENIC_TX_STAT(tx_unicast_bytes_ok),
  49. ENIC_TX_STAT(tx_multicast_bytes_ok),
  50. ENIC_TX_STAT(tx_broadcast_bytes_ok),
  51. ENIC_TX_STAT(tx_drops),
  52. ENIC_TX_STAT(tx_errors),
  53. ENIC_TX_STAT(tx_tso),
  54. };
  55. static const struct enic_stat enic_rx_stats[] = {
  56. ENIC_RX_STAT(rx_frames_ok),
  57. ENIC_RX_STAT(rx_frames_total),
  58. ENIC_RX_STAT(rx_unicast_frames_ok),
  59. ENIC_RX_STAT(rx_multicast_frames_ok),
  60. ENIC_RX_STAT(rx_broadcast_frames_ok),
  61. ENIC_RX_STAT(rx_bytes_ok),
  62. ENIC_RX_STAT(rx_unicast_bytes_ok),
  63. ENIC_RX_STAT(rx_multicast_bytes_ok),
  64. ENIC_RX_STAT(rx_broadcast_bytes_ok),
  65. ENIC_RX_STAT(rx_drop),
  66. ENIC_RX_STAT(rx_no_bufs),
  67. ENIC_RX_STAT(rx_errors),
  68. ENIC_RX_STAT(rx_rss),
  69. ENIC_RX_STAT(rx_crc_errors),
  70. ENIC_RX_STAT(rx_frames_64),
  71. ENIC_RX_STAT(rx_frames_127),
  72. ENIC_RX_STAT(rx_frames_255),
  73. ENIC_RX_STAT(rx_frames_511),
  74. ENIC_RX_STAT(rx_frames_1023),
  75. ENIC_RX_STAT(rx_frames_1518),
  76. ENIC_RX_STAT(rx_frames_to_max),
  77. };
  78. static const struct enic_stat enic_gen_stats[] = {
  79. ENIC_GEN_STAT(dma_map_error),
  80. };
  81. static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
  82. static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
  83. static const unsigned int enic_n_gen_stats = ARRAY_SIZE(enic_gen_stats);
  84. static void enic_intr_coal_set_rx(struct enic *enic, u32 timer)
  85. {
  86. int i;
  87. int intr;
  88. for (i = 0; i < enic->rq_count; i++) {
  89. intr = enic_msix_rq_intr(enic, i);
  90. vnic_intr_coalescing_timer_set(&enic->intr[intr], timer);
  91. }
  92. }
  93. static int enic_get_ksettings(struct net_device *netdev,
  94. struct ethtool_link_ksettings *ecmd)
  95. {
  96. struct enic *enic = netdev_priv(netdev);
  97. struct ethtool_link_settings *base = &ecmd->base;
  98. ethtool_link_ksettings_add_link_mode(ecmd, supported,
  99. 10000baseT_Full);
  100. ethtool_link_ksettings_add_link_mode(ecmd, supported, FIBRE);
  101. ethtool_link_ksettings_add_link_mode(ecmd, advertising,
  102. 10000baseT_Full);
  103. ethtool_link_ksettings_add_link_mode(ecmd, advertising, FIBRE);
  104. base->port = PORT_FIBRE;
  105. if (netif_carrier_ok(netdev)) {
  106. base->speed = vnic_dev_port_speed(enic->vdev);
  107. base->duplex = DUPLEX_FULL;
  108. } else {
  109. base->speed = SPEED_UNKNOWN;
  110. base->duplex = DUPLEX_UNKNOWN;
  111. }
  112. base->autoneg = AUTONEG_DISABLE;
  113. return 0;
  114. }
  115. static void enic_get_drvinfo(struct net_device *netdev,
  116. struct ethtool_drvinfo *drvinfo)
  117. {
  118. struct enic *enic = netdev_priv(netdev);
  119. struct vnic_devcmd_fw_info *fw_info;
  120. int err;
  121. err = enic_dev_fw_info(enic, &fw_info);
  122. /* return only when pci_zalloc_consistent fails in vnic_dev_fw_info
  123. * For other failures, like devcmd failure, we return previously
  124. * recorded info.
  125. */
  126. if (err == -ENOMEM)
  127. return;
  128. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  129. strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
  130. strlcpy(drvinfo->fw_version, fw_info->fw_version,
  131. sizeof(drvinfo->fw_version));
  132. strlcpy(drvinfo->bus_info, pci_name(enic->pdev),
  133. sizeof(drvinfo->bus_info));
  134. }
  135. static void enic_get_strings(struct net_device *netdev, u32 stringset,
  136. u8 *data)
  137. {
  138. unsigned int i;
  139. switch (stringset) {
  140. case ETH_SS_STATS:
  141. for (i = 0; i < enic_n_tx_stats; i++) {
  142. memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
  143. data += ETH_GSTRING_LEN;
  144. }
  145. for (i = 0; i < enic_n_rx_stats; i++) {
  146. memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
  147. data += ETH_GSTRING_LEN;
  148. }
  149. for (i = 0; i < enic_n_gen_stats; i++) {
  150. memcpy(data, enic_gen_stats[i].name, ETH_GSTRING_LEN);
  151. data += ETH_GSTRING_LEN;
  152. }
  153. break;
  154. }
  155. }
  156. static int enic_get_sset_count(struct net_device *netdev, int sset)
  157. {
  158. switch (sset) {
  159. case ETH_SS_STATS:
  160. return enic_n_tx_stats + enic_n_rx_stats + enic_n_gen_stats;
  161. default:
  162. return -EOPNOTSUPP;
  163. }
  164. }
  165. static void enic_get_ethtool_stats(struct net_device *netdev,
  166. struct ethtool_stats *stats, u64 *data)
  167. {
  168. struct enic *enic = netdev_priv(netdev);
  169. struct vnic_stats *vstats;
  170. unsigned int i;
  171. int err;
  172. err = enic_dev_stats_dump(enic, &vstats);
  173. /* return only when pci_zalloc_consistent fails in vnic_dev_stats_dump
  174. * For other failures, like devcmd failure, we return previously
  175. * recorded stats.
  176. */
  177. if (err == -ENOMEM)
  178. return;
  179. for (i = 0; i < enic_n_tx_stats; i++)
  180. *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].index];
  181. for (i = 0; i < enic_n_rx_stats; i++)
  182. *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].index];
  183. for (i = 0; i < enic_n_gen_stats; i++)
  184. *(data++) = ((u64 *)&enic->gen_stats)[enic_gen_stats[i].index];
  185. }
  186. static u32 enic_get_msglevel(struct net_device *netdev)
  187. {
  188. struct enic *enic = netdev_priv(netdev);
  189. return enic->msg_enable;
  190. }
  191. static void enic_set_msglevel(struct net_device *netdev, u32 value)
  192. {
  193. struct enic *enic = netdev_priv(netdev);
  194. enic->msg_enable = value;
  195. }
  196. static int enic_get_coalesce(struct net_device *netdev,
  197. struct ethtool_coalesce *ecmd)
  198. {
  199. struct enic *enic = netdev_priv(netdev);
  200. struct enic_rx_coal *rxcoal = &enic->rx_coalesce_setting;
  201. if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
  202. ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
  203. ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
  204. if (rxcoal->use_adaptive_rx_coalesce)
  205. ecmd->use_adaptive_rx_coalesce = 1;
  206. ecmd->rx_coalesce_usecs_low = rxcoal->small_pkt_range_start;
  207. ecmd->rx_coalesce_usecs_high = rxcoal->range_end;
  208. return 0;
  209. }
  210. static int enic_coalesce_valid(struct enic *enic,
  211. struct ethtool_coalesce *ec)
  212. {
  213. u32 coalesce_usecs_max = vnic_dev_get_intr_coal_timer_max(enic->vdev);
  214. u32 rx_coalesce_usecs_high = min_t(u32, coalesce_usecs_max,
  215. ec->rx_coalesce_usecs_high);
  216. u32 rx_coalesce_usecs_low = min_t(u32, coalesce_usecs_max,
  217. ec->rx_coalesce_usecs_low);
  218. if (ec->rx_max_coalesced_frames ||
  219. ec->rx_coalesce_usecs_irq ||
  220. ec->rx_max_coalesced_frames_irq ||
  221. ec->tx_max_coalesced_frames ||
  222. ec->tx_coalesce_usecs_irq ||
  223. ec->tx_max_coalesced_frames_irq ||
  224. ec->stats_block_coalesce_usecs ||
  225. ec->use_adaptive_tx_coalesce ||
  226. ec->pkt_rate_low ||
  227. ec->rx_max_coalesced_frames_low ||
  228. ec->tx_coalesce_usecs_low ||
  229. ec->tx_max_coalesced_frames_low ||
  230. ec->pkt_rate_high ||
  231. ec->rx_max_coalesced_frames_high ||
  232. ec->tx_coalesce_usecs_high ||
  233. ec->tx_max_coalesced_frames_high ||
  234. ec->rate_sample_interval)
  235. return -EINVAL;
  236. if ((vnic_dev_get_intr_mode(enic->vdev) != VNIC_DEV_INTR_MODE_MSIX) &&
  237. ec->tx_coalesce_usecs)
  238. return -EINVAL;
  239. if ((ec->tx_coalesce_usecs > coalesce_usecs_max) ||
  240. (ec->rx_coalesce_usecs > coalesce_usecs_max) ||
  241. (ec->rx_coalesce_usecs_low > coalesce_usecs_max) ||
  242. (ec->rx_coalesce_usecs_high > coalesce_usecs_max))
  243. netdev_info(enic->netdev, "ethtool_set_coalesce: adaptor supports max coalesce value of %d. Setting max value.\n",
  244. coalesce_usecs_max);
  245. if (ec->rx_coalesce_usecs_high &&
  246. (rx_coalesce_usecs_high <
  247. rx_coalesce_usecs_low + ENIC_AIC_LARGE_PKT_DIFF))
  248. return -EINVAL;
  249. return 0;
  250. }
  251. static int enic_set_coalesce(struct net_device *netdev,
  252. struct ethtool_coalesce *ecmd)
  253. {
  254. struct enic *enic = netdev_priv(netdev);
  255. u32 tx_coalesce_usecs;
  256. u32 rx_coalesce_usecs;
  257. u32 rx_coalesce_usecs_low;
  258. u32 rx_coalesce_usecs_high;
  259. u32 coalesce_usecs_max;
  260. unsigned int i, intr;
  261. int ret;
  262. struct enic_rx_coal *rxcoal = &enic->rx_coalesce_setting;
  263. ret = enic_coalesce_valid(enic, ecmd);
  264. if (ret)
  265. return ret;
  266. coalesce_usecs_max = vnic_dev_get_intr_coal_timer_max(enic->vdev);
  267. tx_coalesce_usecs = min_t(u32, ecmd->tx_coalesce_usecs,
  268. coalesce_usecs_max);
  269. rx_coalesce_usecs = min_t(u32, ecmd->rx_coalesce_usecs,
  270. coalesce_usecs_max);
  271. rx_coalesce_usecs_low = min_t(u32, ecmd->rx_coalesce_usecs_low,
  272. coalesce_usecs_max);
  273. rx_coalesce_usecs_high = min_t(u32, ecmd->rx_coalesce_usecs_high,
  274. coalesce_usecs_max);
  275. if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX) {
  276. for (i = 0; i < enic->wq_count; i++) {
  277. intr = enic_msix_wq_intr(enic, i);
  278. vnic_intr_coalescing_timer_set(&enic->intr[intr],
  279. tx_coalesce_usecs);
  280. }
  281. enic->tx_coalesce_usecs = tx_coalesce_usecs;
  282. }
  283. rxcoal->use_adaptive_rx_coalesce = !!ecmd->use_adaptive_rx_coalesce;
  284. if (!rxcoal->use_adaptive_rx_coalesce)
  285. enic_intr_coal_set_rx(enic, rx_coalesce_usecs);
  286. if (ecmd->rx_coalesce_usecs_high) {
  287. rxcoal->range_end = rx_coalesce_usecs_high;
  288. rxcoal->small_pkt_range_start = rx_coalesce_usecs_low;
  289. rxcoal->large_pkt_range_start = rx_coalesce_usecs_low +
  290. ENIC_AIC_LARGE_PKT_DIFF;
  291. }
  292. enic->rx_coalesce_usecs = rx_coalesce_usecs;
  293. return 0;
  294. }
  295. static int enic_grxclsrlall(struct enic *enic, struct ethtool_rxnfc *cmd,
  296. u32 *rule_locs)
  297. {
  298. int j, ret = 0, cnt = 0;
  299. cmd->data = enic->rfs_h.max - enic->rfs_h.free;
  300. for (j = 0; j < (1 << ENIC_RFS_FLW_BITSHIFT); j++) {
  301. struct hlist_head *hhead;
  302. struct hlist_node *tmp;
  303. struct enic_rfs_fltr_node *n;
  304. hhead = &enic->rfs_h.ht_head[j];
  305. hlist_for_each_entry_safe(n, tmp, hhead, node) {
  306. if (cnt == cmd->rule_cnt)
  307. return -EMSGSIZE;
  308. rule_locs[cnt] = n->fltr_id;
  309. cnt++;
  310. }
  311. }
  312. cmd->rule_cnt = cnt;
  313. return ret;
  314. }
  315. static int enic_grxclsrule(struct enic *enic, struct ethtool_rxnfc *cmd)
  316. {
  317. struct ethtool_rx_flow_spec *fsp =
  318. (struct ethtool_rx_flow_spec *)&cmd->fs;
  319. struct enic_rfs_fltr_node *n;
  320. n = htbl_fltr_search(enic, (u16)fsp->location);
  321. if (!n)
  322. return -EINVAL;
  323. switch (n->keys.basic.ip_proto) {
  324. case IPPROTO_TCP:
  325. fsp->flow_type = TCP_V4_FLOW;
  326. break;
  327. case IPPROTO_UDP:
  328. fsp->flow_type = UDP_V4_FLOW;
  329. break;
  330. default:
  331. return -EINVAL;
  332. break;
  333. }
  334. fsp->h_u.tcp_ip4_spec.ip4src = flow_get_u32_src(&n->keys);
  335. fsp->m_u.tcp_ip4_spec.ip4src = (__u32)~0;
  336. fsp->h_u.tcp_ip4_spec.ip4dst = flow_get_u32_dst(&n->keys);
  337. fsp->m_u.tcp_ip4_spec.ip4dst = (__u32)~0;
  338. fsp->h_u.tcp_ip4_spec.psrc = n->keys.ports.src;
  339. fsp->m_u.tcp_ip4_spec.psrc = (__u16)~0;
  340. fsp->h_u.tcp_ip4_spec.pdst = n->keys.ports.dst;
  341. fsp->m_u.tcp_ip4_spec.pdst = (__u16)~0;
  342. fsp->ring_cookie = n->rq_id;
  343. return 0;
  344. }
  345. static int enic_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
  346. u32 *rule_locs)
  347. {
  348. struct enic *enic = netdev_priv(dev);
  349. int ret = 0;
  350. switch (cmd->cmd) {
  351. case ETHTOOL_GRXRINGS:
  352. cmd->data = enic->rq_count;
  353. break;
  354. case ETHTOOL_GRXCLSRLCNT:
  355. spin_lock_bh(&enic->rfs_h.lock);
  356. cmd->rule_cnt = enic->rfs_h.max - enic->rfs_h.free;
  357. cmd->data = enic->rfs_h.max;
  358. spin_unlock_bh(&enic->rfs_h.lock);
  359. break;
  360. case ETHTOOL_GRXCLSRLALL:
  361. spin_lock_bh(&enic->rfs_h.lock);
  362. ret = enic_grxclsrlall(enic, cmd, rule_locs);
  363. spin_unlock_bh(&enic->rfs_h.lock);
  364. break;
  365. case ETHTOOL_GRXCLSRULE:
  366. spin_lock_bh(&enic->rfs_h.lock);
  367. ret = enic_grxclsrule(enic, cmd);
  368. spin_unlock_bh(&enic->rfs_h.lock);
  369. break;
  370. default:
  371. ret = -EOPNOTSUPP;
  372. break;
  373. }
  374. return ret;
  375. }
  376. static int enic_get_tunable(struct net_device *dev,
  377. const struct ethtool_tunable *tuna, void *data)
  378. {
  379. struct enic *enic = netdev_priv(dev);
  380. int ret = 0;
  381. switch (tuna->id) {
  382. case ETHTOOL_RX_COPYBREAK:
  383. *(u32 *)data = enic->rx_copybreak;
  384. break;
  385. default:
  386. ret = -EINVAL;
  387. break;
  388. }
  389. return ret;
  390. }
  391. static int enic_set_tunable(struct net_device *dev,
  392. const struct ethtool_tunable *tuna,
  393. const void *data)
  394. {
  395. struct enic *enic = netdev_priv(dev);
  396. int ret = 0;
  397. switch (tuna->id) {
  398. case ETHTOOL_RX_COPYBREAK:
  399. enic->rx_copybreak = *(u32 *)data;
  400. break;
  401. default:
  402. ret = -EINVAL;
  403. break;
  404. }
  405. return ret;
  406. }
  407. static u32 enic_get_rxfh_key_size(struct net_device *netdev)
  408. {
  409. return ENIC_RSS_LEN;
  410. }
  411. static int enic_get_rxfh(struct net_device *netdev, u32 *indir, u8 *hkey,
  412. u8 *hfunc)
  413. {
  414. struct enic *enic = netdev_priv(netdev);
  415. if (hkey)
  416. memcpy(hkey, enic->rss_key, ENIC_RSS_LEN);
  417. if (hfunc)
  418. *hfunc = ETH_RSS_HASH_TOP;
  419. return 0;
  420. }
  421. static int enic_set_rxfh(struct net_device *netdev, const u32 *indir,
  422. const u8 *hkey, const u8 hfunc)
  423. {
  424. struct enic *enic = netdev_priv(netdev);
  425. if ((hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) ||
  426. indir)
  427. return -EINVAL;
  428. if (hkey)
  429. memcpy(enic->rss_key, hkey, ENIC_RSS_LEN);
  430. return __enic_set_rsskey(enic);
  431. }
  432. static const struct ethtool_ops enic_ethtool_ops = {
  433. .get_drvinfo = enic_get_drvinfo,
  434. .get_msglevel = enic_get_msglevel,
  435. .set_msglevel = enic_set_msglevel,
  436. .get_link = ethtool_op_get_link,
  437. .get_strings = enic_get_strings,
  438. .get_sset_count = enic_get_sset_count,
  439. .get_ethtool_stats = enic_get_ethtool_stats,
  440. .get_coalesce = enic_get_coalesce,
  441. .set_coalesce = enic_set_coalesce,
  442. .get_rxnfc = enic_get_rxnfc,
  443. .get_tunable = enic_get_tunable,
  444. .set_tunable = enic_set_tunable,
  445. .get_rxfh_key_size = enic_get_rxfh_key_size,
  446. .get_rxfh = enic_get_rxfh,
  447. .set_rxfh = enic_set_rxfh,
  448. .get_link_ksettings = enic_get_ksettings,
  449. };
  450. void enic_set_ethtool_ops(struct net_device *netdev)
  451. {
  452. netdev->ethtool_ops = &enic_ethtool_ops;
  453. }