nfp_net_common.c 86 KB

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