gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / misc / mei / wd.c
blob2725f865c3d6b814ef7b532069f42c6902572e8f
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/device.h>
20 #include <linux/sched.h>
21 #include <linux/watchdog.h>
23 #include <linux/mei.h>
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
29 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
30 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
33 * AMT Watchdog Device
35 #define INTEL_AMT_WATCHDOG_ID "INTCAMT"
37 /* UUIDs for AMT F/W clients */
38 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
39 0x9D, 0xA9, 0x15, 0x14, 0xCB,
40 0x32, 0xAB);
42 static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
44 dev_dbg(dev->dev, "wd: set timeout=%d.\n", timeout);
45 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
46 memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
49 /**
50 * mei_wd_host_init - connect to the watchdog client
52 * @dev: the device structure
54 * Return: -ENOTTY if wd client cannot be found
55 * -EIO if write has failed
56 * 0 on success
58 int mei_wd_host_init(struct mei_device *dev)
60 struct mei_cl *cl = &dev->wd_cl;
61 struct mei_me_client *me_cl;
62 int ret;
64 mei_cl_init(cl, dev);
66 dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
67 dev->wd_state = MEI_WD_IDLE;
70 /* check for valid client id */
71 me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
72 if (!me_cl) {
73 dev_info(dev->dev, "wd: failed to find the client\n");
74 return -ENOTTY;
77 cl->me_client_id = me_cl->client_id;
78 cl->cl_uuid = me_cl->props.protocol_name;
79 mei_me_cl_put(me_cl);
81 ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
83 if (ret < 0) {
84 dev_info(dev->dev, "wd: failed link client\n");
85 return ret;
88 ret = mei_cl_connect(cl, NULL);
90 if (ret) {
91 dev_err(dev->dev, "wd: failed to connect = %d\n", ret);
92 mei_cl_unlink(cl);
93 return ret;
96 ret = mei_watchdog_register(dev);
97 if (ret) {
98 mei_cl_disconnect(cl);
99 mei_cl_unlink(cl);
101 return ret;
105 * mei_wd_send - sends watch dog message to fw.
107 * @dev: the device structure
109 * Return: 0 if success,
110 * -EIO when message send fails
111 * -EINVAL when invalid message is to be sent
112 * -ENODEV on flow control failure
114 int mei_wd_send(struct mei_device *dev)
116 struct mei_cl *cl = &dev->wd_cl;
117 struct mei_msg_hdr hdr;
118 int ret;
120 hdr.host_addr = cl->host_client_id;
121 hdr.me_addr = cl->me_client_id;
122 hdr.msg_complete = 1;
123 hdr.reserved = 0;
124 hdr.internal = 0;
126 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
127 hdr.length = MEI_WD_START_MSG_SIZE;
128 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
129 hdr.length = MEI_WD_STOP_MSG_SIZE;
130 else {
131 dev_err(dev->dev, "wd: invalid message is to be sent, aborting\n");
132 return -EINVAL;
135 ret = mei_write_message(dev, &hdr, dev->wd_data);
136 if (ret) {
137 dev_err(dev->dev, "wd: write message failed\n");
138 return ret;
141 ret = mei_cl_flow_ctrl_reduce(cl);
142 if (ret) {
143 dev_err(dev->dev, "wd: flow_ctrl_reduce failed.\n");
144 return ret;
147 return 0;
151 * mei_wd_stop - sends watchdog stop message to fw.
153 * @dev: the device structure
155 * Return: 0 if success
156 * on error:
157 * -EIO when message send fails
158 * -EINVAL when invalid message is to be sent
159 * -ETIME on message timeout
161 int mei_wd_stop(struct mei_device *dev)
163 struct mei_cl *cl = &dev->wd_cl;
164 int ret;
166 if (!mei_cl_is_connected(cl) ||
167 dev->wd_state != MEI_WD_RUNNING)
168 return 0;
170 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
172 dev->wd_state = MEI_WD_STOPPING;
174 ret = mei_cl_flow_ctrl_creds(cl);
175 if (ret < 0)
176 goto err;
178 if (ret && mei_hbuf_acquire(dev)) {
179 ret = mei_wd_send(dev);
180 if (ret)
181 goto err;
182 dev->wd_pending = false;
183 } else {
184 dev->wd_pending = true;
187 mutex_unlock(&dev->device_lock);
189 ret = wait_event_timeout(dev->wait_stop_wd,
190 dev->wd_state == MEI_WD_IDLE,
191 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
192 mutex_lock(&dev->device_lock);
193 if (dev->wd_state != MEI_WD_IDLE) {
194 /* timeout */
195 ret = -ETIME;
196 dev_warn(dev->dev, "wd: stop failed to complete ret=%d\n", ret);
197 goto err;
199 dev_dbg(dev->dev, "wd: stop completed after %u msec\n",
200 MEI_WD_STOP_TIMEOUT - jiffies_to_msecs(ret));
201 return 0;
202 err:
203 return ret;
207 * mei_wd_ops_start - wd start command from the watchdog core.
209 * @wd_dev: watchdog device struct
211 * Return: 0 if success, negative errno code for failure
213 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
215 struct mei_device *dev;
216 struct mei_cl *cl;
217 int err = -ENODEV;
219 dev = watchdog_get_drvdata(wd_dev);
220 if (!dev)
221 return -ENODEV;
223 cl = &dev->wd_cl;
225 mutex_lock(&dev->device_lock);
227 if (dev->dev_state != MEI_DEV_ENABLED) {
228 dev_dbg(dev->dev, "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
229 mei_dev_state_str(dev->dev_state));
230 goto end_unlock;
233 if (!mei_cl_is_connected(cl)) {
234 cl_dbg(dev, cl, "MEI Driver is not connected to Watchdog Client\n");
235 goto end_unlock;
238 mei_wd_set_start_timeout(dev, dev->wd_timeout);
240 err = 0;
241 end_unlock:
242 mutex_unlock(&dev->device_lock);
243 return err;
247 * mei_wd_ops_stop - wd stop command from the watchdog core.
249 * @wd_dev: watchdog device struct
251 * Return: 0 if success, negative errno code for failure
253 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
255 struct mei_device *dev;
257 dev = watchdog_get_drvdata(wd_dev);
258 if (!dev)
259 return -ENODEV;
261 mutex_lock(&dev->device_lock);
262 mei_wd_stop(dev);
263 mutex_unlock(&dev->device_lock);
265 return 0;
269 * mei_wd_ops_ping - wd ping command from the watchdog core.
271 * @wd_dev: watchdog device struct
273 * Return: 0 if success, negative errno code for failure
275 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
277 struct mei_device *dev;
278 struct mei_cl *cl;
279 int ret;
281 dev = watchdog_get_drvdata(wd_dev);
282 if (!dev)
283 return -ENODEV;
285 cl = &dev->wd_cl;
287 mutex_lock(&dev->device_lock);
289 if (!mei_cl_is_connected(cl)) {
290 cl_err(dev, cl, "wd: not connected.\n");
291 ret = -ENODEV;
292 goto end;
295 dev->wd_state = MEI_WD_RUNNING;
297 ret = mei_cl_flow_ctrl_creds(cl);
298 if (ret < 0)
299 goto end;
301 /* Check if we can send the ping to HW*/
302 if (ret && mei_hbuf_acquire(dev)) {
303 dev_dbg(dev->dev, "wd: sending ping\n");
305 ret = mei_wd_send(dev);
306 if (ret)
307 goto end;
308 dev->wd_pending = false;
309 } else {
310 dev->wd_pending = true;
313 end:
314 mutex_unlock(&dev->device_lock);
315 return ret;
319 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
321 * @wd_dev: watchdog device struct
322 * @timeout: timeout value to set
324 * Return: 0 if success, negative errno code for failure
326 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
327 unsigned int timeout)
329 struct mei_device *dev;
331 dev = watchdog_get_drvdata(wd_dev);
332 if (!dev)
333 return -ENODEV;
335 /* Check Timeout value */
336 if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
337 return -EINVAL;
339 mutex_lock(&dev->device_lock);
341 dev->wd_timeout = timeout;
342 wd_dev->timeout = timeout;
343 mei_wd_set_start_timeout(dev, dev->wd_timeout);
345 mutex_unlock(&dev->device_lock);
347 return 0;
351 * Watchdog Device structs
353 static const struct watchdog_ops wd_ops = {
354 .owner = THIS_MODULE,
355 .start = mei_wd_ops_start,
356 .stop = mei_wd_ops_stop,
357 .ping = mei_wd_ops_ping,
358 .set_timeout = mei_wd_ops_set_timeout,
360 static const struct watchdog_info wd_info = {
361 .identity = INTEL_AMT_WATCHDOG_ID,
362 .options = WDIOF_KEEPALIVEPING |
363 WDIOF_SETTIMEOUT |
364 WDIOF_ALARMONLY,
367 static struct watchdog_device amt_wd_dev = {
368 .info = &wd_info,
369 .ops = &wd_ops,
370 .timeout = MEI_WD_DEFAULT_TIMEOUT,
371 .min_timeout = MEI_WD_MIN_TIMEOUT,
372 .max_timeout = MEI_WD_MAX_TIMEOUT,
376 int mei_watchdog_register(struct mei_device *dev)
379 int ret;
381 /* unlock to perserve correct locking order */
382 mutex_unlock(&dev->device_lock);
383 ret = watchdog_register_device(&amt_wd_dev);
384 mutex_lock(&dev->device_lock);
385 if (ret) {
386 dev_err(dev->dev, "wd: unable to register watchdog device = %d.\n",
387 ret);
388 return ret;
391 dev_dbg(dev->dev, "wd: successfully register watchdog interface.\n");
392 watchdog_set_drvdata(&amt_wd_dev, dev);
393 return 0;
396 void mei_watchdog_unregister(struct mei_device *dev)
398 if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
399 return;
401 watchdog_set_drvdata(&amt_wd_dev, NULL);
402 watchdog_unregister_device(&amt_wd_dev);