hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/char/ipmi/ipmi_ssif.c
....@@ -22,10 +22,8 @@
2222 * and drives the real SSIF state machine.
2323 */
2424
25
-/*
26
- * TODO: Figure out how to use SMB alerts. This will require a new
27
- * interface into the I2C driver, I believe.
28
- */
25
+#define pr_fmt(fmt) "ipmi_ssif: " fmt
26
+#define dev_fmt(fmt) "ipmi_ssif: " fmt
2927
3028 #if defined(MODVERSIONS)
3129 #include <linux/modversions.h>
....@@ -49,10 +47,8 @@
4947 #include <linux/acpi.h>
5048 #include <linux/ctype.h>
5149 #include <linux/time64.h>
52
-#include "ipmi_si_sm.h"
5350 #include "ipmi_dmi.h"
5451
55
-#define PFX "ipmi_ssif: "
5652 #define DEVICE_NAME "ipmi_ssif"
5753
5854 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
....@@ -60,6 +56,7 @@
6056 #define SSIF_IPMI_REQUEST 2
6157 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
6258 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
59
+#define SSIF_IPMI_MULTI_PART_REQUEST_END 8
6360 #define SSIF_IPMI_RESPONSE 3
6461 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
6562
....@@ -87,6 +84,12 @@
8784 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
8885 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
8986 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
87
+
88
+/*
89
+ * Timeout for the watch, only used for get flag timer.
90
+ */
91
+#define SSIF_WATCH_MSG_TIMEOUT msecs_to_jiffies(10)
92
+#define SSIF_WATCH_WATCHDOG_TIMEOUT msecs_to_jiffies(250)
9093
9194 enum ssif_intf_state {
9295 SSIF_NORMAL,
....@@ -181,8 +184,6 @@
181184 struct device *dev;
182185 struct i2c_client *client;
183186
184
- struct i2c_client *added_client;
185
-
186187 struct mutex clients_mutex;
187188 struct list_head clients;
188189
....@@ -268,9 +269,13 @@
268269 struct timer_list retry_timer;
269270 int retries_left;
270271
272
+ long watch_timeout; /* Timeout for flags check, 0 if off. */
273
+ struct timer_list watch_timer; /* Flag fetch timer. */
274
+
271275 /* Info from SSIF cmd */
272276 unsigned char max_xmit_msg_size;
273277 unsigned char max_recv_msg_size;
278
+ bool cmd8_works; /* See test_multipart_messages() for details. */
274279 unsigned int multi_support;
275280 int supports_pec;
276281
....@@ -290,6 +295,7 @@
290295 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
291296
292297 static bool initialized;
298
+static bool platform_registered;
293299
294300 static void return_hosed_msg(struct ssif_info *ssif_info,
295301 struct ipmi_smi_msg *msg);
....@@ -300,6 +306,7 @@
300306
301307 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
302308 unsigned long *flags)
309
+ __acquires(&ssif_info->lock)
303310 {
304311 spin_lock_irqsave(&ssif_info->lock, *flags);
305312 return flags;
....@@ -307,6 +314,7 @@
307314
308315 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
309316 unsigned long *flags)
317
+ __releases(&ssif_info->lock)
310318 {
311319 spin_unlock_irqrestore(&ssif_info->lock, *flags);
312320 }
....@@ -316,9 +324,9 @@
316324 {
317325 if (msg->rsp_size < 0) {
318326 return_hosed_msg(ssif_info, msg);
319
- pr_err(PFX
320
- "Malformed message in deliver_recv_msg: rsp_size = %d\n",
321
- msg->rsp_size);
327
+ dev_err(&ssif_info->client->dev,
328
+ "%s: Malformed message: rsp_size = %d\n",
329
+ __func__, msg->rsp_size);
322330 } else {
323331 ipmi_smi_msg_received(ssif_info->intf, msg);
324332 }
....@@ -534,7 +542,8 @@
534542 if (rv < 0) {
535543 /* request failed, just return the error. */
536544 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
537
- pr_info("Error from i2c_non_blocking_op(5)\n");
545
+ dev_dbg(&ssif_info->client->dev,
546
+ "Error from i2c_non_blocking_op(5)\n");
538547
539548 msg_done_handler(ssif_info, -EIO, NULL, 0);
540549 }
....@@ -558,6 +567,26 @@
558567 start_get(ssif_info);
559568 }
560569
570
+static void watch_timeout(struct timer_list *t)
571
+{
572
+ struct ssif_info *ssif_info = from_timer(ssif_info, t, watch_timer);
573
+ unsigned long oflags, *flags;
574
+
575
+ if (ssif_info->stopping)
576
+ return;
577
+
578
+ flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
579
+ if (ssif_info->watch_timeout) {
580
+ mod_timer(&ssif_info->watch_timer,
581
+ jiffies + ssif_info->watch_timeout);
582
+ if (SSIF_IDLE(ssif_info)) {
583
+ start_flag_fetch(ssif_info, flags); /* Releases lock */
584
+ return;
585
+ }
586
+ ssif_info->req_flags = true;
587
+ }
588
+ ipmi_ssif_unlock_cond(ssif_info, flags);
589
+}
561590
562591 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
563592 unsigned int data)
....@@ -616,7 +645,8 @@
616645 ssif_inc_stat(ssif_info, receive_errors);
617646
618647 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
619
- pr_info("Error in msg_done_handler: %d\n", result);
648
+ dev_dbg(&ssif_info->client->dev,
649
+ "%s: Error %d\n", __func__, result);
620650 len = 0;
621651 goto continue_op;
622652 }
....@@ -641,7 +671,8 @@
641671 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
642672 if (rv < 0) {
643673 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
644
- pr_info("Error from i2c_non_blocking_op(1)\n");
674
+ dev_dbg(&ssif_info->client->dev,
675
+ "Error from i2c_non_blocking_op(1)\n");
645676
646677 result = -EIO;
647678 } else
....@@ -654,7 +685,8 @@
654685 if (len == 0) {
655686 result = -EIO;
656687 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
657
- pr_info(PFX "Middle message with no data\n");
688
+ dev_dbg(&ssif_info->client->dev,
689
+ "Middle message with no data\n");
658690
659691 goto continue_op;
660692 }
....@@ -667,7 +699,8 @@
667699 /* All blocks but the last must have 31 data bytes. */
668700 result = -EIO;
669701 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
670
- pr_info("Received middle message <31\n");
702
+ dev_dbg(&ssif_info->client->dev,
703
+ "Received middle message <31\n");
671704
672705 goto continue_op;
673706 }
....@@ -676,7 +709,8 @@
676709 /* Received message too big, abort the operation. */
677710 result = -E2BIG;
678711 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
679
- pr_info("Received message too big\n");
712
+ dev_dbg(&ssif_info->client->dev,
713
+ "Received message too big\n");
680714
681715 goto continue_op;
682716 }
....@@ -711,7 +745,7 @@
711745 I2C_SMBUS_BLOCK_DATA);
712746 if (rv < 0) {
713747 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
714
- pr_info(PFX
748
+ dev_dbg(&ssif_info->client->dev,
715749 "Error from ssif_i2c_send\n");
716750
717751 result = -EIO;
....@@ -729,7 +763,8 @@
729763 }
730764
731765 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
732
- pr_info(PFX "DONE 1: state = %d, result=%d.\n",
766
+ dev_dbg(&ssif_info->client->dev,
767
+ "DONE 1: state = %d, result=%d\n",
733768 ssif_info->ssif_state, result);
734769
735770 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
....@@ -767,8 +802,9 @@
767802 */
768803 ssif_info->ssif_state = SSIF_NORMAL;
769804 ipmi_ssif_unlock_cond(ssif_info, flags);
770
- pr_warn(PFX "Error getting flags: %d %d, %x\n",
771
- result, len, (len >= 3) ? data[2] : 0);
805
+ dev_warn(&ssif_info->client->dev,
806
+ "Error getting flags: %d %d, %x\n",
807
+ result, len, (len >= 3) ? data[2] : 0);
772808 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
773809 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
774810 /*
....@@ -776,8 +812,9 @@
776812 * response to a previous command.
777813 */
778814 ipmi_ssif_unlock_cond(ssif_info, flags);
779
- pr_warn(PFX "Invalid response getting flags: %x %x\n",
780
- data[0], data[1]);
815
+ dev_warn(&ssif_info->client->dev,
816
+ "Invalid response getting flags: %x %x\n",
817
+ data[0], data[1]);
781818 } else {
782819 ssif_inc_stat(ssif_info, flag_fetches);
783820 ssif_info->msg_flags = data[3];
....@@ -789,18 +826,28 @@
789826 /* We cleared the flags. */
790827 if ((result < 0) || (len < 3) || (data[2] != 0)) {
791828 /* Error clearing flags */
792
- pr_warn(PFX "Error clearing flags: %d %d, %x\n",
793
- result, len, (len >= 3) ? data[2] : 0);
829
+ dev_warn(&ssif_info->client->dev,
830
+ "Error clearing flags: %d %d, %x\n",
831
+ result, len, (len >= 3) ? data[2] : 0);
794832 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
795833 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
796
- pr_warn(PFX "Invalid response clearing flags: %x %x\n",
797
- data[0], data[1]);
834
+ dev_warn(&ssif_info->client->dev,
835
+ "Invalid response clearing flags: %x %x\n",
836
+ data[0], data[1]);
798837 }
799838 ssif_info->ssif_state = SSIF_NORMAL;
800839 ipmi_ssif_unlock_cond(ssif_info, flags);
801840 break;
802841
803842 case SSIF_GETTING_EVENTS:
843
+ if (!msg) {
844
+ /* Should never happen, but just in case. */
845
+ dev_warn(&ssif_info->client->dev,
846
+ "No message set while getting events\n");
847
+ ipmi_ssif_unlock_cond(ssif_info, flags);
848
+ break;
849
+ }
850
+
804851 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
805852 /* Error getting event, probably done. */
806853 msg->done(msg);
....@@ -810,8 +857,9 @@
810857 handle_flags(ssif_info, flags);
811858 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
812859 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
813
- pr_warn(PFX "Invalid response getting events: %x %x\n",
814
- msg->rsp[0], msg->rsp[1]);
860
+ dev_warn(&ssif_info->client->dev,
861
+ "Invalid response getting events: %x %x\n",
862
+ msg->rsp[0], msg->rsp[1]);
815863 msg->done(msg);
816864 /* Take off the event flag. */
817865 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
....@@ -824,6 +872,14 @@
824872 break;
825873
826874 case SSIF_GETTING_MESSAGES:
875
+ if (!msg) {
876
+ /* Should never happen, but just in case. */
877
+ dev_warn(&ssif_info->client->dev,
878
+ "No message set while getting messages\n");
879
+ ipmi_ssif_unlock_cond(ssif_info, flags);
880
+ break;
881
+ }
882
+
827883 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
828884 /* Error getting event, probably done. */
829885 msg->done(msg);
....@@ -833,8 +889,9 @@
833889 handle_flags(ssif_info, flags);
834890 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
835891 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
836
- pr_warn(PFX "Invalid response clearing flags: %x %x\n",
837
- msg->rsp[0], msg->rsp[1]);
892
+ dev_warn(&ssif_info->client->dev,
893
+ "Invalid response clearing flags: %x %x\n",
894
+ msg->rsp[0], msg->rsp[1]);
838895 msg->done(msg);
839896
840897 /* Take off the msg flag. */
....@@ -846,6 +903,13 @@
846903 deliver_recv_msg(ssif_info, msg);
847904 }
848905 break;
906
+
907
+ default:
908
+ /* Should never happen, but just in case. */
909
+ dev_warn(&ssif_info->client->dev,
910
+ "Invalid state in message done handling: %d\n",
911
+ ssif_info->ssif_state);
912
+ ipmi_ssif_unlock_cond(ssif_info, flags);
849913 }
850914
851915 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
....@@ -860,7 +924,8 @@
860924 ipmi_ssif_unlock_cond(ssif_info, flags);
861925
862926 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
863
- pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
927
+ dev_dbg(&ssif_info->client->dev,
928
+ "DONE 2: state = %d.\n", ssif_info->ssif_state);
864929 }
865930
866931 static void msg_written_handler(struct ssif_info *ssif_info, int result,
....@@ -880,8 +945,8 @@
880945 ssif_inc_stat(ssif_info, send_errors);
881946
882947 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
883
- pr_info(PFX
884
- "Out of retries in msg_written_handler\n");
948
+ dev_dbg(&ssif_info->client->dev,
949
+ "%s: Out of retries\n", __func__);
885950 msg_done_handler(ssif_info, -EIO, NULL, 0);
886951 return;
887952 }
....@@ -893,7 +958,8 @@
893958 * handle it.
894959 */
895960 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
896
- pr_info("Error in msg_written_handler: %d\n", result);
961
+ dev_dbg(&ssif_info->client->dev,
962
+ "%s: Error %d\n", __func__, result);
897963
898964 msg_done_handler(ssif_info, result, NULL, 0);
899965 return;
....@@ -905,38 +971,40 @@
905971 * in the SSIF_MULTI_n_PART case in the probe function
906972 * for details on the intricacies of this.
907973 */
908
- int left;
974
+ int left, to_write;
909975 unsigned char *data_to_send;
976
+ unsigned char cmd;
910977
911978 ssif_inc_stat(ssif_info, sent_messages_parts);
912979
913980 left = ssif_info->multi_len - ssif_info->multi_pos;
914
- if (left > 32)
915
- left = 32;
981
+ to_write = left;
982
+ if (to_write > 32)
983
+ to_write = 32;
916984 /* Length byte. */
917
- ssif_info->multi_data[ssif_info->multi_pos] = left;
985
+ ssif_info->multi_data[ssif_info->multi_pos] = to_write;
918986 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
919
- ssif_info->multi_pos += left;
920
- if (left < 32)
921
- /*
922
- * Write is finished. Note that we must end
923
- * with a write of less than 32 bytes to
924
- * complete the transaction, even if it is
925
- * zero bytes.
926
- */
987
+ ssif_info->multi_pos += to_write;
988
+ cmd = SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE;
989
+ if (ssif_info->cmd8_works) {
990
+ if (left == to_write) {
991
+ cmd = SSIF_IPMI_MULTI_PART_REQUEST_END;
992
+ ssif_info->multi_data = NULL;
993
+ }
994
+ } else if (to_write < 32) {
927995 ssif_info->multi_data = NULL;
996
+ }
928997
929998 rv = ssif_i2c_send(ssif_info, msg_written_handler,
930
- I2C_SMBUS_WRITE,
931
- SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
932
- data_to_send,
933
- I2C_SMBUS_BLOCK_DATA);
999
+ I2C_SMBUS_WRITE, cmd,
1000
+ data_to_send, I2C_SMBUS_BLOCK_DATA);
9341001 if (rv < 0) {
9351002 /* request failed, just return the error. */
9361003 ssif_inc_stat(ssif_info, send_errors);
9371004
9381005 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
939
- pr_info("Error from i2c_non_blocking_op(3)\n");
1006
+ dev_dbg(&ssif_info->client->dev,
1007
+ "Error from i2c_non_blocking_op(3)\n");
9401008 msg_done_handler(ssif_info, -EIO, NULL, 0);
9411009 }
9421010 } else {
....@@ -992,7 +1060,8 @@
9921060 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
9931061 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
9941062 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
995
- pr_info("Error from i2c_non_blocking_op(4)\n");
1063
+ dev_dbg(&ssif_info->client->dev,
1064
+ "Error from i2c_non_blocking_op(4)\n");
9961065 return rv;
9971066 }
9981067
....@@ -1061,9 +1130,10 @@
10611130 struct timespec64 t;
10621131
10631132 ktime_get_real_ts64(&t);
1064
- pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1065
- msg->data[0], msg->data[1],
1066
- (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
1133
+ dev_dbg(&ssif_info->client->dev,
1134
+ "**Enqueue %02x %02x: %lld.%6.6ld\n",
1135
+ msg->data[0], msg->data[1],
1136
+ (long long)t.tv_sec, (long)t.tv_nsec / NSEC_PER_USEC);
10671137 }
10681138 }
10691139
....@@ -1080,8 +1150,7 @@
10801150 }
10811151
10821152 /*
1083
- * Instead of having our own timer to periodically check the message
1084
- * flags, we let the message handler drive us.
1153
+ * Upper layer wants us to request events.
10851154 */
10861155 static void request_events(void *send_info)
10871156 {
....@@ -1092,18 +1161,33 @@
10921161 return;
10931162
10941163 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1095
- /*
1096
- * Request flags first, not events, because the lower layer
1097
- * doesn't have a way to send an attention. But make sure
1098
- * event checking still happens.
1099
- */
11001164 ssif_info->req_events = true;
1101
- if (SSIF_IDLE(ssif_info))
1102
- start_flag_fetch(ssif_info, flags);
1103
- else {
1104
- ssif_info->req_flags = true;
1105
- ipmi_ssif_unlock_cond(ssif_info, flags);
1165
+ ipmi_ssif_unlock_cond(ssif_info, flags);
1166
+}
1167
+
1168
+/*
1169
+ * Upper layer is changing the flag saying whether we need to request
1170
+ * flags periodically or not.
1171
+ */
1172
+static void ssif_set_need_watch(void *send_info, unsigned int watch_mask)
1173
+{
1174
+ struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1175
+ unsigned long oflags, *flags;
1176
+ long timeout = 0;
1177
+
1178
+ if (watch_mask & IPMI_WATCH_MASK_CHECK_MESSAGES)
1179
+ timeout = SSIF_WATCH_MSG_TIMEOUT;
1180
+ else if (watch_mask)
1181
+ timeout = SSIF_WATCH_WATCHDOG_TIMEOUT;
1182
+
1183
+ flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1184
+ if (timeout != ssif_info->watch_timeout) {
1185
+ ssif_info->watch_timeout = timeout;
1186
+ if (ssif_info->watch_timeout)
1187
+ mod_timer(&ssif_info->watch_timer,
1188
+ jiffies + ssif_info->watch_timeout);
11061189 }
1190
+ ipmi_ssif_unlock_cond(ssif_info, flags);
11071191 }
11081192
11091193 static int ssif_start_processing(void *send_info,
....@@ -1230,6 +1314,7 @@
12301314 schedule_timeout(1);
12311315
12321316 ssif_info->stopping = true;
1317
+ del_timer_sync(&ssif_info->watch_timer);
12331318 del_timer_sync(&ssif_info->retry_timer);
12341319 if (ssif_info->thread) {
12351320 complete(&ssif_info->wake_thread);
....@@ -1263,6 +1348,24 @@
12631348 return 0;
12641349 }
12651350
1351
+static int read_response(struct i2c_client *client, unsigned char *resp)
1352
+{
1353
+ int ret = -ENODEV, retry_cnt = SSIF_RECV_RETRIES;
1354
+
1355
+ while (retry_cnt > 0) {
1356
+ ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1357
+ resp);
1358
+ if (ret > 0)
1359
+ break;
1360
+ msleep(SSIF_MSG_MSEC);
1361
+ retry_cnt--;
1362
+ if (retry_cnt <= 0)
1363
+ break;
1364
+ }
1365
+
1366
+ return ret;
1367
+}
1368
+
12661369 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
12671370 int *resp_len, unsigned char *resp)
12681371 {
....@@ -1279,26 +1382,16 @@
12791382 return -ENODEV;
12801383 }
12811384
1282
- ret = -ENODEV;
1283
- retry_cnt = SSIF_RECV_RETRIES;
1284
- while (retry_cnt > 0) {
1285
- ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1286
- resp);
1287
- if (ret > 0)
1288
- break;
1289
- msleep(SSIF_MSG_MSEC);
1290
- retry_cnt--;
1291
- if (retry_cnt <= 0)
1292
- break;
1293
- }
1294
-
1385
+ ret = read_response(client, resp);
12951386 if (ret > 0) {
12961387 /* Validate that the response is correct. */
12971388 if (ret < 3 ||
12981389 (resp[0] != (msg[0] | (1 << 2))) ||
12991390 (resp[1] != msg[1]))
13001391 ret = -EINVAL;
1301
- else {
1392
+ else if (ret > IPMI_MAX_MSG_LENGTH) {
1393
+ ret = -E2BIG;
1394
+ } else {
13021395 *resp_len = ret;
13031396 ret = 0;
13041397 }
....@@ -1356,6 +1449,10 @@
13561449 restart:
13571450 list_for_each_entry(info, &ssif_infos, link) {
13581451 if (info->binfo.addr == addr) {
1452
+ if (info->addr_src == SI_SMBIOS)
1453
+ info->adapter_name = kstrdup(adapter_name,
1454
+ GFP_KERNEL);
1455
+
13591456 if (info->adapter_name || adapter_name) {
13601457 if (!info->adapter_name != !adapter_name) {
13611458 /* One is NULL and one is not */
....@@ -1391,6 +1488,7 @@
13911488 if (acpi_handle) {
13921489 ssif_info->addr_source = SI_ACPI;
13931490 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1491
+ request_module("acpi_ipmi");
13941492 return true;
13951493 }
13961494 #endif
....@@ -1410,11 +1508,180 @@
14101508 return slave_addr;
14111509 }
14121510
1511
+static int start_multipart_test(struct i2c_client *client,
1512
+ unsigned char *msg, bool do_middle)
1513
+{
1514
+ int retry_cnt = SSIF_SEND_RETRIES, ret;
1515
+
1516
+retry_write:
1517
+ ret = i2c_smbus_write_block_data(client,
1518
+ SSIF_IPMI_MULTI_PART_REQUEST_START,
1519
+ 32, msg);
1520
+ if (ret) {
1521
+ retry_cnt--;
1522
+ if (retry_cnt > 0)
1523
+ goto retry_write;
1524
+ 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");
1525
+ return ret;
1526
+ }
1527
+
1528
+ if (!do_middle)
1529
+ return 0;
1530
+
1531
+ ret = i2c_smbus_write_block_data(client,
1532
+ SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1533
+ 32, msg + 32);
1534
+ if (ret) {
1535
+ 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");
1536
+ return ret;
1537
+ }
1538
+
1539
+ return 0;
1540
+}
1541
+
1542
+static void test_multipart_messages(struct i2c_client *client,
1543
+ struct ssif_info *ssif_info,
1544
+ unsigned char *resp)
1545
+{
1546
+ unsigned char msg[65];
1547
+ int ret;
1548
+ bool do_middle;
1549
+
1550
+ if (ssif_info->max_xmit_msg_size <= 32)
1551
+ return;
1552
+
1553
+ do_middle = ssif_info->max_xmit_msg_size > 63;
1554
+
1555
+ memset(msg, 0, sizeof(msg));
1556
+ msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1557
+ msg[1] = IPMI_GET_DEVICE_ID_CMD;
1558
+
1559
+ /*
1560
+ * The specification is all messed up dealing with sending
1561
+ * multi-part messages. Per what the specification says, it
1562
+ * is impossible to send a message that is a multiple of 32
1563
+ * bytes, except for 32 itself. It talks about a "start"
1564
+ * transaction (cmd=6) that must be 32 bytes, "middle"
1565
+ * transaction (cmd=7) that must be 32 bytes, and an "end"
1566
+ * transaction. The "end" transaction is shown as cmd=7 in
1567
+ * the text, but if that's the case there is no way to
1568
+ * differentiate between a middle and end part except the
1569
+ * length being less than 32. But there is a table at the far
1570
+ * end of the section (that I had never noticed until someone
1571
+ * pointed it out to me) that mentions it as cmd=8.
1572
+ *
1573
+ * After some thought, I think the example is wrong and the
1574
+ * end transaction should be cmd=8. But some systems don't
1575
+ * implement cmd=8, they use a zero-length end transaction,
1576
+ * even though that violates the SMBus specification.
1577
+ *
1578
+ * So, to work around this, this code tests if cmd=8 works.
1579
+ * If it does, then we use that. If not, it tests zero-
1580
+ * byte end transactions. If that works, good. If not,
1581
+ * we only allow 63-byte transactions max.
1582
+ */
1583
+
1584
+ ret = start_multipart_test(client, msg, do_middle);
1585
+ if (ret)
1586
+ goto out_no_multi_part;
1587
+
1588
+ ret = i2c_smbus_write_block_data(client,
1589
+ SSIF_IPMI_MULTI_PART_REQUEST_END,
1590
+ 1, msg + 64);
1591
+
1592
+ if (!ret)
1593
+ ret = read_response(client, resp);
1594
+
1595
+ if (ret > 0) {
1596
+ /* End transactions work, we are good. */
1597
+ ssif_info->cmd8_works = true;
1598
+ return;
1599
+ }
1600
+
1601
+ ret = start_multipart_test(client, msg, do_middle);
1602
+ if (ret) {
1603
+ dev_err(&client->dev, "Second multipart test failed.\n");
1604
+ goto out_no_multi_part;
1605
+ }
1606
+
1607
+ ret = i2c_smbus_write_block_data(client,
1608
+ SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1609
+ 0, msg + 64);
1610
+ if (!ret)
1611
+ ret = read_response(client, resp);
1612
+ if (ret > 0)
1613
+ /* Zero-size end parts work, use those. */
1614
+ return;
1615
+
1616
+ /* Limit to 63 bytes and use a short middle command to mark the end. */
1617
+ if (ssif_info->max_xmit_msg_size > 63)
1618
+ ssif_info->max_xmit_msg_size = 63;
1619
+ return;
1620
+
1621
+out_no_multi_part:
1622
+ ssif_info->max_xmit_msg_size = 32;
1623
+ return;
1624
+}
1625
+
14131626 /*
14141627 * Global enables we care about.
14151628 */
14161629 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
14171630 IPMI_BMC_EVT_MSG_INTR)
1631
+
1632
+static void ssif_remove_dup(struct i2c_client *client)
1633
+{
1634
+ struct ssif_info *ssif_info = i2c_get_clientdata(client);
1635
+
1636
+ ipmi_unregister_smi(ssif_info->intf);
1637
+ kfree(ssif_info);
1638
+}
1639
+
1640
+static int ssif_add_infos(struct i2c_client *client)
1641
+{
1642
+ struct ssif_addr_info *info;
1643
+
1644
+ info = kzalloc(sizeof(*info), GFP_KERNEL);
1645
+ if (!info)
1646
+ return -ENOMEM;
1647
+ info->addr_src = SI_ACPI;
1648
+ info->client = client;
1649
+ info->adapter_name = kstrdup(client->adapter->name, GFP_KERNEL);
1650
+ info->binfo.addr = client->addr;
1651
+ list_add_tail(&info->link, &ssif_infos);
1652
+ return 0;
1653
+}
1654
+
1655
+/*
1656
+ * Prefer ACPI over SMBIOS, if both are available.
1657
+ * So if we get an ACPI interface and have already registered a SMBIOS
1658
+ * interface at the same address, remove the SMBIOS and add the ACPI one.
1659
+ */
1660
+static int ssif_check_and_remove(struct i2c_client *client,
1661
+ struct ssif_info *ssif_info)
1662
+{
1663
+ struct ssif_addr_info *info;
1664
+
1665
+ list_for_each_entry(info, &ssif_infos, link) {
1666
+ if (!info->client)
1667
+ return 0;
1668
+ if (!strcmp(info->adapter_name, client->adapter->name) &&
1669
+ info->binfo.addr == client->addr) {
1670
+ if (info->addr_src == SI_ACPI)
1671
+ return -EEXIST;
1672
+
1673
+ if (ssif_info->addr_source == SI_ACPI &&
1674
+ info->addr_src == SI_SMBIOS) {
1675
+ dev_info(&client->dev,
1676
+ "Removing %s-specified SSIF interface in favor of ACPI\n",
1677
+ ipmi_addr_src_to_str(info->addr_src));
1678
+ ssif_remove_dup(info->client);
1679
+ return 0;
1680
+ }
1681
+ }
1682
+ }
1683
+ return 0;
1684
+}
14181685
14191686 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
14201687 {
....@@ -1427,13 +1694,17 @@
14271694 u8 slave_addr = 0;
14281695 struct ssif_addr_info *addr_info = NULL;
14291696
1697
+ mutex_lock(&ssif_infos_mutex);
14301698 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1431
- if (!resp)
1699
+ if (!resp) {
1700
+ mutex_unlock(&ssif_infos_mutex);
14321701 return -ENOMEM;
1702
+ }
14331703
14341704 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
14351705 if (!ssif_info) {
14361706 kfree(resp);
1707
+ mutex_unlock(&ssif_infos_mutex);
14371708 return -ENOMEM;
14381709 }
14391710
....@@ -1452,14 +1723,28 @@
14521723 }
14531724 }
14541725
1455
- slave_addr = find_slave_address(client, slave_addr);
1456
-
1457
- pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1458
- ipmi_addr_src_to_str(ssif_info->addr_source),
1459
- client->addr, client->adapter->name, slave_addr);
1460
-
14611726 ssif_info->client = client;
14621727 i2c_set_clientdata(client, ssif_info);
1728
+
1729
+ rv = ssif_check_and_remove(client, ssif_info);
1730
+ /* If rv is 0 and addr source is not SI_ACPI, continue probing */
1731
+ if (!rv && ssif_info->addr_source == SI_ACPI) {
1732
+ rv = ssif_add_infos(client);
1733
+ if (rv) {
1734
+ dev_err(&client->dev, "Out of memory!, exiting ..\n");
1735
+ goto out;
1736
+ }
1737
+ } else if (rv) {
1738
+ dev_err(&client->dev, "Not probing, Interface already present\n");
1739
+ goto out;
1740
+ }
1741
+
1742
+ slave_addr = find_slave_address(client, slave_addr);
1743
+
1744
+ dev_info(&client->dev,
1745
+ "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1746
+ ipmi_addr_src_to_str(ssif_info->addr_source),
1747
+ client->addr, client->adapter->name, slave_addr);
14631748
14641749 /* Now check for system interface capabilities */
14651750 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
....@@ -1469,7 +1754,8 @@
14691754 if (!rv && (len >= 3) && (resp[2] == 0)) {
14701755 if (len < 7) {
14711756 if (ssif_dbg_probe)
1472
- pr_info(PFX "SSIF info too short: %d\n", len);
1757
+ dev_dbg(&ssif_info->client->dev,
1758
+ "SSIF info too short: %d\n", len);
14731759 goto no_support;
14741760 }
14751761
....@@ -1496,26 +1782,7 @@
14961782 break;
14971783
14981784 case SSIF_MULTI_n_PART:
1499
- /*
1500
- * The specification is rather confusing at
1501
- * this point, but I think I understand what
1502
- * is meant. At least I have a workable
1503
- * solution. With multi-part messages, you
1504
- * cannot send a message that is a multiple of
1505
- * 32-bytes in length, because the start and
1506
- * middle messages are 32-bytes and the end
1507
- * message must be at least one byte. You
1508
- * can't fudge on an extra byte, that would
1509
- * screw up things like fru data writes. So
1510
- * we limit the length to 63 bytes. That way
1511
- * a 32-byte message gets sent as a single
1512
- * part. A larger message will be a 32-byte
1513
- * start and the next message is always going
1514
- * to be 1-31 bytes in length. Not ideal, but
1515
- * it should work.
1516
- */
1517
- if (ssif_info->max_xmit_msg_size > 63)
1518
- ssif_info->max_xmit_msg_size = 63;
1785
+ /* We take whatever size given, but do some testing. */
15191786 break;
15201787
15211788 default:
....@@ -1525,8 +1792,9 @@
15251792 } else {
15261793 no_support:
15271794 /* Assume no multi-part or PEC support */
1528
- pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1529
- rv, len, resp[2]);
1795
+ dev_info(&ssif_info->client->dev,
1796
+ "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1797
+ rv, len, resp[2]);
15301798
15311799 ssif_info->max_xmit_msg_size = 32;
15321800 ssif_info->max_recv_msg_size = 32;
....@@ -1534,22 +1802,26 @@
15341802 ssif_info->supports_pec = 0;
15351803 }
15361804
1805
+ test_multipart_messages(client, ssif_info, resp);
1806
+
15371807 /* Make sure the NMI timeout is cleared. */
15381808 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
15391809 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
15401810 msg[2] = WDT_PRE_TIMEOUT_INT;
15411811 rv = do_cmd(client, 3, msg, &len, resp);
15421812 if (rv || (len < 3) || (resp[2] != 0))
1543
- pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1544
- rv, len, resp[2]);
1813
+ dev_warn(&ssif_info->client->dev,
1814
+ "Unable to clear message flags: %d %d %2.2x\n",
1815
+ rv, len, resp[2]);
15451816
15461817 /* Attempt to enable the event buffer. */
15471818 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
15481819 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
15491820 rv = do_cmd(client, 2, msg, &len, resp);
15501821 if (rv || (len < 4) || (resp[2] != 0)) {
1551
- pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1552
- rv, len, resp[2]);
1822
+ dev_warn(&ssif_info->client->dev,
1823
+ "Error getting global enables: %d %d %2.2x\n",
1824
+ rv, len, resp[2]);
15531825 rv = 0; /* Not fatal */
15541826 goto found;
15551827 }
....@@ -1567,8 +1839,9 @@
15671839 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
15681840 rv = do_cmd(client, 3, msg, &len, resp);
15691841 if (rv || (len < 2)) {
1570
- pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1571
- rv, len, resp[2]);
1842
+ dev_warn(&ssif_info->client->dev,
1843
+ "Error setting global enables: %d %d %2.2x\n",
1844
+ rv, len, resp[2]);
15721845 rv = 0; /* Not fatal */
15731846 goto found;
15741847 }
....@@ -1588,8 +1861,9 @@
15881861 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
15891862 rv = do_cmd(client, 3, msg, &len, resp);
15901863 if (rv || (len < 2)) {
1591
- pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1592
- rv, len, resp[2]);
1864
+ dev_warn(&ssif_info->client->dev,
1865
+ "Error setting global enables: %d %d %2.2x\n",
1866
+ rv, len, resp[2]);
15931867 rv = 0; /* Not fatal */
15941868 goto found;
15951869 }
....@@ -1602,13 +1876,15 @@
16021876
16031877 found:
16041878 if (ssif_dbg_probe) {
1605
- pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1606
- client->addr);
1879
+ dev_dbg(&ssif_info->client->dev,
1880
+ "%s: i2c_probe found device at i2c address %x\n",
1881
+ __func__, client->addr);
16071882 }
16081883
16091884 spin_lock_init(&ssif_info->lock);
16101885 ssif_info->ssif_state = SSIF_NORMAL;
16111886 timer_setup(&ssif_info->retry_timer, retry_timeout, 0);
1887
+ timer_setup(&ssif_info->watch_timer, watch_timeout, 0);
16121888
16131889 for (i = 0; i < SSIF_NUM_STATS; i++)
16141890 atomic_set(&ssif_info->stats[i], 0);
....@@ -1622,6 +1898,7 @@
16221898 ssif_info->handlers.get_smi_info = get_smi_info;
16231899 ssif_info->handlers.sender = sender;
16241900 ssif_info->handlers.request_events = request_events;
1901
+ ssif_info->handlers.set_need_watch = ssif_set_need_watch;
16251902
16261903 {
16271904 unsigned int thread_num;
....@@ -1655,8 +1932,9 @@
16551932 ssif_info,
16561933 &ssif_info->client->dev,
16571934 slave_addr);
1658
- if (rv) {
1659
- pr_err(PFX "Unable to register device: error %d\n", rv);
1935
+ if (rv) {
1936
+ dev_err(&ssif_info->client->dev,
1937
+ "Unable to register device: error %d\n", rv);
16601938 goto out_remove_attr;
16611939 }
16621940
....@@ -1665,31 +1943,19 @@
16651943 if (addr_info)
16661944 addr_info->client = NULL;
16671945
1668
- dev_err(&client->dev, "Unable to start IPMI SSIF: %d\n", rv);
1946
+ dev_err(&ssif_info->client->dev,
1947
+ "Unable to start IPMI SSIF: %d\n", rv);
1948
+ i2c_set_clientdata(client, NULL);
16691949 kfree(ssif_info);
16701950 }
16711951 kfree(resp);
1952
+ mutex_unlock(&ssif_infos_mutex);
16721953 return rv;
16731954
16741955 out_remove_attr:
16751956 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
16761957 dev_set_drvdata(&ssif_info->client->dev, NULL);
16771958 goto out;
1678
-}
1679
-
1680
-static int ssif_adapter_handler(struct device *adev, void *opaque)
1681
-{
1682
- struct ssif_addr_info *addr_info = opaque;
1683
-
1684
- if (adev->type != &i2c_adapter_type)
1685
- return 0;
1686
-
1687
- addr_info->added_client = i2c_new_device(to_i2c_adapter(adev),
1688
- &addr_info->binfo);
1689
-
1690
- if (!addr_info->adapter_name)
1691
- return 1; /* Only try the first I2C adapter by default. */
1692
- return 0;
16931959 }
16941960
16951961 static int new_ssif_client(int addr, char *adapter_name,
....@@ -1735,9 +2001,7 @@
17352001
17362002 list_add_tail(&addr_info->link, &ssif_infos);
17372003
1738
- if (initialized)
1739
- i2c_for_each_dev(addr_info, ssif_adapter_handler);
1740
- /* Otherwise address list will get it */
2004
+ /* Address list will get it */
17412005
17422006 out_unlock:
17432007 mutex_unlock(&ssif_infos_mutex);
....@@ -1760,7 +2024,7 @@
17602024 static unsigned short *ssif_address_list(void)
17612025 {
17622026 struct ssif_addr_info *info;
1763
- unsigned int count = 0, i;
2027
+ unsigned int count = 0, i = 0;
17642028 unsigned short *address_list;
17652029
17662030 list_for_each_entry(info, &ssif_infos, link)
....@@ -1771,18 +2035,17 @@
17712035 if (!address_list)
17722036 return NULL;
17732037
1774
- i = 0;
17752038 list_for_each_entry(info, &ssif_infos, link) {
17762039 unsigned short addr = info->binfo.addr;
17772040 int j;
17782041
17792042 for (j = 0; j < i; j++) {
17802043 if (address_list[j] == addr)
1781
- goto skip_addr;
2044
+ /* Found a dup. */
2045
+ break;
17822046 }
1783
- address_list[i] = addr;
1784
-skip_addr:
1785
- i++;
2047
+ if (j == i) /* Didn't find it in the list. */
2048
+ address_list[i++] = addr;
17862049 }
17872050 address_list[i] = I2C_CLIENT_END;
17882051
....@@ -1809,13 +2072,13 @@
18092072
18102073 rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
18112074 if (rv) {
1812
- dev_warn(&pdev->dev, PFX "No i2c-addr property\n");
2075
+ dev_warn(&pdev->dev, "No i2c-addr property\n");
18132076 return -ENODEV;
18142077 }
18152078
18162079 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
18172080 if (rv)
1818
- dev_warn(&pdev->dev, "device has no slave-addr property");
2081
+ slave_addr = 0x20;
18192082
18202083 return new_ssif_client(i2c_addr, NULL, 0,
18212084 slave_addr, SI_SMBIOS, &pdev->dev);
....@@ -1858,13 +2121,16 @@
18582121 return 0;
18592122
18602123 mutex_lock(&ssif_infos_mutex);
1861
- i2c_unregister_device(addr_info->added_client);
1862
-
18632124 list_del(&addr_info->link);
18642125 kfree(addr_info);
18652126 mutex_unlock(&ssif_infos_mutex);
18662127 return 0;
18672128 }
2129
+
2130
+static const struct platform_device_id ssif_plat_ids[] = {
2131
+ { "dmi-ipmi-ssif", 0 },
2132
+ { }
2133
+};
18682134
18692135 static struct platform_driver ipmi_driver = {
18702136 .driver = {
....@@ -1872,6 +2138,7 @@
18722138 },
18732139 .probe = ssif_platform_probe,
18742140 .remove = ssif_platform_remove,
2141
+ .id_table = ssif_plat_ids
18752142 };
18762143
18772144 static int init_ipmi_ssif(void)
....@@ -1890,8 +2157,7 @@
18902157 dbg[i], slave_addrs[i],
18912158 SI_HARDCODED, NULL);
18922159 if (rv)
1893
- pr_err(PFX
1894
- "Couldn't add hardcoded device at addr 0x%x\n",
2160
+ pr_err("Couldn't add hardcoded device at addr 0x%x\n",
18952161 addr[i]);
18962162 }
18972163
....@@ -1902,7 +2168,9 @@
19022168 if (ssif_trydmi) {
19032169 rv = platform_driver_register(&ipmi_driver);
19042170 if (rv)
1905
- pr_err(PFX "Unable to register driver: %d\n", rv);
2171
+ pr_err("Unable to register driver: %d\n", rv);
2172
+ else
2173
+ platform_registered = true;
19062174 }
19072175
19082176 ssif_i2c_driver.address_list = ssif_address_list();
....@@ -1924,7 +2192,10 @@
19242192
19252193 i2c_del_driver(&ssif_i2c_driver);
19262194
1927
- platform_driver_unregister(&ipmi_driver);
2195
+ kfree(ssif_i2c_driver.address_list);
2196
+
2197
+ if (ssif_trydmi && platform_registered)
2198
+ platform_driver_unregister(&ipmi_driver);
19282199
19292200 free_ssif_clients();
19302201 }