ipmi_ssif.c 49 KB

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