staging: comedi: das08: Correct AO output for das08jr-16-ao
[pandora-kernel.git] / drivers / staging / mei / wd.c
1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2011, Intel Corporation.
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>
22 #include <linux/watchdog.h>
23
24 #include "mei_dev.h"
25 #include "hw.h"
26 #include "interface.h"
27 #include "mei.h"
28
29 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
30 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
31
32 const 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
38 /* UUIDs for AMT F/W clients */
39 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
40                                                 0x9D, 0xA9, 0x15, 0x14, 0xCB,
41                                                 0x32, 0xAB);
42
43
44 void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
45 {
46         dev_dbg(&dev->pdev->dev, "timeout=%d.\n", timeout);
47         memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE);
48         memcpy(dev->wd_data + MEI_WD_PARAMS_SIZE,
49                         &timeout, sizeof(u16));
50 }
51
52 /**
53  * host_init_wd - mei initialization wd.
54  *
55  * @dev: the device structure
56  */
57 bool mei_wd_host_init(struct mei_device *dev)
58 {
59         bool ret = false;
60
61         mei_cl_init(&dev->wd_cl, dev);
62
63         /* look for WD client and connect to it */
64         dev->wd_cl.state = MEI_FILE_DISCONNECTED;
65         dev->wd_timeout = AMT_WD_DEFAULT_TIMEOUT;
66
67         /* find ME WD client */
68         mei_find_me_client_update_filext(dev, &dev->wd_cl,
69                                 &mei_wd_guid, MEI_WD_HOST_CLIENT_ID);
70
71         dev_dbg(&dev->pdev->dev, "check wd_cl\n");
72         if (MEI_FILE_CONNECTING == dev->wd_cl.state) {
73                 if (!mei_connect(dev, &dev->wd_cl)) {
74                         dev_dbg(&dev->pdev->dev, "Failed to connect to WD client\n");
75                         dev->wd_cl.state = MEI_FILE_DISCONNECTED;
76                         dev->wd_cl.host_client_id = 0;
77                         ret = false;
78                         goto end;
79                 } else {
80                         dev->wd_cl.timer_count = CONNECT_TIMEOUT;
81                 }
82         } else {
83                 dev_dbg(&dev->pdev->dev, "Failed to find WD client\n");
84                 ret = false;
85                 goto end;
86         }
87
88 end:
89         return ret;
90 }
91
92 /**
93  * mei_wd_send - sends watch dog message to fw.
94  *
95  * @dev: the device structure
96  *
97  * returns 0 if success,
98  *      -EIO when message send fails
99  *      -EINVAL when invalid message is to be sent
100  */
101 int mei_wd_send(struct mei_device *dev)
102 {
103         struct mei_msg_hdr *mei_hdr;
104
105         mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
106         mei_hdr->host_addr = dev->wd_cl.host_client_id;
107         mei_hdr->me_addr = dev->wd_cl.me_client_id;
108         mei_hdr->msg_complete = 1;
109         mei_hdr->reserved = 0;
110
111         if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE))
112                 mei_hdr->length = MEI_START_WD_DATA_SIZE;
113         else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE))
114                 mei_hdr->length = MEI_WD_PARAMS_SIZE;
115         else
116                 return -EINVAL;
117
118         if (mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length))
119                 return 0;
120         return -EIO;
121 }
122
123 /**
124  * mei_wd_stop - sends watchdog stop message to fw.
125  *
126  * @dev: the device structure
127  * @preserve: indicate if to keep the timeout value
128  *
129  * returns 0 if success,
130  *      -EIO when message send fails
131  *      -EINVAL when invalid message is to be sent
132  */
133 int mei_wd_stop(struct mei_device *dev, bool preserve)
134 {
135         int ret;
136         u16 wd_timeout = dev->wd_timeout;
137
138         cancel_delayed_work(&dev->timer_work);
139         if (dev->wd_cl.state != MEI_FILE_CONNECTED || !dev->wd_timeout)
140                 return 0;
141
142         dev->wd_timeout = 0;
143         dev->wd_due_counter = 0;
144         memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE);
145         dev->stop = true;
146
147         ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
148         if (ret < 0)
149                 goto out;
150
151         if (ret && dev->mei_host_buffer_is_empty) {
152                 ret = 0;
153                 dev->mei_host_buffer_is_empty = false;
154
155                 if (!mei_wd_send(dev)) {
156                         ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
157                         if (ret)
158                                 goto out;
159                 } else {
160                         dev_dbg(&dev->pdev->dev, "send stop WD failed\n");
161                 }
162
163                 dev->wd_pending = false;
164         } else {
165                 dev->wd_pending = true;
166         }
167         dev->wd_stopped = false;
168         mutex_unlock(&dev->device_lock);
169
170         ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
171                                         dev->wd_stopped, 10 * HZ);
172         mutex_lock(&dev->device_lock);
173         if (dev->wd_stopped) {
174                 dev_dbg(&dev->pdev->dev, "stop wd complete ret=%d.\n", ret);
175                 ret = 0;
176         } else {
177                 if (!ret)
178                         ret = -ETIMEDOUT;
179                 dev_warn(&dev->pdev->dev,
180                         "stop wd failed to complete ret=%d.\n", ret);
181         }
182
183         if (preserve)
184                 dev->wd_timeout = wd_timeout;
185
186 out:
187         return ret;
188 }
189
190 /*
191  * mei_wd_ops_start - wd start command from the watchdog core.
192  *
193  * @wd_dev - watchdog device struct
194  *
195  * returns 0 if success, negative errno code for failure
196  */
197 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
198 {
199         int err = -ENODEV;
200         struct mei_device *dev;
201
202         dev = pci_get_drvdata(mei_device);
203         if (!dev)
204                 return -ENODEV;
205
206         mutex_lock(&dev->device_lock);
207
208         if (dev->mei_state != MEI_ENABLED) {
209                 dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED  mei_state= %d\n",
210                     dev->mei_state);
211                 goto end_unlock;
212         }
213
214         if (dev->wd_cl.state != MEI_FILE_CONNECTED)     {
215                 dev_dbg(&dev->pdev->dev, "MEI Driver is not connected to Watchdog Client\n");
216                 goto end_unlock;
217         }
218
219         mei_wd_set_start_timeout(dev, dev->wd_timeout);
220
221         err = 0;
222 end_unlock:
223         mutex_unlock(&dev->device_lock);
224         return err;
225 }
226
227 /*
228  * mei_wd_ops_stop -  wd stop command from the watchdog core.
229  *
230  * @wd_dev - watchdog device struct
231  *
232  * returns 0 if success, negative errno code for failure
233  */
234 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
235 {
236         struct mei_device *dev;
237         dev = pci_get_drvdata(mei_device);
238
239         if (!dev)
240                 return -ENODEV;
241
242         mutex_lock(&dev->device_lock);
243         mei_wd_stop(dev, false);
244         mutex_unlock(&dev->device_lock);
245
246         return 0;
247 }
248
249 /*
250  * mei_wd_ops_ping - wd ping command from the watchdog core.
251  *
252  * @wd_dev - watchdog device struct
253  *
254  * returns 0 if success, negative errno code for failure
255  */
256 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
257 {
258         int ret = 0;
259         struct mei_device *dev;
260         dev = pci_get_drvdata(mei_device);
261
262         if (!dev)
263                 return -ENODEV;
264
265         mutex_lock(&dev->device_lock);
266
267         if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
268                 dev_dbg(&dev->pdev->dev, "wd is not connected.\n");
269                 ret = -ENODEV;
270                 goto end;
271         }
272
273         /* Check if we can send the ping to HW*/
274         if (dev->mei_host_buffer_is_empty &&
275                 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
276
277                 dev->mei_host_buffer_is_empty = false;
278                 dev_dbg(&dev->pdev->dev, "sending watchdog ping\n");
279
280                 if (mei_wd_send(dev)) {
281                         dev_dbg(&dev->pdev->dev, "wd send failed.\n");
282                         ret = -EIO;
283                         goto end;
284                 }
285
286                 if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
287                         dev_dbg(&dev->pdev->dev, "mei_flow_ctrl_reduce() failed.\n");
288                         ret = -EIO;
289                         goto end;
290                 }
291
292         } else {
293                 dev->wd_pending = true;
294         }
295
296 end:
297         mutex_unlock(&dev->device_lock);
298         return ret;
299 }
300
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  */
309 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, unsigned int timeout)
310 {
311         struct mei_device *dev;
312         dev = pci_get_drvdata(mei_device);
313
314         if (!dev)
315                 return -ENODEV;
316
317         /* Check Timeout value */
318         if (timeout < AMT_WD_MIN_TIMEOUT || timeout > AMT_WD_MAX_TIMEOUT)
319                 return -EINVAL;
320
321         mutex_lock(&dev->device_lock);
322
323         dev->wd_timeout = timeout;
324         mei_wd_set_start_timeout(dev, dev->wd_timeout);
325
326         mutex_unlock(&dev->device_lock);
327
328         return 0;
329 }
330
331 /*
332  * Watchdog Device structs
333  */
334 const struct watchdog_ops wd_ops = {
335                 .owner = THIS_MODULE,
336                 .start = mei_wd_ops_start,
337                 .stop = mei_wd_ops_stop,
338                 .ping = mei_wd_ops_ping,
339                 .set_timeout = mei_wd_ops_set_timeout,
340 };
341 const struct watchdog_info wd_info = {
342                 .identity = INTEL_AMT_WATCHDOG_ID,
343                 .options = WDIOF_KEEPALIVEPING,
344 };
345
346 struct watchdog_device amt_wd_dev = {
347                 .info = &wd_info,
348                 .ops = &wd_ops,
349                 .timeout = AMT_WD_DEFAULT_TIMEOUT,
350                 .min_timeout = AMT_WD_MIN_TIMEOUT,
351                 .max_timeout = AMT_WD_MAX_TIMEOUT,
352 };
353
354