wd.c 8.8 KB
Newer Older
O
Oren Weil 已提交
1 2 3
/*
 *
 * Intel Management Engine Interface (Intel MEI) Linux driver
4
 * Copyright (c) 2003-2012, Intel Corporation.
O
Oren Weil 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/device.h>
#include <linux/pci.h>
#include <linux/sched.h>
22
#include <linux/watchdog.h>
O
Oren Weil 已提交
23 24 25 26

#include "mei_dev.h"
#include "hw.h"
#include "interface.h"
27
#include <linux/mei.h>
O
Oren Weil 已提交
28 29 30 31 32 33 34 35 36 37

static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };

const u8 mei_wd_state_independence_msg[3][4] = {
	{0x05, 0x02, 0x51, 0x10},
	{0x05, 0x02, 0x52, 0x10},
	{0x07, 0x02, 0x01, 0x10}
};

38 39 40 41 42
/*
 * AMT Watchdog Device
 */
#define INTEL_AMT_WATCHDOG_ID "INTCAMT"

O
Oren Weil 已提交
43 44 45 46 47
/* UUIDs for AMT F/W clients */
const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
						0x9D, 0xA9, 0x15, 0x14, 0xCB,
						0x32, 0xAB);

48
static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
O
Oren Weil 已提交
49
{
50
	dev_dbg(&dev->pdev->dev, "wd: set timeout=%d.\n", timeout);
51 52
	memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
	memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
O
Oren Weil 已提交
53 54 55
}

/**
56
 * mei_wd_host_init - connect to the watchdog client
O
Oren Weil 已提交
57 58
 *
 * @dev: the device structure
59 60
 * returns -ENENT if wd client cannot be found
 *         -EIO if write has failed
61
 *         0 on success
O
Oren Weil 已提交
62
 */
63
int mei_wd_host_init(struct mei_device *dev)
O
Oren Weil 已提交
64
{
65
	mei_cl_init(&dev->wd_cl, dev);
O
Oren Weil 已提交
66 67 68

	/* look for WD client and connect to it */
	dev->wd_cl.state = MEI_FILE_DISCONNECTED;
69
	dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
70
	dev->wd_state = MEI_WD_IDLE;
O
Oren Weil 已提交
71

72
	/* find ME WD client */
73
	mei_me_cl_update_filext(dev, &dev->wd_cl,
74
				&mei_wd_guid, MEI_WD_HOST_CLIENT_ID);
O
Oren Weil 已提交
75

76 77 78 79 80 81 82 83 84 85 86
	dev_dbg(&dev->pdev->dev, "wd: check client\n");
	if (MEI_FILE_CONNECTING != dev->wd_cl.state) {
		dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
		return -ENOENT;
	}

	if (mei_connect(dev, &dev->wd_cl)) {
		dev_err(&dev->pdev->dev, "wd: failed to connect to the client\n");
		dev->wd_cl.state = MEI_FILE_DISCONNECTED;
		dev->wd_cl.host_client_id = 0;
		return -EIO;
O
Oren Weil 已提交
87
	}
88
	dev->wd_cl.timer_count = MEI_CONNECT_TIMEOUT;
89

90
	return 0;
O
Oren Weil 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

/**
 * mei_wd_send - sends watch dog message to fw.
 *
 * @dev: the device structure
 *
 * returns 0 if success,
 *	-EIO when message send fails
 *	-EINVAL when invalid message is to be sent
 */
int mei_wd_send(struct mei_device *dev)
{
	struct mei_msg_hdr *mei_hdr;

	mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
	mei_hdr->host_addr = dev->wd_cl.host_client_id;
	mei_hdr->me_addr = dev->wd_cl.me_client_id;
	mei_hdr->msg_complete = 1;
	mei_hdr->reserved = 0;

112 113 114 115
	if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
		mei_hdr->length = MEI_WD_START_MSG_SIZE;
	else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
		mei_hdr->length = MEI_WD_STOP_MSG_SIZE;
O
Oren Weil 已提交
116 117 118
	else
		return -EINVAL;

119
	return mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length);
O
Oren Weil 已提交
120 121
}

122 123 124 125 126 127 128 129 130 131
/**
 * mei_wd_stop - sends watchdog stop message to fw.
 *
 * @dev: the device structure
 * @preserve: indicate if to keep the timeout value
 *
 * returns 0 if success,
 *	-EIO when message send fails
 *	-EINVAL when invalid message is to be sent
 */
