ena_netdev.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #ifdef CONFIG_RFS_ACCEL
  7. #include <linux/cpu_rmap.h>
  8. #endif /* CONFIG_RFS_ACCEL */
  9. #include <linux/ethtool.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/numa.h>
  13. #include <linux/pci.h>
  14. #include <linux/utsname.h>
  15. #include <linux/version.h>
  16. #include <linux/vmalloc.h>
  17. #include <net/ip.h>
  18. #include "ena_netdev.h"
  19. #include "ena_pci_id_tbl.h"
  20. #include "ena_xdp.h"
  21. MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
  22. MODULE_DESCRIPTION(DEVICE_NAME);
  23. MODULE_LICENSE("GPL");
  24. /* Time in jiffies before concluding the transmitter is hung. */
  25. #define TX_TIMEOUT (5 * HZ)
  26. #define ENA_MAX_RINGS min_t(unsigned int, ENA_MAX_NUM_IO_QUEUES, num_possible_cpus())
  27. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | \
  28. NETIF_MSG_IFDOWN | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
  29. static struct ena_aenq_handlers aenq_handlers;
  30. static struct workqueue_struct *ena_wq;
  31. MODULE_DEVICE_TABLE(pci, ena_pci_tbl);
  32. static int ena_rss_init_default(struct ena_adapter *adapter);
  33. static void check_for_admin_com_state(struct ena_adapter *adapter);
  34. static int ena_destroy_device(struct ena_adapter *adapter, bool graceful);
  35. static int ena_restore_device(struct ena_adapter *adapter);
  36. static void ena_tx_timeout(struct net_device *dev, unsigned int txqueue)
  37. {
  38. enum ena_regs_reset_reason_types reset_reason = ENA_REGS_RESET_OS_NETDEV_WD;
  39. struct ena_adapter *adapter = netdev_priv(dev);
  40. unsigned int time_since_last_napi, threshold;
  41. struct ena_ring *tx_ring;
  42. int napi_scheduled;
  43. if (txqueue >= adapter->num_io_queues) {
  44. netdev_err(dev, "TX timeout on invalid queue %u\n", txqueue);
  45. goto schedule_reset;
  46. }
  47. threshold = jiffies_to_usecs(dev->watchdog_timeo);
  48. tx_ring = &adapter->tx_ring[txqueue];
  49. time_since_last_napi = jiffies_to_usecs(jiffies - tx_ring->tx_stats.last_napi_jiffies);
  50. napi_scheduled = !!(tx_ring->napi->state & NAPIF_STATE_SCHED);
  51. netdev_err(dev,
  52. "TX q %d is paused for too long (threshold %u). Time since last napi %u usec. napi scheduled: %d\n",
  53. txqueue,
  54. threshold,
  55. time_since_last_napi,
  56. napi_scheduled);
  57. if (threshold < time_since_last_napi && napi_scheduled) {
  58. netdev_err(dev,
  59. "napi handler hasn't been called for a long time but is scheduled\n");
  60. reset_reason = ENA_REGS_RESET_SUSPECTED_POLL_STARVATION;
  61. }
  62. schedule_reset:
  63. /* Change the state of the device to trigger reset
  64. * Check that we are not in the middle or a trigger already
  65. */
  66. if (test_and_set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
  67. return;
  68. ena_reset_device(adapter, reset_reason);
  69. ena_increase_stat(&adapter->dev_stats.tx_timeout, 1, &adapter->syncp);
  70. }
  71. static void update_rx_ring_mtu(struct ena_adapter *adapter, int mtu)
  72. {
  73. int i;
  74. for (i = 0; i < adapter->num_io_queues; i++)
  75. adapter->rx_ring[i].mtu = mtu;
  76. }
  77. static int ena_change_mtu(struct net_device *dev, int new_mtu)
  78. {
  79. struct ena_adapter *adapter = netdev_priv(dev);
  80. int ret;
  81. ret = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
  82. if (!ret) {
  83. netif_dbg(adapter, drv, dev, "Set MTU to %d\n", new_mtu);
  84. update_rx_ring_mtu(adapter, new_mtu);
  85. WRITE_ONCE(dev->mtu, new_mtu);
  86. } else {
  87. netif_err(adapter, drv, dev, "Failed to set MTU to %d\n",
  88. new_mtu);
  89. }
  90. return ret;
  91. }
  92. int ena_xmit_common(struct ena_adapter *adapter,
  93. struct ena_ring *ring,
  94. struct ena_tx_buffer *tx_info,
  95. struct ena_com_tx_ctx *ena_tx_ctx,
  96. u16 next_to_use,
  97. u32 bytes)
  98. {
  99. int rc, nb_hw_desc;
  100. if (unlikely(ena_com_is_doorbell_needed(ring->ena_com_io_sq,
  101. ena_tx_ctx))) {
  102. netif_dbg(adapter, tx_queued, adapter->netdev,
  103. "llq tx max burst size of queue %d achieved, writing doorbell to send burst\n",
  104. ring->qid);
  105. ena_ring_tx_doorbell(ring);
  106. }
  107. /* prepare the packet's descriptors to dma engine */
  108. rc = ena_com_prepare_tx(ring->ena_com_io_sq, ena_tx_ctx,
  109. &nb_hw_desc);
  110. /* In case there isn't enough space in the queue for the packet,
  111. * we simply drop it. All other failure reasons of
  112. * ena_com_prepare_tx() are fatal and therefore require a device reset.
  113. */
  114. if (unlikely(rc)) {
  115. netif_err(adapter, tx_queued, adapter->netdev,
  116. "Failed to prepare tx bufs\n");
  117. ena_increase_stat(&ring->tx_stats.prepare_ctx_err, 1, &ring->syncp);
  118. if (rc != -ENOMEM)
  119. ena_reset_device(adapter, ENA_REGS_RESET_DRIVER_INVALID_STATE);
  120. return rc;
  121. }
  122. u64_stats_update_begin(&ring->syncp);
  123. ring->tx_stats.cnt++;
  124. ring->tx_stats.bytes += bytes;
  125. u64_stats_update_end(&ring->syncp);
  126. tx_info->tx_descs = nb_hw_desc;
  127. tx_info->total_tx_size = bytes;
  128. tx_info->last_jiffies = jiffies;
  129. tx_info->print_once = 0;
  130. ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
  131. ring->ring_size);
  132. return 0;
  133. }
  134. static int ena_init_rx_cpu_rmap(struct ena_adapter *adapter)
  135. {
  136. #ifdef CONFIG_RFS_ACCEL
  137. u32 i;
  138. int rc;
  139. adapter->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(adapter->num_io_queues);
  140. if (!adapter->netdev->rx_cpu_rmap)
  141. return -ENOMEM;
  142. for (i = 0; i < adapter->num_io_queues; i++) {
  143. int irq_idx = ENA_IO_IRQ_IDX(i);
  144. rc = irq_cpu_rmap_add(adapter->netdev->rx_cpu_rmap,
  145. pci_irq_vector(adapter->pdev, irq_idx));
  146. if (rc) {
  147. free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
  148. adapter->netdev->rx_cpu_rmap = NULL;
  149. return rc;
  150. }
  151. }
  152. #endif /* CONFIG_RFS_ACCEL */
  153. return 0;
  154. }
  155. static void ena_init_io_rings_common(struct ena_adapter *adapter,
  156. struct ena_ring *ring, u16 qid)
  157. {
  158. ring->qid = qid;
  159. ring->pdev = adapter->pdev;
  160. ring->dev = &adapter->pdev->dev;
  161. ring->netdev = adapter->netdev;
  162. ring->napi = &adapter->ena_napi[qid].napi;
  163. ring->adapter = adapter;
  164. ring->ena_dev = adapter->ena_dev;
  165. ring->per_napi_packets = 0;
  166. ring->cpu = 0;
  167. ring->numa_node = 0;
  168. ring->no_interrupt_event_cnt = 0;
  169. u64_stats_init(&ring->syncp);
  170. }
  171. void ena_init_io_rings(struct ena_adapter *adapter,
  172. int first_index, int count)
  173. {
  174. struct ena_com_dev *ena_dev;
  175. struct ena_ring *txr, *rxr;
  176. int i;
  177. ena_dev = adapter->ena_dev;
  178. for (i = first_index; i < first_index + count; i++) {
  179. txr = &adapter->tx_ring[i];
  180. rxr = &adapter->rx_ring[i];
  181. /* TX common ring state */
  182. ena_init_io_rings_common(adapter, txr, i);
  183. /* TX specific ring state */
  184. txr->ring_size = adapter->requested_tx_ring_size;
  185. txr->tx_max_header_size = ena_dev->tx_max_header_size;
  186. txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
  187. txr->sgl_size = adapter->max_tx_sgl_size;
  188. txr->smoothed_interval =
  189. ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
  190. txr->disable_meta_caching = adapter->disable_meta_caching;
  191. spin_lock_init(&txr->xdp_tx_lock);
  192. /* Don't init RX queues for xdp queues */
  193. if (!ENA_IS_XDP_INDEX(adapter, i)) {
  194. /* RX common ring state */
  195. ena_init_io_rings_common(adapter, rxr, i);
  196. /* RX specific ring state */
  197. rxr->ring_size = adapter->requested_rx_ring_size;
  198. rxr->rx_copybreak = adapter->rx_copybreak;
  199. rxr->sgl_size = adapter->max_rx_sgl_size;
  200. rxr->smoothed_interval =
  201. ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
  202. rxr->empty_rx_queue = 0;
  203. rxr->rx_headroom = NET_SKB_PAD;
  204. adapter->ena_napi[i].dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
  205. rxr->xdp_ring = &adapter->tx_ring[i + adapter->num_io_queues];
  206. }
  207. }
  208. }
  209. /* ena_setup_tx_resources - allocate I/O Tx resources (Descriptors)
  210. * @adapter: network interface device structure
  211. * @qid: queue index
  212. *
  213. * Return 0 on success, negative on failure
  214. */
  215. static int ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
  216. {
  217. struct ena_ring *tx_ring = &adapter->tx_ring[qid];
  218. struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
  219. int size, i, node;
  220. if (tx_ring->tx_buffer_info) {
  221. netif_err(adapter, ifup,
  222. adapter->netdev, "tx_buffer_info info is not NULL");
  223. return -EEXIST;
  224. }
  225. size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
  226. node = cpu_to_node(ena_irq->cpu);
  227. tx_ring->tx_buffer_info = vzalloc_node(size, node);
  228. if (!tx_ring->tx_buffer_info) {
  229. tx_ring->tx_buffer_info = vzalloc(size);
  230. if (!tx_ring->tx_buffer_info)
  231. goto err_tx_buffer_info;
  232. }
  233. size = sizeof(u16) * tx_ring->ring_size;
  234. tx_ring->free_ids = vzalloc_node(size, node);
  235. if (!tx_ring->free_ids) {
  236. tx_ring->free_ids = vzalloc(size);
  237. if (!tx_ring->free_ids)
  238. goto err_tx_free_ids;
  239. }
  240. size = tx_ring->tx_max_header_size;
  241. tx_ring->push_buf_intermediate_buf = vzalloc_node(size, node);
  242. if (!tx_ring->push_buf_intermediate_buf) {
  243. tx_ring->push_buf_intermediate_buf = vzalloc(size);
  244. if (!tx_ring->push_buf_intermediate_buf)
  245. goto err_push_buf_intermediate_buf;
  246. }
  247. /* Req id ring for TX out of order completions */
  248. for (i = 0; i < tx_ring->ring_size; i++)
  249. tx_ring->free_ids[i] = i;
  250. /* Reset tx statistics */
  251. memset(&tx_ring->tx_stats, 0x0, sizeof(tx_ring->tx_stats));
  252. tx_ring->next_to_use = 0;
  253. tx_ring->next_to_clean = 0;
  254. tx_ring->cpu = ena_irq->cpu;
  255. tx_ring->numa_node = node;
  256. return 0;
  257. err_push_buf_intermediate_buf:
  258. vfree(tx_ring->free_ids);
  259. tx_ring->free_ids = NULL;
  260. err_tx_free_ids:
  261. vfree(tx_ring->tx_buffer_info);
  262. tx_ring->tx_buffer_info = NULL;
  263. err_tx_buffer_info:
  264. return -ENOMEM;
  265. }
  266. /* ena_free_tx_resources - Free I/O Tx Resources per Queue
  267. * @adapter: network interface device structure
  268. * @qid: queue index
  269. *
  270. * Free all transmit software resources
  271. */
  272. static void ena_free_tx_resources(struct ena_adapter *adapter, int qid)
  273. {
  274. struct ena_ring *tx_ring = &adapter->tx_ring[qid];
  275. vfree(tx_ring->tx_buffer_info);
  276. tx_ring->tx_buffer_info = NULL;
  277. vfree(tx_ring->free_ids);
  278. tx_ring->free_ids = NULL;
  279. vfree(tx_ring->push_buf_intermediate_buf);
  280. tx_ring->push_buf_intermediate_buf = NULL;
  281. }
  282. int ena_setup_tx_resources_in_range(struct ena_adapter *adapter,
  283. int first_index, int count)
  284. {
  285. int i, rc = 0;
  286. for (i = first_index; i < first_index + count; i++) {
  287. rc = ena_setup_tx_resources(adapter, i);
  288. if (rc)
  289. goto err_setup_tx;
  290. }
  291. return 0;
  292. err_setup_tx:
  293. netif_err(adapter, ifup, adapter->netdev,
  294. "Tx queue %d: allocation failed\n", i);
  295. /* rewind the index freeing the rings as we go */
  296. while (first_index < i--)
  297. ena_free_tx_resources(adapter, i);
  298. return rc;
  299. }
  300. void ena_free_all_io_tx_resources_in_range(struct ena_adapter *adapter,
  301. int first_index, int count)
  302. {
  303. int i;
  304. for (i = first_index; i < first_index + count; i++)
  305. ena_free_tx_resources(adapter, i);
  306. }
  307. /* ena_free_all_io_tx_resources - Free I/O Tx Resources for All Queues
  308. * @adapter: board private structure
  309. *
  310. * Free all transmit software resources
  311. */
  312. void ena_free_all_io_tx_resources(struct ena_adapter *adapter)
  313. {
  314. ena_free_all_io_tx_resources_in_range(adapter,
  315. 0,
  316. adapter->xdp_num_queues +
  317. adapter->num_io_queues);
  318. }
  319. /* ena_setup_rx_resources - allocate I/O Rx resources (Descriptors)
  320. * @adapter: network interface device structure
  321. * @qid: queue index
  322. *
  323. * Returns 0 on success, negative on failure
  324. */
  325. static int ena_setup_rx_resources(struct ena_adapter *adapter,
  326. u32 qid)
  327. {
  328. struct ena_ring *rx_ring = &adapter->rx_ring[qid];
  329. struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
  330. int size, node, i;
  331. if (rx_ring->rx_buffer_info) {
  332. netif_err(adapter, ifup, adapter->netdev,
  333. "rx_buffer_info is not NULL");
  334. return -EEXIST;
  335. }
  336. /* alloc extra element so in rx path
  337. * we can always prefetch rx_info + 1
  338. */
  339. size = sizeof(struct ena_rx_buffer) * (rx_ring->ring_size + 1);
  340. node = cpu_to_node(ena_irq->cpu);
  341. rx_ring->rx_buffer_info = vzalloc_node(size, node);
  342. if (!rx_ring->rx_buffer_info) {
  343. rx_ring->rx_buffer_info = vzalloc(size);
  344. if (!rx_ring->rx_buffer_info)
  345. return -ENOMEM;
  346. }
  347. size = sizeof(u16) * rx_ring->ring_size;
  348. rx_ring->free_ids = vzalloc_node(size, node);
  349. if (!rx_ring->free_ids) {
  350. rx_ring->free_ids = vzalloc(size);
  351. if (!rx_ring->free_ids) {
  352. vfree(rx_ring->rx_buffer_info);
  353. rx_ring->rx_buffer_info = NULL;
  354. return -ENOMEM;
  355. }
  356. }
  357. /* Req id ring for receiving RX pkts out of order */
  358. for (i = 0; i < rx_ring->ring_size; i++)
  359. rx_ring->free_ids[i] = i;
  360. /* Reset rx statistics */
  361. memset(&rx_ring->rx_stats, 0x0, sizeof(rx_ring->rx_stats));
  362. rx_ring->next_to_clean = 0;
  363. rx_ring->next_to_use = 0;
  364. rx_ring->cpu = ena_irq->cpu;
  365. rx_ring->numa_node = node;
  366. return 0;
  367. }
  368. /* ena_free_rx_resources - Free I/O Rx Resources
  369. * @adapter: network interface device structure
  370. * @qid: queue index
  371. *
  372. * Free all receive software resources
  373. */
  374. static void ena_free_rx_resources(struct ena_adapter *adapter,
  375. u32 qid)
  376. {
  377. struct ena_ring *rx_ring = &adapter->rx_ring[qid];
  378. vfree(rx_ring->rx_buffer_info);
  379. rx_ring->rx_buffer_info = NULL;
  380. vfree(rx_ring->free_ids);
  381. rx_ring->free_ids = NULL;
  382. }
  383. /* ena_setup_all_rx_resources - allocate I/O Rx queues resources for all queues
  384. * @adapter: board private structure
  385. *
  386. * Return 0 on success, negative on failure
  387. */
  388. static int ena_setup_all_rx_resources(struct ena_adapter *adapter)
  389. {
  390. int i, rc = 0;
  391. for (i = 0; i < adapter->num_io_queues; i++) {
  392. rc = ena_setup_rx_resources(adapter, i);
  393. if (rc)
  394. goto err_setup_rx;
  395. }
  396. return 0;
  397. err_setup_rx:
  398. netif_err(adapter, ifup, adapter->netdev,
  399. "Rx queue %d: allocation failed\n", i);
  400. /* rewind the index freeing the rings as we go */
  401. while (i--)
  402. ena_free_rx_resources(adapter, i);
  403. return rc;
  404. }
  405. /* ena_free_all_io_rx_resources - Free I/O Rx Resources for All Queues
  406. * @adapter: board private structure
  407. *
  408. * Free all receive software resources
  409. */
  410. static void ena_free_all_io_rx_resources(struct ena_adapter *adapter)
  411. {
  412. int i;
  413. for (i = 0; i < adapter->num_io_queues; i++)
  414. ena_free_rx_resources(adapter, i);
  415. }
  416. static struct page *ena_alloc_map_page(struct ena_ring *rx_ring,
  417. dma_addr_t *dma)
  418. {
  419. struct page *page;
  420. /* This would allocate the page on the same NUMA node the executing code
  421. * is running on.
  422. */
  423. page = dev_alloc_page();
  424. if (!page) {
  425. ena_increase_stat(&rx_ring->rx_stats.page_alloc_fail, 1, &rx_ring->syncp);
  426. return ERR_PTR(-ENOSPC);
  427. }
  428. /* To enable NIC-side port-mirroring, AKA SPAN port,
  429. * we make the buffer readable from the nic as well
  430. */
  431. *dma = dma_map_page(rx_ring->dev, page, 0, ENA_PAGE_SIZE,
  432. DMA_BIDIRECTIONAL);
  433. if (unlikely(dma_mapping_error(rx_ring->dev, *dma))) {
  434. ena_increase_stat(&rx_ring->rx_stats.dma_mapping_err, 1,
  435. &rx_ring->syncp);
  436. __free_page(page);
  437. return ERR_PTR(-EIO);
  438. }
  439. return page;
  440. }
  441. static int ena_alloc_rx_buffer(struct ena_ring *rx_ring,
  442. struct ena_rx_buffer *rx_info)
  443. {
  444. int headroom = rx_ring->rx_headroom;
  445. struct ena_com_buf *ena_buf;
  446. struct page *page;
  447. dma_addr_t dma;
  448. int tailroom;
  449. /* restore page offset value in case it has been changed by device */
  450. rx_info->buf_offset = headroom;
  451. /* if previous allocated page is not used */
  452. if (unlikely(rx_info->page))
  453. return 0;
  454. /* We handle DMA here */
  455. page = ena_alloc_map_page(rx_ring, &dma);
  456. if (IS_ERR(page))
  457. return PTR_ERR(page);
  458. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  459. "Allocate page %p, rx_info %p\n", page, rx_info);
  460. tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  461. rx_info->page = page;
  462. rx_info->dma_addr = dma;
  463. rx_info->page_offset = 0;
  464. ena_buf = &rx_info->ena_buf;
  465. ena_buf->paddr = dma + headroom;
  466. ena_buf->len = ENA_PAGE_SIZE - headroom - tailroom;
  467. return 0;
  468. }
  469. static void ena_unmap_rx_buff_attrs(struct ena_ring *rx_ring,
  470. struct ena_rx_buffer *rx_info,
  471. unsigned long attrs)
  472. {
  473. dma_unmap_page_attrs(rx_ring->dev, rx_info->dma_addr, ENA_PAGE_SIZE, DMA_BIDIRECTIONAL,
  474. attrs);
  475. }
  476. static void ena_free_rx_page(struct ena_ring *rx_ring,
  477. struct ena_rx_buffer *rx_info)
  478. {
  479. struct page *page = rx_info->page;
  480. if (unlikely(!page)) {
  481. netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
  482. "Trying to free unallocated buffer\n");
  483. return;
  484. }
  485. ena_unmap_rx_buff_attrs(rx_ring, rx_info, 0);
  486. __free_page(page);
  487. rx_info->page = NULL;
  488. }
  489. static int ena_refill_rx_bufs(struct ena_ring *rx_ring, u32 num)
  490. {
  491. u16 next_to_use, req_id;
  492. u32 i;
  493. int rc;
  494. next_to_use = rx_ring->next_to_use;
  495. for (i = 0; i < num; i++) {
  496. struct ena_rx_buffer *rx_info;
  497. req_id = rx_ring->free_ids[next_to_use];
  498. rx_info = &rx_ring->rx_buffer_info[req_id];
  499. rc = ena_alloc_rx_buffer(rx_ring, rx_info);
  500. if (unlikely(rc < 0)) {
  501. netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
  502. "Failed to allocate buffer for rx queue %d\n",
  503. rx_ring->qid);
  504. break;
  505. }
  506. rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
  507. &rx_info->ena_buf,
  508. req_id);
  509. if (unlikely(rc)) {
  510. netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
  511. "Failed to add buffer for rx queue %d\n",
  512. rx_ring->qid);
  513. break;
  514. }
  515. next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
  516. rx_ring->ring_size);
  517. }
  518. if (unlikely(i < num)) {
  519. ena_increase_stat(&rx_ring->rx_stats.refil_partial, 1,
  520. &rx_ring->syncp);
  521. netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
  522. "Refilled rx qid %d with only %d buffers (from %d)\n",
  523. rx_ring->qid, i, num);
  524. }
  525. /* ena_com_write_sq_doorbell issues a wmb() */
  526. if (likely(i))
  527. ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
  528. rx_ring->next_to_use = next_to_use;
  529. return i;
  530. }
  531. static void ena_free_rx_bufs(struct ena_adapter *adapter,
  532. u32 qid)
  533. {
  534. struct ena_ring *rx_ring = &adapter->rx_ring[qid];
  535. u32 i;
  536. for (i = 0; i < rx_ring->ring_size; i++) {
  537. struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
  538. if (rx_info->page)
  539. ena_free_rx_page(rx_ring, rx_info);
  540. }
  541. }
  542. /* ena_refill_all_rx_bufs - allocate all queues Rx buffers
  543. * @adapter: board private structure
  544. */
  545. static void ena_refill_all_rx_bufs(struct ena_adapter *adapter)
  546. {
  547. struct ena_ring *rx_ring;
  548. int i, rc, bufs_num;
  549. for (i = 0; i < adapter->num_io_queues; i++) {
  550. rx_ring = &adapter->rx_ring[i];
  551. bufs_num = rx_ring->ring_size - 1;
  552. rc = ena_refill_rx_bufs(rx_ring, bufs_num);
  553. if (unlikely(rc != bufs_num))
  554. netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
  555. "Refilling Queue %d failed. allocated %d buffers from: %d\n",
  556. i, rc, bufs_num);
  557. }
  558. }
  559. static void ena_free_all_rx_bufs(struct ena_adapter *adapter)
  560. {
  561. int i;
  562. for (i = 0; i < adapter->num_io_queues; i++)
  563. ena_free_rx_bufs(adapter, i);
  564. }
  565. void ena_unmap_tx_buff(struct ena_ring *tx_ring,
  566. struct ena_tx_buffer *tx_info)
  567. {
  568. struct ena_com_buf *ena_buf;
  569. u32 cnt;
  570. int i;
  571. ena_buf = tx_info->bufs;
  572. cnt = tx_info->num_of_bufs;
  573. if (unlikely(!cnt))
  574. return;
  575. if (tx_info->map_linear_data) {
  576. dma_unmap_single(tx_ring->dev,
  577. dma_unmap_addr(ena_buf, paddr),
  578. dma_unmap_len(ena_buf, len),
  579. DMA_TO_DEVICE);
  580. ena_buf++;
  581. cnt--;
  582. }
  583. /* unmap remaining mapped pages */
  584. for (i = 0; i < cnt; i++) {
  585. dma_unmap_page(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
  586. dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
  587. ena_buf++;
  588. }
  589. }
  590. /* ena_free_tx_bufs - Free Tx Buffers per Queue
  591. * @tx_ring: TX ring for which buffers be freed
  592. */
  593. static void ena_free_tx_bufs(struct ena_ring *tx_ring)
  594. {
  595. bool print_once = true;
  596. bool is_xdp_ring;
  597. u32 i;
  598. is_xdp_ring = ENA_IS_XDP_INDEX(tx_ring->adapter, tx_ring->qid);
  599. for (i = 0; i < tx_ring->ring_size; i++) {
  600. struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
  601. if (!tx_info->skb)
  602. continue;
  603. if (print_once) {
  604. netif_notice(tx_ring->adapter, ifdown, tx_ring->netdev,
  605. "Free uncompleted tx skb qid %d idx 0x%x\n",
  606. tx_ring->qid, i);
  607. print_once = false;
  608. } else {
  609. netif_dbg(tx_ring->adapter, ifdown, tx_ring->netdev,
  610. "Free uncompleted tx skb qid %d idx 0x%x\n",
  611. tx_ring->qid, i);
  612. }
  613. ena_unmap_tx_buff(tx_ring, tx_info);
  614. if (is_xdp_ring)
  615. xdp_return_frame(tx_info->xdpf);
  616. else
  617. dev_kfree_skb_any(tx_info->skb);
  618. }
  619. if (!is_xdp_ring)
  620. netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
  621. tx_ring->qid));
  622. }
  623. static void ena_free_all_tx_bufs(struct ena_adapter *adapter)
  624. {
  625. struct ena_ring *tx_ring;
  626. int i;
  627. for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
  628. tx_ring = &adapter->tx_ring[i];
  629. ena_free_tx_bufs(tx_ring);
  630. }
  631. }
  632. static void ena_destroy_all_tx_queues(struct ena_adapter *adapter)
  633. {
  634. u16 ena_qid;
  635. int i;
  636. for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
  637. ena_qid = ENA_IO_TXQ_IDX(i);
  638. ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
  639. }
  640. }
  641. static void ena_destroy_all_rx_queues(struct ena_adapter *adapter)
  642. {
  643. u16 ena_qid;
  644. int i;
  645. for (i = 0; i < adapter->num_io_queues; i++) {
  646. ena_qid = ENA_IO_RXQ_IDX(i);
  647. cancel_work_sync(&adapter->ena_napi[i].dim.work);
  648. ena_xdp_unregister_rxq_info(&adapter->rx_ring[i]);
  649. ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
  650. }
  651. }
  652. static void ena_destroy_all_io_queues(struct ena_adapter *adapter)
  653. {
  654. ena_destroy_all_tx_queues(adapter);
  655. ena_destroy_all_rx_queues(adapter);
  656. }
  657. int handle_invalid_req_id(struct ena_ring *ring, u16 req_id,
  658. struct ena_tx_buffer *tx_info, bool is_xdp)
  659. {
  660. if (tx_info)
  661. netif_err(ring->adapter,
  662. tx_done,
  663. ring->netdev,
  664. "tx_info doesn't have valid %s. qid %u req_id %u",
  665. is_xdp ? "xdp frame" : "skb", ring->qid, req_id);
  666. else
  667. netif_err(ring->adapter,
  668. tx_done,
  669. ring->netdev,
  670. "Invalid req_id %u in qid %u\n",
  671. req_id, ring->qid);
  672. ena_increase_stat(&ring->tx_stats.bad_req_id, 1, &ring->syncp);
  673. ena_reset_device(ring->adapter, ENA_REGS_RESET_INV_TX_REQ_ID);
  674. return -EFAULT;
  675. }
  676. static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
  677. {
  678. struct ena_tx_buffer *tx_info;
  679. tx_info = &tx_ring->tx_buffer_info[req_id];
  680. if (likely(tx_info->skb))
  681. return 0;
  682. return handle_invalid_req_id(tx_ring, req_id, tx_info, false);
  683. }
  684. static int ena_clean_tx_irq(struct ena_ring *tx_ring, u32 budget)
  685. {
  686. struct netdev_queue *txq;
  687. bool above_thresh;
  688. u32 tx_bytes = 0;
  689. u32 total_done = 0;
  690. u16 next_to_clean;
  691. u16 req_id;
  692. int tx_pkts = 0;
  693. int rc;
  694. next_to_clean = tx_ring->next_to_clean;
  695. txq = netdev_get_tx_queue(tx_ring->netdev, tx_ring->qid);
  696. while (tx_pkts < budget) {
  697. struct ena_tx_buffer *tx_info;
  698. struct sk_buff *skb;
  699. rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq,
  700. &req_id);
  701. if (rc) {
  702. if (unlikely(rc == -EINVAL))
  703. handle_invalid_req_id(tx_ring, req_id, NULL, false);
  704. break;
  705. }
  706. /* validate that the request id points to a valid skb */
  707. rc = validate_tx_req_id(tx_ring, req_id);
  708. if (rc)
  709. break;
  710. tx_info = &tx_ring->tx_buffer_info[req_id];
  711. skb = tx_info->skb;
  712. /* prefetch skb_end_pointer() to speedup skb_shinfo(skb) */
  713. prefetch(&skb->end);
  714. tx_info->skb = NULL;
  715. tx_info->last_jiffies = 0;
  716. ena_unmap_tx_buff(tx_ring, tx_info);
  717. netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
  718. "tx_poll: q %d skb %p completed\n", tx_ring->qid,
  719. skb);
  720. tx_bytes += tx_info->total_tx_size;
  721. dev_kfree_skb(skb);
  722. tx_pkts++;
  723. total_done += tx_info->tx_descs;
  724. tx_ring->free_ids[next_to_clean] = req_id;
  725. next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
  726. tx_ring->ring_size);
  727. }
  728. tx_ring->next_to_clean = next_to_clean;
  729. ena_com_comp_ack(tx_ring->ena_com_io_sq, total_done);
  730. netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
  731. netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
  732. "tx_poll: q %d done. total pkts: %d\n",
  733. tx_ring->qid, tx_pkts);
  734. /* need to make the rings circular update visible to
  735. * ena_start_xmit() before checking for netif_queue_stopped().
  736. */
  737. smp_mb();
  738. above_thresh = ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
  739. ENA_TX_WAKEUP_THRESH);
  740. if (unlikely(netif_tx_queue_stopped(txq) && above_thresh)) {
  741. __netif_tx_lock(txq, smp_processor_id());
  742. above_thresh =
  743. ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
  744. ENA_TX_WAKEUP_THRESH);
  745. if (netif_tx_queue_stopped(txq) && above_thresh &&
  746. test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags)) {
  747. netif_tx_wake_queue(txq);
  748. ena_increase_stat(&tx_ring->tx_stats.queue_wakeup, 1,
  749. &tx_ring->syncp);
  750. }
  751. __netif_tx_unlock(txq);
  752. }
  753. return tx_pkts;
  754. }
  755. static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag, u16 len)
  756. {
  757. struct sk_buff *skb;
  758. if (!first_frag)
  759. skb = napi_alloc_skb(rx_ring->napi, len);
  760. else
  761. skb = napi_build_skb(first_frag, len);
  762. if (unlikely(!skb)) {
  763. ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail, 1,
  764. &rx_ring->syncp);
  765. netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
  766. "Failed to allocate skb. first_frag %s\n",
  767. first_frag ? "provided" : "not provided");
  768. }
  769. return skb;
  770. }
  771. static bool ena_try_rx_buf_page_reuse(struct ena_rx_buffer *rx_info, u16 buf_len,
  772. u16 len, int pkt_offset)
  773. {
  774. struct ena_com_buf *ena_buf = &rx_info->ena_buf;
  775. /* More than ENA_MIN_RX_BUF_SIZE left in the reused buffer
  776. * for data + headroom + tailroom.
  777. */
  778. if (SKB_DATA_ALIGN(len + pkt_offset) + ENA_MIN_RX_BUF_SIZE <= ena_buf->len) {
  779. page_ref_inc(rx_info->page);
  780. rx_info->page_offset += buf_len;
  781. ena_buf->paddr += buf_len;
  782. ena_buf->len -= buf_len;
  783. return true;
  784. }
  785. return false;
  786. }
  787. static struct sk_buff *ena_rx_skb(struct ena_ring *rx_ring,
  788. struct ena_com_rx_buf_info *ena_bufs,
  789. u32 descs,
  790. u16 *next_to_clean)
  791. {
  792. int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  793. bool is_xdp_loaded = ena_xdp_present_ring(rx_ring);
  794. struct ena_rx_buffer *rx_info;
  795. struct ena_adapter *adapter;
  796. int page_offset, pkt_offset;
  797. dma_addr_t pre_reuse_paddr;
  798. u16 len, req_id, buf = 0;
  799. bool reuse_rx_buf_page;
  800. struct sk_buff *skb;
  801. void *buf_addr;
  802. int buf_offset;
  803. u16 buf_len;
  804. len = ena_bufs[buf].len;
  805. req_id = ena_bufs[buf].req_id;
  806. rx_info = &rx_ring->rx_buffer_info[req_id];
  807. if (unlikely(!rx_info->page)) {
  808. adapter = rx_ring->adapter;
  809. netif_err(adapter, rx_err, rx_ring->netdev,
  810. "Page is NULL. qid %u req_id %u\n", rx_ring->qid, req_id);
  811. ena_increase_stat(&rx_ring->rx_stats.bad_req_id, 1, &rx_ring->syncp);
  812. ena_reset_device(adapter, ENA_REGS_RESET_INV_RX_REQ_ID);
  813. return NULL;
  814. }
  815. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  816. "rx_info %p page %p\n",
  817. rx_info, rx_info->page);
  818. buf_offset = rx_info->buf_offset;
  819. pkt_offset = buf_offset - rx_ring->rx_headroom;
  820. page_offset = rx_info->page_offset;
  821. buf_addr = page_address(rx_info->page) + page_offset;
  822. if (len <= rx_ring->rx_copybreak) {
  823. skb = ena_alloc_skb(rx_ring, NULL, len);
  824. if (unlikely(!skb))
  825. return NULL;
  826. skb_copy_to_linear_data(skb, buf_addr + buf_offset, len);
  827. dma_sync_single_for_device(rx_ring->dev,
  828. dma_unmap_addr(&rx_info->ena_buf, paddr) + pkt_offset,
  829. len,
  830. DMA_FROM_DEVICE);
  831. skb_put(skb, len);
  832. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  833. "RX allocated small packet. len %d.\n", skb->len);
  834. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  835. rx_ring->free_ids[*next_to_clean] = req_id;
  836. *next_to_clean = ENA_RX_RING_IDX_ADD(*next_to_clean, descs,
  837. rx_ring->ring_size);
  838. return skb;
  839. }
  840. buf_len = SKB_DATA_ALIGN(len + buf_offset + tailroom);
  841. /* If XDP isn't loaded try to reuse part of the RX buffer */
  842. reuse_rx_buf_page = !is_xdp_loaded &&
  843. ena_try_rx_buf_page_reuse(rx_info, buf_len, len, pkt_offset);
  844. if (!reuse_rx_buf_page)
  845. ena_unmap_rx_buff_attrs(rx_ring, rx_info, DMA_ATTR_SKIP_CPU_SYNC);
  846. skb = ena_alloc_skb(rx_ring, buf_addr, buf_len);
  847. if (unlikely(!skb))
  848. return NULL;
  849. /* Populate skb's linear part */
  850. skb_reserve(skb, buf_offset);
  851. skb_put(skb, len);
  852. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  853. do {
  854. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  855. "RX skb updated. len %d. data_len %d\n",
  856. skb->len, skb->data_len);
  857. if (!reuse_rx_buf_page)
  858. rx_info->page = NULL;
  859. rx_ring->free_ids[*next_to_clean] = req_id;
  860. *next_to_clean =
  861. ENA_RX_RING_IDX_NEXT(*next_to_clean,
  862. rx_ring->ring_size);
  863. if (likely(--descs == 0))
  864. break;
  865. buf++;
  866. len = ena_bufs[buf].len;
  867. req_id = ena_bufs[buf].req_id;
  868. rx_info = &rx_ring->rx_buffer_info[req_id];
  869. /* rx_info->buf_offset includes rx_ring->rx_headroom */
  870. buf_offset = rx_info->buf_offset;
  871. pkt_offset = buf_offset - rx_ring->rx_headroom;
  872. buf_len = SKB_DATA_ALIGN(len + buf_offset + tailroom);
  873. page_offset = rx_info->page_offset;
  874. pre_reuse_paddr = dma_unmap_addr(&rx_info->ena_buf, paddr);
  875. reuse_rx_buf_page = !is_xdp_loaded &&
  876. ena_try_rx_buf_page_reuse(rx_info, buf_len, len, pkt_offset);
  877. dma_sync_single_for_cpu(rx_ring->dev,
  878. pre_reuse_paddr + pkt_offset,
  879. len,
  880. DMA_FROM_DEVICE);
  881. if (!reuse_rx_buf_page)
  882. ena_unmap_rx_buff_attrs(rx_ring, rx_info, DMA_ATTR_SKIP_CPU_SYNC);
  883. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_info->page,
  884. page_offset + buf_offset, len, buf_len);
  885. } while (1);
  886. return skb;
  887. }
  888. /* ena_rx_checksum - indicate in skb if hw indicated a good cksum
  889. * @adapter: structure containing adapter specific data
  890. * @ena_rx_ctx: received packet context/metadata
  891. * @skb: skb currently being received and modified
  892. */
  893. static void ena_rx_checksum(struct ena_ring *rx_ring,
  894. struct ena_com_rx_ctx *ena_rx_ctx,
  895. struct sk_buff *skb)
  896. {
  897. /* Rx csum disabled */
  898. if (unlikely(!(rx_ring->netdev->features & NETIF_F_RXCSUM))) {
  899. skb->ip_summed = CHECKSUM_NONE;
  900. return;
  901. }
  902. /* For fragmented packets the checksum isn't valid */
  903. if (ena_rx_ctx->frag) {
  904. skb->ip_summed = CHECKSUM_NONE;
  905. return;
  906. }
  907. /* if IP and error */
  908. if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
  909. (ena_rx_ctx->l3_csum_err))) {
  910. /* ipv4 checksum error */
  911. skb->ip_summed = CHECKSUM_NONE;
  912. ena_increase_stat(&rx_ring->rx_stats.csum_bad, 1,
  913. &rx_ring->syncp);
  914. netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
  915. "RX IPv4 header checksum error\n");
  916. return;
  917. }
  918. /* if TCP/UDP */
  919. if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
  920. (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP))) {
  921. if (unlikely(ena_rx_ctx->l4_csum_err)) {
  922. /* TCP/UDP checksum error */
  923. ena_increase_stat(&rx_ring->rx_stats.csum_bad, 1,
  924. &rx_ring->syncp);
  925. netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
  926. "RX L4 checksum error\n");
  927. skb->ip_summed = CHECKSUM_NONE;
  928. return;
  929. }
  930. if (likely(ena_rx_ctx->l4_csum_checked)) {
  931. skb->ip_summed = CHECKSUM_UNNECESSARY;
  932. ena_increase_stat(&rx_ring->rx_stats.csum_good, 1,
  933. &rx_ring->syncp);
  934. } else {
  935. ena_increase_stat(&rx_ring->rx_stats.csum_unchecked, 1,
  936. &rx_ring->syncp);
  937. skb->ip_summed = CHECKSUM_NONE;
  938. }
  939. } else {
  940. skb->ip_summed = CHECKSUM_NONE;
  941. return;
  942. }
  943. }
  944. static void ena_set_rx_hash(struct ena_ring *rx_ring,
  945. struct ena_com_rx_ctx *ena_rx_ctx,
  946. struct sk_buff *skb)
  947. {
  948. enum pkt_hash_types hash_type;
  949. if (likely(rx_ring->netdev->features & NETIF_F_RXHASH)) {
  950. if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
  951. (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)))
  952. hash_type = PKT_HASH_TYPE_L4;
  953. else
  954. hash_type = PKT_HASH_TYPE_NONE;
  955. /* Override hash type if the packet is fragmented */
  956. if (ena_rx_ctx->frag)
  957. hash_type = PKT_HASH_TYPE_NONE;
  958. skb_set_hash(skb, ena_rx_ctx->hash, hash_type);
  959. }
  960. }
  961. static int ena_xdp_handle_buff(struct ena_ring *rx_ring, struct xdp_buff *xdp, u16 num_descs)
  962. {
  963. struct ena_rx_buffer *rx_info;
  964. int ret;
  965. /* XDP multi-buffer packets not supported */
  966. if (unlikely(num_descs > 1)) {
  967. netdev_err_once(rx_ring->adapter->netdev,
  968. "xdp: dropped unsupported multi-buffer packets\n");
  969. ena_increase_stat(&rx_ring->rx_stats.xdp_drop, 1, &rx_ring->syncp);
  970. return ENA_XDP_DROP;
  971. }
  972. rx_info = &rx_ring->rx_buffer_info[rx_ring->ena_bufs[0].req_id];
  973. xdp_prepare_buff(xdp, page_address(rx_info->page),
  974. rx_info->buf_offset,
  975. rx_ring->ena_bufs[0].len, false);
  976. ret = ena_xdp_execute(rx_ring, xdp);
  977. /* The xdp program might expand the headers */
  978. if (ret == ENA_XDP_PASS) {
  979. rx_info->buf_offset = xdp->data - xdp->data_hard_start;
  980. rx_ring->ena_bufs[0].len = xdp->data_end - xdp->data;
  981. }
  982. return ret;
  983. }
  984. /* ena_clean_rx_irq - Cleanup RX irq
  985. * @rx_ring: RX ring to clean
  986. * @napi: napi handler
  987. * @budget: how many packets driver is allowed to clean
  988. *
  989. * Returns the number of cleaned buffers.
  990. */
  991. static int ena_clean_rx_irq(struct ena_ring *rx_ring, struct napi_struct *napi,
  992. u32 budget)
  993. {
  994. u16 next_to_clean = rx_ring->next_to_clean;
  995. struct ena_com_rx_ctx ena_rx_ctx;
  996. struct ena_rx_buffer *rx_info;
  997. struct ena_adapter *adapter;
  998. u32 res_budget, work_done;
  999. int rx_copybreak_pkt = 0;
  1000. int refill_threshold;
  1001. struct sk_buff *skb;
  1002. int refill_required;
  1003. struct xdp_buff xdp;
  1004. int xdp_flags = 0;
  1005. int total_len = 0;
  1006. int xdp_verdict;
  1007. u8 pkt_offset;
  1008. int rc = 0;
  1009. int i;
  1010. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  1011. "%s qid %d\n", __func__, rx_ring->qid);
  1012. res_budget = budget;
  1013. xdp_init_buff(&xdp, ENA_PAGE_SIZE, &rx_ring->xdp_rxq);
  1014. do {
  1015. xdp_verdict = ENA_XDP_PASS;
  1016. skb = NULL;
  1017. ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
  1018. ena_rx_ctx.max_bufs = rx_ring->sgl_size;
  1019. ena_rx_ctx.descs = 0;
  1020. ena_rx_ctx.pkt_offset = 0;
  1021. rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
  1022. rx_ring->ena_com_io_sq,
  1023. &ena_rx_ctx);
  1024. if (unlikely(rc))
  1025. goto error;
  1026. if (unlikely(ena_rx_ctx.descs == 0))
  1027. break;
  1028. /* First descriptor might have an offset set by the device */
  1029. rx_info = &rx_ring->rx_buffer_info[rx_ring->ena_bufs[0].req_id];
  1030. pkt_offset = ena_rx_ctx.pkt_offset;
  1031. rx_info->buf_offset += pkt_offset;
  1032. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  1033. "rx_poll: q %d got packet from ena. descs #: %d l3 proto %d l4 proto %d hash: %x\n",
  1034. rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
  1035. ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
  1036. dma_sync_single_for_cpu(rx_ring->dev,
  1037. dma_unmap_addr(&rx_info->ena_buf, paddr) + pkt_offset,
  1038. rx_ring->ena_bufs[0].len,
  1039. DMA_FROM_DEVICE);
  1040. if (ena_xdp_present_ring(rx_ring))
  1041. xdp_verdict = ena_xdp_handle_buff(rx_ring, &xdp, ena_rx_ctx.descs);
  1042. /* allocate skb and fill it */
  1043. if (xdp_verdict == ENA_XDP_PASS)
  1044. skb = ena_rx_skb(rx_ring,
  1045. rx_ring->ena_bufs,
  1046. ena_rx_ctx.descs,
  1047. &next_to_clean);
  1048. if (unlikely(!skb)) {
  1049. for (i = 0; i < ena_rx_ctx.descs; i++) {
  1050. int req_id = rx_ring->ena_bufs[i].req_id;
  1051. rx_ring->free_ids[next_to_clean] = req_id;
  1052. next_to_clean =
  1053. ENA_RX_RING_IDX_NEXT(next_to_clean,
  1054. rx_ring->ring_size);
  1055. /* Packets was passed for transmission, unmap it
  1056. * from RX side.
  1057. */
  1058. if (xdp_verdict & ENA_XDP_FORWARDED) {
  1059. ena_unmap_rx_buff_attrs(rx_ring,
  1060. &rx_ring->rx_buffer_info[req_id],
  1061. DMA_ATTR_SKIP_CPU_SYNC);
  1062. rx_ring->rx_buffer_info[req_id].page = NULL;
  1063. }
  1064. }
  1065. if (xdp_verdict != ENA_XDP_PASS) {
  1066. xdp_flags |= xdp_verdict;
  1067. total_len += ena_rx_ctx.ena_bufs[0].len;
  1068. res_budget--;
  1069. continue;
  1070. }
  1071. break;
  1072. }
  1073. ena_rx_checksum(rx_ring, &ena_rx_ctx, skb);
  1074. ena_set_rx_hash(rx_ring, &ena_rx_ctx, skb);
  1075. skb_record_rx_queue(skb, rx_ring->qid);
  1076. if (rx_ring->ena_bufs[0].len <= rx_ring->rx_copybreak)
  1077. rx_copybreak_pkt++;
  1078. total_len += skb->len;
  1079. napi_gro_receive(napi, skb);
  1080. res_budget--;
  1081. } while (likely(res_budget));
  1082. work_done = budget - res_budget;
  1083. rx_ring->per_napi_packets += work_done;
  1084. u64_stats_update_begin(&rx_ring->syncp);
  1085. rx_ring->rx_stats.bytes += total_len;
  1086. rx_ring->rx_stats.cnt += work_done;
  1087. rx_ring->rx_stats.rx_copybreak_pkt += rx_copybreak_pkt;
  1088. u64_stats_update_end(&rx_ring->syncp);
  1089. rx_ring->next_to_clean = next_to_clean;
  1090. refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
  1091. refill_threshold =
  1092. min_t(int, rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER,
  1093. ENA_RX_REFILL_THRESH_PACKET);
  1094. /* Optimization, try to batch new rx buffers */
  1095. if (refill_required > refill_threshold)
  1096. ena_refill_rx_bufs(rx_ring, refill_required);
  1097. if (xdp_flags & ENA_XDP_REDIRECT)
  1098. xdp_do_flush();
  1099. return work_done;
  1100. error:
  1101. if (xdp_flags & ENA_XDP_REDIRECT)
  1102. xdp_do_flush();
  1103. adapter = netdev_priv(rx_ring->netdev);
  1104. if (rc == -ENOSPC) {
  1105. ena_increase_stat(&rx_ring->rx_stats.bad_desc_num, 1, &rx_ring->syncp);
  1106. ena_reset_device(adapter, ENA_REGS_RESET_TOO_MANY_RX_DESCS);
  1107. } else if (rc == -EFAULT) {
  1108. ena_reset_device(adapter, ENA_REGS_RESET_RX_DESCRIPTOR_MALFORMED);
  1109. } else {
  1110. ena_increase_stat(&rx_ring->rx_stats.bad_req_id, 1,
  1111. &rx_ring->syncp);
  1112. ena_reset_device(adapter, ENA_REGS_RESET_INV_RX_REQ_ID);
  1113. }
  1114. return 0;
  1115. }
  1116. static void ena_dim_work(struct work_struct *w)
  1117. {
  1118. struct dim *dim = container_of(w, struct dim, work);
  1119. struct dim_cq_moder cur_moder =
  1120. net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
  1121. struct ena_napi *ena_napi = container_of(dim, struct ena_napi, dim);
  1122. ena_napi->rx_ring->smoothed_interval = cur_moder.usec;
  1123. dim->state = DIM_START_MEASURE;
  1124. }
  1125. static void ena_adjust_adaptive_rx_intr_moderation(struct ena_napi *ena_napi)
  1126. {
  1127. struct dim_sample dim_sample;
  1128. struct ena_ring *rx_ring = ena_napi->rx_ring;
  1129. if (!rx_ring->per_napi_packets)
  1130. return;
  1131. rx_ring->non_empty_napi_events++;
  1132. dim_update_sample(rx_ring->non_empty_napi_events,
  1133. rx_ring->rx_stats.cnt,
  1134. rx_ring->rx_stats.bytes,
  1135. &dim_sample);
  1136. net_dim(&ena_napi->dim, dim_sample);
  1137. rx_ring->per_napi_packets = 0;
  1138. }
  1139. void ena_unmask_interrupt(struct ena_ring *tx_ring,
  1140. struct ena_ring *rx_ring)
  1141. {
  1142. u32 rx_interval = tx_ring->smoothed_interval;
  1143. struct ena_eth_io_intr_reg intr_reg;
  1144. /* Rx ring can be NULL when for XDP tx queues which don't have an
  1145. * accompanying rx_ring pair.
  1146. */
  1147. if (rx_ring)
  1148. rx_interval = ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev) ?
  1149. rx_ring->smoothed_interval :
  1150. ena_com_get_nonadaptive_moderation_interval_rx(rx_ring->ena_dev);
  1151. /* Update intr register: rx intr delay,
  1152. * tx intr delay and interrupt unmask
  1153. */
  1154. ena_com_update_intr_reg(&intr_reg,
  1155. rx_interval,
  1156. tx_ring->smoothed_interval,
  1157. true);
  1158. ena_increase_stat(&tx_ring->tx_stats.unmask_interrupt, 1,
  1159. &tx_ring->syncp);
  1160. /* It is a shared MSI-X.
  1161. * Tx and Rx CQ have pointer to it.
  1162. * So we use one of them to reach the intr reg
  1163. * The Tx ring is used because the rx_ring is NULL for XDP queues
  1164. */
  1165. ena_com_unmask_intr(tx_ring->ena_com_io_cq, &intr_reg);
  1166. }
  1167. void ena_update_ring_numa_node(struct ena_ring *tx_ring,
  1168. struct ena_ring *rx_ring)
  1169. {
  1170. int cpu = get_cpu();
  1171. int numa_node;
  1172. /* Check only one ring since the 2 rings are running on the same cpu */
  1173. if (likely(tx_ring->cpu == cpu))
  1174. goto out;
  1175. tx_ring->cpu = cpu;
  1176. if (rx_ring)
  1177. rx_ring->cpu = cpu;
  1178. numa_node = cpu_to_node(cpu);
  1179. if (likely(tx_ring->numa_node == numa_node))
  1180. goto out;
  1181. put_cpu();
  1182. if (numa_node != NUMA_NO_NODE) {
  1183. ena_com_update_numa_node(tx_ring->ena_com_io_cq, numa_node);
  1184. tx_ring->numa_node = numa_node;
  1185. if (rx_ring) {
  1186. rx_ring->numa_node = numa_node;
  1187. ena_com_update_numa_node(rx_ring->ena_com_io_cq,
  1188. numa_node);
  1189. }
  1190. }
  1191. return;
  1192. out:
  1193. put_cpu();
  1194. }
  1195. static int ena_io_poll(struct napi_struct *napi, int budget)
  1196. {
  1197. struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi);
  1198. struct ena_ring *tx_ring, *rx_ring;
  1199. int tx_work_done;
  1200. int rx_work_done = 0;
  1201. int tx_budget;
  1202. int napi_comp_call = 0;
  1203. int ret;
  1204. tx_ring = ena_napi->tx_ring;
  1205. rx_ring = ena_napi->rx_ring;
  1206. tx_budget = tx_ring->ring_size / ENA_TX_POLL_BUDGET_DIVIDER;
  1207. if (!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
  1208. test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags)) {
  1209. napi_complete_done(napi, 0);
  1210. return 0;
  1211. }
  1212. tx_work_done = ena_clean_tx_irq(tx_ring, tx_budget);
  1213. /* On netpoll the budget is zero and the handler should only clean the
  1214. * tx completions.
  1215. */
  1216. if (likely(budget))
  1217. rx_work_done = ena_clean_rx_irq(rx_ring, napi, budget);
  1218. /* If the device is about to reset or down, avoid unmask
  1219. * the interrupt and return 0 so NAPI won't reschedule
  1220. */
  1221. if (unlikely(!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
  1222. test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags))) {
  1223. napi_complete_done(napi, 0);
  1224. ret = 0;
  1225. } else if ((budget > rx_work_done) && (tx_budget > tx_work_done)) {
  1226. napi_comp_call = 1;
  1227. /* Update numa and unmask the interrupt only when schedule
  1228. * from the interrupt context (vs from sk_busy_loop)
  1229. */
  1230. if (napi_complete_done(napi, rx_work_done) &&
  1231. READ_ONCE(ena_napi->interrupts_masked)) {
  1232. smp_rmb(); /* make sure interrupts_masked is read */
  1233. WRITE_ONCE(ena_napi->interrupts_masked, false);
  1234. /* We apply adaptive moderation on Rx path only.
  1235. * Tx uses static interrupt moderation.
  1236. */
  1237. if (ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev))
  1238. ena_adjust_adaptive_rx_intr_moderation(ena_napi);
  1239. ena_update_ring_numa_node(tx_ring, rx_ring);
  1240. ena_unmask_interrupt(tx_ring, rx_ring);
  1241. }
  1242. ret = rx_work_done;
  1243. } else {
  1244. ret = budget;
  1245. }
  1246. u64_stats_update_begin(&tx_ring->syncp);
  1247. tx_ring->tx_stats.napi_comp += napi_comp_call;
  1248. tx_ring->tx_stats.tx_poll++;
  1249. u64_stats_update_end(&tx_ring->syncp);
  1250. tx_ring->tx_stats.last_napi_jiffies = jiffies;
  1251. return ret;
  1252. }
  1253. static irqreturn_t ena_intr_msix_mgmnt(int irq, void *data)
  1254. {
  1255. struct ena_adapter *adapter = (struct ena_adapter *)data;
  1256. ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
  1257. /* Don't call the aenq handler before probe is done */
  1258. if (likely(test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)))
  1259. ena_com_aenq_intr_handler(adapter->ena_dev, data);
  1260. return IRQ_HANDLED;
  1261. }
  1262. /* ena_intr_msix_io - MSI-X Interrupt Handler for Tx/Rx
  1263. * @irq: interrupt number
  1264. * @data: pointer to a network interface private napi device structure
  1265. */
  1266. static irqreturn_t ena_intr_msix_io(int irq, void *data)
  1267. {
  1268. struct ena_napi *ena_napi = data;
  1269. /* Used to check HW health */
  1270. WRITE_ONCE(ena_napi->first_interrupt, true);
  1271. WRITE_ONCE(ena_napi->interrupts_masked, true);
  1272. smp_wmb(); /* write interrupts_masked before calling napi */
  1273. napi_schedule_irqoff(&ena_napi->napi);
  1274. return IRQ_HANDLED;
  1275. }
  1276. /* Reserve a single MSI-X vector for management (admin + aenq).
  1277. * plus reserve one vector for each potential io queue.
  1278. * the number of potential io queues is the minimum of what the device
  1279. * supports and the number of vCPUs.
  1280. */
  1281. static int ena_enable_msix(struct ena_adapter *adapter)
  1282. {
  1283. int msix_vecs, irq_cnt;
  1284. if (test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
  1285. netif_err(adapter, probe, adapter->netdev,
  1286. "Error, MSI-X is already enabled\n");
  1287. return -EPERM;
  1288. }
  1289. /* Reserved the max msix vectors we might need */
  1290. msix_vecs = ENA_MAX_MSIX_VEC(adapter->max_num_io_queues);
  1291. netif_dbg(adapter, probe, adapter->netdev,
  1292. "Trying to enable MSI-X, vectors %d\n", msix_vecs);
  1293. irq_cnt = pci_alloc_irq_vectors(adapter->pdev, ENA_MIN_MSIX_VEC,
  1294. msix_vecs, PCI_IRQ_MSIX);
  1295. if (irq_cnt < 0) {
  1296. netif_err(adapter, probe, adapter->netdev,
  1297. "Failed to enable MSI-X. irq_cnt %d\n", irq_cnt);
  1298. return -ENOSPC;
  1299. }
  1300. if (irq_cnt != msix_vecs) {
  1301. netif_notice(adapter, probe, adapter->netdev,
  1302. "Enable only %d MSI-X (out of %d), reduce the number of queues\n",
  1303. irq_cnt, msix_vecs);
  1304. adapter->num_io_queues = irq_cnt - ENA_ADMIN_MSIX_VEC;
  1305. }
  1306. if (ena_init_rx_cpu_rmap(adapter))
  1307. netif_warn(adapter, probe, adapter->netdev,
  1308. "Failed to map IRQs to CPUs\n");
  1309. adapter->msix_vecs = irq_cnt;
  1310. set_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags);
  1311. return 0;
  1312. }
  1313. static void ena_setup_mgmnt_intr(struct ena_adapter *adapter)
  1314. {
  1315. u32 cpu;
  1316. snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
  1317. ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
  1318. pci_name(adapter->pdev));
  1319. adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler =
  1320. ena_intr_msix_mgmnt;
  1321. adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
  1322. adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
  1323. pci_irq_vector(adapter->pdev, ENA_MGMNT_IRQ_IDX);
  1324. cpu = cpumask_first(cpu_online_mask);
  1325. adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].cpu = cpu;
  1326. cpumask_set_cpu(cpu,
  1327. &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].affinity_hint_mask);
  1328. }
  1329. static void ena_setup_io_intr(struct ena_adapter *adapter)
  1330. {
  1331. struct net_device *netdev;
  1332. int irq_idx, i, cpu;
  1333. int io_queue_count;
  1334. netdev = adapter->netdev;
  1335. io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
  1336. for (i = 0; i < io_queue_count; i++) {
  1337. irq_idx = ENA_IO_IRQ_IDX(i);
  1338. cpu = i % num_online_cpus();
  1339. snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
  1340. "%s-Tx-Rx-%d", netdev->name, i);
  1341. adapter->irq_tbl[irq_idx].handler = ena_intr_msix_io;
  1342. adapter->irq_tbl[irq_idx].data = &adapter->ena_napi[i];
  1343. adapter->irq_tbl[irq_idx].vector =
  1344. pci_irq_vector(adapter->pdev, irq_idx);
  1345. adapter->irq_tbl[irq_idx].cpu = cpu;
  1346. cpumask_set_cpu(cpu,
  1347. &adapter->irq_tbl[irq_idx].affinity_hint_mask);
  1348. }
  1349. }
  1350. static int ena_request_mgmnt_irq(struct ena_adapter *adapter)
  1351. {
  1352. unsigned long flags = 0;
  1353. struct ena_irq *irq;
  1354. int rc;
  1355. irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
  1356. rc = request_irq(irq->vector, irq->handler, flags, irq->name,
  1357. irq->data);
  1358. if (rc) {
  1359. netif_err(adapter, probe, adapter->netdev,
  1360. "Failed to request admin irq\n");
  1361. return rc;
  1362. }
  1363. netif_dbg(adapter, probe, adapter->netdev,
  1364. "Set affinity hint of mgmnt irq.to 0x%lx (irq vector: %d)\n",
  1365. irq->affinity_hint_mask.bits[0], irq->vector);
  1366. irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
  1367. return rc;
  1368. }
  1369. static int ena_request_io_irq(struct ena_adapter *adapter)
  1370. {
  1371. u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
  1372. unsigned long flags = 0;
  1373. struct ena_irq *irq;
  1374. int rc = 0, i, k;
  1375. if (!test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
  1376. netif_err(adapter, ifup, adapter->netdev,
  1377. "Failed to request I/O IRQ: MSI-X is not enabled\n");
  1378. return -EINVAL;
  1379. }
  1380. for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++) {
  1381. irq = &adapter->irq_tbl[i];
  1382. rc = request_irq(irq->vector, irq->handler, flags, irq->name,
  1383. irq->data);
  1384. if (rc) {
  1385. netif_err(adapter, ifup, adapter->netdev,
  1386. "Failed to request I/O IRQ. index %d rc %d\n",
  1387. i, rc);
  1388. goto err;
  1389. }
  1390. netif_dbg(adapter, ifup, adapter->netdev,
  1391. "Set affinity hint of irq. index %d to 0x%lx (irq vector: %d)\n",
  1392. i, irq->affinity_hint_mask.bits[0], irq->vector);
  1393. irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
  1394. }
  1395. return rc;
  1396. err:
  1397. for (k = ENA_IO_IRQ_FIRST_IDX; k < i; k++) {
  1398. irq = &adapter->irq_tbl[k];
  1399. free_irq(irq->vector, irq->data);
  1400. }
  1401. return rc;
  1402. }
  1403. static void ena_free_mgmnt_irq(struct ena_adapter *adapter)
  1404. {
  1405. struct ena_irq *irq;
  1406. irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
  1407. synchronize_irq(irq->vector);
  1408. irq_set_affinity_hint(irq->vector, NULL);
  1409. free_irq(irq->vector, irq->data);
  1410. }
  1411. static void ena_free_io_irq(struct ena_adapter *adapter)
  1412. {
  1413. u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
  1414. struct ena_irq *irq;
  1415. int i;
  1416. #ifdef CONFIG_RFS_ACCEL
  1417. if (adapter->msix_vecs >= 1) {
  1418. free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
  1419. adapter->netdev->rx_cpu_rmap = NULL;
  1420. }
  1421. #endif /* CONFIG_RFS_ACCEL */
  1422. for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++) {
  1423. irq = &adapter->irq_tbl[i];
  1424. irq_set_affinity_hint(irq->vector, NULL);
  1425. free_irq(irq->vector, irq->data);
  1426. }
  1427. }
  1428. static void ena_disable_msix(struct ena_adapter *adapter)
  1429. {
  1430. if (test_and_clear_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags))
  1431. pci_free_irq_vectors(adapter->pdev);
  1432. }
  1433. static void ena_disable_io_intr_sync(struct ena_adapter *adapter)
  1434. {
  1435. u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
  1436. int i;
  1437. if (!netif_running(adapter->netdev))
  1438. return;
  1439. for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++)
  1440. synchronize_irq(adapter->irq_tbl[i].vector);
  1441. }
  1442. static void ena_del_napi_in_range(struct ena_adapter *adapter,
  1443. int first_index,
  1444. int count)
  1445. {
  1446. int i;
  1447. for (i = first_index; i < first_index + count; i++) {
  1448. netif_napi_del(&adapter->ena_napi[i].napi);
  1449. WARN_ON(ENA_IS_XDP_INDEX(adapter, i) &&
  1450. adapter->ena_napi[i].rx_ring);
  1451. }
  1452. }
  1453. static void ena_init_napi_in_range(struct ena_adapter *adapter,
  1454. int first_index, int count)
  1455. {
  1456. int (*napi_handler)(struct napi_struct *napi, int budget);
  1457. int i;
  1458. for (i = first_index; i < first_index + count; i++) {
  1459. struct ena_napi *napi = &adapter->ena_napi[i];
  1460. struct ena_ring *rx_ring, *tx_ring;
  1461. memset(napi, 0, sizeof(*napi));
  1462. rx_ring = &adapter->rx_ring[i];
  1463. tx_ring = &adapter->tx_ring[i];
  1464. napi_handler = ena_io_poll;
  1465. if (ENA_IS_XDP_INDEX(adapter, i))
  1466. napi_handler = ena_xdp_io_poll;
  1467. netif_napi_add(adapter->netdev, &napi->napi, napi_handler);
  1468. if (!ENA_IS_XDP_INDEX(adapter, i))
  1469. napi->rx_ring = rx_ring;
  1470. napi->tx_ring = tx_ring;
  1471. napi->qid = i;
  1472. }
  1473. }
  1474. static void ena_napi_disable_in_range(struct ena_adapter *adapter,
  1475. int first_index,
  1476. int count)
  1477. {
  1478. int i;
  1479. for (i = first_index; i < first_index + count; i++)
  1480. napi_disable(&adapter->ena_napi[i].napi);
  1481. }
  1482. static void ena_napi_enable_in_range(struct ena_adapter *adapter,
  1483. int first_index,
  1484. int count)
  1485. {
  1486. int i;
  1487. for (i = first_index; i < first_index + count; i++)
  1488. napi_enable(&adapter->ena_napi[i].napi);
  1489. }
  1490. /* Configure the Rx forwarding */
  1491. static int ena_rss_configure(struct ena_adapter *adapter)
  1492. {
  1493. struct ena_com_dev *ena_dev = adapter->ena_dev;
  1494. int rc;
  1495. /* In case the RSS table wasn't initialized by probe */
  1496. if (!ena_dev->rss.tbl_log_size) {
  1497. rc = ena_rss_init_default(adapter);
  1498. if (rc && (rc != -EOPNOTSUPP)) {
  1499. netif_err(adapter, ifup, adapter->netdev, "Failed to init RSS rc: %d\n", rc);
  1500. return rc;
  1501. }
  1502. }
  1503. /* Set indirect table */
  1504. rc = ena_com_indirect_table_set(ena_dev);
  1505. if (unlikely(rc && rc != -EOPNOTSUPP))
  1506. return rc;
  1507. /* Configure hash function (if supported) */
  1508. rc = ena_com_set_hash_function(ena_dev);
  1509. if (unlikely(rc && (rc != -EOPNOTSUPP)))
  1510. return rc;
  1511. /* Configure hash inputs (if supported) */
  1512. rc = ena_com_set_hash_ctrl(ena_dev);
  1513. if (unlikely(rc && (rc != -EOPNOTSUPP)))
  1514. return rc;
  1515. return 0;
  1516. }
  1517. static int ena_up_complete(struct ena_adapter *adapter)
  1518. {
  1519. int rc;
  1520. rc = ena_rss_configure(adapter);
  1521. if (rc)
  1522. return rc;
  1523. ena_change_mtu(adapter->netdev, adapter->netdev->mtu);
  1524. ena_refill_all_rx_bufs(adapter);
  1525. /* enable transmits */
  1526. netif_tx_start_all_queues(adapter->netdev);
  1527. ena_napi_enable_in_range(adapter,
  1528. 0,
  1529. adapter->xdp_num_queues + adapter->num_io_queues);
  1530. return 0;
  1531. }
  1532. static int ena_create_io_tx_queue(struct ena_adapter *adapter, int qid)
  1533. {
  1534. struct ena_com_create_io_ctx ctx;
  1535. struct ena_com_dev *ena_dev;
  1536. struct ena_ring *tx_ring;
  1537. u32 msix_vector;
  1538. u16 ena_qid;
  1539. int rc;
  1540. ena_dev = adapter->ena_dev;
  1541. tx_ring = &adapter->tx_ring[qid];
  1542. msix_vector = ENA_IO_IRQ_IDX(qid);
  1543. ena_qid = ENA_IO_TXQ_IDX(qid);
  1544. memset(&ctx, 0x0, sizeof(ctx));
  1545. ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
  1546. ctx.qid = ena_qid;
  1547. ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
  1548. ctx.msix_vector = msix_vector;
  1549. ctx.queue_size = tx_ring->ring_size;
  1550. ctx.numa_node = tx_ring->numa_node;
  1551. rc = ena_com_create_io_queue(ena_dev, &ctx);
  1552. if (rc) {
  1553. netif_err(adapter, ifup, adapter->netdev,
  1554. "Failed to create I/O TX queue num %d rc: %d\n",
  1555. qid, rc);
  1556. return rc;
  1557. }
  1558. rc = ena_com_get_io_handlers(ena_dev, ena_qid,
  1559. &tx_ring->ena_com_io_sq,
  1560. &tx_ring->ena_com_io_cq);
  1561. if (rc) {
  1562. netif_err(adapter, ifup, adapter->netdev,
  1563. "Failed to get TX queue handlers. TX queue num %d rc: %d\n",
  1564. qid, rc);
  1565. ena_com_destroy_io_queue(ena_dev, ena_qid);
  1566. return rc;
  1567. }
  1568. ena_com_update_numa_node(tx_ring->ena_com_io_cq, ctx.numa_node);
  1569. return rc;
  1570. }
  1571. int ena_create_io_tx_queues_in_range(struct ena_adapter *adapter,
  1572. int first_index, int count)
  1573. {
  1574. struct ena_com_dev *ena_dev = adapter->ena_dev;
  1575. int rc, i;
  1576. for (i = first_index; i < first_index + count; i++) {
  1577. rc = ena_create_io_tx_queue(adapter, i);
  1578. if (rc)
  1579. goto create_err;
  1580. }
  1581. return 0;
  1582. create_err:
  1583. while (i-- > first_index)
  1584. ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
  1585. return rc;
  1586. }
  1587. static int ena_create_io_rx_queue(struct ena_adapter *adapter, int qid)
  1588. {
  1589. struct ena_com_dev *ena_dev;
  1590. struct ena_com_create_io_ctx ctx;
  1591. struct ena_ring *rx_ring;
  1592. u32 msix_vector;
  1593. u16 ena_qid;
  1594. int rc;
  1595. ena_dev = adapter->ena_dev;
  1596. rx_ring = &adapter->rx_ring[qid];
  1597. msix_vector = ENA_IO_IRQ_IDX(qid);
  1598. ena_qid = ENA_IO_RXQ_IDX(qid);
  1599. memset(&ctx, 0x0, sizeof(ctx));
  1600. ctx.qid = ena_qid;
  1601. ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
  1602. ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
  1603. ctx.msix_vector = msix_vector;
  1604. ctx.queue_size = rx_ring->ring_size;
  1605. ctx.numa_node = rx_ring->numa_node;
  1606. rc = ena_com_create_io_queue(ena_dev, &ctx);
  1607. if (rc) {
  1608. netif_err(adapter, ifup, adapter->netdev,
  1609. "Failed to create I/O RX queue num %d rc: %d\n",
  1610. qid, rc);
  1611. return rc;
  1612. }
  1613. rc = ena_com_get_io_handlers(ena_dev, ena_qid,
  1614. &rx_ring->ena_com_io_sq,
  1615. &rx_ring->ena_com_io_cq);
  1616. if (rc) {
  1617. netif_err(adapter, ifup, adapter->netdev,
  1618. "Failed to get RX queue handlers. RX queue num %d rc: %d\n",
  1619. qid, rc);
  1620. goto err;
  1621. }
  1622. ena_com_update_numa_node(rx_ring->ena_com_io_cq, ctx.numa_node);
  1623. return rc;
  1624. err:
  1625. ena_com_destroy_io_queue(ena_dev, ena_qid);
  1626. return rc;
  1627. }
  1628. static int ena_create_all_io_rx_queues(struct ena_adapter *adapter)
  1629. {
  1630. struct ena_com_dev *ena_dev = adapter->ena_dev;
  1631. int rc, i;
  1632. for (i = 0; i < adapter->num_io_queues; i++) {
  1633. rc = ena_create_io_rx_queue(adapter, i);
  1634. if (rc)
  1635. goto create_err;
  1636. INIT_WORK(&adapter->ena_napi[i].dim.work, ena_dim_work);
  1637. ena_xdp_register_rxq_info(&adapter->rx_ring[i]);
  1638. }
  1639. return 0;
  1640. create_err:
  1641. while (i--) {
  1642. ena_xdp_unregister_rxq_info(&adapter->rx_ring[i]);
  1643. cancel_work_sync(&adapter->ena_napi[i].dim.work);
  1644. ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
  1645. }
  1646. return rc;
  1647. }
  1648. static void set_io_rings_size(struct ena_adapter *adapter,
  1649. int new_tx_size,
  1650. int new_rx_size)
  1651. {
  1652. int i;
  1653. for (i = 0; i < adapter->num_io_queues; i++) {
  1654. adapter->tx_ring[i].ring_size = new_tx_size;
  1655. adapter->rx_ring[i].ring_size = new_rx_size;
  1656. }
  1657. }
  1658. /* This function allows queue allocation to backoff when the system is
  1659. * low on memory. If there is not enough memory to allocate io queues
  1660. * the driver will try to allocate smaller queues.
  1661. *
  1662. * The backoff algorithm is as follows:
  1663. * 1. Try to allocate TX and RX and if successful.
  1664. * 1.1. return success
  1665. *
  1666. * 2. Divide by 2 the size of the larger of RX and TX queues (or both if their size is the same).
  1667. *
  1668. * 3. If TX or RX is smaller than 256
  1669. * 3.1. return failure.
  1670. * 4. else
  1671. * 4.1. go back to 1.
  1672. */
  1673. static int create_queues_with_size_backoff(struct ena_adapter *adapter)
  1674. {
  1675. int rc, cur_rx_ring_size, cur_tx_ring_size;
  1676. int new_rx_ring_size, new_tx_ring_size;
  1677. /* current queue sizes might be set to smaller than the requested
  1678. * ones due to past queue allocation failures.
  1679. */
  1680. set_io_rings_size(adapter, adapter->requested_tx_ring_size,
  1681. adapter->requested_rx_ring_size);
  1682. while (1) {
  1683. if (ena_xdp_present(adapter)) {
  1684. rc = ena_setup_and_create_all_xdp_queues(adapter);
  1685. if (rc)
  1686. goto err_setup_tx;
  1687. }
  1688. rc = ena_setup_tx_resources_in_range(adapter,
  1689. 0,
  1690. adapter->num_io_queues);
  1691. if (rc)
  1692. goto err_setup_tx;
  1693. rc = ena_create_io_tx_queues_in_range(adapter,
  1694. 0,
  1695. adapter->num_io_queues);
  1696. if (rc)
  1697. goto err_create_tx_queues;
  1698. rc = ena_setup_all_rx_resources(adapter);
  1699. if (rc)
  1700. goto err_setup_rx;
  1701. rc = ena_create_all_io_rx_queues(adapter);
  1702. if (rc)
  1703. goto err_create_rx_queues;
  1704. return 0;
  1705. err_create_rx_queues:
  1706. ena_free_all_io_rx_resources(adapter);
  1707. err_setup_rx:
  1708. ena_destroy_all_tx_queues(adapter);
  1709. err_create_tx_queues:
  1710. ena_free_all_io_tx_resources(adapter);
  1711. err_setup_tx:
  1712. if (rc != -ENOMEM) {
  1713. netif_err(adapter, ifup, adapter->netdev,
  1714. "Queue creation failed with error code %d\n",
  1715. rc);
  1716. return rc;
  1717. }
  1718. cur_tx_ring_size = adapter->tx_ring[0].ring_size;
  1719. cur_rx_ring_size = adapter->rx_ring[0].ring_size;
  1720. netif_err(adapter, ifup, adapter->netdev,
  1721. "Not enough memory to create queues with sizes TX=%d, RX=%d\n",
  1722. cur_tx_ring_size, cur_rx_ring_size);
  1723. new_tx_ring_size = cur_tx_ring_size;
  1724. new_rx_ring_size = cur_rx_ring_size;
  1725. /* Decrease the size of the larger queue, or
  1726. * decrease both if they are the same size.
  1727. */
  1728. if (cur_rx_ring_size <= cur_tx_ring_size)
  1729. new_tx_ring_size = cur_tx_ring_size / 2;
  1730. if (cur_rx_ring_size >= cur_tx_ring_size)
  1731. new_rx_ring_size = cur_rx_ring_size / 2;
  1732. if (new_tx_ring_size < ENA_MIN_RING_SIZE ||
  1733. new_rx_ring_size < ENA_MIN_RING_SIZE) {
  1734. netif_err(adapter, ifup, adapter->netdev,
  1735. "Queue creation failed with the smallest possible queue size of %d for both queues. Not retrying with smaller queues\n",
  1736. ENA_MIN_RING_SIZE);
  1737. return rc;
  1738. }
  1739. netif_err(adapter, ifup, adapter->netdev,
  1740. "Retrying queue creation with sizes TX=%d, RX=%d\n",
  1741. new_tx_ring_size,
  1742. new_rx_ring_size);
  1743. set_io_rings_size(adapter, new_tx_ring_size,
  1744. new_rx_ring_size);
  1745. }
  1746. }
  1747. int ena_up(struct ena_adapter *adapter)
  1748. {
  1749. int io_queue_count, rc, i;
  1750. netif_dbg(adapter, ifup, adapter->netdev, "%s\n", __func__);
  1751. io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
  1752. ena_setup_io_intr(adapter);
  1753. /* napi poll functions should be initialized before running
  1754. * request_irq(), to handle a rare condition where there is a pending
  1755. * interrupt, causing the ISR to fire immediately while the poll
  1756. * function wasn't set yet, causing a null dereference
  1757. */
  1758. ena_init_napi_in_range(adapter, 0, io_queue_count);
  1759. /* Enabling DIM needs to happen before enabling IRQs since DIM
  1760. * is run from napi routine
  1761. */
  1762. if (ena_com_interrupt_moderation_supported(adapter->ena_dev))
  1763. ena_com_enable_adaptive_moderation(adapter->ena_dev);
  1764. rc = ena_request_io_irq(adapter);
  1765. if (rc)
  1766. goto err_req_irq;
  1767. rc = create_queues_with_size_backoff(adapter);
  1768. if (rc)
  1769. goto err_create_queues_with_backoff;
  1770. rc = ena_up_complete(adapter);
  1771. if (rc)
  1772. goto err_up;
  1773. if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
  1774. netif_carrier_on(adapter->netdev);
  1775. ena_increase_stat(&adapter->dev_stats.interface_up, 1,
  1776. &adapter->syncp);
  1777. set_bit(ENA_FLAG_DEV_UP, &adapter->flags);
  1778. /* Enable completion queues interrupt */
  1779. for (i = 0; i < adapter->num_io_queues; i++)
  1780. ena_unmask_interrupt(&adapter->tx_ring[i],
  1781. &adapter->rx_ring[i]);
  1782. /* schedule napi in case we had pending packets
  1783. * from the last time we disable napi
  1784. */
  1785. for (i = 0; i < io_queue_count; i++)
  1786. napi_schedule(&adapter->ena_napi[i].napi);
  1787. return rc;
  1788. err_up:
  1789. ena_destroy_all_tx_queues(adapter);
  1790. ena_free_all_io_tx_resources(adapter);
  1791. ena_destroy_all_rx_queues(adapter);
  1792. ena_free_all_io_rx_resources(adapter);
  1793. err_create_queues_with_backoff:
  1794. ena_free_io_irq(adapter);
  1795. err_req_irq:
  1796. ena_del_napi_in_range(adapter, 0, io_queue_count);
  1797. return rc;
  1798. }
  1799. void ena_down(struct ena_adapter *adapter)
  1800. {
  1801. int io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
  1802. netif_dbg(adapter, ifdown, adapter->netdev, "%s\n", __func__);
  1803. clear_bit(ENA_FLAG_DEV_UP, &adapter->flags);
  1804. ena_increase_stat(&adapter->dev_stats.interface_down, 1,
  1805. &adapter->syncp);
  1806. netif_carrier_off(adapter->netdev);
  1807. netif_tx_disable(adapter->netdev);
  1808. /* After this point the napi handler won't enable the tx queue */
  1809. ena_napi_disable_in_range(adapter, 0, io_queue_count);
  1810. if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) {
  1811. int rc;
  1812. rc = ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
  1813. if (rc)
  1814. netif_err(adapter, ifdown, adapter->netdev,
  1815. "Device reset failed\n");
  1816. /* stop submitting admin commands on a device that was reset */
  1817. ena_com_set_admin_running_state(adapter->ena_dev, false);
  1818. }
  1819. ena_destroy_all_io_queues(adapter);
  1820. ena_disable_io_intr_sync(adapter);
  1821. ena_free_io_irq(adapter);
  1822. ena_del_napi_in_range(adapter, 0, io_queue_count);
  1823. ena_free_all_tx_bufs(adapter);
  1824. ena_free_all_rx_bufs(adapter);
  1825. ena_free_all_io_tx_resources(adapter);
  1826. ena_free_all_io_rx_resources(adapter);
  1827. }
  1828. /* ena_open - Called when a network interface is made active
  1829. * @netdev: network interface device structure
  1830. *
  1831. * Returns 0 on success, negative value on failure
  1832. *
  1833. * The open entry point is called when a network interface is made
  1834. * active by the system (IFF_UP). At this point all resources needed
  1835. * for transmit and receive operations are allocated, the interrupt
  1836. * handler is registered with the OS, the watchdog timer is started,
  1837. * and the stack is notified that the interface is ready.
  1838. */
  1839. static int ena_open(struct net_device *netdev)
  1840. {
  1841. struct ena_adapter *adapter = netdev_priv(netdev);
  1842. int rc;
  1843. /* Notify the stack of the actual queue counts. */
  1844. rc = netif_set_real_num_tx_queues(netdev, adapter->num_io_queues);
  1845. if (rc) {
  1846. netif_err(adapter, ifup, netdev, "Can't set num tx queues\n");
  1847. return rc;
  1848. }
  1849. rc = netif_set_real_num_rx_queues(netdev, adapter->num_io_queues);
  1850. if (rc) {
  1851. netif_err(adapter, ifup, netdev, "Can't set num rx queues\n");
  1852. return rc;
  1853. }
  1854. rc = ena_up(adapter);
  1855. if (rc)
  1856. return rc;
  1857. return rc;
  1858. }
  1859. /* ena_close - Disables a network interface
  1860. * @netdev: network interface device structure
  1861. *
  1862. * Returns 0, this is not allowed to fail
  1863. *
  1864. * The close entry point is called when an interface is de-activated
  1865. * by the OS. The hardware is still under the drivers control, but
  1866. * needs to be disabled. A global MAC reset is issued to stop the
  1867. * hardware, and all transmit and receive resources are freed.
  1868. */
  1869. static int ena_close(struct net_device *netdev)
  1870. {
  1871. struct ena_adapter *adapter = netdev_priv(netdev);
  1872. netif_dbg(adapter, ifdown, netdev, "%s\n", __func__);
  1873. if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))
  1874. return 0;
  1875. if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
  1876. ena_down(adapter);
  1877. /* Check for device status and issue reset if needed*/
  1878. check_for_admin_com_state(adapter);
  1879. if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
  1880. netif_err(adapter, ifdown, adapter->netdev,
  1881. "Destroy failure, restarting device\n");
  1882. ena_dump_stats_to_dmesg(adapter);
  1883. /* rtnl lock already obtained in dev_ioctl() layer */
  1884. ena_destroy_device(adapter, false);
  1885. ena_restore_device(adapter);
  1886. }
  1887. return 0;
  1888. }
  1889. int ena_update_queue_params(struct ena_adapter *adapter,
  1890. u32 new_tx_size,
  1891. u32 new_rx_size,
  1892. u32 new_llq_header_len)
  1893. {
  1894. bool dev_was_up, large_llq_changed = false;
  1895. int rc = 0;
  1896. dev_was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
  1897. ena_close(adapter->netdev);
  1898. adapter->requested_tx_ring_size = new_tx_size;
  1899. adapter->requested_rx_ring_size = new_rx_size;
  1900. ena_init_io_rings(adapter,
  1901. 0,
  1902. adapter->xdp_num_queues +
  1903. adapter->num_io_queues);
  1904. large_llq_changed = adapter->ena_dev->tx_mem_queue_type ==
  1905. ENA_ADMIN_PLACEMENT_POLICY_DEV;
  1906. large_llq_changed &=
  1907. new_llq_header_len != adapter->ena_dev->tx_max_header_size;
  1908. /* a check that the configuration is valid is done by caller */
  1909. if (large_llq_changed) {
  1910. adapter->large_llq_header_enabled = !adapter->large_llq_header_enabled;
  1911. ena_destroy_device(adapter, false);
  1912. rc = ena_restore_device(adapter);
  1913. }
  1914. return dev_was_up && !rc ? ena_up(adapter) : rc;
  1915. }
  1916. int ena_set_rx_copybreak(struct ena_adapter *adapter, u32 rx_copybreak)
  1917. {
  1918. struct ena_ring *rx_ring;
  1919. int i;
  1920. if (rx_copybreak > min_t(u16, adapter->netdev->mtu, ENA_PAGE_SIZE))
  1921. return -EINVAL;
  1922. adapter->rx_copybreak = rx_copybreak;
  1923. for (i = 0; i < adapter->num_io_queues; i++) {
  1924. rx_ring = &adapter->rx_ring[i];
  1925. rx_ring->rx_copybreak = rx_copybreak;
  1926. }
  1927. return 0;
  1928. }
  1929. int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count)
  1930. {
  1931. struct ena_com_dev *ena_dev = adapter->ena_dev;
  1932. int prev_channel_count;
  1933. bool dev_was_up;
  1934. dev_was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
  1935. ena_close(adapter->netdev);
  1936. prev_channel_count = adapter->num_io_queues;
  1937. adapter->num_io_queues = new_channel_count;
  1938. if (ena_xdp_present(adapter) &&
  1939. ena_xdp_allowed(adapter) == ENA_XDP_ALLOWED) {
  1940. adapter->xdp_first_ring = new_channel_count;
  1941. adapter->xdp_num_queues = new_channel_count;
  1942. if (prev_channel_count > new_channel_count)
  1943. ena_xdp_exchange_program_rx_in_range(adapter,
  1944. NULL,
  1945. new_channel_count,
  1946. prev_channel_count);
  1947. else
  1948. ena_xdp_exchange_program_rx_in_range(adapter,
  1949. adapter->xdp_bpf_prog,
  1950. prev_channel_count,
  1951. new_channel_count);
  1952. }
  1953. /* We need to destroy the rss table so that the indirection
  1954. * table will be reinitialized by ena_up()
  1955. */
  1956. ena_com_rss_destroy(ena_dev);
  1957. ena_init_io_rings(adapter,
  1958. 0,
  1959. adapter->xdp_num_queues +
  1960. adapter->num_io_queues);
  1961. return dev_was_up ? ena_open(adapter->netdev) : 0;
  1962. }
  1963. static void ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx,
  1964. struct sk_buff *skb,
  1965. bool disable_meta_caching)
  1966. {
  1967. u32 mss = skb_shinfo(skb)->gso_size;
  1968. struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
  1969. u8 l4_protocol = 0;
  1970. if ((skb->ip_summed == CHECKSUM_PARTIAL) || mss) {
  1971. ena_tx_ctx->l4_csum_enable = 1;
  1972. if (mss) {
  1973. ena_tx_ctx->tso_enable = 1;
  1974. ena_meta->l4_hdr_len = tcp_hdr(skb)->doff;
  1975. ena_tx_ctx->l4_csum_partial = 0;
  1976. } else {
  1977. ena_tx_ctx->tso_enable = 0;
  1978. ena_meta->l4_hdr_len = 0;
  1979. ena_tx_ctx->l4_csum_partial = 1;
  1980. }
  1981. switch (ip_hdr(skb)->version) {
  1982. case IPVERSION:
  1983. ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
  1984. if (ip_hdr(skb)->frag_off & htons(IP_DF))
  1985. ena_tx_ctx->df = 1;
  1986. if (mss)
  1987. ena_tx_ctx->l3_csum_enable = 1;
  1988. l4_protocol = ip_hdr(skb)->protocol;
  1989. break;
  1990. case 6:
  1991. ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
  1992. l4_protocol = ipv6_hdr(skb)->nexthdr;
  1993. break;
  1994. default:
  1995. break;
  1996. }
  1997. if (l4_protocol == IPPROTO_TCP)
  1998. ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
  1999. else
  2000. ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
  2001. ena_meta->mss = mss;
  2002. ena_meta->l3_hdr_len = skb_network_header_len(skb);
  2003. ena_meta->l3_hdr_offset = skb_network_offset(skb);
  2004. ena_tx_ctx->meta_valid = 1;
  2005. } else if (disable_meta_caching) {
  2006. memset(ena_meta, 0, sizeof(*ena_meta));
  2007. ena_tx_ctx->meta_valid = 1;
  2008. } else {
  2009. ena_tx_ctx->meta_valid = 0;
  2010. }
  2011. }
  2012. static int ena_check_and_linearize_skb(struct ena_ring *tx_ring,
  2013. struct sk_buff *skb)
  2014. {
  2015. int num_frags, header_len, rc;
  2016. num_frags = skb_shinfo(skb)->nr_frags;
  2017. header_len = skb_headlen(skb);
  2018. if (num_frags < tx_ring->sgl_size)
  2019. return 0;
  2020. if ((num_frags == tx_ring->sgl_size) &&
  2021. (header_len < tx_ring->tx_max_header_size))
  2022. return 0;
  2023. ena_increase_stat(&tx_ring->tx_stats.linearize, 1, &tx_ring->syncp);
  2024. rc = skb_linearize(skb);
  2025. if (unlikely(rc)) {
  2026. ena_increase_stat(&tx_ring->tx_stats.linearize_failed, 1,
  2027. &tx_ring->syncp);
  2028. }
  2029. return rc;
  2030. }
  2031. static int ena_tx_map_skb(struct ena_ring *tx_ring,
  2032. struct ena_tx_buffer *tx_info,
  2033. struct sk_buff *skb,
  2034. void **push_hdr,
  2035. u16 *header_len)
  2036. {
  2037. struct ena_adapter *adapter = tx_ring->adapter;
  2038. struct ena_com_buf *ena_buf;
  2039. dma_addr_t dma;
  2040. u32 skb_head_len, frag_len, last_frag;
  2041. u16 push_len = 0;
  2042. u16 delta = 0;
  2043. int i = 0;
  2044. skb_head_len = skb_headlen(skb);
  2045. tx_info->skb = skb;
  2046. ena_buf = tx_info->bufs;
  2047. if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
  2048. /* When the device is LLQ mode, the driver will copy
  2049. * the header into the device memory space.
  2050. * the ena_com layer assume the header is in a linear
  2051. * memory space.
  2052. * This assumption might be wrong since part of the header
  2053. * can be in the fragmented buffers.
  2054. * Use skb_header_pointer to make sure the header is in a
  2055. * linear memory space.
  2056. */
  2057. push_len = min_t(u32, skb->len, tx_ring->tx_max_header_size);
  2058. *push_hdr = skb_header_pointer(skb, 0, push_len,
  2059. tx_ring->push_buf_intermediate_buf);
  2060. *header_len = push_len;
  2061. if (unlikely(skb->data != *push_hdr)) {
  2062. ena_increase_stat(&tx_ring->tx_stats.llq_buffer_copy, 1,
  2063. &tx_ring->syncp);
  2064. delta = push_len - skb_head_len;
  2065. }
  2066. } else {
  2067. *push_hdr = NULL;
  2068. *header_len = min_t(u32, skb_head_len,
  2069. tx_ring->tx_max_header_size);
  2070. }
  2071. netif_dbg(adapter, tx_queued, adapter->netdev,
  2072. "skb: %p header_buf->vaddr: %p push_len: %d\n", skb,
  2073. *push_hdr, push_len);
  2074. if (skb_head_len > push_len) {
  2075. dma = dma_map_single(tx_ring->dev, skb->data + push_len,
  2076. skb_head_len - push_len, DMA_TO_DEVICE);
  2077. if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
  2078. goto error_report_dma_error;
  2079. ena_buf->paddr = dma;
  2080. ena_buf->len = skb_head_len - push_len;
  2081. ena_buf++;
  2082. tx_info->num_of_bufs++;
  2083. tx_info->map_linear_data = 1;
  2084. } else {
  2085. tx_info->map_linear_data = 0;
  2086. }
  2087. last_frag = skb_shinfo(skb)->nr_frags;
  2088. for (i = 0; i < last_frag; i++) {
  2089. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2090. frag_len = skb_frag_size(frag);
  2091. if (unlikely(delta >= frag_len)) {
  2092. delta -= frag_len;
  2093. continue;
  2094. }
  2095. dma = skb_frag_dma_map(tx_ring->dev, frag, delta,
  2096. frag_len - delta, DMA_TO_DEVICE);
  2097. if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
  2098. goto error_report_dma_error;
  2099. ena_buf->paddr = dma;
  2100. ena_buf->len = frag_len - delta;
  2101. ena_buf++;
  2102. tx_info->num_of_bufs++;
  2103. delta = 0;
  2104. }
  2105. return 0;
  2106. error_report_dma_error:
  2107. ena_increase_stat(&tx_ring->tx_stats.dma_mapping_err, 1,
  2108. &tx_ring->syncp);
  2109. netif_warn(adapter, tx_queued, adapter->netdev, "Failed to map skb\n");
  2110. tx_info->skb = NULL;
  2111. tx_info->num_of_bufs += i;
  2112. ena_unmap_tx_buff(tx_ring, tx_info);
  2113. return -EINVAL;
  2114. }
  2115. /* Called with netif_tx_lock. */
  2116. static netdev_tx_t ena_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2117. {
  2118. struct ena_adapter *adapter = netdev_priv(dev);
  2119. struct ena_tx_buffer *tx_info;
  2120. struct ena_com_tx_ctx ena_tx_ctx;
  2121. struct ena_ring *tx_ring;
  2122. struct netdev_queue *txq;
  2123. void *push_hdr;
  2124. u16 next_to_use, req_id, header_len;
  2125. int qid, rc;
  2126. netif_dbg(adapter, tx_queued, dev, "%s skb %p\n", __func__, skb);
  2127. /* Determine which tx ring we will be placed on */
  2128. qid = skb_get_queue_mapping(skb);
  2129. tx_ring = &adapter->tx_ring[qid];
  2130. txq = netdev_get_tx_queue(dev, qid);
  2131. rc = ena_check_and_linearize_skb(tx_ring, skb);
  2132. if (unlikely(rc))
  2133. goto error_drop_packet;
  2134. next_to_use = tx_ring->next_to_use;
  2135. req_id = tx_ring->free_ids[next_to_use];
  2136. tx_info = &tx_ring->tx_buffer_info[req_id];
  2137. tx_info->num_of_bufs = 0;
  2138. WARN(tx_info->skb, "SKB isn't NULL req_id %d\n", req_id);
  2139. rc = ena_tx_map_skb(tx_ring, tx_info, skb, &push_hdr, &header_len);
  2140. if (unlikely(rc))
  2141. goto error_drop_packet;
  2142. memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
  2143. ena_tx_ctx.ena_bufs = tx_info->bufs;
  2144. ena_tx_ctx.push_header = push_hdr;
  2145. ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
  2146. ena_tx_ctx.req_id = req_id;
  2147. ena_tx_ctx.header_len = header_len;
  2148. /* set flags and meta data */
  2149. ena_tx_csum(&ena_tx_ctx, skb, tx_ring->disable_meta_caching);
  2150. rc = ena_xmit_common(adapter,
  2151. tx_ring,
  2152. tx_info,
  2153. &ena_tx_ctx,
  2154. next_to_use,
  2155. skb->len);
  2156. if (rc)
  2157. goto error_unmap_dma;
  2158. netdev_tx_sent_queue(txq, skb->len);
  2159. /* stop the queue when no more space available, the packet can have up
  2160. * to sgl_size + 2. one for the meta descriptor and one for header
  2161. * (if the header is larger than tx_max_header_size).
  2162. */
  2163. if (unlikely(!ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
  2164. tx_ring->sgl_size + 2))) {
  2165. netif_dbg(adapter, tx_queued, dev, "%s stop queue %d\n",
  2166. __func__, qid);
  2167. netif_tx_stop_queue(txq);
  2168. ena_increase_stat(&tx_ring->tx_stats.queue_stop, 1,
  2169. &tx_ring->syncp);
  2170. /* There is a rare condition where this function decide to
  2171. * stop the queue but meanwhile clean_tx_irq updates
  2172. * next_to_completion and terminates.
  2173. * The queue will remain stopped forever.
  2174. * To solve this issue add a mb() to make sure that
  2175. * netif_tx_stop_queue() write is vissible before checking if
  2176. * there is additional space in the queue.
  2177. */
  2178. smp_mb();
  2179. if (ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
  2180. ENA_TX_WAKEUP_THRESH)) {
  2181. netif_tx_wake_queue(txq);
  2182. ena_increase_stat(&tx_ring->tx_stats.queue_wakeup, 1,
  2183. &tx_ring->syncp);
  2184. }
  2185. }
  2186. skb_tx_timestamp(skb);
  2187. if (netif_xmit_stopped(txq) || !netdev_xmit_more())
  2188. /* trigger the dma engine. ena_ring_tx_doorbell()
  2189. * calls a memory barrier inside it.
  2190. */
  2191. ena_ring_tx_doorbell(tx_ring);
  2192. return NETDEV_TX_OK;
  2193. error_unmap_dma:
  2194. ena_unmap_tx_buff(tx_ring, tx_info);
  2195. tx_info->skb = NULL;
  2196. error_drop_packet:
  2197. dev_kfree_skb(skb);
  2198. return NETDEV_TX_OK;
  2199. }
  2200. static void ena_config_host_info(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
  2201. {
  2202. struct device *dev = &pdev->dev;
  2203. struct ena_admin_host_info *host_info;
  2204. ssize_t ret;
  2205. int rc;
  2206. /* Allocate only the host info */
  2207. rc = ena_com_allocate_host_info(ena_dev);
  2208. if (rc) {
  2209. dev_err(dev, "Cannot allocate host info\n");
  2210. return;
  2211. }
  2212. host_info = ena_dev->host_attr.host_info;
  2213. host_info->bdf = pci_dev_id(pdev);
  2214. host_info->os_type = ENA_ADMIN_OS_LINUX;
  2215. host_info->kernel_ver = LINUX_VERSION_CODE;
  2216. ret = strscpy(host_info->kernel_ver_str, utsname()->version,
  2217. sizeof(host_info->kernel_ver_str));
  2218. if (ret < 0)
  2219. dev_dbg(dev,
  2220. "kernel version string will be truncated, status = %zd\n", ret);
  2221. host_info->os_dist = 0;
  2222. ret = strscpy(host_info->os_dist_str, utsname()->release,
  2223. sizeof(host_info->os_dist_str));
  2224. if (ret < 0)
  2225. dev_dbg(dev,
  2226. "OS distribution string will be truncated, status = %zd\n", ret);
  2227. host_info->driver_version =
  2228. (DRV_MODULE_GEN_MAJOR) |
  2229. (DRV_MODULE_GEN_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
  2230. (DRV_MODULE_GEN_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT) |
  2231. ("K"[0] << ENA_ADMIN_HOST_INFO_MODULE_TYPE_SHIFT);
  2232. host_info->num_cpus = num_online_cpus();
  2233. host_info->driver_supported_features =
  2234. ENA_ADMIN_HOST_INFO_RX_OFFSET_MASK |
  2235. ENA_ADMIN_HOST_INFO_INTERRUPT_MODERATION_MASK |
  2236. ENA_ADMIN_HOST_INFO_RX_BUF_MIRRORING_MASK |
  2237. ENA_ADMIN_HOST_INFO_RSS_CONFIGURABLE_FUNCTION_KEY_MASK |
  2238. ENA_ADMIN_HOST_INFO_RX_PAGE_REUSE_MASK;
  2239. rc = ena_com_set_host_attributes(ena_dev);
  2240. if (rc) {
  2241. if (rc == -EOPNOTSUPP)
  2242. dev_warn(dev, "Cannot set host attributes\n");
  2243. else
  2244. dev_err(dev, "Cannot set host attributes\n");
  2245. goto err;
  2246. }
  2247. return;
  2248. err:
  2249. ena_com_delete_host_info(ena_dev);
  2250. }
  2251. static void ena_config_debug_area(struct ena_adapter *adapter)
  2252. {
  2253. u32 debug_area_size;
  2254. int rc, ss_count;
  2255. ss_count = ena_get_sset_count(adapter->netdev, ETH_SS_STATS);
  2256. if (ss_count <= 0) {
  2257. netif_err(adapter, drv, adapter->netdev,
  2258. "SS count is negative\n");
  2259. return;
  2260. }
  2261. /* allocate 32 bytes for each string and 64bit for the value */
  2262. debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
  2263. rc = ena_com_allocate_debug_area(adapter->ena_dev, debug_area_size);
  2264. if (rc) {
  2265. netif_err(adapter, drv, adapter->netdev,
  2266. "Cannot allocate debug area\n");
  2267. return;
  2268. }
  2269. rc = ena_com_set_host_attributes(adapter->ena_dev);
  2270. if (rc) {
  2271. if (rc == -EOPNOTSUPP)
  2272. netif_warn(adapter, drv, adapter->netdev, "Cannot set host attributes\n");
  2273. else
  2274. netif_err(adapter, drv, adapter->netdev,
  2275. "Cannot set host attributes\n");
  2276. goto err;
  2277. }
  2278. return;
  2279. err:
  2280. ena_com_delete_debug_area(adapter->ena_dev);
  2281. }
  2282. static void ena_get_stats64(struct net_device *netdev,
  2283. struct rtnl_link_stats64 *stats)
  2284. {
  2285. struct ena_adapter *adapter = netdev_priv(netdev);
  2286. struct ena_ring *rx_ring, *tx_ring;
  2287. u64 total_xdp_rx_drops = 0;
  2288. unsigned int start;
  2289. u64 rx_drops;
  2290. u64 tx_drops;
  2291. int i;
  2292. if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
  2293. return;
  2294. for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
  2295. u64 bytes, packets, xdp_rx_drops;
  2296. tx_ring = &adapter->tx_ring[i];
  2297. do {
  2298. start = u64_stats_fetch_begin(&tx_ring->syncp);
  2299. packets = tx_ring->tx_stats.cnt;
  2300. bytes = tx_ring->tx_stats.bytes;
  2301. } while (u64_stats_fetch_retry(&tx_ring->syncp, start));
  2302. stats->tx_packets += packets;
  2303. stats->tx_bytes += bytes;
  2304. /* In XDP there isn't an RX queue counterpart */
  2305. if (ENA_IS_XDP_INDEX(adapter, i))
  2306. continue;
  2307. rx_ring = &adapter->rx_ring[i];
  2308. do {
  2309. start = u64_stats_fetch_begin(&rx_ring->syncp);
  2310. packets = rx_ring->rx_stats.cnt;
  2311. bytes = rx_ring->rx_stats.bytes;
  2312. xdp_rx_drops = rx_ring->rx_stats.xdp_drop;
  2313. } while (u64_stats_fetch_retry(&rx_ring->syncp, start));
  2314. stats->rx_packets += packets;
  2315. stats->rx_bytes += bytes;
  2316. total_xdp_rx_drops += xdp_rx_drops;
  2317. }
  2318. do {
  2319. start = u64_stats_fetch_begin(&adapter->syncp);
  2320. rx_drops = adapter->dev_stats.rx_drops;
  2321. tx_drops = adapter->dev_stats.tx_drops;
  2322. } while (u64_stats_fetch_retry(&adapter->syncp, start));
  2323. stats->rx_dropped = rx_drops + total_xdp_rx_drops;
  2324. stats->tx_dropped = tx_drops;
  2325. stats->multicast = 0;
  2326. stats->collisions = 0;
  2327. stats->rx_length_errors = 0;
  2328. stats->rx_crc_errors = 0;
  2329. stats->rx_frame_errors = 0;
  2330. stats->rx_fifo_errors = 0;
  2331. stats->rx_missed_errors = 0;
  2332. stats->tx_window_errors = 0;
  2333. stats->rx_errors = 0;
  2334. stats->tx_errors = 0;
  2335. }
  2336. static const struct net_device_ops ena_netdev_ops = {
  2337. .ndo_open = ena_open,
  2338. .ndo_stop = ena_close,
  2339. .ndo_start_xmit = ena_start_xmit,
  2340. .ndo_get_stats64 = ena_get_stats64,
  2341. .ndo_tx_timeout = ena_tx_timeout,
  2342. .ndo_change_mtu = ena_change_mtu,
  2343. .ndo_validate_addr = eth_validate_addr,
  2344. .ndo_bpf = ena_xdp,
  2345. .ndo_xdp_xmit = ena_xdp_xmit,
  2346. };
  2347. static int ena_calc_io_queue_size(struct ena_adapter *adapter,
  2348. struct ena_com_dev_get_features_ctx *get_feat_ctx)
  2349. {
  2350. struct ena_admin_feature_llq_desc *llq = &get_feat_ctx->llq;
  2351. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2352. u32 tx_queue_size = ENA_DEFAULT_RING_SIZE;
  2353. u32 rx_queue_size = ENA_DEFAULT_RING_SIZE;
  2354. u32 max_tx_queue_size;
  2355. u32 max_rx_queue_size;
  2356. /* If this function is called after driver load, the ring sizes have already
  2357. * been configured. Take it into account when recalculating ring size.
  2358. */
  2359. if (adapter->tx_ring->ring_size)
  2360. tx_queue_size = adapter->tx_ring->ring_size;
  2361. if (adapter->rx_ring->ring_size)
  2362. rx_queue_size = adapter->rx_ring->ring_size;
  2363. if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
  2364. struct ena_admin_queue_ext_feature_fields *max_queue_ext =
  2365. &get_feat_ctx->max_queue_ext.max_queue_ext;
  2366. max_rx_queue_size = min_t(u32, max_queue_ext->max_rx_cq_depth,
  2367. max_queue_ext->max_rx_sq_depth);
  2368. max_tx_queue_size = max_queue_ext->max_tx_cq_depth;
  2369. if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
  2370. max_tx_queue_size = min_t(u32, max_tx_queue_size,
  2371. llq->max_llq_depth);
  2372. else
  2373. max_tx_queue_size = min_t(u32, max_tx_queue_size,
  2374. max_queue_ext->max_tx_sq_depth);
  2375. adapter->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
  2376. max_queue_ext->max_per_packet_tx_descs);
  2377. adapter->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
  2378. max_queue_ext->max_per_packet_rx_descs);
  2379. } else {
  2380. struct ena_admin_queue_feature_desc *max_queues =
  2381. &get_feat_ctx->max_queues;
  2382. max_rx_queue_size = min_t(u32, max_queues->max_cq_depth,
  2383. max_queues->max_sq_depth);
  2384. max_tx_queue_size = max_queues->max_cq_depth;
  2385. if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
  2386. max_tx_queue_size = min_t(u32, max_tx_queue_size,
  2387. llq->max_llq_depth);
  2388. else
  2389. max_tx_queue_size = min_t(u32, max_tx_queue_size,
  2390. max_queues->max_sq_depth);
  2391. adapter->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
  2392. max_queues->max_packet_tx_descs);
  2393. adapter->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
  2394. max_queues->max_packet_rx_descs);
  2395. }
  2396. max_tx_queue_size = rounddown_pow_of_two(max_tx_queue_size);
  2397. max_rx_queue_size = rounddown_pow_of_two(max_rx_queue_size);
  2398. if (max_tx_queue_size < ENA_MIN_RING_SIZE) {
  2399. netdev_err(adapter->netdev, "Device max TX queue size: %d < minimum: %d\n",
  2400. max_tx_queue_size, ENA_MIN_RING_SIZE);
  2401. return -EINVAL;
  2402. }
  2403. if (max_rx_queue_size < ENA_MIN_RING_SIZE) {
  2404. netdev_err(adapter->netdev, "Device max RX queue size: %d < minimum: %d\n",
  2405. max_rx_queue_size, ENA_MIN_RING_SIZE);
  2406. return -EINVAL;
  2407. }
  2408. /* When forcing large headers, we multiply the entry size by 2, and therefore divide
  2409. * the queue size by 2, leaving the amount of memory used by the queues unchanged.
  2410. */
  2411. if (adapter->large_llq_header_enabled) {
  2412. if ((llq->entry_size_ctrl_supported & ENA_ADMIN_LIST_ENTRY_SIZE_256B) &&
  2413. ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
  2414. max_tx_queue_size /= 2;
  2415. dev_info(&adapter->pdev->dev,
  2416. "Forcing large headers and decreasing maximum TX queue size to %d\n",
  2417. max_tx_queue_size);
  2418. } else {
  2419. dev_err(&adapter->pdev->dev,
  2420. "Forcing large headers failed: LLQ is disabled or device does not support large headers\n");
  2421. adapter->large_llq_header_enabled = false;
  2422. }
  2423. }
  2424. tx_queue_size = clamp_val(tx_queue_size, ENA_MIN_RING_SIZE,
  2425. max_tx_queue_size);
  2426. rx_queue_size = clamp_val(rx_queue_size, ENA_MIN_RING_SIZE,
  2427. max_rx_queue_size);
  2428. tx_queue_size = rounddown_pow_of_two(tx_queue_size);
  2429. rx_queue_size = rounddown_pow_of_two(rx_queue_size);
  2430. adapter->max_tx_ring_size = max_tx_queue_size;
  2431. adapter->max_rx_ring_size = max_rx_queue_size;
  2432. adapter->requested_tx_ring_size = tx_queue_size;
  2433. adapter->requested_rx_ring_size = rx_queue_size;
  2434. return 0;
  2435. }
  2436. static int ena_device_validate_params(struct ena_adapter *adapter,
  2437. struct ena_com_dev_get_features_ctx *get_feat_ctx)
  2438. {
  2439. struct net_device *netdev = adapter->netdev;
  2440. int rc;
  2441. rc = ether_addr_equal(get_feat_ctx->dev_attr.mac_addr,
  2442. adapter->mac_addr);
  2443. if (!rc) {
  2444. netif_err(adapter, drv, netdev,
  2445. "Error, mac address are different\n");
  2446. return -EINVAL;
  2447. }
  2448. if (get_feat_ctx->dev_attr.max_mtu < netdev->mtu) {
  2449. netif_err(adapter, drv, netdev,
  2450. "Error, device max mtu is smaller than netdev MTU\n");
  2451. return -EINVAL;
  2452. }
  2453. return 0;
  2454. }
  2455. static void set_default_llq_configurations(struct ena_adapter *adapter,
  2456. struct ena_llq_configurations *llq_config,
  2457. struct ena_admin_feature_llq_desc *llq)
  2458. {
  2459. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2460. llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER;
  2461. llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY;
  2462. llq_config->llq_num_decs_before_header = ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2;
  2463. adapter->large_llq_header_supported =
  2464. !!(ena_dev->supported_features & BIT(ENA_ADMIN_LLQ));
  2465. adapter->large_llq_header_supported &=
  2466. !!(llq->entry_size_ctrl_supported &
  2467. ENA_ADMIN_LIST_ENTRY_SIZE_256B);
  2468. if ((llq->entry_size_ctrl_supported & ENA_ADMIN_LIST_ENTRY_SIZE_256B) &&
  2469. adapter->large_llq_header_enabled) {
  2470. llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_256B;
  2471. llq_config->llq_ring_entry_size_value = 256;
  2472. } else {
  2473. llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_128B;
  2474. llq_config->llq_ring_entry_size_value = 128;
  2475. }
  2476. }
  2477. static int ena_set_queues_placement_policy(struct pci_dev *pdev,
  2478. struct ena_com_dev *ena_dev,
  2479. struct ena_admin_feature_llq_desc *llq,
  2480. struct ena_llq_configurations *llq_default_configurations)
  2481. {
  2482. int rc;
  2483. u32 llq_feature_mask;
  2484. llq_feature_mask = 1 << ENA_ADMIN_LLQ;
  2485. if (!(ena_dev->supported_features & llq_feature_mask)) {
  2486. dev_warn(&pdev->dev,
  2487. "LLQ is not supported Fallback to host mode policy.\n");
  2488. ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
  2489. return 0;
  2490. }
  2491. if (!ena_dev->mem_bar) {
  2492. netdev_err(ena_dev->net_device,
  2493. "LLQ is advertised as supported but device doesn't expose mem bar\n");
  2494. ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
  2495. return 0;
  2496. }
  2497. rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations);
  2498. if (unlikely(rc)) {
  2499. dev_err(&pdev->dev,
  2500. "Failed to configure the device mode. Fallback to host mode policy.\n");
  2501. ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
  2502. }
  2503. return 0;
  2504. }
  2505. static int ena_map_llq_mem_bar(struct pci_dev *pdev, struct ena_com_dev *ena_dev,
  2506. int bars)
  2507. {
  2508. bool has_mem_bar = !!(bars & BIT(ENA_MEM_BAR));
  2509. if (!has_mem_bar)
  2510. return 0;
  2511. ena_dev->mem_bar = devm_ioremap_wc(&pdev->dev,
  2512. pci_resource_start(pdev, ENA_MEM_BAR),
  2513. pci_resource_len(pdev, ENA_MEM_BAR));
  2514. if (!ena_dev->mem_bar)
  2515. return -EFAULT;
  2516. return 0;
  2517. }
  2518. static int ena_device_init(struct ena_adapter *adapter, struct pci_dev *pdev,
  2519. struct ena_com_dev_get_features_ctx *get_feat_ctx,
  2520. bool *wd_state)
  2521. {
  2522. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2523. struct net_device *netdev = adapter->netdev;
  2524. struct ena_llq_configurations llq_config;
  2525. struct device *dev = &pdev->dev;
  2526. bool readless_supported;
  2527. u32 aenq_groups;
  2528. int dma_width;
  2529. int rc;
  2530. rc = ena_com_mmio_reg_read_request_init(ena_dev);
  2531. if (rc) {
  2532. dev_err(dev, "Failed to init mmio read less\n");
  2533. return rc;
  2534. }
  2535. /* The PCIe configuration space revision id indicate if mmio reg
  2536. * read is disabled
  2537. */
  2538. readless_supported = !(pdev->revision & ENA_MMIO_DISABLE_REG_READ);
  2539. ena_com_set_mmio_read_mode(ena_dev, readless_supported);
  2540. rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
  2541. if (rc) {
  2542. dev_err(dev, "Can not reset device\n");
  2543. goto err_mmio_read_less;
  2544. }
  2545. rc = ena_com_validate_version(ena_dev);
  2546. if (rc) {
  2547. dev_err(dev, "Device version is too low\n");
  2548. goto err_mmio_read_less;
  2549. }
  2550. dma_width = ena_com_get_dma_width(ena_dev);
  2551. if (dma_width < 0) {
  2552. dev_err(dev, "Invalid dma width value %d", dma_width);
  2553. rc = dma_width;
  2554. goto err_mmio_read_less;
  2555. }
  2556. rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(dma_width));
  2557. if (rc) {
  2558. dev_err(dev, "dma_set_mask_and_coherent failed %d\n", rc);
  2559. goto err_mmio_read_less;
  2560. }
  2561. /* ENA admin level init */
  2562. rc = ena_com_admin_init(ena_dev, &aenq_handlers);
  2563. if (rc) {
  2564. dev_err(dev,
  2565. "Can not initialize ena admin queue with device\n");
  2566. goto err_mmio_read_less;
  2567. }
  2568. /* To enable the msix interrupts the driver needs to know the number
  2569. * of queues. So the driver uses polling mode to retrieve this
  2570. * information
  2571. */
  2572. ena_com_set_admin_polling_mode(ena_dev, true);
  2573. ena_config_host_info(ena_dev, pdev);
  2574. /* Get Device Attributes*/
  2575. rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
  2576. if (rc) {
  2577. dev_err(dev, "Cannot get attribute for ena device rc=%d\n", rc);
  2578. goto err_admin_init;
  2579. }
  2580. /* Try to turn all the available aenq groups */
  2581. aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
  2582. BIT(ENA_ADMIN_FATAL_ERROR) |
  2583. BIT(ENA_ADMIN_WARNING) |
  2584. BIT(ENA_ADMIN_NOTIFICATION) |
  2585. BIT(ENA_ADMIN_KEEP_ALIVE);
  2586. aenq_groups &= get_feat_ctx->aenq.supported_groups;
  2587. rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
  2588. if (rc) {
  2589. dev_err(dev, "Cannot configure aenq groups rc= %d\n", rc);
  2590. goto err_admin_init;
  2591. }
  2592. *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
  2593. set_default_llq_configurations(adapter, &llq_config, &get_feat_ctx->llq);
  2594. rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx->llq,
  2595. &llq_config);
  2596. if (rc) {
  2597. netdev_err(netdev, "Cannot set queues placement policy rc= %d\n", rc);
  2598. goto err_admin_init;
  2599. }
  2600. rc = ena_calc_io_queue_size(adapter, get_feat_ctx);
  2601. if (unlikely(rc))
  2602. goto err_admin_init;
  2603. return 0;
  2604. err_admin_init:
  2605. ena_com_abort_admin_commands(ena_dev);
  2606. ena_com_wait_for_abort_completion(ena_dev);
  2607. ena_com_delete_host_info(ena_dev);
  2608. ena_com_admin_destroy(ena_dev);
  2609. err_mmio_read_less:
  2610. ena_com_mmio_reg_read_request_destroy(ena_dev);
  2611. return rc;
  2612. }
  2613. static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter)
  2614. {
  2615. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2616. struct device *dev = &adapter->pdev->dev;
  2617. int rc;
  2618. rc = ena_enable_msix(adapter);
  2619. if (rc) {
  2620. dev_err(dev, "Can not reserve msix vectors\n");
  2621. return rc;
  2622. }
  2623. ena_setup_mgmnt_intr(adapter);
  2624. rc = ena_request_mgmnt_irq(adapter);
  2625. if (rc) {
  2626. dev_err(dev, "Can not setup management interrupts\n");
  2627. goto err_disable_msix;
  2628. }
  2629. ena_com_set_admin_polling_mode(ena_dev, false);
  2630. ena_com_admin_aenq_enable(ena_dev);
  2631. return 0;
  2632. err_disable_msix:
  2633. ena_disable_msix(adapter);
  2634. return rc;
  2635. }
  2636. static int ena_destroy_device(struct ena_adapter *adapter, bool graceful)
  2637. {
  2638. struct net_device *netdev = adapter->netdev;
  2639. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2640. bool dev_up;
  2641. int rc = 0;
  2642. if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))
  2643. return 0;
  2644. netif_carrier_off(netdev);
  2645. del_timer_sync(&adapter->timer_service);
  2646. dev_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
  2647. adapter->dev_up_before_reset = dev_up;
  2648. if (!graceful)
  2649. ena_com_set_admin_running_state(ena_dev, false);
  2650. if (dev_up)
  2651. ena_down(adapter);
  2652. /* Stop the device from sending AENQ events (in case reset flag is set
  2653. * and device is up, ena_down() already reset the device.
  2654. */
  2655. if (!(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags) && dev_up))
  2656. rc = ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
  2657. ena_free_mgmnt_irq(adapter);
  2658. ena_disable_msix(adapter);
  2659. ena_com_abort_admin_commands(ena_dev);
  2660. ena_com_wait_for_abort_completion(ena_dev);
  2661. ena_com_admin_destroy(ena_dev);
  2662. ena_com_mmio_reg_read_request_destroy(ena_dev);
  2663. /* return reset reason to default value */
  2664. adapter->reset_reason = ENA_REGS_RESET_NORMAL;
  2665. clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  2666. clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
  2667. return rc;
  2668. }
  2669. static int ena_restore_device(struct ena_adapter *adapter)
  2670. {
  2671. struct ena_com_dev_get_features_ctx get_feat_ctx;
  2672. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2673. struct pci_dev *pdev = adapter->pdev;
  2674. struct ena_ring *txr;
  2675. int rc, count, i;
  2676. bool wd_state;
  2677. set_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
  2678. rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx, &wd_state);
  2679. if (rc) {
  2680. dev_err(&pdev->dev, "Can not initialize device\n");
  2681. goto err;
  2682. }
  2683. adapter->wd_state = wd_state;
  2684. count = adapter->xdp_num_queues + adapter->num_io_queues;
  2685. for (i = 0 ; i < count; i++) {
  2686. txr = &adapter->tx_ring[i];
  2687. txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
  2688. txr->tx_max_header_size = ena_dev->tx_max_header_size;
  2689. }
  2690. rc = ena_device_validate_params(adapter, &get_feat_ctx);
  2691. if (rc) {
  2692. dev_err(&pdev->dev, "Validation of device parameters failed\n");
  2693. goto err_device_destroy;
  2694. }
  2695. rc = ena_enable_msix_and_set_admin_interrupts(adapter);
  2696. if (rc) {
  2697. dev_err(&pdev->dev, "Enable MSI-X failed\n");
  2698. goto err_device_destroy;
  2699. }
  2700. /* If the interface was up before the reset bring it up */
  2701. if (adapter->dev_up_before_reset) {
  2702. rc = ena_up(adapter);
  2703. if (rc) {
  2704. dev_err(&pdev->dev, "Failed to create I/O queues\n");
  2705. goto err_disable_msix;
  2706. }
  2707. }
  2708. set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
  2709. clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
  2710. if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
  2711. netif_carrier_on(adapter->netdev);
  2712. mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
  2713. adapter->last_keep_alive_jiffies = jiffies;
  2714. return rc;
  2715. err_disable_msix:
  2716. ena_free_mgmnt_irq(adapter);
  2717. ena_disable_msix(adapter);
  2718. err_device_destroy:
  2719. ena_com_abort_admin_commands(ena_dev);
  2720. ena_com_wait_for_abort_completion(ena_dev);
  2721. ena_com_admin_destroy(ena_dev);
  2722. ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
  2723. ena_com_mmio_reg_read_request_destroy(ena_dev);
  2724. err:
  2725. clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
  2726. clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
  2727. dev_err(&pdev->dev,
  2728. "Reset attempt failed. Can not reset the device\n");
  2729. return rc;
  2730. }
  2731. static void ena_fw_reset_device(struct work_struct *work)
  2732. {
  2733. int rc = 0;
  2734. struct ena_adapter *adapter =
  2735. container_of(work, struct ena_adapter, reset_task);
  2736. rtnl_lock();
  2737. if (likely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
  2738. rc |= ena_destroy_device(adapter, false);
  2739. rc |= ena_restore_device(adapter);
  2740. adapter->dev_stats.reset_fail += !!rc;
  2741. dev_err(&adapter->pdev->dev, "Device reset completed successfully\n");
  2742. }
  2743. rtnl_unlock();
  2744. }
  2745. static int check_for_rx_interrupt_queue(struct ena_adapter *adapter,
  2746. struct ena_ring *rx_ring)
  2747. {
  2748. struct ena_napi *ena_napi = container_of(rx_ring->napi, struct ena_napi, napi);
  2749. if (likely(READ_ONCE(ena_napi->first_interrupt)))
  2750. return 0;
  2751. if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
  2752. return 0;
  2753. rx_ring->no_interrupt_event_cnt++;
  2754. if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
  2755. netif_err(adapter, rx_err, adapter->netdev,
  2756. "Potential MSIX issue on Rx side Queue = %d. Reset the device\n",
  2757. rx_ring->qid);
  2758. ena_reset_device(adapter, ENA_REGS_RESET_MISS_INTERRUPT);
  2759. return -EIO;
  2760. }
  2761. return 0;
  2762. }
  2763. static int check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
  2764. struct ena_ring *tx_ring)
  2765. {
  2766. struct ena_napi *ena_napi = container_of(tx_ring->napi, struct ena_napi, napi);
  2767. enum ena_regs_reset_reason_types reset_reason = ENA_REGS_RESET_MISS_TX_CMPL;
  2768. unsigned int time_since_last_napi;
  2769. unsigned int missing_tx_comp_to;
  2770. bool is_tx_comp_time_expired;
  2771. struct ena_tx_buffer *tx_buf;
  2772. unsigned long last_jiffies;
  2773. int napi_scheduled;
  2774. u32 missed_tx = 0;
  2775. int i, rc = 0;
  2776. missing_tx_comp_to = jiffies_to_msecs(adapter->missing_tx_completion_to);
  2777. for (i = 0; i < tx_ring->ring_size; i++) {
  2778. tx_buf = &tx_ring->tx_buffer_info[i];
  2779. last_jiffies = tx_buf->last_jiffies;
  2780. if (last_jiffies == 0)
  2781. /* no pending Tx at this location */
  2782. continue;
  2783. is_tx_comp_time_expired = time_is_before_jiffies(last_jiffies +
  2784. 2 * adapter->missing_tx_completion_to);
  2785. if (unlikely(!READ_ONCE(ena_napi->first_interrupt) && is_tx_comp_time_expired)) {
  2786. /* If after graceful period interrupt is still not
  2787. * received, we schedule a reset
  2788. */
  2789. netif_err(adapter, tx_err, adapter->netdev,
  2790. "Potential MSIX issue on Tx side Queue = %d. Reset the device\n",
  2791. tx_ring->qid);
  2792. ena_reset_device(adapter, ENA_REGS_RESET_MISS_INTERRUPT);
  2793. return -EIO;
  2794. }
  2795. is_tx_comp_time_expired = time_is_before_jiffies(last_jiffies +
  2796. adapter->missing_tx_completion_to);
  2797. if (unlikely(is_tx_comp_time_expired)) {
  2798. time_since_last_napi =
  2799. jiffies_to_usecs(jiffies - tx_ring->tx_stats.last_napi_jiffies);
  2800. napi_scheduled = !!(ena_napi->napi.state & NAPIF_STATE_SCHED);
  2801. if (missing_tx_comp_to < time_since_last_napi && napi_scheduled) {
  2802. /* We suspect napi isn't called because the
  2803. * bottom half is not run. Require a bigger
  2804. * timeout for these cases
  2805. */
  2806. if (!time_is_before_jiffies(last_jiffies +
  2807. 2 * adapter->missing_tx_completion_to))
  2808. continue;
  2809. reset_reason = ENA_REGS_RESET_SUSPECTED_POLL_STARVATION;
  2810. }
  2811. missed_tx++;
  2812. if (tx_buf->print_once)
  2813. continue;
  2814. netif_notice(adapter, tx_err, adapter->netdev,
  2815. "TX hasn't completed, qid %d, index %d. %u usecs from last napi execution, napi scheduled: %d\n",
  2816. tx_ring->qid, i, time_since_last_napi, napi_scheduled);
  2817. tx_buf->print_once = 1;
  2818. }
  2819. }
  2820. if (unlikely(missed_tx > adapter->missing_tx_completion_threshold)) {
  2821. netif_err(adapter, tx_err, adapter->netdev,
  2822. "Lost TX completions are above the threshold (%d > %d). Completion transmission timeout: %u.\n",
  2823. missed_tx,
  2824. adapter->missing_tx_completion_threshold,
  2825. missing_tx_comp_to);
  2826. netif_err(adapter, tx_err, adapter->netdev,
  2827. "Resetting the device\n");
  2828. ena_reset_device(adapter, reset_reason);
  2829. rc = -EIO;
  2830. }
  2831. ena_increase_stat(&tx_ring->tx_stats.missed_tx, missed_tx,
  2832. &tx_ring->syncp);
  2833. return rc;
  2834. }
  2835. static void check_for_missing_completions(struct ena_adapter *adapter)
  2836. {
  2837. struct ena_ring *tx_ring;
  2838. struct ena_ring *rx_ring;
  2839. int qid, budget, rc;
  2840. int io_queue_count;
  2841. io_queue_count = adapter->xdp_num_queues + adapter->num_io_queues;
  2842. /* Make sure the driver doesn't turn the device in other process */
  2843. smp_rmb();
  2844. if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
  2845. return;
  2846. if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
  2847. return;
  2848. if (adapter->missing_tx_completion_to == ENA_HW_HINTS_NO_TIMEOUT)
  2849. return;
  2850. budget = min_t(u32, io_queue_count, ENA_MONITORED_TX_QUEUES);
  2851. qid = adapter->last_monitored_tx_qid;
  2852. while (budget) {
  2853. qid = (qid + 1) % io_queue_count;
  2854. tx_ring = &adapter->tx_ring[qid];
  2855. rx_ring = &adapter->rx_ring[qid];
  2856. rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
  2857. if (unlikely(rc))
  2858. return;
  2859. rc = !ENA_IS_XDP_INDEX(adapter, qid) ?
  2860. check_for_rx_interrupt_queue(adapter, rx_ring) : 0;
  2861. if (unlikely(rc))
  2862. return;
  2863. budget--;
  2864. }
  2865. adapter->last_monitored_tx_qid = qid;
  2866. }
  2867. /* trigger napi schedule after 2 consecutive detections */
  2868. #define EMPTY_RX_REFILL 2
  2869. /* For the rare case where the device runs out of Rx descriptors and the
  2870. * napi handler failed to refill new Rx descriptors (due to a lack of memory
  2871. * for example).
  2872. * This case will lead to a deadlock:
  2873. * The device won't send interrupts since all the new Rx packets will be dropped
  2874. * The napi handler won't allocate new Rx descriptors so the device will be
  2875. * able to send new packets.
  2876. *
  2877. * This scenario can happen when the kernel's vm.min_free_kbytes is too small.
  2878. * It is recommended to have at least 512MB, with a minimum of 128MB for
  2879. * constrained environment).
  2880. *
  2881. * When such a situation is detected - Reschedule napi
  2882. */
  2883. static void check_for_empty_rx_ring(struct ena_adapter *adapter)
  2884. {
  2885. struct ena_ring *rx_ring;
  2886. int i, refill_required;
  2887. if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
  2888. return;
  2889. if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
  2890. return;
  2891. for (i = 0; i < adapter->num_io_queues; i++) {
  2892. rx_ring = &adapter->rx_ring[i];
  2893. refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
  2894. if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
  2895. rx_ring->empty_rx_queue++;
  2896. if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) {
  2897. ena_increase_stat(&rx_ring->rx_stats.empty_rx_ring, 1,
  2898. &rx_ring->syncp);
  2899. netif_err(adapter, drv, adapter->netdev,
  2900. "Trigger refill for ring %d\n", i);
  2901. napi_schedule(rx_ring->napi);
  2902. rx_ring->empty_rx_queue = 0;
  2903. }
  2904. } else {
  2905. rx_ring->empty_rx_queue = 0;
  2906. }
  2907. }
  2908. }
  2909. /* Check for keep alive expiration */
  2910. static void check_for_missing_keep_alive(struct ena_adapter *adapter)
  2911. {
  2912. unsigned long keep_alive_expired;
  2913. if (!adapter->wd_state)
  2914. return;
  2915. if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
  2916. return;
  2917. keep_alive_expired = adapter->last_keep_alive_jiffies +
  2918. adapter->keep_alive_timeout;
  2919. if (unlikely(time_is_before_jiffies(keep_alive_expired))) {
  2920. netif_err(adapter, drv, adapter->netdev,
  2921. "Keep alive watchdog timeout.\n");
  2922. ena_increase_stat(&adapter->dev_stats.wd_expired, 1,
  2923. &adapter->syncp);
  2924. ena_reset_device(adapter, ENA_REGS_RESET_KEEP_ALIVE_TO);
  2925. }
  2926. }
  2927. static void check_for_admin_com_state(struct ena_adapter *adapter)
  2928. {
  2929. if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) {
  2930. netif_err(adapter, drv, adapter->netdev,
  2931. "ENA admin queue is not in running state!\n");
  2932. ena_increase_stat(&adapter->dev_stats.admin_q_pause, 1,
  2933. &adapter->syncp);
  2934. ena_reset_device(adapter, ENA_REGS_RESET_ADMIN_TO);
  2935. }
  2936. }
  2937. static void ena_update_hints(struct ena_adapter *adapter,
  2938. struct ena_admin_ena_hw_hints *hints)
  2939. {
  2940. struct net_device *netdev = adapter->netdev;
  2941. if (hints->admin_completion_tx_timeout)
  2942. adapter->ena_dev->admin_queue.completion_timeout =
  2943. hints->admin_completion_tx_timeout * 1000;
  2944. if (hints->mmio_read_timeout)
  2945. /* convert to usec */
  2946. adapter->ena_dev->mmio_read.reg_read_to =
  2947. hints->mmio_read_timeout * 1000;
  2948. if (hints->missed_tx_completion_count_threshold_to_reset)
  2949. adapter->missing_tx_completion_threshold =
  2950. hints->missed_tx_completion_count_threshold_to_reset;
  2951. if (hints->missing_tx_completion_timeout) {
  2952. if (hints->missing_tx_completion_timeout == ENA_HW_HINTS_NO_TIMEOUT)
  2953. adapter->missing_tx_completion_to = ENA_HW_HINTS_NO_TIMEOUT;
  2954. else
  2955. adapter->missing_tx_completion_to =
  2956. msecs_to_jiffies(hints->missing_tx_completion_timeout);
  2957. }
  2958. if (hints->netdev_wd_timeout)
  2959. netdev->watchdog_timeo = msecs_to_jiffies(hints->netdev_wd_timeout);
  2960. if (hints->driver_watchdog_timeout) {
  2961. if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
  2962. adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
  2963. else
  2964. adapter->keep_alive_timeout =
  2965. msecs_to_jiffies(hints->driver_watchdog_timeout);
  2966. }
  2967. }
  2968. static void ena_update_host_info(struct ena_admin_host_info *host_info,
  2969. struct net_device *netdev)
  2970. {
  2971. host_info->supported_network_features[0] =
  2972. netdev->features & GENMASK_ULL(31, 0);
  2973. host_info->supported_network_features[1] =
  2974. (netdev->features & GENMASK_ULL(63, 32)) >> 32;
  2975. }
  2976. static void ena_timer_service(struct timer_list *t)
  2977. {
  2978. struct ena_adapter *adapter = from_timer(adapter, t, timer_service);
  2979. u8 *debug_area = adapter->ena_dev->host_attr.debug_area_virt_addr;
  2980. struct ena_admin_host_info *host_info =
  2981. adapter->ena_dev->host_attr.host_info;
  2982. check_for_missing_keep_alive(adapter);
  2983. check_for_admin_com_state(adapter);
  2984. check_for_missing_completions(adapter);
  2985. check_for_empty_rx_ring(adapter);
  2986. if (debug_area)
  2987. ena_dump_stats_to_buf(adapter, debug_area);
  2988. if (host_info)
  2989. ena_update_host_info(host_info, adapter->netdev);
  2990. if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
  2991. netif_err(adapter, drv, adapter->netdev,
  2992. "Trigger reset is on\n");
  2993. ena_dump_stats_to_dmesg(adapter);
  2994. queue_work(ena_wq, &adapter->reset_task);
  2995. return;
  2996. }
  2997. /* Reset the timer */
  2998. mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
  2999. }
  3000. static u32 ena_calc_max_io_queue_num(struct pci_dev *pdev,
  3001. struct ena_com_dev *ena_dev,
  3002. struct ena_com_dev_get_features_ctx *get_feat_ctx)
  3003. {
  3004. u32 io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues;
  3005. if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
  3006. struct ena_admin_queue_ext_feature_fields *max_queue_ext =
  3007. &get_feat_ctx->max_queue_ext.max_queue_ext;
  3008. io_rx_num = min_t(u32, max_queue_ext->max_rx_sq_num,
  3009. max_queue_ext->max_rx_cq_num);
  3010. io_tx_sq_num = max_queue_ext->max_tx_sq_num;
  3011. io_tx_cq_num = max_queue_ext->max_tx_cq_num;
  3012. } else {
  3013. struct ena_admin_queue_feature_desc *max_queues =
  3014. &get_feat_ctx->max_queues;
  3015. io_tx_sq_num = max_queues->max_sq_num;
  3016. io_tx_cq_num = max_queues->max_cq_num;
  3017. io_rx_num = min_t(u32, io_tx_sq_num, io_tx_cq_num);
  3018. }
  3019. /* In case of LLQ use the llq fields for the tx SQ/CQ */
  3020. if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
  3021. io_tx_sq_num = get_feat_ctx->llq.max_llq_num;
  3022. max_num_io_queues = min_t(u32, num_online_cpus(), ENA_MAX_NUM_IO_QUEUES);
  3023. max_num_io_queues = min_t(u32, max_num_io_queues, io_rx_num);
  3024. max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_sq_num);
  3025. max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_cq_num);
  3026. /* 1 IRQ for mgmnt and 1 IRQs for each IO direction */
  3027. max_num_io_queues = min_t(u32, max_num_io_queues, pci_msix_vec_count(pdev) - 1);
  3028. return max_num_io_queues;
  3029. }
  3030. static void ena_set_dev_offloads(struct ena_com_dev_get_features_ctx *feat,
  3031. struct net_device *netdev)
  3032. {
  3033. netdev_features_t dev_features = 0;
  3034. /* Set offload features */
  3035. if (feat->offload.tx &
  3036. ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)
  3037. dev_features |= NETIF_F_IP_CSUM;
  3038. if (feat->offload.tx &
  3039. ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)
  3040. dev_features |= NETIF_F_IPV6_CSUM;
  3041. if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
  3042. dev_features |= NETIF_F_TSO;
  3043. if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK)
  3044. dev_features |= NETIF_F_TSO6;
  3045. if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_ECN_MASK)
  3046. dev_features |= NETIF_F_TSO_ECN;
  3047. if (feat->offload.rx_supported &
  3048. ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK)
  3049. dev_features |= NETIF_F_RXCSUM;
  3050. if (feat->offload.rx_supported &
  3051. ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK)
  3052. dev_features |= NETIF_F_RXCSUM;
  3053. netdev->features =
  3054. dev_features |
  3055. NETIF_F_SG |
  3056. NETIF_F_RXHASH |
  3057. NETIF_F_HIGHDMA;
  3058. netdev->hw_features |= netdev->features;
  3059. netdev->vlan_features |= netdev->features;
  3060. }
  3061. static void ena_set_conf_feat_params(struct ena_adapter *adapter,
  3062. struct ena_com_dev_get_features_ctx *feat)
  3063. {
  3064. struct net_device *netdev = adapter->netdev;
  3065. /* Copy mac address */
  3066. if (!is_valid_ether_addr(feat->dev_attr.mac_addr)) {
  3067. eth_hw_addr_random(netdev);
  3068. ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
  3069. } else {
  3070. ether_addr_copy(adapter->mac_addr, feat->dev_attr.mac_addr);
  3071. eth_hw_addr_set(netdev, adapter->mac_addr);
  3072. }
  3073. /* Set offload features */
  3074. ena_set_dev_offloads(feat, netdev);
  3075. adapter->max_mtu = feat->dev_attr.max_mtu;
  3076. netdev->max_mtu = adapter->max_mtu;
  3077. netdev->min_mtu = ENA_MIN_MTU;
  3078. }
  3079. static int ena_rss_init_default(struct ena_adapter *adapter)
  3080. {
  3081. struct ena_com_dev *ena_dev = adapter->ena_dev;
  3082. struct device *dev = &adapter->pdev->dev;
  3083. int rc, i;
  3084. u32 val;
  3085. rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
  3086. if (unlikely(rc)) {
  3087. dev_err(dev, "Cannot init indirect table\n");
  3088. goto err_rss_init;
  3089. }
  3090. for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
  3091. val = ethtool_rxfh_indir_default(i, adapter->num_io_queues);
  3092. rc = ena_com_indirect_table_fill_entry(ena_dev, i,
  3093. ENA_IO_RXQ_IDX(val));
  3094. if (unlikely(rc)) {
  3095. dev_err(dev, "Cannot fill indirect table\n");
  3096. goto err_fill_indir;
  3097. }
  3098. }
  3099. rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_TOEPLITZ, NULL, ENA_HASH_KEY_SIZE,
  3100. 0xFFFFFFFF);
  3101. if (unlikely(rc && (rc != -EOPNOTSUPP))) {
  3102. dev_err(dev, "Cannot fill hash function\n");
  3103. goto err_fill_indir;
  3104. }
  3105. rc = ena_com_set_default_hash_ctrl(ena_dev);
  3106. if (unlikely(rc && (rc != -EOPNOTSUPP))) {
  3107. dev_err(dev, "Cannot fill hash control\n");
  3108. goto err_fill_indir;
  3109. }
  3110. return 0;
  3111. err_fill_indir:
  3112. ena_com_rss_destroy(ena_dev);
  3113. err_rss_init:
  3114. return rc;
  3115. }
  3116. static void ena_release_bars(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
  3117. {
  3118. int release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
  3119. pci_release_selected_regions(pdev, release_bars);
  3120. }
  3121. /* ena_probe - Device Initialization Routine
  3122. * @pdev: PCI device information struct
  3123. * @ent: entry in ena_pci_tbl
  3124. *
  3125. * Returns 0 on success, negative on failure
  3126. *
  3127. * ena_probe initializes an adapter identified by a pci_dev structure.
  3128. * The OS initialization, configuring of the adapter private structure,
  3129. * and a hardware reset occur.
  3130. */
  3131. static int ena_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3132. {
  3133. struct ena_com_dev_get_features_ctx get_feat_ctx;
  3134. struct ena_com_dev *ena_dev = NULL;
  3135. struct ena_adapter *adapter;
  3136. struct net_device *netdev;
  3137. static int adapters_found;
  3138. u32 max_num_io_queues;
  3139. bool wd_state;
  3140. int bars, rc;
  3141. dev_dbg(&pdev->dev, "%s\n", __func__);
  3142. rc = pci_enable_device_mem(pdev);
  3143. if (rc) {
  3144. dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
  3145. return rc;
  3146. }
  3147. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(ENA_MAX_PHYS_ADDR_SIZE_BITS));
  3148. if (rc) {
  3149. dev_err(&pdev->dev, "dma_set_mask_and_coherent failed %d\n", rc);
  3150. goto err_disable_device;
  3151. }
  3152. pci_set_master(pdev);
  3153. ena_dev = vzalloc(sizeof(*ena_dev));
  3154. if (!ena_dev) {
  3155. rc = -ENOMEM;
  3156. goto err_disable_device;
  3157. }
  3158. bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
  3159. rc = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
  3160. if (rc) {
  3161. dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
  3162. rc);
  3163. goto err_free_ena_dev;
  3164. }
  3165. ena_dev->reg_bar = devm_ioremap(&pdev->dev,
  3166. pci_resource_start(pdev, ENA_REG_BAR),
  3167. pci_resource_len(pdev, ENA_REG_BAR));
  3168. if (!ena_dev->reg_bar) {
  3169. dev_err(&pdev->dev, "Failed to remap regs bar\n");
  3170. rc = -EFAULT;
  3171. goto err_free_region;
  3172. }
  3173. ena_dev->ena_min_poll_delay_us = ENA_ADMIN_POLL_DELAY_US;
  3174. ena_dev->dmadev = &pdev->dev;
  3175. netdev = alloc_etherdev_mq(sizeof(struct ena_adapter), ENA_MAX_RINGS);
  3176. if (!netdev) {
  3177. dev_err(&pdev->dev, "alloc_etherdev_mq failed\n");
  3178. rc = -ENOMEM;
  3179. goto err_free_region;
  3180. }
  3181. SET_NETDEV_DEV(netdev, &pdev->dev);
  3182. adapter = netdev_priv(netdev);
  3183. adapter->ena_dev = ena_dev;
  3184. adapter->netdev = netdev;
  3185. adapter->pdev = pdev;
  3186. adapter->msg_enable = DEFAULT_MSG_ENABLE;
  3187. ena_dev->net_device = netdev;
  3188. pci_set_drvdata(pdev, adapter);
  3189. rc = ena_com_allocate_customer_metrics_buffer(ena_dev);
  3190. if (rc) {
  3191. netdev_err(netdev, "ena_com_allocate_customer_metrics_buffer failed\n");
  3192. goto err_netdev_destroy;
  3193. }
  3194. rc = ena_map_llq_mem_bar(pdev, ena_dev, bars);
  3195. if (rc) {
  3196. dev_err(&pdev->dev, "ENA LLQ bar mapping failed\n");
  3197. goto err_metrics_destroy;
  3198. }
  3199. rc = ena_device_init(adapter, pdev, &get_feat_ctx, &wd_state);
  3200. if (rc) {
  3201. dev_err(&pdev->dev, "ENA device init failed\n");
  3202. if (rc == -ETIME)
  3203. rc = -EPROBE_DEFER;
  3204. goto err_metrics_destroy;
  3205. }
  3206. /* Initial TX and RX interrupt delay. Assumes 1 usec granularity.
  3207. * Updated during device initialization with the real granularity
  3208. */
  3209. ena_dev->intr_moder_tx_interval = ENA_INTR_INITIAL_TX_INTERVAL_USECS;
  3210. ena_dev->intr_moder_rx_interval = ENA_INTR_INITIAL_RX_INTERVAL_USECS;
  3211. ena_dev->intr_delay_resolution = ENA_DEFAULT_INTR_DELAY_RESOLUTION;
  3212. max_num_io_queues = ena_calc_max_io_queue_num(pdev, ena_dev, &get_feat_ctx);
  3213. if (unlikely(!max_num_io_queues)) {
  3214. rc = -EFAULT;
  3215. goto err_device_destroy;
  3216. }
  3217. ena_set_conf_feat_params(adapter, &get_feat_ctx);
  3218. adapter->reset_reason = ENA_REGS_RESET_NORMAL;
  3219. adapter->num_io_queues = max_num_io_queues;
  3220. adapter->max_num_io_queues = max_num_io_queues;
  3221. adapter->last_monitored_tx_qid = 0;
  3222. adapter->xdp_first_ring = 0;
  3223. adapter->xdp_num_queues = 0;
  3224. adapter->rx_copybreak = ENA_DEFAULT_RX_COPYBREAK;
  3225. if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
  3226. adapter->disable_meta_caching =
  3227. !!(get_feat_ctx.llq.accel_mode.u.get.supported_flags &
  3228. BIT(ENA_ADMIN_DISABLE_META_CACHING));
  3229. adapter->wd_state = wd_state;
  3230. snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d", adapters_found);
  3231. rc = ena_com_init_interrupt_moderation(adapter->ena_dev);
  3232. if (rc) {
  3233. dev_err(&pdev->dev,
  3234. "Failed to query interrupt moderation feature\n");
  3235. goto err_device_destroy;
  3236. }
  3237. ena_init_io_rings(adapter,
  3238. 0,
  3239. adapter->xdp_num_queues +
  3240. adapter->num_io_queues);
  3241. netdev->netdev_ops = &ena_netdev_ops;
  3242. netdev->watchdog_timeo = TX_TIMEOUT;
  3243. ena_set_ethtool_ops(netdev);
  3244. netdev->priv_flags |= IFF_UNICAST_FLT;
  3245. u64_stats_init(&adapter->syncp);
  3246. rc = ena_enable_msix_and_set_admin_interrupts(adapter);
  3247. if (rc) {
  3248. dev_err(&pdev->dev,
  3249. "Failed to enable and set the admin interrupts\n");
  3250. goto err_worker_destroy;
  3251. }
  3252. rc = ena_rss_init_default(adapter);
  3253. if (rc && (rc != -EOPNOTSUPP)) {
  3254. dev_err(&pdev->dev, "Cannot init RSS rc: %d\n", rc);
  3255. goto err_free_msix;
  3256. }
  3257. ena_config_debug_area(adapter);
  3258. if (ena_xdp_legal_queue_count(adapter, adapter->num_io_queues))
  3259. netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
  3260. NETDEV_XDP_ACT_REDIRECT;
  3261. memcpy(adapter->netdev->perm_addr, adapter->mac_addr, netdev->addr_len);
  3262. netif_carrier_off(netdev);
  3263. rc = register_netdev(netdev);
  3264. if (rc) {
  3265. dev_err(&pdev->dev, "Cannot register net device\n");
  3266. goto err_rss;
  3267. }
  3268. INIT_WORK(&adapter->reset_task, ena_fw_reset_device);
  3269. adapter->last_keep_alive_jiffies = jiffies;
  3270. adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT;
  3271. adapter->missing_tx_completion_to = TX_TIMEOUT;
  3272. adapter->missing_tx_completion_threshold = MAX_NUM_OF_TIMEOUTED_PACKETS;
  3273. ena_update_hints(adapter, &get_feat_ctx.hw_hints);
  3274. timer_setup(&adapter->timer_service, ena_timer_service, 0);
  3275. mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
  3276. dev_info(&pdev->dev,
  3277. "%s found at mem %lx, mac addr %pM\n",
  3278. DEVICE_NAME, (long)pci_resource_start(pdev, 0),
  3279. netdev->dev_addr);
  3280. set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
  3281. adapters_found++;
  3282. return 0;
  3283. err_rss:
  3284. ena_com_delete_debug_area(ena_dev);
  3285. ena_com_rss_destroy(ena_dev);
  3286. err_free_msix:
  3287. ena_com_dev_reset(ena_dev, ENA_REGS_RESET_INIT_ERR);
  3288. /* stop submitting admin commands on a device that was reset */
  3289. ena_com_set_admin_running_state(ena_dev, false);
  3290. ena_free_mgmnt_irq(adapter);
  3291. ena_disable_msix(adapter);
  3292. err_worker_destroy:
  3293. del_timer(&adapter->timer_service);
  3294. err_device_destroy:
  3295. ena_com_delete_host_info(ena_dev);
  3296. ena_com_admin_destroy(ena_dev);
  3297. err_metrics_destroy:
  3298. ena_com_delete_customer_metrics_buffer(ena_dev);
  3299. err_netdev_destroy:
  3300. free_netdev(netdev);
  3301. err_free_region:
  3302. ena_release_bars(ena_dev, pdev);
  3303. err_free_ena_dev:
  3304. vfree(ena_dev);
  3305. err_disable_device:
  3306. pci_disable_device(pdev);
  3307. return rc;
  3308. }
  3309. /*****************************************************************************/
  3310. /* __ena_shutoff - Helper used in both PCI remove/shutdown routines
  3311. * @pdev: PCI device information struct
  3312. * @shutdown: Is it a shutdown operation? If false, means it is a removal
  3313. *
  3314. * __ena_shutoff is a helper routine that does the real work on shutdown and
  3315. * removal paths; the difference between those paths is with regards to whether
  3316. * dettach or unregister the netdevice.
  3317. */
  3318. static void __ena_shutoff(struct pci_dev *pdev, bool shutdown)
  3319. {
  3320. struct ena_adapter *adapter = pci_get_drvdata(pdev);
  3321. struct ena_com_dev *ena_dev;
  3322. struct net_device *netdev;
  3323. ena_dev = adapter->ena_dev;
  3324. netdev = adapter->netdev;
  3325. #ifdef CONFIG_RFS_ACCEL
  3326. if ((adapter->msix_vecs >= 1) && (netdev->rx_cpu_rmap)) {
  3327. free_irq_cpu_rmap(netdev->rx_cpu_rmap);
  3328. netdev->rx_cpu_rmap = NULL;
  3329. }
  3330. #endif /* CONFIG_RFS_ACCEL */
  3331. /* Make sure timer and reset routine won't be called after
  3332. * freeing device resources.
  3333. */
  3334. del_timer_sync(&adapter->timer_service);
  3335. cancel_work_sync(&adapter->reset_task);
  3336. rtnl_lock(); /* lock released inside the below if-else block */
  3337. adapter->reset_reason = ENA_REGS_RESET_SHUTDOWN;
  3338. ena_destroy_device(adapter, true);
  3339. if (shutdown) {
  3340. netif_device_detach(netdev);
  3341. dev_close(netdev);
  3342. rtnl_unlock();
  3343. } else {
  3344. rtnl_unlock();
  3345. unregister_netdev(netdev);
  3346. free_netdev(netdev);
  3347. }
  3348. ena_com_rss_destroy(ena_dev);
  3349. ena_com_delete_debug_area(ena_dev);
  3350. ena_com_delete_host_info(ena_dev);
  3351. ena_com_delete_customer_metrics_buffer(ena_dev);
  3352. ena_release_bars(ena_dev, pdev);
  3353. pci_disable_device(pdev);
  3354. vfree(ena_dev);
  3355. }
  3356. /* ena_remove - Device Removal Routine
  3357. * @pdev: PCI device information struct
  3358. *
  3359. * ena_remove is called by the PCI subsystem to alert the driver
  3360. * that it should release a PCI device.
  3361. */
  3362. static void ena_remove(struct pci_dev *pdev)
  3363. {
  3364. __ena_shutoff(pdev, false);
  3365. }
  3366. /* ena_shutdown - Device Shutdown Routine
  3367. * @pdev: PCI device information struct
  3368. *
  3369. * ena_shutdown is called by the PCI subsystem to alert the driver that
  3370. * a shutdown/reboot (or kexec) is happening and device must be disabled.
  3371. */
  3372. static void ena_shutdown(struct pci_dev *pdev)
  3373. {
  3374. __ena_shutoff(pdev, true);
  3375. }
  3376. /* ena_suspend - PM suspend callback
  3377. * @dev_d: Device information struct
  3378. */
  3379. static int __maybe_unused ena_suspend(struct device *dev_d)
  3380. {
  3381. struct pci_dev *pdev = to_pci_dev(dev_d);
  3382. struct ena_adapter *adapter = pci_get_drvdata(pdev);
  3383. ena_increase_stat(&adapter->dev_stats.suspend, 1, &adapter->syncp);
  3384. rtnl_lock();
  3385. if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
  3386. dev_err(&pdev->dev,
  3387. "Ignoring device reset request as the device is being suspended\n");
  3388. clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  3389. }
  3390. ena_destroy_device(adapter, true);
  3391. rtnl_unlock();
  3392. return 0;
  3393. }
  3394. /* ena_resume - PM resume callback
  3395. * @dev_d: Device information struct
  3396. */
  3397. static int __maybe_unused ena_resume(struct device *dev_d)
  3398. {
  3399. struct ena_adapter *adapter = dev_get_drvdata(dev_d);
  3400. int rc;
  3401. ena_increase_stat(&adapter->dev_stats.resume, 1, &adapter->syncp);
  3402. rtnl_lock();
  3403. rc = ena_restore_device(adapter);
  3404. rtnl_unlock();
  3405. return rc;
  3406. }
  3407. static SIMPLE_DEV_PM_OPS(ena_pm_ops, ena_suspend, ena_resume);
  3408. static struct pci_driver ena_pci_driver = {
  3409. .name = DRV_MODULE_NAME,
  3410. .id_table = ena_pci_tbl,
  3411. .probe = ena_probe,
  3412. .remove = ena_remove,
  3413. .shutdown = ena_shutdown,
  3414. .driver.pm = &ena_pm_ops,
  3415. .sriov_configure = pci_sriov_configure_simple,
  3416. };
  3417. static int __init ena_init(void)
  3418. {
  3419. int ret;
  3420. ena_wq = create_singlethread_workqueue(DRV_MODULE_NAME);
  3421. if (!ena_wq) {
  3422. pr_err("Failed to create workqueue\n");
  3423. return -ENOMEM;
  3424. }
  3425. ret = pci_register_driver(&ena_pci_driver);
  3426. if (ret)
  3427. destroy_workqueue(ena_wq);
  3428. return ret;
  3429. }
  3430. static void __exit ena_cleanup(void)
  3431. {
  3432. pci_unregister_driver(&ena_pci_driver);
  3433. if (ena_wq) {
  3434. destroy_workqueue(ena_wq);
  3435. ena_wq = NULL;
  3436. }
  3437. }
  3438. /******************************************************************************
  3439. ******************************** AENQ Handlers *******************************
  3440. *****************************************************************************/
  3441. /* ena_update_on_link_change:
  3442. * Notify the network interface about the change in link status
  3443. */
  3444. static void ena_update_on_link_change(void *adapter_data,
  3445. struct ena_admin_aenq_entry *aenq_e)
  3446. {
  3447. struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
  3448. struct ena_admin_aenq_link_change_desc *aenq_desc =
  3449. (struct ena_admin_aenq_link_change_desc *)aenq_e;
  3450. int status = aenq_desc->flags &
  3451. ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
  3452. if (status) {
  3453. netif_dbg(adapter, ifup, adapter->netdev, "%s\n", __func__);
  3454. set_bit(ENA_FLAG_LINK_UP, &adapter->flags);
  3455. if (!test_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags))
  3456. netif_carrier_on(adapter->netdev);
  3457. } else {
  3458. clear_bit(ENA_FLAG_LINK_UP, &adapter->flags);
  3459. netif_carrier_off(adapter->netdev);
  3460. }
  3461. }
  3462. static void ena_keep_alive_wd(void *adapter_data,
  3463. struct ena_admin_aenq_entry *aenq_e)
  3464. {
  3465. struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
  3466. struct ena_admin_aenq_keep_alive_desc *desc;
  3467. u64 rx_drops;
  3468. u64 tx_drops;
  3469. desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
  3470. adapter->last_keep_alive_jiffies = jiffies;
  3471. rx_drops = ((u64)desc->rx_drops_high << 32) | desc->rx_drops_low;
  3472. tx_drops = ((u64)desc->tx_drops_high << 32) | desc->tx_drops_low;
  3473. u64_stats_update_begin(&adapter->syncp);
  3474. /* These stats are accumulated by the device, so the counters indicate
  3475. * all drops since last reset.
  3476. */
  3477. adapter->dev_stats.rx_drops = rx_drops;
  3478. adapter->dev_stats.tx_drops = tx_drops;
  3479. u64_stats_update_end(&adapter->syncp);
  3480. }
  3481. static void ena_notification(void *adapter_data,
  3482. struct ena_admin_aenq_entry *aenq_e)
  3483. {
  3484. struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
  3485. struct ena_admin_ena_hw_hints *hints;
  3486. WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
  3487. "Invalid group(%x) expected %x\n",
  3488. aenq_e->aenq_common_desc.group,
  3489. ENA_ADMIN_NOTIFICATION);
  3490. switch (aenq_e->aenq_common_desc.syndrome) {
  3491. case ENA_ADMIN_UPDATE_HINTS:
  3492. hints = (struct ena_admin_ena_hw_hints *)
  3493. (&aenq_e->inline_data_w4);
  3494. ena_update_hints(adapter, hints);
  3495. break;
  3496. default:
  3497. netif_err(adapter, drv, adapter->netdev,
  3498. "Invalid aenq notification link state %d\n",
  3499. aenq_e->aenq_common_desc.syndrome);
  3500. }
  3501. }
  3502. /* This handler will called for unknown event group or unimplemented handlers*/
  3503. static void unimplemented_aenq_handler(void *data,
  3504. struct ena_admin_aenq_entry *aenq_e)
  3505. {
  3506. struct ena_adapter *adapter = (struct ena_adapter *)data;
  3507. netif_err(adapter, drv, adapter->netdev,
  3508. "Unknown event was received or event with unimplemented handler\n");
  3509. }
  3510. static struct ena_aenq_handlers aenq_handlers = {
  3511. .handlers = {
  3512. [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
  3513. [ENA_ADMIN_NOTIFICATION] = ena_notification,
  3514. [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
  3515. },
  3516. .unimplemented_handler = unimplemented_aenq_handler
  3517. };
  3518. module_init(ena_init);
  3519. module_exit(ena_cleanup);