wd.c 8.7 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
#include <linux/mei.h>

O
Oren Weil 已提交
26
#include "mei_dev.h"
27
#include "hbm.h"
T
Tomas Winkler 已提交
28
#include "client.h"
O
Oren Weil 已提交
29 30 31 32

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

33 34 35 36 37
/*
 * AMT Watchdog Device
 */
#define INTEL_AMT_WATCHDOG_ID "INTCAMT"

O
Oren Weil 已提交
38 39 40 41 42
/* 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);

43
static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
O
Oren Weil 已提交
44
{
45
	dev_dbg(dev->dev, "wd: set timeout=%d.\n", timeout);
46 47
	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 已提交
48 49 50
}

/**
51
 * mei_wd_host_init - connect to the watchdog client
O
Oren Weil 已提交
52 53
 *
 * @dev: the device structure
54
 *
55
 * returns -ENOTTY if wd client cannot be found
56
 *         -EIO if write has failed
57
 *         0 on success
O
Oren Weil 已提交
58
 */
59
int mei_wd_host_init(struct mei_device *dev)
O
Oren Weil 已提交
60
{
61
	struct mei_cl *cl = &dev->wd_cl;
62
	struct mei_me_client *me_cl;
63 64 65
	int ret;

	mei_cl_init(cl, dev);
O
Oren Weil 已提交
66

67
	dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
68
	dev->wd_state = MEI_WD_IDLE;
O
Oren Weil 已提交
69 70


71
	/* check for valid client id */
72 73
	me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
	if (!me_cl) {
74
		dev_info(dev->dev, "wd: failed to find the client\n");
75
		return -ENOTTY;
76 77
	}

78
	cl->me_client_id = me_cl->client_id;
79
	cl->cl_uuid = me_cl->props.protocol_name;
80 81 82 83

	ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);

	if (ret < 0) {
84
		dev_info(dev->dev, "wd: failed link client\n");
85
		return ret;
86 87
	}

88 89 90
	ret = mei_cl_connect(cl, NULL);

	if (ret) {
91
		dev_err(dev->dev, "wd: failed to connect = %d\n", ret);
92 93
		mei_cl_unlink(cl);
		return ret;
O
Oren Weil 已提交
94
	}
95

96 97 98 99 100 101
	ret = mei_watchdog_register(dev);
	if (ret) {
		mei_cl_disconnect(cl);
		mei_cl_unlink(cl);
	}
	return ret;
O
Oren Weil 已提交
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
112
 *	-ENODEV on flow control failure
O
Oren Weil 已提交
113 114 115
 */
int mei_wd_send(struct mei_device *dev)
{
116
	struct mei_cl *cl = &dev->wd_cl;
117
	struct mei_msg_hdr hdr;
118
	int ret;
O
Oren Weil 已提交
119

120 121
	hdr.host_addr = cl->host_client_id;
	hdr.me_addr = cl->me_client_id;
122 123
	hdr.msg_complete = 1;
	hdr.reserved = 0;
124
	hdr.internal = 0;
O
Oren Weil 已提交
125

126
	if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
127
		hdr.length = MEI_WD_START_MSG_SIZE;
128
	else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
129
		hdr.length = MEI_WD_STOP_MSG_SIZE;
130
	else {
131
		dev_err(dev->dev, "wd: invalid message is to be sent, aborting\n");
O
Oren Weil 已提交
132
		return -EINVAL;
133 134 135 136
	}

	ret = mei_write_message(dev, &hdr, dev->wd_data);
	if (ret) {
137
		dev_err(dev->dev, "wd: write message failed\n");
138 139
		return ret;
	}
O
Oren Weil 已提交
140

141 142
	ret = mei_cl_flow_ctrl_reduce(cl);
	if (ret) {
143
		dev_err(dev->dev, "wd: flow_ctrl_reduce failed.\n");
144 145 146 147
		return ret;
	}

	return 0;
O
Oren Weil 已提交
148 149
}

