efx.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2008 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 <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/delay.h>
  15. #include <linux/notifier.h>
  16. #include <linux/ip.h>
  17. #include <linux/tcp.h>
  18. #include <linux/in.h>
  19. #include <linux/crc32.h>
  20. #include <linux/ethtool.h>
  21. #include <linux/topology.h>
  22. #include "net_driver.h"
  23. #include "ethtool.h"
  24. #include "tx.h"
  25. #include "rx.h"
  26. #include "efx.h"
  27. #include "mdio_10g.h"
  28. #include "falcon.h"
  29. #include "mac.h"
  30. #define EFX_MAX_MTU (9 * 1024)
  31. /* RX slow fill workqueue. If memory allocation fails in the fast path,
  32. * a work item is pushed onto this work queue to retry the allocation later,
  33. * to avoid the NIC being starved of RX buffers. Since this is a per cpu
  34. * workqueue, there is nothing to be gained in making it per NIC
  35. */
  36. static struct workqueue_struct *refill_workqueue;
  37. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  38. * queued onto this work queue. This is not a per-nic work queue, because
  39. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  40. */
  41. static struct workqueue_struct *reset_workqueue;
  42. /**************************************************************************
  43. *
  44. * Configurable values
  45. *
  46. *************************************************************************/
  47. /*
  48. * Enable large receive offload (LRO) aka soft segment reassembly (SSR)
  49. *
  50. * This sets the default for new devices. It can be controlled later
  51. * using ethtool.
  52. */
  53. static int lro = true;
  54. module_param(lro, int, 0644);
  55. MODULE_PARM_DESC(lro, "Large receive offload acceleration");
  56. /*
  57. * Use separate channels for TX and RX events
  58. *
  59. * Set this to 1 to use separate channels for TX and RX. It allows us
  60. * to control interrupt affinity separately for TX and RX.
  61. *
  62. * This is only used in MSI-X interrupt mode
  63. */
  64. static unsigned int separate_tx_channels;
  65. module_param(separate_tx_channels, uint, 0644);
  66. MODULE_PARM_DESC(separate_tx_channels,
  67. "Use separate channels for TX and RX");
  68. /* This is the weight assigned to each of the (per-channel) virtual
  69. * NAPI devices.
  70. */
  71. static int napi_weight = 64;
  72. /* This is the time (in jiffies) between invocations of the hardware
  73. * monitor, which checks for known hardware bugs and resets the
  74. * hardware and driver as necessary.
  75. */
  76. unsigned int efx_monitor_interval = 1 * HZ;
  77. /* This controls whether or not the driver will initialise devices
  78. * with invalid MAC addresses stored in the EEPROM or flash. If true,
  79. * such devices will be initialised with a random locally-generated
  80. * MAC address. This allows for loading the sfc_mtd driver to
  81. * reprogram the flash, even if the flash contents (including the MAC
  82. * address) have previously been erased.
  83. */
  84. static unsigned int allow_bad_hwaddr;
  85. /* Initial interrupt moderation settings. They can be modified after
  86. * module load with ethtool.
  87. *
  88. * The default for RX should strike a balance between increasing the
  89. * round-trip latency and reducing overhead.
  90. */
  91. static unsigned int rx_irq_mod_usec = 60;
  92. /* Initial interrupt moderation settings. They can be modified after
  93. * module load with ethtool.
  94. *
  95. * This default is chosen to ensure that a 10G link does not go idle
  96. * while a TX queue is stopped after it has become full. A queue is
  97. * restarted when it drops below half full. The time this takes (assuming
  98. * worst case 3 descriptors per packet and 1024 descriptors) is
  99. * 512 / 3 * 1.2 = 205 usec.
  100. */
  101. static unsigned int tx_irq_mod_usec = 150;
  102. /* This is the first interrupt mode to try out of:
  103. * 0 => MSI-X
  104. * 1 => MSI
  105. * 2 => legacy
  106. */
  107. static unsigned int interrupt_mode;
  108. /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
  109. * i.e. the number of CPUs among which we may distribute simultaneous
  110. * interrupt handling.
  111. *
  112. * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
  113. * The default (0) means to assign an interrupt to each package (level II cache)
  114. */
  115. static unsigned int rss_cpus;
  116. module_param(rss_cpus, uint, 0444);
  117. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  118. static int phy_flash_cfg;
  119. module_param(phy_flash_cfg, int, 0644);
  120. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  121. /**************************************************************************
  122. *
  123. * Utility functions and prototypes
  124. *
  125. *************************************************************************/
  126. static void efx_remove_channel(struct efx_channel *channel);
  127. static void efx_remove_port(struct efx_nic *efx);
  128. static void efx_fini_napi(struct efx_nic *efx);
  129. static void efx_fini_channels(struct efx_nic *efx);
  130. #define EFX_ASSERT_RESET_SERIALISED(efx) \
  131. do { \
  132. if (efx->state == STATE_RUNNING) \
  133. ASSERT_RTNL(); \
  134. } while (0)
  135. /**************************************************************************
  136. *
  137. * Event queue processing
  138. *
  139. *************************************************************************/
  140. /* Process channel's event queue
  141. *
  142. * This function is responsible for processing the event queue of a
  143. * single channel. The caller must guarantee that this function will
  144. * never be concurrently called more than once on the same channel,
  145. * though different channels may be being processed concurrently.
  146. */
  147. static int efx_process_channel(struct efx_channel *channel, int rx_quota)
  148. {
  149. struct efx_nic *efx = channel->efx;
  150. int rx_packets;
  151. if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
  152. !channel->enabled))
  153. return 0;
  154. rx_packets = falcon_process_eventq(channel, rx_quota);
  155. if (rx_packets == 0)
  156. return 0;
  157. /* Deliver last RX packet. */
  158. if (channel->rx_pkt) {
  159. __efx_rx_packet(channel, channel->rx_pkt,
  160. channel->rx_pkt_csummed);
  161. channel->rx_pkt = NULL;
  162. }
  163. efx_flush_lro(channel);
  164. efx_rx_strategy(channel);
  165. efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
  166. return rx_packets;
  167. }
  168. /* Mark channel as finished processing
  169. *
  170. * Note that since we will not receive further interrupts for this
  171. * channel before we finish processing and call the eventq_read_ack()
  172. * method, there is no need to use the interrupt hold-off timers.
  173. */
  174. static inline void efx_channel_processed(struct efx_channel *channel)
  175. {
  176. /* The interrupt handler for this channel may set work_pending
  177. * as soon as we acknowledge the events we've seen. Make sure
  178. * it's cleared before then. */
  179. channel->work_pending = false;
  180. smp_wmb();
  181. falcon_eventq_read_ack(channel);
  182. }
  183. /* NAPI poll handler
  184. *
  185. * NAPI guarantees serialisation of polls of the same device, which
  186. * provides the guarantee required by efx_process_channel().
  187. */
  188. static int efx_poll(struct napi_struct *napi, int budget)
  189. {
  190. struct efx_channel *channel =
  191. container_of(napi, struct efx_channel, napi_str);
  192. struct net_device *napi_dev = channel->napi_dev;
  193. int rx_packets;
  194. EFX_TRACE(channel->efx, "channel %d NAPI poll executing on CPU %d\n",
  195. channel->channel, raw_smp_processor_id());
  196. rx_packets = efx_process_channel(channel, budget);
  197. if (rx_packets < budget) {
  198. /* There is no race here; although napi_disable() will
  199. * only wait for netif_rx_complete(), this isn't a problem
  200. * since efx_channel_processed() will have no effect if
  201. * interrupts have already been disabled.
  202. */
  203. netif_rx_complete(napi_dev, napi);
  204. efx_channel_processed(channel);
  205. }
  206. return rx_packets;
  207. }
  208. /* Process the eventq of the specified channel immediately on this CPU
  209. *
  210. * Disable hardware generated interrupts, wait for any existing
  211. * processing to finish, then directly poll (and ack ) the eventq.
  212. * Finally reenable NAPI and interrupts.
  213. *
  214. * Since we are touching interrupts the caller should hold the suspend lock
  215. */
  216. void efx_process_channel_now(struct efx_channel *channel)
  217. {
  218. struct efx_nic *efx = channel->efx;
  219. BUG_ON(!channel->used_flags);
  220. BUG_ON(!channel->enabled);
  221. /* Disable interrupts and wait for ISRs to complete */
  222. falcon_disable_interrupts(efx);
  223. if (efx->legacy_irq)
  224. synchronize_irq(efx->legacy_irq);
  225. if (channel->irq)
  226. synchronize_irq(channel->irq);
  227. /* Wait for any NAPI processing to complete */
  228. napi_disable(&channel->napi_str);
  229. /* Poll the channel */
  230. efx_process_channel(channel, efx->type->evq_size);
  231. /* Ack the eventq. This may cause an interrupt to be generated
  232. * when they are reenabled */
  233. efx_channel_processed(channel);
  234. napi_enable(&channel->napi_str);
  235. falcon_enable_interrupts(efx);
  236. }
  237. /* Create event queue
  238. * Event queue memory allocations are done only once. If the channel
  239. * is reset, the memory buffer will be reused; this guards against
  240. * errors during channel reset and also simplifies interrupt handling.
  241. */
  242. static int efx_probe_eventq(struct efx_channel *channel)
  243. {
  244. EFX_LOG(channel->efx, "chan %d create event queue\n", channel->channel);
  245. return falcon_probe_eventq(channel);
  246. }
  247. /* Prepare channel's event queue */
  248. static void efx_init_eventq(struct efx_channel *channel)
  249. {
  250. EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
  251. channel->eventq_read_ptr = 0;
  252. falcon_init_eventq(channel);
  253. }
  254. static void efx_fini_eventq(struct efx_channel *channel)
  255. {
  256. EFX_LOG(channel->efx, "chan %d fini event queue\n", channel->channel);
  257. falcon_fini_eventq(channel);
  258. }
  259. static void efx_remove_eventq(struct efx_channel *channel)
  260. {
  261. EFX_LOG(channel->efx, "chan %d remove event queue\n", channel->channel);
  262. falcon_remove_eventq(channel);
  263. }
  264. /**************************************************************************
  265. *
  266. * Channel handling
  267. *
  268. *************************************************************************/
  269. static int efx_probe_channel(struct efx_channel *channel)
  270. {
  271. struct efx_tx_queue *tx_queue;
  272. struct efx_rx_queue *rx_queue;
  273. int rc;
  274. EFX_LOG(channel->efx, "creating channel %d\n", channel->channel);
  275. rc = efx_probe_eventq(channel);
  276. if (rc)
  277. goto fail1;
  278. efx_for_each_channel_tx_queue(tx_queue, channel) {
  279. rc = efx_probe_tx_queue(tx_queue);
  280. if (rc)
  281. goto fail2;
  282. }
  283. efx_for_each_channel_rx_queue(rx_queue, channel) {
  284. rc = efx_probe_rx_queue(rx_queue);
  285. if (rc)
  286. goto fail3;
  287. }
  288. channel->n_rx_frm_trunc = 0;
  289. return 0;
  290. fail3:
  291. efx_for_each_channel_rx_queue(rx_queue, channel)
  292. efx_remove_rx_queue(rx_queue);
  293. fail2:
  294. efx_for_each_channel_tx_queue(tx_queue, channel)
  295. efx_remove_tx_queue(tx_queue);
  296. fail1:
  297. return rc;
  298. }
  299. static void efx_set_channel_names(struct efx_nic *efx)
  300. {
  301. struct efx_channel *channel;
  302. const char *type = "";
  303. int number;
  304. efx_for_each_channel(channel, efx) {
  305. number = channel->channel;
  306. if (efx->n_channels > efx->n_rx_queues) {
  307. if (channel->channel < efx->n_rx_queues) {
  308. type = "-rx";
  309. } else {
  310. type = "-tx";
  311. number -= efx->n_rx_queues;
  312. }
  313. }
  314. snprintf(channel->name, sizeof(channel->name),
  315. "%s%s-%d", efx->name, type, number);
  316. }
  317. }
  318. /* Channels are shutdown and reinitialised whilst the NIC is running
  319. * to propagate configuration changes (mtu, checksum offload), or
  320. * to clear hardware error conditions
  321. */
  322. static void efx_init_channels(struct efx_nic *efx)
  323. {
  324. struct efx_tx_queue *tx_queue;
  325. struct efx_rx_queue *rx_queue;
  326. struct efx_channel *channel;
  327. /* Calculate the rx buffer allocation parameters required to
  328. * support the current MTU, including padding for header
  329. * alignment and overruns.
  330. */
  331. efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
  332. EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
  333. efx->type->rx_buffer_padding);
  334. efx->rx_buffer_order = get_order(efx->rx_buffer_len);
  335. /* Initialise the channels */
  336. efx_for_each_channel(channel, efx) {
  337. EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
  338. efx_init_eventq(channel);
  339. efx_for_each_channel_tx_queue(tx_queue, channel)
  340. efx_init_tx_queue(tx_queue);
  341. /* The rx buffer allocation strategy is MTU dependent */
  342. efx_rx_strategy(channel);
  343. efx_for_each_channel_rx_queue(rx_queue, channel)
  344. efx_init_rx_queue(rx_queue);
  345. WARN_ON(channel->rx_pkt != NULL);
  346. efx_rx_strategy(channel);
  347. }
  348. }
  349. /* This enables event queue processing and packet transmission.
  350. *
  351. * Note that this function is not allowed to fail, since that would
  352. * introduce too much complexity into the suspend/resume path.
  353. */
  354. static void efx_start_channel(struct efx_channel *channel)
  355. {
  356. struct efx_rx_queue *rx_queue;
  357. EFX_LOG(channel->efx, "starting chan %d\n", channel->channel);
  358. if (!(channel->efx->net_dev->flags & IFF_UP))
  359. netif_napi_add(channel->napi_dev, &channel->napi_str,
  360. efx_poll, napi_weight);
  361. /* The interrupt handler for this channel may set work_pending
  362. * as soon as we enable it. Make sure it's cleared before
  363. * then. Similarly, make sure it sees the enabled flag set. */
  364. channel->work_pending = false;
  365. channel->enabled = true;
  366. smp_wmb();
  367. napi_enable(&channel->napi_str);
  368. /* Load up RX descriptors */
  369. efx_for_each_channel_rx_queue(rx_queue, channel)
  370. efx_fast_push_rx_descriptors(rx_queue);
  371. }
  372. /* This disables event queue processing and packet transmission.
  373. * This function does not guarantee that all queue processing
  374. * (e.g. RX refill) is complete.
  375. */
  376. static void efx_stop_channel(struct efx_channel *channel)
  377. {
  378. struct efx_rx_queue *rx_queue;
  379. if (!channel->enabled)
  380. return;
  381. EFX_LOG(channel->efx, "stop chan %d\n", channel->channel);
  382. channel->enabled = false;
  383. napi_disable(&channel->napi_str);
  384. /* Ensure that any worker threads have exited or will be no-ops */
  385. efx_for_each_channel_rx_queue(rx_queue, channel) {
  386. spin_lock_bh(&rx_queue->add_lock);
  387. spin_unlock_bh(&rx_queue->add_lock);
  388. }
  389. }
  390. static void efx_fini_channels(struct efx_nic *efx)
  391. {
  392. struct efx_channel *channel;
  393. struct efx_tx_queue *tx_queue;
  394. struct efx_rx_queue *rx_queue;
  395. int rc;
  396. EFX_ASSERT_RESET_SERIALISED(efx);
  397. BUG_ON(efx->port_enabled);
  398. rc = falcon_flush_queues(efx);
  399. if (rc)
  400. EFX_ERR(efx, "failed to flush queues\n");
  401. else
  402. EFX_LOG(efx, "successfully flushed all queues\n");
  403. efx_for_each_channel(channel, efx) {
  404. EFX_LOG(channel->efx, "shut down chan %d\n", channel->channel);
  405. efx_for_each_channel_rx_queue(rx_queue, channel)
  406. efx_fini_rx_queue(rx_queue);
  407. efx_for_each_channel_tx_queue(tx_queue, channel)
  408. efx_fini_tx_queue(tx_queue);
  409. efx_fini_eventq(channel);
  410. }
  411. }
  412. static void efx_remove_channel(struct efx_channel *channel)
  413. {
  414. struct efx_tx_queue *tx_queue;
  415. struct efx_rx_queue *rx_queue;
  416. EFX_LOG(channel->efx, "destroy chan %d\n", channel->channel);
  417. efx_for_each_channel_rx_queue(rx_queue, channel)
  418. efx_remove_rx_queue(rx_queue);
  419. efx_for_each_channel_tx_queue(tx_queue, channel)
  420. efx_remove_tx_queue(tx_queue);
  421. efx_remove_eventq(channel);
  422. channel->used_flags = 0;
  423. }
  424. void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue, int delay)
  425. {
  426. queue_delayed_work(refill_workqueue, &rx_queue->work, delay);
  427. }
  428. /**************************************************************************
  429. *
  430. * Port handling
  431. *
  432. **************************************************************************/
  433. /* This ensures that the kernel is kept informed (via
  434. * netif_carrier_on/off) of the link status, and also maintains the
  435. * link status's stop on the port's TX queue.
  436. */
  437. static void efx_link_status_changed(struct efx_nic *efx)
  438. {
  439. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  440. * that no events are triggered between unregister_netdev() and the
  441. * driver unloading. A more general condition is that NETDEV_CHANGE
  442. * can only be generated between NETDEV_UP and NETDEV_DOWN */
  443. if (!netif_running(efx->net_dev))
  444. return;
  445. if (efx->port_inhibited) {
  446. netif_carrier_off(efx->net_dev);
  447. return;
  448. }
  449. if (efx->link_up != netif_carrier_ok(efx->net_dev)) {
  450. efx->n_link_state_changes++;
  451. if (efx->link_up)
  452. netif_carrier_on(efx->net_dev);
  453. else
  454. netif_carrier_off(efx->net_dev);
  455. }
  456. /* Status message for kernel log */
  457. if (efx->link_up) {
  458. EFX_INFO(efx, "link up at %uMbps %s-duplex (MTU %d)%s\n",
  459. efx->link_speed, efx->link_fd ? "full" : "half",
  460. efx->net_dev->mtu,
  461. (efx->promiscuous ? " [PROMISC]" : ""));
  462. } else {
  463. EFX_INFO(efx, "link down\n");
  464. }
  465. }
  466. /* This call reinitialises the MAC to pick up new PHY settings. The
  467. * caller must hold the mac_lock */
  468. void __efx_reconfigure_port(struct efx_nic *efx)
  469. {
  470. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  471. EFX_LOG(efx, "reconfiguring MAC from PHY settings on CPU %d\n",
  472. raw_smp_processor_id());
  473. /* Serialise the promiscuous flag with efx_set_multicast_list. */
  474. if (efx_dev_registered(efx)) {
  475. netif_addr_lock_bh(efx->net_dev);
  476. netif_addr_unlock_bh(efx->net_dev);
  477. }
  478. falcon_reconfigure_xmac(efx);
  479. /* Inform kernel of loss/gain of carrier */
  480. efx_link_status_changed(efx);
  481. }
  482. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  483. * disabled. */
  484. void efx_reconfigure_port(struct efx_nic *efx)
  485. {
  486. EFX_ASSERT_RESET_SERIALISED(efx);
  487. mutex_lock(&efx->mac_lock);
  488. __efx_reconfigure_port(efx);
  489. mutex_unlock(&efx->mac_lock);
  490. }
  491. /* Asynchronous efx_reconfigure_port work item. To speed up efx_flush_all()
  492. * we don't efx_reconfigure_port() if the port is disabled. Care is taken
  493. * in efx_stop_all() and efx_start_port() to prevent PHY events being lost */
  494. static void efx_reconfigure_work(struct work_struct *data)
  495. {
  496. struct efx_nic *efx = container_of(data, struct efx_nic,
  497. reconfigure_work);
  498. mutex_lock(&efx->mac_lock);
  499. if (efx->port_enabled)
  500. __efx_reconfigure_port(efx);
  501. mutex_unlock(&efx->mac_lock);
  502. }
  503. static int efx_probe_port(struct efx_nic *efx)
  504. {
  505. int rc;
  506. EFX_LOG(efx, "create port\n");
  507. /* Connect up MAC/PHY operations table and read MAC address */
  508. rc = falcon_probe_port(efx);
  509. if (rc)
  510. goto err;
  511. if (phy_flash_cfg)
  512. efx->phy_mode = PHY_MODE_SPECIAL;
  513. /* Sanity check MAC address */
  514. if (is_valid_ether_addr(efx->mac_address)) {
  515. memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
  516. } else {
  517. EFX_ERR(efx, "invalid MAC address %pM\n",
  518. efx->mac_address);
  519. if (!allow_bad_hwaddr) {
  520. rc = -EINVAL;
  521. goto err;
  522. }
  523. random_ether_addr(efx->net_dev->dev_addr);
  524. EFX_INFO(efx, "using locally-generated MAC %pM\n",
  525. efx->net_dev->dev_addr);
  526. }
  527. return 0;
  528. err:
  529. efx_remove_port(efx);
  530. return rc;
  531. }
  532. static int efx_init_port(struct efx_nic *efx)
  533. {
  534. int rc;
  535. EFX_LOG(efx, "init port\n");
  536. /* Initialise the MAC and PHY */
  537. rc = falcon_init_xmac(efx);
  538. if (rc)
  539. return rc;
  540. efx->port_initialized = true;
  541. efx->stats_enabled = true;
  542. /* Reconfigure port to program MAC registers */
  543. falcon_reconfigure_xmac(efx);
  544. return 0;
  545. }
  546. /* Allow efx_reconfigure_port() to be scheduled, and close the window
  547. * between efx_stop_port and efx_flush_all whereby a previously scheduled
  548. * efx_reconfigure_port() may have been cancelled */
  549. static void efx_start_port(struct efx_nic *efx)
  550. {
  551. EFX_LOG(efx, "start port\n");
  552. BUG_ON(efx->port_enabled);
  553. mutex_lock(&efx->mac_lock);
  554. efx->port_enabled = true;
  555. __efx_reconfigure_port(efx);
  556. mutex_unlock(&efx->mac_lock);
  557. }
  558. /* Prevent efx_reconfigure_work and efx_monitor() from executing, and
  559. * efx_set_multicast_list() from scheduling efx_reconfigure_work.
  560. * efx_reconfigure_work can still be scheduled via NAPI processing
  561. * until efx_flush_all() is called */
  562. static void efx_stop_port(struct efx_nic *efx)
  563. {
  564. EFX_LOG(efx, "stop port\n");
  565. mutex_lock(&efx->mac_lock);
  566. efx->port_enabled = false;
  567. mutex_unlock(&efx->mac_lock);
  568. /* Serialise against efx_set_multicast_list() */
  569. if (efx_dev_registered(efx)) {
  570. netif_addr_lock_bh(efx->net_dev);
  571. netif_addr_unlock_bh(efx->net_dev);
  572. }
  573. }
  574. static void efx_fini_port(struct efx_nic *efx)
  575. {
  576. EFX_LOG(efx, "shut down port\n");
  577. if (!efx->port_initialized)
  578. return;
  579. falcon_fini_xmac(efx);
  580. efx->port_initialized = false;
  581. efx->link_up = false;
  582. efx_link_status_changed(efx);
  583. }
  584. static void efx_remove_port(struct efx_nic *efx)
  585. {
  586. EFX_LOG(efx, "destroying port\n");
  587. falcon_remove_port(efx);
  588. }
  589. /**************************************************************************
  590. *
  591. * NIC handling
  592. *
  593. **************************************************************************/
  594. /* This configures the PCI device to enable I/O and DMA. */
  595. static int efx_init_io(struct efx_nic *efx)
  596. {
  597. struct pci_dev *pci_dev = efx->pci_dev;
  598. dma_addr_t dma_mask = efx->type->max_dma_mask;
  599. int rc;
  600. EFX_LOG(efx, "initialising I/O\n");
  601. rc = pci_enable_device(pci_dev);
  602. if (rc) {
  603. EFX_ERR(efx, "failed to enable PCI device\n");
  604. goto fail1;
  605. }
  606. pci_set_master(pci_dev);
  607. /* Set the PCI DMA mask. Try all possibilities from our
  608. * genuine mask down to 32 bits, because some architectures
  609. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  610. * masks event though they reject 46 bit masks.
  611. */
  612. while (dma_mask > 0x7fffffffUL) {
  613. if (pci_dma_supported(pci_dev, dma_mask) &&
  614. ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
  615. break;
  616. dma_mask >>= 1;
  617. }
  618. if (rc) {
  619. EFX_ERR(efx, "could not find a suitable DMA mask\n");
  620. goto fail2;
  621. }
  622. EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
  623. rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
  624. if (rc) {
  625. /* pci_set_consistent_dma_mask() is not *allowed* to
  626. * fail with a mask that pci_set_dma_mask() accepted,
  627. * but just in case...
  628. */
  629. EFX_ERR(efx, "failed to set consistent DMA mask\n");
  630. goto fail2;
  631. }
  632. efx->membase_phys = pci_resource_start(efx->pci_dev,
  633. efx->type->mem_bar);
  634. rc = pci_request_region(pci_dev, efx->type->mem_bar, "sfc");
  635. if (rc) {
  636. EFX_ERR(efx, "request for memory BAR failed\n");
  637. rc = -EIO;
  638. goto fail3;
  639. }
  640. efx->membase = ioremap_nocache(efx->membase_phys,
  641. efx->type->mem_map_size);
  642. if (!efx->membase) {
  643. EFX_ERR(efx, "could not map memory BAR %d at %llx+%x\n",
  644. efx->type->mem_bar,
  645. (unsigned long long)efx->membase_phys,
  646. efx->type->mem_map_size);
  647. rc = -ENOMEM;
  648. goto fail4;
  649. }
  650. EFX_LOG(efx, "memory BAR %u at %llx+%x (virtual %p)\n",
  651. efx->type->mem_bar, (unsigned long long)efx->membase_phys,
  652. efx->type->mem_map_size, efx->membase);
  653. return 0;
  654. fail4:
  655. pci_release_region(efx->pci_dev, efx->type->mem_bar);
  656. fail3:
  657. efx->membase_phys = 0;
  658. fail2:
  659. pci_disable_device(efx->pci_dev);
  660. fail1:
  661. return rc;
  662. }
  663. static void efx_fini_io(struct efx_nic *efx)
  664. {
  665. EFX_LOG(efx, "shutting down I/O\n");
  666. if (efx->membase) {
  667. iounmap(efx->membase);
  668. efx->membase = NULL;
  669. }
  670. if (efx->membase_phys) {
  671. pci_release_region(efx->pci_dev, efx->type->mem_bar);
  672. efx->membase_phys = 0;
  673. }
  674. pci_disable_device(efx->pci_dev);
  675. }
  676. /* Get number of RX queues wanted. Return number of online CPU
  677. * packages in the expectation that an IRQ balancer will spread
  678. * interrupts across them. */
  679. static int efx_wanted_rx_queues(void)
  680. {
  681. cpumask_t core_mask;
  682. int count;
  683. int cpu;
  684. cpus_clear(core_mask);
  685. count = 0;
  686. for_each_online_cpu(cpu) {
  687. if (!cpu_isset(cpu, core_mask)) {
  688. ++count;
  689. cpus_or(core_mask, core_mask,
  690. topology_core_siblings(cpu));
  691. }
  692. }
  693. return count;
  694. }
  695. /* Probe the number and type of interrupts we are able to obtain, and
  696. * the resulting numbers of channels and RX queues.
  697. */
  698. static void efx_probe_interrupts(struct efx_nic *efx)
  699. {
  700. int max_channels =
  701. min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
  702. int rc, i;
  703. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  704. struct msix_entry xentries[EFX_MAX_CHANNELS];
  705. int wanted_ints;
  706. int rx_queues;
  707. /* We want one RX queue and interrupt per CPU package
  708. * (or as specified by the rss_cpus module parameter).
  709. * We will need one channel per interrupt.
  710. */
  711. rx_queues = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
  712. wanted_ints = rx_queues + (separate_tx_channels ? 1 : 0);
  713. wanted_ints = min(wanted_ints, max_channels);
  714. for (i = 0; i < wanted_ints; i++)
  715. xentries[i].entry = i;
  716. rc = pci_enable_msix(efx->pci_dev, xentries, wanted_ints);
  717. if (rc > 0) {
  718. EFX_ERR(efx, "WARNING: Insufficient MSI-X vectors"
  719. " available (%d < %d).\n", rc, wanted_ints);
  720. EFX_ERR(efx, "WARNING: Performance may be reduced.\n");
  721. EFX_BUG_ON_PARANOID(rc >= wanted_ints);
  722. wanted_ints = rc;
  723. rc = pci_enable_msix(efx->pci_dev, xentries,
  724. wanted_ints);
  725. }
  726. if (rc == 0) {
  727. efx->n_rx_queues = min(rx_queues, wanted_ints);
  728. efx->n_channels = wanted_ints;
  729. for (i = 0; i < wanted_ints; i++)
  730. efx->channel[i].irq = xentries[i].vector;
  731. } else {
  732. /* Fall back to single channel MSI */
  733. efx->interrupt_mode = EFX_INT_MODE_MSI;
  734. EFX_ERR(efx, "could not enable MSI-X\n");
  735. }
  736. }
  737. /* Try single interrupt MSI */
  738. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  739. efx->n_rx_queues = 1;
  740. efx->n_channels = 1;
  741. rc = pci_enable_msi(efx->pci_dev);
  742. if (rc == 0) {
  743. efx->channel[0].irq = efx->pci_dev->irq;
  744. } else {
  745. EFX_ERR(efx, "could not enable MSI\n");
  746. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  747. }
  748. }
  749. /* Assume legacy interrupts */
  750. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  751. efx->n_rx_queues = 1;
  752. efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
  753. efx->legacy_irq = efx->pci_dev->irq;
  754. }
  755. }
  756. static void efx_remove_interrupts(struct efx_nic *efx)
  757. {
  758. struct efx_channel *channel;
  759. /* Remove MSI/MSI-X interrupts */
  760. efx_for_each_channel(channel, efx)
  761. channel->irq = 0;
  762. pci_disable_msi(efx->pci_dev);
  763. pci_disable_msix(efx->pci_dev);
  764. /* Remove legacy interrupt */
  765. efx->legacy_irq = 0;
  766. }
  767. static void efx_set_channels(struct efx_nic *efx)
  768. {
  769. struct efx_tx_queue *tx_queue;
  770. struct efx_rx_queue *rx_queue;
  771. efx_for_each_tx_queue(tx_queue, efx) {
  772. if (separate_tx_channels)
  773. tx_queue->channel = &efx->channel[efx->n_channels-1];
  774. else
  775. tx_queue->channel = &efx->channel[0];
  776. tx_queue->channel->used_flags |= EFX_USED_BY_TX;
  777. }
  778. efx_for_each_rx_queue(rx_queue, efx) {
  779. rx_queue->channel = &efx->channel[rx_queue->queue];
  780. rx_queue->channel->used_flags |= EFX_USED_BY_RX;
  781. }
  782. }
  783. static int efx_probe_nic(struct efx_nic *efx)
  784. {
  785. int rc;
  786. EFX_LOG(efx, "creating NIC\n");
  787. /* Carry out hardware-type specific initialisation */
  788. rc = falcon_probe_nic(efx);
  789. if (rc)
  790. return rc;
  791. /* Determine the number of channels and RX queues by trying to hook
  792. * in MSI-X interrupts. */
  793. efx_probe_interrupts(efx);
  794. efx_set_channels(efx);
  795. /* Initialise the interrupt moderation settings */
  796. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec);
  797. return 0;
  798. }
  799. static void efx_remove_nic(struct efx_nic *efx)
  800. {
  801. EFX_LOG(efx, "destroying NIC\n");
  802. efx_remove_interrupts(efx);
  803. falcon_remove_nic(efx);
  804. }
  805. /**************************************************************************
  806. *
  807. * NIC startup/shutdown
  808. *
  809. *************************************************************************/
  810. static int efx_probe_all(struct efx_nic *efx)
  811. {
  812. struct efx_channel *channel;
  813. int rc;
  814. /* Create NIC */
  815. rc = efx_probe_nic(efx);
  816. if (rc) {
  817. EFX_ERR(efx, "failed to create NIC\n");
  818. goto fail1;
  819. }
  820. /* Create port */
  821. rc = efx_probe_port(efx);
  822. if (rc) {
  823. EFX_ERR(efx, "failed to create port\n");
  824. goto fail2;
  825. }
  826. /* Create channels */
  827. efx_for_each_channel(channel, efx) {
  828. rc = efx_probe_channel(channel);
  829. if (rc) {
  830. EFX_ERR(efx, "failed to create channel %d\n",
  831. channel->channel);
  832. goto fail3;
  833. }
  834. }
  835. efx_set_channel_names(efx);
  836. return 0;
  837. fail3:
  838. efx_for_each_channel(channel, efx)
  839. efx_remove_channel(channel);
  840. efx_remove_port(efx);
  841. fail2:
  842. efx_remove_nic(efx);
  843. fail1:
  844. return rc;
  845. }
  846. /* Called after previous invocation(s) of efx_stop_all, restarts the
  847. * port, kernel transmit queue, NAPI processing and hardware interrupts,
  848. * and ensures that the port is scheduled to be reconfigured.
  849. * This function is safe to call multiple times when the NIC is in any
  850. * state. */
  851. static void efx_start_all(struct efx_nic *efx)
  852. {
  853. struct efx_channel *channel;
  854. EFX_ASSERT_RESET_SERIALISED(efx);
  855. /* Check that it is appropriate to restart the interface. All
  856. * of these flags are safe to read under just the rtnl lock */
  857. if (efx->port_enabled)
  858. return;
  859. if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
  860. return;
  861. if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
  862. return;
  863. /* Mark the port as enabled so port reconfigurations can start, then
  864. * restart the transmit interface early so the watchdog timer stops */
  865. efx_start_port(efx);
  866. if (efx_dev_registered(efx))
  867. efx_wake_queue(efx);
  868. efx_for_each_channel(channel, efx)
  869. efx_start_channel(channel);
  870. falcon_enable_interrupts(efx);
  871. /* Start hardware monitor if we're in RUNNING */
  872. if (efx->state == STATE_RUNNING)
  873. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  874. efx_monitor_interval);
  875. }
  876. /* Flush all delayed work. Should only be called when no more delayed work
  877. * will be scheduled. This doesn't flush pending online resets (efx_reset),
  878. * since we're holding the rtnl_lock at this point. */
  879. static void efx_flush_all(struct efx_nic *efx)
  880. {
  881. struct efx_rx_queue *rx_queue;
  882. /* Make sure the hardware monitor is stopped */
  883. cancel_delayed_work_sync(&efx->monitor_work);
  884. /* Ensure that all RX slow refills are complete. */
  885. efx_for_each_rx_queue(rx_queue, efx)
  886. cancel_delayed_work_sync(&rx_queue->work);
  887. /* Stop scheduled port reconfigurations */
  888. cancel_work_sync(&efx->reconfigure_work);
  889. }
  890. /* Quiesce hardware and software without bringing the link down.
  891. * Safe to call multiple times, when the nic and interface is in any
  892. * state. The caller is guaranteed to subsequently be in a position
  893. * to modify any hardware and software state they see fit without
  894. * taking locks. */
  895. static void efx_stop_all(struct efx_nic *efx)
  896. {
  897. struct efx_channel *channel;
  898. EFX_ASSERT_RESET_SERIALISED(efx);
  899. /* port_enabled can be read safely under the rtnl lock */
  900. if (!efx->port_enabled)
  901. return;
  902. /* Disable interrupts and wait for ISR to complete */
  903. falcon_disable_interrupts(efx);
  904. if (efx->legacy_irq)
  905. synchronize_irq(efx->legacy_irq);
  906. efx_for_each_channel(channel, efx) {
  907. if (channel->irq)
  908. synchronize_irq(channel->irq);
  909. }
  910. /* Stop all NAPI processing and synchronous rx refills */
  911. efx_for_each_channel(channel, efx)
  912. efx_stop_channel(channel);
  913. /* Stop all asynchronous port reconfigurations. Since all
  914. * event processing has already been stopped, there is no
  915. * window to loose phy events */
  916. efx_stop_port(efx);
  917. /* Flush reconfigure_work, refill_workqueue, monitor_work */
  918. efx_flush_all(efx);
  919. /* Isolate the MAC from the TX and RX engines, so that queue
  920. * flushes will complete in a timely fashion. */
  921. falcon_drain_tx_fifo(efx);
  922. /* Stop the kernel transmit interface late, so the watchdog
  923. * timer isn't ticking over the flush */
  924. if (efx_dev_registered(efx)) {
  925. efx_stop_queue(efx);
  926. netif_tx_lock_bh(efx->net_dev);
  927. netif_tx_unlock_bh(efx->net_dev);
  928. }
  929. }
  930. static void efx_remove_all(struct efx_nic *efx)
  931. {
  932. struct efx_channel *channel;
  933. efx_for_each_channel(channel, efx)
  934. efx_remove_channel(channel);
  935. efx_remove_port(efx);
  936. efx_remove_nic(efx);
  937. }
  938. /* A convinience function to safely flush all the queues */
  939. void efx_flush_queues(struct efx_nic *efx)
  940. {
  941. EFX_ASSERT_RESET_SERIALISED(efx);
  942. efx_stop_all(efx);
  943. efx_fini_channels(efx);
  944. efx_init_channels(efx);
  945. efx_start_all(efx);
  946. }
  947. /**************************************************************************
  948. *
  949. * Interrupt moderation
  950. *
  951. **************************************************************************/
  952. /* Set interrupt moderation parameters */
  953. void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs)
  954. {
  955. struct efx_tx_queue *tx_queue;
  956. struct efx_rx_queue *rx_queue;
  957. EFX_ASSERT_RESET_SERIALISED(efx);
  958. efx_for_each_tx_queue(tx_queue, efx)
  959. tx_queue->channel->irq_moderation = tx_usecs;
  960. efx_for_each_rx_queue(rx_queue, efx)
  961. rx_queue->channel->irq_moderation = rx_usecs;
  962. }
  963. /**************************************************************************
  964. *
  965. * Hardware monitor
  966. *
  967. **************************************************************************/
  968. /* Run periodically off the general workqueue. Serialised against
  969. * efx_reconfigure_port via the mac_lock */
  970. static void efx_monitor(struct work_struct *data)
  971. {
  972. struct efx_nic *efx = container_of(data, struct efx_nic,
  973. monitor_work.work);
  974. int rc = 0;
  975. EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
  976. raw_smp_processor_id());
  977. /* If the mac_lock is already held then it is likely a port
  978. * reconfiguration is already in place, which will likely do
  979. * most of the work of check_hw() anyway. */
  980. if (!mutex_trylock(&efx->mac_lock)) {
  981. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  982. efx_monitor_interval);
  983. return;
  984. }
  985. if (efx->port_enabled)
  986. rc = falcon_check_xmac(efx);
  987. mutex_unlock(&efx->mac_lock);
  988. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  989. efx_monitor_interval);
  990. }
  991. /**************************************************************************
  992. *
  993. * ioctls
  994. *
  995. *************************************************************************/
  996. /* Net device ioctl
  997. * Context: process, rtnl_lock() held.
  998. */
  999. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1000. {
  1001. struct efx_nic *efx = netdev_priv(net_dev);
  1002. EFX_ASSERT_RESET_SERIALISED(efx);
  1003. return generic_mii_ioctl(&efx->mii, if_mii(ifr), cmd, NULL);
  1004. }
  1005. /**************************************************************************
  1006. *
  1007. * NAPI interface
  1008. *
  1009. **************************************************************************/
  1010. static int efx_init_napi(struct efx_nic *efx)
  1011. {
  1012. struct efx_channel *channel;
  1013. int rc;
  1014. efx_for_each_channel(channel, efx) {
  1015. channel->napi_dev = efx->net_dev;
  1016. rc = efx_lro_init(&channel->lro_mgr, efx);
  1017. if (rc)
  1018. goto err;
  1019. }
  1020. return 0;
  1021. err:
  1022. efx_fini_napi(efx);
  1023. return rc;
  1024. }
  1025. static void efx_fini_napi(struct efx_nic *efx)
  1026. {
  1027. struct efx_channel *channel;
  1028. efx_for_each_channel(channel, efx) {
  1029. efx_lro_fini(&channel->lro_mgr);
  1030. channel->napi_dev = NULL;
  1031. }
  1032. }
  1033. /**************************************************************************
  1034. *
  1035. * Kernel netpoll interface
  1036. *
  1037. *************************************************************************/
  1038. #ifdef CONFIG_NET_POLL_CONTROLLER
  1039. /* Although in the common case interrupts will be disabled, this is not
  1040. * guaranteed. However, all our work happens inside the NAPI callback,
  1041. * so no locking is required.
  1042. */
  1043. static void efx_netpoll(struct net_device *net_dev)
  1044. {
  1045. struct efx_nic *efx = netdev_priv(net_dev);
  1046. struct efx_channel *channel;
  1047. efx_for_each_channel(channel, efx)
  1048. efx_schedule_channel(channel);
  1049. }
  1050. #endif
  1051. /**************************************************************************
  1052. *
  1053. * Kernel net device interface
  1054. *
  1055. *************************************************************************/
  1056. /* Context: process, rtnl_lock() held. */
  1057. static int efx_net_open(struct net_device *net_dev)
  1058. {
  1059. struct efx_nic *efx = netdev_priv(net_dev);
  1060. EFX_ASSERT_RESET_SERIALISED(efx);
  1061. EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
  1062. raw_smp_processor_id());
  1063. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1064. return -EBUSY;
  1065. efx_start_all(efx);
  1066. return 0;
  1067. }
  1068. /* Context: process, rtnl_lock() held.
  1069. * Note that the kernel will ignore our return code; this method
  1070. * should really be a void.
  1071. */
  1072. static int efx_net_stop(struct net_device *net_dev)
  1073. {
  1074. struct efx_nic *efx = netdev_priv(net_dev);
  1075. EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
  1076. raw_smp_processor_id());
  1077. /* Stop the device and flush all the channels */
  1078. efx_stop_all(efx);
  1079. efx_fini_channels(efx);
  1080. efx_init_channels(efx);
  1081. return 0;
  1082. }
  1083. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1084. static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
  1085. {
  1086. struct efx_nic *efx = netdev_priv(net_dev);
  1087. struct efx_mac_stats *mac_stats = &efx->mac_stats;
  1088. struct net_device_stats *stats = &net_dev->stats;
  1089. /* Update stats if possible, but do not wait if another thread
  1090. * is updating them (or resetting the NIC); slightly stale
  1091. * stats are acceptable.
  1092. */
  1093. if (!spin_trylock(&efx->stats_lock))
  1094. return stats;
  1095. if (efx->stats_enabled) {
  1096. falcon_update_stats_xmac(efx);
  1097. falcon_update_nic_stats(efx);
  1098. }
  1099. spin_unlock(&efx->stats_lock);
  1100. stats->rx_packets = mac_stats->rx_packets;
  1101. stats->tx_packets = mac_stats->tx_packets;
  1102. stats->rx_bytes = mac_stats->rx_bytes;
  1103. stats->tx_bytes = mac_stats->tx_bytes;
  1104. stats->multicast = mac_stats->rx_multicast;
  1105. stats->collisions = mac_stats->tx_collision;
  1106. stats->rx_length_errors = (mac_stats->rx_gtjumbo +
  1107. mac_stats->rx_length_error);
  1108. stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
  1109. stats->rx_crc_errors = mac_stats->rx_bad;
  1110. stats->rx_frame_errors = mac_stats->rx_align_error;
  1111. stats->rx_fifo_errors = mac_stats->rx_overflow;
  1112. stats->rx_missed_errors = mac_stats->rx_missed;
  1113. stats->tx_window_errors = mac_stats->tx_late_collision;
  1114. stats->rx_errors = (stats->rx_length_errors +
  1115. stats->rx_over_errors +
  1116. stats->rx_crc_errors +
  1117. stats->rx_frame_errors +
  1118. stats->rx_fifo_errors +
  1119. stats->rx_missed_errors +
  1120. mac_stats->rx_symbol_error);
  1121. stats->tx_errors = (stats->tx_window_errors +
  1122. mac_stats->tx_bad);
  1123. return stats;
  1124. }
  1125. /* Context: netif_tx_lock held, BHs disabled. */
  1126. static void efx_watchdog(struct net_device *net_dev)
  1127. {
  1128. struct efx_nic *efx = netdev_priv(net_dev);
  1129. EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d:"
  1130. " resetting channels\n",
  1131. atomic_read(&efx->netif_stop_count), efx->port_enabled);
  1132. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1133. }
  1134. /* Context: process, rtnl_lock() held. */
  1135. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1136. {
  1137. struct efx_nic *efx = netdev_priv(net_dev);
  1138. int rc = 0;
  1139. EFX_ASSERT_RESET_SERIALISED(efx);
  1140. if (new_mtu > EFX_MAX_MTU)
  1141. return -EINVAL;
  1142. efx_stop_all(efx);
  1143. EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
  1144. efx_fini_channels(efx);
  1145. net_dev->mtu = new_mtu;
  1146. efx_init_channels(efx);
  1147. efx_start_all(efx);
  1148. return rc;
  1149. }
  1150. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1151. {
  1152. struct efx_nic *efx = netdev_priv(net_dev);
  1153. struct sockaddr *addr = data;
  1154. char *new_addr = addr->sa_data;
  1155. EFX_ASSERT_RESET_SERIALISED(efx);
  1156. if (!is_valid_ether_addr(new_addr)) {
  1157. EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
  1158. new_addr);
  1159. return -EINVAL;
  1160. }
  1161. memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
  1162. /* Reconfigure the MAC */
  1163. efx_reconfigure_port(efx);
  1164. return 0;
  1165. }
  1166. /* Context: netif_addr_lock held, BHs disabled. */
  1167. static void efx_set_multicast_list(struct net_device *net_dev)
  1168. {
  1169. struct efx_nic *efx = netdev_priv(net_dev);
  1170. struct dev_mc_list *mc_list = net_dev->mc_list;
  1171. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  1172. bool promiscuous = !!(net_dev->flags & IFF_PROMISC);
  1173. bool changed = (efx->promiscuous != promiscuous);
  1174. u32 crc;
  1175. int bit;
  1176. int i;
  1177. efx->promiscuous = promiscuous;
  1178. /* Build multicast hash table */
  1179. if (promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
  1180. memset(mc_hash, 0xff, sizeof(*mc_hash));
  1181. } else {
  1182. memset(mc_hash, 0x00, sizeof(*mc_hash));
  1183. for (i = 0; i < net_dev->mc_count; i++) {
  1184. crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
  1185. bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
  1186. set_bit_le(bit, mc_hash->byte);
  1187. mc_list = mc_list->next;
  1188. }
  1189. }
  1190. if (!efx->port_enabled)
  1191. /* Delay pushing settings until efx_start_port() */
  1192. return;
  1193. if (changed)
  1194. queue_work(efx->workqueue, &efx->reconfigure_work);
  1195. /* Create and activate new global multicast hash table */
  1196. falcon_set_multicast_hash(efx);
  1197. }
  1198. static const struct net_device_ops efx_netdev_ops = {
  1199. .ndo_open = efx_net_open,
  1200. .ndo_stop = efx_net_stop,
  1201. .ndo_get_stats = efx_net_stats,
  1202. .ndo_tx_timeout = efx_watchdog,
  1203. .ndo_start_xmit = efx_hard_start_xmit,
  1204. .ndo_validate_addr = eth_validate_addr,
  1205. .ndo_do_ioctl = efx_ioctl,
  1206. .ndo_change_mtu = efx_change_mtu,
  1207. .ndo_set_mac_address = efx_set_mac_address,
  1208. .ndo_set_multicast_list = efx_set_multicast_list,
  1209. #ifdef CONFIG_NET_POLL_CONTROLLER
  1210. .ndo_poll_controller = efx_netpoll,
  1211. #endif
  1212. };
  1213. static int efx_netdev_event(struct notifier_block *this,
  1214. unsigned long event, void *ptr)
  1215. {
  1216. struct net_device *net_dev = ptr;
  1217. if (net_dev->netdev_ops == &efx_netdev_ops && event == NETDEV_CHANGENAME) {
  1218. struct efx_nic *efx = netdev_priv(net_dev);
  1219. strcpy(efx->name, net_dev->name);
  1220. efx_mtd_rename(efx);
  1221. efx_set_channel_names(efx);
  1222. }
  1223. return NOTIFY_DONE;
  1224. }
  1225. static struct notifier_block efx_netdev_notifier = {
  1226. .notifier_call = efx_netdev_event,
  1227. };
  1228. static int efx_register_netdev(struct efx_nic *efx)
  1229. {
  1230. struct net_device *net_dev = efx->net_dev;
  1231. int rc;
  1232. net_dev->watchdog_timeo = 5 * HZ;
  1233. net_dev->irq = efx->pci_dev->irq;
  1234. net_dev->netdev_ops = &efx_netdev_ops;
  1235. SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
  1236. SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
  1237. /* Always start with carrier off; PHY events will detect the link */
  1238. netif_carrier_off(efx->net_dev);
  1239. /* Clear MAC statistics */
  1240. falcon_update_stats_xmac(efx);
  1241. memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
  1242. rc = register_netdev(net_dev);
  1243. if (rc) {
  1244. EFX_ERR(efx, "could not register net dev\n");
  1245. return rc;
  1246. }
  1247. strcpy(efx->name, net_dev->name);
  1248. efx_set_channel_names(efx);
  1249. return 0;
  1250. }
  1251. static void efx_unregister_netdev(struct efx_nic *efx)
  1252. {
  1253. struct efx_tx_queue *tx_queue;
  1254. if (!efx->net_dev)
  1255. return;
  1256. BUG_ON(netdev_priv(efx->net_dev) != efx);
  1257. /* Free up any skbs still remaining. This has to happen before
  1258. * we try to unregister the netdev as running their destructors
  1259. * may be needed to get the device ref. count to 0. */
  1260. efx_for_each_tx_queue(tx_queue, efx)
  1261. efx_release_tx_buffers(tx_queue);
  1262. if (efx_dev_registered(efx)) {
  1263. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  1264. unregister_netdev(efx->net_dev);
  1265. }
  1266. }
  1267. /**************************************************************************
  1268. *
  1269. * Device reset and suspend
  1270. *
  1271. **************************************************************************/
  1272. /* Tears down the entire software state and most of the hardware state
  1273. * before reset. */
  1274. void efx_reset_down(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  1275. {
  1276. int rc;
  1277. EFX_ASSERT_RESET_SERIALISED(efx);
  1278. /* The net_dev->get_stats handler is quite slow, and will fail
  1279. * if a fetch is pending over reset. Serialise against it. */
  1280. spin_lock(&efx->stats_lock);
  1281. efx->stats_enabled = false;
  1282. spin_unlock(&efx->stats_lock);
  1283. efx_stop_all(efx);
  1284. mutex_lock(&efx->mac_lock);
  1285. mutex_lock(&efx->spi_lock);
  1286. rc = falcon_xmac_get_settings(efx, ecmd);
  1287. if (rc)
  1288. EFX_ERR(efx, "could not back up PHY settings\n");
  1289. efx_fini_channels(efx);
  1290. }
  1291. /* This function will always ensure that the locks acquired in
  1292. * efx_reset_down() are released. A failure return code indicates
  1293. * that we were unable to reinitialise the hardware, and the
  1294. * driver should be disabled. If ok is false, then the rx and tx
  1295. * engines are not restarted, pending a RESET_DISABLE. */
  1296. int efx_reset_up(struct efx_nic *efx, struct ethtool_cmd *ecmd, bool ok)
  1297. {
  1298. int rc;
  1299. EFX_ASSERT_RESET_SERIALISED(efx);
  1300. rc = falcon_init_nic(efx);
  1301. if (rc) {
  1302. EFX_ERR(efx, "failed to initialise NIC\n");
  1303. ok = false;
  1304. }
  1305. if (ok) {
  1306. efx_init_channels(efx);
  1307. if (falcon_xmac_set_settings(efx, ecmd))
  1308. EFX_ERR(efx, "could not restore PHY settings\n");
  1309. }
  1310. mutex_unlock(&efx->spi_lock);
  1311. mutex_unlock(&efx->mac_lock);
  1312. if (ok) {
  1313. efx_start_all(efx);
  1314. efx->stats_enabled = true;
  1315. }
  1316. return rc;
  1317. }
  1318. /* Reset the NIC as transparently as possible. Do not reset the PHY
  1319. * Note that the reset may fail, in which case the card will be left
  1320. * in a most-probably-unusable state.
  1321. *
  1322. * This function will sleep. You cannot reset from within an atomic
  1323. * state; use efx_schedule_reset() instead.
  1324. *
  1325. * Grabs the rtnl_lock.
  1326. */
  1327. static int efx_reset(struct efx_nic *efx)
  1328. {
  1329. struct ethtool_cmd ecmd;
  1330. enum reset_type method = efx->reset_pending;
  1331. int rc;
  1332. /* Serialise with kernel interfaces */
  1333. rtnl_lock();
  1334. /* If we're not RUNNING then don't reset. Leave the reset_pending
  1335. * flag set so that efx_pci_probe_main will be retried */
  1336. if (efx->state != STATE_RUNNING) {
  1337. EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
  1338. goto unlock_rtnl;
  1339. }
  1340. EFX_INFO(efx, "resetting (%d)\n", method);
  1341. efx_reset_down(efx, &ecmd);
  1342. rc = falcon_reset_hw(efx, method);
  1343. if (rc) {
  1344. EFX_ERR(efx, "failed to reset hardware\n");
  1345. goto fail;
  1346. }
  1347. /* Allow resets to be rescheduled. */
  1348. efx->reset_pending = RESET_TYPE_NONE;
  1349. /* Reinitialise bus-mastering, which may have been turned off before
  1350. * the reset was scheduled. This is still appropriate, even in the
  1351. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  1352. * can respond to requests. */
  1353. pci_set_master(efx->pci_dev);
  1354. /* Leave device stopped if necessary */
  1355. if (method == RESET_TYPE_DISABLE) {
  1356. rc = -EIO;
  1357. goto fail;
  1358. }
  1359. rc = efx_reset_up(efx, &ecmd, true);
  1360. if (rc)
  1361. goto disable;
  1362. EFX_LOG(efx, "reset complete\n");
  1363. unlock_rtnl:
  1364. rtnl_unlock();
  1365. return 0;
  1366. fail:
  1367. efx_reset_up(efx, &ecmd, false);
  1368. disable:
  1369. EFX_ERR(efx, "has been disabled\n");
  1370. efx->state = STATE_DISABLED;
  1371. rtnl_unlock();
  1372. efx_unregister_netdev(efx);
  1373. efx_fini_port(efx);
  1374. return rc;
  1375. }
  1376. /* The worker thread exists so that code that cannot sleep can
  1377. * schedule a reset for later.
  1378. */
  1379. static void efx_reset_work(struct work_struct *data)
  1380. {
  1381. struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
  1382. efx_reset(nic);
  1383. }
  1384. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  1385. {
  1386. enum reset_type method;
  1387. if (efx->reset_pending != RESET_TYPE_NONE) {
  1388. EFX_INFO(efx, "quenching already scheduled reset\n");
  1389. return;
  1390. }
  1391. switch (type) {
  1392. case RESET_TYPE_INVISIBLE:
  1393. case RESET_TYPE_ALL:
  1394. case RESET_TYPE_WORLD:
  1395. case RESET_TYPE_DISABLE:
  1396. method = type;
  1397. break;
  1398. case RESET_TYPE_RX_RECOVERY:
  1399. case RESET_TYPE_RX_DESC_FETCH:
  1400. case RESET_TYPE_TX_DESC_FETCH:
  1401. case RESET_TYPE_TX_SKIP:
  1402. method = RESET_TYPE_INVISIBLE;
  1403. break;
  1404. default:
  1405. method = RESET_TYPE_ALL;
  1406. break;
  1407. }
  1408. if (method != type)
  1409. EFX_LOG(efx, "scheduling reset (%d:%d)\n", type, method);
  1410. else
  1411. EFX_LOG(efx, "scheduling reset (%d)\n", method);
  1412. efx->reset_pending = method;
  1413. queue_work(reset_workqueue, &efx->reset_work);
  1414. }
  1415. /**************************************************************************
  1416. *
  1417. * List of NICs we support
  1418. *
  1419. **************************************************************************/
  1420. /* PCI device ID table */
  1421. static struct pci_device_id efx_pci_table[] __devinitdata = {
  1422. {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
  1423. .driver_data = (unsigned long) &falcon_a_nic_type},
  1424. {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
  1425. .driver_data = (unsigned long) &falcon_b_nic_type},
  1426. {0} /* end of list */
  1427. };
  1428. /**************************************************************************
  1429. *
  1430. * Dummy PHY/MAC/Board operations
  1431. *
  1432. * Can be used for some unimplemented operations
  1433. * Needed so all function pointers are valid and do not have to be tested
  1434. * before use
  1435. *
  1436. **************************************************************************/
  1437. int efx_port_dummy_op_int(struct efx_nic *efx)
  1438. {
  1439. return 0;
  1440. }
  1441. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  1442. void efx_port_dummy_op_blink(struct efx_nic *efx, bool blink) {}
  1443. static struct efx_phy_operations efx_dummy_phy_operations = {
  1444. .init = efx_port_dummy_op_int,
  1445. .reconfigure = efx_port_dummy_op_void,
  1446. .check_hw = efx_port_dummy_op_int,
  1447. .fini = efx_port_dummy_op_void,
  1448. .clear_interrupt = efx_port_dummy_op_void,
  1449. };
  1450. static struct efx_board efx_dummy_board_info = {
  1451. .init = efx_port_dummy_op_int,
  1452. .init_leds = efx_port_dummy_op_int,
  1453. .set_fault_led = efx_port_dummy_op_blink,
  1454. .monitor = efx_port_dummy_op_int,
  1455. .blink = efx_port_dummy_op_blink,
  1456. .fini = efx_port_dummy_op_void,
  1457. };
  1458. /**************************************************************************
  1459. *
  1460. * Data housekeeping
  1461. *
  1462. **************************************************************************/
  1463. /* This zeroes out and then fills in the invariants in a struct
  1464. * efx_nic (including all sub-structures).
  1465. */
  1466. static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
  1467. struct pci_dev *pci_dev, struct net_device *net_dev)
  1468. {
  1469. struct efx_channel *channel;
  1470. struct efx_tx_queue *tx_queue;
  1471. struct efx_rx_queue *rx_queue;
  1472. int i;
  1473. /* Initialise common structures */
  1474. memset(efx, 0, sizeof(*efx));
  1475. spin_lock_init(&efx->biu_lock);
  1476. spin_lock_init(&efx->phy_lock);
  1477. mutex_init(&efx->spi_lock);
  1478. INIT_WORK(&efx->reset_work, efx_reset_work);
  1479. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  1480. efx->pci_dev = pci_dev;
  1481. efx->state = STATE_INIT;
  1482. efx->reset_pending = RESET_TYPE_NONE;
  1483. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  1484. efx->board_info = efx_dummy_board_info;
  1485. efx->net_dev = net_dev;
  1486. efx->rx_checksum_enabled = true;
  1487. spin_lock_init(&efx->netif_stop_lock);
  1488. spin_lock_init(&efx->stats_lock);
  1489. mutex_init(&efx->mac_lock);
  1490. efx->phy_op = &efx_dummy_phy_operations;
  1491. efx->mii.dev = net_dev;
  1492. INIT_WORK(&efx->reconfigure_work, efx_reconfigure_work);
  1493. atomic_set(&efx->netif_stop_count, 1);
  1494. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  1495. channel = &efx->channel[i];
  1496. channel->efx = efx;
  1497. channel->channel = i;
  1498. channel->work_pending = false;
  1499. }
  1500. for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
  1501. tx_queue = &efx->tx_queue[i];
  1502. tx_queue->efx = efx;
  1503. tx_queue->queue = i;
  1504. tx_queue->buffer = NULL;
  1505. tx_queue->channel = &efx->channel[0]; /* for safety */
  1506. tx_queue->tso_headers_free = NULL;
  1507. }
  1508. for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
  1509. rx_queue = &efx->rx_queue[i];
  1510. rx_queue->efx = efx;
  1511. rx_queue->queue = i;
  1512. rx_queue->channel = &efx->channel[0]; /* for safety */
  1513. rx_queue->buffer = NULL;
  1514. spin_lock_init(&rx_queue->add_lock);
  1515. INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
  1516. }
  1517. efx->type = type;
  1518. /* Sanity-check NIC type */
  1519. EFX_BUG_ON_PARANOID(efx->type->txd_ring_mask &
  1520. (efx->type->txd_ring_mask + 1));
  1521. EFX_BUG_ON_PARANOID(efx->type->rxd_ring_mask &
  1522. (efx->type->rxd_ring_mask + 1));
  1523. EFX_BUG_ON_PARANOID(efx->type->evq_size &
  1524. (efx->type->evq_size - 1));
  1525. /* As close as we can get to guaranteeing that we don't overflow */
  1526. EFX_BUG_ON_PARANOID(efx->type->evq_size <
  1527. (efx->type->txd_ring_mask + 1 +
  1528. efx->type->rxd_ring_mask + 1));
  1529. EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
  1530. /* Higher numbered interrupt modes are less capable! */
  1531. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  1532. interrupt_mode);
  1533. efx->workqueue = create_singlethread_workqueue("sfc_work");
  1534. if (!efx->workqueue)
  1535. return -ENOMEM;
  1536. return 0;
  1537. }
  1538. static void efx_fini_struct(struct efx_nic *efx)
  1539. {
  1540. if (efx->workqueue) {
  1541. destroy_workqueue(efx->workqueue);
  1542. efx->workqueue = NULL;
  1543. }
  1544. }
  1545. /**************************************************************************
  1546. *
  1547. * PCI interface
  1548. *
  1549. **************************************************************************/
  1550. /* Main body of final NIC shutdown code
  1551. * This is called only at module unload (or hotplug removal).
  1552. */
  1553. static void efx_pci_remove_main(struct efx_nic *efx)
  1554. {
  1555. EFX_ASSERT_RESET_SERIALISED(efx);
  1556. /* Skip everything if we never obtained a valid membase */
  1557. if (!efx->membase)
  1558. return;
  1559. efx_fini_channels(efx);
  1560. efx_fini_port(efx);
  1561. /* Shutdown the board, then the NIC and board state */
  1562. efx->board_info.fini(efx);
  1563. falcon_fini_interrupt(efx);
  1564. efx_fini_napi(efx);
  1565. efx_remove_all(efx);
  1566. }
  1567. /* Final NIC shutdown
  1568. * This is called only at module unload (or hotplug removal).
  1569. */
  1570. static void efx_pci_remove(struct pci_dev *pci_dev)
  1571. {
  1572. struct efx_nic *efx;
  1573. efx = pci_get_drvdata(pci_dev);
  1574. if (!efx)
  1575. return;
  1576. efx_mtd_remove(efx);
  1577. /* Mark the NIC as fini, then stop the interface */
  1578. rtnl_lock();
  1579. efx->state = STATE_FINI;
  1580. dev_close(efx->net_dev);
  1581. /* Allow any queued efx_resets() to complete */
  1582. rtnl_unlock();
  1583. if (efx->membase == NULL)
  1584. goto out;
  1585. efx_unregister_netdev(efx);
  1586. /* Wait for any scheduled resets to complete. No more will be
  1587. * scheduled from this point because efx_stop_all() has been
  1588. * called, we are no longer registered with driverlink, and
  1589. * the net_device's have been removed. */
  1590. cancel_work_sync(&efx->reset_work);
  1591. efx_pci_remove_main(efx);
  1592. out:
  1593. efx_fini_io(efx);
  1594. EFX_LOG(efx, "shutdown successful\n");
  1595. pci_set_drvdata(pci_dev, NULL);
  1596. efx_fini_struct(efx);
  1597. free_netdev(efx->net_dev);
  1598. };
  1599. /* Main body of NIC initialisation
  1600. * This is called at module load (or hotplug insertion, theoretically).
  1601. */
  1602. static int efx_pci_probe_main(struct efx_nic *efx)
  1603. {
  1604. int rc;
  1605. /* Do start-of-day initialisation */
  1606. rc = efx_probe_all(efx);
  1607. if (rc)
  1608. goto fail1;
  1609. rc = efx_init_napi(efx);
  1610. if (rc)
  1611. goto fail2;
  1612. /* Initialise the board */
  1613. rc = efx->board_info.init(efx);
  1614. if (rc) {
  1615. EFX_ERR(efx, "failed to initialise board\n");
  1616. goto fail3;
  1617. }
  1618. rc = falcon_init_nic(efx);
  1619. if (rc) {
  1620. EFX_ERR(efx, "failed to initialise NIC\n");
  1621. goto fail4;
  1622. }
  1623. rc = efx_init_port(efx);
  1624. if (rc) {
  1625. EFX_ERR(efx, "failed to initialise port\n");
  1626. goto fail5;
  1627. }
  1628. efx_init_channels(efx);
  1629. rc = falcon_init_interrupt(efx);
  1630. if (rc)
  1631. goto fail6;
  1632. return 0;
  1633. fail6:
  1634. efx_fini_channels(efx);
  1635. efx_fini_port(efx);
  1636. fail5:
  1637. fail4:
  1638. efx->board_info.fini(efx);
  1639. fail3:
  1640. efx_fini_napi(efx);
  1641. fail2:
  1642. efx_remove_all(efx);
  1643. fail1:
  1644. return rc;
  1645. }
  1646. /* NIC initialisation
  1647. *
  1648. * This is called at module load (or hotplug insertion,
  1649. * theoretically). It sets up PCI mappings, tests and resets the NIC,
  1650. * sets up and registers the network devices with the kernel and hooks
  1651. * the interrupt service routine. It does not prepare the device for
  1652. * transmission; this is left to the first time one of the network
  1653. * interfaces is brought up (i.e. efx_net_open).
  1654. */
  1655. static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
  1656. const struct pci_device_id *entry)
  1657. {
  1658. struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
  1659. struct net_device *net_dev;
  1660. struct efx_nic *efx;
  1661. int i, rc;
  1662. /* Allocate and initialise a struct net_device and struct efx_nic */
  1663. net_dev = alloc_etherdev(sizeof(*efx));
  1664. if (!net_dev)
  1665. return -ENOMEM;
  1666. net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
  1667. NETIF_F_HIGHDMA | NETIF_F_TSO);
  1668. if (lro)
  1669. net_dev->features |= NETIF_F_LRO;
  1670. /* Mask for features that also apply to VLAN devices */
  1671. net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
  1672. NETIF_F_HIGHDMA | NETIF_F_TSO);
  1673. efx = netdev_priv(net_dev);
  1674. pci_set_drvdata(pci_dev, efx);
  1675. rc = efx_init_struct(efx, type, pci_dev, net_dev);
  1676. if (rc)
  1677. goto fail1;
  1678. EFX_INFO(efx, "Solarflare Communications NIC detected\n");
  1679. /* Set up basic I/O (BAR mappings etc) */
  1680. rc = efx_init_io(efx);
  1681. if (rc)
  1682. goto fail2;
  1683. /* No serialisation is required with the reset path because
  1684. * we're in STATE_INIT. */
  1685. for (i = 0; i < 5; i++) {
  1686. rc = efx_pci_probe_main(efx);
  1687. if (rc == 0)
  1688. break;
  1689. /* Serialise against efx_reset(). No more resets will be
  1690. * scheduled since efx_stop_all() has been called, and we
  1691. * have not and never have been registered with either
  1692. * the rtnetlink or driverlink layers. */
  1693. cancel_work_sync(&efx->reset_work);
  1694. /* Retry if a recoverably reset event has been scheduled */
  1695. if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
  1696. (efx->reset_pending != RESET_TYPE_ALL))
  1697. goto fail3;
  1698. efx->reset_pending = RESET_TYPE_NONE;
  1699. }
  1700. if (rc) {
  1701. EFX_ERR(efx, "Could not reset NIC\n");
  1702. goto fail4;
  1703. }
  1704. /* Switch to the running state before we expose the device to
  1705. * the OS. This is to ensure that the initial gathering of
  1706. * MAC stats succeeds. */
  1707. rtnl_lock();
  1708. efx->state = STATE_RUNNING;
  1709. rtnl_unlock();
  1710. rc = efx_register_netdev(efx);
  1711. if (rc)
  1712. goto fail5;
  1713. EFX_LOG(efx, "initialisation successful\n");
  1714. efx_mtd_probe(efx); /* allowed to fail */
  1715. return 0;
  1716. fail5:
  1717. efx_pci_remove_main(efx);
  1718. fail4:
  1719. fail3:
  1720. efx_fini_io(efx);
  1721. fail2:
  1722. efx_fini_struct(efx);
  1723. fail1:
  1724. EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
  1725. free_netdev(net_dev);
  1726. return rc;
  1727. }
  1728. static struct pci_driver efx_pci_driver = {
  1729. .name = EFX_DRIVER_NAME,
  1730. .id_table = efx_pci_table,
  1731. .probe = efx_pci_probe,
  1732. .remove = efx_pci_remove,
  1733. };
  1734. /**************************************************************************
  1735. *
  1736. * Kernel module interface
  1737. *
  1738. *************************************************************************/
  1739. module_param(interrupt_mode, uint, 0444);
  1740. MODULE_PARM_DESC(interrupt_mode,
  1741. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  1742. static int __init efx_init_module(void)
  1743. {
  1744. int rc;
  1745. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  1746. rc = register_netdevice_notifier(&efx_netdev_notifier);
  1747. if (rc)
  1748. goto err_notifier;
  1749. refill_workqueue = create_workqueue("sfc_refill");
  1750. if (!refill_workqueue) {
  1751. rc = -ENOMEM;
  1752. goto err_refill;
  1753. }
  1754. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  1755. if (!reset_workqueue) {
  1756. rc = -ENOMEM;
  1757. goto err_reset;
  1758. }
  1759. rc = pci_register_driver(&efx_pci_driver);
  1760. if (rc < 0)
  1761. goto err_pci;
  1762. return 0;
  1763. err_pci:
  1764. destroy_workqueue(reset_workqueue);
  1765. err_reset:
  1766. destroy_workqueue(refill_workqueue);
  1767. err_refill:
  1768. unregister_netdevice_notifier(&efx_netdev_notifier);
  1769. err_notifier:
  1770. return rc;
  1771. }
  1772. static void __exit efx_exit_module(void)
  1773. {
  1774. printk(KERN_INFO "Solarflare NET driver unloading\n");
  1775. pci_unregister_driver(&efx_pci_driver);
  1776. destroy_workqueue(reset_workqueue);
  1777. destroy_workqueue(refill_workqueue);
  1778. unregister_netdevice_notifier(&efx_netdev_notifier);
  1779. }
  1780. module_init(efx_init_module);
  1781. module_exit(efx_exit_module);
  1782. MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
  1783. "Solarflare Communications");
  1784. MODULE_DESCRIPTION("Solarflare Communications network driver");
  1785. MODULE_LICENSE("GPL");
  1786. MODULE_DEVICE_TABLE(pci, efx_pci_table);