ipmi_si_intf.c 89 KB

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