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