ipmi_si_intf.c 87 KB

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