efx.c 59 KB

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