nfp_net_common.c 99 KB

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