efx.c 75 KB

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