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