ipmi_msghandler.c 129 KB

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