efx.c 65 KB

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