efx.c 78 KB

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