efx.c 92 KB

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