132
int mei_wd_stop(struct mei_device *dev)
O
Oren Weil 已提交
133 134 135
{
	int ret;

136 137
	if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
	    dev->wd_state != MEI_WD_RUNNING)
O
Oren Weil 已提交
138 139
		return 0;

140
	memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
141 142

	dev->wd_state = MEI_WD_STOPPING;
O
Oren Weil 已提交
143 144 145 146 147 148 149

	ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
	if (ret < 0)
		goto out;

	if (ret && dev->mei_host_buffer_is_empty) {
		ret = 0;
150
		dev->mei_host_buffer_is_empty = false;
O
Oren Weil 已提交
151 152 153 154 155 156

		if (!mei_wd_send(dev)) {
			ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
			if (ret)
				goto out;
		} else {
157
			dev_err(&dev->pdev->dev, "wd: send stop failed\n");
O
Oren Weil 已提交
158 159
		}

160
		dev->wd_pending = false;
O
Oren Weil 已提交
161
	} else {
162
		dev->wd_pending = true;
O
Oren Weil 已提交
163
	}
164

O
Oren Weil 已提交
165 166 167
	mutex_unlock(&dev->device_lock);

	ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
168 169
					dev->wd_state == MEI_WD_IDLE,
					msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
O
Oren Weil 已提交
170
	mutex_lock(&dev->device_lock);
171
	if (dev->wd_state == MEI_WD_IDLE) {
172
		dev_dbg(&dev->pdev->dev, "wd: stop completed ret=%d.\n", ret);
173 174 175 176 177
		ret = 0;
	} else {
		if (!ret)
			ret = -ETIMEDOUT;
		dev_warn(&dev->pdev->dev,
178
			"wd: stop failed to complete ret=%d.\n", ret);
179
	}
O
Oren Weil 已提交
180 181 182 183 184

out:
	return ret;
}

O
Oren Weil 已提交
185 186 187 188 189 190 191 192 193 194 195 196
/*
 * mei_wd_ops_start - wd start command from the watchdog core.
 *
 * @wd_dev - watchdog device struct
 *
 * returns 0 if success, negative errno code for failure
 */
static int mei_wd_ops_start(struct watchdog_device *wd_dev)
{
	int err = -ENODEV;
	struct mei_device *dev;

197
	dev = watchdog_get_drvdata(wd_dev);
O
Oren Weil 已提交
198 199 200 201 202
	if (!dev)
		return -ENODEV;

	mutex_lock(&dev->device_lock);

203
	if (dev->dev_state != MEI_DEV_ENABLED) {
204
		dev_dbg(&dev->pdev->dev,
205 206
			"wd: dev_state != MEI_DEV_ENABLED  dev_state = %s\n",
			mei_dev_state_str(dev->dev_state));
O
Oren Weil 已提交
207 208 209 210
		goto end_unlock;
	}

	if (dev->wd_cl.state != MEI_FILE_CONNECTED)	{
211 212
		dev_dbg(&dev->pdev->dev,
			"MEI Driver is not connected to Watchdog Client\n");
O
Oren Weil 已提交
213 214 215
		goto end_unlock;
	}

216
	mei_wd_set_start_timeout(dev, dev->wd_timeout);
O
Oren Weil 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

	err = 0;
end_unlock:
	mutex_unlock(&dev->device_lock);
	return err;
}

/*
 * mei_wd_ops_stop -  wd stop command from the watchdog core.
 *
 * @wd_dev - watchdog device struct
 *
 * returns 0 if success, negative errno code for failure
 */
static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
{
	struct mei_device *dev;

235
	dev = watchdog_get_drvdata(wd_dev);
O
Oren Weil 已提交
236 237 238 239
	if (!dev)
		return -ENODEV;

	mutex_lock(&dev->device_lock);
240
	mei_wd_stop(dev);
O
Oren Weil 已提交
241 242 243 244 245
	mutex_unlock(&dev->device_lock);

	return 0;
}

O
Oren Weil 已提交
246 247 248 249 250 251 252 253 254 255 256 257
/*
 * mei_wd_ops_ping - wd ping command from the watchdog core.
 *
 * @wd_dev - watchdog device struct
 *
 * returns 0 if success, negative errno code for failure
 */
