nfp_net_common.c 85 KB

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