ipmi_ssif.c 51 KB

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