ipmi_msghandler.c 128 KB

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