efx.c 101 KB

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