btrfs: replace remaining do_div calls with div_u64 variants
[linux/fpc-iii.git] / drivers / misc / mei / wd.c
blobb1d892cea94da2f0733670392429fa09bb1a58ed
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/device.h>
20 #include <linux/sched.h>
21 #include <linux/watchdog.h>
23 #include <linux/mei.h>
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
29 static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
30 static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
33 * AMT Watchdog Device
35 #define INTEL_AMT_WATCHDOG_ID "INTCAMT"
37 /* UUIDs for AMT F/W clients */
38 const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
39 0x9D, 0xA9, 0x15, 0x14, 0xCB,
40 0x32, 0xAB);
42 static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
44 dev_dbg(dev->dev, "wd: set timeout=%d.\n", timeout);
45 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
46 memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
49 /**
50 * mei_wd_host_init - connect to the watchdog client
52 * @dev: the device structure
54 * Return: -ENOTTY if wd client cannot be found
55 * -EIO if write has failed
56 * 0 on success
58 int mei_wd_host_init(struct mei_device *dev)
60 struct mei_cl *cl = &dev->wd_cl;
61 struct mei_me_client *me_cl;
62 int ret;
64 mei_cl_init(cl, dev);
66 dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
67 dev->wd_state = MEI_WD_IDLE;
70 /* check for valid client id */
71 me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
72 if (!me_cl) {
73 dev_info(dev->dev, "wd: failed to find the client\n");
74 return -ENOTTY;
77 cl->me_client_id = me_cl->client_id;
78 cl->cl_uuid = me_cl->props.protocol_name;
80 ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
82 if (ret < 0) {
83 dev_info(dev->dev, "wd: failed link client\n");
84 return ret;
87 ret = mei_cl_connect(cl, NULL);
89 if (ret) {
90 dev_err(dev->dev, "wd: failed to connect = %d\n", ret);
91 mei_cl_unlink(cl);
92 return ret;
95 ret = mei_watchdog_register(dev);
96 if (ret) {
97 mei_cl_disconnect(cl);
98 mei_cl_unlink(cl);
100 return ret;
104 * mei_wd_send - sends watch dog message to fw.
106 * @dev: the device structure
108 * Return: 0 if success,
109 * -EIO when message send fails
110 * -EINVAL when invalid message is to be sent
111 * -ENODEV on flow control failure
113 int mei_wd_send(struct mei_device *dev)
115 struct mei_cl *cl = &dev->wd_cl;
116 struct mei_msg_hdr hdr;
117 int ret;
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;
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->dev, "wd: invalid message is to be sent, aborting\n");
131 return -EINVAL;
134 ret = mei_write_message(dev, &hdr, dev->wd_data);
135 if (ret) {
136 dev_err(dev->dev, "wd: write message failed\n");
137 return ret;
140 ret = mei_cl_flow_ctrl_reduce(cl);
141 if (ret) {
142 dev_err(dev->dev, "wd: flow_ctrl_reduce failed.\n");
143 return ret;
146 return 0;
150 * mei_wd_stop - sends watchdog stop message to fw.
152 * @dev: the device structure
154 * Return: 0 if success
155 * on error:
156 * -EIO when message send fails
157 * -EINVAL when invalid message is to be sent
158 * -ETIME on message timeout
160 int mei_wd_stop(struct mei_device *dev)
162 int ret;
164 if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
165 dev->wd_state != MEI_WD_RUNNING)
166 return 0;
168 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
170 dev->wd_state = MEI_WD_STOPPING;
172 ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
173 if (ret < 0)
174 goto err;
176 if (ret && mei_hbuf_acquire(dev)) {
177 ret = mei_wd_send(dev);
178 if (ret)
179 goto err;
180 dev->wd_pending = false;
181 } else {
182 dev->wd_pending = true;
185 mutex_unlock(&dev->device_lock);
187 ret = wait_event_timeout(dev->wait_stop_wd,
188 dev->wd_state == MEI_WD_IDLE,
189 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
190 mutex_lock(&dev->device_lock);
191 if (dev->wd_state != MEI_WD_IDLE) {
192 /* timeout */
193 ret = -ETIME;
194 dev_warn(dev->dev, "wd: stop failed to complete ret=%d\n", ret);
195 goto err;
197 dev_dbg(dev->dev, "wd: stop completed after %u msec\n",
198 MEI_WD_STOP_TIMEOUT - jiffies_to_msecs(ret));
199 return 0;
200 err:
201 return ret;
205 * mei_wd_ops_start - wd start command from the watchdog core.
207 * @wd_dev - watchdog device struct
209 * Return: 0 if success, negative errno code for failure
211 static int mei_wd_ops_start(struct watchdog_device *wd_dev)
213 int err = -ENODEV;
214 struct mei_device *dev;
216 dev = watchdog_get_drvdata(wd_dev);
217 if (!dev)
218 return -ENODEV;
220 mutex_lock(&dev->device_lock);
222 if (dev->dev_state != MEI_DEV_ENABLED) {
223 dev_dbg(dev->dev, "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
224 mei_dev_state_str(dev->dev_state));
225 goto end_unlock;
228 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
229 dev_dbg(dev->dev, "MEI Driver is not connected to Watchdog Client\n");
230 goto end_unlock;
233 mei_wd_set_start_timeout(dev, dev->wd_timeout);
235 err = 0;
236 end_unlock:
237 mutex_unlock(&dev->device_lock);
238 return err;
242 * mei_wd_ops_stop - wd stop command from the watchdog core.
244 * @wd_dev - watchdog device struct
246 * Return: 0 if success, negative errno code for failure
248 static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
250 struct mei_device *dev;
252 dev = watchdog_get_drvdata(wd_dev);
253 if (!dev)
254 return -ENODEV;
256 mutex_lock(&dev->device_lock);
257 mei_wd_stop(dev);
258 mutex_unlock(&dev->device_lock);
260 return 0;
264 * mei_wd_ops_ping - wd ping command from the watchdog core.
266 * @wd_dev - watchdog device struct
268 * Return: 0 if success, negative errno code for failure
270 static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
272 struct mei_device *dev;
273 struct mei_cl *cl;
274 int ret;
276 dev = watchdog_get_drvdata(wd_dev);
277 if (!dev)
278 return -ENODEV;
280 cl = &dev->wd_cl;
282 mutex_lock(&dev->device_lock);
284 if (cl->state != MEI_FILE_CONNECTED) {
285 dev_err(dev->dev, "wd: not connected.\n");
286 ret = -ENODEV;
287 goto end;
290 dev->wd_state = MEI_WD_RUNNING;
292 ret = mei_cl_flow_ctrl_creds(cl);
293 if (ret < 0)
294 goto end;
296 /* Check if we can send the ping to HW*/
297 if (ret && mei_hbuf_acquire(dev)) {
298 dev_dbg(dev->dev, "wd: sending ping\n");
300 ret = mei_wd_send(dev);
301 if (ret)
302 goto end;
303 dev->wd_pending = false;
304 } else {
305 dev->wd_pending = true;
308 end:
309 mutex_unlock(&dev->device_lock);
310 return ret;
314 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
316 * @wd_dev - watchdog device struct
317 * @timeout - timeout value to set
319 * Return: 0 if success, negative errno code for failure
321 static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
322 unsigned int timeout)
324 struct mei_device *dev;
326 dev = watchdog_get_drvdata(wd_dev);
327 if (!dev)
328 return -ENODEV;
330 /* Check Timeout value */
331 if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
332 return -EINVAL;
334 mutex_lock(&dev->device_lock);
336 dev->wd_timeout = timeout;
337 wd_dev->timeout = timeout;
338 mei_wd_set_start_timeout(dev, dev->wd_timeout);
340 mutex_unlock(&dev->device_lock);
342 return 0;
346 * Watchdog Device structs
348 static const struct watchdog_ops wd_ops = {
349 .owner = THIS_MODULE,
350 .start = mei_wd_ops_start,
351 .stop = mei_wd_ops_stop,
352 .ping = mei_wd_ops_ping,
353 .set_timeout = mei_wd_ops_set_timeout,
355 static const struct watchdog_info wd_info = {
356 .identity = INTEL_AMT_WATCHDOG_ID,
357 .options = WDIOF_KEEPALIVEPING |
358 WDIOF_SETTIMEOUT |
359 WDIOF_ALARMONLY,
362 static struct watchdog_device amt_wd_dev = {
363 .info = &wd_info,
364 .ops = &wd_ops,
365 .timeout = MEI_WD_DEFAULT_TIMEOUT,
366 .min_timeout = MEI_WD_MIN_TIMEOUT,
367 .max_timeout = MEI_WD_MAX_TIMEOUT,
371 int mei_watchdog_register(struct mei_device *dev)
374 int ret;
376 /* unlock to perserve correct locking order */
377 mutex_unlock(&dev->device_lock);
378 ret = watchdog_register_device(&amt_wd_dev);
379 mutex_lock(&dev->device_lock);
380 if (ret) {
381 dev_err(dev->dev, "wd: unable to register watchdog device = %d.\n",
382 ret);
383 return ret;
386 dev_dbg(dev->dev, "wd: successfully register watchdog interface.\n");
387 watchdog_set_drvdata(&amt_wd_dev, dev);
388 return 0;
391 void mei_watchdog_unregister(struct mei_device *dev)
393 if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
394 return;
396 watchdog_set_drvdata(&amt_wd_dev, NULL);
397 watchdog_unregister_device(&amt_wd_dev);