ef100_netdev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2018 Solarflare Communications Inc.
  5. * Copyright 2019-2020 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 "net_driver.h"
  12. #include "mcdi_port_common.h"
  13. #include "mcdi_functions.h"
  14. #include "efx_common.h"
  15. #include "efx_channels.h"
  16. #include "tx_common.h"
  17. #include "ef100_netdev.h"
  18. #include "ef100_ethtool.h"
  19. #include "nic_common.h"
  20. #include "ef100_nic.h"
  21. #include "ef100_tx.h"
  22. #include "ef100_regs.h"
  23. #include "mcdi_filters.h"
  24. #include "rx_common.h"
  25. #include "ef100_sriov.h"
  26. #include "tc_bindings.h"
  27. #include "tc_encap_actions.h"
  28. #include "efx_devlink.h"
  29. static void ef100_update_name(struct efx_nic *efx)
  30. {
  31. strcpy(efx->name, efx->net_dev->name);
  32. }
  33. static int ef100_alloc_vis(struct efx_nic *efx, unsigned int *allocated_vis)
  34. {
  35. /* EF100 uses a single TXQ per channel, as all checksum offloading
  36. * is configured in the TX descriptor, and there is no TX Pacer for
  37. * HIGHPRI queues.
  38. */
  39. unsigned int tx_vis = efx->n_tx_channels + efx->n_extra_tx_channels;
  40. unsigned int rx_vis = efx->n_rx_channels;
  41. unsigned int min_vis, max_vis;
  42. int rc;
  43. EFX_WARN_ON_PARANOID(efx->tx_queues_per_channel != 1);
  44. tx_vis += efx->n_xdp_channels * efx->xdp_tx_per_channel;
  45. max_vis = max(rx_vis, tx_vis);
  46. /* We require at least a single complete TX channel worth of queues. */
  47. min_vis = efx->tx_queues_per_channel;
  48. rc = efx_mcdi_alloc_vis(efx, min_vis, max_vis,
  49. NULL, allocated_vis);
  50. /* We retry allocating VIs by reallocating channels when we have not
  51. * been able to allocate the maximum VIs.
  52. */
  53. if (!rc && *allocated_vis < max_vis)
  54. rc = -EAGAIN;
  55. return rc;
  56. }
  57. static int ef100_remap_bar(struct efx_nic *efx, int max_vis)
  58. {
  59. unsigned int uc_mem_map_size;
  60. void __iomem *membase;
  61. efx->max_vis = max_vis;
  62. uc_mem_map_size = PAGE_ALIGN(max_vis * efx->vi_stride);
  63. /* Extend the original UC mapping of the memory BAR */
  64. membase = ioremap(efx->membase_phys, uc_mem_map_size);
  65. if (!membase) {
  66. netif_err(efx, probe, efx->net_dev,
  67. "could not extend memory BAR to %x\n",
  68. uc_mem_map_size);
  69. return -ENOMEM;
  70. }
  71. iounmap(efx->membase);
  72. efx->membase = membase;
  73. return 0;
  74. }
  75. /* Context: process, rtnl_lock() held.
  76. * Note that the kernel will ignore our return code; this method
  77. * should really be a void.
  78. */
  79. static int ef100_net_stop(struct net_device *net_dev)
  80. {
  81. struct efx_nic *efx = efx_netdev_priv(net_dev);
  82. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  83. raw_smp_processor_id());
  84. efx_detach_reps(efx);
  85. netif_stop_queue(net_dev);
  86. efx_stop_all(efx);
  87. efx_mcdi_mac_fini_stats(efx);
  88. efx_disable_interrupts(efx);
  89. efx_clear_interrupt_affinity(efx);
  90. efx_nic_fini_interrupt(efx);
  91. efx_remove_filters(efx);
  92. efx_fini_napi(efx);
  93. efx_remove_channels(efx);
  94. efx_mcdi_free_vis(efx);
  95. efx_remove_interrupts(efx);
  96. efx->state = STATE_NET_DOWN;
  97. return 0;
  98. }
  99. /* Context: process, rtnl_lock() held. */
  100. static int ef100_net_open(struct net_device *net_dev)
  101. {
  102. struct efx_nic *efx = efx_netdev_priv(net_dev);
  103. unsigned int allocated_vis;
  104. int rc;
  105. ef100_update_name(efx);
  106. netif_dbg(efx, ifup, net_dev, "opening device on CPU %d\n",
  107. raw_smp_processor_id());
  108. rc = efx_check_disabled(efx);
  109. if (rc)
  110. goto fail;
  111. rc = efx_probe_interrupts(efx);
  112. if (rc)
  113. goto fail;
  114. rc = efx_set_channels(efx);
  115. if (rc)
  116. goto fail;
  117. rc = efx_mcdi_free_vis(efx);
  118. if (rc)
  119. goto fail;
  120. rc = ef100_alloc_vis(efx, &allocated_vis);
  121. if (rc && rc != -EAGAIN)
  122. goto fail;
  123. /* Try one more time but with the maximum number of channels
  124. * equal to the allocated VIs, which would more likely succeed.
  125. */
  126. if (rc == -EAGAIN) {
  127. rc = efx_mcdi_free_vis(efx);
  128. if (rc)
  129. goto fail;
  130. efx_remove_interrupts(efx);
  131. efx->max_channels = allocated_vis;
  132. rc = efx_probe_interrupts(efx);
  133. if (rc)
  134. goto fail;
  135. rc = efx_set_channels(efx);
  136. if (rc)
  137. goto fail;
  138. rc = ef100_alloc_vis(efx, &allocated_vis);
  139. if (rc && rc != -EAGAIN)
  140. goto fail;
  141. /* It should be very unlikely that we failed here again, but in
  142. * such a case we return ENOSPC.
  143. */
  144. if (rc == -EAGAIN) {
  145. rc = -ENOSPC;
  146. goto fail;
  147. }
  148. }
  149. rc = efx_probe_channels(efx);
  150. if (rc)
  151. return rc;
  152. rc = ef100_remap_bar(efx, allocated_vis);
  153. if (rc)
  154. goto fail;
  155. efx_init_napi(efx);
  156. rc = efx_probe_filters(efx);
  157. if (rc)
  158. goto fail;
  159. rc = efx_nic_init_interrupt(efx);
  160. if (rc)
  161. goto fail;
  162. efx_set_interrupt_affinity(efx);
  163. rc = efx_enable_interrupts(efx);
  164. if (rc)
  165. goto fail;
  166. /* in case the MC rebooted while we were stopped, consume the change
  167. * to the warm reboot count
  168. */
  169. (void) efx_mcdi_poll_reboot(efx);
  170. rc = efx_mcdi_mac_init_stats(efx);
  171. if (rc)
  172. goto fail;
  173. efx_start_all(efx);
  174. /* Link state detection is normally event-driven; we have
  175. * to poll now because we could have missed a change
  176. */
  177. mutex_lock(&efx->mac_lock);
  178. if (efx_mcdi_phy_poll(efx))
  179. efx_link_status_changed(efx);
  180. mutex_unlock(&efx->mac_lock);
  181. efx->state = STATE_NET_UP;
  182. if (netif_running(efx->net_dev))
  183. efx_attach_reps(efx);
  184. return 0;
  185. fail:
  186. ef100_net_stop(net_dev);
  187. return rc;
  188. }
  189. /* Initiate a packet transmission. We use one channel per CPU
  190. * (sharing when we have more CPUs than channels).
  191. *
  192. * Context: non-blocking.
  193. * Note that returning anything other than NETDEV_TX_OK will cause the
  194. * OS to free the skb.
  195. */
  196. static netdev_tx_t ef100_hard_start_xmit(struct sk_buff *skb,
  197. struct net_device *net_dev)
  198. {
  199. struct efx_nic *efx = efx_netdev_priv(net_dev);
  200. return __ef100_hard_start_xmit(skb, efx, net_dev, NULL);
  201. }
  202. netdev_tx_t __ef100_hard_start_xmit(struct sk_buff *skb,
  203. struct efx_nic *efx,
  204. struct net_device *net_dev,
  205. struct efx_rep *efv)
  206. {
  207. struct efx_tx_queue *tx_queue;
  208. struct efx_channel *channel;
  209. int rc;
  210. channel = efx_get_tx_channel(efx, skb_get_queue_mapping(skb));
  211. netif_vdbg(efx, tx_queued, efx->net_dev,
  212. "%s len %d data %d channel %d\n", __func__,
  213. skb->len, skb->data_len, channel->channel);
  214. if (!efx->n_channels || !efx->n_tx_channels || !channel) {
  215. netif_stop_queue(net_dev);
  216. dev_kfree_skb_any(skb);
  217. goto err;
  218. }
  219. tx_queue = &channel->tx_queue[0];
  220. rc = __ef100_enqueue_skb(tx_queue, skb, efv);
  221. if (rc == 0)
  222. return NETDEV_TX_OK;
  223. err:
  224. net_dev->stats.tx_dropped++;
  225. return NETDEV_TX_OK;
  226. }
  227. static const struct net_device_ops ef100_netdev_ops = {
  228. .ndo_open = ef100_net_open,
  229. .ndo_stop = ef100_net_stop,
  230. .ndo_start_xmit = ef100_hard_start_xmit,
  231. .ndo_tx_timeout = efx_watchdog,
  232. .ndo_get_stats64 = efx_net_stats,
  233. .ndo_change_mtu = efx_change_mtu,
  234. .ndo_validate_addr = eth_validate_addr,
  235. .ndo_set_mac_address = efx_set_mac_address,
  236. .ndo_set_rx_mode = efx_set_rx_mode, /* Lookout */
  237. .ndo_set_features = efx_set_features,
  238. .ndo_get_phys_port_id = efx_get_phys_port_id,
  239. .ndo_get_phys_port_name = efx_get_phys_port_name,
  240. #ifdef CONFIG_RFS_ACCEL
  241. .ndo_rx_flow_steer = efx_filter_rfs,
  242. #endif
  243. #ifdef CONFIG_SFC_SRIOV
  244. .ndo_setup_tc = efx_tc_setup,
  245. #endif
  246. };
  247. /* Netdev registration
  248. */
  249. int ef100_netdev_event(struct notifier_block *this,
  250. unsigned long event, void *ptr)
  251. {
  252. struct efx_nic *efx = container_of(this, struct efx_nic, netdev_notifier);
  253. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  254. struct ef100_nic_data *nic_data = efx->nic_data;
  255. int err;
  256. if (efx->net_dev == net_dev &&
  257. (event == NETDEV_CHANGENAME || event == NETDEV_REGISTER))
  258. ef100_update_name(efx);
  259. if (!nic_data->grp_mae)
  260. return NOTIFY_DONE;
  261. err = efx_tc_netdev_event(efx, event, net_dev);
  262. if (err & NOTIFY_STOP_MASK)
  263. return err;
  264. return NOTIFY_DONE;
  265. }
  266. static int ef100_netevent_event(struct notifier_block *this,
  267. unsigned long event, void *ptr)
  268. {
  269. struct efx_nic *efx = container_of(this, struct efx_nic, netevent_notifier);
  270. struct ef100_nic_data *nic_data = efx->nic_data;
  271. int err;
  272. if (!nic_data->grp_mae)
  273. return NOTIFY_DONE;
  274. err = efx_tc_netevent_event(efx, event, ptr);
  275. if (err & NOTIFY_STOP_MASK)
  276. return err;
  277. return NOTIFY_DONE;
  278. };
  279. static int ef100_register_netdev(struct efx_nic *efx)
  280. {
  281. struct net_device *net_dev = efx->net_dev;
  282. int rc;
  283. net_dev->watchdog_timeo = 5 * HZ;
  284. net_dev->irq = efx->pci_dev->irq;
  285. net_dev->netdev_ops = &ef100_netdev_ops;
  286. net_dev->min_mtu = EFX_MIN_MTU;
  287. net_dev->max_mtu = EFX_MAX_MTU;
  288. net_dev->ethtool_ops = &ef100_ethtool_ops;
  289. rtnl_lock();
  290. rc = dev_alloc_name(net_dev, net_dev->name);
  291. if (rc < 0)
  292. goto fail_locked;
  293. ef100_update_name(efx);
  294. rc = register_netdevice(net_dev);
  295. if (rc)
  296. goto fail_locked;
  297. /* Always start with carrier off; PHY events will detect the link */
  298. netif_carrier_off(net_dev);
  299. efx->state = STATE_NET_DOWN;
  300. rtnl_unlock();
  301. efx_init_mcdi_logging(efx);
  302. return 0;
  303. fail_locked:
  304. rtnl_unlock();
  305. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  306. return rc;
  307. }
  308. static void ef100_unregister_netdev(struct efx_nic *efx)
  309. {
  310. if (efx_dev_registered(efx)) {
  311. efx_fini_mcdi_logging(efx);
  312. efx->state = STATE_PROBED;
  313. unregister_netdev(efx->net_dev);
  314. }
  315. }
  316. void ef100_remove_netdev(struct efx_probe_data *probe_data)
  317. {
  318. struct efx_nic *efx = &probe_data->efx;
  319. if (!efx->net_dev)
  320. return;
  321. rtnl_lock();
  322. dev_close(efx->net_dev);
  323. rtnl_unlock();
  324. unregister_netdevice_notifier(&efx->netdev_notifier);
  325. unregister_netevent_notifier(&efx->netevent_notifier);
  326. #if defined(CONFIG_SFC_SRIOV)
  327. if (!efx->type->is_vf)
  328. efx_ef100_pci_sriov_disable(efx, true);
  329. #endif
  330. efx_fini_devlink_lock(efx);
  331. ef100_unregister_netdev(efx);
  332. #ifdef CONFIG_SFC_SRIOV
  333. ef100_pf_unset_devlink_port(efx);
  334. efx_fini_tc(efx);
  335. #endif
  336. down_write(&efx->filter_sem);
  337. efx_mcdi_filter_table_remove(efx);
  338. up_write(&efx->filter_sem);
  339. efx_fini_channels(efx);
  340. kfree(efx->phy_data);
  341. efx->phy_data = NULL;
  342. efx_fini_devlink_and_unlock(efx);
  343. free_netdev(efx->net_dev);
  344. efx->net_dev = NULL;
  345. efx->state = STATE_PROBED;
  346. }
  347. int ef100_probe_netdev(struct efx_probe_data *probe_data)
  348. {
  349. struct efx_nic *efx = &probe_data->efx;
  350. struct efx_probe_data **probe_ptr;
  351. struct ef100_nic_data *nic_data;
  352. struct net_device *net_dev;
  353. int rc;
  354. if (efx->mcdi->fn_flags &
  355. (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_NO_ACTIVE_PORT)) {
  356. pci_info(efx->pci_dev, "No network port on this PCI function");
  357. return 0;
  358. }
  359. /* Allocate and initialise a struct net_device */
  360. net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES);
  361. if (!net_dev)
  362. return -ENOMEM;
  363. probe_ptr = netdev_priv(net_dev);
  364. *probe_ptr = probe_data;
  365. efx->net_dev = net_dev;
  366. SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
  367. /* enable all supported features except rx-fcs and rx-all */
  368. net_dev->features |= efx->type->offload_features &
  369. ~(NETIF_F_RXFCS | NETIF_F_RXALL);
  370. net_dev->hw_features |= efx->type->offload_features;
  371. net_dev->hw_enc_features |= efx->type->offload_features;
  372. net_dev->vlan_features |= NETIF_F_HW_CSUM | NETIF_F_SG |
  373. NETIF_F_HIGHDMA | NETIF_F_ALL_TSO;
  374. netif_set_tso_max_segs(net_dev,
  375. ESE_EF100_DP_GZ_TSO_MAX_HDR_NUM_SEGS_DEFAULT);
  376. efx->mdio.dev = net_dev;
  377. rc = efx_ef100_init_datapath_caps(efx);
  378. if (rc < 0)
  379. goto fail;
  380. rc = ef100_phy_probe(efx);
  381. if (rc)
  382. goto fail;
  383. rc = efx_init_channels(efx);
  384. if (rc)
  385. goto fail;
  386. down_write(&efx->filter_sem);
  387. rc = ef100_filter_table_probe(efx);
  388. up_write(&efx->filter_sem);
  389. if (rc)
  390. goto fail;
  391. netdev_rss_key_fill(efx->rss_context.rx_hash_key,
  392. sizeof(efx->rss_context.rx_hash_key));
  393. /* Don't fail init if RSS setup doesn't work. */
  394. efx_mcdi_push_default_indir_table(efx, efx->n_rx_channels);
  395. nic_data = efx->nic_data;
  396. rc = ef100_get_mac_address(efx, net_dev->perm_addr, CLIENT_HANDLE_SELF,
  397. efx->type->is_vf);
  398. if (rc)
  399. return rc;
  400. /* Assign MAC address */
  401. eth_hw_addr_set(net_dev, net_dev->perm_addr);
  402. ether_addr_copy(nic_data->port_id, net_dev->perm_addr);
  403. /* devlink creation, registration and lock */
  404. rc = efx_probe_devlink_and_lock(efx);
  405. if (rc)
  406. pci_info(efx->pci_dev, "devlink registration failed");
  407. rc = ef100_register_netdev(efx);
  408. if (rc)
  409. goto fail;
  410. if (!efx->type->is_vf) {
  411. rc = ef100_probe_netdev_pf(efx);
  412. if (rc)
  413. goto fail;
  414. #ifdef CONFIG_SFC_SRIOV
  415. ef100_pf_set_devlink_port(efx);
  416. #endif
  417. }
  418. efx->netdev_notifier.notifier_call = ef100_netdev_event;
  419. rc = register_netdevice_notifier(&efx->netdev_notifier);
  420. if (rc) {
  421. netif_err(efx, probe, efx->net_dev,
  422. "Failed to register netdevice notifier, rc=%d\n", rc);
  423. goto fail;
  424. }
  425. efx->netevent_notifier.notifier_call = ef100_netevent_event;
  426. rc = register_netevent_notifier(&efx->netevent_notifier);
  427. if (rc) {
  428. netif_err(efx, probe, efx->net_dev,
  429. "Failed to register netevent notifier, rc=%d\n", rc);
  430. goto fail;
  431. }
  432. efx_probe_devlink_unlock(efx);
  433. return rc;
  434. fail:
  435. #ifdef CONFIG_SFC_SRIOV
  436. /* remove devlink port if does exist */
  437. ef100_pf_unset_devlink_port(efx);
  438. #endif
  439. efx_probe_devlink_unlock(efx);
  440. return rc;
  441. }