ipmi_si_intf.c 78 KB

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