2 * Intel Management Engine Interface (Intel MEI) Linux driver
3 * Copyright (c) 2015, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/debugfs.h>
19 #include <linux/completion.h>
20 #include <linux/watchdog.h>
22 #include <linux/uuid.h>
23 #include <linux/mei_cl_bus.h>
26 * iAMT Watchdog Device
28 #define INTEL_AMT_WATCHDOG_ID "iamt_wdt"
30 #define MEI_WDT_DEFAULT_TIMEOUT 120 /* seconds */
31 #define MEI_WDT_MIN_TIMEOUT 120 /* seconds */
32 #define MEI_WDT_MAX_TIMEOUT 65535 /* seconds */
35 #define MEI_MANAGEMENT_CONTROL 0x02
37 /* MEI Management Control version number */
38 #define MEI_MC_VERSION_NUMBER 0x10
41 #define MEI_MC_START_WD_TIMER_REQ 0x13
42 #define MEI_MC_START_WD_TIMER_RES 0x83
43 #define MEI_WDT_STATUS_SUCCESS 0
44 #define MEI_WDT_WDSTATE_NOT_REQUIRED 0x1
45 #define MEI_MC_STOP_WD_TIMER_REQ 0x14
48 * enum mei_wdt_state - internal watchdog state
50 * @MEI_WDT_PROBE: wd in probing stage
51 * @MEI_WDT_IDLE: wd is idle and not opened
52 * @MEI_WDT_START: wd was opened, start was called
53 * @MEI_WDT_RUNNING: wd is expecting keep alive pings
54 * @MEI_WDT_STOPPING: wd is stopping and will move to IDLE
55 * @MEI_WDT_NOT_REQUIRED: wd device is not required
66 static const char *mei_wdt_state_str(enum mei_wdt_state state)
77 case MEI_WDT_STOPPING:
79 case MEI_WDT_NOT_REQUIRED:
80 return "NOT_REQUIRED";
87 * struct mei_wdt - mei watchdog driver
88 * @wdd: watchdog device
90 * @cldev: mei watchdog client device
91 * @state: watchdog internal state
92 * @resp_required: ping required response
93 * @response: ping response completion
94 * @unregister: unregister worker
95 * @reg_lock: watchdog device registration lock
96 * @timeout: watchdog current timeout
98 * @dbgfs_dir: debugfs dir entry
101 struct watchdog_device wdd;
103 struct mei_cl_device *cldev;
104 enum mei_wdt_state state;
106 struct completion response;
107 struct work_struct unregister;
108 struct mutex reg_lock;
111 #if IS_ENABLED(CONFIG_DEBUG_FS)
112 struct dentry *dbgfs_dir;
113 #endif /* CONFIG_DEBUG_FS */
117 * struct mei_mc_hdr - Management Control Command Header
119 * @command: Management Control (0x2)
120 * @bytecount: Number of bytes in the message beyond this byte
121 * @subcommand: Management Control Subcommand
122 * @versionnumber: Management Control Version (0x10)
132 * struct mei_wdt_start_request watchdog start/ping
134 * @hdr: Management Control Command Header
135 * @timeout: timeout value
136 * @reserved: reserved (legacy)
138 struct mei_wdt_start_request {
139 struct mei_mc_hdr hdr;
145 * struct mei_wdt_start_response watchdog start/ping response
147 * @hdr: Management Control Command Header
148 * @status: operation status
149 * @wdstate: watchdog status bit mask
151 struct mei_wdt_start_response {
152 struct mei_mc_hdr hdr;
158 * struct mei_wdt_stop_request - watchdog stop
160 * @hdr: Management Control Command Header
162 struct mei_wdt_stop_request {
163 struct mei_mc_hdr hdr;
167 * mei_wdt_ping - send wd start/ping command
169 * @wdt: mei watchdog device
171 * Return: 0 on success,
172 * negative errno code on failure
174 static int mei_wdt_ping(struct mei_wdt *wdt)
176 struct mei_wdt_start_request req;
177 const size_t req_len = sizeof(req);
180 memset(&req, 0, req_len);
181 req.hdr.command = MEI_MANAGEMENT_CONTROL;
182 req.hdr.bytecount = req_len - offsetof(struct mei_mc_hdr, subcommand);
183 req.hdr.subcommand = MEI_MC_START_WD_TIMER_REQ;
184 req.hdr.versionnumber = MEI_MC_VERSION_NUMBER;
185 req.timeout = wdt->timeout;
187 ret = mei_cldev_send(wdt->cldev, (u8 *)&req, req_len);
195 * mei_wdt_stop - send wd stop command
197 * @wdt: mei watchdog device
199 * Return: 0 on success,
200 * negative errno code on failure
202 static int mei_wdt_stop(struct mei_wdt *wdt)
204 struct mei_wdt_stop_request req;
205 const size_t req_len = sizeof(req);
208 memset(&req, 0, req_len);
209 req.hdr.command = MEI_MANAGEMENT_CONTROL;
210 req.hdr.bytecount = req_len - offsetof(struct mei_mc_hdr, subcommand);
211 req.hdr.subcommand = MEI_MC_STOP_WD_TIMER_REQ;
212 req.hdr.versionnumber = MEI_MC_VERSION_NUMBER;
214 ret = mei_cldev_send(wdt->cldev, (u8 *)&req, req_len);
222 * mei_wdt_ops_start - wd start command from the watchdog core.
224 * @wdd: watchdog device
226 * Return: 0 on success or -ENODEV;
228 static int mei_wdt_ops_start(struct watchdog_device *wdd)
230 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
232 wdt->state = MEI_WDT_START;
233 wdd->timeout = wdt->timeout;
238 * mei_wdt_ops_stop - wd stop command from the watchdog core.
240 * @wdd: watchdog device
242 * Return: 0 if success, negative errno code for failure
244 static int mei_wdt_ops_stop(struct watchdog_device *wdd)
246 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
249 if (wdt->state != MEI_WDT_RUNNING)
252 wdt->state = MEI_WDT_STOPPING;
254 ret = mei_wdt_stop(wdt);
258 wdt->state = MEI_WDT_IDLE;
264 * mei_wdt_ops_ping - wd ping command from the watchdog core.
266 * @wdd: watchdog device
268 * Return: 0 if success, negative errno code on failure
270 static int mei_wdt_ops_ping(struct watchdog_device *wdd)
272 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
275 if (wdt->state != MEI_WDT_START && wdt->state != MEI_WDT_RUNNING)
278 if (wdt->resp_required)
279 init_completion(&wdt->response);
281 wdt->state = MEI_WDT_RUNNING;
282 ret = mei_wdt_ping(wdt);
286 if (wdt->resp_required)
287 ret = wait_for_completion_killable(&wdt->response);
293 * mei_wdt_ops_set_timeout - wd set timeout command from the watchdog core.
295 * @wdd: watchdog device
296 * @timeout: timeout value to set
298 * Return: 0 if success, negative errno code for failure
300 static int mei_wdt_ops_set_timeout(struct watchdog_device *wdd,
301 unsigned int timeout)
304 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
306 /* valid value is already checked by the caller */
307 wdt->timeout = timeout;
308 wdd->timeout = timeout;
313 static const struct watchdog_ops wd_ops = {
314 .owner = THIS_MODULE,
315 .start = mei_wdt_ops_start,
316 .stop = mei_wdt_ops_stop,
317 .ping = mei_wdt_ops_ping,
318 .set_timeout = mei_wdt_ops_set_timeout,
321 /* not const as the firmware_version field need to be retrieved */
322 static struct watchdog_info wd_info = {
323 .identity = INTEL_AMT_WATCHDOG_ID,
324 .options = WDIOF_KEEPALIVEPING |
330 * __mei_wdt_is_registered - check if wdt is registered
332 * @wdt: mei watchdog device
334 * Return: true if the wdt is registered with the watchdog subsystem
335 * Locking: should be called under wdt->reg_lock
337 static inline bool __mei_wdt_is_registered(struct mei_wdt *wdt)
339 return !!watchdog_get_drvdata(&wdt->wdd);
343 * mei_wdt_unregister - unregister from the watchdog subsystem
345 * @wdt: mei watchdog device
347 static void mei_wdt_unregister(struct mei_wdt *wdt)
349 mutex_lock(&wdt->reg_lock);
351 if (__mei_wdt_is_registered(wdt)) {
352 watchdog_unregister_device(&wdt->wdd);
353 watchdog_set_drvdata(&wdt->wdd, NULL);
354 memset(&wdt->wdd, 0, sizeof(wdt->wdd));
357 mutex_unlock(&wdt->reg_lock);
361 * mei_wdt_register - register with the watchdog subsystem
363 * @wdt: mei watchdog device
365 * Return: 0 if success, negative errno code for failure
367 static int mei_wdt_register(struct mei_wdt *wdt)
372 if (!wdt || !wdt->cldev)
375 dev = &wdt->cldev->dev;
377 mutex_lock(&wdt->reg_lock);
379 if (__mei_wdt_is_registered(wdt)) {
384 wdt->wdd.info = &wd_info;
385 wdt->wdd.ops = &wd_ops;
386 wdt->wdd.parent = dev;
387 wdt->wdd.timeout = MEI_WDT_DEFAULT_TIMEOUT;
388 wdt->wdd.min_timeout = MEI_WDT_MIN_TIMEOUT;
389 wdt->wdd.max_timeout = MEI_WDT_MAX_TIMEOUT;
391 watchdog_set_drvdata(&wdt->wdd, wdt);
392 watchdog_stop_on_reboot(&wdt->wdd);
394 ret = watchdog_register_device(&wdt->wdd);
396 dev_err(dev, "unable to register watchdog device = %d.\n", ret);
397 watchdog_set_drvdata(&wdt->wdd, NULL);
400 wdt->state = MEI_WDT_IDLE;
403 mutex_unlock(&wdt->reg_lock);
407 static void mei_wdt_unregister_work(struct work_struct *work)
409 struct mei_wdt *wdt = container_of(work, struct mei_wdt, unregister);
411 mei_wdt_unregister(wdt);
415 * mei_wdt_rx - callback for data receive
419 static void mei_wdt_rx(struct mei_cl_device *cldev)
421 struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
422 struct mei_wdt_start_response res;
423 const size_t res_len = sizeof(res);
426 ret = mei_cldev_recv(wdt->cldev, (u8 *)&res, res_len);
428 dev_err(&cldev->dev, "failure in recv %d\n", ret);
432 /* Empty response can be sent on stop */
436 if (ret < sizeof(struct mei_mc_hdr)) {
437 dev_err(&cldev->dev, "recv small data %d\n", ret);
441 if (res.hdr.command != MEI_MANAGEMENT_CONTROL ||
442 res.hdr.versionnumber != MEI_MC_VERSION_NUMBER) {
443 dev_err(&cldev->dev, "wrong command received\n");
447 if (res.hdr.subcommand != MEI_MC_START_WD_TIMER_RES) {
448 dev_warn(&cldev->dev, "unsupported command %d :%s[%d]\n",
450 mei_wdt_state_str(wdt->state),
455 /* Run the unregistration in a worker as this can be
456 * run only after ping completion, otherwise the flow will
457 * deadlock on watchdog core mutex.
459 if (wdt->state == MEI_WDT_RUNNING) {
460 if (res.wdstate & MEI_WDT_WDSTATE_NOT_REQUIRED) {
461 wdt->state = MEI_WDT_NOT_REQUIRED;
462 schedule_work(&wdt->unregister);
467 if (wdt->state == MEI_WDT_PROBE) {
468 if (res.wdstate & MEI_WDT_WDSTATE_NOT_REQUIRED) {
469 wdt->state = MEI_WDT_NOT_REQUIRED;
471 /* stop the watchdog and register watchdog device */
473 mei_wdt_register(wdt);
478 dev_warn(&cldev->dev, "not in correct state %s[%d]\n",
479 mei_wdt_state_str(wdt->state), wdt->state);
482 if (!completion_done(&wdt->response))
483 complete(&wdt->response);
487 * mei_wdt_notif - callback for event notification
491 static void mei_wdt_notif(struct mei_cl_device *cldev)
493 struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
495 if (wdt->state != MEI_WDT_NOT_REQUIRED)
498 mei_wdt_register(wdt);
501 #if IS_ENABLED(CONFIG_DEBUG_FS)
503 static ssize_t mei_dbgfs_read_activation(struct file *file, char __user *ubuf,
504 size_t cnt, loff_t *ppos)
506 struct mei_wdt *wdt = file->private_data;
507 const size_t bufsz = 32;
511 mutex_lock(&wdt->reg_lock);
512 pos = scnprintf(buf, bufsz, "%s\n",
513 __mei_wdt_is_registered(wdt) ? "activated" : "deactivated");
514 mutex_unlock(&wdt->reg_lock);
516 return simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
519 static const struct file_operations dbgfs_fops_activation = {
521 .read = mei_dbgfs_read_activation,
522 .llseek = generic_file_llseek,
525 static ssize_t mei_dbgfs_read_state(struct file *file, char __user *ubuf,
526 size_t cnt, loff_t *ppos)
528 struct mei_wdt *wdt = file->private_data;
529 const size_t bufsz = 32;
533 pos = scnprintf(buf, bufsz, "state: %s\n",
534 mei_wdt_state_str(wdt->state));
536 return simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
539 static const struct file_operations dbgfs_fops_state = {
541 .read = mei_dbgfs_read_state,
542 .llseek = generic_file_llseek,
545 static void dbgfs_unregister(struct mei_wdt *wdt)
547 debugfs_remove_recursive(wdt->dbgfs_dir);
548 wdt->dbgfs_dir = NULL;
551 static int dbgfs_register(struct mei_wdt *wdt)
553 struct dentry *dir, *f;
555 dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
559 wdt->dbgfs_dir = dir;
560 f = debugfs_create_file("state", S_IRUSR, dir, wdt, &dbgfs_fops_state);
564 f = debugfs_create_file("activation", S_IRUSR,
565 dir, wdt, &dbgfs_fops_activation);
571 dbgfs_unregister(wdt);
577 static inline void dbgfs_unregister(struct mei_wdt *wdt) {}
579 static inline int dbgfs_register(struct mei_wdt *wdt)
583 #endif /* CONFIG_DEBUG_FS */
585 static int mei_wdt_probe(struct mei_cl_device *cldev,
586 const struct mei_cl_device_id *id)
591 wdt = kzalloc(sizeof(struct mei_wdt), GFP_KERNEL);
595 wdt->timeout = MEI_WDT_DEFAULT_TIMEOUT;
596 wdt->state = MEI_WDT_PROBE;
598 wdt->resp_required = mei_cldev_ver(cldev) > 0x1;
599 mutex_init(&wdt->reg_lock);
600 init_completion(&wdt->response);
601 INIT_WORK(&wdt->unregister, mei_wdt_unregister_work);
603 mei_cldev_set_drvdata(cldev, wdt);
605 ret = mei_cldev_enable(cldev);
607 dev_err(&cldev->dev, "Could not enable cl device\n");
611 ret = mei_cldev_register_rx_cb(wdt->cldev, mei_wdt_rx);
613 dev_err(&cldev->dev, "Could not reg rx event ret=%d\n", ret);
617 ret = mei_cldev_register_notif_cb(wdt->cldev, mei_wdt_notif);
618 /* on legacy devices notification is not supported
620 if (ret && ret != -EOPNOTSUPP) {
621 dev_err(&cldev->dev, "Could not reg notif event ret=%d\n", ret);
625 wd_info.firmware_version = mei_cldev_ver(cldev);
627 if (wdt->resp_required)
628 ret = mei_wdt_ping(wdt);
630 ret = mei_wdt_register(wdt);
635 if (dbgfs_register(wdt))
636 dev_warn(&cldev->dev, "cannot register debugfs\n");
641 mei_cldev_disable(cldev);
649 static int mei_wdt_remove(struct mei_cl_device *cldev)
651 struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
653 /* Free the caller in case of fw initiated or unexpected reset */
654 if (!completion_done(&wdt->response))
655 complete(&wdt->response);
657 cancel_work_sync(&wdt->unregister);
659 mei_wdt_unregister(wdt);
661 mei_cldev_disable(cldev);
663 dbgfs_unregister(wdt);
670 #define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
671 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
673 static struct mei_cl_device_id mei_wdt_tbl[] = {
674 { .uuid = MEI_UUID_WD, .version = MEI_CL_VERSION_ANY },
675 /* required last entry */
678 MODULE_DEVICE_TABLE(mei, mei_wdt_tbl);
680 static struct mei_cl_driver mei_wdt_driver = {
681 .id_table = mei_wdt_tbl,
682 .name = KBUILD_MODNAME,
684 .probe = mei_wdt_probe,
685 .remove = mei_wdt_remove,
688 module_mei_cl_driver(mei_wdt_driver);
690 MODULE_AUTHOR("Intel Corporation");
691 MODULE_LICENSE("GPL");
692 MODULE_DESCRIPTION("Device driver for Intel MEI iAMT watchdog");