ipmi_msghandler.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498
  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. sysfs_attr_init(&bmc->device_id_attr.attr);
  1923. bmc->provides_dev_sdrs_attr.attr.name = "provides_device_sdrs";
  1924. bmc->provides_dev_sdrs_attr.attr.mode = S_IRUGO;
  1925. bmc->provides_dev_sdrs_attr.show = provides_dev_sdrs_show;
  1926. sysfs_attr_init(&bmc->provides_dev_sdrs_attr.attr);
  1927. bmc->revision_attr.attr.name = "revision";
  1928. bmc->revision_attr.attr.mode = S_IRUGO;
  1929. bmc->revision_attr.show = revision_show;
  1930. sysfs_attr_init(&bmc->revision_attr.attr);
  1931. bmc->firmware_rev_attr.attr.name = "firmware_revision";
  1932. bmc->firmware_rev_attr.attr.mode = S_IRUGO;
  1933. bmc->firmware_rev_attr.show = firmware_rev_show;
  1934. sysfs_attr_init(&bmc->firmware_rev_attr.attr);
  1935. bmc->version_attr.attr.name = "ipmi_version";
  1936. bmc->version_attr.attr.mode = S_IRUGO;
  1937. bmc->version_attr.show = ipmi_version_show;
  1938. sysfs_attr_init(&bmc->version_attr.attr);
  1939. bmc->add_dev_support_attr.attr.name = "additional_device_support";
  1940. bmc->add_dev_support_attr.attr.mode = S_IRUGO;
  1941. bmc->add_dev_support_attr.show = add_dev_support_show;
  1942. sysfs_attr_init(&bmc->add_dev_support_attr.attr);
  1943. bmc->manufacturer_id_attr.attr.name = "manufacturer_id";
  1944. bmc->manufacturer_id_attr.attr.mode = S_IRUGO;
  1945. bmc->manufacturer_id_attr.show = manufacturer_id_show;
  1946. sysfs_attr_init(&bmc->manufacturer_id_attr.attr);
  1947. bmc->product_id_attr.attr.name = "product_id";
  1948. bmc->product_id_attr.attr.mode = S_IRUGO;
  1949. bmc->product_id_attr.show = product_id_show;
  1950. sysfs_attr_init(&bmc->product_id_attr.attr);
  1951. bmc->guid_attr.attr.name = "guid";
  1952. bmc->guid_attr.attr.mode = S_IRUGO;
  1953. bmc->guid_attr.show = guid_show;
  1954. sysfs_attr_init(&bmc->guid_attr.attr);
  1955. bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
  1956. bmc->aux_firmware_rev_attr.attr.mode = S_IRUGO;
  1957. bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show;
  1958. sysfs_attr_init(&bmc->aux_firmware_rev_attr.attr);
  1959. err = device_create_file(&bmc->dev->dev,
  1960. &bmc->device_id_attr);
  1961. if (err)
  1962. goto out;
  1963. err = device_create_file(&bmc->dev->dev,
  1964. &bmc->provides_dev_sdrs_attr);
  1965. if (err)
  1966. goto out_devid;
  1967. err = device_create_file(&bmc->dev->dev,
  1968. &bmc->revision_attr);
  1969. if (err)
  1970. goto out_sdrs;
  1971. err = device_create_file(&bmc->dev->dev,
  1972. &bmc->firmware_rev_attr);
  1973. if (err)
  1974. goto out_rev;
  1975. err = device_create_file(&bmc->dev->dev,
  1976. &bmc->version_attr);
  1977. if (err)
  1978. goto out_firm;
  1979. err = device_create_file(&bmc->dev->dev,
  1980. &bmc->add_dev_support_attr);
  1981. if (err)
  1982. goto out_version;
  1983. err = device_create_file(&bmc->dev->dev,
  1984. &bmc->manufacturer_id_attr);
  1985. if (err)
  1986. goto out_add_dev;
  1987. err = device_create_file(&bmc->dev->dev,
  1988. &bmc->product_id_attr);
  1989. if (err)
  1990. goto out_manu;
  1991. if (bmc->id.aux_firmware_revision_set) {
  1992. err = device_create_file(&bmc->dev->dev,
  1993. &bmc->aux_firmware_rev_attr);
  1994. if (err)
  1995. goto out_prod_id;
  1996. }
  1997. if (bmc->guid_set) {
  1998. err = device_create_file(&bmc->dev->dev,
  1999. &bmc->guid_attr);
  2000. if (err)
  2001. goto out_aux_firm;
  2002. }
  2003. return 0;
  2004. out_aux_firm:
  2005. if (bmc->id.aux_firmware_revision_set)
  2006. device_remove_file(&bmc->dev->dev,
  2007. &bmc->aux_firmware_rev_attr);
  2008. out_prod_id:
  2009. device_remove_file(&bmc->dev->dev,
  2010. &bmc->product_id_attr);
  2011. out_manu:
  2012. device_remove_file(&bmc->dev->dev,
  2013. &bmc->manufacturer_id_attr);
  2014. out_add_dev:
  2015. device_remove_file(&bmc->dev->dev,
  2016. &bmc->add_dev_support_attr);
  2017. out_version:
  2018. device_remove_file(&bmc->dev->dev,
  2019. &bmc->version_attr);
  2020. out_firm:
  2021. device_remove_file(&bmc->dev->dev,
  2022. &bmc->firmware_rev_attr);
  2023. out_rev:
  2024. device_remove_file(&bmc->dev->dev,
  2025. &bmc->revision_attr);
  2026. out_sdrs:
  2027. device_remove_file(&bmc->dev->dev,
  2028. &bmc->provides_dev_sdrs_attr);
  2029. out_devid:
  2030. device_remove_file(&bmc->dev->dev,
  2031. &bmc->device_id_attr);
  2032. out:
  2033. return err;
  2034. }
  2035. static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum,
  2036. const char *sysfs_name)
  2037. {
  2038. int rv;
  2039. struct bmc_device *bmc = intf->bmc;
  2040. struct bmc_device *old_bmc;
  2041. int size;
  2042. char dummy[1];
  2043. mutex_lock(&ipmidriver_mutex);
  2044. /*
  2045. * Try to find if there is an bmc_device struct
  2046. * representing the interfaced BMC already
  2047. */
  2048. if (bmc->guid_set)
  2049. old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
  2050. else
  2051. old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
  2052. bmc->id.product_id,
  2053. bmc->id.device_id);
  2054. /*
  2055. * If there is already an bmc_device, free the new one,
  2056. * otherwise register the new BMC device
  2057. */
  2058. if (old_bmc) {
  2059. kfree(bmc);
  2060. intf->bmc = old_bmc;
  2061. bmc = old_bmc;
  2062. kref_get(&bmc->refcount);
  2063. mutex_unlock(&ipmidriver_mutex);
  2064. printk(KERN_INFO
  2065. "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
  2066. " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
  2067. bmc->id.manufacturer_id,
  2068. bmc->id.product_id,
  2069. bmc->id.device_id);
  2070. } else {
  2071. char name[14];
  2072. unsigned char orig_dev_id = bmc->id.device_id;
  2073. int warn_printed = 0;
  2074. snprintf(name, sizeof(name),
  2075. "ipmi_bmc.%4.4x", bmc->id.product_id);
  2076. while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
  2077. bmc->id.product_id,
  2078. bmc->id.device_id)) {
  2079. if (!warn_printed) {
  2080. printk(KERN_WARNING PFX
  2081. "This machine has two different BMCs"
  2082. " with the same product id and device"
  2083. " id. This is an error in the"
  2084. " firmware, but incrementing the"
  2085. " device id to work around the problem."
  2086. " Prod ID = 0x%x, Dev ID = 0x%x\n",
  2087. bmc->id.product_id, bmc->id.device_id);
  2088. warn_printed = 1;
  2089. }
  2090. bmc->id.device_id++; /* Wraps at 255 */
  2091. if (bmc->id.device_id == orig_dev_id) {
  2092. printk(KERN_ERR PFX
  2093. "Out of device ids!\n");
  2094. break;
  2095. }
  2096. }
  2097. bmc->dev = platform_device_alloc(name, bmc->id.device_id);
  2098. if (!bmc->dev) {
  2099. mutex_unlock(&ipmidriver_mutex);
  2100. printk(KERN_ERR
  2101. "ipmi_msghandler:"
  2102. " Unable to allocate platform device\n");
  2103. return -ENOMEM;
  2104. }
  2105. bmc->dev->dev.driver = &ipmidriver.driver;
  2106. dev_set_drvdata(&bmc->dev->dev, bmc);
  2107. kref_init(&bmc->refcount);
  2108. rv = platform_device_add(bmc->dev);
  2109. mutex_unlock(&ipmidriver_mutex);
  2110. if (rv) {
  2111. platform_device_put(bmc->dev);
  2112. bmc->dev = NULL;
  2113. printk(KERN_ERR
  2114. "ipmi_msghandler:"
  2115. " Unable to register bmc device: %d\n",
  2116. rv);
  2117. /*
  2118. * Don't go to out_err, you can only do that if
  2119. * the device is registered already.
  2120. */
  2121. return rv;
  2122. }
  2123. rv = create_files(bmc);
  2124. if (rv) {
  2125. mutex_lock(&ipmidriver_mutex);
  2126. platform_device_unregister(bmc->dev);
  2127. mutex_unlock(&ipmidriver_mutex);
  2128. return rv;
  2129. }
  2130. dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
  2131. "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
  2132. bmc->id.manufacturer_id,
  2133. bmc->id.product_id,
  2134. bmc->id.device_id);
  2135. }
  2136. /*
  2137. * create symlink from system interface device to bmc device
  2138. * and back.
  2139. */
  2140. intf->sysfs_name = kstrdup(sysfs_name, GFP_KERNEL);
  2141. if (!intf->sysfs_name) {
  2142. rv = -ENOMEM;
  2143. printk(KERN_ERR
  2144. "ipmi_msghandler: allocate link to BMC: %d\n",
  2145. rv);
  2146. goto out_err;
  2147. }
  2148. rv = sysfs_create_link(&intf->si_dev->kobj,
  2149. &bmc->dev->dev.kobj, intf->sysfs_name);
  2150. if (rv) {
  2151. kfree(intf->sysfs_name);
  2152. intf->sysfs_name = NULL;
  2153. printk(KERN_ERR
  2154. "ipmi_msghandler: Unable to create bmc symlink: %d\n",
  2155. rv);
  2156. goto out_err;
  2157. }
  2158. size = snprintf(dummy, 0, "ipmi%d", ifnum);
  2159. intf->my_dev_name = kmalloc(size+1, GFP_KERNEL);
  2160. if (!intf->my_dev_name) {
  2161. kfree(intf->sysfs_name);
  2162. intf->sysfs_name = NULL;
  2163. rv = -ENOMEM;
  2164. printk(KERN_ERR
  2165. "ipmi_msghandler: allocate link from BMC: %d\n",
  2166. rv);
  2167. goto out_err;
  2168. }
  2169. snprintf(intf->my_dev_name, size+1, "ipmi%d", ifnum);
  2170. rv = sysfs_create_link(&bmc->dev->dev.kobj, &intf->si_dev->kobj,
  2171. intf->my_dev_name);
  2172. if (rv) {
  2173. kfree(intf->sysfs_name);
  2174. intf->sysfs_name = NULL;
  2175. kfree(intf->my_dev_name);
  2176. intf->my_dev_name = NULL;
  2177. printk(KERN_ERR
  2178. "ipmi_msghandler:"
  2179. " Unable to create symlink to bmc: %d\n",
  2180. rv);
  2181. goto out_err;
  2182. }
  2183. return 0;
  2184. out_err:
  2185. ipmi_bmc_unregister(intf);
  2186. return rv;
  2187. }
  2188. static int
  2189. send_guid_cmd(ipmi_smi_t intf, int chan)
  2190. {
  2191. struct kernel_ipmi_msg msg;
  2192. struct ipmi_system_interface_addr si;
  2193. si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2194. si.channel = IPMI_BMC_CHANNEL;
  2195. si.lun = 0;
  2196. msg.netfn = IPMI_NETFN_APP_REQUEST;
  2197. msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
  2198. msg.data = NULL;
  2199. msg.data_len = 0;
  2200. return i_ipmi_request(NULL,
  2201. intf,
  2202. (struct ipmi_addr *) &si,
  2203. 0,
  2204. &msg,
  2205. intf,
  2206. NULL,
  2207. NULL,
  2208. 0,
  2209. intf->channels[0].address,
  2210. intf->channels[0].lun,
  2211. -1, 0);
  2212. }
  2213. static void
  2214. guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
  2215. {
  2216. if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  2217. || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
  2218. || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
  2219. /* Not for me */
  2220. return;
  2221. if (msg->msg.data[0] != 0) {
  2222. /* Error from getting the GUID, the BMC doesn't have one. */
  2223. intf->bmc->guid_set = 0;
  2224. goto out;
  2225. }
  2226. if (msg->msg.data_len < 17) {
  2227. intf->bmc->guid_set = 0;
  2228. printk(KERN_WARNING PFX
  2229. "guid_handler: The GUID response from the BMC was too"
  2230. " short, it was %d but should have been 17. Assuming"
  2231. " GUID is not available.\n",
  2232. msg->msg.data_len);
  2233. goto out;
  2234. }
  2235. memcpy(intf->bmc->guid, msg->msg.data, 16);
  2236. intf->bmc->guid_set = 1;
  2237. out:
  2238. wake_up(&intf->waitq);
  2239. }
  2240. static void
  2241. get_guid(ipmi_smi_t intf)
  2242. {
  2243. int rv;
  2244. intf->bmc->guid_set = 0x2;
  2245. intf->null_user_handler = guid_handler;
  2246. rv = send_guid_cmd(intf, 0);
  2247. if (rv)
  2248. /* Send failed, no GUID available. */
  2249. intf->bmc->guid_set = 0;
  2250. wait_event(intf->waitq, intf->bmc->guid_set != 2);
  2251. intf->null_user_handler = NULL;
  2252. }
  2253. static int
  2254. send_channel_info_cmd(ipmi_smi_t intf, int chan)
  2255. {
  2256. struct kernel_ipmi_msg msg;
  2257. unsigned char data[1];
  2258. struct ipmi_system_interface_addr si;
  2259. si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2260. si.channel = IPMI_BMC_CHANNEL;
  2261. si.lun = 0;
  2262. msg.netfn = IPMI_NETFN_APP_REQUEST;
  2263. msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
  2264. msg.data = data;
  2265. msg.data_len = 1;
  2266. data[0] = chan;
  2267. return i_ipmi_request(NULL,
  2268. intf,
  2269. (struct ipmi_addr *) &si,
  2270. 0,
  2271. &msg,
  2272. intf,
  2273. NULL,
  2274. NULL,
  2275. 0,
  2276. intf->channels[0].address,
  2277. intf->channels[0].lun,
  2278. -1, 0);
  2279. }
  2280. static void
  2281. channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
  2282. {
  2283. int rv = 0;
  2284. int chan;
  2285. if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  2286. && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
  2287. && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
  2288. /* It's the one we want */
  2289. if (msg->msg.data[0] != 0) {
  2290. /* Got an error from the channel, just go on. */
  2291. if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
  2292. /*
  2293. * If the MC does not support this
  2294. * command, that is legal. We just
  2295. * assume it has one IPMB at channel
  2296. * zero.
  2297. */
  2298. intf->channels[0].medium
  2299. = IPMI_CHANNEL_MEDIUM_IPMB;
  2300. intf->channels[0].protocol
  2301. = IPMI_CHANNEL_PROTOCOL_IPMB;
  2302. rv = -ENOSYS;
  2303. intf->curr_channel = IPMI_MAX_CHANNELS;
  2304. wake_up(&intf->waitq);
  2305. goto out;
  2306. }
  2307. goto next_channel;
  2308. }
  2309. if (msg->msg.data_len < 4) {
  2310. /* Message not big enough, just go on. */
  2311. goto next_channel;
  2312. }
  2313. chan = intf->curr_channel;
  2314. intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
  2315. intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
  2316. next_channel:
  2317. intf->curr_channel++;
  2318. if (intf->curr_channel >= IPMI_MAX_CHANNELS)
  2319. wake_up(&intf->waitq);
  2320. else
  2321. rv = send_channel_info_cmd(intf, intf->curr_channel);
  2322. if (rv) {
  2323. /* Got an error somehow, just give up. */
  2324. intf->curr_channel = IPMI_MAX_CHANNELS;
  2325. wake_up(&intf->waitq);
  2326. printk(KERN_WARNING PFX
  2327. "Error sending channel information: %d\n",
  2328. rv);
  2329. }
  2330. }
  2331. out:
  2332. return;
  2333. }
  2334. void ipmi_poll_interface(ipmi_user_t user)
  2335. {
  2336. ipmi_smi_t intf = user->intf;
  2337. if (intf->handlers->poll)
  2338. intf->handlers->poll(intf->send_info);
  2339. }
  2340. EXPORT_SYMBOL(ipmi_poll_interface);
  2341. int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
  2342. void *send_info,
  2343. struct ipmi_device_id *device_id,
  2344. struct device *si_dev,
  2345. const char *sysfs_name,
  2346. unsigned char slave_addr)
  2347. {
  2348. int i, j;
  2349. int rv;
  2350. ipmi_smi_t intf;
  2351. ipmi_smi_t tintf;
  2352. struct list_head *link;
  2353. /*
  2354. * Make sure the driver is actually initialized, this handles
  2355. * problems with initialization order.
  2356. */
  2357. if (!initialized) {
  2358. rv = ipmi_init_msghandler();
  2359. if (rv)
  2360. return rv;
  2361. /*
  2362. * The init code doesn't return an error if it was turned
  2363. * off, but it won't initialize. Check that.
  2364. */
  2365. if (!initialized)
  2366. return -ENODEV;
  2367. }
  2368. intf = kzalloc(sizeof(*intf), GFP_KERNEL);
  2369. if (!intf)
  2370. return -ENOMEM;
  2371. intf->ipmi_version_major = ipmi_version_major(device_id);
  2372. intf->ipmi_version_minor = ipmi_version_minor(device_id);
  2373. intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
  2374. if (!intf->bmc) {
  2375. kfree(intf);
  2376. return -ENOMEM;
  2377. }
  2378. intf->intf_num = -1; /* Mark it invalid for now. */
  2379. kref_init(&intf->refcount);
  2380. intf->bmc->id = *device_id;
  2381. intf->si_dev = si_dev;
  2382. for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
  2383. intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
  2384. intf->channels[j].lun = 2;
  2385. }
  2386. if (slave_addr != 0)
  2387. intf->channels[0].address = slave_addr;
  2388. INIT_LIST_HEAD(&intf->users);
  2389. intf->handlers = handlers;
  2390. intf->send_info = send_info;
  2391. spin_lock_init(&intf->seq_lock);
  2392. for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
  2393. intf->seq_table[j].inuse = 0;
  2394. intf->seq_table[j].seqid = 0;
  2395. }
  2396. intf->curr_seq = 0;
  2397. #ifdef CONFIG_PROC_FS
  2398. mutex_init(&intf->proc_entry_lock);
  2399. #endif
  2400. spin_lock_init(&intf->waiting_msgs_lock);
  2401. INIT_LIST_HEAD(&intf->waiting_msgs);
  2402. spin_lock_init(&intf->events_lock);
  2403. INIT_LIST_HEAD(&intf->waiting_events);
  2404. intf->waiting_events_count = 0;
  2405. mutex_init(&intf->cmd_rcvrs_mutex);
  2406. spin_lock_init(&intf->maintenance_mode_lock);
  2407. INIT_LIST_HEAD(&intf->cmd_rcvrs);
  2408. init_waitqueue_head(&intf->waitq);
  2409. for (i = 0; i < IPMI_NUM_STATS; i++)
  2410. atomic_set(&intf->stats[i], 0);
  2411. intf->proc_dir = NULL;
  2412. mutex_lock(&smi_watchers_mutex);
  2413. mutex_lock(&ipmi_interfaces_mutex);
  2414. /* Look for a hole in the numbers. */
  2415. i = 0;
  2416. link = &ipmi_interfaces;
  2417. list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
  2418. if (tintf->intf_num != i) {
  2419. link = &tintf->link;
  2420. break;
  2421. }
  2422. i++;
  2423. }
  2424. /* Add the new interface in numeric order. */
  2425. if (i == 0)
  2426. list_add_rcu(&intf->link, &ipmi_interfaces);
  2427. else
  2428. list_add_tail_rcu(&intf->link, link);
  2429. rv = handlers->start_processing(send_info, intf);
  2430. if (rv)
  2431. goto out;
  2432. get_guid(intf);
  2433. if ((intf->ipmi_version_major > 1)
  2434. || ((intf->ipmi_version_major == 1)
  2435. && (intf->ipmi_version_minor >= 5))) {
  2436. /*
  2437. * Start scanning the channels to see what is
  2438. * available.
  2439. */
  2440. intf->null_user_handler = channel_handler;
  2441. intf->curr_channel = 0;
  2442. rv = send_channel_info_cmd(intf, 0);
  2443. if (rv)
  2444. goto out;
  2445. /* Wait for the channel info to be read. */
  2446. wait_event(intf->waitq,
  2447. intf->curr_channel >= IPMI_MAX_CHANNELS);
  2448. intf->null_user_handler = NULL;
  2449. } else {
  2450. /* Assume a single IPMB channel at zero. */
  2451. intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
  2452. intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
  2453. intf->curr_channel = IPMI_MAX_CHANNELS;
  2454. }
  2455. if (rv == 0)
  2456. rv = add_proc_entries(intf, i);
  2457. rv = ipmi_bmc_register(intf, i, sysfs_name);
  2458. out:
  2459. if (rv) {
  2460. if (intf->proc_dir)
  2461. remove_proc_entries(intf);
  2462. intf->handlers = NULL;
  2463. list_del_rcu(&intf->link);
  2464. mutex_unlock(&ipmi_interfaces_mutex);
  2465. mutex_unlock(&smi_watchers_mutex);
  2466. synchronize_rcu();
  2467. kref_put(&intf->refcount, intf_free);
  2468. } else {
  2469. /*
  2470. * Keep memory order straight for RCU readers. Make
  2471. * sure everything else is committed to memory before
  2472. * setting intf_num to mark the interface valid.
  2473. */
  2474. smp_wmb();
  2475. intf->intf_num = i;
  2476. mutex_unlock(&ipmi_interfaces_mutex);
  2477. /* After this point the interface is legal to use. */
  2478. call_smi_watchers(i, intf->si_dev);
  2479. mutex_unlock(&smi_watchers_mutex);
  2480. }
  2481. return rv;
  2482. }
  2483. EXPORT_SYMBOL(ipmi_register_smi);
  2484. static void cleanup_smi_msgs(ipmi_smi_t intf)
  2485. {
  2486. int i;
  2487. struct seq_table *ent;
  2488. /* No need for locks, the interface is down. */
  2489. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
  2490. ent = &(intf->seq_table[i]);
  2491. if (!ent->inuse)
  2492. continue;
  2493. deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
  2494. }
  2495. }
  2496. int ipmi_unregister_smi(ipmi_smi_t intf)
  2497. {
  2498. struct ipmi_smi_watcher *w;
  2499. int intf_num = intf->intf_num;
  2500. ipmi_bmc_unregister(intf);
  2501. mutex_lock(&smi_watchers_mutex);
  2502. mutex_lock(&ipmi_interfaces_mutex);
  2503. intf->intf_num = -1;
  2504. intf->handlers = NULL;
  2505. list_del_rcu(&intf->link);
  2506. mutex_unlock(&ipmi_interfaces_mutex);
  2507. synchronize_rcu();
  2508. cleanup_smi_msgs(intf);
  2509. remove_proc_entries(intf);
  2510. /*
  2511. * Call all the watcher interfaces to tell them that
  2512. * an interface is gone.
  2513. */
  2514. list_for_each_entry(w, &smi_watchers, link)
  2515. w->smi_gone(intf_num);
  2516. mutex_unlock(&smi_watchers_mutex);
  2517. kref_put(&intf->refcount, intf_free);
  2518. return 0;
  2519. }
  2520. EXPORT_SYMBOL(ipmi_unregister_smi);
  2521. static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
  2522. struct ipmi_smi_msg *msg)
  2523. {
  2524. struct ipmi_ipmb_addr ipmb_addr;
  2525. struct ipmi_recv_msg *recv_msg;
  2526. /*
  2527. * This is 11, not 10, because the response must contain a
  2528. * completion code.
  2529. */
  2530. if (msg->rsp_size < 11) {
  2531. /* Message not big enough, just ignore it. */
  2532. ipmi_inc_stat(intf, invalid_ipmb_responses);
  2533. return 0;
  2534. }
  2535. if (msg->rsp[2] != 0) {
  2536. /* An error getting the response, just ignore it. */
  2537. return 0;
  2538. }
  2539. ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
  2540. ipmb_addr.slave_addr = msg->rsp[6];
  2541. ipmb_addr.channel = msg->rsp[3] & 0x0f;
  2542. ipmb_addr.lun = msg->rsp[7] & 3;
  2543. /*
  2544. * It's a response from a remote entity. Look up the sequence
  2545. * number and handle the response.
  2546. */
  2547. if (intf_find_seq(intf,
  2548. msg->rsp[7] >> 2,
  2549. msg->rsp[3] & 0x0f,
  2550. msg->rsp[8],
  2551. (msg->rsp[4] >> 2) & (~1),
  2552. (struct ipmi_addr *) &(ipmb_addr),
  2553. &recv_msg)) {
  2554. /*
  2555. * We were unable to find the sequence number,
  2556. * so just nuke the message.
  2557. */
  2558. ipmi_inc_stat(intf, unhandled_ipmb_responses);
  2559. return 0;
  2560. }
  2561. memcpy(recv_msg->msg_data,
  2562. &(msg->rsp[9]),
  2563. msg->rsp_size - 9);
  2564. /*
  2565. * The other fields matched, so no need to set them, except
  2566. * for netfn, which needs to be the response that was
  2567. * returned, not the request value.
  2568. */
  2569. recv_msg->msg.netfn = msg->rsp[4] >> 2;
  2570. recv_msg->msg.data = recv_msg->msg_data;
  2571. recv_msg->msg.data_len = msg->rsp_size - 10;
  2572. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  2573. ipmi_inc_stat(intf, handled_ipmb_responses);
  2574. deliver_response(recv_msg);
  2575. return 0;
  2576. }
  2577. static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
  2578. struct ipmi_smi_msg *msg)
  2579. {
  2580. struct cmd_rcvr *rcvr;
  2581. int rv = 0;
  2582. unsigned char netfn;
  2583. unsigned char cmd;
  2584. unsigned char chan;
  2585. ipmi_user_t user = NULL;
  2586. struct ipmi_ipmb_addr *ipmb_addr;
  2587. struct ipmi_recv_msg *recv_msg;
  2588. struct ipmi_smi_handlers *handlers;
  2589. if (msg->rsp_size < 10) {
  2590. /* Message not big enough, just ignore it. */
  2591. ipmi_inc_stat(intf, invalid_commands);
  2592. return 0;
  2593. }
  2594. if (msg->rsp[2] != 0) {
  2595. /* An error getting the response, just ignore it. */
  2596. return 0;
  2597. }
  2598. netfn = msg->rsp[4] >> 2;
  2599. cmd = msg->rsp[8];
  2600. chan = msg->rsp[3] & 0xf;
  2601. rcu_read_lock();
  2602. rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
  2603. if (rcvr) {
  2604. user = rcvr->user;
  2605. kref_get(&user->refcount);
  2606. } else
  2607. user = NULL;
  2608. rcu_read_unlock();
  2609. if (user == NULL) {
  2610. /* We didn't find a user, deliver an error response. */
  2611. ipmi_inc_stat(intf, unhandled_commands);
  2612. msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  2613. msg->data[1] = IPMI_SEND_MSG_CMD;
  2614. msg->data[2] = msg->rsp[3];
  2615. msg->data[3] = msg->rsp[6];
  2616. msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
  2617. msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
  2618. msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
  2619. /* rqseq/lun */
  2620. msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
  2621. msg->data[8] = msg->rsp[8]; /* cmd */
  2622. msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
  2623. msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
  2624. msg->data_size = 11;
  2625. #ifdef DEBUG_MSGING
  2626. {
  2627. int m;
  2628. printk("Invalid command:");
  2629. for (m = 0; m < msg->data_size; m++)
  2630. printk(" %2.2x", msg->data[m]);
  2631. printk("\n");
  2632. }
  2633. #endif
  2634. rcu_read_lock();
  2635. handlers = intf->handlers;
  2636. if (handlers) {
  2637. handlers->sender(intf->send_info, msg, 0);
  2638. /*
  2639. * We used the message, so return the value
  2640. * that causes it to not be freed or
  2641. * queued.
  2642. */
  2643. rv = -1;
  2644. }
  2645. rcu_read_unlock();
  2646. } else {
  2647. /* Deliver the message to the user. */
  2648. ipmi_inc_stat(intf, handled_commands);
  2649. recv_msg = ipmi_alloc_recv_msg();
  2650. if (!recv_msg) {
  2651. /*
  2652. * We couldn't allocate memory for the
  2653. * message, so requeue it for handling
  2654. * later.
  2655. */
  2656. rv = 1;
  2657. kref_put(&user->refcount, free_user);
  2658. } else {
  2659. /* Extract the source address from the data. */
  2660. ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
  2661. ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
  2662. ipmb_addr->slave_addr = msg->rsp[6];
  2663. ipmb_addr->lun = msg->rsp[7] & 3;
  2664. ipmb_addr->channel = msg->rsp[3] & 0xf;
  2665. /*
  2666. * Extract the rest of the message information
  2667. * from the IPMB header.
  2668. */
  2669. recv_msg->user = user;
  2670. recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
  2671. recv_msg->msgid = msg->rsp[7] >> 2;
  2672. recv_msg->msg.netfn = msg->rsp[4] >> 2;
  2673. recv_msg->msg.cmd = msg->rsp[8];
  2674. recv_msg->msg.data = recv_msg->msg_data;
  2675. /*
  2676. * We chop off 10, not 9 bytes because the checksum
  2677. * at the end also needs to be removed.
  2678. */
  2679. recv_msg->msg.data_len = msg->rsp_size - 10;
  2680. memcpy(recv_msg->msg_data,
  2681. &(msg->rsp[9]),
  2682. msg->rsp_size - 10);
  2683. deliver_response(recv_msg);
  2684. }
  2685. }
  2686. return rv;
  2687. }
  2688. static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
  2689. struct ipmi_smi_msg *msg)
  2690. {
  2691. struct ipmi_lan_addr lan_addr;
  2692. struct ipmi_recv_msg *recv_msg;
  2693. /*
  2694. * This is 13, not 12, because the response must contain a
  2695. * completion code.
  2696. */
  2697. if (msg->rsp_size < 13) {
  2698. /* Message not big enough, just ignore it. */
  2699. ipmi_inc_stat(intf, invalid_lan_responses);
  2700. return 0;
  2701. }
  2702. if (msg->rsp[2] != 0) {
  2703. /* An error getting the response, just ignore it. */
  2704. return 0;
  2705. }
  2706. lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
  2707. lan_addr.session_handle = msg->rsp[4];
  2708. lan_addr.remote_SWID = msg->rsp[8];
  2709. lan_addr.local_SWID = msg->rsp[5];
  2710. lan_addr.channel = msg->rsp[3] & 0x0f;
  2711. lan_addr.privilege = msg->rsp[3] >> 4;
  2712. lan_addr.lun = msg->rsp[9] & 3;
  2713. /*
  2714. * It's a response from a remote entity. Look up the sequence
  2715. * number and handle the response.
  2716. */
  2717. if (intf_find_seq(intf,
  2718. msg->rsp[9] >> 2,
  2719. msg->rsp[3] & 0x0f,
  2720. msg->rsp[10],
  2721. (msg->rsp[6] >> 2) & (~1),
  2722. (struct ipmi_addr *) &(lan_addr),
  2723. &recv_msg)) {
  2724. /*
  2725. * We were unable to find the sequence number,
  2726. * so just nuke the message.
  2727. */
  2728. ipmi_inc_stat(intf, unhandled_lan_responses);
  2729. return 0;
  2730. }
  2731. memcpy(recv_msg->msg_data,
  2732. &(msg->rsp[11]),
  2733. msg->rsp_size - 11);
  2734. /*
  2735. * The other fields matched, so no need to set them, except
  2736. * for netfn, which needs to be the response that was
  2737. * returned, not the request value.
  2738. */
  2739. recv_msg->msg.netfn = msg->rsp[6] >> 2;
  2740. recv_msg->msg.data = recv_msg->msg_data;
  2741. recv_msg->msg.data_len = msg->rsp_size - 12;
  2742. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  2743. ipmi_inc_stat(intf, handled_lan_responses);
  2744. deliver_response(recv_msg);
  2745. return 0;
  2746. }
  2747. static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
  2748. struct ipmi_smi_msg *msg)
  2749. {
  2750. struct cmd_rcvr *rcvr;
  2751. int rv = 0;
  2752. unsigned char netfn;
  2753. unsigned char cmd;
  2754. unsigned char chan;
  2755. ipmi_user_t user = NULL;
  2756. struct ipmi_lan_addr *lan_addr;
  2757. struct ipmi_recv_msg *recv_msg;
  2758. if (msg->rsp_size < 12) {
  2759. /* Message not big enough, just ignore it. */
  2760. ipmi_inc_stat(intf, invalid_commands);
  2761. return 0;
  2762. }
  2763. if (msg->rsp[2] != 0) {
  2764. /* An error getting the response, just ignore it. */
  2765. return 0;
  2766. }
  2767. netfn = msg->rsp[6] >> 2;
  2768. cmd = msg->rsp[10];
  2769. chan = msg->rsp[3] & 0xf;
  2770. rcu_read_lock();
  2771. rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
  2772. if (rcvr) {
  2773. user = rcvr->user;
  2774. kref_get(&user->refcount);
  2775. } else
  2776. user = NULL;
  2777. rcu_read_unlock();
  2778. if (user == NULL) {
  2779. /* We didn't find a user, just give up. */
  2780. ipmi_inc_stat(intf, unhandled_commands);
  2781. /*
  2782. * Don't do anything with these messages, just allow
  2783. * them to be freed.
  2784. */
  2785. rv = 0;
  2786. } else {
  2787. /* Deliver the message to the user. */
  2788. ipmi_inc_stat(intf, handled_commands);
  2789. recv_msg = ipmi_alloc_recv_msg();
  2790. if (!recv_msg) {
  2791. /*
  2792. * We couldn't allocate memory for the
  2793. * message, so requeue it for handling later.
  2794. */
  2795. rv = 1;
  2796. kref_put(&user->refcount, free_user);
  2797. } else {
  2798. /* Extract the source address from the data. */
  2799. lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
  2800. lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
  2801. lan_addr->session_handle = msg->rsp[4];
  2802. lan_addr->remote_SWID = msg->rsp[8];
  2803. lan_addr->local_SWID = msg->rsp[5];
  2804. lan_addr->lun = msg->rsp[9] & 3;
  2805. lan_addr->channel = msg->rsp[3] & 0xf;
  2806. lan_addr->privilege = msg->rsp[3] >> 4;
  2807. /*
  2808. * Extract the rest of the message information
  2809. * from the IPMB header.
  2810. */
  2811. recv_msg->user = user;
  2812. recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
  2813. recv_msg->msgid = msg->rsp[9] >> 2;
  2814. recv_msg->msg.netfn = msg->rsp[6] >> 2;
  2815. recv_msg->msg.cmd = msg->rsp[10];
  2816. recv_msg->msg.data = recv_msg->msg_data;
  2817. /*
  2818. * We chop off 12, not 11 bytes because the checksum
  2819. * at the end also needs to be removed.
  2820. */
  2821. recv_msg->msg.data_len = msg->rsp_size - 12;
  2822. memcpy(recv_msg->msg_data,
  2823. &(msg->rsp[11]),
  2824. msg->rsp_size - 12);
  2825. deliver_response(recv_msg);
  2826. }
  2827. }
  2828. return rv;
  2829. }
  2830. /*
  2831. * This routine will handle "Get Message" command responses with
  2832. * channels that use an OEM Medium. The message format belongs to
  2833. * the OEM. See IPMI 2.0 specification, Chapter 6 and
  2834. * Chapter 22, sections 22.6 and 22.24 for more details.
  2835. */
  2836. static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
  2837. struct ipmi_smi_msg *msg)
  2838. {
  2839. struct cmd_rcvr *rcvr;
  2840. int rv = 0;
  2841. unsigned char netfn;
  2842. unsigned char cmd;
  2843. unsigned char chan;
  2844. ipmi_user_t user = NULL;
  2845. struct ipmi_system_interface_addr *smi_addr;
  2846. struct ipmi_recv_msg *recv_msg;
  2847. /*
  2848. * We expect the OEM SW to perform error checking
  2849. * so we just do some basic sanity checks
  2850. */
  2851. if (msg->rsp_size < 4) {
  2852. /* Message not big enough, just ignore it. */
  2853. ipmi_inc_stat(intf, invalid_commands);
  2854. return 0;
  2855. }
  2856. if (msg->rsp[2] != 0) {
  2857. /* An error getting the response, just ignore it. */
  2858. return 0;
  2859. }
  2860. /*
  2861. * This is an OEM Message so the OEM needs to know how
  2862. * handle the message. We do no interpretation.
  2863. */
  2864. netfn = msg->rsp[0] >> 2;
  2865. cmd = msg->rsp[1];
  2866. chan = msg->rsp[3] & 0xf;
  2867. rcu_read_lock();
  2868. rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
  2869. if (rcvr) {
  2870. user = rcvr->user;
  2871. kref_get(&user->refcount);
  2872. } else
  2873. user = NULL;
  2874. rcu_read_unlock();
  2875. if (user == NULL) {
  2876. /* We didn't find a user, just give up. */
  2877. ipmi_inc_stat(intf, unhandled_commands);
  2878. /*
  2879. * Don't do anything with these messages, just allow
  2880. * them to be freed.
  2881. */
  2882. rv = 0;
  2883. } else {
  2884. /* Deliver the message to the user. */
  2885. ipmi_inc_stat(intf, handled_commands);
  2886. recv_msg = ipmi_alloc_recv_msg();
  2887. if (!recv_msg) {
  2888. /*
  2889. * We couldn't allocate memory for the
  2890. * message, so requeue it for handling
  2891. * later.
  2892. */
  2893. rv = 1;
  2894. kref_put(&user->refcount, free_user);
  2895. } else {
  2896. /*
  2897. * OEM Messages are expected to be delivered via
  2898. * the system interface to SMS software. We might
  2899. * need to visit this again depending on OEM
  2900. * requirements
  2901. */
  2902. smi_addr = ((struct ipmi_system_interface_addr *)
  2903. &(recv_msg->addr));
  2904. smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2905. smi_addr->channel = IPMI_BMC_CHANNEL;
  2906. smi_addr->lun = msg->rsp[0] & 3;
  2907. recv_msg->user = user;
  2908. recv_msg->user_msg_data = NULL;
  2909. recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
  2910. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  2911. recv_msg->msg.cmd = msg->rsp[1];
  2912. recv_msg->msg.data = recv_msg->msg_data;
  2913. /*
  2914. * The message starts at byte 4 which follows the
  2915. * the Channel Byte in the "GET MESSAGE" command
  2916. */
  2917. recv_msg->msg.data_len = msg->rsp_size - 4;
  2918. memcpy(recv_msg->msg_data,
  2919. &(msg->rsp[4]),
  2920. msg->rsp_size - 4);
  2921. deliver_response(recv_msg);
  2922. }
  2923. }
  2924. return rv;
  2925. }
  2926. static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
  2927. struct ipmi_smi_msg *msg)
  2928. {
  2929. struct ipmi_system_interface_addr *smi_addr;
  2930. recv_msg->msgid = 0;
  2931. smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
  2932. smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2933. smi_addr->channel = IPMI_BMC_CHANNEL;
  2934. smi_addr->lun = msg->rsp[0] & 3;
  2935. recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
  2936. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  2937. recv_msg->msg.cmd = msg->rsp[1];
  2938. memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
  2939. recv_msg->msg.data = recv_msg->msg_data;
  2940. recv_msg->msg.data_len = msg->rsp_size - 3;
  2941. }
  2942. static int handle_read_event_rsp(ipmi_smi_t intf,
  2943. struct ipmi_smi_msg *msg)
  2944. {
  2945. struct ipmi_recv_msg *recv_msg, *recv_msg2;
  2946. struct list_head msgs;
  2947. ipmi_user_t user;
  2948. int rv = 0;
  2949. int deliver_count = 0;
  2950. unsigned long flags;
  2951. if (msg->rsp_size < 19) {
  2952. /* Message is too small to be an IPMB event. */
  2953. ipmi_inc_stat(intf, invalid_events);
  2954. return 0;
  2955. }
  2956. if (msg->rsp[2] != 0) {
  2957. /* An error getting the event, just ignore it. */
  2958. return 0;
  2959. }
  2960. INIT_LIST_HEAD(&msgs);
  2961. spin_lock_irqsave(&intf->events_lock, flags);
  2962. ipmi_inc_stat(intf, events);
  2963. /*
  2964. * Allocate and fill in one message for every user that is
  2965. * getting events.
  2966. */
  2967. rcu_read_lock();
  2968. list_for_each_entry_rcu(user, &intf->users, link) {
  2969. if (!user->gets_events)
  2970. continue;
  2971. recv_msg = ipmi_alloc_recv_msg();
  2972. if (!recv_msg) {
  2973. rcu_read_unlock();
  2974. list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
  2975. link) {
  2976. list_del(&recv_msg->link);
  2977. ipmi_free_recv_msg(recv_msg);
  2978. }
  2979. /*
  2980. * We couldn't allocate memory for the
  2981. * message, so requeue it for handling
  2982. * later.
  2983. */
  2984. rv = 1;
  2985. goto out;
  2986. }
  2987. deliver_count++;
  2988. copy_event_into_recv_msg(recv_msg, msg);
  2989. recv_msg->user = user;
  2990. kref_get(&user->refcount);
  2991. list_add_tail(&(recv_msg->link), &msgs);
  2992. }
  2993. rcu_read_unlock();
  2994. if (deliver_count) {
  2995. /* Now deliver all the messages. */
  2996. list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
  2997. list_del(&recv_msg->link);
  2998. deliver_response(recv_msg);
  2999. }
  3000. } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
  3001. /*
  3002. * No one to receive the message, put it in queue if there's
  3003. * not already too many things in the queue.
  3004. */
  3005. recv_msg = ipmi_alloc_recv_msg();
  3006. if (!recv_msg) {
  3007. /*
  3008. * We couldn't allocate memory for the
  3009. * message, so requeue it for handling
  3010. * later.
  3011. */
  3012. rv = 1;
  3013. goto out;
  3014. }
  3015. copy_event_into_recv_msg(recv_msg, msg);
  3016. list_add_tail(&(recv_msg->link), &(intf->waiting_events));
  3017. intf->waiting_events_count++;
  3018. } else if (!intf->event_msg_printed) {
  3019. /*
  3020. * There's too many things in the queue, discard this
  3021. * message.
  3022. */
  3023. printk(KERN_WARNING PFX "Event queue full, discarding"
  3024. " incoming events\n");
  3025. intf->event_msg_printed = 1;
  3026. }
  3027. out:
  3028. spin_unlock_irqrestore(&(intf->events_lock), flags);
  3029. return rv;
  3030. }
  3031. static int handle_bmc_rsp(ipmi_smi_t intf,
  3032. struct ipmi_smi_msg *msg)
  3033. {
  3034. struct ipmi_recv_msg *recv_msg;
  3035. struct ipmi_user *user;
  3036. recv_msg = (struct ipmi_recv_msg *) msg->user_data;
  3037. if (recv_msg == NULL) {
  3038. printk(KERN_WARNING
  3039. "IPMI message received with no owner. This\n"
  3040. "could be because of a malformed message, or\n"
  3041. "because of a hardware error. Contact your\n"
  3042. "hardware vender for assistance\n");
  3043. return 0;
  3044. }
  3045. user = recv_msg->user;
  3046. /* Make sure the user still exists. */
  3047. if (user && !user->valid) {
  3048. /* The user for the message went away, so give up. */
  3049. ipmi_inc_stat(intf, unhandled_local_responses);
  3050. ipmi_free_recv_msg(recv_msg);
  3051. } else {
  3052. struct ipmi_system_interface_addr *smi_addr;
  3053. ipmi_inc_stat(intf, handled_local_responses);
  3054. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  3055. recv_msg->msgid = msg->msgid;
  3056. smi_addr = ((struct ipmi_system_interface_addr *)
  3057. &(recv_msg->addr));
  3058. smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  3059. smi_addr->channel = IPMI_BMC_CHANNEL;
  3060. smi_addr->lun = msg->rsp[0] & 3;
  3061. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  3062. recv_msg->msg.cmd = msg->rsp[1];
  3063. memcpy(recv_msg->msg_data,
  3064. &(msg->rsp[2]),
  3065. msg->rsp_size - 2);
  3066. recv_msg->msg.data = recv_msg->msg_data;
  3067. recv_msg->msg.data_len = msg->rsp_size - 2;
  3068. deliver_response(recv_msg);
  3069. }
  3070. return 0;
  3071. }
  3072. /*
  3073. * Handle a new message. Return 1 if the message should be requeued,
  3074. * 0 if the message should be freed, or -1 if the message should not
  3075. * be freed or requeued.
  3076. */
  3077. static int handle_new_recv_msg(ipmi_smi_t intf,
  3078. struct ipmi_smi_msg *msg)
  3079. {
  3080. int requeue;
  3081. int chan;
  3082. #ifdef DEBUG_MSGING
  3083. int m;
  3084. printk("Recv:");
  3085. for (m = 0; m < msg->rsp_size; m++)
  3086. printk(" %2.2x", msg->rsp[m]);
  3087. printk("\n");
  3088. #endif
  3089. if (msg->rsp_size < 2) {
  3090. /* Message is too small to be correct. */
  3091. printk(KERN_WARNING PFX "BMC returned to small a message"
  3092. " for netfn %x cmd %x, got %d bytes\n",
  3093. (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
  3094. /* Generate an error response for the message. */
  3095. msg->rsp[0] = msg->data[0] | (1 << 2);
  3096. msg->rsp[1] = msg->data[1];
  3097. msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
  3098. msg->rsp_size = 3;
  3099. } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
  3100. || (msg->rsp[1] != msg->data[1])) {
  3101. /*
  3102. * The NetFN and Command in the response is not even
  3103. * marginally correct.
  3104. */
  3105. printk(KERN_WARNING PFX "BMC returned incorrect response,"
  3106. " expected netfn %x cmd %x, got netfn %x cmd %x\n",
  3107. (msg->data[0] >> 2) | 1, msg->data[1],
  3108. msg->rsp[0] >> 2, msg->rsp[1]);
  3109. /* Generate an error response for the message. */
  3110. msg->rsp[0] = msg->data[0] | (1 << 2);
  3111. msg->rsp[1] = msg->data[1];
  3112. msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
  3113. msg->rsp_size = 3;
  3114. }
  3115. if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
  3116. && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
  3117. && (msg->user_data != NULL)) {
  3118. /*
  3119. * It's a response to a response we sent. For this we
  3120. * deliver a send message response to the user.
  3121. */
  3122. struct ipmi_recv_msg *recv_msg = msg->user_data;
  3123. requeue = 0;
  3124. if (msg->rsp_size < 2)
  3125. /* Message is too small to be correct. */
  3126. goto out;
  3127. chan = msg->data[2] & 0x0f;
  3128. if (chan >= IPMI_MAX_CHANNELS)
  3129. /* Invalid channel number */
  3130. goto out;
  3131. if (!recv_msg)
  3132. goto out;
  3133. /* Make sure the user still exists. */
  3134. if (!recv_msg->user || !recv_msg->user->valid)
  3135. goto out;
  3136. recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
  3137. recv_msg->msg.data = recv_msg->msg_data;
  3138. recv_msg->msg.data_len = 1;
  3139. recv_msg->msg_data[0] = msg->rsp[2];
  3140. deliver_response(recv_msg);
  3141. } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
  3142. && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
  3143. /* It's from the receive queue. */
  3144. chan = msg->rsp[3] & 0xf;
  3145. if (chan >= IPMI_MAX_CHANNELS) {
  3146. /* Invalid channel number */
  3147. requeue = 0;
  3148. goto out;
  3149. }
  3150. /*
  3151. * We need to make sure the channels have been initialized.
  3152. * The channel_handler routine will set the "curr_channel"
  3153. * equal to or greater than IPMI_MAX_CHANNELS when all the
  3154. * channels for this interface have been initialized.
  3155. */
  3156. if (intf->curr_channel < IPMI_MAX_CHANNELS) {
  3157. requeue = 0; /* Throw the message away */
  3158. goto out;
  3159. }
  3160. switch (intf->channels[chan].medium) {
  3161. case IPMI_CHANNEL_MEDIUM_IPMB:
  3162. if (msg->rsp[4] & 0x04) {
  3163. /*
  3164. * It's a response, so find the
  3165. * requesting message and send it up.
  3166. */
  3167. requeue = handle_ipmb_get_msg_rsp(intf, msg);
  3168. } else {
  3169. /*
  3170. * It's a command to the SMS from some other
  3171. * entity. Handle that.
  3172. */
  3173. requeue = handle_ipmb_get_msg_cmd(intf, msg);
  3174. }
  3175. break;
  3176. case IPMI_CHANNEL_MEDIUM_8023LAN:
  3177. case IPMI_CHANNEL_MEDIUM_ASYNC:
  3178. if (msg->rsp[6] & 0x04) {
  3179. /*
  3180. * It's a response, so find the
  3181. * requesting message and send it up.
  3182. */
  3183. requeue = handle_lan_get_msg_rsp(intf, msg);
  3184. } else {
  3185. /*
  3186. * It's a command to the SMS from some other
  3187. * entity. Handle that.
  3188. */
  3189. requeue = handle_lan_get_msg_cmd(intf, msg);
  3190. }
  3191. break;
  3192. default:
  3193. /* Check for OEM Channels. Clients had better
  3194. register for these commands. */
  3195. if ((intf->channels[chan].medium
  3196. >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
  3197. && (intf->channels[chan].medium
  3198. <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
  3199. requeue = handle_oem_get_msg_cmd(intf, msg);
  3200. } else {
  3201. /*
  3202. * We don't handle the channel type, so just
  3203. * free the message.
  3204. */
  3205. requeue = 0;
  3206. }
  3207. }
  3208. } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
  3209. && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
  3210. /* It's an asyncronous event. */
  3211. requeue = handle_read_event_rsp(intf, msg);
  3212. } else {
  3213. /* It's a response from the local BMC. */
  3214. requeue = handle_bmc_rsp(intf, msg);
  3215. }
  3216. out:
  3217. return requeue;
  3218. }
  3219. /* Handle a new message from the lower layer. */
  3220. void ipmi_smi_msg_received(ipmi_smi_t intf,
  3221. struct ipmi_smi_msg *msg)
  3222. {
  3223. unsigned long flags = 0; /* keep us warning-free. */
  3224. int rv;
  3225. int run_to_completion;
  3226. if ((msg->data_size >= 2)
  3227. && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
  3228. && (msg->data[1] == IPMI_SEND_MSG_CMD)
  3229. && (msg->user_data == NULL)) {
  3230. /*
  3231. * This is the local response to a command send, start
  3232. * the timer for these. The user_data will not be
  3233. * NULL if this is a response send, and we will let
  3234. * response sends just go through.
  3235. */
  3236. /*
  3237. * Check for errors, if we get certain errors (ones
  3238. * that mean basically we can try again later), we
  3239. * ignore them and start the timer. Otherwise we
  3240. * report the error immediately.
  3241. */
  3242. if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
  3243. && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
  3244. && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
  3245. && (msg->rsp[2] != IPMI_BUS_ERR)
  3246. && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
  3247. int chan = msg->rsp[3] & 0xf;
  3248. /* Got an error sending the message, handle it. */
  3249. if (chan >= IPMI_MAX_CHANNELS)
  3250. ; /* This shouldn't happen */
  3251. else if ((intf->channels[chan].medium
  3252. == IPMI_CHANNEL_MEDIUM_8023LAN)
  3253. || (intf->channels[chan].medium
  3254. == IPMI_CHANNEL_MEDIUM_ASYNC))
  3255. ipmi_inc_stat(intf, sent_lan_command_errs);
  3256. else
  3257. ipmi_inc_stat(intf, sent_ipmb_command_errs);
  3258. intf_err_seq(intf, msg->msgid, msg->rsp[2]);
  3259. } else
  3260. /* The message was sent, start the timer. */
  3261. intf_start_seq_timer(intf, msg->msgid);
  3262. ipmi_free_smi_msg(msg);
  3263. goto out;
  3264. }
  3265. /*
  3266. * To preserve message order, if the list is not empty, we
  3267. * tack this message onto the end of the list.
  3268. */
  3269. run_to_completion = intf->run_to_completion;
  3270. if (!run_to_completion)
  3271. spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
  3272. if (!list_empty(&intf->waiting_msgs)) {
  3273. list_add_tail(&msg->link, &intf->waiting_msgs);
  3274. if (!run_to_completion)
  3275. spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
  3276. goto out;
  3277. }
  3278. if (!run_to_completion)
  3279. spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
  3280. rv = handle_new_recv_msg(intf, msg);
  3281. if (rv > 0) {
  3282. /*
  3283. * Could not handle the message now, just add it to a
  3284. * list to handle later.
  3285. */
  3286. run_to_completion = intf->run_to_completion;
  3287. if (!run_to_completion)
  3288. spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
  3289. list_add_tail(&msg->link, &intf->waiting_msgs);
  3290. if (!run_to_completion)
  3291. spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
  3292. } else if (rv == 0) {
  3293. ipmi_free_smi_msg(msg);
  3294. }
  3295. out:
  3296. return;
  3297. }
  3298. EXPORT_SYMBOL(ipmi_smi_msg_received);
  3299. void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
  3300. {
  3301. ipmi_user_t user;
  3302. rcu_read_lock();
  3303. list_for_each_entry_rcu(user, &intf->users, link) {
  3304. if (!user->handler->ipmi_watchdog_pretimeout)
  3305. continue;
  3306. user->handler->ipmi_watchdog_pretimeout(user->handler_data);
  3307. }
  3308. rcu_read_unlock();
  3309. }
  3310. EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
  3311. static struct ipmi_smi_msg *
  3312. smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
  3313. unsigned char seq, long seqid)
  3314. {
  3315. struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
  3316. if (!smi_msg)
  3317. /*
  3318. * If we can't allocate the message, then just return, we
  3319. * get 4 retries, so this should be ok.
  3320. */
  3321. return NULL;
  3322. memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
  3323. smi_msg->data_size = recv_msg->msg.data_len;
  3324. smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
  3325. #ifdef DEBUG_MSGING
  3326. {
  3327. int m;
  3328. printk("Resend: ");
  3329. for (m = 0; m < smi_msg->data_size; m++)
  3330. printk(" %2.2x", smi_msg->data[m]);
  3331. printk("\n");
  3332. }
  3333. #endif
  3334. return smi_msg;
  3335. }
  3336. static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
  3337. struct list_head *timeouts, long timeout_period,
  3338. int slot, unsigned long *flags)
  3339. {
  3340. struct ipmi_recv_msg *msg;
  3341. struct ipmi_smi_handlers *handlers;
  3342. if (intf->intf_num == -1)
  3343. return;
  3344. if (!ent->inuse)
  3345. return;
  3346. ent->timeout -= timeout_period;
  3347. if (ent->timeout > 0)
  3348. return;
  3349. if (ent->retries_left == 0) {
  3350. /* The message has used all its retries. */
  3351. ent->inuse = 0;
  3352. msg = ent->recv_msg;
  3353. list_add_tail(&msg->link, timeouts);
  3354. if (ent->broadcast)
  3355. ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
  3356. else if (is_lan_addr(&ent->recv_msg->addr))
  3357. ipmi_inc_stat(intf, timed_out_lan_commands);
  3358. else
  3359. ipmi_inc_stat(intf, timed_out_ipmb_commands);
  3360. } else {
  3361. struct ipmi_smi_msg *smi_msg;
  3362. /* More retries, send again. */
  3363. /*
  3364. * Start with the max timer, set to normal timer after
  3365. * the message is sent.
  3366. */
  3367. ent->timeout = MAX_MSG_TIMEOUT;
  3368. ent->retries_left--;
  3369. smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
  3370. ent->seqid);
  3371. if (!smi_msg) {
  3372. if (is_lan_addr(&ent->recv_msg->addr))
  3373. ipmi_inc_stat(intf,
  3374. dropped_rexmit_lan_commands);
  3375. else
  3376. ipmi_inc_stat(intf,
  3377. dropped_rexmit_ipmb_commands);
  3378. return;
  3379. }
  3380. spin_unlock_irqrestore(&intf->seq_lock, *flags);
  3381. /*
  3382. * Send the new message. We send with a zero
  3383. * priority. It timed out, I doubt time is that
  3384. * critical now, and high priority messages are really
  3385. * only for messages to the local MC, which don't get
  3386. * resent.
  3387. */
  3388. handlers = intf->handlers;
  3389. if (handlers) {
  3390. if (is_lan_addr(&ent->recv_msg->addr))
  3391. ipmi_inc_stat(intf,
  3392. retransmitted_lan_commands);
  3393. else
  3394. ipmi_inc_stat(intf,
  3395. retransmitted_ipmb_commands);
  3396. intf->handlers->sender(intf->send_info,
  3397. smi_msg, 0);
  3398. } else
  3399. ipmi_free_smi_msg(smi_msg);
  3400. spin_lock_irqsave(&intf->seq_lock, *flags);
  3401. }
  3402. }
  3403. static void ipmi_timeout_handler(long timeout_period)
  3404. {
  3405. ipmi_smi_t intf;
  3406. struct list_head timeouts;
  3407. struct ipmi_recv_msg *msg, *msg2;
  3408. struct ipmi_smi_msg *smi_msg, *smi_msg2;
  3409. unsigned long flags;
  3410. int i;
  3411. rcu_read_lock();
  3412. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3413. /* See if any waiting messages need to be processed. */
  3414. spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
  3415. list_for_each_entry_safe(smi_msg, smi_msg2,
  3416. &intf->waiting_msgs, link) {
  3417. if (!handle_new_recv_msg(intf, smi_msg)) {
  3418. list_del(&smi_msg->link);
  3419. ipmi_free_smi_msg(smi_msg);
  3420. } else {
  3421. /*
  3422. * To preserve message order, quit if we
  3423. * can't handle a message.
  3424. */
  3425. break;
  3426. }
  3427. }
  3428. spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
  3429. /*
  3430. * Go through the seq table and find any messages that
  3431. * have timed out, putting them in the timeouts
  3432. * list.
  3433. */
  3434. INIT_LIST_HEAD(&timeouts);
  3435. spin_lock_irqsave(&intf->seq_lock, flags);
  3436. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
  3437. check_msg_timeout(intf, &(intf->seq_table[i]),
  3438. &timeouts, timeout_period, i,
  3439. &flags);
  3440. spin_unlock_irqrestore(&intf->seq_lock, flags);
  3441. list_for_each_entry_safe(msg, msg2, &timeouts, link)
  3442. deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
  3443. /*
  3444. * Maintenance mode handling. Check the timeout
  3445. * optimistically before we claim the lock. It may
  3446. * mean a timeout gets missed occasionally, but that
  3447. * only means the timeout gets extended by one period
  3448. * in that case. No big deal, and it avoids the lock
  3449. * most of the time.
  3450. */
  3451. if (intf->auto_maintenance_timeout > 0) {
  3452. spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
  3453. if (intf->auto_maintenance_timeout > 0) {
  3454. intf->auto_maintenance_timeout
  3455. -= timeout_period;
  3456. if (!intf->maintenance_mode
  3457. && (intf->auto_maintenance_timeout <= 0)) {
  3458. intf->maintenance_mode_enable = 0;
  3459. maintenance_mode_update(intf);
  3460. }
  3461. }
  3462. spin_unlock_irqrestore(&intf->maintenance_mode_lock,
  3463. flags);
  3464. }
  3465. }
  3466. rcu_read_unlock();
  3467. }
  3468. static void ipmi_request_event(void)
  3469. {
  3470. ipmi_smi_t intf;
  3471. struct ipmi_smi_handlers *handlers;
  3472. rcu_read_lock();
  3473. /*
  3474. * Called from the timer, no need to check if handlers is
  3475. * valid.
  3476. */
  3477. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3478. /* No event requests when in maintenance mode. */
  3479. if (intf->maintenance_mode_enable)
  3480. continue;
  3481. handlers = intf->handlers;
  3482. if (handlers)
  3483. handlers->request_events(intf->send_info);
  3484. }
  3485. rcu_read_unlock();
  3486. }
  3487. static struct timer_list ipmi_timer;
  3488. /* Call every ~1000 ms. */
  3489. #define IPMI_TIMEOUT_TIME 1000
  3490. /* How many jiffies does it take to get to the timeout time. */
  3491. #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
  3492. /*
  3493. * Request events from the queue every second (this is the number of
  3494. * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
  3495. * future, IPMI will add a way to know immediately if an event is in
  3496. * the queue and this silliness can go away.
  3497. */
  3498. #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
  3499. static atomic_t stop_operation;
  3500. static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
  3501. static void ipmi_timeout(unsigned long data)
  3502. {
  3503. if (atomic_read(&stop_operation))
  3504. return;
  3505. ticks_to_req_ev--;
  3506. if (ticks_to_req_ev == 0) {
  3507. ipmi_request_event();
  3508. ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
  3509. }
  3510. ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
  3511. mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
  3512. }
  3513. static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
  3514. static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
  3515. /* FIXME - convert these to slabs. */
  3516. static void free_smi_msg(struct ipmi_smi_msg *msg)
  3517. {
  3518. atomic_dec(&smi_msg_inuse_count);
  3519. kfree(msg);
  3520. }
  3521. struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
  3522. {
  3523. struct ipmi_smi_msg *rv;
  3524. rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
  3525. if (rv) {
  3526. rv->done = free_smi_msg;
  3527. rv->user_data = NULL;
  3528. atomic_inc(&smi_msg_inuse_count);
  3529. }
  3530. return rv;
  3531. }
  3532. EXPORT_SYMBOL(ipmi_alloc_smi_msg);
  3533. static void free_recv_msg(struct ipmi_recv_msg *msg)
  3534. {
  3535. atomic_dec(&recv_msg_inuse_count);
  3536. kfree(msg);
  3537. }
  3538. static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
  3539. {
  3540. struct ipmi_recv_msg *rv;
  3541. rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
  3542. if (rv) {
  3543. rv->user = NULL;
  3544. rv->done = free_recv_msg;
  3545. atomic_inc(&recv_msg_inuse_count);
  3546. }
  3547. return rv;
  3548. }
  3549. void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
  3550. {
  3551. if (msg->user)
  3552. kref_put(&msg->user->refcount, free_user);
  3553. msg->done(msg);
  3554. }
  3555. EXPORT_SYMBOL(ipmi_free_recv_msg);
  3556. #ifdef CONFIG_IPMI_PANIC_EVENT
  3557. static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
  3558. {
  3559. }
  3560. static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
  3561. {
  3562. }
  3563. #ifdef CONFIG_IPMI_PANIC_STRING
  3564. static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
  3565. {
  3566. if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  3567. && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
  3568. && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
  3569. && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
  3570. /* A get event receiver command, save it. */
  3571. intf->event_receiver = msg->msg.data[1];
  3572. intf->event_receiver_lun = msg->msg.data[2] & 0x3;
  3573. }
  3574. }
  3575. static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
  3576. {
  3577. if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  3578. && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
  3579. && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
  3580. && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
  3581. /*
  3582. * A get device id command, save if we are an event
  3583. * receiver or generator.
  3584. */
  3585. intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
  3586. intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
  3587. }
  3588. }
  3589. #endif
  3590. static void send_panic_events(char *str)
  3591. {
  3592. struct kernel_ipmi_msg msg;
  3593. ipmi_smi_t intf;
  3594. unsigned char data[16];
  3595. struct ipmi_system_interface_addr *si;
  3596. struct ipmi_addr addr;
  3597. struct ipmi_smi_msg smi_msg;
  3598. struct ipmi_recv_msg recv_msg;
  3599. si = (struct ipmi_system_interface_addr *) &addr;
  3600. si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  3601. si->channel = IPMI_BMC_CHANNEL;
  3602. si->lun = 0;
  3603. /* Fill in an event telling that we have failed. */
  3604. msg.netfn = 0x04; /* Sensor or Event. */
  3605. msg.cmd = 2; /* Platform event command. */
  3606. msg.data = data;
  3607. msg.data_len = 8;
  3608. data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
  3609. data[1] = 0x03; /* This is for IPMI 1.0. */
  3610. data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
  3611. data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
  3612. data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
  3613. /*
  3614. * Put a few breadcrumbs in. Hopefully later we can add more things
  3615. * to make the panic events more useful.
  3616. */
  3617. if (str) {
  3618. data[3] = str[0];
  3619. data[6] = str[1];
  3620. data[7] = str[2];
  3621. }
  3622. smi_msg.done = dummy_smi_done_handler;
  3623. recv_msg.done = dummy_recv_done_handler;
  3624. /* For every registered interface, send the event. */
  3625. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3626. if (!intf->handlers)
  3627. /* Interface is not ready. */
  3628. continue;
  3629. intf->run_to_completion = 1;
  3630. /* Send the event announcing the panic. */
  3631. intf->handlers->set_run_to_completion(intf->send_info, 1);
  3632. i_ipmi_request(NULL,
  3633. intf,
  3634. &addr,
  3635. 0,
  3636. &msg,
  3637. intf,
  3638. &smi_msg,
  3639. &recv_msg,
  3640. 0,
  3641. intf->channels[0].address,
  3642. intf->channels[0].lun,
  3643. 0, 1); /* Don't retry, and don't wait. */
  3644. }
  3645. #ifdef CONFIG_IPMI_PANIC_STRING
  3646. /*
  3647. * On every interface, dump a bunch of OEM event holding the
  3648. * string.
  3649. */
  3650. if (!str)
  3651. return;
  3652. /* For every registered interface, send the event. */
  3653. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3654. char *p = str;
  3655. struct ipmi_ipmb_addr *ipmb;
  3656. int j;
  3657. if (intf->intf_num == -1)
  3658. /* Interface was not ready yet. */
  3659. continue;
  3660. /*
  3661. * intf_num is used as an marker to tell if the
  3662. * interface is valid. Thus we need a read barrier to
  3663. * make sure data fetched before checking intf_num
  3664. * won't be used.
  3665. */
  3666. smp_rmb();
  3667. /*
  3668. * First job here is to figure out where to send the
  3669. * OEM events. There's no way in IPMI to send OEM
  3670. * events using an event send command, so we have to
  3671. * find the SEL to put them in and stick them in
  3672. * there.
  3673. */
  3674. /* Get capabilities from the get device id. */
  3675. intf->local_sel_device = 0;
  3676. intf->local_event_generator = 0;
  3677. intf->event_receiver = 0;
  3678. /* Request the device info from the local MC. */
  3679. msg.netfn = IPMI_NETFN_APP_REQUEST;
  3680. msg.cmd = IPMI_GET_DEVICE_ID_CMD;
  3681. msg.data = NULL;
  3682. msg.data_len = 0;
  3683. intf->null_user_handler = device_id_fetcher;
  3684. i_ipmi_request(NULL,
  3685. intf,
  3686. &addr,
  3687. 0,
  3688. &msg,
  3689. intf,
  3690. &smi_msg,
  3691. &recv_msg,
  3692. 0,
  3693. intf->channels[0].address,
  3694. intf->channels[0].lun,
  3695. 0, 1); /* Don't retry, and don't wait. */
  3696. if (intf->local_event_generator) {
  3697. /* Request the event receiver from the local MC. */
  3698. msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
  3699. msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
  3700. msg.data = NULL;
  3701. msg.data_len = 0;
  3702. intf->null_user_handler = event_receiver_fetcher;
  3703. i_ipmi_request(NULL,
  3704. intf,
  3705. &addr,
  3706. 0,
  3707. &msg,
  3708. intf,
  3709. &smi_msg,
  3710. &recv_msg,
  3711. 0,
  3712. intf->channels[0].address,
  3713. intf->channels[0].lun,
  3714. 0, 1); /* no retry, and no wait. */
  3715. }
  3716. intf->null_user_handler = NULL;
  3717. /*
  3718. * Validate the event receiver. The low bit must not
  3719. * be 1 (it must be a valid IPMB address), it cannot
  3720. * be zero, and it must not be my address.
  3721. */
  3722. if (((intf->event_receiver & 1) == 0)
  3723. && (intf->event_receiver != 0)
  3724. && (intf->event_receiver != intf->channels[0].address)) {
  3725. /*
  3726. * The event receiver is valid, send an IPMB
  3727. * message.
  3728. */
  3729. ipmb = (struct ipmi_ipmb_addr *) &addr;
  3730. ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
  3731. ipmb->channel = 0; /* FIXME - is this right? */
  3732. ipmb->lun = intf->event_receiver_lun;
  3733. ipmb->slave_addr = intf->event_receiver;
  3734. } else if (intf->local_sel_device) {
  3735. /*
  3736. * The event receiver was not valid (or was
  3737. * me), but I am an SEL device, just dump it
  3738. * in my SEL.
  3739. */
  3740. si = (struct ipmi_system_interface_addr *) &addr;
  3741. si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  3742. si->channel = IPMI_BMC_CHANNEL;
  3743. si->lun = 0;
  3744. } else
  3745. continue; /* No where to send the event. */
  3746. msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
  3747. msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
  3748. msg.data = data;
  3749. msg.data_len = 16;
  3750. j = 0;
  3751. while (*p) {
  3752. int size = strlen(p);
  3753. if (size > 11)
  3754. size = 11;
  3755. data[0] = 0;
  3756. data[1] = 0;
  3757. data[2] = 0xf0; /* OEM event without timestamp. */
  3758. data[3] = intf->channels[0].address;
  3759. data[4] = j++; /* sequence # */
  3760. /*
  3761. * Always give 11 bytes, so strncpy will fill
  3762. * it with zeroes for me.
  3763. */
  3764. strncpy(data+5, p, 11);
  3765. p += size;
  3766. i_ipmi_request(NULL,
  3767. intf,
  3768. &addr,
  3769. 0,
  3770. &msg,
  3771. intf,
  3772. &smi_msg,
  3773. &recv_msg,
  3774. 0,
  3775. intf->channels[0].address,
  3776. intf->channels[0].lun,
  3777. 0, 1); /* no retry, and no wait. */
  3778. }
  3779. }
  3780. #endif /* CONFIG_IPMI_PANIC_STRING */
  3781. }
  3782. #endif /* CONFIG_IPMI_PANIC_EVENT */
  3783. static int has_panicked;
  3784. static int panic_event(struct notifier_block *this,
  3785. unsigned long event,
  3786. void *ptr)
  3787. {
  3788. ipmi_smi_t intf;
  3789. if (has_panicked)
  3790. return NOTIFY_DONE;
  3791. has_panicked = 1;
  3792. /* For every registered interface, set it to run to completion. */
  3793. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3794. if (!intf->handlers)
  3795. /* Interface is not ready. */
  3796. continue;
  3797. intf->run_to_completion = 1;
  3798. intf->handlers->set_run_to_completion(intf->send_info, 1);
  3799. }
  3800. #ifdef CONFIG_IPMI_PANIC_EVENT
  3801. send_panic_events(ptr);
  3802. #endif
  3803. return NOTIFY_DONE;
  3804. }
  3805. static struct notifier_block panic_block = {
  3806. .notifier_call = panic_event,
  3807. .next = NULL,
  3808. .priority = 200 /* priority: INT_MAX >= x >= 0 */
  3809. };
  3810. static int ipmi_init_msghandler(void)
  3811. {
  3812. int rv;
  3813. if (initialized)
  3814. return 0;
  3815. rv = driver_register(&ipmidriver.driver);
  3816. if (rv) {
  3817. printk(KERN_ERR PFX "Could not register IPMI driver\n");
  3818. return rv;
  3819. }
  3820. printk(KERN_INFO "ipmi message handler version "
  3821. IPMI_DRIVER_VERSION "\n");
  3822. #ifdef CONFIG_PROC_FS
  3823. proc_ipmi_root = proc_mkdir("ipmi", NULL);
  3824. if (!proc_ipmi_root) {
  3825. printk(KERN_ERR PFX "Unable to create IPMI proc dir");
  3826. return -ENOMEM;
  3827. }
  3828. #endif /* CONFIG_PROC_FS */
  3829. setup_timer(&ipmi_timer, ipmi_timeout, 0);
  3830. mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
  3831. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  3832. initialized = 1;
  3833. return 0;
  3834. }
  3835. static __init int ipmi_init_msghandler_mod(void)
  3836. {
  3837. ipmi_init_msghandler();
  3838. return 0;
  3839. }
  3840. static __exit void cleanup_ipmi(void)
  3841. {
  3842. int count;
  3843. if (!initialized)
  3844. return;
  3845. atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
  3846. /*
  3847. * This can't be called if any interfaces exist, so no worry
  3848. * about shutting down the interfaces.
  3849. */
  3850. /*
  3851. * Tell the timer to stop, then wait for it to stop. This
  3852. * avoids problems with race conditions removing the timer
  3853. * here.
  3854. */
  3855. atomic_inc(&stop_operation);
  3856. del_timer_sync(&ipmi_timer);
  3857. #ifdef CONFIG_PROC_FS
  3858. remove_proc_entry(proc_ipmi_root->name, NULL);
  3859. #endif /* CONFIG_PROC_FS */
  3860. driver_unregister(&ipmidriver.driver);
  3861. initialized = 0;
  3862. /* Check for buffer leaks. */
  3863. count = atomic_read(&smi_msg_inuse_count);
  3864. if (count != 0)
  3865. printk(KERN_WARNING PFX "SMI message count %d at exit\n",
  3866. count);
  3867. count = atomic_read(&recv_msg_inuse_count);
  3868. if (count != 0)
  3869. printk(KERN_WARNING PFX "recv message count %d at exit\n",
  3870. count);
  3871. }
  3872. module_exit(cleanup_ipmi);
  3873. module_init(ipmi_init_msghandler_mod);
  3874. MODULE_LICENSE("GPL");
  3875. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  3876. MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
  3877. " interface.");
  3878. MODULE_VERSION(IPMI_DRIVER_VERSION);