ipmi_ssif.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  1. /*
  2. * ipmi_ssif.c
  3. *
  4. * The interface to the IPMI driver for SMBus access to a SMBus
  5. * compliant device. Called SSIF by the IPMI spec.
  6. *
  7. * Author: Intel Corporation
  8. * Todd Davis <todd.c.davis@intel.com>
  9. *
  10. * Rewritten by Corey Minyard <minyard@acm.org> to support the
  11. * non-blocking I2C interface, add support for multi-part
  12. * transactions, add PEC support, and general clenaup.
  13. *
  14. * Copyright 2003 Intel Corporation
  15. * Copyright 2005 MontaVista Software
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the
  19. * Free Software Foundation; either version 2 of the License, or (at your
  20. * option) any later version.
  21. */
  22. /*
  23. * This file holds the "policy" for the interface to the SSIF state
  24. * machine. It does the configuration, handles timers and interrupts,
  25. * and drives the real SSIF state machine.
  26. */
  27. /*
  28. * TODO: Figure out how to use SMB alerts. This will require a new
  29. * interface into the I2C driver, I believe.
  30. */
  31. #if defined(MODVERSIONS)
  32. #include <linux/modversions.h>
  33. #endif
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/sched.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/timer.h>
  39. #include <linux/delay.h>
  40. #include <linux/errno.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/slab.h>
  43. #include <linux/list.h>
  44. #include <linux/i2c.h>
  45. #include <linux/ipmi_smi.h>
  46. #include <linux/init.h>
  47. #include <linux/dmi.h>
  48. #include <linux/kthread.h>
  49. #include <linux/acpi.h>
  50. #include <linux/ctype.h>
  51. #include <linux/time64.h>
  52. #define PFX "ipmi_ssif: "
  53. #define DEVICE_NAME "ipmi_ssif"
  54. #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
  55. #define SSIF_IPMI_REQUEST 2
  56. #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
  57. #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
  58. #define SSIF_IPMI_RESPONSE 3
  59. #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
  60. /* ssif_debug is a bit-field
  61. * SSIF_DEBUG_MSG - commands and their responses
  62. * SSIF_DEBUG_STATES - message states
  63. * SSIF_DEBUG_TIMING - Measure times between events in the driver
  64. */
  65. #define SSIF_DEBUG_TIMING 4
  66. #define SSIF_DEBUG_STATE 2
  67. #define SSIF_DEBUG_MSG 1
  68. #define SSIF_NODEBUG 0
  69. #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
  70. /*
  71. * Timer values
  72. */
  73. #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
  74. #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
  75. /* How many times to we retry sending/receiving the message. */
  76. #define SSIF_SEND_RETRIES 5
  77. #define SSIF_RECV_RETRIES 250
  78. #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
  79. #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
  80. #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
  81. enum ssif_intf_state {
  82. SSIF_NORMAL,
  83. SSIF_GETTING_FLAGS,
  84. SSIF_GETTING_EVENTS,
  85. SSIF_CLEARING_FLAGS,
  86. SSIF_GETTING_MESSAGES,
  87. /* FIXME - add watchdog stuff. */
  88. };
  89. #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
  90. && (ssif)->curr_msg == NULL)
  91. /*
  92. * Indexes into stats[] in ssif_info below.
  93. */
  94. enum ssif_stat_indexes {
  95. /* Number of total messages sent. */
  96. SSIF_STAT_sent_messages = 0,
  97. /*
  98. * Number of message parts sent. Messages may be broken into
  99. * parts if they are long.
  100. */
  101. SSIF_STAT_sent_messages_parts,
  102. /*
  103. * Number of time a message was retried.
  104. */
  105. SSIF_STAT_send_retries,
  106. /*
  107. * Number of times the send of a message failed.
  108. */
  109. SSIF_STAT_send_errors,
  110. /*
  111. * Number of message responses received.
  112. */
  113. SSIF_STAT_received_messages,
  114. /*
  115. * Number of message fragments received.
  116. */
  117. SSIF_STAT_received_message_parts,
  118. /*
  119. * Number of times the receive of a message was retried.
  120. */
  121. SSIF_STAT_receive_retries,
  122. /*
  123. * Number of errors receiving messages.
  124. */
  125. SSIF_STAT_receive_errors,
  126. /*
  127. * Number of times a flag fetch was requested.
  128. */
  129. SSIF_STAT_flag_fetches,
  130. /*
  131. * Number of times the hardware didn't follow the state machine.
  132. */
  133. SSIF_STAT_hosed,
  134. /*
  135. * Number of received events.
  136. */
  137. SSIF_STAT_events,
  138. /* Number of asyncronous messages received. */
  139. SSIF_STAT_incoming_messages,
  140. /* Number of watchdog pretimeouts. */
  141. SSIF_STAT_watchdog_pretimeouts,
  142. /* Number of alers received. */
  143. SSIF_STAT_alerts,
  144. /* Always add statistics before this value, it must be last. */
  145. SSIF_NUM_STATS
  146. };
  147. struct ssif_addr_info {
  148. unsigned short addr;
  149. struct i2c_board_info binfo;
  150. char *adapter_name;
  151. int debug;
  152. int slave_addr;
  153. enum ipmi_addr_src addr_src;
  154. union ipmi_smi_info_union addr_info;
  155. struct mutex clients_mutex;
  156. struct list_head clients;
  157. struct list_head link;
  158. };
  159. struct ssif_info;
  160. typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
  161. unsigned char *data, unsigned int len);
  162. struct ssif_info {
  163. ipmi_smi_t intf;
  164. int intf_num;
  165. spinlock_t lock;
  166. struct ipmi_smi_msg *waiting_msg;
  167. struct ipmi_smi_msg *curr_msg;
  168. enum ssif_intf_state ssif_state;
  169. unsigned long ssif_debug;
  170. struct ipmi_smi_handlers handlers;
  171. enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
  172. union ipmi_smi_info_union addr_info;
  173. /*
  174. * Flags from the last GET_MSG_FLAGS command, used when an ATTN
  175. * is set to hold the flags until we are done handling everything
  176. * from the flags.
  177. */
  178. #define RECEIVE_MSG_AVAIL 0x01
  179. #define EVENT_MSG_BUFFER_FULL 0x02
  180. #define WDT_PRE_TIMEOUT_INT 0x08
  181. unsigned char msg_flags;
  182. u8 global_enables;
  183. bool has_event_buffer;
  184. bool supports_alert;
  185. /*
  186. * Used to tell what we should do with alerts. If we are
  187. * waiting on a response, read the data immediately.
  188. */
  189. bool got_alert;
  190. bool waiting_alert;
  191. /*
  192. * If set to true, this will request events the next time the
  193. * state machine is idle.
  194. */
  195. bool req_events;
  196. /*
  197. * If set to true, this will request flags the next time the
  198. * state machine is idle.
  199. */
  200. bool req_flags;
  201. /*
  202. * Used to perform timer operations when run-to-completion
  203. * mode is on. This is a countdown timer.
  204. */
  205. int rtc_us_timer;
  206. /* Used for sending/receiving data. +1 for the length. */
  207. unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
  208. unsigned int data_len;
  209. /* Temp receive buffer, gets copied into data. */
  210. unsigned char recv[I2C_SMBUS_BLOCK_MAX];
  211. struct i2c_client *client;
  212. ssif_i2c_done done_handler;
  213. /* Thread interface handling */
  214. struct task_struct *thread;
  215. struct completion wake_thread;
  216. bool stopping;
  217. int i2c_read_write;
  218. int i2c_command;
  219. unsigned char *i2c_data;
  220. unsigned int i2c_size;
  221. /* From the device id response. */
  222. struct ipmi_device_id device_id;
  223. struct timer_list retry_timer;
  224. int retries_left;
  225. /* Info from SSIF cmd */
  226. unsigned char max_xmit_msg_size;
  227. unsigned char max_recv_msg_size;
  228. unsigned int multi_support;
  229. int supports_pec;
  230. #define SSIF_NO_MULTI 0
  231. #define SSIF_MULTI_2_PART 1
  232. #define SSIF_MULTI_n_PART 2
  233. unsigned char *multi_data;
  234. unsigned int multi_len;
  235. unsigned int multi_pos;
  236. atomic_t stats[SSIF_NUM_STATS];
  237. };
  238. #define ssif_inc_stat(ssif, stat) \
  239. atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
  240. #define ssif_get_stat(ssif, stat) \
  241. ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
  242. static bool initialized;
  243. static atomic_t next_intf = ATOMIC_INIT(0);
  244. static void return_hosed_msg(struct ssif_info *ssif_info,
  245. struct ipmi_smi_msg *msg);
  246. static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
  247. static int start_send(struct ssif_info *ssif_info,
  248. unsigned char *data,
  249. unsigned int len);
  250. static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
  251. unsigned long *flags)
  252. {
  253. spin_lock_irqsave(&ssif_info->lock, *flags);
  254. return flags;
  255. }
  256. static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
  257. unsigned long *flags)
  258. {
  259. spin_unlock_irqrestore(&ssif_info->lock, *flags);
  260. }
  261. static void deliver_recv_msg(struct ssif_info *ssif_info,
  262. struct ipmi_smi_msg *msg)
  263. {
  264. ipmi_smi_t intf = ssif_info->intf;
  265. if (!intf) {
  266. ipmi_free_smi_msg(msg);
  267. } else if (msg->rsp_size < 0) {
  268. return_hosed_msg(ssif_info, msg);
  269. pr_err(PFX
  270. "Malformed message in deliver_recv_msg: rsp_size = %d\n",
  271. msg->rsp_size);
  272. } else {
  273. ipmi_smi_msg_received(intf, msg);
  274. }
  275. }
  276. static void return_hosed_msg(struct ssif_info *ssif_info,
  277. struct ipmi_smi_msg *msg)
  278. {
  279. ssif_inc_stat(ssif_info, hosed);
  280. /* Make it a response */
  281. msg->rsp[0] = msg->data[0] | 4;
  282. msg->rsp[1] = msg->data[1];
  283. msg->rsp[2] = 0xFF; /* Unknown error. */
  284. msg->rsp_size = 3;
  285. deliver_recv_msg(ssif_info, msg);
  286. }
  287. /*
  288. * Must be called with the message lock held. This will release the
  289. * message lock. Note that the caller will check SSIF_IDLE and start a
  290. * new operation, so there is no need to check for new messages to
  291. * start in here.
  292. */
  293. static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
  294. {
  295. unsigned char msg[3];
  296. ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
  297. ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
  298. ipmi_ssif_unlock_cond(ssif_info, flags);
  299. /* Make sure the watchdog pre-timeout flag is not set at startup. */
  300. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  301. msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
  302. msg[2] = WDT_PRE_TIMEOUT_INT;
  303. if (start_send(ssif_info, msg, 3) != 0) {
  304. /* Error, just go to normal state. */
  305. ssif_info->ssif_state = SSIF_NORMAL;
  306. }
  307. }
  308. static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
  309. {
  310. unsigned char mb[2];
  311. ssif_info->req_flags = false;
  312. ssif_info->ssif_state = SSIF_GETTING_FLAGS;
  313. ipmi_ssif_unlock_cond(ssif_info, flags);
  314. mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
  315. mb[1] = IPMI_GET_MSG_FLAGS_CMD;
  316. if (start_send(ssif_info, mb, 2) != 0)
  317. ssif_info->ssif_state = SSIF_NORMAL;
  318. }
  319. static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
  320. struct ipmi_smi_msg *msg)
  321. {
  322. if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
  323. unsigned long oflags;
  324. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  325. ssif_info->curr_msg = NULL;
  326. ssif_info->ssif_state = SSIF_NORMAL;
  327. ipmi_ssif_unlock_cond(ssif_info, flags);
  328. ipmi_free_smi_msg(msg);
  329. }
  330. }
  331. static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
  332. {
  333. struct ipmi_smi_msg *msg;
  334. ssif_info->req_events = false;
  335. msg = ipmi_alloc_smi_msg();
  336. if (!msg) {
  337. ssif_info->ssif_state = SSIF_NORMAL;
  338. ipmi_ssif_unlock_cond(ssif_info, flags);
  339. return;
  340. }
  341. ssif_info->curr_msg = msg;
  342. ssif_info->ssif_state = SSIF_GETTING_EVENTS;
  343. ipmi_ssif_unlock_cond(ssif_info, flags);
  344. msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  345. msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
  346. msg->data_size = 2;
  347. check_start_send(ssif_info, flags, msg);
  348. }
  349. static void start_recv_msg_fetch(struct ssif_info *ssif_info,
  350. unsigned long *flags)
  351. {
  352. struct ipmi_smi_msg *msg;
  353. msg = ipmi_alloc_smi_msg();
  354. if (!msg) {
  355. ssif_info->ssif_state = SSIF_NORMAL;
  356. ipmi_ssif_unlock_cond(ssif_info, flags);
  357. return;
  358. }
  359. ssif_info->curr_msg = msg;
  360. ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
  361. ipmi_ssif_unlock_cond(ssif_info, flags);
  362. msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  363. msg->data[1] = IPMI_GET_MSG_CMD;
  364. msg->data_size = 2;
  365. check_start_send(ssif_info, flags, msg);
  366. }
  367. /*
  368. * Must be called with the message lock held. This will release the
  369. * message lock. Note that the caller will check SSIF_IDLE and start a
  370. * new operation, so there is no need to check for new messages to
  371. * start in here.
  372. */
  373. static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
  374. {
  375. if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
  376. ipmi_smi_t intf = ssif_info->intf;
  377. /* Watchdog pre-timeout */
  378. ssif_inc_stat(ssif_info, watchdog_pretimeouts);
  379. start_clear_flags(ssif_info, flags);
  380. if (intf)
  381. ipmi_smi_watchdog_pretimeout(intf);
  382. } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
  383. /* Messages available. */
  384. start_recv_msg_fetch(ssif_info, flags);
  385. else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
  386. /* Events available. */
  387. start_event_fetch(ssif_info, flags);
  388. else {
  389. ssif_info->ssif_state = SSIF_NORMAL;
  390. ipmi_ssif_unlock_cond(ssif_info, flags);
  391. }
  392. }
  393. static int ipmi_ssif_thread(void *data)
  394. {
  395. struct ssif_info *ssif_info = data;
  396. while (!kthread_should_stop()) {
  397. int result;
  398. /* Wait for something to do */
  399. result = wait_for_completion_interruptible(
  400. &ssif_info->wake_thread);
  401. if (ssif_info->stopping)
  402. break;
  403. if (result == -ERESTARTSYS)
  404. continue;
  405. init_completion(&ssif_info->wake_thread);
  406. if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
  407. result = i2c_smbus_write_block_data(
  408. ssif_info->client, ssif_info->i2c_command,
  409. ssif_info->i2c_data[0],
  410. ssif_info->i2c_data + 1);
  411. ssif_info->done_handler(ssif_info, result, NULL, 0);
  412. } else {
  413. result = i2c_smbus_read_block_data(
  414. ssif_info->client, ssif_info->i2c_command,
  415. ssif_info->i2c_data);
  416. if (result < 0)
  417. ssif_info->done_handler(ssif_info, result,
  418. NULL, 0);
  419. else
  420. ssif_info->done_handler(ssif_info, 0,
  421. ssif_info->i2c_data,
  422. result);
  423. }
  424. }
  425. return 0;
  426. }
  427. static int ssif_i2c_send(struct ssif_info *ssif_info,
  428. ssif_i2c_done handler,
  429. int read_write, int command,
  430. unsigned char *data, unsigned int size)
  431. {
  432. ssif_info->done_handler = handler;
  433. ssif_info->i2c_read_write = read_write;
  434. ssif_info->i2c_command = command;
  435. ssif_info->i2c_data = data;
  436. ssif_info->i2c_size = size;
  437. complete(&ssif_info->wake_thread);
  438. return 0;
  439. }
  440. static void msg_done_handler(struct ssif_info *ssif_info, int result,
  441. unsigned char *data, unsigned int len);
  442. static void start_get(struct ssif_info *ssif_info)
  443. {
  444. int rv;
  445. ssif_info->rtc_us_timer = 0;
  446. ssif_info->multi_pos = 0;
  447. rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
  448. SSIF_IPMI_RESPONSE,
  449. ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
  450. if (rv < 0) {
  451. /* request failed, just return the error. */
  452. if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
  453. pr_info("Error from i2c_non_blocking_op(5)\n");
  454. msg_done_handler(ssif_info, -EIO, NULL, 0);
  455. }
  456. }
  457. static void retry_timeout(unsigned long data)
  458. {
  459. struct ssif_info *ssif_info = (void *) data;
  460. unsigned long oflags, *flags;
  461. bool waiting;
  462. if (ssif_info->stopping)
  463. return;
  464. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  465. waiting = ssif_info->waiting_alert;
  466. ssif_info->waiting_alert = false;
  467. ipmi_ssif_unlock_cond(ssif_info, flags);
  468. if (waiting)
  469. start_get(ssif_info);
  470. }
  471. static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
  472. unsigned int data)
  473. {
  474. struct ssif_info *ssif_info = i2c_get_clientdata(client);
  475. unsigned long oflags, *flags;
  476. bool do_get = false;
  477. if (type != I2C_PROTOCOL_SMBUS_ALERT)
  478. return;
  479. ssif_inc_stat(ssif_info, alerts);
  480. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  481. if (ssif_info->waiting_alert) {
  482. ssif_info->waiting_alert = false;
  483. del_timer(&ssif_info->retry_timer);
  484. do_get = true;
  485. } else if (ssif_info->curr_msg) {
  486. ssif_info->got_alert = true;
  487. }
  488. ipmi_ssif_unlock_cond(ssif_info, flags);
  489. if (do_get)
  490. start_get(ssif_info);
  491. }
  492. static int start_resend(struct ssif_info *ssif_info);
  493. static void msg_done_handler(struct ssif_info *ssif_info, int result,
  494. unsigned char *data, unsigned int len)
  495. {
  496. struct ipmi_smi_msg *msg;
  497. unsigned long oflags, *flags;
  498. int rv;
  499. /*
  500. * We are single-threaded here, so no need for a lock until we
  501. * start messing with driver states or the queues.
  502. */
  503. if (result < 0) {
  504. ssif_info->retries_left--;
  505. if (ssif_info->retries_left > 0) {
  506. ssif_inc_stat(ssif_info, receive_retries);
  507. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  508. ssif_info->waiting_alert = true;
  509. ssif_info->rtc_us_timer = SSIF_MSG_USEC;
  510. mod_timer(&ssif_info->retry_timer,
  511. jiffies + SSIF_MSG_JIFFIES);
  512. ipmi_ssif_unlock_cond(ssif_info, flags);
  513. return;
  514. }
  515. ssif_inc_stat(ssif_info, receive_errors);
  516. if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
  517. pr_info("Error in msg_done_handler: %d\n", result);
  518. len = 0;
  519. goto continue_op;
  520. }
  521. if ((len > 1) && (ssif_info->multi_pos == 0)
  522. && (data[0] == 0x00) && (data[1] == 0x01)) {
  523. /* Start of multi-part read. Start the next transaction. */
  524. int i;
  525. ssif_inc_stat(ssif_info, received_message_parts);
  526. /* Remove the multi-part read marker. */
  527. len -= 2;
  528. for (i = 0; i < len; i++)
  529. ssif_info->data[i] = data[i+2];
  530. ssif_info->multi_len = len;
  531. ssif_info->multi_pos = 1;
  532. rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
  533. SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
  534. ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
  535. if (rv < 0) {
  536. if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
  537. pr_info("Error from i2c_non_blocking_op(1)\n");
  538. result = -EIO;
  539. } else
  540. return;
  541. } else if (ssif_info->multi_pos) {
  542. /* Middle of multi-part read. Start the next transaction. */
  543. int i;
  544. unsigned char blocknum;
  545. if (len == 0) {
  546. result = -EIO;
  547. if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
  548. pr_info(PFX "Middle message with no data\n");
  549. goto continue_op;
  550. }
  551. blocknum = data[0];
  552. if (ssif_info->multi_len + len - 1 > IPMI_MAX_MSG_LENGTH) {
  553. /* Received message too big, abort the operation. */
  554. result = -E2BIG;
  555. if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
  556. pr_info("Received message too big\n");
  557. goto continue_op;
  558. }
  559. /* Remove the blocknum from the data. */
  560. len--;
  561. for (i = 0; i < len; i++)
  562. ssif_info->data[i + ssif_info->multi_len] = data[i + 1];
  563. ssif_info->multi_len += len;
  564. if (blocknum == 0xff) {
  565. /* End of read */
  566. len = ssif_info->multi_len;
  567. data = ssif_info->data;
  568. } else if (blocknum + 1 != ssif_info->multi_pos) {
  569. /*
  570. * Out of sequence block, just abort. Block
  571. * numbers start at zero for the second block,
  572. * but multi_pos starts at one, so the +1.
  573. */
  574. result = -EIO;
  575. } else {
  576. ssif_inc_stat(ssif_info, received_message_parts);
  577. ssif_info->multi_pos++;
  578. rv = ssif_i2c_send(ssif_info, msg_done_handler,
  579. I2C_SMBUS_READ,
  580. SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
  581. ssif_info->recv,
  582. I2C_SMBUS_BLOCK_DATA);
  583. if (rv < 0) {
  584. if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
  585. pr_info(PFX
  586. "Error from ssif_i2c_send\n");
  587. result = -EIO;
  588. } else
  589. return;
  590. }
  591. }
  592. if (result < 0) {
  593. ssif_inc_stat(ssif_info, receive_errors);
  594. } else {
  595. ssif_inc_stat(ssif_info, received_messages);
  596. ssif_inc_stat(ssif_info, received_message_parts);
  597. }
  598. continue_op:
  599. if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
  600. pr_info(PFX "DONE 1: state = %d, result=%d.\n",
  601. ssif_info->ssif_state, result);
  602. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  603. msg = ssif_info->curr_msg;
  604. if (msg) {
  605. msg->rsp_size = len;
  606. if (msg->rsp_size > IPMI_MAX_MSG_LENGTH)
  607. msg->rsp_size = IPMI_MAX_MSG_LENGTH;
  608. memcpy(msg->rsp, data, msg->rsp_size);
  609. ssif_info->curr_msg = NULL;
  610. }
  611. switch (ssif_info->ssif_state) {
  612. case SSIF_NORMAL:
  613. ipmi_ssif_unlock_cond(ssif_info, flags);
  614. if (!msg)
  615. break;
  616. if (result < 0)
  617. return_hosed_msg(ssif_info, msg);
  618. else
  619. deliver_recv_msg(ssif_info, msg);
  620. break;
  621. case SSIF_GETTING_FLAGS:
  622. /* We got the flags from the SSIF, now handle them. */
  623. if ((result < 0) || (len < 4) || (data[2] != 0)) {
  624. /*
  625. * Error fetching flags, or invalid length,
  626. * just give up for now.
  627. */
  628. ssif_info->ssif_state = SSIF_NORMAL;
  629. ipmi_ssif_unlock_cond(ssif_info, flags);
  630. pr_warn(PFX "Error getting flags: %d %d, %x\n",
  631. result, len, (len >= 3) ? data[2] : 0);
  632. } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
  633. || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
  634. /*
  635. * Don't abort here, maybe it was a queued
  636. * response to a previous command.
  637. */
  638. ipmi_ssif_unlock_cond(ssif_info, flags);
  639. pr_warn(PFX "Invalid response getting flags: %x %x\n",
  640. data[0], data[1]);
  641. } else {
  642. ssif_inc_stat(ssif_info, flag_fetches);
  643. ssif_info->msg_flags = data[3];
  644. handle_flags(ssif_info, flags);
  645. }
  646. break;
  647. case SSIF_CLEARING_FLAGS:
  648. /* We cleared the flags. */
  649. if ((result < 0) || (len < 3) || (data[2] != 0)) {
  650. /* Error clearing flags */
  651. pr_warn(PFX "Error clearing flags: %d %d, %x\n",
  652. result, len, (len >= 3) ? data[2] : 0);
  653. } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
  654. || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
  655. pr_warn(PFX "Invalid response clearing flags: %x %x\n",
  656. data[0], data[1]);
  657. }
  658. ssif_info->ssif_state = SSIF_NORMAL;
  659. ipmi_ssif_unlock_cond(ssif_info, flags);
  660. break;
  661. case SSIF_GETTING_EVENTS:
  662. if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
  663. /* Error getting event, probably done. */
  664. msg->done(msg);
  665. /* Take off the event flag. */
  666. ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
  667. handle_flags(ssif_info, flags);
  668. } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
  669. || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
  670. pr_warn(PFX "Invalid response getting events: %x %x\n",
  671. msg->rsp[0], msg->rsp[1]);
  672. msg->done(msg);
  673. /* Take off the event flag. */
  674. ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
  675. handle_flags(ssif_info, flags);
  676. } else {
  677. handle_flags(ssif_info, flags);
  678. ssif_inc_stat(ssif_info, events);
  679. deliver_recv_msg(ssif_info, msg);
  680. }
  681. break;
  682. case SSIF_GETTING_MESSAGES:
  683. if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
  684. /* Error getting event, probably done. */
  685. msg->done(msg);
  686. /* Take off the msg flag. */
  687. ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
  688. handle_flags(ssif_info, flags);
  689. } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
  690. || msg->rsp[1] != IPMI_GET_MSG_CMD) {
  691. pr_warn(PFX "Invalid response clearing flags: %x %x\n",
  692. msg->rsp[0], msg->rsp[1]);
  693. msg->done(msg);
  694. /* Take off the msg flag. */
  695. ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
  696. handle_flags(ssif_info, flags);
  697. } else {
  698. ssif_inc_stat(ssif_info, incoming_messages);
  699. handle_flags(ssif_info, flags);
  700. deliver_recv_msg(ssif_info, msg);
  701. }
  702. break;
  703. }
  704. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  705. if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
  706. if (ssif_info->req_events)
  707. start_event_fetch(ssif_info, flags);
  708. else if (ssif_info->req_flags)
  709. start_flag_fetch(ssif_info, flags);
  710. else
  711. start_next_msg(ssif_info, flags);
  712. } else
  713. ipmi_ssif_unlock_cond(ssif_info, flags);
  714. if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
  715. pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
  716. }
  717. static void msg_written_handler(struct ssif_info *ssif_info, int result,
  718. unsigned char *data, unsigned int len)
  719. {
  720. int rv;
  721. /* We are single-threaded here, so no need for a lock. */
  722. if (result < 0) {
  723. ssif_info->retries_left--;
  724. if (ssif_info->retries_left > 0) {
  725. if (!start_resend(ssif_info)) {
  726. ssif_inc_stat(ssif_info, send_retries);
  727. return;
  728. }
  729. /* request failed, just return the error. */
  730. ssif_inc_stat(ssif_info, send_errors);
  731. if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
  732. pr_info(PFX
  733. "Out of retries in msg_written_handler\n");
  734. msg_done_handler(ssif_info, -EIO, NULL, 0);
  735. return;
  736. }
  737. ssif_inc_stat(ssif_info, send_errors);
  738. /*
  739. * Got an error on transmit, let the done routine
  740. * handle it.
  741. */
  742. if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
  743. pr_info("Error in msg_written_handler: %d\n", result);
  744. msg_done_handler(ssif_info, result, NULL, 0);
  745. return;
  746. }
  747. if (ssif_info->multi_data) {
  748. /*
  749. * In the middle of a multi-data write. See the comment
  750. * in the SSIF_MULTI_n_PART case in the probe function
  751. * for details on the intricacies of this.
  752. */
  753. int left;
  754. unsigned char *data_to_send;
  755. ssif_inc_stat(ssif_info, sent_messages_parts);
  756. left = ssif_info->multi_len - ssif_info->multi_pos;
  757. if (left > 32)
  758. left = 32;
  759. /* Length byte. */
  760. ssif_info->multi_data[ssif_info->multi_pos] = left;
  761. data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
  762. ssif_info->multi_pos += left;
  763. if (left < 32)
  764. /*
  765. * Write is finished. Note that we must end
  766. * with a write of less than 32 bytes to
  767. * complete the transaction, even if it is
  768. * zero bytes.
  769. */
  770. ssif_info->multi_data = NULL;
  771. rv = ssif_i2c_send(ssif_info, msg_written_handler,
  772. I2C_SMBUS_WRITE,
  773. SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
  774. data_to_send,
  775. I2C_SMBUS_BLOCK_DATA);
  776. if (rv < 0) {
  777. /* request failed, just return the error. */
  778. ssif_inc_stat(ssif_info, send_errors);
  779. if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
  780. pr_info("Error from i2c_non_blocking_op(3)\n");
  781. msg_done_handler(ssif_info, -EIO, NULL, 0);
  782. }
  783. } else {
  784. /* Ready to request the result. */
  785. unsigned long oflags, *flags;
  786. ssif_inc_stat(ssif_info, sent_messages);
  787. ssif_inc_stat(ssif_info, sent_messages_parts);
  788. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  789. if (ssif_info->got_alert) {
  790. /* The result is already ready, just start it. */
  791. ssif_info->got_alert = false;
  792. ipmi_ssif_unlock_cond(ssif_info, flags);
  793. start_get(ssif_info);
  794. } else {
  795. /* Wait a jiffie then request the next message */
  796. ssif_info->waiting_alert = true;
  797. ssif_info->retries_left = SSIF_RECV_RETRIES;
  798. ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
  799. mod_timer(&ssif_info->retry_timer,
  800. jiffies + SSIF_MSG_PART_JIFFIES);
  801. ipmi_ssif_unlock_cond(ssif_info, flags);
  802. }
  803. }
  804. }
  805. static int start_resend(struct ssif_info *ssif_info)
  806. {
  807. int rv;
  808. int command;
  809. ssif_info->got_alert = false;
  810. if (ssif_info->data_len > 32) {
  811. command = SSIF_IPMI_MULTI_PART_REQUEST_START;
  812. ssif_info->multi_data = ssif_info->data;
  813. ssif_info->multi_len = ssif_info->data_len;
  814. /*
  815. * Subtle thing, this is 32, not 33, because we will
  816. * overwrite the thing at position 32 (which was just
  817. * transmitted) with the new length.
  818. */
  819. ssif_info->multi_pos = 32;
  820. ssif_info->data[0] = 32;
  821. } else {
  822. ssif_info->multi_data = NULL;
  823. command = SSIF_IPMI_REQUEST;
  824. ssif_info->data[0] = ssif_info->data_len;
  825. }
  826. rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
  827. command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
  828. if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
  829. pr_info("Error from i2c_non_blocking_op(4)\n");
  830. return rv;
  831. }
  832. static int start_send(struct ssif_info *ssif_info,
  833. unsigned char *data,
  834. unsigned int len)
  835. {
  836. if (len > IPMI_MAX_MSG_LENGTH)
  837. return -E2BIG;
  838. if (len > ssif_info->max_xmit_msg_size)
  839. return -E2BIG;
  840. ssif_info->retries_left = SSIF_SEND_RETRIES;
  841. memcpy(ssif_info->data + 1, data, len);
  842. ssif_info->data_len = len;
  843. return start_resend(ssif_info);
  844. }
  845. /* Must be called with the message lock held. */
  846. static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
  847. {
  848. struct ipmi_smi_msg *msg;
  849. unsigned long oflags;
  850. restart:
  851. if (!SSIF_IDLE(ssif_info)) {
  852. ipmi_ssif_unlock_cond(ssif_info, flags);
  853. return;
  854. }
  855. if (!ssif_info->waiting_msg) {
  856. ssif_info->curr_msg = NULL;
  857. ipmi_ssif_unlock_cond(ssif_info, flags);
  858. } else {
  859. int rv;
  860. ssif_info->curr_msg = ssif_info->waiting_msg;
  861. ssif_info->waiting_msg = NULL;
  862. ipmi_ssif_unlock_cond(ssif_info, flags);
  863. rv = start_send(ssif_info,
  864. ssif_info->curr_msg->data,
  865. ssif_info->curr_msg->data_size);
  866. if (rv) {
  867. msg = ssif_info->curr_msg;
  868. ssif_info->curr_msg = NULL;
  869. return_hosed_msg(ssif_info, msg);
  870. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  871. goto restart;
  872. }
  873. }
  874. }
  875. static void sender(void *send_info,
  876. struct ipmi_smi_msg *msg)
  877. {
  878. struct ssif_info *ssif_info = (struct ssif_info *) send_info;
  879. unsigned long oflags, *flags;
  880. BUG_ON(ssif_info->waiting_msg);
  881. ssif_info->waiting_msg = msg;
  882. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  883. start_next_msg(ssif_info, flags);
  884. if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
  885. struct timespec64 t;
  886. ktime_get_real_ts64(&t);
  887. pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
  888. msg->data[0], msg->data[1],
  889. (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
  890. }
  891. }
  892. static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
  893. {
  894. struct ssif_info *ssif_info = send_info;
  895. data->addr_src = ssif_info->addr_source;
  896. data->dev = &ssif_info->client->dev;
  897. data->addr_info = ssif_info->addr_info;
  898. get_device(data->dev);
  899. return 0;
  900. }
  901. /*
  902. * Instead of having our own timer to periodically check the message
  903. * flags, we let the message handler drive us.
  904. */
  905. static void request_events(void *send_info)
  906. {
  907. struct ssif_info *ssif_info = (struct ssif_info *) send_info;
  908. unsigned long oflags, *flags;
  909. if (!ssif_info->has_event_buffer)
  910. return;
  911. flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
  912. /*
  913. * Request flags first, not events, because the lower layer
  914. * doesn't have a way to send an attention. But make sure
  915. * event checking still happens.
  916. */
  917. ssif_info->req_events = true;
  918. if (SSIF_IDLE(ssif_info))
  919. start_flag_fetch(ssif_info, flags);
  920. else {
  921. ssif_info->req_flags = true;
  922. ipmi_ssif_unlock_cond(ssif_info, flags);
  923. }
  924. }
  925. static int inc_usecount(void *send_info)
  926. {
  927. struct ssif_info *ssif_info = send_info;
  928. if (!i2c_get_adapter(ssif_info->client->adapter->nr))
  929. return -ENODEV;
  930. i2c_use_client(ssif_info->client);
  931. return 0;
  932. }
  933. static void dec_usecount(void *send_info)
  934. {
  935. struct ssif_info *ssif_info = send_info;
  936. i2c_release_client(ssif_info->client);
  937. i2c_put_adapter(ssif_info->client->adapter);
  938. }
  939. static int ssif_start_processing(void *send_info,
  940. ipmi_smi_t intf)
  941. {
  942. struct ssif_info *ssif_info = send_info;
  943. ssif_info->intf = intf;
  944. return 0;
  945. }
  946. #define MAX_SSIF_BMCS 4
  947. static unsigned short addr[MAX_SSIF_BMCS];
  948. static int num_addrs;
  949. module_param_array(addr, ushort, &num_addrs, 0);
  950. MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
  951. static char *adapter_name[MAX_SSIF_BMCS];
  952. static int num_adapter_names;
  953. module_param_array(adapter_name, charp, &num_adapter_names, 0);
  954. MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
  955. static int slave_addrs[MAX_SSIF_BMCS];
  956. static int num_slave_addrs;
  957. module_param_array(slave_addrs, int, &num_slave_addrs, 0);
  958. MODULE_PARM_DESC(slave_addrs,
  959. "The default IPMB slave address for the controller.");
  960. static bool alerts_broken;
  961. module_param(alerts_broken, bool, 0);
  962. MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
  963. /*
  964. * Bit 0 enables message debugging, bit 1 enables state debugging, and
  965. * bit 2 enables timing debugging. This is an array indexed by
  966. * interface number"
  967. */
  968. static int dbg[MAX_SSIF_BMCS];
  969. static int num_dbg;
  970. module_param_array(dbg, int, &num_dbg, 0);
  971. MODULE_PARM_DESC(dbg, "Turn on debugging.");
  972. static bool ssif_dbg_probe;
  973. module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
  974. MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
  975. static int use_thread;
  976. module_param(use_thread, int, 0);
  977. MODULE_PARM_DESC(use_thread, "Use the thread interface.");
  978. static bool ssif_tryacpi = true;
  979. module_param_named(tryacpi, ssif_tryacpi, bool, 0);
  980. MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
  981. static bool ssif_trydmi = true;
  982. module_param_named(trydmi, ssif_trydmi, bool, 0);
  983. MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
  984. static DEFINE_MUTEX(ssif_infos_mutex);
  985. static LIST_HEAD(ssif_infos);
  986. static int ssif_remove(struct i2c_client *client)
  987. {
  988. struct ssif_info *ssif_info = i2c_get_clientdata(client);
  989. int rv;
  990. if (!ssif_info)
  991. return 0;
  992. /*
  993. * After this point, we won't deliver anything asychronously
  994. * to the message handler. We can unregister ourself.
  995. */
  996. rv = ipmi_unregister_smi(ssif_info->intf);
  997. if (rv) {
  998. pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
  999. return rv;
  1000. }
  1001. ssif_info->intf = NULL;
  1002. /* make sure the driver is not looking for flags any more. */
  1003. while (ssif_info->ssif_state != SSIF_NORMAL)
  1004. schedule_timeout(1);
  1005. ssif_info->stopping = true;
  1006. del_timer_sync(&ssif_info->retry_timer);
  1007. if (ssif_info->thread) {
  1008. complete(&ssif_info->wake_thread);
  1009. kthread_stop(ssif_info->thread);
  1010. }
  1011. /*
  1012. * No message can be outstanding now, we have removed the
  1013. * upper layer and it permitted us to do so.
  1014. */
  1015. kfree(ssif_info);
  1016. return 0;
  1017. }
  1018. static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
  1019. int *resp_len, unsigned char *resp)
  1020. {
  1021. int retry_cnt;
  1022. int ret;
  1023. retry_cnt = SSIF_SEND_RETRIES;
  1024. retry1:
  1025. ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
  1026. if (ret) {
  1027. retry_cnt--;
  1028. if (retry_cnt > 0)
  1029. goto retry1;
  1030. return -ENODEV;
  1031. }
  1032. ret = -ENODEV;
  1033. retry_cnt = SSIF_RECV_RETRIES;
  1034. while (retry_cnt > 0) {
  1035. ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
  1036. resp);
  1037. if (ret > 0)
  1038. break;
  1039. msleep(SSIF_MSG_MSEC);
  1040. retry_cnt--;
  1041. if (retry_cnt <= 0)
  1042. break;
  1043. }
  1044. if (ret > 0) {
  1045. /* Validate that the response is correct. */
  1046. if (ret < 3 ||
  1047. (resp[0] != (msg[0] | (1 << 2))) ||
  1048. (resp[1] != msg[1]))
  1049. ret = -EINVAL;
  1050. else {
  1051. *resp_len = ret;
  1052. ret = 0;
  1053. }
  1054. }
  1055. return ret;
  1056. }
  1057. static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
  1058. {
  1059. unsigned char *resp;
  1060. unsigned char msg[3];
  1061. int rv;
  1062. int len;
  1063. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  1064. if (!resp)
  1065. return -ENOMEM;
  1066. /* Do a Get Device ID command, since it is required. */
  1067. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1068. msg[1] = IPMI_GET_DEVICE_ID_CMD;
  1069. rv = do_cmd(client, 2, msg, &len, resp);
  1070. if (rv)
  1071. rv = -ENODEV;
  1072. else
  1073. strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
  1074. kfree(resp);
  1075. return rv;
  1076. }
  1077. static int smi_type_proc_show(struct seq_file *m, void *v)
  1078. {
  1079. seq_puts(m, "ssif\n");
  1080. return 0;
  1081. }
  1082. static int smi_type_proc_open(struct inode *inode, struct file *file)
  1083. {
  1084. return single_open(file, smi_type_proc_show, inode->i_private);
  1085. }
  1086. static const struct file_operations smi_type_proc_ops = {
  1087. .open = smi_type_proc_open,
  1088. .read = seq_read,
  1089. .llseek = seq_lseek,
  1090. .release = single_release,
  1091. };
  1092. static int smi_stats_proc_show(struct seq_file *m, void *v)
  1093. {
  1094. struct ssif_info *ssif_info = m->private;
  1095. seq_printf(m, "sent_messages: %u\n",
  1096. ssif_get_stat(ssif_info, sent_messages));
  1097. seq_printf(m, "sent_messages_parts: %u\n",
  1098. ssif_get_stat(ssif_info, sent_messages_parts));
  1099. seq_printf(m, "send_retries: %u\n",
  1100. ssif_get_stat(ssif_info, send_retries));
  1101. seq_printf(m, "send_errors: %u\n",
  1102. ssif_get_stat(ssif_info, send_errors));
  1103. seq_printf(m, "received_messages: %u\n",
  1104. ssif_get_stat(ssif_info, received_messages));
  1105. seq_printf(m, "received_message_parts: %u\n",
  1106. ssif_get_stat(ssif_info, received_message_parts));
  1107. seq_printf(m, "receive_retries: %u\n",
  1108. ssif_get_stat(ssif_info, receive_retries));
  1109. seq_printf(m, "receive_errors: %u\n",
  1110. ssif_get_stat(ssif_info, receive_errors));
  1111. seq_printf(m, "flag_fetches: %u\n",
  1112. ssif_get_stat(ssif_info, flag_fetches));
  1113. seq_printf(m, "hosed: %u\n",
  1114. ssif_get_stat(ssif_info, hosed));
  1115. seq_printf(m, "events: %u\n",
  1116. ssif_get_stat(ssif_info, events));
  1117. seq_printf(m, "watchdog_pretimeouts: %u\n",
  1118. ssif_get_stat(ssif_info, watchdog_pretimeouts));
  1119. seq_printf(m, "alerts: %u\n",
  1120. ssif_get_stat(ssif_info, alerts));
  1121. return 0;
  1122. }
  1123. static int smi_stats_proc_open(struct inode *inode, struct file *file)
  1124. {
  1125. return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
  1126. }
  1127. static const struct file_operations smi_stats_proc_ops = {
  1128. .open = smi_stats_proc_open,
  1129. .read = seq_read,
  1130. .llseek = seq_lseek,
  1131. .release = single_release,
  1132. };
  1133. static int strcmp_nospace(char *s1, char *s2)
  1134. {
  1135. while (*s1 && *s2) {
  1136. while (isspace(*s1))
  1137. s1++;
  1138. while (isspace(*s2))
  1139. s2++;
  1140. if (*s1 > *s2)
  1141. return 1;
  1142. if (*s1 < *s2)
  1143. return -1;
  1144. s1++;
  1145. s2++;
  1146. }
  1147. return 0;
  1148. }
  1149. static struct ssif_addr_info *ssif_info_find(unsigned short addr,
  1150. char *adapter_name,
  1151. bool match_null_name)
  1152. {
  1153. struct ssif_addr_info *info, *found = NULL;
  1154. restart:
  1155. list_for_each_entry(info, &ssif_infos, link) {
  1156. if (info->binfo.addr == addr) {
  1157. if (info->adapter_name || adapter_name) {
  1158. if (!info->adapter_name != !adapter_name) {
  1159. /* One is NULL and one is not */
  1160. continue;
  1161. }
  1162. if (adapter_name &&
  1163. strcmp_nospace(info->adapter_name,
  1164. adapter_name))
  1165. /* Names do not match */
  1166. continue;
  1167. }
  1168. found = info;
  1169. break;
  1170. }
  1171. }
  1172. if (!found && match_null_name) {
  1173. /* Try to get an exact match first, then try with a NULL name */
  1174. adapter_name = NULL;
  1175. match_null_name = false;
  1176. goto restart;
  1177. }
  1178. return found;
  1179. }
  1180. static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
  1181. {
  1182. #ifdef CONFIG_ACPI
  1183. acpi_handle acpi_handle;
  1184. acpi_handle = ACPI_HANDLE(dev);
  1185. if (acpi_handle) {
  1186. ssif_info->addr_source = SI_ACPI;
  1187. ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
  1188. return true;
  1189. }
  1190. #endif
  1191. return false;
  1192. }
  1193. /*
  1194. * Global enables we care about.
  1195. */
  1196. #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
  1197. IPMI_BMC_EVT_MSG_INTR)
  1198. static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1199. {
  1200. unsigned char msg[3];
  1201. unsigned char *resp;
  1202. struct ssif_info *ssif_info;
  1203. int rv = 0;
  1204. int len;
  1205. int i;
  1206. u8 slave_addr = 0;
  1207. struct ssif_addr_info *addr_info = NULL;
  1208. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  1209. if (!resp)
  1210. return -ENOMEM;
  1211. ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
  1212. if (!ssif_info) {
  1213. kfree(resp);
  1214. return -ENOMEM;
  1215. }
  1216. if (!check_acpi(ssif_info, &client->dev)) {
  1217. addr_info = ssif_info_find(client->addr, client->adapter->name,
  1218. true);
  1219. if (!addr_info) {
  1220. /* Must have come in through sysfs. */
  1221. ssif_info->addr_source = SI_HOTMOD;
  1222. } else {
  1223. ssif_info->addr_source = addr_info->addr_src;
  1224. ssif_info->ssif_debug = addr_info->debug;
  1225. ssif_info->addr_info = addr_info->addr_info;
  1226. slave_addr = addr_info->slave_addr;
  1227. }
  1228. }
  1229. pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
  1230. ipmi_addr_src_to_str(ssif_info->addr_source),
  1231. client->addr, client->adapter->name, slave_addr);
  1232. /*
  1233. * Do a Get Device ID command, since it comes back with some
  1234. * useful info.
  1235. */
  1236. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1237. msg[1] = IPMI_GET_DEVICE_ID_CMD;
  1238. rv = do_cmd(client, 2, msg, &len, resp);
  1239. if (rv)
  1240. goto out;
  1241. rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
  1242. if (rv)
  1243. goto out;
  1244. ssif_info->client = client;
  1245. i2c_set_clientdata(client, ssif_info);
  1246. /* Now check for system interface capabilities */
  1247. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1248. msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
  1249. msg[2] = 0; /* SSIF */
  1250. rv = do_cmd(client, 3, msg, &len, resp);
  1251. if (!rv && (len >= 3) && (resp[2] == 0)) {
  1252. if (len < 7) {
  1253. if (ssif_dbg_probe)
  1254. pr_info(PFX "SSIF info too short: %d\n", len);
  1255. goto no_support;
  1256. }
  1257. /* Got a good SSIF response, handle it. */
  1258. ssif_info->max_xmit_msg_size = resp[5];
  1259. ssif_info->max_recv_msg_size = resp[6];
  1260. ssif_info->multi_support = (resp[4] >> 6) & 0x3;
  1261. ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
  1262. /* Sanitize the data */
  1263. switch (ssif_info->multi_support) {
  1264. case SSIF_NO_MULTI:
  1265. if (ssif_info->max_xmit_msg_size > 32)
  1266. ssif_info->max_xmit_msg_size = 32;
  1267. if (ssif_info->max_recv_msg_size > 32)
  1268. ssif_info->max_recv_msg_size = 32;
  1269. break;
  1270. case SSIF_MULTI_2_PART:
  1271. if (ssif_info->max_xmit_msg_size > 63)
  1272. ssif_info->max_xmit_msg_size = 63;
  1273. if (ssif_info->max_recv_msg_size > 62)
  1274. ssif_info->max_recv_msg_size = 62;
  1275. break;
  1276. case SSIF_MULTI_n_PART:
  1277. /*
  1278. * The specification is rather confusing at
  1279. * this point, but I think I understand what
  1280. * is meant. At least I have a workable
  1281. * solution. With multi-part messages, you
  1282. * cannot send a message that is a multiple of
  1283. * 32-bytes in length, because the start and
  1284. * middle messages are 32-bytes and the end
  1285. * message must be at least one byte. You
  1286. * can't fudge on an extra byte, that would
  1287. * screw up things like fru data writes. So
  1288. * we limit the length to 63 bytes. That way
  1289. * a 32-byte message gets sent as a single
  1290. * part. A larger message will be a 32-byte
  1291. * start and the next message is always going
  1292. * to be 1-31 bytes in length. Not ideal, but
  1293. * it should work.
  1294. */
  1295. if (ssif_info->max_xmit_msg_size > 63)
  1296. ssif_info->max_xmit_msg_size = 63;
  1297. break;
  1298. default:
  1299. /* Data is not sane, just give up. */
  1300. goto no_support;
  1301. }
  1302. } else {
  1303. no_support:
  1304. /* Assume no multi-part or PEC support */
  1305. pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
  1306. rv, len, resp[2]);
  1307. ssif_info->max_xmit_msg_size = 32;
  1308. ssif_info->max_recv_msg_size = 32;
  1309. ssif_info->multi_support = SSIF_NO_MULTI;
  1310. ssif_info->supports_pec = 0;
  1311. }
  1312. /* Make sure the NMI timeout is cleared. */
  1313. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1314. msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
  1315. msg[2] = WDT_PRE_TIMEOUT_INT;
  1316. rv = do_cmd(client, 3, msg, &len, resp);
  1317. if (rv || (len < 3) || (resp[2] != 0))
  1318. pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
  1319. rv, len, resp[2]);
  1320. /* Attempt to enable the event buffer. */
  1321. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1322. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  1323. rv = do_cmd(client, 2, msg, &len, resp);
  1324. if (rv || (len < 4) || (resp[2] != 0)) {
  1325. pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
  1326. rv, len, resp[2]);
  1327. rv = 0; /* Not fatal */
  1328. goto found;
  1329. }
  1330. ssif_info->global_enables = resp[3];
  1331. if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
  1332. ssif_info->has_event_buffer = true;
  1333. /* buffer is already enabled, nothing to do. */
  1334. goto found;
  1335. }
  1336. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1337. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  1338. msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
  1339. rv = do_cmd(client, 3, msg, &len, resp);
  1340. if (rv || (len < 2)) {
  1341. pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
  1342. rv, len, resp[2]);
  1343. rv = 0; /* Not fatal */
  1344. goto found;
  1345. }
  1346. if (resp[2] == 0) {
  1347. /* A successful return means the event buffer is supported. */
  1348. ssif_info->has_event_buffer = true;
  1349. ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
  1350. }
  1351. /* Some systems don't behave well if you enable alerts. */
  1352. if (alerts_broken)
  1353. goto found;
  1354. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1355. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  1356. msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
  1357. rv = do_cmd(client, 3, msg, &len, resp);
  1358. if (rv || (len < 2)) {
  1359. pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
  1360. rv, len, resp[2]);
  1361. rv = 0; /* Not fatal */
  1362. goto found;
  1363. }
  1364. if (resp[2] == 0) {
  1365. /* A successful return means the alert is supported. */
  1366. ssif_info->supports_alert = true;
  1367. ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
  1368. }
  1369. found:
  1370. ssif_info->intf_num = atomic_inc_return(&next_intf);
  1371. if (ssif_dbg_probe) {
  1372. pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
  1373. client->addr);
  1374. }
  1375. spin_lock_init(&ssif_info->lock);
  1376. ssif_info->ssif_state = SSIF_NORMAL;
  1377. init_timer(&ssif_info->retry_timer);
  1378. ssif_info->retry_timer.data = (unsigned long) ssif_info;
  1379. ssif_info->retry_timer.function = retry_timeout;
  1380. for (i = 0; i < SSIF_NUM_STATS; i++)
  1381. atomic_set(&ssif_info->stats[i], 0);
  1382. if (ssif_info->supports_pec)
  1383. ssif_info->client->flags |= I2C_CLIENT_PEC;
  1384. ssif_info->handlers.owner = THIS_MODULE;
  1385. ssif_info->handlers.start_processing = ssif_start_processing;
  1386. ssif_info->handlers.get_smi_info = get_smi_info;
  1387. ssif_info->handlers.sender = sender;
  1388. ssif_info->handlers.request_events = request_events;
  1389. ssif_info->handlers.inc_usecount = inc_usecount;
  1390. ssif_info->handlers.dec_usecount = dec_usecount;
  1391. {
  1392. unsigned int thread_num;
  1393. thread_num = ((ssif_info->client->adapter->nr << 8) |
  1394. ssif_info->client->addr);
  1395. init_completion(&ssif_info->wake_thread);
  1396. ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
  1397. "kssif%4.4x", thread_num);
  1398. if (IS_ERR(ssif_info->thread)) {
  1399. rv = PTR_ERR(ssif_info->thread);
  1400. dev_notice(&ssif_info->client->dev,
  1401. "Could not start kernel thread: error %d\n",
  1402. rv);
  1403. goto out;
  1404. }
  1405. }
  1406. rv = ipmi_register_smi(&ssif_info->handlers,
  1407. ssif_info,
  1408. &ssif_info->device_id,
  1409. &ssif_info->client->dev,
  1410. slave_addr);
  1411. if (rv) {
  1412. pr_err(PFX "Unable to register device: error %d\n", rv);
  1413. goto out;
  1414. }
  1415. rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
  1416. &smi_type_proc_ops,
  1417. ssif_info);
  1418. if (rv) {
  1419. pr_err(PFX "Unable to create proc entry: %d\n", rv);
  1420. goto out_err_unreg;
  1421. }
  1422. rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
  1423. &smi_stats_proc_ops,
  1424. ssif_info);
  1425. if (rv) {
  1426. pr_err(PFX "Unable to create proc entry: %d\n", rv);
  1427. goto out_err_unreg;
  1428. }
  1429. out:
  1430. if (rv)
  1431. kfree(ssif_info);
  1432. kfree(resp);
  1433. return rv;
  1434. out_err_unreg:
  1435. ipmi_unregister_smi(ssif_info->intf);
  1436. goto out;
  1437. }
  1438. static int ssif_adapter_handler(struct device *adev, void *opaque)
  1439. {
  1440. struct ssif_addr_info *addr_info = opaque;
  1441. if (adev->type != &i2c_adapter_type)
  1442. return 0;
  1443. i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
  1444. if (!addr_info->adapter_name)
  1445. return 1; /* Only try the first I2C adapter by default. */
  1446. return 0;
  1447. }
  1448. static int new_ssif_client(int addr, char *adapter_name,
  1449. int debug, int slave_addr,
  1450. enum ipmi_addr_src addr_src)
  1451. {
  1452. struct ssif_addr_info *addr_info;
  1453. int rv = 0;
  1454. mutex_lock(&ssif_infos_mutex);
  1455. if (ssif_info_find(addr, adapter_name, false)) {
  1456. rv = -EEXIST;
  1457. goto out_unlock;
  1458. }
  1459. addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
  1460. if (!addr_info) {
  1461. rv = -ENOMEM;
  1462. goto out_unlock;
  1463. }
  1464. if (adapter_name) {
  1465. addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
  1466. if (!addr_info->adapter_name) {
  1467. kfree(addr_info);
  1468. rv = -ENOMEM;
  1469. goto out_unlock;
  1470. }
  1471. }
  1472. strncpy(addr_info->binfo.type, DEVICE_NAME,
  1473. sizeof(addr_info->binfo.type));
  1474. addr_info->binfo.addr = addr;
  1475. addr_info->binfo.platform_data = addr_info;
  1476. addr_info->debug = debug;
  1477. addr_info->slave_addr = slave_addr;
  1478. addr_info->addr_src = addr_src;
  1479. list_add_tail(&addr_info->link, &ssif_infos);
  1480. if (initialized)
  1481. i2c_for_each_dev(addr_info, ssif_adapter_handler);
  1482. /* Otherwise address list will get it */
  1483. out_unlock:
  1484. mutex_unlock(&ssif_infos_mutex);
  1485. return rv;
  1486. }
  1487. static void free_ssif_clients(void)
  1488. {
  1489. struct ssif_addr_info *info, *tmp;
  1490. mutex_lock(&ssif_infos_mutex);
  1491. list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
  1492. list_del(&info->link);
  1493. kfree(info->adapter_name);
  1494. kfree(info);
  1495. }
  1496. mutex_unlock(&ssif_infos_mutex);
  1497. }
  1498. static unsigned short *ssif_address_list(void)
  1499. {
  1500. struct ssif_addr_info *info;
  1501. unsigned int count = 0, i;
  1502. unsigned short *address_list;
  1503. list_for_each_entry(info, &ssif_infos, link)
  1504. count++;
  1505. address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
  1506. if (!address_list)
  1507. return NULL;
  1508. i = 0;
  1509. list_for_each_entry(info, &ssif_infos, link) {
  1510. unsigned short addr = info->binfo.addr;
  1511. int j;
  1512. for (j = 0; j < i; j++) {
  1513. if (address_list[j] == addr)
  1514. goto skip_addr;
  1515. }
  1516. address_list[i] = addr;
  1517. skip_addr:
  1518. i++;
  1519. }
  1520. address_list[i] = I2C_CLIENT_END;
  1521. return address_list;
  1522. }
  1523. #ifdef CONFIG_ACPI
  1524. static const struct acpi_device_id ssif_acpi_match[] = {
  1525. { "IPI0001", 0 },
  1526. { },
  1527. };
  1528. MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
  1529. /*
  1530. * Once we get an ACPI failure, we don't try any more, because we go
  1531. * through the tables sequentially. Once we don't find a table, there
  1532. * are no more.
  1533. */
  1534. static int acpi_failure;
  1535. /*
  1536. * Defined in the IPMI 2.0 spec.
  1537. */
  1538. struct SPMITable {
  1539. s8 Signature[4];
  1540. u32 Length;
  1541. u8 Revision;
  1542. u8 Checksum;
  1543. s8 OEMID[6];
  1544. s8 OEMTableID[8];
  1545. s8 OEMRevision[4];
  1546. s8 CreatorID[4];
  1547. s8 CreatorRevision[4];
  1548. u8 InterfaceType;
  1549. u8 IPMIlegacy;
  1550. s16 SpecificationRevision;
  1551. /*
  1552. * Bit 0 - SCI interrupt supported
  1553. * Bit 1 - I/O APIC/SAPIC
  1554. */
  1555. u8 InterruptType;
  1556. /*
  1557. * If bit 0 of InterruptType is set, then this is the SCI
  1558. * interrupt in the GPEx_STS register.
  1559. */
  1560. u8 GPE;
  1561. s16 Reserved;
  1562. /*
  1563. * If bit 1 of InterruptType is set, then this is the I/O
  1564. * APIC/SAPIC interrupt.
  1565. */
  1566. u32 GlobalSystemInterrupt;
  1567. /* The actual register address. */
  1568. struct acpi_generic_address addr;
  1569. u8 UID[4];
  1570. s8 spmi_id[1]; /* A '\0' terminated array starts here. */
  1571. };
  1572. static int try_init_spmi(struct SPMITable *spmi)
  1573. {
  1574. unsigned short myaddr;
  1575. if (num_addrs >= MAX_SSIF_BMCS)
  1576. return -1;
  1577. if (spmi->IPMIlegacy != 1) {
  1578. pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
  1579. return -ENODEV;
  1580. }
  1581. if (spmi->InterfaceType != 4)
  1582. return -ENODEV;
  1583. if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
  1584. pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
  1585. spmi->addr.space_id);
  1586. return -EIO;
  1587. }
  1588. myaddr = spmi->addr.address & 0x7f;
  1589. return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI);
  1590. }
  1591. static void spmi_find_bmc(void)
  1592. {
  1593. acpi_status status;
  1594. struct SPMITable *spmi;
  1595. int i;
  1596. if (acpi_disabled)
  1597. return;
  1598. if (acpi_failure)
  1599. return;
  1600. for (i = 0; ; i++) {
  1601. status = acpi_get_table(ACPI_SIG_SPMI, i+1,
  1602. (struct acpi_table_header **)&spmi);
  1603. if (status != AE_OK)
  1604. return;
  1605. try_init_spmi(spmi);
  1606. }
  1607. }
  1608. #else
  1609. static void spmi_find_bmc(void) { }
  1610. #endif
  1611. #ifdef CONFIG_DMI
  1612. static int decode_dmi(const struct dmi_device *dmi_dev)
  1613. {
  1614. struct dmi_header *dm = dmi_dev->device_data;
  1615. u8 *data = (u8 *) dm;
  1616. u8 len = dm->length;
  1617. unsigned short myaddr;
  1618. int slave_addr;
  1619. if (num_addrs >= MAX_SSIF_BMCS)
  1620. return -1;
  1621. if (len < 9)
  1622. return -1;
  1623. if (data[0x04] != 4) /* Not SSIF */
  1624. return -1;
  1625. if ((data[8] >> 1) == 0) {
  1626. /*
  1627. * Some broken systems put the I2C address in
  1628. * the slave address field. We try to
  1629. * accommodate them here.
  1630. */
  1631. myaddr = data[6] >> 1;
  1632. slave_addr = 0;
  1633. } else {
  1634. myaddr = data[8] >> 1;
  1635. slave_addr = data[6];
  1636. }
  1637. return new_ssif_client(myaddr, NULL, 0, 0, SI_SMBIOS);
  1638. }
  1639. static void dmi_iterator(void)
  1640. {
  1641. const struct dmi_device *dev = NULL;
  1642. while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
  1643. decode_dmi(dev);
  1644. }
  1645. #else
  1646. static void dmi_iterator(void) { }
  1647. #endif
  1648. static const struct i2c_device_id ssif_id[] = {
  1649. { DEVICE_NAME, 0 },
  1650. { }
  1651. };
  1652. MODULE_DEVICE_TABLE(i2c, ssif_id);
  1653. static struct i2c_driver ssif_i2c_driver = {
  1654. .class = I2C_CLASS_HWMON,
  1655. .driver = {
  1656. .name = DEVICE_NAME
  1657. },
  1658. .probe = ssif_probe,
  1659. .remove = ssif_remove,
  1660. .alert = ssif_alert,
  1661. .id_table = ssif_id,
  1662. .detect = ssif_detect
  1663. };
  1664. static int init_ipmi_ssif(void)
  1665. {
  1666. int i;
  1667. int rv;
  1668. if (initialized)
  1669. return 0;
  1670. pr_info("IPMI SSIF Interface driver\n");
  1671. /* build list for i2c from addr list */
  1672. for (i = 0; i < num_addrs; i++) {
  1673. rv = new_ssif_client(addr[i], adapter_name[i],
  1674. dbg[i], slave_addrs[i],
  1675. SI_HARDCODED);
  1676. if (rv)
  1677. pr_err(PFX
  1678. "Couldn't add hardcoded device at addr 0x%x\n",
  1679. addr[i]);
  1680. }
  1681. if (ssif_tryacpi)
  1682. ssif_i2c_driver.driver.acpi_match_table =
  1683. ACPI_PTR(ssif_acpi_match);
  1684. if (ssif_trydmi)
  1685. dmi_iterator();
  1686. if (ssif_tryacpi)
  1687. spmi_find_bmc();
  1688. ssif_i2c_driver.address_list = ssif_address_list();
  1689. rv = i2c_add_driver(&ssif_i2c_driver);
  1690. if (!rv)
  1691. initialized = true;
  1692. return rv;
  1693. }
  1694. module_init(init_ipmi_ssif);
  1695. static void cleanup_ipmi_ssif(void)
  1696. {
  1697. if (!initialized)
  1698. return;
  1699. initialized = false;
  1700. i2c_del_driver(&ssif_i2c_driver);
  1701. free_ssif_clients();
  1702. }
  1703. module_exit(cleanup_ipmi_ssif);
  1704. MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
  1705. MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
  1706. MODULE_LICENSE("GPL");