ipmi_msghandler.c 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ipmi_msghandler.c
  4. *
  5. * Incoming and outgoing message routing for an IPMI interface.
  6. *
  7. * Author: MontaVista Software, Inc.
  8. * Corey Minyard <[email protected]>
  9. * [email protected]
  10. *
  11. * Copyright 2002 MontaVista Software Inc.
  12. */
  13. #define pr_fmt(fmt) "IPMI message handler: " fmt
  14. #define dev_fmt(fmt) pr_fmt(fmt)
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/panic_notifier.h>
  18. #include <linux/poll.h>
  19. #include <linux/sched.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/mutex.h>
  23. #include <linux/slab.h>
  24. #include <linux/ipmi.h>
  25. #include <linux/ipmi_smi.h>
  26. #include <linux/notifier.h>
  27. #include <linux/init.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/uuid.h>
  34. #include <linux/nospec.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/delay.h>
  37. #define IPMI_DRIVER_VERSION "39.2"
  38. static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
  39. static int ipmi_init_msghandler(void);
  40. static void smi_recv_tasklet(struct tasklet_struct *t);
  41. static void handle_new_recv_msgs(struct ipmi_smi *intf);
  42. static void need_waiter(struct ipmi_smi *intf);
  43. static int handle_one_recv_msg(struct ipmi_smi *intf,
  44. struct ipmi_smi_msg *msg);
  45. static bool initialized;
  46. static bool drvregistered;
  47. /* Numbers in this enumerator should be mapped to ipmi_panic_event_str */
  48. enum ipmi_panic_event_op {
  49. IPMI_SEND_PANIC_EVENT_NONE,
  50. IPMI_SEND_PANIC_EVENT,
  51. IPMI_SEND_PANIC_EVENT_STRING,
  52. IPMI_SEND_PANIC_EVENT_MAX
  53. };
  54. /* Indices in this array should be mapped to enum ipmi_panic_event_op */
  55. static const char *const ipmi_panic_event_str[] = { "none", "event", "string", NULL };
  56. #ifdef CONFIG_IPMI_PANIC_STRING
  57. #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
  58. #elif defined(CONFIG_IPMI_PANIC_EVENT)
  59. #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
  60. #else
  61. #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
  62. #endif
  63. static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT;
  64. static int panic_op_write_handler(const char *val,
  65. const struct kernel_param *kp)
  66. {
  67. char valcp[16];
  68. int e;
  69. strscpy(valcp, val, sizeof(valcp));
  70. e = match_string(ipmi_panic_event_str, -1, strstrip(valcp));
  71. if (e < 0)
  72. return e;
  73. ipmi_send_panic_event = e;
  74. return 0;
  75. }
  76. static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
  77. {
  78. const char *event_str;
  79. if (ipmi_send_panic_event >= IPMI_SEND_PANIC_EVENT_MAX)
  80. event_str = "???";
  81. else
  82. event_str = ipmi_panic_event_str[ipmi_send_panic_event];
  83. return sprintf(buffer, "%s\n", event_str);
  84. }
  85. static const struct kernel_param_ops panic_op_ops = {
  86. .set = panic_op_write_handler,
  87. .get = panic_op_read_handler
  88. };
  89. module_param_cb(panic_op, &panic_op_ops, NULL, 0600);
  90. MODULE_PARM_DESC(panic_op, "Sets if the IPMI driver will attempt to store panic information in the event log in the event of a panic. Set to 'none' for no, 'event' for a single event, or 'string' for a generic event and the panic string in IPMI OEM events.");
  91. #define MAX_EVENTS_IN_QUEUE 25
  92. /* Remain in auto-maintenance mode for this amount of time (in ms). */
  93. static unsigned long maintenance_mode_timeout_ms = 30000;
  94. module_param(maintenance_mode_timeout_ms, ulong, 0644);
  95. MODULE_PARM_DESC(maintenance_mode_timeout_ms,
  96. "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode.");
  97. /*
  98. * Don't let a message sit in a queue forever, always time it with at lest
  99. * the max message timer. This is in milliseconds.
  100. */
  101. #define MAX_MSG_TIMEOUT 60000
  102. /*
  103. * Timeout times below are in milliseconds, and are done off a 1
  104. * second timer. So setting the value to 1000 would mean anything
  105. * between 0 and 1000ms. So really the only reasonable minimum
  106. * setting it 2000ms, which is between 1 and 2 seconds.
  107. */
  108. /* The default timeout for message retries. */
  109. static unsigned long default_retry_ms = 2000;
  110. module_param(default_retry_ms, ulong, 0644);
  111. MODULE_PARM_DESC(default_retry_ms,
  112. "The time (milliseconds) between retry sends");
  113. /* The default timeout for maintenance mode message retries. */
  114. static unsigned long default_maintenance_retry_ms = 3000;
  115. module_param(default_maintenance_retry_ms, ulong, 0644);
  116. MODULE_PARM_DESC(default_maintenance_retry_ms,
  117. "The time (milliseconds) between retry sends in maintenance mode");
  118. /* The default maximum number of retries */
  119. static unsigned int default_max_retries = 4;
  120. module_param(default_max_retries, uint, 0644);
  121. MODULE_PARM_DESC(default_max_retries,
  122. "The time (milliseconds) between retry sends in maintenance mode");
  123. /* The default maximum number of users that may register. */
  124. static unsigned int max_users = 30;
  125. module_param(max_users, uint, 0644);
  126. MODULE_PARM_DESC(max_users,
  127. "The most users that may use the IPMI stack at one time.");
  128. /* The default maximum number of message a user may have outstanding. */
  129. static unsigned int max_msgs_per_user = 100;
  130. module_param(max_msgs_per_user, uint, 0644);
  131. MODULE_PARM_DESC(max_msgs_per_user,
  132. "The most message a user may have outstanding.");
  133. /* Call every ~1000 ms. */
  134. #define IPMI_TIMEOUT_TIME 1000
  135. /* How many jiffies does it take to get to the timeout time. */
  136. #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
  137. /*
  138. * Request events from the queue every second (this is the number of
  139. * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
  140. * future, IPMI will add a way to know immediately if an event is in
  141. * the queue and this silliness can go away.
  142. */
  143. #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
  144. /* How long should we cache dynamic device IDs? */
  145. #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
  146. /*
  147. * The main "user" data structure.
  148. */
  149. struct ipmi_user {
  150. struct list_head link;
  151. /*
  152. * Set to NULL when the user is destroyed, a pointer to myself
  153. * so srcu_dereference can be used on it.
  154. */
  155. struct ipmi_user *self;
  156. struct srcu_struct release_barrier;
  157. struct kref refcount;
  158. /* The upper layer that handles receive messages. */
  159. const struct ipmi_user_hndl *handler;
  160. void *handler_data;
  161. /* The interface this user is bound to. */
  162. struct ipmi_smi *intf;
  163. /* Does this interface receive IPMI events? */
  164. bool gets_events;
  165. atomic_t nr_msgs;
  166. /* Free must run in process context for RCU cleanup. */
  167. struct work_struct remove_work;
  168. };
  169. static struct workqueue_struct *remove_work_wq;
  170. static struct ipmi_user *acquire_ipmi_user(struct ipmi_user *user, int *index)
  171. __acquires(user->release_barrier)
  172. {
  173. struct ipmi_user *ruser;
  174. *index = srcu_read_lock(&user->release_barrier);
  175. ruser = srcu_dereference(user->self, &user->release_barrier);
  176. if (!ruser)
  177. srcu_read_unlock(&user->release_barrier, *index);
  178. return ruser;
  179. }
  180. static void release_ipmi_user(struct ipmi_user *user, int index)
  181. {
  182. srcu_read_unlock(&user->release_barrier, index);
  183. }
  184. struct cmd_rcvr {
  185. struct list_head link;
  186. struct ipmi_user *user;
  187. unsigned char netfn;
  188. unsigned char cmd;
  189. unsigned int chans;
  190. /*
  191. * This is used to form a linked lised during mass deletion.
  192. * Since this is in an RCU list, we cannot use the link above
  193. * or change any data until the RCU period completes. So we
  194. * use this next variable during mass deletion so we can have
  195. * a list and don't have to wait and restart the search on
  196. * every individual deletion of a command.
  197. */
  198. struct cmd_rcvr *next;
  199. };
  200. struct seq_table {
  201. unsigned int inuse : 1;
  202. unsigned int broadcast : 1;
  203. unsigned long timeout;
  204. unsigned long orig_timeout;
  205. unsigned int retries_left;
  206. /*
  207. * To verify on an incoming send message response that this is
  208. * the message that the response is for, we keep a sequence id
  209. * and increment it every time we send a message.
  210. */
  211. long seqid;
  212. /*
  213. * This is held so we can properly respond to the message on a
  214. * timeout, and it is used to hold the temporary data for
  215. * retransmission, too.
  216. */
  217. struct ipmi_recv_msg *recv_msg;
  218. };
  219. /*
  220. * Store the information in a msgid (long) to allow us to find a
  221. * sequence table entry from the msgid.
  222. */
  223. #define STORE_SEQ_IN_MSGID(seq, seqid) \
  224. ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
  225. #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
  226. do { \
  227. seq = (((msgid) >> 26) & 0x3f); \
  228. seqid = ((msgid) & 0x3ffffff); \
  229. } while (0)
  230. #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
  231. #define IPMI_MAX_CHANNELS 16
  232. struct ipmi_channel {
  233. unsigned char medium;
  234. unsigned char protocol;
  235. };
  236. struct ipmi_channel_set {
  237. struct ipmi_channel c[IPMI_MAX_CHANNELS];
  238. };
  239. struct ipmi_my_addrinfo {
  240. /*
  241. * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
  242. * but may be changed by the user.
  243. */
  244. unsigned char address;
  245. /*
  246. * My LUN. This should generally stay the SMS LUN, but just in
  247. * case...
  248. */
  249. unsigned char lun;
  250. };
  251. /*
  252. * Note that the product id, manufacturer id, guid, and device id are
  253. * immutable in this structure, so dyn_mutex is not required for
  254. * accessing those. If those change on a BMC, a new BMC is allocated.
  255. */
  256. struct bmc_device {
  257. struct platform_device pdev;
  258. struct list_head intfs; /* Interfaces on this BMC. */
  259. struct ipmi_device_id id;
  260. struct ipmi_device_id fetch_id;
  261. int dyn_id_set;
  262. unsigned long dyn_id_expiry;
  263. struct mutex dyn_mutex; /* Protects id, intfs, & dyn* */
  264. guid_t guid;
  265. guid_t fetch_guid;
  266. int dyn_guid_set;
  267. struct kref usecount;
  268. struct work_struct remove_work;
  269. unsigned char cc; /* completion code */
  270. };
  271. #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
  272. static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
  273. struct ipmi_device_id *id,
  274. bool *guid_set, guid_t *guid);
  275. /*
  276. * Various statistics for IPMI, these index stats[] in the ipmi_smi
  277. * structure.
  278. */
  279. enum ipmi_stat_indexes {
  280. /* Commands we got from the user that were invalid. */
  281. IPMI_STAT_sent_invalid_commands = 0,
  282. /* Commands we sent to the MC. */
  283. IPMI_STAT_sent_local_commands,
  284. /* Responses from the MC that were delivered to a user. */
  285. IPMI_STAT_handled_local_responses,
  286. /* Responses from the MC that were not delivered to a user. */
  287. IPMI_STAT_unhandled_local_responses,
  288. /* Commands we sent out to the IPMB bus. */
  289. IPMI_STAT_sent_ipmb_commands,
  290. /* Commands sent on the IPMB that had errors on the SEND CMD */
  291. IPMI_STAT_sent_ipmb_command_errs,
  292. /* Each retransmit increments this count. */
  293. IPMI_STAT_retransmitted_ipmb_commands,
  294. /*
  295. * When a message times out (runs out of retransmits) this is
  296. * incremented.
  297. */
  298. IPMI_STAT_timed_out_ipmb_commands,
  299. /*
  300. * This is like above, but for broadcasts. Broadcasts are
  301. * *not* included in the above count (they are expected to
  302. * time out).
  303. */
  304. IPMI_STAT_timed_out_ipmb_broadcasts,
  305. /* Responses I have sent to the IPMB bus. */
  306. IPMI_STAT_sent_ipmb_responses,
  307. /* The response was delivered to the user. */
  308. IPMI_STAT_handled_ipmb_responses,
  309. /* The response had invalid data in it. */
  310. IPMI_STAT_invalid_ipmb_responses,
  311. /* The response didn't have anyone waiting for it. */
  312. IPMI_STAT_unhandled_ipmb_responses,
  313. /* Commands we sent out to the IPMB bus. */
  314. IPMI_STAT_sent_lan_commands,
  315. /* Commands sent on the IPMB that had errors on the SEND CMD */
  316. IPMI_STAT_sent_lan_command_errs,
  317. /* Each retransmit increments this count. */
  318. IPMI_STAT_retransmitted_lan_commands,
  319. /*
  320. * When a message times out (runs out of retransmits) this is
  321. * incremented.
  322. */
  323. IPMI_STAT_timed_out_lan_commands,
  324. /* Responses I have sent to the IPMB bus. */
  325. IPMI_STAT_sent_lan_responses,
  326. /* The response was delivered to the user. */
  327. IPMI_STAT_handled_lan_responses,
  328. /* The response had invalid data in it. */
  329. IPMI_STAT_invalid_lan_responses,
  330. /* The response didn't have anyone waiting for it. */
  331. IPMI_STAT_unhandled_lan_responses,
  332. /* The command was delivered to the user. */
  333. IPMI_STAT_handled_commands,
  334. /* The command had invalid data in it. */
  335. IPMI_STAT_invalid_commands,
  336. /* The command didn't have anyone waiting for it. */
  337. IPMI_STAT_unhandled_commands,
  338. /* Invalid data in an event. */
  339. IPMI_STAT_invalid_events,
  340. /* Events that were received with the proper format. */
  341. IPMI_STAT_events,
  342. /* Retransmissions on IPMB that failed. */
  343. IPMI_STAT_dropped_rexmit_ipmb_commands,
  344. /* Retransmissions on LAN that failed. */
  345. IPMI_STAT_dropped_rexmit_lan_commands,
  346. /* This *must* remain last, add new values above this. */
  347. IPMI_NUM_STATS
  348. };
  349. #define IPMI_IPMB_NUM_SEQ 64
  350. struct ipmi_smi {
  351. struct module *owner;
  352. /* What interface number are we? */
  353. int intf_num;
  354. struct kref refcount;
  355. /* Set when the interface is being unregistered. */
  356. bool in_shutdown;
  357. /* Used for a list of interfaces. */
  358. struct list_head link;
  359. /*
  360. * The list of upper layers that are using me. seq_lock write
  361. * protects this. Read protection is with srcu.
  362. */
  363. struct list_head users;
  364. struct srcu_struct users_srcu;
  365. atomic_t nr_users;
  366. struct device_attribute nr_users_devattr;
  367. struct device_attribute nr_msgs_devattr;
  368. /* Used for wake ups at startup. */
  369. wait_queue_head_t waitq;
  370. /*
  371. * Prevents the interface from being unregistered when the
  372. * interface is used by being looked up through the BMC
  373. * structure.
  374. */
  375. struct mutex bmc_reg_mutex;
  376. struct bmc_device tmp_bmc;
  377. struct bmc_device *bmc;
  378. bool bmc_registered;
  379. struct list_head bmc_link;
  380. char *my_dev_name;
  381. bool in_bmc_register; /* Handle recursive situations. Yuck. */
  382. struct work_struct bmc_reg_work;
  383. const struct ipmi_smi_handlers *handlers;
  384. void *send_info;
  385. /* Driver-model device for the system interface. */
  386. struct device *si_dev;
  387. /*
  388. * A table of sequence numbers for this interface. We use the
  389. * sequence numbers for IPMB messages that go out of the
  390. * interface to match them up with their responses. A routine
  391. * is called periodically to time the items in this list.
  392. */
  393. spinlock_t seq_lock;
  394. struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
  395. int curr_seq;
  396. /*
  397. * Messages queued for delivery. If delivery fails (out of memory
  398. * for instance), They will stay in here to be processed later in a
  399. * periodic timer interrupt. The tasklet is for handling received
  400. * messages directly from the handler.
  401. */
  402. spinlock_t waiting_rcv_msgs_lock;
  403. struct list_head waiting_rcv_msgs;
  404. atomic_t watchdog_pretimeouts_to_deliver;
  405. struct tasklet_struct recv_tasklet;
  406. spinlock_t xmit_msgs_lock;
  407. struct list_head xmit_msgs;
  408. struct ipmi_smi_msg *curr_msg;
  409. struct list_head hp_xmit_msgs;
  410. /*
  411. * The list of command receivers that are registered for commands
  412. * on this interface.
  413. */
  414. struct mutex cmd_rcvrs_mutex;
  415. struct list_head cmd_rcvrs;
  416. /*
  417. * Events that were queues because no one was there to receive
  418. * them.
  419. */
  420. spinlock_t events_lock; /* For dealing with event stuff. */
  421. struct list_head waiting_events;
  422. unsigned int waiting_events_count; /* How many events in queue? */
  423. char delivering_events;
  424. char event_msg_printed;
  425. /* How many users are waiting for events? */
  426. atomic_t event_waiters;
  427. unsigned int ticks_to_req_ev;
  428. spinlock_t watch_lock; /* For dealing with watch stuff below. */
  429. /* How many users are waiting for commands? */
  430. unsigned int command_waiters;
  431. /* How many users are waiting for watchdogs? */
  432. unsigned int watchdog_waiters;
  433. /* How many users are waiting for message responses? */
  434. unsigned int response_waiters;
  435. /*
  436. * Tells what the lower layer has last been asked to watch for,
  437. * messages and/or watchdogs. Protected by watch_lock.
  438. */
  439. unsigned int last_watch_mask;
  440. /*
  441. * The event receiver for my BMC, only really used at panic
  442. * shutdown as a place to store this.
  443. */
  444. unsigned char event_receiver;
  445. unsigned char event_receiver_lun;
  446. unsigned char local_sel_device;
  447. unsigned char local_event_generator;
  448. /* For handling of maintenance mode. */
  449. int maintenance_mode;
  450. bool maintenance_mode_enable;
  451. int auto_maintenance_timeout;
  452. spinlock_t maintenance_mode_lock; /* Used in a timer... */
  453. /*
  454. * If we are doing maintenance on something on IPMB, extend
  455. * the timeout time to avoid timeouts writing firmware and
  456. * such.
  457. */
  458. int ipmb_maintenance_mode_timeout;
  459. /*
  460. * A cheap hack, if this is non-null and a message to an
  461. * interface comes in with a NULL user, call this routine with
  462. * it. Note that the message will still be freed by the
  463. * caller. This only works on the system interface.
  464. *
  465. * Protected by bmc_reg_mutex.
  466. */
  467. void (*null_user_handler)(struct ipmi_smi *intf,
  468. struct ipmi_recv_msg *msg);
  469. /*
  470. * When we are scanning the channels for an SMI, this will
  471. * tell which channel we are scanning.
  472. */
  473. int curr_channel;
  474. /* Channel information */
  475. struct ipmi_channel_set *channel_list;
  476. unsigned int curr_working_cset; /* First index into the following. */
  477. struct ipmi_channel_set wchannels[2];
  478. struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS];
  479. bool channels_ready;
  480. atomic_t stats[IPMI_NUM_STATS];
  481. /*
  482. * run_to_completion duplicate of smb_info, smi_info
  483. * and ipmi_serial_info structures. Used to decrease numbers of
  484. * parameters passed by "low" level IPMI code.
  485. */
  486. int run_to_completion;
  487. };
  488. #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
  489. static void __get_guid(struct ipmi_smi *intf);
  490. static void __ipmi_bmc_unregister(struct ipmi_smi *intf);
  491. static int __ipmi_bmc_register(struct ipmi_smi *intf,
  492. struct ipmi_device_id *id,
  493. bool guid_set, guid_t *guid, int intf_num);
  494. static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id);
  495. /**
  496. * The driver model view of the IPMI messaging driver.
  497. */
  498. static struct platform_driver ipmidriver = {
  499. .driver = {
  500. .name = "ipmi",
  501. .bus = &platform_bus_type
  502. }
  503. };
  504. /*
  505. * This mutex keeps us from adding the same BMC twice.
  506. */
  507. static DEFINE_MUTEX(ipmidriver_mutex);
  508. static LIST_HEAD(ipmi_interfaces);
  509. static DEFINE_MUTEX(ipmi_interfaces_mutex);
  510. #define ipmi_interfaces_mutex_held() \
  511. lockdep_is_held(&ipmi_interfaces_mutex)
  512. static struct srcu_struct ipmi_interfaces_srcu;
  513. /*
  514. * List of watchers that want to know when smi's are added and deleted.
  515. */
  516. static LIST_HEAD(smi_watchers);
  517. static DEFINE_MUTEX(smi_watchers_mutex);
  518. #define ipmi_inc_stat(intf, stat) \
  519. atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
  520. #define ipmi_get_stat(intf, stat) \
  521. ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
  522. static const char * const addr_src_to_str[] = {
  523. "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
  524. "device-tree", "platform"
  525. };
  526. const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
  527. {
  528. if (src >= SI_LAST)
  529. src = 0; /* Invalid */
  530. return addr_src_to_str[src];
  531. }
  532. EXPORT_SYMBOL(ipmi_addr_src_to_str);
  533. static int is_lan_addr(struct ipmi_addr *addr)
  534. {
  535. return addr->addr_type == IPMI_LAN_ADDR_TYPE;
  536. }
  537. static int is_ipmb_addr(struct ipmi_addr *addr)
  538. {
  539. return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
  540. }
  541. static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
  542. {
  543. return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
  544. }
  545. static int is_ipmb_direct_addr(struct ipmi_addr *addr)
  546. {
  547. return addr->addr_type == IPMI_IPMB_DIRECT_ADDR_TYPE;
  548. }
  549. static void free_recv_msg_list(struct list_head *q)
  550. {
  551. struct ipmi_recv_msg *msg, *msg2;
  552. list_for_each_entry_safe(msg, msg2, q, link) {
  553. list_del(&msg->link);
  554. ipmi_free_recv_msg(msg);
  555. }
  556. }
  557. static void free_smi_msg_list(struct list_head *q)
  558. {
  559. struct ipmi_smi_msg *msg, *msg2;
  560. list_for_each_entry_safe(msg, msg2, q, link) {
  561. list_del(&msg->link);
  562. ipmi_free_smi_msg(msg);
  563. }
  564. }
  565. static void clean_up_interface_data(struct ipmi_smi *intf)
  566. {
  567. int i;
  568. struct cmd_rcvr *rcvr, *rcvr2;
  569. struct list_head list;
  570. tasklet_kill(&intf->recv_tasklet);
  571. free_smi_msg_list(&intf->waiting_rcv_msgs);
  572. free_recv_msg_list(&intf->waiting_events);
  573. /*
  574. * Wholesale remove all the entries from the list in the
  575. * interface and wait for RCU to know that none are in use.
  576. */
  577. mutex_lock(&intf->cmd_rcvrs_mutex);
  578. INIT_LIST_HEAD(&list);
  579. list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
  580. mutex_unlock(&intf->cmd_rcvrs_mutex);
  581. list_for_each_entry_safe(rcvr, rcvr2, &list, link)
  582. kfree(rcvr);
  583. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
  584. if ((intf->seq_table[i].inuse)
  585. && (intf->seq_table[i].recv_msg))
  586. ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
  587. }
  588. }
  589. static void intf_free(struct kref *ref)
  590. {
  591. struct ipmi_smi *intf = container_of(ref, struct ipmi_smi, refcount);
  592. clean_up_interface_data(intf);
  593. kfree(intf);
  594. }
  595. int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
  596. {
  597. struct ipmi_smi *intf;
  598. int index, rv;
  599. /*
  600. * Make sure the driver is actually initialized, this handles
  601. * problems with initialization order.
  602. */
  603. rv = ipmi_init_msghandler();
  604. if (rv)
  605. return rv;
  606. mutex_lock(&smi_watchers_mutex);
  607. list_add(&watcher->link, &smi_watchers);
  608. index = srcu_read_lock(&ipmi_interfaces_srcu);
  609. list_for_each_entry_rcu(intf, &ipmi_interfaces, link,
  610. lockdep_is_held(&smi_watchers_mutex)) {
  611. int intf_num = READ_ONCE(intf->intf_num);
  612. if (intf_num == -1)
  613. continue;
  614. watcher->new_smi(intf_num, intf->si_dev);
  615. }
  616. srcu_read_unlock(&ipmi_interfaces_srcu, index);
  617. mutex_unlock(&smi_watchers_mutex);
  618. return 0;
  619. }
  620. EXPORT_SYMBOL(ipmi_smi_watcher_register);
  621. int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
  622. {
  623. mutex_lock(&smi_watchers_mutex);
  624. list_del(&watcher->link);
  625. mutex_unlock(&smi_watchers_mutex);
  626. return 0;
  627. }
  628. EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
  629. /*
  630. * Must be called with smi_watchers_mutex held.
  631. */
  632. static void
  633. call_smi_watchers(int i, struct device *dev)
  634. {
  635. struct ipmi_smi_watcher *w;
  636. mutex_lock(&smi_watchers_mutex);
  637. list_for_each_entry(w, &smi_watchers, link) {
  638. if (try_module_get(w->owner)) {
  639. w->new_smi(i, dev);
  640. module_put(w->owner);
  641. }
  642. }
  643. mutex_unlock(&smi_watchers_mutex);
  644. }
  645. static int
  646. ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
  647. {
  648. if (addr1->addr_type != addr2->addr_type)
  649. return 0;
  650. if (addr1->channel != addr2->channel)
  651. return 0;
  652. if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
  653. struct ipmi_system_interface_addr *smi_addr1
  654. = (struct ipmi_system_interface_addr *) addr1;
  655. struct ipmi_system_interface_addr *smi_addr2
  656. = (struct ipmi_system_interface_addr *) addr2;
  657. return (smi_addr1->lun == smi_addr2->lun);
  658. }
  659. if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
  660. struct ipmi_ipmb_addr *ipmb_addr1
  661. = (struct ipmi_ipmb_addr *) addr1;
  662. struct ipmi_ipmb_addr *ipmb_addr2
  663. = (struct ipmi_ipmb_addr *) addr2;
  664. return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
  665. && (ipmb_addr1->lun == ipmb_addr2->lun));
  666. }
  667. if (is_ipmb_direct_addr(addr1)) {
  668. struct ipmi_ipmb_direct_addr *daddr1
  669. = (struct ipmi_ipmb_direct_addr *) addr1;
  670. struct ipmi_ipmb_direct_addr *daddr2
  671. = (struct ipmi_ipmb_direct_addr *) addr2;
  672. return daddr1->slave_addr == daddr2->slave_addr &&
  673. daddr1->rq_lun == daddr2->rq_lun &&
  674. daddr1->rs_lun == daddr2->rs_lun;
  675. }
  676. if (is_lan_addr(addr1)) {
  677. struct ipmi_lan_addr *lan_addr1
  678. = (struct ipmi_lan_addr *) addr1;
  679. struct ipmi_lan_addr *lan_addr2
  680. = (struct ipmi_lan_addr *) addr2;
  681. return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
  682. && (lan_addr1->local_SWID == lan_addr2->local_SWID)
  683. && (lan_addr1->session_handle
  684. == lan_addr2->session_handle)
  685. && (lan_addr1->lun == lan_addr2->lun));
  686. }
  687. return 1;
  688. }
  689. int ipmi_validate_addr(struct ipmi_addr *addr, int len)
  690. {
  691. if (len < sizeof(struct ipmi_system_interface_addr))
  692. return -EINVAL;
  693. if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
  694. if (addr->channel != IPMI_BMC_CHANNEL)
  695. return -EINVAL;
  696. return 0;
  697. }
  698. if ((addr->channel == IPMI_BMC_CHANNEL)
  699. || (addr->channel >= IPMI_MAX_CHANNELS)
  700. || (addr->channel < 0))
  701. return -EINVAL;
  702. if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
  703. if (len < sizeof(struct ipmi_ipmb_addr))
  704. return -EINVAL;
  705. return 0;
  706. }
  707. if (is_ipmb_direct_addr(addr)) {
  708. struct ipmi_ipmb_direct_addr *daddr = (void *) addr;
  709. if (addr->channel != 0)
  710. return -EINVAL;
  711. if (len < sizeof(struct ipmi_ipmb_direct_addr))
  712. return -EINVAL;
  713. if (daddr->slave_addr & 0x01)
  714. return -EINVAL;
  715. if (daddr->rq_lun >= 4)
  716. return -EINVAL;
  717. if (daddr->rs_lun >= 4)
  718. return -EINVAL;
  719. return 0;
  720. }
  721. if (is_lan_addr(addr)) {
  722. if (len < sizeof(struct ipmi_lan_addr))
  723. return -EINVAL;
  724. return 0;
  725. }
  726. return -EINVAL;
  727. }
  728. EXPORT_SYMBOL(ipmi_validate_addr);
  729. unsigned int ipmi_addr_length(int addr_type)
  730. {
  731. if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  732. return sizeof(struct ipmi_system_interface_addr);
  733. if ((addr_type == IPMI_IPMB_ADDR_TYPE)
  734. || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
  735. return sizeof(struct ipmi_ipmb_addr);
  736. if (addr_type == IPMI_IPMB_DIRECT_ADDR_TYPE)
  737. return sizeof(struct ipmi_ipmb_direct_addr);
  738. if (addr_type == IPMI_LAN_ADDR_TYPE)
  739. return sizeof(struct ipmi_lan_addr);
  740. return 0;
  741. }
  742. EXPORT_SYMBOL(ipmi_addr_length);
  743. static int deliver_response(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
  744. {
  745. int rv = 0;
  746. if (!msg->user) {
  747. /* Special handling for NULL users. */
  748. if (intf->null_user_handler) {
  749. intf->null_user_handler(intf, msg);
  750. } else {
  751. /* No handler, so give up. */
  752. rv = -EINVAL;
  753. }
  754. ipmi_free_recv_msg(msg);
  755. } else if (oops_in_progress) {
  756. /*
  757. * If we are running in the panic context, calling the
  758. * receive handler doesn't much meaning and has a deadlock
  759. * risk. At this moment, simply skip it in that case.
  760. */
  761. ipmi_free_recv_msg(msg);
  762. atomic_dec(&msg->user->nr_msgs);
  763. } else {
  764. int index;
  765. struct ipmi_user *user = acquire_ipmi_user(msg->user, &index);
  766. if (user) {
  767. atomic_dec(&user->nr_msgs);
  768. user->handler->ipmi_recv_hndl(msg, user->handler_data);
  769. release_ipmi_user(user, index);
  770. } else {
  771. /* User went away, give up. */
  772. ipmi_free_recv_msg(msg);
  773. rv = -EINVAL;
  774. }
  775. }
  776. return rv;
  777. }
  778. static void deliver_local_response(struct ipmi_smi *intf,
  779. struct ipmi_recv_msg *msg)
  780. {
  781. if (deliver_response(intf, msg))
  782. ipmi_inc_stat(intf, unhandled_local_responses);
  783. else
  784. ipmi_inc_stat(intf, handled_local_responses);
  785. }
  786. static void deliver_err_response(struct ipmi_smi *intf,
  787. struct ipmi_recv_msg *msg, int err)
  788. {
  789. msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  790. msg->msg_data[0] = err;
  791. msg->msg.netfn |= 1; /* Convert to a response. */
  792. msg->msg.data_len = 1;
  793. msg->msg.data = msg->msg_data;
  794. deliver_local_response(intf, msg);
  795. }
  796. static void smi_add_watch(struct ipmi_smi *intf, unsigned int flags)
  797. {
  798. unsigned long iflags;
  799. if (!intf->handlers->set_need_watch)
  800. return;
  801. spin_lock_irqsave(&intf->watch_lock, iflags);
  802. if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
  803. intf->response_waiters++;
  804. if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
  805. intf->watchdog_waiters++;
  806. if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
  807. intf->command_waiters++;
  808. if ((intf->last_watch_mask & flags) != flags) {
  809. intf->last_watch_mask |= flags;
  810. intf->handlers->set_need_watch(intf->send_info,
  811. intf->last_watch_mask);
  812. }
  813. spin_unlock_irqrestore(&intf->watch_lock, iflags);
  814. }
  815. static void smi_remove_watch(struct ipmi_smi *intf, unsigned int flags)
  816. {
  817. unsigned long iflags;
  818. if (!intf->handlers->set_need_watch)
  819. return;
  820. spin_lock_irqsave(&intf->watch_lock, iflags);
  821. if (flags & IPMI_WATCH_MASK_CHECK_MESSAGES)
  822. intf->response_waiters--;
  823. if (flags & IPMI_WATCH_MASK_CHECK_WATCHDOG)
  824. intf->watchdog_waiters--;
  825. if (flags & IPMI_WATCH_MASK_CHECK_COMMANDS)
  826. intf->command_waiters--;
  827. flags = 0;
  828. if (intf->response_waiters)
  829. flags |= IPMI_WATCH_MASK_CHECK_MESSAGES;
  830. if (intf->watchdog_waiters)
  831. flags |= IPMI_WATCH_MASK_CHECK_WATCHDOG;
  832. if (intf->command_waiters)
  833. flags |= IPMI_WATCH_MASK_CHECK_COMMANDS;
  834. if (intf->last_watch_mask != flags) {
  835. intf->last_watch_mask = flags;
  836. intf->handlers->set_need_watch(intf->send_info,
  837. intf->last_watch_mask);
  838. }
  839. spin_unlock_irqrestore(&intf->watch_lock, iflags);
  840. }
  841. /*
  842. * Find the next sequence number not being used and add the given
  843. * message with the given timeout to the sequence table. This must be
  844. * called with the interface's seq_lock held.
  845. */
  846. static int intf_next_seq(struct ipmi_smi *intf,
  847. struct ipmi_recv_msg *recv_msg,
  848. unsigned long timeout,
  849. int retries,
  850. int broadcast,
  851. unsigned char *seq,
  852. long *seqid)
  853. {
  854. int rv = 0;
  855. unsigned int i;
  856. if (timeout == 0)
  857. timeout = default_retry_ms;
  858. if (retries < 0)
  859. retries = default_max_retries;
  860. for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
  861. i = (i+1)%IPMI_IPMB_NUM_SEQ) {
  862. if (!intf->seq_table[i].inuse)
  863. break;
  864. }
  865. if (!intf->seq_table[i].inuse) {
  866. intf->seq_table[i].recv_msg = recv_msg;
  867. /*
  868. * Start with the maximum timeout, when the send response
  869. * comes in we will start the real timer.
  870. */
  871. intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
  872. intf->seq_table[i].orig_timeout = timeout;
  873. intf->seq_table[i].retries_left = retries;
  874. intf->seq_table[i].broadcast = broadcast;
  875. intf->seq_table[i].inuse = 1;
  876. intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
  877. *seq = i;
  878. *seqid = intf->seq_table[i].seqid;
  879. intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
  880. smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
  881. need_waiter(intf);
  882. } else {
  883. rv = -EAGAIN;
  884. }
  885. return rv;
  886. }
  887. /*
  888. * Return the receive message for the given sequence number and
  889. * release the sequence number so it can be reused. Some other data
  890. * is passed in to be sure the message matches up correctly (to help
  891. * guard against message coming in after their timeout and the
  892. * sequence number being reused).
  893. */
  894. static int intf_find_seq(struct ipmi_smi *intf,
  895. unsigned char seq,
  896. short channel,
  897. unsigned char cmd,
  898. unsigned char netfn,
  899. struct ipmi_addr *addr,
  900. struct ipmi_recv_msg **recv_msg)
  901. {
  902. int rv = -ENODEV;
  903. unsigned long flags;
  904. if (seq >= IPMI_IPMB_NUM_SEQ)
  905. return -EINVAL;
  906. spin_lock_irqsave(&intf->seq_lock, flags);
  907. if (intf->seq_table[seq].inuse) {
  908. struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
  909. if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
  910. && (msg->msg.netfn == netfn)
  911. && (ipmi_addr_equal(addr, &msg->addr))) {
  912. *recv_msg = msg;
  913. intf->seq_table[seq].inuse = 0;
  914. smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
  915. rv = 0;
  916. }
  917. }
  918. spin_unlock_irqrestore(&intf->seq_lock, flags);
  919. return rv;
  920. }
  921. /* Start the timer for a specific sequence table entry. */
  922. static int intf_start_seq_timer(struct ipmi_smi *intf,
  923. long msgid)
  924. {
  925. int rv = -ENODEV;
  926. unsigned long flags;
  927. unsigned char seq;
  928. unsigned long seqid;
  929. GET_SEQ_FROM_MSGID(msgid, seq, seqid);
  930. spin_lock_irqsave(&intf->seq_lock, flags);
  931. /*
  932. * We do this verification because the user can be deleted
  933. * while a message is outstanding.
  934. */
  935. if ((intf->seq_table[seq].inuse)
  936. && (intf->seq_table[seq].seqid == seqid)) {
  937. struct seq_table *ent = &intf->seq_table[seq];
  938. ent->timeout = ent->orig_timeout;
  939. rv = 0;
  940. }
  941. spin_unlock_irqrestore(&intf->seq_lock, flags);
  942. return rv;
  943. }
  944. /* Got an error for the send message for a specific sequence number. */
  945. static int intf_err_seq(struct ipmi_smi *intf,
  946. long msgid,
  947. unsigned int err)
  948. {
  949. int rv = -ENODEV;
  950. unsigned long flags;
  951. unsigned char seq;
  952. unsigned long seqid;
  953. struct ipmi_recv_msg *msg = NULL;
  954. GET_SEQ_FROM_MSGID(msgid, seq, seqid);
  955. spin_lock_irqsave(&intf->seq_lock, flags);
  956. /*
  957. * We do this verification because the user can be deleted
  958. * while a message is outstanding.
  959. */
  960. if ((intf->seq_table[seq].inuse)
  961. && (intf->seq_table[seq].seqid == seqid)) {
  962. struct seq_table *ent = &intf->seq_table[seq];
  963. ent->inuse = 0;
  964. smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
  965. msg = ent->recv_msg;
  966. rv = 0;
  967. }
  968. spin_unlock_irqrestore(&intf->seq_lock, flags);
  969. if (msg)
  970. deliver_err_response(intf, msg, err);
  971. return rv;
  972. }
  973. static void free_user_work(struct work_struct *work)
  974. {
  975. struct ipmi_user *user = container_of(work, struct ipmi_user,
  976. remove_work);
  977. cleanup_srcu_struct(&user->release_barrier);
  978. vfree(user);
  979. }
  980. int ipmi_create_user(unsigned int if_num,
  981. const struct ipmi_user_hndl *handler,
  982. void *handler_data,
  983. struct ipmi_user **user)
  984. {
  985. unsigned long flags;
  986. struct ipmi_user *new_user;
  987. int rv, index;
  988. struct ipmi_smi *intf;
  989. /*
  990. * There is no module usecount here, because it's not
  991. * required. Since this can only be used by and called from
  992. * other modules, they will implicitly use this module, and
  993. * thus this can't be removed unless the other modules are
  994. * removed.
  995. */
  996. if (handler == NULL)
  997. return -EINVAL;
  998. /*
  999. * Make sure the driver is actually initialized, this handles
  1000. * problems with initialization order.
  1001. */
  1002. rv = ipmi_init_msghandler();
  1003. if (rv)
  1004. return rv;
  1005. new_user = vzalloc(sizeof(*new_user));
  1006. if (!new_user)
  1007. return -ENOMEM;
  1008. index = srcu_read_lock(&ipmi_interfaces_srcu);
  1009. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  1010. if (intf->intf_num == if_num)
  1011. goto found;
  1012. }
  1013. /* Not found, return an error */
  1014. rv = -EINVAL;
  1015. goto out_kfree;
  1016. found:
  1017. if (atomic_add_return(1, &intf->nr_users) > max_users) {
  1018. rv = -EBUSY;
  1019. goto out_kfree;
  1020. }
  1021. INIT_WORK(&new_user->remove_work, free_user_work);
  1022. rv = init_srcu_struct(&new_user->release_barrier);
  1023. if (rv)
  1024. goto out_kfree;
  1025. if (!try_module_get(intf->owner)) {
  1026. rv = -ENODEV;
  1027. goto out_kfree;
  1028. }
  1029. /* Note that each existing user holds a refcount to the interface. */
  1030. kref_get(&intf->refcount);
  1031. atomic_set(&new_user->nr_msgs, 0);
  1032. kref_init(&new_user->refcount);
  1033. new_user->handler = handler;
  1034. new_user->handler_data = handler_data;
  1035. new_user->intf = intf;
  1036. new_user->gets_events = false;
  1037. rcu_assign_pointer(new_user->self, new_user);
  1038. spin_lock_irqsave(&intf->seq_lock, flags);
  1039. list_add_rcu(&new_user->link, &intf->users);
  1040. spin_unlock_irqrestore(&intf->seq_lock, flags);
  1041. if (handler->ipmi_watchdog_pretimeout)
  1042. /* User wants pretimeouts, so make sure to watch for them. */
  1043. smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
  1044. srcu_read_unlock(&ipmi_interfaces_srcu, index);
  1045. *user = new_user;
  1046. return 0;
  1047. out_kfree:
  1048. atomic_dec(&intf->nr_users);
  1049. srcu_read_unlock(&ipmi_interfaces_srcu, index);
  1050. vfree(new_user);
  1051. return rv;
  1052. }
  1053. EXPORT_SYMBOL(ipmi_create_user);
  1054. int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
  1055. {
  1056. int rv, index;
  1057. struct ipmi_smi *intf;
  1058. index = srcu_read_lock(&ipmi_interfaces_srcu);
  1059. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  1060. if (intf->intf_num == if_num)
  1061. goto found;
  1062. }
  1063. srcu_read_unlock(&ipmi_interfaces_srcu, index);
  1064. /* Not found, return an error */
  1065. return -EINVAL;
  1066. found:
  1067. if (!intf->handlers->get_smi_info)
  1068. rv = -ENOTTY;
  1069. else
  1070. rv = intf->handlers->get_smi_info(intf->send_info, data);
  1071. srcu_read_unlock(&ipmi_interfaces_srcu, index);
  1072. return rv;
  1073. }
  1074. EXPORT_SYMBOL(ipmi_get_smi_info);
  1075. static void free_user(struct kref *ref)
  1076. {
  1077. struct ipmi_user *user = container_of(ref, struct ipmi_user, refcount);
  1078. /* SRCU cleanup must happen in task context. */
  1079. queue_work(remove_work_wq, &user->remove_work);
  1080. }
  1081. static void _ipmi_destroy_user(struct ipmi_user *user)
  1082. {
  1083. struct ipmi_smi *intf = user->intf;
  1084. int i;
  1085. unsigned long flags;
  1086. struct cmd_rcvr *rcvr;
  1087. struct cmd_rcvr *rcvrs = NULL;
  1088. struct module *owner;
  1089. if (!acquire_ipmi_user(user, &i)) {
  1090. /*
  1091. * The user has already been cleaned up, just make sure
  1092. * nothing is using it and return.
  1093. */
  1094. synchronize_srcu(&user->release_barrier);
  1095. return;
  1096. }
  1097. rcu_assign_pointer(user->self, NULL);
  1098. release_ipmi_user(user, i);
  1099. synchronize_srcu(&user->release_barrier);
  1100. if (user->handler->shutdown)
  1101. user->handler->shutdown(user->handler_data);
  1102. if (user->handler->ipmi_watchdog_pretimeout)
  1103. smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_WATCHDOG);
  1104. if (user->gets_events)
  1105. atomic_dec(&intf->event_waiters);
  1106. /* Remove the user from the interface's sequence table. */
  1107. spin_lock_irqsave(&intf->seq_lock, flags);
  1108. list_del_rcu(&user->link);
  1109. atomic_dec(&intf->nr_users);
  1110. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
  1111. if (intf->seq_table[i].inuse
  1112. && (intf->seq_table[i].recv_msg->user == user)) {
  1113. intf->seq_table[i].inuse = 0;
  1114. smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
  1115. ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
  1116. }
  1117. }
  1118. spin_unlock_irqrestore(&intf->seq_lock, flags);
  1119. /*
  1120. * Remove the user from the command receiver's table. First
  1121. * we build a list of everything (not using the standard link,
  1122. * since other things may be using it till we do
  1123. * synchronize_srcu()) then free everything in that list.
  1124. */
  1125. mutex_lock(&intf->cmd_rcvrs_mutex);
  1126. list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
  1127. lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
  1128. if (rcvr->user == user) {
  1129. list_del_rcu(&rcvr->link);
  1130. rcvr->next = rcvrs;
  1131. rcvrs = rcvr;
  1132. }
  1133. }
  1134. mutex_unlock(&intf->cmd_rcvrs_mutex);
  1135. synchronize_rcu();
  1136. while (rcvrs) {
  1137. rcvr = rcvrs;
  1138. rcvrs = rcvr->next;
  1139. kfree(rcvr);
  1140. }
  1141. owner = intf->owner;
  1142. kref_put(&intf->refcount, intf_free);
  1143. module_put(owner);
  1144. }
  1145. int ipmi_destroy_user(struct ipmi_user *user)
  1146. {
  1147. _ipmi_destroy_user(user);
  1148. kref_put(&user->refcount, free_user);
  1149. return 0;
  1150. }
  1151. EXPORT_SYMBOL(ipmi_destroy_user);
  1152. int ipmi_get_version(struct ipmi_user *user,
  1153. unsigned char *major,
  1154. unsigned char *minor)
  1155. {
  1156. struct ipmi_device_id id;
  1157. int rv, index;
  1158. user = acquire_ipmi_user(user, &index);
  1159. if (!user)
  1160. return -ENODEV;
  1161. rv = bmc_get_device_id(user->intf, NULL, &id, NULL, NULL);
  1162. if (!rv) {
  1163. *major = ipmi_version_major(&id);
  1164. *minor = ipmi_version_minor(&id);
  1165. }
  1166. release_ipmi_user(user, index);
  1167. return rv;
  1168. }
  1169. EXPORT_SYMBOL(ipmi_get_version);
  1170. int ipmi_set_my_address(struct ipmi_user *user,
  1171. unsigned int channel,
  1172. unsigned char address)
  1173. {
  1174. int index, rv = 0;
  1175. user = acquire_ipmi_user(user, &index);
  1176. if (!user)
  1177. return -ENODEV;
  1178. if (channel >= IPMI_MAX_CHANNELS) {
  1179. rv = -EINVAL;
  1180. } else {
  1181. channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
  1182. user->intf->addrinfo[channel].address = address;
  1183. }
  1184. release_ipmi_user(user, index);
  1185. return rv;
  1186. }
  1187. EXPORT_SYMBOL(ipmi_set_my_address);
  1188. int ipmi_get_my_address(struct ipmi_user *user,
  1189. unsigned int channel,
  1190. unsigned char *address)
  1191. {
  1192. int index, rv = 0;
  1193. user = acquire_ipmi_user(user, &index);
  1194. if (!user)
  1195. return -ENODEV;
  1196. if (channel >= IPMI_MAX_CHANNELS) {
  1197. rv = -EINVAL;
  1198. } else {
  1199. channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
  1200. *address = user->intf->addrinfo[channel].address;
  1201. }
  1202. release_ipmi_user(user, index);
  1203. return rv;
  1204. }
  1205. EXPORT_SYMBOL(ipmi_get_my_address);
  1206. int ipmi_set_my_LUN(struct ipmi_user *user,
  1207. unsigned int channel,
  1208. unsigned char LUN)
  1209. {
  1210. int index, rv = 0;
  1211. user = acquire_ipmi_user(user, &index);
  1212. if (!user)
  1213. return -ENODEV;
  1214. if (channel >= IPMI_MAX_CHANNELS) {
  1215. rv = -EINVAL;
  1216. } else {
  1217. channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
  1218. user->intf->addrinfo[channel].lun = LUN & 0x3;
  1219. }
  1220. release_ipmi_user(user, index);
  1221. return rv;
  1222. }
  1223. EXPORT_SYMBOL(ipmi_set_my_LUN);
  1224. int ipmi_get_my_LUN(struct ipmi_user *user,
  1225. unsigned int channel,
  1226. unsigned char *address)
  1227. {
  1228. int index, rv = 0;
  1229. user = acquire_ipmi_user(user, &index);
  1230. if (!user)
  1231. return -ENODEV;
  1232. if (channel >= IPMI_MAX_CHANNELS) {
  1233. rv = -EINVAL;
  1234. } else {
  1235. channel = array_index_nospec(channel, IPMI_MAX_CHANNELS);
  1236. *address = user->intf->addrinfo[channel].lun;
  1237. }
  1238. release_ipmi_user(user, index);
  1239. return rv;
  1240. }
  1241. EXPORT_SYMBOL(ipmi_get_my_LUN);
  1242. int ipmi_get_maintenance_mode(struct ipmi_user *user)
  1243. {
  1244. int mode, index;
  1245. unsigned long flags;
  1246. user = acquire_ipmi_user(user, &index);
  1247. if (!user)
  1248. return -ENODEV;
  1249. spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
  1250. mode = user->intf->maintenance_mode;
  1251. spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
  1252. release_ipmi_user(user, index);
  1253. return mode;
  1254. }
  1255. EXPORT_SYMBOL(ipmi_get_maintenance_mode);
  1256. static void maintenance_mode_update(struct ipmi_smi *intf)
  1257. {
  1258. if (intf->handlers->set_maintenance_mode)
  1259. intf->handlers->set_maintenance_mode(
  1260. intf->send_info, intf->maintenance_mode_enable);
  1261. }
  1262. int ipmi_set_maintenance_mode(struct ipmi_user *user, int mode)
  1263. {
  1264. int rv = 0, index;
  1265. unsigned long flags;
  1266. struct ipmi_smi *intf = user->intf;
  1267. user = acquire_ipmi_user(user, &index);
  1268. if (!user)
  1269. return -ENODEV;
  1270. spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
  1271. if (intf->maintenance_mode != mode) {
  1272. switch (mode) {
  1273. case IPMI_MAINTENANCE_MODE_AUTO:
  1274. intf->maintenance_mode_enable
  1275. = (intf->auto_maintenance_timeout > 0);
  1276. break;
  1277. case IPMI_MAINTENANCE_MODE_OFF:
  1278. intf->maintenance_mode_enable = false;
  1279. break;
  1280. case IPMI_MAINTENANCE_MODE_ON:
  1281. intf->maintenance_mode_enable = true;
  1282. break;
  1283. default:
  1284. rv = -EINVAL;
  1285. goto out_unlock;
  1286. }
  1287. intf->maintenance_mode = mode;
  1288. maintenance_mode_update(intf);
  1289. }
  1290. out_unlock:
  1291. spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
  1292. release_ipmi_user(user, index);
  1293. return rv;
  1294. }
  1295. EXPORT_SYMBOL(ipmi_set_maintenance_mode);
  1296. int ipmi_set_gets_events(struct ipmi_user *user, bool val)
  1297. {
  1298. unsigned long flags;
  1299. struct ipmi_smi *intf = user->intf;
  1300. struct ipmi_recv_msg *msg, *msg2;
  1301. struct list_head msgs;
  1302. int index;
  1303. user = acquire_ipmi_user(user, &index);
  1304. if (!user)
  1305. return -ENODEV;
  1306. INIT_LIST_HEAD(&msgs);
  1307. spin_lock_irqsave(&intf->events_lock, flags);
  1308. if (user->gets_events == val)
  1309. goto out;
  1310. user->gets_events = val;
  1311. if (val) {
  1312. if (atomic_inc_return(&intf->event_waiters) == 1)
  1313. need_waiter(intf);
  1314. } else {
  1315. atomic_dec(&intf->event_waiters);
  1316. }
  1317. if (intf->delivering_events)
  1318. /*
  1319. * Another thread is delivering events for this, so
  1320. * let it handle any new events.
  1321. */
  1322. goto out;
  1323. /* Deliver any queued events. */
  1324. while (user->gets_events && !list_empty(&intf->waiting_events)) {
  1325. list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
  1326. list_move_tail(&msg->link, &msgs);
  1327. intf->waiting_events_count = 0;
  1328. if (intf->event_msg_printed) {
  1329. dev_warn(intf->si_dev, "Event queue no longer full\n");
  1330. intf->event_msg_printed = 0;
  1331. }
  1332. intf->delivering_events = 1;
  1333. spin_unlock_irqrestore(&intf->events_lock, flags);
  1334. list_for_each_entry_safe(msg, msg2, &msgs, link) {
  1335. msg->user = user;
  1336. kref_get(&user->refcount);
  1337. deliver_local_response(intf, msg);
  1338. }
  1339. spin_lock_irqsave(&intf->events_lock, flags);
  1340. intf->delivering_events = 0;
  1341. }
  1342. out:
  1343. spin_unlock_irqrestore(&intf->events_lock, flags);
  1344. release_ipmi_user(user, index);
  1345. return 0;
  1346. }
  1347. EXPORT_SYMBOL(ipmi_set_gets_events);
  1348. static struct cmd_rcvr *find_cmd_rcvr(struct ipmi_smi *intf,
  1349. unsigned char netfn,
  1350. unsigned char cmd,
  1351. unsigned char chan)
  1352. {
  1353. struct cmd_rcvr *rcvr;
  1354. list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
  1355. lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
  1356. if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
  1357. && (rcvr->chans & (1 << chan)))
  1358. return rcvr;
  1359. }
  1360. return NULL;
  1361. }
  1362. static int is_cmd_rcvr_exclusive(struct ipmi_smi *intf,
  1363. unsigned char netfn,
  1364. unsigned char cmd,
  1365. unsigned int chans)
  1366. {
  1367. struct cmd_rcvr *rcvr;
  1368. list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link,
  1369. lockdep_is_held(&intf->cmd_rcvrs_mutex)) {
  1370. if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
  1371. && (rcvr->chans & chans))
  1372. return 0;
  1373. }
  1374. return 1;
  1375. }
  1376. int ipmi_register_for_cmd(struct ipmi_user *user,
  1377. unsigned char netfn,
  1378. unsigned char cmd,
  1379. unsigned int chans)
  1380. {
  1381. struct ipmi_smi *intf = user->intf;
  1382. struct cmd_rcvr *rcvr;
  1383. int rv = 0, index;
  1384. user = acquire_ipmi_user(user, &index);
  1385. if (!user)
  1386. return -ENODEV;
  1387. rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
  1388. if (!rcvr) {
  1389. rv = -ENOMEM;
  1390. goto out_release;
  1391. }
  1392. rcvr->cmd = cmd;
  1393. rcvr->netfn = netfn;
  1394. rcvr->chans = chans;
  1395. rcvr->user = user;
  1396. mutex_lock(&intf->cmd_rcvrs_mutex);
  1397. /* Make sure the command/netfn is not already registered. */
  1398. if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
  1399. rv = -EBUSY;
  1400. goto out_unlock;
  1401. }
  1402. smi_add_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
  1403. list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
  1404. out_unlock:
  1405. mutex_unlock(&intf->cmd_rcvrs_mutex);
  1406. if (rv)
  1407. kfree(rcvr);
  1408. out_release:
  1409. release_ipmi_user(user, index);
  1410. return rv;
  1411. }
  1412. EXPORT_SYMBOL(ipmi_register_for_cmd);
  1413. int ipmi_unregister_for_cmd(struct ipmi_user *user,
  1414. unsigned char netfn,
  1415. unsigned char cmd,
  1416. unsigned int chans)
  1417. {
  1418. struct ipmi_smi *intf = user->intf;
  1419. struct cmd_rcvr *rcvr;
  1420. struct cmd_rcvr *rcvrs = NULL;
  1421. int i, rv = -ENOENT, index;
  1422. user = acquire_ipmi_user(user, &index);
  1423. if (!user)
  1424. return -ENODEV;
  1425. mutex_lock(&intf->cmd_rcvrs_mutex);
  1426. for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
  1427. if (((1 << i) & chans) == 0)
  1428. continue;
  1429. rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
  1430. if (rcvr == NULL)
  1431. continue;
  1432. if (rcvr->user == user) {
  1433. rv = 0;
  1434. rcvr->chans &= ~chans;
  1435. if (rcvr->chans == 0) {
  1436. list_del_rcu(&rcvr->link);
  1437. rcvr->next = rcvrs;
  1438. rcvrs = rcvr;
  1439. }
  1440. }
  1441. }
  1442. mutex_unlock(&intf->cmd_rcvrs_mutex);
  1443. synchronize_rcu();
  1444. release_ipmi_user(user, index);
  1445. while (rcvrs) {
  1446. smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_COMMANDS);
  1447. rcvr = rcvrs;
  1448. rcvrs = rcvr->next;
  1449. kfree(rcvr);
  1450. }
  1451. return rv;
  1452. }
  1453. EXPORT_SYMBOL(ipmi_unregister_for_cmd);
  1454. unsigned char
  1455. ipmb_checksum(unsigned char *data, int size)
  1456. {
  1457. unsigned char csum = 0;
  1458. for (; size > 0; size--, data++)
  1459. csum += *data;
  1460. return -csum;
  1461. }
  1462. EXPORT_SYMBOL(ipmb_checksum);
  1463. static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
  1464. struct kernel_ipmi_msg *msg,
  1465. struct ipmi_ipmb_addr *ipmb_addr,
  1466. long msgid,
  1467. unsigned char ipmb_seq,
  1468. int broadcast,
  1469. unsigned char source_address,
  1470. unsigned char source_lun)
  1471. {
  1472. int i = broadcast;
  1473. /* Format the IPMB header data. */
  1474. smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  1475. smi_msg->data[1] = IPMI_SEND_MSG_CMD;
  1476. smi_msg->data[2] = ipmb_addr->channel;
  1477. if (broadcast)
  1478. smi_msg->data[3] = 0;
  1479. smi_msg->data[i+3] = ipmb_addr->slave_addr;
  1480. smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
  1481. smi_msg->data[i+5] = ipmb_checksum(&smi_msg->data[i + 3], 2);
  1482. smi_msg->data[i+6] = source_address;
  1483. smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
  1484. smi_msg->data[i+8] = msg->cmd;
  1485. /* Now tack on the data to the message. */
  1486. if (msg->data_len > 0)
  1487. memcpy(&smi_msg->data[i + 9], msg->data, msg->data_len);
  1488. smi_msg->data_size = msg->data_len + 9;
  1489. /* Now calculate the checksum and tack it on. */
  1490. smi_msg->data[i+smi_msg->data_size]
  1491. = ipmb_checksum(&smi_msg->data[i + 6], smi_msg->data_size - 6);
  1492. /*
  1493. * Add on the checksum size and the offset from the
  1494. * broadcast.
  1495. */
  1496. smi_msg->data_size += 1 + i;
  1497. smi_msg->msgid = msgid;
  1498. }
  1499. static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
  1500. struct kernel_ipmi_msg *msg,
  1501. struct ipmi_lan_addr *lan_addr,
  1502. long msgid,
  1503. unsigned char ipmb_seq,
  1504. unsigned char source_lun)
  1505. {
  1506. /* Format the IPMB header data. */
  1507. smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  1508. smi_msg->data[1] = IPMI_SEND_MSG_CMD;
  1509. smi_msg->data[2] = lan_addr->channel;
  1510. smi_msg->data[3] = lan_addr->session_handle;
  1511. smi_msg->data[4] = lan_addr->remote_SWID;
  1512. smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
  1513. smi_msg->data[6] = ipmb_checksum(&smi_msg->data[4], 2);
  1514. smi_msg->data[7] = lan_addr->local_SWID;
  1515. smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
  1516. smi_msg->data[9] = msg->cmd;
  1517. /* Now tack on the data to the message. */
  1518. if (msg->data_len > 0)
  1519. memcpy(&smi_msg->data[10], msg->data, msg->data_len);
  1520. smi_msg->data_size = msg->data_len + 10;
  1521. /* Now calculate the checksum and tack it on. */
  1522. smi_msg->data[smi_msg->data_size]
  1523. = ipmb_checksum(&smi_msg->data[7], smi_msg->data_size - 7);
  1524. /*
  1525. * Add on the checksum size and the offset from the
  1526. * broadcast.
  1527. */
  1528. smi_msg->data_size += 1;
  1529. smi_msg->msgid = msgid;
  1530. }
  1531. static struct ipmi_smi_msg *smi_add_send_msg(struct ipmi_smi *intf,
  1532. struct ipmi_smi_msg *smi_msg,
  1533. int priority)
  1534. {
  1535. if (intf->curr_msg) {
  1536. if (priority > 0)
  1537. list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
  1538. else
  1539. list_add_tail(&smi_msg->link, &intf->xmit_msgs);
  1540. smi_msg = NULL;
  1541. } else {
  1542. intf->curr_msg = smi_msg;
  1543. }
  1544. return smi_msg;
  1545. }
  1546. static void smi_send(struct ipmi_smi *intf,
  1547. const struct ipmi_smi_handlers *handlers,
  1548. struct ipmi_smi_msg *smi_msg, int priority)
  1549. {
  1550. int run_to_completion = intf->run_to_completion;
  1551. unsigned long flags = 0;
  1552. if (!run_to_completion)
  1553. spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
  1554. smi_msg = smi_add_send_msg(intf, smi_msg, priority);
  1555. if (!run_to_completion)
  1556. spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
  1557. if (smi_msg)
  1558. handlers->sender(intf->send_info, smi_msg);
  1559. }
  1560. static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg *msg)
  1561. {
  1562. return (((msg->netfn == IPMI_NETFN_APP_REQUEST)
  1563. && ((msg->cmd == IPMI_COLD_RESET_CMD)
  1564. || (msg->cmd == IPMI_WARM_RESET_CMD)))
  1565. || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST));
  1566. }
  1567. static int i_ipmi_req_sysintf(struct ipmi_smi *intf,
  1568. struct ipmi_addr *addr,
  1569. long msgid,
  1570. struct kernel_ipmi_msg *msg,
  1571. struct ipmi_smi_msg *smi_msg,
  1572. struct ipmi_recv_msg *recv_msg,
  1573. int retries,
  1574. unsigned int retry_time_ms)
  1575. {
  1576. struct ipmi_system_interface_addr *smi_addr;
  1577. if (msg->netfn & 1)
  1578. /* Responses are not allowed to the SMI. */
  1579. return -EINVAL;
  1580. smi_addr = (struct ipmi_system_interface_addr *) addr;
  1581. if (smi_addr->lun > 3) {
  1582. ipmi_inc_stat(intf, sent_invalid_commands);
  1583. return -EINVAL;
  1584. }
  1585. memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
  1586. if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
  1587. && ((msg->cmd == IPMI_SEND_MSG_CMD)
  1588. || (msg->cmd == IPMI_GET_MSG_CMD)
  1589. || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
  1590. /*
  1591. * We don't let the user do these, since we manage
  1592. * the sequence numbers.
  1593. */
  1594. ipmi_inc_stat(intf, sent_invalid_commands);
  1595. return -EINVAL;
  1596. }
  1597. if (is_maintenance_mode_cmd(msg)) {
  1598. unsigned long flags;
  1599. spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
  1600. intf->auto_maintenance_timeout
  1601. = maintenance_mode_timeout_ms;
  1602. if (!intf->maintenance_mode
  1603. && !intf->maintenance_mode_enable) {
  1604. intf->maintenance_mode_enable = true;
  1605. maintenance_mode_update(intf);
  1606. }
  1607. spin_unlock_irqrestore(&intf->maintenance_mode_lock,
  1608. flags);
  1609. }
  1610. if (msg->data_len + 2 > IPMI_MAX_MSG_LENGTH) {
  1611. ipmi_inc_stat(intf, sent_invalid_commands);
  1612. return -EMSGSIZE;
  1613. }
  1614. smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
  1615. smi_msg->data[1] = msg->cmd;
  1616. smi_msg->msgid = msgid;
  1617. smi_msg->user_data = recv_msg;
  1618. if (msg->data_len > 0)
  1619. memcpy(&smi_msg->data[2], msg->data, msg->data_len);
  1620. smi_msg->data_size = msg->data_len + 2;
  1621. ipmi_inc_stat(intf, sent_local_commands);
  1622. return 0;
  1623. }
  1624. static int i_ipmi_req_ipmb(struct ipmi_smi *intf,
  1625. struct ipmi_addr *addr,
  1626. long msgid,
  1627. struct kernel_ipmi_msg *msg,
  1628. struct ipmi_smi_msg *smi_msg,
  1629. struct ipmi_recv_msg *recv_msg,
  1630. unsigned char source_address,
  1631. unsigned char source_lun,
  1632. int retries,
  1633. unsigned int retry_time_ms)
  1634. {
  1635. struct ipmi_ipmb_addr *ipmb_addr;
  1636. unsigned char ipmb_seq;
  1637. long seqid;
  1638. int broadcast = 0;
  1639. struct ipmi_channel *chans;
  1640. int rv = 0;
  1641. if (addr->channel >= IPMI_MAX_CHANNELS) {
  1642. ipmi_inc_stat(intf, sent_invalid_commands);
  1643. return -EINVAL;
  1644. }
  1645. chans = READ_ONCE(intf->channel_list)->c;
  1646. if (chans[addr->channel].medium != IPMI_CHANNEL_MEDIUM_IPMB) {
  1647. ipmi_inc_stat(intf, sent_invalid_commands);
  1648. return -EINVAL;
  1649. }
  1650. if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
  1651. /*
  1652. * Broadcasts add a zero at the beginning of the
  1653. * message, but otherwise is the same as an IPMB
  1654. * address.
  1655. */
  1656. addr->addr_type = IPMI_IPMB_ADDR_TYPE;
  1657. broadcast = 1;
  1658. retries = 0; /* Don't retry broadcasts. */
  1659. }
  1660. /*
  1661. * 9 for the header and 1 for the checksum, plus
  1662. * possibly one for the broadcast.
  1663. */
  1664. if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
  1665. ipmi_inc_stat(intf, sent_invalid_commands);
  1666. return -EMSGSIZE;
  1667. }
  1668. ipmb_addr = (struct ipmi_ipmb_addr *) addr;
  1669. if (ipmb_addr->lun > 3) {
  1670. ipmi_inc_stat(intf, sent_invalid_commands);
  1671. return -EINVAL;
  1672. }
  1673. memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
  1674. if (recv_msg->msg.netfn & 0x1) {
  1675. /*
  1676. * It's a response, so use the user's sequence
  1677. * from msgid.
  1678. */
  1679. ipmi_inc_stat(intf, sent_ipmb_responses);
  1680. format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
  1681. msgid, broadcast,
  1682. source_address, source_lun);
  1683. /*
  1684. * Save the receive message so we can use it
  1685. * to deliver the response.
  1686. */
  1687. smi_msg->user_data = recv_msg;
  1688. } else {
  1689. /* It's a command, so get a sequence for it. */
  1690. unsigned long flags;
  1691. spin_lock_irqsave(&intf->seq_lock, flags);
  1692. if (is_maintenance_mode_cmd(msg))
  1693. intf->ipmb_maintenance_mode_timeout =
  1694. maintenance_mode_timeout_ms;
  1695. if (intf->ipmb_maintenance_mode_timeout && retry_time_ms == 0)
  1696. /* Different default in maintenance mode */
  1697. retry_time_ms = default_maintenance_retry_ms;
  1698. /*
  1699. * Create a sequence number with a 1 second
  1700. * timeout and 4 retries.
  1701. */
  1702. rv = intf_next_seq(intf,
  1703. recv_msg,
  1704. retry_time_ms,
  1705. retries,
  1706. broadcast,
  1707. &ipmb_seq,
  1708. &seqid);
  1709. if (rv)
  1710. /*
  1711. * We have used up all the sequence numbers,
  1712. * probably, so abort.
  1713. */
  1714. goto out_err;
  1715. ipmi_inc_stat(intf, sent_ipmb_commands);
  1716. /*
  1717. * Store the sequence number in the message,
  1718. * so that when the send message response
  1719. * comes back we can start the timer.
  1720. */
  1721. format_ipmb_msg(smi_msg, msg, ipmb_addr,
  1722. STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
  1723. ipmb_seq, broadcast,
  1724. source_address, source_lun);
  1725. /*
  1726. * Copy the message into the recv message data, so we
  1727. * can retransmit it later if necessary.
  1728. */
  1729. memcpy(recv_msg->msg_data, smi_msg->data,
  1730. smi_msg->data_size);
  1731. recv_msg->msg.data = recv_msg->msg_data;
  1732. recv_msg->msg.data_len = smi_msg->data_size;
  1733. /*
  1734. * We don't unlock until here, because we need
  1735. * to copy the completed message into the
  1736. * recv_msg before we release the lock.
  1737. * Otherwise, race conditions may bite us. I
  1738. * know that's pretty paranoid, but I prefer
  1739. * to be correct.
  1740. */
  1741. out_err:
  1742. spin_unlock_irqrestore(&intf->seq_lock, flags);
  1743. }
  1744. return rv;
  1745. }
  1746. static int i_ipmi_req_ipmb_direct(struct ipmi_smi *intf,
  1747. struct ipmi_addr *addr,
  1748. long msgid,
  1749. struct kernel_ipmi_msg *msg,
  1750. struct ipmi_smi_msg *smi_msg,
  1751. struct ipmi_recv_msg *recv_msg,
  1752. unsigned char source_lun)
  1753. {
  1754. struct ipmi_ipmb_direct_addr *daddr;
  1755. bool is_cmd = !(recv_msg->msg.netfn & 0x1);
  1756. if (!(intf->handlers->flags & IPMI_SMI_CAN_HANDLE_IPMB_DIRECT))
  1757. return -EAFNOSUPPORT;
  1758. /* Responses must have a completion code. */
  1759. if (!is_cmd && msg->data_len < 1) {
  1760. ipmi_inc_stat(intf, sent_invalid_commands);
  1761. return -EINVAL;
  1762. }
  1763. if ((msg->data_len + 4) > IPMI_MAX_MSG_LENGTH) {
  1764. ipmi_inc_stat(intf, sent_invalid_commands);
  1765. return -EMSGSIZE;
  1766. }
  1767. daddr = (struct ipmi_ipmb_direct_addr *) addr;
  1768. if (daddr->rq_lun > 3 || daddr->rs_lun > 3) {
  1769. ipmi_inc_stat(intf, sent_invalid_commands);
  1770. return -EINVAL;
  1771. }
  1772. smi_msg->type = IPMI_SMI_MSG_TYPE_IPMB_DIRECT;
  1773. smi_msg->msgid = msgid;
  1774. if (is_cmd) {
  1775. smi_msg->data[0] = msg->netfn << 2 | daddr->rs_lun;
  1776. smi_msg->data[2] = recv_msg->msgid << 2 | daddr->rq_lun;
  1777. } else {
  1778. smi_msg->data[0] = msg->netfn << 2 | daddr->rq_lun;
  1779. smi_msg->data[2] = recv_msg->msgid << 2 | daddr->rs_lun;
  1780. }
  1781. smi_msg->data[1] = daddr->slave_addr;
  1782. smi_msg->data[3] = msg->cmd;
  1783. memcpy(smi_msg->data + 4, msg->data, msg->data_len);
  1784. smi_msg->data_size = msg->data_len + 4;
  1785. smi_msg->user_data = recv_msg;
  1786. return 0;
  1787. }
  1788. static int i_ipmi_req_lan(struct ipmi_smi *intf,
  1789. struct ipmi_addr *addr,
  1790. long msgid,
  1791. struct kernel_ipmi_msg *msg,
  1792. struct ipmi_smi_msg *smi_msg,
  1793. struct ipmi_recv_msg *recv_msg,
  1794. unsigned char source_lun,
  1795. int retries,
  1796. unsigned int retry_time_ms)
  1797. {
  1798. struct ipmi_lan_addr *lan_addr;
  1799. unsigned char ipmb_seq;
  1800. long seqid;
  1801. struct ipmi_channel *chans;
  1802. int rv = 0;
  1803. if (addr->channel >= IPMI_MAX_CHANNELS) {
  1804. ipmi_inc_stat(intf, sent_invalid_commands);
  1805. return -EINVAL;
  1806. }
  1807. chans = READ_ONCE(intf->channel_list)->c;
  1808. if ((chans[addr->channel].medium
  1809. != IPMI_CHANNEL_MEDIUM_8023LAN)
  1810. && (chans[addr->channel].medium
  1811. != IPMI_CHANNEL_MEDIUM_ASYNC)) {
  1812. ipmi_inc_stat(intf, sent_invalid_commands);
  1813. return -EINVAL;
  1814. }
  1815. /* 11 for the header and 1 for the checksum. */
  1816. if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
  1817. ipmi_inc_stat(intf, sent_invalid_commands);
  1818. return -EMSGSIZE;
  1819. }
  1820. lan_addr = (struct ipmi_lan_addr *) addr;
  1821. if (lan_addr->lun > 3) {
  1822. ipmi_inc_stat(intf, sent_invalid_commands);
  1823. return -EINVAL;
  1824. }
  1825. memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
  1826. if (recv_msg->msg.netfn & 0x1) {
  1827. /*
  1828. * It's a response, so use the user's sequence
  1829. * from msgid.
  1830. */
  1831. ipmi_inc_stat(intf, sent_lan_responses);
  1832. format_lan_msg(smi_msg, msg, lan_addr, msgid,
  1833. msgid, source_lun);
  1834. /*
  1835. * Save the receive message so we can use it
  1836. * to deliver the response.
  1837. */
  1838. smi_msg->user_data = recv_msg;
  1839. } else {
  1840. /* It's a command, so get a sequence for it. */
  1841. unsigned long flags;
  1842. spin_lock_irqsave(&intf->seq_lock, flags);
  1843. /*
  1844. * Create a sequence number with a 1 second
  1845. * timeout and 4 retries.
  1846. */
  1847. rv = intf_next_seq(intf,
  1848. recv_msg,
  1849. retry_time_ms,
  1850. retries,
  1851. 0,
  1852. &ipmb_seq,
  1853. &seqid);
  1854. if (rv)
  1855. /*
  1856. * We have used up all the sequence numbers,
  1857. * probably, so abort.
  1858. */
  1859. goto out_err;
  1860. ipmi_inc_stat(intf, sent_lan_commands);
  1861. /*
  1862. * Store the sequence number in the message,
  1863. * so that when the send message response
  1864. * comes back we can start the timer.
  1865. */
  1866. format_lan_msg(smi_msg, msg, lan_addr,
  1867. STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
  1868. ipmb_seq, source_lun);
  1869. /*
  1870. * Copy the message into the recv message data, so we
  1871. * can retransmit it later if necessary.
  1872. */
  1873. memcpy(recv_msg->msg_data, smi_msg->data,
  1874. smi_msg->data_size);
  1875. recv_msg->msg.data = recv_msg->msg_data;
  1876. recv_msg->msg.data_len = smi_msg->data_size;
  1877. /*
  1878. * We don't unlock until here, because we need
  1879. * to copy the completed message into the
  1880. * recv_msg before we release the lock.
  1881. * Otherwise, race conditions may bite us. I
  1882. * know that's pretty paranoid, but I prefer
  1883. * to be correct.
  1884. */
  1885. out_err:
  1886. spin_unlock_irqrestore(&intf->seq_lock, flags);
  1887. }
  1888. return rv;
  1889. }
  1890. /*
  1891. * Separate from ipmi_request so that the user does not have to be
  1892. * supplied in certain circumstances (mainly at panic time). If
  1893. * messages are supplied, they will be freed, even if an error
  1894. * occurs.
  1895. */
  1896. static int i_ipmi_request(struct ipmi_user *user,
  1897. struct ipmi_smi *intf,
  1898. struct ipmi_addr *addr,
  1899. long msgid,
  1900. struct kernel_ipmi_msg *msg,
  1901. void *user_msg_data,
  1902. void *supplied_smi,
  1903. struct ipmi_recv_msg *supplied_recv,
  1904. int priority,
  1905. unsigned char source_address,
  1906. unsigned char source_lun,
  1907. int retries,
  1908. unsigned int retry_time_ms)
  1909. {
  1910. struct ipmi_smi_msg *smi_msg;
  1911. struct ipmi_recv_msg *recv_msg;
  1912. int rv = 0;
  1913. if (user) {
  1914. if (atomic_add_return(1, &user->nr_msgs) > max_msgs_per_user) {
  1915. /* Decrement will happen at the end of the routine. */
  1916. rv = -EBUSY;
  1917. goto out;
  1918. }
  1919. }
  1920. if (supplied_recv)
  1921. recv_msg = supplied_recv;
  1922. else {
  1923. recv_msg = ipmi_alloc_recv_msg();
  1924. if (recv_msg == NULL) {
  1925. rv = -ENOMEM;
  1926. goto out;
  1927. }
  1928. }
  1929. recv_msg->user_msg_data = user_msg_data;
  1930. if (supplied_smi)
  1931. smi_msg = supplied_smi;
  1932. else {
  1933. smi_msg = ipmi_alloc_smi_msg();
  1934. if (smi_msg == NULL) {
  1935. if (!supplied_recv)
  1936. ipmi_free_recv_msg(recv_msg);
  1937. rv = -ENOMEM;
  1938. goto out;
  1939. }
  1940. }
  1941. rcu_read_lock();
  1942. if (intf->in_shutdown) {
  1943. rv = -ENODEV;
  1944. goto out_err;
  1945. }
  1946. recv_msg->user = user;
  1947. if (user)
  1948. /* The put happens when the message is freed. */
  1949. kref_get(&user->refcount);
  1950. recv_msg->msgid = msgid;
  1951. /*
  1952. * Store the message to send in the receive message so timeout
  1953. * responses can get the proper response data.
  1954. */
  1955. recv_msg->msg = *msg;
  1956. if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
  1957. rv = i_ipmi_req_sysintf(intf, addr, msgid, msg, smi_msg,
  1958. recv_msg, retries, retry_time_ms);
  1959. } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
  1960. rv = i_ipmi_req_ipmb(intf, addr, msgid, msg, smi_msg, recv_msg,
  1961. source_address, source_lun,
  1962. retries, retry_time_ms);
  1963. } else if (is_ipmb_direct_addr(addr)) {
  1964. rv = i_ipmi_req_ipmb_direct(intf, addr, msgid, msg, smi_msg,
  1965. recv_msg, source_lun);
  1966. } else if (is_lan_addr(addr)) {
  1967. rv = i_ipmi_req_lan(intf, addr, msgid, msg, smi_msg, recv_msg,
  1968. source_lun, retries, retry_time_ms);
  1969. } else {
  1970. /* Unknown address type. */
  1971. ipmi_inc_stat(intf, sent_invalid_commands);
  1972. rv = -EINVAL;
  1973. }
  1974. if (rv) {
  1975. out_err:
  1976. ipmi_free_smi_msg(smi_msg);
  1977. ipmi_free_recv_msg(recv_msg);
  1978. } else {
  1979. dev_dbg(intf->si_dev, "Send: %*ph\n",
  1980. smi_msg->data_size, smi_msg->data);
  1981. smi_send(intf, intf->handlers, smi_msg, priority);
  1982. }
  1983. rcu_read_unlock();
  1984. out:
  1985. if (rv && user)
  1986. atomic_dec(&user->nr_msgs);
  1987. return rv;
  1988. }
  1989. static int check_addr(struct ipmi_smi *intf,
  1990. struct ipmi_addr *addr,
  1991. unsigned char *saddr,
  1992. unsigned char *lun)
  1993. {
  1994. if (addr->channel >= IPMI_MAX_CHANNELS)
  1995. return -EINVAL;
  1996. addr->channel = array_index_nospec(addr->channel, IPMI_MAX_CHANNELS);
  1997. *lun = intf->addrinfo[addr->channel].lun;
  1998. *saddr = intf->addrinfo[addr->channel].address;
  1999. return 0;
  2000. }
  2001. int ipmi_request_settime(struct ipmi_user *user,
  2002. struct ipmi_addr *addr,
  2003. long msgid,
  2004. struct kernel_ipmi_msg *msg,
  2005. void *user_msg_data,
  2006. int priority,
  2007. int retries,
  2008. unsigned int retry_time_ms)
  2009. {
  2010. unsigned char saddr = 0, lun = 0;
  2011. int rv, index;
  2012. if (!user)
  2013. return -EINVAL;
  2014. user = acquire_ipmi_user(user, &index);
  2015. if (!user)
  2016. return -ENODEV;
  2017. rv = check_addr(user->intf, addr, &saddr, &lun);
  2018. if (!rv)
  2019. rv = i_ipmi_request(user,
  2020. user->intf,
  2021. addr,
  2022. msgid,
  2023. msg,
  2024. user_msg_data,
  2025. NULL, NULL,
  2026. priority,
  2027. saddr,
  2028. lun,
  2029. retries,
  2030. retry_time_ms);
  2031. release_ipmi_user(user, index);
  2032. return rv;
  2033. }
  2034. EXPORT_SYMBOL(ipmi_request_settime);
  2035. int ipmi_request_supply_msgs(struct ipmi_user *user,
  2036. struct ipmi_addr *addr,
  2037. long msgid,
  2038. struct kernel_ipmi_msg *msg,
  2039. void *user_msg_data,
  2040. void *supplied_smi,
  2041. struct ipmi_recv_msg *supplied_recv,
  2042. int priority)
  2043. {
  2044. unsigned char saddr = 0, lun = 0;
  2045. int rv, index;
  2046. if (!user)
  2047. return -EINVAL;
  2048. user = acquire_ipmi_user(user, &index);
  2049. if (!user)
  2050. return -ENODEV;
  2051. rv = check_addr(user->intf, addr, &saddr, &lun);
  2052. if (!rv)
  2053. rv = i_ipmi_request(user,
  2054. user->intf,
  2055. addr,
  2056. msgid,
  2057. msg,
  2058. user_msg_data,
  2059. supplied_smi,
  2060. supplied_recv,
  2061. priority,
  2062. saddr,
  2063. lun,
  2064. -1, 0);
  2065. release_ipmi_user(user, index);
  2066. return rv;
  2067. }
  2068. EXPORT_SYMBOL(ipmi_request_supply_msgs);
  2069. static void bmc_device_id_handler(struct ipmi_smi *intf,
  2070. struct ipmi_recv_msg *msg)
  2071. {
  2072. int rv;
  2073. if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  2074. || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
  2075. || (msg->msg.cmd != IPMI_GET_DEVICE_ID_CMD)) {
  2076. dev_warn(intf->si_dev,
  2077. "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
  2078. msg->addr.addr_type, msg->msg.netfn, msg->msg.cmd);
  2079. return;
  2080. }
  2081. if (msg->msg.data[0]) {
  2082. dev_warn(intf->si_dev, "device id fetch failed: 0x%2.2x\n",
  2083. msg->msg.data[0]);
  2084. intf->bmc->dyn_id_set = 0;
  2085. goto out;
  2086. }
  2087. rv = ipmi_demangle_device_id(msg->msg.netfn, msg->msg.cmd,
  2088. msg->msg.data, msg->msg.data_len, &intf->bmc->fetch_id);
  2089. if (rv) {
  2090. dev_warn(intf->si_dev, "device id demangle failed: %d\n", rv);
  2091. /* record completion code when error */
  2092. intf->bmc->cc = msg->msg.data[0];
  2093. intf->bmc->dyn_id_set = 0;
  2094. } else {
  2095. /*
  2096. * Make sure the id data is available before setting
  2097. * dyn_id_set.
  2098. */
  2099. smp_wmb();
  2100. intf->bmc->dyn_id_set = 1;
  2101. }
  2102. out:
  2103. wake_up(&intf->waitq);
  2104. }
  2105. static int
  2106. send_get_device_id_cmd(struct ipmi_smi *intf)
  2107. {
  2108. struct ipmi_system_interface_addr si;
  2109. struct kernel_ipmi_msg msg;
  2110. si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2111. si.channel = IPMI_BMC_CHANNEL;
  2112. si.lun = 0;
  2113. msg.netfn = IPMI_NETFN_APP_REQUEST;
  2114. msg.cmd = IPMI_GET_DEVICE_ID_CMD;
  2115. msg.data = NULL;
  2116. msg.data_len = 0;
  2117. return i_ipmi_request(NULL,
  2118. intf,
  2119. (struct ipmi_addr *) &si,
  2120. 0,
  2121. &msg,
  2122. intf,
  2123. NULL,
  2124. NULL,
  2125. 0,
  2126. intf->addrinfo[0].address,
  2127. intf->addrinfo[0].lun,
  2128. -1, 0);
  2129. }
  2130. static int __get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc)
  2131. {
  2132. int rv;
  2133. unsigned int retry_count = 0;
  2134. intf->null_user_handler = bmc_device_id_handler;
  2135. retry:
  2136. bmc->cc = 0;
  2137. bmc->dyn_id_set = 2;
  2138. rv = send_get_device_id_cmd(intf);
  2139. if (rv)
  2140. goto out_reset_handler;
  2141. wait_event(intf->waitq, bmc->dyn_id_set != 2);
  2142. if (!bmc->dyn_id_set) {
  2143. if (bmc->cc != IPMI_CC_NO_ERROR &&
  2144. ++retry_count <= GET_DEVICE_ID_MAX_RETRY) {
  2145. msleep(500);
  2146. dev_warn(intf->si_dev,
  2147. "BMC returned 0x%2.2x, retry get bmc device id\n",
  2148. bmc->cc);
  2149. goto retry;
  2150. }
  2151. rv = -EIO; /* Something went wrong in the fetch. */
  2152. }
  2153. /* dyn_id_set makes the id data available. */
  2154. smp_rmb();
  2155. out_reset_handler:
  2156. intf->null_user_handler = NULL;
  2157. return rv;
  2158. }
  2159. /*
  2160. * Fetch the device id for the bmc/interface. You must pass in either
  2161. * bmc or intf, this code will get the other one. If the data has
  2162. * been recently fetched, this will just use the cached data. Otherwise
  2163. * it will run a new fetch.
  2164. *
  2165. * Except for the first time this is called (in ipmi_add_smi()),
  2166. * this will always return good data;
  2167. */
  2168. static int __bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
  2169. struct ipmi_device_id *id,
  2170. bool *guid_set, guid_t *guid, int intf_num)
  2171. {
  2172. int rv = 0;
  2173. int prev_dyn_id_set, prev_guid_set;
  2174. bool intf_set = intf != NULL;
  2175. if (!intf) {
  2176. mutex_lock(&bmc->dyn_mutex);
  2177. retry_bmc_lock:
  2178. if (list_empty(&bmc->intfs)) {
  2179. mutex_unlock(&bmc->dyn_mutex);
  2180. return -ENOENT;
  2181. }
  2182. intf = list_first_entry(&bmc->intfs, struct ipmi_smi,
  2183. bmc_link);
  2184. kref_get(&intf->refcount);
  2185. mutex_unlock(&bmc->dyn_mutex);
  2186. mutex_lock(&intf->bmc_reg_mutex);
  2187. mutex_lock(&bmc->dyn_mutex);
  2188. if (intf != list_first_entry(&bmc->intfs, struct ipmi_smi,
  2189. bmc_link)) {
  2190. mutex_unlock(&intf->bmc_reg_mutex);
  2191. kref_put(&intf->refcount, intf_free);
  2192. goto retry_bmc_lock;
  2193. }
  2194. } else {
  2195. mutex_lock(&intf->bmc_reg_mutex);
  2196. bmc = intf->bmc;
  2197. mutex_lock(&bmc->dyn_mutex);
  2198. kref_get(&intf->refcount);
  2199. }
  2200. /* If we have a valid and current ID, just return that. */
  2201. if (intf->in_bmc_register ||
  2202. (bmc->dyn_id_set && time_is_after_jiffies(bmc->dyn_id_expiry)))
  2203. goto out_noprocessing;
  2204. prev_guid_set = bmc->dyn_guid_set;
  2205. __get_guid(intf);
  2206. prev_dyn_id_set = bmc->dyn_id_set;
  2207. rv = __get_device_id(intf, bmc);
  2208. if (rv)
  2209. goto out;
  2210. /*
  2211. * The guid, device id, manufacturer id, and product id should
  2212. * not change on a BMC. If it does we have to do some dancing.
  2213. */
  2214. if (!intf->bmc_registered
  2215. || (!prev_guid_set && bmc->dyn_guid_set)
  2216. || (!prev_dyn_id_set && bmc->dyn_id_set)
  2217. || (prev_guid_set && bmc->dyn_guid_set
  2218. && !guid_equal(&bmc->guid, &bmc->fetch_guid))
  2219. || bmc->id.device_id != bmc->fetch_id.device_id
  2220. || bmc->id.manufacturer_id != bmc->fetch_id.manufacturer_id
  2221. || bmc->id.product_id != bmc->fetch_id.product_id) {
  2222. struct ipmi_device_id id = bmc->fetch_id;
  2223. int guid_set = bmc->dyn_guid_set;
  2224. guid_t guid;
  2225. guid = bmc->fetch_guid;
  2226. mutex_unlock(&bmc->dyn_mutex);
  2227. __ipmi_bmc_unregister(intf);
  2228. /* Fill in the temporary BMC for good measure. */
  2229. intf->bmc->id = id;
  2230. intf->bmc->dyn_guid_set = guid_set;
  2231. intf->bmc->guid = guid;
  2232. if (__ipmi_bmc_register(intf, &id, guid_set, &guid, intf_num))
  2233. need_waiter(intf); /* Retry later on an error. */
  2234. else
  2235. __scan_channels(intf, &id);
  2236. if (!intf_set) {
  2237. /*
  2238. * We weren't given the interface on the
  2239. * command line, so restart the operation on
  2240. * the next interface for the BMC.
  2241. */
  2242. mutex_unlock(&intf->bmc_reg_mutex);
  2243. mutex_lock(&bmc->dyn_mutex);
  2244. goto retry_bmc_lock;
  2245. }
  2246. /* We have a new BMC, set it up. */
  2247. bmc = intf->bmc;
  2248. mutex_lock(&bmc->dyn_mutex);
  2249. goto out_noprocessing;
  2250. } else if (memcmp(&bmc->fetch_id, &bmc->id, sizeof(bmc->id)))
  2251. /* Version info changes, scan the channels again. */
  2252. __scan_channels(intf, &bmc->fetch_id);
  2253. bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
  2254. out:
  2255. if (rv && prev_dyn_id_set) {
  2256. rv = 0; /* Ignore failures if we have previous data. */
  2257. bmc->dyn_id_set = prev_dyn_id_set;
  2258. }
  2259. if (!rv) {
  2260. bmc->id = bmc->fetch_id;
  2261. if (bmc->dyn_guid_set)
  2262. bmc->guid = bmc->fetch_guid;
  2263. else if (prev_guid_set)
  2264. /*
  2265. * The guid used to be valid and it failed to fetch,
  2266. * just use the cached value.
  2267. */
  2268. bmc->dyn_guid_set = prev_guid_set;
  2269. }
  2270. out_noprocessing:
  2271. if (!rv) {
  2272. if (id)
  2273. *id = bmc->id;
  2274. if (guid_set)
  2275. *guid_set = bmc->dyn_guid_set;
  2276. if (guid && bmc->dyn_guid_set)
  2277. *guid = bmc->guid;
  2278. }
  2279. mutex_unlock(&bmc->dyn_mutex);
  2280. mutex_unlock(&intf->bmc_reg_mutex);
  2281. kref_put(&intf->refcount, intf_free);
  2282. return rv;
  2283. }
  2284. static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
  2285. struct ipmi_device_id *id,
  2286. bool *guid_set, guid_t *guid)
  2287. {
  2288. return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1);
  2289. }
  2290. static ssize_t device_id_show(struct device *dev,
  2291. struct device_attribute *attr,
  2292. char *buf)
  2293. {
  2294. struct bmc_device *bmc = to_bmc_device(dev);
  2295. struct ipmi_device_id id;
  2296. int rv;
  2297. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2298. if (rv)
  2299. return rv;
  2300. return sysfs_emit(buf, "%u\n", id.device_id);
  2301. }
  2302. static DEVICE_ATTR_RO(device_id);
  2303. static ssize_t provides_device_sdrs_show(struct device *dev,
  2304. struct device_attribute *attr,
  2305. char *buf)
  2306. {
  2307. struct bmc_device *bmc = to_bmc_device(dev);
  2308. struct ipmi_device_id id;
  2309. int rv;
  2310. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2311. if (rv)
  2312. return rv;
  2313. return sysfs_emit(buf, "%u\n", (id.device_revision & 0x80) >> 7);
  2314. }
  2315. static DEVICE_ATTR_RO(provides_device_sdrs);
  2316. static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
  2317. char *buf)
  2318. {
  2319. struct bmc_device *bmc = to_bmc_device(dev);
  2320. struct ipmi_device_id id;
  2321. int rv;
  2322. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2323. if (rv)
  2324. return rv;
  2325. return sysfs_emit(buf, "%u\n", id.device_revision & 0x0F);
  2326. }
  2327. static DEVICE_ATTR_RO(revision);
  2328. static ssize_t firmware_revision_show(struct device *dev,
  2329. struct device_attribute *attr,
  2330. char *buf)
  2331. {
  2332. struct bmc_device *bmc = to_bmc_device(dev);
  2333. struct ipmi_device_id id;
  2334. int rv;
  2335. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2336. if (rv)
  2337. return rv;
  2338. return sysfs_emit(buf, "%u.%x\n", id.firmware_revision_1,
  2339. id.firmware_revision_2);
  2340. }
  2341. static DEVICE_ATTR_RO(firmware_revision);
  2342. static ssize_t ipmi_version_show(struct device *dev,
  2343. struct device_attribute *attr,
  2344. char *buf)
  2345. {
  2346. struct bmc_device *bmc = to_bmc_device(dev);
  2347. struct ipmi_device_id id;
  2348. int rv;
  2349. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2350. if (rv)
  2351. return rv;
  2352. return sysfs_emit(buf, "%u.%u\n",
  2353. ipmi_version_major(&id),
  2354. ipmi_version_minor(&id));
  2355. }
  2356. static DEVICE_ATTR_RO(ipmi_version);
  2357. static ssize_t add_dev_support_show(struct device *dev,
  2358. struct device_attribute *attr,
  2359. char *buf)
  2360. {
  2361. struct bmc_device *bmc = to_bmc_device(dev);
  2362. struct ipmi_device_id id;
  2363. int rv;
  2364. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2365. if (rv)
  2366. return rv;
  2367. return sysfs_emit(buf, "0x%02x\n", id.additional_device_support);
  2368. }
  2369. static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
  2370. NULL);
  2371. static ssize_t manufacturer_id_show(struct device *dev,
  2372. struct device_attribute *attr,
  2373. char *buf)
  2374. {
  2375. struct bmc_device *bmc = to_bmc_device(dev);
  2376. struct ipmi_device_id id;
  2377. int rv;
  2378. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2379. if (rv)
  2380. return rv;
  2381. return sysfs_emit(buf, "0x%6.6x\n", id.manufacturer_id);
  2382. }
  2383. static DEVICE_ATTR_RO(manufacturer_id);
  2384. static ssize_t product_id_show(struct device *dev,
  2385. struct device_attribute *attr,
  2386. char *buf)
  2387. {
  2388. struct bmc_device *bmc = to_bmc_device(dev);
  2389. struct ipmi_device_id id;
  2390. int rv;
  2391. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2392. if (rv)
  2393. return rv;
  2394. return sysfs_emit(buf, "0x%4.4x\n", id.product_id);
  2395. }
  2396. static DEVICE_ATTR_RO(product_id);
  2397. static ssize_t aux_firmware_rev_show(struct device *dev,
  2398. struct device_attribute *attr,
  2399. char *buf)
  2400. {
  2401. struct bmc_device *bmc = to_bmc_device(dev);
  2402. struct ipmi_device_id id;
  2403. int rv;
  2404. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2405. if (rv)
  2406. return rv;
  2407. return sysfs_emit(buf, "0x%02x 0x%02x 0x%02x 0x%02x\n",
  2408. id.aux_firmware_revision[3],
  2409. id.aux_firmware_revision[2],
  2410. id.aux_firmware_revision[1],
  2411. id.aux_firmware_revision[0]);
  2412. }
  2413. static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
  2414. static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
  2415. char *buf)
  2416. {
  2417. struct bmc_device *bmc = to_bmc_device(dev);
  2418. bool guid_set;
  2419. guid_t guid;
  2420. int rv;
  2421. rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, &guid);
  2422. if (rv)
  2423. return rv;
  2424. if (!guid_set)
  2425. return -ENOENT;
  2426. return sysfs_emit(buf, "%pUl\n", &guid);
  2427. }
  2428. static DEVICE_ATTR_RO(guid);
  2429. static struct attribute *bmc_dev_attrs[] = {
  2430. &dev_attr_device_id.attr,
  2431. &dev_attr_provides_device_sdrs.attr,
  2432. &dev_attr_revision.attr,
  2433. &dev_attr_firmware_revision.attr,
  2434. &dev_attr_ipmi_version.attr,
  2435. &dev_attr_additional_device_support.attr,
  2436. &dev_attr_manufacturer_id.attr,
  2437. &dev_attr_product_id.attr,
  2438. &dev_attr_aux_firmware_revision.attr,
  2439. &dev_attr_guid.attr,
  2440. NULL
  2441. };
  2442. static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
  2443. struct attribute *attr, int idx)
  2444. {
  2445. struct device *dev = kobj_to_dev(kobj);
  2446. struct bmc_device *bmc = to_bmc_device(dev);
  2447. umode_t mode = attr->mode;
  2448. int rv;
  2449. if (attr == &dev_attr_aux_firmware_revision.attr) {
  2450. struct ipmi_device_id id;
  2451. rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
  2452. return (!rv && id.aux_firmware_revision_set) ? mode : 0;
  2453. }
  2454. if (attr == &dev_attr_guid.attr) {
  2455. bool guid_set;
  2456. rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, NULL);
  2457. return (!rv && guid_set) ? mode : 0;
  2458. }
  2459. return mode;
  2460. }
  2461. static const struct attribute_group bmc_dev_attr_group = {
  2462. .attrs = bmc_dev_attrs,
  2463. .is_visible = bmc_dev_attr_is_visible,
  2464. };
  2465. static const struct attribute_group *bmc_dev_attr_groups[] = {
  2466. &bmc_dev_attr_group,
  2467. NULL
  2468. };
  2469. static const struct device_type bmc_device_type = {
  2470. .groups = bmc_dev_attr_groups,
  2471. };
  2472. static int __find_bmc_guid(struct device *dev, const void *data)
  2473. {
  2474. const guid_t *guid = data;
  2475. struct bmc_device *bmc;
  2476. int rv;
  2477. if (dev->type != &bmc_device_type)
  2478. return 0;
  2479. bmc = to_bmc_device(dev);
  2480. rv = bmc->dyn_guid_set && guid_equal(&bmc->guid, guid);
  2481. if (rv)
  2482. rv = kref_get_unless_zero(&bmc->usecount);
  2483. return rv;
  2484. }
  2485. /*
  2486. * Returns with the bmc's usecount incremented, if it is non-NULL.
  2487. */
  2488. static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
  2489. guid_t *guid)
  2490. {
  2491. struct device *dev;
  2492. struct bmc_device *bmc = NULL;
  2493. dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
  2494. if (dev) {
  2495. bmc = to_bmc_device(dev);
  2496. put_device(dev);
  2497. }
  2498. return bmc;
  2499. }
  2500. struct prod_dev_id {
  2501. unsigned int product_id;
  2502. unsigned char device_id;
  2503. };
  2504. static int __find_bmc_prod_dev_id(struct device *dev, const void *data)
  2505. {
  2506. const struct prod_dev_id *cid = data;
  2507. struct bmc_device *bmc;
  2508. int rv;
  2509. if (dev->type != &bmc_device_type)
  2510. return 0;
  2511. bmc = to_bmc_device(dev);
  2512. rv = (bmc->id.product_id == cid->product_id
  2513. && bmc->id.device_id == cid->device_id);
  2514. if (rv)
  2515. rv = kref_get_unless_zero(&bmc->usecount);
  2516. return rv;
  2517. }
  2518. /*
  2519. * Returns with the bmc's usecount incremented, if it is non-NULL.
  2520. */
  2521. static struct bmc_device *ipmi_find_bmc_prod_dev_id(
  2522. struct device_driver *drv,
  2523. unsigned int product_id, unsigned char device_id)
  2524. {
  2525. struct prod_dev_id id = {
  2526. .product_id = product_id,
  2527. .device_id = device_id,
  2528. };
  2529. struct device *dev;
  2530. struct bmc_device *bmc = NULL;
  2531. dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
  2532. if (dev) {
  2533. bmc = to_bmc_device(dev);
  2534. put_device(dev);
  2535. }
  2536. return bmc;
  2537. }
  2538. static DEFINE_IDA(ipmi_bmc_ida);
  2539. static void
  2540. release_bmc_device(struct device *dev)
  2541. {
  2542. kfree(to_bmc_device(dev));
  2543. }
  2544. static void cleanup_bmc_work(struct work_struct *work)
  2545. {
  2546. struct bmc_device *bmc = container_of(work, struct bmc_device,
  2547. remove_work);
  2548. int id = bmc->pdev.id; /* Unregister overwrites id */
  2549. platform_device_unregister(&bmc->pdev);
  2550. ida_simple_remove(&ipmi_bmc_ida, id);
  2551. }
  2552. static void
  2553. cleanup_bmc_device(struct kref *ref)
  2554. {
  2555. struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
  2556. /*
  2557. * Remove the platform device in a work queue to avoid issues
  2558. * with removing the device attributes while reading a device
  2559. * attribute.
  2560. */
  2561. queue_work(remove_work_wq, &bmc->remove_work);
  2562. }
  2563. /*
  2564. * Must be called with intf->bmc_reg_mutex held.
  2565. */
  2566. static void __ipmi_bmc_unregister(struct ipmi_smi *intf)
  2567. {
  2568. struct bmc_device *bmc = intf->bmc;
  2569. if (!intf->bmc_registered)
  2570. return;
  2571. sysfs_remove_link(&intf->si_dev->kobj, "bmc");
  2572. sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
  2573. kfree(intf->my_dev_name);
  2574. intf->my_dev_name = NULL;
  2575. mutex_lock(&bmc->dyn_mutex);
  2576. list_del(&intf->bmc_link);
  2577. mutex_unlock(&bmc->dyn_mutex);
  2578. intf->bmc = &intf->tmp_bmc;
  2579. kref_put(&bmc->usecount, cleanup_bmc_device);
  2580. intf->bmc_registered = false;
  2581. }
  2582. static void ipmi_bmc_unregister(struct ipmi_smi *intf)
  2583. {
  2584. mutex_lock(&intf->bmc_reg_mutex);
  2585. __ipmi_bmc_unregister(intf);
  2586. mutex_unlock(&intf->bmc_reg_mutex);
  2587. }
  2588. /*
  2589. * Must be called with intf->bmc_reg_mutex held.
  2590. */
  2591. static int __ipmi_bmc_register(struct ipmi_smi *intf,
  2592. struct ipmi_device_id *id,
  2593. bool guid_set, guid_t *guid, int intf_num)
  2594. {
  2595. int rv;
  2596. struct bmc_device *bmc;
  2597. struct bmc_device *old_bmc;
  2598. /*
  2599. * platform_device_register() can cause bmc_reg_mutex to
  2600. * be claimed because of the is_visible functions of
  2601. * the attributes. Eliminate possible recursion and
  2602. * release the lock.
  2603. */
  2604. intf->in_bmc_register = true;
  2605. mutex_unlock(&intf->bmc_reg_mutex);
  2606. /*
  2607. * Try to find if there is an bmc_device struct
  2608. * representing the interfaced BMC already
  2609. */
  2610. mutex_lock(&ipmidriver_mutex);
  2611. if (guid_set)
  2612. old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, guid);
  2613. else
  2614. old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
  2615. id->product_id,
  2616. id->device_id);
  2617. /*
  2618. * If there is already an bmc_device, free the new one,
  2619. * otherwise register the new BMC device
  2620. */
  2621. if (old_bmc) {
  2622. bmc = old_bmc;
  2623. /*
  2624. * Note: old_bmc already has usecount incremented by
  2625. * the BMC find functions.
  2626. */
  2627. intf->bmc = old_bmc;
  2628. mutex_lock(&bmc->dyn_mutex);
  2629. list_add_tail(&intf->bmc_link, &bmc->intfs);
  2630. mutex_unlock(&bmc->dyn_mutex);
  2631. dev_info(intf->si_dev,
  2632. "interfacing existing BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
  2633. bmc->id.manufacturer_id,
  2634. bmc->id.product_id,
  2635. bmc->id.device_id);
  2636. } else {
  2637. bmc = kzalloc(sizeof(*bmc), GFP_KERNEL);
  2638. if (!bmc) {
  2639. rv = -ENOMEM;
  2640. goto out;
  2641. }
  2642. INIT_LIST_HEAD(&bmc->intfs);
  2643. mutex_init(&bmc->dyn_mutex);
  2644. INIT_WORK(&bmc->remove_work, cleanup_bmc_work);
  2645. bmc->id = *id;
  2646. bmc->dyn_id_set = 1;
  2647. bmc->dyn_guid_set = guid_set;
  2648. bmc->guid = *guid;
  2649. bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
  2650. bmc->pdev.name = "ipmi_bmc";
  2651. rv = ida_simple_get(&ipmi_bmc_ida, 0, 0, GFP_KERNEL);
  2652. if (rv < 0) {
  2653. kfree(bmc);
  2654. goto out;
  2655. }
  2656. bmc->pdev.dev.driver = &ipmidriver.driver;
  2657. bmc->pdev.id = rv;
  2658. bmc->pdev.dev.release = release_bmc_device;
  2659. bmc->pdev.dev.type = &bmc_device_type;
  2660. kref_init(&bmc->usecount);
  2661. intf->bmc = bmc;
  2662. mutex_lock(&bmc->dyn_mutex);
  2663. list_add_tail(&intf->bmc_link, &bmc->intfs);
  2664. mutex_unlock(&bmc->dyn_mutex);
  2665. rv = platform_device_register(&bmc->pdev);
  2666. if (rv) {
  2667. dev_err(intf->si_dev,
  2668. "Unable to register bmc device: %d\n",
  2669. rv);
  2670. goto out_list_del;
  2671. }
  2672. dev_info(intf->si_dev,
  2673. "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
  2674. bmc->id.manufacturer_id,
  2675. bmc->id.product_id,
  2676. bmc->id.device_id);
  2677. }
  2678. /*
  2679. * create symlink from system interface device to bmc device
  2680. * and back.
  2681. */
  2682. rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
  2683. if (rv) {
  2684. dev_err(intf->si_dev, "Unable to create bmc symlink: %d\n", rv);
  2685. goto out_put_bmc;
  2686. }
  2687. if (intf_num == -1)
  2688. intf_num = intf->intf_num;
  2689. intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", intf_num);
  2690. if (!intf->my_dev_name) {
  2691. rv = -ENOMEM;
  2692. dev_err(intf->si_dev, "Unable to allocate link from BMC: %d\n",
  2693. rv);
  2694. goto out_unlink1;
  2695. }
  2696. rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
  2697. intf->my_dev_name);
  2698. if (rv) {
  2699. dev_err(intf->si_dev, "Unable to create symlink to bmc: %d\n",
  2700. rv);
  2701. goto out_free_my_dev_name;
  2702. }
  2703. intf->bmc_registered = true;
  2704. out:
  2705. mutex_unlock(&ipmidriver_mutex);
  2706. mutex_lock(&intf->bmc_reg_mutex);
  2707. intf->in_bmc_register = false;
  2708. return rv;
  2709. out_free_my_dev_name:
  2710. kfree(intf->my_dev_name);
  2711. intf->my_dev_name = NULL;
  2712. out_unlink1:
  2713. sysfs_remove_link(&intf->si_dev->kobj, "bmc");
  2714. out_put_bmc:
  2715. mutex_lock(&bmc->dyn_mutex);
  2716. list_del(&intf->bmc_link);
  2717. mutex_unlock(&bmc->dyn_mutex);
  2718. intf->bmc = &intf->tmp_bmc;
  2719. kref_put(&bmc->usecount, cleanup_bmc_device);
  2720. goto out;
  2721. out_list_del:
  2722. mutex_lock(&bmc->dyn_mutex);
  2723. list_del(&intf->bmc_link);
  2724. mutex_unlock(&bmc->dyn_mutex);
  2725. intf->bmc = &intf->tmp_bmc;
  2726. put_device(&bmc->pdev.dev);
  2727. goto out;
  2728. }
  2729. static int
  2730. send_guid_cmd(struct ipmi_smi *intf, int chan)
  2731. {
  2732. struct kernel_ipmi_msg msg;
  2733. struct ipmi_system_interface_addr si;
  2734. si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2735. si.channel = IPMI_BMC_CHANNEL;
  2736. si.lun = 0;
  2737. msg.netfn = IPMI_NETFN_APP_REQUEST;
  2738. msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
  2739. msg.data = NULL;
  2740. msg.data_len = 0;
  2741. return i_ipmi_request(NULL,
  2742. intf,
  2743. (struct ipmi_addr *) &si,
  2744. 0,
  2745. &msg,
  2746. intf,
  2747. NULL,
  2748. NULL,
  2749. 0,
  2750. intf->addrinfo[0].address,
  2751. intf->addrinfo[0].lun,
  2752. -1, 0);
  2753. }
  2754. static void guid_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
  2755. {
  2756. struct bmc_device *bmc = intf->bmc;
  2757. if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  2758. || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
  2759. || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
  2760. /* Not for me */
  2761. return;
  2762. if (msg->msg.data[0] != 0) {
  2763. /* Error from getting the GUID, the BMC doesn't have one. */
  2764. bmc->dyn_guid_set = 0;
  2765. goto out;
  2766. }
  2767. if (msg->msg.data_len < UUID_SIZE + 1) {
  2768. bmc->dyn_guid_set = 0;
  2769. dev_warn(intf->si_dev,
  2770. "The GUID response from the BMC was too short, it was %d but should have been %d. Assuming GUID is not available.\n",
  2771. msg->msg.data_len, UUID_SIZE + 1);
  2772. goto out;
  2773. }
  2774. import_guid(&bmc->fetch_guid, msg->msg.data + 1);
  2775. /*
  2776. * Make sure the guid data is available before setting
  2777. * dyn_guid_set.
  2778. */
  2779. smp_wmb();
  2780. bmc->dyn_guid_set = 1;
  2781. out:
  2782. wake_up(&intf->waitq);
  2783. }
  2784. static void __get_guid(struct ipmi_smi *intf)
  2785. {
  2786. int rv;
  2787. struct bmc_device *bmc = intf->bmc;
  2788. bmc->dyn_guid_set = 2;
  2789. intf->null_user_handler = guid_handler;
  2790. rv = send_guid_cmd(intf, 0);
  2791. if (rv)
  2792. /* Send failed, no GUID available. */
  2793. bmc->dyn_guid_set = 0;
  2794. else
  2795. wait_event(intf->waitq, bmc->dyn_guid_set != 2);
  2796. /* dyn_guid_set makes the guid data available. */
  2797. smp_rmb();
  2798. intf->null_user_handler = NULL;
  2799. }
  2800. static int
  2801. send_channel_info_cmd(struct ipmi_smi *intf, int chan)
  2802. {
  2803. struct kernel_ipmi_msg msg;
  2804. unsigned char data[1];
  2805. struct ipmi_system_interface_addr si;
  2806. si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2807. si.channel = IPMI_BMC_CHANNEL;
  2808. si.lun = 0;
  2809. msg.netfn = IPMI_NETFN_APP_REQUEST;
  2810. msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
  2811. msg.data = data;
  2812. msg.data_len = 1;
  2813. data[0] = chan;
  2814. return i_ipmi_request(NULL,
  2815. intf,
  2816. (struct ipmi_addr *) &si,
  2817. 0,
  2818. &msg,
  2819. intf,
  2820. NULL,
  2821. NULL,
  2822. 0,
  2823. intf->addrinfo[0].address,
  2824. intf->addrinfo[0].lun,
  2825. -1, 0);
  2826. }
  2827. static void
  2828. channel_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
  2829. {
  2830. int rv = 0;
  2831. int ch;
  2832. unsigned int set = intf->curr_working_cset;
  2833. struct ipmi_channel *chans;
  2834. if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  2835. && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
  2836. && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
  2837. /* It's the one we want */
  2838. if (msg->msg.data[0] != 0) {
  2839. /* Got an error from the channel, just go on. */
  2840. if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
  2841. /*
  2842. * If the MC does not support this
  2843. * command, that is legal. We just
  2844. * assume it has one IPMB at channel
  2845. * zero.
  2846. */
  2847. intf->wchannels[set].c[0].medium
  2848. = IPMI_CHANNEL_MEDIUM_IPMB;
  2849. intf->wchannels[set].c[0].protocol
  2850. = IPMI_CHANNEL_PROTOCOL_IPMB;
  2851. intf->channel_list = intf->wchannels + set;
  2852. intf->channels_ready = true;
  2853. wake_up(&intf->waitq);
  2854. goto out;
  2855. }
  2856. goto next_channel;
  2857. }
  2858. if (msg->msg.data_len < 4) {
  2859. /* Message not big enough, just go on. */
  2860. goto next_channel;
  2861. }
  2862. ch = intf->curr_channel;
  2863. chans = intf->wchannels[set].c;
  2864. chans[ch].medium = msg->msg.data[2] & 0x7f;
  2865. chans[ch].protocol = msg->msg.data[3] & 0x1f;
  2866. next_channel:
  2867. intf->curr_channel++;
  2868. if (intf->curr_channel >= IPMI_MAX_CHANNELS) {
  2869. intf->channel_list = intf->wchannels + set;
  2870. intf->channels_ready = true;
  2871. wake_up(&intf->waitq);
  2872. } else {
  2873. intf->channel_list = intf->wchannels + set;
  2874. intf->channels_ready = true;
  2875. rv = send_channel_info_cmd(intf, intf->curr_channel);
  2876. }
  2877. if (rv) {
  2878. /* Got an error somehow, just give up. */
  2879. dev_warn(intf->si_dev,
  2880. "Error sending channel information for channel %d: %d\n",
  2881. intf->curr_channel, rv);
  2882. intf->channel_list = intf->wchannels + set;
  2883. intf->channels_ready = true;
  2884. wake_up(&intf->waitq);
  2885. }
  2886. }
  2887. out:
  2888. return;
  2889. }
  2890. /*
  2891. * Must be holding intf->bmc_reg_mutex to call this.
  2892. */
  2893. static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id)
  2894. {
  2895. int rv;
  2896. if (ipmi_version_major(id) > 1
  2897. || (ipmi_version_major(id) == 1
  2898. && ipmi_version_minor(id) >= 5)) {
  2899. unsigned int set;
  2900. /*
  2901. * Start scanning the channels to see what is
  2902. * available.
  2903. */
  2904. set = !intf->curr_working_cset;
  2905. intf->curr_working_cset = set;
  2906. memset(&intf->wchannels[set], 0,
  2907. sizeof(struct ipmi_channel_set));
  2908. intf->null_user_handler = channel_handler;
  2909. intf->curr_channel = 0;
  2910. rv = send_channel_info_cmd(intf, 0);
  2911. if (rv) {
  2912. dev_warn(intf->si_dev,
  2913. "Error sending channel information for channel 0, %d\n",
  2914. rv);
  2915. intf->null_user_handler = NULL;
  2916. return -EIO;
  2917. }
  2918. /* Wait for the channel info to be read. */
  2919. wait_event(intf->waitq, intf->channels_ready);
  2920. intf->null_user_handler = NULL;
  2921. } else {
  2922. unsigned int set = intf->curr_working_cset;
  2923. /* Assume a single IPMB channel at zero. */
  2924. intf->wchannels[set].c[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
  2925. intf->wchannels[set].c[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
  2926. intf->channel_list = intf->wchannels + set;
  2927. intf->channels_ready = true;
  2928. }
  2929. return 0;
  2930. }
  2931. static void ipmi_poll(struct ipmi_smi *intf)
  2932. {
  2933. if (intf->handlers->poll)
  2934. intf->handlers->poll(intf->send_info);
  2935. /* In case something came in */
  2936. handle_new_recv_msgs(intf);
  2937. }
  2938. void ipmi_poll_interface(struct ipmi_user *user)
  2939. {
  2940. ipmi_poll(user->intf);
  2941. }
  2942. EXPORT_SYMBOL(ipmi_poll_interface);
  2943. static ssize_t nr_users_show(struct device *dev,
  2944. struct device_attribute *attr,
  2945. char *buf)
  2946. {
  2947. struct ipmi_smi *intf = container_of(attr,
  2948. struct ipmi_smi, nr_users_devattr);
  2949. return sysfs_emit(buf, "%d\n", atomic_read(&intf->nr_users));
  2950. }
  2951. static DEVICE_ATTR_RO(nr_users);
  2952. static ssize_t nr_msgs_show(struct device *dev,
  2953. struct device_attribute *attr,
  2954. char *buf)
  2955. {
  2956. struct ipmi_smi *intf = container_of(attr,
  2957. struct ipmi_smi, nr_msgs_devattr);
  2958. struct ipmi_user *user;
  2959. int index;
  2960. unsigned int count = 0;
  2961. index = srcu_read_lock(&intf->users_srcu);
  2962. list_for_each_entry_rcu(user, &intf->users, link)
  2963. count += atomic_read(&user->nr_msgs);
  2964. srcu_read_unlock(&intf->users_srcu, index);
  2965. return sysfs_emit(buf, "%u\n", count);
  2966. }
  2967. static DEVICE_ATTR_RO(nr_msgs);
  2968. static void redo_bmc_reg(struct work_struct *work)
  2969. {
  2970. struct ipmi_smi *intf = container_of(work, struct ipmi_smi,
  2971. bmc_reg_work);
  2972. if (!intf->in_shutdown)
  2973. bmc_get_device_id(intf, NULL, NULL, NULL, NULL);
  2974. kref_put(&intf->refcount, intf_free);
  2975. }
  2976. int ipmi_add_smi(struct module *owner,
  2977. const struct ipmi_smi_handlers *handlers,
  2978. void *send_info,
  2979. struct device *si_dev,
  2980. unsigned char slave_addr)
  2981. {
  2982. int i, j;
  2983. int rv;
  2984. struct ipmi_smi *intf, *tintf;
  2985. struct list_head *link;
  2986. struct ipmi_device_id id;
  2987. /*
  2988. * Make sure the driver is actually initialized, this handles
  2989. * problems with initialization order.
  2990. */
  2991. rv = ipmi_init_msghandler();
  2992. if (rv)
  2993. return rv;
  2994. intf = kzalloc(sizeof(*intf), GFP_KERNEL);
  2995. if (!intf)
  2996. return -ENOMEM;
  2997. rv = init_srcu_struct(&intf->users_srcu);
  2998. if (rv) {
  2999. kfree(intf);
  3000. return rv;
  3001. }
  3002. intf->owner = owner;
  3003. intf->bmc = &intf->tmp_bmc;
  3004. INIT_LIST_HEAD(&intf->bmc->intfs);
  3005. mutex_init(&intf->bmc->dyn_mutex);
  3006. INIT_LIST_HEAD(&intf->bmc_link);
  3007. mutex_init(&intf->bmc_reg_mutex);
  3008. intf->intf_num = -1; /* Mark it invalid for now. */
  3009. kref_init(&intf->refcount);
  3010. INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg);
  3011. intf->si_dev = si_dev;
  3012. for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
  3013. intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR;
  3014. intf->addrinfo[j].lun = 2;
  3015. }
  3016. if (slave_addr != 0)
  3017. intf->addrinfo[0].address = slave_addr;
  3018. INIT_LIST_HEAD(&intf->users);
  3019. atomic_set(&intf->nr_users, 0);
  3020. intf->handlers = handlers;
  3021. intf->send_info = send_info;
  3022. spin_lock_init(&intf->seq_lock);
  3023. for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
  3024. intf->seq_table[j].inuse = 0;
  3025. intf->seq_table[j].seqid = 0;
  3026. }
  3027. intf->curr_seq = 0;
  3028. spin_lock_init(&intf->waiting_rcv_msgs_lock);
  3029. INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
  3030. tasklet_setup(&intf->recv_tasklet,
  3031. smi_recv_tasklet);
  3032. atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
  3033. spin_lock_init(&intf->xmit_msgs_lock);
  3034. INIT_LIST_HEAD(&intf->xmit_msgs);
  3035. INIT_LIST_HEAD(&intf->hp_xmit_msgs);
  3036. spin_lock_init(&intf->events_lock);
  3037. spin_lock_init(&intf->watch_lock);
  3038. atomic_set(&intf->event_waiters, 0);
  3039. intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
  3040. INIT_LIST_HEAD(&intf->waiting_events);
  3041. intf->waiting_events_count = 0;
  3042. mutex_init(&intf->cmd_rcvrs_mutex);
  3043. spin_lock_init(&intf->maintenance_mode_lock);
  3044. INIT_LIST_HEAD(&intf->cmd_rcvrs);
  3045. init_waitqueue_head(&intf->waitq);
  3046. for (i = 0; i < IPMI_NUM_STATS; i++)
  3047. atomic_set(&intf->stats[i], 0);
  3048. mutex_lock(&ipmi_interfaces_mutex);
  3049. /* Look for a hole in the numbers. */
  3050. i = 0;
  3051. link = &ipmi_interfaces;
  3052. list_for_each_entry_rcu(tintf, &ipmi_interfaces, link,
  3053. ipmi_interfaces_mutex_held()) {
  3054. if (tintf->intf_num != i) {
  3055. link = &tintf->link;
  3056. break;
  3057. }
  3058. i++;
  3059. }
  3060. /* Add the new interface in numeric order. */
  3061. if (i == 0)
  3062. list_add_rcu(&intf->link, &ipmi_interfaces);
  3063. else
  3064. list_add_tail_rcu(&intf->link, link);
  3065. rv = handlers->start_processing(send_info, intf);
  3066. if (rv)
  3067. goto out_err;
  3068. rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i);
  3069. if (rv) {
  3070. dev_err(si_dev, "Unable to get the device id: %d\n", rv);
  3071. goto out_err_started;
  3072. }
  3073. mutex_lock(&intf->bmc_reg_mutex);
  3074. rv = __scan_channels(intf, &id);
  3075. mutex_unlock(&intf->bmc_reg_mutex);
  3076. if (rv)
  3077. goto out_err_bmc_reg;
  3078. intf->nr_users_devattr = dev_attr_nr_users;
  3079. sysfs_attr_init(&intf->nr_users_devattr.attr);
  3080. rv = device_create_file(intf->si_dev, &intf->nr_users_devattr);
  3081. if (rv)
  3082. goto out_err_bmc_reg;
  3083. intf->nr_msgs_devattr = dev_attr_nr_msgs;
  3084. sysfs_attr_init(&intf->nr_msgs_devattr.attr);
  3085. rv = device_create_file(intf->si_dev, &intf->nr_msgs_devattr);
  3086. if (rv) {
  3087. device_remove_file(intf->si_dev, &intf->nr_users_devattr);
  3088. goto out_err_bmc_reg;
  3089. }
  3090. /*
  3091. * Keep memory order straight for RCU readers. Make
  3092. * sure everything else is committed to memory before
  3093. * setting intf_num to mark the interface valid.
  3094. */
  3095. smp_wmb();
  3096. intf->intf_num = i;
  3097. mutex_unlock(&ipmi_interfaces_mutex);
  3098. /* After this point the interface is legal to use. */
  3099. call_smi_watchers(i, intf->si_dev);
  3100. return 0;
  3101. out_err_bmc_reg:
  3102. ipmi_bmc_unregister(intf);
  3103. out_err_started:
  3104. if (intf->handlers->shutdown)
  3105. intf->handlers->shutdown(intf->send_info);
  3106. out_err:
  3107. list_del_rcu(&intf->link);
  3108. mutex_unlock(&ipmi_interfaces_mutex);
  3109. synchronize_srcu(&ipmi_interfaces_srcu);
  3110. cleanup_srcu_struct(&intf->users_srcu);
  3111. kref_put(&intf->refcount, intf_free);
  3112. return rv;
  3113. }
  3114. EXPORT_SYMBOL(ipmi_add_smi);
  3115. static void deliver_smi_err_response(struct ipmi_smi *intf,
  3116. struct ipmi_smi_msg *msg,
  3117. unsigned char err)
  3118. {
  3119. int rv;
  3120. msg->rsp[0] = msg->data[0] | 4;
  3121. msg->rsp[1] = msg->data[1];
  3122. msg->rsp[2] = err;
  3123. msg->rsp_size = 3;
  3124. /* This will never requeue, but it may ask us to free the message. */
  3125. rv = handle_one_recv_msg(intf, msg);
  3126. if (rv == 0)
  3127. ipmi_free_smi_msg(msg);
  3128. }
  3129. static void cleanup_smi_msgs(struct ipmi_smi *intf)
  3130. {
  3131. int i;
  3132. struct seq_table *ent;
  3133. struct ipmi_smi_msg *msg;
  3134. struct list_head *entry;
  3135. struct list_head tmplist;
  3136. /* Clear out our transmit queues and hold the messages. */
  3137. INIT_LIST_HEAD(&tmplist);
  3138. list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
  3139. list_splice_tail(&intf->xmit_msgs, &tmplist);
  3140. /* Current message first, to preserve order */
  3141. while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
  3142. /* Wait for the message to clear out. */
  3143. schedule_timeout(1);
  3144. }
  3145. /* No need for locks, the interface is down. */
  3146. /*
  3147. * Return errors for all pending messages in queue and in the
  3148. * tables waiting for remote responses.
  3149. */
  3150. while (!list_empty(&tmplist)) {
  3151. entry = tmplist.next;
  3152. list_del(entry);
  3153. msg = list_entry(entry, struct ipmi_smi_msg, link);
  3154. deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
  3155. }
  3156. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
  3157. ent = &intf->seq_table[i];
  3158. if (!ent->inuse)
  3159. continue;
  3160. deliver_err_response(intf, ent->recv_msg, IPMI_ERR_UNSPECIFIED);
  3161. }
  3162. }
  3163. void ipmi_unregister_smi(struct ipmi_smi *intf)
  3164. {
  3165. struct ipmi_smi_watcher *w;
  3166. int intf_num, index;
  3167. if (!intf)
  3168. return;
  3169. intf_num = intf->intf_num;
  3170. mutex_lock(&ipmi_interfaces_mutex);
  3171. intf->intf_num = -1;
  3172. intf->in_shutdown = true;
  3173. list_del_rcu(&intf->link);
  3174. mutex_unlock(&ipmi_interfaces_mutex);
  3175. synchronize_srcu(&ipmi_interfaces_srcu);
  3176. /* At this point no users can be added to the interface. */
  3177. device_remove_file(intf->si_dev, &intf->nr_msgs_devattr);
  3178. device_remove_file(intf->si_dev, &intf->nr_users_devattr);
  3179. /*
  3180. * Call all the watcher interfaces to tell them that
  3181. * an interface is going away.
  3182. */
  3183. mutex_lock(&smi_watchers_mutex);
  3184. list_for_each_entry(w, &smi_watchers, link)
  3185. w->smi_gone(intf_num);
  3186. mutex_unlock(&smi_watchers_mutex);
  3187. index = srcu_read_lock(&intf->users_srcu);
  3188. while (!list_empty(&intf->users)) {
  3189. struct ipmi_user *user =
  3190. container_of(list_next_rcu(&intf->users),
  3191. struct ipmi_user, link);
  3192. _ipmi_destroy_user(user);
  3193. }
  3194. srcu_read_unlock(&intf->users_srcu, index);
  3195. if (intf->handlers->shutdown)
  3196. intf->handlers->shutdown(intf->send_info);
  3197. cleanup_smi_msgs(intf);
  3198. ipmi_bmc_unregister(intf);
  3199. cleanup_srcu_struct(&intf->users_srcu);
  3200. kref_put(&intf->refcount, intf_free);
  3201. }
  3202. EXPORT_SYMBOL(ipmi_unregister_smi);
  3203. static int handle_ipmb_get_msg_rsp(struct ipmi_smi *intf,
  3204. struct ipmi_smi_msg *msg)
  3205. {
  3206. struct ipmi_ipmb_addr ipmb_addr;
  3207. struct ipmi_recv_msg *recv_msg;
  3208. /*
  3209. * This is 11, not 10, because the response must contain a
  3210. * completion code.
  3211. */
  3212. if (msg->rsp_size < 11) {
  3213. /* Message not big enough, just ignore it. */
  3214. ipmi_inc_stat(intf, invalid_ipmb_responses);
  3215. return 0;
  3216. }
  3217. if (msg->rsp[2] != 0) {
  3218. /* An error getting the response, just ignore it. */
  3219. return 0;
  3220. }
  3221. ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
  3222. ipmb_addr.slave_addr = msg->rsp[6];
  3223. ipmb_addr.channel = msg->rsp[3] & 0x0f;
  3224. ipmb_addr.lun = msg->rsp[7] & 3;
  3225. /*
  3226. * It's a response from a remote entity. Look up the sequence
  3227. * number and handle the response.
  3228. */
  3229. if (intf_find_seq(intf,
  3230. msg->rsp[7] >> 2,
  3231. msg->rsp[3] & 0x0f,
  3232. msg->rsp[8],
  3233. (msg->rsp[4] >> 2) & (~1),
  3234. (struct ipmi_addr *) &ipmb_addr,
  3235. &recv_msg)) {
  3236. /*
  3237. * We were unable to find the sequence number,
  3238. * so just nuke the message.
  3239. */
  3240. ipmi_inc_stat(intf, unhandled_ipmb_responses);
  3241. return 0;
  3242. }
  3243. memcpy(recv_msg->msg_data, &msg->rsp[9], msg->rsp_size - 9);
  3244. /*
  3245. * The other fields matched, so no need to set them, except
  3246. * for netfn, which needs to be the response that was
  3247. * returned, not the request value.
  3248. */
  3249. recv_msg->msg.netfn = msg->rsp[4] >> 2;
  3250. recv_msg->msg.data = recv_msg->msg_data;
  3251. recv_msg->msg.data_len = msg->rsp_size - 10;
  3252. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  3253. if (deliver_response(intf, recv_msg))
  3254. ipmi_inc_stat(intf, unhandled_ipmb_responses);
  3255. else
  3256. ipmi_inc_stat(intf, handled_ipmb_responses);
  3257. return 0;
  3258. }
  3259. static int handle_ipmb_get_msg_cmd(struct ipmi_smi *intf,
  3260. struct ipmi_smi_msg *msg)
  3261. {
  3262. struct cmd_rcvr *rcvr;
  3263. int rv = 0;
  3264. unsigned char netfn;
  3265. unsigned char cmd;
  3266. unsigned char chan;
  3267. struct ipmi_user *user = NULL;
  3268. struct ipmi_ipmb_addr *ipmb_addr;
  3269. struct ipmi_recv_msg *recv_msg;
  3270. if (msg->rsp_size < 10) {
  3271. /* Message not big enough, just ignore it. */
  3272. ipmi_inc_stat(intf, invalid_commands);
  3273. return 0;
  3274. }
  3275. if (msg->rsp[2] != 0) {
  3276. /* An error getting the response, just ignore it. */
  3277. return 0;
  3278. }
  3279. netfn = msg->rsp[4] >> 2;
  3280. cmd = msg->rsp[8];
  3281. chan = msg->rsp[3] & 0xf;
  3282. rcu_read_lock();
  3283. rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
  3284. if (rcvr) {
  3285. user = rcvr->user;
  3286. kref_get(&user->refcount);
  3287. } else
  3288. user = NULL;
  3289. rcu_read_unlock();
  3290. if (user == NULL) {
  3291. /* We didn't find a user, deliver an error response. */
  3292. ipmi_inc_stat(intf, unhandled_commands);
  3293. msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  3294. msg->data[1] = IPMI_SEND_MSG_CMD;
  3295. msg->data[2] = msg->rsp[3];
  3296. msg->data[3] = msg->rsp[6];
  3297. msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
  3298. msg->data[5] = ipmb_checksum(&msg->data[3], 2);
  3299. msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address;
  3300. /* rqseq/lun */
  3301. msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
  3302. msg->data[8] = msg->rsp[8]; /* cmd */
  3303. msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
  3304. msg->data[10] = ipmb_checksum(&msg->data[6], 4);
  3305. msg->data_size = 11;
  3306. dev_dbg(intf->si_dev, "Invalid command: %*ph\n",
  3307. msg->data_size, msg->data);
  3308. rcu_read_lock();
  3309. if (!intf->in_shutdown) {
  3310. smi_send(intf, intf->handlers, msg, 0);
  3311. /*
  3312. * We used the message, so return the value
  3313. * that causes it to not be freed or
  3314. * queued.
  3315. */
  3316. rv = -1;
  3317. }
  3318. rcu_read_unlock();
  3319. } else {
  3320. recv_msg = ipmi_alloc_recv_msg();
  3321. if (!recv_msg) {
  3322. /*
  3323. * We couldn't allocate memory for the
  3324. * message, so requeue it for handling
  3325. * later.
  3326. */
  3327. rv = 1;
  3328. kref_put(&user->refcount, free_user);
  3329. } else {
  3330. /* Extract the source address from the data. */
  3331. ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
  3332. ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
  3333. ipmb_addr->slave_addr = msg->rsp[6];
  3334. ipmb_addr->lun = msg->rsp[7] & 3;
  3335. ipmb_addr->channel = msg->rsp[3] & 0xf;
  3336. /*
  3337. * Extract the rest of the message information
  3338. * from the IPMB header.
  3339. */
  3340. recv_msg->user = user;
  3341. recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
  3342. recv_msg->msgid = msg->rsp[7] >> 2;
  3343. recv_msg->msg.netfn = msg->rsp[4] >> 2;
  3344. recv_msg->msg.cmd = msg->rsp[8];
  3345. recv_msg->msg.data = recv_msg->msg_data;
  3346. /*
  3347. * We chop off 10, not 9 bytes because the checksum
  3348. * at the end also needs to be removed.
  3349. */
  3350. recv_msg->msg.data_len = msg->rsp_size - 10;
  3351. memcpy(recv_msg->msg_data, &msg->rsp[9],
  3352. msg->rsp_size - 10);
  3353. if (deliver_response(intf, recv_msg))
  3354. ipmi_inc_stat(intf, unhandled_commands);
  3355. else
  3356. ipmi_inc_stat(intf, handled_commands);
  3357. }
  3358. }
  3359. return rv;
  3360. }
  3361. static int handle_ipmb_direct_rcv_cmd(struct ipmi_smi *intf,
  3362. struct ipmi_smi_msg *msg)
  3363. {
  3364. struct cmd_rcvr *rcvr;
  3365. int rv = 0;
  3366. struct ipmi_user *user = NULL;
  3367. struct ipmi_ipmb_direct_addr *daddr;
  3368. struct ipmi_recv_msg *recv_msg;
  3369. unsigned char netfn = msg->rsp[0] >> 2;
  3370. unsigned char cmd = msg->rsp[3];
  3371. rcu_read_lock();
  3372. /* We always use channel 0 for direct messages. */
  3373. rcvr = find_cmd_rcvr(intf, netfn, cmd, 0);
  3374. if (rcvr) {
  3375. user = rcvr->user;
  3376. kref_get(&user->refcount);
  3377. } else
  3378. user = NULL;
  3379. rcu_read_unlock();
  3380. if (user == NULL) {
  3381. /* We didn't find a user, deliver an error response. */
  3382. ipmi_inc_stat(intf, unhandled_commands);
  3383. msg->data[0] = (netfn + 1) << 2;
  3384. msg->data[0] |= msg->rsp[2] & 0x3; /* rqLUN */
  3385. msg->data[1] = msg->rsp[1]; /* Addr */
  3386. msg->data[2] = msg->rsp[2] & ~0x3; /* rqSeq */
  3387. msg->data[2] |= msg->rsp[0] & 0x3; /* rsLUN */
  3388. msg->data[3] = cmd;
  3389. msg->data[4] = IPMI_INVALID_CMD_COMPLETION_CODE;
  3390. msg->data_size = 5;
  3391. rcu_read_lock();
  3392. if (!intf->in_shutdown) {
  3393. smi_send(intf, intf->handlers, msg, 0);
  3394. /*
  3395. * We used the message, so return the value
  3396. * that causes it to not be freed or
  3397. * queued.
  3398. */
  3399. rv = -1;
  3400. }
  3401. rcu_read_unlock();
  3402. } else {
  3403. recv_msg = ipmi_alloc_recv_msg();
  3404. if (!recv_msg) {
  3405. /*
  3406. * We couldn't allocate memory for the
  3407. * message, so requeue it for handling
  3408. * later.
  3409. */
  3410. rv = 1;
  3411. kref_put(&user->refcount, free_user);
  3412. } else {
  3413. /* Extract the source address from the data. */
  3414. daddr = (struct ipmi_ipmb_direct_addr *)&recv_msg->addr;
  3415. daddr->addr_type = IPMI_IPMB_DIRECT_ADDR_TYPE;
  3416. daddr->channel = 0;
  3417. daddr->slave_addr = msg->rsp[1];
  3418. daddr->rs_lun = msg->rsp[0] & 3;
  3419. daddr->rq_lun = msg->rsp[2] & 3;
  3420. /*
  3421. * Extract the rest of the message information
  3422. * from the IPMB header.
  3423. */
  3424. recv_msg->user = user;
  3425. recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
  3426. recv_msg->msgid = (msg->rsp[2] >> 2);
  3427. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  3428. recv_msg->msg.cmd = msg->rsp[3];
  3429. recv_msg->msg.data = recv_msg->msg_data;
  3430. recv_msg->msg.data_len = msg->rsp_size - 4;
  3431. memcpy(recv_msg->msg_data, msg->rsp + 4,
  3432. msg->rsp_size - 4);
  3433. if (deliver_response(intf, recv_msg))
  3434. ipmi_inc_stat(intf, unhandled_commands);
  3435. else
  3436. ipmi_inc_stat(intf, handled_commands);
  3437. }
  3438. }
  3439. return rv;
  3440. }
  3441. static int handle_ipmb_direct_rcv_rsp(struct ipmi_smi *intf,
  3442. struct ipmi_smi_msg *msg)
  3443. {
  3444. struct ipmi_recv_msg *recv_msg;
  3445. struct ipmi_ipmb_direct_addr *daddr;
  3446. recv_msg = msg->user_data;
  3447. if (recv_msg == NULL) {
  3448. dev_warn(intf->si_dev,
  3449. "IPMI direct message received with no owner. This could be because of a malformed message, or because of a hardware error. Contact your hardware vendor for assistance.\n");
  3450. return 0;
  3451. }
  3452. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  3453. recv_msg->msgid = msg->msgid;
  3454. daddr = (struct ipmi_ipmb_direct_addr *) &recv_msg->addr;
  3455. daddr->addr_type = IPMI_IPMB_DIRECT_ADDR_TYPE;
  3456. daddr->channel = 0;
  3457. daddr->slave_addr = msg->rsp[1];
  3458. daddr->rq_lun = msg->rsp[0] & 3;
  3459. daddr->rs_lun = msg->rsp[2] & 3;
  3460. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  3461. recv_msg->msg.cmd = msg->rsp[3];
  3462. memcpy(recv_msg->msg_data, &msg->rsp[4], msg->rsp_size - 4);
  3463. recv_msg->msg.data = recv_msg->msg_data;
  3464. recv_msg->msg.data_len = msg->rsp_size - 4;
  3465. deliver_local_response(intf, recv_msg);
  3466. return 0;
  3467. }
  3468. static int handle_lan_get_msg_rsp(struct ipmi_smi *intf,
  3469. struct ipmi_smi_msg *msg)
  3470. {
  3471. struct ipmi_lan_addr lan_addr;
  3472. struct ipmi_recv_msg *recv_msg;
  3473. /*
  3474. * This is 13, not 12, because the response must contain a
  3475. * completion code.
  3476. */
  3477. if (msg->rsp_size < 13) {
  3478. /* Message not big enough, just ignore it. */
  3479. ipmi_inc_stat(intf, invalid_lan_responses);
  3480. return 0;
  3481. }
  3482. if (msg->rsp[2] != 0) {
  3483. /* An error getting the response, just ignore it. */
  3484. return 0;
  3485. }
  3486. lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
  3487. lan_addr.session_handle = msg->rsp[4];
  3488. lan_addr.remote_SWID = msg->rsp[8];
  3489. lan_addr.local_SWID = msg->rsp[5];
  3490. lan_addr.channel = msg->rsp[3] & 0x0f;
  3491. lan_addr.privilege = msg->rsp[3] >> 4;
  3492. lan_addr.lun = msg->rsp[9] & 3;
  3493. /*
  3494. * It's a response from a remote entity. Look up the sequence
  3495. * number and handle the response.
  3496. */
  3497. if (intf_find_seq(intf,
  3498. msg->rsp[9] >> 2,
  3499. msg->rsp[3] & 0x0f,
  3500. msg->rsp[10],
  3501. (msg->rsp[6] >> 2) & (~1),
  3502. (struct ipmi_addr *) &lan_addr,
  3503. &recv_msg)) {
  3504. /*
  3505. * We were unable to find the sequence number,
  3506. * so just nuke the message.
  3507. */
  3508. ipmi_inc_stat(intf, unhandled_lan_responses);
  3509. return 0;
  3510. }
  3511. memcpy(recv_msg->msg_data, &msg->rsp[11], msg->rsp_size - 11);
  3512. /*
  3513. * The other fields matched, so no need to set them, except
  3514. * for netfn, which needs to be the response that was
  3515. * returned, not the request value.
  3516. */
  3517. recv_msg->msg.netfn = msg->rsp[6] >> 2;
  3518. recv_msg->msg.data = recv_msg->msg_data;
  3519. recv_msg->msg.data_len = msg->rsp_size - 12;
  3520. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  3521. if (deliver_response(intf, recv_msg))
  3522. ipmi_inc_stat(intf, unhandled_lan_responses);
  3523. else
  3524. ipmi_inc_stat(intf, handled_lan_responses);
  3525. return 0;
  3526. }
  3527. static int handle_lan_get_msg_cmd(struct ipmi_smi *intf,
  3528. struct ipmi_smi_msg *msg)
  3529. {
  3530. struct cmd_rcvr *rcvr;
  3531. int rv = 0;
  3532. unsigned char netfn;
  3533. unsigned char cmd;
  3534. unsigned char chan;
  3535. struct ipmi_user *user = NULL;
  3536. struct ipmi_lan_addr *lan_addr;
  3537. struct ipmi_recv_msg *recv_msg;
  3538. if (msg->rsp_size < 12) {
  3539. /* Message not big enough, just ignore it. */
  3540. ipmi_inc_stat(intf, invalid_commands);
  3541. return 0;
  3542. }
  3543. if (msg->rsp[2] != 0) {
  3544. /* An error getting the response, just ignore it. */
  3545. return 0;
  3546. }
  3547. netfn = msg->rsp[6] >> 2;
  3548. cmd = msg->rsp[10];
  3549. chan = msg->rsp[3] & 0xf;
  3550. rcu_read_lock();
  3551. rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
  3552. if (rcvr) {
  3553. user = rcvr->user;
  3554. kref_get(&user->refcount);
  3555. } else
  3556. user = NULL;
  3557. rcu_read_unlock();
  3558. if (user == NULL) {
  3559. /* We didn't find a user, just give up. */
  3560. ipmi_inc_stat(intf, unhandled_commands);
  3561. /*
  3562. * Don't do anything with these messages, just allow
  3563. * them to be freed.
  3564. */
  3565. rv = 0;
  3566. } else {
  3567. recv_msg = ipmi_alloc_recv_msg();
  3568. if (!recv_msg) {
  3569. /*
  3570. * We couldn't allocate memory for the
  3571. * message, so requeue it for handling later.
  3572. */
  3573. rv = 1;
  3574. kref_put(&user->refcount, free_user);
  3575. } else {
  3576. /* Extract the source address from the data. */
  3577. lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
  3578. lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
  3579. lan_addr->session_handle = msg->rsp[4];
  3580. lan_addr->remote_SWID = msg->rsp[8];
  3581. lan_addr->local_SWID = msg->rsp[5];
  3582. lan_addr->lun = msg->rsp[9] & 3;
  3583. lan_addr->channel = msg->rsp[3] & 0xf;
  3584. lan_addr->privilege = msg->rsp[3] >> 4;
  3585. /*
  3586. * Extract the rest of the message information
  3587. * from the IPMB header.
  3588. */
  3589. recv_msg->user = user;
  3590. recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
  3591. recv_msg->msgid = msg->rsp[9] >> 2;
  3592. recv_msg->msg.netfn = msg->rsp[6] >> 2;
  3593. recv_msg->msg.cmd = msg->rsp[10];
  3594. recv_msg->msg.data = recv_msg->msg_data;
  3595. /*
  3596. * We chop off 12, not 11 bytes because the checksum
  3597. * at the end also needs to be removed.
  3598. */
  3599. recv_msg->msg.data_len = msg->rsp_size - 12;
  3600. memcpy(recv_msg->msg_data, &msg->rsp[11],
  3601. msg->rsp_size - 12);
  3602. if (deliver_response(intf, recv_msg))
  3603. ipmi_inc_stat(intf, unhandled_commands);
  3604. else
  3605. ipmi_inc_stat(intf, handled_commands);
  3606. }
  3607. }
  3608. return rv;
  3609. }
  3610. /*
  3611. * This routine will handle "Get Message" command responses with
  3612. * channels that use an OEM Medium. The message format belongs to
  3613. * the OEM. See IPMI 2.0 specification, Chapter 6 and
  3614. * Chapter 22, sections 22.6 and 22.24 for more details.
  3615. */
  3616. static int handle_oem_get_msg_cmd(struct ipmi_smi *intf,
  3617. struct ipmi_smi_msg *msg)
  3618. {
  3619. struct cmd_rcvr *rcvr;
  3620. int rv = 0;
  3621. unsigned char netfn;
  3622. unsigned char cmd;
  3623. unsigned char chan;
  3624. struct ipmi_user *user = NULL;
  3625. struct ipmi_system_interface_addr *smi_addr;
  3626. struct ipmi_recv_msg *recv_msg;
  3627. /*
  3628. * We expect the OEM SW to perform error checking
  3629. * so we just do some basic sanity checks
  3630. */
  3631. if (msg->rsp_size < 4) {
  3632. /* Message not big enough, just ignore it. */
  3633. ipmi_inc_stat(intf, invalid_commands);
  3634. return 0;
  3635. }
  3636. if (msg->rsp[2] != 0) {
  3637. /* An error getting the response, just ignore it. */
  3638. return 0;
  3639. }
  3640. /*
  3641. * This is an OEM Message so the OEM needs to know how
  3642. * handle the message. We do no interpretation.
  3643. */
  3644. netfn = msg->rsp[0] >> 2;
  3645. cmd = msg->rsp[1];
  3646. chan = msg->rsp[3] & 0xf;
  3647. rcu_read_lock();
  3648. rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
  3649. if (rcvr) {
  3650. user = rcvr->user;
  3651. kref_get(&user->refcount);
  3652. } else
  3653. user = NULL;
  3654. rcu_read_unlock();
  3655. if (user == NULL) {
  3656. /* We didn't find a user, just give up. */
  3657. ipmi_inc_stat(intf, unhandled_commands);
  3658. /*
  3659. * Don't do anything with these messages, just allow
  3660. * them to be freed.
  3661. */
  3662. rv = 0;
  3663. } else {
  3664. recv_msg = ipmi_alloc_recv_msg();
  3665. if (!recv_msg) {
  3666. /*
  3667. * We couldn't allocate memory for the
  3668. * message, so requeue it for handling
  3669. * later.
  3670. */
  3671. rv = 1;
  3672. kref_put(&user->refcount, free_user);
  3673. } else {
  3674. /*
  3675. * OEM Messages are expected to be delivered via
  3676. * the system interface to SMS software. We might
  3677. * need to visit this again depending on OEM
  3678. * requirements
  3679. */
  3680. smi_addr = ((struct ipmi_system_interface_addr *)
  3681. &recv_msg->addr);
  3682. smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  3683. smi_addr->channel = IPMI_BMC_CHANNEL;
  3684. smi_addr->lun = msg->rsp[0] & 3;
  3685. recv_msg->user = user;
  3686. recv_msg->user_msg_data = NULL;
  3687. recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
  3688. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  3689. recv_msg->msg.cmd = msg->rsp[1];
  3690. recv_msg->msg.data = recv_msg->msg_data;
  3691. /*
  3692. * The message starts at byte 4 which follows the
  3693. * Channel Byte in the "GET MESSAGE" command
  3694. */
  3695. recv_msg->msg.data_len = msg->rsp_size - 4;
  3696. memcpy(recv_msg->msg_data, &msg->rsp[4],
  3697. msg->rsp_size - 4);
  3698. if (deliver_response(intf, recv_msg))
  3699. ipmi_inc_stat(intf, unhandled_commands);
  3700. else
  3701. ipmi_inc_stat(intf, handled_commands);
  3702. }
  3703. }
  3704. return rv;
  3705. }
  3706. static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
  3707. struct ipmi_smi_msg *msg)
  3708. {
  3709. struct ipmi_system_interface_addr *smi_addr;
  3710. recv_msg->msgid = 0;
  3711. smi_addr = (struct ipmi_system_interface_addr *) &recv_msg->addr;
  3712. smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  3713. smi_addr->channel = IPMI_BMC_CHANNEL;
  3714. smi_addr->lun = msg->rsp[0] & 3;
  3715. recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
  3716. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  3717. recv_msg->msg.cmd = msg->rsp[1];
  3718. memcpy(recv_msg->msg_data, &msg->rsp[3], msg->rsp_size - 3);
  3719. recv_msg->msg.data = recv_msg->msg_data;
  3720. recv_msg->msg.data_len = msg->rsp_size - 3;
  3721. }
  3722. static int handle_read_event_rsp(struct ipmi_smi *intf,
  3723. struct ipmi_smi_msg *msg)
  3724. {
  3725. struct ipmi_recv_msg *recv_msg, *recv_msg2;
  3726. struct list_head msgs;
  3727. struct ipmi_user *user;
  3728. int rv = 0, deliver_count = 0, index;
  3729. unsigned long flags;
  3730. if (msg->rsp_size < 19) {
  3731. /* Message is too small to be an IPMB event. */
  3732. ipmi_inc_stat(intf, invalid_events);
  3733. return 0;
  3734. }
  3735. if (msg->rsp[2] != 0) {
  3736. /* An error getting the event, just ignore it. */
  3737. return 0;
  3738. }
  3739. INIT_LIST_HEAD(&msgs);
  3740. spin_lock_irqsave(&intf->events_lock, flags);
  3741. ipmi_inc_stat(intf, events);
  3742. /*
  3743. * Allocate and fill in one message for every user that is
  3744. * getting events.
  3745. */
  3746. index = srcu_read_lock(&intf->users_srcu);
  3747. list_for_each_entry_rcu(user, &intf->users, link) {
  3748. if (!user->gets_events)
  3749. continue;
  3750. recv_msg = ipmi_alloc_recv_msg();
  3751. if (!recv_msg) {
  3752. rcu_read_unlock();
  3753. list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
  3754. link) {
  3755. list_del(&recv_msg->link);
  3756. ipmi_free_recv_msg(recv_msg);
  3757. }
  3758. /*
  3759. * We couldn't allocate memory for the
  3760. * message, so requeue it for handling
  3761. * later.
  3762. */
  3763. rv = 1;
  3764. goto out;
  3765. }
  3766. deliver_count++;
  3767. copy_event_into_recv_msg(recv_msg, msg);
  3768. recv_msg->user = user;
  3769. kref_get(&user->refcount);
  3770. list_add_tail(&recv_msg->link, &msgs);
  3771. }
  3772. srcu_read_unlock(&intf->users_srcu, index);
  3773. if (deliver_count) {
  3774. /* Now deliver all the messages. */
  3775. list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
  3776. list_del(&recv_msg->link);
  3777. deliver_local_response(intf, recv_msg);
  3778. }
  3779. } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
  3780. /*
  3781. * No one to receive the message, put it in queue if there's
  3782. * not already too many things in the queue.
  3783. */
  3784. recv_msg = ipmi_alloc_recv_msg();
  3785. if (!recv_msg) {
  3786. /*
  3787. * We couldn't allocate memory for the
  3788. * message, so requeue it for handling
  3789. * later.
  3790. */
  3791. rv = 1;
  3792. goto out;
  3793. }
  3794. copy_event_into_recv_msg(recv_msg, msg);
  3795. list_add_tail(&recv_msg->link, &intf->waiting_events);
  3796. intf->waiting_events_count++;
  3797. } else if (!intf->event_msg_printed) {
  3798. /*
  3799. * There's too many things in the queue, discard this
  3800. * message.
  3801. */
  3802. dev_warn(intf->si_dev,
  3803. "Event queue full, discarding incoming events\n");
  3804. intf->event_msg_printed = 1;
  3805. }
  3806. out:
  3807. spin_unlock_irqrestore(&intf->events_lock, flags);
  3808. return rv;
  3809. }
  3810. static int handle_bmc_rsp(struct ipmi_smi *intf,
  3811. struct ipmi_smi_msg *msg)
  3812. {
  3813. struct ipmi_recv_msg *recv_msg;
  3814. struct ipmi_system_interface_addr *smi_addr;
  3815. recv_msg = msg->user_data;
  3816. if (recv_msg == NULL) {
  3817. dev_warn(intf->si_dev,
  3818. "IPMI SMI message received with no owner. This could be because of a malformed message, or because of a hardware error. Contact your hardware vendor for assistance.\n");
  3819. return 0;
  3820. }
  3821. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  3822. recv_msg->msgid = msg->msgid;
  3823. smi_addr = ((struct ipmi_system_interface_addr *)
  3824. &recv_msg->addr);
  3825. smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  3826. smi_addr->channel = IPMI_BMC_CHANNEL;
  3827. smi_addr->lun = msg->rsp[0] & 3;
  3828. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  3829. recv_msg->msg.cmd = msg->rsp[1];
  3830. memcpy(recv_msg->msg_data, &msg->rsp[2], msg->rsp_size - 2);
  3831. recv_msg->msg.data = recv_msg->msg_data;
  3832. recv_msg->msg.data_len = msg->rsp_size - 2;
  3833. deliver_local_response(intf, recv_msg);
  3834. return 0;
  3835. }
  3836. /*
  3837. * Handle a received message. Return 1 if the message should be requeued,
  3838. * 0 if the message should be freed, or -1 if the message should not
  3839. * be freed or requeued.
  3840. */
  3841. static int handle_one_recv_msg(struct ipmi_smi *intf,
  3842. struct ipmi_smi_msg *msg)
  3843. {
  3844. int requeue = 0;
  3845. int chan;
  3846. unsigned char cc;
  3847. bool is_cmd = !((msg->rsp[0] >> 2) & 1);
  3848. dev_dbg(intf->si_dev, "Recv: %*ph\n", msg->rsp_size, msg->rsp);
  3849. if (msg->rsp_size < 2) {
  3850. /* Message is too small to be correct. */
  3851. dev_warn(intf->si_dev,
  3852. "BMC returned too small a message for netfn %x cmd %x, got %d bytes\n",
  3853. (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
  3854. return_unspecified:
  3855. /* Generate an error response for the message. */
  3856. msg->rsp[0] = msg->data[0] | (1 << 2);
  3857. msg->rsp[1] = msg->data[1];
  3858. msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
  3859. msg->rsp_size = 3;
  3860. } else if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
  3861. /* commands must have at least 4 bytes, responses 5. */
  3862. if (is_cmd && (msg->rsp_size < 4)) {
  3863. ipmi_inc_stat(intf, invalid_commands);
  3864. goto out;
  3865. }
  3866. if (!is_cmd && (msg->rsp_size < 5)) {
  3867. ipmi_inc_stat(intf, invalid_ipmb_responses);
  3868. /* Construct a valid error response. */
  3869. msg->rsp[0] = msg->data[0] & 0xfc; /* NetFN */
  3870. msg->rsp[0] |= (1 << 2); /* Make it a response */
  3871. msg->rsp[0] |= msg->data[2] & 3; /* rqLUN */
  3872. msg->rsp[1] = msg->data[1]; /* Addr */
  3873. msg->rsp[2] = msg->data[2] & 0xfc; /* rqSeq */
  3874. msg->rsp[2] |= msg->data[0] & 0x3; /* rsLUN */
  3875. msg->rsp[3] = msg->data[3]; /* Cmd */
  3876. msg->rsp[4] = IPMI_ERR_UNSPECIFIED;
  3877. msg->rsp_size = 5;
  3878. }
  3879. } else if ((msg->data_size >= 2)
  3880. && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
  3881. && (msg->data[1] == IPMI_SEND_MSG_CMD)
  3882. && (msg->user_data == NULL)) {
  3883. if (intf->in_shutdown)
  3884. goto out;
  3885. /*
  3886. * This is the local response to a command send, start
  3887. * the timer for these. The user_data will not be
  3888. * NULL if this is a response send, and we will let
  3889. * response sends just go through.
  3890. */
  3891. /*
  3892. * Check for errors, if we get certain errors (ones
  3893. * that mean basically we can try again later), we
  3894. * ignore them and start the timer. Otherwise we
  3895. * report the error immediately.
  3896. */
  3897. if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
  3898. && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
  3899. && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
  3900. && (msg->rsp[2] != IPMI_BUS_ERR)
  3901. && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
  3902. int ch = msg->rsp[3] & 0xf;
  3903. struct ipmi_channel *chans;
  3904. /* Got an error sending the message, handle it. */
  3905. chans = READ_ONCE(intf->channel_list)->c;
  3906. if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN)
  3907. || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC))
  3908. ipmi_inc_stat(intf, sent_lan_command_errs);
  3909. else
  3910. ipmi_inc_stat(intf, sent_ipmb_command_errs);
  3911. intf_err_seq(intf, msg->msgid, msg->rsp[2]);
  3912. } else
  3913. /* The message was sent, start the timer. */
  3914. intf_start_seq_timer(intf, msg->msgid);
  3915. requeue = 0;
  3916. goto out;
  3917. } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
  3918. || (msg->rsp[1] != msg->data[1])) {
  3919. /*
  3920. * The NetFN and Command in the response is not even
  3921. * marginally correct.
  3922. */
  3923. dev_warn(intf->si_dev,
  3924. "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
  3925. (msg->data[0] >> 2) | 1, msg->data[1],
  3926. msg->rsp[0] >> 2, msg->rsp[1]);
  3927. goto return_unspecified;
  3928. }
  3929. if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
  3930. if ((msg->data[0] >> 2) & 1) {
  3931. /* It's a response to a sent response. */
  3932. chan = 0;
  3933. cc = msg->rsp[4];
  3934. goto process_response_response;
  3935. }
  3936. if (is_cmd)
  3937. requeue = handle_ipmb_direct_rcv_cmd(intf, msg);
  3938. else
  3939. requeue = handle_ipmb_direct_rcv_rsp(intf, msg);
  3940. } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
  3941. && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
  3942. && (msg->user_data != NULL)) {
  3943. /*
  3944. * It's a response to a response we sent. For this we
  3945. * deliver a send message response to the user.
  3946. */
  3947. struct ipmi_recv_msg *recv_msg;
  3948. chan = msg->data[2] & 0x0f;
  3949. if (chan >= IPMI_MAX_CHANNELS)
  3950. /* Invalid channel number */
  3951. goto out;
  3952. cc = msg->rsp[2];
  3953. process_response_response:
  3954. recv_msg = msg->user_data;
  3955. requeue = 0;
  3956. if (!recv_msg)
  3957. goto out;
  3958. recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
  3959. recv_msg->msg.data = recv_msg->msg_data;
  3960. recv_msg->msg_data[0] = cc;
  3961. recv_msg->msg.data_len = 1;
  3962. deliver_local_response(intf, recv_msg);
  3963. } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
  3964. && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
  3965. struct ipmi_channel *chans;
  3966. /* It's from the receive queue. */
  3967. chan = msg->rsp[3] & 0xf;
  3968. if (chan >= IPMI_MAX_CHANNELS) {
  3969. /* Invalid channel number */
  3970. requeue = 0;
  3971. goto out;
  3972. }
  3973. /*
  3974. * We need to make sure the channels have been initialized.
  3975. * The channel_handler routine will set the "curr_channel"
  3976. * equal to or greater than IPMI_MAX_CHANNELS when all the
  3977. * channels for this interface have been initialized.
  3978. */
  3979. if (!intf->channels_ready) {
  3980. requeue = 0; /* Throw the message away */
  3981. goto out;
  3982. }
  3983. chans = READ_ONCE(intf->channel_list)->c;
  3984. switch (chans[chan].medium) {
  3985. case IPMI_CHANNEL_MEDIUM_IPMB:
  3986. if (msg->rsp[4] & 0x04) {
  3987. /*
  3988. * It's a response, so find the
  3989. * requesting message and send it up.
  3990. */
  3991. requeue = handle_ipmb_get_msg_rsp(intf, msg);
  3992. } else {
  3993. /*
  3994. * It's a command to the SMS from some other
  3995. * entity. Handle that.
  3996. */
  3997. requeue = handle_ipmb_get_msg_cmd(intf, msg);
  3998. }
  3999. break;
  4000. case IPMI_CHANNEL_MEDIUM_8023LAN:
  4001. case IPMI_CHANNEL_MEDIUM_ASYNC:
  4002. if (msg->rsp[6] & 0x04) {
  4003. /*
  4004. * It's a response, so find the
  4005. * requesting message and send it up.
  4006. */
  4007. requeue = handle_lan_get_msg_rsp(intf, msg);
  4008. } else {
  4009. /*
  4010. * It's a command to the SMS from some other
  4011. * entity. Handle that.
  4012. */
  4013. requeue = handle_lan_get_msg_cmd(intf, msg);
  4014. }
  4015. break;
  4016. default:
  4017. /* Check for OEM Channels. Clients had better
  4018. register for these commands. */
  4019. if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
  4020. && (chans[chan].medium
  4021. <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
  4022. requeue = handle_oem_get_msg_cmd(intf, msg);
  4023. } else {
  4024. /*
  4025. * We don't handle the channel type, so just
  4026. * free the message.
  4027. */
  4028. requeue = 0;
  4029. }
  4030. }
  4031. } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
  4032. && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
  4033. /* It's an asynchronous event. */
  4034. requeue = handle_read_event_rsp(intf, msg);
  4035. } else {
  4036. /* It's a response from the local BMC. */
  4037. requeue = handle_bmc_rsp(intf, msg);
  4038. }
  4039. out:
  4040. return requeue;
  4041. }
  4042. /*
  4043. * If there are messages in the queue or pretimeouts, handle them.
  4044. */
  4045. static void handle_new_recv_msgs(struct ipmi_smi *intf)
  4046. {
  4047. struct ipmi_smi_msg *smi_msg;
  4048. unsigned long flags = 0;
  4049. int rv;
  4050. int run_to_completion = intf->run_to_completion;
  4051. /* See if any waiting messages need to be processed. */
  4052. if (!run_to_completion)
  4053. spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
  4054. while (!list_empty(&intf->waiting_rcv_msgs)) {
  4055. smi_msg = list_entry(intf->waiting_rcv_msgs.next,
  4056. struct ipmi_smi_msg, link);
  4057. list_del(&smi_msg->link);
  4058. if (!run_to_completion)
  4059. spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
  4060. flags);
  4061. rv = handle_one_recv_msg(intf, smi_msg);
  4062. if (!run_to_completion)
  4063. spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
  4064. if (rv > 0) {
  4065. /*
  4066. * To preserve message order, quit if we
  4067. * can't handle a message. Add the message
  4068. * back at the head, this is safe because this
  4069. * tasklet is the only thing that pulls the
  4070. * messages.
  4071. */
  4072. list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
  4073. break;
  4074. } else {
  4075. if (rv == 0)
  4076. /* Message handled */
  4077. ipmi_free_smi_msg(smi_msg);
  4078. /* If rv < 0, fatal error, del but don't free. */
  4079. }
  4080. }
  4081. if (!run_to_completion)
  4082. spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
  4083. /*
  4084. * If the pretimout count is non-zero, decrement one from it and
  4085. * deliver pretimeouts to all the users.
  4086. */
  4087. if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
  4088. struct ipmi_user *user;
  4089. int index;
  4090. index = srcu_read_lock(&intf->users_srcu);
  4091. list_for_each_entry_rcu(user, &intf->users, link) {
  4092. if (user->handler->ipmi_watchdog_pretimeout)
  4093. user->handler->ipmi_watchdog_pretimeout(
  4094. user->handler_data);
  4095. }
  4096. srcu_read_unlock(&intf->users_srcu, index);
  4097. }
  4098. }
  4099. static void smi_recv_tasklet(struct tasklet_struct *t)
  4100. {
  4101. unsigned long flags = 0; /* keep us warning-free. */
  4102. struct ipmi_smi *intf = from_tasklet(intf, t, recv_tasklet);
  4103. int run_to_completion = intf->run_to_completion;
  4104. struct ipmi_smi_msg *newmsg = NULL;
  4105. /*
  4106. * Start the next message if available.
  4107. *
  4108. * Do this here, not in the actual receiver, because we may deadlock
  4109. * because the lower layer is allowed to hold locks while calling
  4110. * message delivery.
  4111. */
  4112. rcu_read_lock();
  4113. if (!run_to_completion)
  4114. spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
  4115. if (intf->curr_msg == NULL && !intf->in_shutdown) {
  4116. struct list_head *entry = NULL;
  4117. /* Pick the high priority queue first. */
  4118. if (!list_empty(&intf->hp_xmit_msgs))
  4119. entry = intf->hp_xmit_msgs.next;
  4120. else if (!list_empty(&intf->xmit_msgs))
  4121. entry = intf->xmit_msgs.next;
  4122. if (entry) {
  4123. list_del(entry);
  4124. newmsg = list_entry(entry, struct ipmi_smi_msg, link);
  4125. intf->curr_msg = newmsg;
  4126. }
  4127. }
  4128. if (!run_to_completion)
  4129. spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
  4130. if (newmsg)
  4131. intf->handlers->sender(intf->send_info, newmsg);
  4132. rcu_read_unlock();
  4133. handle_new_recv_msgs(intf);
  4134. }
  4135. /* Handle a new message from the lower layer. */
  4136. void ipmi_smi_msg_received(struct ipmi_smi *intf,
  4137. struct ipmi_smi_msg *msg)
  4138. {
  4139. unsigned long flags = 0; /* keep us warning-free. */
  4140. int run_to_completion = intf->run_to_completion;
  4141. /*
  4142. * To preserve message order, we keep a queue and deliver from
  4143. * a tasklet.
  4144. */
  4145. if (!run_to_completion)
  4146. spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
  4147. list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
  4148. if (!run_to_completion)
  4149. spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
  4150. flags);
  4151. if (!run_to_completion)
  4152. spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
  4153. /*
  4154. * We can get an asynchronous event or receive message in addition
  4155. * to commands we send.
  4156. */
  4157. if (msg == intf->curr_msg)
  4158. intf->curr_msg = NULL;
  4159. if (!run_to_completion)
  4160. spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
  4161. if (run_to_completion)
  4162. smi_recv_tasklet(&intf->recv_tasklet);
  4163. else
  4164. tasklet_schedule(&intf->recv_tasklet);
  4165. }
  4166. EXPORT_SYMBOL(ipmi_smi_msg_received);
  4167. void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf)
  4168. {
  4169. if (intf->in_shutdown)
  4170. return;
  4171. atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
  4172. tasklet_schedule(&intf->recv_tasklet);
  4173. }
  4174. EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
  4175. static struct ipmi_smi_msg *
  4176. smi_from_recv_msg(struct ipmi_smi *intf, struct ipmi_recv_msg *recv_msg,
  4177. unsigned char seq, long seqid)
  4178. {
  4179. struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
  4180. if (!smi_msg)
  4181. /*
  4182. * If we can't allocate the message, then just return, we
  4183. * get 4 retries, so this should be ok.
  4184. */
  4185. return NULL;
  4186. memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
  4187. smi_msg->data_size = recv_msg->msg.data_len;
  4188. smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
  4189. dev_dbg(intf->si_dev, "Resend: %*ph\n",
  4190. smi_msg->data_size, smi_msg->data);
  4191. return smi_msg;
  4192. }
  4193. static void check_msg_timeout(struct ipmi_smi *intf, struct seq_table *ent,
  4194. struct list_head *timeouts,
  4195. unsigned long timeout_period,
  4196. int slot, unsigned long *flags,
  4197. bool *need_timer)
  4198. {
  4199. struct ipmi_recv_msg *msg;
  4200. if (intf->in_shutdown)
  4201. return;
  4202. if (!ent->inuse)
  4203. return;
  4204. if (timeout_period < ent->timeout) {
  4205. ent->timeout -= timeout_period;
  4206. *need_timer = true;
  4207. return;
  4208. }
  4209. if (ent->retries_left == 0) {
  4210. /* The message has used all its retries. */
  4211. ent->inuse = 0;
  4212. smi_remove_watch(intf, IPMI_WATCH_MASK_CHECK_MESSAGES);
  4213. msg = ent->recv_msg;
  4214. list_add_tail(&msg->link, timeouts);
  4215. if (ent->broadcast)
  4216. ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
  4217. else if (is_lan_addr(&ent->recv_msg->addr))
  4218. ipmi_inc_stat(intf, timed_out_lan_commands);
  4219. else
  4220. ipmi_inc_stat(intf, timed_out_ipmb_commands);
  4221. } else {
  4222. struct ipmi_smi_msg *smi_msg;
  4223. /* More retries, send again. */
  4224. *need_timer = true;
  4225. /*
  4226. * Start with the max timer, set to normal timer after
  4227. * the message is sent.
  4228. */
  4229. ent->timeout = MAX_MSG_TIMEOUT;
  4230. ent->retries_left--;
  4231. smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
  4232. ent->seqid);
  4233. if (!smi_msg) {
  4234. if (is_lan_addr(&ent->recv_msg->addr))
  4235. ipmi_inc_stat(intf,
  4236. dropped_rexmit_lan_commands);
  4237. else
  4238. ipmi_inc_stat(intf,
  4239. dropped_rexmit_ipmb_commands);
  4240. return;
  4241. }
  4242. spin_unlock_irqrestore(&intf->seq_lock, *flags);
  4243. /*
  4244. * Send the new message. We send with a zero
  4245. * priority. It timed out, I doubt time is that
  4246. * critical now, and high priority messages are really
  4247. * only for messages to the local MC, which don't get
  4248. * resent.
  4249. */
  4250. if (intf->handlers) {
  4251. if (is_lan_addr(&ent->recv_msg->addr))
  4252. ipmi_inc_stat(intf,
  4253. retransmitted_lan_commands);
  4254. else
  4255. ipmi_inc_stat(intf,
  4256. retransmitted_ipmb_commands);
  4257. smi_send(intf, intf->handlers, smi_msg, 0);
  4258. } else
  4259. ipmi_free_smi_msg(smi_msg);
  4260. spin_lock_irqsave(&intf->seq_lock, *flags);
  4261. }
  4262. }
  4263. static bool ipmi_timeout_handler(struct ipmi_smi *intf,
  4264. unsigned long timeout_period)
  4265. {
  4266. struct list_head timeouts;
  4267. struct ipmi_recv_msg *msg, *msg2;
  4268. unsigned long flags;
  4269. int i;
  4270. bool need_timer = false;
  4271. if (!intf->bmc_registered) {
  4272. kref_get(&intf->refcount);
  4273. if (!schedule_work(&intf->bmc_reg_work)) {
  4274. kref_put(&intf->refcount, intf_free);
  4275. need_timer = true;
  4276. }
  4277. }
  4278. /*
  4279. * Go through the seq table and find any messages that
  4280. * have timed out, putting them in the timeouts
  4281. * list.
  4282. */
  4283. INIT_LIST_HEAD(&timeouts);
  4284. spin_lock_irqsave(&intf->seq_lock, flags);
  4285. if (intf->ipmb_maintenance_mode_timeout) {
  4286. if (intf->ipmb_maintenance_mode_timeout <= timeout_period)
  4287. intf->ipmb_maintenance_mode_timeout = 0;
  4288. else
  4289. intf->ipmb_maintenance_mode_timeout -= timeout_period;
  4290. }
  4291. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
  4292. check_msg_timeout(intf, &intf->seq_table[i],
  4293. &timeouts, timeout_period, i,
  4294. &flags, &need_timer);
  4295. spin_unlock_irqrestore(&intf->seq_lock, flags);
  4296. list_for_each_entry_safe(msg, msg2, &timeouts, link)
  4297. deliver_err_response(intf, msg, IPMI_TIMEOUT_COMPLETION_CODE);
  4298. /*
  4299. * Maintenance mode handling. Check the timeout
  4300. * optimistically before we claim the lock. It may
  4301. * mean a timeout gets missed occasionally, but that
  4302. * only means the timeout gets extended by one period
  4303. * in that case. No big deal, and it avoids the lock
  4304. * most of the time.
  4305. */
  4306. if (intf->auto_maintenance_timeout > 0) {
  4307. spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
  4308. if (intf->auto_maintenance_timeout > 0) {
  4309. intf->auto_maintenance_timeout
  4310. -= timeout_period;
  4311. if (!intf->maintenance_mode
  4312. && (intf->auto_maintenance_timeout <= 0)) {
  4313. intf->maintenance_mode_enable = false;
  4314. maintenance_mode_update(intf);
  4315. }
  4316. }
  4317. spin_unlock_irqrestore(&intf->maintenance_mode_lock,
  4318. flags);
  4319. }
  4320. tasklet_schedule(&intf->recv_tasklet);
  4321. return need_timer;
  4322. }
  4323. static void ipmi_request_event(struct ipmi_smi *intf)
  4324. {
  4325. /* No event requests when in maintenance mode. */
  4326. if (intf->maintenance_mode_enable)
  4327. return;
  4328. if (!intf->in_shutdown)
  4329. intf->handlers->request_events(intf->send_info);
  4330. }
  4331. static struct timer_list ipmi_timer;
  4332. static atomic_t stop_operation;
  4333. static void ipmi_timeout(struct timer_list *unused)
  4334. {
  4335. struct ipmi_smi *intf;
  4336. bool need_timer = false;
  4337. int index;
  4338. if (atomic_read(&stop_operation))
  4339. return;
  4340. index = srcu_read_lock(&ipmi_interfaces_srcu);
  4341. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  4342. if (atomic_read(&intf->event_waiters)) {
  4343. intf->ticks_to_req_ev--;
  4344. if (intf->ticks_to_req_ev == 0) {
  4345. ipmi_request_event(intf);
  4346. intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
  4347. }
  4348. need_timer = true;
  4349. }
  4350. need_timer |= ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
  4351. }
  4352. srcu_read_unlock(&ipmi_interfaces_srcu, index);
  4353. if (need_timer)
  4354. mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
  4355. }
  4356. static void need_waiter(struct ipmi_smi *intf)
  4357. {
  4358. /* Racy, but worst case we start the timer twice. */
  4359. if (!timer_pending(&ipmi_timer))
  4360. mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
  4361. }
  4362. static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
  4363. static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
  4364. static void free_smi_msg(struct ipmi_smi_msg *msg)
  4365. {
  4366. atomic_dec(&smi_msg_inuse_count);
  4367. /* Try to keep as much stuff out of the panic path as possible. */
  4368. if (!oops_in_progress)
  4369. kfree(msg);
  4370. }
  4371. struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
  4372. {
  4373. struct ipmi_smi_msg *rv;
  4374. rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
  4375. if (rv) {
  4376. rv->done = free_smi_msg;
  4377. rv->user_data = NULL;
  4378. rv->type = IPMI_SMI_MSG_TYPE_NORMAL;
  4379. atomic_inc(&smi_msg_inuse_count);
  4380. }
  4381. return rv;
  4382. }
  4383. EXPORT_SYMBOL(ipmi_alloc_smi_msg);
  4384. static void free_recv_msg(struct ipmi_recv_msg *msg)
  4385. {
  4386. atomic_dec(&recv_msg_inuse_count);
  4387. /* Try to keep as much stuff out of the panic path as possible. */
  4388. if (!oops_in_progress)
  4389. kfree(msg);
  4390. }
  4391. static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
  4392. {
  4393. struct ipmi_recv_msg *rv;
  4394. rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
  4395. if (rv) {
  4396. rv->user = NULL;
  4397. rv->done = free_recv_msg;
  4398. atomic_inc(&recv_msg_inuse_count);
  4399. }
  4400. return rv;
  4401. }
  4402. void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
  4403. {
  4404. if (msg->user && !oops_in_progress)
  4405. kref_put(&msg->user->refcount, free_user);
  4406. msg->done(msg);
  4407. }
  4408. EXPORT_SYMBOL(ipmi_free_recv_msg);
  4409. static atomic_t panic_done_count = ATOMIC_INIT(0);
  4410. static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
  4411. {
  4412. atomic_dec(&panic_done_count);
  4413. }
  4414. static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
  4415. {
  4416. atomic_dec(&panic_done_count);
  4417. }
  4418. /*
  4419. * Inside a panic, send a message and wait for a response.
  4420. */
  4421. static void ipmi_panic_request_and_wait(struct ipmi_smi *intf,
  4422. struct ipmi_addr *addr,
  4423. struct kernel_ipmi_msg *msg)
  4424. {
  4425. struct ipmi_smi_msg smi_msg;
  4426. struct ipmi_recv_msg recv_msg;
  4427. int rv;
  4428. smi_msg.done = dummy_smi_done_handler;
  4429. recv_msg.done = dummy_recv_done_handler;
  4430. atomic_add(2, &panic_done_count);
  4431. rv = i_ipmi_request(NULL,
  4432. intf,
  4433. addr,
  4434. 0,
  4435. msg,
  4436. intf,
  4437. &smi_msg,
  4438. &recv_msg,
  4439. 0,
  4440. intf->addrinfo[0].address,
  4441. intf->addrinfo[0].lun,
  4442. 0, 1); /* Don't retry, and don't wait. */
  4443. if (rv)
  4444. atomic_sub(2, &panic_done_count);
  4445. else if (intf->handlers->flush_messages)
  4446. intf->handlers->flush_messages(intf->send_info);
  4447. while (atomic_read(&panic_done_count) != 0)
  4448. ipmi_poll(intf);
  4449. }
  4450. static void event_receiver_fetcher(struct ipmi_smi *intf,
  4451. struct ipmi_recv_msg *msg)
  4452. {
  4453. if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  4454. && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
  4455. && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
  4456. && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
  4457. /* A get event receiver command, save it. */
  4458. intf->event_receiver = msg->msg.data[1];
  4459. intf->event_receiver_lun = msg->msg.data[2] & 0x3;
  4460. }
  4461. }
  4462. static void device_id_fetcher(struct ipmi_smi *intf, struct ipmi_recv_msg *msg)
  4463. {
  4464. if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  4465. && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
  4466. && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
  4467. && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
  4468. /*
  4469. * A get device id command, save if we are an event
  4470. * receiver or generator.
  4471. */
  4472. intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
  4473. intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
  4474. }
  4475. }
  4476. static void send_panic_events(struct ipmi_smi *intf, char *str)
  4477. {
  4478. struct kernel_ipmi_msg msg;
  4479. unsigned char data[16];
  4480. struct ipmi_system_interface_addr *si;
  4481. struct ipmi_addr addr;
  4482. char *p = str;
  4483. struct ipmi_ipmb_addr *ipmb;
  4484. int j;
  4485. if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE)
  4486. return;
  4487. si = (struct ipmi_system_interface_addr *) &addr;
  4488. si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  4489. si->channel = IPMI_BMC_CHANNEL;
  4490. si->lun = 0;
  4491. /* Fill in an event telling that we have failed. */
  4492. msg.netfn = 0x04; /* Sensor or Event. */
  4493. msg.cmd = 2; /* Platform event command. */
  4494. msg.data = data;
  4495. msg.data_len = 8;
  4496. data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
  4497. data[1] = 0x03; /* This is for IPMI 1.0. */
  4498. data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
  4499. data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
  4500. data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
  4501. /*
  4502. * Put a few breadcrumbs in. Hopefully later we can add more things
  4503. * to make the panic events more useful.
  4504. */
  4505. if (str) {
  4506. data[3] = str[0];
  4507. data[6] = str[1];
  4508. data[7] = str[2];
  4509. }
  4510. /* Send the event announcing the panic. */
  4511. ipmi_panic_request_and_wait(intf, &addr, &msg);
  4512. /*
  4513. * On every interface, dump a bunch of OEM event holding the
  4514. * string.
  4515. */
  4516. if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str)
  4517. return;
  4518. /*
  4519. * intf_num is used as an marker to tell if the
  4520. * interface is valid. Thus we need a read barrier to
  4521. * make sure data fetched before checking intf_num
  4522. * won't be used.
  4523. */
  4524. smp_rmb();
  4525. /*
  4526. * First job here is to figure out where to send the
  4527. * OEM events. There's no way in IPMI to send OEM
  4528. * events using an event send command, so we have to
  4529. * find the SEL to put them in and stick them in
  4530. * there.
  4531. */
  4532. /* Get capabilities from the get device id. */
  4533. intf->local_sel_device = 0;
  4534. intf->local_event_generator = 0;
  4535. intf->event_receiver = 0;
  4536. /* Request the device info from the local MC. */
  4537. msg.netfn = IPMI_NETFN_APP_REQUEST;
  4538. msg.cmd = IPMI_GET_DEVICE_ID_CMD;
  4539. msg.data = NULL;
  4540. msg.data_len = 0;
  4541. intf->null_user_handler = device_id_fetcher;
  4542. ipmi_panic_request_and_wait(intf, &addr, &msg);
  4543. if (intf->local_event_generator) {
  4544. /* Request the event receiver from the local MC. */
  4545. msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
  4546. msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
  4547. msg.data = NULL;
  4548. msg.data_len = 0;
  4549. intf->null_user_handler = event_receiver_fetcher;
  4550. ipmi_panic_request_and_wait(intf, &addr, &msg);
  4551. }
  4552. intf->null_user_handler = NULL;
  4553. /*
  4554. * Validate the event receiver. The low bit must not
  4555. * be 1 (it must be a valid IPMB address), it cannot
  4556. * be zero, and it must not be my address.
  4557. */
  4558. if (((intf->event_receiver & 1) == 0)
  4559. && (intf->event_receiver != 0)
  4560. && (intf->event_receiver != intf->addrinfo[0].address)) {
  4561. /*
  4562. * The event receiver is valid, send an IPMB
  4563. * message.
  4564. */
  4565. ipmb = (struct ipmi_ipmb_addr *) &addr;
  4566. ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
  4567. ipmb->channel = 0; /* FIXME - is this right? */
  4568. ipmb->lun = intf->event_receiver_lun;
  4569. ipmb->slave_addr = intf->event_receiver;
  4570. } else if (intf->local_sel_device) {
  4571. /*
  4572. * The event receiver was not valid (or was
  4573. * me), but I am an SEL device, just dump it
  4574. * in my SEL.
  4575. */
  4576. si = (struct ipmi_system_interface_addr *) &addr;
  4577. si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  4578. si->channel = IPMI_BMC_CHANNEL;
  4579. si->lun = 0;
  4580. } else
  4581. return; /* No where to send the event. */
  4582. msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
  4583. msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
  4584. msg.data = data;
  4585. msg.data_len = 16;
  4586. j = 0;
  4587. while (*p) {
  4588. int size = strlen(p);
  4589. if (size > 11)
  4590. size = 11;
  4591. data[0] = 0;
  4592. data[1] = 0;
  4593. data[2] = 0xf0; /* OEM event without timestamp. */
  4594. data[3] = intf->addrinfo[0].address;
  4595. data[4] = j++; /* sequence # */
  4596. /*
  4597. * Always give 11 bytes, so strncpy will fill
  4598. * it with zeroes for me.
  4599. */
  4600. strncpy(data+5, p, 11);
  4601. p += size;
  4602. ipmi_panic_request_and_wait(intf, &addr, &msg);
  4603. }
  4604. }
  4605. static int has_panicked;
  4606. static int panic_event(struct notifier_block *this,
  4607. unsigned long event,
  4608. void *ptr)
  4609. {
  4610. struct ipmi_smi *intf;
  4611. struct ipmi_user *user;
  4612. if (has_panicked)
  4613. return NOTIFY_DONE;
  4614. has_panicked = 1;
  4615. /* For every registered interface, set it to run to completion. */
  4616. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  4617. if (!intf->handlers || intf->intf_num == -1)
  4618. /* Interface is not ready. */
  4619. continue;
  4620. if (!intf->handlers->poll)
  4621. continue;
  4622. /*
  4623. * If we were interrupted while locking xmit_msgs_lock or
  4624. * waiting_rcv_msgs_lock, the corresponding list may be
  4625. * corrupted. In this case, drop items on the list for
  4626. * the safety.
  4627. */
  4628. if (!spin_trylock(&intf->xmit_msgs_lock)) {
  4629. INIT_LIST_HEAD(&intf->xmit_msgs);
  4630. INIT_LIST_HEAD(&intf->hp_xmit_msgs);
  4631. } else
  4632. spin_unlock(&intf->xmit_msgs_lock);
  4633. if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
  4634. INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
  4635. else
  4636. spin_unlock(&intf->waiting_rcv_msgs_lock);
  4637. intf->run_to_completion = 1;
  4638. if (intf->handlers->set_run_to_completion)
  4639. intf->handlers->set_run_to_completion(intf->send_info,
  4640. 1);
  4641. list_for_each_entry_rcu(user, &intf->users, link) {
  4642. if (user->handler->ipmi_panic_handler)
  4643. user->handler->ipmi_panic_handler(
  4644. user->handler_data);
  4645. }
  4646. send_panic_events(intf, ptr);
  4647. }
  4648. return NOTIFY_DONE;
  4649. }
  4650. /* Must be called with ipmi_interfaces_mutex held. */
  4651. static int ipmi_register_driver(void)
  4652. {
  4653. int rv;
  4654. if (drvregistered)
  4655. return 0;
  4656. rv = driver_register(&ipmidriver.driver);
  4657. if (rv)
  4658. pr_err("Could not register IPMI driver\n");
  4659. else
  4660. drvregistered = true;
  4661. return rv;
  4662. }
  4663. static struct notifier_block panic_block = {
  4664. .notifier_call = panic_event,
  4665. .next = NULL,
  4666. .priority = 200 /* priority: INT_MAX >= x >= 0 */
  4667. };
  4668. static int ipmi_init_msghandler(void)
  4669. {
  4670. int rv;
  4671. mutex_lock(&ipmi_interfaces_mutex);
  4672. rv = ipmi_register_driver();
  4673. if (rv)
  4674. goto out;
  4675. if (initialized)
  4676. goto out;
  4677. rv = init_srcu_struct(&ipmi_interfaces_srcu);
  4678. if (rv)
  4679. goto out;
  4680. remove_work_wq = create_singlethread_workqueue("ipmi-msghandler-remove-wq");
  4681. if (!remove_work_wq) {
  4682. pr_err("unable to create ipmi-msghandler-remove-wq workqueue");
  4683. rv = -ENOMEM;
  4684. goto out_wq;
  4685. }
  4686. timer_setup(&ipmi_timer, ipmi_timeout, 0);
  4687. mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
  4688. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  4689. initialized = true;
  4690. out_wq:
  4691. if (rv)
  4692. cleanup_srcu_struct(&ipmi_interfaces_srcu);
  4693. out:
  4694. mutex_unlock(&ipmi_interfaces_mutex);
  4695. return rv;
  4696. }
  4697. static int __init ipmi_init_msghandler_mod(void)
  4698. {
  4699. int rv;
  4700. pr_info("version " IPMI_DRIVER_VERSION "\n");
  4701. mutex_lock(&ipmi_interfaces_mutex);
  4702. rv = ipmi_register_driver();
  4703. mutex_unlock(&ipmi_interfaces_mutex);
  4704. return rv;
  4705. }
  4706. static void __exit cleanup_ipmi(void)
  4707. {
  4708. int count;
  4709. if (initialized) {
  4710. destroy_workqueue(remove_work_wq);
  4711. atomic_notifier_chain_unregister(&panic_notifier_list,
  4712. &panic_block);
  4713. /*
  4714. * This can't be called if any interfaces exist, so no worry
  4715. * about shutting down the interfaces.
  4716. */
  4717. /*
  4718. * Tell the timer to stop, then wait for it to stop. This
  4719. * avoids problems with race conditions removing the timer
  4720. * here.
  4721. */
  4722. atomic_set(&stop_operation, 1);
  4723. del_timer_sync(&ipmi_timer);
  4724. initialized = false;
  4725. /* Check for buffer leaks. */
  4726. count = atomic_read(&smi_msg_inuse_count);
  4727. if (count != 0)
  4728. pr_warn("SMI message count %d at exit\n", count);
  4729. count = atomic_read(&recv_msg_inuse_count);
  4730. if (count != 0)
  4731. pr_warn("recv message count %d at exit\n", count);
  4732. cleanup_srcu_struct(&ipmi_interfaces_srcu);
  4733. }
  4734. if (drvregistered)
  4735. driver_unregister(&ipmidriver.driver);
  4736. }
  4737. module_exit(cleanup_ipmi);
  4738. module_init(ipmi_init_msghandler_mod);
  4739. MODULE_LICENSE("GPL");
  4740. MODULE_AUTHOR("Corey Minyard <[email protected]>");
  4741. MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI interface.");
  4742. MODULE_VERSION(IPMI_DRIVER_VERSION);
  4743. MODULE_SOFTDEP("post: ipmi_devintf");