nfp_net_common.c 76 KB

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