ipmi_msghandler.c 141 KB

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