ipmi_si_intf.c 61 KB

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