efx.c 60 KB

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