ipmi_ssif.c 51 KB

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