ipmi_msghandler.c 113 KB

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