ipmi_si_intf.c 85 KB

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