ipmi_si_intf.c 97 KB

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