efx.c 63 KB

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