ipmi_si_intf.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561
  1. /*
  2. * ipmi_si.c
  3. *
  4. * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
  5. * BT).
  6. *
  7. * Author: MontaVista Software, Inc.
  8. * Corey Minyard <minyard@mvista.com>
  9. * source@mvista.com
  10. *
  11. * Copyright 2002 MontaVista Software Inc.
  12. * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. *
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  28. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  29. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * You should have received a copy of the GNU General Public License along
  32. * with this program; if not, write to the Free Software Foundation, Inc.,
  33. * 675 Mass Ave, Cambridge, MA 02139, USA.
  34. */
  35. /*
  36. * This file holds the "policy" for the interface to the SMI state
  37. * machine. It does the configuration, handles timers and interrupts,
  38. * and drives the real SMI state machine.
  39. */
  40. #include <linux/module.h>
  41. #include <linux/moduleparam.h>
  42. #include <asm/system.h>
  43. #include <linux/sched.h>
  44. #include <linux/timer.h>
  45. #include <linux/errno.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/slab.h>
  48. #include <linux/delay.h>
  49. #include <linux/list.h>
  50. #include <linux/pci.h>
  51. #include <linux/ioport.h>
  52. #include <linux/notifier.h>
  53. #include <linux/mutex.h>
  54. #include <linux/kthread.h>
  55. #include <asm/irq.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/rcupdate.h>
  58. #include <linux/ipmi_smi.h>
  59. #include <asm/io.h>
  60. #include "ipmi_si_sm.h"
  61. #include <linux/init.h>
  62. #include <linux/dmi.h>
  63. #include <linux/string.h>
  64. #include <linux/ctype.h>
  65. #include <linux/pnp.h>
  66. #ifdef CONFIG_PPC_OF
  67. #include <linux/of_device.h>
  68. #include <linux/of_platform.h>
  69. #endif
  70. #define PFX "ipmi_si: "
  71. /* Measure times between events in the driver. */
  72. #undef DEBUG_TIMING
  73. /* Call every 10 ms. */
  74. #define SI_TIMEOUT_TIME_USEC 10000
  75. #define SI_USEC_PER_JIFFY (1000000/HZ)
  76. #define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
  77. #define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
  78. short timeout */
  79. enum si_intf_state {
  80. SI_NORMAL,
  81. SI_GETTING_FLAGS,
  82. SI_GETTING_EVENTS,
  83. SI_CLEARING_FLAGS,
  84. SI_CLEARING_FLAGS_THEN_SET_IRQ,
  85. SI_GETTING_MESSAGES,
  86. SI_ENABLE_INTERRUPTS1,
  87. SI_ENABLE_INTERRUPTS2,
  88. SI_DISABLE_INTERRUPTS1,
  89. SI_DISABLE_INTERRUPTS2
  90. /* FIXME - add watchdog stuff. */
  91. };
  92. /* Some BT-specific defines we need here. */
  93. #define IPMI_BT_INTMASK_REG 2
  94. #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
  95. #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
  96. enum si_type {
  97. SI_KCS, SI_SMIC, SI_BT
  98. };
  99. static char *si_to_str[] = { "kcs", "smic", "bt" };
  100. enum ipmi_addr_src {
  101. SI_INVALID = 0, SI_HOTMOD, SI_HARDCODED, SI_SPMI, SI_ACPI, SI_SMBIOS,
  102. SI_PCI, SI_DEVICETREE, SI_DEFAULT
  103. };
  104. static char *ipmi_addr_src_to_str[] = { NULL, "hotmod", "hardcoded", "SPMI",
  105. "ACPI", "SMBIOS", "PCI",
  106. "device-tree", "default" };
  107. #define DEVICE_NAME "ipmi_si"
  108. static struct platform_driver ipmi_driver = {
  109. .driver = {
  110. .name = DEVICE_NAME,
  111. .bus = &platform_bus_type
  112. }
  113. };
  114. /*
  115. * Indexes into stats[] in smi_info below.
  116. */
  117. enum si_stat_indexes {
  118. /*
  119. * Number of times the driver requested a timer while an operation
  120. * was in progress.
  121. */
  122. SI_STAT_short_timeouts = 0,
  123. /*
  124. * Number of times the driver requested a timer while nothing was in
  125. * progress.
  126. */
  127. SI_STAT_long_timeouts,
  128. /* Number of times the interface was idle while being polled. */
  129. SI_STAT_idles,
  130. /* Number of interrupts the driver handled. */
  131. SI_STAT_interrupts,
  132. /* Number of time the driver got an ATTN from the hardware. */
  133. SI_STAT_attentions,
  134. /* Number of times the driver requested flags from the hardware. */
  135. SI_STAT_flag_fetches,
  136. /* Number of times the hardware didn't follow the state machine. */
  137. SI_STAT_hosed_count,
  138. /* Number of completed messages. */
  139. SI_STAT_complete_transactions,
  140. /* Number of IPMI events received from the hardware. */
  141. SI_STAT_events,
  142. /* Number of watchdog pretimeouts. */
  143. SI_STAT_watchdog_pretimeouts,
  144. /* Number of asyncronous messages received. */
  145. SI_STAT_incoming_messages,
  146. /* This *must* remain last, add new values above this. */
  147. SI_NUM_STATS
  148. };
  149. struct smi_info {
  150. int intf_num;
  151. ipmi_smi_t intf;
  152. struct si_sm_data *si_sm;
  153. struct si_sm_handlers *handlers;
  154. enum si_type si_type;
  155. spinlock_t si_lock;
  156. spinlock_t msg_lock;
  157. struct list_head xmit_msgs;
  158. struct list_head hp_xmit_msgs;
  159. struct ipmi_smi_msg *curr_msg;
  160. enum si_intf_state si_state;
  161. /*
  162. * Used to handle the various types of I/O that can occur with
  163. * IPMI
  164. */
  165. struct si_sm_io io;
  166. int (*io_setup)(struct smi_info *info);
  167. void (*io_cleanup)(struct smi_info *info);
  168. int (*irq_setup)(struct smi_info *info);
  169. void (*irq_cleanup)(struct smi_info *info);
  170. unsigned int io_size;
  171. enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
  172. void (*addr_source_cleanup)(struct smi_info *info);
  173. void *addr_source_data;
  174. /*
  175. * Per-OEM handler, called from handle_flags(). Returns 1
  176. * when handle_flags() needs to be re-run or 0 indicating it
  177. * set si_state itself.
  178. */
  179. int (*oem_data_avail_handler)(struct smi_info *smi_info);
  180. /*
  181. * Flags from the last GET_MSG_FLAGS command, used when an ATTN
  182. * is set to hold the flags until we are done handling everything
  183. * from the flags.
  184. */
  185. #define RECEIVE_MSG_AVAIL 0x01
  186. #define EVENT_MSG_BUFFER_FULL 0x02
  187. #define WDT_PRE_TIMEOUT_INT 0x08
  188. #define OEM0_DATA_AVAIL 0x20
  189. #define OEM1_DATA_AVAIL 0x40
  190. #define OEM2_DATA_AVAIL 0x80
  191. #define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
  192. OEM1_DATA_AVAIL | \
  193. OEM2_DATA_AVAIL)
  194. unsigned char msg_flags;
  195. /* Does the BMC have an event buffer? */
  196. char has_event_buffer;
  197. /*
  198. * If set to true, this will request events the next time the
  199. * state machine is idle.
  200. */
  201. atomic_t req_events;
  202. /*
  203. * If true, run the state machine to completion on every send
  204. * call. Generally used after a panic to make sure stuff goes
  205. * out.
  206. */
  207. int run_to_completion;
  208. /* The I/O port of an SI interface. */
  209. int port;
  210. /*
  211. * The space between start addresses of the two ports. For
  212. * instance, if the first port is 0xca2 and the spacing is 4, then
  213. * the second port is 0xca6.
  214. */
  215. unsigned int spacing;
  216. /* zero if no irq; */
  217. int irq;
  218. /* The timer for this si. */
  219. struct timer_list si_timer;
  220. /* The time (in jiffies) the last timeout occurred at. */
  221. unsigned long last_timeout_jiffies;
  222. /* Used to gracefully stop the timer without race conditions. */
  223. atomic_t stop_operation;
  224. /*
  225. * The driver will disable interrupts when it gets into a
  226. * situation where it cannot handle messages due to lack of
  227. * memory. Once that situation clears up, it will re-enable
  228. * interrupts.
  229. */
  230. int interrupt_disabled;
  231. /* From the get device id response... */
  232. struct ipmi_device_id device_id;
  233. /* Driver model stuff. */
  234. struct device *dev;
  235. struct platform_device *pdev;
  236. /*
  237. * True if we allocated the device, false if it came from
  238. * someplace else (like PCI).
  239. */
  240. int dev_registered;
  241. /* Slave address, could be reported from DMI. */
  242. unsigned char slave_addr;
  243. /* Counters and things for the proc filesystem. */
  244. atomic_t stats[SI_NUM_STATS];
  245. struct task_struct *thread;
  246. struct list_head link;
  247. };
  248. #define smi_inc_stat(smi, stat) \
  249. atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
  250. #define smi_get_stat(smi, stat) \
  251. ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
  252. #define SI_MAX_PARMS 4
  253. static int force_kipmid[SI_MAX_PARMS];
  254. static int num_force_kipmid;
  255. #ifdef CONFIG_PCI
  256. static int pci_registered;
  257. #endif
  258. #ifdef CONFIG_ACPI
  259. static int pnp_registered;
  260. #endif
  261. #ifdef CONFIG_PPC_OF
  262. static int of_registered;
  263. #endif
  264. static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];
  265. static int num_max_busy_us;
  266. static int unload_when_empty = 1;
  267. static int add_smi(struct smi_info *smi);
  268. static int try_smi_init(struct smi_info *smi);
  269. static void cleanup_one_si(struct smi_info *to_clean);
  270. static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
  271. static int register_xaction_notifier(struct notifier_block *nb)
  272. {
  273. return atomic_notifier_chain_register(&xaction_notifier_list, nb);
  274. }
  275. static void deliver_recv_msg(struct smi_info *smi_info,
  276. struct ipmi_smi_msg *msg)
  277. {
  278. /* Deliver the message to the upper layer with the lock
  279. released. */
  280. if (smi_info->run_to_completion) {
  281. ipmi_smi_msg_received(smi_info->intf, msg);
  282. } else {
  283. spin_unlock(&(smi_info->si_lock));
  284. ipmi_smi_msg_received(smi_info->intf, msg);
  285. spin_lock(&(smi_info->si_lock));
  286. }
  287. }
  288. static void return_hosed_msg(struct smi_info *smi_info, int cCode)
  289. {
  290. struct ipmi_smi_msg *msg = smi_info->curr_msg;
  291. if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
  292. cCode = IPMI_ERR_UNSPECIFIED;
  293. /* else use it as is */
  294. /* Make it a reponse */
  295. msg->rsp[0] = msg->data[0] | 4;
  296. msg->rsp[1] = msg->data[1];
  297. msg->rsp[2] = cCode;
  298. msg->rsp_size = 3;
  299. smi_info->curr_msg = NULL;
  300. deliver_recv_msg(smi_info, msg);
  301. }
  302. static enum si_sm_result start_next_msg(struct smi_info *smi_info)
  303. {
  304. int rv;
  305. struct list_head *entry = NULL;
  306. #ifdef DEBUG_TIMING
  307. struct timeval t;
  308. #endif
  309. /*
  310. * No need to save flags, we aleady have interrupts off and we
  311. * already hold the SMI lock.
  312. */
  313. if (!smi_info->run_to_completion)
  314. spin_lock(&(smi_info->msg_lock));
  315. /* Pick the high priority queue first. */
  316. if (!list_empty(&(smi_info->hp_xmit_msgs))) {
  317. entry = smi_info->hp_xmit_msgs.next;
  318. } else if (!list_empty(&(smi_info->xmit_msgs))) {
  319. entry = smi_info->xmit_msgs.next;
  320. }
  321. if (!entry) {
  322. smi_info->curr_msg = NULL;
  323. rv = SI_SM_IDLE;
  324. } else {
  325. int err;
  326. list_del(entry);
  327. smi_info->curr_msg = list_entry(entry,
  328. struct ipmi_smi_msg,
  329. link);
  330. #ifdef DEBUG_TIMING
  331. do_gettimeofday(&t);
  332. printk(KERN_DEBUG "**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  333. #endif
  334. err = atomic_notifier_call_chain(&xaction_notifier_list,
  335. 0, smi_info);
  336. if (err & NOTIFY_STOP_MASK) {
  337. rv = SI_SM_CALL_WITHOUT_DELAY;
  338. goto out;
  339. }
  340. err = smi_info->handlers->start_transaction(
  341. smi_info->si_sm,
  342. smi_info->curr_msg->data,
  343. smi_info->curr_msg->data_size);
  344. if (err)
  345. return_hosed_msg(smi_info, err);
  346. rv = SI_SM_CALL_WITHOUT_DELAY;
  347. }
  348. out:
  349. if (!smi_info->run_to_completion)
  350. spin_unlock(&(smi_info->msg_lock));
  351. return rv;
  352. }
  353. static void start_enable_irq(struct smi_info *smi_info)
  354. {
  355. unsigned char msg[2];
  356. /*
  357. * If we are enabling interrupts, we have to tell the
  358. * BMC to use them.
  359. */
  360. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  361. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  362. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  363. smi_info->si_state = SI_ENABLE_INTERRUPTS1;
  364. }
  365. static void start_disable_irq(struct smi_info *smi_info)
  366. {
  367. unsigned char msg[2];
  368. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  369. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  370. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  371. smi_info->si_state = SI_DISABLE_INTERRUPTS1;
  372. }
  373. static void start_clear_flags(struct smi_info *smi_info)
  374. {
  375. unsigned char msg[3];
  376. /* Make sure the watchdog pre-timeout flag is not set at startup. */
  377. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  378. msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
  379. msg[2] = WDT_PRE_TIMEOUT_INT;
  380. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
  381. smi_info->si_state = SI_CLEARING_FLAGS;
  382. }
  383. /*
  384. * When we have a situtaion where we run out of memory and cannot
  385. * allocate messages, we just leave them in the BMC and run the system
  386. * polled until we can allocate some memory. Once we have some
  387. * memory, we will re-enable the interrupt.
  388. */
  389. static inline void disable_si_irq(struct smi_info *smi_info)
  390. {
  391. if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
  392. start_disable_irq(smi_info);
  393. smi_info->interrupt_disabled = 1;
  394. if (!atomic_read(&smi_info->stop_operation))
  395. mod_timer(&smi_info->si_timer,
  396. jiffies + SI_TIMEOUT_JIFFIES);
  397. }
  398. }
  399. static inline void enable_si_irq(struct smi_info *smi_info)
  400. {
  401. if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
  402. start_enable_irq(smi_info);
  403. smi_info->interrupt_disabled = 0;
  404. }
  405. }
  406. static void handle_flags(struct smi_info *smi_info)
  407. {
  408. retry:
  409. if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
  410. /* Watchdog pre-timeout */
  411. smi_inc_stat(smi_info, watchdog_pretimeouts);
  412. start_clear_flags(smi_info);
  413. smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
  414. spin_unlock(&(smi_info->si_lock));
  415. ipmi_smi_watchdog_pretimeout(smi_info->intf);
  416. spin_lock(&(smi_info->si_lock));
  417. } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
  418. /* Messages available. */
  419. smi_info->curr_msg = ipmi_alloc_smi_msg();
  420. if (!smi_info->curr_msg) {
  421. disable_si_irq(smi_info);
  422. smi_info->si_state = SI_NORMAL;
  423. return;
  424. }
  425. enable_si_irq(smi_info);
  426. smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  427. smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
  428. smi_info->curr_msg->data_size = 2;
  429. smi_info->handlers->start_transaction(
  430. smi_info->si_sm,
  431. smi_info->curr_msg->data,
  432. smi_info->curr_msg->data_size);
  433. smi_info->si_state = SI_GETTING_MESSAGES;
  434. } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
  435. /* Events available. */
  436. smi_info->curr_msg = ipmi_alloc_smi_msg();
  437. if (!smi_info->curr_msg) {
  438. disable_si_irq(smi_info);
  439. smi_info->si_state = SI_NORMAL;
  440. return;
  441. }
  442. enable_si_irq(smi_info);
  443. smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  444. smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
  445. smi_info->curr_msg->data_size = 2;
  446. smi_info->handlers->start_transaction(
  447. smi_info->si_sm,
  448. smi_info->curr_msg->data,
  449. smi_info->curr_msg->data_size);
  450. smi_info->si_state = SI_GETTING_EVENTS;
  451. } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
  452. smi_info->oem_data_avail_handler) {
  453. if (smi_info->oem_data_avail_handler(smi_info))
  454. goto retry;
  455. } else
  456. smi_info->si_state = SI_NORMAL;
  457. }
  458. static void handle_transaction_done(struct smi_info *smi_info)
  459. {
  460. struct ipmi_smi_msg *msg;
  461. #ifdef DEBUG_TIMING
  462. struct timeval t;
  463. do_gettimeofday(&t);
  464. printk(KERN_DEBUG "**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  465. #endif
  466. switch (smi_info->si_state) {
  467. case SI_NORMAL:
  468. if (!smi_info->curr_msg)
  469. break;
  470. smi_info->curr_msg->rsp_size
  471. = smi_info->handlers->get_result(
  472. smi_info->si_sm,
  473. smi_info->curr_msg->rsp,
  474. IPMI_MAX_MSG_LENGTH);
  475. /*
  476. * Do this here becase deliver_recv_msg() releases the
  477. * lock, and a new message can be put in during the
  478. * time the lock is released.
  479. */
  480. msg = smi_info->curr_msg;
  481. smi_info->curr_msg = NULL;
  482. deliver_recv_msg(smi_info, msg);
  483. break;
  484. case SI_GETTING_FLAGS:
  485. {
  486. unsigned char msg[4];
  487. unsigned int len;
  488. /* We got the flags from the SMI, now handle them. */
  489. len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  490. if (msg[2] != 0) {
  491. /* Error fetching flags, just give up for now. */
  492. smi_info->si_state = SI_NORMAL;
  493. } else if (len < 4) {
  494. /*
  495. * Hmm, no flags. That's technically illegal, but
  496. * don't use uninitialized data.
  497. */
  498. smi_info->si_state = SI_NORMAL;
  499. } else {
  500. smi_info->msg_flags = msg[3];
  501. handle_flags(smi_info);
  502. }
  503. break;
  504. }
  505. case SI_CLEARING_FLAGS:
  506. case SI_CLEARING_FLAGS_THEN_SET_IRQ:
  507. {
  508. unsigned char msg[3];
  509. /* We cleared the flags. */
  510. smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
  511. if (msg[2] != 0) {
  512. /* Error clearing flags */
  513. dev_warn(smi_info->dev,
  514. "Error clearing flags: %2.2x\n", msg[2]);
  515. }
  516. if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
  517. start_enable_irq(smi_info);
  518. else
  519. smi_info->si_state = SI_NORMAL;
  520. break;
  521. }
  522. case SI_GETTING_EVENTS:
  523. {
  524. smi_info->curr_msg->rsp_size
  525. = smi_info->handlers->get_result(
  526. smi_info->si_sm,
  527. smi_info->curr_msg->rsp,
  528. IPMI_MAX_MSG_LENGTH);
  529. /*
  530. * Do this here becase deliver_recv_msg() releases the
  531. * lock, and a new message can be put in during the
  532. * time the lock is released.
  533. */
  534. msg = smi_info->curr_msg;
  535. smi_info->curr_msg = NULL;
  536. if (msg->rsp[2] != 0) {
  537. /* Error getting event, probably done. */
  538. msg->done(msg);
  539. /* Take off the event flag. */
  540. smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
  541. handle_flags(smi_info);
  542. } else {
  543. smi_inc_stat(smi_info, events);
  544. /*
  545. * Do this before we deliver the message
  546. * because delivering the message releases the
  547. * lock and something else can mess with the
  548. * state.
  549. */
  550. handle_flags(smi_info);
  551. deliver_recv_msg(smi_info, msg);
  552. }
  553. break;
  554. }
  555. case SI_GETTING_MESSAGES:
  556. {
  557. smi_info->curr_msg->rsp_size
  558. = smi_info->handlers->get_result(
  559. smi_info->si_sm,
  560. smi_info->curr_msg->rsp,
  561. IPMI_MAX_MSG_LENGTH);
  562. /*
  563. * Do this here becase deliver_recv_msg() releases the
  564. * lock, and a new message can be put in during the
  565. * time the lock is released.
  566. */
  567. msg = smi_info->curr_msg;
  568. smi_info->curr_msg = NULL;
  569. if (msg->rsp[2] != 0) {
  570. /* Error getting event, probably done. */
  571. msg->done(msg);
  572. /* Take off the msg flag. */
  573. smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
  574. handle_flags(smi_info);
  575. } else {
  576. smi_inc_stat(smi_info, incoming_messages);
  577. /*
  578. * Do this before we deliver the message
  579. * because delivering the message releases the
  580. * lock and something else can mess with the
  581. * state.
  582. */
  583. handle_flags(smi_info);
  584. deliver_recv_msg(smi_info, msg);
  585. }
  586. break;
  587. }
  588. case SI_ENABLE_INTERRUPTS1:
  589. {
  590. unsigned char msg[4];
  591. /* We got the flags from the SMI, now handle them. */
  592. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  593. if (msg[2] != 0) {
  594. dev_warn(smi_info->dev, "Could not enable interrupts"
  595. ", failed get, using polled mode.\n");
  596. smi_info->si_state = SI_NORMAL;
  597. } else {
  598. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  599. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  600. msg[2] = (msg[3] |
  601. IPMI_BMC_RCV_MSG_INTR |
  602. IPMI_BMC_EVT_MSG_INTR);
  603. smi_info->handlers->start_transaction(
  604. smi_info->si_sm, msg, 3);
  605. smi_info->si_state = SI_ENABLE_INTERRUPTS2;
  606. }
  607. break;
  608. }
  609. case SI_ENABLE_INTERRUPTS2:
  610. {
  611. unsigned char msg[4];
  612. /* We got the flags from the SMI, now handle them. */
  613. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  614. if (msg[2] != 0)
  615. dev_warn(smi_info->dev, "Could not enable interrupts"
  616. ", failed set, using polled mode.\n");
  617. else
  618. smi_info->interrupt_disabled = 0;
  619. smi_info->si_state = SI_NORMAL;
  620. break;
  621. }
  622. case SI_DISABLE_INTERRUPTS1:
  623. {
  624. unsigned char msg[4];
  625. /* We got the flags from the SMI, now handle them. */
  626. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  627. if (msg[2] != 0) {
  628. dev_warn(smi_info->dev, "Could not disable interrupts"
  629. ", failed get.\n");
  630. smi_info->si_state = SI_NORMAL;
  631. } else {
  632. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  633. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  634. msg[2] = (msg[3] &
  635. ~(IPMI_BMC_RCV_MSG_INTR |
  636. IPMI_BMC_EVT_MSG_INTR));
  637. smi_info->handlers->start_transaction(
  638. smi_info->si_sm, msg, 3);
  639. smi_info->si_state = SI_DISABLE_INTERRUPTS2;
  640. }
  641. break;
  642. }
  643. case SI_DISABLE_INTERRUPTS2:
  644. {
  645. unsigned char msg[4];
  646. /* We got the flags from the SMI, now handle them. */
  647. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  648. if (msg[2] != 0) {
  649. dev_warn(smi_info->dev, "Could not disable interrupts"
  650. ", failed set.\n");
  651. }
  652. smi_info->si_state = SI_NORMAL;
  653. break;
  654. }
  655. }
  656. }
  657. /*
  658. * Called on timeouts and events. Timeouts should pass the elapsed
  659. * time, interrupts should pass in zero. Must be called with
  660. * si_lock held and interrupts disabled.
  661. */
  662. static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
  663. int time)
  664. {
  665. enum si_sm_result si_sm_result;
  666. restart:
  667. /*
  668. * There used to be a loop here that waited a little while
  669. * (around 25us) before giving up. That turned out to be
  670. * pointless, the minimum delays I was seeing were in the 300us
  671. * range, which is far too long to wait in an interrupt. So
  672. * we just run until the state machine tells us something
  673. * happened or it needs a delay.
  674. */
  675. si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
  676. time = 0;
  677. while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
  678. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  679. if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
  680. smi_inc_stat(smi_info, complete_transactions);
  681. handle_transaction_done(smi_info);
  682. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  683. } else if (si_sm_result == SI_SM_HOSED) {
  684. smi_inc_stat(smi_info, hosed_count);
  685. /*
  686. * Do the before return_hosed_msg, because that
  687. * releases the lock.
  688. */
  689. smi_info->si_state = SI_NORMAL;
  690. if (smi_info->curr_msg != NULL) {
  691. /*
  692. * If we were handling a user message, format
  693. * a response to send to the upper layer to
  694. * tell it about the error.
  695. */
  696. return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
  697. }
  698. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  699. }
  700. /*
  701. * We prefer handling attn over new messages. But don't do
  702. * this if there is not yet an upper layer to handle anything.
  703. */
  704. if (likely(smi_info->intf) && si_sm_result == SI_SM_ATTN) {
  705. unsigned char msg[2];
  706. smi_inc_stat(smi_info, attentions);
  707. /*
  708. * Got a attn, send down a get message flags to see
  709. * what's causing it. It would be better to handle
  710. * this in the upper layer, but due to the way
  711. * interrupts work with the SMI, that's not really
  712. * possible.
  713. */
  714. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  715. msg[1] = IPMI_GET_MSG_FLAGS_CMD;
  716. smi_info->handlers->start_transaction(
  717. smi_info->si_sm, msg, 2);
  718. smi_info->si_state = SI_GETTING_FLAGS;
  719. goto restart;
  720. }
  721. /* If we are currently idle, try to start the next message. */
  722. if (si_sm_result == SI_SM_IDLE) {
  723. smi_inc_stat(smi_info, idles);
  724. si_sm_result = start_next_msg(smi_info);
  725. if (si_sm_result != SI_SM_IDLE)
  726. goto restart;
  727. }
  728. if ((si_sm_result == SI_SM_IDLE)
  729. && (atomic_read(&smi_info->req_events))) {
  730. /*
  731. * We are idle and the upper layer requested that I fetch
  732. * events, so do so.
  733. */
  734. atomic_set(&smi_info->req_events, 0);
  735. smi_info->curr_msg = ipmi_alloc_smi_msg();
  736. if (!smi_info->curr_msg)
  737. goto out;
  738. smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  739. smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
  740. smi_info->curr_msg->data_size = 2;
  741. smi_info->handlers->start_transaction(
  742. smi_info->si_sm,
  743. smi_info->curr_msg->data,
  744. smi_info->curr_msg->data_size);
  745. smi_info->si_state = SI_GETTING_EVENTS;
  746. goto restart;
  747. }
  748. out:
  749. return si_sm_result;
  750. }
  751. static void sender(void *send_info,
  752. struct ipmi_smi_msg *msg,
  753. int priority)
  754. {
  755. struct smi_info *smi_info = send_info;
  756. enum si_sm_result result;
  757. unsigned long flags;
  758. #ifdef DEBUG_TIMING
  759. struct timeval t;
  760. #endif
  761. if (atomic_read(&smi_info->stop_operation)) {
  762. msg->rsp[0] = msg->data[0] | 4;
  763. msg->rsp[1] = msg->data[1];
  764. msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
  765. msg->rsp_size = 3;
  766. deliver_recv_msg(smi_info, msg);
  767. return;
  768. }
  769. #ifdef DEBUG_TIMING
  770. do_gettimeofday(&t);
  771. printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  772. #endif
  773. mod_timer(&smi_info->si_timer, jiffies + SI_TIMEOUT_JIFFIES);
  774. if (smi_info->thread)
  775. wake_up_process(smi_info->thread);
  776. if (smi_info->run_to_completion) {
  777. /*
  778. * If we are running to completion, then throw it in
  779. * the list and run transactions until everything is
  780. * clear. Priority doesn't matter here.
  781. */
  782. /*
  783. * Run to completion means we are single-threaded, no
  784. * need for locks.
  785. */
  786. list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
  787. result = smi_event_handler(smi_info, 0);
  788. while (result != SI_SM_IDLE) {
  789. udelay(SI_SHORT_TIMEOUT_USEC);
  790. result = smi_event_handler(smi_info,
  791. SI_SHORT_TIMEOUT_USEC);
  792. }
  793. return;
  794. }
  795. spin_lock_irqsave(&smi_info->msg_lock, flags);
  796. if (priority > 0)
  797. list_add_tail(&msg->link, &smi_info->hp_xmit_msgs);
  798. else
  799. list_add_tail(&msg->link, &smi_info->xmit_msgs);
  800. spin_unlock_irqrestore(&smi_info->msg_lock, flags);
  801. spin_lock_irqsave(&smi_info->si_lock, flags);
  802. if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL)
  803. start_next_msg(smi_info);
  804. spin_unlock_irqrestore(&smi_info->si_lock, flags);
  805. }
  806. static void set_run_to_completion(void *send_info, int i_run_to_completion)
  807. {
  808. struct smi_info *smi_info = send_info;
  809. enum si_sm_result result;
  810. smi_info->run_to_completion = i_run_to_completion;
  811. if (i_run_to_completion) {
  812. result = smi_event_handler(smi_info, 0);
  813. while (result != SI_SM_IDLE) {
  814. udelay(SI_SHORT_TIMEOUT_USEC);
  815. result = smi_event_handler(smi_info,
  816. SI_SHORT_TIMEOUT_USEC);
  817. }
  818. }
  819. }
  820. /*
  821. * Use -1 in the nsec value of the busy waiting timespec to tell that
  822. * we are spinning in kipmid looking for something and not delaying
  823. * between checks
  824. */
  825. static inline void ipmi_si_set_not_busy(struct timespec *ts)
  826. {
  827. ts->tv_nsec = -1;
  828. }
  829. static inline int ipmi_si_is_busy(struct timespec *ts)
  830. {
  831. return ts->tv_nsec != -1;
  832. }
  833. static int ipmi_thread_busy_wait(enum si_sm_result smi_result,
  834. const struct smi_info *smi_info,
  835. struct timespec *busy_until)
  836. {
  837. unsigned int max_busy_us = 0;
  838. if (smi_info->intf_num < num_max_busy_us)
  839. max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
  840. if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
  841. ipmi_si_set_not_busy(busy_until);
  842. else if (!ipmi_si_is_busy(busy_until)) {
  843. getnstimeofday(busy_until);
  844. timespec_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
  845. } else {
  846. struct timespec now;
  847. getnstimeofday(&now);
  848. if (unlikely(timespec_compare(&now, busy_until) > 0)) {
  849. ipmi_si_set_not_busy(busy_until);
  850. return 0;
  851. }
  852. }
  853. return 1;
  854. }
  855. /*
  856. * A busy-waiting loop for speeding up IPMI operation.
  857. *
  858. * Lousy hardware makes this hard. This is only enabled for systems
  859. * that are not BT and do not have interrupts. It starts spinning
  860. * when an operation is complete or until max_busy tells it to stop
  861. * (if that is enabled). See the paragraph on kimid_max_busy_us in
  862. * Documentation/IPMI.txt for details.
  863. */
  864. static int ipmi_thread(void *data)
  865. {
  866. struct smi_info *smi_info = data;
  867. unsigned long flags;
  868. enum si_sm_result smi_result;
  869. struct timespec busy_until;
  870. ipmi_si_set_not_busy(&busy_until);
  871. set_user_nice(current, 19);
  872. while (!kthread_should_stop()) {
  873. int busy_wait;
  874. spin_lock_irqsave(&(smi_info->si_lock), flags);
  875. smi_result = smi_event_handler(smi_info, 0);
  876. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  877. busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
  878. &busy_until);
  879. if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
  880. ; /* do nothing */
  881. else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
  882. schedule();
  883. else if (smi_result == SI_SM_IDLE)
  884. schedule_timeout_interruptible(100);
  885. else
  886. schedule_timeout_interruptible(1);
  887. }
  888. return 0;
  889. }
  890. static void poll(void *send_info)
  891. {
  892. struct smi_info *smi_info = send_info;
  893. unsigned long flags;
  894. /*
  895. * Make sure there is some delay in the poll loop so we can
  896. * drive time forward and timeout things.
  897. */
  898. udelay(10);
  899. spin_lock_irqsave(&smi_info->si_lock, flags);
  900. smi_event_handler(smi_info, 10);
  901. spin_unlock_irqrestore(&smi_info->si_lock, flags);
  902. }
  903. static void request_events(void *send_info)
  904. {
  905. struct smi_info *smi_info = send_info;
  906. if (atomic_read(&smi_info->stop_operation) ||
  907. !smi_info->has_event_buffer)
  908. return;
  909. atomic_set(&smi_info->req_events, 1);
  910. }
  911. static int initialized;
  912. static void smi_timeout(unsigned long data)
  913. {
  914. struct smi_info *smi_info = (struct smi_info *) data;
  915. enum si_sm_result smi_result;
  916. unsigned long flags;
  917. unsigned long jiffies_now;
  918. long time_diff;
  919. long timeout;
  920. #ifdef DEBUG_TIMING
  921. struct timeval t;
  922. #endif
  923. spin_lock_irqsave(&(smi_info->si_lock), flags);
  924. #ifdef DEBUG_TIMING
  925. do_gettimeofday(&t);
  926. printk(KERN_DEBUG "**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  927. #endif
  928. jiffies_now = jiffies;
  929. time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
  930. * SI_USEC_PER_JIFFY);
  931. smi_result = smi_event_handler(smi_info, time_diff);
  932. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  933. smi_info->last_timeout_jiffies = jiffies_now;
  934. if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
  935. /* Running with interrupts, only do long timeouts. */
  936. timeout = jiffies + SI_TIMEOUT_JIFFIES;
  937. smi_inc_stat(smi_info, long_timeouts);
  938. goto do_mod_timer;
  939. }
  940. /*
  941. * If the state machine asks for a short delay, then shorten
  942. * the timer timeout.
  943. */
  944. if (smi_result == SI_SM_CALL_WITH_DELAY) {
  945. smi_inc_stat(smi_info, short_timeouts);
  946. timeout = jiffies + 1;
  947. } else {
  948. smi_inc_stat(smi_info, long_timeouts);
  949. timeout = jiffies + SI_TIMEOUT_JIFFIES;
  950. }
  951. do_mod_timer:
  952. if (smi_result != SI_SM_IDLE)
  953. mod_timer(&(smi_info->si_timer), timeout);
  954. }
  955. static irqreturn_t si_irq_handler(int irq, void *data)
  956. {
  957. struct smi_info *smi_info = data;
  958. unsigned long flags;
  959. #ifdef DEBUG_TIMING
  960. struct timeval t;
  961. #endif
  962. spin_lock_irqsave(&(smi_info->si_lock), flags);
  963. smi_inc_stat(smi_info, interrupts);
  964. #ifdef DEBUG_TIMING
  965. do_gettimeofday(&t);
  966. printk(KERN_DEBUG "**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  967. #endif
  968. smi_event_handler(smi_info, 0);
  969. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  970. return IRQ_HANDLED;
  971. }
  972. static irqreturn_t si_bt_irq_handler(int irq, void *data)
  973. {
  974. struct smi_info *smi_info = data;
  975. /* We need to clear the IRQ flag for the BT interface. */
  976. smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
  977. IPMI_BT_INTMASK_CLEAR_IRQ_BIT
  978. | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  979. return si_irq_handler(irq, data);
  980. }
  981. static int smi_start_processing(void *send_info,
  982. ipmi_smi_t intf)
  983. {
  984. struct smi_info *new_smi = send_info;
  985. int enable = 0;
  986. new_smi->intf = intf;
  987. /* Try to claim any interrupts. */
  988. if (new_smi->irq_setup)
  989. new_smi->irq_setup(new_smi);
  990. /* Set up the timer that drives the interface. */
  991. setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
  992. new_smi->last_timeout_jiffies = jiffies;
  993. mod_timer(&new_smi->si_timer, jiffies + SI_TIMEOUT_JIFFIES);
  994. /*
  995. * Check if the user forcefully enabled the daemon.
  996. */
  997. if (new_smi->intf_num < num_force_kipmid)
  998. enable = force_kipmid[new_smi->intf_num];
  999. /*
  1000. * The BT interface is efficient enough to not need a thread,
  1001. * and there is no need for a thread if we have interrupts.
  1002. */
  1003. else if ((new_smi->si_type != SI_BT) && (!new_smi->irq))
  1004. enable = 1;
  1005. if (enable) {
  1006. new_smi->thread = kthread_run(ipmi_thread, new_smi,
  1007. "kipmi%d", new_smi->intf_num);
  1008. if (IS_ERR(new_smi->thread)) {
  1009. dev_notice(new_smi->dev, "Could not start"
  1010. " kernel thread due to error %ld, only using"
  1011. " timers to drive the interface\n",
  1012. PTR_ERR(new_smi->thread));
  1013. new_smi->thread = NULL;
  1014. }
  1015. }
  1016. return 0;
  1017. }
  1018. static void set_maintenance_mode(void *send_info, int enable)
  1019. {
  1020. struct smi_info *smi_info = send_info;
  1021. if (!enable)
  1022. atomic_set(&smi_info->req_events, 0);
  1023. }
  1024. static struct ipmi_smi_handlers handlers = {
  1025. .owner = THIS_MODULE,
  1026. .start_processing = smi_start_processing,
  1027. .sender = sender,
  1028. .request_events = request_events,
  1029. .set_maintenance_mode = set_maintenance_mode,
  1030. .set_run_to_completion = set_run_to_completion,
  1031. .poll = poll,
  1032. };
  1033. /*
  1034. * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
  1035. * a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.
  1036. */
  1037. static LIST_HEAD(smi_infos);
  1038. static DEFINE_MUTEX(smi_infos_lock);
  1039. static int smi_num; /* Used to sequence the SMIs */
  1040. #define DEFAULT_REGSPACING 1
  1041. #define DEFAULT_REGSIZE 1
  1042. static int si_trydefaults = 1;
  1043. static char *si_type[SI_MAX_PARMS];
  1044. #define MAX_SI_TYPE_STR 30
  1045. static char si_type_str[MAX_SI_TYPE_STR];
  1046. static unsigned long addrs[SI_MAX_PARMS];
  1047. static unsigned int num_addrs;
  1048. static unsigned int ports[SI_MAX_PARMS];
  1049. static unsigned int num_ports;
  1050. static int irqs[SI_MAX_PARMS];
  1051. static unsigned int num_irqs;
  1052. static int regspacings[SI_MAX_PARMS];
  1053. static unsigned int num_regspacings;
  1054. static int regsizes[SI_MAX_PARMS];
  1055. static unsigned int num_regsizes;
  1056. static int regshifts[SI_MAX_PARMS];
  1057. static unsigned int num_regshifts;
  1058. static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */
  1059. static unsigned int num_slave_addrs;
  1060. #define IPMI_IO_ADDR_SPACE 0
  1061. #define IPMI_MEM_ADDR_SPACE 1
  1062. static char *addr_space_to_str[] = { "i/o", "mem" };
  1063. static int hotmod_handler(const char *val, struct kernel_param *kp);
  1064. module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
  1065. MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
  1066. " Documentation/IPMI.txt in the kernel sources for the"
  1067. " gory details.");
  1068. module_param_named(trydefaults, si_trydefaults, bool, 0);
  1069. MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
  1070. " default scan of the KCS and SMIC interface at the standard"
  1071. " address");
  1072. module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
  1073. MODULE_PARM_DESC(type, "Defines the type of each interface, each"
  1074. " interface separated by commas. The types are 'kcs',"
  1075. " 'smic', and 'bt'. For example si_type=kcs,bt will set"
  1076. " the first interface to kcs and the second to bt");
  1077. module_param_array(addrs, ulong, &num_addrs, 0);
  1078. MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
  1079. " addresses separated by commas. Only use if an interface"
  1080. " is in memory. Otherwise, set it to zero or leave"
  1081. " it blank.");
  1082. module_param_array(ports, uint, &num_ports, 0);
  1083. MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
  1084. " addresses separated by commas. Only use if an interface"
  1085. " is a port. Otherwise, set it to zero or leave"
  1086. " it blank.");
  1087. module_param_array(irqs, int, &num_irqs, 0);
  1088. MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
  1089. " addresses separated by commas. Only use if an interface"
  1090. " has an interrupt. Otherwise, set it to zero or leave"
  1091. " it blank.");
  1092. module_param_array(regspacings, int, &num_regspacings, 0);
  1093. MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
  1094. " and each successive register used by the interface. For"
  1095. " instance, if the start address is 0xca2 and the spacing"
  1096. " is 2, then the second address is at 0xca4. Defaults"
  1097. " to 1.");
  1098. module_param_array(regsizes, int, &num_regsizes, 0);
  1099. MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
  1100. " This should generally be 1, 2, 4, or 8 for an 8-bit,"
  1101. " 16-bit, 32-bit, or 64-bit register. Use this if you"
  1102. " the 8-bit IPMI register has to be read from a larger"
  1103. " register.");
  1104. module_param_array(regshifts, int, &num_regshifts, 0);
  1105. MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
  1106. " IPMI register, in bits. For instance, if the data"
  1107. " is read from a 32-bit word and the IPMI data is in"
  1108. " bit 8-15, then the shift would be 8");
  1109. module_param_array(slave_addrs, int, &num_slave_addrs, 0);
  1110. MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
  1111. " the controller. Normally this is 0x20, but can be"
  1112. " overridden by this parm. This is an array indexed"
  1113. " by interface number.");
  1114. module_param_array(force_kipmid, int, &num_force_kipmid, 0);
  1115. MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
  1116. " disabled(0). Normally the IPMI driver auto-detects"
  1117. " this, but the value may be overridden by this parm.");
  1118. module_param(unload_when_empty, int, 0);
  1119. MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
  1120. " specified or found, default is 1. Setting to 0"
  1121. " is useful for hot add of devices using hotmod.");
  1122. module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
  1123. MODULE_PARM_DESC(kipmid_max_busy_us,
  1124. "Max time (in microseconds) to busy-wait for IPMI data before"
  1125. " sleeping. 0 (default) means to wait forever. Set to 100-500"
  1126. " if kipmid is using up a lot of CPU time.");
  1127. static void std_irq_cleanup(struct smi_info *info)
  1128. {
  1129. if (info->si_type == SI_BT)
  1130. /* Disable the interrupt in the BT interface. */
  1131. info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
  1132. free_irq(info->irq, info);
  1133. }
  1134. static int std_irq_setup(struct smi_info *info)
  1135. {
  1136. int rv;
  1137. if (!info->irq)
  1138. return 0;
  1139. if (info->si_type == SI_BT) {
  1140. rv = request_irq(info->irq,
  1141. si_bt_irq_handler,
  1142. IRQF_SHARED | IRQF_DISABLED,
  1143. DEVICE_NAME,
  1144. info);
  1145. if (!rv)
  1146. /* Enable the interrupt in the BT interface. */
  1147. info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
  1148. IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  1149. } else
  1150. rv = request_irq(info->irq,
  1151. si_irq_handler,
  1152. IRQF_SHARED | IRQF_DISABLED,
  1153. DEVICE_NAME,
  1154. info);
  1155. if (rv) {
  1156. dev_warn(info->dev, "%s unable to claim interrupt %d,"
  1157. " running polled\n",
  1158. DEVICE_NAME, info->irq);
  1159. info->irq = 0;
  1160. } else {
  1161. info->irq_cleanup = std_irq_cleanup;
  1162. dev_info(info->dev, "Using irq %d\n", info->irq);
  1163. }
  1164. return rv;
  1165. }
  1166. static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
  1167. {
  1168. unsigned int addr = io->addr_data;
  1169. return inb(addr + (offset * io->regspacing));
  1170. }
  1171. static void port_outb(struct si_sm_io *io, unsigned int offset,
  1172. unsigned char b)
  1173. {
  1174. unsigned int addr = io->addr_data;
  1175. outb(b, addr + (offset * io->regspacing));
  1176. }
  1177. static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
  1178. {
  1179. unsigned int addr = io->addr_data;
  1180. return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
  1181. }
  1182. static void port_outw(struct si_sm_io *io, unsigned int offset,
  1183. unsigned char b)
  1184. {
  1185. unsigned int addr = io->addr_data;
  1186. outw(b << io->regshift, addr + (offset * io->regspacing));
  1187. }
  1188. static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
  1189. {
  1190. unsigned int addr = io->addr_data;
  1191. return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
  1192. }
  1193. static void port_outl(struct si_sm_io *io, unsigned int offset,
  1194. unsigned char b)
  1195. {
  1196. unsigned int addr = io->addr_data;
  1197. outl(b << io->regshift, addr+(offset * io->regspacing));
  1198. }
  1199. static void port_cleanup(struct smi_info *info)
  1200. {
  1201. unsigned int addr = info->io.addr_data;
  1202. int idx;
  1203. if (addr) {
  1204. for (idx = 0; idx < info->io_size; idx++)
  1205. release_region(addr + idx * info->io.regspacing,
  1206. info->io.regsize);
  1207. }
  1208. }
  1209. static int port_setup(struct smi_info *info)
  1210. {
  1211. unsigned int addr = info->io.addr_data;
  1212. int idx;
  1213. if (!addr)
  1214. return -ENODEV;
  1215. info->io_cleanup = port_cleanup;
  1216. /*
  1217. * Figure out the actual inb/inw/inl/etc routine to use based
  1218. * upon the register size.
  1219. */
  1220. switch (info->io.regsize) {
  1221. case 1:
  1222. info->io.inputb = port_inb;
  1223. info->io.outputb = port_outb;
  1224. break;
  1225. case 2:
  1226. info->io.inputb = port_inw;
  1227. info->io.outputb = port_outw;
  1228. break;
  1229. case 4:
  1230. info->io.inputb = port_inl;
  1231. info->io.outputb = port_outl;
  1232. break;
  1233. default:
  1234. dev_warn(info->dev, "Invalid register size: %d\n",
  1235. info->io.regsize);
  1236. return -EINVAL;
  1237. }
  1238. /*
  1239. * Some BIOSes reserve disjoint I/O regions in their ACPI
  1240. * tables. This causes problems when trying to register the
  1241. * entire I/O region. Therefore we must register each I/O
  1242. * port separately.
  1243. */
  1244. for (idx = 0; idx < info->io_size; idx++) {
  1245. if (request_region(addr + idx * info->io.regspacing,
  1246. info->io.regsize, DEVICE_NAME) == NULL) {
  1247. /* Undo allocations */
  1248. while (idx--) {
  1249. release_region(addr + idx * info->io.regspacing,
  1250. info->io.regsize);
  1251. }
  1252. return -EIO;
  1253. }
  1254. }
  1255. return 0;
  1256. }
  1257. static unsigned char intf_mem_inb(struct si_sm_io *io, unsigned int offset)
  1258. {
  1259. return readb((io->addr)+(offset * io->regspacing));
  1260. }
  1261. static void intf_mem_outb(struct si_sm_io *io, unsigned int offset,
  1262. unsigned char b)
  1263. {
  1264. writeb(b, (io->addr)+(offset * io->regspacing));
  1265. }
  1266. static unsigned char intf_mem_inw(struct si_sm_io *io, unsigned int offset)
  1267. {
  1268. return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
  1269. & 0xff;
  1270. }
  1271. static void intf_mem_outw(struct si_sm_io *io, unsigned int offset,
  1272. unsigned char b)
  1273. {
  1274. writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
  1275. }
  1276. static unsigned char intf_mem_inl(struct si_sm_io *io, unsigned int offset)
  1277. {
  1278. return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
  1279. & 0xff;
  1280. }
  1281. static void intf_mem_outl(struct si_sm_io *io, unsigned int offset,
  1282. unsigned char b)
  1283. {
  1284. writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
  1285. }
  1286. #ifdef readq
  1287. static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
  1288. {
  1289. return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
  1290. & 0xff;
  1291. }
  1292. static void mem_outq(struct si_sm_io *io, unsigned int offset,
  1293. unsigned char b)
  1294. {
  1295. writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
  1296. }
  1297. #endif
  1298. static void mem_cleanup(struct smi_info *info)
  1299. {
  1300. unsigned long addr = info->io.addr_data;
  1301. int mapsize;
  1302. if (info->io.addr) {
  1303. iounmap(info->io.addr);
  1304. mapsize = ((info->io_size * info->io.regspacing)
  1305. - (info->io.regspacing - info->io.regsize));
  1306. release_mem_region(addr, mapsize);
  1307. }
  1308. }
  1309. static int mem_setup(struct smi_info *info)
  1310. {
  1311. unsigned long addr = info->io.addr_data;
  1312. int mapsize;
  1313. if (!addr)
  1314. return -ENODEV;
  1315. info->io_cleanup = mem_cleanup;
  1316. /*
  1317. * Figure out the actual readb/readw/readl/etc routine to use based
  1318. * upon the register size.
  1319. */
  1320. switch (info->io.regsize) {
  1321. case 1:
  1322. info->io.inputb = intf_mem_inb;
  1323. info->io.outputb = intf_mem_outb;
  1324. break;
  1325. case 2:
  1326. info->io.inputb = intf_mem_inw;
  1327. info->io.outputb = intf_mem_outw;
  1328. break;
  1329. case 4:
  1330. info->io.inputb = intf_mem_inl;
  1331. info->io.outputb = intf_mem_outl;
  1332. break;
  1333. #ifdef readq
  1334. case 8:
  1335. info->io.inputb = mem_inq;
  1336. info->io.outputb = mem_outq;
  1337. break;
  1338. #endif
  1339. default:
  1340. dev_warn(info->dev, "Invalid register size: %d\n",
  1341. info->io.regsize);
  1342. return -EINVAL;
  1343. }
  1344. /*
  1345. * Calculate the total amount of memory to claim. This is an
  1346. * unusual looking calculation, but it avoids claiming any
  1347. * more memory than it has to. It will claim everything
  1348. * between the first address to the end of the last full
  1349. * register.
  1350. */
  1351. mapsize = ((info->io_size * info->io.regspacing)
  1352. - (info->io.regspacing - info->io.regsize));
  1353. if (request_mem_region(addr, mapsize, DEVICE_NAME) == NULL)
  1354. return -EIO;
  1355. info->io.addr = ioremap(addr, mapsize);
  1356. if (info->io.addr == NULL) {
  1357. release_mem_region(addr, mapsize);
  1358. return -EIO;
  1359. }
  1360. return 0;
  1361. }
  1362. /*
  1363. * Parms come in as <op1>[:op2[:op3...]]. ops are:
  1364. * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
  1365. * Options are:
  1366. * rsp=<regspacing>
  1367. * rsi=<regsize>
  1368. * rsh=<regshift>
  1369. * irq=<irq>
  1370. * ipmb=<ipmb addr>
  1371. */
  1372. enum hotmod_op { HM_ADD, HM_REMOVE };
  1373. struct hotmod_vals {
  1374. char *name;
  1375. int val;
  1376. };
  1377. static struct hotmod_vals hotmod_ops[] = {
  1378. { "add", HM_ADD },
  1379. { "remove", HM_REMOVE },
  1380. { NULL }
  1381. };
  1382. static struct hotmod_vals hotmod_si[] = {
  1383. { "kcs", SI_KCS },
  1384. { "smic", SI_SMIC },
  1385. { "bt", SI_BT },
  1386. { NULL }
  1387. };
  1388. static struct hotmod_vals hotmod_as[] = {
  1389. { "mem", IPMI_MEM_ADDR_SPACE },
  1390. { "i/o", IPMI_IO_ADDR_SPACE },
  1391. { NULL }
  1392. };
  1393. static int parse_str(struct hotmod_vals *v, int *val, char *name, char **curr)
  1394. {
  1395. char *s;
  1396. int i;
  1397. s = strchr(*curr, ',');
  1398. if (!s) {
  1399. printk(KERN_WARNING PFX "No hotmod %s given.\n", name);
  1400. return -EINVAL;
  1401. }
  1402. *s = '\0';
  1403. s++;
  1404. for (i = 0; hotmod_ops[i].name; i++) {
  1405. if (strcmp(*curr, v[i].name) == 0) {
  1406. *val = v[i].val;
  1407. *curr = s;
  1408. return 0;
  1409. }
  1410. }
  1411. printk(KERN_WARNING PFX "Invalid hotmod %s '%s'\n", name, *curr);
  1412. return -EINVAL;
  1413. }
  1414. static int check_hotmod_int_op(const char *curr, const char *option,
  1415. const char *name, int *val)
  1416. {
  1417. char *n;
  1418. if (strcmp(curr, name) == 0) {
  1419. if (!option) {
  1420. printk(KERN_WARNING PFX
  1421. "No option given for '%s'\n",
  1422. curr);
  1423. return -EINVAL;
  1424. }
  1425. *val = simple_strtoul(option, &n, 0);
  1426. if ((*n != '\0') || (*option == '\0')) {
  1427. printk(KERN_WARNING PFX
  1428. "Bad option given for '%s'\n",
  1429. curr);
  1430. return -EINVAL;
  1431. }
  1432. return 1;
  1433. }
  1434. return 0;
  1435. }
  1436. static struct smi_info *smi_info_alloc(void)
  1437. {
  1438. struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
  1439. if (info) {
  1440. spin_lock_init(&info->si_lock);
  1441. spin_lock_init(&info->msg_lock);
  1442. }
  1443. return info;
  1444. }
  1445. static int hotmod_handler(const char *val, struct kernel_param *kp)
  1446. {
  1447. char *str = kstrdup(val, GFP_KERNEL);
  1448. int rv;
  1449. char *next, *curr, *s, *n, *o;
  1450. enum hotmod_op op;
  1451. enum si_type si_type;
  1452. int addr_space;
  1453. unsigned long addr;
  1454. int regspacing;
  1455. int regsize;
  1456. int regshift;
  1457. int irq;
  1458. int ipmb;
  1459. int ival;
  1460. int len;
  1461. struct smi_info *info;
  1462. if (!str)
  1463. return -ENOMEM;
  1464. /* Kill any trailing spaces, as we can get a "\n" from echo. */
  1465. len = strlen(str);
  1466. ival = len - 1;
  1467. while ((ival >= 0) && isspace(str[ival])) {
  1468. str[ival] = '\0';
  1469. ival--;
  1470. }
  1471. for (curr = str; curr; curr = next) {
  1472. regspacing = 1;
  1473. regsize = 1;
  1474. regshift = 0;
  1475. irq = 0;
  1476. ipmb = 0; /* Choose the default if not specified */
  1477. next = strchr(curr, ':');
  1478. if (next) {
  1479. *next = '\0';
  1480. next++;
  1481. }
  1482. rv = parse_str(hotmod_ops, &ival, "operation", &curr);
  1483. if (rv)
  1484. break;
  1485. op = ival;
  1486. rv = parse_str(hotmod_si, &ival, "interface type", &curr);
  1487. if (rv)
  1488. break;
  1489. si_type = ival;
  1490. rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
  1491. if (rv)
  1492. break;
  1493. s = strchr(curr, ',');
  1494. if (s) {
  1495. *s = '\0';
  1496. s++;
  1497. }
  1498. addr = simple_strtoul(curr, &n, 0);
  1499. if ((*n != '\0') || (*curr == '\0')) {
  1500. printk(KERN_WARNING PFX "Invalid hotmod address"
  1501. " '%s'\n", curr);
  1502. break;
  1503. }
  1504. while (s) {
  1505. curr = s;
  1506. s = strchr(curr, ',');
  1507. if (s) {
  1508. *s = '\0';
  1509. s++;
  1510. }
  1511. o = strchr(curr, '=');
  1512. if (o) {
  1513. *o = '\0';
  1514. o++;
  1515. }
  1516. rv = check_hotmod_int_op(curr, o, "rsp", &regspacing);
  1517. if (rv < 0)
  1518. goto out;
  1519. else if (rv)
  1520. continue;
  1521. rv = check_hotmod_int_op(curr, o, "rsi", &regsize);
  1522. if (rv < 0)
  1523. goto out;
  1524. else if (rv)
  1525. continue;
  1526. rv = check_hotmod_int_op(curr, o, "rsh", &regshift);
  1527. if (rv < 0)
  1528. goto out;
  1529. else if (rv)
  1530. continue;
  1531. rv = check_hotmod_int_op(curr, o, "irq", &irq);
  1532. if (rv < 0)
  1533. goto out;
  1534. else if (rv)
  1535. continue;
  1536. rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
  1537. if (rv < 0)
  1538. goto out;
  1539. else if (rv)
  1540. continue;
  1541. rv = -EINVAL;
  1542. printk(KERN_WARNING PFX
  1543. "Invalid hotmod option '%s'\n",
  1544. curr);
  1545. goto out;
  1546. }
  1547. if (op == HM_ADD) {
  1548. info = smi_info_alloc();
  1549. if (!info) {
  1550. rv = -ENOMEM;
  1551. goto out;
  1552. }
  1553. info->addr_source = SI_HOTMOD;
  1554. info->si_type = si_type;
  1555. info->io.addr_data = addr;
  1556. info->io.addr_type = addr_space;
  1557. if (addr_space == IPMI_MEM_ADDR_SPACE)
  1558. info->io_setup = mem_setup;
  1559. else
  1560. info->io_setup = port_setup;
  1561. info->io.addr = NULL;
  1562. info->io.regspacing = regspacing;
  1563. if (!info->io.regspacing)
  1564. info->io.regspacing = DEFAULT_REGSPACING;
  1565. info->io.regsize = regsize;
  1566. if (!info->io.regsize)
  1567. info->io.regsize = DEFAULT_REGSPACING;
  1568. info->io.regshift = regshift;
  1569. info->irq = irq;
  1570. if (info->irq)
  1571. info->irq_setup = std_irq_setup;
  1572. info->slave_addr = ipmb;
  1573. if (!add_smi(info)) {
  1574. if (try_smi_init(info))
  1575. cleanup_one_si(info);
  1576. } else {
  1577. kfree(info);
  1578. }
  1579. } else {
  1580. /* remove */
  1581. struct smi_info *e, *tmp_e;
  1582. mutex_lock(&smi_infos_lock);
  1583. list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
  1584. if (e->io.addr_type != addr_space)
  1585. continue;
  1586. if (e->si_type != si_type)
  1587. continue;
  1588. if (e->io.addr_data == addr)
  1589. cleanup_one_si(e);
  1590. }
  1591. mutex_unlock(&smi_infos_lock);
  1592. }
  1593. }
  1594. rv = len;
  1595. out:
  1596. kfree(str);
  1597. return rv;
  1598. }
  1599. static void __devinit hardcode_find_bmc(void)
  1600. {
  1601. int i;
  1602. struct smi_info *info;
  1603. for (i = 0; i < SI_MAX_PARMS; i++) {
  1604. if (!ports[i] && !addrs[i])
  1605. continue;
  1606. info = smi_info_alloc();
  1607. if (!info)
  1608. return;
  1609. info->addr_source = SI_HARDCODED;
  1610. printk(KERN_INFO PFX "probing via hardcoded address\n");
  1611. if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
  1612. info->si_type = SI_KCS;
  1613. } else if (strcmp(si_type[i], "smic") == 0) {
  1614. info->si_type = SI_SMIC;
  1615. } else if (strcmp(si_type[i], "bt") == 0) {
  1616. info->si_type = SI_BT;
  1617. } else {
  1618. printk(KERN_WARNING PFX "Interface type specified "
  1619. "for interface %d, was invalid: %s\n",
  1620. i, si_type[i]);
  1621. kfree(info);
  1622. continue;
  1623. }
  1624. if (ports[i]) {
  1625. /* An I/O port */
  1626. info->io_setup = port_setup;
  1627. info->io.addr_data = ports[i];
  1628. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1629. } else if (addrs[i]) {
  1630. /* A memory port */
  1631. info->io_setup = mem_setup;
  1632. info->io.addr_data = addrs[i];
  1633. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1634. } else {
  1635. printk(KERN_WARNING PFX "Interface type specified "
  1636. "for interface %d, but port and address were "
  1637. "not set or set to zero.\n", i);
  1638. kfree(info);
  1639. continue;
  1640. }
  1641. info->io.addr = NULL;
  1642. info->io.regspacing = regspacings[i];
  1643. if (!info->io.regspacing)
  1644. info->io.regspacing = DEFAULT_REGSPACING;
  1645. info->io.regsize = regsizes[i];
  1646. if (!info->io.regsize)
  1647. info->io.regsize = DEFAULT_REGSPACING;
  1648. info->io.regshift = regshifts[i];
  1649. info->irq = irqs[i];
  1650. if (info->irq)
  1651. info->irq_setup = std_irq_setup;
  1652. info->slave_addr = slave_addrs[i];
  1653. if (!add_smi(info)) {
  1654. if (try_smi_init(info))
  1655. cleanup_one_si(info);
  1656. } else {
  1657. kfree(info);
  1658. }
  1659. }
  1660. }
  1661. #ifdef CONFIG_ACPI
  1662. #include <linux/acpi.h>
  1663. /*
  1664. * Once we get an ACPI failure, we don't try any more, because we go
  1665. * through the tables sequentially. Once we don't find a table, there
  1666. * are no more.
  1667. */
  1668. static int acpi_failure;
  1669. /* For GPE-type interrupts. */
  1670. static u32 ipmi_acpi_gpe(void *context)
  1671. {
  1672. struct smi_info *smi_info = context;
  1673. unsigned long flags;
  1674. #ifdef DEBUG_TIMING
  1675. struct timeval t;
  1676. #endif
  1677. spin_lock_irqsave(&(smi_info->si_lock), flags);
  1678. smi_inc_stat(smi_info, interrupts);
  1679. #ifdef DEBUG_TIMING
  1680. do_gettimeofday(&t);
  1681. printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  1682. #endif
  1683. smi_event_handler(smi_info, 0);
  1684. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  1685. return ACPI_INTERRUPT_HANDLED;
  1686. }
  1687. static void acpi_gpe_irq_cleanup(struct smi_info *info)
  1688. {
  1689. if (!info->irq)
  1690. return;
  1691. acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
  1692. }
  1693. static int acpi_gpe_irq_setup(struct smi_info *info)
  1694. {
  1695. acpi_status status;
  1696. if (!info->irq)
  1697. return 0;
  1698. /* FIXME - is level triggered right? */
  1699. status = acpi_install_gpe_handler(NULL,
  1700. info->irq,
  1701. ACPI_GPE_LEVEL_TRIGGERED,
  1702. &ipmi_acpi_gpe,
  1703. info);
  1704. if (status != AE_OK) {
  1705. dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"
  1706. " running polled\n", DEVICE_NAME, info->irq);
  1707. info->irq = 0;
  1708. return -EINVAL;
  1709. } else {
  1710. info->irq_cleanup = acpi_gpe_irq_cleanup;
  1711. dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);
  1712. return 0;
  1713. }
  1714. }
  1715. /*
  1716. * Defined at
  1717. * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
  1718. */
  1719. struct SPMITable {
  1720. s8 Signature[4];
  1721. u32 Length;
  1722. u8 Revision;
  1723. u8 Checksum;
  1724. s8 OEMID[6];
  1725. s8 OEMTableID[8];
  1726. s8 OEMRevision[4];
  1727. s8 CreatorID[4];
  1728. s8 CreatorRevision[4];
  1729. u8 InterfaceType;
  1730. u8 IPMIlegacy;
  1731. s16 SpecificationRevision;
  1732. /*
  1733. * Bit 0 - SCI interrupt supported
  1734. * Bit 1 - I/O APIC/SAPIC
  1735. */
  1736. u8 InterruptType;
  1737. /*
  1738. * If bit 0 of InterruptType is set, then this is the SCI
  1739. * interrupt in the GPEx_STS register.
  1740. */
  1741. u8 GPE;
  1742. s16 Reserved;
  1743. /*
  1744. * If bit 1 of InterruptType is set, then this is the I/O
  1745. * APIC/SAPIC interrupt.
  1746. */
  1747. u32 GlobalSystemInterrupt;
  1748. /* The actual register address. */
  1749. struct acpi_generic_address addr;
  1750. u8 UID[4];
  1751. s8 spmi_id[1]; /* A '\0' terminated array starts here. */
  1752. };
  1753. static int __devinit try_init_spmi(struct SPMITable *spmi)
  1754. {
  1755. struct smi_info *info;
  1756. if (spmi->IPMIlegacy != 1) {
  1757. printk(KERN_INFO PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
  1758. return -ENODEV;
  1759. }
  1760. info = smi_info_alloc();
  1761. if (!info) {
  1762. printk(KERN_ERR PFX "Could not allocate SI data (3)\n");
  1763. return -ENOMEM;
  1764. }
  1765. info->addr_source = SI_SPMI;
  1766. printk(KERN_INFO PFX "probing via SPMI\n");
  1767. /* Figure out the interface type. */
  1768. switch (spmi->InterfaceType) {
  1769. case 1: /* KCS */
  1770. info->si_type = SI_KCS;
  1771. break;
  1772. case 2: /* SMIC */
  1773. info->si_type = SI_SMIC;
  1774. break;
  1775. case 3: /* BT */
  1776. info->si_type = SI_BT;
  1777. break;
  1778. default:
  1779. printk(KERN_INFO PFX "Unknown ACPI/SPMI SI type %d\n",
  1780. spmi->InterfaceType);
  1781. kfree(info);
  1782. return -EIO;
  1783. }
  1784. if (spmi->InterruptType & 1) {
  1785. /* We've got a GPE interrupt. */
  1786. info->irq = spmi->GPE;
  1787. info->irq_setup = acpi_gpe_irq_setup;
  1788. } else if (spmi->InterruptType & 2) {
  1789. /* We've got an APIC/SAPIC interrupt. */
  1790. info->irq = spmi->GlobalSystemInterrupt;
  1791. info->irq_setup = std_irq_setup;
  1792. } else {
  1793. /* Use the default interrupt setting. */
  1794. info->irq = 0;
  1795. info->irq_setup = NULL;
  1796. }
  1797. if (spmi->addr.bit_width) {
  1798. /* A (hopefully) properly formed register bit width. */
  1799. info->io.regspacing = spmi->addr.bit_width / 8;
  1800. } else {
  1801. info->io.regspacing = DEFAULT_REGSPACING;
  1802. }
  1803. info->io.regsize = info->io.regspacing;
  1804. info->io.regshift = spmi->addr.bit_offset;
  1805. if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  1806. info->io_setup = mem_setup;
  1807. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1808. } else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  1809. info->io_setup = port_setup;
  1810. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1811. } else {
  1812. kfree(info);
  1813. printk(KERN_WARNING PFX "Unknown ACPI I/O Address type\n");
  1814. return -EIO;
  1815. }
  1816. info->io.addr_data = spmi->addr.address;
  1817. pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",
  1818. (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
  1819. info->io.addr_data, info->io.regsize, info->io.regspacing,
  1820. info->irq);
  1821. if (add_smi(info))
  1822. kfree(info);
  1823. return 0;
  1824. }
  1825. static void __devinit spmi_find_bmc(void)
  1826. {
  1827. acpi_status status;
  1828. struct SPMITable *spmi;
  1829. int i;
  1830. if (acpi_disabled)
  1831. return;
  1832. if (acpi_failure)
  1833. return;
  1834. for (i = 0; ; i++) {
  1835. status = acpi_get_table(ACPI_SIG_SPMI, i+1,
  1836. (struct acpi_table_header **)&spmi);
  1837. if (status != AE_OK)
  1838. return;
  1839. try_init_spmi(spmi);
  1840. }
  1841. }
  1842. static int __devinit ipmi_pnp_probe(struct pnp_dev *dev,
  1843. const struct pnp_device_id *dev_id)
  1844. {
  1845. struct acpi_device *acpi_dev;
  1846. struct smi_info *info;
  1847. struct resource *res, *res_second;
  1848. acpi_handle handle;
  1849. acpi_status status;
  1850. unsigned long long tmp;
  1851. acpi_dev = pnp_acpi_device(dev);
  1852. if (!acpi_dev)
  1853. return -ENODEV;
  1854. info = smi_info_alloc();
  1855. if (!info)
  1856. return -ENOMEM;
  1857. info->addr_source = SI_ACPI;
  1858. printk(KERN_INFO PFX "probing via ACPI\n");
  1859. handle = acpi_dev->handle;
  1860. /* _IFT tells us the interface type: KCS, BT, etc */
  1861. status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
  1862. if (ACPI_FAILURE(status))
  1863. goto err_free;
  1864. switch (tmp) {
  1865. case 1:
  1866. info->si_type = SI_KCS;
  1867. break;
  1868. case 2:
  1869. info->si_type = SI_SMIC;
  1870. break;
  1871. case 3:
  1872. info->si_type = SI_BT;
  1873. break;
  1874. default:
  1875. dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);
  1876. goto err_free;
  1877. }
  1878. res = pnp_get_resource(dev, IORESOURCE_IO, 0);
  1879. if (res) {
  1880. info->io_setup = port_setup;
  1881. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1882. } else {
  1883. res = pnp_get_resource(dev, IORESOURCE_MEM, 0);
  1884. if (res) {
  1885. info->io_setup = mem_setup;
  1886. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1887. }
  1888. }
  1889. if (!res) {
  1890. dev_err(&dev->dev, "no I/O or memory address\n");
  1891. goto err_free;
  1892. }
  1893. info->io.addr_data = res->start;
  1894. info->io.regspacing = DEFAULT_REGSPACING;
  1895. res_second = pnp_get_resource(dev,
  1896. (info->io.addr_type == IPMI_IO_ADDR_SPACE) ?
  1897. IORESOURCE_IO : IORESOURCE_MEM,
  1898. 1);
  1899. if (res_second) {
  1900. if (res_second->start > info->io.addr_data)
  1901. info->io.regspacing = res_second->start - info->io.addr_data;
  1902. }
  1903. info->io.regsize = DEFAULT_REGSPACING;
  1904. info->io.regshift = 0;
  1905. /* If _GPE exists, use it; otherwise use standard interrupts */
  1906. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  1907. if (ACPI_SUCCESS(status)) {
  1908. info->irq = tmp;
  1909. info->irq_setup = acpi_gpe_irq_setup;
  1910. } else if (pnp_irq_valid(dev, 0)) {
  1911. info->irq = pnp_irq(dev, 0);
  1912. info->irq_setup = std_irq_setup;
  1913. }
  1914. info->dev = &dev->dev;
  1915. pnp_set_drvdata(dev, info);
  1916. dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",
  1917. res, info->io.regsize, info->io.regspacing,
  1918. info->irq);
  1919. if (add_smi(info))
  1920. goto err_free;
  1921. return 0;
  1922. err_free:
  1923. kfree(info);
  1924. return -EINVAL;
  1925. }
  1926. static void __devexit ipmi_pnp_remove(struct pnp_dev *dev)
  1927. {
  1928. struct smi_info *info = pnp_get_drvdata(dev);
  1929. cleanup_one_si(info);
  1930. }
  1931. static const struct pnp_device_id pnp_dev_table[] = {
  1932. {"IPI0001", 0},
  1933. {"", 0},
  1934. };
  1935. static struct pnp_driver ipmi_pnp_driver = {
  1936. .name = DEVICE_NAME,
  1937. .probe = ipmi_pnp_probe,
  1938. .remove = __devexit_p(ipmi_pnp_remove),
  1939. .id_table = pnp_dev_table,
  1940. };
  1941. #endif
  1942. #ifdef CONFIG_DMI
  1943. struct dmi_ipmi_data {
  1944. u8 type;
  1945. u8 addr_space;
  1946. unsigned long base_addr;
  1947. u8 irq;
  1948. u8 offset;
  1949. u8 slave_addr;
  1950. };
  1951. static int __devinit decode_dmi(const struct dmi_header *dm,
  1952. struct dmi_ipmi_data *dmi)
  1953. {
  1954. const u8 *data = (const u8 *)dm;
  1955. unsigned long base_addr;
  1956. u8 reg_spacing;
  1957. u8 len = dm->length;
  1958. dmi->type = data[4];
  1959. memcpy(&base_addr, data+8, sizeof(unsigned long));
  1960. if (len >= 0x11) {
  1961. if (base_addr & 1) {
  1962. /* I/O */
  1963. base_addr &= 0xFFFE;
  1964. dmi->addr_space = IPMI_IO_ADDR_SPACE;
  1965. } else
  1966. /* Memory */
  1967. dmi->addr_space = IPMI_MEM_ADDR_SPACE;
  1968. /* If bit 4 of byte 0x10 is set, then the lsb for the address
  1969. is odd. */
  1970. dmi->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
  1971. dmi->irq = data[0x11];
  1972. /* The top two bits of byte 0x10 hold the register spacing. */
  1973. reg_spacing = (data[0x10] & 0xC0) >> 6;
  1974. switch (reg_spacing) {
  1975. case 0x00: /* Byte boundaries */
  1976. dmi->offset = 1;
  1977. break;
  1978. case 0x01: /* 32-bit boundaries */
  1979. dmi->offset = 4;
  1980. break;
  1981. case 0x02: /* 16-byte boundaries */
  1982. dmi->offset = 16;
  1983. break;
  1984. default:
  1985. /* Some other interface, just ignore it. */
  1986. return -EIO;
  1987. }
  1988. } else {
  1989. /* Old DMI spec. */
  1990. /*
  1991. * Note that technically, the lower bit of the base
  1992. * address should be 1 if the address is I/O and 0 if
  1993. * the address is in memory. So many systems get that
  1994. * wrong (and all that I have seen are I/O) so we just
  1995. * ignore that bit and assume I/O. Systems that use
  1996. * memory should use the newer spec, anyway.
  1997. */
  1998. dmi->base_addr = base_addr & 0xfffe;
  1999. dmi->addr_space = IPMI_IO_ADDR_SPACE;
  2000. dmi->offset = 1;
  2001. }
  2002. dmi->slave_addr = data[6];
  2003. return 0;
  2004. }
  2005. static void __devinit try_init_dmi(struct dmi_ipmi_data *ipmi_data)
  2006. {
  2007. struct smi_info *info;
  2008. info = smi_info_alloc();
  2009. if (!info) {
  2010. printk(KERN_ERR PFX "Could not allocate SI data\n");
  2011. return;
  2012. }
  2013. info->addr_source = SI_SMBIOS;
  2014. printk(KERN_INFO PFX "probing via SMBIOS\n");
  2015. switch (ipmi_data->type) {
  2016. case 0x01: /* KCS */
  2017. info->si_type = SI_KCS;
  2018. break;
  2019. case 0x02: /* SMIC */
  2020. info->si_type = SI_SMIC;
  2021. break;
  2022. case 0x03: /* BT */
  2023. info->si_type = SI_BT;
  2024. break;
  2025. default:
  2026. kfree(info);
  2027. return;
  2028. }
  2029. switch (ipmi_data->addr_space) {
  2030. case IPMI_MEM_ADDR_SPACE:
  2031. info->io_setup = mem_setup;
  2032. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  2033. break;
  2034. case IPMI_IO_ADDR_SPACE:
  2035. info->io_setup = port_setup;
  2036. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  2037. break;
  2038. default:
  2039. kfree(info);
  2040. printk(KERN_WARNING PFX "Unknown SMBIOS I/O Address type: %d\n",
  2041. ipmi_data->addr_space);
  2042. return;
  2043. }
  2044. info->io.addr_data = ipmi_data->base_addr;
  2045. info->io.regspacing = ipmi_data->offset;
  2046. if (!info->io.regspacing)
  2047. info->io.regspacing = DEFAULT_REGSPACING;
  2048. info->io.regsize = DEFAULT_REGSPACING;
  2049. info->io.regshift = 0;
  2050. info->slave_addr = ipmi_data->slave_addr;
  2051. info->irq = ipmi_data->irq;
  2052. if (info->irq)
  2053. info->irq_setup = std_irq_setup;
  2054. pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
  2055. (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
  2056. info->io.addr_data, info->io.regsize, info->io.regspacing,
  2057. info->irq);
  2058. if (add_smi(info))
  2059. kfree(info);
  2060. }
  2061. static void __devinit dmi_find_bmc(void)
  2062. {
  2063. const struct dmi_device *dev = NULL;
  2064. struct dmi_ipmi_data data;
  2065. int rv;
  2066. while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
  2067. memset(&data, 0, sizeof(data));
  2068. rv = decode_dmi((const struct dmi_header *) dev->device_data,
  2069. &data);
  2070. if (!rv)
  2071. try_init_dmi(&data);
  2072. }
  2073. }
  2074. #endif /* CONFIG_DMI */
  2075. #ifdef CONFIG_PCI
  2076. #define PCI_ERMC_CLASSCODE 0x0C0700
  2077. #define PCI_ERMC_CLASSCODE_MASK 0xffffff00
  2078. #define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff
  2079. #define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x00
  2080. #define PCI_ERMC_CLASSCODE_TYPE_KCS 0x01
  2081. #define PCI_ERMC_CLASSCODE_TYPE_BT 0x02
  2082. #define PCI_HP_VENDOR_ID 0x103C
  2083. #define PCI_MMC_DEVICE_ID 0x121A
  2084. #define PCI_MMC_ADDR_CW 0x10
  2085. static void ipmi_pci_cleanup(struct smi_info *info)
  2086. {
  2087. struct pci_dev *pdev = info->addr_source_data;
  2088. pci_disable_device(pdev);
  2089. }
  2090. static int __devinit ipmi_pci_probe(struct pci_dev *pdev,
  2091. const struct pci_device_id *ent)
  2092. {
  2093. int rv;
  2094. int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
  2095. struct smi_info *info;
  2096. info = smi_info_alloc();
  2097. if (!info)
  2098. return -ENOMEM;
  2099. info->addr_source = SI_PCI;
  2100. dev_info(&pdev->dev, "probing via PCI");
  2101. switch (class_type) {
  2102. case PCI_ERMC_CLASSCODE_TYPE_SMIC:
  2103. info->si_type = SI_SMIC;
  2104. break;
  2105. case PCI_ERMC_CLASSCODE_TYPE_KCS:
  2106. info->si_type = SI_KCS;
  2107. break;
  2108. case PCI_ERMC_CLASSCODE_TYPE_BT:
  2109. info->si_type = SI_BT;
  2110. break;
  2111. default:
  2112. kfree(info);
  2113. dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
  2114. return -ENOMEM;
  2115. }
  2116. rv = pci_enable_device(pdev);
  2117. if (rv) {
  2118. dev_err(&pdev->dev, "couldn't enable PCI device\n");
  2119. kfree(info);
  2120. return rv;
  2121. }
  2122. info->addr_source_cleanup = ipmi_pci_cleanup;
  2123. info->addr_source_data = pdev;
  2124. if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
  2125. info->io_setup = port_setup;
  2126. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  2127. } else {
  2128. info->io_setup = mem_setup;
  2129. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  2130. }
  2131. info->io.addr_data = pci_resource_start(pdev, 0);
  2132. info->io.regspacing = DEFAULT_REGSPACING;
  2133. info->io.regsize = DEFAULT_REGSPACING;
  2134. info->io.regshift = 0;
  2135. info->irq = pdev->irq;
  2136. if (info->irq)
  2137. info->irq_setup = std_irq_setup;
  2138. info->dev = &pdev->dev;
  2139. pci_set_drvdata(pdev, info);
  2140. dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
  2141. &pdev->resource[0], info->io.regsize, info->io.regspacing,
  2142. info->irq);
  2143. if (add_smi(info))
  2144. kfree(info);
  2145. return 0;
  2146. }
  2147. static void __devexit ipmi_pci_remove(struct pci_dev *pdev)
  2148. {
  2149. struct smi_info *info = pci_get_drvdata(pdev);
  2150. cleanup_one_si(info);
  2151. }
  2152. #ifdef CONFIG_PM
  2153. static int ipmi_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  2154. {
  2155. return 0;
  2156. }
  2157. static int ipmi_pci_resume(struct pci_dev *pdev)
  2158. {
  2159. return 0;
  2160. }
  2161. #endif
  2162. static struct pci_device_id ipmi_pci_devices[] = {
  2163. { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
  2164. { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },
  2165. { 0, }
  2166. };
  2167. MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
  2168. static struct pci_driver ipmi_pci_driver = {
  2169. .name = DEVICE_NAME,
  2170. .id_table = ipmi_pci_devices,
  2171. .probe = ipmi_pci_probe,
  2172. .remove = __devexit_p(ipmi_pci_remove),
  2173. #ifdef CONFIG_PM
  2174. .suspend = ipmi_pci_suspend,
  2175. .resume = ipmi_pci_resume,
  2176. #endif
  2177. };
  2178. #endif /* CONFIG_PCI */
  2179. #ifdef CONFIG_PPC_OF
  2180. static int __devinit ipmi_of_probe(struct platform_device *dev,
  2181. const struct of_device_id *match)
  2182. {
  2183. struct smi_info *info;
  2184. struct resource resource;
  2185. const __be32 *regsize, *regspacing, *regshift;
  2186. struct device_node *np = dev->dev.of_node;
  2187. int ret;
  2188. int proplen;
  2189. dev_info(&dev->dev, "probing via device tree\n");
  2190. ret = of_address_to_resource(np, 0, &resource);
  2191. if (ret) {
  2192. dev_warn(&dev->dev, PFX "invalid address from OF\n");
  2193. return ret;
  2194. }
  2195. regsize = of_get_property(np, "reg-size", &proplen);
  2196. if (regsize && proplen != 4) {
  2197. dev_warn(&dev->dev, PFX "invalid regsize from OF\n");
  2198. return -EINVAL;
  2199. }
  2200. regspacing = of_get_property(np, "reg-spacing", &proplen);
  2201. if (regspacing && proplen != 4) {
  2202. dev_warn(&dev->dev, PFX "invalid regspacing from OF\n");
  2203. return -EINVAL;
  2204. }
  2205. regshift = of_get_property(np, "reg-shift", &proplen);
  2206. if (regshift && proplen != 4) {
  2207. dev_warn(&dev->dev, PFX "invalid regshift from OF\n");
  2208. return -EINVAL;
  2209. }
  2210. info = smi_info_alloc();
  2211. if (!info) {
  2212. dev_err(&dev->dev,
  2213. "could not allocate memory for OF probe\n");
  2214. return -ENOMEM;
  2215. }
  2216. info->si_type = (enum si_type) match->data;
  2217. info->addr_source = SI_DEVICETREE;
  2218. info->irq_setup = std_irq_setup;
  2219. if (resource.flags & IORESOURCE_IO) {
  2220. info->io_setup = port_setup;
  2221. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  2222. } else {
  2223. info->io_setup = mem_setup;
  2224. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  2225. }
  2226. info->io.addr_data = resource.start;
  2227. info->io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
  2228. info->io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
  2229. info->io.regshift = regshift ? be32_to_cpup(regshift) : 0;
  2230. info->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
  2231. info->dev = &dev->dev;
  2232. dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
  2233. info->io.addr_data, info->io.regsize, info->io.regspacing,
  2234. info->irq);
  2235. dev_set_drvdata(&dev->dev, info);
  2236. if (add_smi(info)) {
  2237. kfree(info);
  2238. return -EBUSY;
  2239. }
  2240. return 0;
  2241. }
  2242. static int __devexit ipmi_of_remove(struct platform_device *dev)
  2243. {
  2244. cleanup_one_si(dev_get_drvdata(&dev->dev));
  2245. return 0;
  2246. }
  2247. static struct of_device_id ipmi_match[] =
  2248. {
  2249. { .type = "ipmi", .compatible = "ipmi-kcs",
  2250. .data = (void *)(unsigned long) SI_KCS },
  2251. { .type = "ipmi", .compatible = "ipmi-smic",
  2252. .data = (void *)(unsigned long) SI_SMIC },
  2253. { .type = "ipmi", .compatible = "ipmi-bt",
  2254. .data = (void *)(unsigned long) SI_BT },
  2255. {},
  2256. };
  2257. static struct of_platform_driver ipmi_of_platform_driver = {
  2258. .driver = {
  2259. .name = "ipmi",
  2260. .owner = THIS_MODULE,
  2261. .of_match_table = ipmi_match,
  2262. },
  2263. .probe = ipmi_of_probe,
  2264. .remove = __devexit_p(ipmi_of_remove),
  2265. };
  2266. #endif /* CONFIG_PPC_OF */
  2267. static int wait_for_msg_done(struct smi_info *smi_info)
  2268. {
  2269. enum si_sm_result smi_result;
  2270. smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
  2271. for (;;) {
  2272. if (smi_result == SI_SM_CALL_WITH_DELAY ||
  2273. smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
  2274. schedule_timeout_uninterruptible(1);
  2275. smi_result = smi_info->handlers->event(
  2276. smi_info->si_sm, 100);
  2277. } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
  2278. smi_result = smi_info->handlers->event(
  2279. smi_info->si_sm, 0);
  2280. } else
  2281. break;
  2282. }
  2283. if (smi_result == SI_SM_HOSED)
  2284. /*
  2285. * We couldn't get the state machine to run, so whatever's at
  2286. * the port is probably not an IPMI SMI interface.
  2287. */
  2288. return -ENODEV;
  2289. return 0;
  2290. }
  2291. static int try_get_dev_id(struct smi_info *smi_info)
  2292. {
  2293. unsigned char msg[2];
  2294. unsigned char *resp;
  2295. unsigned long resp_len;
  2296. int rv = 0;
  2297. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  2298. if (!resp)
  2299. return -ENOMEM;
  2300. /*
  2301. * Do a Get Device ID command, since it comes back with some
  2302. * useful info.
  2303. */
  2304. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  2305. msg[1] = IPMI_GET_DEVICE_ID_CMD;
  2306. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  2307. rv = wait_for_msg_done(smi_info);
  2308. if (rv)
  2309. goto out;
  2310. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  2311. resp, IPMI_MAX_MSG_LENGTH);
  2312. /* Check and record info from the get device id, in case we need it. */
  2313. rv = ipmi_demangle_device_id(resp, resp_len, &smi_info->device_id);
  2314. out:
  2315. kfree(resp);
  2316. return rv;
  2317. }
  2318. static int try_enable_event_buffer(struct smi_info *smi_info)
  2319. {
  2320. unsigned char msg[3];
  2321. unsigned char *resp;
  2322. unsigned long resp_len;
  2323. int rv = 0;
  2324. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  2325. if (!resp)
  2326. return -ENOMEM;
  2327. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  2328. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  2329. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  2330. rv = wait_for_msg_done(smi_info);
  2331. if (rv) {
  2332. printk(KERN_WARNING PFX "Error getting response from get"
  2333. " global enables command, the event buffer is not"
  2334. " enabled.\n");
  2335. goto out;
  2336. }
  2337. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  2338. resp, IPMI_MAX_MSG_LENGTH);
  2339. if (resp_len < 4 ||
  2340. resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
  2341. resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
  2342. resp[2] != 0) {
  2343. printk(KERN_WARNING PFX "Invalid return from get global"
  2344. " enables command, cannot enable the event buffer.\n");
  2345. rv = -EINVAL;
  2346. goto out;
  2347. }
  2348. if (resp[3] & IPMI_BMC_EVT_MSG_BUFF)
  2349. /* buffer is already enabled, nothing to do. */
  2350. goto out;
  2351. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  2352. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  2353. msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
  2354. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
  2355. rv = wait_for_msg_done(smi_info);
  2356. if (rv) {
  2357. printk(KERN_WARNING PFX "Error getting response from set"
  2358. " global, enables command, the event buffer is not"
  2359. " enabled.\n");
  2360. goto out;
  2361. }
  2362. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  2363. resp, IPMI_MAX_MSG_LENGTH);
  2364. if (resp_len < 3 ||
  2365. resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
  2366. resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
  2367. printk(KERN_WARNING PFX "Invalid return from get global,"
  2368. "enables command, not enable the event buffer.\n");
  2369. rv = -EINVAL;
  2370. goto out;
  2371. }
  2372. if (resp[2] != 0)
  2373. /*
  2374. * An error when setting the event buffer bit means
  2375. * that the event buffer is not supported.
  2376. */
  2377. rv = -ENOENT;
  2378. out:
  2379. kfree(resp);
  2380. return rv;
  2381. }
  2382. static int type_file_read_proc(char *page, char **start, off_t off,
  2383. int count, int *eof, void *data)
  2384. {
  2385. struct smi_info *smi = data;
  2386. return sprintf(page, "%s\n", si_to_str[smi->si_type]);
  2387. }
  2388. static int stat_file_read_proc(char *page, char **start, off_t off,
  2389. int count, int *eof, void *data)
  2390. {
  2391. char *out = (char *) page;
  2392. struct smi_info *smi = data;
  2393. out += sprintf(out, "interrupts_enabled: %d\n",
  2394. smi->irq && !smi->interrupt_disabled);
  2395. out += sprintf(out, "short_timeouts: %u\n",
  2396. smi_get_stat(smi, short_timeouts));
  2397. out += sprintf(out, "long_timeouts: %u\n",
  2398. smi_get_stat(smi, long_timeouts));
  2399. out += sprintf(out, "idles: %u\n",
  2400. smi_get_stat(smi, idles));
  2401. out += sprintf(out, "interrupts: %u\n",
  2402. smi_get_stat(smi, interrupts));
  2403. out += sprintf(out, "attentions: %u\n",
  2404. smi_get_stat(smi, attentions));
  2405. out += sprintf(out, "flag_fetches: %u\n",
  2406. smi_get_stat(smi, flag_fetches));
  2407. out += sprintf(out, "hosed_count: %u\n",
  2408. smi_get_stat(smi, hosed_count));
  2409. out += sprintf(out, "complete_transactions: %u\n",
  2410. smi_get_stat(smi, complete_transactions));
  2411. out += sprintf(out, "events: %u\n",
  2412. smi_get_stat(smi, events));
  2413. out += sprintf(out, "watchdog_pretimeouts: %u\n",
  2414. smi_get_stat(smi, watchdog_pretimeouts));
  2415. out += sprintf(out, "incoming_messages: %u\n",
  2416. smi_get_stat(smi, incoming_messages));
  2417. return out - page;
  2418. }
  2419. static int param_read_proc(char *page, char **start, off_t off,
  2420. int count, int *eof, void *data)
  2421. {
  2422. struct smi_info *smi = data;
  2423. return sprintf(page,
  2424. "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
  2425. si_to_str[smi->si_type],
  2426. addr_space_to_str[smi->io.addr_type],
  2427. smi->io.addr_data,
  2428. smi->io.regspacing,
  2429. smi->io.regsize,
  2430. smi->io.regshift,
  2431. smi->irq,
  2432. smi->slave_addr);
  2433. }
  2434. /*
  2435. * oem_data_avail_to_receive_msg_avail
  2436. * @info - smi_info structure with msg_flags set
  2437. *
  2438. * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
  2439. * Returns 1 indicating need to re-run handle_flags().
  2440. */
  2441. static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
  2442. {
  2443. smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
  2444. RECEIVE_MSG_AVAIL);
  2445. return 1;
  2446. }
  2447. /*
  2448. * setup_dell_poweredge_oem_data_handler
  2449. * @info - smi_info.device_id must be populated
  2450. *
  2451. * Systems that match, but have firmware version < 1.40 may assert
  2452. * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
  2453. * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
  2454. * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
  2455. * as RECEIVE_MSG_AVAIL instead.
  2456. *
  2457. * As Dell has no plans to release IPMI 1.5 firmware that *ever*
  2458. * assert the OEM[012] bits, and if it did, the driver would have to
  2459. * change to handle that properly, we don't actually check for the
  2460. * firmware version.
  2461. * Device ID = 0x20 BMC on PowerEdge 8G servers
  2462. * Device Revision = 0x80
  2463. * Firmware Revision1 = 0x01 BMC version 1.40
  2464. * Firmware Revision2 = 0x40 BCD encoded
  2465. * IPMI Version = 0x51 IPMI 1.5
  2466. * Manufacturer ID = A2 02 00 Dell IANA
  2467. *
  2468. * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
  2469. * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
  2470. *
  2471. */
  2472. #define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
  2473. #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
  2474. #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
  2475. #define DELL_IANA_MFR_ID 0x0002a2
  2476. static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
  2477. {
  2478. struct ipmi_device_id *id = &smi_info->device_id;
  2479. if (id->manufacturer_id == DELL_IANA_MFR_ID) {
  2480. if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
  2481. id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
  2482. id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
  2483. smi_info->oem_data_avail_handler =
  2484. oem_data_avail_to_receive_msg_avail;
  2485. } else if (ipmi_version_major(id) < 1 ||
  2486. (ipmi_version_major(id) == 1 &&
  2487. ipmi_version_minor(id) < 5)) {
  2488. smi_info->oem_data_avail_handler =
  2489. oem_data_avail_to_receive_msg_avail;
  2490. }
  2491. }
  2492. }
  2493. #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
  2494. static void return_hosed_msg_badsize(struct smi_info *smi_info)
  2495. {
  2496. struct ipmi_smi_msg *msg = smi_info->curr_msg;
  2497. /* Make it a reponse */
  2498. msg->rsp[0] = msg->data[0] | 4;
  2499. msg->rsp[1] = msg->data[1];
  2500. msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
  2501. msg->rsp_size = 3;
  2502. smi_info->curr_msg = NULL;
  2503. deliver_recv_msg(smi_info, msg);
  2504. }
  2505. /*
  2506. * dell_poweredge_bt_xaction_handler
  2507. * @info - smi_info.device_id must be populated
  2508. *
  2509. * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
  2510. * not respond to a Get SDR command if the length of the data
  2511. * requested is exactly 0x3A, which leads to command timeouts and no
  2512. * data returned. This intercepts such commands, and causes userspace
  2513. * callers to try again with a different-sized buffer, which succeeds.
  2514. */
  2515. #define STORAGE_NETFN 0x0A
  2516. #define STORAGE_CMD_GET_SDR 0x23
  2517. static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
  2518. unsigned long unused,
  2519. void *in)
  2520. {
  2521. struct smi_info *smi_info = in;
  2522. unsigned char *data = smi_info->curr_msg->data;
  2523. unsigned int size = smi_info->curr_msg->data_size;
  2524. if (size >= 8 &&
  2525. (data[0]>>2) == STORAGE_NETFN &&
  2526. data[1] == STORAGE_CMD_GET_SDR &&
  2527. data[7] == 0x3A) {
  2528. return_hosed_msg_badsize(smi_info);
  2529. return NOTIFY_STOP;
  2530. }
  2531. return NOTIFY_DONE;
  2532. }
  2533. static struct notifier_block dell_poweredge_bt_xaction_notifier = {
  2534. .notifier_call = dell_poweredge_bt_xaction_handler,
  2535. };
  2536. /*
  2537. * setup_dell_poweredge_bt_xaction_handler
  2538. * @info - smi_info.device_id must be filled in already
  2539. *
  2540. * Fills in smi_info.device_id.start_transaction_pre_hook
  2541. * when we know what function to use there.
  2542. */
  2543. static void
  2544. setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
  2545. {
  2546. struct ipmi_device_id *id = &smi_info->device_id;
  2547. if (id->manufacturer_id == DELL_IANA_MFR_ID &&
  2548. smi_info->si_type == SI_BT)
  2549. register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
  2550. }
  2551. /*
  2552. * setup_oem_data_handler
  2553. * @info - smi_info.device_id must be filled in already
  2554. *
  2555. * Fills in smi_info.device_id.oem_data_available_handler
  2556. * when we know what function to use there.
  2557. */
  2558. static void setup_oem_data_handler(struct smi_info *smi_info)
  2559. {
  2560. setup_dell_poweredge_oem_data_handler(smi_info);
  2561. }
  2562. static void setup_xaction_handlers(struct smi_info *smi_info)
  2563. {
  2564. setup_dell_poweredge_bt_xaction_handler(smi_info);
  2565. }
  2566. static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
  2567. {
  2568. if (smi_info->intf) {
  2569. /*
  2570. * The timer and thread are only running if the
  2571. * interface has been started up and registered.
  2572. */
  2573. if (smi_info->thread != NULL)
  2574. kthread_stop(smi_info->thread);
  2575. del_timer_sync(&smi_info->si_timer);
  2576. }
  2577. }
  2578. static __devinitdata struct ipmi_default_vals
  2579. {
  2580. int type;
  2581. int port;
  2582. } ipmi_defaults[] =
  2583. {
  2584. { .type = SI_KCS, .port = 0xca2 },
  2585. { .type = SI_SMIC, .port = 0xca9 },
  2586. { .type = SI_BT, .port = 0xe4 },
  2587. { .port = 0 }
  2588. };
  2589. static void __devinit default_find_bmc(void)
  2590. {
  2591. struct smi_info *info;
  2592. int i;
  2593. for (i = 0; ; i++) {
  2594. if (!ipmi_defaults[i].port)
  2595. break;
  2596. #ifdef CONFIG_PPC
  2597. if (check_legacy_ioport(ipmi_defaults[i].port))
  2598. continue;
  2599. #endif
  2600. info = smi_info_alloc();
  2601. if (!info)
  2602. return;
  2603. info->addr_source = SI_DEFAULT;
  2604. info->si_type = ipmi_defaults[i].type;
  2605. info->io_setup = port_setup;
  2606. info->io.addr_data = ipmi_defaults[i].port;
  2607. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  2608. info->io.addr = NULL;
  2609. info->io.regspacing = DEFAULT_REGSPACING;
  2610. info->io.regsize = DEFAULT_REGSPACING;
  2611. info->io.regshift = 0;
  2612. if (add_smi(info) == 0) {
  2613. if ((try_smi_init(info)) == 0) {
  2614. /* Found one... */
  2615. printk(KERN_INFO PFX "Found default %s"
  2616. " state machine at %s address 0x%lx\n",
  2617. si_to_str[info->si_type],
  2618. addr_space_to_str[info->io.addr_type],
  2619. info->io.addr_data);
  2620. } else
  2621. cleanup_one_si(info);
  2622. } else {
  2623. kfree(info);
  2624. }
  2625. }
  2626. }
  2627. static int is_new_interface(struct smi_info *info)
  2628. {
  2629. struct smi_info *e;
  2630. list_for_each_entry(e, &smi_infos, link) {
  2631. if (e->io.addr_type != info->io.addr_type)
  2632. continue;
  2633. if (e->io.addr_data == info->io.addr_data)
  2634. return 0;
  2635. }
  2636. return 1;
  2637. }
  2638. static int add_smi(struct smi_info *new_smi)
  2639. {
  2640. int rv = 0;
  2641. printk(KERN_INFO PFX "Adding %s-specified %s state machine",
  2642. ipmi_addr_src_to_str[new_smi->addr_source],
  2643. si_to_str[new_smi->si_type]);
  2644. mutex_lock(&smi_infos_lock);
  2645. if (!is_new_interface(new_smi)) {
  2646. printk(KERN_CONT " duplicate interface\n");
  2647. rv = -EBUSY;
  2648. goto out_err;
  2649. }
  2650. printk(KERN_CONT "\n");
  2651. /* So we know not to free it unless we have allocated one. */
  2652. new_smi->intf = NULL;
  2653. new_smi->si_sm = NULL;
  2654. new_smi->handlers = NULL;
  2655. list_add_tail(&new_smi->link, &smi_infos);
  2656. out_err:
  2657. mutex_unlock(&smi_infos_lock);
  2658. return rv;
  2659. }
  2660. static int try_smi_init(struct smi_info *new_smi)
  2661. {
  2662. int rv = 0;
  2663. int i;
  2664. printk(KERN_INFO PFX "Trying %s-specified %s state"
  2665. " machine at %s address 0x%lx, slave address 0x%x,"
  2666. " irq %d\n",
  2667. ipmi_addr_src_to_str[new_smi->addr_source],
  2668. si_to_str[new_smi->si_type],
  2669. addr_space_to_str[new_smi->io.addr_type],
  2670. new_smi->io.addr_data,
  2671. new_smi->slave_addr, new_smi->irq);
  2672. switch (new_smi->si_type) {
  2673. case SI_KCS:
  2674. new_smi->handlers = &kcs_smi_handlers;
  2675. break;
  2676. case SI_SMIC:
  2677. new_smi->handlers = &smic_smi_handlers;
  2678. break;
  2679. case SI_BT:
  2680. new_smi->handlers = &bt_smi_handlers;
  2681. break;
  2682. default:
  2683. /* No support for anything else yet. */
  2684. rv = -EIO;
  2685. goto out_err;
  2686. }
  2687. /* Allocate the state machine's data and initialize it. */
  2688. new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
  2689. if (!new_smi->si_sm) {
  2690. printk(KERN_ERR PFX
  2691. "Could not allocate state machine memory\n");
  2692. rv = -ENOMEM;
  2693. goto out_err;
  2694. }
  2695. new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
  2696. &new_smi->io);
  2697. /* Now that we know the I/O size, we can set up the I/O. */
  2698. rv = new_smi->io_setup(new_smi);
  2699. if (rv) {
  2700. printk(KERN_ERR PFX "Could not set up I/O space\n");
  2701. goto out_err;
  2702. }
  2703. /* Do low-level detection first. */
  2704. if (new_smi->handlers->detect(new_smi->si_sm)) {
  2705. if (new_smi->addr_source)
  2706. printk(KERN_INFO PFX "Interface detection failed\n");
  2707. rv = -ENODEV;
  2708. goto out_err;
  2709. }
  2710. /*
  2711. * Attempt a get device id command. If it fails, we probably
  2712. * don't have a BMC here.
  2713. */
  2714. rv = try_get_dev_id(new_smi);
  2715. if (rv) {
  2716. if (new_smi->addr_source)
  2717. printk(KERN_INFO PFX "There appears to be no BMC"
  2718. " at this location\n");
  2719. goto out_err;
  2720. }
  2721. setup_oem_data_handler(new_smi);
  2722. setup_xaction_handlers(new_smi);
  2723. INIT_LIST_HEAD(&(new_smi->xmit_msgs));
  2724. INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
  2725. new_smi->curr_msg = NULL;
  2726. atomic_set(&new_smi->req_events, 0);
  2727. new_smi->run_to_completion = 0;
  2728. for (i = 0; i < SI_NUM_STATS; i++)
  2729. atomic_set(&new_smi->stats[i], 0);
  2730. new_smi->interrupt_disabled = 1;
  2731. atomic_set(&new_smi->stop_operation, 0);
  2732. new_smi->intf_num = smi_num;
  2733. smi_num++;
  2734. rv = try_enable_event_buffer(new_smi);
  2735. if (rv == 0)
  2736. new_smi->has_event_buffer = 1;
  2737. /*
  2738. * Start clearing the flags before we enable interrupts or the
  2739. * timer to avoid racing with the timer.
  2740. */
  2741. start_clear_flags(new_smi);
  2742. /* IRQ is defined to be set when non-zero. */
  2743. if (new_smi->irq)
  2744. new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
  2745. if (!new_smi->dev) {
  2746. /*
  2747. * If we don't already have a device from something
  2748. * else (like PCI), then register a new one.
  2749. */
  2750. new_smi->pdev = platform_device_alloc("ipmi_si",
  2751. new_smi->intf_num);
  2752. if (!new_smi->pdev) {
  2753. printk(KERN_ERR PFX
  2754. "Unable to allocate platform device\n");
  2755. goto out_err;
  2756. }
  2757. new_smi->dev = &new_smi->pdev->dev;
  2758. new_smi->dev->driver = &ipmi_driver.driver;
  2759. rv = platform_device_add(new_smi->pdev);
  2760. if (rv) {
  2761. printk(KERN_ERR PFX
  2762. "Unable to register system interface device:"
  2763. " %d\n",
  2764. rv);
  2765. goto out_err;
  2766. }
  2767. new_smi->dev_registered = 1;
  2768. }
  2769. rv = ipmi_register_smi(&handlers,
  2770. new_smi,
  2771. &new_smi->device_id,
  2772. new_smi->dev,
  2773. "bmc",
  2774. new_smi->slave_addr);
  2775. if (rv) {
  2776. dev_err(new_smi->dev, "Unable to register device: error %d\n",
  2777. rv);
  2778. goto out_err_stop_timer;
  2779. }
  2780. rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
  2781. type_file_read_proc,
  2782. new_smi);
  2783. if (rv) {
  2784. dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
  2785. goto out_err_stop_timer;
  2786. }
  2787. rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
  2788. stat_file_read_proc,
  2789. new_smi);
  2790. if (rv) {
  2791. dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
  2792. goto out_err_stop_timer;
  2793. }
  2794. rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
  2795. param_read_proc,
  2796. new_smi);
  2797. if (rv) {
  2798. dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
  2799. goto out_err_stop_timer;
  2800. }
  2801. dev_info(new_smi->dev, "IPMI %s interface initialized\n",
  2802. si_to_str[new_smi->si_type]);
  2803. return 0;
  2804. out_err_stop_timer:
  2805. atomic_inc(&new_smi->stop_operation);
  2806. wait_for_timer_and_thread(new_smi);
  2807. out_err:
  2808. new_smi->interrupt_disabled = 1;
  2809. if (new_smi->intf) {
  2810. ipmi_unregister_smi(new_smi->intf);
  2811. new_smi->intf = NULL;
  2812. }
  2813. if (new_smi->irq_cleanup) {
  2814. new_smi->irq_cleanup(new_smi);
  2815. new_smi->irq_cleanup = NULL;
  2816. }
  2817. /*
  2818. * Wait until we know that we are out of any interrupt
  2819. * handlers might have been running before we freed the
  2820. * interrupt.
  2821. */
  2822. synchronize_sched();
  2823. if (new_smi->si_sm) {
  2824. if (new_smi->handlers)
  2825. new_smi->handlers->cleanup(new_smi->si_sm);
  2826. kfree(new_smi->si_sm);
  2827. new_smi->si_sm = NULL;
  2828. }
  2829. if (new_smi->addr_source_cleanup) {
  2830. new_smi->addr_source_cleanup(new_smi);
  2831. new_smi->addr_source_cleanup = NULL;
  2832. }
  2833. if (new_smi->io_cleanup) {
  2834. new_smi->io_cleanup(new_smi);
  2835. new_smi->io_cleanup = NULL;
  2836. }
  2837. if (new_smi->dev_registered) {
  2838. platform_device_unregister(new_smi->pdev);
  2839. new_smi->dev_registered = 0;
  2840. }
  2841. return rv;
  2842. }
  2843. static int __devinit init_ipmi_si(void)
  2844. {
  2845. int i;
  2846. char *str;
  2847. int rv;
  2848. struct smi_info *e;
  2849. enum ipmi_addr_src type = SI_INVALID;
  2850. if (initialized)
  2851. return 0;
  2852. initialized = 1;
  2853. /* Register the device drivers. */
  2854. rv = driver_register(&ipmi_driver.driver);
  2855. if (rv) {
  2856. printk(KERN_ERR PFX "Unable to register driver: %d\n", rv);
  2857. return rv;
  2858. }
  2859. /* Parse out the si_type string into its components. */
  2860. str = si_type_str;
  2861. if (*str != '\0') {
  2862. for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
  2863. si_type[i] = str;
  2864. str = strchr(str, ',');
  2865. if (str) {
  2866. *str = '\0';
  2867. str++;
  2868. } else {
  2869. break;
  2870. }
  2871. }
  2872. }
  2873. printk(KERN_INFO "IPMI System Interface driver.\n");
  2874. hardcode_find_bmc();
  2875. /* If the user gave us a device, they presumably want us to use it */
  2876. mutex_lock(&smi_infos_lock);
  2877. if (!list_empty(&smi_infos)) {
  2878. mutex_unlock(&smi_infos_lock);
  2879. return 0;
  2880. }
  2881. mutex_unlock(&smi_infos_lock);
  2882. #ifdef CONFIG_PCI
  2883. rv = pci_register_driver(&ipmi_pci_driver);
  2884. if (rv)
  2885. printk(KERN_ERR PFX "Unable to register PCI driver: %d\n", rv);
  2886. else
  2887. pci_registered = 1;
  2888. #endif
  2889. #ifdef CONFIG_ACPI
  2890. pnp_register_driver(&ipmi_pnp_driver);
  2891. pnp_registered = 1;
  2892. #endif
  2893. #ifdef CONFIG_DMI
  2894. dmi_find_bmc();
  2895. #endif
  2896. #ifdef CONFIG_ACPI
  2897. spmi_find_bmc();
  2898. #endif
  2899. #ifdef CONFIG_PPC_OF
  2900. of_register_platform_driver(&ipmi_of_platform_driver);
  2901. of_registered = 1;
  2902. #endif
  2903. /* We prefer devices with interrupts, but in the case of a machine
  2904. with multiple BMCs we assume that there will be several instances
  2905. of a given type so if we succeed in registering a type then also
  2906. try to register everything else of the same type */
  2907. mutex_lock(&smi_infos_lock);
  2908. list_for_each_entry(e, &smi_infos, link) {
  2909. /* Try to register a device if it has an IRQ and we either
  2910. haven't successfully registered a device yet or this
  2911. device has the same type as one we successfully registered */
  2912. if (e->irq && (!type || e->addr_source == type)) {
  2913. if (!try_smi_init(e)) {
  2914. type = e->addr_source;
  2915. }
  2916. }
  2917. }
  2918. /* type will only have been set if we successfully registered an si */
  2919. if (type) {
  2920. mutex_unlock(&smi_infos_lock);
  2921. return 0;
  2922. }
  2923. /* Fall back to the preferred device */
  2924. list_for_each_entry(e, &smi_infos, link) {
  2925. if (!e->irq && (!type || e->addr_source == type)) {
  2926. if (!try_smi_init(e)) {
  2927. type = e->addr_source;
  2928. }
  2929. }
  2930. }
  2931. mutex_unlock(&smi_infos_lock);
  2932. if (type)
  2933. return 0;
  2934. if (si_trydefaults) {
  2935. mutex_lock(&smi_infos_lock);
  2936. if (list_empty(&smi_infos)) {
  2937. /* No BMC was found, try defaults. */
  2938. mutex_unlock(&smi_infos_lock);
  2939. default_find_bmc();
  2940. } else
  2941. mutex_unlock(&smi_infos_lock);
  2942. }
  2943. mutex_lock(&smi_infos_lock);
  2944. if (unload_when_empty && list_empty(&smi_infos)) {
  2945. mutex_unlock(&smi_infos_lock);
  2946. #ifdef CONFIG_PCI
  2947. if (pci_registered)
  2948. pci_unregister_driver(&ipmi_pci_driver);
  2949. #endif
  2950. #ifdef CONFIG_PPC_OF
  2951. if (of_registered)
  2952. of_unregister_platform_driver(&ipmi_of_platform_driver);
  2953. #endif
  2954. driver_unregister(&ipmi_driver.driver);
  2955. printk(KERN_WARNING PFX
  2956. "Unable to find any System Interface(s)\n");
  2957. return -ENODEV;
  2958. } else {
  2959. mutex_unlock(&smi_infos_lock);
  2960. return 0;
  2961. }
  2962. }
  2963. module_init(init_ipmi_si);
  2964. static void cleanup_one_si(struct smi_info *to_clean)
  2965. {
  2966. int rv = 0;
  2967. unsigned long flags;
  2968. if (!to_clean)
  2969. return;
  2970. list_del(&to_clean->link);
  2971. /* Tell the driver that we are shutting down. */
  2972. atomic_inc(&to_clean->stop_operation);
  2973. /*
  2974. * Make sure the timer and thread are stopped and will not run
  2975. * again.
  2976. */
  2977. wait_for_timer_and_thread(to_clean);
  2978. /*
  2979. * Timeouts are stopped, now make sure the interrupts are off
  2980. * for the device. A little tricky with locks to make sure
  2981. * there are no races.
  2982. */
  2983. spin_lock_irqsave(&to_clean->si_lock, flags);
  2984. while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
  2985. spin_unlock_irqrestore(&to_clean->si_lock, flags);
  2986. poll(to_clean);
  2987. schedule_timeout_uninterruptible(1);
  2988. spin_lock_irqsave(&to_clean->si_lock, flags);
  2989. }
  2990. disable_si_irq(to_clean);
  2991. spin_unlock_irqrestore(&to_clean->si_lock, flags);
  2992. while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
  2993. poll(to_clean);
  2994. schedule_timeout_uninterruptible(1);
  2995. }
  2996. /* Clean up interrupts and make sure that everything is done. */
  2997. if (to_clean->irq_cleanup)
  2998. to_clean->irq_cleanup(to_clean);
  2999. while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
  3000. poll(to_clean);
  3001. schedule_timeout_uninterruptible(1);
  3002. }
  3003. if (to_clean->intf)
  3004. rv = ipmi_unregister_smi(to_clean->intf);
  3005. if (rv) {
  3006. printk(KERN_ERR PFX "Unable to unregister device: errno=%d\n",
  3007. rv);
  3008. }
  3009. if (to_clean->handlers)
  3010. to_clean->handlers->cleanup(to_clean->si_sm);
  3011. kfree(to_clean->si_sm);
  3012. if (to_clean->addr_source_cleanup)
  3013. to_clean->addr_source_cleanup(to_clean);
  3014. if (to_clean->io_cleanup)
  3015. to_clean->io_cleanup(to_clean);
  3016. if (to_clean->dev_registered)
  3017. platform_device_unregister(to_clean->pdev);
  3018. kfree(to_clean);
  3019. }
  3020. static void __exit cleanup_ipmi_si(void)
  3021. {
  3022. struct smi_info *e, *tmp_e;
  3023. if (!initialized)
  3024. return;
  3025. #ifdef CONFIG_PCI
  3026. if (pci_registered)
  3027. pci_unregister_driver(&ipmi_pci_driver);
  3028. #endif
  3029. #ifdef CONFIG_ACPI
  3030. if (pnp_registered)
  3031. pnp_unregister_driver(&ipmi_pnp_driver);
  3032. #endif
  3033. #ifdef CONFIG_PPC_OF
  3034. if (of_registered)
  3035. of_unregister_platform_driver(&ipmi_of_platform_driver);
  3036. #endif
  3037. mutex_lock(&smi_infos_lock);
  3038. list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
  3039. cleanup_one_si(e);
  3040. mutex_unlock(&smi_infos_lock);
  3041. driver_unregister(&ipmi_driver.driver);
  3042. }
  3043. module_exit(cleanup_ipmi_si);
  3044. MODULE_LICENSE("GPL");
  3045. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  3046. MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
  3047. " system interfaces.");