xref: /openbmc/linux/drivers/misc/mei/init.c (revision 05bcf503)
1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2012, Intel Corporation.
5  *
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"
25 #include <linux/mei.h>
26 
27 const 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 
46 const uuid_le mei_amthi_guid  = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac,
47 						0xa8, 0x46, 0xe0, 0xff, 0x65,
48 						0x81, 0x4c);
49 
50 /**
51  * mei_io_list_init - Sets up a queue list.
52  *
53  * @list: An instance io list structure
54  * @dev: the device structure
55  */
56 void mei_io_list_init(struct mei_io_list *list)
57 {
58 	/* initialize our queue list */
59 	INIT_LIST_HEAD(&list->mei_cb.cb_list);
60 }
61 
62 /**
63  * mei_io_list_flush - removes list entry belonging to cl.
64  *
65  * @list:  An instance of our list structure
66  * @cl: private data of the file object
67  */
68 void mei_io_list_flush(struct mei_io_list *list, struct mei_cl *cl)
69 {
70 	struct mei_cl_cb *pos;
71 	struct mei_cl_cb *next;
72 
73 	list_for_each_entry_safe(pos, next, &list->mei_cb.cb_list, cb_list) {
74 		if (pos->file_private) {
75 			struct mei_cl *cl_tmp;
76 			cl_tmp = (struct mei_cl *)pos->file_private;
77 			if (mei_cl_cmp_id(cl, cl_tmp))
78 				list_del(&pos->cb_list);
79 		}
80 	}
81 }
82 /**
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  */
88 int 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 
105 
106 /**
107  * mei_reset_iamthif_params - initializes mei device iamthif
108  *
109  * @dev: the device structure
110  */
111 static 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;
117 	dev->iamthif_canceled = false;
118 	dev->iamthif_ioctl = false;
119 	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  */
130 struct mei_device *mei_device_init(struct pci_dev *pdev)
131 {
132 	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 */
139 	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);
145 	dev->dev_state = MEI_DEV_INITIALIZING;
146 	dev->iamthif_state = MEI_IAMTHIF_IDLE;
147 	dev->wd_interface_reg = false;
148 
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);
157 	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  */
168 int 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 
184 	/* Doesn't change in runtime */
185 	dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
186 
187 	dev->recvd_msg = false;
188 	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 
203 	if (err <= 0 && !dev->recvd_msg) {
204 		dev->dev_state = MEI_DEV_DISABLED;
205 		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))) {
214 		dev->dev_state = MEI_DEV_DISABLED;
215 		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 
219 		if (!(dev->host_hw_state & H_RDY))
220 			dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
221 
222 		if (!(dev->me_hw_state & ME_RDY_HRA))
223 			dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
224 
225 		dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
226 		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 
237 	dev->recvd_msg = false;
238 	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 
245 out:
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  */
256 static 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  */
272 void 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 
280 	if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
281 		dev->need_reset = true;
282 		return;
283 	}
284 
285 	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);
289 
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 
305 	dev->need_reset = false;
306 
307 	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;
311 
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);
328 		dev->extra_write_index = 0;
329 	}
330 
331 	dev->me_clients_num = 0;
332 	dev->rd_msg_hdr = 0;
333 	dev->wd_pending = false;
334 
335 	/* update the state of the registers after reset */
336 	dev->host_hw_state = mei_hcsr_read(dev);
337 	dev->me_hw_state = mei_mecsr_read(dev);
338 
339 	dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
340 	    dev->host_hw_state, dev->me_hw_state);
341 
342 	if (unexpected)
343 		dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
344 			 mei_dev_state_str(dev->dev_state));
345 
346 	/* Wake up all readings so they can be interrupted */
347 	list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
348 		if (waitqueue_active(&cl_pos->rx_wait)) {
349 			dev_dbg(&dev->pdev->dev, "Waking up client!\n");
350 			wake_up_interruptible(&cl_pos->rx_wait);
351 		}
352 	}
353 	/* remove all waiting requests */
354 	list_for_each_entry_safe(cb_pos, cb_next,
355 			&dev->write_list.mei_cb.cb_list, cb_list) {
356 		list_del(&cb_pos->cb_list);
357 		mei_free_cb_private(cb_pos);
358 	}
359 }
360 
361 
362 
363 /**
364  * host_start_message - mei host sends start message.
365  *
366  * @dev: the device structure
367  *
368  * returns none.
369  */
370 void mei_host_start_message(struct mei_device *dev)
371 {
372 	struct mei_msg_hdr *mei_hdr;
373 	struct hbm_host_version_request *host_start_req;
374 
375 	/* host start message */
376 	mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
377 	mei_hdr->host_addr = 0;
378 	mei_hdr->me_addr = 0;
379 	mei_hdr->length = sizeof(struct hbm_host_version_request);
380 	mei_hdr->msg_complete = 1;
381 	mei_hdr->reserved = 0;
382 
383 	host_start_req =
384 	    (struct hbm_host_version_request *) &dev->wr_msg_buf[1];
385 	memset(host_start_req, 0, sizeof(struct hbm_host_version_request));
386 	host_start_req->hbm_cmd = HOST_START_REQ_CMD;
387 	host_start_req->host_version.major_version = HBM_MAJOR_VERSION;
388 	host_start_req->host_version.minor_version = HBM_MINOR_VERSION;
389 	dev->recvd_msg = false;
390 	if (mei_write_message(dev, mei_hdr, (unsigned char *)host_start_req,
391 				       mei_hdr->length)) {
392 		dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
393 		dev->dev_state = MEI_DEV_RESETING;
394 		mei_reset(dev, 1);
395 	}
396 	dev->init_clients_state = MEI_START_MESSAGE;
397 	dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
398 	return ;
399 }
400 
401 /**
402  * host_enum_clients_message - host sends enumeration client request message.
403  *
404  * @dev: the device structure
405  *
406  * returns none.
407  */
408 void mei_host_enum_clients_message(struct mei_device *dev)
409 {
410 	struct mei_msg_hdr *mei_hdr;
411 	struct hbm_host_enum_request *host_enum_req;
412 	mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
413 	/* enumerate clients */
414 	mei_hdr->host_addr = 0;
415 	mei_hdr->me_addr = 0;
416 	mei_hdr->length = sizeof(struct hbm_host_enum_request);
417 	mei_hdr->msg_complete = 1;
418 	mei_hdr->reserved = 0;
419 
420 	host_enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
421 	memset(host_enum_req, 0, sizeof(struct hbm_host_enum_request));
422 	host_enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
423 	if (mei_write_message(dev, mei_hdr, (unsigned char *)host_enum_req,
424 				mei_hdr->length)) {
425 		dev->dev_state = MEI_DEV_RESETING;
426 		dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
427 		mei_reset(dev, 1);
428 	}
429 	dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
430 	dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
431 	return;
432 }
433 
434 
435 /**
436  * allocate_me_clients_storage - allocates storage for me clients
437  *
438  * @dev: the device structure
439  *
440  * returns none.
441  */
442 void mei_allocate_me_clients_storage(struct mei_device *dev)
443 {
444 	struct mei_me_client *clients;
445 	int b;
446 
447 	/* count how many ME clients we have */
448 	for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
449 		dev->me_clients_num++;
450 
451 	if (dev->me_clients_num <= 0)
452 		return ;
453 
454 
455 	if (dev->me_clients != NULL) {
456 		kfree(dev->me_clients);
457 		dev->me_clients = NULL;
458 	}
459 	dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
460 		dev->me_clients_num * sizeof(struct mei_me_client));
461 	/* allocate storage for ME clients representation */
462 	clients = kcalloc(dev->me_clients_num,
463 			sizeof(struct mei_me_client), GFP_KERNEL);
464 	if (!clients) {
465 		dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
466 		dev->dev_state = MEI_DEV_RESETING;
467 		mei_reset(dev, 1);
468 		return ;
469 	}
470 	dev->me_clients = clients;
471 	return ;
472 }
473 /**
474  * host_client_properties - reads properties for client
475  *
476  * @dev: the device structure
477  *
478  * returns:
479  * 	< 0 - Error.
480  *  = 0 - no more clients.
481  *  = 1 - still have clients to send properties request.
482  */
483 int mei_host_client_properties(struct mei_device *dev)
484 {
485 	struct mei_msg_hdr *mei_header;
486 	struct hbm_props_request *host_cli_req;
487 	int b;
488 	u8 client_num = dev->me_client_presentation_num;
489 
490 	b = dev->me_client_index;
491 	b = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, b);
492 	if (b < MEI_CLIENTS_MAX) {
493 		dev->me_clients[client_num].client_id = b;
494 		dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
495 		mei_header = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
496 		mei_header->host_addr = 0;
497 		mei_header->me_addr = 0;
498 		mei_header->length = sizeof(struct hbm_props_request);
499 		mei_header->msg_complete = 1;
500 		mei_header->reserved = 0;
501 
502 		host_cli_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
503 
504 		memset(host_cli_req, 0, sizeof(struct hbm_props_request));
505 
506 		host_cli_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
507 		host_cli_req->address = b;
508 
509 		if (mei_write_message(dev, mei_header,
510 				(unsigned char *)host_cli_req,
511 				mei_header->length)) {
512 			dev->dev_state = MEI_DEV_RESETING;
513 			dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
514 			mei_reset(dev, 1);
515 			return -EIO;
516 		}
517 
518 		dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
519 		dev->me_client_index = b;
520 		return 1;
521 	}
522 
523 	return 0;
524 }
525 
526 /**
527  * mei_init_file_private - initializes private file structure.
528  *
529  * @priv: private file structure to be initialized
530  * @file: the file structure
531  */
532 void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
533 {
534 	memset(priv, 0, sizeof(struct mei_cl));
535 	init_waitqueue_head(&priv->wait);
536 	init_waitqueue_head(&priv->rx_wait);
537 	init_waitqueue_head(&priv->tx_wait);
538 	INIT_LIST_HEAD(&priv->link);
539 	priv->reading_state = MEI_IDLE;
540 	priv->writing_state = MEI_IDLE;
541 	priv->dev = dev;
542 }
543 
544 int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid)
545 {
546 	int i, res = -ENOENT;
547 
548 	for (i = 0; i < dev->me_clients_num; ++i)
549 		if (uuid_le_cmp(*cuuid,
550 				dev->me_clients[i].props.protocol_name) == 0) {
551 			res = i;
552 			break;
553 		}
554 
555 	return res;
556 }
557 
558 
559 /**
560  * mei_me_cl_update_filext - searches for ME client guid
561  *                       sets client_id in mei_file_private if found
562  * @dev: the device structure
563  * @cl: private file structure to set client_id in
564  * @cuuid: searched uuid of ME client
565  * @client_id: id of host client to be set in file private structure
566  *
567  * returns ME client index
568  */
569 int mei_me_cl_update_filext(struct mei_device *dev, struct mei_cl *cl,
570 				const uuid_le *cuuid, u8 host_cl_id)
571 {
572 	int i;
573 
574 	if (!dev || !cl || !cuuid)
575 		return -EINVAL;
576 
577 	/* check for valid client id */
578 	i = mei_me_cl_by_uuid(dev, cuuid);
579 	if (i >= 0) {
580 		cl->me_client_id = dev->me_clients[i].client_id;
581 		cl->state = MEI_FILE_CONNECTING;
582 		cl->host_client_id = host_cl_id;
583 
584 		list_add_tail(&cl->link, &dev->file_list);
585 		return (u8)i;
586 	}
587 
588 	return -ENOENT;
589 }
590 
591 /**
592  * host_init_iamthif - mei initialization iamthif client.
593  *
594  * @dev: the device structure
595  *
596  */
597 void mei_host_init_iamthif(struct mei_device *dev)
598 {
599 	int i;
600 	unsigned char *msg_buf;
601 
602 	mei_cl_init(&dev->iamthif_cl, dev);
603 	dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
604 
605 	/* find ME amthi client */
606 	i = mei_me_cl_update_filext(dev, &dev->iamthif_cl,
607 			    &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
608 	if (i < 0) {
609 		dev_dbg(&dev->pdev->dev, "failed to find iamthif client.\n");
610 		return;
611 	}
612 
613 	/* Assign iamthif_mtu to the value received from ME  */
614 
615 	dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
616 	dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
617 			dev->me_clients[i].props.max_msg_length);
618 
619 	kfree(dev->iamthif_msg_buf);
620 	dev->iamthif_msg_buf = NULL;
621 
622 	/* allocate storage for ME message buffer */
623 	msg_buf = kcalloc(dev->iamthif_mtu,
624 			sizeof(unsigned char), GFP_KERNEL);
625 	if (!msg_buf) {
626 		dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n");
627 		return;
628 	}
629 
630 	dev->iamthif_msg_buf = msg_buf;
631 
632 	if (mei_connect(dev, &dev->iamthif_cl)) {
633 		dev_dbg(&dev->pdev->dev, "Failed to connect to AMTHI client\n");
634 		dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
635 		dev->iamthif_cl.host_client_id = 0;
636 	} else {
637 		dev->iamthif_cl.timer_count = CONNECT_TIMEOUT;
638 	}
639 }
640 
641 /**
642  * mei_alloc_file_private - allocates a private file structure and sets it up.
643  * @file: the file structure
644  *
645  * returns  The allocated file or NULL on failure
646  */
647 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
648 {
649 	struct mei_cl *cl;
650 
651 	cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
652 	if (!cl)
653 		return NULL;
654 
655 	mei_cl_init(cl, dev);
656 
657 	return cl;
658 }
659 
660 
661 
662 /**
663  * mei_disconnect_host_client - sends disconnect message to fw from host client.
664  *
665  * @dev: the device structure
666  * @cl: private data of the file object
667  *
668  * Locking: called under "dev->device_lock" lock
669  *
670  * returns 0 on success, <0 on failure.
671  */
672 int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
673 {
674 	int rets, err;
675 	long timeout = 15;	/* 15 seconds */
676 	struct mei_cl_cb *cb;
677 
678 	if (!dev || !cl)
679 		return -ENODEV;
680 
681 	if (cl->state != MEI_FILE_DISCONNECTING)
682 		return 0;
683 
684 	cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
685 	if (!cb)
686 		return -ENOMEM;
687 
688 	INIT_LIST_HEAD(&cb->cb_list);
689 	cb->file_private = cl;
690 	cb->major_file_operations = MEI_CLOSE;
691 	if (dev->mei_host_buffer_is_empty) {
692 		dev->mei_host_buffer_is_empty = false;
693 		if (mei_disconnect(dev, cl)) {
694 			rets = -ENODEV;
695 			dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
696 			goto free;
697 		}
698 		mdelay(10); /* Wait for hardware disconnection ready */
699 		list_add_tail(&cb->cb_list, &dev->ctrl_rd_list.mei_cb.cb_list);
700 	} else {
701 		dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
702 		list_add_tail(&cb->cb_list,
703 				&dev->ctrl_wr_list.mei_cb.cb_list);
704 	}
705 	mutex_unlock(&dev->device_lock);
706 
707 	err = wait_event_timeout(dev->wait_recvd_msg,
708 		 (MEI_FILE_DISCONNECTED == cl->state),
709 		 timeout * HZ);
710 
711 	mutex_lock(&dev->device_lock);
712 	if (MEI_FILE_DISCONNECTED == cl->state) {
713 		rets = 0;
714 		dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
715 	} else {
716 		rets = -ENODEV;
717 		if (MEI_FILE_DISCONNECTED != cl->state)
718 			dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
719 
720 		if (err)
721 			dev_dbg(&dev->pdev->dev,
722 					"wait failed disconnect err=%08x\n",
723 					err);
724 
725 		dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
726 	}
727 
728 	mei_io_list_flush(&dev->ctrl_rd_list, cl);
729 	mei_io_list_flush(&dev->ctrl_wr_list, cl);
730 free:
731 	mei_free_cb_private(cb);
732 	return rets;
733 }
734 
735 /**
736  * mei_remove_client_from_file_list -
737  *	removes file private data from device file list
738  *
739  * @dev: the device structure
740  * @host_client_id: host client id to be removed
741  */
742 void mei_remove_client_from_file_list(struct mei_device *dev,
743 				       u8 host_client_id)
744 {
745 	struct mei_cl *cl_pos = NULL;
746 	struct mei_cl *cl_next = NULL;
747 	list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
748 		if (host_client_id == cl_pos->host_client_id) {
749 			dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
750 					cl_pos->host_client_id,
751 					cl_pos->me_client_id);
752 			list_del_init(&cl_pos->link);
753 			break;
754 		}
755 	}
756 }
757