ipmi_si_intf.c 87 KB

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