mad.c 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948
  1. /*
  2. * Copyright(c) 2015-2018 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, int local_mad)
  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 && !local_mad)
  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, int local_mad)
  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, local_mad);
  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, int local_mad)
  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, local_mad);
  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 0x00000000000080ffUL
  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)
  2264. {
  2265. if (!is_bx(ppd->dd)) {
  2266. unsigned long vl;
  2267. u64 sum_vl_xmit_wait = 0;
  2268. unsigned long vl_all_mask = VL_MASK_ALL;
  2269. for_each_set_bit(vl, &vl_all_mask, BITS_PER_LONG) {
  2270. u64 tmp = sum_vl_xmit_wait +
  2271. read_port_cntr(ppd, C_TX_WAIT_VL,
  2272. idx_from_vl(vl));
  2273. if (tmp < sum_vl_xmit_wait) {
  2274. /* we wrapped */
  2275. sum_vl_xmit_wait = (u64)~0;
  2276. break;
  2277. }
  2278. sum_vl_xmit_wait = tmp;
  2279. }
  2280. if (be64_to_cpu(rsp->port_xmit_wait) > sum_vl_xmit_wait)
  2281. rsp->port_xmit_wait = cpu_to_be64(sum_vl_xmit_wait);
  2282. }
  2283. }
  2284. /**
  2285. * tx_link_width - convert link width bitmask to integer
  2286. * value representing actual link width.
  2287. * @link_width: width of active link
  2288. * @return: return index of the bit set in link_width var
  2289. *
  2290. * The function convert and return the index of bit set
  2291. * that indicate the current link width.
  2292. */
  2293. u16 tx_link_width(u16 link_width)
  2294. {
  2295. int n = LINK_WIDTH_DEFAULT;
  2296. u16 tx_width = n;
  2297. while (link_width && n) {
  2298. if (link_width & (1 << (n - 1))) {
  2299. tx_width = n;
  2300. break;
  2301. }
  2302. n--;
  2303. }
  2304. return tx_width;
  2305. }
  2306. /**
  2307. * get_xmit_wait_counters - Convert HFI 's SendWaitCnt/SendWaitVlCnt
  2308. * counter in unit of TXE cycle times to flit times.
  2309. * @ppd: info of physical Hfi port
  2310. * @link_width: width of active link
  2311. * @link_speed: speed of active link
  2312. * @vl: represent VL0-VL7, VL15 for PortVLXmitWait counters request
  2313. * and if vl value is C_VL_COUNT, it represent SendWaitCnt
  2314. * counter request
  2315. * @return: return SendWaitCnt/SendWaitVlCnt counter value per vl.
  2316. *
  2317. * Convert SendWaitCnt/SendWaitVlCnt counter from TXE cycle times to
  2318. * flit times. Call this function to samples these counters. This
  2319. * function will calculate for previous state transition and update
  2320. * current state at end of function using ppd->prev_link_width and
  2321. * ppd->port_vl_xmit_wait_last to port_vl_xmit_wait_curr and link_width.
  2322. */
  2323. u64 get_xmit_wait_counters(struct hfi1_pportdata *ppd,
  2324. u16 link_width, u16 link_speed, int vl)
  2325. {
  2326. u64 port_vl_xmit_wait_curr;
  2327. u64 delta_vl_xmit_wait;
  2328. u64 xmit_wait_val;
  2329. if (vl > C_VL_COUNT)
  2330. return 0;
  2331. if (vl < C_VL_COUNT)
  2332. port_vl_xmit_wait_curr =
  2333. read_port_cntr(ppd, C_TX_WAIT_VL, vl);
  2334. else
  2335. port_vl_xmit_wait_curr =
  2336. read_port_cntr(ppd, C_TX_WAIT, CNTR_INVALID_VL);
  2337. xmit_wait_val =
  2338. port_vl_xmit_wait_curr -
  2339. ppd->port_vl_xmit_wait_last[vl];
  2340. delta_vl_xmit_wait =
  2341. convert_xmit_counter(xmit_wait_val,
  2342. ppd->prev_link_width,
  2343. link_speed);
  2344. ppd->vl_xmit_flit_cnt[vl] += delta_vl_xmit_wait;
  2345. ppd->port_vl_xmit_wait_last[vl] = port_vl_xmit_wait_curr;
  2346. ppd->prev_link_width = link_width;
  2347. return ppd->vl_xmit_flit_cnt[vl];
  2348. }
  2349. static int pma_get_opa_portstatus(struct opa_pma_mad *pmp,
  2350. struct ib_device *ibdev,
  2351. u8 port, u32 *resp_len)
  2352. {
  2353. struct opa_port_status_req *req =
  2354. (struct opa_port_status_req *)pmp->data;
  2355. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2356. struct opa_port_status_rsp *rsp;
  2357. unsigned long vl_select_mask = be32_to_cpu(req->vl_select_mask);
  2358. unsigned long vl;
  2359. size_t response_data_size;
  2360. u32 nports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
  2361. u8 port_num = req->port_num;
  2362. u8 num_vls = hweight64(vl_select_mask);
  2363. struct _vls_pctrs *vlinfo;
  2364. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  2365. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  2366. int vfi;
  2367. u64 tmp, tmp2;
  2368. u16 link_width;
  2369. u16 link_speed;
  2370. response_data_size = sizeof(struct opa_port_status_rsp) +
  2371. num_vls * sizeof(struct _vls_pctrs);
  2372. if (response_data_size > sizeof(pmp->data)) {
  2373. pmp->mad_hdr.status |= OPA_PM_STATUS_REQUEST_TOO_LARGE;
  2374. return reply((struct ib_mad_hdr *)pmp);
  2375. }
  2376. if (nports != 1 || (port_num && port_num != port) ||
  2377. num_vls > OPA_MAX_VLS || (vl_select_mask & ~VL_MASK_ALL)) {
  2378. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2379. return reply((struct ib_mad_hdr *)pmp);
  2380. }
  2381. memset(pmp->data, 0, sizeof(pmp->data));
  2382. rsp = (struct opa_port_status_rsp *)pmp->data;
  2383. if (port_num)
  2384. rsp->port_num = port_num;
  2385. else
  2386. rsp->port_num = port;
  2387. rsp->port_rcv_constraint_errors =
  2388. cpu_to_be64(read_port_cntr(ppd, C_SW_RCV_CSTR_ERR,
  2389. CNTR_INVALID_VL));
  2390. hfi1_read_link_quality(dd, &rsp->link_quality_indicator);
  2391. rsp->vl_select_mask = cpu_to_be32((u32)vl_select_mask);
  2392. rsp->port_xmit_data = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_FLITS,
  2393. CNTR_INVALID_VL));
  2394. rsp->port_rcv_data = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FLITS,
  2395. CNTR_INVALID_VL));
  2396. rsp->port_xmit_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_PKTS,
  2397. CNTR_INVALID_VL));
  2398. rsp->port_rcv_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_PKTS,
  2399. CNTR_INVALID_VL));
  2400. rsp->port_multicast_xmit_pkts =
  2401. cpu_to_be64(read_dev_cntr(dd, C_DC_MC_XMIT_PKTS,
  2402. CNTR_INVALID_VL));
  2403. rsp->port_multicast_rcv_pkts =
  2404. cpu_to_be64(read_dev_cntr(dd, C_DC_MC_RCV_PKTS,
  2405. CNTR_INVALID_VL));
  2406. /*
  2407. * Convert PortXmitWait counter from TXE cycle times
  2408. * to flit times.
  2409. */
  2410. link_width =
  2411. tx_link_width(ppd->link_width_downgrade_tx_active);
  2412. link_speed = get_link_speed(ppd->link_speed_active);
  2413. rsp->port_xmit_wait =
  2414. cpu_to_be64(get_xmit_wait_counters(ppd, link_width,
  2415. link_speed, C_VL_COUNT));
  2416. rsp->port_rcv_fecn =
  2417. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN, CNTR_INVALID_VL));
  2418. rsp->port_rcv_becn =
  2419. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN, CNTR_INVALID_VL));
  2420. rsp->port_xmit_discards =
  2421. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD,
  2422. CNTR_INVALID_VL));
  2423. rsp->port_xmit_constraint_errors =
  2424. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_CSTR_ERR,
  2425. CNTR_INVALID_VL));
  2426. rsp->port_rcv_remote_physical_errors =
  2427. cpu_to_be64(read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
  2428. CNTR_INVALID_VL));
  2429. rsp->local_link_integrity_errors =
  2430. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_REPLAY,
  2431. CNTR_INVALID_VL));
  2432. tmp = read_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL);
  2433. tmp2 = tmp + read_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT,
  2434. CNTR_INVALID_VL);
  2435. if (tmp2 > (u32)UINT_MAX || tmp2 < tmp) {
  2436. /* overflow/wrapped */
  2437. rsp->link_error_recovery = cpu_to_be32(~0);
  2438. } else {
  2439. rsp->link_error_recovery = cpu_to_be32(tmp2);
  2440. }
  2441. rsp->port_rcv_errors =
  2442. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL));
  2443. rsp->excessive_buffer_overruns =
  2444. cpu_to_be64(read_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL));
  2445. rsp->fm_config_errors =
  2446. cpu_to_be64(read_dev_cntr(dd, C_DC_FM_CFG_ERR,
  2447. CNTR_INVALID_VL));
  2448. rsp->link_downed = cpu_to_be32(read_port_cntr(ppd, C_SW_LINK_DOWN,
  2449. CNTR_INVALID_VL));
  2450. /* rsp->uncorrectable_errors is 8 bits wide, and it pegs at 0xff */
  2451. tmp = read_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL);
  2452. rsp->uncorrectable_errors = tmp < 0x100 ? (tmp & 0xff) : 0xff;
  2453. vlinfo = &rsp->vls[0];
  2454. vfi = 0;
  2455. /* The vl_select_mask has been checked above, and we know
  2456. * that it contains only entries which represent valid VLs.
  2457. * So in the for_each_set_bit() loop below, we don't need
  2458. * any additional checks for vl.
  2459. */
  2460. for_each_set_bit(vl, &vl_select_mask, BITS_PER_LONG) {
  2461. memset(vlinfo, 0, sizeof(*vlinfo));
  2462. tmp = read_dev_cntr(dd, C_DC_RX_FLIT_VL, idx_from_vl(vl));
  2463. rsp->vls[vfi].port_vl_rcv_data = cpu_to_be64(tmp);
  2464. rsp->vls[vfi].port_vl_rcv_pkts =
  2465. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_PKT_VL,
  2466. idx_from_vl(vl)));
  2467. rsp->vls[vfi].port_vl_xmit_data =
  2468. cpu_to_be64(read_port_cntr(ppd, C_TX_FLIT_VL,
  2469. idx_from_vl(vl)));
  2470. rsp->vls[vfi].port_vl_xmit_pkts =
  2471. cpu_to_be64(read_port_cntr(ppd, C_TX_PKT_VL,
  2472. idx_from_vl(vl)));
  2473. /*
  2474. * Convert PortVlXmitWait counter from TXE cycle
  2475. * times to flit times.
  2476. */
  2477. rsp->vls[vfi].port_vl_xmit_wait =
  2478. cpu_to_be64(get_xmit_wait_counters(ppd, link_width,
  2479. link_speed,
  2480. idx_from_vl(vl)));
  2481. rsp->vls[vfi].port_vl_rcv_fecn =
  2482. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN_VL,
  2483. idx_from_vl(vl)));
  2484. rsp->vls[vfi].port_vl_rcv_becn =
  2485. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN_VL,
  2486. idx_from_vl(vl)));
  2487. rsp->vls[vfi].port_vl_xmit_discards =
  2488. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD_VL,
  2489. idx_from_vl(vl)));
  2490. vlinfo++;
  2491. vfi++;
  2492. }
  2493. a0_portstatus(ppd, rsp);
  2494. if (resp_len)
  2495. *resp_len += response_data_size;
  2496. return reply((struct ib_mad_hdr *)pmp);
  2497. }
  2498. static u64 get_error_counter_summary(struct ib_device *ibdev, u8 port,
  2499. u8 res_lli, u8 res_ler)
  2500. {
  2501. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2502. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  2503. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  2504. u64 error_counter_summary = 0, tmp;
  2505. error_counter_summary += read_port_cntr(ppd, C_SW_RCV_CSTR_ERR,
  2506. CNTR_INVALID_VL);
  2507. /* port_rcv_switch_relay_errors is 0 for HFIs */
  2508. error_counter_summary += read_port_cntr(ppd, C_SW_XMIT_DSCD,
  2509. CNTR_INVALID_VL);
  2510. error_counter_summary += read_port_cntr(ppd, C_SW_XMIT_CSTR_ERR,
  2511. CNTR_INVALID_VL);
  2512. error_counter_summary += read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
  2513. CNTR_INVALID_VL);
  2514. /* local link integrity must be right-shifted by the lli resolution */
  2515. error_counter_summary += (read_dev_cntr(dd, C_DC_RX_REPLAY,
  2516. CNTR_INVALID_VL) >> res_lli);
  2517. /* link error recovery must b right-shifted by the ler resolution */
  2518. tmp = read_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL);
  2519. tmp += read_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT, CNTR_INVALID_VL);
  2520. error_counter_summary += (tmp >> res_ler);
  2521. error_counter_summary += read_dev_cntr(dd, C_DC_RCV_ERR,
  2522. CNTR_INVALID_VL);
  2523. error_counter_summary += read_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL);
  2524. error_counter_summary += read_dev_cntr(dd, C_DC_FM_CFG_ERR,
  2525. CNTR_INVALID_VL);
  2526. /* ppd->link_downed is a 32-bit value */
  2527. error_counter_summary += read_port_cntr(ppd, C_SW_LINK_DOWN,
  2528. CNTR_INVALID_VL);
  2529. tmp = read_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL);
  2530. /* this is an 8-bit quantity */
  2531. error_counter_summary += tmp < 0x100 ? (tmp & 0xff) : 0xff;
  2532. return error_counter_summary;
  2533. }
  2534. static void a0_datacounters(struct hfi1_pportdata *ppd, struct _port_dctrs *rsp)
  2535. {
  2536. if (!is_bx(ppd->dd)) {
  2537. unsigned long vl;
  2538. u64 sum_vl_xmit_wait = 0;
  2539. unsigned long vl_all_mask = VL_MASK_ALL;
  2540. for_each_set_bit(vl, &vl_all_mask, BITS_PER_LONG) {
  2541. u64 tmp = sum_vl_xmit_wait +
  2542. read_port_cntr(ppd, C_TX_WAIT_VL,
  2543. idx_from_vl(vl));
  2544. if (tmp < sum_vl_xmit_wait) {
  2545. /* we wrapped */
  2546. sum_vl_xmit_wait = (u64)~0;
  2547. break;
  2548. }
  2549. sum_vl_xmit_wait = tmp;
  2550. }
  2551. if (be64_to_cpu(rsp->port_xmit_wait) > sum_vl_xmit_wait)
  2552. rsp->port_xmit_wait = cpu_to_be64(sum_vl_xmit_wait);
  2553. }
  2554. }
  2555. static void pma_get_opa_port_dctrs(struct ib_device *ibdev,
  2556. struct _port_dctrs *rsp)
  2557. {
  2558. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2559. rsp->port_xmit_data = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_FLITS,
  2560. CNTR_INVALID_VL));
  2561. rsp->port_rcv_data = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FLITS,
  2562. CNTR_INVALID_VL));
  2563. rsp->port_xmit_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_PKTS,
  2564. CNTR_INVALID_VL));
  2565. rsp->port_rcv_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_PKTS,
  2566. CNTR_INVALID_VL));
  2567. rsp->port_multicast_xmit_pkts =
  2568. cpu_to_be64(read_dev_cntr(dd, C_DC_MC_XMIT_PKTS,
  2569. CNTR_INVALID_VL));
  2570. rsp->port_multicast_rcv_pkts =
  2571. cpu_to_be64(read_dev_cntr(dd, C_DC_MC_RCV_PKTS,
  2572. CNTR_INVALID_VL));
  2573. }
  2574. static int pma_get_opa_datacounters(struct opa_pma_mad *pmp,
  2575. struct ib_device *ibdev,
  2576. u8 port, u32 *resp_len)
  2577. {
  2578. struct opa_port_data_counters_msg *req =
  2579. (struct opa_port_data_counters_msg *)pmp->data;
  2580. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2581. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  2582. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  2583. struct _port_dctrs *rsp;
  2584. struct _vls_dctrs *vlinfo;
  2585. size_t response_data_size;
  2586. u32 num_ports;
  2587. u8 lq, num_vls;
  2588. u8 res_lli, res_ler;
  2589. u64 port_mask;
  2590. u8 port_num;
  2591. unsigned long vl;
  2592. unsigned long vl_select_mask;
  2593. int vfi;
  2594. u16 link_width;
  2595. u16 link_speed;
  2596. num_ports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
  2597. num_vls = hweight32(be32_to_cpu(req->vl_select_mask));
  2598. vl_select_mask = be32_to_cpu(req->vl_select_mask);
  2599. res_lli = (u8)(be32_to_cpu(req->resolution) & MSK_LLI) >> MSK_LLI_SFT;
  2600. res_lli = res_lli ? res_lli + ADD_LLI : 0;
  2601. res_ler = (u8)(be32_to_cpu(req->resolution) & MSK_LER) >> MSK_LER_SFT;
  2602. res_ler = res_ler ? res_ler + ADD_LER : 0;
  2603. if (num_ports != 1 || (vl_select_mask & ~VL_MASK_ALL)) {
  2604. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2605. return reply((struct ib_mad_hdr *)pmp);
  2606. }
  2607. /* Sanity check */
  2608. response_data_size = sizeof(struct opa_port_data_counters_msg) +
  2609. num_vls * sizeof(struct _vls_dctrs);
  2610. if (response_data_size > sizeof(pmp->data)) {
  2611. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2612. return reply((struct ib_mad_hdr *)pmp);
  2613. }
  2614. /*
  2615. * The bit set in the mask needs to be consistent with the
  2616. * port the request came in on.
  2617. */
  2618. port_mask = be64_to_cpu(req->port_select_mask[3]);
  2619. port_num = find_first_bit((unsigned long *)&port_mask,
  2620. sizeof(port_mask) * 8);
  2621. if (port_num != port) {
  2622. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2623. return reply((struct ib_mad_hdr *)pmp);
  2624. }
  2625. rsp = &req->port[0];
  2626. memset(rsp, 0, sizeof(*rsp));
  2627. rsp->port_number = port;
  2628. /*
  2629. * Note that link_quality_indicator is a 32 bit quantity in
  2630. * 'datacounters' queries (as opposed to 'portinfo' queries,
  2631. * where it's a byte).
  2632. */
  2633. hfi1_read_link_quality(dd, &lq);
  2634. rsp->link_quality_indicator = cpu_to_be32((u32)lq);
  2635. pma_get_opa_port_dctrs(ibdev, rsp);
  2636. /*
  2637. * Convert PortXmitWait counter from TXE
  2638. * cycle times to flit times.
  2639. */
  2640. link_width =
  2641. tx_link_width(ppd->link_width_downgrade_tx_active);
  2642. link_speed = get_link_speed(ppd->link_speed_active);
  2643. rsp->port_xmit_wait =
  2644. cpu_to_be64(get_xmit_wait_counters(ppd, link_width,
  2645. link_speed, C_VL_COUNT));
  2646. rsp->port_rcv_fecn =
  2647. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN, CNTR_INVALID_VL));
  2648. rsp->port_rcv_becn =
  2649. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN, CNTR_INVALID_VL));
  2650. rsp->port_error_counter_summary =
  2651. cpu_to_be64(get_error_counter_summary(ibdev, port,
  2652. res_lli, res_ler));
  2653. vlinfo = &rsp->vls[0];
  2654. vfi = 0;
  2655. /* The vl_select_mask has been checked above, and we know
  2656. * that it contains only entries which represent valid VLs.
  2657. * So in the for_each_set_bit() loop below, we don't need
  2658. * any additional checks for vl.
  2659. */
  2660. for_each_set_bit(vl, &vl_select_mask, BITS_PER_LONG) {
  2661. memset(vlinfo, 0, sizeof(*vlinfo));
  2662. rsp->vls[vfi].port_vl_xmit_data =
  2663. cpu_to_be64(read_port_cntr(ppd, C_TX_FLIT_VL,
  2664. idx_from_vl(vl)));
  2665. rsp->vls[vfi].port_vl_rcv_data =
  2666. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_FLIT_VL,
  2667. idx_from_vl(vl)));
  2668. rsp->vls[vfi].port_vl_xmit_pkts =
  2669. cpu_to_be64(read_port_cntr(ppd, C_TX_PKT_VL,
  2670. idx_from_vl(vl)));
  2671. rsp->vls[vfi].port_vl_rcv_pkts =
  2672. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_PKT_VL,
  2673. idx_from_vl(vl)));
  2674. /*
  2675. * Convert PortVlXmitWait counter from TXE
  2676. * cycle times to flit times.
  2677. */
  2678. rsp->vls[vfi].port_vl_xmit_wait =
  2679. cpu_to_be64(get_xmit_wait_counters(ppd, link_width,
  2680. link_speed,
  2681. idx_from_vl(vl)));
  2682. rsp->vls[vfi].port_vl_rcv_fecn =
  2683. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN_VL,
  2684. idx_from_vl(vl)));
  2685. rsp->vls[vfi].port_vl_rcv_becn =
  2686. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN_VL,
  2687. idx_from_vl(vl)));
  2688. /* rsp->port_vl_xmit_time_cong is 0 for HFIs */
  2689. /* rsp->port_vl_xmit_wasted_bw ??? */
  2690. /* port_vl_xmit_wait_data - TXE (table 13-9 HFI spec) ???
  2691. * does this differ from rsp->vls[vfi].port_vl_xmit_wait
  2692. */
  2693. /*rsp->vls[vfi].port_vl_mark_fecn =
  2694. * cpu_to_be64(read_csr(dd, DCC_PRF_PORT_VL_MARK_FECN_CNT
  2695. * + offset));
  2696. */
  2697. vlinfo++;
  2698. vfi++;
  2699. }
  2700. a0_datacounters(ppd, rsp);
  2701. if (resp_len)
  2702. *resp_len += response_data_size;
  2703. return reply((struct ib_mad_hdr *)pmp);
  2704. }
  2705. static int pma_get_ib_portcounters_ext(struct ib_pma_mad *pmp,
  2706. struct ib_device *ibdev, u8 port)
  2707. {
  2708. struct ib_pma_portcounters_ext *p = (struct ib_pma_portcounters_ext *)
  2709. pmp->data;
  2710. struct _port_dctrs rsp;
  2711. if (pmp->mad_hdr.attr_mod != 0 || p->port_select != port) {
  2712. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2713. goto bail;
  2714. }
  2715. memset(&rsp, 0, sizeof(rsp));
  2716. pma_get_opa_port_dctrs(ibdev, &rsp);
  2717. p->port_xmit_data = rsp.port_xmit_data;
  2718. p->port_rcv_data = rsp.port_rcv_data;
  2719. p->port_xmit_packets = rsp.port_xmit_pkts;
  2720. p->port_rcv_packets = rsp.port_rcv_pkts;
  2721. p->port_unicast_xmit_packets = 0;
  2722. p->port_unicast_rcv_packets = 0;
  2723. p->port_multicast_xmit_packets = rsp.port_multicast_xmit_pkts;
  2724. p->port_multicast_rcv_packets = rsp.port_multicast_rcv_pkts;
  2725. bail:
  2726. return reply((struct ib_mad_hdr *)pmp);
  2727. }
  2728. static void pma_get_opa_port_ectrs(struct ib_device *ibdev,
  2729. struct _port_ectrs *rsp, u8 port)
  2730. {
  2731. u64 tmp, tmp2;
  2732. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2733. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  2734. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  2735. tmp = read_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL);
  2736. tmp2 = tmp + read_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT,
  2737. CNTR_INVALID_VL);
  2738. if (tmp2 > (u32)UINT_MAX || tmp2 < tmp) {
  2739. /* overflow/wrapped */
  2740. rsp->link_error_recovery = cpu_to_be32(~0);
  2741. } else {
  2742. rsp->link_error_recovery = cpu_to_be32(tmp2);
  2743. }
  2744. rsp->link_downed = cpu_to_be32(read_port_cntr(ppd, C_SW_LINK_DOWN,
  2745. CNTR_INVALID_VL));
  2746. rsp->port_rcv_errors =
  2747. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL));
  2748. rsp->port_rcv_remote_physical_errors =
  2749. cpu_to_be64(read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
  2750. CNTR_INVALID_VL));
  2751. rsp->port_rcv_switch_relay_errors = 0;
  2752. rsp->port_xmit_discards =
  2753. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD,
  2754. CNTR_INVALID_VL));
  2755. rsp->port_xmit_constraint_errors =
  2756. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_CSTR_ERR,
  2757. CNTR_INVALID_VL));
  2758. rsp->port_rcv_constraint_errors =
  2759. cpu_to_be64(read_port_cntr(ppd, C_SW_RCV_CSTR_ERR,
  2760. CNTR_INVALID_VL));
  2761. rsp->local_link_integrity_errors =
  2762. cpu_to_be64(read_dev_cntr(dd, C_DC_RX_REPLAY,
  2763. CNTR_INVALID_VL));
  2764. rsp->excessive_buffer_overruns =
  2765. cpu_to_be64(read_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL));
  2766. }
  2767. static int pma_get_opa_porterrors(struct opa_pma_mad *pmp,
  2768. struct ib_device *ibdev,
  2769. u8 port, u32 *resp_len)
  2770. {
  2771. size_t response_data_size;
  2772. struct _port_ectrs *rsp;
  2773. u8 port_num;
  2774. struct opa_port_error_counters64_msg *req;
  2775. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2776. u32 num_ports;
  2777. u8 num_pslm;
  2778. u8 num_vls;
  2779. struct hfi1_ibport *ibp;
  2780. struct hfi1_pportdata *ppd;
  2781. struct _vls_ectrs *vlinfo;
  2782. unsigned long vl;
  2783. u64 port_mask, tmp;
  2784. unsigned long vl_select_mask;
  2785. int vfi;
  2786. req = (struct opa_port_error_counters64_msg *)pmp->data;
  2787. num_ports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
  2788. num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
  2789. num_vls = hweight32(be32_to_cpu(req->vl_select_mask));
  2790. if (num_ports != 1 || num_ports != num_pslm) {
  2791. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2792. return reply((struct ib_mad_hdr *)pmp);
  2793. }
  2794. response_data_size = sizeof(struct opa_port_error_counters64_msg) +
  2795. num_vls * sizeof(struct _vls_ectrs);
  2796. if (response_data_size > sizeof(pmp->data)) {
  2797. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2798. return reply((struct ib_mad_hdr *)pmp);
  2799. }
  2800. /*
  2801. * The bit set in the mask needs to be consistent with the
  2802. * port the request came in on.
  2803. */
  2804. port_mask = be64_to_cpu(req->port_select_mask[3]);
  2805. port_num = find_first_bit((unsigned long *)&port_mask,
  2806. sizeof(port_mask) * 8);
  2807. if (port_num != port) {
  2808. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2809. return reply((struct ib_mad_hdr *)pmp);
  2810. }
  2811. rsp = &req->port[0];
  2812. ibp = to_iport(ibdev, port_num);
  2813. ppd = ppd_from_ibp(ibp);
  2814. memset(rsp, 0, sizeof(*rsp));
  2815. rsp->port_number = port_num;
  2816. pma_get_opa_port_ectrs(ibdev, rsp, port_num);
  2817. rsp->port_rcv_remote_physical_errors =
  2818. cpu_to_be64(read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
  2819. CNTR_INVALID_VL));
  2820. rsp->fm_config_errors =
  2821. cpu_to_be64(read_dev_cntr(dd, C_DC_FM_CFG_ERR,
  2822. CNTR_INVALID_VL));
  2823. tmp = read_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL);
  2824. rsp->uncorrectable_errors = tmp < 0x100 ? (tmp & 0xff) : 0xff;
  2825. rsp->port_rcv_errors =
  2826. cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL));
  2827. vlinfo = &rsp->vls[0];
  2828. vfi = 0;
  2829. vl_select_mask = be32_to_cpu(req->vl_select_mask);
  2830. for_each_set_bit(vl, &vl_select_mask, BITS_PER_LONG) {
  2831. memset(vlinfo, 0, sizeof(*vlinfo));
  2832. rsp->vls[vfi].port_vl_xmit_discards =
  2833. cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD_VL,
  2834. idx_from_vl(vl)));
  2835. vlinfo += 1;
  2836. vfi++;
  2837. }
  2838. if (resp_len)
  2839. *resp_len += response_data_size;
  2840. return reply((struct ib_mad_hdr *)pmp);
  2841. }
  2842. static int pma_get_ib_portcounters(struct ib_pma_mad *pmp,
  2843. struct ib_device *ibdev, u8 port)
  2844. {
  2845. struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
  2846. pmp->data;
  2847. struct _port_ectrs rsp;
  2848. u64 temp_link_overrun_errors;
  2849. u64 temp_64;
  2850. u32 temp_32;
  2851. memset(&rsp, 0, sizeof(rsp));
  2852. pma_get_opa_port_ectrs(ibdev, &rsp, port);
  2853. if (pmp->mad_hdr.attr_mod != 0 || p->port_select != port) {
  2854. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2855. goto bail;
  2856. }
  2857. p->symbol_error_counter = 0; /* N/A for OPA */
  2858. temp_32 = be32_to_cpu(rsp.link_error_recovery);
  2859. if (temp_32 > 0xFFUL)
  2860. p->link_error_recovery_counter = 0xFF;
  2861. else
  2862. p->link_error_recovery_counter = (u8)temp_32;
  2863. temp_32 = be32_to_cpu(rsp.link_downed);
  2864. if (temp_32 > 0xFFUL)
  2865. p->link_downed_counter = 0xFF;
  2866. else
  2867. p->link_downed_counter = (u8)temp_32;
  2868. temp_64 = be64_to_cpu(rsp.port_rcv_errors);
  2869. if (temp_64 > 0xFFFFUL)
  2870. p->port_rcv_errors = cpu_to_be16(0xFFFF);
  2871. else
  2872. p->port_rcv_errors = cpu_to_be16((u16)temp_64);
  2873. temp_64 = be64_to_cpu(rsp.port_rcv_remote_physical_errors);
  2874. if (temp_64 > 0xFFFFUL)
  2875. p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
  2876. else
  2877. p->port_rcv_remphys_errors = cpu_to_be16((u16)temp_64);
  2878. temp_64 = be64_to_cpu(rsp.port_rcv_switch_relay_errors);
  2879. p->port_rcv_switch_relay_errors = cpu_to_be16((u16)temp_64);
  2880. temp_64 = be64_to_cpu(rsp.port_xmit_discards);
  2881. if (temp_64 > 0xFFFFUL)
  2882. p->port_xmit_discards = cpu_to_be16(0xFFFF);
  2883. else
  2884. p->port_xmit_discards = cpu_to_be16((u16)temp_64);
  2885. temp_64 = be64_to_cpu(rsp.port_xmit_constraint_errors);
  2886. if (temp_64 > 0xFFUL)
  2887. p->port_xmit_constraint_errors = 0xFF;
  2888. else
  2889. p->port_xmit_constraint_errors = (u8)temp_64;
  2890. temp_64 = be64_to_cpu(rsp.port_rcv_constraint_errors);
  2891. if (temp_64 > 0xFFUL)
  2892. p->port_rcv_constraint_errors = 0xFFUL;
  2893. else
  2894. p->port_rcv_constraint_errors = (u8)temp_64;
  2895. /* LocalLink: 7:4, BufferOverrun: 3:0 */
  2896. temp_64 = be64_to_cpu(rsp.local_link_integrity_errors);
  2897. if (temp_64 > 0xFUL)
  2898. temp_64 = 0xFUL;
  2899. temp_link_overrun_errors = temp_64 << 4;
  2900. temp_64 = be64_to_cpu(rsp.excessive_buffer_overruns);
  2901. if (temp_64 > 0xFUL)
  2902. temp_64 = 0xFUL;
  2903. temp_link_overrun_errors |= temp_64;
  2904. p->link_overrun_errors = (u8)temp_link_overrun_errors;
  2905. p->vl15_dropped = 0; /* N/A for OPA */
  2906. bail:
  2907. return reply((struct ib_mad_hdr *)pmp);
  2908. }
  2909. static int pma_get_opa_errorinfo(struct opa_pma_mad *pmp,
  2910. struct ib_device *ibdev,
  2911. u8 port, u32 *resp_len)
  2912. {
  2913. size_t response_data_size;
  2914. struct _port_ei *rsp;
  2915. struct opa_port_error_info_msg *req;
  2916. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2917. u64 port_mask;
  2918. u32 num_ports;
  2919. u8 port_num;
  2920. u8 num_pslm;
  2921. u64 reg;
  2922. req = (struct opa_port_error_info_msg *)pmp->data;
  2923. rsp = &req->port[0];
  2924. num_ports = OPA_AM_NPORT(be32_to_cpu(pmp->mad_hdr.attr_mod));
  2925. num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
  2926. memset(rsp, 0, sizeof(*rsp));
  2927. if (num_ports != 1 || num_ports != num_pslm) {
  2928. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2929. return reply((struct ib_mad_hdr *)pmp);
  2930. }
  2931. /* Sanity check */
  2932. response_data_size = sizeof(struct opa_port_error_info_msg);
  2933. if (response_data_size > sizeof(pmp->data)) {
  2934. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2935. return reply((struct ib_mad_hdr *)pmp);
  2936. }
  2937. /*
  2938. * The bit set in the mask needs to be consistent with the port
  2939. * the request came in on.
  2940. */
  2941. port_mask = be64_to_cpu(req->port_select_mask[3]);
  2942. port_num = find_first_bit((unsigned long *)&port_mask,
  2943. sizeof(port_mask) * 8);
  2944. if (port_num != port) {
  2945. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  2946. return reply((struct ib_mad_hdr *)pmp);
  2947. }
  2948. rsp->port_number = port;
  2949. /* PortRcvErrorInfo */
  2950. rsp->port_rcv_ei.status_and_code =
  2951. dd->err_info_rcvport.status_and_code;
  2952. memcpy(&rsp->port_rcv_ei.ei.ei1to12.packet_flit1,
  2953. &dd->err_info_rcvport.packet_flit1, sizeof(u64));
  2954. memcpy(&rsp->port_rcv_ei.ei.ei1to12.packet_flit2,
  2955. &dd->err_info_rcvport.packet_flit2, sizeof(u64));
  2956. /* ExcessiverBufferOverrunInfo */
  2957. reg = read_csr(dd, RCV_ERR_INFO);
  2958. if (reg & RCV_ERR_INFO_RCV_EXCESS_BUFFER_OVERRUN_SMASK) {
  2959. /*
  2960. * if the RcvExcessBufferOverrun bit is set, save SC of
  2961. * first pkt that encountered an excess buffer overrun
  2962. */
  2963. u8 tmp = (u8)reg;
  2964. tmp &= RCV_ERR_INFO_RCV_EXCESS_BUFFER_OVERRUN_SC_SMASK;
  2965. tmp <<= 2;
  2966. rsp->excessive_buffer_overrun_ei.status_and_sc = tmp;
  2967. /* set the status bit */
  2968. rsp->excessive_buffer_overrun_ei.status_and_sc |= 0x80;
  2969. }
  2970. rsp->port_xmit_constraint_ei.status =
  2971. dd->err_info_xmit_constraint.status;
  2972. rsp->port_xmit_constraint_ei.pkey =
  2973. cpu_to_be16(dd->err_info_xmit_constraint.pkey);
  2974. rsp->port_xmit_constraint_ei.slid =
  2975. cpu_to_be32(dd->err_info_xmit_constraint.slid);
  2976. rsp->port_rcv_constraint_ei.status =
  2977. dd->err_info_rcv_constraint.status;
  2978. rsp->port_rcv_constraint_ei.pkey =
  2979. cpu_to_be16(dd->err_info_rcv_constraint.pkey);
  2980. rsp->port_rcv_constraint_ei.slid =
  2981. cpu_to_be32(dd->err_info_rcv_constraint.slid);
  2982. /* UncorrectableErrorInfo */
  2983. rsp->uncorrectable_ei.status_and_code = dd->err_info_uncorrectable;
  2984. /* FMConfigErrorInfo */
  2985. rsp->fm_config_ei.status_and_code = dd->err_info_fmconfig;
  2986. if (resp_len)
  2987. *resp_len += response_data_size;
  2988. return reply((struct ib_mad_hdr *)pmp);
  2989. }
  2990. static int pma_set_opa_portstatus(struct opa_pma_mad *pmp,
  2991. struct ib_device *ibdev,
  2992. u8 port, u32 *resp_len)
  2993. {
  2994. struct opa_clear_port_status *req =
  2995. (struct opa_clear_port_status *)pmp->data;
  2996. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  2997. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  2998. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  2999. u32 nports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
  3000. u64 portn = be64_to_cpu(req->port_select_mask[3]);
  3001. u32 counter_select = be32_to_cpu(req->counter_select_mask);
  3002. unsigned long vl_select_mask = VL_MASK_ALL; /* clear all per-vl cnts */
  3003. unsigned long vl;
  3004. if ((nports != 1) || (portn != 1 << port)) {
  3005. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  3006. return reply((struct ib_mad_hdr *)pmp);
  3007. }
  3008. /*
  3009. * only counters returned by pma_get_opa_portstatus() are
  3010. * handled, so when pma_get_opa_portstatus() gets a fix,
  3011. * the corresponding change should be made here as well.
  3012. */
  3013. if (counter_select & CS_PORT_XMIT_DATA)
  3014. write_dev_cntr(dd, C_DC_XMIT_FLITS, CNTR_INVALID_VL, 0);
  3015. if (counter_select & CS_PORT_RCV_DATA)
  3016. write_dev_cntr(dd, C_DC_RCV_FLITS, CNTR_INVALID_VL, 0);
  3017. if (counter_select & CS_PORT_XMIT_PKTS)
  3018. write_dev_cntr(dd, C_DC_XMIT_PKTS, CNTR_INVALID_VL, 0);
  3019. if (counter_select & CS_PORT_RCV_PKTS)
  3020. write_dev_cntr(dd, C_DC_RCV_PKTS, CNTR_INVALID_VL, 0);
  3021. if (counter_select & CS_PORT_MCAST_XMIT_PKTS)
  3022. write_dev_cntr(dd, C_DC_MC_XMIT_PKTS, CNTR_INVALID_VL, 0);
  3023. if (counter_select & CS_PORT_MCAST_RCV_PKTS)
  3024. write_dev_cntr(dd, C_DC_MC_RCV_PKTS, CNTR_INVALID_VL, 0);
  3025. if (counter_select & CS_PORT_XMIT_WAIT) {
  3026. write_port_cntr(ppd, C_TX_WAIT, CNTR_INVALID_VL, 0);
  3027. ppd->port_vl_xmit_wait_last[C_VL_COUNT] = 0;
  3028. ppd->vl_xmit_flit_cnt[C_VL_COUNT] = 0;
  3029. }
  3030. /* ignore cs_sw_portCongestion for HFIs */
  3031. if (counter_select & CS_PORT_RCV_FECN)
  3032. write_dev_cntr(dd, C_DC_RCV_FCN, CNTR_INVALID_VL, 0);
  3033. if (counter_select & CS_PORT_RCV_BECN)
  3034. write_dev_cntr(dd, C_DC_RCV_BCN, CNTR_INVALID_VL, 0);
  3035. /* ignore cs_port_xmit_time_cong for HFIs */
  3036. /* ignore cs_port_xmit_wasted_bw for now */
  3037. /* ignore cs_port_xmit_wait_data for now */
  3038. if (counter_select & CS_PORT_RCV_BUBBLE)
  3039. write_dev_cntr(dd, C_DC_RCV_BBL, CNTR_INVALID_VL, 0);
  3040. /* Only applicable for switch */
  3041. /* if (counter_select & CS_PORT_MARK_FECN)
  3042. * write_csr(dd, DCC_PRF_PORT_MARK_FECN_CNT, 0);
  3043. */
  3044. if (counter_select & CS_PORT_RCV_CONSTRAINT_ERRORS)
  3045. write_port_cntr(ppd, C_SW_RCV_CSTR_ERR, CNTR_INVALID_VL, 0);
  3046. /* ignore cs_port_rcv_switch_relay_errors for HFIs */
  3047. if (counter_select & CS_PORT_XMIT_DISCARDS)
  3048. write_port_cntr(ppd, C_SW_XMIT_DSCD, CNTR_INVALID_VL, 0);
  3049. if (counter_select & CS_PORT_XMIT_CONSTRAINT_ERRORS)
  3050. write_port_cntr(ppd, C_SW_XMIT_CSTR_ERR, CNTR_INVALID_VL, 0);
  3051. if (counter_select & CS_PORT_RCV_REMOTE_PHYSICAL_ERRORS)
  3052. write_dev_cntr(dd, C_DC_RMT_PHY_ERR, CNTR_INVALID_VL, 0);
  3053. if (counter_select & CS_LOCAL_LINK_INTEGRITY_ERRORS)
  3054. write_dev_cntr(dd, C_DC_RX_REPLAY, CNTR_INVALID_VL, 0);
  3055. if (counter_select & CS_LINK_ERROR_RECOVERY) {
  3056. write_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL, 0);
  3057. write_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT,
  3058. CNTR_INVALID_VL, 0);
  3059. }
  3060. if (counter_select & CS_PORT_RCV_ERRORS)
  3061. write_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL, 0);
  3062. if (counter_select & CS_EXCESSIVE_BUFFER_OVERRUNS) {
  3063. write_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL, 0);
  3064. dd->rcv_ovfl_cnt = 0;
  3065. }
  3066. if (counter_select & CS_FM_CONFIG_ERRORS)
  3067. write_dev_cntr(dd, C_DC_FM_CFG_ERR, CNTR_INVALID_VL, 0);
  3068. if (counter_select & CS_LINK_DOWNED)
  3069. write_port_cntr(ppd, C_SW_LINK_DOWN, CNTR_INVALID_VL, 0);
  3070. if (counter_select & CS_UNCORRECTABLE_ERRORS)
  3071. write_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL, 0);
  3072. for_each_set_bit(vl, &vl_select_mask, BITS_PER_LONG) {
  3073. if (counter_select & CS_PORT_XMIT_DATA)
  3074. write_port_cntr(ppd, C_TX_FLIT_VL, idx_from_vl(vl), 0);
  3075. if (counter_select & CS_PORT_RCV_DATA)
  3076. write_dev_cntr(dd, C_DC_RX_FLIT_VL, idx_from_vl(vl), 0);
  3077. if (counter_select & CS_PORT_XMIT_PKTS)
  3078. write_port_cntr(ppd, C_TX_PKT_VL, idx_from_vl(vl), 0);
  3079. if (counter_select & CS_PORT_RCV_PKTS)
  3080. write_dev_cntr(dd, C_DC_RX_PKT_VL, idx_from_vl(vl), 0);
  3081. if (counter_select & CS_PORT_XMIT_WAIT) {
  3082. write_port_cntr(ppd, C_TX_WAIT_VL, idx_from_vl(vl), 0);
  3083. ppd->port_vl_xmit_wait_last[idx_from_vl(vl)] = 0;
  3084. ppd->vl_xmit_flit_cnt[idx_from_vl(vl)] = 0;
  3085. }
  3086. /* sw_port_vl_congestion is 0 for HFIs */
  3087. if (counter_select & CS_PORT_RCV_FECN)
  3088. write_dev_cntr(dd, C_DC_RCV_FCN_VL, idx_from_vl(vl), 0);
  3089. if (counter_select & CS_PORT_RCV_BECN)
  3090. write_dev_cntr(dd, C_DC_RCV_BCN_VL, idx_from_vl(vl), 0);
  3091. /* port_vl_xmit_time_cong is 0 for HFIs */
  3092. /* port_vl_xmit_wasted_bw ??? */
  3093. /* port_vl_xmit_wait_data - TXE (table 13-9 HFI spec) ??? */
  3094. if (counter_select & CS_PORT_RCV_BUBBLE)
  3095. write_dev_cntr(dd, C_DC_RCV_BBL_VL, idx_from_vl(vl), 0);
  3096. /* if (counter_select & CS_PORT_MARK_FECN)
  3097. * write_csr(dd, DCC_PRF_PORT_VL_MARK_FECN_CNT + offset, 0);
  3098. */
  3099. if (counter_select & C_SW_XMIT_DSCD_VL)
  3100. write_port_cntr(ppd, C_SW_XMIT_DSCD_VL,
  3101. idx_from_vl(vl), 0);
  3102. }
  3103. if (resp_len)
  3104. *resp_len += sizeof(*req);
  3105. return reply((struct ib_mad_hdr *)pmp);
  3106. }
  3107. static int pma_set_opa_errorinfo(struct opa_pma_mad *pmp,
  3108. struct ib_device *ibdev,
  3109. u8 port, u32 *resp_len)
  3110. {
  3111. struct _port_ei *rsp;
  3112. struct opa_port_error_info_msg *req;
  3113. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  3114. u64 port_mask;
  3115. u32 num_ports;
  3116. u8 port_num;
  3117. u8 num_pslm;
  3118. u32 error_info_select;
  3119. req = (struct opa_port_error_info_msg *)pmp->data;
  3120. rsp = &req->port[0];
  3121. num_ports = OPA_AM_NPORT(be32_to_cpu(pmp->mad_hdr.attr_mod));
  3122. num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
  3123. memset(rsp, 0, sizeof(*rsp));
  3124. if (num_ports != 1 || num_ports != num_pslm) {
  3125. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  3126. return reply((struct ib_mad_hdr *)pmp);
  3127. }
  3128. /*
  3129. * The bit set in the mask needs to be consistent with the port
  3130. * the request came in on.
  3131. */
  3132. port_mask = be64_to_cpu(req->port_select_mask[3]);
  3133. port_num = find_first_bit((unsigned long *)&port_mask,
  3134. sizeof(port_mask) * 8);
  3135. if (port_num != port) {
  3136. pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
  3137. return reply((struct ib_mad_hdr *)pmp);
  3138. }
  3139. error_info_select = be32_to_cpu(req->error_info_select_mask);
  3140. /* PortRcvErrorInfo */
  3141. if (error_info_select & ES_PORT_RCV_ERROR_INFO)
  3142. /* turn off status bit */
  3143. dd->err_info_rcvport.status_and_code &= ~OPA_EI_STATUS_SMASK;
  3144. /* ExcessiverBufferOverrunInfo */
  3145. if (error_info_select & ES_EXCESSIVE_BUFFER_OVERRUN_INFO)
  3146. /*
  3147. * status bit is essentially kept in the h/w - bit 5 of
  3148. * RCV_ERR_INFO
  3149. */
  3150. write_csr(dd, RCV_ERR_INFO,
  3151. RCV_ERR_INFO_RCV_EXCESS_BUFFER_OVERRUN_SMASK);
  3152. if (error_info_select & ES_PORT_XMIT_CONSTRAINT_ERROR_INFO)
  3153. dd->err_info_xmit_constraint.status &= ~OPA_EI_STATUS_SMASK;
  3154. if (error_info_select & ES_PORT_RCV_CONSTRAINT_ERROR_INFO)
  3155. dd->err_info_rcv_constraint.status &= ~OPA_EI_STATUS_SMASK;
  3156. /* UncorrectableErrorInfo */
  3157. if (error_info_select & ES_UNCORRECTABLE_ERROR_INFO)
  3158. /* turn off status bit */
  3159. dd->err_info_uncorrectable &= ~OPA_EI_STATUS_SMASK;
  3160. /* FMConfigErrorInfo */
  3161. if (error_info_select & ES_FM_CONFIG_ERROR_INFO)
  3162. /* turn off status bit */
  3163. dd->err_info_fmconfig &= ~OPA_EI_STATUS_SMASK;
  3164. if (resp_len)
  3165. *resp_len += sizeof(*req);
  3166. return reply((struct ib_mad_hdr *)pmp);
  3167. }
  3168. struct opa_congestion_info_attr {
  3169. __be16 congestion_info;
  3170. u8 control_table_cap; /* Multiple of 64 entry unit CCTs */
  3171. u8 congestion_log_length;
  3172. } __packed;
  3173. static int __subn_get_opa_cong_info(struct opa_smp *smp, u32 am, u8 *data,
  3174. struct ib_device *ibdev, u8 port,
  3175. u32 *resp_len, u32 max_len)
  3176. {
  3177. struct opa_congestion_info_attr *p =
  3178. (struct opa_congestion_info_attr *)data;
  3179. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3180. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3181. if (smp_length_check(sizeof(*p), max_len)) {
  3182. smp->status |= IB_SMP_INVALID_FIELD;
  3183. return reply((struct ib_mad_hdr *)smp);
  3184. }
  3185. p->congestion_info = 0;
  3186. p->control_table_cap = ppd->cc_max_table_entries;
  3187. p->congestion_log_length = OPA_CONG_LOG_ELEMS;
  3188. if (resp_len)
  3189. *resp_len += sizeof(*p);
  3190. return reply((struct ib_mad_hdr *)smp);
  3191. }
  3192. static int __subn_get_opa_cong_setting(struct opa_smp *smp, u32 am,
  3193. u8 *data, struct ib_device *ibdev,
  3194. u8 port, u32 *resp_len, u32 max_len)
  3195. {
  3196. int i;
  3197. struct opa_congestion_setting_attr *p =
  3198. (struct opa_congestion_setting_attr *)data;
  3199. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3200. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3201. struct opa_congestion_setting_entry_shadow *entries;
  3202. struct cc_state *cc_state;
  3203. if (smp_length_check(sizeof(*p), max_len)) {
  3204. smp->status |= IB_SMP_INVALID_FIELD;
  3205. return reply((struct ib_mad_hdr *)smp);
  3206. }
  3207. rcu_read_lock();
  3208. cc_state = get_cc_state(ppd);
  3209. if (!cc_state) {
  3210. rcu_read_unlock();
  3211. return reply((struct ib_mad_hdr *)smp);
  3212. }
  3213. entries = cc_state->cong_setting.entries;
  3214. p->port_control = cpu_to_be16(cc_state->cong_setting.port_control);
  3215. p->control_map = cpu_to_be32(cc_state->cong_setting.control_map);
  3216. for (i = 0; i < OPA_MAX_SLS; i++) {
  3217. p->entries[i].ccti_increase = entries[i].ccti_increase;
  3218. p->entries[i].ccti_timer = cpu_to_be16(entries[i].ccti_timer);
  3219. p->entries[i].trigger_threshold =
  3220. entries[i].trigger_threshold;
  3221. p->entries[i].ccti_min = entries[i].ccti_min;
  3222. }
  3223. rcu_read_unlock();
  3224. if (resp_len)
  3225. *resp_len += sizeof(*p);
  3226. return reply((struct ib_mad_hdr *)smp);
  3227. }
  3228. /*
  3229. * Apply congestion control information stored in the ppd to the
  3230. * active structure.
  3231. */
  3232. static void apply_cc_state(struct hfi1_pportdata *ppd)
  3233. {
  3234. struct cc_state *old_cc_state, *new_cc_state;
  3235. new_cc_state = kzalloc(sizeof(*new_cc_state), GFP_KERNEL);
  3236. if (!new_cc_state)
  3237. return;
  3238. /*
  3239. * Hold the lock for updating *and* to prevent ppd information
  3240. * from changing during the update.
  3241. */
  3242. spin_lock(&ppd->cc_state_lock);
  3243. old_cc_state = get_cc_state_protected(ppd);
  3244. if (!old_cc_state) {
  3245. /* never active, or shutting down */
  3246. spin_unlock(&ppd->cc_state_lock);
  3247. kfree(new_cc_state);
  3248. return;
  3249. }
  3250. *new_cc_state = *old_cc_state;
  3251. if (ppd->total_cct_entry)
  3252. new_cc_state->cct.ccti_limit = ppd->total_cct_entry - 1;
  3253. else
  3254. new_cc_state->cct.ccti_limit = 0;
  3255. memcpy(new_cc_state->cct.entries, ppd->ccti_entries,
  3256. ppd->total_cct_entry * sizeof(struct ib_cc_table_entry));
  3257. new_cc_state->cong_setting.port_control = IB_CC_CCS_PC_SL_BASED;
  3258. new_cc_state->cong_setting.control_map = ppd->cc_sl_control_map;
  3259. memcpy(new_cc_state->cong_setting.entries, ppd->congestion_entries,
  3260. OPA_MAX_SLS * sizeof(struct opa_congestion_setting_entry));
  3261. rcu_assign_pointer(ppd->cc_state, new_cc_state);
  3262. spin_unlock(&ppd->cc_state_lock);
  3263. kfree_rcu(old_cc_state, rcu);
  3264. }
  3265. static int __subn_set_opa_cong_setting(struct opa_smp *smp, u32 am, u8 *data,
  3266. struct ib_device *ibdev, u8 port,
  3267. u32 *resp_len, u32 max_len)
  3268. {
  3269. struct opa_congestion_setting_attr *p =
  3270. (struct opa_congestion_setting_attr *)data;
  3271. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3272. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3273. struct opa_congestion_setting_entry_shadow *entries;
  3274. int i;
  3275. if (smp_length_check(sizeof(*p), max_len)) {
  3276. smp->status |= IB_SMP_INVALID_FIELD;
  3277. return reply((struct ib_mad_hdr *)smp);
  3278. }
  3279. /*
  3280. * Save details from packet into the ppd. Hold the cc_state_lock so
  3281. * our information is consistent with anyone trying to apply the state.
  3282. */
  3283. spin_lock(&ppd->cc_state_lock);
  3284. ppd->cc_sl_control_map = be32_to_cpu(p->control_map);
  3285. entries = ppd->congestion_entries;
  3286. for (i = 0; i < OPA_MAX_SLS; i++) {
  3287. entries[i].ccti_increase = p->entries[i].ccti_increase;
  3288. entries[i].ccti_timer = be16_to_cpu(p->entries[i].ccti_timer);
  3289. entries[i].trigger_threshold =
  3290. p->entries[i].trigger_threshold;
  3291. entries[i].ccti_min = p->entries[i].ccti_min;
  3292. }
  3293. spin_unlock(&ppd->cc_state_lock);
  3294. /* now apply the information */
  3295. apply_cc_state(ppd);
  3296. return __subn_get_opa_cong_setting(smp, am, data, ibdev, port,
  3297. resp_len, max_len);
  3298. }
  3299. static int __subn_get_opa_hfi1_cong_log(struct opa_smp *smp, u32 am,
  3300. u8 *data, struct ib_device *ibdev,
  3301. u8 port, u32 *resp_len, u32 max_len)
  3302. {
  3303. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3304. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3305. struct opa_hfi1_cong_log *cong_log = (struct opa_hfi1_cong_log *)data;
  3306. u64 ts;
  3307. int i;
  3308. if (am || smp_length_check(sizeof(*cong_log), max_len)) {
  3309. smp->status |= IB_SMP_INVALID_FIELD;
  3310. return reply((struct ib_mad_hdr *)smp);
  3311. }
  3312. spin_lock_irq(&ppd->cc_log_lock);
  3313. cong_log->log_type = OPA_CC_LOG_TYPE_HFI;
  3314. cong_log->congestion_flags = 0;
  3315. cong_log->threshold_event_counter =
  3316. cpu_to_be16(ppd->threshold_event_counter);
  3317. memcpy(cong_log->threshold_cong_event_map,
  3318. ppd->threshold_cong_event_map,
  3319. sizeof(cong_log->threshold_cong_event_map));
  3320. /* keep timestamp in units of 1.024 usec */
  3321. ts = ktime_get_ns() / 1024;
  3322. cong_log->current_time_stamp = cpu_to_be32(ts);
  3323. for (i = 0; i < OPA_CONG_LOG_ELEMS; i++) {
  3324. struct opa_hfi1_cong_log_event_internal *cce =
  3325. &ppd->cc_events[ppd->cc_mad_idx++];
  3326. if (ppd->cc_mad_idx == OPA_CONG_LOG_ELEMS)
  3327. ppd->cc_mad_idx = 0;
  3328. /*
  3329. * Entries which are older than twice the time
  3330. * required to wrap the counter are supposed to
  3331. * be zeroed (CA10-49 IBTA, release 1.2.1, V1).
  3332. */
  3333. if ((ts - cce->timestamp) / 2 > U32_MAX)
  3334. continue;
  3335. memcpy(cong_log->events[i].local_qp_cn_entry, &cce->lqpn, 3);
  3336. memcpy(cong_log->events[i].remote_qp_number_cn_entry,
  3337. &cce->rqpn, 3);
  3338. cong_log->events[i].sl_svc_type_cn_entry =
  3339. ((cce->sl & 0x1f) << 3) | (cce->svc_type & 0x7);
  3340. cong_log->events[i].remote_lid_cn_entry =
  3341. cpu_to_be32(cce->rlid);
  3342. cong_log->events[i].timestamp_cn_entry =
  3343. cpu_to_be32(cce->timestamp);
  3344. }
  3345. /*
  3346. * Reset threshold_cong_event_map, and threshold_event_counter
  3347. * to 0 when log is read.
  3348. */
  3349. memset(ppd->threshold_cong_event_map, 0x0,
  3350. sizeof(ppd->threshold_cong_event_map));
  3351. ppd->threshold_event_counter = 0;
  3352. spin_unlock_irq(&ppd->cc_log_lock);
  3353. if (resp_len)
  3354. *resp_len += sizeof(struct opa_hfi1_cong_log);
  3355. return reply((struct ib_mad_hdr *)smp);
  3356. }
  3357. static int __subn_get_opa_cc_table(struct opa_smp *smp, u32 am, u8 *data,
  3358. struct ib_device *ibdev, u8 port,
  3359. u32 *resp_len, u32 max_len)
  3360. {
  3361. struct ib_cc_table_attr *cc_table_attr =
  3362. (struct ib_cc_table_attr *)data;
  3363. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3364. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3365. u32 start_block = OPA_AM_START_BLK(am);
  3366. u32 n_blocks = OPA_AM_NBLK(am);
  3367. struct ib_cc_table_entry_shadow *entries;
  3368. int i, j;
  3369. u32 sentry, eentry;
  3370. struct cc_state *cc_state;
  3371. u32 size = sizeof(u16) * (IB_CCT_ENTRIES * n_blocks + 1);
  3372. /* sanity check n_blocks, start_block */
  3373. if (n_blocks == 0 || smp_length_check(size, max_len) ||
  3374. start_block + n_blocks > ppd->cc_max_table_entries) {
  3375. smp->status |= IB_SMP_INVALID_FIELD;
  3376. return reply((struct ib_mad_hdr *)smp);
  3377. }
  3378. rcu_read_lock();
  3379. cc_state = get_cc_state(ppd);
  3380. if (!cc_state) {
  3381. rcu_read_unlock();
  3382. return reply((struct ib_mad_hdr *)smp);
  3383. }
  3384. sentry = start_block * IB_CCT_ENTRIES;
  3385. eentry = sentry + (IB_CCT_ENTRIES * n_blocks);
  3386. cc_table_attr->ccti_limit = cpu_to_be16(cc_state->cct.ccti_limit);
  3387. entries = cc_state->cct.entries;
  3388. /* return n_blocks, though the last block may not be full */
  3389. for (j = 0, i = sentry; i < eentry; j++, i++)
  3390. cc_table_attr->ccti_entries[j].entry =
  3391. cpu_to_be16(entries[i].entry);
  3392. rcu_read_unlock();
  3393. if (resp_len)
  3394. *resp_len += size;
  3395. return reply((struct ib_mad_hdr *)smp);
  3396. }
  3397. static int __subn_set_opa_cc_table(struct opa_smp *smp, u32 am, u8 *data,
  3398. struct ib_device *ibdev, u8 port,
  3399. u32 *resp_len, u32 max_len)
  3400. {
  3401. struct ib_cc_table_attr *p = (struct ib_cc_table_attr *)data;
  3402. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3403. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3404. u32 start_block = OPA_AM_START_BLK(am);
  3405. u32 n_blocks = OPA_AM_NBLK(am);
  3406. struct ib_cc_table_entry_shadow *entries;
  3407. int i, j;
  3408. u32 sentry, eentry;
  3409. u16 ccti_limit;
  3410. u32 size = sizeof(u16) * (IB_CCT_ENTRIES * n_blocks + 1);
  3411. /* sanity check n_blocks, start_block */
  3412. if (n_blocks == 0 || smp_length_check(size, max_len) ||
  3413. start_block + n_blocks > ppd->cc_max_table_entries) {
  3414. smp->status |= IB_SMP_INVALID_FIELD;
  3415. return reply((struct ib_mad_hdr *)smp);
  3416. }
  3417. sentry = start_block * IB_CCT_ENTRIES;
  3418. eentry = sentry + ((n_blocks - 1) * IB_CCT_ENTRIES) +
  3419. (be16_to_cpu(p->ccti_limit)) % IB_CCT_ENTRIES + 1;
  3420. /* sanity check ccti_limit */
  3421. ccti_limit = be16_to_cpu(p->ccti_limit);
  3422. if (ccti_limit + 1 > eentry) {
  3423. smp->status |= IB_SMP_INVALID_FIELD;
  3424. return reply((struct ib_mad_hdr *)smp);
  3425. }
  3426. /*
  3427. * Save details from packet into the ppd. Hold the cc_state_lock so
  3428. * our information is consistent with anyone trying to apply the state.
  3429. */
  3430. spin_lock(&ppd->cc_state_lock);
  3431. ppd->total_cct_entry = ccti_limit + 1;
  3432. entries = ppd->ccti_entries;
  3433. for (j = 0, i = sentry; i < eentry; j++, i++)
  3434. entries[i].entry = be16_to_cpu(p->ccti_entries[j].entry);
  3435. spin_unlock(&ppd->cc_state_lock);
  3436. /* now apply the information */
  3437. apply_cc_state(ppd);
  3438. return __subn_get_opa_cc_table(smp, am, data, ibdev, port, resp_len,
  3439. max_len);
  3440. }
  3441. struct opa_led_info {
  3442. __be32 rsvd_led_mask;
  3443. __be32 rsvd;
  3444. };
  3445. #define OPA_LED_SHIFT 31
  3446. #define OPA_LED_MASK BIT(OPA_LED_SHIFT)
  3447. static int __subn_get_opa_led_info(struct opa_smp *smp, u32 am, u8 *data,
  3448. struct ib_device *ibdev, u8 port,
  3449. u32 *resp_len, u32 max_len)
  3450. {
  3451. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  3452. struct hfi1_pportdata *ppd = dd->pport;
  3453. struct opa_led_info *p = (struct opa_led_info *)data;
  3454. u32 nport = OPA_AM_NPORT(am);
  3455. u32 is_beaconing_active;
  3456. if (nport != 1 || smp_length_check(sizeof(*p), max_len)) {
  3457. smp->status |= IB_SMP_INVALID_FIELD;
  3458. return reply((struct ib_mad_hdr *)smp);
  3459. }
  3460. /*
  3461. * This pairs with the memory barrier in hfi1_start_led_override to
  3462. * ensure that we read the correct state of LED beaconing represented
  3463. * by led_override_timer_active
  3464. */
  3465. smp_rmb();
  3466. is_beaconing_active = !!atomic_read(&ppd->led_override_timer_active);
  3467. p->rsvd_led_mask = cpu_to_be32(is_beaconing_active << OPA_LED_SHIFT);
  3468. if (resp_len)
  3469. *resp_len += sizeof(struct opa_led_info);
  3470. return reply((struct ib_mad_hdr *)smp);
  3471. }
  3472. static int __subn_set_opa_led_info(struct opa_smp *smp, u32 am, u8 *data,
  3473. struct ib_device *ibdev, u8 port,
  3474. u32 *resp_len, u32 max_len)
  3475. {
  3476. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  3477. struct opa_led_info *p = (struct opa_led_info *)data;
  3478. u32 nport = OPA_AM_NPORT(am);
  3479. int on = !!(be32_to_cpu(p->rsvd_led_mask) & OPA_LED_MASK);
  3480. if (nport != 1 || smp_length_check(sizeof(*p), max_len)) {
  3481. smp->status |= IB_SMP_INVALID_FIELD;
  3482. return reply((struct ib_mad_hdr *)smp);
  3483. }
  3484. if (on)
  3485. hfi1_start_led_override(dd->pport, 2000, 1500);
  3486. else
  3487. shutdown_led_override(dd->pport);
  3488. return __subn_get_opa_led_info(smp, am, data, ibdev, port, resp_len,
  3489. max_len);
  3490. }
  3491. static int subn_get_opa_sma(__be16 attr_id, struct opa_smp *smp, u32 am,
  3492. u8 *data, struct ib_device *ibdev, u8 port,
  3493. u32 *resp_len, u32 max_len)
  3494. {
  3495. int ret;
  3496. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3497. switch (attr_id) {
  3498. case IB_SMP_ATTR_NODE_DESC:
  3499. ret = __subn_get_opa_nodedesc(smp, am, data, ibdev, port,
  3500. resp_len, max_len);
  3501. break;
  3502. case IB_SMP_ATTR_NODE_INFO:
  3503. ret = __subn_get_opa_nodeinfo(smp, am, data, ibdev, port,
  3504. resp_len, max_len);
  3505. break;
  3506. case IB_SMP_ATTR_PORT_INFO:
  3507. ret = __subn_get_opa_portinfo(smp, am, data, ibdev, port,
  3508. resp_len, max_len);
  3509. break;
  3510. case IB_SMP_ATTR_PKEY_TABLE:
  3511. ret = __subn_get_opa_pkeytable(smp, am, data, ibdev, port,
  3512. resp_len, max_len);
  3513. break;
  3514. case OPA_ATTRIB_ID_SL_TO_SC_MAP:
  3515. ret = __subn_get_opa_sl_to_sc(smp, am, data, ibdev, port,
  3516. resp_len, max_len);
  3517. break;
  3518. case OPA_ATTRIB_ID_SC_TO_SL_MAP:
  3519. ret = __subn_get_opa_sc_to_sl(smp, am, data, ibdev, port,
  3520. resp_len, max_len);
  3521. break;
  3522. case OPA_ATTRIB_ID_SC_TO_VLT_MAP:
  3523. ret = __subn_get_opa_sc_to_vlt(smp, am, data, ibdev, port,
  3524. resp_len, max_len);
  3525. break;
  3526. case OPA_ATTRIB_ID_SC_TO_VLNT_MAP:
  3527. ret = __subn_get_opa_sc_to_vlnt(smp, am, data, ibdev, port,
  3528. resp_len, max_len);
  3529. break;
  3530. case OPA_ATTRIB_ID_PORT_STATE_INFO:
  3531. ret = __subn_get_opa_psi(smp, am, data, ibdev, port,
  3532. resp_len, max_len);
  3533. break;
  3534. case OPA_ATTRIB_ID_BUFFER_CONTROL_TABLE:
  3535. ret = __subn_get_opa_bct(smp, am, data, ibdev, port,
  3536. resp_len, max_len);
  3537. break;
  3538. case OPA_ATTRIB_ID_CABLE_INFO:
  3539. ret = __subn_get_opa_cable_info(smp, am, data, ibdev, port,
  3540. resp_len, max_len);
  3541. break;
  3542. case IB_SMP_ATTR_VL_ARB_TABLE:
  3543. ret = __subn_get_opa_vl_arb(smp, am, data, ibdev, port,
  3544. resp_len, max_len);
  3545. break;
  3546. case OPA_ATTRIB_ID_CONGESTION_INFO:
  3547. ret = __subn_get_opa_cong_info(smp, am, data, ibdev, port,
  3548. resp_len, max_len);
  3549. break;
  3550. case OPA_ATTRIB_ID_HFI_CONGESTION_SETTING:
  3551. ret = __subn_get_opa_cong_setting(smp, am, data, ibdev,
  3552. port, resp_len, max_len);
  3553. break;
  3554. case OPA_ATTRIB_ID_HFI_CONGESTION_LOG:
  3555. ret = __subn_get_opa_hfi1_cong_log(smp, am, data, ibdev,
  3556. port, resp_len, max_len);
  3557. break;
  3558. case OPA_ATTRIB_ID_CONGESTION_CONTROL_TABLE:
  3559. ret = __subn_get_opa_cc_table(smp, am, data, ibdev, port,
  3560. resp_len, max_len);
  3561. break;
  3562. case IB_SMP_ATTR_LED_INFO:
  3563. ret = __subn_get_opa_led_info(smp, am, data, ibdev, port,
  3564. resp_len, max_len);
  3565. break;
  3566. case IB_SMP_ATTR_SM_INFO:
  3567. if (ibp->rvp.port_cap_flags & IB_PORT_SM_DISABLED)
  3568. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  3569. if (ibp->rvp.port_cap_flags & IB_PORT_SM)
  3570. return IB_MAD_RESULT_SUCCESS;
  3571. /* FALLTHROUGH */
  3572. default:
  3573. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  3574. ret = reply((struct ib_mad_hdr *)smp);
  3575. break;
  3576. }
  3577. return ret;
  3578. }
  3579. static int subn_set_opa_sma(__be16 attr_id, struct opa_smp *smp, u32 am,
  3580. u8 *data, struct ib_device *ibdev, u8 port,
  3581. u32 *resp_len, u32 max_len, int local_mad)
  3582. {
  3583. int ret;
  3584. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3585. switch (attr_id) {
  3586. case IB_SMP_ATTR_PORT_INFO:
  3587. ret = __subn_set_opa_portinfo(smp, am, data, ibdev, port,
  3588. resp_len, max_len, local_mad);
  3589. break;
  3590. case IB_SMP_ATTR_PKEY_TABLE:
  3591. ret = __subn_set_opa_pkeytable(smp, am, data, ibdev, port,
  3592. resp_len, max_len);
  3593. break;
  3594. case OPA_ATTRIB_ID_SL_TO_SC_MAP:
  3595. ret = __subn_set_opa_sl_to_sc(smp, am, data, ibdev, port,
  3596. resp_len, max_len);
  3597. break;
  3598. case OPA_ATTRIB_ID_SC_TO_SL_MAP:
  3599. ret = __subn_set_opa_sc_to_sl(smp, am, data, ibdev, port,
  3600. resp_len, max_len);
  3601. break;
  3602. case OPA_ATTRIB_ID_SC_TO_VLT_MAP:
  3603. ret = __subn_set_opa_sc_to_vlt(smp, am, data, ibdev, port,
  3604. resp_len, max_len);
  3605. break;
  3606. case OPA_ATTRIB_ID_SC_TO_VLNT_MAP:
  3607. ret = __subn_set_opa_sc_to_vlnt(smp, am, data, ibdev, port,
  3608. resp_len, max_len);
  3609. break;
  3610. case OPA_ATTRIB_ID_PORT_STATE_INFO:
  3611. ret = __subn_set_opa_psi(smp, am, data, ibdev, port,
  3612. resp_len, max_len, local_mad);
  3613. break;
  3614. case OPA_ATTRIB_ID_BUFFER_CONTROL_TABLE:
  3615. ret = __subn_set_opa_bct(smp, am, data, ibdev, port,
  3616. resp_len, max_len);
  3617. break;
  3618. case IB_SMP_ATTR_VL_ARB_TABLE:
  3619. ret = __subn_set_opa_vl_arb(smp, am, data, ibdev, port,
  3620. resp_len, max_len);
  3621. break;
  3622. case OPA_ATTRIB_ID_HFI_CONGESTION_SETTING:
  3623. ret = __subn_set_opa_cong_setting(smp, am, data, ibdev,
  3624. port, resp_len, max_len);
  3625. break;
  3626. case OPA_ATTRIB_ID_CONGESTION_CONTROL_TABLE:
  3627. ret = __subn_set_opa_cc_table(smp, am, data, ibdev, port,
  3628. resp_len, max_len);
  3629. break;
  3630. case IB_SMP_ATTR_LED_INFO:
  3631. ret = __subn_set_opa_led_info(smp, am, data, ibdev, port,
  3632. resp_len, max_len);
  3633. break;
  3634. case IB_SMP_ATTR_SM_INFO:
  3635. if (ibp->rvp.port_cap_flags & IB_PORT_SM_DISABLED)
  3636. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  3637. if (ibp->rvp.port_cap_flags & IB_PORT_SM)
  3638. return IB_MAD_RESULT_SUCCESS;
  3639. /* FALLTHROUGH */
  3640. default:
  3641. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  3642. ret = reply((struct ib_mad_hdr *)smp);
  3643. break;
  3644. }
  3645. return ret;
  3646. }
  3647. static inline void set_aggr_error(struct opa_aggregate *ag)
  3648. {
  3649. ag->err_reqlength |= cpu_to_be16(0x8000);
  3650. }
  3651. static int subn_get_opa_aggregate(struct opa_smp *smp,
  3652. struct ib_device *ibdev, u8 port,
  3653. u32 *resp_len)
  3654. {
  3655. int i;
  3656. u32 num_attr = be32_to_cpu(smp->attr_mod) & 0x000000ff;
  3657. u8 *next_smp = opa_get_smp_data(smp);
  3658. if (num_attr < 1 || num_attr > 117) {
  3659. smp->status |= IB_SMP_INVALID_FIELD;
  3660. return reply((struct ib_mad_hdr *)smp);
  3661. }
  3662. for (i = 0; i < num_attr; i++) {
  3663. struct opa_aggregate *agg;
  3664. size_t agg_data_len;
  3665. size_t agg_size;
  3666. u32 am;
  3667. agg = (struct opa_aggregate *)next_smp;
  3668. agg_data_len = (be16_to_cpu(agg->err_reqlength) & 0x007f) * 8;
  3669. agg_size = sizeof(*agg) + agg_data_len;
  3670. am = be32_to_cpu(agg->attr_mod);
  3671. *resp_len += agg_size;
  3672. if (next_smp + agg_size > ((u8 *)smp) + sizeof(*smp)) {
  3673. smp->status |= IB_SMP_INVALID_FIELD;
  3674. return reply((struct ib_mad_hdr *)smp);
  3675. }
  3676. /* zero the payload for this segment */
  3677. memset(next_smp + sizeof(*agg), 0, agg_data_len);
  3678. (void)subn_get_opa_sma(agg->attr_id, smp, am, agg->data,
  3679. ibdev, port, NULL, (u32)agg_data_len);
  3680. if (smp->status & IB_SMP_INVALID_FIELD)
  3681. break;
  3682. if (smp->status & ~IB_SMP_DIRECTION) {
  3683. set_aggr_error(agg);
  3684. return reply((struct ib_mad_hdr *)smp);
  3685. }
  3686. next_smp += agg_size;
  3687. }
  3688. return reply((struct ib_mad_hdr *)smp);
  3689. }
  3690. static int subn_set_opa_aggregate(struct opa_smp *smp,
  3691. struct ib_device *ibdev, u8 port,
  3692. u32 *resp_len, int local_mad)
  3693. {
  3694. int i;
  3695. u32 num_attr = be32_to_cpu(smp->attr_mod) & 0x000000ff;
  3696. u8 *next_smp = opa_get_smp_data(smp);
  3697. if (num_attr < 1 || num_attr > 117) {
  3698. smp->status |= IB_SMP_INVALID_FIELD;
  3699. return reply((struct ib_mad_hdr *)smp);
  3700. }
  3701. for (i = 0; i < num_attr; i++) {
  3702. struct opa_aggregate *agg;
  3703. size_t agg_data_len;
  3704. size_t agg_size;
  3705. u32 am;
  3706. agg = (struct opa_aggregate *)next_smp;
  3707. agg_data_len = (be16_to_cpu(agg->err_reqlength) & 0x007f) * 8;
  3708. agg_size = sizeof(*agg) + agg_data_len;
  3709. am = be32_to_cpu(agg->attr_mod);
  3710. *resp_len += agg_size;
  3711. if (next_smp + agg_size > ((u8 *)smp) + sizeof(*smp)) {
  3712. smp->status |= IB_SMP_INVALID_FIELD;
  3713. return reply((struct ib_mad_hdr *)smp);
  3714. }
  3715. (void)subn_set_opa_sma(agg->attr_id, smp, am, agg->data,
  3716. ibdev, port, NULL, (u32)agg_data_len,
  3717. local_mad);
  3718. if (smp->status & IB_SMP_INVALID_FIELD)
  3719. break;
  3720. if (smp->status & ~IB_SMP_DIRECTION) {
  3721. set_aggr_error(agg);
  3722. return reply((struct ib_mad_hdr *)smp);
  3723. }
  3724. next_smp += agg_size;
  3725. }
  3726. return reply((struct ib_mad_hdr *)smp);
  3727. }
  3728. /*
  3729. * OPAv1 specifies that, on the transition to link up, these counters
  3730. * are cleared:
  3731. * PortRcvErrors [*]
  3732. * LinkErrorRecovery
  3733. * LocalLinkIntegrityErrors
  3734. * ExcessiveBufferOverruns [*]
  3735. *
  3736. * [*] Error info associated with these counters is retained, but the
  3737. * error info status is reset to 0.
  3738. */
  3739. void clear_linkup_counters(struct hfi1_devdata *dd)
  3740. {
  3741. /* PortRcvErrors */
  3742. write_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL, 0);
  3743. dd->err_info_rcvport.status_and_code &= ~OPA_EI_STATUS_SMASK;
  3744. /* LinkErrorRecovery */
  3745. write_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL, 0);
  3746. write_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT, CNTR_INVALID_VL, 0);
  3747. /* LocalLinkIntegrityErrors */
  3748. write_dev_cntr(dd, C_DC_RX_REPLAY, CNTR_INVALID_VL, 0);
  3749. /* ExcessiveBufferOverruns */
  3750. write_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL, 0);
  3751. dd->rcv_ovfl_cnt = 0;
  3752. dd->err_info_xmit_constraint.status &= ~OPA_EI_STATUS_SMASK;
  3753. }
  3754. static int is_full_mgmt_pkey_in_table(struct hfi1_ibport *ibp)
  3755. {
  3756. unsigned int i;
  3757. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3758. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
  3759. if (ppd->pkeys[i] == FULL_MGMT_P_KEY)
  3760. return 1;
  3761. return 0;
  3762. }
  3763. /*
  3764. * is_local_mad() returns 1 if 'mad' is sent from, and destined to the
  3765. * local node, 0 otherwise.
  3766. */
  3767. static int is_local_mad(struct hfi1_ibport *ibp, const struct opa_mad *mad,
  3768. const struct ib_wc *in_wc)
  3769. {
  3770. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3771. const struct opa_smp *smp = (const struct opa_smp *)mad;
  3772. if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  3773. return (smp->hop_cnt == 0 &&
  3774. smp->route.dr.dr_slid == OPA_LID_PERMISSIVE &&
  3775. smp->route.dr.dr_dlid == OPA_LID_PERMISSIVE);
  3776. }
  3777. return (in_wc->slid == ppd->lid);
  3778. }
  3779. /*
  3780. * opa_local_smp_check() should only be called on MADs for which
  3781. * is_local_mad() returns true. It applies the SMP checks that are
  3782. * specific to SMPs which are sent from, and destined to this node.
  3783. * opa_local_smp_check() returns 0 if the SMP passes its checks, 1
  3784. * otherwise.
  3785. *
  3786. * SMPs which arrive from other nodes are instead checked by
  3787. * opa_smp_check().
  3788. */
  3789. static int opa_local_smp_check(struct hfi1_ibport *ibp,
  3790. const struct ib_wc *in_wc)
  3791. {
  3792. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  3793. u16 pkey;
  3794. if (in_wc->pkey_index >= ARRAY_SIZE(ppd->pkeys))
  3795. return 1;
  3796. pkey = ppd->pkeys[in_wc->pkey_index];
  3797. /*
  3798. * We need to do the "node-local" checks specified in OPAv1,
  3799. * rev 0.90, section 9.10.26, which are:
  3800. * - pkey is 0x7fff, or 0xffff
  3801. * - Source QPN == 0 || Destination QPN == 0
  3802. * - the MAD header's management class is either
  3803. * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE or
  3804. * IB_MGMT_CLASS_SUBN_LID_ROUTED
  3805. * - SLID != 0
  3806. *
  3807. * However, we know (and so don't need to check again) that,
  3808. * for local SMPs, the MAD stack passes MADs with:
  3809. * - Source QPN of 0
  3810. * - MAD mgmt_class is IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
  3811. * - SLID is either: OPA_LID_PERMISSIVE (0xFFFFFFFF), or
  3812. * our own port's lid
  3813. *
  3814. */
  3815. if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY)
  3816. return 0;
  3817. ingress_pkey_table_fail(ppd, pkey, in_wc->slid);
  3818. return 1;
  3819. }
  3820. /**
  3821. * hfi1_pkey_validation_pma - It validates PKEYs for incoming PMA MAD packets.
  3822. * @ibp: IB port data
  3823. * @in_mad: MAD packet with header and data
  3824. * @in_wc: Work completion data such as source LID, port number, etc.
  3825. *
  3826. * These are all the possible logic rules for validating a pkey:
  3827. *
  3828. * a) If pkey neither FULL_MGMT_P_KEY nor LIM_MGMT_P_KEY,
  3829. * and NOT self-originated packet:
  3830. * Drop MAD packet as it should always be part of the
  3831. * management partition unless it's a self-originated packet.
  3832. *
  3833. * b) If pkey_index -> FULL_MGMT_P_KEY, and LIM_MGMT_P_KEY in pkey table:
  3834. * The packet is coming from a management node and the receiving node
  3835. * is also a management node, so it is safe for the packet to go through.
  3836. *
  3837. * c) If pkey_index -> FULL_MGMT_P_KEY, and LIM_MGMT_P_KEY is NOT in pkey table:
  3838. * Drop the packet as LIM_MGMT_P_KEY should always be in the pkey table.
  3839. * It could be an FM misconfiguration.
  3840. *
  3841. * d) If pkey_index -> LIM_MGMT_P_KEY and FULL_MGMT_P_KEY is NOT in pkey table:
  3842. * It is safe for the packet to go through since a non-management node is
  3843. * talking to another non-management node.
  3844. *
  3845. * e) If pkey_index -> LIM_MGMT_P_KEY and FULL_MGMT_P_KEY in pkey table:
  3846. * Drop the packet because a non-management node is talking to a
  3847. * management node, and it could be an attack.
  3848. *
  3849. * For the implementation, these rules can be simplied to only checking
  3850. * for (a) and (e). There's no need to check for rule (b) as
  3851. * the packet doesn't need to be dropped. Rule (c) is not possible in
  3852. * the driver as LIM_MGMT_P_KEY is always in the pkey table.
  3853. *
  3854. * Return:
  3855. * 0 - pkey is okay, -EINVAL it's a bad pkey
  3856. */
  3857. static int hfi1_pkey_validation_pma(struct hfi1_ibport *ibp,
  3858. const struct opa_mad *in_mad,
  3859. const struct ib_wc *in_wc)
  3860. {
  3861. u16 pkey_value = hfi1_lookup_pkey_value(ibp, in_wc->pkey_index);
  3862. /* Rule (a) from above */
  3863. if (!is_local_mad(ibp, in_mad, in_wc) &&
  3864. pkey_value != LIM_MGMT_P_KEY &&
  3865. pkey_value != FULL_MGMT_P_KEY)
  3866. return -EINVAL;
  3867. /* Rule (e) from above */
  3868. if (pkey_value == LIM_MGMT_P_KEY &&
  3869. is_full_mgmt_pkey_in_table(ibp))
  3870. return -EINVAL;
  3871. return 0;
  3872. }
  3873. static int process_subn_opa(struct ib_device *ibdev, int mad_flags,
  3874. u8 port, const struct opa_mad *in_mad,
  3875. struct opa_mad *out_mad,
  3876. u32 *resp_len, int local_mad)
  3877. {
  3878. struct opa_smp *smp = (struct opa_smp *)out_mad;
  3879. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3880. u8 *data;
  3881. u32 am, data_size;
  3882. __be16 attr_id;
  3883. int ret;
  3884. *out_mad = *in_mad;
  3885. data = opa_get_smp_data(smp);
  3886. data_size = (u32)opa_get_smp_data_size(smp);
  3887. am = be32_to_cpu(smp->attr_mod);
  3888. attr_id = smp->attr_id;
  3889. if (smp->class_version != OPA_SM_CLASS_VERSION) {
  3890. smp->status |= IB_SMP_UNSUP_VERSION;
  3891. ret = reply((struct ib_mad_hdr *)smp);
  3892. return ret;
  3893. }
  3894. ret = check_mkey(ibp, (struct ib_mad_hdr *)smp, mad_flags, smp->mkey,
  3895. smp->route.dr.dr_slid, smp->route.dr.return_path,
  3896. smp->hop_cnt);
  3897. if (ret) {
  3898. u32 port_num = be32_to_cpu(smp->attr_mod);
  3899. /*
  3900. * If this is a get/set portinfo, we already check the
  3901. * M_Key if the MAD is for another port and the M_Key
  3902. * is OK on the receiving port. This check is needed
  3903. * to increment the error counters when the M_Key
  3904. * fails to match on *both* ports.
  3905. */
  3906. if (attr_id == IB_SMP_ATTR_PORT_INFO &&
  3907. (smp->method == IB_MGMT_METHOD_GET ||
  3908. smp->method == IB_MGMT_METHOD_SET) &&
  3909. port_num && port_num <= ibdev->phys_port_cnt &&
  3910. port != port_num)
  3911. (void)check_mkey(to_iport(ibdev, port_num),
  3912. (struct ib_mad_hdr *)smp, 0,
  3913. smp->mkey, smp->route.dr.dr_slid,
  3914. smp->route.dr.return_path,
  3915. smp->hop_cnt);
  3916. ret = IB_MAD_RESULT_FAILURE;
  3917. return ret;
  3918. }
  3919. *resp_len = opa_get_smp_header_size(smp);
  3920. switch (smp->method) {
  3921. case IB_MGMT_METHOD_GET:
  3922. switch (attr_id) {
  3923. default:
  3924. clear_opa_smp_data(smp);
  3925. ret = subn_get_opa_sma(attr_id, smp, am, data,
  3926. ibdev, port, resp_len,
  3927. data_size);
  3928. break;
  3929. case OPA_ATTRIB_ID_AGGREGATE:
  3930. ret = subn_get_opa_aggregate(smp, ibdev, port,
  3931. resp_len);
  3932. break;
  3933. }
  3934. break;
  3935. case IB_MGMT_METHOD_SET:
  3936. switch (attr_id) {
  3937. default:
  3938. ret = subn_set_opa_sma(attr_id, smp, am, data,
  3939. ibdev, port, resp_len,
  3940. data_size, local_mad);
  3941. break;
  3942. case OPA_ATTRIB_ID_AGGREGATE:
  3943. ret = subn_set_opa_aggregate(smp, ibdev, port,
  3944. resp_len, local_mad);
  3945. break;
  3946. }
  3947. break;
  3948. case IB_MGMT_METHOD_TRAP:
  3949. case IB_MGMT_METHOD_REPORT:
  3950. case IB_MGMT_METHOD_REPORT_RESP:
  3951. case IB_MGMT_METHOD_GET_RESP:
  3952. /*
  3953. * The ib_mad module will call us to process responses
  3954. * before checking for other consumers.
  3955. * Just tell the caller to process it normally.
  3956. */
  3957. ret = IB_MAD_RESULT_SUCCESS;
  3958. break;
  3959. case IB_MGMT_METHOD_TRAP_REPRESS:
  3960. subn_handle_opa_trap_repress(ibp, smp);
  3961. /* Always successful */
  3962. ret = IB_MAD_RESULT_SUCCESS;
  3963. break;
  3964. default:
  3965. smp->status |= IB_SMP_UNSUP_METHOD;
  3966. ret = reply((struct ib_mad_hdr *)smp);
  3967. break;
  3968. }
  3969. return ret;
  3970. }
  3971. static int process_subn(struct ib_device *ibdev, int mad_flags,
  3972. u8 port, const struct ib_mad *in_mad,
  3973. struct ib_mad *out_mad)
  3974. {
  3975. struct ib_smp *smp = (struct ib_smp *)out_mad;
  3976. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  3977. int ret;
  3978. *out_mad = *in_mad;
  3979. if (smp->class_version != 1) {
  3980. smp->status |= IB_SMP_UNSUP_VERSION;
  3981. ret = reply((struct ib_mad_hdr *)smp);
  3982. return ret;
  3983. }
  3984. ret = check_mkey(ibp, (struct ib_mad_hdr *)smp, mad_flags,
  3985. smp->mkey, (__force __be32)smp->dr_slid,
  3986. smp->return_path, smp->hop_cnt);
  3987. if (ret) {
  3988. u32 port_num = be32_to_cpu(smp->attr_mod);
  3989. /*
  3990. * If this is a get/set portinfo, we already check the
  3991. * M_Key if the MAD is for another port and the M_Key
  3992. * is OK on the receiving port. This check is needed
  3993. * to increment the error counters when the M_Key
  3994. * fails to match on *both* ports.
  3995. */
  3996. if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
  3997. (smp->method == IB_MGMT_METHOD_GET ||
  3998. smp->method == IB_MGMT_METHOD_SET) &&
  3999. port_num && port_num <= ibdev->phys_port_cnt &&
  4000. port != port_num)
  4001. (void)check_mkey(to_iport(ibdev, port_num),
  4002. (struct ib_mad_hdr *)smp, 0,
  4003. smp->mkey,
  4004. (__force __be32)smp->dr_slid,
  4005. smp->return_path, smp->hop_cnt);
  4006. ret = IB_MAD_RESULT_FAILURE;
  4007. return ret;
  4008. }
  4009. switch (smp->method) {
  4010. case IB_MGMT_METHOD_GET:
  4011. switch (smp->attr_id) {
  4012. case IB_SMP_ATTR_NODE_INFO:
  4013. ret = subn_get_nodeinfo(smp, ibdev, port);
  4014. break;
  4015. default:
  4016. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  4017. ret = reply((struct ib_mad_hdr *)smp);
  4018. break;
  4019. }
  4020. break;
  4021. }
  4022. return ret;
  4023. }
  4024. static int process_perf(struct ib_device *ibdev, u8 port,
  4025. const struct ib_mad *in_mad,
  4026. struct ib_mad *out_mad)
  4027. {
  4028. struct ib_pma_mad *pmp = (struct ib_pma_mad *)out_mad;
  4029. struct ib_class_port_info *cpi = (struct ib_class_port_info *)
  4030. &pmp->data;
  4031. int ret = IB_MAD_RESULT_FAILURE;
  4032. *out_mad = *in_mad;
  4033. if (pmp->mad_hdr.class_version != 1) {
  4034. pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
  4035. ret = reply((struct ib_mad_hdr *)pmp);
  4036. return ret;
  4037. }
  4038. switch (pmp->mad_hdr.method) {
  4039. case IB_MGMT_METHOD_GET:
  4040. switch (pmp->mad_hdr.attr_id) {
  4041. case IB_PMA_PORT_COUNTERS:
  4042. ret = pma_get_ib_portcounters(pmp, ibdev, port);
  4043. break;
  4044. case IB_PMA_PORT_COUNTERS_EXT:
  4045. ret = pma_get_ib_portcounters_ext(pmp, ibdev, port);
  4046. break;
  4047. case IB_PMA_CLASS_PORT_INFO:
  4048. cpi->capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
  4049. ret = reply((struct ib_mad_hdr *)pmp);
  4050. break;
  4051. default:
  4052. pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
  4053. ret = reply((struct ib_mad_hdr *)pmp);
  4054. break;
  4055. }
  4056. break;
  4057. case IB_MGMT_METHOD_SET:
  4058. if (pmp->mad_hdr.attr_id) {
  4059. pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
  4060. ret = reply((struct ib_mad_hdr *)pmp);
  4061. }
  4062. break;
  4063. case IB_MGMT_METHOD_TRAP:
  4064. case IB_MGMT_METHOD_GET_RESP:
  4065. /*
  4066. * The ib_mad module will call us to process responses
  4067. * before checking for other consumers.
  4068. * Just tell the caller to process it normally.
  4069. */
  4070. ret = IB_MAD_RESULT_SUCCESS;
  4071. break;
  4072. default:
  4073. pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
  4074. ret = reply((struct ib_mad_hdr *)pmp);
  4075. break;
  4076. }
  4077. return ret;
  4078. }
  4079. static int process_perf_opa(struct ib_device *ibdev, u8 port,
  4080. const struct opa_mad *in_mad,
  4081. struct opa_mad *out_mad, u32 *resp_len)
  4082. {
  4083. struct opa_pma_mad *pmp = (struct opa_pma_mad *)out_mad;
  4084. int ret;
  4085. *out_mad = *in_mad;
  4086. if (pmp->mad_hdr.class_version != OPA_SM_CLASS_VERSION) {
  4087. pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
  4088. return reply((struct ib_mad_hdr *)pmp);
  4089. }
  4090. *resp_len = sizeof(pmp->mad_hdr);
  4091. switch (pmp->mad_hdr.method) {
  4092. case IB_MGMT_METHOD_GET:
  4093. switch (pmp->mad_hdr.attr_id) {
  4094. case IB_PMA_CLASS_PORT_INFO:
  4095. ret = pma_get_opa_classportinfo(pmp, ibdev, resp_len);
  4096. break;
  4097. case OPA_PM_ATTRIB_ID_PORT_STATUS:
  4098. ret = pma_get_opa_portstatus(pmp, ibdev, port,
  4099. resp_len);
  4100. break;
  4101. case OPA_PM_ATTRIB_ID_DATA_PORT_COUNTERS:
  4102. ret = pma_get_opa_datacounters(pmp, ibdev, port,
  4103. resp_len);
  4104. break;
  4105. case OPA_PM_ATTRIB_ID_ERROR_PORT_COUNTERS:
  4106. ret = pma_get_opa_porterrors(pmp, ibdev, port,
  4107. resp_len);
  4108. break;
  4109. case OPA_PM_ATTRIB_ID_ERROR_INFO:
  4110. ret = pma_get_opa_errorinfo(pmp, ibdev, port,
  4111. resp_len);
  4112. break;
  4113. default:
  4114. pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
  4115. ret = reply((struct ib_mad_hdr *)pmp);
  4116. break;
  4117. }
  4118. break;
  4119. case IB_MGMT_METHOD_SET:
  4120. switch (pmp->mad_hdr.attr_id) {
  4121. case OPA_PM_ATTRIB_ID_CLEAR_PORT_STATUS:
  4122. ret = pma_set_opa_portstatus(pmp, ibdev, port,
  4123. resp_len);
  4124. break;
  4125. case OPA_PM_ATTRIB_ID_ERROR_INFO:
  4126. ret = pma_set_opa_errorinfo(pmp, ibdev, port,
  4127. resp_len);
  4128. break;
  4129. default:
  4130. pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
  4131. ret = reply((struct ib_mad_hdr *)pmp);
  4132. break;
  4133. }
  4134. break;
  4135. case IB_MGMT_METHOD_TRAP:
  4136. case IB_MGMT_METHOD_GET_RESP:
  4137. /*
  4138. * The ib_mad module will call us to process responses
  4139. * before checking for other consumers.
  4140. * Just tell the caller to process it normally.
  4141. */
  4142. ret = IB_MAD_RESULT_SUCCESS;
  4143. break;
  4144. default:
  4145. pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
  4146. ret = reply((struct ib_mad_hdr *)pmp);
  4147. break;
  4148. }
  4149. return ret;
  4150. }
  4151. static int hfi1_process_opa_mad(struct ib_device *ibdev, int mad_flags,
  4152. u8 port, const struct ib_wc *in_wc,
  4153. const struct ib_grh *in_grh,
  4154. const struct opa_mad *in_mad,
  4155. struct opa_mad *out_mad, size_t *out_mad_size,
  4156. u16 *out_mad_pkey_index)
  4157. {
  4158. int ret;
  4159. int pkey_idx;
  4160. int local_mad = 0;
  4161. u32 resp_len = in_wc->byte_len - sizeof(*in_grh);
  4162. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  4163. pkey_idx = hfi1_lookup_pkey_idx(ibp, LIM_MGMT_P_KEY);
  4164. if (pkey_idx < 0) {
  4165. pr_warn("failed to find limited mgmt pkey, defaulting 0x%x\n",
  4166. hfi1_get_pkey(ibp, 1));
  4167. pkey_idx = 1;
  4168. }
  4169. *out_mad_pkey_index = (u16)pkey_idx;
  4170. switch (in_mad->mad_hdr.mgmt_class) {
  4171. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  4172. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  4173. local_mad = is_local_mad(ibp, in_mad, in_wc);
  4174. if (local_mad) {
  4175. ret = opa_local_smp_check(ibp, in_wc);
  4176. if (ret)
  4177. return IB_MAD_RESULT_FAILURE;
  4178. }
  4179. ret = process_subn_opa(ibdev, mad_flags, port, in_mad,
  4180. out_mad, &resp_len, local_mad);
  4181. goto bail;
  4182. case IB_MGMT_CLASS_PERF_MGMT:
  4183. ret = hfi1_pkey_validation_pma(ibp, in_mad, in_wc);
  4184. if (ret)
  4185. return IB_MAD_RESULT_FAILURE;
  4186. ret = process_perf_opa(ibdev, port, in_mad, out_mad, &resp_len);
  4187. goto bail;
  4188. default:
  4189. ret = IB_MAD_RESULT_SUCCESS;
  4190. }
  4191. bail:
  4192. if (ret & IB_MAD_RESULT_REPLY)
  4193. *out_mad_size = round_up(resp_len, 8);
  4194. else if (ret & IB_MAD_RESULT_SUCCESS)
  4195. *out_mad_size = in_wc->byte_len - sizeof(struct ib_grh);
  4196. return ret;
  4197. }
  4198. static int hfi1_process_ib_mad(struct ib_device *ibdev, int mad_flags, u8 port,
  4199. const struct ib_wc *in_wc,
  4200. const struct ib_grh *in_grh,
  4201. const struct ib_mad *in_mad,
  4202. struct ib_mad *out_mad)
  4203. {
  4204. int ret;
  4205. switch (in_mad->mad_hdr.mgmt_class) {
  4206. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  4207. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  4208. ret = process_subn(ibdev, mad_flags, port, in_mad, out_mad);
  4209. break;
  4210. case IB_MGMT_CLASS_PERF_MGMT:
  4211. ret = process_perf(ibdev, port, in_mad, out_mad);
  4212. break;
  4213. default:
  4214. ret = IB_MAD_RESULT_SUCCESS;
  4215. break;
  4216. }
  4217. return ret;
  4218. }
  4219. /**
  4220. * hfi1_process_mad - process an incoming MAD packet
  4221. * @ibdev: the infiniband device this packet came in on
  4222. * @mad_flags: MAD flags
  4223. * @port: the port number this packet came in on
  4224. * @in_wc: the work completion entry for this packet
  4225. * @in_grh: the global route header for this packet
  4226. * @in_mad: the incoming MAD
  4227. * @out_mad: any outgoing MAD reply
  4228. *
  4229. * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
  4230. * interested in processing.
  4231. *
  4232. * Note that the verbs framework has already done the MAD sanity checks,
  4233. * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
  4234. * MADs.
  4235. *
  4236. * This is called by the ib_mad module.
  4237. */
  4238. int hfi1_process_mad(struct ib_device *ibdev, int mad_flags, u8 port,
  4239. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  4240. const struct ib_mad_hdr *in_mad, size_t in_mad_size,
  4241. struct ib_mad_hdr *out_mad, size_t *out_mad_size,
  4242. u16 *out_mad_pkey_index)
  4243. {
  4244. switch (in_mad->base_version) {
  4245. case OPA_MGMT_BASE_VERSION:
  4246. if (unlikely(in_mad_size != sizeof(struct opa_mad))) {
  4247. dev_err(ibdev->dev.parent, "invalid in_mad_size\n");
  4248. return IB_MAD_RESULT_FAILURE;
  4249. }
  4250. return hfi1_process_opa_mad(ibdev, mad_flags, port,
  4251. in_wc, in_grh,
  4252. (struct opa_mad *)in_mad,
  4253. (struct opa_mad *)out_mad,
  4254. out_mad_size,
  4255. out_mad_pkey_index);
  4256. case IB_MGMT_BASE_VERSION:
  4257. return hfi1_process_ib_mad(ibdev, mad_flags, port,
  4258. in_wc, in_grh,
  4259. (const struct ib_mad *)in_mad,
  4260. (struct ib_mad *)out_mad);
  4261. default:
  4262. break;
  4263. }
  4264. return IB_MAD_RESULT_FAILURE;
  4265. }