efx.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2013 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/ethtool.h>
  20. #include <linux/topology.h>
  21. #include <linux/gfp.h>
  22. #include <linux/aer.h>
  23. #include <linux/interrupt.h>
  24. #include "net_driver.h"
  25. #include "efx.h"
  26. #include "nic.h"
  27. #include "selftest.h"
  28. #include "mcdi.h"
  29. #include "workarounds.h"
  30. /**************************************************************************
  31. *
  32. * Type name strings
  33. *
  34. **************************************************************************
  35. */
  36. /* Loopback mode names (see LOOPBACK_MODE()) */
  37. const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
  38. const char *const efx_loopback_mode_names[] = {
  39. [LOOPBACK_NONE] = "NONE",
  40. [LOOPBACK_DATA] = "DATAPATH",
  41. [LOOPBACK_GMAC] = "GMAC",
  42. [LOOPBACK_XGMII] = "XGMII",
  43. [LOOPBACK_XGXS] = "XGXS",
  44. [LOOPBACK_XAUI] = "XAUI",
  45. [LOOPBACK_GMII] = "GMII",
  46. [LOOPBACK_SGMII] = "SGMII",
  47. [LOOPBACK_XGBR] = "XGBR",
  48. [LOOPBACK_XFI] = "XFI",
  49. [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
  50. [LOOPBACK_GMII_FAR] = "GMII_FAR",
  51. [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
  52. [LOOPBACK_XFI_FAR] = "XFI_FAR",
  53. [LOOPBACK_GPHY] = "GPHY",
  54. [LOOPBACK_PHYXS] = "PHYXS",
  55. [LOOPBACK_PCS] = "PCS",
  56. [LOOPBACK_PMAPMD] = "PMA/PMD",
  57. [LOOPBACK_XPORT] = "XPORT",
  58. [LOOPBACK_XGMII_WS] = "XGMII_WS",
  59. [LOOPBACK_XAUI_WS] = "XAUI_WS",
  60. [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
  61. [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
  62. [LOOPBACK_GMII_WS] = "GMII_WS",
  63. [LOOPBACK_XFI_WS] = "XFI_WS",
  64. [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
  65. [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
  66. };
  67. const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
  68. const char *const efx_reset_type_names[] = {
  69. [RESET_TYPE_INVISIBLE] = "INVISIBLE",
  70. [RESET_TYPE_ALL] = "ALL",
  71. [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
  72. [RESET_TYPE_WORLD] = "WORLD",
  73. [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
  74. [RESET_TYPE_DISABLE] = "DISABLE",
  75. [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
  76. [RESET_TYPE_INT_ERROR] = "INT_ERROR",
  77. [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
  78. [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
  79. [RESET_TYPE_TX_SKIP] = "TX_SKIP",
  80. [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
  81. [RESET_TYPE_MC_BIST] = "MC_BIST",
  82. };
  83. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  84. * queued onto this work queue. This is not a per-nic work queue, because
  85. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  86. */
  87. static struct workqueue_struct *reset_workqueue;
  88. /* How often and how many times to poll for a reset while waiting for a
  89. * BIST that another function started to complete.
  90. */
  91. #define BIST_WAIT_DELAY_MS 100
  92. #define BIST_WAIT_DELAY_COUNT 100
  93. /**************************************************************************
  94. *
  95. * Configurable values
  96. *
  97. *************************************************************************/
  98. /*
  99. * Use separate channels for TX and RX events
  100. *
  101. * Set this to 1 to use separate channels for TX and RX. It allows us
  102. * to control interrupt affinity separately for TX and RX.
  103. *
  104. * This is only used in MSI-X interrupt mode
  105. */
  106. static bool separate_tx_channels;
  107. module_param(separate_tx_channels, bool, 0444);
  108. MODULE_PARM_DESC(separate_tx_channels,
  109. "Use separate channels for TX and RX");
  110. /* This is the weight assigned to each of the (per-channel) virtual
  111. * NAPI devices.
  112. */
  113. static int napi_weight = 64;
  114. /* This is the time (in jiffies) between invocations of the hardware
  115. * monitor.
  116. * On Falcon-based NICs, this will:
  117. * - Check the on-board hardware monitor;
  118. * - Poll the link state and reconfigure the hardware as necessary.
  119. * On Siena-based NICs for power systems with EEH support, this will give EEH a
  120. * chance to start.
  121. */
  122. static unsigned int efx_monitor_interval = 1 * HZ;
  123. /* Initial interrupt moderation settings. They can be modified after
  124. * module load with ethtool.
  125. *
  126. * The default for RX should strike a balance between increasing the
  127. * round-trip latency and reducing overhead.
  128. */
  129. static unsigned int rx_irq_mod_usec = 60;
  130. /* Initial interrupt moderation settings. They can be modified after
  131. * module load with ethtool.
  132. *
  133. * This default is chosen to ensure that a 10G link does not go idle
  134. * while a TX queue is stopped after it has become full. A queue is
  135. * restarted when it drops below half full. The time this takes (assuming
  136. * worst case 3 descriptors per packet and 1024 descriptors) is
  137. * 512 / 3 * 1.2 = 205 usec.
  138. */
  139. static unsigned int tx_irq_mod_usec = 150;
  140. /* This is the first interrupt mode to try out of:
  141. * 0 => MSI-X
  142. * 1 => MSI
  143. * 2 => legacy
  144. */
  145. static unsigned int interrupt_mode;
  146. /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
  147. * i.e. the number of CPUs among which we may distribute simultaneous
  148. * interrupt handling.
  149. *
  150. * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
  151. * The default (0) means to assign an interrupt to each core.
  152. */
  153. static unsigned int rss_cpus;
  154. module_param(rss_cpus, uint, 0444);
  155. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  156. static bool phy_flash_cfg;
  157. module_param(phy_flash_cfg, bool, 0644);
  158. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  159. static unsigned irq_adapt_low_thresh = 8000;
  160. module_param(irq_adapt_low_thresh, uint, 0644);
  161. MODULE_PARM_DESC(irq_adapt_low_thresh,
  162. "Threshold score for reducing IRQ moderation");
  163. static unsigned irq_adapt_high_thresh = 16000;
  164. module_param(irq_adapt_high_thresh, uint, 0644);
  165. MODULE_PARM_DESC(irq_adapt_high_thresh,
  166. "Threshold score for increasing IRQ moderation");
  167. static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  168. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  169. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  170. NETIF_MSG_TX_ERR | NETIF_MSG_HW);
  171. module_param(debug, uint, 0);
  172. MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
  173. /**************************************************************************
  174. *
  175. * Utility functions and prototypes
  176. *
  177. *************************************************************************/
  178. static int efx_soft_enable_interrupts(struct efx_nic *efx);
  179. static void efx_soft_disable_interrupts(struct efx_nic *efx);
  180. static void efx_remove_channel(struct efx_channel *channel);
  181. static void efx_remove_channels(struct efx_nic *efx);
  182. static const struct efx_channel_type efx_default_channel_type;
  183. static void efx_remove_port(struct efx_nic *efx);
  184. static void efx_init_napi_channel(struct efx_channel *channel);
  185. static void efx_fini_napi(struct efx_nic *efx);
  186. static void efx_fini_napi_channel(struct efx_channel *channel);
  187. static void efx_fini_struct(struct efx_nic *efx);
  188. static void efx_start_all(struct efx_nic *efx);
  189. static void efx_stop_all(struct efx_nic *efx);
  190. #define EFX_ASSERT_RESET_SERIALISED(efx) \
  191. do { \
  192. if ((efx->state == STATE_READY) || \
  193. (efx->state == STATE_RECOVERY) || \
  194. (efx->state == STATE_DISABLED)) \
  195. ASSERT_RTNL(); \
  196. } while (0)
  197. static int efx_check_disabled(struct efx_nic *efx)
  198. {
  199. if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
  200. netif_err(efx, drv, efx->net_dev,
  201. "device is disabled due to earlier errors\n");
  202. return -EIO;
  203. }
  204. return 0;
  205. }
  206. /**************************************************************************
  207. *
  208. * Event queue processing
  209. *
  210. *************************************************************************/
  211. /* Process channel's event queue
  212. *
  213. * This function is responsible for processing the event queue of a
  214. * single channel. The caller must guarantee that this function will
  215. * never be concurrently called more than once on the same channel,
  216. * though different channels may be being processed concurrently.
  217. */
  218. static int efx_process_channel(struct efx_channel *channel, int budget)
  219. {
  220. int spent;
  221. if (unlikely(!channel->enabled))
  222. return 0;
  223. spent = efx_nic_process_eventq(channel, budget);
  224. if (spent && efx_channel_has_rx_queue(channel)) {
  225. struct efx_rx_queue *rx_queue =
  226. efx_channel_get_rx_queue(channel);
  227. efx_rx_flush_packet(channel);
  228. efx_fast_push_rx_descriptors(rx_queue, true);
  229. }
  230. return spent;
  231. }
  232. /* NAPI poll handler
  233. *
  234. * NAPI guarantees serialisation of polls of the same device, which
  235. * provides the guarantee required by efx_process_channel().
  236. */
  237. static int efx_poll(struct napi_struct *napi, int budget)
  238. {
  239. struct efx_channel *channel =
  240. container_of(napi, struct efx_channel, napi_str);
  241. struct efx_nic *efx = channel->efx;
  242. int spent;
  243. netif_vdbg(efx, intr, efx->net_dev,
  244. "channel %d NAPI poll executing on CPU %d\n",
  245. channel->channel, raw_smp_processor_id());
  246. spent = efx_process_channel(channel, budget);
  247. if (spent < budget) {
  248. if (efx_channel_has_rx_queue(channel) &&
  249. efx->irq_rx_adaptive &&
  250. unlikely(++channel->irq_count == 1000)) {
  251. if (unlikely(channel->irq_mod_score <
  252. irq_adapt_low_thresh)) {
  253. if (channel->irq_moderation > 1) {
  254. channel->irq_moderation -= 1;
  255. efx->type->push_irq_moderation(channel);
  256. }
  257. } else if (unlikely(channel->irq_mod_score >
  258. irq_adapt_high_thresh)) {
  259. if (channel->irq_moderation <
  260. efx->irq_rx_moderation) {
  261. channel->irq_moderation += 1;
  262. efx->type->push_irq_moderation(channel);
  263. }
  264. }
  265. channel->irq_count = 0;
  266. channel->irq_mod_score = 0;
  267. }
  268. efx_filter_rfs_expire(channel);
  269. /* There is no race here; although napi_disable() will
  270. * only wait for napi_complete(), this isn't a problem
  271. * since efx_nic_eventq_read_ack() will have no effect if
  272. * interrupts have already been disabled.
  273. */
  274. napi_complete(napi);
  275. efx_nic_eventq_read_ack(channel);
  276. }
  277. return spent;
  278. }
  279. /* Create event queue
  280. * Event queue memory allocations are done only once. If the channel
  281. * is reset, the memory buffer will be reused; this guards against
  282. * errors during channel reset and also simplifies interrupt handling.
  283. */
  284. static int efx_probe_eventq(struct efx_channel *channel)
  285. {
  286. struct efx_nic *efx = channel->efx;
  287. unsigned long entries;
  288. netif_dbg(efx, probe, efx->net_dev,
  289. "chan %d create event queue\n", channel->channel);
  290. /* Build an event queue with room for one event per tx and rx buffer,
  291. * plus some extra for link state events and MCDI completions. */
  292. entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
  293. EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
  294. channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
  295. return efx_nic_probe_eventq(channel);
  296. }
  297. /* Prepare channel's event queue */
  298. static int efx_init_eventq(struct efx_channel *channel)
  299. {
  300. struct efx_nic *efx = channel->efx;
  301. int rc;
  302. EFX_WARN_ON_PARANOID(channel->eventq_init);
  303. netif_dbg(efx, drv, efx->net_dev,
  304. "chan %d init event queue\n", channel->channel);
  305. rc = efx_nic_init_eventq(channel);
  306. if (rc == 0) {
  307. efx->type->push_irq_moderation(channel);
  308. channel->eventq_read_ptr = 0;
  309. channel->eventq_init = true;
  310. }
  311. return rc;
  312. }
  313. /* Enable event queue processing and NAPI */
  314. static void efx_start_eventq(struct efx_channel *channel)
  315. {
  316. netif_dbg(channel->efx, ifup, channel->efx->net_dev,
  317. "chan %d start event queue\n", channel->channel);
  318. /* Make sure the NAPI handler sees the enabled flag set */
  319. channel->enabled = true;
  320. smp_wmb();
  321. napi_enable(&channel->napi_str);
  322. efx_nic_eventq_read_ack(channel);
  323. }
  324. /* Disable event queue processing and NAPI */
  325. static void efx_stop_eventq(struct efx_channel *channel)
  326. {
  327. if (!channel->enabled)
  328. return;
  329. napi_disable(&channel->napi_str);
  330. channel->enabled = false;
  331. }
  332. static void efx_fini_eventq(struct efx_channel *channel)
  333. {
  334. if (!channel->eventq_init)
  335. return;
  336. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  337. "chan %d fini event queue\n", channel->channel);
  338. efx_nic_fini_eventq(channel);
  339. channel->eventq_init = false;
  340. }
  341. static void efx_remove_eventq(struct efx_channel *channel)
  342. {
  343. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  344. "chan %d remove event queue\n", channel->channel);
  345. efx_nic_remove_eventq(channel);
  346. }
  347. /**************************************************************************
  348. *
  349. * Channel handling
  350. *
  351. *************************************************************************/
  352. /* Allocate and initialise a channel structure. */
  353. static struct efx_channel *
  354. efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
  355. {
  356. struct efx_channel *channel;
  357. struct efx_rx_queue *rx_queue;
  358. struct efx_tx_queue *tx_queue;
  359. int j;
  360. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  361. if (!channel)
  362. return NULL;
  363. channel->efx = efx;
  364. channel->channel = i;
  365. channel->type = &efx_default_channel_type;
  366. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  367. tx_queue = &channel->tx_queue[j];
  368. tx_queue->efx = efx;
  369. tx_queue->queue = i * EFX_TXQ_TYPES + j;
  370. tx_queue->channel = channel;
  371. }
  372. rx_queue = &channel->rx_queue;
  373. rx_queue->efx = efx;
  374. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  375. (unsigned long)rx_queue);
  376. return channel;
  377. }
  378. /* Allocate and initialise a channel structure, copying parameters
  379. * (but not resources) from an old channel structure.
  380. */
  381. static struct efx_channel *
  382. efx_copy_channel(const struct efx_channel *old_channel)
  383. {
  384. struct efx_channel *channel;
  385. struct efx_rx_queue *rx_queue;
  386. struct efx_tx_queue *tx_queue;
  387. int j;
  388. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  389. if (!channel)
  390. return NULL;
  391. *channel = *old_channel;
  392. channel->napi_dev = NULL;
  393. memset(&channel->eventq, 0, sizeof(channel->eventq));
  394. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  395. tx_queue = &channel->tx_queue[j];
  396. if (tx_queue->channel)
  397. tx_queue->channel = channel;
  398. tx_queue->buffer = NULL;
  399. memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
  400. }
  401. rx_queue = &channel->rx_queue;
  402. rx_queue->buffer = NULL;
  403. memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
  404. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  405. (unsigned long)rx_queue);
  406. return channel;
  407. }
  408. static int efx_probe_channel(struct efx_channel *channel)
  409. {
  410. struct efx_tx_queue *tx_queue;
  411. struct efx_rx_queue *rx_queue;
  412. int rc;
  413. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  414. "creating channel %d\n", channel->channel);
  415. rc = channel->type->pre_probe(channel);
  416. if (rc)
  417. goto fail;
  418. rc = efx_probe_eventq(channel);
  419. if (rc)
  420. goto fail;
  421. efx_for_each_channel_tx_queue(tx_queue, channel) {
  422. rc = efx_probe_tx_queue(tx_queue);
  423. if (rc)
  424. goto fail;
  425. }
  426. efx_for_each_channel_rx_queue(rx_queue, channel) {
  427. rc = efx_probe_rx_queue(rx_queue);
  428. if (rc)
  429. goto fail;
  430. }
  431. channel->n_rx_frm_trunc = 0;
  432. return 0;
  433. fail:
  434. efx_remove_channel(channel);
  435. return rc;
  436. }
  437. static void
  438. efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
  439. {
  440. struct efx_nic *efx = channel->efx;
  441. const char *type;
  442. int number;
  443. number = channel->channel;
  444. if (efx->tx_channel_offset == 0) {
  445. type = "";
  446. } else if (channel->channel < efx->tx_channel_offset) {
  447. type = "-rx";
  448. } else {
  449. type = "-tx";
  450. number -= efx->tx_channel_offset;
  451. }
  452. snprintf(buf, len, "%s%s-%d", efx->name, type, number);
  453. }
  454. static void efx_set_channel_names(struct efx_nic *efx)
  455. {
  456. struct efx_channel *channel;
  457. efx_for_each_channel(channel, efx)
  458. channel->type->get_name(channel,
  459. efx->msi_context[channel->channel].name,
  460. sizeof(efx->msi_context[0].name));
  461. }
  462. static int efx_probe_channels(struct efx_nic *efx)
  463. {
  464. struct efx_channel *channel;
  465. int rc;
  466. /* Restart special buffer allocation */
  467. efx->next_buffer_table = 0;
  468. /* Probe channels in reverse, so that any 'extra' channels
  469. * use the start of the buffer table. This allows the traffic
  470. * channels to be resized without moving them or wasting the
  471. * entries before them.
  472. */
  473. efx_for_each_channel_rev(channel, efx) {
  474. rc = efx_probe_channel(channel);
  475. if (rc) {
  476. netif_err(efx, probe, efx->net_dev,
  477. "failed to create channel %d\n",
  478. channel->channel);
  479. goto fail;
  480. }
  481. }
  482. efx_set_channel_names(efx);
  483. return 0;
  484. fail:
  485. efx_remove_channels(efx);
  486. return rc;
  487. }
  488. /* Channels are shutdown and reinitialised whilst the NIC is running
  489. * to propagate configuration changes (mtu, checksum offload), or
  490. * to clear hardware error conditions
  491. */
  492. static void efx_start_datapath(struct efx_nic *efx)
  493. {
  494. bool old_rx_scatter = efx->rx_scatter;
  495. struct efx_tx_queue *tx_queue;
  496. struct efx_rx_queue *rx_queue;
  497. struct efx_channel *channel;
  498. size_t rx_buf_len;
  499. /* Calculate the rx buffer allocation parameters required to
  500. * support the current MTU, including padding for header
  501. * alignment and overruns.
  502. */
  503. efx->rx_dma_len = (efx->rx_prefix_size +
  504. EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
  505. efx->type->rx_buffer_padding);
  506. rx_buf_len = (sizeof(struct efx_rx_page_state) +
  507. efx->rx_ip_align + efx->rx_dma_len);
  508. if (rx_buf_len <= PAGE_SIZE) {
  509. efx->rx_scatter = efx->type->always_rx_scatter;
  510. efx->rx_buffer_order = 0;
  511. } else if (efx->type->can_rx_scatter) {
  512. BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
  513. BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
  514. 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
  515. EFX_RX_BUF_ALIGNMENT) >
  516. PAGE_SIZE);
  517. efx->rx_scatter = true;
  518. efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
  519. efx->rx_buffer_order = 0;
  520. } else {
  521. efx->rx_scatter = false;
  522. efx->rx_buffer_order = get_order(rx_buf_len);
  523. }
  524. efx_rx_config_page_split(efx);
  525. if (efx->rx_buffer_order)
  526. netif_dbg(efx, drv, efx->net_dev,
  527. "RX buf len=%u; page order=%u batch=%u\n",
  528. efx->rx_dma_len, efx->rx_buffer_order,
  529. efx->rx_pages_per_batch);
  530. else
  531. netif_dbg(efx, drv, efx->net_dev,
  532. "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
  533. efx->rx_dma_len, efx->rx_page_buf_step,
  534. efx->rx_bufs_per_page, efx->rx_pages_per_batch);
  535. /* RX filters may also have scatter-enabled flags */
  536. if (efx->rx_scatter != old_rx_scatter)
  537. efx->type->filter_update_rx_scatter(efx);
  538. /* We must keep at least one descriptor in a TX ring empty.
  539. * We could avoid this when the queue size does not exactly
  540. * match the hardware ring size, but it's not that important.
  541. * Therefore we stop the queue when one more skb might fill
  542. * the ring completely. We wake it when half way back to
  543. * empty.
  544. */
  545. efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
  546. efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
  547. /* Initialise the channels */
  548. efx_for_each_channel(channel, efx) {
  549. efx_for_each_channel_tx_queue(tx_queue, channel) {
  550. efx_init_tx_queue(tx_queue);
  551. atomic_inc(&efx->active_queues);
  552. }
  553. efx_for_each_channel_rx_queue(rx_queue, channel) {
  554. efx_init_rx_queue(rx_queue);
  555. atomic_inc(&efx->active_queues);
  556. efx_stop_eventq(channel);
  557. efx_fast_push_rx_descriptors(rx_queue, false);
  558. efx_start_eventq(channel);
  559. }
  560. WARN_ON(channel->rx_pkt_n_frags);
  561. }
  562. efx_ptp_start_datapath(efx);
  563. if (netif_device_present(efx->net_dev))
  564. netif_tx_wake_all_queues(efx->net_dev);
  565. }
  566. static void efx_stop_datapath(struct efx_nic *efx)
  567. {
  568. struct efx_channel *channel;
  569. struct efx_tx_queue *tx_queue;
  570. struct efx_rx_queue *rx_queue;
  571. int rc;
  572. EFX_ASSERT_RESET_SERIALISED(efx);
  573. BUG_ON(efx->port_enabled);
  574. efx_ptp_stop_datapath(efx);
  575. /* Stop RX refill */
  576. efx_for_each_channel(channel, efx) {
  577. efx_for_each_channel_rx_queue(rx_queue, channel)
  578. rx_queue->refill_enabled = false;
  579. }
  580. efx_for_each_channel(channel, efx) {
  581. /* RX packet processing is pipelined, so wait for the
  582. * NAPI handler to complete. At least event queue 0
  583. * might be kept active by non-data events, so don't
  584. * use napi_synchronize() but actually disable NAPI
  585. * temporarily.
  586. */
  587. if (efx_channel_has_rx_queue(channel)) {
  588. efx_stop_eventq(channel);
  589. efx_start_eventq(channel);
  590. }
  591. }
  592. rc = efx->type->fini_dmaq(efx);
  593. if (rc && EFX_WORKAROUND_7803(efx)) {
  594. /* Schedule a reset to recover from the flush failure. The
  595. * descriptor caches reference memory we're about to free,
  596. * but falcon_reconfigure_mac_wrapper() won't reconnect
  597. * the MACs because of the pending reset.
  598. */
  599. netif_err(efx, drv, efx->net_dev,
  600. "Resetting to recover from flush failure\n");
  601. efx_schedule_reset(efx, RESET_TYPE_ALL);
  602. } else if (rc) {
  603. netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
  604. } else {
  605. netif_dbg(efx, drv, efx->net_dev,
  606. "successfully flushed all queues\n");
  607. }
  608. efx_for_each_channel(channel, efx) {
  609. efx_for_each_channel_rx_queue(rx_queue, channel)
  610. efx_fini_rx_queue(rx_queue);
  611. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  612. efx_fini_tx_queue(tx_queue);
  613. }
  614. }
  615. static void efx_remove_channel(struct efx_channel *channel)
  616. {
  617. struct efx_tx_queue *tx_queue;
  618. struct efx_rx_queue *rx_queue;
  619. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  620. "destroy chan %d\n", channel->channel);
  621. efx_for_each_channel_rx_queue(rx_queue, channel)
  622. efx_remove_rx_queue(rx_queue);
  623. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  624. efx_remove_tx_queue(tx_queue);
  625. efx_remove_eventq(channel);
  626. channel->type->post_remove(channel);
  627. }
  628. static void efx_remove_channels(struct efx_nic *efx)
  629. {
  630. struct efx_channel *channel;
  631. efx_for_each_channel(channel, efx)
  632. efx_remove_channel(channel);
  633. }
  634. int
  635. efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
  636. {
  637. struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
  638. u32 old_rxq_entries, old_txq_entries;
  639. unsigned i, next_buffer_table = 0;
  640. int rc, rc2;
  641. rc = efx_check_disabled(efx);
  642. if (rc)
  643. return rc;
  644. /* Not all channels should be reallocated. We must avoid
  645. * reallocating their buffer table entries.
  646. */
  647. efx_for_each_channel(channel, efx) {
  648. struct efx_rx_queue *rx_queue;
  649. struct efx_tx_queue *tx_queue;
  650. if (channel->type->copy)
  651. continue;
  652. next_buffer_table = max(next_buffer_table,
  653. channel->eventq.index +
  654. channel->eventq.entries);
  655. efx_for_each_channel_rx_queue(rx_queue, channel)
  656. next_buffer_table = max(next_buffer_table,
  657. rx_queue->rxd.index +
  658. rx_queue->rxd.entries);
  659. efx_for_each_channel_tx_queue(tx_queue, channel)
  660. next_buffer_table = max(next_buffer_table,
  661. tx_queue->txd.index +
  662. tx_queue->txd.entries);
  663. }
  664. efx_device_detach_sync(efx);
  665. efx_stop_all(efx);
  666. efx_soft_disable_interrupts(efx);
  667. /* Clone channels (where possible) */
  668. memset(other_channel, 0, sizeof(other_channel));
  669. for (i = 0; i < efx->n_channels; i++) {
  670. channel = efx->channel[i];
  671. if (channel->type->copy)
  672. channel = channel->type->copy(channel);
  673. if (!channel) {
  674. rc = -ENOMEM;
  675. goto out;
  676. }
  677. other_channel[i] = channel;
  678. }
  679. /* Swap entry counts and channel pointers */
  680. old_rxq_entries = efx->rxq_entries;
  681. old_txq_entries = efx->txq_entries;
  682. efx->rxq_entries = rxq_entries;
  683. efx->txq_entries = txq_entries;
  684. for (i = 0; i < efx->n_channels; i++) {
  685. channel = efx->channel[i];
  686. efx->channel[i] = other_channel[i];
  687. other_channel[i] = channel;
  688. }
  689. /* Restart buffer table allocation */
  690. efx->next_buffer_table = next_buffer_table;
  691. for (i = 0; i < efx->n_channels; i++) {
  692. channel = efx->channel[i];
  693. if (!channel->type->copy)
  694. continue;
  695. rc = efx_probe_channel(channel);
  696. if (rc)
  697. goto rollback;
  698. efx_init_napi_channel(efx->channel[i]);
  699. }
  700. out:
  701. /* Destroy unused channel structures */
  702. for (i = 0; i < efx->n_channels; i++) {
  703. channel = other_channel[i];
  704. if (channel && channel->type->copy) {
  705. efx_fini_napi_channel(channel);
  706. efx_remove_channel(channel);
  707. kfree(channel);
  708. }
  709. }
  710. rc2 = efx_soft_enable_interrupts(efx);
  711. if (rc2) {
  712. rc = rc ? rc : rc2;
  713. netif_err(efx, drv, efx->net_dev,
  714. "unable to restart interrupts on channel reallocation\n");
  715. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  716. } else {
  717. efx_start_all(efx);
  718. netif_device_attach(efx->net_dev);
  719. }
  720. return rc;
  721. rollback:
  722. /* Swap back */
  723. efx->rxq_entries = old_rxq_entries;
  724. efx->txq_entries = old_txq_entries;
  725. for (i = 0; i < efx->n_channels; i++) {
  726. channel = efx->channel[i];
  727. efx->channel[i] = other_channel[i];
  728. other_channel[i] = channel;
  729. }
  730. goto out;
  731. }
  732. void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
  733. {
  734. mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
  735. }
  736. static const struct efx_channel_type efx_default_channel_type = {
  737. .pre_probe = efx_channel_dummy_op_int,
  738. .post_remove = efx_channel_dummy_op_void,
  739. .get_name = efx_get_channel_name,
  740. .copy = efx_copy_channel,
  741. .keep_eventq = false,
  742. };
  743. int efx_channel_dummy_op_int(struct efx_channel *channel)
  744. {
  745. return 0;
  746. }
  747. void efx_channel_dummy_op_void(struct efx_channel *channel)
  748. {
  749. }
  750. /**************************************************************************
  751. *
  752. * Port handling
  753. *
  754. **************************************************************************/
  755. /* This ensures that the kernel is kept informed (via
  756. * netif_carrier_on/off) of the link status, and also maintains the
  757. * link status's stop on the port's TX queue.
  758. */
  759. void efx_link_status_changed(struct efx_nic *efx)
  760. {
  761. struct efx_link_state *link_state = &efx->link_state;
  762. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  763. * that no events are triggered between unregister_netdev() and the
  764. * driver unloading. A more general condition is that NETDEV_CHANGE
  765. * can only be generated between NETDEV_UP and NETDEV_DOWN */
  766. if (!netif_running(efx->net_dev))
  767. return;
  768. if (link_state->up != netif_carrier_ok(efx->net_dev)) {
  769. efx->n_link_state_changes++;
  770. if (link_state->up)
  771. netif_carrier_on(efx->net_dev);
  772. else
  773. netif_carrier_off(efx->net_dev);
  774. }
  775. /* Status message for kernel log */
  776. if (link_state->up)
  777. netif_info(efx, link, efx->net_dev,
  778. "link up at %uMbps %s-duplex (MTU %d)\n",
  779. link_state->speed, link_state->fd ? "full" : "half",
  780. efx->net_dev->mtu);
  781. else
  782. netif_info(efx, link, efx->net_dev, "link down\n");
  783. }
  784. void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
  785. {
  786. efx->link_advertising = advertising;
  787. if (advertising) {
  788. if (advertising & ADVERTISED_Pause)
  789. efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
  790. else
  791. efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
  792. if (advertising & ADVERTISED_Asym_Pause)
  793. efx->wanted_fc ^= EFX_FC_TX;
  794. }
  795. }
  796. void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
  797. {
  798. efx->wanted_fc = wanted_fc;
  799. if (efx->link_advertising) {
  800. if (wanted_fc & EFX_FC_RX)
  801. efx->link_advertising |= (ADVERTISED_Pause |
  802. ADVERTISED_Asym_Pause);
  803. else
  804. efx->link_advertising &= ~(ADVERTISED_Pause |
  805. ADVERTISED_Asym_Pause);
  806. if (wanted_fc & EFX_FC_TX)
  807. efx->link_advertising ^= ADVERTISED_Asym_Pause;
  808. }
  809. }
  810. static void efx_fini_port(struct efx_nic *efx);
  811. /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
  812. * the MAC appropriately. All other PHY configuration changes are pushed
  813. * through phy_op->set_settings(), and pushed asynchronously to the MAC
  814. * through efx_monitor().
  815. *
  816. * Callers must hold the mac_lock
  817. */
  818. int __efx_reconfigure_port(struct efx_nic *efx)
  819. {
  820. enum efx_phy_mode phy_mode;
  821. int rc;
  822. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  823. /* Disable PHY transmit in mac level loopbacks */
  824. phy_mode = efx->phy_mode;
  825. if (LOOPBACK_INTERNAL(efx))
  826. efx->phy_mode |= PHY_MODE_TX_DISABLED;
  827. else
  828. efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
  829. rc = efx->type->reconfigure_port(efx);
  830. if (rc)
  831. efx->phy_mode = phy_mode;
  832. return rc;
  833. }
  834. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  835. * disabled. */
  836. int efx_reconfigure_port(struct efx_nic *efx)
  837. {
  838. int rc;
  839. EFX_ASSERT_RESET_SERIALISED(efx);
  840. mutex_lock(&efx->mac_lock);
  841. rc = __efx_reconfigure_port(efx);
  842. mutex_unlock(&efx->mac_lock);
  843. return rc;
  844. }
  845. /* Asynchronous work item for changing MAC promiscuity and multicast
  846. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  847. * MAC directly. */
  848. static void efx_mac_work(struct work_struct *data)
  849. {
  850. struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
  851. mutex_lock(&efx->mac_lock);
  852. if (efx->port_enabled)
  853. efx->type->reconfigure_mac(efx);
  854. mutex_unlock(&efx->mac_lock);
  855. }
  856. static int efx_probe_port(struct efx_nic *efx)
  857. {
  858. int rc;
  859. netif_dbg(efx, probe, efx->net_dev, "create port\n");
  860. if (phy_flash_cfg)
  861. efx->phy_mode = PHY_MODE_SPECIAL;
  862. /* Connect up MAC/PHY operations table */
  863. rc = efx->type->probe_port(efx);
  864. if (rc)
  865. return rc;
  866. /* Initialise MAC address to permanent address */
  867. memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
  868. return 0;
  869. }
  870. static int efx_init_port(struct efx_nic *efx)
  871. {
  872. int rc;
  873. netif_dbg(efx, drv, efx->net_dev, "init port\n");
  874. mutex_lock(&efx->mac_lock);
  875. rc = efx->phy_op->init(efx);
  876. if (rc)
  877. goto fail1;
  878. efx->port_initialized = true;
  879. /* Reconfigure the MAC before creating dma queues (required for
  880. * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
  881. efx->type->reconfigure_mac(efx);
  882. /* Ensure the PHY advertises the correct flow control settings */
  883. rc = efx->phy_op->reconfigure(efx);
  884. if (rc)
  885. goto fail2;
  886. mutex_unlock(&efx->mac_lock);
  887. return 0;
  888. fail2:
  889. efx->phy_op->fini(efx);
  890. fail1:
  891. mutex_unlock(&efx->mac_lock);
  892. return rc;
  893. }
  894. static void efx_start_port(struct efx_nic *efx)
  895. {
  896. netif_dbg(efx, ifup, efx->net_dev, "start port\n");
  897. BUG_ON(efx->port_enabled);
  898. mutex_lock(&efx->mac_lock);
  899. efx->port_enabled = true;
  900. /* Ensure MAC ingress/egress is enabled */
  901. efx->type->reconfigure_mac(efx);
  902. mutex_unlock(&efx->mac_lock);
  903. }
  904. /* Cancel work for MAC reconfiguration, periodic hardware monitoring
  905. * and the async self-test, wait for them to finish and prevent them
  906. * being scheduled again. This doesn't cover online resets, which
  907. * should only be cancelled when removing the device.
  908. */
  909. static void efx_stop_port(struct efx_nic *efx)
  910. {
  911. netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
  912. EFX_ASSERT_RESET_SERIALISED(efx);
  913. mutex_lock(&efx->mac_lock);
  914. efx->port_enabled = false;
  915. mutex_unlock(&efx->mac_lock);
  916. /* Serialise against efx_set_multicast_list() */
  917. netif_addr_lock_bh(efx->net_dev);
  918. netif_addr_unlock_bh(efx->net_dev);
  919. cancel_delayed_work_sync(&efx->monitor_work);
  920. efx_selftest_async_cancel(efx);
  921. cancel_work_sync(&efx->mac_work);
  922. }
  923. static void efx_fini_port(struct efx_nic *efx)
  924. {
  925. netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
  926. if (!efx->port_initialized)
  927. return;
  928. efx->phy_op->fini(efx);
  929. efx->port_initialized = false;
  930. efx->link_state.up = false;
  931. efx_link_status_changed(efx);
  932. }
  933. static void efx_remove_port(struct efx_nic *efx)
  934. {
  935. netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
  936. efx->type->remove_port(efx);
  937. }
  938. /**************************************************************************
  939. *
  940. * NIC handling
  941. *
  942. **************************************************************************/
  943. static LIST_HEAD(efx_primary_list);
  944. static LIST_HEAD(efx_unassociated_list);
  945. static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
  946. {
  947. return left->type == right->type &&
  948. left->vpd_sn && right->vpd_sn &&
  949. !strcmp(left->vpd_sn, right->vpd_sn);
  950. }
  951. static void efx_associate(struct efx_nic *efx)
  952. {
  953. struct efx_nic *other, *next;
  954. if (efx->primary == efx) {
  955. /* Adding primary function; look for secondaries */
  956. netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
  957. list_add_tail(&efx->node, &efx_primary_list);
  958. list_for_each_entry_safe(other, next, &efx_unassociated_list,
  959. node) {
  960. if (efx_same_controller(efx, other)) {
  961. list_del(&other->node);
  962. netif_dbg(other, probe, other->net_dev,
  963. "moving to secondary list of %s %s\n",
  964. pci_name(efx->pci_dev),
  965. efx->net_dev->name);
  966. list_add_tail(&other->node,
  967. &efx->secondary_list);
  968. other->primary = efx;
  969. }
  970. }
  971. } else {
  972. /* Adding secondary function; look for primary */
  973. list_for_each_entry(other, &efx_primary_list, node) {
  974. if (efx_same_controller(efx, other)) {
  975. netif_dbg(efx, probe, efx->net_dev,
  976. "adding to secondary list of %s %s\n",
  977. pci_name(other->pci_dev),
  978. other->net_dev->name);
  979. list_add_tail(&efx->node,
  980. &other->secondary_list);
  981. efx->primary = other;
  982. return;
  983. }
  984. }
  985. netif_dbg(efx, probe, efx->net_dev,
  986. "adding to unassociated list\n");
  987. list_add_tail(&efx->node, &efx_unassociated_list);
  988. }
  989. }
  990. static void efx_dissociate(struct efx_nic *efx)
  991. {
  992. struct efx_nic *other, *next;
  993. list_del(&efx->node);
  994. efx->primary = NULL;
  995. list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
  996. list_del(&other->node);
  997. netif_dbg(other, probe, other->net_dev,
  998. "moving to unassociated list\n");
  999. list_add_tail(&other->node, &efx_unassociated_list);
  1000. other->primary = NULL;
  1001. }
  1002. }
  1003. /* This configures the PCI device to enable I/O and DMA. */
  1004. static int efx_init_io(struct efx_nic *efx)
  1005. {
  1006. struct pci_dev *pci_dev = efx->pci_dev;
  1007. dma_addr_t dma_mask = efx->type->max_dma_mask;
  1008. unsigned int mem_map_size = efx->type->mem_map_size(efx);
  1009. int rc;
  1010. netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
  1011. rc = pci_enable_device(pci_dev);
  1012. if (rc) {
  1013. netif_err(efx, probe, efx->net_dev,
  1014. "failed to enable PCI device\n");
  1015. goto fail1;
  1016. }
  1017. pci_set_master(pci_dev);
  1018. /* Set the PCI DMA mask. Try all possibilities from our
  1019. * genuine mask down to 32 bits, because some architectures
  1020. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  1021. * masks event though they reject 46 bit masks.
  1022. */
  1023. while (dma_mask > 0x7fffffffUL) {
  1024. if (dma_supported(&pci_dev->dev, dma_mask)) {
  1025. rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
  1026. if (rc == 0)
  1027. break;
  1028. }
  1029. dma_mask >>= 1;
  1030. }
  1031. if (rc) {
  1032. netif_err(efx, probe, efx->net_dev,
  1033. "could not find a suitable DMA mask\n");
  1034. goto fail2;
  1035. }
  1036. netif_dbg(efx, probe, efx->net_dev,
  1037. "using DMA mask %llx\n", (unsigned long long) dma_mask);
  1038. efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
  1039. rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
  1040. if (rc) {
  1041. netif_err(efx, probe, efx->net_dev,
  1042. "request for memory BAR failed\n");
  1043. rc = -EIO;
  1044. goto fail3;
  1045. }
  1046. efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
  1047. if (!efx->membase) {
  1048. netif_err(efx, probe, efx->net_dev,
  1049. "could not map memory BAR at %llx+%x\n",
  1050. (unsigned long long)efx->membase_phys, mem_map_size);
  1051. rc = -ENOMEM;
  1052. goto fail4;
  1053. }
  1054. netif_dbg(efx, probe, efx->net_dev,
  1055. "memory BAR at %llx+%x (virtual %p)\n",
  1056. (unsigned long long)efx->membase_phys, mem_map_size,
  1057. efx->membase);
  1058. return 0;
  1059. fail4:
  1060. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  1061. fail3:
  1062. efx->membase_phys = 0;
  1063. fail2:
  1064. pci_disable_device(efx->pci_dev);
  1065. fail1:
  1066. return rc;
  1067. }
  1068. static void efx_fini_io(struct efx_nic *efx)
  1069. {
  1070. netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
  1071. if (efx->membase) {
  1072. iounmap(efx->membase);
  1073. efx->membase = NULL;
  1074. }
  1075. if (efx->membase_phys) {
  1076. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  1077. efx->membase_phys = 0;
  1078. }
  1079. pci_disable_device(efx->pci_dev);
  1080. }
  1081. static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
  1082. {
  1083. cpumask_var_t thread_mask;
  1084. unsigned int count;
  1085. int cpu;
  1086. if (rss_cpus) {
  1087. count = rss_cpus;
  1088. } else {
  1089. if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
  1090. netif_warn(efx, probe, efx->net_dev,
  1091. "RSS disabled due to allocation failure\n");
  1092. return 1;
  1093. }
  1094. count = 0;
  1095. for_each_online_cpu(cpu) {
  1096. if (!cpumask_test_cpu(cpu, thread_mask)) {
  1097. ++count;
  1098. cpumask_or(thread_mask, thread_mask,
  1099. topology_thread_cpumask(cpu));
  1100. }
  1101. }
  1102. free_cpumask_var(thread_mask);
  1103. }
  1104. /* If RSS is requested for the PF *and* VFs then we can't write RSS
  1105. * table entries that are inaccessible to VFs
  1106. */
  1107. if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
  1108. count > efx_vf_size(efx)) {
  1109. netif_warn(efx, probe, efx->net_dev,
  1110. "Reducing number of RSS channels from %u to %u for "
  1111. "VF support. Increase vf-msix-limit to use more "
  1112. "channels on the PF.\n",
  1113. count, efx_vf_size(efx));
  1114. count = efx_vf_size(efx);
  1115. }
  1116. return count;
  1117. }
  1118. /* Probe the number and type of interrupts we are able to obtain, and
  1119. * the resulting numbers of channels and RX queues.
  1120. */
  1121. static int efx_probe_interrupts(struct efx_nic *efx)
  1122. {
  1123. unsigned int extra_channels = 0;
  1124. unsigned int i, j;
  1125. int rc;
  1126. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
  1127. if (efx->extra_channel_type[i])
  1128. ++extra_channels;
  1129. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  1130. struct msix_entry xentries[EFX_MAX_CHANNELS];
  1131. unsigned int n_channels;
  1132. n_channels = efx_wanted_parallelism(efx);
  1133. if (separate_tx_channels)
  1134. n_channels *= 2;
  1135. n_channels += extra_channels;
  1136. n_channels = min(n_channels, efx->max_channels);
  1137. for (i = 0; i < n_channels; i++)
  1138. xentries[i].entry = i;
  1139. rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
  1140. if (rc > 0) {
  1141. netif_err(efx, drv, efx->net_dev,
  1142. "WARNING: Insufficient MSI-X vectors"
  1143. " available (%d < %u).\n", rc, n_channels);
  1144. netif_err(efx, drv, efx->net_dev,
  1145. "WARNING: Performance may be reduced.\n");
  1146. EFX_BUG_ON_PARANOID(rc >= n_channels);
  1147. n_channels = rc;
  1148. rc = pci_enable_msix(efx->pci_dev, xentries,
  1149. n_channels);
  1150. }
  1151. if (rc == 0) {
  1152. efx->n_channels = n_channels;
  1153. if (n_channels > extra_channels)
  1154. n_channels -= extra_channels;
  1155. if (separate_tx_channels) {
  1156. efx->n_tx_channels = max(n_channels / 2, 1U);
  1157. efx->n_rx_channels = max(n_channels -
  1158. efx->n_tx_channels,
  1159. 1U);
  1160. } else {
  1161. efx->n_tx_channels = n_channels;
  1162. efx->n_rx_channels = n_channels;
  1163. }
  1164. for (i = 0; i < efx->n_channels; i++)
  1165. efx_get_channel(efx, i)->irq =
  1166. xentries[i].vector;
  1167. } else {
  1168. /* Fall back to single channel MSI */
  1169. efx->interrupt_mode = EFX_INT_MODE_MSI;
  1170. netif_err(efx, drv, efx->net_dev,
  1171. "could not enable MSI-X\n");
  1172. }
  1173. }
  1174. /* Try single interrupt MSI */
  1175. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  1176. efx->n_channels = 1;
  1177. efx->n_rx_channels = 1;
  1178. efx->n_tx_channels = 1;
  1179. rc = pci_enable_msi(efx->pci_dev);
  1180. if (rc == 0) {
  1181. efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
  1182. } else {
  1183. netif_err(efx, drv, efx->net_dev,
  1184. "could not enable MSI\n");
  1185. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  1186. }
  1187. }
  1188. /* Assume legacy interrupts */
  1189. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  1190. efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
  1191. efx->n_rx_channels = 1;
  1192. efx->n_tx_channels = 1;
  1193. efx->legacy_irq = efx->pci_dev->irq;
  1194. }
  1195. /* Assign extra channels if possible */
  1196. j = efx->n_channels;
  1197. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
  1198. if (!efx->extra_channel_type[i])
  1199. continue;
  1200. if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
  1201. efx->n_channels <= extra_channels) {
  1202. efx->extra_channel_type[i]->handle_no_channel(efx);
  1203. } else {
  1204. --j;
  1205. efx_get_channel(efx, j)->type =
  1206. efx->extra_channel_type[i];
  1207. }
  1208. }
  1209. /* RSS might be usable on VFs even if it is disabled on the PF */
  1210. efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
  1211. efx->n_rx_channels : efx_vf_size(efx));
  1212. return 0;
  1213. }
  1214. static int efx_soft_enable_interrupts(struct efx_nic *efx)
  1215. {
  1216. struct efx_channel *channel, *end_channel;
  1217. int rc;
  1218. BUG_ON(efx->state == STATE_DISABLED);
  1219. efx->irq_soft_enabled = true;
  1220. smp_wmb();
  1221. efx_for_each_channel(channel, efx) {
  1222. if (!channel->type->keep_eventq) {
  1223. rc = efx_init_eventq(channel);
  1224. if (rc)
  1225. goto fail;
  1226. }
  1227. efx_start_eventq(channel);
  1228. }
  1229. efx_mcdi_mode_event(efx);
  1230. return 0;
  1231. fail:
  1232. end_channel = channel;
  1233. efx_for_each_channel(channel, efx) {
  1234. if (channel == end_channel)
  1235. break;
  1236. efx_stop_eventq(channel);
  1237. if (!channel->type->keep_eventq)
  1238. efx_fini_eventq(channel);
  1239. }
  1240. return rc;
  1241. }
  1242. static void efx_soft_disable_interrupts(struct efx_nic *efx)
  1243. {
  1244. struct efx_channel *channel;
  1245. if (efx->state == STATE_DISABLED)
  1246. return;
  1247. efx_mcdi_mode_poll(efx);
  1248. efx->irq_soft_enabled = false;
  1249. smp_wmb();
  1250. if (efx->legacy_irq)
  1251. synchronize_irq(efx->legacy_irq);
  1252. efx_for_each_channel(channel, efx) {
  1253. if (channel->irq)
  1254. synchronize_irq(channel->irq);
  1255. efx_stop_eventq(channel);
  1256. if (!channel->type->keep_eventq)
  1257. efx_fini_eventq(channel);
  1258. }
  1259. /* Flush the asynchronous MCDI request queue */
  1260. efx_mcdi_flush_async(efx);
  1261. }
  1262. static int efx_enable_interrupts(struct efx_nic *efx)
  1263. {
  1264. struct efx_channel *channel, *end_channel;
  1265. int rc;
  1266. BUG_ON(efx->state == STATE_DISABLED);
  1267. if (efx->eeh_disabled_legacy_irq) {
  1268. enable_irq(efx->legacy_irq);
  1269. efx->eeh_disabled_legacy_irq = false;
  1270. }
  1271. efx->type->irq_enable_master(efx);
  1272. efx_for_each_channel(channel, efx) {
  1273. if (channel->type->keep_eventq) {
  1274. rc = efx_init_eventq(channel);
  1275. if (rc)
  1276. goto fail;
  1277. }
  1278. }
  1279. rc = efx_soft_enable_interrupts(efx);
  1280. if (rc)
  1281. goto fail;
  1282. return 0;
  1283. fail:
  1284. end_channel = channel;
  1285. efx_for_each_channel(channel, efx) {
  1286. if (channel == end_channel)
  1287. break;
  1288. if (channel->type->keep_eventq)
  1289. efx_fini_eventq(channel);
  1290. }
  1291. efx->type->irq_disable_non_ev(efx);
  1292. return rc;
  1293. }
  1294. static void efx_disable_interrupts(struct efx_nic *efx)
  1295. {
  1296. struct efx_channel *channel;
  1297. efx_soft_disable_interrupts(efx);
  1298. efx_for_each_channel(channel, efx) {
  1299. if (channel->type->keep_eventq)
  1300. efx_fini_eventq(channel);
  1301. }
  1302. efx->type->irq_disable_non_ev(efx);
  1303. }
  1304. static void efx_remove_interrupts(struct efx_nic *efx)
  1305. {
  1306. struct efx_channel *channel;
  1307. /* Remove MSI/MSI-X interrupts */
  1308. efx_for_each_channel(channel, efx)
  1309. channel->irq = 0;
  1310. pci_disable_msi(efx->pci_dev);
  1311. pci_disable_msix(efx->pci_dev);
  1312. /* Remove legacy interrupt */
  1313. efx->legacy_irq = 0;
  1314. }
  1315. static void efx_set_channels(struct efx_nic *efx)
  1316. {
  1317. struct efx_channel *channel;
  1318. struct efx_tx_queue *tx_queue;
  1319. efx->tx_channel_offset =
  1320. separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
  1321. /* We need to mark which channels really have RX and TX
  1322. * queues, and adjust the TX queue numbers if we have separate
  1323. * RX-only and TX-only channels.
  1324. */
  1325. efx_for_each_channel(channel, efx) {
  1326. if (channel->channel < efx->n_rx_channels)
  1327. channel->rx_queue.core_index = channel->channel;
  1328. else
  1329. channel->rx_queue.core_index = -1;
  1330. efx_for_each_channel_tx_queue(tx_queue, channel)
  1331. tx_queue->queue -= (efx->tx_channel_offset *
  1332. EFX_TXQ_TYPES);
  1333. }
  1334. }
  1335. static int efx_probe_nic(struct efx_nic *efx)
  1336. {
  1337. size_t i;
  1338. int rc;
  1339. netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
  1340. /* Carry out hardware-type specific initialisation */
  1341. rc = efx->type->probe(efx);
  1342. if (rc)
  1343. return rc;
  1344. /* Determine the number of channels and queues by trying to hook
  1345. * in MSI-X interrupts. */
  1346. rc = efx_probe_interrupts(efx);
  1347. if (rc)
  1348. goto fail1;
  1349. rc = efx->type->dimension_resources(efx);
  1350. if (rc)
  1351. goto fail2;
  1352. if (efx->n_channels > 1)
  1353. get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
  1354. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
  1355. efx->rx_indir_table[i] =
  1356. ethtool_rxfh_indir_default(i, efx->rss_spread);
  1357. efx_set_channels(efx);
  1358. netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
  1359. netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
  1360. /* Initialise the interrupt moderation settings */
  1361. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
  1362. true);
  1363. return 0;
  1364. fail2:
  1365. efx_remove_interrupts(efx);
  1366. fail1:
  1367. efx->type->remove(efx);
  1368. return rc;
  1369. }
  1370. static void efx_remove_nic(struct efx_nic *efx)
  1371. {
  1372. netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
  1373. efx_remove_interrupts(efx);
  1374. efx->type->remove(efx);
  1375. }
  1376. static int efx_probe_filters(struct efx_nic *efx)
  1377. {
  1378. int rc;
  1379. spin_lock_init(&efx->filter_lock);
  1380. rc = efx->type->filter_table_probe(efx);
  1381. if (rc)
  1382. return rc;
  1383. #ifdef CONFIG_RFS_ACCEL
  1384. if (efx->type->offload_features & NETIF_F_NTUPLE) {
  1385. efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
  1386. sizeof(*efx->rps_flow_id),
  1387. GFP_KERNEL);
  1388. if (!efx->rps_flow_id) {
  1389. efx->type->filter_table_remove(efx);
  1390. return -ENOMEM;
  1391. }
  1392. }
  1393. #endif
  1394. return 0;
  1395. }
  1396. static void efx_remove_filters(struct efx_nic *efx)
  1397. {
  1398. #ifdef CONFIG_RFS_ACCEL
  1399. kfree(efx->rps_flow_id);
  1400. #endif
  1401. efx->type->filter_table_remove(efx);
  1402. }
  1403. static void efx_restore_filters(struct efx_nic *efx)
  1404. {
  1405. efx->type->filter_table_restore(efx);
  1406. }
  1407. /**************************************************************************
  1408. *
  1409. * NIC startup/shutdown
  1410. *
  1411. *************************************************************************/
  1412. static int efx_probe_all(struct efx_nic *efx)
  1413. {
  1414. int rc;
  1415. rc = efx_probe_nic(efx);
  1416. if (rc) {
  1417. netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
  1418. goto fail1;
  1419. }
  1420. rc = efx_probe_port(efx);
  1421. if (rc) {
  1422. netif_err(efx, probe, efx->net_dev, "failed to create port\n");
  1423. goto fail2;
  1424. }
  1425. BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
  1426. if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
  1427. rc = -EINVAL;
  1428. goto fail3;
  1429. }
  1430. efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
  1431. rc = efx_probe_filters(efx);
  1432. if (rc) {
  1433. netif_err(efx, probe, efx->net_dev,
  1434. "failed to create filter tables\n");
  1435. goto fail3;
  1436. }
  1437. rc = efx_probe_channels(efx);
  1438. if (rc)
  1439. goto fail4;
  1440. return 0;
  1441. fail4:
  1442. efx_remove_filters(efx);
  1443. fail3:
  1444. efx_remove_port(efx);
  1445. fail2:
  1446. efx_remove_nic(efx);
  1447. fail1:
  1448. return rc;
  1449. }
  1450. /* If the interface is supposed to be running but is not, start
  1451. * the hardware and software data path, regular activity for the port
  1452. * (MAC statistics, link polling, etc.) and schedule the port to be
  1453. * reconfigured. Interrupts must already be enabled. This function
  1454. * is safe to call multiple times, so long as the NIC is not disabled.
  1455. * Requires the RTNL lock.
  1456. */
  1457. static void efx_start_all(struct efx_nic *efx)
  1458. {
  1459. EFX_ASSERT_RESET_SERIALISED(efx);
  1460. BUG_ON(efx->state == STATE_DISABLED);
  1461. /* Check that it is appropriate to restart the interface. All
  1462. * of these flags are safe to read under just the rtnl lock */
  1463. if (efx->port_enabled || !netif_running(efx->net_dev))
  1464. return;
  1465. efx_start_port(efx);
  1466. efx_start_datapath(efx);
  1467. /* Start the hardware monitor if there is one */
  1468. if (efx->type->monitor != NULL)
  1469. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1470. efx_monitor_interval);
  1471. /* If link state detection is normally event-driven, we have
  1472. * to poll now because we could have missed a change
  1473. */
  1474. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
  1475. mutex_lock(&efx->mac_lock);
  1476. if (efx->phy_op->poll(efx))
  1477. efx_link_status_changed(efx);
  1478. mutex_unlock(&efx->mac_lock);
  1479. }
  1480. efx->type->start_stats(efx);
  1481. efx->type->pull_stats(efx);
  1482. spin_lock_bh(&efx->stats_lock);
  1483. efx->type->update_stats(efx, NULL, NULL);
  1484. spin_unlock_bh(&efx->stats_lock);
  1485. }
  1486. /* Quiesce the hardware and software data path, and regular activity
  1487. * for the port without bringing the link down. Safe to call multiple
  1488. * times with the NIC in almost any state, but interrupts should be
  1489. * enabled. Requires the RTNL lock.
  1490. */
  1491. static void efx_stop_all(struct efx_nic *efx)
  1492. {
  1493. EFX_ASSERT_RESET_SERIALISED(efx);
  1494. /* port_enabled can be read safely under the rtnl lock */
  1495. if (!efx->port_enabled)
  1496. return;
  1497. /* update stats before we go down so we can accurately count
  1498. * rx_nodesc_drops
  1499. */
  1500. efx->type->pull_stats(efx);
  1501. spin_lock_bh(&efx->stats_lock);
  1502. efx->type->update_stats(efx, NULL, NULL);
  1503. spin_unlock_bh(&efx->stats_lock);
  1504. efx->type->stop_stats(efx);
  1505. efx_stop_port(efx);
  1506. /* Stop the kernel transmit interface. This is only valid if
  1507. * the device is stopped or detached; otherwise the watchdog
  1508. * may fire immediately.
  1509. */
  1510. WARN_ON(netif_running(efx->net_dev) &&
  1511. netif_device_present(efx->net_dev));
  1512. netif_tx_disable(efx->net_dev);
  1513. efx_stop_datapath(efx);
  1514. }
  1515. static void efx_remove_all(struct efx_nic *efx)
  1516. {
  1517. efx_remove_channels(efx);
  1518. efx_remove_filters(efx);
  1519. efx_remove_port(efx);
  1520. efx_remove_nic(efx);
  1521. }
  1522. /**************************************************************************
  1523. *
  1524. * Interrupt moderation
  1525. *
  1526. **************************************************************************/
  1527. static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
  1528. {
  1529. if (usecs == 0)
  1530. return 0;
  1531. if (usecs * 1000 < quantum_ns)
  1532. return 1; /* never round down to 0 */
  1533. return usecs * 1000 / quantum_ns;
  1534. }
  1535. /* Set interrupt moderation parameters */
  1536. int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
  1537. unsigned int rx_usecs, bool rx_adaptive,
  1538. bool rx_may_override_tx)
  1539. {
  1540. struct efx_channel *channel;
  1541. unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
  1542. efx->timer_quantum_ns,
  1543. 1000);
  1544. unsigned int tx_ticks;
  1545. unsigned int rx_ticks;
  1546. EFX_ASSERT_RESET_SERIALISED(efx);
  1547. if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
  1548. return -EINVAL;
  1549. tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
  1550. rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
  1551. if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
  1552. !rx_may_override_tx) {
  1553. netif_err(efx, drv, efx->net_dev, "Channels are shared. "
  1554. "RX and TX IRQ moderation must be equal\n");
  1555. return -EINVAL;
  1556. }
  1557. efx->irq_rx_adaptive = rx_adaptive;
  1558. efx->irq_rx_moderation = rx_ticks;
  1559. efx_for_each_channel(channel, efx) {
  1560. if (efx_channel_has_rx_queue(channel))
  1561. channel->irq_moderation = rx_ticks;
  1562. else if (efx_channel_has_tx_queues(channel))
  1563. channel->irq_moderation = tx_ticks;
  1564. }
  1565. return 0;
  1566. }
  1567. void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
  1568. unsigned int *rx_usecs, bool *rx_adaptive)
  1569. {
  1570. /* We must round up when converting ticks to microseconds
  1571. * because we round down when converting the other way.
  1572. */
  1573. *rx_adaptive = efx->irq_rx_adaptive;
  1574. *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
  1575. efx->timer_quantum_ns,
  1576. 1000);
  1577. /* If channels are shared between RX and TX, so is IRQ
  1578. * moderation. Otherwise, IRQ moderation is the same for all
  1579. * TX channels and is not adaptive.
  1580. */
  1581. if (efx->tx_channel_offset == 0)
  1582. *tx_usecs = *rx_usecs;
  1583. else
  1584. *tx_usecs = DIV_ROUND_UP(
  1585. efx->channel[efx->tx_channel_offset]->irq_moderation *
  1586. efx->timer_quantum_ns,
  1587. 1000);
  1588. }
  1589. /**************************************************************************
  1590. *
  1591. * Hardware monitor
  1592. *
  1593. **************************************************************************/
  1594. /* Run periodically off the general workqueue */
  1595. static void efx_monitor(struct work_struct *data)
  1596. {
  1597. struct efx_nic *efx = container_of(data, struct efx_nic,
  1598. monitor_work.work);
  1599. netif_vdbg(efx, timer, efx->net_dev,
  1600. "hardware monitor executing on CPU %d\n",
  1601. raw_smp_processor_id());
  1602. BUG_ON(efx->type->monitor == NULL);
  1603. /* If the mac_lock is already held then it is likely a port
  1604. * reconfiguration is already in place, which will likely do
  1605. * most of the work of monitor() anyway. */
  1606. if (mutex_trylock(&efx->mac_lock)) {
  1607. if (efx->port_enabled)
  1608. efx->type->monitor(efx);
  1609. mutex_unlock(&efx->mac_lock);
  1610. }
  1611. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1612. efx_monitor_interval);
  1613. }
  1614. /**************************************************************************
  1615. *
  1616. * ioctls
  1617. *
  1618. *************************************************************************/
  1619. /* Net device ioctl
  1620. * Context: process, rtnl_lock() held.
  1621. */
  1622. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1623. {
  1624. struct efx_nic *efx = netdev_priv(net_dev);
  1625. struct mii_ioctl_data *data = if_mii(ifr);
  1626. if (cmd == SIOCSHWTSTAMP)
  1627. return efx_ptp_set_ts_config(efx, ifr);
  1628. if (cmd == SIOCGHWTSTAMP)
  1629. return efx_ptp_get_ts_config(efx, ifr);
  1630. /* Convert phy_id from older PRTAD/DEVAD format */
  1631. if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
  1632. (data->phy_id & 0xfc00) == 0x0400)
  1633. data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
  1634. return mdio_mii_ioctl(&efx->mdio, data, cmd);
  1635. }
  1636. /**************************************************************************
  1637. *
  1638. * NAPI interface
  1639. *
  1640. **************************************************************************/
  1641. static void efx_init_napi_channel(struct efx_channel *channel)
  1642. {
  1643. struct efx_nic *efx = channel->efx;
  1644. channel->napi_dev = efx->net_dev;
  1645. netif_napi_add(channel->napi_dev, &channel->napi_str,
  1646. efx_poll, napi_weight);
  1647. }
  1648. static void efx_init_napi(struct efx_nic *efx)
  1649. {
  1650. struct efx_channel *channel;
  1651. efx_for_each_channel(channel, efx)
  1652. efx_init_napi_channel(channel);
  1653. }
  1654. static void efx_fini_napi_channel(struct efx_channel *channel)
  1655. {
  1656. if (channel->napi_dev)
  1657. netif_napi_del(&channel->napi_str);
  1658. channel->napi_dev = NULL;
  1659. }
  1660. static void efx_fini_napi(struct efx_nic *efx)
  1661. {
  1662. struct efx_channel *channel;
  1663. efx_for_each_channel(channel, efx)
  1664. efx_fini_napi_channel(channel);
  1665. }
  1666. /**************************************************************************
  1667. *
  1668. * Kernel netpoll interface
  1669. *
  1670. *************************************************************************/
  1671. #ifdef CONFIG_NET_POLL_CONTROLLER
  1672. /* Although in the common case interrupts will be disabled, this is not
  1673. * guaranteed. However, all our work happens inside the NAPI callback,
  1674. * so no locking is required.
  1675. */
  1676. static void efx_netpoll(struct net_device *net_dev)
  1677. {
  1678. struct efx_nic *efx = netdev_priv(net_dev);
  1679. struct efx_channel *channel;
  1680. efx_for_each_channel(channel, efx)
  1681. efx_schedule_channel(channel);
  1682. }
  1683. #endif
  1684. /**************************************************************************
  1685. *
  1686. * Kernel net device interface
  1687. *
  1688. *************************************************************************/
  1689. /* Context: process, rtnl_lock() held. */
  1690. static int efx_net_open(struct net_device *net_dev)
  1691. {
  1692. struct efx_nic *efx = netdev_priv(net_dev);
  1693. int rc;
  1694. netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
  1695. raw_smp_processor_id());
  1696. rc = efx_check_disabled(efx);
  1697. if (rc)
  1698. return rc;
  1699. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1700. return -EBUSY;
  1701. if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
  1702. return -EIO;
  1703. /* Notify the kernel of the link state polled during driver load,
  1704. * before the monitor starts running */
  1705. efx_link_status_changed(efx);
  1706. efx_start_all(efx);
  1707. efx_selftest_async_start(efx);
  1708. return 0;
  1709. }
  1710. /* Context: process, rtnl_lock() held.
  1711. * Note that the kernel will ignore our return code; this method
  1712. * should really be a void.
  1713. */
  1714. static int efx_net_stop(struct net_device *net_dev)
  1715. {
  1716. struct efx_nic *efx = netdev_priv(net_dev);
  1717. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  1718. raw_smp_processor_id());
  1719. /* Stop the device and flush all the channels */
  1720. efx_stop_all(efx);
  1721. return 0;
  1722. }
  1723. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1724. static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
  1725. struct rtnl_link_stats64 *stats)
  1726. {
  1727. struct efx_nic *efx = netdev_priv(net_dev);
  1728. spin_lock_bh(&efx->stats_lock);
  1729. efx->type->update_stats(efx, NULL, stats);
  1730. spin_unlock_bh(&efx->stats_lock);
  1731. return stats;
  1732. }
  1733. /* Context: netif_tx_lock held, BHs disabled. */
  1734. static void efx_watchdog(struct net_device *net_dev)
  1735. {
  1736. struct efx_nic *efx = netdev_priv(net_dev);
  1737. netif_err(efx, tx_err, efx->net_dev,
  1738. "TX stuck with port_enabled=%d: resetting channels\n",
  1739. efx->port_enabled);
  1740. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1741. }
  1742. /* Context: process, rtnl_lock() held. */
  1743. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1744. {
  1745. struct efx_nic *efx = netdev_priv(net_dev);
  1746. int rc;
  1747. rc = efx_check_disabled(efx);
  1748. if (rc)
  1749. return rc;
  1750. if (new_mtu > EFX_MAX_MTU)
  1751. return -EINVAL;
  1752. netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
  1753. efx_device_detach_sync(efx);
  1754. efx_stop_all(efx);
  1755. mutex_lock(&efx->mac_lock);
  1756. net_dev->mtu = new_mtu;
  1757. efx->type->reconfigure_mac(efx);
  1758. mutex_unlock(&efx->mac_lock);
  1759. efx_start_all(efx);
  1760. netif_device_attach(efx->net_dev);
  1761. return 0;
  1762. }
  1763. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1764. {
  1765. struct efx_nic *efx = netdev_priv(net_dev);
  1766. struct sockaddr *addr = data;
  1767. char *new_addr = addr->sa_data;
  1768. if (!is_valid_ether_addr(new_addr)) {
  1769. netif_err(efx, drv, efx->net_dev,
  1770. "invalid ethernet MAC address requested: %pM\n",
  1771. new_addr);
  1772. return -EADDRNOTAVAIL;
  1773. }
  1774. memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
  1775. efx_sriov_mac_address_changed(efx);
  1776. /* Reconfigure the MAC */
  1777. mutex_lock(&efx->mac_lock);
  1778. efx->type->reconfigure_mac(efx);
  1779. mutex_unlock(&efx->mac_lock);
  1780. return 0;
  1781. }
  1782. /* Context: netif_addr_lock held, BHs disabled. */
  1783. static void efx_set_rx_mode(struct net_device *net_dev)
  1784. {
  1785. struct efx_nic *efx = netdev_priv(net_dev);
  1786. if (efx->port_enabled)
  1787. queue_work(efx->workqueue, &efx->mac_work);
  1788. /* Otherwise efx_start_port() will do this */
  1789. }
  1790. static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
  1791. {
  1792. struct efx_nic *efx = netdev_priv(net_dev);
  1793. /* If disabling RX n-tuple filtering, clear existing filters */
  1794. if (net_dev->features & ~data & NETIF_F_NTUPLE)
  1795. return efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
  1796. return 0;
  1797. }
  1798. static const struct net_device_ops efx_farch_netdev_ops = {
  1799. .ndo_open = efx_net_open,
  1800. .ndo_stop = efx_net_stop,
  1801. .ndo_get_stats64 = efx_net_stats,
  1802. .ndo_tx_timeout = efx_watchdog,
  1803. .ndo_start_xmit = efx_hard_start_xmit,
  1804. .ndo_validate_addr = eth_validate_addr,
  1805. .ndo_do_ioctl = efx_ioctl,
  1806. .ndo_change_mtu = efx_change_mtu,
  1807. .ndo_set_mac_address = efx_set_mac_address,
  1808. .ndo_set_rx_mode = efx_set_rx_mode,
  1809. .ndo_set_features = efx_set_features,
  1810. #ifdef CONFIG_SFC_SRIOV
  1811. .ndo_set_vf_mac = efx_sriov_set_vf_mac,
  1812. .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
  1813. .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
  1814. .ndo_get_vf_config = efx_sriov_get_vf_config,
  1815. #endif
  1816. #ifdef CONFIG_NET_POLL_CONTROLLER
  1817. .ndo_poll_controller = efx_netpoll,
  1818. #endif
  1819. .ndo_setup_tc = efx_setup_tc,
  1820. #ifdef CONFIG_RFS_ACCEL
  1821. .ndo_rx_flow_steer = efx_filter_rfs,
  1822. #endif
  1823. };
  1824. static const struct net_device_ops efx_ef10_netdev_ops = {
  1825. .ndo_open = efx_net_open,
  1826. .ndo_stop = efx_net_stop,
  1827. .ndo_get_stats64 = efx_net_stats,
  1828. .ndo_tx_timeout = efx_watchdog,
  1829. .ndo_start_xmit = efx_hard_start_xmit,
  1830. .ndo_validate_addr = eth_validate_addr,
  1831. .ndo_do_ioctl = efx_ioctl,
  1832. .ndo_change_mtu = efx_change_mtu,
  1833. .ndo_set_mac_address = efx_set_mac_address,
  1834. .ndo_set_rx_mode = efx_set_rx_mode,
  1835. .ndo_set_features = efx_set_features,
  1836. #ifdef CONFIG_NET_POLL_CONTROLLER
  1837. .ndo_poll_controller = efx_netpoll,
  1838. #endif
  1839. #ifdef CONFIG_RFS_ACCEL
  1840. .ndo_rx_flow_steer = efx_filter_rfs,
  1841. #endif
  1842. };
  1843. static void efx_update_name(struct efx_nic *efx)
  1844. {
  1845. strcpy(efx->name, efx->net_dev->name);
  1846. efx_mtd_rename(efx);
  1847. efx_set_channel_names(efx);
  1848. }
  1849. static int efx_netdev_event(struct notifier_block *this,
  1850. unsigned long event, void *ptr)
  1851. {
  1852. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  1853. if ((net_dev->netdev_ops == &efx_farch_netdev_ops ||
  1854. net_dev->netdev_ops == &efx_ef10_netdev_ops) &&
  1855. event == NETDEV_CHANGENAME)
  1856. efx_update_name(netdev_priv(net_dev));
  1857. return NOTIFY_DONE;
  1858. }
  1859. static struct notifier_block efx_netdev_notifier = {
  1860. .notifier_call = efx_netdev_event,
  1861. };
  1862. static ssize_t
  1863. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  1864. {
  1865. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1866. return sprintf(buf, "%d\n", efx->phy_type);
  1867. }
  1868. static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
  1869. static int efx_register_netdev(struct efx_nic *efx)
  1870. {
  1871. struct net_device *net_dev = efx->net_dev;
  1872. struct efx_channel *channel;
  1873. int rc;
  1874. net_dev->watchdog_timeo = 5 * HZ;
  1875. net_dev->irq = efx->pci_dev->irq;
  1876. if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
  1877. net_dev->netdev_ops = &efx_ef10_netdev_ops;
  1878. net_dev->priv_flags |= IFF_UNICAST_FLT;
  1879. } else {
  1880. net_dev->netdev_ops = &efx_farch_netdev_ops;
  1881. }
  1882. SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
  1883. net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
  1884. rtnl_lock();
  1885. /* Enable resets to be scheduled and check whether any were
  1886. * already requested. If so, the NIC is probably hosed so we
  1887. * abort.
  1888. */
  1889. efx->state = STATE_READY;
  1890. smp_mb(); /* ensure we change state before checking reset_pending */
  1891. if (efx->reset_pending) {
  1892. netif_err(efx, probe, efx->net_dev,
  1893. "aborting probe due to scheduled reset\n");
  1894. rc = -EIO;
  1895. goto fail_locked;
  1896. }
  1897. rc = dev_alloc_name(net_dev, net_dev->name);
  1898. if (rc < 0)
  1899. goto fail_locked;
  1900. efx_update_name(efx);
  1901. /* Always start with carrier off; PHY events will detect the link */
  1902. netif_carrier_off(net_dev);
  1903. rc = register_netdevice(net_dev);
  1904. if (rc)
  1905. goto fail_locked;
  1906. efx_for_each_channel(channel, efx) {
  1907. struct efx_tx_queue *tx_queue;
  1908. efx_for_each_channel_tx_queue(tx_queue, channel)
  1909. efx_init_tx_queue_core_txq(tx_queue);
  1910. }
  1911. efx_associate(efx);
  1912. rtnl_unlock();
  1913. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1914. if (rc) {
  1915. netif_err(efx, drv, efx->net_dev,
  1916. "failed to init net dev attributes\n");
  1917. goto fail_registered;
  1918. }
  1919. return 0;
  1920. fail_registered:
  1921. rtnl_lock();
  1922. efx_dissociate(efx);
  1923. unregister_netdevice(net_dev);
  1924. fail_locked:
  1925. efx->state = STATE_UNINIT;
  1926. rtnl_unlock();
  1927. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  1928. return rc;
  1929. }
  1930. static void efx_unregister_netdev(struct efx_nic *efx)
  1931. {
  1932. if (!efx->net_dev)
  1933. return;
  1934. BUG_ON(netdev_priv(efx->net_dev) != efx);
  1935. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  1936. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1937. rtnl_lock();
  1938. unregister_netdevice(efx->net_dev);
  1939. efx->state = STATE_UNINIT;
  1940. rtnl_unlock();
  1941. }
  1942. /**************************************************************************
  1943. *
  1944. * Device reset and suspend
  1945. *
  1946. **************************************************************************/
  1947. /* Tears down the entire software state and most of the hardware state
  1948. * before reset. */
  1949. void efx_reset_down(struct efx_nic *efx, enum reset_type method)
  1950. {
  1951. EFX_ASSERT_RESET_SERIALISED(efx);
  1952. efx_stop_all(efx);
  1953. efx_disable_interrupts(efx);
  1954. mutex_lock(&efx->mac_lock);
  1955. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
  1956. efx->phy_op->fini(efx);
  1957. efx->type->fini(efx);
  1958. }
  1959. /* This function will always ensure that the locks acquired in
  1960. * efx_reset_down() are released. A failure return code indicates
  1961. * that we were unable to reinitialise the hardware, and the
  1962. * driver should be disabled. If ok is false, then the rx and tx
  1963. * engines are not restarted, pending a RESET_DISABLE. */
  1964. int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
  1965. {
  1966. int rc;
  1967. EFX_ASSERT_RESET_SERIALISED(efx);
  1968. rc = efx->type->init(efx);
  1969. if (rc) {
  1970. netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
  1971. goto fail;
  1972. }
  1973. if (!ok)
  1974. goto fail;
  1975. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
  1976. rc = efx->phy_op->init(efx);
  1977. if (rc)
  1978. goto fail;
  1979. if (efx->phy_op->reconfigure(efx))
  1980. netif_err(efx, drv, efx->net_dev,
  1981. "could not restore PHY settings\n");
  1982. }
  1983. rc = efx_enable_interrupts(efx);
  1984. if (rc)
  1985. goto fail;
  1986. efx_restore_filters(efx);
  1987. efx_sriov_reset(efx);
  1988. mutex_unlock(&efx->mac_lock);
  1989. efx_start_all(efx);
  1990. return 0;
  1991. fail:
  1992. efx->port_initialized = false;
  1993. mutex_unlock(&efx->mac_lock);
  1994. return rc;
  1995. }
  1996. /* Reset the NIC using the specified method. Note that the reset may
  1997. * fail, in which case the card will be left in an unusable state.
  1998. *
  1999. * Caller must hold the rtnl_lock.
  2000. */
  2001. int efx_reset(struct efx_nic *efx, enum reset_type method)
  2002. {
  2003. int rc, rc2;
  2004. bool disabled;
  2005. netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
  2006. RESET_TYPE(method));
  2007. efx_device_detach_sync(efx);
  2008. efx_reset_down(efx, method);
  2009. rc = efx->type->reset(efx, method);
  2010. if (rc) {
  2011. netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
  2012. goto out;
  2013. }
  2014. /* Clear flags for the scopes we covered. We assume the NIC and
  2015. * driver are now quiescent so that there is no race here.
  2016. */
  2017. efx->reset_pending &= -(1 << (method + 1));
  2018. /* Reinitialise bus-mastering, which may have been turned off before
  2019. * the reset was scheduled. This is still appropriate, even in the
  2020. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  2021. * can respond to requests. */
  2022. pci_set_master(efx->pci_dev);
  2023. out:
  2024. /* Leave device stopped if necessary */
  2025. disabled = rc ||
  2026. method == RESET_TYPE_DISABLE ||
  2027. method == RESET_TYPE_RECOVER_OR_DISABLE;
  2028. rc2 = efx_reset_up(efx, method, !disabled);
  2029. if (rc2) {
  2030. disabled = true;
  2031. if (!rc)
  2032. rc = rc2;
  2033. }
  2034. if (disabled) {
  2035. dev_close(efx->net_dev);
  2036. netif_err(efx, drv, efx->net_dev, "has been disabled\n");
  2037. efx->state = STATE_DISABLED;
  2038. } else {
  2039. netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
  2040. netif_device_attach(efx->net_dev);
  2041. }
  2042. return rc;
  2043. }
  2044. /* Try recovery mechanisms.
  2045. * For now only EEH is supported.
  2046. * Returns 0 if the recovery mechanisms are unsuccessful.
  2047. * Returns a non-zero value otherwise.
  2048. */
  2049. int efx_try_recovery(struct efx_nic *efx)
  2050. {
  2051. #ifdef CONFIG_EEH
  2052. /* A PCI error can occur and not be seen by EEH because nothing
  2053. * happens on the PCI bus. In this case the driver may fail and
  2054. * schedule a 'recover or reset', leading to this recovery handler.
  2055. * Manually call the eeh failure check function.
  2056. */
  2057. struct eeh_dev *eehdev =
  2058. of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
  2059. if (eeh_dev_check_failure(eehdev)) {
  2060. /* The EEH mechanisms will handle the error and reset the
  2061. * device if necessary.
  2062. */
  2063. return 1;
  2064. }
  2065. #endif
  2066. return 0;
  2067. }
  2068. static void efx_wait_for_bist_end(struct efx_nic *efx)
  2069. {
  2070. int i;
  2071. for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
  2072. if (efx_mcdi_poll_reboot(efx))
  2073. goto out;
  2074. msleep(BIST_WAIT_DELAY_MS);
  2075. }
  2076. netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
  2077. out:
  2078. /* Either way unset the BIST flag. If we found no reboot we probably
  2079. * won't recover, but we should try.
  2080. */
  2081. efx->mc_bist_for_other_fn = false;
  2082. }
  2083. /* The worker thread exists so that code that cannot sleep can
  2084. * schedule a reset for later.
  2085. */
  2086. static void efx_reset_work(struct work_struct *data)
  2087. {
  2088. struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
  2089. unsigned long pending;
  2090. enum reset_type method;
  2091. pending = ACCESS_ONCE(efx->reset_pending);
  2092. method = fls(pending) - 1;
  2093. if (method == RESET_TYPE_MC_BIST)
  2094. efx_wait_for_bist_end(efx);
  2095. if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
  2096. method == RESET_TYPE_RECOVER_OR_ALL) &&
  2097. efx_try_recovery(efx))
  2098. return;
  2099. if (!pending)
  2100. return;
  2101. rtnl_lock();
  2102. /* We checked the state in efx_schedule_reset() but it may
  2103. * have changed by now. Now that we have the RTNL lock,
  2104. * it cannot change again.
  2105. */
  2106. if (efx->state == STATE_READY)
  2107. (void)efx_reset(efx, method);
  2108. rtnl_unlock();
  2109. }
  2110. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  2111. {
  2112. enum reset_type method;
  2113. if (efx->state == STATE_RECOVERY) {
  2114. netif_dbg(efx, drv, efx->net_dev,
  2115. "recovering: skip scheduling %s reset\n",
  2116. RESET_TYPE(type));
  2117. return;
  2118. }
  2119. switch (type) {
  2120. case RESET_TYPE_INVISIBLE:
  2121. case RESET_TYPE_ALL:
  2122. case RESET_TYPE_RECOVER_OR_ALL:
  2123. case RESET_TYPE_WORLD:
  2124. case RESET_TYPE_DISABLE:
  2125. case RESET_TYPE_RECOVER_OR_DISABLE:
  2126. case RESET_TYPE_MC_BIST:
  2127. method = type;
  2128. netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
  2129. RESET_TYPE(method));
  2130. break;
  2131. default:
  2132. method = efx->type->map_reset_reason(type);
  2133. netif_dbg(efx, drv, efx->net_dev,
  2134. "scheduling %s reset for %s\n",
  2135. RESET_TYPE(method), RESET_TYPE(type));
  2136. break;
  2137. }
  2138. set_bit(method, &efx->reset_pending);
  2139. smp_mb(); /* ensure we change reset_pending before checking state */
  2140. /* If we're not READY then just leave the flags set as the cue
  2141. * to abort probing or reschedule the reset later.
  2142. */
  2143. if (ACCESS_ONCE(efx->state) != STATE_READY)
  2144. return;
  2145. /* efx_process_channel() will no longer read events once a
  2146. * reset is scheduled. So switch back to poll'd MCDI completions. */
  2147. efx_mcdi_mode_poll(efx);
  2148. queue_work(reset_workqueue, &efx->reset_work);
  2149. }
  2150. /**************************************************************************
  2151. *
  2152. * List of NICs we support
  2153. *
  2154. **************************************************************************/
  2155. /* PCI device ID table */
  2156. static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
  2157. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  2158. PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
  2159. .driver_data = (unsigned long) &falcon_a1_nic_type},
  2160. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  2161. PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
  2162. .driver_data = (unsigned long) &falcon_b0_nic_type},
  2163. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
  2164. .driver_data = (unsigned long) &siena_a0_nic_type},
  2165. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
  2166. .driver_data = (unsigned long) &siena_a0_nic_type},
  2167. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
  2168. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  2169. {0} /* end of list */
  2170. };
  2171. /**************************************************************************
  2172. *
  2173. * Dummy PHY/MAC operations
  2174. *
  2175. * Can be used for some unimplemented operations
  2176. * Needed so all function pointers are valid and do not have to be tested
  2177. * before use
  2178. *
  2179. **************************************************************************/
  2180. int efx_port_dummy_op_int(struct efx_nic *efx)
  2181. {
  2182. return 0;
  2183. }
  2184. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  2185. static bool efx_port_dummy_op_poll(struct efx_nic *efx)
  2186. {
  2187. return false;
  2188. }
  2189. static const struct efx_phy_operations efx_dummy_phy_operations = {
  2190. .init = efx_port_dummy_op_int,
  2191. .reconfigure = efx_port_dummy_op_int,
  2192. .poll = efx_port_dummy_op_poll,
  2193. .fini = efx_port_dummy_op_void,
  2194. };
  2195. /**************************************************************************
  2196. *
  2197. * Data housekeeping
  2198. *
  2199. **************************************************************************/
  2200. /* This zeroes out and then fills in the invariants in a struct
  2201. * efx_nic (including all sub-structures).
  2202. */
  2203. static int efx_init_struct(struct efx_nic *efx,
  2204. struct pci_dev *pci_dev, struct net_device *net_dev)
  2205. {
  2206. int i;
  2207. /* Initialise common structures */
  2208. INIT_LIST_HEAD(&efx->node);
  2209. INIT_LIST_HEAD(&efx->secondary_list);
  2210. spin_lock_init(&efx->biu_lock);
  2211. #ifdef CONFIG_SFC_MTD
  2212. INIT_LIST_HEAD(&efx->mtd_list);
  2213. #endif
  2214. INIT_WORK(&efx->reset_work, efx_reset_work);
  2215. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  2216. INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
  2217. efx->pci_dev = pci_dev;
  2218. efx->msg_enable = debug;
  2219. efx->state = STATE_UNINIT;
  2220. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  2221. efx->net_dev = net_dev;
  2222. efx->rx_prefix_size = efx->type->rx_prefix_size;
  2223. efx->rx_ip_align =
  2224. NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
  2225. efx->rx_packet_hash_offset =
  2226. efx->type->rx_hash_offset - efx->type->rx_prefix_size;
  2227. efx->rx_packet_ts_offset =
  2228. efx->type->rx_ts_offset - efx->type->rx_prefix_size;
  2229. spin_lock_init(&efx->stats_lock);
  2230. mutex_init(&efx->mac_lock);
  2231. efx->phy_op = &efx_dummy_phy_operations;
  2232. efx->mdio.dev = net_dev;
  2233. INIT_WORK(&efx->mac_work, efx_mac_work);
  2234. init_waitqueue_head(&efx->flush_wq);
  2235. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  2236. efx->channel[i] = efx_alloc_channel(efx, i, NULL);
  2237. if (!efx->channel[i])
  2238. goto fail;
  2239. efx->msi_context[i].efx = efx;
  2240. efx->msi_context[i].index = i;
  2241. }
  2242. /* Higher numbered interrupt modes are less capable! */
  2243. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  2244. interrupt_mode);
  2245. /* Would be good to use the net_dev name, but we're too early */
  2246. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  2247. pci_name(pci_dev));
  2248. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  2249. if (!efx->workqueue)
  2250. goto fail;
  2251. return 0;
  2252. fail:
  2253. efx_fini_struct(efx);
  2254. return -ENOMEM;
  2255. }
  2256. static void efx_fini_struct(struct efx_nic *efx)
  2257. {
  2258. int i;
  2259. for (i = 0; i < EFX_MAX_CHANNELS; i++)
  2260. kfree(efx->channel[i]);
  2261. kfree(efx->vpd_sn);
  2262. if (efx->workqueue) {
  2263. destroy_workqueue(efx->workqueue);
  2264. efx->workqueue = NULL;
  2265. }
  2266. }
  2267. /**************************************************************************
  2268. *
  2269. * PCI interface
  2270. *
  2271. **************************************************************************/
  2272. /* Main body of final NIC shutdown code
  2273. * This is called only at module unload (or hotplug removal).
  2274. */
  2275. static void efx_pci_remove_main(struct efx_nic *efx)
  2276. {
  2277. /* Flush reset_work. It can no longer be scheduled since we
  2278. * are not READY.
  2279. */
  2280. BUG_ON(efx->state == STATE_READY);
  2281. cancel_work_sync(&efx->reset_work);
  2282. efx_disable_interrupts(efx);
  2283. efx_nic_fini_interrupt(efx);
  2284. efx_fini_port(efx);
  2285. efx->type->fini(efx);
  2286. efx_fini_napi(efx);
  2287. efx_remove_all(efx);
  2288. }
  2289. /* Final NIC shutdown
  2290. * This is called only at module unload (or hotplug removal).
  2291. */
  2292. static void efx_pci_remove(struct pci_dev *pci_dev)
  2293. {
  2294. struct efx_nic *efx;
  2295. efx = pci_get_drvdata(pci_dev);
  2296. if (!efx)
  2297. return;
  2298. /* Mark the NIC as fini, then stop the interface */
  2299. rtnl_lock();
  2300. efx_dissociate(efx);
  2301. dev_close(efx->net_dev);
  2302. efx_disable_interrupts(efx);
  2303. rtnl_unlock();
  2304. efx_sriov_fini(efx);
  2305. efx_unregister_netdev(efx);
  2306. efx_mtd_remove(efx);
  2307. efx_pci_remove_main(efx);
  2308. efx_fini_io(efx);
  2309. netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
  2310. efx_fini_struct(efx);
  2311. free_netdev(efx->net_dev);
  2312. pci_disable_pcie_error_reporting(pci_dev);
  2313. };
  2314. /* NIC VPD information
  2315. * Called during probe to display the part number of the
  2316. * installed NIC. VPD is potentially very large but this should
  2317. * always appear within the first 512 bytes.
  2318. */
  2319. #define SFC_VPD_LEN 512
  2320. static void efx_probe_vpd_strings(struct efx_nic *efx)
  2321. {
  2322. struct pci_dev *dev = efx->pci_dev;
  2323. char vpd_data[SFC_VPD_LEN];
  2324. ssize_t vpd_size;
  2325. int ro_start, ro_size, i, j;
  2326. /* Get the vpd data from the device */
  2327. vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
  2328. if (vpd_size <= 0) {
  2329. netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
  2330. return;
  2331. }
  2332. /* Get the Read only section */
  2333. ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
  2334. if (ro_start < 0) {
  2335. netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
  2336. return;
  2337. }
  2338. ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
  2339. j = ro_size;
  2340. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  2341. if (i + j > vpd_size)
  2342. j = vpd_size - i;
  2343. /* Get the Part number */
  2344. i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
  2345. if (i < 0) {
  2346. netif_err(efx, drv, efx->net_dev, "Part number not found\n");
  2347. return;
  2348. }
  2349. j = pci_vpd_info_field_size(&vpd_data[i]);
  2350. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2351. if (i + j > vpd_size) {
  2352. netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
  2353. return;
  2354. }
  2355. netif_info(efx, drv, efx->net_dev,
  2356. "Part Number : %.*s\n", j, &vpd_data[i]);
  2357. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  2358. j = ro_size;
  2359. i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
  2360. if (i < 0) {
  2361. netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
  2362. return;
  2363. }
  2364. j = pci_vpd_info_field_size(&vpd_data[i]);
  2365. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2366. if (i + j > vpd_size) {
  2367. netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
  2368. return;
  2369. }
  2370. efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
  2371. if (!efx->vpd_sn)
  2372. return;
  2373. snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
  2374. }
  2375. /* Main body of NIC initialisation
  2376. * This is called at module load (or hotplug insertion, theoretically).
  2377. */
  2378. static int efx_pci_probe_main(struct efx_nic *efx)
  2379. {
  2380. int rc;
  2381. /* Do start-of-day initialisation */
  2382. rc = efx_probe_all(efx);
  2383. if (rc)
  2384. goto fail1;
  2385. efx_init_napi(efx);
  2386. rc = efx->type->init(efx);
  2387. if (rc) {
  2388. netif_err(efx, probe, efx->net_dev,
  2389. "failed to initialise NIC\n");
  2390. goto fail3;
  2391. }
  2392. rc = efx_init_port(efx);
  2393. if (rc) {
  2394. netif_err(efx, probe, efx->net_dev,
  2395. "failed to initialise port\n");
  2396. goto fail4;
  2397. }
  2398. rc = efx_nic_init_interrupt(efx);
  2399. if (rc)
  2400. goto fail5;
  2401. rc = efx_enable_interrupts(efx);
  2402. if (rc)
  2403. goto fail6;
  2404. return 0;
  2405. fail6:
  2406. efx_nic_fini_interrupt(efx);
  2407. fail5:
  2408. efx_fini_port(efx);
  2409. fail4:
  2410. efx->type->fini(efx);
  2411. fail3:
  2412. efx_fini_napi(efx);
  2413. efx_remove_all(efx);
  2414. fail1:
  2415. return rc;
  2416. }
  2417. /* NIC initialisation
  2418. *
  2419. * This is called at module load (or hotplug insertion,
  2420. * theoretically). It sets up PCI mappings, resets the NIC,
  2421. * sets up and registers the network devices with the kernel and hooks
  2422. * the interrupt service routine. It does not prepare the device for
  2423. * transmission; this is left to the first time one of the network
  2424. * interfaces is brought up (i.e. efx_net_open).
  2425. */
  2426. static int efx_pci_probe(struct pci_dev *pci_dev,
  2427. const struct pci_device_id *entry)
  2428. {
  2429. struct net_device *net_dev;
  2430. struct efx_nic *efx;
  2431. int rc;
  2432. /* Allocate and initialise a struct net_device and struct efx_nic */
  2433. net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
  2434. EFX_MAX_RX_QUEUES);
  2435. if (!net_dev)
  2436. return -ENOMEM;
  2437. efx = netdev_priv(net_dev);
  2438. efx->type = (const struct efx_nic_type *) entry->driver_data;
  2439. net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
  2440. NETIF_F_HIGHDMA | NETIF_F_TSO |
  2441. NETIF_F_RXCSUM);
  2442. if (efx->type->offload_features & NETIF_F_V6_CSUM)
  2443. net_dev->features |= NETIF_F_TSO6;
  2444. /* Mask for features that also apply to VLAN devices */
  2445. net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
  2446. NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
  2447. NETIF_F_RXCSUM);
  2448. /* All offloads can be toggled */
  2449. net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
  2450. pci_set_drvdata(pci_dev, efx);
  2451. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  2452. rc = efx_init_struct(efx, pci_dev, net_dev);
  2453. if (rc)
  2454. goto fail1;
  2455. netif_info(efx, probe, efx->net_dev,
  2456. "Solarflare NIC detected\n");
  2457. efx_probe_vpd_strings(efx);
  2458. /* Set up basic I/O (BAR mappings etc) */
  2459. rc = efx_init_io(efx);
  2460. if (rc)
  2461. goto fail2;
  2462. rc = efx_pci_probe_main(efx);
  2463. if (rc)
  2464. goto fail3;
  2465. rc = efx_register_netdev(efx);
  2466. if (rc)
  2467. goto fail4;
  2468. rc = efx_sriov_init(efx);
  2469. if (rc)
  2470. netif_err(efx, probe, efx->net_dev,
  2471. "SR-IOV can't be enabled rc %d\n", rc);
  2472. netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
  2473. /* Try to create MTDs, but allow this to fail */
  2474. rtnl_lock();
  2475. rc = efx_mtd_probe(efx);
  2476. rtnl_unlock();
  2477. if (rc)
  2478. netif_warn(efx, probe, efx->net_dev,
  2479. "failed to create MTDs (%d)\n", rc);
  2480. rc = pci_enable_pcie_error_reporting(pci_dev);
  2481. if (rc && rc != -EINVAL)
  2482. netif_warn(efx, probe, efx->net_dev,
  2483. "pci_enable_pcie_error_reporting failed (%d)\n", rc);
  2484. return 0;
  2485. fail4:
  2486. efx_pci_remove_main(efx);
  2487. fail3:
  2488. efx_fini_io(efx);
  2489. fail2:
  2490. efx_fini_struct(efx);
  2491. fail1:
  2492. WARN_ON(rc > 0);
  2493. netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
  2494. free_netdev(net_dev);
  2495. return rc;
  2496. }
  2497. static int efx_pm_freeze(struct device *dev)
  2498. {
  2499. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2500. rtnl_lock();
  2501. if (efx->state != STATE_DISABLED) {
  2502. efx->state = STATE_UNINIT;
  2503. efx_device_detach_sync(efx);
  2504. efx_stop_all(efx);
  2505. efx_disable_interrupts(efx);
  2506. }
  2507. rtnl_unlock();
  2508. return 0;
  2509. }
  2510. static int efx_pm_thaw(struct device *dev)
  2511. {
  2512. int rc;
  2513. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2514. rtnl_lock();
  2515. if (efx->state != STATE_DISABLED) {
  2516. rc = efx_enable_interrupts(efx);
  2517. if (rc)
  2518. goto fail;
  2519. mutex_lock(&efx->mac_lock);
  2520. efx->phy_op->reconfigure(efx);
  2521. mutex_unlock(&efx->mac_lock);
  2522. efx_start_all(efx);
  2523. netif_device_attach(efx->net_dev);
  2524. efx->state = STATE_READY;
  2525. efx->type->resume_wol(efx);
  2526. }
  2527. rtnl_unlock();
  2528. /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
  2529. queue_work(reset_workqueue, &efx->reset_work);
  2530. return 0;
  2531. fail:
  2532. rtnl_unlock();
  2533. return rc;
  2534. }
  2535. static int efx_pm_poweroff(struct device *dev)
  2536. {
  2537. struct pci_dev *pci_dev = to_pci_dev(dev);
  2538. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2539. efx->type->fini(efx);
  2540. efx->reset_pending = 0;
  2541. pci_save_state(pci_dev);
  2542. return pci_set_power_state(pci_dev, PCI_D3hot);
  2543. }
  2544. /* Used for both resume and restore */
  2545. static int efx_pm_resume(struct device *dev)
  2546. {
  2547. struct pci_dev *pci_dev = to_pci_dev(dev);
  2548. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2549. int rc;
  2550. rc = pci_set_power_state(pci_dev, PCI_D0);
  2551. if (rc)
  2552. return rc;
  2553. pci_restore_state(pci_dev);
  2554. rc = pci_enable_device(pci_dev);
  2555. if (rc)
  2556. return rc;
  2557. pci_set_master(efx->pci_dev);
  2558. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  2559. if (rc)
  2560. return rc;
  2561. rc = efx->type->init(efx);
  2562. if (rc)
  2563. return rc;
  2564. rc = efx_pm_thaw(dev);
  2565. return rc;
  2566. }
  2567. static int efx_pm_suspend(struct device *dev)
  2568. {
  2569. int rc;
  2570. efx_pm_freeze(dev);
  2571. rc = efx_pm_poweroff(dev);
  2572. if (rc)
  2573. efx_pm_resume(dev);
  2574. return rc;
  2575. }
  2576. static const struct dev_pm_ops efx_pm_ops = {
  2577. .suspend = efx_pm_suspend,
  2578. .resume = efx_pm_resume,
  2579. .freeze = efx_pm_freeze,
  2580. .thaw = efx_pm_thaw,
  2581. .poweroff = efx_pm_poweroff,
  2582. .restore = efx_pm_resume,
  2583. };
  2584. /* A PCI error affecting this device was detected.
  2585. * At this point MMIO and DMA may be disabled.
  2586. * Stop the software path and request a slot reset.
  2587. */
  2588. static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
  2589. enum pci_channel_state state)
  2590. {
  2591. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2592. struct efx_nic *efx = pci_get_drvdata(pdev);
  2593. if (state == pci_channel_io_perm_failure)
  2594. return PCI_ERS_RESULT_DISCONNECT;
  2595. rtnl_lock();
  2596. if (efx->state != STATE_DISABLED) {
  2597. efx->state = STATE_RECOVERY;
  2598. efx->reset_pending = 0;
  2599. efx_device_detach_sync(efx);
  2600. efx_stop_all(efx);
  2601. efx_disable_interrupts(efx);
  2602. status = PCI_ERS_RESULT_NEED_RESET;
  2603. } else {
  2604. /* If the interface is disabled we don't want to do anything
  2605. * with it.
  2606. */
  2607. status = PCI_ERS_RESULT_RECOVERED;
  2608. }
  2609. rtnl_unlock();
  2610. pci_disable_device(pdev);
  2611. return status;
  2612. }
  2613. /* Fake a successfull reset, which will be performed later in efx_io_resume. */
  2614. static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
  2615. {
  2616. struct efx_nic *efx = pci_get_drvdata(pdev);
  2617. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2618. int rc;
  2619. if (pci_enable_device(pdev)) {
  2620. netif_err(efx, hw, efx->net_dev,
  2621. "Cannot re-enable PCI device after reset.\n");
  2622. status = PCI_ERS_RESULT_DISCONNECT;
  2623. }
  2624. rc = pci_cleanup_aer_uncorrect_error_status(pdev);
  2625. if (rc) {
  2626. netif_err(efx, hw, efx->net_dev,
  2627. "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
  2628. /* Non-fatal error. Continue. */
  2629. }
  2630. return status;
  2631. }
  2632. /* Perform the actual reset and resume I/O operations. */
  2633. static void efx_io_resume(struct pci_dev *pdev)
  2634. {
  2635. struct efx_nic *efx = pci_get_drvdata(pdev);
  2636. int rc;
  2637. rtnl_lock();
  2638. if (efx->state == STATE_DISABLED)
  2639. goto out;
  2640. rc = efx_reset(efx, RESET_TYPE_ALL);
  2641. if (rc) {
  2642. netif_err(efx, hw, efx->net_dev,
  2643. "efx_reset failed after PCI error (%d)\n", rc);
  2644. } else {
  2645. efx->state = STATE_READY;
  2646. netif_dbg(efx, hw, efx->net_dev,
  2647. "Done resetting and resuming IO after PCI error.\n");
  2648. }
  2649. out:
  2650. rtnl_unlock();
  2651. }
  2652. /* For simplicity and reliability, we always require a slot reset and try to
  2653. * reset the hardware when a pci error affecting the device is detected.
  2654. * We leave both the link_reset and mmio_enabled callback unimplemented:
  2655. * with our request for slot reset the mmio_enabled callback will never be
  2656. * called, and the link_reset callback is not used by AER or EEH mechanisms.
  2657. */
  2658. static struct pci_error_handlers efx_err_handlers = {
  2659. .error_detected = efx_io_error_detected,
  2660. .slot_reset = efx_io_slot_reset,
  2661. .resume = efx_io_resume,
  2662. };
  2663. static struct pci_driver efx_pci_driver = {
  2664. .name = KBUILD_MODNAME,
  2665. .id_table = efx_pci_table,
  2666. .probe = efx_pci_probe,
  2667. .remove = efx_pci_remove,
  2668. .driver.pm = &efx_pm_ops,
  2669. .err_handler = &efx_err_handlers,
  2670. };
  2671. /**************************************************************************
  2672. *
  2673. * Kernel module interface
  2674. *
  2675. *************************************************************************/
  2676. module_param(interrupt_mode, uint, 0444);
  2677. MODULE_PARM_DESC(interrupt_mode,
  2678. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  2679. static int __init efx_init_module(void)
  2680. {
  2681. int rc;
  2682. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  2683. rc = register_netdevice_notifier(&efx_netdev_notifier);
  2684. if (rc)
  2685. goto err_notifier;
  2686. rc = efx_init_sriov();
  2687. if (rc)
  2688. goto err_sriov;
  2689. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  2690. if (!reset_workqueue) {
  2691. rc = -ENOMEM;
  2692. goto err_reset;
  2693. }
  2694. rc = pci_register_driver(&efx_pci_driver);
  2695. if (rc < 0)
  2696. goto err_pci;
  2697. return 0;
  2698. err_pci:
  2699. destroy_workqueue(reset_workqueue);
  2700. err_reset:
  2701. efx_fini_sriov();
  2702. err_sriov:
  2703. unregister_netdevice_notifier(&efx_netdev_notifier);
  2704. err_notifier:
  2705. return rc;
  2706. }
  2707. static void __exit efx_exit_module(void)
  2708. {
  2709. printk(KERN_INFO "Solarflare NET driver unloading\n");
  2710. pci_unregister_driver(&efx_pci_driver);
  2711. destroy_workqueue(reset_workqueue);
  2712. efx_fini_sriov();
  2713. unregister_netdevice_notifier(&efx_netdev_notifier);
  2714. }
  2715. module_init(efx_init_module);
  2716. module_exit(efx_exit_module);
  2717. MODULE_AUTHOR("Solarflare Communications and "
  2718. "Michael Brown <mbrown@fensystems.co.uk>");
  2719. MODULE_DESCRIPTION("Solarflare Communications network driver");
  2720. MODULE_LICENSE("GPL");
  2721. MODULE_DEVICE_TABLE(pci, efx_pci_table);