efx.c 57 KB

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