3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2011, 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"
30 * MEI Watchdog Module Parameters
32 static u16 watchdog_timeout
= AMT_WD_DEFAULT_TIMEOUT
;
33 module_param(watchdog_timeout
, ushort
, 0);
34 MODULE_PARM_DESC(watchdog_timeout
,
35 "Intel(R) AMT Watchdog timeout value in seconds. (default="
36 __MODULE_STRING(AMT_WD_DEFAULT_TIMEOUT
)
39 static const u8 mei_start_wd_params
[] = { 0x02, 0x12, 0x13, 0x10 };
40 static const u8 mei_stop_wd_params
[] = { 0x02, 0x02, 0x14, 0x10 };
42 const u8 mei_wd_state_independence_msg
[3][4] = {
43 {0x05, 0x02, 0x51, 0x10},
44 {0x05, 0x02, 0x52, 0x10},
45 {0x07, 0x02, 0x01, 0x10}
48 /* UUIDs for AMT F/W clients */
49 const uuid_le mei_wd_guid
= UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
50 0x9D, 0xA9, 0x15, 0x14, 0xCB,
54 void mei_wd_start_setup(struct mei_device
*dev
)
56 dev_dbg(&dev
->pdev
->dev
, "dev->wd_timeout=%d.\n", dev
->wd_timeout
);
57 memcpy(dev
->wd_data
, mei_start_wd_params
, MEI_WD_PARAMS_SIZE
);
58 memcpy(dev
->wd_data
+ MEI_WD_PARAMS_SIZE
,
59 &dev
->wd_timeout
, sizeof(u16
));
63 * host_init_wd - mei initialization wd.
65 * @dev: the device structure
67 bool mei_wd_host_init(struct mei_device
*dev
)
71 mei_cl_init(&dev
->wd_cl
, dev
);
73 /* look for WD client and connect to it */
74 dev
->wd_cl
.state
= MEI_FILE_DISCONNECTED
;
75 dev
->wd_timeout
= watchdog_timeout
;
77 if (dev
->wd_timeout
> 0) {
78 mei_wd_start_setup(dev
);
79 /* find ME WD client */
80 mei_find_me_client_update_filext(dev
, &dev
->wd_cl
,
81 &mei_wd_guid
, MEI_WD_HOST_CLIENT_ID
);
83 dev_dbg(&dev
->pdev
->dev
, "check wd_cl\n");
84 if (MEI_FILE_CONNECTING
== dev
->wd_cl
.state
) {
85 if (!mei_connect(dev
, &dev
->wd_cl
)) {
86 dev_dbg(&dev
->pdev
->dev
, "Failed to connect to WD client\n");
87 dev
->wd_cl
.state
= MEI_FILE_DISCONNECTED
;
88 dev
->wd_cl
.host_client_id
= 0;
92 dev
->wd_cl
.timer_count
= CONNECT_TIMEOUT
;
95 dev_dbg(&dev
->pdev
->dev
, "Failed to find WD client\n");
100 dev
->wd_bypass
= true;
101 dev_dbg(&dev
->pdev
->dev
, "WD requested to be disabled\n");
111 * mei_wd_send - sends watch dog message to fw.
113 * @dev: the device structure
115 * returns 0 if success,
116 * -EIO when message send fails
117 * -EINVAL when invalid message is to be sent
119 int mei_wd_send(struct mei_device
*dev
)
121 struct mei_msg_hdr
*mei_hdr
;
123 mei_hdr
= (struct mei_msg_hdr
*) &dev
->wr_msg_buf
[0];
124 mei_hdr
->host_addr
= dev
->wd_cl
.host_client_id
;
125 mei_hdr
->me_addr
= dev
->wd_cl
.me_client_id
;
126 mei_hdr
->msg_complete
= 1;
127 mei_hdr
->reserved
= 0;
129 if (!memcmp(dev
->wd_data
, mei_start_wd_params
, MEI_WD_PARAMS_SIZE
))
130 mei_hdr
->length
= MEI_START_WD_DATA_SIZE
;
131 else if (!memcmp(dev
->wd_data
, mei_stop_wd_params
, MEI_WD_PARAMS_SIZE
))
132 mei_hdr
->length
= MEI_WD_PARAMS_SIZE
;
136 if (mei_write_message(dev
, mei_hdr
, dev
->wd_data
, mei_hdr
->length
))
141 int mei_wd_stop(struct mei_device
*dev
, bool preserve
)
144 u16 wd_timeout
= dev
->wd_timeout
;
146 cancel_delayed_work(&dev
->wd_work
);
147 if (dev
->wd_cl
.state
!= MEI_FILE_CONNECTED
|| !dev
->wd_timeout
)
151 dev
->wd_due_counter
= 0;
152 memcpy(dev
->wd_data
, mei_stop_wd_params
, MEI_WD_PARAMS_SIZE
);
155 ret
= mei_flow_ctrl_creds(dev
, &dev
->wd_cl
);
159 if (ret
&& dev
->mei_host_buffer_is_empty
) {
161 dev
->mei_host_buffer_is_empty
= false;
163 if (!mei_wd_send(dev
)) {
164 ret
= mei_flow_ctrl_reduce(dev
, &dev
->wd_cl
);
168 dev_dbg(&dev
->pdev
->dev
, "send stop WD failed\n");
171 dev
->wd_pending
= false;
173 dev
->wd_pending
= true;
175 dev
->wd_stopped
= false;
176 mutex_unlock(&dev
->device_lock
);
178 ret
= wait_event_interruptible_timeout(dev
->wait_stop_wd
,
179 dev
->wd_stopped
, 10 * HZ
);
180 mutex_lock(&dev
->device_lock
);
181 if (dev
->wd_stopped
) {
182 dev_dbg(&dev
->pdev
->dev
, "stop wd complete ret=%d.\n", ret
);
187 dev_warn(&dev
->pdev
->dev
,
188 "stop wd failed to complete ret=%d.\n", ret
);
192 dev
->wd_timeout
= wd_timeout
;
199 * mei_wd_ops_start - wd start command from the watchdog core.
201 * @wd_dev - watchdog device struct
203 * returns 0 if success, negative errno code for failure
205 static int mei_wd_ops_start(struct watchdog_device
*wd_dev
)
208 struct mei_device
*dev
;
210 dev
= pci_get_drvdata(mei_device
);
214 mutex_lock(&dev
->device_lock
);
216 if (dev
->mei_state
!= MEI_ENABLED
) {
217 dev_dbg(&dev
->pdev
->dev
, "mei_state != MEI_ENABLED mei_state= %d\n",
222 if (dev
->wd_cl
.state
!= MEI_FILE_CONNECTED
) {
223 dev_dbg(&dev
->pdev
->dev
, "MEI Driver is not connected to Watchdog Client\n");
227 mei_wd_start_setup(dev
);
231 mutex_unlock(&dev
->device_lock
);
236 * mei_wd_ops_stop - wd stop command from the watchdog core.
238 * @wd_dev - watchdog device struct
240 * returns 0 if success, negative errno code for failure
242 static int mei_wd_ops_stop(struct watchdog_device
*wd_dev
)
244 struct mei_device
*dev
;
245 dev
= pci_get_drvdata(mei_device
);
250 mutex_lock(&dev
->device_lock
);
251 mei_wd_stop(dev
, false);
252 mutex_unlock(&dev
->device_lock
);
258 * mei_wd_ops_ping - wd ping command from the watchdog core.
260 * @wd_dev - watchdog device struct
262 * returns 0 if success, negative errno code for failure
264 static int mei_wd_ops_ping(struct watchdog_device
*wd_dev
)
267 struct mei_device
*dev
;
268 dev
= pci_get_drvdata(mei_device
);
273 mutex_lock(&dev
->device_lock
);
275 if (dev
->wd_cl
.state
!= MEI_FILE_CONNECTED
) {
276 dev_dbg(&dev
->pdev
->dev
, "wd is not connected.\n");
281 /* Check if we can send the ping to HW*/
282 if (dev
->mei_host_buffer_is_empty
&&
283 mei_flow_ctrl_creds(dev
, &dev
->wd_cl
) > 0) {
285 dev
->mei_host_buffer_is_empty
= false;
286 dev_dbg(&dev
->pdev
->dev
, "sending watchdog ping\n");
288 if (mei_wd_send(dev
)) {
289 dev_dbg(&dev
->pdev
->dev
, "wd send failed.\n");
294 if (mei_flow_ctrl_reduce(dev
, &dev
->wd_cl
)) {
295 dev_dbg(&dev
->pdev
->dev
, "mei_flow_ctrl_reduce() failed.\n");
301 dev
->wd_pending
= true;
305 mutex_unlock(&dev
->device_lock
);
310 * Watchdog Device structs
312 const struct watchdog_ops wd_ops
= {
313 .owner
= THIS_MODULE
,
314 .start
= mei_wd_ops_start
,
315 .stop
= mei_wd_ops_stop
,
316 .ping
= mei_wd_ops_ping
,
318 const struct watchdog_info wd_info
= {
319 .identity
= INTEL_AMT_WATCHDOG_ID
,
320 .options
= WDIOF_KEEPALIVEPING
,
323 struct watchdog_device amt_wd_dev
= {
326 .timeout
= AMT_WD_DEFAULT_TIMEOUT
,
327 .min_timeout
= AMT_WD_MIN_TIMEOUT
,
328 .max_timeout
= AMT_WD_MAX_TIMEOUT
,