nfp_net_common.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817
  1. /*
  2. * Copyright (C) 2015-2017 Netronome Systems, Inc.
  3. *
  4. * This software is dual licensed under the GNU General License Version 2,
  5. * June 1991 as shown in the file COPYING in the top-level directory of this
  6. * source tree or the BSD 2-Clause License provided below. You have the
  7. * option to license this software under the complete terms of either license.
  8. *
  9. * The BSD 2-Clause License:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /*
  34. * nfp_net_common.c
  35. * Netronome network device driver: Common functions between PF and VF
  36. * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
  37. * Jason McMullan <jason.mcmullan@netronome.com>
  38. * Rolf Neugebauer <rolf.neugebauer@netronome.com>
  39. * Brad Petrus <brad.petrus@netronome.com>
  40. * Chris Telfer <chris.telfer@netronome.com>
  41. */
  42. #include <linux/bitfield.h>
  43. #include <linux/bpf.h>
  44. #include <linux/bpf_trace.h>
  45. #include <linux/module.h>
  46. #include <linux/kernel.h>
  47. #include <linux/init.h>
  48. #include <linux/fs.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/ip.h>
  53. #include <linux/ipv6.h>
  54. #include <linux/page_ref.h>
  55. #include <linux/pci.h>
  56. #include <linux/pci_regs.h>
  57. #include <linux/msi.h>
  58. #include <linux/ethtool.h>
  59. #include <linux/log2.h>
  60. #include <linux/if_vlan.h>
  61. #include <linux/random.h>
  62. #include <linux/vmalloc.h>
  63. #include <linux/ktime.h>
  64. #include <net/switchdev.h>
  65. #include <net/vxlan.h>
  66. #include "nfpcore/nfp_nsp.h"
  67. #include "nfp_app.h"
  68. #include "nfp_net_ctrl.h"
  69. #include "nfp_net.h"
  70. #include "nfp_port.h"
  71. /**
  72. * nfp_net_get_fw_version() - Read and parse the FW version
  73. * @fw_ver: Output fw_version structure to read to
  74. * @ctrl_bar: Mapped address of the control BAR
  75. */
  76. void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
  77. void __iomem *ctrl_bar)
  78. {
  79. u32 reg;
  80. reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
  81. put_unaligned_le32(reg, fw_ver);
  82. }
  83. static dma_addr_t nfp_net_dma_map_rx(struct nfp_net_dp *dp, void *frag)
  84. {
  85. return dma_map_single_attrs(dp->dev, frag + NFP_NET_RX_BUF_HEADROOM,
  86. dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
  87. dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
  88. }
  89. static void
  90. nfp_net_dma_sync_dev_rx(const struct nfp_net_dp *dp, dma_addr_t dma_addr)
  91. {
  92. dma_sync_single_for_device(dp->dev, dma_addr,
  93. dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
  94. dp->rx_dma_dir);
  95. }
  96. static void nfp_net_dma_unmap_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr)
  97. {
  98. dma_unmap_single_attrs(dp->dev, dma_addr,
  99. dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
  100. dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
  101. }
  102. static void nfp_net_dma_sync_cpu_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr,
  103. unsigned int len)
  104. {
  105. dma_sync_single_for_cpu(dp->dev, dma_addr - NFP_NET_RX_BUF_HEADROOM,
  106. len, dp->rx_dma_dir);
  107. }
  108. /* Firmware reconfig
  109. *
  110. * Firmware reconfig may take a while so we have two versions of it -
  111. * synchronous and asynchronous (posted). All synchronous callers are holding
  112. * RTNL so we don't have to worry about serializing them.
  113. */
  114. static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
  115. {
  116. nn_writel(nn, NFP_NET_CFG_UPDATE, update);
  117. /* ensure update is written before pinging HW */
  118. nn_pci_flush(nn);
  119. nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
  120. }
  121. /* Pass 0 as update to run posted reconfigs. */
  122. static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
  123. {
  124. update |= nn->reconfig_posted;
  125. nn->reconfig_posted = 0;
  126. nfp_net_reconfig_start(nn, update);
  127. nn->reconfig_timer_active = true;
  128. mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
  129. }
  130. static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
  131. {
  132. u32 reg;
  133. reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
  134. if (reg == 0)
  135. return true;
  136. if (reg & NFP_NET_CFG_UPDATE_ERR) {
  137. nn_err(nn, "Reconfig error: 0x%08x\n", reg);
  138. return true;
  139. } else if (last_check) {
  140. nn_err(nn, "Reconfig timeout: 0x%08x\n", reg);
  141. return true;
  142. }
  143. return false;
  144. }
  145. static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
  146. {
  147. bool timed_out = false;
  148. /* Poll update field, waiting for NFP to ack the config */
  149. while (!nfp_net_reconfig_check_done(nn, timed_out)) {
  150. msleep(1);
  151. timed_out = time_is_before_eq_jiffies(deadline);
  152. }
  153. if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
  154. return -EIO;
  155. return timed_out ? -EIO : 0;
  156. }
  157. static void nfp_net_reconfig_timer(unsigned long data)
  158. {
  159. struct nfp_net *nn = (void *)data;
  160. spin_lock_bh(&nn->reconfig_lock);
  161. nn->reconfig_timer_active = false;
  162. /* If sync caller is present it will take over from us */
  163. if (nn->reconfig_sync_present)
  164. goto done;
  165. /* Read reconfig status and report errors */
  166. nfp_net_reconfig_check_done(nn, true);
  167. if (nn->reconfig_posted)
  168. nfp_net_reconfig_start_async(nn, 0);
  169. done:
  170. spin_unlock_bh(&nn->reconfig_lock);
  171. }
  172. /**
  173. * nfp_net_reconfig_post() - Post async reconfig request
  174. * @nn: NFP Net device to reconfigure
  175. * @update: The value for the update field in the BAR config
  176. *
  177. * Record FW reconfiguration request. Reconfiguration will be kicked off
  178. * whenever reconfiguration machinery is idle. Multiple requests can be
  179. * merged together!
  180. */
  181. static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
  182. {
  183. spin_lock_bh(&nn->reconfig_lock);
  184. /* Sync caller will kick off async reconf when it's done, just post */
  185. if (nn->reconfig_sync_present) {
  186. nn->reconfig_posted |= update;
  187. goto done;
  188. }
  189. /* Opportunistically check if the previous command is done */
  190. if (!nn->reconfig_timer_active ||
  191. nfp_net_reconfig_check_done(nn, false))
  192. nfp_net_reconfig_start_async(nn, update);
  193. else
  194. nn->reconfig_posted |= update;
  195. done:
  196. spin_unlock_bh(&nn->reconfig_lock);
  197. }
  198. /**
  199. * nfp_net_reconfig() - Reconfigure the firmware
  200. * @nn: NFP Net device to reconfigure
  201. * @update: The value for the update field in the BAR config
  202. *
  203. * Write the update word to the BAR and ping the reconfig queue. The
  204. * poll until the firmware has acknowledged the update by zeroing the
  205. * update word.
  206. *
  207. * Return: Negative errno on error, 0 on success
  208. */
  209. int nfp_net_reconfig(struct nfp_net *nn, u32 update)
  210. {
  211. bool cancelled_timer = false;
  212. u32 pre_posted_requests;
  213. int ret;
  214. spin_lock_bh(&nn->reconfig_lock);
  215. nn->reconfig_sync_present = true;
  216. if (nn->reconfig_timer_active) {
  217. del_timer(&nn->reconfig_timer);
  218. nn->reconfig_timer_active = false;
  219. cancelled_timer = true;
  220. }
  221. pre_posted_requests = nn->reconfig_posted;
  222. nn->reconfig_posted = 0;
  223. spin_unlock_bh(&nn->reconfig_lock);
  224. if (cancelled_timer)
  225. nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
  226. /* Run the posted reconfigs which were issued before we started */
  227. if (pre_posted_requests) {
  228. nfp_net_reconfig_start(nn, pre_posted_requests);
  229. nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
  230. }
  231. nfp_net_reconfig_start(nn, update);
  232. ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
  233. spin_lock_bh(&nn->reconfig_lock);
  234. if (nn->reconfig_posted)
  235. nfp_net_reconfig_start_async(nn, 0);
  236. nn->reconfig_sync_present = false;
  237. spin_unlock_bh(&nn->reconfig_lock);
  238. return ret;
  239. }
  240. /**
  241. * nfp_net_reconfig_mbox() - Reconfigure the firmware via the mailbox
  242. * @nn: NFP Net device to reconfigure
  243. * @mbox_cmd: The value for the mailbox command
  244. *
  245. * Helper function for mailbox updates
  246. *
  247. * Return: Negative errno on error, 0 on success
  248. */
  249. static int nfp_net_reconfig_mbox(struct nfp_net *nn, u32 mbox_cmd)
  250. {
  251. int ret;
  252. nn_writeq(nn, NFP_NET_CFG_MBOX_CMD, mbox_cmd);
  253. ret = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MBOX);
  254. if (ret) {
  255. nn_err(nn, "Mailbox update error\n");
  256. return ret;
  257. }
  258. return -nn_readl(nn, NFP_NET_CFG_MBOX_RET);
  259. }
  260. /* Interrupt configuration and handling
  261. */
  262. /**
  263. * nfp_net_irq_unmask() - Unmask automasked interrupt
  264. * @nn: NFP Network structure
  265. * @entry_nr: MSI-X table entry
  266. *
  267. * Clear the ICR for the IRQ entry.
  268. */
  269. static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
  270. {
  271. nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
  272. nn_pci_flush(nn);
  273. }
  274. /**
  275. * nfp_net_irqs_alloc() - allocates MSI-X irqs
  276. * @pdev: PCI device structure
  277. * @irq_entries: Array to be initialized and used to hold the irq entries
  278. * @min_irqs: Minimal acceptable number of interrupts
  279. * @wanted_irqs: Target number of interrupts to allocate
  280. *
  281. * Return: Number of irqs obtained or 0 on error.
  282. */
  283. unsigned int
  284. nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
  285. unsigned int min_irqs, unsigned int wanted_irqs)
  286. {
  287. unsigned int i;
  288. int got_irqs;
  289. for (i = 0; i < wanted_irqs; i++)
  290. irq_entries[i].entry = i;
  291. got_irqs = pci_enable_msix_range(pdev, irq_entries,
  292. min_irqs, wanted_irqs);
  293. if (got_irqs < 0) {
  294. dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
  295. min_irqs, wanted_irqs, got_irqs);
  296. return 0;
  297. }
  298. if (got_irqs < wanted_irqs)
  299. dev_warn(&pdev->dev, "Unable to allocate %d IRQs got only %d\n",
  300. wanted_irqs, got_irqs);
  301. return got_irqs;
  302. }
  303. /**
  304. * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
  305. * @nn: NFP Network structure
  306. * @irq_entries: Table of allocated interrupts
  307. * @n: Size of @irq_entries (number of entries to grab)
  308. *
  309. * After interrupts are allocated with nfp_net_irqs_alloc() this function
  310. * should be called to assign them to a specific netdev (port).
  311. */
  312. void
  313. nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
  314. unsigned int n)
  315. {
  316. struct nfp_net_dp *dp = &nn->dp;
  317. nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
  318. dp->num_r_vecs = nn->max_r_vecs;
  319. memcpy(nn->irq_entries, irq_entries, sizeof(*irq_entries) * n);
  320. if (dp->num_rx_rings > dp->num_r_vecs ||
  321. dp->num_tx_rings > dp->num_r_vecs)
  322. dev_warn(nn->dp.dev, "More rings (%d,%d) than vectors (%d).\n",
  323. dp->num_rx_rings, dp->num_tx_rings,
  324. dp->num_r_vecs);
  325. dp->num_rx_rings = min(dp->num_r_vecs, dp->num_rx_rings);
  326. dp->num_tx_rings = min(dp->num_r_vecs, dp->num_tx_rings);
  327. dp->num_stack_tx_rings = dp->num_tx_rings;
  328. }
  329. /**
  330. * nfp_net_irqs_disable() - Disable interrupts
  331. * @pdev: PCI device structure
  332. *
  333. * Undoes what @nfp_net_irqs_alloc() does.
  334. */
  335. void nfp_net_irqs_disable(struct pci_dev *pdev)
  336. {
  337. pci_disable_msix(pdev);
  338. }
  339. /**
  340. * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
  341. * @irq: Interrupt
  342. * @data: Opaque data structure
  343. *
  344. * Return: Indicate if the interrupt has been handled.
  345. */
  346. static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
  347. {
  348. struct nfp_net_r_vector *r_vec = data;
  349. napi_schedule_irqoff(&r_vec->napi);
  350. /* The FW auto-masks any interrupt, either via the MASK bit in
  351. * the MSI-X table or via the per entry ICR field. So there
  352. * is no need to disable interrupts here.
  353. */
  354. return IRQ_HANDLED;
  355. }
  356. static irqreturn_t nfp_ctrl_irq_rxtx(int irq, void *data)
  357. {
  358. struct nfp_net_r_vector *r_vec = data;
  359. tasklet_schedule(&r_vec->tasklet);
  360. return IRQ_HANDLED;
  361. }
  362. /**
  363. * nfp_net_read_link_status() - Reread link status from control BAR
  364. * @nn: NFP Network structure
  365. */
  366. static void nfp_net_read_link_status(struct nfp_net *nn)
  367. {
  368. unsigned long flags;
  369. bool link_up;
  370. u32 sts;
  371. spin_lock_irqsave(&nn->link_status_lock, flags);
  372. sts = nn_readl(nn, NFP_NET_CFG_STS);
  373. link_up = !!(sts & NFP_NET_CFG_STS_LINK);
  374. if (nn->link_up == link_up)
  375. goto out;
  376. nn->link_up = link_up;
  377. if (nn->port)
  378. set_bit(NFP_PORT_CHANGED, &nn->port->flags);
  379. if (nn->link_up) {
  380. netif_carrier_on(nn->dp.netdev);
  381. netdev_info(nn->dp.netdev, "NIC Link is Up\n");
  382. } else {
  383. netif_carrier_off(nn->dp.netdev);
  384. netdev_info(nn->dp.netdev, "NIC Link is Down\n");
  385. }
  386. out:
  387. spin_unlock_irqrestore(&nn->link_status_lock, flags);
  388. }
  389. /**
  390. * nfp_net_irq_lsc() - Interrupt service routine for link state changes
  391. * @irq: Interrupt
  392. * @data: Opaque data structure
  393. *
  394. * Return: Indicate if the interrupt has been handled.
  395. */
  396. static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
  397. {
  398. struct nfp_net *nn = data;
  399. struct msix_entry *entry;
  400. entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
  401. nfp_net_read_link_status(nn);
  402. nfp_net_irq_unmask(nn, entry->entry);
  403. return IRQ_HANDLED;
  404. }
  405. /**
  406. * nfp_net_irq_exn() - Interrupt service routine for exceptions
  407. * @irq: Interrupt
  408. * @data: Opaque data structure
  409. *
  410. * Return: Indicate if the interrupt has been handled.
  411. */
  412. static irqreturn_t nfp_net_irq_exn(int irq, void *data)
  413. {
  414. struct nfp_net *nn = data;
  415. nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
  416. /* XXX TO BE IMPLEMENTED */
  417. return IRQ_HANDLED;
  418. }
  419. /**
  420. * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
  421. * @tx_ring: TX ring structure
  422. * @r_vec: IRQ vector servicing this ring
  423. * @idx: Ring index
  424. * @is_xdp: Is this an XDP TX ring?
  425. */
  426. static void
  427. nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
  428. struct nfp_net_r_vector *r_vec, unsigned int idx,
  429. bool is_xdp)
  430. {
  431. struct nfp_net *nn = r_vec->nfp_net;
  432. tx_ring->idx = idx;
  433. tx_ring->r_vec = r_vec;
  434. tx_ring->is_xdp = is_xdp;
  435. u64_stats_init(&tx_ring->r_vec->tx_sync);
  436. tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
  437. tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
  438. }
  439. /**
  440. * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
  441. * @rx_ring: RX ring structure
  442. * @r_vec: IRQ vector servicing this ring
  443. * @idx: Ring index
  444. */
  445. static void
  446. nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
  447. struct nfp_net_r_vector *r_vec, unsigned int idx)
  448. {
  449. struct nfp_net *nn = r_vec->nfp_net;
  450. rx_ring->idx = idx;
  451. rx_ring->r_vec = r_vec;
  452. u64_stats_init(&rx_ring->r_vec->rx_sync);
  453. rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
  454. rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
  455. }
  456. /**
  457. * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
  458. * @nn: NFP Network structure
  459. * @ctrl_offset: Control BAR offset where IRQ configuration should be written
  460. * @format: printf-style format to construct the interrupt name
  461. * @name: Pointer to allocated space for interrupt name
  462. * @name_sz: Size of space for interrupt name
  463. * @vector_idx: Index of MSI-X vector used for this interrupt
  464. * @handler: IRQ handler to register for this interrupt
  465. */
  466. static int
  467. nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
  468. const char *format, char *name, size_t name_sz,
  469. unsigned int vector_idx, irq_handler_t handler)
  470. {
  471. struct msix_entry *entry;
  472. int err;
  473. entry = &nn->irq_entries[vector_idx];
  474. snprintf(name, name_sz, format, nfp_net_name(nn));
  475. err = request_irq(entry->vector, handler, 0, name, nn);
  476. if (err) {
  477. nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
  478. entry->vector, err);
  479. return err;
  480. }
  481. nn_writeb(nn, ctrl_offset, entry->entry);
  482. return 0;
  483. }
  484. /**
  485. * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
  486. * @nn: NFP Network structure
  487. * @ctrl_offset: Control BAR offset where IRQ configuration should be written
  488. * @vector_idx: Index of MSI-X vector used for this interrupt
  489. */
  490. static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
  491. unsigned int vector_idx)
  492. {
  493. nn_writeb(nn, ctrl_offset, 0xff);
  494. free_irq(nn->irq_entries[vector_idx].vector, nn);
  495. }
  496. /* Transmit
  497. *
  498. * One queue controller peripheral queue is used for transmit. The
  499. * driver en-queues packets for transmit by advancing the write
  500. * pointer. The device indicates that packets have transmitted by
  501. * advancing the read pointer. The driver maintains a local copy of
  502. * the read and write pointer in @struct nfp_net_tx_ring. The driver
  503. * keeps @wr_p in sync with the queue controller write pointer and can
  504. * determine how many packets have been transmitted by comparing its
  505. * copy of the read pointer @rd_p with the read pointer maintained by
  506. * the queue controller peripheral.
  507. */
  508. /**
  509. * nfp_net_tx_full() - Check if the TX ring is full
  510. * @tx_ring: TX ring to check
  511. * @dcnt: Number of descriptors that need to be enqueued (must be >= 1)
  512. *
  513. * This function checks, based on the *host copy* of read/write
  514. * pointer if a given TX ring is full. The real TX queue may have
  515. * some newly made available slots.
  516. *
  517. * Return: True if the ring is full.
  518. */
  519. static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
  520. {
  521. return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
  522. }
  523. /* Wrappers for deciding when to stop and restart TX queues */
  524. static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
  525. {
  526. return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
  527. }
  528. static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
  529. {
  530. return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
  531. }
  532. /**
  533. * nfp_net_tx_ring_stop() - stop tx ring
  534. * @nd_q: netdev queue
  535. * @tx_ring: driver tx queue structure
  536. *
  537. * Safely stop TX ring. Remember that while we are running .start_xmit()
  538. * someone else may be cleaning the TX ring completions so we need to be
  539. * extra careful here.
  540. */
  541. static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
  542. struct nfp_net_tx_ring *tx_ring)
  543. {
  544. netif_tx_stop_queue(nd_q);
  545. /* We can race with the TX completion out of NAPI so recheck */
  546. smp_mb();
  547. if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
  548. netif_tx_start_queue(nd_q);
  549. }
  550. /**
  551. * nfp_net_tx_tso() - Set up Tx descriptor for LSO
  552. * @r_vec: per-ring structure
  553. * @txbuf: Pointer to driver soft TX descriptor
  554. * @txd: Pointer to HW TX descriptor
  555. * @skb: Pointer to SKB
  556. *
  557. * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
  558. * Return error on packet header greater than maximum supported LSO header size.
  559. */
  560. static void nfp_net_tx_tso(struct nfp_net_r_vector *r_vec,
  561. struct nfp_net_tx_buf *txbuf,
  562. struct nfp_net_tx_desc *txd, struct sk_buff *skb)
  563. {
  564. u32 hdrlen;
  565. u16 mss;
  566. if (!skb_is_gso(skb))
  567. return;
  568. if (!skb->encapsulation) {
  569. txd->l3_offset = skb_network_offset(skb);
  570. txd->l4_offset = skb_transport_offset(skb);
  571. hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  572. } else {
  573. txd->l3_offset = skb_inner_network_offset(skb);
  574. txd->l4_offset = skb_inner_transport_offset(skb);
  575. hdrlen = skb_inner_transport_header(skb) - skb->data +
  576. inner_tcp_hdrlen(skb);
  577. }
  578. txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
  579. txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
  580. mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
  581. txd->lso_hdrlen = hdrlen;
  582. txd->mss = cpu_to_le16(mss);
  583. txd->flags |= PCIE_DESC_TX_LSO;
  584. u64_stats_update_begin(&r_vec->tx_sync);
  585. r_vec->tx_lso++;
  586. u64_stats_update_end(&r_vec->tx_sync);
  587. }
  588. /**
  589. * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
  590. * @dp: NFP Net data path struct
  591. * @r_vec: per-ring structure
  592. * @txbuf: Pointer to driver soft TX descriptor
  593. * @txd: Pointer to TX descriptor
  594. * @skb: Pointer to SKB
  595. *
  596. * This function sets the TX checksum flags in the TX descriptor based
  597. * on the configuration and the protocol of the packet to be transmitted.
  598. */
  599. static void nfp_net_tx_csum(struct nfp_net_dp *dp,
  600. struct nfp_net_r_vector *r_vec,
  601. struct nfp_net_tx_buf *txbuf,
  602. struct nfp_net_tx_desc *txd, struct sk_buff *skb)
  603. {
  604. struct ipv6hdr *ipv6h;
  605. struct iphdr *iph;
  606. u8 l4_hdr;
  607. if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
  608. return;
  609. if (skb->ip_summed != CHECKSUM_PARTIAL)
  610. return;
  611. txd->flags |= PCIE_DESC_TX_CSUM;
  612. if (skb->encapsulation)
  613. txd->flags |= PCIE_DESC_TX_ENCAP;
  614. iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
  615. ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
  616. if (iph->version == 4) {
  617. txd->flags |= PCIE_DESC_TX_IP4_CSUM;
  618. l4_hdr = iph->protocol;
  619. } else if (ipv6h->version == 6) {
  620. l4_hdr = ipv6h->nexthdr;
  621. } else {
  622. nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
  623. return;
  624. }
  625. switch (l4_hdr) {
  626. case IPPROTO_TCP:
  627. txd->flags |= PCIE_DESC_TX_TCP_CSUM;
  628. break;
  629. case IPPROTO_UDP:
  630. txd->flags |= PCIE_DESC_TX_UDP_CSUM;
  631. break;
  632. default:
  633. nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr);
  634. return;
  635. }
  636. u64_stats_update_begin(&r_vec->tx_sync);
  637. if (skb->encapsulation)
  638. r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
  639. else
  640. r_vec->hw_csum_tx += txbuf->pkt_cnt;
  641. u64_stats_update_end(&r_vec->tx_sync);
  642. }
  643. static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring)
  644. {
  645. wmb();
  646. nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
  647. tx_ring->wr_ptr_add = 0;
  648. }
  649. static int nfp_net_prep_port_id(struct sk_buff *skb)
  650. {
  651. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  652. unsigned char *data;
  653. if (likely(!md_dst))
  654. return 0;
  655. if (unlikely(md_dst->type != METADATA_HW_PORT_MUX))
  656. return 0;
  657. if (unlikely(skb_cow_head(skb, 8)))
  658. return -ENOMEM;
  659. data = skb_push(skb, 8);
  660. put_unaligned_be32(NFP_NET_META_PORTID, data);
  661. put_unaligned_be32(md_dst->u.port_info.port_id, data + 4);
  662. return 8;
  663. }
  664. /**
  665. * nfp_net_tx() - Main transmit entry point
  666. * @skb: SKB to transmit
  667. * @netdev: netdev structure
  668. *
  669. * Return: NETDEV_TX_OK on success.
  670. */
  671. static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
  672. {
  673. struct nfp_net *nn = netdev_priv(netdev);
  674. const struct skb_frag_struct *frag;
  675. struct nfp_net_tx_desc *txd, txdg;
  676. int f, nr_frags, wr_idx, md_bytes;
  677. struct nfp_net_tx_ring *tx_ring;
  678. struct nfp_net_r_vector *r_vec;
  679. struct nfp_net_tx_buf *txbuf;
  680. struct netdev_queue *nd_q;
  681. struct nfp_net_dp *dp;
  682. dma_addr_t dma_addr;
  683. unsigned int fsize;
  684. u16 qidx;
  685. dp = &nn->dp;
  686. qidx = skb_get_queue_mapping(skb);
  687. tx_ring = &dp->tx_rings[qidx];
  688. r_vec = tx_ring->r_vec;
  689. nd_q = netdev_get_tx_queue(dp->netdev, qidx);
  690. nr_frags = skb_shinfo(skb)->nr_frags;
  691. if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
  692. nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
  693. qidx, tx_ring->wr_p, tx_ring->rd_p);
  694. netif_tx_stop_queue(nd_q);
  695. nfp_net_tx_xmit_more_flush(tx_ring);
  696. u64_stats_update_begin(&r_vec->tx_sync);
  697. r_vec->tx_busy++;
  698. u64_stats_update_end(&r_vec->tx_sync);
  699. return NETDEV_TX_BUSY;
  700. }
  701. md_bytes = nfp_net_prep_port_id(skb);
  702. if (unlikely(md_bytes < 0)) {
  703. nfp_net_tx_xmit_more_flush(tx_ring);
  704. dev_kfree_skb_any(skb);
  705. return NETDEV_TX_OK;
  706. }
  707. /* Start with the head skbuf */
  708. dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
  709. DMA_TO_DEVICE);
  710. if (dma_mapping_error(dp->dev, dma_addr))
  711. goto err_free;
  712. wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
  713. /* Stash the soft descriptor of the head then initialize it */
  714. txbuf = &tx_ring->txbufs[wr_idx];
  715. txbuf->skb = skb;
  716. txbuf->dma_addr = dma_addr;
  717. txbuf->fidx = -1;
  718. txbuf->pkt_cnt = 1;
  719. txbuf->real_len = skb->len;
  720. /* Build TX descriptor */
  721. txd = &tx_ring->txds[wr_idx];
  722. txd->offset_eop = (nr_frags ? 0 : PCIE_DESC_TX_EOP) | md_bytes;
  723. txd->dma_len = cpu_to_le16(skb_headlen(skb));
  724. nfp_desc_set_dma_addr(txd, dma_addr);
  725. txd->data_len = cpu_to_le16(skb->len);
  726. txd->flags = 0;
  727. txd->mss = 0;
  728. txd->lso_hdrlen = 0;
  729. /* Do not reorder - tso may adjust pkt cnt, vlan may override fields */
  730. nfp_net_tx_tso(r_vec, txbuf, txd, skb);
  731. nfp_net_tx_csum(dp, r_vec, txbuf, txd, skb);
  732. if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
  733. txd->flags |= PCIE_DESC_TX_VLAN;
  734. txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
  735. }
  736. /* Gather DMA */
  737. if (nr_frags > 0) {
  738. /* all descs must match except for in addr, length and eop */
  739. txdg = *txd;
  740. for (f = 0; f < nr_frags; f++) {
  741. frag = &skb_shinfo(skb)->frags[f];
  742. fsize = skb_frag_size(frag);
  743. dma_addr = skb_frag_dma_map(dp->dev, frag, 0,
  744. fsize, DMA_TO_DEVICE);
  745. if (dma_mapping_error(dp->dev, dma_addr))
  746. goto err_unmap;
  747. wr_idx = D_IDX(tx_ring, wr_idx + 1);
  748. tx_ring->txbufs[wr_idx].skb = skb;
  749. tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
  750. tx_ring->txbufs[wr_idx].fidx = f;
  751. txd = &tx_ring->txds[wr_idx];
  752. *txd = txdg;
  753. txd->dma_len = cpu_to_le16(fsize);
  754. nfp_desc_set_dma_addr(txd, dma_addr);
  755. txd->offset_eop |=
  756. (f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0;
  757. }
  758. u64_stats_update_begin(&r_vec->tx_sync);
  759. r_vec->tx_gather++;
  760. u64_stats_update_end(&r_vec->tx_sync);
  761. }
  762. netdev_tx_sent_queue(nd_q, txbuf->real_len);
  763. tx_ring->wr_p += nr_frags + 1;
  764. if (nfp_net_tx_ring_should_stop(tx_ring))
  765. nfp_net_tx_ring_stop(nd_q, tx_ring);
  766. tx_ring->wr_ptr_add += nr_frags + 1;
  767. if (!skb->xmit_more || netif_xmit_stopped(nd_q))
  768. nfp_net_tx_xmit_more_flush(tx_ring);
  769. skb_tx_timestamp(skb);
  770. return NETDEV_TX_OK;
  771. err_unmap:
  772. --f;
  773. while (f >= 0) {
  774. frag = &skb_shinfo(skb)->frags[f];
  775. dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
  776. skb_frag_size(frag), DMA_TO_DEVICE);
  777. tx_ring->txbufs[wr_idx].skb = NULL;
  778. tx_ring->txbufs[wr_idx].dma_addr = 0;
  779. tx_ring->txbufs[wr_idx].fidx = -2;
  780. wr_idx = wr_idx - 1;
  781. if (wr_idx < 0)
  782. wr_idx += tx_ring->cnt;
  783. }
  784. dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
  785. skb_headlen(skb), DMA_TO_DEVICE);
  786. tx_ring->txbufs[wr_idx].skb = NULL;
  787. tx_ring->txbufs[wr_idx].dma_addr = 0;
  788. tx_ring->txbufs[wr_idx].fidx = -2;
  789. err_free:
  790. nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
  791. nfp_net_tx_xmit_more_flush(tx_ring);
  792. u64_stats_update_begin(&r_vec->tx_sync);
  793. r_vec->tx_errors++;
  794. u64_stats_update_end(&r_vec->tx_sync);
  795. dev_kfree_skb_any(skb);
  796. return NETDEV_TX_OK;
  797. }
  798. /**
  799. * nfp_net_tx_complete() - Handled completed TX packets
  800. * @tx_ring: TX ring structure
  801. *
  802. * Return: Number of completed TX descriptors
  803. */
  804. static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring)
  805. {
  806. struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
  807. struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
  808. const struct skb_frag_struct *frag;
  809. struct netdev_queue *nd_q;
  810. u32 done_pkts = 0, done_bytes = 0;
  811. struct sk_buff *skb;
  812. int todo, nr_frags;
  813. u32 qcp_rd_p;
  814. int fidx;
  815. int idx;
  816. if (tx_ring->wr_p == tx_ring->rd_p)
  817. return;
  818. /* Work out how many descriptors have been transmitted */
  819. qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
  820. if (qcp_rd_p == tx_ring->qcp_rd_p)
  821. return;
  822. todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
  823. while (todo--) {
  824. idx = D_IDX(tx_ring, tx_ring->rd_p++);
  825. skb = tx_ring->txbufs[idx].skb;
  826. if (!skb)
  827. continue;
  828. nr_frags = skb_shinfo(skb)->nr_frags;
  829. fidx = tx_ring->txbufs[idx].fidx;
  830. if (fidx == -1) {
  831. /* unmap head */
  832. dma_unmap_single(dp->dev, tx_ring->txbufs[idx].dma_addr,
  833. skb_headlen(skb), DMA_TO_DEVICE);
  834. done_pkts += tx_ring->txbufs[idx].pkt_cnt;
  835. done_bytes += tx_ring->txbufs[idx].real_len;
  836. } else {
  837. /* unmap fragment */
  838. frag = &skb_shinfo(skb)->frags[fidx];
  839. dma_unmap_page(dp->dev, tx_ring->txbufs[idx].dma_addr,
  840. skb_frag_size(frag), DMA_TO_DEVICE);
  841. }
  842. /* check for last gather fragment */
  843. if (fidx == nr_frags - 1)
  844. dev_kfree_skb_any(skb);
  845. tx_ring->txbufs[idx].dma_addr = 0;
  846. tx_ring->txbufs[idx].skb = NULL;
  847. tx_ring->txbufs[idx].fidx = -2;
  848. }
  849. tx_ring->qcp_rd_p = qcp_rd_p;
  850. u64_stats_update_begin(&r_vec->tx_sync);
  851. r_vec->tx_bytes += done_bytes;
  852. r_vec->tx_pkts += done_pkts;
  853. u64_stats_update_end(&r_vec->tx_sync);
  854. if (!dp->netdev)
  855. return;
  856. nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
  857. netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
  858. if (nfp_net_tx_ring_should_wake(tx_ring)) {
  859. /* Make sure TX thread will see updated tx_ring->rd_p */
  860. smp_mb();
  861. if (unlikely(netif_tx_queue_stopped(nd_q)))
  862. netif_tx_wake_queue(nd_q);
  863. }
  864. WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
  865. "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
  866. tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
  867. }
  868. static bool nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
  869. {
  870. struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
  871. u32 done_pkts = 0, done_bytes = 0;
  872. bool done_all;
  873. int idx, todo;
  874. u32 qcp_rd_p;
  875. /* Work out how many descriptors have been transmitted */
  876. qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
  877. if (qcp_rd_p == tx_ring->qcp_rd_p)
  878. return true;
  879. todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
  880. done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
  881. todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);
  882. tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + todo);
  883. done_pkts = todo;
  884. while (todo--) {
  885. idx = D_IDX(tx_ring, tx_ring->rd_p);
  886. tx_ring->rd_p++;
  887. done_bytes += tx_ring->txbufs[idx].real_len;
  888. }
  889. u64_stats_update_begin(&r_vec->tx_sync);
  890. r_vec->tx_bytes += done_bytes;
  891. r_vec->tx_pkts += done_pkts;
  892. u64_stats_update_end(&r_vec->tx_sync);
  893. WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
  894. "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
  895. tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
  896. return done_all;
  897. }
  898. /**
  899. * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
  900. * @dp: NFP Net data path struct
  901. * @tx_ring: TX ring structure
  902. *
  903. * Assumes that the device is stopped
  904. */
  905. static void
  906. nfp_net_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
  907. {
  908. const struct skb_frag_struct *frag;
  909. struct netdev_queue *nd_q;
  910. while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) {
  911. struct nfp_net_tx_buf *tx_buf;
  912. struct sk_buff *skb;
  913. int idx, nr_frags;
  914. idx = D_IDX(tx_ring, tx_ring->rd_p);
  915. tx_buf = &tx_ring->txbufs[idx];
  916. skb = tx_ring->txbufs[idx].skb;
  917. nr_frags = skb_shinfo(skb)->nr_frags;
  918. if (tx_buf->fidx == -1) {
  919. /* unmap head */
  920. dma_unmap_single(dp->dev, tx_buf->dma_addr,
  921. skb_headlen(skb), DMA_TO_DEVICE);
  922. } else {
  923. /* unmap fragment */
  924. frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
  925. dma_unmap_page(dp->dev, tx_buf->dma_addr,
  926. skb_frag_size(frag), DMA_TO_DEVICE);
  927. }
  928. /* check for last gather fragment */
  929. if (tx_buf->fidx == nr_frags - 1)
  930. dev_kfree_skb_any(skb);
  931. tx_buf->dma_addr = 0;
  932. tx_buf->skb = NULL;
  933. tx_buf->fidx = -2;
  934. tx_ring->qcp_rd_p++;
  935. tx_ring->rd_p++;
  936. }
  937. memset(tx_ring->txds, 0, sizeof(*tx_ring->txds) * tx_ring->cnt);
  938. tx_ring->wr_p = 0;
  939. tx_ring->rd_p = 0;
  940. tx_ring->qcp_rd_p = 0;
  941. tx_ring->wr_ptr_add = 0;
  942. if (tx_ring->is_xdp || !dp->netdev)
  943. return;
  944. nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
  945. netdev_tx_reset_queue(nd_q);
  946. }
  947. static void nfp_net_tx_timeout(struct net_device *netdev)
  948. {
  949. struct nfp_net *nn = netdev_priv(netdev);
  950. int i;
  951. for (i = 0; i < nn->dp.netdev->real_num_tx_queues; i++) {
  952. if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
  953. continue;
  954. nn_warn(nn, "TX timeout on ring: %d\n", i);
  955. }
  956. nn_warn(nn, "TX watchdog timeout\n");
  957. }
  958. /* Receive processing
  959. */
  960. static unsigned int
  961. nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
  962. {
  963. unsigned int fl_bufsz;
  964. fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
  965. fl_bufsz += dp->rx_dma_off;
  966. if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
  967. fl_bufsz += NFP_NET_MAX_PREPEND;
  968. else
  969. fl_bufsz += dp->rx_offset;
  970. fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
  971. fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
  972. fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  973. return fl_bufsz;
  974. }
  975. static void
  976. nfp_net_free_frag(void *frag, bool xdp)
  977. {
  978. if (!xdp)
  979. skb_free_frag(frag);
  980. else
  981. __free_page(virt_to_page(frag));
  982. }
  983. /**
  984. * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
  985. * @dp: NFP Net data path struct
  986. * @dma_addr: Pointer to storage for DMA address (output param)
  987. *
  988. * This function will allcate a new page frag, map it for DMA.
  989. *
  990. * Return: allocated page frag or NULL on failure.
  991. */
  992. static void *nfp_net_rx_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
  993. {
  994. void *frag;
  995. if (!dp->xdp_prog)
  996. frag = netdev_alloc_frag(dp->fl_bufsz);
  997. else
  998. frag = page_address(alloc_page(GFP_KERNEL | __GFP_COLD));
  999. if (!frag) {
  1000. nn_dp_warn(dp, "Failed to alloc receive page frag\n");
  1001. return NULL;
  1002. }
  1003. *dma_addr = nfp_net_dma_map_rx(dp, frag);
  1004. if (dma_mapping_error(dp->dev, *dma_addr)) {
  1005. nfp_net_free_frag(frag, dp->xdp_prog);
  1006. nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
  1007. return NULL;
  1008. }
  1009. return frag;
  1010. }
  1011. static void *nfp_net_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
  1012. {
  1013. void *frag;
  1014. if (!dp->xdp_prog)
  1015. frag = napi_alloc_frag(dp->fl_bufsz);
  1016. else
  1017. frag = page_address(alloc_page(GFP_ATOMIC | __GFP_COLD));
  1018. if (!frag) {
  1019. nn_dp_warn(dp, "Failed to alloc receive page frag\n");
  1020. return NULL;
  1021. }
  1022. *dma_addr = nfp_net_dma_map_rx(dp, frag);
  1023. if (dma_mapping_error(dp->dev, *dma_addr)) {
  1024. nfp_net_free_frag(frag, dp->xdp_prog);
  1025. nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
  1026. return NULL;
  1027. }
  1028. return frag;
  1029. }
  1030. /**
  1031. * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
  1032. * @dp: NFP Net data path struct
  1033. * @rx_ring: RX ring structure
  1034. * @frag: page fragment buffer
  1035. * @dma_addr: DMA address of skb mapping
  1036. */
  1037. static void nfp_net_rx_give_one(const struct nfp_net_dp *dp,
  1038. struct nfp_net_rx_ring *rx_ring,
  1039. void *frag, dma_addr_t dma_addr)
  1040. {
  1041. unsigned int wr_idx;
  1042. wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
  1043. nfp_net_dma_sync_dev_rx(dp, dma_addr);
  1044. /* Stash SKB and DMA address away */
  1045. rx_ring->rxbufs[wr_idx].frag = frag;
  1046. rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
  1047. /* Fill freelist descriptor */
  1048. rx_ring->rxds[wr_idx].fld.reserved = 0;
  1049. rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
  1050. nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld,
  1051. dma_addr + dp->rx_dma_off);
  1052. rx_ring->wr_p++;
  1053. if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
  1054. /* Update write pointer of the freelist queue. Make
  1055. * sure all writes are flushed before telling the hardware.
  1056. */
  1057. wmb();
  1058. nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
  1059. }
  1060. }
  1061. /**
  1062. * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
  1063. * @rx_ring: RX ring structure
  1064. *
  1065. * Warning: Do *not* call if ring buffers were never put on the FW freelist
  1066. * (i.e. device was not enabled)!
  1067. */
  1068. static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
  1069. {
  1070. unsigned int wr_idx, last_idx;
  1071. /* Move the empty entry to the end of the list */
  1072. wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
  1073. last_idx = rx_ring->cnt - 1;
  1074. rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
  1075. rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
  1076. rx_ring->rxbufs[last_idx].dma_addr = 0;
  1077. rx_ring->rxbufs[last_idx].frag = NULL;
  1078. memset(rx_ring->rxds, 0, sizeof(*rx_ring->rxds) * rx_ring->cnt);
  1079. rx_ring->wr_p = 0;
  1080. rx_ring->rd_p = 0;
  1081. }
  1082. /**
  1083. * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
  1084. * @dp: NFP Net data path struct
  1085. * @rx_ring: RX ring to remove buffers from
  1086. *
  1087. * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
  1088. * entries. After device is disabled nfp_net_rx_ring_reset() must be called
  1089. * to restore required ring geometry.
  1090. */
  1091. static void
  1092. nfp_net_rx_ring_bufs_free(struct nfp_net_dp *dp,
  1093. struct nfp_net_rx_ring *rx_ring)
  1094. {
  1095. unsigned int i;
  1096. for (i = 0; i < rx_ring->cnt - 1; i++) {
  1097. /* NULL skb can only happen when initial filling of the ring
  1098. * fails to allocate enough buffers and calls here to free
  1099. * already allocated ones.
  1100. */
  1101. if (!rx_ring->rxbufs[i].frag)
  1102. continue;
  1103. nfp_net_dma_unmap_rx(dp, rx_ring->rxbufs[i].dma_addr);
  1104. nfp_net_free_frag(rx_ring->rxbufs[i].frag, dp->xdp_prog);
  1105. rx_ring->rxbufs[i].dma_addr = 0;
  1106. rx_ring->rxbufs[i].frag = NULL;
  1107. }
  1108. }
  1109. /**
  1110. * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
  1111. * @dp: NFP Net data path struct
  1112. * @rx_ring: RX ring to remove buffers from
  1113. */
  1114. static int
  1115. nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp *dp,
  1116. struct nfp_net_rx_ring *rx_ring)
  1117. {
  1118. struct nfp_net_rx_buf *rxbufs;
  1119. unsigned int i;
  1120. rxbufs = rx_ring->rxbufs;
  1121. for (i = 0; i < rx_ring->cnt - 1; i++) {
  1122. rxbufs[i].frag = nfp_net_rx_alloc_one(dp, &rxbufs[i].dma_addr);
  1123. if (!rxbufs[i].frag) {
  1124. nfp_net_rx_ring_bufs_free(dp, rx_ring);
  1125. return -ENOMEM;
  1126. }
  1127. }
  1128. return 0;
  1129. }
  1130. /**
  1131. * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
  1132. * @dp: NFP Net data path struct
  1133. * @rx_ring: RX ring to fill
  1134. */
  1135. static void
  1136. nfp_net_rx_ring_fill_freelist(struct nfp_net_dp *dp,
  1137. struct nfp_net_rx_ring *rx_ring)
  1138. {
  1139. unsigned int i;
  1140. for (i = 0; i < rx_ring->cnt - 1; i++)
  1141. nfp_net_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
  1142. rx_ring->rxbufs[i].dma_addr);
  1143. }
  1144. /**
  1145. * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
  1146. * @flags: RX descriptor flags field in CPU byte order
  1147. */
  1148. static int nfp_net_rx_csum_has_errors(u16 flags)
  1149. {
  1150. u16 csum_all_checked, csum_all_ok;
  1151. csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
  1152. csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
  1153. return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
  1154. }
  1155. /**
  1156. * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
  1157. * @dp: NFP Net data path struct
  1158. * @r_vec: per-ring structure
  1159. * @rxd: Pointer to RX descriptor
  1160. * @meta: Parsed metadata prepend
  1161. * @skb: Pointer to SKB
  1162. */
  1163. static void nfp_net_rx_csum(struct nfp_net_dp *dp,
  1164. struct nfp_net_r_vector *r_vec,
  1165. struct nfp_net_rx_desc *rxd,
  1166. struct nfp_meta_parsed *meta, struct sk_buff *skb)
  1167. {
  1168. skb_checksum_none_assert(skb);
  1169. if (!(dp->netdev->features & NETIF_F_RXCSUM))
  1170. return;
  1171. if (meta->csum_type) {
  1172. skb->ip_summed = meta->csum_type;
  1173. skb->csum = meta->csum;
  1174. u64_stats_update_begin(&r_vec->rx_sync);
  1175. r_vec->hw_csum_rx_ok++;
  1176. u64_stats_update_end(&r_vec->rx_sync);
  1177. return;
  1178. }
  1179. if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
  1180. u64_stats_update_begin(&r_vec->rx_sync);
  1181. r_vec->hw_csum_rx_error++;
  1182. u64_stats_update_end(&r_vec->rx_sync);
  1183. return;
  1184. }
  1185. /* Assume that the firmware will never report inner CSUM_OK unless outer
  1186. * L4 headers were successfully parsed. FW will always report zero UDP
  1187. * checksum as CSUM_OK.
  1188. */
  1189. if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
  1190. rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
  1191. __skb_incr_checksum_unnecessary(skb);
  1192. u64_stats_update_begin(&r_vec->rx_sync);
  1193. r_vec->hw_csum_rx_ok++;
  1194. u64_stats_update_end(&r_vec->rx_sync);
  1195. }
  1196. if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
  1197. rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
  1198. __skb_incr_checksum_unnecessary(skb);
  1199. u64_stats_update_begin(&r_vec->rx_sync);
  1200. r_vec->hw_csum_rx_inner_ok++;
  1201. u64_stats_update_end(&r_vec->rx_sync);
  1202. }
  1203. }
  1204. static void
  1205. nfp_net_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
  1206. unsigned int type, __be32 *hash)
  1207. {
  1208. if (!(netdev->features & NETIF_F_RXHASH))
  1209. return;
  1210. switch (type) {
  1211. case NFP_NET_RSS_IPV4:
  1212. case NFP_NET_RSS_IPV6:
  1213. case NFP_NET_RSS_IPV6_EX:
  1214. meta->hash_type = PKT_HASH_TYPE_L3;
  1215. break;
  1216. default:
  1217. meta->hash_type = PKT_HASH_TYPE_L4;
  1218. break;
  1219. }
  1220. meta->hash = get_unaligned_be32(hash);
  1221. }
  1222. static void
  1223. nfp_net_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta,
  1224. void *data, struct nfp_net_rx_desc *rxd)
  1225. {
  1226. struct nfp_net_rx_hash *rx_hash = data;
  1227. if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
  1228. return;
  1229. nfp_net_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type),
  1230. &rx_hash->hash);
  1231. }
  1232. static void *
  1233. nfp_net_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
  1234. void *data, int meta_len)
  1235. {
  1236. u32 meta_info;
  1237. meta_info = get_unaligned_be32(data);
  1238. data += 4;
  1239. while (meta_info) {
  1240. switch (meta_info & NFP_NET_META_FIELD_MASK) {
  1241. case NFP_NET_META_HASH:
  1242. meta_info >>= NFP_NET_META_FIELD_SIZE;
  1243. nfp_net_set_hash(netdev, meta,
  1244. meta_info & NFP_NET_META_FIELD_MASK,
  1245. (__be32 *)data);
  1246. data += 4;
  1247. break;
  1248. case NFP_NET_META_MARK:
  1249. meta->mark = get_unaligned_be32(data);
  1250. data += 4;
  1251. break;
  1252. case NFP_NET_META_PORTID:
  1253. meta->portid = get_unaligned_be32(data);
  1254. data += 4;
  1255. break;
  1256. case NFP_NET_META_CSUM:
  1257. meta->csum_type = CHECKSUM_COMPLETE;
  1258. meta->csum =
  1259. (__force __wsum)__get_unaligned_cpu32(data);
  1260. data += 4;
  1261. break;
  1262. default:
  1263. return NULL;
  1264. }
  1265. meta_info >>= NFP_NET_META_FIELD_SIZE;
  1266. }
  1267. return data;
  1268. }
  1269. static void
  1270. nfp_net_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
  1271. struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
  1272. struct sk_buff *skb)
  1273. {
  1274. u64_stats_update_begin(&r_vec->rx_sync);
  1275. r_vec->rx_drops++;
  1276. u64_stats_update_end(&r_vec->rx_sync);
  1277. /* skb is build based on the frag, free_skb() would free the frag
  1278. * so to be able to reuse it we need an extra ref.
  1279. */
  1280. if (skb && rxbuf && skb->head == rxbuf->frag)
  1281. page_ref_inc(virt_to_head_page(rxbuf->frag));
  1282. if (rxbuf)
  1283. nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
  1284. if (skb)
  1285. dev_kfree_skb_any(skb);
  1286. }
  1287. static bool
  1288. nfp_net_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
  1289. struct nfp_net_tx_ring *tx_ring,
  1290. struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
  1291. unsigned int pkt_len, bool *completed)
  1292. {
  1293. struct nfp_net_tx_buf *txbuf;
  1294. struct nfp_net_tx_desc *txd;
  1295. int wr_idx;
  1296. if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
  1297. if (!*completed) {
  1298. nfp_net_xdp_complete(tx_ring);
  1299. *completed = true;
  1300. }
  1301. if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
  1302. nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
  1303. NULL);
  1304. return false;
  1305. }
  1306. }
  1307. wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
  1308. /* Stash the soft descriptor of the head then initialize it */
  1309. txbuf = &tx_ring->txbufs[wr_idx];
  1310. nfp_net_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr);
  1311. txbuf->frag = rxbuf->frag;
  1312. txbuf->dma_addr = rxbuf->dma_addr;
  1313. txbuf->fidx = -1;
  1314. txbuf->pkt_cnt = 1;
  1315. txbuf->real_len = pkt_len;
  1316. dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
  1317. pkt_len, DMA_BIDIRECTIONAL);
  1318. /* Build TX descriptor */
  1319. txd = &tx_ring->txds[wr_idx];
  1320. txd->offset_eop = PCIE_DESC_TX_EOP;
  1321. txd->dma_len = cpu_to_le16(pkt_len);
  1322. nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + dma_off);
  1323. txd->data_len = cpu_to_le16(pkt_len);
  1324. txd->flags = 0;
  1325. txd->mss = 0;
  1326. txd->lso_hdrlen = 0;
  1327. tx_ring->wr_p++;
  1328. tx_ring->wr_ptr_add++;
  1329. return true;
  1330. }
  1331. static int nfp_net_run_xdp(struct bpf_prog *prog, void *data, void *hard_start,
  1332. unsigned int *off, unsigned int *len)
  1333. {
  1334. struct xdp_buff xdp;
  1335. void *orig_data;
  1336. int ret;
  1337. xdp.data_hard_start = hard_start;
  1338. xdp.data = data + *off;
  1339. xdp.data_end = data + *off + *len;
  1340. orig_data = xdp.data;
  1341. ret = bpf_prog_run_xdp(prog, &xdp);
  1342. *len -= xdp.data - orig_data;
  1343. *off += xdp.data - orig_data;
  1344. return ret;
  1345. }
  1346. /**
  1347. * nfp_net_rx() - receive up to @budget packets on @rx_ring
  1348. * @rx_ring: RX ring to receive from
  1349. * @budget: NAPI budget
  1350. *
  1351. * Note, this function is separated out from the napi poll function to
  1352. * more cleanly separate packet receive code from other bookkeeping
  1353. * functions performed in the napi poll function.
  1354. *
  1355. * Return: Number of packets received.
  1356. */
  1357. static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
  1358. {
  1359. struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
  1360. struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
  1361. struct nfp_net_tx_ring *tx_ring;
  1362. struct bpf_prog *xdp_prog;
  1363. bool xdp_tx_cmpl = false;
  1364. unsigned int true_bufsz;
  1365. struct sk_buff *skb;
  1366. int pkts_polled = 0;
  1367. int idx;
  1368. rcu_read_lock();
  1369. xdp_prog = READ_ONCE(dp->xdp_prog);
  1370. true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
  1371. tx_ring = r_vec->xdp_ring;
  1372. while (pkts_polled < budget) {
  1373. unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
  1374. struct nfp_net_rx_buf *rxbuf;
  1375. struct nfp_net_rx_desc *rxd;
  1376. struct nfp_meta_parsed meta;
  1377. struct net_device *netdev;
  1378. dma_addr_t new_dma_addr;
  1379. void *new_frag;
  1380. idx = D_IDX(rx_ring, rx_ring->rd_p);
  1381. rxd = &rx_ring->rxds[idx];
  1382. if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
  1383. break;
  1384. /* Memory barrier to ensure that we won't do other reads
  1385. * before the DD bit.
  1386. */
  1387. dma_rmb();
  1388. memset(&meta, 0, sizeof(meta));
  1389. rx_ring->rd_p++;
  1390. pkts_polled++;
  1391. rxbuf = &rx_ring->rxbufs[idx];
  1392. /* < meta_len >
  1393. * <-- [rx_offset] -->
  1394. * ---------------------------------------------------------
  1395. * | [XX] | metadata | packet | XXXX |
  1396. * ---------------------------------------------------------
  1397. * <---------------- data_len --------------->
  1398. *
  1399. * The rx_offset is fixed for all packets, the meta_len can vary
  1400. * on a packet by packet basis. If rx_offset is set to zero
  1401. * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
  1402. * buffer and is immediately followed by the packet (no [XX]).
  1403. */
  1404. meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
  1405. data_len = le16_to_cpu(rxd->rxd.data_len);
  1406. pkt_len = data_len - meta_len;
  1407. pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
  1408. if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
  1409. pkt_off += meta_len;
  1410. else
  1411. pkt_off += dp->rx_offset;
  1412. meta_off = pkt_off - meta_len;
  1413. /* Stats update */
  1414. u64_stats_update_begin(&r_vec->rx_sync);
  1415. r_vec->rx_pkts++;
  1416. r_vec->rx_bytes += pkt_len;
  1417. u64_stats_update_end(&r_vec->rx_sync);
  1418. if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
  1419. (dp->rx_offset && meta_len > dp->rx_offset))) {
  1420. nn_dp_warn(dp, "oversized RX packet metadata %u\n",
  1421. meta_len);
  1422. nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
  1423. continue;
  1424. }
  1425. nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
  1426. data_len);
  1427. if (!dp->chained_metadata_format) {
  1428. nfp_net_set_hash_desc(dp->netdev, &meta,
  1429. rxbuf->frag + meta_off, rxd);
  1430. } else if (meta_len) {
  1431. void *end;
  1432. end = nfp_net_parse_meta(dp->netdev, &meta,
  1433. rxbuf->frag + meta_off,
  1434. meta_len);
  1435. if (unlikely(end != rxbuf->frag + pkt_off)) {
  1436. nn_dp_warn(dp, "invalid RX packet metadata\n");
  1437. nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
  1438. NULL);
  1439. continue;
  1440. }
  1441. }
  1442. if (xdp_prog && !(rxd->rxd.flags & PCIE_DESC_RX_BPF &&
  1443. dp->bpf_offload_xdp) && !meta.portid) {
  1444. unsigned int dma_off;
  1445. void *hard_start;
  1446. int act;
  1447. hard_start = rxbuf->frag + NFP_NET_RX_BUF_HEADROOM;
  1448. act = nfp_net_run_xdp(xdp_prog, rxbuf->frag, hard_start,
  1449. &pkt_off, &pkt_len);
  1450. switch (act) {
  1451. case XDP_PASS:
  1452. break;
  1453. case XDP_TX:
  1454. dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
  1455. if (unlikely(!nfp_net_tx_xdp_buf(dp, rx_ring,
  1456. tx_ring, rxbuf,
  1457. dma_off,
  1458. pkt_len,
  1459. &xdp_tx_cmpl)))
  1460. trace_xdp_exception(dp->netdev,
  1461. xdp_prog, act);
  1462. continue;
  1463. default:
  1464. bpf_warn_invalid_xdp_action(act);
  1465. /* fall through */
  1466. case XDP_ABORTED:
  1467. trace_xdp_exception(dp->netdev, xdp_prog, act);
  1468. /* fall through */
  1469. case XDP_DROP:
  1470. nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
  1471. rxbuf->dma_addr);
  1472. continue;
  1473. }
  1474. }
  1475. skb = build_skb(rxbuf->frag, true_bufsz);
  1476. if (unlikely(!skb)) {
  1477. nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
  1478. continue;
  1479. }
  1480. new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
  1481. if (unlikely(!new_frag)) {
  1482. nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
  1483. continue;
  1484. }
  1485. if (likely(!meta.portid)) {
  1486. netdev = dp->netdev;
  1487. } else {
  1488. struct nfp_net *nn;
  1489. nn = netdev_priv(dp->netdev);
  1490. netdev = nfp_app_repr_get(nn->app, meta.portid);
  1491. if (unlikely(!netdev)) {
  1492. nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
  1493. continue;
  1494. }
  1495. nfp_repr_inc_rx_stats(netdev, pkt_len);
  1496. }
  1497. nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
  1498. nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
  1499. skb_reserve(skb, pkt_off);
  1500. skb_put(skb, pkt_len);
  1501. skb->mark = meta.mark;
  1502. skb_set_hash(skb, meta.hash, meta.hash_type);
  1503. skb_record_rx_queue(skb, rx_ring->idx);
  1504. skb->protocol = eth_type_trans(skb, netdev);
  1505. nfp_net_rx_csum(dp, r_vec, rxd, &meta, skb);
  1506. if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
  1507. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1508. le16_to_cpu(rxd->rxd.vlan));
  1509. napi_gro_receive(&rx_ring->r_vec->napi, skb);
  1510. }
  1511. if (xdp_prog) {
  1512. if (tx_ring->wr_ptr_add)
  1513. nfp_net_tx_xmit_more_flush(tx_ring);
  1514. else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
  1515. !xdp_tx_cmpl)
  1516. if (!nfp_net_xdp_complete(tx_ring))
  1517. pkts_polled = budget;
  1518. }
  1519. rcu_read_unlock();
  1520. return pkts_polled;
  1521. }
  1522. /**
  1523. * nfp_net_poll() - napi poll function
  1524. * @napi: NAPI structure
  1525. * @budget: NAPI budget
  1526. *
  1527. * Return: number of packets polled.
  1528. */
  1529. static int nfp_net_poll(struct napi_struct *napi, int budget)
  1530. {
  1531. struct nfp_net_r_vector *r_vec =
  1532. container_of(napi, struct nfp_net_r_vector, napi);
  1533. unsigned int pkts_polled = 0;
  1534. if (r_vec->tx_ring)
  1535. nfp_net_tx_complete(r_vec->tx_ring);
  1536. if (r_vec->rx_ring)
  1537. pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
  1538. if (pkts_polled < budget)
  1539. if (napi_complete_done(napi, pkts_polled))
  1540. nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
  1541. return pkts_polled;
  1542. }
  1543. /* Control device data path
  1544. */
  1545. static bool
  1546. nfp_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
  1547. struct sk_buff *skb, bool old)
  1548. {
  1549. unsigned int real_len = skb->len, meta_len = 0;
  1550. struct nfp_net_tx_ring *tx_ring;
  1551. struct nfp_net_tx_buf *txbuf;
  1552. struct nfp_net_tx_desc *txd;
  1553. struct nfp_net_dp *dp;
  1554. dma_addr_t dma_addr;
  1555. int wr_idx;
  1556. dp = &r_vec->nfp_net->dp;
  1557. tx_ring = r_vec->tx_ring;
  1558. if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
  1559. nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
  1560. goto err_free;
  1561. }
  1562. if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
  1563. u64_stats_update_begin(&r_vec->tx_sync);
  1564. r_vec->tx_busy++;
  1565. u64_stats_update_end(&r_vec->tx_sync);
  1566. if (!old)
  1567. __skb_queue_tail(&r_vec->queue, skb);
  1568. else
  1569. __skb_queue_head(&r_vec->queue, skb);
  1570. return true;
  1571. }
  1572. if (nfp_app_ctrl_has_meta(nn->app)) {
  1573. if (unlikely(skb_headroom(skb) < 8)) {
  1574. nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
  1575. goto err_free;
  1576. }
  1577. meta_len = 8;
  1578. put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
  1579. put_unaligned_be32(NFP_NET_META_PORTID, skb_push(skb, 4));
  1580. }
  1581. /* Start with the head skbuf */
  1582. dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
  1583. DMA_TO_DEVICE);
  1584. if (dma_mapping_error(dp->dev, dma_addr))
  1585. goto err_dma_warn;
  1586. wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
  1587. /* Stash the soft descriptor of the head then initialize it */
  1588. txbuf = &tx_ring->txbufs[wr_idx];
  1589. txbuf->skb = skb;
  1590. txbuf->dma_addr = dma_addr;
  1591. txbuf->fidx = -1;
  1592. txbuf->pkt_cnt = 1;
  1593. txbuf->real_len = real_len;
  1594. /* Build TX descriptor */
  1595. txd = &tx_ring->txds[wr_idx];
  1596. txd->offset_eop = meta_len | PCIE_DESC_TX_EOP;
  1597. txd->dma_len = cpu_to_le16(skb_headlen(skb));
  1598. nfp_desc_set_dma_addr(txd, dma_addr);
  1599. txd->data_len = cpu_to_le16(skb->len);
  1600. txd->flags = 0;
  1601. txd->mss = 0;
  1602. txd->lso_hdrlen = 0;
  1603. tx_ring->wr_p++;
  1604. tx_ring->wr_ptr_add++;
  1605. nfp_net_tx_xmit_more_flush(tx_ring);
  1606. return false;
  1607. err_dma_warn:
  1608. nn_dp_warn(dp, "Failed to DMA map TX CTRL buffer\n");
  1609. err_free:
  1610. u64_stats_update_begin(&r_vec->tx_sync);
  1611. r_vec->tx_errors++;
  1612. u64_stats_update_end(&r_vec->tx_sync);
  1613. dev_kfree_skb_any(skb);
  1614. return false;
  1615. }
  1616. bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
  1617. {
  1618. struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
  1619. bool ret;
  1620. spin_lock_bh(&r_vec->lock);
  1621. ret = nfp_ctrl_tx_one(nn, r_vec, skb, false);
  1622. spin_unlock_bh(&r_vec->lock);
  1623. return ret;
  1624. }
  1625. static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
  1626. {
  1627. struct sk_buff *skb;
  1628. while ((skb = __skb_dequeue(&r_vec->queue)))
  1629. if (nfp_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
  1630. return;
  1631. }
  1632. static bool
  1633. nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
  1634. {
  1635. u32 meta_type, meta_tag;
  1636. if (!nfp_app_ctrl_has_meta(nn->app))
  1637. return !meta_len;
  1638. if (meta_len != 8)
  1639. return false;
  1640. meta_type = get_unaligned_be32(data);
  1641. meta_tag = get_unaligned_be32(data + 4);
  1642. return (meta_type == NFP_NET_META_PORTID &&
  1643. meta_tag == NFP_META_PORT_ID_CTRL);
  1644. }
  1645. static bool
  1646. nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp,
  1647. struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring)
  1648. {
  1649. unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
  1650. struct nfp_net_rx_buf *rxbuf;
  1651. struct nfp_net_rx_desc *rxd;
  1652. dma_addr_t new_dma_addr;
  1653. struct sk_buff *skb;
  1654. void *new_frag;
  1655. int idx;
  1656. idx = D_IDX(rx_ring, rx_ring->rd_p);
  1657. rxd = &rx_ring->rxds[idx];
  1658. if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
  1659. return false;
  1660. /* Memory barrier to ensure that we won't do other reads
  1661. * before the DD bit.
  1662. */
  1663. dma_rmb();
  1664. rx_ring->rd_p++;
  1665. rxbuf = &rx_ring->rxbufs[idx];
  1666. meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
  1667. data_len = le16_to_cpu(rxd->rxd.data_len);
  1668. pkt_len = data_len - meta_len;
  1669. pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
  1670. if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
  1671. pkt_off += meta_len;
  1672. else
  1673. pkt_off += dp->rx_offset;
  1674. meta_off = pkt_off - meta_len;
  1675. /* Stats update */
  1676. u64_stats_update_begin(&r_vec->rx_sync);
  1677. r_vec->rx_pkts++;
  1678. r_vec->rx_bytes += pkt_len;
  1679. u64_stats_update_end(&r_vec->rx_sync);
  1680. nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off, data_len);
  1681. if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
  1682. nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
  1683. meta_len);
  1684. nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
  1685. return true;
  1686. }
  1687. skb = build_skb(rxbuf->frag, dp->fl_bufsz);
  1688. if (unlikely(!skb)) {
  1689. nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
  1690. return true;
  1691. }
  1692. new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
  1693. if (unlikely(!new_frag)) {
  1694. nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
  1695. return true;
  1696. }
  1697. nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
  1698. nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
  1699. skb_reserve(skb, pkt_off);
  1700. skb_put(skb, pkt_len);
  1701. nfp_app_ctrl_rx(nn->app, skb);
  1702. return true;
  1703. }
  1704. static void nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
  1705. {
  1706. struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
  1707. struct nfp_net *nn = r_vec->nfp_net;
  1708. struct nfp_net_dp *dp = &nn->dp;
  1709. while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring))
  1710. continue;
  1711. }
  1712. static void nfp_ctrl_poll(unsigned long arg)
  1713. {
  1714. struct nfp_net_r_vector *r_vec = (void *)arg;
  1715. spin_lock_bh(&r_vec->lock);
  1716. nfp_net_tx_complete(r_vec->tx_ring);
  1717. __nfp_ctrl_tx_queued(r_vec);
  1718. spin_unlock_bh(&r_vec->lock);
  1719. nfp_ctrl_rx(r_vec);
  1720. nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
  1721. }
  1722. /* Setup and Configuration
  1723. */
  1724. /**
  1725. * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
  1726. * @nn: NFP Network structure
  1727. */
  1728. static void nfp_net_vecs_init(struct nfp_net *nn)
  1729. {
  1730. struct nfp_net_r_vector *r_vec;
  1731. int r;
  1732. nn->lsc_handler = nfp_net_irq_lsc;
  1733. nn->exn_handler = nfp_net_irq_exn;
  1734. for (r = 0; r < nn->max_r_vecs; r++) {
  1735. struct msix_entry *entry;
  1736. entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
  1737. r_vec = &nn->r_vecs[r];
  1738. r_vec->nfp_net = nn;
  1739. r_vec->irq_entry = entry->entry;
  1740. r_vec->irq_vector = entry->vector;
  1741. if (nn->dp.netdev) {
  1742. r_vec->handler = nfp_net_irq_rxtx;
  1743. } else {
  1744. r_vec->handler = nfp_ctrl_irq_rxtx;
  1745. __skb_queue_head_init(&r_vec->queue);
  1746. spin_lock_init(&r_vec->lock);
  1747. tasklet_init(&r_vec->tasklet, nfp_ctrl_poll,
  1748. (unsigned long)r_vec);
  1749. tasklet_disable(&r_vec->tasklet);
  1750. }
  1751. cpumask_set_cpu(r, &r_vec->affinity_mask);
  1752. }
  1753. }
  1754. /**
  1755. * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
  1756. * @tx_ring: TX ring to free
  1757. */
  1758. static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
  1759. {
  1760. struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
  1761. struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
  1762. kfree(tx_ring->txbufs);
  1763. if (tx_ring->txds)
  1764. dma_free_coherent(dp->dev, tx_ring->size,
  1765. tx_ring->txds, tx_ring->dma);
  1766. tx_ring->cnt = 0;
  1767. tx_ring->txbufs = NULL;
  1768. tx_ring->txds = NULL;
  1769. tx_ring->dma = 0;
  1770. tx_ring->size = 0;
  1771. }
  1772. /**
  1773. * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
  1774. * @dp: NFP Net data path struct
  1775. * @tx_ring: TX Ring structure to allocate
  1776. *
  1777. * Return: 0 on success, negative errno otherwise.
  1778. */
  1779. static int
  1780. nfp_net_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
  1781. {
  1782. struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
  1783. int sz;
  1784. tx_ring->cnt = dp->txd_cnt;
  1785. tx_ring->size = sizeof(*tx_ring->txds) * tx_ring->cnt;
  1786. tx_ring->txds = dma_zalloc_coherent(dp->dev, tx_ring->size,
  1787. &tx_ring->dma, GFP_KERNEL);
  1788. if (!tx_ring->txds)
  1789. goto err_alloc;
  1790. sz = sizeof(*tx_ring->txbufs) * tx_ring->cnt;
  1791. tx_ring->txbufs = kzalloc(sz, GFP_KERNEL);
  1792. if (!tx_ring->txbufs)
  1793. goto err_alloc;
  1794. if (!tx_ring->is_xdp && dp->netdev)
  1795. netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
  1796. tx_ring->idx);
  1797. return 0;
  1798. err_alloc:
  1799. nfp_net_tx_ring_free(tx_ring);
  1800. return -ENOMEM;
  1801. }
  1802. static void
  1803. nfp_net_tx_ring_bufs_free(struct nfp_net_dp *dp,
  1804. struct nfp_net_tx_ring *tx_ring)
  1805. {
  1806. unsigned int i;
  1807. if (!tx_ring->is_xdp)
  1808. return;
  1809. for (i = 0; i < tx_ring->cnt; i++) {
  1810. if (!tx_ring->txbufs[i].frag)
  1811. return;
  1812. nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr);
  1813. __free_page(virt_to_page(tx_ring->txbufs[i].frag));
  1814. }
  1815. }
  1816. static int
  1817. nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp *dp,
  1818. struct nfp_net_tx_ring *tx_ring)
  1819. {
  1820. struct nfp_net_tx_buf *txbufs = tx_ring->txbufs;
  1821. unsigned int i;
  1822. if (!tx_ring->is_xdp)
  1823. return 0;
  1824. for (i = 0; i < tx_ring->cnt; i++) {
  1825. txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr);
  1826. if (!txbufs[i].frag) {
  1827. nfp_net_tx_ring_bufs_free(dp, tx_ring);
  1828. return -ENOMEM;
  1829. }
  1830. }
  1831. return 0;
  1832. }
  1833. static int nfp_net_tx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
  1834. {
  1835. unsigned int r;
  1836. dp->tx_rings = kcalloc(dp->num_tx_rings, sizeof(*dp->tx_rings),
  1837. GFP_KERNEL);
  1838. if (!dp->tx_rings)
  1839. return -ENOMEM;
  1840. for (r = 0; r < dp->num_tx_rings; r++) {
  1841. int bias = 0;
  1842. if (r >= dp->num_stack_tx_rings)
  1843. bias = dp->num_stack_tx_rings;
  1844. nfp_net_tx_ring_init(&dp->tx_rings[r], &nn->r_vecs[r - bias],
  1845. r, bias);
  1846. if (nfp_net_tx_ring_alloc(dp, &dp->tx_rings[r]))
  1847. goto err_free_prev;
  1848. if (nfp_net_tx_ring_bufs_alloc(dp, &dp->tx_rings[r]))
  1849. goto err_free_ring;
  1850. }
  1851. return 0;
  1852. err_free_prev:
  1853. while (r--) {
  1854. nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
  1855. err_free_ring:
  1856. nfp_net_tx_ring_free(&dp->tx_rings[r]);
  1857. }
  1858. kfree(dp->tx_rings);
  1859. return -ENOMEM;
  1860. }
  1861. static void nfp_net_tx_rings_free(struct nfp_net_dp *dp)
  1862. {
  1863. unsigned int r;
  1864. for (r = 0; r < dp->num_tx_rings; r++) {
  1865. nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
  1866. nfp_net_tx_ring_free(&dp->tx_rings[r]);
  1867. }
  1868. kfree(dp->tx_rings);
  1869. }
  1870. /**
  1871. * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
  1872. * @rx_ring: RX ring to free
  1873. */
  1874. static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
  1875. {
  1876. struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
  1877. struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
  1878. kfree(rx_ring->rxbufs);
  1879. if (rx_ring->rxds)
  1880. dma_free_coherent(dp->dev, rx_ring->size,
  1881. rx_ring->rxds, rx_ring->dma);
  1882. rx_ring->cnt = 0;
  1883. rx_ring->rxbufs = NULL;
  1884. rx_ring->rxds = NULL;
  1885. rx_ring->dma = 0;
  1886. rx_ring->size = 0;
  1887. }
  1888. /**
  1889. * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
  1890. * @dp: NFP Net data path struct
  1891. * @rx_ring: RX ring to allocate
  1892. *
  1893. * Return: 0 on success, negative errno otherwise.
  1894. */
  1895. static int
  1896. nfp_net_rx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring)
  1897. {
  1898. int sz;
  1899. rx_ring->cnt = dp->rxd_cnt;
  1900. rx_ring->size = sizeof(*rx_ring->rxds) * rx_ring->cnt;
  1901. rx_ring->rxds = dma_zalloc_coherent(dp->dev, rx_ring->size,
  1902. &rx_ring->dma, GFP_KERNEL);
  1903. if (!rx_ring->rxds)
  1904. goto err_alloc;
  1905. sz = sizeof(*rx_ring->rxbufs) * rx_ring->cnt;
  1906. rx_ring->rxbufs = kzalloc(sz, GFP_KERNEL);
  1907. if (!rx_ring->rxbufs)
  1908. goto err_alloc;
  1909. return 0;
  1910. err_alloc:
  1911. nfp_net_rx_ring_free(rx_ring);
  1912. return -ENOMEM;
  1913. }
  1914. static int nfp_net_rx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
  1915. {
  1916. unsigned int r;
  1917. dp->rx_rings = kcalloc(dp->num_rx_rings, sizeof(*dp->rx_rings),
  1918. GFP_KERNEL);
  1919. if (!dp->rx_rings)
  1920. return -ENOMEM;
  1921. for (r = 0; r < dp->num_rx_rings; r++) {
  1922. nfp_net_rx_ring_init(&dp->rx_rings[r], &nn->r_vecs[r], r);
  1923. if (nfp_net_rx_ring_alloc(dp, &dp->rx_rings[r]))
  1924. goto err_free_prev;
  1925. if (nfp_net_rx_ring_bufs_alloc(dp, &dp->rx_rings[r]))
  1926. goto err_free_ring;
  1927. }
  1928. return 0;
  1929. err_free_prev:
  1930. while (r--) {
  1931. nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
  1932. err_free_ring:
  1933. nfp_net_rx_ring_free(&dp->rx_rings[r]);
  1934. }
  1935. kfree(dp->rx_rings);
  1936. return -ENOMEM;
  1937. }
  1938. static void nfp_net_rx_rings_free(struct nfp_net_dp *dp)
  1939. {
  1940. unsigned int r;
  1941. for (r = 0; r < dp->num_rx_rings; r++) {
  1942. nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
  1943. nfp_net_rx_ring_free(&dp->rx_rings[r]);
  1944. }
  1945. kfree(dp->rx_rings);
  1946. }
  1947. static void
  1948. nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
  1949. struct nfp_net_r_vector *r_vec, int idx)
  1950. {
  1951. r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
  1952. r_vec->tx_ring =
  1953. idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
  1954. r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
  1955. &dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
  1956. }
  1957. static int
  1958. nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
  1959. int idx)
  1960. {
  1961. int err;
  1962. /* Setup NAPI */
  1963. if (nn->dp.netdev)
  1964. netif_napi_add(nn->dp.netdev, &r_vec->napi,
  1965. nfp_net_poll, NAPI_POLL_WEIGHT);
  1966. else
  1967. tasklet_enable(&r_vec->tasklet);
  1968. snprintf(r_vec->name, sizeof(r_vec->name),
  1969. "%s-rxtx-%d", nfp_net_name(nn), idx);
  1970. err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
  1971. r_vec);
  1972. if (err) {
  1973. if (nn->dp.netdev)
  1974. netif_napi_del(&r_vec->napi);
  1975. else
  1976. tasklet_disable(&r_vec->tasklet);
  1977. nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
  1978. return err;
  1979. }
  1980. disable_irq(r_vec->irq_vector);
  1981. irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
  1982. nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
  1983. r_vec->irq_entry);
  1984. return 0;
  1985. }
  1986. static void
  1987. nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
  1988. {
  1989. irq_set_affinity_hint(r_vec->irq_vector, NULL);
  1990. if (nn->dp.netdev)
  1991. netif_napi_del(&r_vec->napi);
  1992. else
  1993. tasklet_disable(&r_vec->tasklet);
  1994. free_irq(r_vec->irq_vector, r_vec);
  1995. }
  1996. /**
  1997. * nfp_net_rss_write_itbl() - Write RSS indirection table to device
  1998. * @nn: NFP Net device to reconfigure
  1999. */
  2000. void nfp_net_rss_write_itbl(struct nfp_net *nn)
  2001. {
  2002. int i;
  2003. for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
  2004. nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
  2005. get_unaligned_le32(nn->rss_itbl + i));
  2006. }
  2007. /**
  2008. * nfp_net_rss_write_key() - Write RSS hash key to device
  2009. * @nn: NFP Net device to reconfigure
  2010. */
  2011. void nfp_net_rss_write_key(struct nfp_net *nn)
  2012. {
  2013. int i;
  2014. for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
  2015. nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
  2016. get_unaligned_le32(nn->rss_key + i));
  2017. }
  2018. /**
  2019. * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
  2020. * @nn: NFP Net device to reconfigure
  2021. */
  2022. void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
  2023. {
  2024. u8 i;
  2025. u32 factor;
  2026. u32 value;
  2027. /* Compute factor used to convert coalesce '_usecs' parameters to
  2028. * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
  2029. * count.
  2030. */
  2031. factor = nn->me_freq_mhz / 16;
  2032. /* copy RX interrupt coalesce parameters */
  2033. value = (nn->rx_coalesce_max_frames << 16) |
  2034. (factor * nn->rx_coalesce_usecs);
  2035. for (i = 0; i < nn->dp.num_rx_rings; i++)
  2036. nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
  2037. /* copy TX interrupt coalesce parameters */
  2038. value = (nn->tx_coalesce_max_frames << 16) |
  2039. (factor * nn->tx_coalesce_usecs);
  2040. for (i = 0; i < nn->dp.num_tx_rings; i++)
  2041. nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
  2042. }
  2043. /**
  2044. * nfp_net_write_mac_addr() - Write mac address to the device control BAR
  2045. * @nn: NFP Net device to reconfigure
  2046. * @addr: MAC address to write
  2047. *
  2048. * Writes the MAC address from the netdev to the device control BAR. Does not
  2049. * perform the required reconfig. We do a bit of byte swapping dance because
  2050. * firmware is LE.
  2051. */
  2052. static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
  2053. {
  2054. nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
  2055. nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
  2056. }
  2057. static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
  2058. {
  2059. nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
  2060. nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
  2061. nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
  2062. nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
  2063. nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
  2064. nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
  2065. }
  2066. /**
  2067. * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
  2068. * @nn: NFP Net device to reconfigure
  2069. */
  2070. static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
  2071. {
  2072. u32 new_ctrl, update;
  2073. unsigned int r;
  2074. int err;
  2075. new_ctrl = nn->dp.ctrl;
  2076. new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
  2077. update = NFP_NET_CFG_UPDATE_GEN;
  2078. update |= NFP_NET_CFG_UPDATE_MSIX;
  2079. update |= NFP_NET_CFG_UPDATE_RING;
  2080. if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
  2081. new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
  2082. nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
  2083. nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
  2084. nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
  2085. err = nfp_net_reconfig(nn, update);
  2086. if (err)
  2087. nn_err(nn, "Could not disable device: %d\n", err);
  2088. for (r = 0; r < nn->dp.num_rx_rings; r++)
  2089. nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
  2090. for (r = 0; r < nn->dp.num_tx_rings; r++)
  2091. nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
  2092. for (r = 0; r < nn->dp.num_r_vecs; r++)
  2093. nfp_net_vec_clear_ring_data(nn, r);
  2094. nn->dp.ctrl = new_ctrl;
  2095. }
  2096. static void
  2097. nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
  2098. struct nfp_net_rx_ring *rx_ring, unsigned int idx)
  2099. {
  2100. /* Write the DMA address, size and MSI-X info to the device */
  2101. nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
  2102. nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
  2103. nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_entry);
  2104. }
  2105. static void
  2106. nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
  2107. struct nfp_net_tx_ring *tx_ring, unsigned int idx)
  2108. {
  2109. nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
  2110. nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
  2111. nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_entry);
  2112. }
  2113. /**
  2114. * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
  2115. * @nn: NFP Net device to reconfigure
  2116. */
  2117. static int nfp_net_set_config_and_enable(struct nfp_net *nn)
  2118. {
  2119. u32 bufsz, new_ctrl, update = 0;
  2120. unsigned int r;
  2121. int err;
  2122. new_ctrl = nn->dp.ctrl;
  2123. if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
  2124. nfp_net_rss_write_key(nn);
  2125. nfp_net_rss_write_itbl(nn);
  2126. nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
  2127. update |= NFP_NET_CFG_UPDATE_RSS;
  2128. }
  2129. if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
  2130. nfp_net_coalesce_write_cfg(nn);
  2131. update |= NFP_NET_CFG_UPDATE_IRQMOD;
  2132. }
  2133. for (r = 0; r < nn->dp.num_tx_rings; r++)
  2134. nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
  2135. for (r = 0; r < nn->dp.num_rx_rings; r++)
  2136. nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
  2137. nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->dp.num_tx_rings == 64 ?
  2138. 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_tx_rings) - 1);
  2139. nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->dp.num_rx_rings == 64 ?
  2140. 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_rx_rings) - 1);
  2141. if (nn->dp.netdev)
  2142. nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
  2143. nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
  2144. bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
  2145. nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
  2146. /* Enable device */
  2147. new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
  2148. update |= NFP_NET_CFG_UPDATE_GEN;
  2149. update |= NFP_NET_CFG_UPDATE_MSIX;
  2150. update |= NFP_NET_CFG_UPDATE_RING;
  2151. if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
  2152. new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
  2153. nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
  2154. err = nfp_net_reconfig(nn, update);
  2155. if (err) {
  2156. nfp_net_clear_config_and_disable(nn);
  2157. return err;
  2158. }
  2159. nn->dp.ctrl = new_ctrl;
  2160. for (r = 0; r < nn->dp.num_rx_rings; r++)
  2161. nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
  2162. /* Since reconfiguration requests while NFP is down are ignored we
  2163. * have to wipe the entire VXLAN configuration and reinitialize it.
  2164. */
  2165. if (nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN) {
  2166. memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
  2167. memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
  2168. udp_tunnel_get_rx_info(nn->dp.netdev);
  2169. }
  2170. return 0;
  2171. }
  2172. /**
  2173. * nfp_net_close_stack() - Quiesce the stack (part of close)
  2174. * @nn: NFP Net device to reconfigure
  2175. */
  2176. static void nfp_net_close_stack(struct nfp_net *nn)
  2177. {
  2178. unsigned int r;
  2179. disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
  2180. netif_carrier_off(nn->dp.netdev);
  2181. nn->link_up = false;
  2182. for (r = 0; r < nn->dp.num_r_vecs; r++) {
  2183. disable_irq(nn->r_vecs[r].irq_vector);
  2184. napi_disable(&nn->r_vecs[r].napi);
  2185. }
  2186. netif_tx_disable(nn->dp.netdev);
  2187. }
  2188. /**
  2189. * nfp_net_close_free_all() - Free all runtime resources
  2190. * @nn: NFP Net device to reconfigure
  2191. */
  2192. static void nfp_net_close_free_all(struct nfp_net *nn)
  2193. {
  2194. unsigned int r;
  2195. nfp_net_tx_rings_free(&nn->dp);
  2196. nfp_net_rx_rings_free(&nn->dp);
  2197. for (r = 0; r < nn->dp.num_r_vecs; r++)
  2198. nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
  2199. nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
  2200. nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
  2201. }
  2202. /**
  2203. * nfp_net_netdev_close() - Called when the device is downed
  2204. * @netdev: netdev structure
  2205. */
  2206. static int nfp_net_netdev_close(struct net_device *netdev)
  2207. {
  2208. struct nfp_net *nn = netdev_priv(netdev);
  2209. /* Step 1: Disable RX and TX rings from the Linux kernel perspective
  2210. */
  2211. nfp_net_close_stack(nn);
  2212. /* Step 2: Tell NFP
  2213. */
  2214. nfp_net_clear_config_and_disable(nn);
  2215. nfp_port_configure(netdev, false);
  2216. /* Step 3: Free resources
  2217. */
  2218. nfp_net_close_free_all(nn);
  2219. nn_dbg(nn, "%s down", netdev->name);
  2220. return 0;
  2221. }
  2222. void nfp_ctrl_close(struct nfp_net *nn)
  2223. {
  2224. int r;
  2225. rtnl_lock();
  2226. for (r = 0; r < nn->dp.num_r_vecs; r++) {
  2227. disable_irq(nn->r_vecs[r].irq_vector);
  2228. tasklet_disable(&nn->r_vecs[r].tasklet);
  2229. }
  2230. nfp_net_clear_config_and_disable(nn);
  2231. nfp_net_close_free_all(nn);
  2232. rtnl_unlock();
  2233. }
  2234. /**
  2235. * nfp_net_open_stack() - Start the device from stack's perspective
  2236. * @nn: NFP Net device to reconfigure
  2237. */
  2238. static void nfp_net_open_stack(struct nfp_net *nn)
  2239. {
  2240. unsigned int r;
  2241. for (r = 0; r < nn->dp.num_r_vecs; r++) {
  2242. napi_enable(&nn->r_vecs[r].napi);
  2243. enable_irq(nn->r_vecs[r].irq_vector);
  2244. }
  2245. netif_tx_wake_all_queues(nn->dp.netdev);
  2246. enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
  2247. nfp_net_read_link_status(nn);
  2248. }
  2249. static int nfp_net_open_alloc_all(struct nfp_net *nn)
  2250. {
  2251. int err, r;
  2252. err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
  2253. nn->exn_name, sizeof(nn->exn_name),
  2254. NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
  2255. if (err)
  2256. return err;
  2257. err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
  2258. nn->lsc_name, sizeof(nn->lsc_name),
  2259. NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
  2260. if (err)
  2261. goto err_free_exn;
  2262. disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
  2263. for (r = 0; r < nn->dp.num_r_vecs; r++) {
  2264. err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
  2265. if (err)
  2266. goto err_cleanup_vec_p;
  2267. }
  2268. err = nfp_net_rx_rings_prepare(nn, &nn->dp);
  2269. if (err)
  2270. goto err_cleanup_vec;
  2271. err = nfp_net_tx_rings_prepare(nn, &nn->dp);
  2272. if (err)
  2273. goto err_free_rx_rings;
  2274. for (r = 0; r < nn->max_r_vecs; r++)
  2275. nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
  2276. return 0;
  2277. err_free_rx_rings:
  2278. nfp_net_rx_rings_free(&nn->dp);
  2279. err_cleanup_vec:
  2280. r = nn->dp.num_r_vecs;
  2281. err_cleanup_vec_p:
  2282. while (r--)
  2283. nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
  2284. nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
  2285. err_free_exn:
  2286. nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
  2287. return err;
  2288. }
  2289. static int nfp_net_netdev_open(struct net_device *netdev)
  2290. {
  2291. struct nfp_net *nn = netdev_priv(netdev);
  2292. int err;
  2293. /* Step 1: Allocate resources for rings and the like
  2294. * - Request interrupts
  2295. * - Allocate RX and TX ring resources
  2296. * - Setup initial RSS table
  2297. */
  2298. err = nfp_net_open_alloc_all(nn);
  2299. if (err)
  2300. return err;
  2301. err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
  2302. if (err)
  2303. goto err_free_all;
  2304. err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
  2305. if (err)
  2306. goto err_free_all;
  2307. /* Step 2: Configure the NFP
  2308. * - Ifup the physical interface if it exists
  2309. * - Enable rings from 0 to tx_rings/rx_rings - 1.
  2310. * - Write MAC address (in case it changed)
  2311. * - Set the MTU
  2312. * - Set the Freelist buffer size
  2313. * - Enable the FW
  2314. */
  2315. err = nfp_port_configure(netdev, true);
  2316. if (err)
  2317. goto err_free_all;
  2318. err = nfp_net_set_config_and_enable(nn);
  2319. if (err)
  2320. goto err_port_disable;
  2321. /* Step 3: Enable for kernel
  2322. * - put some freelist descriptors on each RX ring
  2323. * - enable NAPI on each ring
  2324. * - enable all TX queues
  2325. * - set link state
  2326. */
  2327. nfp_net_open_stack(nn);
  2328. return 0;
  2329. err_port_disable:
  2330. nfp_port_configure(netdev, false);
  2331. err_free_all:
  2332. nfp_net_close_free_all(nn);
  2333. return err;
  2334. }
  2335. int nfp_ctrl_open(struct nfp_net *nn)
  2336. {
  2337. int err, r;
  2338. /* ring dumping depends on vNICs being opened/closed under rtnl */
  2339. rtnl_lock();
  2340. err = nfp_net_open_alloc_all(nn);
  2341. if (err)
  2342. goto err_unlock;
  2343. err = nfp_net_set_config_and_enable(nn);
  2344. if (err)
  2345. goto err_free_all;
  2346. for (r = 0; r < nn->dp.num_r_vecs; r++)
  2347. enable_irq(nn->r_vecs[r].irq_vector);
  2348. rtnl_unlock();
  2349. return 0;
  2350. err_free_all:
  2351. nfp_net_close_free_all(nn);
  2352. err_unlock:
  2353. rtnl_unlock();
  2354. return err;
  2355. }
  2356. static void nfp_net_set_rx_mode(struct net_device *netdev)
  2357. {
  2358. struct nfp_net *nn = netdev_priv(netdev);
  2359. u32 new_ctrl;
  2360. new_ctrl = nn->dp.ctrl;
  2361. if (netdev->flags & IFF_PROMISC) {
  2362. if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
  2363. new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
  2364. else
  2365. nn_warn(nn, "FW does not support promiscuous mode\n");
  2366. } else {
  2367. new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
  2368. }
  2369. if (new_ctrl == nn->dp.ctrl)
  2370. return;
  2371. nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
  2372. nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
  2373. nn->dp.ctrl = new_ctrl;
  2374. }
  2375. static void nfp_net_rss_init_itbl(struct nfp_net *nn)
  2376. {
  2377. int i;
  2378. for (i = 0; i < sizeof(nn->rss_itbl); i++)
  2379. nn->rss_itbl[i] =
  2380. ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
  2381. }
  2382. static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
  2383. {
  2384. struct nfp_net_dp new_dp = *dp;
  2385. *dp = nn->dp;
  2386. nn->dp = new_dp;
  2387. nn->dp.netdev->mtu = new_dp.mtu;
  2388. if (!netif_is_rxfh_configured(nn->dp.netdev))
  2389. nfp_net_rss_init_itbl(nn);
  2390. }
  2391. static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
  2392. {
  2393. unsigned int r;
  2394. int err;
  2395. nfp_net_dp_swap(nn, dp);
  2396. for (r = 0; r < nn->max_r_vecs; r++)
  2397. nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
  2398. err = netif_set_real_num_rx_queues(nn->dp.netdev, nn->dp.num_rx_rings);
  2399. if (err)
  2400. return err;
  2401. if (nn->dp.netdev->real_num_tx_queues != nn->dp.num_stack_tx_rings) {
  2402. err = netif_set_real_num_tx_queues(nn->dp.netdev,
  2403. nn->dp.num_stack_tx_rings);
  2404. if (err)
  2405. return err;
  2406. }
  2407. return nfp_net_set_config_and_enable(nn);
  2408. }
  2409. struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
  2410. {
  2411. struct nfp_net_dp *new;
  2412. new = kmalloc(sizeof(*new), GFP_KERNEL);
  2413. if (!new)
  2414. return NULL;
  2415. *new = nn->dp;
  2416. /* Clear things which need to be recomputed */
  2417. new->fl_bufsz = 0;
  2418. new->tx_rings = NULL;
  2419. new->rx_rings = NULL;
  2420. new->num_r_vecs = 0;
  2421. new->num_stack_tx_rings = 0;
  2422. return new;
  2423. }
  2424. static int
  2425. nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
  2426. struct netlink_ext_ack *extack)
  2427. {
  2428. /* XDP-enabled tests */
  2429. if (!dp->xdp_prog)
  2430. return 0;
  2431. if (dp->fl_bufsz > PAGE_SIZE) {
  2432. NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
  2433. return -EINVAL;
  2434. }
  2435. if (dp->num_tx_rings > nn->max_tx_rings) {
  2436. NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
  2437. return -EINVAL;
  2438. }
  2439. return 0;
  2440. }
  2441. int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
  2442. struct netlink_ext_ack *extack)
  2443. {
  2444. int r, err;
  2445. dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
  2446. dp->num_stack_tx_rings = dp->num_tx_rings;
  2447. if (dp->xdp_prog)
  2448. dp->num_stack_tx_rings -= dp->num_rx_rings;
  2449. dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
  2450. err = nfp_net_check_config(nn, dp, extack);
  2451. if (err)
  2452. goto exit_free_dp;
  2453. if (!netif_running(dp->netdev)) {
  2454. nfp_net_dp_swap(nn, dp);
  2455. err = 0;
  2456. goto exit_free_dp;
  2457. }
  2458. /* Prepare new rings */
  2459. for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
  2460. err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
  2461. if (err) {
  2462. dp->num_r_vecs = r;
  2463. goto err_cleanup_vecs;
  2464. }
  2465. }
  2466. err = nfp_net_rx_rings_prepare(nn, dp);
  2467. if (err)
  2468. goto err_cleanup_vecs;
  2469. err = nfp_net_tx_rings_prepare(nn, dp);
  2470. if (err)
  2471. goto err_free_rx;
  2472. /* Stop device, swap in new rings, try to start the firmware */
  2473. nfp_net_close_stack(nn);
  2474. nfp_net_clear_config_and_disable(nn);
  2475. err = nfp_net_dp_swap_enable(nn, dp);
  2476. if (err) {
  2477. int err2;
  2478. nfp_net_clear_config_and_disable(nn);
  2479. /* Try with old configuration and old rings */
  2480. err2 = nfp_net_dp_swap_enable(nn, dp);
  2481. if (err2)
  2482. nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
  2483. err, err2);
  2484. }
  2485. for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
  2486. nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
  2487. nfp_net_rx_rings_free(dp);
  2488. nfp_net_tx_rings_free(dp);
  2489. nfp_net_open_stack(nn);
  2490. exit_free_dp:
  2491. kfree(dp);
  2492. return err;
  2493. err_free_rx:
  2494. nfp_net_rx_rings_free(dp);
  2495. err_cleanup_vecs:
  2496. for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
  2497. nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
  2498. kfree(dp);
  2499. return err;
  2500. }
  2501. static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
  2502. {
  2503. struct nfp_net *nn = netdev_priv(netdev);
  2504. struct nfp_net_dp *dp;
  2505. dp = nfp_net_clone_dp(nn);
  2506. if (!dp)
  2507. return -ENOMEM;
  2508. dp->mtu = new_mtu;
  2509. return nfp_net_ring_reconfig(nn, dp, NULL);
  2510. }
  2511. static int
  2512. nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
  2513. {
  2514. struct nfp_net *nn = netdev_priv(netdev);
  2515. /* Priority tagged packets with vlan id 0 are processed by the
  2516. * NFP as untagged packets
  2517. */
  2518. if (!vid)
  2519. return 0;
  2520. nn_writew(nn, NFP_NET_CFG_VLAN_FILTER_VID, vid);
  2521. nn_writew(nn, NFP_NET_CFG_VLAN_FILTER_PROTO, ETH_P_8021Q);
  2522. return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD);
  2523. }
  2524. static int
  2525. nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
  2526. {
  2527. struct nfp_net *nn = netdev_priv(netdev);
  2528. /* Priority tagged packets with vlan id 0 are processed by the
  2529. * NFP as untagged packets
  2530. */
  2531. if (!vid)
  2532. return 0;
  2533. nn_writew(nn, NFP_NET_CFG_VLAN_FILTER_VID, vid);
  2534. nn_writew(nn, NFP_NET_CFG_VLAN_FILTER_PROTO, ETH_P_8021Q);
  2535. return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL);
  2536. }
  2537. static void nfp_net_stat64(struct net_device *netdev,
  2538. struct rtnl_link_stats64 *stats)
  2539. {
  2540. struct nfp_net *nn = netdev_priv(netdev);
  2541. int r;
  2542. for (r = 0; r < nn->dp.num_r_vecs; r++) {
  2543. struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
  2544. u64 data[3];
  2545. unsigned int start;
  2546. do {
  2547. start = u64_stats_fetch_begin(&r_vec->rx_sync);
  2548. data[0] = r_vec->rx_pkts;
  2549. data[1] = r_vec->rx_bytes;
  2550. data[2] = r_vec->rx_drops;
  2551. } while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
  2552. stats->rx_packets += data[0];
  2553. stats->rx_bytes += data[1];
  2554. stats->rx_dropped += data[2];
  2555. do {
  2556. start = u64_stats_fetch_begin(&r_vec->tx_sync);
  2557. data[0] = r_vec->tx_pkts;
  2558. data[1] = r_vec->tx_bytes;
  2559. data[2] = r_vec->tx_errors;
  2560. } while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
  2561. stats->tx_packets += data[0];
  2562. stats->tx_bytes += data[1];
  2563. stats->tx_errors += data[2];
  2564. }
  2565. }
  2566. static int nfp_net_set_features(struct net_device *netdev,
  2567. netdev_features_t features)
  2568. {
  2569. netdev_features_t changed = netdev->features ^ features;
  2570. struct nfp_net *nn = netdev_priv(netdev);
  2571. u32 new_ctrl;
  2572. int err;
  2573. /* Assume this is not called with features we have not advertised */
  2574. new_ctrl = nn->dp.ctrl;
  2575. if (changed & NETIF_F_RXCSUM) {
  2576. if (features & NETIF_F_RXCSUM)
  2577. new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
  2578. else
  2579. new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
  2580. }
  2581. if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
  2582. if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
  2583. new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
  2584. else
  2585. new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
  2586. }
  2587. if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
  2588. if (features & (NETIF_F_TSO | NETIF_F_TSO6))
  2589. new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
  2590. NFP_NET_CFG_CTRL_LSO;
  2591. else
  2592. new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
  2593. }
  2594. if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
  2595. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2596. new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
  2597. else
  2598. new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
  2599. }
  2600. if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
  2601. if (features & NETIF_F_HW_VLAN_CTAG_TX)
  2602. new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
  2603. else
  2604. new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
  2605. }
  2606. if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
  2607. if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
  2608. new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
  2609. else
  2610. new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
  2611. }
  2612. if (changed & NETIF_F_SG) {
  2613. if (features & NETIF_F_SG)
  2614. new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
  2615. else
  2616. new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
  2617. }
  2618. if (changed & NETIF_F_HW_TC && nfp_app_tc_busy(nn->app, nn)) {
  2619. nn_err(nn, "Cannot disable HW TC offload while in use\n");
  2620. return -EBUSY;
  2621. }
  2622. nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
  2623. netdev->features, features, changed);
  2624. if (new_ctrl == nn->dp.ctrl)
  2625. return 0;
  2626. nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
  2627. nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
  2628. err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
  2629. if (err)
  2630. return err;
  2631. nn->dp.ctrl = new_ctrl;
  2632. return 0;
  2633. }
  2634. static netdev_features_t
  2635. nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
  2636. netdev_features_t features)
  2637. {
  2638. u8 l4_hdr;
  2639. /* We can't do TSO over double tagged packets (802.1AD) */
  2640. features &= vlan_features_check(skb, features);
  2641. if (!skb->encapsulation)
  2642. return features;
  2643. /* Ensure that inner L4 header offset fits into TX descriptor field */
  2644. if (skb_is_gso(skb)) {
  2645. u32 hdrlen;
  2646. hdrlen = skb_inner_transport_header(skb) - skb->data +
  2647. inner_tcp_hdrlen(skb);
  2648. if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ))
  2649. features &= ~NETIF_F_GSO_MASK;
  2650. }
  2651. /* VXLAN/GRE check */
  2652. switch (vlan_get_protocol(skb)) {
  2653. case htons(ETH_P_IP):
  2654. l4_hdr = ip_hdr(skb)->protocol;
  2655. break;
  2656. case htons(ETH_P_IPV6):
  2657. l4_hdr = ipv6_hdr(skb)->nexthdr;
  2658. break;
  2659. default:
  2660. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  2661. }
  2662. if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  2663. skb->inner_protocol != htons(ETH_P_TEB) ||
  2664. (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
  2665. (l4_hdr == IPPROTO_UDP &&
  2666. (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
  2667. sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
  2668. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  2669. return features;
  2670. }
  2671. /**
  2672. * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
  2673. * @nn: NFP Net device to reconfigure
  2674. * @idx: Index into the port table where new port should be written
  2675. * @port: UDP port to configure (pass zero to remove VXLAN port)
  2676. */
  2677. static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
  2678. {
  2679. int i;
  2680. nn->vxlan_ports[idx] = port;
  2681. if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN))
  2682. return;
  2683. BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
  2684. for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
  2685. nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
  2686. be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
  2687. be16_to_cpu(nn->vxlan_ports[i]));
  2688. nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
  2689. }
  2690. /**
  2691. * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
  2692. * @nn: NFP Network structure
  2693. * @port: UDP port to look for
  2694. *
  2695. * Return: if the port is already in the table -- it's position;
  2696. * if the port is not in the table -- free position to use;
  2697. * if the table is full -- -ENOSPC.
  2698. */
  2699. static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
  2700. {
  2701. int i, free_idx = -ENOSPC;
  2702. for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
  2703. if (nn->vxlan_ports[i] == port)
  2704. return i;
  2705. if (!nn->vxlan_usecnt[i])
  2706. free_idx = i;
  2707. }
  2708. return free_idx;
  2709. }
  2710. static void nfp_net_add_vxlan_port(struct net_device *netdev,
  2711. struct udp_tunnel_info *ti)
  2712. {
  2713. struct nfp_net *nn = netdev_priv(netdev);
  2714. int idx;
  2715. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  2716. return;
  2717. idx = nfp_net_find_vxlan_idx(nn, ti->port);
  2718. if (idx == -ENOSPC)
  2719. return;
  2720. if (!nn->vxlan_usecnt[idx]++)
  2721. nfp_net_set_vxlan_port(nn, idx, ti->port);
  2722. }
  2723. static void nfp_net_del_vxlan_port(struct net_device *netdev,
  2724. struct udp_tunnel_info *ti)
  2725. {
  2726. struct nfp_net *nn = netdev_priv(netdev);
  2727. int idx;
  2728. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  2729. return;
  2730. idx = nfp_net_find_vxlan_idx(nn, ti->port);
  2731. if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
  2732. return;
  2733. if (!--nn->vxlan_usecnt[idx])
  2734. nfp_net_set_vxlan_port(nn, idx, 0);
  2735. }
  2736. static int
  2737. nfp_net_xdp_setup_drv(struct nfp_net *nn, struct bpf_prog *prog,
  2738. struct netlink_ext_ack *extack)
  2739. {
  2740. struct nfp_net_dp *dp;
  2741. if (!prog == !nn->dp.xdp_prog) {
  2742. WRITE_ONCE(nn->dp.xdp_prog, prog);
  2743. return 0;
  2744. }
  2745. dp = nfp_net_clone_dp(nn);
  2746. if (!dp)
  2747. return -ENOMEM;
  2748. dp->xdp_prog = prog;
  2749. dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
  2750. dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
  2751. dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
  2752. /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
  2753. return nfp_net_ring_reconfig(nn, dp, extack);
  2754. }
  2755. static int
  2756. nfp_net_xdp_setup(struct nfp_net *nn, struct bpf_prog *prog, u32 flags,
  2757. struct netlink_ext_ack *extack)
  2758. {
  2759. struct bpf_prog *drv_prog, *offload_prog;
  2760. int err;
  2761. if (nn->xdp_prog && (flags ^ nn->xdp_flags) & XDP_FLAGS_MODES)
  2762. return -EBUSY;
  2763. /* Load both when no flags set to allow easy activation of driver path
  2764. * when program is replaced by one which can't be offloaded.
  2765. */
  2766. drv_prog = flags & XDP_FLAGS_HW_MODE ? NULL : prog;
  2767. offload_prog = flags & XDP_FLAGS_DRV_MODE ? NULL : prog;
  2768. err = nfp_net_xdp_setup_drv(nn, drv_prog, extack);
  2769. if (err)
  2770. return err;
  2771. err = nfp_app_xdp_offload(nn->app, nn, offload_prog);
  2772. if (err && flags & XDP_FLAGS_HW_MODE)
  2773. return err;
  2774. if (nn->xdp_prog)
  2775. bpf_prog_put(nn->xdp_prog);
  2776. nn->xdp_prog = prog;
  2777. nn->xdp_flags = flags;
  2778. return 0;
  2779. }
  2780. static int nfp_net_xdp(struct net_device *netdev, struct netdev_xdp *xdp)
  2781. {
  2782. struct nfp_net *nn = netdev_priv(netdev);
  2783. switch (xdp->command) {
  2784. case XDP_SETUP_PROG:
  2785. case XDP_SETUP_PROG_HW:
  2786. return nfp_net_xdp_setup(nn, xdp->prog, xdp->flags,
  2787. xdp->extack);
  2788. case XDP_QUERY_PROG:
  2789. xdp->prog_attached = !!nn->xdp_prog;
  2790. if (nn->dp.bpf_offload_xdp)
  2791. xdp->prog_attached = XDP_ATTACHED_HW;
  2792. xdp->prog_id = nn->xdp_prog ? nn->xdp_prog->aux->id : 0;
  2793. return 0;
  2794. default:
  2795. return -EINVAL;
  2796. }
  2797. }
  2798. static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
  2799. {
  2800. struct nfp_net *nn = netdev_priv(netdev);
  2801. struct sockaddr *saddr = addr;
  2802. int err;
  2803. err = eth_prepare_mac_addr_change(netdev, addr);
  2804. if (err)
  2805. return err;
  2806. nfp_net_write_mac_addr(nn, saddr->sa_data);
  2807. err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
  2808. if (err)
  2809. return err;
  2810. eth_commit_mac_addr_change(netdev, addr);
  2811. return 0;
  2812. }
  2813. const struct net_device_ops nfp_net_netdev_ops = {
  2814. .ndo_open = nfp_net_netdev_open,
  2815. .ndo_stop = nfp_net_netdev_close,
  2816. .ndo_start_xmit = nfp_net_tx,
  2817. .ndo_get_stats64 = nfp_net_stat64,
  2818. .ndo_vlan_rx_add_vid = nfp_net_vlan_rx_add_vid,
  2819. .ndo_vlan_rx_kill_vid = nfp_net_vlan_rx_kill_vid,
  2820. .ndo_setup_tc = nfp_port_setup_tc,
  2821. .ndo_tx_timeout = nfp_net_tx_timeout,
  2822. .ndo_set_rx_mode = nfp_net_set_rx_mode,
  2823. .ndo_change_mtu = nfp_net_change_mtu,
  2824. .ndo_set_mac_address = nfp_net_set_mac_address,
  2825. .ndo_set_features = nfp_net_set_features,
  2826. .ndo_features_check = nfp_net_features_check,
  2827. .ndo_get_phys_port_name = nfp_port_get_phys_port_name,
  2828. .ndo_udp_tunnel_add = nfp_net_add_vxlan_port,
  2829. .ndo_udp_tunnel_del = nfp_net_del_vxlan_port,
  2830. .ndo_xdp = nfp_net_xdp,
  2831. };
  2832. /**
  2833. * nfp_net_info() - Print general info about the NIC
  2834. * @nn: NFP Net device to reconfigure
  2835. */
  2836. void nfp_net_info(struct nfp_net *nn)
  2837. {
  2838. nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
  2839. nn->dp.is_vf ? "VF " : "",
  2840. nn->dp.num_tx_rings, nn->max_tx_rings,
  2841. nn->dp.num_rx_rings, nn->max_rx_rings);
  2842. nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
  2843. nn->fw_ver.resv, nn->fw_ver.class,
  2844. nn->fw_ver.major, nn->fw_ver.minor,
  2845. nn->max_mtu);
  2846. nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
  2847. nn->cap,
  2848. nn->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
  2849. nn->cap & NFP_NET_CFG_CTRL_L2BC ? "L2BCFILT " : "",
  2850. nn->cap & NFP_NET_CFG_CTRL_L2MC ? "L2MCFILT " : "",
  2851. nn->cap & NFP_NET_CFG_CTRL_RXCSUM ? "RXCSUM " : "",
  2852. nn->cap & NFP_NET_CFG_CTRL_TXCSUM ? "TXCSUM " : "",
  2853. nn->cap & NFP_NET_CFG_CTRL_RXVLAN ? "RXVLAN " : "",
  2854. nn->cap & NFP_NET_CFG_CTRL_TXVLAN ? "TXVLAN " : "",
  2855. nn->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
  2856. nn->cap & NFP_NET_CFG_CTRL_GATHER ? "GATHER " : "",
  2857. nn->cap & NFP_NET_CFG_CTRL_LSO ? "TSO1 " : "",
  2858. nn->cap & NFP_NET_CFG_CTRL_LSO2 ? "TSO2 " : "",
  2859. nn->cap & NFP_NET_CFG_CTRL_RSS ? "RSS1 " : "",
  2860. nn->cap & NFP_NET_CFG_CTRL_RSS2 ? "RSS2 " : "",
  2861. nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
  2862. nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
  2863. nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
  2864. nn->cap & NFP_NET_CFG_CTRL_IRQMOD ? "IRQMOD " : "",
  2865. nn->cap & NFP_NET_CFG_CTRL_VXLAN ? "VXLAN " : "",
  2866. nn->cap & NFP_NET_CFG_CTRL_NVGRE ? "NVGRE " : "",
  2867. nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
  2868. "RXCSUM_COMPLETE " : "",
  2869. nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
  2870. nfp_app_extra_cap(nn->app, nn));
  2871. }
  2872. /**
  2873. * nfp_net_alloc() - Allocate netdev and related structure
  2874. * @pdev: PCI device
  2875. * @needs_netdev: Whether to allocate a netdev for this vNIC
  2876. * @max_tx_rings: Maximum number of TX rings supported by device
  2877. * @max_rx_rings: Maximum number of RX rings supported by device
  2878. *
  2879. * This function allocates a netdev device and fills in the initial
  2880. * part of the @struct nfp_net structure. In case of control device
  2881. * nfp_net structure is allocated without the netdev.
  2882. *
  2883. * Return: NFP Net device structure, or ERR_PTR on error.
  2884. */
  2885. struct nfp_net *nfp_net_alloc(struct pci_dev *pdev, bool needs_netdev,
  2886. unsigned int max_tx_rings,
  2887. unsigned int max_rx_rings)
  2888. {
  2889. struct nfp_net *nn;
  2890. if (needs_netdev) {
  2891. struct net_device *netdev;
  2892. netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
  2893. max_tx_rings, max_rx_rings);
  2894. if (!netdev)
  2895. return ERR_PTR(-ENOMEM);
  2896. SET_NETDEV_DEV(netdev, &pdev->dev);
  2897. nn = netdev_priv(netdev);
  2898. nn->dp.netdev = netdev;
  2899. } else {
  2900. nn = vzalloc(sizeof(*nn));
  2901. if (!nn)
  2902. return ERR_PTR(-ENOMEM);
  2903. }
  2904. nn->dp.dev = &pdev->dev;
  2905. nn->pdev = pdev;
  2906. nn->max_tx_rings = max_tx_rings;
  2907. nn->max_rx_rings = max_rx_rings;
  2908. nn->dp.num_tx_rings = min_t(unsigned int,
  2909. max_tx_rings, num_online_cpus());
  2910. nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
  2911. netif_get_num_default_rss_queues());
  2912. nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
  2913. nn->dp.num_r_vecs = min_t(unsigned int,
  2914. nn->dp.num_r_vecs, num_online_cpus());
  2915. nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
  2916. nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
  2917. spin_lock_init(&nn->reconfig_lock);
  2918. spin_lock_init(&nn->link_status_lock);
  2919. setup_timer(&nn->reconfig_timer,
  2920. nfp_net_reconfig_timer, (unsigned long)nn);
  2921. return nn;
  2922. }
  2923. /**
  2924. * nfp_net_free() - Undo what @nfp_net_alloc() did
  2925. * @nn: NFP Net device to reconfigure
  2926. */
  2927. void nfp_net_free(struct nfp_net *nn)
  2928. {
  2929. if (nn->xdp_prog)
  2930. bpf_prog_put(nn->xdp_prog);
  2931. if (nn->dp.netdev)
  2932. free_netdev(nn->dp.netdev);
  2933. else
  2934. vfree(nn);
  2935. }
  2936. /**
  2937. * nfp_net_rss_key_sz() - Get current size of the RSS key
  2938. * @nn: NFP Net device instance
  2939. *
  2940. * Return: size of the RSS key for currently selected hash function.
  2941. */
  2942. unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
  2943. {
  2944. switch (nn->rss_hfunc) {
  2945. case ETH_RSS_HASH_TOP:
  2946. return NFP_NET_CFG_RSS_KEY_SZ;
  2947. case ETH_RSS_HASH_XOR:
  2948. return 0;
  2949. case ETH_RSS_HASH_CRC32:
  2950. return 4;
  2951. }
  2952. nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
  2953. return 0;
  2954. }
  2955. /**
  2956. * nfp_net_rss_init() - Set the initial RSS parameters
  2957. * @nn: NFP Net device to reconfigure
  2958. */
  2959. static void nfp_net_rss_init(struct nfp_net *nn)
  2960. {
  2961. unsigned long func_bit, rss_cap_hfunc;
  2962. u32 reg;
  2963. /* Read the RSS function capability and select first supported func */
  2964. reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
  2965. rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
  2966. if (!rss_cap_hfunc)
  2967. rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
  2968. NFP_NET_CFG_RSS_TOEPLITZ);
  2969. func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
  2970. if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
  2971. dev_warn(nn->dp.dev,
  2972. "Bad RSS config, defaulting to Toeplitz hash\n");
  2973. func_bit = ETH_RSS_HASH_TOP_BIT;
  2974. }
  2975. nn->rss_hfunc = 1 << func_bit;
  2976. netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
  2977. nfp_net_rss_init_itbl(nn);
  2978. /* Enable IPv4/IPv6 TCP by default */
  2979. nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
  2980. NFP_NET_CFG_RSS_IPV6_TCP |
  2981. FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
  2982. NFP_NET_CFG_RSS_MASK;
  2983. }
  2984. /**
  2985. * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
  2986. * @nn: NFP Net device to reconfigure
  2987. */
  2988. static void nfp_net_irqmod_init(struct nfp_net *nn)
  2989. {
  2990. nn->rx_coalesce_usecs = 50;
  2991. nn->rx_coalesce_max_frames = 64;
  2992. nn->tx_coalesce_usecs = 50;
  2993. nn->tx_coalesce_max_frames = 64;
  2994. }
  2995. static void nfp_net_netdev_init(struct nfp_net *nn)
  2996. {
  2997. struct net_device *netdev = nn->dp.netdev;
  2998. nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
  2999. netdev->mtu = nn->dp.mtu;
  3000. /* Advertise/enable offloads based on capabilities
  3001. *
  3002. * Note: netdev->features show the currently enabled features
  3003. * and netdev->hw_features advertises which features are
  3004. * supported. By default we enable most features.
  3005. */
  3006. if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
  3007. netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  3008. netdev->hw_features = NETIF_F_HIGHDMA;
  3009. if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
  3010. netdev->hw_features |= NETIF_F_RXCSUM;
  3011. nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
  3012. }
  3013. if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
  3014. netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  3015. nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
  3016. }
  3017. if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
  3018. netdev->hw_features |= NETIF_F_SG;
  3019. nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
  3020. }
  3021. if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
  3022. nn->cap & NFP_NET_CFG_CTRL_LSO2) {
  3023. netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
  3024. nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
  3025. NFP_NET_CFG_CTRL_LSO;
  3026. }
  3027. if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
  3028. netdev->hw_features |= NETIF_F_RXHASH;
  3029. if (nn->cap & NFP_NET_CFG_CTRL_VXLAN &&
  3030. nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
  3031. if (nn->cap & NFP_NET_CFG_CTRL_LSO)
  3032. netdev->hw_features |= NETIF_F_GSO_GRE |
  3033. NETIF_F_GSO_UDP_TUNNEL;
  3034. nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE;
  3035. netdev->hw_enc_features = netdev->hw_features;
  3036. }
  3037. netdev->vlan_features = netdev->hw_features;
  3038. if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
  3039. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
  3040. nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
  3041. }
  3042. if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
  3043. if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
  3044. nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
  3045. } else {
  3046. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
  3047. nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
  3048. }
  3049. }
  3050. if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
  3051. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  3052. nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
  3053. }
  3054. netdev->features = netdev->hw_features;
  3055. if (nfp_app_has_tc(nn->app))
  3056. netdev->hw_features |= NETIF_F_HW_TC;
  3057. /* Advertise but disable TSO by default. */
  3058. netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
  3059. nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
  3060. /* Finalise the netdev setup */
  3061. netdev->netdev_ops = &nfp_net_netdev_ops;
  3062. netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
  3063. SWITCHDEV_SET_OPS(netdev, &nfp_port_switchdev_ops);
  3064. /* MTU range: 68 - hw-specific max */
  3065. netdev->min_mtu = ETH_MIN_MTU;
  3066. netdev->max_mtu = nn->max_mtu;
  3067. netif_carrier_off(netdev);
  3068. nfp_net_set_ethtool_ops(netdev);
  3069. }
  3070. /**
  3071. * nfp_net_init() - Initialise/finalise the nfp_net structure
  3072. * @nn: NFP Net device structure
  3073. *
  3074. * Return: 0 on success or negative errno on error.
  3075. */
  3076. int nfp_net_init(struct nfp_net *nn)
  3077. {
  3078. int err;
  3079. nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
  3080. /* Get some of the read-only fields from the BAR */
  3081. nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
  3082. nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
  3083. /* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
  3084. * we allow use of non-chained metadata if RSS(v1) is the only
  3085. * advertised capability requiring metadata.
  3086. */
  3087. nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
  3088. !nn->dp.netdev ||
  3089. !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
  3090. nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
  3091. /* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
  3092. * it has the same meaning as RSSv2.
  3093. */
  3094. if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
  3095. nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
  3096. /* Determine RX packet/metadata boundary offset */
  3097. if (nn->fw_ver.major >= 2) {
  3098. u32 reg;
  3099. reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
  3100. if (reg > NFP_NET_MAX_PREPEND) {
  3101. nn_err(nn, "Invalid rx offset: %d\n", reg);
  3102. return -EINVAL;
  3103. }
  3104. nn->dp.rx_offset = reg;
  3105. } else {
  3106. nn->dp.rx_offset = NFP_NET_RX_OFFSET;
  3107. }
  3108. /* Set default MTU and Freelist buffer size */
  3109. if (nn->max_mtu < NFP_NET_DEFAULT_MTU)
  3110. nn->dp.mtu = nn->max_mtu;
  3111. else
  3112. nn->dp.mtu = NFP_NET_DEFAULT_MTU;
  3113. nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
  3114. if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
  3115. nfp_net_rss_init(nn);
  3116. nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
  3117. NFP_NET_CFG_CTRL_RSS;
  3118. }
  3119. /* Allow L2 Broadcast and Multicast through by default, if supported */
  3120. if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
  3121. nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
  3122. if (nn->cap & NFP_NET_CFG_CTRL_L2MC)
  3123. nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2MC;
  3124. /* Allow IRQ moderation, if supported */
  3125. if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
  3126. nfp_net_irqmod_init(nn);
  3127. nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
  3128. }
  3129. if (nn->dp.netdev)
  3130. nfp_net_netdev_init(nn);
  3131. /* Stash the re-configuration queue away. First odd queue in TX Bar */
  3132. nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
  3133. /* Make sure the FW knows the netdev is supposed to be disabled here */
  3134. nn_writel(nn, NFP_NET_CFG_CTRL, 0);
  3135. nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
  3136. nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
  3137. err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
  3138. NFP_NET_CFG_UPDATE_GEN);
  3139. if (err)
  3140. return err;
  3141. nfp_net_vecs_init(nn);
  3142. if (!nn->dp.netdev)
  3143. return 0;
  3144. return register_netdev(nn->dp.netdev);
  3145. }
  3146. /**
  3147. * nfp_net_clean() - Undo what nfp_net_init() did.
  3148. * @nn: NFP Net device structure
  3149. */
  3150. void nfp_net_clean(struct nfp_net *nn)
  3151. {
  3152. if (!nn->dp.netdev)
  3153. return;
  3154. unregister_netdev(nn->dp.netdev);
  3155. }