150 151 152 153 154 155
/**
 * mei_wd_stop - sends watchdog stop message to fw.
 *
 * @dev: the device structure
 * @preserve: indicate if to keep the timeout value
 *
156 157 158
 * returns 0 if success
 * on error:
 *	-EIO    when message send fails
159
 *	-EINVAL when invalid message is to be sent
160
 *	-ETIME  on message timeout
161
 */
162
int mei_wd_stop(struct mei_device *dev)
O
Oren Weil 已提交
163 164 165
{
	int ret;

166 167
	if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
	    dev->wd_state != MEI_WD_RUNNING)
O
Oren Weil 已提交
168 169
		return 0;

170
	memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
171 172

	dev->wd_state = MEI_WD_STOPPING;
O
Oren Weil 已提交
173

T
Tomas Winkler 已提交
174
	ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
O
Oren Weil 已提交
175
	if (ret < 0)
176
		goto err;
O
Oren Weil 已提交
177

178
	if (ret && mei_hbuf_acquire(dev)) {
179 180
		ret = mei_wd_send(dev);
		if (ret)
181
			goto err;
182
		dev->wd_pending = false;
O
Oren Weil 已提交
183
	} else {
184
		dev->wd_pending = true;
O
Oren Weil 已提交
185
	}
186

O
Oren Weil 已提交
187 188
	mutex_unlock(&dev->device_lock);

189 190 191
	ret = wait_event_timeout(dev->wait_stop_wd,
				dev->wd_state == MEI_WD_IDLE,
				msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
O
Oren Weil 已提交
192
	mutex_lock(&dev->device_lock);
193 194 195
	if (dev->wd_state != MEI_WD_IDLE) {
		/* timeout */
		ret = -ETIME;
196
		dev_warn(dev->dev, "wd: stop failed to complete ret=%d\n", ret);
197
		goto err;
198
	}
199
	dev_dbg(dev->dev, "wd: stop completed after %u msec\n",
200 201 202
			MEI_WD_STOP_TIMEOUT - jiffies_to_msecs(ret));
	return 0;
err:
O
Oren Weil 已提交
203 204 205
	return ret;
}

O
Oren Weil 已提交
206 207 208 209 210 211 212 213 214 215 216 217
/*
 * 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;

218
	dev = watchdog_get_drvdata(wd_dev);
O
Oren Weil 已提交
219 220 221 222 223
	if (!dev)
		return -ENODEV;

	mutex_lock(&dev->device_lock);

224
	if (dev->dev_state != MEI_DEV_ENABLED) {
225
		dev_dbg(dev->dev, "wd: dev_state != MEI_DEV_ENABLED  dev_state = %s\n",
226
			mei_dev_state_str(dev->dev_state));
O
Oren Weil 已提交
227 228 229 230
		goto end_unlock;
	}

	if (dev->wd_cl.state != MEI_FILE_CONNECTED)	{
231
		dev_dbg(dev->dev, "MEI Driver is not connected to Watchdog Client\n");
O
Oren Weil 已提交
232 233 234
		goto end_unlock;
	}

235
	mei_wd_set_start_timeout(dev, dev->wd_timeout);
O
Oren Weil 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

	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;

254
	dev = watchdog_get_drvdata(wd_dev);
O
Oren Weil 已提交
255 256 257 258
	if (!dev)
		return -ENODEV;

	mutex_lock(&dev->device_lock);
259
	mei_wd_stop(dev);
O
Oren Weil 已提交
260 261 262 263 264
	mutex_unlock(&dev->device_lock);

	return 0;
}

O
Oren Weil 已提交
265 266 267 268 269 270 271 272 273 274
/*
 * 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)
{
	struct mei_device *dev;
275
	int ret;
O
Oren Weil 已提交
276

277
	dev = watchdog_get_drvdata(wd_dev);
O
Oren Weil 已提交
278 279 280 281 282 283
	if (!dev)
		return -ENODEV;

	mutex_lock(&dev->device_lock);

	if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
284
		dev_err(dev->dev, "wd: not connected.\n");
O
Oren Weil 已提交
285 286 287 288
		ret = -ENODEV;
		goto end;
	}

289 290
	dev->wd_state = MEI_WD_RUNNING;

291 292 293
	ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
	if (ret < 0)
		goto end;
O
Oren Weil 已提交
294
	/* Check if we can send the ping to HW*/
