ipmi_si_intf.c 89 KB

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