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