efx.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2013 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/delay.h>
  15. #include <linux/notifier.h>
  16. #include <linux/ip.h>
  17. #include <linux/tcp.h>
  18. #include <linux/in.h>
  19. #include <linux/ethtool.h>
  20. #include <linux/topology.h>
  21. #include <linux/gfp.h>
  22. #include <linux/aer.h>
  23. #include <linux/interrupt.h>
  24. #include "net_driver.h"
  25. #include <net/gre.h>
  26. #include <net/udp_tunnel.h>
  27. #include "efx.h"
  28. #include "nic.h"
  29. #include "io.h"
  30. #include "selftest.h"
  31. #include "sriov.h"
  32. #include "mcdi.h"
  33. #include "mcdi_pcol.h"
  34. #include "workarounds.h"
  35. /**************************************************************************
  36. *
  37. * Type name strings
  38. *
  39. **************************************************************************
  40. */
  41. /* Loopback mode names (see LOOPBACK_MODE()) */
  42. const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
  43. const char *const efx_loopback_mode_names[] = {
  44. [LOOPBACK_NONE] = "NONE",
  45. [LOOPBACK_DATA] = "DATAPATH",
  46. [LOOPBACK_GMAC] = "GMAC",
  47. [LOOPBACK_XGMII] = "XGMII",
  48. [LOOPBACK_XGXS] = "XGXS",
  49. [LOOPBACK_XAUI] = "XAUI",
  50. [LOOPBACK_GMII] = "GMII",
  51. [LOOPBACK_SGMII] = "SGMII",
  52. [LOOPBACK_XGBR] = "XGBR",
  53. [LOOPBACK_XFI] = "XFI",
  54. [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
  55. [LOOPBACK_GMII_FAR] = "GMII_FAR",
  56. [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
  57. [LOOPBACK_XFI_FAR] = "XFI_FAR",
  58. [LOOPBACK_GPHY] = "GPHY",
  59. [LOOPBACK_PHYXS] = "PHYXS",
  60. [LOOPBACK_PCS] = "PCS",
  61. [LOOPBACK_PMAPMD] = "PMA/PMD",
  62. [LOOPBACK_XPORT] = "XPORT",
  63. [LOOPBACK_XGMII_WS] = "XGMII_WS",
  64. [LOOPBACK_XAUI_WS] = "XAUI_WS",
  65. [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
  66. [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
  67. [LOOPBACK_GMII_WS] = "GMII_WS",
  68. [LOOPBACK_XFI_WS] = "XFI_WS",
  69. [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
  70. [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
  71. };
  72. const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
  73. const char *const efx_reset_type_names[] = {
  74. [RESET_TYPE_INVISIBLE] = "INVISIBLE",
  75. [RESET_TYPE_ALL] = "ALL",
  76. [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
  77. [RESET_TYPE_WORLD] = "WORLD",
  78. [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
  79. [RESET_TYPE_DATAPATH] = "DATAPATH",
  80. [RESET_TYPE_MC_BIST] = "MC_BIST",
  81. [RESET_TYPE_DISABLE] = "DISABLE",
  82. [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
  83. [RESET_TYPE_INT_ERROR] = "INT_ERROR",
  84. [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
  85. [RESET_TYPE_TX_SKIP] = "TX_SKIP",
  86. [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
  87. [RESET_TYPE_MCDI_TIMEOUT] = "MCDI_TIMEOUT (FLR)",
  88. };
  89. /* UDP tunnel type names */
  90. static const char *const efx_udp_tunnel_type_names[] = {
  91. [TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN] = "vxlan",
  92. [TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE] = "geneve",
  93. };
  94. void efx_get_udp_tunnel_type_name(u16 type, char *buf, size_t buflen)
  95. {
  96. if (type < ARRAY_SIZE(efx_udp_tunnel_type_names) &&
  97. efx_udp_tunnel_type_names[type] != NULL)
  98. snprintf(buf, buflen, "%s", efx_udp_tunnel_type_names[type]);
  99. else
  100. snprintf(buf, buflen, "type %d", type);
  101. }
  102. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  103. * queued onto this work queue. This is not a per-nic work queue, because
  104. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  105. */
  106. static struct workqueue_struct *reset_workqueue;
  107. /* How often and how many times to poll for a reset while waiting for a
  108. * BIST that another function started to complete.
  109. */
  110. #define BIST_WAIT_DELAY_MS 100
  111. #define BIST_WAIT_DELAY_COUNT 100
  112. /**************************************************************************
  113. *
  114. * Configurable values
  115. *
  116. *************************************************************************/
  117. /*
  118. * Use separate channels for TX and RX events
  119. *
  120. * Set this to 1 to use separate channels for TX and RX. It allows us
  121. * to control interrupt affinity separately for TX and RX.
  122. *
  123. * This is only used in MSI-X interrupt mode
  124. */
  125. bool efx_separate_tx_channels;
  126. module_param(efx_separate_tx_channels, bool, 0444);
  127. MODULE_PARM_DESC(efx_separate_tx_channels,
  128. "Use separate channels for TX and RX");
  129. /* This is the weight assigned to each of the (per-channel) virtual
  130. * NAPI devices.
  131. */
  132. static int napi_weight = 64;
  133. /* This is the time (in jiffies) between invocations of the hardware
  134. * monitor.
  135. * On Falcon-based NICs, this will:
  136. * - Check the on-board hardware monitor;
  137. * - Poll the link state and reconfigure the hardware as necessary.
  138. * On Siena-based NICs for power systems with EEH support, this will give EEH a
  139. * chance to start.
  140. */
  141. static unsigned int efx_monitor_interval = 1 * HZ;
  142. /* Initial interrupt moderation settings. They can be modified after
  143. * module load with ethtool.
  144. *
  145. * The default for RX should strike a balance between increasing the
  146. * round-trip latency and reducing overhead.
  147. */
  148. static unsigned int rx_irq_mod_usec = 60;
  149. /* Initial interrupt moderation settings. They can be modified after
  150. * module load with ethtool.
  151. *
  152. * This default is chosen to ensure that a 10G link does not go idle
  153. * while a TX queue is stopped after it has become full. A queue is
  154. * restarted when it drops below half full. The time this takes (assuming
  155. * worst case 3 descriptors per packet and 1024 descriptors) is
  156. * 512 / 3 * 1.2 = 205 usec.
  157. */
  158. static unsigned int tx_irq_mod_usec = 150;
  159. /* This is the first interrupt mode to try out of:
  160. * 0 => MSI-X
  161. * 1 => MSI
  162. * 2 => legacy
  163. */
  164. static unsigned int interrupt_mode;
  165. /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
  166. * i.e. the number of CPUs among which we may distribute simultaneous
  167. * interrupt handling.
  168. *
  169. * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
  170. * The default (0) means to assign an interrupt to each core.
  171. */
  172. static unsigned int rss_cpus;
  173. module_param(rss_cpus, uint, 0444);
  174. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  175. static bool phy_flash_cfg;
  176. module_param(phy_flash_cfg, bool, 0644);
  177. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  178. static unsigned irq_adapt_low_thresh = 8000;
  179. module_param(irq_adapt_low_thresh, uint, 0644);
  180. MODULE_PARM_DESC(irq_adapt_low_thresh,
  181. "Threshold score for reducing IRQ moderation");
  182. static unsigned irq_adapt_high_thresh = 16000;
  183. module_param(irq_adapt_high_thresh, uint, 0644);
  184. MODULE_PARM_DESC(irq_adapt_high_thresh,
  185. "Threshold score for increasing IRQ moderation");
  186. static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  187. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  188. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  189. NETIF_MSG_TX_ERR | NETIF_MSG_HW);
  190. module_param(debug, uint, 0);
  191. MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
  192. /**************************************************************************
  193. *
  194. * Utility functions and prototypes
  195. *
  196. *************************************************************************/
  197. static int efx_soft_enable_interrupts(struct efx_nic *efx);
  198. static void efx_soft_disable_interrupts(struct efx_nic *efx);
  199. static void efx_remove_channel(struct efx_channel *channel);
  200. static void efx_remove_channels(struct efx_nic *efx);
  201. static const struct efx_channel_type efx_default_channel_type;
  202. static void efx_remove_port(struct efx_nic *efx);
  203. static void efx_init_napi_channel(struct efx_channel *channel);
  204. static void efx_fini_napi(struct efx_nic *efx);
  205. static void efx_fini_napi_channel(struct efx_channel *channel);
  206. static void efx_fini_struct(struct efx_nic *efx);
  207. static void efx_start_all(struct efx_nic *efx);
  208. static void efx_stop_all(struct efx_nic *efx);
  209. #define EFX_ASSERT_RESET_SERIALISED(efx) \
  210. do { \
  211. if ((efx->state == STATE_READY) || \
  212. (efx->state == STATE_RECOVERY) || \
  213. (efx->state == STATE_DISABLED)) \
  214. ASSERT_RTNL(); \
  215. } while (0)
  216. static int efx_check_disabled(struct efx_nic *efx)
  217. {
  218. if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
  219. netif_err(efx, drv, efx->net_dev,
  220. "device is disabled due to earlier errors\n");
  221. return -EIO;
  222. }
  223. return 0;
  224. }
  225. /**************************************************************************
  226. *
  227. * Event queue processing
  228. *
  229. *************************************************************************/
  230. /* Process channel's event queue
  231. *
  232. * This function is responsible for processing the event queue of a
  233. * single channel. The caller must guarantee that this function will
  234. * never be concurrently called more than once on the same channel,
  235. * though different channels may be being processed concurrently.
  236. */
  237. static int efx_process_channel(struct efx_channel *channel, int budget)
  238. {
  239. struct efx_tx_queue *tx_queue;
  240. int spent;
  241. if (unlikely(!channel->enabled))
  242. return 0;
  243. efx_for_each_channel_tx_queue(tx_queue, channel) {
  244. tx_queue->pkts_compl = 0;
  245. tx_queue->bytes_compl = 0;
  246. }
  247. spent = efx_nic_process_eventq(channel, budget);
  248. if (spent && efx_channel_has_rx_queue(channel)) {
  249. struct efx_rx_queue *rx_queue =
  250. efx_channel_get_rx_queue(channel);
  251. efx_rx_flush_packet(channel);
  252. efx_fast_push_rx_descriptors(rx_queue, true);
  253. }
  254. /* Update BQL */
  255. efx_for_each_channel_tx_queue(tx_queue, channel) {
  256. if (tx_queue->bytes_compl) {
  257. netdev_tx_completed_queue(tx_queue->core_txq,
  258. tx_queue->pkts_compl, tx_queue->bytes_compl);
  259. }
  260. }
  261. return spent;
  262. }
  263. /* NAPI poll handler
  264. *
  265. * NAPI guarantees serialisation of polls of the same device, which
  266. * provides the guarantee required by efx_process_channel().
  267. */
  268. static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel)
  269. {
  270. int step = efx->irq_mod_step_us;
  271. if (channel->irq_mod_score < irq_adapt_low_thresh) {
  272. if (channel->irq_moderation_us > step) {
  273. channel->irq_moderation_us -= step;
  274. efx->type->push_irq_moderation(channel);
  275. }
  276. } else if (channel->irq_mod_score > irq_adapt_high_thresh) {
  277. if (channel->irq_moderation_us <
  278. efx->irq_rx_moderation_us) {
  279. channel->irq_moderation_us += step;
  280. efx->type->push_irq_moderation(channel);
  281. }
  282. }
  283. channel->irq_count = 0;
  284. channel->irq_mod_score = 0;
  285. }
  286. static int efx_poll(struct napi_struct *napi, int budget)
  287. {
  288. struct efx_channel *channel =
  289. container_of(napi, struct efx_channel, napi_str);
  290. struct efx_nic *efx = channel->efx;
  291. int spent;
  292. netif_vdbg(efx, intr, efx->net_dev,
  293. "channel %d NAPI poll executing on CPU %d\n",
  294. channel->channel, raw_smp_processor_id());
  295. spent = efx_process_channel(channel, budget);
  296. if (spent < budget) {
  297. if (efx_channel_has_rx_queue(channel) &&
  298. efx->irq_rx_adaptive &&
  299. unlikely(++channel->irq_count == 1000)) {
  300. efx_update_irq_mod(efx, channel);
  301. }
  302. #ifdef CONFIG_RFS_ACCEL
  303. /* Perhaps expire some ARFS filters */
  304. schedule_work(&channel->filter_work);
  305. #endif
  306. /* There is no race here; although napi_disable() will
  307. * only wait for napi_complete(), this isn't a problem
  308. * since efx_nic_eventq_read_ack() will have no effect if
  309. * interrupts have already been disabled.
  310. */
  311. if (napi_complete_done(napi, spent))
  312. efx_nic_eventq_read_ack(channel);
  313. }
  314. return spent;
  315. }
  316. /* Create event queue
  317. * Event queue memory allocations are done only once. If the channel
  318. * is reset, the memory buffer will be reused; this guards against
  319. * errors during channel reset and also simplifies interrupt handling.
  320. */
  321. static int efx_probe_eventq(struct efx_channel *channel)
  322. {
  323. struct efx_nic *efx = channel->efx;
  324. unsigned long entries;
  325. netif_dbg(efx, probe, efx->net_dev,
  326. "chan %d create event queue\n", channel->channel);
  327. /* Build an event queue with room for one event per tx and rx buffer,
  328. * plus some extra for link state events and MCDI completions. */
  329. entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
  330. EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
  331. channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
  332. return efx_nic_probe_eventq(channel);
  333. }
  334. /* Prepare channel's event queue */
  335. static int efx_init_eventq(struct efx_channel *channel)
  336. {
  337. struct efx_nic *efx = channel->efx;
  338. int rc;
  339. EFX_WARN_ON_PARANOID(channel->eventq_init);
  340. netif_dbg(efx, drv, efx->net_dev,
  341. "chan %d init event queue\n", channel->channel);
  342. rc = efx_nic_init_eventq(channel);
  343. if (rc == 0) {
  344. efx->type->push_irq_moderation(channel);
  345. channel->eventq_read_ptr = 0;
  346. channel->eventq_init = true;
  347. }
  348. return rc;
  349. }
  350. /* Enable event queue processing and NAPI */
  351. void efx_start_eventq(struct efx_channel *channel)
  352. {
  353. netif_dbg(channel->efx, ifup, channel->efx->net_dev,
  354. "chan %d start event queue\n", channel->channel);
  355. /* Make sure the NAPI handler sees the enabled flag set */
  356. channel->enabled = true;
  357. smp_wmb();
  358. napi_enable(&channel->napi_str);
  359. efx_nic_eventq_read_ack(channel);
  360. }
  361. /* Disable event queue processing and NAPI */
  362. void efx_stop_eventq(struct efx_channel *channel)
  363. {
  364. if (!channel->enabled)
  365. return;
  366. napi_disable(&channel->napi_str);
  367. channel->enabled = false;
  368. }
  369. static void efx_fini_eventq(struct efx_channel *channel)
  370. {
  371. if (!channel->eventq_init)
  372. return;
  373. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  374. "chan %d fini event queue\n", channel->channel);
  375. efx_nic_fini_eventq(channel);
  376. channel->eventq_init = false;
  377. }
  378. static void efx_remove_eventq(struct efx_channel *channel)
  379. {
  380. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  381. "chan %d remove event queue\n", channel->channel);
  382. efx_nic_remove_eventq(channel);
  383. }
  384. /**************************************************************************
  385. *
  386. * Channel handling
  387. *
  388. *************************************************************************/
  389. /* Allocate and initialise a channel structure. */
  390. static struct efx_channel *
  391. efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
  392. {
  393. struct efx_channel *channel;
  394. struct efx_rx_queue *rx_queue;
  395. struct efx_tx_queue *tx_queue;
  396. int j;
  397. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  398. if (!channel)
  399. return NULL;
  400. channel->efx = efx;
  401. channel->channel = i;
  402. channel->type = &efx_default_channel_type;
  403. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  404. tx_queue = &channel->tx_queue[j];
  405. tx_queue->efx = efx;
  406. tx_queue->queue = i * EFX_TXQ_TYPES + j;
  407. tx_queue->channel = channel;
  408. }
  409. #ifdef CONFIG_RFS_ACCEL
  410. INIT_WORK(&channel->filter_work, efx_filter_rfs_expire);
  411. #endif
  412. rx_queue = &channel->rx_queue;
  413. rx_queue->efx = efx;
  414. timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
  415. return channel;
  416. }
  417. /* Allocate and initialise a channel structure, copying parameters
  418. * (but not resources) from an old channel structure.
  419. */
  420. static struct efx_channel *
  421. efx_copy_channel(const struct efx_channel *old_channel)
  422. {
  423. struct efx_channel *channel;
  424. struct efx_rx_queue *rx_queue;
  425. struct efx_tx_queue *tx_queue;
  426. int j;
  427. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  428. if (!channel)
  429. return NULL;
  430. *channel = *old_channel;
  431. channel->napi_dev = NULL;
  432. INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
  433. channel->napi_str.napi_id = 0;
  434. channel->napi_str.state = 0;
  435. memset(&channel->eventq, 0, sizeof(channel->eventq));
  436. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  437. tx_queue = &channel->tx_queue[j];
  438. if (tx_queue->channel)
  439. tx_queue->channel = channel;
  440. tx_queue->buffer = NULL;
  441. memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
  442. }
  443. rx_queue = &channel->rx_queue;
  444. rx_queue->buffer = NULL;
  445. memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
  446. timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
  447. #ifdef CONFIG_RFS_ACCEL
  448. INIT_WORK(&channel->filter_work, efx_filter_rfs_expire);
  449. #endif
  450. return channel;
  451. }
  452. static int efx_probe_channel(struct efx_channel *channel)
  453. {
  454. struct efx_tx_queue *tx_queue;
  455. struct efx_rx_queue *rx_queue;
  456. int rc;
  457. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  458. "creating channel %d\n", channel->channel);
  459. rc = channel->type->pre_probe(channel);
  460. if (rc)
  461. goto fail;
  462. rc = efx_probe_eventq(channel);
  463. if (rc)
  464. goto fail;
  465. efx_for_each_channel_tx_queue(tx_queue, channel) {
  466. rc = efx_probe_tx_queue(tx_queue);
  467. if (rc)
  468. goto fail;
  469. }
  470. efx_for_each_channel_rx_queue(rx_queue, channel) {
  471. rc = efx_probe_rx_queue(rx_queue);
  472. if (rc)
  473. goto fail;
  474. }
  475. return 0;
  476. fail:
  477. efx_remove_channel(channel);
  478. return rc;
  479. }
  480. static void
  481. efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
  482. {
  483. struct efx_nic *efx = channel->efx;
  484. const char *type;
  485. int number;
  486. number = channel->channel;
  487. if (efx->tx_channel_offset == 0) {
  488. type = "";
  489. } else if (channel->channel < efx->tx_channel_offset) {
  490. type = "-rx";
  491. } else {
  492. type = "-tx";
  493. number -= efx->tx_channel_offset;
  494. }
  495. snprintf(buf, len, "%s%s-%d", efx->name, type, number);
  496. }
  497. static void efx_set_channel_names(struct efx_nic *efx)
  498. {
  499. struct efx_channel *channel;
  500. efx_for_each_channel(channel, efx)
  501. channel->type->get_name(channel,
  502. efx->msi_context[channel->channel].name,
  503. sizeof(efx->msi_context[0].name));
  504. }
  505. static int efx_probe_channels(struct efx_nic *efx)
  506. {
  507. struct efx_channel *channel;
  508. int rc;
  509. /* Restart special buffer allocation */
  510. efx->next_buffer_table = 0;
  511. /* Probe channels in reverse, so that any 'extra' channels
  512. * use the start of the buffer table. This allows the traffic
  513. * channels to be resized without moving them or wasting the
  514. * entries before them.
  515. */
  516. efx_for_each_channel_rev(channel, efx) {
  517. rc = efx_probe_channel(channel);
  518. if (rc) {
  519. netif_err(efx, probe, efx->net_dev,
  520. "failed to create channel %d\n",
  521. channel->channel);
  522. goto fail;
  523. }
  524. }
  525. efx_set_channel_names(efx);
  526. return 0;
  527. fail:
  528. efx_remove_channels(efx);
  529. return rc;
  530. }
  531. /* Channels are shutdown and reinitialised whilst the NIC is running
  532. * to propagate configuration changes (mtu, checksum offload), or
  533. * to clear hardware error conditions
  534. */
  535. static void efx_start_datapath(struct efx_nic *efx)
  536. {
  537. netdev_features_t old_features = efx->net_dev->features;
  538. bool old_rx_scatter = efx->rx_scatter;
  539. struct efx_tx_queue *tx_queue;
  540. struct efx_rx_queue *rx_queue;
  541. struct efx_channel *channel;
  542. size_t rx_buf_len;
  543. /* Calculate the rx buffer allocation parameters required to
  544. * support the current MTU, including padding for header
  545. * alignment and overruns.
  546. */
  547. efx->rx_dma_len = (efx->rx_prefix_size +
  548. EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
  549. efx->type->rx_buffer_padding);
  550. rx_buf_len = (sizeof(struct efx_rx_page_state) +
  551. efx->rx_ip_align + efx->rx_dma_len);
  552. if (rx_buf_len <= PAGE_SIZE) {
  553. efx->rx_scatter = efx->type->always_rx_scatter;
  554. efx->rx_buffer_order = 0;
  555. } else if (efx->type->can_rx_scatter) {
  556. BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
  557. BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
  558. 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
  559. EFX_RX_BUF_ALIGNMENT) >
  560. PAGE_SIZE);
  561. efx->rx_scatter = true;
  562. efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
  563. efx->rx_buffer_order = 0;
  564. } else {
  565. efx->rx_scatter = false;
  566. efx->rx_buffer_order = get_order(rx_buf_len);
  567. }
  568. efx_rx_config_page_split(efx);
  569. if (efx->rx_buffer_order)
  570. netif_dbg(efx, drv, efx->net_dev,
  571. "RX buf len=%u; page order=%u batch=%u\n",
  572. efx->rx_dma_len, efx->rx_buffer_order,
  573. efx->rx_pages_per_batch);
  574. else
  575. netif_dbg(efx, drv, efx->net_dev,
  576. "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
  577. efx->rx_dma_len, efx->rx_page_buf_step,
  578. efx->rx_bufs_per_page, efx->rx_pages_per_batch);
  579. /* Restore previously fixed features in hw_features and remove
  580. * features which are fixed now
  581. */
  582. efx->net_dev->hw_features |= efx->net_dev->features;
  583. efx->net_dev->hw_features &= ~efx->fixed_features;
  584. efx->net_dev->features |= efx->fixed_features;
  585. if (efx->net_dev->features != old_features)
  586. netdev_features_change(efx->net_dev);
  587. /* RX filters may also have scatter-enabled flags */
  588. if (efx->rx_scatter != old_rx_scatter)
  589. efx->type->filter_update_rx_scatter(efx);
  590. /* We must keep at least one descriptor in a TX ring empty.
  591. * We could avoid this when the queue size does not exactly
  592. * match the hardware ring size, but it's not that important.
  593. * Therefore we stop the queue when one more skb might fill
  594. * the ring completely. We wake it when half way back to
  595. * empty.
  596. */
  597. efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
  598. efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
  599. /* Initialise the channels */
  600. efx_for_each_channel(channel, efx) {
  601. efx_for_each_channel_tx_queue(tx_queue, channel) {
  602. efx_init_tx_queue(tx_queue);
  603. atomic_inc(&efx->active_queues);
  604. }
  605. efx_for_each_channel_rx_queue(rx_queue, channel) {
  606. efx_init_rx_queue(rx_queue);
  607. atomic_inc(&efx->active_queues);
  608. efx_stop_eventq(channel);
  609. efx_fast_push_rx_descriptors(rx_queue, false);
  610. efx_start_eventq(channel);
  611. }
  612. WARN_ON(channel->rx_pkt_n_frags);
  613. }
  614. efx_ptp_start_datapath(efx);
  615. if (netif_device_present(efx->net_dev))
  616. netif_tx_wake_all_queues(efx->net_dev);
  617. }
  618. static void efx_stop_datapath(struct efx_nic *efx)
  619. {
  620. struct efx_channel *channel;
  621. struct efx_tx_queue *tx_queue;
  622. struct efx_rx_queue *rx_queue;
  623. int rc;
  624. EFX_ASSERT_RESET_SERIALISED(efx);
  625. BUG_ON(efx->port_enabled);
  626. efx_ptp_stop_datapath(efx);
  627. /* Stop RX refill */
  628. efx_for_each_channel(channel, efx) {
  629. efx_for_each_channel_rx_queue(rx_queue, channel)
  630. rx_queue->refill_enabled = false;
  631. }
  632. efx_for_each_channel(channel, efx) {
  633. /* RX packet processing is pipelined, so wait for the
  634. * NAPI handler to complete. At least event queue 0
  635. * might be kept active by non-data events, so don't
  636. * use napi_synchronize() but actually disable NAPI
  637. * temporarily.
  638. */
  639. if (efx_channel_has_rx_queue(channel)) {
  640. efx_stop_eventq(channel);
  641. efx_start_eventq(channel);
  642. }
  643. }
  644. rc = efx->type->fini_dmaq(efx);
  645. if (rc) {
  646. netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
  647. } else {
  648. netif_dbg(efx, drv, efx->net_dev,
  649. "successfully flushed all queues\n");
  650. }
  651. efx_for_each_channel(channel, efx) {
  652. efx_for_each_channel_rx_queue(rx_queue, channel)
  653. efx_fini_rx_queue(rx_queue);
  654. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  655. efx_fini_tx_queue(tx_queue);
  656. }
  657. }
  658. static void efx_remove_channel(struct efx_channel *channel)
  659. {
  660. struct efx_tx_queue *tx_queue;
  661. struct efx_rx_queue *rx_queue;
  662. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  663. "destroy chan %d\n", channel->channel);
  664. efx_for_each_channel_rx_queue(rx_queue, channel)
  665. efx_remove_rx_queue(rx_queue);
  666. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  667. efx_remove_tx_queue(tx_queue);
  668. efx_remove_eventq(channel);
  669. channel->type->post_remove(channel);
  670. }
  671. static void efx_remove_channels(struct efx_nic *efx)
  672. {
  673. struct efx_channel *channel;
  674. efx_for_each_channel(channel, efx)
  675. efx_remove_channel(channel);
  676. }
  677. int
  678. efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
  679. {
  680. struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
  681. u32 old_rxq_entries, old_txq_entries;
  682. unsigned i, next_buffer_table = 0;
  683. int rc, rc2;
  684. rc = efx_check_disabled(efx);
  685. if (rc)
  686. return rc;
  687. /* Not all channels should be reallocated. We must avoid
  688. * reallocating their buffer table entries.
  689. */
  690. efx_for_each_channel(channel, efx) {
  691. struct efx_rx_queue *rx_queue;
  692. struct efx_tx_queue *tx_queue;
  693. if (channel->type->copy)
  694. continue;
  695. next_buffer_table = max(next_buffer_table,
  696. channel->eventq.index +
  697. channel->eventq.entries);
  698. efx_for_each_channel_rx_queue(rx_queue, channel)
  699. next_buffer_table = max(next_buffer_table,
  700. rx_queue->rxd.index +
  701. rx_queue->rxd.entries);
  702. efx_for_each_channel_tx_queue(tx_queue, channel)
  703. next_buffer_table = max(next_buffer_table,
  704. tx_queue->txd.index +
  705. tx_queue->txd.entries);
  706. }
  707. efx_device_detach_sync(efx);
  708. efx_stop_all(efx);
  709. efx_soft_disable_interrupts(efx);
  710. /* Clone channels (where possible) */
  711. memset(other_channel, 0, sizeof(other_channel));
  712. for (i = 0; i < efx->n_channels; i++) {
  713. channel = efx->channel[i];
  714. if (channel->type->copy)
  715. channel = channel->type->copy(channel);
  716. if (!channel) {
  717. rc = -ENOMEM;
  718. goto out;
  719. }
  720. other_channel[i] = channel;
  721. }
  722. /* Swap entry counts and channel pointers */
  723. old_rxq_entries = efx->rxq_entries;
  724. old_txq_entries = efx->txq_entries;
  725. efx->rxq_entries = rxq_entries;
  726. efx->txq_entries = txq_entries;
  727. for (i = 0; i < efx->n_channels; i++) {
  728. channel = efx->channel[i];
  729. efx->channel[i] = other_channel[i];
  730. other_channel[i] = channel;
  731. }
  732. /* Restart buffer table allocation */
  733. efx->next_buffer_table = next_buffer_table;
  734. for (i = 0; i < efx->n_channels; i++) {
  735. channel = efx->channel[i];
  736. if (!channel->type->copy)
  737. continue;
  738. rc = efx_probe_channel(channel);
  739. if (rc)
  740. goto rollback;
  741. efx_init_napi_channel(efx->channel[i]);
  742. }
  743. out:
  744. /* Destroy unused channel structures */
  745. for (i = 0; i < efx->n_channels; i++) {
  746. channel = other_channel[i];
  747. if (channel && channel->type->copy) {
  748. efx_fini_napi_channel(channel);
  749. efx_remove_channel(channel);
  750. kfree(channel);
  751. }
  752. }
  753. rc2 = efx_soft_enable_interrupts(efx);
  754. if (rc2) {
  755. rc = rc ? rc : rc2;
  756. netif_err(efx, drv, efx->net_dev,
  757. "unable to restart interrupts on channel reallocation\n");
  758. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  759. } else {
  760. efx_start_all(efx);
  761. efx_device_attach_if_not_resetting(efx);
  762. }
  763. return rc;
  764. rollback:
  765. /* Swap back */
  766. efx->rxq_entries = old_rxq_entries;
  767. efx->txq_entries = old_txq_entries;
  768. for (i = 0; i < efx->n_channels; i++) {
  769. channel = efx->channel[i];
  770. efx->channel[i] = other_channel[i];
  771. other_channel[i] = channel;
  772. }
  773. goto out;
  774. }
  775. void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
  776. {
  777. mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
  778. }
  779. static bool efx_default_channel_want_txqs(struct efx_channel *channel)
  780. {
  781. return channel->channel - channel->efx->tx_channel_offset <
  782. channel->efx->n_tx_channels;
  783. }
  784. static const struct efx_channel_type efx_default_channel_type = {
  785. .pre_probe = efx_channel_dummy_op_int,
  786. .post_remove = efx_channel_dummy_op_void,
  787. .get_name = efx_get_channel_name,
  788. .copy = efx_copy_channel,
  789. .want_txqs = efx_default_channel_want_txqs,
  790. .keep_eventq = false,
  791. .want_pio = true,
  792. };
  793. int efx_channel_dummy_op_int(struct efx_channel *channel)
  794. {
  795. return 0;
  796. }
  797. void efx_channel_dummy_op_void(struct efx_channel *channel)
  798. {
  799. }
  800. /**************************************************************************
  801. *
  802. * Port handling
  803. *
  804. **************************************************************************/
  805. /* This ensures that the kernel is kept informed (via
  806. * netif_carrier_on/off) of the link status, and also maintains the
  807. * link status's stop on the port's TX queue.
  808. */
  809. void efx_link_status_changed(struct efx_nic *efx)
  810. {
  811. struct efx_link_state *link_state = &efx->link_state;
  812. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  813. * that no events are triggered between unregister_netdev() and the
  814. * driver unloading. A more general condition is that NETDEV_CHANGE
  815. * can only be generated between NETDEV_UP and NETDEV_DOWN */
  816. if (!netif_running(efx->net_dev))
  817. return;
  818. if (link_state->up != netif_carrier_ok(efx->net_dev)) {
  819. efx->n_link_state_changes++;
  820. if (link_state->up)
  821. netif_carrier_on(efx->net_dev);
  822. else
  823. netif_carrier_off(efx->net_dev);
  824. }
  825. /* Status message for kernel log */
  826. if (link_state->up)
  827. netif_info(efx, link, efx->net_dev,
  828. "link up at %uMbps %s-duplex (MTU %d)\n",
  829. link_state->speed, link_state->fd ? "full" : "half",
  830. efx->net_dev->mtu);
  831. else
  832. netif_info(efx, link, efx->net_dev, "link down\n");
  833. }
  834. void efx_link_set_advertising(struct efx_nic *efx,
  835. const unsigned long *advertising)
  836. {
  837. memcpy(efx->link_advertising, advertising,
  838. sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
  839. efx->link_advertising[0] |= ADVERTISED_Autoneg;
  840. if (advertising[0] & ADVERTISED_Pause)
  841. efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
  842. else
  843. efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
  844. if (advertising[0] & ADVERTISED_Asym_Pause)
  845. efx->wanted_fc ^= EFX_FC_TX;
  846. }
  847. /* Equivalent to efx_link_set_advertising with all-zeroes, except does not
  848. * force the Autoneg bit on.
  849. */
  850. void efx_link_clear_advertising(struct efx_nic *efx)
  851. {
  852. bitmap_zero(efx->link_advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
  853. efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
  854. }
  855. void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
  856. {
  857. efx->wanted_fc = wanted_fc;
  858. if (efx->link_advertising[0]) {
  859. if (wanted_fc & EFX_FC_RX)
  860. efx->link_advertising[0] |= (ADVERTISED_Pause |
  861. ADVERTISED_Asym_Pause);
  862. else
  863. efx->link_advertising[0] &= ~(ADVERTISED_Pause |
  864. ADVERTISED_Asym_Pause);
  865. if (wanted_fc & EFX_FC_TX)
  866. efx->link_advertising[0] ^= ADVERTISED_Asym_Pause;
  867. }
  868. }
  869. static void efx_fini_port(struct efx_nic *efx);
  870. /* We assume that efx->type->reconfigure_mac will always try to sync RX
  871. * filters and therefore needs to read-lock the filter table against freeing
  872. */
  873. void efx_mac_reconfigure(struct efx_nic *efx)
  874. {
  875. down_read(&efx->filter_sem);
  876. efx->type->reconfigure_mac(efx);
  877. up_read(&efx->filter_sem);
  878. }
  879. /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
  880. * the MAC appropriately. All other PHY configuration changes are pushed
  881. * through phy_op->set_settings(), and pushed asynchronously to the MAC
  882. * through efx_monitor().
  883. *
  884. * Callers must hold the mac_lock
  885. */
  886. int __efx_reconfigure_port(struct efx_nic *efx)
  887. {
  888. enum efx_phy_mode phy_mode;
  889. int rc;
  890. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  891. /* Disable PHY transmit in mac level loopbacks */
  892. phy_mode = efx->phy_mode;
  893. if (LOOPBACK_INTERNAL(efx))
  894. efx->phy_mode |= PHY_MODE_TX_DISABLED;
  895. else
  896. efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
  897. rc = efx->type->reconfigure_port(efx);
  898. if (rc)
  899. efx->phy_mode = phy_mode;
  900. return rc;
  901. }
  902. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  903. * disabled. */
  904. int efx_reconfigure_port(struct efx_nic *efx)
  905. {
  906. int rc;
  907. EFX_ASSERT_RESET_SERIALISED(efx);
  908. mutex_lock(&efx->mac_lock);
  909. rc = __efx_reconfigure_port(efx);
  910. mutex_unlock(&efx->mac_lock);
  911. return rc;
  912. }
  913. /* Asynchronous work item for changing MAC promiscuity and multicast
  914. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  915. * MAC directly. */
  916. static void efx_mac_work(struct work_struct *data)
  917. {
  918. struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
  919. mutex_lock(&efx->mac_lock);
  920. if (efx->port_enabled)
  921. efx_mac_reconfigure(efx);
  922. mutex_unlock(&efx->mac_lock);
  923. }
  924. static int efx_probe_port(struct efx_nic *efx)
  925. {
  926. int rc;
  927. netif_dbg(efx, probe, efx->net_dev, "create port\n");
  928. if (phy_flash_cfg)
  929. efx->phy_mode = PHY_MODE_SPECIAL;
  930. /* Connect up MAC/PHY operations table */
  931. rc = efx->type->probe_port(efx);
  932. if (rc)
  933. return rc;
  934. /* Initialise MAC address to permanent address */
  935. ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
  936. return 0;
  937. }
  938. static int efx_init_port(struct efx_nic *efx)
  939. {
  940. int rc;
  941. netif_dbg(efx, drv, efx->net_dev, "init port\n");
  942. mutex_lock(&efx->mac_lock);
  943. rc = efx->phy_op->init(efx);
  944. if (rc)
  945. goto fail1;
  946. efx->port_initialized = true;
  947. /* Reconfigure the MAC before creating dma queues (required for
  948. * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
  949. efx_mac_reconfigure(efx);
  950. /* Ensure the PHY advertises the correct flow control settings */
  951. rc = efx->phy_op->reconfigure(efx);
  952. if (rc && rc != -EPERM)
  953. goto fail2;
  954. mutex_unlock(&efx->mac_lock);
  955. return 0;
  956. fail2:
  957. efx->phy_op->fini(efx);
  958. fail1:
  959. mutex_unlock(&efx->mac_lock);
  960. return rc;
  961. }
  962. static void efx_start_port(struct efx_nic *efx)
  963. {
  964. netif_dbg(efx, ifup, efx->net_dev, "start port\n");
  965. BUG_ON(efx->port_enabled);
  966. mutex_lock(&efx->mac_lock);
  967. efx->port_enabled = true;
  968. /* Ensure MAC ingress/egress is enabled */
  969. efx_mac_reconfigure(efx);
  970. mutex_unlock(&efx->mac_lock);
  971. }
  972. /* Cancel work for MAC reconfiguration, periodic hardware monitoring
  973. * and the async self-test, wait for them to finish and prevent them
  974. * being scheduled again. This doesn't cover online resets, which
  975. * should only be cancelled when removing the device.
  976. */
  977. static void efx_stop_port(struct efx_nic *efx)
  978. {
  979. netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
  980. EFX_ASSERT_RESET_SERIALISED(efx);
  981. mutex_lock(&efx->mac_lock);
  982. efx->port_enabled = false;
  983. mutex_unlock(&efx->mac_lock);
  984. /* Serialise against efx_set_multicast_list() */
  985. netif_addr_lock_bh(efx->net_dev);
  986. netif_addr_unlock_bh(efx->net_dev);
  987. cancel_delayed_work_sync(&efx->monitor_work);
  988. efx_selftest_async_cancel(efx);
  989. cancel_work_sync(&efx->mac_work);
  990. }
  991. static void efx_fini_port(struct efx_nic *efx)
  992. {
  993. netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
  994. if (!efx->port_initialized)
  995. return;
  996. efx->phy_op->fini(efx);
  997. efx->port_initialized = false;
  998. efx->link_state.up = false;
  999. efx_link_status_changed(efx);
  1000. }
  1001. static void efx_remove_port(struct efx_nic *efx)
  1002. {
  1003. netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
  1004. efx->type->remove_port(efx);
  1005. }
  1006. /**************************************************************************
  1007. *
  1008. * NIC handling
  1009. *
  1010. **************************************************************************/
  1011. static LIST_HEAD(efx_primary_list);
  1012. static LIST_HEAD(efx_unassociated_list);
  1013. static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
  1014. {
  1015. return left->type == right->type &&
  1016. left->vpd_sn && right->vpd_sn &&
  1017. !strcmp(left->vpd_sn, right->vpd_sn);
  1018. }
  1019. static void efx_associate(struct efx_nic *efx)
  1020. {
  1021. struct efx_nic *other, *next;
  1022. if (efx->primary == efx) {
  1023. /* Adding primary function; look for secondaries */
  1024. netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
  1025. list_add_tail(&efx->node, &efx_primary_list);
  1026. list_for_each_entry_safe(other, next, &efx_unassociated_list,
  1027. node) {
  1028. if (efx_same_controller(efx, other)) {
  1029. list_del(&other->node);
  1030. netif_dbg(other, probe, other->net_dev,
  1031. "moving to secondary list of %s %s\n",
  1032. pci_name(efx->pci_dev),
  1033. efx->net_dev->name);
  1034. list_add_tail(&other->node,
  1035. &efx->secondary_list);
  1036. other->primary = efx;
  1037. }
  1038. }
  1039. } else {
  1040. /* Adding secondary function; look for primary */
  1041. list_for_each_entry(other, &efx_primary_list, node) {
  1042. if (efx_same_controller(efx, other)) {
  1043. netif_dbg(efx, probe, efx->net_dev,
  1044. "adding to secondary list of %s %s\n",
  1045. pci_name(other->pci_dev),
  1046. other->net_dev->name);
  1047. list_add_tail(&efx->node,
  1048. &other->secondary_list);
  1049. efx->primary = other;
  1050. return;
  1051. }
  1052. }
  1053. netif_dbg(efx, probe, efx->net_dev,
  1054. "adding to unassociated list\n");
  1055. list_add_tail(&efx->node, &efx_unassociated_list);
  1056. }
  1057. }
  1058. static void efx_dissociate(struct efx_nic *efx)
  1059. {
  1060. struct efx_nic *other, *next;
  1061. list_del(&efx->node);
  1062. efx->primary = NULL;
  1063. list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
  1064. list_del(&other->node);
  1065. netif_dbg(other, probe, other->net_dev,
  1066. "moving to unassociated list\n");
  1067. list_add_tail(&other->node, &efx_unassociated_list);
  1068. other->primary = NULL;
  1069. }
  1070. }
  1071. /* This configures the PCI device to enable I/O and DMA. */
  1072. static int efx_init_io(struct efx_nic *efx)
  1073. {
  1074. struct pci_dev *pci_dev = efx->pci_dev;
  1075. dma_addr_t dma_mask = efx->type->max_dma_mask;
  1076. unsigned int mem_map_size = efx->type->mem_map_size(efx);
  1077. int rc, bar;
  1078. netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
  1079. bar = efx->type->mem_bar(efx);
  1080. rc = pci_enable_device(pci_dev);
  1081. if (rc) {
  1082. netif_err(efx, probe, efx->net_dev,
  1083. "failed to enable PCI device\n");
  1084. goto fail1;
  1085. }
  1086. pci_set_master(pci_dev);
  1087. /* Set the PCI DMA mask. Try all possibilities from our genuine mask
  1088. * down to 32 bits, because some architectures will allow 40 bit
  1089. * masks event though they reject 46 bit masks.
  1090. */
  1091. while (dma_mask > 0x7fffffffUL) {
  1092. rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
  1093. if (rc == 0)
  1094. break;
  1095. dma_mask >>= 1;
  1096. }
  1097. if (rc) {
  1098. netif_err(efx, probe, efx->net_dev,
  1099. "could not find a suitable DMA mask\n");
  1100. goto fail2;
  1101. }
  1102. netif_dbg(efx, probe, efx->net_dev,
  1103. "using DMA mask %llx\n", (unsigned long long) dma_mask);
  1104. efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
  1105. rc = pci_request_region(pci_dev, bar, "sfc");
  1106. if (rc) {
  1107. netif_err(efx, probe, efx->net_dev,
  1108. "request for memory BAR failed\n");
  1109. rc = -EIO;
  1110. goto fail3;
  1111. }
  1112. efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
  1113. if (!efx->membase) {
  1114. netif_err(efx, probe, efx->net_dev,
  1115. "could not map memory BAR at %llx+%x\n",
  1116. (unsigned long long)efx->membase_phys, mem_map_size);
  1117. rc = -ENOMEM;
  1118. goto fail4;
  1119. }
  1120. netif_dbg(efx, probe, efx->net_dev,
  1121. "memory BAR at %llx+%x (virtual %p)\n",
  1122. (unsigned long long)efx->membase_phys, mem_map_size,
  1123. efx->membase);
  1124. return 0;
  1125. fail4:
  1126. pci_release_region(efx->pci_dev, bar);
  1127. fail3:
  1128. efx->membase_phys = 0;
  1129. fail2:
  1130. pci_disable_device(efx->pci_dev);
  1131. fail1:
  1132. return rc;
  1133. }
  1134. static void efx_fini_io(struct efx_nic *efx)
  1135. {
  1136. int bar;
  1137. netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
  1138. if (efx->membase) {
  1139. iounmap(efx->membase);
  1140. efx->membase = NULL;
  1141. }
  1142. if (efx->membase_phys) {
  1143. bar = efx->type->mem_bar(efx);
  1144. pci_release_region(efx->pci_dev, bar);
  1145. efx->membase_phys = 0;
  1146. }
  1147. /* Don't disable bus-mastering if VFs are assigned */
  1148. if (!pci_vfs_assigned(efx->pci_dev))
  1149. pci_disable_device(efx->pci_dev);
  1150. }
  1151. void efx_set_default_rx_indir_table(struct efx_nic *efx,
  1152. struct efx_rss_context *ctx)
  1153. {
  1154. size_t i;
  1155. for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
  1156. ctx->rx_indir_table[i] =
  1157. ethtool_rxfh_indir_default(i, efx->rss_spread);
  1158. }
  1159. static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
  1160. {
  1161. cpumask_var_t thread_mask;
  1162. unsigned int count;
  1163. int cpu;
  1164. if (rss_cpus) {
  1165. count = rss_cpus;
  1166. } else {
  1167. if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
  1168. netif_warn(efx, probe, efx->net_dev,
  1169. "RSS disabled due to allocation failure\n");
  1170. return 1;
  1171. }
  1172. count = 0;
  1173. for_each_online_cpu(cpu) {
  1174. if (!cpumask_test_cpu(cpu, thread_mask)) {
  1175. ++count;
  1176. cpumask_or(thread_mask, thread_mask,
  1177. topology_sibling_cpumask(cpu));
  1178. }
  1179. }
  1180. free_cpumask_var(thread_mask);
  1181. }
  1182. if (count > EFX_MAX_RX_QUEUES) {
  1183. netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn,
  1184. "Reducing number of rx queues from %u to %u.\n",
  1185. count, EFX_MAX_RX_QUEUES);
  1186. count = EFX_MAX_RX_QUEUES;
  1187. }
  1188. /* If RSS is requested for the PF *and* VFs then we can't write RSS
  1189. * table entries that are inaccessible to VFs
  1190. */
  1191. #ifdef CONFIG_SFC_SRIOV
  1192. if (efx->type->sriov_wanted) {
  1193. if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
  1194. count > efx_vf_size(efx)) {
  1195. netif_warn(efx, probe, efx->net_dev,
  1196. "Reducing number of RSS channels from %u to %u for "
  1197. "VF support. Increase vf-msix-limit to use more "
  1198. "channels on the PF.\n",
  1199. count, efx_vf_size(efx));
  1200. count = efx_vf_size(efx);
  1201. }
  1202. }
  1203. #endif
  1204. return count;
  1205. }
  1206. /* Probe the number and type of interrupts we are able to obtain, and
  1207. * the resulting numbers of channels and RX queues.
  1208. */
  1209. static int efx_probe_interrupts(struct efx_nic *efx)
  1210. {
  1211. unsigned int extra_channels = 0;
  1212. unsigned int i, j;
  1213. int rc;
  1214. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
  1215. if (efx->extra_channel_type[i])
  1216. ++extra_channels;
  1217. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  1218. struct msix_entry xentries[EFX_MAX_CHANNELS];
  1219. unsigned int n_channels;
  1220. n_channels = efx_wanted_parallelism(efx);
  1221. if (efx_separate_tx_channels)
  1222. n_channels *= 2;
  1223. n_channels += extra_channels;
  1224. n_channels = min(n_channels, efx->max_channels);
  1225. for (i = 0; i < n_channels; i++)
  1226. xentries[i].entry = i;
  1227. rc = pci_enable_msix_range(efx->pci_dev,
  1228. xentries, 1, n_channels);
  1229. if (rc < 0) {
  1230. /* Fall back to single channel MSI */
  1231. netif_err(efx, drv, efx->net_dev,
  1232. "could not enable MSI-X\n");
  1233. if (efx->type->min_interrupt_mode >= EFX_INT_MODE_MSI)
  1234. efx->interrupt_mode = EFX_INT_MODE_MSI;
  1235. else
  1236. return rc;
  1237. } else if (rc < n_channels) {
  1238. netif_err(efx, drv, efx->net_dev,
  1239. "WARNING: Insufficient MSI-X vectors"
  1240. " available (%d < %u).\n", rc, n_channels);
  1241. netif_err(efx, drv, efx->net_dev,
  1242. "WARNING: Performance may be reduced.\n");
  1243. n_channels = rc;
  1244. }
  1245. if (rc > 0) {
  1246. efx->n_channels = n_channels;
  1247. if (n_channels > extra_channels)
  1248. n_channels -= extra_channels;
  1249. if (efx_separate_tx_channels) {
  1250. efx->n_tx_channels = min(max(n_channels / 2,
  1251. 1U),
  1252. efx->max_tx_channels);
  1253. efx->n_rx_channels = max(n_channels -
  1254. efx->n_tx_channels,
  1255. 1U);
  1256. } else {
  1257. efx->n_tx_channels = min(n_channels,
  1258. efx->max_tx_channels);
  1259. efx->n_rx_channels = n_channels;
  1260. }
  1261. for (i = 0; i < efx->n_channels; i++)
  1262. efx_get_channel(efx, i)->irq =
  1263. xentries[i].vector;
  1264. }
  1265. }
  1266. /* Try single interrupt MSI */
  1267. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  1268. efx->n_channels = 1;
  1269. efx->n_rx_channels = 1;
  1270. efx->n_tx_channels = 1;
  1271. rc = pci_enable_msi(efx->pci_dev);
  1272. if (rc == 0) {
  1273. efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
  1274. } else {
  1275. netif_err(efx, drv, efx->net_dev,
  1276. "could not enable MSI\n");
  1277. if (efx->type->min_interrupt_mode >= EFX_INT_MODE_LEGACY)
  1278. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  1279. else
  1280. return rc;
  1281. }
  1282. }
  1283. /* Assume legacy interrupts */
  1284. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  1285. efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
  1286. efx->n_rx_channels = 1;
  1287. efx->n_tx_channels = 1;
  1288. efx->legacy_irq = efx->pci_dev->irq;
  1289. }
  1290. /* Assign extra channels if possible */
  1291. efx->n_extra_tx_channels = 0;
  1292. j = efx->n_channels;
  1293. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
  1294. if (!efx->extra_channel_type[i])
  1295. continue;
  1296. if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
  1297. efx->n_channels <= extra_channels) {
  1298. efx->extra_channel_type[i]->handle_no_channel(efx);
  1299. } else {
  1300. --j;
  1301. efx_get_channel(efx, j)->type =
  1302. efx->extra_channel_type[i];
  1303. if (efx_channel_has_tx_queues(efx_get_channel(efx, j)))
  1304. efx->n_extra_tx_channels++;
  1305. }
  1306. }
  1307. /* RSS might be usable on VFs even if it is disabled on the PF */
  1308. #ifdef CONFIG_SFC_SRIOV
  1309. if (efx->type->sriov_wanted) {
  1310. efx->rss_spread = ((efx->n_rx_channels > 1 ||
  1311. !efx->type->sriov_wanted(efx)) ?
  1312. efx->n_rx_channels : efx_vf_size(efx));
  1313. return 0;
  1314. }
  1315. #endif
  1316. efx->rss_spread = efx->n_rx_channels;
  1317. return 0;
  1318. }
  1319. #if defined(CONFIG_SMP)
  1320. static void efx_set_interrupt_affinity(struct efx_nic *efx)
  1321. {
  1322. struct efx_channel *channel;
  1323. unsigned int cpu;
  1324. efx_for_each_channel(channel, efx) {
  1325. cpu = cpumask_local_spread(channel->channel,
  1326. pcibus_to_node(efx->pci_dev->bus));
  1327. irq_set_affinity_hint(channel->irq, cpumask_of(cpu));
  1328. }
  1329. }
  1330. static void efx_clear_interrupt_affinity(struct efx_nic *efx)
  1331. {
  1332. struct efx_channel *channel;
  1333. efx_for_each_channel(channel, efx)
  1334. irq_set_affinity_hint(channel->irq, NULL);
  1335. }
  1336. #else
  1337. static void
  1338. efx_set_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
  1339. {
  1340. }
  1341. static void
  1342. efx_clear_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
  1343. {
  1344. }
  1345. #endif /* CONFIG_SMP */
  1346. static int efx_soft_enable_interrupts(struct efx_nic *efx)
  1347. {
  1348. struct efx_channel *channel, *end_channel;
  1349. int rc;
  1350. BUG_ON(efx->state == STATE_DISABLED);
  1351. efx->irq_soft_enabled = true;
  1352. smp_wmb();
  1353. efx_for_each_channel(channel, efx) {
  1354. if (!channel->type->keep_eventq) {
  1355. rc = efx_init_eventq(channel);
  1356. if (rc)
  1357. goto fail;
  1358. }
  1359. efx_start_eventq(channel);
  1360. }
  1361. efx_mcdi_mode_event(efx);
  1362. return 0;
  1363. fail:
  1364. end_channel = channel;
  1365. efx_for_each_channel(channel, efx) {
  1366. if (channel == end_channel)
  1367. break;
  1368. efx_stop_eventq(channel);
  1369. if (!channel->type->keep_eventq)
  1370. efx_fini_eventq(channel);
  1371. }
  1372. return rc;
  1373. }
  1374. static void efx_soft_disable_interrupts(struct efx_nic *efx)
  1375. {
  1376. struct efx_channel *channel;
  1377. if (efx->state == STATE_DISABLED)
  1378. return;
  1379. efx_mcdi_mode_poll(efx);
  1380. efx->irq_soft_enabled = false;
  1381. smp_wmb();
  1382. if (efx->legacy_irq)
  1383. synchronize_irq(efx->legacy_irq);
  1384. efx_for_each_channel(channel, efx) {
  1385. if (channel->irq)
  1386. synchronize_irq(channel->irq);
  1387. efx_stop_eventq(channel);
  1388. if (!channel->type->keep_eventq)
  1389. efx_fini_eventq(channel);
  1390. }
  1391. /* Flush the asynchronous MCDI request queue */
  1392. efx_mcdi_flush_async(efx);
  1393. }
  1394. static int efx_enable_interrupts(struct efx_nic *efx)
  1395. {
  1396. struct efx_channel *channel, *end_channel;
  1397. int rc;
  1398. BUG_ON(efx->state == STATE_DISABLED);
  1399. if (efx->eeh_disabled_legacy_irq) {
  1400. enable_irq(efx->legacy_irq);
  1401. efx->eeh_disabled_legacy_irq = false;
  1402. }
  1403. efx->type->irq_enable_master(efx);
  1404. efx_for_each_channel(channel, efx) {
  1405. if (channel->type->keep_eventq) {
  1406. rc = efx_init_eventq(channel);
  1407. if (rc)
  1408. goto fail;
  1409. }
  1410. }
  1411. rc = efx_soft_enable_interrupts(efx);
  1412. if (rc)
  1413. goto fail;
  1414. return 0;
  1415. fail:
  1416. end_channel = channel;
  1417. efx_for_each_channel(channel, efx) {
  1418. if (channel == end_channel)
  1419. break;
  1420. if (channel->type->keep_eventq)
  1421. efx_fini_eventq(channel);
  1422. }
  1423. efx->type->irq_disable_non_ev(efx);
  1424. return rc;
  1425. }
  1426. static void efx_disable_interrupts(struct efx_nic *efx)
  1427. {
  1428. struct efx_channel *channel;
  1429. efx_soft_disable_interrupts(efx);
  1430. efx_for_each_channel(channel, efx) {
  1431. if (channel->type->keep_eventq)
  1432. efx_fini_eventq(channel);
  1433. }
  1434. efx->type->irq_disable_non_ev(efx);
  1435. }
  1436. static void efx_remove_interrupts(struct efx_nic *efx)
  1437. {
  1438. struct efx_channel *channel;
  1439. /* Remove MSI/MSI-X interrupts */
  1440. efx_for_each_channel(channel, efx)
  1441. channel->irq = 0;
  1442. pci_disable_msi(efx->pci_dev);
  1443. pci_disable_msix(efx->pci_dev);
  1444. /* Remove legacy interrupt */
  1445. efx->legacy_irq = 0;
  1446. }
  1447. static void efx_set_channels(struct efx_nic *efx)
  1448. {
  1449. struct efx_channel *channel;
  1450. struct efx_tx_queue *tx_queue;
  1451. efx->tx_channel_offset =
  1452. efx_separate_tx_channels ?
  1453. efx->n_channels - efx->n_tx_channels : 0;
  1454. /* We need to mark which channels really have RX and TX
  1455. * queues, and adjust the TX queue numbers if we have separate
  1456. * RX-only and TX-only channels.
  1457. */
  1458. efx_for_each_channel(channel, efx) {
  1459. if (channel->channel < efx->n_rx_channels)
  1460. channel->rx_queue.core_index = channel->channel;
  1461. else
  1462. channel->rx_queue.core_index = -1;
  1463. efx_for_each_channel_tx_queue(tx_queue, channel)
  1464. tx_queue->queue -= (efx->tx_channel_offset *
  1465. EFX_TXQ_TYPES);
  1466. }
  1467. }
  1468. static int efx_probe_nic(struct efx_nic *efx)
  1469. {
  1470. int rc;
  1471. netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
  1472. /* Carry out hardware-type specific initialisation */
  1473. rc = efx->type->probe(efx);
  1474. if (rc)
  1475. return rc;
  1476. do {
  1477. if (!efx->max_channels || !efx->max_tx_channels) {
  1478. netif_err(efx, drv, efx->net_dev,
  1479. "Insufficient resources to allocate"
  1480. " any channels\n");
  1481. rc = -ENOSPC;
  1482. goto fail1;
  1483. }
  1484. /* Determine the number of channels and queues by trying
  1485. * to hook in MSI-X interrupts.
  1486. */
  1487. rc = efx_probe_interrupts(efx);
  1488. if (rc)
  1489. goto fail1;
  1490. efx_set_channels(efx);
  1491. /* dimension_resources can fail with EAGAIN */
  1492. rc = efx->type->dimension_resources(efx);
  1493. if (rc != 0 && rc != -EAGAIN)
  1494. goto fail2;
  1495. if (rc == -EAGAIN)
  1496. /* try again with new max_channels */
  1497. efx_remove_interrupts(efx);
  1498. } while (rc == -EAGAIN);
  1499. if (efx->n_channels > 1)
  1500. netdev_rss_key_fill(efx->rss_context.rx_hash_key,
  1501. sizeof(efx->rss_context.rx_hash_key));
  1502. efx_set_default_rx_indir_table(efx, &efx->rss_context);
  1503. netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
  1504. netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
  1505. /* Initialise the interrupt moderation settings */
  1506. efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
  1507. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
  1508. true);
  1509. return 0;
  1510. fail2:
  1511. efx_remove_interrupts(efx);
  1512. fail1:
  1513. efx->type->remove(efx);
  1514. return rc;
  1515. }
  1516. static void efx_remove_nic(struct efx_nic *efx)
  1517. {
  1518. netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
  1519. efx_remove_interrupts(efx);
  1520. efx->type->remove(efx);
  1521. }
  1522. static int efx_probe_filters(struct efx_nic *efx)
  1523. {
  1524. int rc;
  1525. init_rwsem(&efx->filter_sem);
  1526. mutex_lock(&efx->mac_lock);
  1527. down_write(&efx->filter_sem);
  1528. rc = efx->type->filter_table_probe(efx);
  1529. if (rc)
  1530. goto out_unlock;
  1531. #ifdef CONFIG_RFS_ACCEL
  1532. if (efx->type->offload_features & NETIF_F_NTUPLE) {
  1533. struct efx_channel *channel;
  1534. int i, success = 1;
  1535. efx_for_each_channel(channel, efx) {
  1536. channel->rps_flow_id =
  1537. kcalloc(efx->type->max_rx_ip_filters,
  1538. sizeof(*channel->rps_flow_id),
  1539. GFP_KERNEL);
  1540. if (!channel->rps_flow_id)
  1541. success = 0;
  1542. else
  1543. for (i = 0;
  1544. i < efx->type->max_rx_ip_filters;
  1545. ++i)
  1546. channel->rps_flow_id[i] =
  1547. RPS_FLOW_ID_INVALID;
  1548. }
  1549. if (!success) {
  1550. efx_for_each_channel(channel, efx)
  1551. kfree(channel->rps_flow_id);
  1552. efx->type->filter_table_remove(efx);
  1553. rc = -ENOMEM;
  1554. goto out_unlock;
  1555. }
  1556. efx->rps_expire_index = efx->rps_expire_channel = 0;
  1557. }
  1558. #endif
  1559. out_unlock:
  1560. up_write(&efx->filter_sem);
  1561. mutex_unlock(&efx->mac_lock);
  1562. return rc;
  1563. }
  1564. static void efx_remove_filters(struct efx_nic *efx)
  1565. {
  1566. #ifdef CONFIG_RFS_ACCEL
  1567. struct efx_channel *channel;
  1568. efx_for_each_channel(channel, efx)
  1569. kfree(channel->rps_flow_id);
  1570. #endif
  1571. down_write(&efx->filter_sem);
  1572. efx->type->filter_table_remove(efx);
  1573. up_write(&efx->filter_sem);
  1574. }
  1575. static void efx_restore_filters(struct efx_nic *efx)
  1576. {
  1577. down_read(&efx->filter_sem);
  1578. efx->type->filter_table_restore(efx);
  1579. up_read(&efx->filter_sem);
  1580. }
  1581. /**************************************************************************
  1582. *
  1583. * NIC startup/shutdown
  1584. *
  1585. *************************************************************************/
  1586. static int efx_probe_all(struct efx_nic *efx)
  1587. {
  1588. int rc;
  1589. rc = efx_probe_nic(efx);
  1590. if (rc) {
  1591. netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
  1592. goto fail1;
  1593. }
  1594. rc = efx_probe_port(efx);
  1595. if (rc) {
  1596. netif_err(efx, probe, efx->net_dev, "failed to create port\n");
  1597. goto fail2;
  1598. }
  1599. BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
  1600. if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
  1601. rc = -EINVAL;
  1602. goto fail3;
  1603. }
  1604. efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
  1605. #ifdef CONFIG_SFC_SRIOV
  1606. rc = efx->type->vswitching_probe(efx);
  1607. if (rc) /* not fatal; the PF will still work fine */
  1608. netif_warn(efx, probe, efx->net_dev,
  1609. "failed to setup vswitching rc=%d;"
  1610. " VFs may not function\n", rc);
  1611. #endif
  1612. rc = efx_probe_filters(efx);
  1613. if (rc) {
  1614. netif_err(efx, probe, efx->net_dev,
  1615. "failed to create filter tables\n");
  1616. goto fail4;
  1617. }
  1618. rc = efx_probe_channels(efx);
  1619. if (rc)
  1620. goto fail5;
  1621. return 0;
  1622. fail5:
  1623. efx_remove_filters(efx);
  1624. fail4:
  1625. #ifdef CONFIG_SFC_SRIOV
  1626. efx->type->vswitching_remove(efx);
  1627. #endif
  1628. fail3:
  1629. efx_remove_port(efx);
  1630. fail2:
  1631. efx_remove_nic(efx);
  1632. fail1:
  1633. return rc;
  1634. }
  1635. /* If the interface is supposed to be running but is not, start
  1636. * the hardware and software data path, regular activity for the port
  1637. * (MAC statistics, link polling, etc.) and schedule the port to be
  1638. * reconfigured. Interrupts must already be enabled. This function
  1639. * is safe to call multiple times, so long as the NIC is not disabled.
  1640. * Requires the RTNL lock.
  1641. */
  1642. static void efx_start_all(struct efx_nic *efx)
  1643. {
  1644. EFX_ASSERT_RESET_SERIALISED(efx);
  1645. BUG_ON(efx->state == STATE_DISABLED);
  1646. /* Check that it is appropriate to restart the interface. All
  1647. * of these flags are safe to read under just the rtnl lock */
  1648. if (efx->port_enabled || !netif_running(efx->net_dev) ||
  1649. efx->reset_pending)
  1650. return;
  1651. efx_start_port(efx);
  1652. efx_start_datapath(efx);
  1653. /* Start the hardware monitor if there is one */
  1654. if (efx->type->monitor != NULL)
  1655. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1656. efx_monitor_interval);
  1657. /* Link state detection is normally event-driven; we have
  1658. * to poll now because we could have missed a change
  1659. */
  1660. mutex_lock(&efx->mac_lock);
  1661. if (efx->phy_op->poll(efx))
  1662. efx_link_status_changed(efx);
  1663. mutex_unlock(&efx->mac_lock);
  1664. efx->type->start_stats(efx);
  1665. efx->type->pull_stats(efx);
  1666. spin_lock_bh(&efx->stats_lock);
  1667. efx->type->update_stats(efx, NULL, NULL);
  1668. spin_unlock_bh(&efx->stats_lock);
  1669. }
  1670. /* Quiesce the hardware and software data path, and regular activity
  1671. * for the port without bringing the link down. Safe to call multiple
  1672. * times with the NIC in almost any state, but interrupts should be
  1673. * enabled. Requires the RTNL lock.
  1674. */
  1675. static void efx_stop_all(struct efx_nic *efx)
  1676. {
  1677. EFX_ASSERT_RESET_SERIALISED(efx);
  1678. /* port_enabled can be read safely under the rtnl lock */
  1679. if (!efx->port_enabled)
  1680. return;
  1681. /* update stats before we go down so we can accurately count
  1682. * rx_nodesc_drops
  1683. */
  1684. efx->type->pull_stats(efx);
  1685. spin_lock_bh(&efx->stats_lock);
  1686. efx->type->update_stats(efx, NULL, NULL);
  1687. spin_unlock_bh(&efx->stats_lock);
  1688. efx->type->stop_stats(efx);
  1689. efx_stop_port(efx);
  1690. /* Stop the kernel transmit interface. This is only valid if
  1691. * the device is stopped or detached; otherwise the watchdog
  1692. * may fire immediately.
  1693. */
  1694. WARN_ON(netif_running(efx->net_dev) &&
  1695. netif_device_present(efx->net_dev));
  1696. netif_tx_disable(efx->net_dev);
  1697. efx_stop_datapath(efx);
  1698. }
  1699. static void efx_remove_all(struct efx_nic *efx)
  1700. {
  1701. efx_remove_channels(efx);
  1702. efx_remove_filters(efx);
  1703. #ifdef CONFIG_SFC_SRIOV
  1704. efx->type->vswitching_remove(efx);
  1705. #endif
  1706. efx_remove_port(efx);
  1707. efx_remove_nic(efx);
  1708. }
  1709. /**************************************************************************
  1710. *
  1711. * Interrupt moderation
  1712. *
  1713. **************************************************************************/
  1714. unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
  1715. {
  1716. if (usecs == 0)
  1717. return 0;
  1718. if (usecs * 1000 < efx->timer_quantum_ns)
  1719. return 1; /* never round down to 0 */
  1720. return usecs * 1000 / efx->timer_quantum_ns;
  1721. }
  1722. unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
  1723. {
  1724. /* We must round up when converting ticks to microseconds
  1725. * because we round down when converting the other way.
  1726. */
  1727. return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
  1728. }
  1729. /* Set interrupt moderation parameters */
  1730. int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
  1731. unsigned int rx_usecs, bool rx_adaptive,
  1732. bool rx_may_override_tx)
  1733. {
  1734. struct efx_channel *channel;
  1735. unsigned int timer_max_us;
  1736. EFX_ASSERT_RESET_SERIALISED(efx);
  1737. timer_max_us = efx->timer_max_ns / 1000;
  1738. if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
  1739. return -EINVAL;
  1740. if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
  1741. !rx_may_override_tx) {
  1742. netif_err(efx, drv, efx->net_dev, "Channels are shared. "
  1743. "RX and TX IRQ moderation must be equal\n");
  1744. return -EINVAL;
  1745. }
  1746. efx->irq_rx_adaptive = rx_adaptive;
  1747. efx->irq_rx_moderation_us = rx_usecs;
  1748. efx_for_each_channel(channel, efx) {
  1749. if (efx_channel_has_rx_queue(channel))
  1750. channel->irq_moderation_us = rx_usecs;
  1751. else if (efx_channel_has_tx_queues(channel))
  1752. channel->irq_moderation_us = tx_usecs;
  1753. }
  1754. return 0;
  1755. }
  1756. void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
  1757. unsigned int *rx_usecs, bool *rx_adaptive)
  1758. {
  1759. *rx_adaptive = efx->irq_rx_adaptive;
  1760. *rx_usecs = efx->irq_rx_moderation_us;
  1761. /* If channels are shared between RX and TX, so is IRQ
  1762. * moderation. Otherwise, IRQ moderation is the same for all
  1763. * TX channels and is not adaptive.
  1764. */
  1765. if (efx->tx_channel_offset == 0) {
  1766. *tx_usecs = *rx_usecs;
  1767. } else {
  1768. struct efx_channel *tx_channel;
  1769. tx_channel = efx->channel[efx->tx_channel_offset];
  1770. *tx_usecs = tx_channel->irq_moderation_us;
  1771. }
  1772. }
  1773. /**************************************************************************
  1774. *
  1775. * Hardware monitor
  1776. *
  1777. **************************************************************************/
  1778. /* Run periodically off the general workqueue */
  1779. static void efx_monitor(struct work_struct *data)
  1780. {
  1781. struct efx_nic *efx = container_of(data, struct efx_nic,
  1782. monitor_work.work);
  1783. netif_vdbg(efx, timer, efx->net_dev,
  1784. "hardware monitor executing on CPU %d\n",
  1785. raw_smp_processor_id());
  1786. BUG_ON(efx->type->monitor == NULL);
  1787. /* If the mac_lock is already held then it is likely a port
  1788. * reconfiguration is already in place, which will likely do
  1789. * most of the work of monitor() anyway. */
  1790. if (mutex_trylock(&efx->mac_lock)) {
  1791. if (efx->port_enabled)
  1792. efx->type->monitor(efx);
  1793. mutex_unlock(&efx->mac_lock);
  1794. }
  1795. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1796. efx_monitor_interval);
  1797. }
  1798. /**************************************************************************
  1799. *
  1800. * ioctls
  1801. *
  1802. *************************************************************************/
  1803. /* Net device ioctl
  1804. * Context: process, rtnl_lock() held.
  1805. */
  1806. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1807. {
  1808. struct efx_nic *efx = netdev_priv(net_dev);
  1809. struct mii_ioctl_data *data = if_mii(ifr);
  1810. if (cmd == SIOCSHWTSTAMP)
  1811. return efx_ptp_set_ts_config(efx, ifr);
  1812. if (cmd == SIOCGHWTSTAMP)
  1813. return efx_ptp_get_ts_config(efx, ifr);
  1814. /* Convert phy_id from older PRTAD/DEVAD format */
  1815. if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
  1816. (data->phy_id & 0xfc00) == 0x0400)
  1817. data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
  1818. return mdio_mii_ioctl(&efx->mdio, data, cmd);
  1819. }
  1820. /**************************************************************************
  1821. *
  1822. * NAPI interface
  1823. *
  1824. **************************************************************************/
  1825. static void efx_init_napi_channel(struct efx_channel *channel)
  1826. {
  1827. struct efx_nic *efx = channel->efx;
  1828. channel->napi_dev = efx->net_dev;
  1829. netif_napi_add(channel->napi_dev, &channel->napi_str,
  1830. efx_poll, napi_weight);
  1831. }
  1832. static void efx_init_napi(struct efx_nic *efx)
  1833. {
  1834. struct efx_channel *channel;
  1835. efx_for_each_channel(channel, efx)
  1836. efx_init_napi_channel(channel);
  1837. }
  1838. static void efx_fini_napi_channel(struct efx_channel *channel)
  1839. {
  1840. if (channel->napi_dev)
  1841. netif_napi_del(&channel->napi_str);
  1842. channel->napi_dev = NULL;
  1843. }
  1844. static void efx_fini_napi(struct efx_nic *efx)
  1845. {
  1846. struct efx_channel *channel;
  1847. efx_for_each_channel(channel, efx)
  1848. efx_fini_napi_channel(channel);
  1849. }
  1850. /**************************************************************************
  1851. *
  1852. * Kernel netpoll interface
  1853. *
  1854. *************************************************************************/
  1855. #ifdef CONFIG_NET_POLL_CONTROLLER
  1856. /* Although in the common case interrupts will be disabled, this is not
  1857. * guaranteed. However, all our work happens inside the NAPI callback,
  1858. * so no locking is required.
  1859. */
  1860. static void efx_netpoll(struct net_device *net_dev)
  1861. {
  1862. struct efx_nic *efx = netdev_priv(net_dev);
  1863. struct efx_channel *channel;
  1864. efx_for_each_channel(channel, efx)
  1865. efx_schedule_channel(channel);
  1866. }
  1867. #endif
  1868. /**************************************************************************
  1869. *
  1870. * Kernel net device interface
  1871. *
  1872. *************************************************************************/
  1873. /* Context: process, rtnl_lock() held. */
  1874. int efx_net_open(struct net_device *net_dev)
  1875. {
  1876. struct efx_nic *efx = netdev_priv(net_dev);
  1877. int rc;
  1878. netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
  1879. raw_smp_processor_id());
  1880. rc = efx_check_disabled(efx);
  1881. if (rc)
  1882. return rc;
  1883. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1884. return -EBUSY;
  1885. if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
  1886. return -EIO;
  1887. /* Notify the kernel of the link state polled during driver load,
  1888. * before the monitor starts running */
  1889. efx_link_status_changed(efx);
  1890. efx_start_all(efx);
  1891. if (efx->state == STATE_DISABLED || efx->reset_pending)
  1892. netif_device_detach(efx->net_dev);
  1893. efx_selftest_async_start(efx);
  1894. return 0;
  1895. }
  1896. /* Context: process, rtnl_lock() held.
  1897. * Note that the kernel will ignore our return code; this method
  1898. * should really be a void.
  1899. */
  1900. int efx_net_stop(struct net_device *net_dev)
  1901. {
  1902. struct efx_nic *efx = netdev_priv(net_dev);
  1903. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  1904. raw_smp_processor_id());
  1905. /* Stop the device and flush all the channels */
  1906. efx_stop_all(efx);
  1907. return 0;
  1908. }
  1909. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1910. static void efx_net_stats(struct net_device *net_dev,
  1911. struct rtnl_link_stats64 *stats)
  1912. {
  1913. struct efx_nic *efx = netdev_priv(net_dev);
  1914. spin_lock_bh(&efx->stats_lock);
  1915. efx->type->update_stats(efx, NULL, stats);
  1916. spin_unlock_bh(&efx->stats_lock);
  1917. }
  1918. /* Context: netif_tx_lock held, BHs disabled. */
  1919. static void efx_watchdog(struct net_device *net_dev)
  1920. {
  1921. struct efx_nic *efx = netdev_priv(net_dev);
  1922. netif_err(efx, tx_err, efx->net_dev,
  1923. "TX stuck with port_enabled=%d: resetting channels\n",
  1924. efx->port_enabled);
  1925. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1926. }
  1927. /* Context: process, rtnl_lock() held. */
  1928. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1929. {
  1930. struct efx_nic *efx = netdev_priv(net_dev);
  1931. int rc;
  1932. rc = efx_check_disabled(efx);
  1933. if (rc)
  1934. return rc;
  1935. netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
  1936. efx_device_detach_sync(efx);
  1937. efx_stop_all(efx);
  1938. mutex_lock(&efx->mac_lock);
  1939. net_dev->mtu = new_mtu;
  1940. efx_mac_reconfigure(efx);
  1941. mutex_unlock(&efx->mac_lock);
  1942. efx_start_all(efx);
  1943. efx_device_attach_if_not_resetting(efx);
  1944. return 0;
  1945. }
  1946. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1947. {
  1948. struct efx_nic *efx = netdev_priv(net_dev);
  1949. struct sockaddr *addr = data;
  1950. u8 *new_addr = addr->sa_data;
  1951. u8 old_addr[6];
  1952. int rc;
  1953. if (!is_valid_ether_addr(new_addr)) {
  1954. netif_err(efx, drv, efx->net_dev,
  1955. "invalid ethernet MAC address requested: %pM\n",
  1956. new_addr);
  1957. return -EADDRNOTAVAIL;
  1958. }
  1959. /* save old address */
  1960. ether_addr_copy(old_addr, net_dev->dev_addr);
  1961. ether_addr_copy(net_dev->dev_addr, new_addr);
  1962. if (efx->type->set_mac_address) {
  1963. rc = efx->type->set_mac_address(efx);
  1964. if (rc) {
  1965. ether_addr_copy(net_dev->dev_addr, old_addr);
  1966. return rc;
  1967. }
  1968. }
  1969. /* Reconfigure the MAC */
  1970. mutex_lock(&efx->mac_lock);
  1971. efx_mac_reconfigure(efx);
  1972. mutex_unlock(&efx->mac_lock);
  1973. return 0;
  1974. }
  1975. /* Context: netif_addr_lock held, BHs disabled. */
  1976. static void efx_set_rx_mode(struct net_device *net_dev)
  1977. {
  1978. struct efx_nic *efx = netdev_priv(net_dev);
  1979. if (efx->port_enabled)
  1980. queue_work(efx->workqueue, &efx->mac_work);
  1981. /* Otherwise efx_start_port() will do this */
  1982. }
  1983. static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
  1984. {
  1985. struct efx_nic *efx = netdev_priv(net_dev);
  1986. int rc;
  1987. /* If disabling RX n-tuple filtering, clear existing filters */
  1988. if (net_dev->features & ~data & NETIF_F_NTUPLE) {
  1989. rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
  1990. if (rc)
  1991. return rc;
  1992. }
  1993. /* If Rx VLAN filter is changed, update filters via mac_reconfigure.
  1994. * If rx-fcs is changed, mac_reconfigure updates that too.
  1995. */
  1996. if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
  1997. NETIF_F_RXFCS)) {
  1998. /* efx_set_rx_mode() will schedule MAC work to update filters
  1999. * when a new features are finally set in net_dev.
  2000. */
  2001. efx_set_rx_mode(net_dev);
  2002. }
  2003. return 0;
  2004. }
  2005. static int efx_get_phys_port_id(struct net_device *net_dev,
  2006. struct netdev_phys_item_id *ppid)
  2007. {
  2008. struct efx_nic *efx = netdev_priv(net_dev);
  2009. if (efx->type->get_phys_port_id)
  2010. return efx->type->get_phys_port_id(efx, ppid);
  2011. else
  2012. return -EOPNOTSUPP;
  2013. }
  2014. static int efx_get_phys_port_name(struct net_device *net_dev,
  2015. char *name, size_t len)
  2016. {
  2017. struct efx_nic *efx = netdev_priv(net_dev);
  2018. if (snprintf(name, len, "p%u", efx->port_num) >= len)
  2019. return -EINVAL;
  2020. return 0;
  2021. }
  2022. static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
  2023. {
  2024. struct efx_nic *efx = netdev_priv(net_dev);
  2025. if (efx->type->vlan_rx_add_vid)
  2026. return efx->type->vlan_rx_add_vid(efx, proto, vid);
  2027. else
  2028. return -EOPNOTSUPP;
  2029. }
  2030. static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
  2031. {
  2032. struct efx_nic *efx = netdev_priv(net_dev);
  2033. if (efx->type->vlan_rx_kill_vid)
  2034. return efx->type->vlan_rx_kill_vid(efx, proto, vid);
  2035. else
  2036. return -EOPNOTSUPP;
  2037. }
  2038. static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
  2039. {
  2040. switch (in) {
  2041. case UDP_TUNNEL_TYPE_VXLAN:
  2042. return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
  2043. case UDP_TUNNEL_TYPE_GENEVE:
  2044. return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
  2045. default:
  2046. return -1;
  2047. }
  2048. }
  2049. static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
  2050. {
  2051. struct efx_nic *efx = netdev_priv(dev);
  2052. struct efx_udp_tunnel tnl;
  2053. int efx_tunnel_type;
  2054. efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
  2055. if (efx_tunnel_type < 0)
  2056. return;
  2057. tnl.type = (u16)efx_tunnel_type;
  2058. tnl.port = ti->port;
  2059. if (efx->type->udp_tnl_add_port)
  2060. (void)efx->type->udp_tnl_add_port(efx, tnl);
  2061. }
  2062. static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
  2063. {
  2064. struct efx_nic *efx = netdev_priv(dev);
  2065. struct efx_udp_tunnel tnl;
  2066. int efx_tunnel_type;
  2067. efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
  2068. if (efx_tunnel_type < 0)
  2069. return;
  2070. tnl.type = (u16)efx_tunnel_type;
  2071. tnl.port = ti->port;
  2072. if (efx->type->udp_tnl_del_port)
  2073. (void)efx->type->udp_tnl_del_port(efx, tnl);
  2074. }
  2075. static const struct net_device_ops efx_netdev_ops = {
  2076. .ndo_open = efx_net_open,
  2077. .ndo_stop = efx_net_stop,
  2078. .ndo_get_stats64 = efx_net_stats,
  2079. .ndo_tx_timeout = efx_watchdog,
  2080. .ndo_start_xmit = efx_hard_start_xmit,
  2081. .ndo_validate_addr = eth_validate_addr,
  2082. .ndo_do_ioctl = efx_ioctl,
  2083. .ndo_change_mtu = efx_change_mtu,
  2084. .ndo_set_mac_address = efx_set_mac_address,
  2085. .ndo_set_rx_mode = efx_set_rx_mode,
  2086. .ndo_set_features = efx_set_features,
  2087. .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
  2088. .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
  2089. #ifdef CONFIG_SFC_SRIOV
  2090. .ndo_set_vf_mac = efx_sriov_set_vf_mac,
  2091. .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
  2092. .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
  2093. .ndo_get_vf_config = efx_sriov_get_vf_config,
  2094. .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
  2095. #endif
  2096. .ndo_get_phys_port_id = efx_get_phys_port_id,
  2097. .ndo_get_phys_port_name = efx_get_phys_port_name,
  2098. #ifdef CONFIG_NET_POLL_CONTROLLER
  2099. .ndo_poll_controller = efx_netpoll,
  2100. #endif
  2101. .ndo_setup_tc = efx_setup_tc,
  2102. #ifdef CONFIG_RFS_ACCEL
  2103. .ndo_rx_flow_steer = efx_filter_rfs,
  2104. #endif
  2105. .ndo_udp_tunnel_add = efx_udp_tunnel_add,
  2106. .ndo_udp_tunnel_del = efx_udp_tunnel_del,
  2107. };
  2108. static void efx_update_name(struct efx_nic *efx)
  2109. {
  2110. strcpy(efx->name, efx->net_dev->name);
  2111. efx_mtd_rename(efx);
  2112. efx_set_channel_names(efx);
  2113. }
  2114. static int efx_netdev_event(struct notifier_block *this,
  2115. unsigned long event, void *ptr)
  2116. {
  2117. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  2118. if ((net_dev->netdev_ops == &efx_netdev_ops) &&
  2119. event == NETDEV_CHANGENAME)
  2120. efx_update_name(netdev_priv(net_dev));
  2121. return NOTIFY_DONE;
  2122. }
  2123. static struct notifier_block efx_netdev_notifier = {
  2124. .notifier_call = efx_netdev_event,
  2125. };
  2126. static ssize_t
  2127. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  2128. {
  2129. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2130. return sprintf(buf, "%d\n", efx->phy_type);
  2131. }
  2132. static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
  2133. #ifdef CONFIG_SFC_MCDI_LOGGING
  2134. static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
  2135. char *buf)
  2136. {
  2137. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2138. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  2139. return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
  2140. }
  2141. static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
  2142. const char *buf, size_t count)
  2143. {
  2144. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2145. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  2146. bool enable = count > 0 && *buf != '0';
  2147. mcdi->logging_enabled = enable;
  2148. return count;
  2149. }
  2150. static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
  2151. #endif
  2152. static int efx_register_netdev(struct efx_nic *efx)
  2153. {
  2154. struct net_device *net_dev = efx->net_dev;
  2155. struct efx_channel *channel;
  2156. int rc;
  2157. net_dev->watchdog_timeo = 5 * HZ;
  2158. net_dev->irq = efx->pci_dev->irq;
  2159. net_dev->netdev_ops = &efx_netdev_ops;
  2160. if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
  2161. net_dev->priv_flags |= IFF_UNICAST_FLT;
  2162. net_dev->ethtool_ops = &efx_ethtool_ops;
  2163. net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
  2164. net_dev->min_mtu = EFX_MIN_MTU;
  2165. net_dev->max_mtu = EFX_MAX_MTU;
  2166. rtnl_lock();
  2167. /* Enable resets to be scheduled and check whether any were
  2168. * already requested. If so, the NIC is probably hosed so we
  2169. * abort.
  2170. */
  2171. efx->state = STATE_READY;
  2172. smp_mb(); /* ensure we change state before checking reset_pending */
  2173. if (efx->reset_pending) {
  2174. netif_err(efx, probe, efx->net_dev,
  2175. "aborting probe due to scheduled reset\n");
  2176. rc = -EIO;
  2177. goto fail_locked;
  2178. }
  2179. rc = dev_alloc_name(net_dev, net_dev->name);
  2180. if (rc < 0)
  2181. goto fail_locked;
  2182. efx_update_name(efx);
  2183. /* Always start with carrier off; PHY events will detect the link */
  2184. netif_carrier_off(net_dev);
  2185. rc = register_netdevice(net_dev);
  2186. if (rc)
  2187. goto fail_locked;
  2188. efx_for_each_channel(channel, efx) {
  2189. struct efx_tx_queue *tx_queue;
  2190. efx_for_each_channel_tx_queue(tx_queue, channel)
  2191. efx_init_tx_queue_core_txq(tx_queue);
  2192. }
  2193. efx_associate(efx);
  2194. rtnl_unlock();
  2195. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  2196. if (rc) {
  2197. netif_err(efx, drv, efx->net_dev,
  2198. "failed to init net dev attributes\n");
  2199. goto fail_registered;
  2200. }
  2201. #ifdef CONFIG_SFC_MCDI_LOGGING
  2202. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
  2203. if (rc) {
  2204. netif_err(efx, drv, efx->net_dev,
  2205. "failed to init net dev attributes\n");
  2206. goto fail_attr_mcdi_logging;
  2207. }
  2208. #endif
  2209. return 0;
  2210. #ifdef CONFIG_SFC_MCDI_LOGGING
  2211. fail_attr_mcdi_logging:
  2212. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  2213. #endif
  2214. fail_registered:
  2215. rtnl_lock();
  2216. efx_dissociate(efx);
  2217. unregister_netdevice(net_dev);
  2218. fail_locked:
  2219. efx->state = STATE_UNINIT;
  2220. rtnl_unlock();
  2221. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  2222. return rc;
  2223. }
  2224. static void efx_unregister_netdev(struct efx_nic *efx)
  2225. {
  2226. if (!efx->net_dev)
  2227. return;
  2228. BUG_ON(netdev_priv(efx->net_dev) != efx);
  2229. if (efx_dev_registered(efx)) {
  2230. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  2231. #ifdef CONFIG_SFC_MCDI_LOGGING
  2232. device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
  2233. #endif
  2234. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  2235. unregister_netdev(efx->net_dev);
  2236. }
  2237. }
  2238. /**************************************************************************
  2239. *
  2240. * Device reset and suspend
  2241. *
  2242. **************************************************************************/
  2243. /* Tears down the entire software state and most of the hardware state
  2244. * before reset. */
  2245. void efx_reset_down(struct efx_nic *efx, enum reset_type method)
  2246. {
  2247. EFX_ASSERT_RESET_SERIALISED(efx);
  2248. if (method == RESET_TYPE_MCDI_TIMEOUT)
  2249. efx->type->prepare_flr(efx);
  2250. efx_stop_all(efx);
  2251. efx_disable_interrupts(efx);
  2252. mutex_lock(&efx->mac_lock);
  2253. mutex_lock(&efx->rss_lock);
  2254. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
  2255. method != RESET_TYPE_DATAPATH)
  2256. efx->phy_op->fini(efx);
  2257. efx->type->fini(efx);
  2258. }
  2259. /* This function will always ensure that the locks acquired in
  2260. * efx_reset_down() are released. A failure return code indicates
  2261. * that we were unable to reinitialise the hardware, and the
  2262. * driver should be disabled. If ok is false, then the rx and tx
  2263. * engines are not restarted, pending a RESET_DISABLE. */
  2264. int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
  2265. {
  2266. int rc;
  2267. EFX_ASSERT_RESET_SERIALISED(efx);
  2268. if (method == RESET_TYPE_MCDI_TIMEOUT)
  2269. efx->type->finish_flr(efx);
  2270. /* Ensure that SRAM is initialised even if we're disabling the device */
  2271. rc = efx->type->init(efx);
  2272. if (rc) {
  2273. netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
  2274. goto fail;
  2275. }
  2276. if (!ok)
  2277. goto fail;
  2278. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
  2279. method != RESET_TYPE_DATAPATH) {
  2280. rc = efx->phy_op->init(efx);
  2281. if (rc)
  2282. goto fail;
  2283. rc = efx->phy_op->reconfigure(efx);
  2284. if (rc && rc != -EPERM)
  2285. netif_err(efx, drv, efx->net_dev,
  2286. "could not restore PHY settings\n");
  2287. }
  2288. rc = efx_enable_interrupts(efx);
  2289. if (rc)
  2290. goto fail;
  2291. #ifdef CONFIG_SFC_SRIOV
  2292. rc = efx->type->vswitching_restore(efx);
  2293. if (rc) /* not fatal; the PF will still work fine */
  2294. netif_warn(efx, probe, efx->net_dev,
  2295. "failed to restore vswitching rc=%d;"
  2296. " VFs may not function\n", rc);
  2297. #endif
  2298. if (efx->type->rx_restore_rss_contexts)
  2299. efx->type->rx_restore_rss_contexts(efx);
  2300. mutex_unlock(&efx->rss_lock);
  2301. down_read(&efx->filter_sem);
  2302. efx_restore_filters(efx);
  2303. up_read(&efx->filter_sem);
  2304. if (efx->type->sriov_reset)
  2305. efx->type->sriov_reset(efx);
  2306. mutex_unlock(&efx->mac_lock);
  2307. efx_start_all(efx);
  2308. if (efx->type->udp_tnl_push_ports)
  2309. efx->type->udp_tnl_push_ports(efx);
  2310. return 0;
  2311. fail:
  2312. efx->port_initialized = false;
  2313. mutex_unlock(&efx->rss_lock);
  2314. mutex_unlock(&efx->mac_lock);
  2315. return rc;
  2316. }
  2317. /* Reset the NIC using the specified method. Note that the reset may
  2318. * fail, in which case the card will be left in an unusable state.
  2319. *
  2320. * Caller must hold the rtnl_lock.
  2321. */
  2322. int efx_reset(struct efx_nic *efx, enum reset_type method)
  2323. {
  2324. int rc, rc2;
  2325. bool disabled;
  2326. netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
  2327. RESET_TYPE(method));
  2328. efx_device_detach_sync(efx);
  2329. efx_reset_down(efx, method);
  2330. rc = efx->type->reset(efx, method);
  2331. if (rc) {
  2332. netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
  2333. goto out;
  2334. }
  2335. /* Clear flags for the scopes we covered. We assume the NIC and
  2336. * driver are now quiescent so that there is no race here.
  2337. */
  2338. if (method < RESET_TYPE_MAX_METHOD)
  2339. efx->reset_pending &= -(1 << (method + 1));
  2340. else /* it doesn't fit into the well-ordered scope hierarchy */
  2341. __clear_bit(method, &efx->reset_pending);
  2342. /* Reinitialise bus-mastering, which may have been turned off before
  2343. * the reset was scheduled. This is still appropriate, even in the
  2344. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  2345. * can respond to requests. */
  2346. pci_set_master(efx->pci_dev);
  2347. out:
  2348. /* Leave device stopped if necessary */
  2349. disabled = rc ||
  2350. method == RESET_TYPE_DISABLE ||
  2351. method == RESET_TYPE_RECOVER_OR_DISABLE;
  2352. rc2 = efx_reset_up(efx, method, !disabled);
  2353. if (rc2) {
  2354. disabled = true;
  2355. if (!rc)
  2356. rc = rc2;
  2357. }
  2358. if (disabled) {
  2359. dev_close(efx->net_dev);
  2360. netif_err(efx, drv, efx->net_dev, "has been disabled\n");
  2361. efx->state = STATE_DISABLED;
  2362. } else {
  2363. netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
  2364. efx_device_attach_if_not_resetting(efx);
  2365. }
  2366. return rc;
  2367. }
  2368. /* Try recovery mechanisms.
  2369. * For now only EEH is supported.
  2370. * Returns 0 if the recovery mechanisms are unsuccessful.
  2371. * Returns a non-zero value otherwise.
  2372. */
  2373. int efx_try_recovery(struct efx_nic *efx)
  2374. {
  2375. #ifdef CONFIG_EEH
  2376. /* A PCI error can occur and not be seen by EEH because nothing
  2377. * happens on the PCI bus. In this case the driver may fail and
  2378. * schedule a 'recover or reset', leading to this recovery handler.
  2379. * Manually call the eeh failure check function.
  2380. */
  2381. struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
  2382. if (eeh_dev_check_failure(eehdev)) {
  2383. /* The EEH mechanisms will handle the error and reset the
  2384. * device if necessary.
  2385. */
  2386. return 1;
  2387. }
  2388. #endif
  2389. return 0;
  2390. }
  2391. static void efx_wait_for_bist_end(struct efx_nic *efx)
  2392. {
  2393. int i;
  2394. for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
  2395. if (efx_mcdi_poll_reboot(efx))
  2396. goto out;
  2397. msleep(BIST_WAIT_DELAY_MS);
  2398. }
  2399. netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
  2400. out:
  2401. /* Either way unset the BIST flag. If we found no reboot we probably
  2402. * won't recover, but we should try.
  2403. */
  2404. efx->mc_bist_for_other_fn = false;
  2405. }
  2406. /* The worker thread exists so that code that cannot sleep can
  2407. * schedule a reset for later.
  2408. */
  2409. static void efx_reset_work(struct work_struct *data)
  2410. {
  2411. struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
  2412. unsigned long pending;
  2413. enum reset_type method;
  2414. pending = READ_ONCE(efx->reset_pending);
  2415. method = fls(pending) - 1;
  2416. if (method == RESET_TYPE_MC_BIST)
  2417. efx_wait_for_bist_end(efx);
  2418. if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
  2419. method == RESET_TYPE_RECOVER_OR_ALL) &&
  2420. efx_try_recovery(efx))
  2421. return;
  2422. if (!pending)
  2423. return;
  2424. rtnl_lock();
  2425. /* We checked the state in efx_schedule_reset() but it may
  2426. * have changed by now. Now that we have the RTNL lock,
  2427. * it cannot change again.
  2428. */
  2429. if (efx->state == STATE_READY)
  2430. (void)efx_reset(efx, method);
  2431. rtnl_unlock();
  2432. }
  2433. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  2434. {
  2435. enum reset_type method;
  2436. if (efx->state == STATE_RECOVERY) {
  2437. netif_dbg(efx, drv, efx->net_dev,
  2438. "recovering: skip scheduling %s reset\n",
  2439. RESET_TYPE(type));
  2440. return;
  2441. }
  2442. switch (type) {
  2443. case RESET_TYPE_INVISIBLE:
  2444. case RESET_TYPE_ALL:
  2445. case RESET_TYPE_RECOVER_OR_ALL:
  2446. case RESET_TYPE_WORLD:
  2447. case RESET_TYPE_DISABLE:
  2448. case RESET_TYPE_RECOVER_OR_DISABLE:
  2449. case RESET_TYPE_DATAPATH:
  2450. case RESET_TYPE_MC_BIST:
  2451. case RESET_TYPE_MCDI_TIMEOUT:
  2452. method = type;
  2453. netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
  2454. RESET_TYPE(method));
  2455. break;
  2456. default:
  2457. method = efx->type->map_reset_reason(type);
  2458. netif_dbg(efx, drv, efx->net_dev,
  2459. "scheduling %s reset for %s\n",
  2460. RESET_TYPE(method), RESET_TYPE(type));
  2461. break;
  2462. }
  2463. set_bit(method, &efx->reset_pending);
  2464. smp_mb(); /* ensure we change reset_pending before checking state */
  2465. /* If we're not READY then just leave the flags set as the cue
  2466. * to abort probing or reschedule the reset later.
  2467. */
  2468. if (READ_ONCE(efx->state) != STATE_READY)
  2469. return;
  2470. /* efx_process_channel() will no longer read events once a
  2471. * reset is scheduled. So switch back to poll'd MCDI completions. */
  2472. efx_mcdi_mode_poll(efx);
  2473. queue_work(reset_workqueue, &efx->reset_work);
  2474. }
  2475. /**************************************************************************
  2476. *
  2477. * List of NICs we support
  2478. *
  2479. **************************************************************************/
  2480. /* PCI device ID table */
  2481. static const struct pci_device_id efx_pci_table[] = {
  2482. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
  2483. .driver_data = (unsigned long) &siena_a0_nic_type},
  2484. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
  2485. .driver_data = (unsigned long) &siena_a0_nic_type},
  2486. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
  2487. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  2488. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
  2489. .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
  2490. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
  2491. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  2492. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
  2493. .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
  2494. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
  2495. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  2496. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
  2497. .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
  2498. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
  2499. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  2500. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
  2501. .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
  2502. {0} /* end of list */
  2503. };
  2504. /**************************************************************************
  2505. *
  2506. * Dummy PHY/MAC operations
  2507. *
  2508. * Can be used for some unimplemented operations
  2509. * Needed so all function pointers are valid and do not have to be tested
  2510. * before use
  2511. *
  2512. **************************************************************************/
  2513. int efx_port_dummy_op_int(struct efx_nic *efx)
  2514. {
  2515. return 0;
  2516. }
  2517. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  2518. static bool efx_port_dummy_op_poll(struct efx_nic *efx)
  2519. {
  2520. return false;
  2521. }
  2522. static const struct efx_phy_operations efx_dummy_phy_operations = {
  2523. .init = efx_port_dummy_op_int,
  2524. .reconfigure = efx_port_dummy_op_int,
  2525. .poll = efx_port_dummy_op_poll,
  2526. .fini = efx_port_dummy_op_void,
  2527. };
  2528. /**************************************************************************
  2529. *
  2530. * Data housekeeping
  2531. *
  2532. **************************************************************************/
  2533. /* This zeroes out and then fills in the invariants in a struct
  2534. * efx_nic (including all sub-structures).
  2535. */
  2536. static int efx_init_struct(struct efx_nic *efx,
  2537. struct pci_dev *pci_dev, struct net_device *net_dev)
  2538. {
  2539. int rc = -ENOMEM, i;
  2540. /* Initialise common structures */
  2541. INIT_LIST_HEAD(&efx->node);
  2542. INIT_LIST_HEAD(&efx->secondary_list);
  2543. spin_lock_init(&efx->biu_lock);
  2544. #ifdef CONFIG_SFC_MTD
  2545. INIT_LIST_HEAD(&efx->mtd_list);
  2546. #endif
  2547. INIT_WORK(&efx->reset_work, efx_reset_work);
  2548. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  2549. INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
  2550. efx->pci_dev = pci_dev;
  2551. efx->msg_enable = debug;
  2552. efx->state = STATE_UNINIT;
  2553. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  2554. efx->net_dev = net_dev;
  2555. efx->rx_prefix_size = efx->type->rx_prefix_size;
  2556. efx->rx_ip_align =
  2557. NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
  2558. efx->rx_packet_hash_offset =
  2559. efx->type->rx_hash_offset - efx->type->rx_prefix_size;
  2560. efx->rx_packet_ts_offset =
  2561. efx->type->rx_ts_offset - efx->type->rx_prefix_size;
  2562. INIT_LIST_HEAD(&efx->rss_context.list);
  2563. mutex_init(&efx->rss_lock);
  2564. spin_lock_init(&efx->stats_lock);
  2565. efx->vi_stride = EFX_DEFAULT_VI_STRIDE;
  2566. efx->num_mac_stats = MC_CMD_MAC_NSTATS;
  2567. BUILD_BUG_ON(MC_CMD_MAC_NSTATS - 1 != MC_CMD_MAC_GENERATION_END);
  2568. mutex_init(&efx->mac_lock);
  2569. #ifdef CONFIG_RFS_ACCEL
  2570. mutex_init(&efx->rps_mutex);
  2571. spin_lock_init(&efx->rps_hash_lock);
  2572. /* Failure to allocate is not fatal, but may degrade ARFS performance */
  2573. efx->rps_hash_table = kcalloc(EFX_ARFS_HASH_TABLE_SIZE,
  2574. sizeof(*efx->rps_hash_table), GFP_KERNEL);
  2575. #endif
  2576. efx->phy_op = &efx_dummy_phy_operations;
  2577. efx->mdio.dev = net_dev;
  2578. INIT_WORK(&efx->mac_work, efx_mac_work);
  2579. init_waitqueue_head(&efx->flush_wq);
  2580. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  2581. efx->channel[i] = efx_alloc_channel(efx, i, NULL);
  2582. if (!efx->channel[i])
  2583. goto fail;
  2584. efx->msi_context[i].efx = efx;
  2585. efx->msi_context[i].index = i;
  2586. }
  2587. /* Higher numbered interrupt modes are less capable! */
  2588. if (WARN_ON_ONCE(efx->type->max_interrupt_mode >
  2589. efx->type->min_interrupt_mode)) {
  2590. rc = -EIO;
  2591. goto fail;
  2592. }
  2593. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  2594. interrupt_mode);
  2595. efx->interrupt_mode = min(efx->type->min_interrupt_mode,
  2596. interrupt_mode);
  2597. /* Would be good to use the net_dev name, but we're too early */
  2598. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  2599. pci_name(pci_dev));
  2600. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  2601. if (!efx->workqueue)
  2602. goto fail;
  2603. return 0;
  2604. fail:
  2605. efx_fini_struct(efx);
  2606. return rc;
  2607. }
  2608. static void efx_fini_struct(struct efx_nic *efx)
  2609. {
  2610. int i;
  2611. #ifdef CONFIG_RFS_ACCEL
  2612. kfree(efx->rps_hash_table);
  2613. #endif
  2614. for (i = 0; i < EFX_MAX_CHANNELS; i++)
  2615. kfree(efx->channel[i]);
  2616. kfree(efx->vpd_sn);
  2617. if (efx->workqueue) {
  2618. destroy_workqueue(efx->workqueue);
  2619. efx->workqueue = NULL;
  2620. }
  2621. }
  2622. void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
  2623. {
  2624. u64 n_rx_nodesc_trunc = 0;
  2625. struct efx_channel *channel;
  2626. efx_for_each_channel(channel, efx)
  2627. n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
  2628. stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
  2629. stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
  2630. }
  2631. bool efx_filter_spec_equal(const struct efx_filter_spec *left,
  2632. const struct efx_filter_spec *right)
  2633. {
  2634. if ((left->match_flags ^ right->match_flags) |
  2635. ((left->flags ^ right->flags) &
  2636. (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
  2637. return false;
  2638. return memcmp(&left->outer_vid, &right->outer_vid,
  2639. sizeof(struct efx_filter_spec) -
  2640. offsetof(struct efx_filter_spec, outer_vid)) == 0;
  2641. }
  2642. u32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
  2643. {
  2644. BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
  2645. return jhash2((const u32 *)&spec->outer_vid,
  2646. (sizeof(struct efx_filter_spec) -
  2647. offsetof(struct efx_filter_spec, outer_vid)) / 4,
  2648. 0);
  2649. }
  2650. #ifdef CONFIG_RFS_ACCEL
  2651. bool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
  2652. bool *force)
  2653. {
  2654. if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
  2655. /* ARFS is currently updating this entry, leave it */
  2656. return false;
  2657. }
  2658. if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
  2659. /* ARFS tried and failed to update this, so it's probably out
  2660. * of date. Remove the filter and the ARFS rule entry.
  2661. */
  2662. rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
  2663. *force = true;
  2664. return true;
  2665. } else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
  2666. /* ARFS has moved on, so old filter is not needed. Since we did
  2667. * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
  2668. * not be removed by efx_rps_hash_del() subsequently.
  2669. */
  2670. *force = true;
  2671. return true;
  2672. }
  2673. /* Remove it iff ARFS wants to. */
  2674. return true;
  2675. }
  2676. struct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
  2677. const struct efx_filter_spec *spec)
  2678. {
  2679. u32 hash = efx_filter_spec_hash(spec);
  2680. WARN_ON(!spin_is_locked(&efx->rps_hash_lock));
  2681. if (!efx->rps_hash_table)
  2682. return NULL;
  2683. return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
  2684. }
  2685. struct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
  2686. const struct efx_filter_spec *spec)
  2687. {
  2688. struct efx_arfs_rule *rule;
  2689. struct hlist_head *head;
  2690. struct hlist_node *node;
  2691. head = efx_rps_hash_bucket(efx, spec);
  2692. if (!head)
  2693. return NULL;
  2694. hlist_for_each(node, head) {
  2695. rule = container_of(node, struct efx_arfs_rule, node);
  2696. if (efx_filter_spec_equal(spec, &rule->spec))
  2697. return rule;
  2698. }
  2699. return NULL;
  2700. }
  2701. struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
  2702. const struct efx_filter_spec *spec,
  2703. bool *new)
  2704. {
  2705. struct efx_arfs_rule *rule;
  2706. struct hlist_head *head;
  2707. struct hlist_node *node;
  2708. head = efx_rps_hash_bucket(efx, spec);
  2709. if (!head)
  2710. return NULL;
  2711. hlist_for_each(node, head) {
  2712. rule = container_of(node, struct efx_arfs_rule, node);
  2713. if (efx_filter_spec_equal(spec, &rule->spec)) {
  2714. *new = false;
  2715. return rule;
  2716. }
  2717. }
  2718. rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
  2719. *new = true;
  2720. if (rule) {
  2721. memcpy(&rule->spec, spec, sizeof(rule->spec));
  2722. hlist_add_head(&rule->node, head);
  2723. }
  2724. return rule;
  2725. }
  2726. void efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
  2727. {
  2728. struct efx_arfs_rule *rule;
  2729. struct hlist_head *head;
  2730. struct hlist_node *node;
  2731. head = efx_rps_hash_bucket(efx, spec);
  2732. if (WARN_ON(!head))
  2733. return;
  2734. hlist_for_each(node, head) {
  2735. rule = container_of(node, struct efx_arfs_rule, node);
  2736. if (efx_filter_spec_equal(spec, &rule->spec)) {
  2737. /* Someone already reused the entry. We know that if
  2738. * this check doesn't fire (i.e. filter_id == REMOVING)
  2739. * then the REMOVING mark was put there by our caller,
  2740. * because caller is holding a lock on filter table and
  2741. * only holders of that lock set REMOVING.
  2742. */
  2743. if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
  2744. return;
  2745. hlist_del(node);
  2746. kfree(rule);
  2747. return;
  2748. }
  2749. }
  2750. /* We didn't find it. */
  2751. WARN_ON(1);
  2752. }
  2753. #endif
  2754. /* RSS contexts. We're using linked lists and crappy O(n) algorithms, because
  2755. * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
  2756. */
  2757. struct efx_rss_context *efx_alloc_rss_context_entry(struct efx_nic *efx)
  2758. {
  2759. struct list_head *head = &efx->rss_context.list;
  2760. struct efx_rss_context *ctx, *new;
  2761. u32 id = 1; /* Don't use zero, that refers to the master RSS context */
  2762. WARN_ON(!mutex_is_locked(&efx->rss_lock));
  2763. /* Search for first gap in the numbering */
  2764. list_for_each_entry(ctx, head, list) {
  2765. if (ctx->user_id != id)
  2766. break;
  2767. id++;
  2768. /* Check for wrap. If this happens, we have nearly 2^32
  2769. * allocated RSS contexts, which seems unlikely.
  2770. */
  2771. if (WARN_ON_ONCE(!id))
  2772. return NULL;
  2773. }
  2774. /* Create the new entry */
  2775. new = kmalloc(sizeof(struct efx_rss_context), GFP_KERNEL);
  2776. if (!new)
  2777. return NULL;
  2778. new->context_id = EFX_EF10_RSS_CONTEXT_INVALID;
  2779. new->rx_hash_udp_4tuple = false;
  2780. /* Insert the new entry into the gap */
  2781. new->user_id = id;
  2782. list_add_tail(&new->list, &ctx->list);
  2783. return new;
  2784. }
  2785. struct efx_rss_context *efx_find_rss_context_entry(struct efx_nic *efx, u32 id)
  2786. {
  2787. struct list_head *head = &efx->rss_context.list;
  2788. struct efx_rss_context *ctx;
  2789. WARN_ON(!mutex_is_locked(&efx->rss_lock));
  2790. list_for_each_entry(ctx, head, list)
  2791. if (ctx->user_id == id)
  2792. return ctx;
  2793. return NULL;
  2794. }
  2795. void efx_free_rss_context_entry(struct efx_rss_context *ctx)
  2796. {
  2797. list_del(&ctx->list);
  2798. kfree(ctx);
  2799. }
  2800. /**************************************************************************
  2801. *
  2802. * PCI interface
  2803. *
  2804. **************************************************************************/
  2805. /* Main body of final NIC shutdown code
  2806. * This is called only at module unload (or hotplug removal).
  2807. */
  2808. static void efx_pci_remove_main(struct efx_nic *efx)
  2809. {
  2810. /* Flush reset_work. It can no longer be scheduled since we
  2811. * are not READY.
  2812. */
  2813. BUG_ON(efx->state == STATE_READY);
  2814. cancel_work_sync(&efx->reset_work);
  2815. efx_disable_interrupts(efx);
  2816. efx_clear_interrupt_affinity(efx);
  2817. efx_nic_fini_interrupt(efx);
  2818. efx_fini_port(efx);
  2819. efx->type->fini(efx);
  2820. efx_fini_napi(efx);
  2821. efx_remove_all(efx);
  2822. }
  2823. /* Final NIC shutdown
  2824. * This is called only at module unload (or hotplug removal). A PF can call
  2825. * this on its VFs to ensure they are unbound first.
  2826. */
  2827. static void efx_pci_remove(struct pci_dev *pci_dev)
  2828. {
  2829. struct efx_nic *efx;
  2830. efx = pci_get_drvdata(pci_dev);
  2831. if (!efx)
  2832. return;
  2833. /* Mark the NIC as fini, then stop the interface */
  2834. rtnl_lock();
  2835. efx_dissociate(efx);
  2836. dev_close(efx->net_dev);
  2837. efx_disable_interrupts(efx);
  2838. efx->state = STATE_UNINIT;
  2839. rtnl_unlock();
  2840. if (efx->type->sriov_fini)
  2841. efx->type->sriov_fini(efx);
  2842. efx_unregister_netdev(efx);
  2843. efx_mtd_remove(efx);
  2844. efx_pci_remove_main(efx);
  2845. efx_fini_io(efx);
  2846. netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
  2847. efx_fini_struct(efx);
  2848. free_netdev(efx->net_dev);
  2849. pci_disable_pcie_error_reporting(pci_dev);
  2850. };
  2851. /* NIC VPD information
  2852. * Called during probe to display the part number of the
  2853. * installed NIC. VPD is potentially very large but this should
  2854. * always appear within the first 512 bytes.
  2855. */
  2856. #define SFC_VPD_LEN 512
  2857. static void efx_probe_vpd_strings(struct efx_nic *efx)
  2858. {
  2859. struct pci_dev *dev = efx->pci_dev;
  2860. char vpd_data[SFC_VPD_LEN];
  2861. ssize_t vpd_size;
  2862. int ro_start, ro_size, i, j;
  2863. /* Get the vpd data from the device */
  2864. vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
  2865. if (vpd_size <= 0) {
  2866. netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
  2867. return;
  2868. }
  2869. /* Get the Read only section */
  2870. ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
  2871. if (ro_start < 0) {
  2872. netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
  2873. return;
  2874. }
  2875. ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
  2876. j = ro_size;
  2877. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  2878. if (i + j > vpd_size)
  2879. j = vpd_size - i;
  2880. /* Get the Part number */
  2881. i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
  2882. if (i < 0) {
  2883. netif_err(efx, drv, efx->net_dev, "Part number not found\n");
  2884. return;
  2885. }
  2886. j = pci_vpd_info_field_size(&vpd_data[i]);
  2887. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2888. if (i + j > vpd_size) {
  2889. netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
  2890. return;
  2891. }
  2892. netif_info(efx, drv, efx->net_dev,
  2893. "Part Number : %.*s\n", j, &vpd_data[i]);
  2894. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  2895. j = ro_size;
  2896. i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
  2897. if (i < 0) {
  2898. netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
  2899. return;
  2900. }
  2901. j = pci_vpd_info_field_size(&vpd_data[i]);
  2902. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2903. if (i + j > vpd_size) {
  2904. netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
  2905. return;
  2906. }
  2907. efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
  2908. if (!efx->vpd_sn)
  2909. return;
  2910. snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
  2911. }
  2912. /* Main body of NIC initialisation
  2913. * This is called at module load (or hotplug insertion, theoretically).
  2914. */
  2915. static int efx_pci_probe_main(struct efx_nic *efx)
  2916. {
  2917. int rc;
  2918. /* Do start-of-day initialisation */
  2919. rc = efx_probe_all(efx);
  2920. if (rc)
  2921. goto fail1;
  2922. efx_init_napi(efx);
  2923. rc = efx->type->init(efx);
  2924. if (rc) {
  2925. netif_err(efx, probe, efx->net_dev,
  2926. "failed to initialise NIC\n");
  2927. goto fail3;
  2928. }
  2929. rc = efx_init_port(efx);
  2930. if (rc) {
  2931. netif_err(efx, probe, efx->net_dev,
  2932. "failed to initialise port\n");
  2933. goto fail4;
  2934. }
  2935. rc = efx_nic_init_interrupt(efx);
  2936. if (rc)
  2937. goto fail5;
  2938. efx_set_interrupt_affinity(efx);
  2939. rc = efx_enable_interrupts(efx);
  2940. if (rc)
  2941. goto fail6;
  2942. return 0;
  2943. fail6:
  2944. efx_clear_interrupt_affinity(efx);
  2945. efx_nic_fini_interrupt(efx);
  2946. fail5:
  2947. efx_fini_port(efx);
  2948. fail4:
  2949. efx->type->fini(efx);
  2950. fail3:
  2951. efx_fini_napi(efx);
  2952. efx_remove_all(efx);
  2953. fail1:
  2954. return rc;
  2955. }
  2956. static int efx_pci_probe_post_io(struct efx_nic *efx)
  2957. {
  2958. struct net_device *net_dev = efx->net_dev;
  2959. int rc = efx_pci_probe_main(efx);
  2960. if (rc)
  2961. return rc;
  2962. if (efx->type->sriov_init) {
  2963. rc = efx->type->sriov_init(efx);
  2964. if (rc)
  2965. netif_err(efx, probe, efx->net_dev,
  2966. "SR-IOV can't be enabled rc %d\n", rc);
  2967. }
  2968. /* Determine netdevice features */
  2969. net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
  2970. NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
  2971. if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
  2972. net_dev->features |= NETIF_F_TSO6;
  2973. /* Check whether device supports TSO */
  2974. if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
  2975. net_dev->features &= ~NETIF_F_ALL_TSO;
  2976. /* Mask for features that also apply to VLAN devices */
  2977. net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
  2978. NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
  2979. NETIF_F_RXCSUM);
  2980. net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
  2981. /* Disable receiving frames with bad FCS, by default. */
  2982. net_dev->features &= ~NETIF_F_RXALL;
  2983. /* Disable VLAN filtering by default. It may be enforced if
  2984. * the feature is fixed (i.e. VLAN filters are required to
  2985. * receive VLAN tagged packets due to vPort restrictions).
  2986. */
  2987. net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
  2988. net_dev->features |= efx->fixed_features;
  2989. rc = efx_register_netdev(efx);
  2990. if (!rc)
  2991. return 0;
  2992. efx_pci_remove_main(efx);
  2993. return rc;
  2994. }
  2995. /* NIC initialisation
  2996. *
  2997. * This is called at module load (or hotplug insertion,
  2998. * theoretically). It sets up PCI mappings, resets the NIC,
  2999. * sets up and registers the network devices with the kernel and hooks
  3000. * the interrupt service routine. It does not prepare the device for
  3001. * transmission; this is left to the first time one of the network
  3002. * interfaces is brought up (i.e. efx_net_open).
  3003. */
  3004. static int efx_pci_probe(struct pci_dev *pci_dev,
  3005. const struct pci_device_id *entry)
  3006. {
  3007. struct net_device *net_dev;
  3008. struct efx_nic *efx;
  3009. int rc;
  3010. /* Allocate and initialise a struct net_device and struct efx_nic */
  3011. net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
  3012. EFX_MAX_RX_QUEUES);
  3013. if (!net_dev)
  3014. return -ENOMEM;
  3015. efx = netdev_priv(net_dev);
  3016. efx->type = (const struct efx_nic_type *) entry->driver_data;
  3017. efx->fixed_features |= NETIF_F_HIGHDMA;
  3018. pci_set_drvdata(pci_dev, efx);
  3019. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  3020. rc = efx_init_struct(efx, pci_dev, net_dev);
  3021. if (rc)
  3022. goto fail1;
  3023. netif_info(efx, probe, efx->net_dev,
  3024. "Solarflare NIC detected\n");
  3025. if (!efx->type->is_vf)
  3026. efx_probe_vpd_strings(efx);
  3027. /* Set up basic I/O (BAR mappings etc) */
  3028. rc = efx_init_io(efx);
  3029. if (rc)
  3030. goto fail2;
  3031. rc = efx_pci_probe_post_io(efx);
  3032. if (rc) {
  3033. /* On failure, retry once immediately.
  3034. * If we aborted probe due to a scheduled reset, dismiss it.
  3035. */
  3036. efx->reset_pending = 0;
  3037. rc = efx_pci_probe_post_io(efx);
  3038. if (rc) {
  3039. /* On another failure, retry once more
  3040. * after a 50-305ms delay.
  3041. */
  3042. unsigned char r;
  3043. get_random_bytes(&r, 1);
  3044. msleep((unsigned int)r + 50);
  3045. efx->reset_pending = 0;
  3046. rc = efx_pci_probe_post_io(efx);
  3047. }
  3048. }
  3049. if (rc)
  3050. goto fail3;
  3051. netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
  3052. /* Try to create MTDs, but allow this to fail */
  3053. rtnl_lock();
  3054. rc = efx_mtd_probe(efx);
  3055. rtnl_unlock();
  3056. if (rc && rc != -EPERM)
  3057. netif_warn(efx, probe, efx->net_dev,
  3058. "failed to create MTDs (%d)\n", rc);
  3059. rc = pci_enable_pcie_error_reporting(pci_dev);
  3060. if (rc && rc != -EINVAL)
  3061. netif_notice(efx, probe, efx->net_dev,
  3062. "PCIE error reporting unavailable (%d).\n",
  3063. rc);
  3064. if (efx->type->udp_tnl_push_ports)
  3065. efx->type->udp_tnl_push_ports(efx);
  3066. return 0;
  3067. fail3:
  3068. efx_fini_io(efx);
  3069. fail2:
  3070. efx_fini_struct(efx);
  3071. fail1:
  3072. WARN_ON(rc > 0);
  3073. netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
  3074. free_netdev(net_dev);
  3075. return rc;
  3076. }
  3077. /* efx_pci_sriov_configure returns the actual number of Virtual Functions
  3078. * enabled on success
  3079. */
  3080. #ifdef CONFIG_SFC_SRIOV
  3081. static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
  3082. {
  3083. int rc;
  3084. struct efx_nic *efx = pci_get_drvdata(dev);
  3085. if (efx->type->sriov_configure) {
  3086. rc = efx->type->sriov_configure(efx, num_vfs);
  3087. if (rc)
  3088. return rc;
  3089. else
  3090. return num_vfs;
  3091. } else
  3092. return -EOPNOTSUPP;
  3093. }
  3094. #endif
  3095. static int efx_pm_freeze(struct device *dev)
  3096. {
  3097. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  3098. rtnl_lock();
  3099. if (efx->state != STATE_DISABLED) {
  3100. efx->state = STATE_UNINIT;
  3101. efx_device_detach_sync(efx);
  3102. efx_stop_all(efx);
  3103. efx_disable_interrupts(efx);
  3104. }
  3105. rtnl_unlock();
  3106. return 0;
  3107. }
  3108. static int efx_pm_thaw(struct device *dev)
  3109. {
  3110. int rc;
  3111. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  3112. rtnl_lock();
  3113. if (efx->state != STATE_DISABLED) {
  3114. rc = efx_enable_interrupts(efx);
  3115. if (rc)
  3116. goto fail;
  3117. mutex_lock(&efx->mac_lock);
  3118. efx->phy_op->reconfigure(efx);
  3119. mutex_unlock(&efx->mac_lock);
  3120. efx_start_all(efx);
  3121. efx_device_attach_if_not_resetting(efx);
  3122. efx->state = STATE_READY;
  3123. efx->type->resume_wol(efx);
  3124. }
  3125. rtnl_unlock();
  3126. /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
  3127. queue_work(reset_workqueue, &efx->reset_work);
  3128. return 0;
  3129. fail:
  3130. rtnl_unlock();
  3131. return rc;
  3132. }
  3133. static int efx_pm_poweroff(struct device *dev)
  3134. {
  3135. struct pci_dev *pci_dev = to_pci_dev(dev);
  3136. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  3137. efx->type->fini(efx);
  3138. efx->reset_pending = 0;
  3139. pci_save_state(pci_dev);
  3140. return pci_set_power_state(pci_dev, PCI_D3hot);
  3141. }
  3142. /* Used for both resume and restore */
  3143. static int efx_pm_resume(struct device *dev)
  3144. {
  3145. struct pci_dev *pci_dev = to_pci_dev(dev);
  3146. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  3147. int rc;
  3148. rc = pci_set_power_state(pci_dev, PCI_D0);
  3149. if (rc)
  3150. return rc;
  3151. pci_restore_state(pci_dev);
  3152. rc = pci_enable_device(pci_dev);
  3153. if (rc)
  3154. return rc;
  3155. pci_set_master(efx->pci_dev);
  3156. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  3157. if (rc)
  3158. return rc;
  3159. rc = efx->type->init(efx);
  3160. if (rc)
  3161. return rc;
  3162. rc = efx_pm_thaw(dev);
  3163. return rc;
  3164. }
  3165. static int efx_pm_suspend(struct device *dev)
  3166. {
  3167. int rc;
  3168. efx_pm_freeze(dev);
  3169. rc = efx_pm_poweroff(dev);
  3170. if (rc)
  3171. efx_pm_resume(dev);
  3172. return rc;
  3173. }
  3174. static const struct dev_pm_ops efx_pm_ops = {
  3175. .suspend = efx_pm_suspend,
  3176. .resume = efx_pm_resume,
  3177. .freeze = efx_pm_freeze,
  3178. .thaw = efx_pm_thaw,
  3179. .poweroff = efx_pm_poweroff,
  3180. .restore = efx_pm_resume,
  3181. };
  3182. /* A PCI error affecting this device was detected.
  3183. * At this point MMIO and DMA may be disabled.
  3184. * Stop the software path and request a slot reset.
  3185. */
  3186. static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
  3187. enum pci_channel_state state)
  3188. {
  3189. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  3190. struct efx_nic *efx = pci_get_drvdata(pdev);
  3191. if (state == pci_channel_io_perm_failure)
  3192. return PCI_ERS_RESULT_DISCONNECT;
  3193. rtnl_lock();
  3194. if (efx->state != STATE_DISABLED) {
  3195. efx->state = STATE_RECOVERY;
  3196. efx->reset_pending = 0;
  3197. efx_device_detach_sync(efx);
  3198. efx_stop_all(efx);
  3199. efx_disable_interrupts(efx);
  3200. status = PCI_ERS_RESULT_NEED_RESET;
  3201. } else {
  3202. /* If the interface is disabled we don't want to do anything
  3203. * with it.
  3204. */
  3205. status = PCI_ERS_RESULT_RECOVERED;
  3206. }
  3207. rtnl_unlock();
  3208. pci_disable_device(pdev);
  3209. return status;
  3210. }
  3211. /* Fake a successful reset, which will be performed later in efx_io_resume. */
  3212. static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
  3213. {
  3214. struct efx_nic *efx = pci_get_drvdata(pdev);
  3215. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  3216. int rc;
  3217. if (pci_enable_device(pdev)) {
  3218. netif_err(efx, hw, efx->net_dev,
  3219. "Cannot re-enable PCI device after reset.\n");
  3220. status = PCI_ERS_RESULT_DISCONNECT;
  3221. }
  3222. rc = pci_cleanup_aer_uncorrect_error_status(pdev);
  3223. if (rc) {
  3224. netif_err(efx, hw, efx->net_dev,
  3225. "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
  3226. /* Non-fatal error. Continue. */
  3227. }
  3228. return status;
  3229. }
  3230. /* Perform the actual reset and resume I/O operations. */
  3231. static void efx_io_resume(struct pci_dev *pdev)
  3232. {
  3233. struct efx_nic *efx = pci_get_drvdata(pdev);
  3234. int rc;
  3235. rtnl_lock();
  3236. if (efx->state == STATE_DISABLED)
  3237. goto out;
  3238. rc = efx_reset(efx, RESET_TYPE_ALL);
  3239. if (rc) {
  3240. netif_err(efx, hw, efx->net_dev,
  3241. "efx_reset failed after PCI error (%d)\n", rc);
  3242. } else {
  3243. efx->state = STATE_READY;
  3244. netif_dbg(efx, hw, efx->net_dev,
  3245. "Done resetting and resuming IO after PCI error.\n");
  3246. }
  3247. out:
  3248. rtnl_unlock();
  3249. }
  3250. /* For simplicity and reliability, we always require a slot reset and try to
  3251. * reset the hardware when a pci error affecting the device is detected.
  3252. * We leave both the link_reset and mmio_enabled callback unimplemented:
  3253. * with our request for slot reset the mmio_enabled callback will never be
  3254. * called, and the link_reset callback is not used by AER or EEH mechanisms.
  3255. */
  3256. static const struct pci_error_handlers efx_err_handlers = {
  3257. .error_detected = efx_io_error_detected,
  3258. .slot_reset = efx_io_slot_reset,
  3259. .resume = efx_io_resume,
  3260. };
  3261. static struct pci_driver efx_pci_driver = {
  3262. .name = KBUILD_MODNAME,
  3263. .id_table = efx_pci_table,
  3264. .probe = efx_pci_probe,
  3265. .remove = efx_pci_remove,
  3266. .driver.pm = &efx_pm_ops,
  3267. .err_handler = &efx_err_handlers,
  3268. #ifdef CONFIG_SFC_SRIOV
  3269. .sriov_configure = efx_pci_sriov_configure,
  3270. #endif
  3271. };
  3272. /**************************************************************************
  3273. *
  3274. * Kernel module interface
  3275. *
  3276. *************************************************************************/
  3277. module_param(interrupt_mode, uint, 0444);
  3278. MODULE_PARM_DESC(interrupt_mode,
  3279. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  3280. static int __init efx_init_module(void)
  3281. {
  3282. int rc;
  3283. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  3284. rc = register_netdevice_notifier(&efx_netdev_notifier);
  3285. if (rc)
  3286. goto err_notifier;
  3287. #ifdef CONFIG_SFC_SRIOV
  3288. rc = efx_init_sriov();
  3289. if (rc)
  3290. goto err_sriov;
  3291. #endif
  3292. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  3293. if (!reset_workqueue) {
  3294. rc = -ENOMEM;
  3295. goto err_reset;
  3296. }
  3297. rc = pci_register_driver(&efx_pci_driver);
  3298. if (rc < 0)
  3299. goto err_pci;
  3300. return 0;
  3301. err_pci:
  3302. destroy_workqueue(reset_workqueue);
  3303. err_reset:
  3304. #ifdef CONFIG_SFC_SRIOV
  3305. efx_fini_sriov();
  3306. err_sriov:
  3307. #endif
  3308. unregister_netdevice_notifier(&efx_netdev_notifier);
  3309. err_notifier:
  3310. return rc;
  3311. }
  3312. static void __exit efx_exit_module(void)
  3313. {
  3314. printk(KERN_INFO "Solarflare NET driver unloading\n");
  3315. pci_unregister_driver(&efx_pci_driver);
  3316. destroy_workqueue(reset_workqueue);
  3317. #ifdef CONFIG_SFC_SRIOV
  3318. efx_fini_sriov();
  3319. #endif
  3320. unregister_netdevice_notifier(&efx_netdev_notifier);
  3321. }
  3322. module_init(efx_init_module);
  3323. module_exit(efx_exit_module);
  3324. MODULE_AUTHOR("Solarflare Communications and "
  3325. "Michael Brown <mbrown@fensystems.co.uk>");
  3326. MODULE_DESCRIPTION("Solarflare network driver");
  3327. MODULE_LICENSE("GPL");
  3328. MODULE_DEVICE_TABLE(pci, efx_pci_table);
  3329. MODULE_VERSION(EFX_DRIVER_VERSION);