ipmi_msghandler.c 116 KB

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