ipmi_ssif.c 55 KB

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