static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
{
	int ret = 0;
	struct mei_device *dev;

258
	dev = watchdog_get_drvdata(wd_dev);
O
Oren Weil 已提交
259 260 261 262 263 264
	if (!dev)
		return -ENODEV;

	mutex_lock(&dev->device_lock);

	if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
265
		dev_err(&dev->pdev->dev, "wd: not connected.\n");
O
Oren Weil 已提交
266 267 268 269
		ret = -ENODEV;
		goto end;
	}

270 271
	dev->wd_state = MEI_WD_RUNNING;

O
Oren Weil 已提交
272 273 274 275 276
	/* Check if we can send the ping to HW*/
	if (dev->mei_host_buffer_is_empty &&
		mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {

		dev->mei_host_buffer_is_empty = false;
277
		dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
O
Oren Weil 已提交
278 279

		if (mei_wd_send(dev)) {
280
			dev_err(&dev->pdev->dev, "wd: send failed.\n");
O
Oren Weil 已提交
281 282 283 284 285
			ret = -EIO;
			goto end;
		}

		if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
286 287
			dev_err(&dev->pdev->dev,
				"wd: mei_flow_ctrl_reduce() failed.\n");
O
Oren Weil 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300
			ret = -EIO;
			goto end;
		}

	} else {
		dev->wd_pending = true;
	}

end:
	mutex_unlock(&dev->device_lock);
	return ret;
}

301 302 303 304 305 306 307 308 309 310 311 312
/*
 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
 *
 * @wd_dev - watchdog device struct
 * @timeout - timeout value to set
 *
 * returns 0 if success, negative errno code for failure
 */
static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, unsigned int timeout)
{
	struct mei_device *dev;

313
	dev = watchdog_get_drvdata(wd_dev);
314 315 316 317
	if (!dev)
		return -ENODEV;

	/* Check Timeout value */
318
	if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
319 320 321 322 323
		return -EINVAL;

	mutex_lock(&dev->device_lock);

	dev->wd_timeout = timeout;
324
	wd_dev->timeout = timeout;
325 326 327 328 329 330 331
	mei_wd_set_start_timeout(dev, dev->wd_timeout);

	mutex_unlock(&dev->device_lock);

	return 0;
}

O
Oren Weil 已提交
332 333 334
/*
 * Watchdog Device structs
 */
335
static const struct watchdog_ops wd_ops = {
O
Oren Weil 已提交
336 337 338
		.owner = THIS_MODULE,
		.start = mei_wd_ops_start,
		.stop = mei_wd_ops_stop,
O
Oren Weil 已提交
339
		.ping = mei_wd_ops_ping,
340
		.set_timeout = mei_wd_ops_set_timeout,
O
Oren Weil 已提交
341
};
342
static const struct watchdog_info wd_info = {
O
Oren Weil 已提交
343
		.identity = INTEL_AMT_WATCHDOG_ID,
344 345 346
		.options = WDIOF_KEEPALIVEPING |
			   WDIOF_SETTIMEOUT |
			   WDIOF_ALARMONLY,
O
Oren Weil 已提交
347 348
};

349
static struct watchdog_device amt_wd_dev = {
O
Oren Weil 已提交
350 351
		.info = &wd_info,
		.ops = &wd_ops,
352 353 354
		.timeout = MEI_WD_DEFAULT_TIMEOUT,
		.min_timeout = MEI_WD_MIN_TIMEOUT,
		.max_timeout = MEI_WD_MAX_TIMEOUT,
O
Oren Weil 已提交
355 356 357
};


358
void mei_watchdog_register(struct mei_device *dev)
359 360
{
	if (watchdog_register_device(&amt_wd_dev)) {
361 362
		dev_err(&dev->pdev->dev,
			"wd: unable to register watchdog device.\n");
363
		return;
364
	}
365 366 367 368

	dev_dbg(&dev->pdev->dev,
		"wd: successfully register watchdog interface.\n");
	watchdog_set_drvdata(&amt_wd_dev, dev);
369 370 371 372
}

void mei_watchdog_unregister(struct mei_device *dev)
{
373
	if (test_bit(WDOG_UNREGISTERED, &amt_wd_dev.status))
374 375 376 377
		return;

	watchdog_set_drvdata(&amt_wd_dev, NULL);
	watchdog_unregister_device(&amt_wd_dev);
378 379
}