blob: cd6a7f1ff9160f8bfb3c5c3843ca84b7ac5e454f [file] [log] [blame]
Oren Weil91f01c62011-05-15 13:43:44 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba91c2012-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 +030046const uuid_le mei_amthi_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac,
47 0xa8, 0x46, 0xe0, 0xff, 0x65,
48 0x81, 0x4c);
49
50/**
Tomas Winkler0288c7c2011-06-06 10:44:34 +030051 * mei_io_list_init - Sets up a queue list.
Oren Weil91f01c62011-05-15 13:43:44 +030052 *
Tomas Winkler0288c7c2011-06-06 10:44:34 +030053 * @list: An instance io list structure
Oren Weil91f01c62011-05-15 13:43:44 +030054 * @dev: the device structure
55 */
Tomas Winkler0288c7c2011-06-06 10:44:34 +030056void mei_io_list_init(struct mei_io_list *list)
Oren Weil91f01c62011-05-15 13:43:44 +030057{
58 /* initialize our queue list */
59 INIT_LIST_HEAD(&list->mei_cb.cb_list);
Oren Weil91f01c62011-05-15 13:43:44 +030060}
61
62/**
Tomas Winkler0288c7c2011-06-06 10:44:34 +030063 * mei_io_list_flush - removes list entry belonging to cl.
Oren Weil91f01c62011-05-15 13:43:44 +030064 *
65 * @list: An instance of our list structure
66 * @cl: private data of the file object
67 */
Tomas Winkler0288c7c2011-06-06 10:44:34 +030068void mei_io_list_flush(struct mei_io_list *list, struct mei_cl *cl)
Oren Weil91f01c62011-05-15 13:43:44 +030069{
Tomas Winkler3a5352f2011-12-04 16:16:27 +020070 struct mei_cl_cb *pos;
71 struct mei_cl_cb *next;
Oren Weil91f01c62011-05-15 13:43:44 +030072
Tomas Winklerb7cd2d92011-11-27 21:43:34 +020073 list_for_each_entry_safe(pos, next, &list->mei_cb.cb_list, cb_list) {
Tomas Winkler3a5352f2011-12-04 16:16:27 +020074 if (pos->file_private) {
Tomas Winkler0288c7c2011-06-06 10:44:34 +030075 struct mei_cl *cl_tmp;
Tomas Winklerb7cd2d92011-11-27 21:43:34 +020076 cl_tmp = (struct mei_cl *)pos->file_private;
Tomas Winkler0288c7c2011-06-06 10:44:34 +030077 if (mei_cl_cmp_id(cl, cl_tmp))
Tomas Winklerb7cd2d92011-11-27 21:43:34 +020078 list_del(&pos->cb_list);
Oren Weil91f01c62011-05-15 13:43:44 +030079 }
80 }
81}
Tomas Winkler0288c7c2011-06-06 10:44:34 +030082/**
83 * mei_cl_flush_queues - flushes queue lists belonging to cl.
84 *
85 * @dev: the device structure
86 * @cl: private data of the file object
87 */
88int mei_cl_flush_queues(struct mei_cl *cl)
89{
90 if (!cl || !cl->dev)
91 return -EINVAL;
92
93 dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n");
94 mei_io_list_flush(&cl->dev->read_list, cl);
95 mei_io_list_flush(&cl->dev->write_list, cl);
96 mei_io_list_flush(&cl->dev->write_waiting_list, cl);
97 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
98 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
99 mei_io_list_flush(&cl->dev->amthi_cmd_list, cl);
100 mei_io_list_flush(&cl->dev->amthi_read_complete_list, cl);
101 return 0;
102}
103
104
Oren Weil91f01c62011-05-15 13:43:44 +0300105
106/**
107 * mei_reset_iamthif_params - initializes mei device iamthif
108 *
109 * @dev: the device structure
110 */
111static void mei_reset_iamthif_params(struct mei_device *dev)
112{
113 /* reset iamthif parameters. */
114 dev->iamthif_current_cb = NULL;
115 dev->iamthif_msg_buf_size = 0;
116 dev->iamthif_msg_buf_index = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300117 dev->iamthif_canceled = false;
118 dev->iamthif_ioctl = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300119 dev->iamthif_state = MEI_IAMTHIF_IDLE;
120 dev->iamthif_timer = 0;
121}
122
123/**
124 * init_mei_device - allocates and initializes the mei device structure
125 *
126 * @pdev: The pci device structure
127 *
128 * returns The mei_device_device pointer on success, NULL on failure.
129 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300130struct mei_device *mei_device_init(struct pci_dev *pdev)
Oren Weil91f01c62011-05-15 13:43:44 +0300131{
Oren Weil91f01c62011-05-15 13:43:44 +0300132 struct mei_device *dev;
133
134 dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
135 if (!dev)
136 return NULL;
137
138 /* setup our list array */
Oren Weil91f01c62011-05-15 13:43:44 +0300139 INIT_LIST_HEAD(&dev->file_list);
140 INIT_LIST_HEAD(&dev->wd_cl.link);
141 INIT_LIST_HEAD(&dev->iamthif_cl.link);
142 mutex_init(&dev->device_lock);
143 init_waitqueue_head(&dev->wait_recvd_msg);
144 init_waitqueue_head(&dev->wait_stop_wd);
Tomas Winklerb210d752012-08-07 00:03:56 +0300145 dev->dev_state = MEI_DEV_INITIALIZING;
Oren Weil91f01c62011-05-15 13:43:44 +0300146 dev->iamthif_state = MEI_IAMTHIF_IDLE;
Oren Weil9ce178e2011-09-07 09:03:09 +0300147 dev->wd_interface_reg = false;
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300148
149
150 mei_io_list_init(&dev->read_list);
151 mei_io_list_init(&dev->write_list);
152 mei_io_list_init(&dev->write_waiting_list);
153 mei_io_list_init(&dev->ctrl_wr_list);
154 mei_io_list_init(&dev->ctrl_rd_list);
155 mei_io_list_init(&dev->amthi_cmd_list);
156 mei_io_list_init(&dev->amthi_read_complete_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300157 dev->pdev = pdev;
158 return dev;
159}
160
161/**
162 * mei_hw_init - initializes host and fw to start work.
163 *
164 * @dev: the device structure
165 *
166 * returns 0 on success, <0 on failure.
167 */
168int mei_hw_init(struct mei_device *dev)
169{
170 int err = 0;
171 int ret;
172
173 mutex_lock(&dev->device_lock);
174
175 dev->host_hw_state = mei_hcsr_read(dev);
176 dev->me_hw_state = mei_mecsr_read(dev);
177 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
178 dev->host_hw_state, dev->me_hw_state);
179
180 /* acknowledge interrupt and stop interupts */
181 if ((dev->host_hw_state & H_IS) == H_IS)
182 mei_reg_write(dev, H_CSR, dev->host_hw_state);
183
Tomas Winkler24aadc82012-06-25 23:46:27 +0300184 /* Doesn't change in runtime */
185 dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
186
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300187 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300188 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
189
190 mei_reset(dev, 1);
191
192 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
193 dev->host_hw_state, dev->me_hw_state);
194
195 /* wait for ME to turn on ME_RDY */
196 if (!dev->recvd_msg) {
197 mutex_unlock(&dev->device_lock);
198 err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
199 dev->recvd_msg, MEI_INTEROP_TIMEOUT);
200 mutex_lock(&dev->device_lock);
201 }
202
Tomas Winklera534bb62011-06-13 16:39:31 +0300203 if (err <= 0 && !dev->recvd_msg) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300204 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300205 dev_dbg(&dev->pdev->dev,
206 "wait_event_interruptible_timeout failed"
207 "on wait for ME to turn on ME_RDY.\n");
208 ret = -ENODEV;
209 goto out;
210 }
211
212 if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
213 ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300214 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300215 dev_dbg(&dev->pdev->dev,
216 "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
217 dev->host_hw_state, dev->me_hw_state);
218
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300219 if (!(dev->host_hw_state & H_RDY))
Oren Weil91f01c62011-05-15 13:43:44 +0300220 dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
221
Dan Carpenter8eb73c62011-06-09 10:18:23 +0300222 if (!(dev->me_hw_state & ME_RDY_HRA))
Oren Weil91f01c62011-05-15 13:43:44 +0300223 dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
224
Tomas Winklerd39a4642012-03-19 17:58:42 +0200225 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
Oren Weil91f01c62011-05-15 13:43:44 +0300226 ret = -ENODEV;
227 goto out;
228 }
229
230 if (dev->version.major_version != HBM_MAJOR_VERSION ||
231 dev->version.minor_version != HBM_MINOR_VERSION) {
232 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
233 ret = -ENODEV;
234 goto out;
235 }
236
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300237 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300238 dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
239 dev->host_hw_state, dev->me_hw_state);
240 dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
241 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
242 dev_dbg(&dev->pdev->dev, "MEI start success.\n");
243 ret = 0;
244
245out:
246 mutex_unlock(&dev->device_lock);
247 return ret;
248}
249
250/**
251 * mei_hw_reset - resets fw via mei csr register.
252 *
253 * @dev: the device structure
254 * @interrupts_enabled: if interrupt should be enabled after reset.
255 */
256static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
257{
258 dev->host_hw_state |= (H_RST | H_IG);
259
260 if (interrupts_enabled)
261 mei_enable_interrupts(dev);
262 else
263 mei_disable_interrupts(dev);
264}
265
266/**
267 * mei_reset - resets host and fw.
268 *
269 * @dev: the device structure
270 * @interrupts_enabled: if interrupt should be enabled after reset.
271 */
272void mei_reset(struct mei_device *dev, int interrupts_enabled)
273{
274 struct mei_cl *cl_pos = NULL;
275 struct mei_cl *cl_next = NULL;
276 struct mei_cl_cb *cb_pos = NULL;
277 struct mei_cl_cb *cb_next = NULL;
278 bool unexpected;
279
Tomas Winklerb210d752012-08-07 00:03:56 +0300280 if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300281 dev->need_reset = true;
Oren Weil91f01c62011-05-15 13:43:44 +0300282 return;
283 }
284
Tomas Winklerb210d752012-08-07 00:03:56 +0300285 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
286 dev->dev_state != MEI_DEV_DISABLED &&
287 dev->dev_state != MEI_DEV_POWER_DOWN &&
288 dev->dev_state != MEI_DEV_POWER_UP);
Oren Weil91f01c62011-05-15 13:43:44 +0300289
290 dev->host_hw_state = mei_hcsr_read(dev);
291
292 dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
293 dev->host_hw_state);
294
295 mei_hw_reset(dev, interrupts_enabled);
296
297 dev->host_hw_state &= ~H_RST;
298 dev->host_hw_state |= H_IG;
299
300 mei_hcsr_set(dev);
301
302 dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
303 dev->host_hw_state);
304
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300305 dev->need_reset = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300306
Tomas Winklerb210d752012-08-07 00:03:56 +0300307 if (dev->dev_state != MEI_DEV_INITIALIZING) {
308 if (dev->dev_state != MEI_DEV_DISABLED &&
309 dev->dev_state != MEI_DEV_POWER_DOWN)
310 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300311
312 list_for_each_entry_safe(cl_pos,
313 cl_next, &dev->file_list, link) {
314 cl_pos->state = MEI_FILE_DISCONNECTED;
315 cl_pos->mei_flow_ctrl_creds = 0;
316 cl_pos->read_cb = NULL;
317 cl_pos->timer_count = 0;
318 }
319 /* remove entry if already in list */
320 dev_dbg(&dev->pdev->dev, "list del iamthif and wd file list.\n");
321 mei_remove_client_from_file_list(dev,
322 dev->wd_cl.host_client_id);
323
324 mei_remove_client_from_file_list(dev,
325 dev->iamthif_cl.host_client_id);
326
327 mei_reset_iamthif_params(dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300328 dev->extra_write_index = 0;
329 }
330
Tomas Winklercf9673d2011-06-06 10:44:33 +0300331 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300332 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300333 dev->stop = false;
334 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300335
336 /* update the state of the registers after reset */
337 dev->host_hw_state = mei_hcsr_read(dev);
338 dev->me_hw_state = mei_mecsr_read(dev);
339
340 dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
341 dev->host_hw_state, dev->me_hw_state);
342
343 if (unexpected)
Tomas Winklerb210d752012-08-07 00:03:56 +0300344 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
345 mei_dev_state_str(dev->dev_state));
Oren Weil91f01c62011-05-15 13:43:44 +0300346
347 /* Wake up all readings so they can be interrupted */
348 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
349 if (waitqueue_active(&cl_pos->rx_wait)) {
350 dev_dbg(&dev->pdev->dev, "Waking up client!\n");
351 wake_up_interruptible(&cl_pos->rx_wait);
352 }
353 }
354 /* remove all waiting requests */
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200355 list_for_each_entry_safe(cb_pos, cb_next,
356 &dev->write_list.mei_cb.cb_list, cb_list) {
Tomas Winkler3a5352f2011-12-04 16:16:27 +0200357 list_del(&cb_pos->cb_list);
358 mei_free_cb_private(cb_pos);
Oren Weil91f01c62011-05-15 13:43:44 +0300359 }
360}
361
362
363
364/**
365 * host_start_message - mei host sends start message.
366 *
367 * @dev: the device structure
368 *
369 * returns none.
370 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300371void mei_host_start_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300372{
373 struct mei_msg_hdr *mei_hdr;
374 struct hbm_host_version_request *host_start_req;
375
376 /* host start message */
377 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
378 mei_hdr->host_addr = 0;
379 mei_hdr->me_addr = 0;
380 mei_hdr->length = sizeof(struct hbm_host_version_request);
381 mei_hdr->msg_complete = 1;
382 mei_hdr->reserved = 0;
383
384 host_start_req =
385 (struct hbm_host_version_request *) &dev->wr_msg_buf[1];
386 memset(host_start_req, 0, sizeof(struct hbm_host_version_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200387 host_start_req->hbm_cmd = HOST_START_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300388 host_start_req->host_version.major_version = HBM_MAJOR_VERSION;
389 host_start_req->host_version.minor_version = HBM_MINOR_VERSION;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300390 dev->recvd_msg = false;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200391 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_start_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300392 mei_hdr->length)) {
393 dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300394 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300395 mei_reset(dev, 1);
396 }
397 dev->init_clients_state = MEI_START_MESSAGE;
398 dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
399 return ;
400}
401
402/**
403 * host_enum_clients_message - host sends enumeration client request message.
404 *
405 * @dev: the device structure
406 *
407 * returns none.
408 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300409void mei_host_enum_clients_message(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300410{
411 struct mei_msg_hdr *mei_hdr;
412 struct hbm_host_enum_request *host_enum_req;
413 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
414 /* enumerate clients */
415 mei_hdr->host_addr = 0;
416 mei_hdr->me_addr = 0;
417 mei_hdr->length = sizeof(struct hbm_host_enum_request);
418 mei_hdr->msg_complete = 1;
419 mei_hdr->reserved = 0;
420
421 host_enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
422 memset(host_enum_req, 0, sizeof(struct hbm_host_enum_request));
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200423 host_enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200424 if (mei_write_message(dev, mei_hdr, (unsigned char *)host_enum_req,
Oren Weil91f01c62011-05-15 13:43:44 +0300425 mei_hdr->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300426 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300427 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
428 mei_reset(dev, 1);
429 }
430 dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
431 dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200432 return;
Oren Weil91f01c62011-05-15 13:43:44 +0300433}
434
435
436/**
437 * allocate_me_clients_storage - allocates storage for me clients
438 *
439 * @dev: the device structure
440 *
441 * returns none.
442 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300443void mei_allocate_me_clients_storage(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300444{
445 struct mei_me_client *clients;
446 int b;
447
448 /* count how many ME clients we have */
449 for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
Tomas Winklercf9673d2011-06-06 10:44:33 +0300450 dev->me_clients_num++;
Oren Weil91f01c62011-05-15 13:43:44 +0300451
Tomas Winklercf9673d2011-06-06 10:44:33 +0300452 if (dev->me_clients_num <= 0)
Oren Weil91f01c62011-05-15 13:43:44 +0300453 return ;
454
455
456 if (dev->me_clients != NULL) {
457 kfree(dev->me_clients);
458 dev->me_clients = NULL;
459 }
460 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
Tomas Winklercf9673d2011-06-06 10:44:33 +0300461 dev->me_clients_num * sizeof(struct mei_me_client));
Oren Weil91f01c62011-05-15 13:43:44 +0300462 /* allocate storage for ME clients representation */
Tomas Winklercf9673d2011-06-06 10:44:33 +0300463 clients = kcalloc(dev->me_clients_num,
Oren Weil91f01c62011-05-15 13:43:44 +0300464 sizeof(struct mei_me_client), GFP_KERNEL);
465 if (!clients) {
466 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
Tomas Winklerb210d752012-08-07 00:03:56 +0300467 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300468 mei_reset(dev, 1);
469 return ;
470 }
471 dev->me_clients = clients;
472 return ;
473}
474/**
475 * host_client_properties - reads properties for client
476 *
477 * @dev: the device structure
478 *
Oren Weilabc51b62011-09-21 16:45:30 +0300479 * returns:
480 * < 0 - Error.
481 * = 0 - no more clients.
482 * = 1 - still have clients to send properties request.
Oren Weil91f01c62011-05-15 13:43:44 +0300483 */
Oren Weilabc51b62011-09-21 16:45:30 +0300484int mei_host_client_properties(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300485{
486 struct mei_msg_hdr *mei_header;
487 struct hbm_props_request *host_cli_req;
488 int b;
489 u8 client_num = dev->me_client_presentation_num;
490
491 b = dev->me_client_index;
492 b = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, b);
493 if (b < MEI_CLIENTS_MAX) {
494 dev->me_clients[client_num].client_id = b;
495 dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
496 mei_header = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
497 mei_header->host_addr = 0;
498 mei_header->me_addr = 0;
499 mei_header->length = sizeof(struct hbm_props_request);
500 mei_header->msg_complete = 1;
501 mei_header->reserved = 0;
502
503 host_cli_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
504
505 memset(host_cli_req, 0, sizeof(struct hbm_props_request));
506
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200507 host_cli_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
Oren Weil91f01c62011-05-15 13:43:44 +0300508 host_cli_req->address = b;
509
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200510 if (mei_write_message(dev, mei_header,
Oren Weil91f01c62011-05-15 13:43:44 +0300511 (unsigned char *)host_cli_req,
512 mei_header->length)) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300513 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300514 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
515 mei_reset(dev, 1);
Oren Weilabc51b62011-09-21 16:45:30 +0300516 return -EIO;
Oren Weil91f01c62011-05-15 13:43:44 +0300517 }
518
519 dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
520 dev->me_client_index = b;
Oren Weilabc51b62011-09-21 16:45:30 +0300521 return 1;
Oren Weil91f01c62011-05-15 13:43:44 +0300522 }
523
Oren Weilabc51b62011-09-21 16:45:30 +0300524 return 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300525}
526
527/**
528 * mei_init_file_private - initializes private file structure.
529 *
530 * @priv: private file structure to be initialized
531 * @file: the file structure
532 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300533void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300534{
535 memset(priv, 0, sizeof(struct mei_cl));
536 init_waitqueue_head(&priv->wait);
537 init_waitqueue_head(&priv->rx_wait);
538 init_waitqueue_head(&priv->tx_wait);
539 INIT_LIST_HEAD(&priv->link);
540 priv->reading_state = MEI_IDLE;
541 priv->writing_state = MEI_IDLE;
542 priv->dev = dev;
543}
544
Tomas Winkler07b509b2012-07-23 14:05:39 +0300545int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid)
Oren Weil91f01c62011-05-15 13:43:44 +0300546{
Tomas Winkler07b509b2012-07-23 14:05:39 +0300547 int i, res = -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300548
Tomas Winklercf9673d2011-06-06 10:44:33 +0300549 for (i = 0; i < dev->me_clients_num; ++i)
Tomas Winkler07b509b2012-07-23 14:05:39 +0300550 if (uuid_le_cmp(*cuuid,
Oren Weil91f01c62011-05-15 13:43:44 +0300551 dev->me_clients[i].props.protocol_name) == 0) {
552 res = i;
553 break;
554 }
555
556 return res;
557}
558
559
560/**
Tomas Winkler07b509b2012-07-23 14:05:39 +0300561 * mei_me_cl_update_filext - searches for ME client guid
Oren Weil91f01c62011-05-15 13:43:44 +0300562 * sets client_id in mei_file_private if found
563 * @dev: the device structure
Tomas Winkler07b509b2012-07-23 14:05:39 +0300564 * @cl: private file structure to set client_id in
565 * @cuuid: searched uuid of ME client
Oren Weil91f01c62011-05-15 13:43:44 +0300566 * @client_id: id of host client to be set in file private structure
567 *
568 * returns ME client index
569 */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300570int mei_me_cl_update_filext(struct mei_device *dev, struct mei_cl *cl,
571 const uuid_le *cuuid, u8 host_cl_id)
Oren Weil91f01c62011-05-15 13:43:44 +0300572{
573 int i;
574
Tomas Winkler07b509b2012-07-23 14:05:39 +0300575 if (!dev || !cl || !cuuid)
576 return -EINVAL;
Oren Weil91f01c62011-05-15 13:43:44 +0300577
578 /* check for valid client id */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300579 i = mei_me_cl_by_uuid(dev, cuuid);
Oren Weil91f01c62011-05-15 13:43:44 +0300580 if (i >= 0) {
Tomas Winkler07b509b2012-07-23 14:05:39 +0300581 cl->me_client_id = dev->me_clients[i].client_id;
582 cl->state = MEI_FILE_CONNECTING;
583 cl->host_client_id = host_cl_id;
Oren Weil91f01c62011-05-15 13:43:44 +0300584
Tomas Winkler07b509b2012-07-23 14:05:39 +0300585 list_add_tail(&cl->link, &dev->file_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300586 return (u8)i;
587 }
588
Tomas Winkler07b509b2012-07-23 14:05:39 +0300589 return -ENOENT;
Oren Weil91f01c62011-05-15 13:43:44 +0300590}
591
592/**
593 * host_init_iamthif - mei initialization iamthif client.
594 *
595 * @dev: the device structure
596 *
597 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300598void mei_host_init_iamthif(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300599{
Tomas Winkler07b509b2012-07-23 14:05:39 +0300600 int i;
Oren Weil91f01c62011-05-15 13:43:44 +0300601 unsigned char *msg_buf;
602
Tomas Winklerc95efb72011-05-25 17:28:21 +0300603 mei_cl_init(&dev->iamthif_cl, dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300604 dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
605
606 /* find ME amthi client */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300607 i = mei_me_cl_update_filext(dev, &dev->iamthif_cl,
Oren Weil91f01c62011-05-15 13:43:44 +0300608 &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
Tomas Winkler07b509b2012-07-23 14:05:39 +0300609 if (i < 0) {
Oren Weil91f01c62011-05-15 13:43:44 +0300610 dev_dbg(&dev->pdev->dev, "failed to find iamthif client.\n");
611 return;
612 }
613
Tomas Winklerc9667bf2011-12-13 23:39:33 +0200614 /* Assign iamthif_mtu to the value received from ME */
Oren Weil91f01c62011-05-15 13:43:44 +0300615
Oren Weil91f01c62011-05-15 13:43:44 +0300616 dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
Tomas Winklerc9667bf2011-12-13 23:39:33 +0200617 dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
Oren Weil91f01c62011-05-15 13:43:44 +0300618 dev->me_clients[i].props.max_msg_length);
619
620 kfree(dev->iamthif_msg_buf);
621 dev->iamthif_msg_buf = NULL;
622
623 /* allocate storage for ME message buffer */
624 msg_buf = kcalloc(dev->iamthif_mtu,
625 sizeof(unsigned char), GFP_KERNEL);
626 if (!msg_buf) {
627 dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n");
628 return;
629 }
630
631 dev->iamthif_msg_buf = msg_buf;
632
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200633 if (mei_connect(dev, &dev->iamthif_cl)) {
Oren Weil91f01c62011-05-15 13:43:44 +0300634 dev_dbg(&dev->pdev->dev, "Failed to connect to AMTHI client\n");
635 dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
636 dev->iamthif_cl.host_client_id = 0;
637 } else {
638 dev->iamthif_cl.timer_count = CONNECT_TIMEOUT;
639 }
640}
641
642/**
643 * mei_alloc_file_private - allocates a private file structure and sets it up.
644 * @file: the file structure
645 *
646 * returns The allocated file or NULL on failure
647 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300648struct mei_cl *mei_cl_allocate(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +0300649{
Tomas Winklerc95efb72011-05-25 17:28:21 +0300650 struct mei_cl *cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300651
Tomas Winklerc95efb72011-05-25 17:28:21 +0300652 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
653 if (!cl)
Oren Weil91f01c62011-05-15 13:43:44 +0300654 return NULL;
655
Tomas Winklerc95efb72011-05-25 17:28:21 +0300656 mei_cl_init(cl, dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300657
Tomas Winklerc95efb72011-05-25 17:28:21 +0300658 return cl;
Oren Weil91f01c62011-05-15 13:43:44 +0300659}
660
661
662
663/**
664 * mei_disconnect_host_client - sends disconnect message to fw from host client.
665 *
666 * @dev: the device structure
667 * @cl: private data of the file object
668 *
669 * Locking: called under "dev->device_lock" lock
670 *
671 * returns 0 on success, <0 on failure.
672 */
673int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
674{
675 int rets, err;
676 long timeout = 15; /* 15 seconds */
677 struct mei_cl_cb *cb;
678
679 if (!dev || !cl)
680 return -ENODEV;
681
682 if (cl->state != MEI_FILE_DISCONNECTING)
683 return 0;
684
685 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
686 if (!cb)
687 return -ENOMEM;
688
689 INIT_LIST_HEAD(&cb->cb_list);
690 cb->file_private = cl;
691 cb->major_file_operations = MEI_CLOSE;
692 if (dev->mei_host_buffer_is_empty) {
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300693 dev->mei_host_buffer_is_empty = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300694 if (mei_disconnect(dev, cl)) {
Oren Weil91f01c62011-05-15 13:43:44 +0300695 rets = -ENODEV;
696 dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
697 goto free;
698 }
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200699 mdelay(10); /* Wait for hardware disconnection ready */
700 list_add_tail(&cb->cb_list, &dev->ctrl_rd_list.mei_cb.cb_list);
Oren Weil91f01c62011-05-15 13:43:44 +0300701 } else {
702 dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
703 list_add_tail(&cb->cb_list,
704 &dev->ctrl_wr_list.mei_cb.cb_list);
705 }
706 mutex_unlock(&dev->device_lock);
707
708 err = wait_event_timeout(dev->wait_recvd_msg,
709 (MEI_FILE_DISCONNECTED == cl->state),
710 timeout * HZ);
711
712 mutex_lock(&dev->device_lock);
713 if (MEI_FILE_DISCONNECTED == cl->state) {
714 rets = 0;
715 dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
716 } else {
717 rets = -ENODEV;
718 if (MEI_FILE_DISCONNECTED != cl->state)
719 dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
720
721 if (err)
722 dev_dbg(&dev->pdev->dev,
723 "wait failed disconnect err=%08x\n",
724 err);
725
726 dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
727 }
728
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300729 mei_io_list_flush(&dev->ctrl_rd_list, cl);
730 mei_io_list_flush(&dev->ctrl_wr_list, cl);
Oren Weil91f01c62011-05-15 13:43:44 +0300731free:
732 mei_free_cb_private(cb);
733 return rets;
734}
735
736/**
737 * mei_remove_client_from_file_list -
738 * removes file private data from device file list
739 *
740 * @dev: the device structure
741 * @host_client_id: host client id to be removed
742 */
743void mei_remove_client_from_file_list(struct mei_device *dev,
744 u8 host_client_id)
745{
746 struct mei_cl *cl_pos = NULL;
747 struct mei_cl *cl_next = NULL;
748 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
749 if (host_client_id == cl_pos->host_client_id) {
750 dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
751 cl_pos->host_client_id,
752 cl_pos->me_client_id);
753 list_del_init(&cl_pos->link);
754 break;
755 }
756 }
757}