blob: 4fcb0bb2c9f0fa80f7924bff350afb27d4d4fd95 [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 */
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);
Oren Weil91f01c62011-05-15 13:43:44 +0300290 dev->extra_write_index = 0;
291 }
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;
334 struct hbm_host_version_request *host_start_req;
335
336 /* host start message */
337 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
338 mei_hdr->host_addr = 0;
339 mei_hdr->me_addr = 0;
340 mei_hdr->length = sizeof(struct hbm_host_version_request);
341 mei_hdr->msg_complete = 1;
342 mei_hdr->reserved = 0;
343
344 host_start_req =
345 (struct hbm_host_version_request *) &dev->wr_msg_buf[1];
346 memset(host_start_req, 0, sizeof(struct hbm_host_version_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200347 host_start_req->hbm_cmd = HOST_START_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300348 host_start_req->host_version.major_version = HBM_MAJOR_VERSION;
349 host_start_req->host_version.minor_version = HBM_MINOR_VERSION;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300350 dev->recvd_msg = false;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200351 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_start_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300352 mei_hdr->length)) {
353 dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300354 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300355 mei_reset(dev, 1);
356 }
357 dev->init_clients_state = MEI_START_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200358 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Oren Weil91f01c62011-05-15 13:43:44 +0300359 return ;
360}
361
362/**
363 * host_enum_clients_message - host sends enumeration client request message.
364 *
365 * @dev: the device structure
366 *
367 * returns none.
368 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300369void mei_host_enum_clients_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300370{
371 struct mei_msg_hdr *mei_hdr;
372 struct hbm_host_enum_request *host_enum_req;
373 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
374 /* enumerate clients */
375 mei_hdr->host_addr = 0;
376 mei_hdr->me_addr = 0;
377 mei_hdr->length = sizeof(struct hbm_host_enum_request);
378 mei_hdr->msg_complete = 1;
379 mei_hdr->reserved = 0;
380
381 host_enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
382 memset(host_enum_req, 0, sizeof(struct hbm_host_enum_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200383 host_enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200384 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_enum_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300385 mei_hdr->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300386 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300387 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
388 mei_reset(dev, 1);
389 }
390 dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
Tomas Winkler3870c322012-11-01 21:17:14 +0200391 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200392 return;
Oren Weil91f01c62011-05-15 13:43:44 +0300393}
394
395
396/**
397 * allocate_me_clients_storage - allocates storage for me clients
398 *
399 * @dev: the device structure
400 *
401 * returns none.
402 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300403void mei_allocate_me_clients_storage(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300404{
405 struct mei_me_client *clients;
406 int b;
407
408 /* count how many ME clients we have */
409 for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
Tomas Winklercf9673d2011-06-06 10:44:33 +0300410 dev->me_clients_num++;
Oren Weil91f01c62011-05-15 13:43:44 +0300411
Tomas Winklercf9673d2011-06-06 10:44:33 +0300412 if (dev->me_clients_num <= 0)
Oren Weil91f01c62011-05-15 13:43:44 +0300413 return ;
414
415
416 if (dev->me_clients != NULL) {
417 kfree(dev->me_clients);
418 dev->me_clients = NULL;
419 }
420 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
Tomas Winklercf9673d2011-06-06 10:44:33 +0300421 dev->me_clients_num * sizeof(struct mei_me_client));
Oren Weil91f01c62011-05-15 13:43:44 +0300422 /* allocate storage for ME clients representation */
Tomas Winklercf9673d2011-06-06 10:44:33 +0300423 clients = kcalloc(dev->me_clients_num,
Oren Weil91f01c62011-05-15 13:43:44 +0300424 sizeof(struct mei_me_client), GFP_KERNEL);
425 if (!clients) {
426 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300427 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300428 mei_reset(dev, 1);
429 return ;
430 }
431 dev->me_clients = clients;
432 return ;
433}
434/**
435 * host_client_properties - reads properties for client
436 *
437 * @dev: the device structure
438 *
Oren Weilabc51b62011-09-21 16:45:30 +0300439 * returns:
440 * < 0 - Error.
441 * = 0 - no more clients.
442 * = 1 - still have clients to send properties request.
Oren Weil91f01c62011-05-15 13:43:44 +0300443 */
Oren Weilabc51b62011-09-21 16:45:30 +0300444int mei_host_client_properties(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300445{
446 struct mei_msg_hdr *mei_header;
447 struct hbm_props_request *host_cli_req;
448 int b;
449 u8 client_num = dev->me_client_presentation_num;
450
451 b = dev->me_client_index;
452 b = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, b);
453 if (b < MEI_CLIENTS_MAX) {
454 dev->me_clients[client_num].client_id = b;
455 dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
456 mei_header = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
457 mei_header->host_addr = 0;
458 mei_header->me_addr = 0;
459 mei_header->length = sizeof(struct hbm_props_request);
460 mei_header->msg_complete = 1;
461 mei_header->reserved = 0;
462
463 host_cli_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
464
465 memset(host_cli_req, 0, sizeof(struct hbm_props_request));
466
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200467 host_cli_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300468 host_cli_req->address = b;
469
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200470 if (mei_write_message(dev, mei_header,
Oren Weil91f01c62011-05-15 13:43:44 +0300471 (unsigned char *)host_cli_req,
472 mei_header->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300473 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300474 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
475 mei_reset(dev, 1);
Oren Weilabc51b62011-09-21 16:45:30 +0300476 return -EIO;
Oren Weil91f01c62011-05-15 13:43:44 +0300477 }
478
Tomas Winkler3870c322012-11-01 21:17:14 +0200479 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
Oren Weil91f01c62011-05-15 13:43:44 +0300480 dev->me_client_index = b;
Oren Weilabc51b62011-09-21 16:45:30 +0300481 return 1;
Oren Weil91f01c62011-05-15 13:43:44 +0300482 }
483
Oren Weilabc51b62011-09-21 16:45:30 +0300484 return 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300485}
486
487/**
488 * mei_init_file_private - initializes private file structure.
489 *
490 * @priv: private file structure to be initialized
491 * @file: the file structure
492 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300493void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300494{
495 memset(priv, 0, sizeof(struct mei_cl));
496 init_waitqueue_head(&priv->wait);
497 init_waitqueue_head(&priv->rx_wait);
498 init_waitqueue_head(&priv->tx_wait);
499 INIT_LIST_HEAD(&priv->link);
500 priv->reading_state = MEI_IDLE;
501 priv->writing_state = MEI_IDLE;
502 priv->dev = dev;
503}
504
Tomas Winkler07b509b2012-07-23 14:05:39 +0300505int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid)
Oren Weil91f01c62011-05-15 13:43:44 +0300506{
Tomas Winkler07b509b2012-07-23 14:05:39 +0300507 int i, res = -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300508
Tomas Winklercf9673d2011-06-06 10:44:33 +0300509 for (i = 0; i < dev->me_clients_num; ++i)
Tomas Winkler07b509b2012-07-23 14:05:39 +0300510 if (uuid_le_cmp(*cuuid,
Oren Weil91f01c62011-05-15 13:43:44 +0300511 dev->me_clients[i].props.protocol_name) == 0) {
512 res = i;
513 break;
514 }
515
516 return res;
517}
518
519
520/**
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200521 * mei_me_cl_link - create link between host and me clinet and add
522 * me_cl to the list
Oren Weil91f01c62011-05-15 13:43:44 +0300523 *
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200524 * @dev: the device structure
525 * @cl: link between me and host client assocated with opened file descriptor
526 * @cuuid: uuid of ME client
527 * @client_id: id of the host client
528 *
529 * returns ME client index if ME client
530 * -EINVAL on incorrect values
531 * -ENONET if client not found
Oren Weil91f01c62011-05-15 13:43:44 +0300532 */
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200533int mei_me_cl_link(struct mei_device *dev, struct mei_cl *cl,
534 const uuid_le *cuuid, u8 host_cl_id)
Oren Weil91f01c62011-05-15 13:43:44 +0300535{
536 int i;
537
Tomas Winkler07b509b2012-07-23 14:05:39 +0300538 if (!dev || !cl || !cuuid)
539 return -EINVAL;
Oren Weil91f01c62011-05-15 13:43:44 +0300540
541 /* check for valid client id */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300542 i = mei_me_cl_by_uuid(dev, cuuid);
Oren Weil91f01c62011-05-15 13:43:44 +0300543 if (i >= 0) {
Tomas Winkler07b509b2012-07-23 14:05:39 +0300544 cl->me_client_id = dev->me_clients[i].client_id;
545 cl->state = MEI_FILE_CONNECTING;
546 cl->host_client_id = host_cl_id;
Oren Weil91f01c62011-05-15 13:43:44 +0300547
Tomas Winkler07b509b2012-07-23 14:05:39 +0300548 list_add_tail(&cl->link, &dev->file_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300549 return (u8)i;
550 }
551
Tomas Winkler07b509b2012-07-23 14:05:39 +0300552 return -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300553}
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200554/**
555 * mei_me_cl_unlink - remove me_cl from the list
556 *
557 * @dev: the device structure
558 * @host_client_id: host client id to be removed
559 */
560void mei_me_cl_unlink(struct mei_device *dev, struct mei_cl *cl)
561{
562 struct mei_cl *pos, *next;
563 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
564 if (cl->host_client_id == pos->host_client_id) {
565 dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
566 pos->host_client_id, pos->me_client_id);
567 list_del_init(&pos->link);
568 break;
569 }
570 }
571}
Oren Weil91f01c62011-05-15 13:43:44 +0300572
573/**
Oren Weil91f01c62011-05-15 13:43:44 +0300574 * mei_alloc_file_private - allocates a private file structure and sets it up.
575 * @file: the file structure
576 *
577 * returns The allocated file or NULL on failure
578 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300579struct mei_cl *mei_cl_allocate(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300580{
Tomas Winklerc95efb72011-05-25 17:28:21 +0300581 struct mei_cl *cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300582
Tomas Winklerc95efb72011-05-25 17:28:21 +0300583 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
584 if (!cl)
Oren Weil91f01c62011-05-15 13:43:44 +0300585 return NULL;
586
Tomas Winklerc95efb72011-05-25 17:28:21 +0300587 mei_cl_init(cl, dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300588
Tomas Winklerc95efb72011-05-25 17:28:21 +0300589 return cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300590}
591
592
593
594/**
595 * mei_disconnect_host_client - sends disconnect message to fw from host client.
596 *
597 * @dev: the device structure
598 * @cl: private data of the file object
599 *
600 * Locking: called under "dev->device_lock" lock
601 *
602 * returns 0 on success, <0 on failure.
603 */
604int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
605{
Oren Weil91f01c62011-05-15 13:43:44 +0300606 struct mei_cl_cb *cb;
Tomas Winkler3870c322012-11-01 21:17:14 +0200607 int rets, err;
Oren Weil91f01c62011-05-15 13:43:44 +0300608
609 if (!dev || !cl)
610 return -ENODEV;
611
612 if (cl->state != MEI_FILE_DISCONNECTING)
613 return 0;
614
Tomas Winkler664df382012-10-11 16:35:08 +0200615 cb = mei_io_cb_init(cl, NULL);
Oren Weil91f01c62011-05-15 13:43:44 +0300616 if (!cb)
617 return -ENOMEM;
618
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200619 cb->fop_type = MEI_FOP_CLOSE;
Oren Weil91f01c62011-05-15 13:43:44 +0300620 if (dev->mei_host_buffer_is_empty) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300621 dev->mei_host_buffer_is_empty = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300622 if (mei_disconnect(dev, cl)) {
Oren Weil91f01c62011-05-15 13:43:44 +0300623 rets = -ENODEV;
624 dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
625 goto free;
626 }
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200627 mdelay(10); /* Wait for hardware disconnection ready */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200628 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
Oren Weil91f01c62011-05-15 13:43:44 +0300629 } else {
630 dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200631 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
632
Oren Weil91f01c62011-05-15 13:43:44 +0300633 }
634 mutex_unlock(&dev->device_lock);
635
636 err = wait_event_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200637 MEI_FILE_DISCONNECTED == cl->state,
638 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300639
640 mutex_lock(&dev->device_lock);
641 if (MEI_FILE_DISCONNECTED == cl->state) {
642 rets = 0;
643 dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
644 } else {
645 rets = -ENODEV;
646 if (MEI_FILE_DISCONNECTED != cl->state)
647 dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
648
649 if (err)
650 dev_dbg(&dev->pdev->dev,
651 "wait failed disconnect err=%08x\n",
652 err);
653
654 dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
655 }
656
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300657 mei_io_list_flush(&dev->ctrl_rd_list, cl);
658 mei_io_list_flush(&dev->ctrl_wr_list, cl);
Oren Weil91f01c62011-05-15 13:43:44 +0300659free:
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200660 mei_io_cb_free(cb);
Oren Weil91f01c62011-05-15 13:43:44 +0300661 return rets;
662}
663