efx_common.c 38 KB

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