mad.c 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949
  1. /*
  2. * Copyright(c) 2015-2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/net.h>
  48. #include <rdma/opa_addr.h>
  49. #define OPA_NUM_PKEY_BLOCKS_PER_SMP (OPA_SMP_DR_DATA_SIZE \
  50. / (OPA_PARTITION_TABLE_BLK_SIZE * sizeof(u16)))
  51. #include "hfi.h"
  52. #include "mad.h"
  53. #include "trace.h"
  54. #include "qp.h"
  55. #include "vnic.h"
  56. /* the reset value from the FM is supposed to be 0xffff, handle both */
  57. #define OPA_LINK_WIDTH_RESET_OLD 0x0fff
  58. #define OPA_LINK_WIDTH_RESET 0xffff
  59. struct trap_node {
  60. struct list_head list;
  61. struct opa_mad_notice_attr data;
  62. __be64 tid;
  63. int len;
  64. u32 retry;
  65. u8 in_use;
  66. u8 repress;
  67. };
  68. static int smp_length_check(u32 data_size, u32 request_len)
  69. {
  70. if (unlikely(request_len < data_size))
  71. return -EINVAL;
  72. return 0;
  73. }
  74. static int reply(struct ib_mad_hdr *smp)
  75. {
  76. /*
  77. * The verbs framework will handle the directed/LID route
  78. * packet changes.
  79. */
  80. smp->method = IB_MGMT_METHOD_GET_RESP;
  81. if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  82. smp->status |= IB_SMP_DIRECTION;
  83. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  84. }
  85. static inline void clear_opa_smp_data(struct opa_smp *smp)
  86. {
  87. void *data = opa_get_smp_data(smp);
  88. size_t size = opa_get_smp_data_size(smp);
  89. memset(data, 0, size);
  90. }
  91. static u16 hfi1_lookup_pkey_value(struct hfi1_ibport *ibp, int pkey_idx)
  92. {
  93. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  94. if (pkey_idx < ARRAY_SIZE(ppd->pkeys))
  95. return ppd->pkeys[pkey_idx];
  96. return 0;
  97. }
  98. void hfi1_event_pkey_change(struct hfi1_devdata *dd, u8 port)
  99. {
  100. struct ib_event event;
  101. event.event = IB_EVENT_PKEY_CHANGE;
  102. event.device = &dd->verbs_dev.rdi.ibdev;
  103. event.element.port_num = port;
  104. ib_dispatch_event(&event);
  105. }
  106. /*
  107. * If the port is down, clean up all pending traps. We need to be careful
  108. * with the given trap, because it may be queued.
  109. */
  110. static void cleanup_traps(struct hfi1_ibport *ibp, struct trap_node *trap)
  111. {
  112. struct trap_node *node, *q;
  113. unsigned long flags;
  114. struct list_head trap_list;
  115. int i;
  116. for (i = 0; i < RVT_MAX_TRAP_LISTS; i++) {
  117. spin_lock_irqsave(&ibp->rvp.lock, flags);
  118. list_replace_init(&ibp->rvp.trap_lists[i].list, &trap_list);
  119. ibp->rvp.trap_lists[i].list_len = 0;
  120. spin_unlock_irqrestore(&ibp->rvp.lock, flags);
  121. /*
  122. * Remove all items from the list, freeing all the non-given
  123. * traps.
  124. */
  125. list_for_each_entry_safe(node, q, &trap_list, list) {
  126. list_del(&node->list);
  127. if (node != trap)
  128. kfree(node);
  129. }
  130. }
  131. /*
  132. * If this wasn't on one of the lists it would not be freed. If it
  133. * was on the list, it is now safe to free.
  134. */
  135. kfree(trap);
  136. }
  137. static struct trap_node *check_and_add_trap(struct hfi1_ibport *ibp,
  138. struct trap_node *trap)
  139. {
  140. struct trap_node *node;
  141. struct trap_list *trap_list;
  142. unsigned long flags;
  143. unsigned long timeout;
  144. int found = 0;
  145. unsigned int queue_id;
  146. static int trap_count;
  147. queue_id = trap->data.generic_type & 0x0F;
  148. if (queue_id >= RVT_MAX_TRAP_LISTS) {
  149. trap_count++;
  150. pr_err_ratelimited("hfi1: Invalid trap 0x%0x dropped. Total dropped: %d\n",
  151. trap->data.generic_type, trap_count);
  152. kfree(trap);
  153. return NULL;
  154. }
  155. /*
  156. * Since the retry (handle timeout) does not remove a trap request
  157. * from the list, all we have to do is compare the node.
  158. */
  159. spin_lock_irqsave(&ibp->rvp.lock, flags);
  160. trap_list = &ibp->rvp.trap_lists[queue_id];
  161. list_for_each_entry(node, &trap_list->list, list) {
  162. if (node == trap) {
  163. node->retry++;
  164. found = 1;
  165. break;
  166. }
  167. }
  168. /* If it is not on the list, add it, limited to RVT-MAX_TRAP_LEN. */
  169. if (!found) {
  170. if (trap_list->list_len < RVT_MAX_TRAP_LEN) {
  171. trap_list->list_len++;
  172. list_add_tail(&trap->list, &trap_list->list);
  173. } else {
  174. pr_warn_ratelimited("hfi1: Maximum trap limit reached for 0x%0x traps\n",
  175. trap->data.generic_type);
  176. kfree(trap);
  177. }
  178. }
  179. /*
  180. * Next check to see if there is a timer pending. If not, set it up
  181. * and get the first trap from the list.
  182. */
  183. node = NULL;
  184. if (!timer_pending(&ibp->rvp.trap_timer)) {
  185. /*
  186. * o14-2
  187. * If the time out is set we have to wait until it expires
  188. * before the trap can be sent.
  189. * This should be > RVT_TRAP_TIMEOUT
  190. */
  191. timeout = (RVT_TRAP_TIMEOUT *
  192. (1UL << ibp->rvp.subnet_timeout)) / 1000;
  193. mod_timer(&ibp->rvp.trap_timer,
  194. jiffies + usecs_to_jiffies(timeout));
  195. node = list_first_entry(&trap_list->list, struct trap_node,
  196. list);
  197. node->in_use = 1;
  198. }
  199. spin_unlock_irqrestore(&ibp->rvp.lock, flags);
  200. return node;
  201. }
  202. static void subn_handle_opa_trap_repress(struct hfi1_ibport *ibp,
  203. struct opa_smp *smp)
  204. {
  205. struct trap_list *trap_list;
  206. struct trap_node *trap;
  207. unsigned long flags;
  208. int i;
  209. if (smp->attr_id != IB_SMP_ATTR_NOTICE)
  210. return;
  211. spin_lock_irqsave(&ibp->rvp.lock, flags);
  212. for (i = 0; i < RVT_MAX_TRAP_LISTS; i++) {
  213. trap_list = &ibp->rvp.trap_lists[i];
  214. trap = list_first_entry_or_null(&trap_list->list,
  215. struct trap_node, list);
  216. if (trap && trap->tid == smp->tid) {
  217. if (trap->in_use) {
  218. trap->repress = 1;
  219. } else {
  220. trap_list->list_len--;
  221. list_del(&trap->list);
  222. kfree(trap);
  223. }
  224. break;
  225. }
  226. }
  227. spin_unlock_irqrestore(&ibp->rvp.lock, flags);
  228. }
  229. static void hfi1_update_sm_ah_attr(struct hfi1_ibport *ibp,
  230. struct rdma_ah_attr *attr, u32 dlid)
  231. {
  232. rdma_ah_set_dlid(attr, dlid);
  233. rdma_ah_set_port_num(attr, ppd_from_ibp(ibp)->port);
  234. if (dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) {
  235. struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
  236. rdma_ah_set_ah_flags(attr, IB_AH_GRH);
  237. grh->sgid_index = 0;
  238. grh->hop_limit = 1;
  239. grh->dgid.global.subnet_prefix =
  240. ibp->rvp.gid_prefix;
  241. grh->dgid.global.interface_id = OPA_MAKE_ID(dlid);
  242. }
  243. }
  244. static int hfi1_modify_qp0_ah(struct hfi1_ibport *ibp,
  245. struct rvt_ah *ah, u32 dlid)
  246. {
  247. struct rdma_ah_attr attr;
  248. struct rvt_qp *qp0;
  249. int ret = -EINVAL;
  250. memset(&attr, 0, sizeof(attr));
  251. attr.type = ah->ibah.type;
  252. hfi1_update_sm_ah_attr(ibp, &attr, dlid);
  253. rcu_read_lock();
  254. qp0 = rcu_dereference(ibp->rvp.qp[0]);
  255. if (qp0)
  256. ret = rdma_modify_ah(&ah->ibah, &attr);
  257. rcu_read_unlock();
  258. return ret;
  259. }
  260. static struct ib_ah *hfi1_create_qp0_ah(struct hfi1_ibport *ibp, u32 dlid)
  261. {
  262. struct rdma_ah_attr attr;
  263. struct ib_ah *ah = ERR_PTR(-EINVAL);
  264. struct rvt_qp *qp0;
  265. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  266. struct hfi1_devdata *dd = dd_from_ppd(ppd);
  267. u8 port_num = ppd->port;
  268. memset(&attr, 0, sizeof(attr));
  269. attr.type = rdma_ah_find_type(&dd->verbs_dev.rdi.ibdev, port_num);
  270. hfi1_update_sm_ah_attr(ibp, &attr, dlid);
  271. rcu_read_lock();
  272. qp0 = rcu_dereference(ibp->rvp.qp[0]);
  273. if (qp0)
  274. ah = rdma_create_ah(qp0->ibqp.pd, &attr);
  275. rcu_read_unlock();
  276. return ah;
  277. }
  278. static void send_trap(struct hfi1_ibport *ibp, struct trap_node *trap)
  279. {
  280. struct ib_mad_send_buf *send_buf;
  281. struct ib_mad_agent *agent;
  282. struct opa_smp *smp;
  283. unsigned long flags;
  284. int pkey_idx;
  285. u32 qpn = ppd_from_ibp(ibp)->sm_trap_qp;
  286. agent = ibp->rvp.send_agent;
  287. if (!agent) {
  288. cleanup_traps(ibp, trap);
  289. return;
  290. }
  291. /* o14-3.2.1 */
  292. if (driver_lstate(ppd_from_ibp(ibp)) != IB_PORT_ACTIVE) {
  293. cleanup_traps(ibp, trap);
  294. return;
  295. }
  296. /* Add the trap to the list if necessary and see if we can send it */
  297. trap = check_and_add_trap(ibp, trap);
  298. if (!trap)
  299. return;
  300. pkey_idx = hfi1_lookup_pkey_idx(ibp, LIM_MGMT_P_KEY);
  301. if (pkey_idx < 0) {
  302. pr_warn("%s: failed to find limited mgmt pkey, defaulting 0x%x\n",
  303. __func__, hfi1_get_pkey(ibp, 1));
  304. pkey_idx = 1;
  305. }
  306. send_buf = ib_create_send_mad(agent, qpn, pkey_idx, 0,
  307. IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
  308. GFP_ATOMIC, IB_MGMT_BASE_VERSION);
  309. if (IS_ERR(send_buf))
  310. return;
  311. smp = send_buf->mad;
  312. smp->base_version = OPA_MGMT_BASE_VERSION;
  313. smp->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  314. smp->class_version = OPA_SM_CLASS_VERSION;
  315. smp->method = IB_MGMT_METHOD_TRAP;
  316. /* Only update the transaction ID for new traps (o13-5). */
  317. if (trap->tid == 0) {
  318. ibp->rvp.tid++;
  319. /* make sure that tid != 0 */
  320. if (ibp->rvp.tid == 0)
  321. ibp->rvp.tid++;
  322. trap->tid = cpu_to_be64(ibp->rvp.tid);
  323. }
  324. smp->tid = trap->tid;
  325. smp->attr_id = IB_SMP_ATTR_NOTICE;
  326. /* o14-1: smp->mkey = 0; */
  327. memcpy(smp->route.lid.data, &trap->data, trap->len);
  328. spin_lock_irqsave(&ibp->rvp.lock, flags);
  329. if (!ibp->rvp.sm_ah) {
  330. if (ibp->rvp.sm_lid != be16_to_cpu(IB_LID_PERMISSIVE)) {
  331. struct ib_ah *ah;
  332. ah = hfi1_create_qp0_ah(ibp, ibp->rvp.sm_lid);
  333. if (IS_ERR(ah)) {
  334. spin_unlock_irqrestore(&ibp->rvp.lock, flags);
  335. return;
  336. }
  337. send_buf->ah = ah;
  338. ibp->rvp.sm_ah = ibah_to_rvtah(ah);
  339. } else {
  340. spin_unlock_irqrestore(&ibp->rvp.lock, flags);
  341. return;
  342. }
  343. } else {
  344. send_buf->ah = &ibp->rvp.sm_ah->ibah;
  345. }
  346. /*
  347. * If the trap was repressed while things were getting set up, don't
  348. * bother sending it. This could happen for a retry.
  349. */
  350. if (trap->repress) {
  351. list_del(&trap->list);
  352. spin_unlock_irqrestore(&ibp->rvp.lock, flags);
  353. kfree(trap);
  354. ib_free_send_mad(send_buf);
  355. return;
  356. }
  357. trap->in_use = 0;
  358. spin_unlock_irqrestore(&ibp->rvp.lock, flags);
  359. if (ib_post_send_mad(send_buf, NULL))
  360. ib_free_send_mad(send_buf);
  361. }
  362. void hfi1_handle_trap_timer(struct timer_list *t)
  363. {
  364. struct hfi1_ibport *ibp = from_timer(ibp, t, rvp.trap_timer);
  365. struct trap_node *trap = NULL;
  366. unsigned long flags;
  367. int i;
  368. /* Find the trap with the highest priority */
  369. spin_lock_irqsave(&ibp->rvp.lock, flags);
  370. for (i = 0; !trap && i < RVT_MAX_TRAP_LISTS; i++) {
  371. trap = list_first_entry_or_null(&ibp->rvp.trap_lists[i].list,
  372. struct trap_node, list);
  373. }
  374. spin_unlock_irqrestore(&ibp->rvp.lock, flags);
  375. if (trap)
  376. send_trap(ibp, trap);
  377. }
  378. static struct trap_node *create_trap_node(u8 type, __be16 trap_num, u32 lid)
  379. {
  380. struct trap_node *trap;
  381. trap = kzalloc(sizeof(*trap), GFP_ATOMIC);
  382. if (!trap)
  383. return NULL;
  384. INIT_LIST_HEAD(&trap->list);
  385. trap->data.generic_type = type;
  386. trap->data.prod_type_lsb = IB_NOTICE_PROD_CA;
  387. trap->data.trap_num = trap_num;
  388. trap->data.issuer_lid = cpu_to_be32(lid);
  389. return trap;
  390. }
  391. /*
  392. * Send a bad P_Key trap (ch. 14.3.8).
  393. */
  394. void hfi1_bad_pkey(struct hfi1_ibport *ibp, u32 key, u32 sl,
  395. u32 qp1, u32 qp2, u32 lid1, u32 lid2)
  396. {
  397. struct trap_node *trap;
  398. u32 lid = ppd_from_ibp(ibp)->lid;
  399. ibp->rvp.n_pkt_drops++;
  400. ibp->rvp.pkey_violations++;
  401. trap = create_trap_node(IB_NOTICE_TYPE_SECURITY, OPA_TRAP_BAD_P_KEY,
  402. lid);
  403. if (!trap)
  404. return;
  405. /* Send violation trap */
  406. trap->data.ntc_257_258.lid1 = cpu_to_be32(lid1);
  407. trap->data.ntc_257_258.lid2 = cpu_to_be32(lid2);
  408. trap->data.ntc_257_258.key = cpu_to_be32(key);
  409. trap->data.ntc_257_258.sl = sl << 3;
  410. trap->data.ntc_257_258.qp1 = cpu_to_be32(qp1);
  411. trap->data.ntc_257_258.qp2 = cpu_to_be32(qp2);
  412. trap->len = sizeof(trap->data);
  413. send_trap(ibp, trap);
  414. }
  415. /*
  416. * Send a bad M_Key trap (ch. 14.3.9).
  417. */
  418. static void bad_mkey(struct hfi1_ibport *ibp, struct ib_mad_hdr *mad,
  419. __be64 mkey, __be32 dr_slid, u8 return_path[], u8 hop_cnt)
  420. {
  421. struct trap_node *trap;
  422. u32 lid = ppd_from_ibp(ibp)->lid;
  423. trap = create_trap_node(IB_NOTICE_TYPE_SECURITY, OPA_TRAP_BAD_M_KEY,
  424. lid);
  425. if (!trap)
  426. return;
  427. /* Send violation trap */
  428. trap->data.ntc_256.lid = trap->data.issuer_lid;
  429. trap->data.ntc_256.method = mad->method;
  430. trap->data.ntc_256.attr_id = mad->attr_id;
  431. trap->data.ntc_256.attr_mod = mad->attr_mod;
  432. trap->data.ntc_256.mkey = mkey;
  433. if (mad->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  434. trap->data.ntc_256.dr_slid = dr_slid;
  435. trap->data.ntc_256.dr_trunc_hop = IB_NOTICE_TRAP_DR_NOTICE;
  436. if (hop_cnt > ARRAY_SIZE(trap->data.ntc_256.dr_rtn_path)) {
  437. trap->data.ntc_256.dr_trunc_hop |=
  438. IB_NOTICE_TRAP_DR_TRUNC;
  439. hop_cnt = ARRAY_SIZE(trap->data.ntc_256.dr_rtn_path);
  440. }
  441. trap->data.ntc_256.dr_trunc_hop |= hop_cnt;
  442. memcpy(trap->data.ntc_256.dr_rtn_path, return_path,
  443. hop_cnt);
  444. }
  445. trap->len = sizeof(trap->data);
  446. send_trap(ibp, trap);
  447. }
  448. /*
  449. * Send a Port Capability Mask Changed trap (ch. 14.3.11).
  450. */
  451. void hfi1_cap_mask_chg(struct rvt_dev_info *rdi, u8 port_num)
  452. {
  453. struct trap_node *trap;
  454. struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
  455. struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
  456. struct hfi1_ibport *ibp = &dd->pport[port_num - 1].ibport_data;
  457. u32 lid = ppd_from_ibp(ibp)->lid;
  458. trap = create_trap_node(IB_NOTICE_TYPE_INFO,
  459. OPA_TRAP_CHANGE_CAPABILITY,
  460. lid);
  461. if (!trap)
  462. return;
  463. trap->data.ntc_144.lid = trap->data.issuer_lid;
  464. trap->data.ntc_144.new_cap_mask = cpu_to_be32(ibp->rvp.port_cap_flags);
  465. trap->data.ntc_144.cap_mask3 = cpu_to_be16(ibp->rvp.port_cap3_flags);
  466. trap->len = sizeof(trap->data);
  467. send_trap(ibp, trap);
  468. }
  469. /*
  470. * Send a System Image GUID Changed trap (ch. 14.3.12).
  471. */
  472. void hfi1_sys_guid_chg(struct hfi1_ibport *ibp)
  473. {
  474. struct trap_node *trap;
  475. u32 lid = ppd_from_ibp(ibp)->lid;
  476. trap = create_trap_node(IB_NOTICE_TYPE_INFO, OPA_TRAP_CHANGE_SYSGUID,
  477. lid);
  478. if (!trap)
  479. return;
  480. trap->data.ntc_145.new_sys_guid = ib_hfi1_sys_image_guid;
  481. trap->data.ntc_145.lid = trap->data.issuer_lid;
  482. trap->len = sizeof(trap->data);
  483. send_trap(ibp, trap);
  484. }
  485. /*
  486. * Send a Node Description Changed trap (ch. 14.3.13).
  487. */
  488. void hfi1_node_desc_chg(struct hfi1_ibport *ibp)
  489. {
  490. struct trap_node *trap;
  491. u32 lid = ppd_from_ibp(ibp)->lid;
  492. trap = create_trap_node(IB_NOTICE_TYPE_INFO,
  493. OPA_TRAP_CHANGE_CAPABILITY,
  494. lid);
  495. if (!trap)
  496. return;
  497. trap->data.ntc_144.lid = trap->data.issuer_lid;
  498. trap->data.ntc_144.change_flags =
  499. cpu_to_be16(OPA_NOTICE_TRAP_NODE_DESC_CHG);
  500. trap->len = sizeof(trap->data);
  501. send_trap(ibp, trap);
  502. }
  503. static int __subn_get_opa_nodedesc(struct opa_smp *smp, u32 am,
  504. u8 *data, struct ib_device *ibdev,
  505. u8 port, u32 *resp_len, u32 max_len)
  506. {
  507. struct opa_node_description *nd;
  508. if (am || smp_length_check(sizeof(*nd), max_len)) {
  509. smp->status |= IB_SMP_INVALID_FIELD;
  510. return reply((struct ib_mad_hdr *)smp);
  511. }
  512. nd = (struct opa_node_description *)data;
  513. memcpy(nd->data, ibdev->node_desc, sizeof(nd->data));
  514. if (resp_len)
  515. *resp_len += sizeof(*nd);
  516. return reply((struct ib_mad_hdr *)smp);
  517. }
  518. static int __subn_get_opa_nodeinfo(struct opa_smp *smp, u32 am, u8 *data,
  519. struct ib_device *ibdev, u8 port,
  520. u32 *resp_len, u32 max_len)
  521. {
  522. struct opa_node_info *ni;
  523. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  524. unsigned pidx = port - 1; /* IB number port from 1, hw from 0 */
  525. ni = (struct opa_node_info *)data;
  526. /* GUID 0 is illegal */
  527. if (am || pidx >= dd->num_pports || ibdev->node_guid == 0 ||
  528. smp_length_check(sizeof(*ni), max_len) ||
  529. get_sguid(to_iport(ibdev, port), HFI1_PORT_GUID_INDEX) == 0) {
  530. smp->status |= IB_SMP_INVALID_FIELD;
  531. return reply((struct ib_mad_hdr *)smp);
  532. }
  533. ni->port_guid = get_sguid(to_iport(ibdev, port), HFI1_PORT_GUID_INDEX);
  534. ni->base_version = OPA_MGMT_BASE_VERSION;
  535. ni->class_version = OPA_SM_CLASS_VERSION;
  536. ni->node_type = 1; /* channel adapter */
  537. ni->num_ports = ibdev->phys_port_cnt;
  538. /* This is already in network order */
  539. ni->system_image_guid = ib_hfi1_sys_image_guid;
  540. ni->node_guid = ibdev->node_guid;
  541. ni->partition_cap = cpu_to_be16(hfi1_get_npkeys(dd));
  542. ni->device_id = cpu_to_be16(dd->pcidev->device);
  543. ni->revision = cpu_to_be32(dd->minrev);
  544. ni->local_port_num = port;
  545. ni->vendor_id[0] = dd->oui1;
  546. ni->vendor_id[1] = dd->oui2;
  547. ni->vendor_id[2] = dd->oui3;
  548. if (resp_len)
  549. *resp_len += sizeof(*ni);
  550. return reply((struct ib_mad_hdr *)smp);
  551. }
  552. static int subn_get_nodeinfo(struct ib_smp *smp, struct ib_device *ibdev,
  553. u8 port)
  554. {
  555. struct ib_node_info *nip = (struct ib_node_info *)&smp->data;
  556. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  557. unsigned pidx = port - 1; /* IB number port from 1, hw from 0 */
  558. /* GUID 0 is illegal */
  559. if (smp->attr_mod || pidx >= dd->num_pports ||
  560. ibdev->node_guid == 0 ||
  561. get_sguid(to_iport(ibdev, port), HFI1_PORT_GUID_INDEX) == 0) {
  562. smp->status |= IB_SMP_INVALID_FIELD;
  563. return reply((struct ib_mad_hdr *)smp);
  564. }
  565. nip->port_guid = get_sguid(to_iport(ibdev, port), HFI1_PORT_GUID_INDEX);
  566. nip->base_version = OPA_MGMT_BASE_VERSION;
  567. nip->class_version = OPA_SM_CLASS_VERSION;
  568. nip->node_type = 1; /* channel adapter */
  569. nip->num_ports = ibdev->phys_port_cnt;
  570. /* This is already in network order */
  571. nip->sys_guid = ib_hfi1_sys_image_guid;
  572. nip->node_guid = ibdev->node_guid;
  573. nip->partition_cap = cpu_to_be16(hfi1_get_npkeys(dd));
  574. nip->device_id = cpu_to_be16(dd->pcidev->device);
  575. nip->revision = cpu_to_be32(dd->minrev);
  576. nip->local_port_num = port;
  577. nip->vendor_id[0] = dd->oui1;
  578. nip->vendor_id[1] = dd->oui2;
  579. nip->vendor_id[2] = dd->oui3;
  580. return reply((struct ib_mad_hdr *)smp);
  581. }
  582. static void set_link_width_enabled(struct hfi1_pportdata *ppd, u32 w)
  583. {
  584. (void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LWID_ENB, w);
  585. }
  586. static void set_link_width_downgrade_enabled(struct hfi1_pportdata *ppd, u32 w)
  587. {
  588. (void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LWID_DG_ENB, w);
  589. }
  590. static void set_link_speed_enabled(struct hfi1_pportdata *ppd, u32 s)
  591. {
  592. (void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_SPD_ENB, s);
  593. }
  594. static int check_mkey(struct hfi1_ibport *ibp, struct ib_mad_hdr *mad,
  595. int mad_flags, __be64 mkey, __be32 dr_slid,
  596. u8 return_path[], u8 hop_cnt)
  597. {
  598. int valid_mkey = 0;
  599. int ret = 0;
  600. /* Is the mkey in the process of expiring? */
  601. if (ibp->rvp.mkey_lease_timeout &&
  602. time_after_eq(jiffies, ibp->rvp.mkey_lease_timeout)) {
  603. /* Clear timeout and mkey protection field. */
  604. ibp->rvp.mkey_lease_timeout = 0;
  605. ibp->rvp.mkeyprot = 0;
  606. }
  607. if ((mad_flags & IB_MAD_IGNORE_MKEY) || ibp->rvp.mkey == 0 ||
  608. ibp->rvp.mkey == mkey)
  609. valid_mkey = 1;
  610. /* Unset lease timeout on any valid Get/Set/TrapRepress */
  611. if (valid_mkey && ibp->rvp.mkey_lease_timeout &&
  612. (mad->method == IB_MGMT_METHOD_GET ||
  613. mad->method == IB_MGMT_METHOD_SET ||
  614. mad->method == IB_MGMT_METHOD_TRAP_REPRESS))
  615. ibp->rvp.mkey_lease_timeout = 0;
  616. if (!valid_mkey) {
  617. switch (mad->method) {
  618. case IB_MGMT_METHOD_GET:
  619. /* Bad mkey not a violation below level 2 */
  620. if (ibp->rvp.mkeyprot < 2)
  621. break;
  622. /* fall through */
  623. case IB_MGMT_METHOD_SET:
  624. case IB_MGMT_METHOD_TRAP_REPRESS:
  625. if (ibp->rvp.mkey_violations != 0xFFFF)
  626. ++ibp->rvp.mkey_violations;
  627. if (!ibp->rvp.mkey_lease_timeout &&
  628. ibp->rvp.mkey_lease_period)
  629. ibp->rvp.mkey_lease_timeout = jiffies +
  630. ibp->rvp.mkey_lease_period * HZ;
  631. /* Generate a trap notice. */
  632. bad_mkey(ibp, mad, mkey, dr_slid, return_path,
  633. hop_cnt);
  634. ret = 1;
  635. }
  636. }
  637. return ret;
  638. }
  639. /*
  640. * The SMA caches reads from LCB registers in case the LCB is unavailable.
  641. * (The LCB is unavailable in certain link states, for example.)
  642. */
  643. struct lcb_datum {
  644. u32 off;
  645. u64 val;
  646. };
  647. static struct lcb_datum lcb_cache[] = {
  648. { DC_LCB_STS_ROUND_TRIP_LTP_CNT, 0 },
  649. };
  650. static int write_lcb_cache(u32 off, u64 val)
  651. {
  652. int i;
  653. for (i = 0; i < ARRAY_SIZE(lcb_cache); i++) {
  654. if (lcb_cache[i].off == off) {
  655. lcb_cache[i].val = val;
  656. return 0;
  657. }
  658. }
  659. pr_warn("%s bad offset 0x%x\n", __func__, off);
  660. return -1;
  661. }
  662. static int read_lcb_cache(u32 off, u64 *val)
  663. {
  664. int i;
  665. for (i = 0; i < ARRAY_SIZE(lcb_cache); i++) {
  666. if (lcb_cache[i].off == off) {
  667. *val = lcb_cache[i].val;
  668. return 0;
  669. }
  670. }
  671. pr_warn("%s bad offset 0x%x\n", __func__, off);
  672. return -1;
  673. }
  674. void read_ltp_rtt(struct hfi1_devdata *dd)
  675. {
  676. u64 reg;
  677. if (read_lcb_csr(dd, DC_LCB_STS_ROUND_TRIP_LTP_CNT, &reg))
  678. dd_dev_err(dd, "%s: unable to read LTP RTT\n", __func__);
  679. else
  680. write_lcb_cache(DC_LCB_STS_ROUND_TRIP_LTP_CNT, reg);
  681. }
  682. static int __subn_get_opa_portinfo(struct opa_smp *smp, u32 am, u8 *data,
  683. struct ib_device *ibdev, u8 port,
  684. u32 *resp_len, u32 max_len)
  685. {
  686. int i;
  687. struct hfi1_devdata *dd;
  688. struct hfi1_pportdata *ppd;
  689. struct hfi1_ibport *ibp;
  690. struct opa_port_info *pi = (struct opa_port_info *)data;
  691. u8 mtu;
  692. u8 credit_rate;
  693. u8 is_beaconing_active;
  694. u32 state;
  695. u32 num_ports = OPA_AM_NPORT(am);
  696. u32 start_of_sm_config = OPA_AM_START_SM_CFG(am);
  697. u32 buffer_units;
  698. u64 tmp = 0;
  699. if (num_ports != 1 || smp_length_check(sizeof(*pi), max_len)) {
  700. smp->status |= IB_SMP_INVALID_FIELD;
  701. return reply((struct ib_mad_hdr *)smp);
  702. }
  703. dd = dd_from_ibdev(ibdev);
  704. /* IB numbers ports from 1, hw from 0 */
  705. ppd = dd->pport + (port - 1);
  706. ibp = &ppd->ibport_data;
  707. if (ppd->vls_supported / 2 > ARRAY_SIZE(pi->neigh_mtu.pvlx_to_mtu) ||
  708. ppd->vls_supported > ARRAY_SIZE(dd->vld)) {
  709. smp->status |= IB_SMP_INVALID_FIELD;
  710. return reply((struct ib_mad_hdr *)smp);
  711. }
  712. pi->lid = cpu_to_be32(ppd->lid);
  713. /* Only return the mkey if the protection field allows it. */
  714. if (!(smp->method == IB_MGMT_METHOD_GET &&
  715. ibp->rvp.mkey != smp->mkey &&
  716. ibp->rvp.mkeyprot == 1))
  717. pi->mkey = ibp->rvp.mkey;
  718. pi->subnet_prefix = ibp->rvp.gid_prefix;
  719. pi->sm_lid = cpu_to_be32(ibp->rvp.sm_lid);
  720. pi->ib_cap_mask = cpu_to_be32(ibp->rvp.port_cap_flags);
  721. pi->mkey_lease_period = cpu_to_be16(ibp->rvp.mkey_lease_period);
  722. pi->sm_trap_qp = cpu_to_be32(ppd->sm_trap_qp);
  723. pi->sa_qp = cpu_to_be32(ppd->sa_qp);
  724. pi->link_width.enabled = cpu_to_be16(ppd->link_width_enabled);
  725. pi->link_width.supported = cpu_to_be16(ppd->link_width_supported);
  726. pi->link_width.active = cpu_to_be16(ppd->link_width_active);
  727. pi->link_width_downgrade.supported =
  728. cpu_to_be16(ppd->link_width_downgrade_supported);
  729. pi->link_width_downgrade.enabled =
  730. cpu_to_be16(ppd->link_width_downgrade_enabled);
  731. pi->link_width_downgrade.tx_active =
  732. cpu_to_be16(ppd->link_width_downgrade_tx_active);
  733. pi->link_width_downgrade.rx_active =
  734. cpu_to_be16(ppd->link_width_downgrade_rx_active);
  735. pi->link_speed.supported = cpu_to_be16(ppd->link_speed_supported);
  736. pi->link_speed.active = cpu_to_be16(ppd->link_speed_active);
  737. pi->link_speed.enabled = cpu_to_be16(ppd->link_speed_enabled);
  738. state = driver_lstate(ppd);
  739. if (start_of_sm_config && (state == IB_PORT_INIT))
  740. ppd->is_sm_config_started = 1;
  741. pi->port_phys_conf = (ppd->port_type & 0xf);
  742. pi->port_states.ledenable_offlinereason = ppd->neighbor_normal << 4;
  743. pi->port_states.ledenable_offlinereason |=
  744. ppd->is_sm_config_started << 5;
  745. /*
  746. * This pairs with the memory barrier in hfi1_start_led_override to
  747. * ensure that we read the correct state of LED beaconing represented
  748. * by led_override_timer_active
  749. */
  750. smp_rmb();
  751. is_beaconing_active = !!atomic_read(&ppd->led_override_timer_active);
  752. pi->port_states.ledenable_offlinereason |= is_beaconing_active << 6;
  753. pi->port_states.ledenable_offlinereason |=
  754. ppd->offline_disabled_reason;
  755. pi->port_states.portphysstate_portstate =
  756. (driver_pstate(ppd) << 4) | state;
  757. pi->mkeyprotect_lmc = (ibp->rvp.mkeyprot << 6) | ppd->lmc;
  758. memset(pi->neigh_mtu.pvlx_to_mtu, 0, sizeof(pi->neigh_mtu.pvlx_to_mtu));
  759. for (i = 0; i < ppd->vls_supported; i++) {
  760. mtu = mtu_to_enum(dd->vld[i].mtu, HFI1_DEFAULT_ACTIVE_MTU);
  761. if ((i % 2) == 0)
  762. pi->neigh_mtu.pvlx_to_mtu[i / 2] |= (mtu << 4);
  763. else
  764. pi->neigh_mtu.pvlx_to_mtu[i / 2] |= mtu;
  765. }
  766. /* don't forget VL 15 */
  767. mtu = mtu_to_enum(dd->vld[15].mtu, 2048);
  768. pi->neigh_mtu.pvlx_to_mtu[15 / 2] |= mtu;
  769. pi->smsl = ibp->rvp.sm_sl & OPA_PI_MASK_SMSL;
  770. pi->operational_vls = hfi1_get_ib_cfg(ppd, HFI1_IB_CFG_OP_VLS);
  771. pi->partenforce_filterraw |=
  772. (ppd->linkinit_reason & OPA_PI_MASK_LINKINIT_REASON);
  773. if (ppd->part_enforce & HFI1_PART_ENFORCE_IN)
  774. pi->partenforce_filterraw |= OPA_PI_MASK_PARTITION_ENFORCE_IN;
  775. if (ppd->part_enforce & HFI1_PART_ENFORCE_OUT)
  776. pi->partenforce_filterraw |= OPA_PI_MASK_PARTITION_ENFORCE_OUT;
  777. pi->mkey_violations = cpu_to_be16(ibp->rvp.mkey_violations);
  778. /* P_KeyViolations are counted by hardware. */
  779. pi->pkey_violations = cpu_to_be16(ibp->rvp.pkey_violations);
  780. pi->qkey_violations = cpu_to_be16(ibp->rvp.qkey_violations);
  781. pi->vl.cap = ppd->vls_supported;
  782. pi->vl.high_limit = cpu_to_be16(ibp->rvp.vl_high_limit);
  783. pi->vl.arb_high_cap = (u8)hfi1_get_ib_cfg(ppd, HFI1_IB_CFG_VL_HIGH_CAP);
  784. pi->vl.arb_low_cap = (u8)hfi1_get_ib_cfg(ppd, HFI1_IB_CFG_VL_LOW_CAP);
  785. pi->clientrereg_subnettimeout = ibp->rvp.subnet_timeout;
  786. pi->port_link_mode = cpu_to_be16(OPA_PORT_LINK_MODE_OPA << 10 |
  787. OPA_PORT_LINK_MODE_OPA << 5 |
  788. OPA_PORT_LINK_MODE_OPA);
  789. pi->port_ltp_crc_mode = cpu_to_be16(ppd->port_ltp_crc_mode);
  790. pi->port_mode = cpu_to_be16(
  791. ppd->is_active_optimize_enabled ?
  792. OPA_PI_MASK_PORT_ACTIVE_OPTOMIZE : 0);
  793. pi->port_packet_format.supported =
  794. cpu_to_be16(OPA_PORT_PACKET_FORMAT_9B |
  795. OPA_PORT_PACKET_FORMAT_16B);
  796. pi->port_packet_format.enabled =
  797. cpu_to_be16(OPA_PORT_PACKET_FORMAT_9B |
  798. OPA_PORT_PACKET_FORMAT_16B);
  799. /* flit_control.interleave is (OPA V1, version .76):
  800. * bits use
  801. * ---- ---
  802. * 2 res
  803. * 2 DistanceSupported
  804. * 2 DistanceEnabled
  805. * 5 MaxNextLevelTxEnabled
  806. * 5 MaxNestLevelRxSupported
  807. *
  808. * HFI supports only "distance mode 1" (see OPA V1, version .76,
  809. * section 9.6.2), so set DistanceSupported, DistanceEnabled
  810. * to 0x1.
  811. */
  812. pi->flit_control.interleave = cpu_to_be16(0x1400);
  813. pi->link_down_reason = ppd->local_link_down_reason.sma;
  814. pi->neigh_link_down_reason = ppd->neigh_link_down_reason.sma;
  815. pi->port_error_action = cpu_to_be32(ppd->port_error_action);
  816. pi->mtucap = mtu_to_enum(hfi1_max_mtu, IB_MTU_4096);
  817. /* 32.768 usec. response time (guessing) */
  818. pi->resptimevalue = 3;
  819. pi->local_port_num = port;
  820. /* buffer info for FM */
  821. pi->overall_buffer_space = cpu_to_be16(dd->link_credits);
  822. pi->neigh_node_guid = cpu_to_be64(ppd->neighbor_guid);
  823. pi->neigh_port_num = ppd->neighbor_port_number;
  824. pi->port_neigh_mode =
  825. (ppd->neighbor_type & OPA_PI_MASK_NEIGH_NODE_TYPE) |
  826. (ppd->mgmt_allowed ? OPA_PI_MASK_NEIGH_MGMT_ALLOWED : 0) |
  827. (ppd->neighbor_fm_security ?
  828. OPA_PI_MASK_NEIGH_FW_AUTH_BYPASS : 0);
  829. /* HFIs shall always return VL15 credits to their
  830. * neighbor in a timely manner, without any credit return pacing.
  831. */
  832. credit_rate = 0;
  833. buffer_units = (dd->vau) & OPA_PI_MASK_BUF_UNIT_BUF_ALLOC;
  834. buffer_units |= (dd->vcu << 3) & OPA_PI_MASK_BUF_UNIT_CREDIT_ACK;
  835. buffer_units |= (credit_rate << 6) &
  836. OPA_PI_MASK_BUF_UNIT_VL15_CREDIT_RATE;
  837. buffer_units |= (dd->vl15_init << 11) & OPA_PI_MASK_BUF_UNIT_VL15_INIT;
  838. pi->buffer_units = cpu_to_be32(buffer_units);
  839. pi->opa_cap_mask = cpu_to_be16(ibp->rvp.port_cap3_flags);
  840. pi->collectivemask_multicastmask = ((OPA_COLLECTIVE_NR & 0x7)
  841. << 3 | (OPA_MCAST_NR & 0x7));
  842. /* HFI supports a replay buffer 128 LTPs in size */
  843. pi->replay_depth.buffer = 0x80;
  844. /* read the cached value of DC_LCB_STS_ROUND_TRIP_LTP_CNT */
  845. read_lcb_cache(DC_LCB_STS_ROUND_TRIP_LTP_CNT, &tmp);
  846. /*
  847. * this counter is 16 bits wide, but the replay_depth.wire
  848. * variable is only 8 bits
  849. */
  850. if (tmp > 0xff)
  851. tmp = 0xff;
  852. pi->replay_depth.wire = tmp;
  853. if (resp_len)
  854. *resp_len += sizeof(struct opa_port_info);
  855. return reply((struct ib_mad_hdr *)smp);
  856. }
  857. /**
  858. * get_pkeys - return the PKEY table
  859. * @dd: the hfi1_ib device
  860. * @port: the IB port number
  861. * @pkeys: the pkey table is placed here
  862. */
  863. static int get_pkeys(struct hfi1_devdata *dd, u8 port, u16 *pkeys)
  864. {
  865. struct hfi1_pportdata *ppd = dd->pport + port - 1;
  866. memcpy(pkeys, ppd->pkeys, sizeof(ppd->pkeys));
  867. return 0;
  868. }
  869. static int __subn_get_opa_pkeytable(struct opa_smp *smp, u32 am, u8 *data,
  870. struct ib_device *ibdev, u8 port,
  871. u32 *resp_len, u32 max_len)
  872. {
  873. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  874. u32 n_blocks_req = OPA_AM_NBLK(am);
  875. u32 start_block = am & 0x7ff;
  876. __be16 *p;
  877. u16 *q;
  878. int i;
  879. u16 n_blocks_avail;
  880. unsigned npkeys = hfi1_get_npkeys(dd);
  881. size_t size;
  882. if (n_blocks_req == 0) {
  883. pr_warn("OPA Get PKey AM Invalid : P = %d; B = 0x%x; N = 0x%x\n",
  884. port, start_block, n_blocks_req);
  885. smp->status |= IB_SMP_INVALID_FIELD;
  886. return reply((struct ib_mad_hdr *)smp);
  887. }
  888. n_blocks_avail = (u16)(npkeys / OPA_PARTITION_TABLE_BLK_SIZE) + 1;
  889. size = (n_blocks_req * OPA_PARTITION_TABLE_BLK_SIZE) * sizeof(u16);
  890. if (smp_length_check(size, max_len)) {
  891. smp->status |= IB_SMP_INVALID_FIELD;
  892. return reply((struct ib_mad_hdr *)smp);
  893. }
  894. if (start_block + n_blocks_req > n_blocks_avail ||
  895. n_blocks_req > OPA_NUM_PKEY_BLOCKS_PER_SMP) {
  896. pr_warn("OPA Get PKey AM Invalid : s 0x%x; req 0x%x; "
  897. "avail 0x%x; blk/smp 0x%lx\n",
  898. start_block, n_blocks_req, n_blocks_avail,
  899. OPA_NUM_PKEY_BLOCKS_PER_SMP);
  900. smp->status |= IB_SMP_INVALID_FIELD;
  901. return reply((struct ib_mad_hdr *)smp);
  902. }
  903. p = (__be16 *)data;
  904. q = (u16 *)data;
  905. /* get the real pkeys if we are requesting the first block */
  906. if (start_block == 0) {
  907. get_pkeys(dd, port, q);
  908. for (i = 0; i < npkeys; i++)
  909. p[i] = cpu_to_be16(q[i]);
  910. if (resp_len)
  911. *resp_len += size;
  912. } else {
  913. smp->status |= IB_SMP_INVALID_FIELD;
  914. }
  915. return reply((struct ib_mad_hdr *)smp);
  916. }
  917. enum {
  918. HFI_TRANSITION_DISALLOWED,
  919. HFI_TRANSITION_IGNORED,
  920. HFI_TRANSITION_ALLOWED,
  921. HFI_TRANSITION_UNDEFINED,
  922. };
  923. /*
  924. * Use shortened names to improve readability of
  925. * {logical,physical}_state_transitions
  926. */
  927. enum {
  928. __D = HFI_TRANSITION_DISALLOWED,
  929. __I = HFI_TRANSITION_IGNORED,
  930. __A = HFI_TRANSITION_ALLOWED,
  931. __U = HFI_TRANSITION_UNDEFINED,
  932. };
  933. /*
  934. * IB_PORTPHYSSTATE_POLLING (2) through OPA_PORTPHYSSTATE_MAX (11) are
  935. * represented in physical_state_transitions.
  936. */
  937. #define __N_PHYSTATES (OPA_PORTPHYSSTATE_MAX - IB_PORTPHYSSTATE_POLLING + 1)
  938. /*
  939. * Within physical_state_transitions, rows represent "old" states,
  940. * columns "new" states, and physical_state_transitions.allowed[old][new]
  941. * indicates if the transition from old state to new state is legal (see
  942. * OPAg1v1, Table 6-4).
  943. */
  944. static const struct {
  945. u8 allowed[__N_PHYSTATES][__N_PHYSTATES];
  946. } physical_state_transitions = {
  947. {
  948. /* 2 3 4 5 6 7 8 9 10 11 */
  949. /* 2 */ { __A, __A, __D, __D, __D, __D, __D, __D, __D, __D },
  950. /* 3 */ { __A, __I, __D, __D, __D, __D, __D, __D, __D, __A },
  951. /* 4 */ { __U, __U, __U, __U, __U, __U, __U, __U, __U, __U },
  952. /* 5 */ { __A, __A, __D, __I, __D, __D, __D, __D, __D, __D },
  953. /* 6 */ { __U, __U, __U, __U, __U, __U, __U, __U, __U, __U },
  954. /* 7 */ { __D, __A, __D, __D, __D, __I, __D, __D, __D, __D },
  955. /* 8 */ { __U, __U, __U, __U, __U, __U, __U, __U, __U, __U },
  956. /* 9 */ { __I, __A, __D, __D, __D, __D, __D, __I, __D, __D },
  957. /*10 */ { __U, __U, __U, __U, __U, __U, __U, __U, __U, __U },
  958. /*11 */ { __D, __A, __D, __D, __D, __D, __D, __D, __D, __I },
  959. }
  960. };
  961. /*
  962. * IB_PORT_DOWN (1) through IB_PORT_ACTIVE_DEFER (5) are represented
  963. * logical_state_transitions
  964. */
  965. #define __N_LOGICAL_STATES (IB_PORT_ACTIVE_DEFER - IB_PORT_DOWN + 1)
  966. /*
  967. * Within logical_state_transitions rows represent "old" states,
  968. * columns "new" states, and logical_state_transitions.allowed[old][new]
  969. * indicates if the transition from old state to new state is legal (see
  970. * OPAg1v1, Table 9-12).
  971. */
  972. static const struct {
  973. u8 allowed[__N_LOGICAL_STATES][__N_LOGICAL_STATES];
  974. } logical_state_transitions = {
  975. {
  976. /* 1 2 3 4 5 */
  977. /* 1 */ { __I, __D, __D, __D, __U},
  978. /* 2 */ { __D, __I, __A, __D, __U},
  979. /* 3 */ { __D, __D, __I, __A, __U},
  980. /* 4 */ { __D, __D, __I, __I, __U},
  981. /* 5 */ { __U, __U, __U, __U, __U},
  982. }
  983. };
  984. static int logical_transition_allowed(int old, int new)
  985. {
  986. if (old < IB_PORT_NOP || old > IB_PORT_ACTIVE_DEFER ||
  987. new < IB_PORT_NOP || new > IB_PORT_ACTIVE_DEFER) {
  988. pr_warn("invalid logical state(s) (old %d new %d)\n",
  989. old, new);
  990. return HFI_TRANSITION_UNDEFINED;
  991. }
  992. if (new == IB_PORT_NOP)
  993. return HFI_TRANSITION_ALLOWED; /* always allowed */
  994. /* adjust states for indexing into logical_state_transitions */
  995. old -= IB_PORT_DOWN;
  996. new -= IB_PORT_DOWN;
  997. if (old < 0 || new < 0)
  998. return HFI_TRANSITION_UNDEFINED;
  999. return logical_state_transitions.allowed[old][new];
  1000. }
  1001. static int physical_transition_allowed(int old, int new)
  1002. {
  1003. if (old < IB_PORTPHYSSTATE_NOP || old > OPA_PORTPHYSSTATE_MAX ||
  1004. new < IB_PORTPHYSSTATE_NOP || new > OPA_PORTPHYSSTATE_MAX) {
  1005. pr_warn("invalid physical state(s) (old %d new %d)\n",
  1006. old, new);
  1007. return HFI_TRANSITION_UNDEFINED;
  1008. }
  1009. if (new == IB_PORTPHYSSTATE_NOP)
  1010. return HFI_TRANSITION_ALLOWED; /* always allowed */
  1011. /* adjust states for indexing into physical_state_transitions */
  1012. old -= IB_PORTPHYSSTATE_POLLING;
  1013. new -= IB_PORTPHYSSTATE_POLLING;
  1014. if (old < 0 || new < 0)
  1015. return HFI_TRANSITION_UNDEFINED;
  1016. return physical_state_transitions.allowed[old][new];
  1017. }
  1018. static int port_states_transition_allowed(struct hfi1_pportdata *ppd,
  1019. u32 logical_new, u32 physical_new)
  1020. {
  1021. u32 physical_old = driver_pstate(ppd);
  1022. u32 logical_old = driver_lstate(ppd);
  1023. int ret, logical_allowed, physical_allowed;
  1024. ret = logical_transition_allowed(logical_old, logical_new);
  1025. logical_allowed = ret;
  1026. if (ret == HFI_TRANSITION_DISALLOWED ||
  1027. ret == HFI_TRANSITION_UNDEFINED) {
  1028. pr_warn("invalid logical state transition %s -> %s\n",
  1029. opa_lstate_name(logical_old),
  1030. opa_lstate_name(logical_new));
  1031. return ret;
  1032. }
  1033. ret = physical_transition_allowed(physical_old, physical_new);
  1034. physical_allowed = ret;
  1035. if (ret == HFI_TRANSITION_DISALLOWED ||
  1036. ret == HFI_TRANSITION_UNDEFINED) {
  1037. pr_warn("invalid physical state transition %s -> %s\n",
  1038. opa_pstate_name(physical_old),
  1039. opa_pstate_name(physical_new));
  1040. return ret;
  1041. }
  1042. if (logical_allowed == HFI_TRANSITION_IGNORED &&
  1043. physical_allowed == HFI_TRANSITION_IGNORED)
  1044. return HFI_TRANSITION_IGNORED;
  1045. /*
  1046. * A change request of Physical Port State from
  1047. * 'Offline' to 'Polling' should be ignored.
  1048. */
  1049. if ((physical_old == OPA_PORTPHYSSTATE_OFFLINE) &&
  1050. (physical_new == IB_PORTPHYSSTATE_POLLING))
  1051. return HFI_TRANSITION_IGNORED;
  1052. /*
  1053. * Either physical_allowed or logical_allowed is
  1054. * HFI_TRANSITION_ALLOWED.
  1055. */
  1056. return HFI_TRANSITION_ALLOWED;
  1057. }
  1058. static int set_port_states(struct hfi1_pportdata *ppd, struct opa_smp *smp,
  1059. u32 logical_state, u32 phys_state)
  1060. {
  1061. struct hfi1_devdata *dd = ppd->dd;
  1062. u32 link_state;
  1063. int ret;
  1064. ret = port_states_transition_allowed(ppd, logical_state, phys_state);
  1065. if (ret == HFI_TRANSITION_DISALLOWED ||
  1066. ret == HFI_TRANSITION_UNDEFINED) {
  1067. /* error message emitted above */
  1068. smp->status |= IB_SMP_INVALID_FIELD;
  1069. return 0;
  1070. }
  1071. if (ret == HFI_TRANSITION_IGNORED)
  1072. return 0;
  1073. if ((phys_state != IB_PORTPHYSSTATE_NOP) &&
  1074. !(logical_state == IB_PORT_DOWN ||
  1075. logical_state == IB_PORT_NOP)){
  1076. pr_warn("SubnSet(OPA_PortInfo) port state invalid: logical_state 0x%x physical_state 0x%x\n",
  1077. logical_state, phys_state);
  1078. smp->status |= IB_SMP_INVALID_FIELD;
  1079. }
  1080. /*
  1081. * Logical state changes are summarized in OPAv1g1 spec.,
  1082. * Table 9-12; physical state changes are summarized in
  1083. * OPAv1g1 spec., Table 6.4.
  1084. */
  1085. switch (logical_state) {
  1086. case IB_PORT_NOP:
  1087. if (phys_state == IB_PORTPHYSSTATE_NOP)
  1088. break;
  1089. /* FALLTHROUGH */
  1090. case IB_PORT_DOWN:
  1091. if (phys_state == IB_PORTPHYSSTATE_NOP) {
  1092. link_state = HLS_DN_DOWNDEF;
  1093. } else if (phys_state == IB_PORTPHYSSTATE_POLLING) {
  1094. link_state = HLS_DN_POLL;
  1095. set_link_down_reason(ppd, OPA_LINKDOWN_REASON_FM_BOUNCE,
  1096. 0, OPA_LINKDOWN_REASON_FM_BOUNCE);
  1097. } else if (phys_state == IB_PORTPHYSSTATE_DISABLED) {
  1098. link_state = HLS_DN_DISABLE;
  1099. } else {
  1100. pr_warn("SubnSet(OPA_PortInfo) invalid physical state 0x%x\n",
  1101. phys_state);
  1102. smp->status |= IB_SMP_INVALID_FIELD;
  1103. break;
  1104. }
  1105. if ((link_state == HLS_DN_POLL ||
  1106. link_state == HLS_DN_DOWNDEF)) {
  1107. /*
  1108. * Going to poll. No matter what the current state,
  1109. * always move offline first, then tune and start the
  1110. * link. This correctly handles a FM link bounce and
  1111. * a link enable. Going offline is a no-op if already
  1112. * offline.
  1113. */
  1114. set_link_state(ppd, HLS_DN_OFFLINE);
  1115. start_link(ppd);
  1116. } else {
  1117. set_link_state(ppd, link_state);
  1118. }
  1119. if (link_state == HLS_DN_DISABLE &&
  1120. (ppd->offline_disabled_reason >
  1121. HFI1_ODR_MASK(OPA_LINKDOWN_REASON_SMA_DISABLED) ||
  1122. ppd->offline_disabled_reason ==
  1123. HFI1_ODR_MASK(OPA_LINKDOWN_REASON_NONE)))
  1124. ppd->offline_disabled_reason =
  1125. HFI1_ODR_MASK(OPA_LINKDOWN_REASON_SMA_DISABLED);
  1126. /*
  1127. * Don't send a reply if the response would be sent
  1128. * through the disabled port.
  1129. */
  1130. if (link_state == HLS_DN_DISABLE && smp->hop_cnt)
  1131. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  1132. break;
  1133. case IB_PORT_ARMED:
  1134. ret = set_link_state(ppd, HLS_UP_ARMED);
  1135. if (!ret)
  1136. send_idle_sma(dd, SMA_IDLE_ARM);
  1137. break;
  1138. case IB_PORT_ACTIVE:
  1139. if (ppd->neighbor_normal) {
  1140. ret = set_link_state(ppd, HLS_UP_ACTIVE);
  1141. if (ret == 0)
  1142. send_idle_sma(dd, SMA_IDLE_ACTIVE);
  1143. } else {
  1144. pr_warn("SubnSet(OPA_PortInfo) Cannot move to Active with NeighborNormal 0\n");
  1145. smp->status |= IB_SMP_INVALID_FIELD;
  1146. }
  1147. break;
  1148. default:
  1149. pr_warn("SubnSet(OPA_PortInfo) invalid logical state 0x%x\n",
  1150. logical_state);
  1151. smp->status |= IB_SMP_INVALID_FIELD;
  1152. }
  1153. return 0;
  1154. }
  1155. /**
  1156. * subn_set_opa_portinfo - set port information
  1157. * @smp: the incoming SM packet
  1158. * @ibdev: the infiniband device
  1159. * @port: the port on the device
  1160. *
  1161. */
  1162. static int __subn_set_opa_portinfo(struct opa_smp *smp, u32 am, u8 *data,
  1163. struct ib_device *ibdev, u8 port,
  1164. u32 *resp_len, u32 max_len)
  1165. {
  1166. struct opa_port_info *pi = (struct opa_port_info *)data;
  1167. struct ib_event event;
  1168. struct hfi1_devdata *dd;
  1169. struct hfi1_pportdata *ppd;
  1170. struct hfi1_ibport *ibp;
  1171. u8 clientrereg;
  1172. unsigned long flags;
  1173. u32 smlid;
  1174. u32 lid;
  1175. u8 ls_old, ls_new, ps_new;
  1176. u8 vls;
  1177. u8 msl;
  1178. u8 crc_enabled;
  1179. u16 lse, lwe, mtu;
  1180. u32 num_ports = OPA_AM_NPORT(am);
  1181. u32 start_of_sm_config = OPA_AM_START_SM_CFG(am);
  1182. int ret, i, invalid = 0, call_set_mtu = 0;
  1183. int call_link_downgrade_policy = 0;
  1184. if (num_ports != 1 ||
  1185. smp_length_check(sizeof(*pi), max_len)) {
  1186. smp->status |= IB_SMP_INVALID_FIELD;
  1187. return reply((struct ib_mad_hdr *)smp);
  1188. }
  1189. lid = be32_to_cpu(pi->lid);
  1190. if (lid & 0xFF000000) {
  1191. pr_warn("OPA_PortInfo lid out of range: %X\n", lid);
  1192. smp->status |= IB_SMP_INVALID_FIELD;
  1193. goto get_only;
  1194. }
  1195. smlid = be32_to_cpu(pi->sm_lid);
  1196. if (smlid & 0xFF000000) {
  1197. pr_warn("OPA_PortInfo SM lid out of range: %X\n", smlid);
  1198. smp->status |= IB_SMP_INVALID_FIELD;
  1199. goto get_only;
  1200. }
  1201. clientrereg = (pi->clientrereg_subnettimeout &
  1202. OPA_PI_MASK_CLIENT_REREGISTER);
  1203. dd = dd_from_ibdev(ibdev);
  1204. /* IB numbers ports from 1, hw from 0 */
  1205. ppd = dd->pport + (port - 1);
  1206. ibp = &ppd->ibport_data;
  1207. event.device = ibdev;
  1208. event.element.port_num = port;
  1209. ls_old = driver_lstate(ppd);
  1210. ibp->rvp.mkey = pi->mkey;
  1211. if (ibp->rvp.gid_prefix != pi->subnet_prefix) {
  1212. ibp->rvp.gid_prefix = pi->subnet_prefix;
  1213. event.event = IB_EVENT_GID_CHANGE;
  1214. ib_dispatch_event(&event);
  1215. }
  1216. ibp->rvp.mkey_lease_period = be16_to_cpu(pi->mkey_lease_period);
  1217. /* Must be a valid unicast LID address. */
  1218. if ((lid == 0 && ls_old > IB_PORT_INIT) ||
  1219. (hfi1_is_16B_mcast(lid))) {
  1220. smp->status |= IB_SMP_INVALID_FIELD;
  1221. pr_warn("SubnSet(OPA_PortInfo) lid invalid 0x%x\n",
  1222. lid);
  1223. } else if (ppd->lid != lid ||
  1224. ppd->lmc != (pi->mkeyprotect_lmc & OPA_PI_MASK_LMC)) {
  1225. if (ppd->lid != lid)
  1226. hfi1_set_uevent_bits(ppd, _HFI1_EVENT_LID_CHANGE_BIT);
  1227. if (ppd->lmc != (pi->mkeyprotect_lmc & OPA_PI_MASK_LMC))
  1228. hfi1_set_uevent_bits(ppd, _HFI1_EVENT_LMC_CHANGE_BIT);
  1229. hfi1_set_lid(ppd, lid, pi->mkeyprotect_lmc & OPA_PI_MASK_LMC);
  1230. event.event = IB_EVENT_LID_CHANGE;
  1231. ib_dispatch_event(&event);
  1232. if (HFI1_PORT_GUID_INDEX + 1 < HFI1_GUIDS_PER_PORT) {
  1233. /* Manufacture GID from LID to support extended
  1234. * addresses
  1235. */
  1236. ppd->guids[HFI1_PORT_GUID_INDEX + 1] =
  1237. be64_to_cpu(OPA_MAKE_ID(lid));
  1238. event.event = IB_EVENT_GID_CHANGE;
  1239. ib_dispatch_event(&event);
  1240. }
  1241. }
  1242. msl = pi->smsl & OPA_PI_MASK_SMSL;
  1243. if (pi->partenforce_filterraw & OPA_PI_MASK_LINKINIT_REASON)
  1244. ppd->linkinit_reason =
  1245. (pi->partenforce_filterraw &
  1246. OPA_PI_MASK_LINKINIT_REASON);
  1247. /* Must be a valid unicast LID address. */
  1248. if ((smlid == 0 && ls_old > IB_PORT_INIT) ||
  1249. (hfi1_is_16B_mcast(smlid))) {
  1250. smp->status |= IB_SMP_INVALID_FIELD;
  1251. pr_warn("SubnSet(OPA_PortInfo) smlid invalid 0x%x\n", smlid);
  1252. } else if (smlid != ibp->rvp.sm_lid || msl != ibp->rvp.sm_sl) {
  1253. pr_warn("SubnSet(OPA_PortInfo) smlid 0x%x\n", smlid);
  1254. spin_lock_irqsave(&ibp->rvp.lock, flags);
  1255. if (ibp->rvp.sm_ah) {
  1256. if (smlid != ibp->rvp.sm_lid)
  1257. hfi1_modify_qp0_ah(ibp, ibp->rvp.sm_ah, smlid);
  1258. if (msl != ibp->rvp.sm_sl)
  1259. rdma_ah_set_sl(&ibp->rvp.sm_ah->attr, msl);
  1260. }
  1261. spin_unlock_irqrestore(&ibp->rvp.lock, flags);
  1262. if (smlid != ibp->rvp.sm_lid)
  1263. ibp->rvp.sm_lid = smlid;
  1264. if (msl != ibp->rvp.sm_sl)
  1265. ibp->rvp.sm_sl = msl;
  1266. event.event = IB_EVENT_SM_CHANGE;
  1267. ib_dispatch_event(&event);
  1268. }
  1269. if (pi->link_down_reason == 0) {
  1270. ppd->local_link_down_reason.sma = 0;
  1271. ppd->local_link_down_reason.latest = 0;
  1272. }
  1273. if (pi->neigh_link_down_reason == 0) {
  1274. ppd->neigh_link_down_reason.sma = 0;
  1275. ppd->neigh_link_down_reason.latest = 0;
  1276. }
  1277. ppd->sm_trap_qp = be32_to_cpu(pi->sm_trap_qp);
  1278. ppd->sa_qp = be32_to_cpu(pi->sa_qp);
  1279. ppd->port_error_action = be32_to_cpu(pi->port_error_action);
  1280. lwe = be16_to_cpu(pi->link_width.enabled);
  1281. if (lwe) {
  1282. if (lwe == OPA_LINK_WIDTH_RESET ||
  1283. lwe == OPA_LINK_WIDTH_RESET_OLD)
  1284. set_link_width_enabled(ppd, ppd->link_width_supported);
  1285. else if ((lwe & ~ppd->link_width_supported) == 0)
  1286. set_link_width_enabled(ppd, lwe);
  1287. else
  1288. smp->status |= IB_SMP_INVALID_FIELD;
  1289. }
  1290. lwe = be16_to_cpu(pi->link_width_downgrade.enabled);
  1291. /* LWD.E is always applied - 0 means "disabled" */
  1292. if (lwe == OPA_LINK_WIDTH_RESET ||
  1293. lwe == OPA_LINK_WIDTH_RESET_OLD) {
  1294. set_link_width_downgrade_enabled(ppd,
  1295. ppd->
  1296. link_width_downgrade_supported
  1297. );
  1298. } else if ((lwe & ~ppd->link_width_downgrade_supported) == 0) {
  1299. /* only set and apply if something changed */
  1300. if (lwe != ppd->link_width_downgrade_enabled) {
  1301. set_link_width_downgrade_enabled(ppd, lwe);
  1302. call_link_downgrade_policy = 1;
  1303. }
  1304. } else {
  1305. smp->status |= IB_SMP_INVALID_FIELD;
  1306. }
  1307. lse = be16_to_cpu(pi->link_speed.enabled);
  1308. if (lse) {
  1309. if (lse & be16_to_cpu(pi->link_speed.supported))
  1310. set_link_speed_enabled(ppd, lse);
  1311. else
  1312. smp->status |= IB_SMP_INVALID_FIELD;
  1313. }
  1314. ibp->rvp.mkeyprot =
  1315. (pi->mkeyprotect_lmc & OPA_PI_MASK_MKEY_PROT_BIT) >> 6;
  1316. ibp->rvp.vl_high_limit = be16_to_cpu(pi->vl.high_limit) & 0xFF;
  1317. (void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_VL_HIGH_LIMIT,
  1318. ibp->rvp.vl_high_limit);
  1319. if (ppd->vls_supported / 2 > ARRAY_SIZE(pi->neigh_mtu.pvlx_to_mtu) ||
  1320. ppd->vls_supported > ARRAY_SIZE(dd->vld)) {
  1321. smp->status |= IB_SMP_INVALID_FIELD;
  1322. return reply((struct ib_mad_hdr *)smp);
  1323. }
  1324. for (i = 0; i < ppd->vls_supported; i++) {
  1325. if ((i % 2) == 0)
  1326. mtu = enum_to_mtu((pi->neigh_mtu.pvlx_to_mtu[i / 2] >>
  1327. 4) & 0xF);
  1328. else
  1329. mtu = enum_to_mtu(pi->neigh_mtu.pvlx_to_mtu[i / 2] &
  1330. 0xF);
  1331. if (mtu == 0xffff) {
  1332. pr_warn("SubnSet(OPA_PortInfo) mtu invalid %d (0x%x)\n",
  1333. mtu,
  1334. (pi->neigh_mtu.pvlx_to_mtu[0] >> 4) & 0xF);
  1335. smp->status |= IB_SMP_INVALID_FIELD;
  1336. mtu = hfi1_max_mtu; /* use a valid MTU */
  1337. }
  1338. if (dd->vld[i].mtu != mtu) {
  1339. dd_dev_info(dd,
  1340. "MTU change on vl %d from %d to %d\n",
  1341. i, dd->vld[i].mtu, mtu);
  1342. dd->vld[i].mtu = mtu;
  1343. call_set_mtu++;
  1344. }
  1345. }
  1346. /* As per OPAV1 spec: VL15 must support and be configured
  1347. * for operation with a 2048 or larger MTU.
  1348. */
  1349. mtu = enum_to_mtu(pi->neigh_mtu.pvlx_to_mtu[15 / 2] & 0xF);
  1350. if (mtu < 2048 || mtu == 0xffff)
  1351. mtu = 2048;
  1352. if (dd->vld[15].mtu != mtu) {
  1353. dd_dev_info(dd,
  1354. "MTU change on vl 15 from %d to %d\n",
  1355. dd->vld[15].mtu, mtu);
  1356. dd->vld[15].mtu = mtu;
  1357. call_set_mtu++;
  1358. }
  1359. if (call_set_mtu)
  1360. set_mtu(ppd);
  1361. /* Set operational VLs */
  1362. vls = pi->operational_vls & OPA_PI_MASK_OPERATIONAL_VL;
  1363. if (vls) {
  1364. if (vls > ppd->vls_supported) {
  1365. pr_warn("SubnSet(OPA_PortInfo) VL's supported invalid %d\n",
  1366. pi->operational_vls);
  1367. smp->status |= IB_SMP_INVALID_FIELD;
  1368. } else {
  1369. if (hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_OP_VLS,
  1370. vls) == -EINVAL)
  1371. smp->status |= IB_SMP_INVALID_FIELD;
  1372. }
  1373. }
  1374. if (pi->mkey_violations == 0)
  1375. ibp->rvp.mkey_violations = 0;
  1376. if (pi->pkey_violations == 0)
  1377. ibp->rvp.pkey_violations = 0;
  1378. if (pi->qkey_violations == 0)
  1379. ibp->rvp.qkey_violations = 0;
  1380. ibp->rvp.subnet_timeout =
  1381. pi->clientrereg_subnettimeout & OPA_PI_MASK_SUBNET_TIMEOUT;
  1382. crc_enabled = be16_to_cpu(pi->port_ltp_crc_mode);
  1383. crc_enabled >>= 4;
  1384. crc_enabled &= 0xf;
  1385. if (crc_enabled != 0)
  1386. ppd->port_crc_mode_enabled = port_ltp_to_cap(crc_enabled);
  1387. ppd->is_active_optimize_enabled =
  1388. !!(be16_to_cpu(pi->port_mode)
  1389. & OPA_PI_MASK_PORT_ACTIVE_OPTOMIZE);
  1390. ls_new = pi->port_states.portphysstate_portstate &
  1391. OPA_PI_MASK_PORT_STATE;
  1392. ps_new = (pi->port_states.portphysstate_portstate &
  1393. OPA_PI_MASK_PORT_PHYSICAL_STATE) >> 4;
  1394. if (ls_old == IB_PORT_INIT) {
  1395. if (start_of_sm_config) {
  1396. if (ls_new == ls_old || (ls_new == IB_PORT_ARMED))
  1397. ppd->is_sm_config_started = 1;
  1398. } else if (ls_new == IB_PORT_ARMED) {
  1399. if (ppd->is_sm_config_started == 0) {
  1400. invalid = 1;
  1401. smp->status |= IB_SMP_INVALID_FIELD;
  1402. }
  1403. }
  1404. }
  1405. /* Handle CLIENT_REREGISTER event b/c SM asked us for it */
  1406. if (clientrereg) {
  1407. event.event = IB_EVENT_CLIENT_REREGISTER;
  1408. ib_dispatch_event(&event);
  1409. }
  1410. /*
  1411. * Do the port state change now that the other link parameters
  1412. * have been set.
  1413. * Changing the port physical state only makes sense if the link
  1414. * is down or is being set to down.
  1415. */
  1416. if (!invalid) {
  1417. ret = set_port_states(ppd, smp, ls_new, ps_new);
  1418. if (ret)
  1419. return ret;
  1420. }
  1421. ret = __subn_get_opa_portinfo(smp, am, data, ibdev, port, resp_len,
  1422. max_len);
  1423. /* restore re-reg bit per o14-12.2.1 */
  1424. pi->clientrereg_subnettimeout |= clientrereg;
  1425. /*
  1426. * Apply the new link downgrade policy. This may result in a link
  1427. * bounce. Do this after everything else so things are settled.
  1428. * Possible problem: if setting the port state above fails, then
  1429. * the policy change is not applied.
  1430. */
  1431. if (call_link_downgrade_policy)
  1432. apply_link_downgrade_policy(ppd, 0);
  1433. return ret;
  1434. get_only:
  1435. return __subn_get_opa_portinfo(smp, am, data, ibdev, port, resp_len,
  1436. max_len);
  1437. }
  1438. /**
  1439. * set_pkeys - set the PKEY table for ctxt 0
  1440. * @dd: the hfi1_ib device
  1441. * @port: the IB port number
  1442. * @pkeys: the PKEY table
  1443. */
  1444. static int set_pkeys(struct hfi1_devdata *dd, u8 port, u16 *pkeys)
  1445. {
  1446. struct hfi1_pportdata *ppd;
  1447. int i;
  1448. int changed = 0;
  1449. int update_includes_mgmt_partition = 0;
  1450. /*
  1451. * IB port one/two always maps to context zero/one,
  1452. * always a kernel context, no locking needed
  1453. * If we get here with ppd setup, no need to check
  1454. * that rcd is valid.
  1455. */
  1456. ppd = dd->pport + (port - 1);
  1457. /*
  1458. * If the update does not include the management pkey, don't do it.
  1459. */
  1460. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
  1461. if (pkeys[i] == LIM_MGMT_P_KEY) {
  1462. update_includes_mgmt_partition = 1;
  1463. break;
  1464. }
  1465. }
  1466. if (!update_includes_mgmt_partition)
  1467. return 1;
  1468. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
  1469. u16 key = pkeys[i];
  1470. u16 okey = ppd->pkeys[i];
  1471. if (key == okey)
  1472. continue;
  1473. /*
  1474. * The SM gives us the complete PKey table. We have
  1475. * to ensure that we put the PKeys in the matching
  1476. * slots.
  1477. */
  1478. ppd->pkeys[i] = key;
  1479. changed = 1;
  1480. }
  1481. if (changed) {
  1482. (void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_PKEYS, 0);
  1483. hfi1_event_pkey_change(dd, port);
  1484. }
  1485. return 0;
  1486. }
  1487. static int __subn_set_opa_pkeytable(struct opa_smp *smp, u32 am, u8 *data,
  1488. struct ib_device *ibdev, u8 port,
  1489. u32 *resp_len, u32 max_len)
  1490. {
  1491. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1492. u32 n_blocks_sent = OPA_AM_NBLK(am);
  1493. u32 start_block = am & 0x7ff;
  1494. u16 *p = (u16 *)data;
  1495. __be16 *q = (__be16 *)data;
  1496. int i;
  1497. u16 n_blocks_avail;
  1498. unsigned npkeys = hfi1_get_npkeys(dd);
  1499. u32 size = 0;
  1500. if (n_blocks_sent == 0) {
  1501. pr_warn("OPA Get PKey AM Invalid : P = %d; B = 0x%x; N = 0x%x\n",
  1502. port, start_block, n_blocks_sent);
  1503. smp->status |= IB_SMP_INVALID_FIELD;
  1504. return reply((struct ib_mad_hdr *)smp);
  1505. }
  1506. n_blocks_avail = (u16)(npkeys / OPA_PARTITION_TABLE_BLK_SIZE) + 1;
  1507. size = sizeof(u16) * (n_blocks_sent * OPA_PARTITION_TABLE_BLK_SIZE);
  1508. if (smp_length_check(size, max_len)) {
  1509. smp->status |= IB_SMP_INVALID_FIELD;
  1510. return reply((struct ib_mad_hdr *)smp);
  1511. }
  1512. if (start_block + n_blocks_sent > n_blocks_avail ||
  1513. n_blocks_sent > OPA_NUM_PKEY_BLOCKS_PER_SMP) {
  1514. pr_warn("OPA Set PKey AM Invalid : s 0x%x; req 0x%x; avail 0x%x; blk/smp 0x%lx\n",
  1515. start_block, n_blocks_sent, n_blocks_avail,
  1516. OPA_NUM_PKEY_BLOCKS_PER_SMP);
  1517. smp->status |= IB_SMP_INVALID_FIELD;
  1518. return reply((struct ib_mad_hdr *)smp);
  1519. }
  1520. for (i = 0; i < n_blocks_sent * OPA_PARTITION_TABLE_BLK_SIZE; i++)
  1521. p[i] = be16_to_cpu(q[i]);
  1522. if (start_block == 0 && set_pkeys(dd, port, p) != 0) {
  1523. smp->status |= IB_SMP_INVALID_FIELD;
  1524. return reply((struct ib_mad_hdr *)smp);
  1525. }
  1526. return __subn_get_opa_pkeytable(smp, am, data, ibdev, port, resp_len,
  1527. max_len);
  1528. }
  1529. #define ILLEGAL_VL 12
  1530. /*
  1531. * filter_sc2vlt changes mappings to VL15 to ILLEGAL_VL (except
  1532. * for SC15, which must map to VL15). If we don't remap things this
  1533. * way it is possible for VL15 counters to increment when we try to
  1534. * send on a SC which is mapped to an invalid VL.
  1535. * When getting the table convert ILLEGAL_VL back to VL15.
  1536. */
  1537. static void filter_sc2vlt(void *data, bool set)
  1538. {
  1539. int i;
  1540. u8 *pd = data;
  1541. for (i = 0; i < OPA_MAX_SCS; i++) {
  1542. if (i == 15)
  1543. continue;
  1544. if (set) {
  1545. if ((pd[i] & 0x1f) == 0xf)
  1546. pd[i] = ILLEGAL_VL;
  1547. } else {
  1548. if ((pd[i] & 0x1f) == ILLEGAL_VL)
  1549. pd[i] = 0xf;
  1550. }
  1551. }
  1552. }
  1553. static int set_sc2vlt_tables(struct hfi1_devdata *dd, void *data)
  1554. {
  1555. u64 *val = data;
  1556. filter_sc2vlt(data, true);
  1557. write_csr(dd, SEND_SC2VLT0, *val++);
  1558. write_csr(dd, SEND_SC2VLT1, *val++);
  1559. write_csr(dd, SEND_SC2VLT2, *val++);
  1560. write_csr(dd, SEND_SC2VLT3, *val++);
  1561. write_seqlock_irq(&dd->sc2vl_lock);
  1562. memcpy(dd->sc2vl, data, sizeof(dd->sc2vl));
  1563. write_sequnlock_irq(&dd->sc2vl_lock);
  1564. return 0;
  1565. }
  1566. static int get_sc2vlt_tables(struct hfi1_devdata *dd, void *data)
  1567. {
  1568. u64 *val = (u64 *)data;
  1569. *val++ = read_csr(dd, SEND_SC2VLT0);
  1570. *val++ = read_csr(dd, SEND_SC2VLT1);
  1571. *val++ = read_csr(dd, SEND_SC2VLT2);
  1572. *val++ = read_csr(dd, SEND_SC2VLT3);
  1573. filter_sc2vlt((u64 *)data, false);
  1574. return 0;
  1575. }
  1576. static int __subn_get_opa_sl_to_sc(struct opa_smp *smp, u32 am, u8 *data,
  1577. struct ib_device *ibdev, u8 port,
  1578. u32 *resp_len, u32 max_len)
  1579. {
  1580. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  1581. u8 *p = data;
  1582. size_t size = ARRAY_SIZE(ibp->sl_to_sc); /* == 32 */
  1583. unsigned i;
  1584. if (am || smp_length_check(size, max_len)) {
  1585. smp->status |= IB_SMP_INVALID_FIELD;
  1586. return reply((struct ib_mad_hdr *)smp);
  1587. }
  1588. for (i = 0; i < ARRAY_SIZE(ibp->sl_to_sc); i++)
  1589. *p++ = ibp->sl_to_sc[i];
  1590. if (resp_len)
  1591. *resp_len += size;
  1592. return reply((struct ib_mad_hdr *)smp);
  1593. }
  1594. static int __subn_set_opa_sl_to_sc(struct opa_smp *smp, u32 am, u8 *data,
  1595. struct ib_device *ibdev, u8 port,
  1596. u32 *resp_len, u32 max_len)
  1597. {
  1598. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  1599. u8 *p = data;
  1600. size_t size = ARRAY_SIZE(ibp->sl_to_sc);
  1601. int i;
  1602. u8 sc;
  1603. if (am || smp_length_check(size, max_len)) {
  1604. smp->status |= IB_SMP_INVALID_FIELD;
  1605. return reply((struct ib_mad_hdr *)smp);
  1606. }
  1607. for (i = 0; i < ARRAY_SIZE(ibp->sl_to_sc); i++) {
  1608. sc = *p++;
  1609. if (ibp->sl_to_sc[i] != sc) {
  1610. ibp->sl_to_sc[i] = sc;
  1611. /* Put all stale qps into error state */
  1612. hfi1_error_port_qps(ibp, i);
  1613. }
  1614. }
  1615. return __subn_get_opa_sl_to_sc(smp, am, data, ibdev, port, resp_len,
  1616. max_len);
  1617. }
  1618. static int __subn_get_opa_sc_to_sl(struct opa_smp *smp, u32 am, u8 *data,
  1619. struct ib_device *ibdev, u8 port,
  1620. u32 *resp_len, u32 max_len)
  1621. {
  1622. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  1623. u8 *p = data;
  1624. size_t size = ARRAY_SIZE(ibp->sc_to_sl); /* == 32 */
  1625. unsigned i;
  1626. if (am || smp_length_check(size, max_len)) {
  1627. smp->status |= IB_SMP_INVALID_FIELD;
  1628. return reply((struct ib_mad_hdr *)smp);
  1629. }
  1630. for (i = 0; i < ARRAY_SIZE(ibp->sc_to_sl); i++)
  1631. *p++ = ibp->sc_to_sl[i];
  1632. if (resp_len)
  1633. *resp_len += size;
  1634. return reply((struct ib_mad_hdr *)smp);
  1635. }
  1636. static int __subn_set_opa_sc_to_sl(struct opa_smp *smp, u32 am, u8 *data,
  1637. struct ib_device *ibdev, u8 port,
  1638. u32 *resp_len, u32 max_len)
  1639. {
  1640. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  1641. size_t size = ARRAY_SIZE(ibp->sc_to_sl);
  1642. u8 *p = data;
  1643. int i;
  1644. if (am || smp_length_check(size, max_len)) {
  1645. smp->status |= IB_SMP_INVALID_FIELD;
  1646. return reply((struct ib_mad_hdr *)smp);
  1647. }
  1648. for (i = 0; i < ARRAY_SIZE(ibp->sc_to_sl); i++)
  1649. ibp->sc_to_sl[i] = *p++;
  1650. return __subn_get_opa_sc_to_sl(smp, am, data, ibdev, port, resp_len,
  1651. max_len);
  1652. }
  1653. static int __subn_get_opa_sc_to_vlt(struct opa_smp *smp, u32 am, u8 *data,
  1654. struct ib_device *ibdev, u8 port,
  1655. u32 *resp_len, u32 max_len)
  1656. {
  1657. u32 n_blocks = OPA_AM_NBLK(am);
  1658. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1659. void *vp = (void *)data;
  1660. size_t size = 4 * sizeof(u64);
  1661. if (n_blocks != 1 || smp_length_check(size, max_len)) {
  1662. smp->status |= IB_SMP_INVALID_FIELD;
  1663. return reply((struct ib_mad_hdr *)smp);
  1664. }
  1665. get_sc2vlt_tables(dd, vp);
  1666. if (resp_len)
  1667. *resp_len += size;
  1668. return reply((struct ib_mad_hdr *)smp);
  1669. }
  1670. static int __subn_set_opa_sc_to_vlt(struct opa_smp *smp, u32 am, u8 *data,
  1671. struct ib_device *ibdev, u8 port,
  1672. u32 *resp_len, u32 max_len)
  1673. {
  1674. u32 n_blocks = OPA_AM_NBLK(am);
  1675. int async_update = OPA_AM_ASYNC(am);
  1676. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1677. void *vp = (void *)data;
  1678. struct hfi1_pportdata *ppd;
  1679. int lstate;
  1680. /*
  1681. * set_sc2vlt_tables writes the information contained in *data
  1682. * to four 64-bit registers SendSC2VLt[0-3]. We need to make
  1683. * sure *max_len is not greater than the total size of the four
  1684. * SendSC2VLt[0-3] registers.
  1685. */
  1686. size_t size = 4 * sizeof(u64);
  1687. if (n_blocks != 1 || async_update || smp_length_check(size, max_len)) {
  1688. smp->status |= IB_SMP_INVALID_FIELD;
  1689. return reply((struct ib_mad_hdr *)smp);
  1690. }
  1691. /* IB numbers ports from 1, hw from 0 */
  1692. ppd = dd->pport + (port - 1);
  1693. lstate = driver_lstate(ppd);
  1694. /*
  1695. * it's known that async_update is 0 by this point, but include
  1696. * the explicit check for clarity
  1697. */
  1698. if (!async_update &&
  1699. (lstate == IB_PORT_ARMED || lstate == IB_PORT_ACTIVE)) {
  1700. smp->status |= IB_SMP_INVALID_FIELD;
  1701. return reply((struct ib_mad_hdr *)smp);
  1702. }
  1703. set_sc2vlt_tables(dd, vp);
  1704. return __subn_get_opa_sc_to_vlt(smp, am, data, ibdev, port, resp_len,
  1705. max_len);
  1706. }
  1707. static int __subn_get_opa_sc_to_vlnt(struct opa_smp *smp, u32 am, u8 *data,
  1708. struct ib_device *ibdev, u8 port,
  1709. u32 *resp_len, u32 max_len)
  1710. {
  1711. u32 n_blocks = OPA_AM_NPORT(am);
  1712. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1713. struct hfi1_pportdata *ppd;
  1714. void *vp = (void *)data;
  1715. int size = sizeof(struct sc2vlnt);
  1716. if (n_blocks != 1 || smp_length_check(size, max_len)) {
  1717. smp->status |= IB_SMP_INVALID_FIELD;
  1718. return reply((struct ib_mad_hdr *)smp);
  1719. }
  1720. ppd = dd->pport + (port - 1);
  1721. fm_get_table(ppd, FM_TBL_SC2VLNT, vp);
  1722. if (resp_len)
  1723. *resp_len += size;
  1724. return reply((struct ib_mad_hdr *)smp);
  1725. }
  1726. static int __subn_set_opa_sc_to_vlnt(struct opa_smp *smp, u32 am, u8 *data,
  1727. struct ib_device *ibdev, u8 port,
  1728. u32 *resp_len, u32 max_len)
  1729. {
  1730. u32 n_blocks = OPA_AM_NPORT(am);
  1731. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1732. struct hfi1_pportdata *ppd;
  1733. void *vp = (void *)data;
  1734. int lstate;
  1735. int size = sizeof(struct sc2vlnt);
  1736. if (n_blocks != 1 || smp_length_check(size, max_len)) {
  1737. smp->status |= IB_SMP_INVALID_FIELD;
  1738. return reply((struct ib_mad_hdr *)smp);
  1739. }
  1740. /* IB numbers ports from 1, hw from 0 */
  1741. ppd = dd->pport + (port - 1);
  1742. lstate = driver_lstate(ppd);
  1743. if (lstate == IB_PORT_ARMED || lstate == IB_PORT_ACTIVE) {
  1744. smp->status |= IB_SMP_INVALID_FIELD;
  1745. return reply((struct ib_mad_hdr *)smp);
  1746. }
  1747. ppd = dd->pport + (port - 1);
  1748. fm_set_table(ppd, FM_TBL_SC2VLNT, vp);
  1749. return __subn_get_opa_sc_to_vlnt(smp, am, data, ibdev, port,
  1750. resp_len, max_len);
  1751. }
  1752. static int __subn_get_opa_psi(struct opa_smp *smp, u32 am, u8 *data,
  1753. struct ib_device *ibdev, u8 port,
  1754. u32 *resp_len, u32 max_len)
  1755. {
  1756. u32 nports = OPA_AM_NPORT(am);
  1757. u32 start_of_sm_config = OPA_AM_START_SM_CFG(am);
  1758. u32 lstate;
  1759. struct hfi1_ibport *ibp;
  1760. struct hfi1_pportdata *ppd;
  1761. struct opa_port_state_info *psi = (struct opa_port_state_info *)data;
  1762. if (nports != 1 || smp_length_check(sizeof(*psi), max_len)) {
  1763. smp->status |= IB_SMP_INVALID_FIELD;
  1764. return reply((struct ib_mad_hdr *)smp);
  1765. }
  1766. ibp = to_iport(ibdev, port);
  1767. ppd = ppd_from_ibp(ibp);
  1768. lstate = driver_lstate(ppd);
  1769. if (start_of_sm_config && (lstate == IB_PORT_INIT))
  1770. ppd->is_sm_config_started = 1;
  1771. psi->port_states.ledenable_offlinereason = ppd->neighbor_normal << 4;
  1772. psi->port_states.ledenable_offlinereason |=
  1773. ppd->is_sm_config_started << 5;
  1774. psi->port_states.ledenable_offlinereason |=
  1775. ppd->offline_disabled_reason;
  1776. psi->port_states.portphysstate_portstate =
  1777. (driver_pstate(ppd) << 4) | (lstate & 0xf);
  1778. psi->link_width_downgrade_tx_active =
  1779. cpu_to_be16(ppd->link_width_downgrade_tx_active);
  1780. psi->link_width_downgrade_rx_active =
  1781. cpu_to_be16(ppd->link_width_downgrade_rx_active);
  1782. if (resp_len)
  1783. *resp_len += sizeof(struct opa_port_state_info);
  1784. return reply((struct ib_mad_hdr *)smp);
  1785. }
  1786. static int __subn_set_opa_psi(struct opa_smp *smp, u32 am, u8 *data,
  1787. struct ib_device *ibdev, u8 port,
  1788. u32 *resp_len, u32 max_len)
  1789. {
  1790. u32 nports = OPA_AM_NPORT(am);
  1791. u32 start_of_sm_config = OPA_AM_START_SM_CFG(am);
  1792. u32 ls_old;
  1793. u8 ls_new, ps_new;
  1794. struct hfi1_ibport *ibp;
  1795. struct hfi1_pportdata *ppd;
  1796. struct opa_port_state_info *psi = (struct opa_port_state_info *)data;
  1797. int ret, invalid = 0;
  1798. if (nports != 1 || smp_length_check(sizeof(*psi), max_len)) {
  1799. smp->status |= IB_SMP_INVALID_FIELD;
  1800. return reply((struct ib_mad_hdr *)smp);
  1801. }
  1802. ibp = to_iport(ibdev, port);
  1803. ppd = ppd_from_ibp(ibp);
  1804. ls_old = driver_lstate(ppd);
  1805. ls_new = port_states_to_logical_state(&psi->port_states);
  1806. ps_new = port_states_to_phys_state(&psi->port_states);
  1807. if (ls_old == IB_PORT_INIT) {
  1808. if (start_of_sm_config) {
  1809. if (ls_new == ls_old || (ls_new == IB_PORT_ARMED))
  1810. ppd->is_sm_config_started = 1;
  1811. } else if (ls_new == IB_PORT_ARMED) {
  1812. if (ppd->is_sm_config_started == 0) {
  1813. invalid = 1;
  1814. smp->status |= IB_SMP_INVALID_FIELD;
  1815. }
  1816. }
  1817. }
  1818. if (!invalid) {
  1819. ret = set_port_states(ppd, smp, ls_new, ps_new);
  1820. if (ret)
  1821. return ret;
  1822. }
  1823. return __subn_get_opa_psi(smp, am, data, ibdev, port, resp_len,
  1824. max_len);
  1825. }
  1826. static int __subn_get_opa_cable_info(struct opa_smp *smp, u32 am, u8 *data,
  1827. struct ib_device *ibdev, u8 port,
  1828. u32 *resp_len, u32 max_len)
  1829. {
  1830. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1831. u32 addr = OPA_AM_CI_ADDR(am);
  1832. u32 len = OPA_AM_CI_LEN(am) + 1;
  1833. int ret;
  1834. if (dd->pport->port_type != PORT_TYPE_QSFP ||
  1835. smp_length_check(len, max_len)) {
  1836. smp->status |= IB_SMP_INVALID_FIELD;
  1837. return reply((struct ib_mad_hdr *)smp);
  1838. }
  1839. #define __CI_PAGE_SIZE BIT(7) /* 128 bytes */
  1840. #define __CI_PAGE_MASK ~(__CI_PAGE_SIZE - 1)
  1841. #define __CI_PAGE_NUM(a) ((a) & __CI_PAGE_MASK)
  1842. /*
  1843. * check that addr is within spec, and
  1844. * addr and (addr + len - 1) are on the same "page"
  1845. */
  1846. if (addr >= 4096 ||
  1847. (__CI_PAGE_NUM(addr) != __CI_PAGE_NUM(addr + len - 1))) {
  1848. smp->status |= IB_SMP_INVALID_FIELD;
  1849. return reply((struct ib_mad_hdr *)smp);
  1850. }
  1851. ret = get_cable_info(dd, port, addr, len, data);
  1852. if (ret == -ENODEV) {
  1853. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  1854. return reply((struct ib_mad_hdr *)smp);
  1855. }
  1856. /* The address range for the CableInfo SMA query is wider than the
  1857. * memory available on the QSFP cable. We want to return a valid
  1858. * response, albeit zeroed out, for address ranges beyond available
  1859. * memory but that are within the CableInfo query spec
  1860. */
  1861. if (ret < 0 && ret != -ERANGE) {
  1862. smp->status |= IB_SMP_INVALID_FIELD;
  1863. return reply((struct ib_mad_hdr *)smp);
  1864. }
  1865. if (resp_len)
  1866. *resp_len += len;
  1867. return reply((struct ib_mad_hdr *)smp);
  1868. }
  1869. static int __subn_get_opa_bct(struct opa_smp *smp, u32 am, u8 *data,
  1870. struct ib_device *ibdev, u8 port, u32 *resp_len,
  1871. u32 max_len)
  1872. {
  1873. u32 num_ports = OPA_AM_NPORT(am);
  1874. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1875. struct hfi1_pportdata *ppd;
  1876. struct buffer_control *p = (struct buffer_control *)data;
  1877. int size = sizeof(struct buffer_control);
  1878. if (num_ports != 1 || smp_length_check(size, max_len)) {
  1879. smp->status |= IB_SMP_INVALID_FIELD;
  1880. return reply((struct ib_mad_hdr *)smp);
  1881. }
  1882. ppd = dd->pport + (port - 1);
  1883. fm_get_table(ppd, FM_TBL_BUFFER_CONTROL, p);
  1884. trace_bct_get(dd, p);
  1885. if (resp_len)
  1886. *resp_len += size;
  1887. return reply((struct ib_mad_hdr *)smp);
  1888. }
  1889. static int __subn_set_opa_bct(struct opa_smp *smp, u32 am, u8 *data,
  1890. struct ib_device *ibdev, u8 port, u32 *resp_len,
  1891. u32 max_len)
  1892. {
  1893. u32 num_ports = OPA_AM_NPORT(am);
  1894. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1895. struct hfi1_pportdata *ppd;
  1896. struct buffer_control *p = (struct buffer_control *)data;
  1897. if (num_ports != 1 || smp_length_check(sizeof(*p), max_len)) {
  1898. smp->status |= IB_SMP_INVALID_FIELD;
  1899. return reply((struct ib_mad_hdr *)smp);
  1900. }
  1901. ppd = dd->pport + (port - 1);
  1902. trace_bct_set(dd, p);
  1903. if (fm_set_table(ppd, FM_TBL_BUFFER_CONTROL, p) < 0) {
  1904. smp->status |= IB_SMP_INVALID_FIELD;
  1905. return reply((struct ib_mad_hdr *)smp);
  1906. }
  1907. return __subn_get_opa_bct(smp, am, data, ibdev, port, resp_len,
  1908. max_len);
  1909. }
  1910. static int __subn_get_opa_vl_arb(struct opa_smp *smp, u32 am, u8 *data,
  1911. struct ib_device *ibdev, u8 port,
  1912. u32 *resp_len, u32 max_len)
  1913. {
  1914. struct hfi1_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
  1915. u32 num_ports = OPA_AM_NPORT(am);
  1916. u8 section = (am & 0x00ff0000) >> 16;
  1917. u8 *p = data;
  1918. int size = 256;
  1919. if (num_ports != 1 || smp_length_check(size, max_len)) {
  1920. smp->status |= IB_SMP_INVALID_FIELD;
  1921. return reply((struct ib_mad_hdr *)smp);
  1922. }
  1923. switch (section) {
  1924. case OPA_VLARB_LOW_ELEMENTS:
  1925. fm_get_table(ppd, FM_TBL_VL_LOW_ARB, p);
  1926. break;
  1927. case OPA_VLARB_HIGH_ELEMENTS:
  1928. fm_get_table(ppd, FM_TBL_VL_HIGH_ARB, p);
  1929. break;
  1930. case OPA_VLARB_PREEMPT_ELEMENTS:
  1931. fm_get_table(ppd, FM_TBL_VL_PREEMPT_ELEMS, p);
  1932. break;
  1933. case OPA_VLARB_PREEMPT_MATRIX:
  1934. fm_get_table(ppd, FM_TBL_VL_PREEMPT_MATRIX, p);
  1935. break;
  1936. default:
  1937. pr_warn("OPA SubnGet(VL Arb) AM Invalid : 0x%x\n",
  1938. be32_to_cpu(smp->attr_mod));
  1939. smp->status |= IB_SMP_INVALID_FIELD;
  1940. size = 0;
  1941. break;
  1942. }
  1943. if (size > 0 && resp_len)
  1944. *resp_len += size;
  1945. return reply((struct ib_mad_hdr *)smp);
  1946. }
  1947. static int __subn_set_opa_vl_arb(struct opa_smp *smp, u32 am, u8 *data,
  1948. struct ib_device *ibdev, u8 port,
  1949. u32 *resp_len, u32 max_len)
  1950. {
  1951. struct hfi1_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
  1952. u32 num_ports = OPA_AM_NPORT(am);
  1953. u8 section = (am & 0x00ff0000) >> 16;
  1954. u8 *p = data;
  1955. int size = 256;
  1956. if (num_ports != 1 || smp_length_check(size, max_len)) {
  1957. smp->status |= IB_SMP_INVALID_FIELD;
  1958. return reply((struct ib_mad_hdr *)smp);
  1959. }
  1960. switch (section) {
  1961. case OPA_VLARB_LOW_ELEMENTS:
  1962. (void)fm_set_table(ppd, FM_TBL_VL_LOW_ARB, p);
  1963. break;
  1964. case OPA_VLARB_HIGH_ELEMENTS:
  1965. (void)fm_set_table(ppd, FM_TBL_VL_HIGH_ARB, p);
  1966. break;
  1967. /*
  1968. * neither OPA_VLARB_PREEMPT_ELEMENTS, or OPA_VLARB_PREEMPT_MATRIX
  1969. * can be changed from the default values
  1970. */
  1971. case OPA_VLARB_PREEMPT_ELEMENTS:
  1972. /* FALLTHROUGH */
  1973. case OPA_VLARB_PREEMPT_MATRIX:
  1974. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  1975. break;
  1976. default:
  1977. pr_warn("OPA SubnSet(VL Arb) AM Invalid : 0x%x\n",
  1978. be32_to_cpu(smp->attr_mod));
  1979. smp->status |= IB_SMP_INVALID_FIELD;
  1980. break;
  1981. }
  1982. return __subn_get_opa_vl_arb(smp, am, data, ibdev, port, resp_len,
  1983. max_len);
  1984. }
  1985. struct opa_pma_mad {
  1986. struct ib_mad_hdr mad_hdr;
  1987. u8 data[2024];
  1988. } __packed;
  1989. struct opa_port_status_req {
  1990. __u8 port_num;
  1991. __u8 reserved[3];
  1992. __be32 vl_select_mask;
  1993. };
  1994. #define VL_MASK_ALL 0x000080ff
  1995. struct opa_port_status_rsp {
  1996. __u8 port_num;
  1997. __u8 reserved[3];
  1998. __be32 vl_select_mask;
  1999. /* Data counters */
  2000. __be64 port_xmit_data;
  2001. __be64 port_rcv_data;
  2002. __be64 port_xmit_pkts;
  2003. __be64 port_rcv_pkts;
  2004. __be64 port_multicast_xmit_pkts;
  2005. __be64 port_multicast_rcv_pkts;
  2006. __be64 port_xmit_wait;
  2007. __be64 sw_port_congestion;
  2008. __be64 port_rcv_fecn;
  2009. __be64 port_rcv_becn;
  2010. __be64 port_xmit_time_cong;
  2011. __be64 port_xmit_wasted_bw;
  2012. __be64 port_xmit_wait_data;
  2013. __be64 port_rcv_bubble;
  2014. __be64 port_mark_fecn;
  2015. /* Error counters */
  2016. __be64 port_rcv_constraint_errors;
  2017. __be64 port_rcv_switch_relay_errors;
  2018. __be64 port_xmit_discards;
  2019. __be64 port_xmit_constraint_errors;
  2020. __be64 port_rcv_remote_physical_errors;
  2021. __be64 local_link_integrity_errors;
  2022. __be64 port_rcv_errors;
  2023. __be64 excessive_buffer_overruns;
  2024. __be64 fm_config_errors;
  2025. __be32 link_error_recovery;
  2026. __be32 link_downed;
  2027. u8 uncorrectable_errors;
  2028. u8 link_quality_indicator; /* 5res, 3bit */
  2029. u8 res2[6];
  2030. struct _vls_pctrs {
  2031. /* per-VL Data counters */
  2032. __be64 port_vl_xmit_data;
  2033. __be64 port_vl_rcv_data;
  2034. __be64 port_vl_xmit_pkts;
  2035. __be64 port_vl_rcv_pkts;
  2036. __be64 port_vl_xmit_wait;
  2037. __be64 sw_port_vl_congestion;
  2038. __be64 port_vl_rcv_fecn;
  2039. __be64 port_vl_rcv_becn;
  2040. __be64 port_xmit_time_cong;
  2041. __be64 port_vl_xmit_wasted_bw;
  2042. __be64 port_vl_xmit_wait_data;
  2043. __be64 port_vl_rcv_bubble;
  2044. __be64 port_vl_mark_fecn;
  2045. __be64 port_vl_xmit_discards;
  2046. } vls[0]; /* real array size defined by # bits set in vl_select_mask */
  2047. };
  2048. enum counter_selects {
  2049. CS_PORT_XMIT_DATA = (1 << 31),
  2050. CS_PORT_RCV_DATA = (1 << 30),
  2051. CS_PORT_XMIT_PKTS = (1 << 29),
  2052. CS_PORT_RCV_PKTS = (1 << 28),
  2053. CS_PORT_MCAST_XMIT_PKTS = (1 << 27),
  2054. CS_PORT_MCAST_RCV_PKTS = (1 << 26),
  2055. CS_PORT_XMIT_WAIT = (1 << 25),
  2056. CS_SW_PORT_CONGESTION = (1 << 24),
  2057. CS_PORT_RCV_FECN = (1 << 23),
  2058. CS_PORT_RCV_BECN = (1 << 22),
  2059. CS_PORT_XMIT_TIME_CONG = (1 << 21),
  2060. CS_PORT_XMIT_WASTED_BW = (1 << 20),
  2061. CS_PORT_XMIT_WAIT_DATA = (1 << 19),
  2062. CS_PORT_RCV_BUBBLE = (1 << 18),
  2063. CS_PORT_MARK_FECN = (1 << 17),
  2064. CS_PORT_RCV_CONSTRAINT_ERRORS = (1 << 16),
  2065. CS_PORT_RCV_SWITCH_RELAY_ERRORS = (1 << 15),
  2066. CS_PORT_XMIT_DISCARDS = (1 << 14),
  2067. CS_PORT_XMIT_CONSTRAINT_ERRORS = (1 << 13),
  2068. CS_PORT_RCV_REMOTE_PHYSICAL_ERRORS = (1 << 12),
  2069. CS_LOCAL_LINK_INTEGRITY_ERRORS = (1 << 11),
  2070. CS_PORT_RCV_ERRORS = (1 << 10),
  2071. CS_EXCESSIVE_BUFFER_OVERRUNS = (1 << 9),
  2072. CS_FM_CONFIG_ERRORS = (1 << 8),
  2073. CS_LINK_ERROR_RECOVERY = (1 << 7),
  2074. CS_LINK_DOWNED = (1 << 6),
  2075. CS_UNCORRECTABLE_ERRORS = (1 << 5),
  2076. };
  2077. struct opa_clear_port_status {
  2078. __be64 port_select_mask[4];
  2079. __be32 counter_select_mask;
  2080. };
  2081. struct opa_aggregate {
  2082. __be16 attr_id;
  2083. __be16 err_reqlength; /* 1 bit, 8 res, 7 bit */
  2084. __be32 attr_mod;
  2085. u8 data[0];
  2086. };
  2087. #define MSK_LLI 0x000000f0
  2088. #define MSK_LLI_SFT 4
  2089. #define MSK_LER 0x0000000f
  2090. #define MSK_LER_SFT 0
  2091. #define ADD_LLI 8
  2092. #define ADD_LER 2
  2093. /* Request contains first three fields, response contains those plus the rest */
  2094. struct opa_port_data_counters_msg {
  2095. __be64 port_select_mask[4];
  2096. __be32 vl_select_mask;
  2097. __be32 resolution;
  2098. /* Response fields follow */
  2099. struct _port_dctrs {
  2100. u8 port_number;
  2101. u8 reserved2[3];
  2102. __be32 link_quality_indicator; /* 29res, 3bit */
  2103. /* Data counters */
  2104. __be64 port_xmit_data;
  2105. __be64 port_rcv_data;
  2106. __be64 port_xmit_pkts;
  2107. __be64 port_rcv_pkts;
  2108. __be64 port_multicast_xmit_pkts;
  2109. __be64 port_multicast_rcv_pkts;
  2110. __be64 port_xmit_wait;
  2111. __be64 sw_port_congestion;
  2112. __be64 port_rcv_fecn;
  2113. __be64 port_rcv_becn;
  2114. __be64 port_xmit_time_cong;
  2115. __be64 port_xmit_wasted_bw;
  2116. __be64 port_xmit_wait_data;
  2117. __be64 port_rcv_bubble;
  2118. __be64 port_mark_fecn;
  2119. __be64 port_error_counter_summary;
  2120. /* Sum of error counts/port */
  2121. struct _vls_dctrs {
  2122. /* per-VL Data counters */
  2123. __be64 port_vl_xmit_data;
  2124. __be64 port_vl_rcv_data;
  2125. __be64 port_vl_xmit_pkts;
  2126. __be64 port_vl_rcv_pkts;
  2127. __be64 port_vl_xmit_wait;
  2128. __be64 sw_port_vl_congestion;
  2129. __be64 port_vl_rcv_fecn;
  2130. __be64 port_vl_rcv_becn;
  2131. __be64 port_xmit_time_cong;
  2132. __be64 port_vl_xmit_wasted_bw;
  2133. __be64 port_vl_xmit_wait_data;
  2134. __be64 port_vl_rcv_bubble;
  2135. __be64 port_vl_mark_fecn;
  2136. } vls[0];
  2137. /* array size defined by #bits set in vl_select_mask*/
  2138. } port[1]; /* array size defined by #ports in attribute modifier */
  2139. };
  2140. struct opa_port_error_counters64_msg {
  2141. /*
  2142. * Request contains first two fields, response contains the
  2143. * whole magilla
  2144. */
  2145. __be64 port_select_mask[4];
  2146. __be32 vl_select_mask;
  2147. /* Response-only fields follow */
  2148. __be32 reserved1;
  2149. struct _port_ectrs {
  2150. u8 port_number;
  2151. u8 reserved2[7];
  2152. __be64 port_rcv_constraint_errors;
  2153. __be64 port_rcv_switch_relay_errors;
  2154. __be64 port_xmit_discards;
  2155. __be64 port_xmit_constraint_errors;
  2156. __be64 port_rcv_remote_physical_errors;
  2157. __be64 local_link_integrity_errors;
  2158. __be64 port_rcv_errors;
  2159. __be64 excessive_buffer_overruns;
  2160. __be64 fm_config_errors;
  2161. __be32 link_error_recovery;
  2162. __be32 link_downed;
  2163. u8 uncorrectable_errors;
  2164. u8 reserved3[7];
  2165. struct _vls_ectrs {
  2166. __be64 port_vl_xmit_discards;
  2167. } vls[0];
  2168. /* array size defined by #bits set in vl_select_mask */
  2169. } port[1]; /* array size defined by #ports in attribute modifier */
  2170. };
  2171. struct opa_port_error_info_msg {
  2172. __be64 port_select_mask[4];
  2173. __be32 error_info_select_mask;
  2174. __be32 reserved1;
  2175. struct _port_ei {
  2176. u8 port_number;
  2177. u8 reserved2[7];
  2178. /* PortRcvErrorInfo */
  2179. struct {
  2180. u8 status_and_code;
  2181. union {
  2182. u8 raw[17];
  2183. struct {
  2184. /* EI1to12 format */
  2185. u8 packet_flit1[8];
  2186. u8 packet_flit2[8];
  2187. u8 remaining_flit_bits12;
  2188. } ei1to12;
  2189. struct {
  2190. u8 packet_bytes[8];
  2191. u8 remaining_flit_bits;
  2192. } ei13;
  2193. } ei;
  2194. u8 reserved3[6];
  2195. } __packed port_rcv_ei;
  2196. /* ExcessiveBufferOverrunInfo */
  2197. struct {
  2198. u8 status_and_sc;
  2199. u8 reserved4[7];
  2200. } __packed excessive_buffer_overrun_ei;
  2201. /* PortXmitConstraintErrorInfo */
  2202. struct {
  2203. u8 status;
  2204. u8 reserved5;
  2205. __be16 pkey;
  2206. __be32 slid;
  2207. } __packed port_xmit_constraint_ei;
  2208. /* PortRcvConstraintErrorInfo */
  2209. struct {
  2210. u8 status;
  2211. u8 reserved6;
  2212. __be16 pkey;
  2213. __be32 slid;
  2214. } __packed port_rcv_constraint_ei;
  2215. /* PortRcvSwitchRelayErrorInfo */
  2216. struct {
  2217. u8 status_and_code;
  2218. u8 reserved7[3];
  2219. __u32 error_info;
  2220. } __packed port_rcv_switch_relay_ei;
  2221. /* UncorrectableErrorInfo */
  2222. struct {
  2223. u8 status_and_code;
  2224. u8 reserved8;
  2225. } __packed uncorrectable_ei;
  2226. /* FMConfigErrorInfo */
  2227. struct {
  2228. u8 status_and_code;
  2229. u8 error_info;
  2230. } __packed fm_config_ei;
  2231. __u32 reserved9;
  2232. } port[1]; /* actual array size defined by #ports in attr modifier */
  2233. };
  2234. /* opa_port_error_info_msg error_info_select_mask bit definitions */
  2235. enum error_info_selects {
  2236. ES_PORT_RCV_ERROR_INFO = (1 << 31),
  2237. ES_EXCESSIVE_BUFFER_OVERRUN_INFO = (1 << 30),
  2238. ES_PORT_XMIT_CONSTRAINT_ERROR_INFO = (1 << 29),
  2239. ES_PORT_RCV_CONSTRAINT_ERROR_INFO = (1 << 28),
  2240. ES_PORT_RCV_SWITCH_RELAY_ERROR_INFO = (1 << 27),
  2241. ES_UNCORRECTABLE_ERROR_INFO = (1 << 26),
  2242. ES_FM_CONFIG_ERROR_INFO = (1 << 25)
  2243. };
  2244. static int pma_get_opa_classportinfo(struct opa_pma_mad *pmp,
  2245. struct ib_device *ibdev, u32 *resp_len)
  2246. {
  2247. struct opa_class_port_info *p =
  2248. (struct opa_class_port_info *)pmp->data;
  2249. memset(pmp->data, 0, sizeof(pmp->data));
  2250. if (pmp->mad_hdr.attr_mod != 0)
  2251. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2252. p->base_version = OPA_MGMT_BASE_VERSION;
  2253. p->class_version = OPA_SM_CLASS_VERSION;
  2254. /*
  2255. * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
  2256. */
  2257. p->cap_mask2_resp_time = cpu_to_be32(18);
  2258. if (resp_len)
  2259. *resp_len += sizeof(*p);
  2260. return reply((struct ib_mad_hdr *)pmp);
  2261. }
  2262. static void a0_portstatus(struct hfi1_pportdata *ppd,
  2263. struct opa_port_status_rsp *rsp, u32 vl_select_mask)
  2264. {
  2265. if (!is_bx(ppd->dd)) {
  2266. unsigned long vl;
  2267. u64 sum_vl_xmit_wait = 0;
  2268. u32 vl_all_mask = VL_MASK_ALL;
  2269. for_each_set_bit(vl, (unsigned long *)&(vl_all_mask),
  2270. 8 * sizeof(vl_all_mask)) {
  2271. u64 tmp = sum_vl_xmit_wait +
  2272. read_port_cntr(ppd, C_TX_WAIT_VL,
  2273. idx_from_vl(vl));
  2274. if (tmp < sum_vl_xmit_wait) {
  2275. /* we wrapped */
  2276. sum_vl_xmit_wait = (u64)~0;
  2277. break;
  2278. }
  2279. sum_vl_xmit_wait = tmp;
  2280. }
  2281. if (be64_to_cpu(rsp->port_xmit_wait) > sum_vl_xmit_wait)
  2282. rsp->port_xmit_wait = cpu_to_be64(sum_vl_xmit_wait);
  2283. }
  2284. }
  2285. /**
  2286. * tx_link_width - convert link width bitmask to integer
  2287. * value representing actual link width.
  2288. * @link_width: width of active link
  2289. * @return: return index of the bit set in link_width var
  2290. *
  2291. * The function convert and return the index of bit set
  2292. * that indicate the current link width.
  2293. */
  2294. u16 tx_link_width(u16 link_width)
  2295. {
  2296. int n = LINK_WIDTH_DEFAULT;
  2297. u16 tx_width = n;
  2298. while (link_width && n) {
  2299. if (link_width & (1 << (n - 1))) {
  2300. tx_width = n;
  2301. break;
  2302. }
  2303. n--;
  2304. }
  2305. return tx_width;
  2306. }
  2307. /**
  2308. * get_xmit_wait_counters - Convert HFI 's SendWaitCnt/SendWaitVlCnt
  2309. * counter in unit of TXE cycle times to flit times.
  2310. * @ppd: info of physical Hfi port
  2311. * @link_width: width of active link
  2312. * @link_speed: speed of active link
  2313. * @vl: represent VL0-VL7, VL15 for PortVLXmitWait counters request
  2314. * and if vl value is C_VL_COUNT, it represent SendWaitCnt
  2315. * counter request
  2316. * @return: return SendWaitCnt/SendWaitVlCnt counter value per vl.
  2317. *
  2318. * Convert SendWaitCnt/SendWaitVlCnt counter from TXE cycle times to
  2319. * flit times. Call this function to samples these counters. This
  2320. * function will calculate for previous state transition and update
  2321. * current state at end of function using ppd->prev_link_width and
  2322. * ppd->port_vl_xmit_wait_last to port_vl_xmit_wait_curr and link_width.
  2323. */
  2324. u64 get_xmit_wait_counters(struct hfi1_pportdata *ppd,
  2325. u16 link_width, u16 link_speed, int vl)
  2326. {
  2327. u64 port_vl_xmit_wait_curr;
  2328. u64 delta_vl_xmit_wait;
  2329. u64 xmit_wait_val;
  2330. if (vl > C_VL_COUNT)
  2331. return 0;
  2332. if (vl < C_VL_COUNT)
  2333. port_vl_xmit_wait_curr =
  2334. read_port_cntr(ppd, C_TX_WAIT_VL, vl);
  2335. else
  2336. port_vl_xmit_wait_curr =
  2337. read_port_cntr(ppd, C_TX_WAIT, CNTR_INVALID_VL);
  2338. xmit_wait_val =
  2339. port_vl_xmit_wait_curr -
  2340. ppd->port_vl_xmit_wait_last[vl];
  2341. delta_vl_xmit_wait =
  2342. convert_xmit_counter(xmit_wait_val,
  2343. ppd->prev_link_width,
  2344. link_speed);
  2345. ppd->vl_xmit_flit_cnt[vl] += delta_vl_xmit_wait;
  2346. ppd->port_vl_xmit_wait_last[vl] = port_vl_xmit_wait_curr;
  2347. ppd->prev_link_width = link_width;
  2348. return ppd->vl_xmit_flit_cnt[vl];
  2349. }
  2350. static int pma_get_opa_portstatus(struct opa_pma_mad *pmp,
  2351. struct ib_device *ibdev,
  2352. u8 port, u32 *resp_len)
  2353. {
  2354. struct opa_port_status_req *req =
  2355. (struct opa_port_status_req *)pmp->data;
  2356. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2357. struct opa_port_status_rsp *rsp;
  2358. u32 vl_select_mask = be32_to_cpu(req->vl_select_mask);
  2359. unsigned long vl;
  2360. size_t response_data_size;
  2361. u32 nports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
  2362. u8 port_num = req->port_num;
  2363. u8 num_vls = hweight32(vl_select_mask);
  2364. struct _vls_pctrs *vlinfo;
  2365. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  2366. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  2367. int vfi;
  2368. u64 tmp, tmp2;
  2369. u16 link_width;
  2370. u16 link_speed;
  2371. response_data_size = sizeof(struct opa_port_status_rsp) +
  2372. num_vls * sizeof(struct _vls_pctrs);
  2373. if (response_data_size > sizeof(pmp->data)) {
  2374. pmp->mad_hdr.status |= OPA_PM_STATUS_REQUEST_TOO_LARGE;
  2375. return reply((struct ib_mad_hdr *)pmp);
  2376. }
  2377. if (nports != 1 || (port_num && port_num != port) ||
  2378. num_vls > OPA_MAX_VLS || (vl_select_mask & ~VL_MASK_ALL)) {
  2379. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2380. return reply((struct ib_mad_hdr *)pmp);
  2381. }
  2382. memset(pmp->data, 0, sizeof(pmp->data));
  2383. rsp = (struct opa_port_status_rsp *)pmp->data;
  2384. if (port_num)
  2385. rsp->port_num = port_num;
  2386. else
  2387. rsp->port_num = port;
  2388. rsp->port_rcv_constraint_errors =
  2389. cpu_to_be64(read_port_cntr(ppd, C_SW_RCV_CSTR_ERR,
  2390. CNTR_INVALID_VL));
  2391. hfi1_read_link_quality(dd, &rsp->link_quality_indicator);
  2392. rsp->vl_select_mask = cpu_to_be32(vl_select_mask);
  2393. rsp->port_xmit_data = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_FLITS,
  2394. CNTR_INVALID_VL));
  2395. rsp->port_rcv_data = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FLITS,
  2396. CNTR_INVALID_VL));
  2397. rsp->port_xmit_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_PKTS,
  2398. CNTR_INVALID_VL));
  2399. rsp->port_rcv_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_PKTS,
  2400. CNTR_INVALID_VL));
  2401. rsp->port_multicast_xmit_pkts =
  2402. cpu_to_be64(read_dev_cntr(dd, C_DC_MC_XMIT_PKTS,
  2403. CNTR_INVALID_VL));
  2404. rsp->port_multicast_rcv_pkts =
  2405. cpu_to_be64(read_dev_cntr(dd, C_DC_MC_RCV_PKTS,
  2406. CNTR_INVALID_VL));
  2407. /*
  2408. * Convert PortXmitWait counter from TXE cycle times
  2409. * to flit times.
  2410. */
  2411. link_width =
  2412. tx_link_width(ppd->link_width_downgrade_tx_active);
  2413. link_speed = get_link_speed(ppd->link_speed_active);
  2414. rsp->port_xmit_wait =
  2415. cpu_to_be64(get_xmit_wait_counters(ppd, link_width,
  2416. link_speed, C_VL_COUNT));
  2417. rsp->port_rcv_fecn =
  2418. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN, CNTR_INVALID_VL));
  2419. rsp->port_rcv_becn =
  2420. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN, CNTR_INVALID_VL));
  2421. rsp->port_xmit_discards =
  2422. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD,
  2423. CNTR_INVALID_VL));
  2424. rsp->port_xmit_constraint_errors =
  2425. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_CSTR_ERR,
  2426. CNTR_INVALID_VL));
  2427. rsp->port_rcv_remote_physical_errors =
  2428. cpu_to_be64(read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
  2429. CNTR_INVALID_VL));
  2430. rsp->local_link_integrity_errors =
  2431. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_REPLAY,
  2432. CNTR_INVALID_VL));
  2433. tmp = read_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL);
  2434. tmp2 = tmp + read_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT,
  2435. CNTR_INVALID_VL);
  2436. if (tmp2 > (u32)UINT_MAX || tmp2 < tmp) {
  2437. /* overflow/wrapped */
  2438. rsp->link_error_recovery = cpu_to_be32(~0);
  2439. } else {
  2440. rsp->link_error_recovery = cpu_to_be32(tmp2);
  2441. }
  2442. rsp->port_rcv_errors =
  2443. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL));
  2444. rsp->excessive_buffer_overruns =
  2445. cpu_to_be64(read_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL));
  2446. rsp->fm_config_errors =
  2447. cpu_to_be64(read_dev_cntr(dd, C_DC_FM_CFG_ERR,
  2448. CNTR_INVALID_VL));
  2449. rsp->link_downed = cpu_to_be32(read_port_cntr(ppd, C_SW_LINK_DOWN,
  2450. CNTR_INVALID_VL));
  2451. /* rsp->uncorrectable_errors is 8 bits wide, and it pegs at 0xff */
  2452. tmp = read_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL);
  2453. rsp->uncorrectable_errors = tmp < 0x100 ? (tmp & 0xff) : 0xff;
  2454. vlinfo = &rsp->vls[0];
  2455. vfi = 0;
  2456. /* The vl_select_mask has been checked above, and we know
  2457. * that it contains only entries which represent valid VLs.
  2458. * So in the for_each_set_bit() loop below, we don't need
  2459. * any additional checks for vl.
  2460. */
  2461. for_each_set_bit(vl, (unsigned long *)&(vl_select_mask),
  2462. 8 * sizeof(vl_select_mask)) {
  2463. memset(vlinfo, 0, sizeof(*vlinfo));
  2464. tmp = read_dev_cntr(dd, C_DC_RX_FLIT_VL, idx_from_vl(vl));
  2465. rsp->vls[vfi].port_vl_rcv_data = cpu_to_be64(tmp);
  2466. rsp->vls[vfi].port_vl_rcv_pkts =
  2467. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_PKT_VL,
  2468. idx_from_vl(vl)));
  2469. rsp->vls[vfi].port_vl_xmit_data =
  2470. cpu_to_be64(read_port_cntr(ppd, C_TX_FLIT_VL,
  2471. idx_from_vl(vl)));
  2472. rsp->vls[vfi].port_vl_xmit_pkts =
  2473. cpu_to_be64(read_port_cntr(ppd, C_TX_PKT_VL,
  2474. idx_from_vl(vl)));
  2475. /*
  2476. * Convert PortVlXmitWait counter from TXE cycle
  2477. * times to flit times.
  2478. */
  2479. rsp->vls[vfi].port_vl_xmit_wait =
  2480. cpu_to_be64(get_xmit_wait_counters(ppd, link_width,
  2481. link_speed,
  2482. idx_from_vl(vl)));
  2483. rsp->vls[vfi].port_vl_rcv_fecn =
  2484. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN_VL,
  2485. idx_from_vl(vl)));
  2486. rsp->vls[vfi].port_vl_rcv_becn =
  2487. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN_VL,
  2488. idx_from_vl(vl)));
  2489. rsp->vls[vfi].port_vl_xmit_discards =
  2490. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD_VL,
  2491. idx_from_vl(vl)));
  2492. vlinfo++;
  2493. vfi++;
  2494. }
  2495. a0_portstatus(ppd, rsp, vl_select_mask);
  2496. if (resp_len)
  2497. *resp_len += response_data_size;
  2498. return reply((struct ib_mad_hdr *)pmp);
  2499. }
  2500. static u64 get_error_counter_summary(struct ib_device *ibdev, u8 port,
  2501. u8 res_lli, u8 res_ler)
  2502. {
  2503. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2504. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  2505. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  2506. u64 error_counter_summary = 0, tmp;
  2507. error_counter_summary += read_port_cntr(ppd, C_SW_RCV_CSTR_ERR,
  2508. CNTR_INVALID_VL);
  2509. /* port_rcv_switch_relay_errors is 0 for HFIs */
  2510. error_counter_summary += read_port_cntr(ppd, C_SW_XMIT_DSCD,
  2511. CNTR_INVALID_VL);
  2512. error_counter_summary += read_port_cntr(ppd, C_SW_XMIT_CSTR_ERR,
  2513. CNTR_INVALID_VL);
  2514. error_counter_summary += read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
  2515. CNTR_INVALID_VL);
  2516. /* local link integrity must be right-shifted by the lli resolution */
  2517. error_counter_summary += (read_dev_cntr(dd, C_DC_RX_REPLAY,
  2518. CNTR_INVALID_VL) >> res_lli);
  2519. /* link error recovery must b right-shifted by the ler resolution */
  2520. tmp = read_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL);
  2521. tmp += read_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT, CNTR_INVALID_VL);
  2522. error_counter_summary += (tmp >> res_ler);
  2523. error_counter_summary += read_dev_cntr(dd, C_DC_RCV_ERR,
  2524. CNTR_INVALID_VL);
  2525. error_counter_summary += read_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL);
  2526. error_counter_summary += read_dev_cntr(dd, C_DC_FM_CFG_ERR,
  2527. CNTR_INVALID_VL);
  2528. /* ppd->link_downed is a 32-bit value */
  2529. error_counter_summary += read_port_cntr(ppd, C_SW_LINK_DOWN,
  2530. CNTR_INVALID_VL);
  2531. tmp = read_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL);
  2532. /* this is an 8-bit quantity */
  2533. error_counter_summary += tmp < 0x100 ? (tmp & 0xff) : 0xff;
  2534. return error_counter_summary;
  2535. }
  2536. static void a0_datacounters(struct hfi1_pportdata *ppd, struct _port_dctrs *rsp,
  2537. u32 vl_select_mask)
  2538. {
  2539. if (!is_bx(ppd->dd)) {
  2540. unsigned long vl;
  2541. u64 sum_vl_xmit_wait = 0;
  2542. u32 vl_all_mask = VL_MASK_ALL;
  2543. for_each_set_bit(vl, (unsigned long *)&(vl_all_mask),
  2544. 8 * sizeof(vl_all_mask)) {
  2545. u64 tmp = sum_vl_xmit_wait +
  2546. read_port_cntr(ppd, C_TX_WAIT_VL,
  2547. idx_from_vl(vl));
  2548. if (tmp < sum_vl_xmit_wait) {
  2549. /* we wrapped */
  2550. sum_vl_xmit_wait = (u64)~0;
  2551. break;
  2552. }
  2553. sum_vl_xmit_wait = tmp;
  2554. }
  2555. if (be64_to_cpu(rsp->port_xmit_wait) > sum_vl_xmit_wait)
  2556. rsp->port_xmit_wait = cpu_to_be64(sum_vl_xmit_wait);
  2557. }
  2558. }
  2559. static void pma_get_opa_port_dctrs(struct ib_device *ibdev,
  2560. struct _port_dctrs *rsp)
  2561. {
  2562. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2563. rsp->port_xmit_data = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_FLITS,
  2564. CNTR_INVALID_VL));
  2565. rsp->port_rcv_data = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FLITS,
  2566. CNTR_INVALID_VL));
  2567. rsp->port_xmit_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_PKTS,
  2568. CNTR_INVALID_VL));
  2569. rsp->port_rcv_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_PKTS,
  2570. CNTR_INVALID_VL));
  2571. rsp->port_multicast_xmit_pkts =
  2572. cpu_to_be64(read_dev_cntr(dd, C_DC_MC_XMIT_PKTS,
  2573. CNTR_INVALID_VL));
  2574. rsp->port_multicast_rcv_pkts =
  2575. cpu_to_be64(read_dev_cntr(dd, C_DC_MC_RCV_PKTS,
  2576. CNTR_INVALID_VL));
  2577. }
  2578. static int pma_get_opa_datacounters(struct opa_pma_mad *pmp,
  2579. struct ib_device *ibdev,
  2580. u8 port, u32 *resp_len)
  2581. {
  2582. struct opa_port_data_counters_msg *req =
  2583. (struct opa_port_data_counters_msg *)pmp->data;
  2584. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2585. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  2586. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  2587. struct _port_dctrs *rsp;
  2588. struct _vls_dctrs *vlinfo;
  2589. size_t response_data_size;
  2590. u32 num_ports;
  2591. u8 lq, num_vls;
  2592. u8 res_lli, res_ler;
  2593. u64 port_mask;
  2594. u8 port_num;
  2595. unsigned long vl;
  2596. u32 vl_select_mask;
  2597. int vfi;
  2598. u16 link_width;
  2599. u16 link_speed;
  2600. num_ports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
  2601. num_vls = hweight32(be32_to_cpu(req->vl_select_mask));
  2602. vl_select_mask = be32_to_cpu(req->vl_select_mask);
  2603. res_lli = (u8)(be32_to_cpu(req->resolution) & MSK_LLI) >> MSK_LLI_SFT;
  2604. res_lli = res_lli ? res_lli + ADD_LLI : 0;
  2605. res_ler = (u8)(be32_to_cpu(req->resolution) & MSK_LER) >> MSK_LER_SFT;
  2606. res_ler = res_ler ? res_ler + ADD_LER : 0;
  2607. if (num_ports != 1 || (vl_select_mask & ~VL_MASK_ALL)) {
  2608. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2609. return reply((struct ib_mad_hdr *)pmp);
  2610. }
  2611. /* Sanity check */
  2612. response_data_size = sizeof(struct opa_port_data_counters_msg) +
  2613. num_vls * sizeof(struct _vls_dctrs);
  2614. if (response_data_size > sizeof(pmp->data)) {
  2615. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2616. return reply((struct ib_mad_hdr *)pmp);
  2617. }
  2618. /*
  2619. * The bit set in the mask needs to be consistent with the
  2620. * port the request came in on.
  2621. */
  2622. port_mask = be64_to_cpu(req->port_select_mask[3]);
  2623. port_num = find_first_bit((unsigned long *)&port_mask,
  2624. sizeof(port_mask) * 8);
  2625. if (port_num != port) {
  2626. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2627. return reply((struct ib_mad_hdr *)pmp);
  2628. }
  2629. rsp = &req->port[0];
  2630. memset(rsp, 0, sizeof(*rsp));
  2631. rsp->port_number = port;
  2632. /*
  2633. * Note that link_quality_indicator is a 32 bit quantity in
  2634. * 'datacounters' queries (as opposed to 'portinfo' queries,
  2635. * where it's a byte).
  2636. */
  2637. hfi1_read_link_quality(dd, &lq);
  2638. rsp->link_quality_indicator = cpu_to_be32((u32)lq);
  2639. pma_get_opa_port_dctrs(ibdev, rsp);
  2640. /*
  2641. * Convert PortXmitWait counter from TXE
  2642. * cycle times to flit times.
  2643. */
  2644. link_width =
  2645. tx_link_width(ppd->link_width_downgrade_tx_active);
  2646. link_speed = get_link_speed(ppd->link_speed_active);
  2647. rsp->port_xmit_wait =
  2648. cpu_to_be64(get_xmit_wait_counters(ppd, link_width,
  2649. link_speed, C_VL_COUNT));
  2650. rsp->port_rcv_fecn =
  2651. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN, CNTR_INVALID_VL));
  2652. rsp->port_rcv_becn =
  2653. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN, CNTR_INVALID_VL));
  2654. rsp->port_error_counter_summary =
  2655. cpu_to_be64(get_error_counter_summary(ibdev, port,
  2656. res_lli, res_ler));
  2657. vlinfo = &rsp->vls[0];
  2658. vfi = 0;
  2659. /* The vl_select_mask has been checked above, and we know
  2660. * that it contains only entries which represent valid VLs.
  2661. * So in the for_each_set_bit() loop below, we don't need
  2662. * any additional checks for vl.
  2663. */
  2664. for_each_set_bit(vl, (unsigned long *)&(vl_select_mask),
  2665. 8 * sizeof(req->vl_select_mask)) {
  2666. memset(vlinfo, 0, sizeof(*vlinfo));
  2667. rsp->vls[vfi].port_vl_xmit_data =
  2668. cpu_to_be64(read_port_cntr(ppd, C_TX_FLIT_VL,
  2669. idx_from_vl(vl)));
  2670. rsp->vls[vfi].port_vl_rcv_data =
  2671. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_FLIT_VL,
  2672. idx_from_vl(vl)));
  2673. rsp->vls[vfi].port_vl_xmit_pkts =
  2674. cpu_to_be64(read_port_cntr(ppd, C_TX_PKT_VL,
  2675. idx_from_vl(vl)));
  2676. rsp->vls[vfi].port_vl_rcv_pkts =
  2677. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_PKT_VL,
  2678. idx_from_vl(vl)));
  2679. /*
  2680. * Convert PortVlXmitWait counter from TXE
  2681. * cycle times to flit times.
  2682. */
  2683. rsp->vls[vfi].port_vl_xmit_wait =
  2684. cpu_to_be64(get_xmit_wait_counters(ppd, link_width,
  2685. link_speed,
  2686. idx_from_vl(vl)));
  2687. rsp->vls[vfi].port_vl_rcv_fecn =
  2688. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN_VL,
  2689. idx_from_vl(vl)));
  2690. rsp->vls[vfi].port_vl_rcv_becn =
  2691. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN_VL,
  2692. idx_from_vl(vl)));
  2693. /* rsp->port_vl_xmit_time_cong is 0 for HFIs */
  2694. /* rsp->port_vl_xmit_wasted_bw ??? */
  2695. /* port_vl_xmit_wait_data - TXE (table 13-9 HFI spec) ???
  2696. * does this differ from rsp->vls[vfi].port_vl_xmit_wait
  2697. */
  2698. /*rsp->vls[vfi].port_vl_mark_fecn =
  2699. * cpu_to_be64(read_csr(dd, DCC_PRF_PORT_VL_MARK_FECN_CNT
  2700. * + offset));
  2701. */
  2702. vlinfo++;
  2703. vfi++;
  2704. }
  2705. a0_datacounters(ppd, rsp, vl_select_mask);
  2706. if (resp_len)
  2707. *resp_len += response_data_size;
  2708. return reply((struct ib_mad_hdr *)pmp);
  2709. }
  2710. static int pma_get_ib_portcounters_ext(struct ib_pma_mad *pmp,
  2711. struct ib_device *ibdev, u8 port)
  2712. {
  2713. struct ib_pma_portcounters_ext *p = (struct ib_pma_portcounters_ext *)
  2714. pmp->data;
  2715. struct _port_dctrs rsp;
  2716. if (pmp->mad_hdr.attr_mod != 0 || p->port_select != port) {
  2717. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2718. goto bail;
  2719. }
  2720. memset(&rsp, 0, sizeof(rsp));
  2721. pma_get_opa_port_dctrs(ibdev, &rsp);
  2722. p->port_xmit_data = rsp.port_xmit_data;
  2723. p->port_rcv_data = rsp.port_rcv_data;
  2724. p->port_xmit_packets = rsp.port_xmit_pkts;
  2725. p->port_rcv_packets = rsp.port_rcv_pkts;
  2726. p->port_unicast_xmit_packets = 0;
  2727. p->port_unicast_rcv_packets = 0;
  2728. p->port_multicast_xmit_packets = rsp.port_multicast_xmit_pkts;
  2729. p->port_multicast_rcv_packets = rsp.port_multicast_rcv_pkts;
  2730. bail:
  2731. return reply((struct ib_mad_hdr *)pmp);
  2732. }
  2733. static void pma_get_opa_port_ectrs(struct ib_device *ibdev,
  2734. struct _port_ectrs *rsp, u8 port)
  2735. {
  2736. u64 tmp, tmp2;
  2737. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2738. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  2739. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  2740. tmp = read_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL);
  2741. tmp2 = tmp + read_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT,
  2742. CNTR_INVALID_VL);
  2743. if (tmp2 > (u32)UINT_MAX || tmp2 < tmp) {
  2744. /* overflow/wrapped */
  2745. rsp->link_error_recovery = cpu_to_be32(~0);
  2746. } else {
  2747. rsp->link_error_recovery = cpu_to_be32(tmp2);
  2748. }
  2749. rsp->link_downed = cpu_to_be32(read_port_cntr(ppd, C_SW_LINK_DOWN,
  2750. CNTR_INVALID_VL));
  2751. rsp->port_rcv_errors =
  2752. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL));
  2753. rsp->port_rcv_remote_physical_errors =
  2754. cpu_to_be64(read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
  2755. CNTR_INVALID_VL));
  2756. rsp->port_rcv_switch_relay_errors = 0;
  2757. rsp->port_xmit_discards =
  2758. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD,
  2759. CNTR_INVALID_VL));
  2760. rsp->port_xmit_constraint_errors =
  2761. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_CSTR_ERR,
  2762. CNTR_INVALID_VL));
  2763. rsp->port_rcv_constraint_errors =
  2764. cpu_to_be64(read_port_cntr(ppd, C_SW_RCV_CSTR_ERR,
  2765. CNTR_INVALID_VL));
  2766. rsp->local_link_integrity_errors =
  2767. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_REPLAY,
  2768. CNTR_INVALID_VL));
  2769. rsp->excessive_buffer_overruns =
  2770. cpu_to_be64(read_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL));
  2771. }
  2772. static int pma_get_opa_porterrors(struct opa_pma_mad *pmp,
  2773. struct ib_device *ibdev,
  2774. u8 port, u32 *resp_len)
  2775. {
  2776. size_t response_data_size;
  2777. struct _port_ectrs *rsp;
  2778. u8 port_num;
  2779. struct opa_port_error_counters64_msg *req;
  2780. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2781. u32 num_ports;
  2782. u8 num_pslm;
  2783. u8 num_vls;
  2784. struct hfi1_ibport *ibp;
  2785. struct hfi1_pportdata *ppd;
  2786. struct _vls_ectrs *vlinfo;
  2787. unsigned long vl;
  2788. u64 port_mask, tmp;
  2789. u32 vl_select_mask;
  2790. int vfi;
  2791. req = (struct opa_port_error_counters64_msg *)pmp->data;
  2792. num_ports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
  2793. num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
  2794. num_vls = hweight32(be32_to_cpu(req->vl_select_mask));
  2795. if (num_ports != 1 || num_ports != num_pslm) {
  2796. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2797. return reply((struct ib_mad_hdr *)pmp);
  2798. }
  2799. response_data_size = sizeof(struct opa_port_error_counters64_msg) +
  2800. num_vls * sizeof(struct _vls_ectrs);
  2801. if (response_data_size > sizeof(pmp->data)) {
  2802. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2803. return reply((struct ib_mad_hdr *)pmp);
  2804. }
  2805. /*
  2806. * The bit set in the mask needs to be consistent with the
  2807. * port the request came in on.
  2808. */
  2809. port_mask = be64_to_cpu(req->port_select_mask[3]);
  2810. port_num = find_first_bit((unsigned long *)&port_mask,
  2811. sizeof(port_mask) * 8);
  2812. if (port_num != port) {
  2813. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2814. return reply((struct ib_mad_hdr *)pmp);
  2815. }
  2816. rsp = &req->port[0];
  2817. ibp = to_iport(ibdev, port_num);
  2818. ppd = ppd_from_ibp(ibp);
  2819. memset(rsp, 0, sizeof(*rsp));
  2820. rsp->port_number = port_num;
  2821. pma_get_opa_port_ectrs(ibdev, rsp, port_num);
  2822. rsp->port_rcv_remote_physical_errors =
  2823. cpu_to_be64(read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
  2824. CNTR_INVALID_VL));
  2825. rsp->fm_config_errors =
  2826. cpu_to_be64(read_dev_cntr(dd, C_DC_FM_CFG_ERR,
  2827. CNTR_INVALID_VL));
  2828. tmp = read_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL);
  2829. rsp->uncorrectable_errors = tmp < 0x100 ? (tmp & 0xff) : 0xff;
  2830. rsp->port_rcv_errors =
  2831. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL));
  2832. vlinfo = &rsp->vls[0];
  2833. vfi = 0;
  2834. vl_select_mask = be32_to_cpu(req->vl_select_mask);
  2835. for_each_set_bit(vl, (unsigned long *)&(vl_select_mask),
  2836. 8 * sizeof(req->vl_select_mask)) {
  2837. memset(vlinfo, 0, sizeof(*vlinfo));
  2838. rsp->vls[vfi].port_vl_xmit_discards =
  2839. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD_VL,
  2840. idx_from_vl(vl)));
  2841. vlinfo += 1;
  2842. vfi++;
  2843. }
  2844. if (resp_len)
  2845. *resp_len += response_data_size;
  2846. return reply((struct ib_mad_hdr *)pmp);
  2847. }
  2848. static int pma_get_ib_portcounters(struct ib_pma_mad *pmp,
  2849. struct ib_device *ibdev, u8 port)
  2850. {
  2851. struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
  2852. pmp->data;
  2853. struct _port_ectrs rsp;
  2854. u64 temp_link_overrun_errors;
  2855. u64 temp_64;
  2856. u32 temp_32;
  2857. memset(&rsp, 0, sizeof(rsp));
  2858. pma_get_opa_port_ectrs(ibdev, &rsp, port);
  2859. if (pmp->mad_hdr.attr_mod != 0 || p->port_select != port) {
  2860. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2861. goto bail;
  2862. }
  2863. p->symbol_error_counter = 0; /* N/A for OPA */
  2864. temp_32 = be32_to_cpu(rsp.link_error_recovery);
  2865. if (temp_32 > 0xFFUL)
  2866. p->link_error_recovery_counter = 0xFF;
  2867. else
  2868. p->link_error_recovery_counter = (u8)temp_32;
  2869. temp_32 = be32_to_cpu(rsp.link_downed);
  2870. if (temp_32 > 0xFFUL)
  2871. p->link_downed_counter = 0xFF;
  2872. else
  2873. p->link_downed_counter = (u8)temp_32;
  2874. temp_64 = be64_to_cpu(rsp.port_rcv_errors);
  2875. if (temp_64 > 0xFFFFUL)
  2876. p->port_rcv_errors = cpu_to_be16(0xFFFF);
  2877. else
  2878. p->port_rcv_errors = cpu_to_be16((u16)temp_64);
  2879. temp_64 = be64_to_cpu(rsp.port_rcv_remote_physical_errors);
  2880. if (temp_64 > 0xFFFFUL)
  2881. p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
  2882. else
  2883. p->port_rcv_remphys_errors = cpu_to_be16((u16)temp_64);
  2884. temp_64 = be64_to_cpu(rsp.port_rcv_switch_relay_errors);
  2885. p->port_rcv_switch_relay_errors = cpu_to_be16((u16)temp_64);
  2886. temp_64 = be64_to_cpu(rsp.port_xmit_discards);
  2887. if (temp_64 > 0xFFFFUL)
  2888. p->port_xmit_discards = cpu_to_be16(0xFFFF);
  2889. else
  2890. p->port_xmit_discards = cpu_to_be16((u16)temp_64);
  2891. temp_64 = be64_to_cpu(rsp.port_xmit_constraint_errors);
  2892. if (temp_64 > 0xFFUL)
  2893. p->port_xmit_constraint_errors = 0xFF;
  2894. else
  2895. p->port_xmit_constraint_errors = (u8)temp_64;
  2896. temp_64 = be64_to_cpu(rsp.port_rcv_constraint_errors);
  2897. if (temp_64 > 0xFFUL)
  2898. p->port_rcv_constraint_errors = 0xFFUL;
  2899. else
  2900. p->port_rcv_constraint_errors = (u8)temp_64;
  2901. /* LocalLink: 7:4, BufferOverrun: 3:0 */
  2902. temp_64 = be64_to_cpu(rsp.local_link_integrity_errors);
  2903. if (temp_64 > 0xFUL)
  2904. temp_64 = 0xFUL;
  2905. temp_link_overrun_errors = temp_64 << 4;
  2906. temp_64 = be64_to_cpu(rsp.excessive_buffer_overruns);
  2907. if (temp_64 > 0xFUL)
  2908. temp_64 = 0xFUL;
  2909. temp_link_overrun_errors |= temp_64;
  2910. p->link_overrun_errors = (u8)temp_link_overrun_errors;
  2911. p->vl15_dropped = 0; /* N/A for OPA */
  2912. bail:
  2913. return reply((struct ib_mad_hdr *)pmp);
  2914. }
  2915. static int pma_get_opa_errorinfo(struct opa_pma_mad *pmp,
  2916. struct ib_device *ibdev,
  2917. u8 port, u32 *resp_len)
  2918. {
  2919. size_t response_data_size;
  2920. struct _port_ei *rsp;
  2921. struct opa_port_error_info_msg *req;
  2922. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2923. u64 port_mask;
  2924. u32 num_ports;
  2925. u8 port_num;
  2926. u8 num_pslm;
  2927. u64 reg;
  2928. req = (struct opa_port_error_info_msg *)pmp->data;
  2929. rsp = &req->port[0];
  2930. num_ports = OPA_AM_NPORT(be32_to_cpu(pmp->mad_hdr.attr_mod));
  2931. num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
  2932. memset(rsp, 0, sizeof(*rsp));
  2933. if (num_ports != 1 || num_ports != num_pslm) {
  2934. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2935. return reply((struct ib_mad_hdr *)pmp);
  2936. }
  2937. /* Sanity check */
  2938. response_data_size = sizeof(struct opa_port_error_info_msg);
  2939. if (response_data_size > sizeof(pmp->data)) {
  2940. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2941. return reply((struct ib_mad_hdr *)pmp);
  2942. }
  2943. /*
  2944. * The bit set in the mask needs to be consistent with the port
  2945. * the request came in on.
  2946. */
  2947. port_mask = be64_to_cpu(req->port_select_mask[3]);
  2948. port_num = find_first_bit((unsigned long *)&port_mask,
  2949. sizeof(port_mask) * 8);
  2950. if (port_num != port) {
  2951. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2952. return reply((struct ib_mad_hdr *)pmp);
  2953. }
  2954. /* PortRcvErrorInfo */
  2955. rsp->port_rcv_ei.status_and_code =
  2956. dd->err_info_rcvport.status_and_code;
  2957. memcpy(&rsp->port_rcv_ei.ei.ei1to12.packet_flit1,
  2958. &dd->err_info_rcvport.packet_flit1, sizeof(u64));
  2959. memcpy(&rsp->port_rcv_ei.ei.ei1to12.packet_flit2,
  2960. &dd->err_info_rcvport.packet_flit2, sizeof(u64));
  2961. /* ExcessiverBufferOverrunInfo */
  2962. reg = read_csr(dd, RCV_ERR_INFO);
  2963. if (reg & RCV_ERR_INFO_RCV_EXCESS_BUFFER_OVERRUN_SMASK) {
  2964. /*
  2965. * if the RcvExcessBufferOverrun bit is set, save SC of
  2966. * first pkt that encountered an excess buffer overrun
  2967. */
  2968. u8 tmp = (u8)reg;
  2969. tmp &= RCV_ERR_INFO_RCV_EXCESS_BUFFER_OVERRUN_SC_SMASK;
  2970. tmp <<= 2;
  2971. rsp->excessive_buffer_overrun_ei.status_and_sc = tmp;
  2972. /* set the status bit */
  2973. rsp->excessive_buffer_overrun_ei.status_and_sc |= 0x80;
  2974. }
  2975. rsp->port_xmit_constraint_ei.status =
  2976. dd->err_info_xmit_constraint.status;
  2977. rsp->port_xmit_constraint_ei.pkey =
  2978. cpu_to_be16(dd->err_info_xmit_constraint.pkey);
  2979. rsp->port_xmit_constraint_ei.slid =
  2980. cpu_to_be32(dd->err_info_xmit_constraint.slid);
  2981. rsp->port_rcv_constraint_ei.status =
  2982. dd->err_info_rcv_constraint.status;
  2983. rsp->port_rcv_constraint_ei.pkey =
  2984. cpu_to_be16(dd->err_info_rcv_constraint.pkey);
  2985. rsp->port_rcv_constraint_ei.slid =
  2986. cpu_to_be32(dd->err_info_rcv_constraint.slid);
  2987. /* UncorrectableErrorInfo */
  2988. rsp->uncorrectable_ei.status_and_code = dd->err_info_uncorrectable;
  2989. /* FMConfigErrorInfo */
  2990. rsp->fm_config_ei.status_and_code = dd->err_info_fmconfig;
  2991. if (resp_len)
  2992. *resp_len += response_data_size;
  2993. return reply((struct ib_mad_hdr *)pmp);
  2994. }
  2995. static int pma_set_opa_portstatus(struct opa_pma_mad *pmp,
  2996. struct ib_device *ibdev,
  2997. u8 port, u32 *resp_len)
  2998. {
  2999. struct opa_clear_port_status *req =
  3000. (struct opa_clear_port_status *)pmp->data;
  3001. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  3002. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3003. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3004. u32 nports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
  3005. u64 portn = be64_to_cpu(req->port_select_mask[3]);
  3006. u32 counter_select = be32_to_cpu(req->counter_select_mask);
  3007. u32 vl_select_mask = VL_MASK_ALL; /* clear all per-vl cnts */
  3008. unsigned long vl;
  3009. if ((nports != 1) || (portn != 1 << port)) {
  3010. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  3011. return reply((struct ib_mad_hdr *)pmp);
  3012. }
  3013. /*
  3014. * only counters returned by pma_get_opa_portstatus() are
  3015. * handled, so when pma_get_opa_portstatus() gets a fix,
  3016. * the corresponding change should be made here as well.
  3017. */
  3018. if (counter_select & CS_PORT_XMIT_DATA)
  3019. write_dev_cntr(dd, C_DC_XMIT_FLITS, CNTR_INVALID_VL, 0);
  3020. if (counter_select & CS_PORT_RCV_DATA)
  3021. write_dev_cntr(dd, C_DC_RCV_FLITS, CNTR_INVALID_VL, 0);
  3022. if (counter_select & CS_PORT_XMIT_PKTS)
  3023. write_dev_cntr(dd, C_DC_XMIT_PKTS, CNTR_INVALID_VL, 0);
  3024. if (counter_select & CS_PORT_RCV_PKTS)
  3025. write_dev_cntr(dd, C_DC_RCV_PKTS, CNTR_INVALID_VL, 0);
  3026. if (counter_select & CS_PORT_MCAST_XMIT_PKTS)
  3027. write_dev_cntr(dd, C_DC_MC_XMIT_PKTS, CNTR_INVALID_VL, 0);
  3028. if (counter_select & CS_PORT_MCAST_RCV_PKTS)
  3029. write_dev_cntr(dd, C_DC_MC_RCV_PKTS, CNTR_INVALID_VL, 0);
  3030. if (counter_select & CS_PORT_XMIT_WAIT) {
  3031. write_port_cntr(ppd, C_TX_WAIT, CNTR_INVALID_VL, 0);
  3032. ppd->port_vl_xmit_wait_last[C_VL_COUNT] = 0;
  3033. ppd->vl_xmit_flit_cnt[C_VL_COUNT] = 0;
  3034. }
  3035. /* ignore cs_sw_portCongestion for HFIs */
  3036. if (counter_select & CS_PORT_RCV_FECN)
  3037. write_dev_cntr(dd, C_DC_RCV_FCN, CNTR_INVALID_VL, 0);
  3038. if (counter_select & CS_PORT_RCV_BECN)
  3039. write_dev_cntr(dd, C_DC_RCV_BCN, CNTR_INVALID_VL, 0);
  3040. /* ignore cs_port_xmit_time_cong for HFIs */
  3041. /* ignore cs_port_xmit_wasted_bw for now */
  3042. /* ignore cs_port_xmit_wait_data for now */
  3043. if (counter_select & CS_PORT_RCV_BUBBLE)
  3044. write_dev_cntr(dd, C_DC_RCV_BBL, CNTR_INVALID_VL, 0);
  3045. /* Only applicable for switch */
  3046. /* if (counter_select & CS_PORT_MARK_FECN)
  3047. * write_csr(dd, DCC_PRF_PORT_MARK_FECN_CNT, 0);
  3048. */
  3049. if (counter_select & CS_PORT_RCV_CONSTRAINT_ERRORS)
  3050. write_port_cntr(ppd, C_SW_RCV_CSTR_ERR, CNTR_INVALID_VL, 0);
  3051. /* ignore cs_port_rcv_switch_relay_errors for HFIs */
  3052. if (counter_select & CS_PORT_XMIT_DISCARDS)
  3053. write_port_cntr(ppd, C_SW_XMIT_DSCD, CNTR_INVALID_VL, 0);
  3054. if (counter_select & CS_PORT_XMIT_CONSTRAINT_ERRORS)
  3055. write_port_cntr(ppd, C_SW_XMIT_CSTR_ERR, CNTR_INVALID_VL, 0);
  3056. if (counter_select & CS_PORT_RCV_REMOTE_PHYSICAL_ERRORS)
  3057. write_dev_cntr(dd, C_DC_RMT_PHY_ERR, CNTR_INVALID_VL, 0);
  3058. if (counter_select & CS_LOCAL_LINK_INTEGRITY_ERRORS)
  3059. write_dev_cntr(dd, C_DC_RX_REPLAY, CNTR_INVALID_VL, 0);
  3060. if (counter_select & CS_LINK_ERROR_RECOVERY) {
  3061. write_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL, 0);
  3062. write_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT,
  3063. CNTR_INVALID_VL, 0);
  3064. }
  3065. if (counter_select & CS_PORT_RCV_ERRORS)
  3066. write_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL, 0);
  3067. if (counter_select & CS_EXCESSIVE_BUFFER_OVERRUNS) {
  3068. write_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL, 0);
  3069. dd->rcv_ovfl_cnt = 0;
  3070. }
  3071. if (counter_select & CS_FM_CONFIG_ERRORS)
  3072. write_dev_cntr(dd, C_DC_FM_CFG_ERR, CNTR_INVALID_VL, 0);
  3073. if (counter_select & CS_LINK_DOWNED)
  3074. write_port_cntr(ppd, C_SW_LINK_DOWN, CNTR_INVALID_VL, 0);
  3075. if (counter_select & CS_UNCORRECTABLE_ERRORS)
  3076. write_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL, 0);
  3077. for_each_set_bit(vl, (unsigned long *)&(vl_select_mask),
  3078. 8 * sizeof(vl_select_mask)) {
  3079. if (counter_select & CS_PORT_XMIT_DATA)
  3080. write_port_cntr(ppd, C_TX_FLIT_VL, idx_from_vl(vl), 0);
  3081. if (counter_select & CS_PORT_RCV_DATA)
  3082. write_dev_cntr(dd, C_DC_RX_FLIT_VL, idx_from_vl(vl), 0);
  3083. if (counter_select & CS_PORT_XMIT_PKTS)
  3084. write_port_cntr(ppd, C_TX_PKT_VL, idx_from_vl(vl), 0);
  3085. if (counter_select & CS_PORT_RCV_PKTS)
  3086. write_dev_cntr(dd, C_DC_RX_PKT_VL, idx_from_vl(vl), 0);
  3087. if (counter_select & CS_PORT_XMIT_WAIT) {
  3088. write_port_cntr(ppd, C_TX_WAIT_VL, idx_from_vl(vl), 0);
  3089. ppd->port_vl_xmit_wait_last[idx_from_vl(vl)] = 0;
  3090. ppd->vl_xmit_flit_cnt[idx_from_vl(vl)] = 0;
  3091. }
  3092. /* sw_port_vl_congestion is 0 for HFIs */
  3093. if (counter_select & CS_PORT_RCV_FECN)
  3094. write_dev_cntr(dd, C_DC_RCV_FCN_VL, idx_from_vl(vl), 0);
  3095. if (counter_select & CS_PORT_RCV_BECN)
  3096. write_dev_cntr(dd, C_DC_RCV_BCN_VL, idx_from_vl(vl), 0);
  3097. /* port_vl_xmit_time_cong is 0 for HFIs */
  3098. /* port_vl_xmit_wasted_bw ??? */
  3099. /* port_vl_xmit_wait_data - TXE (table 13-9 HFI spec) ??? */
  3100. if (counter_select & CS_PORT_RCV_BUBBLE)
  3101. write_dev_cntr(dd, C_DC_RCV_BBL_VL, idx_from_vl(vl), 0);
  3102. /* if (counter_select & CS_PORT_MARK_FECN)
  3103. * write_csr(dd, DCC_PRF_PORT_VL_MARK_FECN_CNT + offset, 0);
  3104. */
  3105. if (counter_select & C_SW_XMIT_DSCD_VL)
  3106. write_port_cntr(ppd, C_SW_XMIT_DSCD_VL,
  3107. idx_from_vl(vl), 0);
  3108. }
  3109. if (resp_len)
  3110. *resp_len += sizeof(*req);
  3111. return reply((struct ib_mad_hdr *)pmp);
  3112. }
  3113. static int pma_set_opa_errorinfo(struct opa_pma_mad *pmp,
  3114. struct ib_device *ibdev,
  3115. u8 port, u32 *resp_len)
  3116. {
  3117. struct _port_ei *rsp;
  3118. struct opa_port_error_info_msg *req;
  3119. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  3120. u64 port_mask;
  3121. u32 num_ports;
  3122. u8 port_num;
  3123. u8 num_pslm;
  3124. u32 error_info_select;
  3125. req = (struct opa_port_error_info_msg *)pmp->data;
  3126. rsp = &req->port[0];
  3127. num_ports = OPA_AM_NPORT(be32_to_cpu(pmp->mad_hdr.attr_mod));
  3128. num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
  3129. memset(rsp, 0, sizeof(*rsp));
  3130. if (num_ports != 1 || num_ports != num_pslm) {
  3131. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  3132. return reply((struct ib_mad_hdr *)pmp);
  3133. }
  3134. /*
  3135. * The bit set in the mask needs to be consistent with the port
  3136. * the request came in on.
  3137. */
  3138. port_mask = be64_to_cpu(req->port_select_mask[3]);
  3139. port_num = find_first_bit((unsigned long *)&port_mask,
  3140. sizeof(port_mask) * 8);
  3141. if (port_num != port) {
  3142. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  3143. return reply((struct ib_mad_hdr *)pmp);
  3144. }
  3145. error_info_select = be32_to_cpu(req->error_info_select_mask);
  3146. /* PortRcvErrorInfo */
  3147. if (error_info_select & ES_PORT_RCV_ERROR_INFO)
  3148. /* turn off status bit */
  3149. dd->err_info_rcvport.status_and_code &= ~OPA_EI_STATUS_SMASK;
  3150. /* ExcessiverBufferOverrunInfo */
  3151. if (error_info_select & ES_EXCESSIVE_BUFFER_OVERRUN_INFO)
  3152. /*
  3153. * status bit is essentially kept in the h/w - bit 5 of
  3154. * RCV_ERR_INFO
  3155. */
  3156. write_csr(dd, RCV_ERR_INFO,
  3157. RCV_ERR_INFO_RCV_EXCESS_BUFFER_OVERRUN_SMASK);
  3158. if (error_info_select & ES_PORT_XMIT_CONSTRAINT_ERROR_INFO)
  3159. dd->err_info_xmit_constraint.status &= ~OPA_EI_STATUS_SMASK;
  3160. if (error_info_select & ES_PORT_RCV_CONSTRAINT_ERROR_INFO)
  3161. dd->err_info_rcv_constraint.status &= ~OPA_EI_STATUS_SMASK;
  3162. /* UncorrectableErrorInfo */
  3163. if (error_info_select & ES_UNCORRECTABLE_ERROR_INFO)
  3164. /* turn off status bit */
  3165. dd->err_info_uncorrectable &= ~OPA_EI_STATUS_SMASK;
  3166. /* FMConfigErrorInfo */
  3167. if (error_info_select & ES_FM_CONFIG_ERROR_INFO)
  3168. /* turn off status bit */
  3169. dd->err_info_fmconfig &= ~OPA_EI_STATUS_SMASK;
  3170. if (resp_len)
  3171. *resp_len += sizeof(*req);
  3172. return reply((struct ib_mad_hdr *)pmp);
  3173. }
  3174. struct opa_congestion_info_attr {
  3175. __be16 congestion_info;
  3176. u8 control_table_cap; /* Multiple of 64 entry unit CCTs */
  3177. u8 congestion_log_length;
  3178. } __packed;
  3179. static int __subn_get_opa_cong_info(struct opa_smp *smp, u32 am, u8 *data,
  3180. struct ib_device *ibdev, u8 port,
  3181. u32 *resp_len, u32 max_len)
  3182. {
  3183. struct opa_congestion_info_attr *p =
  3184. (struct opa_congestion_info_attr *)data;
  3185. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3186. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3187. if (smp_length_check(sizeof(*p), max_len)) {
  3188. smp->status |= IB_SMP_INVALID_FIELD;
  3189. return reply((struct ib_mad_hdr *)smp);
  3190. }
  3191. p->congestion_info = 0;
  3192. p->control_table_cap = ppd->cc_max_table_entries;
  3193. p->congestion_log_length = OPA_CONG_LOG_ELEMS;
  3194. if (resp_len)
  3195. *resp_len += sizeof(*p);
  3196. return reply((struct ib_mad_hdr *)smp);
  3197. }
  3198. static int __subn_get_opa_cong_setting(struct opa_smp *smp, u32 am,
  3199. u8 *data, struct ib_device *ibdev,
  3200. u8 port, u32 *resp_len, u32 max_len)
  3201. {
  3202. int i;
  3203. struct opa_congestion_setting_attr *p =
  3204. (struct opa_congestion_setting_attr *)data;
  3205. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3206. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3207. struct opa_congestion_setting_entry_shadow *entries;
  3208. struct cc_state *cc_state;
  3209. if (smp_length_check(sizeof(*p), max_len)) {
  3210. smp->status |= IB_SMP_INVALID_FIELD;
  3211. return reply((struct ib_mad_hdr *)smp);
  3212. }
  3213. rcu_read_lock();
  3214. cc_state = get_cc_state(ppd);
  3215. if (!cc_state) {
  3216. rcu_read_unlock();
  3217. return reply((struct ib_mad_hdr *)smp);
  3218. }
  3219. entries = cc_state->cong_setting.entries;
  3220. p->port_control = cpu_to_be16(cc_state->cong_setting.port_control);
  3221. p->control_map = cpu_to_be32(cc_state->cong_setting.control_map);
  3222. for (i = 0; i < OPA_MAX_SLS; i++) {
  3223. p->entries[i].ccti_increase = entries[i].ccti_increase;
  3224. p->entries[i].ccti_timer = cpu_to_be16(entries[i].ccti_timer);
  3225. p->entries[i].trigger_threshold =
  3226. entries[i].trigger_threshold;
  3227. p->entries[i].ccti_min = entries[i].ccti_min;
  3228. }
  3229. rcu_read_unlock();
  3230. if (resp_len)
  3231. *resp_len += sizeof(*p);
  3232. return reply((struct ib_mad_hdr *)smp);
  3233. }
  3234. /*
  3235. * Apply congestion control information stored in the ppd to the
  3236. * active structure.
  3237. */
  3238. static void apply_cc_state(struct hfi1_pportdata *ppd)
  3239. {
  3240. struct cc_state *old_cc_state, *new_cc_state;
  3241. new_cc_state = kzalloc(sizeof(*new_cc_state), GFP_KERNEL);
  3242. if (!new_cc_state)
  3243. return;
  3244. /*
  3245. * Hold the lock for updating *and* to prevent ppd information
  3246. * from changing during the update.
  3247. */
  3248. spin_lock(&ppd->cc_state_lock);
  3249. old_cc_state = get_cc_state_protected(ppd);
  3250. if (!old_cc_state) {
  3251. /* never active, or shutting down */
  3252. spin_unlock(&ppd->cc_state_lock);
  3253. kfree(new_cc_state);
  3254. return;
  3255. }
  3256. *new_cc_state = *old_cc_state;
  3257. if (ppd->total_cct_entry)
  3258. new_cc_state->cct.ccti_limit = ppd->total_cct_entry - 1;
  3259. else
  3260. new_cc_state->cct.ccti_limit = 0;
  3261. memcpy(new_cc_state->cct.entries, ppd->ccti_entries,
  3262. ppd->total_cct_entry * sizeof(struct ib_cc_table_entry));
  3263. new_cc_state->cong_setting.port_control = IB_CC_CCS_PC_SL_BASED;
  3264. new_cc_state->cong_setting.control_map = ppd->cc_sl_control_map;
  3265. memcpy(new_cc_state->cong_setting.entries, ppd->congestion_entries,
  3266. OPA_MAX_SLS * sizeof(struct opa_congestion_setting_entry));
  3267. rcu_assign_pointer(ppd->cc_state, new_cc_state);
  3268. spin_unlock(&ppd->cc_state_lock);
  3269. kfree_rcu(old_cc_state, rcu);
  3270. }
  3271. static int __subn_set_opa_cong_setting(struct opa_smp *smp, u32 am, u8 *data,
  3272. struct ib_device *ibdev, u8 port,
  3273. u32 *resp_len, u32 max_len)
  3274. {
  3275. struct opa_congestion_setting_attr *p =
  3276. (struct opa_congestion_setting_attr *)data;
  3277. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3278. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3279. struct opa_congestion_setting_entry_shadow *entries;
  3280. int i;
  3281. if (smp_length_check(sizeof(*p), max_len)) {
  3282. smp->status |= IB_SMP_INVALID_FIELD;
  3283. return reply((struct ib_mad_hdr *)smp);
  3284. }
  3285. /*
  3286. * Save details from packet into the ppd. Hold the cc_state_lock so
  3287. * our information is consistent with anyone trying to apply the state.
  3288. */
  3289. spin_lock(&ppd->cc_state_lock);
  3290. ppd->cc_sl_control_map = be32_to_cpu(p->control_map);
  3291. entries = ppd->congestion_entries;
  3292. for (i = 0; i < OPA_MAX_SLS; i++) {
  3293. entries[i].ccti_increase = p->entries[i].ccti_increase;
  3294. entries[i].ccti_timer = be16_to_cpu(p->entries[i].ccti_timer);
  3295. entries[i].trigger_threshold =
  3296. p->entries[i].trigger_threshold;
  3297. entries[i].ccti_min = p->entries[i].ccti_min;
  3298. }
  3299. spin_unlock(&ppd->cc_state_lock);
  3300. /* now apply the information */
  3301. apply_cc_state(ppd);
  3302. return __subn_get_opa_cong_setting(smp, am, data, ibdev, port,
  3303. resp_len, max_len);
  3304. }
  3305. static int __subn_get_opa_hfi1_cong_log(struct opa_smp *smp, u32 am,
  3306. u8 *data, struct ib_device *ibdev,
  3307. u8 port, u32 *resp_len, u32 max_len)
  3308. {
  3309. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3310. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3311. struct opa_hfi1_cong_log *cong_log = (struct opa_hfi1_cong_log *)data;
  3312. u64 ts;
  3313. int i;
  3314. if (am || smp_length_check(sizeof(*cong_log), max_len)) {
  3315. smp->status |= IB_SMP_INVALID_FIELD;
  3316. return reply((struct ib_mad_hdr *)smp);
  3317. }
  3318. spin_lock_irq(&ppd->cc_log_lock);
  3319. cong_log->log_type = OPA_CC_LOG_TYPE_HFI;
  3320. cong_log->congestion_flags = 0;
  3321. cong_log->threshold_event_counter =
  3322. cpu_to_be16(ppd->threshold_event_counter);
  3323. memcpy(cong_log->threshold_cong_event_map,
  3324. ppd->threshold_cong_event_map,
  3325. sizeof(cong_log->threshold_cong_event_map));
  3326. /* keep timestamp in units of 1.024 usec */
  3327. ts = ktime_get_ns() / 1024;
  3328. cong_log->current_time_stamp = cpu_to_be32(ts);
  3329. for (i = 0; i < OPA_CONG_LOG_ELEMS; i++) {
  3330. struct opa_hfi1_cong_log_event_internal *cce =
  3331. &ppd->cc_events[ppd->cc_mad_idx++];
  3332. if (ppd->cc_mad_idx == OPA_CONG_LOG_ELEMS)
  3333. ppd->cc_mad_idx = 0;
  3334. /*
  3335. * Entries which are older than twice the time
  3336. * required to wrap the counter are supposed to
  3337. * be zeroed (CA10-49 IBTA, release 1.2.1, V1).
  3338. */
  3339. if ((ts - cce->timestamp) / 2 > U32_MAX)
  3340. continue;
  3341. memcpy(cong_log->events[i].local_qp_cn_entry, &cce->lqpn, 3);
  3342. memcpy(cong_log->events[i].remote_qp_number_cn_entry,
  3343. &cce->rqpn, 3);
  3344. cong_log->events[i].sl_svc_type_cn_entry =
  3345. ((cce->sl & 0x1f) << 3) | (cce->svc_type & 0x7);
  3346. cong_log->events[i].remote_lid_cn_entry =
  3347. cpu_to_be32(cce->rlid);
  3348. cong_log->events[i].timestamp_cn_entry =
  3349. cpu_to_be32(cce->timestamp);
  3350. }
  3351. /*
  3352. * Reset threshold_cong_event_map, and threshold_event_counter
  3353. * to 0 when log is read.
  3354. */
  3355. memset(ppd->threshold_cong_event_map, 0x0,
  3356. sizeof(ppd->threshold_cong_event_map));
  3357. ppd->threshold_event_counter = 0;
  3358. spin_unlock_irq(&ppd->cc_log_lock);
  3359. if (resp_len)
  3360. *resp_len += sizeof(struct opa_hfi1_cong_log);
  3361. return reply((struct ib_mad_hdr *)smp);
  3362. }
  3363. static int __subn_get_opa_cc_table(struct opa_smp *smp, u32 am, u8 *data,
  3364. struct ib_device *ibdev, u8 port,
  3365. u32 *resp_len, u32 max_len)
  3366. {
  3367. struct ib_cc_table_attr *cc_table_attr =
  3368. (struct ib_cc_table_attr *)data;
  3369. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3370. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3371. u32 start_block = OPA_AM_START_BLK(am);
  3372. u32 n_blocks = OPA_AM_NBLK(am);
  3373. struct ib_cc_table_entry_shadow *entries;
  3374. int i, j;
  3375. u32 sentry, eentry;
  3376. struct cc_state *cc_state;
  3377. u32 size = sizeof(u16) * (IB_CCT_ENTRIES * n_blocks + 1);
  3378. /* sanity check n_blocks, start_block */
  3379. if (n_blocks == 0 || smp_length_check(size, max_len) ||
  3380. start_block + n_blocks > ppd->cc_max_table_entries) {
  3381. smp->status |= IB_SMP_INVALID_FIELD;
  3382. return reply((struct ib_mad_hdr *)smp);
  3383. }
  3384. rcu_read_lock();
  3385. cc_state = get_cc_state(ppd);
  3386. if (!cc_state) {
  3387. rcu_read_unlock();
  3388. return reply((struct ib_mad_hdr *)smp);
  3389. }
  3390. sentry = start_block * IB_CCT_ENTRIES;
  3391. eentry = sentry + (IB_CCT_ENTRIES * n_blocks);
  3392. cc_table_attr->ccti_limit = cpu_to_be16(cc_state->cct.ccti_limit);
  3393. entries = cc_state->cct.entries;
  3394. /* return n_blocks, though the last block may not be full */
  3395. for (j = 0, i = sentry; i < eentry; j++, i++)
  3396. cc_table_attr->ccti_entries[j].entry =
  3397. cpu_to_be16(entries[i].entry);
  3398. rcu_read_unlock();
  3399. if (resp_len)
  3400. *resp_len += size;
  3401. return reply((struct ib_mad_hdr *)smp);
  3402. }
  3403. static int __subn_set_opa_cc_table(struct opa_smp *smp, u32 am, u8 *data,
  3404. struct ib_device *ibdev, u8 port,
  3405. u32 *resp_len, u32 max_len)
  3406. {
  3407. struct ib_cc_table_attr *p = (struct ib_cc_table_attr *)data;
  3408. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3409. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3410. u32 start_block = OPA_AM_START_BLK(am);
  3411. u32 n_blocks = OPA_AM_NBLK(am);
  3412. struct ib_cc_table_entry_shadow *entries;
  3413. int i, j;
  3414. u32 sentry, eentry;
  3415. u16 ccti_limit;
  3416. u32 size = sizeof(u16) * (IB_CCT_ENTRIES * n_blocks + 1);
  3417. /* sanity check n_blocks, start_block */
  3418. if (n_blocks == 0 || smp_length_check(size, max_len) ||
  3419. start_block + n_blocks > ppd->cc_max_table_entries) {
  3420. smp->status |= IB_SMP_INVALID_FIELD;
  3421. return reply((struct ib_mad_hdr *)smp);
  3422. }
  3423. sentry = start_block * IB_CCT_ENTRIES;
  3424. eentry = sentry + ((n_blocks - 1) * IB_CCT_ENTRIES) +
  3425. (be16_to_cpu(p->ccti_limit)) % IB_CCT_ENTRIES + 1;
  3426. /* sanity check ccti_limit */
  3427. ccti_limit = be16_to_cpu(p->ccti_limit);
  3428. if (ccti_limit + 1 > eentry) {
  3429. smp->status |= IB_SMP_INVALID_FIELD;
  3430. return reply((struct ib_mad_hdr *)smp);
  3431. }
  3432. /*
  3433. * Save details from packet into the ppd. Hold the cc_state_lock so
  3434. * our information is consistent with anyone trying to apply the state.
  3435. */
  3436. spin_lock(&ppd->cc_state_lock);
  3437. ppd->total_cct_entry = ccti_limit + 1;
  3438. entries = ppd->ccti_entries;
  3439. for (j = 0, i = sentry; i < eentry; j++, i++)
  3440. entries[i].entry = be16_to_cpu(p->ccti_entries[j].entry);
  3441. spin_unlock(&ppd->cc_state_lock);
  3442. /* now apply the information */
  3443. apply_cc_state(ppd);
  3444. return __subn_get_opa_cc_table(smp, am, data, ibdev, port, resp_len,
  3445. max_len);
  3446. }
  3447. struct opa_led_info {
  3448. __be32 rsvd_led_mask;
  3449. __be32 rsvd;
  3450. };
  3451. #define OPA_LED_SHIFT 31
  3452. #define OPA_LED_MASK BIT(OPA_LED_SHIFT)
  3453. static int __subn_get_opa_led_info(struct opa_smp *smp, u32 am, u8 *data,
  3454. struct ib_device *ibdev, u8 port,
  3455. u32 *resp_len, u32 max_len)
  3456. {
  3457. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  3458. struct hfi1_pportdata *ppd = dd->pport;
  3459. struct opa_led_info *p = (struct opa_led_info *)data;
  3460. u32 nport = OPA_AM_NPORT(am);
  3461. u32 is_beaconing_active;
  3462. if (nport != 1 || smp_length_check(sizeof(*p), max_len)) {
  3463. smp->status |= IB_SMP_INVALID_FIELD;
  3464. return reply((struct ib_mad_hdr *)smp);
  3465. }
  3466. /*
  3467. * This pairs with the memory barrier in hfi1_start_led_override to
  3468. * ensure that we read the correct state of LED beaconing represented
  3469. * by led_override_timer_active
  3470. */
  3471. smp_rmb();
  3472. is_beaconing_active = !!atomic_read(&ppd->led_override_timer_active);
  3473. p->rsvd_led_mask = cpu_to_be32(is_beaconing_active << OPA_LED_SHIFT);
  3474. if (resp_len)
  3475. *resp_len += sizeof(struct opa_led_info);
  3476. return reply((struct ib_mad_hdr *)smp);
  3477. }
  3478. static int __subn_set_opa_led_info(struct opa_smp *smp, u32 am, u8 *data,
  3479. struct ib_device *ibdev, u8 port,
  3480. u32 *resp_len, u32 max_len)
  3481. {
  3482. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  3483. struct opa_led_info *p = (struct opa_led_info *)data;
  3484. u32 nport = OPA_AM_NPORT(am);
  3485. int on = !!(be32_to_cpu(p->rsvd_led_mask) & OPA_LED_MASK);
  3486. if (nport != 1 || smp_length_check(sizeof(*p), max_len)) {
  3487. smp->status |= IB_SMP_INVALID_FIELD;
  3488. return reply((struct ib_mad_hdr *)smp);
  3489. }
  3490. if (on)
  3491. hfi1_start_led_override(dd->pport, 2000, 1500);
  3492. else
  3493. shutdown_led_override(dd->pport);
  3494. return __subn_get_opa_led_info(smp, am, data, ibdev, port, resp_len,
  3495. max_len);
  3496. }
  3497. static int subn_get_opa_sma(__be16 attr_id, struct opa_smp *smp, u32 am,
  3498. u8 *data, struct ib_device *ibdev, u8 port,
  3499. u32 *resp_len, u32 max_len)
  3500. {
  3501. int ret;
  3502. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3503. switch (attr_id) {
  3504. case IB_SMP_ATTR_NODE_DESC:
  3505. ret = __subn_get_opa_nodedesc(smp, am, data, ibdev, port,
  3506. resp_len, max_len);
  3507. break;
  3508. case IB_SMP_ATTR_NODE_INFO:
  3509. ret = __subn_get_opa_nodeinfo(smp, am, data, ibdev, port,
  3510. resp_len, max_len);
  3511. break;
  3512. case IB_SMP_ATTR_PORT_INFO:
  3513. ret = __subn_get_opa_portinfo(smp, am, data, ibdev, port,
  3514. resp_len, max_len);
  3515. break;
  3516. case IB_SMP_ATTR_PKEY_TABLE:
  3517. ret = __subn_get_opa_pkeytable(smp, am, data, ibdev, port,
  3518. resp_len, max_len);
  3519. break;
  3520. case OPA_ATTRIB_ID_SL_TO_SC_MAP:
  3521. ret = __subn_get_opa_sl_to_sc(smp, am, data, ibdev, port,
  3522. resp_len, max_len);
  3523. break;
  3524. case OPA_ATTRIB_ID_SC_TO_SL_MAP:
  3525. ret = __subn_get_opa_sc_to_sl(smp, am, data, ibdev, port,
  3526. resp_len, max_len);
  3527. break;
  3528. case OPA_ATTRIB_ID_SC_TO_VLT_MAP:
  3529. ret = __subn_get_opa_sc_to_vlt(smp, am, data, ibdev, port,
  3530. resp_len, max_len);
  3531. break;
  3532. case OPA_ATTRIB_ID_SC_TO_VLNT_MAP:
  3533. ret = __subn_get_opa_sc_to_vlnt(smp, am, data, ibdev, port,
  3534. resp_len, max_len);
  3535. break;
  3536. case OPA_ATTRIB_ID_PORT_STATE_INFO:
  3537. ret = __subn_get_opa_psi(smp, am, data, ibdev, port,
  3538. resp_len, max_len);
  3539. break;
  3540. case OPA_ATTRIB_ID_BUFFER_CONTROL_TABLE:
  3541. ret = __subn_get_opa_bct(smp, am, data, ibdev, port,
  3542. resp_len, max_len);
  3543. break;
  3544. case OPA_ATTRIB_ID_CABLE_INFO:
  3545. ret = __subn_get_opa_cable_info(smp, am, data, ibdev, port,
  3546. resp_len, max_len);
  3547. break;
  3548. case IB_SMP_ATTR_VL_ARB_TABLE:
  3549. ret = __subn_get_opa_vl_arb(smp, am, data, ibdev, port,
  3550. resp_len, max_len);
  3551. break;
  3552. case OPA_ATTRIB_ID_CONGESTION_INFO:
  3553. ret = __subn_get_opa_cong_info(smp, am, data, ibdev, port,
  3554. resp_len, max_len);
  3555. break;
  3556. case OPA_ATTRIB_ID_HFI_CONGESTION_SETTING:
  3557. ret = __subn_get_opa_cong_setting(smp, am, data, ibdev,
  3558. port, resp_len, max_len);
  3559. break;
  3560. case OPA_ATTRIB_ID_HFI_CONGESTION_LOG:
  3561. ret = __subn_get_opa_hfi1_cong_log(smp, am, data, ibdev,
  3562. port, resp_len, max_len);
  3563. break;
  3564. case OPA_ATTRIB_ID_CONGESTION_CONTROL_TABLE:
  3565. ret = __subn_get_opa_cc_table(smp, am, data, ibdev, port,
  3566. resp_len, max_len);
  3567. break;
  3568. case IB_SMP_ATTR_LED_INFO:
  3569. ret = __subn_get_opa_led_info(smp, am, data, ibdev, port,
  3570. resp_len, max_len);
  3571. break;
  3572. case IB_SMP_ATTR_SM_INFO:
  3573. if (ibp->rvp.port_cap_flags & IB_PORT_SM_DISABLED)
  3574. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  3575. if (ibp->rvp.port_cap_flags & IB_PORT_SM)
  3576. return IB_MAD_RESULT_SUCCESS;
  3577. /* FALLTHROUGH */
  3578. default:
  3579. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  3580. ret = reply((struct ib_mad_hdr *)smp);
  3581. break;
  3582. }
  3583. return ret;
  3584. }
  3585. static int subn_set_opa_sma(__be16 attr_id, struct opa_smp *smp, u32 am,
  3586. u8 *data, struct ib_device *ibdev, u8 port,
  3587. u32 *resp_len, u32 max_len)
  3588. {
  3589. int ret;
  3590. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3591. switch (attr_id) {
  3592. case IB_SMP_ATTR_PORT_INFO:
  3593. ret = __subn_set_opa_portinfo(smp, am, data, ibdev, port,
  3594. resp_len, max_len);
  3595. break;
  3596. case IB_SMP_ATTR_PKEY_TABLE:
  3597. ret = __subn_set_opa_pkeytable(smp, am, data, ibdev, port,
  3598. resp_len, max_len);
  3599. break;
  3600. case OPA_ATTRIB_ID_SL_TO_SC_MAP:
  3601. ret = __subn_set_opa_sl_to_sc(smp, am, data, ibdev, port,
  3602. resp_len, max_len);
  3603. break;
  3604. case OPA_ATTRIB_ID_SC_TO_SL_MAP:
  3605. ret = __subn_set_opa_sc_to_sl(smp, am, data, ibdev, port,
  3606. resp_len, max_len);
  3607. break;
  3608. case OPA_ATTRIB_ID_SC_TO_VLT_MAP:
  3609. ret = __subn_set_opa_sc_to_vlt(smp, am, data, ibdev, port,
  3610. resp_len, max_len);
  3611. break;
  3612. case OPA_ATTRIB_ID_SC_TO_VLNT_MAP:
  3613. ret = __subn_set_opa_sc_to_vlnt(smp, am, data, ibdev, port,
  3614. resp_len, max_len);
  3615. break;
  3616. case OPA_ATTRIB_ID_PORT_STATE_INFO:
  3617. ret = __subn_set_opa_psi(smp, am, data, ibdev, port,
  3618. resp_len, max_len);
  3619. break;
  3620. case OPA_ATTRIB_ID_BUFFER_CONTROL_TABLE:
  3621. ret = __subn_set_opa_bct(smp, am, data, ibdev, port,
  3622. resp_len, max_len);
  3623. break;
  3624. case IB_SMP_ATTR_VL_ARB_TABLE:
  3625. ret = __subn_set_opa_vl_arb(smp, am, data, ibdev, port,
  3626. resp_len, max_len);
  3627. break;
  3628. case OPA_ATTRIB_ID_HFI_CONGESTION_SETTING:
  3629. ret = __subn_set_opa_cong_setting(smp, am, data, ibdev,
  3630. port, resp_len, max_len);
  3631. break;
  3632. case OPA_ATTRIB_ID_CONGESTION_CONTROL_TABLE:
  3633. ret = __subn_set_opa_cc_table(smp, am, data, ibdev, port,
  3634. resp_len, max_len);
  3635. break;
  3636. case IB_SMP_ATTR_LED_INFO:
  3637. ret = __subn_set_opa_led_info(smp, am, data, ibdev, port,
  3638. resp_len, max_len);
  3639. break;
  3640. case IB_SMP_ATTR_SM_INFO:
  3641. if (ibp->rvp.port_cap_flags & IB_PORT_SM_DISABLED)
  3642. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  3643. if (ibp->rvp.port_cap_flags & IB_PORT_SM)
  3644. return IB_MAD_RESULT_SUCCESS;
  3645. /* FALLTHROUGH */
  3646. default:
  3647. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  3648. ret = reply((struct ib_mad_hdr *)smp);
  3649. break;
  3650. }
  3651. return ret;
  3652. }
  3653. static inline void set_aggr_error(struct opa_aggregate *ag)
  3654. {
  3655. ag->err_reqlength |= cpu_to_be16(0x8000);
  3656. }
  3657. static int subn_get_opa_aggregate(struct opa_smp *smp,
  3658. struct ib_device *ibdev, u8 port,
  3659. u32 *resp_len)
  3660. {
  3661. int i;
  3662. u32 num_attr = be32_to_cpu(smp->attr_mod) & 0x000000ff;
  3663. u8 *next_smp = opa_get_smp_data(smp);
  3664. if (num_attr < 1 || num_attr > 117) {
  3665. smp->status |= IB_SMP_INVALID_FIELD;
  3666. return reply((struct ib_mad_hdr *)smp);
  3667. }
  3668. for (i = 0; i < num_attr; i++) {
  3669. struct opa_aggregate *agg;
  3670. size_t agg_data_len;
  3671. size_t agg_size;
  3672. u32 am;
  3673. agg = (struct opa_aggregate *)next_smp;
  3674. agg_data_len = (be16_to_cpu(agg->err_reqlength) & 0x007f) * 8;
  3675. agg_size = sizeof(*agg) + agg_data_len;
  3676. am = be32_to_cpu(agg->attr_mod);
  3677. *resp_len += agg_size;
  3678. if (next_smp + agg_size > ((u8 *)smp) + sizeof(*smp)) {
  3679. smp->status |= IB_SMP_INVALID_FIELD;
  3680. return reply((struct ib_mad_hdr *)smp);
  3681. }
  3682. /* zero the payload for this segment */
  3683. memset(next_smp + sizeof(*agg), 0, agg_data_len);
  3684. (void)subn_get_opa_sma(agg->attr_id, smp, am, agg->data,
  3685. ibdev, port, NULL, (u32)agg_data_len);
  3686. if (smp->status & IB_SMP_INVALID_FIELD)
  3687. break;
  3688. if (smp->status & ~IB_SMP_DIRECTION) {
  3689. set_aggr_error(agg);
  3690. return reply((struct ib_mad_hdr *)smp);
  3691. }
  3692. next_smp += agg_size;
  3693. }
  3694. return reply((struct ib_mad_hdr *)smp);
  3695. }
  3696. static int subn_set_opa_aggregate(struct opa_smp *smp,
  3697. struct ib_device *ibdev, u8 port,
  3698. u32 *resp_len)
  3699. {
  3700. int i;
  3701. u32 num_attr = be32_to_cpu(smp->attr_mod) & 0x000000ff;
  3702. u8 *next_smp = opa_get_smp_data(smp);
  3703. if (num_attr < 1 || num_attr > 117) {
  3704. smp->status |= IB_SMP_INVALID_FIELD;
  3705. return reply((struct ib_mad_hdr *)smp);
  3706. }
  3707. for (i = 0; i < num_attr; i++) {
  3708. struct opa_aggregate *agg;
  3709. size_t agg_data_len;
  3710. size_t agg_size;
  3711. u32 am;
  3712. agg = (struct opa_aggregate *)next_smp;
  3713. agg_data_len = (be16_to_cpu(agg->err_reqlength) & 0x007f) * 8;
  3714. agg_size = sizeof(*agg) + agg_data_len;
  3715. am = be32_to_cpu(agg->attr_mod);
  3716. *resp_len += agg_size;
  3717. if (next_smp + agg_size > ((u8 *)smp) + sizeof(*smp)) {
  3718. smp->status |= IB_SMP_INVALID_FIELD;
  3719. return reply((struct ib_mad_hdr *)smp);
  3720. }
  3721. (void)subn_set_opa_sma(agg->attr_id, smp, am, agg->data,
  3722. ibdev, port, NULL, (u32)agg_data_len);
  3723. if (smp->status & IB_SMP_INVALID_FIELD)
  3724. break;
  3725. if (smp->status & ~IB_SMP_DIRECTION) {
  3726. set_aggr_error(agg);
  3727. return reply((struct ib_mad_hdr *)smp);
  3728. }
  3729. next_smp += agg_size;
  3730. }
  3731. return reply((struct ib_mad_hdr *)smp);
  3732. }
  3733. /*
  3734. * OPAv1 specifies that, on the transition to link up, these counters
  3735. * are cleared:
  3736. * PortRcvErrors [*]
  3737. * LinkErrorRecovery
  3738. * LocalLinkIntegrityErrors
  3739. * ExcessiveBufferOverruns [*]
  3740. *
  3741. * [*] Error info associated with these counters is retained, but the
  3742. * error info status is reset to 0.
  3743. */
  3744. void clear_linkup_counters(struct hfi1_devdata *dd)
  3745. {
  3746. /* PortRcvErrors */
  3747. write_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL, 0);
  3748. dd->err_info_rcvport.status_and_code &= ~OPA_EI_STATUS_SMASK;
  3749. /* LinkErrorRecovery */
  3750. write_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL, 0);
  3751. write_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT, CNTR_INVALID_VL, 0);
  3752. /* LocalLinkIntegrityErrors */
  3753. write_dev_cntr(dd, C_DC_RX_REPLAY, CNTR_INVALID_VL, 0);
  3754. /* ExcessiveBufferOverruns */
  3755. write_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL, 0);
  3756. dd->rcv_ovfl_cnt = 0;
  3757. dd->err_info_xmit_constraint.status &= ~OPA_EI_STATUS_SMASK;
  3758. }
  3759. static int is_full_mgmt_pkey_in_table(struct hfi1_ibport *ibp)
  3760. {
  3761. unsigned int i;
  3762. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3763. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
  3764. if (ppd->pkeys[i] == FULL_MGMT_P_KEY)
  3765. return 1;
  3766. return 0;
  3767. }
  3768. /*
  3769. * is_local_mad() returns 1 if 'mad' is sent from, and destined to the
  3770. * local node, 0 otherwise.
  3771. */
  3772. static int is_local_mad(struct hfi1_ibport *ibp, const struct opa_mad *mad,
  3773. const struct ib_wc *in_wc)
  3774. {
  3775. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3776. const struct opa_smp *smp = (const struct opa_smp *)mad;
  3777. if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  3778. return (smp->hop_cnt == 0 &&
  3779. smp->route.dr.dr_slid == OPA_LID_PERMISSIVE &&
  3780. smp->route.dr.dr_dlid == OPA_LID_PERMISSIVE);
  3781. }
  3782. return (in_wc->slid == ppd->lid);
  3783. }
  3784. /*
  3785. * opa_local_smp_check() should only be called on MADs for which
  3786. * is_local_mad() returns true. It applies the SMP checks that are
  3787. * specific to SMPs which are sent from, and destined to this node.
  3788. * opa_local_smp_check() returns 0 if the SMP passes its checks, 1
  3789. * otherwise.
  3790. *
  3791. * SMPs which arrive from other nodes are instead checked by
  3792. * opa_smp_check().
  3793. */
  3794. static int opa_local_smp_check(struct hfi1_ibport *ibp,
  3795. const struct ib_wc *in_wc)
  3796. {
  3797. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3798. u16 pkey;
  3799. if (in_wc->pkey_index >= ARRAY_SIZE(ppd->pkeys))
  3800. return 1;
  3801. pkey = ppd->pkeys[in_wc->pkey_index];
  3802. /*
  3803. * We need to do the "node-local" checks specified in OPAv1,
  3804. * rev 0.90, section 9.10.26, which are:
  3805. * - pkey is 0x7fff, or 0xffff
  3806. * - Source QPN == 0 || Destination QPN == 0
  3807. * - the MAD header's management class is either
  3808. * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE or
  3809. * IB_MGMT_CLASS_SUBN_LID_ROUTED
  3810. * - SLID != 0
  3811. *
  3812. * However, we know (and so don't need to check again) that,
  3813. * for local SMPs, the MAD stack passes MADs with:
  3814. * - Source QPN of 0
  3815. * - MAD mgmt_class is IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
  3816. * - SLID is either: OPA_LID_PERMISSIVE (0xFFFFFFFF), or
  3817. * our own port's lid
  3818. *
  3819. */
  3820. if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY)
  3821. return 0;
  3822. ingress_pkey_table_fail(ppd, pkey, in_wc->slid);
  3823. return 1;
  3824. }
  3825. /**
  3826. * hfi1_pkey_validation_pma - It validates PKEYs for incoming PMA MAD packets.
  3827. * @ibp: IB port data
  3828. * @in_mad: MAD packet with header and data
  3829. * @in_wc: Work completion data such as source LID, port number, etc.
  3830. *
  3831. * These are all the possible logic rules for validating a pkey:
  3832. *
  3833. * a) If pkey neither FULL_MGMT_P_KEY nor LIM_MGMT_P_KEY,
  3834. * and NOT self-originated packet:
  3835. * Drop MAD packet as it should always be part of the
  3836. * management partition unless it's a self-originated packet.
  3837. *
  3838. * b) If pkey_index -> FULL_MGMT_P_KEY, and LIM_MGMT_P_KEY in pkey table:
  3839. * The packet is coming from a management node and the receiving node
  3840. * is also a management node, so it is safe for the packet to go through.
  3841. *
  3842. * c) If pkey_index -> FULL_MGMT_P_KEY, and LIM_MGMT_P_KEY is NOT in pkey table:
  3843. * Drop the packet as LIM_MGMT_P_KEY should always be in the pkey table.
  3844. * It could be an FM misconfiguration.
  3845. *
  3846. * d) If pkey_index -> LIM_MGMT_P_KEY and FULL_MGMT_P_KEY is NOT in pkey table:
  3847. * It is safe for the packet to go through since a non-management node is
  3848. * talking to another non-management node.
  3849. *
  3850. * e) If pkey_index -> LIM_MGMT_P_KEY and FULL_MGMT_P_KEY in pkey table:
  3851. * Drop the packet because a non-management node is talking to a
  3852. * management node, and it could be an attack.
  3853. *
  3854. * For the implementation, these rules can be simplied to only checking
  3855. * for (a) and (e). There's no need to check for rule (b) as
  3856. * the packet doesn't need to be dropped. Rule (c) is not possible in
  3857. * the driver as LIM_MGMT_P_KEY is always in the pkey table.
  3858. *
  3859. * Return:
  3860. * 0 - pkey is okay, -EINVAL it's a bad pkey
  3861. */
  3862. static int hfi1_pkey_validation_pma(struct hfi1_ibport *ibp,
  3863. const struct opa_mad *in_mad,
  3864. const struct ib_wc *in_wc)
  3865. {
  3866. u16 pkey_value = hfi1_lookup_pkey_value(ibp, in_wc->pkey_index);
  3867. /* Rule (a) from above */
  3868. if (!is_local_mad(ibp, in_mad, in_wc) &&
  3869. pkey_value != LIM_MGMT_P_KEY &&
  3870. pkey_value != FULL_MGMT_P_KEY)
  3871. return -EINVAL;
  3872. /* Rule (e) from above */
  3873. if (pkey_value == LIM_MGMT_P_KEY &&
  3874. is_full_mgmt_pkey_in_table(ibp))
  3875. return -EINVAL;
  3876. return 0;
  3877. }
  3878. static int process_subn_opa(struct ib_device *ibdev, int mad_flags,
  3879. u8 port, const struct opa_mad *in_mad,
  3880. struct opa_mad *out_mad,
  3881. u32 *resp_len)
  3882. {
  3883. struct opa_smp *smp = (struct opa_smp *)out_mad;
  3884. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3885. u8 *data;
  3886. u32 am, data_size;
  3887. __be16 attr_id;
  3888. int ret;
  3889. *out_mad = *in_mad;
  3890. data = opa_get_smp_data(smp);
  3891. data_size = (u32)opa_get_smp_data_size(smp);
  3892. am = be32_to_cpu(smp->attr_mod);
  3893. attr_id = smp->attr_id;
  3894. if (smp->class_version != OPA_SM_CLASS_VERSION) {
  3895. smp->status |= IB_SMP_UNSUP_VERSION;
  3896. ret = reply((struct ib_mad_hdr *)smp);
  3897. return ret;
  3898. }
  3899. ret = check_mkey(ibp, (struct ib_mad_hdr *)smp, mad_flags, smp->mkey,
  3900. smp->route.dr.dr_slid, smp->route.dr.return_path,
  3901. smp->hop_cnt);
  3902. if (ret) {
  3903. u32 port_num = be32_to_cpu(smp->attr_mod);
  3904. /*
  3905. * If this is a get/set portinfo, we already check the
  3906. * M_Key if the MAD is for another port and the M_Key
  3907. * is OK on the receiving port. This check is needed
  3908. * to increment the error counters when the M_Key
  3909. * fails to match on *both* ports.
  3910. */
  3911. if (attr_id == IB_SMP_ATTR_PORT_INFO &&
  3912. (smp->method == IB_MGMT_METHOD_GET ||
  3913. smp->method == IB_MGMT_METHOD_SET) &&
  3914. port_num && port_num <= ibdev->phys_port_cnt &&
  3915. port != port_num)
  3916. (void)check_mkey(to_iport(ibdev, port_num),
  3917. (struct ib_mad_hdr *)smp, 0,
  3918. smp->mkey, smp->route.dr.dr_slid,
  3919. smp->route.dr.return_path,
  3920. smp->hop_cnt);
  3921. ret = IB_MAD_RESULT_FAILURE;
  3922. return ret;
  3923. }
  3924. *resp_len = opa_get_smp_header_size(smp);
  3925. switch (smp->method) {
  3926. case IB_MGMT_METHOD_GET:
  3927. switch (attr_id) {
  3928. default:
  3929. clear_opa_smp_data(smp);
  3930. ret = subn_get_opa_sma(attr_id, smp, am, data,
  3931. ibdev, port, resp_len,
  3932. data_size);
  3933. break;
  3934. case OPA_ATTRIB_ID_AGGREGATE:
  3935. ret = subn_get_opa_aggregate(smp, ibdev, port,
  3936. resp_len);
  3937. break;
  3938. }
  3939. break;
  3940. case IB_MGMT_METHOD_SET:
  3941. switch (attr_id) {
  3942. default:
  3943. ret = subn_set_opa_sma(attr_id, smp, am, data,
  3944. ibdev, port, resp_len,
  3945. data_size);
  3946. break;
  3947. case OPA_ATTRIB_ID_AGGREGATE:
  3948. ret = subn_set_opa_aggregate(smp, ibdev, port,
  3949. resp_len);
  3950. break;
  3951. }
  3952. break;
  3953. case IB_MGMT_METHOD_TRAP:
  3954. case IB_MGMT_METHOD_REPORT:
  3955. case IB_MGMT_METHOD_REPORT_RESP:
  3956. case IB_MGMT_METHOD_GET_RESP:
  3957. /*
  3958. * The ib_mad module will call us to process responses
  3959. * before checking for other consumers.
  3960. * Just tell the caller to process it normally.
  3961. */
  3962. ret = IB_MAD_RESULT_SUCCESS;
  3963. break;
  3964. case IB_MGMT_METHOD_TRAP_REPRESS:
  3965. subn_handle_opa_trap_repress(ibp, smp);
  3966. /* Always successful */
  3967. ret = IB_MAD_RESULT_SUCCESS;
  3968. break;
  3969. default:
  3970. smp->status |= IB_SMP_UNSUP_METHOD;
  3971. ret = reply((struct ib_mad_hdr *)smp);
  3972. break;
  3973. }
  3974. return ret;
  3975. }
  3976. static int process_subn(struct ib_device *ibdev, int mad_flags,
  3977. u8 port, const struct ib_mad *in_mad,
  3978. struct ib_mad *out_mad)
  3979. {
  3980. struct ib_smp *smp = (struct ib_smp *)out_mad;
  3981. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3982. int ret;
  3983. *out_mad = *in_mad;
  3984. if (smp->class_version != 1) {
  3985. smp->status |= IB_SMP_UNSUP_VERSION;
  3986. ret = reply((struct ib_mad_hdr *)smp);
  3987. return ret;
  3988. }
  3989. ret = check_mkey(ibp, (struct ib_mad_hdr *)smp, mad_flags,
  3990. smp->mkey, (__force __be32)smp->dr_slid,
  3991. smp->return_path, smp->hop_cnt);
  3992. if (ret) {
  3993. u32 port_num = be32_to_cpu(smp->attr_mod);
  3994. /*
  3995. * If this is a get/set portinfo, we already check the
  3996. * M_Key if the MAD is for another port and the M_Key
  3997. * is OK on the receiving port. This check is needed
  3998. * to increment the error counters when the M_Key
  3999. * fails to match on *both* ports.
  4000. */
  4001. if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
  4002. (smp->method == IB_MGMT_METHOD_GET ||
  4003. smp->method == IB_MGMT_METHOD_SET) &&
  4004. port_num && port_num <= ibdev->phys_port_cnt &&
  4005. port != port_num)
  4006. (void)check_mkey(to_iport(ibdev, port_num),
  4007. (struct ib_mad_hdr *)smp, 0,
  4008. smp->mkey,
  4009. (__force __be32)smp->dr_slid,
  4010. smp->return_path, smp->hop_cnt);
  4011. ret = IB_MAD_RESULT_FAILURE;
  4012. return ret;
  4013. }
  4014. switch (smp->method) {
  4015. case IB_MGMT_METHOD_GET:
  4016. switch (smp->attr_id) {
  4017. case IB_SMP_ATTR_NODE_INFO:
  4018. ret = subn_get_nodeinfo(smp, ibdev, port);
  4019. break;
  4020. default:
  4021. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  4022. ret = reply((struct ib_mad_hdr *)smp);
  4023. break;
  4024. }
  4025. break;
  4026. }
  4027. return ret;
  4028. }
  4029. static int process_perf(struct ib_device *ibdev, u8 port,
  4030. const struct ib_mad *in_mad,
  4031. struct ib_mad *out_mad)
  4032. {
  4033. struct ib_pma_mad *pmp = (struct ib_pma_mad *)out_mad;
  4034. struct ib_class_port_info *cpi = (struct ib_class_port_info *)
  4035. &pmp->data;
  4036. int ret = IB_MAD_RESULT_FAILURE;
  4037. *out_mad = *in_mad;
  4038. if (pmp->mad_hdr.class_version != 1) {
  4039. pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
  4040. ret = reply((struct ib_mad_hdr *)pmp);
  4041. return ret;
  4042. }
  4043. switch (pmp->mad_hdr.method) {
  4044. case IB_MGMT_METHOD_GET:
  4045. switch (pmp->mad_hdr.attr_id) {
  4046. case IB_PMA_PORT_COUNTERS:
  4047. ret = pma_get_ib_portcounters(pmp, ibdev, port);
  4048. break;
  4049. case IB_PMA_PORT_COUNTERS_EXT:
  4050. ret = pma_get_ib_portcounters_ext(pmp, ibdev, port);
  4051. break;
  4052. case IB_PMA_CLASS_PORT_INFO:
  4053. cpi->capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
  4054. ret = reply((struct ib_mad_hdr *)pmp);
  4055. break;
  4056. default:
  4057. pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
  4058. ret = reply((struct ib_mad_hdr *)pmp);
  4059. break;
  4060. }
  4061. break;
  4062. case IB_MGMT_METHOD_SET:
  4063. if (pmp->mad_hdr.attr_id) {
  4064. pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
  4065. ret = reply((struct ib_mad_hdr *)pmp);
  4066. }
  4067. break;
  4068. case IB_MGMT_METHOD_TRAP:
  4069. case IB_MGMT_METHOD_GET_RESP:
  4070. /*
  4071. * The ib_mad module will call us to process responses
  4072. * before checking for other consumers.
  4073. * Just tell the caller to process it normally.
  4074. */
  4075. ret = IB_MAD_RESULT_SUCCESS;
  4076. break;
  4077. default:
  4078. pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
  4079. ret = reply((struct ib_mad_hdr *)pmp);
  4080. break;
  4081. }
  4082. return ret;
  4083. }
  4084. static int process_perf_opa(struct ib_device *ibdev, u8 port,
  4085. const struct opa_mad *in_mad,
  4086. struct opa_mad *out_mad, u32 *resp_len)
  4087. {
  4088. struct opa_pma_mad *pmp = (struct opa_pma_mad *)out_mad;
  4089. int ret;
  4090. *out_mad = *in_mad;
  4091. if (pmp->mad_hdr.class_version != OPA_SM_CLASS_VERSION) {
  4092. pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
  4093. return reply((struct ib_mad_hdr *)pmp);
  4094. }
  4095. *resp_len = sizeof(pmp->mad_hdr);
  4096. switch (pmp->mad_hdr.method) {
  4097. case IB_MGMT_METHOD_GET:
  4098. switch (pmp->mad_hdr.attr_id) {
  4099. case IB_PMA_CLASS_PORT_INFO:
  4100. ret = pma_get_opa_classportinfo(pmp, ibdev, resp_len);
  4101. break;
  4102. case OPA_PM_ATTRIB_ID_PORT_STATUS:
  4103. ret = pma_get_opa_portstatus(pmp, ibdev, port,
  4104. resp_len);
  4105. break;
  4106. case OPA_PM_ATTRIB_ID_DATA_PORT_COUNTERS:
  4107. ret = pma_get_opa_datacounters(pmp, ibdev, port,
  4108. resp_len);
  4109. break;
  4110. case OPA_PM_ATTRIB_ID_ERROR_PORT_COUNTERS:
  4111. ret = pma_get_opa_porterrors(pmp, ibdev, port,
  4112. resp_len);
  4113. break;
  4114. case OPA_PM_ATTRIB_ID_ERROR_INFO:
  4115. ret = pma_get_opa_errorinfo(pmp, ibdev, port,
  4116. resp_len);
  4117. break;
  4118. default:
  4119. pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
  4120. ret = reply((struct ib_mad_hdr *)pmp);
  4121. break;
  4122. }
  4123. break;
  4124. case IB_MGMT_METHOD_SET:
  4125. switch (pmp->mad_hdr.attr_id) {
  4126. case OPA_PM_ATTRIB_ID_CLEAR_PORT_STATUS:
  4127. ret = pma_set_opa_portstatus(pmp, ibdev, port,
  4128. resp_len);
  4129. break;
  4130. case OPA_PM_ATTRIB_ID_ERROR_INFO:
  4131. ret = pma_set_opa_errorinfo(pmp, ibdev, port,
  4132. resp_len);
  4133. break;
  4134. default:
  4135. pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
  4136. ret = reply((struct ib_mad_hdr *)pmp);
  4137. break;
  4138. }
  4139. break;
  4140. case IB_MGMT_METHOD_TRAP:
  4141. case IB_MGMT_METHOD_GET_RESP:
  4142. /*
  4143. * The ib_mad module will call us to process responses
  4144. * before checking for other consumers.
  4145. * Just tell the caller to process it normally.
  4146. */
  4147. ret = IB_MAD_RESULT_SUCCESS;
  4148. break;
  4149. default:
  4150. pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
  4151. ret = reply((struct ib_mad_hdr *)pmp);
  4152. break;
  4153. }
  4154. return ret;
  4155. }
  4156. static int hfi1_process_opa_mad(struct ib_device *ibdev, int mad_flags,
  4157. u8 port, const struct ib_wc *in_wc,
  4158. const struct ib_grh *in_grh,
  4159. const struct opa_mad *in_mad,
  4160. struct opa_mad *out_mad, size_t *out_mad_size,
  4161. u16 *out_mad_pkey_index)
  4162. {
  4163. int ret;
  4164. int pkey_idx;
  4165. u32 resp_len = 0;
  4166. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  4167. pkey_idx = hfi1_lookup_pkey_idx(ibp, LIM_MGMT_P_KEY);
  4168. if (pkey_idx < 0) {
  4169. pr_warn("failed to find limited mgmt pkey, defaulting 0x%x\n",
  4170. hfi1_get_pkey(ibp, 1));
  4171. pkey_idx = 1;
  4172. }
  4173. *out_mad_pkey_index = (u16)pkey_idx;
  4174. switch (in_mad->mad_hdr.mgmt_class) {
  4175. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  4176. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  4177. if (is_local_mad(ibp, in_mad, in_wc)) {
  4178. ret = opa_local_smp_check(ibp, in_wc);
  4179. if (ret)
  4180. return IB_MAD_RESULT_FAILURE;
  4181. }
  4182. ret = process_subn_opa(ibdev, mad_flags, port, in_mad,
  4183. out_mad, &resp_len);
  4184. goto bail;
  4185. case IB_MGMT_CLASS_PERF_MGMT:
  4186. ret = hfi1_pkey_validation_pma(ibp, in_mad, in_wc);
  4187. if (ret)
  4188. return IB_MAD_RESULT_FAILURE;
  4189. ret = process_perf_opa(ibdev, port, in_mad, out_mad, &resp_len);
  4190. goto bail;
  4191. default:
  4192. ret = IB_MAD_RESULT_SUCCESS;
  4193. }
  4194. bail:
  4195. if (ret & IB_MAD_RESULT_REPLY)
  4196. *out_mad_size = round_up(resp_len, 8);
  4197. else if (ret & IB_MAD_RESULT_SUCCESS)
  4198. *out_mad_size = in_wc->byte_len - sizeof(struct ib_grh);
  4199. return ret;
  4200. }
  4201. static int hfi1_process_ib_mad(struct ib_device *ibdev, int mad_flags, u8 port,
  4202. const struct ib_wc *in_wc,
  4203. const struct ib_grh *in_grh,
  4204. const struct ib_mad *in_mad,
  4205. struct ib_mad *out_mad)
  4206. {
  4207. int ret;
  4208. switch (in_mad->mad_hdr.mgmt_class) {
  4209. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  4210. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  4211. ret = process_subn(ibdev, mad_flags, port, in_mad, out_mad);
  4212. break;
  4213. case IB_MGMT_CLASS_PERF_MGMT:
  4214. ret = process_perf(ibdev, port, in_mad, out_mad);
  4215. break;
  4216. default:
  4217. ret = IB_MAD_RESULT_SUCCESS;
  4218. break;
  4219. }
  4220. return ret;
  4221. }
  4222. /**
  4223. * hfi1_process_mad - process an incoming MAD packet
  4224. * @ibdev: the infiniband device this packet came in on
  4225. * @mad_flags: MAD flags
  4226. * @port: the port number this packet came in on
  4227. * @in_wc: the work completion entry for this packet
  4228. * @in_grh: the global route header for this packet
  4229. * @in_mad: the incoming MAD
  4230. * @out_mad: any outgoing MAD reply
  4231. *
  4232. * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
  4233. * interested in processing.
  4234. *
  4235. * Note that the verbs framework has already done the MAD sanity checks,
  4236. * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
  4237. * MADs.
  4238. *
  4239. * This is called by the ib_mad module.
  4240. */
  4241. int hfi1_process_mad(struct ib_device *ibdev, int mad_flags, u8 port,
  4242. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  4243. const struct ib_mad_hdr *in_mad, size_t in_mad_size,
  4244. struct ib_mad_hdr *out_mad, size_t *out_mad_size,
  4245. u16 *out_mad_pkey_index)
  4246. {
  4247. switch (in_mad->base_version) {
  4248. case OPA_MGMT_BASE_VERSION:
  4249. if (unlikely(in_mad_size != sizeof(struct opa_mad))) {
  4250. dev_err(ibdev->dev.parent, "invalid in_mad_size\n");
  4251. return IB_MAD_RESULT_FAILURE;
  4252. }
  4253. return hfi1_process_opa_mad(ibdev, mad_flags, port,
  4254. in_wc, in_grh,
  4255. (struct opa_mad *)in_mad,
  4256. (struct opa_mad *)out_mad,
  4257. out_mad_size,
  4258. out_mad_pkey_index);
  4259. case IB_MGMT_BASE_VERSION:
  4260. return hfi1_process_ib_mad(ibdev, mad_flags, port,
  4261. in_wc, in_grh,
  4262. (const struct ib_mad *)in_mad,
  4263. (struct ib_mad *)out_mad);
  4264. default:
  4265. break;
  4266. }
  4267. return IB_MAD_RESULT_FAILURE;
  4268. }