1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277 |
- /*
- * Copyright (C) 2015 Netronome Systems, Inc.
- *
- * This software is dual licensed under the GNU General License Version 2,
- * June 1991 as shown in the file COPYING in the top-level directory of this
- * source tree or the BSD 2-Clause License provided below. You have the
- * option to license this software under the complete terms of either license.
- *
- * The BSD 2-Clause License:
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * 1. Redistributions of source code must retain the above
- * copyright notice, this list of conditions and the following
- * disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials
- * provided with the distribution.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
- /*
- * nfp_net_common.c
- * Netronome network device driver: Common functions between PF and VF
- * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
- * Jason McMullan <jason.mcmullan@netronome.com>
- * Rolf Neugebauer <rolf.neugebauer@netronome.com>
- * Brad Petrus <brad.petrus@netronome.com>
- * Chris Telfer <chris.telfer@netronome.com>
- */
- #include <linux/bpf.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/fs.h>
- #include <linux/netdevice.h>
- #include <linux/etherdevice.h>
- #include <linux/interrupt.h>
- #include <linux/ip.h>
- #include <linux/ipv6.h>
- #include <linux/page_ref.h>
- #include <linux/pci.h>
- #include <linux/pci_regs.h>
- #include <linux/msi.h>
- #include <linux/ethtool.h>
- #include <linux/log2.h>
- #include <linux/if_vlan.h>
- #include <linux/random.h>
- #include <linux/ktime.h>
- #include <net/pkt_cls.h>
- #include <net/vxlan.h>
- #include "nfp_net_ctrl.h"
- #include "nfp_net.h"
- /**
- * nfp_net_get_fw_version() - Read and parse the FW version
- * @fw_ver: Output fw_version structure to read to
- * @ctrl_bar: Mapped address of the control BAR
- */
- void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
- void __iomem *ctrl_bar)
- {
- u32 reg;
- reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
- put_unaligned_le32(reg, fw_ver);
- }
- static dma_addr_t
- nfp_net_dma_map_rx(struct nfp_net *nn, void *frag, unsigned int bufsz,
- int direction)
- {
- return dma_map_single(&nn->pdev->dev, frag + NFP_NET_RX_BUF_HEADROOM,
- bufsz - NFP_NET_RX_BUF_NON_DATA, direction);
- }
- static void
- nfp_net_dma_unmap_rx(struct nfp_net *nn, dma_addr_t dma_addr,
- unsigned int bufsz, int direction)
- {
- dma_unmap_single(&nn->pdev->dev, dma_addr,
- bufsz - NFP_NET_RX_BUF_NON_DATA, direction);
- }
- /* Firmware reconfig
- *
- * Firmware reconfig may take a while so we have two versions of it -
- * synchronous and asynchronous (posted). All synchronous callers are holding
- * RTNL so we don't have to worry about serializing them.
- */
- static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
- {
- nn_writel(nn, NFP_NET_CFG_UPDATE, update);
- /* ensure update is written before pinging HW */
- nn_pci_flush(nn);
- nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
- }
- /* Pass 0 as update to run posted reconfigs. */
- static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
- {
- update |= nn->reconfig_posted;
- nn->reconfig_posted = 0;
- nfp_net_reconfig_start(nn, update);
- nn->reconfig_timer_active = true;
- mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
- }
- static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
- {
- u32 reg;
- reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
- if (reg == 0)
- return true;
- if (reg & NFP_NET_CFG_UPDATE_ERR) {
- nn_err(nn, "Reconfig error: 0x%08x\n", reg);
- return true;
- } else if (last_check) {
- nn_err(nn, "Reconfig timeout: 0x%08x\n", reg);
- return true;
- }
- return false;
- }
- static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
- {
- bool timed_out = false;
- /* Poll update field, waiting for NFP to ack the config */
- while (!nfp_net_reconfig_check_done(nn, timed_out)) {
- msleep(1);
- timed_out = time_is_before_eq_jiffies(deadline);
- }
- if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
- return -EIO;
- return timed_out ? -EIO : 0;
- }
- static void nfp_net_reconfig_timer(unsigned long data)
- {
- struct nfp_net *nn = (void *)data;
- spin_lock_bh(&nn->reconfig_lock);
- nn->reconfig_timer_active = false;
- /* If sync caller is present it will take over from us */
- if (nn->reconfig_sync_present)
- goto done;
- /* Read reconfig status and report errors */
- nfp_net_reconfig_check_done(nn, true);
- if (nn->reconfig_posted)
- nfp_net_reconfig_start_async(nn, 0);
- done:
- spin_unlock_bh(&nn->reconfig_lock);
- }
- /**
- * nfp_net_reconfig_post() - Post async reconfig request
- * @nn: NFP Net device to reconfigure
- * @update: The value for the update field in the BAR config
- *
- * Record FW reconfiguration request. Reconfiguration will be kicked off
- * whenever reconfiguration machinery is idle. Multiple requests can be
- * merged together!
- */
- static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
- {
- spin_lock_bh(&nn->reconfig_lock);
- /* Sync caller will kick off async reconf when it's done, just post */
- if (nn->reconfig_sync_present) {
- nn->reconfig_posted |= update;
- goto done;
- }
- /* Opportunistically check if the previous command is done */
- if (!nn->reconfig_timer_active ||
- nfp_net_reconfig_check_done(nn, false))
- nfp_net_reconfig_start_async(nn, update);
- else
- nn->reconfig_posted |= update;
- done:
- spin_unlock_bh(&nn->reconfig_lock);
- }
- /**
- * nfp_net_reconfig() - Reconfigure the firmware
- * @nn: NFP Net device to reconfigure
- * @update: The value for the update field in the BAR config
- *
- * Write the update word to the BAR and ping the reconfig queue. The
- * poll until the firmware has acknowledged the update by zeroing the
- * update word.
- *
- * Return: Negative errno on error, 0 on success
- */
- int nfp_net_reconfig(struct nfp_net *nn, u32 update)
- {
- bool cancelled_timer = false;
- u32 pre_posted_requests;
- int ret;
- spin_lock_bh(&nn->reconfig_lock);
- nn->reconfig_sync_present = true;
- if (nn->reconfig_timer_active) {
- del_timer(&nn->reconfig_timer);
- nn->reconfig_timer_active = false;
- cancelled_timer = true;
- }
- pre_posted_requests = nn->reconfig_posted;
- nn->reconfig_posted = 0;
- spin_unlock_bh(&nn->reconfig_lock);
- if (cancelled_timer)
- nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
- /* Run the posted reconfigs which were issued before we started */
- if (pre_posted_requests) {
- nfp_net_reconfig_start(nn, pre_posted_requests);
- nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
- }
- nfp_net_reconfig_start(nn, update);
- ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
- spin_lock_bh(&nn->reconfig_lock);
- if (nn->reconfig_posted)
- nfp_net_reconfig_start_async(nn, 0);
- nn->reconfig_sync_present = false;
- spin_unlock_bh(&nn->reconfig_lock);
- return ret;
- }
- /* Interrupt configuration and handling
- */
- /**
- * nfp_net_irq_unmask() - Unmask automasked interrupt
- * @nn: NFP Network structure
- * @entry_nr: MSI-X table entry
- *
- * Clear the ICR for the IRQ entry.
- */
- static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
- {
- nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
- nn_pci_flush(nn);
- }
- /**
- * nfp_net_msix_alloc() - Try to allocate MSI-X irqs
- * @nn: NFP Network structure
- * @nr_vecs: Number of MSI-X vectors to allocate
- *
- * For MSI-X we want at least NFP_NET_NON_Q_VECTORS + 1 vectors.
- *
- * Return: Number of MSI-X vectors obtained or 0 on error.
- */
- static int nfp_net_msix_alloc(struct nfp_net *nn, int nr_vecs)
- {
- struct pci_dev *pdev = nn->pdev;
- int nvecs;
- int i;
- for (i = 0; i < nr_vecs; i++)
- nn->irq_entries[i].entry = i;
- nvecs = pci_enable_msix_range(pdev, nn->irq_entries,
- NFP_NET_NON_Q_VECTORS + 1, nr_vecs);
- if (nvecs < 0) {
- nn_warn(nn, "Failed to enable MSI-X. Wanted %d-%d (err=%d)\n",
- NFP_NET_NON_Q_VECTORS + 1, nr_vecs, nvecs);
- return 0;
- }
- return nvecs;
- }
- /**
- * nfp_net_irqs_alloc() - allocates MSI-X irqs
- * @nn: NFP Network structure
- *
- * Return: Number of irqs obtained or 0 on error.
- */
- int nfp_net_irqs_alloc(struct nfp_net *nn)
- {
- int wanted_irqs;
- unsigned int n;
- wanted_irqs = nn->num_r_vecs + NFP_NET_NON_Q_VECTORS;
- n = nfp_net_msix_alloc(nn, wanted_irqs);
- if (n == 0) {
- nn_err(nn, "Failed to allocate MSI-X IRQs\n");
- return 0;
- }
- nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
- nn->num_r_vecs = nn->max_r_vecs;
- if (n < wanted_irqs)
- nn_warn(nn, "Unable to allocate %d vectors. Got %d instead\n",
- wanted_irqs, n);
- return n;
- }
- /**
- * nfp_net_irqs_disable() - Disable interrupts
- * @nn: NFP Network structure
- *
- * Undoes what @nfp_net_irqs_alloc() does.
- */
- void nfp_net_irqs_disable(struct nfp_net *nn)
- {
- pci_disable_msix(nn->pdev);
- }
- /**
- * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
- * @irq: Interrupt
- * @data: Opaque data structure
- *
- * Return: Indicate if the interrupt has been handled.
- */
- static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
- {
- struct nfp_net_r_vector *r_vec = data;
- napi_schedule_irqoff(&r_vec->napi);
- /* The FW auto-masks any interrupt, either via the MASK bit in
- * the MSI-X table or via the per entry ICR field. So there
- * is no need to disable interrupts here.
- */
- return IRQ_HANDLED;
- }
- /**
- * nfp_net_read_link_status() - Reread link status from control BAR
- * @nn: NFP Network structure
- */
- static void nfp_net_read_link_status(struct nfp_net *nn)
- {
- unsigned long flags;
- bool link_up;
- u32 sts;
- spin_lock_irqsave(&nn->link_status_lock, flags);
- sts = nn_readl(nn, NFP_NET_CFG_STS);
- link_up = !!(sts & NFP_NET_CFG_STS_LINK);
- if (nn->link_up == link_up)
- goto out;
- nn->link_up = link_up;
- if (nn->link_up) {
- netif_carrier_on(nn->netdev);
- netdev_info(nn->netdev, "NIC Link is Up\n");
- } else {
- netif_carrier_off(nn->netdev);
- netdev_info(nn->netdev, "NIC Link is Down\n");
- }
- out:
- spin_unlock_irqrestore(&nn->link_status_lock, flags);
- }
- /**
- * nfp_net_irq_lsc() - Interrupt service routine for link state changes
- * @irq: Interrupt
- * @data: Opaque data structure
- *
- * Return: Indicate if the interrupt has been handled.
- */
- static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
- {
- struct nfp_net *nn = data;
- nfp_net_read_link_status(nn);
- nfp_net_irq_unmask(nn, NFP_NET_IRQ_LSC_IDX);
- return IRQ_HANDLED;
- }
- /**
- * nfp_net_irq_exn() - Interrupt service routine for exceptions
- * @irq: Interrupt
- * @data: Opaque data structure
- *
- * Return: Indicate if the interrupt has been handled.
- */
- static irqreturn_t nfp_net_irq_exn(int irq, void *data)
- {
- struct nfp_net *nn = data;
- nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
- /* XXX TO BE IMPLEMENTED */
- return IRQ_HANDLED;
- }
- /**
- * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
- * @tx_ring: TX ring structure
- * @r_vec: IRQ vector servicing this ring
- * @idx: Ring index
- */
- static void
- nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
- struct nfp_net_r_vector *r_vec, unsigned int idx)
- {
- struct nfp_net *nn = r_vec->nfp_net;
- tx_ring->idx = idx;
- tx_ring->r_vec = r_vec;
- tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
- tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
- }
- /**
- * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
- * @rx_ring: RX ring structure
- * @r_vec: IRQ vector servicing this ring
- * @idx: Ring index
- */
- static void
- nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
- struct nfp_net_r_vector *r_vec, unsigned int idx)
- {
- struct nfp_net *nn = r_vec->nfp_net;
- rx_ring->idx = idx;
- rx_ring->r_vec = r_vec;
- rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
- rx_ring->rx_qcidx = rx_ring->fl_qcidx + (nn->stride_rx - 1);
- rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
- rx_ring->qcp_rx = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->rx_qcidx);
- }
- /**
- * nfp_net_irqs_assign() - Assign IRQs and setup rvecs.
- * @netdev: netdev structure
- */
- static void nfp_net_irqs_assign(struct net_device *netdev)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- struct nfp_net_r_vector *r_vec;
- int r;
- if (nn->num_rx_rings > nn->num_r_vecs ||
- nn->num_tx_rings > nn->num_r_vecs)
- nn_warn(nn, "More rings (%d,%d) than vectors (%d).\n",
- nn->num_rx_rings, nn->num_tx_rings, nn->num_r_vecs);
- nn->num_rx_rings = min(nn->num_r_vecs, nn->num_rx_rings);
- nn->num_tx_rings = min(nn->num_r_vecs, nn->num_tx_rings);
- nn->num_stack_tx_rings = nn->num_tx_rings;
- nn->lsc_handler = nfp_net_irq_lsc;
- nn->exn_handler = nfp_net_irq_exn;
- for (r = 0; r < nn->max_r_vecs; r++) {
- r_vec = &nn->r_vecs[r];
- r_vec->nfp_net = nn;
- r_vec->handler = nfp_net_irq_rxtx;
- r_vec->irq_idx = NFP_NET_NON_Q_VECTORS + r;
- cpumask_set_cpu(r, &r_vec->affinity_mask);
- }
- }
- /**
- * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
- * @nn: NFP Network structure
- * @ctrl_offset: Control BAR offset where IRQ configuration should be written
- * @format: printf-style format to construct the interrupt name
- * @name: Pointer to allocated space for interrupt name
- * @name_sz: Size of space for interrupt name
- * @vector_idx: Index of MSI-X vector used for this interrupt
- * @handler: IRQ handler to register for this interrupt
- */
- static int
- nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
- const char *format, char *name, size_t name_sz,
- unsigned int vector_idx, irq_handler_t handler)
- {
- struct msix_entry *entry;
- int err;
- entry = &nn->irq_entries[vector_idx];
- snprintf(name, name_sz, format, netdev_name(nn->netdev));
- err = request_irq(entry->vector, handler, 0, name, nn);
- if (err) {
- nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
- entry->vector, err);
- return err;
- }
- nn_writeb(nn, ctrl_offset, vector_idx);
- return 0;
- }
- /**
- * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
- * @nn: NFP Network structure
- * @ctrl_offset: Control BAR offset where IRQ configuration should be written
- * @vector_idx: Index of MSI-X vector used for this interrupt
- */
- static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
- unsigned int vector_idx)
- {
- nn_writeb(nn, ctrl_offset, 0xff);
- free_irq(nn->irq_entries[vector_idx].vector, nn);
- }
- /* Transmit
- *
- * One queue controller peripheral queue is used for transmit. The
- * driver en-queues packets for transmit by advancing the write
- * pointer. The device indicates that packets have transmitted by
- * advancing the read pointer. The driver maintains a local copy of
- * the read and write pointer in @struct nfp_net_tx_ring. The driver
- * keeps @wr_p in sync with the queue controller write pointer and can
- * determine how many packets have been transmitted by comparing its
- * copy of the read pointer @rd_p with the read pointer maintained by
- * the queue controller peripheral.
- */
- /**
- * nfp_net_tx_full() - Check if the TX ring is full
- * @tx_ring: TX ring to check
- * @dcnt: Number of descriptors that need to be enqueued (must be >= 1)
- *
- * This function checks, based on the *host copy* of read/write
- * pointer if a given TX ring is full. The real TX queue may have
- * some newly made available slots.
- *
- * Return: True if the ring is full.
- */
- static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
- {
- return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
- }
- /* Wrappers for deciding when to stop and restart TX queues */
- static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
- {
- return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
- }
- static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
- {
- return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
- }
- /**
- * nfp_net_tx_ring_stop() - stop tx ring
- * @nd_q: netdev queue
- * @tx_ring: driver tx queue structure
- *
- * Safely stop TX ring. Remember that while we are running .start_xmit()
- * someone else may be cleaning the TX ring completions so we need to be
- * extra careful here.
- */
- static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
- struct nfp_net_tx_ring *tx_ring)
- {
- netif_tx_stop_queue(nd_q);
- /* We can race with the TX completion out of NAPI so recheck */
- smp_mb();
- if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
- netif_tx_start_queue(nd_q);
- }
- /**
- * nfp_net_tx_tso() - Set up Tx descriptor for LSO
- * @nn: NFP Net device
- * @r_vec: per-ring structure
- * @txbuf: Pointer to driver soft TX descriptor
- * @txd: Pointer to HW TX descriptor
- * @skb: Pointer to SKB
- *
- * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
- * Return error on packet header greater than maximum supported LSO header size.
- */
- static void nfp_net_tx_tso(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
- struct nfp_net_tx_buf *txbuf,
- struct nfp_net_tx_desc *txd, struct sk_buff *skb)
- {
- u32 hdrlen;
- u16 mss;
- if (!skb_is_gso(skb))
- return;
- if (!skb->encapsulation)
- hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
- else
- hdrlen = skb_inner_transport_header(skb) - skb->data +
- inner_tcp_hdrlen(skb);
- txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
- txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
- mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
- txd->l4_offset = hdrlen;
- txd->mss = cpu_to_le16(mss);
- txd->flags |= PCIE_DESC_TX_LSO;
- u64_stats_update_begin(&r_vec->tx_sync);
- r_vec->tx_lso++;
- u64_stats_update_end(&r_vec->tx_sync);
- }
- /**
- * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
- * @nn: NFP Net device
- * @r_vec: per-ring structure
- * @txbuf: Pointer to driver soft TX descriptor
- * @txd: Pointer to TX descriptor
- * @skb: Pointer to SKB
- *
- * This function sets the TX checksum flags in the TX descriptor based
- * on the configuration and the protocol of the packet to be transmitted.
- */
- static void nfp_net_tx_csum(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
- struct nfp_net_tx_buf *txbuf,
- struct nfp_net_tx_desc *txd, struct sk_buff *skb)
- {
- struct ipv6hdr *ipv6h;
- struct iphdr *iph;
- u8 l4_hdr;
- if (!(nn->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
- return;
- if (skb->ip_summed != CHECKSUM_PARTIAL)
- return;
- txd->flags |= PCIE_DESC_TX_CSUM;
- if (skb->encapsulation)
- txd->flags |= PCIE_DESC_TX_ENCAP;
- iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
- ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
- if (iph->version == 4) {
- txd->flags |= PCIE_DESC_TX_IP4_CSUM;
- l4_hdr = iph->protocol;
- } else if (ipv6h->version == 6) {
- l4_hdr = ipv6h->nexthdr;
- } else {
- nn_warn_ratelimit(nn, "partial checksum but ipv=%x!\n",
- iph->version);
- return;
- }
- switch (l4_hdr) {
- case IPPROTO_TCP:
- txd->flags |= PCIE_DESC_TX_TCP_CSUM;
- break;
- case IPPROTO_UDP:
- txd->flags |= PCIE_DESC_TX_UDP_CSUM;
- break;
- default:
- nn_warn_ratelimit(nn, "partial checksum but l4 proto=%x!\n",
- l4_hdr);
- return;
- }
- u64_stats_update_begin(&r_vec->tx_sync);
- if (skb->encapsulation)
- r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
- else
- r_vec->hw_csum_tx += txbuf->pkt_cnt;
- u64_stats_update_end(&r_vec->tx_sync);
- }
- static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring)
- {
- wmb();
- nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
- tx_ring->wr_ptr_add = 0;
- }
- /**
- * nfp_net_tx() - Main transmit entry point
- * @skb: SKB to transmit
- * @netdev: netdev structure
- *
- * Return: NETDEV_TX_OK on success.
- */
- static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- const struct skb_frag_struct *frag;
- struct nfp_net_r_vector *r_vec;
- struct nfp_net_tx_desc *txd, txdg;
- struct nfp_net_tx_buf *txbuf;
- struct nfp_net_tx_ring *tx_ring;
- struct netdev_queue *nd_q;
- dma_addr_t dma_addr;
- unsigned int fsize;
- int f, nr_frags;
- int wr_idx;
- u16 qidx;
- qidx = skb_get_queue_mapping(skb);
- tx_ring = &nn->tx_rings[qidx];
- r_vec = tx_ring->r_vec;
- nd_q = netdev_get_tx_queue(nn->netdev, qidx);
- nr_frags = skb_shinfo(skb)->nr_frags;
- if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
- nn_warn_ratelimit(nn, "TX ring %d busy. wrp=%u rdp=%u\n",
- qidx, tx_ring->wr_p, tx_ring->rd_p);
- netif_tx_stop_queue(nd_q);
- u64_stats_update_begin(&r_vec->tx_sync);
- r_vec->tx_busy++;
- u64_stats_update_end(&r_vec->tx_sync);
- return NETDEV_TX_BUSY;
- }
- /* Start with the head skbuf */
- dma_addr = dma_map_single(&nn->pdev->dev, skb->data, skb_headlen(skb),
- DMA_TO_DEVICE);
- if (dma_mapping_error(&nn->pdev->dev, dma_addr))
- goto err_free;
- wr_idx = tx_ring->wr_p & (tx_ring->cnt - 1);
- /* Stash the soft descriptor of the head then initialize it */
- txbuf = &tx_ring->txbufs[wr_idx];
- txbuf->skb = skb;
- txbuf->dma_addr = dma_addr;
- txbuf->fidx = -1;
- txbuf->pkt_cnt = 1;
- txbuf->real_len = skb->len;
- /* Build TX descriptor */
- txd = &tx_ring->txds[wr_idx];
- txd->offset_eop = (nr_frags == 0) ? PCIE_DESC_TX_EOP : 0;
- txd->dma_len = cpu_to_le16(skb_headlen(skb));
- nfp_desc_set_dma_addr(txd, dma_addr);
- txd->data_len = cpu_to_le16(skb->len);
- txd->flags = 0;
- txd->mss = 0;
- txd->l4_offset = 0;
- nfp_net_tx_tso(nn, r_vec, txbuf, txd, skb);
- nfp_net_tx_csum(nn, r_vec, txbuf, txd, skb);
- if (skb_vlan_tag_present(skb) && nn->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
- txd->flags |= PCIE_DESC_TX_VLAN;
- txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
- }
- /* Gather DMA */
- if (nr_frags > 0) {
- /* all descs must match except for in addr, length and eop */
- txdg = *txd;
- for (f = 0; f < nr_frags; f++) {
- frag = &skb_shinfo(skb)->frags[f];
- fsize = skb_frag_size(frag);
- dma_addr = skb_frag_dma_map(&nn->pdev->dev, frag, 0,
- fsize, DMA_TO_DEVICE);
- if (dma_mapping_error(&nn->pdev->dev, dma_addr))
- goto err_unmap;
- wr_idx = (wr_idx + 1) & (tx_ring->cnt - 1);
- tx_ring->txbufs[wr_idx].skb = skb;
- tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
- tx_ring->txbufs[wr_idx].fidx = f;
- txd = &tx_ring->txds[wr_idx];
- *txd = txdg;
- txd->dma_len = cpu_to_le16(fsize);
- nfp_desc_set_dma_addr(txd, dma_addr);
- txd->offset_eop =
- (f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0;
- }
- u64_stats_update_begin(&r_vec->tx_sync);
- r_vec->tx_gather++;
- u64_stats_update_end(&r_vec->tx_sync);
- }
- netdev_tx_sent_queue(nd_q, txbuf->real_len);
- tx_ring->wr_p += nr_frags + 1;
- if (nfp_net_tx_ring_should_stop(tx_ring))
- nfp_net_tx_ring_stop(nd_q, tx_ring);
- tx_ring->wr_ptr_add += nr_frags + 1;
- if (!skb->xmit_more || netif_xmit_stopped(nd_q))
- nfp_net_tx_xmit_more_flush(tx_ring);
- skb_tx_timestamp(skb);
- return NETDEV_TX_OK;
- err_unmap:
- --f;
- while (f >= 0) {
- frag = &skb_shinfo(skb)->frags[f];
- dma_unmap_page(&nn->pdev->dev,
- tx_ring->txbufs[wr_idx].dma_addr,
- skb_frag_size(frag), DMA_TO_DEVICE);
- tx_ring->txbufs[wr_idx].skb = NULL;
- tx_ring->txbufs[wr_idx].dma_addr = 0;
- tx_ring->txbufs[wr_idx].fidx = -2;
- wr_idx = wr_idx - 1;
- if (wr_idx < 0)
- wr_idx += tx_ring->cnt;
- }
- dma_unmap_single(&nn->pdev->dev, tx_ring->txbufs[wr_idx].dma_addr,
- skb_headlen(skb), DMA_TO_DEVICE);
- tx_ring->txbufs[wr_idx].skb = NULL;
- tx_ring->txbufs[wr_idx].dma_addr = 0;
- tx_ring->txbufs[wr_idx].fidx = -2;
- err_free:
- nn_warn_ratelimit(nn, "Failed to map DMA TX buffer\n");
- u64_stats_update_begin(&r_vec->tx_sync);
- r_vec->tx_errors++;
- u64_stats_update_end(&r_vec->tx_sync);
- dev_kfree_skb_any(skb);
- return NETDEV_TX_OK;
- }
- /**
- * nfp_net_tx_complete() - Handled completed TX packets
- * @tx_ring: TX ring structure
- *
- * Return: Number of completed TX descriptors
- */
- static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring)
- {
- struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
- struct nfp_net *nn = r_vec->nfp_net;
- const struct skb_frag_struct *frag;
- struct netdev_queue *nd_q;
- u32 done_pkts = 0, done_bytes = 0;
- struct sk_buff *skb;
- int todo, nr_frags;
- u32 qcp_rd_p;
- int fidx;
- int idx;
- /* Work out how many descriptors have been transmitted */
- qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
- if (qcp_rd_p == tx_ring->qcp_rd_p)
- return;
- if (qcp_rd_p > tx_ring->qcp_rd_p)
- todo = qcp_rd_p - tx_ring->qcp_rd_p;
- else
- todo = qcp_rd_p + tx_ring->cnt - tx_ring->qcp_rd_p;
- while (todo--) {
- idx = tx_ring->rd_p & (tx_ring->cnt - 1);
- tx_ring->rd_p++;
- skb = tx_ring->txbufs[idx].skb;
- if (!skb)
- continue;
- nr_frags = skb_shinfo(skb)->nr_frags;
- fidx = tx_ring->txbufs[idx].fidx;
- if (fidx == -1) {
- /* unmap head */
- dma_unmap_single(&nn->pdev->dev,
- tx_ring->txbufs[idx].dma_addr,
- skb_headlen(skb), DMA_TO_DEVICE);
- done_pkts += tx_ring->txbufs[idx].pkt_cnt;
- done_bytes += tx_ring->txbufs[idx].real_len;
- } else {
- /* unmap fragment */
- frag = &skb_shinfo(skb)->frags[fidx];
- dma_unmap_page(&nn->pdev->dev,
- tx_ring->txbufs[idx].dma_addr,
- skb_frag_size(frag), DMA_TO_DEVICE);
- }
- /* check for last gather fragment */
- if (fidx == nr_frags - 1)
- dev_kfree_skb_any(skb);
- tx_ring->txbufs[idx].dma_addr = 0;
- tx_ring->txbufs[idx].skb = NULL;
- tx_ring->txbufs[idx].fidx = -2;
- }
- tx_ring->qcp_rd_p = qcp_rd_p;
- u64_stats_update_begin(&r_vec->tx_sync);
- r_vec->tx_bytes += done_bytes;
- r_vec->tx_pkts += done_pkts;
- u64_stats_update_end(&r_vec->tx_sync);
- nd_q = netdev_get_tx_queue(nn->netdev, tx_ring->idx);
- netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
- if (nfp_net_tx_ring_should_wake(tx_ring)) {
- /* Make sure TX thread will see updated tx_ring->rd_p */
- smp_mb();
- if (unlikely(netif_tx_queue_stopped(nd_q)))
- netif_tx_wake_queue(nd_q);
- }
- WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
- "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
- tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
- }
- static void nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
- {
- struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
- struct nfp_net *nn = r_vec->nfp_net;
- u32 done_pkts = 0, done_bytes = 0;
- int idx, todo;
- u32 qcp_rd_p;
- /* Work out how many descriptors have been transmitted */
- qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
- if (qcp_rd_p == tx_ring->qcp_rd_p)
- return;
- if (qcp_rd_p > tx_ring->qcp_rd_p)
- todo = qcp_rd_p - tx_ring->qcp_rd_p;
- else
- todo = qcp_rd_p + tx_ring->cnt - tx_ring->qcp_rd_p;
- while (todo--) {
- idx = tx_ring->rd_p & (tx_ring->cnt - 1);
- tx_ring->rd_p++;
- if (!tx_ring->txbufs[idx].frag)
- continue;
- nfp_net_dma_unmap_rx(nn, tx_ring->txbufs[idx].dma_addr,
- nn->fl_bufsz, DMA_BIDIRECTIONAL);
- __free_page(virt_to_page(tx_ring->txbufs[idx].frag));
- done_pkts++;
- done_bytes += tx_ring->txbufs[idx].real_len;
- tx_ring->txbufs[idx].dma_addr = 0;
- tx_ring->txbufs[idx].frag = NULL;
- tx_ring->txbufs[idx].fidx = -2;
- }
- tx_ring->qcp_rd_p = qcp_rd_p;
- u64_stats_update_begin(&r_vec->tx_sync);
- r_vec->tx_bytes += done_bytes;
- r_vec->tx_pkts += done_pkts;
- u64_stats_update_end(&r_vec->tx_sync);
- WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
- "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
- tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
- }
- /**
- * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
- * @nn: NFP Net device
- * @tx_ring: TX ring structure
- *
- * Assumes that the device is stopped
- */
- static void
- nfp_net_tx_ring_reset(struct nfp_net *nn, struct nfp_net_tx_ring *tx_ring)
- {
- struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
- const struct skb_frag_struct *frag;
- struct pci_dev *pdev = nn->pdev;
- struct netdev_queue *nd_q;
- while (tx_ring->rd_p != tx_ring->wr_p) {
- struct nfp_net_tx_buf *tx_buf;
- int idx;
- idx = tx_ring->rd_p & (tx_ring->cnt - 1);
- tx_buf = &tx_ring->txbufs[idx];
- if (tx_ring == r_vec->xdp_ring) {
- nfp_net_dma_unmap_rx(nn, tx_buf->dma_addr,
- nn->fl_bufsz, DMA_BIDIRECTIONAL);
- __free_page(virt_to_page(tx_ring->txbufs[idx].frag));
- } else {
- struct sk_buff *skb = tx_ring->txbufs[idx].skb;
- int nr_frags = skb_shinfo(skb)->nr_frags;
- if (tx_buf->fidx == -1) {
- /* unmap head */
- dma_unmap_single(&pdev->dev, tx_buf->dma_addr,
- skb_headlen(skb),
- DMA_TO_DEVICE);
- } else {
- /* unmap fragment */
- frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
- dma_unmap_page(&pdev->dev, tx_buf->dma_addr,
- skb_frag_size(frag),
- DMA_TO_DEVICE);
- }
- /* check for last gather fragment */
- if (tx_buf->fidx == nr_frags - 1)
- dev_kfree_skb_any(skb);
- }
- tx_buf->dma_addr = 0;
- tx_buf->skb = NULL;
- tx_buf->fidx = -2;
- tx_ring->qcp_rd_p++;
- tx_ring->rd_p++;
- }
- memset(tx_ring->txds, 0, sizeof(*tx_ring->txds) * tx_ring->cnt);
- tx_ring->wr_p = 0;
- tx_ring->rd_p = 0;
- tx_ring->qcp_rd_p = 0;
- tx_ring->wr_ptr_add = 0;
- if (tx_ring == r_vec->xdp_ring)
- return;
- nd_q = netdev_get_tx_queue(nn->netdev, tx_ring->idx);
- netdev_tx_reset_queue(nd_q);
- }
- static void nfp_net_tx_timeout(struct net_device *netdev)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- int i;
- for (i = 0; i < nn->netdev->real_num_tx_queues; i++) {
- if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
- continue;
- nn_warn(nn, "TX timeout on ring: %d\n", i);
- }
- nn_warn(nn, "TX watchdog timeout\n");
- }
- /* Receive processing
- */
- static unsigned int
- nfp_net_calc_fl_bufsz(struct nfp_net *nn, unsigned int mtu)
- {
- unsigned int fl_bufsz;
- fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
- if (nn->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
- fl_bufsz += NFP_NET_MAX_PREPEND;
- else
- fl_bufsz += nn->rx_offset;
- fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + mtu;
- fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
- fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
- return fl_bufsz;
- }
- static void
- nfp_net_free_frag(void *frag, bool xdp)
- {
- if (!xdp)
- skb_free_frag(frag);
- else
- __free_page(virt_to_page(frag));
- }
- /**
- * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
- * @rx_ring: RX ring structure of the skb
- * @dma_addr: Pointer to storage for DMA address (output param)
- * @fl_bufsz: size of freelist buffers
- * @xdp: Whether XDP is enabled
- *
- * This function will allcate a new page frag, map it for DMA.
- *
- * Return: allocated page frag or NULL on failure.
- */
- static void *
- nfp_net_rx_alloc_one(struct nfp_net_rx_ring *rx_ring, dma_addr_t *dma_addr,
- unsigned int fl_bufsz, bool xdp)
- {
- struct nfp_net *nn = rx_ring->r_vec->nfp_net;
- int direction;
- void *frag;
- if (!xdp)
- frag = netdev_alloc_frag(fl_bufsz);
- else
- frag = page_address(alloc_page(GFP_KERNEL | __GFP_COLD));
- if (!frag) {
- nn_warn_ratelimit(nn, "Failed to alloc receive page frag\n");
- return NULL;
- }
- direction = xdp ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
- *dma_addr = nfp_net_dma_map_rx(nn, frag, fl_bufsz, direction);
- if (dma_mapping_error(&nn->pdev->dev, *dma_addr)) {
- nfp_net_free_frag(frag, xdp);
- nn_warn_ratelimit(nn, "Failed to map DMA RX buffer\n");
- return NULL;
- }
- return frag;
- }
- static void *
- nfp_net_napi_alloc_one(struct nfp_net *nn, int direction, dma_addr_t *dma_addr)
- {
- void *frag;
- if (!nn->xdp_prog)
- frag = napi_alloc_frag(nn->fl_bufsz);
- else
- frag = page_address(alloc_page(GFP_ATOMIC | __GFP_COLD));
- if (!frag) {
- nn_warn_ratelimit(nn, "Failed to alloc receive page frag\n");
- return NULL;
- }
- *dma_addr = nfp_net_dma_map_rx(nn, frag, nn->fl_bufsz, direction);
- if (dma_mapping_error(&nn->pdev->dev, *dma_addr)) {
- nfp_net_free_frag(frag, nn->xdp_prog);
- nn_warn_ratelimit(nn, "Failed to map DMA RX buffer\n");
- return NULL;
- }
- return frag;
- }
- /**
- * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
- * @rx_ring: RX ring structure
- * @frag: page fragment buffer
- * @dma_addr: DMA address of skb mapping
- */
- static void nfp_net_rx_give_one(struct nfp_net_rx_ring *rx_ring,
- void *frag, dma_addr_t dma_addr)
- {
- unsigned int wr_idx;
- wr_idx = rx_ring->wr_p & (rx_ring->cnt - 1);
- /* Stash SKB and DMA address away */
- rx_ring->rxbufs[wr_idx].frag = frag;
- rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
- /* Fill freelist descriptor */
- rx_ring->rxds[wr_idx].fld.reserved = 0;
- rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
- nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld, dma_addr);
- rx_ring->wr_p++;
- rx_ring->wr_ptr_add++;
- if (rx_ring->wr_ptr_add >= NFP_NET_FL_BATCH) {
- /* Update write pointer of the freelist queue. Make
- * sure all writes are flushed before telling the hardware.
- */
- wmb();
- nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, rx_ring->wr_ptr_add);
- rx_ring->wr_ptr_add = 0;
- }
- }
- /**
- * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
- * @rx_ring: RX ring structure
- *
- * Warning: Do *not* call if ring buffers were never put on the FW freelist
- * (i.e. device was not enabled)!
- */
- static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
- {
- unsigned int wr_idx, last_idx;
- /* Move the empty entry to the end of the list */
- wr_idx = rx_ring->wr_p & (rx_ring->cnt - 1);
- last_idx = rx_ring->cnt - 1;
- rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
- rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
- rx_ring->rxbufs[last_idx].dma_addr = 0;
- rx_ring->rxbufs[last_idx].frag = NULL;
- memset(rx_ring->rxds, 0, sizeof(*rx_ring->rxds) * rx_ring->cnt);
- rx_ring->wr_p = 0;
- rx_ring->rd_p = 0;
- rx_ring->wr_ptr_add = 0;
- }
- /**
- * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
- * @nn: NFP Net device
- * @rx_ring: RX ring to remove buffers from
- * @xdp: Whether XDP is enabled
- *
- * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
- * entries. After device is disabled nfp_net_rx_ring_reset() must be called
- * to restore required ring geometry.
- */
- static void
- nfp_net_rx_ring_bufs_free(struct nfp_net *nn, struct nfp_net_rx_ring *rx_ring,
- bool xdp)
- {
- int direction = xdp ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
- unsigned int i;
- for (i = 0; i < rx_ring->cnt - 1; i++) {
- /* NULL skb can only happen when initial filling of the ring
- * fails to allocate enough buffers and calls here to free
- * already allocated ones.
- */
- if (!rx_ring->rxbufs[i].frag)
- continue;
- nfp_net_dma_unmap_rx(nn, rx_ring->rxbufs[i].dma_addr,
- rx_ring->bufsz, direction);
- nfp_net_free_frag(rx_ring->rxbufs[i].frag, xdp);
- rx_ring->rxbufs[i].dma_addr = 0;
- rx_ring->rxbufs[i].frag = NULL;
- }
- }
- /**
- * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
- * @nn: NFP Net device
- * @rx_ring: RX ring to remove buffers from
- * @xdp: Whether XDP is enabled
- */
- static int
- nfp_net_rx_ring_bufs_alloc(struct nfp_net *nn, struct nfp_net_rx_ring *rx_ring,
- bool xdp)
- {
- struct nfp_net_rx_buf *rxbufs;
- unsigned int i;
- rxbufs = rx_ring->rxbufs;
- for (i = 0; i < rx_ring->cnt - 1; i++) {
- rxbufs[i].frag =
- nfp_net_rx_alloc_one(rx_ring, &rxbufs[i].dma_addr,
- rx_ring->bufsz, xdp);
- if (!rxbufs[i].frag) {
- nfp_net_rx_ring_bufs_free(nn, rx_ring, xdp);
- return -ENOMEM;
- }
- }
- return 0;
- }
- /**
- * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
- * @rx_ring: RX ring to fill
- */
- static void nfp_net_rx_ring_fill_freelist(struct nfp_net_rx_ring *rx_ring)
- {
- unsigned int i;
- for (i = 0; i < rx_ring->cnt - 1; i++)
- nfp_net_rx_give_one(rx_ring, rx_ring->rxbufs[i].frag,
- rx_ring->rxbufs[i].dma_addr);
- }
- /**
- * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
- * @flags: RX descriptor flags field in CPU byte order
- */
- static int nfp_net_rx_csum_has_errors(u16 flags)
- {
- u16 csum_all_checked, csum_all_ok;
- csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
- csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
- return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
- }
- /**
- * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
- * @nn: NFP Net device
- * @r_vec: per-ring structure
- * @rxd: Pointer to RX descriptor
- * @skb: Pointer to SKB
- */
- static void nfp_net_rx_csum(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
- struct nfp_net_rx_desc *rxd, struct sk_buff *skb)
- {
- skb_checksum_none_assert(skb);
- if (!(nn->netdev->features & NETIF_F_RXCSUM))
- return;
- if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
- u64_stats_update_begin(&r_vec->rx_sync);
- r_vec->hw_csum_rx_error++;
- u64_stats_update_end(&r_vec->rx_sync);
- return;
- }
- /* Assume that the firmware will never report inner CSUM_OK unless outer
- * L4 headers were successfully parsed. FW will always report zero UDP
- * checksum as CSUM_OK.
- */
- if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
- rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
- __skb_incr_checksum_unnecessary(skb);
- u64_stats_update_begin(&r_vec->rx_sync);
- r_vec->hw_csum_rx_ok++;
- u64_stats_update_end(&r_vec->rx_sync);
- }
- if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
- rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
- __skb_incr_checksum_unnecessary(skb);
- u64_stats_update_begin(&r_vec->rx_sync);
- r_vec->hw_csum_rx_inner_ok++;
- u64_stats_update_end(&r_vec->rx_sync);
- }
- }
- static void nfp_net_set_hash(struct net_device *netdev, struct sk_buff *skb,
- unsigned int type, __be32 *hash)
- {
- if (!(netdev->features & NETIF_F_RXHASH))
- return;
- switch (type) {
- case NFP_NET_RSS_IPV4:
- case NFP_NET_RSS_IPV6:
- case NFP_NET_RSS_IPV6_EX:
- skb_set_hash(skb, get_unaligned_be32(hash), PKT_HASH_TYPE_L3);
- break;
- default:
- skb_set_hash(skb, get_unaligned_be32(hash), PKT_HASH_TYPE_L4);
- break;
- }
- }
- static void
- nfp_net_set_hash_desc(struct net_device *netdev, struct sk_buff *skb,
- struct nfp_net_rx_desc *rxd)
- {
- struct nfp_net_rx_hash *rx_hash;
- if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
- return;
- rx_hash = (struct nfp_net_rx_hash *)(skb->data - sizeof(*rx_hash));
- nfp_net_set_hash(netdev, skb, get_unaligned_be32(&rx_hash->hash_type),
- &rx_hash->hash);
- }
- static void *
- nfp_net_parse_meta(struct net_device *netdev, struct sk_buff *skb,
- int meta_len)
- {
- u8 *data = skb->data - meta_len;
- u32 meta_info;
- meta_info = get_unaligned_be32(data);
- data += 4;
- while (meta_info) {
- switch (meta_info & NFP_NET_META_FIELD_MASK) {
- case NFP_NET_META_HASH:
- meta_info >>= NFP_NET_META_FIELD_SIZE;
- nfp_net_set_hash(netdev, skb,
- meta_info & NFP_NET_META_FIELD_MASK,
- (__be32 *)data);
- data += 4;
- break;
- case NFP_NET_META_MARK:
- skb->mark = get_unaligned_be32(data);
- data += 4;
- break;
- default:
- return NULL;
- }
- meta_info >>= NFP_NET_META_FIELD_SIZE;
- }
- return data;
- }
- static void
- nfp_net_rx_drop(struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring,
- struct nfp_net_rx_buf *rxbuf, struct sk_buff *skb)
- {
- u64_stats_update_begin(&r_vec->rx_sync);
- r_vec->rx_drops++;
- u64_stats_update_end(&r_vec->rx_sync);
- /* skb is build based on the frag, free_skb() would free the frag
- * so to be able to reuse it we need an extra ref.
- */
- if (skb && rxbuf && skb->head == rxbuf->frag)
- page_ref_inc(virt_to_head_page(rxbuf->frag));
- if (rxbuf)
- nfp_net_rx_give_one(rx_ring, rxbuf->frag, rxbuf->dma_addr);
- if (skb)
- dev_kfree_skb_any(skb);
- }
- static void
- nfp_net_tx_xdp_buf(struct nfp_net *nn, struct nfp_net_rx_ring *rx_ring,
- struct nfp_net_tx_ring *tx_ring,
- struct nfp_net_rx_buf *rxbuf, unsigned int pkt_off,
- unsigned int pkt_len)
- {
- struct nfp_net_tx_buf *txbuf;
- struct nfp_net_tx_desc *txd;
- dma_addr_t new_dma_addr;
- void *new_frag;
- int wr_idx;
- if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
- nfp_net_rx_drop(rx_ring->r_vec, rx_ring, rxbuf, NULL);
- return;
- }
- new_frag = nfp_net_napi_alloc_one(nn, DMA_BIDIRECTIONAL, &new_dma_addr);
- if (unlikely(!new_frag)) {
- nfp_net_rx_drop(rx_ring->r_vec, rx_ring, rxbuf, NULL);
- return;
- }
- nfp_net_rx_give_one(rx_ring, new_frag, new_dma_addr);
- wr_idx = tx_ring->wr_p & (tx_ring->cnt - 1);
- /* Stash the soft descriptor of the head then initialize it */
- txbuf = &tx_ring->txbufs[wr_idx];
- txbuf->frag = rxbuf->frag;
- txbuf->dma_addr = rxbuf->dma_addr;
- txbuf->fidx = -1;
- txbuf->pkt_cnt = 1;
- txbuf->real_len = pkt_len;
- dma_sync_single_for_device(&nn->pdev->dev, rxbuf->dma_addr + pkt_off,
- pkt_len, DMA_TO_DEVICE);
- /* Build TX descriptor */
- txd = &tx_ring->txds[wr_idx];
- txd->offset_eop = PCIE_DESC_TX_EOP;
- txd->dma_len = cpu_to_le16(pkt_len);
- nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + pkt_off);
- txd->data_len = cpu_to_le16(pkt_len);
- txd->flags = 0;
- txd->mss = 0;
- txd->l4_offset = 0;
- tx_ring->wr_p++;
- tx_ring->wr_ptr_add++;
- }
- static int nfp_net_run_xdp(struct bpf_prog *prog, void *data, unsigned int len)
- {
- struct xdp_buff xdp;
- xdp.data = data;
- xdp.data_end = data + len;
- return bpf_prog_run_xdp(prog, &xdp);
- }
- /**
- * nfp_net_rx() - receive up to @budget packets on @rx_ring
- * @rx_ring: RX ring to receive from
- * @budget: NAPI budget
- *
- * Note, this function is separated out from the napi poll function to
- * more cleanly separate packet receive code from other bookkeeping
- * functions performed in the napi poll function.
- *
- * Return: Number of packets received.
- */
- static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
- {
- struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
- struct nfp_net *nn = r_vec->nfp_net;
- struct nfp_net_tx_ring *tx_ring;
- struct bpf_prog *xdp_prog;
- unsigned int true_bufsz;
- struct sk_buff *skb;
- int pkts_polled = 0;
- int rx_dma_map_dir;
- int idx;
- rcu_read_lock();
- xdp_prog = READ_ONCE(nn->xdp_prog);
- rx_dma_map_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
- true_bufsz = xdp_prog ? PAGE_SIZE : nn->fl_bufsz;
- tx_ring = r_vec->xdp_ring;
- while (pkts_polled < budget) {
- unsigned int meta_len, data_len, data_off, pkt_len, pkt_off;
- struct nfp_net_rx_buf *rxbuf;
- struct nfp_net_rx_desc *rxd;
- dma_addr_t new_dma_addr;
- void *new_frag;
- idx = rx_ring->rd_p & (rx_ring->cnt - 1);
- rxd = &rx_ring->rxds[idx];
- if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
- break;
- /* Memory barrier to ensure that we won't do other reads
- * before the DD bit.
- */
- dma_rmb();
- rx_ring->rd_p++;
- pkts_polled++;
- rxbuf = &rx_ring->rxbufs[idx];
- /* < meta_len >
- * <-- [rx_offset] -->
- * ---------------------------------------------------------
- * | [XX] | metadata | packet | XXXX |
- * ---------------------------------------------------------
- * <---------------- data_len --------------->
- *
- * The rx_offset is fixed for all packets, the meta_len can vary
- * on a packet by packet basis. If rx_offset is set to zero
- * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
- * buffer and is immediately followed by the packet (no [XX]).
- */
- meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
- data_len = le16_to_cpu(rxd->rxd.data_len);
- pkt_len = data_len - meta_len;
- if (nn->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
- pkt_off = meta_len;
- else
- pkt_off = nn->rx_offset;
- data_off = NFP_NET_RX_BUF_HEADROOM + pkt_off;
- /* Stats update */
- u64_stats_update_begin(&r_vec->rx_sync);
- r_vec->rx_pkts++;
- r_vec->rx_bytes += pkt_len;
- u64_stats_update_end(&r_vec->rx_sync);
- if (xdp_prog && !(rxd->rxd.flags & PCIE_DESC_RX_BPF &&
- nn->bpf_offload_xdp)) {
- int act;
- dma_sync_single_for_cpu(&nn->pdev->dev,
- rxbuf->dma_addr + pkt_off,
- pkt_len, DMA_FROM_DEVICE);
- act = nfp_net_run_xdp(xdp_prog, rxbuf->frag + data_off,
- pkt_len);
- switch (act) {
- case XDP_PASS:
- break;
- case XDP_TX:
- nfp_net_tx_xdp_buf(nn, rx_ring, tx_ring, rxbuf,
- pkt_off, pkt_len);
- continue;
- default:
- bpf_warn_invalid_xdp_action(act);
- case XDP_ABORTED:
- case XDP_DROP:
- nfp_net_rx_give_one(rx_ring, rxbuf->frag,
- rxbuf->dma_addr);
- continue;
- }
- }
- skb = build_skb(rxbuf->frag, true_bufsz);
- if (unlikely(!skb)) {
- nfp_net_rx_drop(r_vec, rx_ring, rxbuf, NULL);
- continue;
- }
- new_frag = nfp_net_napi_alloc_one(nn, rx_dma_map_dir,
- &new_dma_addr);
- if (unlikely(!new_frag)) {
- nfp_net_rx_drop(r_vec, rx_ring, rxbuf, skb);
- continue;
- }
- nfp_net_dma_unmap_rx(nn, rxbuf->dma_addr, nn->fl_bufsz,
- rx_dma_map_dir);
- nfp_net_rx_give_one(rx_ring, new_frag, new_dma_addr);
- skb_reserve(skb, data_off);
- skb_put(skb, pkt_len);
- if (nn->fw_ver.major <= 3) {
- nfp_net_set_hash_desc(nn->netdev, skb, rxd);
- } else if (meta_len) {
- void *end;
- end = nfp_net_parse_meta(nn->netdev, skb, meta_len);
- if (unlikely(end != skb->data)) {
- nn_warn_ratelimit(nn, "invalid RX packet metadata\n");
- nfp_net_rx_drop(r_vec, rx_ring, NULL, skb);
- continue;
- }
- }
- skb_record_rx_queue(skb, rx_ring->idx);
- skb->protocol = eth_type_trans(skb, nn->netdev);
- nfp_net_rx_csum(nn, r_vec, rxd, skb);
- if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
- __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
- le16_to_cpu(rxd->rxd.vlan));
- napi_gro_receive(&rx_ring->r_vec->napi, skb);
- }
- if (xdp_prog && tx_ring->wr_ptr_add)
- nfp_net_tx_xmit_more_flush(tx_ring);
- rcu_read_unlock();
- return pkts_polled;
- }
- /**
- * nfp_net_poll() - napi poll function
- * @napi: NAPI structure
- * @budget: NAPI budget
- *
- * Return: number of packets polled.
- */
- static int nfp_net_poll(struct napi_struct *napi, int budget)
- {
- struct nfp_net_r_vector *r_vec =
- container_of(napi, struct nfp_net_r_vector, napi);
- unsigned int pkts_polled = 0;
- if (r_vec->tx_ring)
- nfp_net_tx_complete(r_vec->tx_ring);
- if (r_vec->rx_ring) {
- pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
- if (r_vec->xdp_ring)
- nfp_net_xdp_complete(r_vec->xdp_ring);
- }
- if (pkts_polled < budget) {
- napi_complete_done(napi, pkts_polled);
- nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_idx);
- }
- return pkts_polled;
- }
- /* Setup and Configuration
- */
- /**
- * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
- * @tx_ring: TX ring to free
- */
- static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
- {
- struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
- struct nfp_net *nn = r_vec->nfp_net;
- struct pci_dev *pdev = nn->pdev;
- kfree(tx_ring->txbufs);
- if (tx_ring->txds)
- dma_free_coherent(&pdev->dev, tx_ring->size,
- tx_ring->txds, tx_ring->dma);
- tx_ring->cnt = 0;
- tx_ring->txbufs = NULL;
- tx_ring->txds = NULL;
- tx_ring->dma = 0;
- tx_ring->size = 0;
- }
- /**
- * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
- * @tx_ring: TX Ring structure to allocate
- * @cnt: Ring buffer count
- * @is_xdp: True if ring will be used for XDP
- *
- * Return: 0 on success, negative errno otherwise.
- */
- static int
- nfp_net_tx_ring_alloc(struct nfp_net_tx_ring *tx_ring, u32 cnt, bool is_xdp)
- {
- struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
- struct nfp_net *nn = r_vec->nfp_net;
- struct pci_dev *pdev = nn->pdev;
- int sz;
- tx_ring->cnt = cnt;
- tx_ring->size = sizeof(*tx_ring->txds) * tx_ring->cnt;
- tx_ring->txds = dma_zalloc_coherent(&pdev->dev, tx_ring->size,
- &tx_ring->dma, GFP_KERNEL);
- if (!tx_ring->txds)
- goto err_alloc;
- sz = sizeof(*tx_ring->txbufs) * tx_ring->cnt;
- tx_ring->txbufs = kzalloc(sz, GFP_KERNEL);
- if (!tx_ring->txbufs)
- goto err_alloc;
- if (!is_xdp)
- netif_set_xps_queue(nn->netdev, &r_vec->affinity_mask,
- tx_ring->idx);
- nn_dbg(nn, "TxQ%02d: QCidx=%02d cnt=%d dma=%#llx host=%p %s\n",
- tx_ring->idx, tx_ring->qcidx,
- tx_ring->cnt, (unsigned long long)tx_ring->dma, tx_ring->txds,
- is_xdp ? "XDP" : "");
- return 0;
- err_alloc:
- nfp_net_tx_ring_free(tx_ring);
- return -ENOMEM;
- }
- static struct nfp_net_tx_ring *
- nfp_net_tx_ring_set_prepare(struct nfp_net *nn, struct nfp_net_ring_set *s,
- unsigned int num_stack_tx_rings)
- {
- struct nfp_net_tx_ring *rings;
- unsigned int r;
- rings = kcalloc(s->n_rings, sizeof(*rings), GFP_KERNEL);
- if (!rings)
- return NULL;
- for (r = 0; r < s->n_rings; r++) {
- int bias = 0;
- if (r >= num_stack_tx_rings)
- bias = num_stack_tx_rings;
- nfp_net_tx_ring_init(&rings[r], &nn->r_vecs[r - bias], r);
- if (nfp_net_tx_ring_alloc(&rings[r], s->dcnt, bias))
- goto err_free_prev;
- }
- return s->rings = rings;
- err_free_prev:
- while (r--)
- nfp_net_tx_ring_free(&rings[r]);
- kfree(rings);
- return NULL;
- }
- static void
- nfp_net_tx_ring_set_swap(struct nfp_net *nn, struct nfp_net_ring_set *s)
- {
- struct nfp_net_ring_set new = *s;
- s->dcnt = nn->txd_cnt;
- s->rings = nn->tx_rings;
- s->n_rings = nn->num_tx_rings;
- nn->txd_cnt = new.dcnt;
- nn->tx_rings = new.rings;
- nn->num_tx_rings = new.n_rings;
- }
- static void
- nfp_net_tx_ring_set_free(struct nfp_net *nn, struct nfp_net_ring_set *s)
- {
- struct nfp_net_tx_ring *rings = s->rings;
- unsigned int r;
- for (r = 0; r < s->n_rings; r++)
- nfp_net_tx_ring_free(&rings[r]);
- kfree(rings);
- }
- /**
- * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
- * @rx_ring: RX ring to free
- */
- static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
- {
- struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
- struct nfp_net *nn = r_vec->nfp_net;
- struct pci_dev *pdev = nn->pdev;
- kfree(rx_ring->rxbufs);
- if (rx_ring->rxds)
- dma_free_coherent(&pdev->dev, rx_ring->size,
- rx_ring->rxds, rx_ring->dma);
- rx_ring->cnt = 0;
- rx_ring->rxbufs = NULL;
- rx_ring->rxds = NULL;
- rx_ring->dma = 0;
- rx_ring->size = 0;
- }
- /**
- * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
- * @rx_ring: RX ring to allocate
- * @fl_bufsz: Size of buffers to allocate
- * @cnt: Ring buffer count
- *
- * Return: 0 on success, negative errno otherwise.
- */
- static int
- nfp_net_rx_ring_alloc(struct nfp_net_rx_ring *rx_ring, unsigned int fl_bufsz,
- u32 cnt)
- {
- struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
- struct nfp_net *nn = r_vec->nfp_net;
- struct pci_dev *pdev = nn->pdev;
- int sz;
- rx_ring->cnt = cnt;
- rx_ring->bufsz = fl_bufsz;
- rx_ring->size = sizeof(*rx_ring->rxds) * rx_ring->cnt;
- rx_ring->rxds = dma_zalloc_coherent(&pdev->dev, rx_ring->size,
- &rx_ring->dma, GFP_KERNEL);
- if (!rx_ring->rxds)
- goto err_alloc;
- sz = sizeof(*rx_ring->rxbufs) * rx_ring->cnt;
- rx_ring->rxbufs = kzalloc(sz, GFP_KERNEL);
- if (!rx_ring->rxbufs)
- goto err_alloc;
- nn_dbg(nn, "RxQ%02d: FlQCidx=%02d RxQCidx=%02d cnt=%d dma=%#llx host=%p\n",
- rx_ring->idx, rx_ring->fl_qcidx, rx_ring->rx_qcidx,
- rx_ring->cnt, (unsigned long long)rx_ring->dma, rx_ring->rxds);
- return 0;
- err_alloc:
- nfp_net_rx_ring_free(rx_ring);
- return -ENOMEM;
- }
- static struct nfp_net_rx_ring *
- nfp_net_rx_ring_set_prepare(struct nfp_net *nn, struct nfp_net_ring_set *s,
- bool xdp)
- {
- unsigned int fl_bufsz = nfp_net_calc_fl_bufsz(nn, s->mtu);
- struct nfp_net_rx_ring *rings;
- unsigned int r;
- rings = kcalloc(s->n_rings, sizeof(*rings), GFP_KERNEL);
- if (!rings)
- return NULL;
- for (r = 0; r < s->n_rings; r++) {
- nfp_net_rx_ring_init(&rings[r], &nn->r_vecs[r], r);
- if (nfp_net_rx_ring_alloc(&rings[r], fl_bufsz, s->dcnt))
- goto err_free_prev;
- if (nfp_net_rx_ring_bufs_alloc(nn, &rings[r], xdp))
- goto err_free_ring;
- }
- return s->rings = rings;
- err_free_prev:
- while (r--) {
- nfp_net_rx_ring_bufs_free(nn, &rings[r], xdp);
- err_free_ring:
- nfp_net_rx_ring_free(&rings[r]);
- }
- kfree(rings);
- return NULL;
- }
- static void
- nfp_net_rx_ring_set_swap(struct nfp_net *nn, struct nfp_net_ring_set *s)
- {
- struct nfp_net_ring_set new = *s;
- s->mtu = nn->netdev->mtu;
- s->dcnt = nn->rxd_cnt;
- s->rings = nn->rx_rings;
- s->n_rings = nn->num_rx_rings;
- nn->netdev->mtu = new.mtu;
- nn->fl_bufsz = nfp_net_calc_fl_bufsz(nn, new.mtu);
- nn->rxd_cnt = new.dcnt;
- nn->rx_rings = new.rings;
- nn->num_rx_rings = new.n_rings;
- }
- static void
- nfp_net_rx_ring_set_free(struct nfp_net *nn, struct nfp_net_ring_set *s,
- bool xdp)
- {
- struct nfp_net_rx_ring *rings = s->rings;
- unsigned int r;
- for (r = 0; r < s->n_rings; r++) {
- nfp_net_rx_ring_bufs_free(nn, &rings[r], xdp);
- nfp_net_rx_ring_free(&rings[r]);
- }
- kfree(rings);
- }
- static void
- nfp_net_vector_assign_rings(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
- int idx)
- {
- r_vec->rx_ring = idx < nn->num_rx_rings ? &nn->rx_rings[idx] : NULL;
- r_vec->tx_ring =
- idx < nn->num_stack_tx_rings ? &nn->tx_rings[idx] : NULL;
- r_vec->xdp_ring = idx < nn->num_tx_rings - nn->num_stack_tx_rings ?
- &nn->tx_rings[nn->num_stack_tx_rings + idx] : NULL;
- }
- static int
- nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
- int idx)
- {
- struct msix_entry *entry = &nn->irq_entries[r_vec->irq_idx];
- int err;
- /* Setup NAPI */
- netif_napi_add(nn->netdev, &r_vec->napi,
- nfp_net_poll, NAPI_POLL_WEIGHT);
- snprintf(r_vec->name, sizeof(r_vec->name),
- "%s-rxtx-%d", nn->netdev->name, idx);
- err = request_irq(entry->vector, r_vec->handler, 0, r_vec->name, r_vec);
- if (err) {
- netif_napi_del(&r_vec->napi);
- nn_err(nn, "Error requesting IRQ %d\n", entry->vector);
- return err;
- }
- disable_irq(entry->vector);
- irq_set_affinity_hint(entry->vector, &r_vec->affinity_mask);
- nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, entry->vector, entry->entry);
- return 0;
- }
- static void
- nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
- {
- struct msix_entry *entry = &nn->irq_entries[r_vec->irq_idx];
- irq_set_affinity_hint(entry->vector, NULL);
- netif_napi_del(&r_vec->napi);
- free_irq(entry->vector, r_vec);
- }
- /**
- * nfp_net_rss_write_itbl() - Write RSS indirection table to device
- * @nn: NFP Net device to reconfigure
- */
- void nfp_net_rss_write_itbl(struct nfp_net *nn)
- {
- int i;
- for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
- nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
- get_unaligned_le32(nn->rss_itbl + i));
- }
- /**
- * nfp_net_rss_write_key() - Write RSS hash key to device
- * @nn: NFP Net device to reconfigure
- */
- void nfp_net_rss_write_key(struct nfp_net *nn)
- {
- int i;
- for (i = 0; i < NFP_NET_CFG_RSS_KEY_SZ; i += 4)
- nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
- get_unaligned_le32(nn->rss_key + i));
- }
- /**
- * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
- * @nn: NFP Net device to reconfigure
- */
- void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
- {
- u8 i;
- u32 factor;
- u32 value;
- /* Compute factor used to convert coalesce '_usecs' parameters to
- * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
- * count.
- */
- factor = nn->me_freq_mhz / 16;
- /* copy RX interrupt coalesce parameters */
- value = (nn->rx_coalesce_max_frames << 16) |
- (factor * nn->rx_coalesce_usecs);
- for (i = 0; i < nn->num_rx_rings; i++)
- nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
- /* copy TX interrupt coalesce parameters */
- value = (nn->tx_coalesce_max_frames << 16) |
- (factor * nn->tx_coalesce_usecs);
- for (i = 0; i < nn->num_tx_rings; i++)
- nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
- }
- /**
- * nfp_net_write_mac_addr() - Write mac address to the device control BAR
- * @nn: NFP Net device to reconfigure
- *
- * Writes the MAC address from the netdev to the device control BAR. Does not
- * perform the required reconfig. We do a bit of byte swapping dance because
- * firmware is LE.
- */
- static void nfp_net_write_mac_addr(struct nfp_net *nn)
- {
- nn_writel(nn, NFP_NET_CFG_MACADDR + 0,
- get_unaligned_be32(nn->netdev->dev_addr));
- nn_writew(nn, NFP_NET_CFG_MACADDR + 6,
- get_unaligned_be16(nn->netdev->dev_addr + 4));
- }
- static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
- {
- nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
- nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
- nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
- nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
- nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
- nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
- }
- /**
- * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
- * @nn: NFP Net device to reconfigure
- */
- static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
- {
- u32 new_ctrl, update;
- unsigned int r;
- int err;
- new_ctrl = nn->ctrl;
- new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
- update = NFP_NET_CFG_UPDATE_GEN;
- update |= NFP_NET_CFG_UPDATE_MSIX;
- update |= NFP_NET_CFG_UPDATE_RING;
- if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
- new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
- nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
- nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
- nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
- err = nfp_net_reconfig(nn, update);
- if (err)
- nn_err(nn, "Could not disable device: %d\n", err);
- for (r = 0; r < nn->num_rx_rings; r++)
- nfp_net_rx_ring_reset(&nn->rx_rings[r]);
- for (r = 0; r < nn->num_tx_rings; r++)
- nfp_net_tx_ring_reset(nn, &nn->tx_rings[r]);
- for (r = 0; r < nn->num_r_vecs; r++)
- nfp_net_vec_clear_ring_data(nn, r);
- nn->ctrl = new_ctrl;
- }
- static void
- nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
- struct nfp_net_rx_ring *rx_ring, unsigned int idx)
- {
- /* Write the DMA address, size and MSI-X info to the device */
- nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
- nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
- nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_idx);
- }
- static void
- nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
- struct nfp_net_tx_ring *tx_ring, unsigned int idx)
- {
- nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
- nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
- nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_idx);
- }
- static int __nfp_net_set_config_and_enable(struct nfp_net *nn)
- {
- u32 new_ctrl, update = 0;
- unsigned int r;
- int err;
- new_ctrl = nn->ctrl;
- if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
- nfp_net_rss_write_key(nn);
- nfp_net_rss_write_itbl(nn);
- nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
- update |= NFP_NET_CFG_UPDATE_RSS;
- }
- if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
- nfp_net_coalesce_write_cfg(nn);
- new_ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
- update |= NFP_NET_CFG_UPDATE_IRQMOD;
- }
- for (r = 0; r < nn->num_tx_rings; r++)
- nfp_net_tx_ring_hw_cfg_write(nn, &nn->tx_rings[r], r);
- for (r = 0; r < nn->num_rx_rings; r++)
- nfp_net_rx_ring_hw_cfg_write(nn, &nn->rx_rings[r], r);
- nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->num_tx_rings == 64 ?
- 0xffffffffffffffffULL : ((u64)1 << nn->num_tx_rings) - 1);
- nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->num_rx_rings == 64 ?
- 0xffffffffffffffffULL : ((u64)1 << nn->num_rx_rings) - 1);
- nfp_net_write_mac_addr(nn);
- nn_writel(nn, NFP_NET_CFG_MTU, nn->netdev->mtu);
- nn_writel(nn, NFP_NET_CFG_FLBUFSZ, nn->fl_bufsz);
- /* Enable device */
- new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
- update |= NFP_NET_CFG_UPDATE_GEN;
- update |= NFP_NET_CFG_UPDATE_MSIX;
- update |= NFP_NET_CFG_UPDATE_RING;
- if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
- new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
- nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
- err = nfp_net_reconfig(nn, update);
- nn->ctrl = new_ctrl;
- for (r = 0; r < nn->num_rx_rings; r++)
- nfp_net_rx_ring_fill_freelist(&nn->rx_rings[r]);
- /* Since reconfiguration requests while NFP is down are ignored we
- * have to wipe the entire VXLAN configuration and reinitialize it.
- */
- if (nn->ctrl & NFP_NET_CFG_CTRL_VXLAN) {
- memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
- memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
- udp_tunnel_get_rx_info(nn->netdev);
- }
- return err;
- }
- /**
- * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
- * @nn: NFP Net device to reconfigure
- */
- static int nfp_net_set_config_and_enable(struct nfp_net *nn)
- {
- int err;
- err = __nfp_net_set_config_and_enable(nn);
- if (err)
- nfp_net_clear_config_and_disable(nn);
- return err;
- }
- /**
- * nfp_net_open_stack() - Start the device from stack's perspective
- * @nn: NFP Net device to reconfigure
- */
- static void nfp_net_open_stack(struct nfp_net *nn)
- {
- unsigned int r;
- for (r = 0; r < nn->num_r_vecs; r++) {
- napi_enable(&nn->r_vecs[r].napi);
- enable_irq(nn->irq_entries[nn->r_vecs[r].irq_idx].vector);
- }
- netif_tx_wake_all_queues(nn->netdev);
- enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
- nfp_net_read_link_status(nn);
- }
- static int nfp_net_netdev_open(struct net_device *netdev)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- struct nfp_net_ring_set rx = {
- .n_rings = nn->num_rx_rings,
- .mtu = nn->netdev->mtu,
- .dcnt = nn->rxd_cnt,
- };
- struct nfp_net_ring_set tx = {
- .n_rings = nn->num_tx_rings,
- .dcnt = nn->txd_cnt,
- };
- int err, r;
- if (nn->ctrl & NFP_NET_CFG_CTRL_ENABLE) {
- nn_err(nn, "Dev is already enabled: 0x%08x\n", nn->ctrl);
- return -EBUSY;
- }
- /* Step 1: Allocate resources for rings and the like
- * - Request interrupts
- * - Allocate RX and TX ring resources
- * - Setup initial RSS table
- */
- err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
- nn->exn_name, sizeof(nn->exn_name),
- NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
- if (err)
- return err;
- err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
- nn->lsc_name, sizeof(nn->lsc_name),
- NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
- if (err)
- goto err_free_exn;
- disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
- for (r = 0; r < nn->num_r_vecs; r++) {
- err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
- if (err)
- goto err_cleanup_vec_p;
- }
- nn->rx_rings = nfp_net_rx_ring_set_prepare(nn, &rx, nn->xdp_prog);
- if (!nn->rx_rings) {
- err = -ENOMEM;
- goto err_cleanup_vec;
- }
- nn->tx_rings = nfp_net_tx_ring_set_prepare(nn, &tx,
- nn->num_stack_tx_rings);
- if (!nn->tx_rings) {
- err = -ENOMEM;
- goto err_free_rx_rings;
- }
- for (r = 0; r < nn->max_r_vecs; r++)
- nfp_net_vector_assign_rings(nn, &nn->r_vecs[r], r);
- err = netif_set_real_num_tx_queues(netdev, nn->num_stack_tx_rings);
- if (err)
- goto err_free_rings;
- err = netif_set_real_num_rx_queues(netdev, nn->num_rx_rings);
- if (err)
- goto err_free_rings;
- /* Step 2: Configure the NFP
- * - Enable rings from 0 to tx_rings/rx_rings - 1.
- * - Write MAC address (in case it changed)
- * - Set the MTU
- * - Set the Freelist buffer size
- * - Enable the FW
- */
- err = nfp_net_set_config_and_enable(nn);
- if (err)
- goto err_free_rings;
- /* Step 3: Enable for kernel
- * - put some freelist descriptors on each RX ring
- * - enable NAPI on each ring
- * - enable all TX queues
- * - set link state
- */
- nfp_net_open_stack(nn);
- return 0;
- err_free_rings:
- nfp_net_tx_ring_set_free(nn, &tx);
- err_free_rx_rings:
- nfp_net_rx_ring_set_free(nn, &rx, nn->xdp_prog);
- err_cleanup_vec:
- r = nn->num_r_vecs;
- err_cleanup_vec_p:
- while (r--)
- nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
- nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
- err_free_exn:
- nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
- return err;
- }
- /**
- * nfp_net_close_stack() - Quiescent the stack (part of close)
- * @nn: NFP Net device to reconfigure
- */
- static void nfp_net_close_stack(struct nfp_net *nn)
- {
- unsigned int r;
- disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
- netif_carrier_off(nn->netdev);
- nn->link_up = false;
- for (r = 0; r < nn->num_r_vecs; r++) {
- disable_irq(nn->irq_entries[nn->r_vecs[r].irq_idx].vector);
- napi_disable(&nn->r_vecs[r].napi);
- }
- netif_tx_disable(nn->netdev);
- }
- /**
- * nfp_net_close_free_all() - Free all runtime resources
- * @nn: NFP Net device to reconfigure
- */
- static void nfp_net_close_free_all(struct nfp_net *nn)
- {
- unsigned int r;
- for (r = 0; r < nn->num_rx_rings; r++) {
- nfp_net_rx_ring_bufs_free(nn, &nn->rx_rings[r], nn->xdp_prog);
- nfp_net_rx_ring_free(&nn->rx_rings[r]);
- }
- for (r = 0; r < nn->num_tx_rings; r++)
- nfp_net_tx_ring_free(&nn->tx_rings[r]);
- for (r = 0; r < nn->num_r_vecs; r++)
- nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
- kfree(nn->rx_rings);
- kfree(nn->tx_rings);
- nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
- nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
- }
- /**
- * nfp_net_netdev_close() - Called when the device is downed
- * @netdev: netdev structure
- */
- static int nfp_net_netdev_close(struct net_device *netdev)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- if (!(nn->ctrl & NFP_NET_CFG_CTRL_ENABLE)) {
- nn_err(nn, "Dev is not up: 0x%08x\n", nn->ctrl);
- return 0;
- }
- /* Step 1: Disable RX and TX rings from the Linux kernel perspective
- */
- nfp_net_close_stack(nn);
- /* Step 2: Tell NFP
- */
- nfp_net_clear_config_and_disable(nn);
- /* Step 3: Free resources
- */
- nfp_net_close_free_all(nn);
- nn_dbg(nn, "%s down", netdev->name);
- return 0;
- }
- static void nfp_net_set_rx_mode(struct net_device *netdev)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- u32 new_ctrl;
- new_ctrl = nn->ctrl;
- if (netdev->flags & IFF_PROMISC) {
- if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
- new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
- else
- nn_warn(nn, "FW does not support promiscuous mode\n");
- } else {
- new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
- }
- if (new_ctrl == nn->ctrl)
- return;
- nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
- nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
- nn->ctrl = new_ctrl;
- }
- static void nfp_net_rss_init_itbl(struct nfp_net *nn)
- {
- int i;
- for (i = 0; i < sizeof(nn->rss_itbl); i++)
- nn->rss_itbl[i] =
- ethtool_rxfh_indir_default(i, nn->num_rx_rings);
- }
- static int
- nfp_net_ring_swap_enable(struct nfp_net *nn, unsigned int *num_vecs,
- unsigned int *stack_tx_rings,
- struct bpf_prog **xdp_prog,
- struct nfp_net_ring_set *rx,
- struct nfp_net_ring_set *tx)
- {
- unsigned int r;
- int err;
- if (rx)
- nfp_net_rx_ring_set_swap(nn, rx);
- if (tx)
- nfp_net_tx_ring_set_swap(nn, tx);
- swap(*num_vecs, nn->num_r_vecs);
- swap(*stack_tx_rings, nn->num_stack_tx_rings);
- *xdp_prog = xchg(&nn->xdp_prog, *xdp_prog);
- for (r = 0; r < nn->max_r_vecs; r++)
- nfp_net_vector_assign_rings(nn, &nn->r_vecs[r], r);
- if (!netif_is_rxfh_configured(nn->netdev))
- nfp_net_rss_init_itbl(nn);
- err = netif_set_real_num_rx_queues(nn->netdev,
- nn->num_rx_rings);
- if (err)
- return err;
- if (nn->netdev->real_num_tx_queues != nn->num_stack_tx_rings) {
- err = netif_set_real_num_tx_queues(nn->netdev,
- nn->num_stack_tx_rings);
- if (err)
- return err;
- }
- return __nfp_net_set_config_and_enable(nn);
- }
- static int
- nfp_net_check_config(struct nfp_net *nn, struct bpf_prog *xdp_prog,
- struct nfp_net_ring_set *rx, struct nfp_net_ring_set *tx)
- {
- /* XDP-enabled tests */
- if (!xdp_prog)
- return 0;
- if (rx && nfp_net_calc_fl_bufsz(nn, rx->mtu) > PAGE_SIZE) {
- nn_warn(nn, "MTU too large w/ XDP enabled\n");
- return -EINVAL;
- }
- if (tx && tx->n_rings > nn->max_tx_rings) {
- nn_warn(nn, "Insufficient number of TX rings w/ XDP enabled\n");
- return -EINVAL;
- }
- return 0;
- }
- static void
- nfp_net_ring_reconfig_down(struct nfp_net *nn, struct bpf_prog **xdp_prog,
- struct nfp_net_ring_set *rx,
- struct nfp_net_ring_set *tx,
- unsigned int stack_tx_rings, unsigned int num_vecs)
- {
- nn->netdev->mtu = rx ? rx->mtu : nn->netdev->mtu;
- nn->fl_bufsz = nfp_net_calc_fl_bufsz(nn, nn->netdev->mtu);
- nn->rxd_cnt = rx ? rx->dcnt : nn->rxd_cnt;
- nn->txd_cnt = tx ? tx->dcnt : nn->txd_cnt;
- nn->num_rx_rings = rx ? rx->n_rings : nn->num_rx_rings;
- nn->num_tx_rings = tx ? tx->n_rings : nn->num_tx_rings;
- nn->num_stack_tx_rings = stack_tx_rings;
- nn->num_r_vecs = num_vecs;
- *xdp_prog = xchg(&nn->xdp_prog, *xdp_prog);
- if (!netif_is_rxfh_configured(nn->netdev))
- nfp_net_rss_init_itbl(nn);
- }
- int
- nfp_net_ring_reconfig(struct nfp_net *nn, struct bpf_prog **xdp_prog,
- struct nfp_net_ring_set *rx, struct nfp_net_ring_set *tx)
- {
- unsigned int stack_tx_rings, num_vecs, r;
- int err;
- stack_tx_rings = tx ? tx->n_rings : nn->num_tx_rings;
- if (*xdp_prog)
- stack_tx_rings -= rx ? rx->n_rings : nn->num_rx_rings;
- num_vecs = max(rx ? rx->n_rings : nn->num_rx_rings, stack_tx_rings);
- err = nfp_net_check_config(nn, *xdp_prog, rx, tx);
- if (err)
- return err;
- if (!netif_running(nn->netdev)) {
- nfp_net_ring_reconfig_down(nn, xdp_prog, rx, tx,
- stack_tx_rings, num_vecs);
- return 0;
- }
- /* Prepare new rings */
- for (r = nn->num_r_vecs; r < num_vecs; r++) {
- err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
- if (err) {
- num_vecs = r;
- goto err_cleanup_vecs;
- }
- }
- if (rx) {
- if (!nfp_net_rx_ring_set_prepare(nn, rx, *xdp_prog)) {
- err = -ENOMEM;
- goto err_cleanup_vecs;
- }
- }
- if (tx) {
- if (!nfp_net_tx_ring_set_prepare(nn, tx, stack_tx_rings)) {
- err = -ENOMEM;
- goto err_free_rx;
- }
- }
- /* Stop device, swap in new rings, try to start the firmware */
- nfp_net_close_stack(nn);
- nfp_net_clear_config_and_disable(nn);
- err = nfp_net_ring_swap_enable(nn, &num_vecs, &stack_tx_rings,
- xdp_prog, rx, tx);
- if (err) {
- int err2;
- nfp_net_clear_config_and_disable(nn);
- /* Try with old configuration and old rings */
- err2 = nfp_net_ring_swap_enable(nn, &num_vecs, &stack_tx_rings,
- xdp_prog, rx, tx);
- if (err2)
- nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
- err, err2);
- }
- for (r = num_vecs - 1; r >= nn->num_r_vecs; r--)
- nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
- if (rx)
- nfp_net_rx_ring_set_free(nn, rx, *xdp_prog);
- if (tx)
- nfp_net_tx_ring_set_free(nn, tx);
- nfp_net_open_stack(nn);
- return err;
- err_free_rx:
- if (rx)
- nfp_net_rx_ring_set_free(nn, rx, *xdp_prog);
- err_cleanup_vecs:
- for (r = num_vecs - 1; r >= nn->num_r_vecs; r--)
- nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
- return err;
- }
- static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- struct nfp_net_ring_set rx = {
- .n_rings = nn->num_rx_rings,
- .mtu = new_mtu,
- .dcnt = nn->rxd_cnt,
- };
- return nfp_net_ring_reconfig(nn, &nn->xdp_prog, &rx, NULL);
- }
- static struct rtnl_link_stats64 *nfp_net_stat64(struct net_device *netdev,
- struct rtnl_link_stats64 *stats)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- int r;
- for (r = 0; r < nn->num_r_vecs; r++) {
- struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
- u64 data[3];
- unsigned int start;
- do {
- start = u64_stats_fetch_begin(&r_vec->rx_sync);
- data[0] = r_vec->rx_pkts;
- data[1] = r_vec->rx_bytes;
- data[2] = r_vec->rx_drops;
- } while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
- stats->rx_packets += data[0];
- stats->rx_bytes += data[1];
- stats->rx_dropped += data[2];
- do {
- start = u64_stats_fetch_begin(&r_vec->tx_sync);
- data[0] = r_vec->tx_pkts;
- data[1] = r_vec->tx_bytes;
- data[2] = r_vec->tx_errors;
- } while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
- stats->tx_packets += data[0];
- stats->tx_bytes += data[1];
- stats->tx_errors += data[2];
- }
- return stats;
- }
- static bool nfp_net_ebpf_capable(struct nfp_net *nn)
- {
- if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
- nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
- return true;
- return false;
- }
- static int
- nfp_net_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
- struct tc_to_netdev *tc)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
- return -ENOTSUPP;
- if (proto != htons(ETH_P_ALL))
- return -ENOTSUPP;
- if (tc->type == TC_SETUP_CLSBPF && nfp_net_ebpf_capable(nn)) {
- if (!nn->bpf_offload_xdp)
- return nfp_net_bpf_offload(nn, tc->cls_bpf);
- else
- return -EBUSY;
- }
- return -EINVAL;
- }
- static int nfp_net_set_features(struct net_device *netdev,
- netdev_features_t features)
- {
- netdev_features_t changed = netdev->features ^ features;
- struct nfp_net *nn = netdev_priv(netdev);
- u32 new_ctrl;
- int err;
- /* Assume this is not called with features we have not advertised */
- new_ctrl = nn->ctrl;
- if (changed & NETIF_F_RXCSUM) {
- if (features & NETIF_F_RXCSUM)
- new_ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
- else
- new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM;
- }
- if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
- if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
- new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
- else
- new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
- }
- if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
- if (features & (NETIF_F_TSO | NETIF_F_TSO6))
- new_ctrl |= NFP_NET_CFG_CTRL_LSO;
- else
- new_ctrl &= ~NFP_NET_CFG_CTRL_LSO;
- }
- if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
- if (features & NETIF_F_HW_VLAN_CTAG_RX)
- new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
- else
- new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
- }
- if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
- if (features & NETIF_F_HW_VLAN_CTAG_TX)
- new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
- else
- new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
- }
- if (changed & NETIF_F_SG) {
- if (features & NETIF_F_SG)
- new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
- else
- new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
- }
- if (changed & NETIF_F_HW_TC && nn->ctrl & NFP_NET_CFG_CTRL_BPF) {
- nn_err(nn, "Cannot disable HW TC offload while in use\n");
- return -EBUSY;
- }
- nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
- netdev->features, features, changed);
- if (new_ctrl == nn->ctrl)
- return 0;
- nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->ctrl, new_ctrl);
- nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
- err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
- if (err)
- return err;
- nn->ctrl = new_ctrl;
- return 0;
- }
- static netdev_features_t
- nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
- netdev_features_t features)
- {
- u8 l4_hdr;
- /* We can't do TSO over double tagged packets (802.1AD) */
- features &= vlan_features_check(skb, features);
- if (!skb->encapsulation)
- return features;
- /* Ensure that inner L4 header offset fits into TX descriptor field */
- if (skb_is_gso(skb)) {
- u32 hdrlen;
- hdrlen = skb_inner_transport_header(skb) - skb->data +
- inner_tcp_hdrlen(skb);
- if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ))
- features &= ~NETIF_F_GSO_MASK;
- }
- /* VXLAN/GRE check */
- switch (vlan_get_protocol(skb)) {
- case htons(ETH_P_IP):
- l4_hdr = ip_hdr(skb)->protocol;
- break;
- case htons(ETH_P_IPV6):
- l4_hdr = ipv6_hdr(skb)->nexthdr;
- break;
- default:
- return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
- }
- if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
- skb->inner_protocol != htons(ETH_P_TEB) ||
- (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
- (l4_hdr == IPPROTO_UDP &&
- (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
- sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
- return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
- return features;
- }
- /**
- * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
- * @nn: NFP Net device to reconfigure
- * @idx: Index into the port table where new port should be written
- * @port: UDP port to configure (pass zero to remove VXLAN port)
- */
- static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
- {
- int i;
- nn->vxlan_ports[idx] = port;
- if (!(nn->ctrl & NFP_NET_CFG_CTRL_VXLAN))
- return;
- BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
- for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
- nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
- be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
- be16_to_cpu(nn->vxlan_ports[i]));
- nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
- }
- /**
- * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
- * @nn: NFP Network structure
- * @port: UDP port to look for
- *
- * Return: if the port is already in the table -- it's position;
- * if the port is not in the table -- free position to use;
- * if the table is full -- -ENOSPC.
- */
- static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
- {
- int i, free_idx = -ENOSPC;
- for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
- if (nn->vxlan_ports[i] == port)
- return i;
- if (!nn->vxlan_usecnt[i])
- free_idx = i;
- }
- return free_idx;
- }
- static void nfp_net_add_vxlan_port(struct net_device *netdev,
- struct udp_tunnel_info *ti)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- int idx;
- if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
- return;
- idx = nfp_net_find_vxlan_idx(nn, ti->port);
- if (idx == -ENOSPC)
- return;
- if (!nn->vxlan_usecnt[idx]++)
- nfp_net_set_vxlan_port(nn, idx, ti->port);
- }
- static void nfp_net_del_vxlan_port(struct net_device *netdev,
- struct udp_tunnel_info *ti)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- int idx;
- if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
- return;
- idx = nfp_net_find_vxlan_idx(nn, ti->port);
- if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
- return;
- if (!--nn->vxlan_usecnt[idx])
- nfp_net_set_vxlan_port(nn, idx, 0);
- }
- static int nfp_net_xdp_offload(struct nfp_net *nn, struct bpf_prog *prog)
- {
- struct tc_cls_bpf_offload cmd = {
- .prog = prog,
- };
- int ret;
- if (!nfp_net_ebpf_capable(nn))
- return -EINVAL;
- if (nn->ctrl & NFP_NET_CFG_CTRL_BPF) {
- if (!nn->bpf_offload_xdp)
- return prog ? -EBUSY : 0;
- cmd.command = prog ? TC_CLSBPF_REPLACE : TC_CLSBPF_DESTROY;
- } else {
- if (!prog)
- return 0;
- cmd.command = TC_CLSBPF_ADD;
- }
- ret = nfp_net_bpf_offload(nn, &cmd);
- /* Stop offload if replace not possible */
- if (ret && cmd.command == TC_CLSBPF_REPLACE)
- nfp_net_xdp_offload(nn, NULL);
- nn->bpf_offload_xdp = prog && !ret;
- return ret;
- }
- static int nfp_net_xdp_setup(struct nfp_net *nn, struct bpf_prog *prog)
- {
- struct nfp_net_ring_set rx = {
- .n_rings = nn->num_rx_rings,
- .mtu = nn->netdev->mtu,
- .dcnt = nn->rxd_cnt,
- };
- struct nfp_net_ring_set tx = {
- .n_rings = nn->num_tx_rings,
- .dcnt = nn->txd_cnt,
- };
- int err;
- if (prog && prog->xdp_adjust_head) {
- nn_err(nn, "Does not support bpf_xdp_adjust_head()\n");
- return -EOPNOTSUPP;
- }
- if (!prog && !nn->xdp_prog)
- return 0;
- if (prog && nn->xdp_prog) {
- prog = xchg(&nn->xdp_prog, prog);
- bpf_prog_put(prog);
- nfp_net_xdp_offload(nn, nn->xdp_prog);
- return 0;
- }
- tx.n_rings += prog ? nn->num_rx_rings : -nn->num_rx_rings;
- /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
- err = nfp_net_ring_reconfig(nn, &prog, &rx, &tx);
- if (err)
- return err;
- /* @prog got swapped and is now the old one */
- if (prog)
- bpf_prog_put(prog);
- nfp_net_xdp_offload(nn, nn->xdp_prog);
- return 0;
- }
- static int nfp_net_xdp(struct net_device *netdev, struct netdev_xdp *xdp)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- switch (xdp->command) {
- case XDP_SETUP_PROG:
- return nfp_net_xdp_setup(nn, xdp->prog);
- case XDP_QUERY_PROG:
- xdp->prog_attached = !!nn->xdp_prog;
- return 0;
- default:
- return -EINVAL;
- }
- }
- static const struct net_device_ops nfp_net_netdev_ops = {
- .ndo_open = nfp_net_netdev_open,
- .ndo_stop = nfp_net_netdev_close,
- .ndo_start_xmit = nfp_net_tx,
- .ndo_get_stats64 = nfp_net_stat64,
- .ndo_setup_tc = nfp_net_setup_tc,
- .ndo_tx_timeout = nfp_net_tx_timeout,
- .ndo_set_rx_mode = nfp_net_set_rx_mode,
- .ndo_change_mtu = nfp_net_change_mtu,
- .ndo_set_mac_address = eth_mac_addr,
- .ndo_set_features = nfp_net_set_features,
- .ndo_features_check = nfp_net_features_check,
- .ndo_udp_tunnel_add = nfp_net_add_vxlan_port,
- .ndo_udp_tunnel_del = nfp_net_del_vxlan_port,
- .ndo_xdp = nfp_net_xdp,
- };
- /**
- * nfp_net_info() - Print general info about the NIC
- * @nn: NFP Net device to reconfigure
- */
- void nfp_net_info(struct nfp_net *nn)
- {
- nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
- nn->is_vf ? "VF " : "",
- nn->num_tx_rings, nn->max_tx_rings,
- nn->num_rx_rings, nn->max_rx_rings);
- nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
- nn->fw_ver.resv, nn->fw_ver.class,
- nn->fw_ver.major, nn->fw_ver.minor,
- nn->max_mtu);
- nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
- nn->cap,
- nn->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
- nn->cap & NFP_NET_CFG_CTRL_L2BC ? "L2BCFILT " : "",
- nn->cap & NFP_NET_CFG_CTRL_L2MC ? "L2MCFILT " : "",
- nn->cap & NFP_NET_CFG_CTRL_RXCSUM ? "RXCSUM " : "",
- nn->cap & NFP_NET_CFG_CTRL_TXCSUM ? "TXCSUM " : "",
- nn->cap & NFP_NET_CFG_CTRL_RXVLAN ? "RXVLAN " : "",
- nn->cap & NFP_NET_CFG_CTRL_TXVLAN ? "TXVLAN " : "",
- nn->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
- nn->cap & NFP_NET_CFG_CTRL_GATHER ? "GATHER " : "",
- nn->cap & NFP_NET_CFG_CTRL_LSO ? "TSO " : "",
- nn->cap & NFP_NET_CFG_CTRL_RSS ? "RSS " : "",
- nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
- nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
- nn->cap & NFP_NET_CFG_CTRL_IRQMOD ? "IRQMOD " : "",
- nn->cap & NFP_NET_CFG_CTRL_VXLAN ? "VXLAN " : "",
- nn->cap & NFP_NET_CFG_CTRL_NVGRE ? "NVGRE " : "",
- nfp_net_ebpf_capable(nn) ? "BPF " : "");
- }
- /**
- * nfp_net_netdev_alloc() - Allocate netdev and related structure
- * @pdev: PCI device
- * @max_tx_rings: Maximum number of TX rings supported by device
- * @max_rx_rings: Maximum number of RX rings supported by device
- *
- * This function allocates a netdev device and fills in the initial
- * part of the @struct nfp_net structure.
- *
- * Return: NFP Net device structure, or ERR_PTR on error.
- */
- struct nfp_net *nfp_net_netdev_alloc(struct pci_dev *pdev,
- unsigned int max_tx_rings,
- unsigned int max_rx_rings)
- {
- struct net_device *netdev;
- struct nfp_net *nn;
- netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
- max_tx_rings, max_rx_rings);
- if (!netdev)
- return ERR_PTR(-ENOMEM);
- SET_NETDEV_DEV(netdev, &pdev->dev);
- nn = netdev_priv(netdev);
- nn->netdev = netdev;
- nn->pdev = pdev;
- nn->max_tx_rings = max_tx_rings;
- nn->max_rx_rings = max_rx_rings;
- nn->num_tx_rings = min_t(unsigned int, max_tx_rings, num_online_cpus());
- nn->num_rx_rings = min_t(unsigned int, max_rx_rings,
- netif_get_num_default_rss_queues());
- nn->num_r_vecs = max(nn->num_tx_rings, nn->num_rx_rings);
- nn->num_r_vecs = min_t(unsigned int, nn->num_r_vecs, num_online_cpus());
- nn->txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
- nn->rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
- spin_lock_init(&nn->reconfig_lock);
- spin_lock_init(&nn->rx_filter_lock);
- spin_lock_init(&nn->link_status_lock);
- setup_timer(&nn->reconfig_timer,
- nfp_net_reconfig_timer, (unsigned long)nn);
- setup_timer(&nn->rx_filter_stats_timer,
- nfp_net_filter_stats_timer, (unsigned long)nn);
- return nn;
- }
- /**
- * nfp_net_netdev_free() - Undo what @nfp_net_netdev_alloc() did
- * @nn: NFP Net device to reconfigure
- */
- void nfp_net_netdev_free(struct nfp_net *nn)
- {
- free_netdev(nn->netdev);
- }
- /**
- * nfp_net_rss_init() - Set the initial RSS parameters
- * @nn: NFP Net device to reconfigure
- */
- static void nfp_net_rss_init(struct nfp_net *nn)
- {
- netdev_rss_key_fill(nn->rss_key, NFP_NET_CFG_RSS_KEY_SZ);
- nfp_net_rss_init_itbl(nn);
- /* Enable IPv4/IPv6 TCP by default */
- nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
- NFP_NET_CFG_RSS_IPV6_TCP |
- NFP_NET_CFG_RSS_TOEPLITZ |
- NFP_NET_CFG_RSS_MASK;
- }
- /**
- * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
- * @nn: NFP Net device to reconfigure
- */
- static void nfp_net_irqmod_init(struct nfp_net *nn)
- {
- nn->rx_coalesce_usecs = 50;
- nn->rx_coalesce_max_frames = 64;
- nn->tx_coalesce_usecs = 50;
- nn->tx_coalesce_max_frames = 64;
- }
- /**
- * nfp_net_netdev_init() - Initialise/finalise the netdev structure
- * @netdev: netdev structure
- *
- * Return: 0 on success or negative errno on error.
- */
- int nfp_net_netdev_init(struct net_device *netdev)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- int err;
- /* Get some of the read-only fields from the BAR */
- nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
- nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
- nfp_net_write_mac_addr(nn);
- /* Determine RX packet/metadata boundary offset */
- if (nn->fw_ver.major >= 2)
- nn->rx_offset = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
- else
- nn->rx_offset = NFP_NET_RX_OFFSET;
- /* Set default MTU and Freelist buffer size */
- if (nn->max_mtu < NFP_NET_DEFAULT_MTU)
- netdev->mtu = nn->max_mtu;
- else
- netdev->mtu = NFP_NET_DEFAULT_MTU;
- nn->fl_bufsz = nfp_net_calc_fl_bufsz(nn, netdev->mtu);
- /* Advertise/enable offloads based on capabilities
- *
- * Note: netdev->features show the currently enabled features
- * and netdev->hw_features advertises which features are
- * supported. By default we enable most features.
- */
- netdev->hw_features = NETIF_F_HIGHDMA;
- if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM) {
- netdev->hw_features |= NETIF_F_RXCSUM;
- nn->ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
- }
- if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
- netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
- nn->ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
- }
- if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
- netdev->hw_features |= NETIF_F_SG;
- nn->ctrl |= NFP_NET_CFG_CTRL_GATHER;
- }
- if ((nn->cap & NFP_NET_CFG_CTRL_LSO) && nn->fw_ver.major > 2) {
- netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
- nn->ctrl |= NFP_NET_CFG_CTRL_LSO;
- }
- if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
- netdev->hw_features |= NETIF_F_RXHASH;
- nfp_net_rss_init(nn);
- nn->ctrl |= NFP_NET_CFG_CTRL_RSS;
- }
- if (nn->cap & NFP_NET_CFG_CTRL_VXLAN &&
- nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
- if (nn->cap & NFP_NET_CFG_CTRL_LSO)
- netdev->hw_features |= NETIF_F_GSO_GRE |
- NETIF_F_GSO_UDP_TUNNEL;
- nn->ctrl |= NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE;
- netdev->hw_enc_features = netdev->hw_features;
- }
- netdev->vlan_features = netdev->hw_features;
- if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
- netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
- nn->ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
- }
- if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
- netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
- nn->ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
- }
- netdev->features = netdev->hw_features;
- if (nfp_net_ebpf_capable(nn))
- netdev->hw_features |= NETIF_F_HW_TC;
- /* Advertise but disable TSO by default. */
- netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
- /* Allow L2 Broadcast and Multicast through by default, if supported */
- if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
- nn->ctrl |= NFP_NET_CFG_CTRL_L2BC;
- if (nn->cap & NFP_NET_CFG_CTRL_L2MC)
- nn->ctrl |= NFP_NET_CFG_CTRL_L2MC;
- /* Allow IRQ moderation, if supported */
- if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
- nfp_net_irqmod_init(nn);
- nn->ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
- }
- /* Stash the re-configuration queue away. First odd queue in TX Bar */
- nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
- /* Make sure the FW knows the netdev is supposed to be disabled here */
- nn_writel(nn, NFP_NET_CFG_CTRL, 0);
- nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
- nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
- err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
- NFP_NET_CFG_UPDATE_GEN);
- if (err)
- return err;
- /* Finalise the netdev setup */
- netdev->netdev_ops = &nfp_net_netdev_ops;
- netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
- /* MTU range: 68 - hw-specific max */
- netdev->min_mtu = ETH_MIN_MTU;
- netdev->max_mtu = nn->max_mtu;
- netif_carrier_off(netdev);
- nfp_net_set_ethtool_ops(netdev);
- nfp_net_irqs_assign(netdev);
- return register_netdev(netdev);
- }
- /**
- * nfp_net_netdev_clean() - Undo what nfp_net_netdev_init() did.
- * @netdev: netdev structure
- */
- void nfp_net_netdev_clean(struct net_device *netdev)
- {
- struct nfp_net *nn = netdev_priv(netdev);
- if (nn->xdp_prog)
- bpf_prog_put(nn->xdp_prog);
- if (nn->bpf_offload_xdp)
- nfp_net_xdp_offload(nn, NULL);
- unregister_netdev(nn->netdev);
- }
|