ipmi_ssif.c 55 KB

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