blob: d96c537f046ffd4abf58b2a84db5e5dc48690747 [file] [log] [blame]
Oren Weil3ceb3e62011-05-15 13:43:47 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba912012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weil3ceb3e62011-05-15 13:43:47 +03005 *
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.
9 *
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.
14 *
15 */
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>
Oren Weil9ce178e2011-09-07 09:03:09 +030022#include <linux/watchdog.h>
Oren Weil3ceb3e62011-05-15 13:43:47 +030023
24#include "mei_dev.h"
25#include "hw.h"
26#include "interface.h"
Tomas Winkler4f3afe12012-05-09 16:38:59 +030027#include <linux/mei.h>
Oren Weil3ceb3e62011-05-15 13:43:47 +030028
Oren Weil3ceb3e62011-05-15 13:43:47 +030029static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
30static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
31
32const u8 mei_wd_state_independence_msg[3][4] = {
33 {0x05, 0x02, 0x51, 0x10},
34 {0x05, 0x02, 0x52, 0x10},
35 {0x07, 0x02, 0x01, 0x10}
36};
37
Tomas Winkler70cd5332011-12-22 18:50:50 +020038/*
39 * AMT Watchdog Device
40 */
41#define INTEL_AMT_WATCHDOG_ID "INTCAMT"
42
Oren Weil3ceb3e62011-05-15 13:43:47 +030043/* UUIDs for AMT F/W clients */
44const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
45 0x9D, 0xA9, 0x15, 0x14, 0xCB,
46 0x32, 0xAB);
47
Tomas Winkler5192dda2012-03-19 17:58:43 +020048static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
Oren Weil3ceb3e62011-05-15 13:43:47 +030049{
Tomas Winkler13c398a2012-04-03 23:34:59 +030050 dev_dbg(&dev->pdev->dev, "wd: set timeout=%d.\n", timeout);
Tomas Winkler248ffdf2012-08-16 19:39:42 +030051 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
52 memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
Oren Weil3ceb3e62011-05-15 13:43:47 +030053}
54
55/**
Tomas Winkler7cb1ba92012-07-04 19:24:51 +030056 * mei_wd_host_init - connect to the watchdog client
Oren Weil3ceb3e62011-05-15 13:43:47 +030057 *
58 * @dev: the device structure
Tomas Winkler9487eb02012-04-03 23:34:58 +030059 * returns -ENENT if wd client cannot be found
60 * -EIO if write has failed
Tomas Winkler7cb1ba92012-07-04 19:24:51 +030061 * 0 on success
Oren Weil3ceb3e62011-05-15 13:43:47 +030062 */
Tomas Winkler9487eb02012-04-03 23:34:58 +030063int mei_wd_host_init(struct mei_device *dev)
Oren Weil3ceb3e62011-05-15 13:43:47 +030064{
Tomas Winklerc95efb72011-05-25 17:28:21 +030065 mei_cl_init(&dev->wd_cl, dev);
Oren Weil3ceb3e62011-05-15 13:43:47 +030066
67 /* look for WD client and connect to it */
68 dev->wd_cl.state = MEI_FILE_DISCONNECTED;
Tomas Winkler248ffdf2012-08-16 19:39:42 +030069 dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
Tomas Winklerc216fde2012-08-16 19:39:43 +030070 dev->wd_state = MEI_WD_IDLE;
Oren Weil3ceb3e62011-05-15 13:43:47 +030071
Oren Weil2ad64ba2011-09-07 09:03:15 +030072 /* find ME WD client */
Tomas Winkler07b509b2012-07-23 14:05:39 +030073 mei_me_cl_update_filext(dev, &dev->wd_cl,
Oren Weil2ad64ba2011-09-07 09:03:15 +030074 &mei_wd_guid, MEI_WD_HOST_CLIENT_ID);
Oren Weil3ceb3e62011-05-15 13:43:47 +030075
Tomas Winkler9487eb02012-04-03 23:34:58 +030076 dev_dbg(&dev->pdev->dev, "wd: check client\n");
77 if (MEI_FILE_CONNECTING != dev->wd_cl.state) {
78 dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
79 return -ENOENT;
Oren Weil3ceb3e62011-05-15 13:43:47 +030080 }
Oren Weil617aa392011-09-07 09:03:07 +030081
Tomas Winkler9487eb02012-04-03 23:34:58 +030082 if (mei_connect(dev, &dev->wd_cl)) {
83 dev_err(&dev->pdev->dev, "wd: failed to connect to the client\n");
84 dev->wd_cl.state = MEI_FILE_DISCONNECTED;
85 dev->wd_cl.host_client_id = 0;
86 return -EIO;
87 }
88 dev->wd_cl.timer_count = CONNECT_TIMEOUT;
89
90 return 0;
Oren Weil3ceb3e62011-05-15 13:43:47 +030091}
92
93/**
94 * mei_wd_send - sends watch dog message to fw.
95 *
96 * @dev: the device structure
97 *
98 * returns 0 if success,
99 * -EIO when message send fails
100 * -EINVAL when invalid message is to be sent
101 */
102int mei_wd_send(struct mei_device *dev)
103{
104 struct mei_msg_hdr *mei_hdr;
105
106 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
107 mei_hdr->host_addr = dev->wd_cl.host_client_id;
108 mei_hdr->me_addr = dev->wd_cl.me_client_id;
109 mei_hdr->msg_complete = 1;
110 mei_hdr->reserved = 0;
111
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300112 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
113 mei_hdr->length = MEI_WD_START_MSG_SIZE;
114 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
115 mei_hdr->length = MEI_WD_STOP_MSG_SIZE;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300116 else
117 return -EINVAL;
118
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200119 return mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length);
Oren Weil3ceb3e62011-05-15 13:43:47 +0300120}
121
Oren Weild8deca32011-09-07 09:03:17 +0300122/**
123 * mei_wd_stop - sends watchdog stop message to fw.
124 *
125 * @dev: the device structure
126 * @preserve: indicate if to keep the timeout value
127 *
128 * returns 0 if success,
129 * -EIO when message send fails
130 * -EINVAL when invalid message is to be sent
131 */
Tomas Winklerc216fde2012-08-16 19:39:43 +0300132int mei_wd_stop(struct mei_device *dev)
Oren Weil3ceb3e62011-05-15 13:43:47 +0300133{
134 int ret;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300135
Tomas Winklerc216fde2012-08-16 19:39:43 +0300136 if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
137 dev->wd_state != MEI_WD_RUNNING)
Oren Weil3ceb3e62011-05-15 13:43:47 +0300138 return 0;
139
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300140 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
Tomas Winklerc216fde2012-08-16 19:39:43 +0300141
142 dev->wd_state = MEI_WD_STOPPING;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300143
144 ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
145 if (ret < 0)
146 goto out;
147
148 if (ret && dev->mei_host_buffer_is_empty) {
149 ret = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300150 dev->mei_host_buffer_is_empty = false;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300151
152 if (!mei_wd_send(dev)) {
153 ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
154 if (ret)
155 goto out;
156 } else {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300157 dev_err(&dev->pdev->dev, "wd: send stop failed\n");
Oren Weil3ceb3e62011-05-15 13:43:47 +0300158 }
159
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300160 dev->wd_pending = false;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300161 } else {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300162 dev->wd_pending = true;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300163 }
Tomas Winklerc216fde2012-08-16 19:39:43 +0300164
Oren Weil3ceb3e62011-05-15 13:43:47 +0300165 mutex_unlock(&dev->device_lock);
166
167 ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
Tomas Winklerc216fde2012-08-16 19:39:43 +0300168 dev->wd_state == MEI_WD_IDLE,
169 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
Oren Weil3ceb3e62011-05-15 13:43:47 +0300170 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +0300171 if (dev->wd_state == MEI_WD_IDLE) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300172 dev_dbg(&dev->pdev->dev, "wd: stop completed ret=%d.\n", ret);
Tomas Winklera534bb62011-06-13 16:39:31 +0300173 ret = 0;
174 } else {
175 if (!ret)
176 ret = -ETIMEDOUT;
177 dev_warn(&dev->pdev->dev,
Tomas Winkler13c398a2012-04-03 23:34:59 +0300178 "wd: stop failed to complete ret=%d.\n", ret);
Tomas Winklera534bb62011-06-13 16:39:31 +0300179 }
Oren Weil3ceb3e62011-05-15 13:43:47 +0300180
Oren Weil3ceb3e62011-05-15 13:43:47 +0300181out:
182 return ret;
183}
184
Oren Weil4a3cafd2011-09-07 09:03:10 +0300185/*
186 * mei_wd_ops_start - wd start command from the watchdog core.
187 *
188 * @wd_dev - watchdog device struct
189 *
190 * returns 0 if success, negative errno code for failure
191 */
192static int mei_wd_ops_start(struct watchdog_device *wd_dev)
193{
194 int err = -ENODEV;
195 struct mei_device *dev;
196
Tomas Winkler09649a82012-08-16 19:39:44 +0300197 dev = watchdog_get_drvdata(wd_dev);
Oren Weil4a3cafd2011-09-07 09:03:10 +0300198 if (!dev)
199 return -ENODEV;
200
201 mutex_lock(&dev->device_lock);
202
Tomas Winklerb210d752012-08-07 00:03:56 +0300203 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300204 dev_dbg(&dev->pdev->dev,
Tomas Winklerb210d752012-08-07 00:03:56 +0300205 "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
206 mei_dev_state_str(dev->dev_state));
Oren Weil4a3cafd2011-09-07 09:03:10 +0300207 goto end_unlock;
208 }
209
210 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300211 dev_dbg(&dev->pdev->dev,
212 "MEI Driver is not connected to Watchdog Client\n");
Oren Weil4a3cafd2011-09-07 09:03:10 +0300213 goto end_unlock;
214 }
215
Oren Weil55d33852011-09-07 09:03:12 +0300216 mei_wd_set_start_timeout(dev, dev->wd_timeout);
Oren Weil4a3cafd2011-09-07 09:03:10 +0300217
218 err = 0;
219end_unlock:
220 mutex_unlock(&dev->device_lock);
221 return err;
222}
223
224/*
225 * mei_wd_ops_stop - wd stop command from the watchdog core.
226 *
227 * @wd_dev - watchdog device struct
228 *
229 * returns 0 if success, negative errno code for failure
230 */
231static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
232{
233 struct mei_device *dev;
Oren Weil4a3cafd2011-09-07 09:03:10 +0300234
Tomas Winkler09649a82012-08-16 19:39:44 +0300235 dev = watchdog_get_drvdata(wd_dev);
Oren Weil4a3cafd2011-09-07 09:03:10 +0300236 if (!dev)
237 return -ENODEV;
238
239 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +0300240 mei_wd_stop(dev);
Oren Weil4a3cafd2011-09-07 09:03:10 +0300241 mutex_unlock(&dev->device_lock);
242
243 return 0;
244}
245
246/*
Oren Weil8c4a59a2011-09-07 09:03:11 +0300247 * mei_wd_ops_ping - wd ping command from the watchdog core.
248 *
249 * @wd_dev - watchdog device struct
250 *
251 * returns 0 if success, negative errno code for failure
252 */
253static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
254{
255 int ret = 0;
256 struct mei_device *dev;
Oren Weil8c4a59a2011-09-07 09:03:11 +0300257
Tomas Winkler09649a82012-08-16 19:39:44 +0300258 dev = watchdog_get_drvdata(wd_dev);
Oren Weil8c4a59a2011-09-07 09:03:11 +0300259 if (!dev)
260 return -ENODEV;
261
262 mutex_lock(&dev->device_lock);
263
264 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300265 dev_err(&dev->pdev->dev, "wd: not connected.\n");
Oren Weil8c4a59a2011-09-07 09:03:11 +0300266 ret = -ENODEV;
267 goto end;
268 }
269
Tomas Winklerc216fde2012-08-16 19:39:43 +0300270 dev->wd_state = MEI_WD_RUNNING;
271
Oren Weil8c4a59a2011-09-07 09:03:11 +0300272 /* 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) {
275
276 dev->mei_host_buffer_is_empty = false;
Tomas Winkler13c398a2012-04-03 23:34:59 +0300277 dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
Oren Weil8c4a59a2011-09-07 09:03:11 +0300278
279 if (mei_wd_send(dev)) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300280 dev_err(&dev->pdev->dev, "wd: send failed.\n");
Oren Weil8c4a59a2011-09-07 09:03:11 +0300281 ret = -EIO;
282 goto end;
283 }
284
285 if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300286 dev_err(&dev->pdev->dev,
287 "wd: mei_flow_ctrl_reduce() failed.\n");
Oren Weil8c4a59a2011-09-07 09:03:11 +0300288 ret = -EIO;
289 goto end;
290 }
291
292 } else {
293 dev->wd_pending = true;
294 }
295
296end:
297 mutex_unlock(&dev->device_lock);
298 return ret;
299}
300
301/*
Oren Weil55d33852011-09-07 09:03:12 +0300302 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
303 *
304 * @wd_dev - watchdog device struct
305 * @timeout - timeout value to set
306 *
307 * returns 0 if success, negative errno code for failure
308 */
309static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, unsigned int timeout)
310{
311 struct mei_device *dev;
Oren Weil55d33852011-09-07 09:03:12 +0300312
Tomas Winkler09649a82012-08-16 19:39:44 +0300313 dev = watchdog_get_drvdata(wd_dev);
Oren Weil55d33852011-09-07 09:03:12 +0300314 if (!dev)
315 return -ENODEV;
316
317 /* Check Timeout value */
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300318 if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
Oren Weil55d33852011-09-07 09:03:12 +0300319 return -EINVAL;
320
321 mutex_lock(&dev->device_lock);
322
323 dev->wd_timeout = timeout;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100324 wd_dev->timeout = timeout;
Oren Weil55d33852011-09-07 09:03:12 +0300325 mei_wd_set_start_timeout(dev, dev->wd_timeout);
326
327 mutex_unlock(&dev->device_lock);
328
329 return 0;
330}
331
332/*
Oren Weil4a3cafd2011-09-07 09:03:10 +0300333 * Watchdog Device structs
334 */
Tomas Winkler575c1e42011-10-23 18:30:39 +0200335static const struct watchdog_ops wd_ops = {
Oren Weil4a3cafd2011-09-07 09:03:10 +0300336 .owner = THIS_MODULE,
337 .start = mei_wd_ops_start,
338 .stop = mei_wd_ops_stop,
Oren Weil8c4a59a2011-09-07 09:03:11 +0300339 .ping = mei_wd_ops_ping,
Oren Weil55d33852011-09-07 09:03:12 +0300340 .set_timeout = mei_wd_ops_set_timeout,
Oren Weil4a3cafd2011-09-07 09:03:10 +0300341};
Tomas Winkler575c1e42011-10-23 18:30:39 +0200342static const struct watchdog_info wd_info = {
Oren Weil4a3cafd2011-09-07 09:03:10 +0300343 .identity = INTEL_AMT_WATCHDOG_ID,
Tomas Winklerc8df7292012-08-16 19:39:41 +0300344 .options = WDIOF_KEEPALIVEPING |
345 WDIOF_SETTIMEOUT |
346 WDIOF_ALARMONLY,
Oren Weil4a3cafd2011-09-07 09:03:10 +0300347};
348
Tomas Winkler68d923d2012-04-02 20:32:38 +0300349static struct watchdog_device amt_wd_dev = {
Oren Weil4a3cafd2011-09-07 09:03:10 +0300350 .info = &wd_info,
351 .ops = &wd_ops,
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300352 .timeout = MEI_WD_DEFAULT_TIMEOUT,
353 .min_timeout = MEI_WD_MIN_TIMEOUT,
354 .max_timeout = MEI_WD_MAX_TIMEOUT,
Oren Weil4a3cafd2011-09-07 09:03:10 +0300355};
356
357
Tomas Winkler09649a82012-08-16 19:39:44 +0300358void mei_watchdog_register(struct mei_device *dev)
Tomas Winkler70cd5332011-12-22 18:50:50 +0200359{
Tomas Winkler70cd5332011-12-22 18:50:50 +0200360 if (watchdog_register_device(&amt_wd_dev)) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300361 dev_err(&dev->pdev->dev,
362 "wd: unable to register watchdog device.\n");
Tomas Winkler70cd5332011-12-22 18:50:50 +0200363 dev->wd_interface_reg = false;
Tomas Winkler09649a82012-08-16 19:39:44 +0300364 return;
Tomas Winkler70cd5332011-12-22 18:50:50 +0200365 }
Tomas Winkler09649a82012-08-16 19:39:44 +0300366
367 dev_dbg(&dev->pdev->dev,
368 "wd: successfully register watchdog interface.\n");
369 dev->wd_interface_reg = true;
370 watchdog_set_drvdata(&amt_wd_dev, dev);
Tomas Winkler70cd5332011-12-22 18:50:50 +0200371}
372
373void mei_watchdog_unregister(struct mei_device *dev)
374{
Tomas Winkler09649a82012-08-16 19:39:44 +0300375 if (!dev->wd_interface_reg)
376 return;
377
378 watchdog_set_drvdata(&amt_wd_dev, NULL);
379 watchdog_unregister_device(&amt_wd_dev);
Tomas Winkler70cd5332011-12-22 18:50:50 +0200380 dev->wd_interface_reg = false;
381}
382