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