efx.c 64 KB

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