efx.c 56 KB

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