efx.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177
  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_DISABLE] = "DISABLE",
  75. [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
  76. [RESET_TYPE_INT_ERROR] = "INT_ERROR",
  77. [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
  78. [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
  79. [RESET_TYPE_TX_SKIP] = "TX_SKIP",
  80. [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
  81. [RESET_TYPE_MC_BIST] = "MC_BIST",
  82. };
  83. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  84. * queued onto this work queue. This is not a per-nic work queue, because
  85. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  86. */
  87. static struct workqueue_struct *reset_workqueue;
  88. /* How often and how many times to poll for a reset while waiting for a
  89. * BIST that another function started to complete.
  90. */
  91. #define BIST_WAIT_DELAY_MS 100
  92. #define BIST_WAIT_DELAY_COUNT 100
  93. /**************************************************************************
  94. *
  95. * Configurable values
  96. *
  97. *************************************************************************/
  98. /*
  99. * Use separate channels for TX and RX events
  100. *
  101. * Set this to 1 to use separate channels for TX and RX. It allows us
  102. * to control interrupt affinity separately for TX and RX.
  103. *
  104. * This is only used in MSI-X interrupt mode
  105. */
  106. static bool separate_tx_channels;
  107. module_param(separate_tx_channels, bool, 0444);
  108. MODULE_PARM_DESC(separate_tx_channels,
  109. "Use separate channels for TX and RX");
  110. /* This is the weight assigned to each of the (per-channel) virtual
  111. * NAPI devices.
  112. */
  113. static int napi_weight = 64;
  114. /* This is the time (in jiffies) between invocations of the hardware
  115. * monitor.
  116. * On Falcon-based NICs, this will:
  117. * - Check the on-board hardware monitor;
  118. * - Poll the link state and reconfigure the hardware as necessary.
  119. * On Siena-based NICs for power systems with EEH support, this will give EEH a
  120. * chance to start.
  121. */
  122. static unsigned int efx_monitor_interval = 1 * HZ;
  123. /* Initial interrupt moderation settings. They can be modified after
  124. * module load with ethtool.
  125. *
  126. * The default for RX should strike a balance between increasing the
  127. * round-trip latency and reducing overhead.
  128. */
  129. static unsigned int rx_irq_mod_usec = 60;
  130. /* Initial interrupt moderation settings. They can be modified after
  131. * module load with ethtool.
  132. *
  133. * This default is chosen to ensure that a 10G link does not go idle
  134. * while a TX queue is stopped after it has become full. A queue is
  135. * restarted when it drops below half full. The time this takes (assuming
  136. * worst case 3 descriptors per packet and 1024 descriptors) is
  137. * 512 / 3 * 1.2 = 205 usec.
  138. */
  139. static unsigned int tx_irq_mod_usec = 150;
  140. /* This is the first interrupt mode to try out of:
  141. * 0 => MSI-X
  142. * 1 => MSI
  143. * 2 => legacy
  144. */
  145. static unsigned int interrupt_mode;
  146. /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
  147. * i.e. the number of CPUs among which we may distribute simultaneous
  148. * interrupt handling.
  149. *
  150. * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
  151. * The default (0) means to assign an interrupt to each core.
  152. */
  153. static unsigned int rss_cpus;
  154. module_param(rss_cpus, uint, 0444);
  155. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  156. static bool phy_flash_cfg;
  157. module_param(phy_flash_cfg, bool, 0644);
  158. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  159. static unsigned irq_adapt_low_thresh = 8000;
  160. module_param(irq_adapt_low_thresh, uint, 0644);
  161. MODULE_PARM_DESC(irq_adapt_low_thresh,
  162. "Threshold score for reducing IRQ moderation");
  163. static unsigned irq_adapt_high_thresh = 16000;
  164. module_param(irq_adapt_high_thresh, uint, 0644);
  165. MODULE_PARM_DESC(irq_adapt_high_thresh,
  166. "Threshold score for increasing IRQ moderation");
  167. static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  168. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  169. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  170. NETIF_MSG_TX_ERR | NETIF_MSG_HW);
  171. module_param(debug, uint, 0);
  172. MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
  173. /**************************************************************************
  174. *
  175. * Utility functions and prototypes
  176. *
  177. *************************************************************************/
  178. static int efx_soft_enable_interrupts(struct efx_nic *efx);
  179. static void efx_soft_disable_interrupts(struct efx_nic *efx);
  180. static void efx_remove_channel(struct efx_channel *channel);
  181. static void efx_remove_channels(struct efx_nic *efx);
  182. static const struct efx_channel_type efx_default_channel_type;
  183. static void efx_remove_port(struct efx_nic *efx);
  184. static void efx_init_napi_channel(struct efx_channel *channel);
  185. static void efx_fini_napi(struct efx_nic *efx);
  186. static void efx_fini_napi_channel(struct efx_channel *channel);
  187. static void efx_fini_struct(struct efx_nic *efx);
  188. static void efx_start_all(struct efx_nic *efx);
  189. static void efx_stop_all(struct efx_nic *efx);
  190. #define EFX_ASSERT_RESET_SERIALISED(efx) \
  191. do { \
  192. if ((efx->state == STATE_READY) || \
  193. (efx->state == STATE_RECOVERY) || \
  194. (efx->state == STATE_DISABLED)) \
  195. ASSERT_RTNL(); \
  196. } while (0)
  197. static int efx_check_disabled(struct efx_nic *efx)
  198. {
  199. if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
  200. netif_err(efx, drv, efx->net_dev,
  201. "device is disabled due to earlier errors\n");
  202. return -EIO;
  203. }
  204. return 0;
  205. }
  206. /**************************************************************************
  207. *
  208. * Event queue processing
  209. *
  210. *************************************************************************/
  211. /* Process channel's event queue
  212. *
  213. * This function is responsible for processing the event queue of a
  214. * single channel. The caller must guarantee that this function will
  215. * never be concurrently called more than once on the same channel,
  216. * though different channels may be being processed concurrently.
  217. */
  218. static int efx_process_channel(struct efx_channel *channel, int budget)
  219. {
  220. int spent;
  221. if (unlikely(!channel->enabled))
  222. return 0;
  223. spent = efx_nic_process_eventq(channel, budget);
  224. if (spent && efx_channel_has_rx_queue(channel)) {
  225. struct efx_rx_queue *rx_queue =
  226. efx_channel_get_rx_queue(channel);
  227. efx_rx_flush_packet(channel);
  228. efx_fast_push_rx_descriptors(rx_queue, true);
  229. }
  230. return spent;
  231. }
  232. /* NAPI poll handler
  233. *
  234. * NAPI guarantees serialisation of polls of the same device, which
  235. * provides the guarantee required by efx_process_channel().
  236. */
  237. static int efx_poll(struct napi_struct *napi, int budget)
  238. {
  239. struct efx_channel *channel =
  240. container_of(napi, struct efx_channel, napi_str);
  241. struct efx_nic *efx = channel->efx;
  242. int spent;
  243. netif_vdbg(efx, intr, efx->net_dev,
  244. "channel %d NAPI poll executing on CPU %d\n",
  245. channel->channel, raw_smp_processor_id());
  246. spent = efx_process_channel(channel, budget);
  247. if (spent < budget) {
  248. if (efx_channel_has_rx_queue(channel) &&
  249. efx->irq_rx_adaptive &&
  250. unlikely(++channel->irq_count == 1000)) {
  251. if (unlikely(channel->irq_mod_score <
  252. irq_adapt_low_thresh)) {
  253. if (channel->irq_moderation > 1) {
  254. channel->irq_moderation -= 1;
  255. efx->type->push_irq_moderation(channel);
  256. }
  257. } else if (unlikely(channel->irq_mod_score >
  258. irq_adapt_high_thresh)) {
  259. if (channel->irq_moderation <
  260. efx->irq_rx_moderation) {
  261. channel->irq_moderation += 1;
  262. efx->type->push_irq_moderation(channel);
  263. }
  264. }
  265. channel->irq_count = 0;
  266. channel->irq_mod_score = 0;
  267. }
  268. efx_filter_rfs_expire(channel);
  269. /* There is no race here; although napi_disable() will
  270. * only wait for napi_complete(), this isn't a problem
  271. * since efx_nic_eventq_read_ack() will have no effect if
  272. * interrupts have already been disabled.
  273. */
  274. napi_complete(napi);
  275. efx_nic_eventq_read_ack(channel);
  276. }
  277. return spent;
  278. }
  279. /* Create event queue
  280. * Event queue memory allocations are done only once. If the channel
  281. * is reset, the memory buffer will be reused; this guards against
  282. * errors during channel reset and also simplifies interrupt handling.
  283. */
  284. static int efx_probe_eventq(struct efx_channel *channel)
  285. {
  286. struct efx_nic *efx = channel->efx;
  287. unsigned long entries;
  288. netif_dbg(efx, probe, efx->net_dev,
  289. "chan %d create event queue\n", channel->channel);
  290. /* Build an event queue with room for one event per tx and rx buffer,
  291. * plus some extra for link state events and MCDI completions. */
  292. entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
  293. EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
  294. channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
  295. return efx_nic_probe_eventq(channel);
  296. }
  297. /* Prepare channel's event queue */
  298. static int efx_init_eventq(struct efx_channel *channel)
  299. {
  300. struct efx_nic *efx = channel->efx;
  301. int rc;
  302. EFX_WARN_ON_PARANOID(channel->eventq_init);
  303. netif_dbg(efx, drv, efx->net_dev,
  304. "chan %d init event queue\n", channel->channel);
  305. rc = efx_nic_init_eventq(channel);
  306. if (rc == 0) {
  307. efx->type->push_irq_moderation(channel);
  308. channel->eventq_read_ptr = 0;
  309. channel->eventq_init = true;
  310. }
  311. return rc;
  312. }
  313. /* Enable event queue processing and NAPI */
  314. static void efx_start_eventq(struct efx_channel *channel)
  315. {
  316. netif_dbg(channel->efx, ifup, channel->efx->net_dev,
  317. "chan %d start event queue\n", channel->channel);
  318. /* Make sure the NAPI handler sees the enabled flag set */
  319. channel->enabled = true;
  320. smp_wmb();
  321. napi_enable(&channel->napi_str);
  322. efx_nic_eventq_read_ack(channel);
  323. }
  324. /* Disable event queue processing and NAPI */
  325. static void efx_stop_eventq(struct efx_channel *channel)
  326. {
  327. if (!channel->enabled)
  328. return;
  329. napi_disable(&channel->napi_str);
  330. channel->enabled = false;
  331. }
  332. static void efx_fini_eventq(struct efx_channel *channel)
  333. {
  334. if (!channel->eventq_init)
  335. return;
  336. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  337. "chan %d fini event queue\n", channel->channel);
  338. efx_nic_fini_eventq(channel);
  339. channel->eventq_init = false;
  340. }
  341. static void efx_remove_eventq(struct efx_channel *channel)
  342. {
  343. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  344. "chan %d remove event queue\n", channel->channel);
  345. efx_nic_remove_eventq(channel);
  346. }
  347. /**************************************************************************
  348. *
  349. * Channel handling
  350. *
  351. *************************************************************************/
  352. /* Allocate and initialise a channel structure. */
  353. static struct efx_channel *
  354. efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
  355. {
  356. struct efx_channel *channel;
  357. struct efx_rx_queue *rx_queue;
  358. struct efx_tx_queue *tx_queue;
  359. int j;
  360. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  361. if (!channel)
  362. return NULL;
  363. channel->efx = efx;
  364. channel->channel = i;
  365. channel->type = &efx_default_channel_type;
  366. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  367. tx_queue = &channel->tx_queue[j];
  368. tx_queue->efx = efx;
  369. tx_queue->queue = i * EFX_TXQ_TYPES + j;
  370. tx_queue->channel = channel;
  371. }
  372. rx_queue = &channel->rx_queue;
  373. rx_queue->efx = efx;
  374. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  375. (unsigned long)rx_queue);
  376. return channel;
  377. }
  378. /* Allocate and initialise a channel structure, copying parameters
  379. * (but not resources) from an old channel structure.
  380. */
  381. static struct efx_channel *
  382. efx_copy_channel(const struct efx_channel *old_channel)
  383. {
  384. struct efx_channel *channel;
  385. struct efx_rx_queue *rx_queue;
  386. struct efx_tx_queue *tx_queue;
  387. int j;
  388. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  389. if (!channel)
  390. return NULL;
  391. *channel = *old_channel;
  392. channel->napi_dev = NULL;
  393. memset(&channel->eventq, 0, sizeof(channel->eventq));
  394. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  395. tx_queue = &channel->tx_queue[j];
  396. if (tx_queue->channel)
  397. tx_queue->channel = channel;
  398. tx_queue->buffer = NULL;
  399. memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
  400. }
  401. rx_queue = &channel->rx_queue;
  402. rx_queue->buffer = NULL;
  403. memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
  404. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  405. (unsigned long)rx_queue);
  406. return channel;
  407. }
  408. static int efx_probe_channel(struct efx_channel *channel)
  409. {
  410. struct efx_tx_queue *tx_queue;
  411. struct efx_rx_queue *rx_queue;
  412. int rc;
  413. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  414. "creating channel %d\n", channel->channel);
  415. rc = channel->type->pre_probe(channel);
  416. if (rc)
  417. goto fail;
  418. rc = efx_probe_eventq(channel);
  419. if (rc)
  420. goto fail;
  421. efx_for_each_channel_tx_queue(tx_queue, channel) {
  422. rc = efx_probe_tx_queue(tx_queue);
  423. if (rc)
  424. goto fail;
  425. }
  426. efx_for_each_channel_rx_queue(rx_queue, channel) {
  427. rc = efx_probe_rx_queue(rx_queue);
  428. if (rc)
  429. goto fail;
  430. }
  431. channel->n_rx_frm_trunc = 0;
  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. memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
  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. /* This configures the PCI device to enable I/O and DMA. */
  944. static int efx_init_io(struct efx_nic *efx)
  945. {
  946. struct pci_dev *pci_dev = efx->pci_dev;
  947. dma_addr_t dma_mask = efx->type->max_dma_mask;
  948. unsigned int mem_map_size = efx->type->mem_map_size(efx);
  949. int rc;
  950. netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
  951. rc = pci_enable_device(pci_dev);
  952. if (rc) {
  953. netif_err(efx, probe, efx->net_dev,
  954. "failed to enable PCI device\n");
  955. goto fail1;
  956. }
  957. pci_set_master(pci_dev);
  958. /* Set the PCI DMA mask. Try all possibilities from our
  959. * genuine mask down to 32 bits, because some architectures
  960. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  961. * masks event though they reject 46 bit masks.
  962. */
  963. while (dma_mask > 0x7fffffffUL) {
  964. if (dma_supported(&pci_dev->dev, dma_mask)) {
  965. rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
  966. if (rc == 0)
  967. break;
  968. }
  969. dma_mask >>= 1;
  970. }
  971. if (rc) {
  972. netif_err(efx, probe, efx->net_dev,
  973. "could not find a suitable DMA mask\n");
  974. goto fail2;
  975. }
  976. netif_dbg(efx, probe, efx->net_dev,
  977. "using DMA mask %llx\n", (unsigned long long) dma_mask);
  978. efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
  979. rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
  980. if (rc) {
  981. netif_err(efx, probe, efx->net_dev,
  982. "request for memory BAR failed\n");
  983. rc = -EIO;
  984. goto fail3;
  985. }
  986. efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
  987. if (!efx->membase) {
  988. netif_err(efx, probe, efx->net_dev,
  989. "could not map memory BAR at %llx+%x\n",
  990. (unsigned long long)efx->membase_phys, mem_map_size);
  991. rc = -ENOMEM;
  992. goto fail4;
  993. }
  994. netif_dbg(efx, probe, efx->net_dev,
  995. "memory BAR at %llx+%x (virtual %p)\n",
  996. (unsigned long long)efx->membase_phys, mem_map_size,
  997. efx->membase);
  998. return 0;
  999. fail4:
  1000. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  1001. fail3:
  1002. efx->membase_phys = 0;
  1003. fail2:
  1004. pci_disable_device(efx->pci_dev);
  1005. fail1:
  1006. return rc;
  1007. }
  1008. static void efx_fini_io(struct efx_nic *efx)
  1009. {
  1010. netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
  1011. if (efx->membase) {
  1012. iounmap(efx->membase);
  1013. efx->membase = NULL;
  1014. }
  1015. if (efx->membase_phys) {
  1016. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  1017. efx->membase_phys = 0;
  1018. }
  1019. pci_disable_device(efx->pci_dev);
  1020. }
  1021. static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
  1022. {
  1023. cpumask_var_t thread_mask;
  1024. unsigned int count;
  1025. int cpu;
  1026. if (rss_cpus) {
  1027. count = rss_cpus;
  1028. } else {
  1029. if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
  1030. netif_warn(efx, probe, efx->net_dev,
  1031. "RSS disabled due to allocation failure\n");
  1032. return 1;
  1033. }
  1034. count = 0;
  1035. for_each_online_cpu(cpu) {
  1036. if (!cpumask_test_cpu(cpu, thread_mask)) {
  1037. ++count;
  1038. cpumask_or(thread_mask, thread_mask,
  1039. topology_thread_cpumask(cpu));
  1040. }
  1041. }
  1042. free_cpumask_var(thread_mask);
  1043. }
  1044. /* If RSS is requested for the PF *and* VFs then we can't write RSS
  1045. * table entries that are inaccessible to VFs
  1046. */
  1047. if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
  1048. count > efx_vf_size(efx)) {
  1049. netif_warn(efx, probe, efx->net_dev,
  1050. "Reducing number of RSS channels from %u to %u for "
  1051. "VF support. Increase vf-msix-limit to use more "
  1052. "channels on the PF.\n",
  1053. count, efx_vf_size(efx));
  1054. count = efx_vf_size(efx);
  1055. }
  1056. return count;
  1057. }
  1058. /* Probe the number and type of interrupts we are able to obtain, and
  1059. * the resulting numbers of channels and RX queues.
  1060. */
  1061. static int efx_probe_interrupts(struct efx_nic *efx)
  1062. {
  1063. unsigned int extra_channels = 0;
  1064. unsigned int i, j;
  1065. int rc;
  1066. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
  1067. if (efx->extra_channel_type[i])
  1068. ++extra_channels;
  1069. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  1070. struct msix_entry xentries[EFX_MAX_CHANNELS];
  1071. unsigned int n_channels;
  1072. n_channels = efx_wanted_parallelism(efx);
  1073. if (separate_tx_channels)
  1074. n_channels *= 2;
  1075. n_channels += extra_channels;
  1076. n_channels = min(n_channels, efx->max_channels);
  1077. for (i = 0; i < n_channels; i++)
  1078. xentries[i].entry = i;
  1079. rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
  1080. if (rc > 0) {
  1081. netif_err(efx, drv, efx->net_dev,
  1082. "WARNING: Insufficient MSI-X vectors"
  1083. " available (%d < %u).\n", rc, n_channels);
  1084. netif_err(efx, drv, efx->net_dev,
  1085. "WARNING: Performance may be reduced.\n");
  1086. EFX_BUG_ON_PARANOID(rc >= n_channels);
  1087. n_channels = rc;
  1088. rc = pci_enable_msix(efx->pci_dev, xentries,
  1089. n_channels);
  1090. }
  1091. if (rc == 0) {
  1092. efx->n_channels = n_channels;
  1093. if (n_channels > extra_channels)
  1094. n_channels -= extra_channels;
  1095. if (separate_tx_channels) {
  1096. efx->n_tx_channels = max(n_channels / 2, 1U);
  1097. efx->n_rx_channels = max(n_channels -
  1098. efx->n_tx_channels,
  1099. 1U);
  1100. } else {
  1101. efx->n_tx_channels = n_channels;
  1102. efx->n_rx_channels = n_channels;
  1103. }
  1104. for (i = 0; i < efx->n_channels; i++)
  1105. efx_get_channel(efx, i)->irq =
  1106. xentries[i].vector;
  1107. } else {
  1108. /* Fall back to single channel MSI */
  1109. efx->interrupt_mode = EFX_INT_MODE_MSI;
  1110. netif_err(efx, drv, efx->net_dev,
  1111. "could not enable MSI-X\n");
  1112. }
  1113. }
  1114. /* Try single interrupt MSI */
  1115. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  1116. efx->n_channels = 1;
  1117. efx->n_rx_channels = 1;
  1118. efx->n_tx_channels = 1;
  1119. rc = pci_enable_msi(efx->pci_dev);
  1120. if (rc == 0) {
  1121. efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
  1122. } else {
  1123. netif_err(efx, drv, efx->net_dev,
  1124. "could not enable MSI\n");
  1125. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  1126. }
  1127. }
  1128. /* Assume legacy interrupts */
  1129. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  1130. efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
  1131. efx->n_rx_channels = 1;
  1132. efx->n_tx_channels = 1;
  1133. efx->legacy_irq = efx->pci_dev->irq;
  1134. }
  1135. /* Assign extra channels if possible */
  1136. j = efx->n_channels;
  1137. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
  1138. if (!efx->extra_channel_type[i])
  1139. continue;
  1140. if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
  1141. efx->n_channels <= extra_channels) {
  1142. efx->extra_channel_type[i]->handle_no_channel(efx);
  1143. } else {
  1144. --j;
  1145. efx_get_channel(efx, j)->type =
  1146. efx->extra_channel_type[i];
  1147. }
  1148. }
  1149. /* RSS might be usable on VFs even if it is disabled on the PF */
  1150. efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
  1151. efx->n_rx_channels : efx_vf_size(efx));
  1152. return 0;
  1153. }
  1154. static int efx_soft_enable_interrupts(struct efx_nic *efx)
  1155. {
  1156. struct efx_channel *channel, *end_channel;
  1157. int rc;
  1158. BUG_ON(efx->state == STATE_DISABLED);
  1159. efx->irq_soft_enabled = true;
  1160. smp_wmb();
  1161. efx_for_each_channel(channel, efx) {
  1162. if (!channel->type->keep_eventq) {
  1163. rc = efx_init_eventq(channel);
  1164. if (rc)
  1165. goto fail;
  1166. }
  1167. efx_start_eventq(channel);
  1168. }
  1169. efx_mcdi_mode_event(efx);
  1170. return 0;
  1171. fail:
  1172. end_channel = channel;
  1173. efx_for_each_channel(channel, efx) {
  1174. if (channel == end_channel)
  1175. break;
  1176. efx_stop_eventq(channel);
  1177. if (!channel->type->keep_eventq)
  1178. efx_fini_eventq(channel);
  1179. }
  1180. return rc;
  1181. }
  1182. static void efx_soft_disable_interrupts(struct efx_nic *efx)
  1183. {
  1184. struct efx_channel *channel;
  1185. if (efx->state == STATE_DISABLED)
  1186. return;
  1187. efx_mcdi_mode_poll(efx);
  1188. efx->irq_soft_enabled = false;
  1189. smp_wmb();
  1190. if (efx->legacy_irq)
  1191. synchronize_irq(efx->legacy_irq);
  1192. efx_for_each_channel(channel, efx) {
  1193. if (channel->irq)
  1194. synchronize_irq(channel->irq);
  1195. efx_stop_eventq(channel);
  1196. if (!channel->type->keep_eventq)
  1197. efx_fini_eventq(channel);
  1198. }
  1199. /* Flush the asynchronous MCDI request queue */
  1200. efx_mcdi_flush_async(efx);
  1201. }
  1202. static int efx_enable_interrupts(struct efx_nic *efx)
  1203. {
  1204. struct efx_channel *channel, *end_channel;
  1205. int rc;
  1206. BUG_ON(efx->state == STATE_DISABLED);
  1207. if (efx->eeh_disabled_legacy_irq) {
  1208. enable_irq(efx->legacy_irq);
  1209. efx->eeh_disabled_legacy_irq = false;
  1210. }
  1211. efx->type->irq_enable_master(efx);
  1212. efx_for_each_channel(channel, efx) {
  1213. if (channel->type->keep_eventq) {
  1214. rc = efx_init_eventq(channel);
  1215. if (rc)
  1216. goto fail;
  1217. }
  1218. }
  1219. rc = efx_soft_enable_interrupts(efx);
  1220. if (rc)
  1221. goto fail;
  1222. return 0;
  1223. fail:
  1224. end_channel = channel;
  1225. efx_for_each_channel(channel, efx) {
  1226. if (channel == end_channel)
  1227. break;
  1228. if (channel->type->keep_eventq)
  1229. efx_fini_eventq(channel);
  1230. }
  1231. efx->type->irq_disable_non_ev(efx);
  1232. return rc;
  1233. }
  1234. static void efx_disable_interrupts(struct efx_nic *efx)
  1235. {
  1236. struct efx_channel *channel;
  1237. efx_soft_disable_interrupts(efx);
  1238. efx_for_each_channel(channel, efx) {
  1239. if (channel->type->keep_eventq)
  1240. efx_fini_eventq(channel);
  1241. }
  1242. efx->type->irq_disable_non_ev(efx);
  1243. }
  1244. static void efx_remove_interrupts(struct efx_nic *efx)
  1245. {
  1246. struct efx_channel *channel;
  1247. /* Remove MSI/MSI-X interrupts */
  1248. efx_for_each_channel(channel, efx)
  1249. channel->irq = 0;
  1250. pci_disable_msi(efx->pci_dev);
  1251. pci_disable_msix(efx->pci_dev);
  1252. /* Remove legacy interrupt */
  1253. efx->legacy_irq = 0;
  1254. }
  1255. static void efx_set_channels(struct efx_nic *efx)
  1256. {
  1257. struct efx_channel *channel;
  1258. struct efx_tx_queue *tx_queue;
  1259. efx->tx_channel_offset =
  1260. separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
  1261. /* We need to mark which channels really have RX and TX
  1262. * queues, and adjust the TX queue numbers if we have separate
  1263. * RX-only and TX-only channels.
  1264. */
  1265. efx_for_each_channel(channel, efx) {
  1266. if (channel->channel < efx->n_rx_channels)
  1267. channel->rx_queue.core_index = channel->channel;
  1268. else
  1269. channel->rx_queue.core_index = -1;
  1270. efx_for_each_channel_tx_queue(tx_queue, channel)
  1271. tx_queue->queue -= (efx->tx_channel_offset *
  1272. EFX_TXQ_TYPES);
  1273. }
  1274. }
  1275. static int efx_probe_nic(struct efx_nic *efx)
  1276. {
  1277. size_t i;
  1278. int rc;
  1279. netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
  1280. /* Carry out hardware-type specific initialisation */
  1281. rc = efx->type->probe(efx);
  1282. if (rc)
  1283. return rc;
  1284. /* Determine the number of channels and queues by trying to hook
  1285. * in MSI-X interrupts. */
  1286. rc = efx_probe_interrupts(efx);
  1287. if (rc)
  1288. goto fail1;
  1289. rc = efx->type->dimension_resources(efx);
  1290. if (rc)
  1291. goto fail2;
  1292. if (efx->n_channels > 1)
  1293. get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
  1294. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
  1295. efx->rx_indir_table[i] =
  1296. ethtool_rxfh_indir_default(i, efx->rss_spread);
  1297. efx_set_channels(efx);
  1298. netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
  1299. netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
  1300. /* Initialise the interrupt moderation settings */
  1301. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
  1302. true);
  1303. return 0;
  1304. fail2:
  1305. efx_remove_interrupts(efx);
  1306. fail1:
  1307. efx->type->remove(efx);
  1308. return rc;
  1309. }
  1310. static void efx_remove_nic(struct efx_nic *efx)
  1311. {
  1312. netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
  1313. efx_remove_interrupts(efx);
  1314. efx->type->remove(efx);
  1315. }
  1316. static int efx_probe_filters(struct efx_nic *efx)
  1317. {
  1318. int rc;
  1319. spin_lock_init(&efx->filter_lock);
  1320. rc = efx->type->filter_table_probe(efx);
  1321. if (rc)
  1322. return rc;
  1323. #ifdef CONFIG_RFS_ACCEL
  1324. if (efx->type->offload_features & NETIF_F_NTUPLE) {
  1325. efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
  1326. sizeof(*efx->rps_flow_id),
  1327. GFP_KERNEL);
  1328. if (!efx->rps_flow_id) {
  1329. efx->type->filter_table_remove(efx);
  1330. return -ENOMEM;
  1331. }
  1332. }
  1333. #endif
  1334. return 0;
  1335. }
  1336. static void efx_remove_filters(struct efx_nic *efx)
  1337. {
  1338. #ifdef CONFIG_RFS_ACCEL
  1339. kfree(efx->rps_flow_id);
  1340. #endif
  1341. efx->type->filter_table_remove(efx);
  1342. }
  1343. static void efx_restore_filters(struct efx_nic *efx)
  1344. {
  1345. efx->type->filter_table_restore(efx);
  1346. }
  1347. /**************************************************************************
  1348. *
  1349. * NIC startup/shutdown
  1350. *
  1351. *************************************************************************/
  1352. static int efx_probe_all(struct efx_nic *efx)
  1353. {
  1354. int rc;
  1355. rc = efx_probe_nic(efx);
  1356. if (rc) {
  1357. netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
  1358. goto fail1;
  1359. }
  1360. rc = efx_probe_port(efx);
  1361. if (rc) {
  1362. netif_err(efx, probe, efx->net_dev, "failed to create port\n");
  1363. goto fail2;
  1364. }
  1365. BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
  1366. if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
  1367. rc = -EINVAL;
  1368. goto fail3;
  1369. }
  1370. efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
  1371. rc = efx_probe_filters(efx);
  1372. if (rc) {
  1373. netif_err(efx, probe, efx->net_dev,
  1374. "failed to create filter tables\n");
  1375. goto fail3;
  1376. }
  1377. rc = efx_probe_channels(efx);
  1378. if (rc)
  1379. goto fail4;
  1380. return 0;
  1381. fail4:
  1382. efx_remove_filters(efx);
  1383. fail3:
  1384. efx_remove_port(efx);
  1385. fail2:
  1386. efx_remove_nic(efx);
  1387. fail1:
  1388. return rc;
  1389. }
  1390. /* If the interface is supposed to be running but is not, start
  1391. * the hardware and software data path, regular activity for the port
  1392. * (MAC statistics, link polling, etc.) and schedule the port to be
  1393. * reconfigured. Interrupts must already be enabled. This function
  1394. * is safe to call multiple times, so long as the NIC is not disabled.
  1395. * Requires the RTNL lock.
  1396. */
  1397. static void efx_start_all(struct efx_nic *efx)
  1398. {
  1399. EFX_ASSERT_RESET_SERIALISED(efx);
  1400. BUG_ON(efx->state == STATE_DISABLED);
  1401. /* Check that it is appropriate to restart the interface. All
  1402. * of these flags are safe to read under just the rtnl lock */
  1403. if (efx->port_enabled || !netif_running(efx->net_dev))
  1404. return;
  1405. efx_start_port(efx);
  1406. efx_start_datapath(efx);
  1407. /* Start the hardware monitor if there is one */
  1408. if (efx->type->monitor != NULL)
  1409. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1410. efx_monitor_interval);
  1411. /* If link state detection is normally event-driven, we have
  1412. * to poll now because we could have missed a change
  1413. */
  1414. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
  1415. mutex_lock(&efx->mac_lock);
  1416. if (efx->phy_op->poll(efx))
  1417. efx_link_status_changed(efx);
  1418. mutex_unlock(&efx->mac_lock);
  1419. }
  1420. efx->type->start_stats(efx);
  1421. efx->type->pull_stats(efx);
  1422. spin_lock_bh(&efx->stats_lock);
  1423. efx->type->update_stats(efx, NULL, NULL);
  1424. spin_unlock_bh(&efx->stats_lock);
  1425. }
  1426. /* Quiesce the hardware and software data path, and regular activity
  1427. * for the port without bringing the link down. Safe to call multiple
  1428. * times with the NIC in almost any state, but interrupts should be
  1429. * enabled. Requires the RTNL lock.
  1430. */
  1431. static void efx_stop_all(struct efx_nic *efx)
  1432. {
  1433. EFX_ASSERT_RESET_SERIALISED(efx);
  1434. /* port_enabled can be read safely under the rtnl lock */
  1435. if (!efx->port_enabled)
  1436. return;
  1437. /* update stats before we go down so we can accurately count
  1438. * rx_nodesc_drops
  1439. */
  1440. efx->type->pull_stats(efx);
  1441. spin_lock_bh(&efx->stats_lock);
  1442. efx->type->update_stats(efx, NULL, NULL);
  1443. spin_unlock_bh(&efx->stats_lock);
  1444. efx->type->stop_stats(efx);
  1445. efx_stop_port(efx);
  1446. /* Stop the kernel transmit interface. This is only valid if
  1447. * the device is stopped or detached; otherwise the watchdog
  1448. * may fire immediately.
  1449. */
  1450. WARN_ON(netif_running(efx->net_dev) &&
  1451. netif_device_present(efx->net_dev));
  1452. netif_tx_disable(efx->net_dev);
  1453. efx_stop_datapath(efx);
  1454. }
  1455. static void efx_remove_all(struct efx_nic *efx)
  1456. {
  1457. efx_remove_channels(efx);
  1458. efx_remove_filters(efx);
  1459. efx_remove_port(efx);
  1460. efx_remove_nic(efx);
  1461. }
  1462. /**************************************************************************
  1463. *
  1464. * Interrupt moderation
  1465. *
  1466. **************************************************************************/
  1467. static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
  1468. {
  1469. if (usecs == 0)
  1470. return 0;
  1471. if (usecs * 1000 < quantum_ns)
  1472. return 1; /* never round down to 0 */
  1473. return usecs * 1000 / quantum_ns;
  1474. }
  1475. /* Set interrupt moderation parameters */
  1476. int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
  1477. unsigned int rx_usecs, bool rx_adaptive,
  1478. bool rx_may_override_tx)
  1479. {
  1480. struct efx_channel *channel;
  1481. unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
  1482. efx->timer_quantum_ns,
  1483. 1000);
  1484. unsigned int tx_ticks;
  1485. unsigned int rx_ticks;
  1486. EFX_ASSERT_RESET_SERIALISED(efx);
  1487. if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
  1488. return -EINVAL;
  1489. tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
  1490. rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
  1491. if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
  1492. !rx_may_override_tx) {
  1493. netif_err(efx, drv, efx->net_dev, "Channels are shared. "
  1494. "RX and TX IRQ moderation must be equal\n");
  1495. return -EINVAL;
  1496. }
  1497. efx->irq_rx_adaptive = rx_adaptive;
  1498. efx->irq_rx_moderation = rx_ticks;
  1499. efx_for_each_channel(channel, efx) {
  1500. if (efx_channel_has_rx_queue(channel))
  1501. channel->irq_moderation = rx_ticks;
  1502. else if (efx_channel_has_tx_queues(channel))
  1503. channel->irq_moderation = tx_ticks;
  1504. }
  1505. return 0;
  1506. }
  1507. void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
  1508. unsigned int *rx_usecs, bool *rx_adaptive)
  1509. {
  1510. /* We must round up when converting ticks to microseconds
  1511. * because we round down when converting the other way.
  1512. */
  1513. *rx_adaptive = efx->irq_rx_adaptive;
  1514. *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
  1515. efx->timer_quantum_ns,
  1516. 1000);
  1517. /* If channels are shared between RX and TX, so is IRQ
  1518. * moderation. Otherwise, IRQ moderation is the same for all
  1519. * TX channels and is not adaptive.
  1520. */
  1521. if (efx->tx_channel_offset == 0)
  1522. *tx_usecs = *rx_usecs;
  1523. else
  1524. *tx_usecs = DIV_ROUND_UP(
  1525. efx->channel[efx->tx_channel_offset]->irq_moderation *
  1526. efx->timer_quantum_ns,
  1527. 1000);
  1528. }
  1529. /**************************************************************************
  1530. *
  1531. * Hardware monitor
  1532. *
  1533. **************************************************************************/
  1534. /* Run periodically off the general workqueue */
  1535. static void efx_monitor(struct work_struct *data)
  1536. {
  1537. struct efx_nic *efx = container_of(data, struct efx_nic,
  1538. monitor_work.work);
  1539. netif_vdbg(efx, timer, efx->net_dev,
  1540. "hardware monitor executing on CPU %d\n",
  1541. raw_smp_processor_id());
  1542. BUG_ON(efx->type->monitor == NULL);
  1543. /* If the mac_lock is already held then it is likely a port
  1544. * reconfiguration is already in place, which will likely do
  1545. * most of the work of monitor() anyway. */
  1546. if (mutex_trylock(&efx->mac_lock)) {
  1547. if (efx->port_enabled)
  1548. efx->type->monitor(efx);
  1549. mutex_unlock(&efx->mac_lock);
  1550. }
  1551. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1552. efx_monitor_interval);
  1553. }
  1554. /**************************************************************************
  1555. *
  1556. * ioctls
  1557. *
  1558. *************************************************************************/
  1559. /* Net device ioctl
  1560. * Context: process, rtnl_lock() held.
  1561. */
  1562. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1563. {
  1564. struct efx_nic *efx = netdev_priv(net_dev);
  1565. struct mii_ioctl_data *data = if_mii(ifr);
  1566. if (cmd == SIOCSHWTSTAMP)
  1567. return efx_ptp_set_ts_config(efx, ifr);
  1568. if (cmd == SIOCGHWTSTAMP)
  1569. return efx_ptp_get_ts_config(efx, ifr);
  1570. /* Convert phy_id from older PRTAD/DEVAD format */
  1571. if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
  1572. (data->phy_id & 0xfc00) == 0x0400)
  1573. data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
  1574. return mdio_mii_ioctl(&efx->mdio, data, cmd);
  1575. }
  1576. /**************************************************************************
  1577. *
  1578. * NAPI interface
  1579. *
  1580. **************************************************************************/
  1581. static void efx_init_napi_channel(struct efx_channel *channel)
  1582. {
  1583. struct efx_nic *efx = channel->efx;
  1584. channel->napi_dev = efx->net_dev;
  1585. netif_napi_add(channel->napi_dev, &channel->napi_str,
  1586. efx_poll, napi_weight);
  1587. }
  1588. static void efx_init_napi(struct efx_nic *efx)
  1589. {
  1590. struct efx_channel *channel;
  1591. efx_for_each_channel(channel, efx)
  1592. efx_init_napi_channel(channel);
  1593. }
  1594. static void efx_fini_napi_channel(struct efx_channel *channel)
  1595. {
  1596. if (channel->napi_dev)
  1597. netif_napi_del(&channel->napi_str);
  1598. channel->napi_dev = NULL;
  1599. }
  1600. static void efx_fini_napi(struct efx_nic *efx)
  1601. {
  1602. struct efx_channel *channel;
  1603. efx_for_each_channel(channel, efx)
  1604. efx_fini_napi_channel(channel);
  1605. }
  1606. /**************************************************************************
  1607. *
  1608. * Kernel netpoll interface
  1609. *
  1610. *************************************************************************/
  1611. #ifdef CONFIG_NET_POLL_CONTROLLER
  1612. /* Although in the common case interrupts will be disabled, this is not
  1613. * guaranteed. However, all our work happens inside the NAPI callback,
  1614. * so no locking is required.
  1615. */
  1616. static void efx_netpoll(struct net_device *net_dev)
  1617. {
  1618. struct efx_nic *efx = netdev_priv(net_dev);
  1619. struct efx_channel *channel;
  1620. efx_for_each_channel(channel, efx)
  1621. efx_schedule_channel(channel);
  1622. }
  1623. #endif
  1624. /**************************************************************************
  1625. *
  1626. * Kernel net device interface
  1627. *
  1628. *************************************************************************/
  1629. /* Context: process, rtnl_lock() held. */
  1630. static int efx_net_open(struct net_device *net_dev)
  1631. {
  1632. struct efx_nic *efx = netdev_priv(net_dev);
  1633. int rc;
  1634. netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
  1635. raw_smp_processor_id());
  1636. rc = efx_check_disabled(efx);
  1637. if (rc)
  1638. return rc;
  1639. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1640. return -EBUSY;
  1641. if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
  1642. return -EIO;
  1643. /* Notify the kernel of the link state polled during driver load,
  1644. * before the monitor starts running */
  1645. efx_link_status_changed(efx);
  1646. efx_start_all(efx);
  1647. efx_selftest_async_start(efx);
  1648. return 0;
  1649. }
  1650. /* Context: process, rtnl_lock() held.
  1651. * Note that the kernel will ignore our return code; this method
  1652. * should really be a void.
  1653. */
  1654. static int efx_net_stop(struct net_device *net_dev)
  1655. {
  1656. struct efx_nic *efx = netdev_priv(net_dev);
  1657. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  1658. raw_smp_processor_id());
  1659. /* Stop the device and flush all the channels */
  1660. efx_stop_all(efx);
  1661. return 0;
  1662. }
  1663. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1664. static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
  1665. struct rtnl_link_stats64 *stats)
  1666. {
  1667. struct efx_nic *efx = netdev_priv(net_dev);
  1668. spin_lock_bh(&efx->stats_lock);
  1669. efx->type->update_stats(efx, NULL, stats);
  1670. spin_unlock_bh(&efx->stats_lock);
  1671. return stats;
  1672. }
  1673. /* Context: netif_tx_lock held, BHs disabled. */
  1674. static void efx_watchdog(struct net_device *net_dev)
  1675. {
  1676. struct efx_nic *efx = netdev_priv(net_dev);
  1677. netif_err(efx, tx_err, efx->net_dev,
  1678. "TX stuck with port_enabled=%d: resetting channels\n",
  1679. efx->port_enabled);
  1680. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1681. }
  1682. /* Context: process, rtnl_lock() held. */
  1683. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1684. {
  1685. struct efx_nic *efx = netdev_priv(net_dev);
  1686. int rc;
  1687. rc = efx_check_disabled(efx);
  1688. if (rc)
  1689. return rc;
  1690. if (new_mtu > EFX_MAX_MTU)
  1691. return -EINVAL;
  1692. netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
  1693. efx_device_detach_sync(efx);
  1694. efx_stop_all(efx);
  1695. mutex_lock(&efx->mac_lock);
  1696. net_dev->mtu = new_mtu;
  1697. efx->type->reconfigure_mac(efx);
  1698. mutex_unlock(&efx->mac_lock);
  1699. efx_start_all(efx);
  1700. netif_device_attach(efx->net_dev);
  1701. return 0;
  1702. }
  1703. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1704. {
  1705. struct efx_nic *efx = netdev_priv(net_dev);
  1706. struct sockaddr *addr = data;
  1707. char *new_addr = addr->sa_data;
  1708. if (!is_valid_ether_addr(new_addr)) {
  1709. netif_err(efx, drv, efx->net_dev,
  1710. "invalid ethernet MAC address requested: %pM\n",
  1711. new_addr);
  1712. return -EADDRNOTAVAIL;
  1713. }
  1714. memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
  1715. efx_sriov_mac_address_changed(efx);
  1716. /* Reconfigure the MAC */
  1717. mutex_lock(&efx->mac_lock);
  1718. efx->type->reconfigure_mac(efx);
  1719. mutex_unlock(&efx->mac_lock);
  1720. return 0;
  1721. }
  1722. /* Context: netif_addr_lock held, BHs disabled. */
  1723. static void efx_set_rx_mode(struct net_device *net_dev)
  1724. {
  1725. struct efx_nic *efx = netdev_priv(net_dev);
  1726. if (efx->port_enabled)
  1727. queue_work(efx->workqueue, &efx->mac_work);
  1728. /* Otherwise efx_start_port() will do this */
  1729. }
  1730. static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
  1731. {
  1732. struct efx_nic *efx = netdev_priv(net_dev);
  1733. /* If disabling RX n-tuple filtering, clear existing filters */
  1734. if (net_dev->features & ~data & NETIF_F_NTUPLE)
  1735. efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
  1736. return 0;
  1737. }
  1738. static const struct net_device_ops efx_farch_netdev_ops = {
  1739. .ndo_open = efx_net_open,
  1740. .ndo_stop = efx_net_stop,
  1741. .ndo_get_stats64 = efx_net_stats,
  1742. .ndo_tx_timeout = efx_watchdog,
  1743. .ndo_start_xmit = efx_hard_start_xmit,
  1744. .ndo_validate_addr = eth_validate_addr,
  1745. .ndo_do_ioctl = efx_ioctl,
  1746. .ndo_change_mtu = efx_change_mtu,
  1747. .ndo_set_mac_address = efx_set_mac_address,
  1748. .ndo_set_rx_mode = efx_set_rx_mode,
  1749. .ndo_set_features = efx_set_features,
  1750. #ifdef CONFIG_SFC_SRIOV
  1751. .ndo_set_vf_mac = efx_sriov_set_vf_mac,
  1752. .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
  1753. .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
  1754. .ndo_get_vf_config = efx_sriov_get_vf_config,
  1755. #endif
  1756. #ifdef CONFIG_NET_POLL_CONTROLLER
  1757. .ndo_poll_controller = efx_netpoll,
  1758. #endif
  1759. .ndo_setup_tc = efx_setup_tc,
  1760. #ifdef CONFIG_RFS_ACCEL
  1761. .ndo_rx_flow_steer = efx_filter_rfs,
  1762. #endif
  1763. };
  1764. static const struct net_device_ops efx_ef10_netdev_ops = {
  1765. .ndo_open = efx_net_open,
  1766. .ndo_stop = efx_net_stop,
  1767. .ndo_get_stats64 = efx_net_stats,
  1768. .ndo_tx_timeout = efx_watchdog,
  1769. .ndo_start_xmit = efx_hard_start_xmit,
  1770. .ndo_validate_addr = eth_validate_addr,
  1771. .ndo_do_ioctl = efx_ioctl,
  1772. .ndo_change_mtu = efx_change_mtu,
  1773. .ndo_set_mac_address = efx_set_mac_address,
  1774. .ndo_set_rx_mode = efx_set_rx_mode,
  1775. .ndo_set_features = efx_set_features,
  1776. #ifdef CONFIG_NET_POLL_CONTROLLER
  1777. .ndo_poll_controller = efx_netpoll,
  1778. #endif
  1779. #ifdef CONFIG_RFS_ACCEL
  1780. .ndo_rx_flow_steer = efx_filter_rfs,
  1781. #endif
  1782. };
  1783. static void efx_update_name(struct efx_nic *efx)
  1784. {
  1785. strcpy(efx->name, efx->net_dev->name);
  1786. efx_mtd_rename(efx);
  1787. efx_set_channel_names(efx);
  1788. }
  1789. static int efx_netdev_event(struct notifier_block *this,
  1790. unsigned long event, void *ptr)
  1791. {
  1792. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  1793. if ((net_dev->netdev_ops == &efx_farch_netdev_ops ||
  1794. net_dev->netdev_ops == &efx_ef10_netdev_ops) &&
  1795. event == NETDEV_CHANGENAME)
  1796. efx_update_name(netdev_priv(net_dev));
  1797. return NOTIFY_DONE;
  1798. }
  1799. static struct notifier_block efx_netdev_notifier = {
  1800. .notifier_call = efx_netdev_event,
  1801. };
  1802. static ssize_t
  1803. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  1804. {
  1805. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1806. return sprintf(buf, "%d\n", efx->phy_type);
  1807. }
  1808. static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
  1809. static int efx_register_netdev(struct efx_nic *efx)
  1810. {
  1811. struct net_device *net_dev = efx->net_dev;
  1812. struct efx_channel *channel;
  1813. int rc;
  1814. net_dev->watchdog_timeo = 5 * HZ;
  1815. net_dev->irq = efx->pci_dev->irq;
  1816. if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
  1817. net_dev->netdev_ops = &efx_ef10_netdev_ops;
  1818. net_dev->priv_flags |= IFF_UNICAST_FLT;
  1819. } else {
  1820. net_dev->netdev_ops = &efx_farch_netdev_ops;
  1821. }
  1822. SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
  1823. net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
  1824. rtnl_lock();
  1825. /* Enable resets to be scheduled and check whether any were
  1826. * already requested. If so, the NIC is probably hosed so we
  1827. * abort.
  1828. */
  1829. efx->state = STATE_READY;
  1830. smp_mb(); /* ensure we change state before checking reset_pending */
  1831. if (efx->reset_pending) {
  1832. netif_err(efx, probe, efx->net_dev,
  1833. "aborting probe due to scheduled reset\n");
  1834. rc = -EIO;
  1835. goto fail_locked;
  1836. }
  1837. rc = dev_alloc_name(net_dev, net_dev->name);
  1838. if (rc < 0)
  1839. goto fail_locked;
  1840. efx_update_name(efx);
  1841. /* Always start with carrier off; PHY events will detect the link */
  1842. netif_carrier_off(net_dev);
  1843. rc = register_netdevice(net_dev);
  1844. if (rc)
  1845. goto fail_locked;
  1846. efx_for_each_channel(channel, efx) {
  1847. struct efx_tx_queue *tx_queue;
  1848. efx_for_each_channel_tx_queue(tx_queue, channel)
  1849. efx_init_tx_queue_core_txq(tx_queue);
  1850. }
  1851. rtnl_unlock();
  1852. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1853. if (rc) {
  1854. netif_err(efx, drv, efx->net_dev,
  1855. "failed to init net dev attributes\n");
  1856. goto fail_registered;
  1857. }
  1858. return 0;
  1859. fail_registered:
  1860. rtnl_lock();
  1861. unregister_netdevice(net_dev);
  1862. fail_locked:
  1863. efx->state = STATE_UNINIT;
  1864. rtnl_unlock();
  1865. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  1866. return rc;
  1867. }
  1868. static void efx_unregister_netdev(struct efx_nic *efx)
  1869. {
  1870. if (!efx->net_dev)
  1871. return;
  1872. BUG_ON(netdev_priv(efx->net_dev) != efx);
  1873. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  1874. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1875. rtnl_lock();
  1876. unregister_netdevice(efx->net_dev);
  1877. efx->state = STATE_UNINIT;
  1878. rtnl_unlock();
  1879. }
  1880. /**************************************************************************
  1881. *
  1882. * Device reset and suspend
  1883. *
  1884. **************************************************************************/
  1885. /* Tears down the entire software state and most of the hardware state
  1886. * before reset. */
  1887. void efx_reset_down(struct efx_nic *efx, enum reset_type method)
  1888. {
  1889. EFX_ASSERT_RESET_SERIALISED(efx);
  1890. efx_stop_all(efx);
  1891. efx_disable_interrupts(efx);
  1892. mutex_lock(&efx->mac_lock);
  1893. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
  1894. efx->phy_op->fini(efx);
  1895. efx->type->fini(efx);
  1896. }
  1897. /* This function will always ensure that the locks acquired in
  1898. * efx_reset_down() are released. A failure return code indicates
  1899. * that we were unable to reinitialise the hardware, and the
  1900. * driver should be disabled. If ok is false, then the rx and tx
  1901. * engines are not restarted, pending a RESET_DISABLE. */
  1902. int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
  1903. {
  1904. int rc;
  1905. EFX_ASSERT_RESET_SERIALISED(efx);
  1906. rc = efx->type->init(efx);
  1907. if (rc) {
  1908. netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
  1909. goto fail;
  1910. }
  1911. if (!ok)
  1912. goto fail;
  1913. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
  1914. rc = efx->phy_op->init(efx);
  1915. if (rc)
  1916. goto fail;
  1917. if (efx->phy_op->reconfigure(efx))
  1918. netif_err(efx, drv, efx->net_dev,
  1919. "could not restore PHY settings\n");
  1920. }
  1921. rc = efx_enable_interrupts(efx);
  1922. if (rc)
  1923. goto fail;
  1924. efx_restore_filters(efx);
  1925. efx_sriov_reset(efx);
  1926. mutex_unlock(&efx->mac_lock);
  1927. efx_start_all(efx);
  1928. return 0;
  1929. fail:
  1930. efx->port_initialized = false;
  1931. mutex_unlock(&efx->mac_lock);
  1932. return rc;
  1933. }
  1934. /* Reset the NIC using the specified method. Note that the reset may
  1935. * fail, in which case the card will be left in an unusable state.
  1936. *
  1937. * Caller must hold the rtnl_lock.
  1938. */
  1939. int efx_reset(struct efx_nic *efx, enum reset_type method)
  1940. {
  1941. int rc, rc2;
  1942. bool disabled;
  1943. netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
  1944. RESET_TYPE(method));
  1945. efx_device_detach_sync(efx);
  1946. efx_reset_down(efx, method);
  1947. rc = efx->type->reset(efx, method);
  1948. if (rc) {
  1949. netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
  1950. goto out;
  1951. }
  1952. /* Clear flags for the scopes we covered. We assume the NIC and
  1953. * driver are now quiescent so that there is no race here.
  1954. */
  1955. efx->reset_pending &= -(1 << (method + 1));
  1956. /* Reinitialise bus-mastering, which may have been turned off before
  1957. * the reset was scheduled. This is still appropriate, even in the
  1958. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  1959. * can respond to requests. */
  1960. pci_set_master(efx->pci_dev);
  1961. out:
  1962. /* Leave device stopped if necessary */
  1963. disabled = rc ||
  1964. method == RESET_TYPE_DISABLE ||
  1965. method == RESET_TYPE_RECOVER_OR_DISABLE;
  1966. rc2 = efx_reset_up(efx, method, !disabled);
  1967. if (rc2) {
  1968. disabled = true;
  1969. if (!rc)
  1970. rc = rc2;
  1971. }
  1972. if (disabled) {
  1973. dev_close(efx->net_dev);
  1974. netif_err(efx, drv, efx->net_dev, "has been disabled\n");
  1975. efx->state = STATE_DISABLED;
  1976. } else {
  1977. netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
  1978. netif_device_attach(efx->net_dev);
  1979. }
  1980. return rc;
  1981. }
  1982. /* Try recovery mechanisms.
  1983. * For now only EEH is supported.
  1984. * Returns 0 if the recovery mechanisms are unsuccessful.
  1985. * Returns a non-zero value otherwise.
  1986. */
  1987. int efx_try_recovery(struct efx_nic *efx)
  1988. {
  1989. #ifdef CONFIG_EEH
  1990. /* A PCI error can occur and not be seen by EEH because nothing
  1991. * happens on the PCI bus. In this case the driver may fail and
  1992. * schedule a 'recover or reset', leading to this recovery handler.
  1993. * Manually call the eeh failure check function.
  1994. */
  1995. struct eeh_dev *eehdev =
  1996. of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
  1997. if (eeh_dev_check_failure(eehdev)) {
  1998. /* The EEH mechanisms will handle the error and reset the
  1999. * device if necessary.
  2000. */
  2001. return 1;
  2002. }
  2003. #endif
  2004. return 0;
  2005. }
  2006. static void efx_wait_for_bist_end(struct efx_nic *efx)
  2007. {
  2008. int i;
  2009. for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
  2010. if (efx_mcdi_poll_reboot(efx))
  2011. goto out;
  2012. msleep(BIST_WAIT_DELAY_MS);
  2013. }
  2014. netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
  2015. out:
  2016. /* Either way unset the BIST flag. If we found no reboot we probably
  2017. * won't recover, but we should try.
  2018. */
  2019. efx->mc_bist_for_other_fn = false;
  2020. }
  2021. /* The worker thread exists so that code that cannot sleep can
  2022. * schedule a reset for later.
  2023. */
  2024. static void efx_reset_work(struct work_struct *data)
  2025. {
  2026. struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
  2027. unsigned long pending;
  2028. enum reset_type method;
  2029. pending = ACCESS_ONCE(efx->reset_pending);
  2030. method = fls(pending) - 1;
  2031. if (method == RESET_TYPE_MC_BIST)
  2032. efx_wait_for_bist_end(efx);
  2033. if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
  2034. method == RESET_TYPE_RECOVER_OR_ALL) &&
  2035. efx_try_recovery(efx))
  2036. return;
  2037. if (!pending)
  2038. return;
  2039. rtnl_lock();
  2040. /* We checked the state in efx_schedule_reset() but it may
  2041. * have changed by now. Now that we have the RTNL lock,
  2042. * it cannot change again.
  2043. */
  2044. if (efx->state == STATE_READY)
  2045. (void)efx_reset(efx, method);
  2046. rtnl_unlock();
  2047. }
  2048. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  2049. {
  2050. enum reset_type method;
  2051. if (efx->state == STATE_RECOVERY) {
  2052. netif_dbg(efx, drv, efx->net_dev,
  2053. "recovering: skip scheduling %s reset\n",
  2054. RESET_TYPE(type));
  2055. return;
  2056. }
  2057. switch (type) {
  2058. case RESET_TYPE_INVISIBLE:
  2059. case RESET_TYPE_ALL:
  2060. case RESET_TYPE_RECOVER_OR_ALL:
  2061. case RESET_TYPE_WORLD:
  2062. case RESET_TYPE_DISABLE:
  2063. case RESET_TYPE_RECOVER_OR_DISABLE:
  2064. case RESET_TYPE_MC_BIST:
  2065. method = type;
  2066. netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
  2067. RESET_TYPE(method));
  2068. break;
  2069. default:
  2070. method = efx->type->map_reset_reason(type);
  2071. netif_dbg(efx, drv, efx->net_dev,
  2072. "scheduling %s reset for %s\n",
  2073. RESET_TYPE(method), RESET_TYPE(type));
  2074. break;
  2075. }
  2076. set_bit(method, &efx->reset_pending);
  2077. smp_mb(); /* ensure we change reset_pending before checking state */
  2078. /* If we're not READY then just leave the flags set as the cue
  2079. * to abort probing or reschedule the reset later.
  2080. */
  2081. if (ACCESS_ONCE(efx->state) != STATE_READY)
  2082. return;
  2083. /* efx_process_channel() will no longer read events once a
  2084. * reset is scheduled. So switch back to poll'd MCDI completions. */
  2085. efx_mcdi_mode_poll(efx);
  2086. queue_work(reset_workqueue, &efx->reset_work);
  2087. }
  2088. /**************************************************************************
  2089. *
  2090. * List of NICs we support
  2091. *
  2092. **************************************************************************/
  2093. /* PCI device ID table */
  2094. static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
  2095. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  2096. PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
  2097. .driver_data = (unsigned long) &falcon_a1_nic_type},
  2098. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  2099. PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
  2100. .driver_data = (unsigned long) &falcon_b0_nic_type},
  2101. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
  2102. .driver_data = (unsigned long) &siena_a0_nic_type},
  2103. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
  2104. .driver_data = (unsigned long) &siena_a0_nic_type},
  2105. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
  2106. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  2107. {0} /* end of list */
  2108. };
  2109. /**************************************************************************
  2110. *
  2111. * Dummy PHY/MAC operations
  2112. *
  2113. * Can be used for some unimplemented operations
  2114. * Needed so all function pointers are valid and do not have to be tested
  2115. * before use
  2116. *
  2117. **************************************************************************/
  2118. int efx_port_dummy_op_int(struct efx_nic *efx)
  2119. {
  2120. return 0;
  2121. }
  2122. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  2123. static bool efx_port_dummy_op_poll(struct efx_nic *efx)
  2124. {
  2125. return false;
  2126. }
  2127. static const struct efx_phy_operations efx_dummy_phy_operations = {
  2128. .init = efx_port_dummy_op_int,
  2129. .reconfigure = efx_port_dummy_op_int,
  2130. .poll = efx_port_dummy_op_poll,
  2131. .fini = efx_port_dummy_op_void,
  2132. };
  2133. /**************************************************************************
  2134. *
  2135. * Data housekeeping
  2136. *
  2137. **************************************************************************/
  2138. /* This zeroes out and then fills in the invariants in a struct
  2139. * efx_nic (including all sub-structures).
  2140. */
  2141. static int efx_init_struct(struct efx_nic *efx,
  2142. struct pci_dev *pci_dev, struct net_device *net_dev)
  2143. {
  2144. int i;
  2145. /* Initialise common structures */
  2146. spin_lock_init(&efx->biu_lock);
  2147. #ifdef CONFIG_SFC_MTD
  2148. INIT_LIST_HEAD(&efx->mtd_list);
  2149. #endif
  2150. INIT_WORK(&efx->reset_work, efx_reset_work);
  2151. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  2152. INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
  2153. efx->pci_dev = pci_dev;
  2154. efx->msg_enable = debug;
  2155. efx->state = STATE_UNINIT;
  2156. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  2157. efx->net_dev = net_dev;
  2158. efx->rx_prefix_size = efx->type->rx_prefix_size;
  2159. efx->rx_ip_align =
  2160. NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
  2161. efx->rx_packet_hash_offset =
  2162. efx->type->rx_hash_offset - efx->type->rx_prefix_size;
  2163. spin_lock_init(&efx->stats_lock);
  2164. mutex_init(&efx->mac_lock);
  2165. efx->phy_op = &efx_dummy_phy_operations;
  2166. efx->mdio.dev = net_dev;
  2167. INIT_WORK(&efx->mac_work, efx_mac_work);
  2168. init_waitqueue_head(&efx->flush_wq);
  2169. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  2170. efx->channel[i] = efx_alloc_channel(efx, i, NULL);
  2171. if (!efx->channel[i])
  2172. goto fail;
  2173. efx->msi_context[i].efx = efx;
  2174. efx->msi_context[i].index = i;
  2175. }
  2176. /* Higher numbered interrupt modes are less capable! */
  2177. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  2178. interrupt_mode);
  2179. /* Would be good to use the net_dev name, but we're too early */
  2180. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  2181. pci_name(pci_dev));
  2182. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  2183. if (!efx->workqueue)
  2184. goto fail;
  2185. return 0;
  2186. fail:
  2187. efx_fini_struct(efx);
  2188. return -ENOMEM;
  2189. }
  2190. static void efx_fini_struct(struct efx_nic *efx)
  2191. {
  2192. int i;
  2193. for (i = 0; i < EFX_MAX_CHANNELS; i++)
  2194. kfree(efx->channel[i]);
  2195. if (efx->workqueue) {
  2196. destroy_workqueue(efx->workqueue);
  2197. efx->workqueue = NULL;
  2198. }
  2199. }
  2200. /**************************************************************************
  2201. *
  2202. * PCI interface
  2203. *
  2204. **************************************************************************/
  2205. /* Main body of final NIC shutdown code
  2206. * This is called only at module unload (or hotplug removal).
  2207. */
  2208. static void efx_pci_remove_main(struct efx_nic *efx)
  2209. {
  2210. /* Flush reset_work. It can no longer be scheduled since we
  2211. * are not READY.
  2212. */
  2213. BUG_ON(efx->state == STATE_READY);
  2214. cancel_work_sync(&efx->reset_work);
  2215. efx_disable_interrupts(efx);
  2216. efx_nic_fini_interrupt(efx);
  2217. efx_fini_port(efx);
  2218. efx->type->fini(efx);
  2219. efx_fini_napi(efx);
  2220. efx_remove_all(efx);
  2221. }
  2222. /* Final NIC shutdown
  2223. * This is called only at module unload (or hotplug removal).
  2224. */
  2225. static void efx_pci_remove(struct pci_dev *pci_dev)
  2226. {
  2227. struct efx_nic *efx;
  2228. efx = pci_get_drvdata(pci_dev);
  2229. if (!efx)
  2230. return;
  2231. /* Mark the NIC as fini, then stop the interface */
  2232. rtnl_lock();
  2233. dev_close(efx->net_dev);
  2234. efx_disable_interrupts(efx);
  2235. rtnl_unlock();
  2236. efx_sriov_fini(efx);
  2237. efx_unregister_netdev(efx);
  2238. efx_mtd_remove(efx);
  2239. efx_pci_remove_main(efx);
  2240. efx_fini_io(efx);
  2241. netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
  2242. efx_fini_struct(efx);
  2243. pci_set_drvdata(pci_dev, NULL);
  2244. free_netdev(efx->net_dev);
  2245. pci_disable_pcie_error_reporting(pci_dev);
  2246. };
  2247. /* NIC VPD information
  2248. * Called during probe to display the part number of the
  2249. * installed NIC. VPD is potentially very large but this should
  2250. * always appear within the first 512 bytes.
  2251. */
  2252. #define SFC_VPD_LEN 512
  2253. static void efx_print_product_vpd(struct efx_nic *efx)
  2254. {
  2255. struct pci_dev *dev = efx->pci_dev;
  2256. char vpd_data[SFC_VPD_LEN];
  2257. ssize_t vpd_size;
  2258. int i, j;
  2259. /* Get the vpd data from the device */
  2260. vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
  2261. if (vpd_size <= 0) {
  2262. netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
  2263. return;
  2264. }
  2265. /* Get the Read only section */
  2266. i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
  2267. if (i < 0) {
  2268. netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
  2269. return;
  2270. }
  2271. j = pci_vpd_lrdt_size(&vpd_data[i]);
  2272. i += PCI_VPD_LRDT_TAG_SIZE;
  2273. if (i + j > vpd_size)
  2274. j = vpd_size - i;
  2275. /* Get the Part number */
  2276. i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
  2277. if (i < 0) {
  2278. netif_err(efx, drv, efx->net_dev, "Part number not found\n");
  2279. return;
  2280. }
  2281. j = pci_vpd_info_field_size(&vpd_data[i]);
  2282. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2283. if (i + j > vpd_size) {
  2284. netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
  2285. return;
  2286. }
  2287. netif_info(efx, drv, efx->net_dev,
  2288. "Part Number : %.*s\n", j, &vpd_data[i]);
  2289. }
  2290. /* Main body of NIC initialisation
  2291. * This is called at module load (or hotplug insertion, theoretically).
  2292. */
  2293. static int efx_pci_probe_main(struct efx_nic *efx)
  2294. {
  2295. int rc;
  2296. /* Do start-of-day initialisation */
  2297. rc = efx_probe_all(efx);
  2298. if (rc)
  2299. goto fail1;
  2300. efx_init_napi(efx);
  2301. rc = efx->type->init(efx);
  2302. if (rc) {
  2303. netif_err(efx, probe, efx->net_dev,
  2304. "failed to initialise NIC\n");
  2305. goto fail3;
  2306. }
  2307. rc = efx_init_port(efx);
  2308. if (rc) {
  2309. netif_err(efx, probe, efx->net_dev,
  2310. "failed to initialise port\n");
  2311. goto fail4;
  2312. }
  2313. rc = efx_nic_init_interrupt(efx);
  2314. if (rc)
  2315. goto fail5;
  2316. rc = efx_enable_interrupts(efx);
  2317. if (rc)
  2318. goto fail6;
  2319. return 0;
  2320. fail6:
  2321. efx_nic_fini_interrupt(efx);
  2322. fail5:
  2323. efx_fini_port(efx);
  2324. fail4:
  2325. efx->type->fini(efx);
  2326. fail3:
  2327. efx_fini_napi(efx);
  2328. efx_remove_all(efx);
  2329. fail1:
  2330. return rc;
  2331. }
  2332. /* NIC initialisation
  2333. *
  2334. * This is called at module load (or hotplug insertion,
  2335. * theoretically). It sets up PCI mappings, resets the NIC,
  2336. * sets up and registers the network devices with the kernel and hooks
  2337. * the interrupt service routine. It does not prepare the device for
  2338. * transmission; this is left to the first time one of the network
  2339. * interfaces is brought up (i.e. efx_net_open).
  2340. */
  2341. static int efx_pci_probe(struct pci_dev *pci_dev,
  2342. const struct pci_device_id *entry)
  2343. {
  2344. struct net_device *net_dev;
  2345. struct efx_nic *efx;
  2346. int rc;
  2347. /* Allocate and initialise a struct net_device and struct efx_nic */
  2348. net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
  2349. EFX_MAX_RX_QUEUES);
  2350. if (!net_dev)
  2351. return -ENOMEM;
  2352. efx = netdev_priv(net_dev);
  2353. efx->type = (const struct efx_nic_type *) entry->driver_data;
  2354. net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
  2355. NETIF_F_HIGHDMA | NETIF_F_TSO |
  2356. NETIF_F_RXCSUM);
  2357. if (efx->type->offload_features & NETIF_F_V6_CSUM)
  2358. net_dev->features |= NETIF_F_TSO6;
  2359. /* Mask for features that also apply to VLAN devices */
  2360. net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
  2361. NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
  2362. NETIF_F_RXCSUM);
  2363. /* All offloads can be toggled */
  2364. net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
  2365. pci_set_drvdata(pci_dev, efx);
  2366. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  2367. rc = efx_init_struct(efx, pci_dev, net_dev);
  2368. if (rc)
  2369. goto fail1;
  2370. netif_info(efx, probe, efx->net_dev,
  2371. "Solarflare NIC detected\n");
  2372. efx_print_product_vpd(efx);
  2373. /* Set up basic I/O (BAR mappings etc) */
  2374. rc = efx_init_io(efx);
  2375. if (rc)
  2376. goto fail2;
  2377. rc = efx_pci_probe_main(efx);
  2378. if (rc)
  2379. goto fail3;
  2380. rc = efx_register_netdev(efx);
  2381. if (rc)
  2382. goto fail4;
  2383. rc = efx_sriov_init(efx);
  2384. if (rc)
  2385. netif_err(efx, probe, efx->net_dev,
  2386. "SR-IOV can't be enabled rc %d\n", rc);
  2387. netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
  2388. /* Try to create MTDs, but allow this to fail */
  2389. rtnl_lock();
  2390. rc = efx_mtd_probe(efx);
  2391. rtnl_unlock();
  2392. if (rc)
  2393. netif_warn(efx, probe, efx->net_dev,
  2394. "failed to create MTDs (%d)\n", rc);
  2395. rc = pci_enable_pcie_error_reporting(pci_dev);
  2396. if (rc && rc != -EINVAL)
  2397. netif_warn(efx, probe, efx->net_dev,
  2398. "pci_enable_pcie_error_reporting failed (%d)\n", rc);
  2399. return 0;
  2400. fail4:
  2401. efx_pci_remove_main(efx);
  2402. fail3:
  2403. efx_fini_io(efx);
  2404. fail2:
  2405. efx_fini_struct(efx);
  2406. fail1:
  2407. pci_set_drvdata(pci_dev, NULL);
  2408. WARN_ON(rc > 0);
  2409. netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
  2410. free_netdev(net_dev);
  2411. return rc;
  2412. }
  2413. static int efx_pm_freeze(struct device *dev)
  2414. {
  2415. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2416. rtnl_lock();
  2417. if (efx->state != STATE_DISABLED) {
  2418. efx->state = STATE_UNINIT;
  2419. efx_device_detach_sync(efx);
  2420. efx_stop_all(efx);
  2421. efx_disable_interrupts(efx);
  2422. }
  2423. rtnl_unlock();
  2424. return 0;
  2425. }
  2426. static int efx_pm_thaw(struct device *dev)
  2427. {
  2428. int rc;
  2429. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2430. rtnl_lock();
  2431. if (efx->state != STATE_DISABLED) {
  2432. rc = efx_enable_interrupts(efx);
  2433. if (rc)
  2434. goto fail;
  2435. mutex_lock(&efx->mac_lock);
  2436. efx->phy_op->reconfigure(efx);
  2437. mutex_unlock(&efx->mac_lock);
  2438. efx_start_all(efx);
  2439. netif_device_attach(efx->net_dev);
  2440. efx->state = STATE_READY;
  2441. efx->type->resume_wol(efx);
  2442. }
  2443. rtnl_unlock();
  2444. /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
  2445. queue_work(reset_workqueue, &efx->reset_work);
  2446. return 0;
  2447. fail:
  2448. rtnl_unlock();
  2449. return rc;
  2450. }
  2451. static int efx_pm_poweroff(struct device *dev)
  2452. {
  2453. struct pci_dev *pci_dev = to_pci_dev(dev);
  2454. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2455. efx->type->fini(efx);
  2456. efx->reset_pending = 0;
  2457. pci_save_state(pci_dev);
  2458. return pci_set_power_state(pci_dev, PCI_D3hot);
  2459. }
  2460. /* Used for both resume and restore */
  2461. static int efx_pm_resume(struct device *dev)
  2462. {
  2463. struct pci_dev *pci_dev = to_pci_dev(dev);
  2464. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2465. int rc;
  2466. rc = pci_set_power_state(pci_dev, PCI_D0);
  2467. if (rc)
  2468. return rc;
  2469. pci_restore_state(pci_dev);
  2470. rc = pci_enable_device(pci_dev);
  2471. if (rc)
  2472. return rc;
  2473. pci_set_master(efx->pci_dev);
  2474. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  2475. if (rc)
  2476. return rc;
  2477. rc = efx->type->init(efx);
  2478. if (rc)
  2479. return rc;
  2480. rc = efx_pm_thaw(dev);
  2481. return rc;
  2482. }
  2483. static int efx_pm_suspend(struct device *dev)
  2484. {
  2485. int rc;
  2486. efx_pm_freeze(dev);
  2487. rc = efx_pm_poweroff(dev);
  2488. if (rc)
  2489. efx_pm_resume(dev);
  2490. return rc;
  2491. }
  2492. static const struct dev_pm_ops efx_pm_ops = {
  2493. .suspend = efx_pm_suspend,
  2494. .resume = efx_pm_resume,
  2495. .freeze = efx_pm_freeze,
  2496. .thaw = efx_pm_thaw,
  2497. .poweroff = efx_pm_poweroff,
  2498. .restore = efx_pm_resume,
  2499. };
  2500. /* A PCI error affecting this device was detected.
  2501. * At this point MMIO and DMA may be disabled.
  2502. * Stop the software path and request a slot reset.
  2503. */
  2504. static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
  2505. enum pci_channel_state state)
  2506. {
  2507. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2508. struct efx_nic *efx = pci_get_drvdata(pdev);
  2509. if (state == pci_channel_io_perm_failure)
  2510. return PCI_ERS_RESULT_DISCONNECT;
  2511. rtnl_lock();
  2512. if (efx->state != STATE_DISABLED) {
  2513. efx->state = STATE_RECOVERY;
  2514. efx->reset_pending = 0;
  2515. efx_device_detach_sync(efx);
  2516. efx_stop_all(efx);
  2517. efx_disable_interrupts(efx);
  2518. status = PCI_ERS_RESULT_NEED_RESET;
  2519. } else {
  2520. /* If the interface is disabled we don't want to do anything
  2521. * with it.
  2522. */
  2523. status = PCI_ERS_RESULT_RECOVERED;
  2524. }
  2525. rtnl_unlock();
  2526. pci_disable_device(pdev);
  2527. return status;
  2528. }
  2529. /* Fake a successfull reset, which will be performed later in efx_io_resume. */
  2530. static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
  2531. {
  2532. struct efx_nic *efx = pci_get_drvdata(pdev);
  2533. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2534. int rc;
  2535. if (pci_enable_device(pdev)) {
  2536. netif_err(efx, hw, efx->net_dev,
  2537. "Cannot re-enable PCI device after reset.\n");
  2538. status = PCI_ERS_RESULT_DISCONNECT;
  2539. }
  2540. rc = pci_cleanup_aer_uncorrect_error_status(pdev);
  2541. if (rc) {
  2542. netif_err(efx, hw, efx->net_dev,
  2543. "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
  2544. /* Non-fatal error. Continue. */
  2545. }
  2546. return status;
  2547. }
  2548. /* Perform the actual reset and resume I/O operations. */
  2549. static void efx_io_resume(struct pci_dev *pdev)
  2550. {
  2551. struct efx_nic *efx = pci_get_drvdata(pdev);
  2552. int rc;
  2553. rtnl_lock();
  2554. if (efx->state == STATE_DISABLED)
  2555. goto out;
  2556. rc = efx_reset(efx, RESET_TYPE_ALL);
  2557. if (rc) {
  2558. netif_err(efx, hw, efx->net_dev,
  2559. "efx_reset failed after PCI error (%d)\n", rc);
  2560. } else {
  2561. efx->state = STATE_READY;
  2562. netif_dbg(efx, hw, efx->net_dev,
  2563. "Done resetting and resuming IO after PCI error.\n");
  2564. }
  2565. out:
  2566. rtnl_unlock();
  2567. }
  2568. /* For simplicity and reliability, we always require a slot reset and try to
  2569. * reset the hardware when a pci error affecting the device is detected.
  2570. * We leave both the link_reset and mmio_enabled callback unimplemented:
  2571. * with our request for slot reset the mmio_enabled callback will never be
  2572. * called, and the link_reset callback is not used by AER or EEH mechanisms.
  2573. */
  2574. static struct pci_error_handlers efx_err_handlers = {
  2575. .error_detected = efx_io_error_detected,
  2576. .slot_reset = efx_io_slot_reset,
  2577. .resume = efx_io_resume,
  2578. };
  2579. static struct pci_driver efx_pci_driver = {
  2580. .name = KBUILD_MODNAME,
  2581. .id_table = efx_pci_table,
  2582. .probe = efx_pci_probe,
  2583. .remove = efx_pci_remove,
  2584. .driver.pm = &efx_pm_ops,
  2585. .err_handler = &efx_err_handlers,
  2586. };
  2587. /**************************************************************************
  2588. *
  2589. * Kernel module interface
  2590. *
  2591. *************************************************************************/
  2592. module_param(interrupt_mode, uint, 0444);
  2593. MODULE_PARM_DESC(interrupt_mode,
  2594. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  2595. static int __init efx_init_module(void)
  2596. {
  2597. int rc;
  2598. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  2599. rc = register_netdevice_notifier(&efx_netdev_notifier);
  2600. if (rc)
  2601. goto err_notifier;
  2602. rc = efx_init_sriov();
  2603. if (rc)
  2604. goto err_sriov;
  2605. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  2606. if (!reset_workqueue) {
  2607. rc = -ENOMEM;
  2608. goto err_reset;
  2609. }
  2610. rc = pci_register_driver(&efx_pci_driver);
  2611. if (rc < 0)
  2612. goto err_pci;
  2613. return 0;
  2614. err_pci:
  2615. destroy_workqueue(reset_workqueue);
  2616. err_reset:
  2617. efx_fini_sriov();
  2618. err_sriov:
  2619. unregister_netdevice_notifier(&efx_netdev_notifier);
  2620. err_notifier:
  2621. return rc;
  2622. }
  2623. static void __exit efx_exit_module(void)
  2624. {
  2625. printk(KERN_INFO "Solarflare NET driver unloading\n");
  2626. pci_unregister_driver(&efx_pci_driver);
  2627. destroy_workqueue(reset_workqueue);
  2628. efx_fini_sriov();
  2629. unregister_netdevice_notifier(&efx_netdev_notifier);
  2630. }
  2631. module_init(efx_init_module);
  2632. module_exit(efx_exit_module);
  2633. MODULE_AUTHOR("Solarflare Communications and "
  2634. "Michael Brown <mbrown@fensystems.co.uk>");
  2635. MODULE_DESCRIPTION("Solarflare Communications network driver");
  2636. MODULE_LICENSE("GPL");
  2637. MODULE_DEVICE_TABLE(pci, efx_pci_table);