efx.c 91 KB

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