packaging: ivi: Do Not Use profile macro
[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                 cl->state = MEI_FILE_INITIALIZING;
568                 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
569         }
570
571         mutex_unlock(&dev->device_lock);
572         rets = wait_event_timeout(dev->wait_recvd_msg,
573                                  (cl->state == MEI_FILE_CONNECTED ||
574                                   cl->state == MEI_FILE_DISCONNECTED),
575                                  mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
576         mutex_lock(&dev->device_lock);
577
578         if (cl->state != MEI_FILE_CONNECTED) {
579                 rets = -EFAULT;
580
581                 mei_io_list_flush(&dev->ctrl_rd_list, cl);
582                 mei_io_list_flush(&dev->ctrl_wr_list, cl);
583                 goto out;
584         }
585
586         rets = cl->status;
587
588 out:
589         mei_io_cb_free(cb);
590         return rets;
591 }
592
593 /**
594  * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
595  *
596  * @cl: private data of the file object
597  *
598  * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
599  *      -ENOENT if mei_cl is not present
600  *      -EINVAL if single_recv_buf == 0
601  */
602 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
603 {
604         struct mei_device *dev;
605         int i;
606
607         if (WARN_ON(!cl || !cl->dev))
608                 return -EINVAL;
609
610         dev = cl->dev;
611
612         if (!dev->me_clients_num)
613                 return 0;
614
615         if (cl->mei_flow_ctrl_creds > 0)
616                 return 1;
617
618         for (i = 0; i < dev->me_clients_num; i++) {
619                 struct mei_me_client  *me_cl = &dev->me_clients[i];
620                 if (me_cl->client_id == cl->me_client_id) {
621                         if (me_cl->mei_flow_ctrl_creds) {
622                                 if (WARN_ON(me_cl->props.single_recv_buf == 0))
623                                         return -EINVAL;
624                                 return 1;
625                         } else {
626                                 return 0;
627                         }
628                 }
629         }
630         return -ENOENT;
631 }
632
633 /**
634  * mei_cl_flow_ctrl_reduce - reduces flow_control.
635  *
636  * @cl: private data of the file object
637  *
638  * @returns
639  *      0 on success
640  *      -ENOENT when me client is not found
641  *      -EINVAL when ctrl credits are <= 0
642  */
643 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
644 {
645         struct mei_device *dev;
646         int i;
647
648         if (WARN_ON(!cl || !cl->dev))
649                 return -EINVAL;
650
651         dev = cl->dev;
652
653         if (!dev->me_clients_num)
654                 return -ENOENT;
655
656         for (i = 0; i < dev->me_clients_num; i++) {
657                 struct mei_me_client  *me_cl = &dev->me_clients[i];
658                 if (me_cl->client_id == cl->me_client_id) {
659                         if (me_cl->props.single_recv_buf != 0) {
660                                 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
661                                         return -EINVAL;
662                                 dev->me_clients[i].mei_flow_ctrl_creds--;
663                         } else {
664                                 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
665                                         return -EINVAL;
666                                 cl->mei_flow_ctrl_creds--;
667                         }
668                         return 0;
669                 }
670         }
671         return -ENOENT;
672 }
673
674 /**
675  * mei_cl_read_start - the start read client message function.
676  *
677  * @cl: host client
678  *
679  * returns 0 on success, <0 on failure.
680  */
681 int mei_cl_read_start(struct mei_cl *cl, size_t length)
682 {
683         struct mei_device *dev;
684         struct mei_cl_cb *cb;
685         int rets;
686         int i;
687
688         if (WARN_ON(!cl || !cl->dev))
689                 return -ENODEV;
690
691         dev = cl->dev;
692
693         if (!mei_cl_is_connected(cl))
694                 return -ENODEV;
695
696         if (cl->read_cb) {
697                 cl_dbg(dev, cl, "read is pending.\n");
698                 return -EBUSY;
699         }
700         i = mei_me_cl_by_id(dev, cl->me_client_id);
701         if (i < 0) {
702                 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
703                 return  -ENODEV;
704         }
705
706         cb = mei_io_cb_init(cl, NULL);
707         if (!cb)
708                 return -ENOMEM;
709
710         /* always allocate at least client max message */
711         length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
712         rets = mei_io_cb_alloc_resp_buf(cb, length);
713         if (rets)
714                 goto err;
715
716         cb->fop_type = MEI_FOP_READ;
717         if (dev->hbuf_is_ready) {
718                 dev->hbuf_is_ready = false;
719                 if (mei_hbm_cl_flow_control_req(dev, cl)) {
720                         cl_err(dev, cl, "flow control send failed\n");
721                         rets = -ENODEV;
722                         goto err;
723                 }
724                 list_add_tail(&cb->list, &dev->read_list.list);
725         } else {
726                 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
727         }
728
729         cl->read_cb = cb;
730
731         return rets;
732 err:
733         mei_io_cb_free(cb);
734         return rets;
735 }
736
737 /**
738  * mei_cl_irq_write_complete - write a message to device
739  *      from the interrupt thread context
740  *
741  * @cl: client
742  * @cb: callback block.
743  * @slots: free slots.
744  * @cmpl_list: complete list.
745  *
746  * returns 0, OK; otherwise error.
747  */
748 int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
749                                      s32 *slots, struct mei_cl_cb *cmpl_list)
750 {
751         struct mei_device *dev;
752         struct mei_msg_data *buf;
753         struct mei_msg_hdr mei_hdr;
754         size_t len;
755         u32 msg_slots;
756         int rets;
757
758
759         if (WARN_ON(!cl || !cl->dev))
760                 return -ENODEV;
761
762         dev = cl->dev;
763
764         buf = &cb->request_buffer;
765
766         rets = mei_cl_flow_ctrl_creds(cl);
767         if (rets < 0)
768                 return rets;
769
770         if (rets == 0) {
771                 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
772                 return 0;
773         }
774
775         len = buf->size - cb->buf_idx;
776         msg_slots = mei_data2slots(len);
777
778         mei_hdr.host_addr = cl->host_client_id;
779         mei_hdr.me_addr = cl->me_client_id;
780         mei_hdr.reserved = 0;
781         mei_hdr.internal = cb->internal;
782
783         if (*slots >= msg_slots) {
784                 mei_hdr.length = len;
785                 mei_hdr.msg_complete = 1;
786         /* Split the message only if we can write the whole host buffer */
787         } else if (*slots == dev->hbuf_depth) {
788                 msg_slots = *slots;
789                 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
790                 mei_hdr.length = len;
791                 mei_hdr.msg_complete = 0;
792         } else {
793                 /* wait for next time the host buffer is empty */
794                 return 0;
795         }
796
797         cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
798                         cb->request_buffer.size, cb->buf_idx);
799
800         *slots -=  msg_slots;
801         rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
802         if (rets) {
803                 cl->status = rets;
804                 list_move_tail(&cb->list, &cmpl_list->list);
805                 return rets;
806         }
807
808         cl->status = 0;
809         cl->writing_state = MEI_WRITING;
810         cb->buf_idx += mei_hdr.length;
811
812         if (mei_hdr.msg_complete) {
813                 if (mei_cl_flow_ctrl_reduce(cl))
814                         return -EIO;
815                 list_move_tail(&cb->list, &dev->write_waiting_list.list);
816         }
817
818         return 0;
819 }
820
821 /**
822  * mei_cl_write - submit a write cb to mei device
823         assumes device_lock is locked
824  *
825  * @cl: host client
826  * @cl: write callback with filled data
827  *
828  * returns number of bytes sent on success, <0 on failure.
829  */
830 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
831 {
832         struct mei_device *dev;
833         struct mei_msg_data *buf;
834         struct mei_msg_hdr mei_hdr;
835         int rets;
836
837
838         if (WARN_ON(!cl || !cl->dev))
839                 return -ENODEV;
840
841         if (WARN_ON(!cb))
842                 return -EINVAL;
843
844         dev = cl->dev;
845
846
847         buf = &cb->request_buffer;
848
849         cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
850
851
852         cb->fop_type = MEI_FOP_WRITE;
853
854         rets = mei_cl_flow_ctrl_creds(cl);
855         if (rets < 0)
856                 goto err;
857
858         /* Host buffer is not ready, we queue the request */
859         if (rets == 0 || !dev->hbuf_is_ready) {
860                 cb->buf_idx = 0;
861                 /* unseting complete will enqueue the cb for write */
862                 mei_hdr.msg_complete = 0;
863                 rets = buf->size;
864                 goto out;
865         }
866
867         dev->hbuf_is_ready = false;
868
869         /* Check for a maximum length */
870         if (buf->size > mei_hbuf_max_len(dev)) {
871                 mei_hdr.length = mei_hbuf_max_len(dev);
872                 mei_hdr.msg_complete = 0;
873         } else {
874                 mei_hdr.length = buf->size;
875                 mei_hdr.msg_complete = 1;
876         }
877
878         mei_hdr.host_addr = cl->host_client_id;
879         mei_hdr.me_addr = cl->me_client_id;
880         mei_hdr.reserved = 0;
881         mei_hdr.internal = cb->internal;
882
883
884         rets = mei_write_message(dev, &mei_hdr, buf->data);
885         if (rets)
886                 goto err;
887
888         cl->writing_state = MEI_WRITING;
889         cb->buf_idx = mei_hdr.length;
890
891         rets = buf->size;
892 out:
893         if (mei_hdr.msg_complete) {
894                 if (mei_cl_flow_ctrl_reduce(cl)) {
895                         rets = -ENODEV;
896                         goto err;
897                 }
898                 list_add_tail(&cb->list, &dev->write_waiting_list.list);
899         } else {
900                 list_add_tail(&cb->list, &dev->write_list.list);
901         }
902
903
904         if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
905
906                 mutex_unlock(&dev->device_lock);
907                 if (wait_event_interruptible(cl->tx_wait,
908                         cl->writing_state == MEI_WRITE_COMPLETE)) {
909                                 if (signal_pending(current))
910                                         rets = -EINTR;
911                                 else
912                                         rets = -ERESTARTSYS;
913                 }
914                 mutex_lock(&dev->device_lock);
915         }
916 err:
917         return rets;
918 }
919
920
921 /**
922  * mei_cl_complete - processes completed operation for a client
923  *
924  * @cl: private data of the file object.
925  * @cb: callback block.
926  */
927 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
928 {
929         if (cb->fop_type == MEI_FOP_WRITE) {
930                 mei_io_cb_free(cb);
931                 cb = NULL;
932                 cl->writing_state = MEI_WRITE_COMPLETE;
933                 if (waitqueue_active(&cl->tx_wait))
934                         wake_up_interruptible(&cl->tx_wait);
935
936         } else if (cb->fop_type == MEI_FOP_READ &&
937                         MEI_READING == cl->reading_state) {
938                 cl->reading_state = MEI_READ_COMPLETE;
939                 if (waitqueue_active(&cl->rx_wait))
940                         wake_up_interruptible(&cl->rx_wait);
941                 else
942                         mei_cl_bus_rx_event(cl);
943
944         }
945 }
946
947
948 /**
949  * mei_cl_all_disconnect - disconnect forcefully all connected clients
950  *
951  * @dev - mei device
952  */
953
954 void mei_cl_all_disconnect(struct mei_device *dev)
955 {
956         struct mei_cl *cl, *next;
957
958         list_for_each_entry_safe(cl, next, &dev->file_list, link) {
959                 cl->state = MEI_FILE_DISCONNECTED;
960                 cl->mei_flow_ctrl_creds = 0;
961                 cl->timer_count = 0;
962         }
963 }
964
965
966 /**
967  * mei_cl_all_wakeup  - wake up all readers and writers they can be interrupted
968  *
969  * @dev  - mei device
970  */
971 void mei_cl_all_wakeup(struct mei_device *dev)
972 {
973         struct mei_cl *cl, *next;
974         list_for_each_entry_safe(cl, next, &dev->file_list, link) {
975                 if (waitqueue_active(&cl->rx_wait)) {
976                         cl_dbg(dev, cl, "Waking up reading client!\n");
977                         wake_up_interruptible(&cl->rx_wait);
978                 }
979                 if (waitqueue_active(&cl->tx_wait)) {
980                         cl_dbg(dev, cl, "Waking up writing client!\n");
981                         wake_up_interruptible(&cl->tx_wait);
982                 }
983         }
984 }
985
986 /**
987  * mei_cl_all_write_clear - clear all pending writes
988
989  * @dev - mei device
990  */
991 void mei_cl_all_write_clear(struct mei_device *dev)
992 {
993         mei_io_list_free(&dev->write_list, NULL);
994         mei_io_list_free(&dev->write_waiting_list, NULL);
995 }
996
997