efx.c 92 KB

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