ef100_rep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2019 Solarflare Communications Inc.
  5. * Copyright 2020-2022 Xilinx Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation, incorporated herein by reference.
  10. */
  11. #include <linux/rhashtable.h>
  12. #include "ef100_rep.h"
  13. #include "ef100_netdev.h"
  14. #include "ef100_nic.h"
  15. #include "mae.h"
  16. #include "rx_common.h"
  17. #include "tc_bindings.h"
  18. #include "efx_devlink.h"
  19. #define EFX_EF100_REP_DRIVER "efx_ef100_rep"
  20. #define EFX_REP_DEFAULT_PSEUDO_RING_SIZE 64
  21. static int efx_ef100_rep_poll(struct napi_struct *napi, int weight);
  22. static int efx_ef100_rep_init_struct(struct efx_nic *efx, struct efx_rep *efv,
  23. unsigned int i)
  24. {
  25. efv->parent = efx;
  26. efv->idx = i;
  27. INIT_LIST_HEAD(&efv->list);
  28. efv->dflt.fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL;
  29. INIT_LIST_HEAD(&efv->dflt.acts.list);
  30. INIT_LIST_HEAD(&efv->rx_list);
  31. spin_lock_init(&efv->rx_lock);
  32. efv->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  33. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  34. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  35. NETIF_MSG_TX_ERR | NETIF_MSG_HW;
  36. return 0;
  37. }
  38. static int efx_ef100_rep_open(struct net_device *net_dev)
  39. {
  40. struct efx_rep *efv = netdev_priv(net_dev);
  41. netif_napi_add(net_dev, &efv->napi, efx_ef100_rep_poll);
  42. napi_enable(&efv->napi);
  43. return 0;
  44. }
  45. static int efx_ef100_rep_close(struct net_device *net_dev)
  46. {
  47. struct efx_rep *efv = netdev_priv(net_dev);
  48. napi_disable(&efv->napi);
  49. netif_napi_del(&efv->napi);
  50. return 0;
  51. }
  52. static netdev_tx_t efx_ef100_rep_xmit(struct sk_buff *skb,
  53. struct net_device *dev)
  54. {
  55. struct efx_rep *efv = netdev_priv(dev);
  56. struct efx_nic *efx = efv->parent;
  57. netdev_tx_t rc;
  58. /* __ef100_hard_start_xmit() will always return success even in the
  59. * case of TX drops, where it will increment efx's tx_dropped. The
  60. * efv stats really only count attempted TX, not success/failure.
  61. */
  62. atomic64_inc(&efv->stats.tx_packets);
  63. atomic64_add(skb->len, &efv->stats.tx_bytes);
  64. netif_tx_lock(efx->net_dev);
  65. rc = __ef100_hard_start_xmit(skb, efx, dev, efv);
  66. netif_tx_unlock(efx->net_dev);
  67. return rc;
  68. }
  69. static int efx_ef100_rep_get_port_parent_id(struct net_device *dev,
  70. struct netdev_phys_item_id *ppid)
  71. {
  72. struct efx_rep *efv = netdev_priv(dev);
  73. struct efx_nic *efx = efv->parent;
  74. struct ef100_nic_data *nic_data;
  75. nic_data = efx->nic_data;
  76. /* nic_data->port_id is a u8[] */
  77. ppid->id_len = sizeof(nic_data->port_id);
  78. memcpy(ppid->id, nic_data->port_id, sizeof(nic_data->port_id));
  79. return 0;
  80. }
  81. static int efx_ef100_rep_get_phys_port_name(struct net_device *dev,
  82. char *buf, size_t len)
  83. {
  84. struct efx_rep *efv = netdev_priv(dev);
  85. struct efx_nic *efx = efv->parent;
  86. struct ef100_nic_data *nic_data;
  87. int ret;
  88. nic_data = efx->nic_data;
  89. ret = snprintf(buf, len, "p%upf%uvf%u", efx->port_num,
  90. nic_data->pf_index, efv->idx);
  91. if (ret >= len)
  92. return -EOPNOTSUPP;
  93. return 0;
  94. }
  95. static int efx_ef100_rep_setup_tc(struct net_device *net_dev,
  96. enum tc_setup_type type, void *type_data)
  97. {
  98. struct efx_rep *efv = netdev_priv(net_dev);
  99. struct efx_nic *efx = efv->parent;
  100. if (type == TC_SETUP_CLSFLOWER)
  101. return efx_tc_flower(efx, net_dev, type_data, efv);
  102. if (type == TC_SETUP_BLOCK)
  103. return efx_tc_setup_block(net_dev, efx, type_data, efv);
  104. return -EOPNOTSUPP;
  105. }
  106. static void efx_ef100_rep_get_stats64(struct net_device *dev,
  107. struct rtnl_link_stats64 *stats)
  108. {
  109. struct efx_rep *efv = netdev_priv(dev);
  110. stats->rx_packets = atomic64_read(&efv->stats.rx_packets);
  111. stats->tx_packets = atomic64_read(&efv->stats.tx_packets);
  112. stats->rx_bytes = atomic64_read(&efv->stats.rx_bytes);
  113. stats->tx_bytes = atomic64_read(&efv->stats.tx_bytes);
  114. stats->rx_dropped = atomic64_read(&efv->stats.rx_dropped);
  115. stats->tx_errors = atomic64_read(&efv->stats.tx_errors);
  116. }
  117. const struct net_device_ops efx_ef100_rep_netdev_ops = {
  118. .ndo_open = efx_ef100_rep_open,
  119. .ndo_stop = efx_ef100_rep_close,
  120. .ndo_start_xmit = efx_ef100_rep_xmit,
  121. .ndo_get_port_parent_id = efx_ef100_rep_get_port_parent_id,
  122. .ndo_get_phys_port_name = efx_ef100_rep_get_phys_port_name,
  123. .ndo_get_stats64 = efx_ef100_rep_get_stats64,
  124. .ndo_setup_tc = efx_ef100_rep_setup_tc,
  125. };
  126. static void efx_ef100_rep_get_drvinfo(struct net_device *dev,
  127. struct ethtool_drvinfo *drvinfo)
  128. {
  129. strscpy(drvinfo->driver, EFX_EF100_REP_DRIVER, sizeof(drvinfo->driver));
  130. }
  131. static u32 efx_ef100_rep_ethtool_get_msglevel(struct net_device *net_dev)
  132. {
  133. struct efx_rep *efv = netdev_priv(net_dev);
  134. return efv->msg_enable;
  135. }
  136. static void efx_ef100_rep_ethtool_set_msglevel(struct net_device *net_dev,
  137. u32 msg_enable)
  138. {
  139. struct efx_rep *efv = netdev_priv(net_dev);
  140. efv->msg_enable = msg_enable;
  141. }
  142. static void efx_ef100_rep_ethtool_get_ringparam(struct net_device *net_dev,
  143. struct ethtool_ringparam *ring,
  144. struct kernel_ethtool_ringparam *kring,
  145. struct netlink_ext_ack *ext_ack)
  146. {
  147. struct efx_rep *efv = netdev_priv(net_dev);
  148. ring->rx_max_pending = U32_MAX;
  149. ring->rx_pending = efv->rx_pring_size;
  150. }
  151. static int efx_ef100_rep_ethtool_set_ringparam(struct net_device *net_dev,
  152. struct ethtool_ringparam *ring,
  153. struct kernel_ethtool_ringparam *kring,
  154. struct netlink_ext_ack *ext_ack)
  155. {
  156. struct efx_rep *efv = netdev_priv(net_dev);
  157. if (ring->rx_mini_pending || ring->rx_jumbo_pending || ring->tx_pending)
  158. return -EINVAL;
  159. efv->rx_pring_size = ring->rx_pending;
  160. return 0;
  161. }
  162. static const struct ethtool_ops efx_ef100_rep_ethtool_ops = {
  163. .get_drvinfo = efx_ef100_rep_get_drvinfo,
  164. .get_msglevel = efx_ef100_rep_ethtool_get_msglevel,
  165. .set_msglevel = efx_ef100_rep_ethtool_set_msglevel,
  166. .get_ringparam = efx_ef100_rep_ethtool_get_ringparam,
  167. .set_ringparam = efx_ef100_rep_ethtool_set_ringparam,
  168. };
  169. static struct efx_rep *efx_ef100_rep_create_netdev(struct efx_nic *efx,
  170. unsigned int i)
  171. {
  172. struct net_device *net_dev;
  173. struct efx_rep *efv;
  174. int rc;
  175. net_dev = alloc_etherdev_mq(sizeof(*efv), 1);
  176. if (!net_dev)
  177. return ERR_PTR(-ENOMEM);
  178. efv = netdev_priv(net_dev);
  179. rc = efx_ef100_rep_init_struct(efx, efv, i);
  180. if (rc)
  181. goto fail1;
  182. efv->net_dev = net_dev;
  183. rtnl_lock();
  184. spin_lock_bh(&efx->vf_reps_lock);
  185. list_add_tail(&efv->list, &efx->vf_reps);
  186. spin_unlock_bh(&efx->vf_reps_lock);
  187. if (netif_running(efx->net_dev) && efx->state == STATE_NET_UP) {
  188. netif_device_attach(net_dev);
  189. netif_carrier_on(net_dev);
  190. } else {
  191. netif_carrier_off(net_dev);
  192. netif_tx_stop_all_queues(net_dev);
  193. }
  194. rtnl_unlock();
  195. net_dev->netdev_ops = &efx_ef100_rep_netdev_ops;
  196. net_dev->ethtool_ops = &efx_ef100_rep_ethtool_ops;
  197. net_dev->min_mtu = EFX_MIN_MTU;
  198. net_dev->max_mtu = EFX_MAX_MTU;
  199. net_dev->lltx = true;
  200. return efv;
  201. fail1:
  202. free_netdev(net_dev);
  203. return ERR_PTR(rc);
  204. }
  205. static int efx_ef100_configure_rep(struct efx_rep *efv)
  206. {
  207. struct efx_nic *efx = efv->parent;
  208. int rc;
  209. efv->rx_pring_size = EFX_REP_DEFAULT_PSEUDO_RING_SIZE;
  210. /* Look up actual mport ID */
  211. rc = efx_mae_lookup_mport(efx, efv->idx, &efv->mport);
  212. if (rc)
  213. return rc;
  214. pci_dbg(efx->pci_dev, "VF %u has mport ID %#x\n", efv->idx, efv->mport);
  215. /* mport label should fit in 16 bits */
  216. WARN_ON(efv->mport >> 16);
  217. return efx_tc_configure_default_rule_rep(efv);
  218. }
  219. static void efx_ef100_deconfigure_rep(struct efx_rep *efv)
  220. {
  221. struct efx_nic *efx = efv->parent;
  222. efx_tc_deconfigure_default_rule(efx, &efv->dflt);
  223. }
  224. static void efx_ef100_rep_destroy_netdev(struct efx_rep *efv)
  225. {
  226. struct efx_nic *efx = efv->parent;
  227. rtnl_lock();
  228. spin_lock_bh(&efx->vf_reps_lock);
  229. list_del(&efv->list);
  230. spin_unlock_bh(&efx->vf_reps_lock);
  231. rtnl_unlock();
  232. synchronize_rcu();
  233. free_netdev(efv->net_dev);
  234. }
  235. int efx_ef100_vfrep_create(struct efx_nic *efx, unsigned int i)
  236. {
  237. struct efx_rep *efv;
  238. int rc;
  239. efv = efx_ef100_rep_create_netdev(efx, i);
  240. if (IS_ERR(efv)) {
  241. rc = PTR_ERR(efv);
  242. pci_err(efx->pci_dev,
  243. "Failed to create representor for VF %d, rc %d\n", i,
  244. rc);
  245. return rc;
  246. }
  247. rc = efx_ef100_configure_rep(efv);
  248. if (rc) {
  249. pci_err(efx->pci_dev,
  250. "Failed to configure representor for VF %d, rc %d\n",
  251. i, rc);
  252. goto fail1;
  253. }
  254. ef100_rep_set_devlink_port(efv);
  255. rc = register_netdev(efv->net_dev);
  256. if (rc) {
  257. pci_err(efx->pci_dev,
  258. "Failed to register representor for VF %d, rc %d\n",
  259. i, rc);
  260. goto fail2;
  261. }
  262. pci_dbg(efx->pci_dev, "Representor for VF %d is %s\n", i,
  263. efv->net_dev->name);
  264. return 0;
  265. fail2:
  266. ef100_rep_unset_devlink_port(efv);
  267. efx_ef100_deconfigure_rep(efv);
  268. fail1:
  269. efx_ef100_rep_destroy_netdev(efv);
  270. return rc;
  271. }
  272. void efx_ef100_vfrep_destroy(struct efx_nic *efx, struct efx_rep *efv)
  273. {
  274. struct net_device *rep_dev;
  275. rep_dev = efv->net_dev;
  276. if (!rep_dev)
  277. return;
  278. netif_dbg(efx, drv, rep_dev, "Removing VF representor\n");
  279. unregister_netdev(rep_dev);
  280. ef100_rep_unset_devlink_port(efv);
  281. efx_ef100_deconfigure_rep(efv);
  282. efx_ef100_rep_destroy_netdev(efv);
  283. }
  284. void efx_ef100_fini_vfreps(struct efx_nic *efx)
  285. {
  286. struct ef100_nic_data *nic_data = efx->nic_data;
  287. struct efx_rep *efv, *next;
  288. if (!nic_data->grp_mae)
  289. return;
  290. list_for_each_entry_safe(efv, next, &efx->vf_reps, list)
  291. efx_ef100_vfrep_destroy(efx, efv);
  292. }
  293. static bool ef100_mport_is_pcie_vnic(struct mae_mport_desc *mport_desc)
  294. {
  295. return mport_desc->mport_type == MAE_MPORT_DESC_MPORT_TYPE_VNIC &&
  296. mport_desc->vnic_client_type == MAE_MPORT_DESC_VNIC_CLIENT_TYPE_FUNCTION;
  297. }
  298. bool ef100_mport_on_local_intf(struct efx_nic *efx,
  299. struct mae_mport_desc *mport_desc)
  300. {
  301. struct ef100_nic_data *nic_data = efx->nic_data;
  302. bool pcie_func;
  303. pcie_func = ef100_mport_is_pcie_vnic(mport_desc);
  304. return nic_data->have_local_intf && pcie_func &&
  305. mport_desc->interface_idx == nic_data->local_mae_intf;
  306. }
  307. bool ef100_mport_is_vf(struct mae_mport_desc *mport_desc)
  308. {
  309. bool pcie_func;
  310. pcie_func = ef100_mport_is_pcie_vnic(mport_desc);
  311. return pcie_func && (mport_desc->vf_idx != MAE_MPORT_DESC_VF_IDX_NULL);
  312. }
  313. void efx_ef100_init_reps(struct efx_nic *efx)
  314. {
  315. struct ef100_nic_data *nic_data = efx->nic_data;
  316. int rc;
  317. nic_data->have_local_intf = false;
  318. rc = efx_mae_enumerate_mports(efx);
  319. if (rc)
  320. pci_warn(efx->pci_dev,
  321. "Could not enumerate mports (rc=%d), are we admin?",
  322. rc);
  323. }
  324. void efx_ef100_fini_reps(struct efx_nic *efx)
  325. {
  326. struct efx_mae *mae = efx->mae;
  327. rhashtable_free_and_destroy(&mae->mports_ht, efx_mae_remove_mport,
  328. NULL);
  329. }
  330. static int efx_ef100_rep_poll(struct napi_struct *napi, int weight)
  331. {
  332. struct efx_rep *efv = container_of(napi, struct efx_rep, napi);
  333. unsigned int read_index;
  334. struct list_head head;
  335. struct sk_buff *skb;
  336. bool need_resched;
  337. int spent = 0;
  338. INIT_LIST_HEAD(&head);
  339. /* Grab up to 'weight' pending SKBs */
  340. spin_lock_bh(&efv->rx_lock);
  341. read_index = efv->write_index;
  342. while (spent < weight && !list_empty(&efv->rx_list)) {
  343. skb = list_first_entry(&efv->rx_list, struct sk_buff, list);
  344. list_del(&skb->list);
  345. list_add_tail(&skb->list, &head);
  346. spent++;
  347. }
  348. spin_unlock_bh(&efv->rx_lock);
  349. /* Receive them */
  350. netif_receive_skb_list(&head);
  351. if (spent < weight)
  352. if (napi_complete_done(napi, spent)) {
  353. spin_lock_bh(&efv->rx_lock);
  354. efv->read_index = read_index;
  355. /* If write_index advanced while we were doing the
  356. * RX, then storing our read_index won't re-prime the
  357. * fake-interrupt. In that case, we need to schedule
  358. * NAPI again to consume the additional packet(s).
  359. */
  360. need_resched = efv->write_index != read_index;
  361. spin_unlock_bh(&efv->rx_lock);
  362. if (need_resched)
  363. napi_schedule(&efv->napi);
  364. }
  365. return spent;
  366. }
  367. void efx_ef100_rep_rx_packet(struct efx_rep *efv, struct efx_rx_buffer *rx_buf)
  368. {
  369. u8 *eh = efx_rx_buf_va(rx_buf);
  370. struct sk_buff *skb;
  371. bool primed;
  372. /* Don't allow too many queued SKBs to build up, as they consume
  373. * GFP_ATOMIC memory. If we overrun, just start dropping.
  374. */
  375. if (efv->write_index - READ_ONCE(efv->read_index) > efv->rx_pring_size) {
  376. atomic64_inc(&efv->stats.rx_dropped);
  377. if (net_ratelimit())
  378. netif_dbg(efv->parent, rx_err, efv->net_dev,
  379. "nodesc-dropped packet of length %u\n",
  380. rx_buf->len);
  381. return;
  382. }
  383. skb = netdev_alloc_skb(efv->net_dev, rx_buf->len);
  384. if (!skb) {
  385. atomic64_inc(&efv->stats.rx_dropped);
  386. if (net_ratelimit())
  387. netif_dbg(efv->parent, rx_err, efv->net_dev,
  388. "noskb-dropped packet of length %u\n",
  389. rx_buf->len);
  390. return;
  391. }
  392. memcpy(skb->data, eh, rx_buf->len);
  393. __skb_put(skb, rx_buf->len);
  394. skb_record_rx_queue(skb, 0); /* rep is single-queue */
  395. /* Move past the ethernet header */
  396. skb->protocol = eth_type_trans(skb, efv->net_dev);
  397. skb_checksum_none_assert(skb);
  398. atomic64_inc(&efv->stats.rx_packets);
  399. atomic64_add(rx_buf->len, &efv->stats.rx_bytes);
  400. /* Add it to the rx list */
  401. spin_lock_bh(&efv->rx_lock);
  402. primed = efv->read_index == efv->write_index;
  403. list_add_tail(&skb->list, &efv->rx_list);
  404. efv->write_index++;
  405. spin_unlock_bh(&efv->rx_lock);
  406. /* Trigger rx work */
  407. if (primed)
  408. napi_schedule(&efv->napi);
  409. }
  410. struct efx_rep *efx_ef100_find_rep_by_mport(struct efx_nic *efx, u16 mport)
  411. {
  412. struct efx_rep *efv, *out = NULL;
  413. /* spinlock guards against list mutation while we're walking it;
  414. * but caller must also hold rcu_read_lock() to ensure the netdev
  415. * isn't freed after we drop the spinlock.
  416. */
  417. spin_lock_bh(&efx->vf_reps_lock);
  418. list_for_each_entry(efv, &efx->vf_reps, list)
  419. if (efv->mport == mport) {
  420. out = efv;
  421. break;
  422. }
  423. spin_unlock_bh(&efx->vf_reps_lock);
  424. return out;
  425. }