ipmi_si_intf.c 89 KB

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