wx_lib.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019 - 2022 Beijing WangXun Technology Co., Ltd. */
  3. #include <linux/etherdevice.h>
  4. #include <net/ip6_checksum.h>
  5. #include <net/page_pool/helpers.h>
  6. #include <net/inet_ecn.h>
  7. #include <linux/iopoll.h>
  8. #include <linux/sctp.h>
  9. #include <linux/pci.h>
  10. #include <net/tcp.h>
  11. #include <net/ip.h>
  12. #include "wx_type.h"
  13. #include "wx_lib.h"
  14. #include "wx_hw.h"
  15. /* Lookup table mapping the HW PTYPE to the bit field for decoding */
  16. static struct wx_dec_ptype wx_ptype_lookup[256] = {
  17. /* L2: mac */
  18. [0x11] = WX_PTT(L2, NONE, NONE, NONE, NONE, PAY2),
  19. [0x12] = WX_PTT(L2, NONE, NONE, NONE, TS, PAY2),
  20. [0x13] = WX_PTT(L2, NONE, NONE, NONE, NONE, PAY2),
  21. [0x14] = WX_PTT(L2, NONE, NONE, NONE, NONE, PAY2),
  22. [0x15] = WX_PTT(L2, NONE, NONE, NONE, NONE, NONE),
  23. [0x16] = WX_PTT(L2, NONE, NONE, NONE, NONE, PAY2),
  24. [0x17] = WX_PTT(L2, NONE, NONE, NONE, NONE, NONE),
  25. /* L2: ethertype filter */
  26. [0x18 ... 0x1F] = WX_PTT(L2, NONE, NONE, NONE, NONE, NONE),
  27. /* L3: ip non-tunnel */
  28. [0x21] = WX_PTT(IP, FGV4, NONE, NONE, NONE, PAY3),
  29. [0x22] = WX_PTT(IP, IPV4, NONE, NONE, NONE, PAY3),
  30. [0x23] = WX_PTT(IP, IPV4, NONE, NONE, UDP, PAY4),
  31. [0x24] = WX_PTT(IP, IPV4, NONE, NONE, TCP, PAY4),
  32. [0x25] = WX_PTT(IP, IPV4, NONE, NONE, SCTP, PAY4),
  33. [0x29] = WX_PTT(IP, FGV6, NONE, NONE, NONE, PAY3),
  34. [0x2A] = WX_PTT(IP, IPV6, NONE, NONE, NONE, PAY3),
  35. [0x2B] = WX_PTT(IP, IPV6, NONE, NONE, UDP, PAY3),
  36. [0x2C] = WX_PTT(IP, IPV6, NONE, NONE, TCP, PAY4),
  37. [0x2D] = WX_PTT(IP, IPV6, NONE, NONE, SCTP, PAY4),
  38. /* L2: fcoe */
  39. [0x30 ... 0x34] = WX_PTT(FCOE, NONE, NONE, NONE, NONE, PAY3),
  40. [0x38 ... 0x3C] = WX_PTT(FCOE, NONE, NONE, NONE, NONE, PAY3),
  41. /* IPv4 --> IPv4/IPv6 */
  42. [0x81] = WX_PTT(IP, IPV4, IPIP, FGV4, NONE, PAY3),
  43. [0x82] = WX_PTT(IP, IPV4, IPIP, IPV4, NONE, PAY3),
  44. [0x83] = WX_PTT(IP, IPV4, IPIP, IPV4, UDP, PAY4),
  45. [0x84] = WX_PTT(IP, IPV4, IPIP, IPV4, TCP, PAY4),
  46. [0x85] = WX_PTT(IP, IPV4, IPIP, IPV4, SCTP, PAY4),
  47. [0x89] = WX_PTT(IP, IPV4, IPIP, FGV6, NONE, PAY3),
  48. [0x8A] = WX_PTT(IP, IPV4, IPIP, IPV6, NONE, PAY3),
  49. [0x8B] = WX_PTT(IP, IPV4, IPIP, IPV6, UDP, PAY4),
  50. [0x8C] = WX_PTT(IP, IPV4, IPIP, IPV6, TCP, PAY4),
  51. [0x8D] = WX_PTT(IP, IPV4, IPIP, IPV6, SCTP, PAY4),
  52. /* IPv4 --> GRE/NAT --> NONE/IPv4/IPv6 */
  53. [0x90] = WX_PTT(IP, IPV4, IG, NONE, NONE, PAY3),
  54. [0x91] = WX_PTT(IP, IPV4, IG, FGV4, NONE, PAY3),
  55. [0x92] = WX_PTT(IP, IPV4, IG, IPV4, NONE, PAY3),
  56. [0x93] = WX_PTT(IP, IPV4, IG, IPV4, UDP, PAY4),
  57. [0x94] = WX_PTT(IP, IPV4, IG, IPV4, TCP, PAY4),
  58. [0x95] = WX_PTT(IP, IPV4, IG, IPV4, SCTP, PAY4),
  59. [0x99] = WX_PTT(IP, IPV4, IG, FGV6, NONE, PAY3),
  60. [0x9A] = WX_PTT(IP, IPV4, IG, IPV6, NONE, PAY3),
  61. [0x9B] = WX_PTT(IP, IPV4, IG, IPV6, UDP, PAY4),
  62. [0x9C] = WX_PTT(IP, IPV4, IG, IPV6, TCP, PAY4),
  63. [0x9D] = WX_PTT(IP, IPV4, IG, IPV6, SCTP, PAY4),
  64. /* IPv4 --> GRE/NAT --> MAC --> NONE/IPv4/IPv6 */
  65. [0xA0] = WX_PTT(IP, IPV4, IGM, NONE, NONE, PAY3),
  66. [0xA1] = WX_PTT(IP, IPV4, IGM, FGV4, NONE, PAY3),
  67. [0xA2] = WX_PTT(IP, IPV4, IGM, IPV4, NONE, PAY3),
  68. [0xA3] = WX_PTT(IP, IPV4, IGM, IPV4, UDP, PAY4),
  69. [0xA4] = WX_PTT(IP, IPV4, IGM, IPV4, TCP, PAY4),
  70. [0xA5] = WX_PTT(IP, IPV4, IGM, IPV4, SCTP, PAY4),
  71. [0xA9] = WX_PTT(IP, IPV4, IGM, FGV6, NONE, PAY3),
  72. [0xAA] = WX_PTT(IP, IPV4, IGM, IPV6, NONE, PAY3),
  73. [0xAB] = WX_PTT(IP, IPV4, IGM, IPV6, UDP, PAY4),
  74. [0xAC] = WX_PTT(IP, IPV4, IGM, IPV6, TCP, PAY4),
  75. [0xAD] = WX_PTT(IP, IPV4, IGM, IPV6, SCTP, PAY4),
  76. /* IPv4 --> GRE/NAT --> MAC+VLAN --> NONE/IPv4/IPv6 */
  77. [0xB0] = WX_PTT(IP, IPV4, IGMV, NONE, NONE, PAY3),
  78. [0xB1] = WX_PTT(IP, IPV4, IGMV, FGV4, NONE, PAY3),
  79. [0xB2] = WX_PTT(IP, IPV4, IGMV, IPV4, NONE, PAY3),
  80. [0xB3] = WX_PTT(IP, IPV4, IGMV, IPV4, UDP, PAY4),
  81. [0xB4] = WX_PTT(IP, IPV4, IGMV, IPV4, TCP, PAY4),
  82. [0xB5] = WX_PTT(IP, IPV4, IGMV, IPV4, SCTP, PAY4),
  83. [0xB9] = WX_PTT(IP, IPV4, IGMV, FGV6, NONE, PAY3),
  84. [0xBA] = WX_PTT(IP, IPV4, IGMV, IPV6, NONE, PAY3),
  85. [0xBB] = WX_PTT(IP, IPV4, IGMV, IPV6, UDP, PAY4),
  86. [0xBC] = WX_PTT(IP, IPV4, IGMV, IPV6, TCP, PAY4),
  87. [0xBD] = WX_PTT(IP, IPV4, IGMV, IPV6, SCTP, PAY4),
  88. /* IPv6 --> IPv4/IPv6 */
  89. [0xC1] = WX_PTT(IP, IPV6, IPIP, FGV4, NONE, PAY3),
  90. [0xC2] = WX_PTT(IP, IPV6, IPIP, IPV4, NONE, PAY3),
  91. [0xC3] = WX_PTT(IP, IPV6, IPIP, IPV4, UDP, PAY4),
  92. [0xC4] = WX_PTT(IP, IPV6, IPIP, IPV4, TCP, PAY4),
  93. [0xC5] = WX_PTT(IP, IPV6, IPIP, IPV4, SCTP, PAY4),
  94. [0xC9] = WX_PTT(IP, IPV6, IPIP, FGV6, NONE, PAY3),
  95. [0xCA] = WX_PTT(IP, IPV6, IPIP, IPV6, NONE, PAY3),
  96. [0xCB] = WX_PTT(IP, IPV6, IPIP, IPV6, UDP, PAY4),
  97. [0xCC] = WX_PTT(IP, IPV6, IPIP, IPV6, TCP, PAY4),
  98. [0xCD] = WX_PTT(IP, IPV6, IPIP, IPV6, SCTP, PAY4),
  99. /* IPv6 --> GRE/NAT -> NONE/IPv4/IPv6 */
  100. [0xD0] = WX_PTT(IP, IPV6, IG, NONE, NONE, PAY3),
  101. [0xD1] = WX_PTT(IP, IPV6, IG, FGV4, NONE, PAY3),
  102. [0xD2] = WX_PTT(IP, IPV6, IG, IPV4, NONE, PAY3),
  103. [0xD3] = WX_PTT(IP, IPV6, IG, IPV4, UDP, PAY4),
  104. [0xD4] = WX_PTT(IP, IPV6, IG, IPV4, TCP, PAY4),
  105. [0xD5] = WX_PTT(IP, IPV6, IG, IPV4, SCTP, PAY4),
  106. [0xD9] = WX_PTT(IP, IPV6, IG, FGV6, NONE, PAY3),
  107. [0xDA] = WX_PTT(IP, IPV6, IG, IPV6, NONE, PAY3),
  108. [0xDB] = WX_PTT(IP, IPV6, IG, IPV6, UDP, PAY4),
  109. [0xDC] = WX_PTT(IP, IPV6, IG, IPV6, TCP, PAY4),
  110. [0xDD] = WX_PTT(IP, IPV6, IG, IPV6, SCTP, PAY4),
  111. /* IPv6 --> GRE/NAT -> MAC -> NONE/IPv4/IPv6 */
  112. [0xE0] = WX_PTT(IP, IPV6, IGM, NONE, NONE, PAY3),
  113. [0xE1] = WX_PTT(IP, IPV6, IGM, FGV4, NONE, PAY3),
  114. [0xE2] = WX_PTT(IP, IPV6, IGM, IPV4, NONE, PAY3),
  115. [0xE3] = WX_PTT(IP, IPV6, IGM, IPV4, UDP, PAY4),
  116. [0xE4] = WX_PTT(IP, IPV6, IGM, IPV4, TCP, PAY4),
  117. [0xE5] = WX_PTT(IP, IPV6, IGM, IPV4, SCTP, PAY4),
  118. [0xE9] = WX_PTT(IP, IPV6, IGM, FGV6, NONE, PAY3),
  119. [0xEA] = WX_PTT(IP, IPV6, IGM, IPV6, NONE, PAY3),
  120. [0xEB] = WX_PTT(IP, IPV6, IGM, IPV6, UDP, PAY4),
  121. [0xEC] = WX_PTT(IP, IPV6, IGM, IPV6, TCP, PAY4),
  122. [0xED] = WX_PTT(IP, IPV6, IGM, IPV6, SCTP, PAY4),
  123. /* IPv6 --> GRE/NAT -> MAC--> NONE/IPv */
  124. [0xF0] = WX_PTT(IP, IPV6, IGMV, NONE, NONE, PAY3),
  125. [0xF1] = WX_PTT(IP, IPV6, IGMV, FGV4, NONE, PAY3),
  126. [0xF2] = WX_PTT(IP, IPV6, IGMV, IPV4, NONE, PAY3),
  127. [0xF3] = WX_PTT(IP, IPV6, IGMV, IPV4, UDP, PAY4),
  128. [0xF4] = WX_PTT(IP, IPV6, IGMV, IPV4, TCP, PAY4),
  129. [0xF5] = WX_PTT(IP, IPV6, IGMV, IPV4, SCTP, PAY4),
  130. [0xF9] = WX_PTT(IP, IPV6, IGMV, FGV6, NONE, PAY3),
  131. [0xFA] = WX_PTT(IP, IPV6, IGMV, IPV6, NONE, PAY3),
  132. [0xFB] = WX_PTT(IP, IPV6, IGMV, IPV6, UDP, PAY4),
  133. [0xFC] = WX_PTT(IP, IPV6, IGMV, IPV6, TCP, PAY4),
  134. [0xFD] = WX_PTT(IP, IPV6, IGMV, IPV6, SCTP, PAY4),
  135. };
  136. struct wx_dec_ptype wx_decode_ptype(const u8 ptype)
  137. {
  138. return wx_ptype_lookup[ptype];
  139. }
  140. EXPORT_SYMBOL(wx_decode_ptype);
  141. /* wx_test_staterr - tests bits in Rx descriptor status and error fields */
  142. static __le32 wx_test_staterr(union wx_rx_desc *rx_desc,
  143. const u32 stat_err_bits)
  144. {
  145. return rx_desc->wb.upper.status_error & cpu_to_le32(stat_err_bits);
  146. }
  147. static void wx_dma_sync_frag(struct wx_ring *rx_ring,
  148. struct wx_rx_buffer *rx_buffer)
  149. {
  150. struct sk_buff *skb = rx_buffer->skb;
  151. skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
  152. dma_sync_single_range_for_cpu(rx_ring->dev,
  153. WX_CB(skb)->dma,
  154. skb_frag_off(frag),
  155. skb_frag_size(frag),
  156. DMA_FROM_DEVICE);
  157. }
  158. static struct wx_rx_buffer *wx_get_rx_buffer(struct wx_ring *rx_ring,
  159. union wx_rx_desc *rx_desc,
  160. struct sk_buff **skb,
  161. int *rx_buffer_pgcnt)
  162. {
  163. struct wx_rx_buffer *rx_buffer;
  164. unsigned int size;
  165. rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
  166. size = le16_to_cpu(rx_desc->wb.upper.length);
  167. #if (PAGE_SIZE < 8192)
  168. *rx_buffer_pgcnt = page_count(rx_buffer->page);
  169. #else
  170. *rx_buffer_pgcnt = 0;
  171. #endif
  172. prefetchw(rx_buffer->page);
  173. *skb = rx_buffer->skb;
  174. /* Delay unmapping of the first packet. It carries the header
  175. * information, HW may still access the header after the writeback.
  176. * Only unmap it when EOP is reached
  177. */
  178. if (!wx_test_staterr(rx_desc, WX_RXD_STAT_EOP)) {
  179. if (!*skb)
  180. goto skip_sync;
  181. } else {
  182. if (*skb)
  183. wx_dma_sync_frag(rx_ring, rx_buffer);
  184. }
  185. /* we are reusing so sync this buffer for CPU use */
  186. dma_sync_single_range_for_cpu(rx_ring->dev,
  187. rx_buffer->dma,
  188. rx_buffer->page_offset,
  189. size,
  190. DMA_FROM_DEVICE);
  191. skip_sync:
  192. return rx_buffer;
  193. }
  194. static void wx_put_rx_buffer(struct wx_ring *rx_ring,
  195. struct wx_rx_buffer *rx_buffer,
  196. struct sk_buff *skb,
  197. int rx_buffer_pgcnt)
  198. {
  199. /* clear contents of rx_buffer */
  200. rx_buffer->page = NULL;
  201. rx_buffer->skb = NULL;
  202. }
  203. static struct sk_buff *wx_build_skb(struct wx_ring *rx_ring,
  204. struct wx_rx_buffer *rx_buffer,
  205. union wx_rx_desc *rx_desc)
  206. {
  207. unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
  208. #if (PAGE_SIZE < 8192)
  209. unsigned int truesize = WX_RX_BUFSZ;
  210. #else
  211. unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
  212. #endif
  213. struct sk_buff *skb = rx_buffer->skb;
  214. if (!skb) {
  215. void *page_addr = page_address(rx_buffer->page) +
  216. rx_buffer->page_offset;
  217. /* prefetch first cache line of first page */
  218. net_prefetch(page_addr);
  219. /* allocate a skb to store the frags */
  220. skb = napi_alloc_skb(&rx_ring->q_vector->napi, WX_RXBUFFER_256);
  221. if (unlikely(!skb))
  222. return NULL;
  223. /* we will be copying header into skb->data in
  224. * pskb_may_pull so it is in our interest to prefetch
  225. * it now to avoid a possible cache miss
  226. */
  227. prefetchw(skb->data);
  228. if (size <= WX_RXBUFFER_256) {
  229. memcpy(__skb_put(skb, size), page_addr,
  230. ALIGN(size, sizeof(long)));
  231. page_pool_put_full_page(rx_ring->page_pool, rx_buffer->page, true);
  232. return skb;
  233. }
  234. skb_mark_for_recycle(skb);
  235. if (!wx_test_staterr(rx_desc, WX_RXD_STAT_EOP))
  236. WX_CB(skb)->dma = rx_buffer->dma;
  237. skb_add_rx_frag(skb, 0, rx_buffer->page,
  238. rx_buffer->page_offset,
  239. size, truesize);
  240. goto out;
  241. } else {
  242. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  243. rx_buffer->page_offset, size, truesize);
  244. }
  245. out:
  246. #if (PAGE_SIZE < 8192)
  247. /* flip page offset to other buffer */
  248. rx_buffer->page_offset ^= truesize;
  249. #else
  250. /* move offset up to the next cache line */
  251. rx_buffer->page_offset += truesize;
  252. #endif
  253. return skb;
  254. }
  255. static bool wx_alloc_mapped_page(struct wx_ring *rx_ring,
  256. struct wx_rx_buffer *bi)
  257. {
  258. struct page *page = bi->page;
  259. dma_addr_t dma;
  260. /* since we are recycling buffers we should seldom need to alloc */
  261. if (likely(page))
  262. return true;
  263. page = page_pool_dev_alloc_pages(rx_ring->page_pool);
  264. if (unlikely(!page))
  265. return false;
  266. dma = page_pool_get_dma_addr(page);
  267. bi->dma = dma;
  268. bi->page = page;
  269. bi->page_offset = 0;
  270. return true;
  271. }
  272. /**
  273. * wx_alloc_rx_buffers - Replace used receive buffers
  274. * @rx_ring: ring to place buffers on
  275. * @cleaned_count: number of buffers to replace
  276. **/
  277. void wx_alloc_rx_buffers(struct wx_ring *rx_ring, u16 cleaned_count)
  278. {
  279. u16 i = rx_ring->next_to_use;
  280. union wx_rx_desc *rx_desc;
  281. struct wx_rx_buffer *bi;
  282. /* nothing to do */
  283. if (!cleaned_count)
  284. return;
  285. rx_desc = WX_RX_DESC(rx_ring, i);
  286. bi = &rx_ring->rx_buffer_info[i];
  287. i -= rx_ring->count;
  288. do {
  289. if (!wx_alloc_mapped_page(rx_ring, bi))
  290. break;
  291. /* sync the buffer for use by the device */
  292. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  293. bi->page_offset,
  294. WX_RX_BUFSZ,
  295. DMA_FROM_DEVICE);
  296. rx_desc->read.pkt_addr =
  297. cpu_to_le64(bi->dma + bi->page_offset);
  298. rx_desc++;
  299. bi++;
  300. i++;
  301. if (unlikely(!i)) {
  302. rx_desc = WX_RX_DESC(rx_ring, 0);
  303. bi = rx_ring->rx_buffer_info;
  304. i -= rx_ring->count;
  305. }
  306. /* clear the status bits for the next_to_use descriptor */
  307. rx_desc->wb.upper.status_error = 0;
  308. /* clear the length for the next_to_use descriptor */
  309. rx_desc->wb.upper.length = 0;
  310. cleaned_count--;
  311. } while (cleaned_count);
  312. i += rx_ring->count;
  313. if (rx_ring->next_to_use != i) {
  314. rx_ring->next_to_use = i;
  315. /* update next to alloc since we have filled the ring */
  316. rx_ring->next_to_alloc = i;
  317. /* Force memory writes to complete before letting h/w
  318. * know there are new descriptors to fetch. (Only
  319. * applicable for weak-ordered memory model archs,
  320. * such as IA-64).
  321. */
  322. wmb();
  323. writel(i, rx_ring->tail);
  324. }
  325. }
  326. u16 wx_desc_unused(struct wx_ring *ring)
  327. {
  328. u16 ntc = ring->next_to_clean;
  329. u16 ntu = ring->next_to_use;
  330. return ((ntc > ntu) ? 0 : ring->count) + ntc - ntu - 1;
  331. }
  332. /**
  333. * wx_is_non_eop - process handling of non-EOP buffers
  334. * @rx_ring: Rx ring being processed
  335. * @rx_desc: Rx descriptor for current buffer
  336. * @skb: Current socket buffer containing buffer in progress
  337. *
  338. * This function updates next to clean. If the buffer is an EOP buffer
  339. * this function exits returning false, otherwise it will place the
  340. * sk_buff in the next buffer to be chained and return true indicating
  341. * that this is in fact a non-EOP buffer.
  342. **/
  343. static bool wx_is_non_eop(struct wx_ring *rx_ring,
  344. union wx_rx_desc *rx_desc,
  345. struct sk_buff *skb)
  346. {
  347. u32 ntc = rx_ring->next_to_clean + 1;
  348. /* fetch, update, and store next to clean */
  349. ntc = (ntc < rx_ring->count) ? ntc : 0;
  350. rx_ring->next_to_clean = ntc;
  351. prefetch(WX_RX_DESC(rx_ring, ntc));
  352. /* if we are the last buffer then there is nothing else to do */
  353. if (likely(wx_test_staterr(rx_desc, WX_RXD_STAT_EOP)))
  354. return false;
  355. rx_ring->rx_buffer_info[ntc].skb = skb;
  356. rx_ring->rx_stats.non_eop_descs++;
  357. return true;
  358. }
  359. static void wx_pull_tail(struct sk_buff *skb)
  360. {
  361. skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
  362. unsigned int pull_len;
  363. unsigned char *va;
  364. /* it is valid to use page_address instead of kmap since we are
  365. * working with pages allocated out of the lomem pool per
  366. * alloc_page(GFP_ATOMIC)
  367. */
  368. va = skb_frag_address(frag);
  369. /* we need the header to contain the greater of either ETH_HLEN or
  370. * 60 bytes if the skb->len is less than 60 for skb_pad.
  371. */
  372. pull_len = eth_get_headlen(skb->dev, va, WX_RXBUFFER_256);
  373. /* align pull length to size of long to optimize memcpy performance */
  374. skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
  375. /* update all of the pointers */
  376. skb_frag_size_sub(frag, pull_len);
  377. skb_frag_off_add(frag, pull_len);
  378. skb->data_len -= pull_len;
  379. skb->tail += pull_len;
  380. }
  381. /**
  382. * wx_cleanup_headers - Correct corrupted or empty headers
  383. * @rx_ring: rx descriptor ring packet is being transacted on
  384. * @rx_desc: pointer to the EOP Rx descriptor
  385. * @skb: pointer to current skb being fixed
  386. *
  387. * Check for corrupted packet headers caused by senders on the local L2
  388. * embedded NIC switch not setting up their Tx Descriptors right. These
  389. * should be very rare.
  390. *
  391. * Also address the case where we are pulling data in on pages only
  392. * and as such no data is present in the skb header.
  393. *
  394. * In addition if skb is not at least 60 bytes we need to pad it so that
  395. * it is large enough to qualify as a valid Ethernet frame.
  396. *
  397. * Returns true if an error was encountered and skb was freed.
  398. **/
  399. static bool wx_cleanup_headers(struct wx_ring *rx_ring,
  400. union wx_rx_desc *rx_desc,
  401. struct sk_buff *skb)
  402. {
  403. struct net_device *netdev = rx_ring->netdev;
  404. /* verify that the packet does not have any known errors */
  405. if (!netdev ||
  406. unlikely(wx_test_staterr(rx_desc, WX_RXD_ERR_RXE) &&
  407. !(netdev->features & NETIF_F_RXALL))) {
  408. dev_kfree_skb_any(skb);
  409. return true;
  410. }
  411. /* place header in linear portion of buffer */
  412. if (!skb_headlen(skb))
  413. wx_pull_tail(skb);
  414. /* if eth_skb_pad returns an error the skb was freed */
  415. if (eth_skb_pad(skb))
  416. return true;
  417. return false;
  418. }
  419. static void wx_rx_hash(struct wx_ring *ring,
  420. union wx_rx_desc *rx_desc,
  421. struct sk_buff *skb)
  422. {
  423. u16 rss_type;
  424. if (!(ring->netdev->features & NETIF_F_RXHASH))
  425. return;
  426. rss_type = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info) &
  427. WX_RXD_RSSTYPE_MASK;
  428. if (!rss_type)
  429. return;
  430. skb_set_hash(skb, le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
  431. (WX_RSS_L4_TYPES_MASK & (1ul << rss_type)) ?
  432. PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
  433. }
  434. /**
  435. * wx_rx_checksum - indicate in skb if hw indicated a good cksum
  436. * @ring: structure containing ring specific data
  437. * @rx_desc: current Rx descriptor being processed
  438. * @skb: skb currently being received and modified
  439. **/
  440. static void wx_rx_checksum(struct wx_ring *ring,
  441. union wx_rx_desc *rx_desc,
  442. struct sk_buff *skb)
  443. {
  444. struct wx_dec_ptype dptype = wx_decode_ptype(WX_RXD_PKTTYPE(rx_desc));
  445. skb_checksum_none_assert(skb);
  446. /* Rx csum disabled */
  447. if (!(ring->netdev->features & NETIF_F_RXCSUM))
  448. return;
  449. /* if IPv4 header checksum error */
  450. if ((wx_test_staterr(rx_desc, WX_RXD_STAT_IPCS) &&
  451. wx_test_staterr(rx_desc, WX_RXD_ERR_IPE)) ||
  452. (wx_test_staterr(rx_desc, WX_RXD_STAT_OUTERIPCS) &&
  453. wx_test_staterr(rx_desc, WX_RXD_ERR_OUTERIPER))) {
  454. ring->rx_stats.csum_err++;
  455. return;
  456. }
  457. /* L4 checksum offload flag must set for the below code to work */
  458. if (!wx_test_staterr(rx_desc, WX_RXD_STAT_L4CS))
  459. return;
  460. /* Hardware can't guarantee csum if IPv6 Dest Header found */
  461. if (dptype.prot != WX_DEC_PTYPE_PROT_SCTP && WX_RXD_IPV6EX(rx_desc))
  462. return;
  463. /* if L4 checksum error */
  464. if (wx_test_staterr(rx_desc, WX_RXD_ERR_TCPE)) {
  465. ring->rx_stats.csum_err++;
  466. return;
  467. }
  468. /* It must be a TCP or UDP or SCTP packet with a valid checksum */
  469. skb->ip_summed = CHECKSUM_UNNECESSARY;
  470. /* If there is an outer header present that might contain a checksum
  471. * we need to bump the checksum level by 1 to reflect the fact that
  472. * we are indicating we validated the inner checksum.
  473. */
  474. if (dptype.etype >= WX_DEC_PTYPE_ETYPE_IG)
  475. __skb_incr_checksum_unnecessary(skb);
  476. ring->rx_stats.csum_good_cnt++;
  477. }
  478. static void wx_rx_vlan(struct wx_ring *ring, union wx_rx_desc *rx_desc,
  479. struct sk_buff *skb)
  480. {
  481. u16 ethertype;
  482. u8 idx = 0;
  483. if ((ring->netdev->features &
  484. (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) &&
  485. wx_test_staterr(rx_desc, WX_RXD_STAT_VP)) {
  486. idx = (le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info) &
  487. 0x1c0) >> 6;
  488. ethertype = ring->q_vector->wx->tpid[idx];
  489. __vlan_hwaccel_put_tag(skb, htons(ethertype),
  490. le16_to_cpu(rx_desc->wb.upper.vlan));
  491. }
  492. }
  493. /**
  494. * wx_process_skb_fields - Populate skb header fields from Rx descriptor
  495. * @rx_ring: rx descriptor ring packet is being transacted on
  496. * @rx_desc: pointer to the EOP Rx descriptor
  497. * @skb: pointer to current skb being populated
  498. *
  499. * This function checks the ring, descriptor, and packet information in
  500. * order to populate the hash, checksum, protocol, and
  501. * other fields within the skb.
  502. **/
  503. static void wx_process_skb_fields(struct wx_ring *rx_ring,
  504. union wx_rx_desc *rx_desc,
  505. struct sk_buff *skb)
  506. {
  507. wx_rx_hash(rx_ring, rx_desc, skb);
  508. wx_rx_checksum(rx_ring, rx_desc, skb);
  509. wx_rx_vlan(rx_ring, rx_desc, skb);
  510. skb_record_rx_queue(skb, rx_ring->queue_index);
  511. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  512. }
  513. /**
  514. * wx_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  515. * @q_vector: structure containing interrupt and ring information
  516. * @rx_ring: rx descriptor ring to transact packets on
  517. * @budget: Total limit on number of packets to process
  518. *
  519. * This function provides a "bounce buffer" approach to Rx interrupt
  520. * processing. The advantage to this is that on systems that have
  521. * expensive overhead for IOMMU access this provides a means of avoiding
  522. * it by maintaining the mapping of the page to the system.
  523. *
  524. * Returns amount of work completed.
  525. **/
  526. static int wx_clean_rx_irq(struct wx_q_vector *q_vector,
  527. struct wx_ring *rx_ring,
  528. int budget)
  529. {
  530. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  531. u16 cleaned_count = wx_desc_unused(rx_ring);
  532. do {
  533. struct wx_rx_buffer *rx_buffer;
  534. union wx_rx_desc *rx_desc;
  535. struct sk_buff *skb;
  536. int rx_buffer_pgcnt;
  537. /* return some buffers to hardware, one at a time is too slow */
  538. if (cleaned_count >= WX_RX_BUFFER_WRITE) {
  539. wx_alloc_rx_buffers(rx_ring, cleaned_count);
  540. cleaned_count = 0;
  541. }
  542. rx_desc = WX_RX_DESC(rx_ring, rx_ring->next_to_clean);
  543. if (!wx_test_staterr(rx_desc, WX_RXD_STAT_DD))
  544. break;
  545. /* This memory barrier is needed to keep us from reading
  546. * any other fields out of the rx_desc until we know the
  547. * descriptor has been written back
  548. */
  549. dma_rmb();
  550. rx_buffer = wx_get_rx_buffer(rx_ring, rx_desc, &skb, &rx_buffer_pgcnt);
  551. /* retrieve a buffer from the ring */
  552. skb = wx_build_skb(rx_ring, rx_buffer, rx_desc);
  553. /* exit if we failed to retrieve a buffer */
  554. if (!skb) {
  555. rx_ring->rx_stats.alloc_rx_buff_failed++;
  556. break;
  557. }
  558. wx_put_rx_buffer(rx_ring, rx_buffer, skb, rx_buffer_pgcnt);
  559. cleaned_count++;
  560. /* place incomplete frames back on ring for completion */
  561. if (wx_is_non_eop(rx_ring, rx_desc, skb))
  562. continue;
  563. /* verify the packet layout is correct */
  564. if (wx_cleanup_headers(rx_ring, rx_desc, skb))
  565. continue;
  566. /* probably a little skewed due to removing CRC */
  567. total_rx_bytes += skb->len;
  568. /* populate checksum, timestamp, VLAN, and protocol */
  569. wx_process_skb_fields(rx_ring, rx_desc, skb);
  570. napi_gro_receive(&q_vector->napi, skb);
  571. /* update budget accounting */
  572. total_rx_packets++;
  573. } while (likely(total_rx_packets < budget));
  574. u64_stats_update_begin(&rx_ring->syncp);
  575. rx_ring->stats.packets += total_rx_packets;
  576. rx_ring->stats.bytes += total_rx_bytes;
  577. u64_stats_update_end(&rx_ring->syncp);
  578. q_vector->rx.total_packets += total_rx_packets;
  579. q_vector->rx.total_bytes += total_rx_bytes;
  580. return total_rx_packets;
  581. }
  582. static struct netdev_queue *wx_txring_txq(const struct wx_ring *ring)
  583. {
  584. return netdev_get_tx_queue(ring->netdev, ring->queue_index);
  585. }
  586. /**
  587. * wx_clean_tx_irq - Reclaim resources after transmit completes
  588. * @q_vector: structure containing interrupt and ring information
  589. * @tx_ring: tx ring to clean
  590. * @napi_budget: Used to determine if we are in netpoll
  591. **/
  592. static bool wx_clean_tx_irq(struct wx_q_vector *q_vector,
  593. struct wx_ring *tx_ring, int napi_budget)
  594. {
  595. unsigned int budget = q_vector->wx->tx_work_limit;
  596. unsigned int total_bytes = 0, total_packets = 0;
  597. unsigned int i = tx_ring->next_to_clean;
  598. struct wx_tx_buffer *tx_buffer;
  599. union wx_tx_desc *tx_desc;
  600. if (!netif_carrier_ok(tx_ring->netdev))
  601. return true;
  602. tx_buffer = &tx_ring->tx_buffer_info[i];
  603. tx_desc = WX_TX_DESC(tx_ring, i);
  604. i -= tx_ring->count;
  605. do {
  606. union wx_tx_desc *eop_desc = tx_buffer->next_to_watch;
  607. /* if next_to_watch is not set then there is no work pending */
  608. if (!eop_desc)
  609. break;
  610. /* prevent any other reads prior to eop_desc */
  611. smp_rmb();
  612. /* if DD is not set pending work has not been completed */
  613. if (!(eop_desc->wb.status & cpu_to_le32(WX_TXD_STAT_DD)))
  614. break;
  615. /* clear next_to_watch to prevent false hangs */
  616. tx_buffer->next_to_watch = NULL;
  617. /* update the statistics for this packet */
  618. total_bytes += tx_buffer->bytecount;
  619. total_packets += tx_buffer->gso_segs;
  620. /* free the skb */
  621. napi_consume_skb(tx_buffer->skb, napi_budget);
  622. /* unmap skb header data */
  623. dma_unmap_single(tx_ring->dev,
  624. dma_unmap_addr(tx_buffer, dma),
  625. dma_unmap_len(tx_buffer, len),
  626. DMA_TO_DEVICE);
  627. /* clear tx_buffer data */
  628. dma_unmap_len_set(tx_buffer, len, 0);
  629. /* unmap remaining buffers */
  630. while (tx_desc != eop_desc) {
  631. tx_buffer++;
  632. tx_desc++;
  633. i++;
  634. if (unlikely(!i)) {
  635. i -= tx_ring->count;
  636. tx_buffer = tx_ring->tx_buffer_info;
  637. tx_desc = WX_TX_DESC(tx_ring, 0);
  638. }
  639. /* unmap any remaining paged data */
  640. if (dma_unmap_len(tx_buffer, len)) {
  641. dma_unmap_page(tx_ring->dev,
  642. dma_unmap_addr(tx_buffer, dma),
  643. dma_unmap_len(tx_buffer, len),
  644. DMA_TO_DEVICE);
  645. dma_unmap_len_set(tx_buffer, len, 0);
  646. }
  647. }
  648. /* move us one more past the eop_desc for start of next pkt */
  649. tx_buffer++;
  650. tx_desc++;
  651. i++;
  652. if (unlikely(!i)) {
  653. i -= tx_ring->count;
  654. tx_buffer = tx_ring->tx_buffer_info;
  655. tx_desc = WX_TX_DESC(tx_ring, 0);
  656. }
  657. /* issue prefetch for next Tx descriptor */
  658. prefetch(tx_desc);
  659. /* update budget accounting */
  660. budget--;
  661. } while (likely(budget));
  662. i += tx_ring->count;
  663. tx_ring->next_to_clean = i;
  664. u64_stats_update_begin(&tx_ring->syncp);
  665. tx_ring->stats.bytes += total_bytes;
  666. tx_ring->stats.packets += total_packets;
  667. u64_stats_update_end(&tx_ring->syncp);
  668. q_vector->tx.total_bytes += total_bytes;
  669. q_vector->tx.total_packets += total_packets;
  670. netdev_tx_completed_queue(wx_txring_txq(tx_ring),
  671. total_packets, total_bytes);
  672. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  673. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  674. (wx_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
  675. /* Make sure that anybody stopping the queue after this
  676. * sees the new next_to_clean.
  677. */
  678. smp_mb();
  679. if (__netif_subqueue_stopped(tx_ring->netdev,
  680. tx_ring->queue_index) &&
  681. netif_running(tx_ring->netdev)) {
  682. netif_wake_subqueue(tx_ring->netdev,
  683. tx_ring->queue_index);
  684. ++tx_ring->tx_stats.restart_queue;
  685. }
  686. }
  687. return !!budget;
  688. }
  689. /**
  690. * wx_poll - NAPI polling RX/TX cleanup routine
  691. * @napi: napi struct with our devices info in it
  692. * @budget: amount of work driver is allowed to do this pass, in packets
  693. *
  694. * This function will clean all queues associated with a q_vector.
  695. **/
  696. static int wx_poll(struct napi_struct *napi, int budget)
  697. {
  698. struct wx_q_vector *q_vector = container_of(napi, struct wx_q_vector, napi);
  699. int per_ring_budget, work_done = 0;
  700. struct wx *wx = q_vector->wx;
  701. bool clean_complete = true;
  702. struct wx_ring *ring;
  703. wx_for_each_ring(ring, q_vector->tx) {
  704. if (!wx_clean_tx_irq(q_vector, ring, budget))
  705. clean_complete = false;
  706. }
  707. /* Exit if we are called by netpoll */
  708. if (budget <= 0)
  709. return budget;
  710. /* attempt to distribute budget to each queue fairly, but don't allow
  711. * the budget to go below 1 because we'll exit polling
  712. */
  713. if (q_vector->rx.count > 1)
  714. per_ring_budget = max(budget / q_vector->rx.count, 1);
  715. else
  716. per_ring_budget = budget;
  717. wx_for_each_ring(ring, q_vector->rx) {
  718. int cleaned = wx_clean_rx_irq(q_vector, ring, per_ring_budget);
  719. work_done += cleaned;
  720. if (cleaned >= per_ring_budget)
  721. clean_complete = false;
  722. }
  723. /* If all work not completed, return budget and keep polling */
  724. if (!clean_complete)
  725. return budget;
  726. /* all work done, exit the polling mode */
  727. if (likely(napi_complete_done(napi, work_done))) {
  728. if (netif_running(wx->netdev))
  729. wx_intr_enable(wx, WX_INTR_Q(q_vector->v_idx));
  730. }
  731. return min(work_done, budget - 1);
  732. }
  733. static int wx_maybe_stop_tx(struct wx_ring *tx_ring, u16 size)
  734. {
  735. if (likely(wx_desc_unused(tx_ring) >= size))
  736. return 0;
  737. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  738. /* For the next check */
  739. smp_mb();
  740. /* We need to check again in a case another CPU has just
  741. * made room available.
  742. */
  743. if (likely(wx_desc_unused(tx_ring) < size))
  744. return -EBUSY;
  745. /* A reprieve! - use start_queue because it doesn't call schedule */
  746. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  747. ++tx_ring->tx_stats.restart_queue;
  748. return 0;
  749. }
  750. static u32 wx_tx_cmd_type(u32 tx_flags)
  751. {
  752. /* set type for advanced descriptor with frame checksum insertion */
  753. u32 cmd_type = WX_TXD_DTYP_DATA | WX_TXD_IFCS;
  754. /* set HW vlan bit if vlan is present */
  755. cmd_type |= WX_SET_FLAG(tx_flags, WX_TX_FLAGS_HW_VLAN, WX_TXD_VLE);
  756. /* set segmentation enable bits for TSO/FSO */
  757. cmd_type |= WX_SET_FLAG(tx_flags, WX_TX_FLAGS_TSO, WX_TXD_TSE);
  758. /* set timestamp bit if present */
  759. cmd_type |= WX_SET_FLAG(tx_flags, WX_TX_FLAGS_TSTAMP, WX_TXD_MAC_TSTAMP);
  760. cmd_type |= WX_SET_FLAG(tx_flags, WX_TX_FLAGS_LINKSEC, WX_TXD_LINKSEC);
  761. return cmd_type;
  762. }
  763. static void wx_tx_olinfo_status(union wx_tx_desc *tx_desc,
  764. u32 tx_flags, unsigned int paylen)
  765. {
  766. u32 olinfo_status = paylen << WX_TXD_PAYLEN_SHIFT;
  767. /* enable L4 checksum for TSO and TX checksum offload */
  768. olinfo_status |= WX_SET_FLAG(tx_flags, WX_TX_FLAGS_CSUM, WX_TXD_L4CS);
  769. /* enable IPv4 checksum for TSO */
  770. olinfo_status |= WX_SET_FLAG(tx_flags, WX_TX_FLAGS_IPV4, WX_TXD_IIPCS);
  771. /* enable outer IPv4 checksum for TSO */
  772. olinfo_status |= WX_SET_FLAG(tx_flags, WX_TX_FLAGS_OUTER_IPV4,
  773. WX_TXD_EIPCS);
  774. /* Check Context must be set if Tx switch is enabled, which it
  775. * always is for case where virtual functions are running
  776. */
  777. olinfo_status |= WX_SET_FLAG(tx_flags, WX_TX_FLAGS_CC, WX_TXD_CC);
  778. olinfo_status |= WX_SET_FLAG(tx_flags, WX_TX_FLAGS_IPSEC,
  779. WX_TXD_IPSEC);
  780. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  781. }
  782. static void wx_tx_map(struct wx_ring *tx_ring,
  783. struct wx_tx_buffer *first,
  784. const u8 hdr_len)
  785. {
  786. struct sk_buff *skb = first->skb;
  787. struct wx_tx_buffer *tx_buffer;
  788. u32 tx_flags = first->tx_flags;
  789. u16 i = tx_ring->next_to_use;
  790. unsigned int data_len, size;
  791. union wx_tx_desc *tx_desc;
  792. skb_frag_t *frag;
  793. dma_addr_t dma;
  794. u32 cmd_type;
  795. cmd_type = wx_tx_cmd_type(tx_flags);
  796. tx_desc = WX_TX_DESC(tx_ring, i);
  797. wx_tx_olinfo_status(tx_desc, tx_flags, skb->len - hdr_len);
  798. size = skb_headlen(skb);
  799. data_len = skb->data_len;
  800. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  801. tx_buffer = first;
  802. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  803. if (dma_mapping_error(tx_ring->dev, dma))
  804. goto dma_error;
  805. /* record length, and DMA address */
  806. dma_unmap_len_set(tx_buffer, len, size);
  807. dma_unmap_addr_set(tx_buffer, dma, dma);
  808. tx_desc->read.buffer_addr = cpu_to_le64(dma);
  809. while (unlikely(size > WX_MAX_DATA_PER_TXD)) {
  810. tx_desc->read.cmd_type_len =
  811. cpu_to_le32(cmd_type ^ WX_MAX_DATA_PER_TXD);
  812. i++;
  813. tx_desc++;
  814. if (i == tx_ring->count) {
  815. tx_desc = WX_TX_DESC(tx_ring, 0);
  816. i = 0;
  817. }
  818. tx_desc->read.olinfo_status = 0;
  819. dma += WX_MAX_DATA_PER_TXD;
  820. size -= WX_MAX_DATA_PER_TXD;
  821. tx_desc->read.buffer_addr = cpu_to_le64(dma);
  822. }
  823. if (likely(!data_len))
  824. break;
  825. tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
  826. i++;
  827. tx_desc++;
  828. if (i == tx_ring->count) {
  829. tx_desc = WX_TX_DESC(tx_ring, 0);
  830. i = 0;
  831. }
  832. tx_desc->read.olinfo_status = 0;
  833. size = skb_frag_size(frag);
  834. data_len -= size;
  835. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  836. DMA_TO_DEVICE);
  837. tx_buffer = &tx_ring->tx_buffer_info[i];
  838. }
  839. /* write last descriptor with RS and EOP bits */
  840. cmd_type |= size | WX_TXD_EOP | WX_TXD_RS;
  841. tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
  842. netdev_tx_sent_queue(wx_txring_txq(tx_ring), first->bytecount);
  843. skb_tx_timestamp(skb);
  844. /* Force memory writes to complete before letting h/w know there
  845. * are new descriptors to fetch. (Only applicable for weak-ordered
  846. * memory model archs, such as IA-64).
  847. *
  848. * We also need this memory barrier to make certain all of the
  849. * status bits have been updated before next_to_watch is written.
  850. */
  851. wmb();
  852. /* set next_to_watch value indicating a packet is present */
  853. first->next_to_watch = tx_desc;
  854. i++;
  855. if (i == tx_ring->count)
  856. i = 0;
  857. tx_ring->next_to_use = i;
  858. wx_maybe_stop_tx(tx_ring, DESC_NEEDED);
  859. if (netif_xmit_stopped(wx_txring_txq(tx_ring)) || !netdev_xmit_more())
  860. writel(i, tx_ring->tail);
  861. return;
  862. dma_error:
  863. dev_err(tx_ring->dev, "TX DMA map failed\n");
  864. /* clear dma mappings for failed tx_buffer_info map */
  865. for (;;) {
  866. tx_buffer = &tx_ring->tx_buffer_info[i];
  867. if (dma_unmap_len(tx_buffer, len))
  868. dma_unmap_page(tx_ring->dev,
  869. dma_unmap_addr(tx_buffer, dma),
  870. dma_unmap_len(tx_buffer, len),
  871. DMA_TO_DEVICE);
  872. dma_unmap_len_set(tx_buffer, len, 0);
  873. if (tx_buffer == first)
  874. break;
  875. if (i == 0)
  876. i += tx_ring->count;
  877. i--;
  878. }
  879. dev_kfree_skb_any(first->skb);
  880. first->skb = NULL;
  881. tx_ring->next_to_use = i;
  882. }
  883. static void wx_tx_ctxtdesc(struct wx_ring *tx_ring, u32 vlan_macip_lens,
  884. u32 fcoe_sof_eof, u32 type_tucmd, u32 mss_l4len_idx)
  885. {
  886. struct wx_tx_context_desc *context_desc;
  887. u16 i = tx_ring->next_to_use;
  888. context_desc = WX_TX_CTXTDESC(tx_ring, i);
  889. i++;
  890. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  891. /* set bits to identify this as an advanced context descriptor */
  892. type_tucmd |= WX_TXD_DTYP_CTXT;
  893. context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
  894. context_desc->seqnum_seed = cpu_to_le32(fcoe_sof_eof);
  895. context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
  896. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  897. }
  898. static void wx_get_ipv6_proto(struct sk_buff *skb, int offset, u8 *nexthdr)
  899. {
  900. struct ipv6hdr *hdr = (struct ipv6hdr *)(skb->data + offset);
  901. *nexthdr = hdr->nexthdr;
  902. offset += sizeof(struct ipv6hdr);
  903. while (ipv6_ext_hdr(*nexthdr)) {
  904. struct ipv6_opt_hdr _hdr, *hp;
  905. if (*nexthdr == NEXTHDR_NONE)
  906. return;
  907. hp = skb_header_pointer(skb, offset, sizeof(_hdr), &_hdr);
  908. if (!hp)
  909. return;
  910. if (*nexthdr == NEXTHDR_FRAGMENT)
  911. break;
  912. *nexthdr = hp->nexthdr;
  913. }
  914. }
  915. union network_header {
  916. struct iphdr *ipv4;
  917. struct ipv6hdr *ipv6;
  918. void *raw;
  919. };
  920. static u8 wx_encode_tx_desc_ptype(const struct wx_tx_buffer *first)
  921. {
  922. u8 tun_prot = 0, l4_prot = 0, ptype = 0;
  923. struct sk_buff *skb = first->skb;
  924. if (skb->encapsulation) {
  925. union network_header hdr;
  926. switch (first->protocol) {
  927. case htons(ETH_P_IP):
  928. tun_prot = ip_hdr(skb)->protocol;
  929. ptype = WX_PTYPE_TUN_IPV4;
  930. break;
  931. case htons(ETH_P_IPV6):
  932. wx_get_ipv6_proto(skb, skb_network_offset(skb), &tun_prot);
  933. ptype = WX_PTYPE_TUN_IPV6;
  934. break;
  935. default:
  936. return ptype;
  937. }
  938. if (tun_prot == IPPROTO_IPIP) {
  939. hdr.raw = (void *)inner_ip_hdr(skb);
  940. ptype |= WX_PTYPE_PKT_IPIP;
  941. } else if (tun_prot == IPPROTO_UDP) {
  942. hdr.raw = (void *)inner_ip_hdr(skb);
  943. if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  944. skb->inner_protocol != htons(ETH_P_TEB)) {
  945. ptype |= WX_PTYPE_PKT_IG;
  946. } else {
  947. if (((struct ethhdr *)skb_inner_mac_header(skb))->h_proto
  948. == htons(ETH_P_8021Q))
  949. ptype |= WX_PTYPE_PKT_IGMV;
  950. else
  951. ptype |= WX_PTYPE_PKT_IGM;
  952. }
  953. } else if (tun_prot == IPPROTO_GRE) {
  954. hdr.raw = (void *)inner_ip_hdr(skb);
  955. if (skb->inner_protocol == htons(ETH_P_IP) ||
  956. skb->inner_protocol == htons(ETH_P_IPV6)) {
  957. ptype |= WX_PTYPE_PKT_IG;
  958. } else {
  959. if (((struct ethhdr *)skb_inner_mac_header(skb))->h_proto
  960. == htons(ETH_P_8021Q))
  961. ptype |= WX_PTYPE_PKT_IGMV;
  962. else
  963. ptype |= WX_PTYPE_PKT_IGM;
  964. }
  965. } else {
  966. return ptype;
  967. }
  968. switch (hdr.ipv4->version) {
  969. case IPVERSION:
  970. l4_prot = hdr.ipv4->protocol;
  971. break;
  972. case 6:
  973. wx_get_ipv6_proto(skb, skb_inner_network_offset(skb), &l4_prot);
  974. ptype |= WX_PTYPE_PKT_IPV6;
  975. break;
  976. default:
  977. return ptype;
  978. }
  979. } else {
  980. switch (first->protocol) {
  981. case htons(ETH_P_IP):
  982. l4_prot = ip_hdr(skb)->protocol;
  983. ptype = WX_PTYPE_PKT_IP;
  984. break;
  985. case htons(ETH_P_IPV6):
  986. wx_get_ipv6_proto(skb, skb_network_offset(skb), &l4_prot);
  987. ptype = WX_PTYPE_PKT_IP | WX_PTYPE_PKT_IPV6;
  988. break;
  989. default:
  990. return WX_PTYPE_PKT_MAC | WX_PTYPE_TYP_MAC;
  991. }
  992. }
  993. switch (l4_prot) {
  994. case IPPROTO_TCP:
  995. ptype |= WX_PTYPE_TYP_TCP;
  996. break;
  997. case IPPROTO_UDP:
  998. ptype |= WX_PTYPE_TYP_UDP;
  999. break;
  1000. case IPPROTO_SCTP:
  1001. ptype |= WX_PTYPE_TYP_SCTP;
  1002. break;
  1003. default:
  1004. ptype |= WX_PTYPE_TYP_IP;
  1005. break;
  1006. }
  1007. return ptype;
  1008. }
  1009. static int wx_tso(struct wx_ring *tx_ring, struct wx_tx_buffer *first,
  1010. u8 *hdr_len, u8 ptype)
  1011. {
  1012. u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
  1013. struct net_device *netdev = tx_ring->netdev;
  1014. u32 l4len, tunhdr_eiplen_tunlen = 0;
  1015. struct sk_buff *skb = first->skb;
  1016. bool enc = skb->encapsulation;
  1017. struct ipv6hdr *ipv6h;
  1018. struct tcphdr *tcph;
  1019. struct iphdr *iph;
  1020. u8 tun_prot = 0;
  1021. int err;
  1022. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1023. return 0;
  1024. if (!skb_is_gso(skb))
  1025. return 0;
  1026. err = skb_cow_head(skb, 0);
  1027. if (err < 0)
  1028. return err;
  1029. /* indicates the inner headers in the skbuff are valid. */
  1030. iph = enc ? inner_ip_hdr(skb) : ip_hdr(skb);
  1031. if (iph->version == 4) {
  1032. tcph = enc ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1033. iph->tot_len = 0;
  1034. iph->check = 0;
  1035. tcph->check = ~csum_tcpudp_magic(iph->saddr,
  1036. iph->daddr, 0,
  1037. IPPROTO_TCP, 0);
  1038. first->tx_flags |= WX_TX_FLAGS_TSO |
  1039. WX_TX_FLAGS_CSUM |
  1040. WX_TX_FLAGS_IPV4 |
  1041. WX_TX_FLAGS_CC;
  1042. } else if (iph->version == 6 && skb_is_gso_v6(skb)) {
  1043. ipv6h = enc ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
  1044. tcph = enc ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1045. ipv6h->payload_len = 0;
  1046. tcph->check = ~csum_ipv6_magic(&ipv6h->saddr,
  1047. &ipv6h->daddr, 0,
  1048. IPPROTO_TCP, 0);
  1049. first->tx_flags |= WX_TX_FLAGS_TSO |
  1050. WX_TX_FLAGS_CSUM |
  1051. WX_TX_FLAGS_CC;
  1052. }
  1053. /* compute header lengths */
  1054. l4len = enc ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
  1055. *hdr_len = enc ? skb_inner_transport_offset(skb) :
  1056. skb_transport_offset(skb);
  1057. *hdr_len += l4len;
  1058. /* update gso size and bytecount with header size */
  1059. first->gso_segs = skb_shinfo(skb)->gso_segs;
  1060. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  1061. /* mss_l4len_id: use 0 as index for TSO */
  1062. mss_l4len_idx = l4len << WX_TXD_L4LEN_SHIFT;
  1063. mss_l4len_idx |= skb_shinfo(skb)->gso_size << WX_TXD_MSS_SHIFT;
  1064. /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
  1065. if (enc) {
  1066. switch (first->protocol) {
  1067. case htons(ETH_P_IP):
  1068. tun_prot = ip_hdr(skb)->protocol;
  1069. first->tx_flags |= WX_TX_FLAGS_OUTER_IPV4;
  1070. break;
  1071. case htons(ETH_P_IPV6):
  1072. tun_prot = ipv6_hdr(skb)->nexthdr;
  1073. break;
  1074. default:
  1075. break;
  1076. }
  1077. switch (tun_prot) {
  1078. case IPPROTO_UDP:
  1079. tunhdr_eiplen_tunlen = WX_TXD_TUNNEL_UDP;
  1080. tunhdr_eiplen_tunlen |= ((skb_network_header_len(skb) >> 2) <<
  1081. WX_TXD_OUTER_IPLEN_SHIFT) |
  1082. (((skb_inner_mac_header(skb) -
  1083. skb_transport_header(skb)) >> 1) <<
  1084. WX_TXD_TUNNEL_LEN_SHIFT);
  1085. break;
  1086. case IPPROTO_GRE:
  1087. tunhdr_eiplen_tunlen = WX_TXD_TUNNEL_GRE;
  1088. tunhdr_eiplen_tunlen |= ((skb_network_header_len(skb) >> 2) <<
  1089. WX_TXD_OUTER_IPLEN_SHIFT) |
  1090. (((skb_inner_mac_header(skb) -
  1091. skb_transport_header(skb)) >> 1) <<
  1092. WX_TXD_TUNNEL_LEN_SHIFT);
  1093. break;
  1094. case IPPROTO_IPIP:
  1095. tunhdr_eiplen_tunlen = (((char *)inner_ip_hdr(skb) -
  1096. (char *)ip_hdr(skb)) >> 2) <<
  1097. WX_TXD_OUTER_IPLEN_SHIFT;
  1098. break;
  1099. default:
  1100. break;
  1101. }
  1102. vlan_macip_lens = skb_inner_network_header_len(skb) >> 1;
  1103. } else {
  1104. vlan_macip_lens = skb_network_header_len(skb) >> 1;
  1105. }
  1106. vlan_macip_lens |= skb_network_offset(skb) << WX_TXD_MACLEN_SHIFT;
  1107. vlan_macip_lens |= first->tx_flags & WX_TX_FLAGS_VLAN_MASK;
  1108. type_tucmd = ptype << 24;
  1109. if (skb->vlan_proto == htons(ETH_P_8021AD) &&
  1110. netdev->features & NETIF_F_HW_VLAN_STAG_TX)
  1111. type_tucmd |= WX_SET_FLAG(first->tx_flags,
  1112. WX_TX_FLAGS_HW_VLAN,
  1113. 0x1 << WX_TXD_TAG_TPID_SEL_SHIFT);
  1114. wx_tx_ctxtdesc(tx_ring, vlan_macip_lens, tunhdr_eiplen_tunlen,
  1115. type_tucmd, mss_l4len_idx);
  1116. return 1;
  1117. }
  1118. static void wx_tx_csum(struct wx_ring *tx_ring, struct wx_tx_buffer *first,
  1119. u8 ptype)
  1120. {
  1121. u32 tunhdr_eiplen_tunlen = 0, vlan_macip_lens = 0;
  1122. struct net_device *netdev = tx_ring->netdev;
  1123. u32 mss_l4len_idx = 0, type_tucmd;
  1124. struct sk_buff *skb = first->skb;
  1125. u8 tun_prot = 0;
  1126. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  1127. csum_failed:
  1128. if (!(first->tx_flags & WX_TX_FLAGS_HW_VLAN) &&
  1129. !(first->tx_flags & WX_TX_FLAGS_CC))
  1130. return;
  1131. vlan_macip_lens = skb_network_offset(skb) <<
  1132. WX_TXD_MACLEN_SHIFT;
  1133. } else {
  1134. u8 l4_prot = 0;
  1135. union {
  1136. struct iphdr *ipv4;
  1137. struct ipv6hdr *ipv6;
  1138. u8 *raw;
  1139. } network_hdr;
  1140. union {
  1141. struct tcphdr *tcphdr;
  1142. u8 *raw;
  1143. } transport_hdr;
  1144. if (skb->encapsulation) {
  1145. network_hdr.raw = skb_inner_network_header(skb);
  1146. transport_hdr.raw = skb_inner_transport_header(skb);
  1147. vlan_macip_lens = skb_network_offset(skb) <<
  1148. WX_TXD_MACLEN_SHIFT;
  1149. switch (first->protocol) {
  1150. case htons(ETH_P_IP):
  1151. tun_prot = ip_hdr(skb)->protocol;
  1152. break;
  1153. case htons(ETH_P_IPV6):
  1154. tun_prot = ipv6_hdr(skb)->nexthdr;
  1155. break;
  1156. default:
  1157. return;
  1158. }
  1159. switch (tun_prot) {
  1160. case IPPROTO_UDP:
  1161. tunhdr_eiplen_tunlen = WX_TXD_TUNNEL_UDP;
  1162. tunhdr_eiplen_tunlen |=
  1163. ((skb_network_header_len(skb) >> 2) <<
  1164. WX_TXD_OUTER_IPLEN_SHIFT) |
  1165. (((skb_inner_mac_header(skb) -
  1166. skb_transport_header(skb)) >> 1) <<
  1167. WX_TXD_TUNNEL_LEN_SHIFT);
  1168. break;
  1169. case IPPROTO_GRE:
  1170. tunhdr_eiplen_tunlen = WX_TXD_TUNNEL_GRE;
  1171. tunhdr_eiplen_tunlen |= ((skb_network_header_len(skb) >> 2) <<
  1172. WX_TXD_OUTER_IPLEN_SHIFT) |
  1173. (((skb_inner_mac_header(skb) -
  1174. skb_transport_header(skb)) >> 1) <<
  1175. WX_TXD_TUNNEL_LEN_SHIFT);
  1176. break;
  1177. case IPPROTO_IPIP:
  1178. tunhdr_eiplen_tunlen = (((char *)inner_ip_hdr(skb) -
  1179. (char *)ip_hdr(skb)) >> 2) <<
  1180. WX_TXD_OUTER_IPLEN_SHIFT;
  1181. break;
  1182. default:
  1183. break;
  1184. }
  1185. } else {
  1186. network_hdr.raw = skb_network_header(skb);
  1187. transport_hdr.raw = skb_transport_header(skb);
  1188. vlan_macip_lens = skb_network_offset(skb) <<
  1189. WX_TXD_MACLEN_SHIFT;
  1190. }
  1191. switch (network_hdr.ipv4->version) {
  1192. case IPVERSION:
  1193. vlan_macip_lens |= (transport_hdr.raw - network_hdr.raw) >> 1;
  1194. l4_prot = network_hdr.ipv4->protocol;
  1195. break;
  1196. case 6:
  1197. vlan_macip_lens |= (transport_hdr.raw - network_hdr.raw) >> 1;
  1198. l4_prot = network_hdr.ipv6->nexthdr;
  1199. break;
  1200. default:
  1201. break;
  1202. }
  1203. switch (l4_prot) {
  1204. case IPPROTO_TCP:
  1205. mss_l4len_idx = (transport_hdr.tcphdr->doff * 4) <<
  1206. WX_TXD_L4LEN_SHIFT;
  1207. break;
  1208. case IPPROTO_SCTP:
  1209. mss_l4len_idx = sizeof(struct sctphdr) <<
  1210. WX_TXD_L4LEN_SHIFT;
  1211. break;
  1212. case IPPROTO_UDP:
  1213. mss_l4len_idx = sizeof(struct udphdr) <<
  1214. WX_TXD_L4LEN_SHIFT;
  1215. break;
  1216. default:
  1217. skb_checksum_help(skb);
  1218. goto csum_failed;
  1219. }
  1220. /* update TX checksum flag */
  1221. first->tx_flags |= WX_TX_FLAGS_CSUM;
  1222. }
  1223. first->tx_flags |= WX_TX_FLAGS_CC;
  1224. /* vlan_macip_lens: MACLEN, VLAN tag */
  1225. vlan_macip_lens |= first->tx_flags & WX_TX_FLAGS_VLAN_MASK;
  1226. type_tucmd = ptype << 24;
  1227. if (skb->vlan_proto == htons(ETH_P_8021AD) &&
  1228. netdev->features & NETIF_F_HW_VLAN_STAG_TX)
  1229. type_tucmd |= WX_SET_FLAG(first->tx_flags,
  1230. WX_TX_FLAGS_HW_VLAN,
  1231. 0x1 << WX_TXD_TAG_TPID_SEL_SHIFT);
  1232. wx_tx_ctxtdesc(tx_ring, vlan_macip_lens, tunhdr_eiplen_tunlen,
  1233. type_tucmd, mss_l4len_idx);
  1234. }
  1235. static netdev_tx_t wx_xmit_frame_ring(struct sk_buff *skb,
  1236. struct wx_ring *tx_ring)
  1237. {
  1238. struct wx *wx = netdev_priv(tx_ring->netdev);
  1239. u16 count = TXD_USE_COUNT(skb_headlen(skb));
  1240. struct wx_tx_buffer *first;
  1241. u8 hdr_len = 0, ptype;
  1242. unsigned short f;
  1243. u32 tx_flags = 0;
  1244. int tso;
  1245. /* need: 1 descriptor per page * PAGE_SIZE/WX_MAX_DATA_PER_TXD,
  1246. * + 1 desc for skb_headlen/WX_MAX_DATA_PER_TXD,
  1247. * + 2 desc gap to keep tail from touching head,
  1248. * + 1 desc for context descriptor,
  1249. * otherwise try next time
  1250. */
  1251. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  1252. count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->
  1253. frags[f]));
  1254. if (wx_maybe_stop_tx(tx_ring, count + 3)) {
  1255. tx_ring->tx_stats.tx_busy++;
  1256. return NETDEV_TX_BUSY;
  1257. }
  1258. /* record the location of the first descriptor for this packet */
  1259. first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
  1260. first->skb = skb;
  1261. first->bytecount = skb->len;
  1262. first->gso_segs = 1;
  1263. /* if we have a HW VLAN tag being added default to the HW one */
  1264. if (skb_vlan_tag_present(skb)) {
  1265. tx_flags |= skb_vlan_tag_get(skb) << WX_TX_FLAGS_VLAN_SHIFT;
  1266. tx_flags |= WX_TX_FLAGS_HW_VLAN;
  1267. }
  1268. /* record initial flags and protocol */
  1269. first->tx_flags = tx_flags;
  1270. first->protocol = vlan_get_protocol(skb);
  1271. ptype = wx_encode_tx_desc_ptype(first);
  1272. tso = wx_tso(tx_ring, first, &hdr_len, ptype);
  1273. if (tso < 0)
  1274. goto out_drop;
  1275. else if (!tso)
  1276. wx_tx_csum(tx_ring, first, ptype);
  1277. if (test_bit(WX_FLAG_FDIR_CAPABLE, wx->flags) && tx_ring->atr_sample_rate)
  1278. wx->atr(tx_ring, first, ptype);
  1279. wx_tx_map(tx_ring, first, hdr_len);
  1280. return NETDEV_TX_OK;
  1281. out_drop:
  1282. dev_kfree_skb_any(first->skb);
  1283. first->skb = NULL;
  1284. return NETDEV_TX_OK;
  1285. }
  1286. netdev_tx_t wx_xmit_frame(struct sk_buff *skb,
  1287. struct net_device *netdev)
  1288. {
  1289. unsigned int r_idx = skb->queue_mapping;
  1290. struct wx *wx = netdev_priv(netdev);
  1291. struct wx_ring *tx_ring;
  1292. if (!netif_carrier_ok(netdev)) {
  1293. dev_kfree_skb_any(skb);
  1294. return NETDEV_TX_OK;
  1295. }
  1296. /* The minimum packet size for olinfo paylen is 17 so pad the skb
  1297. * in order to meet this minimum size requirement.
  1298. */
  1299. if (skb_put_padto(skb, 17))
  1300. return NETDEV_TX_OK;
  1301. if (r_idx >= wx->num_tx_queues)
  1302. r_idx = r_idx % wx->num_tx_queues;
  1303. tx_ring = wx->tx_ring[r_idx];
  1304. return wx_xmit_frame_ring(skb, tx_ring);
  1305. }
  1306. EXPORT_SYMBOL(wx_xmit_frame);
  1307. void wx_napi_enable_all(struct wx *wx)
  1308. {
  1309. struct wx_q_vector *q_vector;
  1310. int q_idx;
  1311. for (q_idx = 0; q_idx < wx->num_q_vectors; q_idx++) {
  1312. q_vector = wx->q_vector[q_idx];
  1313. napi_enable(&q_vector->napi);
  1314. }
  1315. }
  1316. EXPORT_SYMBOL(wx_napi_enable_all);
  1317. void wx_napi_disable_all(struct wx *wx)
  1318. {
  1319. struct wx_q_vector *q_vector;
  1320. int q_idx;
  1321. for (q_idx = 0; q_idx < wx->num_q_vectors; q_idx++) {
  1322. q_vector = wx->q_vector[q_idx];
  1323. napi_disable(&q_vector->napi);
  1324. }
  1325. }
  1326. EXPORT_SYMBOL(wx_napi_disable_all);
  1327. /**
  1328. * wx_set_rss_queues: Allocate queues for RSS
  1329. * @wx: board private structure to initialize
  1330. *
  1331. * This is our "base" multiqueue mode. RSS (Receive Side Scaling) will try
  1332. * to allocate one Rx queue per CPU, and if available, one Tx queue per CPU.
  1333. *
  1334. **/
  1335. static void wx_set_rss_queues(struct wx *wx)
  1336. {
  1337. struct wx_ring_feature *f;
  1338. /* set mask for 16 queue limit of RSS */
  1339. f = &wx->ring_feature[RING_F_RSS];
  1340. f->indices = f->limit;
  1341. if (!(test_bit(WX_FLAG_FDIR_CAPABLE, wx->flags)))
  1342. goto out;
  1343. clear_bit(WX_FLAG_FDIR_HASH, wx->flags);
  1344. wx->ring_feature[RING_F_FDIR].indices = 1;
  1345. /* Use Flow Director in addition to RSS to ensure the best
  1346. * distribution of flows across cores, even when an FDIR flow
  1347. * isn't matched.
  1348. */
  1349. if (f->indices > 1) {
  1350. f = &wx->ring_feature[RING_F_FDIR];
  1351. f->indices = f->limit;
  1352. if (!(test_bit(WX_FLAG_FDIR_PERFECT, wx->flags)))
  1353. set_bit(WX_FLAG_FDIR_HASH, wx->flags);
  1354. }
  1355. out:
  1356. wx->num_rx_queues = f->indices;
  1357. wx->num_tx_queues = f->indices;
  1358. }
  1359. static void wx_set_num_queues(struct wx *wx)
  1360. {
  1361. /* Start with base case */
  1362. wx->num_rx_queues = 1;
  1363. wx->num_tx_queues = 1;
  1364. wx->queues_per_pool = 1;
  1365. wx_set_rss_queues(wx);
  1366. }
  1367. /**
  1368. * wx_acquire_msix_vectors - acquire MSI-X vectors
  1369. * @wx: board private structure
  1370. *
  1371. * Attempts to acquire a suitable range of MSI-X vector interrupts. Will
  1372. * return a negative error code if unable to acquire MSI-X vectors for any
  1373. * reason.
  1374. */
  1375. static int wx_acquire_msix_vectors(struct wx *wx)
  1376. {
  1377. struct irq_affinity affd = { .post_vectors = 1 };
  1378. int nvecs, i;
  1379. /* We start by asking for one vector per queue pair */
  1380. nvecs = max(wx->num_rx_queues, wx->num_tx_queues);
  1381. nvecs = min_t(int, nvecs, num_online_cpus());
  1382. nvecs = min_t(int, nvecs, wx->mac.max_msix_vectors);
  1383. wx->msix_q_entries = kcalloc(nvecs, sizeof(struct msix_entry),
  1384. GFP_KERNEL);
  1385. if (!wx->msix_q_entries)
  1386. return -ENOMEM;
  1387. /* One for non-queue interrupts */
  1388. nvecs += 1;
  1389. wx->msix_entry = kcalloc(1, sizeof(struct msix_entry),
  1390. GFP_KERNEL);
  1391. if (!wx->msix_entry) {
  1392. kfree(wx->msix_q_entries);
  1393. wx->msix_q_entries = NULL;
  1394. return -ENOMEM;
  1395. }
  1396. nvecs = pci_alloc_irq_vectors_affinity(wx->pdev, nvecs,
  1397. nvecs,
  1398. PCI_IRQ_MSIX | PCI_IRQ_AFFINITY,
  1399. &affd);
  1400. if (nvecs < 0) {
  1401. wx_err(wx, "Failed to allocate MSI-X interrupts. Err: %d\n", nvecs);
  1402. kfree(wx->msix_q_entries);
  1403. wx->msix_q_entries = NULL;
  1404. kfree(wx->msix_entry);
  1405. wx->msix_entry = NULL;
  1406. return nvecs;
  1407. }
  1408. nvecs -= 1;
  1409. for (i = 0; i < nvecs; i++) {
  1410. wx->msix_q_entries[i].entry = i;
  1411. wx->msix_q_entries[i].vector = pci_irq_vector(wx->pdev, i);
  1412. }
  1413. wx->num_q_vectors = nvecs;
  1414. wx->msix_entry->entry = nvecs;
  1415. wx->msix_entry->vector = pci_irq_vector(wx->pdev, nvecs);
  1416. return 0;
  1417. }
  1418. /**
  1419. * wx_set_interrupt_capability - set MSI-X or MSI if supported
  1420. * @wx: board private structure to initialize
  1421. *
  1422. * Attempt to configure the interrupts using the best available
  1423. * capabilities of the hardware and the kernel.
  1424. **/
  1425. static int wx_set_interrupt_capability(struct wx *wx)
  1426. {
  1427. struct pci_dev *pdev = wx->pdev;
  1428. int nvecs, ret;
  1429. /* We will try to get MSI-X interrupts first */
  1430. ret = wx_acquire_msix_vectors(wx);
  1431. if (ret == 0 || (ret == -ENOMEM))
  1432. return ret;
  1433. /* Disable RSS */
  1434. dev_warn(&wx->pdev->dev, "Disabling RSS support\n");
  1435. wx->ring_feature[RING_F_RSS].limit = 1;
  1436. wx_set_num_queues(wx);
  1437. /* minmum one for queue, one for misc*/
  1438. nvecs = 1;
  1439. nvecs = pci_alloc_irq_vectors(pdev, nvecs,
  1440. nvecs, PCI_IRQ_MSI | PCI_IRQ_INTX);
  1441. if (nvecs == 1) {
  1442. if (pdev->msi_enabled)
  1443. wx_err(wx, "Fallback to MSI.\n");
  1444. else
  1445. wx_err(wx, "Fallback to INTx.\n");
  1446. } else {
  1447. wx_err(wx, "Failed to allocate MSI/INTx interrupts. Error: %d\n", nvecs);
  1448. return nvecs;
  1449. }
  1450. pdev->irq = pci_irq_vector(pdev, 0);
  1451. wx->num_q_vectors = 1;
  1452. return 0;
  1453. }
  1454. /**
  1455. * wx_cache_ring_rss - Descriptor ring to register mapping for RSS
  1456. * @wx: board private structure to initialize
  1457. *
  1458. * Cache the descriptor ring offsets for RSS, ATR, FCoE, and SR-IOV.
  1459. *
  1460. **/
  1461. static void wx_cache_ring_rss(struct wx *wx)
  1462. {
  1463. u16 i;
  1464. for (i = 0; i < wx->num_rx_queues; i++)
  1465. wx->rx_ring[i]->reg_idx = i;
  1466. for (i = 0; i < wx->num_tx_queues; i++)
  1467. wx->tx_ring[i]->reg_idx = i;
  1468. }
  1469. static void wx_add_ring(struct wx_ring *ring, struct wx_ring_container *head)
  1470. {
  1471. ring->next = head->ring;
  1472. head->ring = ring;
  1473. head->count++;
  1474. }
  1475. /**
  1476. * wx_alloc_q_vector - Allocate memory for a single interrupt vector
  1477. * @wx: board private structure to initialize
  1478. * @v_count: q_vectors allocated on wx, used for ring interleaving
  1479. * @v_idx: index of vector in wx struct
  1480. * @txr_count: total number of Tx rings to allocate
  1481. * @txr_idx: index of first Tx ring to allocate
  1482. * @rxr_count: total number of Rx rings to allocate
  1483. * @rxr_idx: index of first Rx ring to allocate
  1484. *
  1485. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  1486. **/
  1487. static int wx_alloc_q_vector(struct wx *wx,
  1488. unsigned int v_count, unsigned int v_idx,
  1489. unsigned int txr_count, unsigned int txr_idx,
  1490. unsigned int rxr_count, unsigned int rxr_idx)
  1491. {
  1492. struct wx_q_vector *q_vector;
  1493. int ring_count, default_itr;
  1494. struct wx_ring *ring;
  1495. /* note this will allocate space for the ring structure as well! */
  1496. ring_count = txr_count + rxr_count;
  1497. q_vector = kzalloc(struct_size(q_vector, ring, ring_count),
  1498. GFP_KERNEL);
  1499. if (!q_vector)
  1500. return -ENOMEM;
  1501. /* initialize NAPI */
  1502. netif_napi_add(wx->netdev, &q_vector->napi,
  1503. wx_poll);
  1504. /* tie q_vector and wx together */
  1505. wx->q_vector[v_idx] = q_vector;
  1506. q_vector->wx = wx;
  1507. q_vector->v_idx = v_idx;
  1508. if (cpu_online(v_idx))
  1509. q_vector->numa_node = cpu_to_node(v_idx);
  1510. /* initialize pointer to rings */
  1511. ring = q_vector->ring;
  1512. if (wx->mac.type == wx_mac_sp)
  1513. default_itr = WX_12K_ITR;
  1514. else
  1515. default_itr = WX_7K_ITR;
  1516. /* initialize ITR */
  1517. if (txr_count && !rxr_count)
  1518. /* tx only vector */
  1519. q_vector->itr = wx->tx_itr_setting ?
  1520. default_itr : wx->tx_itr_setting;
  1521. else
  1522. /* rx or rx/tx vector */
  1523. q_vector->itr = wx->rx_itr_setting ?
  1524. default_itr : wx->rx_itr_setting;
  1525. while (txr_count) {
  1526. /* assign generic ring traits */
  1527. ring->dev = &wx->pdev->dev;
  1528. ring->netdev = wx->netdev;
  1529. /* configure backlink on ring */
  1530. ring->q_vector = q_vector;
  1531. /* update q_vector Tx values */
  1532. wx_add_ring(ring, &q_vector->tx);
  1533. /* apply Tx specific ring traits */
  1534. ring->count = wx->tx_ring_count;
  1535. ring->queue_index = txr_idx;
  1536. /* assign ring to wx */
  1537. wx->tx_ring[txr_idx] = ring;
  1538. /* update count and index */
  1539. txr_count--;
  1540. txr_idx += v_count;
  1541. /* push pointer to next ring */
  1542. ring++;
  1543. }
  1544. while (rxr_count) {
  1545. /* assign generic ring traits */
  1546. ring->dev = &wx->pdev->dev;
  1547. ring->netdev = wx->netdev;
  1548. /* configure backlink on ring */
  1549. ring->q_vector = q_vector;
  1550. /* update q_vector Rx values */
  1551. wx_add_ring(ring, &q_vector->rx);
  1552. /* apply Rx specific ring traits */
  1553. ring->count = wx->rx_ring_count;
  1554. ring->queue_index = rxr_idx;
  1555. /* assign ring to wx */
  1556. wx->rx_ring[rxr_idx] = ring;
  1557. /* update count and index */
  1558. rxr_count--;
  1559. rxr_idx += v_count;
  1560. /* push pointer to next ring */
  1561. ring++;
  1562. }
  1563. return 0;
  1564. }
  1565. /**
  1566. * wx_free_q_vector - Free memory allocated for specific interrupt vector
  1567. * @wx: board private structure to initialize
  1568. * @v_idx: Index of vector to be freed
  1569. *
  1570. * This function frees the memory allocated to the q_vector. In addition if
  1571. * NAPI is enabled it will delete any references to the NAPI struct prior
  1572. * to freeing the q_vector.
  1573. **/
  1574. static void wx_free_q_vector(struct wx *wx, int v_idx)
  1575. {
  1576. struct wx_q_vector *q_vector = wx->q_vector[v_idx];
  1577. struct wx_ring *ring;
  1578. wx_for_each_ring(ring, q_vector->tx)
  1579. wx->tx_ring[ring->queue_index] = NULL;
  1580. wx_for_each_ring(ring, q_vector->rx)
  1581. wx->rx_ring[ring->queue_index] = NULL;
  1582. wx->q_vector[v_idx] = NULL;
  1583. netif_napi_del(&q_vector->napi);
  1584. kfree_rcu(q_vector, rcu);
  1585. }
  1586. /**
  1587. * wx_alloc_q_vectors - Allocate memory for interrupt vectors
  1588. * @wx: board private structure to initialize
  1589. *
  1590. * We allocate one q_vector per queue interrupt. If allocation fails we
  1591. * return -ENOMEM.
  1592. **/
  1593. static int wx_alloc_q_vectors(struct wx *wx)
  1594. {
  1595. unsigned int rxr_idx = 0, txr_idx = 0, v_idx = 0;
  1596. unsigned int rxr_remaining = wx->num_rx_queues;
  1597. unsigned int txr_remaining = wx->num_tx_queues;
  1598. unsigned int q_vectors = wx->num_q_vectors;
  1599. int rqpv, tqpv;
  1600. int err;
  1601. for (; v_idx < q_vectors; v_idx++) {
  1602. rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
  1603. tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
  1604. err = wx_alloc_q_vector(wx, q_vectors, v_idx,
  1605. tqpv, txr_idx,
  1606. rqpv, rxr_idx);
  1607. if (err)
  1608. goto err_out;
  1609. /* update counts and index */
  1610. rxr_remaining -= rqpv;
  1611. txr_remaining -= tqpv;
  1612. rxr_idx++;
  1613. txr_idx++;
  1614. }
  1615. return 0;
  1616. err_out:
  1617. wx->num_tx_queues = 0;
  1618. wx->num_rx_queues = 0;
  1619. wx->num_q_vectors = 0;
  1620. while (v_idx--)
  1621. wx_free_q_vector(wx, v_idx);
  1622. return -ENOMEM;
  1623. }
  1624. /**
  1625. * wx_free_q_vectors - Free memory allocated for interrupt vectors
  1626. * @wx: board private structure to initialize
  1627. *
  1628. * This function frees the memory allocated to the q_vectors. In addition if
  1629. * NAPI is enabled it will delete any references to the NAPI struct prior
  1630. * to freeing the q_vector.
  1631. **/
  1632. static void wx_free_q_vectors(struct wx *wx)
  1633. {
  1634. int v_idx = wx->num_q_vectors;
  1635. wx->num_tx_queues = 0;
  1636. wx->num_rx_queues = 0;
  1637. wx->num_q_vectors = 0;
  1638. while (v_idx--)
  1639. wx_free_q_vector(wx, v_idx);
  1640. }
  1641. void wx_reset_interrupt_capability(struct wx *wx)
  1642. {
  1643. struct pci_dev *pdev = wx->pdev;
  1644. if (!pdev->msi_enabled && !pdev->msix_enabled)
  1645. return;
  1646. if (pdev->msix_enabled) {
  1647. kfree(wx->msix_q_entries);
  1648. wx->msix_q_entries = NULL;
  1649. kfree(wx->msix_entry);
  1650. wx->msix_entry = NULL;
  1651. }
  1652. pci_free_irq_vectors(wx->pdev);
  1653. }
  1654. EXPORT_SYMBOL(wx_reset_interrupt_capability);
  1655. /**
  1656. * wx_clear_interrupt_scheme - Clear the current interrupt scheme settings
  1657. * @wx: board private structure to clear interrupt scheme on
  1658. *
  1659. * We go through and clear interrupt specific resources and reset the structure
  1660. * to pre-load conditions
  1661. **/
  1662. void wx_clear_interrupt_scheme(struct wx *wx)
  1663. {
  1664. wx_free_q_vectors(wx);
  1665. wx_reset_interrupt_capability(wx);
  1666. }
  1667. EXPORT_SYMBOL(wx_clear_interrupt_scheme);
  1668. int wx_init_interrupt_scheme(struct wx *wx)
  1669. {
  1670. int ret;
  1671. /* Number of supported queues */
  1672. wx_set_num_queues(wx);
  1673. /* Set interrupt mode */
  1674. ret = wx_set_interrupt_capability(wx);
  1675. if (ret) {
  1676. wx_err(wx, "Allocate irq vectors for failed.\n");
  1677. return ret;
  1678. }
  1679. /* Allocate memory for queues */
  1680. ret = wx_alloc_q_vectors(wx);
  1681. if (ret) {
  1682. wx_err(wx, "Unable to allocate memory for queue vectors.\n");
  1683. wx_reset_interrupt_capability(wx);
  1684. return ret;
  1685. }
  1686. wx_cache_ring_rss(wx);
  1687. return 0;
  1688. }
  1689. EXPORT_SYMBOL(wx_init_interrupt_scheme);
  1690. irqreturn_t wx_msix_clean_rings(int __always_unused irq, void *data)
  1691. {
  1692. struct wx_q_vector *q_vector = data;
  1693. /* EIAM disabled interrupts (on this vector) for us */
  1694. if (q_vector->rx.ring || q_vector->tx.ring)
  1695. napi_schedule_irqoff(&q_vector->napi);
  1696. return IRQ_HANDLED;
  1697. }
  1698. EXPORT_SYMBOL(wx_msix_clean_rings);
  1699. void wx_free_irq(struct wx *wx)
  1700. {
  1701. struct pci_dev *pdev = wx->pdev;
  1702. int vector;
  1703. if (!(pdev->msix_enabled)) {
  1704. if (!wx->misc_irq_domain)
  1705. free_irq(pdev->irq, wx);
  1706. return;
  1707. }
  1708. for (vector = 0; vector < wx->num_q_vectors; vector++) {
  1709. struct wx_q_vector *q_vector = wx->q_vector[vector];
  1710. struct msix_entry *entry = &wx->msix_q_entries[vector];
  1711. /* free only the irqs that were actually requested */
  1712. if (!q_vector->rx.ring && !q_vector->tx.ring)
  1713. continue;
  1714. free_irq(entry->vector, q_vector);
  1715. }
  1716. if (!wx->misc_irq_domain)
  1717. free_irq(wx->msix_entry->vector, wx);
  1718. }
  1719. EXPORT_SYMBOL(wx_free_irq);
  1720. /**
  1721. * wx_setup_isb_resources - allocate interrupt status resources
  1722. * @wx: board private structure
  1723. *
  1724. * Return 0 on success, negative on failure
  1725. **/
  1726. int wx_setup_isb_resources(struct wx *wx)
  1727. {
  1728. struct pci_dev *pdev = wx->pdev;
  1729. if (wx->isb_mem)
  1730. return 0;
  1731. wx->isb_mem = dma_alloc_coherent(&pdev->dev,
  1732. sizeof(u32) * 4,
  1733. &wx->isb_dma,
  1734. GFP_KERNEL);
  1735. if (!wx->isb_mem) {
  1736. wx_err(wx, "Alloc isb_mem failed\n");
  1737. return -ENOMEM;
  1738. }
  1739. return 0;
  1740. }
  1741. EXPORT_SYMBOL(wx_setup_isb_resources);
  1742. /**
  1743. * wx_free_isb_resources - allocate all queues Rx resources
  1744. * @wx: board private structure
  1745. *
  1746. * Return 0 on success, negative on failure
  1747. **/
  1748. void wx_free_isb_resources(struct wx *wx)
  1749. {
  1750. struct pci_dev *pdev = wx->pdev;
  1751. dma_free_coherent(&pdev->dev, sizeof(u32) * 4,
  1752. wx->isb_mem, wx->isb_dma);
  1753. wx->isb_mem = NULL;
  1754. }
  1755. EXPORT_SYMBOL(wx_free_isb_resources);
  1756. u32 wx_misc_isb(struct wx *wx, enum wx_isb_idx idx)
  1757. {
  1758. u32 cur_tag = 0;
  1759. cur_tag = wx->isb_mem[WX_ISB_HEADER];
  1760. wx->isb_tag[idx] = cur_tag;
  1761. return (__force u32)cpu_to_le32(wx->isb_mem[idx]);
  1762. }
  1763. EXPORT_SYMBOL(wx_misc_isb);
  1764. /**
  1765. * wx_set_ivar - set the IVAR registers, mapping interrupt causes to vectors
  1766. * @wx: pointer to wx struct
  1767. * @direction: 0 for Rx, 1 for Tx, -1 for other causes
  1768. * @queue: queue to map the corresponding interrupt to
  1769. * @msix_vector: the vector to map to the corresponding queue
  1770. *
  1771. **/
  1772. static void wx_set_ivar(struct wx *wx, s8 direction,
  1773. u16 queue, u16 msix_vector)
  1774. {
  1775. u32 ivar, index;
  1776. if (direction == -1) {
  1777. /* other causes */
  1778. msix_vector |= WX_PX_IVAR_ALLOC_VAL;
  1779. index = 0;
  1780. ivar = rd32(wx, WX_PX_MISC_IVAR);
  1781. ivar &= ~(0xFF << index);
  1782. ivar |= (msix_vector << index);
  1783. wr32(wx, WX_PX_MISC_IVAR, ivar);
  1784. } else {
  1785. /* tx or rx causes */
  1786. msix_vector |= WX_PX_IVAR_ALLOC_VAL;
  1787. index = ((16 * (queue & 1)) + (8 * direction));
  1788. ivar = rd32(wx, WX_PX_IVAR(queue >> 1));
  1789. ivar &= ~(0xFF << index);
  1790. ivar |= (msix_vector << index);
  1791. wr32(wx, WX_PX_IVAR(queue >> 1), ivar);
  1792. }
  1793. }
  1794. /**
  1795. * wx_write_eitr - write EITR register in hardware specific way
  1796. * @q_vector: structure containing interrupt and ring information
  1797. *
  1798. * This function is made to be called by ethtool and by the driver
  1799. * when it needs to update EITR registers at runtime. Hardware
  1800. * specific quirks/differences are taken care of here.
  1801. */
  1802. void wx_write_eitr(struct wx_q_vector *q_vector)
  1803. {
  1804. struct wx *wx = q_vector->wx;
  1805. int v_idx = q_vector->v_idx;
  1806. u32 itr_reg;
  1807. if (wx->mac.type == wx_mac_sp)
  1808. itr_reg = q_vector->itr & WX_SP_MAX_EITR;
  1809. else
  1810. itr_reg = q_vector->itr & WX_EM_MAX_EITR;
  1811. itr_reg |= WX_PX_ITR_CNT_WDIS;
  1812. wr32(wx, WX_PX_ITR(v_idx), itr_reg);
  1813. }
  1814. /**
  1815. * wx_configure_vectors - Configure vectors for hardware
  1816. * @wx: board private structure
  1817. *
  1818. * wx_configure_vectors sets up the hardware to properly generate MSI-X/MSI/INTx
  1819. * interrupts.
  1820. **/
  1821. void wx_configure_vectors(struct wx *wx)
  1822. {
  1823. struct pci_dev *pdev = wx->pdev;
  1824. u32 eitrsel = 0;
  1825. u16 v_idx;
  1826. if (pdev->msix_enabled) {
  1827. /* Populate MSIX to EITR Select */
  1828. wr32(wx, WX_PX_ITRSEL, eitrsel);
  1829. /* use EIAM to auto-mask when MSI-X interrupt is asserted
  1830. * this saves a register write for every interrupt
  1831. */
  1832. wr32(wx, WX_PX_GPIE, WX_PX_GPIE_MODEL);
  1833. } else {
  1834. /* legacy interrupts, use EIAM to auto-mask when reading EICR,
  1835. * specifically only auto mask tx and rx interrupts.
  1836. */
  1837. wr32(wx, WX_PX_GPIE, 0);
  1838. }
  1839. /* Populate the IVAR table and set the ITR values to the
  1840. * corresponding register.
  1841. */
  1842. for (v_idx = 0; v_idx < wx->num_q_vectors; v_idx++) {
  1843. struct wx_q_vector *q_vector = wx->q_vector[v_idx];
  1844. struct wx_ring *ring;
  1845. wx_for_each_ring(ring, q_vector->rx)
  1846. wx_set_ivar(wx, 0, ring->reg_idx, v_idx);
  1847. wx_for_each_ring(ring, q_vector->tx)
  1848. wx_set_ivar(wx, 1, ring->reg_idx, v_idx);
  1849. wx_write_eitr(q_vector);
  1850. }
  1851. wx_set_ivar(wx, -1, 0, v_idx);
  1852. if (pdev->msix_enabled)
  1853. wr32(wx, WX_PX_ITR(v_idx), 1950);
  1854. }
  1855. EXPORT_SYMBOL(wx_configure_vectors);
  1856. /**
  1857. * wx_clean_rx_ring - Free Rx Buffers per Queue
  1858. * @rx_ring: ring to free buffers from
  1859. **/
  1860. static void wx_clean_rx_ring(struct wx_ring *rx_ring)
  1861. {
  1862. struct wx_rx_buffer *rx_buffer;
  1863. u16 i = rx_ring->next_to_clean;
  1864. rx_buffer = &rx_ring->rx_buffer_info[i];
  1865. /* Free all the Rx ring sk_buffs */
  1866. while (i != rx_ring->next_to_alloc) {
  1867. if (rx_buffer->skb) {
  1868. struct sk_buff *skb = rx_buffer->skb;
  1869. dev_kfree_skb(skb);
  1870. }
  1871. /* Invalidate cache lines that may have been written to by
  1872. * device so that we avoid corrupting memory.
  1873. */
  1874. dma_sync_single_range_for_cpu(rx_ring->dev,
  1875. rx_buffer->dma,
  1876. rx_buffer->page_offset,
  1877. WX_RX_BUFSZ,
  1878. DMA_FROM_DEVICE);
  1879. /* free resources associated with mapping */
  1880. page_pool_put_full_page(rx_ring->page_pool, rx_buffer->page, false);
  1881. i++;
  1882. rx_buffer++;
  1883. if (i == rx_ring->count) {
  1884. i = 0;
  1885. rx_buffer = rx_ring->rx_buffer_info;
  1886. }
  1887. }
  1888. /* Zero out the descriptor ring */
  1889. memset(rx_ring->desc, 0, rx_ring->size);
  1890. rx_ring->next_to_alloc = 0;
  1891. rx_ring->next_to_clean = 0;
  1892. rx_ring->next_to_use = 0;
  1893. }
  1894. /**
  1895. * wx_clean_all_rx_rings - Free Rx Buffers for all queues
  1896. * @wx: board private structure
  1897. **/
  1898. void wx_clean_all_rx_rings(struct wx *wx)
  1899. {
  1900. int i;
  1901. for (i = 0; i < wx->num_rx_queues; i++)
  1902. wx_clean_rx_ring(wx->rx_ring[i]);
  1903. }
  1904. EXPORT_SYMBOL(wx_clean_all_rx_rings);
  1905. /**
  1906. * wx_free_rx_resources - Free Rx Resources
  1907. * @rx_ring: ring to clean the resources from
  1908. *
  1909. * Free all receive software resources
  1910. **/
  1911. static void wx_free_rx_resources(struct wx_ring *rx_ring)
  1912. {
  1913. wx_clean_rx_ring(rx_ring);
  1914. kvfree(rx_ring->rx_buffer_info);
  1915. rx_ring->rx_buffer_info = NULL;
  1916. /* if not set, then don't free */
  1917. if (!rx_ring->desc)
  1918. return;
  1919. dma_free_coherent(rx_ring->dev, rx_ring->size,
  1920. rx_ring->desc, rx_ring->dma);
  1921. rx_ring->desc = NULL;
  1922. if (rx_ring->page_pool) {
  1923. page_pool_destroy(rx_ring->page_pool);
  1924. rx_ring->page_pool = NULL;
  1925. }
  1926. }
  1927. /**
  1928. * wx_free_all_rx_resources - Free Rx Resources for All Queues
  1929. * @wx: pointer to hardware structure
  1930. *
  1931. * Free all receive software resources
  1932. **/
  1933. static void wx_free_all_rx_resources(struct wx *wx)
  1934. {
  1935. int i;
  1936. for (i = 0; i < wx->num_rx_queues; i++)
  1937. wx_free_rx_resources(wx->rx_ring[i]);
  1938. }
  1939. /**
  1940. * wx_clean_tx_ring - Free Tx Buffers
  1941. * @tx_ring: ring to be cleaned
  1942. **/
  1943. static void wx_clean_tx_ring(struct wx_ring *tx_ring)
  1944. {
  1945. struct wx_tx_buffer *tx_buffer;
  1946. u16 i = tx_ring->next_to_clean;
  1947. tx_buffer = &tx_ring->tx_buffer_info[i];
  1948. while (i != tx_ring->next_to_use) {
  1949. union wx_tx_desc *eop_desc, *tx_desc;
  1950. /* Free all the Tx ring sk_buffs */
  1951. dev_kfree_skb_any(tx_buffer->skb);
  1952. /* unmap skb header data */
  1953. dma_unmap_single(tx_ring->dev,
  1954. dma_unmap_addr(tx_buffer, dma),
  1955. dma_unmap_len(tx_buffer, len),
  1956. DMA_TO_DEVICE);
  1957. /* check for eop_desc to determine the end of the packet */
  1958. eop_desc = tx_buffer->next_to_watch;
  1959. tx_desc = WX_TX_DESC(tx_ring, i);
  1960. /* unmap remaining buffers */
  1961. while (tx_desc != eop_desc) {
  1962. tx_buffer++;
  1963. tx_desc++;
  1964. i++;
  1965. if (unlikely(i == tx_ring->count)) {
  1966. i = 0;
  1967. tx_buffer = tx_ring->tx_buffer_info;
  1968. tx_desc = WX_TX_DESC(tx_ring, 0);
  1969. }
  1970. /* unmap any remaining paged data */
  1971. if (dma_unmap_len(tx_buffer, len))
  1972. dma_unmap_page(tx_ring->dev,
  1973. dma_unmap_addr(tx_buffer, dma),
  1974. dma_unmap_len(tx_buffer, len),
  1975. DMA_TO_DEVICE);
  1976. }
  1977. /* move us one more past the eop_desc for start of next pkt */
  1978. tx_buffer++;
  1979. i++;
  1980. if (unlikely(i == tx_ring->count)) {
  1981. i = 0;
  1982. tx_buffer = tx_ring->tx_buffer_info;
  1983. }
  1984. }
  1985. netdev_tx_reset_queue(wx_txring_txq(tx_ring));
  1986. /* reset next_to_use and next_to_clean */
  1987. tx_ring->next_to_use = 0;
  1988. tx_ring->next_to_clean = 0;
  1989. }
  1990. /**
  1991. * wx_clean_all_tx_rings - Free Tx Buffers for all queues
  1992. * @wx: board private structure
  1993. **/
  1994. void wx_clean_all_tx_rings(struct wx *wx)
  1995. {
  1996. int i;
  1997. for (i = 0; i < wx->num_tx_queues; i++)
  1998. wx_clean_tx_ring(wx->tx_ring[i]);
  1999. }
  2000. EXPORT_SYMBOL(wx_clean_all_tx_rings);
  2001. /**
  2002. * wx_free_tx_resources - Free Tx Resources per Queue
  2003. * @tx_ring: Tx descriptor ring for a specific queue
  2004. *
  2005. * Free all transmit software resources
  2006. **/
  2007. static void wx_free_tx_resources(struct wx_ring *tx_ring)
  2008. {
  2009. wx_clean_tx_ring(tx_ring);
  2010. kvfree(tx_ring->tx_buffer_info);
  2011. tx_ring->tx_buffer_info = NULL;
  2012. /* if not set, then don't free */
  2013. if (!tx_ring->desc)
  2014. return;
  2015. dma_free_coherent(tx_ring->dev, tx_ring->size,
  2016. tx_ring->desc, tx_ring->dma);
  2017. tx_ring->desc = NULL;
  2018. }
  2019. /**
  2020. * wx_free_all_tx_resources - Free Tx Resources for All Queues
  2021. * @wx: pointer to hardware structure
  2022. *
  2023. * Free all transmit software resources
  2024. **/
  2025. static void wx_free_all_tx_resources(struct wx *wx)
  2026. {
  2027. int i;
  2028. for (i = 0; i < wx->num_tx_queues; i++)
  2029. wx_free_tx_resources(wx->tx_ring[i]);
  2030. }
  2031. void wx_free_resources(struct wx *wx)
  2032. {
  2033. wx_free_all_rx_resources(wx);
  2034. wx_free_all_tx_resources(wx);
  2035. }
  2036. EXPORT_SYMBOL(wx_free_resources);
  2037. static int wx_alloc_page_pool(struct wx_ring *rx_ring)
  2038. {
  2039. int ret = 0;
  2040. struct page_pool_params pp_params = {
  2041. .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
  2042. .order = 0,
  2043. .pool_size = rx_ring->count,
  2044. .nid = dev_to_node(rx_ring->dev),
  2045. .dev = rx_ring->dev,
  2046. .dma_dir = DMA_FROM_DEVICE,
  2047. .offset = 0,
  2048. .max_len = PAGE_SIZE,
  2049. };
  2050. rx_ring->page_pool = page_pool_create(&pp_params);
  2051. if (IS_ERR(rx_ring->page_pool)) {
  2052. ret = PTR_ERR(rx_ring->page_pool);
  2053. rx_ring->page_pool = NULL;
  2054. }
  2055. return ret;
  2056. }
  2057. /**
  2058. * wx_setup_rx_resources - allocate Rx resources (Descriptors)
  2059. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  2060. *
  2061. * Returns 0 on success, negative on failure
  2062. **/
  2063. static int wx_setup_rx_resources(struct wx_ring *rx_ring)
  2064. {
  2065. struct device *dev = rx_ring->dev;
  2066. int orig_node = dev_to_node(dev);
  2067. int numa_node = NUMA_NO_NODE;
  2068. int size, ret;
  2069. size = sizeof(struct wx_rx_buffer) * rx_ring->count;
  2070. if (rx_ring->q_vector)
  2071. numa_node = rx_ring->q_vector->numa_node;
  2072. rx_ring->rx_buffer_info = kvmalloc_node(size, GFP_KERNEL, numa_node);
  2073. if (!rx_ring->rx_buffer_info)
  2074. rx_ring->rx_buffer_info = kvmalloc(size, GFP_KERNEL);
  2075. if (!rx_ring->rx_buffer_info)
  2076. goto err;
  2077. /* Round up to nearest 4K */
  2078. rx_ring->size = rx_ring->count * sizeof(union wx_rx_desc);
  2079. rx_ring->size = ALIGN(rx_ring->size, 4096);
  2080. set_dev_node(dev, numa_node);
  2081. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  2082. &rx_ring->dma, GFP_KERNEL);
  2083. if (!rx_ring->desc) {
  2084. set_dev_node(dev, orig_node);
  2085. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  2086. &rx_ring->dma, GFP_KERNEL);
  2087. }
  2088. if (!rx_ring->desc)
  2089. goto err;
  2090. rx_ring->next_to_clean = 0;
  2091. rx_ring->next_to_use = 0;
  2092. ret = wx_alloc_page_pool(rx_ring);
  2093. if (ret < 0) {
  2094. dev_err(rx_ring->dev, "Page pool creation failed: %d\n", ret);
  2095. goto err_desc;
  2096. }
  2097. return 0;
  2098. err_desc:
  2099. dma_free_coherent(dev, rx_ring->size, rx_ring->desc, rx_ring->dma);
  2100. err:
  2101. kvfree(rx_ring->rx_buffer_info);
  2102. rx_ring->rx_buffer_info = NULL;
  2103. dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
  2104. return -ENOMEM;
  2105. }
  2106. /**
  2107. * wx_setup_all_rx_resources - allocate all queues Rx resources
  2108. * @wx: pointer to hardware structure
  2109. *
  2110. * If this function returns with an error, then it's possible one or
  2111. * more of the rings is populated (while the rest are not). It is the
  2112. * callers duty to clean those orphaned rings.
  2113. *
  2114. * Return 0 on success, negative on failure
  2115. **/
  2116. static int wx_setup_all_rx_resources(struct wx *wx)
  2117. {
  2118. int i, err = 0;
  2119. for (i = 0; i < wx->num_rx_queues; i++) {
  2120. err = wx_setup_rx_resources(wx->rx_ring[i]);
  2121. if (!err)
  2122. continue;
  2123. wx_err(wx, "Allocation for Rx Queue %u failed\n", i);
  2124. goto err_setup_rx;
  2125. }
  2126. return 0;
  2127. err_setup_rx:
  2128. /* rewind the index freeing the rings as we go */
  2129. while (i--)
  2130. wx_free_rx_resources(wx->rx_ring[i]);
  2131. return err;
  2132. }
  2133. /**
  2134. * wx_setup_tx_resources - allocate Tx resources (Descriptors)
  2135. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  2136. *
  2137. * Return 0 on success, negative on failure
  2138. **/
  2139. static int wx_setup_tx_resources(struct wx_ring *tx_ring)
  2140. {
  2141. struct device *dev = tx_ring->dev;
  2142. int orig_node = dev_to_node(dev);
  2143. int numa_node = NUMA_NO_NODE;
  2144. int size;
  2145. size = sizeof(struct wx_tx_buffer) * tx_ring->count;
  2146. if (tx_ring->q_vector)
  2147. numa_node = tx_ring->q_vector->numa_node;
  2148. tx_ring->tx_buffer_info = kvmalloc_node(size, GFP_KERNEL, numa_node);
  2149. if (!tx_ring->tx_buffer_info)
  2150. tx_ring->tx_buffer_info = kvmalloc(size, GFP_KERNEL);
  2151. if (!tx_ring->tx_buffer_info)
  2152. goto err;
  2153. /* round up to nearest 4K */
  2154. tx_ring->size = tx_ring->count * sizeof(union wx_tx_desc);
  2155. tx_ring->size = ALIGN(tx_ring->size, 4096);
  2156. set_dev_node(dev, numa_node);
  2157. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  2158. &tx_ring->dma, GFP_KERNEL);
  2159. if (!tx_ring->desc) {
  2160. set_dev_node(dev, orig_node);
  2161. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  2162. &tx_ring->dma, GFP_KERNEL);
  2163. }
  2164. if (!tx_ring->desc)
  2165. goto err;
  2166. tx_ring->next_to_use = 0;
  2167. tx_ring->next_to_clean = 0;
  2168. return 0;
  2169. err:
  2170. kvfree(tx_ring->tx_buffer_info);
  2171. tx_ring->tx_buffer_info = NULL;
  2172. dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
  2173. return -ENOMEM;
  2174. }
  2175. /**
  2176. * wx_setup_all_tx_resources - allocate all queues Tx resources
  2177. * @wx: pointer to private structure
  2178. *
  2179. * If this function returns with an error, then it's possible one or
  2180. * more of the rings is populated (while the rest are not). It is the
  2181. * callers duty to clean those orphaned rings.
  2182. *
  2183. * Return 0 on success, negative on failure
  2184. **/
  2185. static int wx_setup_all_tx_resources(struct wx *wx)
  2186. {
  2187. int i, err = 0;
  2188. for (i = 0; i < wx->num_tx_queues; i++) {
  2189. err = wx_setup_tx_resources(wx->tx_ring[i]);
  2190. if (!err)
  2191. continue;
  2192. wx_err(wx, "Allocation for Tx Queue %u failed\n", i);
  2193. goto err_setup_tx;
  2194. }
  2195. return 0;
  2196. err_setup_tx:
  2197. /* rewind the index freeing the rings as we go */
  2198. while (i--)
  2199. wx_free_tx_resources(wx->tx_ring[i]);
  2200. return err;
  2201. }
  2202. int wx_setup_resources(struct wx *wx)
  2203. {
  2204. int err;
  2205. /* allocate transmit descriptors */
  2206. err = wx_setup_all_tx_resources(wx);
  2207. if (err)
  2208. return err;
  2209. /* allocate receive descriptors */
  2210. err = wx_setup_all_rx_resources(wx);
  2211. if (err)
  2212. goto err_free_tx;
  2213. err = wx_setup_isb_resources(wx);
  2214. if (err)
  2215. goto err_free_rx;
  2216. return 0;
  2217. err_free_rx:
  2218. wx_free_all_rx_resources(wx);
  2219. err_free_tx:
  2220. wx_free_all_tx_resources(wx);
  2221. return err;
  2222. }
  2223. EXPORT_SYMBOL(wx_setup_resources);
  2224. /**
  2225. * wx_get_stats64 - Get System Network Statistics
  2226. * @netdev: network interface device structure
  2227. * @stats: storage space for 64bit statistics
  2228. */
  2229. void wx_get_stats64(struct net_device *netdev,
  2230. struct rtnl_link_stats64 *stats)
  2231. {
  2232. struct wx *wx = netdev_priv(netdev);
  2233. struct wx_hw_stats *hwstats;
  2234. int i;
  2235. wx_update_stats(wx);
  2236. rcu_read_lock();
  2237. for (i = 0; i < wx->num_rx_queues; i++) {
  2238. struct wx_ring *ring = READ_ONCE(wx->rx_ring[i]);
  2239. u64 bytes, packets;
  2240. unsigned int start;
  2241. if (ring) {
  2242. do {
  2243. start = u64_stats_fetch_begin(&ring->syncp);
  2244. packets = ring->stats.packets;
  2245. bytes = ring->stats.bytes;
  2246. } while (u64_stats_fetch_retry(&ring->syncp, start));
  2247. stats->rx_packets += packets;
  2248. stats->rx_bytes += bytes;
  2249. }
  2250. }
  2251. for (i = 0; i < wx->num_tx_queues; i++) {
  2252. struct wx_ring *ring = READ_ONCE(wx->tx_ring[i]);
  2253. u64 bytes, packets;
  2254. unsigned int start;
  2255. if (ring) {
  2256. do {
  2257. start = u64_stats_fetch_begin(&ring->syncp);
  2258. packets = ring->stats.packets;
  2259. bytes = ring->stats.bytes;
  2260. } while (u64_stats_fetch_retry(&ring->syncp,
  2261. start));
  2262. stats->tx_packets += packets;
  2263. stats->tx_bytes += bytes;
  2264. }
  2265. }
  2266. rcu_read_unlock();
  2267. hwstats = &wx->stats;
  2268. stats->rx_errors = hwstats->crcerrs + hwstats->rlec;
  2269. stats->multicast = hwstats->qmprc;
  2270. stats->rx_length_errors = hwstats->rlec;
  2271. stats->rx_crc_errors = hwstats->crcerrs;
  2272. }
  2273. EXPORT_SYMBOL(wx_get_stats64);
  2274. int wx_set_features(struct net_device *netdev, netdev_features_t features)
  2275. {
  2276. netdev_features_t changed = netdev->features ^ features;
  2277. struct wx *wx = netdev_priv(netdev);
  2278. bool need_reset = false;
  2279. if (features & NETIF_F_RXHASH) {
  2280. wr32m(wx, WX_RDB_RA_CTL, WX_RDB_RA_CTL_RSS_EN,
  2281. WX_RDB_RA_CTL_RSS_EN);
  2282. wx->rss_enabled = true;
  2283. } else {
  2284. wr32m(wx, WX_RDB_RA_CTL, WX_RDB_RA_CTL_RSS_EN, 0);
  2285. wx->rss_enabled = false;
  2286. }
  2287. netdev->features = features;
  2288. if (wx->mac.type == wx_mac_sp && changed & NETIF_F_HW_VLAN_CTAG_RX)
  2289. wx->do_reset(netdev);
  2290. else if (changed & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_FILTER))
  2291. wx_set_rx_mode(netdev);
  2292. if (!(test_bit(WX_FLAG_FDIR_CAPABLE, wx->flags)))
  2293. return 0;
  2294. /* Check if Flow Director n-tuple support was enabled or disabled. If
  2295. * the state changed, we need to reset.
  2296. */
  2297. switch (features & NETIF_F_NTUPLE) {
  2298. case NETIF_F_NTUPLE:
  2299. /* turn off ATR, enable perfect filters and reset */
  2300. if (!(test_and_set_bit(WX_FLAG_FDIR_PERFECT, wx->flags)))
  2301. need_reset = true;
  2302. clear_bit(WX_FLAG_FDIR_HASH, wx->flags);
  2303. break;
  2304. default:
  2305. /* turn off perfect filters, enable ATR and reset */
  2306. if (test_and_clear_bit(WX_FLAG_FDIR_PERFECT, wx->flags))
  2307. need_reset = true;
  2308. /* We cannot enable ATR if RSS is disabled */
  2309. if (wx->ring_feature[RING_F_RSS].limit <= 1)
  2310. break;
  2311. set_bit(WX_FLAG_FDIR_HASH, wx->flags);
  2312. break;
  2313. }
  2314. if (need_reset)
  2315. wx->do_reset(netdev);
  2316. return 0;
  2317. }
  2318. EXPORT_SYMBOL(wx_set_features);
  2319. #define NETIF_VLAN_STRIPPING_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
  2320. NETIF_F_HW_VLAN_STAG_RX)
  2321. #define NETIF_VLAN_INSERTION_FEATURES (NETIF_F_HW_VLAN_CTAG_TX | \
  2322. NETIF_F_HW_VLAN_STAG_TX)
  2323. #define NETIF_VLAN_FILTERING_FEATURES (NETIF_F_HW_VLAN_CTAG_FILTER | \
  2324. NETIF_F_HW_VLAN_STAG_FILTER)
  2325. netdev_features_t wx_fix_features(struct net_device *netdev,
  2326. netdev_features_t features)
  2327. {
  2328. netdev_features_t changed = netdev->features ^ features;
  2329. struct wx *wx = netdev_priv(netdev);
  2330. if (changed & NETIF_VLAN_STRIPPING_FEATURES) {
  2331. if ((features & NETIF_VLAN_STRIPPING_FEATURES) != NETIF_VLAN_STRIPPING_FEATURES &&
  2332. (features & NETIF_VLAN_STRIPPING_FEATURES) != 0) {
  2333. features &= ~NETIF_VLAN_STRIPPING_FEATURES;
  2334. features |= netdev->features & NETIF_VLAN_STRIPPING_FEATURES;
  2335. wx_err(wx, "802.1Q and 802.1ad VLAN stripping must be either both on or both off.");
  2336. }
  2337. }
  2338. if (changed & NETIF_VLAN_INSERTION_FEATURES) {
  2339. if ((features & NETIF_VLAN_INSERTION_FEATURES) != NETIF_VLAN_INSERTION_FEATURES &&
  2340. (features & NETIF_VLAN_INSERTION_FEATURES) != 0) {
  2341. features &= ~NETIF_VLAN_INSERTION_FEATURES;
  2342. features |= netdev->features & NETIF_VLAN_INSERTION_FEATURES;
  2343. wx_err(wx, "802.1Q and 802.1ad VLAN insertion must be either both on or both off.");
  2344. }
  2345. }
  2346. if (changed & NETIF_VLAN_FILTERING_FEATURES) {
  2347. if ((features & NETIF_VLAN_FILTERING_FEATURES) != NETIF_VLAN_FILTERING_FEATURES &&
  2348. (features & NETIF_VLAN_FILTERING_FEATURES) != 0) {
  2349. features &= ~NETIF_VLAN_FILTERING_FEATURES;
  2350. features |= netdev->features & NETIF_VLAN_FILTERING_FEATURES;
  2351. wx_err(wx, "802.1Q and 802.1ad VLAN filtering must be either both on or both off.");
  2352. }
  2353. }
  2354. return features;
  2355. }
  2356. EXPORT_SYMBOL(wx_fix_features);
  2357. void wx_set_ring(struct wx *wx, u32 new_tx_count,
  2358. u32 new_rx_count, struct wx_ring *temp_ring)
  2359. {
  2360. int i, err = 0;
  2361. /* Setup new Tx resources and free the old Tx resources in that order.
  2362. * We can then assign the new resources to the rings via a memcpy.
  2363. * The advantage to this approach is that we are guaranteed to still
  2364. * have resources even in the case of an allocation failure.
  2365. */
  2366. if (new_tx_count != wx->tx_ring_count) {
  2367. for (i = 0; i < wx->num_tx_queues; i++) {
  2368. memcpy(&temp_ring[i], wx->tx_ring[i],
  2369. sizeof(struct wx_ring));
  2370. temp_ring[i].count = new_tx_count;
  2371. err = wx_setup_tx_resources(&temp_ring[i]);
  2372. if (err) {
  2373. wx_err(wx, "setup new tx resources failed, keep using the old config\n");
  2374. while (i) {
  2375. i--;
  2376. wx_free_tx_resources(&temp_ring[i]);
  2377. }
  2378. return;
  2379. }
  2380. }
  2381. for (i = 0; i < wx->num_tx_queues; i++) {
  2382. wx_free_tx_resources(wx->tx_ring[i]);
  2383. memcpy(wx->tx_ring[i], &temp_ring[i],
  2384. sizeof(struct wx_ring));
  2385. }
  2386. wx->tx_ring_count = new_tx_count;
  2387. }
  2388. /* Repeat the process for the Rx rings if needed */
  2389. if (new_rx_count != wx->rx_ring_count) {
  2390. for (i = 0; i < wx->num_rx_queues; i++) {
  2391. memcpy(&temp_ring[i], wx->rx_ring[i],
  2392. sizeof(struct wx_ring));
  2393. temp_ring[i].count = new_rx_count;
  2394. err = wx_setup_rx_resources(&temp_ring[i]);
  2395. if (err) {
  2396. wx_err(wx, "setup new rx resources failed, keep using the old config\n");
  2397. while (i) {
  2398. i--;
  2399. wx_free_rx_resources(&temp_ring[i]);
  2400. }
  2401. return;
  2402. }
  2403. }
  2404. for (i = 0; i < wx->num_rx_queues; i++) {
  2405. wx_free_rx_resources(wx->rx_ring[i]);
  2406. memcpy(wx->rx_ring[i], &temp_ring[i],
  2407. sizeof(struct wx_ring));
  2408. }
  2409. wx->rx_ring_count = new_rx_count;
  2410. }
  2411. }
  2412. EXPORT_SYMBOL(wx_set_ring);
  2413. MODULE_DESCRIPTION("Common library for Wangxun(R) Ethernet drivers.");
  2414. MODULE_LICENSE("GPL");