efx_common.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2018 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include "net_driver.h"
  11. #include <linux/filter.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <net/gre.h>
  15. #include "efx_common.h"
  16. #include "efx_channels.h"
  17. #include "efx.h"
  18. #include "mcdi.h"
  19. #include "selftest.h"
  20. #include "rx_common.h"
  21. #include "tx_common.h"
  22. #include "nic.h"
  23. #include "mcdi_port_common.h"
  24. #include "io.h"
  25. #include "mcdi_pcol.h"
  26. #include "ef100_rep.h"
  27. static unsigned int debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  28. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  29. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  30. NETIF_MSG_TX_ERR | NETIF_MSG_HW);
  31. module_param(debug, uint, 0);
  32. MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
  33. /* This is the time (in jiffies) between invocations of the hardware
  34. * monitor.
  35. */
  36. static unsigned int efx_monitor_interval = 1 * HZ;
  37. /* How often and how many times to poll for a reset while waiting for a
  38. * BIST that another function started to complete.
  39. */
  40. #define BIST_WAIT_DELAY_MS 100
  41. #define BIST_WAIT_DELAY_COUNT 100
  42. /* Default stats update time */
  43. #define STATS_PERIOD_MS_DEFAULT 1000
  44. static const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
  45. static const char *const efx_reset_type_names[] = {
  46. [RESET_TYPE_INVISIBLE] = "INVISIBLE",
  47. [RESET_TYPE_ALL] = "ALL",
  48. [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
  49. [RESET_TYPE_WORLD] = "WORLD",
  50. [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
  51. [RESET_TYPE_DATAPATH] = "DATAPATH",
  52. [RESET_TYPE_MC_BIST] = "MC_BIST",
  53. [RESET_TYPE_DISABLE] = "DISABLE",
  54. [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
  55. [RESET_TYPE_INT_ERROR] = "INT_ERROR",
  56. [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
  57. [RESET_TYPE_TX_SKIP] = "TX_SKIP",
  58. [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
  59. [RESET_TYPE_MCDI_TIMEOUT] = "MCDI_TIMEOUT (FLR)",
  60. };
  61. #define RESET_TYPE(type) \
  62. STRING_TABLE_LOOKUP(type, efx_reset_type)
  63. /* Loopback mode names (see LOOPBACK_MODE()) */
  64. const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
  65. const char *const efx_loopback_mode_names[] = {
  66. [LOOPBACK_NONE] = "NONE",
  67. [LOOPBACK_DATA] = "DATAPATH",
  68. [LOOPBACK_GMAC] = "GMAC",
  69. [LOOPBACK_XGMII] = "XGMII",
  70. [LOOPBACK_XGXS] = "XGXS",
  71. [LOOPBACK_XAUI] = "XAUI",
  72. [LOOPBACK_GMII] = "GMII",
  73. [LOOPBACK_SGMII] = "SGMII",
  74. [LOOPBACK_XGBR] = "XGBR",
  75. [LOOPBACK_XFI] = "XFI",
  76. [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
  77. [LOOPBACK_GMII_FAR] = "GMII_FAR",
  78. [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
  79. [LOOPBACK_XFI_FAR] = "XFI_FAR",
  80. [LOOPBACK_GPHY] = "GPHY",
  81. [LOOPBACK_PHYXS] = "PHYXS",
  82. [LOOPBACK_PCS] = "PCS",
  83. [LOOPBACK_PMAPMD] = "PMA/PMD",
  84. [LOOPBACK_XPORT] = "XPORT",
  85. [LOOPBACK_XGMII_WS] = "XGMII_WS",
  86. [LOOPBACK_XAUI_WS] = "XAUI_WS",
  87. [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
  88. [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
  89. [LOOPBACK_GMII_WS] = "GMII_WS",
  90. [LOOPBACK_XFI_WS] = "XFI_WS",
  91. [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
  92. [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
  93. };
  94. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  95. * queued onto this work queue. This is not a per-nic work queue, because
  96. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  97. */
  98. static struct workqueue_struct *reset_workqueue;
  99. int efx_create_reset_workqueue(void)
  100. {
  101. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  102. if (!reset_workqueue) {
  103. printk(KERN_ERR "Failed to create reset workqueue\n");
  104. return -ENOMEM;
  105. }
  106. return 0;
  107. }
  108. void efx_queue_reset_work(struct efx_nic *efx)
  109. {
  110. queue_work(reset_workqueue, &efx->reset_work);
  111. }
  112. void efx_flush_reset_workqueue(struct efx_nic *efx)
  113. {
  114. cancel_work_sync(&efx->reset_work);
  115. }
  116. void efx_destroy_reset_workqueue(void)
  117. {
  118. if (reset_workqueue) {
  119. destroy_workqueue(reset_workqueue);
  120. reset_workqueue = NULL;
  121. }
  122. }
  123. /* We assume that efx->type->reconfigure_mac will always try to sync RX
  124. * filters and therefore needs to read-lock the filter table against freeing
  125. */
  126. void efx_mac_reconfigure(struct efx_nic *efx, bool mtu_only)
  127. {
  128. if (efx->type->reconfigure_mac) {
  129. down_read(&efx->filter_sem);
  130. efx->type->reconfigure_mac(efx, mtu_only);
  131. up_read(&efx->filter_sem);
  132. }
  133. }
  134. /* Asynchronous work item for changing MAC promiscuity and multicast
  135. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  136. * MAC directly.
  137. */
  138. static void efx_mac_work(struct work_struct *data)
  139. {
  140. struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
  141. mutex_lock(&efx->mac_lock);
  142. if (efx->port_enabled)
  143. efx_mac_reconfigure(efx, false);
  144. mutex_unlock(&efx->mac_lock);
  145. }
  146. int efx_set_mac_address(struct net_device *net_dev, void *data)
  147. {
  148. struct efx_nic *efx = efx_netdev_priv(net_dev);
  149. struct sockaddr *addr = data;
  150. u8 *new_addr = addr->sa_data;
  151. u8 old_addr[6];
  152. int rc;
  153. if (!is_valid_ether_addr(new_addr)) {
  154. netif_err(efx, drv, efx->net_dev,
  155. "invalid ethernet MAC address requested: %pM\n",
  156. new_addr);
  157. return -EADDRNOTAVAIL;
  158. }
  159. /* save old address */
  160. ether_addr_copy(old_addr, net_dev->dev_addr);
  161. eth_hw_addr_set(net_dev, new_addr);
  162. if (efx->type->set_mac_address) {
  163. rc = efx->type->set_mac_address(efx);
  164. if (rc) {
  165. eth_hw_addr_set(net_dev, old_addr);
  166. return rc;
  167. }
  168. }
  169. /* Reconfigure the MAC */
  170. mutex_lock(&efx->mac_lock);
  171. efx_mac_reconfigure(efx, false);
  172. mutex_unlock(&efx->mac_lock);
  173. return 0;
  174. }
  175. /* Context: netif_addr_lock held, BHs disabled. */
  176. void efx_set_rx_mode(struct net_device *net_dev)
  177. {
  178. struct efx_nic *efx = efx_netdev_priv(net_dev);
  179. if (efx->port_enabled)
  180. queue_work(efx->workqueue, &efx->mac_work);
  181. /* Otherwise efx_start_port() will do this */
  182. }
  183. int efx_set_features(struct net_device *net_dev, netdev_features_t data)
  184. {
  185. struct efx_nic *efx = efx_netdev_priv(net_dev);
  186. int rc;
  187. /* If disabling RX n-tuple filtering, clear existing filters */
  188. if (net_dev->features & ~data & NETIF_F_NTUPLE) {
  189. rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
  190. if (rc)
  191. return rc;
  192. }
  193. /* If Rx VLAN filter is changed, update filters via mac_reconfigure.
  194. * If rx-fcs is changed, mac_reconfigure updates that too.
  195. */
  196. if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
  197. NETIF_F_RXFCS)) {
  198. /* efx_set_rx_mode() will schedule MAC work to update filters
  199. * when a new features are finally set in net_dev.
  200. */
  201. efx_set_rx_mode(net_dev);
  202. }
  203. return 0;
  204. }
  205. /* This ensures that the kernel is kept informed (via
  206. * netif_carrier_on/off) of the link status, and also maintains the
  207. * link status's stop on the port's TX queue.
  208. */
  209. void efx_link_status_changed(struct efx_nic *efx)
  210. {
  211. struct efx_link_state *link_state = &efx->link_state;
  212. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  213. * that no events are triggered between unregister_netdev() and the
  214. * driver unloading. A more general condition is that NETDEV_CHANGE
  215. * can only be generated between NETDEV_UP and NETDEV_DOWN
  216. */
  217. if (!netif_running(efx->net_dev))
  218. return;
  219. if (link_state->up != netif_carrier_ok(efx->net_dev)) {
  220. efx->n_link_state_changes++;
  221. if (link_state->up)
  222. netif_carrier_on(efx->net_dev);
  223. else
  224. netif_carrier_off(efx->net_dev);
  225. }
  226. /* Status message for kernel log */
  227. if (link_state->up)
  228. netif_info(efx, link, efx->net_dev,
  229. "link up at %uMbps %s-duplex (MTU %d)\n",
  230. link_state->speed, link_state->fd ? "full" : "half",
  231. efx->net_dev->mtu);
  232. else
  233. netif_info(efx, link, efx->net_dev, "link down\n");
  234. }
  235. unsigned int efx_xdp_max_mtu(struct efx_nic *efx)
  236. {
  237. /* The maximum MTU that we can fit in a single page, allowing for
  238. * framing, overhead and XDP headroom + tailroom.
  239. */
  240. int overhead = EFX_MAX_FRAME_LEN(0) + sizeof(struct efx_rx_page_state) +
  241. efx->rx_prefix_size + efx->type->rx_buffer_padding +
  242. efx->rx_ip_align + EFX_XDP_HEADROOM + EFX_XDP_TAILROOM;
  243. return PAGE_SIZE - overhead;
  244. }
  245. /* Context: process, rtnl_lock() held. */
  246. int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  247. {
  248. struct efx_nic *efx = efx_netdev_priv(net_dev);
  249. int rc;
  250. rc = efx_check_disabled(efx);
  251. if (rc)
  252. return rc;
  253. if (rtnl_dereference(efx->xdp_prog) &&
  254. new_mtu > efx_xdp_max_mtu(efx)) {
  255. netif_err(efx, drv, efx->net_dev,
  256. "Requested MTU of %d too big for XDP (max: %d)\n",
  257. new_mtu, efx_xdp_max_mtu(efx));
  258. return -EINVAL;
  259. }
  260. netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
  261. efx_device_detach_sync(efx);
  262. efx_stop_all(efx);
  263. mutex_lock(&efx->mac_lock);
  264. WRITE_ONCE(net_dev->mtu, new_mtu);
  265. efx_mac_reconfigure(efx, true);
  266. mutex_unlock(&efx->mac_lock);
  267. efx_start_all(efx);
  268. efx_device_attach_if_not_resetting(efx);
  269. return 0;
  270. }
  271. /**************************************************************************
  272. *
  273. * Hardware monitor
  274. *
  275. **************************************************************************/
  276. /* Run periodically off the general workqueue */
  277. static void efx_monitor(struct work_struct *data)
  278. {
  279. struct efx_nic *efx = container_of(data, struct efx_nic,
  280. monitor_work.work);
  281. netif_vdbg(efx, timer, efx->net_dev,
  282. "hardware monitor executing on CPU %d\n",
  283. raw_smp_processor_id());
  284. BUG_ON(efx->type->monitor == NULL);
  285. /* If the mac_lock is already held then it is likely a port
  286. * reconfiguration is already in place, which will likely do
  287. * most of the work of monitor() anyway.
  288. */
  289. if (mutex_trylock(&efx->mac_lock)) {
  290. if (efx->port_enabled && efx->type->monitor)
  291. efx->type->monitor(efx);
  292. mutex_unlock(&efx->mac_lock);
  293. }
  294. efx_start_monitor(efx);
  295. }
  296. void efx_start_monitor(struct efx_nic *efx)
  297. {
  298. if (efx->type->monitor)
  299. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  300. efx_monitor_interval);
  301. }
  302. /**************************************************************************
  303. *
  304. * Event queue processing
  305. *
  306. *************************************************************************/
  307. /* Channels are shutdown and reinitialised whilst the NIC is running
  308. * to propagate configuration changes (mtu, checksum offload), or
  309. * to clear hardware error conditions
  310. */
  311. static void efx_start_datapath(struct efx_nic *efx)
  312. {
  313. netdev_features_t old_features = efx->net_dev->features;
  314. bool old_rx_scatter = efx->rx_scatter;
  315. size_t rx_buf_len;
  316. /* Calculate the rx buffer allocation parameters required to
  317. * support the current MTU, including padding for header
  318. * alignment and overruns.
  319. */
  320. efx->rx_dma_len = (efx->rx_prefix_size +
  321. EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
  322. efx->type->rx_buffer_padding);
  323. rx_buf_len = (sizeof(struct efx_rx_page_state) + EFX_XDP_HEADROOM +
  324. efx->rx_ip_align + efx->rx_dma_len + EFX_XDP_TAILROOM);
  325. if (rx_buf_len <= PAGE_SIZE) {
  326. efx->rx_scatter = efx->type->always_rx_scatter;
  327. efx->rx_buffer_order = 0;
  328. } else if (efx->type->can_rx_scatter) {
  329. BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
  330. BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
  331. 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
  332. EFX_RX_BUF_ALIGNMENT) >
  333. PAGE_SIZE);
  334. efx->rx_scatter = true;
  335. efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
  336. efx->rx_buffer_order = 0;
  337. } else {
  338. efx->rx_scatter = false;
  339. efx->rx_buffer_order = get_order(rx_buf_len);
  340. }
  341. efx_rx_config_page_split(efx);
  342. if (efx->rx_buffer_order)
  343. netif_dbg(efx, drv, efx->net_dev,
  344. "RX buf len=%u; page order=%u batch=%u\n",
  345. efx->rx_dma_len, efx->rx_buffer_order,
  346. efx->rx_pages_per_batch);
  347. else
  348. netif_dbg(efx, drv, efx->net_dev,
  349. "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
  350. efx->rx_dma_len, efx->rx_page_buf_step,
  351. efx->rx_bufs_per_page, efx->rx_pages_per_batch);
  352. /* Restore previously fixed features in hw_features and remove
  353. * features which are fixed now
  354. */
  355. efx->net_dev->hw_features |= efx->net_dev->features;
  356. efx->net_dev->hw_features &= ~efx->fixed_features;
  357. efx->net_dev->features |= efx->fixed_features;
  358. if (efx->net_dev->features != old_features)
  359. netdev_features_change(efx->net_dev);
  360. /* RX filters may also have scatter-enabled flags */
  361. if ((efx->rx_scatter != old_rx_scatter) &&
  362. efx->type->filter_update_rx_scatter)
  363. efx->type->filter_update_rx_scatter(efx);
  364. /* We must keep at least one descriptor in a TX ring empty.
  365. * We could avoid this when the queue size does not exactly
  366. * match the hardware ring size, but it's not that important.
  367. * Therefore we stop the queue when one more skb might fill
  368. * the ring completely. We wake it when half way back to
  369. * empty.
  370. */
  371. efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
  372. efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
  373. /* Initialise the channels */
  374. efx_start_channels(efx);
  375. efx_ptp_start_datapath(efx);
  376. if (netif_device_present(efx->net_dev))
  377. netif_tx_wake_all_queues(efx->net_dev);
  378. }
  379. static void efx_stop_datapath(struct efx_nic *efx)
  380. {
  381. EFX_ASSERT_RESET_SERIALISED(efx);
  382. BUG_ON(efx->port_enabled);
  383. efx_ptp_stop_datapath(efx);
  384. efx_stop_channels(efx);
  385. }
  386. /**************************************************************************
  387. *
  388. * Port handling
  389. *
  390. **************************************************************************/
  391. /* Equivalent to efx_link_set_advertising with all-zeroes, except does not
  392. * force the Autoneg bit on.
  393. */
  394. void efx_link_clear_advertising(struct efx_nic *efx)
  395. {
  396. bitmap_zero(efx->link_advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
  397. efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
  398. }
  399. void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
  400. {
  401. efx->wanted_fc = wanted_fc;
  402. if (efx->link_advertising[0]) {
  403. if (wanted_fc & EFX_FC_RX)
  404. efx->link_advertising[0] |= (ADVERTISED_Pause |
  405. ADVERTISED_Asym_Pause);
  406. else
  407. efx->link_advertising[0] &= ~(ADVERTISED_Pause |
  408. ADVERTISED_Asym_Pause);
  409. if (wanted_fc & EFX_FC_TX)
  410. efx->link_advertising[0] ^= ADVERTISED_Asym_Pause;
  411. }
  412. }
  413. static void efx_start_port(struct efx_nic *efx)
  414. {
  415. netif_dbg(efx, ifup, efx->net_dev, "start port\n");
  416. BUG_ON(efx->port_enabled);
  417. mutex_lock(&efx->mac_lock);
  418. efx->port_enabled = true;
  419. /* Ensure MAC ingress/egress is enabled */
  420. efx_mac_reconfigure(efx, false);
  421. mutex_unlock(&efx->mac_lock);
  422. }
  423. /* Cancel work for MAC reconfiguration, periodic hardware monitoring
  424. * and the async self-test, wait for them to finish and prevent them
  425. * being scheduled again. This doesn't cover online resets, which
  426. * should only be cancelled when removing the device.
  427. */
  428. static void efx_stop_port(struct efx_nic *efx)
  429. {
  430. netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
  431. EFX_ASSERT_RESET_SERIALISED(efx);
  432. mutex_lock(&efx->mac_lock);
  433. efx->port_enabled = false;
  434. mutex_unlock(&efx->mac_lock);
  435. /* Serialise against efx_set_multicast_list() */
  436. netif_addr_lock_bh(efx->net_dev);
  437. netif_addr_unlock_bh(efx->net_dev);
  438. cancel_delayed_work_sync(&efx->monitor_work);
  439. efx_selftest_async_cancel(efx);
  440. cancel_work_sync(&efx->mac_work);
  441. }
  442. /* If the interface is supposed to be running but is not, start
  443. * the hardware and software data path, regular activity for the port
  444. * (MAC statistics, link polling, etc.) and schedule the port to be
  445. * reconfigured. Interrupts must already be enabled. This function
  446. * is safe to call multiple times, so long as the NIC is not disabled.
  447. * Requires the RTNL lock.
  448. */
  449. void efx_start_all(struct efx_nic *efx)
  450. {
  451. EFX_ASSERT_RESET_SERIALISED(efx);
  452. BUG_ON(efx->state == STATE_DISABLED);
  453. /* Check that it is appropriate to restart the interface. All
  454. * of these flags are safe to read under just the rtnl lock
  455. */
  456. if (efx->port_enabled || !netif_running(efx->net_dev) ||
  457. efx->reset_pending)
  458. return;
  459. efx_start_port(efx);
  460. efx_start_datapath(efx);
  461. /* Start the hardware monitor if there is one */
  462. efx_start_monitor(efx);
  463. efx_selftest_async_start(efx);
  464. /* Link state detection is normally event-driven; we have
  465. * to poll now because we could have missed a change
  466. */
  467. mutex_lock(&efx->mac_lock);
  468. if (efx_mcdi_phy_poll(efx))
  469. efx_link_status_changed(efx);
  470. mutex_unlock(&efx->mac_lock);
  471. if (efx->type->start_stats) {
  472. efx->type->start_stats(efx);
  473. efx->type->pull_stats(efx);
  474. spin_lock_bh(&efx->stats_lock);
  475. efx->type->update_stats(efx, NULL, NULL);
  476. spin_unlock_bh(&efx->stats_lock);
  477. }
  478. }
  479. /* Quiesce the hardware and software data path, and regular activity
  480. * for the port without bringing the link down. Safe to call multiple
  481. * times with the NIC in almost any state, but interrupts should be
  482. * enabled. Requires the RTNL lock.
  483. */
  484. void efx_stop_all(struct efx_nic *efx)
  485. {
  486. EFX_ASSERT_RESET_SERIALISED(efx);
  487. /* port_enabled can be read safely under the rtnl lock */
  488. if (!efx->port_enabled)
  489. return;
  490. if (efx->type->update_stats) {
  491. /* update stats before we go down so we can accurately count
  492. * rx_nodesc_drops
  493. */
  494. efx->type->pull_stats(efx);
  495. spin_lock_bh(&efx->stats_lock);
  496. efx->type->update_stats(efx, NULL, NULL);
  497. spin_unlock_bh(&efx->stats_lock);
  498. efx->type->stop_stats(efx);
  499. }
  500. efx_stop_port(efx);
  501. /* Stop the kernel transmit interface. This is only valid if
  502. * the device is stopped or detached; otherwise the watchdog
  503. * may fire immediately.
  504. */
  505. WARN_ON(netif_running(efx->net_dev) &&
  506. netif_device_present(efx->net_dev));
  507. netif_tx_disable(efx->net_dev);
  508. efx_stop_datapath(efx);
  509. }
  510. /* Context: process, rcu_read_lock or RTNL held, non-blocking. */
  511. void efx_net_stats(struct net_device *net_dev, struct rtnl_link_stats64 *stats)
  512. {
  513. struct efx_nic *efx = efx_netdev_priv(net_dev);
  514. spin_lock_bh(&efx->stats_lock);
  515. efx_nic_update_stats_atomic(efx, NULL, stats);
  516. spin_unlock_bh(&efx->stats_lock);
  517. }
  518. /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
  519. * the MAC appropriately. All other PHY configuration changes are pushed
  520. * through phy_op->set_settings(), and pushed asynchronously to the MAC
  521. * through efx_monitor().
  522. *
  523. * Callers must hold the mac_lock
  524. */
  525. int __efx_reconfigure_port(struct efx_nic *efx)
  526. {
  527. enum efx_phy_mode phy_mode;
  528. int rc = 0;
  529. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  530. /* Disable PHY transmit in mac level loopbacks */
  531. phy_mode = efx->phy_mode;
  532. if (LOOPBACK_INTERNAL(efx))
  533. efx->phy_mode |= PHY_MODE_TX_DISABLED;
  534. else
  535. efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
  536. if (efx->type->reconfigure_port)
  537. rc = efx->type->reconfigure_port(efx);
  538. if (rc)
  539. efx->phy_mode = phy_mode;
  540. return rc;
  541. }
  542. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  543. * disabled.
  544. */
  545. int efx_reconfigure_port(struct efx_nic *efx)
  546. {
  547. int rc;
  548. EFX_ASSERT_RESET_SERIALISED(efx);
  549. mutex_lock(&efx->mac_lock);
  550. rc = __efx_reconfigure_port(efx);
  551. mutex_unlock(&efx->mac_lock);
  552. return rc;
  553. }
  554. /**************************************************************************
  555. *
  556. * Device reset and suspend
  557. *
  558. **************************************************************************/
  559. static void efx_wait_for_bist_end(struct efx_nic *efx)
  560. {
  561. int i;
  562. for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
  563. if (efx_mcdi_poll_reboot(efx))
  564. goto out;
  565. msleep(BIST_WAIT_DELAY_MS);
  566. }
  567. netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
  568. out:
  569. /* Either way unset the BIST flag. If we found no reboot we probably
  570. * won't recover, but we should try.
  571. */
  572. efx->mc_bist_for_other_fn = false;
  573. }
  574. /* Try recovery mechanisms.
  575. * For now only EEH is supported.
  576. * Returns 0 if the recovery mechanisms are unsuccessful.
  577. * Returns a non-zero value otherwise.
  578. */
  579. int efx_try_recovery(struct efx_nic *efx)
  580. {
  581. #ifdef CONFIG_EEH
  582. /* A PCI error can occur and not be seen by EEH because nothing
  583. * happens on the PCI bus. In this case the driver may fail and
  584. * schedule a 'recover or reset', leading to this recovery handler.
  585. * Manually call the eeh failure check function.
  586. */
  587. struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
  588. if (eeh_dev_check_failure(eehdev)) {
  589. /* The EEH mechanisms will handle the error and reset the
  590. * device if necessary.
  591. */
  592. return 1;
  593. }
  594. #endif
  595. return 0;
  596. }
  597. /* Tears down the entire software state and most of the hardware state
  598. * before reset.
  599. */
  600. void efx_reset_down(struct efx_nic *efx, enum reset_type method)
  601. {
  602. EFX_ASSERT_RESET_SERIALISED(efx);
  603. if (method == RESET_TYPE_MCDI_TIMEOUT)
  604. efx->type->prepare_flr(efx);
  605. efx_stop_all(efx);
  606. efx_disable_interrupts(efx);
  607. mutex_lock(&efx->mac_lock);
  608. down_write(&efx->filter_sem);
  609. mutex_lock(&efx->net_dev->ethtool->rss_lock);
  610. efx->type->fini(efx);
  611. }
  612. /* Context: netif_tx_lock held, BHs disabled. */
  613. void efx_watchdog(struct net_device *net_dev, unsigned int txqueue)
  614. {
  615. struct efx_nic *efx = efx_netdev_priv(net_dev);
  616. netif_err(efx, tx_err, efx->net_dev,
  617. "TX stuck with port_enabled=%d: resetting channels\n",
  618. efx->port_enabled);
  619. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  620. }
  621. /* This function will always ensure that the locks acquired in
  622. * efx_reset_down() are released. A failure return code indicates
  623. * that we were unable to reinitialise the hardware, and the
  624. * driver should be disabled. If ok is false, then the rx and tx
  625. * engines are not restarted, pending a RESET_DISABLE.
  626. */
  627. int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
  628. {
  629. int rc;
  630. EFX_ASSERT_RESET_SERIALISED(efx);
  631. if (method == RESET_TYPE_MCDI_TIMEOUT)
  632. efx->type->finish_flr(efx);
  633. /* Ensure that SRAM is initialised even if we're disabling the device */
  634. rc = efx->type->init(efx);
  635. if (rc) {
  636. netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
  637. goto fail;
  638. }
  639. if (!ok)
  640. goto fail;
  641. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
  642. method != RESET_TYPE_DATAPATH) {
  643. rc = efx_mcdi_port_reconfigure(efx);
  644. if (rc && rc != -EPERM)
  645. netif_err(efx, drv, efx->net_dev,
  646. "could not restore PHY settings\n");
  647. }
  648. rc = efx_enable_interrupts(efx);
  649. if (rc)
  650. goto fail;
  651. #ifdef CONFIG_SFC_SRIOV
  652. rc = efx->type->vswitching_restore(efx);
  653. if (rc) /* not fatal; the PF will still work fine */
  654. netif_warn(efx, probe, efx->net_dev,
  655. "failed to restore vswitching rc=%d;"
  656. " VFs may not function\n", rc);
  657. #endif
  658. if (efx->type->rx_restore_rss_contexts)
  659. efx->type->rx_restore_rss_contexts(efx);
  660. mutex_unlock(&efx->net_dev->ethtool->rss_lock);
  661. efx->type->filter_table_restore(efx);
  662. up_write(&efx->filter_sem);
  663. mutex_unlock(&efx->mac_lock);
  664. efx_start_all(efx);
  665. if (efx->type->udp_tnl_push_ports)
  666. efx->type->udp_tnl_push_ports(efx);
  667. return 0;
  668. fail:
  669. efx->port_initialized = false;
  670. mutex_unlock(&efx->net_dev->ethtool->rss_lock);
  671. up_write(&efx->filter_sem);
  672. mutex_unlock(&efx->mac_lock);
  673. return rc;
  674. }
  675. /* Reset the NIC using the specified method. Note that the reset may
  676. * fail, in which case the card will be left in an unusable state.
  677. *
  678. * Caller must hold the rtnl_lock.
  679. */
  680. int efx_reset(struct efx_nic *efx, enum reset_type method)
  681. {
  682. int rc, rc2 = 0;
  683. bool disabled;
  684. netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
  685. RESET_TYPE(method));
  686. efx_device_detach_sync(efx);
  687. /* efx_reset_down() grabs locks that prevent recovery on EF100.
  688. * EF100 reset is handled in the efx_nic_type callback below.
  689. */
  690. if (efx_nic_rev(efx) != EFX_REV_EF100)
  691. efx_reset_down(efx, method);
  692. rc = efx->type->reset(efx, method);
  693. if (rc) {
  694. netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
  695. goto out;
  696. }
  697. /* Clear flags for the scopes we covered. We assume the NIC and
  698. * driver are now quiescent so that there is no race here.
  699. */
  700. if (method < RESET_TYPE_MAX_METHOD)
  701. efx->reset_pending &= -(1 << (method + 1));
  702. else /* it doesn't fit into the well-ordered scope hierarchy */
  703. __clear_bit(method, &efx->reset_pending);
  704. /* Reinitialise bus-mastering, which may have been turned off before
  705. * the reset was scheduled. This is still appropriate, even in the
  706. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  707. * can respond to requests.
  708. */
  709. pci_set_master(efx->pci_dev);
  710. out:
  711. /* Leave device stopped if necessary */
  712. disabled = rc ||
  713. method == RESET_TYPE_DISABLE ||
  714. method == RESET_TYPE_RECOVER_OR_DISABLE;
  715. if (efx_nic_rev(efx) != EFX_REV_EF100)
  716. rc2 = efx_reset_up(efx, method, !disabled);
  717. if (rc2) {
  718. disabled = true;
  719. if (!rc)
  720. rc = rc2;
  721. }
  722. if (disabled) {
  723. dev_close(efx->net_dev);
  724. netif_err(efx, drv, efx->net_dev, "has been disabled\n");
  725. efx->state = STATE_DISABLED;
  726. } else {
  727. netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
  728. efx_device_attach_if_not_resetting(efx);
  729. }
  730. return rc;
  731. }
  732. /* The worker thread exists so that code that cannot sleep can
  733. * schedule a reset for later.
  734. */
  735. static void efx_reset_work(struct work_struct *data)
  736. {
  737. struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
  738. unsigned long pending;
  739. enum reset_type method;
  740. pending = READ_ONCE(efx->reset_pending);
  741. method = fls(pending) - 1;
  742. if (method == RESET_TYPE_MC_BIST)
  743. efx_wait_for_bist_end(efx);
  744. if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
  745. method == RESET_TYPE_RECOVER_OR_ALL) &&
  746. efx_try_recovery(efx))
  747. return;
  748. if (!pending)
  749. return;
  750. rtnl_lock();
  751. /* We checked the state in efx_schedule_reset() but it may
  752. * have changed by now. Now that we have the RTNL lock,
  753. * it cannot change again.
  754. */
  755. if (efx_net_active(efx->state))
  756. (void)efx_reset(efx, method);
  757. rtnl_unlock();
  758. }
  759. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  760. {
  761. enum reset_type method;
  762. if (efx_recovering(efx->state)) {
  763. netif_dbg(efx, drv, efx->net_dev,
  764. "recovering: skip scheduling %s reset\n",
  765. RESET_TYPE(type));
  766. return;
  767. }
  768. switch (type) {
  769. case RESET_TYPE_INVISIBLE:
  770. case RESET_TYPE_ALL:
  771. case RESET_TYPE_RECOVER_OR_ALL:
  772. case RESET_TYPE_WORLD:
  773. case RESET_TYPE_DISABLE:
  774. case RESET_TYPE_RECOVER_OR_DISABLE:
  775. case RESET_TYPE_DATAPATH:
  776. case RESET_TYPE_MC_BIST:
  777. case RESET_TYPE_MCDI_TIMEOUT:
  778. method = type;
  779. netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
  780. RESET_TYPE(method));
  781. break;
  782. default:
  783. method = efx->type->map_reset_reason(type);
  784. netif_dbg(efx, drv, efx->net_dev,
  785. "scheduling %s reset for %s\n",
  786. RESET_TYPE(method), RESET_TYPE(type));
  787. break;
  788. }
  789. set_bit(method, &efx->reset_pending);
  790. smp_mb(); /* ensure we change reset_pending before checking state */
  791. /* If we're not READY then just leave the flags set as the cue
  792. * to abort probing or reschedule the reset later.
  793. */
  794. if (!efx_net_active(READ_ONCE(efx->state)))
  795. return;
  796. /* efx_process_channel() will no longer read events once a
  797. * reset is scheduled. So switch back to poll'd MCDI completions.
  798. */
  799. efx_mcdi_mode_poll(efx);
  800. efx_queue_reset_work(efx);
  801. }
  802. /**************************************************************************
  803. *
  804. * Dummy NIC operations
  805. *
  806. * Can be used for some unimplemented operations
  807. * Needed so all function pointers are valid and do not have to be tested
  808. * before use
  809. *
  810. **************************************************************************/
  811. int efx_port_dummy_op_int(struct efx_nic *efx)
  812. {
  813. return 0;
  814. }
  815. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  816. /**************************************************************************
  817. *
  818. * Data housekeeping
  819. *
  820. **************************************************************************/
  821. /* This zeroes out and then fills in the invariants in a struct
  822. * efx_nic (including all sub-structures).
  823. */
  824. int efx_init_struct(struct efx_nic *efx, struct pci_dev *pci_dev)
  825. {
  826. int rc = -ENOMEM;
  827. /* Initialise common structures */
  828. INIT_LIST_HEAD(&efx->node);
  829. INIT_LIST_HEAD(&efx->secondary_list);
  830. spin_lock_init(&efx->biu_lock);
  831. #ifdef CONFIG_SFC_MTD
  832. INIT_LIST_HEAD(&efx->mtd_list);
  833. #endif
  834. INIT_WORK(&efx->reset_work, efx_reset_work);
  835. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  836. efx_selftest_async_init(efx);
  837. efx->pci_dev = pci_dev;
  838. efx->msg_enable = debug;
  839. efx->state = STATE_UNINIT;
  840. strscpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  841. efx->rx_prefix_size = efx->type->rx_prefix_size;
  842. efx->rx_ip_align =
  843. NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
  844. efx->rx_packet_hash_offset =
  845. efx->type->rx_hash_offset - efx->type->rx_prefix_size;
  846. efx->rx_packet_ts_offset =
  847. efx->type->rx_ts_offset - efx->type->rx_prefix_size;
  848. efx->rss_context.priv.context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
  849. efx->vport_id = EVB_PORT_ID_ASSIGNED;
  850. spin_lock_init(&efx->stats_lock);
  851. efx->vi_stride = EFX_DEFAULT_VI_STRIDE;
  852. efx->num_mac_stats = MC_CMD_MAC_NSTATS;
  853. BUILD_BUG_ON(MC_CMD_MAC_NSTATS - 1 != MC_CMD_MAC_GENERATION_END);
  854. mutex_init(&efx->mac_lock);
  855. init_rwsem(&efx->filter_sem);
  856. #ifdef CONFIG_RFS_ACCEL
  857. mutex_init(&efx->rps_mutex);
  858. spin_lock_init(&efx->rps_hash_lock);
  859. /* Failure to allocate is not fatal, but may degrade ARFS performance */
  860. efx->rps_hash_table = kcalloc(EFX_ARFS_HASH_TABLE_SIZE,
  861. sizeof(*efx->rps_hash_table), GFP_KERNEL);
  862. #endif
  863. spin_lock_init(&efx->vf_reps_lock);
  864. INIT_LIST_HEAD(&efx->vf_reps);
  865. INIT_WORK(&efx->mac_work, efx_mac_work);
  866. init_waitqueue_head(&efx->flush_wq);
  867. efx->tx_queues_per_channel = 1;
  868. efx->rxq_entries = EFX_DEFAULT_DMAQ_SIZE;
  869. efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
  870. efx->mem_bar = UINT_MAX;
  871. rc = efx_init_channels(efx);
  872. if (rc)
  873. goto fail;
  874. /* Would be good to use the net_dev name, but we're too early */
  875. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  876. pci_name(pci_dev));
  877. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  878. if (!efx->workqueue) {
  879. rc = -ENOMEM;
  880. goto fail;
  881. }
  882. return 0;
  883. fail:
  884. efx_fini_struct(efx);
  885. return rc;
  886. }
  887. void efx_fini_struct(struct efx_nic *efx)
  888. {
  889. #ifdef CONFIG_RFS_ACCEL
  890. kfree(efx->rps_hash_table);
  891. #endif
  892. efx_fini_channels(efx);
  893. kfree(efx->vpd_sn);
  894. if (efx->workqueue) {
  895. destroy_workqueue(efx->workqueue);
  896. efx->workqueue = NULL;
  897. }
  898. }
  899. /* This configures the PCI device to enable I/O and DMA. */
  900. int efx_init_io(struct efx_nic *efx, int bar, dma_addr_t dma_mask,
  901. unsigned int mem_map_size)
  902. {
  903. struct pci_dev *pci_dev = efx->pci_dev;
  904. int rc;
  905. efx->mem_bar = UINT_MAX;
  906. pci_dbg(pci_dev, "initialising I/O bar=%d\n", bar);
  907. rc = pci_enable_device(pci_dev);
  908. if (rc) {
  909. pci_err(pci_dev, "failed to enable PCI device\n");
  910. goto fail1;
  911. }
  912. pci_set_master(pci_dev);
  913. rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
  914. if (rc) {
  915. pci_err(efx->pci_dev, "could not find a suitable DMA mask\n");
  916. goto fail2;
  917. }
  918. pci_dbg(efx->pci_dev, "using DMA mask %llx\n", (unsigned long long)dma_mask);
  919. efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
  920. if (!efx->membase_phys) {
  921. pci_err(efx->pci_dev,
  922. "ERROR: No BAR%d mapping from the BIOS. Try pci=realloc on the kernel command line\n",
  923. bar);
  924. rc = -ENODEV;
  925. goto fail3;
  926. }
  927. rc = pci_request_region(pci_dev, bar, "sfc");
  928. if (rc) {
  929. pci_err(efx->pci_dev,
  930. "request for memory BAR[%d] failed\n", bar);
  931. rc = -EIO;
  932. goto fail3;
  933. }
  934. efx->mem_bar = bar;
  935. efx->membase = ioremap(efx->membase_phys, mem_map_size);
  936. if (!efx->membase) {
  937. pci_err(efx->pci_dev,
  938. "could not map memory BAR[%d] at %llx+%x\n", bar,
  939. (unsigned long long)efx->membase_phys, mem_map_size);
  940. rc = -ENOMEM;
  941. goto fail4;
  942. }
  943. pci_dbg(efx->pci_dev,
  944. "memory BAR[%d] at %llx+%x (virtual %p)\n", bar,
  945. (unsigned long long)efx->membase_phys, mem_map_size,
  946. efx->membase);
  947. return 0;
  948. fail4:
  949. pci_release_region(efx->pci_dev, bar);
  950. fail3:
  951. efx->membase_phys = 0;
  952. fail2:
  953. pci_disable_device(efx->pci_dev);
  954. fail1:
  955. return rc;
  956. }
  957. void efx_fini_io(struct efx_nic *efx)
  958. {
  959. pci_dbg(efx->pci_dev, "shutting down I/O\n");
  960. if (efx->membase) {
  961. iounmap(efx->membase);
  962. efx->membase = NULL;
  963. }
  964. if (efx->membase_phys) {
  965. pci_release_region(efx->pci_dev, efx->mem_bar);
  966. efx->membase_phys = 0;
  967. efx->mem_bar = UINT_MAX;
  968. }
  969. /* Don't disable bus-mastering if VFs are assigned */
  970. if (!pci_vfs_assigned(efx->pci_dev))
  971. pci_disable_device(efx->pci_dev);
  972. }
  973. #ifdef CONFIG_SFC_MCDI_LOGGING
  974. static ssize_t mcdi_logging_show(struct device *dev,
  975. struct device_attribute *attr,
  976. char *buf)
  977. {
  978. struct efx_nic *efx = dev_get_drvdata(dev);
  979. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  980. return sysfs_emit(buf, "%d\n", mcdi->logging_enabled);
  981. }
  982. static ssize_t mcdi_logging_store(struct device *dev,
  983. struct device_attribute *attr,
  984. const char *buf, size_t count)
  985. {
  986. struct efx_nic *efx = dev_get_drvdata(dev);
  987. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  988. bool enable = count > 0 && *buf != '0';
  989. mcdi->logging_enabled = enable;
  990. return count;
  991. }
  992. static DEVICE_ATTR_RW(mcdi_logging);
  993. void efx_init_mcdi_logging(struct efx_nic *efx)
  994. {
  995. int rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
  996. if (rc) {
  997. netif_warn(efx, drv, efx->net_dev,
  998. "failed to init net dev attributes\n");
  999. }
  1000. }
  1001. void efx_fini_mcdi_logging(struct efx_nic *efx)
  1002. {
  1003. device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
  1004. }
  1005. #endif
  1006. /* A PCI error affecting this device was detected.
  1007. * At this point MMIO and DMA may be disabled.
  1008. * Stop the software path and request a slot reset.
  1009. */
  1010. static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
  1011. pci_channel_state_t state)
  1012. {
  1013. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  1014. struct efx_nic *efx = pci_get_drvdata(pdev);
  1015. if (state == pci_channel_io_perm_failure)
  1016. return PCI_ERS_RESULT_DISCONNECT;
  1017. rtnl_lock();
  1018. if (efx->state != STATE_DISABLED) {
  1019. efx->state = efx_recover(efx->state);
  1020. efx->reset_pending = 0;
  1021. efx_device_detach_sync(efx);
  1022. if (efx_net_active(efx->state)) {
  1023. efx_stop_all(efx);
  1024. efx_disable_interrupts(efx);
  1025. }
  1026. status = PCI_ERS_RESULT_NEED_RESET;
  1027. } else {
  1028. /* If the interface is disabled we don't want to do anything
  1029. * with it.
  1030. */
  1031. status = PCI_ERS_RESULT_RECOVERED;
  1032. }
  1033. rtnl_unlock();
  1034. pci_disable_device(pdev);
  1035. return status;
  1036. }
  1037. /* Fake a successful reset, which will be performed later in efx_io_resume. */
  1038. static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
  1039. {
  1040. struct efx_nic *efx = pci_get_drvdata(pdev);
  1041. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  1042. if (pci_enable_device(pdev)) {
  1043. netif_err(efx, hw, efx->net_dev,
  1044. "Cannot re-enable PCI device after reset.\n");
  1045. status = PCI_ERS_RESULT_DISCONNECT;
  1046. }
  1047. return status;
  1048. }
  1049. /* Perform the actual reset and resume I/O operations. */
  1050. static void efx_io_resume(struct pci_dev *pdev)
  1051. {
  1052. struct efx_nic *efx = pci_get_drvdata(pdev);
  1053. int rc;
  1054. rtnl_lock();
  1055. if (efx->state == STATE_DISABLED)
  1056. goto out;
  1057. rc = efx_reset(efx, RESET_TYPE_ALL);
  1058. if (rc) {
  1059. netif_err(efx, hw, efx->net_dev,
  1060. "efx_reset failed after PCI error (%d)\n", rc);
  1061. } else {
  1062. efx->state = efx_recovered(efx->state);
  1063. netif_dbg(efx, hw, efx->net_dev,
  1064. "Done resetting and resuming IO after PCI error.\n");
  1065. }
  1066. out:
  1067. rtnl_unlock();
  1068. }
  1069. /* For simplicity and reliability, we always require a slot reset and try to
  1070. * reset the hardware when a pci error affecting the device is detected.
  1071. * We leave both the link_reset and mmio_enabled callback unimplemented:
  1072. * with our request for slot reset the mmio_enabled callback will never be
  1073. * called, and the link_reset callback is not used by AER or EEH mechanisms.
  1074. */
  1075. const struct pci_error_handlers efx_err_handlers = {
  1076. .error_detected = efx_io_error_detected,
  1077. .slot_reset = efx_io_slot_reset,
  1078. .resume = efx_io_resume,
  1079. };
  1080. /* Determine whether the NIC will be able to handle TX offloads for a given
  1081. * encapsulated packet.
  1082. */
  1083. static bool efx_can_encap_offloads(struct efx_nic *efx, struct sk_buff *skb)
  1084. {
  1085. struct gre_base_hdr *greh;
  1086. __be16 dst_port;
  1087. u8 ipproto;
  1088. /* Does the NIC support encap offloads?
  1089. * If not, we should never get here, because we shouldn't have
  1090. * advertised encap offload feature flags in the first place.
  1091. */
  1092. if (WARN_ON_ONCE(!efx->type->udp_tnl_has_port))
  1093. return false;
  1094. /* Determine encapsulation protocol in use */
  1095. switch (skb->protocol) {
  1096. case htons(ETH_P_IP):
  1097. ipproto = ip_hdr(skb)->protocol;
  1098. break;
  1099. case htons(ETH_P_IPV6):
  1100. /* If there are extension headers, this will cause us to
  1101. * think we can't offload something that we maybe could have.
  1102. */
  1103. ipproto = ipv6_hdr(skb)->nexthdr;
  1104. break;
  1105. default:
  1106. /* Not IP, so can't offload it */
  1107. return false;
  1108. }
  1109. switch (ipproto) {
  1110. case IPPROTO_GRE:
  1111. /* We support NVGRE but not IP over GRE or random gretaps.
  1112. * Specifically, the NIC will accept GRE as encapsulated if
  1113. * the inner protocol is Ethernet, but only handle it
  1114. * correctly if the GRE header is 8 bytes long. Moreover,
  1115. * it will not update the Checksum or Sequence Number fields
  1116. * if they are present. (The Routing Present flag,
  1117. * GRE_ROUTING, cannot be set else the header would be more
  1118. * than 8 bytes long; so we don't have to worry about it.)
  1119. */
  1120. if (skb->inner_protocol_type != ENCAP_TYPE_ETHER)
  1121. return false;
  1122. if (ntohs(skb->inner_protocol) != ETH_P_TEB)
  1123. return false;
  1124. if (skb_inner_mac_header(skb) - skb_transport_header(skb) != 8)
  1125. return false;
  1126. greh = (struct gre_base_hdr *)skb_transport_header(skb);
  1127. return !(greh->flags & (GRE_CSUM | GRE_SEQ));
  1128. case IPPROTO_UDP:
  1129. /* If the port is registered for a UDP tunnel, we assume the
  1130. * packet is for that tunnel, and the NIC will handle it as
  1131. * such. If not, the NIC won't know what to do with it.
  1132. */
  1133. dst_port = udp_hdr(skb)->dest;
  1134. return efx->type->udp_tnl_has_port(efx, dst_port);
  1135. default:
  1136. return false;
  1137. }
  1138. }
  1139. netdev_features_t efx_features_check(struct sk_buff *skb, struct net_device *dev,
  1140. netdev_features_t features)
  1141. {
  1142. struct efx_nic *efx = efx_netdev_priv(dev);
  1143. if (skb->encapsulation) {
  1144. if (features & NETIF_F_GSO_MASK)
  1145. /* Hardware can only do TSO with at most 208 bytes
  1146. * of headers.
  1147. */
  1148. if (skb_inner_transport_offset(skb) >
  1149. EFX_TSO2_MAX_HDRLEN)
  1150. features &= ~(NETIF_F_GSO_MASK);
  1151. if (features & (NETIF_F_GSO_MASK | NETIF_F_CSUM_MASK))
  1152. if (!efx_can_encap_offloads(efx, skb))
  1153. features &= ~(NETIF_F_GSO_MASK |
  1154. NETIF_F_CSUM_MASK);
  1155. }
  1156. return features;
  1157. }
  1158. int efx_get_phys_port_id(struct net_device *net_dev,
  1159. struct netdev_phys_item_id *ppid)
  1160. {
  1161. struct efx_nic *efx = efx_netdev_priv(net_dev);
  1162. if (efx->type->get_phys_port_id)
  1163. return efx->type->get_phys_port_id(efx, ppid);
  1164. else
  1165. return -EOPNOTSUPP;
  1166. }
  1167. int efx_get_phys_port_name(struct net_device *net_dev, char *name, size_t len)
  1168. {
  1169. struct efx_nic *efx = efx_netdev_priv(net_dev);
  1170. if (snprintf(name, len, "p%u", efx->port_num) >= len)
  1171. return -EINVAL;
  1172. return 0;
  1173. }
  1174. void efx_detach_reps(struct efx_nic *efx)
  1175. {
  1176. struct net_device *rep_dev;
  1177. struct efx_rep *efv;
  1178. ASSERT_RTNL();
  1179. netif_dbg(efx, drv, efx->net_dev, "Detaching VF representors\n");
  1180. list_for_each_entry(efv, &efx->vf_reps, list) {
  1181. rep_dev = efv->net_dev;
  1182. if (!rep_dev)
  1183. continue;
  1184. netif_carrier_off(rep_dev);
  1185. /* See efx_device_detach_sync() */
  1186. netif_tx_lock_bh(rep_dev);
  1187. netif_tx_stop_all_queues(rep_dev);
  1188. netif_tx_unlock_bh(rep_dev);
  1189. }
  1190. }
  1191. void efx_attach_reps(struct efx_nic *efx)
  1192. {
  1193. struct net_device *rep_dev;
  1194. struct efx_rep *efv;
  1195. ASSERT_RTNL();
  1196. netif_dbg(efx, drv, efx->net_dev, "Attaching VF representors\n");
  1197. list_for_each_entry(efv, &efx->vf_reps, list) {
  1198. rep_dev = efv->net_dev;
  1199. if (!rep_dev)
  1200. continue;
  1201. netif_tx_wake_all_queues(rep_dev);
  1202. netif_carrier_on(rep_dev);
  1203. }
  1204. }