efx.c 69 KB

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