efx.c 70 KB

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