efx.c 84 KB

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