efx.c 60 KB

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