sge.c 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/skbuff.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/ip.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/prefetch.h>
  42. #include <linux/export.h>
  43. #include <net/xfrm.h>
  44. #include <net/ipv6.h>
  45. #include <net/tcp.h>
  46. #include <net/busy_poll.h>
  47. #ifdef CONFIG_CHELSIO_T4_FCOE
  48. #include <scsi/fc/fc_fcoe.h>
  49. #endif /* CONFIG_CHELSIO_T4_FCOE */
  50. #include "cxgb4.h"
  51. #include "t4_regs.h"
  52. #include "t4_values.h"
  53. #include "t4_msg.h"
  54. #include "t4fw_api.h"
  55. #include "cxgb4_ptp.h"
  56. #include "cxgb4_uld.h"
  57. #include "cxgb4_tc_mqprio.h"
  58. #include "sched.h"
  59. /*
  60. * Rx buffer size. We use largish buffers if possible but settle for single
  61. * pages under memory shortage.
  62. */
  63. #if PAGE_SHIFT >= 16
  64. # define FL_PG_ORDER 0
  65. #else
  66. # define FL_PG_ORDER (16 - PAGE_SHIFT)
  67. #endif
  68. /* RX_PULL_LEN should be <= RX_COPY_THRES */
  69. #define RX_COPY_THRES 256
  70. #define RX_PULL_LEN 128
  71. /*
  72. * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
  73. * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
  74. */
  75. #define RX_PKT_SKB_LEN 512
  76. /*
  77. * Max number of Tx descriptors we clean up at a time. Should be modest as
  78. * freeing skbs isn't cheap and it happens while holding locks. We just need
  79. * to free packets faster than they arrive, we eventually catch up and keep
  80. * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES. It should
  81. * also match the CIDX Flush Threshold.
  82. */
  83. #define MAX_TX_RECLAIM 32
  84. /*
  85. * Max number of Rx buffers we replenish at a time. Again keep this modest,
  86. * allocating buffers isn't cheap either.
  87. */
  88. #define MAX_RX_REFILL 16U
  89. /*
  90. * Period of the Rx queue check timer. This timer is infrequent as it has
  91. * something to do only when the system experiences severe memory shortage.
  92. */
  93. #define RX_QCHECK_PERIOD (HZ / 2)
  94. /*
  95. * Period of the Tx queue check timer.
  96. */
  97. #define TX_QCHECK_PERIOD (HZ / 2)
  98. /*
  99. * Max number of Tx descriptors to be reclaimed by the Tx timer.
  100. */
  101. #define MAX_TIMER_TX_RECLAIM 100
  102. /*
  103. * Timer index used when backing off due to memory shortage.
  104. */
  105. #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
  106. /*
  107. * Suspension threshold for non-Ethernet Tx queues. We require enough room
  108. * for a full sized WR.
  109. */
  110. #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
  111. /*
  112. * Max Tx descriptor space we allow for an Ethernet packet to be inlined
  113. * into a WR.
  114. */
  115. #define MAX_IMM_TX_PKT_LEN 256
  116. /*
  117. * Max size of a WR sent through a control Tx queue.
  118. */
  119. #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
  120. struct rx_sw_desc { /* SW state per Rx descriptor */
  121. struct page *page;
  122. dma_addr_t dma_addr;
  123. };
  124. /*
  125. * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
  126. * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
  127. * We could easily support more but there doesn't seem to be much need for
  128. * that ...
  129. */
  130. #define FL_MTU_SMALL 1500
  131. #define FL_MTU_LARGE 9000
  132. static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
  133. unsigned int mtu)
  134. {
  135. struct sge *s = &adapter->sge;
  136. return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
  137. }
  138. #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
  139. #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
  140. /*
  141. * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
  142. * these to specify the buffer size as an index into the SGE Free List Buffer
  143. * Size register array. We also use bit 4, when the buffer has been unmapped
  144. * for DMA, but this is of course never sent to the hardware and is only used
  145. * to prevent double unmappings. All of the above requires that the Free List
  146. * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
  147. * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
  148. * Free List Buffer alignment is 32 bytes, this works out for us ...
  149. */
  150. enum {
  151. RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
  152. RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
  153. RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
  154. /*
  155. * XXX We shouldn't depend on being able to use these indices.
  156. * XXX Especially when some other Master PF has initialized the
  157. * XXX adapter or we use the Firmware Configuration File. We
  158. * XXX should really search through the Host Buffer Size register
  159. * XXX array for the appropriately sized buffer indices.
  160. */
  161. RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
  162. RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
  163. RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
  164. RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
  165. };
  166. static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
  167. #define MIN_NAPI_WORK 1
  168. static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
  169. {
  170. return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
  171. }
  172. static inline bool is_buf_mapped(const struct rx_sw_desc *d)
  173. {
  174. return !(d->dma_addr & RX_UNMAPPED_BUF);
  175. }
  176. /**
  177. * txq_avail - return the number of available slots in a Tx queue
  178. * @q: the Tx queue
  179. *
  180. * Returns the number of descriptors in a Tx queue available to write new
  181. * packets.
  182. */
  183. static inline unsigned int txq_avail(const struct sge_txq *q)
  184. {
  185. return q->size - 1 - q->in_use;
  186. }
  187. /**
  188. * fl_cap - return the capacity of a free-buffer list
  189. * @fl: the FL
  190. *
  191. * Returns the capacity of a free-buffer list. The capacity is less than
  192. * the size because one descriptor needs to be left unpopulated, otherwise
  193. * HW will think the FL is empty.
  194. */
  195. static inline unsigned int fl_cap(const struct sge_fl *fl)
  196. {
  197. return fl->size - 8; /* 1 descriptor = 8 buffers */
  198. }
  199. /**
  200. * fl_starving - return whether a Free List is starving.
  201. * @adapter: pointer to the adapter
  202. * @fl: the Free List
  203. *
  204. * Tests specified Free List to see whether the number of buffers
  205. * available to the hardware has falled below our "starvation"
  206. * threshold.
  207. */
  208. static inline bool fl_starving(const struct adapter *adapter,
  209. const struct sge_fl *fl)
  210. {
  211. const struct sge *s = &adapter->sge;
  212. return fl->avail - fl->pend_cred <= s->fl_starve_thres;
  213. }
  214. int cxgb4_map_skb(struct device *dev, const struct sk_buff *skb,
  215. dma_addr_t *addr)
  216. {
  217. const skb_frag_t *fp, *end;
  218. const struct skb_shared_info *si;
  219. *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  220. if (dma_mapping_error(dev, *addr))
  221. goto out_err;
  222. si = skb_shinfo(skb);
  223. end = &si->frags[si->nr_frags];
  224. for (fp = si->frags; fp < end; fp++) {
  225. *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
  226. DMA_TO_DEVICE);
  227. if (dma_mapping_error(dev, *addr))
  228. goto unwind;
  229. }
  230. return 0;
  231. unwind:
  232. while (fp-- > si->frags)
  233. dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
  234. dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
  235. out_err:
  236. return -ENOMEM;
  237. }
  238. EXPORT_SYMBOL(cxgb4_map_skb);
  239. static void unmap_skb(struct device *dev, const struct sk_buff *skb,
  240. const dma_addr_t *addr)
  241. {
  242. const skb_frag_t *fp, *end;
  243. const struct skb_shared_info *si;
  244. dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
  245. si = skb_shinfo(skb);
  246. end = &si->frags[si->nr_frags];
  247. for (fp = si->frags; fp < end; fp++)
  248. dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
  249. }
  250. #ifdef CONFIG_NEED_DMA_MAP_STATE
  251. /**
  252. * deferred_unmap_destructor - unmap a packet when it is freed
  253. * @skb: the packet
  254. *
  255. * This is the packet destructor used for Tx packets that need to remain
  256. * mapped until they are freed rather than until their Tx descriptors are
  257. * freed.
  258. */
  259. static void deferred_unmap_destructor(struct sk_buff *skb)
  260. {
  261. unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
  262. }
  263. #endif
  264. /**
  265. * free_tx_desc - reclaims Tx descriptors and their buffers
  266. * @adap: the adapter
  267. * @q: the Tx queue to reclaim descriptors from
  268. * @n: the number of descriptors to reclaim
  269. * @unmap: whether the buffers should be unmapped for DMA
  270. *
  271. * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
  272. * Tx buffers. Called with the Tx queue lock held.
  273. */
  274. void free_tx_desc(struct adapter *adap, struct sge_txq *q,
  275. unsigned int n, bool unmap)
  276. {
  277. unsigned int cidx = q->cidx;
  278. struct tx_sw_desc *d;
  279. d = &q->sdesc[cidx];
  280. while (n--) {
  281. if (d->skb) { /* an SGL is present */
  282. if (unmap && d->addr[0]) {
  283. unmap_skb(adap->pdev_dev, d->skb, d->addr);
  284. memset(d->addr, 0, sizeof(d->addr));
  285. }
  286. dev_consume_skb_any(d->skb);
  287. d->skb = NULL;
  288. }
  289. ++d;
  290. if (++cidx == q->size) {
  291. cidx = 0;
  292. d = q->sdesc;
  293. }
  294. }
  295. q->cidx = cidx;
  296. }
  297. /*
  298. * Return the number of reclaimable descriptors in a Tx queue.
  299. */
  300. static inline int reclaimable(const struct sge_txq *q)
  301. {
  302. int hw_cidx = ntohs(READ_ONCE(q->stat->cidx));
  303. hw_cidx -= q->cidx;
  304. return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
  305. }
  306. /**
  307. * reclaim_completed_tx - reclaims completed TX Descriptors
  308. * @adap: the adapter
  309. * @q: the Tx queue to reclaim completed descriptors from
  310. * @maxreclaim: the maximum number of TX Descriptors to reclaim or -1
  311. * @unmap: whether the buffers should be unmapped for DMA
  312. *
  313. * Reclaims Tx Descriptors that the SGE has indicated it has processed,
  314. * and frees the associated buffers if possible. If @max == -1, then
  315. * we'll use a defaiult maximum. Called with the TX Queue locked.
  316. */
  317. static inline int reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
  318. int maxreclaim, bool unmap)
  319. {
  320. int reclaim = reclaimable(q);
  321. if (reclaim) {
  322. /*
  323. * Limit the amount of clean up work we do at a time to keep
  324. * the Tx lock hold time O(1).
  325. */
  326. if (maxreclaim < 0)
  327. maxreclaim = MAX_TX_RECLAIM;
  328. if (reclaim > maxreclaim)
  329. reclaim = maxreclaim;
  330. free_tx_desc(adap, q, reclaim, unmap);
  331. q->in_use -= reclaim;
  332. }
  333. return reclaim;
  334. }
  335. /**
  336. * cxgb4_reclaim_completed_tx - reclaims completed Tx descriptors
  337. * @adap: the adapter
  338. * @q: the Tx queue to reclaim completed descriptors from
  339. * @unmap: whether the buffers should be unmapped for DMA
  340. *
  341. * Reclaims Tx descriptors that the SGE has indicated it has processed,
  342. * and frees the associated buffers if possible. Called with the Tx
  343. * queue locked.
  344. */
  345. void cxgb4_reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
  346. bool unmap)
  347. {
  348. (void)reclaim_completed_tx(adap, q, -1, unmap);
  349. }
  350. EXPORT_SYMBOL(cxgb4_reclaim_completed_tx);
  351. static inline int get_buf_size(struct adapter *adapter,
  352. const struct rx_sw_desc *d)
  353. {
  354. struct sge *s = &adapter->sge;
  355. unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
  356. int buf_size;
  357. switch (rx_buf_size_idx) {
  358. case RX_SMALL_PG_BUF:
  359. buf_size = PAGE_SIZE;
  360. break;
  361. case RX_LARGE_PG_BUF:
  362. buf_size = PAGE_SIZE << s->fl_pg_order;
  363. break;
  364. case RX_SMALL_MTU_BUF:
  365. buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
  366. break;
  367. case RX_LARGE_MTU_BUF:
  368. buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
  369. break;
  370. default:
  371. BUG();
  372. }
  373. return buf_size;
  374. }
  375. /**
  376. * free_rx_bufs - free the Rx buffers on an SGE free list
  377. * @adap: the adapter
  378. * @q: the SGE free list to free buffers from
  379. * @n: how many buffers to free
  380. *
  381. * Release the next @n buffers on an SGE free-buffer Rx queue. The
  382. * buffers must be made inaccessible to HW before calling this function.
  383. */
  384. static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
  385. {
  386. while (n--) {
  387. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  388. if (is_buf_mapped(d))
  389. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  390. get_buf_size(adap, d),
  391. DMA_FROM_DEVICE);
  392. put_page(d->page);
  393. d->page = NULL;
  394. if (++q->cidx == q->size)
  395. q->cidx = 0;
  396. q->avail--;
  397. }
  398. }
  399. /**
  400. * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
  401. * @adap: the adapter
  402. * @q: the SGE free list
  403. *
  404. * Unmap the current buffer on an SGE free-buffer Rx queue. The
  405. * buffer must be made inaccessible to HW before calling this function.
  406. *
  407. * This is similar to @free_rx_bufs above but does not free the buffer.
  408. * Do note that the FL still loses any further access to the buffer.
  409. */
  410. static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
  411. {
  412. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  413. if (is_buf_mapped(d))
  414. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  415. get_buf_size(adap, d), DMA_FROM_DEVICE);
  416. d->page = NULL;
  417. if (++q->cidx == q->size)
  418. q->cidx = 0;
  419. q->avail--;
  420. }
  421. static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
  422. {
  423. if (q->pend_cred >= 8) {
  424. u32 val = adap->params.arch.sge_fl_db;
  425. if (is_t4(adap->params.chip))
  426. val |= PIDX_V(q->pend_cred / 8);
  427. else
  428. val |= PIDX_T5_V(q->pend_cred / 8);
  429. /* Make sure all memory writes to the Free List queue are
  430. * committed before we tell the hardware about them.
  431. */
  432. wmb();
  433. /* If we don't have access to the new User Doorbell (T5+), use
  434. * the old doorbell mechanism; otherwise use the new BAR2
  435. * mechanism.
  436. */
  437. if (unlikely(q->bar2_addr == NULL)) {
  438. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  439. val | QID_V(q->cntxt_id));
  440. } else {
  441. writel(val | QID_V(q->bar2_qid),
  442. q->bar2_addr + SGE_UDB_KDOORBELL);
  443. /* This Write memory Barrier will force the write to
  444. * the User Doorbell area to be flushed.
  445. */
  446. wmb();
  447. }
  448. q->pend_cred &= 7;
  449. }
  450. }
  451. static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
  452. dma_addr_t mapping)
  453. {
  454. sd->page = pg;
  455. sd->dma_addr = mapping; /* includes size low bits */
  456. }
  457. /**
  458. * refill_fl - refill an SGE Rx buffer ring
  459. * @adap: the adapter
  460. * @q: the ring to refill
  461. * @n: the number of new buffers to allocate
  462. * @gfp: the gfp flags for the allocations
  463. *
  464. * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
  465. * allocated with the supplied gfp flags. The caller must assure that
  466. * @n does not exceed the queue's capacity. If afterwards the queue is
  467. * found critically low mark it as starving in the bitmap of starving FLs.
  468. *
  469. * Returns the number of buffers allocated.
  470. */
  471. static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
  472. gfp_t gfp)
  473. {
  474. struct sge *s = &adap->sge;
  475. struct page *pg;
  476. dma_addr_t mapping;
  477. unsigned int cred = q->avail;
  478. __be64 *d = &q->desc[q->pidx];
  479. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  480. int node;
  481. #ifdef CONFIG_DEBUG_FS
  482. if (test_bit(q->cntxt_id - adap->sge.egr_start, adap->sge.blocked_fl))
  483. goto out;
  484. #endif
  485. gfp |= __GFP_NOWARN;
  486. node = dev_to_node(adap->pdev_dev);
  487. if (s->fl_pg_order == 0)
  488. goto alloc_small_pages;
  489. /*
  490. * Prefer large buffers
  491. */
  492. while (n) {
  493. pg = alloc_pages_node(node, gfp | __GFP_COMP, s->fl_pg_order);
  494. if (unlikely(!pg)) {
  495. q->large_alloc_failed++;
  496. break; /* fall back to single pages */
  497. }
  498. mapping = dma_map_page(adap->pdev_dev, pg, 0,
  499. PAGE_SIZE << s->fl_pg_order,
  500. DMA_FROM_DEVICE);
  501. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  502. __free_pages(pg, s->fl_pg_order);
  503. q->mapping_err++;
  504. goto out; /* do not try small pages for this error */
  505. }
  506. mapping |= RX_LARGE_PG_BUF;
  507. *d++ = cpu_to_be64(mapping);
  508. set_rx_sw_desc(sd, pg, mapping);
  509. sd++;
  510. q->avail++;
  511. if (++q->pidx == q->size) {
  512. q->pidx = 0;
  513. sd = q->sdesc;
  514. d = q->desc;
  515. }
  516. n--;
  517. }
  518. alloc_small_pages:
  519. while (n--) {
  520. pg = alloc_pages_node(node, gfp, 0);
  521. if (unlikely(!pg)) {
  522. q->alloc_failed++;
  523. break;
  524. }
  525. mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
  526. DMA_FROM_DEVICE);
  527. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  528. put_page(pg);
  529. q->mapping_err++;
  530. goto out;
  531. }
  532. *d++ = cpu_to_be64(mapping);
  533. set_rx_sw_desc(sd, pg, mapping);
  534. sd++;
  535. q->avail++;
  536. if (++q->pidx == q->size) {
  537. q->pidx = 0;
  538. sd = q->sdesc;
  539. d = q->desc;
  540. }
  541. }
  542. out: cred = q->avail - cred;
  543. q->pend_cred += cred;
  544. ring_fl_db(adap, q);
  545. if (unlikely(fl_starving(adap, q))) {
  546. smp_wmb();
  547. q->low++;
  548. set_bit(q->cntxt_id - adap->sge.egr_start,
  549. adap->sge.starving_fl);
  550. }
  551. return cred;
  552. }
  553. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  554. {
  555. refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  556. GFP_ATOMIC);
  557. }
  558. /**
  559. * alloc_ring - allocate resources for an SGE descriptor ring
  560. * @dev: the PCI device's core device
  561. * @nelem: the number of descriptors
  562. * @elem_size: the size of each descriptor
  563. * @sw_size: the size of the SW state associated with each ring element
  564. * @phys: the physical address of the allocated ring
  565. * @metadata: address of the array holding the SW state for the ring
  566. * @stat_size: extra space in HW ring for status information
  567. * @node: preferred node for memory allocations
  568. *
  569. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  570. * free buffer lists, or response queues. Each SGE ring requires
  571. * space for its HW descriptors plus, optionally, space for the SW state
  572. * associated with each HW entry (the metadata). The function returns
  573. * three values: the virtual address for the HW ring (the return value
  574. * of the function), the bus address of the HW ring, and the address
  575. * of the SW ring.
  576. */
  577. static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
  578. size_t sw_size, dma_addr_t *phys, void *metadata,
  579. size_t stat_size, int node)
  580. {
  581. size_t len = nelem * elem_size + stat_size;
  582. void *s = NULL;
  583. void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
  584. if (!p)
  585. return NULL;
  586. if (sw_size) {
  587. s = kcalloc_node(sw_size, nelem, GFP_KERNEL, node);
  588. if (!s) {
  589. dma_free_coherent(dev, len, p, *phys);
  590. return NULL;
  591. }
  592. }
  593. if (metadata)
  594. *(void **)metadata = s;
  595. return p;
  596. }
  597. /**
  598. * sgl_len - calculates the size of an SGL of the given capacity
  599. * @n: the number of SGL entries
  600. *
  601. * Calculates the number of flits needed for a scatter/gather list that
  602. * can hold the given number of entries.
  603. */
  604. static inline unsigned int sgl_len(unsigned int n)
  605. {
  606. /* A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
  607. * addresses. The DSGL Work Request starts off with a 32-bit DSGL
  608. * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
  609. * repeated sequences of { Length[i], Length[i+1], Address[i],
  610. * Address[i+1] } (this ensures that all addresses are on 64-bit
  611. * boundaries). If N is even, then Length[N+1] should be set to 0 and
  612. * Address[N+1] is omitted.
  613. *
  614. * The following calculation incorporates all of the above. It's
  615. * somewhat hard to follow but, briefly: the "+2" accounts for the
  616. * first two flits which include the DSGL header, Length0 and
  617. * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
  618. * flits for every pair of the remaining N) +1 if (n-1) is odd; and
  619. * finally the "+((n-1)&1)" adds the one remaining flit needed if
  620. * (n-1) is odd ...
  621. */
  622. n--;
  623. return (3 * n) / 2 + (n & 1) + 2;
  624. }
  625. /**
  626. * flits_to_desc - returns the num of Tx descriptors for the given flits
  627. * @n: the number of flits
  628. *
  629. * Returns the number of Tx descriptors needed for the supplied number
  630. * of flits.
  631. */
  632. static inline unsigned int flits_to_desc(unsigned int n)
  633. {
  634. BUG_ON(n > SGE_MAX_WR_LEN / 8);
  635. return DIV_ROUND_UP(n, 8);
  636. }
  637. /**
  638. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  639. * @skb: the packet
  640. * @chip_ver: chip version
  641. *
  642. * Returns whether an Ethernet packet is small enough to fit as
  643. * immediate data. Return value corresponds to headroom required.
  644. */
  645. static inline int is_eth_imm(const struct sk_buff *skb, unsigned int chip_ver)
  646. {
  647. int hdrlen = 0;
  648. if (skb->encapsulation && skb_shinfo(skb)->gso_size &&
  649. chip_ver > CHELSIO_T5) {
  650. hdrlen = sizeof(struct cpl_tx_tnl_lso);
  651. hdrlen += sizeof(struct cpl_tx_pkt_core);
  652. } else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
  653. return 0;
  654. } else {
  655. hdrlen = skb_shinfo(skb)->gso_size ?
  656. sizeof(struct cpl_tx_pkt_lso_core) : 0;
  657. hdrlen += sizeof(struct cpl_tx_pkt);
  658. }
  659. if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
  660. return hdrlen;
  661. return 0;
  662. }
  663. /**
  664. * calc_tx_flits - calculate the number of flits for a packet Tx WR
  665. * @skb: the packet
  666. * @chip_ver: chip version
  667. *
  668. * Returns the number of flits needed for a Tx WR for the given Ethernet
  669. * packet, including the needed WR and CPL headers.
  670. */
  671. static inline unsigned int calc_tx_flits(const struct sk_buff *skb,
  672. unsigned int chip_ver)
  673. {
  674. unsigned int flits;
  675. int hdrlen = is_eth_imm(skb, chip_ver);
  676. /* If the skb is small enough, we can pump it out as a work request
  677. * with only immediate data. In that case we just have to have the
  678. * TX Packet header plus the skb data in the Work Request.
  679. */
  680. if (hdrlen)
  681. return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
  682. /* Otherwise, we're going to have to construct a Scatter gather list
  683. * of the skb body and fragments. We also include the flits necessary
  684. * for the TX Packet Work Request and CPL. We always have a firmware
  685. * Write Header (incorporated as part of the cpl_tx_pkt_lso and
  686. * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
  687. * message or, if we're doing a Large Send Offload, an LSO CPL message
  688. * with an embedded TX Packet Write CPL message.
  689. */
  690. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
  691. if (skb_shinfo(skb)->gso_size) {
  692. if (skb->encapsulation && chip_ver > CHELSIO_T5) {
  693. hdrlen = sizeof(struct fw_eth_tx_pkt_wr) +
  694. sizeof(struct cpl_tx_tnl_lso);
  695. } else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
  696. u32 pkt_hdrlen;
  697. pkt_hdrlen = eth_get_headlen(skb->dev, skb->data,
  698. skb_headlen(skb));
  699. hdrlen = sizeof(struct fw_eth_tx_eo_wr) +
  700. round_up(pkt_hdrlen, 16);
  701. } else {
  702. hdrlen = sizeof(struct fw_eth_tx_pkt_wr) +
  703. sizeof(struct cpl_tx_pkt_lso_core);
  704. }
  705. hdrlen += sizeof(struct cpl_tx_pkt_core);
  706. flits += (hdrlen / sizeof(__be64));
  707. } else {
  708. flits += (sizeof(struct fw_eth_tx_pkt_wr) +
  709. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  710. }
  711. return flits;
  712. }
  713. /**
  714. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  715. * @skb: the packet
  716. * @chip_ver: chip version
  717. *
  718. * Returns the number of Tx descriptors needed for the given Ethernet
  719. * packet, including the needed WR and CPL headers.
  720. */
  721. static inline unsigned int calc_tx_descs(const struct sk_buff *skb,
  722. unsigned int chip_ver)
  723. {
  724. return flits_to_desc(calc_tx_flits(skb, chip_ver));
  725. }
  726. /**
  727. * cxgb4_write_sgl - populate a scatter/gather list for a packet
  728. * @skb: the packet
  729. * @q: the Tx queue we are writing into
  730. * @sgl: starting location for writing the SGL
  731. * @end: points right after the end of the SGL
  732. * @start: start offset into skb main-body data to include in the SGL
  733. * @addr: the list of bus addresses for the SGL elements
  734. *
  735. * Generates a gather list for the buffers that make up a packet.
  736. * The caller must provide adequate space for the SGL that will be written.
  737. * The SGL includes all of the packet's page fragments and the data in its
  738. * main body except for the first @start bytes. @sgl must be 16-byte
  739. * aligned and within a Tx descriptor with available space. @end points
  740. * right after the end of the SGL but does not account for any potential
  741. * wrap around, i.e., @end > @sgl.
  742. */
  743. void cxgb4_write_sgl(const struct sk_buff *skb, struct sge_txq *q,
  744. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  745. const dma_addr_t *addr)
  746. {
  747. unsigned int i, len;
  748. struct ulptx_sge_pair *to;
  749. const struct skb_shared_info *si = skb_shinfo(skb);
  750. unsigned int nfrags = si->nr_frags;
  751. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  752. len = skb_headlen(skb) - start;
  753. if (likely(len)) {
  754. sgl->len0 = htonl(len);
  755. sgl->addr0 = cpu_to_be64(addr[0] + start);
  756. nfrags++;
  757. } else {
  758. sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
  759. sgl->addr0 = cpu_to_be64(addr[1]);
  760. }
  761. sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
  762. ULPTX_NSGE_V(nfrags));
  763. if (likely(--nfrags == 0))
  764. return;
  765. /*
  766. * Most of the complexity below deals with the possibility we hit the
  767. * end of the queue in the middle of writing the SGL. For this case
  768. * only we create the SGL in a temporary buffer and then copy it.
  769. */
  770. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  771. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  772. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  773. to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
  774. to->addr[0] = cpu_to_be64(addr[i]);
  775. to->addr[1] = cpu_to_be64(addr[++i]);
  776. }
  777. if (nfrags) {
  778. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  779. to->len[1] = cpu_to_be32(0);
  780. to->addr[0] = cpu_to_be64(addr[i + 1]);
  781. }
  782. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  783. unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  784. if (likely(part0))
  785. memcpy(sgl->sge, buf, part0);
  786. part1 = (u8 *)end - (u8 *)q->stat;
  787. memcpy(q->desc, (u8 *)buf + part0, part1);
  788. end = (void *)q->desc + part1;
  789. }
  790. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  791. *end = 0;
  792. }
  793. EXPORT_SYMBOL(cxgb4_write_sgl);
  794. /* cxgb4_write_partial_sgl - populate SGL for partial packet
  795. * @skb: the packet
  796. * @q: the Tx queue we are writing into
  797. * @sgl: starting location for writing the SGL
  798. * @end: points right after the end of the SGL
  799. * @addr: the list of bus addresses for the SGL elements
  800. * @start: start offset in the SKB where partial data starts
  801. * @len: length of data from @start to send out
  802. *
  803. * This API will handle sending out partial data of a skb if required.
  804. * Unlike cxgb4_write_sgl, @start can be any offset into the skb data,
  805. * and @len will decide how much data after @start offset to send out.
  806. */
  807. void cxgb4_write_partial_sgl(const struct sk_buff *skb, struct sge_txq *q,
  808. struct ulptx_sgl *sgl, u64 *end,
  809. const dma_addr_t *addr, u32 start, u32 len)
  810. {
  811. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1] = {0}, *to;
  812. u32 frag_size, skb_linear_data_len = skb_headlen(skb);
  813. struct skb_shared_info *si = skb_shinfo(skb);
  814. u8 i = 0, frag_idx = 0, nfrags = 0;
  815. skb_frag_t *frag;
  816. /* Fill the first SGL either from linear data or from partial
  817. * frag based on @start.
  818. */
  819. if (unlikely(start < skb_linear_data_len)) {
  820. frag_size = min(len, skb_linear_data_len - start);
  821. sgl->len0 = htonl(frag_size);
  822. sgl->addr0 = cpu_to_be64(addr[0] + start);
  823. len -= frag_size;
  824. nfrags++;
  825. } else {
  826. start -= skb_linear_data_len;
  827. frag = &si->frags[frag_idx];
  828. frag_size = skb_frag_size(frag);
  829. /* find the first frag */
  830. while (start >= frag_size) {
  831. start -= frag_size;
  832. frag_idx++;
  833. frag = &si->frags[frag_idx];
  834. frag_size = skb_frag_size(frag);
  835. }
  836. frag_size = min(len, skb_frag_size(frag) - start);
  837. sgl->len0 = cpu_to_be32(frag_size);
  838. sgl->addr0 = cpu_to_be64(addr[frag_idx + 1] + start);
  839. len -= frag_size;
  840. nfrags++;
  841. frag_idx++;
  842. }
  843. /* If the entire partial data fit in one SGL, then send it out
  844. * now.
  845. */
  846. if (!len)
  847. goto done;
  848. /* Most of the complexity below deals with the possibility we hit the
  849. * end of the queue in the middle of writing the SGL. For this case
  850. * only we create the SGL in a temporary buffer and then copy it.
  851. */
  852. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  853. /* If the skb couldn't fit in first SGL completely, fill the
  854. * rest of the frags in subsequent SGLs. Note that each SGL
  855. * pair can store 2 frags.
  856. */
  857. while (len) {
  858. frag_size = min(len, skb_frag_size(&si->frags[frag_idx]));
  859. to->len[i & 1] = cpu_to_be32(frag_size);
  860. to->addr[i & 1] = cpu_to_be64(addr[frag_idx + 1]);
  861. if (i && (i & 1))
  862. to++;
  863. nfrags++;
  864. frag_idx++;
  865. i++;
  866. len -= frag_size;
  867. }
  868. /* If we ended in an odd boundary, then set the second SGL's
  869. * length in the pair to 0.
  870. */
  871. if (i & 1)
  872. to->len[1] = cpu_to_be32(0);
  873. /* Copy from temporary buffer to Tx ring, in case we hit the
  874. * end of the queue in the middle of writing the SGL.
  875. */
  876. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  877. u32 part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  878. if (likely(part0))
  879. memcpy(sgl->sge, buf, part0);
  880. part1 = (u8 *)end - (u8 *)q->stat;
  881. memcpy(q->desc, (u8 *)buf + part0, part1);
  882. end = (void *)q->desc + part1;
  883. }
  884. /* 0-pad to multiple of 16 */
  885. if ((uintptr_t)end & 8)
  886. *end = 0;
  887. done:
  888. sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
  889. ULPTX_NSGE_V(nfrags));
  890. }
  891. EXPORT_SYMBOL(cxgb4_write_partial_sgl);
  892. /* This function copies 64 byte coalesced work request to
  893. * memory mapped BAR2 space. For coalesced WR SGE fetches
  894. * data from the FIFO instead of from Host.
  895. */
  896. static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
  897. {
  898. int count = 8;
  899. while (count) {
  900. writeq(*src, dst);
  901. src++;
  902. dst++;
  903. count--;
  904. }
  905. }
  906. /**
  907. * cxgb4_ring_tx_db - check and potentially ring a Tx queue's doorbell
  908. * @adap: the adapter
  909. * @q: the Tx queue
  910. * @n: number of new descriptors to give to HW
  911. *
  912. * Ring the doorbel for a Tx queue.
  913. */
  914. inline void cxgb4_ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
  915. {
  916. /* Make sure that all writes to the TX Descriptors are committed
  917. * before we tell the hardware about them.
  918. */
  919. wmb();
  920. /* If we don't have access to the new User Doorbell (T5+), use the old
  921. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  922. */
  923. if (unlikely(q->bar2_addr == NULL)) {
  924. u32 val = PIDX_V(n);
  925. unsigned long flags;
  926. /* For T4 we need to participate in the Doorbell Recovery
  927. * mechanism.
  928. */
  929. spin_lock_irqsave(&q->db_lock, flags);
  930. if (!q->db_disabled)
  931. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  932. QID_V(q->cntxt_id) | val);
  933. else
  934. q->db_pidx_inc += n;
  935. q->db_pidx = q->pidx;
  936. spin_unlock_irqrestore(&q->db_lock, flags);
  937. } else {
  938. u32 val = PIDX_T5_V(n);
  939. /* T4 and later chips share the same PIDX field offset within
  940. * the doorbell, but T5 and later shrank the field in order to
  941. * gain a bit for Doorbell Priority. The field was absurdly
  942. * large in the first place (14 bits) so we just use the T5
  943. * and later limits and warn if a Queue ID is too large.
  944. */
  945. WARN_ON(val & DBPRIO_F);
  946. /* If we're only writing a single TX Descriptor and we can use
  947. * Inferred QID registers, we can use the Write Combining
  948. * Gather Buffer; otherwise we use the simple doorbell.
  949. */
  950. if (n == 1 && q->bar2_qid == 0) {
  951. int index = (q->pidx
  952. ? (q->pidx - 1)
  953. : (q->size - 1));
  954. u64 *wr = (u64 *)&q->desc[index];
  955. cxgb_pio_copy((u64 __iomem *)
  956. (q->bar2_addr + SGE_UDB_WCDOORBELL),
  957. wr);
  958. } else {
  959. writel(val | QID_V(q->bar2_qid),
  960. q->bar2_addr + SGE_UDB_KDOORBELL);
  961. }
  962. /* This Write Memory Barrier will force the write to the User
  963. * Doorbell area to be flushed. This is needed to prevent
  964. * writes on different CPUs for the same queue from hitting
  965. * the adapter out of order. This is required when some Work
  966. * Requests take the Write Combine Gather Buffer path (user
  967. * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
  968. * take the traditional path where we simply increment the
  969. * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
  970. * hardware DMA read the actual Work Request.
  971. */
  972. wmb();
  973. }
  974. }
  975. EXPORT_SYMBOL(cxgb4_ring_tx_db);
  976. /**
  977. * cxgb4_inline_tx_skb - inline a packet's data into Tx descriptors
  978. * @skb: the packet
  979. * @q: the Tx queue where the packet will be inlined
  980. * @pos: starting position in the Tx queue where to inline the packet
  981. *
  982. * Inline a packet's contents directly into Tx descriptors, starting at
  983. * the given position within the Tx DMA ring.
  984. * Most of the complexity of this operation is dealing with wrap arounds
  985. * in the middle of the packet we want to inline.
  986. */
  987. void cxgb4_inline_tx_skb(const struct sk_buff *skb,
  988. const struct sge_txq *q, void *pos)
  989. {
  990. int left = (void *)q->stat - pos;
  991. u64 *p;
  992. if (likely(skb->len <= left)) {
  993. if (likely(!skb->data_len))
  994. skb_copy_from_linear_data(skb, pos, skb->len);
  995. else
  996. skb_copy_bits(skb, 0, pos, skb->len);
  997. pos += skb->len;
  998. } else {
  999. skb_copy_bits(skb, 0, pos, left);
  1000. skb_copy_bits(skb, left, q->desc, skb->len - left);
  1001. pos = (void *)q->desc + (skb->len - left);
  1002. }
  1003. /* 0-pad to multiple of 16 */
  1004. p = PTR_ALIGN(pos, 8);
  1005. if ((uintptr_t)p & 8)
  1006. *p = 0;
  1007. }
  1008. EXPORT_SYMBOL(cxgb4_inline_tx_skb);
  1009. static void *inline_tx_skb_header(const struct sk_buff *skb,
  1010. const struct sge_txq *q, void *pos,
  1011. int length)
  1012. {
  1013. u64 *p;
  1014. int left = (void *)q->stat - pos;
  1015. if (likely(length <= left)) {
  1016. memcpy(pos, skb->data, length);
  1017. pos += length;
  1018. } else {
  1019. memcpy(pos, skb->data, left);
  1020. memcpy(q->desc, skb->data + left, length - left);
  1021. pos = (void *)q->desc + (length - left);
  1022. }
  1023. /* 0-pad to multiple of 16 */
  1024. p = PTR_ALIGN(pos, 8);
  1025. if ((uintptr_t)p & 8) {
  1026. *p = 0;
  1027. return p + 1;
  1028. }
  1029. return p;
  1030. }
  1031. /*
  1032. * Figure out what HW csum a packet wants and return the appropriate control
  1033. * bits.
  1034. */
  1035. static u64 hwcsum(enum chip_type chip, const struct sk_buff *skb)
  1036. {
  1037. int csum_type;
  1038. bool inner_hdr_csum = false;
  1039. u16 proto, ver;
  1040. if (skb->encapsulation &&
  1041. (CHELSIO_CHIP_VERSION(chip) > CHELSIO_T5))
  1042. inner_hdr_csum = true;
  1043. if (inner_hdr_csum) {
  1044. ver = inner_ip_hdr(skb)->version;
  1045. proto = (ver == 4) ? inner_ip_hdr(skb)->protocol :
  1046. inner_ipv6_hdr(skb)->nexthdr;
  1047. } else {
  1048. ver = ip_hdr(skb)->version;
  1049. proto = (ver == 4) ? ip_hdr(skb)->protocol :
  1050. ipv6_hdr(skb)->nexthdr;
  1051. }
  1052. if (ver == 4) {
  1053. if (proto == IPPROTO_TCP)
  1054. csum_type = TX_CSUM_TCPIP;
  1055. else if (proto == IPPROTO_UDP)
  1056. csum_type = TX_CSUM_UDPIP;
  1057. else {
  1058. nocsum: /*
  1059. * unknown protocol, disable HW csum
  1060. * and hope a bad packet is detected
  1061. */
  1062. return TXPKT_L4CSUM_DIS_F;
  1063. }
  1064. } else {
  1065. /*
  1066. * this doesn't work with extension headers
  1067. */
  1068. if (proto == IPPROTO_TCP)
  1069. csum_type = TX_CSUM_TCPIP6;
  1070. else if (proto == IPPROTO_UDP)
  1071. csum_type = TX_CSUM_UDPIP6;
  1072. else
  1073. goto nocsum;
  1074. }
  1075. if (likely(csum_type >= TX_CSUM_TCPIP)) {
  1076. int eth_hdr_len, l4_len;
  1077. u64 hdr_len;
  1078. if (inner_hdr_csum) {
  1079. /* This allows checksum offload for all encapsulated
  1080. * packets like GRE etc..
  1081. */
  1082. l4_len = skb_inner_network_header_len(skb);
  1083. eth_hdr_len = skb_inner_network_offset(skb) - ETH_HLEN;
  1084. } else {
  1085. l4_len = skb_network_header_len(skb);
  1086. eth_hdr_len = skb_network_offset(skb) - ETH_HLEN;
  1087. }
  1088. hdr_len = TXPKT_IPHDR_LEN_V(l4_len);
  1089. if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
  1090. hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len);
  1091. else
  1092. hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len);
  1093. return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len;
  1094. } else {
  1095. int start = skb_transport_offset(skb);
  1096. return TXPKT_CSUM_TYPE_V(csum_type) |
  1097. TXPKT_CSUM_START_V(start) |
  1098. TXPKT_CSUM_LOC_V(start + skb->csum_offset);
  1099. }
  1100. }
  1101. static void eth_txq_stop(struct sge_eth_txq *q)
  1102. {
  1103. netif_tx_stop_queue(q->txq);
  1104. q->q.stops++;
  1105. }
  1106. static inline void txq_advance(struct sge_txq *q, unsigned int n)
  1107. {
  1108. q->in_use += n;
  1109. q->pidx += n;
  1110. if (q->pidx >= q->size)
  1111. q->pidx -= q->size;
  1112. }
  1113. #ifdef CONFIG_CHELSIO_T4_FCOE
  1114. static inline int
  1115. cxgb_fcoe_offload(struct sk_buff *skb, struct adapter *adap,
  1116. const struct port_info *pi, u64 *cntrl)
  1117. {
  1118. const struct cxgb_fcoe *fcoe = &pi->fcoe;
  1119. if (!(fcoe->flags & CXGB_FCOE_ENABLED))
  1120. return 0;
  1121. if (skb->protocol != htons(ETH_P_FCOE))
  1122. return 0;
  1123. skb_reset_mac_header(skb);
  1124. skb->mac_len = sizeof(struct ethhdr);
  1125. skb_set_network_header(skb, skb->mac_len);
  1126. skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
  1127. if (!cxgb_fcoe_sof_eof_supported(adap, skb))
  1128. return -ENOTSUPP;
  1129. /* FC CRC offload */
  1130. *cntrl = TXPKT_CSUM_TYPE_V(TX_CSUM_FCOE) |
  1131. TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F |
  1132. TXPKT_CSUM_START_V(CXGB_FCOE_TXPKT_CSUM_START) |
  1133. TXPKT_CSUM_END_V(CXGB_FCOE_TXPKT_CSUM_END) |
  1134. TXPKT_CSUM_LOC_V(CXGB_FCOE_TXPKT_CSUM_END);
  1135. return 0;
  1136. }
  1137. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1138. /* Returns tunnel type if hardware supports offloading of the same.
  1139. * It is called only for T5 and onwards.
  1140. */
  1141. enum cpl_tx_tnl_lso_type cxgb_encap_offload_supported(struct sk_buff *skb)
  1142. {
  1143. u8 l4_hdr = 0;
  1144. enum cpl_tx_tnl_lso_type tnl_type = TX_TNL_TYPE_OPAQUE;
  1145. struct port_info *pi = netdev_priv(skb->dev);
  1146. struct adapter *adapter = pi->adapter;
  1147. if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  1148. skb->inner_protocol != htons(ETH_P_TEB))
  1149. return tnl_type;
  1150. switch (vlan_get_protocol(skb)) {
  1151. case htons(ETH_P_IP):
  1152. l4_hdr = ip_hdr(skb)->protocol;
  1153. break;
  1154. case htons(ETH_P_IPV6):
  1155. l4_hdr = ipv6_hdr(skb)->nexthdr;
  1156. break;
  1157. default:
  1158. return tnl_type;
  1159. }
  1160. switch (l4_hdr) {
  1161. case IPPROTO_UDP:
  1162. if (adapter->vxlan_port == udp_hdr(skb)->dest)
  1163. tnl_type = TX_TNL_TYPE_VXLAN;
  1164. else if (adapter->geneve_port == udp_hdr(skb)->dest)
  1165. tnl_type = TX_TNL_TYPE_GENEVE;
  1166. break;
  1167. default:
  1168. return tnl_type;
  1169. }
  1170. return tnl_type;
  1171. }
  1172. static inline void t6_fill_tnl_lso(struct sk_buff *skb,
  1173. struct cpl_tx_tnl_lso *tnl_lso,
  1174. enum cpl_tx_tnl_lso_type tnl_type)
  1175. {
  1176. u32 val;
  1177. int in_eth_xtra_len;
  1178. int l3hdr_len = skb_network_header_len(skb);
  1179. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  1180. const struct skb_shared_info *ssi = skb_shinfo(skb);
  1181. bool v6 = (ip_hdr(skb)->version == 6);
  1182. val = CPL_TX_TNL_LSO_OPCODE_V(CPL_TX_TNL_LSO) |
  1183. CPL_TX_TNL_LSO_FIRST_F |
  1184. CPL_TX_TNL_LSO_LAST_F |
  1185. (v6 ? CPL_TX_TNL_LSO_IPV6OUT_F : 0) |
  1186. CPL_TX_TNL_LSO_ETHHDRLENOUT_V(eth_xtra_len / 4) |
  1187. CPL_TX_TNL_LSO_IPHDRLENOUT_V(l3hdr_len / 4) |
  1188. (v6 ? 0 : CPL_TX_TNL_LSO_IPHDRCHKOUT_F) |
  1189. CPL_TX_TNL_LSO_IPLENSETOUT_F |
  1190. (v6 ? 0 : CPL_TX_TNL_LSO_IPIDINCOUT_F);
  1191. tnl_lso->op_to_IpIdSplitOut = htonl(val);
  1192. tnl_lso->IpIdOffsetOut = 0;
  1193. /* Get the tunnel header length */
  1194. val = skb_inner_mac_header(skb) - skb_mac_header(skb);
  1195. in_eth_xtra_len = skb_inner_network_header(skb) -
  1196. skb_inner_mac_header(skb) - ETH_HLEN;
  1197. switch (tnl_type) {
  1198. case TX_TNL_TYPE_VXLAN:
  1199. case TX_TNL_TYPE_GENEVE:
  1200. tnl_lso->UdpLenSetOut_to_TnlHdrLen =
  1201. htons(CPL_TX_TNL_LSO_UDPCHKCLROUT_F |
  1202. CPL_TX_TNL_LSO_UDPLENSETOUT_F);
  1203. break;
  1204. default:
  1205. tnl_lso->UdpLenSetOut_to_TnlHdrLen = 0;
  1206. break;
  1207. }
  1208. tnl_lso->UdpLenSetOut_to_TnlHdrLen |=
  1209. htons(CPL_TX_TNL_LSO_TNLHDRLEN_V(val) |
  1210. CPL_TX_TNL_LSO_TNLTYPE_V(tnl_type));
  1211. tnl_lso->r1 = 0;
  1212. val = CPL_TX_TNL_LSO_ETHHDRLEN_V(in_eth_xtra_len / 4) |
  1213. CPL_TX_TNL_LSO_IPV6_V(inner_ip_hdr(skb)->version == 6) |
  1214. CPL_TX_TNL_LSO_IPHDRLEN_V(skb_inner_network_header_len(skb) / 4) |
  1215. CPL_TX_TNL_LSO_TCPHDRLEN_V(inner_tcp_hdrlen(skb) / 4);
  1216. tnl_lso->Flow_to_TcpHdrLen = htonl(val);
  1217. tnl_lso->IpIdOffset = htons(0);
  1218. tnl_lso->IpIdSplit_to_Mss = htons(CPL_TX_TNL_LSO_MSS_V(ssi->gso_size));
  1219. tnl_lso->TCPSeqOffset = htonl(0);
  1220. tnl_lso->EthLenOffset_Size = htonl(CPL_TX_TNL_LSO_SIZE_V(skb->len));
  1221. }
  1222. static inline void *write_tso_wr(struct adapter *adap, struct sk_buff *skb,
  1223. struct cpl_tx_pkt_lso_core *lso)
  1224. {
  1225. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  1226. int l3hdr_len = skb_network_header_len(skb);
  1227. const struct skb_shared_info *ssi;
  1228. bool ipv6 = false;
  1229. ssi = skb_shinfo(skb);
  1230. if (ssi->gso_type & SKB_GSO_TCPV6)
  1231. ipv6 = true;
  1232. lso->lso_ctrl = htonl(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
  1233. LSO_FIRST_SLICE_F | LSO_LAST_SLICE_F |
  1234. LSO_IPV6_V(ipv6) |
  1235. LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
  1236. LSO_IPHDR_LEN_V(l3hdr_len / 4) |
  1237. LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
  1238. lso->ipid_ofst = htons(0);
  1239. lso->mss = htons(ssi->gso_size);
  1240. lso->seqno_offset = htonl(0);
  1241. if (is_t4(adap->params.chip))
  1242. lso->len = htonl(skb->len);
  1243. else
  1244. lso->len = htonl(LSO_T5_XFER_SIZE_V(skb->len));
  1245. return (void *)(lso + 1);
  1246. }
  1247. /**
  1248. * t4_sge_eth_txq_egress_update - handle Ethernet TX Queue update
  1249. * @adap: the adapter
  1250. * @eq: the Ethernet TX Queue
  1251. * @maxreclaim: the maximum number of TX Descriptors to reclaim or -1
  1252. *
  1253. * We're typically called here to update the state of an Ethernet TX
  1254. * Queue with respect to the hardware's progress in consuming the TX
  1255. * Work Requests that we've put on that Egress Queue. This happens
  1256. * when we get Egress Queue Update messages and also prophylactically
  1257. * in regular timer-based Ethernet TX Queue maintenance.
  1258. */
  1259. int t4_sge_eth_txq_egress_update(struct adapter *adap, struct sge_eth_txq *eq,
  1260. int maxreclaim)
  1261. {
  1262. unsigned int reclaimed, hw_cidx;
  1263. struct sge_txq *q = &eq->q;
  1264. int hw_in_use;
  1265. if (!q->in_use || !__netif_tx_trylock(eq->txq))
  1266. return 0;
  1267. /* Reclaim pending completed TX Descriptors. */
  1268. reclaimed = reclaim_completed_tx(adap, &eq->q, maxreclaim, true);
  1269. hw_cidx = ntohs(READ_ONCE(q->stat->cidx));
  1270. hw_in_use = q->pidx - hw_cidx;
  1271. if (hw_in_use < 0)
  1272. hw_in_use += q->size;
  1273. /* If the TX Queue is currently stopped and there's now more than half
  1274. * the queue available, restart it. Otherwise bail out since the rest
  1275. * of what we want do here is with the possibility of shipping any
  1276. * currently buffered Coalesced TX Work Request.
  1277. */
  1278. if (netif_tx_queue_stopped(eq->txq) && hw_in_use < (q->size / 2)) {
  1279. netif_tx_wake_queue(eq->txq);
  1280. eq->q.restarts++;
  1281. }
  1282. __netif_tx_unlock(eq->txq);
  1283. return reclaimed;
  1284. }
  1285. static inline int cxgb4_validate_skb(struct sk_buff *skb,
  1286. struct net_device *dev,
  1287. u32 min_pkt_len)
  1288. {
  1289. u32 max_pkt_len;
  1290. /* The chip min packet length is 10 octets but some firmware
  1291. * commands have a minimum packet length requirement. So, play
  1292. * safe and reject anything shorter than @min_pkt_len.
  1293. */
  1294. if (unlikely(skb->len < min_pkt_len))
  1295. return -EINVAL;
  1296. /* Discard the packet if the length is greater than mtu */
  1297. max_pkt_len = ETH_HLEN + dev->mtu;
  1298. if (skb_vlan_tagged(skb))
  1299. max_pkt_len += VLAN_HLEN;
  1300. if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
  1301. return -EINVAL;
  1302. return 0;
  1303. }
  1304. static void *write_eo_udp_wr(struct sk_buff *skb, struct fw_eth_tx_eo_wr *wr,
  1305. u32 hdr_len)
  1306. {
  1307. wr->u.udpseg.type = FW_ETH_TX_EO_TYPE_UDPSEG;
  1308. wr->u.udpseg.ethlen = skb_network_offset(skb);
  1309. wr->u.udpseg.iplen = cpu_to_be16(skb_network_header_len(skb));
  1310. wr->u.udpseg.udplen = sizeof(struct udphdr);
  1311. wr->u.udpseg.rtplen = 0;
  1312. wr->u.udpseg.r4 = 0;
  1313. if (skb_shinfo(skb)->gso_size)
  1314. wr->u.udpseg.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
  1315. else
  1316. wr->u.udpseg.mss = cpu_to_be16(skb->len - hdr_len);
  1317. wr->u.udpseg.schedpktsize = wr->u.udpseg.mss;
  1318. wr->u.udpseg.plen = cpu_to_be32(skb->len - hdr_len);
  1319. return (void *)(wr + 1);
  1320. }
  1321. /**
  1322. * cxgb4_eth_xmit - add a packet to an Ethernet Tx queue
  1323. * @skb: the packet
  1324. * @dev: the egress net device
  1325. *
  1326. * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
  1327. */
  1328. static netdev_tx_t cxgb4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  1329. {
  1330. enum cpl_tx_tnl_lso_type tnl_type = TX_TNL_TYPE_OPAQUE;
  1331. bool ptp_enabled = is_ptp_enabled(skb, dev);
  1332. unsigned int last_desc, flits, ndesc;
  1333. u32 wr_mid, ctrl0, op, sgl_off = 0;
  1334. const struct skb_shared_info *ssi;
  1335. int len, qidx, credits, ret, left;
  1336. struct tx_sw_desc *sgl_sdesc;
  1337. struct fw_eth_tx_eo_wr *eowr;
  1338. struct fw_eth_tx_pkt_wr *wr;
  1339. struct cpl_tx_pkt_core *cpl;
  1340. const struct port_info *pi;
  1341. bool immediate = false;
  1342. u64 cntrl, *end, *sgl;
  1343. struct sge_eth_txq *q;
  1344. unsigned int chip_ver;
  1345. struct adapter *adap;
  1346. ret = cxgb4_validate_skb(skb, dev, ETH_HLEN);
  1347. if (ret)
  1348. goto out_free;
  1349. pi = netdev_priv(dev);
  1350. adap = pi->adapter;
  1351. ssi = skb_shinfo(skb);
  1352. #if IS_ENABLED(CONFIG_CHELSIO_IPSEC_INLINE)
  1353. if (xfrm_offload(skb) && !ssi->gso_size)
  1354. return adap->uld[CXGB4_ULD_IPSEC].tx_handler(skb, dev);
  1355. #endif /* CHELSIO_IPSEC_INLINE */
  1356. #if IS_ENABLED(CONFIG_CHELSIO_TLS_DEVICE)
  1357. if (cxgb4_is_ktls_skb(skb) &&
  1358. (skb->len - skb_tcp_all_headers(skb)))
  1359. return adap->uld[CXGB4_ULD_KTLS].tx_handler(skb, dev);
  1360. #endif /* CHELSIO_TLS_DEVICE */
  1361. qidx = skb_get_queue_mapping(skb);
  1362. if (ptp_enabled) {
  1363. if (!(adap->ptp_tx_skb)) {
  1364. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1365. adap->ptp_tx_skb = skb_get(skb);
  1366. } else {
  1367. goto out_free;
  1368. }
  1369. q = &adap->sge.ptptxq;
  1370. } else {
  1371. q = &adap->sge.ethtxq[qidx + pi->first_qset];
  1372. }
  1373. skb_tx_timestamp(skb);
  1374. reclaim_completed_tx(adap, &q->q, -1, true);
  1375. cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
  1376. #ifdef CONFIG_CHELSIO_T4_FCOE
  1377. ret = cxgb_fcoe_offload(skb, adap, pi, &cntrl);
  1378. if (unlikely(ret == -EOPNOTSUPP))
  1379. goto out_free;
  1380. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1381. chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
  1382. flits = calc_tx_flits(skb, chip_ver);
  1383. ndesc = flits_to_desc(flits);
  1384. credits = txq_avail(&q->q) - ndesc;
  1385. if (unlikely(credits < 0)) {
  1386. eth_txq_stop(q);
  1387. dev_err(adap->pdev_dev,
  1388. "%s: Tx ring %u full while queue awake!\n",
  1389. dev->name, qidx);
  1390. return NETDEV_TX_BUSY;
  1391. }
  1392. if (is_eth_imm(skb, chip_ver))
  1393. immediate = true;
  1394. if (skb->encapsulation && chip_ver > CHELSIO_T5)
  1395. tnl_type = cxgb_encap_offload_supported(skb);
  1396. last_desc = q->q.pidx + ndesc - 1;
  1397. if (last_desc >= q->q.size)
  1398. last_desc -= q->q.size;
  1399. sgl_sdesc = &q->q.sdesc[last_desc];
  1400. if (!immediate &&
  1401. unlikely(cxgb4_map_skb(adap->pdev_dev, skb, sgl_sdesc->addr) < 0)) {
  1402. memset(sgl_sdesc->addr, 0, sizeof(sgl_sdesc->addr));
  1403. q->mapping_err++;
  1404. goto out_free;
  1405. }
  1406. wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
  1407. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  1408. /* After we're done injecting the Work Request for this
  1409. * packet, we'll be below our "stop threshold" so stop the TX
  1410. * Queue now and schedule a request for an SGE Egress Queue
  1411. * Update message. The queue will get started later on when
  1412. * the firmware processes this Work Request and sends us an
  1413. * Egress Queue Status Update message indicating that space
  1414. * has opened up.
  1415. */
  1416. eth_txq_stop(q);
  1417. if (chip_ver > CHELSIO_T5)
  1418. wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
  1419. }
  1420. wr = (void *)&q->q.desc[q->q.pidx];
  1421. eowr = (void *)&q->q.desc[q->q.pidx];
  1422. wr->equiq_to_len16 = htonl(wr_mid);
  1423. wr->r3 = cpu_to_be64(0);
  1424. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
  1425. end = (u64 *)eowr + flits;
  1426. else
  1427. end = (u64 *)wr + flits;
  1428. len = immediate ? skb->len : 0;
  1429. len += sizeof(*cpl);
  1430. if (ssi->gso_size && !(ssi->gso_type & SKB_GSO_UDP_L4)) {
  1431. struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
  1432. struct cpl_tx_tnl_lso *tnl_lso = (void *)(wr + 1);
  1433. if (tnl_type)
  1434. len += sizeof(*tnl_lso);
  1435. else
  1436. len += sizeof(*lso);
  1437. wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
  1438. FW_WR_IMMDLEN_V(len));
  1439. if (tnl_type) {
  1440. struct iphdr *iph = ip_hdr(skb);
  1441. t6_fill_tnl_lso(skb, tnl_lso, tnl_type);
  1442. cpl = (void *)(tnl_lso + 1);
  1443. /* Driver is expected to compute partial checksum that
  1444. * does not include the IP Total Length.
  1445. */
  1446. if (iph->version == 4) {
  1447. iph->check = 0;
  1448. iph->tot_len = 0;
  1449. iph->check = ~ip_fast_csum((u8 *)iph, iph->ihl);
  1450. }
  1451. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1452. cntrl = hwcsum(adap->params.chip, skb);
  1453. } else {
  1454. cpl = write_tso_wr(adap, skb, lso);
  1455. cntrl = hwcsum(adap->params.chip, skb);
  1456. }
  1457. sgl = (u64 *)(cpl + 1); /* sgl start here */
  1458. q->tso++;
  1459. q->tx_cso += ssi->gso_segs;
  1460. } else if (ssi->gso_size) {
  1461. u64 *start;
  1462. u32 hdrlen;
  1463. hdrlen = eth_get_headlen(dev, skb->data, skb_headlen(skb));
  1464. len += hdrlen;
  1465. wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_EO_WR) |
  1466. FW_ETH_TX_EO_WR_IMMDLEN_V(len));
  1467. cpl = write_eo_udp_wr(skb, eowr, hdrlen);
  1468. cntrl = hwcsum(adap->params.chip, skb);
  1469. start = (u64 *)(cpl + 1);
  1470. sgl = (u64 *)inline_tx_skb_header(skb, &q->q, (void *)start,
  1471. hdrlen);
  1472. if (unlikely(start > sgl)) {
  1473. left = (u8 *)end - (u8 *)q->q.stat;
  1474. end = (void *)q->q.desc + left;
  1475. }
  1476. sgl_off = hdrlen;
  1477. q->uso++;
  1478. q->tx_cso += ssi->gso_segs;
  1479. } else {
  1480. if (ptp_enabled)
  1481. op = FW_PTP_TX_PKT_WR;
  1482. else
  1483. op = FW_ETH_TX_PKT_WR;
  1484. wr->op_immdlen = htonl(FW_WR_OP_V(op) |
  1485. FW_WR_IMMDLEN_V(len));
  1486. cpl = (void *)(wr + 1);
  1487. sgl = (u64 *)(cpl + 1);
  1488. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1489. cntrl = hwcsum(adap->params.chip, skb) |
  1490. TXPKT_IPCSUM_DIS_F;
  1491. q->tx_cso++;
  1492. }
  1493. }
  1494. if (unlikely((u8 *)sgl >= (u8 *)q->q.stat)) {
  1495. /* If current position is already at the end of the
  1496. * txq, reset the current to point to start of the queue
  1497. * and update the end ptr as well.
  1498. */
  1499. left = (u8 *)end - (u8 *)q->q.stat;
  1500. end = (void *)q->q.desc + left;
  1501. sgl = (void *)q->q.desc;
  1502. }
  1503. if (skb_vlan_tag_present(skb)) {
  1504. q->vlan_ins++;
  1505. cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
  1506. #ifdef CONFIG_CHELSIO_T4_FCOE
  1507. if (skb->protocol == htons(ETH_P_FCOE))
  1508. cntrl |= TXPKT_VLAN_V(
  1509. ((skb->priority & 0x7) << VLAN_PRIO_SHIFT));
  1510. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1511. }
  1512. ctrl0 = TXPKT_OPCODE_V(CPL_TX_PKT_XT) | TXPKT_INTF_V(pi->tx_chan) |
  1513. TXPKT_PF_V(adap->pf);
  1514. if (ptp_enabled)
  1515. ctrl0 |= TXPKT_TSTAMP_F;
  1516. #ifdef CONFIG_CHELSIO_T4_DCB
  1517. if (is_t4(adap->params.chip))
  1518. ctrl0 |= TXPKT_OVLAN_IDX_V(q->dcb_prio);
  1519. else
  1520. ctrl0 |= TXPKT_T5_OVLAN_IDX_V(q->dcb_prio);
  1521. #endif
  1522. cpl->ctrl0 = htonl(ctrl0);
  1523. cpl->pack = htons(0);
  1524. cpl->len = htons(skb->len);
  1525. cpl->ctrl1 = cpu_to_be64(cntrl);
  1526. if (immediate) {
  1527. cxgb4_inline_tx_skb(skb, &q->q, sgl);
  1528. dev_consume_skb_any(skb);
  1529. } else {
  1530. cxgb4_write_sgl(skb, &q->q, (void *)sgl, end, sgl_off,
  1531. sgl_sdesc->addr);
  1532. skb_orphan(skb);
  1533. sgl_sdesc->skb = skb;
  1534. }
  1535. txq_advance(&q->q, ndesc);
  1536. cxgb4_ring_tx_db(adap, &q->q, ndesc);
  1537. return NETDEV_TX_OK;
  1538. out_free:
  1539. dev_kfree_skb_any(skb);
  1540. return NETDEV_TX_OK;
  1541. }
  1542. /* Constants ... */
  1543. enum {
  1544. /* Egress Queue sizes, producer and consumer indices are all in units
  1545. * of Egress Context Units bytes. Note that as far as the hardware is
  1546. * concerned, the free list is an Egress Queue (the host produces free
  1547. * buffers which the hardware consumes) and free list entries are
  1548. * 64-bit PCI DMA addresses.
  1549. */
  1550. EQ_UNIT = SGE_EQ_IDXSIZE,
  1551. FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
  1552. TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
  1553. T4VF_ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) +
  1554. sizeof(struct cpl_tx_pkt_lso_core) +
  1555. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64),
  1556. };
  1557. /**
  1558. * t4vf_is_eth_imm - can an Ethernet packet be sent as immediate data?
  1559. * @skb: the packet
  1560. *
  1561. * Returns whether an Ethernet packet is small enough to fit completely as
  1562. * immediate data.
  1563. */
  1564. static inline int t4vf_is_eth_imm(const struct sk_buff *skb)
  1565. {
  1566. /* The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request
  1567. * which does not accommodate immediate data. We could dike out all
  1568. * of the support code for immediate data but that would tie our hands
  1569. * too much if we ever want to enhace the firmware. It would also
  1570. * create more differences between the PF and VF Drivers.
  1571. */
  1572. return false;
  1573. }
  1574. /**
  1575. * t4vf_calc_tx_flits - calculate the number of flits for a packet TX WR
  1576. * @skb: the packet
  1577. *
  1578. * Returns the number of flits needed for a TX Work Request for the
  1579. * given Ethernet packet, including the needed WR and CPL headers.
  1580. */
  1581. static inline unsigned int t4vf_calc_tx_flits(const struct sk_buff *skb)
  1582. {
  1583. unsigned int flits;
  1584. /* If the skb is small enough, we can pump it out as a work request
  1585. * with only immediate data. In that case we just have to have the
  1586. * TX Packet header plus the skb data in the Work Request.
  1587. */
  1588. if (t4vf_is_eth_imm(skb))
  1589. return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt),
  1590. sizeof(__be64));
  1591. /* Otherwise, we're going to have to construct a Scatter gather list
  1592. * of the skb body and fragments. We also include the flits necessary
  1593. * for the TX Packet Work Request and CPL. We always have a firmware
  1594. * Write Header (incorporated as part of the cpl_tx_pkt_lso and
  1595. * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
  1596. * message or, if we're doing a Large Send Offload, an LSO CPL message
  1597. * with an embedded TX Packet Write CPL message.
  1598. */
  1599. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
  1600. if (skb_shinfo(skb)->gso_size)
  1601. flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
  1602. sizeof(struct cpl_tx_pkt_lso_core) +
  1603. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  1604. else
  1605. flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
  1606. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  1607. return flits;
  1608. }
  1609. /**
  1610. * cxgb4_vf_eth_xmit - add a packet to an Ethernet TX queue
  1611. * @skb: the packet
  1612. * @dev: the egress net device
  1613. *
  1614. * Add a packet to an SGE Ethernet TX queue. Runs with softirqs disabled.
  1615. */
  1616. static netdev_tx_t cxgb4_vf_eth_xmit(struct sk_buff *skb,
  1617. struct net_device *dev)
  1618. {
  1619. unsigned int last_desc, flits, ndesc;
  1620. const struct skb_shared_info *ssi;
  1621. struct fw_eth_tx_pkt_vm_wr *wr;
  1622. struct tx_sw_desc *sgl_sdesc;
  1623. struct cpl_tx_pkt_core *cpl;
  1624. const struct port_info *pi;
  1625. struct sge_eth_txq *txq;
  1626. struct adapter *adapter;
  1627. int qidx, credits, ret;
  1628. size_t fw_hdr_copy_len;
  1629. unsigned int chip_ver;
  1630. u64 cntrl, *end;
  1631. u32 wr_mid;
  1632. /* The chip minimum packet length is 10 octets but the firmware
  1633. * command that we are using requires that we copy the Ethernet header
  1634. * (including the VLAN tag) into the header so we reject anything
  1635. * smaller than that ...
  1636. */
  1637. BUILD_BUG_ON(sizeof(wr->firmware) !=
  1638. (sizeof(wr->ethmacdst) + sizeof(wr->ethmacsrc) +
  1639. sizeof(wr->ethtype) + sizeof(wr->vlantci)));
  1640. fw_hdr_copy_len = sizeof(wr->firmware);
  1641. ret = cxgb4_validate_skb(skb, dev, fw_hdr_copy_len);
  1642. if (ret)
  1643. goto out_free;
  1644. /* Figure out which TX Queue we're going to use. */
  1645. pi = netdev_priv(dev);
  1646. adapter = pi->adapter;
  1647. qidx = skb_get_queue_mapping(skb);
  1648. WARN_ON(qidx >= pi->nqsets);
  1649. txq = &adapter->sge.ethtxq[pi->first_qset + qidx];
  1650. /* Take this opportunity to reclaim any TX Descriptors whose DMA
  1651. * transfers have completed.
  1652. */
  1653. reclaim_completed_tx(adapter, &txq->q, -1, true);
  1654. /* Calculate the number of flits and TX Descriptors we're going to
  1655. * need along with how many TX Descriptors will be left over after
  1656. * we inject our Work Request.
  1657. */
  1658. flits = t4vf_calc_tx_flits(skb);
  1659. ndesc = flits_to_desc(flits);
  1660. credits = txq_avail(&txq->q) - ndesc;
  1661. if (unlikely(credits < 0)) {
  1662. /* Not enough room for this packet's Work Request. Stop the
  1663. * TX Queue and return a "busy" condition. The queue will get
  1664. * started later on when the firmware informs us that space
  1665. * has opened up.
  1666. */
  1667. eth_txq_stop(txq);
  1668. dev_err(adapter->pdev_dev,
  1669. "%s: TX ring %u full while queue awake!\n",
  1670. dev->name, qidx);
  1671. return NETDEV_TX_BUSY;
  1672. }
  1673. last_desc = txq->q.pidx + ndesc - 1;
  1674. if (last_desc >= txq->q.size)
  1675. last_desc -= txq->q.size;
  1676. sgl_sdesc = &txq->q.sdesc[last_desc];
  1677. if (!t4vf_is_eth_imm(skb) &&
  1678. unlikely(cxgb4_map_skb(adapter->pdev_dev, skb,
  1679. sgl_sdesc->addr) < 0)) {
  1680. /* We need to map the skb into PCI DMA space (because it can't
  1681. * be in-lined directly into the Work Request) and the mapping
  1682. * operation failed. Record the error and drop the packet.
  1683. */
  1684. memset(sgl_sdesc->addr, 0, sizeof(sgl_sdesc->addr));
  1685. txq->mapping_err++;
  1686. goto out_free;
  1687. }
  1688. chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
  1689. wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
  1690. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  1691. /* After we're done injecting the Work Request for this
  1692. * packet, we'll be below our "stop threshold" so stop the TX
  1693. * Queue now and schedule a request for an SGE Egress Queue
  1694. * Update message. The queue will get started later on when
  1695. * the firmware processes this Work Request and sends us an
  1696. * Egress Queue Status Update message indicating that space
  1697. * has opened up.
  1698. */
  1699. eth_txq_stop(txq);
  1700. if (chip_ver > CHELSIO_T5)
  1701. wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
  1702. }
  1703. /* Start filling in our Work Request. Note that we do _not_ handle
  1704. * the WR Header wrapping around the TX Descriptor Ring. If our
  1705. * maximum header size ever exceeds one TX Descriptor, we'll need to
  1706. * do something else here.
  1707. */
  1708. WARN_ON(DIV_ROUND_UP(T4VF_ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1);
  1709. wr = (void *)&txq->q.desc[txq->q.pidx];
  1710. wr->equiq_to_len16 = cpu_to_be32(wr_mid);
  1711. wr->r3[0] = cpu_to_be32(0);
  1712. wr->r3[1] = cpu_to_be32(0);
  1713. skb_copy_from_linear_data(skb, &wr->firmware, fw_hdr_copy_len);
  1714. end = (u64 *)wr + flits;
  1715. /* If this is a Large Send Offload packet we'll put in an LSO CPL
  1716. * message with an encapsulated TX Packet CPL message. Otherwise we
  1717. * just use a TX Packet CPL message.
  1718. */
  1719. ssi = skb_shinfo(skb);
  1720. if (ssi->gso_size) {
  1721. struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
  1722. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  1723. int l3hdr_len = skb_network_header_len(skb);
  1724. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  1725. wr->op_immdlen =
  1726. cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
  1727. FW_WR_IMMDLEN_V(sizeof(*lso) +
  1728. sizeof(*cpl)));
  1729. /* Fill in the LSO CPL message. */
  1730. lso->lso_ctrl =
  1731. cpu_to_be32(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
  1732. LSO_FIRST_SLICE_F |
  1733. LSO_LAST_SLICE_F |
  1734. LSO_IPV6_V(v6) |
  1735. LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
  1736. LSO_IPHDR_LEN_V(l3hdr_len / 4) |
  1737. LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
  1738. lso->ipid_ofst = cpu_to_be16(0);
  1739. lso->mss = cpu_to_be16(ssi->gso_size);
  1740. lso->seqno_offset = cpu_to_be32(0);
  1741. if (is_t4(adapter->params.chip))
  1742. lso->len = cpu_to_be32(skb->len);
  1743. else
  1744. lso->len = cpu_to_be32(LSO_T5_XFER_SIZE_V(skb->len));
  1745. /* Set up TX Packet CPL pointer, control word and perform
  1746. * accounting.
  1747. */
  1748. cpl = (void *)(lso + 1);
  1749. if (chip_ver <= CHELSIO_T5)
  1750. cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1751. else
  1752. cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1753. cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
  1754. TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  1755. TXPKT_IPHDR_LEN_V(l3hdr_len);
  1756. txq->tso++;
  1757. txq->tx_cso += ssi->gso_segs;
  1758. } else {
  1759. int len;
  1760. len = (t4vf_is_eth_imm(skb)
  1761. ? skb->len + sizeof(*cpl)
  1762. : sizeof(*cpl));
  1763. wr->op_immdlen =
  1764. cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
  1765. FW_WR_IMMDLEN_V(len));
  1766. /* Set up TX Packet CPL pointer, control word and perform
  1767. * accounting.
  1768. */
  1769. cpl = (void *)(wr + 1);
  1770. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1771. cntrl = hwcsum(adapter->params.chip, skb) |
  1772. TXPKT_IPCSUM_DIS_F;
  1773. txq->tx_cso++;
  1774. } else {
  1775. cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
  1776. }
  1777. }
  1778. /* If there's a VLAN tag present, add that to the list of things to
  1779. * do in this Work Request.
  1780. */
  1781. if (skb_vlan_tag_present(skb)) {
  1782. txq->vlan_ins++;
  1783. cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
  1784. }
  1785. /* Fill in the TX Packet CPL message header. */
  1786. cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE_V(CPL_TX_PKT_XT) |
  1787. TXPKT_INTF_V(pi->port_id) |
  1788. TXPKT_PF_V(0));
  1789. cpl->pack = cpu_to_be16(0);
  1790. cpl->len = cpu_to_be16(skb->len);
  1791. cpl->ctrl1 = cpu_to_be64(cntrl);
  1792. /* Fill in the body of the TX Packet CPL message with either in-lined
  1793. * data or a Scatter/Gather List.
  1794. */
  1795. if (t4vf_is_eth_imm(skb)) {
  1796. /* In-line the packet's data and free the skb since we don't
  1797. * need it any longer.
  1798. */
  1799. cxgb4_inline_tx_skb(skb, &txq->q, cpl + 1);
  1800. dev_consume_skb_any(skb);
  1801. } else {
  1802. /* Write the skb's Scatter/Gather list into the TX Packet CPL
  1803. * message and retain a pointer to the skb so we can free it
  1804. * later when its DMA completes. (We store the skb pointer
  1805. * in the Software Descriptor corresponding to the last TX
  1806. * Descriptor used by the Work Request.)
  1807. *
  1808. * The retained skb will be freed when the corresponding TX
  1809. * Descriptors are reclaimed after their DMAs complete.
  1810. * However, this could take quite a while since, in general,
  1811. * the hardware is set up to be lazy about sending DMA
  1812. * completion notifications to us and we mostly perform TX
  1813. * reclaims in the transmit routine.
  1814. *
  1815. * This is good for performamce but means that we rely on new
  1816. * TX packets arriving to run the destructors of completed
  1817. * packets, which open up space in their sockets' send queues.
  1818. * Sometimes we do not get such new packets causing TX to
  1819. * stall. A single UDP transmitter is a good example of this
  1820. * situation. We have a clean up timer that periodically
  1821. * reclaims completed packets but it doesn't run often enough
  1822. * (nor do we want it to) to prevent lengthy stalls. A
  1823. * solution to this problem is to run the destructor early,
  1824. * after the packet is queued but before it's DMAd. A con is
  1825. * that we lie to socket memory accounting, but the amount of
  1826. * extra memory is reasonable (limited by the number of TX
  1827. * descriptors), the packets do actually get freed quickly by
  1828. * new packets almost always, and for protocols like TCP that
  1829. * wait for acks to really free up the data the extra memory
  1830. * is even less. On the positive side we run the destructors
  1831. * on the sending CPU rather than on a potentially different
  1832. * completing CPU, usually a good thing.
  1833. *
  1834. * Run the destructor before telling the DMA engine about the
  1835. * packet to make sure it doesn't complete and get freed
  1836. * prematurely.
  1837. */
  1838. struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1);
  1839. struct sge_txq *tq = &txq->q;
  1840. /* If the Work Request header was an exact multiple of our TX
  1841. * Descriptor length, then it's possible that the starting SGL
  1842. * pointer lines up exactly with the end of our TX Descriptor
  1843. * ring. If that's the case, wrap around to the beginning
  1844. * here ...
  1845. */
  1846. if (unlikely((void *)sgl == (void *)tq->stat)) {
  1847. sgl = (void *)tq->desc;
  1848. end = (void *)((void *)tq->desc +
  1849. ((void *)end - (void *)tq->stat));
  1850. }
  1851. cxgb4_write_sgl(skb, tq, sgl, end, 0, sgl_sdesc->addr);
  1852. skb_orphan(skb);
  1853. sgl_sdesc->skb = skb;
  1854. }
  1855. /* Advance our internal TX Queue state, tell the hardware about
  1856. * the new TX descriptors and return success.
  1857. */
  1858. txq_advance(&txq->q, ndesc);
  1859. cxgb4_ring_tx_db(adapter, &txq->q, ndesc);
  1860. return NETDEV_TX_OK;
  1861. out_free:
  1862. /* An error of some sort happened. Free the TX skb and tell the
  1863. * OS that we've "dealt" with the packet ...
  1864. */
  1865. dev_kfree_skb_any(skb);
  1866. return NETDEV_TX_OK;
  1867. }
  1868. /**
  1869. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  1870. * @q: the SGE control Tx queue
  1871. *
  1872. * This is a variant of cxgb4_reclaim_completed_tx() that is used
  1873. * for Tx queues that send only immediate data (presently just
  1874. * the control queues) and thus do not have any sk_buffs to release.
  1875. */
  1876. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  1877. {
  1878. int hw_cidx = ntohs(READ_ONCE(q->stat->cidx));
  1879. int reclaim = hw_cidx - q->cidx;
  1880. if (reclaim < 0)
  1881. reclaim += q->size;
  1882. q->in_use -= reclaim;
  1883. q->cidx = hw_cidx;
  1884. }
  1885. static inline void eosw_txq_advance_index(u32 *idx, u32 n, u32 max)
  1886. {
  1887. u32 val = *idx + n;
  1888. if (val >= max)
  1889. val -= max;
  1890. *idx = val;
  1891. }
  1892. void cxgb4_eosw_txq_free_desc(struct adapter *adap,
  1893. struct sge_eosw_txq *eosw_txq, u32 ndesc)
  1894. {
  1895. struct tx_sw_desc *d;
  1896. d = &eosw_txq->desc[eosw_txq->last_cidx];
  1897. while (ndesc--) {
  1898. if (d->skb) {
  1899. if (d->addr[0]) {
  1900. unmap_skb(adap->pdev_dev, d->skb, d->addr);
  1901. memset(d->addr, 0, sizeof(d->addr));
  1902. }
  1903. dev_consume_skb_any(d->skb);
  1904. d->skb = NULL;
  1905. }
  1906. eosw_txq_advance_index(&eosw_txq->last_cidx, 1,
  1907. eosw_txq->ndesc);
  1908. d = &eosw_txq->desc[eosw_txq->last_cidx];
  1909. }
  1910. }
  1911. static inline void eosw_txq_advance(struct sge_eosw_txq *eosw_txq, u32 n)
  1912. {
  1913. eosw_txq_advance_index(&eosw_txq->pidx, n, eosw_txq->ndesc);
  1914. eosw_txq->inuse += n;
  1915. }
  1916. static inline int eosw_txq_enqueue(struct sge_eosw_txq *eosw_txq,
  1917. struct sk_buff *skb)
  1918. {
  1919. if (eosw_txq->inuse == eosw_txq->ndesc)
  1920. return -ENOMEM;
  1921. eosw_txq->desc[eosw_txq->pidx].skb = skb;
  1922. return 0;
  1923. }
  1924. static inline struct sk_buff *eosw_txq_peek(struct sge_eosw_txq *eosw_txq)
  1925. {
  1926. return eosw_txq->desc[eosw_txq->last_pidx].skb;
  1927. }
  1928. static inline u8 ethofld_calc_tx_flits(struct adapter *adap,
  1929. struct sk_buff *skb, u32 hdr_len)
  1930. {
  1931. u8 flits, nsgl = 0;
  1932. u32 wrlen;
  1933. wrlen = sizeof(struct fw_eth_tx_eo_wr) + sizeof(struct cpl_tx_pkt_core);
  1934. if (skb_shinfo(skb)->gso_size &&
  1935. !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4))
  1936. wrlen += sizeof(struct cpl_tx_pkt_lso_core);
  1937. wrlen += roundup(hdr_len, 16);
  1938. /* Packet headers + WR + CPLs */
  1939. flits = DIV_ROUND_UP(wrlen, 8);
  1940. if (skb_shinfo(skb)->nr_frags > 0) {
  1941. if (skb_headlen(skb) - hdr_len)
  1942. nsgl = sgl_len(skb_shinfo(skb)->nr_frags + 1);
  1943. else
  1944. nsgl = sgl_len(skb_shinfo(skb)->nr_frags);
  1945. } else if (skb->len - hdr_len) {
  1946. nsgl = sgl_len(1);
  1947. }
  1948. return flits + nsgl;
  1949. }
  1950. static void *write_eo_wr(struct adapter *adap, struct sge_eosw_txq *eosw_txq,
  1951. struct sk_buff *skb, struct fw_eth_tx_eo_wr *wr,
  1952. u32 hdr_len, u32 wrlen)
  1953. {
  1954. const struct skb_shared_info *ssi = skb_shinfo(skb);
  1955. struct cpl_tx_pkt_core *cpl;
  1956. u32 immd_len, wrlen16;
  1957. bool compl = false;
  1958. u8 ver, proto;
  1959. ver = ip_hdr(skb)->version;
  1960. proto = (ver == 6) ? ipv6_hdr(skb)->nexthdr : ip_hdr(skb)->protocol;
  1961. wrlen16 = DIV_ROUND_UP(wrlen, 16);
  1962. immd_len = sizeof(struct cpl_tx_pkt_core);
  1963. if (skb_shinfo(skb)->gso_size &&
  1964. !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4))
  1965. immd_len += sizeof(struct cpl_tx_pkt_lso_core);
  1966. immd_len += hdr_len;
  1967. if (!eosw_txq->ncompl ||
  1968. (eosw_txq->last_compl + wrlen16) >=
  1969. (adap->params.ofldq_wr_cred / 2)) {
  1970. compl = true;
  1971. eosw_txq->ncompl++;
  1972. eosw_txq->last_compl = 0;
  1973. }
  1974. wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_EO_WR) |
  1975. FW_ETH_TX_EO_WR_IMMDLEN_V(immd_len) |
  1976. FW_WR_COMPL_V(compl));
  1977. wr->equiq_to_len16 = cpu_to_be32(FW_WR_LEN16_V(wrlen16) |
  1978. FW_WR_FLOWID_V(eosw_txq->hwtid));
  1979. wr->r3 = 0;
  1980. if (proto == IPPROTO_UDP) {
  1981. cpl = write_eo_udp_wr(skb, wr, hdr_len);
  1982. } else {
  1983. wr->u.tcpseg.type = FW_ETH_TX_EO_TYPE_TCPSEG;
  1984. wr->u.tcpseg.ethlen = skb_network_offset(skb);
  1985. wr->u.tcpseg.iplen = cpu_to_be16(skb_network_header_len(skb));
  1986. wr->u.tcpseg.tcplen = tcp_hdrlen(skb);
  1987. wr->u.tcpseg.tsclk_tsoff = 0;
  1988. wr->u.tcpseg.r4 = 0;
  1989. wr->u.tcpseg.r5 = 0;
  1990. wr->u.tcpseg.plen = cpu_to_be32(skb->len - hdr_len);
  1991. if (ssi->gso_size) {
  1992. struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
  1993. wr->u.tcpseg.mss = cpu_to_be16(ssi->gso_size);
  1994. cpl = write_tso_wr(adap, skb, lso);
  1995. } else {
  1996. wr->u.tcpseg.mss = cpu_to_be16(0xffff);
  1997. cpl = (void *)(wr + 1);
  1998. }
  1999. }
  2000. eosw_txq->cred -= wrlen16;
  2001. eosw_txq->last_compl += wrlen16;
  2002. return cpl;
  2003. }
  2004. static int ethofld_hard_xmit(struct net_device *dev,
  2005. struct sge_eosw_txq *eosw_txq)
  2006. {
  2007. struct port_info *pi = netdev2pinfo(dev);
  2008. struct adapter *adap = netdev2adap(dev);
  2009. u32 wrlen, wrlen16, hdr_len, data_len;
  2010. enum sge_eosw_state next_state;
  2011. u64 cntrl, *start, *end, *sgl;
  2012. struct sge_eohw_txq *eohw_txq;
  2013. struct cpl_tx_pkt_core *cpl;
  2014. struct fw_eth_tx_eo_wr *wr;
  2015. bool skip_eotx_wr = false;
  2016. struct tx_sw_desc *d;
  2017. struct sk_buff *skb;
  2018. int left, ret = 0;
  2019. u8 flits, ndesc;
  2020. eohw_txq = &adap->sge.eohw_txq[eosw_txq->hwqid];
  2021. spin_lock(&eohw_txq->lock);
  2022. reclaim_completed_tx_imm(&eohw_txq->q);
  2023. d = &eosw_txq->desc[eosw_txq->last_pidx];
  2024. skb = d->skb;
  2025. skb_tx_timestamp(skb);
  2026. wr = (struct fw_eth_tx_eo_wr *)&eohw_txq->q.desc[eohw_txq->q.pidx];
  2027. if (unlikely(eosw_txq->state != CXGB4_EO_STATE_ACTIVE &&
  2028. eosw_txq->last_pidx == eosw_txq->flowc_idx)) {
  2029. hdr_len = skb->len;
  2030. data_len = 0;
  2031. flits = DIV_ROUND_UP(hdr_len, 8);
  2032. if (eosw_txq->state == CXGB4_EO_STATE_FLOWC_OPEN_SEND)
  2033. next_state = CXGB4_EO_STATE_FLOWC_OPEN_REPLY;
  2034. else
  2035. next_state = CXGB4_EO_STATE_FLOWC_CLOSE_REPLY;
  2036. skip_eotx_wr = true;
  2037. } else {
  2038. hdr_len = eth_get_headlen(dev, skb->data, skb_headlen(skb));
  2039. data_len = skb->len - hdr_len;
  2040. flits = ethofld_calc_tx_flits(adap, skb, hdr_len);
  2041. }
  2042. ndesc = flits_to_desc(flits);
  2043. wrlen = flits * 8;
  2044. wrlen16 = DIV_ROUND_UP(wrlen, 16);
  2045. left = txq_avail(&eohw_txq->q) - ndesc;
  2046. /* If there are no descriptors left in hardware queues or no
  2047. * CPL credits left in software queues, then wait for them
  2048. * to come back and retry again. Note that we always request
  2049. * for credits update via interrupt for every half credits
  2050. * consumed. So, the interrupt will eventually restore the
  2051. * credits and invoke the Tx path again.
  2052. */
  2053. if (unlikely(left < 0 || wrlen16 > eosw_txq->cred)) {
  2054. ret = -ENOMEM;
  2055. goto out_unlock;
  2056. }
  2057. if (unlikely(skip_eotx_wr)) {
  2058. start = (u64 *)wr;
  2059. eosw_txq->state = next_state;
  2060. eosw_txq->cred -= wrlen16;
  2061. eosw_txq->ncompl++;
  2062. eosw_txq->last_compl = 0;
  2063. goto write_wr_headers;
  2064. }
  2065. cpl = write_eo_wr(adap, eosw_txq, skb, wr, hdr_len, wrlen);
  2066. cntrl = hwcsum(adap->params.chip, skb);
  2067. if (skb_vlan_tag_present(skb))
  2068. cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
  2069. cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE_V(CPL_TX_PKT_XT) |
  2070. TXPKT_INTF_V(pi->tx_chan) |
  2071. TXPKT_PF_V(adap->pf));
  2072. cpl->pack = 0;
  2073. cpl->len = cpu_to_be16(skb->len);
  2074. cpl->ctrl1 = cpu_to_be64(cntrl);
  2075. start = (u64 *)(cpl + 1);
  2076. write_wr_headers:
  2077. sgl = (u64 *)inline_tx_skb_header(skb, &eohw_txq->q, (void *)start,
  2078. hdr_len);
  2079. if (data_len) {
  2080. ret = cxgb4_map_skb(adap->pdev_dev, skb, d->addr);
  2081. if (unlikely(ret)) {
  2082. memset(d->addr, 0, sizeof(d->addr));
  2083. eohw_txq->mapping_err++;
  2084. goto out_unlock;
  2085. }
  2086. end = (u64 *)wr + flits;
  2087. if (unlikely(start > sgl)) {
  2088. left = (u8 *)end - (u8 *)eohw_txq->q.stat;
  2089. end = (void *)eohw_txq->q.desc + left;
  2090. }
  2091. if (unlikely((u8 *)sgl >= (u8 *)eohw_txq->q.stat)) {
  2092. /* If current position is already at the end of the
  2093. * txq, reset the current to point to start of the queue
  2094. * and update the end ptr as well.
  2095. */
  2096. left = (u8 *)end - (u8 *)eohw_txq->q.stat;
  2097. end = (void *)eohw_txq->q.desc + left;
  2098. sgl = (void *)eohw_txq->q.desc;
  2099. }
  2100. cxgb4_write_sgl(skb, &eohw_txq->q, (void *)sgl, end, hdr_len,
  2101. d->addr);
  2102. }
  2103. if (skb_shinfo(skb)->gso_size) {
  2104. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
  2105. eohw_txq->uso++;
  2106. else
  2107. eohw_txq->tso++;
  2108. eohw_txq->tx_cso += skb_shinfo(skb)->gso_segs;
  2109. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2110. eohw_txq->tx_cso++;
  2111. }
  2112. if (skb_vlan_tag_present(skb))
  2113. eohw_txq->vlan_ins++;
  2114. txq_advance(&eohw_txq->q, ndesc);
  2115. cxgb4_ring_tx_db(adap, &eohw_txq->q, ndesc);
  2116. eosw_txq_advance_index(&eosw_txq->last_pidx, 1, eosw_txq->ndesc);
  2117. out_unlock:
  2118. spin_unlock(&eohw_txq->lock);
  2119. return ret;
  2120. }
  2121. static void ethofld_xmit(struct net_device *dev, struct sge_eosw_txq *eosw_txq)
  2122. {
  2123. struct sk_buff *skb;
  2124. int pktcount, ret;
  2125. switch (eosw_txq->state) {
  2126. case CXGB4_EO_STATE_ACTIVE:
  2127. case CXGB4_EO_STATE_FLOWC_OPEN_SEND:
  2128. case CXGB4_EO_STATE_FLOWC_CLOSE_SEND:
  2129. pktcount = eosw_txq->pidx - eosw_txq->last_pidx;
  2130. if (pktcount < 0)
  2131. pktcount += eosw_txq->ndesc;
  2132. break;
  2133. case CXGB4_EO_STATE_FLOWC_OPEN_REPLY:
  2134. case CXGB4_EO_STATE_FLOWC_CLOSE_REPLY:
  2135. case CXGB4_EO_STATE_CLOSED:
  2136. default:
  2137. return;
  2138. }
  2139. while (pktcount--) {
  2140. skb = eosw_txq_peek(eosw_txq);
  2141. if (!skb) {
  2142. eosw_txq_advance_index(&eosw_txq->last_pidx, 1,
  2143. eosw_txq->ndesc);
  2144. continue;
  2145. }
  2146. ret = ethofld_hard_xmit(dev, eosw_txq);
  2147. if (ret)
  2148. break;
  2149. }
  2150. }
  2151. static netdev_tx_t cxgb4_ethofld_xmit(struct sk_buff *skb,
  2152. struct net_device *dev)
  2153. {
  2154. struct cxgb4_tc_port_mqprio *tc_port_mqprio;
  2155. struct port_info *pi = netdev2pinfo(dev);
  2156. struct adapter *adap = netdev2adap(dev);
  2157. struct sge_eosw_txq *eosw_txq;
  2158. u32 qid;
  2159. int ret;
  2160. ret = cxgb4_validate_skb(skb, dev, ETH_HLEN);
  2161. if (ret)
  2162. goto out_free;
  2163. tc_port_mqprio = &adap->tc_mqprio->port_mqprio[pi->port_id];
  2164. qid = skb_get_queue_mapping(skb) - pi->nqsets;
  2165. eosw_txq = &tc_port_mqprio->eosw_txq[qid];
  2166. spin_lock_bh(&eosw_txq->lock);
  2167. if (eosw_txq->state != CXGB4_EO_STATE_ACTIVE)
  2168. goto out_unlock;
  2169. ret = eosw_txq_enqueue(eosw_txq, skb);
  2170. if (ret)
  2171. goto out_unlock;
  2172. /* SKB is queued for processing until credits are available.
  2173. * So, call the destructor now and we'll free the skb later
  2174. * after it has been successfully transmitted.
  2175. */
  2176. skb_orphan(skb);
  2177. eosw_txq_advance(eosw_txq, 1);
  2178. ethofld_xmit(dev, eosw_txq);
  2179. spin_unlock_bh(&eosw_txq->lock);
  2180. return NETDEV_TX_OK;
  2181. out_unlock:
  2182. spin_unlock_bh(&eosw_txq->lock);
  2183. out_free:
  2184. dev_kfree_skb_any(skb);
  2185. return NETDEV_TX_OK;
  2186. }
  2187. netdev_tx_t t4_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2188. {
  2189. struct port_info *pi = netdev_priv(dev);
  2190. u16 qid = skb_get_queue_mapping(skb);
  2191. if (unlikely(pi->eth_flags & PRIV_FLAG_PORT_TX_VM))
  2192. return cxgb4_vf_eth_xmit(skb, dev);
  2193. if (unlikely(qid >= pi->nqsets))
  2194. return cxgb4_ethofld_xmit(skb, dev);
  2195. if (is_ptp_enabled(skb, dev)) {
  2196. struct adapter *adap = netdev2adap(dev);
  2197. netdev_tx_t ret;
  2198. spin_lock(&adap->ptp_lock);
  2199. ret = cxgb4_eth_xmit(skb, dev);
  2200. spin_unlock(&adap->ptp_lock);
  2201. return ret;
  2202. }
  2203. return cxgb4_eth_xmit(skb, dev);
  2204. }
  2205. static void eosw_txq_flush_pending_skbs(struct sge_eosw_txq *eosw_txq)
  2206. {
  2207. int pktcount = eosw_txq->pidx - eosw_txq->last_pidx;
  2208. int pidx = eosw_txq->pidx;
  2209. struct sk_buff *skb;
  2210. if (!pktcount)
  2211. return;
  2212. if (pktcount < 0)
  2213. pktcount += eosw_txq->ndesc;
  2214. while (pktcount--) {
  2215. pidx--;
  2216. if (pidx < 0)
  2217. pidx += eosw_txq->ndesc;
  2218. skb = eosw_txq->desc[pidx].skb;
  2219. if (skb) {
  2220. dev_consume_skb_any(skb);
  2221. eosw_txq->desc[pidx].skb = NULL;
  2222. eosw_txq->inuse--;
  2223. }
  2224. }
  2225. eosw_txq->pidx = eosw_txq->last_pidx + 1;
  2226. }
  2227. /**
  2228. * cxgb4_ethofld_send_flowc - Send ETHOFLD flowc request to bind eotid to tc.
  2229. * @dev: netdevice
  2230. * @eotid: ETHOFLD tid to bind/unbind
  2231. * @tc: traffic class. If set to FW_SCHED_CLS_NONE, then unbinds the @eotid
  2232. *
  2233. * Send a FLOWC work request to bind an ETHOFLD TID to a traffic class.
  2234. * If @tc is set to FW_SCHED_CLS_NONE, then the @eotid is unbound from
  2235. * a traffic class.
  2236. */
  2237. int cxgb4_ethofld_send_flowc(struct net_device *dev, u32 eotid, u32 tc)
  2238. {
  2239. struct port_info *pi = netdev2pinfo(dev);
  2240. struct adapter *adap = netdev2adap(dev);
  2241. enum sge_eosw_state next_state;
  2242. struct sge_eosw_txq *eosw_txq;
  2243. u32 len, len16, nparams = 6;
  2244. struct fw_flowc_wr *flowc;
  2245. struct eotid_entry *entry;
  2246. struct sge_ofld_rxq *rxq;
  2247. struct sk_buff *skb;
  2248. int ret = 0;
  2249. len = struct_size(flowc, mnemval, nparams);
  2250. len16 = DIV_ROUND_UP(len, 16);
  2251. entry = cxgb4_lookup_eotid(&adap->tids, eotid);
  2252. if (!entry)
  2253. return -ENOMEM;
  2254. eosw_txq = (struct sge_eosw_txq *)entry->data;
  2255. if (!eosw_txq)
  2256. return -ENOMEM;
  2257. if (!(adap->flags & CXGB4_FW_OK)) {
  2258. /* Don't stall caller when access to FW is lost */
  2259. complete(&eosw_txq->completion);
  2260. return -EIO;
  2261. }
  2262. skb = alloc_skb(len, GFP_KERNEL);
  2263. if (!skb)
  2264. return -ENOMEM;
  2265. spin_lock_bh(&eosw_txq->lock);
  2266. if (tc != FW_SCHED_CLS_NONE) {
  2267. if (eosw_txq->state != CXGB4_EO_STATE_CLOSED)
  2268. goto out_free_skb;
  2269. next_state = CXGB4_EO_STATE_FLOWC_OPEN_SEND;
  2270. } else {
  2271. if (eosw_txq->state != CXGB4_EO_STATE_ACTIVE)
  2272. goto out_free_skb;
  2273. next_state = CXGB4_EO_STATE_FLOWC_CLOSE_SEND;
  2274. }
  2275. flowc = __skb_put(skb, len);
  2276. memset(flowc, 0, len);
  2277. rxq = &adap->sge.eohw_rxq[eosw_txq->hwqid];
  2278. flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(len16) |
  2279. FW_WR_FLOWID_V(eosw_txq->hwtid));
  2280. flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
  2281. FW_FLOWC_WR_NPARAMS_V(nparams) |
  2282. FW_WR_COMPL_V(1));
  2283. flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
  2284. flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V(adap->pf));
  2285. flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
  2286. flowc->mnemval[1].val = cpu_to_be32(pi->tx_chan);
  2287. flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
  2288. flowc->mnemval[2].val = cpu_to_be32(pi->tx_chan);
  2289. flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
  2290. flowc->mnemval[3].val = cpu_to_be32(rxq->rspq.abs_id);
  2291. flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
  2292. flowc->mnemval[4].val = cpu_to_be32(tc);
  2293. flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_EOSTATE;
  2294. flowc->mnemval[5].val = cpu_to_be32(tc == FW_SCHED_CLS_NONE ?
  2295. FW_FLOWC_MNEM_EOSTATE_CLOSING :
  2296. FW_FLOWC_MNEM_EOSTATE_ESTABLISHED);
  2297. /* Free up any pending skbs to ensure there's room for
  2298. * termination FLOWC.
  2299. */
  2300. if (tc == FW_SCHED_CLS_NONE)
  2301. eosw_txq_flush_pending_skbs(eosw_txq);
  2302. ret = eosw_txq_enqueue(eosw_txq, skb);
  2303. if (ret)
  2304. goto out_free_skb;
  2305. eosw_txq->state = next_state;
  2306. eosw_txq->flowc_idx = eosw_txq->pidx;
  2307. eosw_txq_advance(eosw_txq, 1);
  2308. ethofld_xmit(dev, eosw_txq);
  2309. spin_unlock_bh(&eosw_txq->lock);
  2310. return 0;
  2311. out_free_skb:
  2312. dev_consume_skb_any(skb);
  2313. spin_unlock_bh(&eosw_txq->lock);
  2314. return ret;
  2315. }
  2316. /**
  2317. * is_imm - check whether a packet can be sent as immediate data
  2318. * @skb: the packet
  2319. *
  2320. * Returns true if a packet can be sent as a WR with immediate data.
  2321. */
  2322. static inline int is_imm(const struct sk_buff *skb)
  2323. {
  2324. return skb->len <= MAX_CTRL_WR_LEN;
  2325. }
  2326. /**
  2327. * ctrlq_check_stop - check if a control queue is full and should stop
  2328. * @q: the queue
  2329. * @wr: most recent WR written to the queue
  2330. *
  2331. * Check if a control queue has become full and should be stopped.
  2332. * We clean up control queue descriptors very lazily, only when we are out.
  2333. * If the queue is still full after reclaiming any completed descriptors
  2334. * we suspend it and have the last WR wake it up.
  2335. */
  2336. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  2337. {
  2338. reclaim_completed_tx_imm(&q->q);
  2339. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  2340. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  2341. q->q.stops++;
  2342. q->full = 1;
  2343. }
  2344. }
  2345. #define CXGB4_SELFTEST_LB_STR "CHELSIO_SELFTEST"
  2346. int cxgb4_selftest_lb_pkt(struct net_device *netdev)
  2347. {
  2348. struct port_info *pi = netdev_priv(netdev);
  2349. struct adapter *adap = pi->adapter;
  2350. struct cxgb4_ethtool_lb_test *lb;
  2351. int ret, i = 0, pkt_len, credits;
  2352. struct fw_eth_tx_pkt_wr *wr;
  2353. struct cpl_tx_pkt_core *cpl;
  2354. u32 ctrl0, ndesc, flits;
  2355. struct sge_eth_txq *q;
  2356. u8 *sgl;
  2357. pkt_len = ETH_HLEN + sizeof(CXGB4_SELFTEST_LB_STR);
  2358. flits = DIV_ROUND_UP(pkt_len + sizeof(*cpl) + sizeof(*wr),
  2359. sizeof(__be64));
  2360. ndesc = flits_to_desc(flits);
  2361. lb = &pi->ethtool_lb;
  2362. lb->loopback = 1;
  2363. q = &adap->sge.ethtxq[pi->first_qset];
  2364. __netif_tx_lock(q->txq, smp_processor_id());
  2365. reclaim_completed_tx(adap, &q->q, -1, true);
  2366. credits = txq_avail(&q->q) - ndesc;
  2367. if (unlikely(credits < 0)) {
  2368. __netif_tx_unlock(q->txq);
  2369. return -ENOMEM;
  2370. }
  2371. wr = (void *)&q->q.desc[q->q.pidx];
  2372. memset(wr, 0, sizeof(struct tx_desc));
  2373. wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
  2374. FW_WR_IMMDLEN_V(pkt_len +
  2375. sizeof(*cpl)));
  2376. wr->equiq_to_len16 = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2)));
  2377. wr->r3 = cpu_to_be64(0);
  2378. cpl = (void *)(wr + 1);
  2379. sgl = (u8 *)(cpl + 1);
  2380. ctrl0 = TXPKT_OPCODE_V(CPL_TX_PKT_XT) | TXPKT_PF_V(adap->pf) |
  2381. TXPKT_INTF_V(pi->tx_chan + 4);
  2382. cpl->ctrl0 = htonl(ctrl0);
  2383. cpl->pack = htons(0);
  2384. cpl->len = htons(pkt_len);
  2385. cpl->ctrl1 = cpu_to_be64(TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F);
  2386. eth_broadcast_addr(sgl);
  2387. i += ETH_ALEN;
  2388. ether_addr_copy(&sgl[i], netdev->dev_addr);
  2389. i += ETH_ALEN;
  2390. snprintf(&sgl[i], sizeof(CXGB4_SELFTEST_LB_STR), "%s",
  2391. CXGB4_SELFTEST_LB_STR);
  2392. init_completion(&lb->completion);
  2393. txq_advance(&q->q, ndesc);
  2394. cxgb4_ring_tx_db(adap, &q->q, ndesc);
  2395. __netif_tx_unlock(q->txq);
  2396. /* wait for the pkt to return */
  2397. ret = wait_for_completion_timeout(&lb->completion, 10 * HZ);
  2398. if (!ret)
  2399. ret = -ETIMEDOUT;
  2400. else
  2401. ret = lb->result;
  2402. lb->loopback = 0;
  2403. return ret;
  2404. }
  2405. /**
  2406. * ctrl_xmit - send a packet through an SGE control Tx queue
  2407. * @q: the control queue
  2408. * @skb: the packet
  2409. *
  2410. * Send a packet through an SGE control Tx queue. Packets sent through
  2411. * a control queue must fit entirely as immediate data.
  2412. */
  2413. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  2414. {
  2415. unsigned int ndesc;
  2416. struct fw_wr_hdr *wr;
  2417. if (unlikely(!is_imm(skb))) {
  2418. WARN_ON(1);
  2419. dev_kfree_skb(skb);
  2420. return NET_XMIT_DROP;
  2421. }
  2422. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  2423. spin_lock(&q->sendq.lock);
  2424. if (unlikely(q->full)) {
  2425. skb->priority = ndesc; /* save for restart */
  2426. __skb_queue_tail(&q->sendq, skb);
  2427. spin_unlock(&q->sendq.lock);
  2428. return NET_XMIT_CN;
  2429. }
  2430. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  2431. cxgb4_inline_tx_skb(skb, &q->q, wr);
  2432. txq_advance(&q->q, ndesc);
  2433. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  2434. ctrlq_check_stop(q, wr);
  2435. cxgb4_ring_tx_db(q->adap, &q->q, ndesc);
  2436. spin_unlock(&q->sendq.lock);
  2437. kfree_skb(skb);
  2438. return NET_XMIT_SUCCESS;
  2439. }
  2440. /**
  2441. * restart_ctrlq - restart a suspended control queue
  2442. * @t: pointer to the tasklet associated with this handler
  2443. *
  2444. * Resumes transmission on a suspended Tx control queue.
  2445. */
  2446. static void restart_ctrlq(struct tasklet_struct *t)
  2447. {
  2448. struct sk_buff *skb;
  2449. unsigned int written = 0;
  2450. struct sge_ctrl_txq *q = from_tasklet(q, t, qresume_tsk);
  2451. spin_lock(&q->sendq.lock);
  2452. reclaim_completed_tx_imm(&q->q);
  2453. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  2454. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  2455. struct fw_wr_hdr *wr;
  2456. unsigned int ndesc = skb->priority; /* previously saved */
  2457. written += ndesc;
  2458. /* Write descriptors and free skbs outside the lock to limit
  2459. * wait times. q->full is still set so new skbs will be queued.
  2460. */
  2461. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  2462. txq_advance(&q->q, ndesc);
  2463. spin_unlock(&q->sendq.lock);
  2464. cxgb4_inline_tx_skb(skb, &q->q, wr);
  2465. kfree_skb(skb);
  2466. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  2467. unsigned long old = q->q.stops;
  2468. ctrlq_check_stop(q, wr);
  2469. if (q->q.stops != old) { /* suspended anew */
  2470. spin_lock(&q->sendq.lock);
  2471. goto ringdb;
  2472. }
  2473. }
  2474. if (written > 16) {
  2475. cxgb4_ring_tx_db(q->adap, &q->q, written);
  2476. written = 0;
  2477. }
  2478. spin_lock(&q->sendq.lock);
  2479. }
  2480. q->full = 0;
  2481. ringdb:
  2482. if (written)
  2483. cxgb4_ring_tx_db(q->adap, &q->q, written);
  2484. spin_unlock(&q->sendq.lock);
  2485. }
  2486. /**
  2487. * t4_mgmt_tx - send a management message
  2488. * @adap: the adapter
  2489. * @skb: the packet containing the management message
  2490. *
  2491. * Send a management message through control queue 0.
  2492. */
  2493. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  2494. {
  2495. int ret;
  2496. local_bh_disable();
  2497. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  2498. local_bh_enable();
  2499. return ret;
  2500. }
  2501. /**
  2502. * is_ofld_imm - check whether a packet can be sent as immediate data
  2503. * @skb: the packet
  2504. *
  2505. * Returns true if a packet can be sent as an offload WR with immediate
  2506. * data.
  2507. * FW_OFLD_TX_DATA_WR limits the payload to 255 bytes due to 8-bit field.
  2508. * However, FW_ULPTX_WR commands have a 256 byte immediate only
  2509. * payload limit.
  2510. */
  2511. static inline int is_ofld_imm(const struct sk_buff *skb)
  2512. {
  2513. struct work_request_hdr *req = (struct work_request_hdr *)skb->data;
  2514. unsigned long opcode = FW_WR_OP_G(ntohl(req->wr_hi));
  2515. if (unlikely(opcode == FW_ULPTX_WR))
  2516. return skb->len <= MAX_IMM_ULPTX_WR_LEN;
  2517. else if (opcode == FW_CRYPTO_LOOKASIDE_WR)
  2518. return skb->len <= SGE_MAX_WR_LEN;
  2519. else
  2520. return skb->len <= MAX_IMM_OFLD_TX_DATA_WR_LEN;
  2521. }
  2522. /**
  2523. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  2524. * @skb: the packet
  2525. *
  2526. * Returns the number of flits needed for the given offload packet.
  2527. * These packets are already fully constructed and no additional headers
  2528. * will be added.
  2529. */
  2530. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  2531. {
  2532. unsigned int flits, cnt;
  2533. if (is_ofld_imm(skb))
  2534. return DIV_ROUND_UP(skb->len, 8);
  2535. flits = skb_transport_offset(skb) / 8U; /* headers */
  2536. cnt = skb_shinfo(skb)->nr_frags;
  2537. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  2538. cnt++;
  2539. return flits + sgl_len(cnt);
  2540. }
  2541. /**
  2542. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  2543. * @q: the queue to stop
  2544. *
  2545. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  2546. * inability to map packets. A periodic timer attempts to restart
  2547. * queues so marked.
  2548. */
  2549. static void txq_stop_maperr(struct sge_uld_txq *q)
  2550. {
  2551. q->mapping_err++;
  2552. q->q.stops++;
  2553. set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
  2554. q->adap->sge.txq_maperr);
  2555. }
  2556. /**
  2557. * ofldtxq_stop - stop an offload Tx queue that has become full
  2558. * @q: the queue to stop
  2559. * @wr: the Work Request causing the queue to become full
  2560. *
  2561. * Stops an offload Tx queue that has become full and modifies the packet
  2562. * being written to request a wakeup.
  2563. */
  2564. static void ofldtxq_stop(struct sge_uld_txq *q, struct fw_wr_hdr *wr)
  2565. {
  2566. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  2567. q->q.stops++;
  2568. q->full = 1;
  2569. }
  2570. /**
  2571. * service_ofldq - service/restart a suspended offload queue
  2572. * @q: the offload queue
  2573. *
  2574. * Services an offload Tx queue by moving packets from its Pending Send
  2575. * Queue to the Hardware TX ring. The function starts and ends with the
  2576. * Send Queue locked, but drops the lock while putting the skb at the
  2577. * head of the Send Queue onto the Hardware TX Ring. Dropping the lock
  2578. * allows more skbs to be added to the Send Queue by other threads.
  2579. * The packet being processed at the head of the Pending Send Queue is
  2580. * left on the queue in case we experience DMA Mapping errors, etc.
  2581. * and need to give up and restart later.
  2582. *
  2583. * service_ofldq() can be thought of as a task which opportunistically
  2584. * uses other threads execution contexts. We use the Offload Queue
  2585. * boolean "service_ofldq_running" to make sure that only one instance
  2586. * is ever running at a time ...
  2587. */
  2588. static void service_ofldq(struct sge_uld_txq *q)
  2589. __must_hold(&q->sendq.lock)
  2590. {
  2591. u64 *pos, *before, *end;
  2592. int credits;
  2593. struct sk_buff *skb;
  2594. struct sge_txq *txq;
  2595. unsigned int left;
  2596. unsigned int written = 0;
  2597. unsigned int flits, ndesc;
  2598. /* If another thread is currently in service_ofldq() processing the
  2599. * Pending Send Queue then there's nothing to do. Otherwise, flag
  2600. * that we're doing the work and continue. Examining/modifying
  2601. * the Offload Queue boolean "service_ofldq_running" must be done
  2602. * while holding the Pending Send Queue Lock.
  2603. */
  2604. if (q->service_ofldq_running)
  2605. return;
  2606. q->service_ofldq_running = true;
  2607. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  2608. /* We drop the lock while we're working with the skb at the
  2609. * head of the Pending Send Queue. This allows more skbs to
  2610. * be added to the Pending Send Queue while we're working on
  2611. * this one. We don't need to lock to guard the TX Ring
  2612. * updates because only one thread of execution is ever
  2613. * allowed into service_ofldq() at a time.
  2614. */
  2615. spin_unlock(&q->sendq.lock);
  2616. cxgb4_reclaim_completed_tx(q->adap, &q->q, false);
  2617. flits = skb->priority; /* previously saved */
  2618. ndesc = flits_to_desc(flits);
  2619. credits = txq_avail(&q->q) - ndesc;
  2620. BUG_ON(credits < 0);
  2621. if (unlikely(credits < TXQ_STOP_THRES))
  2622. ofldtxq_stop(q, (struct fw_wr_hdr *)skb->data);
  2623. pos = (u64 *)&q->q.desc[q->q.pidx];
  2624. if (is_ofld_imm(skb))
  2625. cxgb4_inline_tx_skb(skb, &q->q, pos);
  2626. else if (cxgb4_map_skb(q->adap->pdev_dev, skb,
  2627. (dma_addr_t *)skb->head)) {
  2628. txq_stop_maperr(q);
  2629. spin_lock(&q->sendq.lock);
  2630. break;
  2631. } else {
  2632. int last_desc, hdr_len = skb_transport_offset(skb);
  2633. /* The WR headers may not fit within one descriptor.
  2634. * So we need to deal with wrap-around here.
  2635. */
  2636. before = (u64 *)pos;
  2637. end = (u64 *)pos + flits;
  2638. txq = &q->q;
  2639. pos = (void *)inline_tx_skb_header(skb, &q->q,
  2640. (void *)pos,
  2641. hdr_len);
  2642. if (before > (u64 *)pos) {
  2643. left = (u8 *)end - (u8 *)txq->stat;
  2644. end = (void *)txq->desc + left;
  2645. }
  2646. /* If current position is already at the end of the
  2647. * ofld queue, reset the current to point to
  2648. * start of the queue and update the end ptr as well.
  2649. */
  2650. if (pos == (u64 *)txq->stat) {
  2651. left = (u8 *)end - (u8 *)txq->stat;
  2652. end = (void *)txq->desc + left;
  2653. pos = (void *)txq->desc;
  2654. }
  2655. cxgb4_write_sgl(skb, &q->q, (void *)pos,
  2656. end, hdr_len,
  2657. (dma_addr_t *)skb->head);
  2658. #ifdef CONFIG_NEED_DMA_MAP_STATE
  2659. skb->dev = q->adap->port[0];
  2660. skb->destructor = deferred_unmap_destructor;
  2661. #endif
  2662. last_desc = q->q.pidx + ndesc - 1;
  2663. if (last_desc >= q->q.size)
  2664. last_desc -= q->q.size;
  2665. q->q.sdesc[last_desc].skb = skb;
  2666. }
  2667. txq_advance(&q->q, ndesc);
  2668. written += ndesc;
  2669. if (unlikely(written > 32)) {
  2670. cxgb4_ring_tx_db(q->adap, &q->q, written);
  2671. written = 0;
  2672. }
  2673. /* Reacquire the Pending Send Queue Lock so we can unlink the
  2674. * skb we've just successfully transferred to the TX Ring and
  2675. * loop for the next skb which may be at the head of the
  2676. * Pending Send Queue.
  2677. */
  2678. spin_lock(&q->sendq.lock);
  2679. __skb_unlink(skb, &q->sendq);
  2680. if (is_ofld_imm(skb))
  2681. kfree_skb(skb);
  2682. }
  2683. if (likely(written))
  2684. cxgb4_ring_tx_db(q->adap, &q->q, written);
  2685. /*Indicate that no thread is processing the Pending Send Queue
  2686. * currently.
  2687. */
  2688. q->service_ofldq_running = false;
  2689. }
  2690. /**
  2691. * ofld_xmit - send a packet through an offload queue
  2692. * @q: the Tx offload queue
  2693. * @skb: the packet
  2694. *
  2695. * Send an offload packet through an SGE offload queue.
  2696. */
  2697. static int ofld_xmit(struct sge_uld_txq *q, struct sk_buff *skb)
  2698. {
  2699. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  2700. spin_lock(&q->sendq.lock);
  2701. /* Queue the new skb onto the Offload Queue's Pending Send Queue. If
  2702. * that results in this new skb being the only one on the queue, start
  2703. * servicing it. If there are other skbs already on the list, then
  2704. * either the queue is currently being processed or it's been stopped
  2705. * for some reason and it'll be restarted at a later time. Restart
  2706. * paths are triggered by events like experiencing a DMA Mapping Error
  2707. * or filling the Hardware TX Ring.
  2708. */
  2709. __skb_queue_tail(&q->sendq, skb);
  2710. if (q->sendq.qlen == 1)
  2711. service_ofldq(q);
  2712. spin_unlock(&q->sendq.lock);
  2713. return NET_XMIT_SUCCESS;
  2714. }
  2715. /**
  2716. * restart_ofldq - restart a suspended offload queue
  2717. * @t: pointer to the tasklet associated with this handler
  2718. *
  2719. * Resumes transmission on a suspended Tx offload queue.
  2720. */
  2721. static void restart_ofldq(struct tasklet_struct *t)
  2722. {
  2723. struct sge_uld_txq *q = from_tasklet(q, t, qresume_tsk);
  2724. spin_lock(&q->sendq.lock);
  2725. q->full = 0; /* the queue actually is completely empty now */
  2726. service_ofldq(q);
  2727. spin_unlock(&q->sendq.lock);
  2728. }
  2729. /**
  2730. * skb_txq - return the Tx queue an offload packet should use
  2731. * @skb: the packet
  2732. *
  2733. * Returns the Tx queue an offload packet should use as indicated by bits
  2734. * 1-15 in the packet's queue_mapping.
  2735. */
  2736. static inline unsigned int skb_txq(const struct sk_buff *skb)
  2737. {
  2738. return skb->queue_mapping >> 1;
  2739. }
  2740. /**
  2741. * is_ctrl_pkt - return whether an offload packet is a control packet
  2742. * @skb: the packet
  2743. *
  2744. * Returns whether an offload packet should use an OFLD or a CTRL
  2745. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  2746. */
  2747. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  2748. {
  2749. return skb->queue_mapping & 1;
  2750. }
  2751. static inline int uld_send(struct adapter *adap, struct sk_buff *skb,
  2752. unsigned int tx_uld_type)
  2753. {
  2754. struct sge_uld_txq_info *txq_info;
  2755. struct sge_uld_txq *txq;
  2756. unsigned int idx = skb_txq(skb);
  2757. if (unlikely(is_ctrl_pkt(skb))) {
  2758. /* Single ctrl queue is a requirement for LE workaround path */
  2759. if (adap->tids.nsftids)
  2760. idx = 0;
  2761. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  2762. }
  2763. txq_info = adap->sge.uld_txq_info[tx_uld_type];
  2764. if (unlikely(!txq_info)) {
  2765. WARN_ON(true);
  2766. kfree_skb(skb);
  2767. return NET_XMIT_DROP;
  2768. }
  2769. txq = &txq_info->uldtxq[idx];
  2770. return ofld_xmit(txq, skb);
  2771. }
  2772. /**
  2773. * t4_ofld_send - send an offload packet
  2774. * @adap: the adapter
  2775. * @skb: the packet
  2776. *
  2777. * Sends an offload packet. We use the packet queue_mapping to select the
  2778. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  2779. * should be sent as regular or control, bits 1-15 select the queue.
  2780. */
  2781. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  2782. {
  2783. int ret;
  2784. local_bh_disable();
  2785. ret = uld_send(adap, skb, CXGB4_TX_OFLD);
  2786. local_bh_enable();
  2787. return ret;
  2788. }
  2789. /**
  2790. * cxgb4_ofld_send - send an offload packet
  2791. * @dev: the net device
  2792. * @skb: the packet
  2793. *
  2794. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  2795. * intended for ULDs.
  2796. */
  2797. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  2798. {
  2799. return t4_ofld_send(netdev2adap(dev), skb);
  2800. }
  2801. EXPORT_SYMBOL(cxgb4_ofld_send);
  2802. static void *inline_tx_header(const void *src,
  2803. const struct sge_txq *q,
  2804. void *pos, int length)
  2805. {
  2806. int left = (void *)q->stat - pos;
  2807. u64 *p;
  2808. if (likely(length <= left)) {
  2809. memcpy(pos, src, length);
  2810. pos += length;
  2811. } else {
  2812. memcpy(pos, src, left);
  2813. memcpy(q->desc, src + left, length - left);
  2814. pos = (void *)q->desc + (length - left);
  2815. }
  2816. /* 0-pad to multiple of 16 */
  2817. p = PTR_ALIGN(pos, 8);
  2818. if ((uintptr_t)p & 8) {
  2819. *p = 0;
  2820. return p + 1;
  2821. }
  2822. return p;
  2823. }
  2824. /**
  2825. * ofld_xmit_direct - copy a WR into offload queue
  2826. * @q: the Tx offload queue
  2827. * @src: location of WR
  2828. * @len: WR length
  2829. *
  2830. * Copy an immediate WR into an uncontended SGE offload queue.
  2831. */
  2832. static int ofld_xmit_direct(struct sge_uld_txq *q, const void *src,
  2833. unsigned int len)
  2834. {
  2835. unsigned int ndesc;
  2836. int credits;
  2837. u64 *pos;
  2838. /* Use the lower limit as the cut-off */
  2839. if (len > MAX_IMM_OFLD_TX_DATA_WR_LEN) {
  2840. WARN_ON(1);
  2841. return NET_XMIT_DROP;
  2842. }
  2843. /* Don't return NET_XMIT_CN here as the current
  2844. * implementation doesn't queue the request
  2845. * using an skb when the following conditions not met
  2846. */
  2847. if (!spin_trylock(&q->sendq.lock))
  2848. return NET_XMIT_DROP;
  2849. if (q->full || !skb_queue_empty(&q->sendq) ||
  2850. q->service_ofldq_running) {
  2851. spin_unlock(&q->sendq.lock);
  2852. return NET_XMIT_DROP;
  2853. }
  2854. ndesc = flits_to_desc(DIV_ROUND_UP(len, 8));
  2855. credits = txq_avail(&q->q) - ndesc;
  2856. pos = (u64 *)&q->q.desc[q->q.pidx];
  2857. /* ofldtxq_stop modifies WR header in-situ */
  2858. inline_tx_header(src, &q->q, pos, len);
  2859. if (unlikely(credits < TXQ_STOP_THRES))
  2860. ofldtxq_stop(q, (struct fw_wr_hdr *)pos);
  2861. txq_advance(&q->q, ndesc);
  2862. cxgb4_ring_tx_db(q->adap, &q->q, ndesc);
  2863. spin_unlock(&q->sendq.lock);
  2864. return NET_XMIT_SUCCESS;
  2865. }
  2866. int cxgb4_immdata_send(struct net_device *dev, unsigned int idx,
  2867. const void *src, unsigned int len)
  2868. {
  2869. struct sge_uld_txq_info *txq_info;
  2870. struct sge_uld_txq *txq;
  2871. struct adapter *adap;
  2872. int ret;
  2873. adap = netdev2adap(dev);
  2874. local_bh_disable();
  2875. txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
  2876. if (unlikely(!txq_info)) {
  2877. WARN_ON(true);
  2878. local_bh_enable();
  2879. return NET_XMIT_DROP;
  2880. }
  2881. txq = &txq_info->uldtxq[idx];
  2882. ret = ofld_xmit_direct(txq, src, len);
  2883. local_bh_enable();
  2884. return net_xmit_eval(ret);
  2885. }
  2886. EXPORT_SYMBOL(cxgb4_immdata_send);
  2887. /**
  2888. * t4_crypto_send - send crypto packet
  2889. * @adap: the adapter
  2890. * @skb: the packet
  2891. *
  2892. * Sends crypto packet. We use the packet queue_mapping to select the
  2893. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  2894. * should be sent as regular or control, bits 1-15 select the queue.
  2895. */
  2896. static int t4_crypto_send(struct adapter *adap, struct sk_buff *skb)
  2897. {
  2898. int ret;
  2899. local_bh_disable();
  2900. ret = uld_send(adap, skb, CXGB4_TX_CRYPTO);
  2901. local_bh_enable();
  2902. return ret;
  2903. }
  2904. /**
  2905. * cxgb4_crypto_send - send crypto packet
  2906. * @dev: the net device
  2907. * @skb: the packet
  2908. *
  2909. * Sends crypto packet. This is an exported version of @t4_crypto_send,
  2910. * intended for ULDs.
  2911. */
  2912. int cxgb4_crypto_send(struct net_device *dev, struct sk_buff *skb)
  2913. {
  2914. return t4_crypto_send(netdev2adap(dev), skb);
  2915. }
  2916. EXPORT_SYMBOL(cxgb4_crypto_send);
  2917. static inline void copy_frags(struct sk_buff *skb,
  2918. const struct pkt_gl *gl, unsigned int offset)
  2919. {
  2920. int i;
  2921. /* usually there's just one frag */
  2922. __skb_fill_page_desc(skb, 0, gl->frags[0].page,
  2923. gl->frags[0].offset + offset,
  2924. gl->frags[0].size - offset);
  2925. skb_shinfo(skb)->nr_frags = gl->nfrags;
  2926. for (i = 1; i < gl->nfrags; i++)
  2927. __skb_fill_page_desc(skb, i, gl->frags[i].page,
  2928. gl->frags[i].offset,
  2929. gl->frags[i].size);
  2930. /* get a reference to the last page, we don't own it */
  2931. get_page(gl->frags[gl->nfrags - 1].page);
  2932. }
  2933. /**
  2934. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  2935. * @gl: the gather list
  2936. * @skb_len: size of sk_buff main body if it carries fragments
  2937. * @pull_len: amount of data to move to the sk_buff's main body
  2938. *
  2939. * Builds an sk_buff from the given packet gather list. Returns the
  2940. * sk_buff or %NULL if sk_buff allocation failed.
  2941. */
  2942. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  2943. unsigned int skb_len, unsigned int pull_len)
  2944. {
  2945. struct sk_buff *skb;
  2946. /*
  2947. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  2948. * size, which is expected since buffers are at least PAGE_SIZEd.
  2949. * In this case packets up to RX_COPY_THRES have only one fragment.
  2950. */
  2951. if (gl->tot_len <= RX_COPY_THRES) {
  2952. skb = dev_alloc_skb(gl->tot_len);
  2953. if (unlikely(!skb))
  2954. goto out;
  2955. __skb_put(skb, gl->tot_len);
  2956. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  2957. } else {
  2958. skb = dev_alloc_skb(skb_len);
  2959. if (unlikely(!skb))
  2960. goto out;
  2961. __skb_put(skb, pull_len);
  2962. skb_copy_to_linear_data(skb, gl->va, pull_len);
  2963. copy_frags(skb, gl, pull_len);
  2964. skb->len = gl->tot_len;
  2965. skb->data_len = skb->len - pull_len;
  2966. skb->truesize += skb->data_len;
  2967. }
  2968. out: return skb;
  2969. }
  2970. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  2971. /**
  2972. * t4_pktgl_free - free a packet gather list
  2973. * @gl: the gather list
  2974. *
  2975. * Releases the pages of a packet gather list. We do not own the last
  2976. * page on the list and do not free it.
  2977. */
  2978. static void t4_pktgl_free(const struct pkt_gl *gl)
  2979. {
  2980. int n;
  2981. const struct page_frag *p;
  2982. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  2983. put_page(p->page);
  2984. }
  2985. /*
  2986. * Process an MPS trace packet. Give it an unused protocol number so it won't
  2987. * be delivered to anyone and send it to the stack for capture.
  2988. */
  2989. static noinline int handle_trace_pkt(struct adapter *adap,
  2990. const struct pkt_gl *gl)
  2991. {
  2992. struct sk_buff *skb;
  2993. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  2994. if (unlikely(!skb)) {
  2995. t4_pktgl_free(gl);
  2996. return 0;
  2997. }
  2998. if (is_t4(adap->params.chip))
  2999. __skb_pull(skb, sizeof(struct cpl_trace_pkt));
  3000. else
  3001. __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
  3002. skb_reset_mac_header(skb);
  3003. skb->protocol = htons(0xffff);
  3004. skb->dev = adap->port[0];
  3005. netif_receive_skb(skb);
  3006. return 0;
  3007. }
  3008. /**
  3009. * cxgb4_sgetim_to_hwtstamp - convert sge time stamp to hw time stamp
  3010. * @adap: the adapter
  3011. * @hwtstamps: time stamp structure to update
  3012. * @sgetstamp: 60bit iqe timestamp
  3013. *
  3014. * Every ingress queue entry has the 60-bit timestamp, convert that timestamp
  3015. * which is in Core Clock ticks into ktime_t and assign it
  3016. **/
  3017. static void cxgb4_sgetim_to_hwtstamp(struct adapter *adap,
  3018. struct skb_shared_hwtstamps *hwtstamps,
  3019. u64 sgetstamp)
  3020. {
  3021. u64 ns;
  3022. u64 tmp = (sgetstamp * 1000 * 1000 + adap->params.vpd.cclk / 2);
  3023. ns = div_u64(tmp, adap->params.vpd.cclk);
  3024. memset(hwtstamps, 0, sizeof(*hwtstamps));
  3025. hwtstamps->hwtstamp = ns_to_ktime(ns);
  3026. }
  3027. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  3028. const struct cpl_rx_pkt *pkt, unsigned long tnl_hdr_len)
  3029. {
  3030. struct adapter *adapter = rxq->rspq.adap;
  3031. struct sge *s = &adapter->sge;
  3032. struct port_info *pi;
  3033. int ret;
  3034. struct sk_buff *skb;
  3035. skb = napi_get_frags(&rxq->rspq.napi);
  3036. if (unlikely(!skb)) {
  3037. t4_pktgl_free(gl);
  3038. rxq->stats.rx_drops++;
  3039. return;
  3040. }
  3041. copy_frags(skb, gl, s->pktshift);
  3042. if (tnl_hdr_len)
  3043. skb->csum_level = 1;
  3044. skb->len = gl->tot_len - s->pktshift;
  3045. skb->data_len = skb->len;
  3046. skb->truesize += skb->data_len;
  3047. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3048. skb_record_rx_queue(skb, rxq->rspq.idx);
  3049. pi = netdev_priv(skb->dev);
  3050. if (pi->rxtstamp)
  3051. cxgb4_sgetim_to_hwtstamp(adapter, skb_hwtstamps(skb),
  3052. gl->sgetstamp);
  3053. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  3054. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  3055. PKT_HASH_TYPE_L3);
  3056. if (unlikely(pkt->vlan_ex)) {
  3057. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  3058. rxq->stats.vlan_ex++;
  3059. }
  3060. ret = napi_gro_frags(&rxq->rspq.napi);
  3061. if (ret == GRO_HELD)
  3062. rxq->stats.lro_pkts++;
  3063. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  3064. rxq->stats.lro_merged++;
  3065. rxq->stats.pkts++;
  3066. rxq->stats.rx_cso++;
  3067. }
  3068. enum {
  3069. RX_NON_PTP_PKT = 0,
  3070. RX_PTP_PKT_SUC = 1,
  3071. RX_PTP_PKT_ERR = 2
  3072. };
  3073. /**
  3074. * t4_systim_to_hwstamp - read hardware time stamp
  3075. * @adapter: the adapter
  3076. * @skb: the packet
  3077. *
  3078. * Read Time Stamp from MPS packet and insert in skb which
  3079. * is forwarded to PTP application
  3080. */
  3081. static noinline int t4_systim_to_hwstamp(struct adapter *adapter,
  3082. struct sk_buff *skb)
  3083. {
  3084. struct skb_shared_hwtstamps *hwtstamps;
  3085. struct cpl_rx_mps_pkt *cpl = NULL;
  3086. unsigned char *data;
  3087. int offset;
  3088. cpl = (struct cpl_rx_mps_pkt *)skb->data;
  3089. if (!(CPL_RX_MPS_PKT_TYPE_G(ntohl(cpl->op_to_r1_hi)) &
  3090. X_CPL_RX_MPS_PKT_TYPE_PTP))
  3091. return RX_PTP_PKT_ERR;
  3092. data = skb->data + sizeof(*cpl);
  3093. skb_pull(skb, 2 * sizeof(u64) + sizeof(struct cpl_rx_mps_pkt));
  3094. offset = ETH_HLEN + IPV4_HLEN(skb->data) + UDP_HLEN;
  3095. if (skb->len < offset + OFF_PTP_SEQUENCE_ID + sizeof(short))
  3096. return RX_PTP_PKT_ERR;
  3097. hwtstamps = skb_hwtstamps(skb);
  3098. memset(hwtstamps, 0, sizeof(*hwtstamps));
  3099. hwtstamps->hwtstamp = ns_to_ktime(get_unaligned_be64(data));
  3100. return RX_PTP_PKT_SUC;
  3101. }
  3102. /**
  3103. * t4_rx_hststamp - Recv PTP Event Message
  3104. * @adapter: the adapter
  3105. * @rsp: the response queue descriptor holding the RX_PKT message
  3106. * @rxq: the response queue holding the RX_PKT message
  3107. * @skb: the packet
  3108. *
  3109. * PTP enabled and MPS packet, read HW timestamp
  3110. */
  3111. static int t4_rx_hststamp(struct adapter *adapter, const __be64 *rsp,
  3112. struct sge_eth_rxq *rxq, struct sk_buff *skb)
  3113. {
  3114. int ret;
  3115. if (unlikely((*(u8 *)rsp == CPL_RX_MPS_PKT) &&
  3116. !is_t4(adapter->params.chip))) {
  3117. ret = t4_systim_to_hwstamp(adapter, skb);
  3118. if (ret == RX_PTP_PKT_ERR) {
  3119. kfree_skb(skb);
  3120. rxq->stats.rx_drops++;
  3121. }
  3122. return ret;
  3123. }
  3124. return RX_NON_PTP_PKT;
  3125. }
  3126. /**
  3127. * t4_tx_hststamp - Loopback PTP Transmit Event Message
  3128. * @adapter: the adapter
  3129. * @skb: the packet
  3130. * @dev: the ingress net device
  3131. *
  3132. * Read hardware timestamp for the loopback PTP Tx event message
  3133. */
  3134. static int t4_tx_hststamp(struct adapter *adapter, struct sk_buff *skb,
  3135. struct net_device *dev)
  3136. {
  3137. struct port_info *pi = netdev_priv(dev);
  3138. if (!is_t4(adapter->params.chip) && adapter->ptp_tx_skb) {
  3139. cxgb4_ptp_read_hwstamp(adapter, pi);
  3140. kfree_skb(skb);
  3141. return 0;
  3142. }
  3143. return 1;
  3144. }
  3145. /**
  3146. * t4_tx_completion_handler - handle CPL_SGE_EGR_UPDATE messages
  3147. * @rspq: Ethernet RX Response Queue associated with Ethernet TX Queue
  3148. * @rsp: Response Entry pointer into Response Queue
  3149. * @gl: Gather List pointer
  3150. *
  3151. * For adapters which support the SGE Doorbell Queue Timer facility,
  3152. * we configure the Ethernet TX Queues to send CIDX Updates to the
  3153. * Associated Ethernet RX Response Queue with CPL_SGE_EGR_UPDATE
  3154. * messages. This adds a small load to PCIe Link RX bandwidth and,
  3155. * potentially, higher CPU Interrupt load, but allows us to respond
  3156. * much more quickly to the CIDX Updates. This is important for
  3157. * Upper Layer Software which isn't willing to have a large amount
  3158. * of TX Data outstanding before receiving DMA Completions.
  3159. */
  3160. static void t4_tx_completion_handler(struct sge_rspq *rspq,
  3161. const __be64 *rsp,
  3162. const struct pkt_gl *gl)
  3163. {
  3164. u8 opcode = ((const struct rss_header *)rsp)->opcode;
  3165. struct port_info *pi = netdev_priv(rspq->netdev);
  3166. struct adapter *adapter = rspq->adap;
  3167. struct sge *s = &adapter->sge;
  3168. struct sge_eth_txq *txq;
  3169. /* skip RSS header */
  3170. rsp++;
  3171. /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
  3172. */
  3173. if (unlikely(opcode == CPL_FW4_MSG &&
  3174. ((const struct cpl_fw4_msg *)rsp)->type ==
  3175. FW_TYPE_RSSCPL)) {
  3176. rsp++;
  3177. opcode = ((const struct rss_header *)rsp)->opcode;
  3178. rsp++;
  3179. }
  3180. if (unlikely(opcode != CPL_SGE_EGR_UPDATE)) {
  3181. pr_info("%s: unexpected FW4/CPL %#x on Rx queue\n",
  3182. __func__, opcode);
  3183. return;
  3184. }
  3185. txq = &s->ethtxq[pi->first_qset + rspq->idx];
  3186. /* We've got the Hardware Consumer Index Update in the Egress Update
  3187. * message. These Egress Update messages will be our sole CIDX Updates
  3188. * we get since we don't want to chew up PCIe bandwidth for both Ingress
  3189. * Messages and Status Page writes. However, The code which manages
  3190. * reclaiming successfully DMA'ed TX Work Requests uses the CIDX value
  3191. * stored in the Status Page at the end of the TX Queue. It's easiest
  3192. * to simply copy the CIDX Update value from the Egress Update message
  3193. * to the Status Page. Also note that no Endian issues need to be
  3194. * considered here since both are Big Endian and we're just copying
  3195. * bytes consistently ...
  3196. */
  3197. if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) {
  3198. struct cpl_sge_egr_update *egr;
  3199. egr = (struct cpl_sge_egr_update *)rsp;
  3200. WRITE_ONCE(txq->q.stat->cidx, egr->cidx);
  3201. }
  3202. t4_sge_eth_txq_egress_update(adapter, txq, -1);
  3203. }
  3204. static int cxgb4_validate_lb_pkt(struct port_info *pi, const struct pkt_gl *si)
  3205. {
  3206. struct adapter *adap = pi->adapter;
  3207. struct cxgb4_ethtool_lb_test *lb;
  3208. struct sge *s = &adap->sge;
  3209. struct net_device *netdev;
  3210. u8 *data;
  3211. int i;
  3212. netdev = adap->port[pi->port_id];
  3213. lb = &pi->ethtool_lb;
  3214. data = si->va + s->pktshift;
  3215. i = ETH_ALEN;
  3216. if (!ether_addr_equal(data + i, netdev->dev_addr))
  3217. return -1;
  3218. i += ETH_ALEN;
  3219. if (strcmp(&data[i], CXGB4_SELFTEST_LB_STR))
  3220. lb->result = -EIO;
  3221. complete(&lb->completion);
  3222. return 0;
  3223. }
  3224. /**
  3225. * t4_ethrx_handler - process an ingress ethernet packet
  3226. * @q: the response queue that received the packet
  3227. * @rsp: the response queue descriptor holding the RX_PKT message
  3228. * @si: the gather list of packet fragments
  3229. *
  3230. * Process an ingress ethernet packet and deliver it to the stack.
  3231. */
  3232. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  3233. const struct pkt_gl *si)
  3234. {
  3235. bool csum_ok;
  3236. struct sk_buff *skb;
  3237. const struct cpl_rx_pkt *pkt;
  3238. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  3239. struct adapter *adapter = q->adap;
  3240. struct sge *s = &q->adap->sge;
  3241. int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
  3242. CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
  3243. u16 err_vec, tnl_hdr_len = 0;
  3244. struct port_info *pi;
  3245. int ret = 0;
  3246. pi = netdev_priv(q->netdev);
  3247. /* If we're looking at TX Queue CIDX Update, handle that separately
  3248. * and return.
  3249. */
  3250. if (unlikely((*(u8 *)rsp == CPL_FW4_MSG) ||
  3251. (*(u8 *)rsp == CPL_SGE_EGR_UPDATE))) {
  3252. t4_tx_completion_handler(q, rsp, si);
  3253. return 0;
  3254. }
  3255. if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
  3256. return handle_trace_pkt(q->adap, si);
  3257. pkt = (const struct cpl_rx_pkt *)rsp;
  3258. /* Compressed error vector is enabled for T6 only */
  3259. if (q->adap->params.tp.rx_pkt_encap) {
  3260. err_vec = T6_COMPR_RXERR_VEC_G(be16_to_cpu(pkt->err_vec));
  3261. tnl_hdr_len = T6_RX_TNLHDR_LEN_G(ntohs(pkt->err_vec));
  3262. } else {
  3263. err_vec = be16_to_cpu(pkt->err_vec);
  3264. }
  3265. csum_ok = pkt->csum_calc && !err_vec &&
  3266. (q->netdev->features & NETIF_F_RXCSUM);
  3267. if (err_vec)
  3268. rxq->stats.bad_rx_pkts++;
  3269. if (unlikely(pi->ethtool_lb.loopback && pkt->iff >= NCHAN)) {
  3270. ret = cxgb4_validate_lb_pkt(pi, si);
  3271. if (!ret)
  3272. return 0;
  3273. }
  3274. if (((pkt->l2info & htonl(RXF_TCP_F)) ||
  3275. tnl_hdr_len) &&
  3276. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  3277. do_gro(rxq, si, pkt, tnl_hdr_len);
  3278. return 0;
  3279. }
  3280. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  3281. if (unlikely(!skb)) {
  3282. t4_pktgl_free(si);
  3283. rxq->stats.rx_drops++;
  3284. return 0;
  3285. }
  3286. /* Handle PTP Event Rx packet */
  3287. if (unlikely(pi->ptp_enable)) {
  3288. ret = t4_rx_hststamp(adapter, rsp, rxq, skb);
  3289. if (ret == RX_PTP_PKT_ERR)
  3290. return 0;
  3291. }
  3292. if (likely(!ret))
  3293. __skb_pull(skb, s->pktshift); /* remove ethernet header pad */
  3294. /* Handle the PTP Event Tx Loopback packet */
  3295. if (unlikely(pi->ptp_enable && !ret &&
  3296. (pkt->l2info & htonl(RXF_UDP_F)) &&
  3297. cxgb4_ptp_is_ptp_rx(skb))) {
  3298. if (!t4_tx_hststamp(adapter, skb, q->netdev))
  3299. return 0;
  3300. }
  3301. skb->protocol = eth_type_trans(skb, q->netdev);
  3302. skb_record_rx_queue(skb, q->idx);
  3303. if (skb->dev->features & NETIF_F_RXHASH)
  3304. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  3305. PKT_HASH_TYPE_L3);
  3306. rxq->stats.pkts++;
  3307. if (pi->rxtstamp)
  3308. cxgb4_sgetim_to_hwtstamp(q->adap, skb_hwtstamps(skb),
  3309. si->sgetstamp);
  3310. if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
  3311. if (!pkt->ip_frag) {
  3312. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3313. rxq->stats.rx_cso++;
  3314. } else if (pkt->l2info & htonl(RXF_IP_F)) {
  3315. __sum16 c = (__force __sum16)pkt->csum;
  3316. skb->csum = csum_unfold(c);
  3317. if (tnl_hdr_len) {
  3318. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3319. skb->csum_level = 1;
  3320. } else {
  3321. skb->ip_summed = CHECKSUM_COMPLETE;
  3322. }
  3323. rxq->stats.rx_cso++;
  3324. }
  3325. } else {
  3326. skb_checksum_none_assert(skb);
  3327. #ifdef CONFIG_CHELSIO_T4_FCOE
  3328. #define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
  3329. RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
  3330. if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
  3331. if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
  3332. (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
  3333. if (q->adap->params.tp.rx_pkt_encap)
  3334. csum_ok = err_vec &
  3335. T6_COMPR_RXERR_SUM_F;
  3336. else
  3337. csum_ok = err_vec & RXERR_CSUM_F;
  3338. if (!csum_ok)
  3339. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3340. }
  3341. }
  3342. #undef CPL_RX_PKT_FLAGS
  3343. #endif /* CONFIG_CHELSIO_T4_FCOE */
  3344. }
  3345. if (unlikely(pkt->vlan_ex)) {
  3346. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  3347. rxq->stats.vlan_ex++;
  3348. }
  3349. skb_mark_napi_id(skb, &q->napi);
  3350. netif_receive_skb(skb);
  3351. return 0;
  3352. }
  3353. /**
  3354. * restore_rx_bufs - put back a packet's Rx buffers
  3355. * @si: the packet gather list
  3356. * @q: the SGE free list
  3357. * @frags: number of FL buffers to restore
  3358. *
  3359. * Puts back on an FL the Rx buffers associated with @si. The buffers
  3360. * have already been unmapped and are left unmapped, we mark them so to
  3361. * prevent further unmapping attempts.
  3362. *
  3363. * This function undoes a series of @unmap_rx_buf calls when we find out
  3364. * that the current packet can't be processed right away afterall and we
  3365. * need to come back to it later. This is a very rare event and there's
  3366. * no effort to make this particularly efficient.
  3367. */
  3368. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  3369. int frags)
  3370. {
  3371. struct rx_sw_desc *d;
  3372. while (frags--) {
  3373. if (q->cidx == 0)
  3374. q->cidx = q->size - 1;
  3375. else
  3376. q->cidx--;
  3377. d = &q->sdesc[q->cidx];
  3378. d->page = si->frags[frags].page;
  3379. d->dma_addr |= RX_UNMAPPED_BUF;
  3380. q->avail++;
  3381. }
  3382. }
  3383. /**
  3384. * is_new_response - check if a response is newly written
  3385. * @r: the response descriptor
  3386. * @q: the response queue
  3387. *
  3388. * Returns true if a response descriptor contains a yet unprocessed
  3389. * response.
  3390. */
  3391. static inline bool is_new_response(const struct rsp_ctrl *r,
  3392. const struct sge_rspq *q)
  3393. {
  3394. return (r->type_gen >> RSPD_GEN_S) == q->gen;
  3395. }
  3396. /**
  3397. * rspq_next - advance to the next entry in a response queue
  3398. * @q: the queue
  3399. *
  3400. * Updates the state of a response queue to advance it to the next entry.
  3401. */
  3402. static inline void rspq_next(struct sge_rspq *q)
  3403. {
  3404. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  3405. if (unlikely(++q->cidx == q->size)) {
  3406. q->cidx = 0;
  3407. q->gen ^= 1;
  3408. q->cur_desc = q->desc;
  3409. }
  3410. }
  3411. /**
  3412. * process_responses - process responses from an SGE response queue
  3413. * @q: the ingress queue to process
  3414. * @budget: how many responses can be processed in this round
  3415. *
  3416. * Process responses from an SGE response queue up to the supplied budget.
  3417. * Responses include received packets as well as control messages from FW
  3418. * or HW.
  3419. *
  3420. * Additionally choose the interrupt holdoff time for the next interrupt
  3421. * on this queue. If the system is under memory shortage use a fairly
  3422. * long delay to help recovery.
  3423. */
  3424. static int process_responses(struct sge_rspq *q, int budget)
  3425. {
  3426. int ret, rsp_type;
  3427. int budget_left = budget;
  3428. const struct rsp_ctrl *rc;
  3429. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  3430. struct adapter *adapter = q->adap;
  3431. struct sge *s = &adapter->sge;
  3432. while (likely(budget_left)) {
  3433. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  3434. if (!is_new_response(rc, q)) {
  3435. if (q->flush_handler)
  3436. q->flush_handler(q);
  3437. break;
  3438. }
  3439. dma_rmb();
  3440. rsp_type = RSPD_TYPE_G(rc->type_gen);
  3441. if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
  3442. struct page_frag *fp;
  3443. struct pkt_gl si;
  3444. const struct rx_sw_desc *rsd;
  3445. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  3446. if (len & RSPD_NEWBUF_F) {
  3447. if (likely(q->offset > 0)) {
  3448. free_rx_bufs(q->adap, &rxq->fl, 1);
  3449. q->offset = 0;
  3450. }
  3451. len = RSPD_LEN_G(len);
  3452. }
  3453. si.tot_len = len;
  3454. /* gather packet fragments */
  3455. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  3456. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  3457. bufsz = get_buf_size(adapter, rsd);
  3458. fp->page = rsd->page;
  3459. fp->offset = q->offset;
  3460. fp->size = min(bufsz, len);
  3461. len -= fp->size;
  3462. if (!len)
  3463. break;
  3464. unmap_rx_buf(q->adap, &rxq->fl);
  3465. }
  3466. si.sgetstamp = SGE_TIMESTAMP_G(
  3467. be64_to_cpu(rc->last_flit));
  3468. /*
  3469. * Last buffer remains mapped so explicitly make it
  3470. * coherent for CPU access.
  3471. */
  3472. dma_sync_single_for_cpu(q->adap->pdev_dev,
  3473. get_buf_addr(rsd),
  3474. fp->size, DMA_FROM_DEVICE);
  3475. si.va = page_address(si.frags[0].page) +
  3476. si.frags[0].offset;
  3477. prefetch(si.va);
  3478. si.nfrags = frags + 1;
  3479. ret = q->handler(q, q->cur_desc, &si);
  3480. if (likely(ret == 0))
  3481. q->offset += ALIGN(fp->size, s->fl_align);
  3482. else
  3483. restore_rx_bufs(&si, &rxq->fl, frags);
  3484. } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
  3485. ret = q->handler(q, q->cur_desc, NULL);
  3486. } else {
  3487. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  3488. }
  3489. if (unlikely(ret)) {
  3490. /* couldn't process descriptor, back off for recovery */
  3491. q->next_intr_params = QINTR_TIMER_IDX_V(NOMEM_TMR_IDX);
  3492. break;
  3493. }
  3494. rspq_next(q);
  3495. budget_left--;
  3496. }
  3497. if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 16)
  3498. __refill_fl(q->adap, &rxq->fl);
  3499. return budget - budget_left;
  3500. }
  3501. /**
  3502. * napi_rx_handler - the NAPI handler for Rx processing
  3503. * @napi: the napi instance
  3504. * @budget: how many packets we can process in this round
  3505. *
  3506. * Handler for new data events when using NAPI. This does not need any
  3507. * locking or protection from interrupts as data interrupts are off at
  3508. * this point and other adapter interrupts do not interfere (the latter
  3509. * in not a concern at all with MSI-X as non-data interrupts then have
  3510. * a separate handler).
  3511. */
  3512. static int napi_rx_handler(struct napi_struct *napi, int budget)
  3513. {
  3514. unsigned int params;
  3515. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  3516. int work_done;
  3517. u32 val;
  3518. work_done = process_responses(q, budget);
  3519. if (likely(work_done < budget)) {
  3520. int timer_index;
  3521. napi_complete_done(napi, work_done);
  3522. timer_index = QINTR_TIMER_IDX_G(q->next_intr_params);
  3523. if (q->adaptive_rx) {
  3524. if (work_done > max(timer_pkt_quota[timer_index],
  3525. MIN_NAPI_WORK))
  3526. timer_index = (timer_index + 1);
  3527. else
  3528. timer_index = timer_index - 1;
  3529. timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
  3530. q->next_intr_params =
  3531. QINTR_TIMER_IDX_V(timer_index) |
  3532. QINTR_CNT_EN_V(0);
  3533. params = q->next_intr_params;
  3534. } else {
  3535. params = q->next_intr_params;
  3536. q->next_intr_params = q->intr_params;
  3537. }
  3538. } else
  3539. params = QINTR_TIMER_IDX_V(7);
  3540. val = CIDXINC_V(work_done) | SEINTARM_V(params);
  3541. /* If we don't have access to the new User GTS (T5+), use the old
  3542. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  3543. */
  3544. if (unlikely(q->bar2_addr == NULL)) {
  3545. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
  3546. val | INGRESSQID_V((u32)q->cntxt_id));
  3547. } else {
  3548. writel(val | INGRESSQID_V(q->bar2_qid),
  3549. q->bar2_addr + SGE_UDB_GTS);
  3550. wmb();
  3551. }
  3552. return work_done;
  3553. }
  3554. void cxgb4_ethofld_restart(struct tasklet_struct *t)
  3555. {
  3556. struct sge_eosw_txq *eosw_txq = from_tasklet(eosw_txq, t,
  3557. qresume_tsk);
  3558. int pktcount;
  3559. spin_lock(&eosw_txq->lock);
  3560. pktcount = eosw_txq->cidx - eosw_txq->last_cidx;
  3561. if (pktcount < 0)
  3562. pktcount += eosw_txq->ndesc;
  3563. if (pktcount) {
  3564. cxgb4_eosw_txq_free_desc(netdev2adap(eosw_txq->netdev),
  3565. eosw_txq, pktcount);
  3566. eosw_txq->inuse -= pktcount;
  3567. }
  3568. /* There may be some packets waiting for completions. So,
  3569. * attempt to send these packets now.
  3570. */
  3571. ethofld_xmit(eosw_txq->netdev, eosw_txq);
  3572. spin_unlock(&eosw_txq->lock);
  3573. }
  3574. /* cxgb4_ethofld_rx_handler - Process ETHOFLD Tx completions
  3575. * @q: the response queue that received the packet
  3576. * @rsp: the response queue descriptor holding the CPL message
  3577. * @si: the gather list of packet fragments
  3578. *
  3579. * Process a ETHOFLD Tx completion. Increment the cidx here, but
  3580. * free up the descriptors in a tasklet later.
  3581. */
  3582. int cxgb4_ethofld_rx_handler(struct sge_rspq *q, const __be64 *rsp,
  3583. const struct pkt_gl *si)
  3584. {
  3585. u8 opcode = ((const struct rss_header *)rsp)->opcode;
  3586. /* skip RSS header */
  3587. rsp++;
  3588. if (opcode == CPL_FW4_ACK) {
  3589. const struct cpl_fw4_ack *cpl;
  3590. struct sge_eosw_txq *eosw_txq;
  3591. struct eotid_entry *entry;
  3592. struct sk_buff *skb;
  3593. u32 hdr_len, eotid;
  3594. u8 flits, wrlen16;
  3595. int credits;
  3596. cpl = (const struct cpl_fw4_ack *)rsp;
  3597. eotid = CPL_FW4_ACK_FLOWID_G(ntohl(OPCODE_TID(cpl))) -
  3598. q->adap->tids.eotid_base;
  3599. entry = cxgb4_lookup_eotid(&q->adap->tids, eotid);
  3600. if (!entry)
  3601. goto out_done;
  3602. eosw_txq = (struct sge_eosw_txq *)entry->data;
  3603. if (!eosw_txq)
  3604. goto out_done;
  3605. spin_lock(&eosw_txq->lock);
  3606. credits = cpl->credits;
  3607. while (credits > 0) {
  3608. skb = eosw_txq->desc[eosw_txq->cidx].skb;
  3609. if (!skb)
  3610. break;
  3611. if (unlikely((eosw_txq->state ==
  3612. CXGB4_EO_STATE_FLOWC_OPEN_REPLY ||
  3613. eosw_txq->state ==
  3614. CXGB4_EO_STATE_FLOWC_CLOSE_REPLY) &&
  3615. eosw_txq->cidx == eosw_txq->flowc_idx)) {
  3616. flits = DIV_ROUND_UP(skb->len, 8);
  3617. if (eosw_txq->state ==
  3618. CXGB4_EO_STATE_FLOWC_OPEN_REPLY)
  3619. eosw_txq->state = CXGB4_EO_STATE_ACTIVE;
  3620. else
  3621. eosw_txq->state = CXGB4_EO_STATE_CLOSED;
  3622. complete(&eosw_txq->completion);
  3623. } else {
  3624. hdr_len = eth_get_headlen(eosw_txq->netdev,
  3625. skb->data,
  3626. skb_headlen(skb));
  3627. flits = ethofld_calc_tx_flits(q->adap, skb,
  3628. hdr_len);
  3629. }
  3630. eosw_txq_advance_index(&eosw_txq->cidx, 1,
  3631. eosw_txq->ndesc);
  3632. wrlen16 = DIV_ROUND_UP(flits * 8, 16);
  3633. credits -= wrlen16;
  3634. }
  3635. eosw_txq->cred += cpl->credits;
  3636. eosw_txq->ncompl--;
  3637. spin_unlock(&eosw_txq->lock);
  3638. /* Schedule a tasklet to reclaim SKBs and restart ETHOFLD Tx,
  3639. * if there were packets waiting for completion.
  3640. */
  3641. tasklet_schedule(&eosw_txq->qresume_tsk);
  3642. }
  3643. out_done:
  3644. return 0;
  3645. }
  3646. /*
  3647. * The MSI-X interrupt handler for an SGE response queue.
  3648. */
  3649. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  3650. {
  3651. struct sge_rspq *q = cookie;
  3652. napi_schedule(&q->napi);
  3653. return IRQ_HANDLED;
  3654. }
  3655. /*
  3656. * Process the indirect interrupt entries in the interrupt queue and kick off
  3657. * NAPI for each queue that has generated an entry.
  3658. */
  3659. static unsigned int process_intrq(struct adapter *adap)
  3660. {
  3661. unsigned int credits;
  3662. const struct rsp_ctrl *rc;
  3663. struct sge_rspq *q = &adap->sge.intrq;
  3664. u32 val;
  3665. spin_lock(&adap->sge.intrq_lock);
  3666. for (credits = 0; ; credits++) {
  3667. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  3668. if (!is_new_response(rc, q))
  3669. break;
  3670. dma_rmb();
  3671. if (RSPD_TYPE_G(rc->type_gen) == RSPD_TYPE_INTR_X) {
  3672. unsigned int qid = ntohl(rc->pldbuflen_qid);
  3673. qid -= adap->sge.ingr_start;
  3674. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  3675. }
  3676. rspq_next(q);
  3677. }
  3678. val = CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
  3679. /* If we don't have access to the new User GTS (T5+), use the old
  3680. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  3681. */
  3682. if (unlikely(q->bar2_addr == NULL)) {
  3683. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
  3684. val | INGRESSQID_V(q->cntxt_id));
  3685. } else {
  3686. writel(val | INGRESSQID_V(q->bar2_qid),
  3687. q->bar2_addr + SGE_UDB_GTS);
  3688. wmb();
  3689. }
  3690. spin_unlock(&adap->sge.intrq_lock);
  3691. return credits;
  3692. }
  3693. /*
  3694. * The MSI interrupt handler, which handles data events from SGE response queues
  3695. * as well as error and other async events as they all use the same MSI vector.
  3696. */
  3697. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  3698. {
  3699. struct adapter *adap = cookie;
  3700. if (adap->flags & CXGB4_MASTER_PF)
  3701. t4_slow_intr_handler(adap);
  3702. process_intrq(adap);
  3703. return IRQ_HANDLED;
  3704. }
  3705. /*
  3706. * Interrupt handler for legacy INTx interrupts.
  3707. * Handles data events from SGE response queues as well as error and other
  3708. * async events as they all use the same interrupt line.
  3709. */
  3710. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  3711. {
  3712. struct adapter *adap = cookie;
  3713. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
  3714. if (((adap->flags & CXGB4_MASTER_PF) && t4_slow_intr_handler(adap)) |
  3715. process_intrq(adap))
  3716. return IRQ_HANDLED;
  3717. return IRQ_NONE; /* probably shared interrupt */
  3718. }
  3719. /**
  3720. * t4_intr_handler - select the top-level interrupt handler
  3721. * @adap: the adapter
  3722. *
  3723. * Selects the top-level interrupt handler based on the type of interrupts
  3724. * (MSI-X, MSI, or INTx).
  3725. */
  3726. irq_handler_t t4_intr_handler(struct adapter *adap)
  3727. {
  3728. if (adap->flags & CXGB4_USING_MSIX)
  3729. return t4_sge_intr_msix;
  3730. if (adap->flags & CXGB4_USING_MSI)
  3731. return t4_intr_msi;
  3732. return t4_intr_intx;
  3733. }
  3734. static void sge_rx_timer_cb(struct timer_list *t)
  3735. {
  3736. unsigned long m;
  3737. unsigned int i;
  3738. struct adapter *adap = from_timer(adap, t, sge.rx_timer);
  3739. struct sge *s = &adap->sge;
  3740. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  3741. for (m = s->starving_fl[i]; m; m &= m - 1) {
  3742. struct sge_eth_rxq *rxq;
  3743. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  3744. struct sge_fl *fl = s->egr_map[id];
  3745. clear_bit(id, s->starving_fl);
  3746. smp_mb__after_atomic();
  3747. if (fl_starving(adap, fl)) {
  3748. rxq = container_of(fl, struct sge_eth_rxq, fl);
  3749. if (napi_reschedule(&rxq->rspq.napi))
  3750. fl->starving++;
  3751. else
  3752. set_bit(id, s->starving_fl);
  3753. }
  3754. }
  3755. /* The remainder of the SGE RX Timer Callback routine is dedicated to
  3756. * global Master PF activities like checking for chip ingress stalls,
  3757. * etc.
  3758. */
  3759. if (!(adap->flags & CXGB4_MASTER_PF))
  3760. goto done;
  3761. t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
  3762. done:
  3763. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  3764. }
  3765. static void sge_tx_timer_cb(struct timer_list *t)
  3766. {
  3767. struct adapter *adap = from_timer(adap, t, sge.tx_timer);
  3768. struct sge *s = &adap->sge;
  3769. unsigned long m, period;
  3770. unsigned int i, budget;
  3771. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  3772. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  3773. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  3774. struct sge_uld_txq *txq = s->egr_map[id];
  3775. clear_bit(id, s->txq_maperr);
  3776. tasklet_schedule(&txq->qresume_tsk);
  3777. }
  3778. if (!is_t4(adap->params.chip)) {
  3779. struct sge_eth_txq *q = &s->ptptxq;
  3780. int avail;
  3781. spin_lock(&adap->ptp_lock);
  3782. avail = reclaimable(&q->q);
  3783. if (avail) {
  3784. free_tx_desc(adap, &q->q, avail, false);
  3785. q->q.in_use -= avail;
  3786. }
  3787. spin_unlock(&adap->ptp_lock);
  3788. }
  3789. budget = MAX_TIMER_TX_RECLAIM;
  3790. i = s->ethtxq_rover;
  3791. do {
  3792. budget -= t4_sge_eth_txq_egress_update(adap, &s->ethtxq[i],
  3793. budget);
  3794. if (!budget)
  3795. break;
  3796. if (++i >= s->ethqsets)
  3797. i = 0;
  3798. } while (i != s->ethtxq_rover);
  3799. s->ethtxq_rover = i;
  3800. if (budget == 0) {
  3801. /* If we found too many reclaimable packets schedule a timer
  3802. * in the near future to continue where we left off.
  3803. */
  3804. period = 2;
  3805. } else {
  3806. /* We reclaimed all reclaimable TX Descriptors, so reschedule
  3807. * at the normal period.
  3808. */
  3809. period = TX_QCHECK_PERIOD;
  3810. }
  3811. mod_timer(&s->tx_timer, jiffies + period);
  3812. }
  3813. /**
  3814. * bar2_address - return the BAR2 address for an SGE Queue's Registers
  3815. * @adapter: the adapter
  3816. * @qid: the SGE Queue ID
  3817. * @qtype: the SGE Queue Type (Egress or Ingress)
  3818. * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
  3819. *
  3820. * Returns the BAR2 address for the SGE Queue Registers associated with
  3821. * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
  3822. * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
  3823. * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
  3824. * Registers are supported (e.g. the Write Combining Doorbell Buffer).
  3825. */
  3826. static void __iomem *bar2_address(struct adapter *adapter,
  3827. unsigned int qid,
  3828. enum t4_bar2_qtype qtype,
  3829. unsigned int *pbar2_qid)
  3830. {
  3831. u64 bar2_qoffset;
  3832. int ret;
  3833. ret = t4_bar2_sge_qregs(adapter, qid, qtype, 0,
  3834. &bar2_qoffset, pbar2_qid);
  3835. if (ret)
  3836. return NULL;
  3837. return adapter->bar2 + bar2_qoffset;
  3838. }
  3839. /* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
  3840. * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
  3841. */
  3842. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  3843. struct net_device *dev, int intr_idx,
  3844. struct sge_fl *fl, rspq_handler_t hnd,
  3845. rspq_flush_handler_t flush_hnd, int cong)
  3846. {
  3847. int ret, flsz = 0;
  3848. struct fw_iq_cmd c;
  3849. struct sge *s = &adap->sge;
  3850. struct port_info *pi = netdev_priv(dev);
  3851. int relaxed = !(adap->flags & CXGB4_ROOT_NO_RELAXED_ORDERING);
  3852. /* Size needs to be multiple of 16, including status entry. */
  3853. iq->size = roundup(iq->size, 16);
  3854. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  3855. &iq->phys_addr, NULL, 0,
  3856. dev_to_node(adap->pdev_dev));
  3857. if (!iq->desc)
  3858. return -ENOMEM;
  3859. memset(&c, 0, sizeof(c));
  3860. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
  3861. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  3862. FW_IQ_CMD_PFN_V(adap->pf) | FW_IQ_CMD_VFN_V(0));
  3863. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
  3864. FW_LEN16(c));
  3865. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
  3866. FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
  3867. FW_IQ_CMD_IQANDST_V(intr_idx < 0) |
  3868. FW_IQ_CMD_IQANUD_V(UPDATEDELIVERY_INTERRUPT_X) |
  3869. FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
  3870. -intr_idx - 1));
  3871. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
  3872. FW_IQ_CMD_IQGTSMODE_F |
  3873. FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
  3874. FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
  3875. c.iqsize = htons(iq->size);
  3876. c.iqaddr = cpu_to_be64(iq->phys_addr);
  3877. if (cong >= 0)
  3878. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F |
  3879. FW_IQ_CMD_IQTYPE_V(cong ? FW_IQ_IQTYPE_NIC
  3880. : FW_IQ_IQTYPE_OFLD));
  3881. if (fl) {
  3882. unsigned int chip_ver =
  3883. CHELSIO_CHIP_VERSION(adap->params.chip);
  3884. /* Allocate the ring for the hardware free list (with space
  3885. * for its status page) along with the associated software
  3886. * descriptor ring. The free list size needs to be a multiple
  3887. * of the Egress Queue Unit and at least 2 Egress Units larger
  3888. * than the SGE's Egress Congrestion Threshold
  3889. * (fl_starve_thres - 1).
  3890. */
  3891. if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
  3892. fl->size = s->fl_starve_thres - 1 + 2 * 8;
  3893. fl->size = roundup(fl->size, 8);
  3894. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  3895. sizeof(struct rx_sw_desc), &fl->addr,
  3896. &fl->sdesc, s->stat_len,
  3897. dev_to_node(adap->pdev_dev));
  3898. if (!fl->desc)
  3899. goto fl_nomem;
  3900. flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
  3901. c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
  3902. FW_IQ_CMD_FL0FETCHRO_V(relaxed) |
  3903. FW_IQ_CMD_FL0DATARO_V(relaxed) |
  3904. FW_IQ_CMD_FL0PADEN_F);
  3905. if (cong >= 0)
  3906. c.iqns_to_fl0congen |=
  3907. htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
  3908. FW_IQ_CMD_FL0CONGCIF_F |
  3909. FW_IQ_CMD_FL0CONGEN_F);
  3910. /* In T6, for egress queue type FL there is internal overhead
  3911. * of 16B for header going into FLM module. Hence the maximum
  3912. * allowed burst size is 448 bytes. For T4/T5, the hardware
  3913. * doesn't coalesce fetch requests if more than 64 bytes of
  3914. * Free List pointers are provided, so we use a 128-byte Fetch
  3915. * Burst Minimum there (T6 implements coalescing so we can use
  3916. * the smaller 64-byte value there).
  3917. */
  3918. c.fl0dcaen_to_fl0cidxfthresh =
  3919. htons(FW_IQ_CMD_FL0FBMIN_V(chip_ver <= CHELSIO_T5 ?
  3920. FETCHBURSTMIN_128B_X :
  3921. FETCHBURSTMIN_64B_T6_X) |
  3922. FW_IQ_CMD_FL0FBMAX_V((chip_ver <= CHELSIO_T5) ?
  3923. FETCHBURSTMAX_512B_X :
  3924. FETCHBURSTMAX_256B_X));
  3925. c.fl0size = htons(flsz);
  3926. c.fl0addr = cpu_to_be64(fl->addr);
  3927. }
  3928. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  3929. if (ret)
  3930. goto err;
  3931. netif_napi_add(dev, &iq->napi, napi_rx_handler);
  3932. iq->cur_desc = iq->desc;
  3933. iq->cidx = 0;
  3934. iq->gen = 1;
  3935. iq->next_intr_params = iq->intr_params;
  3936. iq->cntxt_id = ntohs(c.iqid);
  3937. iq->abs_id = ntohs(c.physiqid);
  3938. iq->bar2_addr = bar2_address(adap,
  3939. iq->cntxt_id,
  3940. T4_BAR2_QTYPE_INGRESS,
  3941. &iq->bar2_qid);
  3942. iq->size--; /* subtract status entry */
  3943. iq->netdev = dev;
  3944. iq->handler = hnd;
  3945. iq->flush_handler = flush_hnd;
  3946. memset(&iq->lro_mgr, 0, sizeof(struct t4_lro_mgr));
  3947. skb_queue_head_init(&iq->lro_mgr.lroq);
  3948. /* set offset to -1 to distinguish ingress queues without FL */
  3949. iq->offset = fl ? 0 : -1;
  3950. adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
  3951. if (fl) {
  3952. fl->cntxt_id = ntohs(c.fl0id);
  3953. fl->avail = fl->pend_cred = 0;
  3954. fl->pidx = fl->cidx = 0;
  3955. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  3956. adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
  3957. /* Note, we must initialize the BAR2 Free List User Doorbell
  3958. * information before refilling the Free List!
  3959. */
  3960. fl->bar2_addr = bar2_address(adap,
  3961. fl->cntxt_id,
  3962. T4_BAR2_QTYPE_EGRESS,
  3963. &fl->bar2_qid);
  3964. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  3965. }
  3966. /* For T5 and later we attempt to set up the Congestion Manager values
  3967. * of the new RX Ethernet Queue. This should really be handled by
  3968. * firmware because it's more complex than any host driver wants to
  3969. * get involved with and it's different per chip and this is almost
  3970. * certainly wrong. Firmware would be wrong as well, but it would be
  3971. * a lot easier to fix in one place ... For now we do something very
  3972. * simple (and hopefully less wrong).
  3973. */
  3974. if (!is_t4(adap->params.chip) && cong >= 0) {
  3975. u32 param, val, ch_map = 0;
  3976. int i;
  3977. u16 cng_ch_bits_log = adap->params.arch.cng_ch_bits_log;
  3978. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  3979. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
  3980. FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
  3981. if (cong == 0) {
  3982. val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
  3983. } else {
  3984. val =
  3985. CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
  3986. for (i = 0; i < 4; i++) {
  3987. if (cong & (1 << i))
  3988. ch_map |= 1 << (i << cng_ch_bits_log);
  3989. }
  3990. val |= CONMCTXT_CNGCHMAP_V(ch_map);
  3991. }
  3992. ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
  3993. &param, &val);
  3994. if (ret)
  3995. dev_warn(adap->pdev_dev, "Failed to set Congestion"
  3996. " Manager Context for Ingress Queue %d: %d\n",
  3997. iq->cntxt_id, -ret);
  3998. }
  3999. return 0;
  4000. fl_nomem:
  4001. ret = -ENOMEM;
  4002. err:
  4003. if (iq->desc) {
  4004. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  4005. iq->desc, iq->phys_addr);
  4006. iq->desc = NULL;
  4007. }
  4008. if (fl && fl->desc) {
  4009. kfree(fl->sdesc);
  4010. fl->sdesc = NULL;
  4011. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  4012. fl->desc, fl->addr);
  4013. fl->desc = NULL;
  4014. }
  4015. return ret;
  4016. }
  4017. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  4018. {
  4019. q->cntxt_id = id;
  4020. q->bar2_addr = bar2_address(adap,
  4021. q->cntxt_id,
  4022. T4_BAR2_QTYPE_EGRESS,
  4023. &q->bar2_qid);
  4024. q->in_use = 0;
  4025. q->cidx = q->pidx = 0;
  4026. q->stops = q->restarts = 0;
  4027. q->stat = (void *)&q->desc[q->size];
  4028. spin_lock_init(&q->db_lock);
  4029. adap->sge.egr_map[id - adap->sge.egr_start] = q;
  4030. }
  4031. /**
  4032. * t4_sge_alloc_eth_txq - allocate an Ethernet TX Queue
  4033. * @adap: the adapter
  4034. * @txq: the SGE Ethernet TX Queue to initialize
  4035. * @dev: the Linux Network Device
  4036. * @netdevq: the corresponding Linux TX Queue
  4037. * @iqid: the Ingress Queue to which to deliver CIDX Update messages
  4038. * @dbqt: whether this TX Queue will use the SGE Doorbell Queue Timers
  4039. */
  4040. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  4041. struct net_device *dev, struct netdev_queue *netdevq,
  4042. unsigned int iqid, u8 dbqt)
  4043. {
  4044. unsigned int chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
  4045. struct port_info *pi = netdev_priv(dev);
  4046. struct sge *s = &adap->sge;
  4047. struct fw_eq_eth_cmd c;
  4048. int ret, nentries;
  4049. /* Add status entries */
  4050. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  4051. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  4052. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  4053. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  4054. netdev_queue_numa_node_read(netdevq));
  4055. if (!txq->q.desc)
  4056. return -ENOMEM;
  4057. memset(&c, 0, sizeof(c));
  4058. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
  4059. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  4060. FW_EQ_ETH_CMD_PFN_V(adap->pf) |
  4061. FW_EQ_ETH_CMD_VFN_V(0));
  4062. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
  4063. FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
  4064. /* For TX Ethernet Queues using the SGE Doorbell Queue Timer
  4065. * mechanism, we use Ingress Queue messages for Hardware Consumer
  4066. * Index Updates on the TX Queue. Otherwise we have the Hardware
  4067. * write the CIDX Updates into the Status Page at the end of the
  4068. * TX Queue.
  4069. */
  4070. c.autoequiqe_to_viid = htonl(((chip_ver <= CHELSIO_T5) ?
  4071. FW_EQ_ETH_CMD_AUTOEQUIQE_F :
  4072. FW_EQ_ETH_CMD_AUTOEQUEQE_F) |
  4073. FW_EQ_ETH_CMD_VIID_V(pi->viid));
  4074. c.fetchszm_to_iqid =
  4075. htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V((chip_ver <= CHELSIO_T5) ?
  4076. HOSTFCMODE_INGRESS_QUEUE_X :
  4077. HOSTFCMODE_STATUS_PAGE_X) |
  4078. FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
  4079. FW_EQ_ETH_CMD_FETCHRO_F | FW_EQ_ETH_CMD_IQID_V(iqid));
  4080. /* Note that the CIDX Flush Threshold should match MAX_TX_RECLAIM. */
  4081. c.dcaen_to_eqsize =
  4082. htonl(FW_EQ_ETH_CMD_FBMIN_V(chip_ver <= CHELSIO_T5
  4083. ? FETCHBURSTMIN_64B_X
  4084. : FETCHBURSTMIN_64B_T6_X) |
  4085. FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  4086. FW_EQ_ETH_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  4087. FW_EQ_ETH_CMD_CIDXFTHRESHO_V(chip_ver == CHELSIO_T5) |
  4088. FW_EQ_ETH_CMD_EQSIZE_V(nentries));
  4089. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  4090. /* If we're using the SGE Doorbell Queue Timer mechanism, pass in the
  4091. * currently configured Timer Index. THis can be changed later via an
  4092. * ethtool -C tx-usecs {Timer Val} command. Note that the SGE
  4093. * Doorbell Queue mode is currently automatically enabled in the
  4094. * Firmware by setting either AUTOEQUEQE or AUTOEQUIQE ...
  4095. */
  4096. if (dbqt)
  4097. c.timeren_timerix =
  4098. cpu_to_be32(FW_EQ_ETH_CMD_TIMEREN_F |
  4099. FW_EQ_ETH_CMD_TIMERIX_V(txq->dbqtimerix));
  4100. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  4101. if (ret) {
  4102. kfree(txq->q.sdesc);
  4103. txq->q.sdesc = NULL;
  4104. dma_free_coherent(adap->pdev_dev,
  4105. nentries * sizeof(struct tx_desc),
  4106. txq->q.desc, txq->q.phys_addr);
  4107. txq->q.desc = NULL;
  4108. return ret;
  4109. }
  4110. txq->q.q_type = CXGB4_TXQ_ETH;
  4111. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
  4112. txq->txq = netdevq;
  4113. txq->tso = 0;
  4114. txq->uso = 0;
  4115. txq->tx_cso = 0;
  4116. txq->vlan_ins = 0;
  4117. txq->mapping_err = 0;
  4118. txq->dbqt = dbqt;
  4119. return 0;
  4120. }
  4121. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  4122. struct net_device *dev, unsigned int iqid,
  4123. unsigned int cmplqid)
  4124. {
  4125. unsigned int chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
  4126. struct port_info *pi = netdev_priv(dev);
  4127. struct sge *s = &adap->sge;
  4128. struct fw_eq_ctrl_cmd c;
  4129. int ret, nentries;
  4130. /* Add status entries */
  4131. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  4132. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  4133. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  4134. NULL, 0, dev_to_node(adap->pdev_dev));
  4135. if (!txq->q.desc)
  4136. return -ENOMEM;
  4137. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
  4138. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  4139. FW_EQ_CTRL_CMD_PFN_V(adap->pf) |
  4140. FW_EQ_CTRL_CMD_VFN_V(0));
  4141. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
  4142. FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
  4143. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
  4144. c.physeqid_pkd = htonl(0);
  4145. c.fetchszm_to_iqid =
  4146. htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  4147. FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
  4148. FW_EQ_CTRL_CMD_FETCHRO_F | FW_EQ_CTRL_CMD_IQID_V(iqid));
  4149. c.dcaen_to_eqsize =
  4150. htonl(FW_EQ_CTRL_CMD_FBMIN_V(chip_ver <= CHELSIO_T5
  4151. ? FETCHBURSTMIN_64B_X
  4152. : FETCHBURSTMIN_64B_T6_X) |
  4153. FW_EQ_CTRL_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  4154. FW_EQ_CTRL_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  4155. FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
  4156. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  4157. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  4158. if (ret) {
  4159. dma_free_coherent(adap->pdev_dev,
  4160. nentries * sizeof(struct tx_desc),
  4161. txq->q.desc, txq->q.phys_addr);
  4162. txq->q.desc = NULL;
  4163. return ret;
  4164. }
  4165. txq->q.q_type = CXGB4_TXQ_CTRL;
  4166. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
  4167. txq->adap = adap;
  4168. skb_queue_head_init(&txq->sendq);
  4169. tasklet_setup(&txq->qresume_tsk, restart_ctrlq);
  4170. txq->full = 0;
  4171. return 0;
  4172. }
  4173. int t4_sge_mod_ctrl_txq(struct adapter *adap, unsigned int eqid,
  4174. unsigned int cmplqid)
  4175. {
  4176. u32 param, val;
  4177. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  4178. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL) |
  4179. FW_PARAMS_PARAM_YZ_V(eqid));
  4180. val = cmplqid;
  4181. return t4_set_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
  4182. }
  4183. static int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_txq *q,
  4184. struct net_device *dev, u32 cmd, u32 iqid)
  4185. {
  4186. unsigned int chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
  4187. struct port_info *pi = netdev_priv(dev);
  4188. struct sge *s = &adap->sge;
  4189. struct fw_eq_ofld_cmd c;
  4190. u32 fb_min, nentries;
  4191. int ret;
  4192. /* Add status entries */
  4193. nentries = q->size + s->stat_len / sizeof(struct tx_desc);
  4194. q->desc = alloc_ring(adap->pdev_dev, q->size, sizeof(struct tx_desc),
  4195. sizeof(struct tx_sw_desc), &q->phys_addr,
  4196. &q->sdesc, s->stat_len, NUMA_NO_NODE);
  4197. if (!q->desc)
  4198. return -ENOMEM;
  4199. if (chip_ver <= CHELSIO_T5)
  4200. fb_min = FETCHBURSTMIN_64B_X;
  4201. else
  4202. fb_min = FETCHBURSTMIN_64B_T6_X;
  4203. memset(&c, 0, sizeof(c));
  4204. c.op_to_vfn = htonl(FW_CMD_OP_V(cmd) | FW_CMD_REQUEST_F |
  4205. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  4206. FW_EQ_OFLD_CMD_PFN_V(adap->pf) |
  4207. FW_EQ_OFLD_CMD_VFN_V(0));
  4208. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
  4209. FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
  4210. c.fetchszm_to_iqid =
  4211. htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  4212. FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
  4213. FW_EQ_OFLD_CMD_FETCHRO_F | FW_EQ_OFLD_CMD_IQID_V(iqid));
  4214. c.dcaen_to_eqsize =
  4215. htonl(FW_EQ_OFLD_CMD_FBMIN_V(fb_min) |
  4216. FW_EQ_OFLD_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  4217. FW_EQ_OFLD_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  4218. FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
  4219. c.eqaddr = cpu_to_be64(q->phys_addr);
  4220. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  4221. if (ret) {
  4222. kfree(q->sdesc);
  4223. q->sdesc = NULL;
  4224. dma_free_coherent(adap->pdev_dev,
  4225. nentries * sizeof(struct tx_desc),
  4226. q->desc, q->phys_addr);
  4227. q->desc = NULL;
  4228. return ret;
  4229. }
  4230. init_txq(adap, q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
  4231. return 0;
  4232. }
  4233. int t4_sge_alloc_uld_txq(struct adapter *adap, struct sge_uld_txq *txq,
  4234. struct net_device *dev, unsigned int iqid,
  4235. unsigned int uld_type)
  4236. {
  4237. u32 cmd = FW_EQ_OFLD_CMD;
  4238. int ret;
  4239. if (unlikely(uld_type == CXGB4_TX_CRYPTO))
  4240. cmd = FW_EQ_CTRL_CMD;
  4241. ret = t4_sge_alloc_ofld_txq(adap, &txq->q, dev, cmd, iqid);
  4242. if (ret)
  4243. return ret;
  4244. txq->q.q_type = CXGB4_TXQ_ULD;
  4245. txq->adap = adap;
  4246. skb_queue_head_init(&txq->sendq);
  4247. tasklet_setup(&txq->qresume_tsk, restart_ofldq);
  4248. txq->full = 0;
  4249. txq->mapping_err = 0;
  4250. return 0;
  4251. }
  4252. int t4_sge_alloc_ethofld_txq(struct adapter *adap, struct sge_eohw_txq *txq,
  4253. struct net_device *dev, u32 iqid)
  4254. {
  4255. int ret;
  4256. ret = t4_sge_alloc_ofld_txq(adap, &txq->q, dev, FW_EQ_OFLD_CMD, iqid);
  4257. if (ret)
  4258. return ret;
  4259. txq->q.q_type = CXGB4_TXQ_ULD;
  4260. spin_lock_init(&txq->lock);
  4261. txq->adap = adap;
  4262. txq->tso = 0;
  4263. txq->uso = 0;
  4264. txq->tx_cso = 0;
  4265. txq->vlan_ins = 0;
  4266. txq->mapping_err = 0;
  4267. return 0;
  4268. }
  4269. void free_txq(struct adapter *adap, struct sge_txq *q)
  4270. {
  4271. struct sge *s = &adap->sge;
  4272. dma_free_coherent(adap->pdev_dev,
  4273. q->size * sizeof(struct tx_desc) + s->stat_len,
  4274. q->desc, q->phys_addr);
  4275. q->cntxt_id = 0;
  4276. q->sdesc = NULL;
  4277. q->desc = NULL;
  4278. }
  4279. void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  4280. struct sge_fl *fl)
  4281. {
  4282. struct sge *s = &adap->sge;
  4283. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  4284. adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
  4285. t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
  4286. rq->cntxt_id, fl_id, 0xffff);
  4287. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  4288. rq->desc, rq->phys_addr);
  4289. netif_napi_del(&rq->napi);
  4290. rq->netdev = NULL;
  4291. rq->cntxt_id = rq->abs_id = 0;
  4292. rq->desc = NULL;
  4293. if (fl) {
  4294. free_rx_bufs(adap, fl, fl->avail);
  4295. dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
  4296. fl->desc, fl->addr);
  4297. kfree(fl->sdesc);
  4298. fl->sdesc = NULL;
  4299. fl->cntxt_id = 0;
  4300. fl->desc = NULL;
  4301. }
  4302. }
  4303. /**
  4304. * t4_free_ofld_rxqs - free a block of consecutive Rx queues
  4305. * @adap: the adapter
  4306. * @n: number of queues
  4307. * @q: pointer to first queue
  4308. *
  4309. * Release the resources of a consecutive block of offload Rx queues.
  4310. */
  4311. void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
  4312. {
  4313. for ( ; n; n--, q++)
  4314. if (q->rspq.desc)
  4315. free_rspq_fl(adap, &q->rspq,
  4316. q->fl.size ? &q->fl : NULL);
  4317. }
  4318. void t4_sge_free_ethofld_txq(struct adapter *adap, struct sge_eohw_txq *txq)
  4319. {
  4320. if (txq->q.desc) {
  4321. t4_ofld_eq_free(adap, adap->mbox, adap->pf, 0,
  4322. txq->q.cntxt_id);
  4323. free_tx_desc(adap, &txq->q, txq->q.in_use, false);
  4324. kfree(txq->q.sdesc);
  4325. free_txq(adap, &txq->q);
  4326. }
  4327. }
  4328. /**
  4329. * t4_free_sge_resources - free SGE resources
  4330. * @adap: the adapter
  4331. *
  4332. * Frees resources used by the SGE queue sets.
  4333. */
  4334. void t4_free_sge_resources(struct adapter *adap)
  4335. {
  4336. int i;
  4337. struct sge_eth_rxq *eq;
  4338. struct sge_eth_txq *etq;
  4339. /* stop all Rx queues in order to start them draining */
  4340. for (i = 0; i < adap->sge.ethqsets; i++) {
  4341. eq = &adap->sge.ethrxq[i];
  4342. if (eq->rspq.desc)
  4343. t4_iq_stop(adap, adap->mbox, adap->pf, 0,
  4344. FW_IQ_TYPE_FL_INT_CAP,
  4345. eq->rspq.cntxt_id,
  4346. eq->fl.size ? eq->fl.cntxt_id : 0xffff,
  4347. 0xffff);
  4348. }
  4349. /* clean up Ethernet Tx/Rx queues */
  4350. for (i = 0; i < adap->sge.ethqsets; i++) {
  4351. eq = &adap->sge.ethrxq[i];
  4352. if (eq->rspq.desc)
  4353. free_rspq_fl(adap, &eq->rspq,
  4354. eq->fl.size ? &eq->fl : NULL);
  4355. if (eq->msix) {
  4356. cxgb4_free_msix_idx_in_bmap(adap, eq->msix->idx);
  4357. eq->msix = NULL;
  4358. }
  4359. etq = &adap->sge.ethtxq[i];
  4360. if (etq->q.desc) {
  4361. t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
  4362. etq->q.cntxt_id);
  4363. __netif_tx_lock_bh(etq->txq);
  4364. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  4365. __netif_tx_unlock_bh(etq->txq);
  4366. kfree(etq->q.sdesc);
  4367. free_txq(adap, &etq->q);
  4368. }
  4369. }
  4370. /* clean up control Tx queues */
  4371. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  4372. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  4373. if (cq->q.desc) {
  4374. tasklet_kill(&cq->qresume_tsk);
  4375. t4_ctrl_eq_free(adap, adap->mbox, adap->pf, 0,
  4376. cq->q.cntxt_id);
  4377. __skb_queue_purge(&cq->sendq);
  4378. free_txq(adap, &cq->q);
  4379. }
  4380. }
  4381. if (adap->sge.fw_evtq.desc) {
  4382. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  4383. if (adap->sge.fwevtq_msix_idx >= 0)
  4384. cxgb4_free_msix_idx_in_bmap(adap,
  4385. adap->sge.fwevtq_msix_idx);
  4386. }
  4387. if (adap->sge.nd_msix_idx >= 0)
  4388. cxgb4_free_msix_idx_in_bmap(adap, adap->sge.nd_msix_idx);
  4389. if (adap->sge.intrq.desc)
  4390. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  4391. if (!is_t4(adap->params.chip)) {
  4392. etq = &adap->sge.ptptxq;
  4393. if (etq->q.desc) {
  4394. t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
  4395. etq->q.cntxt_id);
  4396. spin_lock_bh(&adap->ptp_lock);
  4397. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  4398. spin_unlock_bh(&adap->ptp_lock);
  4399. kfree(etq->q.sdesc);
  4400. free_txq(adap, &etq->q);
  4401. }
  4402. }
  4403. /* clear the reverse egress queue map */
  4404. memset(adap->sge.egr_map, 0,
  4405. adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
  4406. }
  4407. void t4_sge_start(struct adapter *adap)
  4408. {
  4409. adap->sge.ethtxq_rover = 0;
  4410. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  4411. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  4412. }
  4413. /**
  4414. * t4_sge_stop - disable SGE operation
  4415. * @adap: the adapter
  4416. *
  4417. * Stop tasklets and timers associated with the DMA engine. Note that
  4418. * this is effective only if measures have been taken to disable any HW
  4419. * events that may restart them.
  4420. */
  4421. void t4_sge_stop(struct adapter *adap)
  4422. {
  4423. int i;
  4424. struct sge *s = &adap->sge;
  4425. if (s->rx_timer.function)
  4426. del_timer_sync(&s->rx_timer);
  4427. if (s->tx_timer.function)
  4428. del_timer_sync(&s->tx_timer);
  4429. if (is_offload(adap)) {
  4430. struct sge_uld_txq_info *txq_info;
  4431. txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
  4432. if (txq_info) {
  4433. struct sge_uld_txq *txq = txq_info->uldtxq;
  4434. for_each_ofldtxq(&adap->sge, i) {
  4435. if (txq->q.desc)
  4436. tasklet_kill(&txq->qresume_tsk);
  4437. }
  4438. }
  4439. }
  4440. if (is_pci_uld(adap)) {
  4441. struct sge_uld_txq_info *txq_info;
  4442. txq_info = adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
  4443. if (txq_info) {
  4444. struct sge_uld_txq *txq = txq_info->uldtxq;
  4445. for_each_ofldtxq(&adap->sge, i) {
  4446. if (txq->q.desc)
  4447. tasklet_kill(&txq->qresume_tsk);
  4448. }
  4449. }
  4450. }
  4451. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  4452. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  4453. if (cq->q.desc)
  4454. tasklet_kill(&cq->qresume_tsk);
  4455. }
  4456. }
  4457. /**
  4458. * t4_sge_init_soft - grab core SGE values needed by SGE code
  4459. * @adap: the adapter
  4460. *
  4461. * We need to grab the SGE operating parameters that we need to have
  4462. * in order to do our job and make sure we can live with them.
  4463. */
  4464. static int t4_sge_init_soft(struct adapter *adap)
  4465. {
  4466. struct sge *s = &adap->sge;
  4467. u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
  4468. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  4469. u32 ingress_rx_threshold;
  4470. /*
  4471. * Verify that CPL messages are going to the Ingress Queue for
  4472. * process_responses() and that only packet data is going to the
  4473. * Free Lists.
  4474. */
  4475. if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
  4476. RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
  4477. dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
  4478. return -EINVAL;
  4479. }
  4480. /*
  4481. * Validate the Host Buffer Register Array indices that we want to
  4482. * use ...
  4483. *
  4484. * XXX Note that we should really read through the Host Buffer Size
  4485. * XXX register array and find the indices of the Buffer Sizes which
  4486. * XXX meet our needs!
  4487. */
  4488. #define READ_FL_BUF(x) \
  4489. t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
  4490. fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
  4491. fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
  4492. fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
  4493. fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
  4494. /* We only bother using the Large Page logic if the Large Page Buffer
  4495. * is larger than our Page Size Buffer.
  4496. */
  4497. if (fl_large_pg <= fl_small_pg)
  4498. fl_large_pg = 0;
  4499. #undef READ_FL_BUF
  4500. /* The Page Size Buffer must be exactly equal to our Page Size and the
  4501. * Large Page Size Buffer should be 0 (per above) or a power of 2.
  4502. */
  4503. if (fl_small_pg != PAGE_SIZE ||
  4504. (fl_large_pg & (fl_large_pg-1)) != 0) {
  4505. dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
  4506. fl_small_pg, fl_large_pg);
  4507. return -EINVAL;
  4508. }
  4509. if (fl_large_pg)
  4510. s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
  4511. if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
  4512. fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
  4513. dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
  4514. fl_small_mtu, fl_large_mtu);
  4515. return -EINVAL;
  4516. }
  4517. /*
  4518. * Retrieve our RX interrupt holdoff timer values and counter
  4519. * threshold values from the SGE parameters.
  4520. */
  4521. timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
  4522. timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
  4523. timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
  4524. s->timer_val[0] = core_ticks_to_us(adap,
  4525. TIMERVALUE0_G(timer_value_0_and_1));
  4526. s->timer_val[1] = core_ticks_to_us(adap,
  4527. TIMERVALUE1_G(timer_value_0_and_1));
  4528. s->timer_val[2] = core_ticks_to_us(adap,
  4529. TIMERVALUE2_G(timer_value_2_and_3));
  4530. s->timer_val[3] = core_ticks_to_us(adap,
  4531. TIMERVALUE3_G(timer_value_2_and_3));
  4532. s->timer_val[4] = core_ticks_to_us(adap,
  4533. TIMERVALUE4_G(timer_value_4_and_5));
  4534. s->timer_val[5] = core_ticks_to_us(adap,
  4535. TIMERVALUE5_G(timer_value_4_and_5));
  4536. ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
  4537. s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
  4538. s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
  4539. s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
  4540. s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
  4541. return 0;
  4542. }
  4543. /**
  4544. * t4_sge_init - initialize SGE
  4545. * @adap: the adapter
  4546. *
  4547. * Perform low-level SGE code initialization needed every time after a
  4548. * chip reset.
  4549. */
  4550. int t4_sge_init(struct adapter *adap)
  4551. {
  4552. struct sge *s = &adap->sge;
  4553. u32 sge_control, sge_conm_ctrl;
  4554. int ret, egress_threshold;
  4555. /*
  4556. * Ingress Padding Boundary and Egress Status Page Size are set up by
  4557. * t4_fixup_host_params().
  4558. */
  4559. sge_control = t4_read_reg(adap, SGE_CONTROL_A);
  4560. s->pktshift = PKTSHIFT_G(sge_control);
  4561. s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
  4562. s->fl_align = t4_fl_pkt_align(adap);
  4563. ret = t4_sge_init_soft(adap);
  4564. if (ret < 0)
  4565. return ret;
  4566. /*
  4567. * A FL with <= fl_starve_thres buffers is starving and a periodic
  4568. * timer will attempt to refill it. This needs to be larger than the
  4569. * SGE's Egress Congestion Threshold. If it isn't, then we can get
  4570. * stuck waiting for new packets while the SGE is waiting for us to
  4571. * give it more Free List entries. (Note that the SGE's Egress
  4572. * Congestion Threshold is in units of 2 Free List pointers.) For T4,
  4573. * there was only a single field to control this. For T5 there's the
  4574. * original field which now only applies to Unpacked Mode Free List
  4575. * buffers and a new field which only applies to Packed Mode Free List
  4576. * buffers.
  4577. */
  4578. sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
  4579. switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
  4580. case CHELSIO_T4:
  4581. egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
  4582. break;
  4583. case CHELSIO_T5:
  4584. egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  4585. break;
  4586. case CHELSIO_T6:
  4587. egress_threshold = T6_EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  4588. break;
  4589. default:
  4590. dev_err(adap->pdev_dev, "Unsupported Chip version %d\n",
  4591. CHELSIO_CHIP_VERSION(adap->params.chip));
  4592. return -EINVAL;
  4593. }
  4594. s->fl_starve_thres = 2*egress_threshold + 1;
  4595. t4_idma_monitor_init(adap, &s->idma_monitor);
  4596. /* Set up timers used for recuring callbacks to process RX and TX
  4597. * administrative tasks.
  4598. */
  4599. timer_setup(&s->rx_timer, sge_rx_timer_cb, 0);
  4600. timer_setup(&s->tx_timer, sge_tx_timer_cb, 0);
  4601. spin_lock_init(&s->intrq_lock);
  4602. return 0;
  4603. }