efx.c 56 KB

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