Merge branch 'x86-acpi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / drivers / misc / mei / wd.c
CommitLineData
3ceb3e66
OW
1/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
733ba91c 4 * Copyright (c) 2003-2012, Intel Corporation.
3ceb3e66
OW
5 *
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.
9 *
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.
14 *
15 */
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>
9ce178e5 22#include <linux/watchdog.h>
3ceb3e66
OW
23
24#include "mei_dev.h"
25#include "hw.h"
26#include "interface.h"
4f3afe1d 27#include <linux/mei.h>
3ceb3e66 28
3ceb3e66
OW
29static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
30static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
31
32const u8 mei_wd_state_independence_msg[3][4] = {
33 {0x05, 0x02, 0x51, 0x10},
34 {0x05, 0x02, 0x52, 0x10},
35 {0x07, 0x02, 0x01, 0x10}
36};
37
70cd5337
TW
38/*
39 * AMT Watchdog Device
40 */
41#define INTEL_AMT_WATCHDOG_ID "INTCAMT"
42
3ceb3e66
OW
43/* UUIDs for AMT F/W clients */
44const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
45 0x9D, 0xA9, 0x15, 0x14, 0xCB,
46 0x32, 0xAB);
47
5192dda1 48static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
3ceb3e66 49{
13c398aa 50 dev_dbg(&dev->pdev->dev, "wd: set timeout=%d.\n", timeout);
248ffdf7
TW
51 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
52 memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
3ceb3e66
OW
53}
54
55/**
7cb1ba9b 56 * mei_wd_host_init - connect to the watchdog client
3ceb3e66
OW
57 *
58 * @dev: the device structure
9487eb0a
TW
59 * returns -ENENT if wd client cannot be found
60 * -EIO if write has failed
7cb1ba9b 61 * 0 on success
3ceb3e66 62 */
9487eb0a 63int mei_wd_host_init(struct mei_device *dev)
3ceb3e66 64{
ff8b2f4e 65 int id;
c95efb74 66 mei_cl_init(&dev->wd_cl, dev);
3ceb3e66
OW
67
68 /* look for WD client and connect to it */
69 dev->wd_cl.state = MEI_FILE_DISCONNECTED;
248ffdf7 70 dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
c216fdeb 71 dev->wd_state = MEI_WD_IDLE;
3ceb3e66 72
ff8b2f4e
TW
73 /* Connect WD ME client to the host client */
74 id = mei_me_cl_link(dev, &dev->wd_cl,
2ad64ba8 75 &mei_wd_guid, MEI_WD_HOST_CLIENT_ID);
3ceb3e66 76
ff8b2f4e 77 if (id < 0) {
9487eb0a
TW
78 dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
79 return -ENOENT;
80 }
81
82 if (mei_connect(dev, &dev->wd_cl)) {
83 dev_err(&dev->pdev->dev, "wd: failed to connect to the client\n");
84 dev->wd_cl.state = MEI_FILE_DISCONNECTED;
85 dev->wd_cl.host_client_id = 0;
86 return -EIO;
3ceb3e66 87 }
3870c320 88 dev->wd_cl.timer_count = MEI_CONNECT_TIMEOUT;
617aa396 89
9487eb0a 90 return 0;
3ceb3e66
OW
91}
92
93/**
94 * mei_wd_send - sends watch dog message to fw.
95 *
96 * @dev: the device structure
97 *
98 * returns 0 if success,
99 * -EIO when message send fails
100 * -EINVAL when invalid message is to be sent
101 */
102int mei_wd_send(struct mei_device *dev)
103{
104 struct mei_msg_hdr *mei_hdr;
105
106 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
107 mei_hdr->host_addr = dev->wd_cl.host_client_id;
108 mei_hdr->me_addr = dev->wd_cl.me_client_id;
109 mei_hdr->msg_complete = 1;
110 mei_hdr->reserved = 0;
111
248ffdf7
TW
112 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
113 mei_hdr->length = MEI_WD_START_MSG_SIZE;
114 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
115 mei_hdr->length = MEI_WD_STOP_MSG_SIZE;
3ceb3e66
OW
116 else
117 return -EINVAL;
118
1ccb7b62 119 return mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length);
3ceb3e66
OW
120}
121
d8deca31
OW
122/**
123 * mei_wd_stop - sends watchdog stop message to fw.
124 *
125 * @dev: the device structure
126 * @preserve: indicate if to keep the timeout value
127 *
128 * returns 0 if success,
129 * -EIO when message send fails
130 * -EINVAL when invalid message is to be sent
131 */
c216fdeb 132int mei_wd_stop(struct mei_device *dev)
3ceb3e66
OW
133{
134 int ret;
3ceb3e66 135
c216fdeb
TW
136 if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
137 dev->wd_state != MEI_WD_RUNNING)
3ceb3e66
OW
138 return 0;
139
248ffdf7 140 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
c216fdeb
TW
141
142 dev->wd_state = MEI_WD_STOPPING;
3ceb3e66
OW
143
144 ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
145 if (ret < 0)
146 goto out;
147
148 if (ret && dev->mei_host_buffer_is_empty) {
149 ret = 0;
eb9af0ac 150 dev->mei_host_buffer_is_empty = false;
3ceb3e66
OW
151
152 if (!mei_wd_send(dev)) {
153 ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
154 if (ret)
155 goto out;
156 } else {
13c398aa 157 dev_err(&dev->pdev->dev, "wd: send stop failed\n");
3ceb3e66
OW
158 }
159
eb9af0ac 160 dev->wd_pending = false;
3ceb3e66 161 } else {
eb9af0ac 162 dev->wd_pending = true;
3ceb3e66 163 }
c216fdeb 164
3ceb3e66
OW
165 mutex_unlock(&dev->device_lock);
166
167 ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
c216fdeb
TW
168 dev->wd_state == MEI_WD_IDLE,
169 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
3ceb3e66 170 mutex_lock(&dev->device_lock);
c216fdeb 171 if (dev->wd_state == MEI_WD_IDLE) {
13c398aa 172 dev_dbg(&dev->pdev->dev, "wd: stop completed ret=%d.\n", ret);
a534bb6e
TW
173 ret = 0;
174 } else {
175 if (!ret)
176 ret = -ETIMEDOUT;
177 dev_warn(&dev->pdev->dev,
13c398aa 178 "wd: stop failed to complete ret=%d.\n", ret);
a534bb6e 179 }
3ceb3e66 180
3ceb3e66
OW
181out:
182 return ret;
183}
184
4a3cafd5
OW
185/*
186 * mei_wd_ops_start - wd start command from the watchdog core.
187 *
188 * @wd_dev - watchdog device struct
189 *
190 * returns 0 if success, negative errno code for failure
191 */
192static int mei_wd_ops_start(struct watchdog_device *wd_dev)
193{
194 int err = -ENODEV;
195 struct mei_device *dev;
196
09649a85 197 dev = watchdog_get_drvdata(wd_dev);
4a3cafd5
OW
198 if (!dev)
199 return -ENODEV;
200
201 mutex_lock(&dev->device_lock);
202
b210d750 203 if (dev->dev_state != MEI_DEV_ENABLED) {
13c398aa 204 dev_dbg(&dev->pdev->dev,
b210d750
TW
205 "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
206 mei_dev_state_str(dev->dev_state));
4a3cafd5
OW
207 goto end_unlock;
208 }
209
210 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
13c398aa
TW
211 dev_dbg(&dev->pdev->dev,
212 "MEI Driver is not connected to Watchdog Client\n");
4a3cafd5
OW
213 goto end_unlock;
214 }
215
55d33856 216 mei_wd_set_start_timeout(dev, dev->wd_timeout);
4a3cafd5
OW
217
218 err = 0;
219end_unlock:
220 mutex_unlock(&dev->device_lock);
221 return err;
222}
223
224/*
225 * mei_wd_ops_stop - wd stop command from the watchdog core.
226 *
227 * @wd_dev - watchdog device struct
228 *
229 * returns 0 if success, negative errno code for failure
230 */
231static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
232{
233 struct mei_device *dev;
4a3cafd5 234
09649a85 235 dev = watchdog_get_drvdata(wd_dev);
4a3cafd5
OW
236 if (!dev)
237 return -ENODEV;
238
239 mutex_lock(&dev->device_lock);
c216fdeb 240 mei_wd_stop(dev);
4a3cafd5
OW
241 mutex_unlock(&dev->device_lock);
242
243 return 0;
244}
245
8c4a59a7
OW
246/*
247 * mei_wd_ops_ping - wd ping command from the watchdog core.
248 *
249 * @wd_dev - watchdog device struct
250 *
251 * returns 0 if success, negative errno code for failure
252 */
253static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
254{
255 int ret = 0;
256 struct mei_device *dev;
8c4a59a7 257
09649a85 258 dev = watchdog_get_drvdata(wd_dev);
8c4a59a7
OW
259 if (!dev)
260 return -ENODEV;
261
262 mutex_lock(&dev->device_lock);
263
264 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
13c398aa 265 dev_err(&dev->pdev->dev, "wd: not connected.\n");
8c4a59a7
OW
266 ret = -ENODEV;
267 goto end;
268 }
269
c216fdeb
TW
270 dev->wd_state = MEI_WD_RUNNING;
271
8c4a59a7
OW
272 /* Check if we can send the ping to HW*/
273 if (dev->mei_host_buffer_is_empty &&
274 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
275
276 dev->mei_host_buffer_is_empty = false;
13c398aa 277 dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
8c4a59a7
OW
278
279 if (mei_wd_send(dev)) {
13c398aa 280 dev_err(&dev->pdev->dev, "wd: send failed.\n");
8c4a59a7
OW
281 ret = -EIO;
282 goto end;
283 }
284
285 if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
13c398aa
TW
286 dev_err(&dev->pdev->dev,
287 "wd: mei_flow_ctrl_reduce() failed.\n");
8c4a59a7
OW
288 ret = -EIO;
289 goto end;
290 }
291
292 } else {
293 dev->wd_pending = true;
294 }
295
296end:
297 mutex_unlock(&dev->device_lock);
298 return ret;
299}
300
55d33856
OW
301/*
302 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
303 *
304 * @wd_dev - watchdog device struct
305 * @timeout - timeout value to set
306 *
307 * returns 0 if success, negative errno code for failure
308 */
309static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, unsigned int timeout)
310{
311 struct mei_device *dev;
55d33856 312
09649a85 313 dev = watchdog_get_drvdata(wd_dev);
55d33856
OW
314 if (!dev)
315 return -ENODEV;
316
317 /* Check Timeout value */
248ffdf7 318 if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
55d33856
OW
319 return -EINVAL;
320
321 mutex_lock(&dev->device_lock);
322
323 dev->wd_timeout = timeout;
0197c1c4 324 wd_dev->timeout = timeout;
55d33856
OW
325 mei_wd_set_start_timeout(dev, dev->wd_timeout);
326
327 mutex_unlock(&dev->device_lock);
328
329 return 0;
330}
331
4a3cafd5
OW
332/*
333 * Watchdog Device structs
334 */
575c1e43 335static const struct watchdog_ops wd_ops = {
4a3cafd5
OW
336 .owner = THIS_MODULE,
337 .start = mei_wd_ops_start,
338 .stop = mei_wd_ops_stop,
8c4a59a7 339 .ping = mei_wd_ops_ping,
55d33856 340 .set_timeout = mei_wd_ops_set_timeout,
4a3cafd5 341};
575c1e43 342static const struct watchdog_info wd_info = {
4a3cafd5 343 .identity = INTEL_AMT_WATCHDOG_ID,
c8df7292
TW
344 .options = WDIOF_KEEPALIVEPING |
345 WDIOF_SETTIMEOUT |
346 WDIOF_ALARMONLY,
4a3cafd5
OW
347};
348
68d923d5 349static struct watchdog_device amt_wd_dev = {
4a3cafd5
OW
350 .info = &wd_info,
351 .ops = &wd_ops,
248ffdf7
TW
352 .timeout = MEI_WD_DEFAULT_TIMEOUT,
353 .min_timeout = MEI_WD_MIN_TIMEOUT,
354 .max_timeout = MEI_WD_MAX_TIMEOUT,
4a3cafd5
OW
355};
356
357
09649a85 358void mei_watchdog_register(struct mei_device *dev)
70cd5337 359{
70cd5337 360 if (watchdog_register_device(&amt_wd_dev)) {
13c398aa
TW
361 dev_err(&dev->pdev->dev,
362 "wd: unable to register watchdog device.\n");
09649a85 363 return;
70cd5337 364 }
09649a85
TW
365
366 dev_dbg(&dev->pdev->dev,
367 "wd: successfully register watchdog interface.\n");
09649a85 368 watchdog_set_drvdata(&amt_wd_dev, dev);
70cd5337
TW
369}
370
371void mei_watchdog_unregister(struct mei_device *dev)
372{
c7d3df35 373 if (test_bit(WDOG_UNREGISTERED, &amt_wd_dev.status))
09649a85
TW
374 return;
375
376 watchdog_set_drvdata(&amt_wd_dev, NULL);
377 watchdog_unregister_device(&amt_wd_dev);
70cd5337
TW
378}
379
This page took 0.213306 seconds and 5 git commands to generate.