1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2012, 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 	return mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length);
123 }
124 
125 /**
126  * mei_wd_stop - sends watchdog stop message to fw.
127  *
128  * @dev: the device structure
129  * @preserve: indicate if to keep the timeout value
130  *
131  * returns 0 if success,
132  *	-EIO when message send fails
133  *	-EINVAL when invalid message is to be sent
134  */
mei_wd_stop(struct mei_device * dev,bool preserve)135 int mei_wd_stop(struct mei_device *dev, bool preserve)
136 {
137 	int ret;
138 	u16 wd_timeout = dev->wd_timeout;
139 
140 	cancel_delayed_work(&dev->timer_work);
141 	if (dev->wd_cl.state != MEI_FILE_CONNECTED || !dev->wd_timeout)
142 		return 0;
143 
144 	dev->wd_timeout = 0;
145 	dev->wd_due_counter = 0;
146 	memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE);
147 	dev->stop = true;
148 
149 	ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
150 	if (ret < 0)
151 		goto out;
152 
153 	if (ret && dev->mei_host_buffer_is_empty) {
154 		ret = 0;
155 		dev->mei_host_buffer_is_empty = false;
156 
157 		if (!mei_wd_send(dev)) {
158 			ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
159 			if (ret)
160 				goto out;
161 		} else {
162 			dev_dbg(&dev->pdev->dev, "send stop WD failed\n");
163 		}
164 
165 		dev->wd_pending = false;
166 	} else {
167 		dev->wd_pending = true;
168 	}
169 	dev->wd_stopped = false;
170 	mutex_unlock(&dev->device_lock);
171 
172 	ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
173 					dev->wd_stopped, 10 * HZ);
174 	mutex_lock(&dev->device_lock);
175 	if (dev->wd_stopped) {
176 		dev_dbg(&dev->pdev->dev, "stop wd complete ret=%d.\n", ret);
177 		ret = 0;
178 	} else {
179 		if (!ret)
180 			ret = -ETIMEDOUT;
181 		dev_warn(&dev->pdev->dev,
182 			"stop wd failed to complete ret=%d.\n", ret);
183 	}
184 
185 	if (preserve)
186 		dev->wd_timeout = wd_timeout;
187 
188 out:
189 	return ret;
190 }
191 
192 /*
193  * mei_wd_ops_start - wd start command from the watchdog core.
194  *
195  * @wd_dev - watchdog device struct
196  *
197  * returns 0 if success, negative errno code for failure
198  */
mei_wd_ops_start(struct watchdog_device * wd_dev)199 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
200 {
201 	int err = -ENODEV;
202 	struct mei_device *dev;
203 
204 	dev = pci_get_drvdata(mei_device);
205 	if (!dev)
206 		return -ENODEV;
207 
208 	mutex_lock(&dev->device_lock);
209 
210 	if (dev->mei_state != MEI_ENABLED) {
211 		dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED  mei_state= %d\n",
212 		    dev->mei_state);
213 		goto end_unlock;
214 	}
215 
216 	if (dev->wd_cl.state != MEI_FILE_CONNECTED)	{
217 		dev_dbg(&dev->pdev->dev, "MEI Driver is not connected to Watchdog Client\n");
218 		goto end_unlock;
219 	}
220 
221 	mei_wd_set_start_timeout(dev, dev->wd_timeout);
222 
223 	err = 0;
224 end_unlock:
225 	mutex_unlock(&dev->device_lock);
226 	return err;
227 }
228 
229 /*
230  * mei_wd_ops_stop -  wd stop command from the watchdog core.
231  *
232  * @wd_dev - watchdog device struct
233  *
234  * returns 0 if success, negative errno code for failure
235  */
mei_wd_ops_stop(struct watchdog_device * wd_dev)236 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
237 {
238 	struct mei_device *dev;
239 	dev = pci_get_drvdata(mei_device);
240 
241 	if (!dev)
242 		return -ENODEV;
243 
244 	mutex_lock(&dev->device_lock);
245 	mei_wd_stop(dev, false);
246 	mutex_unlock(&dev->device_lock);
247 
248 	return 0;
249 }
250 
251 /*
252  * mei_wd_ops_ping - wd ping command from the watchdog core.
253  *
254  * @wd_dev - watchdog device struct
255  *
256  * returns 0 if success, negative errno code for failure
257  */
mei_wd_ops_ping(struct watchdog_device * wd_dev)258 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
259 {
260 	int ret = 0;
261 	struct mei_device *dev;
262 	dev = pci_get_drvdata(mei_device);
263 
264 	if (!dev)
265 		return -ENODEV;
266 
267 	mutex_lock(&dev->device_lock);
268 
269 	if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
270 		dev_dbg(&dev->pdev->dev, "wd is not connected.\n");
271 		ret = -ENODEV;
272 		goto end;
273 	}
274 
275 	/* Check if we can send the ping to HW*/
276 	if (dev->mei_host_buffer_is_empty &&
277 		mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
278 
279 		dev->mei_host_buffer_is_empty = false;
280 		dev_dbg(&dev->pdev->dev, "sending watchdog ping\n");
281 
282 		if (mei_wd_send(dev)) {
283 			dev_dbg(&dev->pdev->dev, "wd send failed.\n");
284 			ret = -EIO;
285 			goto end;
286 		}
287 
288 		if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
289 			dev_dbg(&dev->pdev->dev, "mei_flow_ctrl_reduce() failed.\n");
290 			ret = -EIO;
291 			goto end;
292 		}
293 
294 	} else {
295 		dev->wd_pending = true;
296 	}
297 
298 end:
299 	mutex_unlock(&dev->device_lock);
300 	return ret;
301 }
302 
303 /*
304  * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
305  *
306  * @wd_dev - watchdog device struct
307  * @timeout - timeout value to set
308  *
309  * returns 0 if success, negative errno code for failure
310  */
mei_wd_ops_set_timeout(struct watchdog_device * wd_dev,unsigned int timeout)311 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, unsigned int timeout)
312 {
313 	struct mei_device *dev;
314 	dev = pci_get_drvdata(mei_device);
315 
316 	if (!dev)
317 		return -ENODEV;
318 
319 	/* Check Timeout value */
320 	if (timeout < AMT_WD_MIN_TIMEOUT || timeout > AMT_WD_MAX_TIMEOUT)
321 		return -EINVAL;
322 
323 	mutex_lock(&dev->device_lock);
324 
325 	dev->wd_timeout = timeout;
326 	wd_dev->timeout = timeout;
327 	mei_wd_set_start_timeout(dev, dev->wd_timeout);
328 
329 	mutex_unlock(&dev->device_lock);
330 
331 	return 0;
332 }
333 
334 /*
335  * Watchdog Device structs
336  */
337 static const struct watchdog_ops wd_ops = {
338 		.owner = THIS_MODULE,
339 		.start = mei_wd_ops_start,
340 		.stop = mei_wd_ops_stop,
341 		.ping = mei_wd_ops_ping,
342 		.set_timeout = mei_wd_ops_set_timeout,
343 };
344 static const struct watchdog_info wd_info = {
345 		.identity = INTEL_AMT_WATCHDOG_ID,
346 		.options = WDIOF_KEEPALIVEPING,
347 };
348 
349 struct watchdog_device amt_wd_dev = {
350 		.info = &wd_info,
351 		.ops = &wd_ops,
352 		.timeout = AMT_WD_DEFAULT_TIMEOUT,
353 		.min_timeout = AMT_WD_MIN_TIMEOUT,
354 		.max_timeout = AMT_WD_MAX_TIMEOUT,
355 };
356 
357 
mei_watchdog_register(struct mei_device * dev)358 void  mei_watchdog_register(struct mei_device *dev)
359 {
360 	dev_dbg(&dev->pdev->dev, "dev->wd_timeout =%d.\n", dev->wd_timeout);
361 
362 	dev->wd_due_counter = !!dev->wd_timeout;
363 
364 	if (watchdog_register_device(&amt_wd_dev)) {
365 		dev_err(&dev->pdev->dev, "unable to register watchdog device.\n");
366 		dev->wd_interface_reg = false;
367 	} else {
368 		dev_dbg(&dev->pdev->dev, "successfully register watchdog interface.\n");
369 		dev->wd_interface_reg = true;
370 	}
371 }
372 
mei_watchdog_unregister(struct mei_device * dev)373 void mei_watchdog_unregister(struct mei_device *dev)
374 {
375 	if (dev->wd_interface_reg)
376 		watchdog_unregister_device(&amt_wd_dev);
377 	dev->wd_interface_reg = false;
378 }
379 
380