efx.c 59 KB

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