ipmi_si_intf.c 88 KB

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