efx.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2008 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/delay.h>
  15. #include <linux/notifier.h>
  16. #include <linux/ip.h>
  17. #include <linux/tcp.h>
  18. #include <linux/in.h>
  19. #include <linux/crc32.h>
  20. #include <linux/ethtool.h>
  21. #include <linux/topology.h>
  22. #include "net_driver.h"
  23. #include "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. 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 work item for changing MAC promiscuity and multicast
  562. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  563. * MAC directly. */
  564. static void efx_mac_work(struct work_struct *data)
  565. {
  566. struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
  567. mutex_lock(&efx->mac_lock);
  568. if (efx->port_enabled) {
  569. falcon_push_multicast_hash(efx);
  570. efx->mac_op->reconfigure(efx);
  571. }
  572. mutex_unlock(&efx->mac_lock);
  573. }
  574. static int efx_probe_port(struct efx_nic *efx)
  575. {
  576. int rc;
  577. EFX_LOG(efx, "create port\n");
  578. /* Connect up MAC/PHY operations table and read MAC address */
  579. rc = falcon_probe_port(efx);
  580. if (rc)
  581. goto err;
  582. if (phy_flash_cfg)
  583. efx->phy_mode = PHY_MODE_SPECIAL;
  584. /* Sanity check MAC address */
  585. if (is_valid_ether_addr(efx->mac_address)) {
  586. memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
  587. } else {
  588. EFX_ERR(efx, "invalid MAC address %pM\n",
  589. efx->mac_address);
  590. if (!allow_bad_hwaddr) {
  591. rc = -EINVAL;
  592. goto err;
  593. }
  594. random_ether_addr(efx->net_dev->dev_addr);
  595. EFX_INFO(efx, "using locally-generated MAC %pM\n",
  596. efx->net_dev->dev_addr);
  597. }
  598. return 0;
  599. err:
  600. efx_remove_port(efx);
  601. return rc;
  602. }
  603. static int efx_init_port(struct efx_nic *efx)
  604. {
  605. int rc;
  606. EFX_LOG(efx, "init port\n");
  607. mutex_lock(&efx->mac_lock);
  608. rc = efx->phy_op->init(efx);
  609. if (rc)
  610. goto fail1;
  611. efx->phy_op->reconfigure(efx);
  612. rc = falcon_switch_mac(efx);
  613. if (rc)
  614. goto fail2;
  615. efx->mac_op->reconfigure(efx);
  616. efx->port_initialized = true;
  617. mutex_unlock(&efx->mac_lock);
  618. return 0;
  619. fail2:
  620. efx->phy_op->fini(efx);
  621. fail1:
  622. mutex_unlock(&efx->mac_lock);
  623. return rc;
  624. }
  625. static void efx_start_port(struct efx_nic *efx)
  626. {
  627. EFX_LOG(efx, "start port\n");
  628. BUG_ON(efx->port_enabled);
  629. mutex_lock(&efx->mac_lock);
  630. efx->port_enabled = true;
  631. /* efx_mac_work() might have been scheduled after efx_stop_port(),
  632. * and then cancelled by efx_flush_all() */
  633. falcon_push_multicast_hash(efx);
  634. efx->mac_op->reconfigure(efx);
  635. mutex_unlock(&efx->mac_lock);
  636. }
  637. /* Prevent efx_mac_work() and efx_monitor() from working */
  638. static void efx_stop_port(struct efx_nic *efx)
  639. {
  640. EFX_LOG(efx, "stop port\n");
  641. mutex_lock(&efx->mac_lock);
  642. efx->port_enabled = false;
  643. mutex_unlock(&efx->mac_lock);
  644. /* Serialise against efx_set_multicast_list() */
  645. if (efx_dev_registered(efx)) {
  646. netif_addr_lock_bh(efx->net_dev);
  647. netif_addr_unlock_bh(efx->net_dev);
  648. }
  649. }
  650. static void efx_fini_port(struct efx_nic *efx)
  651. {
  652. EFX_LOG(efx, "shut down port\n");
  653. if (!efx->port_initialized)
  654. return;
  655. efx->phy_op->fini(efx);
  656. efx->port_initialized = false;
  657. efx->link_state.up = false;
  658. efx_link_status_changed(efx);
  659. }
  660. static void efx_remove_port(struct efx_nic *efx)
  661. {
  662. EFX_LOG(efx, "destroying port\n");
  663. falcon_remove_port(efx);
  664. }
  665. /**************************************************************************
  666. *
  667. * NIC handling
  668. *
  669. **************************************************************************/
  670. /* This configures the PCI device to enable I/O and DMA. */
  671. static int efx_init_io(struct efx_nic *efx)
  672. {
  673. struct pci_dev *pci_dev = efx->pci_dev;
  674. dma_addr_t dma_mask = efx->type->max_dma_mask;
  675. int rc;
  676. EFX_LOG(efx, "initialising I/O\n");
  677. rc = pci_enable_device(pci_dev);
  678. if (rc) {
  679. EFX_ERR(efx, "failed to enable PCI device\n");
  680. goto fail1;
  681. }
  682. pci_set_master(pci_dev);
  683. /* Set the PCI DMA mask. Try all possibilities from our
  684. * genuine mask down to 32 bits, because some architectures
  685. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  686. * masks event though they reject 46 bit masks.
  687. */
  688. while (dma_mask > 0x7fffffffUL) {
  689. if (pci_dma_supported(pci_dev, dma_mask) &&
  690. ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
  691. break;
  692. dma_mask >>= 1;
  693. }
  694. if (rc) {
  695. EFX_ERR(efx, "could not find a suitable DMA mask\n");
  696. goto fail2;
  697. }
  698. EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
  699. rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
  700. if (rc) {
  701. /* pci_set_consistent_dma_mask() is not *allowed* to
  702. * fail with a mask that pci_set_dma_mask() accepted,
  703. * but just in case...
  704. */
  705. EFX_ERR(efx, "failed to set consistent DMA mask\n");
  706. goto fail2;
  707. }
  708. efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
  709. rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
  710. if (rc) {
  711. EFX_ERR(efx, "request for memory BAR failed\n");
  712. rc = -EIO;
  713. goto fail3;
  714. }
  715. efx->membase = ioremap_nocache(efx->membase_phys,
  716. efx->type->mem_map_size);
  717. if (!efx->membase) {
  718. EFX_ERR(efx, "could not map memory BAR at %llx+%x\n",
  719. (unsigned long long)efx->membase_phys,
  720. efx->type->mem_map_size);
  721. rc = -ENOMEM;
  722. goto fail4;
  723. }
  724. EFX_LOG(efx, "memory BAR at %llx+%x (virtual %p)\n",
  725. (unsigned long long)efx->membase_phys,
  726. efx->type->mem_map_size, efx->membase);
  727. return 0;
  728. fail4:
  729. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  730. fail3:
  731. efx->membase_phys = 0;
  732. fail2:
  733. pci_disable_device(efx->pci_dev);
  734. fail1:
  735. return rc;
  736. }
  737. static void efx_fini_io(struct efx_nic *efx)
  738. {
  739. EFX_LOG(efx, "shutting down I/O\n");
  740. if (efx->membase) {
  741. iounmap(efx->membase);
  742. efx->membase = NULL;
  743. }
  744. if (efx->membase_phys) {
  745. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  746. efx->membase_phys = 0;
  747. }
  748. pci_disable_device(efx->pci_dev);
  749. }
  750. /* Get number of RX queues wanted. Return number of online CPU
  751. * packages in the expectation that an IRQ balancer will spread
  752. * interrupts across them. */
  753. static int efx_wanted_rx_queues(void)
  754. {
  755. cpumask_var_t core_mask;
  756. int count;
  757. int cpu;
  758. if (unlikely(!zalloc_cpumask_var(&core_mask, GFP_KERNEL))) {
  759. printk(KERN_WARNING
  760. "sfc: RSS disabled due to allocation failure\n");
  761. return 1;
  762. }
  763. count = 0;
  764. for_each_online_cpu(cpu) {
  765. if (!cpumask_test_cpu(cpu, core_mask)) {
  766. ++count;
  767. cpumask_or(core_mask, core_mask,
  768. topology_core_cpumask(cpu));
  769. }
  770. }
  771. free_cpumask_var(core_mask);
  772. return count;
  773. }
  774. /* Probe the number and type of interrupts we are able to obtain, and
  775. * the resulting numbers of channels and RX queues.
  776. */
  777. static void efx_probe_interrupts(struct efx_nic *efx)
  778. {
  779. int max_channels =
  780. min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
  781. int rc, i;
  782. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  783. struct msix_entry xentries[EFX_MAX_CHANNELS];
  784. int wanted_ints;
  785. int rx_queues;
  786. /* We want one RX queue and interrupt per CPU package
  787. * (or as specified by the rss_cpus module parameter).
  788. * We will need one channel per interrupt.
  789. */
  790. rx_queues = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
  791. wanted_ints = rx_queues + (separate_tx_channels ? 1 : 0);
  792. wanted_ints = min(wanted_ints, max_channels);
  793. for (i = 0; i < wanted_ints; i++)
  794. xentries[i].entry = i;
  795. rc = pci_enable_msix(efx->pci_dev, xentries, wanted_ints);
  796. if (rc > 0) {
  797. EFX_ERR(efx, "WARNING: Insufficient MSI-X vectors"
  798. " available (%d < %d).\n", rc, wanted_ints);
  799. EFX_ERR(efx, "WARNING: Performance may be reduced.\n");
  800. EFX_BUG_ON_PARANOID(rc >= wanted_ints);
  801. wanted_ints = rc;
  802. rc = pci_enable_msix(efx->pci_dev, xentries,
  803. wanted_ints);
  804. }
  805. if (rc == 0) {
  806. efx->n_rx_queues = min(rx_queues, wanted_ints);
  807. efx->n_channels = wanted_ints;
  808. for (i = 0; i < wanted_ints; i++)
  809. efx->channel[i].irq = xentries[i].vector;
  810. } else {
  811. /* Fall back to single channel MSI */
  812. efx->interrupt_mode = EFX_INT_MODE_MSI;
  813. EFX_ERR(efx, "could not enable MSI-X\n");
  814. }
  815. }
  816. /* Try single interrupt MSI */
  817. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  818. efx->n_rx_queues = 1;
  819. efx->n_channels = 1;
  820. rc = pci_enable_msi(efx->pci_dev);
  821. if (rc == 0) {
  822. efx->channel[0].irq = efx->pci_dev->irq;
  823. } else {
  824. EFX_ERR(efx, "could not enable MSI\n");
  825. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  826. }
  827. }
  828. /* Assume legacy interrupts */
  829. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  830. efx->n_rx_queues = 1;
  831. efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
  832. efx->legacy_irq = efx->pci_dev->irq;
  833. }
  834. }
  835. static void efx_remove_interrupts(struct efx_nic *efx)
  836. {
  837. struct efx_channel *channel;
  838. /* Remove MSI/MSI-X interrupts */
  839. efx_for_each_channel(channel, efx)
  840. channel->irq = 0;
  841. pci_disable_msi(efx->pci_dev);
  842. pci_disable_msix(efx->pci_dev);
  843. /* Remove legacy interrupt */
  844. efx->legacy_irq = 0;
  845. }
  846. static void efx_set_channels(struct efx_nic *efx)
  847. {
  848. struct efx_tx_queue *tx_queue;
  849. struct efx_rx_queue *rx_queue;
  850. efx_for_each_tx_queue(tx_queue, efx) {
  851. if (separate_tx_channels)
  852. tx_queue->channel = &efx->channel[efx->n_channels-1];
  853. else
  854. tx_queue->channel = &efx->channel[0];
  855. tx_queue->channel->used_flags |= EFX_USED_BY_TX;
  856. }
  857. efx_for_each_rx_queue(rx_queue, efx) {
  858. rx_queue->channel = &efx->channel[rx_queue->queue];
  859. rx_queue->channel->used_flags |= EFX_USED_BY_RX;
  860. }
  861. }
  862. static int efx_probe_nic(struct efx_nic *efx)
  863. {
  864. int rc;
  865. EFX_LOG(efx, "creating NIC\n");
  866. /* Carry out hardware-type specific initialisation */
  867. rc = falcon_probe_nic(efx);
  868. if (rc)
  869. return rc;
  870. /* Determine the number of channels and RX queues by trying to hook
  871. * in MSI-X interrupts. */
  872. efx_probe_interrupts(efx);
  873. efx_set_channels(efx);
  874. /* Initialise the interrupt moderation settings */
  875. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true);
  876. return 0;
  877. }
  878. static void efx_remove_nic(struct efx_nic *efx)
  879. {
  880. EFX_LOG(efx, "destroying NIC\n");
  881. efx_remove_interrupts(efx);
  882. falcon_remove_nic(efx);
  883. }
  884. /**************************************************************************
  885. *
  886. * NIC startup/shutdown
  887. *
  888. *************************************************************************/
  889. static int efx_probe_all(struct efx_nic *efx)
  890. {
  891. struct efx_channel *channel;
  892. int rc;
  893. /* Create NIC */
  894. rc = efx_probe_nic(efx);
  895. if (rc) {
  896. EFX_ERR(efx, "failed to create NIC\n");
  897. goto fail1;
  898. }
  899. /* Create port */
  900. rc = efx_probe_port(efx);
  901. if (rc) {
  902. EFX_ERR(efx, "failed to create port\n");
  903. goto fail2;
  904. }
  905. /* Create channels */
  906. efx_for_each_channel(channel, efx) {
  907. rc = efx_probe_channel(channel);
  908. if (rc) {
  909. EFX_ERR(efx, "failed to create channel %d\n",
  910. channel->channel);
  911. goto fail3;
  912. }
  913. }
  914. efx_set_channel_names(efx);
  915. return 0;
  916. fail3:
  917. efx_for_each_channel(channel, efx)
  918. efx_remove_channel(channel);
  919. efx_remove_port(efx);
  920. fail2:
  921. efx_remove_nic(efx);
  922. fail1:
  923. return rc;
  924. }
  925. /* Called after previous invocation(s) of efx_stop_all, restarts the
  926. * port, kernel transmit queue, NAPI processing and hardware interrupts,
  927. * and ensures that the port is scheduled to be reconfigured.
  928. * This function is safe to call multiple times when the NIC is in any
  929. * state. */
  930. static void efx_start_all(struct efx_nic *efx)
  931. {
  932. struct efx_channel *channel;
  933. EFX_ASSERT_RESET_SERIALISED(efx);
  934. /* Check that it is appropriate to restart the interface. All
  935. * of these flags are safe to read under just the rtnl lock */
  936. if (efx->port_enabled)
  937. return;
  938. if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
  939. return;
  940. if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
  941. return;
  942. /* Mark the port as enabled so port reconfigurations can start, then
  943. * restart the transmit interface early so the watchdog timer stops */
  944. efx_start_port(efx);
  945. if (efx_dev_registered(efx))
  946. efx_wake_queue(efx);
  947. efx_for_each_channel(channel, efx)
  948. efx_start_channel(channel);
  949. falcon_enable_interrupts(efx);
  950. /* Start hardware monitor if we're in RUNNING */
  951. if (efx->state == STATE_RUNNING)
  952. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  953. efx_monitor_interval);
  954. falcon_start_nic_stats(efx);
  955. }
  956. /* Flush all delayed work. Should only be called when no more delayed work
  957. * will be scheduled. This doesn't flush pending online resets (efx_reset),
  958. * since we're holding the rtnl_lock at this point. */
  959. static void efx_flush_all(struct efx_nic *efx)
  960. {
  961. struct efx_rx_queue *rx_queue;
  962. /* Make sure the hardware monitor is stopped */
  963. cancel_delayed_work_sync(&efx->monitor_work);
  964. /* Ensure that all RX slow refills are complete. */
  965. efx_for_each_rx_queue(rx_queue, efx)
  966. cancel_delayed_work_sync(&rx_queue->work);
  967. /* Stop scheduled port reconfigurations */
  968. cancel_work_sync(&efx->mac_work);
  969. }
  970. /* Quiesce hardware and software without bringing the link down.
  971. * Safe to call multiple times, when the nic and interface is in any
  972. * state. The caller is guaranteed to subsequently be in a position
  973. * to modify any hardware and software state they see fit without
  974. * taking locks. */
  975. static void efx_stop_all(struct efx_nic *efx)
  976. {
  977. struct efx_channel *channel;
  978. EFX_ASSERT_RESET_SERIALISED(efx);
  979. /* port_enabled can be read safely under the rtnl lock */
  980. if (!efx->port_enabled)
  981. return;
  982. falcon_stop_nic_stats(efx);
  983. /* Disable interrupts and wait for ISR to complete */
  984. falcon_disable_interrupts(efx);
  985. if (efx->legacy_irq)
  986. synchronize_irq(efx->legacy_irq);
  987. efx_for_each_channel(channel, efx) {
  988. if (channel->irq)
  989. synchronize_irq(channel->irq);
  990. }
  991. /* Stop all NAPI processing and synchronous rx refills */
  992. efx_for_each_channel(channel, efx)
  993. efx_stop_channel(channel);
  994. /* Stop all asynchronous port reconfigurations. Since all
  995. * event processing has already been stopped, there is no
  996. * window to loose phy events */
  997. efx_stop_port(efx);
  998. /* Flush efx_mac_work(), refill_workqueue, monitor_work */
  999. efx_flush_all(efx);
  1000. /* Isolate the MAC from the TX and RX engines, so that queue
  1001. * flushes will complete in a timely fashion. */
  1002. falcon_deconfigure_mac_wrapper(efx);
  1003. msleep(10); /* Let the Rx FIFO drain */
  1004. falcon_drain_tx_fifo(efx);
  1005. /* Stop the kernel transmit interface late, so the watchdog
  1006. * timer isn't ticking over the flush */
  1007. if (efx_dev_registered(efx)) {
  1008. efx_stop_queue(efx);
  1009. netif_tx_lock_bh(efx->net_dev);
  1010. netif_tx_unlock_bh(efx->net_dev);
  1011. }
  1012. }
  1013. static void efx_remove_all(struct efx_nic *efx)
  1014. {
  1015. struct efx_channel *channel;
  1016. efx_for_each_channel(channel, efx)
  1017. efx_remove_channel(channel);
  1018. efx_remove_port(efx);
  1019. efx_remove_nic(efx);
  1020. }
  1021. /**************************************************************************
  1022. *
  1023. * Interrupt moderation
  1024. *
  1025. **************************************************************************/
  1026. static unsigned irq_mod_ticks(int usecs, int resolution)
  1027. {
  1028. if (usecs <= 0)
  1029. return 0; /* cannot receive interrupts ahead of time :-) */
  1030. if (usecs < resolution)
  1031. return 1; /* never round down to 0 */
  1032. return usecs / resolution;
  1033. }
  1034. /* Set interrupt moderation parameters */
  1035. void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs,
  1036. bool rx_adaptive)
  1037. {
  1038. struct efx_tx_queue *tx_queue;
  1039. struct efx_rx_queue *rx_queue;
  1040. unsigned tx_ticks = irq_mod_ticks(tx_usecs, FALCON_IRQ_MOD_RESOLUTION);
  1041. unsigned rx_ticks = irq_mod_ticks(rx_usecs, FALCON_IRQ_MOD_RESOLUTION);
  1042. EFX_ASSERT_RESET_SERIALISED(efx);
  1043. efx_for_each_tx_queue(tx_queue, efx)
  1044. tx_queue->channel->irq_moderation = tx_ticks;
  1045. efx->irq_rx_adaptive = rx_adaptive;
  1046. efx->irq_rx_moderation = rx_ticks;
  1047. efx_for_each_rx_queue(rx_queue, efx)
  1048. rx_queue->channel->irq_moderation = rx_ticks;
  1049. }
  1050. /**************************************************************************
  1051. *
  1052. * Hardware monitor
  1053. *
  1054. **************************************************************************/
  1055. /* Run periodically off the general workqueue. Serialised against
  1056. * efx_reconfigure_port via the mac_lock */
  1057. static void efx_monitor(struct work_struct *data)
  1058. {
  1059. struct efx_nic *efx = container_of(data, struct efx_nic,
  1060. monitor_work.work);
  1061. EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
  1062. raw_smp_processor_id());
  1063. /* If the mac_lock is already held then it is likely a port
  1064. * reconfiguration is already in place, which will likely do
  1065. * most of the work of check_hw() anyway. */
  1066. if (!mutex_trylock(&efx->mac_lock))
  1067. goto out_requeue;
  1068. if (!efx->port_enabled)
  1069. goto out_unlock;
  1070. falcon_monitor(efx);
  1071. out_unlock:
  1072. mutex_unlock(&efx->mac_lock);
  1073. out_requeue:
  1074. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1075. efx_monitor_interval);
  1076. }
  1077. /**************************************************************************
  1078. *
  1079. * ioctls
  1080. *
  1081. *************************************************************************/
  1082. /* Net device ioctl
  1083. * Context: process, rtnl_lock() held.
  1084. */
  1085. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1086. {
  1087. struct efx_nic *efx = netdev_priv(net_dev);
  1088. struct mii_ioctl_data *data = if_mii(ifr);
  1089. EFX_ASSERT_RESET_SERIALISED(efx);
  1090. /* Convert phy_id from older PRTAD/DEVAD format */
  1091. if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
  1092. (data->phy_id & 0xfc00) == 0x0400)
  1093. data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
  1094. return mdio_mii_ioctl(&efx->mdio, data, cmd);
  1095. }
  1096. /**************************************************************************
  1097. *
  1098. * NAPI interface
  1099. *
  1100. **************************************************************************/
  1101. static int efx_init_napi(struct efx_nic *efx)
  1102. {
  1103. struct efx_channel *channel;
  1104. efx_for_each_channel(channel, efx) {
  1105. channel->napi_dev = efx->net_dev;
  1106. netif_napi_add(channel->napi_dev, &channel->napi_str,
  1107. efx_poll, napi_weight);
  1108. }
  1109. return 0;
  1110. }
  1111. static void efx_fini_napi(struct efx_nic *efx)
  1112. {
  1113. struct efx_channel *channel;
  1114. efx_for_each_channel(channel, efx) {
  1115. if (channel->napi_dev)
  1116. netif_napi_del(&channel->napi_str);
  1117. channel->napi_dev = NULL;
  1118. }
  1119. }
  1120. /**************************************************************************
  1121. *
  1122. * Kernel netpoll interface
  1123. *
  1124. *************************************************************************/
  1125. #ifdef CONFIG_NET_POLL_CONTROLLER
  1126. /* Although in the common case interrupts will be disabled, this is not
  1127. * guaranteed. However, all our work happens inside the NAPI callback,
  1128. * so no locking is required.
  1129. */
  1130. static void efx_netpoll(struct net_device *net_dev)
  1131. {
  1132. struct efx_nic *efx = netdev_priv(net_dev);
  1133. struct efx_channel *channel;
  1134. efx_for_each_channel(channel, efx)
  1135. efx_schedule_channel(channel);
  1136. }
  1137. #endif
  1138. /**************************************************************************
  1139. *
  1140. * Kernel net device interface
  1141. *
  1142. *************************************************************************/
  1143. /* Context: process, rtnl_lock() held. */
  1144. static int efx_net_open(struct net_device *net_dev)
  1145. {
  1146. struct efx_nic *efx = netdev_priv(net_dev);
  1147. EFX_ASSERT_RESET_SERIALISED(efx);
  1148. EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
  1149. raw_smp_processor_id());
  1150. if (efx->state == STATE_DISABLED)
  1151. return -EIO;
  1152. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1153. return -EBUSY;
  1154. efx_start_all(efx);
  1155. return 0;
  1156. }
  1157. /* Context: process, rtnl_lock() held.
  1158. * Note that the kernel will ignore our return code; this method
  1159. * should really be a void.
  1160. */
  1161. static int efx_net_stop(struct net_device *net_dev)
  1162. {
  1163. struct efx_nic *efx = netdev_priv(net_dev);
  1164. EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
  1165. raw_smp_processor_id());
  1166. if (efx->state != STATE_DISABLED) {
  1167. /* Stop the device and flush all the channels */
  1168. efx_stop_all(efx);
  1169. efx_fini_channels(efx);
  1170. efx_init_channels(efx);
  1171. }
  1172. return 0;
  1173. }
  1174. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1175. static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
  1176. {
  1177. struct efx_nic *efx = netdev_priv(net_dev);
  1178. struct efx_mac_stats *mac_stats = &efx->mac_stats;
  1179. struct net_device_stats *stats = &net_dev->stats;
  1180. spin_lock_bh(&efx->stats_lock);
  1181. falcon_update_nic_stats(efx);
  1182. spin_unlock_bh(&efx->stats_lock);
  1183. stats->rx_packets = mac_stats->rx_packets;
  1184. stats->tx_packets = mac_stats->tx_packets;
  1185. stats->rx_bytes = mac_stats->rx_bytes;
  1186. stats->tx_bytes = mac_stats->tx_bytes;
  1187. stats->multicast = mac_stats->rx_multicast;
  1188. stats->collisions = mac_stats->tx_collision;
  1189. stats->rx_length_errors = (mac_stats->rx_gtjumbo +
  1190. mac_stats->rx_length_error);
  1191. stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
  1192. stats->rx_crc_errors = mac_stats->rx_bad;
  1193. stats->rx_frame_errors = mac_stats->rx_align_error;
  1194. stats->rx_fifo_errors = mac_stats->rx_overflow;
  1195. stats->rx_missed_errors = mac_stats->rx_missed;
  1196. stats->tx_window_errors = mac_stats->tx_late_collision;
  1197. stats->rx_errors = (stats->rx_length_errors +
  1198. stats->rx_over_errors +
  1199. stats->rx_crc_errors +
  1200. stats->rx_frame_errors +
  1201. stats->rx_fifo_errors +
  1202. stats->rx_missed_errors +
  1203. mac_stats->rx_symbol_error);
  1204. stats->tx_errors = (stats->tx_window_errors +
  1205. mac_stats->tx_bad);
  1206. return stats;
  1207. }
  1208. /* Context: netif_tx_lock held, BHs disabled. */
  1209. static void efx_watchdog(struct net_device *net_dev)
  1210. {
  1211. struct efx_nic *efx = netdev_priv(net_dev);
  1212. EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d:"
  1213. " resetting channels\n",
  1214. atomic_read(&efx->netif_stop_count), efx->port_enabled);
  1215. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1216. }
  1217. /* Context: process, rtnl_lock() held. */
  1218. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1219. {
  1220. struct efx_nic *efx = netdev_priv(net_dev);
  1221. int rc = 0;
  1222. EFX_ASSERT_RESET_SERIALISED(efx);
  1223. if (new_mtu > EFX_MAX_MTU)
  1224. return -EINVAL;
  1225. efx_stop_all(efx);
  1226. EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
  1227. efx_fini_channels(efx);
  1228. net_dev->mtu = new_mtu;
  1229. efx_init_channels(efx);
  1230. efx_start_all(efx);
  1231. return rc;
  1232. }
  1233. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1234. {
  1235. struct efx_nic *efx = netdev_priv(net_dev);
  1236. struct sockaddr *addr = data;
  1237. char *new_addr = addr->sa_data;
  1238. EFX_ASSERT_RESET_SERIALISED(efx);
  1239. if (!is_valid_ether_addr(new_addr)) {
  1240. EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
  1241. new_addr);
  1242. return -EINVAL;
  1243. }
  1244. memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
  1245. /* Reconfigure the MAC */
  1246. efx_reconfigure_port(efx);
  1247. return 0;
  1248. }
  1249. /* Context: netif_addr_lock held, BHs disabled. */
  1250. static void efx_set_multicast_list(struct net_device *net_dev)
  1251. {
  1252. struct efx_nic *efx = netdev_priv(net_dev);
  1253. struct dev_mc_list *mc_list = net_dev->mc_list;
  1254. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  1255. u32 crc;
  1256. int bit;
  1257. int i;
  1258. efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
  1259. /* Build multicast hash table */
  1260. if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
  1261. memset(mc_hash, 0xff, sizeof(*mc_hash));
  1262. } else {
  1263. memset(mc_hash, 0x00, sizeof(*mc_hash));
  1264. for (i = 0; i < net_dev->mc_count; i++) {
  1265. crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
  1266. bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
  1267. set_bit_le(bit, mc_hash->byte);
  1268. mc_list = mc_list->next;
  1269. }
  1270. /* Broadcast packets go through the multicast hash filter.
  1271. * ether_crc_le() of the broadcast address is 0xbe2612ff
  1272. * so we always add bit 0xff to the mask.
  1273. */
  1274. set_bit_le(0xff, mc_hash->byte);
  1275. }
  1276. if (efx->port_enabled)
  1277. queue_work(efx->workqueue, &efx->mac_work);
  1278. /* Otherwise efx_start_port() will do this */
  1279. }
  1280. static const struct net_device_ops efx_netdev_ops = {
  1281. .ndo_open = efx_net_open,
  1282. .ndo_stop = efx_net_stop,
  1283. .ndo_get_stats = efx_net_stats,
  1284. .ndo_tx_timeout = efx_watchdog,
  1285. .ndo_start_xmit = efx_hard_start_xmit,
  1286. .ndo_validate_addr = eth_validate_addr,
  1287. .ndo_do_ioctl = efx_ioctl,
  1288. .ndo_change_mtu = efx_change_mtu,
  1289. .ndo_set_mac_address = efx_set_mac_address,
  1290. .ndo_set_multicast_list = efx_set_multicast_list,
  1291. #ifdef CONFIG_NET_POLL_CONTROLLER
  1292. .ndo_poll_controller = efx_netpoll,
  1293. #endif
  1294. };
  1295. static void efx_update_name(struct efx_nic *efx)
  1296. {
  1297. strcpy(efx->name, efx->net_dev->name);
  1298. efx_mtd_rename(efx);
  1299. efx_set_channel_names(efx);
  1300. }
  1301. static int efx_netdev_event(struct notifier_block *this,
  1302. unsigned long event, void *ptr)
  1303. {
  1304. struct net_device *net_dev = ptr;
  1305. if (net_dev->netdev_ops == &efx_netdev_ops &&
  1306. event == NETDEV_CHANGENAME)
  1307. efx_update_name(netdev_priv(net_dev));
  1308. return NOTIFY_DONE;
  1309. }
  1310. static struct notifier_block efx_netdev_notifier = {
  1311. .notifier_call = efx_netdev_event,
  1312. };
  1313. static ssize_t
  1314. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  1315. {
  1316. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1317. return sprintf(buf, "%d\n", efx->phy_type);
  1318. }
  1319. static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
  1320. static int efx_register_netdev(struct efx_nic *efx)
  1321. {
  1322. struct net_device *net_dev = efx->net_dev;
  1323. int rc;
  1324. net_dev->watchdog_timeo = 5 * HZ;
  1325. net_dev->irq = efx->pci_dev->irq;
  1326. net_dev->netdev_ops = &efx_netdev_ops;
  1327. SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
  1328. SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
  1329. /* Clear MAC statistics */
  1330. efx->mac_op->update_stats(efx);
  1331. memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
  1332. rtnl_lock();
  1333. rc = dev_alloc_name(net_dev, net_dev->name);
  1334. if (rc < 0)
  1335. goto fail_locked;
  1336. efx_update_name(efx);
  1337. rc = register_netdevice(net_dev);
  1338. if (rc)
  1339. goto fail_locked;
  1340. /* Always start with carrier off; PHY events will detect the link */
  1341. netif_carrier_off(efx->net_dev);
  1342. rtnl_unlock();
  1343. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1344. if (rc) {
  1345. EFX_ERR(efx, "failed to init net dev attributes\n");
  1346. goto fail_registered;
  1347. }
  1348. return 0;
  1349. fail_locked:
  1350. rtnl_unlock();
  1351. EFX_ERR(efx, "could not register net dev\n");
  1352. return rc;
  1353. fail_registered:
  1354. unregister_netdev(net_dev);
  1355. return rc;
  1356. }
  1357. static void efx_unregister_netdev(struct efx_nic *efx)
  1358. {
  1359. struct efx_tx_queue *tx_queue;
  1360. if (!efx->net_dev)
  1361. return;
  1362. BUG_ON(netdev_priv(efx->net_dev) != efx);
  1363. /* Free up any skbs still remaining. This has to happen before
  1364. * we try to unregister the netdev as running their destructors
  1365. * may be needed to get the device ref. count to 0. */
  1366. efx_for_each_tx_queue(tx_queue, efx)
  1367. efx_release_tx_buffers(tx_queue);
  1368. if (efx_dev_registered(efx)) {
  1369. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  1370. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1371. unregister_netdev(efx->net_dev);
  1372. }
  1373. }
  1374. /**************************************************************************
  1375. *
  1376. * Device reset and suspend
  1377. *
  1378. **************************************************************************/
  1379. /* Tears down the entire software state and most of the hardware state
  1380. * before reset. */
  1381. void efx_reset_down(struct efx_nic *efx, enum reset_type method,
  1382. struct ethtool_cmd *ecmd)
  1383. {
  1384. EFX_ASSERT_RESET_SERIALISED(efx);
  1385. efx_stop_all(efx);
  1386. mutex_lock(&efx->mac_lock);
  1387. mutex_lock(&efx->spi_lock);
  1388. efx->phy_op->get_settings(efx, ecmd);
  1389. efx_fini_channels(efx);
  1390. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
  1391. efx->phy_op->fini(efx);
  1392. }
  1393. /* This function will always ensure that the locks acquired in
  1394. * efx_reset_down() are released. A failure return code indicates
  1395. * that we were unable to reinitialise the hardware, and the
  1396. * driver should be disabled. If ok is false, then the rx and tx
  1397. * engines are not restarted, pending a RESET_DISABLE. */
  1398. int efx_reset_up(struct efx_nic *efx, enum reset_type method,
  1399. struct ethtool_cmd *ecmd, bool ok)
  1400. {
  1401. int rc;
  1402. EFX_ASSERT_RESET_SERIALISED(efx);
  1403. rc = falcon_init_nic(efx);
  1404. if (rc) {
  1405. EFX_ERR(efx, "failed to initialise NIC\n");
  1406. ok = false;
  1407. }
  1408. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
  1409. if (ok) {
  1410. rc = efx->phy_op->init(efx);
  1411. if (rc)
  1412. ok = false;
  1413. }
  1414. if (!ok)
  1415. efx->port_initialized = false;
  1416. }
  1417. if (ok) {
  1418. efx_init_channels(efx);
  1419. if (efx->phy_op->set_settings(efx, ecmd))
  1420. EFX_ERR(efx, "could not restore PHY settings\n");
  1421. }
  1422. mutex_unlock(&efx->spi_lock);
  1423. mutex_unlock(&efx->mac_lock);
  1424. if (ok)
  1425. efx_start_all(efx);
  1426. return rc;
  1427. }
  1428. /* Reset the NIC as transparently as possible. Do not reset the PHY
  1429. * Note that the reset may fail, in which case the card will be left
  1430. * in a most-probably-unusable state.
  1431. *
  1432. * This function will sleep. You cannot reset from within an atomic
  1433. * state; use efx_schedule_reset() instead.
  1434. *
  1435. * Grabs the rtnl_lock.
  1436. */
  1437. static int efx_reset(struct efx_nic *efx)
  1438. {
  1439. struct ethtool_cmd ecmd;
  1440. enum reset_type method = efx->reset_pending;
  1441. int rc = 0;
  1442. /* Serialise with kernel interfaces */
  1443. rtnl_lock();
  1444. /* If we're not RUNNING then don't reset. Leave the reset_pending
  1445. * flag set so that efx_pci_probe_main will be retried */
  1446. if (efx->state != STATE_RUNNING) {
  1447. EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
  1448. goto out_unlock;
  1449. }
  1450. EFX_INFO(efx, "resetting (%s)\n", RESET_TYPE(method));
  1451. efx_reset_down(efx, method, &ecmd);
  1452. rc = falcon_reset_hw(efx, method);
  1453. if (rc) {
  1454. EFX_ERR(efx, "failed to reset hardware\n");
  1455. goto out_disable;
  1456. }
  1457. /* Allow resets to be rescheduled. */
  1458. efx->reset_pending = RESET_TYPE_NONE;
  1459. /* Reinitialise bus-mastering, which may have been turned off before
  1460. * the reset was scheduled. This is still appropriate, even in the
  1461. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  1462. * can respond to requests. */
  1463. pci_set_master(efx->pci_dev);
  1464. /* Leave device stopped if necessary */
  1465. if (method == RESET_TYPE_DISABLE) {
  1466. efx_reset_up(efx, method, &ecmd, false);
  1467. rc = -EIO;
  1468. } else {
  1469. rc = efx_reset_up(efx, method, &ecmd, true);
  1470. }
  1471. out_disable:
  1472. if (rc) {
  1473. EFX_ERR(efx, "has been disabled\n");
  1474. efx->state = STATE_DISABLED;
  1475. dev_close(efx->net_dev);
  1476. } else {
  1477. EFX_LOG(efx, "reset complete\n");
  1478. }
  1479. out_unlock:
  1480. rtnl_unlock();
  1481. return rc;
  1482. }
  1483. /* The worker thread exists so that code that cannot sleep can
  1484. * schedule a reset for later.
  1485. */
  1486. static void efx_reset_work(struct work_struct *data)
  1487. {
  1488. struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
  1489. efx_reset(nic);
  1490. }
  1491. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  1492. {
  1493. enum reset_type method;
  1494. if (efx->reset_pending != RESET_TYPE_NONE) {
  1495. EFX_INFO(efx, "quenching already scheduled reset\n");
  1496. return;
  1497. }
  1498. switch (type) {
  1499. case RESET_TYPE_INVISIBLE:
  1500. case RESET_TYPE_ALL:
  1501. case RESET_TYPE_WORLD:
  1502. case RESET_TYPE_DISABLE:
  1503. method = type;
  1504. break;
  1505. case RESET_TYPE_RX_RECOVERY:
  1506. case RESET_TYPE_RX_DESC_FETCH:
  1507. case RESET_TYPE_TX_DESC_FETCH:
  1508. case RESET_TYPE_TX_SKIP:
  1509. method = RESET_TYPE_INVISIBLE;
  1510. break;
  1511. default:
  1512. method = RESET_TYPE_ALL;
  1513. break;
  1514. }
  1515. if (method != type)
  1516. EFX_LOG(efx, "scheduling %s reset for %s\n",
  1517. RESET_TYPE(method), RESET_TYPE(type));
  1518. else
  1519. EFX_LOG(efx, "scheduling %s reset\n", RESET_TYPE(method));
  1520. efx->reset_pending = method;
  1521. queue_work(reset_workqueue, &efx->reset_work);
  1522. }
  1523. /**************************************************************************
  1524. *
  1525. * List of NICs we support
  1526. *
  1527. **************************************************************************/
  1528. /* PCI device ID table */
  1529. static struct pci_device_id efx_pci_table[] __devinitdata = {
  1530. {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
  1531. .driver_data = (unsigned long) &falcon_a_nic_type},
  1532. {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
  1533. .driver_data = (unsigned long) &falcon_b_nic_type},
  1534. {0} /* end of list */
  1535. };
  1536. /**************************************************************************
  1537. *
  1538. * Dummy PHY/MAC operations
  1539. *
  1540. * Can be used for some unimplemented operations
  1541. * Needed so all function pointers are valid and do not have to be tested
  1542. * before use
  1543. *
  1544. **************************************************************************/
  1545. int efx_port_dummy_op_int(struct efx_nic *efx)
  1546. {
  1547. return 0;
  1548. }
  1549. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  1550. void efx_port_dummy_op_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
  1551. {
  1552. }
  1553. bool efx_port_dummy_op_poll(struct efx_nic *efx)
  1554. {
  1555. return false;
  1556. }
  1557. static struct efx_phy_operations efx_dummy_phy_operations = {
  1558. .init = efx_port_dummy_op_int,
  1559. .reconfigure = efx_port_dummy_op_void,
  1560. .poll = efx_port_dummy_op_poll,
  1561. .fini = efx_port_dummy_op_void,
  1562. };
  1563. /**************************************************************************
  1564. *
  1565. * Data housekeeping
  1566. *
  1567. **************************************************************************/
  1568. /* This zeroes out and then fills in the invariants in a struct
  1569. * efx_nic (including all sub-structures).
  1570. */
  1571. static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
  1572. struct pci_dev *pci_dev, struct net_device *net_dev)
  1573. {
  1574. struct efx_channel *channel;
  1575. struct efx_tx_queue *tx_queue;
  1576. struct efx_rx_queue *rx_queue;
  1577. int i;
  1578. /* Initialise common structures */
  1579. memset(efx, 0, sizeof(*efx));
  1580. spin_lock_init(&efx->biu_lock);
  1581. mutex_init(&efx->mdio_lock);
  1582. mutex_init(&efx->spi_lock);
  1583. INIT_WORK(&efx->reset_work, efx_reset_work);
  1584. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  1585. efx->pci_dev = pci_dev;
  1586. efx->state = STATE_INIT;
  1587. efx->reset_pending = RESET_TYPE_NONE;
  1588. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  1589. efx->net_dev = net_dev;
  1590. efx->rx_checksum_enabled = true;
  1591. spin_lock_init(&efx->netif_stop_lock);
  1592. spin_lock_init(&efx->stats_lock);
  1593. mutex_init(&efx->mac_lock);
  1594. efx->mac_op = type->default_mac_ops;
  1595. efx->phy_op = &efx_dummy_phy_operations;
  1596. efx->mdio.dev = net_dev;
  1597. INIT_WORK(&efx->mac_work, efx_mac_work);
  1598. atomic_set(&efx->netif_stop_count, 1);
  1599. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  1600. channel = &efx->channel[i];
  1601. channel->efx = efx;
  1602. channel->channel = i;
  1603. channel->work_pending = false;
  1604. }
  1605. for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
  1606. tx_queue = &efx->tx_queue[i];
  1607. tx_queue->efx = efx;
  1608. tx_queue->queue = i;
  1609. tx_queue->buffer = NULL;
  1610. tx_queue->channel = &efx->channel[0]; /* for safety */
  1611. tx_queue->tso_headers_free = NULL;
  1612. }
  1613. for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
  1614. rx_queue = &efx->rx_queue[i];
  1615. rx_queue->efx = efx;
  1616. rx_queue->queue = i;
  1617. rx_queue->channel = &efx->channel[0]; /* for safety */
  1618. rx_queue->buffer = NULL;
  1619. spin_lock_init(&rx_queue->add_lock);
  1620. INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
  1621. }
  1622. efx->type = type;
  1623. /* As close as we can get to guaranteeing that we don't overflow */
  1624. BUILD_BUG_ON(EFX_EVQ_SIZE < EFX_TXQ_SIZE + EFX_RXQ_SIZE);
  1625. EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
  1626. /* Higher numbered interrupt modes are less capable! */
  1627. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  1628. interrupt_mode);
  1629. /* Would be good to use the net_dev name, but we're too early */
  1630. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  1631. pci_name(pci_dev));
  1632. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  1633. if (!efx->workqueue)
  1634. return -ENOMEM;
  1635. return 0;
  1636. }
  1637. static void efx_fini_struct(struct efx_nic *efx)
  1638. {
  1639. if (efx->workqueue) {
  1640. destroy_workqueue(efx->workqueue);
  1641. efx->workqueue = NULL;
  1642. }
  1643. }
  1644. /**************************************************************************
  1645. *
  1646. * PCI interface
  1647. *
  1648. **************************************************************************/
  1649. /* Main body of final NIC shutdown code
  1650. * This is called only at module unload (or hotplug removal).
  1651. */
  1652. static void efx_pci_remove_main(struct efx_nic *efx)
  1653. {
  1654. falcon_fini_interrupt(efx);
  1655. efx_fini_channels(efx);
  1656. efx_fini_port(efx);
  1657. efx_fini_napi(efx);
  1658. efx_remove_all(efx);
  1659. }
  1660. /* Final NIC shutdown
  1661. * This is called only at module unload (or hotplug removal).
  1662. */
  1663. static void efx_pci_remove(struct pci_dev *pci_dev)
  1664. {
  1665. struct efx_nic *efx;
  1666. efx = pci_get_drvdata(pci_dev);
  1667. if (!efx)
  1668. return;
  1669. /* Mark the NIC as fini, then stop the interface */
  1670. rtnl_lock();
  1671. efx->state = STATE_FINI;
  1672. dev_close(efx->net_dev);
  1673. /* Allow any queued efx_resets() to complete */
  1674. rtnl_unlock();
  1675. efx_unregister_netdev(efx);
  1676. efx_mtd_remove(efx);
  1677. /* Wait for any scheduled resets to complete. No more will be
  1678. * scheduled from this point because efx_stop_all() has been
  1679. * called, we are no longer registered with driverlink, and
  1680. * the net_device's have been removed. */
  1681. cancel_work_sync(&efx->reset_work);
  1682. efx_pci_remove_main(efx);
  1683. efx_fini_io(efx);
  1684. EFX_LOG(efx, "shutdown successful\n");
  1685. pci_set_drvdata(pci_dev, NULL);
  1686. efx_fini_struct(efx);
  1687. free_netdev(efx->net_dev);
  1688. };
  1689. /* Main body of NIC initialisation
  1690. * This is called at module load (or hotplug insertion, theoretically).
  1691. */
  1692. static int efx_pci_probe_main(struct efx_nic *efx)
  1693. {
  1694. int rc;
  1695. /* Do start-of-day initialisation */
  1696. rc = efx_probe_all(efx);
  1697. if (rc)
  1698. goto fail1;
  1699. rc = efx_init_napi(efx);
  1700. if (rc)
  1701. goto fail2;
  1702. rc = falcon_init_nic(efx);
  1703. if (rc) {
  1704. EFX_ERR(efx, "failed to initialise NIC\n");
  1705. goto fail3;
  1706. }
  1707. rc = efx_init_port(efx);
  1708. if (rc) {
  1709. EFX_ERR(efx, "failed to initialise port\n");
  1710. goto fail4;
  1711. }
  1712. efx_init_channels(efx);
  1713. rc = falcon_init_interrupt(efx);
  1714. if (rc)
  1715. goto fail5;
  1716. return 0;
  1717. fail5:
  1718. efx_fini_channels(efx);
  1719. efx_fini_port(efx);
  1720. fail4:
  1721. fail3:
  1722. efx_fini_napi(efx);
  1723. fail2:
  1724. efx_remove_all(efx);
  1725. fail1:
  1726. return rc;
  1727. }
  1728. /* NIC initialisation
  1729. *
  1730. * This is called at module load (or hotplug insertion,
  1731. * theoretically). It sets up PCI mappings, tests and resets the NIC,
  1732. * sets up and registers the network devices with the kernel and hooks
  1733. * the interrupt service routine. It does not prepare the device for
  1734. * transmission; this is left to the first time one of the network
  1735. * interfaces is brought up (i.e. efx_net_open).
  1736. */
  1737. static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
  1738. const struct pci_device_id *entry)
  1739. {
  1740. struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
  1741. struct net_device *net_dev;
  1742. struct efx_nic *efx;
  1743. int i, rc;
  1744. /* Allocate and initialise a struct net_device and struct efx_nic */
  1745. net_dev = alloc_etherdev(sizeof(*efx));
  1746. if (!net_dev)
  1747. return -ENOMEM;
  1748. net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
  1749. NETIF_F_HIGHDMA | NETIF_F_TSO |
  1750. NETIF_F_GRO);
  1751. /* Mask for features that also apply to VLAN devices */
  1752. net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
  1753. NETIF_F_HIGHDMA | NETIF_F_TSO);
  1754. efx = netdev_priv(net_dev);
  1755. pci_set_drvdata(pci_dev, efx);
  1756. rc = efx_init_struct(efx, type, pci_dev, net_dev);
  1757. if (rc)
  1758. goto fail1;
  1759. EFX_INFO(efx, "Solarflare Communications NIC detected\n");
  1760. /* Set up basic I/O (BAR mappings etc) */
  1761. rc = efx_init_io(efx);
  1762. if (rc)
  1763. goto fail2;
  1764. /* No serialisation is required with the reset path because
  1765. * we're in STATE_INIT. */
  1766. for (i = 0; i < 5; i++) {
  1767. rc = efx_pci_probe_main(efx);
  1768. /* Serialise against efx_reset(). No more resets will be
  1769. * scheduled since efx_stop_all() has been called, and we
  1770. * have not and never have been registered with either
  1771. * the rtnetlink or driverlink layers. */
  1772. cancel_work_sync(&efx->reset_work);
  1773. if (rc == 0) {
  1774. if (efx->reset_pending != RESET_TYPE_NONE) {
  1775. /* If there was a scheduled reset during
  1776. * probe, the NIC is probably hosed anyway */
  1777. efx_pci_remove_main(efx);
  1778. rc = -EIO;
  1779. } else {
  1780. break;
  1781. }
  1782. }
  1783. /* Retry if a recoverably reset event has been scheduled */
  1784. if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
  1785. (efx->reset_pending != RESET_TYPE_ALL))
  1786. goto fail3;
  1787. efx->reset_pending = RESET_TYPE_NONE;
  1788. }
  1789. if (rc) {
  1790. EFX_ERR(efx, "Could not reset NIC\n");
  1791. goto fail4;
  1792. }
  1793. /* Switch to the running state before we expose the device to the OS,
  1794. * so that dev_open()|efx_start_all() will actually start the device */
  1795. efx->state = STATE_RUNNING;
  1796. rc = efx_register_netdev(efx);
  1797. if (rc)
  1798. goto fail5;
  1799. EFX_LOG(efx, "initialisation successful\n");
  1800. rtnl_lock();
  1801. efx_mtd_probe(efx); /* allowed to fail */
  1802. rtnl_unlock();
  1803. return 0;
  1804. fail5:
  1805. efx_pci_remove_main(efx);
  1806. fail4:
  1807. fail3:
  1808. efx_fini_io(efx);
  1809. fail2:
  1810. efx_fini_struct(efx);
  1811. fail1:
  1812. EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
  1813. free_netdev(net_dev);
  1814. return rc;
  1815. }
  1816. static struct pci_driver efx_pci_driver = {
  1817. .name = EFX_DRIVER_NAME,
  1818. .id_table = efx_pci_table,
  1819. .probe = efx_pci_probe,
  1820. .remove = efx_pci_remove,
  1821. };
  1822. /**************************************************************************
  1823. *
  1824. * Kernel module interface
  1825. *
  1826. *************************************************************************/
  1827. module_param(interrupt_mode, uint, 0444);
  1828. MODULE_PARM_DESC(interrupt_mode,
  1829. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  1830. static int __init efx_init_module(void)
  1831. {
  1832. int rc;
  1833. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  1834. rc = register_netdevice_notifier(&efx_netdev_notifier);
  1835. if (rc)
  1836. goto err_notifier;
  1837. refill_workqueue = create_workqueue("sfc_refill");
  1838. if (!refill_workqueue) {
  1839. rc = -ENOMEM;
  1840. goto err_refill;
  1841. }
  1842. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  1843. if (!reset_workqueue) {
  1844. rc = -ENOMEM;
  1845. goto err_reset;
  1846. }
  1847. rc = pci_register_driver(&efx_pci_driver);
  1848. if (rc < 0)
  1849. goto err_pci;
  1850. return 0;
  1851. err_pci:
  1852. destroy_workqueue(reset_workqueue);
  1853. err_reset:
  1854. destroy_workqueue(refill_workqueue);
  1855. err_refill:
  1856. unregister_netdevice_notifier(&efx_netdev_notifier);
  1857. err_notifier:
  1858. return rc;
  1859. }
  1860. static void __exit efx_exit_module(void)
  1861. {
  1862. printk(KERN_INFO "Solarflare NET driver unloading\n");
  1863. pci_unregister_driver(&efx_pci_driver);
  1864. destroy_workqueue(reset_workqueue);
  1865. destroy_workqueue(refill_workqueue);
  1866. unregister_netdevice_notifier(&efx_netdev_notifier);
  1867. }
  1868. module_init(efx_init_module);
  1869. module_exit(efx_exit_module);
  1870. MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
  1871. "Solarflare Communications");
  1872. MODULE_DESCRIPTION("Solarflare Communications network driver");
  1873. MODULE_LICENSE("GPL");
  1874. MODULE_DEVICE_TABLE(pci, efx_pci_table);