efx.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2009 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 <linux/gfp.h>
  23. #include "net_driver.h"
  24. #include "efx.h"
  25. #include "mdio_10g.h"
  26. #include "nic.h"
  27. #include "mcdi.h"
  28. #include "workarounds.h"
  29. /**************************************************************************
  30. *
  31. * Type name strings
  32. *
  33. **************************************************************************
  34. */
  35. /* Loopback mode names (see LOOPBACK_MODE()) */
  36. const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
  37. const char *efx_loopback_mode_names[] = {
  38. [LOOPBACK_NONE] = "NONE",
  39. [LOOPBACK_DATA] = "DATAPATH",
  40. [LOOPBACK_GMAC] = "GMAC",
  41. [LOOPBACK_XGMII] = "XGMII",
  42. [LOOPBACK_XGXS] = "XGXS",
  43. [LOOPBACK_XAUI] = "XAUI",
  44. [LOOPBACK_GMII] = "GMII",
  45. [LOOPBACK_SGMII] = "SGMII",
  46. [LOOPBACK_XGBR] = "XGBR",
  47. [LOOPBACK_XFI] = "XFI",
  48. [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
  49. [LOOPBACK_GMII_FAR] = "GMII_FAR",
  50. [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
  51. [LOOPBACK_XFI_FAR] = "XFI_FAR",
  52. [LOOPBACK_GPHY] = "GPHY",
  53. [LOOPBACK_PHYXS] = "PHYXS",
  54. [LOOPBACK_PCS] = "PCS",
  55. [LOOPBACK_PMAPMD] = "PMA/PMD",
  56. [LOOPBACK_XPORT] = "XPORT",
  57. [LOOPBACK_XGMII_WS] = "XGMII_WS",
  58. [LOOPBACK_XAUI_WS] = "XAUI_WS",
  59. [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
  60. [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
  61. [LOOPBACK_GMII_WS] = "GMII_WS",
  62. [LOOPBACK_XFI_WS] = "XFI_WS",
  63. [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
  64. [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
  65. };
  66. const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
  67. const char *efx_reset_type_names[] = {
  68. [RESET_TYPE_INVISIBLE] = "INVISIBLE",
  69. [RESET_TYPE_ALL] = "ALL",
  70. [RESET_TYPE_WORLD] = "WORLD",
  71. [RESET_TYPE_DISABLE] = "DISABLE",
  72. [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
  73. [RESET_TYPE_INT_ERROR] = "INT_ERROR",
  74. [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
  75. [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
  76. [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
  77. [RESET_TYPE_TX_SKIP] = "TX_SKIP",
  78. [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
  79. };
  80. #define EFX_MAX_MTU (9 * 1024)
  81. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  82. * queued onto this work queue. This is not a per-nic work queue, because
  83. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  84. */
  85. static struct workqueue_struct *reset_workqueue;
  86. /**************************************************************************
  87. *
  88. * Configurable values
  89. *
  90. *************************************************************************/
  91. /*
  92. * Use separate channels for TX and RX events
  93. *
  94. * Set this to 1 to use separate channels for TX and RX. It allows us
  95. * to control interrupt affinity separately for TX and RX.
  96. *
  97. * This is only used in MSI-X interrupt mode
  98. */
  99. static unsigned int separate_tx_channels;
  100. module_param(separate_tx_channels, uint, 0444);
  101. MODULE_PARM_DESC(separate_tx_channels,
  102. "Use separate channels for TX and RX");
  103. /* This is the weight assigned to each of the (per-channel) virtual
  104. * NAPI devices.
  105. */
  106. static int napi_weight = 64;
  107. /* This is the time (in jiffies) between invocations of the hardware
  108. * monitor. On Falcon-based NICs, this will:
  109. * - Check the on-board hardware monitor;
  110. * - Poll the link state and reconfigure the hardware as necessary.
  111. */
  112. static unsigned int efx_monitor_interval = 1 * HZ;
  113. /* This controls whether or not the driver will initialise devices
  114. * with invalid MAC addresses stored in the EEPROM or flash. If true,
  115. * such devices will be initialised with a random locally-generated
  116. * MAC address. This allows for loading the sfc_mtd driver to
  117. * reprogram the flash, even if the flash contents (including the MAC
  118. * address) have previously been erased.
  119. */
  120. static unsigned int allow_bad_hwaddr;
  121. /* Initial interrupt moderation settings. They can be modified after
  122. * module load with ethtool.
  123. *
  124. * The default for RX should strike a balance between increasing the
  125. * round-trip latency and reducing overhead.
  126. */
  127. static unsigned int rx_irq_mod_usec = 60;
  128. /* Initial interrupt moderation settings. They can be modified after
  129. * module load with ethtool.
  130. *
  131. * This default is chosen to ensure that a 10G link does not go idle
  132. * while a TX queue is stopped after it has become full. A queue is
  133. * restarted when it drops below half full. The time this takes (assuming
  134. * worst case 3 descriptors per packet and 1024 descriptors) is
  135. * 512 / 3 * 1.2 = 205 usec.
  136. */
  137. static unsigned int tx_irq_mod_usec = 150;
  138. /* This is the first interrupt mode to try out of:
  139. * 0 => MSI-X
  140. * 1 => MSI
  141. * 2 => legacy
  142. */
  143. static unsigned int interrupt_mode;
  144. /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
  145. * i.e. the number of CPUs among which we may distribute simultaneous
  146. * interrupt handling.
  147. *
  148. * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
  149. * The default (0) means to assign an interrupt to each package (level II cache)
  150. */
  151. static unsigned int rss_cpus;
  152. module_param(rss_cpus, uint, 0444);
  153. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  154. static int phy_flash_cfg;
  155. module_param(phy_flash_cfg, int, 0644);
  156. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  157. static unsigned irq_adapt_low_thresh = 10000;
  158. module_param(irq_adapt_low_thresh, uint, 0644);
  159. MODULE_PARM_DESC(irq_adapt_low_thresh,
  160. "Threshold score for reducing IRQ moderation");
  161. static unsigned irq_adapt_high_thresh = 20000;
  162. module_param(irq_adapt_high_thresh, uint, 0644);
  163. MODULE_PARM_DESC(irq_adapt_high_thresh,
  164. "Threshold score for increasing IRQ moderation");
  165. static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  166. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  167. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  168. NETIF_MSG_TX_ERR | NETIF_MSG_HW);
  169. module_param(debug, uint, 0);
  170. MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
  171. /**************************************************************************
  172. *
  173. * Utility functions and prototypes
  174. *
  175. *************************************************************************/
  176. static void efx_remove_channels(struct efx_nic *efx);
  177. static void efx_remove_port(struct efx_nic *efx);
  178. static void efx_fini_napi(struct efx_nic *efx);
  179. static void efx_fini_struct(struct efx_nic *efx);
  180. static void efx_start_all(struct efx_nic *efx);
  181. static void efx_stop_all(struct efx_nic *efx);
  182. #define EFX_ASSERT_RESET_SERIALISED(efx) \
  183. do { \
  184. if ((efx->state == STATE_RUNNING) || \
  185. (efx->state == STATE_DISABLED)) \
  186. ASSERT_RTNL(); \
  187. } while (0)
  188. /**************************************************************************
  189. *
  190. * Event queue processing
  191. *
  192. *************************************************************************/
  193. /* Process channel's event queue
  194. *
  195. * This function is responsible for processing the event queue of a
  196. * single channel. The caller must guarantee that this function will
  197. * never be concurrently called more than once on the same channel,
  198. * though different channels may be being processed concurrently.
  199. */
  200. static int efx_process_channel(struct efx_channel *channel, int budget)
  201. {
  202. struct efx_nic *efx = channel->efx;
  203. int spent;
  204. if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
  205. !channel->enabled))
  206. return 0;
  207. spent = efx_nic_process_eventq(channel, budget);
  208. if (spent == 0)
  209. return 0;
  210. /* Deliver last RX packet. */
  211. if (channel->rx_pkt) {
  212. __efx_rx_packet(channel, channel->rx_pkt,
  213. channel->rx_pkt_csummed);
  214. channel->rx_pkt = NULL;
  215. }
  216. efx_rx_strategy(channel);
  217. efx_fast_push_rx_descriptors(efx_channel_get_rx_queue(channel));
  218. return spent;
  219. }
  220. /* Mark channel as finished processing
  221. *
  222. * Note that since we will not receive further interrupts for this
  223. * channel before we finish processing and call the eventq_read_ack()
  224. * method, there is no need to use the interrupt hold-off timers.
  225. */
  226. static inline void efx_channel_processed(struct efx_channel *channel)
  227. {
  228. /* The interrupt handler for this channel may set work_pending
  229. * as soon as we acknowledge the events we've seen. Make sure
  230. * it's cleared before then. */
  231. channel->work_pending = false;
  232. smp_wmb();
  233. efx_nic_eventq_read_ack(channel);
  234. }
  235. /* NAPI poll handler
  236. *
  237. * NAPI guarantees serialisation of polls of the same device, which
  238. * provides the guarantee required by efx_process_channel().
  239. */
  240. static int efx_poll(struct napi_struct *napi, int budget)
  241. {
  242. struct efx_channel *channel =
  243. container_of(napi, struct efx_channel, napi_str);
  244. struct efx_nic *efx = channel->efx;
  245. int spent;
  246. netif_vdbg(efx, intr, efx->net_dev,
  247. "channel %d NAPI poll executing on CPU %d\n",
  248. channel->channel, raw_smp_processor_id());
  249. spent = efx_process_channel(channel, budget);
  250. if (spent < budget) {
  251. if (channel->channel < efx->n_rx_channels &&
  252. efx->irq_rx_adaptive &&
  253. unlikely(++channel->irq_count == 1000)) {
  254. if (unlikely(channel->irq_mod_score <
  255. irq_adapt_low_thresh)) {
  256. if (channel->irq_moderation > 1) {
  257. channel->irq_moderation -= 1;
  258. efx->type->push_irq_moderation(channel);
  259. }
  260. } else if (unlikely(channel->irq_mod_score >
  261. irq_adapt_high_thresh)) {
  262. if (channel->irq_moderation <
  263. efx->irq_rx_moderation) {
  264. channel->irq_moderation += 1;
  265. efx->type->push_irq_moderation(channel);
  266. }
  267. }
  268. channel->irq_count = 0;
  269. channel->irq_mod_score = 0;
  270. }
  271. /* There is no race here; although napi_disable() will
  272. * only wait for napi_complete(), this isn't a problem
  273. * since efx_channel_processed() will have no effect if
  274. * interrupts have already been disabled.
  275. */
  276. napi_complete(napi);
  277. efx_channel_processed(channel);
  278. }
  279. return spent;
  280. }
  281. /* Process the eventq of the specified channel immediately on this CPU
  282. *
  283. * Disable hardware generated interrupts, wait for any existing
  284. * processing to finish, then directly poll (and ack ) the eventq.
  285. * Finally reenable NAPI and interrupts.
  286. *
  287. * Since we are touching interrupts the caller should hold the suspend lock
  288. */
  289. void efx_process_channel_now(struct efx_channel *channel)
  290. {
  291. struct efx_nic *efx = channel->efx;
  292. BUG_ON(channel->channel >= efx->n_channels);
  293. BUG_ON(!channel->enabled);
  294. /* Disable interrupts and wait for ISRs to complete */
  295. efx_nic_disable_interrupts(efx);
  296. if (efx->legacy_irq)
  297. synchronize_irq(efx->legacy_irq);
  298. if (channel->irq)
  299. synchronize_irq(channel->irq);
  300. /* Wait for any NAPI processing to complete */
  301. napi_disable(&channel->napi_str);
  302. /* Poll the channel */
  303. efx_process_channel(channel, channel->eventq_mask + 1);
  304. /* Ack the eventq. This may cause an interrupt to be generated
  305. * when they are reenabled */
  306. efx_channel_processed(channel);
  307. napi_enable(&channel->napi_str);
  308. efx_nic_enable_interrupts(efx);
  309. }
  310. /* Create event queue
  311. * Event queue memory allocations are done only once. If the channel
  312. * is reset, the memory buffer will be reused; this guards against
  313. * errors during channel reset and also simplifies interrupt handling.
  314. */
  315. static int efx_probe_eventq(struct efx_channel *channel)
  316. {
  317. struct efx_nic *efx = channel->efx;
  318. unsigned long entries;
  319. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  320. "chan %d create event queue\n", channel->channel);
  321. /* Build an event queue with room for one event per tx and rx buffer,
  322. * plus some extra for link state events and MCDI completions. */
  323. entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
  324. EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
  325. channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
  326. return efx_nic_probe_eventq(channel);
  327. }
  328. /* Prepare channel's event queue */
  329. static void efx_init_eventq(struct efx_channel *channel)
  330. {
  331. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  332. "chan %d init event queue\n", channel->channel);
  333. channel->eventq_read_ptr = 0;
  334. efx_nic_init_eventq(channel);
  335. }
  336. static void efx_fini_eventq(struct efx_channel *channel)
  337. {
  338. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  339. "chan %d fini event queue\n", channel->channel);
  340. efx_nic_fini_eventq(channel);
  341. }
  342. static void efx_remove_eventq(struct efx_channel *channel)
  343. {
  344. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  345. "chan %d remove event queue\n", channel->channel);
  346. efx_nic_remove_eventq(channel);
  347. }
  348. /**************************************************************************
  349. *
  350. * Channel handling
  351. *
  352. *************************************************************************/
  353. /* Allocate and initialise a channel structure, optionally copying
  354. * parameters (but not resources) from an old channel structure. */
  355. static struct efx_channel *
  356. efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
  357. {
  358. struct efx_channel *channel;
  359. struct efx_rx_queue *rx_queue;
  360. struct efx_tx_queue *tx_queue;
  361. int j;
  362. if (old_channel) {
  363. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  364. if (!channel)
  365. return NULL;
  366. *channel = *old_channel;
  367. memset(&channel->eventq, 0, sizeof(channel->eventq));
  368. rx_queue = &channel->rx_queue;
  369. rx_queue->buffer = NULL;
  370. memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
  371. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  372. tx_queue = &channel->tx_queue[j];
  373. if (tx_queue->channel)
  374. tx_queue->channel = channel;
  375. tx_queue->buffer = NULL;
  376. memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
  377. }
  378. } else {
  379. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  380. if (!channel)
  381. return NULL;
  382. channel->efx = efx;
  383. channel->channel = i;
  384. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  385. tx_queue = &channel->tx_queue[j];
  386. tx_queue->efx = efx;
  387. tx_queue->queue = i * EFX_TXQ_TYPES + j;
  388. tx_queue->channel = channel;
  389. }
  390. }
  391. spin_lock_init(&channel->tx_stop_lock);
  392. atomic_set(&channel->tx_stop_count, 1);
  393. rx_queue = &channel->rx_queue;
  394. rx_queue->efx = efx;
  395. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  396. (unsigned long)rx_queue);
  397. return channel;
  398. }
  399. static int efx_probe_channel(struct efx_channel *channel)
  400. {
  401. struct efx_tx_queue *tx_queue;
  402. struct efx_rx_queue *rx_queue;
  403. int rc;
  404. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  405. "creating channel %d\n", channel->channel);
  406. rc = efx_probe_eventq(channel);
  407. if (rc)
  408. goto fail1;
  409. efx_for_each_channel_tx_queue(tx_queue, channel) {
  410. rc = efx_probe_tx_queue(tx_queue);
  411. if (rc)
  412. goto fail2;
  413. }
  414. efx_for_each_channel_rx_queue(rx_queue, channel) {
  415. rc = efx_probe_rx_queue(rx_queue);
  416. if (rc)
  417. goto fail3;
  418. }
  419. channel->n_rx_frm_trunc = 0;
  420. return 0;
  421. fail3:
  422. efx_for_each_channel_rx_queue(rx_queue, channel)
  423. efx_remove_rx_queue(rx_queue);
  424. fail2:
  425. efx_for_each_channel_tx_queue(tx_queue, channel)
  426. efx_remove_tx_queue(tx_queue);
  427. fail1:
  428. return rc;
  429. }
  430. static void efx_set_channel_names(struct efx_nic *efx)
  431. {
  432. struct efx_channel *channel;
  433. const char *type = "";
  434. int number;
  435. efx_for_each_channel(channel, efx) {
  436. number = channel->channel;
  437. if (efx->n_channels > efx->n_rx_channels) {
  438. if (channel->channel < efx->n_rx_channels) {
  439. type = "-rx";
  440. } else {
  441. type = "-tx";
  442. number -= efx->n_rx_channels;
  443. }
  444. }
  445. snprintf(efx->channel_name[channel->channel],
  446. sizeof(efx->channel_name[0]),
  447. "%s%s-%d", efx->name, type, number);
  448. }
  449. }
  450. static int efx_probe_channels(struct efx_nic *efx)
  451. {
  452. struct efx_channel *channel;
  453. int rc;
  454. /* Restart special buffer allocation */
  455. efx->next_buffer_table = 0;
  456. efx_for_each_channel(channel, efx) {
  457. rc = efx_probe_channel(channel);
  458. if (rc) {
  459. netif_err(efx, probe, efx->net_dev,
  460. "failed to create channel %d\n",
  461. channel->channel);
  462. goto fail;
  463. }
  464. }
  465. efx_set_channel_names(efx);
  466. return 0;
  467. fail:
  468. efx_remove_channels(efx);
  469. return rc;
  470. }
  471. /* Channels are shutdown and reinitialised whilst the NIC is running
  472. * to propagate configuration changes (mtu, checksum offload), or
  473. * to clear hardware error conditions
  474. */
  475. static void efx_init_channels(struct efx_nic *efx)
  476. {
  477. struct efx_tx_queue *tx_queue;
  478. struct efx_rx_queue *rx_queue;
  479. struct efx_channel *channel;
  480. /* Calculate the rx buffer allocation parameters required to
  481. * support the current MTU, including padding for header
  482. * alignment and overruns.
  483. */
  484. efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
  485. EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
  486. efx->type->rx_buffer_hash_size +
  487. efx->type->rx_buffer_padding);
  488. efx->rx_buffer_order = get_order(efx->rx_buffer_len +
  489. sizeof(struct efx_rx_page_state));
  490. /* Initialise the channels */
  491. efx_for_each_channel(channel, efx) {
  492. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  493. "init chan %d\n", channel->channel);
  494. efx_init_eventq(channel);
  495. efx_for_each_channel_tx_queue(tx_queue, channel)
  496. efx_init_tx_queue(tx_queue);
  497. /* The rx buffer allocation strategy is MTU dependent */
  498. efx_rx_strategy(channel);
  499. efx_for_each_channel_rx_queue(rx_queue, channel)
  500. efx_init_rx_queue(rx_queue);
  501. WARN_ON(channel->rx_pkt != NULL);
  502. efx_rx_strategy(channel);
  503. }
  504. }
  505. /* This enables event queue processing and packet transmission.
  506. *
  507. * Note that this function is not allowed to fail, since that would
  508. * introduce too much complexity into the suspend/resume path.
  509. */
  510. static void efx_start_channel(struct efx_channel *channel)
  511. {
  512. struct efx_rx_queue *rx_queue;
  513. netif_dbg(channel->efx, ifup, channel->efx->net_dev,
  514. "starting chan %d\n", channel->channel);
  515. /* The interrupt handler for this channel may set work_pending
  516. * as soon as we enable it. Make sure it's cleared before
  517. * then. Similarly, make sure it sees the enabled flag set. */
  518. channel->work_pending = false;
  519. channel->enabled = true;
  520. smp_wmb();
  521. /* Fill the queues before enabling NAPI */
  522. efx_for_each_channel_rx_queue(rx_queue, channel)
  523. efx_fast_push_rx_descriptors(rx_queue);
  524. napi_enable(&channel->napi_str);
  525. }
  526. /* This disables event queue processing and packet transmission.
  527. * This function does not guarantee that all queue processing
  528. * (e.g. RX refill) is complete.
  529. */
  530. static void efx_stop_channel(struct efx_channel *channel)
  531. {
  532. if (!channel->enabled)
  533. return;
  534. netif_dbg(channel->efx, ifdown, channel->efx->net_dev,
  535. "stop chan %d\n", channel->channel);
  536. channel->enabled = false;
  537. napi_disable(&channel->napi_str);
  538. }
  539. static void efx_fini_channels(struct efx_nic *efx)
  540. {
  541. struct efx_channel *channel;
  542. struct efx_tx_queue *tx_queue;
  543. struct efx_rx_queue *rx_queue;
  544. int rc;
  545. EFX_ASSERT_RESET_SERIALISED(efx);
  546. BUG_ON(efx->port_enabled);
  547. rc = efx_nic_flush_queues(efx);
  548. if (rc && EFX_WORKAROUND_7803(efx)) {
  549. /* Schedule a reset to recover from the flush failure. The
  550. * descriptor caches reference memory we're about to free,
  551. * but falcon_reconfigure_mac_wrapper() won't reconnect
  552. * the MACs because of the pending reset. */
  553. netif_err(efx, drv, efx->net_dev,
  554. "Resetting to recover from flush failure\n");
  555. efx_schedule_reset(efx, RESET_TYPE_ALL);
  556. } else if (rc) {
  557. netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
  558. } else {
  559. netif_dbg(efx, drv, efx->net_dev,
  560. "successfully flushed all queues\n");
  561. }
  562. efx_for_each_channel(channel, efx) {
  563. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  564. "shut down chan %d\n", channel->channel);
  565. efx_for_each_channel_rx_queue(rx_queue, channel)
  566. efx_fini_rx_queue(rx_queue);
  567. efx_for_each_channel_tx_queue(tx_queue, channel)
  568. efx_fini_tx_queue(tx_queue);
  569. efx_fini_eventq(channel);
  570. }
  571. }
  572. static void efx_remove_channel(struct efx_channel *channel)
  573. {
  574. struct efx_tx_queue *tx_queue;
  575. struct efx_rx_queue *rx_queue;
  576. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  577. "destroy chan %d\n", channel->channel);
  578. efx_for_each_channel_rx_queue(rx_queue, channel)
  579. efx_remove_rx_queue(rx_queue);
  580. efx_for_each_channel_tx_queue(tx_queue, channel)
  581. efx_remove_tx_queue(tx_queue);
  582. efx_remove_eventq(channel);
  583. }
  584. static void efx_remove_channels(struct efx_nic *efx)
  585. {
  586. struct efx_channel *channel;
  587. efx_for_each_channel(channel, efx)
  588. efx_remove_channel(channel);
  589. }
  590. int
  591. efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
  592. {
  593. struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
  594. u32 old_rxq_entries, old_txq_entries;
  595. unsigned i;
  596. int rc;
  597. efx_stop_all(efx);
  598. efx_fini_channels(efx);
  599. /* Clone channels */
  600. memset(other_channel, 0, sizeof(other_channel));
  601. for (i = 0; i < efx->n_channels; i++) {
  602. channel = efx_alloc_channel(efx, i, efx->channel[i]);
  603. if (!channel) {
  604. rc = -ENOMEM;
  605. goto out;
  606. }
  607. other_channel[i] = channel;
  608. }
  609. /* Swap entry counts and channel pointers */
  610. old_rxq_entries = efx->rxq_entries;
  611. old_txq_entries = efx->txq_entries;
  612. efx->rxq_entries = rxq_entries;
  613. efx->txq_entries = txq_entries;
  614. for (i = 0; i < efx->n_channels; i++) {
  615. channel = efx->channel[i];
  616. efx->channel[i] = other_channel[i];
  617. other_channel[i] = channel;
  618. }
  619. rc = efx_probe_channels(efx);
  620. if (rc)
  621. goto rollback;
  622. /* Destroy old channels */
  623. for (i = 0; i < efx->n_channels; i++)
  624. efx_remove_channel(other_channel[i]);
  625. out:
  626. /* Free unused channel structures */
  627. for (i = 0; i < efx->n_channels; i++)
  628. kfree(other_channel[i]);
  629. efx_init_channels(efx);
  630. efx_start_all(efx);
  631. return rc;
  632. rollback:
  633. /* Swap back */
  634. efx->rxq_entries = old_rxq_entries;
  635. efx->txq_entries = old_txq_entries;
  636. for (i = 0; i < efx->n_channels; i++) {
  637. channel = efx->channel[i];
  638. efx->channel[i] = other_channel[i];
  639. other_channel[i] = channel;
  640. }
  641. goto out;
  642. }
  643. void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
  644. {
  645. mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
  646. }
  647. /**************************************************************************
  648. *
  649. * Port handling
  650. *
  651. **************************************************************************/
  652. /* This ensures that the kernel is kept informed (via
  653. * netif_carrier_on/off) of the link status, and also maintains the
  654. * link status's stop on the port's TX queue.
  655. */
  656. void efx_link_status_changed(struct efx_nic *efx)
  657. {
  658. struct efx_link_state *link_state = &efx->link_state;
  659. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  660. * that no events are triggered between unregister_netdev() and the
  661. * driver unloading. A more general condition is that NETDEV_CHANGE
  662. * can only be generated between NETDEV_UP and NETDEV_DOWN */
  663. if (!netif_running(efx->net_dev))
  664. return;
  665. if (efx->port_inhibited) {
  666. netif_carrier_off(efx->net_dev);
  667. return;
  668. }
  669. if (link_state->up != netif_carrier_ok(efx->net_dev)) {
  670. efx->n_link_state_changes++;
  671. if (link_state->up)
  672. netif_carrier_on(efx->net_dev);
  673. else
  674. netif_carrier_off(efx->net_dev);
  675. }
  676. /* Status message for kernel log */
  677. if (link_state->up) {
  678. netif_info(efx, link, efx->net_dev,
  679. "link up at %uMbps %s-duplex (MTU %d)%s\n",
  680. link_state->speed, link_state->fd ? "full" : "half",
  681. efx->net_dev->mtu,
  682. (efx->promiscuous ? " [PROMISC]" : ""));
  683. } else {
  684. netif_info(efx, link, efx->net_dev, "link down\n");
  685. }
  686. }
  687. void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
  688. {
  689. efx->link_advertising = advertising;
  690. if (advertising) {
  691. if (advertising & ADVERTISED_Pause)
  692. efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
  693. else
  694. efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
  695. if (advertising & ADVERTISED_Asym_Pause)
  696. efx->wanted_fc ^= EFX_FC_TX;
  697. }
  698. }
  699. void efx_link_set_wanted_fc(struct efx_nic *efx, enum efx_fc_type wanted_fc)
  700. {
  701. efx->wanted_fc = wanted_fc;
  702. if (efx->link_advertising) {
  703. if (wanted_fc & EFX_FC_RX)
  704. efx->link_advertising |= (ADVERTISED_Pause |
  705. ADVERTISED_Asym_Pause);
  706. else
  707. efx->link_advertising &= ~(ADVERTISED_Pause |
  708. ADVERTISED_Asym_Pause);
  709. if (wanted_fc & EFX_FC_TX)
  710. efx->link_advertising ^= ADVERTISED_Asym_Pause;
  711. }
  712. }
  713. static void efx_fini_port(struct efx_nic *efx);
  714. /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
  715. * the MAC appropriately. All other PHY configuration changes are pushed
  716. * through phy_op->set_settings(), and pushed asynchronously to the MAC
  717. * through efx_monitor().
  718. *
  719. * Callers must hold the mac_lock
  720. */
  721. int __efx_reconfigure_port(struct efx_nic *efx)
  722. {
  723. enum efx_phy_mode phy_mode;
  724. int rc;
  725. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  726. /* Serialise the promiscuous flag with efx_set_multicast_list. */
  727. if (efx_dev_registered(efx)) {
  728. netif_addr_lock_bh(efx->net_dev);
  729. netif_addr_unlock_bh(efx->net_dev);
  730. }
  731. /* Disable PHY transmit in mac level loopbacks */
  732. phy_mode = efx->phy_mode;
  733. if (LOOPBACK_INTERNAL(efx))
  734. efx->phy_mode |= PHY_MODE_TX_DISABLED;
  735. else
  736. efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
  737. rc = efx->type->reconfigure_port(efx);
  738. if (rc)
  739. efx->phy_mode = phy_mode;
  740. return rc;
  741. }
  742. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  743. * disabled. */
  744. int efx_reconfigure_port(struct efx_nic *efx)
  745. {
  746. int rc;
  747. EFX_ASSERT_RESET_SERIALISED(efx);
  748. mutex_lock(&efx->mac_lock);
  749. rc = __efx_reconfigure_port(efx);
  750. mutex_unlock(&efx->mac_lock);
  751. return rc;
  752. }
  753. /* Asynchronous work item for changing MAC promiscuity and multicast
  754. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  755. * MAC directly. */
  756. static void efx_mac_work(struct work_struct *data)
  757. {
  758. struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
  759. mutex_lock(&efx->mac_lock);
  760. if (efx->port_enabled) {
  761. efx->type->push_multicast_hash(efx);
  762. efx->mac_op->reconfigure(efx);
  763. }
  764. mutex_unlock(&efx->mac_lock);
  765. }
  766. static int efx_probe_port(struct efx_nic *efx)
  767. {
  768. int rc;
  769. netif_dbg(efx, probe, efx->net_dev, "create port\n");
  770. if (phy_flash_cfg)
  771. efx->phy_mode = PHY_MODE_SPECIAL;
  772. /* Connect up MAC/PHY operations table */
  773. rc = efx->type->probe_port(efx);
  774. if (rc)
  775. return rc;
  776. /* Sanity check MAC address */
  777. if (is_valid_ether_addr(efx->mac_address)) {
  778. memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
  779. } else {
  780. netif_err(efx, probe, efx->net_dev, "invalid MAC address %pM\n",
  781. efx->mac_address);
  782. if (!allow_bad_hwaddr) {
  783. rc = -EINVAL;
  784. goto err;
  785. }
  786. random_ether_addr(efx->net_dev->dev_addr);
  787. netif_info(efx, probe, efx->net_dev,
  788. "using locally-generated MAC %pM\n",
  789. efx->net_dev->dev_addr);
  790. }
  791. return 0;
  792. err:
  793. efx->type->remove_port(efx);
  794. return rc;
  795. }
  796. static int efx_init_port(struct efx_nic *efx)
  797. {
  798. int rc;
  799. netif_dbg(efx, drv, efx->net_dev, "init port\n");
  800. mutex_lock(&efx->mac_lock);
  801. rc = efx->phy_op->init(efx);
  802. if (rc)
  803. goto fail1;
  804. efx->port_initialized = true;
  805. /* Reconfigure the MAC before creating dma queues (required for
  806. * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
  807. efx->mac_op->reconfigure(efx);
  808. /* Ensure the PHY advertises the correct flow control settings */
  809. rc = efx->phy_op->reconfigure(efx);
  810. if (rc)
  811. goto fail2;
  812. mutex_unlock(&efx->mac_lock);
  813. return 0;
  814. fail2:
  815. efx->phy_op->fini(efx);
  816. fail1:
  817. mutex_unlock(&efx->mac_lock);
  818. return rc;
  819. }
  820. static void efx_start_port(struct efx_nic *efx)
  821. {
  822. netif_dbg(efx, ifup, efx->net_dev, "start port\n");
  823. BUG_ON(efx->port_enabled);
  824. mutex_lock(&efx->mac_lock);
  825. efx->port_enabled = true;
  826. /* efx_mac_work() might have been scheduled after efx_stop_port(),
  827. * and then cancelled by efx_flush_all() */
  828. efx->type->push_multicast_hash(efx);
  829. efx->mac_op->reconfigure(efx);
  830. mutex_unlock(&efx->mac_lock);
  831. }
  832. /* Prevent efx_mac_work() and efx_monitor() from working */
  833. static void efx_stop_port(struct efx_nic *efx)
  834. {
  835. netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
  836. mutex_lock(&efx->mac_lock);
  837. efx->port_enabled = false;
  838. mutex_unlock(&efx->mac_lock);
  839. /* Serialise against efx_set_multicast_list() */
  840. if (efx_dev_registered(efx)) {
  841. netif_addr_lock_bh(efx->net_dev);
  842. netif_addr_unlock_bh(efx->net_dev);
  843. }
  844. }
  845. static void efx_fini_port(struct efx_nic *efx)
  846. {
  847. netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
  848. if (!efx->port_initialized)
  849. return;
  850. efx->phy_op->fini(efx);
  851. efx->port_initialized = false;
  852. efx->link_state.up = false;
  853. efx_link_status_changed(efx);
  854. }
  855. static void efx_remove_port(struct efx_nic *efx)
  856. {
  857. netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
  858. efx->type->remove_port(efx);
  859. }
  860. /**************************************************************************
  861. *
  862. * NIC handling
  863. *
  864. **************************************************************************/
  865. /* This configures the PCI device to enable I/O and DMA. */
  866. static int efx_init_io(struct efx_nic *efx)
  867. {
  868. struct pci_dev *pci_dev = efx->pci_dev;
  869. dma_addr_t dma_mask = efx->type->max_dma_mask;
  870. int rc;
  871. netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
  872. rc = pci_enable_device(pci_dev);
  873. if (rc) {
  874. netif_err(efx, probe, efx->net_dev,
  875. "failed to enable PCI device\n");
  876. goto fail1;
  877. }
  878. pci_set_master(pci_dev);
  879. /* Set the PCI DMA mask. Try all possibilities from our
  880. * genuine mask down to 32 bits, because some architectures
  881. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  882. * masks event though they reject 46 bit masks.
  883. */
  884. while (dma_mask > 0x7fffffffUL) {
  885. if (pci_dma_supported(pci_dev, dma_mask) &&
  886. ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
  887. break;
  888. dma_mask >>= 1;
  889. }
  890. if (rc) {
  891. netif_err(efx, probe, efx->net_dev,
  892. "could not find a suitable DMA mask\n");
  893. goto fail2;
  894. }
  895. netif_dbg(efx, probe, efx->net_dev,
  896. "using DMA mask %llx\n", (unsigned long long) dma_mask);
  897. rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
  898. if (rc) {
  899. /* pci_set_consistent_dma_mask() is not *allowed* to
  900. * fail with a mask that pci_set_dma_mask() accepted,
  901. * but just in case...
  902. */
  903. netif_err(efx, probe, efx->net_dev,
  904. "failed to set consistent DMA mask\n");
  905. goto fail2;
  906. }
  907. efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
  908. rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
  909. if (rc) {
  910. netif_err(efx, probe, efx->net_dev,
  911. "request for memory BAR failed\n");
  912. rc = -EIO;
  913. goto fail3;
  914. }
  915. efx->membase = ioremap_nocache(efx->membase_phys,
  916. efx->type->mem_map_size);
  917. if (!efx->membase) {
  918. netif_err(efx, probe, efx->net_dev,
  919. "could not map memory BAR at %llx+%x\n",
  920. (unsigned long long)efx->membase_phys,
  921. efx->type->mem_map_size);
  922. rc = -ENOMEM;
  923. goto fail4;
  924. }
  925. netif_dbg(efx, probe, efx->net_dev,
  926. "memory BAR at %llx+%x (virtual %p)\n",
  927. (unsigned long long)efx->membase_phys,
  928. efx->type->mem_map_size, efx->membase);
  929. return 0;
  930. fail4:
  931. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  932. fail3:
  933. efx->membase_phys = 0;
  934. fail2:
  935. pci_disable_device(efx->pci_dev);
  936. fail1:
  937. return rc;
  938. }
  939. static void efx_fini_io(struct efx_nic *efx)
  940. {
  941. netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
  942. if (efx->membase) {
  943. iounmap(efx->membase);
  944. efx->membase = NULL;
  945. }
  946. if (efx->membase_phys) {
  947. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  948. efx->membase_phys = 0;
  949. }
  950. pci_disable_device(efx->pci_dev);
  951. }
  952. /* Get number of channels wanted. Each channel will have its own IRQ,
  953. * 1 RX queue and/or 2 TX queues. */
  954. static int efx_wanted_channels(void)
  955. {
  956. cpumask_var_t core_mask;
  957. int count;
  958. int cpu;
  959. if (unlikely(!zalloc_cpumask_var(&core_mask, GFP_KERNEL))) {
  960. printk(KERN_WARNING
  961. "sfc: RSS disabled due to allocation failure\n");
  962. return 1;
  963. }
  964. count = 0;
  965. for_each_online_cpu(cpu) {
  966. if (!cpumask_test_cpu(cpu, core_mask)) {
  967. ++count;
  968. cpumask_or(core_mask, core_mask,
  969. topology_core_cpumask(cpu));
  970. }
  971. }
  972. free_cpumask_var(core_mask);
  973. return count;
  974. }
  975. /* Probe the number and type of interrupts we are able to obtain, and
  976. * the resulting numbers of channels and RX queues.
  977. */
  978. static void efx_probe_interrupts(struct efx_nic *efx)
  979. {
  980. int max_channels =
  981. min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
  982. int rc, i;
  983. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  984. struct msix_entry xentries[EFX_MAX_CHANNELS];
  985. int n_channels;
  986. n_channels = efx_wanted_channels();
  987. if (separate_tx_channels)
  988. n_channels *= 2;
  989. n_channels = min(n_channels, max_channels);
  990. for (i = 0; i < n_channels; i++)
  991. xentries[i].entry = i;
  992. rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
  993. if (rc > 0) {
  994. netif_err(efx, drv, efx->net_dev,
  995. "WARNING: Insufficient MSI-X vectors"
  996. " available (%d < %d).\n", rc, n_channels);
  997. netif_err(efx, drv, efx->net_dev,
  998. "WARNING: Performance may be reduced.\n");
  999. EFX_BUG_ON_PARANOID(rc >= n_channels);
  1000. n_channels = rc;
  1001. rc = pci_enable_msix(efx->pci_dev, xentries,
  1002. n_channels);
  1003. }
  1004. if (rc == 0) {
  1005. efx->n_channels = n_channels;
  1006. if (separate_tx_channels) {
  1007. efx->n_tx_channels =
  1008. max(efx->n_channels / 2, 1U);
  1009. efx->n_rx_channels =
  1010. max(efx->n_channels -
  1011. efx->n_tx_channels, 1U);
  1012. } else {
  1013. efx->n_tx_channels = efx->n_channels;
  1014. efx->n_rx_channels = efx->n_channels;
  1015. }
  1016. for (i = 0; i < n_channels; i++)
  1017. efx_get_channel(efx, i)->irq =
  1018. xentries[i].vector;
  1019. } else {
  1020. /* Fall back to single channel MSI */
  1021. efx->interrupt_mode = EFX_INT_MODE_MSI;
  1022. netif_err(efx, drv, efx->net_dev,
  1023. "could not enable MSI-X\n");
  1024. }
  1025. }
  1026. /* Try single interrupt MSI */
  1027. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  1028. efx->n_channels = 1;
  1029. efx->n_rx_channels = 1;
  1030. efx->n_tx_channels = 1;
  1031. rc = pci_enable_msi(efx->pci_dev);
  1032. if (rc == 0) {
  1033. efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
  1034. } else {
  1035. netif_err(efx, drv, efx->net_dev,
  1036. "could not enable MSI\n");
  1037. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  1038. }
  1039. }
  1040. /* Assume legacy interrupts */
  1041. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  1042. efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
  1043. efx->n_rx_channels = 1;
  1044. efx->n_tx_channels = 1;
  1045. efx->legacy_irq = efx->pci_dev->irq;
  1046. }
  1047. }
  1048. static void efx_remove_interrupts(struct efx_nic *efx)
  1049. {
  1050. struct efx_channel *channel;
  1051. /* Remove MSI/MSI-X interrupts */
  1052. efx_for_each_channel(channel, efx)
  1053. channel->irq = 0;
  1054. pci_disable_msi(efx->pci_dev);
  1055. pci_disable_msix(efx->pci_dev);
  1056. /* Remove legacy interrupt */
  1057. efx->legacy_irq = 0;
  1058. }
  1059. struct efx_tx_queue *
  1060. efx_get_tx_queue(struct efx_nic *efx, unsigned index, unsigned type)
  1061. {
  1062. unsigned tx_channel_offset =
  1063. separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
  1064. EFX_BUG_ON_PARANOID(index >= efx->n_tx_channels ||
  1065. type >= EFX_TXQ_TYPES);
  1066. return &efx->channel[tx_channel_offset + index]->tx_queue[type];
  1067. }
  1068. static void efx_set_channels(struct efx_nic *efx)
  1069. {
  1070. struct efx_channel *channel;
  1071. struct efx_tx_queue *tx_queue;
  1072. unsigned tx_channel_offset =
  1073. separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
  1074. /* Channel pointers were set in efx_init_struct() but we now
  1075. * need to clear them for TX queues in any RX-only channels. */
  1076. efx_for_each_channel(channel, efx) {
  1077. if (channel->channel - tx_channel_offset >=
  1078. efx->n_tx_channels) {
  1079. efx_for_each_channel_tx_queue(tx_queue, channel)
  1080. tx_queue->channel = NULL;
  1081. }
  1082. }
  1083. }
  1084. static int efx_probe_nic(struct efx_nic *efx)
  1085. {
  1086. size_t i;
  1087. int rc;
  1088. netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
  1089. /* Carry out hardware-type specific initialisation */
  1090. rc = efx->type->probe(efx);
  1091. if (rc)
  1092. return rc;
  1093. /* Determine the number of channels and queues by trying to hook
  1094. * in MSI-X interrupts. */
  1095. efx_probe_interrupts(efx);
  1096. if (efx->n_channels > 1)
  1097. get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
  1098. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
  1099. efx->rx_indir_table[i] = i % efx->n_rx_channels;
  1100. efx_set_channels(efx);
  1101. netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
  1102. netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
  1103. /* Initialise the interrupt moderation settings */
  1104. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true);
  1105. return 0;
  1106. }
  1107. static void efx_remove_nic(struct efx_nic *efx)
  1108. {
  1109. netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
  1110. efx_remove_interrupts(efx);
  1111. efx->type->remove(efx);
  1112. }
  1113. /**************************************************************************
  1114. *
  1115. * NIC startup/shutdown
  1116. *
  1117. *************************************************************************/
  1118. static int efx_probe_all(struct efx_nic *efx)
  1119. {
  1120. int rc;
  1121. rc = efx_probe_nic(efx);
  1122. if (rc) {
  1123. netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
  1124. goto fail1;
  1125. }
  1126. rc = efx_probe_port(efx);
  1127. if (rc) {
  1128. netif_err(efx, probe, efx->net_dev, "failed to create port\n");
  1129. goto fail2;
  1130. }
  1131. efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
  1132. rc = efx_probe_channels(efx);
  1133. if (rc)
  1134. goto fail3;
  1135. rc = efx_probe_filters(efx);
  1136. if (rc) {
  1137. netif_err(efx, probe, efx->net_dev,
  1138. "failed to create filter tables\n");
  1139. goto fail4;
  1140. }
  1141. return 0;
  1142. fail4:
  1143. efx_remove_channels(efx);
  1144. fail3:
  1145. efx_remove_port(efx);
  1146. fail2:
  1147. efx_remove_nic(efx);
  1148. fail1:
  1149. return rc;
  1150. }
  1151. /* Called after previous invocation(s) of efx_stop_all, restarts the
  1152. * port, kernel transmit queue, NAPI processing and hardware interrupts,
  1153. * and ensures that the port is scheduled to be reconfigured.
  1154. * This function is safe to call multiple times when the NIC is in any
  1155. * state. */
  1156. static void efx_start_all(struct efx_nic *efx)
  1157. {
  1158. struct efx_channel *channel;
  1159. EFX_ASSERT_RESET_SERIALISED(efx);
  1160. /* Check that it is appropriate to restart the interface. All
  1161. * of these flags are safe to read under just the rtnl lock */
  1162. if (efx->port_enabled)
  1163. return;
  1164. if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
  1165. return;
  1166. if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
  1167. return;
  1168. /* Mark the port as enabled so port reconfigurations can start, then
  1169. * restart the transmit interface early so the watchdog timer stops */
  1170. efx_start_port(efx);
  1171. efx_for_each_channel(channel, efx) {
  1172. if (efx_dev_registered(efx))
  1173. efx_wake_queue(channel);
  1174. efx_start_channel(channel);
  1175. }
  1176. efx_nic_enable_interrupts(efx);
  1177. /* Switch to event based MCDI completions after enabling interrupts.
  1178. * If a reset has been scheduled, then we need to stay in polled mode.
  1179. * Rather than serialising efx_mcdi_mode_event() [which sleeps] and
  1180. * reset_pending [modified from an atomic context], we instead guarantee
  1181. * that efx_mcdi_mode_poll() isn't reverted erroneously */
  1182. efx_mcdi_mode_event(efx);
  1183. if (efx->reset_pending != RESET_TYPE_NONE)
  1184. efx_mcdi_mode_poll(efx);
  1185. /* Start the hardware monitor if there is one. Otherwise (we're link
  1186. * event driven), we have to poll the PHY because after an event queue
  1187. * flush, we could have a missed a link state change */
  1188. if (efx->type->monitor != NULL) {
  1189. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1190. efx_monitor_interval);
  1191. } else {
  1192. mutex_lock(&efx->mac_lock);
  1193. if (efx->phy_op->poll(efx))
  1194. efx_link_status_changed(efx);
  1195. mutex_unlock(&efx->mac_lock);
  1196. }
  1197. efx->type->start_stats(efx);
  1198. }
  1199. /* Flush all delayed work. Should only be called when no more delayed work
  1200. * will be scheduled. This doesn't flush pending online resets (efx_reset),
  1201. * since we're holding the rtnl_lock at this point. */
  1202. static void efx_flush_all(struct efx_nic *efx)
  1203. {
  1204. /* Make sure the hardware monitor is stopped */
  1205. cancel_delayed_work_sync(&efx->monitor_work);
  1206. /* Stop scheduled port reconfigurations */
  1207. cancel_work_sync(&efx->mac_work);
  1208. }
  1209. /* Quiesce hardware and software without bringing the link down.
  1210. * Safe to call multiple times, when the nic and interface is in any
  1211. * state. The caller is guaranteed to subsequently be in a position
  1212. * to modify any hardware and software state they see fit without
  1213. * taking locks. */
  1214. static void efx_stop_all(struct efx_nic *efx)
  1215. {
  1216. struct efx_channel *channel;
  1217. EFX_ASSERT_RESET_SERIALISED(efx);
  1218. /* port_enabled can be read safely under the rtnl lock */
  1219. if (!efx->port_enabled)
  1220. return;
  1221. efx->type->stop_stats(efx);
  1222. /* Switch to MCDI polling on Siena before disabling interrupts */
  1223. efx_mcdi_mode_poll(efx);
  1224. /* Disable interrupts and wait for ISR to complete */
  1225. efx_nic_disable_interrupts(efx);
  1226. if (efx->legacy_irq)
  1227. synchronize_irq(efx->legacy_irq);
  1228. efx_for_each_channel(channel, efx) {
  1229. if (channel->irq)
  1230. synchronize_irq(channel->irq);
  1231. }
  1232. /* Stop all NAPI processing and synchronous rx refills */
  1233. efx_for_each_channel(channel, efx)
  1234. efx_stop_channel(channel);
  1235. /* Stop all asynchronous port reconfigurations. Since all
  1236. * event processing has already been stopped, there is no
  1237. * window to loose phy events */
  1238. efx_stop_port(efx);
  1239. /* Flush efx_mac_work(), refill_workqueue, monitor_work */
  1240. efx_flush_all(efx);
  1241. /* Stop the kernel transmit interface late, so the watchdog
  1242. * timer isn't ticking over the flush */
  1243. if (efx_dev_registered(efx)) {
  1244. struct efx_channel *channel;
  1245. efx_for_each_channel(channel, efx)
  1246. efx_stop_queue(channel);
  1247. netif_tx_lock_bh(efx->net_dev);
  1248. netif_tx_unlock_bh(efx->net_dev);
  1249. }
  1250. }
  1251. static void efx_remove_all(struct efx_nic *efx)
  1252. {
  1253. efx_remove_filters(efx);
  1254. efx_remove_channels(efx);
  1255. efx_remove_port(efx);
  1256. efx_remove_nic(efx);
  1257. }
  1258. /**************************************************************************
  1259. *
  1260. * Interrupt moderation
  1261. *
  1262. **************************************************************************/
  1263. static unsigned irq_mod_ticks(int usecs, int resolution)
  1264. {
  1265. if (usecs <= 0)
  1266. return 0; /* cannot receive interrupts ahead of time :-) */
  1267. if (usecs < resolution)
  1268. return 1; /* never round down to 0 */
  1269. return usecs / resolution;
  1270. }
  1271. /* Set interrupt moderation parameters */
  1272. void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs,
  1273. bool rx_adaptive)
  1274. {
  1275. struct efx_channel *channel;
  1276. unsigned tx_ticks = irq_mod_ticks(tx_usecs, EFX_IRQ_MOD_RESOLUTION);
  1277. unsigned rx_ticks = irq_mod_ticks(rx_usecs, EFX_IRQ_MOD_RESOLUTION);
  1278. EFX_ASSERT_RESET_SERIALISED(efx);
  1279. efx->irq_rx_adaptive = rx_adaptive;
  1280. efx->irq_rx_moderation = rx_ticks;
  1281. efx_for_each_channel(channel, efx) {
  1282. if (efx_channel_get_rx_queue(channel))
  1283. channel->irq_moderation = rx_ticks;
  1284. else if (efx_channel_get_tx_queue(channel, 0))
  1285. channel->irq_moderation = tx_ticks;
  1286. }
  1287. }
  1288. /**************************************************************************
  1289. *
  1290. * Hardware monitor
  1291. *
  1292. **************************************************************************/
  1293. /* Run periodically off the general workqueue */
  1294. static void efx_monitor(struct work_struct *data)
  1295. {
  1296. struct efx_nic *efx = container_of(data, struct efx_nic,
  1297. monitor_work.work);
  1298. netif_vdbg(efx, timer, efx->net_dev,
  1299. "hardware monitor executing on CPU %d\n",
  1300. raw_smp_processor_id());
  1301. BUG_ON(efx->type->monitor == NULL);
  1302. /* If the mac_lock is already held then it is likely a port
  1303. * reconfiguration is already in place, which will likely do
  1304. * most of the work of monitor() anyway. */
  1305. if (mutex_trylock(&efx->mac_lock)) {
  1306. if (efx->port_enabled)
  1307. efx->type->monitor(efx);
  1308. mutex_unlock(&efx->mac_lock);
  1309. }
  1310. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1311. efx_monitor_interval);
  1312. }
  1313. /**************************************************************************
  1314. *
  1315. * ioctls
  1316. *
  1317. *************************************************************************/
  1318. /* Net device ioctl
  1319. * Context: process, rtnl_lock() held.
  1320. */
  1321. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1322. {
  1323. struct efx_nic *efx = netdev_priv(net_dev);
  1324. struct mii_ioctl_data *data = if_mii(ifr);
  1325. EFX_ASSERT_RESET_SERIALISED(efx);
  1326. /* Convert phy_id from older PRTAD/DEVAD format */
  1327. if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
  1328. (data->phy_id & 0xfc00) == 0x0400)
  1329. data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
  1330. return mdio_mii_ioctl(&efx->mdio, data, cmd);
  1331. }
  1332. /**************************************************************************
  1333. *
  1334. * NAPI interface
  1335. *
  1336. **************************************************************************/
  1337. static int efx_init_napi(struct efx_nic *efx)
  1338. {
  1339. struct efx_channel *channel;
  1340. efx_for_each_channel(channel, efx) {
  1341. channel->napi_dev = efx->net_dev;
  1342. netif_napi_add(channel->napi_dev, &channel->napi_str,
  1343. efx_poll, napi_weight);
  1344. }
  1345. return 0;
  1346. }
  1347. static void efx_fini_napi(struct efx_nic *efx)
  1348. {
  1349. struct efx_channel *channel;
  1350. efx_for_each_channel(channel, efx) {
  1351. if (channel->napi_dev)
  1352. netif_napi_del(&channel->napi_str);
  1353. channel->napi_dev = NULL;
  1354. }
  1355. }
  1356. /**************************************************************************
  1357. *
  1358. * Kernel netpoll interface
  1359. *
  1360. *************************************************************************/
  1361. #ifdef CONFIG_NET_POLL_CONTROLLER
  1362. /* Although in the common case interrupts will be disabled, this is not
  1363. * guaranteed. However, all our work happens inside the NAPI callback,
  1364. * so no locking is required.
  1365. */
  1366. static void efx_netpoll(struct net_device *net_dev)
  1367. {
  1368. struct efx_nic *efx = netdev_priv(net_dev);
  1369. struct efx_channel *channel;
  1370. efx_for_each_channel(channel, efx)
  1371. efx_schedule_channel(channel);
  1372. }
  1373. #endif
  1374. /**************************************************************************
  1375. *
  1376. * Kernel net device interface
  1377. *
  1378. *************************************************************************/
  1379. /* Context: process, rtnl_lock() held. */
  1380. static int efx_net_open(struct net_device *net_dev)
  1381. {
  1382. struct efx_nic *efx = netdev_priv(net_dev);
  1383. EFX_ASSERT_RESET_SERIALISED(efx);
  1384. netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
  1385. raw_smp_processor_id());
  1386. if (efx->state == STATE_DISABLED)
  1387. return -EIO;
  1388. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1389. return -EBUSY;
  1390. if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
  1391. return -EIO;
  1392. /* Notify the kernel of the link state polled during driver load,
  1393. * before the monitor starts running */
  1394. efx_link_status_changed(efx);
  1395. efx_start_all(efx);
  1396. return 0;
  1397. }
  1398. /* Context: process, rtnl_lock() held.
  1399. * Note that the kernel will ignore our return code; this method
  1400. * should really be a void.
  1401. */
  1402. static int efx_net_stop(struct net_device *net_dev)
  1403. {
  1404. struct efx_nic *efx = netdev_priv(net_dev);
  1405. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  1406. raw_smp_processor_id());
  1407. if (efx->state != STATE_DISABLED) {
  1408. /* Stop the device and flush all the channels */
  1409. efx_stop_all(efx);
  1410. efx_fini_channels(efx);
  1411. efx_init_channels(efx);
  1412. }
  1413. return 0;
  1414. }
  1415. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1416. static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev, struct rtnl_link_stats64 *stats)
  1417. {
  1418. struct efx_nic *efx = netdev_priv(net_dev);
  1419. struct efx_mac_stats *mac_stats = &efx->mac_stats;
  1420. spin_lock_bh(&efx->stats_lock);
  1421. efx->type->update_stats(efx);
  1422. spin_unlock_bh(&efx->stats_lock);
  1423. stats->rx_packets = mac_stats->rx_packets;
  1424. stats->tx_packets = mac_stats->tx_packets;
  1425. stats->rx_bytes = mac_stats->rx_bytes;
  1426. stats->tx_bytes = mac_stats->tx_bytes;
  1427. stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
  1428. stats->multicast = mac_stats->rx_multicast;
  1429. stats->collisions = mac_stats->tx_collision;
  1430. stats->rx_length_errors = (mac_stats->rx_gtjumbo +
  1431. mac_stats->rx_length_error);
  1432. stats->rx_crc_errors = mac_stats->rx_bad;
  1433. stats->rx_frame_errors = mac_stats->rx_align_error;
  1434. stats->rx_fifo_errors = mac_stats->rx_overflow;
  1435. stats->rx_missed_errors = mac_stats->rx_missed;
  1436. stats->tx_window_errors = mac_stats->tx_late_collision;
  1437. stats->rx_errors = (stats->rx_length_errors +
  1438. stats->rx_crc_errors +
  1439. stats->rx_frame_errors +
  1440. mac_stats->rx_symbol_error);
  1441. stats->tx_errors = (stats->tx_window_errors +
  1442. mac_stats->tx_bad);
  1443. return stats;
  1444. }
  1445. /* Context: netif_tx_lock held, BHs disabled. */
  1446. static void efx_watchdog(struct net_device *net_dev)
  1447. {
  1448. struct efx_nic *efx = netdev_priv(net_dev);
  1449. netif_err(efx, tx_err, efx->net_dev,
  1450. "TX stuck with port_enabled=%d: resetting channels\n",
  1451. efx->port_enabled);
  1452. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1453. }
  1454. /* Context: process, rtnl_lock() held. */
  1455. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1456. {
  1457. struct efx_nic *efx = netdev_priv(net_dev);
  1458. int rc = 0;
  1459. EFX_ASSERT_RESET_SERIALISED(efx);
  1460. if (new_mtu > EFX_MAX_MTU)
  1461. return -EINVAL;
  1462. efx_stop_all(efx);
  1463. netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
  1464. efx_fini_channels(efx);
  1465. mutex_lock(&efx->mac_lock);
  1466. /* Reconfigure the MAC before enabling the dma queues so that
  1467. * the RX buffers don't overflow */
  1468. net_dev->mtu = new_mtu;
  1469. efx->mac_op->reconfigure(efx);
  1470. mutex_unlock(&efx->mac_lock);
  1471. efx_init_channels(efx);
  1472. efx_start_all(efx);
  1473. return rc;
  1474. }
  1475. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1476. {
  1477. struct efx_nic *efx = netdev_priv(net_dev);
  1478. struct sockaddr *addr = data;
  1479. char *new_addr = addr->sa_data;
  1480. EFX_ASSERT_RESET_SERIALISED(efx);
  1481. if (!is_valid_ether_addr(new_addr)) {
  1482. netif_err(efx, drv, efx->net_dev,
  1483. "invalid ethernet MAC address requested: %pM\n",
  1484. new_addr);
  1485. return -EINVAL;
  1486. }
  1487. memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
  1488. /* Reconfigure the MAC */
  1489. mutex_lock(&efx->mac_lock);
  1490. efx->mac_op->reconfigure(efx);
  1491. mutex_unlock(&efx->mac_lock);
  1492. return 0;
  1493. }
  1494. /* Context: netif_addr_lock held, BHs disabled. */
  1495. static void efx_set_multicast_list(struct net_device *net_dev)
  1496. {
  1497. struct efx_nic *efx = netdev_priv(net_dev);
  1498. struct netdev_hw_addr *ha;
  1499. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  1500. u32 crc;
  1501. int bit;
  1502. efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
  1503. /* Build multicast hash table */
  1504. if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
  1505. memset(mc_hash, 0xff, sizeof(*mc_hash));
  1506. } else {
  1507. memset(mc_hash, 0x00, sizeof(*mc_hash));
  1508. netdev_for_each_mc_addr(ha, net_dev) {
  1509. crc = ether_crc_le(ETH_ALEN, ha->addr);
  1510. bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
  1511. set_bit_le(bit, mc_hash->byte);
  1512. }
  1513. /* Broadcast packets go through the multicast hash filter.
  1514. * ether_crc_le() of the broadcast address is 0xbe2612ff
  1515. * so we always add bit 0xff to the mask.
  1516. */
  1517. set_bit_le(0xff, mc_hash->byte);
  1518. }
  1519. if (efx->port_enabled)
  1520. queue_work(efx->workqueue, &efx->mac_work);
  1521. /* Otherwise efx_start_port() will do this */
  1522. }
  1523. static const struct net_device_ops efx_netdev_ops = {
  1524. .ndo_open = efx_net_open,
  1525. .ndo_stop = efx_net_stop,
  1526. .ndo_get_stats64 = efx_net_stats,
  1527. .ndo_tx_timeout = efx_watchdog,
  1528. .ndo_start_xmit = efx_hard_start_xmit,
  1529. .ndo_validate_addr = eth_validate_addr,
  1530. .ndo_do_ioctl = efx_ioctl,
  1531. .ndo_change_mtu = efx_change_mtu,
  1532. .ndo_set_mac_address = efx_set_mac_address,
  1533. .ndo_set_multicast_list = efx_set_multicast_list,
  1534. #ifdef CONFIG_NET_POLL_CONTROLLER
  1535. .ndo_poll_controller = efx_netpoll,
  1536. #endif
  1537. };
  1538. static void efx_update_name(struct efx_nic *efx)
  1539. {
  1540. strcpy(efx->name, efx->net_dev->name);
  1541. efx_mtd_rename(efx);
  1542. efx_set_channel_names(efx);
  1543. }
  1544. static int efx_netdev_event(struct notifier_block *this,
  1545. unsigned long event, void *ptr)
  1546. {
  1547. struct net_device *net_dev = ptr;
  1548. if (net_dev->netdev_ops == &efx_netdev_ops &&
  1549. event == NETDEV_CHANGENAME)
  1550. efx_update_name(netdev_priv(net_dev));
  1551. return NOTIFY_DONE;
  1552. }
  1553. static struct notifier_block efx_netdev_notifier = {
  1554. .notifier_call = efx_netdev_event,
  1555. };
  1556. static ssize_t
  1557. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  1558. {
  1559. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1560. return sprintf(buf, "%d\n", efx->phy_type);
  1561. }
  1562. static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
  1563. static int efx_register_netdev(struct efx_nic *efx)
  1564. {
  1565. struct net_device *net_dev = efx->net_dev;
  1566. int rc;
  1567. net_dev->watchdog_timeo = 5 * HZ;
  1568. net_dev->irq = efx->pci_dev->irq;
  1569. net_dev->netdev_ops = &efx_netdev_ops;
  1570. SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
  1571. /* Clear MAC statistics */
  1572. efx->mac_op->update_stats(efx);
  1573. memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
  1574. rtnl_lock();
  1575. rc = dev_alloc_name(net_dev, net_dev->name);
  1576. if (rc < 0)
  1577. goto fail_locked;
  1578. efx_update_name(efx);
  1579. rc = register_netdevice(net_dev);
  1580. if (rc)
  1581. goto fail_locked;
  1582. /* Always start with carrier off; PHY events will detect the link */
  1583. netif_carrier_off(efx->net_dev);
  1584. rtnl_unlock();
  1585. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1586. if (rc) {
  1587. netif_err(efx, drv, efx->net_dev,
  1588. "failed to init net dev attributes\n");
  1589. goto fail_registered;
  1590. }
  1591. return 0;
  1592. fail_locked:
  1593. rtnl_unlock();
  1594. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  1595. return rc;
  1596. fail_registered:
  1597. unregister_netdev(net_dev);
  1598. return rc;
  1599. }
  1600. static void efx_unregister_netdev(struct efx_nic *efx)
  1601. {
  1602. struct efx_channel *channel;
  1603. struct efx_tx_queue *tx_queue;
  1604. if (!efx->net_dev)
  1605. return;
  1606. BUG_ON(netdev_priv(efx->net_dev) != efx);
  1607. /* Free up any skbs still remaining. This has to happen before
  1608. * we try to unregister the netdev as running their destructors
  1609. * may be needed to get the device ref. count to 0. */
  1610. efx_for_each_channel(channel, efx) {
  1611. efx_for_each_channel_tx_queue(tx_queue, channel)
  1612. efx_release_tx_buffers(tx_queue);
  1613. }
  1614. if (efx_dev_registered(efx)) {
  1615. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  1616. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1617. unregister_netdev(efx->net_dev);
  1618. }
  1619. }
  1620. /**************************************************************************
  1621. *
  1622. * Device reset and suspend
  1623. *
  1624. **************************************************************************/
  1625. /* Tears down the entire software state and most of the hardware state
  1626. * before reset. */
  1627. void efx_reset_down(struct efx_nic *efx, enum reset_type method)
  1628. {
  1629. EFX_ASSERT_RESET_SERIALISED(efx);
  1630. efx_stop_all(efx);
  1631. mutex_lock(&efx->mac_lock);
  1632. mutex_lock(&efx->spi_lock);
  1633. efx_fini_channels(efx);
  1634. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
  1635. efx->phy_op->fini(efx);
  1636. efx->type->fini(efx);
  1637. }
  1638. /* This function will always ensure that the locks acquired in
  1639. * efx_reset_down() are released. A failure return code indicates
  1640. * that we were unable to reinitialise the hardware, and the
  1641. * driver should be disabled. If ok is false, then the rx and tx
  1642. * engines are not restarted, pending a RESET_DISABLE. */
  1643. int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
  1644. {
  1645. int rc;
  1646. EFX_ASSERT_RESET_SERIALISED(efx);
  1647. rc = efx->type->init(efx);
  1648. if (rc) {
  1649. netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
  1650. goto fail;
  1651. }
  1652. if (!ok)
  1653. goto fail;
  1654. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
  1655. rc = efx->phy_op->init(efx);
  1656. if (rc)
  1657. goto fail;
  1658. if (efx->phy_op->reconfigure(efx))
  1659. netif_err(efx, drv, efx->net_dev,
  1660. "could not restore PHY settings\n");
  1661. }
  1662. efx->mac_op->reconfigure(efx);
  1663. efx_init_channels(efx);
  1664. efx_restore_filters(efx);
  1665. mutex_unlock(&efx->spi_lock);
  1666. mutex_unlock(&efx->mac_lock);
  1667. efx_start_all(efx);
  1668. return 0;
  1669. fail:
  1670. efx->port_initialized = false;
  1671. mutex_unlock(&efx->spi_lock);
  1672. mutex_unlock(&efx->mac_lock);
  1673. return rc;
  1674. }
  1675. /* Reset the NIC using the specified method. Note that the reset may
  1676. * fail, in which case the card will be left in an unusable state.
  1677. *
  1678. * Caller must hold the rtnl_lock.
  1679. */
  1680. int efx_reset(struct efx_nic *efx, enum reset_type method)
  1681. {
  1682. int rc, rc2;
  1683. bool disabled;
  1684. netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
  1685. RESET_TYPE(method));
  1686. efx_reset_down(efx, method);
  1687. rc = efx->type->reset(efx, method);
  1688. if (rc) {
  1689. netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
  1690. goto out;
  1691. }
  1692. /* Allow resets to be rescheduled. */
  1693. efx->reset_pending = RESET_TYPE_NONE;
  1694. /* Reinitialise bus-mastering, which may have been turned off before
  1695. * the reset was scheduled. This is still appropriate, even in the
  1696. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  1697. * can respond to requests. */
  1698. pci_set_master(efx->pci_dev);
  1699. out:
  1700. /* Leave device stopped if necessary */
  1701. disabled = rc || method == RESET_TYPE_DISABLE;
  1702. rc2 = efx_reset_up(efx, method, !disabled);
  1703. if (rc2) {
  1704. disabled = true;
  1705. if (!rc)
  1706. rc = rc2;
  1707. }
  1708. if (disabled) {
  1709. dev_close(efx->net_dev);
  1710. netif_err(efx, drv, efx->net_dev, "has been disabled\n");
  1711. efx->state = STATE_DISABLED;
  1712. } else {
  1713. netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
  1714. }
  1715. return rc;
  1716. }
  1717. /* The worker thread exists so that code that cannot sleep can
  1718. * schedule a reset for later.
  1719. */
  1720. static void efx_reset_work(struct work_struct *data)
  1721. {
  1722. struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
  1723. if (efx->reset_pending == RESET_TYPE_NONE)
  1724. return;
  1725. /* If we're not RUNNING then don't reset. Leave the reset_pending
  1726. * flag set so that efx_pci_probe_main will be retried */
  1727. if (efx->state != STATE_RUNNING) {
  1728. netif_info(efx, drv, efx->net_dev,
  1729. "scheduled reset quenched. NIC not RUNNING\n");
  1730. return;
  1731. }
  1732. rtnl_lock();
  1733. (void)efx_reset(efx, efx->reset_pending);
  1734. rtnl_unlock();
  1735. }
  1736. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  1737. {
  1738. enum reset_type method;
  1739. if (efx->reset_pending != RESET_TYPE_NONE) {
  1740. netif_info(efx, drv, efx->net_dev,
  1741. "quenching already scheduled reset\n");
  1742. return;
  1743. }
  1744. switch (type) {
  1745. case RESET_TYPE_INVISIBLE:
  1746. case RESET_TYPE_ALL:
  1747. case RESET_TYPE_WORLD:
  1748. case RESET_TYPE_DISABLE:
  1749. method = type;
  1750. break;
  1751. case RESET_TYPE_RX_RECOVERY:
  1752. case RESET_TYPE_RX_DESC_FETCH:
  1753. case RESET_TYPE_TX_DESC_FETCH:
  1754. case RESET_TYPE_TX_SKIP:
  1755. method = RESET_TYPE_INVISIBLE;
  1756. break;
  1757. case RESET_TYPE_MC_FAILURE:
  1758. default:
  1759. method = RESET_TYPE_ALL;
  1760. break;
  1761. }
  1762. if (method != type)
  1763. netif_dbg(efx, drv, efx->net_dev,
  1764. "scheduling %s reset for %s\n",
  1765. RESET_TYPE(method), RESET_TYPE(type));
  1766. else
  1767. netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
  1768. RESET_TYPE(method));
  1769. efx->reset_pending = method;
  1770. /* efx_process_channel() will no longer read events once a
  1771. * reset is scheduled. So switch back to poll'd MCDI completions. */
  1772. efx_mcdi_mode_poll(efx);
  1773. queue_work(reset_workqueue, &efx->reset_work);
  1774. }
  1775. /**************************************************************************
  1776. *
  1777. * List of NICs we support
  1778. *
  1779. **************************************************************************/
  1780. /* PCI device ID table */
  1781. static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
  1782. {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
  1783. .driver_data = (unsigned long) &falcon_a1_nic_type},
  1784. {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
  1785. .driver_data = (unsigned long) &falcon_b0_nic_type},
  1786. {PCI_DEVICE(EFX_VENDID_SFC, BETHPAGE_A_P_DEVID),
  1787. .driver_data = (unsigned long) &siena_a0_nic_type},
  1788. {PCI_DEVICE(EFX_VENDID_SFC, SIENA_A_P_DEVID),
  1789. .driver_data = (unsigned long) &siena_a0_nic_type},
  1790. {0} /* end of list */
  1791. };
  1792. /**************************************************************************
  1793. *
  1794. * Dummy PHY/MAC operations
  1795. *
  1796. * Can be used for some unimplemented operations
  1797. * Needed so all function pointers are valid and do not have to be tested
  1798. * before use
  1799. *
  1800. **************************************************************************/
  1801. int efx_port_dummy_op_int(struct efx_nic *efx)
  1802. {
  1803. return 0;
  1804. }
  1805. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  1806. static bool efx_port_dummy_op_poll(struct efx_nic *efx)
  1807. {
  1808. return false;
  1809. }
  1810. static struct efx_phy_operations efx_dummy_phy_operations = {
  1811. .init = efx_port_dummy_op_int,
  1812. .reconfigure = efx_port_dummy_op_int,
  1813. .poll = efx_port_dummy_op_poll,
  1814. .fini = efx_port_dummy_op_void,
  1815. };
  1816. /**************************************************************************
  1817. *
  1818. * Data housekeeping
  1819. *
  1820. **************************************************************************/
  1821. /* This zeroes out and then fills in the invariants in a struct
  1822. * efx_nic (including all sub-structures).
  1823. */
  1824. static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
  1825. struct pci_dev *pci_dev, struct net_device *net_dev)
  1826. {
  1827. int i;
  1828. /* Initialise common structures */
  1829. memset(efx, 0, sizeof(*efx));
  1830. spin_lock_init(&efx->biu_lock);
  1831. mutex_init(&efx->mdio_lock);
  1832. mutex_init(&efx->spi_lock);
  1833. #ifdef CONFIG_SFC_MTD
  1834. INIT_LIST_HEAD(&efx->mtd_list);
  1835. #endif
  1836. INIT_WORK(&efx->reset_work, efx_reset_work);
  1837. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  1838. efx->pci_dev = pci_dev;
  1839. efx->msg_enable = debug;
  1840. efx->state = STATE_INIT;
  1841. efx->reset_pending = RESET_TYPE_NONE;
  1842. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  1843. efx->net_dev = net_dev;
  1844. efx->rx_checksum_enabled = true;
  1845. spin_lock_init(&efx->stats_lock);
  1846. mutex_init(&efx->mac_lock);
  1847. efx->mac_op = type->default_mac_ops;
  1848. efx->phy_op = &efx_dummy_phy_operations;
  1849. efx->mdio.dev = net_dev;
  1850. INIT_WORK(&efx->mac_work, efx_mac_work);
  1851. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  1852. efx->channel[i] = efx_alloc_channel(efx, i, NULL);
  1853. if (!efx->channel[i])
  1854. goto fail;
  1855. }
  1856. efx->type = type;
  1857. EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
  1858. /* Higher numbered interrupt modes are less capable! */
  1859. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  1860. interrupt_mode);
  1861. /* Would be good to use the net_dev name, but we're too early */
  1862. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  1863. pci_name(pci_dev));
  1864. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  1865. if (!efx->workqueue)
  1866. goto fail;
  1867. return 0;
  1868. fail:
  1869. efx_fini_struct(efx);
  1870. return -ENOMEM;
  1871. }
  1872. static void efx_fini_struct(struct efx_nic *efx)
  1873. {
  1874. int i;
  1875. for (i = 0; i < EFX_MAX_CHANNELS; i++)
  1876. kfree(efx->channel[i]);
  1877. if (efx->workqueue) {
  1878. destroy_workqueue(efx->workqueue);
  1879. efx->workqueue = NULL;
  1880. }
  1881. }
  1882. /**************************************************************************
  1883. *
  1884. * PCI interface
  1885. *
  1886. **************************************************************************/
  1887. /* Main body of final NIC shutdown code
  1888. * This is called only at module unload (or hotplug removal).
  1889. */
  1890. static void efx_pci_remove_main(struct efx_nic *efx)
  1891. {
  1892. efx_nic_fini_interrupt(efx);
  1893. efx_fini_channels(efx);
  1894. efx_fini_port(efx);
  1895. efx->type->fini(efx);
  1896. efx_fini_napi(efx);
  1897. efx_remove_all(efx);
  1898. }
  1899. /* Final NIC shutdown
  1900. * This is called only at module unload (or hotplug removal).
  1901. */
  1902. static void efx_pci_remove(struct pci_dev *pci_dev)
  1903. {
  1904. struct efx_nic *efx;
  1905. efx = pci_get_drvdata(pci_dev);
  1906. if (!efx)
  1907. return;
  1908. /* Mark the NIC as fini, then stop the interface */
  1909. rtnl_lock();
  1910. efx->state = STATE_FINI;
  1911. dev_close(efx->net_dev);
  1912. /* Allow any queued efx_resets() to complete */
  1913. rtnl_unlock();
  1914. efx_unregister_netdev(efx);
  1915. efx_mtd_remove(efx);
  1916. /* Wait for any scheduled resets to complete. No more will be
  1917. * scheduled from this point because efx_stop_all() has been
  1918. * called, we are no longer registered with driverlink, and
  1919. * the net_device's have been removed. */
  1920. cancel_work_sync(&efx->reset_work);
  1921. efx_pci_remove_main(efx);
  1922. efx_fini_io(efx);
  1923. netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
  1924. pci_set_drvdata(pci_dev, NULL);
  1925. efx_fini_struct(efx);
  1926. free_netdev(efx->net_dev);
  1927. };
  1928. /* Main body of NIC initialisation
  1929. * This is called at module load (or hotplug insertion, theoretically).
  1930. */
  1931. static int efx_pci_probe_main(struct efx_nic *efx)
  1932. {
  1933. int rc;
  1934. /* Do start-of-day initialisation */
  1935. rc = efx_probe_all(efx);
  1936. if (rc)
  1937. goto fail1;
  1938. rc = efx_init_napi(efx);
  1939. if (rc)
  1940. goto fail2;
  1941. rc = efx->type->init(efx);
  1942. if (rc) {
  1943. netif_err(efx, probe, efx->net_dev,
  1944. "failed to initialise NIC\n");
  1945. goto fail3;
  1946. }
  1947. rc = efx_init_port(efx);
  1948. if (rc) {
  1949. netif_err(efx, probe, efx->net_dev,
  1950. "failed to initialise port\n");
  1951. goto fail4;
  1952. }
  1953. efx_init_channels(efx);
  1954. rc = efx_nic_init_interrupt(efx);
  1955. if (rc)
  1956. goto fail5;
  1957. return 0;
  1958. fail5:
  1959. efx_fini_channels(efx);
  1960. efx_fini_port(efx);
  1961. fail4:
  1962. efx->type->fini(efx);
  1963. fail3:
  1964. efx_fini_napi(efx);
  1965. fail2:
  1966. efx_remove_all(efx);
  1967. fail1:
  1968. return rc;
  1969. }
  1970. /* NIC initialisation
  1971. *
  1972. * This is called at module load (or hotplug insertion,
  1973. * theoretically). It sets up PCI mappings, tests and resets the NIC,
  1974. * sets up and registers the network devices with the kernel and hooks
  1975. * the interrupt service routine. It does not prepare the device for
  1976. * transmission; this is left to the first time one of the network
  1977. * interfaces is brought up (i.e. efx_net_open).
  1978. */
  1979. static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
  1980. const struct pci_device_id *entry)
  1981. {
  1982. struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
  1983. struct net_device *net_dev;
  1984. struct efx_nic *efx;
  1985. int i, rc;
  1986. /* Allocate and initialise a struct net_device and struct efx_nic */
  1987. net_dev = alloc_etherdev_mq(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES);
  1988. if (!net_dev)
  1989. return -ENOMEM;
  1990. net_dev->features |= (type->offload_features | NETIF_F_SG |
  1991. NETIF_F_HIGHDMA | NETIF_F_TSO |
  1992. NETIF_F_GRO);
  1993. if (type->offload_features & NETIF_F_V6_CSUM)
  1994. net_dev->features |= NETIF_F_TSO6;
  1995. /* Mask for features that also apply to VLAN devices */
  1996. net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
  1997. NETIF_F_HIGHDMA | NETIF_F_TSO);
  1998. efx = netdev_priv(net_dev);
  1999. pci_set_drvdata(pci_dev, efx);
  2000. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  2001. rc = efx_init_struct(efx, type, pci_dev, net_dev);
  2002. if (rc)
  2003. goto fail1;
  2004. netif_info(efx, probe, efx->net_dev,
  2005. "Solarflare Communications NIC detected\n");
  2006. /* Set up basic I/O (BAR mappings etc) */
  2007. rc = efx_init_io(efx);
  2008. if (rc)
  2009. goto fail2;
  2010. /* No serialisation is required with the reset path because
  2011. * we're in STATE_INIT. */
  2012. for (i = 0; i < 5; i++) {
  2013. rc = efx_pci_probe_main(efx);
  2014. /* Serialise against efx_reset(). No more resets will be
  2015. * scheduled since efx_stop_all() has been called, and we
  2016. * have not and never have been registered with either
  2017. * the rtnetlink or driverlink layers. */
  2018. cancel_work_sync(&efx->reset_work);
  2019. if (rc == 0) {
  2020. if (efx->reset_pending != RESET_TYPE_NONE) {
  2021. /* If there was a scheduled reset during
  2022. * probe, the NIC is probably hosed anyway */
  2023. efx_pci_remove_main(efx);
  2024. rc = -EIO;
  2025. } else {
  2026. break;
  2027. }
  2028. }
  2029. /* Retry if a recoverably reset event has been scheduled */
  2030. if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
  2031. (efx->reset_pending != RESET_TYPE_ALL))
  2032. goto fail3;
  2033. efx->reset_pending = RESET_TYPE_NONE;
  2034. }
  2035. if (rc) {
  2036. netif_err(efx, probe, efx->net_dev, "Could not reset NIC\n");
  2037. goto fail4;
  2038. }
  2039. /* Switch to the running state before we expose the device to the OS,
  2040. * so that dev_open()|efx_start_all() will actually start the device */
  2041. efx->state = STATE_RUNNING;
  2042. rc = efx_register_netdev(efx);
  2043. if (rc)
  2044. goto fail5;
  2045. netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
  2046. rtnl_lock();
  2047. efx_mtd_probe(efx); /* allowed to fail */
  2048. rtnl_unlock();
  2049. return 0;
  2050. fail5:
  2051. efx_pci_remove_main(efx);
  2052. fail4:
  2053. fail3:
  2054. efx_fini_io(efx);
  2055. fail2:
  2056. efx_fini_struct(efx);
  2057. fail1:
  2058. WARN_ON(rc > 0);
  2059. netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
  2060. free_netdev(net_dev);
  2061. return rc;
  2062. }
  2063. static int efx_pm_freeze(struct device *dev)
  2064. {
  2065. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2066. efx->state = STATE_FINI;
  2067. netif_device_detach(efx->net_dev);
  2068. efx_stop_all(efx);
  2069. efx_fini_channels(efx);
  2070. return 0;
  2071. }
  2072. static int efx_pm_thaw(struct device *dev)
  2073. {
  2074. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2075. efx->state = STATE_INIT;
  2076. efx_init_channels(efx);
  2077. mutex_lock(&efx->mac_lock);
  2078. efx->phy_op->reconfigure(efx);
  2079. mutex_unlock(&efx->mac_lock);
  2080. efx_start_all(efx);
  2081. netif_device_attach(efx->net_dev);
  2082. efx->state = STATE_RUNNING;
  2083. efx->type->resume_wol(efx);
  2084. /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
  2085. queue_work(reset_workqueue, &efx->reset_work);
  2086. return 0;
  2087. }
  2088. static int efx_pm_poweroff(struct device *dev)
  2089. {
  2090. struct pci_dev *pci_dev = to_pci_dev(dev);
  2091. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2092. efx->type->fini(efx);
  2093. efx->reset_pending = RESET_TYPE_NONE;
  2094. pci_save_state(pci_dev);
  2095. return pci_set_power_state(pci_dev, PCI_D3hot);
  2096. }
  2097. /* Used for both resume and restore */
  2098. static int efx_pm_resume(struct device *dev)
  2099. {
  2100. struct pci_dev *pci_dev = to_pci_dev(dev);
  2101. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2102. int rc;
  2103. rc = pci_set_power_state(pci_dev, PCI_D0);
  2104. if (rc)
  2105. return rc;
  2106. pci_restore_state(pci_dev);
  2107. rc = pci_enable_device(pci_dev);
  2108. if (rc)
  2109. return rc;
  2110. pci_set_master(efx->pci_dev);
  2111. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  2112. if (rc)
  2113. return rc;
  2114. rc = efx->type->init(efx);
  2115. if (rc)
  2116. return rc;
  2117. efx_pm_thaw(dev);
  2118. return 0;
  2119. }
  2120. static int efx_pm_suspend(struct device *dev)
  2121. {
  2122. int rc;
  2123. efx_pm_freeze(dev);
  2124. rc = efx_pm_poweroff(dev);
  2125. if (rc)
  2126. efx_pm_resume(dev);
  2127. return rc;
  2128. }
  2129. static struct dev_pm_ops efx_pm_ops = {
  2130. .suspend = efx_pm_suspend,
  2131. .resume = efx_pm_resume,
  2132. .freeze = efx_pm_freeze,
  2133. .thaw = efx_pm_thaw,
  2134. .poweroff = efx_pm_poweroff,
  2135. .restore = efx_pm_resume,
  2136. };
  2137. static struct pci_driver efx_pci_driver = {
  2138. .name = KBUILD_MODNAME,
  2139. .id_table = efx_pci_table,
  2140. .probe = efx_pci_probe,
  2141. .remove = efx_pci_remove,
  2142. .driver.pm = &efx_pm_ops,
  2143. };
  2144. /**************************************************************************
  2145. *
  2146. * Kernel module interface
  2147. *
  2148. *************************************************************************/
  2149. module_param(interrupt_mode, uint, 0444);
  2150. MODULE_PARM_DESC(interrupt_mode,
  2151. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  2152. static int __init efx_init_module(void)
  2153. {
  2154. int rc;
  2155. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  2156. rc = register_netdevice_notifier(&efx_netdev_notifier);
  2157. if (rc)
  2158. goto err_notifier;
  2159. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  2160. if (!reset_workqueue) {
  2161. rc = -ENOMEM;
  2162. goto err_reset;
  2163. }
  2164. rc = pci_register_driver(&efx_pci_driver);
  2165. if (rc < 0)
  2166. goto err_pci;
  2167. return 0;
  2168. err_pci:
  2169. destroy_workqueue(reset_workqueue);
  2170. err_reset:
  2171. unregister_netdevice_notifier(&efx_netdev_notifier);
  2172. err_notifier:
  2173. return rc;
  2174. }
  2175. static void __exit efx_exit_module(void)
  2176. {
  2177. printk(KERN_INFO "Solarflare NET driver unloading\n");
  2178. pci_unregister_driver(&efx_pci_driver);
  2179. destroy_workqueue(reset_workqueue);
  2180. unregister_netdevice_notifier(&efx_netdev_notifier);
  2181. }
  2182. module_init(efx_init_module);
  2183. module_exit(efx_exit_module);
  2184. MODULE_AUTHOR("Solarflare Communications and "
  2185. "Michael Brown <mbrown@fensystems.co.uk>");
  2186. MODULE_DESCRIPTION("Solarflare Communications network driver");
  2187. MODULE_LICENSE("GPL");
  2188. MODULE_DEVICE_TABLE(pci, efx_pci_table);