ipmi_ssif.c 52 KB

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