rtc: pcf8523: don't return invalid date when battery is low
[linux-2.6-block.git] / drivers / watchdog / mei_wdt.c
CommitLineData
2e62c498 1// SPDX-License-Identifier: GPL-2.0
222818c3
TW
2/*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2015, Intel Corporation.
222818c3
TW
5 */
6
7#include <linux/module.h>
8#include <linux/slab.h>
9#include <linux/interrupt.h>
c9cf20ee 10#include <linux/debugfs.h>
7a23f80e 11#include <linux/completion.h>
222818c3
TW
12#include <linux/watchdog.h>
13
14#include <linux/uuid.h>
15#include <linux/mei_cl_bus.h>
16
17/*
18 * iAMT Watchdog Device
19 */
20#define INTEL_AMT_WATCHDOG_ID "iamt_wdt"
21
22#define MEI_WDT_DEFAULT_TIMEOUT 120 /* seconds */
23#define MEI_WDT_MIN_TIMEOUT 120 /* seconds */
24#define MEI_WDT_MAX_TIMEOUT 65535 /* seconds */
25
26/* Commands */
27#define MEI_MANAGEMENT_CONTROL 0x02
28
29/* MEI Management Control version number */
30#define MEI_MC_VERSION_NUMBER 0x10
31
32/* Sub Commands */
33#define MEI_MC_START_WD_TIMER_REQ 0x13
7a23f80e
TW
34#define MEI_MC_START_WD_TIMER_RES 0x83
35#define MEI_WDT_STATUS_SUCCESS 0
36#define MEI_WDT_WDSTATE_NOT_REQUIRED 0x1
222818c3
TW
37#define MEI_MC_STOP_WD_TIMER_REQ 0x14
38
39/**
40 * enum mei_wdt_state - internal watchdog state
41 *
7a23f80e 42 * @MEI_WDT_PROBE: wd in probing stage
222818c3
TW
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
7a23f80e 47 * @MEI_WDT_NOT_REQUIRED: wd device is not required
222818c3
TW
48 */
49enum mei_wdt_state {
7a23f80e 50 MEI_WDT_PROBE,
222818c3
TW
51 MEI_WDT_IDLE,
52 MEI_WDT_START,
53 MEI_WDT_RUNNING,
54 MEI_WDT_STOPPING,
7a23f80e 55 MEI_WDT_NOT_REQUIRED,
222818c3
TW
56};
57
c9cf20ee
TW
58static const char *mei_wdt_state_str(enum mei_wdt_state state)
59{
60 switch (state) {
7a23f80e
TW
61 case MEI_WDT_PROBE:
62 return "PROBE";
c9cf20ee
TW
63 case MEI_WDT_IDLE:
64 return "IDLE";
65 case MEI_WDT_START:
66 return "START";
67 case MEI_WDT_RUNNING:
68 return "RUNNING";
69 case MEI_WDT_STOPPING:
70 return "STOPPING";
7a23f80e
TW
71 case MEI_WDT_NOT_REQUIRED:
72 return "NOT_REQUIRED";
c9cf20ee
TW
73 default:
74 return "unknown";
75 }
76}
c9cf20ee 77
222818c3
TW
78/**
79 * struct mei_wdt - mei watchdog driver
80 * @wdd: watchdog device
81 *
82 * @cldev: mei watchdog client device
83 * @state: watchdog internal state
7a23f80e
TW
84 * @resp_required: ping required response
85 * @response: ping response completion
86 * @unregister: unregister worker
87 * @reg_lock: watchdog device registration lock
222818c3 88 * @timeout: watchdog current timeout
c9cf20ee
TW
89 *
90 * @dbgfs_dir: debugfs dir entry
222818c3
TW
91 */
92struct mei_wdt {
93 struct watchdog_device wdd;
94
95 struct mei_cl_device *cldev;
96 enum mei_wdt_state state;
7a23f80e
TW
97 bool resp_required;
98 struct completion response;
99 struct work_struct unregister;
100 struct mutex reg_lock;
222818c3 101 u16 timeout;
c9cf20ee
TW
102
103#if IS_ENABLED(CONFIG_DEBUG_FS)
104 struct dentry *dbgfs_dir;
105#endif /* CONFIG_DEBUG_FS */
222818c3
TW
106};
107
108/*
109 * struct mei_mc_hdr - Management Control Command Header
110 *
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)
115 */
116struct mei_mc_hdr {
117 u8 command;
118 u8 bytecount;
119 u8 subcommand;
120 u8 versionnumber;
121};
122
123/**
124 * struct mei_wdt_start_request watchdog start/ping
125 *
126 * @hdr: Management Control Command Header
127 * @timeout: timeout value
128 * @reserved: reserved (legacy)
129 */
130struct mei_wdt_start_request {
131 struct mei_mc_hdr hdr;
132 u16 timeout;
133 u8 reserved[17];
134} __packed;
135
7a23f80e
TW
136/**
137 * struct mei_wdt_start_response watchdog start/ping response
138 *
139 * @hdr: Management Control Command Header
140 * @status: operation status
141 * @wdstate: watchdog status bit mask
142 */
143struct mei_wdt_start_response {
144 struct mei_mc_hdr hdr;
145 u8 status;
146 u8 wdstate;
147} __packed;
148
222818c3
TW
149/**
150 * struct mei_wdt_stop_request - watchdog stop
151 *
152 * @hdr: Management Control Command Header
153 */
154struct mei_wdt_stop_request {
155 struct mei_mc_hdr hdr;
156} __packed;
157
158/**
159 * mei_wdt_ping - send wd start/ping command
160 *
161 * @wdt: mei watchdog device
162 *
163 * Return: 0 on success,
164 * negative errno code on failure
165 */
166static int mei_wdt_ping(struct mei_wdt *wdt)
167{
168 struct mei_wdt_start_request req;
169 const size_t req_len = sizeof(req);
170 int ret;
171
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;
178
179 ret = mei_cldev_send(wdt->cldev, (u8 *)&req, req_len);
180 if (ret < 0)
181 return ret;
182
183 return 0;
184}
185
186/**
187 * mei_wdt_stop - send wd stop command
188 *
189 * @wdt: mei watchdog device
190 *
191 * Return: 0 on success,
192 * negative errno code on failure
193 */
194static int mei_wdt_stop(struct mei_wdt *wdt)
195{
196 struct mei_wdt_stop_request req;
197 const size_t req_len = sizeof(req);
198 int ret;
199
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;
205
206 ret = mei_cldev_send(wdt->cldev, (u8 *)&req, req_len);
207 if (ret < 0)
208 return ret;
209
210 return 0;
211}
212
213/**
214 * mei_wdt_ops_start - wd start command from the watchdog core.
215 *
216 * @wdd: watchdog device
217 *
218 * Return: 0 on success or -ENODEV;
219 */
220static int mei_wdt_ops_start(struct watchdog_device *wdd)
221{
222 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
223
224 wdt->state = MEI_WDT_START;
225 wdd->timeout = wdt->timeout;
226 return 0;
227}
228
229/**
230 * mei_wdt_ops_stop - wd stop command from the watchdog core.
231 *
232 * @wdd: watchdog device
233 *
234 * Return: 0 if success, negative errno code for failure
235 */
236static int mei_wdt_ops_stop(struct watchdog_device *wdd)
237{
238 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
239 int ret;
240
241 if (wdt->state != MEI_WDT_RUNNING)
242 return 0;
243
244 wdt->state = MEI_WDT_STOPPING;
245
246 ret = mei_wdt_stop(wdt);
247 if (ret)
248 return ret;
249
250 wdt->state = MEI_WDT_IDLE;
251
252 return 0;
253}
254
255/**
256 * mei_wdt_ops_ping - wd ping command from the watchdog core.
257 *
258 * @wdd: watchdog device
259 *
260 * Return: 0 if success, negative errno code on failure
261 */
262static int mei_wdt_ops_ping(struct watchdog_device *wdd)
263{
264 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
265 int ret;
266
267 if (wdt->state != MEI_WDT_START && wdt->state != MEI_WDT_RUNNING)
268 return 0;
269
7a23f80e
TW
270 if (wdt->resp_required)
271 init_completion(&wdt->response);
272
273 wdt->state = MEI_WDT_RUNNING;
222818c3
TW
274 ret = mei_wdt_ping(wdt);
275 if (ret)
276 return ret;
277
7a23f80e
TW
278 if (wdt->resp_required)
279 ret = wait_for_completion_killable(&wdt->response);
222818c3 280
7a23f80e 281 return ret;
222818c3
TW
282}
283
284/**
285 * mei_wdt_ops_set_timeout - wd set timeout command from the watchdog core.
286 *
287 * @wdd: watchdog device
288 * @timeout: timeout value to set
289 *
290 * Return: 0 if success, negative errno code for failure
291 */
292static int mei_wdt_ops_set_timeout(struct watchdog_device *wdd,
293 unsigned int timeout)
294{
295
296 struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
297
298 /* valid value is already checked by the caller */
299 wdt->timeout = timeout;
300 wdd->timeout = timeout;
301
302 return 0;
303}
304
305static 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,
311};
312
313/* not const as the firmware_version field need to be retrieved */
314static struct watchdog_info wd_info = {
315 .identity = INTEL_AMT_WATCHDOG_ID,
316 .options = WDIOF_KEEPALIVEPING |
317 WDIOF_SETTIMEOUT |
318 WDIOF_ALARMONLY,
319};
320
7a23f80e
TW
321/**
322 * __mei_wdt_is_registered - check if wdt is registered
323 *
324 * @wdt: mei watchdog device
325 *
326 * Return: true if the wdt is registered with the watchdog subsystem
327 * Locking: should be called under wdt->reg_lock
328 */
329static inline bool __mei_wdt_is_registered(struct mei_wdt *wdt)
330{
331 return !!watchdog_get_drvdata(&wdt->wdd);
332}
333
222818c3
TW
334/**
335 * mei_wdt_unregister - unregister from the watchdog subsystem
336 *
337 * @wdt: mei watchdog device
338 */
339static void mei_wdt_unregister(struct mei_wdt *wdt)
340{
7a23f80e
TW
341 mutex_lock(&wdt->reg_lock);
342
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));
347 }
348
349 mutex_unlock(&wdt->reg_lock);
222818c3
TW
350}
351
352/**
353 * mei_wdt_register - register with the watchdog subsystem
354 *
355 * @wdt: mei watchdog device
356 *
357 * Return: 0 if success, negative errno code for failure
358 */
359static int mei_wdt_register(struct mei_wdt *wdt)
360{
361 struct device *dev;
362 int ret;
363
364 if (!wdt || !wdt->cldev)
365 return -EINVAL;
366
367 dev = &wdt->cldev->dev;
368
7a23f80e
TW
369 mutex_lock(&wdt->reg_lock);
370
371 if (__mei_wdt_is_registered(wdt)) {
372 ret = 0;
373 goto out;
374 }
375
222818c3
TW
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;
382
383 watchdog_set_drvdata(&wdt->wdd, wdt);
9eff1140
AU
384 watchdog_stop_on_reboot(&wdt->wdd);
385
222818c3
TW
386 ret = watchdog_register_device(&wdt->wdd);
387 if (ret) {
388 dev_err(dev, "unable to register watchdog device = %d.\n", ret);
389 watchdog_set_drvdata(&wdt->wdd, NULL);
390 }
391
7a23f80e
TW
392 wdt->state = MEI_WDT_IDLE;
393
394out:
395 mutex_unlock(&wdt->reg_lock);
222818c3
TW
396 return ret;
397}
398
7a23f80e
TW
399static void mei_wdt_unregister_work(struct work_struct *work)
400{
401 struct mei_wdt *wdt = container_of(work, struct mei_wdt, unregister);
402
403 mei_wdt_unregister(wdt);
404}
405
406/**
7c7a6077 407 * mei_wdt_rx - callback for data receive
7a23f80e
TW
408 *
409 * @cldev: bus device
410 */
7c7a6077 411static void mei_wdt_rx(struct mei_cl_device *cldev)
7a23f80e
TW
412{
413 struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
414 struct mei_wdt_start_response res;
415 const size_t res_len = sizeof(res);
416 int ret;
417
418 ret = mei_cldev_recv(wdt->cldev, (u8 *)&res, res_len);
419 if (ret < 0) {
420 dev_err(&cldev->dev, "failure in recv %d\n", ret);
421 return;
422 }
423
424 /* Empty response can be sent on stop */
425 if (ret == 0)
426 return;
427
428 if (ret < sizeof(struct mei_mc_hdr)) {
429 dev_err(&cldev->dev, "recv small data %d\n", ret);
430 return;
431 }
432
433 if (res.hdr.command != MEI_MANAGEMENT_CONTROL ||
434 res.hdr.versionnumber != MEI_MC_VERSION_NUMBER) {
435 dev_err(&cldev->dev, "wrong command received\n");
436 return;
437 }
438
439 if (res.hdr.subcommand != MEI_MC_START_WD_TIMER_RES) {
440 dev_warn(&cldev->dev, "unsupported command %d :%s[%d]\n",
441 res.hdr.subcommand,
442 mei_wdt_state_str(wdt->state),
443 wdt->state);
444 return;
445 }
446
447 /* Run the unregistration in a worker as this can be
448 * run only after ping completion, otherwise the flow will
449 * deadlock on watchdog core mutex.
450 */
451 if (wdt->state == MEI_WDT_RUNNING) {
452 if (res.wdstate & MEI_WDT_WDSTATE_NOT_REQUIRED) {
453 wdt->state = MEI_WDT_NOT_REQUIRED;
454 schedule_work(&wdt->unregister);
455 }
456 goto out;
457 }
458
459 if (wdt->state == MEI_WDT_PROBE) {
460 if (res.wdstate & MEI_WDT_WDSTATE_NOT_REQUIRED) {
461 wdt->state = MEI_WDT_NOT_REQUIRED;
462 } else {
463 /* stop the watchdog and register watchdog device */
464 mei_wdt_stop(wdt);
465 mei_wdt_register(wdt);
466 }
467 return;
468 }
469
470 dev_warn(&cldev->dev, "not in correct state %s[%d]\n",
471 mei_wdt_state_str(wdt->state), wdt->state);
472
473out:
474 if (!completion_done(&wdt->response))
475 complete(&wdt->response);
476}
477
3a20a5c3 478/*
7c7a6077 479 * mei_wdt_notif - callback for event notification
3a20a5c3
TW
480 *
481 * @cldev: bus device
482 */
7c7a6077 483static void mei_wdt_notif(struct mei_cl_device *cldev)
3a20a5c3
TW
484{
485 struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
486
487 if (wdt->state != MEI_WDT_NOT_REQUIRED)
488 return;
489
490 mei_wdt_register(wdt);
491}
492
c9cf20ee
TW
493#if IS_ENABLED(CONFIG_DEBUG_FS)
494
ad1cd720
TW
495static ssize_t mei_dbgfs_read_activation(struct file *file, char __user *ubuf,
496 size_t cnt, loff_t *ppos)
497{
498 struct mei_wdt *wdt = file->private_data;
499 const size_t bufsz = 32;
500 char buf[32];
501 ssize_t pos;
502
503 mutex_lock(&wdt->reg_lock);
504 pos = scnprintf(buf, bufsz, "%s\n",
505 __mei_wdt_is_registered(wdt) ? "activated" : "deactivated");
506 mutex_unlock(&wdt->reg_lock);
507
508 return simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
509}
510
511static const struct file_operations dbgfs_fops_activation = {
512 .open = simple_open,
513 .read = mei_dbgfs_read_activation,
514 .llseek = generic_file_llseek,
515};
516
c9cf20ee
TW
517static ssize_t mei_dbgfs_read_state(struct file *file, char __user *ubuf,
518 size_t cnt, loff_t *ppos)
519{
520 struct mei_wdt *wdt = file->private_data;
7692a982 521 char buf[32];
c9cf20ee
TW
522 ssize_t pos;
523
7692a982
TW
524 pos = scnprintf(buf, sizeof(buf), "state: %s\n",
525 mei_wdt_state_str(wdt->state));
c9cf20ee
TW
526
527 return simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
528}
529
530static const struct file_operations dbgfs_fops_state = {
531 .open = simple_open,
532 .read = mei_dbgfs_read_state,
533 .llseek = generic_file_llseek,
534};
535
536static void dbgfs_unregister(struct mei_wdt *wdt)
537{
538 debugfs_remove_recursive(wdt->dbgfs_dir);
539 wdt->dbgfs_dir = NULL;
540}
541
542static int dbgfs_register(struct mei_wdt *wdt)
543{
544 struct dentry *dir, *f;
545
546 dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
547 if (!dir)
548 return -ENOMEM;
549
550 wdt->dbgfs_dir = dir;
551 f = debugfs_create_file("state", S_IRUSR, dir, wdt, &dbgfs_fops_state);
552 if (!f)
553 goto err;
554
ad1cd720
TW
555 f = debugfs_create_file("activation", S_IRUSR,
556 dir, wdt, &dbgfs_fops_activation);
557 if (!f)
558 goto err;
559
c9cf20ee
TW
560 return 0;
561err:
562 dbgfs_unregister(wdt);
563 return -ENODEV;
564}
565
566#else
567
568static inline void dbgfs_unregister(struct mei_wdt *wdt) {}
569
570static inline int dbgfs_register(struct mei_wdt *wdt)
571{
572 return 0;
573}
574#endif /* CONFIG_DEBUG_FS */
575
222818c3
TW
576static int mei_wdt_probe(struct mei_cl_device *cldev,
577 const struct mei_cl_device_id *id)
578{
579 struct mei_wdt *wdt;
580 int ret;
581
582 wdt = kzalloc(sizeof(struct mei_wdt), GFP_KERNEL);
583 if (!wdt)
584 return -ENOMEM;
585
586 wdt->timeout = MEI_WDT_DEFAULT_TIMEOUT;
7a23f80e 587 wdt->state = MEI_WDT_PROBE;
222818c3 588 wdt->cldev = cldev;
7a23f80e
TW
589 wdt->resp_required = mei_cldev_ver(cldev) > 0x1;
590 mutex_init(&wdt->reg_lock);
591 init_completion(&wdt->response);
592 INIT_WORK(&wdt->unregister, mei_wdt_unregister_work);
593
222818c3
TW
594 mei_cldev_set_drvdata(cldev, wdt);
595
596 ret = mei_cldev_enable(cldev);
597 if (ret < 0) {
598 dev_err(&cldev->dev, "Could not enable cl device\n");
599 goto err_out;
600 }
601
7c7a6077
AU
602 ret = mei_cldev_register_rx_cb(wdt->cldev, mei_wdt_rx);
603 if (ret) {
604 dev_err(&cldev->dev, "Could not reg rx event ret=%d\n", ret);
605 goto err_disable;
606 }
3a20a5c3 607
7c7a6077 608 ret = mei_cldev_register_notif_cb(wdt->cldev, mei_wdt_notif);
3a20a5c3 609 /* on legacy devices notification is not supported
3a20a5c3
TW
610 */
611 if (ret && ret != -EOPNOTSUPP) {
7c7a6077 612 dev_err(&cldev->dev, "Could not reg notif event ret=%d\n", ret);
7a23f80e
TW
613 goto err_disable;
614 }
615
222818c3
TW
616 wd_info.firmware_version = mei_cldev_ver(cldev);
617
7a23f80e
TW
618 if (wdt->resp_required)
619 ret = mei_wdt_ping(wdt);
620 else
621 ret = mei_wdt_register(wdt);
622
222818c3
TW
623 if (ret)
624 goto err_disable;
625
c9cf20ee
TW
626 if (dbgfs_register(wdt))
627 dev_warn(&cldev->dev, "cannot register debugfs\n");
628
222818c3
TW
629 return 0;
630
631err_disable:
632 mei_cldev_disable(cldev);
633
634err_out:
635 kfree(wdt);
636
637 return ret;
638}
639
640static int mei_wdt_remove(struct mei_cl_device *cldev)
641{
642 struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
643
7a23f80e
TW
644 /* Free the caller in case of fw initiated or unexpected reset */
645 if (!completion_done(&wdt->response))
646 complete(&wdt->response);
647
648 cancel_work_sync(&wdt->unregister);
649
222818c3
TW
650 mei_wdt_unregister(wdt);
651
652 mei_cldev_disable(cldev);
653
c9cf20ee
TW
654 dbgfs_unregister(wdt);
655
222818c3
TW
656 kfree(wdt);
657
658 return 0;
659}
660
661#define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
662 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
663
4cb30b04 664static const struct mei_cl_device_id mei_wdt_tbl[] = {
7a23f80e 665 { .uuid = MEI_UUID_WD, .version = MEI_CL_VERSION_ANY },
222818c3
TW
666 /* required last entry */
667 { }
668};
669MODULE_DEVICE_TABLE(mei, mei_wdt_tbl);
670
671static struct mei_cl_driver mei_wdt_driver = {
672 .id_table = mei_wdt_tbl,
673 .name = KBUILD_MODNAME,
674
675 .probe = mei_wdt_probe,
676 .remove = mei_wdt_remove,
677};
678
b2a2f708 679module_mei_cl_driver(mei_wdt_driver);
222818c3
TW
680
681MODULE_AUTHOR("Intel Corporation");
2e62c498 682MODULE_LICENSE("GPL v2");
222818c3 683MODULE_DESCRIPTION("Device driver for Intel MEI iAMT watchdog");