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/pci.h>
21 #include <linux/sched.h>
22 #include <linux/watchdog.h>
24 #include <linux/mei.h>
31 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
32 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
34 const u8 mei_wd_state_independence_msg[3][4] = {
35 {0x05, 0x02, 0x51, 0x10},
36 {0x05, 0x02, 0x52, 0x10},
37 {0x07, 0x02, 0x01, 0x10}
43 #define INTEL_AMT_WATCHDOG_ID "INTCAMT"
45 /* UUIDs for AMT F/W clients */
46 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
47 0x9D, 0xA9, 0x15, 0x14, 0xCB,
50 static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
52 dev_dbg(&dev->pdev->dev, "wd: set timeout=%d.\n", timeout);
53 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
54 memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
58 * mei_wd_host_init - connect to the watchdog client
60 * @dev: the device structure
61 * returns -ENENT if wd client cannot be found
62 * -EIO if write has failed
65 int mei_wd_host_init(struct mei_device *dev)
67 struct mei_cl *cl = &dev->wd_cl;
73 dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
74 dev->wd_state = MEI_WD_IDLE;
77 /* check for valid client id */
78 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
80 dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
84 cl->me_client_id = dev->me_clients[i].client_id;
86 ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
89 dev_info(&dev->pdev->dev, "wd: failed link client\n");
93 cl->state = MEI_FILE_CONNECTING;
95 if (mei_hbm_cl_connect_req(dev, cl)) {
96 dev_err(&dev->pdev->dev, "wd: failed to connect to the client\n");
97 cl->state = MEI_FILE_DISCONNECTED;
98 cl->host_client_id = 0;
101 cl->timer_count = MEI_CONNECT_TIMEOUT;
107 * mei_wd_send - sends watch dog message to fw.
109 * @dev: the device structure
111 * returns 0 if success,
112 * -EIO when message send fails
113 * -EINVAL when invalid message is to be sent
115 int mei_wd_send(struct mei_device *dev)
117 struct mei_msg_hdr hdr;
119 hdr.host_addr = dev->wd_cl.host_client_id;
120 hdr.me_addr = dev->wd_cl.me_client_id;
121 hdr.msg_complete = 1;
124 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
125 hdr.length = MEI_WD_START_MSG_SIZE;
126 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
127 hdr.length = MEI_WD_STOP_MSG_SIZE;
131 return mei_write_message(dev, &hdr, dev->wd_data);
135 * mei_wd_stop - sends watchdog stop message to fw.
137 * @dev: the device structure
138 * @preserve: indicate if to keep the timeout value
140 * returns 0 if success,
141 * -EIO when message send fails
142 * -EINVAL when invalid message is to be sent
144 int mei_wd_stop(struct mei_device *dev)
148 if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
149 dev->wd_state != MEI_WD_RUNNING)
152 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
154 dev->wd_state = MEI_WD_STOPPING;
156 ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
160 if (ret && dev->hbuf_is_ready) {
162 dev->hbuf_is_ready = false;
164 if (!mei_wd_send(dev)) {
165 ret = mei_cl_flow_ctrl_reduce(&dev->wd_cl);
169 dev_err(&dev->pdev->dev, "wd: send stop failed\n");
172 dev->wd_pending = false;
174 dev->wd_pending = true;
177 mutex_unlock(&dev->device_lock);
179 ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
180 dev->wd_state == MEI_WD_IDLE,
181 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
182 mutex_lock(&dev->device_lock);
183 if (dev->wd_state == MEI_WD_IDLE) {
184 dev_dbg(&dev->pdev->dev, "wd: stop completed ret=%d.\n", ret);
189 dev_warn(&dev->pdev->dev,
190 "wd: stop failed to complete ret=%d.\n", ret);
198 * mei_wd_ops_start - wd start command from the watchdog core.
200 * @wd_dev - watchdog device struct
202 * returns 0 if success, negative errno code for failure
204 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
207 struct mei_device *dev;
209 dev = watchdog_get_drvdata(wd_dev);
213 mutex_lock(&dev->device_lock);
215 if (dev->dev_state != MEI_DEV_ENABLED) {
216 dev_dbg(&dev->pdev->dev,
217 "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
218 mei_dev_state_str(dev->dev_state));
222 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
223 dev_dbg(&dev->pdev->dev,
224 "MEI Driver is not connected to Watchdog Client\n");
228 mei_wd_set_start_timeout(dev, dev->wd_timeout);
232 mutex_unlock(&dev->device_lock);
237 * mei_wd_ops_stop - wd stop command from the watchdog core.
239 * @wd_dev - watchdog device struct
241 * returns 0 if success, negative errno code for failure
243 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
245 struct mei_device *dev;
247 dev = watchdog_get_drvdata(wd_dev);
251 mutex_lock(&dev->device_lock);
253 mutex_unlock(&dev->device_lock);
259 * mei_wd_ops_ping - wd ping command from the watchdog core.
261 * @wd_dev - watchdog device struct
263 * returns 0 if success, negative errno code for failure
265 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
268 struct mei_device *dev;
270 dev = watchdog_get_drvdata(wd_dev);
274 mutex_lock(&dev->device_lock);
276 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
277 dev_err(&dev->pdev->dev, "wd: not connected.\n");
282 dev->wd_state = MEI_WD_RUNNING;
284 /* Check if we can send the ping to HW*/
285 if (dev->hbuf_is_ready && mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
287 dev->hbuf_is_ready = false;
288 dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
290 if (mei_wd_send(dev)) {
291 dev_err(&dev->pdev->dev, "wd: send failed.\n");
296 if (mei_cl_flow_ctrl_reduce(&dev->wd_cl)) {
297 dev_err(&dev->pdev->dev,
298 "wd: mei_cl_flow_ctrl_reduce() failed.\n");
304 dev->wd_pending = true;
308 mutex_unlock(&dev->device_lock);
313 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
315 * @wd_dev - watchdog device struct
316 * @timeout - timeout value to set
318 * returns 0 if success, negative errno code for failure
320 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, 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 void mei_watchdog_register(struct mei_device *dev)
371 if (watchdog_register_device(&amt_wd_dev)) {
372 dev_err(&dev->pdev->dev,
373 "wd: unable to register watchdog device.\n");
377 dev_dbg(&dev->pdev->dev,
378 "wd: successfully register watchdog interface.\n");
379 watchdog_set_drvdata(&amt_wd_dev, dev);
382 void mei_watchdog_unregister(struct mei_device *dev)
384 if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
387 watchdog_set_drvdata(&amt_wd_dev, NULL);
388 watchdog_unregister_device(&amt_wd_dev);