295
	if (ret && mei_hbuf_acquire(dev)) {
O
Oren Weil 已提交
296

297
		dev_dbg(dev->dev, "wd: sending ping\n");
O
Oren Weil 已提交
298

299 300
		ret = mei_wd_send(dev);
		if (ret)
O
Oren Weil 已提交
301
			goto end;
302
		dev->wd_pending = false;
O
Oren Weil 已提交
303 304 305 306 307 308 309 310 311
	} else {
		dev->wd_pending = true;
	}

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

312 313 314 315 316 317 318 319
/*
 * 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
 */
320 321
static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
		unsigned int timeout)
322 323 324
{
	struct mei_device *dev;

325
	dev = watchdog_get_drvdata(wd_dev);
326 327 328 329
	if (!dev)
		return -ENODEV;

	/* Check Timeout value */
330
	if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
331 332 333 334 335
		return -EINVAL;

	mutex_lock(&dev->device_lock);

	dev->wd_timeout = timeout;
336
	wd_dev->timeout = timeout;
337 338 339 340 341 342 343
	mei_wd_set_start_timeout(dev, dev->wd_timeout);

	mutex_unlock(&dev->device_lock);

	return 0;
}

O
Oren Weil 已提交
344 345 346
/*
 * Watchdog Device structs
 */
347
static const struct watchdog_ops wd_ops = {
O
Oren Weil 已提交
348 349 350
		.owner = THIS_MODULE,
		.start = mei_wd_ops_start,
		.stop = mei_wd_ops_stop,
O
Oren Weil 已提交
351
		.ping = mei_wd_ops_ping,
352
		.set_timeout = mei_wd_ops_set_timeout,
O
Oren Weil 已提交
353
};
354
static const struct watchdog_info wd_info = {
O
Oren Weil 已提交
355
		.identity = INTEL_AMT_WATCHDOG_ID,
356 357 358
		.options = WDIOF_KEEPALIVEPING |
			   WDIOF_SETTIMEOUT |
			   WDIOF_ALARMONLY,
O
Oren Weil 已提交
359 360
};

361
static struct watchdog_device amt_wd_dev = {
O
Oren Weil 已提交
362 363
		.info = &wd_info,
		.ops = &wd_ops,
364 365 366
		.timeout = MEI_WD_DEFAULT_TIMEOUT,
		.min_timeout = MEI_WD_MIN_TIMEOUT,
		.max_timeout = MEI_WD_MAX_TIMEOUT,
O
Oren Weil 已提交
367 368 369
};


370
int mei_watchdog_register(struct mei_device *dev)
371
{
372 373 374 375 376 377 378 379

	int ret;

	/* unlock to perserve correct locking order */
	mutex_unlock(&dev->device_lock);
	ret = watchdog_register_device(&amt_wd_dev);
	mutex_lock(&dev->device_lock);
	if (ret) {
380
		dev_err(dev->dev, "wd: unable to register watchdog device = %d.\n",
381 382
			ret);
		return ret;
383
	}
384

385
	dev_dbg(dev->dev, "wd: successfully register watchdog interface.\n");
386
	watchdog_set_drvdata(&amt_wd_dev, dev);
387
	return 0;
388 389 390 391
}

void mei_watchdog_unregister(struct mei_device *dev)
{
392
	if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
393 394 395 396
		return;

	watchdog_set_drvdata(&amt_wd_dev, NULL);
	watchdog_unregister_device(&amt_wd_dev);
397 398
}