efx.c 89 KB

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