ipmi_si_intf.c 87 KB

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