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