ipmi_si_intf.c 96 KB

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