ipmi_si_intf.c 89 KB

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