Linux-libre 3.17.4-gnu
[librecmc/linux-libre.git] / drivers / misc / mei / wd.c
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 <linux/mei.h>
25
26 #include "mei_dev.h"
27 #include "hbm.h"
28 #include "client.h"
29
30 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
31 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
32
33 /*
34  * AMT Watchdog Device
35  */
36 #define INTEL_AMT_WATCHDOG_ID "INTCAMT"
37
38 /* UUIDs for AMT F/W clients */
39 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
40                                                 0x9D, 0xA9, 0x15, 0x14, 0xCB,
41                                                 0x32, 0xAB);
42
43 static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
44 {
45         dev_dbg(&dev->pdev->dev, "wd: set timeout=%d.\n", timeout);
46         memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
47         memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
48 }
49
50 /**
51  * mei_wd_host_init - connect to the watchdog client
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
58  */
59 int mei_wd_host_init(struct mei_device *dev)
60 {
61         struct mei_cl *cl = &dev->wd_cl;
62         int id;
63         int ret;
64
65         mei_cl_init(cl, dev);
66
67         dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
68         dev->wd_state = MEI_WD_IDLE;
69
70
71         /* check for valid client id */
72         id = mei_me_cl_by_uuid(dev, &mei_wd_guid);
73         if (id < 0) {
74                 dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
75                 return -ENOTTY;
76         }
77
78         cl->me_client_id = dev->me_clients[id].client_id;
79
80         ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
81
82         if (ret < 0) {
83                 dev_info(&dev->pdev->dev, "wd: failed link client\n");
84                 return ret;
85         }
86
87         ret = mei_cl_connect(cl, NULL);
88
89         if (ret) {
90                 dev_err(&dev->pdev->dev, "wd: failed to connect = %d\n", ret);
91                 mei_cl_unlink(cl);
92                 return ret;
93         }
94
95         ret = mei_watchdog_register(dev);
96         if (ret) {
97                 mei_cl_disconnect(cl);
98                 mei_cl_unlink(cl);
99         }
100         return ret;
101 }
102
103 /**
104  * mei_wd_send - sends watch dog message to fw.
105  *
106  * @dev: the device structure
107  *
108  * returns 0 if success,
109  *      -EIO when message send fails
110  *      -EINVAL when invalid message is to be sent
111  *      -ENODEV on flow control failure
112  */
113 int mei_wd_send(struct mei_device *dev)
114 {
115         struct mei_cl *cl = &dev->wd_cl;
116         struct mei_msg_hdr hdr;
117         int ret;
118
119         hdr.host_addr = cl->host_client_id;
120         hdr.me_addr = cl->me_client_id;
121         hdr.msg_complete = 1;
122         hdr.reserved = 0;
123         hdr.internal = 0;
124
125         if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
126                 hdr.length = MEI_WD_START_MSG_SIZE;
127         else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
128                 hdr.length = MEI_WD_STOP_MSG_SIZE;
129         else {
130                 dev_err(&dev->pdev->dev, "wd: invalid message is to be sent, aborting\n");
131                 return -EINVAL;
132         }
133
134         ret = mei_write_message(dev, &hdr, dev->wd_data);
135         if (ret) {
136                 dev_err(&dev->pdev->dev, "wd: write message failed\n");
137                 return ret;
138         }
139
140         ret = mei_cl_flow_ctrl_reduce(cl);
141         if (ret) {
142                 dev_err(&dev->pdev->dev, "wd: flow_ctrl_reduce failed.\n");
143                 return ret;
144         }
145
146         return 0;
147 }
148
149 /**
150  * mei_wd_stop - sends watchdog stop message to fw.
151  *
152  * @dev: the device structure
153  * @preserve: indicate if to keep the timeout value
154  *
155  * returns 0 if success
156  * on error:
157  *      -EIO    when message send fails
158  *      -EINVAL when invalid message is to be sent
159  *      -ETIME  on message timeout
160  */
161 int mei_wd_stop(struct mei_device *dev)
162 {
163         int ret;
164
165         if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
166             dev->wd_state != MEI_WD_RUNNING)
167                 return 0;
168
169         memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
170
171         dev->wd_state = MEI_WD_STOPPING;
172
173         ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
174         if (ret < 0)
175                 goto err;
176
177         if (ret && mei_hbuf_acquire(dev)) {
178                 ret = mei_wd_send(dev);
179                 if (ret)
180                         goto err;
181                 dev->wd_pending = false;
182         } else {
183                 dev->wd_pending = true;
184         }
185
186         mutex_unlock(&dev->device_lock);
187
188         ret = wait_event_timeout(dev->wait_stop_wd,
189                                 dev->wd_state == MEI_WD_IDLE,
190                                 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
191         mutex_lock(&dev->device_lock);
192         if (dev->wd_state != MEI_WD_IDLE) {
193                 /* timeout */
194                 ret = -ETIME;
195                 dev_warn(&dev->pdev->dev,
196                         "wd: stop failed to complete ret=%d.\n", ret);
197                 goto err;
198         }
199         dev_dbg(&dev->pdev->dev, "wd: stop completed after %u msec\n",
200                         MEI_WD_STOP_TIMEOUT - jiffies_to_msecs(ret));
201         return 0;
202 err:
203         return ret;
204 }
205
206 /*
207  * mei_wd_ops_start - wd start command from the watchdog core.
208  *
209  * @wd_dev - watchdog device struct
210  *
211  * returns 0 if success, negative errno code for failure
212  */
213 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
214 {
215         int err = -ENODEV;
216         struct mei_device *dev;
217
218         dev = watchdog_get_drvdata(wd_dev);
219         if (!dev)
220                 return -ENODEV;
221
222         mutex_lock(&dev->device_lock);
223
224         if (dev->dev_state != MEI_DEV_ENABLED) {
225                 dev_dbg(&dev->pdev->dev,
226                         "wd: dev_state != MEI_DEV_ENABLED  dev_state = %s\n",
227                         mei_dev_state_str(dev->dev_state));
228                 goto end_unlock;
229         }
230
231         if (dev->wd_cl.state != MEI_FILE_CONNECTED)     {
232                 dev_dbg(&dev->pdev->dev,
233                         "MEI Driver is not connected to Watchdog Client\n");
234                 goto end_unlock;
235         }
236
237         mei_wd_set_start_timeout(dev, dev->wd_timeout);
238
239         err = 0;
240 end_unlock:
241         mutex_unlock(&dev->device_lock);
242         return err;
243 }
244
245 /*
246  * mei_wd_ops_stop -  wd stop command from the watchdog core.
247  *
248  * @wd_dev - watchdog device struct
249  *
250  * returns 0 if success, negative errno code for failure
251  */
252 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
253 {
254         struct mei_device *dev;
255
256         dev = watchdog_get_drvdata(wd_dev);
257         if (!dev)
258                 return -ENODEV;
259
260         mutex_lock(&dev->device_lock);
261         mei_wd_stop(dev);
262         mutex_unlock(&dev->device_lock);
263
264         return 0;
265 }
266
267 /*
268  * mei_wd_ops_ping - wd ping command from the watchdog core.
269  *
270  * @wd_dev - watchdog device struct
271  *
272  * returns 0 if success, negative errno code for failure
273  */
274 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
275 {
276         struct mei_device *dev;
277         int ret;
278
279         dev = watchdog_get_drvdata(wd_dev);
280         if (!dev)
281                 return -ENODEV;
282
283         mutex_lock(&dev->device_lock);
284
285         if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
286                 dev_err(&dev->pdev->dev, "wd: not connected.\n");
287                 ret = -ENODEV;
288                 goto end;
289         }
290
291         dev->wd_state = MEI_WD_RUNNING;
292
293         ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
294         if (ret < 0)
295                 goto end;
296         /* Check if we can send the ping to HW*/
297         if (ret && mei_hbuf_acquire(dev)) {
298
299                 dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
300
301                 ret = mei_wd_send(dev);
302                 if (ret)
303                         goto end;
304                 dev->wd_pending = false;
305         } else {
306                 dev->wd_pending = true;
307         }
308
309 end:
310         mutex_unlock(&dev->device_lock);
311         return ret;
312 }
313
314 /*
315  * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
316  *
317  * @wd_dev - watchdog device struct
318  * @timeout - timeout value to set
319  *
320  * returns 0 if success, negative errno code for failure
321  */
322 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
323                 unsigned int timeout)
324 {
325         struct mei_device *dev;
326
327         dev = watchdog_get_drvdata(wd_dev);
328         if (!dev)
329                 return -ENODEV;
330
331         /* Check Timeout value */
332         if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
333                 return -EINVAL;
334
335         mutex_lock(&dev->device_lock);
336
337         dev->wd_timeout = timeout;
338         wd_dev->timeout = timeout;
339         mei_wd_set_start_timeout(dev, dev->wd_timeout);
340
341         mutex_unlock(&dev->device_lock);
342
343         return 0;
344 }
345
346 /*
347  * Watchdog Device structs
348  */
349 static const struct watchdog_ops wd_ops = {
350                 .owner = THIS_MODULE,
351                 .start = mei_wd_ops_start,
352                 .stop = mei_wd_ops_stop,
353                 .ping = mei_wd_ops_ping,
354                 .set_timeout = mei_wd_ops_set_timeout,
355 };
356 static const struct watchdog_info wd_info = {
357                 .identity = INTEL_AMT_WATCHDOG_ID,
358                 .options = WDIOF_KEEPALIVEPING |
359                            WDIOF_SETTIMEOUT |
360                            WDIOF_ALARMONLY,
361 };
362
363 static struct watchdog_device amt_wd_dev = {
364                 .info = &wd_info,
365                 .ops = &wd_ops,
366                 .timeout = MEI_WD_DEFAULT_TIMEOUT,
367                 .min_timeout = MEI_WD_MIN_TIMEOUT,
368                 .max_timeout = MEI_WD_MAX_TIMEOUT,
369 };
370
371
372 int mei_watchdog_register(struct mei_device *dev)
373 {
374
375         int ret;
376
377         /* unlock to perserve correct locking order */
378         mutex_unlock(&dev->device_lock);
379         ret = watchdog_register_device(&amt_wd_dev);
380         mutex_lock(&dev->device_lock);
381         if (ret) {
382                 dev_err(&dev->pdev->dev, "wd: unable to register watchdog device = %d.\n",
383                         ret);
384                 return ret;
385         }
386
387         dev_dbg(&dev->pdev->dev,
388                 "wd: successfully register watchdog interface.\n");
389         watchdog_set_drvdata(&amt_wd_dev, dev);
390         return 0;
391 }
392
393 void mei_watchdog_unregister(struct mei_device *dev)
394 {
395         if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
396                 return;
397
398         watchdog_set_drvdata(&amt_wd_dev, NULL);
399         watchdog_unregister_device(&amt_wd_dev);
400 }
401