ipmi_si_intf.c 88 KB

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