ipmi_ssif.c 51 KB

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