efx.c 81 KB

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