efx.c 83 KB

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