1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2011, Intel Corporation.
5  *
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>
22 #include <linux/watchdog.h>
23 
24 #include "mei_dev.h"
25 #include "hw.h"
26 #include "interface.h"
27 #include "mei.h"
28 
29 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
30 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
31 
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}
36 };
37 
38 /*
39  * AMT Watchdog Device
40  */
41 #define INTEL_AMT_WATCHDOG_ID "INTCAMT"
42 
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,
46 						0x32, 0xAB);
47 
mei_wd_set_start_timeout(struct mei_device * dev,u16 timeout)48 void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
49 {
50 	dev_dbg(&dev->pdev->dev, "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,
53 			&timeout, sizeof(u16));
54 }
55 
56 /**
57  * host_init_wd - mei initialization wd.
58  *
59  * @dev: the device structure
60  */
mei_wd_host_init(struct mei_device * dev)61 bool mei_wd_host_init(struct mei_device *dev)
62 {
63 	bool ret = false;
64 
65 	mei_cl_init(&dev->wd_cl, dev);
66 
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;
70 
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);
74 
75 	dev_dbg(&dev->pdev->dev, "check wd_cl\n");
76 	if (MEI_FILE_CONNECTING == dev->wd_cl.state) {
77 		if (!mei_connect(dev, &dev->wd_cl)) {
78 			dev_dbg(&dev->pdev->dev, "Failed to connect to WD client\n");
79 			dev->wd_cl.state = MEI_FILE_DISCONNECTED;
80 			dev->wd_cl.host_client_id = 0;
81 			ret = false;
82 			goto end;
83 		} else {
84 			dev->wd_cl.timer_count = CONNECT_TIMEOUT;
85 		}
86 	} else {
87 		dev_dbg(&dev->pdev->dev, "Failed to find WD client\n");
88 		ret = false;
89 		goto end;
90 	}
91 
92 end:
93 	return ret;
94 }
95 
96 /**
97  * mei_wd_send - sends watch dog message to fw.
98  *
99  * @dev: the device structure
100  *
101  * returns 0 if success,
102  *	-EIO when message send fails
103  *	-EINVAL when invalid message is to be sent
104  */
mei_wd_send(struct mei_device * dev)105 int mei_wd_send(struct mei_device *dev)
106 {
107 	struct mei_msg_hdr *mei_hdr;
108 
109 	mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
110 	mei_hdr->host_addr = dev->wd_cl.host_client_id;
111 	mei_hdr->me_addr = dev->wd_cl.me_client_id;
112 	mei_hdr->msg_complete = 1;
113 	mei_hdr->reserved = 0;
114 
115 	if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE))
116 		mei_hdr->length = MEI_START_WD_DATA_SIZE;
117 	else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE))
118 		mei_hdr->length = MEI_WD_PARAMS_SIZE;
119 	else
120 		return -EINVAL;
121 
122 	if (mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length))
123 		return 0;
124 	return -EIO;
125 }
126 
127 /**
128  * mei_wd_stop - sends watchdog stop message to fw.
129  *
130  * @dev: the device structure
131  * @preserve: indicate if to keep the timeout value
132  *
133  * returns 0 if success,
134  *	-EIO when message send fails
135  *	-EINVAL when invalid message is to be sent
136  */
mei_wd_stop(struct mei_device * dev,bool preserve)137 int mei_wd_stop(struct mei_device *dev, bool preserve)
138 {
139 	int ret;
140 	u16 wd_timeout = dev->wd_timeout;
141 
142 	cancel_delayed_work(&dev->timer_work);
143 	if (dev->wd_cl.state != MEI_FILE_CONNECTED || !dev->wd_timeout)
144 		return 0;
145 
146 	dev->wd_timeout = 0;
147 	dev->wd_due_counter = 0;
148 	memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE);
149 	dev->stop = true;
150 
151 	ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
152 	if (ret < 0)
153 		goto out;
154 
155 	if (ret && dev->mei_host_buffer_is_empty) {
156 		ret = 0;
157 		dev->mei_host_buffer_is_empty = false;
158 
159 		if (!mei_wd_send(dev)) {
160 			ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
161 			if (ret)
162 				goto out;
163 		} else {
164 			dev_dbg(&dev->pdev->dev, "send stop WD failed\n");
165 		}
166 
167 		dev->wd_pending = false;
168 	} else {
169 		dev->wd_pending = true;
170 	}
171 	dev->wd_stopped = false;
172 	mutex_unlock(&dev->device_lock);
173 
174 	ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
175 					dev->wd_stopped, 10 * HZ);
176 	mutex_lock(&dev->device_lock);
177 	if (dev->wd_stopped) {
178 		dev_dbg(&dev->pdev->dev, "stop wd complete ret=%d.\n", ret);
179 		ret = 0;
180 	} else {
181 		if (!ret)
182 			ret = -ETIMEDOUT;
183 		dev_warn(&dev->pdev->dev,
184 			"stop wd failed to complete ret=%d.\n", ret);
185 	}
186 
187 	if (preserve)
188 		dev->wd_timeout = wd_timeout;
189 
190 out:
191 	return ret;
192 }
193 
194 /*
195  * mei_wd_ops_start - wd start command from the watchdog core.
196  *
197  * @wd_dev - watchdog device struct
198  *
199  * returns 0 if success, negative errno code for failure
200  */
mei_wd_ops_start(struct watchdog_device * wd_dev)201 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
202 {
203 	int err = -ENODEV;
204 	struct mei_device *dev;
205 
206 	dev = pci_get_drvdata(mei_device);
207 	if (!dev)
208 		return -ENODEV;
209 
210 	mutex_lock(&dev->device_lock);
211 
212 	if (dev->mei_state != MEI_ENABLED) {
213 		dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED  mei_state= %d\n",
214 		    dev->mei_state);
215 		goto end_unlock;
216 	}
217 
218 	if (dev->wd_cl.state != MEI_FILE_CONNECTED)	{
219 		dev_dbg(&dev->pdev->dev, "MEI Driver is not connected to Watchdog Client\n");
220 		goto end_unlock;
221 	}
222 
223 	mei_wd_set_start_timeout(dev, dev->wd_timeout);
224 
225 	err = 0;
226 end_unlock:
227 	mutex_unlock(&dev->device_lock);
228 	return err;
229 }
230 
231 /*
232  * mei_wd_ops_stop -  wd stop command from the watchdog core.
233  *
234  * @wd_dev - watchdog device struct
235  *
236  * returns 0 if success, negative errno code for failure
237  */
mei_wd_ops_stop(struct watchdog_device * wd_dev)238 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
239 {
240 	struct mei_device *dev;
241 	dev = pci_get_drvdata(mei_device);
242 
243 	if (!dev)
244 		return -ENODEV;
245 
246 	mutex_lock(&dev->device_lock);
247 	mei_wd_stop(dev, false);
248 	mutex_unlock(&dev->device_lock);
249 
250 	return 0;
251 }
252 
253 /*
254  * mei_wd_ops_ping - wd ping command from the watchdog core.
255  *
256  * @wd_dev - watchdog device struct
257  *
258  * returns 0 if success, negative errno code for failure
259  */
mei_wd_ops_ping(struct watchdog_device * wd_dev)260 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
261 {
262 	int ret = 0;
263 	struct mei_device *dev;
264 	dev = pci_get_drvdata(mei_device);
265 
266 	if (!dev)
267 		return -ENODEV;
268 
269 	mutex_lock(&dev->device_lock);
270 
271 	if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
272 		dev_dbg(&dev->pdev->dev, "wd is not connected.\n");
273 		ret = -ENODEV;
274 		goto end;
275 	}
276 
277 	/* Check if we can send the ping to HW*/
278 	if (dev->mei_host_buffer_is_empty &&
279 		mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
280 
281 		dev->mei_host_buffer_is_empty = false;
282 		dev_dbg(&dev->pdev->dev, "sending watchdog ping\n");
283 
284 		if (mei_wd_send(dev)) {
285 			dev_dbg(&dev->pdev->dev, "wd send failed.\n");
286 			ret = -EIO;
287 			goto end;
288 		}
289 
290 		if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
291 			dev_dbg(&dev->pdev->dev, "mei_flow_ctrl_reduce() failed.\n");
292 			ret = -EIO;
293 			goto end;
294 		}
295 
296 	} else {
297 		dev->wd_pending = true;
298 	}
299 
300 end:
301 	mutex_unlock(&dev->device_lock);
302 	return ret;
303 }
304 
305 /*
306  * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
307  *
308  * @wd_dev - watchdog device struct
309  * @timeout - timeout value to set
310  *
311  * returns 0 if success, negative errno code for failure
312  */
mei_wd_ops_set_timeout(struct watchdog_device * wd_dev,unsigned int timeout)313 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, unsigned int timeout)
314 {
315 	struct mei_device *dev;
316 	dev = pci_get_drvdata(mei_device);
317 
318 	if (!dev)
319 		return -ENODEV;
320 
321 	/* Check Timeout value */
322 	if (timeout < AMT_WD_MIN_TIMEOUT || timeout > AMT_WD_MAX_TIMEOUT)
323 		return -EINVAL;
324 
325 	mutex_lock(&dev->device_lock);
326 
327 	dev->wd_timeout = timeout;
328 	mei_wd_set_start_timeout(dev, dev->wd_timeout);
329 
330 	mutex_unlock(&dev->device_lock);
331 
332 	return 0;
333 }
334 
335 /*
336  * Watchdog Device structs
337  */
338 static const struct watchdog_ops wd_ops = {
339 		.owner = THIS_MODULE,
340 		.start = mei_wd_ops_start,
341 		.stop = mei_wd_ops_stop,
342 		.ping = mei_wd_ops_ping,
343 		.set_timeout = mei_wd_ops_set_timeout,
344 };
345 static const struct watchdog_info wd_info = {
346 		.identity = INTEL_AMT_WATCHDOG_ID,
347 		.options = WDIOF_KEEPALIVEPING,
348 };
349 
350 struct watchdog_device amt_wd_dev = {
351 		.info = &wd_info,
352 		.ops = &wd_ops,
353 		.timeout = AMT_WD_DEFAULT_TIMEOUT,
354 		.min_timeout = AMT_WD_MIN_TIMEOUT,
355 		.max_timeout = AMT_WD_MAX_TIMEOUT,
356 };
357 
358 
mei_watchdog_register(struct mei_device * dev)359 void  mei_watchdog_register(struct mei_device *dev)
360 {
361 	dev_dbg(&dev->pdev->dev, "dev->wd_timeout =%d.\n", dev->wd_timeout);
362 
363 	dev->wd_due_counter = !!dev->wd_timeout;
364 
365 	if (watchdog_register_device(&amt_wd_dev)) {
366 		dev_err(&dev->pdev->dev, "unable to register watchdog device.\n");
367 		dev->wd_interface_reg = false;
368 	} else {
369 		dev_dbg(&dev->pdev->dev, "successfully register watchdog interface.\n");
370 		dev->wd_interface_reg = true;
371 	}
372 }
373 
mei_watchdog_unregister(struct mei_device * dev)374 void mei_watchdog_unregister(struct mei_device *dev)
375 {
376 	if (dev->wd_interface_reg)
377 		watchdog_unregister_device(&amt_wd_dev);
378 	dev->wd_interface_reg = false;
379 }
380 
381