blob: f70945ed96f6f82ab6f6f62d5485cea9fa39ea16 [file] [log] [blame]
Oren Weil3ceb3e62011-05-15 13:43:47 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba91c2012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weil3ceb3e62011-05-15 13:43:47 +03005 *
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>
Oren Weil9ce178e2011-09-07 09:03:09 +030022#include <linux/watchdog.h>
Oren Weil3ceb3e62011-05-15 13:43:47 +030023
Tomas Winkler4f3afe12012-05-09 16:38:59 +030024#include <linux/mei.h>
Oren Weil3ceb3e62011-05-15 13:43:47 +030025
Tomas Winkler47a73802012-12-25 19:06:03 +020026#include "mei_dev.h"
Tomas Winkler0edb23f2013-01-08 23:07:12 +020027#include "hbm.h"
Tomas Winkler9dc64d62013-01-08 23:07:17 +020028#include "hw-me.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020029#include "client.h"
Tomas Winkler47a73802012-12-25 19:06:03 +020030
Oren Weil3ceb3e62011-05-15 13:43:47 +030031static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
32static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
33
Tomas Winkler70cd5332011-12-22 18:50:50 +020034/*
35 * AMT Watchdog Device
36 */
37#define INTEL_AMT_WATCHDOG_ID "INTCAMT"
38
Oren Weil3ceb3e62011-05-15 13:43:47 +030039/* UUIDs for AMT F/W clients */
40const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
41 0x9D, 0xA9, 0x15, 0x14, 0xCB,
42 0x32, 0xAB);
43
Tomas Winkler5192dda2012-03-19 17:58:43 +020044static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
Oren Weil3ceb3e62011-05-15 13:43:47 +030045{
Tomas Winkler13c398a2012-04-03 23:34:59 +030046 dev_dbg(&dev->pdev->dev, "wd: set timeout=%d.\n", timeout);
Tomas Winkler248ffdf2012-08-16 19:39:42 +030047 memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
48 memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
Oren Weil3ceb3e62011-05-15 13:43:47 +030049}
50
51/**
Tomas Winkler7cb1ba92012-07-04 19:24:51 +030052 * mei_wd_host_init - connect to the watchdog client
Oren Weil3ceb3e62011-05-15 13:43:47 +030053 *
54 * @dev: the device structure
Masanari Iida393b1482013-04-05 01:05:05 +090055 *
Tomas Winkler9487eb02012-04-03 23:34:58 +030056 * returns -ENENT if wd client cannot be found
57 * -EIO if write has failed
Tomas Winkler7cb1ba92012-07-04 19:24:51 +030058 * 0 on success
Oren Weil3ceb3e62011-05-15 13:43:47 +030059 */
Tomas Winkler9487eb02012-04-03 23:34:58 +030060int mei_wd_host_init(struct mei_device *dev)
Oren Weil3ceb3e62011-05-15 13:43:47 +030061{
Tomas Winkler781d0d82013-01-08 23:07:22 +020062 struct mei_cl *cl = &dev->wd_cl;
Tomas Winkler50f67a02013-10-21 22:05:39 +030063 int id;
Tomas Winkler781d0d82013-01-08 23:07:22 +020064 int ret;
Oren Weil3ceb3e62011-05-15 13:43:47 +030065
Tomas Winkler781d0d82013-01-08 23:07:22 +020066 mei_cl_init(cl, dev);
67
Tomas Winkler248ffdf2012-08-16 19:39:42 +030068 dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
Tomas Winklerc216fde2012-08-16 19:39:43 +030069 dev->wd_state = MEI_WD_IDLE;
Oren Weil3ceb3e62011-05-15 13:43:47 +030070
Oren Weil3ceb3e62011-05-15 13:43:47 +030071
Tomas Winkler781d0d82013-01-08 23:07:22 +020072 /* check for valid client id */
Tomas Winkler50f67a02013-10-21 22:05:39 +030073 id = mei_me_cl_by_uuid(dev, &mei_wd_guid);
74 if (id < 0) {
Tomas Winkler9487eb02012-04-03 23:34:58 +030075 dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
Tomas Winkler50f67a02013-10-21 22:05:39 +030076 return id;
Oren Weil3ceb3e62011-05-15 13:43:47 +030077 }
Oren Weil617aa392011-09-07 09:03:07 +030078
Tomas Winkler50f67a02013-10-21 22:05:39 +030079 cl->me_client_id = dev->me_clients[id].client_id;
Tomas Winkler781d0d82013-01-08 23:07:22 +020080
81 ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
82
83 if (ret < 0) {
84 dev_info(&dev->pdev->dev, "wd: failed link client\n");
Tomas Winkler50f67a02013-10-21 22:05:39 +030085 return ret;
Tomas Winkler781d0d82013-01-08 23:07:22 +020086 }
87
88 cl->state = MEI_FILE_CONNECTING;
89
90 if (mei_hbm_cl_connect_req(dev, cl)) {
Tomas Winkler9487eb02012-04-03 23:34:58 +030091 dev_err(&dev->pdev->dev, "wd: failed to connect to the client\n");
Tomas Winkler781d0d82013-01-08 23:07:22 +020092 cl->state = MEI_FILE_DISCONNECTED;
93 cl->host_client_id = 0;
Tomas Winkler9487eb02012-04-03 23:34:58 +030094 return -EIO;
95 }
Tomas Winkler781d0d82013-01-08 23:07:22 +020096 cl->timer_count = MEI_CONNECT_TIMEOUT;
Tomas Winkler9487eb02012-04-03 23:34:58 +030097
98 return 0;
Oren Weil3ceb3e62011-05-15 13:43:47 +030099}
100
101/**
102 * mei_wd_send - sends watch dog message to fw.
103 *
104 * @dev: the device structure
105 *
106 * returns 0 if success,
107 * -EIO when message send fails
108 * -EINVAL when invalid message is to be sent
109 */
110int mei_wd_send(struct mei_device *dev)
111{
Tomas Winklere46f1872012-12-25 19:06:10 +0200112 struct mei_msg_hdr hdr;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300113
Tomas Winklere46f1872012-12-25 19:06:10 +0200114 hdr.host_addr = dev->wd_cl.host_client_id;
115 hdr.me_addr = dev->wd_cl.me_client_id;
116 hdr.msg_complete = 1;
117 hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +0200118 hdr.internal = 0;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300119
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300120 if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
Tomas Winklere46f1872012-12-25 19:06:10 +0200121 hdr.length = MEI_WD_START_MSG_SIZE;
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300122 else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
Tomas Winklere46f1872012-12-25 19:06:10 +0200123 hdr.length = MEI_WD_STOP_MSG_SIZE;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300124 else
125 return -EINVAL;
126
Tomas Winklere46f1872012-12-25 19:06:10 +0200127 return mei_write_message(dev, &hdr, dev->wd_data);
Oren Weil3ceb3e62011-05-15 13:43:47 +0300128}
129
Oren Weild8deca32011-09-07 09:03:17 +0300130/**
131 * mei_wd_stop - sends watchdog stop message to fw.
132 *
133 * @dev: the device structure
134 * @preserve: indicate if to keep the timeout value
135 *
136 * returns 0 if success,
137 * -EIO when message send fails
138 * -EINVAL when invalid message is to be sent
139 */
Tomas Winklerc216fde2012-08-16 19:39:43 +0300140int mei_wd_stop(struct mei_device *dev)
Oren Weil3ceb3e62011-05-15 13:43:47 +0300141{
142 int ret;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300143
Tomas Winklerc216fde2012-08-16 19:39:43 +0300144 if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
145 dev->wd_state != MEI_WD_RUNNING)
Oren Weil3ceb3e62011-05-15 13:43:47 +0300146 return 0;
147
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300148 memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
Tomas Winklerc216fde2012-08-16 19:39:43 +0300149
150 dev->wd_state = MEI_WD_STOPPING;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300151
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200152 ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
Oren Weil3ceb3e62011-05-15 13:43:47 +0300153 if (ret < 0)
154 goto out;
155
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200156 if (ret && dev->hbuf_is_ready) {
Oren Weil3ceb3e62011-05-15 13:43:47 +0300157 ret = 0;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200158 dev->hbuf_is_ready = false;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300159
160 if (!mei_wd_send(dev)) {
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200161 ret = mei_cl_flow_ctrl_reduce(&dev->wd_cl);
Oren Weil3ceb3e62011-05-15 13:43:47 +0300162 if (ret)
163 goto out;
164 } else {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300165 dev_err(&dev->pdev->dev, "wd: send stop failed\n");
Oren Weil3ceb3e62011-05-15 13:43:47 +0300166 }
167
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300168 dev->wd_pending = false;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300169 } else {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300170 dev->wd_pending = true;
Oren Weil3ceb3e62011-05-15 13:43:47 +0300171 }
Tomas Winklerc216fde2012-08-16 19:39:43 +0300172
Oren Weil3ceb3e62011-05-15 13:43:47 +0300173 mutex_unlock(&dev->device_lock);
174
175 ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
Tomas Winklerc216fde2012-08-16 19:39:43 +0300176 dev->wd_state == MEI_WD_IDLE,
177 msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
Oren Weil3ceb3e62011-05-15 13:43:47 +0300178 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +0300179 if (dev->wd_state == MEI_WD_IDLE) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300180 dev_dbg(&dev->pdev->dev, "wd: stop completed ret=%d.\n", ret);
Tomas Winklera534bb62011-06-13 16:39:31 +0300181 ret = 0;
182 } else {
183 if (!ret)
184 ret = -ETIMEDOUT;
185 dev_warn(&dev->pdev->dev,
Tomas Winkler13c398a2012-04-03 23:34:59 +0300186 "wd: stop failed to complete ret=%d.\n", ret);
Tomas Winklera534bb62011-06-13 16:39:31 +0300187 }
Oren Weil3ceb3e62011-05-15 13:43:47 +0300188
Oren Weil3ceb3e62011-05-15 13:43:47 +0300189out:
190 return ret;
191}
192
Oren Weil4a3cafd2011-09-07 09:03:10 +0300193/*
194 * mei_wd_ops_start - wd start command from the watchdog core.
195 *
196 * @wd_dev - watchdog device struct
197 *
198 * returns 0 if success, negative errno code for failure
199 */
200static int mei_wd_ops_start(struct watchdog_device *wd_dev)
201{
202 int err = -ENODEV;
203 struct mei_device *dev;
204
Tomas Winkler09649a82012-08-16 19:39:44 +0300205 dev = watchdog_get_drvdata(wd_dev);
Oren Weil4a3cafd2011-09-07 09:03:10 +0300206 if (!dev)
207 return -ENODEV;
208
209 mutex_lock(&dev->device_lock);
210
Tomas Winklerb210d752012-08-07 00:03:56 +0300211 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300212 dev_dbg(&dev->pdev->dev,
Tomas Winklerb210d752012-08-07 00:03:56 +0300213 "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
214 mei_dev_state_str(dev->dev_state));
Oren Weil4a3cafd2011-09-07 09:03:10 +0300215 goto end_unlock;
216 }
217
218 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300219 dev_dbg(&dev->pdev->dev,
220 "MEI Driver is not connected to Watchdog Client\n");
Oren Weil4a3cafd2011-09-07 09:03:10 +0300221 goto end_unlock;
222 }
223
Oren Weil55d33852011-09-07 09:03:12 +0300224 mei_wd_set_start_timeout(dev, dev->wd_timeout);
Oren Weil4a3cafd2011-09-07 09:03:10 +0300225
226 err = 0;
227end_unlock:
228 mutex_unlock(&dev->device_lock);
229 return err;
230}
231
232/*
233 * mei_wd_ops_stop - wd stop command from the watchdog core.
234 *
235 * @wd_dev - watchdog device struct
236 *
237 * returns 0 if success, negative errno code for failure
238 */
239static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
240{
241 struct mei_device *dev;
Oren Weil4a3cafd2011-09-07 09:03:10 +0300242
Tomas Winkler09649a82012-08-16 19:39:44 +0300243 dev = watchdog_get_drvdata(wd_dev);
Oren Weil4a3cafd2011-09-07 09:03:10 +0300244 if (!dev)
245 return -ENODEV;
246
247 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +0300248 mei_wd_stop(dev);
Oren Weil4a3cafd2011-09-07 09:03:10 +0300249 mutex_unlock(&dev->device_lock);
250
251 return 0;
252}
253
254/*
Oren Weil8c4a59a2011-09-07 09:03:11 +0300255 * mei_wd_ops_ping - wd ping command from the watchdog core.
256 *
257 * @wd_dev - watchdog device struct
258 *
259 * returns 0 if success, negative errno code for failure
260 */
261static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
262{
263 int ret = 0;
264 struct mei_device *dev;
Oren Weil8c4a59a2011-09-07 09:03:11 +0300265
Tomas Winkler09649a82012-08-16 19:39:44 +0300266 dev = watchdog_get_drvdata(wd_dev);
Oren Weil8c4a59a2011-09-07 09:03:11 +0300267 if (!dev)
268 return -ENODEV;
269
270 mutex_lock(&dev->device_lock);
271
272 if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300273 dev_err(&dev->pdev->dev, "wd: not connected.\n");
Oren Weil8c4a59a2011-09-07 09:03:11 +0300274 ret = -ENODEV;
275 goto end;
276 }
277
Tomas Winklerc216fde2012-08-16 19:39:43 +0300278 dev->wd_state = MEI_WD_RUNNING;
279
Oren Weil8c4a59a2011-09-07 09:03:11 +0300280 /* Check if we can send the ping to HW*/
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200281 if (dev->hbuf_is_ready && mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
Oren Weil8c4a59a2011-09-07 09:03:11 +0300282
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200283 dev->hbuf_is_ready = false;
Tomas Winkler13c398a2012-04-03 23:34:59 +0300284 dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
Oren Weil8c4a59a2011-09-07 09:03:11 +0300285
286 if (mei_wd_send(dev)) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300287 dev_err(&dev->pdev->dev, "wd: send failed.\n");
Oren Weil8c4a59a2011-09-07 09:03:11 +0300288 ret = -EIO;
289 goto end;
290 }
291
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200292 if (mei_cl_flow_ctrl_reduce(&dev->wd_cl)) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300293 dev_err(&dev->pdev->dev,
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200294 "wd: mei_cl_flow_ctrl_reduce() failed.\n");
Oren Weil8c4a59a2011-09-07 09:03:11 +0300295 ret = -EIO;
296 goto end;
297 }
298
299 } else {
300 dev->wd_pending = true;
301 }
302
303end:
304 mutex_unlock(&dev->device_lock);
305 return ret;
306}
307
308/*
Oren Weil55d33852011-09-07 09:03:12 +0300309 * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
310 *
311 * @wd_dev - watchdog device struct
312 * @timeout - timeout value to set
313 *
314 * returns 0 if success, negative errno code for failure
315 */
Tomas Winkler5f1e54e2013-04-13 23:41:25 +0300316static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
317 unsigned int timeout)
Oren Weil55d33852011-09-07 09:03:12 +0300318{
319 struct mei_device *dev;
Oren Weil55d33852011-09-07 09:03:12 +0300320
Tomas Winkler09649a82012-08-16 19:39:44 +0300321 dev = watchdog_get_drvdata(wd_dev);
Oren Weil55d33852011-09-07 09:03:12 +0300322 if (!dev)
323 return -ENODEV;
324
325 /* Check Timeout value */
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300326 if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
Oren Weil55d33852011-09-07 09:03:12 +0300327 return -EINVAL;
328
329 mutex_lock(&dev->device_lock);
330
331 dev->wd_timeout = timeout;
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100332 wd_dev->timeout = timeout;
Oren Weil55d33852011-09-07 09:03:12 +0300333 mei_wd_set_start_timeout(dev, dev->wd_timeout);
334
335 mutex_unlock(&dev->device_lock);
336
337 return 0;
338}
339
340/*
Oren Weil4a3cafd2011-09-07 09:03:10 +0300341 * Watchdog Device structs
342 */
Tomas Winkler575c1e42011-10-23 18:30:39 +0200343static const struct watchdog_ops wd_ops = {
Oren Weil4a3cafd2011-09-07 09:03:10 +0300344 .owner = THIS_MODULE,
345 .start = mei_wd_ops_start,
346 .stop = mei_wd_ops_stop,
Oren Weil8c4a59a2011-09-07 09:03:11 +0300347 .ping = mei_wd_ops_ping,
Oren Weil55d33852011-09-07 09:03:12 +0300348 .set_timeout = mei_wd_ops_set_timeout,
Oren Weil4a3cafd2011-09-07 09:03:10 +0300349};
Tomas Winkler575c1e42011-10-23 18:30:39 +0200350static const struct watchdog_info wd_info = {
Oren Weil4a3cafd2011-09-07 09:03:10 +0300351 .identity = INTEL_AMT_WATCHDOG_ID,
Tomas Winklerc8df7292012-08-16 19:39:41 +0300352 .options = WDIOF_KEEPALIVEPING |
353 WDIOF_SETTIMEOUT |
354 WDIOF_ALARMONLY,
Oren Weil4a3cafd2011-09-07 09:03:10 +0300355};
356
Tomas Winkler68d923d2012-04-02 20:32:38 +0300357static struct watchdog_device amt_wd_dev = {
Oren Weil4a3cafd2011-09-07 09:03:10 +0300358 .info = &wd_info,
359 .ops = &wd_ops,
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300360 .timeout = MEI_WD_DEFAULT_TIMEOUT,
361 .min_timeout = MEI_WD_MIN_TIMEOUT,
362 .max_timeout = MEI_WD_MAX_TIMEOUT,
Oren Weil4a3cafd2011-09-07 09:03:10 +0300363};
364
365
Tomas Winkler09649a82012-08-16 19:39:44 +0300366void mei_watchdog_register(struct mei_device *dev)
Tomas Winkler70cd5332011-12-22 18:50:50 +0200367{
Tomas Winkler70cd5332011-12-22 18:50:50 +0200368 if (watchdog_register_device(&amt_wd_dev)) {
Tomas Winkler13c398a2012-04-03 23:34:59 +0300369 dev_err(&dev->pdev->dev,
370 "wd: unable to register watchdog device.\n");
Tomas Winkler09649a82012-08-16 19:39:44 +0300371 return;
Tomas Winkler70cd5332011-12-22 18:50:50 +0200372 }
Tomas Winkler09649a82012-08-16 19:39:44 +0300373
374 dev_dbg(&dev->pdev->dev,
375 "wd: successfully register watchdog interface.\n");
Tomas Winkler09649a82012-08-16 19:39:44 +0300376 watchdog_set_drvdata(&amt_wd_dev, dev);
Tomas Winkler70cd5332011-12-22 18:50:50 +0200377}
378
379void mei_watchdog_unregister(struct mei_device *dev)
380{
Tomas Winklerd6921702012-12-16 13:23:17 +0200381 if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
Tomas Winkler09649a82012-08-16 19:39:44 +0300382 return;
383
384 watchdog_set_drvdata(&amt_wd_dev, NULL);
385 watchdog_unregister_device(&amt_wd_dev);
Tomas Winkler70cd5332011-12-22 18:50:50 +0200386}
387