efx.c 71 KB

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