efx.c 56 KB

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