efx.c 70 KB

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