ipmi_msghandler.c 128 KB

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