blob: eb180555d28279e12a16439b588fc95d5c92e058 [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"
26
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/**
Tomas Winkler0288c7c2011-06-06 10:44:34 +030048 * mei_io_list_flush - removes list entry belonging to cl.
Oren Weil91f01c62011-05-15 13:43:44 +030049 *
50 * @list: An instance of our list structure
51 * @cl: private data of the file object
52 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +020053void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
Oren Weil91f01c62011-05-15 13:43:44 +030054{
Tomas Winkler3a5352f2011-12-04 16:16:27 +020055 struct mei_cl_cb *pos;
56 struct mei_cl_cb *next;
Oren Weil91f01c62011-05-15 13:43:44 +030057
Tomas Winklerfb601ad2012-10-15 12:06:48 +020058 list_for_each_entry_safe(pos, next, &list->list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +020059 if (pos->cl) {
60 if (mei_cl_cmp_id(cl, pos->cl))
Tomas Winklerfb601ad2012-10-15 12:06:48 +020061 list_del(&pos->list);
Oren Weil91f01c62011-05-15 13:43:44 +030062 }
63 }
64}
Tomas Winkler0288c7c2011-06-06 10:44:34 +030065/**
66 * mei_cl_flush_queues - flushes queue lists belonging to cl.
67 *
68 * @dev: the device structure
69 * @cl: private data of the file object
70 */
71int mei_cl_flush_queues(struct mei_cl *cl)
72{
73 if (!cl || !cl->dev)
74 return -EINVAL;
75
76 dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n");
77 mei_io_list_flush(&cl->dev->read_list, cl);
78 mei_io_list_flush(&cl->dev->write_list, cl);
79 mei_io_list_flush(&cl->dev->write_waiting_list, cl);
80 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
81 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
Tomas Winklere773efc2012-11-11 17:37:58 +020082 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
83 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
Tomas Winkler0288c7c2011-06-06 10:44:34 +030084 return 0;
85}
86
87
Oren Weil91f01c62011-05-15 13:43:44 +030088
89/**
Oren Weil91f01c62011-05-15 13:43:44 +030090 * init_mei_device - allocates and initializes the mei device structure
91 *
92 * @pdev: The pci device structure
93 *
94 * returns The mei_device_device pointer on success, NULL on failure.
95 */
Tomas Winklerc95efb72011-05-25 17:28:21 +030096struct mei_device *mei_device_init(struct pci_dev *pdev)
Oren Weil91f01c62011-05-15 13:43:44 +030097{
Oren Weil91f01c62011-05-15 13:43:44 +030098 struct mei_device *dev;
99
100 dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
101 if (!dev)
102 return NULL;
103
104 /* setup our list array */
Oren Weil91f01c62011-05-15 13:43:44 +0300105 INIT_LIST_HEAD(&dev->file_list);
106 INIT_LIST_HEAD(&dev->wd_cl.link);
107 INIT_LIST_HEAD(&dev->iamthif_cl.link);
108 mutex_init(&dev->device_lock);
109 init_waitqueue_head(&dev->wait_recvd_msg);
110 init_waitqueue_head(&dev->wait_stop_wd);
Tomas Winklerb210d752012-08-07 00:03:56 +0300111 dev->dev_state = MEI_DEV_INITIALIZING;
Oren Weil91f01c62011-05-15 13:43:44 +0300112 dev->iamthif_state = MEI_IAMTHIF_IDLE;
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300113
114 mei_io_list_init(&dev->read_list);
115 mei_io_list_init(&dev->write_list);
116 mei_io_list_init(&dev->write_waiting_list);
117 mei_io_list_init(&dev->ctrl_wr_list);
118 mei_io_list_init(&dev->ctrl_rd_list);
Tomas Winklere773efc2012-11-11 17:37:58 +0200119 mei_io_list_init(&dev->amthif_cmd_list);
120 mei_io_list_init(&dev->amthif_rd_complete_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300121 dev->pdev = pdev;
122 return dev;
123}
124
125/**
126 * mei_hw_init - initializes host and fw to start work.
127 *
128 * @dev: the device structure
129 *
130 * returns 0 on success, <0 on failure.
131 */
132int mei_hw_init(struct mei_device *dev)
133{
134 int err = 0;
135 int ret;
136
137 mutex_lock(&dev->device_lock);
138
139 dev->host_hw_state = mei_hcsr_read(dev);
140 dev->me_hw_state = mei_mecsr_read(dev);
141 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
142 dev->host_hw_state, dev->me_hw_state);
143
144 /* acknowledge interrupt and stop interupts */
Tomas Winkler3a65dd42012-12-25 19:06:06 +0200145 mei_clear_interrupts(dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300146
Tomas Winkler24aadc82012-06-25 23:46:27 +0300147 /* Doesn't change in runtime */
148 dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
149
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300150 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300151 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
152
153 mei_reset(dev, 1);
154
155 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
156 dev->host_hw_state, dev->me_hw_state);
157
158 /* wait for ME to turn on ME_RDY */
159 if (!dev->recvd_msg) {
160 mutex_unlock(&dev->device_lock);
161 err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200162 dev->recvd_msg,
163 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300164 mutex_lock(&dev->device_lock);
165 }
166
Tomas Winklera534bb62011-06-13 16:39:31 +0300167 if (err <= 0 && !dev->recvd_msg) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300168 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300169 dev_dbg(&dev->pdev->dev,
170 "wait_event_interruptible_timeout failed"
171 "on wait for ME to turn on ME_RDY.\n");
172 ret = -ENODEV;
173 goto out;
174 }
175
176 if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
177 ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300178 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300179 dev_dbg(&dev->pdev->dev,
180 "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
181 dev->host_hw_state, dev->me_hw_state);
182
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300183 if (!(dev->host_hw_state & H_RDY))
Oren Weil91f01c62011-05-15 13:43:44 +0300184 dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
185
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300186 if (!(dev->me_hw_state & ME_RDY_HRA))
Oren Weil91f01c62011-05-15 13:43:44 +0300187 dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
188
Tomas Winklerd39a4642012-03-19 17:58:42 +0200189 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
Oren Weil91f01c62011-05-15 13:43:44 +0300190 ret = -ENODEV;
191 goto out;
192 }
193
194 if (dev->version.major_version != HBM_MAJOR_VERSION ||
195 dev->version.minor_version != HBM_MINOR_VERSION) {
196 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
197 ret = -ENODEV;
198 goto out;
199 }
200
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300201 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300202 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
203 dev->host_hw_state, dev->me_hw_state);
204 dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
205 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
206 dev_dbg(&dev->pdev->dev, "MEI start success.\n");
207 ret = 0;
208
209out:
210 mutex_unlock(&dev->device_lock);
211 return ret;
212}
213
214/**
215 * mei_hw_reset - resets fw via mei csr register.
216 *
217 * @dev: the device structure
218 * @interrupts_enabled: if interrupt should be enabled after reset.
219 */
220static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
221{
222 dev->host_hw_state |= (H_RST | H_IG);
223
224 if (interrupts_enabled)
225 mei_enable_interrupts(dev);
226 else
227 mei_disable_interrupts(dev);
228}
229
230/**
231 * mei_reset - resets host and fw.
232 *
233 * @dev: the device structure
234 * @interrupts_enabled: if interrupt should be enabled after reset.
235 */
236void mei_reset(struct mei_device *dev, int interrupts_enabled)
237{
238 struct mei_cl *cl_pos = NULL;
239 struct mei_cl *cl_next = NULL;
240 struct mei_cl_cb *cb_pos = NULL;
241 struct mei_cl_cb *cb_next = NULL;
242 bool unexpected;
243
Tomas Winklerb210d752012-08-07 00:03:56 +0300244 if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300245 dev->need_reset = true;
Oren Weil91f01c62011-05-15 13:43:44 +0300246 return;
247 }
248
Tomas Winklerb210d752012-08-07 00:03:56 +0300249 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
250 dev->dev_state != MEI_DEV_DISABLED &&
251 dev->dev_state != MEI_DEV_POWER_DOWN &&
252 dev->dev_state != MEI_DEV_POWER_UP);
Oren Weil91f01c62011-05-15 13:43:44 +0300253
254 dev->host_hw_state = mei_hcsr_read(dev);
255
256 dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
257 dev->host_hw_state);
258
259 mei_hw_reset(dev, interrupts_enabled);
260
261 dev->host_hw_state &= ~H_RST;
262 dev->host_hw_state |= H_IG;
263
264 mei_hcsr_set(dev);
265
266 dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
267 dev->host_hw_state);
268
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300269 dev->need_reset = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300270
Tomas Winklerb210d752012-08-07 00:03:56 +0300271 if (dev->dev_state != MEI_DEV_INITIALIZING) {
272 if (dev->dev_state != MEI_DEV_DISABLED &&
273 dev->dev_state != MEI_DEV_POWER_DOWN)
274 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300275
276 list_for_each_entry_safe(cl_pos,
277 cl_next, &dev->file_list, link) {
278 cl_pos->state = MEI_FILE_DISCONNECTED;
279 cl_pos->mei_flow_ctrl_creds = 0;
280 cl_pos->read_cb = NULL;
281 cl_pos->timer_count = 0;
282 }
283 /* remove entry if already in list */
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200284 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
285 mei_me_cl_unlink(dev, &dev->wd_cl);
Oren Weil91f01c62011-05-15 13:43:44 +0300286
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200287 mei_me_cl_unlink(dev, &dev->iamthif_cl);
Oren Weil91f01c62011-05-15 13:43:44 +0300288
Tomas Winkler19838fb2012-11-01 21:17:15 +0200289 mei_amthif_reset_params(dev);
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200290 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
Oren Weil91f01c62011-05-15 13:43:44 +0300291 }
292
Tomas Winklercf9673d2011-06-06 10:44:33 +0300293 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300294 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300295 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300296
297 /* update the state of the registers after reset */
298 dev->host_hw_state = mei_hcsr_read(dev);
299 dev->me_hw_state = mei_mecsr_read(dev);
300
301 dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
302 dev->host_hw_state, dev->me_hw_state);
303
304 if (unexpected)
Tomas Winklerb210d752012-08-07 00:03:56 +0300305 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
306 mei_dev_state_str(dev->dev_state));
Oren Weil91f01c62011-05-15 13:43:44 +0300307
308 /* Wake up all readings so they can be interrupted */
309 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
310 if (waitqueue_active(&cl_pos->rx_wait)) {
311 dev_dbg(&dev->pdev->dev, "Waking up client!\n");
312 wake_up_interruptible(&cl_pos->rx_wait);
313 }
314 }
315 /* remove all waiting requests */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200316 list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
317 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200318 mei_io_cb_free(cb_pos);
Oren Weil91f01c62011-05-15 13:43:44 +0300319 }
320}
321
322
323
324/**
325 * host_start_message - mei host sends start message.
326 *
327 * @dev: the device structure
328 *
329 * returns none.
330 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300331void mei_host_start_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300332{
333 struct mei_msg_hdr *mei_hdr;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200334 struct hbm_host_version_request *start_req;
335 const size_t len = sizeof(struct hbm_host_version_request);
336
337 mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len);
Oren Weil91f01c62011-05-15 13:43:44 +0300338
339 /* host start message */
Tomas Winkler5bd64712012-11-18 15:13:14 +0200340 start_req = (struct hbm_host_version_request *)&dev->wr_msg_buf[1];
341 memset(start_req, 0, len);
342 start_req->hbm_cmd = HOST_START_REQ_CMD;
343 start_req->host_version.major_version = HBM_MAJOR_VERSION;
344 start_req->host_version.minor_version = HBM_MINOR_VERSION;
Oren Weil91f01c62011-05-15 13:43:44 +0300345
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300346 dev->recvd_msg = false;
Tomas Winkler438763f2012-12-25 19:05:59 +0200347 if (mei_write_message(dev, mei_hdr, (unsigned char *)start_req)) {
Oren Weil91f01c62011-05-15 13:43:44 +0300348 dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300349 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300350 mei_reset(dev, 1);
351 }
352 dev->init_clients_state = MEI_START_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200353 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Oren Weil91f01c62011-05-15 13:43:44 +0300354 return ;
355}
356
357/**
358 * host_enum_clients_message - host sends enumeration client request message.
359 *
360 * @dev: the device structure
361 *
362 * returns none.
363 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300364void mei_host_enum_clients_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300365{
366 struct mei_msg_hdr *mei_hdr;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200367 struct hbm_host_enum_request *enum_req;
368 const size_t len = sizeof(struct hbm_host_enum_request);
Oren Weil91f01c62011-05-15 13:43:44 +0300369 /* enumerate clients */
Tomas Winkler5bd64712012-11-18 15:13:14 +0200370 mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len);
Oren Weil91f01c62011-05-15 13:43:44 +0300371
Tomas Winkler5bd64712012-11-18 15:13:14 +0200372 enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
373 memset(enum_req, 0, sizeof(struct hbm_host_enum_request));
374 enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
375
Tomas Winkler438763f2012-12-25 19:05:59 +0200376 if (mei_write_message(dev, mei_hdr, (unsigned char *)enum_req)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300377 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300378 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
379 mei_reset(dev, 1);
380 }
381 dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200382 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200383 return;
Oren Weil91f01c62011-05-15 13:43:44 +0300384}
385
386
387/**
388 * allocate_me_clients_storage - allocates storage for me clients
389 *
390 * @dev: the device structure
391 *
392 * returns none.
393 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300394void mei_allocate_me_clients_storage(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300395{
396 struct mei_me_client *clients;
397 int b;
398
399 /* count how many ME clients we have */
400 for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
Tomas Winklercf9673d2011-06-06 10:44:33 +0300401 dev->me_clients_num++;
Oren Weil91f01c62011-05-15 13:43:44 +0300402
Tomas Winklercf9673d2011-06-06 10:44:33 +0300403 if (dev->me_clients_num <= 0)
Oren Weil91f01c62011-05-15 13:43:44 +0300404 return ;
405
406
407 if (dev->me_clients != NULL) {
408 kfree(dev->me_clients);
409 dev->me_clients = NULL;
410 }
411 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
Tomas Winklercf9673d2011-06-06 10:44:33 +0300412 dev->me_clients_num * sizeof(struct mei_me_client));
Oren Weil91f01c62011-05-15 13:43:44 +0300413 /* allocate storage for ME clients representation */
Tomas Winklercf9673d2011-06-06 10:44:33 +0300414 clients = kcalloc(dev->me_clients_num,
Oren Weil91f01c62011-05-15 13:43:44 +0300415 sizeof(struct mei_me_client), GFP_KERNEL);
416 if (!clients) {
417 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300418 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300419 mei_reset(dev, 1);
420 return ;
421 }
422 dev->me_clients = clients;
423 return ;
424}
Samuel Ortizc1174c02012-11-18 15:13:20 +0200425
426void mei_host_client_init(struct work_struct *work)
427{
428 struct mei_device *dev = container_of(work,
429 struct mei_device, init_work);
430 struct mei_client_properties *client_props;
431 int i;
432
433 mutex_lock(&dev->device_lock);
434
435 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
436 dev->open_handle_count = 0;
437
438 /*
439 * Reserving the first three client IDs
440 * 0: Reserved for MEI Bus Message communications
441 * 1: Reserved for Watchdog
442 * 2: Reserved for AMTHI
443 */
444 bitmap_set(dev->host_clients_map, 0, 3);
445
446 for (i = 0; i < dev->me_clients_num; i++) {
447 client_props = &dev->me_clients[i].props;
448
449 if (!uuid_le_cmp(client_props->protocol_name, mei_amthi_guid))
450 mei_amthif_host_init(dev);
451 else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
452 mei_wd_host_init(dev);
453 }
454
455 dev->dev_state = MEI_DEV_ENABLED;
456
457 mutex_unlock(&dev->device_lock);
458}
459
460int mei_host_client_enumerate(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300461{
Tomas Winkler5bd64712012-11-18 15:13:14 +0200462
463 struct mei_msg_hdr *mei_hdr;
464 struct hbm_props_request *prop_req;
465 const size_t len = sizeof(struct hbm_props_request);
Samuel Ortizc1174c02012-11-18 15:13:20 +0200466 unsigned long next_client_index;
467 u8 client_num;
Tomas Winkler5bd64712012-11-18 15:13:14 +0200468
Oren Weil91f01c62011-05-15 13:43:44 +0300469
Samuel Ortizc1174c02012-11-18 15:13:20 +0200470 client_num = dev->me_client_presentation_num;
471
472 next_client_index = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX,
473 dev->me_client_index);
474
475 /* We got all client properties */
476 if (next_client_index == MEI_CLIENTS_MAX) {
477 schedule_work(&dev->init_work);
478
479 return 0;
480 }
481
482 dev->me_clients[client_num].client_id = next_client_index;
483 dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
484
485 mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len);
Tomas Winkler5bd64712012-11-18 15:13:14 +0200486 prop_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
487
Samuel Ortizc1174c02012-11-18 15:13:20 +0200488 memset(prop_req, 0, sizeof(struct hbm_props_request));
Oren Weil91f01c62011-05-15 13:43:44 +0300489
Oren Weil91f01c62011-05-15 13:43:44 +0300490
Samuel Ortizc1174c02012-11-18 15:13:20 +0200491 prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
492 prop_req->address = next_client_index;
Oren Weil91f01c62011-05-15 13:43:44 +0300493
Tomas Winkler438763f2012-12-25 19:05:59 +0200494 if (mei_write_message(dev, mei_hdr, (unsigned char *) prop_req)) {
Samuel Ortizc1174c02012-11-18 15:13:20 +0200495 dev->dev_state = MEI_DEV_RESETING;
496 dev_err(&dev->pdev->dev, "Properties request command failed\n");
497 mei_reset(dev, 1);
Oren Weil91f01c62011-05-15 13:43:44 +0300498
Samuel Ortizc1174c02012-11-18 15:13:20 +0200499 return -EIO;
Oren Weil91f01c62011-05-15 13:43:44 +0300500 }
501
Samuel Ortizc1174c02012-11-18 15:13:20 +0200502 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
503 dev->me_client_index = next_client_index;
504
Oren Weilabc51b62011-09-21 16:45:30 +0300505 return 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300506}
507
508/**
509 * mei_init_file_private - initializes private file structure.
510 *
511 * @priv: private file structure to be initialized
512 * @file: the file structure
513 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300514void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300515{
516 memset(priv, 0, sizeof(struct mei_cl));
517 init_waitqueue_head(&priv->wait);
518 init_waitqueue_head(&priv->rx_wait);
519 init_waitqueue_head(&priv->tx_wait);
520 INIT_LIST_HEAD(&priv->link);
521 priv->reading_state = MEI_IDLE;
522 priv->writing_state = MEI_IDLE;
523 priv->dev = dev;
524}
525
Tomas Winkler07b509b2012-07-23 14:05:39 +0300526int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid)
Oren Weil91f01c62011-05-15 13:43:44 +0300527{
Tomas Winkler07b509b2012-07-23 14:05:39 +0300528 int i, res = -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300529
Tomas Winklercf9673d2011-06-06 10:44:33 +0300530 for (i = 0; i < dev->me_clients_num; ++i)
Tomas Winkler07b509b2012-07-23 14:05:39 +0300531 if (uuid_le_cmp(*cuuid,
Oren Weil91f01c62011-05-15 13:43:44 +0300532 dev->me_clients[i].props.protocol_name) == 0) {
533 res = i;
534 break;
535 }
536
537 return res;
538}
539
540
541/**
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200542 * mei_me_cl_link - create link between host and me clinet and add
543 * me_cl to the list
Oren Weil91f01c62011-05-15 13:43:44 +0300544 *
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200545 * @dev: the device structure
546 * @cl: link between me and host client assocated with opened file descriptor
547 * @cuuid: uuid of ME client
548 * @client_id: id of the host client
549 *
550 * returns ME client index if ME client
551 * -EINVAL on incorrect values
552 * -ENONET if client not found
Oren Weil91f01c62011-05-15 13:43:44 +0300553 */
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200554int mei_me_cl_link(struct mei_device *dev, struct mei_cl *cl,
555 const uuid_le *cuuid, u8 host_cl_id)
Oren Weil91f01c62011-05-15 13:43:44 +0300556{
557 int i;
558
Tomas Winkler07b509b2012-07-23 14:05:39 +0300559 if (!dev || !cl || !cuuid)
560 return -EINVAL;
Oren Weil91f01c62011-05-15 13:43:44 +0300561
562 /* check for valid client id */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300563 i = mei_me_cl_by_uuid(dev, cuuid);
Oren Weil91f01c62011-05-15 13:43:44 +0300564 if (i >= 0) {
Tomas Winkler07b509b2012-07-23 14:05:39 +0300565 cl->me_client_id = dev->me_clients[i].client_id;
566 cl->state = MEI_FILE_CONNECTING;
567 cl->host_client_id = host_cl_id;
Oren Weil91f01c62011-05-15 13:43:44 +0300568
Tomas Winkler07b509b2012-07-23 14:05:39 +0300569 list_add_tail(&cl->link, &dev->file_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300570 return (u8)i;
571 }
572
Tomas Winkler07b509b2012-07-23 14:05:39 +0300573 return -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300574}
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200575/**
576 * mei_me_cl_unlink - remove me_cl from the list
577 *
578 * @dev: the device structure
579 * @host_client_id: host client id to be removed
580 */
581void mei_me_cl_unlink(struct mei_device *dev, struct mei_cl *cl)
582{
583 struct mei_cl *pos, *next;
584 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
585 if (cl->host_client_id == pos->host_client_id) {
586 dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
587 pos->host_client_id, pos->me_client_id);
588 list_del_init(&pos->link);
589 break;
590 }
591 }
592}
Oren Weil91f01c62011-05-15 13:43:44 +0300593
594/**
Oren Weil91f01c62011-05-15 13:43:44 +0300595 * mei_alloc_file_private - allocates a private file structure and sets it up.
596 * @file: the file structure
597 *
598 * returns The allocated file or NULL on failure
599 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300600struct mei_cl *mei_cl_allocate(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300601{
Tomas Winklerc95efb72011-05-25 17:28:21 +0300602 struct mei_cl *cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300603
Tomas Winklerc95efb72011-05-25 17:28:21 +0300604 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
605 if (!cl)
Oren Weil91f01c62011-05-15 13:43:44 +0300606 return NULL;
607
Tomas Winklerc95efb72011-05-25 17:28:21 +0300608 mei_cl_init(cl, dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300609
Tomas Winklerc95efb72011-05-25 17:28:21 +0300610 return cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300611}
612
613
614
615/**
616 * mei_disconnect_host_client - sends disconnect message to fw from host client.
617 *
618 * @dev: the device structure
619 * @cl: private data of the file object
620 *
621 * Locking: called under "dev->device_lock" lock
622 *
623 * returns 0 on success, <0 on failure.
624 */
625int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
626{
Oren Weil91f01c62011-05-15 13:43:44 +0300627 struct mei_cl_cb *cb;
Tomas Winkler3870c322012-11-01 21:17:14 +0200628 int rets, err;
Oren Weil91f01c62011-05-15 13:43:44 +0300629
630 if (!dev || !cl)
631 return -ENODEV;
632
633 if (cl->state != MEI_FILE_DISCONNECTING)
634 return 0;
635
Tomas Winkler664df382012-10-11 16:35:08 +0200636 cb = mei_io_cb_init(cl, NULL);
Oren Weil91f01c62011-05-15 13:43:44 +0300637 if (!cb)
638 return -ENOMEM;
639
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200640 cb->fop_type = MEI_FOP_CLOSE;
Oren Weil91f01c62011-05-15 13:43:44 +0300641 if (dev->mei_host_buffer_is_empty) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300642 dev->mei_host_buffer_is_empty = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300643 if (mei_disconnect(dev, cl)) {
Oren Weil91f01c62011-05-15 13:43:44 +0300644 rets = -ENODEV;
645 dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
646 goto free;
647 }
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200648 mdelay(10); /* Wait for hardware disconnection ready */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200649 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
Oren Weil91f01c62011-05-15 13:43:44 +0300650 } else {
651 dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200652 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
653
Oren Weil91f01c62011-05-15 13:43:44 +0300654 }
655 mutex_unlock(&dev->device_lock);
656
657 err = wait_event_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200658 MEI_FILE_DISCONNECTED == cl->state,
659 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300660
661 mutex_lock(&dev->device_lock);
662 if (MEI_FILE_DISCONNECTED == cl->state) {
663 rets = 0;
664 dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
665 } else {
666 rets = -ENODEV;
667 if (MEI_FILE_DISCONNECTED != cl->state)
668 dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
669
670 if (err)
671 dev_dbg(&dev->pdev->dev,
672 "wait failed disconnect err=%08x\n",
673 err);
674
675 dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
676 }
677
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300678 mei_io_list_flush(&dev->ctrl_rd_list, cl);
679 mei_io_list_flush(&dev->ctrl_wr_list, cl);
Oren Weil91f01c62011-05-15 13:43:44 +0300680free:
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200681 mei_io_cb_free(cb);
Oren Weil91f01c62011-05-15 13:43:44 +0300682 return rets;
683}
684