efx.c 56 KB

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