efx.c 76 KB

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