Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/egtvedt...
[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
23 #include "mei_dev.h"
24 #include "hw.h"
25 #include "interface.h"
26 #include "mei.h"
27
28 /*
29  * MEI Watchdog Module Parameters
30  */
31 static u16 watchdog_timeout = AMT_WD_VALUE;
32 module_param(watchdog_timeout, ushort, 0);
33 MODULE_PARM_DESC(watchdog_timeout,
34                 "Intel(R) AMT Watchdog timeout value in seconds. (default="
35                                         __MODULE_STRING(AMT_WD_VALUE)
36                                         ", disable=0)");
37
38 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
39 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
40
41 const u8 mei_wd_state_independence_msg[3][4] = {
42         {0x05, 0x02, 0x51, 0x10},
43         {0x05, 0x02, 0x52, 0x10},
44         {0x07, 0x02, 0x01, 0x10}
45 };
46
47 /* UUIDs for AMT F/W clients */
48 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
49                                                 0x9D, 0xA9, 0x15, 0x14, 0xCB,
50                                                 0x32, 0xAB);
51
52
53 void mei_wd_start_setup(struct mei_device *dev)
54 {
55         dev_dbg(&dev->pdev->dev, "dev->wd_timeout=%d.\n", dev->wd_timeout);
56         memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE);
57         memcpy(dev->wd_data + MEI_WD_PARAMS_SIZE,
58                 &dev->wd_timeout, sizeof(u16));
59 }
60
61 /**
62  * host_init_wd - mei initialization wd.
63  *
64  * @dev: the device structure
65  */
66 void mei_wd_host_init(struct mei_device *dev)
67 {
68         mei_init_file_private(&dev->wd_cl, dev);
69
70         /* look for WD client and connect to it */
71         dev->wd_cl.state = MEI_FILE_DISCONNECTED;
72         dev->wd_timeout = watchdog_timeout;
73
74         if (dev->wd_timeout > 0) {
75                 mei_wd_start_setup(dev);
76                 /* find ME WD client */
77                 mei_find_me_client_update_filext(dev, &dev->wd_cl,
78                                         &mei_wd_guid, MEI_WD_HOST_CLIENT_ID);
79
80                 dev_dbg(&dev->pdev->dev, "check wd_cl\n");
81                 if (MEI_FILE_CONNECTING == dev->wd_cl.state) {
82                         if (!mei_connect(dev, &dev->wd_cl)) {
83                                 dev_dbg(&dev->pdev->dev, "Failed to connect to WD client\n");
84                                 dev->wd_cl.state = MEI_FILE_DISCONNECTED;
85                                 dev->wd_cl.host_client_id = 0;
86                                 host_init_iamthif(dev) ;
87                         } else {
88                                 dev->wd_cl.timer_count = CONNECT_TIMEOUT;
89                         }
90                 } else {
91                         dev_dbg(&dev->pdev->dev, "Failed to find WD client\n");
92                         host_init_iamthif(dev) ;
93                 }
94         } else {
95                 dev->wd_bypass = true;
96                 dev_dbg(&dev->pdev->dev, "WD requested to be disabled\n");
97                 host_init_iamthif(dev) ;
98         }
99 }
100
101 /**
102  * mei_wd_send - sends watch dog message to fw.
103  *
104  * @dev: the device structure
105  *
106  * returns 0 if success,
107  *      -EIO when message send fails
108  *      -EINVAL when invalid message is to be sent
109  */
110 int mei_wd_send(struct mei_device *dev)
111 {
112         struct mei_msg_hdr *mei_hdr;
113
114         mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
115         mei_hdr->host_addr = dev->wd_cl.host_client_id;
116         mei_hdr->me_addr = dev->wd_cl.me_client_id;
117         mei_hdr->msg_complete = 1;
118         mei_hdr->reserved = 0;
119
120         if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE))
121                 mei_hdr->length = MEI_START_WD_DATA_SIZE;
122         else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE))
123                 mei_hdr->length = MEI_WD_PARAMS_SIZE;
124         else
125                 return -EINVAL;
126
127         if (mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length))
128                 return 0;
129         return -EIO;
130 }
131
132 int mei_wd_stop(struct mei_device *dev, bool preserve)
133 {
134         int ret;
135         u16 wd_timeout = dev->wd_timeout;
136
137         cancel_delayed_work(&dev->wd_work);
138         if (dev->wd_cl.state != MEI_FILE_CONNECTED || !dev->wd_timeout)
139                 return 0;
140
141         dev->wd_timeout = 0;
142         dev->wd_due_counter = 0;
143         memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE);
144         dev->stop = 1;
145
146         ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
147         if (ret < 0)
148                 goto out;
149
150         if (ret && dev->mei_host_buffer_is_empty) {
151                 ret = 0;
152                 dev->mei_host_buffer_is_empty = 0;
153
154                 if (!mei_wd_send(dev)) {
155                         ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
156                         if (ret)
157                                 goto out;
158                 } else {
159                         dev_dbg(&dev->pdev->dev, "send stop WD failed\n");
160                 }
161
162                 dev->wd_pending = 0;
163         } else {
164                 dev->wd_pending = 1;
165         }
166         dev->wd_stopped = 0;
167         mutex_unlock(&dev->device_lock);
168
169         ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
170                                         dev->wd_stopped, 10 * HZ);
171         mutex_lock(&dev->device_lock);
172         if (dev->wd_stopped) {
173                 dev_dbg(&dev->pdev->dev, "stop wd complete ret=%d.\n", ret);
174                 ret = 0;
175         } else {
176                 if (!ret)
177                         ret = -ETIMEDOUT;
178                 dev_warn(&dev->pdev->dev,
179                         "stop wd failed to complete ret=%d.\n", ret);
180         }
181
182         if (preserve)
183                 dev->wd_timeout = wd_timeout;
184
185 out:
186         return ret;
187 }
188