xref: /openbmc/linux/drivers/misc/mei/client.c (revision 161f4089)
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 <linux/mei.h>
23 
24 #include "mei_dev.h"
25 #include "hbm.h"
26 #include "client.h"
27 
28 /**
29  * mei_me_cl_by_uuid - locate index of me client
30  *
31  * @dev: mei device
32  * returns me client index or -ENOENT if not found
33  */
34 int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid)
35 {
36 	int i, res = -ENOENT;
37 
38 	for (i = 0; i < dev->me_clients_num; ++i)
39 		if (uuid_le_cmp(*uuid,
40 				dev->me_clients[i].props.protocol_name) == 0) {
41 			res = i;
42 			break;
43 		}
44 
45 	return res;
46 }
47 
48 
49 /**
50  * mei_me_cl_by_id return index to me_clients for client_id
51  *
52  * @dev: the device structure
53  * @client_id: me client id
54  *
55  * Locking: called under "dev->device_lock" lock
56  *
57  * returns index on success, -ENOENT on failure.
58  */
59 
60 int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
61 {
62 	int i;
63 	for (i = 0; i < dev->me_clients_num; i++)
64 		if (dev->me_clients[i].client_id == client_id)
65 			break;
66 	if (WARN_ON(dev->me_clients[i].client_id != client_id))
67 		return -ENOENT;
68 
69 	if (i == dev->me_clients_num)
70 		return -ENOENT;
71 
72 	return i;
73 }
74 
75 
76 /**
77  * mei_io_list_flush - removes list entry belonging to cl.
78  *
79  * @list:  An instance of our list structure
80  * @cl: host client
81  */
82 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
83 {
84 	struct mei_cl_cb *cb;
85 	struct mei_cl_cb *next;
86 
87 	list_for_each_entry_safe(cb, next, &list->list, list) {
88 		if (cb->cl && mei_cl_cmp_id(cl, cb->cl))
89 			list_del(&cb->list);
90 	}
91 }
92 
93 /**
94  * mei_io_cb_free - free mei_cb_private related memory
95  *
96  * @cb: mei callback struct
97  */
98 void mei_io_cb_free(struct mei_cl_cb *cb)
99 {
100 	if (cb == NULL)
101 		return;
102 
103 	kfree(cb->request_buffer.data);
104 	kfree(cb->response_buffer.data);
105 	kfree(cb);
106 }
107 
108 /**
109  * mei_io_cb_init - allocate and initialize io callback
110  *
111  * @cl - mei client
112  * @fp: pointer to file structure
113  *
114  * returns mei_cl_cb pointer or NULL;
115  */
116 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
117 {
118 	struct mei_cl_cb *cb;
119 
120 	cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
121 	if (!cb)
122 		return NULL;
123 
124 	mei_io_list_init(cb);
125 
126 	cb->file_object = fp;
127 	cb->cl = cl;
128 	cb->buf_idx = 0;
129 	return cb;
130 }
131 
132 /**
133  * mei_io_cb_alloc_req_buf - allocate request buffer
134  *
135  * @cb: io callback structure
136  * @length: size of the buffer
137  *
138  * returns 0 on success
139  *         -EINVAL if cb is NULL
140  *         -ENOMEM if allocation failed
141  */
142 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
143 {
144 	if (!cb)
145 		return -EINVAL;
146 
147 	if (length == 0)
148 		return 0;
149 
150 	cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
151 	if (!cb->request_buffer.data)
152 		return -ENOMEM;
153 	cb->request_buffer.size = length;
154 	return 0;
155 }
156 /**
157  * mei_io_cb_alloc_resp_buf - allocate respose buffer
158  *
159  * @cb: io callback structure
160  * @length: size of the buffer
161  *
162  * returns 0 on success
163  *         -EINVAL if cb is NULL
164  *         -ENOMEM if allocation failed
165  */
166 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
167 {
168 	if (!cb)
169 		return -EINVAL;
170 
171 	if (length == 0)
172 		return 0;
173 
174 	cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
175 	if (!cb->response_buffer.data)
176 		return -ENOMEM;
177 	cb->response_buffer.size = length;
178 	return 0;
179 }
180 
181 
182 
183 /**
184  * mei_cl_flush_queues - flushes queue lists belonging to cl.
185  *
186  * @cl: host client
187  */
188 int mei_cl_flush_queues(struct mei_cl *cl)
189 {
190 	struct mei_device *dev;
191 
192 	if (WARN_ON(!cl || !cl->dev))
193 		return -EINVAL;
194 
195 	dev = cl->dev;
196 
197 	cl_dbg(dev, cl, "remove list entry belonging to cl\n");
198 	mei_io_list_flush(&cl->dev->read_list, cl);
199 	mei_io_list_flush(&cl->dev->write_list, cl);
200 	mei_io_list_flush(&cl->dev->write_waiting_list, cl);
201 	mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
202 	mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
203 	mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
204 	mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
205 	return 0;
206 }
207 
208 
209 /**
210  * mei_cl_init - initializes intialize cl.
211  *
212  * @cl: host client to be initialized
213  * @dev: mei device
214  */
215 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
216 {
217 	memset(cl, 0, sizeof(struct mei_cl));
218 	init_waitqueue_head(&cl->wait);
219 	init_waitqueue_head(&cl->rx_wait);
220 	init_waitqueue_head(&cl->tx_wait);
221 	INIT_LIST_HEAD(&cl->link);
222 	INIT_LIST_HEAD(&cl->device_link);
223 	cl->reading_state = MEI_IDLE;
224 	cl->writing_state = MEI_IDLE;
225 	cl->dev = dev;
226 }
227 
228 /**
229  * mei_cl_allocate - allocates cl  structure and sets it up.
230  *
231  * @dev: mei device
232  * returns  The allocated file or NULL on failure
233  */
234 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
235 {
236 	struct mei_cl *cl;
237 
238 	cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
239 	if (!cl)
240 		return NULL;
241 
242 	mei_cl_init(cl, dev);
243 
244 	return cl;
245 }
246 
247 /**
248  * mei_cl_find_read_cb - find this cl's callback in the read list
249  *
250  * @cl: host client
251  *
252  * returns cb on success, NULL on error
253  */
254 struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
255 {
256 	struct mei_device *dev = cl->dev;
257 	struct mei_cl_cb *cb = NULL;
258 	struct mei_cl_cb *next = NULL;
259 
260 	list_for_each_entry_safe(cb, next, &dev->read_list.list, list)
261 		if (mei_cl_cmp_id(cl, cb->cl))
262 			return cb;
263 	return NULL;
264 }
265 
266 /** mei_cl_link: allocte host id in the host map
267  *
268  * @cl - host client
269  * @id - fixed host id or -1 for genereting one
270  *
271  * returns 0 on success
272  *	-EINVAL on incorrect values
273  *	-ENONET if client not found
274  */
275 int mei_cl_link(struct mei_cl *cl, int id)
276 {
277 	struct mei_device *dev;
278 	long open_handle_count;
279 
280 	if (WARN_ON(!cl || !cl->dev))
281 		return -EINVAL;
282 
283 	dev = cl->dev;
284 
285 	/* If Id is not asigned get one*/
286 	if (id == MEI_HOST_CLIENT_ID_ANY)
287 		id = find_first_zero_bit(dev->host_clients_map,
288 					MEI_CLIENTS_MAX);
289 
290 	if (id >= MEI_CLIENTS_MAX) {
291 		dev_err(&dev->pdev->dev, "id exceded %d", MEI_CLIENTS_MAX) ;
292 		return -EMFILE;
293 	}
294 
295 	open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
296 	if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
297 		dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
298 			MEI_MAX_OPEN_HANDLE_COUNT);
299 		return -EMFILE;
300 	}
301 
302 	dev->open_handle_count++;
303 
304 	cl->host_client_id = id;
305 	list_add_tail(&cl->link, &dev->file_list);
306 
307 	set_bit(id, dev->host_clients_map);
308 
309 	cl->state = MEI_FILE_INITIALIZING;
310 
311 	cl_dbg(dev, cl, "link cl\n");
312 	return 0;
313 }
314 
315 /**
316  * mei_cl_unlink - remove me_cl from the list
317  *
318  * @cl: host client
319  */
320 int mei_cl_unlink(struct mei_cl *cl)
321 {
322 	struct mei_device *dev;
323 
324 	/* don't shout on error exit path */
325 	if (!cl)
326 		return 0;
327 
328 	/* wd and amthif might not be initialized */
329 	if (!cl->dev)
330 		return 0;
331 
332 	dev = cl->dev;
333 
334 	cl_dbg(dev, cl, "unlink client");
335 
336 	if (dev->open_handle_count > 0)
337 		dev->open_handle_count--;
338 
339 	/* never clear the 0 bit */
340 	if (cl->host_client_id)
341 		clear_bit(cl->host_client_id, dev->host_clients_map);
342 
343 	list_del_init(&cl->link);
344 
345 	cl->state = MEI_FILE_INITIALIZING;
346 
347 	list_del_init(&cl->link);
348 
349 	return 0;
350 }
351 
352 
353 void mei_host_client_init(struct work_struct *work)
354 {
355 	struct mei_device *dev = container_of(work,
356 					      struct mei_device, init_work);
357 	struct mei_client_properties *client_props;
358 	int i;
359 
360 	mutex_lock(&dev->device_lock);
361 
362 	for (i = 0; i < dev->me_clients_num; i++) {
363 		client_props = &dev->me_clients[i].props;
364 
365 		if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
366 			mei_amthif_host_init(dev);
367 		else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
368 			mei_wd_host_init(dev);
369 		else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
370 			mei_nfc_host_init(dev);
371 
372 	}
373 
374 	dev->dev_state = MEI_DEV_ENABLED;
375 
376 	mutex_unlock(&dev->device_lock);
377 }
378 
379 
380 /**
381  * mei_cl_disconnect - disconnect host clinet form the me one
382  *
383  * @cl: host client
384  *
385  * Locking: called under "dev->device_lock" lock
386  *
387  * returns 0 on success, <0 on failure.
388  */
389 int mei_cl_disconnect(struct mei_cl *cl)
390 {
391 	struct mei_device *dev;
392 	struct mei_cl_cb *cb;
393 	int rets, err;
394 
395 	if (WARN_ON(!cl || !cl->dev))
396 		return -ENODEV;
397 
398 	dev = cl->dev;
399 
400 	cl_dbg(dev, cl, "disconnecting");
401 
402 	if (cl->state != MEI_FILE_DISCONNECTING)
403 		return 0;
404 
405 	cb = mei_io_cb_init(cl, NULL);
406 	if (!cb)
407 		return -ENOMEM;
408 
409 	cb->fop_type = MEI_FOP_CLOSE;
410 	if (dev->hbuf_is_ready) {
411 		dev->hbuf_is_ready = false;
412 		if (mei_hbm_cl_disconnect_req(dev, cl)) {
413 			rets = -ENODEV;
414 			cl_err(dev, cl, "failed to disconnect.\n");
415 			goto free;
416 		}
417 		mdelay(10); /* Wait for hardware disconnection ready */
418 		list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
419 	} else {
420 		cl_dbg(dev, cl, "add disconnect cb to control write list\n");
421 		list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
422 
423 	}
424 	mutex_unlock(&dev->device_lock);
425 
426 	err = wait_event_timeout(dev->wait_recvd_msg,
427 			MEI_FILE_DISCONNECTED == cl->state,
428 			mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
429 
430 	mutex_lock(&dev->device_lock);
431 	if (MEI_FILE_DISCONNECTED == cl->state) {
432 		rets = 0;
433 		cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
434 	} else {
435 		rets = -ENODEV;
436 		if (MEI_FILE_DISCONNECTED != cl->state)
437 			cl_err(dev, cl, "wrong status client disconnect.\n");
438 
439 		if (err)
440 			cl_dbg(dev, cl, "wait failed disconnect err=%08x\n",
441 					err);
442 
443 		cl_err(dev, cl, "failed to disconnect from FW client.\n");
444 	}
445 
446 	mei_io_list_flush(&dev->ctrl_rd_list, cl);
447 	mei_io_list_flush(&dev->ctrl_wr_list, cl);
448 free:
449 	mei_io_cb_free(cb);
450 	return rets;
451 }
452 
453 
454 /**
455  * mei_cl_is_other_connecting - checks if other
456  *    client with the same me client id is connecting
457  *
458  * @cl: private data of the file object
459  *
460  * returns ture if other client is connected, 0 - otherwise.
461  */
462 bool mei_cl_is_other_connecting(struct mei_cl *cl)
463 {
464 	struct mei_device *dev;
465 	struct mei_cl *pos;
466 	struct mei_cl *next;
467 
468 	if (WARN_ON(!cl || !cl->dev))
469 		return false;
470 
471 	dev = cl->dev;
472 
473 	list_for_each_entry_safe(pos, next, &dev->file_list, link) {
474 		if ((pos->state == MEI_FILE_CONNECTING) &&
475 		    (pos != cl) && cl->me_client_id == pos->me_client_id)
476 			return true;
477 
478 	}
479 
480 	return false;
481 }
482 
483 /**
484  * mei_cl_connect - connect host clinet to the me one
485  *
486  * @cl: host client
487  *
488  * Locking: called under "dev->device_lock" lock
489  *
490  * returns 0 on success, <0 on failure.
491  */
492 int mei_cl_connect(struct mei_cl *cl, struct file *file)
493 {
494 	struct mei_device *dev;
495 	struct mei_cl_cb *cb;
496 	int rets;
497 
498 	if (WARN_ON(!cl || !cl->dev))
499 		return -ENODEV;
500 
501 	dev = cl->dev;
502 
503 	cb = mei_io_cb_init(cl, file);
504 	if (!cb) {
505 		rets = -ENOMEM;
506 		goto out;
507 	}
508 
509 	cb->fop_type = MEI_FOP_IOCTL;
510 
511 	if (dev->hbuf_is_ready && !mei_cl_is_other_connecting(cl)) {
512 		dev->hbuf_is_ready = false;
513 
514 		if (mei_hbm_cl_connect_req(dev, cl)) {
515 			rets = -ENODEV;
516 			goto out;
517 		}
518 		cl->timer_count = MEI_CONNECT_TIMEOUT;
519 		list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
520 	} else {
521 		list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
522 	}
523 
524 	mutex_unlock(&dev->device_lock);
525 	rets = wait_event_timeout(dev->wait_recvd_msg,
526 				 (cl->state == MEI_FILE_CONNECTED ||
527 				  cl->state == MEI_FILE_DISCONNECTED),
528 				 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
529 	mutex_lock(&dev->device_lock);
530 
531 	if (cl->state != MEI_FILE_CONNECTED) {
532 		rets = -EFAULT;
533 
534 		mei_io_list_flush(&dev->ctrl_rd_list, cl);
535 		mei_io_list_flush(&dev->ctrl_wr_list, cl);
536 		goto out;
537 	}
538 
539 	rets = cl->status;
540 
541 out:
542 	mei_io_cb_free(cb);
543 	return rets;
544 }
545 
546 /**
547  * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
548  *
549  * @cl: private data of the file object
550  *
551  * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
552  *	-ENOENT if mei_cl is not present
553  *	-EINVAL if single_recv_buf == 0
554  */
555 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
556 {
557 	struct mei_device *dev;
558 	int i;
559 
560 	if (WARN_ON(!cl || !cl->dev))
561 		return -EINVAL;
562 
563 	dev = cl->dev;
564 
565 	if (!dev->me_clients_num)
566 		return 0;
567 
568 	if (cl->mei_flow_ctrl_creds > 0)
569 		return 1;
570 
571 	for (i = 0; i < dev->me_clients_num; i++) {
572 		struct mei_me_client  *me_cl = &dev->me_clients[i];
573 		if (me_cl->client_id == cl->me_client_id) {
574 			if (me_cl->mei_flow_ctrl_creds) {
575 				if (WARN_ON(me_cl->props.single_recv_buf == 0))
576 					return -EINVAL;
577 				return 1;
578 			} else {
579 				return 0;
580 			}
581 		}
582 	}
583 	return -ENOENT;
584 }
585 
586 /**
587  * mei_cl_flow_ctrl_reduce - reduces flow_control.
588  *
589  * @cl: private data of the file object
590  *
591  * @returns
592  *	0 on success
593  *	-ENOENT when me client is not found
594  *	-EINVAL when ctrl credits are <= 0
595  */
596 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
597 {
598 	struct mei_device *dev;
599 	int i;
600 
601 	if (WARN_ON(!cl || !cl->dev))
602 		return -EINVAL;
603 
604 	dev = cl->dev;
605 
606 	if (!dev->me_clients_num)
607 		return -ENOENT;
608 
609 	for (i = 0; i < dev->me_clients_num; i++) {
610 		struct mei_me_client  *me_cl = &dev->me_clients[i];
611 		if (me_cl->client_id == cl->me_client_id) {
612 			if (me_cl->props.single_recv_buf != 0) {
613 				if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
614 					return -EINVAL;
615 				dev->me_clients[i].mei_flow_ctrl_creds--;
616 			} else {
617 				if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
618 					return -EINVAL;
619 				cl->mei_flow_ctrl_creds--;
620 			}
621 			return 0;
622 		}
623 	}
624 	return -ENOENT;
625 }
626 
627 /**
628  * mei_cl_read_start - the start read client message function.
629  *
630  * @cl: host client
631  *
632  * returns 0 on success, <0 on failure.
633  */
634 int mei_cl_read_start(struct mei_cl *cl, size_t length)
635 {
636 	struct mei_device *dev;
637 	struct mei_cl_cb *cb;
638 	int rets;
639 	int i;
640 
641 	if (WARN_ON(!cl || !cl->dev))
642 		return -ENODEV;
643 
644 	dev = cl->dev;
645 
646 	if (!mei_cl_is_connected(cl))
647 		return -ENODEV;
648 
649 	if (cl->read_cb) {
650 		cl_dbg(dev, cl, "read is pending.\n");
651 		return -EBUSY;
652 	}
653 	i = mei_me_cl_by_id(dev, cl->me_client_id);
654 	if (i < 0) {
655 		cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
656 		return  -ENODEV;
657 	}
658 
659 	cb = mei_io_cb_init(cl, NULL);
660 	if (!cb)
661 		return -ENOMEM;
662 
663 	/* always allocate at least client max message */
664 	length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
665 	rets = mei_io_cb_alloc_resp_buf(cb, length);
666 	if (rets)
667 		goto err;
668 
669 	cb->fop_type = MEI_FOP_READ;
670 	cl->read_cb = cb;
671 	if (dev->hbuf_is_ready) {
672 		dev->hbuf_is_ready = false;
673 		if (mei_hbm_cl_flow_control_req(dev, cl)) {
674 			cl_err(dev, cl, "flow control send failed\n");
675 			rets = -ENODEV;
676 			goto err;
677 		}
678 		list_add_tail(&cb->list, &dev->read_list.list);
679 	} else {
680 		list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
681 	}
682 	return rets;
683 err:
684 	mei_io_cb_free(cb);
685 	return rets;
686 }
687 
688 /**
689  * mei_cl_irq_write_complete - write a message to device
690  *	from the interrupt thread context
691  *
692  * @cl: client
693  * @cb: callback block.
694  * @slots: free slots.
695  * @cmpl_list: complete list.
696  *
697  * returns 0, OK; otherwise error.
698  */
699 int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
700 				     s32 *slots, struct mei_cl_cb *cmpl_list)
701 {
702 	struct mei_device *dev;
703 	struct mei_msg_data *buf;
704 	struct mei_msg_hdr mei_hdr;
705 	size_t len;
706 	u32 msg_slots;
707 	int rets;
708 
709 
710 	if (WARN_ON(!cl || !cl->dev))
711 		return -ENODEV;
712 
713 	dev = cl->dev;
714 
715 	buf = &cb->request_buffer;
716 
717 	rets = mei_cl_flow_ctrl_creds(cl);
718 	if (rets < 0)
719 		return rets;
720 
721 	if (rets == 0) {
722 		cl_dbg(dev, cl,	"No flow control credentials: not sending.\n");
723 		return 0;
724 	}
725 
726 	len = buf->size - cb->buf_idx;
727 	msg_slots = mei_data2slots(len);
728 
729 	mei_hdr.host_addr = cl->host_client_id;
730 	mei_hdr.me_addr = cl->me_client_id;
731 	mei_hdr.reserved = 0;
732 
733 	if (*slots >= msg_slots) {
734 		mei_hdr.length = len;
735 		mei_hdr.msg_complete = 1;
736 	/* Split the message only if we can write the whole host buffer */
737 	} else if (*slots == dev->hbuf_depth) {
738 		msg_slots = *slots;
739 		len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
740 		mei_hdr.length = len;
741 		mei_hdr.msg_complete = 0;
742 	} else {
743 		/* wait for next time the host buffer is empty */
744 		return 0;
745 	}
746 
747 	cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
748 			cb->request_buffer.size, cb->buf_idx);
749 
750 	*slots -=  msg_slots;
751 	rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
752 	if (rets) {
753 		cl->status = rets;
754 		list_move_tail(&cb->list, &cmpl_list->list);
755 		return rets;
756 	}
757 
758 	cl->status = 0;
759 	cl->writing_state = MEI_WRITING;
760 	cb->buf_idx += mei_hdr.length;
761 
762 	if (mei_hdr.msg_complete) {
763 		if (mei_cl_flow_ctrl_reduce(cl))
764 			return -EIO;
765 		list_move_tail(&cb->list, &dev->write_waiting_list.list);
766 	}
767 
768 	return 0;
769 }
770 
771 /**
772  * mei_cl_write - submit a write cb to mei device
773 	assumes device_lock is locked
774  *
775  * @cl: host client
776  * @cl: write callback with filled data
777  *
778  * returns numbe of bytes sent on success, <0 on failure.
779  */
780 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
781 {
782 	struct mei_device *dev;
783 	struct mei_msg_data *buf;
784 	struct mei_msg_hdr mei_hdr;
785 	int rets;
786 
787 
788 	if (WARN_ON(!cl || !cl->dev))
789 		return -ENODEV;
790 
791 	if (WARN_ON(!cb))
792 		return -EINVAL;
793 
794 	dev = cl->dev;
795 
796 
797 	buf = &cb->request_buffer;
798 
799 	cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
800 
801 
802 	cb->fop_type = MEI_FOP_WRITE;
803 
804 	rets = mei_cl_flow_ctrl_creds(cl);
805 	if (rets < 0)
806 		goto err;
807 
808 	/* Host buffer is not ready, we queue the request */
809 	if (rets == 0 || !dev->hbuf_is_ready) {
810 		cb->buf_idx = 0;
811 		/* unseting complete will enqueue the cb for write */
812 		mei_hdr.msg_complete = 0;
813 		rets = buf->size;
814 		goto out;
815 	}
816 
817 	dev->hbuf_is_ready = false;
818 
819 	/* Check for a maximum length */
820 	if (buf->size > mei_hbuf_max_len(dev)) {
821 		mei_hdr.length = mei_hbuf_max_len(dev);
822 		mei_hdr.msg_complete = 0;
823 	} else {
824 		mei_hdr.length = buf->size;
825 		mei_hdr.msg_complete = 1;
826 	}
827 
828 	mei_hdr.host_addr = cl->host_client_id;
829 	mei_hdr.me_addr = cl->me_client_id;
830 	mei_hdr.reserved = 0;
831 
832 
833 	rets = mei_write_message(dev, &mei_hdr, buf->data);
834 	if (rets)
835 		goto err;
836 
837 	cl->writing_state = MEI_WRITING;
838 	cb->buf_idx = mei_hdr.length;
839 
840 	rets = buf->size;
841 out:
842 	if (mei_hdr.msg_complete) {
843 		if (mei_cl_flow_ctrl_reduce(cl)) {
844 			rets = -ENODEV;
845 			goto err;
846 		}
847 		list_add_tail(&cb->list, &dev->write_waiting_list.list);
848 	} else {
849 		list_add_tail(&cb->list, &dev->write_list.list);
850 	}
851 
852 
853 	if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
854 
855 		mutex_unlock(&dev->device_lock);
856 		if (wait_event_interruptible(cl->tx_wait,
857 			cl->writing_state == MEI_WRITE_COMPLETE)) {
858 				if (signal_pending(current))
859 					rets = -EINTR;
860 				else
861 					rets = -ERESTARTSYS;
862 		}
863 		mutex_lock(&dev->device_lock);
864 	}
865 err:
866 	return rets;
867 }
868 
869 
870 /**
871  * mei_cl_complete - processes completed operation for a client
872  *
873  * @cl: private data of the file object.
874  * @cb: callback block.
875  */
876 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
877 {
878 	if (cb->fop_type == MEI_FOP_WRITE) {
879 		mei_io_cb_free(cb);
880 		cb = NULL;
881 		cl->writing_state = MEI_WRITE_COMPLETE;
882 		if (waitqueue_active(&cl->tx_wait))
883 			wake_up_interruptible(&cl->tx_wait);
884 
885 	} else if (cb->fop_type == MEI_FOP_READ &&
886 			MEI_READING == cl->reading_state) {
887 		cl->reading_state = MEI_READ_COMPLETE;
888 		if (waitqueue_active(&cl->rx_wait))
889 			wake_up_interruptible(&cl->rx_wait);
890 		else
891 			mei_cl_bus_rx_event(cl);
892 
893 	}
894 }
895 
896 
897 /**
898  * mei_cl_all_disconnect - disconnect forcefully all connected clients
899  *
900  * @dev - mei device
901  */
902 
903 void mei_cl_all_disconnect(struct mei_device *dev)
904 {
905 	struct mei_cl *cl, *next;
906 
907 	list_for_each_entry_safe(cl, next, &dev->file_list, link) {
908 		cl->state = MEI_FILE_DISCONNECTED;
909 		cl->mei_flow_ctrl_creds = 0;
910 		cl->read_cb = NULL;
911 		cl->timer_count = 0;
912 	}
913 }
914 
915 
916 /**
917  * mei_cl_all_wakeup  - wake up all readers and writers they can be interrupted
918  *
919  * @dev  - mei device
920  */
921 void mei_cl_all_wakeup(struct mei_device *dev)
922 {
923 	struct mei_cl *cl, *next;
924 	list_for_each_entry_safe(cl, next, &dev->file_list, link) {
925 		if (waitqueue_active(&cl->rx_wait)) {
926 			cl_dbg(dev, cl, "Waking up reading client!\n");
927 			wake_up_interruptible(&cl->rx_wait);
928 		}
929 		if (waitqueue_active(&cl->tx_wait)) {
930 			cl_dbg(dev, cl, "Waking up writing client!\n");
931 			wake_up_interruptible(&cl->tx_wait);
932 		}
933 	}
934 }
935 
936 /**
937  * mei_cl_all_write_clear - clear all pending writes
938 
939  * @dev - mei device
940  */
941 void mei_cl_all_write_clear(struct mei_device *dev)
942 {
943 	struct mei_cl_cb *cb, *next;
944 
945 	list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
946 		list_del(&cb->list);
947 		mei_io_cb_free(cb);
948 	}
949 }
950 
951 
952