ipmi_msghandler.c 118 KB

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