ipmi_ssif.c 51 KB

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