efx.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2005-2006 Fen Systems Ltd.
  5. * Copyright 2005-2013 Solarflare Communications Inc.
  6. */
  7. #include <linux/filter.h>
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/delay.h>
  13. #include <linux/notifier.h>
  14. #include <linux/ip.h>
  15. #include <linux/tcp.h>
  16. #include <linux/in.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/topology.h>
  19. #include <linux/gfp.h>
  20. #include <linux/interrupt.h>
  21. #include "net_driver.h"
  22. #include <net/gre.h>
  23. #include <net/udp_tunnel.h>
  24. #include <net/netdev_queues.h>
  25. #include "efx.h"
  26. #include "efx_common.h"
  27. #include "efx_channels.h"
  28. #include "ef100.h"
  29. #include "rx_common.h"
  30. #include "tx_common.h"
  31. #include "nic.h"
  32. #include "io.h"
  33. #include "selftest.h"
  34. #include "sriov.h"
  35. #include "efx_devlink.h"
  36. #include "mcdi_port_common.h"
  37. #include "mcdi_pcol.h"
  38. #include "workarounds.h"
  39. /**************************************************************************
  40. *
  41. * Configurable values
  42. *
  43. *************************************************************************/
  44. module_param_named(interrupt_mode, efx_interrupt_mode, uint, 0444);
  45. MODULE_PARM_DESC(interrupt_mode,
  46. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  47. module_param(rss_cpus, uint, 0444);
  48. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  49. /*
  50. * Use separate channels for TX and RX events
  51. *
  52. * Set this to 1 to use separate channels for TX and RX. It allows us
  53. * to control interrupt affinity separately for TX and RX.
  54. *
  55. * This is only used in MSI-X interrupt mode
  56. */
  57. bool efx_separate_tx_channels;
  58. module_param(efx_separate_tx_channels, bool, 0444);
  59. MODULE_PARM_DESC(efx_separate_tx_channels,
  60. "Use separate channels for TX and RX");
  61. /* Initial interrupt moderation settings. They can be modified after
  62. * module load with ethtool.
  63. *
  64. * The default for RX should strike a balance between increasing the
  65. * round-trip latency and reducing overhead.
  66. */
  67. static unsigned int rx_irq_mod_usec = 60;
  68. /* Initial interrupt moderation settings. They can be modified after
  69. * module load with ethtool.
  70. *
  71. * This default is chosen to ensure that a 10G link does not go idle
  72. * while a TX queue is stopped after it has become full. A queue is
  73. * restarted when it drops below half full. The time this takes (assuming
  74. * worst case 3 descriptors per packet and 1024 descriptors) is
  75. * 512 / 3 * 1.2 = 205 usec.
  76. */
  77. static unsigned int tx_irq_mod_usec = 150;
  78. static bool phy_flash_cfg;
  79. module_param(phy_flash_cfg, bool, 0644);
  80. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  81. static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  82. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  83. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  84. NETIF_MSG_TX_ERR | NETIF_MSG_HW);
  85. module_param(debug, uint, 0);
  86. MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
  87. /**************************************************************************
  88. *
  89. * Utility functions and prototypes
  90. *
  91. *************************************************************************/
  92. static void efx_remove_port(struct efx_nic *efx);
  93. static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
  94. static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
  95. static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
  96. u32 flags);
  97. /**************************************************************************
  98. *
  99. * Port handling
  100. *
  101. **************************************************************************/
  102. static void efx_fini_port(struct efx_nic *efx);
  103. static int efx_probe_port(struct efx_nic *efx)
  104. {
  105. int rc;
  106. netif_dbg(efx, probe, efx->net_dev, "create port\n");
  107. if (phy_flash_cfg)
  108. efx->phy_mode = PHY_MODE_SPECIAL;
  109. /* Connect up MAC/PHY operations table */
  110. rc = efx->type->probe_port(efx);
  111. if (rc)
  112. return rc;
  113. /* Initialise MAC address to permanent address */
  114. eth_hw_addr_set(efx->net_dev, efx->net_dev->perm_addr);
  115. return 0;
  116. }
  117. static int efx_init_port(struct efx_nic *efx)
  118. {
  119. int rc;
  120. netif_dbg(efx, drv, efx->net_dev, "init port\n");
  121. mutex_lock(&efx->mac_lock);
  122. efx->port_initialized = true;
  123. /* Ensure the PHY advertises the correct flow control settings */
  124. rc = efx_mcdi_port_reconfigure(efx);
  125. if (rc && rc != -EPERM)
  126. goto fail;
  127. mutex_unlock(&efx->mac_lock);
  128. return 0;
  129. fail:
  130. mutex_unlock(&efx->mac_lock);
  131. return rc;
  132. }
  133. static void efx_fini_port(struct efx_nic *efx)
  134. {
  135. netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
  136. if (!efx->port_initialized)
  137. return;
  138. efx->port_initialized = false;
  139. efx->link_state.up = false;
  140. efx_link_status_changed(efx);
  141. }
  142. static void efx_remove_port(struct efx_nic *efx)
  143. {
  144. netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
  145. efx->type->remove_port(efx);
  146. }
  147. /**************************************************************************
  148. *
  149. * NIC handling
  150. *
  151. **************************************************************************/
  152. static LIST_HEAD(efx_primary_list);
  153. static LIST_HEAD(efx_unassociated_list);
  154. static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
  155. {
  156. return left->type == right->type &&
  157. left->vpd_sn && right->vpd_sn &&
  158. !strcmp(left->vpd_sn, right->vpd_sn);
  159. }
  160. static void efx_associate(struct efx_nic *efx)
  161. {
  162. struct efx_nic *other, *next;
  163. if (efx->primary == efx) {
  164. /* Adding primary function; look for secondaries */
  165. netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
  166. list_add_tail(&efx->node, &efx_primary_list);
  167. list_for_each_entry_safe(other, next, &efx_unassociated_list,
  168. node) {
  169. if (efx_same_controller(efx, other)) {
  170. list_del(&other->node);
  171. netif_dbg(other, probe, other->net_dev,
  172. "moving to secondary list of %s %s\n",
  173. pci_name(efx->pci_dev),
  174. efx->net_dev->name);
  175. list_add_tail(&other->node,
  176. &efx->secondary_list);
  177. other->primary = efx;
  178. }
  179. }
  180. } else {
  181. /* Adding secondary function; look for primary */
  182. list_for_each_entry(other, &efx_primary_list, node) {
  183. if (efx_same_controller(efx, other)) {
  184. netif_dbg(efx, probe, efx->net_dev,
  185. "adding to secondary list of %s %s\n",
  186. pci_name(other->pci_dev),
  187. other->net_dev->name);
  188. list_add_tail(&efx->node,
  189. &other->secondary_list);
  190. efx->primary = other;
  191. return;
  192. }
  193. }
  194. netif_dbg(efx, probe, efx->net_dev,
  195. "adding to unassociated list\n");
  196. list_add_tail(&efx->node, &efx_unassociated_list);
  197. }
  198. }
  199. static void efx_dissociate(struct efx_nic *efx)
  200. {
  201. struct efx_nic *other, *next;
  202. list_del(&efx->node);
  203. efx->primary = NULL;
  204. list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
  205. list_del(&other->node);
  206. netif_dbg(other, probe, other->net_dev,
  207. "moving to unassociated list\n");
  208. list_add_tail(&other->node, &efx_unassociated_list);
  209. other->primary = NULL;
  210. }
  211. }
  212. static int efx_probe_nic(struct efx_nic *efx)
  213. {
  214. int rc;
  215. netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
  216. /* Carry out hardware-type specific initialisation */
  217. rc = efx->type->probe(efx);
  218. if (rc)
  219. return rc;
  220. do {
  221. if (!efx->max_channels || !efx->max_tx_channels) {
  222. netif_err(efx, drv, efx->net_dev,
  223. "Insufficient resources to allocate"
  224. " any channels\n");
  225. rc = -ENOSPC;
  226. goto fail1;
  227. }
  228. /* Determine the number of channels and queues by trying
  229. * to hook in MSI-X interrupts.
  230. */
  231. rc = efx_probe_interrupts(efx);
  232. if (rc)
  233. goto fail1;
  234. rc = efx_set_channels(efx);
  235. if (rc)
  236. goto fail1;
  237. /* dimension_resources can fail with EAGAIN */
  238. rc = efx->type->dimension_resources(efx);
  239. if (rc != 0 && rc != -EAGAIN)
  240. goto fail2;
  241. if (rc == -EAGAIN)
  242. /* try again with new max_channels */
  243. efx_remove_interrupts(efx);
  244. } while (rc == -EAGAIN);
  245. if (efx->n_channels > 1)
  246. netdev_rss_key_fill(efx->rss_context.rx_hash_key,
  247. sizeof(efx->rss_context.rx_hash_key));
  248. efx_set_default_rx_indir_table(efx, efx->rss_context.rx_indir_table);
  249. /* Initialise the interrupt moderation settings */
  250. efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
  251. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
  252. true);
  253. return 0;
  254. fail2:
  255. efx_remove_interrupts(efx);
  256. fail1:
  257. efx->type->remove(efx);
  258. return rc;
  259. }
  260. static void efx_remove_nic(struct efx_nic *efx)
  261. {
  262. netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
  263. efx_remove_interrupts(efx);
  264. efx->type->remove(efx);
  265. }
  266. /**************************************************************************
  267. *
  268. * NIC startup/shutdown
  269. *
  270. *************************************************************************/
  271. static int efx_probe_all(struct efx_nic *efx)
  272. {
  273. int rc;
  274. rc = efx_probe_nic(efx);
  275. if (rc) {
  276. netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
  277. goto fail1;
  278. }
  279. rc = efx_probe_port(efx);
  280. if (rc) {
  281. netif_err(efx, probe, efx->net_dev, "failed to create port\n");
  282. goto fail2;
  283. }
  284. BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
  285. if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
  286. rc = -EINVAL;
  287. goto fail3;
  288. }
  289. #ifdef CONFIG_SFC_SRIOV
  290. rc = efx->type->vswitching_probe(efx);
  291. if (rc) /* not fatal; the PF will still work fine */
  292. netif_warn(efx, probe, efx->net_dev,
  293. "failed to setup vswitching rc=%d;"
  294. " VFs may not function\n", rc);
  295. #endif
  296. rc = efx_probe_filters(efx);
  297. if (rc) {
  298. netif_err(efx, probe, efx->net_dev,
  299. "failed to create filter tables\n");
  300. goto fail4;
  301. }
  302. rc = efx_probe_channels(efx);
  303. if (rc)
  304. goto fail5;
  305. efx->state = STATE_NET_DOWN;
  306. return 0;
  307. fail5:
  308. efx_remove_filters(efx);
  309. fail4:
  310. #ifdef CONFIG_SFC_SRIOV
  311. efx->type->vswitching_remove(efx);
  312. #endif
  313. fail3:
  314. efx_remove_port(efx);
  315. fail2:
  316. efx_remove_nic(efx);
  317. fail1:
  318. return rc;
  319. }
  320. static void efx_remove_all(struct efx_nic *efx)
  321. {
  322. rtnl_lock();
  323. efx_xdp_setup_prog(efx, NULL);
  324. rtnl_unlock();
  325. efx_remove_channels(efx);
  326. efx_remove_filters(efx);
  327. #ifdef CONFIG_SFC_SRIOV
  328. efx->type->vswitching_remove(efx);
  329. #endif
  330. efx_remove_port(efx);
  331. efx_remove_nic(efx);
  332. }
  333. /**************************************************************************
  334. *
  335. * Interrupt moderation
  336. *
  337. **************************************************************************/
  338. unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
  339. {
  340. if (usecs == 0)
  341. return 0;
  342. if (usecs * 1000 < efx->timer_quantum_ns)
  343. return 1; /* never round down to 0 */
  344. return usecs * 1000 / efx->timer_quantum_ns;
  345. }
  346. /* Set interrupt moderation parameters */
  347. int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
  348. unsigned int rx_usecs, bool rx_adaptive,
  349. bool rx_may_override_tx)
  350. {
  351. struct efx_channel *channel;
  352. unsigned int timer_max_us;
  353. EFX_ASSERT_RESET_SERIALISED(efx);
  354. timer_max_us = efx->timer_max_ns / 1000;
  355. if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
  356. return -EINVAL;
  357. if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
  358. !rx_may_override_tx) {
  359. netif_err(efx, drv, efx->net_dev, "Channels are shared. "
  360. "RX and TX IRQ moderation must be equal\n");
  361. return -EINVAL;
  362. }
  363. efx->irq_rx_adaptive = rx_adaptive;
  364. efx->irq_rx_moderation_us = rx_usecs;
  365. efx_for_each_channel(channel, efx) {
  366. if (efx_channel_has_rx_queue(channel))
  367. channel->irq_moderation_us = rx_usecs;
  368. else if (efx_channel_has_tx_queues(channel))
  369. channel->irq_moderation_us = tx_usecs;
  370. else if (efx_channel_is_xdp_tx(channel))
  371. channel->irq_moderation_us = tx_usecs;
  372. }
  373. return 0;
  374. }
  375. void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
  376. unsigned int *rx_usecs, bool *rx_adaptive)
  377. {
  378. *rx_adaptive = efx->irq_rx_adaptive;
  379. *rx_usecs = efx->irq_rx_moderation_us;
  380. /* If channels are shared between RX and TX, so is IRQ
  381. * moderation. Otherwise, IRQ moderation is the same for all
  382. * TX channels and is not adaptive.
  383. */
  384. if (efx->tx_channel_offset == 0) {
  385. *tx_usecs = *rx_usecs;
  386. } else {
  387. struct efx_channel *tx_channel;
  388. tx_channel = efx->channel[efx->tx_channel_offset];
  389. *tx_usecs = tx_channel->irq_moderation_us;
  390. }
  391. }
  392. /**************************************************************************
  393. *
  394. * Kernel net device interface
  395. *
  396. *************************************************************************/
  397. /* Context: process, rtnl_lock() held. */
  398. int efx_net_open(struct net_device *net_dev)
  399. {
  400. struct efx_nic *efx = efx_netdev_priv(net_dev);
  401. int rc;
  402. netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
  403. raw_smp_processor_id());
  404. rc = efx_check_disabled(efx);
  405. if (rc)
  406. return rc;
  407. if (efx->phy_mode & PHY_MODE_SPECIAL)
  408. return -EBUSY;
  409. if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
  410. return -EIO;
  411. /* Notify the kernel of the link state polled during driver load,
  412. * before the monitor starts running */
  413. efx_link_status_changed(efx);
  414. efx_start_all(efx);
  415. if (efx->state == STATE_DISABLED || efx->reset_pending)
  416. netif_device_detach(efx->net_dev);
  417. else
  418. efx->state = STATE_NET_UP;
  419. return 0;
  420. }
  421. /* Context: process, rtnl_lock() held.
  422. * Note that the kernel will ignore our return code; this method
  423. * should really be a void.
  424. */
  425. int efx_net_stop(struct net_device *net_dev)
  426. {
  427. struct efx_nic *efx = efx_netdev_priv(net_dev);
  428. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  429. raw_smp_processor_id());
  430. /* Stop the device and flush all the channels */
  431. efx_stop_all(efx);
  432. return 0;
  433. }
  434. static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
  435. {
  436. struct efx_nic *efx = efx_netdev_priv(net_dev);
  437. if (efx->type->vlan_rx_add_vid)
  438. return efx->type->vlan_rx_add_vid(efx, proto, vid);
  439. else
  440. return -EOPNOTSUPP;
  441. }
  442. static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
  443. {
  444. struct efx_nic *efx = efx_netdev_priv(net_dev);
  445. if (efx->type->vlan_rx_kill_vid)
  446. return efx->type->vlan_rx_kill_vid(efx, proto, vid);
  447. else
  448. return -EOPNOTSUPP;
  449. }
  450. static int efx_hwtstamp_set(struct net_device *net_dev,
  451. struct kernel_hwtstamp_config *config,
  452. struct netlink_ext_ack *extack)
  453. {
  454. struct efx_nic *efx = efx_netdev_priv(net_dev);
  455. return efx_ptp_set_ts_config(efx, config, extack);
  456. }
  457. static int efx_hwtstamp_get(struct net_device *net_dev,
  458. struct kernel_hwtstamp_config *config)
  459. {
  460. struct efx_nic *efx = efx_netdev_priv(net_dev);
  461. return efx_ptp_get_ts_config(efx, config);
  462. }
  463. static const struct net_device_ops efx_netdev_ops = {
  464. .ndo_open = efx_net_open,
  465. .ndo_stop = efx_net_stop,
  466. .ndo_get_stats64 = efx_net_stats,
  467. .ndo_tx_timeout = efx_watchdog,
  468. .ndo_start_xmit = efx_hard_start_xmit,
  469. .ndo_validate_addr = eth_validate_addr,
  470. .ndo_change_mtu = efx_change_mtu,
  471. .ndo_set_mac_address = efx_set_mac_address,
  472. .ndo_set_rx_mode = efx_set_rx_mode,
  473. .ndo_set_features = efx_set_features,
  474. .ndo_features_check = efx_features_check,
  475. .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
  476. .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
  477. .ndo_hwtstamp_set = efx_hwtstamp_set,
  478. .ndo_hwtstamp_get = efx_hwtstamp_get,
  479. #ifdef CONFIG_SFC_SRIOV
  480. .ndo_set_vf_mac = efx_sriov_set_vf_mac,
  481. .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
  482. .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
  483. .ndo_get_vf_config = efx_sriov_get_vf_config,
  484. .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
  485. #endif
  486. .ndo_get_phys_port_id = efx_get_phys_port_id,
  487. .ndo_get_phys_port_name = efx_get_phys_port_name,
  488. #ifdef CONFIG_RFS_ACCEL
  489. .ndo_rx_flow_steer = efx_filter_rfs,
  490. #endif
  491. .ndo_xdp_xmit = efx_xdp_xmit,
  492. .ndo_bpf = efx_xdp
  493. };
  494. static void efx_get_queue_stats_rx(struct net_device *net_dev, int idx,
  495. struct netdev_queue_stats_rx *stats)
  496. {
  497. struct efx_nic *efx = efx_netdev_priv(net_dev);
  498. struct efx_rx_queue *rx_queue;
  499. struct efx_channel *channel;
  500. channel = efx_get_channel(efx, idx);
  501. rx_queue = efx_channel_get_rx_queue(channel);
  502. /* Count only packets since last time datapath was started */
  503. stats->packets = rx_queue->rx_packets - rx_queue->old_rx_packets;
  504. stats->bytes = rx_queue->rx_bytes - rx_queue->old_rx_bytes;
  505. stats->hw_drops = efx_get_queue_stat_rx_hw_drops(channel) -
  506. channel->old_n_rx_hw_drops;
  507. stats->hw_drop_overruns = channel->n_rx_nodesc_trunc -
  508. channel->old_n_rx_hw_drop_overruns;
  509. }
  510. static void efx_get_queue_stats_tx(struct net_device *net_dev, int idx,
  511. struct netdev_queue_stats_tx *stats)
  512. {
  513. struct efx_nic *efx = efx_netdev_priv(net_dev);
  514. struct efx_tx_queue *tx_queue;
  515. struct efx_channel *channel;
  516. channel = efx_get_tx_channel(efx, idx);
  517. stats->packets = 0;
  518. stats->bytes = 0;
  519. stats->hw_gso_packets = 0;
  520. stats->hw_gso_wire_packets = 0;
  521. efx_for_each_channel_tx_queue(tx_queue, channel) {
  522. stats->packets += tx_queue->complete_packets -
  523. tx_queue->old_complete_packets;
  524. stats->bytes += tx_queue->complete_bytes -
  525. tx_queue->old_complete_bytes;
  526. /* Note that, unlike stats->packets and stats->bytes,
  527. * these count TXes enqueued, rather than completed,
  528. * which may not be what users expect.
  529. */
  530. stats->hw_gso_packets += tx_queue->tso_bursts -
  531. tx_queue->old_tso_bursts;
  532. stats->hw_gso_wire_packets += tx_queue->tso_packets -
  533. tx_queue->old_tso_packets;
  534. }
  535. }
  536. static void efx_get_base_stats(struct net_device *net_dev,
  537. struct netdev_queue_stats_rx *rx,
  538. struct netdev_queue_stats_tx *tx)
  539. {
  540. struct efx_nic *efx = efx_netdev_priv(net_dev);
  541. struct efx_tx_queue *tx_queue;
  542. struct efx_rx_queue *rx_queue;
  543. struct efx_channel *channel;
  544. rx->packets = 0;
  545. rx->bytes = 0;
  546. rx->hw_drops = 0;
  547. rx->hw_drop_overruns = 0;
  548. tx->packets = 0;
  549. tx->bytes = 0;
  550. tx->hw_gso_packets = 0;
  551. tx->hw_gso_wire_packets = 0;
  552. /* Count all packets on non-core queues, and packets before last
  553. * datapath start on core queues.
  554. */
  555. efx_for_each_channel(channel, efx) {
  556. rx_queue = efx_channel_get_rx_queue(channel);
  557. if (channel->channel >= net_dev->real_num_rx_queues) {
  558. rx->packets += rx_queue->rx_packets;
  559. rx->bytes += rx_queue->rx_bytes;
  560. rx->hw_drops += efx_get_queue_stat_rx_hw_drops(channel);
  561. rx->hw_drop_overruns += channel->n_rx_nodesc_trunc;
  562. } else {
  563. rx->packets += rx_queue->old_rx_packets;
  564. rx->bytes += rx_queue->old_rx_bytes;
  565. rx->hw_drops += channel->old_n_rx_hw_drops;
  566. rx->hw_drop_overruns += channel->old_n_rx_hw_drop_overruns;
  567. }
  568. efx_for_each_channel_tx_queue(tx_queue, channel) {
  569. if (channel->channel < efx->tx_channel_offset ||
  570. channel->channel >= efx->tx_channel_offset +
  571. net_dev->real_num_tx_queues) {
  572. tx->packets += tx_queue->complete_packets;
  573. tx->bytes += tx_queue->complete_bytes;
  574. tx->hw_gso_packets += tx_queue->tso_bursts;
  575. tx->hw_gso_wire_packets += tx_queue->tso_packets;
  576. } else {
  577. tx->packets += tx_queue->old_complete_packets;
  578. tx->bytes += tx_queue->old_complete_bytes;
  579. tx->hw_gso_packets += tx_queue->old_tso_bursts;
  580. tx->hw_gso_wire_packets += tx_queue->old_tso_packets;
  581. }
  582. /* Include XDP TX in device-wide stats */
  583. tx->packets += tx_queue->complete_xdp_packets;
  584. tx->bytes += tx_queue->complete_xdp_bytes;
  585. }
  586. }
  587. }
  588. static const struct netdev_stat_ops efx_stat_ops = {
  589. .get_queue_stats_rx = efx_get_queue_stats_rx,
  590. .get_queue_stats_tx = efx_get_queue_stats_tx,
  591. .get_base_stats = efx_get_base_stats,
  592. };
  593. static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
  594. {
  595. struct bpf_prog *old_prog;
  596. if (efx->xdp_rxq_info_failed) {
  597. netif_err(efx, drv, efx->net_dev,
  598. "Unable to bind XDP program due to previous failure of rxq_info\n");
  599. return -EINVAL;
  600. }
  601. if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
  602. netif_err(efx, drv, efx->net_dev,
  603. "Unable to configure XDP with MTU of %d (max: %d)\n",
  604. efx->net_dev->mtu, efx_xdp_max_mtu(efx));
  605. return -EINVAL;
  606. }
  607. old_prog = rtnl_dereference(efx->xdp_prog);
  608. rcu_assign_pointer(efx->xdp_prog, prog);
  609. /* Release the reference that was originally passed by the caller. */
  610. if (old_prog)
  611. bpf_prog_put(old_prog);
  612. return 0;
  613. }
  614. /* Context: process, rtnl_lock() held. */
  615. static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  616. {
  617. struct efx_nic *efx = efx_netdev_priv(dev);
  618. switch (xdp->command) {
  619. case XDP_SETUP_PROG:
  620. return efx_xdp_setup_prog(efx, xdp->prog);
  621. default:
  622. return -EINVAL;
  623. }
  624. }
  625. static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
  626. u32 flags)
  627. {
  628. struct efx_nic *efx = efx_netdev_priv(dev);
  629. if (!netif_running(dev))
  630. return -EINVAL;
  631. return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
  632. }
  633. static void efx_update_name(struct efx_nic *efx)
  634. {
  635. strcpy(efx->name, efx->net_dev->name);
  636. efx_mtd_rename(efx);
  637. efx_set_channel_names(efx);
  638. }
  639. static int efx_netdev_event(struct notifier_block *this,
  640. unsigned long event, void *ptr)
  641. {
  642. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  643. if ((net_dev->netdev_ops == &efx_netdev_ops) &&
  644. event == NETDEV_CHANGENAME)
  645. efx_update_name(efx_netdev_priv(net_dev));
  646. return NOTIFY_DONE;
  647. }
  648. static struct notifier_block efx_netdev_notifier = {
  649. .notifier_call = efx_netdev_event,
  650. };
  651. static ssize_t phy_type_show(struct device *dev,
  652. struct device_attribute *attr, char *buf)
  653. {
  654. struct efx_nic *efx = dev_get_drvdata(dev);
  655. return sprintf(buf, "%d\n", efx->phy_type);
  656. }
  657. static DEVICE_ATTR_RO(phy_type);
  658. static int efx_register_netdev(struct efx_nic *efx)
  659. {
  660. struct net_device *net_dev = efx->net_dev;
  661. struct efx_channel *channel;
  662. int rc;
  663. net_dev->watchdog_timeo = 5 * HZ;
  664. net_dev->irq = efx->pci_dev->irq;
  665. net_dev->netdev_ops = &efx_netdev_ops;
  666. net_dev->stat_ops = &efx_stat_ops;
  667. if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
  668. net_dev->priv_flags |= IFF_UNICAST_FLT;
  669. net_dev->ethtool_ops = &efx_ethtool_ops;
  670. netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS);
  671. net_dev->min_mtu = EFX_MIN_MTU;
  672. net_dev->max_mtu = EFX_MAX_MTU;
  673. rtnl_lock();
  674. /* Enable resets to be scheduled and check whether any were
  675. * already requested. If so, the NIC is probably hosed so we
  676. * abort.
  677. */
  678. if (efx->reset_pending) {
  679. pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
  680. rc = -EIO;
  681. goto fail_locked;
  682. }
  683. rc = dev_alloc_name(net_dev, net_dev->name);
  684. if (rc < 0)
  685. goto fail_locked;
  686. efx_update_name(efx);
  687. /* Always start with carrier off; PHY events will detect the link */
  688. netif_carrier_off(net_dev);
  689. rc = register_netdevice(net_dev);
  690. if (rc)
  691. goto fail_locked;
  692. efx_for_each_channel(channel, efx) {
  693. struct efx_tx_queue *tx_queue;
  694. efx_for_each_channel_tx_queue(tx_queue, channel)
  695. efx_init_tx_queue_core_txq(tx_queue);
  696. }
  697. efx_associate(efx);
  698. efx->state = STATE_NET_DOWN;
  699. rtnl_unlock();
  700. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  701. if (rc) {
  702. netif_err(efx, drv, efx->net_dev,
  703. "failed to init net dev attributes\n");
  704. goto fail_registered;
  705. }
  706. efx_init_mcdi_logging(efx);
  707. return 0;
  708. fail_registered:
  709. rtnl_lock();
  710. efx_dissociate(efx);
  711. unregister_netdevice(net_dev);
  712. fail_locked:
  713. efx->state = STATE_UNINIT;
  714. rtnl_unlock();
  715. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  716. return rc;
  717. }
  718. static void efx_unregister_netdev(struct efx_nic *efx)
  719. {
  720. if (!efx->net_dev)
  721. return;
  722. if (WARN_ON(efx_netdev_priv(efx->net_dev) != efx))
  723. return;
  724. if (efx_dev_registered(efx)) {
  725. strscpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  726. efx_fini_mcdi_logging(efx);
  727. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  728. unregister_netdev(efx->net_dev);
  729. }
  730. }
  731. /**************************************************************************
  732. *
  733. * List of NICs we support
  734. *
  735. **************************************************************************/
  736. /* PCI device ID table */
  737. static const struct pci_device_id efx_pci_table[] = {
  738. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
  739. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  740. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
  741. .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
  742. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
  743. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  744. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
  745. .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
  746. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
  747. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  748. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
  749. .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
  750. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
  751. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  752. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
  753. .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
  754. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0c03), /* X4 PF (FF/LL) */
  755. .driver_data = (unsigned long)&efx_x4_nic_type},
  756. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x2c03), /* X4 PF (FF only) */
  757. .driver_data = (unsigned long)&efx_x4_nic_type},
  758. {0} /* end of list */
  759. };
  760. /**************************************************************************
  761. *
  762. * Data housekeeping
  763. *
  764. **************************************************************************/
  765. void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
  766. {
  767. u64 n_rx_nodesc_trunc = 0;
  768. struct efx_channel *channel;
  769. efx_for_each_channel(channel, efx)
  770. n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
  771. stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
  772. stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
  773. }
  774. /**************************************************************************
  775. *
  776. * PCI interface
  777. *
  778. **************************************************************************/
  779. /* Main body of final NIC shutdown code
  780. * This is called only at module unload (or hotplug removal).
  781. */
  782. static void efx_pci_remove_main(struct efx_nic *efx)
  783. {
  784. /* Flush reset_work. It can no longer be scheduled since we
  785. * are not READY.
  786. */
  787. WARN_ON(efx_net_active(efx->state));
  788. efx_flush_reset_workqueue(efx);
  789. efx_disable_interrupts(efx);
  790. efx_clear_interrupt_affinity(efx);
  791. efx_nic_fini_interrupt(efx);
  792. efx_fini_port(efx);
  793. efx->type->fini(efx);
  794. efx_fini_napi(efx);
  795. efx_remove_all(efx);
  796. }
  797. /* Final NIC shutdown
  798. * This is called only at module unload (or hotplug removal). A PF can call
  799. * this on its VFs to ensure they are unbound first.
  800. */
  801. static void efx_pci_remove(struct pci_dev *pci_dev)
  802. {
  803. struct efx_probe_data *probe_data;
  804. struct efx_nic *efx;
  805. efx = pci_get_drvdata(pci_dev);
  806. if (!efx)
  807. return;
  808. /* Mark the NIC as fini, then stop the interface */
  809. rtnl_lock();
  810. efx_dissociate(efx);
  811. dev_close(efx->net_dev);
  812. efx_disable_interrupts(efx);
  813. efx->state = STATE_UNINIT;
  814. rtnl_unlock();
  815. if (efx->type->sriov_fini)
  816. efx->type->sriov_fini(efx);
  817. efx_fini_devlink_lock(efx);
  818. efx_unregister_netdev(efx);
  819. efx_mtd_remove(efx);
  820. efx_pci_remove_main(efx);
  821. efx_fini_io(efx);
  822. pci_dbg(efx->pci_dev, "shutdown successful\n");
  823. efx_fini_devlink_and_unlock(efx);
  824. efx_fini_struct(efx);
  825. free_netdev(efx->net_dev);
  826. probe_data = container_of(efx, struct efx_probe_data, efx);
  827. kfree(probe_data);
  828. };
  829. /* NIC VPD information
  830. * Called during probe to display the part number of the
  831. * installed NIC.
  832. */
  833. static void efx_probe_vpd_strings(struct efx_nic *efx)
  834. {
  835. struct pci_dev *dev = efx->pci_dev;
  836. unsigned int vpd_size, kw_len;
  837. u8 *vpd_data;
  838. int start;
  839. vpd_data = pci_vpd_alloc(dev, &vpd_size);
  840. if (IS_ERR(vpd_data)) {
  841. pci_warn(dev, "Unable to read VPD\n");
  842. return;
  843. }
  844. start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
  845. PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
  846. if (start < 0)
  847. pci_err(dev, "Part number not found or incomplete\n");
  848. else
  849. pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
  850. start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
  851. PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
  852. if (start < 0)
  853. pci_err(dev, "Serial number not found or incomplete\n");
  854. else
  855. efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL);
  856. kfree(vpd_data);
  857. }
  858. /* Main body of NIC initialisation
  859. * This is called at module load (or hotplug insertion, theoretically).
  860. */
  861. static int efx_pci_probe_main(struct efx_nic *efx)
  862. {
  863. int rc;
  864. /* Do start-of-day initialisation */
  865. rc = efx_probe_all(efx);
  866. if (rc)
  867. goto fail1;
  868. efx_init_napi(efx);
  869. down_write(&efx->filter_sem);
  870. rc = efx->type->init(efx);
  871. up_write(&efx->filter_sem);
  872. if (rc) {
  873. pci_err(efx->pci_dev, "failed to initialise NIC\n");
  874. goto fail3;
  875. }
  876. rc = efx_init_port(efx);
  877. if (rc) {
  878. netif_err(efx, probe, efx->net_dev,
  879. "failed to initialise port\n");
  880. goto fail4;
  881. }
  882. rc = efx_nic_init_interrupt(efx);
  883. if (rc)
  884. goto fail5;
  885. efx_set_interrupt_affinity(efx);
  886. rc = efx_enable_interrupts(efx);
  887. if (rc)
  888. goto fail6;
  889. return 0;
  890. fail6:
  891. efx_clear_interrupt_affinity(efx);
  892. efx_nic_fini_interrupt(efx);
  893. fail5:
  894. efx_fini_port(efx);
  895. fail4:
  896. efx->type->fini(efx);
  897. fail3:
  898. efx_fini_napi(efx);
  899. efx_remove_all(efx);
  900. fail1:
  901. return rc;
  902. }
  903. static int efx_pci_probe_post_io(struct efx_nic *efx)
  904. {
  905. struct net_device *net_dev = efx->net_dev;
  906. int rc = efx_pci_probe_main(efx);
  907. if (rc)
  908. return rc;
  909. if (efx->type->sriov_init) {
  910. rc = efx->type->sriov_init(efx);
  911. if (rc)
  912. pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
  913. rc);
  914. }
  915. /* Determine netdevice features */
  916. net_dev->features |= efx->type->offload_features;
  917. /* Add TSO features */
  918. if (efx->type->tso_versions && efx->type->tso_versions(efx))
  919. net_dev->features |= NETIF_F_TSO | NETIF_F_TSO6;
  920. /* Mask for features that also apply to VLAN devices */
  921. net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
  922. NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
  923. NETIF_F_RXCSUM);
  924. /* Determine user configurable features */
  925. net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
  926. /* Disable receiving frames with bad FCS, by default. */
  927. net_dev->features &= ~NETIF_F_RXALL;
  928. /* Disable VLAN filtering by default. It may be enforced if
  929. * the feature is fixed (i.e. VLAN filters are required to
  930. * receive VLAN tagged packets due to vPort restrictions).
  931. */
  932. net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
  933. net_dev->features |= efx->fixed_features;
  934. net_dev->xdp_features = NETDEV_XDP_ACT_BASIC |
  935. NETDEV_XDP_ACT_REDIRECT |
  936. NETDEV_XDP_ACT_NDO_XMIT;
  937. /* devlink creation, registration and lock */
  938. rc = efx_probe_devlink_and_lock(efx);
  939. if (rc)
  940. pci_err(efx->pci_dev, "devlink registration failed");
  941. rc = efx_register_netdev(efx);
  942. efx_probe_devlink_unlock(efx);
  943. if (!rc)
  944. return 0;
  945. efx_pci_remove_main(efx);
  946. return rc;
  947. }
  948. /* NIC initialisation
  949. *
  950. * This is called at module load (or hotplug insertion,
  951. * theoretically). It sets up PCI mappings, resets the NIC,
  952. * sets up and registers the network devices with the kernel and hooks
  953. * the interrupt service routine. It does not prepare the device for
  954. * transmission; this is left to the first time one of the network
  955. * interfaces is brought up (i.e. efx_net_open).
  956. */
  957. static int efx_pci_probe(struct pci_dev *pci_dev,
  958. const struct pci_device_id *entry)
  959. {
  960. struct efx_probe_data *probe_data, **probe_ptr;
  961. struct net_device *net_dev;
  962. struct efx_nic *efx;
  963. int rc;
  964. /* Allocate probe data and struct efx_nic */
  965. probe_data = kzalloc_obj(*probe_data);
  966. if (!probe_data)
  967. return -ENOMEM;
  968. probe_data->pci_dev = pci_dev;
  969. efx = &probe_data->efx;
  970. /* Allocate and initialise a struct net_device */
  971. net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES);
  972. if (!net_dev) {
  973. rc = -ENOMEM;
  974. goto fail0;
  975. }
  976. probe_ptr = netdev_priv(net_dev);
  977. *probe_ptr = probe_data;
  978. efx->net_dev = net_dev;
  979. efx->type = (const struct efx_nic_type *) entry->driver_data;
  980. efx->fixed_features |= NETIF_F_HIGHDMA;
  981. pci_set_drvdata(pci_dev, efx);
  982. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  983. rc = efx_init_struct(efx, pci_dev);
  984. if (rc)
  985. goto fail1;
  986. pci_info(pci_dev, "Solarflare NIC detected\n");
  987. if (!efx->type->is_vf)
  988. efx_probe_vpd_strings(efx);
  989. /* Set up basic I/O (BAR mappings etc) */
  990. rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
  991. efx->type->mem_map_size(efx));
  992. if (rc)
  993. goto fail2;
  994. rc = efx_pci_probe_post_io(efx);
  995. if (rc) {
  996. /* On failure, retry once immediately.
  997. * If we aborted probe due to a scheduled reset, dismiss it.
  998. */
  999. efx->reset_pending = 0;
  1000. rc = efx_pci_probe_post_io(efx);
  1001. if (rc) {
  1002. /* On another failure, retry once more
  1003. * after a 50-305ms delay.
  1004. */
  1005. unsigned char r;
  1006. get_random_bytes(&r, 1);
  1007. msleep((unsigned int)r + 50);
  1008. efx->reset_pending = 0;
  1009. rc = efx_pci_probe_post_io(efx);
  1010. }
  1011. }
  1012. if (rc)
  1013. goto fail3;
  1014. netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
  1015. /* Try to create MTDs, but allow this to fail */
  1016. rtnl_lock();
  1017. rc = efx_mtd_probe(efx);
  1018. rtnl_unlock();
  1019. if (rc && rc != -EPERM)
  1020. netif_warn(efx, probe, efx->net_dev,
  1021. "failed to create MTDs (%d)\n", rc);
  1022. if (efx->type->udp_tnl_push_ports)
  1023. efx->type->udp_tnl_push_ports(efx);
  1024. return 0;
  1025. fail3:
  1026. efx_fini_io(efx);
  1027. fail2:
  1028. efx_fini_struct(efx);
  1029. fail1:
  1030. WARN_ON(rc > 0);
  1031. netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
  1032. free_netdev(net_dev);
  1033. fail0:
  1034. kfree(probe_data);
  1035. return rc;
  1036. }
  1037. /* efx_pci_sriov_configure returns the actual number of Virtual Functions
  1038. * enabled on success
  1039. */
  1040. #ifdef CONFIG_SFC_SRIOV
  1041. static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
  1042. {
  1043. int rc;
  1044. struct efx_nic *efx = pci_get_drvdata(dev);
  1045. if (efx->type->sriov_configure) {
  1046. rc = efx->type->sriov_configure(efx, num_vfs);
  1047. if (rc)
  1048. return rc;
  1049. else
  1050. return num_vfs;
  1051. } else
  1052. return -EOPNOTSUPP;
  1053. }
  1054. #endif
  1055. static int efx_pm_freeze(struct device *dev)
  1056. {
  1057. struct efx_nic *efx = dev_get_drvdata(dev);
  1058. rtnl_lock();
  1059. if (efx_net_active(efx->state)) {
  1060. efx_device_detach_sync(efx);
  1061. efx_stop_all(efx);
  1062. efx_disable_interrupts(efx);
  1063. efx->state = efx_freeze(efx->state);
  1064. }
  1065. rtnl_unlock();
  1066. return 0;
  1067. }
  1068. static void efx_pci_shutdown(struct pci_dev *pci_dev)
  1069. {
  1070. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  1071. if (!efx)
  1072. return;
  1073. efx_pm_freeze(&pci_dev->dev);
  1074. pci_disable_device(pci_dev);
  1075. }
  1076. static int efx_pm_thaw(struct device *dev)
  1077. {
  1078. int rc;
  1079. struct efx_nic *efx = dev_get_drvdata(dev);
  1080. rtnl_lock();
  1081. if (efx_frozen(efx->state)) {
  1082. rc = efx_enable_interrupts(efx);
  1083. if (rc)
  1084. goto fail;
  1085. mutex_lock(&efx->mac_lock);
  1086. efx_mcdi_port_reconfigure(efx);
  1087. mutex_unlock(&efx->mac_lock);
  1088. efx_start_all(efx);
  1089. efx_device_attach_if_not_resetting(efx);
  1090. efx->state = efx_thaw(efx->state);
  1091. efx->type->resume_wol(efx);
  1092. }
  1093. rtnl_unlock();
  1094. /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
  1095. efx_queue_reset_work(efx);
  1096. return 0;
  1097. fail:
  1098. rtnl_unlock();
  1099. return rc;
  1100. }
  1101. static int efx_pm_poweroff(struct device *dev)
  1102. {
  1103. struct pci_dev *pci_dev = to_pci_dev(dev);
  1104. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  1105. efx->type->fini(efx);
  1106. efx->reset_pending = 0;
  1107. pci_save_state(pci_dev);
  1108. return pci_set_power_state(pci_dev, PCI_D3hot);
  1109. }
  1110. /* Used for both resume and restore */
  1111. static int efx_pm_resume(struct device *dev)
  1112. {
  1113. struct pci_dev *pci_dev = to_pci_dev(dev);
  1114. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  1115. int rc;
  1116. rc = pci_set_power_state(pci_dev, PCI_D0);
  1117. if (rc)
  1118. return rc;
  1119. pci_restore_state(pci_dev);
  1120. rc = pci_enable_device(pci_dev);
  1121. if (rc)
  1122. return rc;
  1123. pci_set_master(efx->pci_dev);
  1124. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  1125. if (rc)
  1126. return rc;
  1127. down_write(&efx->filter_sem);
  1128. rc = efx->type->init(efx);
  1129. up_write(&efx->filter_sem);
  1130. if (rc)
  1131. return rc;
  1132. rc = efx_pm_thaw(dev);
  1133. return rc;
  1134. }
  1135. static int efx_pm_suspend(struct device *dev)
  1136. {
  1137. int rc;
  1138. efx_pm_freeze(dev);
  1139. rc = efx_pm_poweroff(dev);
  1140. if (rc)
  1141. efx_pm_resume(dev);
  1142. return rc;
  1143. }
  1144. static const struct dev_pm_ops efx_pm_ops = {
  1145. .suspend = efx_pm_suspend,
  1146. .resume = efx_pm_resume,
  1147. .freeze = efx_pm_freeze,
  1148. .thaw = efx_pm_thaw,
  1149. .poweroff = efx_pm_poweroff,
  1150. .restore = efx_pm_resume,
  1151. };
  1152. static struct pci_driver efx_pci_driver = {
  1153. .name = KBUILD_MODNAME,
  1154. .id_table = efx_pci_table,
  1155. .probe = efx_pci_probe,
  1156. .remove = efx_pci_remove,
  1157. .driver.pm = &efx_pm_ops,
  1158. .shutdown = efx_pci_shutdown,
  1159. .err_handler = &efx_err_handlers,
  1160. #ifdef CONFIG_SFC_SRIOV
  1161. .sriov_configure = efx_pci_sriov_configure,
  1162. #endif
  1163. };
  1164. /**************************************************************************
  1165. *
  1166. * Kernel module interface
  1167. *
  1168. *************************************************************************/
  1169. static int __init efx_init_module(void)
  1170. {
  1171. int rc;
  1172. printk(KERN_INFO "Solarflare NET driver\n");
  1173. rc = register_netdevice_notifier(&efx_netdev_notifier);
  1174. if (rc)
  1175. goto err_notifier;
  1176. rc = efx_create_reset_workqueue();
  1177. if (rc)
  1178. goto err_reset;
  1179. rc = pci_register_driver(&efx_pci_driver);
  1180. if (rc < 0)
  1181. goto err_pci;
  1182. rc = pci_register_driver(&ef100_pci_driver);
  1183. if (rc < 0)
  1184. goto err_pci_ef100;
  1185. return 0;
  1186. err_pci_ef100:
  1187. pci_unregister_driver(&efx_pci_driver);
  1188. err_pci:
  1189. efx_destroy_reset_workqueue();
  1190. err_reset:
  1191. unregister_netdevice_notifier(&efx_netdev_notifier);
  1192. err_notifier:
  1193. return rc;
  1194. }
  1195. static void __exit efx_exit_module(void)
  1196. {
  1197. printk(KERN_INFO "Solarflare NET driver unloading\n");
  1198. pci_unregister_driver(&ef100_pci_driver);
  1199. pci_unregister_driver(&efx_pci_driver);
  1200. efx_destroy_reset_workqueue();
  1201. unregister_netdevice_notifier(&efx_netdev_notifier);
  1202. }
  1203. module_init(efx_init_module);
  1204. module_exit(efx_exit_module);
  1205. MODULE_AUTHOR("Solarflare Communications and "
  1206. "Michael Brown <mbrown@fensystems.co.uk>");
  1207. MODULE_DESCRIPTION("Solarflare network driver");
  1208. MODULE_LICENSE("GPL");
  1209. MODULE_DEVICE_TABLE(pci, efx_pci_table);