efx.c 60 KB

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