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