ipmi_si_intf.c 93 KB

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