nfp_net_common.c 86 KB

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