efx.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2011 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/ethtool.h>
  20. #include <linux/topology.h>
  21. #include <linux/gfp.h>
  22. #include <linux/aer.h>
  23. #include <linux/interrupt.h>
  24. #include "net_driver.h"
  25. #include "efx.h"
  26. #include "nic.h"
  27. #include "selftest.h"
  28. #include "mcdi.h"
  29. #include "workarounds.h"
  30. /**************************************************************************
  31. *
  32. * Type name strings
  33. *
  34. **************************************************************************
  35. */
  36. /* Loopback mode names (see LOOPBACK_MODE()) */
  37. const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
  38. const char *const efx_loopback_mode_names[] = {
  39. [LOOPBACK_NONE] = "NONE",
  40. [LOOPBACK_DATA] = "DATAPATH",
  41. [LOOPBACK_GMAC] = "GMAC",
  42. [LOOPBACK_XGMII] = "XGMII",
  43. [LOOPBACK_XGXS] = "XGXS",
  44. [LOOPBACK_XAUI] = "XAUI",
  45. [LOOPBACK_GMII] = "GMII",
  46. [LOOPBACK_SGMII] = "SGMII",
  47. [LOOPBACK_XGBR] = "XGBR",
  48. [LOOPBACK_XFI] = "XFI",
  49. [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
  50. [LOOPBACK_GMII_FAR] = "GMII_FAR",
  51. [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
  52. [LOOPBACK_XFI_FAR] = "XFI_FAR",
  53. [LOOPBACK_GPHY] = "GPHY",
  54. [LOOPBACK_PHYXS] = "PHYXS",
  55. [LOOPBACK_PCS] = "PCS",
  56. [LOOPBACK_PMAPMD] = "PMA/PMD",
  57. [LOOPBACK_XPORT] = "XPORT",
  58. [LOOPBACK_XGMII_WS] = "XGMII_WS",
  59. [LOOPBACK_XAUI_WS] = "XAUI_WS",
  60. [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
  61. [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
  62. [LOOPBACK_GMII_WS] = "GMII_WS",
  63. [LOOPBACK_XFI_WS] = "XFI_WS",
  64. [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
  65. [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
  66. };
  67. const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
  68. const char *const efx_reset_type_names[] = {
  69. [RESET_TYPE_INVISIBLE] = "INVISIBLE",
  70. [RESET_TYPE_ALL] = "ALL",
  71. [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
  72. [RESET_TYPE_WORLD] = "WORLD",
  73. [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
  74. [RESET_TYPE_DISABLE] = "DISABLE",
  75. [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
  76. [RESET_TYPE_INT_ERROR] = "INT_ERROR",
  77. [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
  78. [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
  79. [RESET_TYPE_TX_SKIP] = "TX_SKIP",
  80. [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
  81. };
  82. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  83. * queued onto this work queue. This is not a per-nic work queue, because
  84. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  85. */
  86. static struct workqueue_struct *reset_workqueue;
  87. /**************************************************************************
  88. *
  89. * Configurable values
  90. *
  91. *************************************************************************/
  92. /*
  93. * Use separate channels for TX and RX events
  94. *
  95. * Set this to 1 to use separate channels for TX and RX. It allows us
  96. * to control interrupt affinity separately for TX and RX.
  97. *
  98. * This is only used in MSI-X interrupt mode
  99. */
  100. static bool separate_tx_channels;
  101. module_param(separate_tx_channels, bool, 0444);
  102. MODULE_PARM_DESC(separate_tx_channels,
  103. "Use separate channels for TX and RX");
  104. /* This is the weight assigned to each of the (per-channel) virtual
  105. * NAPI devices.
  106. */
  107. static int napi_weight = 64;
  108. /* This is the time (in jiffies) between invocations of the hardware
  109. * monitor.
  110. * On Falcon-based NICs, this will:
  111. * - Check the on-board hardware monitor;
  112. * - Poll the link state and reconfigure the hardware as necessary.
  113. * On Siena-based NICs for power systems with EEH support, this will give EEH a
  114. * chance to start.
  115. */
  116. static unsigned int efx_monitor_interval = 1 * HZ;
  117. /* Initial interrupt moderation settings. They can be modified after
  118. * module load with ethtool.
  119. *
  120. * The default for RX should strike a balance between increasing the
  121. * round-trip latency and reducing overhead.
  122. */
  123. static unsigned int rx_irq_mod_usec = 60;
  124. /* Initial interrupt moderation settings. They can be modified after
  125. * module load with ethtool.
  126. *
  127. * This default is chosen to ensure that a 10G link does not go idle
  128. * while a TX queue is stopped after it has become full. A queue is
  129. * restarted when it drops below half full. The time this takes (assuming
  130. * worst case 3 descriptors per packet and 1024 descriptors) is
  131. * 512 / 3 * 1.2 = 205 usec.
  132. */
  133. static unsigned int tx_irq_mod_usec = 150;
  134. /* This is the first interrupt mode to try out of:
  135. * 0 => MSI-X
  136. * 1 => MSI
  137. * 2 => legacy
  138. */
  139. static unsigned int interrupt_mode;
  140. /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
  141. * i.e. the number of CPUs among which we may distribute simultaneous
  142. * interrupt handling.
  143. *
  144. * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
  145. * The default (0) means to assign an interrupt to each core.
  146. */
  147. static unsigned int rss_cpus;
  148. module_param(rss_cpus, uint, 0444);
  149. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  150. static bool phy_flash_cfg;
  151. module_param(phy_flash_cfg, bool, 0644);
  152. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  153. static unsigned irq_adapt_low_thresh = 8000;
  154. module_param(irq_adapt_low_thresh, uint, 0644);
  155. MODULE_PARM_DESC(irq_adapt_low_thresh,
  156. "Threshold score for reducing IRQ moderation");
  157. static unsigned irq_adapt_high_thresh = 16000;
  158. module_param(irq_adapt_high_thresh, uint, 0644);
  159. MODULE_PARM_DESC(irq_adapt_high_thresh,
  160. "Threshold score for increasing IRQ moderation");
  161. static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  162. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  163. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  164. NETIF_MSG_TX_ERR | NETIF_MSG_HW);
  165. module_param(debug, uint, 0);
  166. MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
  167. /**************************************************************************
  168. *
  169. * Utility functions and prototypes
  170. *
  171. *************************************************************************/
  172. static void efx_soft_enable_interrupts(struct efx_nic *efx);
  173. static void efx_soft_disable_interrupts(struct efx_nic *efx);
  174. static void efx_remove_channel(struct efx_channel *channel);
  175. static void efx_remove_channels(struct efx_nic *efx);
  176. static const struct efx_channel_type efx_default_channel_type;
  177. static void efx_remove_port(struct efx_nic *efx);
  178. static void efx_init_napi_channel(struct efx_channel *channel);
  179. static void efx_fini_napi(struct efx_nic *efx);
  180. static void efx_fini_napi_channel(struct efx_channel *channel);
  181. static void efx_fini_struct(struct efx_nic *efx);
  182. static void efx_start_all(struct efx_nic *efx);
  183. static void efx_stop_all(struct efx_nic *efx);
  184. #define EFX_ASSERT_RESET_SERIALISED(efx) \
  185. do { \
  186. if ((efx->state == STATE_READY) || \
  187. (efx->state == STATE_RECOVERY) || \
  188. (efx->state == STATE_DISABLED)) \
  189. ASSERT_RTNL(); \
  190. } while (0)
  191. static int efx_check_disabled(struct efx_nic *efx)
  192. {
  193. if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
  194. netif_err(efx, drv, efx->net_dev,
  195. "device is disabled due to earlier errors\n");
  196. return -EIO;
  197. }
  198. return 0;
  199. }
  200. /**************************************************************************
  201. *
  202. * Event queue processing
  203. *
  204. *************************************************************************/
  205. /* Process channel's event queue
  206. *
  207. * This function is responsible for processing the event queue of a
  208. * single channel. The caller must guarantee that this function will
  209. * never be concurrently called more than once on the same channel,
  210. * though different channels may be being processed concurrently.
  211. */
  212. static int efx_process_channel(struct efx_channel *channel, int budget)
  213. {
  214. int spent;
  215. if (unlikely(!channel->enabled))
  216. return 0;
  217. spent = efx_nic_process_eventq(channel, budget);
  218. if (spent && efx_channel_has_rx_queue(channel)) {
  219. struct efx_rx_queue *rx_queue =
  220. efx_channel_get_rx_queue(channel);
  221. efx_rx_flush_packet(channel);
  222. efx_fast_push_rx_descriptors(rx_queue);
  223. }
  224. return spent;
  225. }
  226. /* NAPI poll handler
  227. *
  228. * NAPI guarantees serialisation of polls of the same device, which
  229. * provides the guarantee required by efx_process_channel().
  230. */
  231. static int efx_poll(struct napi_struct *napi, int budget)
  232. {
  233. struct efx_channel *channel =
  234. container_of(napi, struct efx_channel, napi_str);
  235. struct efx_nic *efx = channel->efx;
  236. int spent;
  237. netif_vdbg(efx, intr, efx->net_dev,
  238. "channel %d NAPI poll executing on CPU %d\n",
  239. channel->channel, raw_smp_processor_id());
  240. spent = efx_process_channel(channel, budget);
  241. if (spent < budget) {
  242. if (efx_channel_has_rx_queue(channel) &&
  243. efx->irq_rx_adaptive &&
  244. unlikely(++channel->irq_count == 1000)) {
  245. if (unlikely(channel->irq_mod_score <
  246. irq_adapt_low_thresh)) {
  247. if (channel->irq_moderation > 1) {
  248. channel->irq_moderation -= 1;
  249. efx->type->push_irq_moderation(channel);
  250. }
  251. } else if (unlikely(channel->irq_mod_score >
  252. irq_adapt_high_thresh)) {
  253. if (channel->irq_moderation <
  254. efx->irq_rx_moderation) {
  255. channel->irq_moderation += 1;
  256. efx->type->push_irq_moderation(channel);
  257. }
  258. }
  259. channel->irq_count = 0;
  260. channel->irq_mod_score = 0;
  261. }
  262. efx_filter_rfs_expire(channel);
  263. /* There is no race here; although napi_disable() will
  264. * only wait for napi_complete(), this isn't a problem
  265. * since efx_nic_eventq_read_ack() will have no effect if
  266. * interrupts have already been disabled.
  267. */
  268. napi_complete(napi);
  269. efx_nic_eventq_read_ack(channel);
  270. }
  271. return spent;
  272. }
  273. /* Create event queue
  274. * Event queue memory allocations are done only once. If the channel
  275. * is reset, the memory buffer will be reused; this guards against
  276. * errors during channel reset and also simplifies interrupt handling.
  277. */
  278. static int efx_probe_eventq(struct efx_channel *channel)
  279. {
  280. struct efx_nic *efx = channel->efx;
  281. unsigned long entries;
  282. netif_dbg(efx, probe, efx->net_dev,
  283. "chan %d create event queue\n", channel->channel);
  284. /* Build an event queue with room for one event per tx and rx buffer,
  285. * plus some extra for link state events and MCDI completions. */
  286. entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
  287. EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
  288. channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
  289. return efx_nic_probe_eventq(channel);
  290. }
  291. /* Prepare channel's event queue */
  292. static void efx_init_eventq(struct efx_channel *channel)
  293. {
  294. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  295. "chan %d init event queue\n", channel->channel);
  296. channel->eventq_read_ptr = 0;
  297. efx_nic_init_eventq(channel);
  298. channel->eventq_init = true;
  299. }
  300. /* Enable event queue processing and NAPI */
  301. static void efx_start_eventq(struct efx_channel *channel)
  302. {
  303. netif_dbg(channel->efx, ifup, channel->efx->net_dev,
  304. "chan %d start event queue\n", channel->channel);
  305. /* Make sure the NAPI handler sees the enabled flag set */
  306. channel->enabled = true;
  307. smp_wmb();
  308. napi_enable(&channel->napi_str);
  309. efx_nic_eventq_read_ack(channel);
  310. }
  311. /* Disable event queue processing and NAPI */
  312. static void efx_stop_eventq(struct efx_channel *channel)
  313. {
  314. if (!channel->enabled)
  315. return;
  316. napi_disable(&channel->napi_str);
  317. channel->enabled = false;
  318. }
  319. static void efx_fini_eventq(struct efx_channel *channel)
  320. {
  321. if (!channel->eventq_init)
  322. return;
  323. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  324. "chan %d fini event queue\n", channel->channel);
  325. efx_nic_fini_eventq(channel);
  326. channel->eventq_init = false;
  327. }
  328. static void efx_remove_eventq(struct efx_channel *channel)
  329. {
  330. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  331. "chan %d remove event queue\n", channel->channel);
  332. efx_nic_remove_eventq(channel);
  333. }
  334. /**************************************************************************
  335. *
  336. * Channel handling
  337. *
  338. *************************************************************************/
  339. /* Allocate and initialise a channel structure. */
  340. static struct efx_channel *
  341. efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
  342. {
  343. struct efx_channel *channel;
  344. struct efx_rx_queue *rx_queue;
  345. struct efx_tx_queue *tx_queue;
  346. int j;
  347. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  348. if (!channel)
  349. return NULL;
  350. channel->efx = efx;
  351. channel->channel = i;
  352. channel->type = &efx_default_channel_type;
  353. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  354. tx_queue = &channel->tx_queue[j];
  355. tx_queue->efx = efx;
  356. tx_queue->queue = i * EFX_TXQ_TYPES + j;
  357. tx_queue->channel = channel;
  358. }
  359. rx_queue = &channel->rx_queue;
  360. rx_queue->efx = efx;
  361. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  362. (unsigned long)rx_queue);
  363. return channel;
  364. }
  365. /* Allocate and initialise a channel structure, copying parameters
  366. * (but not resources) from an old channel structure.
  367. */
  368. static struct efx_channel *
  369. efx_copy_channel(const struct efx_channel *old_channel)
  370. {
  371. struct efx_channel *channel;
  372. struct efx_rx_queue *rx_queue;
  373. struct efx_tx_queue *tx_queue;
  374. int j;
  375. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  376. if (!channel)
  377. return NULL;
  378. *channel = *old_channel;
  379. channel->napi_dev = NULL;
  380. memset(&channel->eventq, 0, sizeof(channel->eventq));
  381. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  382. tx_queue = &channel->tx_queue[j];
  383. if (tx_queue->channel)
  384. tx_queue->channel = channel;
  385. tx_queue->buffer = NULL;
  386. memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
  387. }
  388. rx_queue = &channel->rx_queue;
  389. rx_queue->buffer = NULL;
  390. memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
  391. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  392. (unsigned long)rx_queue);
  393. return channel;
  394. }
  395. static int efx_probe_channel(struct efx_channel *channel)
  396. {
  397. struct efx_tx_queue *tx_queue;
  398. struct efx_rx_queue *rx_queue;
  399. int rc;
  400. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  401. "creating channel %d\n", channel->channel);
  402. rc = channel->type->pre_probe(channel);
  403. if (rc)
  404. goto fail;
  405. rc = efx_probe_eventq(channel);
  406. if (rc)
  407. goto fail;
  408. efx_for_each_channel_tx_queue(tx_queue, channel) {
  409. rc = efx_probe_tx_queue(tx_queue);
  410. if (rc)
  411. goto fail;
  412. }
  413. efx_for_each_channel_rx_queue(rx_queue, channel) {
  414. rc = efx_probe_rx_queue(rx_queue);
  415. if (rc)
  416. goto fail;
  417. }
  418. channel->n_rx_frm_trunc = 0;
  419. return 0;
  420. fail:
  421. efx_remove_channel(channel);
  422. return rc;
  423. }
  424. static void
  425. efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
  426. {
  427. struct efx_nic *efx = channel->efx;
  428. const char *type;
  429. int number;
  430. number = channel->channel;
  431. if (efx->tx_channel_offset == 0) {
  432. type = "";
  433. } else if (channel->channel < efx->tx_channel_offset) {
  434. type = "-rx";
  435. } else {
  436. type = "-tx";
  437. number -= efx->tx_channel_offset;
  438. }
  439. snprintf(buf, len, "%s%s-%d", efx->name, type, number);
  440. }
  441. static void efx_set_channel_names(struct efx_nic *efx)
  442. {
  443. struct efx_channel *channel;
  444. efx_for_each_channel(channel, efx)
  445. channel->type->get_name(channel,
  446. efx->msi_context[channel->channel].name,
  447. sizeof(efx->msi_context[0].name));
  448. }
  449. static int efx_probe_channels(struct efx_nic *efx)
  450. {
  451. struct efx_channel *channel;
  452. int rc;
  453. /* Restart special buffer allocation */
  454. efx->next_buffer_table = 0;
  455. /* Probe channels in reverse, so that any 'extra' channels
  456. * use the start of the buffer table. This allows the traffic
  457. * channels to be resized without moving them or wasting the
  458. * entries before them.
  459. */
  460. efx_for_each_channel_rev(channel, efx) {
  461. rc = efx_probe_channel(channel);
  462. if (rc) {
  463. netif_err(efx, probe, efx->net_dev,
  464. "failed to create channel %d\n",
  465. channel->channel);
  466. goto fail;
  467. }
  468. }
  469. efx_set_channel_names(efx);
  470. return 0;
  471. fail:
  472. efx_remove_channels(efx);
  473. return rc;
  474. }
  475. /* Channels are shutdown and reinitialised whilst the NIC is running
  476. * to propagate configuration changes (mtu, checksum offload), or
  477. * to clear hardware error conditions
  478. */
  479. static void efx_start_datapath(struct efx_nic *efx)
  480. {
  481. bool old_rx_scatter = efx->rx_scatter;
  482. struct efx_tx_queue *tx_queue;
  483. struct efx_rx_queue *rx_queue;
  484. struct efx_channel *channel;
  485. size_t rx_buf_len;
  486. /* Calculate the rx buffer allocation parameters required to
  487. * support the current MTU, including padding for header
  488. * alignment and overruns.
  489. */
  490. efx->rx_dma_len = (efx->rx_prefix_size +
  491. EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
  492. efx->type->rx_buffer_padding);
  493. rx_buf_len = (sizeof(struct efx_rx_page_state) +
  494. NET_IP_ALIGN + efx->rx_dma_len);
  495. if (rx_buf_len <= PAGE_SIZE) {
  496. efx->rx_scatter = false;
  497. efx->rx_buffer_order = 0;
  498. } else if (efx->type->can_rx_scatter) {
  499. BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
  500. BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
  501. 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
  502. EFX_RX_BUF_ALIGNMENT) >
  503. PAGE_SIZE);
  504. efx->rx_scatter = true;
  505. efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
  506. efx->rx_buffer_order = 0;
  507. } else {
  508. efx->rx_scatter = false;
  509. efx->rx_buffer_order = get_order(rx_buf_len);
  510. }
  511. efx_rx_config_page_split(efx);
  512. if (efx->rx_buffer_order)
  513. netif_dbg(efx, drv, efx->net_dev,
  514. "RX buf len=%u; page order=%u batch=%u\n",
  515. efx->rx_dma_len, efx->rx_buffer_order,
  516. efx->rx_pages_per_batch);
  517. else
  518. netif_dbg(efx, drv, efx->net_dev,
  519. "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
  520. efx->rx_dma_len, efx->rx_page_buf_step,
  521. efx->rx_bufs_per_page, efx->rx_pages_per_batch);
  522. /* RX filters also have scatter-enabled flags */
  523. if (efx->rx_scatter != old_rx_scatter)
  524. efx->type->filter_update_rx_scatter(efx);
  525. /* We must keep at least one descriptor in a TX ring empty.
  526. * We could avoid this when the queue size does not exactly
  527. * match the hardware ring size, but it's not that important.
  528. * Therefore we stop the queue when one more skb might fill
  529. * the ring completely. We wake it when half way back to
  530. * empty.
  531. */
  532. efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
  533. efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
  534. /* Initialise the channels */
  535. efx_for_each_channel(channel, efx) {
  536. efx_for_each_channel_tx_queue(tx_queue, channel)
  537. efx_init_tx_queue(tx_queue);
  538. efx_for_each_channel_rx_queue(rx_queue, channel) {
  539. efx_init_rx_queue(rx_queue);
  540. efx_nic_generate_fill_event(rx_queue);
  541. }
  542. WARN_ON(channel->rx_pkt_n_frags);
  543. }
  544. if (netif_device_present(efx->net_dev))
  545. netif_tx_wake_all_queues(efx->net_dev);
  546. }
  547. static void efx_stop_datapath(struct efx_nic *efx)
  548. {
  549. struct efx_channel *channel;
  550. struct efx_tx_queue *tx_queue;
  551. struct efx_rx_queue *rx_queue;
  552. int rc;
  553. EFX_ASSERT_RESET_SERIALISED(efx);
  554. BUG_ON(efx->port_enabled);
  555. /* Stop RX refill */
  556. efx_for_each_channel(channel, efx) {
  557. efx_for_each_channel_rx_queue(rx_queue, channel)
  558. rx_queue->refill_enabled = false;
  559. }
  560. efx_for_each_channel(channel, efx) {
  561. /* RX packet processing is pipelined, so wait for the
  562. * NAPI handler to complete. At least event queue 0
  563. * might be kept active by non-data events, so don't
  564. * use napi_synchronize() but actually disable NAPI
  565. * temporarily.
  566. */
  567. if (efx_channel_has_rx_queue(channel)) {
  568. efx_stop_eventq(channel);
  569. efx_start_eventq(channel);
  570. }
  571. }
  572. rc = efx->type->fini_dmaq(efx);
  573. if (rc && EFX_WORKAROUND_7803(efx)) {
  574. /* Schedule a reset to recover from the flush failure. The
  575. * descriptor caches reference memory we're about to free,
  576. * but falcon_reconfigure_mac_wrapper() won't reconnect
  577. * the MACs because of the pending reset.
  578. */
  579. netif_err(efx, drv, efx->net_dev,
  580. "Resetting to recover from flush failure\n");
  581. efx_schedule_reset(efx, RESET_TYPE_ALL);
  582. } else if (rc) {
  583. netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
  584. } else {
  585. netif_dbg(efx, drv, efx->net_dev,
  586. "successfully flushed all queues\n");
  587. }
  588. efx_for_each_channel(channel, efx) {
  589. efx_for_each_channel_rx_queue(rx_queue, channel)
  590. efx_fini_rx_queue(rx_queue);
  591. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  592. efx_fini_tx_queue(tx_queue);
  593. }
  594. }
  595. static void efx_remove_channel(struct efx_channel *channel)
  596. {
  597. struct efx_tx_queue *tx_queue;
  598. struct efx_rx_queue *rx_queue;
  599. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  600. "destroy chan %d\n", channel->channel);
  601. efx_for_each_channel_rx_queue(rx_queue, channel)
  602. efx_remove_rx_queue(rx_queue);
  603. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  604. efx_remove_tx_queue(tx_queue);
  605. efx_remove_eventq(channel);
  606. channel->type->post_remove(channel);
  607. }
  608. static void efx_remove_channels(struct efx_nic *efx)
  609. {
  610. struct efx_channel *channel;
  611. efx_for_each_channel(channel, efx)
  612. efx_remove_channel(channel);
  613. }
  614. int
  615. efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
  616. {
  617. struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
  618. u32 old_rxq_entries, old_txq_entries;
  619. unsigned i, next_buffer_table = 0;
  620. int rc;
  621. rc = efx_check_disabled(efx);
  622. if (rc)
  623. return rc;
  624. /* Not all channels should be reallocated. We must avoid
  625. * reallocating their buffer table entries.
  626. */
  627. efx_for_each_channel(channel, efx) {
  628. struct efx_rx_queue *rx_queue;
  629. struct efx_tx_queue *tx_queue;
  630. if (channel->type->copy)
  631. continue;
  632. next_buffer_table = max(next_buffer_table,
  633. channel->eventq.index +
  634. channel->eventq.entries);
  635. efx_for_each_channel_rx_queue(rx_queue, channel)
  636. next_buffer_table = max(next_buffer_table,
  637. rx_queue->rxd.index +
  638. rx_queue->rxd.entries);
  639. efx_for_each_channel_tx_queue(tx_queue, channel)
  640. next_buffer_table = max(next_buffer_table,
  641. tx_queue->txd.index +
  642. tx_queue->txd.entries);
  643. }
  644. efx_device_detach_sync(efx);
  645. efx_stop_all(efx);
  646. efx_soft_disable_interrupts(efx);
  647. /* Clone channels (where possible) */
  648. memset(other_channel, 0, sizeof(other_channel));
  649. for (i = 0; i < efx->n_channels; i++) {
  650. channel = efx->channel[i];
  651. if (channel->type->copy)
  652. channel = channel->type->copy(channel);
  653. if (!channel) {
  654. rc = -ENOMEM;
  655. goto out;
  656. }
  657. other_channel[i] = channel;
  658. }
  659. /* Swap entry counts and channel pointers */
  660. old_rxq_entries = efx->rxq_entries;
  661. old_txq_entries = efx->txq_entries;
  662. efx->rxq_entries = rxq_entries;
  663. efx->txq_entries = txq_entries;
  664. for (i = 0; i < efx->n_channels; i++) {
  665. channel = efx->channel[i];
  666. efx->channel[i] = other_channel[i];
  667. other_channel[i] = channel;
  668. }
  669. /* Restart buffer table allocation */
  670. efx->next_buffer_table = next_buffer_table;
  671. for (i = 0; i < efx->n_channels; i++) {
  672. channel = efx->channel[i];
  673. if (!channel->type->copy)
  674. continue;
  675. rc = efx_probe_channel(channel);
  676. if (rc)
  677. goto rollback;
  678. efx_init_napi_channel(efx->channel[i]);
  679. }
  680. out:
  681. /* Destroy unused channel structures */
  682. for (i = 0; i < efx->n_channels; i++) {
  683. channel = other_channel[i];
  684. if (channel && channel->type->copy) {
  685. efx_fini_napi_channel(channel);
  686. efx_remove_channel(channel);
  687. kfree(channel);
  688. }
  689. }
  690. efx_soft_enable_interrupts(efx);
  691. efx_start_all(efx);
  692. netif_device_attach(efx->net_dev);
  693. return rc;
  694. rollback:
  695. /* Swap back */
  696. efx->rxq_entries = old_rxq_entries;
  697. efx->txq_entries = old_txq_entries;
  698. for (i = 0; i < efx->n_channels; i++) {
  699. channel = efx->channel[i];
  700. efx->channel[i] = other_channel[i];
  701. other_channel[i] = channel;
  702. }
  703. goto out;
  704. }
  705. void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
  706. {
  707. mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
  708. }
  709. static const struct efx_channel_type efx_default_channel_type = {
  710. .pre_probe = efx_channel_dummy_op_int,
  711. .post_remove = efx_channel_dummy_op_void,
  712. .get_name = efx_get_channel_name,
  713. .copy = efx_copy_channel,
  714. .keep_eventq = false,
  715. };
  716. int efx_channel_dummy_op_int(struct efx_channel *channel)
  717. {
  718. return 0;
  719. }
  720. void efx_channel_dummy_op_void(struct efx_channel *channel)
  721. {
  722. }
  723. /**************************************************************************
  724. *
  725. * Port handling
  726. *
  727. **************************************************************************/
  728. /* This ensures that the kernel is kept informed (via
  729. * netif_carrier_on/off) of the link status, and also maintains the
  730. * link status's stop on the port's TX queue.
  731. */
  732. void efx_link_status_changed(struct efx_nic *efx)
  733. {
  734. struct efx_link_state *link_state = &efx->link_state;
  735. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  736. * that no events are triggered between unregister_netdev() and the
  737. * driver unloading. A more general condition is that NETDEV_CHANGE
  738. * can only be generated between NETDEV_UP and NETDEV_DOWN */
  739. if (!netif_running(efx->net_dev))
  740. return;
  741. if (link_state->up != netif_carrier_ok(efx->net_dev)) {
  742. efx->n_link_state_changes++;
  743. if (link_state->up)
  744. netif_carrier_on(efx->net_dev);
  745. else
  746. netif_carrier_off(efx->net_dev);
  747. }
  748. /* Status message for kernel log */
  749. if (link_state->up)
  750. netif_info(efx, link, efx->net_dev,
  751. "link up at %uMbps %s-duplex (MTU %d)\n",
  752. link_state->speed, link_state->fd ? "full" : "half",
  753. efx->net_dev->mtu);
  754. else
  755. netif_info(efx, link, efx->net_dev, "link down\n");
  756. }
  757. void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
  758. {
  759. efx->link_advertising = advertising;
  760. if (advertising) {
  761. if (advertising & ADVERTISED_Pause)
  762. efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
  763. else
  764. efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
  765. if (advertising & ADVERTISED_Asym_Pause)
  766. efx->wanted_fc ^= EFX_FC_TX;
  767. }
  768. }
  769. void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
  770. {
  771. efx->wanted_fc = wanted_fc;
  772. if (efx->link_advertising) {
  773. if (wanted_fc & EFX_FC_RX)
  774. efx->link_advertising |= (ADVERTISED_Pause |
  775. ADVERTISED_Asym_Pause);
  776. else
  777. efx->link_advertising &= ~(ADVERTISED_Pause |
  778. ADVERTISED_Asym_Pause);
  779. if (wanted_fc & EFX_FC_TX)
  780. efx->link_advertising ^= ADVERTISED_Asym_Pause;
  781. }
  782. }
  783. static void efx_fini_port(struct efx_nic *efx);
  784. /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
  785. * the MAC appropriately. All other PHY configuration changes are pushed
  786. * through phy_op->set_settings(), and pushed asynchronously to the MAC
  787. * through efx_monitor().
  788. *
  789. * Callers must hold the mac_lock
  790. */
  791. int __efx_reconfigure_port(struct efx_nic *efx)
  792. {
  793. enum efx_phy_mode phy_mode;
  794. int rc;
  795. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  796. /* Disable PHY transmit in mac level loopbacks */
  797. phy_mode = efx->phy_mode;
  798. if (LOOPBACK_INTERNAL(efx))
  799. efx->phy_mode |= PHY_MODE_TX_DISABLED;
  800. else
  801. efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
  802. rc = efx->type->reconfigure_port(efx);
  803. if (rc)
  804. efx->phy_mode = phy_mode;
  805. return rc;
  806. }
  807. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  808. * disabled. */
  809. int efx_reconfigure_port(struct efx_nic *efx)
  810. {
  811. int rc;
  812. EFX_ASSERT_RESET_SERIALISED(efx);
  813. mutex_lock(&efx->mac_lock);
  814. rc = __efx_reconfigure_port(efx);
  815. mutex_unlock(&efx->mac_lock);
  816. return rc;
  817. }
  818. /* Asynchronous work item for changing MAC promiscuity and multicast
  819. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  820. * MAC directly. */
  821. static void efx_mac_work(struct work_struct *data)
  822. {
  823. struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
  824. mutex_lock(&efx->mac_lock);
  825. if (efx->port_enabled)
  826. efx->type->reconfigure_mac(efx);
  827. mutex_unlock(&efx->mac_lock);
  828. }
  829. static int efx_probe_port(struct efx_nic *efx)
  830. {
  831. int rc;
  832. netif_dbg(efx, probe, efx->net_dev, "create port\n");
  833. if (phy_flash_cfg)
  834. efx->phy_mode = PHY_MODE_SPECIAL;
  835. /* Connect up MAC/PHY operations table */
  836. rc = efx->type->probe_port(efx);
  837. if (rc)
  838. return rc;
  839. /* Initialise MAC address to permanent address */
  840. memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
  841. return 0;
  842. }
  843. static int efx_init_port(struct efx_nic *efx)
  844. {
  845. int rc;
  846. netif_dbg(efx, drv, efx->net_dev, "init port\n");
  847. mutex_lock(&efx->mac_lock);
  848. rc = efx->phy_op->init(efx);
  849. if (rc)
  850. goto fail1;
  851. efx->port_initialized = true;
  852. /* Reconfigure the MAC before creating dma queues (required for
  853. * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
  854. efx->type->reconfigure_mac(efx);
  855. /* Ensure the PHY advertises the correct flow control settings */
  856. rc = efx->phy_op->reconfigure(efx);
  857. if (rc)
  858. goto fail2;
  859. mutex_unlock(&efx->mac_lock);
  860. return 0;
  861. fail2:
  862. efx->phy_op->fini(efx);
  863. fail1:
  864. mutex_unlock(&efx->mac_lock);
  865. return rc;
  866. }
  867. static void efx_start_port(struct efx_nic *efx)
  868. {
  869. netif_dbg(efx, ifup, efx->net_dev, "start port\n");
  870. BUG_ON(efx->port_enabled);
  871. mutex_lock(&efx->mac_lock);
  872. efx->port_enabled = true;
  873. /* efx_mac_work() might have been scheduled after efx_stop_port(),
  874. * and then cancelled by efx_flush_all() */
  875. efx->type->reconfigure_mac(efx);
  876. mutex_unlock(&efx->mac_lock);
  877. }
  878. /* Prevent efx_mac_work() and efx_monitor() from working */
  879. static void efx_stop_port(struct efx_nic *efx)
  880. {
  881. netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
  882. mutex_lock(&efx->mac_lock);
  883. efx->port_enabled = false;
  884. mutex_unlock(&efx->mac_lock);
  885. /* Serialise against efx_set_multicast_list() */
  886. netif_addr_lock_bh(efx->net_dev);
  887. netif_addr_unlock_bh(efx->net_dev);
  888. }
  889. static void efx_fini_port(struct efx_nic *efx)
  890. {
  891. netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
  892. if (!efx->port_initialized)
  893. return;
  894. efx->phy_op->fini(efx);
  895. efx->port_initialized = false;
  896. efx->link_state.up = false;
  897. efx_link_status_changed(efx);
  898. }
  899. static void efx_remove_port(struct efx_nic *efx)
  900. {
  901. netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
  902. efx->type->remove_port(efx);
  903. }
  904. /**************************************************************************
  905. *
  906. * NIC handling
  907. *
  908. **************************************************************************/
  909. /* This configures the PCI device to enable I/O and DMA. */
  910. static int efx_init_io(struct efx_nic *efx)
  911. {
  912. struct pci_dev *pci_dev = efx->pci_dev;
  913. dma_addr_t dma_mask = efx->type->max_dma_mask;
  914. unsigned int mem_map_size = efx->type->mem_map_size(efx);
  915. int rc;
  916. netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
  917. rc = pci_enable_device(pci_dev);
  918. if (rc) {
  919. netif_err(efx, probe, efx->net_dev,
  920. "failed to enable PCI device\n");
  921. goto fail1;
  922. }
  923. pci_set_master(pci_dev);
  924. /* Set the PCI DMA mask. Try all possibilities from our
  925. * genuine mask down to 32 bits, because some architectures
  926. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  927. * masks event though they reject 46 bit masks.
  928. */
  929. while (dma_mask > 0x7fffffffUL) {
  930. if (dma_supported(&pci_dev->dev, dma_mask)) {
  931. rc = dma_set_mask(&pci_dev->dev, dma_mask);
  932. if (rc == 0)
  933. break;
  934. }
  935. dma_mask >>= 1;
  936. }
  937. if (rc) {
  938. netif_err(efx, probe, efx->net_dev,
  939. "could not find a suitable DMA mask\n");
  940. goto fail2;
  941. }
  942. netif_dbg(efx, probe, efx->net_dev,
  943. "using DMA mask %llx\n", (unsigned long long) dma_mask);
  944. rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
  945. if (rc) {
  946. /* dma_set_coherent_mask() is not *allowed* to
  947. * fail with a mask that dma_set_mask() accepted,
  948. * but just in case...
  949. */
  950. netif_err(efx, probe, efx->net_dev,
  951. "failed to set consistent DMA mask\n");
  952. goto fail2;
  953. }
  954. efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
  955. rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
  956. if (rc) {
  957. netif_err(efx, probe, efx->net_dev,
  958. "request for memory BAR failed\n");
  959. rc = -EIO;
  960. goto fail3;
  961. }
  962. efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
  963. if (!efx->membase) {
  964. netif_err(efx, probe, efx->net_dev,
  965. "could not map memory BAR at %llx+%x\n",
  966. (unsigned long long)efx->membase_phys, mem_map_size);
  967. rc = -ENOMEM;
  968. goto fail4;
  969. }
  970. netif_dbg(efx, probe, efx->net_dev,
  971. "memory BAR at %llx+%x (virtual %p)\n",
  972. (unsigned long long)efx->membase_phys, mem_map_size,
  973. efx->membase);
  974. return 0;
  975. fail4:
  976. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  977. fail3:
  978. efx->membase_phys = 0;
  979. fail2:
  980. pci_disable_device(efx->pci_dev);
  981. fail1:
  982. return rc;
  983. }
  984. static void efx_fini_io(struct efx_nic *efx)
  985. {
  986. netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
  987. if (efx->membase) {
  988. iounmap(efx->membase);
  989. efx->membase = NULL;
  990. }
  991. if (efx->membase_phys) {
  992. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  993. efx->membase_phys = 0;
  994. }
  995. pci_disable_device(efx->pci_dev);
  996. }
  997. static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
  998. {
  999. cpumask_var_t thread_mask;
  1000. unsigned int count;
  1001. int cpu;
  1002. if (rss_cpus) {
  1003. count = rss_cpus;
  1004. } else {
  1005. if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
  1006. netif_warn(efx, probe, efx->net_dev,
  1007. "RSS disabled due to allocation failure\n");
  1008. return 1;
  1009. }
  1010. count = 0;
  1011. for_each_online_cpu(cpu) {
  1012. if (!cpumask_test_cpu(cpu, thread_mask)) {
  1013. ++count;
  1014. cpumask_or(thread_mask, thread_mask,
  1015. topology_thread_cpumask(cpu));
  1016. }
  1017. }
  1018. free_cpumask_var(thread_mask);
  1019. }
  1020. /* If RSS is requested for the PF *and* VFs then we can't write RSS
  1021. * table entries that are inaccessible to VFs
  1022. */
  1023. if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
  1024. count > efx_vf_size(efx)) {
  1025. netif_warn(efx, probe, efx->net_dev,
  1026. "Reducing number of RSS channels from %u to %u for "
  1027. "VF support. Increase vf-msix-limit to use more "
  1028. "channels on the PF.\n",
  1029. count, efx_vf_size(efx));
  1030. count = efx_vf_size(efx);
  1031. }
  1032. return count;
  1033. }
  1034. /* Probe the number and type of interrupts we are able to obtain, and
  1035. * the resulting numbers of channels and RX queues.
  1036. */
  1037. static int efx_probe_interrupts(struct efx_nic *efx)
  1038. {
  1039. unsigned int extra_channels = 0;
  1040. unsigned int i, j;
  1041. int rc;
  1042. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
  1043. if (efx->extra_channel_type[i])
  1044. ++extra_channels;
  1045. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  1046. struct msix_entry xentries[EFX_MAX_CHANNELS];
  1047. unsigned int n_channels;
  1048. n_channels = efx_wanted_parallelism(efx);
  1049. if (separate_tx_channels)
  1050. n_channels *= 2;
  1051. n_channels += extra_channels;
  1052. n_channels = min(n_channels, efx->max_channels);
  1053. for (i = 0; i < n_channels; i++)
  1054. xentries[i].entry = i;
  1055. rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
  1056. if (rc > 0) {
  1057. netif_err(efx, drv, efx->net_dev,
  1058. "WARNING: Insufficient MSI-X vectors"
  1059. " available (%d < %u).\n", rc, n_channels);
  1060. netif_err(efx, drv, efx->net_dev,
  1061. "WARNING: Performance may be reduced.\n");
  1062. EFX_BUG_ON_PARANOID(rc >= n_channels);
  1063. n_channels = rc;
  1064. rc = pci_enable_msix(efx->pci_dev, xentries,
  1065. n_channels);
  1066. }
  1067. if (rc == 0) {
  1068. efx->n_channels = n_channels;
  1069. if (n_channels > extra_channels)
  1070. n_channels -= extra_channels;
  1071. if (separate_tx_channels) {
  1072. efx->n_tx_channels = max(n_channels / 2, 1U);
  1073. efx->n_rx_channels = max(n_channels -
  1074. efx->n_tx_channels,
  1075. 1U);
  1076. } else {
  1077. efx->n_tx_channels = n_channels;
  1078. efx->n_rx_channels = n_channels;
  1079. }
  1080. for (i = 0; i < efx->n_channels; i++)
  1081. efx_get_channel(efx, i)->irq =
  1082. xentries[i].vector;
  1083. } else {
  1084. /* Fall back to single channel MSI */
  1085. efx->interrupt_mode = EFX_INT_MODE_MSI;
  1086. netif_err(efx, drv, efx->net_dev,
  1087. "could not enable MSI-X\n");
  1088. }
  1089. }
  1090. /* Try single interrupt MSI */
  1091. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  1092. efx->n_channels = 1;
  1093. efx->n_rx_channels = 1;
  1094. efx->n_tx_channels = 1;
  1095. rc = pci_enable_msi(efx->pci_dev);
  1096. if (rc == 0) {
  1097. efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
  1098. } else {
  1099. netif_err(efx, drv, efx->net_dev,
  1100. "could not enable MSI\n");
  1101. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  1102. }
  1103. }
  1104. /* Assume legacy interrupts */
  1105. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  1106. efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
  1107. efx->n_rx_channels = 1;
  1108. efx->n_tx_channels = 1;
  1109. efx->legacy_irq = efx->pci_dev->irq;
  1110. }
  1111. /* Assign extra channels if possible */
  1112. j = efx->n_channels;
  1113. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
  1114. if (!efx->extra_channel_type[i])
  1115. continue;
  1116. if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
  1117. efx->n_channels <= extra_channels) {
  1118. efx->extra_channel_type[i]->handle_no_channel(efx);
  1119. } else {
  1120. --j;
  1121. efx_get_channel(efx, j)->type =
  1122. efx->extra_channel_type[i];
  1123. }
  1124. }
  1125. /* RSS might be usable on VFs even if it is disabled on the PF */
  1126. efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
  1127. efx->n_rx_channels : efx_vf_size(efx));
  1128. return 0;
  1129. }
  1130. static void efx_soft_enable_interrupts(struct efx_nic *efx)
  1131. {
  1132. struct efx_channel *channel;
  1133. BUG_ON(efx->state == STATE_DISABLED);
  1134. efx->irq_soft_enabled = true;
  1135. smp_wmb();
  1136. efx_for_each_channel(channel, efx) {
  1137. if (!channel->type->keep_eventq)
  1138. efx_init_eventq(channel);
  1139. efx_start_eventq(channel);
  1140. }
  1141. efx_mcdi_mode_event(efx);
  1142. }
  1143. static void efx_soft_disable_interrupts(struct efx_nic *efx)
  1144. {
  1145. struct efx_channel *channel;
  1146. if (efx->state == STATE_DISABLED)
  1147. return;
  1148. efx_mcdi_mode_poll(efx);
  1149. efx->irq_soft_enabled = false;
  1150. smp_wmb();
  1151. if (efx->legacy_irq)
  1152. synchronize_irq(efx->legacy_irq);
  1153. efx_for_each_channel(channel, efx) {
  1154. if (channel->irq)
  1155. synchronize_irq(channel->irq);
  1156. efx_stop_eventq(channel);
  1157. if (!channel->type->keep_eventq)
  1158. efx_fini_eventq(channel);
  1159. }
  1160. }
  1161. static void efx_enable_interrupts(struct efx_nic *efx)
  1162. {
  1163. struct efx_channel *channel;
  1164. BUG_ON(efx->state == STATE_DISABLED);
  1165. if (efx->eeh_disabled_legacy_irq) {
  1166. enable_irq(efx->legacy_irq);
  1167. efx->eeh_disabled_legacy_irq = false;
  1168. }
  1169. efx->type->irq_enable_master(efx);
  1170. efx_for_each_channel(channel, efx) {
  1171. if (channel->type->keep_eventq)
  1172. efx_init_eventq(channel);
  1173. }
  1174. efx_soft_enable_interrupts(efx);
  1175. }
  1176. static void efx_disable_interrupts(struct efx_nic *efx)
  1177. {
  1178. struct efx_channel *channel;
  1179. efx_soft_disable_interrupts(efx);
  1180. efx_for_each_channel(channel, efx) {
  1181. if (channel->type->keep_eventq)
  1182. efx_fini_eventq(channel);
  1183. }
  1184. efx->type->irq_disable_non_ev(efx);
  1185. }
  1186. static void efx_remove_interrupts(struct efx_nic *efx)
  1187. {
  1188. struct efx_channel *channel;
  1189. /* Remove MSI/MSI-X interrupts */
  1190. efx_for_each_channel(channel, efx)
  1191. channel->irq = 0;
  1192. pci_disable_msi(efx->pci_dev);
  1193. pci_disable_msix(efx->pci_dev);
  1194. /* Remove legacy interrupt */
  1195. efx->legacy_irq = 0;
  1196. }
  1197. static void efx_set_channels(struct efx_nic *efx)
  1198. {
  1199. struct efx_channel *channel;
  1200. struct efx_tx_queue *tx_queue;
  1201. efx->tx_channel_offset =
  1202. separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
  1203. /* We need to mark which channels really have RX and TX
  1204. * queues, and adjust the TX queue numbers if we have separate
  1205. * RX-only and TX-only channels.
  1206. */
  1207. efx_for_each_channel(channel, efx) {
  1208. if (channel->channel < efx->n_rx_channels)
  1209. channel->rx_queue.core_index = channel->channel;
  1210. else
  1211. channel->rx_queue.core_index = -1;
  1212. efx_for_each_channel_tx_queue(tx_queue, channel)
  1213. tx_queue->queue -= (efx->tx_channel_offset *
  1214. EFX_TXQ_TYPES);
  1215. }
  1216. }
  1217. static int efx_probe_nic(struct efx_nic *efx)
  1218. {
  1219. size_t i;
  1220. int rc;
  1221. netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
  1222. /* Carry out hardware-type specific initialisation */
  1223. rc = efx->type->probe(efx);
  1224. if (rc)
  1225. return rc;
  1226. /* Determine the number of channels and queues by trying to hook
  1227. * in MSI-X interrupts. */
  1228. rc = efx_probe_interrupts(efx);
  1229. if (rc)
  1230. goto fail;
  1231. efx->type->dimension_resources(efx);
  1232. if (efx->n_channels > 1)
  1233. get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
  1234. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
  1235. efx->rx_indir_table[i] =
  1236. ethtool_rxfh_indir_default(i, efx->rss_spread);
  1237. efx_set_channels(efx);
  1238. netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
  1239. netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
  1240. /* Initialise the interrupt moderation settings */
  1241. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
  1242. true);
  1243. return 0;
  1244. fail:
  1245. efx->type->remove(efx);
  1246. return rc;
  1247. }
  1248. static void efx_remove_nic(struct efx_nic *efx)
  1249. {
  1250. netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
  1251. efx_remove_interrupts(efx);
  1252. efx->type->remove(efx);
  1253. }
  1254. static int efx_probe_filters(struct efx_nic *efx)
  1255. {
  1256. int rc;
  1257. spin_lock_init(&efx->filter_lock);
  1258. rc = efx->type->filter_table_probe(efx);
  1259. if (rc)
  1260. return rc;
  1261. #ifdef CONFIG_RFS_ACCEL
  1262. if (efx->type->offload_features & NETIF_F_NTUPLE) {
  1263. efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
  1264. sizeof(*efx->rps_flow_id),
  1265. GFP_KERNEL);
  1266. if (!efx->rps_flow_id) {
  1267. efx->type->filter_table_remove(efx);
  1268. return -ENOMEM;
  1269. }
  1270. }
  1271. #endif
  1272. return 0;
  1273. }
  1274. static void efx_remove_filters(struct efx_nic *efx)
  1275. {
  1276. #ifdef CONFIG_RFS_ACCEL
  1277. kfree(efx->rps_flow_id);
  1278. #endif
  1279. efx->type->filter_table_remove(efx);
  1280. }
  1281. static void efx_restore_filters(struct efx_nic *efx)
  1282. {
  1283. efx->type->filter_table_restore(efx);
  1284. }
  1285. /**************************************************************************
  1286. *
  1287. * NIC startup/shutdown
  1288. *
  1289. *************************************************************************/
  1290. static int efx_probe_all(struct efx_nic *efx)
  1291. {
  1292. int rc;
  1293. rc = efx_probe_nic(efx);
  1294. if (rc) {
  1295. netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
  1296. goto fail1;
  1297. }
  1298. rc = efx_probe_port(efx);
  1299. if (rc) {
  1300. netif_err(efx, probe, efx->net_dev, "failed to create port\n");
  1301. goto fail2;
  1302. }
  1303. BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
  1304. if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
  1305. rc = -EINVAL;
  1306. goto fail3;
  1307. }
  1308. efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
  1309. rc = efx_probe_filters(efx);
  1310. if (rc) {
  1311. netif_err(efx, probe, efx->net_dev,
  1312. "failed to create filter tables\n");
  1313. goto fail3;
  1314. }
  1315. rc = efx_probe_channels(efx);
  1316. if (rc)
  1317. goto fail4;
  1318. return 0;
  1319. fail4:
  1320. efx_remove_filters(efx);
  1321. fail3:
  1322. efx_remove_port(efx);
  1323. fail2:
  1324. efx_remove_nic(efx);
  1325. fail1:
  1326. return rc;
  1327. }
  1328. /* If the interface is supposed to be running but is not, start
  1329. * the hardware and software data path, regular activity for the port
  1330. * (MAC statistics, link polling, etc.) and schedule the port to be
  1331. * reconfigured. Interrupts must already be enabled. This function
  1332. * is safe to call multiple times, so long as the NIC is not disabled.
  1333. * Requires the RTNL lock.
  1334. */
  1335. static void efx_start_all(struct efx_nic *efx)
  1336. {
  1337. EFX_ASSERT_RESET_SERIALISED(efx);
  1338. BUG_ON(efx->state == STATE_DISABLED);
  1339. /* Check that it is appropriate to restart the interface. All
  1340. * of these flags are safe to read under just the rtnl lock */
  1341. if (efx->port_enabled || !netif_running(efx->net_dev))
  1342. return;
  1343. efx_start_port(efx);
  1344. efx_start_datapath(efx);
  1345. /* Start the hardware monitor if there is one */
  1346. if (efx->type->monitor != NULL)
  1347. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1348. efx_monitor_interval);
  1349. /* If link state detection is normally event-driven, we have
  1350. * to poll now because we could have missed a change
  1351. */
  1352. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
  1353. mutex_lock(&efx->mac_lock);
  1354. if (efx->phy_op->poll(efx))
  1355. efx_link_status_changed(efx);
  1356. mutex_unlock(&efx->mac_lock);
  1357. }
  1358. efx->type->start_stats(efx);
  1359. }
  1360. /* Flush all delayed work. Should only be called when no more delayed work
  1361. * will be scheduled. This doesn't flush pending online resets (efx_reset),
  1362. * since we're holding the rtnl_lock at this point. */
  1363. static void efx_flush_all(struct efx_nic *efx)
  1364. {
  1365. /* Make sure the hardware monitor and event self-test are stopped */
  1366. cancel_delayed_work_sync(&efx->monitor_work);
  1367. efx_selftest_async_cancel(efx);
  1368. /* Stop scheduled port reconfigurations */
  1369. cancel_work_sync(&efx->mac_work);
  1370. }
  1371. /* Quiesce the hardware and software data path, and regular activity
  1372. * for the port without bringing the link down. Safe to call multiple
  1373. * times with the NIC in almost any state, but interrupts should be
  1374. * enabled. Requires the RTNL lock.
  1375. */
  1376. static void efx_stop_all(struct efx_nic *efx)
  1377. {
  1378. EFX_ASSERT_RESET_SERIALISED(efx);
  1379. /* port_enabled can be read safely under the rtnl lock */
  1380. if (!efx->port_enabled)
  1381. return;
  1382. efx->type->stop_stats(efx);
  1383. efx_stop_port(efx);
  1384. /* Flush efx_mac_work(), refill_workqueue, monitor_work */
  1385. efx_flush_all(efx);
  1386. /* Stop the kernel transmit interface. This is only valid if
  1387. * the device is stopped or detached; otherwise the watchdog
  1388. * may fire immediately.
  1389. */
  1390. WARN_ON(netif_running(efx->net_dev) &&
  1391. netif_device_present(efx->net_dev));
  1392. netif_tx_disable(efx->net_dev);
  1393. efx_stop_datapath(efx);
  1394. }
  1395. static void efx_remove_all(struct efx_nic *efx)
  1396. {
  1397. efx_remove_channels(efx);
  1398. efx_remove_filters(efx);
  1399. efx_remove_port(efx);
  1400. efx_remove_nic(efx);
  1401. }
  1402. /**************************************************************************
  1403. *
  1404. * Interrupt moderation
  1405. *
  1406. **************************************************************************/
  1407. static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
  1408. {
  1409. if (usecs == 0)
  1410. return 0;
  1411. if (usecs * 1000 < quantum_ns)
  1412. return 1; /* never round down to 0 */
  1413. return usecs * 1000 / quantum_ns;
  1414. }
  1415. /* Set interrupt moderation parameters */
  1416. int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
  1417. unsigned int rx_usecs, bool rx_adaptive,
  1418. bool rx_may_override_tx)
  1419. {
  1420. struct efx_channel *channel;
  1421. unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
  1422. efx->timer_quantum_ns,
  1423. 1000);
  1424. unsigned int tx_ticks;
  1425. unsigned int rx_ticks;
  1426. EFX_ASSERT_RESET_SERIALISED(efx);
  1427. if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
  1428. return -EINVAL;
  1429. tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
  1430. rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
  1431. if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
  1432. !rx_may_override_tx) {
  1433. netif_err(efx, drv, efx->net_dev, "Channels are shared. "
  1434. "RX and TX IRQ moderation must be equal\n");
  1435. return -EINVAL;
  1436. }
  1437. efx->irq_rx_adaptive = rx_adaptive;
  1438. efx->irq_rx_moderation = rx_ticks;
  1439. efx_for_each_channel(channel, efx) {
  1440. if (efx_channel_has_rx_queue(channel))
  1441. channel->irq_moderation = rx_ticks;
  1442. else if (efx_channel_has_tx_queues(channel))
  1443. channel->irq_moderation = tx_ticks;
  1444. }
  1445. return 0;
  1446. }
  1447. void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
  1448. unsigned int *rx_usecs, bool *rx_adaptive)
  1449. {
  1450. /* We must round up when converting ticks to microseconds
  1451. * because we round down when converting the other way.
  1452. */
  1453. *rx_adaptive = efx->irq_rx_adaptive;
  1454. *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
  1455. efx->timer_quantum_ns,
  1456. 1000);
  1457. /* If channels are shared between RX and TX, so is IRQ
  1458. * moderation. Otherwise, IRQ moderation is the same for all
  1459. * TX channels and is not adaptive.
  1460. */
  1461. if (efx->tx_channel_offset == 0)
  1462. *tx_usecs = *rx_usecs;
  1463. else
  1464. *tx_usecs = DIV_ROUND_UP(
  1465. efx->channel[efx->tx_channel_offset]->irq_moderation *
  1466. efx->timer_quantum_ns,
  1467. 1000);
  1468. }
  1469. /**************************************************************************
  1470. *
  1471. * Hardware monitor
  1472. *
  1473. **************************************************************************/
  1474. /* Run periodically off the general workqueue */
  1475. static void efx_monitor(struct work_struct *data)
  1476. {
  1477. struct efx_nic *efx = container_of(data, struct efx_nic,
  1478. monitor_work.work);
  1479. netif_vdbg(efx, timer, efx->net_dev,
  1480. "hardware monitor executing on CPU %d\n",
  1481. raw_smp_processor_id());
  1482. BUG_ON(efx->type->monitor == NULL);
  1483. /* If the mac_lock is already held then it is likely a port
  1484. * reconfiguration is already in place, which will likely do
  1485. * most of the work of monitor() anyway. */
  1486. if (mutex_trylock(&efx->mac_lock)) {
  1487. if (efx->port_enabled)
  1488. efx->type->monitor(efx);
  1489. mutex_unlock(&efx->mac_lock);
  1490. }
  1491. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1492. efx_monitor_interval);
  1493. }
  1494. /**************************************************************************
  1495. *
  1496. * ioctls
  1497. *
  1498. *************************************************************************/
  1499. /* Net device ioctl
  1500. * Context: process, rtnl_lock() held.
  1501. */
  1502. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1503. {
  1504. struct efx_nic *efx = netdev_priv(net_dev);
  1505. struct mii_ioctl_data *data = if_mii(ifr);
  1506. if (cmd == SIOCSHWTSTAMP)
  1507. return efx_ptp_ioctl(efx, ifr, cmd);
  1508. /* Convert phy_id from older PRTAD/DEVAD format */
  1509. if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
  1510. (data->phy_id & 0xfc00) == 0x0400)
  1511. data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
  1512. return mdio_mii_ioctl(&efx->mdio, data, cmd);
  1513. }
  1514. /**************************************************************************
  1515. *
  1516. * NAPI interface
  1517. *
  1518. **************************************************************************/
  1519. static void efx_init_napi_channel(struct efx_channel *channel)
  1520. {
  1521. struct efx_nic *efx = channel->efx;
  1522. channel->napi_dev = efx->net_dev;
  1523. netif_napi_add(channel->napi_dev, &channel->napi_str,
  1524. efx_poll, napi_weight);
  1525. }
  1526. static void efx_init_napi(struct efx_nic *efx)
  1527. {
  1528. struct efx_channel *channel;
  1529. efx_for_each_channel(channel, efx)
  1530. efx_init_napi_channel(channel);
  1531. }
  1532. static void efx_fini_napi_channel(struct efx_channel *channel)
  1533. {
  1534. if (channel->napi_dev)
  1535. netif_napi_del(&channel->napi_str);
  1536. channel->napi_dev = NULL;
  1537. }
  1538. static void efx_fini_napi(struct efx_nic *efx)
  1539. {
  1540. struct efx_channel *channel;
  1541. efx_for_each_channel(channel, efx)
  1542. efx_fini_napi_channel(channel);
  1543. }
  1544. /**************************************************************************
  1545. *
  1546. * Kernel netpoll interface
  1547. *
  1548. *************************************************************************/
  1549. #ifdef CONFIG_NET_POLL_CONTROLLER
  1550. /* Although in the common case interrupts will be disabled, this is not
  1551. * guaranteed. However, all our work happens inside the NAPI callback,
  1552. * so no locking is required.
  1553. */
  1554. static void efx_netpoll(struct net_device *net_dev)
  1555. {
  1556. struct efx_nic *efx = netdev_priv(net_dev);
  1557. struct efx_channel *channel;
  1558. efx_for_each_channel(channel, efx)
  1559. efx_schedule_channel(channel);
  1560. }
  1561. #endif
  1562. /**************************************************************************
  1563. *
  1564. * Kernel net device interface
  1565. *
  1566. *************************************************************************/
  1567. /* Context: process, rtnl_lock() held. */
  1568. static int efx_net_open(struct net_device *net_dev)
  1569. {
  1570. struct efx_nic *efx = netdev_priv(net_dev);
  1571. int rc;
  1572. netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
  1573. raw_smp_processor_id());
  1574. rc = efx_check_disabled(efx);
  1575. if (rc)
  1576. return rc;
  1577. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1578. return -EBUSY;
  1579. if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
  1580. return -EIO;
  1581. /* Notify the kernel of the link state polled during driver load,
  1582. * before the monitor starts running */
  1583. efx_link_status_changed(efx);
  1584. efx_start_all(efx);
  1585. efx_selftest_async_start(efx);
  1586. return 0;
  1587. }
  1588. /* Context: process, rtnl_lock() held.
  1589. * Note that the kernel will ignore our return code; this method
  1590. * should really be a void.
  1591. */
  1592. static int efx_net_stop(struct net_device *net_dev)
  1593. {
  1594. struct efx_nic *efx = netdev_priv(net_dev);
  1595. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  1596. raw_smp_processor_id());
  1597. /* Stop the device and flush all the channels */
  1598. efx_stop_all(efx);
  1599. return 0;
  1600. }
  1601. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1602. static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
  1603. struct rtnl_link_stats64 *stats)
  1604. {
  1605. struct efx_nic *efx = netdev_priv(net_dev);
  1606. spin_lock_bh(&efx->stats_lock);
  1607. efx->type->update_stats(efx, NULL, stats);
  1608. spin_unlock_bh(&efx->stats_lock);
  1609. return stats;
  1610. }
  1611. /* Context: netif_tx_lock held, BHs disabled. */
  1612. static void efx_watchdog(struct net_device *net_dev)
  1613. {
  1614. struct efx_nic *efx = netdev_priv(net_dev);
  1615. netif_err(efx, tx_err, efx->net_dev,
  1616. "TX stuck with port_enabled=%d: resetting channels\n",
  1617. efx->port_enabled);
  1618. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1619. }
  1620. /* Context: process, rtnl_lock() held. */
  1621. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1622. {
  1623. struct efx_nic *efx = netdev_priv(net_dev);
  1624. int rc;
  1625. rc = efx_check_disabled(efx);
  1626. if (rc)
  1627. return rc;
  1628. if (new_mtu > EFX_MAX_MTU)
  1629. return -EINVAL;
  1630. netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
  1631. efx_device_detach_sync(efx);
  1632. efx_stop_all(efx);
  1633. mutex_lock(&efx->mac_lock);
  1634. net_dev->mtu = new_mtu;
  1635. efx->type->reconfigure_mac(efx);
  1636. mutex_unlock(&efx->mac_lock);
  1637. efx_start_all(efx);
  1638. netif_device_attach(efx->net_dev);
  1639. return 0;
  1640. }
  1641. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1642. {
  1643. struct efx_nic *efx = netdev_priv(net_dev);
  1644. struct sockaddr *addr = data;
  1645. char *new_addr = addr->sa_data;
  1646. if (!is_valid_ether_addr(new_addr)) {
  1647. netif_err(efx, drv, efx->net_dev,
  1648. "invalid ethernet MAC address requested: %pM\n",
  1649. new_addr);
  1650. return -EADDRNOTAVAIL;
  1651. }
  1652. memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
  1653. efx_sriov_mac_address_changed(efx);
  1654. /* Reconfigure the MAC */
  1655. mutex_lock(&efx->mac_lock);
  1656. efx->type->reconfigure_mac(efx);
  1657. mutex_unlock(&efx->mac_lock);
  1658. return 0;
  1659. }
  1660. /* Context: netif_addr_lock held, BHs disabled. */
  1661. static void efx_set_rx_mode(struct net_device *net_dev)
  1662. {
  1663. struct efx_nic *efx = netdev_priv(net_dev);
  1664. if (efx->port_enabled)
  1665. queue_work(efx->workqueue, &efx->mac_work);
  1666. /* Otherwise efx_start_port() will do this */
  1667. }
  1668. static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
  1669. {
  1670. struct efx_nic *efx = netdev_priv(net_dev);
  1671. /* If disabling RX n-tuple filtering, clear existing filters */
  1672. if (net_dev->features & ~data & NETIF_F_NTUPLE)
  1673. efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
  1674. return 0;
  1675. }
  1676. static const struct net_device_ops efx_netdev_ops = {
  1677. .ndo_open = efx_net_open,
  1678. .ndo_stop = efx_net_stop,
  1679. .ndo_get_stats64 = efx_net_stats,
  1680. .ndo_tx_timeout = efx_watchdog,
  1681. .ndo_start_xmit = efx_hard_start_xmit,
  1682. .ndo_validate_addr = eth_validate_addr,
  1683. .ndo_do_ioctl = efx_ioctl,
  1684. .ndo_change_mtu = efx_change_mtu,
  1685. .ndo_set_mac_address = efx_set_mac_address,
  1686. .ndo_set_rx_mode = efx_set_rx_mode,
  1687. .ndo_set_features = efx_set_features,
  1688. #ifdef CONFIG_SFC_SRIOV
  1689. .ndo_set_vf_mac = efx_sriov_set_vf_mac,
  1690. .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
  1691. .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
  1692. .ndo_get_vf_config = efx_sriov_get_vf_config,
  1693. #endif
  1694. #ifdef CONFIG_NET_POLL_CONTROLLER
  1695. .ndo_poll_controller = efx_netpoll,
  1696. #endif
  1697. .ndo_setup_tc = efx_setup_tc,
  1698. #ifdef CONFIG_RFS_ACCEL
  1699. .ndo_rx_flow_steer = efx_filter_rfs,
  1700. #endif
  1701. };
  1702. static void efx_update_name(struct efx_nic *efx)
  1703. {
  1704. strcpy(efx->name, efx->net_dev->name);
  1705. efx_mtd_rename(efx);
  1706. efx_set_channel_names(efx);
  1707. }
  1708. static int efx_netdev_event(struct notifier_block *this,
  1709. unsigned long event, void *ptr)
  1710. {
  1711. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  1712. if (net_dev->netdev_ops == &efx_netdev_ops &&
  1713. event == NETDEV_CHANGENAME)
  1714. efx_update_name(netdev_priv(net_dev));
  1715. return NOTIFY_DONE;
  1716. }
  1717. static struct notifier_block efx_netdev_notifier = {
  1718. .notifier_call = efx_netdev_event,
  1719. };
  1720. static ssize_t
  1721. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  1722. {
  1723. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1724. return sprintf(buf, "%d\n", efx->phy_type);
  1725. }
  1726. static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
  1727. static int efx_register_netdev(struct efx_nic *efx)
  1728. {
  1729. struct net_device *net_dev = efx->net_dev;
  1730. struct efx_channel *channel;
  1731. int rc;
  1732. net_dev->watchdog_timeo = 5 * HZ;
  1733. net_dev->irq = efx->pci_dev->irq;
  1734. net_dev->netdev_ops = &efx_netdev_ops;
  1735. SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
  1736. net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
  1737. rtnl_lock();
  1738. /* Enable resets to be scheduled and check whether any were
  1739. * already requested. If so, the NIC is probably hosed so we
  1740. * abort.
  1741. */
  1742. efx->state = STATE_READY;
  1743. smp_mb(); /* ensure we change state before checking reset_pending */
  1744. if (efx->reset_pending) {
  1745. netif_err(efx, probe, efx->net_dev,
  1746. "aborting probe due to scheduled reset\n");
  1747. rc = -EIO;
  1748. goto fail_locked;
  1749. }
  1750. rc = dev_alloc_name(net_dev, net_dev->name);
  1751. if (rc < 0)
  1752. goto fail_locked;
  1753. efx_update_name(efx);
  1754. /* Always start with carrier off; PHY events will detect the link */
  1755. netif_carrier_off(net_dev);
  1756. rc = register_netdevice(net_dev);
  1757. if (rc)
  1758. goto fail_locked;
  1759. efx_for_each_channel(channel, efx) {
  1760. struct efx_tx_queue *tx_queue;
  1761. efx_for_each_channel_tx_queue(tx_queue, channel)
  1762. efx_init_tx_queue_core_txq(tx_queue);
  1763. }
  1764. rtnl_unlock();
  1765. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1766. if (rc) {
  1767. netif_err(efx, drv, efx->net_dev,
  1768. "failed to init net dev attributes\n");
  1769. goto fail_registered;
  1770. }
  1771. return 0;
  1772. fail_registered:
  1773. rtnl_lock();
  1774. unregister_netdevice(net_dev);
  1775. fail_locked:
  1776. efx->state = STATE_UNINIT;
  1777. rtnl_unlock();
  1778. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  1779. return rc;
  1780. }
  1781. static void efx_unregister_netdev(struct efx_nic *efx)
  1782. {
  1783. if (!efx->net_dev)
  1784. return;
  1785. BUG_ON(netdev_priv(efx->net_dev) != efx);
  1786. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  1787. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1788. rtnl_lock();
  1789. unregister_netdevice(efx->net_dev);
  1790. efx->state = STATE_UNINIT;
  1791. rtnl_unlock();
  1792. }
  1793. /**************************************************************************
  1794. *
  1795. * Device reset and suspend
  1796. *
  1797. **************************************************************************/
  1798. /* Tears down the entire software state and most of the hardware state
  1799. * before reset. */
  1800. void efx_reset_down(struct efx_nic *efx, enum reset_type method)
  1801. {
  1802. EFX_ASSERT_RESET_SERIALISED(efx);
  1803. efx_stop_all(efx);
  1804. efx_disable_interrupts(efx);
  1805. mutex_lock(&efx->mac_lock);
  1806. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
  1807. efx->phy_op->fini(efx);
  1808. efx->type->fini(efx);
  1809. }
  1810. /* This function will always ensure that the locks acquired in
  1811. * efx_reset_down() are released. A failure return code indicates
  1812. * that we were unable to reinitialise the hardware, and the
  1813. * driver should be disabled. If ok is false, then the rx and tx
  1814. * engines are not restarted, pending a RESET_DISABLE. */
  1815. int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
  1816. {
  1817. int rc;
  1818. EFX_ASSERT_RESET_SERIALISED(efx);
  1819. rc = efx->type->init(efx);
  1820. if (rc) {
  1821. netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
  1822. goto fail;
  1823. }
  1824. if (!ok)
  1825. goto fail;
  1826. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
  1827. rc = efx->phy_op->init(efx);
  1828. if (rc)
  1829. goto fail;
  1830. if (efx->phy_op->reconfigure(efx))
  1831. netif_err(efx, drv, efx->net_dev,
  1832. "could not restore PHY settings\n");
  1833. }
  1834. efx_enable_interrupts(efx);
  1835. efx_restore_filters(efx);
  1836. efx_sriov_reset(efx);
  1837. mutex_unlock(&efx->mac_lock);
  1838. efx_start_all(efx);
  1839. return 0;
  1840. fail:
  1841. efx->port_initialized = false;
  1842. mutex_unlock(&efx->mac_lock);
  1843. return rc;
  1844. }
  1845. /* Reset the NIC using the specified method. Note that the reset may
  1846. * fail, in which case the card will be left in an unusable state.
  1847. *
  1848. * Caller must hold the rtnl_lock.
  1849. */
  1850. int efx_reset(struct efx_nic *efx, enum reset_type method)
  1851. {
  1852. int rc, rc2;
  1853. bool disabled;
  1854. netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
  1855. RESET_TYPE(method));
  1856. efx_device_detach_sync(efx);
  1857. efx_reset_down(efx, method);
  1858. rc = efx->type->reset(efx, method);
  1859. if (rc) {
  1860. netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
  1861. goto out;
  1862. }
  1863. /* Clear flags for the scopes we covered. We assume the NIC and
  1864. * driver are now quiescent so that there is no race here.
  1865. */
  1866. efx->reset_pending &= -(1 << (method + 1));
  1867. /* Reinitialise bus-mastering, which may have been turned off before
  1868. * the reset was scheduled. This is still appropriate, even in the
  1869. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  1870. * can respond to requests. */
  1871. pci_set_master(efx->pci_dev);
  1872. out:
  1873. /* Leave device stopped if necessary */
  1874. disabled = rc ||
  1875. method == RESET_TYPE_DISABLE ||
  1876. method == RESET_TYPE_RECOVER_OR_DISABLE;
  1877. rc2 = efx_reset_up(efx, method, !disabled);
  1878. if (rc2) {
  1879. disabled = true;
  1880. if (!rc)
  1881. rc = rc2;
  1882. }
  1883. if (disabled) {
  1884. dev_close(efx->net_dev);
  1885. netif_err(efx, drv, efx->net_dev, "has been disabled\n");
  1886. efx->state = STATE_DISABLED;
  1887. } else {
  1888. netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
  1889. netif_device_attach(efx->net_dev);
  1890. }
  1891. return rc;
  1892. }
  1893. /* Try recovery mechanisms.
  1894. * For now only EEH is supported.
  1895. * Returns 0 if the recovery mechanisms are unsuccessful.
  1896. * Returns a non-zero value otherwise.
  1897. */
  1898. int efx_try_recovery(struct efx_nic *efx)
  1899. {
  1900. #ifdef CONFIG_EEH
  1901. /* A PCI error can occur and not be seen by EEH because nothing
  1902. * happens on the PCI bus. In this case the driver may fail and
  1903. * schedule a 'recover or reset', leading to this recovery handler.
  1904. * Manually call the eeh failure check function.
  1905. */
  1906. struct eeh_dev *eehdev =
  1907. of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
  1908. if (eeh_dev_check_failure(eehdev)) {
  1909. /* The EEH mechanisms will handle the error and reset the
  1910. * device if necessary.
  1911. */
  1912. return 1;
  1913. }
  1914. #endif
  1915. return 0;
  1916. }
  1917. /* The worker thread exists so that code that cannot sleep can
  1918. * schedule a reset for later.
  1919. */
  1920. static void efx_reset_work(struct work_struct *data)
  1921. {
  1922. struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
  1923. unsigned long pending;
  1924. enum reset_type method;
  1925. pending = ACCESS_ONCE(efx->reset_pending);
  1926. method = fls(pending) - 1;
  1927. if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
  1928. method == RESET_TYPE_RECOVER_OR_ALL) &&
  1929. efx_try_recovery(efx))
  1930. return;
  1931. if (!pending)
  1932. return;
  1933. rtnl_lock();
  1934. /* We checked the state in efx_schedule_reset() but it may
  1935. * have changed by now. Now that we have the RTNL lock,
  1936. * it cannot change again.
  1937. */
  1938. if (efx->state == STATE_READY)
  1939. (void)efx_reset(efx, method);
  1940. rtnl_unlock();
  1941. }
  1942. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  1943. {
  1944. enum reset_type method;
  1945. if (efx->state == STATE_RECOVERY) {
  1946. netif_dbg(efx, drv, efx->net_dev,
  1947. "recovering: skip scheduling %s reset\n",
  1948. RESET_TYPE(type));
  1949. return;
  1950. }
  1951. switch (type) {
  1952. case RESET_TYPE_INVISIBLE:
  1953. case RESET_TYPE_ALL:
  1954. case RESET_TYPE_RECOVER_OR_ALL:
  1955. case RESET_TYPE_WORLD:
  1956. case RESET_TYPE_DISABLE:
  1957. case RESET_TYPE_RECOVER_OR_DISABLE:
  1958. method = type;
  1959. netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
  1960. RESET_TYPE(method));
  1961. break;
  1962. default:
  1963. method = efx->type->map_reset_reason(type);
  1964. netif_dbg(efx, drv, efx->net_dev,
  1965. "scheduling %s reset for %s\n",
  1966. RESET_TYPE(method), RESET_TYPE(type));
  1967. break;
  1968. }
  1969. set_bit(method, &efx->reset_pending);
  1970. smp_mb(); /* ensure we change reset_pending before checking state */
  1971. /* If we're not READY then just leave the flags set as the cue
  1972. * to abort probing or reschedule the reset later.
  1973. */
  1974. if (ACCESS_ONCE(efx->state) != STATE_READY)
  1975. return;
  1976. /* efx_process_channel() will no longer read events once a
  1977. * reset is scheduled. So switch back to poll'd MCDI completions. */
  1978. efx_mcdi_mode_poll(efx);
  1979. queue_work(reset_workqueue, &efx->reset_work);
  1980. }
  1981. /**************************************************************************
  1982. *
  1983. * List of NICs we support
  1984. *
  1985. **************************************************************************/
  1986. /* PCI device ID table */
  1987. static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
  1988. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  1989. PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
  1990. .driver_data = (unsigned long) &falcon_a1_nic_type},
  1991. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  1992. PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
  1993. .driver_data = (unsigned long) &falcon_b0_nic_type},
  1994. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
  1995. .driver_data = (unsigned long) &siena_a0_nic_type},
  1996. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
  1997. .driver_data = (unsigned long) &siena_a0_nic_type},
  1998. {0} /* end of list */
  1999. };
  2000. /**************************************************************************
  2001. *
  2002. * Dummy PHY/MAC operations
  2003. *
  2004. * Can be used for some unimplemented operations
  2005. * Needed so all function pointers are valid and do not have to be tested
  2006. * before use
  2007. *
  2008. **************************************************************************/
  2009. int efx_port_dummy_op_int(struct efx_nic *efx)
  2010. {
  2011. return 0;
  2012. }
  2013. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  2014. static bool efx_port_dummy_op_poll(struct efx_nic *efx)
  2015. {
  2016. return false;
  2017. }
  2018. static const struct efx_phy_operations efx_dummy_phy_operations = {
  2019. .init = efx_port_dummy_op_int,
  2020. .reconfigure = efx_port_dummy_op_int,
  2021. .poll = efx_port_dummy_op_poll,
  2022. .fini = efx_port_dummy_op_void,
  2023. };
  2024. /**************************************************************************
  2025. *
  2026. * Data housekeeping
  2027. *
  2028. **************************************************************************/
  2029. /* This zeroes out and then fills in the invariants in a struct
  2030. * efx_nic (including all sub-structures).
  2031. */
  2032. static int efx_init_struct(struct efx_nic *efx,
  2033. struct pci_dev *pci_dev, struct net_device *net_dev)
  2034. {
  2035. int i;
  2036. /* Initialise common structures */
  2037. spin_lock_init(&efx->biu_lock);
  2038. #ifdef CONFIG_SFC_MTD
  2039. INIT_LIST_HEAD(&efx->mtd_list);
  2040. #endif
  2041. INIT_WORK(&efx->reset_work, efx_reset_work);
  2042. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  2043. INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
  2044. efx->pci_dev = pci_dev;
  2045. efx->msg_enable = debug;
  2046. efx->state = STATE_UNINIT;
  2047. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  2048. efx->net_dev = net_dev;
  2049. efx->rx_prefix_size = efx->type->rx_prefix_size;
  2050. efx->rx_packet_hash_offset =
  2051. efx->type->rx_hash_offset - efx->type->rx_prefix_size;
  2052. spin_lock_init(&efx->stats_lock);
  2053. mutex_init(&efx->mac_lock);
  2054. efx->phy_op = &efx_dummy_phy_operations;
  2055. efx->mdio.dev = net_dev;
  2056. INIT_WORK(&efx->mac_work, efx_mac_work);
  2057. init_waitqueue_head(&efx->flush_wq);
  2058. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  2059. efx->channel[i] = efx_alloc_channel(efx, i, NULL);
  2060. if (!efx->channel[i])
  2061. goto fail;
  2062. efx->msi_context[i].efx = efx;
  2063. efx->msi_context[i].index = i;
  2064. }
  2065. /* Higher numbered interrupt modes are less capable! */
  2066. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  2067. interrupt_mode);
  2068. /* Would be good to use the net_dev name, but we're too early */
  2069. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  2070. pci_name(pci_dev));
  2071. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  2072. if (!efx->workqueue)
  2073. goto fail;
  2074. return 0;
  2075. fail:
  2076. efx_fini_struct(efx);
  2077. return -ENOMEM;
  2078. }
  2079. static void efx_fini_struct(struct efx_nic *efx)
  2080. {
  2081. int i;
  2082. for (i = 0; i < EFX_MAX_CHANNELS; i++)
  2083. kfree(efx->channel[i]);
  2084. if (efx->workqueue) {
  2085. destroy_workqueue(efx->workqueue);
  2086. efx->workqueue = NULL;
  2087. }
  2088. }
  2089. /**************************************************************************
  2090. *
  2091. * PCI interface
  2092. *
  2093. **************************************************************************/
  2094. /* Main body of final NIC shutdown code
  2095. * This is called only at module unload (or hotplug removal).
  2096. */
  2097. static void efx_pci_remove_main(struct efx_nic *efx)
  2098. {
  2099. /* Flush reset_work. It can no longer be scheduled since we
  2100. * are not READY.
  2101. */
  2102. BUG_ON(efx->state == STATE_READY);
  2103. cancel_work_sync(&efx->reset_work);
  2104. efx_disable_interrupts(efx);
  2105. efx_nic_fini_interrupt(efx);
  2106. efx_fini_port(efx);
  2107. efx->type->fini(efx);
  2108. efx_fini_napi(efx);
  2109. efx_remove_all(efx);
  2110. }
  2111. /* Final NIC shutdown
  2112. * This is called only at module unload (or hotplug removal).
  2113. */
  2114. static void efx_pci_remove(struct pci_dev *pci_dev)
  2115. {
  2116. struct efx_nic *efx;
  2117. efx = pci_get_drvdata(pci_dev);
  2118. if (!efx)
  2119. return;
  2120. /* Mark the NIC as fini, then stop the interface */
  2121. rtnl_lock();
  2122. dev_close(efx->net_dev);
  2123. efx_disable_interrupts(efx);
  2124. rtnl_unlock();
  2125. efx_sriov_fini(efx);
  2126. efx_unregister_netdev(efx);
  2127. efx_mtd_remove(efx);
  2128. efx_pci_remove_main(efx);
  2129. efx_fini_io(efx);
  2130. netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
  2131. efx_fini_struct(efx);
  2132. pci_set_drvdata(pci_dev, NULL);
  2133. free_netdev(efx->net_dev);
  2134. pci_disable_pcie_error_reporting(pci_dev);
  2135. };
  2136. /* NIC VPD information
  2137. * Called during probe to display the part number of the
  2138. * installed NIC. VPD is potentially very large but this should
  2139. * always appear within the first 512 bytes.
  2140. */
  2141. #define SFC_VPD_LEN 512
  2142. static void efx_print_product_vpd(struct efx_nic *efx)
  2143. {
  2144. struct pci_dev *dev = efx->pci_dev;
  2145. char vpd_data[SFC_VPD_LEN];
  2146. ssize_t vpd_size;
  2147. int i, j;
  2148. /* Get the vpd data from the device */
  2149. vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
  2150. if (vpd_size <= 0) {
  2151. netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
  2152. return;
  2153. }
  2154. /* Get the Read only section */
  2155. i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
  2156. if (i < 0) {
  2157. netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
  2158. return;
  2159. }
  2160. j = pci_vpd_lrdt_size(&vpd_data[i]);
  2161. i += PCI_VPD_LRDT_TAG_SIZE;
  2162. if (i + j > vpd_size)
  2163. j = vpd_size - i;
  2164. /* Get the Part number */
  2165. i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
  2166. if (i < 0) {
  2167. netif_err(efx, drv, efx->net_dev, "Part number not found\n");
  2168. return;
  2169. }
  2170. j = pci_vpd_info_field_size(&vpd_data[i]);
  2171. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2172. if (i + j > vpd_size) {
  2173. netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
  2174. return;
  2175. }
  2176. netif_info(efx, drv, efx->net_dev,
  2177. "Part Number : %.*s\n", j, &vpd_data[i]);
  2178. }
  2179. /* Main body of NIC initialisation
  2180. * This is called at module load (or hotplug insertion, theoretically).
  2181. */
  2182. static int efx_pci_probe_main(struct efx_nic *efx)
  2183. {
  2184. int rc;
  2185. /* Do start-of-day initialisation */
  2186. rc = efx_probe_all(efx);
  2187. if (rc)
  2188. goto fail1;
  2189. efx_init_napi(efx);
  2190. rc = efx->type->init(efx);
  2191. if (rc) {
  2192. netif_err(efx, probe, efx->net_dev,
  2193. "failed to initialise NIC\n");
  2194. goto fail3;
  2195. }
  2196. rc = efx_init_port(efx);
  2197. if (rc) {
  2198. netif_err(efx, probe, efx->net_dev,
  2199. "failed to initialise port\n");
  2200. goto fail4;
  2201. }
  2202. rc = efx_nic_init_interrupt(efx);
  2203. if (rc)
  2204. goto fail5;
  2205. efx_enable_interrupts(efx);
  2206. return 0;
  2207. fail5:
  2208. efx_fini_port(efx);
  2209. fail4:
  2210. efx->type->fini(efx);
  2211. fail3:
  2212. efx_fini_napi(efx);
  2213. efx_remove_all(efx);
  2214. fail1:
  2215. return rc;
  2216. }
  2217. /* NIC initialisation
  2218. *
  2219. * This is called at module load (or hotplug insertion,
  2220. * theoretically). It sets up PCI mappings, resets the NIC,
  2221. * sets up and registers the network devices with the kernel and hooks
  2222. * the interrupt service routine. It does not prepare the device for
  2223. * transmission; this is left to the first time one of the network
  2224. * interfaces is brought up (i.e. efx_net_open).
  2225. */
  2226. static int efx_pci_probe(struct pci_dev *pci_dev,
  2227. const struct pci_device_id *entry)
  2228. {
  2229. struct net_device *net_dev;
  2230. struct efx_nic *efx;
  2231. int rc;
  2232. /* Allocate and initialise a struct net_device and struct efx_nic */
  2233. net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
  2234. EFX_MAX_RX_QUEUES);
  2235. if (!net_dev)
  2236. return -ENOMEM;
  2237. efx = netdev_priv(net_dev);
  2238. efx->type = (const struct efx_nic_type *) entry->driver_data;
  2239. net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
  2240. NETIF_F_HIGHDMA | NETIF_F_TSO |
  2241. NETIF_F_RXCSUM);
  2242. if (efx->type->offload_features & NETIF_F_V6_CSUM)
  2243. net_dev->features |= NETIF_F_TSO6;
  2244. /* Mask for features that also apply to VLAN devices */
  2245. net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
  2246. NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
  2247. NETIF_F_RXCSUM);
  2248. /* All offloads can be toggled */
  2249. net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
  2250. pci_set_drvdata(pci_dev, efx);
  2251. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  2252. rc = efx_init_struct(efx, pci_dev, net_dev);
  2253. if (rc)
  2254. goto fail1;
  2255. netif_info(efx, probe, efx->net_dev,
  2256. "Solarflare NIC detected\n");
  2257. efx_print_product_vpd(efx);
  2258. /* Set up basic I/O (BAR mappings etc) */
  2259. rc = efx_init_io(efx);
  2260. if (rc)
  2261. goto fail2;
  2262. rc = efx_pci_probe_main(efx);
  2263. if (rc)
  2264. goto fail3;
  2265. rc = efx_register_netdev(efx);
  2266. if (rc)
  2267. goto fail4;
  2268. rc = efx_sriov_init(efx);
  2269. if (rc)
  2270. netif_err(efx, probe, efx->net_dev,
  2271. "SR-IOV can't be enabled rc %d\n", rc);
  2272. netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
  2273. /* Try to create MTDs, but allow this to fail */
  2274. rtnl_lock();
  2275. rc = efx_mtd_probe(efx);
  2276. rtnl_unlock();
  2277. if (rc)
  2278. netif_warn(efx, probe, efx->net_dev,
  2279. "failed to create MTDs (%d)\n", rc);
  2280. rc = pci_enable_pcie_error_reporting(pci_dev);
  2281. if (rc && rc != -EINVAL)
  2282. netif_warn(efx, probe, efx->net_dev,
  2283. "pci_enable_pcie_error_reporting failed (%d)\n", rc);
  2284. return 0;
  2285. fail4:
  2286. efx_pci_remove_main(efx);
  2287. fail3:
  2288. efx_fini_io(efx);
  2289. fail2:
  2290. efx_fini_struct(efx);
  2291. fail1:
  2292. pci_set_drvdata(pci_dev, NULL);
  2293. WARN_ON(rc > 0);
  2294. netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
  2295. free_netdev(net_dev);
  2296. return rc;
  2297. }
  2298. static int efx_pm_freeze(struct device *dev)
  2299. {
  2300. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2301. rtnl_lock();
  2302. if (efx->state != STATE_DISABLED) {
  2303. efx->state = STATE_UNINIT;
  2304. efx_device_detach_sync(efx);
  2305. efx_stop_all(efx);
  2306. efx_disable_interrupts(efx);
  2307. }
  2308. rtnl_unlock();
  2309. return 0;
  2310. }
  2311. static int efx_pm_thaw(struct device *dev)
  2312. {
  2313. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2314. rtnl_lock();
  2315. if (efx->state != STATE_DISABLED) {
  2316. efx_enable_interrupts(efx);
  2317. mutex_lock(&efx->mac_lock);
  2318. efx->phy_op->reconfigure(efx);
  2319. mutex_unlock(&efx->mac_lock);
  2320. efx_start_all(efx);
  2321. netif_device_attach(efx->net_dev);
  2322. efx->state = STATE_READY;
  2323. efx->type->resume_wol(efx);
  2324. }
  2325. rtnl_unlock();
  2326. /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
  2327. queue_work(reset_workqueue, &efx->reset_work);
  2328. return 0;
  2329. }
  2330. static int efx_pm_poweroff(struct device *dev)
  2331. {
  2332. struct pci_dev *pci_dev = to_pci_dev(dev);
  2333. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2334. efx->type->fini(efx);
  2335. efx->reset_pending = 0;
  2336. pci_save_state(pci_dev);
  2337. return pci_set_power_state(pci_dev, PCI_D3hot);
  2338. }
  2339. /* Used for both resume and restore */
  2340. static int efx_pm_resume(struct device *dev)
  2341. {
  2342. struct pci_dev *pci_dev = to_pci_dev(dev);
  2343. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2344. int rc;
  2345. rc = pci_set_power_state(pci_dev, PCI_D0);
  2346. if (rc)
  2347. return rc;
  2348. pci_restore_state(pci_dev);
  2349. rc = pci_enable_device(pci_dev);
  2350. if (rc)
  2351. return rc;
  2352. pci_set_master(efx->pci_dev);
  2353. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  2354. if (rc)
  2355. return rc;
  2356. rc = efx->type->init(efx);
  2357. if (rc)
  2358. return rc;
  2359. efx_pm_thaw(dev);
  2360. return 0;
  2361. }
  2362. static int efx_pm_suspend(struct device *dev)
  2363. {
  2364. int rc;
  2365. efx_pm_freeze(dev);
  2366. rc = efx_pm_poweroff(dev);
  2367. if (rc)
  2368. efx_pm_resume(dev);
  2369. return rc;
  2370. }
  2371. static const struct dev_pm_ops efx_pm_ops = {
  2372. .suspend = efx_pm_suspend,
  2373. .resume = efx_pm_resume,
  2374. .freeze = efx_pm_freeze,
  2375. .thaw = efx_pm_thaw,
  2376. .poweroff = efx_pm_poweroff,
  2377. .restore = efx_pm_resume,
  2378. };
  2379. /* A PCI error affecting this device was detected.
  2380. * At this point MMIO and DMA may be disabled.
  2381. * Stop the software path and request a slot reset.
  2382. */
  2383. static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
  2384. enum pci_channel_state state)
  2385. {
  2386. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2387. struct efx_nic *efx = pci_get_drvdata(pdev);
  2388. if (state == pci_channel_io_perm_failure)
  2389. return PCI_ERS_RESULT_DISCONNECT;
  2390. rtnl_lock();
  2391. if (efx->state != STATE_DISABLED) {
  2392. efx->state = STATE_RECOVERY;
  2393. efx->reset_pending = 0;
  2394. efx_device_detach_sync(efx);
  2395. efx_stop_all(efx);
  2396. efx_disable_interrupts(efx);
  2397. status = PCI_ERS_RESULT_NEED_RESET;
  2398. } else {
  2399. /* If the interface is disabled we don't want to do anything
  2400. * with it.
  2401. */
  2402. status = PCI_ERS_RESULT_RECOVERED;
  2403. }
  2404. rtnl_unlock();
  2405. pci_disable_device(pdev);
  2406. return status;
  2407. }
  2408. /* Fake a successfull reset, which will be performed later in efx_io_resume. */
  2409. static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
  2410. {
  2411. struct efx_nic *efx = pci_get_drvdata(pdev);
  2412. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2413. int rc;
  2414. if (pci_enable_device(pdev)) {
  2415. netif_err(efx, hw, efx->net_dev,
  2416. "Cannot re-enable PCI device after reset.\n");
  2417. status = PCI_ERS_RESULT_DISCONNECT;
  2418. }
  2419. rc = pci_cleanup_aer_uncorrect_error_status(pdev);
  2420. if (rc) {
  2421. netif_err(efx, hw, efx->net_dev,
  2422. "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
  2423. /* Non-fatal error. Continue. */
  2424. }
  2425. return status;
  2426. }
  2427. /* Perform the actual reset and resume I/O operations. */
  2428. static void efx_io_resume(struct pci_dev *pdev)
  2429. {
  2430. struct efx_nic *efx = pci_get_drvdata(pdev);
  2431. int rc;
  2432. rtnl_lock();
  2433. if (efx->state == STATE_DISABLED)
  2434. goto out;
  2435. rc = efx_reset(efx, RESET_TYPE_ALL);
  2436. if (rc) {
  2437. netif_err(efx, hw, efx->net_dev,
  2438. "efx_reset failed after PCI error (%d)\n", rc);
  2439. } else {
  2440. efx->state = STATE_READY;
  2441. netif_dbg(efx, hw, efx->net_dev,
  2442. "Done resetting and resuming IO after PCI error.\n");
  2443. }
  2444. out:
  2445. rtnl_unlock();
  2446. }
  2447. /* For simplicity and reliability, we always require a slot reset and try to
  2448. * reset the hardware when a pci error affecting the device is detected.
  2449. * We leave both the link_reset and mmio_enabled callback unimplemented:
  2450. * with our request for slot reset the mmio_enabled callback will never be
  2451. * called, and the link_reset callback is not used by AER or EEH mechanisms.
  2452. */
  2453. static struct pci_error_handlers efx_err_handlers = {
  2454. .error_detected = efx_io_error_detected,
  2455. .slot_reset = efx_io_slot_reset,
  2456. .resume = efx_io_resume,
  2457. };
  2458. static struct pci_driver efx_pci_driver = {
  2459. .name = KBUILD_MODNAME,
  2460. .id_table = efx_pci_table,
  2461. .probe = efx_pci_probe,
  2462. .remove = efx_pci_remove,
  2463. .driver.pm = &efx_pm_ops,
  2464. .err_handler = &efx_err_handlers,
  2465. };
  2466. /**************************************************************************
  2467. *
  2468. * Kernel module interface
  2469. *
  2470. *************************************************************************/
  2471. module_param(interrupt_mode, uint, 0444);
  2472. MODULE_PARM_DESC(interrupt_mode,
  2473. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  2474. static int __init efx_init_module(void)
  2475. {
  2476. int rc;
  2477. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  2478. rc = register_netdevice_notifier(&efx_netdev_notifier);
  2479. if (rc)
  2480. goto err_notifier;
  2481. rc = efx_init_sriov();
  2482. if (rc)
  2483. goto err_sriov;
  2484. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  2485. if (!reset_workqueue) {
  2486. rc = -ENOMEM;
  2487. goto err_reset;
  2488. }
  2489. rc = pci_register_driver(&efx_pci_driver);
  2490. if (rc < 0)
  2491. goto err_pci;
  2492. return 0;
  2493. err_pci:
  2494. destroy_workqueue(reset_workqueue);
  2495. err_reset:
  2496. efx_fini_sriov();
  2497. err_sriov:
  2498. unregister_netdevice_notifier(&efx_netdev_notifier);
  2499. err_notifier:
  2500. return rc;
  2501. }
  2502. static void __exit efx_exit_module(void)
  2503. {
  2504. printk(KERN_INFO "Solarflare NET driver unloading\n");
  2505. pci_unregister_driver(&efx_pci_driver);
  2506. destroy_workqueue(reset_workqueue);
  2507. efx_fini_sriov();
  2508. unregister_netdevice_notifier(&efx_netdev_notifier);
  2509. }
  2510. module_init(efx_init_module);
  2511. module_exit(efx_exit_module);
  2512. MODULE_AUTHOR("Solarflare Communications and "
  2513. "Michael Brown <mbrown@fensystems.co.uk>");
  2514. MODULE_DESCRIPTION("Solarflare Communications network driver");
  2515. MODULE_LICENSE("GPL");
  2516. MODULE_DEVICE_TABLE(pci, efx_pci_table);