ipmi_si_intf.c 89 KB

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