efx.c 57 KB

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