ipmi_si_intf.c 88 KB

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