1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2015, Intel Corporation.
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/debugfs.h>
11 #include <linux/completion.h>
12 #include <linux/watchdog.h>
14 #include <linux/uuid.h>
15 #include <linux/mei_cl_bus.h>
18 * iAMT Watchdog Device
20 #define INTEL_AMT_WATCHDOG_ID "iamt_wdt"
22 #define MEI_WDT_DEFAULT_TIMEOUT 120 /* seconds */
23 #define MEI_WDT_MIN_TIMEOUT 120 /* seconds */
24 #define MEI_WDT_MAX_TIMEOUT 65535 /* seconds */
27 #define MEI_MANAGEMENT_CONTROL 0x02
29 /* MEI Management Control version number */
30 #define MEI_MC_VERSION_NUMBER 0x10
33 #define MEI_MC_START_WD_TIMER_REQ 0x13
34 #define MEI_MC_START_WD_TIMER_RES 0x83
35 #define MEI_WDT_STATUS_SUCCESS 0
36 #define MEI_WDT_WDSTATE_NOT_REQUIRED 0x1
37 #define MEI_MC_STOP_WD_TIMER_REQ 0x14
40 * enum mei_wdt_state - internal watchdog state
42 * @MEI_WDT_PROBE: wd in probing stage
43 * @MEI_WDT_IDLE: wd is idle and not opened
44 * @MEI_WDT_START: wd was opened, start was called
45 * @MEI_WDT_RUNNING: wd is expecting keep alive pings
46 * @MEI_WDT_STOPPING: wd is stopping and will move to IDLE
47 * @MEI_WDT_NOT_REQUIRED: wd device is not required
58 static const char *mei_wdt_state_str(enum mei_wdt_state state)
69 case MEI_WDT_STOPPING:
71 case MEI_WDT_NOT_REQUIRED:
72 return "NOT_REQUIRED";
79 * struct mei_wdt - mei watchdog driver
80 * @wdd: watchdog device
82 * @cldev: mei watchdog client device
83 * @state: watchdog internal state
84 * @resp_required: ping required response
85 * @response: ping response completion
86 * @unregister: unregister worker
87 * @reg_lock: watchdog device registration lock
88 * @timeout: watchdog current timeout
90 * @dbgfs_dir: debugfs dir entry
93 struct watchdog_device wdd;
95 struct mei_cl_device *cldev;
96 enum mei_wdt_state state;
98 struct completion response;
99 struct work_struct unregister;
100 struct mutex reg_lock;
103 #if IS_ENABLED(CONFIG_DEBUG_FS)
104 struct dentry *dbgfs_dir;
105 #endif /* CONFIG_DEBUG_FS */
109 * struct mei_mc_hdr - Management Control Command Header
111 * @command: Management Control (0x2)
112 * @bytecount: Number of bytes in the message beyond this byte
113 * @subcommand: Management Control Subcommand
114 * @versionnumber: Management Control Version (0x10)
124 * struct mei_wdt_start_request watchdog start/ping
126 * @hdr: Management Control Command Header
127 * @timeout: timeout value
128 * @reserved: reserved (legacy)
130 struct mei_wdt_start_request {
131 struct mei_mc_hdr hdr;
137 * struct mei_wdt_start_response watchdog start/ping response
139 * @hdr: Management Control Command Header
140 * @status: operation status
141 * @wdstate: watchdog status bit mask
143 struct mei_wdt_start_response {
144 struct mei_mc_hdr hdr;
150 * struct mei_wdt_stop_request - watchdog stop
152 * @hdr: Management Control Command Header
154 struct mei_wdt_stop_request {
155 struct mei_mc_hdr hdr;
159 * mei_wdt_ping - send wd start/ping command
161 * @wdt: mei watchdog device
163 * Return: 0 on success,
164 * negative errno code on failure
166 static int mei_wdt_ping(struct mei_wdt *wdt)
168 struct mei_wdt_start_request req;
169 const size_t req_len = sizeof(req);
172 memset(&req, 0, req_len);
173 req.hdr.command = MEI_MANAGEMENT_CONTROL;
174 req.hdr.bytecount = req_len - offsetof(struct mei_mc_hdr, subcommand);
175 req.hdr.subcommand = MEI_MC_START_WD_TIMER_REQ;
176 req.hdr.versionnumber = MEI_MC_VERSION_NUMBER;
177 req.timeout = wdt->timeout;
179 ret = mei_cldev_send(wdt->cldev, (u8 *)&req, req_len);
187 * mei_wdt_stop - send wd stop command
189 * @wdt: mei watchdog device
191 * Return: 0 on success,
192 * negative errno code on failure
194 static int mei_wdt_stop(struct mei_wdt *wdt)
196 struct mei_wdt_stop_request req;
197 const size_t req_len = sizeof(req);
200 memset(&req, 0, req_len);
201 req.hdr.command = MEI_MANAGEMENT_CONTROL;
202 req.hdr.bytecount = req_len - offsetof(struct mei_mc_hdr, subcommand);
203 req.hdr.subcommand = MEI_MC_STOP_WD_TIMER_REQ;
204 req.hdr.versionnumber = MEI_MC_VERSION_NUMBER;
206 ret = mei_cldev_send(wdt->cldev, (u8 *)&req, req_len);
214 * mei_wdt_ops_start - wd start command from the watchdog core.
216 * @wdd: watchdog device
218 * Return: 0 on success or -ENODEV;
220 static int mei_wdt_ops_start(struct watchdog_device *wdd)
222 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
224 wdt->state = MEI_WDT_START;
225 wdd->timeout = wdt->timeout;
230 * mei_wdt_ops_stop - wd stop command from the watchdog core.
232 * @wdd: watchdog device
234 * Return: 0 if success, negative errno code for failure
236 static int mei_wdt_ops_stop(struct watchdog_device *wdd)
238 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
241 if (wdt->state != MEI_WDT_RUNNING)
244 wdt->state = MEI_WDT_STOPPING;
246 ret = mei_wdt_stop(wdt);
250 wdt->state = MEI_WDT_IDLE;
256 * mei_wdt_ops_ping - wd ping command from the watchdog core.
258 * @wdd: watchdog device
260 * Return: 0 if success, negative errno code on failure
262 static int mei_wdt_ops_ping(struct watchdog_device *wdd)
264 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
267 if (wdt->state != MEI_WDT_START && wdt->state != MEI_WDT_RUNNING)
270 if (wdt->resp_required)
271 init_completion(&wdt->response);
273 wdt->state = MEI_WDT_RUNNING;
274 ret = mei_wdt_ping(wdt);
278 if (wdt->resp_required)
279 ret = wait_for_completion_killable(&wdt->response);
285 * mei_wdt_ops_set_timeout - wd set timeout command from the watchdog core.
287 * @wdd: watchdog device
288 * @timeout: timeout value to set
290 * Return: 0 if success, negative errno code for failure
292 static int mei_wdt_ops_set_timeout(struct watchdog_device *wdd,
293 unsigned int timeout)
296 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
298 /* valid value is already checked by the caller */
299 wdt->timeout = timeout;
300 wdd->timeout = timeout;
305 static const struct watchdog_ops wd_ops = {
306 .owner = THIS_MODULE,
307 .start = mei_wdt_ops_start,
308 .stop = mei_wdt_ops_stop,
309 .ping = mei_wdt_ops_ping,
310 .set_timeout = mei_wdt_ops_set_timeout,
313 /* not const as the firmware_version field need to be retrieved */
314 static struct watchdog_info wd_info = {
315 .identity = INTEL_AMT_WATCHDOG_ID,
316 .options = WDIOF_KEEPALIVEPING |
322 * __mei_wdt_is_registered - check if wdt is registered
324 * @wdt: mei watchdog device
326 * Return: true if the wdt is registered with the watchdog subsystem
327 * Locking: should be called under wdt->reg_lock
329 static inline bool __mei_wdt_is_registered(struct mei_wdt *wdt)
331 return !!watchdog_get_drvdata(&wdt->wdd);
335 * mei_wdt_unregister - unregister from the watchdog subsystem
337 * @wdt: mei watchdog device
339 static void mei_wdt_unregister(struct mei_wdt *wdt)
341 mutex_lock(&wdt->reg_lock);
343 if (__mei_wdt_is_registered(wdt)) {
344 watchdog_unregister_device(&wdt->wdd);
345 watchdog_set_drvdata(&wdt->wdd, NULL);
346 memset(&wdt->wdd, 0, sizeof(wdt->wdd));
349 mutex_unlock(&wdt->reg_lock);
353 * mei_wdt_register - register with the watchdog subsystem
355 * @wdt: mei watchdog device
357 * Return: 0 if success, negative errno code for failure
359 static int mei_wdt_register(struct mei_wdt *wdt)
364 if (!wdt || !wdt->cldev)
367 dev = &wdt->cldev->dev;
369 mutex_lock(&wdt->reg_lock);
371 if (__mei_wdt_is_registered(wdt)) {
376 wdt->wdd.info = &wd_info;
377 wdt->wdd.ops = &wd_ops;
378 wdt->wdd.parent = dev;
379 wdt->wdd.timeout = MEI_WDT_DEFAULT_TIMEOUT;
380 wdt->wdd.min_timeout = MEI_WDT_MIN_TIMEOUT;
381 wdt->wdd.max_timeout = MEI_WDT_MAX_TIMEOUT;
383 watchdog_set_drvdata(&wdt->wdd, wdt);
384 watchdog_stop_on_reboot(&wdt->wdd);
385 watchdog_stop_on_unregister(&wdt->wdd);
387 ret = watchdog_register_device(&wdt->wdd);
389 dev_err(dev, "unable to register watchdog device = %d.\n", ret);
390 watchdog_set_drvdata(&wdt->wdd, NULL);
393 wdt->state = MEI_WDT_IDLE;
396 mutex_unlock(&wdt->reg_lock);
400 static void mei_wdt_unregister_work(struct work_struct *work)
402 struct mei_wdt *wdt = container_of(work, struct mei_wdt, unregister);
404 mei_wdt_unregister(wdt);
408 * mei_wdt_rx - callback for data receive
412 static void mei_wdt_rx(struct mei_cl_device *cldev)
414 struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
415 struct mei_wdt_start_response res;
416 const size_t res_len = sizeof(res);
419 ret = mei_cldev_recv(wdt->cldev, (u8 *)&res, res_len);
421 dev_err(&cldev->dev, "failure in recv %d\n", ret);
425 /* Empty response can be sent on stop */
429 if (ret < sizeof(struct mei_mc_hdr)) {
430 dev_err(&cldev->dev, "recv small data %d\n", ret);
434 if (res.hdr.command != MEI_MANAGEMENT_CONTROL ||
435 res.hdr.versionnumber != MEI_MC_VERSION_NUMBER) {
436 dev_err(&cldev->dev, "wrong command received\n");
440 if (res.hdr.subcommand != MEI_MC_START_WD_TIMER_RES) {
441 dev_warn(&cldev->dev, "unsupported command %d :%s[%d]\n",
443 mei_wdt_state_str(wdt->state),
448 /* Run the unregistration in a worker as this can be
449 * run only after ping completion, otherwise the flow will
450 * deadlock on watchdog core mutex.
452 if (wdt->state == MEI_WDT_RUNNING) {
453 if (res.wdstate & MEI_WDT_WDSTATE_NOT_REQUIRED) {
454 wdt->state = MEI_WDT_NOT_REQUIRED;
455 schedule_work(&wdt->unregister);
460 if (wdt->state == MEI_WDT_PROBE) {
461 if (res.wdstate & MEI_WDT_WDSTATE_NOT_REQUIRED) {
462 wdt->state = MEI_WDT_NOT_REQUIRED;
464 /* stop the watchdog and register watchdog device */
466 mei_wdt_register(wdt);
471 dev_warn(&cldev->dev, "not in correct state %s[%d]\n",
472 mei_wdt_state_str(wdt->state), wdt->state);
475 if (!completion_done(&wdt->response))
476 complete(&wdt->response);
480 * mei_wdt_notif - callback for event notification
484 static void mei_wdt_notif(struct mei_cl_device *cldev)
486 struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
488 if (wdt->state != MEI_WDT_NOT_REQUIRED)
491 mei_wdt_register(wdt);
494 #if IS_ENABLED(CONFIG_DEBUG_FS)
496 static ssize_t mei_dbgfs_read_activation(struct file *file, char __user *ubuf,
497 size_t cnt, loff_t *ppos)
499 struct mei_wdt *wdt = file->private_data;
500 const size_t bufsz = 32;
504 mutex_lock(&wdt->reg_lock);
505 pos = scnprintf(buf, bufsz, "%s\n",
506 __mei_wdt_is_registered(wdt) ? "activated" : "deactivated");
507 mutex_unlock(&wdt->reg_lock);
509 return simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
512 static const struct file_operations dbgfs_fops_activation = {
514 .read = mei_dbgfs_read_activation,
515 .llseek = generic_file_llseek,
518 static ssize_t mei_dbgfs_read_state(struct file *file, char __user *ubuf,
519 size_t cnt, loff_t *ppos)
521 struct mei_wdt *wdt = file->private_data;
525 pos = scnprintf(buf, sizeof(buf), "state: %s\n",
526 mei_wdt_state_str(wdt->state));
528 return simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
531 static const struct file_operations dbgfs_fops_state = {
533 .read = mei_dbgfs_read_state,
534 .llseek = generic_file_llseek,
537 static void dbgfs_unregister(struct mei_wdt *wdt)
539 debugfs_remove_recursive(wdt->dbgfs_dir);
540 wdt->dbgfs_dir = NULL;
543 static int dbgfs_register(struct mei_wdt *wdt)
545 struct dentry *dir, *f;
547 dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
551 wdt->dbgfs_dir = dir;
552 f = debugfs_create_file("state", S_IRUSR, dir, wdt, &dbgfs_fops_state);
556 f = debugfs_create_file("activation", S_IRUSR,
557 dir, wdt, &dbgfs_fops_activation);
563 dbgfs_unregister(wdt);
569 static inline void dbgfs_unregister(struct mei_wdt *wdt) {}
571 static inline int dbgfs_register(struct mei_wdt *wdt)
575 #endif /* CONFIG_DEBUG_FS */
577 static int mei_wdt_probe(struct mei_cl_device *cldev,
578 const struct mei_cl_device_id *id)
583 wdt = kzalloc(sizeof(struct mei_wdt), GFP_KERNEL);
587 wdt->timeout = MEI_WDT_DEFAULT_TIMEOUT;
588 wdt->state = MEI_WDT_PROBE;
590 wdt->resp_required = mei_cldev_ver(cldev) > 0x1;
591 mutex_init(&wdt->reg_lock);
592 init_completion(&wdt->response);
593 INIT_WORK(&wdt->unregister, mei_wdt_unregister_work);
595 mei_cldev_set_drvdata(cldev, wdt);
597 ret = mei_cldev_enable(cldev);
599 dev_err(&cldev->dev, "Could not enable cl device\n");
603 ret = mei_cldev_register_rx_cb(wdt->cldev, mei_wdt_rx);
605 dev_err(&cldev->dev, "Could not reg rx event ret=%d\n", ret);
609 ret = mei_cldev_register_notif_cb(wdt->cldev, mei_wdt_notif);
610 /* on legacy devices notification is not supported
612 if (ret && ret != -EOPNOTSUPP) {
613 dev_err(&cldev->dev, "Could not reg notif event ret=%d\n", ret);
617 wd_info.firmware_version = mei_cldev_ver(cldev);
619 if (wdt->resp_required)
620 ret = mei_wdt_ping(wdt);
622 ret = mei_wdt_register(wdt);
627 if (dbgfs_register(wdt))
628 dev_warn(&cldev->dev, "cannot register debugfs\n");
633 mei_cldev_disable(cldev);
641 static int mei_wdt_remove(struct mei_cl_device *cldev)
643 struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
645 /* Free the caller in case of fw initiated or unexpected reset */
646 if (!completion_done(&wdt->response))
647 complete(&wdt->response);
649 cancel_work_sync(&wdt->unregister);
651 mei_wdt_unregister(wdt);
653 mei_cldev_disable(cldev);
655 dbgfs_unregister(wdt);
662 #define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
663 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
665 static const struct mei_cl_device_id mei_wdt_tbl[] = {
666 { .uuid = MEI_UUID_WD, .version = MEI_CL_VERSION_ANY },
667 /* required last entry */
670 MODULE_DEVICE_TABLE(mei, mei_wdt_tbl);
672 static struct mei_cl_driver mei_wdt_driver = {
673 .id_table = mei_wdt_tbl,
674 .name = KBUILD_MODNAME,
676 .probe = mei_wdt_probe,
677 .remove = mei_wdt_remove,
680 module_mei_cl_driver(mei_wdt_driver);
682 MODULE_AUTHOR("Intel Corporation");
683 MODULE_LICENSE("GPL v2");
684 MODULE_DESCRIPTION("Device driver for Intel MEI iAMT watchdog");