Linux Audio

Check our new training course

Buildroot integration, development and maintenance

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