efx.c 69 KB

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