Linux 4.2.1
[linux/fpc-iii.git] / drivers / misc / mei / wd.c
blob2bc0f5089f829ea76477dbf2648a5c7edf77853f
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/device.h>
20 #include <linux/sched.h>
21 #include <linux/watchdog.h>
23 #include <linux/mei.h>
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
29 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
30 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
33 * AMT Watchdog Device
35 #define INTEL_AMT_WATCHDOG_ID "INTCAMT"
37 /* UUIDs for AMT F/W clients */
38 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
39 0x9D, 0xA9, 0x15, 0x14, 0xCB,
40 0x32, 0xAB);
42 static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
44 dev_dbg(dev->dev, "wd: set timeout=%d.\n", timeout);
45 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
46 memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
49 /**
50 * mei_wd_host_init - connect to the watchdog client
52 * @dev: the device structure
53 * @me_cl: me client
55 * Return: -ENOTTY if wd client cannot be found
56 * -EIO if write has failed
57 * 0 on success
59 int mei_wd_host_init(struct mei_device *dev, struct mei_me_client *me_cl)
61 struct mei_cl *cl = &dev->wd_cl;
62 int ret;
64 mei_cl_init(cl, dev);
66 dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
67 dev->wd_state = MEI_WD_IDLE;
69 ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
70 if (ret < 0) {
71 dev_info(dev->dev, "wd: failed link client\n");
72 return ret;
75 ret = mei_cl_connect(cl, me_cl, NULL);
76 if (ret) {
77 dev_err(dev->dev, "wd: failed to connect = %d\n", ret);
78 mei_cl_unlink(cl);
79 return ret;
82 ret = mei_watchdog_register(dev);
83 if (ret) {
84 mei_cl_disconnect(cl);
85 mei_cl_unlink(cl);
87 return ret;
90 /**
91 * mei_wd_send - sends watch dog message to fw.
93 * @dev: the device structure
95 * Return: 0 if success,
96 * -EIO when message send fails
97 * -EINVAL when invalid message is to be sent
98 * -ENODEV on flow control failure
100 int mei_wd_send(struct mei_device *dev)
102 struct mei_cl *cl = &dev->wd_cl;
103 struct mei_msg_hdr hdr;
104 int ret;
106 hdr.host_addr = cl->host_client_id;
107 hdr.me_addr = mei_cl_me_id(cl);
108 hdr.msg_complete = 1;
109 hdr.reserved = 0;
110 hdr.internal = 0;
112 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
113 hdr.length = MEI_WD_START_MSG_SIZE;
114 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
115 hdr.length = MEI_WD_STOP_MSG_SIZE;
116 else {
117 dev_err(dev->dev, "wd: invalid message is to be sent, aborting\n");
118 return -EINVAL;
121 ret = mei_write_message(dev, &hdr, dev->wd_data);
122 if (ret) {
123 dev_err(dev->dev, "wd: write message failed\n");
124 return ret;
127 ret = mei_cl_flow_ctrl_reduce(cl);
128 if (ret) {
129 dev_err(dev->dev, "wd: flow_ctrl_reduce failed.\n");
130 return ret;
133 return 0;
137 * mei_wd_stop - sends watchdog stop message to fw.
139 * @dev: the device structure
141 * Return: 0 if success
142 * on error:
143 * -EIO when message send fails
144 * -EINVAL when invalid message is to be sent
145 * -ETIME on message timeout
147 int mei_wd_stop(struct mei_device *dev)
149 struct mei_cl *cl = &dev->wd_cl;
150 int ret;
152 if (!mei_cl_is_connected(cl) ||
153 dev->wd_state != MEI_WD_RUNNING)
154 return 0;
156 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
158 dev->wd_state = MEI_WD_STOPPING;
160 ret = mei_cl_flow_ctrl_creds(cl);
161 if (ret < 0)
162 goto err;
164 if (ret && mei_hbuf_acquire(dev)) {
165 ret = mei_wd_send(dev);
166 if (ret)
167 goto err;
168 dev->wd_pending = false;
169 } else {
170 dev->wd_pending = true;
173 mutex_unlock(&dev->device_lock);
175 ret = wait_event_timeout(dev->wait_stop_wd,
176 dev->wd_state == MEI_WD_IDLE,
177 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
178 mutex_lock(&dev->device_lock);
179 if (dev->wd_state != MEI_WD_IDLE) {
180 /* timeout */
181 ret = -ETIME;
182 dev_warn(dev->dev, "wd: stop failed to complete ret=%d\n", ret);
183 goto err;
185 dev_dbg(dev->dev, "wd: stop completed after %u msec\n",
186 MEI_WD_STOP_TIMEOUT - jiffies_to_msecs(ret));
187 return 0;
188 err:
189 return ret;
193 * mei_wd_ops_start - wd start command from the watchdog core.
195 * @wd_dev: watchdog device struct
197 * Return: 0 if success, negative errno code for failure
199 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
201 struct mei_device *dev;
202 struct mei_cl *cl;
203 int err = -ENODEV;
205 dev = watchdog_get_drvdata(wd_dev);
206 if (!dev)
207 return -ENODEV;
209 cl = &dev->wd_cl;
211 mutex_lock(&dev->device_lock);
213 if (dev->dev_state != MEI_DEV_ENABLED) {
214 dev_dbg(dev->dev, "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
215 mei_dev_state_str(dev->dev_state));
216 goto end_unlock;
219 if (!mei_cl_is_connected(cl)) {
220 cl_dbg(dev, cl, "MEI Driver is not connected to Watchdog Client\n");
221 goto end_unlock;
224 mei_wd_set_start_timeout(dev, dev->wd_timeout);
226 err = 0;
227 end_unlock:
228 mutex_unlock(&dev->device_lock);
229 return err;
233 * mei_wd_ops_stop - wd stop command from the watchdog core.
235 * @wd_dev: watchdog device struct
237 * Return: 0 if success, negative errno code for failure
239 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
241 struct mei_device *dev;
243 dev = watchdog_get_drvdata(wd_dev);
244 if (!dev)
245 return -ENODEV;
247 mutex_lock(&dev->device_lock);
248 mei_wd_stop(dev);
249 mutex_unlock(&dev->device_lock);
251 return 0;
255 * mei_wd_ops_ping - wd ping command from the watchdog core.
257 * @wd_dev: watchdog device struct
259 * Return: 0 if success, negative errno code for failure
261 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
263 struct mei_device *dev;
264 struct mei_cl *cl;
265 int ret;
267 dev = watchdog_get_drvdata(wd_dev);
268 if (!dev)
269 return -ENODEV;
271 cl = &dev->wd_cl;
273 mutex_lock(&dev->device_lock);
275 if (!mei_cl_is_connected(cl)) {
276 cl_err(dev, cl, "wd: not connected.\n");
277 ret = -ENODEV;
278 goto end;
281 dev->wd_state = MEI_WD_RUNNING;
283 ret = mei_cl_flow_ctrl_creds(cl);
284 if (ret < 0)
285 goto end;
287 /* Check if we can send the ping to HW*/
288 if (ret && mei_hbuf_acquire(dev)) {
289 dev_dbg(dev->dev, "wd: sending ping\n");
291 ret = mei_wd_send(dev);
292 if (ret)
293 goto end;
294 dev->wd_pending = false;
295 } else {
296 dev->wd_pending = true;
299 end:
300 mutex_unlock(&dev->device_lock);
301 return ret;
305 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
307 * @wd_dev: watchdog device struct
308 * @timeout: timeout value to set
310 * Return: 0 if success, negative errno code for failure
312 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
313 unsigned int timeout)
315 struct mei_device *dev;
317 dev = watchdog_get_drvdata(wd_dev);
318 if (!dev)
319 return -ENODEV;
321 /* Check Timeout value */
322 if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
323 return -EINVAL;
325 mutex_lock(&dev->device_lock);
327 dev->wd_timeout = timeout;
328 wd_dev->timeout = timeout;
329 mei_wd_set_start_timeout(dev, dev->wd_timeout);
331 mutex_unlock(&dev->device_lock);
333 return 0;
337 * Watchdog Device structs
339 static const struct watchdog_ops wd_ops = {
340 .owner = THIS_MODULE,
341 .start = mei_wd_ops_start,
342 .stop = mei_wd_ops_stop,
343 .ping = mei_wd_ops_ping,
344 .set_timeout = mei_wd_ops_set_timeout,
346 static const struct watchdog_info wd_info = {
347 .identity = INTEL_AMT_WATCHDOG_ID,
348 .options = WDIOF_KEEPALIVEPING |
349 WDIOF_SETTIMEOUT |
350 WDIOF_ALARMONLY,
353 static struct watchdog_device amt_wd_dev = {
354 .info = &wd_info,
355 .ops = &wd_ops,
356 .timeout = MEI_WD_DEFAULT_TIMEOUT,
357 .min_timeout = MEI_WD_MIN_TIMEOUT,
358 .max_timeout = MEI_WD_MAX_TIMEOUT,
362 int mei_watchdog_register(struct mei_device *dev)
365 int ret;
367 /* unlock to perserve correct locking order */
368 mutex_unlock(&dev->device_lock);
369 ret = watchdog_register_device(&amt_wd_dev);
370 mutex_lock(&dev->device_lock);
371 if (ret) {
372 dev_err(dev->dev, "wd: unable to register watchdog device = %d.\n",
373 ret);
374 return ret;
377 dev_dbg(dev->dev, "wd: successfully register watchdog interface.\n");
378 watchdog_set_drvdata(&amt_wd_dev, dev);
379 return 0;
382 void mei_watchdog_unregister(struct mei_device *dev)
384 if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
385 return;
387 watchdog_set_drvdata(&amt_wd_dev, NULL);
388 watchdog_unregister_device(&amt_wd_dev);