efx.c 102 KB

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