ipmi_si_intf.c 83 KB

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