efx.c 81 KB

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