ipmi_si_intf.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  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. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  27. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  28. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * You should have received a copy of the GNU General Public License along
  31. * with this program; if not, write to the Free Software Foundation, Inc.,
  32. * 675 Mass Ave, Cambridge, MA 02139, USA.
  33. */
  34. /*
  35. * This file holds the "policy" for the interface to the SMI state
  36. * machine. It does the configuration, handles timers and interrupts,
  37. * and drives the real SMI state machine.
  38. */
  39. #include <linux/config.h>
  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. #ifdef CONFIG_HIGH_RES_TIMERS
  57. #include <linux/hrtime.h>
  58. # if defined(schedule_next_int)
  59. /* Old high-res timer code, do translations. */
  60. # define get_arch_cycles(a) quick_update_jiffies_sub(a)
  61. # define arch_cycles_per_jiffy cycles_per_jiffies
  62. # endif
  63. static inline void add_usec_to_timer(struct timer_list *t, long v)
  64. {
  65. t->arch_cycle_expires += nsec_to_arch_cycle(v * 1000);
  66. while (t->arch_cycle_expires >= arch_cycles_per_jiffy)
  67. {
  68. t->expires++;
  69. t->arch_cycle_expires -= arch_cycles_per_jiffy;
  70. }
  71. }
  72. #endif
  73. #include <linux/interrupt.h>
  74. #include <linux/rcupdate.h>
  75. #include <linux/ipmi_smi.h>
  76. #include <asm/io.h>
  77. #include "ipmi_si_sm.h"
  78. #include <linux/init.h>
  79. #include <linux/dmi.h>
  80. /* Measure times between events in the driver. */
  81. #undef DEBUG_TIMING
  82. /* Call every 10 ms. */
  83. #define SI_TIMEOUT_TIME_USEC 10000
  84. #define SI_USEC_PER_JIFFY (1000000/HZ)
  85. #define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
  86. #define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
  87. short timeout */
  88. enum si_intf_state {
  89. SI_NORMAL,
  90. SI_GETTING_FLAGS,
  91. SI_GETTING_EVENTS,
  92. SI_CLEARING_FLAGS,
  93. SI_CLEARING_FLAGS_THEN_SET_IRQ,
  94. SI_GETTING_MESSAGES,
  95. SI_ENABLE_INTERRUPTS1,
  96. SI_ENABLE_INTERRUPTS2
  97. /* FIXME - add watchdog stuff. */
  98. };
  99. /* Some BT-specific defines we need here. */
  100. #define IPMI_BT_INTMASK_REG 2
  101. #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
  102. #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
  103. enum si_type {
  104. SI_KCS, SI_SMIC, SI_BT
  105. };
  106. static char *si_to_str[] = { "KCS", "SMIC", "BT" };
  107. #define DEVICE_NAME "ipmi_si"
  108. static struct device_driver ipmi_driver =
  109. {
  110. .name = DEVICE_NAME,
  111. .bus = &platform_bus_type
  112. };
  113. struct smi_info
  114. {
  115. int intf_num;
  116. ipmi_smi_t intf;
  117. struct si_sm_data *si_sm;
  118. struct si_sm_handlers *handlers;
  119. enum si_type si_type;
  120. spinlock_t si_lock;
  121. spinlock_t msg_lock;
  122. struct list_head xmit_msgs;
  123. struct list_head hp_xmit_msgs;
  124. struct ipmi_smi_msg *curr_msg;
  125. enum si_intf_state si_state;
  126. /* Used to handle the various types of I/O that can occur with
  127. IPMI */
  128. struct si_sm_io io;
  129. int (*io_setup)(struct smi_info *info);
  130. void (*io_cleanup)(struct smi_info *info);
  131. int (*irq_setup)(struct smi_info *info);
  132. void (*irq_cleanup)(struct smi_info *info);
  133. unsigned int io_size;
  134. char *addr_source; /* ACPI, PCI, SMBIOS, hardcode, default. */
  135. void (*addr_source_cleanup)(struct smi_info *info);
  136. void *addr_source_data;
  137. /* Per-OEM handler, called from handle_flags().
  138. Returns 1 when handle_flags() needs to be re-run
  139. or 0 indicating it set si_state itself.
  140. */
  141. int (*oem_data_avail_handler)(struct smi_info *smi_info);
  142. /* Flags from the last GET_MSG_FLAGS command, used when an ATTN
  143. is set to hold the flags until we are done handling everything
  144. from the flags. */
  145. #define RECEIVE_MSG_AVAIL 0x01
  146. #define EVENT_MSG_BUFFER_FULL 0x02
  147. #define WDT_PRE_TIMEOUT_INT 0x08
  148. #define OEM0_DATA_AVAIL 0x20
  149. #define OEM1_DATA_AVAIL 0x40
  150. #define OEM2_DATA_AVAIL 0x80
  151. #define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
  152. OEM1_DATA_AVAIL | \
  153. OEM2_DATA_AVAIL)
  154. unsigned char msg_flags;
  155. /* If set to true, this will request events the next time the
  156. state machine is idle. */
  157. atomic_t req_events;
  158. /* If true, run the state machine to completion on every send
  159. call. Generally used after a panic to make sure stuff goes
  160. out. */
  161. int run_to_completion;
  162. /* The I/O port of an SI interface. */
  163. int port;
  164. /* The space between start addresses of the two ports. For
  165. instance, if the first port is 0xca2 and the spacing is 4, then
  166. the second port is 0xca6. */
  167. unsigned int spacing;
  168. /* zero if no irq; */
  169. int irq;
  170. /* The timer for this si. */
  171. struct timer_list si_timer;
  172. /* The time (in jiffies) the last timeout occurred at. */
  173. unsigned long last_timeout_jiffies;
  174. /* Used to gracefully stop the timer without race conditions. */
  175. atomic_t stop_operation;
  176. /* The driver will disable interrupts when it gets into a
  177. situation where it cannot handle messages due to lack of
  178. memory. Once that situation clears up, it will re-enable
  179. interrupts. */
  180. int interrupt_disabled;
  181. /* From the get device id response... */
  182. struct ipmi_device_id device_id;
  183. /* Driver model stuff. */
  184. struct device *dev;
  185. struct platform_device *pdev;
  186. /* True if we allocated the device, false if it came from
  187. * someplace else (like PCI). */
  188. int dev_registered;
  189. /* Slave address, could be reported from DMI. */
  190. unsigned char slave_addr;
  191. /* Counters and things for the proc filesystem. */
  192. spinlock_t count_lock;
  193. unsigned long short_timeouts;
  194. unsigned long long_timeouts;
  195. unsigned long timeout_restarts;
  196. unsigned long idles;
  197. unsigned long interrupts;
  198. unsigned long attentions;
  199. unsigned long flag_fetches;
  200. unsigned long hosed_count;
  201. unsigned long complete_transactions;
  202. unsigned long events;
  203. unsigned long watchdog_pretimeouts;
  204. unsigned long incoming_messages;
  205. struct task_struct *thread;
  206. struct list_head link;
  207. };
  208. static int try_smi_init(struct smi_info *smi);
  209. static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
  210. static int register_xaction_notifier(struct notifier_block * nb)
  211. {
  212. return atomic_notifier_chain_register(&xaction_notifier_list, nb);
  213. }
  214. static void si_restart_short_timer(struct smi_info *smi_info);
  215. static void deliver_recv_msg(struct smi_info *smi_info,
  216. struct ipmi_smi_msg *msg)
  217. {
  218. /* Deliver the message to the upper layer with the lock
  219. released. */
  220. spin_unlock(&(smi_info->si_lock));
  221. ipmi_smi_msg_received(smi_info->intf, msg);
  222. spin_lock(&(smi_info->si_lock));
  223. }
  224. static void return_hosed_msg(struct smi_info *smi_info)
  225. {
  226. struct ipmi_smi_msg *msg = smi_info->curr_msg;
  227. /* Make it a reponse */
  228. msg->rsp[0] = msg->data[0] | 4;
  229. msg->rsp[1] = msg->data[1];
  230. msg->rsp[2] = 0xFF; /* Unknown error. */
  231. msg->rsp_size = 3;
  232. smi_info->curr_msg = NULL;
  233. deliver_recv_msg(smi_info, msg);
  234. }
  235. static enum si_sm_result start_next_msg(struct smi_info *smi_info)
  236. {
  237. int rv;
  238. struct list_head *entry = NULL;
  239. #ifdef DEBUG_TIMING
  240. struct timeval t;
  241. #endif
  242. /* No need to save flags, we aleady have interrupts off and we
  243. already hold the SMI lock. */
  244. spin_lock(&(smi_info->msg_lock));
  245. /* Pick the high priority queue first. */
  246. if (!list_empty(&(smi_info->hp_xmit_msgs))) {
  247. entry = smi_info->hp_xmit_msgs.next;
  248. } else if (!list_empty(&(smi_info->xmit_msgs))) {
  249. entry = smi_info->xmit_msgs.next;
  250. }
  251. if (!entry) {
  252. smi_info->curr_msg = NULL;
  253. rv = SI_SM_IDLE;
  254. } else {
  255. int err;
  256. list_del(entry);
  257. smi_info->curr_msg = list_entry(entry,
  258. struct ipmi_smi_msg,
  259. link);
  260. #ifdef DEBUG_TIMING
  261. do_gettimeofday(&t);
  262. printk("**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  263. #endif
  264. err = atomic_notifier_call_chain(&xaction_notifier_list,
  265. 0, smi_info);
  266. if (err & NOTIFY_STOP_MASK) {
  267. rv = SI_SM_CALL_WITHOUT_DELAY;
  268. goto out;
  269. }
  270. err = smi_info->handlers->start_transaction(
  271. smi_info->si_sm,
  272. smi_info->curr_msg->data,
  273. smi_info->curr_msg->data_size);
  274. if (err) {
  275. return_hosed_msg(smi_info);
  276. }
  277. rv = SI_SM_CALL_WITHOUT_DELAY;
  278. }
  279. out:
  280. spin_unlock(&(smi_info->msg_lock));
  281. return rv;
  282. }
  283. static void start_enable_irq(struct smi_info *smi_info)
  284. {
  285. unsigned char msg[2];
  286. /* If we are enabling interrupts, we have to tell the
  287. BMC to use them. */
  288. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  289. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  290. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  291. smi_info->si_state = SI_ENABLE_INTERRUPTS1;
  292. }
  293. static void start_clear_flags(struct smi_info *smi_info)
  294. {
  295. unsigned char msg[3];
  296. /* Make sure the watchdog pre-timeout flag is not set at startup. */
  297. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  298. msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
  299. msg[2] = WDT_PRE_TIMEOUT_INT;
  300. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
  301. smi_info->si_state = SI_CLEARING_FLAGS;
  302. }
  303. /* When we have a situtaion where we run out of memory and cannot
  304. allocate messages, we just leave them in the BMC and run the system
  305. polled until we can allocate some memory. Once we have some
  306. memory, we will re-enable the interrupt. */
  307. static inline void disable_si_irq(struct smi_info *smi_info)
  308. {
  309. if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
  310. disable_irq_nosync(smi_info->irq);
  311. smi_info->interrupt_disabled = 1;
  312. }
  313. }
  314. static inline void enable_si_irq(struct smi_info *smi_info)
  315. {
  316. if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
  317. enable_irq(smi_info->irq);
  318. smi_info->interrupt_disabled = 0;
  319. }
  320. }
  321. static void handle_flags(struct smi_info *smi_info)
  322. {
  323. retry:
  324. if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
  325. /* Watchdog pre-timeout */
  326. spin_lock(&smi_info->count_lock);
  327. smi_info->watchdog_pretimeouts++;
  328. spin_unlock(&smi_info->count_lock);
  329. start_clear_flags(smi_info);
  330. smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
  331. spin_unlock(&(smi_info->si_lock));
  332. ipmi_smi_watchdog_pretimeout(smi_info->intf);
  333. spin_lock(&(smi_info->si_lock));
  334. } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
  335. /* Messages available. */
  336. smi_info->curr_msg = ipmi_alloc_smi_msg();
  337. if (!smi_info->curr_msg) {
  338. disable_si_irq(smi_info);
  339. smi_info->si_state = SI_NORMAL;
  340. return;
  341. }
  342. enable_si_irq(smi_info);
  343. smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  344. smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
  345. smi_info->curr_msg->data_size = 2;
  346. smi_info->handlers->start_transaction(
  347. smi_info->si_sm,
  348. smi_info->curr_msg->data,
  349. smi_info->curr_msg->data_size);
  350. smi_info->si_state = SI_GETTING_MESSAGES;
  351. } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
  352. /* Events available. */
  353. smi_info->curr_msg = ipmi_alloc_smi_msg();
  354. if (!smi_info->curr_msg) {
  355. disable_si_irq(smi_info);
  356. smi_info->si_state = SI_NORMAL;
  357. return;
  358. }
  359. enable_si_irq(smi_info);
  360. smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  361. smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
  362. smi_info->curr_msg->data_size = 2;
  363. smi_info->handlers->start_transaction(
  364. smi_info->si_sm,
  365. smi_info->curr_msg->data,
  366. smi_info->curr_msg->data_size);
  367. smi_info->si_state = SI_GETTING_EVENTS;
  368. } else if (smi_info->msg_flags & OEM_DATA_AVAIL) {
  369. if (smi_info->oem_data_avail_handler)
  370. if (smi_info->oem_data_avail_handler(smi_info))
  371. goto retry;
  372. } else {
  373. smi_info->si_state = SI_NORMAL;
  374. }
  375. }
  376. static void handle_transaction_done(struct smi_info *smi_info)
  377. {
  378. struct ipmi_smi_msg *msg;
  379. #ifdef DEBUG_TIMING
  380. struct timeval t;
  381. do_gettimeofday(&t);
  382. printk("**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  383. #endif
  384. switch (smi_info->si_state) {
  385. case SI_NORMAL:
  386. if (!smi_info->curr_msg)
  387. break;
  388. smi_info->curr_msg->rsp_size
  389. = smi_info->handlers->get_result(
  390. smi_info->si_sm,
  391. smi_info->curr_msg->rsp,
  392. IPMI_MAX_MSG_LENGTH);
  393. /* Do this here becase deliver_recv_msg() releases the
  394. lock, and a new message can be put in during the
  395. time the lock is released. */
  396. msg = smi_info->curr_msg;
  397. smi_info->curr_msg = NULL;
  398. deliver_recv_msg(smi_info, msg);
  399. break;
  400. case SI_GETTING_FLAGS:
  401. {
  402. unsigned char msg[4];
  403. unsigned int len;
  404. /* We got the flags from the SMI, now handle them. */
  405. len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  406. if (msg[2] != 0) {
  407. /* Error fetching flags, just give up for
  408. now. */
  409. smi_info->si_state = SI_NORMAL;
  410. } else if (len < 4) {
  411. /* Hmm, no flags. That's technically illegal, but
  412. don't use uninitialized data. */
  413. smi_info->si_state = SI_NORMAL;
  414. } else {
  415. smi_info->msg_flags = msg[3];
  416. handle_flags(smi_info);
  417. }
  418. break;
  419. }
  420. case SI_CLEARING_FLAGS:
  421. case SI_CLEARING_FLAGS_THEN_SET_IRQ:
  422. {
  423. unsigned char msg[3];
  424. /* We cleared the flags. */
  425. smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
  426. if (msg[2] != 0) {
  427. /* Error clearing flags */
  428. printk(KERN_WARNING
  429. "ipmi_si: Error clearing flags: %2.2x\n",
  430. msg[2]);
  431. }
  432. if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
  433. start_enable_irq(smi_info);
  434. else
  435. smi_info->si_state = SI_NORMAL;
  436. break;
  437. }
  438. case SI_GETTING_EVENTS:
  439. {
  440. smi_info->curr_msg->rsp_size
  441. = smi_info->handlers->get_result(
  442. smi_info->si_sm,
  443. smi_info->curr_msg->rsp,
  444. IPMI_MAX_MSG_LENGTH);
  445. /* Do this here becase deliver_recv_msg() releases the
  446. lock, and a new message can be put in during the
  447. time the lock is released. */
  448. msg = smi_info->curr_msg;
  449. smi_info->curr_msg = NULL;
  450. if (msg->rsp[2] != 0) {
  451. /* Error getting event, probably done. */
  452. msg->done(msg);
  453. /* Take off the event flag. */
  454. smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
  455. handle_flags(smi_info);
  456. } else {
  457. spin_lock(&smi_info->count_lock);
  458. smi_info->events++;
  459. spin_unlock(&smi_info->count_lock);
  460. /* Do this before we deliver the message
  461. because delivering the message releases the
  462. lock and something else can mess with the
  463. state. */
  464. handle_flags(smi_info);
  465. deliver_recv_msg(smi_info, msg);
  466. }
  467. break;
  468. }
  469. case SI_GETTING_MESSAGES:
  470. {
  471. smi_info->curr_msg->rsp_size
  472. = smi_info->handlers->get_result(
  473. smi_info->si_sm,
  474. smi_info->curr_msg->rsp,
  475. IPMI_MAX_MSG_LENGTH);
  476. /* Do this here becase deliver_recv_msg() releases the
  477. lock, and a new message can be put in during the
  478. time the lock is released. */
  479. msg = smi_info->curr_msg;
  480. smi_info->curr_msg = NULL;
  481. if (msg->rsp[2] != 0) {
  482. /* Error getting event, probably done. */
  483. msg->done(msg);
  484. /* Take off the msg flag. */
  485. smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
  486. handle_flags(smi_info);
  487. } else {
  488. spin_lock(&smi_info->count_lock);
  489. smi_info->incoming_messages++;
  490. spin_unlock(&smi_info->count_lock);
  491. /* Do this before we deliver the message
  492. because delivering the message releases the
  493. lock and something else can mess with the
  494. state. */
  495. handle_flags(smi_info);
  496. deliver_recv_msg(smi_info, msg);
  497. }
  498. break;
  499. }
  500. case SI_ENABLE_INTERRUPTS1:
  501. {
  502. unsigned char msg[4];
  503. /* We got the flags from the SMI, now handle them. */
  504. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  505. if (msg[2] != 0) {
  506. printk(KERN_WARNING
  507. "ipmi_si: Could not enable interrupts"
  508. ", failed get, using polled mode.\n");
  509. smi_info->si_state = SI_NORMAL;
  510. } else {
  511. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  512. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  513. msg[2] = msg[3] | 1; /* enable msg queue int */
  514. smi_info->handlers->start_transaction(
  515. smi_info->si_sm, msg, 3);
  516. smi_info->si_state = SI_ENABLE_INTERRUPTS2;
  517. }
  518. break;
  519. }
  520. case SI_ENABLE_INTERRUPTS2:
  521. {
  522. unsigned char msg[4];
  523. /* We got the flags from the SMI, now handle them. */
  524. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  525. if (msg[2] != 0) {
  526. printk(KERN_WARNING
  527. "ipmi_si: Could not enable interrupts"
  528. ", failed set, using polled mode.\n");
  529. }
  530. smi_info->si_state = SI_NORMAL;
  531. break;
  532. }
  533. }
  534. }
  535. /* Called on timeouts and events. Timeouts should pass the elapsed
  536. time, interrupts should pass in zero. */
  537. static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
  538. int time)
  539. {
  540. enum si_sm_result si_sm_result;
  541. restart:
  542. /* There used to be a loop here that waited a little while
  543. (around 25us) before giving up. That turned out to be
  544. pointless, the minimum delays I was seeing were in the 300us
  545. range, which is far too long to wait in an interrupt. So
  546. we just run until the state machine tells us something
  547. happened or it needs a delay. */
  548. si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
  549. time = 0;
  550. while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
  551. {
  552. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  553. }
  554. if (si_sm_result == SI_SM_TRANSACTION_COMPLETE)
  555. {
  556. spin_lock(&smi_info->count_lock);
  557. smi_info->complete_transactions++;
  558. spin_unlock(&smi_info->count_lock);
  559. handle_transaction_done(smi_info);
  560. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  561. }
  562. else if (si_sm_result == SI_SM_HOSED)
  563. {
  564. spin_lock(&smi_info->count_lock);
  565. smi_info->hosed_count++;
  566. spin_unlock(&smi_info->count_lock);
  567. /* Do the before return_hosed_msg, because that
  568. releases the lock. */
  569. smi_info->si_state = SI_NORMAL;
  570. if (smi_info->curr_msg != NULL) {
  571. /* If we were handling a user message, format
  572. a response to send to the upper layer to
  573. tell it about the error. */
  574. return_hosed_msg(smi_info);
  575. }
  576. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  577. }
  578. /* We prefer handling attn over new messages. */
  579. if (si_sm_result == SI_SM_ATTN)
  580. {
  581. unsigned char msg[2];
  582. spin_lock(&smi_info->count_lock);
  583. smi_info->attentions++;
  584. spin_unlock(&smi_info->count_lock);
  585. /* Got a attn, send down a get message flags to see
  586. what's causing it. It would be better to handle
  587. this in the upper layer, but due to the way
  588. interrupts work with the SMI, that's not really
  589. possible. */
  590. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  591. msg[1] = IPMI_GET_MSG_FLAGS_CMD;
  592. smi_info->handlers->start_transaction(
  593. smi_info->si_sm, msg, 2);
  594. smi_info->si_state = SI_GETTING_FLAGS;
  595. goto restart;
  596. }
  597. /* If we are currently idle, try to start the next message. */
  598. if (si_sm_result == SI_SM_IDLE) {
  599. spin_lock(&smi_info->count_lock);
  600. smi_info->idles++;
  601. spin_unlock(&smi_info->count_lock);
  602. si_sm_result = start_next_msg(smi_info);
  603. if (si_sm_result != SI_SM_IDLE)
  604. goto restart;
  605. }
  606. if ((si_sm_result == SI_SM_IDLE)
  607. && (atomic_read(&smi_info->req_events)))
  608. {
  609. /* We are idle and the upper layer requested that I fetch
  610. events, so do so. */
  611. unsigned char msg[2];
  612. spin_lock(&smi_info->count_lock);
  613. smi_info->flag_fetches++;
  614. spin_unlock(&smi_info->count_lock);
  615. atomic_set(&smi_info->req_events, 0);
  616. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  617. msg[1] = IPMI_GET_MSG_FLAGS_CMD;
  618. smi_info->handlers->start_transaction(
  619. smi_info->si_sm, msg, 2);
  620. smi_info->si_state = SI_GETTING_FLAGS;
  621. goto restart;
  622. }
  623. return si_sm_result;
  624. }
  625. static void sender(void *send_info,
  626. struct ipmi_smi_msg *msg,
  627. int priority)
  628. {
  629. struct smi_info *smi_info = send_info;
  630. enum si_sm_result result;
  631. unsigned long flags;
  632. #ifdef DEBUG_TIMING
  633. struct timeval t;
  634. #endif
  635. spin_lock_irqsave(&(smi_info->msg_lock), flags);
  636. #ifdef DEBUG_TIMING
  637. do_gettimeofday(&t);
  638. printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  639. #endif
  640. if (smi_info->run_to_completion) {
  641. /* If we are running to completion, then throw it in
  642. the list and run transactions until everything is
  643. clear. Priority doesn't matter here. */
  644. list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
  645. /* We have to release the msg lock and claim the smi
  646. lock in this case, because of race conditions. */
  647. spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
  648. spin_lock_irqsave(&(smi_info->si_lock), flags);
  649. result = smi_event_handler(smi_info, 0);
  650. while (result != SI_SM_IDLE) {
  651. udelay(SI_SHORT_TIMEOUT_USEC);
  652. result = smi_event_handler(smi_info,
  653. SI_SHORT_TIMEOUT_USEC);
  654. }
  655. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  656. return;
  657. } else {
  658. if (priority > 0) {
  659. list_add_tail(&(msg->link), &(smi_info->hp_xmit_msgs));
  660. } else {
  661. list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
  662. }
  663. }
  664. spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
  665. spin_lock_irqsave(&(smi_info->si_lock), flags);
  666. if ((smi_info->si_state == SI_NORMAL)
  667. && (smi_info->curr_msg == NULL))
  668. {
  669. start_next_msg(smi_info);
  670. si_restart_short_timer(smi_info);
  671. }
  672. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  673. }
  674. static void set_run_to_completion(void *send_info, int i_run_to_completion)
  675. {
  676. struct smi_info *smi_info = send_info;
  677. enum si_sm_result result;
  678. unsigned long flags;
  679. spin_lock_irqsave(&(smi_info->si_lock), flags);
  680. smi_info->run_to_completion = i_run_to_completion;
  681. if (i_run_to_completion) {
  682. result = smi_event_handler(smi_info, 0);
  683. while (result != SI_SM_IDLE) {
  684. udelay(SI_SHORT_TIMEOUT_USEC);
  685. result = smi_event_handler(smi_info,
  686. SI_SHORT_TIMEOUT_USEC);
  687. }
  688. }
  689. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  690. }
  691. static int ipmi_thread(void *data)
  692. {
  693. struct smi_info *smi_info = data;
  694. unsigned long flags;
  695. enum si_sm_result smi_result;
  696. set_user_nice(current, 19);
  697. while (!kthread_should_stop()) {
  698. spin_lock_irqsave(&(smi_info->si_lock), flags);
  699. smi_result = smi_event_handler(smi_info, 0);
  700. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  701. if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
  702. /* do nothing */
  703. }
  704. else if (smi_result == SI_SM_CALL_WITH_DELAY)
  705. udelay(1);
  706. else
  707. schedule_timeout_interruptible(1);
  708. }
  709. return 0;
  710. }
  711. static void poll(void *send_info)
  712. {
  713. struct smi_info *smi_info = send_info;
  714. smi_event_handler(smi_info, 0);
  715. }
  716. static void request_events(void *send_info)
  717. {
  718. struct smi_info *smi_info = send_info;
  719. atomic_set(&smi_info->req_events, 1);
  720. }
  721. static int initialized = 0;
  722. /* Must be called with interrupts off and with the si_lock held. */
  723. static void si_restart_short_timer(struct smi_info *smi_info)
  724. {
  725. #if defined(CONFIG_HIGH_RES_TIMERS)
  726. unsigned long flags;
  727. unsigned long jiffies_now;
  728. unsigned long seq;
  729. if (del_timer(&(smi_info->si_timer))) {
  730. /* If we don't delete the timer, then it will go off
  731. immediately, anyway. So we only process if we
  732. actually delete the timer. */
  733. do {
  734. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  735. jiffies_now = jiffies;
  736. smi_info->si_timer.expires = jiffies_now;
  737. smi_info->si_timer.arch_cycle_expires
  738. = get_arch_cycles(jiffies_now);
  739. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  740. add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
  741. add_timer(&(smi_info->si_timer));
  742. spin_lock_irqsave(&smi_info->count_lock, flags);
  743. smi_info->timeout_restarts++;
  744. spin_unlock_irqrestore(&smi_info->count_lock, flags);
  745. }
  746. #endif
  747. }
  748. static void smi_timeout(unsigned long data)
  749. {
  750. struct smi_info *smi_info = (struct smi_info *) data;
  751. enum si_sm_result smi_result;
  752. unsigned long flags;
  753. unsigned long jiffies_now;
  754. long time_diff;
  755. #ifdef DEBUG_TIMING
  756. struct timeval t;
  757. #endif
  758. if (atomic_read(&smi_info->stop_operation))
  759. return;
  760. spin_lock_irqsave(&(smi_info->si_lock), flags);
  761. #ifdef DEBUG_TIMING
  762. do_gettimeofday(&t);
  763. printk("**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  764. #endif
  765. jiffies_now = jiffies;
  766. time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
  767. * SI_USEC_PER_JIFFY);
  768. smi_result = smi_event_handler(smi_info, time_diff);
  769. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  770. smi_info->last_timeout_jiffies = jiffies_now;
  771. if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
  772. /* Running with interrupts, only do long timeouts. */
  773. smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
  774. spin_lock_irqsave(&smi_info->count_lock, flags);
  775. smi_info->long_timeouts++;
  776. spin_unlock_irqrestore(&smi_info->count_lock, flags);
  777. goto do_add_timer;
  778. }
  779. /* If the state machine asks for a short delay, then shorten
  780. the timer timeout. */
  781. if (smi_result == SI_SM_CALL_WITH_DELAY) {
  782. #if defined(CONFIG_HIGH_RES_TIMERS)
  783. unsigned long seq;
  784. #endif
  785. spin_lock_irqsave(&smi_info->count_lock, flags);
  786. smi_info->short_timeouts++;
  787. spin_unlock_irqrestore(&smi_info->count_lock, flags);
  788. #if defined(CONFIG_HIGH_RES_TIMERS)
  789. do {
  790. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  791. smi_info->si_timer.expires = jiffies;
  792. smi_info->si_timer.arch_cycle_expires
  793. = get_arch_cycles(smi_info->si_timer.expires);
  794. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  795. add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
  796. #else
  797. smi_info->si_timer.expires = jiffies + 1;
  798. #endif
  799. } else {
  800. spin_lock_irqsave(&smi_info->count_lock, flags);
  801. smi_info->long_timeouts++;
  802. spin_unlock_irqrestore(&smi_info->count_lock, flags);
  803. smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
  804. #if defined(CONFIG_HIGH_RES_TIMERS)
  805. smi_info->si_timer.arch_cycle_expires = 0;
  806. #endif
  807. }
  808. do_add_timer:
  809. add_timer(&(smi_info->si_timer));
  810. }
  811. static irqreturn_t si_irq_handler(int irq, void *data, struct pt_regs *regs)
  812. {
  813. struct smi_info *smi_info = data;
  814. unsigned long flags;
  815. #ifdef DEBUG_TIMING
  816. struct timeval t;
  817. #endif
  818. spin_lock_irqsave(&(smi_info->si_lock), flags);
  819. spin_lock(&smi_info->count_lock);
  820. smi_info->interrupts++;
  821. spin_unlock(&smi_info->count_lock);
  822. if (atomic_read(&smi_info->stop_operation))
  823. goto out;
  824. #ifdef DEBUG_TIMING
  825. do_gettimeofday(&t);
  826. printk("**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  827. #endif
  828. smi_event_handler(smi_info, 0);
  829. out:
  830. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  831. return IRQ_HANDLED;
  832. }
  833. static irqreturn_t si_bt_irq_handler(int irq, void *data, struct pt_regs *regs)
  834. {
  835. struct smi_info *smi_info = data;
  836. /* We need to clear the IRQ flag for the BT interface. */
  837. smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
  838. IPMI_BT_INTMASK_CLEAR_IRQ_BIT
  839. | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  840. return si_irq_handler(irq, data, regs);
  841. }
  842. static int smi_start_processing(void *send_info,
  843. ipmi_smi_t intf)
  844. {
  845. struct smi_info *new_smi = send_info;
  846. new_smi->intf = intf;
  847. /* Set up the timer that drives the interface. */
  848. setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
  849. new_smi->last_timeout_jiffies = jiffies;
  850. mod_timer(&new_smi->si_timer, jiffies + SI_TIMEOUT_JIFFIES);
  851. if (new_smi->si_type != SI_BT) {
  852. new_smi->thread = kthread_run(ipmi_thread, new_smi,
  853. "kipmi%d", new_smi->intf_num);
  854. if (IS_ERR(new_smi->thread)) {
  855. printk(KERN_NOTICE "ipmi_si_intf: Could not start"
  856. " kernel thread due to error %ld, only using"
  857. " timers to drive the interface\n",
  858. PTR_ERR(new_smi->thread));
  859. new_smi->thread = NULL;
  860. }
  861. }
  862. return 0;
  863. }
  864. static struct ipmi_smi_handlers handlers =
  865. {
  866. .owner = THIS_MODULE,
  867. .start_processing = smi_start_processing,
  868. .sender = sender,
  869. .request_events = request_events,
  870. .set_run_to_completion = set_run_to_completion,
  871. .poll = poll,
  872. };
  873. /* There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
  874. a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS */
  875. #define SI_MAX_PARMS 4
  876. static LIST_HEAD(smi_infos);
  877. static DEFINE_MUTEX(smi_infos_lock);
  878. static int smi_num; /* Used to sequence the SMIs */
  879. #define DEFAULT_REGSPACING 1
  880. static int si_trydefaults = 1;
  881. static char *si_type[SI_MAX_PARMS];
  882. #define MAX_SI_TYPE_STR 30
  883. static char si_type_str[MAX_SI_TYPE_STR];
  884. static unsigned long addrs[SI_MAX_PARMS];
  885. static int num_addrs;
  886. static unsigned int ports[SI_MAX_PARMS];
  887. static int num_ports;
  888. static int irqs[SI_MAX_PARMS];
  889. static int num_irqs;
  890. static int regspacings[SI_MAX_PARMS];
  891. static int num_regspacings = 0;
  892. static int regsizes[SI_MAX_PARMS];
  893. static int num_regsizes = 0;
  894. static int regshifts[SI_MAX_PARMS];
  895. static int num_regshifts = 0;
  896. static int slave_addrs[SI_MAX_PARMS];
  897. static int num_slave_addrs = 0;
  898. module_param_named(trydefaults, si_trydefaults, bool, 0);
  899. MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
  900. " default scan of the KCS and SMIC interface at the standard"
  901. " address");
  902. module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
  903. MODULE_PARM_DESC(type, "Defines the type of each interface, each"
  904. " interface separated by commas. The types are 'kcs',"
  905. " 'smic', and 'bt'. For example si_type=kcs,bt will set"
  906. " the first interface to kcs and the second to bt");
  907. module_param_array(addrs, long, &num_addrs, 0);
  908. MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
  909. " addresses separated by commas. Only use if an interface"
  910. " is in memory. Otherwise, set it to zero or leave"
  911. " it blank.");
  912. module_param_array(ports, int, &num_ports, 0);
  913. MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
  914. " addresses separated by commas. Only use if an interface"
  915. " is a port. Otherwise, set it to zero or leave"
  916. " it blank.");
  917. module_param_array(irqs, int, &num_irqs, 0);
  918. MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
  919. " addresses separated by commas. Only use if an interface"
  920. " has an interrupt. Otherwise, set it to zero or leave"
  921. " it blank.");
  922. module_param_array(regspacings, int, &num_regspacings, 0);
  923. MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
  924. " and each successive register used by the interface. For"
  925. " instance, if the start address is 0xca2 and the spacing"
  926. " is 2, then the second address is at 0xca4. Defaults"
  927. " to 1.");
  928. module_param_array(regsizes, int, &num_regsizes, 0);
  929. MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
  930. " This should generally be 1, 2, 4, or 8 for an 8-bit,"
  931. " 16-bit, 32-bit, or 64-bit register. Use this if you"
  932. " the 8-bit IPMI register has to be read from a larger"
  933. " register.");
  934. module_param_array(regshifts, int, &num_regshifts, 0);
  935. MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
  936. " IPMI register, in bits. For instance, if the data"
  937. " is read from a 32-bit word and the IPMI data is in"
  938. " bit 8-15, then the shift would be 8");
  939. module_param_array(slave_addrs, int, &num_slave_addrs, 0);
  940. MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
  941. " the controller. Normally this is 0x20, but can be"
  942. " overridden by this parm. This is an array indexed"
  943. " by interface number.");
  944. #define IPMI_IO_ADDR_SPACE 0
  945. #define IPMI_MEM_ADDR_SPACE 1
  946. static char *addr_space_to_str[] = { "I/O", "memory" };
  947. static void std_irq_cleanup(struct smi_info *info)
  948. {
  949. if (info->si_type == SI_BT)
  950. /* Disable the interrupt in the BT interface. */
  951. info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
  952. free_irq(info->irq, info);
  953. }
  954. static int std_irq_setup(struct smi_info *info)
  955. {
  956. int rv;
  957. if (!info->irq)
  958. return 0;
  959. if (info->si_type == SI_BT) {
  960. rv = request_irq(info->irq,
  961. si_bt_irq_handler,
  962. SA_INTERRUPT,
  963. DEVICE_NAME,
  964. info);
  965. if (!rv)
  966. /* Enable the interrupt in the BT interface. */
  967. info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
  968. IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  969. } else
  970. rv = request_irq(info->irq,
  971. si_irq_handler,
  972. SA_INTERRUPT,
  973. DEVICE_NAME,
  974. info);
  975. if (rv) {
  976. printk(KERN_WARNING
  977. "ipmi_si: %s unable to claim interrupt %d,"
  978. " running polled\n",
  979. DEVICE_NAME, info->irq);
  980. info->irq = 0;
  981. } else {
  982. info->irq_cleanup = std_irq_cleanup;
  983. printk(" Using irq %d\n", info->irq);
  984. }
  985. return rv;
  986. }
  987. static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
  988. {
  989. unsigned int addr = io->addr_data;
  990. return inb(addr + (offset * io->regspacing));
  991. }
  992. static void port_outb(struct si_sm_io *io, unsigned int offset,
  993. unsigned char b)
  994. {
  995. unsigned int addr = io->addr_data;
  996. outb(b, addr + (offset * io->regspacing));
  997. }
  998. static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
  999. {
  1000. unsigned int addr = io->addr_data;
  1001. return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
  1002. }
  1003. static void port_outw(struct si_sm_io *io, unsigned int offset,
  1004. unsigned char b)
  1005. {
  1006. unsigned int addr = io->addr_data;
  1007. outw(b << io->regshift, addr + (offset * io->regspacing));
  1008. }
  1009. static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
  1010. {
  1011. unsigned int addr = io->addr_data;
  1012. return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
  1013. }
  1014. static void port_outl(struct si_sm_io *io, unsigned int offset,
  1015. unsigned char b)
  1016. {
  1017. unsigned int addr = io->addr_data;
  1018. outl(b << io->regshift, addr+(offset * io->regspacing));
  1019. }
  1020. static void port_cleanup(struct smi_info *info)
  1021. {
  1022. unsigned int addr = info->io.addr_data;
  1023. int mapsize;
  1024. if (addr) {
  1025. mapsize = ((info->io_size * info->io.regspacing)
  1026. - (info->io.regspacing - info->io.regsize));
  1027. release_region (addr, mapsize);
  1028. }
  1029. }
  1030. static int port_setup(struct smi_info *info)
  1031. {
  1032. unsigned int addr = info->io.addr_data;
  1033. int mapsize;
  1034. if (!addr)
  1035. return -ENODEV;
  1036. info->io_cleanup = port_cleanup;
  1037. /* Figure out the actual inb/inw/inl/etc routine to use based
  1038. upon the register size. */
  1039. switch (info->io.regsize) {
  1040. case 1:
  1041. info->io.inputb = port_inb;
  1042. info->io.outputb = port_outb;
  1043. break;
  1044. case 2:
  1045. info->io.inputb = port_inw;
  1046. info->io.outputb = port_outw;
  1047. break;
  1048. case 4:
  1049. info->io.inputb = port_inl;
  1050. info->io.outputb = port_outl;
  1051. break;
  1052. default:
  1053. printk("ipmi_si: Invalid register size: %d\n",
  1054. info->io.regsize);
  1055. return -EINVAL;
  1056. }
  1057. /* Calculate the total amount of memory to claim. This is an
  1058. * unusual looking calculation, but it avoids claiming any
  1059. * more memory than it has to. It will claim everything
  1060. * between the first address to the end of the last full
  1061. * register. */
  1062. mapsize = ((info->io_size * info->io.regspacing)
  1063. - (info->io.regspacing - info->io.regsize));
  1064. if (request_region(addr, mapsize, DEVICE_NAME) == NULL)
  1065. return -EIO;
  1066. return 0;
  1067. }
  1068. static unsigned char intf_mem_inb(struct si_sm_io *io, unsigned int offset)
  1069. {
  1070. return readb((io->addr)+(offset * io->regspacing));
  1071. }
  1072. static void intf_mem_outb(struct si_sm_io *io, unsigned int offset,
  1073. unsigned char b)
  1074. {
  1075. writeb(b, (io->addr)+(offset * io->regspacing));
  1076. }
  1077. static unsigned char intf_mem_inw(struct si_sm_io *io, unsigned int offset)
  1078. {
  1079. return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
  1080. && 0xff;
  1081. }
  1082. static void intf_mem_outw(struct si_sm_io *io, unsigned int offset,
  1083. unsigned char b)
  1084. {
  1085. writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
  1086. }
  1087. static unsigned char intf_mem_inl(struct si_sm_io *io, unsigned int offset)
  1088. {
  1089. return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
  1090. && 0xff;
  1091. }
  1092. static void intf_mem_outl(struct si_sm_io *io, unsigned int offset,
  1093. unsigned char b)
  1094. {
  1095. writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
  1096. }
  1097. #ifdef readq
  1098. static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
  1099. {
  1100. return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
  1101. && 0xff;
  1102. }
  1103. static void mem_outq(struct si_sm_io *io, unsigned int offset,
  1104. unsigned char b)
  1105. {
  1106. writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
  1107. }
  1108. #endif
  1109. static void mem_cleanup(struct smi_info *info)
  1110. {
  1111. unsigned long addr = info->io.addr_data;
  1112. int mapsize;
  1113. if (info->io.addr) {
  1114. iounmap(info->io.addr);
  1115. mapsize = ((info->io_size * info->io.regspacing)
  1116. - (info->io.regspacing - info->io.regsize));
  1117. release_mem_region(addr, mapsize);
  1118. }
  1119. }
  1120. static int mem_setup(struct smi_info *info)
  1121. {
  1122. unsigned long addr = info->io.addr_data;
  1123. int mapsize;
  1124. if (!addr)
  1125. return -ENODEV;
  1126. info->io_cleanup = mem_cleanup;
  1127. /* Figure out the actual readb/readw/readl/etc routine to use based
  1128. upon the register size. */
  1129. switch (info->io.regsize) {
  1130. case 1:
  1131. info->io.inputb = intf_mem_inb;
  1132. info->io.outputb = intf_mem_outb;
  1133. break;
  1134. case 2:
  1135. info->io.inputb = intf_mem_inw;
  1136. info->io.outputb = intf_mem_outw;
  1137. break;
  1138. case 4:
  1139. info->io.inputb = intf_mem_inl;
  1140. info->io.outputb = intf_mem_outl;
  1141. break;
  1142. #ifdef readq
  1143. case 8:
  1144. info->io.inputb = mem_inq;
  1145. info->io.outputb = mem_outq;
  1146. break;
  1147. #endif
  1148. default:
  1149. printk("ipmi_si: Invalid register size: %d\n",
  1150. info->io.regsize);
  1151. return -EINVAL;
  1152. }
  1153. /* Calculate the total amount of memory to claim. This is an
  1154. * unusual looking calculation, but it avoids claiming any
  1155. * more memory than it has to. It will claim everything
  1156. * between the first address to the end of the last full
  1157. * register. */
  1158. mapsize = ((info->io_size * info->io.regspacing)
  1159. - (info->io.regspacing - info->io.regsize));
  1160. if (request_mem_region(addr, mapsize, DEVICE_NAME) == NULL)
  1161. return -EIO;
  1162. info->io.addr = ioremap(addr, mapsize);
  1163. if (info->io.addr == NULL) {
  1164. release_mem_region(addr, mapsize);
  1165. return -EIO;
  1166. }
  1167. return 0;
  1168. }
  1169. static __devinit void hardcode_find_bmc(void)
  1170. {
  1171. int i;
  1172. struct smi_info *info;
  1173. for (i = 0; i < SI_MAX_PARMS; i++) {
  1174. if (!ports[i] && !addrs[i])
  1175. continue;
  1176. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1177. if (!info)
  1178. return;
  1179. info->addr_source = "hardcoded";
  1180. if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
  1181. info->si_type = SI_KCS;
  1182. } else if (strcmp(si_type[i], "smic") == 0) {
  1183. info->si_type = SI_SMIC;
  1184. } else if (strcmp(si_type[i], "bt") == 0) {
  1185. info->si_type = SI_BT;
  1186. } else {
  1187. printk(KERN_WARNING
  1188. "ipmi_si: Interface type specified "
  1189. "for interface %d, was invalid: %s\n",
  1190. i, si_type[i]);
  1191. kfree(info);
  1192. continue;
  1193. }
  1194. if (ports[i]) {
  1195. /* An I/O port */
  1196. info->io_setup = port_setup;
  1197. info->io.addr_data = ports[i];
  1198. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1199. } else if (addrs[i]) {
  1200. /* A memory port */
  1201. info->io_setup = mem_setup;
  1202. info->io.addr_data = addrs[i];
  1203. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1204. } else {
  1205. printk(KERN_WARNING
  1206. "ipmi_si: Interface type specified "
  1207. "for interface %d, "
  1208. "but port and address were not set or "
  1209. "set to zero.\n", i);
  1210. kfree(info);
  1211. continue;
  1212. }
  1213. info->io.addr = NULL;
  1214. info->io.regspacing = regspacings[i];
  1215. if (!info->io.regspacing)
  1216. info->io.regspacing = DEFAULT_REGSPACING;
  1217. info->io.regsize = regsizes[i];
  1218. if (!info->io.regsize)
  1219. info->io.regsize = DEFAULT_REGSPACING;
  1220. info->io.regshift = regshifts[i];
  1221. info->irq = irqs[i];
  1222. if (info->irq)
  1223. info->irq_setup = std_irq_setup;
  1224. try_smi_init(info);
  1225. }
  1226. }
  1227. #ifdef CONFIG_ACPI
  1228. #include <linux/acpi.h>
  1229. /* Once we get an ACPI failure, we don't try any more, because we go
  1230. through the tables sequentially. Once we don't find a table, there
  1231. are no more. */
  1232. static int acpi_failure = 0;
  1233. /* For GPE-type interrupts. */
  1234. static u32 ipmi_acpi_gpe(void *context)
  1235. {
  1236. struct smi_info *smi_info = context;
  1237. unsigned long flags;
  1238. #ifdef DEBUG_TIMING
  1239. struct timeval t;
  1240. #endif
  1241. spin_lock_irqsave(&(smi_info->si_lock), flags);
  1242. spin_lock(&smi_info->count_lock);
  1243. smi_info->interrupts++;
  1244. spin_unlock(&smi_info->count_lock);
  1245. if (atomic_read(&smi_info->stop_operation))
  1246. goto out;
  1247. #ifdef DEBUG_TIMING
  1248. do_gettimeofday(&t);
  1249. printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  1250. #endif
  1251. smi_event_handler(smi_info, 0);
  1252. out:
  1253. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  1254. return ACPI_INTERRUPT_HANDLED;
  1255. }
  1256. static void acpi_gpe_irq_cleanup(struct smi_info *info)
  1257. {
  1258. if (!info->irq)
  1259. return;
  1260. acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
  1261. }
  1262. static int acpi_gpe_irq_setup(struct smi_info *info)
  1263. {
  1264. acpi_status status;
  1265. if (!info->irq)
  1266. return 0;
  1267. /* FIXME - is level triggered right? */
  1268. status = acpi_install_gpe_handler(NULL,
  1269. info->irq,
  1270. ACPI_GPE_LEVEL_TRIGGERED,
  1271. &ipmi_acpi_gpe,
  1272. info);
  1273. if (status != AE_OK) {
  1274. printk(KERN_WARNING
  1275. "ipmi_si: %s unable to claim ACPI GPE %d,"
  1276. " running polled\n",
  1277. DEVICE_NAME, info->irq);
  1278. info->irq = 0;
  1279. return -EINVAL;
  1280. } else {
  1281. info->irq_cleanup = acpi_gpe_irq_cleanup;
  1282. printk(" Using ACPI GPE %d\n", info->irq);
  1283. return 0;
  1284. }
  1285. }
  1286. /*
  1287. * Defined at
  1288. * http://h21007.www2.hp.com/dspp/files/unprotected/devresource/Docs/TechPapers/IA64/hpspmi.pdf
  1289. */
  1290. struct SPMITable {
  1291. s8 Signature[4];
  1292. u32 Length;
  1293. u8 Revision;
  1294. u8 Checksum;
  1295. s8 OEMID[6];
  1296. s8 OEMTableID[8];
  1297. s8 OEMRevision[4];
  1298. s8 CreatorID[4];
  1299. s8 CreatorRevision[4];
  1300. u8 InterfaceType;
  1301. u8 IPMIlegacy;
  1302. s16 SpecificationRevision;
  1303. /*
  1304. * Bit 0 - SCI interrupt supported
  1305. * Bit 1 - I/O APIC/SAPIC
  1306. */
  1307. u8 InterruptType;
  1308. /* If bit 0 of InterruptType is set, then this is the SCI
  1309. interrupt in the GPEx_STS register. */
  1310. u8 GPE;
  1311. s16 Reserved;
  1312. /* If bit 1 of InterruptType is set, then this is the I/O
  1313. APIC/SAPIC interrupt. */
  1314. u32 GlobalSystemInterrupt;
  1315. /* The actual register address. */
  1316. struct acpi_generic_address addr;
  1317. u8 UID[4];
  1318. s8 spmi_id[1]; /* A '\0' terminated array starts here. */
  1319. };
  1320. static __devinit int try_init_acpi(struct SPMITable *spmi)
  1321. {
  1322. struct smi_info *info;
  1323. char *io_type;
  1324. u8 addr_space;
  1325. if (spmi->IPMIlegacy != 1) {
  1326. printk(KERN_INFO "IPMI: Bad SPMI legacy %d\n", spmi->IPMIlegacy);
  1327. return -ENODEV;
  1328. }
  1329. if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  1330. addr_space = IPMI_MEM_ADDR_SPACE;
  1331. else
  1332. addr_space = IPMI_IO_ADDR_SPACE;
  1333. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1334. if (!info) {
  1335. printk(KERN_ERR "ipmi_si: Could not allocate SI data (3)\n");
  1336. return -ENOMEM;
  1337. }
  1338. info->addr_source = "ACPI";
  1339. /* Figure out the interface type. */
  1340. switch (spmi->InterfaceType)
  1341. {
  1342. case 1: /* KCS */
  1343. info->si_type = SI_KCS;
  1344. break;
  1345. case 2: /* SMIC */
  1346. info->si_type = SI_SMIC;
  1347. break;
  1348. case 3: /* BT */
  1349. info->si_type = SI_BT;
  1350. break;
  1351. default:
  1352. printk(KERN_INFO "ipmi_si: Unknown ACPI/SPMI SI type %d\n",
  1353. spmi->InterfaceType);
  1354. kfree(info);
  1355. return -EIO;
  1356. }
  1357. if (spmi->InterruptType & 1) {
  1358. /* We've got a GPE interrupt. */
  1359. info->irq = spmi->GPE;
  1360. info->irq_setup = acpi_gpe_irq_setup;
  1361. } else if (spmi->InterruptType & 2) {
  1362. /* We've got an APIC/SAPIC interrupt. */
  1363. info->irq = spmi->GlobalSystemInterrupt;
  1364. info->irq_setup = std_irq_setup;
  1365. } else {
  1366. /* Use the default interrupt setting. */
  1367. info->irq = 0;
  1368. info->irq_setup = NULL;
  1369. }
  1370. if (spmi->addr.register_bit_width) {
  1371. /* A (hopefully) properly formed register bit width. */
  1372. info->io.regspacing = spmi->addr.register_bit_width / 8;
  1373. } else {
  1374. info->io.regspacing = DEFAULT_REGSPACING;
  1375. }
  1376. info->io.regsize = info->io.regspacing;
  1377. info->io.regshift = spmi->addr.register_bit_offset;
  1378. if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  1379. io_type = "memory";
  1380. info->io_setup = mem_setup;
  1381. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1382. } else if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  1383. io_type = "I/O";
  1384. info->io_setup = port_setup;
  1385. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1386. } else {
  1387. kfree(info);
  1388. printk("ipmi_si: Unknown ACPI I/O Address type\n");
  1389. return -EIO;
  1390. }
  1391. info->io.addr_data = spmi->addr.address;
  1392. try_smi_init(info);
  1393. return 0;
  1394. }
  1395. static __devinit void acpi_find_bmc(void)
  1396. {
  1397. acpi_status status;
  1398. struct SPMITable *spmi;
  1399. int i;
  1400. if (acpi_disabled)
  1401. return;
  1402. if (acpi_failure)
  1403. return;
  1404. for (i = 0; ; i++) {
  1405. status = acpi_get_firmware_table("SPMI", i+1,
  1406. ACPI_LOGICAL_ADDRESSING,
  1407. (struct acpi_table_header **)
  1408. &spmi);
  1409. if (status != AE_OK)
  1410. return;
  1411. try_init_acpi(spmi);
  1412. }
  1413. }
  1414. #endif
  1415. #ifdef CONFIG_DMI
  1416. struct dmi_ipmi_data
  1417. {
  1418. u8 type;
  1419. u8 addr_space;
  1420. unsigned long base_addr;
  1421. u8 irq;
  1422. u8 offset;
  1423. u8 slave_addr;
  1424. };
  1425. static int __devinit decode_dmi(struct dmi_header *dm,
  1426. struct dmi_ipmi_data *dmi)
  1427. {
  1428. u8 *data = (u8 *)dm;
  1429. unsigned long base_addr;
  1430. u8 reg_spacing;
  1431. u8 len = dm->length;
  1432. dmi->type = data[4];
  1433. memcpy(&base_addr, data+8, sizeof(unsigned long));
  1434. if (len >= 0x11) {
  1435. if (base_addr & 1) {
  1436. /* I/O */
  1437. base_addr &= 0xFFFE;
  1438. dmi->addr_space = IPMI_IO_ADDR_SPACE;
  1439. }
  1440. else {
  1441. /* Memory */
  1442. dmi->addr_space = IPMI_MEM_ADDR_SPACE;
  1443. }
  1444. /* If bit 4 of byte 0x10 is set, then the lsb for the address
  1445. is odd. */
  1446. dmi->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
  1447. dmi->irq = data[0x11];
  1448. /* The top two bits of byte 0x10 hold the register spacing. */
  1449. reg_spacing = (data[0x10] & 0xC0) >> 6;
  1450. switch(reg_spacing){
  1451. case 0x00: /* Byte boundaries */
  1452. dmi->offset = 1;
  1453. break;
  1454. case 0x01: /* 32-bit boundaries */
  1455. dmi->offset = 4;
  1456. break;
  1457. case 0x02: /* 16-byte boundaries */
  1458. dmi->offset = 16;
  1459. break;
  1460. default:
  1461. /* Some other interface, just ignore it. */
  1462. return -EIO;
  1463. }
  1464. } else {
  1465. /* Old DMI spec. */
  1466. /* Note that technically, the lower bit of the base
  1467. * address should be 1 if the address is I/O and 0 if
  1468. * the address is in memory. So many systems get that
  1469. * wrong (and all that I have seen are I/O) so we just
  1470. * ignore that bit and assume I/O. Systems that use
  1471. * memory should use the newer spec, anyway. */
  1472. dmi->base_addr = base_addr & 0xfffe;
  1473. dmi->addr_space = IPMI_IO_ADDR_SPACE;
  1474. dmi->offset = 1;
  1475. }
  1476. dmi->slave_addr = data[6];
  1477. return 0;
  1478. }
  1479. static __devinit void try_init_dmi(struct dmi_ipmi_data *ipmi_data)
  1480. {
  1481. struct smi_info *info;
  1482. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1483. if (!info) {
  1484. printk(KERN_ERR
  1485. "ipmi_si: Could not allocate SI data\n");
  1486. return;
  1487. }
  1488. info->addr_source = "SMBIOS";
  1489. switch (ipmi_data->type) {
  1490. case 0x01: /* KCS */
  1491. info->si_type = SI_KCS;
  1492. break;
  1493. case 0x02: /* SMIC */
  1494. info->si_type = SI_SMIC;
  1495. break;
  1496. case 0x03: /* BT */
  1497. info->si_type = SI_BT;
  1498. break;
  1499. default:
  1500. return;
  1501. }
  1502. switch (ipmi_data->addr_space) {
  1503. case IPMI_MEM_ADDR_SPACE:
  1504. info->io_setup = mem_setup;
  1505. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1506. break;
  1507. case IPMI_IO_ADDR_SPACE:
  1508. info->io_setup = port_setup;
  1509. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1510. break;
  1511. default:
  1512. kfree(info);
  1513. printk(KERN_WARNING
  1514. "ipmi_si: Unknown SMBIOS I/O Address type: %d.\n",
  1515. ipmi_data->addr_space);
  1516. return;
  1517. }
  1518. info->io.addr_data = ipmi_data->base_addr;
  1519. info->io.regspacing = ipmi_data->offset;
  1520. if (!info->io.regspacing)
  1521. info->io.regspacing = DEFAULT_REGSPACING;
  1522. info->io.regsize = DEFAULT_REGSPACING;
  1523. info->io.regshift = 0;
  1524. info->slave_addr = ipmi_data->slave_addr;
  1525. info->irq = ipmi_data->irq;
  1526. if (info->irq)
  1527. info->irq_setup = std_irq_setup;
  1528. try_smi_init(info);
  1529. }
  1530. static void __devinit dmi_find_bmc(void)
  1531. {
  1532. struct dmi_device *dev = NULL;
  1533. struct dmi_ipmi_data data;
  1534. int rv;
  1535. while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
  1536. rv = decode_dmi((struct dmi_header *) dev->device_data, &data);
  1537. if (!rv)
  1538. try_init_dmi(&data);
  1539. }
  1540. }
  1541. #endif /* CONFIG_DMI */
  1542. #ifdef CONFIG_PCI
  1543. #define PCI_ERMC_CLASSCODE 0x0C0700
  1544. #define PCI_ERMC_CLASSCODE_MASK 0xffffff00
  1545. #define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff
  1546. #define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x00
  1547. #define PCI_ERMC_CLASSCODE_TYPE_KCS 0x01
  1548. #define PCI_ERMC_CLASSCODE_TYPE_BT 0x02
  1549. #define PCI_HP_VENDOR_ID 0x103C
  1550. #define PCI_MMC_DEVICE_ID 0x121A
  1551. #define PCI_MMC_ADDR_CW 0x10
  1552. static void ipmi_pci_cleanup(struct smi_info *info)
  1553. {
  1554. struct pci_dev *pdev = info->addr_source_data;
  1555. pci_disable_device(pdev);
  1556. }
  1557. static int __devinit ipmi_pci_probe(struct pci_dev *pdev,
  1558. const struct pci_device_id *ent)
  1559. {
  1560. int rv;
  1561. int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
  1562. struct smi_info *info;
  1563. int first_reg_offset = 0;
  1564. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1565. if (!info)
  1566. return ENOMEM;
  1567. info->addr_source = "PCI";
  1568. switch (class_type) {
  1569. case PCI_ERMC_CLASSCODE_TYPE_SMIC:
  1570. info->si_type = SI_SMIC;
  1571. break;
  1572. case PCI_ERMC_CLASSCODE_TYPE_KCS:
  1573. info->si_type = SI_KCS;
  1574. break;
  1575. case PCI_ERMC_CLASSCODE_TYPE_BT:
  1576. info->si_type = SI_BT;
  1577. break;
  1578. default:
  1579. kfree(info);
  1580. printk(KERN_INFO "ipmi_si: %s: Unknown IPMI type: %d\n",
  1581. pci_name(pdev), class_type);
  1582. return ENOMEM;
  1583. }
  1584. rv = pci_enable_device(pdev);
  1585. if (rv) {
  1586. printk(KERN_ERR "ipmi_si: %s: couldn't enable PCI device\n",
  1587. pci_name(pdev));
  1588. kfree(info);
  1589. return rv;
  1590. }
  1591. info->addr_source_cleanup = ipmi_pci_cleanup;
  1592. info->addr_source_data = pdev;
  1593. if (pdev->subsystem_vendor == PCI_HP_VENDOR_ID)
  1594. first_reg_offset = 1;
  1595. if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
  1596. info->io_setup = port_setup;
  1597. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1598. } else {
  1599. info->io_setup = mem_setup;
  1600. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1601. }
  1602. info->io.addr_data = pci_resource_start(pdev, 0);
  1603. info->io.regspacing = DEFAULT_REGSPACING;
  1604. info->io.regsize = DEFAULT_REGSPACING;
  1605. info->io.regshift = 0;
  1606. info->irq = pdev->irq;
  1607. if (info->irq)
  1608. info->irq_setup = std_irq_setup;
  1609. info->dev = &pdev->dev;
  1610. return try_smi_init(info);
  1611. }
  1612. static void __devexit ipmi_pci_remove(struct pci_dev *pdev)
  1613. {
  1614. }
  1615. #ifdef CONFIG_PM
  1616. static int ipmi_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1617. {
  1618. return 0;
  1619. }
  1620. static int ipmi_pci_resume(struct pci_dev *pdev)
  1621. {
  1622. return 0;
  1623. }
  1624. #endif
  1625. static struct pci_device_id ipmi_pci_devices[] = {
  1626. { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
  1627. { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE) }
  1628. };
  1629. MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
  1630. static struct pci_driver ipmi_pci_driver = {
  1631. .name = DEVICE_NAME,
  1632. .id_table = ipmi_pci_devices,
  1633. .probe = ipmi_pci_probe,
  1634. .remove = __devexit_p(ipmi_pci_remove),
  1635. #ifdef CONFIG_PM
  1636. .suspend = ipmi_pci_suspend,
  1637. .resume = ipmi_pci_resume,
  1638. #endif
  1639. };
  1640. #endif /* CONFIG_PCI */
  1641. static int try_get_dev_id(struct smi_info *smi_info)
  1642. {
  1643. unsigned char msg[2];
  1644. unsigned char *resp;
  1645. unsigned long resp_len;
  1646. enum si_sm_result smi_result;
  1647. int rv = 0;
  1648. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  1649. if (!resp)
  1650. return -ENOMEM;
  1651. /* Do a Get Device ID command, since it comes back with some
  1652. useful info. */
  1653. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1654. msg[1] = IPMI_GET_DEVICE_ID_CMD;
  1655. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  1656. smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
  1657. for (;;)
  1658. {
  1659. if (smi_result == SI_SM_CALL_WITH_DELAY ||
  1660. smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
  1661. schedule_timeout_uninterruptible(1);
  1662. smi_result = smi_info->handlers->event(
  1663. smi_info->si_sm, 100);
  1664. }
  1665. else if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
  1666. {
  1667. smi_result = smi_info->handlers->event(
  1668. smi_info->si_sm, 0);
  1669. }
  1670. else
  1671. break;
  1672. }
  1673. if (smi_result == SI_SM_HOSED) {
  1674. /* We couldn't get the state machine to run, so whatever's at
  1675. the port is probably not an IPMI SMI interface. */
  1676. rv = -ENODEV;
  1677. goto out;
  1678. }
  1679. /* Otherwise, we got some data. */
  1680. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  1681. resp, IPMI_MAX_MSG_LENGTH);
  1682. if (resp_len < 14) {
  1683. /* That's odd, it should be longer. */
  1684. rv = -EINVAL;
  1685. goto out;
  1686. }
  1687. if ((resp[1] != IPMI_GET_DEVICE_ID_CMD) || (resp[2] != 0)) {
  1688. /* That's odd, it shouldn't be able to fail. */
  1689. rv = -EINVAL;
  1690. goto out;
  1691. }
  1692. /* Record info from the get device id, in case we need it. */
  1693. ipmi_demangle_device_id(resp+3, resp_len-3, &smi_info->device_id);
  1694. out:
  1695. kfree(resp);
  1696. return rv;
  1697. }
  1698. static int type_file_read_proc(char *page, char **start, off_t off,
  1699. int count, int *eof, void *data)
  1700. {
  1701. char *out = (char *) page;
  1702. struct smi_info *smi = data;
  1703. switch (smi->si_type) {
  1704. case SI_KCS:
  1705. return sprintf(out, "kcs\n");
  1706. case SI_SMIC:
  1707. return sprintf(out, "smic\n");
  1708. case SI_BT:
  1709. return sprintf(out, "bt\n");
  1710. default:
  1711. return 0;
  1712. }
  1713. }
  1714. static int stat_file_read_proc(char *page, char **start, off_t off,
  1715. int count, int *eof, void *data)
  1716. {
  1717. char *out = (char *) page;
  1718. struct smi_info *smi = data;
  1719. out += sprintf(out, "interrupts_enabled: %d\n",
  1720. smi->irq && !smi->interrupt_disabled);
  1721. out += sprintf(out, "short_timeouts: %ld\n",
  1722. smi->short_timeouts);
  1723. out += sprintf(out, "long_timeouts: %ld\n",
  1724. smi->long_timeouts);
  1725. out += sprintf(out, "timeout_restarts: %ld\n",
  1726. smi->timeout_restarts);
  1727. out += sprintf(out, "idles: %ld\n",
  1728. smi->idles);
  1729. out += sprintf(out, "interrupts: %ld\n",
  1730. smi->interrupts);
  1731. out += sprintf(out, "attentions: %ld\n",
  1732. smi->attentions);
  1733. out += sprintf(out, "flag_fetches: %ld\n",
  1734. smi->flag_fetches);
  1735. out += sprintf(out, "hosed_count: %ld\n",
  1736. smi->hosed_count);
  1737. out += sprintf(out, "complete_transactions: %ld\n",
  1738. smi->complete_transactions);
  1739. out += sprintf(out, "events: %ld\n",
  1740. smi->events);
  1741. out += sprintf(out, "watchdog_pretimeouts: %ld\n",
  1742. smi->watchdog_pretimeouts);
  1743. out += sprintf(out, "incoming_messages: %ld\n",
  1744. smi->incoming_messages);
  1745. return (out - ((char *) page));
  1746. }
  1747. /*
  1748. * oem_data_avail_to_receive_msg_avail
  1749. * @info - smi_info structure with msg_flags set
  1750. *
  1751. * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
  1752. * Returns 1 indicating need to re-run handle_flags().
  1753. */
  1754. static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
  1755. {
  1756. smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
  1757. RECEIVE_MSG_AVAIL);
  1758. return 1;
  1759. }
  1760. /*
  1761. * setup_dell_poweredge_oem_data_handler
  1762. * @info - smi_info.device_id must be populated
  1763. *
  1764. * Systems that match, but have firmware version < 1.40 may assert
  1765. * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
  1766. * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
  1767. * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
  1768. * as RECEIVE_MSG_AVAIL instead.
  1769. *
  1770. * As Dell has no plans to release IPMI 1.5 firmware that *ever*
  1771. * assert the OEM[012] bits, and if it did, the driver would have to
  1772. * change to handle that properly, we don't actually check for the
  1773. * firmware version.
  1774. * Device ID = 0x20 BMC on PowerEdge 8G servers
  1775. * Device Revision = 0x80
  1776. * Firmware Revision1 = 0x01 BMC version 1.40
  1777. * Firmware Revision2 = 0x40 BCD encoded
  1778. * IPMI Version = 0x51 IPMI 1.5
  1779. * Manufacturer ID = A2 02 00 Dell IANA
  1780. *
  1781. * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
  1782. * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
  1783. *
  1784. */
  1785. #define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
  1786. #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
  1787. #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
  1788. #define DELL_IANA_MFR_ID 0x0002a2
  1789. static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
  1790. {
  1791. struct ipmi_device_id *id = &smi_info->device_id;
  1792. if (id->manufacturer_id == DELL_IANA_MFR_ID) {
  1793. if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
  1794. id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
  1795. id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
  1796. smi_info->oem_data_avail_handler =
  1797. oem_data_avail_to_receive_msg_avail;
  1798. }
  1799. else if (ipmi_version_major(id) < 1 ||
  1800. (ipmi_version_major(id) == 1 &&
  1801. ipmi_version_minor(id) < 5)) {
  1802. smi_info->oem_data_avail_handler =
  1803. oem_data_avail_to_receive_msg_avail;
  1804. }
  1805. }
  1806. }
  1807. #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
  1808. static void return_hosed_msg_badsize(struct smi_info *smi_info)
  1809. {
  1810. struct ipmi_smi_msg *msg = smi_info->curr_msg;
  1811. /* Make it a reponse */
  1812. msg->rsp[0] = msg->data[0] | 4;
  1813. msg->rsp[1] = msg->data[1];
  1814. msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
  1815. msg->rsp_size = 3;
  1816. smi_info->curr_msg = NULL;
  1817. deliver_recv_msg(smi_info, msg);
  1818. }
  1819. /*
  1820. * dell_poweredge_bt_xaction_handler
  1821. * @info - smi_info.device_id must be populated
  1822. *
  1823. * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
  1824. * not respond to a Get SDR command if the length of the data
  1825. * requested is exactly 0x3A, which leads to command timeouts and no
  1826. * data returned. This intercepts such commands, and causes userspace
  1827. * callers to try again with a different-sized buffer, which succeeds.
  1828. */
  1829. #define STORAGE_NETFN 0x0A
  1830. #define STORAGE_CMD_GET_SDR 0x23
  1831. static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
  1832. unsigned long unused,
  1833. void *in)
  1834. {
  1835. struct smi_info *smi_info = in;
  1836. unsigned char *data = smi_info->curr_msg->data;
  1837. unsigned int size = smi_info->curr_msg->data_size;
  1838. if (size >= 8 &&
  1839. (data[0]>>2) == STORAGE_NETFN &&
  1840. data[1] == STORAGE_CMD_GET_SDR &&
  1841. data[7] == 0x3A) {
  1842. return_hosed_msg_badsize(smi_info);
  1843. return NOTIFY_STOP;
  1844. }
  1845. return NOTIFY_DONE;
  1846. }
  1847. static struct notifier_block dell_poweredge_bt_xaction_notifier = {
  1848. .notifier_call = dell_poweredge_bt_xaction_handler,
  1849. };
  1850. /*
  1851. * setup_dell_poweredge_bt_xaction_handler
  1852. * @info - smi_info.device_id must be filled in already
  1853. *
  1854. * Fills in smi_info.device_id.start_transaction_pre_hook
  1855. * when we know what function to use there.
  1856. */
  1857. static void
  1858. setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
  1859. {
  1860. struct ipmi_device_id *id = &smi_info->device_id;
  1861. if (id->manufacturer_id == DELL_IANA_MFR_ID &&
  1862. smi_info->si_type == SI_BT)
  1863. register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
  1864. }
  1865. /*
  1866. * setup_oem_data_handler
  1867. * @info - smi_info.device_id must be filled in already
  1868. *
  1869. * Fills in smi_info.device_id.oem_data_available_handler
  1870. * when we know what function to use there.
  1871. */
  1872. static void setup_oem_data_handler(struct smi_info *smi_info)
  1873. {
  1874. setup_dell_poweredge_oem_data_handler(smi_info);
  1875. }
  1876. static void setup_xaction_handlers(struct smi_info *smi_info)
  1877. {
  1878. setup_dell_poweredge_bt_xaction_handler(smi_info);
  1879. }
  1880. static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
  1881. {
  1882. if (smi_info->intf) {
  1883. /* The timer and thread are only running if the
  1884. interface has been started up and registered. */
  1885. if (smi_info->thread != NULL)
  1886. kthread_stop(smi_info->thread);
  1887. del_timer_sync(&smi_info->si_timer);
  1888. }
  1889. }
  1890. static __devinitdata struct ipmi_default_vals
  1891. {
  1892. int type;
  1893. int port;
  1894. } ipmi_defaults[] =
  1895. {
  1896. { .type = SI_KCS, .port = 0xca2 },
  1897. { .type = SI_SMIC, .port = 0xca9 },
  1898. { .type = SI_BT, .port = 0xe4 },
  1899. { .port = 0 }
  1900. };
  1901. static __devinit void default_find_bmc(void)
  1902. {
  1903. struct smi_info *info;
  1904. int i;
  1905. for (i = 0; ; i++) {
  1906. if (!ipmi_defaults[i].port)
  1907. break;
  1908. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1909. if (!info)
  1910. return;
  1911. info->addr_source = NULL;
  1912. info->si_type = ipmi_defaults[i].type;
  1913. info->io_setup = port_setup;
  1914. info->io.addr_data = ipmi_defaults[i].port;
  1915. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1916. info->io.addr = NULL;
  1917. info->io.regspacing = DEFAULT_REGSPACING;
  1918. info->io.regsize = DEFAULT_REGSPACING;
  1919. info->io.regshift = 0;
  1920. if (try_smi_init(info) == 0) {
  1921. /* Found one... */
  1922. printk(KERN_INFO "ipmi_si: Found default %s state"
  1923. " machine at %s address 0x%lx\n",
  1924. si_to_str[info->si_type],
  1925. addr_space_to_str[info->io.addr_type],
  1926. info->io.addr_data);
  1927. return;
  1928. }
  1929. }
  1930. }
  1931. static int is_new_interface(struct smi_info *info)
  1932. {
  1933. struct smi_info *e;
  1934. list_for_each_entry(e, &smi_infos, link) {
  1935. if (e->io.addr_type != info->io.addr_type)
  1936. continue;
  1937. if (e->io.addr_data == info->io.addr_data)
  1938. return 0;
  1939. }
  1940. return 1;
  1941. }
  1942. static int try_smi_init(struct smi_info *new_smi)
  1943. {
  1944. int rv;
  1945. if (new_smi->addr_source) {
  1946. printk(KERN_INFO "ipmi_si: Trying %s-specified %s state"
  1947. " machine at %s address 0x%lx, slave address 0x%x,"
  1948. " irq %d\n",
  1949. new_smi->addr_source,
  1950. si_to_str[new_smi->si_type],
  1951. addr_space_to_str[new_smi->io.addr_type],
  1952. new_smi->io.addr_data,
  1953. new_smi->slave_addr, new_smi->irq);
  1954. }
  1955. mutex_lock(&smi_infos_lock);
  1956. if (!is_new_interface(new_smi)) {
  1957. printk(KERN_WARNING "ipmi_si: duplicate interface\n");
  1958. rv = -EBUSY;
  1959. goto out_err;
  1960. }
  1961. /* So we know not to free it unless we have allocated one. */
  1962. new_smi->intf = NULL;
  1963. new_smi->si_sm = NULL;
  1964. new_smi->handlers = NULL;
  1965. switch (new_smi->si_type) {
  1966. case SI_KCS:
  1967. new_smi->handlers = &kcs_smi_handlers;
  1968. break;
  1969. case SI_SMIC:
  1970. new_smi->handlers = &smic_smi_handlers;
  1971. break;
  1972. case SI_BT:
  1973. new_smi->handlers = &bt_smi_handlers;
  1974. break;
  1975. default:
  1976. /* No support for anything else yet. */
  1977. rv = -EIO;
  1978. goto out_err;
  1979. }
  1980. /* Allocate the state machine's data and initialize it. */
  1981. new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
  1982. if (!new_smi->si_sm) {
  1983. printk(" Could not allocate state machine memory\n");
  1984. rv = -ENOMEM;
  1985. goto out_err;
  1986. }
  1987. new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
  1988. &new_smi->io);
  1989. /* Now that we know the I/O size, we can set up the I/O. */
  1990. rv = new_smi->io_setup(new_smi);
  1991. if (rv) {
  1992. printk(" Could not set up I/O space\n");
  1993. goto out_err;
  1994. }
  1995. spin_lock_init(&(new_smi->si_lock));
  1996. spin_lock_init(&(new_smi->msg_lock));
  1997. spin_lock_init(&(new_smi->count_lock));
  1998. /* Do low-level detection first. */
  1999. if (new_smi->handlers->detect(new_smi->si_sm)) {
  2000. if (new_smi->addr_source)
  2001. printk(KERN_INFO "ipmi_si: Interface detection"
  2002. " failed\n");
  2003. rv = -ENODEV;
  2004. goto out_err;
  2005. }
  2006. /* Attempt a get device id command. If it fails, we probably
  2007. don't have a BMC here. */
  2008. rv = try_get_dev_id(new_smi);
  2009. if (rv) {
  2010. if (new_smi->addr_source)
  2011. printk(KERN_INFO "ipmi_si: There appears to be no BMC"
  2012. " at this location\n");
  2013. goto out_err;
  2014. }
  2015. setup_oem_data_handler(new_smi);
  2016. setup_xaction_handlers(new_smi);
  2017. /* Try to claim any interrupts. */
  2018. if (new_smi->irq_setup)
  2019. new_smi->irq_setup(new_smi);
  2020. INIT_LIST_HEAD(&(new_smi->xmit_msgs));
  2021. INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
  2022. new_smi->curr_msg = NULL;
  2023. atomic_set(&new_smi->req_events, 0);
  2024. new_smi->run_to_completion = 0;
  2025. new_smi->interrupt_disabled = 0;
  2026. atomic_set(&new_smi->stop_operation, 0);
  2027. new_smi->intf_num = smi_num;
  2028. smi_num++;
  2029. /* Start clearing the flags before we enable interrupts or the
  2030. timer to avoid racing with the timer. */
  2031. start_clear_flags(new_smi);
  2032. /* IRQ is defined to be set when non-zero. */
  2033. if (new_smi->irq)
  2034. new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
  2035. if (!new_smi->dev) {
  2036. /* If we don't already have a device from something
  2037. * else (like PCI), then register a new one. */
  2038. new_smi->pdev = platform_device_alloc("ipmi_si",
  2039. new_smi->intf_num);
  2040. if (rv) {
  2041. printk(KERN_ERR
  2042. "ipmi_si_intf:"
  2043. " Unable to allocate platform device\n");
  2044. goto out_err;
  2045. }
  2046. new_smi->dev = &new_smi->pdev->dev;
  2047. new_smi->dev->driver = &ipmi_driver;
  2048. rv = platform_device_register(new_smi->pdev);
  2049. if (rv) {
  2050. printk(KERN_ERR
  2051. "ipmi_si_intf:"
  2052. " Unable to register system interface device:"
  2053. " %d\n",
  2054. rv);
  2055. goto out_err;
  2056. }
  2057. new_smi->dev_registered = 1;
  2058. }
  2059. rv = ipmi_register_smi(&handlers,
  2060. new_smi,
  2061. &new_smi->device_id,
  2062. new_smi->dev,
  2063. new_smi->slave_addr);
  2064. if (rv) {
  2065. printk(KERN_ERR
  2066. "ipmi_si: Unable to register device: error %d\n",
  2067. rv);
  2068. goto out_err_stop_timer;
  2069. }
  2070. rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
  2071. type_file_read_proc, NULL,
  2072. new_smi, THIS_MODULE);
  2073. if (rv) {
  2074. printk(KERN_ERR
  2075. "ipmi_si: Unable to create proc entry: %d\n",
  2076. rv);
  2077. goto out_err_stop_timer;
  2078. }
  2079. rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
  2080. stat_file_read_proc, NULL,
  2081. new_smi, THIS_MODULE);
  2082. if (rv) {
  2083. printk(KERN_ERR
  2084. "ipmi_si: Unable to create proc entry: %d\n",
  2085. rv);
  2086. goto out_err_stop_timer;
  2087. }
  2088. list_add_tail(&new_smi->link, &smi_infos);
  2089. mutex_unlock(&smi_infos_lock);
  2090. printk(" IPMI %s interface initialized\n",si_to_str[new_smi->si_type]);
  2091. return 0;
  2092. out_err_stop_timer:
  2093. atomic_inc(&new_smi->stop_operation);
  2094. wait_for_timer_and_thread(new_smi);
  2095. out_err:
  2096. if (new_smi->intf)
  2097. ipmi_unregister_smi(new_smi->intf);
  2098. if (new_smi->irq_cleanup)
  2099. new_smi->irq_cleanup(new_smi);
  2100. /* Wait until we know that we are out of any interrupt
  2101. handlers might have been running before we freed the
  2102. interrupt. */
  2103. synchronize_sched();
  2104. if (new_smi->si_sm) {
  2105. if (new_smi->handlers)
  2106. new_smi->handlers->cleanup(new_smi->si_sm);
  2107. kfree(new_smi->si_sm);
  2108. }
  2109. if (new_smi->addr_source_cleanup)
  2110. new_smi->addr_source_cleanup(new_smi);
  2111. if (new_smi->io_cleanup)
  2112. new_smi->io_cleanup(new_smi);
  2113. if (new_smi->dev_registered)
  2114. platform_device_unregister(new_smi->pdev);
  2115. kfree(new_smi);
  2116. mutex_unlock(&smi_infos_lock);
  2117. return rv;
  2118. }
  2119. static __devinit int init_ipmi_si(void)
  2120. {
  2121. int i;
  2122. char *str;
  2123. int rv;
  2124. if (initialized)
  2125. return 0;
  2126. initialized = 1;
  2127. /* Register the device drivers. */
  2128. rv = driver_register(&ipmi_driver);
  2129. if (rv) {
  2130. printk(KERN_ERR
  2131. "init_ipmi_si: Unable to register driver: %d\n",
  2132. rv);
  2133. return rv;
  2134. }
  2135. /* Parse out the si_type string into its components. */
  2136. str = si_type_str;
  2137. if (*str != '\0') {
  2138. for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
  2139. si_type[i] = str;
  2140. str = strchr(str, ',');
  2141. if (str) {
  2142. *str = '\0';
  2143. str++;
  2144. } else {
  2145. break;
  2146. }
  2147. }
  2148. }
  2149. printk(KERN_INFO "IPMI System Interface driver.\n");
  2150. hardcode_find_bmc();
  2151. #ifdef CONFIG_DMI
  2152. dmi_find_bmc();
  2153. #endif
  2154. #ifdef CONFIG_ACPI
  2155. if (si_trydefaults)
  2156. acpi_find_bmc();
  2157. #endif
  2158. #ifdef CONFIG_PCI
  2159. pci_module_init(&ipmi_pci_driver);
  2160. #endif
  2161. if (si_trydefaults) {
  2162. mutex_lock(&smi_infos_lock);
  2163. if (list_empty(&smi_infos)) {
  2164. /* No BMC was found, try defaults. */
  2165. mutex_unlock(&smi_infos_lock);
  2166. default_find_bmc();
  2167. } else {
  2168. mutex_unlock(&smi_infos_lock);
  2169. }
  2170. }
  2171. mutex_lock(&smi_infos_lock);
  2172. if (list_empty(&smi_infos)) {
  2173. mutex_unlock(&smi_infos_lock);
  2174. #ifdef CONFIG_PCI
  2175. pci_unregister_driver(&ipmi_pci_driver);
  2176. #endif
  2177. printk("ipmi_si: Unable to find any System Interface(s)\n");
  2178. return -ENODEV;
  2179. } else {
  2180. mutex_unlock(&smi_infos_lock);
  2181. return 0;
  2182. }
  2183. }
  2184. module_init(init_ipmi_si);
  2185. static void __devexit cleanup_one_si(struct smi_info *to_clean)
  2186. {
  2187. int rv;
  2188. unsigned long flags;
  2189. if (!to_clean)
  2190. return;
  2191. list_del(&to_clean->link);
  2192. /* Tell the timer and interrupt handlers that we are shutting
  2193. down. */
  2194. spin_lock_irqsave(&(to_clean->si_lock), flags);
  2195. spin_lock(&(to_clean->msg_lock));
  2196. atomic_inc(&to_clean->stop_operation);
  2197. if (to_clean->irq_cleanup)
  2198. to_clean->irq_cleanup(to_clean);
  2199. spin_unlock(&(to_clean->msg_lock));
  2200. spin_unlock_irqrestore(&(to_clean->si_lock), flags);
  2201. /* Wait until we know that we are out of any interrupt
  2202. handlers might have been running before we freed the
  2203. interrupt. */
  2204. synchronize_sched();
  2205. wait_for_timer_and_thread(to_clean);
  2206. /* Interrupts and timeouts are stopped, now make sure the
  2207. interface is in a clean state. */
  2208. while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
  2209. poll(to_clean);
  2210. schedule_timeout_uninterruptible(1);
  2211. }
  2212. rv = ipmi_unregister_smi(to_clean->intf);
  2213. if (rv) {
  2214. printk(KERN_ERR
  2215. "ipmi_si: Unable to unregister device: errno=%d\n",
  2216. rv);
  2217. }
  2218. to_clean->handlers->cleanup(to_clean->si_sm);
  2219. kfree(to_clean->si_sm);
  2220. if (to_clean->addr_source_cleanup)
  2221. to_clean->addr_source_cleanup(to_clean);
  2222. if (to_clean->io_cleanup)
  2223. to_clean->io_cleanup(to_clean);
  2224. if (to_clean->dev_registered)
  2225. platform_device_unregister(to_clean->pdev);
  2226. kfree(to_clean);
  2227. }
  2228. static __exit void cleanup_ipmi_si(void)
  2229. {
  2230. struct smi_info *e, *tmp_e;
  2231. if (!initialized)
  2232. return;
  2233. #ifdef CONFIG_PCI
  2234. pci_unregister_driver(&ipmi_pci_driver);
  2235. #endif
  2236. mutex_lock(&smi_infos_lock);
  2237. list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
  2238. cleanup_one_si(e);
  2239. mutex_unlock(&smi_infos_lock);
  2240. driver_unregister(&ipmi_driver);
  2241. }
  2242. module_exit(cleanup_ipmi_si);
  2243. MODULE_LICENSE("GPL");
  2244. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  2245. MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT system interfaces.");