blob: 4a8eb920f7fb1dcd605d4aad741fb8cda7426bef [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);
83 mei_io_list_flush(&cl->dev->amthi_cmd_list, cl);
84 mei_io_list_flush(&cl->dev->amthi_read_complete_list, cl);
85 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;
Oren Weil9ce178e2011-09-07 09:03:09 +0300114 dev->wd_interface_reg = false;
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300115
116
117 mei_io_list_init(&dev->read_list);
118 mei_io_list_init(&dev->write_list);
119 mei_io_list_init(&dev->write_waiting_list);
120 mei_io_list_init(&dev->ctrl_wr_list);
121 mei_io_list_init(&dev->ctrl_rd_list);
122 mei_io_list_init(&dev->amthi_cmd_list);
123 mei_io_list_init(&dev->amthi_read_complete_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300124 dev->pdev = pdev;
125 return dev;
126}
127
128/**
129 * mei_hw_init - initializes host and fw to start work.
130 *
131 * @dev: the device structure
132 *
133 * returns 0 on success, <0 on failure.
134 */
135int mei_hw_init(struct mei_device *dev)
136{
137 int err = 0;
138 int ret;
139
140 mutex_lock(&dev->device_lock);
141
142 dev->host_hw_state = mei_hcsr_read(dev);
143 dev->me_hw_state = mei_mecsr_read(dev);
144 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
145 dev->host_hw_state, dev->me_hw_state);
146
147 /* acknowledge interrupt and stop interupts */
148 if ((dev->host_hw_state & H_IS) == H_IS)
149 mei_reg_write(dev, H_CSR, dev->host_hw_state);
150
Tomas Winkler24aadc82012-06-25 23:46:27 +0300151 /* Doesn't change in runtime */
152 dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
153
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300154 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300155 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
156
157 mei_reset(dev, 1);
158
159 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
160 dev->host_hw_state, dev->me_hw_state);
161
162 /* wait for ME to turn on ME_RDY */
163 if (!dev->recvd_msg) {
164 mutex_unlock(&dev->device_lock);
165 err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200166 dev->recvd_msg,
167 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300168 mutex_lock(&dev->device_lock);
169 }
170
Tomas Winklera534bb62011-06-13 16:39:31 +0300171 if (err <= 0 && !dev->recvd_msg) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300172 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300173 dev_dbg(&dev->pdev->dev,
174 "wait_event_interruptible_timeout failed"
175 "on wait for ME to turn on ME_RDY.\n");
176 ret = -ENODEV;
177 goto out;
178 }
179
180 if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
181 ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300182 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300183 dev_dbg(&dev->pdev->dev,
184 "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
185 dev->host_hw_state, dev->me_hw_state);
186
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300187 if (!(dev->host_hw_state & H_RDY))
Oren Weil91f01c62011-05-15 13:43:44 +0300188 dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
189
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300190 if (!(dev->me_hw_state & ME_RDY_HRA))
Oren Weil91f01c62011-05-15 13:43:44 +0300191 dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
192
Tomas Winklerd39a4642012-03-19 17:58:42 +0200193 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
Oren Weil91f01c62011-05-15 13:43:44 +0300194 ret = -ENODEV;
195 goto out;
196 }
197
198 if (dev->version.major_version != HBM_MAJOR_VERSION ||
199 dev->version.minor_version != HBM_MINOR_VERSION) {
200 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
201 ret = -ENODEV;
202 goto out;
203 }
204
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300205 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300206 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
207 dev->host_hw_state, dev->me_hw_state);
208 dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
209 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
210 dev_dbg(&dev->pdev->dev, "MEI start success.\n");
211 ret = 0;
212
213out:
214 mutex_unlock(&dev->device_lock);
215 return ret;
216}
217
218/**
219 * mei_hw_reset - resets fw via mei csr register.
220 *
221 * @dev: the device structure
222 * @interrupts_enabled: if interrupt should be enabled after reset.
223 */
224static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
225{
226 dev->host_hw_state |= (H_RST | H_IG);
227
228 if (interrupts_enabled)
229 mei_enable_interrupts(dev);
230 else
231 mei_disable_interrupts(dev);
232}
233
234/**
235 * mei_reset - resets host and fw.
236 *
237 * @dev: the device structure
238 * @interrupts_enabled: if interrupt should be enabled after reset.
239 */
240void mei_reset(struct mei_device *dev, int interrupts_enabled)
241{
242 struct mei_cl *cl_pos = NULL;
243 struct mei_cl *cl_next = NULL;
244 struct mei_cl_cb *cb_pos = NULL;
245 struct mei_cl_cb *cb_next = NULL;
246 bool unexpected;
247
Tomas Winklerb210d752012-08-07 00:03:56 +0300248 if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300249 dev->need_reset = true;
Oren Weil91f01c62011-05-15 13:43:44 +0300250 return;
251 }
252
Tomas Winklerb210d752012-08-07 00:03:56 +0300253 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
254 dev->dev_state != MEI_DEV_DISABLED &&
255 dev->dev_state != MEI_DEV_POWER_DOWN &&
256 dev->dev_state != MEI_DEV_POWER_UP);
Oren Weil91f01c62011-05-15 13:43:44 +0300257
258 dev->host_hw_state = mei_hcsr_read(dev);
259
260 dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
261 dev->host_hw_state);
262
263 mei_hw_reset(dev, interrupts_enabled);
264
265 dev->host_hw_state &= ~H_RST;
266 dev->host_hw_state |= H_IG;
267
268 mei_hcsr_set(dev);
269
270 dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
271 dev->host_hw_state);
272
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300273 dev->need_reset = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300274
Tomas Winklerb210d752012-08-07 00:03:56 +0300275 if (dev->dev_state != MEI_DEV_INITIALIZING) {
276 if (dev->dev_state != MEI_DEV_DISABLED &&
277 dev->dev_state != MEI_DEV_POWER_DOWN)
278 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300279
280 list_for_each_entry_safe(cl_pos,
281 cl_next, &dev->file_list, link) {
282 cl_pos->state = MEI_FILE_DISCONNECTED;
283 cl_pos->mei_flow_ctrl_creds = 0;
284 cl_pos->read_cb = NULL;
285 cl_pos->timer_count = 0;
286 }
287 /* remove entry if already in list */
288 dev_dbg(&dev->pdev->dev, "list del iamthif and wd file list.\n");
289 mei_remove_client_from_file_list(dev,
290 dev->wd_cl.host_client_id);
291
292 mei_remove_client_from_file_list(dev,
293 dev->iamthif_cl.host_client_id);
294
Tomas Winkler19838fb2012-11-01 21:17:15 +0200295 mei_amthif_reset_params(dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300296 dev->extra_write_index = 0;
297 }
298
Tomas Winklercf9673d2011-06-06 10:44:33 +0300299 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300300 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300301 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300302
303 /* update the state of the registers after reset */
304 dev->host_hw_state = mei_hcsr_read(dev);
305 dev->me_hw_state = mei_mecsr_read(dev);
306
307 dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
308 dev->host_hw_state, dev->me_hw_state);
309
310 if (unexpected)
Tomas Winklerb210d752012-08-07 00:03:56 +0300311 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
312 mei_dev_state_str(dev->dev_state));
Oren Weil91f01c62011-05-15 13:43:44 +0300313
314 /* Wake up all readings so they can be interrupted */
315 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
316 if (waitqueue_active(&cl_pos->rx_wait)) {
317 dev_dbg(&dev->pdev->dev, "Waking up client!\n");
318 wake_up_interruptible(&cl_pos->rx_wait);
319 }
320 }
321 /* remove all waiting requests */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200322 list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
323 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200324 mei_io_cb_free(cb_pos);
Oren Weil91f01c62011-05-15 13:43:44 +0300325 }
326}
327
328
329
330/**
331 * host_start_message - mei host sends start message.
332 *
333 * @dev: the device structure
334 *
335 * returns none.
336 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300337void mei_host_start_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300338{
339 struct mei_msg_hdr *mei_hdr;
340 struct hbm_host_version_request *host_start_req;
341
342 /* host start message */
343 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
344 mei_hdr->host_addr = 0;
345 mei_hdr->me_addr = 0;
346 mei_hdr->length = sizeof(struct hbm_host_version_request);
347 mei_hdr->msg_complete = 1;
348 mei_hdr->reserved = 0;
349
350 host_start_req =
351 (struct hbm_host_version_request *) &dev->wr_msg_buf[1];
352 memset(host_start_req, 0, sizeof(struct hbm_host_version_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200353 host_start_req->hbm_cmd = HOST_START_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300354 host_start_req->host_version.major_version = HBM_MAJOR_VERSION;
355 host_start_req->host_version.minor_version = HBM_MINOR_VERSION;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300356 dev->recvd_msg = false;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200357 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_start_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300358 mei_hdr->length)) {
359 dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300360 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300361 mei_reset(dev, 1);
362 }
363 dev->init_clients_state = MEI_START_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200364 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Oren Weil91f01c62011-05-15 13:43:44 +0300365 return ;
366}
367
368/**
369 * host_enum_clients_message - host sends enumeration client request message.
370 *
371 * @dev: the device structure
372 *
373 * returns none.
374 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300375void mei_host_enum_clients_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300376{
377 struct mei_msg_hdr *mei_hdr;
378 struct hbm_host_enum_request *host_enum_req;
379 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
380 /* enumerate clients */
381 mei_hdr->host_addr = 0;
382 mei_hdr->me_addr = 0;
383 mei_hdr->length = sizeof(struct hbm_host_enum_request);
384 mei_hdr->msg_complete = 1;
385 mei_hdr->reserved = 0;
386
387 host_enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
388 memset(host_enum_req, 0, sizeof(struct hbm_host_enum_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200389 host_enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200390 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_enum_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300391 mei_hdr->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300392 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300393 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
394 mei_reset(dev, 1);
395 }
396 dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200397 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200398 return;
Oren Weil91f01c62011-05-15 13:43:44 +0300399}
400
401
402/**
403 * allocate_me_clients_storage - allocates storage for me clients
404 *
405 * @dev: the device structure
406 *
407 * returns none.
408 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300409void mei_allocate_me_clients_storage(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300410{
411 struct mei_me_client *clients;
412 int b;
413
414 /* count how many ME clients we have */
415 for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
Tomas Winklercf9673d2011-06-06 10:44:33 +0300416 dev->me_clients_num++;
Oren Weil91f01c62011-05-15 13:43:44 +0300417
Tomas Winklercf9673d2011-06-06 10:44:33 +0300418 if (dev->me_clients_num <= 0)
Oren Weil91f01c62011-05-15 13:43:44 +0300419 return ;
420
421
422 if (dev->me_clients != NULL) {
423 kfree(dev->me_clients);
424 dev->me_clients = NULL;
425 }
426 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
Tomas Winklercf9673d2011-06-06 10:44:33 +0300427 dev->me_clients_num * sizeof(struct mei_me_client));
Oren Weil91f01c62011-05-15 13:43:44 +0300428 /* allocate storage for ME clients representation */
Tomas Winklercf9673d2011-06-06 10:44:33 +0300429 clients = kcalloc(dev->me_clients_num,
Oren Weil91f01c62011-05-15 13:43:44 +0300430 sizeof(struct mei_me_client), GFP_KERNEL);
431 if (!clients) {
432 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300433 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300434 mei_reset(dev, 1);
435 return ;
436 }
437 dev->me_clients = clients;
438 return ;
439}
440/**
441 * host_client_properties - reads properties for client
442 *
443 * @dev: the device structure
444 *
Oren Weilabc51b62011-09-21 16:45:30 +0300445 * returns:
446 * < 0 - Error.
447 * = 0 - no more clients.
448 * = 1 - still have clients to send properties request.
Oren Weil91f01c62011-05-15 13:43:44 +0300449 */
Oren Weilabc51b62011-09-21 16:45:30 +0300450int mei_host_client_properties(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300451{
452 struct mei_msg_hdr *mei_header;
453 struct hbm_props_request *host_cli_req;
454 int b;
455 u8 client_num = dev->me_client_presentation_num;
456
457 b = dev->me_client_index;
458 b = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, b);
459 if (b < MEI_CLIENTS_MAX) {
460 dev->me_clients[client_num].client_id = b;
461 dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
462 mei_header = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
463 mei_header->host_addr = 0;
464 mei_header->me_addr = 0;
465 mei_header->length = sizeof(struct hbm_props_request);
466 mei_header->msg_complete = 1;
467 mei_header->reserved = 0;
468
469 host_cli_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
470
471 memset(host_cli_req, 0, sizeof(struct hbm_props_request));
472
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200473 host_cli_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300474 host_cli_req->address = b;
475
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200476 if (mei_write_message(dev, mei_header,
Oren Weil91f01c62011-05-15 13:43:44 +0300477 (unsigned char *)host_cli_req,
478 mei_header->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300479 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300480 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
481 mei_reset(dev, 1);
Oren Weilabc51b62011-09-21 16:45:30 +0300482 return -EIO;
Oren Weil91f01c62011-05-15 13:43:44 +0300483 }
484
Tomas Winkler3870c322012-11-01 21:17:14 +0200485 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Oren Weil91f01c62011-05-15 13:43:44 +0300486 dev->me_client_index = b;
Oren Weilabc51b62011-09-21 16:45:30 +0300487 return 1;
Oren Weil91f01c62011-05-15 13:43:44 +0300488 }
489
Oren Weilabc51b62011-09-21 16:45:30 +0300490 return 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300491}
492
493/**
494 * mei_init_file_private - initializes private file structure.
495 *
496 * @priv: private file structure to be initialized
497 * @file: the file structure
498 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300499void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300500{
501 memset(priv, 0, sizeof(struct mei_cl));
502 init_waitqueue_head(&priv->wait);
503 init_waitqueue_head(&priv->rx_wait);
504 init_waitqueue_head(&priv->tx_wait);
505 INIT_LIST_HEAD(&priv->link);
506 priv->reading_state = MEI_IDLE;
507 priv->writing_state = MEI_IDLE;
508 priv->dev = dev;
509}
510
Tomas Winkler07b509b2012-07-23 14:05:39 +0300511int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid)
Oren Weil91f01c62011-05-15 13:43:44 +0300512{
Tomas Winkler07b509b2012-07-23 14:05:39 +0300513 int i, res = -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300514
Tomas Winklercf9673d2011-06-06 10:44:33 +0300515 for (i = 0; i < dev->me_clients_num; ++i)
Tomas Winkler07b509b2012-07-23 14:05:39 +0300516 if (uuid_le_cmp(*cuuid,
Oren Weil91f01c62011-05-15 13:43:44 +0300517 dev->me_clients[i].props.protocol_name) == 0) {
518 res = i;
519 break;
520 }
521
522 return res;
523}
524
525
526/**
Tomas Winkler07b509b2012-07-23 14:05:39 +0300527 * mei_me_cl_update_filext - searches for ME client guid
Oren Weil91f01c62011-05-15 13:43:44 +0300528 * sets client_id in mei_file_private if found
529 * @dev: the device structure
Tomas Winkler07b509b2012-07-23 14:05:39 +0300530 * @cl: private file structure to set client_id in
531 * @cuuid: searched uuid of ME client
Oren Weil91f01c62011-05-15 13:43:44 +0300532 * @client_id: id of host client to be set in file private structure
533 *
534 * returns ME client index
535 */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300536int mei_me_cl_update_filext(struct mei_device *dev, struct mei_cl *cl,
537 const uuid_le *cuuid, u8 host_cl_id)
Oren Weil91f01c62011-05-15 13:43:44 +0300538{
539 int i;
540
Tomas Winkler07b509b2012-07-23 14:05:39 +0300541 if (!dev || !cl || !cuuid)
542 return -EINVAL;
Oren Weil91f01c62011-05-15 13:43:44 +0300543
544 /* check for valid client id */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300545 i = mei_me_cl_by_uuid(dev, cuuid);
Oren Weil91f01c62011-05-15 13:43:44 +0300546 if (i >= 0) {
Tomas Winkler07b509b2012-07-23 14:05:39 +0300547 cl->me_client_id = dev->me_clients[i].client_id;
548 cl->state = MEI_FILE_CONNECTING;
549 cl->host_client_id = host_cl_id;
Oren Weil91f01c62011-05-15 13:43:44 +0300550
Tomas Winkler07b509b2012-07-23 14:05:39 +0300551 list_add_tail(&cl->link, &dev->file_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300552 return (u8)i;
553 }
554
Tomas Winkler07b509b2012-07-23 14:05:39 +0300555 return -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300556}
557
558/**
Oren Weil91f01c62011-05-15 13:43:44 +0300559 * mei_alloc_file_private - allocates a private file structure and sets it up.
560 * @file: the file structure
561 *
562 * returns The allocated file or NULL on failure
563 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300564struct mei_cl *mei_cl_allocate(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300565{
Tomas Winklerc95efb72011-05-25 17:28:21 +0300566 struct mei_cl *cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300567
Tomas Winklerc95efb72011-05-25 17:28:21 +0300568 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
569 if (!cl)
Oren Weil91f01c62011-05-15 13:43:44 +0300570 return NULL;
571
Tomas Winklerc95efb72011-05-25 17:28:21 +0300572 mei_cl_init(cl, dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300573
Tomas Winklerc95efb72011-05-25 17:28:21 +0300574 return cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300575}
576
577
578
579/**
580 * mei_disconnect_host_client - sends disconnect message to fw from host client.
581 *
582 * @dev: the device structure
583 * @cl: private data of the file object
584 *
585 * Locking: called under "dev->device_lock" lock
586 *
587 * returns 0 on success, <0 on failure.
588 */
589int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
590{
Oren Weil91f01c62011-05-15 13:43:44 +0300591 struct mei_cl_cb *cb;
Tomas Winkler3870c322012-11-01 21:17:14 +0200592 int rets, err;
Oren Weil91f01c62011-05-15 13:43:44 +0300593
594 if (!dev || !cl)
595 return -ENODEV;
596
597 if (cl->state != MEI_FILE_DISCONNECTING)
598 return 0;
599
Tomas Winkler664df382012-10-11 16:35:08 +0200600 cb = mei_io_cb_init(cl, NULL);
Oren Weil91f01c62011-05-15 13:43:44 +0300601 if (!cb)
602 return -ENOMEM;
603
Oren Weil91f01c62011-05-15 13:43:44 +0300604 cb->major_file_operations = MEI_CLOSE;
605 if (dev->mei_host_buffer_is_empty) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300606 dev->mei_host_buffer_is_empty = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300607 if (mei_disconnect(dev, cl)) {
Oren Weil91f01c62011-05-15 13:43:44 +0300608 rets = -ENODEV;
609 dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
610 goto free;
611 }
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200612 mdelay(10); /* Wait for hardware disconnection ready */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200613 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
Oren Weil91f01c62011-05-15 13:43:44 +0300614 } else {
615 dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200616 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
617
Oren Weil91f01c62011-05-15 13:43:44 +0300618 }
619 mutex_unlock(&dev->device_lock);
620
621 err = wait_event_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200622 MEI_FILE_DISCONNECTED == cl->state,
623 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300624
625 mutex_lock(&dev->device_lock);
626 if (MEI_FILE_DISCONNECTED == cl->state) {
627 rets = 0;
628 dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
629 } else {
630 rets = -ENODEV;
631 if (MEI_FILE_DISCONNECTED != cl->state)
632 dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
633
634 if (err)
635 dev_dbg(&dev->pdev->dev,
636 "wait failed disconnect err=%08x\n",
637 err);
638
639 dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
640 }
641
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300642 mei_io_list_flush(&dev->ctrl_rd_list, cl);
643 mei_io_list_flush(&dev->ctrl_wr_list, cl);
Oren Weil91f01c62011-05-15 13:43:44 +0300644free:
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200645 mei_io_cb_free(cb);
Oren Weil91f01c62011-05-15 13:43:44 +0300646 return rets;
647}
648
649/**
650 * mei_remove_client_from_file_list -
651 * removes file private data from device file list
652 *
653 * @dev: the device structure
654 * @host_client_id: host client id to be removed
655 */
656void mei_remove_client_from_file_list(struct mei_device *dev,
657 u8 host_client_id)
658{
659 struct mei_cl *cl_pos = NULL;
660 struct mei_cl *cl_next = NULL;
661 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
662 if (host_client_id == cl_pos->host_client_id) {
663 dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
664 cl_pos->host_client_id,
665 cl_pos->me_client_id);
666 list_del_init(&cl_pos->link);
667 break;
668 }
669 }
670}