ipmi_ssif.c 48 KB

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