efx_common.c 39 KB

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