blob: 85b6520f39f159201f9343724bfa85306d717a23 [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
22#include "mei_dev.h"
23#include "hw.h"
24#include "interface.h"
Tomas Winkler4f3afe12012-05-09 16:38:59 +030025#include <linux/mei.h>
Oren Weil91f01c62011-05-15 13:43:44 +030026
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
Oren Weil91f01c62011-05-15 13:43:44 +030046/**
Tomas Winkler0288c7c2011-06-06 10:44:34 +030047 * mei_io_list_flush - removes list entry belonging to cl.
Oren Weil91f01c62011-05-15 13:43:44 +030048 *
49 * @list: An instance of our list structure
50 * @cl: private data of the file object
51 */
Tomas Winklerfb601ad2012-10-15 12:06:48 +020052void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
Oren Weil91f01c62011-05-15 13:43:44 +030053{
Tomas Winkler3a5352f2011-12-04 16:16:27 +020054 struct mei_cl_cb *pos;
55 struct mei_cl_cb *next;
Oren Weil91f01c62011-05-15 13:43:44 +030056
Tomas Winklerfb601ad2012-10-15 12:06:48 +020057 list_for_each_entry_safe(pos, next, &list->list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +020058 if (pos->cl) {
59 if (mei_cl_cmp_id(cl, pos->cl))
Tomas Winklerfb601ad2012-10-15 12:06:48 +020060 list_del(&pos->list);
Oren Weil91f01c62011-05-15 13:43:44 +030061 }
62 }
63}
Tomas Winkler0288c7c2011-06-06 10:44:34 +030064/**
65 * mei_cl_flush_queues - flushes queue lists belonging to cl.
66 *
67 * @dev: the device structure
68 * @cl: private data of the file object
69 */
70int mei_cl_flush_queues(struct mei_cl *cl)
71{
72 if (!cl || !cl->dev)
73 return -EINVAL;
74
75 dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n");
76 mei_io_list_flush(&cl->dev->read_list, cl);
77 mei_io_list_flush(&cl->dev->write_list, cl);
78 mei_io_list_flush(&cl->dev->write_waiting_list, cl);
79 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
80 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
Tomas Winklere773efc2012-11-11 17:37:58 +020081 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
82 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
Tomas Winkler0288c7c2011-06-06 10:44:34 +030083 return 0;
84}
85
86
Oren Weil91f01c62011-05-15 13:43:44 +030087
88/**
Oren Weil91f01c62011-05-15 13:43:44 +030089 * init_mei_device - allocates and initializes the mei device structure
90 *
91 * @pdev: The pci device structure
92 *
93 * returns The mei_device_device pointer on success, NULL on failure.
94 */
Tomas Winklerc95efb72011-05-25 17:28:21 +030095struct mei_device *mei_device_init(struct pci_dev *pdev)
Oren Weil91f01c62011-05-15 13:43:44 +030096{
Oren Weil91f01c62011-05-15 13:43:44 +030097 struct mei_device *dev;
98
99 dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
100 if (!dev)
101 return NULL;
102
103 /* setup our list array */
Oren Weil91f01c62011-05-15 13:43:44 +0300104 INIT_LIST_HEAD(&dev->file_list);
105 INIT_LIST_HEAD(&dev->wd_cl.link);
106 INIT_LIST_HEAD(&dev->iamthif_cl.link);
107 mutex_init(&dev->device_lock);
108 init_waitqueue_head(&dev->wait_recvd_msg);
109 init_waitqueue_head(&dev->wait_stop_wd);
Tomas Winklerb210d752012-08-07 00:03:56 +0300110 dev->dev_state = MEI_DEV_INITIALIZING;
Oren Weil91f01c62011-05-15 13:43:44 +0300111 dev->iamthif_state = MEI_IAMTHIF_IDLE;
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300112
113 mei_io_list_init(&dev->read_list);
114 mei_io_list_init(&dev->write_list);
115 mei_io_list_init(&dev->write_waiting_list);
116 mei_io_list_init(&dev->ctrl_wr_list);
117 mei_io_list_init(&dev->ctrl_rd_list);
Tomas Winklere773efc2012-11-11 17:37:58 +0200118 mei_io_list_init(&dev->amthif_cmd_list);
119 mei_io_list_init(&dev->amthif_rd_complete_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300120 dev->pdev = pdev;
121 return dev;
122}
123
124/**
125 * mei_hw_init - initializes host and fw to start work.
126 *
127 * @dev: the device structure
128 *
129 * returns 0 on success, <0 on failure.
130 */
131int mei_hw_init(struct mei_device *dev)
132{
133 int err = 0;
134 int ret;
135
136 mutex_lock(&dev->device_lock);
137
138 dev->host_hw_state = mei_hcsr_read(dev);
139 dev->me_hw_state = mei_mecsr_read(dev);
140 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
141 dev->host_hw_state, dev->me_hw_state);
142
143 /* acknowledge interrupt and stop interupts */
144 if ((dev->host_hw_state & H_IS) == H_IS)
145 mei_reg_write(dev, H_CSR, dev->host_hw_state);
146
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 */
284 dev_dbg(&dev->pdev->dev, "list del iamthif and wd file list.\n");
285 mei_remove_client_from_file_list(dev,
286 dev->wd_cl.host_client_id);
287
288 mei_remove_client_from_file_list(dev,
289 dev->iamthif_cl.host_client_id);
290
Tomas Winkler19838fb2012-11-01 21:17:15 +0200291 mei_amthif_reset_params(dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300292 dev->extra_write_index = 0;
293 }
294
Tomas Winklercf9673d2011-06-06 10:44:33 +0300295 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300296 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300297 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300298
299 /* update the state of the registers after reset */
300 dev->host_hw_state = mei_hcsr_read(dev);
301 dev->me_hw_state = mei_mecsr_read(dev);
302
303 dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
304 dev->host_hw_state, dev->me_hw_state);
305
306 if (unexpected)
Tomas Winklerb210d752012-08-07 00:03:56 +0300307 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
308 mei_dev_state_str(dev->dev_state));
Oren Weil91f01c62011-05-15 13:43:44 +0300309
310 /* Wake up all readings so they can be interrupted */
311 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
312 if (waitqueue_active(&cl_pos->rx_wait)) {
313 dev_dbg(&dev->pdev->dev, "Waking up client!\n");
314 wake_up_interruptible(&cl_pos->rx_wait);
315 }
316 }
317 /* remove all waiting requests */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200318 list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
319 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200320 mei_io_cb_free(cb_pos);
Oren Weil91f01c62011-05-15 13:43:44 +0300321 }
322}
323
324
325
326/**
327 * host_start_message - mei host sends start message.
328 *
329 * @dev: the device structure
330 *
331 * returns none.
332 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300333void mei_host_start_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300334{
335 struct mei_msg_hdr *mei_hdr;
336 struct hbm_host_version_request *host_start_req;
337
338 /* host start message */
339 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
340 mei_hdr->host_addr = 0;
341 mei_hdr->me_addr = 0;
342 mei_hdr->length = sizeof(struct hbm_host_version_request);
343 mei_hdr->msg_complete = 1;
344 mei_hdr->reserved = 0;
345
346 host_start_req =
347 (struct hbm_host_version_request *) &dev->wr_msg_buf[1];
348 memset(host_start_req, 0, sizeof(struct hbm_host_version_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200349 host_start_req->hbm_cmd = HOST_START_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300350 host_start_req->host_version.major_version = HBM_MAJOR_VERSION;
351 host_start_req->host_version.minor_version = HBM_MINOR_VERSION;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300352 dev->recvd_msg = false;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200353 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_start_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300354 mei_hdr->length)) {
355 dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300356 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300357 mei_reset(dev, 1);
358 }
359 dev->init_clients_state = MEI_START_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200360 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Oren Weil91f01c62011-05-15 13:43:44 +0300361 return ;
362}
363
364/**
365 * host_enum_clients_message - host sends enumeration client request message.
366 *
367 * @dev: the device structure
368 *
369 * returns none.
370 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300371void mei_host_enum_clients_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300372{
373 struct mei_msg_hdr *mei_hdr;
374 struct hbm_host_enum_request *host_enum_req;
375 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
376 /* enumerate clients */
377 mei_hdr->host_addr = 0;
378 mei_hdr->me_addr = 0;
379 mei_hdr->length = sizeof(struct hbm_host_enum_request);
380 mei_hdr->msg_complete = 1;
381 mei_hdr->reserved = 0;
382
383 host_enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
384 memset(host_enum_req, 0, sizeof(struct hbm_host_enum_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200385 host_enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200386 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_enum_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300387 mei_hdr->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300388 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300389 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
390 mei_reset(dev, 1);
391 }
392 dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200393 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200394 return;
Oren Weil91f01c62011-05-15 13:43:44 +0300395}
396
397
398/**
399 * allocate_me_clients_storage - allocates storage for me clients
400 *
401 * @dev: the device structure
402 *
403 * returns none.
404 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300405void mei_allocate_me_clients_storage(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300406{
407 struct mei_me_client *clients;
408 int b;
409
410 /* count how many ME clients we have */
411 for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
Tomas Winklercf9673d2011-06-06 10:44:33 +0300412 dev->me_clients_num++;
Oren Weil91f01c62011-05-15 13:43:44 +0300413
Tomas Winklercf9673d2011-06-06 10:44:33 +0300414 if (dev->me_clients_num <= 0)
Oren Weil91f01c62011-05-15 13:43:44 +0300415 return ;
416
417
418 if (dev->me_clients != NULL) {
419 kfree(dev->me_clients);
420 dev->me_clients = NULL;
421 }
422 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
Tomas Winklercf9673d2011-06-06 10:44:33 +0300423 dev->me_clients_num * sizeof(struct mei_me_client));
Oren Weil91f01c62011-05-15 13:43:44 +0300424 /* allocate storage for ME clients representation */
Tomas Winklercf9673d2011-06-06 10:44:33 +0300425 clients = kcalloc(dev->me_clients_num,
Oren Weil91f01c62011-05-15 13:43:44 +0300426 sizeof(struct mei_me_client), GFP_KERNEL);
427 if (!clients) {
428 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300429 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300430 mei_reset(dev, 1);
431 return ;
432 }
433 dev->me_clients = clients;
434 return ;
435}
436/**
437 * host_client_properties - reads properties for client
438 *
439 * @dev: the device structure
440 *
Oren Weilabc51b62011-09-21 16:45:30 +0300441 * returns:
442 * < 0 - Error.
443 * = 0 - no more clients.
444 * = 1 - still have clients to send properties request.
Oren Weil91f01c62011-05-15 13:43:44 +0300445 */
Oren Weilabc51b62011-09-21 16:45:30 +0300446int mei_host_client_properties(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300447{
448 struct mei_msg_hdr *mei_header;
449 struct hbm_props_request *host_cli_req;
450 int b;
451 u8 client_num = dev->me_client_presentation_num;
452
453 b = dev->me_client_index;
454 b = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, b);
455 if (b < MEI_CLIENTS_MAX) {
456 dev->me_clients[client_num].client_id = b;
457 dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
458 mei_header = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
459 mei_header->host_addr = 0;
460 mei_header->me_addr = 0;
461 mei_header->length = sizeof(struct hbm_props_request);
462 mei_header->msg_complete = 1;
463 mei_header->reserved = 0;
464
465 host_cli_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
466
467 memset(host_cli_req, 0, sizeof(struct hbm_props_request));
468
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200469 host_cli_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300470 host_cli_req->address = b;
471
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200472 if (mei_write_message(dev, mei_header,
Oren Weil91f01c62011-05-15 13:43:44 +0300473 (unsigned char *)host_cli_req,
474 mei_header->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300475 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300476 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
477 mei_reset(dev, 1);
Oren Weilabc51b62011-09-21 16:45:30 +0300478 return -EIO;
Oren Weil91f01c62011-05-15 13:43:44 +0300479 }
480
Tomas Winkler3870c322012-11-01 21:17:14 +0200481 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Oren Weil91f01c62011-05-15 13:43:44 +0300482 dev->me_client_index = b;
Oren Weilabc51b62011-09-21 16:45:30 +0300483 return 1;
Oren Weil91f01c62011-05-15 13:43:44 +0300484 }
485
Oren Weilabc51b62011-09-21 16:45:30 +0300486 return 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300487}
488
489/**
490 * mei_init_file_private - initializes private file structure.
491 *
492 * @priv: private file structure to be initialized
493 * @file: the file structure
494 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300495void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300496{
497 memset(priv, 0, sizeof(struct mei_cl));
498 init_waitqueue_head(&priv->wait);
499 init_waitqueue_head(&priv->rx_wait);
500 init_waitqueue_head(&priv->tx_wait);
501 INIT_LIST_HEAD(&priv->link);
502 priv->reading_state = MEI_IDLE;
503 priv->writing_state = MEI_IDLE;
504 priv->dev = dev;
505}
506
Tomas Winkler07b509b2012-07-23 14:05:39 +0300507int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid)
Oren Weil91f01c62011-05-15 13:43:44 +0300508{
Tomas Winkler07b509b2012-07-23 14:05:39 +0300509 int i, res = -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300510
Tomas Winklercf9673d2011-06-06 10:44:33 +0300511 for (i = 0; i < dev->me_clients_num; ++i)
Tomas Winkler07b509b2012-07-23 14:05:39 +0300512 if (uuid_le_cmp(*cuuid,
Oren Weil91f01c62011-05-15 13:43:44 +0300513 dev->me_clients[i].props.protocol_name) == 0) {
514 res = i;
515 break;
516 }
517
518 return res;
519}
520
521
522/**
Tomas Winkler07b509b2012-07-23 14:05:39 +0300523 * mei_me_cl_update_filext - searches for ME client guid
Oren Weil91f01c62011-05-15 13:43:44 +0300524 * sets client_id in mei_file_private if found
525 * @dev: the device structure
Tomas Winkler07b509b2012-07-23 14:05:39 +0300526 * @cl: private file structure to set client_id in
527 * @cuuid: searched uuid of ME client
Oren Weil91f01c62011-05-15 13:43:44 +0300528 * @client_id: id of host client to be set in file private structure
529 *
530 * returns ME client index
531 */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300532int mei_me_cl_update_filext(struct mei_device *dev, struct mei_cl *cl,
533 const uuid_le *cuuid, u8 host_cl_id)
Oren Weil91f01c62011-05-15 13:43:44 +0300534{
535 int i;
536
Tomas Winkler07b509b2012-07-23 14:05:39 +0300537 if (!dev || !cl || !cuuid)
538 return -EINVAL;
Oren Weil91f01c62011-05-15 13:43:44 +0300539
540 /* check for valid client id */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300541 i = mei_me_cl_by_uuid(dev, cuuid);
Oren Weil91f01c62011-05-15 13:43:44 +0300542 if (i >= 0) {
Tomas Winkler07b509b2012-07-23 14:05:39 +0300543 cl->me_client_id = dev->me_clients[i].client_id;
544 cl->state = MEI_FILE_CONNECTING;
545 cl->host_client_id = host_cl_id;
Oren Weil91f01c62011-05-15 13:43:44 +0300546
Tomas Winkler07b509b2012-07-23 14:05:39 +0300547 list_add_tail(&cl->link, &dev->file_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300548 return (u8)i;
549 }
550
Tomas Winkler07b509b2012-07-23 14:05:39 +0300551 return -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300552}
553
554/**
Oren Weil91f01c62011-05-15 13:43:44 +0300555 * mei_alloc_file_private - allocates a private file structure and sets it up.
556 * @file: the file structure
557 *
558 * returns The allocated file or NULL on failure
559 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300560struct mei_cl *mei_cl_allocate(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300561{
Tomas Winklerc95efb72011-05-25 17:28:21 +0300562 struct mei_cl *cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300563
Tomas Winklerc95efb72011-05-25 17:28:21 +0300564 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
565 if (!cl)
Oren Weil91f01c62011-05-15 13:43:44 +0300566 return NULL;
567
Tomas Winklerc95efb72011-05-25 17:28:21 +0300568 mei_cl_init(cl, dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300569
Tomas Winklerc95efb72011-05-25 17:28:21 +0300570 return cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300571}
572
573
574
575/**
576 * mei_disconnect_host_client - sends disconnect message to fw from host client.
577 *
578 * @dev: the device structure
579 * @cl: private data of the file object
580 *
581 * Locking: called under "dev->device_lock" lock
582 *
583 * returns 0 on success, <0 on failure.
584 */
585int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
586{
Oren Weil91f01c62011-05-15 13:43:44 +0300587 struct mei_cl_cb *cb;
Tomas Winkler3870c322012-11-01 21:17:14 +0200588 int rets, err;
Oren Weil91f01c62011-05-15 13:43:44 +0300589
590 if (!dev || !cl)
591 return -ENODEV;
592
593 if (cl->state != MEI_FILE_DISCONNECTING)
594 return 0;
595
Tomas Winkler664df382012-10-11 16:35:08 +0200596 cb = mei_io_cb_init(cl, NULL);
Oren Weil91f01c62011-05-15 13:43:44 +0300597 if (!cb)
598 return -ENOMEM;
599
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200600 cb->fop_type = MEI_FOP_CLOSE;
Oren Weil91f01c62011-05-15 13:43:44 +0300601 if (dev->mei_host_buffer_is_empty) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300602 dev->mei_host_buffer_is_empty = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300603 if (mei_disconnect(dev, cl)) {
Oren Weil91f01c62011-05-15 13:43:44 +0300604 rets = -ENODEV;
605 dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
606 goto free;
607 }
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200608 mdelay(10); /* Wait for hardware disconnection ready */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200609 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
Oren Weil91f01c62011-05-15 13:43:44 +0300610 } else {
611 dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200612 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
613
Oren Weil91f01c62011-05-15 13:43:44 +0300614 }
615 mutex_unlock(&dev->device_lock);
616
617 err = wait_event_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200618 MEI_FILE_DISCONNECTED == cl->state,
619 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300620
621 mutex_lock(&dev->device_lock);
622 if (MEI_FILE_DISCONNECTED == cl->state) {
623 rets = 0;
624 dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
625 } else {
626 rets = -ENODEV;
627 if (MEI_FILE_DISCONNECTED != cl->state)
628 dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
629
630 if (err)
631 dev_dbg(&dev->pdev->dev,
632 "wait failed disconnect err=%08x\n",
633 err);
634
635 dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
636 }
637
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300638 mei_io_list_flush(&dev->ctrl_rd_list, cl);
639 mei_io_list_flush(&dev->ctrl_wr_list, cl);
Oren Weil91f01c62011-05-15 13:43:44 +0300640free:
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200641 mei_io_cb_free(cb);
Oren Weil91f01c62011-05-15 13:43:44 +0300642 return rets;
643}
644
645/**
646 * mei_remove_client_from_file_list -
647 * removes file private data from device file list
648 *
649 * @dev: the device structure
650 * @host_client_id: host client id to be removed
651 */
652void mei_remove_client_from_file_list(struct mei_device *dev,
653 u8 host_client_id)
654{
655 struct mei_cl *cl_pos = NULL;
656 struct mei_cl *cl_next = NULL;
657 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
658 if (host_client_id == cl_pos->host_client_id) {
659 dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
660 cl_pos->host_client_id,
661 cl_pos->me_client_id);
662 list_del_init(&cl_pos->link);
663 break;
664 }
665 }
666}