ipmi_si_intf.c 98 KB

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