Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / misc / mei / wd.c
blobf70945ed96f6f82ab6f6f62d5485cea9fa39ea16
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/pci.h>
21 #include <linux/sched.h>
22 #include <linux/watchdog.h>
24 #include <linux/mei.h>
26 #include "mei_dev.h"
27 #include "hbm.h"
28 #include "hw-me.h"
29 #include "client.h"
31 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
32 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
35 * AMT Watchdog Device
37 #define INTEL_AMT_WATCHDOG_ID "INTCAMT"
39 /* UUIDs for AMT F/W clients */
40 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
41 0x9D, 0xA9, 0x15, 0x14, 0xCB,
42 0x32, 0xAB);
44 static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
46 dev_dbg(&dev->pdev->dev, "wd: set timeout=%d.\n", timeout);
47 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
48 memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
51 /**
52 * mei_wd_host_init - connect to the watchdog client
54 * @dev: the device structure
56 * returns -ENENT if wd client cannot be found
57 * -EIO if write has failed
58 * 0 on success
60 int mei_wd_host_init(struct mei_device *dev)
62 struct mei_cl *cl = &dev->wd_cl;
63 int id;
64 int ret;
66 mei_cl_init(cl, dev);
68 dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
69 dev->wd_state = MEI_WD_IDLE;
72 /* check for valid client id */
73 id = mei_me_cl_by_uuid(dev, &mei_wd_guid);
74 if (id < 0) {
75 dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
76 return id;
79 cl->me_client_id = dev->me_clients[id].client_id;
81 ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
83 if (ret < 0) {
84 dev_info(&dev->pdev->dev, "wd: failed link client\n");
85 return ret;
88 cl->state = MEI_FILE_CONNECTING;
90 if (mei_hbm_cl_connect_req(dev, cl)) {
91 dev_err(&dev->pdev->dev, "wd: failed to connect to the client\n");
92 cl->state = MEI_FILE_DISCONNECTED;
93 cl->host_client_id = 0;
94 return -EIO;
96 cl->timer_count = MEI_CONNECT_TIMEOUT;
98 return 0;
102 * mei_wd_send - sends watch dog message to fw.
104 * @dev: the device structure
106 * returns 0 if success,
107 * -EIO when message send fails
108 * -EINVAL when invalid message is to be sent
110 int mei_wd_send(struct mei_device *dev)
112 struct mei_msg_hdr hdr;
114 hdr.host_addr = dev->wd_cl.host_client_id;
115 hdr.me_addr = dev->wd_cl.me_client_id;
116 hdr.msg_complete = 1;
117 hdr.reserved = 0;
118 hdr.internal = 0;
120 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
121 hdr.length = MEI_WD_START_MSG_SIZE;
122 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
123 hdr.length = MEI_WD_STOP_MSG_SIZE;
124 else
125 return -EINVAL;
127 return mei_write_message(dev, &hdr, dev->wd_data);
131 * mei_wd_stop - sends watchdog stop message to fw.
133 * @dev: the device structure
134 * @preserve: indicate if to keep the timeout value
136 * returns 0 if success,
137 * -EIO when message send fails
138 * -EINVAL when invalid message is to be sent
140 int mei_wd_stop(struct mei_device *dev)
142 int ret;
144 if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
145 dev->wd_state != MEI_WD_RUNNING)
146 return 0;
148 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
150 dev->wd_state = MEI_WD_STOPPING;
152 ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
153 if (ret < 0)
154 goto out;
156 if (ret && dev->hbuf_is_ready) {
157 ret = 0;
158 dev->hbuf_is_ready = false;
160 if (!mei_wd_send(dev)) {
161 ret = mei_cl_flow_ctrl_reduce(&dev->wd_cl);
162 if (ret)
163 goto out;
164 } else {
165 dev_err(&dev->pdev->dev, "wd: send stop failed\n");
168 dev->wd_pending = false;
169 } else {
170 dev->wd_pending = true;
173 mutex_unlock(&dev->device_lock);
175 ret = wait_event_interruptible_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 dev_dbg(&dev->pdev->dev, "wd: stop completed ret=%d.\n", ret);
181 ret = 0;
182 } else {
183 if (!ret)
184 ret = -ETIMEDOUT;
185 dev_warn(&dev->pdev->dev,
186 "wd: stop failed to complete ret=%d.\n", ret);
189 out:
190 return ret;
194 * mei_wd_ops_start - wd start command from the watchdog core.
196 * @wd_dev - watchdog device struct
198 * returns 0 if success, negative errno code for failure
200 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
202 int err = -ENODEV;
203 struct mei_device *dev;
205 dev = watchdog_get_drvdata(wd_dev);
206 if (!dev)
207 return -ENODEV;
209 mutex_lock(&dev->device_lock);
211 if (dev->dev_state != MEI_DEV_ENABLED) {
212 dev_dbg(&dev->pdev->dev,
213 "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
214 mei_dev_state_str(dev->dev_state));
215 goto end_unlock;
218 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
219 dev_dbg(&dev->pdev->dev,
220 "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 * returns 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 * returns 0 if success, negative errno code for failure
261 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
263 int ret = 0;
264 struct mei_device *dev;
266 dev = watchdog_get_drvdata(wd_dev);
267 if (!dev)
268 return -ENODEV;
270 mutex_lock(&dev->device_lock);
272 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
273 dev_err(&dev->pdev->dev, "wd: not connected.\n");
274 ret = -ENODEV;
275 goto end;
278 dev->wd_state = MEI_WD_RUNNING;
280 /* Check if we can send the ping to HW*/
281 if (dev->hbuf_is_ready && mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
283 dev->hbuf_is_ready = false;
284 dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
286 if (mei_wd_send(dev)) {
287 dev_err(&dev->pdev->dev, "wd: send failed.\n");
288 ret = -EIO;
289 goto end;
292 if (mei_cl_flow_ctrl_reduce(&dev->wd_cl)) {
293 dev_err(&dev->pdev->dev,
294 "wd: mei_cl_flow_ctrl_reduce() failed.\n");
295 ret = -EIO;
296 goto end;
299 } else {
300 dev->wd_pending = true;
303 end:
304 mutex_unlock(&dev->device_lock);
305 return ret;
309 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
311 * @wd_dev - watchdog device struct
312 * @timeout - timeout value to set
314 * returns 0 if success, negative errno code for failure
316 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
317 unsigned int timeout)
319 struct mei_device *dev;
321 dev = watchdog_get_drvdata(wd_dev);
322 if (!dev)
323 return -ENODEV;
325 /* Check Timeout value */
326 if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
327 return -EINVAL;
329 mutex_lock(&dev->device_lock);
331 dev->wd_timeout = timeout;
332 wd_dev->timeout = timeout;
333 mei_wd_set_start_timeout(dev, dev->wd_timeout);
335 mutex_unlock(&dev->device_lock);
337 return 0;
341 * Watchdog Device structs
343 static const struct watchdog_ops wd_ops = {
344 .owner = THIS_MODULE,
345 .start = mei_wd_ops_start,
346 .stop = mei_wd_ops_stop,
347 .ping = mei_wd_ops_ping,
348 .set_timeout = mei_wd_ops_set_timeout,
350 static const struct watchdog_info wd_info = {
351 .identity = INTEL_AMT_WATCHDOG_ID,
352 .options = WDIOF_KEEPALIVEPING |
353 WDIOF_SETTIMEOUT |
354 WDIOF_ALARMONLY,
357 static struct watchdog_device amt_wd_dev = {
358 .info = &wd_info,
359 .ops = &wd_ops,
360 .timeout = MEI_WD_DEFAULT_TIMEOUT,
361 .min_timeout = MEI_WD_MIN_TIMEOUT,
362 .max_timeout = MEI_WD_MAX_TIMEOUT,
366 void mei_watchdog_register(struct mei_device *dev)
368 if (watchdog_register_device(&amt_wd_dev)) {
369 dev_err(&dev->pdev->dev,
370 "wd: unable to register watchdog device.\n");
371 return;
374 dev_dbg(&dev->pdev->dev,
375 "wd: successfully register watchdog interface.\n");
376 watchdog_set_drvdata(&amt_wd_dev, dev);
379 void mei_watchdog_unregister(struct mei_device *dev)
381 if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
382 return;
384 watchdog_set_drvdata(&amt_wd_dev, NULL);
385 watchdog_unregister_device(&amt_wd_dev);