ipmi_si_intf.c 88 KB

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