efx.c 61 KB

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