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>
26 #include "interface.h"
27 #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 };
32 const u8 mei_wd_state_independence_msg[3][4] = {
33 {0x05, 0x02, 0x51, 0x10},
34 {0x05, 0x02, 0x52, 0x10},
35 {0x07, 0x02, 0x01, 0x10}
41 #define INTEL_AMT_WATCHDOG_ID "INTCAMT"
43 /* UUIDs for AMT F/W clients */
44 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
45 0x9D, 0xA9, 0x15, 0x14, 0xCB,
48 static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
50 dev_dbg(&dev->pdev->dev, "wd: set timeout=%d.\n", timeout);
51 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE);
52 memcpy(dev->wd_data + MEI_WD_PARAMS_SIZE, &timeout, sizeof(u16));
56 * mei_wd_host_init - connect to the watchdog client
58 * @dev: the device structure
59 * returns -ENENT if wd client cannot be found
60 * -EIO if write has failed
63 int mei_wd_host_init(struct mei_device *dev)
65 mei_cl_init(&dev->wd_cl, dev);
67 /* look for WD client and connect to it */
68 dev->wd_cl.state = MEI_FILE_DISCONNECTED;
69 dev->wd_timeout = AMT_WD_DEFAULT_TIMEOUT;
71 /* find ME WD client */
72 mei_find_me_client_update_filext(dev, &dev->wd_cl,
73 &mei_wd_guid, MEI_WD_HOST_CLIENT_ID);
75 dev_dbg(&dev->pdev->dev, "wd: check client\n");
76 if (MEI_FILE_CONNECTING != dev->wd_cl.state) {
77 dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
81 if (mei_connect(dev, &dev->wd_cl)) {
82 dev_err(&dev->pdev->dev, "wd: failed to connect to the client\n");
83 dev->wd_cl.state = MEI_FILE_DISCONNECTED;
84 dev->wd_cl.host_client_id = 0;
87 dev->wd_cl.timer_count = CONNECT_TIMEOUT;
93 * mei_wd_send - sends watch dog message to fw.
95 * @dev: the device structure
97 * returns 0 if success,
98 * -EIO when message send fails
99 * -EINVAL when invalid message is to be sent
101 int mei_wd_send(struct mei_device *dev)
103 struct mei_msg_hdr *mei_hdr;
105 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
106 mei_hdr->host_addr = dev->wd_cl.host_client_id;
107 mei_hdr->me_addr = dev->wd_cl.me_client_id;
108 mei_hdr->msg_complete = 1;
109 mei_hdr->reserved = 0;
111 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE))
112 mei_hdr->length = MEI_START_WD_DATA_SIZE;
113 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE))
114 mei_hdr->length = MEI_WD_PARAMS_SIZE;
118 return mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length);
122 * mei_wd_stop - sends watchdog stop message to fw.
124 * @dev: the device structure
125 * @preserve: indicate if to keep the timeout value
127 * returns 0 if success,
128 * -EIO when message send fails
129 * -EINVAL when invalid message is to be sent
131 int mei_wd_stop(struct mei_device *dev, bool preserve)
134 u16 wd_timeout = dev->wd_timeout;
136 cancel_delayed_work(&dev->timer_work);
137 if (dev->wd_cl.state != MEI_FILE_CONNECTED || !dev->wd_timeout)
141 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE);
144 ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
148 if (ret && dev->mei_host_buffer_is_empty) {
150 dev->mei_host_buffer_is_empty = false;
152 if (!mei_wd_send(dev)) {
153 ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
157 dev_err(&dev->pdev->dev, "wd: send stop failed\n");
160 dev->wd_pending = false;
162 dev->wd_pending = true;
164 dev->wd_stopped = false;
165 mutex_unlock(&dev->device_lock);
167 ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
168 dev->wd_stopped, 10 * HZ);
169 mutex_lock(&dev->device_lock);
170 if (dev->wd_stopped) {
171 dev_dbg(&dev->pdev->dev, "wd: stop completed ret=%d.\n", ret);
176 dev_warn(&dev->pdev->dev,
177 "wd: stop failed to complete ret=%d.\n", ret);
181 dev->wd_timeout = wd_timeout;
188 * mei_wd_ops_start - wd start command from the watchdog core.
190 * @wd_dev - watchdog device struct
192 * returns 0 if success, negative errno code for failure
194 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
197 struct mei_device *dev;
199 dev = pci_get_drvdata(mei_device);
203 mutex_lock(&dev->device_lock);
205 if (dev->mei_state != MEI_ENABLED) {
206 dev_dbg(&dev->pdev->dev,
207 "wd: mei_state != MEI_ENABLED mei_state = %d\n",
212 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
213 dev_dbg(&dev->pdev->dev,
214 "MEI Driver is not connected to Watchdog Client\n");
218 mei_wd_set_start_timeout(dev, dev->wd_timeout);
222 mutex_unlock(&dev->device_lock);
227 * mei_wd_ops_stop - wd stop command from the watchdog core.
229 * @wd_dev - watchdog device struct
231 * returns 0 if success, negative errno code for failure
233 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
235 struct mei_device *dev;
236 dev = pci_get_drvdata(mei_device);
241 mutex_lock(&dev->device_lock);
242 mei_wd_stop(dev, false);
243 mutex_unlock(&dev->device_lock);
249 * mei_wd_ops_ping - wd ping command from the watchdog core.
251 * @wd_dev - watchdog device struct
253 * returns 0 if success, negative errno code for failure
255 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
258 struct mei_device *dev;
259 dev = pci_get_drvdata(mei_device);
264 mutex_lock(&dev->device_lock);
266 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
267 dev_err(&dev->pdev->dev, "wd: not connected.\n");
272 /* Check if we can send the ping to HW*/
273 if (dev->mei_host_buffer_is_empty &&
274 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
276 dev->mei_host_buffer_is_empty = false;
277 dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
279 if (mei_wd_send(dev)) {
280 dev_err(&dev->pdev->dev, "wd: send failed.\n");
285 if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
286 dev_err(&dev->pdev->dev,
287 "wd: mei_flow_ctrl_reduce() failed.\n");
293 dev->wd_pending = true;
297 mutex_unlock(&dev->device_lock);
302 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
304 * @wd_dev - watchdog device struct
305 * @timeout - timeout value to set
307 * returns 0 if success, negative errno code for failure
309 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, unsigned int timeout)
311 struct mei_device *dev;
312 dev = pci_get_drvdata(mei_device);
317 /* Check Timeout value */
318 if (timeout < AMT_WD_MIN_TIMEOUT || timeout > AMT_WD_MAX_TIMEOUT)
321 mutex_lock(&dev->device_lock);
323 dev->wd_timeout = timeout;
324 wd_dev->timeout = timeout;
325 mei_wd_set_start_timeout(dev, dev->wd_timeout);
327 mutex_unlock(&dev->device_lock);
333 * Watchdog Device structs
335 static const struct watchdog_ops wd_ops = {
336 .owner = THIS_MODULE,
337 .start = mei_wd_ops_start,
338 .stop = mei_wd_ops_stop,
339 .ping = mei_wd_ops_ping,
340 .set_timeout = mei_wd_ops_set_timeout,
342 static const struct watchdog_info wd_info = {
343 .identity = INTEL_AMT_WATCHDOG_ID,
344 .options = WDIOF_KEEPALIVEPING | WDIOF_ALARMONLY,
347 static struct watchdog_device amt_wd_dev = {
350 .timeout = AMT_WD_DEFAULT_TIMEOUT,
351 .min_timeout = AMT_WD_MIN_TIMEOUT,
352 .max_timeout = AMT_WD_MAX_TIMEOUT,
356 void mei_watchdog_register(struct mei_device *dev)
358 dev_dbg(&dev->pdev->dev, "dev->wd_timeout =%d.\n", dev->wd_timeout);
360 if (watchdog_register_device(&amt_wd_dev)) {
361 dev_err(&dev->pdev->dev,
362 "wd: unable to register watchdog device.\n");
363 dev->wd_interface_reg = false;
365 dev_dbg(&dev->pdev->dev,
366 "wd: successfully register watchdog interface.\n");
367 dev->wd_interface_reg = true;
371 void mei_watchdog_unregister(struct mei_device *dev)
373 if (dev->wd_interface_reg)
374 watchdog_unregister_device(&amt_wd_dev);
375 dev->wd_interface_reg = false;