efx.c 64 KB

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