efx.c 73 KB

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