ipmi_msghandler.c 118 KB

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