efx.c 69 KB

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