hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/char/ipmi/ipmi_si_intf.c
....@@ -19,6 +19,8 @@
1919 * and drives the real SMI state machine.
2020 */
2121
22
+#define pr_fmt(fmt) "ipmi_si: " fmt
23
+
2224 #include <linux/module.h>
2325 #include <linux/moduleparam.h>
2426 #include <linux/sched.h>
....@@ -38,10 +40,9 @@
3840 #include <linux/ipmi.h>
3941 #include <linux/ipmi_smi.h>
4042 #include "ipmi_si.h"
43
+#include "ipmi_si_sm.h"
4144 #include <linux/string.h>
4245 #include <linux/ctype.h>
43
-
44
-#define PFX "ipmi_si: "
4546
4647 /* Measure times between events in the driver. */
4748 #undef DEBUG_TIMING
....@@ -71,7 +72,7 @@
7172
7273 static const char * const si_to_str[] = { "invalid", "kcs", "smic", "bt" };
7374
74
-static int initialized;
75
+static bool initialized;
7576
7677 /*
7778 * Indexes into stats[] in smi_info below.
....@@ -232,14 +233,8 @@
232233 /* From the get device id response... */
233234 struct ipmi_device_id device_id;
234235
235
- /* Default driver model device. */
236
- struct platform_device *pdev;
237
-
238236 /* Have we added the device group to the device? */
239237 bool dev_group_added;
240
-
241
- /* Have we added the platform device? */
242
- bool pdev_registered;
243238
244239 /* Counters and things for the proc filesystem. */
245240 atomic_t stats[SI_NUM_STATS];
....@@ -272,8 +267,8 @@
272267 {
273268 struct timespec64 t;
274269
275
- getnstimeofday64(&t);
276
- pr_debug("**%s: %lld.%9.9ld\n", msg, (long long) t.tv_sec, t.tv_nsec);
270
+ ktime_get_ts64(&t);
271
+ pr_debug("**%s: %lld.%9.9ld\n", msg, t.tv_sec, t.tv_nsec);
277272 }
278273 #else
279274 #define debug_timestamp(x)
....@@ -940,42 +935,29 @@
940935 }
941936
942937 /*
943
- * Use -1 in the nsec value of the busy waiting timespec to tell that
944
- * we are spinning in kipmid looking for something and not delaying
945
- * between checks
938
+ * Use -1 as a special constant to tell that we are spinning in kipmid
939
+ * looking for something and not delaying between checks
946940 */
947
-static inline void ipmi_si_set_not_busy(struct timespec64 *ts)
948
-{
949
- ts->tv_nsec = -1;
950
-}
951
-static inline int ipmi_si_is_busy(struct timespec64 *ts)
952
-{
953
- return ts->tv_nsec != -1;
954
-}
955
-
956
-static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
957
- const struct smi_info *smi_info,
958
- struct timespec64 *busy_until)
941
+#define IPMI_TIME_NOT_BUSY ns_to_ktime(-1ull)
942
+static inline bool ipmi_thread_busy_wait(enum si_sm_result smi_result,
943
+ const struct smi_info *smi_info,
944
+ ktime_t *busy_until)
959945 {
960946 unsigned int max_busy_us = 0;
961947
962948 if (smi_info->si_num < num_max_busy_us)
963949 max_busy_us = kipmid_max_busy_us[smi_info->si_num];
964950 if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
965
- ipmi_si_set_not_busy(busy_until);
966
- else if (!ipmi_si_is_busy(busy_until)) {
967
- getnstimeofday64(busy_until);
968
- timespec64_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
951
+ *busy_until = IPMI_TIME_NOT_BUSY;
952
+ else if (*busy_until == IPMI_TIME_NOT_BUSY) {
953
+ *busy_until = ktime_get() + max_busy_us * NSEC_PER_USEC;
969954 } else {
970
- struct timespec64 now;
971
-
972
- getnstimeofday64(&now);
973
- if (unlikely(timespec64_compare(&now, busy_until) > 0)) {
974
- ipmi_si_set_not_busy(busy_until);
975
- return 0;
955
+ if (unlikely(ktime_get() > *busy_until)) {
956
+ *busy_until = IPMI_TIME_NOT_BUSY;
957
+ return false;
976958 }
977959 }
978
- return 1;
960
+ return true;
979961 }
980962
981963
....@@ -986,16 +968,15 @@
986968 * that are not BT and do not have interrupts. It starts spinning
987969 * when an operation is complete or until max_busy tells it to stop
988970 * (if that is enabled). See the paragraph on kimid_max_busy_us in
989
- * Documentation/IPMI.txt for details.
971
+ * Documentation/driver-api/ipmi.rst for details.
990972 */
991973 static int ipmi_thread(void *data)
992974 {
993975 struct smi_info *smi_info = data;
994976 unsigned long flags;
995977 enum si_sm_result smi_result;
996
- struct timespec64 busy_until;
978
+ ktime_t busy_until = IPMI_TIME_NOT_BUSY;
997979
998
- ipmi_si_set_not_busy(&busy_until);
999980 set_user_nice(current, MAX_NICE);
1000981 while (!kthread_should_stop()) {
1001982 int busy_wait;
....@@ -1073,10 +1054,13 @@
10731054 atomic_set(&smi_info->req_events, 1);
10741055 }
10751056
1076
-static void set_need_watch(void *send_info, bool enable)
1057
+static void set_need_watch(void *send_info, unsigned int watch_mask)
10771058 {
10781059 struct smi_info *smi_info = send_info;
10791060 unsigned long flags;
1061
+ int enable;
1062
+
1063
+ enable = !!watch_mask;
10801064
10811065 atomic_set(&smi_info->need_watch, enable);
10821066 spin_lock_irqsave(&smi_info->si_lock, flags);
....@@ -1283,12 +1267,12 @@
12831267 rv = request_irq(io->irq,
12841268 ipmi_si_irq_handler,
12851269 IRQF_SHARED,
1286
- DEVICE_NAME,
1270
+ SI_DEVICE_NAME,
12871271 io->irq_handler_data);
12881272 if (rv) {
12891273 dev_warn(io->dev, "%s unable to claim interrupt %d,"
12901274 " running polled\n",
1291
- DEVICE_NAME, io->irq);
1275
+ SI_DEVICE_NAME, io->irq);
12921276 io->irq = 0;
12931277 } else {
12941278 io->irq_cleanup = std_irq_cleanup;
....@@ -1332,6 +1316,7 @@
13321316 unsigned char *resp;
13331317 unsigned long resp_len;
13341318 int rv = 0;
1319
+ unsigned int retry_count = 0;
13351320
13361321 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
13371322 if (!resp)
....@@ -1343,6 +1328,8 @@
13431328 */
13441329 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
13451330 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1331
+
1332
+retry:
13461333 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
13471334
13481335 rv = wait_for_msg_done(smi_info);
....@@ -1355,6 +1342,20 @@
13551342 /* Check and record info from the get device id, in case we need it. */
13561343 rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
13571344 resp + 2, resp_len - 2, &smi_info->device_id);
1345
+ if (rv) {
1346
+ /* record completion code */
1347
+ unsigned char cc = *(resp + 2);
1348
+
1349
+ if ((cc == IPMI_DEVICE_IN_FW_UPDATE_ERR
1350
+ || cc == IPMI_DEVICE_IN_INIT_ERR
1351
+ || cc == IPMI_NOT_IN_MY_STATE_ERR)
1352
+ && ++retry_count <= GET_DEVICE_ID_MAX_RETRY) {
1353
+ dev_warn(smi_info->io.dev,
1354
+ "BMC returned 0x%2.2x, retry get bmc device id\n",
1355
+ cc);
1356
+ goto retry;
1357
+ }
1358
+ }
13581359
13591360 out:
13601361 kfree(resp);
....@@ -1544,7 +1545,7 @@
15441545
15451546 rv = wait_for_msg_done(smi_info);
15461547 if (rv) {
1547
- pr_warn(PFX "Error getting response from get global enables command, the event buffer is not enabled.\n");
1548
+ pr_warn("Error getting response from get global enables command, the event buffer is not enabled\n");
15481549 goto out;
15491550 }
15501551
....@@ -1555,7 +1556,7 @@
15551556 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
15561557 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
15571558 resp[2] != 0) {
1558
- pr_warn(PFX "Invalid return from get global enables command, cannot enable the event buffer.\n");
1559
+ pr_warn("Invalid return from get global enables command, cannot enable the event buffer\n");
15591560 rv = -EINVAL;
15601561 goto out;
15611562 }
....@@ -1573,7 +1574,7 @@
15731574
15741575 rv = wait_for_msg_done(smi_info);
15751576 if (rv) {
1576
- pr_warn(PFX "Error getting response from set global, enables command, the event buffer is not enabled.\n");
1577
+ pr_warn("Error getting response from set global, enables command, the event buffer is not enabled\n");
15771578 goto out;
15781579 }
15791580
....@@ -1583,7 +1584,7 @@
15831584 if (resp_len < 3 ||
15841585 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
15851586 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1586
- pr_warn(PFX "Invalid return from get global, enables command, not enable the event buffer.\n");
1587
+ pr_warn("Invalid return from get global, enables command, not enable the event buffer\n");
15871588 rv = -EINVAL;
15881589 goto out;
15891590 }
....@@ -1603,37 +1604,37 @@
16031604 }
16041605
16051606 #define IPMI_SI_ATTR(name) \
1606
-static ssize_t ipmi_##name##_show(struct device *dev, \
1607
- struct device_attribute *attr, \
1608
- char *buf) \
1607
+static ssize_t name##_show(struct device *dev, \
1608
+ struct device_attribute *attr, \
1609
+ char *buf) \
16091610 { \
16101611 struct smi_info *smi_info = dev_get_drvdata(dev); \
16111612 \
16121613 return snprintf(buf, 10, "%u\n", smi_get_stat(smi_info, name)); \
16131614 } \
1614
-static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1615
+static DEVICE_ATTR(name, 0444, name##_show, NULL)
16151616
1616
-static ssize_t ipmi_type_show(struct device *dev,
1617
- struct device_attribute *attr,
1618
- char *buf)
1617
+static ssize_t type_show(struct device *dev,
1618
+ struct device_attribute *attr,
1619
+ char *buf)
16191620 {
16201621 struct smi_info *smi_info = dev_get_drvdata(dev);
16211622
16221623 return snprintf(buf, 10, "%s\n", si_to_str[smi_info->io.si_type]);
16231624 }
1624
-static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1625
+static DEVICE_ATTR(type, 0444, type_show, NULL);
16251626
1626
-static ssize_t ipmi_interrupts_enabled_show(struct device *dev,
1627
- struct device_attribute *attr,
1628
- char *buf)
1627
+static ssize_t interrupts_enabled_show(struct device *dev,
1628
+ struct device_attribute *attr,
1629
+ char *buf)
16291630 {
16301631 struct smi_info *smi_info = dev_get_drvdata(dev);
16311632 int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
16321633
16331634 return snprintf(buf, 10, "%d\n", enabled);
16341635 }
1635
-static DEVICE_ATTR(interrupts_enabled, S_IRUGO,
1636
- ipmi_interrupts_enabled_show, NULL);
1636
+static DEVICE_ATTR(interrupts_enabled, 0444,
1637
+ interrupts_enabled_show, NULL);
16371638
16381639 IPMI_SI_ATTR(short_timeouts);
16391640 IPMI_SI_ATTR(long_timeouts);
....@@ -1647,16 +1648,16 @@
16471648 IPMI_SI_ATTR(watchdog_pretimeouts);
16481649 IPMI_SI_ATTR(incoming_messages);
16491650
1650
-static ssize_t ipmi_params_show(struct device *dev,
1651
- struct device_attribute *attr,
1652
- char *buf)
1651
+static ssize_t params_show(struct device *dev,
1652
+ struct device_attribute *attr,
1653
+ char *buf)
16531654 {
16541655 struct smi_info *smi_info = dev_get_drvdata(dev);
16551656
16561657 return snprintf(buf, 200,
16571658 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
16581659 si_to_str[smi_info->io.si_type],
1659
- addr_space_to_str[smi_info->io.addr_type],
1660
+ addr_space_to_str[smi_info->io.addr_space],
16601661 smi_info->io.addr_data,
16611662 smi_info->io.regspacing,
16621663 smi_info->io.regsize,
....@@ -1664,7 +1665,7 @@
16641665 smi_info->io.irq,
16651666 smi_info->io.slave_addr);
16661667 }
1667
-static DEVICE_ATTR(params, S_IRUGO, ipmi_params_show, NULL);
1668
+static DEVICE_ATTR(params, 0444, params_show, NULL);
16681669
16691670 static struct attribute *ipmi_si_dev_attrs[] = {
16701671 &dev_attr_type.attr,
....@@ -1845,8 +1846,7 @@
18451846 }
18461847
18471848 smi_info->timer_can_start = false;
1848
- if (smi_info->timer_running)
1849
- del_timer_sync(&smi_info->si_timer);
1849
+ del_timer_sync(&smi_info->si_timer);
18501850 }
18511851
18521852 static struct smi_info *find_dup_si(struct smi_info *info)
....@@ -1854,7 +1854,7 @@
18541854 struct smi_info *e;
18551855
18561856 list_for_each_entry(e, &smi_infos, link) {
1857
- if (e->io.addr_type != info->io.addr_type)
1857
+ if (e->io.addr_space != info->io.addr_space)
18581858 continue;
18591859 if (e->io.addr_data == info->io.addr_data) {
18601860 /*
....@@ -1881,17 +1881,17 @@
18811881 * address, they presumably want us to use it and not what is
18821882 * in the firmware.
18831883 */
1884
- if (io->addr_source != SI_HARDCODED &&
1885
- ipmi_si_hardcode_match(io->addr_type, io->addr_data)) {
1884
+ if (io->addr_source != SI_HARDCODED && io->addr_source != SI_HOTMOD &&
1885
+ ipmi_si_hardcode_match(io->addr_space, io->addr_data)) {
18861886 dev_info(io->dev,
18871887 "Hard-coded device at this address already exists");
18881888 return -ENODEV;
18891889 }
18901890
18911891 if (!io->io_setup) {
1892
- if (io->addr_type == IPMI_IO_ADDR_SPACE) {
1892
+ if (io->addr_space == IPMI_IO_ADDR_SPACE) {
18931893 io->io_setup = ipmi_si_port_setup;
1894
- } else if (io->addr_type == IPMI_MEM_ADDR_SPACE) {
1894
+ } else if (io->addr_space == IPMI_MEM_ADDR_SPACE) {
18951895 io->io_setup = ipmi_si_mem_setup;
18961896 } else {
18971897 return -EINVAL;
....@@ -1926,7 +1926,7 @@
19261926 }
19271927 }
19281928
1929
- pr_info(PFX "Adding %s-specified %s state machine\n",
1929
+ pr_info("Adding %s-specified %s state machine\n",
19301930 ipmi_addr_src_to_str(new_smi->io.addr_source),
19311931 si_to_str[new_smi->io.si_type]);
19321932
....@@ -1948,12 +1948,11 @@
19481948 {
19491949 int rv = 0;
19501950 int i;
1951
- char *init_name = NULL;
19521951
1953
- pr_info(PFX "Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
1952
+ pr_info("Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
19541953 ipmi_addr_src_to_str(new_smi->io.addr_source),
19551954 si_to_str[new_smi->io.si_type],
1956
- addr_space_to_str[new_smi->io.addr_type],
1955
+ addr_space_to_str[new_smi->io.addr_space],
19571956 new_smi->io.addr_data,
19581957 new_smi->io.slave_addr, new_smi->io.irq);
19591958
....@@ -1980,24 +1979,9 @@
19801979
19811980 /* Do this early so it's available for logs. */
19821981 if (!new_smi->io.dev) {
1983
- init_name = kasprintf(GFP_KERNEL, "ipmi_si.%d",
1984
- new_smi->si_num);
1985
-
1986
- /*
1987
- * If we don't already have a device from something
1988
- * else (like PCI), then register a new one.
1989
- */
1990
- new_smi->pdev = platform_device_alloc("ipmi_si",
1991
- new_smi->si_num);
1992
- if (!new_smi->pdev) {
1993
- pr_err(PFX "Unable to allocate platform device\n");
1994
- rv = -ENOMEM;
1995
- goto out_err;
1996
- }
1997
- new_smi->io.dev = &new_smi->pdev->dev;
1998
- new_smi->io.dev->driver = &ipmi_platform_driver.driver;
1999
- /* Nulled by device_add() */
2000
- new_smi->io.dev->init_name = init_name;
1982
+ pr_err("IPMI interface added with no device\n");
1983
+ rv = -EIO;
1984
+ goto out_err;
20011985 }
20021986
20031987 /* Allocate the state machine's data and initialize it. */
....@@ -2070,17 +2054,6 @@
20702054 atomic_set(&new_smi->req_events, 1);
20712055 }
20722056
2073
- if (new_smi->pdev && !new_smi->pdev_registered) {
2074
- rv = platform_device_add(new_smi->pdev);
2075
- if (rv) {
2076
- dev_err(new_smi->io.dev,
2077
- "Unable to register system interface device: %d\n",
2078
- rv);
2079
- goto out_err;
2080
- }
2081
- new_smi->pdev_registered = true;
2082
- }
2083
-
20842057 dev_set_drvdata(new_smi->io.dev, new_smi);
20852058 rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
20862059 if (rv) {
....@@ -2116,7 +2089,6 @@
21162089 new_smi->io.io_cleanup = NULL;
21172090 }
21182091
2119
- kfree(init_name);
21202092 return rv;
21212093 }
21222094
....@@ -2129,7 +2101,8 @@
21292101 return 0;
21302102
21312103 ipmi_hardcode_init();
2132
- pr_info("IPMI System Interface driver.\n");
2104
+
2105
+ pr_info("IPMI System Interface driver\n");
21332106
21342107 ipmi_si_platform_init();
21352108
....@@ -2168,7 +2141,7 @@
21682141 }
21692142
21702143 skip_fallback_noirq:
2171
- initialized = 1;
2144
+ initialized = true;
21722145 mutex_unlock(&smi_infos_lock);
21732146
21742147 if (type)
....@@ -2178,7 +2151,7 @@
21782151 if (unload_when_empty && list_empty(&smi_infos)) {
21792152 mutex_unlock(&smi_infos_lock);
21802153 cleanup_ipmi_si();
2181
- pr_warn(PFX "Unable to find any System Interface(s)\n");
2154
+ pr_warn("Unable to find any System Interface(s)\n");
21822155 return -ENODEV;
21832156 } else {
21842157 mutex_unlock(&smi_infos_lock);
....@@ -2214,7 +2187,7 @@
22142187 * handlers might have been running before we freed the
22152188 * interrupt.
22162189 */
2217
- synchronize_sched();
2190
+ synchronize_rcu();
22182191
22192192 /*
22202193 * Timeouts are stopped, now make sure the interrupts are off
....@@ -2263,13 +2236,6 @@
22632236 if (smi_info->intf)
22642237 ipmi_unregister_smi(smi_info->intf);
22652238
2266
- if (smi_info->pdev) {
2267
- if (smi_info->pdev_registered)
2268
- platform_device_unregister(smi_info->pdev);
2269
- else
2270
- platform_device_put(smi_info->pdev);
2271
- }
2272
-
22732239 kfree(smi_info);
22742240 }
22752241
....@@ -2291,22 +2257,27 @@
22912257 return rv;
22922258 }
22932259
2294
-void ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
2295
- unsigned long addr)
2260
+struct device *ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
2261
+ unsigned long addr)
22962262 {
22972263 /* remove */
22982264 struct smi_info *e, *tmp_e;
2265
+ struct device *dev = NULL;
22992266
23002267 mutex_lock(&smi_infos_lock);
23012268 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
2302
- if (e->io.addr_type != addr_space)
2269
+ if (e->io.addr_space != addr_space)
23032270 continue;
23042271 if (e->io.si_type != si_type)
23052272 continue;
2306
- if (e->io.addr_data == addr)
2273
+ if (e->io.addr_data == addr) {
2274
+ dev = get_device(e->io.dev);
23072275 cleanup_one_si(e);
2276
+ }
23082277 }
23092278 mutex_unlock(&smi_infos_lock);
2279
+
2280
+ return dev;
23102281 }
23112282
23122283 static void cleanup_ipmi_si(void)
....@@ -2328,6 +2299,7 @@
23282299 mutex_unlock(&smi_infos_lock);
23292300
23302301 ipmi_si_hardcode_exit();
2302
+ ipmi_si_hotmod_exit();
23312303 }
23322304 module_exit(cleanup_ipmi_si);
23332305