d28511b78eaa596b4591befcee7a0ac587e1ca5d
[firefly-linux-kernel-4.4.55.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->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         struct mei_me_client *me_cl;
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         me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
73         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         ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
82
83         if (ret < 0) {
84                 dev_info(dev->dev, "wd: failed link client\n");
85                 return ret;
86         }
87
88         ret = mei_cl_connect(cl, NULL);
89
90         if (ret) {
91                 dev_err(dev->dev, "wd: failed to connect = %d\n", ret);
92                 mei_cl_unlink(cl);
93                 return ret;
94         }
95
96         ret = mei_watchdog_register(dev);
97         if (ret) {
98                 mei_cl_disconnect(cl);
99                 mei_cl_unlink(cl);
100         }
101         return ret;
102 }
103
104 /**
105  * mei_wd_send - sends watch dog message to fw.
106  *
107  * @dev: the device structure
108  *
109  * returns 0 if success,
110  *      -EIO when message send fails
111  *      -EINVAL when invalid message is to be sent
112  *      -ENODEV on flow control failure
113  */
114 int mei_wd_send(struct mei_device *dev)
115 {
116         struct mei_cl *cl = &dev->wd_cl;
117         struct mei_msg_hdr hdr;
118         int ret;
119
120         hdr.host_addr = cl->host_client_id;
121         hdr.me_addr = cl->me_client_id;
122         hdr.msg_complete = 1;
123         hdr.reserved = 0;
124         hdr.internal = 0;
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");
132                 return -EINVAL;
133         }
134
135         ret = mei_write_message(dev, &hdr, dev->wd_data);
136         if (ret) {
137                 dev_err(dev->dev, "wd: write message failed\n");
138                 return ret;
139         }
140
141         ret = mei_cl_flow_ctrl_reduce(cl);
142         if (ret) {
143                 dev_err(dev->dev, "wd: flow_ctrl_reduce failed.\n");
144                 return ret;
145         }
146
147         return 0;
148 }
149
150 /**
151  * mei_wd_stop - sends watchdog stop message to fw.
152  *
153  * @dev: the device structure
154  * @preserve: indicate if to keep the timeout value
155  *
156  * returns 0 if success
157  * on error:
158  *      -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)
163 {
164         int ret;
165
166         if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
167             dev->wd_state != MEI_WD_RUNNING)
168                 return 0;
169
170         memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
171
172         dev->wd_state = MEI_WD_STOPPING;
173
174         ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
175         if (ret < 0)
176                 goto err;
177
178         if (ret && mei_hbuf_acquire(dev)) {
179                 ret = mei_wd_send(dev);
180                 if (ret)
181                         goto err;
182                 dev->wd_pending = false;
183         } else {
184                 dev->wd_pending = true;
185         }
186
187         mutex_unlock(&dev->device_lock);
188
189         ret = wait_event_timeout(dev->wait_stop_wd,
190                                 dev->wd_state == MEI_WD_IDLE,
191                                 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
192         mutex_lock(&dev->device_lock);
193         if (dev->wd_state != MEI_WD_IDLE) {
194                 /* timeout */
195                 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                         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->dev, "wd: dev_state != MEI_DEV_ENABLED  dev_state = %s\n",
226                         mei_dev_state_str(dev->dev_state));
227                 goto end_unlock;
228         }
229
230         if (dev->wd_cl.state != MEI_FILE_CONNECTED)     {
231                 dev_dbg(dev->dev, "MEI Driver is not connected to Watchdog Client\n");
232                 goto end_unlock;
233         }
234
235         mei_wd_set_start_timeout(dev, dev->wd_timeout);
236
237         err = 0;
238 end_unlock:
239         mutex_unlock(&dev->device_lock);
240         return err;
241 }
242
243 /*
244  * mei_wd_ops_stop -  wd stop command from the watchdog core.
245  *
246  * @wd_dev - watchdog device struct
247  *
248  * returns 0 if success, negative errno code for failure
249  */
250 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
251 {
252         struct mei_device *dev;
253
254         dev = watchdog_get_drvdata(wd_dev);
255         if (!dev)
256                 return -ENODEV;
257
258         mutex_lock(&dev->device_lock);
259         mei_wd_stop(dev);
260         mutex_unlock(&dev->device_lock);
261
262         return 0;
263 }
264
265 /*
266  * mei_wd_ops_ping - wd ping command from the watchdog core.
267  *
268  * @wd_dev - watchdog device struct
269  *
270  * returns 0 if success, negative errno code for failure
271  */
272 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
273 {
274         struct mei_device *dev;
275         int ret;
276
277         dev = watchdog_get_drvdata(wd_dev);
278         if (!dev)
279                 return -ENODEV;
280
281         mutex_lock(&dev->device_lock);
282
283         if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
284                 dev_err(dev->dev, "wd: not connected.\n");
285                 ret = -ENODEV;
286                 goto end;
287         }
288
289         dev->wd_state = MEI_WD_RUNNING;
290
291         ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
292         if (ret < 0)
293                 goto end;
294         /* Check if we can send the ping to HW*/
295         if (ret && mei_hbuf_acquire(dev)) {
296
297                 dev_dbg(dev->dev, "wd: sending ping\n");
298
299                 ret = mei_wd_send(dev);
300                 if (ret)
301                         goto end;
302                 dev->wd_pending = false;
303         } else {
304                 dev->wd_pending = true;
305         }
306
307 end:
308         mutex_unlock(&dev->device_lock);
309         return ret;
310 }
311
312 /*
313  * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
314  *
315  * @wd_dev - watchdog device struct
316  * @timeout - timeout value to set
317  *
318  * returns 0 if success, negative errno code for failure
319  */
320 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
321                 unsigned int timeout)
322 {
323         struct mei_device *dev;
324
325         dev = watchdog_get_drvdata(wd_dev);
326         if (!dev)
327                 return -ENODEV;
328
329         /* Check Timeout value */
330         if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
331                 return -EINVAL;
332
333         mutex_lock(&dev->device_lock);
334
335         dev->wd_timeout = timeout;
336         wd_dev->timeout = timeout;
337         mei_wd_set_start_timeout(dev, dev->wd_timeout);
338
339         mutex_unlock(&dev->device_lock);
340
341         return 0;
342 }
343
344 /*
345  * Watchdog Device structs
346  */
347 static const struct watchdog_ops wd_ops = {
348                 .owner = THIS_MODULE,
349                 .start = mei_wd_ops_start,
350                 .stop = mei_wd_ops_stop,
351                 .ping = mei_wd_ops_ping,
352                 .set_timeout = mei_wd_ops_set_timeout,
353 };
354 static const struct watchdog_info wd_info = {
355                 .identity = INTEL_AMT_WATCHDOG_ID,
356                 .options = WDIOF_KEEPALIVEPING |
357                            WDIOF_SETTIMEOUT |
358                            WDIOF_ALARMONLY,
359 };
360
361 static struct watchdog_device amt_wd_dev = {
362                 .info = &wd_info,
363                 .ops = &wd_ops,
364                 .timeout = MEI_WD_DEFAULT_TIMEOUT,
365                 .min_timeout = MEI_WD_MIN_TIMEOUT,
366                 .max_timeout = MEI_WD_MAX_TIMEOUT,
367 };
368
369
370 int mei_watchdog_register(struct mei_device *dev)
371 {
372
373         int ret;
374
375         /* unlock to perserve correct locking order */
376         mutex_unlock(&dev->device_lock);
377         ret = watchdog_register_device(&amt_wd_dev);
378         mutex_lock(&dev->device_lock);
379         if (ret) {
380                 dev_err(dev->dev, "wd: unable to register watchdog device = %d.\n",
381                         ret);
382                 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 void mei_watchdog_unregister(struct mei_device *dev)
391 {
392         if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
393                 return;
394
395         watchdog_set_drvdata(&amt_wd_dev, NULL);
396         watchdog_unregister_device(&amt_wd_dev);
397 }
398