blob: 7e6d591fef4927db4b9a516e8e2991f7f2037b53 [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 Winkler3a5352f2011-12-04 16:16:27 +020058 if (pos->file_private) {
Tomas Winkler0288c7c2011-06-06 10:44:34 +030059 struct mei_cl *cl_tmp;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +020060 cl_tmp = (struct mei_cl *)pos->file_private;
Tomas Winkler0288c7c2011-06-06 10:44:34 +030061 if (mei_cl_cmp_id(cl, cl_tmp))
Tomas Winklerfb601ad2012-10-15 12:06:48 +020062 list_del(&pos->list);
Oren Weil91f01c62011-05-15 13:43:44 +030063 }
64 }
65}
Tomas Winkler0288c7c2011-06-06 10:44:34 +030066/**
67 * mei_cl_flush_queues - flushes queue lists belonging to cl.
68 *
69 * @dev: the device structure
70 * @cl: private data of the file object
71 */
72int mei_cl_flush_queues(struct mei_cl *cl)
73{
74 if (!cl || !cl->dev)
75 return -EINVAL;
76
77 dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n");
78 mei_io_list_flush(&cl->dev->read_list, cl);
79 mei_io_list_flush(&cl->dev->write_list, cl);
80 mei_io_list_flush(&cl->dev->write_waiting_list, cl);
81 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
82 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
Tomas Winklere773efc2012-11-11 17:37:58 +020083 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
84 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
Tomas Winkler0288c7c2011-06-06 10:44:34 +030085 return 0;
86}
87
88
Oren Weil91f01c62011-05-15 13:43:44 +030089
90/**
Oren Weil91f01c62011-05-15 13:43:44 +030091 * init_mei_device - allocates and initializes the mei device structure
92 *
93 * @pdev: The pci device structure
94 *
95 * returns The mei_device_device pointer on success, NULL on failure.
96 */
Tomas Winklerc95efb72011-05-25 17:28:21 +030097struct mei_device *mei_device_init(struct pci_dev *pdev)
Oren Weil91f01c62011-05-15 13:43:44 +030098{
Oren Weil91f01c62011-05-15 13:43:44 +030099 struct mei_device *dev;
100
101 dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
102 if (!dev)
103 return NULL;
104
105 /* setup our list array */
Oren Weil91f01c62011-05-15 13:43:44 +0300106 INIT_LIST_HEAD(&dev->file_list);
107 INIT_LIST_HEAD(&dev->wd_cl.link);
108 INIT_LIST_HEAD(&dev->iamthif_cl.link);
109 mutex_init(&dev->device_lock);
110 init_waitqueue_head(&dev->wait_recvd_msg);
111 init_waitqueue_head(&dev->wait_stop_wd);
Tomas Winklerb210d752012-08-07 00:03:56 +0300112 dev->dev_state = MEI_DEV_INITIALIZING;
Oren Weil91f01c62011-05-15 13:43:44 +0300113 dev->iamthif_state = MEI_IAMTHIF_IDLE;
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300114
115 mei_io_list_init(&dev->read_list);
116 mei_io_list_init(&dev->write_list);
117 mei_io_list_init(&dev->write_waiting_list);
118 mei_io_list_init(&dev->ctrl_wr_list);
119 mei_io_list_init(&dev->ctrl_rd_list);
Tomas Winklere773efc2012-11-11 17:37:58 +0200120 mei_io_list_init(&dev->amthif_cmd_list);
121 mei_io_list_init(&dev->amthif_rd_complete_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300122 dev->pdev = pdev;
123 return dev;
124}
125
126/**
127 * mei_hw_init - initializes host and fw to start work.
128 *
129 * @dev: the device structure
130 *
131 * returns 0 on success, <0 on failure.
132 */
133int mei_hw_init(struct mei_device *dev)
134{
135 int err = 0;
136 int ret;
137
138 mutex_lock(&dev->device_lock);
139
140 dev->host_hw_state = mei_hcsr_read(dev);
141 dev->me_hw_state = mei_mecsr_read(dev);
142 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
143 dev->host_hw_state, dev->me_hw_state);
144
145 /* acknowledge interrupt and stop interupts */
146 if ((dev->host_hw_state & H_IS) == H_IS)
147 mei_reg_write(dev, H_CSR, dev->host_hw_state);
148
Tomas Winkler24aadc82012-06-25 23:46:27 +0300149 /* Doesn't change in runtime */
150 dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
151
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300152 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300153 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
154
155 mei_reset(dev, 1);
156
157 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
158 dev->host_hw_state, dev->me_hw_state);
159
160 /* wait for ME to turn on ME_RDY */
161 if (!dev->recvd_msg) {
162 mutex_unlock(&dev->device_lock);
163 err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200164 dev->recvd_msg,
165 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300166 mutex_lock(&dev->device_lock);
167 }
168
Tomas Winklera534bb62011-06-13 16:39:31 +0300169 if (err <= 0 && !dev->recvd_msg) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300170 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300171 dev_dbg(&dev->pdev->dev,
172 "wait_event_interruptible_timeout failed"
173 "on wait for ME to turn on ME_RDY.\n");
174 ret = -ENODEV;
175 goto out;
176 }
177
178 if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
179 ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300180 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300181 dev_dbg(&dev->pdev->dev,
182 "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
183 dev->host_hw_state, dev->me_hw_state);
184
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300185 if (!(dev->host_hw_state & H_RDY))
Oren Weil91f01c62011-05-15 13:43:44 +0300186 dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
187
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300188 if (!(dev->me_hw_state & ME_RDY_HRA))
Oren Weil91f01c62011-05-15 13:43:44 +0300189 dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
190
Tomas Winklerd39a4642012-03-19 17:58:42 +0200191 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
Oren Weil91f01c62011-05-15 13:43:44 +0300192 ret = -ENODEV;
193 goto out;
194 }
195
196 if (dev->version.major_version != HBM_MAJOR_VERSION ||
197 dev->version.minor_version != HBM_MINOR_VERSION) {
198 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
199 ret = -ENODEV;
200 goto out;
201 }
202
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300203 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300204 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
205 dev->host_hw_state, dev->me_hw_state);
206 dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
207 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
208 dev_dbg(&dev->pdev->dev, "MEI start success.\n");
209 ret = 0;
210
211out:
212 mutex_unlock(&dev->device_lock);
213 return ret;
214}
215
216/**
217 * mei_hw_reset - resets fw via mei csr register.
218 *
219 * @dev: the device structure
220 * @interrupts_enabled: if interrupt should be enabled after reset.
221 */
222static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
223{
224 dev->host_hw_state |= (H_RST | H_IG);
225
226 if (interrupts_enabled)
227 mei_enable_interrupts(dev);
228 else
229 mei_disable_interrupts(dev);
230}
231
232/**
233 * mei_reset - resets host and fw.
234 *
235 * @dev: the device structure
236 * @interrupts_enabled: if interrupt should be enabled after reset.
237 */
238void mei_reset(struct mei_device *dev, int interrupts_enabled)
239{
240 struct mei_cl *cl_pos = NULL;
241 struct mei_cl *cl_next = NULL;
242 struct mei_cl_cb *cb_pos = NULL;
243 struct mei_cl_cb *cb_next = NULL;
244 bool unexpected;
245
Tomas Winklerb210d752012-08-07 00:03:56 +0300246 if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300247 dev->need_reset = true;
Oren Weil91f01c62011-05-15 13:43:44 +0300248 return;
249 }
250
Tomas Winklerb210d752012-08-07 00:03:56 +0300251 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
252 dev->dev_state != MEI_DEV_DISABLED &&
253 dev->dev_state != MEI_DEV_POWER_DOWN &&
254 dev->dev_state != MEI_DEV_POWER_UP);
Oren Weil91f01c62011-05-15 13:43:44 +0300255
256 dev->host_hw_state = mei_hcsr_read(dev);
257
258 dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
259 dev->host_hw_state);
260
261 mei_hw_reset(dev, interrupts_enabled);
262
263 dev->host_hw_state &= ~H_RST;
264 dev->host_hw_state |= H_IG;
265
266 mei_hcsr_set(dev);
267
268 dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
269 dev->host_hw_state);
270
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300271 dev->need_reset = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300272
Tomas Winklerb210d752012-08-07 00:03:56 +0300273 if (dev->dev_state != MEI_DEV_INITIALIZING) {
274 if (dev->dev_state != MEI_DEV_DISABLED &&
275 dev->dev_state != MEI_DEV_POWER_DOWN)
276 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300277
278 list_for_each_entry_safe(cl_pos,
279 cl_next, &dev->file_list, link) {
280 cl_pos->state = MEI_FILE_DISCONNECTED;
281 cl_pos->mei_flow_ctrl_creds = 0;
282 cl_pos->read_cb = NULL;
283 cl_pos->timer_count = 0;
284 }
285 /* remove entry if already in list */
286 dev_dbg(&dev->pdev->dev, "list del iamthif and wd file list.\n");
287 mei_remove_client_from_file_list(dev,
288 dev->wd_cl.host_client_id);
289
290 mei_remove_client_from_file_list(dev,
291 dev->iamthif_cl.host_client_id);
292
Tomas Winkler19838fb2012-11-01 21:17:15 +0200293 mei_amthif_reset_params(dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300294 dev->extra_write_index = 0;
295 }
296
Tomas Winklercf9673d2011-06-06 10:44:33 +0300297 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300298 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300299 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300300
301 /* update the state of the registers after reset */
302 dev->host_hw_state = mei_hcsr_read(dev);
303 dev->me_hw_state = mei_mecsr_read(dev);
304
305 dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
306 dev->host_hw_state, dev->me_hw_state);
307
308 if (unexpected)
Tomas Winklerb210d752012-08-07 00:03:56 +0300309 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
310 mei_dev_state_str(dev->dev_state));
Oren Weil91f01c62011-05-15 13:43:44 +0300311
312 /* Wake up all readings so they can be interrupted */
313 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
314 if (waitqueue_active(&cl_pos->rx_wait)) {
315 dev_dbg(&dev->pdev->dev, "Waking up client!\n");
316 wake_up_interruptible(&cl_pos->rx_wait);
317 }
318 }
319 /* remove all waiting requests */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200320 list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
321 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200322 mei_io_cb_free(cb_pos);
Oren Weil91f01c62011-05-15 13:43:44 +0300323 }
324}
325
326
327
328/**
329 * host_start_message - mei host sends start message.
330 *
331 * @dev: the device structure
332 *
333 * returns none.
334 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300335void mei_host_start_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300336{
337 struct mei_msg_hdr *mei_hdr;
338 struct hbm_host_version_request *host_start_req;
339
340 /* host start message */
341 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
342 mei_hdr->host_addr = 0;
343 mei_hdr->me_addr = 0;
344 mei_hdr->length = sizeof(struct hbm_host_version_request);
345 mei_hdr->msg_complete = 1;
346 mei_hdr->reserved = 0;
347
348 host_start_req =
349 (struct hbm_host_version_request *) &dev->wr_msg_buf[1];
350 memset(host_start_req, 0, sizeof(struct hbm_host_version_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200351 host_start_req->hbm_cmd = HOST_START_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300352 host_start_req->host_version.major_version = HBM_MAJOR_VERSION;
353 host_start_req->host_version.minor_version = HBM_MINOR_VERSION;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300354 dev->recvd_msg = false;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200355 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_start_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300356 mei_hdr->length)) {
357 dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300358 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300359 mei_reset(dev, 1);
360 }
361 dev->init_clients_state = MEI_START_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200362 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Oren Weil91f01c62011-05-15 13:43:44 +0300363 return ;
364}
365
366/**
367 * host_enum_clients_message - host sends enumeration client request message.
368 *
369 * @dev: the device structure
370 *
371 * returns none.
372 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300373void mei_host_enum_clients_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300374{
375 struct mei_msg_hdr *mei_hdr;
376 struct hbm_host_enum_request *host_enum_req;
377 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
378 /* enumerate clients */
379 mei_hdr->host_addr = 0;
380 mei_hdr->me_addr = 0;
381 mei_hdr->length = sizeof(struct hbm_host_enum_request);
382 mei_hdr->msg_complete = 1;
383 mei_hdr->reserved = 0;
384
385 host_enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
386 memset(host_enum_req, 0, sizeof(struct hbm_host_enum_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200387 host_enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200388 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_enum_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300389 mei_hdr->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300390 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300391 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
392 mei_reset(dev, 1);
393 }
394 dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200395 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200396 return;
Oren Weil91f01c62011-05-15 13:43:44 +0300397}
398
399
400/**
401 * allocate_me_clients_storage - allocates storage for me clients
402 *
403 * @dev: the device structure
404 *
405 * returns none.
406 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300407void mei_allocate_me_clients_storage(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300408{
409 struct mei_me_client *clients;
410 int b;
411
412 /* count how many ME clients we have */
413 for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
Tomas Winklercf9673d2011-06-06 10:44:33 +0300414 dev->me_clients_num++;
Oren Weil91f01c62011-05-15 13:43:44 +0300415
Tomas Winklercf9673d2011-06-06 10:44:33 +0300416 if (dev->me_clients_num <= 0)
Oren Weil91f01c62011-05-15 13:43:44 +0300417 return ;
418
419
420 if (dev->me_clients != NULL) {
421 kfree(dev->me_clients);
422 dev->me_clients = NULL;
423 }
424 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
Tomas Winklercf9673d2011-06-06 10:44:33 +0300425 dev->me_clients_num * sizeof(struct mei_me_client));
Oren Weil91f01c62011-05-15 13:43:44 +0300426 /* allocate storage for ME clients representation */
Tomas Winklercf9673d2011-06-06 10:44:33 +0300427 clients = kcalloc(dev->me_clients_num,
Oren Weil91f01c62011-05-15 13:43:44 +0300428 sizeof(struct mei_me_client), GFP_KERNEL);
429 if (!clients) {
430 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300431 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300432 mei_reset(dev, 1);
433 return ;
434 }
435 dev->me_clients = clients;
436 return ;
437}
438/**
439 * host_client_properties - reads properties for client
440 *
441 * @dev: the device structure
442 *
Oren Weilabc51b62011-09-21 16:45:30 +0300443 * returns:
444 * < 0 - Error.
445 * = 0 - no more clients.
446 * = 1 - still have clients to send properties request.
Oren Weil91f01c62011-05-15 13:43:44 +0300447 */
Oren Weilabc51b62011-09-21 16:45:30 +0300448int mei_host_client_properties(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300449{
450 struct mei_msg_hdr *mei_header;
451 struct hbm_props_request *host_cli_req;
452 int b;
453 u8 client_num = dev->me_client_presentation_num;
454
455 b = dev->me_client_index;
456 b = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, b);
457 if (b < MEI_CLIENTS_MAX) {
458 dev->me_clients[client_num].client_id = b;
459 dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
460 mei_header = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
461 mei_header->host_addr = 0;
462 mei_header->me_addr = 0;
463 mei_header->length = sizeof(struct hbm_props_request);
464 mei_header->msg_complete = 1;
465 mei_header->reserved = 0;
466
467 host_cli_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
468
469 memset(host_cli_req, 0, sizeof(struct hbm_props_request));
470
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200471 host_cli_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300472 host_cli_req->address = b;
473
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200474 if (mei_write_message(dev, mei_header,
Oren Weil91f01c62011-05-15 13:43:44 +0300475 (unsigned char *)host_cli_req,
476 mei_header->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300477 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300478 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
479 mei_reset(dev, 1);
Oren Weilabc51b62011-09-21 16:45:30 +0300480 return -EIO;
Oren Weil91f01c62011-05-15 13:43:44 +0300481 }
482
Tomas Winkler3870c322012-11-01 21:17:14 +0200483 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Oren Weil91f01c62011-05-15 13:43:44 +0300484 dev->me_client_index = b;
Oren Weilabc51b62011-09-21 16:45:30 +0300485 return 1;
Oren Weil91f01c62011-05-15 13:43:44 +0300486 }
487
Oren Weilabc51b62011-09-21 16:45:30 +0300488 return 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300489}
490
491/**
492 * mei_init_file_private - initializes private file structure.
493 *
494 * @priv: private file structure to be initialized
495 * @file: the file structure
496 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300497void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300498{
499 memset(priv, 0, sizeof(struct mei_cl));
500 init_waitqueue_head(&priv->wait);
501 init_waitqueue_head(&priv->rx_wait);
502 init_waitqueue_head(&priv->tx_wait);
503 INIT_LIST_HEAD(&priv->link);
504 priv->reading_state = MEI_IDLE;
505 priv->writing_state = MEI_IDLE;
506 priv->dev = dev;
507}
508
Tomas Winkler07b509b2012-07-23 14:05:39 +0300509int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid)
Oren Weil91f01c62011-05-15 13:43:44 +0300510{
Tomas Winkler07b509b2012-07-23 14:05:39 +0300511 int i, res = -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300512
Tomas Winklercf9673d2011-06-06 10:44:33 +0300513 for (i = 0; i < dev->me_clients_num; ++i)
Tomas Winkler07b509b2012-07-23 14:05:39 +0300514 if (uuid_le_cmp(*cuuid,
Oren Weil91f01c62011-05-15 13:43:44 +0300515 dev->me_clients[i].props.protocol_name) == 0) {
516 res = i;
517 break;
518 }
519
520 return res;
521}
522
523
524/**
Tomas Winkler07b509b2012-07-23 14:05:39 +0300525 * mei_me_cl_update_filext - searches for ME client guid
Oren Weil91f01c62011-05-15 13:43:44 +0300526 * sets client_id in mei_file_private if found
527 * @dev: the device structure
Tomas Winkler07b509b2012-07-23 14:05:39 +0300528 * @cl: private file structure to set client_id in
529 * @cuuid: searched uuid of ME client
Oren Weil91f01c62011-05-15 13:43:44 +0300530 * @client_id: id of host client to be set in file private structure
531 *
532 * returns ME client index
533 */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300534int mei_me_cl_update_filext(struct mei_device *dev, struct mei_cl *cl,
535 const uuid_le *cuuid, u8 host_cl_id)
Oren Weil91f01c62011-05-15 13:43:44 +0300536{
537 int i;
538
Tomas Winkler07b509b2012-07-23 14:05:39 +0300539 if (!dev || !cl || !cuuid)
540 return -EINVAL;
Oren Weil91f01c62011-05-15 13:43:44 +0300541
542 /* check for valid client id */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300543 i = mei_me_cl_by_uuid(dev, cuuid);
Oren Weil91f01c62011-05-15 13:43:44 +0300544 if (i >= 0) {
Tomas Winkler07b509b2012-07-23 14:05:39 +0300545 cl->me_client_id = dev->me_clients[i].client_id;
546 cl->state = MEI_FILE_CONNECTING;
547 cl->host_client_id = host_cl_id;
Oren Weil91f01c62011-05-15 13:43:44 +0300548
Tomas Winkler07b509b2012-07-23 14:05:39 +0300549 list_add_tail(&cl->link, &dev->file_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300550 return (u8)i;
551 }
552
Tomas Winkler07b509b2012-07-23 14:05:39 +0300553 return -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300554}
555
556/**
Oren Weil91f01c62011-05-15 13:43:44 +0300557 * mei_alloc_file_private - allocates a private file structure and sets it up.
558 * @file: the file structure
559 *
560 * returns The allocated file or NULL on failure
561 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300562struct mei_cl *mei_cl_allocate(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300563{
Tomas Winklerc95efb72011-05-25 17:28:21 +0300564 struct mei_cl *cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300565
Tomas Winklerc95efb72011-05-25 17:28:21 +0300566 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
567 if (!cl)
Oren Weil91f01c62011-05-15 13:43:44 +0300568 return NULL;
569
Tomas Winklerc95efb72011-05-25 17:28:21 +0300570 mei_cl_init(cl, dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300571
Tomas Winklerc95efb72011-05-25 17:28:21 +0300572 return cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300573}
574
575
576
577/**
578 * mei_disconnect_host_client - sends disconnect message to fw from host client.
579 *
580 * @dev: the device structure
581 * @cl: private data of the file object
582 *
583 * Locking: called under "dev->device_lock" lock
584 *
585 * returns 0 on success, <0 on failure.
586 */
587int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
588{
Oren Weil91f01c62011-05-15 13:43:44 +0300589 struct mei_cl_cb *cb;
Tomas Winkler3870c322012-11-01 21:17:14 +0200590 int rets, err;
Oren Weil91f01c62011-05-15 13:43:44 +0300591
592 if (!dev || !cl)
593 return -ENODEV;
594
595 if (cl->state != MEI_FILE_DISCONNECTING)
596 return 0;
597
Tomas Winkler664df382012-10-11 16:35:08 +0200598 cb = mei_io_cb_init(cl, NULL);
Oren Weil91f01c62011-05-15 13:43:44 +0300599 if (!cb)
600 return -ENOMEM;
601
Oren Weil91f01c62011-05-15 13:43:44 +0300602 cb->major_file_operations = MEI_CLOSE;
603 if (dev->mei_host_buffer_is_empty) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300604 dev->mei_host_buffer_is_empty = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300605 if (mei_disconnect(dev, cl)) {
Oren Weil91f01c62011-05-15 13:43:44 +0300606 rets = -ENODEV;
607 dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
608 goto free;
609 }
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200610 mdelay(10); /* Wait for hardware disconnection ready */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200611 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
Oren Weil91f01c62011-05-15 13:43:44 +0300612 } else {
613 dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200614 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
615
Oren Weil91f01c62011-05-15 13:43:44 +0300616 }
617 mutex_unlock(&dev->device_lock);
618
619 err = wait_event_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200620 MEI_FILE_DISCONNECTED == cl->state,
621 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300622
623 mutex_lock(&dev->device_lock);
624 if (MEI_FILE_DISCONNECTED == cl->state) {
625 rets = 0;
626 dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
627 } else {
628 rets = -ENODEV;
629 if (MEI_FILE_DISCONNECTED != cl->state)
630 dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
631
632 if (err)
633 dev_dbg(&dev->pdev->dev,
634 "wait failed disconnect err=%08x\n",
635 err);
636
637 dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
638 }
639
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300640 mei_io_list_flush(&dev->ctrl_rd_list, cl);
641 mei_io_list_flush(&dev->ctrl_wr_list, cl);
Oren Weil91f01c62011-05-15 13:43:44 +0300642free:
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200643 mei_io_cb_free(cb);
Oren Weil91f01c62011-05-15 13:43:44 +0300644 return rets;
645}
646
647/**
648 * mei_remove_client_from_file_list -
649 * removes file private data from device file list
650 *
651 * @dev: the device structure
652 * @host_client_id: host client id to be removed
653 */
654void mei_remove_client_from_file_list(struct mei_device *dev,
655 u8 host_client_id)
656{
657 struct mei_cl *cl_pos = NULL;
658 struct mei_cl *cl_next = NULL;
659 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
660 if (host_client_id == cl_pos->host_client_id) {
661 dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
662 cl_pos->host_client_id,
663 cl_pos->me_client_id);
664 list_del_init(&cl_pos->link);
665 break;
666 }
667 }
668}