efx.c 60 KB

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