efx.c 74 KB

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