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