nfp_net_common.c 98 KB

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