blob: 9e4011ef850860975f404f7e59fff8d8dbcb54b2 [file] [log] [blame]
Oren Weil91f01c62011-05-15 13:43:44 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba912012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weil91f01c62011-05-15 13:43:44 +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
17#include <linux/pci.h>
18#include <linux/sched.h>
19#include <linux/wait.h>
20#include <linux/delay.h>
21
Tomas Winkler4f3afe12012-05-09 16:38:59 +030022#include <linux/mei.h>
Oren Weil91f01c62011-05-15 13:43:44 +030023
Tomas Winkler47a73802012-12-25 19:06:03 +020024#include "mei_dev.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020025#include "client.h"
Tomas Winkler47a73802012-12-25 19:06:03 +020026
Tomas Winklerb210d752012-08-07 00:03:56 +030027const char *mei_dev_state_str(int state)
28{
29#define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
30 switch (state) {
31 MEI_DEV_STATE(INITIALIZING);
32 MEI_DEV_STATE(INIT_CLIENTS);
33 MEI_DEV_STATE(ENABLED);
34 MEI_DEV_STATE(RESETING);
35 MEI_DEV_STATE(DISABLED);
36 MEI_DEV_STATE(RECOVERING_FROM_RESET);
37 MEI_DEV_STATE(POWER_DOWN);
38 MEI_DEV_STATE(POWER_UP);
39 default:
40 return "unkown";
41 }
42#undef MEI_DEV_STATE
43}
44
45
Tomas Winkler5bd64712012-11-18 15:13:14 +020046
Oren Weil91f01c62011-05-15 13:43:44 +030047
48/**
Oren Weil91f01c62011-05-15 13:43:44 +030049 * init_mei_device - allocates and initializes the mei device structure
50 *
51 * @pdev: The pci device structure
52 *
53 * returns The mei_device_device pointer on success, NULL on failure.
54 */
Tomas Winklerc95efb72011-05-25 17:28:21 +030055struct mei_device *mei_device_init(struct pci_dev *pdev)
Oren Weil91f01c62011-05-15 13:43:44 +030056{
Oren Weil91f01c62011-05-15 13:43:44 +030057 struct mei_device *dev;
58
59 dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
60 if (!dev)
61 return NULL;
62
63 /* setup our list array */
Oren Weil91f01c62011-05-15 13:43:44 +030064 INIT_LIST_HEAD(&dev->file_list);
65 INIT_LIST_HEAD(&dev->wd_cl.link);
66 INIT_LIST_HEAD(&dev->iamthif_cl.link);
67 mutex_init(&dev->device_lock);
68 init_waitqueue_head(&dev->wait_recvd_msg);
69 init_waitqueue_head(&dev->wait_stop_wd);
Tomas Winklerb210d752012-08-07 00:03:56 +030070 dev->dev_state = MEI_DEV_INITIALIZING;
Oren Weil91f01c62011-05-15 13:43:44 +030071 dev->iamthif_state = MEI_IAMTHIF_IDLE;
Tomas Winkler0288c7c2011-06-06 10:44:34 +030072
73 mei_io_list_init(&dev->read_list);
74 mei_io_list_init(&dev->write_list);
75 mei_io_list_init(&dev->write_waiting_list);
76 mei_io_list_init(&dev->ctrl_wr_list);
77 mei_io_list_init(&dev->ctrl_rd_list);
Tomas Winklere773efc2012-11-11 17:37:58 +020078 mei_io_list_init(&dev->amthif_cmd_list);
79 mei_io_list_init(&dev->amthif_rd_complete_list);
Oren Weil91f01c62011-05-15 13:43:44 +030080 dev->pdev = pdev;
81 return dev;
82}
83
84/**
85 * mei_hw_init - initializes host and fw to start work.
86 *
87 * @dev: the device structure
88 *
89 * returns 0 on success, <0 on failure.
90 */
91int mei_hw_init(struct mei_device *dev)
92{
93 int err = 0;
94 int ret;
95
96 mutex_lock(&dev->device_lock);
97
98 dev->host_hw_state = mei_hcsr_read(dev);
99 dev->me_hw_state = mei_mecsr_read(dev);
100 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
101 dev->host_hw_state, dev->me_hw_state);
102
103 /* acknowledge interrupt and stop interupts */
Tomas Winkler3a65dd42012-12-25 19:06:06 +0200104 mei_clear_interrupts(dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300105
Tomas Winkler24aadc82012-06-25 23:46:27 +0300106 /* Doesn't change in runtime */
107 dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
108
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300109 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300110 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
111
112 mei_reset(dev, 1);
113
114 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
115 dev->host_hw_state, dev->me_hw_state);
116
117 /* wait for ME to turn on ME_RDY */
118 if (!dev->recvd_msg) {
119 mutex_unlock(&dev->device_lock);
120 err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200121 dev->recvd_msg,
122 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300123 mutex_lock(&dev->device_lock);
124 }
125
Tomas Winklera534bb62011-06-13 16:39:31 +0300126 if (err <= 0 && !dev->recvd_msg) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300127 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300128 dev_dbg(&dev->pdev->dev,
129 "wait_event_interruptible_timeout failed"
130 "on wait for ME to turn on ME_RDY.\n");
131 ret = -ENODEV;
132 goto out;
133 }
134
135 if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
136 ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300137 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300138 dev_dbg(&dev->pdev->dev,
139 "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
140 dev->host_hw_state, dev->me_hw_state);
141
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300142 if (!(dev->host_hw_state & H_RDY))
Oren Weil91f01c62011-05-15 13:43:44 +0300143 dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
144
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300145 if (!(dev->me_hw_state & ME_RDY_HRA))
Oren Weil91f01c62011-05-15 13:43:44 +0300146 dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
147
Tomas Winklerd39a4642012-03-19 17:58:42 +0200148 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
Oren Weil91f01c62011-05-15 13:43:44 +0300149 ret = -ENODEV;
150 goto out;
151 }
152
153 if (dev->version.major_version != HBM_MAJOR_VERSION ||
154 dev->version.minor_version != HBM_MINOR_VERSION) {
155 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
156 ret = -ENODEV;
157 goto out;
158 }
159
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300160 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300161 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
162 dev->host_hw_state, dev->me_hw_state);
163 dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
164 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
165 dev_dbg(&dev->pdev->dev, "MEI start success.\n");
166 ret = 0;
167
168out:
169 mutex_unlock(&dev->device_lock);
170 return ret;
171}
172
173/**
174 * mei_hw_reset - resets fw via mei csr register.
175 *
176 * @dev: the device structure
177 * @interrupts_enabled: if interrupt should be enabled after reset.
178 */
179static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
180{
181 dev->host_hw_state |= (H_RST | H_IG);
182
183 if (interrupts_enabled)
184 mei_enable_interrupts(dev);
185 else
186 mei_disable_interrupts(dev);
187}
188
189/**
190 * mei_reset - resets host and fw.
191 *
192 * @dev: the device structure
193 * @interrupts_enabled: if interrupt should be enabled after reset.
194 */
195void mei_reset(struct mei_device *dev, int interrupts_enabled)
196{
197 struct mei_cl *cl_pos = NULL;
198 struct mei_cl *cl_next = NULL;
199 struct mei_cl_cb *cb_pos = NULL;
200 struct mei_cl_cb *cb_next = NULL;
201 bool unexpected;
202
Tomas Winklerb210d752012-08-07 00:03:56 +0300203 if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300204 dev->need_reset = true;
Oren Weil91f01c62011-05-15 13:43:44 +0300205 return;
206 }
207
Tomas Winklerb210d752012-08-07 00:03:56 +0300208 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
209 dev->dev_state != MEI_DEV_DISABLED &&
210 dev->dev_state != MEI_DEV_POWER_DOWN &&
211 dev->dev_state != MEI_DEV_POWER_UP);
Oren Weil91f01c62011-05-15 13:43:44 +0300212
213 dev->host_hw_state = mei_hcsr_read(dev);
214
215 dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
216 dev->host_hw_state);
217
218 mei_hw_reset(dev, interrupts_enabled);
219
220 dev->host_hw_state &= ~H_RST;
221 dev->host_hw_state |= H_IG;
222
223 mei_hcsr_set(dev);
224
225 dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
226 dev->host_hw_state);
227
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300228 dev->need_reset = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300229
Tomas Winklerb210d752012-08-07 00:03:56 +0300230 if (dev->dev_state != MEI_DEV_INITIALIZING) {
231 if (dev->dev_state != MEI_DEV_DISABLED &&
232 dev->dev_state != MEI_DEV_POWER_DOWN)
233 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300234
235 list_for_each_entry_safe(cl_pos,
236 cl_next, &dev->file_list, link) {
237 cl_pos->state = MEI_FILE_DISCONNECTED;
238 cl_pos->mei_flow_ctrl_creds = 0;
239 cl_pos->read_cb = NULL;
240 cl_pos->timer_count = 0;
241 }
242 /* remove entry if already in list */
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200243 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200244 mei_cl_unlink(&dev->wd_cl);
Tomas Winkler781d0d82013-01-08 23:07:22 +0200245 if (dev->open_handle_count > 0)
246 dev->open_handle_count--;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200247 mei_cl_unlink(&dev->iamthif_cl);
Tomas Winkler781d0d82013-01-08 23:07:22 +0200248 if (dev->open_handle_count > 0)
249 dev->open_handle_count--;
Oren Weil91f01c62011-05-15 13:43:44 +0300250
Tomas Winkler19838fb2012-11-01 21:17:15 +0200251 mei_amthif_reset_params(dev);
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200252 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
Oren Weil91f01c62011-05-15 13:43:44 +0300253 }
254
Tomas Winklercf9673d2011-06-06 10:44:33 +0300255 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300256 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300257 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300258
259 /* update the state of the registers after reset */
260 dev->host_hw_state = mei_hcsr_read(dev);
261 dev->me_hw_state = mei_mecsr_read(dev);
262
263 dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
264 dev->host_hw_state, dev->me_hw_state);
265
266 if (unexpected)
Tomas Winklerb210d752012-08-07 00:03:56 +0300267 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
268 mei_dev_state_str(dev->dev_state));
Oren Weil91f01c62011-05-15 13:43:44 +0300269
270 /* Wake up all readings so they can be interrupted */
271 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
272 if (waitqueue_active(&cl_pos->rx_wait)) {
273 dev_dbg(&dev->pdev->dev, "Waking up client!\n");
274 wake_up_interruptible(&cl_pos->rx_wait);
275 }
276 }
277 /* remove all waiting requests */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200278 list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
279 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200280 mei_io_cb_free(cb_pos);
Oren Weil91f01c62011-05-15 13:43:44 +0300281 }
282}
283
284
Samuel Ortizc1174c02012-11-18 15:13:20 +0200285
Oren Weil91f01c62011-05-15 13:43:44 +0300286