staging: mei: adding watchdog ping
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / mei / wd.c
blob9338394b236956bbfe6e2e9c613dfa5e4d5db660
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2011, 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 "mei_dev.h"
25 #include "hw.h"
26 #include "interface.h"
27 #include "mei.h"
30 * MEI Watchdog Module Parameters
32 static u16 watchdog_timeout = AMT_WD_DEFAULT_TIMEOUT;
33 module_param(watchdog_timeout, ushort, 0);
34 MODULE_PARM_DESC(watchdog_timeout,
35 "Intel(R) AMT Watchdog timeout value in seconds. (default="
36 __MODULE_STRING(AMT_WD_DEFAULT_TIMEOUT)
37 ", disable=0)");
39 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
40 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
42 const u8 mei_wd_state_independence_msg[3][4] = {
43 {0x05, 0x02, 0x51, 0x10},
44 {0x05, 0x02, 0x52, 0x10},
45 {0x07, 0x02, 0x01, 0x10}
48 /* UUIDs for AMT F/W clients */
49 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
50 0x9D, 0xA9, 0x15, 0x14, 0xCB,
51 0x32, 0xAB);
54 void mei_wd_start_setup(struct mei_device *dev)
56 dev_dbg(&dev->pdev->dev, "dev->wd_timeout=%d.\n", dev->wd_timeout);
57 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE);
58 memcpy(dev->wd_data + MEI_WD_PARAMS_SIZE,
59 &dev->wd_timeout, sizeof(u16));
62 /**
63 * host_init_wd - mei initialization wd.
65 * @dev: the device structure
67 bool mei_wd_host_init(struct mei_device *dev)
69 bool ret = false;
71 mei_cl_init(&dev->wd_cl, dev);
73 /* look for WD client and connect to it */
74 dev->wd_cl.state = MEI_FILE_DISCONNECTED;
75 dev->wd_timeout = watchdog_timeout;
77 if (dev->wd_timeout > 0) {
78 mei_wd_start_setup(dev);
79 /* find ME WD client */
80 mei_find_me_client_update_filext(dev, &dev->wd_cl,
81 &mei_wd_guid, MEI_WD_HOST_CLIENT_ID);
83 dev_dbg(&dev->pdev->dev, "check wd_cl\n");
84 if (MEI_FILE_CONNECTING == dev->wd_cl.state) {
85 if (!mei_connect(dev, &dev->wd_cl)) {
86 dev_dbg(&dev->pdev->dev, "Failed to connect to WD client\n");
87 dev->wd_cl.state = MEI_FILE_DISCONNECTED;
88 dev->wd_cl.host_client_id = 0;
89 ret = false;
90 goto end;
91 } else {
92 dev->wd_cl.timer_count = CONNECT_TIMEOUT;
94 } else {
95 dev_dbg(&dev->pdev->dev, "Failed to find WD client\n");
96 ret = false;
97 goto end;
99 } else {
100 dev->wd_bypass = true;
101 dev_dbg(&dev->pdev->dev, "WD requested to be disabled\n");
102 ret = false;
103 goto end;
106 end:
107 return ret;
111 * mei_wd_send - sends watch dog message to fw.
113 * @dev: the device structure
115 * returns 0 if success,
116 * -EIO when message send fails
117 * -EINVAL when invalid message is to be sent
119 int mei_wd_send(struct mei_device *dev)
121 struct mei_msg_hdr *mei_hdr;
123 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
124 mei_hdr->host_addr = dev->wd_cl.host_client_id;
125 mei_hdr->me_addr = dev->wd_cl.me_client_id;
126 mei_hdr->msg_complete = 1;
127 mei_hdr->reserved = 0;
129 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE))
130 mei_hdr->length = MEI_START_WD_DATA_SIZE;
131 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE))
132 mei_hdr->length = MEI_WD_PARAMS_SIZE;
133 else
134 return -EINVAL;
136 if (mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length))
137 return 0;
138 return -EIO;
141 int mei_wd_stop(struct mei_device *dev, bool preserve)
143 int ret;
144 u16 wd_timeout = dev->wd_timeout;
146 cancel_delayed_work(&dev->wd_work);
147 if (dev->wd_cl.state != MEI_FILE_CONNECTED || !dev->wd_timeout)
148 return 0;
150 dev->wd_timeout = 0;
151 dev->wd_due_counter = 0;
152 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE);
153 dev->stop = true;
155 ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
156 if (ret < 0)
157 goto out;
159 if (ret && dev->mei_host_buffer_is_empty) {
160 ret = 0;
161 dev->mei_host_buffer_is_empty = false;
163 if (!mei_wd_send(dev)) {
164 ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
165 if (ret)
166 goto out;
167 } else {
168 dev_dbg(&dev->pdev->dev, "send stop WD failed\n");
171 dev->wd_pending = false;
172 } else {
173 dev->wd_pending = true;
175 dev->wd_stopped = false;
176 mutex_unlock(&dev->device_lock);
178 ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
179 dev->wd_stopped, 10 * HZ);
180 mutex_lock(&dev->device_lock);
181 if (dev->wd_stopped) {
182 dev_dbg(&dev->pdev->dev, "stop wd complete ret=%d.\n", ret);
183 ret = 0;
184 } else {
185 if (!ret)
186 ret = -ETIMEDOUT;
187 dev_warn(&dev->pdev->dev,
188 "stop wd failed to complete ret=%d.\n", ret);
191 if (preserve)
192 dev->wd_timeout = wd_timeout;
194 out:
195 return ret;
199 * mei_wd_ops_start - wd start command from the watchdog core.
201 * @wd_dev - watchdog device struct
203 * returns 0 if success, negative errno code for failure
205 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
207 int err = -ENODEV;
208 struct mei_device *dev;
210 dev = pci_get_drvdata(mei_device);
211 if (!dev)
212 return -ENODEV;
214 mutex_lock(&dev->device_lock);
216 if (dev->mei_state != MEI_ENABLED) {
217 dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED mei_state= %d\n",
218 dev->mei_state);
219 goto end_unlock;
222 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
223 dev_dbg(&dev->pdev->dev, "MEI Driver is not connected to Watchdog Client\n");
224 goto end_unlock;
227 mei_wd_start_setup(dev);
229 err = 0;
230 end_unlock:
231 mutex_unlock(&dev->device_lock);
232 return err;
236 * mei_wd_ops_stop - wd stop command from the watchdog core.
238 * @wd_dev - watchdog device struct
240 * returns 0 if success, negative errno code for failure
242 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
244 struct mei_device *dev;
245 dev = pci_get_drvdata(mei_device);
247 if (!dev)
248 return -ENODEV;
250 mutex_lock(&dev->device_lock);
251 mei_wd_stop(dev, false);
252 mutex_unlock(&dev->device_lock);
254 return 0;
258 * mei_wd_ops_ping - wd ping command from the watchdog core.
260 * @wd_dev - watchdog device struct
262 * returns 0 if success, negative errno code for failure
264 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
266 int ret = 0;
267 struct mei_device *dev;
268 dev = pci_get_drvdata(mei_device);
270 if (!dev)
271 return -ENODEV;
273 mutex_lock(&dev->device_lock);
275 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
276 dev_dbg(&dev->pdev->dev, "wd is not connected.\n");
277 ret = -ENODEV;
278 goto end;
281 /* Check if we can send the ping to HW*/
282 if (dev->mei_host_buffer_is_empty &&
283 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
285 dev->mei_host_buffer_is_empty = false;
286 dev_dbg(&dev->pdev->dev, "sending watchdog ping\n");
288 if (mei_wd_send(dev)) {
289 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
290 ret = -EIO;
291 goto end;
294 if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
295 dev_dbg(&dev->pdev->dev, "mei_flow_ctrl_reduce() failed.\n");
296 ret = -EIO;
297 goto end;
300 } else {
301 dev->wd_pending = true;
304 end:
305 mutex_unlock(&dev->device_lock);
306 return ret;
310 * Watchdog Device structs
312 const struct watchdog_ops wd_ops = {
313 .owner = THIS_MODULE,
314 .start = mei_wd_ops_start,
315 .stop = mei_wd_ops_stop,
316 .ping = mei_wd_ops_ping,
318 const struct watchdog_info wd_info = {
319 .identity = INTEL_AMT_WATCHDOG_ID,
320 .options = WDIOF_KEEPALIVEPING,
323 struct watchdog_device amt_wd_dev = {
324 .info = &wd_info,
325 .ops = &wd_ops,
326 .timeout = AMT_WD_DEFAULT_TIMEOUT,
327 .min_timeout = AMT_WD_MIN_TIMEOUT,
328 .max_timeout = AMT_WD_MAX_TIMEOUT,