efx.c 89 KB

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