blob: 7028dbd99cf7485699686b83047828e2397c2793 [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"
25#include "interface.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020026#include "client.h"
Tomas Winkler47a73802012-12-25 19:06:03 +020027
Tomas Winklerb210d752012-08-07 00:03:56 +030028const char *mei_dev_state_str(int state)
29{
30#define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
31 switch (state) {
32 MEI_DEV_STATE(INITIALIZING);
33 MEI_DEV_STATE(INIT_CLIENTS);
34 MEI_DEV_STATE(ENABLED);
35 MEI_DEV_STATE(RESETING);
36 MEI_DEV_STATE(DISABLED);
37 MEI_DEV_STATE(RECOVERING_FROM_RESET);
38 MEI_DEV_STATE(POWER_DOWN);
39 MEI_DEV_STATE(POWER_UP);
40 default:
41 return "unkown";
42 }
43#undef MEI_DEV_STATE
44}
45
46
Tomas Winkler5bd64712012-11-18 15:13:14 +020047
Oren Weil91f01c62011-05-15 13:43:44 +030048
49/**
Oren Weil91f01c62011-05-15 13:43:44 +030050 * init_mei_device - allocates and initializes the mei device structure
51 *
52 * @pdev: The pci device structure
53 *
54 * returns The mei_device_device pointer on success, NULL on failure.
55 */
Tomas Winklerc95efb72011-05-25 17:28:21 +030056struct mei_device *mei_device_init(struct pci_dev *pdev)
Oren Weil91f01c62011-05-15 13:43:44 +030057{
Oren Weil91f01c62011-05-15 13:43:44 +030058 struct mei_device *dev;
59
60 dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
61 if (!dev)
62 return NULL;
63
64 /* setup our list array */
Oren Weil91f01c62011-05-15 13:43:44 +030065 INIT_LIST_HEAD(&dev->file_list);
66 INIT_LIST_HEAD(&dev->wd_cl.link);
67 INIT_LIST_HEAD(&dev->iamthif_cl.link);
68 mutex_init(&dev->device_lock);
69 init_waitqueue_head(&dev->wait_recvd_msg);
70 init_waitqueue_head(&dev->wait_stop_wd);
Tomas Winklerb210d752012-08-07 00:03:56 +030071 dev->dev_state = MEI_DEV_INITIALIZING;
Oren Weil91f01c62011-05-15 13:43:44 +030072 dev->iamthif_state = MEI_IAMTHIF_IDLE;
Tomas Winkler0288c7c2011-06-06 10:44:34 +030073
74 mei_io_list_init(&dev->read_list);
75 mei_io_list_init(&dev->write_list);
76 mei_io_list_init(&dev->write_waiting_list);
77 mei_io_list_init(&dev->ctrl_wr_list);
78 mei_io_list_init(&dev->ctrl_rd_list);
Tomas Winklere773efc2012-11-11 17:37:58 +020079 mei_io_list_init(&dev->amthif_cmd_list);
80 mei_io_list_init(&dev->amthif_rd_complete_list);
Oren Weil91f01c62011-05-15 13:43:44 +030081 dev->pdev = pdev;
82 return dev;
83}
84
85/**
86 * mei_hw_init - initializes host and fw to start work.
87 *
88 * @dev: the device structure
89 *
90 * returns 0 on success, <0 on failure.
91 */
92int mei_hw_init(struct mei_device *dev)
93{
94 int err = 0;
95 int ret;
96
97 mutex_lock(&dev->device_lock);
98
99 dev->host_hw_state = mei_hcsr_read(dev);
100 dev->me_hw_state = mei_mecsr_read(dev);
101 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
102 dev->host_hw_state, dev->me_hw_state);
103
104 /* acknowledge interrupt and stop interupts */
Tomas Winkler3a65dd42012-12-25 19:06:06 +0200105 mei_clear_interrupts(dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300106
Tomas Winkler24aadc82012-06-25 23:46:27 +0300107 /* Doesn't change in runtime */
108 dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
109
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300110 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300111 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
112
113 mei_reset(dev, 1);
114
115 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
116 dev->host_hw_state, dev->me_hw_state);
117
118 /* wait for ME to turn on ME_RDY */
119 if (!dev->recvd_msg) {
120 mutex_unlock(&dev->device_lock);
121 err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200122 dev->recvd_msg,
123 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300124 mutex_lock(&dev->device_lock);
125 }
126
Tomas Winklera534bb62011-06-13 16:39:31 +0300127 if (err <= 0 && !dev->recvd_msg) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300128 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300129 dev_dbg(&dev->pdev->dev,
130 "wait_event_interruptible_timeout failed"
131 "on wait for ME to turn on ME_RDY.\n");
132 ret = -ENODEV;
133 goto out;
134 }
135
136 if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
137 ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300138 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300139 dev_dbg(&dev->pdev->dev,
140 "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
141 dev->host_hw_state, dev->me_hw_state);
142
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300143 if (!(dev->host_hw_state & H_RDY))
Oren Weil91f01c62011-05-15 13:43:44 +0300144 dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
145
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300146 if (!(dev->me_hw_state & ME_RDY_HRA))
Oren Weil91f01c62011-05-15 13:43:44 +0300147 dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
148
Tomas Winklerd39a4642012-03-19 17:58:42 +0200149 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
Oren Weil91f01c62011-05-15 13:43:44 +0300150 ret = -ENODEV;
151 goto out;
152 }
153
154 if (dev->version.major_version != HBM_MAJOR_VERSION ||
155 dev->version.minor_version != HBM_MINOR_VERSION) {
156 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
157 ret = -ENODEV;
158 goto out;
159 }
160
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300161 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300162 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
163 dev->host_hw_state, dev->me_hw_state);
164 dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
165 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
166 dev_dbg(&dev->pdev->dev, "MEI start success.\n");
167 ret = 0;
168
169out:
170 mutex_unlock(&dev->device_lock);
171 return ret;
172}
173
174/**
175 * mei_hw_reset - resets fw via mei csr register.
176 *
177 * @dev: the device structure
178 * @interrupts_enabled: if interrupt should be enabled after reset.
179 */
180static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
181{
182 dev->host_hw_state |= (H_RST | H_IG);
183
184 if (interrupts_enabled)
185 mei_enable_interrupts(dev);
186 else
187 mei_disable_interrupts(dev);
188}
189
190/**
191 * mei_reset - resets host and fw.
192 *
193 * @dev: the device structure
194 * @interrupts_enabled: if interrupt should be enabled after reset.
195 */
196void mei_reset(struct mei_device *dev, int interrupts_enabled)
197{
198 struct mei_cl *cl_pos = NULL;
199 struct mei_cl *cl_next = NULL;
200 struct mei_cl_cb *cb_pos = NULL;
201 struct mei_cl_cb *cb_next = NULL;
202 bool unexpected;
203
Tomas Winklerb210d752012-08-07 00:03:56 +0300204 if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300205 dev->need_reset = true;
Oren Weil91f01c62011-05-15 13:43:44 +0300206 return;
207 }
208
Tomas Winklerb210d752012-08-07 00:03:56 +0300209 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
210 dev->dev_state != MEI_DEV_DISABLED &&
211 dev->dev_state != MEI_DEV_POWER_DOWN &&
212 dev->dev_state != MEI_DEV_POWER_UP);
Oren Weil91f01c62011-05-15 13:43:44 +0300213
214 dev->host_hw_state = mei_hcsr_read(dev);
215
216 dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
217 dev->host_hw_state);
218
219 mei_hw_reset(dev, interrupts_enabled);
220
221 dev->host_hw_state &= ~H_RST;
222 dev->host_hw_state |= H_IG;
223
224 mei_hcsr_set(dev);
225
226 dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
227 dev->host_hw_state);
228
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300229 dev->need_reset = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300230
Tomas Winklerb210d752012-08-07 00:03:56 +0300231 if (dev->dev_state != MEI_DEV_INITIALIZING) {
232 if (dev->dev_state != MEI_DEV_DISABLED &&
233 dev->dev_state != MEI_DEV_POWER_DOWN)
234 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300235
236 list_for_each_entry_safe(cl_pos,
237 cl_next, &dev->file_list, link) {
238 cl_pos->state = MEI_FILE_DISCONNECTED;
239 cl_pos->mei_flow_ctrl_creds = 0;
240 cl_pos->read_cb = NULL;
241 cl_pos->timer_count = 0;
242 }
243 /* remove entry if already in list */
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200244 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200245 mei_cl_unlink(&dev->wd_cl);
246 mei_cl_unlink(&dev->iamthif_cl);
Oren Weil91f01c62011-05-15 13:43:44 +0300247
Tomas Winkler19838fb2012-11-01 21:17:15 +0200248 mei_amthif_reset_params(dev);
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200249 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
Oren Weil91f01c62011-05-15 13:43:44 +0300250 }
251
Tomas Winklercf9673d2011-06-06 10:44:33 +0300252 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300253 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300254 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300255
256 /* update the state of the registers after reset */
257 dev->host_hw_state = mei_hcsr_read(dev);
258 dev->me_hw_state = mei_mecsr_read(dev);
259
260 dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
261 dev->host_hw_state, dev->me_hw_state);
262
263 if (unexpected)
Tomas Winklerb210d752012-08-07 00:03:56 +0300264 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
265 mei_dev_state_str(dev->dev_state));
Oren Weil91f01c62011-05-15 13:43:44 +0300266
267 /* Wake up all readings so they can be interrupted */
268 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
269 if (waitqueue_active(&cl_pos->rx_wait)) {
270 dev_dbg(&dev->pdev->dev, "Waking up client!\n");
271 wake_up_interruptible(&cl_pos->rx_wait);
272 }
273 }
274 /* remove all waiting requests */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200275 list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
276 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200277 mei_io_cb_free(cb_pos);
Oren Weil91f01c62011-05-15 13:43:44 +0300278 }
279}
280
281
Oren Weil91f01c62011-05-15 13:43:44 +0300282/**
283 * allocate_me_clients_storage - allocates storage for me clients
284 *
285 * @dev: the device structure
286 *
287 * returns none.
288 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300289void mei_allocate_me_clients_storage(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300290{
291 struct mei_me_client *clients;
292 int b;
293
294 /* count how many ME clients we have */
295 for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
Tomas Winklercf9673d2011-06-06 10:44:33 +0300296 dev->me_clients_num++;
Oren Weil91f01c62011-05-15 13:43:44 +0300297
Tomas Winklercf9673d2011-06-06 10:44:33 +0300298 if (dev->me_clients_num <= 0)
Oren Weil91f01c62011-05-15 13:43:44 +0300299 return ;
300
301
302 if (dev->me_clients != NULL) {
303 kfree(dev->me_clients);
304 dev->me_clients = NULL;
305 }
306 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
Tomas Winklercf9673d2011-06-06 10:44:33 +0300307 dev->me_clients_num * sizeof(struct mei_me_client));
Oren Weil91f01c62011-05-15 13:43:44 +0300308 /* allocate storage for ME clients representation */
Tomas Winklercf9673d2011-06-06 10:44:33 +0300309 clients = kcalloc(dev->me_clients_num,
Oren Weil91f01c62011-05-15 13:43:44 +0300310 sizeof(struct mei_me_client), GFP_KERNEL);
311 if (!clients) {
312 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300313 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300314 mei_reset(dev, 1);
315 return ;
316 }
317 dev->me_clients = clients;
318 return ;
319}
Samuel Ortizc1174c02012-11-18 15:13:20 +0200320
Samuel Ortizc1174c02012-11-18 15:13:20 +0200321
Oren Weil91f01c62011-05-15 13:43:44 +0300322