ipmi_si_intf.c 86 KB

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