tid_rdma.c 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /*
  3. * Copyright(c) 2018 - 2020 Intel Corporation.
  4. *
  5. */
  6. #include "hfi.h"
  7. #include "qp.h"
  8. #include "rc.h"
  9. #include "verbs.h"
  10. #include "tid_rdma.h"
  11. #include "exp_rcv.h"
  12. #include "trace.h"
  13. /**
  14. * DOC: TID RDMA READ protocol
  15. *
  16. * This is an end-to-end protocol at the hfi1 level between two nodes that
  17. * improves performance by avoiding data copy on the requester side. It
  18. * converts a qualified RDMA READ request into a TID RDMA READ request on
  19. * the requester side and thereafter handles the request and response
  20. * differently. To be qualified, the RDMA READ request should meet the
  21. * following:
  22. * -- The total data length should be greater than 256K;
  23. * -- The total data length should be a multiple of 4K page size;
  24. * -- Each local scatter-gather entry should be 4K page aligned;
  25. * -- Each local scatter-gather entry should be a multiple of 4K page size;
  26. */
  27. #define RCV_TID_FLOW_TABLE_CTRL_FLOW_VALID_SMASK BIT_ULL(32)
  28. #define RCV_TID_FLOW_TABLE_CTRL_HDR_SUPP_EN_SMASK BIT_ULL(33)
  29. #define RCV_TID_FLOW_TABLE_CTRL_KEEP_AFTER_SEQ_ERR_SMASK BIT_ULL(34)
  30. #define RCV_TID_FLOW_TABLE_CTRL_KEEP_ON_GEN_ERR_SMASK BIT_ULL(35)
  31. #define RCV_TID_FLOW_TABLE_STATUS_SEQ_MISMATCH_SMASK BIT_ULL(37)
  32. #define RCV_TID_FLOW_TABLE_STATUS_GEN_MISMATCH_SMASK BIT_ULL(38)
  33. /* Maximum number of packets within a flow generation. */
  34. #define MAX_TID_FLOW_PSN BIT(HFI1_KDETH_BTH_SEQ_SHIFT)
  35. #define GENERATION_MASK 0xFFFFF
  36. static u32 mask_generation(u32 a)
  37. {
  38. return a & GENERATION_MASK;
  39. }
  40. /* Reserved generation value to set to unused flows for kernel contexts */
  41. #define KERN_GENERATION_RESERVED mask_generation(U32_MAX)
  42. /*
  43. * J_KEY for kernel contexts when TID RDMA is used.
  44. * See generate_jkey() in hfi.h for more information.
  45. */
  46. #define TID_RDMA_JKEY 32
  47. #define HFI1_KERNEL_MIN_JKEY HFI1_ADMIN_JKEY_RANGE
  48. #define HFI1_KERNEL_MAX_JKEY (2 * HFI1_ADMIN_JKEY_RANGE - 1)
  49. /* Maximum number of segments in flight per QP request. */
  50. #define TID_RDMA_MAX_READ_SEGS_PER_REQ 6
  51. #define TID_RDMA_MAX_WRITE_SEGS_PER_REQ 4
  52. #define MAX_REQ max_t(u16, TID_RDMA_MAX_READ_SEGS_PER_REQ, \
  53. TID_RDMA_MAX_WRITE_SEGS_PER_REQ)
  54. #define MAX_FLOWS roundup_pow_of_two(MAX_REQ + 1)
  55. #define MAX_EXPECTED_PAGES (MAX_EXPECTED_BUFFER / PAGE_SIZE)
  56. #define TID_RDMA_DESTQP_FLOW_SHIFT 11
  57. #define TID_RDMA_DESTQP_FLOW_MASK 0x1f
  58. #define TID_OPFN_QP_CTXT_MASK 0xff
  59. #define TID_OPFN_QP_CTXT_SHIFT 56
  60. #define TID_OPFN_QP_KDETH_MASK 0xff
  61. #define TID_OPFN_QP_KDETH_SHIFT 48
  62. #define TID_OPFN_MAX_LEN_MASK 0x7ff
  63. #define TID_OPFN_MAX_LEN_SHIFT 37
  64. #define TID_OPFN_TIMEOUT_MASK 0x1f
  65. #define TID_OPFN_TIMEOUT_SHIFT 32
  66. #define TID_OPFN_RESERVED_MASK 0x3f
  67. #define TID_OPFN_RESERVED_SHIFT 26
  68. #define TID_OPFN_URG_MASK 0x1
  69. #define TID_OPFN_URG_SHIFT 25
  70. #define TID_OPFN_VER_MASK 0x7
  71. #define TID_OPFN_VER_SHIFT 22
  72. #define TID_OPFN_JKEY_MASK 0x3f
  73. #define TID_OPFN_JKEY_SHIFT 16
  74. #define TID_OPFN_MAX_READ_MASK 0x3f
  75. #define TID_OPFN_MAX_READ_SHIFT 10
  76. #define TID_OPFN_MAX_WRITE_MASK 0x3f
  77. #define TID_OPFN_MAX_WRITE_SHIFT 4
  78. /*
  79. * OPFN TID layout
  80. *
  81. * 63 47 31 15
  82. * NNNNNNNNKKKKKKKK MMMMMMMMMMMTTTTT DDDDDDUVVVJJJJJJ RRRRRRWWWWWWCCCC
  83. * 3210987654321098 7654321098765432 1098765432109876 5432109876543210
  84. * N - the context Number
  85. * K - the Kdeth_qp
  86. * M - Max_len
  87. * T - Timeout
  88. * D - reserveD
  89. * V - version
  90. * U - Urg capable
  91. * J - Jkey
  92. * R - max_Read
  93. * W - max_Write
  94. * C - Capcode
  95. */
  96. static void tid_rdma_trigger_resume(struct work_struct *work);
  97. static void hfi1_kern_exp_rcv_free_flows(struct tid_rdma_request *req);
  98. static int hfi1_kern_exp_rcv_alloc_flows(struct tid_rdma_request *req,
  99. gfp_t gfp);
  100. static void hfi1_init_trdma_req(struct rvt_qp *qp,
  101. struct tid_rdma_request *req);
  102. static void hfi1_tid_write_alloc_resources(struct rvt_qp *qp, bool intr_ctx);
  103. static void hfi1_tid_timeout(struct timer_list *t);
  104. static void hfi1_add_tid_reap_timer(struct rvt_qp *qp);
  105. static void hfi1_mod_tid_reap_timer(struct rvt_qp *qp);
  106. static void hfi1_mod_tid_retry_timer(struct rvt_qp *qp);
  107. static int hfi1_stop_tid_retry_timer(struct rvt_qp *qp);
  108. static void hfi1_tid_retry_timeout(struct timer_list *t);
  109. static int make_tid_rdma_ack(struct rvt_qp *qp,
  110. struct ib_other_headers *ohdr,
  111. struct hfi1_pkt_state *ps);
  112. static void hfi1_do_tid_send(struct rvt_qp *qp);
  113. static u32 read_r_next_psn(struct hfi1_devdata *dd, u8 ctxt, u8 fidx);
  114. static void tid_rdma_rcv_err(struct hfi1_packet *packet,
  115. struct ib_other_headers *ohdr,
  116. struct rvt_qp *qp, u32 psn, int diff, bool fecn);
  117. static void update_r_next_psn_fecn(struct hfi1_packet *packet,
  118. struct hfi1_qp_priv *priv,
  119. struct hfi1_ctxtdata *rcd,
  120. struct tid_rdma_flow *flow,
  121. bool fecn);
  122. static void validate_r_tid_ack(struct hfi1_qp_priv *priv)
  123. {
  124. if (priv->r_tid_ack == HFI1_QP_WQE_INVALID)
  125. priv->r_tid_ack = priv->r_tid_tail;
  126. }
  127. static void tid_rdma_schedule_ack(struct rvt_qp *qp)
  128. {
  129. struct hfi1_qp_priv *priv = qp->priv;
  130. priv->s_flags |= RVT_S_ACK_PENDING;
  131. hfi1_schedule_tid_send(qp);
  132. }
  133. static void tid_rdma_trigger_ack(struct rvt_qp *qp)
  134. {
  135. validate_r_tid_ack(qp->priv);
  136. tid_rdma_schedule_ack(qp);
  137. }
  138. static u64 tid_rdma_opfn_encode(struct tid_rdma_params *p)
  139. {
  140. return
  141. (((u64)p->qp & TID_OPFN_QP_CTXT_MASK) <<
  142. TID_OPFN_QP_CTXT_SHIFT) |
  143. ((((u64)p->qp >> 16) & TID_OPFN_QP_KDETH_MASK) <<
  144. TID_OPFN_QP_KDETH_SHIFT) |
  145. (((u64)((p->max_len >> PAGE_SHIFT) - 1) &
  146. TID_OPFN_MAX_LEN_MASK) << TID_OPFN_MAX_LEN_SHIFT) |
  147. (((u64)p->timeout & TID_OPFN_TIMEOUT_MASK) <<
  148. TID_OPFN_TIMEOUT_SHIFT) |
  149. (((u64)p->urg & TID_OPFN_URG_MASK) << TID_OPFN_URG_SHIFT) |
  150. (((u64)p->jkey & TID_OPFN_JKEY_MASK) << TID_OPFN_JKEY_SHIFT) |
  151. (((u64)p->max_read & TID_OPFN_MAX_READ_MASK) <<
  152. TID_OPFN_MAX_READ_SHIFT) |
  153. (((u64)p->max_write & TID_OPFN_MAX_WRITE_MASK) <<
  154. TID_OPFN_MAX_WRITE_SHIFT);
  155. }
  156. static void tid_rdma_opfn_decode(struct tid_rdma_params *p, u64 data)
  157. {
  158. p->max_len = (((data >> TID_OPFN_MAX_LEN_SHIFT) &
  159. TID_OPFN_MAX_LEN_MASK) + 1) << PAGE_SHIFT;
  160. p->jkey = (data >> TID_OPFN_JKEY_SHIFT) & TID_OPFN_JKEY_MASK;
  161. p->max_write = (data >> TID_OPFN_MAX_WRITE_SHIFT) &
  162. TID_OPFN_MAX_WRITE_MASK;
  163. p->max_read = (data >> TID_OPFN_MAX_READ_SHIFT) &
  164. TID_OPFN_MAX_READ_MASK;
  165. p->qp =
  166. ((((data >> TID_OPFN_QP_KDETH_SHIFT) & TID_OPFN_QP_KDETH_MASK)
  167. << 16) |
  168. ((data >> TID_OPFN_QP_CTXT_SHIFT) & TID_OPFN_QP_CTXT_MASK));
  169. p->urg = (data >> TID_OPFN_URG_SHIFT) & TID_OPFN_URG_MASK;
  170. p->timeout = (data >> TID_OPFN_TIMEOUT_SHIFT) & TID_OPFN_TIMEOUT_MASK;
  171. }
  172. void tid_rdma_opfn_init(struct rvt_qp *qp, struct tid_rdma_params *p)
  173. {
  174. struct hfi1_qp_priv *priv = qp->priv;
  175. p->qp = (RVT_KDETH_QP_PREFIX << 16) | priv->rcd->ctxt;
  176. p->max_len = TID_RDMA_MAX_SEGMENT_SIZE;
  177. p->jkey = priv->rcd->jkey;
  178. p->max_read = TID_RDMA_MAX_READ_SEGS_PER_REQ;
  179. p->max_write = TID_RDMA_MAX_WRITE_SEGS_PER_REQ;
  180. p->timeout = qp->timeout;
  181. p->urg = is_urg_masked(priv->rcd);
  182. }
  183. bool tid_rdma_conn_req(struct rvt_qp *qp, u64 *data)
  184. {
  185. struct hfi1_qp_priv *priv = qp->priv;
  186. *data = tid_rdma_opfn_encode(&priv->tid_rdma.local);
  187. return true;
  188. }
  189. bool tid_rdma_conn_reply(struct rvt_qp *qp, u64 data)
  190. {
  191. struct hfi1_qp_priv *priv = qp->priv;
  192. struct tid_rdma_params *remote, *old;
  193. bool ret = true;
  194. old = rcu_dereference_protected(priv->tid_rdma.remote,
  195. lockdep_is_held(&priv->opfn.lock));
  196. data &= ~0xfULL;
  197. /*
  198. * If data passed in is zero, return true so as not to continue the
  199. * negotiation process
  200. */
  201. if (!data || !HFI1_CAP_IS_KSET(TID_RDMA))
  202. goto null;
  203. /*
  204. * If kzalloc fails, return false. This will result in:
  205. * * at the requester a new OPFN request being generated to retry
  206. * the negotiation
  207. * * at the responder, 0 being returned to the requester so as to
  208. * disable TID RDMA at both the requester and the responder
  209. */
  210. remote = kzalloc(sizeof(*remote), GFP_ATOMIC);
  211. if (!remote) {
  212. ret = false;
  213. goto null;
  214. }
  215. tid_rdma_opfn_decode(remote, data);
  216. priv->tid_timer_timeout_jiffies =
  217. usecs_to_jiffies((((4096UL * (1UL << remote->timeout)) /
  218. 1000UL) << 3) * 7);
  219. trace_hfi1_opfn_param(qp, 0, &priv->tid_rdma.local);
  220. trace_hfi1_opfn_param(qp, 1, remote);
  221. rcu_assign_pointer(priv->tid_rdma.remote, remote);
  222. /*
  223. * A TID RDMA READ request's segment size is not equal to
  224. * remote->max_len only when the request's data length is smaller
  225. * than remote->max_len. In that case, there will be only one segment.
  226. * Therefore, when priv->pkts_ps is used to calculate req->cur_seg
  227. * during retry, it will lead to req->cur_seg = 0, which is exactly
  228. * what is expected.
  229. */
  230. priv->pkts_ps = (u16)rvt_div_mtu(qp, remote->max_len);
  231. priv->timeout_shift = ilog2(priv->pkts_ps - 1) + 1;
  232. goto free;
  233. null:
  234. RCU_INIT_POINTER(priv->tid_rdma.remote, NULL);
  235. priv->timeout_shift = 0;
  236. free:
  237. if (old)
  238. kfree_rcu(old, rcu_head);
  239. return ret;
  240. }
  241. bool tid_rdma_conn_resp(struct rvt_qp *qp, u64 *data)
  242. {
  243. bool ret;
  244. ret = tid_rdma_conn_reply(qp, *data);
  245. *data = 0;
  246. /*
  247. * If tid_rdma_conn_reply() returns error, set *data as 0 to indicate
  248. * TID RDMA could not be enabled. This will result in TID RDMA being
  249. * disabled at the requester too.
  250. */
  251. if (ret)
  252. (void)tid_rdma_conn_req(qp, data);
  253. return ret;
  254. }
  255. void tid_rdma_conn_error(struct rvt_qp *qp)
  256. {
  257. struct hfi1_qp_priv *priv = qp->priv;
  258. struct tid_rdma_params *old;
  259. old = rcu_dereference_protected(priv->tid_rdma.remote,
  260. lockdep_is_held(&priv->opfn.lock));
  261. RCU_INIT_POINTER(priv->tid_rdma.remote, NULL);
  262. if (old)
  263. kfree_rcu(old, rcu_head);
  264. }
  265. /* This is called at context initialization time */
  266. int hfi1_kern_exp_rcv_init(struct hfi1_ctxtdata *rcd, int reinit)
  267. {
  268. if (reinit)
  269. return 0;
  270. BUILD_BUG_ON(TID_RDMA_JKEY < HFI1_KERNEL_MIN_JKEY);
  271. BUILD_BUG_ON(TID_RDMA_JKEY > HFI1_KERNEL_MAX_JKEY);
  272. rcd->jkey = TID_RDMA_JKEY;
  273. hfi1_set_ctxt_jkey(rcd->dd, rcd, rcd->jkey);
  274. return hfi1_alloc_ctxt_rcv_groups(rcd);
  275. }
  276. /**
  277. * qp_to_rcd - determine the receive context used by a qp
  278. * @rdi: rvt dev struct
  279. * @qp: the qp
  280. *
  281. * This routine returns the receive context associated
  282. * with a a qp's qpn.
  283. *
  284. * Returns the context.
  285. */
  286. static struct hfi1_ctxtdata *qp_to_rcd(struct rvt_dev_info *rdi,
  287. struct rvt_qp *qp)
  288. {
  289. struct hfi1_ibdev *verbs_dev = container_of(rdi,
  290. struct hfi1_ibdev,
  291. rdi);
  292. struct hfi1_devdata *dd = container_of(verbs_dev,
  293. struct hfi1_devdata,
  294. verbs_dev);
  295. unsigned int ctxt;
  296. if (qp->ibqp.qp_num == 0)
  297. ctxt = 0;
  298. else
  299. ctxt = hfi1_get_qp_map(dd, qp->ibqp.qp_num >> dd->qos_shift);
  300. return dd->rcd[ctxt];
  301. }
  302. int hfi1_qp_priv_init(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  303. struct ib_qp_init_attr *init_attr)
  304. {
  305. struct hfi1_qp_priv *qpriv = qp->priv;
  306. int i, ret;
  307. qpriv->rcd = qp_to_rcd(rdi, qp);
  308. spin_lock_init(&qpriv->opfn.lock);
  309. INIT_WORK(&qpriv->opfn.opfn_work, opfn_send_conn_request);
  310. INIT_WORK(&qpriv->tid_rdma.trigger_work, tid_rdma_trigger_resume);
  311. qpriv->flow_state.psn = 0;
  312. qpriv->flow_state.index = RXE_NUM_TID_FLOWS;
  313. qpriv->flow_state.last_index = RXE_NUM_TID_FLOWS;
  314. qpriv->flow_state.generation = KERN_GENERATION_RESERVED;
  315. qpriv->s_state = TID_OP(WRITE_RESP);
  316. qpriv->s_tid_cur = HFI1_QP_WQE_INVALID;
  317. qpriv->s_tid_head = HFI1_QP_WQE_INVALID;
  318. qpriv->s_tid_tail = HFI1_QP_WQE_INVALID;
  319. qpriv->rnr_nak_state = TID_RNR_NAK_INIT;
  320. qpriv->r_tid_head = HFI1_QP_WQE_INVALID;
  321. qpriv->r_tid_tail = HFI1_QP_WQE_INVALID;
  322. qpriv->r_tid_ack = HFI1_QP_WQE_INVALID;
  323. qpriv->r_tid_alloc = HFI1_QP_WQE_INVALID;
  324. atomic_set(&qpriv->n_requests, 0);
  325. atomic_set(&qpriv->n_tid_requests, 0);
  326. timer_setup(&qpriv->s_tid_timer, hfi1_tid_timeout, 0);
  327. timer_setup(&qpriv->s_tid_retry_timer, hfi1_tid_retry_timeout, 0);
  328. INIT_LIST_HEAD(&qpriv->tid_wait);
  329. if (init_attr->qp_type == IB_QPT_RC && HFI1_CAP_IS_KSET(TID_RDMA)) {
  330. struct hfi1_devdata *dd = qpriv->rcd->dd;
  331. qpriv->pages = kzalloc_node(TID_RDMA_MAX_PAGES *
  332. sizeof(*qpriv->pages),
  333. GFP_KERNEL, dd->node);
  334. if (!qpriv->pages)
  335. return -ENOMEM;
  336. for (i = 0; i < qp->s_size; i++) {
  337. struct hfi1_swqe_priv *priv;
  338. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, i);
  339. priv = kzalloc_node(sizeof(*priv), GFP_KERNEL,
  340. dd->node);
  341. if (!priv)
  342. return -ENOMEM;
  343. hfi1_init_trdma_req(qp, &priv->tid_req);
  344. priv->tid_req.e.swqe = wqe;
  345. wqe->priv = priv;
  346. }
  347. for (i = 0; i < rvt_max_atomic(rdi); i++) {
  348. struct hfi1_ack_priv *priv;
  349. priv = kzalloc_node(sizeof(*priv), GFP_KERNEL,
  350. dd->node);
  351. if (!priv)
  352. return -ENOMEM;
  353. hfi1_init_trdma_req(qp, &priv->tid_req);
  354. priv->tid_req.e.ack = &qp->s_ack_queue[i];
  355. ret = hfi1_kern_exp_rcv_alloc_flows(&priv->tid_req,
  356. GFP_KERNEL);
  357. if (ret) {
  358. kfree(priv);
  359. return ret;
  360. }
  361. qp->s_ack_queue[i].priv = priv;
  362. }
  363. }
  364. return 0;
  365. }
  366. void hfi1_qp_priv_tid_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  367. {
  368. struct hfi1_qp_priv *qpriv = qp->priv;
  369. struct rvt_swqe *wqe;
  370. u32 i;
  371. if (qp->ibqp.qp_type == IB_QPT_RC && HFI1_CAP_IS_KSET(TID_RDMA)) {
  372. for (i = 0; i < qp->s_size; i++) {
  373. wqe = rvt_get_swqe_ptr(qp, i);
  374. kfree(wqe->priv);
  375. wqe->priv = NULL;
  376. }
  377. for (i = 0; i < rvt_max_atomic(rdi); i++) {
  378. struct hfi1_ack_priv *priv = qp->s_ack_queue[i].priv;
  379. if (priv)
  380. hfi1_kern_exp_rcv_free_flows(&priv->tid_req);
  381. kfree(priv);
  382. qp->s_ack_queue[i].priv = NULL;
  383. }
  384. cancel_work_sync(&qpriv->opfn.opfn_work);
  385. kfree(qpriv->pages);
  386. qpriv->pages = NULL;
  387. }
  388. }
  389. /* Flow and tid waiter functions */
  390. /**
  391. * DOC: lock ordering
  392. *
  393. * There are two locks involved with the queuing
  394. * routines: the qp s_lock and the exp_lock.
  395. *
  396. * Since the tid space allocation is called from
  397. * the send engine, the qp s_lock is already held.
  398. *
  399. * The allocation routines will get the exp_lock.
  400. *
  401. * The first_qp() call is provided to allow the head of
  402. * the rcd wait queue to be fetched under the exp_lock and
  403. * followed by a drop of the exp_lock.
  404. *
  405. * Any qp in the wait list will have the qp reference count held
  406. * to hold the qp in memory.
  407. */
  408. /*
  409. * return head of rcd wait list
  410. *
  411. * Must hold the exp_lock.
  412. *
  413. * Get a reference to the QP to hold the QP in memory.
  414. *
  415. * The caller must release the reference when the local
  416. * is no longer being used.
  417. */
  418. static struct rvt_qp *first_qp(struct hfi1_ctxtdata *rcd,
  419. struct tid_queue *queue)
  420. __must_hold(&rcd->exp_lock)
  421. {
  422. struct hfi1_qp_priv *priv;
  423. lockdep_assert_held(&rcd->exp_lock);
  424. priv = list_first_entry_or_null(&queue->queue_head,
  425. struct hfi1_qp_priv,
  426. tid_wait);
  427. if (!priv)
  428. return NULL;
  429. rvt_get_qp(priv->owner);
  430. return priv->owner;
  431. }
  432. /**
  433. * kernel_tid_waiters - determine rcd wait
  434. * @rcd: the receive context
  435. * @queue: the queue to operate on
  436. * @qp: the head of the qp being processed
  437. *
  438. * This routine will return false IFF
  439. * the list is NULL or the head of the
  440. * list is the indicated qp.
  441. *
  442. * Must hold the qp s_lock and the exp_lock.
  443. *
  444. * Return:
  445. * false if either of the conditions below are satisfied:
  446. * 1. The list is empty or
  447. * 2. The indicated qp is at the head of the list and the
  448. * HFI1_S_WAIT_TID_SPACE bit is set in qp->s_flags.
  449. * true is returned otherwise.
  450. */
  451. static bool kernel_tid_waiters(struct hfi1_ctxtdata *rcd,
  452. struct tid_queue *queue, struct rvt_qp *qp)
  453. __must_hold(&rcd->exp_lock) __must_hold(&qp->s_lock)
  454. {
  455. struct rvt_qp *fqp;
  456. bool ret = true;
  457. lockdep_assert_held(&qp->s_lock);
  458. lockdep_assert_held(&rcd->exp_lock);
  459. fqp = first_qp(rcd, queue);
  460. if (!fqp || (fqp == qp && (qp->s_flags & HFI1_S_WAIT_TID_SPACE)))
  461. ret = false;
  462. rvt_put_qp(fqp);
  463. return ret;
  464. }
  465. /**
  466. * dequeue_tid_waiter - dequeue the qp from the list
  467. * @rcd: the receive context
  468. * @queue: the queue to operate on
  469. * @qp: the qp to remove the wait list
  470. *
  471. * This routine removes the indicated qp from the
  472. * wait list if it is there.
  473. *
  474. * This should be done after the hardware flow and
  475. * tid array resources have been allocated.
  476. *
  477. * Must hold the qp s_lock and the rcd exp_lock.
  478. *
  479. * It assumes the s_lock to protect the s_flags
  480. * field and to reliably test the HFI1_S_WAIT_TID_SPACE flag.
  481. */
  482. static void dequeue_tid_waiter(struct hfi1_ctxtdata *rcd,
  483. struct tid_queue *queue, struct rvt_qp *qp)
  484. __must_hold(&rcd->exp_lock) __must_hold(&qp->s_lock)
  485. {
  486. struct hfi1_qp_priv *priv = qp->priv;
  487. lockdep_assert_held(&qp->s_lock);
  488. lockdep_assert_held(&rcd->exp_lock);
  489. if (list_empty(&priv->tid_wait))
  490. return;
  491. list_del_init(&priv->tid_wait);
  492. qp->s_flags &= ~HFI1_S_WAIT_TID_SPACE;
  493. queue->dequeue++;
  494. rvt_put_qp(qp);
  495. }
  496. /**
  497. * queue_qp_for_tid_wait - suspend QP on tid space
  498. * @rcd: the receive context
  499. * @queue: the queue to operate on
  500. * @qp: the qp
  501. *
  502. * The qp is inserted at the tail of the rcd
  503. * wait queue and the HFI1_S_WAIT_TID_SPACE s_flag is set.
  504. *
  505. * Must hold the qp s_lock and the exp_lock.
  506. */
  507. static void queue_qp_for_tid_wait(struct hfi1_ctxtdata *rcd,
  508. struct tid_queue *queue, struct rvt_qp *qp)
  509. __must_hold(&rcd->exp_lock) __must_hold(&qp->s_lock)
  510. {
  511. struct hfi1_qp_priv *priv = qp->priv;
  512. lockdep_assert_held(&qp->s_lock);
  513. lockdep_assert_held(&rcd->exp_lock);
  514. if (list_empty(&priv->tid_wait)) {
  515. qp->s_flags |= HFI1_S_WAIT_TID_SPACE;
  516. list_add_tail(&priv->tid_wait, &queue->queue_head);
  517. priv->tid_enqueue = ++queue->enqueue;
  518. rcd->dd->verbs_dev.n_tidwait++;
  519. trace_hfi1_qpsleep(qp, HFI1_S_WAIT_TID_SPACE);
  520. rvt_get_qp(qp);
  521. }
  522. }
  523. /**
  524. * __trigger_tid_waiter - trigger tid waiter
  525. * @qp: the qp
  526. *
  527. * This is a private entrance to schedule the qp
  528. * assuming the caller is holding the qp->s_lock.
  529. */
  530. static void __trigger_tid_waiter(struct rvt_qp *qp)
  531. __must_hold(&qp->s_lock)
  532. {
  533. lockdep_assert_held(&qp->s_lock);
  534. if (!(qp->s_flags & HFI1_S_WAIT_TID_SPACE))
  535. return;
  536. trace_hfi1_qpwakeup(qp, HFI1_S_WAIT_TID_SPACE);
  537. hfi1_schedule_send(qp);
  538. }
  539. /**
  540. * tid_rdma_schedule_tid_wakeup - schedule wakeup for a qp
  541. * @qp: the qp
  542. *
  543. * trigger a schedule or a waiting qp in a deadlock
  544. * safe manner. The qp reference is held prior
  545. * to this call via first_qp().
  546. *
  547. * If the qp trigger was already scheduled (!rval)
  548. * the reference is dropped, otherwise the resume
  549. * or the destroy cancel will dispatch the reference.
  550. */
  551. static void tid_rdma_schedule_tid_wakeup(struct rvt_qp *qp)
  552. {
  553. struct hfi1_qp_priv *priv;
  554. struct hfi1_ibport *ibp;
  555. struct hfi1_pportdata *ppd;
  556. struct hfi1_devdata *dd;
  557. bool rval;
  558. if (!qp)
  559. return;
  560. priv = qp->priv;
  561. ibp = to_iport(qp->ibqp.device, qp->port_num);
  562. ppd = ppd_from_ibp(ibp);
  563. dd = dd_from_ibdev(qp->ibqp.device);
  564. rval = queue_work_on(priv->s_sde ?
  565. priv->s_sde->cpu :
  566. cpumask_first(cpumask_of_node(dd->node)),
  567. ppd->hfi1_wq,
  568. &priv->tid_rdma.trigger_work);
  569. if (!rval)
  570. rvt_put_qp(qp);
  571. }
  572. /**
  573. * tid_rdma_trigger_resume - field a trigger work request
  574. * @work: the work item
  575. *
  576. * Complete the off qp trigger processing by directly
  577. * calling the progress routine.
  578. */
  579. static void tid_rdma_trigger_resume(struct work_struct *work)
  580. {
  581. struct tid_rdma_qp_params *tr;
  582. struct hfi1_qp_priv *priv;
  583. struct rvt_qp *qp;
  584. tr = container_of(work, struct tid_rdma_qp_params, trigger_work);
  585. priv = container_of(tr, struct hfi1_qp_priv, tid_rdma);
  586. qp = priv->owner;
  587. spin_lock_irq(&qp->s_lock);
  588. if (qp->s_flags & HFI1_S_WAIT_TID_SPACE) {
  589. spin_unlock_irq(&qp->s_lock);
  590. hfi1_do_send(priv->owner, true);
  591. } else {
  592. spin_unlock_irq(&qp->s_lock);
  593. }
  594. rvt_put_qp(qp);
  595. }
  596. /*
  597. * tid_rdma_flush_wait - unwind any tid space wait
  598. *
  599. * This is called when resetting a qp to
  600. * allow a destroy or reset to get rid
  601. * of any tid space linkage and reference counts.
  602. */
  603. static void _tid_rdma_flush_wait(struct rvt_qp *qp, struct tid_queue *queue)
  604. __must_hold(&qp->s_lock)
  605. {
  606. struct hfi1_qp_priv *priv;
  607. if (!qp)
  608. return;
  609. lockdep_assert_held(&qp->s_lock);
  610. priv = qp->priv;
  611. qp->s_flags &= ~HFI1_S_WAIT_TID_SPACE;
  612. spin_lock(&priv->rcd->exp_lock);
  613. if (!list_empty(&priv->tid_wait)) {
  614. list_del_init(&priv->tid_wait);
  615. qp->s_flags &= ~HFI1_S_WAIT_TID_SPACE;
  616. queue->dequeue++;
  617. rvt_put_qp(qp);
  618. }
  619. spin_unlock(&priv->rcd->exp_lock);
  620. }
  621. void hfi1_tid_rdma_flush_wait(struct rvt_qp *qp)
  622. __must_hold(&qp->s_lock)
  623. {
  624. struct hfi1_qp_priv *priv = qp->priv;
  625. _tid_rdma_flush_wait(qp, &priv->rcd->flow_queue);
  626. _tid_rdma_flush_wait(qp, &priv->rcd->rarr_queue);
  627. }
  628. /* Flow functions */
  629. /**
  630. * kern_reserve_flow - allocate a hardware flow
  631. * @rcd: the context to use for allocation
  632. * @last: the index of the preferred flow. Use RXE_NUM_TID_FLOWS to
  633. * signify "don't care".
  634. *
  635. * Use a bit mask based allocation to reserve a hardware
  636. * flow for use in receiving KDETH data packets. If a preferred flow is
  637. * specified the function will attempt to reserve that flow again, if
  638. * available.
  639. *
  640. * The exp_lock must be held.
  641. *
  642. * Return:
  643. * On success: a value postive value between 0 and RXE_NUM_TID_FLOWS - 1
  644. * On failure: -EAGAIN
  645. */
  646. static int kern_reserve_flow(struct hfi1_ctxtdata *rcd, int last)
  647. __must_hold(&rcd->exp_lock)
  648. {
  649. int nr;
  650. /* Attempt to reserve the preferred flow index */
  651. if (last >= 0 && last < RXE_NUM_TID_FLOWS &&
  652. !test_and_set_bit(last, &rcd->flow_mask))
  653. return last;
  654. nr = ffz(rcd->flow_mask);
  655. BUILD_BUG_ON(RXE_NUM_TID_FLOWS >=
  656. (sizeof(rcd->flow_mask) * BITS_PER_BYTE));
  657. if (nr > (RXE_NUM_TID_FLOWS - 1))
  658. return -EAGAIN;
  659. set_bit(nr, &rcd->flow_mask);
  660. return nr;
  661. }
  662. static void kern_set_hw_flow(struct hfi1_ctxtdata *rcd, u32 generation,
  663. u32 flow_idx)
  664. {
  665. u64 reg;
  666. reg = ((u64)generation << HFI1_KDETH_BTH_SEQ_SHIFT) |
  667. RCV_TID_FLOW_TABLE_CTRL_FLOW_VALID_SMASK |
  668. RCV_TID_FLOW_TABLE_CTRL_KEEP_AFTER_SEQ_ERR_SMASK |
  669. RCV_TID_FLOW_TABLE_CTRL_KEEP_ON_GEN_ERR_SMASK |
  670. RCV_TID_FLOW_TABLE_STATUS_SEQ_MISMATCH_SMASK |
  671. RCV_TID_FLOW_TABLE_STATUS_GEN_MISMATCH_SMASK;
  672. if (generation != KERN_GENERATION_RESERVED)
  673. reg |= RCV_TID_FLOW_TABLE_CTRL_HDR_SUPP_EN_SMASK;
  674. write_uctxt_csr(rcd->dd, rcd->ctxt,
  675. RCV_TID_FLOW_TABLE + 8 * flow_idx, reg);
  676. }
  677. static u32 kern_setup_hw_flow(struct hfi1_ctxtdata *rcd, u32 flow_idx)
  678. __must_hold(&rcd->exp_lock)
  679. {
  680. u32 generation = rcd->flows[flow_idx].generation;
  681. kern_set_hw_flow(rcd, generation, flow_idx);
  682. return generation;
  683. }
  684. static u32 kern_flow_generation_next(u32 gen)
  685. {
  686. u32 generation = mask_generation(gen + 1);
  687. if (generation == KERN_GENERATION_RESERVED)
  688. generation = mask_generation(generation + 1);
  689. return generation;
  690. }
  691. static void kern_clear_hw_flow(struct hfi1_ctxtdata *rcd, u32 flow_idx)
  692. __must_hold(&rcd->exp_lock)
  693. {
  694. rcd->flows[flow_idx].generation =
  695. kern_flow_generation_next(rcd->flows[flow_idx].generation);
  696. kern_set_hw_flow(rcd, KERN_GENERATION_RESERVED, flow_idx);
  697. }
  698. int hfi1_kern_setup_hw_flow(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp)
  699. {
  700. struct hfi1_qp_priv *qpriv = (struct hfi1_qp_priv *)qp->priv;
  701. struct tid_flow_state *fs = &qpriv->flow_state;
  702. struct rvt_qp *fqp;
  703. unsigned long flags;
  704. int ret = 0;
  705. /* The QP already has an allocated flow */
  706. if (fs->index != RXE_NUM_TID_FLOWS)
  707. return ret;
  708. spin_lock_irqsave(&rcd->exp_lock, flags);
  709. if (kernel_tid_waiters(rcd, &rcd->flow_queue, qp))
  710. goto queue;
  711. ret = kern_reserve_flow(rcd, fs->last_index);
  712. if (ret < 0)
  713. goto queue;
  714. fs->index = ret;
  715. fs->last_index = fs->index;
  716. /* Generation received in a RESYNC overrides default flow generation */
  717. if (fs->generation != KERN_GENERATION_RESERVED)
  718. rcd->flows[fs->index].generation = fs->generation;
  719. fs->generation = kern_setup_hw_flow(rcd, fs->index);
  720. fs->psn = 0;
  721. dequeue_tid_waiter(rcd, &rcd->flow_queue, qp);
  722. /* get head before dropping lock */
  723. fqp = first_qp(rcd, &rcd->flow_queue);
  724. spin_unlock_irqrestore(&rcd->exp_lock, flags);
  725. tid_rdma_schedule_tid_wakeup(fqp);
  726. return 0;
  727. queue:
  728. queue_qp_for_tid_wait(rcd, &rcd->flow_queue, qp);
  729. spin_unlock_irqrestore(&rcd->exp_lock, flags);
  730. return -EAGAIN;
  731. }
  732. void hfi1_kern_clear_hw_flow(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp)
  733. {
  734. struct hfi1_qp_priv *qpriv = (struct hfi1_qp_priv *)qp->priv;
  735. struct tid_flow_state *fs = &qpriv->flow_state;
  736. struct rvt_qp *fqp;
  737. unsigned long flags;
  738. if (fs->index >= RXE_NUM_TID_FLOWS)
  739. return;
  740. spin_lock_irqsave(&rcd->exp_lock, flags);
  741. kern_clear_hw_flow(rcd, fs->index);
  742. clear_bit(fs->index, &rcd->flow_mask);
  743. fs->index = RXE_NUM_TID_FLOWS;
  744. fs->psn = 0;
  745. fs->generation = KERN_GENERATION_RESERVED;
  746. /* get head before dropping lock */
  747. fqp = first_qp(rcd, &rcd->flow_queue);
  748. spin_unlock_irqrestore(&rcd->exp_lock, flags);
  749. if (fqp == qp) {
  750. __trigger_tid_waiter(fqp);
  751. rvt_put_qp(fqp);
  752. } else {
  753. tid_rdma_schedule_tid_wakeup(fqp);
  754. }
  755. }
  756. void hfi1_kern_init_ctxt_generations(struct hfi1_ctxtdata *rcd)
  757. {
  758. int i;
  759. for (i = 0; i < RXE_NUM_TID_FLOWS; i++) {
  760. rcd->flows[i].generation = mask_generation(get_random_u32());
  761. kern_set_hw_flow(rcd, KERN_GENERATION_RESERVED, i);
  762. }
  763. }
  764. /* TID allocation functions */
  765. static u8 trdma_pset_order(struct tid_rdma_pageset *s)
  766. {
  767. u8 count = s->count;
  768. return ilog2(count) + 1;
  769. }
  770. /**
  771. * tid_rdma_find_phys_blocks_4k - get groups base on mr info
  772. * @flow: overall info for a TID RDMA segment
  773. * @pages: pointer to an array of page structs
  774. * @npages: number of pages
  775. * @list: page set array to return
  776. *
  777. * This routine returns the number of groups associated with
  778. * the current sge information. This implementation is based
  779. * on the expected receive find_phys_blocks() adjusted to
  780. * use the MR information vs. the pfn.
  781. *
  782. * Return:
  783. * the number of RcvArray entries
  784. */
  785. static u32 tid_rdma_find_phys_blocks_4k(struct tid_rdma_flow *flow,
  786. struct page **pages,
  787. u32 npages,
  788. struct tid_rdma_pageset *list)
  789. {
  790. u32 pagecount, pageidx, setcount = 0, i;
  791. void *vaddr, *this_vaddr;
  792. if (!npages)
  793. return 0;
  794. /*
  795. * Look for sets of physically contiguous pages in the user buffer.
  796. * This will allow us to optimize Expected RcvArray entry usage by
  797. * using the bigger supported sizes.
  798. */
  799. vaddr = page_address(pages[0]);
  800. trace_hfi1_tid_flow_page(flow->req->qp, flow, 0, 0, 0, vaddr);
  801. for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
  802. this_vaddr = i < npages ? page_address(pages[i]) : NULL;
  803. trace_hfi1_tid_flow_page(flow->req->qp, flow, i, 0, 0,
  804. this_vaddr);
  805. /*
  806. * If the vaddr's are not sequential, pages are not physically
  807. * contiguous.
  808. */
  809. if (this_vaddr != (vaddr + PAGE_SIZE)) {
  810. /*
  811. * At this point we have to loop over the set of
  812. * physically contiguous pages and break them down it
  813. * sizes supported by the HW.
  814. * There are two main constraints:
  815. * 1. The max buffer size is MAX_EXPECTED_BUFFER.
  816. * If the total set size is bigger than that
  817. * program only a MAX_EXPECTED_BUFFER chunk.
  818. * 2. The buffer size has to be a power of two. If
  819. * it is not, round down to the closes power of
  820. * 2 and program that size.
  821. */
  822. while (pagecount) {
  823. int maxpages = pagecount;
  824. u32 bufsize = pagecount * PAGE_SIZE;
  825. if (bufsize > MAX_EXPECTED_BUFFER)
  826. maxpages =
  827. MAX_EXPECTED_BUFFER >>
  828. PAGE_SHIFT;
  829. else if (!is_power_of_2(bufsize))
  830. maxpages =
  831. rounddown_pow_of_two(bufsize) >>
  832. PAGE_SHIFT;
  833. list[setcount].idx = pageidx;
  834. list[setcount].count = maxpages;
  835. trace_hfi1_tid_pageset(flow->req->qp, setcount,
  836. list[setcount].idx,
  837. list[setcount].count);
  838. pagecount -= maxpages;
  839. pageidx += maxpages;
  840. setcount++;
  841. }
  842. pageidx = i;
  843. pagecount = 1;
  844. vaddr = this_vaddr;
  845. } else {
  846. vaddr += PAGE_SIZE;
  847. pagecount++;
  848. }
  849. }
  850. /* insure we always return an even number of sets */
  851. if (setcount & 1)
  852. list[setcount++].count = 0;
  853. return setcount;
  854. }
  855. /**
  856. * tid_flush_pages - dump out pages into pagesets
  857. * @list: list of pagesets
  858. * @idx: pointer to current page index
  859. * @pages: number of pages to dump
  860. * @sets: current number of pagesset
  861. *
  862. * This routine flushes out accumuated pages.
  863. *
  864. * To insure an even number of sets the
  865. * code may add a filler.
  866. *
  867. * This can happen with when pages is not
  868. * a power of 2 or pages is a power of 2
  869. * less than the maximum pages.
  870. *
  871. * Return:
  872. * The new number of sets
  873. */
  874. static u32 tid_flush_pages(struct tid_rdma_pageset *list,
  875. u32 *idx, u32 pages, u32 sets)
  876. {
  877. while (pages) {
  878. u32 maxpages = pages;
  879. if (maxpages > MAX_EXPECTED_PAGES)
  880. maxpages = MAX_EXPECTED_PAGES;
  881. else if (!is_power_of_2(maxpages))
  882. maxpages = rounddown_pow_of_two(maxpages);
  883. list[sets].idx = *idx;
  884. list[sets++].count = maxpages;
  885. *idx += maxpages;
  886. pages -= maxpages;
  887. }
  888. /* might need a filler */
  889. if (sets & 1)
  890. list[sets++].count = 0;
  891. return sets;
  892. }
  893. /**
  894. * tid_rdma_find_phys_blocks_8k - get groups base on mr info
  895. * @flow: overall info for a TID RDMA segment
  896. * @pages: pointer to an array of page structs
  897. * @npages: number of pages
  898. * @list: page set array to return
  899. *
  900. * This routine parses an array of pages to compute pagesets
  901. * in an 8k compatible way.
  902. *
  903. * pages are tested two at a time, i, i + 1 for contiguous
  904. * pages and i - 1 and i contiguous pages.
  905. *
  906. * If any condition is false, any accumlated pages are flushed and
  907. * v0,v1 are emitted as separate PAGE_SIZE pagesets
  908. *
  909. * Otherwise, the current 8k is totaled for a future flush.
  910. *
  911. * Return:
  912. * The number of pagesets
  913. * list set with the returned number of pagesets
  914. *
  915. */
  916. static u32 tid_rdma_find_phys_blocks_8k(struct tid_rdma_flow *flow,
  917. struct page **pages,
  918. u32 npages,
  919. struct tid_rdma_pageset *list)
  920. {
  921. u32 idx, sets = 0, i;
  922. u32 pagecnt = 0;
  923. void *v0, *v1, *vm1;
  924. if (!npages)
  925. return 0;
  926. for (idx = 0, i = 0, vm1 = NULL; i < npages; i += 2) {
  927. /* get a new v0 */
  928. v0 = page_address(pages[i]);
  929. trace_hfi1_tid_flow_page(flow->req->qp, flow, i, 1, 0, v0);
  930. v1 = i + 1 < npages ?
  931. page_address(pages[i + 1]) : NULL;
  932. trace_hfi1_tid_flow_page(flow->req->qp, flow, i, 1, 1, v1);
  933. /* compare i, i + 1 vaddr */
  934. if (v1 != (v0 + PAGE_SIZE)) {
  935. /* flush out pages */
  936. sets = tid_flush_pages(list, &idx, pagecnt, sets);
  937. /* output v0,v1 as two pagesets */
  938. list[sets].idx = idx++;
  939. list[sets++].count = 1;
  940. if (v1) {
  941. list[sets].count = 1;
  942. list[sets++].idx = idx++;
  943. } else {
  944. list[sets++].count = 0;
  945. }
  946. vm1 = NULL;
  947. pagecnt = 0;
  948. continue;
  949. }
  950. /* i,i+1 consecutive, look at i-1,i */
  951. if (vm1 && v0 != (vm1 + PAGE_SIZE)) {
  952. /* flush out pages */
  953. sets = tid_flush_pages(list, &idx, pagecnt, sets);
  954. pagecnt = 0;
  955. }
  956. /* pages will always be a multiple of 8k */
  957. pagecnt += 2;
  958. /* save i-1 */
  959. vm1 = v1;
  960. /* move to next pair */
  961. }
  962. /* dump residual pages at end */
  963. sets = tid_flush_pages(list, &idx, npages - idx, sets);
  964. /* by design cannot be odd sets */
  965. WARN_ON(sets & 1);
  966. return sets;
  967. }
  968. /*
  969. * Find pages for one segment of a sge array represented by @ss. The function
  970. * does not check the sge, the sge must have been checked for alignment with a
  971. * prior call to hfi1_kern_trdma_ok. Other sge checking is done as part of
  972. * rvt_lkey_ok and rvt_rkey_ok. Also, the function only modifies the local sge
  973. * copy maintained in @ss->sge, the original sge is not modified.
  974. *
  975. * Unlike IB RDMA WRITE, we can't decrement ss->num_sge here because we are not
  976. * releasing the MR reference count at the same time. Otherwise, we'll "leak"
  977. * references to the MR. This difference requires that we keep track of progress
  978. * into the sg_list. This is done by the cur_seg cursor in the tid_rdma_request
  979. * structure.
  980. */
  981. static u32 kern_find_pages(struct tid_rdma_flow *flow,
  982. struct page **pages,
  983. struct rvt_sge_state *ss, bool *last)
  984. {
  985. struct tid_rdma_request *req = flow->req;
  986. struct rvt_sge *sge = &ss->sge;
  987. u32 length = flow->req->seg_len;
  988. u32 len = PAGE_SIZE;
  989. u32 i = 0;
  990. while (length && req->isge < ss->num_sge) {
  991. pages[i++] = virt_to_page(sge->vaddr);
  992. sge->vaddr += len;
  993. sge->length -= len;
  994. sge->sge_length -= len;
  995. if (!sge->sge_length) {
  996. if (++req->isge < ss->num_sge)
  997. *sge = ss->sg_list[req->isge - 1];
  998. } else if (sge->length == 0 && sge->mr->lkey) {
  999. if (++sge->n >= RVT_SEGSZ) {
  1000. ++sge->m;
  1001. sge->n = 0;
  1002. }
  1003. sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
  1004. sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
  1005. }
  1006. length -= len;
  1007. }
  1008. flow->length = flow->req->seg_len - length;
  1009. *last = req->isge != ss->num_sge;
  1010. return i;
  1011. }
  1012. static void dma_unmap_flow(struct tid_rdma_flow *flow)
  1013. {
  1014. struct hfi1_devdata *dd;
  1015. int i;
  1016. struct tid_rdma_pageset *pset;
  1017. dd = flow->req->rcd->dd;
  1018. for (i = 0, pset = &flow->pagesets[0]; i < flow->npagesets;
  1019. i++, pset++) {
  1020. if (pset->count && pset->addr) {
  1021. dma_unmap_page(&dd->pcidev->dev,
  1022. pset->addr,
  1023. PAGE_SIZE * pset->count,
  1024. DMA_FROM_DEVICE);
  1025. pset->mapped = 0;
  1026. }
  1027. }
  1028. }
  1029. static int dma_map_flow(struct tid_rdma_flow *flow, struct page **pages)
  1030. {
  1031. int i;
  1032. struct hfi1_devdata *dd = flow->req->rcd->dd;
  1033. struct tid_rdma_pageset *pset;
  1034. for (i = 0, pset = &flow->pagesets[0]; i < flow->npagesets;
  1035. i++, pset++) {
  1036. if (pset->count) {
  1037. pset->addr = dma_map_page(&dd->pcidev->dev,
  1038. pages[pset->idx],
  1039. 0,
  1040. PAGE_SIZE * pset->count,
  1041. DMA_FROM_DEVICE);
  1042. if (dma_mapping_error(&dd->pcidev->dev, pset->addr)) {
  1043. dma_unmap_flow(flow);
  1044. return -ENOMEM;
  1045. }
  1046. pset->mapped = 1;
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. static inline bool dma_mapped(struct tid_rdma_flow *flow)
  1052. {
  1053. return !!flow->pagesets[0].mapped;
  1054. }
  1055. /*
  1056. * Get pages pointers and identify contiguous physical memory chunks for a
  1057. * segment. All segments are of length flow->req->seg_len.
  1058. */
  1059. static int kern_get_phys_blocks(struct tid_rdma_flow *flow,
  1060. struct page **pages,
  1061. struct rvt_sge_state *ss, bool *last)
  1062. {
  1063. u8 npages;
  1064. /* Reuse previously computed pagesets, if any */
  1065. if (flow->npagesets) {
  1066. trace_hfi1_tid_flow_alloc(flow->req->qp, flow->req->setup_head,
  1067. flow);
  1068. if (!dma_mapped(flow))
  1069. return dma_map_flow(flow, pages);
  1070. return 0;
  1071. }
  1072. npages = kern_find_pages(flow, pages, ss, last);
  1073. if (flow->req->qp->pmtu == enum_to_mtu(OPA_MTU_4096))
  1074. flow->npagesets =
  1075. tid_rdma_find_phys_blocks_4k(flow, pages, npages,
  1076. flow->pagesets);
  1077. else
  1078. flow->npagesets =
  1079. tid_rdma_find_phys_blocks_8k(flow, pages, npages,
  1080. flow->pagesets);
  1081. return dma_map_flow(flow, pages);
  1082. }
  1083. static inline void kern_add_tid_node(struct tid_rdma_flow *flow,
  1084. struct hfi1_ctxtdata *rcd, char *s,
  1085. struct tid_group *grp, u8 cnt)
  1086. {
  1087. struct kern_tid_node *node = &flow->tnode[flow->tnode_cnt++];
  1088. WARN_ON_ONCE(flow->tnode_cnt >=
  1089. (TID_RDMA_MAX_SEGMENT_SIZE >> PAGE_SHIFT));
  1090. if (WARN_ON_ONCE(cnt & 1))
  1091. dd_dev_err(rcd->dd,
  1092. "unexpected odd allocation cnt %u map 0x%x used %u",
  1093. cnt, grp->map, grp->used);
  1094. node->grp = grp;
  1095. node->map = grp->map;
  1096. node->cnt = cnt;
  1097. trace_hfi1_tid_node_add(flow->req->qp, s, flow->tnode_cnt - 1,
  1098. grp->base, grp->map, grp->used, cnt);
  1099. }
  1100. /*
  1101. * Try to allocate pageset_count TID's from TID groups for a context
  1102. *
  1103. * This function allocates TID's without moving groups between lists or
  1104. * modifying grp->map. This is done as follows, being cogizant of the lists
  1105. * between which the TID groups will move:
  1106. * 1. First allocate complete groups of 8 TID's since this is more efficient,
  1107. * these groups will move from group->full without affecting used
  1108. * 2. If more TID's are needed allocate from used (will move from used->full or
  1109. * stay in used)
  1110. * 3. If we still don't have the required number of TID's go back and look again
  1111. * at a complete group (will move from group->used)
  1112. */
  1113. static int kern_alloc_tids(struct tid_rdma_flow *flow)
  1114. {
  1115. struct hfi1_ctxtdata *rcd = flow->req->rcd;
  1116. struct hfi1_devdata *dd = rcd->dd;
  1117. u32 ngroups, pageidx = 0;
  1118. struct tid_group *group = NULL, *used;
  1119. u8 use;
  1120. flow->tnode_cnt = 0;
  1121. ngroups = flow->npagesets / dd->rcv_entries.group_size;
  1122. if (!ngroups)
  1123. goto used_list;
  1124. /* First look at complete groups */
  1125. list_for_each_entry(group, &rcd->tid_group_list.list, list) {
  1126. kern_add_tid_node(flow, rcd, "complete groups", group,
  1127. group->size);
  1128. pageidx += group->size;
  1129. if (!--ngroups)
  1130. break;
  1131. }
  1132. if (pageidx >= flow->npagesets)
  1133. goto ok;
  1134. used_list:
  1135. /* Now look at partially used groups */
  1136. list_for_each_entry(used, &rcd->tid_used_list.list, list) {
  1137. use = min_t(u32, flow->npagesets - pageidx,
  1138. used->size - used->used);
  1139. kern_add_tid_node(flow, rcd, "used groups", used, use);
  1140. pageidx += use;
  1141. if (pageidx >= flow->npagesets)
  1142. goto ok;
  1143. }
  1144. /*
  1145. * Look again at a complete group, continuing from where we left.
  1146. * However, if we are at the head, we have reached the end of the
  1147. * complete groups list from the first loop above
  1148. */
  1149. if (group && &group->list == &rcd->tid_group_list.list)
  1150. goto bail_eagain;
  1151. group = list_prepare_entry(group, &rcd->tid_group_list.list,
  1152. list);
  1153. if (list_is_last(&group->list, &rcd->tid_group_list.list))
  1154. goto bail_eagain;
  1155. group = list_next_entry(group, list);
  1156. use = min_t(u32, flow->npagesets - pageidx, group->size);
  1157. kern_add_tid_node(flow, rcd, "complete continue", group, use);
  1158. pageidx += use;
  1159. if (pageidx >= flow->npagesets)
  1160. goto ok;
  1161. bail_eagain:
  1162. trace_hfi1_msg_alloc_tids(flow->req->qp, " insufficient tids: needed ",
  1163. (u64)flow->npagesets);
  1164. return -EAGAIN;
  1165. ok:
  1166. return 0;
  1167. }
  1168. static void kern_program_rcv_group(struct tid_rdma_flow *flow, int grp_num,
  1169. u32 *pset_idx)
  1170. {
  1171. struct hfi1_ctxtdata *rcd = flow->req->rcd;
  1172. struct hfi1_devdata *dd = rcd->dd;
  1173. struct kern_tid_node *node = &flow->tnode[grp_num];
  1174. struct tid_group *grp = node->grp;
  1175. struct tid_rdma_pageset *pset;
  1176. u32 pmtu_pg = flow->req->qp->pmtu >> PAGE_SHIFT;
  1177. u32 rcventry, npages = 0, pair = 0, tidctrl;
  1178. u8 i, cnt = 0;
  1179. for (i = 0; i < grp->size; i++) {
  1180. rcventry = grp->base + i;
  1181. if (node->map & BIT(i) || cnt >= node->cnt) {
  1182. rcv_array_wc_fill(dd, rcventry);
  1183. continue;
  1184. }
  1185. pset = &flow->pagesets[(*pset_idx)++];
  1186. if (pset->count) {
  1187. hfi1_put_tid(dd, rcventry, PT_EXPECTED,
  1188. pset->addr, trdma_pset_order(pset));
  1189. } else {
  1190. hfi1_put_tid(dd, rcventry, PT_INVALID, 0, 0);
  1191. }
  1192. npages += pset->count;
  1193. rcventry -= rcd->expected_base;
  1194. tidctrl = pair ? 0x3 : rcventry & 0x1 ? 0x2 : 0x1;
  1195. /*
  1196. * A single TID entry will be used to use a rcvarr pair (with
  1197. * tidctrl 0x3), if ALL these are true (a) the bit pos is even
  1198. * (b) the group map shows current and the next bits as free
  1199. * indicating two consecutive rcvarry entries are available (c)
  1200. * we actually need 2 more entries
  1201. */
  1202. pair = !(i & 0x1) && !((node->map >> i) & 0x3) &&
  1203. node->cnt >= cnt + 2;
  1204. if (!pair) {
  1205. if (!pset->count)
  1206. tidctrl = 0x1;
  1207. flow->tid_entry[flow->tidcnt++] =
  1208. EXP_TID_SET(IDX, rcventry >> 1) |
  1209. EXP_TID_SET(CTRL, tidctrl) |
  1210. EXP_TID_SET(LEN, npages);
  1211. trace_hfi1_tid_entry_alloc(/* entry */
  1212. flow->req->qp, flow->tidcnt - 1,
  1213. flow->tid_entry[flow->tidcnt - 1]);
  1214. /* Efficient DIV_ROUND_UP(npages, pmtu_pg) */
  1215. flow->npkts += (npages + pmtu_pg - 1) >> ilog2(pmtu_pg);
  1216. npages = 0;
  1217. }
  1218. if (grp->used == grp->size - 1)
  1219. tid_group_move(grp, &rcd->tid_used_list,
  1220. &rcd->tid_full_list);
  1221. else if (!grp->used)
  1222. tid_group_move(grp, &rcd->tid_group_list,
  1223. &rcd->tid_used_list);
  1224. grp->used++;
  1225. grp->map |= BIT(i);
  1226. cnt++;
  1227. }
  1228. }
  1229. static void kern_unprogram_rcv_group(struct tid_rdma_flow *flow, int grp_num)
  1230. {
  1231. struct hfi1_ctxtdata *rcd = flow->req->rcd;
  1232. struct hfi1_devdata *dd = rcd->dd;
  1233. struct kern_tid_node *node = &flow->tnode[grp_num];
  1234. struct tid_group *grp = node->grp;
  1235. u32 rcventry;
  1236. u8 i, cnt = 0;
  1237. for (i = 0; i < grp->size; i++) {
  1238. rcventry = grp->base + i;
  1239. if (node->map & BIT(i) || cnt >= node->cnt) {
  1240. rcv_array_wc_fill(dd, rcventry);
  1241. continue;
  1242. }
  1243. hfi1_put_tid(dd, rcventry, PT_INVALID, 0, 0);
  1244. grp->used--;
  1245. grp->map &= ~BIT(i);
  1246. cnt++;
  1247. if (grp->used == grp->size - 1)
  1248. tid_group_move(grp, &rcd->tid_full_list,
  1249. &rcd->tid_used_list);
  1250. else if (!grp->used)
  1251. tid_group_move(grp, &rcd->tid_used_list,
  1252. &rcd->tid_group_list);
  1253. }
  1254. if (WARN_ON_ONCE(cnt & 1)) {
  1255. struct hfi1_ctxtdata *rcd = flow->req->rcd;
  1256. struct hfi1_devdata *dd = rcd->dd;
  1257. dd_dev_err(dd, "unexpected odd free cnt %u map 0x%x used %u",
  1258. cnt, grp->map, grp->used);
  1259. }
  1260. }
  1261. static void kern_program_rcvarray(struct tid_rdma_flow *flow)
  1262. {
  1263. u32 pset_idx = 0;
  1264. int i;
  1265. flow->npkts = 0;
  1266. flow->tidcnt = 0;
  1267. for (i = 0; i < flow->tnode_cnt; i++)
  1268. kern_program_rcv_group(flow, i, &pset_idx);
  1269. trace_hfi1_tid_flow_alloc(flow->req->qp, flow->req->setup_head, flow);
  1270. }
  1271. /**
  1272. * hfi1_kern_exp_rcv_setup() - setup TID's and flow for one segment of a
  1273. * TID RDMA request
  1274. *
  1275. * @req: TID RDMA request for which the segment/flow is being set up
  1276. * @ss: sge state, maintains state across successive segments of a sge
  1277. * @last: set to true after the last sge segment has been processed
  1278. *
  1279. * This function
  1280. * (1) finds a free flow entry in the flow circular buffer
  1281. * (2) finds pages and continuous physical chunks constituing one segment
  1282. * of an sge
  1283. * (3) allocates TID group entries for those chunks
  1284. * (4) programs rcvarray entries in the hardware corresponding to those
  1285. * TID's
  1286. * (5) computes a tidarray with formatted TID entries which can be sent
  1287. * to the sender
  1288. * (6) Reserves and programs HW flows.
  1289. * (7) It also manages queing the QP when TID/flow resources are not
  1290. * available.
  1291. *
  1292. * @req points to struct tid_rdma_request of which the segments are a part. The
  1293. * function uses qp, rcd and seg_len members of @req. In the absence of errors,
  1294. * req->flow_idx is the index of the flow which has been prepared in this
  1295. * invocation of function call. With flow = &req->flows[req->flow_idx],
  1296. * flow->tid_entry contains the TID array which the sender can use for TID RDMA
  1297. * sends and flow->npkts contains number of packets required to send the
  1298. * segment.
  1299. *
  1300. * hfi1_check_sge_align should be called prior to calling this function and if
  1301. * it signals error TID RDMA cannot be used for this sge and this function
  1302. * should not be called.
  1303. *
  1304. * For the queuing, caller must hold the flow->req->qp s_lock from the send
  1305. * engine and the function will procure the exp_lock.
  1306. *
  1307. * Return:
  1308. * The function returns -EAGAIN if sufficient number of TID/flow resources to
  1309. * map the segment could not be allocated. In this case the function should be
  1310. * called again with previous arguments to retry the TID allocation. There are
  1311. * no other error returns. The function returns 0 on success.
  1312. */
  1313. int hfi1_kern_exp_rcv_setup(struct tid_rdma_request *req,
  1314. struct rvt_sge_state *ss, bool *last)
  1315. __must_hold(&req->qp->s_lock)
  1316. {
  1317. struct tid_rdma_flow *flow = &req->flows[req->setup_head];
  1318. struct hfi1_ctxtdata *rcd = req->rcd;
  1319. struct hfi1_qp_priv *qpriv = req->qp->priv;
  1320. unsigned long flags;
  1321. struct rvt_qp *fqp;
  1322. u16 clear_tail = req->clear_tail;
  1323. lockdep_assert_held(&req->qp->s_lock);
  1324. /*
  1325. * We return error if either (a) we don't have space in the flow
  1326. * circular buffer, or (b) we already have max entries in the buffer.
  1327. * Max entries depend on the type of request we are processing and the
  1328. * negotiated TID RDMA parameters.
  1329. */
  1330. if (!CIRC_SPACE(req->setup_head, clear_tail, MAX_FLOWS) ||
  1331. CIRC_CNT(req->setup_head, clear_tail, MAX_FLOWS) >=
  1332. req->n_flows)
  1333. return -EINVAL;
  1334. /*
  1335. * Get pages, identify contiguous physical memory chunks for the segment
  1336. * If we can not determine a DMA address mapping we will treat it just
  1337. * like if we ran out of space above.
  1338. */
  1339. if (kern_get_phys_blocks(flow, qpriv->pages, ss, last)) {
  1340. hfi1_wait_kmem(flow->req->qp);
  1341. return -ENOMEM;
  1342. }
  1343. spin_lock_irqsave(&rcd->exp_lock, flags);
  1344. if (kernel_tid_waiters(rcd, &rcd->rarr_queue, flow->req->qp))
  1345. goto queue;
  1346. /*
  1347. * At this point we know the number of pagesets and hence the number of
  1348. * TID's to map the segment. Allocate the TID's from the TID groups. If
  1349. * we cannot allocate the required number we exit and try again later
  1350. */
  1351. if (kern_alloc_tids(flow))
  1352. goto queue;
  1353. /*
  1354. * Finally program the TID entries with the pagesets, compute the
  1355. * tidarray and enable the HW flow
  1356. */
  1357. kern_program_rcvarray(flow);
  1358. /*
  1359. * Setup the flow state with relevant information.
  1360. * This information is used for tracking the sequence of data packets
  1361. * for the segment.
  1362. * The flow is setup here as this is the most accurate time and place
  1363. * to do so. Doing at a later time runs the risk of the flow data in
  1364. * qpriv getting out of sync.
  1365. */
  1366. memset(&flow->flow_state, 0x0, sizeof(flow->flow_state));
  1367. flow->idx = qpriv->flow_state.index;
  1368. flow->flow_state.generation = qpriv->flow_state.generation;
  1369. flow->flow_state.spsn = qpriv->flow_state.psn;
  1370. flow->flow_state.lpsn = flow->flow_state.spsn + flow->npkts - 1;
  1371. flow->flow_state.r_next_psn =
  1372. full_flow_psn(flow, flow->flow_state.spsn);
  1373. qpriv->flow_state.psn += flow->npkts;
  1374. dequeue_tid_waiter(rcd, &rcd->rarr_queue, flow->req->qp);
  1375. /* get head before dropping lock */
  1376. fqp = first_qp(rcd, &rcd->rarr_queue);
  1377. spin_unlock_irqrestore(&rcd->exp_lock, flags);
  1378. tid_rdma_schedule_tid_wakeup(fqp);
  1379. req->setup_head = (req->setup_head + 1) & (MAX_FLOWS - 1);
  1380. return 0;
  1381. queue:
  1382. queue_qp_for_tid_wait(rcd, &rcd->rarr_queue, flow->req->qp);
  1383. spin_unlock_irqrestore(&rcd->exp_lock, flags);
  1384. return -EAGAIN;
  1385. }
  1386. static void hfi1_tid_rdma_reset_flow(struct tid_rdma_flow *flow)
  1387. {
  1388. flow->npagesets = 0;
  1389. }
  1390. /*
  1391. * This function is called after one segment has been successfully sent to
  1392. * release the flow and TID HW/SW resources for that segment. The segments for a
  1393. * TID RDMA request are setup and cleared in FIFO order which is managed using a
  1394. * circular buffer.
  1395. */
  1396. int hfi1_kern_exp_rcv_clear(struct tid_rdma_request *req)
  1397. __must_hold(&req->qp->s_lock)
  1398. {
  1399. struct tid_rdma_flow *flow = &req->flows[req->clear_tail];
  1400. struct hfi1_ctxtdata *rcd = req->rcd;
  1401. unsigned long flags;
  1402. int i;
  1403. struct rvt_qp *fqp;
  1404. lockdep_assert_held(&req->qp->s_lock);
  1405. /* Exit if we have nothing in the flow circular buffer */
  1406. if (!CIRC_CNT(req->setup_head, req->clear_tail, MAX_FLOWS))
  1407. return -EINVAL;
  1408. spin_lock_irqsave(&rcd->exp_lock, flags);
  1409. for (i = 0; i < flow->tnode_cnt; i++)
  1410. kern_unprogram_rcv_group(flow, i);
  1411. /* To prevent double unprogramming */
  1412. flow->tnode_cnt = 0;
  1413. /* get head before dropping lock */
  1414. fqp = first_qp(rcd, &rcd->rarr_queue);
  1415. spin_unlock_irqrestore(&rcd->exp_lock, flags);
  1416. dma_unmap_flow(flow);
  1417. hfi1_tid_rdma_reset_flow(flow);
  1418. req->clear_tail = (req->clear_tail + 1) & (MAX_FLOWS - 1);
  1419. if (fqp == req->qp) {
  1420. __trigger_tid_waiter(fqp);
  1421. rvt_put_qp(fqp);
  1422. } else {
  1423. tid_rdma_schedule_tid_wakeup(fqp);
  1424. }
  1425. return 0;
  1426. }
  1427. /*
  1428. * This function is called to release all the tid entries for
  1429. * a request.
  1430. */
  1431. void hfi1_kern_exp_rcv_clear_all(struct tid_rdma_request *req)
  1432. __must_hold(&req->qp->s_lock)
  1433. {
  1434. /* Use memory barrier for proper ordering */
  1435. while (CIRC_CNT(req->setup_head, req->clear_tail, MAX_FLOWS)) {
  1436. if (hfi1_kern_exp_rcv_clear(req))
  1437. break;
  1438. }
  1439. }
  1440. /**
  1441. * hfi1_kern_exp_rcv_free_flows - free priviously allocated flow information
  1442. * @req: the tid rdma request to be cleaned
  1443. */
  1444. static void hfi1_kern_exp_rcv_free_flows(struct tid_rdma_request *req)
  1445. {
  1446. kfree(req->flows);
  1447. req->flows = NULL;
  1448. }
  1449. /**
  1450. * __trdma_clean_swqe - clean up for large sized QPs
  1451. * @qp: the queue patch
  1452. * @wqe: the send wqe
  1453. */
  1454. void __trdma_clean_swqe(struct rvt_qp *qp, struct rvt_swqe *wqe)
  1455. {
  1456. struct hfi1_swqe_priv *p = wqe->priv;
  1457. hfi1_kern_exp_rcv_free_flows(&p->tid_req);
  1458. }
  1459. /*
  1460. * This can be called at QP create time or in the data path.
  1461. */
  1462. static int hfi1_kern_exp_rcv_alloc_flows(struct tid_rdma_request *req,
  1463. gfp_t gfp)
  1464. {
  1465. struct tid_rdma_flow *flows;
  1466. int i;
  1467. if (likely(req->flows))
  1468. return 0;
  1469. flows = kmalloc_node(MAX_FLOWS * sizeof(*flows), gfp,
  1470. req->rcd->numa_id);
  1471. if (!flows)
  1472. return -ENOMEM;
  1473. /* mini init */
  1474. for (i = 0; i < MAX_FLOWS; i++) {
  1475. flows[i].req = req;
  1476. flows[i].npagesets = 0;
  1477. flows[i].pagesets[0].mapped = 0;
  1478. flows[i].resync_npkts = 0;
  1479. }
  1480. req->flows = flows;
  1481. return 0;
  1482. }
  1483. static void hfi1_init_trdma_req(struct rvt_qp *qp,
  1484. struct tid_rdma_request *req)
  1485. {
  1486. struct hfi1_qp_priv *qpriv = qp->priv;
  1487. /*
  1488. * Initialize various TID RDMA request variables.
  1489. * These variables are "static", which is why they
  1490. * can be pre-initialized here before the WRs has
  1491. * even been submitted.
  1492. * However, non-NULL values for these variables do not
  1493. * imply that this WQE has been enabled for TID RDMA.
  1494. * Drivers should check the WQE's opcode to determine
  1495. * if a request is a TID RDMA one or not.
  1496. */
  1497. req->qp = qp;
  1498. req->rcd = qpriv->rcd;
  1499. }
  1500. u64 hfi1_access_sw_tid_wait(const struct cntr_entry *entry,
  1501. void *context, int vl, int mode, u64 data)
  1502. {
  1503. struct hfi1_devdata *dd = context;
  1504. return dd->verbs_dev.n_tidwait;
  1505. }
  1506. static struct tid_rdma_flow *find_flow_ib(struct tid_rdma_request *req,
  1507. u32 psn, u16 *fidx)
  1508. {
  1509. u16 head, tail;
  1510. struct tid_rdma_flow *flow;
  1511. head = req->setup_head;
  1512. tail = req->clear_tail;
  1513. for ( ; CIRC_CNT(head, tail, MAX_FLOWS);
  1514. tail = CIRC_NEXT(tail, MAX_FLOWS)) {
  1515. flow = &req->flows[tail];
  1516. if (cmp_psn(psn, flow->flow_state.ib_spsn) >= 0 &&
  1517. cmp_psn(psn, flow->flow_state.ib_lpsn) <= 0) {
  1518. if (fidx)
  1519. *fidx = tail;
  1520. return flow;
  1521. }
  1522. }
  1523. return NULL;
  1524. }
  1525. /* TID RDMA READ functions */
  1526. u32 hfi1_build_tid_rdma_read_packet(struct rvt_swqe *wqe,
  1527. struct ib_other_headers *ohdr, u32 *bth1,
  1528. u32 *bth2, u32 *len)
  1529. {
  1530. struct tid_rdma_request *req = wqe_to_tid_req(wqe);
  1531. struct tid_rdma_flow *flow = &req->flows[req->flow_idx];
  1532. struct rvt_qp *qp = req->qp;
  1533. struct hfi1_qp_priv *qpriv = qp->priv;
  1534. struct hfi1_swqe_priv *wpriv = wqe->priv;
  1535. struct tid_rdma_read_req *rreq = &ohdr->u.tid_rdma.r_req;
  1536. struct tid_rdma_params *remote;
  1537. u32 req_len = 0;
  1538. void *req_addr = NULL;
  1539. /* This is the IB psn used to send the request */
  1540. *bth2 = mask_psn(flow->flow_state.ib_spsn + flow->pkt);
  1541. trace_hfi1_tid_flow_build_read_pkt(qp, req->flow_idx, flow);
  1542. /* TID Entries for TID RDMA READ payload */
  1543. req_addr = &flow->tid_entry[flow->tid_idx];
  1544. req_len = sizeof(*flow->tid_entry) *
  1545. (flow->tidcnt - flow->tid_idx);
  1546. memset(&ohdr->u.tid_rdma.r_req, 0, sizeof(ohdr->u.tid_rdma.r_req));
  1547. wpriv->ss.sge.vaddr = req_addr;
  1548. wpriv->ss.sge.sge_length = req_len;
  1549. wpriv->ss.sge.length = wpriv->ss.sge.sge_length;
  1550. /*
  1551. * We can safely zero these out. Since the first SGE covers the
  1552. * entire packet, nothing else should even look at the MR.
  1553. */
  1554. wpriv->ss.sge.mr = NULL;
  1555. wpriv->ss.sge.m = 0;
  1556. wpriv->ss.sge.n = 0;
  1557. wpriv->ss.sg_list = NULL;
  1558. wpriv->ss.total_len = wpriv->ss.sge.sge_length;
  1559. wpriv->ss.num_sge = 1;
  1560. /* Construct the TID RDMA READ REQ packet header */
  1561. rcu_read_lock();
  1562. remote = rcu_dereference(qpriv->tid_rdma.remote);
  1563. KDETH_RESET(rreq->kdeth0, KVER, 0x1);
  1564. KDETH_RESET(rreq->kdeth1, JKEY, remote->jkey);
  1565. rreq->reth.vaddr = cpu_to_be64(wqe->rdma_wr.remote_addr +
  1566. req->cur_seg * req->seg_len + flow->sent);
  1567. rreq->reth.rkey = cpu_to_be32(wqe->rdma_wr.rkey);
  1568. rreq->reth.length = cpu_to_be32(*len);
  1569. rreq->tid_flow_psn =
  1570. cpu_to_be32((flow->flow_state.generation <<
  1571. HFI1_KDETH_BTH_SEQ_SHIFT) |
  1572. ((flow->flow_state.spsn + flow->pkt) &
  1573. HFI1_KDETH_BTH_SEQ_MASK));
  1574. rreq->tid_flow_qp =
  1575. cpu_to_be32(qpriv->tid_rdma.local.qp |
  1576. ((flow->idx & TID_RDMA_DESTQP_FLOW_MASK) <<
  1577. TID_RDMA_DESTQP_FLOW_SHIFT) |
  1578. qpriv->rcd->ctxt);
  1579. rreq->verbs_qp = cpu_to_be32(qp->remote_qpn);
  1580. *bth1 &= ~RVT_QPN_MASK;
  1581. *bth1 |= remote->qp;
  1582. *bth2 |= IB_BTH_REQ_ACK;
  1583. rcu_read_unlock();
  1584. /* We are done with this segment */
  1585. flow->sent += *len;
  1586. req->cur_seg++;
  1587. qp->s_state = TID_OP(READ_REQ);
  1588. req->ack_pending++;
  1589. req->flow_idx = (req->flow_idx + 1) & (MAX_FLOWS - 1);
  1590. qpriv->pending_tid_r_segs++;
  1591. qp->s_num_rd_atomic++;
  1592. /* Set the TID RDMA READ request payload size */
  1593. *len = req_len;
  1594. return sizeof(ohdr->u.tid_rdma.r_req) / sizeof(u32);
  1595. }
  1596. /*
  1597. * @len: contains the data length to read upon entry and the read request
  1598. * payload length upon exit.
  1599. */
  1600. u32 hfi1_build_tid_rdma_read_req(struct rvt_qp *qp, struct rvt_swqe *wqe,
  1601. struct ib_other_headers *ohdr, u32 *bth1,
  1602. u32 *bth2, u32 *len)
  1603. __must_hold(&qp->s_lock)
  1604. {
  1605. struct hfi1_qp_priv *qpriv = qp->priv;
  1606. struct tid_rdma_request *req = wqe_to_tid_req(wqe);
  1607. struct tid_rdma_flow *flow = NULL;
  1608. u32 hdwords = 0;
  1609. bool last;
  1610. bool retry = true;
  1611. u32 npkts = rvt_div_round_up_mtu(qp, *len);
  1612. trace_hfi1_tid_req_build_read_req(qp, 0, wqe->wr.opcode, wqe->psn,
  1613. wqe->lpsn, req);
  1614. /*
  1615. * Check sync conditions. Make sure that there are no pending
  1616. * segments before freeing the flow.
  1617. */
  1618. sync_check:
  1619. if (req->state == TID_REQUEST_SYNC) {
  1620. if (qpriv->pending_tid_r_segs)
  1621. goto done;
  1622. hfi1_kern_clear_hw_flow(req->rcd, qp);
  1623. qpriv->s_flags &= ~HFI1_R_TID_SW_PSN;
  1624. req->state = TID_REQUEST_ACTIVE;
  1625. }
  1626. /*
  1627. * If the request for this segment is resent, the tid resources should
  1628. * have been allocated before. In this case, req->flow_idx should
  1629. * fall behind req->setup_head.
  1630. */
  1631. if (req->flow_idx == req->setup_head) {
  1632. retry = false;
  1633. if (req->state == TID_REQUEST_RESEND) {
  1634. /*
  1635. * This is the first new segment for a request whose
  1636. * earlier segments have been re-sent. We need to
  1637. * set up the sge pointer correctly.
  1638. */
  1639. restart_sge(&qp->s_sge, wqe, req->s_next_psn,
  1640. qp->pmtu);
  1641. req->isge = 0;
  1642. req->state = TID_REQUEST_ACTIVE;
  1643. }
  1644. /*
  1645. * Check sync. The last PSN of each generation is reserved for
  1646. * RESYNC.
  1647. */
  1648. if ((qpriv->flow_state.psn + npkts) > MAX_TID_FLOW_PSN - 1) {
  1649. req->state = TID_REQUEST_SYNC;
  1650. goto sync_check;
  1651. }
  1652. /* Allocate the flow if not yet */
  1653. if (hfi1_kern_setup_hw_flow(qpriv->rcd, qp))
  1654. goto done;
  1655. /*
  1656. * The following call will advance req->setup_head after
  1657. * allocating the tid entries.
  1658. */
  1659. if (hfi1_kern_exp_rcv_setup(req, &qp->s_sge, &last)) {
  1660. req->state = TID_REQUEST_QUEUED;
  1661. /*
  1662. * We don't have resources for this segment. The QP has
  1663. * already been queued.
  1664. */
  1665. goto done;
  1666. }
  1667. }
  1668. /* req->flow_idx should only be one slot behind req->setup_head */
  1669. flow = &req->flows[req->flow_idx];
  1670. flow->pkt = 0;
  1671. flow->tid_idx = 0;
  1672. flow->sent = 0;
  1673. if (!retry) {
  1674. /* Set the first and last IB PSN for the flow in use.*/
  1675. flow->flow_state.ib_spsn = req->s_next_psn;
  1676. flow->flow_state.ib_lpsn =
  1677. flow->flow_state.ib_spsn + flow->npkts - 1;
  1678. }
  1679. /* Calculate the next segment start psn.*/
  1680. req->s_next_psn += flow->npkts;
  1681. /* Build the packet header */
  1682. hdwords = hfi1_build_tid_rdma_read_packet(wqe, ohdr, bth1, bth2, len);
  1683. done:
  1684. return hdwords;
  1685. }
  1686. /*
  1687. * Validate and accept the TID RDMA READ request parameters.
  1688. * Return 0 if the request is accepted successfully;
  1689. * Return 1 otherwise.
  1690. */
  1691. static int tid_rdma_rcv_read_request(struct rvt_qp *qp,
  1692. struct rvt_ack_entry *e,
  1693. struct hfi1_packet *packet,
  1694. struct ib_other_headers *ohdr,
  1695. u32 bth0, u32 psn, u64 vaddr, u32 len)
  1696. {
  1697. struct hfi1_qp_priv *qpriv = qp->priv;
  1698. struct tid_rdma_request *req;
  1699. struct tid_rdma_flow *flow;
  1700. u32 flow_psn, i, tidlen = 0, pktlen, tlen;
  1701. req = ack_to_tid_req(e);
  1702. /* Validate the payload first */
  1703. flow = &req->flows[req->setup_head];
  1704. /* payload length = packet length - (header length + ICRC length) */
  1705. pktlen = packet->tlen - (packet->hlen + 4);
  1706. if (pktlen > sizeof(flow->tid_entry))
  1707. return 1;
  1708. memcpy(flow->tid_entry, packet->ebuf, pktlen);
  1709. flow->tidcnt = pktlen / sizeof(*flow->tid_entry);
  1710. /*
  1711. * Walk the TID_ENTRY list to make sure we have enough space for a
  1712. * complete segment. Also calculate the number of required packets.
  1713. */
  1714. flow->npkts = rvt_div_round_up_mtu(qp, len);
  1715. for (i = 0; i < flow->tidcnt; i++) {
  1716. trace_hfi1_tid_entry_rcv_read_req(qp, i,
  1717. flow->tid_entry[i]);
  1718. tlen = EXP_TID_GET(flow->tid_entry[i], LEN);
  1719. if (!tlen)
  1720. return 1;
  1721. /*
  1722. * For tid pair (tidctr == 3), the buffer size of the pair
  1723. * should be the sum of the buffer size described by each
  1724. * tid entry. However, only the first entry needs to be
  1725. * specified in the request (see WFR HAS Section 8.5.7.1).
  1726. */
  1727. tidlen += tlen;
  1728. }
  1729. if (tidlen * PAGE_SIZE < len)
  1730. return 1;
  1731. /* Empty the flow array */
  1732. req->clear_tail = req->setup_head;
  1733. flow->pkt = 0;
  1734. flow->tid_idx = 0;
  1735. flow->tid_offset = 0;
  1736. flow->sent = 0;
  1737. flow->tid_qpn = be32_to_cpu(ohdr->u.tid_rdma.r_req.tid_flow_qp);
  1738. flow->idx = (flow->tid_qpn >> TID_RDMA_DESTQP_FLOW_SHIFT) &
  1739. TID_RDMA_DESTQP_FLOW_MASK;
  1740. flow_psn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.r_req.tid_flow_psn));
  1741. flow->flow_state.generation = flow_psn >> HFI1_KDETH_BTH_SEQ_SHIFT;
  1742. flow->flow_state.spsn = flow_psn & HFI1_KDETH_BTH_SEQ_MASK;
  1743. flow->length = len;
  1744. flow->flow_state.lpsn = flow->flow_state.spsn +
  1745. flow->npkts - 1;
  1746. flow->flow_state.ib_spsn = psn;
  1747. flow->flow_state.ib_lpsn = flow->flow_state.ib_spsn + flow->npkts - 1;
  1748. trace_hfi1_tid_flow_rcv_read_req(qp, req->setup_head, flow);
  1749. /* Set the initial flow index to the current flow. */
  1750. req->flow_idx = req->setup_head;
  1751. /* advance circular buffer head */
  1752. req->setup_head = (req->setup_head + 1) & (MAX_FLOWS - 1);
  1753. /*
  1754. * Compute last PSN for request.
  1755. */
  1756. e->opcode = (bth0 >> 24) & 0xff;
  1757. e->psn = psn;
  1758. e->lpsn = psn + flow->npkts - 1;
  1759. e->sent = 0;
  1760. req->n_flows = qpriv->tid_rdma.local.max_read;
  1761. req->state = TID_REQUEST_ACTIVE;
  1762. req->cur_seg = 0;
  1763. req->comp_seg = 0;
  1764. req->ack_seg = 0;
  1765. req->isge = 0;
  1766. req->seg_len = qpriv->tid_rdma.local.max_len;
  1767. req->total_len = len;
  1768. req->total_segs = 1;
  1769. req->r_flow_psn = e->psn;
  1770. trace_hfi1_tid_req_rcv_read_req(qp, 0, e->opcode, e->psn, e->lpsn,
  1771. req);
  1772. return 0;
  1773. }
  1774. static int tid_rdma_rcv_error(struct hfi1_packet *packet,
  1775. struct ib_other_headers *ohdr,
  1776. struct rvt_qp *qp, u32 psn, int diff)
  1777. {
  1778. struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  1779. struct hfi1_ctxtdata *rcd = ((struct hfi1_qp_priv *)qp->priv)->rcd;
  1780. struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
  1781. struct hfi1_qp_priv *qpriv = qp->priv;
  1782. struct rvt_ack_entry *e;
  1783. struct tid_rdma_request *req;
  1784. unsigned long flags;
  1785. u8 prev;
  1786. bool old_req;
  1787. trace_hfi1_rsp_tid_rcv_error(qp, psn);
  1788. trace_hfi1_tid_rdma_rcv_err(qp, 0, psn, diff);
  1789. if (diff > 0) {
  1790. /* sequence error */
  1791. if (!qp->r_nak_state) {
  1792. ibp->rvp.n_rc_seqnak++;
  1793. qp->r_nak_state = IB_NAK_PSN_ERROR;
  1794. qp->r_ack_psn = qp->r_psn;
  1795. rc_defered_ack(rcd, qp);
  1796. }
  1797. goto done;
  1798. }
  1799. ibp->rvp.n_rc_dupreq++;
  1800. spin_lock_irqsave(&qp->s_lock, flags);
  1801. e = find_prev_entry(qp, psn, &prev, NULL, &old_req);
  1802. if (!e || (e->opcode != TID_OP(READ_REQ) &&
  1803. e->opcode != TID_OP(WRITE_REQ)))
  1804. goto unlock;
  1805. req = ack_to_tid_req(e);
  1806. req->r_flow_psn = psn;
  1807. trace_hfi1_tid_req_rcv_err(qp, 0, e->opcode, e->psn, e->lpsn, req);
  1808. if (e->opcode == TID_OP(READ_REQ)) {
  1809. struct ib_reth *reth;
  1810. u32 len;
  1811. u32 rkey;
  1812. u64 vaddr;
  1813. int ok;
  1814. u32 bth0;
  1815. reth = &ohdr->u.tid_rdma.r_req.reth;
  1816. /*
  1817. * The requester always restarts from the start of the original
  1818. * request.
  1819. */
  1820. len = be32_to_cpu(reth->length);
  1821. if (psn != e->psn || len != req->total_len)
  1822. goto unlock;
  1823. release_rdma_sge_mr(e);
  1824. rkey = be32_to_cpu(reth->rkey);
  1825. vaddr = get_ib_reth_vaddr(reth);
  1826. qp->r_len = len;
  1827. ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
  1828. IB_ACCESS_REMOTE_READ);
  1829. if (unlikely(!ok))
  1830. goto unlock;
  1831. /*
  1832. * If all the response packets for the current request have
  1833. * been sent out and this request is complete (old_request
  1834. * == false) and the TID flow may be unusable (the
  1835. * req->clear_tail is advanced). However, when an earlier
  1836. * request is received, this request will not be complete any
  1837. * more (qp->s_tail_ack_queue is moved back, see below).
  1838. * Consequently, we need to update the TID flow info everytime
  1839. * a duplicate request is received.
  1840. */
  1841. bth0 = be32_to_cpu(ohdr->bth[0]);
  1842. if (tid_rdma_rcv_read_request(qp, e, packet, ohdr, bth0, psn,
  1843. vaddr, len))
  1844. goto unlock;
  1845. /*
  1846. * True if the request is already scheduled (between
  1847. * qp->s_tail_ack_queue and qp->r_head_ack_queue);
  1848. */
  1849. if (old_req)
  1850. goto unlock;
  1851. } else {
  1852. struct flow_state *fstate;
  1853. bool schedule = false;
  1854. u8 i;
  1855. if (req->state == TID_REQUEST_RESEND) {
  1856. req->state = TID_REQUEST_RESEND_ACTIVE;
  1857. } else if (req->state == TID_REQUEST_INIT_RESEND) {
  1858. req->state = TID_REQUEST_INIT;
  1859. schedule = true;
  1860. }
  1861. /*
  1862. * True if the request is already scheduled (between
  1863. * qp->s_tail_ack_queue and qp->r_head_ack_queue).
  1864. * Also, don't change requests, which are at the SYNC
  1865. * point and haven't generated any responses yet.
  1866. * There is nothing to retransmit for them yet.
  1867. */
  1868. if (old_req || req->state == TID_REQUEST_INIT ||
  1869. (req->state == TID_REQUEST_SYNC && !req->cur_seg)) {
  1870. for (i = prev + 1; ; i++) {
  1871. if (i > rvt_size_atomic(&dev->rdi))
  1872. i = 0;
  1873. if (i == qp->r_head_ack_queue)
  1874. break;
  1875. e = &qp->s_ack_queue[i];
  1876. req = ack_to_tid_req(e);
  1877. if (e->opcode == TID_OP(WRITE_REQ) &&
  1878. req->state == TID_REQUEST_INIT)
  1879. req->state = TID_REQUEST_INIT_RESEND;
  1880. }
  1881. /*
  1882. * If the state of the request has been changed,
  1883. * the first leg needs to get scheduled in order to
  1884. * pick up the change. Otherwise, normal response
  1885. * processing should take care of it.
  1886. */
  1887. if (!schedule)
  1888. goto unlock;
  1889. }
  1890. /*
  1891. * If there is no more allocated segment, just schedule the qp
  1892. * without changing any state.
  1893. */
  1894. if (req->clear_tail == req->setup_head)
  1895. goto schedule;
  1896. /*
  1897. * If this request has sent responses for segments, which have
  1898. * not received data yet (flow_idx != clear_tail), the flow_idx
  1899. * pointer needs to be adjusted so the same responses can be
  1900. * re-sent.
  1901. */
  1902. if (CIRC_CNT(req->flow_idx, req->clear_tail, MAX_FLOWS)) {
  1903. fstate = &req->flows[req->clear_tail].flow_state;
  1904. qpriv->pending_tid_w_segs -=
  1905. CIRC_CNT(req->flow_idx, req->clear_tail,
  1906. MAX_FLOWS);
  1907. req->flow_idx =
  1908. CIRC_ADD(req->clear_tail,
  1909. delta_psn(psn, fstate->resp_ib_psn),
  1910. MAX_FLOWS);
  1911. qpriv->pending_tid_w_segs +=
  1912. delta_psn(psn, fstate->resp_ib_psn);
  1913. /*
  1914. * When flow_idx == setup_head, we've gotten a duplicate
  1915. * request for a segment, which has not been allocated
  1916. * yet. In that case, don't adjust this request.
  1917. * However, we still want to go through the loop below
  1918. * to adjust all subsequent requests.
  1919. */
  1920. if (CIRC_CNT(req->setup_head, req->flow_idx,
  1921. MAX_FLOWS)) {
  1922. req->cur_seg = delta_psn(psn, e->psn);
  1923. req->state = TID_REQUEST_RESEND_ACTIVE;
  1924. }
  1925. }
  1926. for (i = prev + 1; ; i++) {
  1927. /*
  1928. * Look at everything up to and including
  1929. * s_tail_ack_queue
  1930. */
  1931. if (i > rvt_size_atomic(&dev->rdi))
  1932. i = 0;
  1933. if (i == qp->r_head_ack_queue)
  1934. break;
  1935. e = &qp->s_ack_queue[i];
  1936. req = ack_to_tid_req(e);
  1937. trace_hfi1_tid_req_rcv_err(qp, 0, e->opcode, e->psn,
  1938. e->lpsn, req);
  1939. if (e->opcode != TID_OP(WRITE_REQ) ||
  1940. req->cur_seg == req->comp_seg ||
  1941. req->state == TID_REQUEST_INIT ||
  1942. req->state == TID_REQUEST_INIT_RESEND) {
  1943. if (req->state == TID_REQUEST_INIT)
  1944. req->state = TID_REQUEST_INIT_RESEND;
  1945. continue;
  1946. }
  1947. qpriv->pending_tid_w_segs -=
  1948. CIRC_CNT(req->flow_idx,
  1949. req->clear_tail,
  1950. MAX_FLOWS);
  1951. req->flow_idx = req->clear_tail;
  1952. req->state = TID_REQUEST_RESEND;
  1953. req->cur_seg = req->comp_seg;
  1954. }
  1955. qpriv->s_flags &= ~HFI1_R_TID_WAIT_INTERLCK;
  1956. }
  1957. /* Re-process old requests.*/
  1958. if (qp->s_acked_ack_queue == qp->s_tail_ack_queue)
  1959. qp->s_acked_ack_queue = prev;
  1960. qp->s_tail_ack_queue = prev;
  1961. /*
  1962. * Since the qp->s_tail_ack_queue is modified, the
  1963. * qp->s_ack_state must be changed to re-initialize
  1964. * qp->s_ack_rdma_sge; Otherwise, we will end up in
  1965. * wrong memory region.
  1966. */
  1967. qp->s_ack_state = OP(ACKNOWLEDGE);
  1968. schedule:
  1969. /*
  1970. * It's possible to receive a retry psn that is earlier than an RNRNAK
  1971. * psn. In this case, the rnrnak state should be cleared.
  1972. */
  1973. if (qpriv->rnr_nak_state) {
  1974. qp->s_nak_state = 0;
  1975. qpriv->rnr_nak_state = TID_RNR_NAK_INIT;
  1976. qp->r_psn = e->lpsn + 1;
  1977. hfi1_tid_write_alloc_resources(qp, true);
  1978. }
  1979. qp->r_state = e->opcode;
  1980. qp->r_nak_state = 0;
  1981. qp->s_flags |= RVT_S_RESP_PENDING;
  1982. hfi1_schedule_send(qp);
  1983. unlock:
  1984. spin_unlock_irqrestore(&qp->s_lock, flags);
  1985. done:
  1986. return 1;
  1987. }
  1988. void hfi1_rc_rcv_tid_rdma_read_req(struct hfi1_packet *packet)
  1989. {
  1990. /* HANDLER FOR TID RDMA READ REQUEST packet (Responder side)*/
  1991. /*
  1992. * 1. Verify TID RDMA READ REQ as per IB_OPCODE_RC_RDMA_READ
  1993. * (see hfi1_rc_rcv())
  1994. * 2. Put TID RDMA READ REQ into the response queueu (s_ack_queue)
  1995. * - Setup struct tid_rdma_req with request info
  1996. * - Initialize struct tid_rdma_flow info;
  1997. * - Copy TID entries;
  1998. * 3. Set the qp->s_ack_state.
  1999. * 4. Set RVT_S_RESP_PENDING in s_flags.
  2000. * 5. Kick the send engine (hfi1_schedule_send())
  2001. */
  2002. struct hfi1_ctxtdata *rcd = packet->rcd;
  2003. struct rvt_qp *qp = packet->qp;
  2004. struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  2005. struct ib_other_headers *ohdr = packet->ohdr;
  2006. struct rvt_ack_entry *e;
  2007. unsigned long flags;
  2008. struct ib_reth *reth;
  2009. struct hfi1_qp_priv *qpriv = qp->priv;
  2010. u32 bth0, psn, len, rkey;
  2011. bool fecn;
  2012. u8 next;
  2013. u64 vaddr;
  2014. int diff;
  2015. u8 nack_state = IB_NAK_INVALID_REQUEST;
  2016. bth0 = be32_to_cpu(ohdr->bth[0]);
  2017. if (hfi1_ruc_check_hdr(ibp, packet))
  2018. return;
  2019. fecn = process_ecn(qp, packet);
  2020. psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
  2021. trace_hfi1_rsp_rcv_tid_read_req(qp, psn);
  2022. if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
  2023. rvt_comm_est(qp);
  2024. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
  2025. goto nack_inv;
  2026. reth = &ohdr->u.tid_rdma.r_req.reth;
  2027. vaddr = be64_to_cpu(reth->vaddr);
  2028. len = be32_to_cpu(reth->length);
  2029. /* The length needs to be in multiples of PAGE_SIZE */
  2030. if (!len || len & ~PAGE_MASK || len > qpriv->tid_rdma.local.max_len)
  2031. goto nack_inv;
  2032. diff = delta_psn(psn, qp->r_psn);
  2033. if (unlikely(diff)) {
  2034. tid_rdma_rcv_err(packet, ohdr, qp, psn, diff, fecn);
  2035. return;
  2036. }
  2037. /* We've verified the request, insert it into the ack queue. */
  2038. next = qp->r_head_ack_queue + 1;
  2039. if (next > rvt_size_atomic(ib_to_rvt(qp->ibqp.device)))
  2040. next = 0;
  2041. spin_lock_irqsave(&qp->s_lock, flags);
  2042. if (unlikely(next == qp->s_tail_ack_queue)) {
  2043. if (!qp->s_ack_queue[next].sent) {
  2044. nack_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
  2045. goto nack_inv_unlock;
  2046. }
  2047. update_ack_queue(qp, next);
  2048. }
  2049. e = &qp->s_ack_queue[qp->r_head_ack_queue];
  2050. release_rdma_sge_mr(e);
  2051. rkey = be32_to_cpu(reth->rkey);
  2052. qp->r_len = len;
  2053. if (unlikely(!rvt_rkey_ok(qp, &e->rdma_sge, qp->r_len, vaddr,
  2054. rkey, IB_ACCESS_REMOTE_READ)))
  2055. goto nack_acc;
  2056. /* Accept the request parameters */
  2057. if (tid_rdma_rcv_read_request(qp, e, packet, ohdr, bth0, psn, vaddr,
  2058. len))
  2059. goto nack_inv_unlock;
  2060. qp->r_state = e->opcode;
  2061. qp->r_nak_state = 0;
  2062. /*
  2063. * We need to increment the MSN here instead of when we
  2064. * finish sending the result since a duplicate request would
  2065. * increment it more than once.
  2066. */
  2067. qp->r_msn++;
  2068. qp->r_psn += e->lpsn - e->psn + 1;
  2069. qp->r_head_ack_queue = next;
  2070. /*
  2071. * For all requests other than TID WRITE which are added to the ack
  2072. * queue, qpriv->r_tid_alloc follows qp->r_head_ack_queue. It is ok to
  2073. * do this because of interlocks between these and TID WRITE
  2074. * requests. The same change has also been made in hfi1_rc_rcv().
  2075. */
  2076. qpriv->r_tid_alloc = qp->r_head_ack_queue;
  2077. /* Schedule the send tasklet. */
  2078. qp->s_flags |= RVT_S_RESP_PENDING;
  2079. if (fecn)
  2080. qp->s_flags |= RVT_S_ECN;
  2081. hfi1_schedule_send(qp);
  2082. spin_unlock_irqrestore(&qp->s_lock, flags);
  2083. return;
  2084. nack_inv_unlock:
  2085. spin_unlock_irqrestore(&qp->s_lock, flags);
  2086. nack_inv:
  2087. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  2088. qp->r_nak_state = nack_state;
  2089. qp->r_ack_psn = qp->r_psn;
  2090. /* Queue NAK for later */
  2091. rc_defered_ack(rcd, qp);
  2092. return;
  2093. nack_acc:
  2094. spin_unlock_irqrestore(&qp->s_lock, flags);
  2095. rvt_rc_error(qp, IB_WC_LOC_PROT_ERR);
  2096. qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
  2097. qp->r_ack_psn = qp->r_psn;
  2098. }
  2099. u32 hfi1_build_tid_rdma_read_resp(struct rvt_qp *qp, struct rvt_ack_entry *e,
  2100. struct ib_other_headers *ohdr, u32 *bth0,
  2101. u32 *bth1, u32 *bth2, u32 *len, bool *last)
  2102. {
  2103. struct hfi1_ack_priv *epriv = e->priv;
  2104. struct tid_rdma_request *req = &epriv->tid_req;
  2105. struct hfi1_qp_priv *qpriv = qp->priv;
  2106. struct tid_rdma_flow *flow = &req->flows[req->clear_tail];
  2107. u32 tidentry = flow->tid_entry[flow->tid_idx];
  2108. u32 tidlen = EXP_TID_GET(tidentry, LEN) << PAGE_SHIFT;
  2109. struct tid_rdma_read_resp *resp = &ohdr->u.tid_rdma.r_rsp;
  2110. u32 next_offset, om = KDETH_OM_LARGE;
  2111. bool last_pkt;
  2112. u32 hdwords = 0;
  2113. struct tid_rdma_params *remote;
  2114. *len = min_t(u32, qp->pmtu, tidlen - flow->tid_offset);
  2115. flow->sent += *len;
  2116. next_offset = flow->tid_offset + *len;
  2117. last_pkt = (flow->sent >= flow->length);
  2118. trace_hfi1_tid_entry_build_read_resp(qp, flow->tid_idx, tidentry);
  2119. trace_hfi1_tid_flow_build_read_resp(qp, req->clear_tail, flow);
  2120. rcu_read_lock();
  2121. remote = rcu_dereference(qpriv->tid_rdma.remote);
  2122. if (!remote) {
  2123. rcu_read_unlock();
  2124. goto done;
  2125. }
  2126. KDETH_RESET(resp->kdeth0, KVER, 0x1);
  2127. KDETH_SET(resp->kdeth0, SH, !last_pkt);
  2128. KDETH_SET(resp->kdeth0, INTR, !!(!last_pkt && remote->urg));
  2129. KDETH_SET(resp->kdeth0, TIDCTRL, EXP_TID_GET(tidentry, CTRL));
  2130. KDETH_SET(resp->kdeth0, TID, EXP_TID_GET(tidentry, IDX));
  2131. KDETH_SET(resp->kdeth0, OM, om == KDETH_OM_LARGE);
  2132. KDETH_SET(resp->kdeth0, OFFSET, flow->tid_offset / om);
  2133. KDETH_RESET(resp->kdeth1, JKEY, remote->jkey);
  2134. resp->verbs_qp = cpu_to_be32(qp->remote_qpn);
  2135. rcu_read_unlock();
  2136. resp->aeth = rvt_compute_aeth(qp);
  2137. resp->verbs_psn = cpu_to_be32(mask_psn(flow->flow_state.ib_spsn +
  2138. flow->pkt));
  2139. *bth0 = TID_OP(READ_RESP) << 24;
  2140. *bth1 = flow->tid_qpn;
  2141. *bth2 = mask_psn(((flow->flow_state.spsn + flow->pkt++) &
  2142. HFI1_KDETH_BTH_SEQ_MASK) |
  2143. (flow->flow_state.generation <<
  2144. HFI1_KDETH_BTH_SEQ_SHIFT));
  2145. *last = last_pkt;
  2146. if (last_pkt)
  2147. /* Advance to next flow */
  2148. req->clear_tail = (req->clear_tail + 1) &
  2149. (MAX_FLOWS - 1);
  2150. if (next_offset >= tidlen) {
  2151. flow->tid_offset = 0;
  2152. flow->tid_idx++;
  2153. } else {
  2154. flow->tid_offset = next_offset;
  2155. }
  2156. hdwords = sizeof(ohdr->u.tid_rdma.r_rsp) / sizeof(u32);
  2157. done:
  2158. return hdwords;
  2159. }
  2160. static inline struct tid_rdma_request *
  2161. find_tid_request(struct rvt_qp *qp, u32 psn, enum ib_wr_opcode opcode)
  2162. __must_hold(&qp->s_lock)
  2163. {
  2164. struct rvt_swqe *wqe;
  2165. struct tid_rdma_request *req = NULL;
  2166. u32 i, end;
  2167. end = qp->s_cur + 1;
  2168. if (end == qp->s_size)
  2169. end = 0;
  2170. for (i = qp->s_acked; i != end;) {
  2171. wqe = rvt_get_swqe_ptr(qp, i);
  2172. if (cmp_psn(psn, wqe->psn) >= 0 &&
  2173. cmp_psn(psn, wqe->lpsn) <= 0) {
  2174. if (wqe->wr.opcode == opcode)
  2175. req = wqe_to_tid_req(wqe);
  2176. break;
  2177. }
  2178. if (++i == qp->s_size)
  2179. i = 0;
  2180. }
  2181. return req;
  2182. }
  2183. void hfi1_rc_rcv_tid_rdma_read_resp(struct hfi1_packet *packet)
  2184. {
  2185. /* HANDLER FOR TID RDMA READ RESPONSE packet (Requestor side */
  2186. /*
  2187. * 1. Find matching SWQE
  2188. * 2. Check that the entire segment has been read.
  2189. * 3. Remove HFI1_S_WAIT_TID_RESP from s_flags.
  2190. * 4. Free the TID flow resources.
  2191. * 5. Kick the send engine (hfi1_schedule_send())
  2192. */
  2193. struct ib_other_headers *ohdr = packet->ohdr;
  2194. struct rvt_qp *qp = packet->qp;
  2195. struct hfi1_qp_priv *priv = qp->priv;
  2196. struct hfi1_ctxtdata *rcd = packet->rcd;
  2197. struct tid_rdma_request *req;
  2198. struct tid_rdma_flow *flow;
  2199. u32 opcode, aeth;
  2200. bool fecn;
  2201. unsigned long flags;
  2202. u32 kpsn, ipsn;
  2203. trace_hfi1_sender_rcv_tid_read_resp(qp);
  2204. fecn = process_ecn(qp, packet);
  2205. kpsn = mask_psn(be32_to_cpu(ohdr->bth[2]));
  2206. aeth = be32_to_cpu(ohdr->u.tid_rdma.r_rsp.aeth);
  2207. opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0xff;
  2208. spin_lock_irqsave(&qp->s_lock, flags);
  2209. ipsn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.r_rsp.verbs_psn));
  2210. req = find_tid_request(qp, ipsn, IB_WR_TID_RDMA_READ);
  2211. if (unlikely(!req))
  2212. goto ack_op_err;
  2213. flow = &req->flows[req->clear_tail];
  2214. /* When header suppression is disabled */
  2215. if (cmp_psn(ipsn, flow->flow_state.ib_lpsn)) {
  2216. update_r_next_psn_fecn(packet, priv, rcd, flow, fecn);
  2217. if (cmp_psn(kpsn, flow->flow_state.r_next_psn))
  2218. goto ack_done;
  2219. flow->flow_state.r_next_psn = mask_psn(kpsn + 1);
  2220. /*
  2221. * Copy the payload to destination buffer if this packet is
  2222. * delivered as an eager packet due to RSM rule and FECN.
  2223. * The RSM rule selects FECN bit in BTH and SH bit in
  2224. * KDETH header and therefore will not match the last
  2225. * packet of each segment that has SH bit cleared.
  2226. */
  2227. if (fecn && packet->etype == RHF_RCV_TYPE_EAGER) {
  2228. struct rvt_sge_state ss;
  2229. u32 len;
  2230. u32 tlen = packet->tlen;
  2231. u16 hdrsize = packet->hlen;
  2232. u8 pad = packet->pad;
  2233. u8 extra_bytes = pad + packet->extra_byte +
  2234. (SIZE_OF_CRC << 2);
  2235. u32 pmtu = qp->pmtu;
  2236. if (unlikely(tlen != (hdrsize + pmtu + extra_bytes)))
  2237. goto ack_op_err;
  2238. len = restart_sge(&ss, req->e.swqe, ipsn, pmtu);
  2239. if (unlikely(len < pmtu))
  2240. goto ack_op_err;
  2241. rvt_copy_sge(qp, &ss, packet->payload, pmtu, false,
  2242. false);
  2243. /* Raise the sw sequence check flag for next packet */
  2244. priv->s_flags |= HFI1_R_TID_SW_PSN;
  2245. }
  2246. goto ack_done;
  2247. }
  2248. flow->flow_state.r_next_psn = mask_psn(kpsn + 1);
  2249. req->ack_pending--;
  2250. priv->pending_tid_r_segs--;
  2251. qp->s_num_rd_atomic--;
  2252. if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
  2253. !qp->s_num_rd_atomic) {
  2254. qp->s_flags &= ~(RVT_S_WAIT_FENCE |
  2255. RVT_S_WAIT_ACK);
  2256. hfi1_schedule_send(qp);
  2257. }
  2258. if (qp->s_flags & RVT_S_WAIT_RDMAR) {
  2259. qp->s_flags &= ~(RVT_S_WAIT_RDMAR | RVT_S_WAIT_ACK);
  2260. hfi1_schedule_send(qp);
  2261. }
  2262. trace_hfi1_ack(qp, ipsn);
  2263. trace_hfi1_tid_req_rcv_read_resp(qp, 0, req->e.swqe->wr.opcode,
  2264. req->e.swqe->psn, req->e.swqe->lpsn,
  2265. req);
  2266. trace_hfi1_tid_flow_rcv_read_resp(qp, req->clear_tail, flow);
  2267. /* Release the tid resources */
  2268. hfi1_kern_exp_rcv_clear(req);
  2269. if (!do_rc_ack(qp, aeth, ipsn, opcode, 0, rcd))
  2270. goto ack_done;
  2271. /* If not done yet, build next read request */
  2272. if (++req->comp_seg >= req->total_segs) {
  2273. priv->tid_r_comp++;
  2274. req->state = TID_REQUEST_COMPLETE;
  2275. }
  2276. /*
  2277. * Clear the hw flow under two conditions:
  2278. * 1. This request is a sync point and it is complete;
  2279. * 2. Current request is completed and there are no more requests.
  2280. */
  2281. if ((req->state == TID_REQUEST_SYNC &&
  2282. req->comp_seg == req->cur_seg) ||
  2283. priv->tid_r_comp == priv->tid_r_reqs) {
  2284. hfi1_kern_clear_hw_flow(priv->rcd, qp);
  2285. priv->s_flags &= ~HFI1_R_TID_SW_PSN;
  2286. if (req->state == TID_REQUEST_SYNC)
  2287. req->state = TID_REQUEST_ACTIVE;
  2288. }
  2289. hfi1_schedule_send(qp);
  2290. goto ack_done;
  2291. ack_op_err:
  2292. /*
  2293. * The test indicates that the send engine has finished its cleanup
  2294. * after sending the request and it's now safe to put the QP into error
  2295. * state. However, if the wqe queue is empty (qp->s_acked == qp->s_tail
  2296. * == qp->s_head), it would be unsafe to complete the wqe pointed by
  2297. * qp->s_acked here. Putting the qp into error state will safely flush
  2298. * all remaining requests.
  2299. */
  2300. if (qp->s_last == qp->s_acked)
  2301. rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  2302. ack_done:
  2303. spin_unlock_irqrestore(&qp->s_lock, flags);
  2304. }
  2305. void hfi1_kern_read_tid_flow_free(struct rvt_qp *qp)
  2306. __must_hold(&qp->s_lock)
  2307. {
  2308. u32 n = qp->s_acked;
  2309. struct rvt_swqe *wqe;
  2310. struct tid_rdma_request *req;
  2311. struct hfi1_qp_priv *priv = qp->priv;
  2312. lockdep_assert_held(&qp->s_lock);
  2313. /* Free any TID entries */
  2314. while (n != qp->s_tail) {
  2315. wqe = rvt_get_swqe_ptr(qp, n);
  2316. if (wqe->wr.opcode == IB_WR_TID_RDMA_READ) {
  2317. req = wqe_to_tid_req(wqe);
  2318. hfi1_kern_exp_rcv_clear_all(req);
  2319. }
  2320. if (++n == qp->s_size)
  2321. n = 0;
  2322. }
  2323. /* Free flow */
  2324. hfi1_kern_clear_hw_flow(priv->rcd, qp);
  2325. }
  2326. static bool tid_rdma_tid_err(struct hfi1_packet *packet, u8 rcv_type)
  2327. {
  2328. struct rvt_qp *qp = packet->qp;
  2329. if (rcv_type >= RHF_RCV_TYPE_IB)
  2330. goto done;
  2331. spin_lock(&qp->s_lock);
  2332. /*
  2333. * We've ran out of space in the eager buffer.
  2334. * Eagerly received KDETH packets which require space in the
  2335. * Eager buffer (packet that have payload) are TID RDMA WRITE
  2336. * response packets. In this case, we have to re-transmit the
  2337. * TID RDMA WRITE request.
  2338. */
  2339. if (rcv_type == RHF_RCV_TYPE_EAGER) {
  2340. hfi1_restart_rc(qp, qp->s_last_psn + 1, 1);
  2341. hfi1_schedule_send(qp);
  2342. }
  2343. /* Since no payload is delivered, just drop the packet */
  2344. spin_unlock(&qp->s_lock);
  2345. done:
  2346. return true;
  2347. }
  2348. static void restart_tid_rdma_read_req(struct hfi1_ctxtdata *rcd,
  2349. struct rvt_qp *qp, struct rvt_swqe *wqe)
  2350. {
  2351. struct tid_rdma_request *req;
  2352. struct tid_rdma_flow *flow;
  2353. /* Start from the right segment */
  2354. qp->r_flags |= RVT_R_RDMAR_SEQ;
  2355. req = wqe_to_tid_req(wqe);
  2356. flow = &req->flows[req->clear_tail];
  2357. hfi1_restart_rc(qp, flow->flow_state.ib_spsn, 0);
  2358. if (list_empty(&qp->rspwait)) {
  2359. qp->r_flags |= RVT_R_RSP_SEND;
  2360. rvt_get_qp(qp);
  2361. list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
  2362. }
  2363. }
  2364. /*
  2365. * Handle the KDETH eflags for TID RDMA READ response.
  2366. *
  2367. * Return true if the last packet for a segment has been received and it is
  2368. * time to process the response normally; otherwise, return true.
  2369. *
  2370. * The caller must hold the packet->qp->r_lock and the rcu_read_lock.
  2371. */
  2372. static bool handle_read_kdeth_eflags(struct hfi1_ctxtdata *rcd,
  2373. struct hfi1_packet *packet, u8 rcv_type,
  2374. u8 rte, u32 psn, u32 ibpsn)
  2375. __must_hold(&packet->qp->r_lock) __must_hold(RCU)
  2376. {
  2377. struct hfi1_pportdata *ppd = rcd->ppd;
  2378. struct hfi1_devdata *dd = ppd->dd;
  2379. struct hfi1_ibport *ibp;
  2380. struct rvt_swqe *wqe;
  2381. struct tid_rdma_request *req;
  2382. struct tid_rdma_flow *flow;
  2383. u32 ack_psn;
  2384. struct rvt_qp *qp = packet->qp;
  2385. struct hfi1_qp_priv *priv = qp->priv;
  2386. bool ret = true;
  2387. int diff = 0;
  2388. u32 fpsn;
  2389. lockdep_assert_held(&qp->r_lock);
  2390. trace_hfi1_rsp_read_kdeth_eflags(qp, ibpsn);
  2391. trace_hfi1_sender_read_kdeth_eflags(qp);
  2392. trace_hfi1_tid_read_sender_kdeth_eflags(qp, 0);
  2393. spin_lock(&qp->s_lock);
  2394. /* If the psn is out of valid range, drop the packet */
  2395. if (cmp_psn(ibpsn, qp->s_last_psn) < 0 ||
  2396. cmp_psn(ibpsn, qp->s_psn) > 0)
  2397. goto s_unlock;
  2398. /*
  2399. * Note that NAKs implicitly ACK outstanding SEND and RDMA write
  2400. * requests and implicitly NAK RDMA read and atomic requests issued
  2401. * before the NAK'ed request.
  2402. */
  2403. ack_psn = ibpsn - 1;
  2404. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  2405. ibp = to_iport(qp->ibqp.device, qp->port_num);
  2406. /* Complete WQEs that the PSN finishes. */
  2407. while ((int)delta_psn(ack_psn, wqe->lpsn) >= 0) {
  2408. /*
  2409. * If this request is a RDMA read or atomic, and the NACK is
  2410. * for a later operation, this NACK NAKs the RDMA read or
  2411. * atomic.
  2412. */
  2413. if (wqe->wr.opcode == IB_WR_RDMA_READ ||
  2414. wqe->wr.opcode == IB_WR_TID_RDMA_READ ||
  2415. wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  2416. wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
  2417. /* Retry this request. */
  2418. if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
  2419. qp->r_flags |= RVT_R_RDMAR_SEQ;
  2420. if (wqe->wr.opcode == IB_WR_TID_RDMA_READ) {
  2421. restart_tid_rdma_read_req(rcd, qp,
  2422. wqe);
  2423. } else {
  2424. hfi1_restart_rc(qp, qp->s_last_psn + 1,
  2425. 0);
  2426. if (list_empty(&qp->rspwait)) {
  2427. qp->r_flags |= RVT_R_RSP_SEND;
  2428. rvt_get_qp(qp);
  2429. list_add_tail(/* wait */
  2430. &qp->rspwait,
  2431. &rcd->qp_wait_list);
  2432. }
  2433. }
  2434. }
  2435. /*
  2436. * No need to process the NAK since we are
  2437. * restarting an earlier request.
  2438. */
  2439. break;
  2440. }
  2441. wqe = do_rc_completion(qp, wqe, ibp);
  2442. if (qp->s_acked == qp->s_tail)
  2443. goto s_unlock;
  2444. }
  2445. if (qp->s_acked == qp->s_tail)
  2446. goto s_unlock;
  2447. /* Handle the eflags for the request */
  2448. if (wqe->wr.opcode != IB_WR_TID_RDMA_READ)
  2449. goto s_unlock;
  2450. req = wqe_to_tid_req(wqe);
  2451. trace_hfi1_tid_req_read_kdeth_eflags(qp, 0, wqe->wr.opcode, wqe->psn,
  2452. wqe->lpsn, req);
  2453. switch (rcv_type) {
  2454. case RHF_RCV_TYPE_EXPECTED:
  2455. switch (rte) {
  2456. case RHF_RTE_EXPECTED_FLOW_SEQ_ERR:
  2457. /*
  2458. * On the first occurrence of a Flow Sequence error,
  2459. * the flag TID_FLOW_SW_PSN is set.
  2460. *
  2461. * After that, the flow is *not* reprogrammed and the
  2462. * protocol falls back to SW PSN checking. This is done
  2463. * to prevent continuous Flow Sequence errors for any
  2464. * packets that could be still in the fabric.
  2465. */
  2466. flow = &req->flows[req->clear_tail];
  2467. trace_hfi1_tid_flow_read_kdeth_eflags(qp,
  2468. req->clear_tail,
  2469. flow);
  2470. if (priv->s_flags & HFI1_R_TID_SW_PSN) {
  2471. diff = cmp_psn(psn,
  2472. flow->flow_state.r_next_psn);
  2473. if (diff > 0) {
  2474. /* Drop the packet.*/
  2475. goto s_unlock;
  2476. } else if (diff < 0) {
  2477. /*
  2478. * If a response packet for a restarted
  2479. * request has come back, reset the
  2480. * restart flag.
  2481. */
  2482. if (qp->r_flags & RVT_R_RDMAR_SEQ)
  2483. qp->r_flags &=
  2484. ~RVT_R_RDMAR_SEQ;
  2485. /* Drop the packet.*/
  2486. goto s_unlock;
  2487. }
  2488. /*
  2489. * If SW PSN verification is successful and
  2490. * this is the last packet in the segment, tell
  2491. * the caller to process it as a normal packet.
  2492. */
  2493. fpsn = full_flow_psn(flow,
  2494. flow->flow_state.lpsn);
  2495. if (cmp_psn(fpsn, psn) == 0) {
  2496. ret = false;
  2497. if (qp->r_flags & RVT_R_RDMAR_SEQ)
  2498. qp->r_flags &=
  2499. ~RVT_R_RDMAR_SEQ;
  2500. }
  2501. flow->flow_state.r_next_psn =
  2502. mask_psn(psn + 1);
  2503. } else {
  2504. u32 last_psn;
  2505. last_psn = read_r_next_psn(dd, rcd->ctxt,
  2506. flow->idx);
  2507. flow->flow_state.r_next_psn = last_psn;
  2508. priv->s_flags |= HFI1_R_TID_SW_PSN;
  2509. /*
  2510. * If no request has been restarted yet,
  2511. * restart the current one.
  2512. */
  2513. if (!(qp->r_flags & RVT_R_RDMAR_SEQ))
  2514. restart_tid_rdma_read_req(rcd, qp,
  2515. wqe);
  2516. }
  2517. break;
  2518. case RHF_RTE_EXPECTED_FLOW_GEN_ERR:
  2519. /*
  2520. * Since the TID flow is able to ride through
  2521. * generation mismatch, drop this stale packet.
  2522. */
  2523. break;
  2524. default:
  2525. break;
  2526. }
  2527. break;
  2528. case RHF_RCV_TYPE_ERROR:
  2529. switch (rte) {
  2530. case RHF_RTE_ERROR_OP_CODE_ERR:
  2531. case RHF_RTE_ERROR_KHDR_MIN_LEN_ERR:
  2532. case RHF_RTE_ERROR_KHDR_HCRC_ERR:
  2533. case RHF_RTE_ERROR_KHDR_KVER_ERR:
  2534. case RHF_RTE_ERROR_CONTEXT_ERR:
  2535. case RHF_RTE_ERROR_KHDR_TID_ERR:
  2536. default:
  2537. break;
  2538. }
  2539. break;
  2540. default:
  2541. break;
  2542. }
  2543. s_unlock:
  2544. spin_unlock(&qp->s_lock);
  2545. return ret;
  2546. }
  2547. bool hfi1_handle_kdeth_eflags(struct hfi1_ctxtdata *rcd,
  2548. struct hfi1_pportdata *ppd,
  2549. struct hfi1_packet *packet)
  2550. {
  2551. struct hfi1_ibport *ibp = &ppd->ibport_data;
  2552. struct hfi1_devdata *dd = ppd->dd;
  2553. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  2554. u8 rcv_type = rhf_rcv_type(packet->rhf);
  2555. u8 rte = rhf_rcv_type_err(packet->rhf);
  2556. struct ib_header *hdr = packet->hdr;
  2557. struct ib_other_headers *ohdr = NULL;
  2558. int lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  2559. u16 lid = be16_to_cpu(hdr->lrh[1]);
  2560. u8 opcode;
  2561. u32 qp_num, psn, ibpsn;
  2562. struct rvt_qp *qp;
  2563. struct hfi1_qp_priv *qpriv;
  2564. unsigned long flags;
  2565. bool ret = true;
  2566. struct rvt_ack_entry *e;
  2567. struct tid_rdma_request *req;
  2568. struct tid_rdma_flow *flow;
  2569. int diff = 0;
  2570. trace_hfi1_msg_handle_kdeth_eflags(NULL, "Kdeth error: rhf ",
  2571. packet->rhf);
  2572. if (packet->rhf & RHF_ICRC_ERR)
  2573. return ret;
  2574. packet->ohdr = &hdr->u.oth;
  2575. ohdr = packet->ohdr;
  2576. trace_input_ibhdr(rcd->dd, packet, !!(rhf_dc_info(packet->rhf)));
  2577. /* Get the destination QP number. */
  2578. qp_num = be32_to_cpu(ohdr->u.tid_rdma.r_rsp.verbs_qp) &
  2579. RVT_QPN_MASK;
  2580. if (lid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
  2581. goto drop;
  2582. psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
  2583. opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0xff;
  2584. rcu_read_lock();
  2585. qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
  2586. if (!qp)
  2587. goto rcu_unlock;
  2588. packet->qp = qp;
  2589. /* Check for valid receive state. */
  2590. spin_lock_irqsave(&qp->r_lock, flags);
  2591. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
  2592. ibp->rvp.n_pkt_drops++;
  2593. goto r_unlock;
  2594. }
  2595. if (packet->rhf & RHF_TID_ERR) {
  2596. /* For TIDERR and RC QPs preemptively schedule a NAK */
  2597. u32 tlen = rhf_pkt_len(packet->rhf); /* in bytes */
  2598. /* Sanity check packet */
  2599. if (tlen < 24)
  2600. goto r_unlock;
  2601. /*
  2602. * Check for GRH. We should never get packets with GRH in this
  2603. * path.
  2604. */
  2605. if (lnh == HFI1_LRH_GRH)
  2606. goto r_unlock;
  2607. if (tid_rdma_tid_err(packet, rcv_type))
  2608. goto r_unlock;
  2609. }
  2610. /* handle TID RDMA READ */
  2611. if (opcode == TID_OP(READ_RESP)) {
  2612. ibpsn = be32_to_cpu(ohdr->u.tid_rdma.r_rsp.verbs_psn);
  2613. ibpsn = mask_psn(ibpsn);
  2614. ret = handle_read_kdeth_eflags(rcd, packet, rcv_type, rte, psn,
  2615. ibpsn);
  2616. goto r_unlock;
  2617. }
  2618. /*
  2619. * qp->s_tail_ack_queue points to the rvt_ack_entry currently being
  2620. * processed. These a completed sequentially so we can be sure that
  2621. * the pointer will not change until the entire request has completed.
  2622. */
  2623. spin_lock(&qp->s_lock);
  2624. qpriv = qp->priv;
  2625. if (qpriv->r_tid_tail == HFI1_QP_WQE_INVALID ||
  2626. qpriv->r_tid_tail == qpriv->r_tid_head)
  2627. goto unlock;
  2628. e = &qp->s_ack_queue[qpriv->r_tid_tail];
  2629. if (e->opcode != TID_OP(WRITE_REQ))
  2630. goto unlock;
  2631. req = ack_to_tid_req(e);
  2632. if (req->comp_seg == req->cur_seg)
  2633. goto unlock;
  2634. flow = &req->flows[req->clear_tail];
  2635. trace_hfi1_eflags_err_write(qp, rcv_type, rte, psn);
  2636. trace_hfi1_rsp_handle_kdeth_eflags(qp, psn);
  2637. trace_hfi1_tid_write_rsp_handle_kdeth_eflags(qp);
  2638. trace_hfi1_tid_req_handle_kdeth_eflags(qp, 0, e->opcode, e->psn,
  2639. e->lpsn, req);
  2640. trace_hfi1_tid_flow_handle_kdeth_eflags(qp, req->clear_tail, flow);
  2641. switch (rcv_type) {
  2642. case RHF_RCV_TYPE_EXPECTED:
  2643. switch (rte) {
  2644. case RHF_RTE_EXPECTED_FLOW_SEQ_ERR:
  2645. if (!(qpriv->s_flags & HFI1_R_TID_SW_PSN)) {
  2646. qpriv->s_flags |= HFI1_R_TID_SW_PSN;
  2647. flow->flow_state.r_next_psn =
  2648. read_r_next_psn(dd, rcd->ctxt,
  2649. flow->idx);
  2650. qpriv->r_next_psn_kdeth =
  2651. flow->flow_state.r_next_psn;
  2652. goto nak_psn;
  2653. } else {
  2654. /*
  2655. * If the received PSN does not match the next
  2656. * expected PSN, NAK the packet.
  2657. * However, only do that if we know that the a
  2658. * NAK has already been sent. Otherwise, this
  2659. * mismatch could be due to packets that were
  2660. * already in flight.
  2661. */
  2662. diff = cmp_psn(psn,
  2663. flow->flow_state.r_next_psn);
  2664. if (diff > 0)
  2665. goto nak_psn;
  2666. else if (diff < 0)
  2667. break;
  2668. qpriv->s_nak_state = 0;
  2669. /*
  2670. * If SW PSN verification is successful and this
  2671. * is the last packet in the segment, tell the
  2672. * caller to process it as a normal packet.
  2673. */
  2674. if (psn == full_flow_psn(flow,
  2675. flow->flow_state.lpsn))
  2676. ret = false;
  2677. flow->flow_state.r_next_psn =
  2678. mask_psn(psn + 1);
  2679. qpriv->r_next_psn_kdeth =
  2680. flow->flow_state.r_next_psn;
  2681. }
  2682. break;
  2683. case RHF_RTE_EXPECTED_FLOW_GEN_ERR:
  2684. goto nak_psn;
  2685. default:
  2686. break;
  2687. }
  2688. break;
  2689. case RHF_RCV_TYPE_ERROR:
  2690. switch (rte) {
  2691. case RHF_RTE_ERROR_OP_CODE_ERR:
  2692. case RHF_RTE_ERROR_KHDR_MIN_LEN_ERR:
  2693. case RHF_RTE_ERROR_KHDR_HCRC_ERR:
  2694. case RHF_RTE_ERROR_KHDR_KVER_ERR:
  2695. case RHF_RTE_ERROR_CONTEXT_ERR:
  2696. case RHF_RTE_ERROR_KHDR_TID_ERR:
  2697. default:
  2698. break;
  2699. }
  2700. break;
  2701. default:
  2702. break;
  2703. }
  2704. unlock:
  2705. spin_unlock(&qp->s_lock);
  2706. r_unlock:
  2707. spin_unlock_irqrestore(&qp->r_lock, flags);
  2708. rcu_unlock:
  2709. rcu_read_unlock();
  2710. drop:
  2711. return ret;
  2712. nak_psn:
  2713. ibp->rvp.n_rc_seqnak++;
  2714. if (!qpriv->s_nak_state) {
  2715. qpriv->s_nak_state = IB_NAK_PSN_ERROR;
  2716. /* We are NAK'ing the next expected PSN */
  2717. qpriv->s_nak_psn = mask_psn(flow->flow_state.r_next_psn);
  2718. tid_rdma_trigger_ack(qp);
  2719. }
  2720. goto unlock;
  2721. }
  2722. /*
  2723. * "Rewind" the TID request information.
  2724. * This means that we reset the state back to ACTIVE,
  2725. * find the proper flow, set the flow index to that flow,
  2726. * and reset the flow information.
  2727. */
  2728. void hfi1_tid_rdma_restart_req(struct rvt_qp *qp, struct rvt_swqe *wqe,
  2729. u32 *bth2)
  2730. {
  2731. struct tid_rdma_request *req = wqe_to_tid_req(wqe);
  2732. struct tid_rdma_flow *flow;
  2733. struct hfi1_qp_priv *qpriv = qp->priv;
  2734. int diff, delta_pkts;
  2735. u32 tididx = 0, i;
  2736. u16 fidx;
  2737. if (wqe->wr.opcode == IB_WR_TID_RDMA_READ) {
  2738. *bth2 = mask_psn(qp->s_psn);
  2739. flow = find_flow_ib(req, *bth2, &fidx);
  2740. if (!flow) {
  2741. trace_hfi1_msg_tid_restart_req(/* msg */
  2742. qp, "!!!!!! Could not find flow to restart: bth2 ",
  2743. (u64)*bth2);
  2744. trace_hfi1_tid_req_restart_req(qp, 0, wqe->wr.opcode,
  2745. wqe->psn, wqe->lpsn,
  2746. req);
  2747. return;
  2748. }
  2749. } else {
  2750. fidx = req->acked_tail;
  2751. flow = &req->flows[fidx];
  2752. *bth2 = mask_psn(req->r_ack_psn);
  2753. }
  2754. if (wqe->wr.opcode == IB_WR_TID_RDMA_READ)
  2755. delta_pkts = delta_psn(*bth2, flow->flow_state.ib_spsn);
  2756. else
  2757. delta_pkts = delta_psn(*bth2,
  2758. full_flow_psn(flow,
  2759. flow->flow_state.spsn));
  2760. trace_hfi1_tid_flow_restart_req(qp, fidx, flow);
  2761. diff = delta_pkts + flow->resync_npkts;
  2762. flow->sent = 0;
  2763. flow->pkt = 0;
  2764. flow->tid_idx = 0;
  2765. flow->tid_offset = 0;
  2766. if (diff) {
  2767. for (tididx = 0; tididx < flow->tidcnt; tididx++) {
  2768. u32 tidentry = flow->tid_entry[tididx], tidlen,
  2769. tidnpkts, npkts;
  2770. flow->tid_offset = 0;
  2771. tidlen = EXP_TID_GET(tidentry, LEN) * PAGE_SIZE;
  2772. tidnpkts = rvt_div_round_up_mtu(qp, tidlen);
  2773. npkts = min_t(u32, diff, tidnpkts);
  2774. flow->pkt += npkts;
  2775. flow->sent += (npkts == tidnpkts ? tidlen :
  2776. npkts * qp->pmtu);
  2777. flow->tid_offset += npkts * qp->pmtu;
  2778. diff -= npkts;
  2779. if (!diff)
  2780. break;
  2781. }
  2782. }
  2783. if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE) {
  2784. rvt_skip_sge(&qpriv->tid_ss, (req->cur_seg * req->seg_len) +
  2785. flow->sent, 0);
  2786. /*
  2787. * Packet PSN is based on flow_state.spsn + flow->pkt. However,
  2788. * during a RESYNC, the generation is incremented and the
  2789. * sequence is reset to 0. Since we've adjusted the npkts in the
  2790. * flow and the SGE has been sufficiently advanced, we have to
  2791. * adjust flow->pkt in order to calculate the correct PSN.
  2792. */
  2793. flow->pkt -= flow->resync_npkts;
  2794. }
  2795. if (flow->tid_offset ==
  2796. EXP_TID_GET(flow->tid_entry[tididx], LEN) * PAGE_SIZE) {
  2797. tididx++;
  2798. flow->tid_offset = 0;
  2799. }
  2800. flow->tid_idx = tididx;
  2801. if (wqe->wr.opcode == IB_WR_TID_RDMA_READ)
  2802. /* Move flow_idx to correct index */
  2803. req->flow_idx = fidx;
  2804. else
  2805. req->clear_tail = fidx;
  2806. trace_hfi1_tid_flow_restart_req(qp, fidx, flow);
  2807. trace_hfi1_tid_req_restart_req(qp, 0, wqe->wr.opcode, wqe->psn,
  2808. wqe->lpsn, req);
  2809. req->state = TID_REQUEST_ACTIVE;
  2810. if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE) {
  2811. /* Reset all the flows that we are going to resend */
  2812. fidx = CIRC_NEXT(fidx, MAX_FLOWS);
  2813. i = qpriv->s_tid_tail;
  2814. do {
  2815. for (; CIRC_CNT(req->setup_head, fidx, MAX_FLOWS);
  2816. fidx = CIRC_NEXT(fidx, MAX_FLOWS)) {
  2817. req->flows[fidx].sent = 0;
  2818. req->flows[fidx].pkt = 0;
  2819. req->flows[fidx].tid_idx = 0;
  2820. req->flows[fidx].tid_offset = 0;
  2821. req->flows[fidx].resync_npkts = 0;
  2822. }
  2823. if (i == qpriv->s_tid_cur)
  2824. break;
  2825. do {
  2826. i = (++i == qp->s_size ? 0 : i);
  2827. wqe = rvt_get_swqe_ptr(qp, i);
  2828. } while (wqe->wr.opcode != IB_WR_TID_RDMA_WRITE);
  2829. req = wqe_to_tid_req(wqe);
  2830. req->cur_seg = req->ack_seg;
  2831. fidx = req->acked_tail;
  2832. /* Pull req->clear_tail back */
  2833. req->clear_tail = fidx;
  2834. } while (1);
  2835. }
  2836. }
  2837. void hfi1_qp_kern_exp_rcv_clear_all(struct rvt_qp *qp)
  2838. {
  2839. int i, ret;
  2840. struct hfi1_qp_priv *qpriv = qp->priv;
  2841. struct tid_flow_state *fs;
  2842. if (qp->ibqp.qp_type != IB_QPT_RC || !HFI1_CAP_IS_KSET(TID_RDMA))
  2843. return;
  2844. /*
  2845. * First, clear the flow to help prevent any delayed packets from
  2846. * being delivered.
  2847. */
  2848. fs = &qpriv->flow_state;
  2849. if (fs->index != RXE_NUM_TID_FLOWS)
  2850. hfi1_kern_clear_hw_flow(qpriv->rcd, qp);
  2851. for (i = qp->s_acked; i != qp->s_head;) {
  2852. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, i);
  2853. if (++i == qp->s_size)
  2854. i = 0;
  2855. /* Free only locally allocated TID entries */
  2856. if (wqe->wr.opcode != IB_WR_TID_RDMA_READ)
  2857. continue;
  2858. do {
  2859. struct hfi1_swqe_priv *priv = wqe->priv;
  2860. ret = hfi1_kern_exp_rcv_clear(&priv->tid_req);
  2861. } while (!ret);
  2862. }
  2863. for (i = qp->s_acked_ack_queue; i != qp->r_head_ack_queue;) {
  2864. struct rvt_ack_entry *e = &qp->s_ack_queue[i];
  2865. if (++i == rvt_max_atomic(ib_to_rvt(qp->ibqp.device)))
  2866. i = 0;
  2867. /* Free only locally allocated TID entries */
  2868. if (e->opcode != TID_OP(WRITE_REQ))
  2869. continue;
  2870. do {
  2871. struct hfi1_ack_priv *priv = e->priv;
  2872. ret = hfi1_kern_exp_rcv_clear(&priv->tid_req);
  2873. } while (!ret);
  2874. }
  2875. }
  2876. bool hfi1_tid_rdma_wqe_interlock(struct rvt_qp *qp, struct rvt_swqe *wqe)
  2877. {
  2878. struct rvt_swqe *prev;
  2879. struct hfi1_qp_priv *priv = qp->priv;
  2880. u32 s_prev;
  2881. struct tid_rdma_request *req;
  2882. s_prev = (qp->s_cur == 0 ? qp->s_size : qp->s_cur) - 1;
  2883. prev = rvt_get_swqe_ptr(qp, s_prev);
  2884. switch (wqe->wr.opcode) {
  2885. case IB_WR_SEND:
  2886. case IB_WR_SEND_WITH_IMM:
  2887. case IB_WR_SEND_WITH_INV:
  2888. case IB_WR_ATOMIC_CMP_AND_SWP:
  2889. case IB_WR_ATOMIC_FETCH_AND_ADD:
  2890. case IB_WR_RDMA_WRITE:
  2891. case IB_WR_RDMA_WRITE_WITH_IMM:
  2892. switch (prev->wr.opcode) {
  2893. case IB_WR_TID_RDMA_WRITE:
  2894. req = wqe_to_tid_req(prev);
  2895. if (req->ack_seg != req->total_segs)
  2896. goto interlock;
  2897. break;
  2898. default:
  2899. break;
  2900. }
  2901. break;
  2902. case IB_WR_RDMA_READ:
  2903. if (prev->wr.opcode != IB_WR_TID_RDMA_WRITE)
  2904. break;
  2905. fallthrough;
  2906. case IB_WR_TID_RDMA_READ:
  2907. switch (prev->wr.opcode) {
  2908. case IB_WR_RDMA_READ:
  2909. if (qp->s_acked != qp->s_cur)
  2910. goto interlock;
  2911. break;
  2912. case IB_WR_TID_RDMA_WRITE:
  2913. req = wqe_to_tid_req(prev);
  2914. if (req->ack_seg != req->total_segs)
  2915. goto interlock;
  2916. break;
  2917. default:
  2918. break;
  2919. }
  2920. break;
  2921. default:
  2922. break;
  2923. }
  2924. return false;
  2925. interlock:
  2926. priv->s_flags |= HFI1_S_TID_WAIT_INTERLCK;
  2927. return true;
  2928. }
  2929. /* Does @sge meet the alignment requirements for tid rdma? */
  2930. static inline bool hfi1_check_sge_align(struct rvt_qp *qp,
  2931. struct rvt_sge *sge, int num_sge)
  2932. {
  2933. int i;
  2934. for (i = 0; i < num_sge; i++, sge++) {
  2935. trace_hfi1_sge_check_align(qp, i, sge);
  2936. if ((u64)sge->vaddr & ~PAGE_MASK ||
  2937. sge->sge_length & ~PAGE_MASK)
  2938. return false;
  2939. }
  2940. return true;
  2941. }
  2942. void setup_tid_rdma_wqe(struct rvt_qp *qp, struct rvt_swqe *wqe)
  2943. {
  2944. struct hfi1_qp_priv *qpriv = (struct hfi1_qp_priv *)qp->priv;
  2945. struct hfi1_swqe_priv *priv = wqe->priv;
  2946. struct tid_rdma_params *remote;
  2947. enum ib_wr_opcode new_opcode;
  2948. bool do_tid_rdma = false;
  2949. struct hfi1_pportdata *ppd = qpriv->rcd->ppd;
  2950. if ((rdma_ah_get_dlid(&qp->remote_ah_attr) & ~((1 << ppd->lmc) - 1)) ==
  2951. ppd->lid)
  2952. return;
  2953. if (qpriv->hdr_type != HFI1_PKT_TYPE_9B)
  2954. return;
  2955. rcu_read_lock();
  2956. remote = rcu_dereference(qpriv->tid_rdma.remote);
  2957. /*
  2958. * If TID RDMA is disabled by the negotiation, don't
  2959. * use it.
  2960. */
  2961. if (!remote)
  2962. goto exit;
  2963. if (wqe->wr.opcode == IB_WR_RDMA_READ) {
  2964. if (hfi1_check_sge_align(qp, &wqe->sg_list[0],
  2965. wqe->wr.num_sge)) {
  2966. new_opcode = IB_WR_TID_RDMA_READ;
  2967. do_tid_rdma = true;
  2968. }
  2969. } else if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
  2970. /*
  2971. * TID RDMA is enabled for this RDMA WRITE request iff:
  2972. * 1. The remote address is page-aligned,
  2973. * 2. The length is larger than the minimum segment size,
  2974. * 3. The length is page-multiple.
  2975. */
  2976. if (!(wqe->rdma_wr.remote_addr & ~PAGE_MASK) &&
  2977. !(wqe->length & ~PAGE_MASK)) {
  2978. new_opcode = IB_WR_TID_RDMA_WRITE;
  2979. do_tid_rdma = true;
  2980. }
  2981. }
  2982. if (do_tid_rdma) {
  2983. if (hfi1_kern_exp_rcv_alloc_flows(&priv->tid_req, GFP_ATOMIC))
  2984. goto exit;
  2985. wqe->wr.opcode = new_opcode;
  2986. priv->tid_req.seg_len =
  2987. min_t(u32, remote->max_len, wqe->length);
  2988. priv->tid_req.total_segs =
  2989. DIV_ROUND_UP(wqe->length, priv->tid_req.seg_len);
  2990. /* Compute the last PSN of the request */
  2991. wqe->lpsn = wqe->psn;
  2992. if (wqe->wr.opcode == IB_WR_TID_RDMA_READ) {
  2993. priv->tid_req.n_flows = remote->max_read;
  2994. qpriv->tid_r_reqs++;
  2995. wqe->lpsn += rvt_div_round_up_mtu(qp, wqe->length) - 1;
  2996. } else {
  2997. wqe->lpsn += priv->tid_req.total_segs - 1;
  2998. atomic_inc(&qpriv->n_requests);
  2999. }
  3000. priv->tid_req.cur_seg = 0;
  3001. priv->tid_req.comp_seg = 0;
  3002. priv->tid_req.ack_seg = 0;
  3003. priv->tid_req.state = TID_REQUEST_INACTIVE;
  3004. /*
  3005. * Reset acked_tail.
  3006. * TID RDMA READ does not have ACKs so it does not
  3007. * update the pointer. We have to reset it so TID RDMA
  3008. * WRITE does not get confused.
  3009. */
  3010. priv->tid_req.acked_tail = priv->tid_req.setup_head;
  3011. trace_hfi1_tid_req_setup_tid_wqe(qp, 1, wqe->wr.opcode,
  3012. wqe->psn, wqe->lpsn,
  3013. &priv->tid_req);
  3014. }
  3015. exit:
  3016. rcu_read_unlock();
  3017. }
  3018. /* TID RDMA WRITE functions */
  3019. u32 hfi1_build_tid_rdma_write_req(struct rvt_qp *qp, struct rvt_swqe *wqe,
  3020. struct ib_other_headers *ohdr,
  3021. u32 *bth1, u32 *bth2, u32 *len)
  3022. {
  3023. struct hfi1_qp_priv *qpriv = qp->priv;
  3024. struct tid_rdma_request *req = wqe_to_tid_req(wqe);
  3025. struct tid_rdma_params *remote;
  3026. rcu_read_lock();
  3027. remote = rcu_dereference(qpriv->tid_rdma.remote);
  3028. /*
  3029. * Set the number of flow to be used based on negotiated
  3030. * parameters.
  3031. */
  3032. req->n_flows = remote->max_write;
  3033. req->state = TID_REQUEST_ACTIVE;
  3034. KDETH_RESET(ohdr->u.tid_rdma.w_req.kdeth0, KVER, 0x1);
  3035. KDETH_RESET(ohdr->u.tid_rdma.w_req.kdeth1, JKEY, remote->jkey);
  3036. ohdr->u.tid_rdma.w_req.reth.vaddr =
  3037. cpu_to_be64(wqe->rdma_wr.remote_addr + (wqe->length - *len));
  3038. ohdr->u.tid_rdma.w_req.reth.rkey =
  3039. cpu_to_be32(wqe->rdma_wr.rkey);
  3040. ohdr->u.tid_rdma.w_req.reth.length = cpu_to_be32(*len);
  3041. ohdr->u.tid_rdma.w_req.verbs_qp = cpu_to_be32(qp->remote_qpn);
  3042. *bth1 &= ~RVT_QPN_MASK;
  3043. *bth1 |= remote->qp;
  3044. qp->s_state = TID_OP(WRITE_REQ);
  3045. qp->s_flags |= HFI1_S_WAIT_TID_RESP;
  3046. *bth2 |= IB_BTH_REQ_ACK;
  3047. *len = 0;
  3048. rcu_read_unlock();
  3049. return sizeof(ohdr->u.tid_rdma.w_req) / sizeof(u32);
  3050. }
  3051. static u32 hfi1_compute_tid_rdma_flow_wt(struct rvt_qp *qp)
  3052. {
  3053. /*
  3054. * Heuristic for computing the RNR timeout when waiting on the flow
  3055. * queue. Rather than a computationaly expensive exact estimate of when
  3056. * a flow will be available, we assume that if a QP is at position N in
  3057. * the flow queue it has to wait approximately (N + 1) * (number of
  3058. * segments between two sync points). The rationale for this is that
  3059. * flows are released and recycled at each sync point.
  3060. */
  3061. return (MAX_TID_FLOW_PSN * qp->pmtu) >> TID_RDMA_SEGMENT_SHIFT;
  3062. }
  3063. static u32 position_in_queue(struct hfi1_qp_priv *qpriv,
  3064. struct tid_queue *queue)
  3065. {
  3066. return qpriv->tid_enqueue - queue->dequeue;
  3067. }
  3068. /*
  3069. * @qp: points to rvt_qp context.
  3070. * @to_seg: desired RNR timeout in segments.
  3071. * Return: index of the next highest timeout in the ib_hfi1_rnr_table[]
  3072. */
  3073. static u32 hfi1_compute_tid_rnr_timeout(struct rvt_qp *qp, u32 to_seg)
  3074. {
  3075. struct hfi1_qp_priv *qpriv = qp->priv;
  3076. u64 timeout;
  3077. u32 bytes_per_us;
  3078. u8 i;
  3079. bytes_per_us = active_egress_rate(qpriv->rcd->ppd) / 8;
  3080. timeout = (to_seg * TID_RDMA_MAX_SEGMENT_SIZE) / bytes_per_us;
  3081. /*
  3082. * Find the next highest value in the RNR table to the required
  3083. * timeout. This gives the responder some padding.
  3084. */
  3085. for (i = 1; i <= IB_AETH_CREDIT_MASK; i++)
  3086. if (rvt_rnr_tbl_to_usec(i) >= timeout)
  3087. return i;
  3088. return 0;
  3089. }
  3090. /*
  3091. * Central place for resource allocation at TID write responder,
  3092. * is called from write_req and write_data interrupt handlers as
  3093. * well as the send thread when a queued QP is scheduled for
  3094. * resource allocation.
  3095. *
  3096. * Iterates over (a) segments of a request and then (b) queued requests
  3097. * themselves to allocate resources for up to local->max_write
  3098. * segments across multiple requests. Stop allocating when we
  3099. * hit a sync point, resume allocating after data packets at
  3100. * sync point have been received.
  3101. *
  3102. * Resource allocation and sending of responses is decoupled. The
  3103. * request/segment which are being allocated and sent are as follows.
  3104. * Resources are allocated for:
  3105. * [request: qpriv->r_tid_alloc, segment: req->alloc_seg]
  3106. * The send thread sends:
  3107. * [request: qp->s_tail_ack_queue, segment:req->cur_seg]
  3108. */
  3109. static void hfi1_tid_write_alloc_resources(struct rvt_qp *qp, bool intr_ctx)
  3110. {
  3111. struct tid_rdma_request *req;
  3112. struct hfi1_qp_priv *qpriv = qp->priv;
  3113. struct hfi1_ctxtdata *rcd = qpriv->rcd;
  3114. struct tid_rdma_params *local = &qpriv->tid_rdma.local;
  3115. struct rvt_ack_entry *e;
  3116. u32 npkts, to_seg;
  3117. bool last;
  3118. int ret = 0;
  3119. lockdep_assert_held(&qp->s_lock);
  3120. while (1) {
  3121. trace_hfi1_rsp_tid_write_alloc_res(qp, 0);
  3122. trace_hfi1_tid_write_rsp_alloc_res(qp);
  3123. /*
  3124. * Don't allocate more segments if a RNR NAK has already been
  3125. * scheduled to avoid messing up qp->r_psn: the RNR NAK will
  3126. * be sent only when all allocated segments have been sent.
  3127. * However, if more segments are allocated before that, TID RDMA
  3128. * WRITE RESP packets will be sent out for these new segments
  3129. * before the RNR NAK packet. When the requester receives the
  3130. * RNR NAK packet, it will restart with qp->s_last_psn + 1,
  3131. * which does not match qp->r_psn and will be dropped.
  3132. * Consequently, the requester will exhaust its retries and
  3133. * put the qp into error state.
  3134. */
  3135. if (qpriv->rnr_nak_state == TID_RNR_NAK_SEND)
  3136. break;
  3137. /* No requests left to process */
  3138. if (qpriv->r_tid_alloc == qpriv->r_tid_head) {
  3139. /* If all data has been received, clear the flow */
  3140. if (qpriv->flow_state.index < RXE_NUM_TID_FLOWS &&
  3141. !qpriv->alloc_w_segs) {
  3142. hfi1_kern_clear_hw_flow(rcd, qp);
  3143. qpriv->s_flags &= ~HFI1_R_TID_SW_PSN;
  3144. }
  3145. break;
  3146. }
  3147. e = &qp->s_ack_queue[qpriv->r_tid_alloc];
  3148. if (e->opcode != TID_OP(WRITE_REQ))
  3149. goto next_req;
  3150. req = ack_to_tid_req(e);
  3151. trace_hfi1_tid_req_write_alloc_res(qp, 0, e->opcode, e->psn,
  3152. e->lpsn, req);
  3153. /* Finished allocating for all segments of this request */
  3154. if (req->alloc_seg >= req->total_segs)
  3155. goto next_req;
  3156. /* Can allocate only a maximum of local->max_write for a QP */
  3157. if (qpriv->alloc_w_segs >= local->max_write)
  3158. break;
  3159. /* Don't allocate at a sync point with data packets pending */
  3160. if (qpriv->sync_pt && qpriv->alloc_w_segs)
  3161. break;
  3162. /* All data received at the sync point, continue */
  3163. if (qpriv->sync_pt && !qpriv->alloc_w_segs) {
  3164. hfi1_kern_clear_hw_flow(rcd, qp);
  3165. qpriv->sync_pt = false;
  3166. qpriv->s_flags &= ~HFI1_R_TID_SW_PSN;
  3167. }
  3168. /* Allocate flow if we don't have one */
  3169. if (qpriv->flow_state.index >= RXE_NUM_TID_FLOWS) {
  3170. ret = hfi1_kern_setup_hw_flow(qpriv->rcd, qp);
  3171. if (ret) {
  3172. to_seg = hfi1_compute_tid_rdma_flow_wt(qp) *
  3173. position_in_queue(qpriv,
  3174. &rcd->flow_queue);
  3175. break;
  3176. }
  3177. }
  3178. npkts = rvt_div_round_up_mtu(qp, req->seg_len);
  3179. /*
  3180. * We are at a sync point if we run out of KDETH PSN space.
  3181. * Last PSN of every generation is reserved for RESYNC.
  3182. */
  3183. if (qpriv->flow_state.psn + npkts > MAX_TID_FLOW_PSN - 1) {
  3184. qpriv->sync_pt = true;
  3185. break;
  3186. }
  3187. /*
  3188. * If overtaking req->acked_tail, send an RNR NAK. Because the
  3189. * QP is not queued in this case, and the issue can only be
  3190. * caused by a delay in scheduling the second leg which we
  3191. * cannot estimate, we use a rather arbitrary RNR timeout of
  3192. * (MAX_FLOWS / 2) segments
  3193. */
  3194. if (!CIRC_SPACE(req->setup_head, req->acked_tail,
  3195. MAX_FLOWS)) {
  3196. ret = -EAGAIN;
  3197. to_seg = MAX_FLOWS >> 1;
  3198. tid_rdma_trigger_ack(qp);
  3199. break;
  3200. }
  3201. /* Try to allocate rcv array / TID entries */
  3202. ret = hfi1_kern_exp_rcv_setup(req, &req->ss, &last);
  3203. if (ret == -EAGAIN)
  3204. to_seg = position_in_queue(qpriv, &rcd->rarr_queue);
  3205. if (ret)
  3206. break;
  3207. qpriv->alloc_w_segs++;
  3208. req->alloc_seg++;
  3209. continue;
  3210. next_req:
  3211. /* Begin processing the next request */
  3212. if (++qpriv->r_tid_alloc >
  3213. rvt_size_atomic(ib_to_rvt(qp->ibqp.device)))
  3214. qpriv->r_tid_alloc = 0;
  3215. }
  3216. /*
  3217. * Schedule an RNR NAK to be sent if (a) flow or rcv array allocation
  3218. * has failed (b) we are called from the rcv handler interrupt context
  3219. * (c) an RNR NAK has not already been scheduled
  3220. */
  3221. if (ret == -EAGAIN && intr_ctx && !qp->r_nak_state)
  3222. goto send_rnr_nak;
  3223. return;
  3224. send_rnr_nak:
  3225. lockdep_assert_held(&qp->r_lock);
  3226. /* Set r_nak_state to prevent unrelated events from generating NAK's */
  3227. qp->r_nak_state = hfi1_compute_tid_rnr_timeout(qp, to_seg) | IB_RNR_NAK;
  3228. /* Pull back r_psn to the segment being RNR NAK'd */
  3229. qp->r_psn = e->psn + req->alloc_seg;
  3230. qp->r_ack_psn = qp->r_psn;
  3231. /*
  3232. * Pull back r_head_ack_queue to the ack entry following the request
  3233. * being RNR NAK'd. This allows resources to be allocated to the request
  3234. * if the queued QP is scheduled.
  3235. */
  3236. qp->r_head_ack_queue = qpriv->r_tid_alloc + 1;
  3237. if (qp->r_head_ack_queue > rvt_size_atomic(ib_to_rvt(qp->ibqp.device)))
  3238. qp->r_head_ack_queue = 0;
  3239. qpriv->r_tid_head = qp->r_head_ack_queue;
  3240. /*
  3241. * These send side fields are used in make_rc_ack(). They are set in
  3242. * hfi1_send_rc_ack() but must be set here before dropping qp->s_lock
  3243. * for consistency
  3244. */
  3245. qp->s_nak_state = qp->r_nak_state;
  3246. qp->s_ack_psn = qp->r_ack_psn;
  3247. /*
  3248. * Clear the ACK PENDING flag to prevent unwanted ACK because we
  3249. * have modified qp->s_ack_psn here.
  3250. */
  3251. qp->s_flags &= ~(RVT_S_ACK_PENDING);
  3252. trace_hfi1_rsp_tid_write_alloc_res(qp, qp->r_psn);
  3253. /*
  3254. * qpriv->rnr_nak_state is used to determine when the scheduled RNR NAK
  3255. * has actually been sent. qp->s_flags RVT_S_ACK_PENDING bit cannot be
  3256. * used for this because qp->s_lock is dropped before calling
  3257. * hfi1_send_rc_ack() leading to inconsistency between the receive
  3258. * interrupt handlers and the send thread in make_rc_ack()
  3259. */
  3260. qpriv->rnr_nak_state = TID_RNR_NAK_SEND;
  3261. /*
  3262. * Schedule RNR NAK to be sent. RNR NAK's are scheduled from the receive
  3263. * interrupt handlers but will be sent from the send engine behind any
  3264. * previous responses that may have been scheduled
  3265. */
  3266. rc_defered_ack(rcd, qp);
  3267. }
  3268. void hfi1_rc_rcv_tid_rdma_write_req(struct hfi1_packet *packet)
  3269. {
  3270. /* HANDLER FOR TID RDMA WRITE REQUEST packet (Responder side)*/
  3271. /*
  3272. * 1. Verify TID RDMA WRITE REQ as per IB_OPCODE_RC_RDMA_WRITE_FIRST
  3273. * (see hfi1_rc_rcv())
  3274. * - Don't allow 0-length requests.
  3275. * 2. Put TID RDMA WRITE REQ into the response queueu (s_ack_queue)
  3276. * - Setup struct tid_rdma_req with request info
  3277. * - Prepare struct tid_rdma_flow array?
  3278. * 3. Set the qp->s_ack_state as state diagram in design doc.
  3279. * 4. Set RVT_S_RESP_PENDING in s_flags.
  3280. * 5. Kick the send engine (hfi1_schedule_send())
  3281. */
  3282. struct hfi1_ctxtdata *rcd = packet->rcd;
  3283. struct rvt_qp *qp = packet->qp;
  3284. struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  3285. struct ib_other_headers *ohdr = packet->ohdr;
  3286. struct rvt_ack_entry *e;
  3287. unsigned long flags;
  3288. struct ib_reth *reth;
  3289. struct hfi1_qp_priv *qpriv = qp->priv;
  3290. struct tid_rdma_request *req;
  3291. u32 bth0, psn, len, rkey, num_segs;
  3292. bool fecn;
  3293. u8 next;
  3294. u64 vaddr;
  3295. int diff;
  3296. bth0 = be32_to_cpu(ohdr->bth[0]);
  3297. if (hfi1_ruc_check_hdr(ibp, packet))
  3298. return;
  3299. fecn = process_ecn(qp, packet);
  3300. psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
  3301. trace_hfi1_rsp_rcv_tid_write_req(qp, psn);
  3302. if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
  3303. rvt_comm_est(qp);
  3304. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  3305. goto nack_inv;
  3306. reth = &ohdr->u.tid_rdma.w_req.reth;
  3307. vaddr = be64_to_cpu(reth->vaddr);
  3308. len = be32_to_cpu(reth->length);
  3309. num_segs = DIV_ROUND_UP(len, qpriv->tid_rdma.local.max_len);
  3310. diff = delta_psn(psn, qp->r_psn);
  3311. if (unlikely(diff)) {
  3312. tid_rdma_rcv_err(packet, ohdr, qp, psn, diff, fecn);
  3313. return;
  3314. }
  3315. /*
  3316. * The resent request which was previously RNR NAK'd is inserted at the
  3317. * location of the original request, which is one entry behind
  3318. * r_head_ack_queue
  3319. */
  3320. if (qpriv->rnr_nak_state)
  3321. qp->r_head_ack_queue = qp->r_head_ack_queue ?
  3322. qp->r_head_ack_queue - 1 :
  3323. rvt_size_atomic(ib_to_rvt(qp->ibqp.device));
  3324. /* We've verified the request, insert it into the ack queue. */
  3325. next = qp->r_head_ack_queue + 1;
  3326. if (next > rvt_size_atomic(ib_to_rvt(qp->ibqp.device)))
  3327. next = 0;
  3328. spin_lock_irqsave(&qp->s_lock, flags);
  3329. if (unlikely(next == qp->s_acked_ack_queue)) {
  3330. if (!qp->s_ack_queue[next].sent)
  3331. goto nack_inv_unlock;
  3332. update_ack_queue(qp, next);
  3333. }
  3334. e = &qp->s_ack_queue[qp->r_head_ack_queue];
  3335. req = ack_to_tid_req(e);
  3336. /* Bring previously RNR NAK'd request back to life */
  3337. if (qpriv->rnr_nak_state) {
  3338. qp->r_nak_state = 0;
  3339. qp->s_nak_state = 0;
  3340. qpriv->rnr_nak_state = TID_RNR_NAK_INIT;
  3341. qp->r_psn = e->lpsn + 1;
  3342. req->state = TID_REQUEST_INIT;
  3343. goto update_head;
  3344. }
  3345. release_rdma_sge_mr(e);
  3346. /* The length needs to be in multiples of PAGE_SIZE */
  3347. if (!len || len & ~PAGE_MASK)
  3348. goto nack_inv_unlock;
  3349. rkey = be32_to_cpu(reth->rkey);
  3350. qp->r_len = len;
  3351. if (e->opcode == TID_OP(WRITE_REQ) &&
  3352. (req->setup_head != req->clear_tail ||
  3353. req->clear_tail != req->acked_tail))
  3354. goto nack_inv_unlock;
  3355. if (unlikely(!rvt_rkey_ok(qp, &e->rdma_sge, qp->r_len, vaddr,
  3356. rkey, IB_ACCESS_REMOTE_WRITE)))
  3357. goto nack_acc;
  3358. qp->r_psn += num_segs - 1;
  3359. e->opcode = (bth0 >> 24) & 0xff;
  3360. e->psn = psn;
  3361. e->lpsn = qp->r_psn;
  3362. e->sent = 0;
  3363. req->n_flows = min_t(u16, num_segs, qpriv->tid_rdma.local.max_write);
  3364. req->state = TID_REQUEST_INIT;
  3365. req->cur_seg = 0;
  3366. req->comp_seg = 0;
  3367. req->ack_seg = 0;
  3368. req->alloc_seg = 0;
  3369. req->isge = 0;
  3370. req->seg_len = qpriv->tid_rdma.local.max_len;
  3371. req->total_len = len;
  3372. req->total_segs = num_segs;
  3373. req->r_flow_psn = e->psn;
  3374. req->ss.sge = e->rdma_sge;
  3375. req->ss.num_sge = 1;
  3376. req->flow_idx = req->setup_head;
  3377. req->clear_tail = req->setup_head;
  3378. req->acked_tail = req->setup_head;
  3379. qp->r_state = e->opcode;
  3380. qp->r_nak_state = 0;
  3381. /*
  3382. * We need to increment the MSN here instead of when we
  3383. * finish sending the result since a duplicate request would
  3384. * increment it more than once.
  3385. */
  3386. qp->r_msn++;
  3387. qp->r_psn++;
  3388. trace_hfi1_tid_req_rcv_write_req(qp, 0, e->opcode, e->psn, e->lpsn,
  3389. req);
  3390. if (qpriv->r_tid_tail == HFI1_QP_WQE_INVALID) {
  3391. qpriv->r_tid_tail = qp->r_head_ack_queue;
  3392. } else if (qpriv->r_tid_tail == qpriv->r_tid_head) {
  3393. struct tid_rdma_request *ptr;
  3394. e = &qp->s_ack_queue[qpriv->r_tid_tail];
  3395. ptr = ack_to_tid_req(e);
  3396. if (e->opcode != TID_OP(WRITE_REQ) ||
  3397. ptr->comp_seg == ptr->total_segs) {
  3398. if (qpriv->r_tid_tail == qpriv->r_tid_ack)
  3399. qpriv->r_tid_ack = qp->r_head_ack_queue;
  3400. qpriv->r_tid_tail = qp->r_head_ack_queue;
  3401. }
  3402. }
  3403. update_head:
  3404. qp->r_head_ack_queue = next;
  3405. qpriv->r_tid_head = qp->r_head_ack_queue;
  3406. hfi1_tid_write_alloc_resources(qp, true);
  3407. trace_hfi1_tid_write_rsp_rcv_req(qp);
  3408. /* Schedule the send tasklet. */
  3409. qp->s_flags |= RVT_S_RESP_PENDING;
  3410. if (fecn)
  3411. qp->s_flags |= RVT_S_ECN;
  3412. hfi1_schedule_send(qp);
  3413. spin_unlock_irqrestore(&qp->s_lock, flags);
  3414. return;
  3415. nack_inv_unlock:
  3416. spin_unlock_irqrestore(&qp->s_lock, flags);
  3417. nack_inv:
  3418. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  3419. qp->r_nak_state = IB_NAK_INVALID_REQUEST;
  3420. qp->r_ack_psn = qp->r_psn;
  3421. /* Queue NAK for later */
  3422. rc_defered_ack(rcd, qp);
  3423. return;
  3424. nack_acc:
  3425. spin_unlock_irqrestore(&qp->s_lock, flags);
  3426. rvt_rc_error(qp, IB_WC_LOC_PROT_ERR);
  3427. qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
  3428. qp->r_ack_psn = qp->r_psn;
  3429. }
  3430. u32 hfi1_build_tid_rdma_write_resp(struct rvt_qp *qp, struct rvt_ack_entry *e,
  3431. struct ib_other_headers *ohdr, u32 *bth1,
  3432. u32 bth2, u32 *len,
  3433. struct rvt_sge_state **ss)
  3434. {
  3435. struct hfi1_ack_priv *epriv = e->priv;
  3436. struct tid_rdma_request *req = &epriv->tid_req;
  3437. struct hfi1_qp_priv *qpriv = qp->priv;
  3438. struct tid_rdma_flow *flow = NULL;
  3439. u32 resp_len = 0, hdwords = 0;
  3440. void *resp_addr = NULL;
  3441. struct tid_rdma_params *remote;
  3442. trace_hfi1_tid_req_build_write_resp(qp, 0, e->opcode, e->psn, e->lpsn,
  3443. req);
  3444. trace_hfi1_tid_write_rsp_build_resp(qp);
  3445. trace_hfi1_rsp_build_tid_write_resp(qp, bth2);
  3446. flow = &req->flows[req->flow_idx];
  3447. switch (req->state) {
  3448. default:
  3449. /*
  3450. * Try to allocate resources here in case QP was queued and was
  3451. * later scheduled when resources became available
  3452. */
  3453. hfi1_tid_write_alloc_resources(qp, false);
  3454. /* We've already sent everything which is ready */
  3455. if (req->cur_seg >= req->alloc_seg)
  3456. goto done;
  3457. /*
  3458. * Resources can be assigned but responses cannot be sent in
  3459. * rnr_nak state, till the resent request is received
  3460. */
  3461. if (qpriv->rnr_nak_state == TID_RNR_NAK_SENT)
  3462. goto done;
  3463. req->state = TID_REQUEST_ACTIVE;
  3464. trace_hfi1_tid_flow_build_write_resp(qp, req->flow_idx, flow);
  3465. req->flow_idx = CIRC_NEXT(req->flow_idx, MAX_FLOWS);
  3466. hfi1_add_tid_reap_timer(qp);
  3467. break;
  3468. case TID_REQUEST_RESEND_ACTIVE:
  3469. case TID_REQUEST_RESEND:
  3470. trace_hfi1_tid_flow_build_write_resp(qp, req->flow_idx, flow);
  3471. req->flow_idx = CIRC_NEXT(req->flow_idx, MAX_FLOWS);
  3472. if (!CIRC_CNT(req->setup_head, req->flow_idx, MAX_FLOWS))
  3473. req->state = TID_REQUEST_ACTIVE;
  3474. hfi1_mod_tid_reap_timer(qp);
  3475. break;
  3476. }
  3477. flow->flow_state.resp_ib_psn = bth2;
  3478. resp_addr = (void *)flow->tid_entry;
  3479. resp_len = sizeof(*flow->tid_entry) * flow->tidcnt;
  3480. req->cur_seg++;
  3481. memset(&ohdr->u.tid_rdma.w_rsp, 0, sizeof(ohdr->u.tid_rdma.w_rsp));
  3482. epriv->ss.sge.vaddr = resp_addr;
  3483. epriv->ss.sge.sge_length = resp_len;
  3484. epriv->ss.sge.length = epriv->ss.sge.sge_length;
  3485. /*
  3486. * We can safely zero these out. Since the first SGE covers the
  3487. * entire packet, nothing else should even look at the MR.
  3488. */
  3489. epriv->ss.sge.mr = NULL;
  3490. epriv->ss.sge.m = 0;
  3491. epriv->ss.sge.n = 0;
  3492. epriv->ss.sg_list = NULL;
  3493. epriv->ss.total_len = epriv->ss.sge.sge_length;
  3494. epriv->ss.num_sge = 1;
  3495. *ss = &epriv->ss;
  3496. *len = epriv->ss.total_len;
  3497. /* Construct the TID RDMA WRITE RESP packet header */
  3498. rcu_read_lock();
  3499. remote = rcu_dereference(qpriv->tid_rdma.remote);
  3500. KDETH_RESET(ohdr->u.tid_rdma.w_rsp.kdeth0, KVER, 0x1);
  3501. KDETH_RESET(ohdr->u.tid_rdma.w_rsp.kdeth1, JKEY, remote->jkey);
  3502. ohdr->u.tid_rdma.w_rsp.aeth = rvt_compute_aeth(qp);
  3503. ohdr->u.tid_rdma.w_rsp.tid_flow_psn =
  3504. cpu_to_be32((flow->flow_state.generation <<
  3505. HFI1_KDETH_BTH_SEQ_SHIFT) |
  3506. (flow->flow_state.spsn &
  3507. HFI1_KDETH_BTH_SEQ_MASK));
  3508. ohdr->u.tid_rdma.w_rsp.tid_flow_qp =
  3509. cpu_to_be32(qpriv->tid_rdma.local.qp |
  3510. ((flow->idx & TID_RDMA_DESTQP_FLOW_MASK) <<
  3511. TID_RDMA_DESTQP_FLOW_SHIFT) |
  3512. qpriv->rcd->ctxt);
  3513. ohdr->u.tid_rdma.w_rsp.verbs_qp = cpu_to_be32(qp->remote_qpn);
  3514. *bth1 = remote->qp;
  3515. rcu_read_unlock();
  3516. hdwords = sizeof(ohdr->u.tid_rdma.w_rsp) / sizeof(u32);
  3517. qpriv->pending_tid_w_segs++;
  3518. done:
  3519. return hdwords;
  3520. }
  3521. static void hfi1_add_tid_reap_timer(struct rvt_qp *qp)
  3522. {
  3523. struct hfi1_qp_priv *qpriv = qp->priv;
  3524. lockdep_assert_held(&qp->s_lock);
  3525. if (!(qpriv->s_flags & HFI1_R_TID_RSC_TIMER)) {
  3526. qpriv->s_flags |= HFI1_R_TID_RSC_TIMER;
  3527. qpriv->s_tid_timer.expires = jiffies +
  3528. qpriv->tid_timer_timeout_jiffies;
  3529. add_timer(&qpriv->s_tid_timer);
  3530. }
  3531. }
  3532. static void hfi1_mod_tid_reap_timer(struct rvt_qp *qp)
  3533. {
  3534. struct hfi1_qp_priv *qpriv = qp->priv;
  3535. lockdep_assert_held(&qp->s_lock);
  3536. qpriv->s_flags |= HFI1_R_TID_RSC_TIMER;
  3537. mod_timer(&qpriv->s_tid_timer, jiffies +
  3538. qpriv->tid_timer_timeout_jiffies);
  3539. }
  3540. static int hfi1_stop_tid_reap_timer(struct rvt_qp *qp)
  3541. {
  3542. struct hfi1_qp_priv *qpriv = qp->priv;
  3543. int rval = 0;
  3544. lockdep_assert_held(&qp->s_lock);
  3545. if (qpriv->s_flags & HFI1_R_TID_RSC_TIMER) {
  3546. rval = del_timer(&qpriv->s_tid_timer);
  3547. qpriv->s_flags &= ~HFI1_R_TID_RSC_TIMER;
  3548. }
  3549. return rval;
  3550. }
  3551. void hfi1_del_tid_reap_timer(struct rvt_qp *qp)
  3552. {
  3553. struct hfi1_qp_priv *qpriv = qp->priv;
  3554. del_timer_sync(&qpriv->s_tid_timer);
  3555. qpriv->s_flags &= ~HFI1_R_TID_RSC_TIMER;
  3556. }
  3557. static void hfi1_tid_timeout(struct timer_list *t)
  3558. {
  3559. struct hfi1_qp_priv *qpriv = from_timer(qpriv, t, s_tid_timer);
  3560. struct rvt_qp *qp = qpriv->owner;
  3561. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  3562. unsigned long flags;
  3563. u32 i;
  3564. spin_lock_irqsave(&qp->r_lock, flags);
  3565. spin_lock(&qp->s_lock);
  3566. if (qpriv->s_flags & HFI1_R_TID_RSC_TIMER) {
  3567. dd_dev_warn(dd_from_ibdev(qp->ibqp.device), "[QP%u] %s %d\n",
  3568. qp->ibqp.qp_num, __func__, __LINE__);
  3569. trace_hfi1_msg_tid_timeout(/* msg */
  3570. qp, "resource timeout = ",
  3571. (u64)qpriv->tid_timer_timeout_jiffies);
  3572. hfi1_stop_tid_reap_timer(qp);
  3573. /*
  3574. * Go though the entire ack queue and clear any outstanding
  3575. * HW flow and RcvArray resources.
  3576. */
  3577. hfi1_kern_clear_hw_flow(qpriv->rcd, qp);
  3578. for (i = 0; i < rvt_max_atomic(rdi); i++) {
  3579. struct tid_rdma_request *req =
  3580. ack_to_tid_req(&qp->s_ack_queue[i]);
  3581. hfi1_kern_exp_rcv_clear_all(req);
  3582. }
  3583. spin_unlock(&qp->s_lock);
  3584. if (qp->ibqp.event_handler) {
  3585. struct ib_event ev;
  3586. ev.device = qp->ibqp.device;
  3587. ev.element.qp = &qp->ibqp;
  3588. ev.event = IB_EVENT_QP_FATAL;
  3589. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  3590. }
  3591. rvt_rc_error(qp, IB_WC_RESP_TIMEOUT_ERR);
  3592. goto unlock_r_lock;
  3593. }
  3594. spin_unlock(&qp->s_lock);
  3595. unlock_r_lock:
  3596. spin_unlock_irqrestore(&qp->r_lock, flags);
  3597. }
  3598. void hfi1_rc_rcv_tid_rdma_write_resp(struct hfi1_packet *packet)
  3599. {
  3600. /* HANDLER FOR TID RDMA WRITE RESPONSE packet (Requestor side */
  3601. /*
  3602. * 1. Find matching SWQE
  3603. * 2. Check that TIDENTRY array has enough space for a complete
  3604. * segment. If not, put QP in error state.
  3605. * 3. Save response data in struct tid_rdma_req and struct tid_rdma_flow
  3606. * 4. Remove HFI1_S_WAIT_TID_RESP from s_flags.
  3607. * 5. Set qp->s_state
  3608. * 6. Kick the send engine (hfi1_schedule_send())
  3609. */
  3610. struct ib_other_headers *ohdr = packet->ohdr;
  3611. struct rvt_qp *qp = packet->qp;
  3612. struct hfi1_qp_priv *qpriv = qp->priv;
  3613. struct hfi1_ctxtdata *rcd = packet->rcd;
  3614. struct rvt_swqe *wqe;
  3615. struct tid_rdma_request *req;
  3616. struct tid_rdma_flow *flow;
  3617. enum ib_wc_status status;
  3618. u32 opcode, aeth, psn, flow_psn, i, tidlen = 0, pktlen;
  3619. bool fecn;
  3620. unsigned long flags;
  3621. fecn = process_ecn(qp, packet);
  3622. psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
  3623. aeth = be32_to_cpu(ohdr->u.tid_rdma.w_rsp.aeth);
  3624. opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0xff;
  3625. spin_lock_irqsave(&qp->s_lock, flags);
  3626. /* Ignore invalid responses */
  3627. if (cmp_psn(psn, qp->s_next_psn) >= 0)
  3628. goto ack_done;
  3629. /* Ignore duplicate responses. */
  3630. if (unlikely(cmp_psn(psn, qp->s_last_psn) <= 0))
  3631. goto ack_done;
  3632. if (unlikely(qp->s_acked == qp->s_tail))
  3633. goto ack_done;
  3634. /*
  3635. * If we are waiting for a particular packet sequence number
  3636. * due to a request being resent, check for it. Otherwise,
  3637. * ensure that we haven't missed anything.
  3638. */
  3639. if (qp->r_flags & RVT_R_RDMAR_SEQ) {
  3640. if (cmp_psn(psn, qp->s_last_psn + 1) != 0)
  3641. goto ack_done;
  3642. qp->r_flags &= ~RVT_R_RDMAR_SEQ;
  3643. }
  3644. wqe = rvt_get_swqe_ptr(qp, qpriv->s_tid_cur);
  3645. if (unlikely(wqe->wr.opcode != IB_WR_TID_RDMA_WRITE))
  3646. goto ack_op_err;
  3647. req = wqe_to_tid_req(wqe);
  3648. /*
  3649. * If we've lost ACKs and our acked_tail pointer is too far
  3650. * behind, don't overwrite segments. Just drop the packet and
  3651. * let the reliability protocol take care of it.
  3652. */
  3653. if (!CIRC_SPACE(req->setup_head, req->acked_tail, MAX_FLOWS))
  3654. goto ack_done;
  3655. /*
  3656. * The call to do_rc_ack() should be last in the chain of
  3657. * packet checks because it will end up updating the QP state.
  3658. * Therefore, anything that would prevent the packet from
  3659. * being accepted as a successful response should be prior
  3660. * to it.
  3661. */
  3662. if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
  3663. goto ack_done;
  3664. trace_hfi1_ack(qp, psn);
  3665. flow = &req->flows[req->setup_head];
  3666. flow->pkt = 0;
  3667. flow->tid_idx = 0;
  3668. flow->tid_offset = 0;
  3669. flow->sent = 0;
  3670. flow->resync_npkts = 0;
  3671. flow->tid_qpn = be32_to_cpu(ohdr->u.tid_rdma.w_rsp.tid_flow_qp);
  3672. flow->idx = (flow->tid_qpn >> TID_RDMA_DESTQP_FLOW_SHIFT) &
  3673. TID_RDMA_DESTQP_FLOW_MASK;
  3674. flow_psn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.w_rsp.tid_flow_psn));
  3675. flow->flow_state.generation = flow_psn >> HFI1_KDETH_BTH_SEQ_SHIFT;
  3676. flow->flow_state.spsn = flow_psn & HFI1_KDETH_BTH_SEQ_MASK;
  3677. flow->flow_state.resp_ib_psn = psn;
  3678. flow->length = min_t(u32, req->seg_len,
  3679. (wqe->length - (req->comp_seg * req->seg_len)));
  3680. flow->npkts = rvt_div_round_up_mtu(qp, flow->length);
  3681. flow->flow_state.lpsn = flow->flow_state.spsn +
  3682. flow->npkts - 1;
  3683. /* payload length = packet length - (header length + ICRC length) */
  3684. pktlen = packet->tlen - (packet->hlen + 4);
  3685. if (pktlen > sizeof(flow->tid_entry)) {
  3686. status = IB_WC_LOC_LEN_ERR;
  3687. goto ack_err;
  3688. }
  3689. memcpy(flow->tid_entry, packet->ebuf, pktlen);
  3690. flow->tidcnt = pktlen / sizeof(*flow->tid_entry);
  3691. trace_hfi1_tid_flow_rcv_write_resp(qp, req->setup_head, flow);
  3692. req->comp_seg++;
  3693. trace_hfi1_tid_write_sender_rcv_resp(qp, 0);
  3694. /*
  3695. * Walk the TID_ENTRY list to make sure we have enough space for a
  3696. * complete segment.
  3697. */
  3698. for (i = 0; i < flow->tidcnt; i++) {
  3699. trace_hfi1_tid_entry_rcv_write_resp(/* entry */
  3700. qp, i, flow->tid_entry[i]);
  3701. if (!EXP_TID_GET(flow->tid_entry[i], LEN)) {
  3702. status = IB_WC_LOC_LEN_ERR;
  3703. goto ack_err;
  3704. }
  3705. tidlen += EXP_TID_GET(flow->tid_entry[i], LEN);
  3706. }
  3707. if (tidlen * PAGE_SIZE < flow->length) {
  3708. status = IB_WC_LOC_LEN_ERR;
  3709. goto ack_err;
  3710. }
  3711. trace_hfi1_tid_req_rcv_write_resp(qp, 0, wqe->wr.opcode, wqe->psn,
  3712. wqe->lpsn, req);
  3713. /*
  3714. * If this is the first response for this request, set the initial
  3715. * flow index to the current flow.
  3716. */
  3717. if (!cmp_psn(psn, wqe->psn)) {
  3718. req->r_last_acked = mask_psn(wqe->psn - 1);
  3719. /* Set acked flow index to head index */
  3720. req->acked_tail = req->setup_head;
  3721. }
  3722. /* advance circular buffer head */
  3723. req->setup_head = CIRC_NEXT(req->setup_head, MAX_FLOWS);
  3724. req->state = TID_REQUEST_ACTIVE;
  3725. /*
  3726. * If all responses for this TID RDMA WRITE request have been received
  3727. * advance the pointer to the next one.
  3728. * Since TID RDMA requests could be mixed in with regular IB requests,
  3729. * they might not appear sequentially in the queue. Therefore, the
  3730. * next request needs to be "found".
  3731. */
  3732. if (qpriv->s_tid_cur != qpriv->s_tid_head &&
  3733. req->comp_seg == req->total_segs) {
  3734. for (i = qpriv->s_tid_cur + 1; ; i++) {
  3735. if (i == qp->s_size)
  3736. i = 0;
  3737. wqe = rvt_get_swqe_ptr(qp, i);
  3738. if (i == qpriv->s_tid_head)
  3739. break;
  3740. if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE)
  3741. break;
  3742. }
  3743. qpriv->s_tid_cur = i;
  3744. }
  3745. qp->s_flags &= ~HFI1_S_WAIT_TID_RESP;
  3746. hfi1_schedule_tid_send(qp);
  3747. goto ack_done;
  3748. ack_op_err:
  3749. status = IB_WC_LOC_QP_OP_ERR;
  3750. ack_err:
  3751. rvt_error_qp(qp, status);
  3752. ack_done:
  3753. if (fecn)
  3754. qp->s_flags |= RVT_S_ECN;
  3755. spin_unlock_irqrestore(&qp->s_lock, flags);
  3756. }
  3757. bool hfi1_build_tid_rdma_packet(struct rvt_swqe *wqe,
  3758. struct ib_other_headers *ohdr,
  3759. u32 *bth1, u32 *bth2, u32 *len)
  3760. {
  3761. struct tid_rdma_request *req = wqe_to_tid_req(wqe);
  3762. struct tid_rdma_flow *flow = &req->flows[req->clear_tail];
  3763. struct tid_rdma_params *remote;
  3764. struct rvt_qp *qp = req->qp;
  3765. struct hfi1_qp_priv *qpriv = qp->priv;
  3766. u32 tidentry = flow->tid_entry[flow->tid_idx];
  3767. u32 tidlen = EXP_TID_GET(tidentry, LEN) << PAGE_SHIFT;
  3768. struct tid_rdma_write_data *wd = &ohdr->u.tid_rdma.w_data;
  3769. u32 next_offset, om = KDETH_OM_LARGE;
  3770. bool last_pkt;
  3771. if (!tidlen) {
  3772. hfi1_trdma_send_complete(qp, wqe, IB_WC_REM_INV_RD_REQ_ERR);
  3773. rvt_error_qp(qp, IB_WC_REM_INV_RD_REQ_ERR);
  3774. }
  3775. *len = min_t(u32, qp->pmtu, tidlen - flow->tid_offset);
  3776. flow->sent += *len;
  3777. next_offset = flow->tid_offset + *len;
  3778. last_pkt = (flow->tid_idx == (flow->tidcnt - 1) &&
  3779. next_offset >= tidlen) || (flow->sent >= flow->length);
  3780. trace_hfi1_tid_entry_build_write_data(qp, flow->tid_idx, tidentry);
  3781. trace_hfi1_tid_flow_build_write_data(qp, req->clear_tail, flow);
  3782. rcu_read_lock();
  3783. remote = rcu_dereference(qpriv->tid_rdma.remote);
  3784. KDETH_RESET(wd->kdeth0, KVER, 0x1);
  3785. KDETH_SET(wd->kdeth0, SH, !last_pkt);
  3786. KDETH_SET(wd->kdeth0, INTR, !!(!last_pkt && remote->urg));
  3787. KDETH_SET(wd->kdeth0, TIDCTRL, EXP_TID_GET(tidentry, CTRL));
  3788. KDETH_SET(wd->kdeth0, TID, EXP_TID_GET(tidentry, IDX));
  3789. KDETH_SET(wd->kdeth0, OM, om == KDETH_OM_LARGE);
  3790. KDETH_SET(wd->kdeth0, OFFSET, flow->tid_offset / om);
  3791. KDETH_RESET(wd->kdeth1, JKEY, remote->jkey);
  3792. wd->verbs_qp = cpu_to_be32(qp->remote_qpn);
  3793. rcu_read_unlock();
  3794. *bth1 = flow->tid_qpn;
  3795. *bth2 = mask_psn(((flow->flow_state.spsn + flow->pkt++) &
  3796. HFI1_KDETH_BTH_SEQ_MASK) |
  3797. (flow->flow_state.generation <<
  3798. HFI1_KDETH_BTH_SEQ_SHIFT));
  3799. if (last_pkt) {
  3800. /* PSNs are zero-based, so +1 to count number of packets */
  3801. if (flow->flow_state.lpsn + 1 +
  3802. rvt_div_round_up_mtu(qp, req->seg_len) >
  3803. MAX_TID_FLOW_PSN)
  3804. req->state = TID_REQUEST_SYNC;
  3805. *bth2 |= IB_BTH_REQ_ACK;
  3806. }
  3807. if (next_offset >= tidlen) {
  3808. flow->tid_offset = 0;
  3809. flow->tid_idx++;
  3810. } else {
  3811. flow->tid_offset = next_offset;
  3812. }
  3813. return last_pkt;
  3814. }
  3815. void hfi1_rc_rcv_tid_rdma_write_data(struct hfi1_packet *packet)
  3816. {
  3817. struct rvt_qp *qp = packet->qp;
  3818. struct hfi1_qp_priv *priv = qp->priv;
  3819. struct hfi1_ctxtdata *rcd = priv->rcd;
  3820. struct ib_other_headers *ohdr = packet->ohdr;
  3821. struct rvt_ack_entry *e;
  3822. struct tid_rdma_request *req;
  3823. struct tid_rdma_flow *flow;
  3824. struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
  3825. unsigned long flags;
  3826. u32 psn, next;
  3827. u8 opcode;
  3828. bool fecn;
  3829. fecn = process_ecn(qp, packet);
  3830. psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
  3831. opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0xff;
  3832. /*
  3833. * All error handling should be done by now. If we are here, the packet
  3834. * is either good or been accepted by the error handler.
  3835. */
  3836. spin_lock_irqsave(&qp->s_lock, flags);
  3837. e = &qp->s_ack_queue[priv->r_tid_tail];
  3838. req = ack_to_tid_req(e);
  3839. flow = &req->flows[req->clear_tail];
  3840. if (cmp_psn(psn, full_flow_psn(flow, flow->flow_state.lpsn))) {
  3841. update_r_next_psn_fecn(packet, priv, rcd, flow, fecn);
  3842. if (cmp_psn(psn, flow->flow_state.r_next_psn))
  3843. goto send_nak;
  3844. flow->flow_state.r_next_psn = mask_psn(psn + 1);
  3845. /*
  3846. * Copy the payload to destination buffer if this packet is
  3847. * delivered as an eager packet due to RSM rule and FECN.
  3848. * The RSM rule selects FECN bit in BTH and SH bit in
  3849. * KDETH header and therefore will not match the last
  3850. * packet of each segment that has SH bit cleared.
  3851. */
  3852. if (fecn && packet->etype == RHF_RCV_TYPE_EAGER) {
  3853. struct rvt_sge_state ss;
  3854. u32 len;
  3855. u32 tlen = packet->tlen;
  3856. u16 hdrsize = packet->hlen;
  3857. u8 pad = packet->pad;
  3858. u8 extra_bytes = pad + packet->extra_byte +
  3859. (SIZE_OF_CRC << 2);
  3860. u32 pmtu = qp->pmtu;
  3861. if (unlikely(tlen != (hdrsize + pmtu + extra_bytes)))
  3862. goto send_nak;
  3863. len = req->comp_seg * req->seg_len;
  3864. len += delta_psn(psn,
  3865. full_flow_psn(flow, flow->flow_state.spsn)) *
  3866. pmtu;
  3867. if (unlikely(req->total_len - len < pmtu))
  3868. goto send_nak;
  3869. /*
  3870. * The e->rdma_sge field is set when TID RDMA WRITE REQ
  3871. * is first received and is never modified thereafter.
  3872. */
  3873. ss.sge = e->rdma_sge;
  3874. ss.sg_list = NULL;
  3875. ss.num_sge = 1;
  3876. ss.total_len = req->total_len;
  3877. rvt_skip_sge(&ss, len, false);
  3878. rvt_copy_sge(qp, &ss, packet->payload, pmtu, false,
  3879. false);
  3880. /* Raise the sw sequence check flag for next packet */
  3881. priv->r_next_psn_kdeth = mask_psn(psn + 1);
  3882. priv->s_flags |= HFI1_R_TID_SW_PSN;
  3883. }
  3884. goto exit;
  3885. }
  3886. flow->flow_state.r_next_psn = mask_psn(psn + 1);
  3887. hfi1_kern_exp_rcv_clear(req);
  3888. priv->alloc_w_segs--;
  3889. rcd->flows[flow->idx].psn = psn & HFI1_KDETH_BTH_SEQ_MASK;
  3890. req->comp_seg++;
  3891. priv->s_nak_state = 0;
  3892. /*
  3893. * Release the flow if one of the following conditions has been met:
  3894. * - The request has reached a sync point AND all outstanding
  3895. * segments have been completed, or
  3896. * - The entire request is complete and there are no more requests
  3897. * (of any kind) in the queue.
  3898. */
  3899. trace_hfi1_rsp_rcv_tid_write_data(qp, psn);
  3900. trace_hfi1_tid_req_rcv_write_data(qp, 0, e->opcode, e->psn, e->lpsn,
  3901. req);
  3902. trace_hfi1_tid_write_rsp_rcv_data(qp);
  3903. validate_r_tid_ack(priv);
  3904. if (opcode == TID_OP(WRITE_DATA_LAST)) {
  3905. release_rdma_sge_mr(e);
  3906. for (next = priv->r_tid_tail + 1; ; next++) {
  3907. if (next > rvt_size_atomic(&dev->rdi))
  3908. next = 0;
  3909. if (next == priv->r_tid_head)
  3910. break;
  3911. e = &qp->s_ack_queue[next];
  3912. if (e->opcode == TID_OP(WRITE_REQ))
  3913. break;
  3914. }
  3915. priv->r_tid_tail = next;
  3916. if (++qp->s_acked_ack_queue > rvt_size_atomic(&dev->rdi))
  3917. qp->s_acked_ack_queue = 0;
  3918. }
  3919. hfi1_tid_write_alloc_resources(qp, true);
  3920. /*
  3921. * If we need to generate more responses, schedule the
  3922. * send engine.
  3923. */
  3924. if (req->cur_seg < req->total_segs ||
  3925. qp->s_tail_ack_queue != qp->r_head_ack_queue) {
  3926. qp->s_flags |= RVT_S_RESP_PENDING;
  3927. hfi1_schedule_send(qp);
  3928. }
  3929. priv->pending_tid_w_segs--;
  3930. if (priv->s_flags & HFI1_R_TID_RSC_TIMER) {
  3931. if (priv->pending_tid_w_segs)
  3932. hfi1_mod_tid_reap_timer(req->qp);
  3933. else
  3934. hfi1_stop_tid_reap_timer(req->qp);
  3935. }
  3936. done:
  3937. tid_rdma_schedule_ack(qp);
  3938. exit:
  3939. priv->r_next_psn_kdeth = flow->flow_state.r_next_psn;
  3940. if (fecn)
  3941. qp->s_flags |= RVT_S_ECN;
  3942. spin_unlock_irqrestore(&qp->s_lock, flags);
  3943. return;
  3944. send_nak:
  3945. if (!priv->s_nak_state) {
  3946. priv->s_nak_state = IB_NAK_PSN_ERROR;
  3947. priv->s_nak_psn = flow->flow_state.r_next_psn;
  3948. tid_rdma_trigger_ack(qp);
  3949. }
  3950. goto done;
  3951. }
  3952. static bool hfi1_tid_rdma_is_resync_psn(u32 psn)
  3953. {
  3954. return (bool)((psn & HFI1_KDETH_BTH_SEQ_MASK) ==
  3955. HFI1_KDETH_BTH_SEQ_MASK);
  3956. }
  3957. u32 hfi1_build_tid_rdma_write_ack(struct rvt_qp *qp, struct rvt_ack_entry *e,
  3958. struct ib_other_headers *ohdr, u16 iflow,
  3959. u32 *bth1, u32 *bth2)
  3960. {
  3961. struct hfi1_qp_priv *qpriv = qp->priv;
  3962. struct tid_flow_state *fs = &qpriv->flow_state;
  3963. struct tid_rdma_request *req = ack_to_tid_req(e);
  3964. struct tid_rdma_flow *flow = &req->flows[iflow];
  3965. struct tid_rdma_params *remote;
  3966. rcu_read_lock();
  3967. remote = rcu_dereference(qpriv->tid_rdma.remote);
  3968. KDETH_RESET(ohdr->u.tid_rdma.ack.kdeth1, JKEY, remote->jkey);
  3969. ohdr->u.tid_rdma.ack.verbs_qp = cpu_to_be32(qp->remote_qpn);
  3970. *bth1 = remote->qp;
  3971. rcu_read_unlock();
  3972. if (qpriv->resync) {
  3973. *bth2 = mask_psn((fs->generation <<
  3974. HFI1_KDETH_BTH_SEQ_SHIFT) - 1);
  3975. ohdr->u.tid_rdma.ack.aeth = rvt_compute_aeth(qp);
  3976. } else if (qpriv->s_nak_state) {
  3977. *bth2 = mask_psn(qpriv->s_nak_psn);
  3978. ohdr->u.tid_rdma.ack.aeth =
  3979. cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
  3980. (qpriv->s_nak_state <<
  3981. IB_AETH_CREDIT_SHIFT));
  3982. } else {
  3983. *bth2 = full_flow_psn(flow, flow->flow_state.lpsn);
  3984. ohdr->u.tid_rdma.ack.aeth = rvt_compute_aeth(qp);
  3985. }
  3986. KDETH_RESET(ohdr->u.tid_rdma.ack.kdeth0, KVER, 0x1);
  3987. ohdr->u.tid_rdma.ack.tid_flow_qp =
  3988. cpu_to_be32(qpriv->tid_rdma.local.qp |
  3989. ((flow->idx & TID_RDMA_DESTQP_FLOW_MASK) <<
  3990. TID_RDMA_DESTQP_FLOW_SHIFT) |
  3991. qpriv->rcd->ctxt);
  3992. ohdr->u.tid_rdma.ack.tid_flow_psn = 0;
  3993. ohdr->u.tid_rdma.ack.verbs_psn =
  3994. cpu_to_be32(flow->flow_state.resp_ib_psn);
  3995. if (qpriv->resync) {
  3996. /*
  3997. * If the PSN before the current expect KDETH PSN is the
  3998. * RESYNC PSN, then we never received a good TID RDMA WRITE
  3999. * DATA packet after a previous RESYNC.
  4000. * In this case, the next expected KDETH PSN stays the same.
  4001. */
  4002. if (hfi1_tid_rdma_is_resync_psn(qpriv->r_next_psn_kdeth - 1)) {
  4003. ohdr->u.tid_rdma.ack.tid_flow_psn =
  4004. cpu_to_be32(qpriv->r_next_psn_kdeth_save);
  4005. } else {
  4006. /*
  4007. * Because the KDETH PSNs jump during a RESYNC, it's
  4008. * not possible to infer (or compute) the previous value
  4009. * of r_next_psn_kdeth in the case of back-to-back
  4010. * RESYNC packets. Therefore, we save it.
  4011. */
  4012. qpriv->r_next_psn_kdeth_save =
  4013. qpriv->r_next_psn_kdeth - 1;
  4014. ohdr->u.tid_rdma.ack.tid_flow_psn =
  4015. cpu_to_be32(qpriv->r_next_psn_kdeth_save);
  4016. qpriv->r_next_psn_kdeth = mask_psn(*bth2 + 1);
  4017. }
  4018. qpriv->resync = false;
  4019. }
  4020. return sizeof(ohdr->u.tid_rdma.ack) / sizeof(u32);
  4021. }
  4022. void hfi1_rc_rcv_tid_rdma_ack(struct hfi1_packet *packet)
  4023. {
  4024. struct ib_other_headers *ohdr = packet->ohdr;
  4025. struct rvt_qp *qp = packet->qp;
  4026. struct hfi1_qp_priv *qpriv = qp->priv;
  4027. struct rvt_swqe *wqe;
  4028. struct tid_rdma_request *req;
  4029. struct tid_rdma_flow *flow;
  4030. u32 aeth, psn, req_psn, ack_psn, flpsn, resync_psn, ack_kpsn;
  4031. unsigned long flags;
  4032. u16 fidx;
  4033. trace_hfi1_tid_write_sender_rcv_tid_ack(qp, 0);
  4034. process_ecn(qp, packet);
  4035. psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
  4036. aeth = be32_to_cpu(ohdr->u.tid_rdma.ack.aeth);
  4037. req_psn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.ack.verbs_psn));
  4038. resync_psn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.ack.tid_flow_psn));
  4039. spin_lock_irqsave(&qp->s_lock, flags);
  4040. trace_hfi1_rcv_tid_ack(qp, aeth, psn, req_psn, resync_psn);
  4041. /* If we are waiting for an ACK to RESYNC, drop any other packets */
  4042. if ((qp->s_flags & HFI1_S_WAIT_HALT) &&
  4043. cmp_psn(psn, qpriv->s_resync_psn))
  4044. goto ack_op_err;
  4045. ack_psn = req_psn;
  4046. if (hfi1_tid_rdma_is_resync_psn(psn))
  4047. ack_kpsn = resync_psn;
  4048. else
  4049. ack_kpsn = psn;
  4050. if (aeth >> 29) {
  4051. ack_psn--;
  4052. ack_kpsn--;
  4053. }
  4054. if (unlikely(qp->s_acked == qp->s_tail))
  4055. goto ack_op_err;
  4056. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  4057. if (wqe->wr.opcode != IB_WR_TID_RDMA_WRITE)
  4058. goto ack_op_err;
  4059. req = wqe_to_tid_req(wqe);
  4060. trace_hfi1_tid_req_rcv_tid_ack(qp, 0, wqe->wr.opcode, wqe->psn,
  4061. wqe->lpsn, req);
  4062. flow = &req->flows[req->acked_tail];
  4063. trace_hfi1_tid_flow_rcv_tid_ack(qp, req->acked_tail, flow);
  4064. /* Drop stale ACK/NAK */
  4065. if (cmp_psn(psn, full_flow_psn(flow, flow->flow_state.spsn)) < 0 ||
  4066. cmp_psn(req_psn, flow->flow_state.resp_ib_psn) < 0)
  4067. goto ack_op_err;
  4068. while (cmp_psn(ack_kpsn,
  4069. full_flow_psn(flow, flow->flow_state.lpsn)) >= 0 &&
  4070. req->ack_seg < req->cur_seg) {
  4071. req->ack_seg++;
  4072. /* advance acked segment pointer */
  4073. req->acked_tail = CIRC_NEXT(req->acked_tail, MAX_FLOWS);
  4074. req->r_last_acked = flow->flow_state.resp_ib_psn;
  4075. trace_hfi1_tid_req_rcv_tid_ack(qp, 0, wqe->wr.opcode, wqe->psn,
  4076. wqe->lpsn, req);
  4077. if (req->ack_seg == req->total_segs) {
  4078. req->state = TID_REQUEST_COMPLETE;
  4079. wqe = do_rc_completion(qp, wqe,
  4080. to_iport(qp->ibqp.device,
  4081. qp->port_num));
  4082. trace_hfi1_sender_rcv_tid_ack(qp);
  4083. atomic_dec(&qpriv->n_tid_requests);
  4084. if (qp->s_acked == qp->s_tail)
  4085. break;
  4086. if (wqe->wr.opcode != IB_WR_TID_RDMA_WRITE)
  4087. break;
  4088. req = wqe_to_tid_req(wqe);
  4089. }
  4090. flow = &req->flows[req->acked_tail];
  4091. trace_hfi1_tid_flow_rcv_tid_ack(qp, req->acked_tail, flow);
  4092. }
  4093. trace_hfi1_tid_req_rcv_tid_ack(qp, 0, wqe->wr.opcode, wqe->psn,
  4094. wqe->lpsn, req);
  4095. switch (aeth >> 29) {
  4096. case 0: /* ACK */
  4097. if (qpriv->s_flags & RVT_S_WAIT_ACK)
  4098. qpriv->s_flags &= ~RVT_S_WAIT_ACK;
  4099. if (!hfi1_tid_rdma_is_resync_psn(psn)) {
  4100. /* Check if there is any pending TID ACK */
  4101. if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE &&
  4102. req->ack_seg < req->cur_seg)
  4103. hfi1_mod_tid_retry_timer(qp);
  4104. else
  4105. hfi1_stop_tid_retry_timer(qp);
  4106. hfi1_schedule_send(qp);
  4107. } else {
  4108. u32 spsn, fpsn, last_acked, generation;
  4109. struct tid_rdma_request *rptr;
  4110. /* ACK(RESYNC) */
  4111. hfi1_stop_tid_retry_timer(qp);
  4112. /* Allow new requests (see hfi1_make_tid_rdma_pkt) */
  4113. qp->s_flags &= ~HFI1_S_WAIT_HALT;
  4114. /*
  4115. * Clear RVT_S_SEND_ONE flag in case that the TID RDMA
  4116. * ACK is received after the TID retry timer is fired
  4117. * again. In this case, do not send any more TID
  4118. * RESYNC request or wait for any more TID ACK packet.
  4119. */
  4120. qpriv->s_flags &= ~RVT_S_SEND_ONE;
  4121. hfi1_schedule_send(qp);
  4122. if ((qp->s_acked == qpriv->s_tid_tail &&
  4123. req->ack_seg == req->total_segs) ||
  4124. qp->s_acked == qp->s_tail) {
  4125. qpriv->s_state = TID_OP(WRITE_DATA_LAST);
  4126. goto done;
  4127. }
  4128. if (req->ack_seg == req->comp_seg) {
  4129. qpriv->s_state = TID_OP(WRITE_DATA);
  4130. goto done;
  4131. }
  4132. /*
  4133. * The PSN to start with is the next PSN after the
  4134. * RESYNC PSN.
  4135. */
  4136. psn = mask_psn(psn + 1);
  4137. generation = psn >> HFI1_KDETH_BTH_SEQ_SHIFT;
  4138. spsn = 0;
  4139. /*
  4140. * Update to the correct WQE when we get an ACK(RESYNC)
  4141. * in the middle of a request.
  4142. */
  4143. if (delta_psn(ack_psn, wqe->lpsn))
  4144. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  4145. req = wqe_to_tid_req(wqe);
  4146. flow = &req->flows[req->acked_tail];
  4147. /*
  4148. * RESYNC re-numbers the PSN ranges of all remaining
  4149. * segments. Also, PSN's start from 0 in the middle of a
  4150. * segment and the first segment size is less than the
  4151. * default number of packets. flow->resync_npkts is used
  4152. * to track the number of packets from the start of the
  4153. * real segment to the point of 0 PSN after the RESYNC
  4154. * in order to later correctly rewind the SGE.
  4155. */
  4156. fpsn = full_flow_psn(flow, flow->flow_state.spsn);
  4157. req->r_ack_psn = psn;
  4158. /*
  4159. * If resync_psn points to the last flow PSN for a
  4160. * segment and the new segment (likely from a new
  4161. * request) starts with a new generation number, we
  4162. * need to adjust resync_psn accordingly.
  4163. */
  4164. if (flow->flow_state.generation !=
  4165. (resync_psn >> HFI1_KDETH_BTH_SEQ_SHIFT))
  4166. resync_psn = mask_psn(fpsn - 1);
  4167. flow->resync_npkts +=
  4168. delta_psn(mask_psn(resync_psn + 1), fpsn);
  4169. /*
  4170. * Renumber all packet sequence number ranges
  4171. * based on the new generation.
  4172. */
  4173. last_acked = qp->s_acked;
  4174. rptr = req;
  4175. while (1) {
  4176. /* start from last acked segment */
  4177. for (fidx = rptr->acked_tail;
  4178. CIRC_CNT(rptr->setup_head, fidx,
  4179. MAX_FLOWS);
  4180. fidx = CIRC_NEXT(fidx, MAX_FLOWS)) {
  4181. u32 lpsn;
  4182. u32 gen;
  4183. flow = &rptr->flows[fidx];
  4184. gen = flow->flow_state.generation;
  4185. if (WARN_ON(gen == generation &&
  4186. flow->flow_state.spsn !=
  4187. spsn))
  4188. continue;
  4189. lpsn = flow->flow_state.lpsn;
  4190. lpsn = full_flow_psn(flow, lpsn);
  4191. flow->npkts =
  4192. delta_psn(lpsn,
  4193. mask_psn(resync_psn)
  4194. );
  4195. flow->flow_state.generation =
  4196. generation;
  4197. flow->flow_state.spsn = spsn;
  4198. flow->flow_state.lpsn =
  4199. flow->flow_state.spsn +
  4200. flow->npkts - 1;
  4201. flow->pkt = 0;
  4202. spsn += flow->npkts;
  4203. resync_psn += flow->npkts;
  4204. trace_hfi1_tid_flow_rcv_tid_ack(qp,
  4205. fidx,
  4206. flow);
  4207. }
  4208. if (++last_acked == qpriv->s_tid_cur + 1)
  4209. break;
  4210. if (last_acked == qp->s_size)
  4211. last_acked = 0;
  4212. wqe = rvt_get_swqe_ptr(qp, last_acked);
  4213. rptr = wqe_to_tid_req(wqe);
  4214. }
  4215. req->cur_seg = req->ack_seg;
  4216. qpriv->s_tid_tail = qp->s_acked;
  4217. qpriv->s_state = TID_OP(WRITE_REQ);
  4218. hfi1_schedule_tid_send(qp);
  4219. }
  4220. done:
  4221. qpriv->s_retry = qp->s_retry_cnt;
  4222. break;
  4223. case 3: /* NAK */
  4224. hfi1_stop_tid_retry_timer(qp);
  4225. switch ((aeth >> IB_AETH_CREDIT_SHIFT) &
  4226. IB_AETH_CREDIT_MASK) {
  4227. case 0: /* PSN sequence error */
  4228. if (!req->flows)
  4229. break;
  4230. flow = &req->flows[req->acked_tail];
  4231. flpsn = full_flow_psn(flow, flow->flow_state.lpsn);
  4232. if (cmp_psn(psn, flpsn) > 0)
  4233. break;
  4234. trace_hfi1_tid_flow_rcv_tid_ack(qp, req->acked_tail,
  4235. flow);
  4236. req->r_ack_psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
  4237. req->cur_seg = req->ack_seg;
  4238. qpriv->s_tid_tail = qp->s_acked;
  4239. qpriv->s_state = TID_OP(WRITE_REQ);
  4240. qpriv->s_retry = qp->s_retry_cnt;
  4241. hfi1_schedule_tid_send(qp);
  4242. break;
  4243. default:
  4244. break;
  4245. }
  4246. break;
  4247. default:
  4248. break;
  4249. }
  4250. ack_op_err:
  4251. spin_unlock_irqrestore(&qp->s_lock, flags);
  4252. }
  4253. void hfi1_add_tid_retry_timer(struct rvt_qp *qp)
  4254. {
  4255. struct hfi1_qp_priv *priv = qp->priv;
  4256. struct ib_qp *ibqp = &qp->ibqp;
  4257. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  4258. lockdep_assert_held(&qp->s_lock);
  4259. if (!(priv->s_flags & HFI1_S_TID_RETRY_TIMER)) {
  4260. priv->s_flags |= HFI1_S_TID_RETRY_TIMER;
  4261. priv->s_tid_retry_timer.expires = jiffies +
  4262. priv->tid_retry_timeout_jiffies + rdi->busy_jiffies;
  4263. add_timer(&priv->s_tid_retry_timer);
  4264. }
  4265. }
  4266. static void hfi1_mod_tid_retry_timer(struct rvt_qp *qp)
  4267. {
  4268. struct hfi1_qp_priv *priv = qp->priv;
  4269. struct ib_qp *ibqp = &qp->ibqp;
  4270. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  4271. lockdep_assert_held(&qp->s_lock);
  4272. priv->s_flags |= HFI1_S_TID_RETRY_TIMER;
  4273. mod_timer(&priv->s_tid_retry_timer, jiffies +
  4274. priv->tid_retry_timeout_jiffies + rdi->busy_jiffies);
  4275. }
  4276. static int hfi1_stop_tid_retry_timer(struct rvt_qp *qp)
  4277. {
  4278. struct hfi1_qp_priv *priv = qp->priv;
  4279. int rval = 0;
  4280. lockdep_assert_held(&qp->s_lock);
  4281. if (priv->s_flags & HFI1_S_TID_RETRY_TIMER) {
  4282. rval = del_timer(&priv->s_tid_retry_timer);
  4283. priv->s_flags &= ~HFI1_S_TID_RETRY_TIMER;
  4284. }
  4285. return rval;
  4286. }
  4287. void hfi1_del_tid_retry_timer(struct rvt_qp *qp)
  4288. {
  4289. struct hfi1_qp_priv *priv = qp->priv;
  4290. del_timer_sync(&priv->s_tid_retry_timer);
  4291. priv->s_flags &= ~HFI1_S_TID_RETRY_TIMER;
  4292. }
  4293. static void hfi1_tid_retry_timeout(struct timer_list *t)
  4294. {
  4295. struct hfi1_qp_priv *priv = from_timer(priv, t, s_tid_retry_timer);
  4296. struct rvt_qp *qp = priv->owner;
  4297. struct rvt_swqe *wqe;
  4298. unsigned long flags;
  4299. struct tid_rdma_request *req;
  4300. spin_lock_irqsave(&qp->r_lock, flags);
  4301. spin_lock(&qp->s_lock);
  4302. trace_hfi1_tid_write_sender_retry_timeout(qp, 0);
  4303. if (priv->s_flags & HFI1_S_TID_RETRY_TIMER) {
  4304. hfi1_stop_tid_retry_timer(qp);
  4305. if (!priv->s_retry) {
  4306. trace_hfi1_msg_tid_retry_timeout(/* msg */
  4307. qp,
  4308. "Exhausted retries. Tid retry timeout = ",
  4309. (u64)priv->tid_retry_timeout_jiffies);
  4310. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  4311. hfi1_trdma_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
  4312. rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  4313. } else {
  4314. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  4315. req = wqe_to_tid_req(wqe);
  4316. trace_hfi1_tid_req_tid_retry_timeout(/* req */
  4317. qp, 0, wqe->wr.opcode, wqe->psn, wqe->lpsn, req);
  4318. priv->s_flags &= ~RVT_S_WAIT_ACK;
  4319. /* Only send one packet (the RESYNC) */
  4320. priv->s_flags |= RVT_S_SEND_ONE;
  4321. /*
  4322. * No additional request shall be made by this QP until
  4323. * the RESYNC has been complete.
  4324. */
  4325. qp->s_flags |= HFI1_S_WAIT_HALT;
  4326. priv->s_state = TID_OP(RESYNC);
  4327. priv->s_retry--;
  4328. hfi1_schedule_tid_send(qp);
  4329. }
  4330. }
  4331. spin_unlock(&qp->s_lock);
  4332. spin_unlock_irqrestore(&qp->r_lock, flags);
  4333. }
  4334. u32 hfi1_build_tid_rdma_resync(struct rvt_qp *qp, struct rvt_swqe *wqe,
  4335. struct ib_other_headers *ohdr, u32 *bth1,
  4336. u32 *bth2, u16 fidx)
  4337. {
  4338. struct hfi1_qp_priv *qpriv = qp->priv;
  4339. struct tid_rdma_params *remote;
  4340. struct tid_rdma_request *req = wqe_to_tid_req(wqe);
  4341. struct tid_rdma_flow *flow = &req->flows[fidx];
  4342. u32 generation;
  4343. rcu_read_lock();
  4344. remote = rcu_dereference(qpriv->tid_rdma.remote);
  4345. KDETH_RESET(ohdr->u.tid_rdma.ack.kdeth1, JKEY, remote->jkey);
  4346. ohdr->u.tid_rdma.ack.verbs_qp = cpu_to_be32(qp->remote_qpn);
  4347. *bth1 = remote->qp;
  4348. rcu_read_unlock();
  4349. generation = kern_flow_generation_next(flow->flow_state.generation);
  4350. *bth2 = mask_psn((generation << HFI1_KDETH_BTH_SEQ_SHIFT) - 1);
  4351. qpriv->s_resync_psn = *bth2;
  4352. *bth2 |= IB_BTH_REQ_ACK;
  4353. KDETH_RESET(ohdr->u.tid_rdma.ack.kdeth0, KVER, 0x1);
  4354. return sizeof(ohdr->u.tid_rdma.resync) / sizeof(u32);
  4355. }
  4356. void hfi1_rc_rcv_tid_rdma_resync(struct hfi1_packet *packet)
  4357. {
  4358. struct ib_other_headers *ohdr = packet->ohdr;
  4359. struct rvt_qp *qp = packet->qp;
  4360. struct hfi1_qp_priv *qpriv = qp->priv;
  4361. struct hfi1_ctxtdata *rcd = qpriv->rcd;
  4362. struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
  4363. struct rvt_ack_entry *e;
  4364. struct tid_rdma_request *req;
  4365. struct tid_rdma_flow *flow;
  4366. struct tid_flow_state *fs = &qpriv->flow_state;
  4367. u32 psn, generation, idx, gen_next;
  4368. bool fecn;
  4369. unsigned long flags;
  4370. fecn = process_ecn(qp, packet);
  4371. psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
  4372. generation = mask_psn(psn + 1) >> HFI1_KDETH_BTH_SEQ_SHIFT;
  4373. spin_lock_irqsave(&qp->s_lock, flags);
  4374. gen_next = (fs->generation == KERN_GENERATION_RESERVED) ?
  4375. generation : kern_flow_generation_next(fs->generation);
  4376. /*
  4377. * RESYNC packet contains the "next" generation and can only be
  4378. * from the current or previous generations
  4379. */
  4380. if (generation != mask_generation(gen_next - 1) &&
  4381. generation != gen_next)
  4382. goto bail;
  4383. /* Already processing a resync */
  4384. if (qpriv->resync)
  4385. goto bail;
  4386. spin_lock(&rcd->exp_lock);
  4387. if (fs->index >= RXE_NUM_TID_FLOWS) {
  4388. /*
  4389. * If we don't have a flow, save the generation so it can be
  4390. * applied when a new flow is allocated
  4391. */
  4392. fs->generation = generation;
  4393. } else {
  4394. /* Reprogram the QP flow with new generation */
  4395. rcd->flows[fs->index].generation = generation;
  4396. fs->generation = kern_setup_hw_flow(rcd, fs->index);
  4397. }
  4398. fs->psn = 0;
  4399. /*
  4400. * Disable SW PSN checking since a RESYNC is equivalent to a
  4401. * sync point and the flow has/will be reprogrammed
  4402. */
  4403. qpriv->s_flags &= ~HFI1_R_TID_SW_PSN;
  4404. trace_hfi1_tid_write_rsp_rcv_resync(qp);
  4405. /*
  4406. * Reset all TID flow information with the new generation.
  4407. * This is done for all requests and segments after the
  4408. * last received segment
  4409. */
  4410. for (idx = qpriv->r_tid_tail; ; idx++) {
  4411. u16 flow_idx;
  4412. if (idx > rvt_size_atomic(&dev->rdi))
  4413. idx = 0;
  4414. e = &qp->s_ack_queue[idx];
  4415. if (e->opcode == TID_OP(WRITE_REQ)) {
  4416. req = ack_to_tid_req(e);
  4417. trace_hfi1_tid_req_rcv_resync(qp, 0, e->opcode, e->psn,
  4418. e->lpsn, req);
  4419. /* start from last unacked segment */
  4420. for (flow_idx = req->clear_tail;
  4421. CIRC_CNT(req->setup_head, flow_idx,
  4422. MAX_FLOWS);
  4423. flow_idx = CIRC_NEXT(flow_idx, MAX_FLOWS)) {
  4424. u32 lpsn;
  4425. u32 next;
  4426. flow = &req->flows[flow_idx];
  4427. lpsn = full_flow_psn(flow,
  4428. flow->flow_state.lpsn);
  4429. next = flow->flow_state.r_next_psn;
  4430. flow->npkts = delta_psn(lpsn, next - 1);
  4431. flow->flow_state.generation = fs->generation;
  4432. flow->flow_state.spsn = fs->psn;
  4433. flow->flow_state.lpsn =
  4434. flow->flow_state.spsn + flow->npkts - 1;
  4435. flow->flow_state.r_next_psn =
  4436. full_flow_psn(flow,
  4437. flow->flow_state.spsn);
  4438. fs->psn += flow->npkts;
  4439. trace_hfi1_tid_flow_rcv_resync(qp, flow_idx,
  4440. flow);
  4441. }
  4442. }
  4443. if (idx == qp->s_tail_ack_queue)
  4444. break;
  4445. }
  4446. spin_unlock(&rcd->exp_lock);
  4447. qpriv->resync = true;
  4448. /* RESYNC request always gets a TID RDMA ACK. */
  4449. qpriv->s_nak_state = 0;
  4450. tid_rdma_trigger_ack(qp);
  4451. bail:
  4452. if (fecn)
  4453. qp->s_flags |= RVT_S_ECN;
  4454. spin_unlock_irqrestore(&qp->s_lock, flags);
  4455. }
  4456. /*
  4457. * Call this function when the last TID RDMA WRITE DATA packet for a request
  4458. * is built.
  4459. */
  4460. static void update_tid_tail(struct rvt_qp *qp)
  4461. __must_hold(&qp->s_lock)
  4462. {
  4463. struct hfi1_qp_priv *priv = qp->priv;
  4464. u32 i;
  4465. struct rvt_swqe *wqe;
  4466. lockdep_assert_held(&qp->s_lock);
  4467. /* Can't move beyond s_tid_cur */
  4468. if (priv->s_tid_tail == priv->s_tid_cur)
  4469. return;
  4470. for (i = priv->s_tid_tail + 1; ; i++) {
  4471. if (i == qp->s_size)
  4472. i = 0;
  4473. if (i == priv->s_tid_cur)
  4474. break;
  4475. wqe = rvt_get_swqe_ptr(qp, i);
  4476. if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE)
  4477. break;
  4478. }
  4479. priv->s_tid_tail = i;
  4480. priv->s_state = TID_OP(WRITE_RESP);
  4481. }
  4482. int hfi1_make_tid_rdma_pkt(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
  4483. __must_hold(&qp->s_lock)
  4484. {
  4485. struct hfi1_qp_priv *priv = qp->priv;
  4486. struct rvt_swqe *wqe;
  4487. u32 bth1 = 0, bth2 = 0, hwords = 5, len, middle = 0;
  4488. struct ib_other_headers *ohdr;
  4489. struct rvt_sge_state *ss = &qp->s_sge;
  4490. struct rvt_ack_entry *e = &qp->s_ack_queue[qp->s_tail_ack_queue];
  4491. struct tid_rdma_request *req = ack_to_tid_req(e);
  4492. bool last = false;
  4493. u8 opcode = TID_OP(WRITE_DATA);
  4494. lockdep_assert_held(&qp->s_lock);
  4495. trace_hfi1_tid_write_sender_make_tid_pkt(qp, 0);
  4496. /*
  4497. * Prioritize the sending of the requests and responses over the
  4498. * sending of the TID RDMA data packets.
  4499. */
  4500. if (((atomic_read(&priv->n_tid_requests) < HFI1_TID_RDMA_WRITE_CNT) &&
  4501. atomic_read(&priv->n_requests) &&
  4502. !(qp->s_flags & (RVT_S_BUSY | RVT_S_WAIT_ACK |
  4503. HFI1_S_ANY_WAIT_IO))) ||
  4504. (e->opcode == TID_OP(WRITE_REQ) && req->cur_seg < req->alloc_seg &&
  4505. !(qp->s_flags & (RVT_S_BUSY | HFI1_S_ANY_WAIT_IO)))) {
  4506. struct iowait_work *iowork;
  4507. iowork = iowait_get_ib_work(&priv->s_iowait);
  4508. ps->s_txreq = get_waiting_verbs_txreq(iowork);
  4509. if (ps->s_txreq || hfi1_make_rc_req(qp, ps)) {
  4510. priv->s_flags |= HFI1_S_TID_BUSY_SET;
  4511. return 1;
  4512. }
  4513. }
  4514. ps->s_txreq = get_txreq(ps->dev, qp);
  4515. if (!ps->s_txreq)
  4516. goto bail_no_tx;
  4517. ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
  4518. if ((priv->s_flags & RVT_S_ACK_PENDING) &&
  4519. make_tid_rdma_ack(qp, ohdr, ps))
  4520. return 1;
  4521. /*
  4522. * Bail out if we can't send data.
  4523. * Be reminded that this check must been done after the call to
  4524. * make_tid_rdma_ack() because the responding QP could be in
  4525. * RTR state where it can send TID RDMA ACK, not TID RDMA WRITE DATA.
  4526. */
  4527. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK))
  4528. goto bail;
  4529. if (priv->s_flags & RVT_S_WAIT_ACK)
  4530. goto bail;
  4531. /* Check whether there is anything to do. */
  4532. if (priv->s_tid_tail == HFI1_QP_WQE_INVALID)
  4533. goto bail;
  4534. wqe = rvt_get_swqe_ptr(qp, priv->s_tid_tail);
  4535. req = wqe_to_tid_req(wqe);
  4536. trace_hfi1_tid_req_make_tid_pkt(qp, 0, wqe->wr.opcode, wqe->psn,
  4537. wqe->lpsn, req);
  4538. switch (priv->s_state) {
  4539. case TID_OP(WRITE_REQ):
  4540. case TID_OP(WRITE_RESP):
  4541. priv->tid_ss.sge = wqe->sg_list[0];
  4542. priv->tid_ss.sg_list = wqe->sg_list + 1;
  4543. priv->tid_ss.num_sge = wqe->wr.num_sge;
  4544. priv->tid_ss.total_len = wqe->length;
  4545. if (priv->s_state == TID_OP(WRITE_REQ))
  4546. hfi1_tid_rdma_restart_req(qp, wqe, &bth2);
  4547. priv->s_state = TID_OP(WRITE_DATA);
  4548. fallthrough;
  4549. case TID_OP(WRITE_DATA):
  4550. /*
  4551. * 1. Check whether TID RDMA WRITE RESP available.
  4552. * 2. If no:
  4553. * 2.1 If have more segments and no TID RDMA WRITE RESP,
  4554. * set HFI1_S_WAIT_TID_RESP
  4555. * 2.2 Return indicating no progress made.
  4556. * 3. If yes:
  4557. * 3.1 Build TID RDMA WRITE DATA packet.
  4558. * 3.2 If last packet in segment:
  4559. * 3.2.1 Change KDETH header bits
  4560. * 3.2.2 Advance RESP pointers.
  4561. * 3.3 Return indicating progress made.
  4562. */
  4563. trace_hfi1_sender_make_tid_pkt(qp);
  4564. trace_hfi1_tid_write_sender_make_tid_pkt(qp, 0);
  4565. wqe = rvt_get_swqe_ptr(qp, priv->s_tid_tail);
  4566. req = wqe_to_tid_req(wqe);
  4567. len = wqe->length;
  4568. if (!req->comp_seg || req->cur_seg == req->comp_seg)
  4569. goto bail;
  4570. trace_hfi1_tid_req_make_tid_pkt(qp, 0, wqe->wr.opcode,
  4571. wqe->psn, wqe->lpsn, req);
  4572. last = hfi1_build_tid_rdma_packet(wqe, ohdr, &bth1, &bth2,
  4573. &len);
  4574. if (last) {
  4575. /* move pointer to next flow */
  4576. req->clear_tail = CIRC_NEXT(req->clear_tail,
  4577. MAX_FLOWS);
  4578. if (++req->cur_seg < req->total_segs) {
  4579. if (!CIRC_CNT(req->setup_head, req->clear_tail,
  4580. MAX_FLOWS))
  4581. qp->s_flags |= HFI1_S_WAIT_TID_RESP;
  4582. } else {
  4583. priv->s_state = TID_OP(WRITE_DATA_LAST);
  4584. opcode = TID_OP(WRITE_DATA_LAST);
  4585. /* Advance the s_tid_tail now */
  4586. update_tid_tail(qp);
  4587. }
  4588. }
  4589. hwords += sizeof(ohdr->u.tid_rdma.w_data) / sizeof(u32);
  4590. ss = &priv->tid_ss;
  4591. break;
  4592. case TID_OP(RESYNC):
  4593. trace_hfi1_sender_make_tid_pkt(qp);
  4594. /* Use generation from the most recently received response */
  4595. wqe = rvt_get_swqe_ptr(qp, priv->s_tid_cur);
  4596. req = wqe_to_tid_req(wqe);
  4597. /* If no responses for this WQE look at the previous one */
  4598. if (!req->comp_seg) {
  4599. wqe = rvt_get_swqe_ptr(qp,
  4600. (!priv->s_tid_cur ? qp->s_size :
  4601. priv->s_tid_cur) - 1);
  4602. req = wqe_to_tid_req(wqe);
  4603. }
  4604. hwords += hfi1_build_tid_rdma_resync(qp, wqe, ohdr, &bth1,
  4605. &bth2,
  4606. CIRC_PREV(req->setup_head,
  4607. MAX_FLOWS));
  4608. ss = NULL;
  4609. len = 0;
  4610. opcode = TID_OP(RESYNC);
  4611. break;
  4612. default:
  4613. goto bail;
  4614. }
  4615. if (priv->s_flags & RVT_S_SEND_ONE) {
  4616. priv->s_flags &= ~RVT_S_SEND_ONE;
  4617. priv->s_flags |= RVT_S_WAIT_ACK;
  4618. bth2 |= IB_BTH_REQ_ACK;
  4619. }
  4620. qp->s_len -= len;
  4621. ps->s_txreq->hdr_dwords = hwords;
  4622. ps->s_txreq->sde = priv->s_sde;
  4623. ps->s_txreq->ss = ss;
  4624. ps->s_txreq->s_cur_size = len;
  4625. hfi1_make_ruc_header(qp, ohdr, (opcode << 24), bth1, bth2,
  4626. middle, ps);
  4627. return 1;
  4628. bail:
  4629. hfi1_put_txreq(ps->s_txreq);
  4630. bail_no_tx:
  4631. ps->s_txreq = NULL;
  4632. priv->s_flags &= ~RVT_S_BUSY;
  4633. /*
  4634. * If we didn't get a txreq, the QP will be woken up later to try
  4635. * again, set the flags to the wake up which work item to wake
  4636. * up.
  4637. * (A better algorithm should be found to do this and generalize the
  4638. * sleep/wakeup flags.)
  4639. */
  4640. iowait_set_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
  4641. return 0;
  4642. }
  4643. static int make_tid_rdma_ack(struct rvt_qp *qp,
  4644. struct ib_other_headers *ohdr,
  4645. struct hfi1_pkt_state *ps)
  4646. {
  4647. struct rvt_ack_entry *e;
  4648. struct hfi1_qp_priv *qpriv = qp->priv;
  4649. struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
  4650. u32 hwords, next;
  4651. u32 len = 0;
  4652. u32 bth1 = 0, bth2 = 0;
  4653. int middle = 0;
  4654. u16 flow;
  4655. struct tid_rdma_request *req, *nreq;
  4656. trace_hfi1_tid_write_rsp_make_tid_ack(qp);
  4657. /* Don't send an ACK if we aren't supposed to. */
  4658. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
  4659. goto bail;
  4660. /* header size in 32-bit words LRH+BTH = (8+12)/4. */
  4661. hwords = 5;
  4662. e = &qp->s_ack_queue[qpriv->r_tid_ack];
  4663. req = ack_to_tid_req(e);
  4664. /*
  4665. * In the RESYNC case, we are exactly one segment past the
  4666. * previously sent ack or at the previously sent NAK. So to send
  4667. * the resync ack, we go back one segment (which might be part of
  4668. * the previous request) and let the do-while loop execute again.
  4669. * The advantage of executing the do-while loop is that any data
  4670. * received after the previous ack is automatically acked in the
  4671. * RESYNC ack. It turns out that for the do-while loop we only need
  4672. * to pull back qpriv->r_tid_ack, not the segment
  4673. * indices/counters. The scheme works even if the previous request
  4674. * was not a TID WRITE request.
  4675. */
  4676. if (qpriv->resync) {
  4677. if (!req->ack_seg || req->ack_seg == req->total_segs)
  4678. qpriv->r_tid_ack = !qpriv->r_tid_ack ?
  4679. rvt_size_atomic(&dev->rdi) :
  4680. qpriv->r_tid_ack - 1;
  4681. e = &qp->s_ack_queue[qpriv->r_tid_ack];
  4682. req = ack_to_tid_req(e);
  4683. }
  4684. trace_hfi1_rsp_make_tid_ack(qp, e->psn);
  4685. trace_hfi1_tid_req_make_tid_ack(qp, 0, e->opcode, e->psn, e->lpsn,
  4686. req);
  4687. /*
  4688. * If we've sent all the ACKs that we can, we are done
  4689. * until we get more segments...
  4690. */
  4691. if (!qpriv->s_nak_state && !qpriv->resync &&
  4692. req->ack_seg == req->comp_seg)
  4693. goto bail;
  4694. do {
  4695. /*
  4696. * To deal with coalesced ACKs, the acked_tail pointer
  4697. * into the flow array is used. The distance between it
  4698. * and the clear_tail is the number of flows that are
  4699. * being ACK'ed.
  4700. */
  4701. req->ack_seg +=
  4702. /* Get up-to-date value */
  4703. CIRC_CNT(req->clear_tail, req->acked_tail,
  4704. MAX_FLOWS);
  4705. /* Advance acked index */
  4706. req->acked_tail = req->clear_tail;
  4707. /*
  4708. * req->clear_tail points to the segment currently being
  4709. * received. So, when sending an ACK, the previous
  4710. * segment is being ACK'ed.
  4711. */
  4712. flow = CIRC_PREV(req->acked_tail, MAX_FLOWS);
  4713. if (req->ack_seg != req->total_segs)
  4714. break;
  4715. req->state = TID_REQUEST_COMPLETE;
  4716. next = qpriv->r_tid_ack + 1;
  4717. if (next > rvt_size_atomic(&dev->rdi))
  4718. next = 0;
  4719. qpriv->r_tid_ack = next;
  4720. if (qp->s_ack_queue[next].opcode != TID_OP(WRITE_REQ))
  4721. break;
  4722. nreq = ack_to_tid_req(&qp->s_ack_queue[next]);
  4723. if (!nreq->comp_seg || nreq->ack_seg == nreq->comp_seg)
  4724. break;
  4725. /* Move to the next ack entry now */
  4726. e = &qp->s_ack_queue[qpriv->r_tid_ack];
  4727. req = ack_to_tid_req(e);
  4728. } while (1);
  4729. /*
  4730. * At this point qpriv->r_tid_ack == qpriv->r_tid_tail but e and
  4731. * req could be pointing at the previous ack queue entry
  4732. */
  4733. if (qpriv->s_nak_state ||
  4734. (qpriv->resync &&
  4735. !hfi1_tid_rdma_is_resync_psn(qpriv->r_next_psn_kdeth - 1) &&
  4736. (cmp_psn(qpriv->r_next_psn_kdeth - 1,
  4737. full_flow_psn(&req->flows[flow],
  4738. req->flows[flow].flow_state.lpsn)) > 0))) {
  4739. /*
  4740. * A NAK will implicitly acknowledge all previous TID RDMA
  4741. * requests. Therefore, we NAK with the req->acked_tail
  4742. * segment for the request at qpriv->r_tid_ack (same at
  4743. * this point as the req->clear_tail segment for the
  4744. * qpriv->r_tid_tail request)
  4745. */
  4746. e = &qp->s_ack_queue[qpriv->r_tid_ack];
  4747. req = ack_to_tid_req(e);
  4748. flow = req->acked_tail;
  4749. } else if (req->ack_seg == req->total_segs &&
  4750. qpriv->s_flags & HFI1_R_TID_WAIT_INTERLCK)
  4751. qpriv->s_flags &= ~HFI1_R_TID_WAIT_INTERLCK;
  4752. trace_hfi1_tid_write_rsp_make_tid_ack(qp);
  4753. trace_hfi1_tid_req_make_tid_ack(qp, 0, e->opcode, e->psn, e->lpsn,
  4754. req);
  4755. hwords += hfi1_build_tid_rdma_write_ack(qp, e, ohdr, flow, &bth1,
  4756. &bth2);
  4757. len = 0;
  4758. qpriv->s_flags &= ~RVT_S_ACK_PENDING;
  4759. ps->s_txreq->hdr_dwords = hwords;
  4760. ps->s_txreq->sde = qpriv->s_sde;
  4761. ps->s_txreq->s_cur_size = len;
  4762. ps->s_txreq->ss = NULL;
  4763. hfi1_make_ruc_header(qp, ohdr, (TID_OP(ACK) << 24), bth1, bth2, middle,
  4764. ps);
  4765. ps->s_txreq->txreq.flags |= SDMA_TXREQ_F_VIP;
  4766. return 1;
  4767. bail:
  4768. /*
  4769. * Ensure s_rdma_ack_cnt changes are committed prior to resetting
  4770. * RVT_S_RESP_PENDING
  4771. */
  4772. smp_wmb();
  4773. qpriv->s_flags &= ~RVT_S_ACK_PENDING;
  4774. return 0;
  4775. }
  4776. static int hfi1_send_tid_ok(struct rvt_qp *qp)
  4777. {
  4778. struct hfi1_qp_priv *priv = qp->priv;
  4779. return !(priv->s_flags & RVT_S_BUSY ||
  4780. qp->s_flags & HFI1_S_ANY_WAIT_IO) &&
  4781. (verbs_txreq_queued(iowait_get_tid_work(&priv->s_iowait)) ||
  4782. (priv->s_flags & RVT_S_RESP_PENDING) ||
  4783. !(qp->s_flags & HFI1_S_ANY_TID_WAIT_SEND));
  4784. }
  4785. void _hfi1_do_tid_send(struct work_struct *work)
  4786. {
  4787. struct iowait_work *w = container_of(work, struct iowait_work, iowork);
  4788. struct rvt_qp *qp = iowait_to_qp(w->iow);
  4789. hfi1_do_tid_send(qp);
  4790. }
  4791. static void hfi1_do_tid_send(struct rvt_qp *qp)
  4792. {
  4793. struct hfi1_pkt_state ps;
  4794. struct hfi1_qp_priv *priv = qp->priv;
  4795. ps.dev = to_idev(qp->ibqp.device);
  4796. ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
  4797. ps.ppd = ppd_from_ibp(ps.ibp);
  4798. ps.wait = iowait_get_tid_work(&priv->s_iowait);
  4799. ps.in_thread = false;
  4800. ps.timeout_int = qp->timeout_jiffies / 8;
  4801. trace_hfi1_rc_do_tid_send(qp, false);
  4802. spin_lock_irqsave(&qp->s_lock, ps.flags);
  4803. /* Return if we are already busy processing a work request. */
  4804. if (!hfi1_send_tid_ok(qp)) {
  4805. if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
  4806. iowait_set_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
  4807. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  4808. return;
  4809. }
  4810. priv->s_flags |= RVT_S_BUSY;
  4811. ps.timeout = jiffies + ps.timeout_int;
  4812. ps.cpu = priv->s_sde ? priv->s_sde->cpu :
  4813. cpumask_first(cpumask_of_node(ps.ppd->dd->node));
  4814. ps.pkts_sent = false;
  4815. /* insure a pre-built packet is handled */
  4816. ps.s_txreq = get_waiting_verbs_txreq(ps.wait);
  4817. do {
  4818. /* Check for a constructed packet to be sent. */
  4819. if (ps.s_txreq) {
  4820. if (priv->s_flags & HFI1_S_TID_BUSY_SET) {
  4821. qp->s_flags |= RVT_S_BUSY;
  4822. ps.wait = iowait_get_ib_work(&priv->s_iowait);
  4823. }
  4824. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  4825. /*
  4826. * If the packet cannot be sent now, return and
  4827. * the send tasklet will be woken up later.
  4828. */
  4829. if (hfi1_verbs_send(qp, &ps))
  4830. return;
  4831. /* allow other tasks to run */
  4832. if (hfi1_schedule_send_yield(qp, &ps, true))
  4833. return;
  4834. spin_lock_irqsave(&qp->s_lock, ps.flags);
  4835. if (priv->s_flags & HFI1_S_TID_BUSY_SET) {
  4836. qp->s_flags &= ~RVT_S_BUSY;
  4837. priv->s_flags &= ~HFI1_S_TID_BUSY_SET;
  4838. ps.wait = iowait_get_tid_work(&priv->s_iowait);
  4839. if (iowait_flag_set(&priv->s_iowait,
  4840. IOWAIT_PENDING_IB))
  4841. hfi1_schedule_send(qp);
  4842. }
  4843. }
  4844. } while (hfi1_make_tid_rdma_pkt(qp, &ps));
  4845. iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
  4846. spin_unlock_irqrestore(&qp->s_lock, ps.flags);
  4847. }
  4848. static bool _hfi1_schedule_tid_send(struct rvt_qp *qp)
  4849. {
  4850. struct hfi1_qp_priv *priv = qp->priv;
  4851. struct hfi1_ibport *ibp =
  4852. to_iport(qp->ibqp.device, qp->port_num);
  4853. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  4854. struct hfi1_devdata *dd = ppd->dd;
  4855. if ((dd->flags & HFI1_SHUTDOWN))
  4856. return true;
  4857. return iowait_tid_schedule(&priv->s_iowait, ppd->hfi1_wq,
  4858. priv->s_sde ?
  4859. priv->s_sde->cpu :
  4860. cpumask_first(cpumask_of_node(dd->node)));
  4861. }
  4862. /**
  4863. * hfi1_schedule_tid_send - schedule progress on TID RDMA state machine
  4864. * @qp: the QP
  4865. *
  4866. * This schedules qp progress on the TID RDMA state machine. Caller
  4867. * should hold the s_lock.
  4868. * Unlike hfi1_schedule_send(), this cannot use hfi1_send_ok() because
  4869. * the two state machines can step on each other with respect to the
  4870. * RVT_S_BUSY flag.
  4871. * Therefore, a modified test is used.
  4872. * @return true if the second leg is scheduled;
  4873. * false if the second leg is not scheduled.
  4874. */
  4875. bool hfi1_schedule_tid_send(struct rvt_qp *qp)
  4876. {
  4877. lockdep_assert_held(&qp->s_lock);
  4878. if (hfi1_send_tid_ok(qp)) {
  4879. /*
  4880. * The following call returns true if the qp is not on the
  4881. * queue and false if the qp is already on the queue before
  4882. * this call. Either way, the qp will be on the queue when the
  4883. * call returns.
  4884. */
  4885. _hfi1_schedule_tid_send(qp);
  4886. return true;
  4887. }
  4888. if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
  4889. iowait_set_flag(&((struct hfi1_qp_priv *)qp->priv)->s_iowait,
  4890. IOWAIT_PENDING_TID);
  4891. return false;
  4892. }
  4893. bool hfi1_tid_rdma_ack_interlock(struct rvt_qp *qp, struct rvt_ack_entry *e)
  4894. {
  4895. struct rvt_ack_entry *prev;
  4896. struct tid_rdma_request *req;
  4897. struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
  4898. struct hfi1_qp_priv *priv = qp->priv;
  4899. u32 s_prev;
  4900. s_prev = qp->s_tail_ack_queue == 0 ? rvt_size_atomic(&dev->rdi) :
  4901. (qp->s_tail_ack_queue - 1);
  4902. prev = &qp->s_ack_queue[s_prev];
  4903. if ((e->opcode == TID_OP(READ_REQ) ||
  4904. e->opcode == OP(RDMA_READ_REQUEST)) &&
  4905. prev->opcode == TID_OP(WRITE_REQ)) {
  4906. req = ack_to_tid_req(prev);
  4907. if (req->ack_seg != req->total_segs) {
  4908. priv->s_flags |= HFI1_R_TID_WAIT_INTERLCK;
  4909. return true;
  4910. }
  4911. }
  4912. return false;
  4913. }
  4914. static u32 read_r_next_psn(struct hfi1_devdata *dd, u8 ctxt, u8 fidx)
  4915. {
  4916. u64 reg;
  4917. /*
  4918. * The only sane way to get the amount of
  4919. * progress is to read the HW flow state.
  4920. */
  4921. reg = read_uctxt_csr(dd, ctxt, RCV_TID_FLOW_TABLE + (8 * fidx));
  4922. return mask_psn(reg);
  4923. }
  4924. static void tid_rdma_rcv_err(struct hfi1_packet *packet,
  4925. struct ib_other_headers *ohdr,
  4926. struct rvt_qp *qp, u32 psn, int diff, bool fecn)
  4927. {
  4928. unsigned long flags;
  4929. tid_rdma_rcv_error(packet, ohdr, qp, psn, diff);
  4930. if (fecn) {
  4931. spin_lock_irqsave(&qp->s_lock, flags);
  4932. qp->s_flags |= RVT_S_ECN;
  4933. spin_unlock_irqrestore(&qp->s_lock, flags);
  4934. }
  4935. }
  4936. static void update_r_next_psn_fecn(struct hfi1_packet *packet,
  4937. struct hfi1_qp_priv *priv,
  4938. struct hfi1_ctxtdata *rcd,
  4939. struct tid_rdma_flow *flow,
  4940. bool fecn)
  4941. {
  4942. /*
  4943. * If a start/middle packet is delivered here due to
  4944. * RSM rule and FECN, we need to update the r_next_psn.
  4945. */
  4946. if (fecn && packet->etype == RHF_RCV_TYPE_EAGER &&
  4947. !(priv->s_flags & HFI1_R_TID_SW_PSN)) {
  4948. struct hfi1_devdata *dd = rcd->dd;
  4949. flow->flow_state.r_next_psn =
  4950. read_r_next_psn(dd, rcd->ctxt, flow->idx);
  4951. }
  4952. }