enic_ethtool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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_settings(struct net_device *netdev,
  94. struct ethtool_cmd *ecmd)
  95. {
  96. struct enic *enic = netdev_priv(netdev);
  97. ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
  98. ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
  99. ecmd->port = PORT_FIBRE;
  100. ecmd->transceiver = XCVR_EXTERNAL;
  101. if (netif_carrier_ok(netdev)) {
  102. ethtool_cmd_speed_set(ecmd, vnic_dev_port_speed(enic->vdev));
  103. ecmd->duplex = DUPLEX_FULL;
  104. } else {
  105. ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
  106. ecmd->duplex = DUPLEX_UNKNOWN;
  107. }
  108. ecmd->autoneg = AUTONEG_DISABLE;
  109. return 0;
  110. }
  111. static void enic_get_drvinfo(struct net_device *netdev,
  112. struct ethtool_drvinfo *drvinfo)
  113. {
  114. struct enic *enic = netdev_priv(netdev);
  115. struct vnic_devcmd_fw_info *fw_info;
  116. int err;
  117. err = enic_dev_fw_info(enic, &fw_info);
  118. /* return only when pci_zalloc_consistent fails in vnic_dev_fw_info
  119. * For other failures, like devcmd failure, we return previously
  120. * recorded info.
  121. */
  122. if (err == -ENOMEM)
  123. return;
  124. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  125. strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
  126. strlcpy(drvinfo->fw_version, fw_info->fw_version,
  127. sizeof(drvinfo->fw_version));
  128. strlcpy(drvinfo->bus_info, pci_name(enic->pdev),
  129. sizeof(drvinfo->bus_info));
  130. }
  131. static void enic_get_strings(struct net_device *netdev, u32 stringset,
  132. u8 *data)
  133. {
  134. unsigned int i;
  135. switch (stringset) {
  136. case ETH_SS_STATS:
  137. for (i = 0; i < enic_n_tx_stats; i++) {
  138. memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
  139. data += ETH_GSTRING_LEN;
  140. }
  141. for (i = 0; i < enic_n_rx_stats; i++) {
  142. memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
  143. data += ETH_GSTRING_LEN;
  144. }
  145. for (i = 0; i < enic_n_gen_stats; i++) {
  146. memcpy(data, enic_gen_stats[i].name, ETH_GSTRING_LEN);
  147. data += ETH_GSTRING_LEN;
  148. }
  149. break;
  150. }
  151. }
  152. static int enic_get_sset_count(struct net_device *netdev, int sset)
  153. {
  154. switch (sset) {
  155. case ETH_SS_STATS:
  156. return enic_n_tx_stats + enic_n_rx_stats + enic_n_gen_stats;
  157. default:
  158. return -EOPNOTSUPP;
  159. }
  160. }
  161. static void enic_get_ethtool_stats(struct net_device *netdev,
  162. struct ethtool_stats *stats, u64 *data)
  163. {
  164. struct enic *enic = netdev_priv(netdev);
  165. struct vnic_stats *vstats;
  166. unsigned int i;
  167. int err;
  168. err = enic_dev_stats_dump(enic, &vstats);
  169. /* return only when pci_zalloc_consistent fails in vnic_dev_stats_dump
  170. * For other failures, like devcmd failure, we return previously
  171. * recorded stats.
  172. */
  173. if (err == -ENOMEM)
  174. return;
  175. for (i = 0; i < enic_n_tx_stats; i++)
  176. *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].index];
  177. for (i = 0; i < enic_n_rx_stats; i++)
  178. *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].index];
  179. for (i = 0; i < enic_n_gen_stats; i++)
  180. *(data++) = ((u64 *)&enic->gen_stats)[enic_gen_stats[i].index];
  181. }
  182. static u32 enic_get_msglevel(struct net_device *netdev)
  183. {
  184. struct enic *enic = netdev_priv(netdev);
  185. return enic->msg_enable;
  186. }
  187. static void enic_set_msglevel(struct net_device *netdev, u32 value)
  188. {
  189. struct enic *enic = netdev_priv(netdev);
  190. enic->msg_enable = value;
  191. }
  192. static int enic_get_coalesce(struct net_device *netdev,
  193. struct ethtool_coalesce *ecmd)
  194. {
  195. struct enic *enic = netdev_priv(netdev);
  196. struct enic_rx_coal *rxcoal = &enic->rx_coalesce_setting;
  197. if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
  198. ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
  199. ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
  200. if (rxcoal->use_adaptive_rx_coalesce)
  201. ecmd->use_adaptive_rx_coalesce = 1;
  202. ecmd->rx_coalesce_usecs_low = rxcoal->small_pkt_range_start;
  203. ecmd->rx_coalesce_usecs_high = rxcoal->range_end;
  204. return 0;
  205. }
  206. static int enic_coalesce_valid(struct enic *enic,
  207. struct ethtool_coalesce *ec)
  208. {
  209. u32 coalesce_usecs_max = vnic_dev_get_intr_coal_timer_max(enic->vdev);
  210. u32 rx_coalesce_usecs_high = min_t(u32, coalesce_usecs_max,
  211. ec->rx_coalesce_usecs_high);
  212. u32 rx_coalesce_usecs_low = min_t(u32, coalesce_usecs_max,
  213. ec->rx_coalesce_usecs_low);
  214. if (ec->rx_max_coalesced_frames ||
  215. ec->rx_coalesce_usecs_irq ||
  216. ec->rx_max_coalesced_frames_irq ||
  217. ec->tx_max_coalesced_frames ||
  218. ec->tx_coalesce_usecs_irq ||
  219. ec->tx_max_coalesced_frames_irq ||
  220. ec->stats_block_coalesce_usecs ||
  221. ec->use_adaptive_tx_coalesce ||
  222. ec->pkt_rate_low ||
  223. ec->rx_max_coalesced_frames_low ||
  224. ec->tx_coalesce_usecs_low ||
  225. ec->tx_max_coalesced_frames_low ||
  226. ec->pkt_rate_high ||
  227. ec->rx_max_coalesced_frames_high ||
  228. ec->tx_coalesce_usecs_high ||
  229. ec->tx_max_coalesced_frames_high ||
  230. ec->rate_sample_interval)
  231. return -EINVAL;
  232. if ((vnic_dev_get_intr_mode(enic->vdev) != VNIC_DEV_INTR_MODE_MSIX) &&
  233. ec->tx_coalesce_usecs)
  234. return -EINVAL;
  235. if ((ec->tx_coalesce_usecs > coalesce_usecs_max) ||
  236. (ec->rx_coalesce_usecs > coalesce_usecs_max) ||
  237. (ec->rx_coalesce_usecs_low > coalesce_usecs_max) ||
  238. (ec->rx_coalesce_usecs_high > coalesce_usecs_max))
  239. netdev_info(enic->netdev, "ethtool_set_coalesce: adaptor supports max coalesce value of %d. Setting max value.\n",
  240. coalesce_usecs_max);
  241. if (ec->rx_coalesce_usecs_high &&
  242. (rx_coalesce_usecs_high <
  243. rx_coalesce_usecs_low + ENIC_AIC_LARGE_PKT_DIFF))
  244. return -EINVAL;
  245. return 0;
  246. }
  247. static int enic_set_coalesce(struct net_device *netdev,
  248. struct ethtool_coalesce *ecmd)
  249. {
  250. struct enic *enic = netdev_priv(netdev);
  251. u32 tx_coalesce_usecs;
  252. u32 rx_coalesce_usecs;
  253. u32 rx_coalesce_usecs_low;
  254. u32 rx_coalesce_usecs_high;
  255. u32 coalesce_usecs_max;
  256. unsigned int i, intr;
  257. int ret;
  258. struct enic_rx_coal *rxcoal = &enic->rx_coalesce_setting;
  259. ret = enic_coalesce_valid(enic, ecmd);
  260. if (ret)
  261. return ret;
  262. coalesce_usecs_max = vnic_dev_get_intr_coal_timer_max(enic->vdev);
  263. tx_coalesce_usecs = min_t(u32, ecmd->tx_coalesce_usecs,
  264. coalesce_usecs_max);
  265. rx_coalesce_usecs = min_t(u32, ecmd->rx_coalesce_usecs,
  266. coalesce_usecs_max);
  267. rx_coalesce_usecs_low = min_t(u32, ecmd->rx_coalesce_usecs_low,
  268. coalesce_usecs_max);
  269. rx_coalesce_usecs_high = min_t(u32, ecmd->rx_coalesce_usecs_high,
  270. coalesce_usecs_max);
  271. if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX) {
  272. for (i = 0; i < enic->wq_count; i++) {
  273. intr = enic_msix_wq_intr(enic, i);
  274. vnic_intr_coalescing_timer_set(&enic->intr[intr],
  275. tx_coalesce_usecs);
  276. }
  277. enic->tx_coalesce_usecs = tx_coalesce_usecs;
  278. }
  279. rxcoal->use_adaptive_rx_coalesce = !!ecmd->use_adaptive_rx_coalesce;
  280. if (!rxcoal->use_adaptive_rx_coalesce)
  281. enic_intr_coal_set_rx(enic, rx_coalesce_usecs);
  282. if (ecmd->rx_coalesce_usecs_high) {
  283. rxcoal->range_end = rx_coalesce_usecs_high;
  284. rxcoal->small_pkt_range_start = rx_coalesce_usecs_low;
  285. rxcoal->large_pkt_range_start = rx_coalesce_usecs_low +
  286. ENIC_AIC_LARGE_PKT_DIFF;
  287. }
  288. enic->rx_coalesce_usecs = rx_coalesce_usecs;
  289. return 0;
  290. }
  291. static int enic_grxclsrlall(struct enic *enic, struct ethtool_rxnfc *cmd,
  292. u32 *rule_locs)
  293. {
  294. int j, ret = 0, cnt = 0;
  295. cmd->data = enic->rfs_h.max - enic->rfs_h.free;
  296. for (j = 0; j < (1 << ENIC_RFS_FLW_BITSHIFT); j++) {
  297. struct hlist_head *hhead;
  298. struct hlist_node *tmp;
  299. struct enic_rfs_fltr_node *n;
  300. hhead = &enic->rfs_h.ht_head[j];
  301. hlist_for_each_entry_safe(n, tmp, hhead, node) {
  302. if (cnt == cmd->rule_cnt)
  303. return -EMSGSIZE;
  304. rule_locs[cnt] = n->fltr_id;
  305. cnt++;
  306. }
  307. }
  308. cmd->rule_cnt = cnt;
  309. return ret;
  310. }
  311. static int enic_grxclsrule(struct enic *enic, struct ethtool_rxnfc *cmd)
  312. {
  313. struct ethtool_rx_flow_spec *fsp =
  314. (struct ethtool_rx_flow_spec *)&cmd->fs;
  315. struct enic_rfs_fltr_node *n;
  316. n = htbl_fltr_search(enic, (u16)fsp->location);
  317. if (!n)
  318. return -EINVAL;
  319. switch (n->keys.basic.ip_proto) {
  320. case IPPROTO_TCP:
  321. fsp->flow_type = TCP_V4_FLOW;
  322. break;
  323. case IPPROTO_UDP:
  324. fsp->flow_type = UDP_V4_FLOW;
  325. break;
  326. default:
  327. return -EINVAL;
  328. break;
  329. }
  330. fsp->h_u.tcp_ip4_spec.ip4src = flow_get_u32_src(&n->keys);
  331. fsp->m_u.tcp_ip4_spec.ip4src = (__u32)~0;
  332. fsp->h_u.tcp_ip4_spec.ip4dst = flow_get_u32_dst(&n->keys);
  333. fsp->m_u.tcp_ip4_spec.ip4dst = (__u32)~0;
  334. fsp->h_u.tcp_ip4_spec.psrc = n->keys.ports.src;
  335. fsp->m_u.tcp_ip4_spec.psrc = (__u16)~0;
  336. fsp->h_u.tcp_ip4_spec.pdst = n->keys.ports.dst;
  337. fsp->m_u.tcp_ip4_spec.pdst = (__u16)~0;
  338. fsp->ring_cookie = n->rq_id;
  339. return 0;
  340. }
  341. static int enic_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
  342. u32 *rule_locs)
  343. {
  344. struct enic *enic = netdev_priv(dev);
  345. int ret = 0;
  346. switch (cmd->cmd) {
  347. case ETHTOOL_GRXRINGS:
  348. cmd->data = enic->rq_count;
  349. break;
  350. case ETHTOOL_GRXCLSRLCNT:
  351. spin_lock_bh(&enic->rfs_h.lock);
  352. cmd->rule_cnt = enic->rfs_h.max - enic->rfs_h.free;
  353. cmd->data = enic->rfs_h.max;
  354. spin_unlock_bh(&enic->rfs_h.lock);
  355. break;
  356. case ETHTOOL_GRXCLSRLALL:
  357. spin_lock_bh(&enic->rfs_h.lock);
  358. ret = enic_grxclsrlall(enic, cmd, rule_locs);
  359. spin_unlock_bh(&enic->rfs_h.lock);
  360. break;
  361. case ETHTOOL_GRXCLSRULE:
  362. spin_lock_bh(&enic->rfs_h.lock);
  363. ret = enic_grxclsrule(enic, cmd);
  364. spin_unlock_bh(&enic->rfs_h.lock);
  365. break;
  366. default:
  367. ret = -EOPNOTSUPP;
  368. break;
  369. }
  370. return ret;
  371. }
  372. static int enic_get_tunable(struct net_device *dev,
  373. const struct ethtool_tunable *tuna, void *data)
  374. {
  375. struct enic *enic = netdev_priv(dev);
  376. int ret = 0;
  377. switch (tuna->id) {
  378. case ETHTOOL_RX_COPYBREAK:
  379. *(u32 *)data = enic->rx_copybreak;
  380. break;
  381. default:
  382. ret = -EINVAL;
  383. break;
  384. }
  385. return ret;
  386. }
  387. static int enic_set_tunable(struct net_device *dev,
  388. const struct ethtool_tunable *tuna,
  389. const void *data)
  390. {
  391. struct enic *enic = netdev_priv(dev);
  392. int ret = 0;
  393. switch (tuna->id) {
  394. case ETHTOOL_RX_COPYBREAK:
  395. enic->rx_copybreak = *(u32 *)data;
  396. break;
  397. default:
  398. ret = -EINVAL;
  399. break;
  400. }
  401. return ret;
  402. }
  403. static u32 enic_get_rxfh_key_size(struct net_device *netdev)
  404. {
  405. return ENIC_RSS_LEN;
  406. }
  407. static int enic_get_rxfh(struct net_device *netdev, u32 *indir, u8 *hkey,
  408. u8 *hfunc)
  409. {
  410. struct enic *enic = netdev_priv(netdev);
  411. if (hkey)
  412. memcpy(hkey, enic->rss_key, ENIC_RSS_LEN);
  413. if (hfunc)
  414. *hfunc = ETH_RSS_HASH_TOP;
  415. return 0;
  416. }
  417. static int enic_set_rxfh(struct net_device *netdev, const u32 *indir,
  418. const u8 *hkey, const u8 hfunc)
  419. {
  420. struct enic *enic = netdev_priv(netdev);
  421. if ((hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) ||
  422. indir)
  423. return -EINVAL;
  424. if (hkey)
  425. memcpy(enic->rss_key, hkey, ENIC_RSS_LEN);
  426. return __enic_set_rsskey(enic);
  427. }
  428. static const struct ethtool_ops enic_ethtool_ops = {
  429. .get_settings = enic_get_settings,
  430. .get_drvinfo = enic_get_drvinfo,
  431. .get_msglevel = enic_get_msglevel,
  432. .set_msglevel = enic_set_msglevel,
  433. .get_link = ethtool_op_get_link,
  434. .get_strings = enic_get_strings,
  435. .get_sset_count = enic_get_sset_count,
  436. .get_ethtool_stats = enic_get_ethtool_stats,
  437. .get_coalesce = enic_get_coalesce,
  438. .set_coalesce = enic_set_coalesce,
  439. .get_rxnfc = enic_get_rxnfc,
  440. .get_tunable = enic_get_tunable,
  441. .set_tunable = enic_set_tunable,
  442. .get_rxfh_key_size = enic_get_rxfh_key_size,
  443. .get_rxfh = enic_get_rxfh,
  444. .set_rxfh = enic_set_rxfh,
  445. };
  446. void enic_set_ethtool_ops(struct net_device *netdev)
  447. {
  448. netdev->ethtool_ops = &enic_ethtool_ops;
  449. }