ipmi_si_intf.c 67 KB

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