ipmi_ssif.c 55 KB

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