efx.c 60 KB

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