ipmi_si_intf.c 96 KB

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