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
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>
29 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
30 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
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,
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));
50 * mei_wd_host_init - connect to the watchdog client
52 * @dev: the device structure
54 * returns -ENOTTY if wd client cannot be found
55 * -EIO if write has failed
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;
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);
73 dev_info(dev->dev, "wd: failed to find the client\n");
77 cl->me_client_id = me_cl->client_id;
78 cl->cl_uuid = me_cl->props.protocol_name;
80 ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
83 dev_info(dev->dev, "wd: failed link client\n");
87 ret = mei_cl_connect(cl, NULL);
90 dev_err(dev->dev, "wd: failed to connect = %d\n", ret);
95 ret = mei_watchdog_register(dev);
97 mei_cl_disconnect(cl);
104 * mei_wd_send - sends watch dog message to fw.
106 * @dev: the device structure
108 * returns 0 if success,
109 * -EIO when message send fails
110 * -EINVAL when invalid message is to be sent
111 * -ENODEV on flow control failure
113 int mei_wd_send(struct mei_device *dev)
115 struct mei_cl *cl = &dev->wd_cl;
116 struct mei_msg_hdr hdr;
119 hdr.host_addr = cl->host_client_id;
120 hdr.me_addr = cl->me_client_id;
121 hdr.msg_complete = 1;
125 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
126 hdr.length = MEI_WD_START_MSG_SIZE;
127 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
128 hdr.length = MEI_WD_STOP_MSG_SIZE;
130 dev_err(dev->dev, "wd: invalid message is to be sent, aborting\n");
134 ret = mei_write_message(dev, &hdr, dev->wd_data);
136 dev_err(dev->dev, "wd: write message failed\n");
140 ret = mei_cl_flow_ctrl_reduce(cl);
142 dev_err(dev->dev, "wd: flow_ctrl_reduce failed.\n");
150 * mei_wd_stop - sends watchdog stop message to fw.
152 * @dev: the device structure
153 * @preserve: indicate if to keep the timeout value
155 * returns 0 if success
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)
165 if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
166 dev->wd_state != MEI_WD_RUNNING)
169 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
171 dev->wd_state = MEI_WD_STOPPING;
173 ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
177 if (ret && mei_hbuf_acquire(dev)) {
178 ret = mei_wd_send(dev);
181 dev->wd_pending = false;
183 dev->wd_pending = true;
186 mutex_unlock(&dev->device_lock);
188 ret = wait_event_timeout(dev->wait_stop_wd,
189 dev->wd_state == MEI_WD_IDLE,
190 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
191 mutex_lock(&dev->device_lock);
192 if (dev->wd_state != MEI_WD_IDLE) {
195 dev_warn(dev->dev, "wd: stop failed to complete ret=%d\n", ret);
198 dev_dbg(dev->dev, "wd: stop completed after %u msec\n",
199 MEI_WD_STOP_TIMEOUT - jiffies_to_msecs(ret));
206 * mei_wd_ops_start - wd start command from the watchdog core.
208 * @wd_dev - watchdog device struct
210 * returns 0 if success, negative errno code for failure
212 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
215 struct mei_device *dev;
217 dev = watchdog_get_drvdata(wd_dev);
221 mutex_lock(&dev->device_lock);
223 if (dev->dev_state != MEI_DEV_ENABLED) {
224 dev_dbg(dev->dev, "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
225 mei_dev_state_str(dev->dev_state));
229 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
230 dev_dbg(dev->dev, "MEI Driver is not connected to Watchdog Client\n");
234 mei_wd_set_start_timeout(dev, dev->wd_timeout);
238 mutex_unlock(&dev->device_lock);
243 * mei_wd_ops_stop - wd stop command from the watchdog core.
245 * @wd_dev - watchdog device struct
247 * returns 0 if success, negative errno code for failure
249 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
251 struct mei_device *dev;
253 dev = watchdog_get_drvdata(wd_dev);
257 mutex_lock(&dev->device_lock);
259 mutex_unlock(&dev->device_lock);
265 * mei_wd_ops_ping - wd ping command from the watchdog core.
267 * @wd_dev - watchdog device struct
269 * returns 0 if success, negative errno code for failure
271 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
273 struct mei_device *dev;
276 dev = watchdog_get_drvdata(wd_dev);
280 mutex_lock(&dev->device_lock);
282 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
283 dev_err(dev->dev, "wd: not connected.\n");
288 dev->wd_state = MEI_WD_RUNNING;
290 ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
293 /* Check if we can send the ping to HW*/
294 if (ret && mei_hbuf_acquire(dev)) {
296 dev_dbg(dev->dev, "wd: sending ping\n");
298 ret = mei_wd_send(dev);
301 dev->wd_pending = false;
303 dev->wd_pending = true;
307 mutex_unlock(&dev->device_lock);
312 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
314 * @wd_dev - watchdog device struct
315 * @timeout - timeout value to set
317 * returns 0 if success, negative errno code for failure
319 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
320 unsigned int timeout)
322 struct mei_device *dev;
324 dev = watchdog_get_drvdata(wd_dev);
328 /* Check Timeout value */
329 if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
332 mutex_lock(&dev->device_lock);
334 dev->wd_timeout = timeout;
335 wd_dev->timeout = timeout;
336 mei_wd_set_start_timeout(dev, dev->wd_timeout);
338 mutex_unlock(&dev->device_lock);
344 * Watchdog Device structs
346 static const struct watchdog_ops wd_ops = {
347 .owner = THIS_MODULE,
348 .start = mei_wd_ops_start,
349 .stop = mei_wd_ops_stop,
350 .ping = mei_wd_ops_ping,
351 .set_timeout = mei_wd_ops_set_timeout,
353 static const struct watchdog_info wd_info = {
354 .identity = INTEL_AMT_WATCHDOG_ID,
355 .options = WDIOF_KEEPALIVEPING |
360 static struct watchdog_device amt_wd_dev = {
363 .timeout = MEI_WD_DEFAULT_TIMEOUT,
364 .min_timeout = MEI_WD_MIN_TIMEOUT,
365 .max_timeout = MEI_WD_MAX_TIMEOUT,
369 int mei_watchdog_register(struct mei_device *dev)
374 /* unlock to perserve correct locking order */
375 mutex_unlock(&dev->device_lock);
376 ret = watchdog_register_device(&amt_wd_dev);
377 mutex_lock(&dev->device_lock);
379 dev_err(dev->dev, "wd: unable to register watchdog device = %d.\n",
384 dev_dbg(dev->dev, "wd: successfully register watchdog interface.\n");
385 watchdog_set_drvdata(&amt_wd_dev, dev);
389 void mei_watchdog_unregister(struct mei_device *dev)
391 if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
394 watchdog_set_drvdata(&amt_wd_dev, NULL);
395 watchdog_unregister_device(&amt_wd_dev);