ef100_netdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. static void ef100_update_name(struct efx_nic *efx)
  28. {
  29. strcpy(efx->name, efx->net_dev->name);
  30. }
  31. static int ef100_alloc_vis(struct efx_nic *efx, unsigned int *allocated_vis)
  32. {
  33. /* EF100 uses a single TXQ per channel, as all checksum offloading
  34. * is configured in the TX descriptor, and there is no TX Pacer for
  35. * HIGHPRI queues.
  36. */
  37. unsigned int tx_vis = efx->n_tx_channels + efx->n_extra_tx_channels;
  38. unsigned int rx_vis = efx->n_rx_channels;
  39. unsigned int min_vis, max_vis;
  40. EFX_WARN_ON_PARANOID(efx->tx_queues_per_channel != 1);
  41. tx_vis += efx->n_xdp_channels * efx->xdp_tx_per_channel;
  42. max_vis = max(rx_vis, tx_vis);
  43. /* Currently don't handle resource starvation and only accept
  44. * our maximum needs and no less.
  45. */
  46. min_vis = max_vis;
  47. return efx_mcdi_alloc_vis(efx, min_vis, max_vis,
  48. NULL, allocated_vis);
  49. }
  50. static int ef100_remap_bar(struct efx_nic *efx, int max_vis)
  51. {
  52. unsigned int uc_mem_map_size;
  53. void __iomem *membase;
  54. efx->max_vis = max_vis;
  55. uc_mem_map_size = PAGE_ALIGN(max_vis * efx->vi_stride);
  56. /* Extend the original UC mapping of the memory BAR */
  57. membase = ioremap(efx->membase_phys, uc_mem_map_size);
  58. if (!membase) {
  59. netif_err(efx, probe, efx->net_dev,
  60. "could not extend memory BAR to %x\n",
  61. uc_mem_map_size);
  62. return -ENOMEM;
  63. }
  64. iounmap(efx->membase);
  65. efx->membase = membase;
  66. return 0;
  67. }
  68. /* Context: process, rtnl_lock() held.
  69. * Note that the kernel will ignore our return code; this method
  70. * should really be a void.
  71. */
  72. static int ef100_net_stop(struct net_device *net_dev)
  73. {
  74. struct efx_nic *efx = efx_netdev_priv(net_dev);
  75. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  76. raw_smp_processor_id());
  77. efx_detach_reps(efx);
  78. netif_stop_queue(net_dev);
  79. efx_stop_all(efx);
  80. efx_mcdi_mac_fini_stats(efx);
  81. efx_disable_interrupts(efx);
  82. efx_clear_interrupt_affinity(efx);
  83. efx_nic_fini_interrupt(efx);
  84. efx_remove_filters(efx);
  85. efx_fini_napi(efx);
  86. efx_remove_channels(efx);
  87. efx_mcdi_free_vis(efx);
  88. efx_remove_interrupts(efx);
  89. efx->state = STATE_NET_DOWN;
  90. return 0;
  91. }
  92. /* Context: process, rtnl_lock() held. */
  93. static int ef100_net_open(struct net_device *net_dev)
  94. {
  95. struct efx_nic *efx = efx_netdev_priv(net_dev);
  96. unsigned int allocated_vis;
  97. int rc;
  98. ef100_update_name(efx);
  99. netif_dbg(efx, ifup, net_dev, "opening device on CPU %d\n",
  100. raw_smp_processor_id());
  101. rc = efx_check_disabled(efx);
  102. if (rc)
  103. goto fail;
  104. rc = efx_probe_interrupts(efx);
  105. if (rc)
  106. goto fail;
  107. rc = efx_set_channels(efx);
  108. if (rc)
  109. goto fail;
  110. rc = efx_mcdi_free_vis(efx);
  111. if (rc)
  112. goto fail;
  113. rc = ef100_alloc_vis(efx, &allocated_vis);
  114. if (rc)
  115. goto fail;
  116. rc = efx_probe_channels(efx);
  117. if (rc)
  118. return rc;
  119. rc = ef100_remap_bar(efx, allocated_vis);
  120. if (rc)
  121. goto fail;
  122. efx_init_napi(efx);
  123. rc = efx_probe_filters(efx);
  124. if (rc)
  125. goto fail;
  126. rc = efx_nic_init_interrupt(efx);
  127. if (rc)
  128. goto fail;
  129. efx_set_interrupt_affinity(efx);
  130. rc = efx_enable_interrupts(efx);
  131. if (rc)
  132. goto fail;
  133. /* in case the MC rebooted while we were stopped, consume the change
  134. * to the warm reboot count
  135. */
  136. (void) efx_mcdi_poll_reboot(efx);
  137. rc = efx_mcdi_mac_init_stats(efx);
  138. if (rc)
  139. goto fail;
  140. efx_start_all(efx);
  141. /* Link state detection is normally event-driven; we have
  142. * to poll now because we could have missed a change
  143. */
  144. mutex_lock(&efx->mac_lock);
  145. if (efx_mcdi_phy_poll(efx))
  146. efx_link_status_changed(efx);
  147. mutex_unlock(&efx->mac_lock);
  148. efx->state = STATE_NET_UP;
  149. if (netif_running(efx->net_dev))
  150. efx_attach_reps(efx);
  151. return 0;
  152. fail:
  153. ef100_net_stop(net_dev);
  154. return rc;
  155. }
  156. /* Initiate a packet transmission. We use one channel per CPU
  157. * (sharing when we have more CPUs than channels).
  158. *
  159. * Context: non-blocking.
  160. * Note that returning anything other than NETDEV_TX_OK will cause the
  161. * OS to free the skb.
  162. */
  163. static netdev_tx_t ef100_hard_start_xmit(struct sk_buff *skb,
  164. struct net_device *net_dev)
  165. {
  166. struct efx_nic *efx = efx_netdev_priv(net_dev);
  167. return __ef100_hard_start_xmit(skb, efx, net_dev, NULL);
  168. }
  169. netdev_tx_t __ef100_hard_start_xmit(struct sk_buff *skb,
  170. struct efx_nic *efx,
  171. struct net_device *net_dev,
  172. struct efx_rep *efv)
  173. {
  174. struct efx_tx_queue *tx_queue;
  175. struct efx_channel *channel;
  176. int rc;
  177. channel = efx_get_tx_channel(efx, skb_get_queue_mapping(skb));
  178. netif_vdbg(efx, tx_queued, efx->net_dev,
  179. "%s len %d data %d channel %d\n", __func__,
  180. skb->len, skb->data_len, channel->channel);
  181. if (!efx->n_channels || !efx->n_tx_channels || !channel) {
  182. netif_stop_queue(net_dev);
  183. dev_kfree_skb_any(skb);
  184. goto err;
  185. }
  186. tx_queue = &channel->tx_queue[0];
  187. rc = __ef100_enqueue_skb(tx_queue, skb, efv);
  188. if (rc == 0)
  189. return NETDEV_TX_OK;
  190. err:
  191. net_dev->stats.tx_dropped++;
  192. return NETDEV_TX_OK;
  193. }
  194. static const struct net_device_ops ef100_netdev_ops = {
  195. .ndo_open = ef100_net_open,
  196. .ndo_stop = ef100_net_stop,
  197. .ndo_start_xmit = ef100_hard_start_xmit,
  198. .ndo_tx_timeout = efx_watchdog,
  199. .ndo_get_stats64 = efx_net_stats,
  200. .ndo_change_mtu = efx_change_mtu,
  201. .ndo_validate_addr = eth_validate_addr,
  202. .ndo_set_mac_address = efx_set_mac_address,
  203. .ndo_set_rx_mode = efx_set_rx_mode, /* Lookout */
  204. .ndo_set_features = efx_set_features,
  205. .ndo_get_phys_port_id = efx_get_phys_port_id,
  206. .ndo_get_phys_port_name = efx_get_phys_port_name,
  207. #ifdef CONFIG_RFS_ACCEL
  208. .ndo_rx_flow_steer = efx_filter_rfs,
  209. #endif
  210. #ifdef CONFIG_SFC_SRIOV
  211. .ndo_setup_tc = efx_tc_setup,
  212. #endif
  213. };
  214. /* Netdev registration
  215. */
  216. int ef100_netdev_event(struct notifier_block *this,
  217. unsigned long event, void *ptr)
  218. {
  219. struct efx_nic *efx = container_of(this, struct efx_nic, netdev_notifier);
  220. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  221. if (efx->net_dev == net_dev &&
  222. (event == NETDEV_CHANGENAME || event == NETDEV_REGISTER))
  223. ef100_update_name(efx);
  224. return NOTIFY_DONE;
  225. }
  226. static int ef100_register_netdev(struct efx_nic *efx)
  227. {
  228. struct net_device *net_dev = efx->net_dev;
  229. int rc;
  230. net_dev->watchdog_timeo = 5 * HZ;
  231. net_dev->irq = efx->pci_dev->irq;
  232. net_dev->netdev_ops = &ef100_netdev_ops;
  233. net_dev->min_mtu = EFX_MIN_MTU;
  234. net_dev->max_mtu = EFX_MAX_MTU;
  235. net_dev->ethtool_ops = &ef100_ethtool_ops;
  236. rtnl_lock();
  237. rc = dev_alloc_name(net_dev, net_dev->name);
  238. if (rc < 0)
  239. goto fail_locked;
  240. ef100_update_name(efx);
  241. rc = register_netdevice(net_dev);
  242. if (rc)
  243. goto fail_locked;
  244. /* Always start with carrier off; PHY events will detect the link */
  245. netif_carrier_off(net_dev);
  246. efx->state = STATE_NET_DOWN;
  247. rtnl_unlock();
  248. efx_init_mcdi_logging(efx);
  249. return 0;
  250. fail_locked:
  251. rtnl_unlock();
  252. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  253. return rc;
  254. }
  255. static void ef100_unregister_netdev(struct efx_nic *efx)
  256. {
  257. if (efx_dev_registered(efx)) {
  258. efx_fini_mcdi_logging(efx);
  259. efx->state = STATE_PROBED;
  260. unregister_netdev(efx->net_dev);
  261. }
  262. }
  263. void ef100_remove_netdev(struct efx_probe_data *probe_data)
  264. {
  265. struct efx_nic *efx = &probe_data->efx;
  266. if (!efx->net_dev)
  267. return;
  268. rtnl_lock();
  269. dev_close(efx->net_dev);
  270. rtnl_unlock();
  271. unregister_netdevice_notifier(&efx->netdev_notifier);
  272. #if defined(CONFIG_SFC_SRIOV)
  273. if (!efx->type->is_vf)
  274. efx_ef100_pci_sriov_disable(efx, true);
  275. #endif
  276. ef100_unregister_netdev(efx);
  277. #ifdef CONFIG_SFC_SRIOV
  278. efx_fini_tc(efx);
  279. #endif
  280. down_write(&efx->filter_sem);
  281. efx_mcdi_filter_table_remove(efx);
  282. up_write(&efx->filter_sem);
  283. efx_fini_channels(efx);
  284. kfree(efx->phy_data);
  285. efx->phy_data = NULL;
  286. free_netdev(efx->net_dev);
  287. efx->net_dev = NULL;
  288. efx->state = STATE_PROBED;
  289. }
  290. int ef100_probe_netdev(struct efx_probe_data *probe_data)
  291. {
  292. struct efx_nic *efx = &probe_data->efx;
  293. struct efx_probe_data **probe_ptr;
  294. struct net_device *net_dev;
  295. int rc;
  296. if (efx->mcdi->fn_flags &
  297. (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_NO_ACTIVE_PORT)) {
  298. pci_info(efx->pci_dev, "No network port on this PCI function");
  299. return 0;
  300. }
  301. /* Allocate and initialise a struct net_device */
  302. net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES);
  303. if (!net_dev)
  304. return -ENOMEM;
  305. probe_ptr = netdev_priv(net_dev);
  306. *probe_ptr = probe_data;
  307. efx->net_dev = net_dev;
  308. SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
  309. /* enable all supported features except rx-fcs and rx-all */
  310. net_dev->features |= efx->type->offload_features &
  311. ~(NETIF_F_RXFCS | NETIF_F_RXALL);
  312. net_dev->hw_features |= efx->type->offload_features;
  313. net_dev->hw_enc_features |= efx->type->offload_features;
  314. net_dev->vlan_features |= NETIF_F_HW_CSUM | NETIF_F_SG |
  315. NETIF_F_HIGHDMA | NETIF_F_ALL_TSO;
  316. netif_set_tso_max_segs(net_dev,
  317. ESE_EF100_DP_GZ_TSO_MAX_HDR_NUM_SEGS_DEFAULT);
  318. efx->mdio.dev = net_dev;
  319. rc = efx_ef100_init_datapath_caps(efx);
  320. if (rc < 0)
  321. goto fail;
  322. rc = ef100_phy_probe(efx);
  323. if (rc)
  324. goto fail;
  325. rc = efx_init_channels(efx);
  326. if (rc)
  327. goto fail;
  328. down_write(&efx->filter_sem);
  329. rc = ef100_filter_table_probe(efx);
  330. up_write(&efx->filter_sem);
  331. if (rc)
  332. goto fail;
  333. netdev_rss_key_fill(efx->rss_context.rx_hash_key,
  334. sizeof(efx->rss_context.rx_hash_key));
  335. /* Don't fail init if RSS setup doesn't work. */
  336. efx_mcdi_push_default_indir_table(efx, efx->n_rx_channels);
  337. rc = ef100_register_netdev(efx);
  338. if (rc)
  339. goto fail;
  340. if (!efx->type->is_vf) {
  341. rc = ef100_probe_netdev_pf(efx);
  342. if (rc)
  343. goto fail;
  344. }
  345. efx->netdev_notifier.notifier_call = ef100_netdev_event;
  346. rc = register_netdevice_notifier(&efx->netdev_notifier);
  347. if (rc) {
  348. netif_err(efx, probe, efx->net_dev,
  349. "Failed to register netdevice notifier, rc=%d\n", rc);
  350. goto fail;
  351. }
  352. fail:
  353. return rc;
  354. }