efx.c 102 KB

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