efx.c 59 KB

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