3f8bb90dbb58d9e9334a6bc9471a8e95c84f8910
[cascardo/linux.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/sched.h>
18 #include <linux/wait.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22
23 #include <linux/mei.h>
24
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
28
29 /**
30  * mei_me_cl_init - initialize me client
31  *
32  * @me_cl: me client
33  */
34 void mei_me_cl_init(struct mei_me_client *me_cl)
35 {
36         INIT_LIST_HEAD(&me_cl->list);
37         kref_init(&me_cl->refcnt);
38 }
39
40 /**
41  * mei_me_cl_get - increases me client refcount
42  *
43  * @me_cl: me client
44  *
45  * Locking: called under "dev->device_lock" lock
46  *
47  * Return: me client or NULL
48  */
49 struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
50 {
51         if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
52                 return me_cl;
53
54         return NULL;
55 }
56
57 /**
58  * mei_me_cl_release - free me client
59  *
60  * Locking: called under "dev->device_lock" lock
61  *
62  * @ref: me_client refcount
63  */
64 static void mei_me_cl_release(struct kref *ref)
65 {
66         struct mei_me_client *me_cl =
67                 container_of(ref, struct mei_me_client, refcnt);
68
69         kfree(me_cl);
70 }
71
72 /**
73  * mei_me_cl_put - decrease me client refcount and free client if necessary
74  *
75  * Locking: called under "dev->device_lock" lock
76  *
77  * @me_cl: me client
78  */
79 void mei_me_cl_put(struct mei_me_client *me_cl)
80 {
81         if (me_cl)
82                 kref_put(&me_cl->refcnt, mei_me_cl_release);
83 }
84
85 /**
86  * __mei_me_cl_del  - delete me client form the list and decrease
87  *     reference counter
88  *
89  * @dev: mei device
90  * @me_cl: me client
91  *
92  * Locking: dev->me_clients_rwsem
93  */
94 static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
95 {
96         if (!me_cl)
97                 return;
98
99         list_del(&me_cl->list);
100         mei_me_cl_put(me_cl);
101 }
102
103 /**
104  * mei_me_cl_add - add me client to the list
105  *
106  * @dev: mei device
107  * @me_cl: me client
108  */
109 void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
110 {
111         down_write(&dev->me_clients_rwsem);
112         list_add(&me_cl->list, &dev->me_clients);
113         up_write(&dev->me_clients_rwsem);
114 }
115
116 /**
117  * __mei_me_cl_by_uuid - locate me client by uuid
118  *      increases ref count
119  *
120  * @dev: mei device
121  * @uuid: me client uuid
122  *
123  * Return: me client or NULL if not found
124  *
125  * Locking: dev->me_clients_rwsem
126  */
127 static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
128                                         const uuid_le *uuid)
129 {
130         struct mei_me_client *me_cl;
131         const uuid_le *pn;
132
133         WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
134
135         list_for_each_entry(me_cl, &dev->me_clients, list) {
136                 pn = &me_cl->props.protocol_name;
137                 if (uuid_le_cmp(*uuid, *pn) == 0)
138                         return mei_me_cl_get(me_cl);
139         }
140
141         return NULL;
142 }
143
144 /**
145  * mei_me_cl_by_uuid - locate me client by uuid
146  *      increases ref count
147  *
148  * @dev: mei device
149  * @uuid: me client uuid
150  *
151  * Return: me client or NULL if not found
152  *
153  * Locking: dev->me_clients_rwsem
154  */
155 struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
156                                         const uuid_le *uuid)
157 {
158         struct mei_me_client *me_cl;
159
160         down_read(&dev->me_clients_rwsem);
161         me_cl = __mei_me_cl_by_uuid(dev, uuid);
162         up_read(&dev->me_clients_rwsem);
163
164         return me_cl;
165 }
166
167 /**
168  * mei_me_cl_by_id - locate me client by client id
169  *      increases ref count
170  *
171  * @dev: the device structure
172  * @client_id: me client id
173  *
174  * Return: me client or NULL if not found
175  *
176  * Locking: dev->me_clients_rwsem
177  */
178 struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
179 {
180
181         struct mei_me_client *__me_cl, *me_cl = NULL;
182
183         down_read(&dev->me_clients_rwsem);
184         list_for_each_entry(__me_cl, &dev->me_clients, list) {
185                 if (__me_cl->client_id == client_id) {
186                         me_cl = mei_me_cl_get(__me_cl);
187                         break;
188                 }
189         }
190         up_read(&dev->me_clients_rwsem);
191
192         return me_cl;
193 }
194
195 /**
196  * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
197  *      increases ref count
198  *
199  * @dev: the device structure
200  * @uuid: me client uuid
201  * @client_id: me client id
202  *
203  * Return: me client or null if not found
204  *
205  * Locking: dev->me_clients_rwsem
206  */
207 static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
208                                            const uuid_le *uuid, u8 client_id)
209 {
210         struct mei_me_client *me_cl;
211         const uuid_le *pn;
212
213         WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
214
215         list_for_each_entry(me_cl, &dev->me_clients, list) {
216                 pn = &me_cl->props.protocol_name;
217                 if (uuid_le_cmp(*uuid, *pn) == 0 &&
218                     me_cl->client_id == client_id)
219                         return mei_me_cl_get(me_cl);
220         }
221
222         return NULL;
223 }
224
225
226 /**
227  * mei_me_cl_by_uuid_id - locate me client by client id and uuid
228  *      increases ref count
229  *
230  * @dev: the device structure
231  * @uuid: me client uuid
232  * @client_id: me client id
233  *
234  * Return: me client or null if not found
235  */
236 struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
237                                            const uuid_le *uuid, u8 client_id)
238 {
239         struct mei_me_client *me_cl;
240
241         down_read(&dev->me_clients_rwsem);
242         me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
243         up_read(&dev->me_clients_rwsem);
244
245         return me_cl;
246 }
247
248 /**
249  * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
250  *
251  * @dev: the device structure
252  * @uuid: me client uuid
253  *
254  * Locking: called under "dev->device_lock" lock
255  */
256 void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
257 {
258         struct mei_me_client *me_cl;
259
260         dev_dbg(dev->dev, "remove %pUl\n", uuid);
261
262         down_write(&dev->me_clients_rwsem);
263         me_cl = __mei_me_cl_by_uuid(dev, uuid);
264         __mei_me_cl_del(dev, me_cl);
265         up_write(&dev->me_clients_rwsem);
266 }
267
268 /**
269  * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
270  *
271  * @dev: the device structure
272  * @uuid: me client uuid
273  * @id: me client id
274  *
275  * Locking: called under "dev->device_lock" lock
276  */
277 void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
278 {
279         struct mei_me_client *me_cl;
280
281         dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
282
283         down_write(&dev->me_clients_rwsem);
284         me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
285         __mei_me_cl_del(dev, me_cl);
286         up_write(&dev->me_clients_rwsem);
287 }
288
289 /**
290  * mei_me_cl_rm_all - remove all me clients
291  *
292  * @dev: the device structure
293  *
294  * Locking: called under "dev->device_lock" lock
295  */
296 void mei_me_cl_rm_all(struct mei_device *dev)
297 {
298         struct mei_me_client *me_cl, *next;
299
300         down_write(&dev->me_clients_rwsem);
301         list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
302                 __mei_me_cl_del(dev, me_cl);
303         up_write(&dev->me_clients_rwsem);
304 }
305
306 /**
307  * mei_cl_cmp_id - tells if the clients are the same
308  *
309  * @cl1: host client 1
310  * @cl2: host client 2
311  *
312  * Return: true  - if the clients has same host and me ids
313  *         false - otherwise
314  */
315 static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
316                                 const struct mei_cl *cl2)
317 {
318         return cl1 && cl2 &&
319                 (cl1->host_client_id == cl2->host_client_id) &&
320                 (cl1->me_client_id == cl2->me_client_id);
321 }
322
323 /**
324  * mei_io_cb_free - free mei_cb_private related memory
325  *
326  * @cb: mei callback struct
327  */
328 void mei_io_cb_free(struct mei_cl_cb *cb)
329 {
330         if (cb == NULL)
331                 return;
332
333         list_del(&cb->list);
334         kfree(cb->buf.data);
335         kfree(cb);
336 }
337
338 /**
339  * mei_io_cb_init - allocate and initialize io callback
340  *
341  * @cl: mei client
342  * @type: operation type
343  * @fp: pointer to file structure
344  *
345  * Return: mei_cl_cb pointer or NULL;
346  */
347 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, enum mei_cb_file_ops type,
348                                  struct file *fp)
349 {
350         struct mei_cl_cb *cb;
351
352         cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
353         if (!cb)
354                 return NULL;
355
356         INIT_LIST_HEAD(&cb->list);
357         cb->file_object = fp;
358         cb->cl = cl;
359         cb->buf_idx = 0;
360         cb->fop_type = type;
361         return cb;
362 }
363
364 /**
365  * __mei_io_list_flush - removes and frees cbs belonging to cl.
366  *
367  * @list:  an instance of our list structure
368  * @cl:    host client, can be NULL for flushing the whole list
369  * @free:  whether to free the cbs
370  */
371 static void __mei_io_list_flush(struct mei_cl_cb *list,
372                                 struct mei_cl *cl, bool free)
373 {
374         struct mei_cl_cb *cb, *next;
375
376         /* enable removing everything if no cl is specified */
377         list_for_each_entry_safe(cb, next, &list->list, list) {
378                 if (!cl || mei_cl_cmp_id(cl, cb->cl)) {
379                         list_del_init(&cb->list);
380                         if (free)
381                                 mei_io_cb_free(cb);
382                 }
383         }
384 }
385
386 /**
387  * mei_io_list_flush - removes list entry belonging to cl.
388  *
389  * @list:  An instance of our list structure
390  * @cl: host client
391  */
392 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
393 {
394         __mei_io_list_flush(list, cl, false);
395 }
396
397 /**
398  * mei_io_list_free - removes cb belonging to cl and free them
399  *
400  * @list:  An instance of our list structure
401  * @cl: host client
402  */
403 static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
404 {
405         __mei_io_list_flush(list, cl, true);
406 }
407
408 /**
409  * mei_io_cb_alloc_buf - allocate callback buffer
410  *
411  * @cb: io callback structure
412  * @length: size of the buffer
413  *
414  * Return: 0 on success
415  *         -EINVAL if cb is NULL
416  *         -ENOMEM if allocation failed
417  */
418 int mei_io_cb_alloc_buf(struct mei_cl_cb *cb, size_t length)
419 {
420         if (!cb)
421                 return -EINVAL;
422
423         if (length == 0)
424                 return 0;
425
426         cb->buf.data = kmalloc(length, GFP_KERNEL);
427         if (!cb->buf.data)
428                 return -ENOMEM;
429         cb->buf.size = length;
430         return 0;
431 }
432
433 /**
434  * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
435  *
436  * @cl: host client
437  * @length: size of the buffer
438  * @type: operation type
439  * @fp: associated file pointer (might be NULL)
440  *
441  * Return: cb on success and NULL on failure
442  */
443 struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
444                                   enum mei_cb_file_ops type, struct file *fp)
445 {
446         struct mei_cl_cb *cb;
447
448         cb = mei_io_cb_init(cl, type, fp);
449         if (!cb)
450                 return NULL;
451
452         if (mei_io_cb_alloc_buf(cb, length)) {
453                 mei_io_cb_free(cb);
454                 return NULL;
455         }
456
457         return cb;
458 }
459
460 /**
461  * mei_cl_read_cb - find this cl's callback in the read list
462  *     for a specific file
463  *
464  * @cl: host client
465  * @fp: file pointer (matching cb file object), may be NULL
466  *
467  * Return: cb on success, NULL if cb is not found
468  */
469 struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
470 {
471         struct mei_cl_cb *cb;
472
473         list_for_each_entry(cb, &cl->rd_completed, list)
474                 if (!fp || fp == cb->file_object)
475                         return cb;
476
477         return NULL;
478 }
479
480 /**
481  * mei_cl_read_cb_flush - free client's read pending and completed cbs
482  *   for a specific file
483  *
484  * @cl: host client
485  * @fp: file pointer (matching cb file object), may be NULL
486  */
487 void mei_cl_read_cb_flush(const struct mei_cl *cl, const struct file *fp)
488 {
489         struct mei_cl_cb *cb, *next;
490
491         list_for_each_entry_safe(cb, next, &cl->rd_completed, list)
492                 if (!fp || fp == cb->file_object)
493                         mei_io_cb_free(cb);
494
495
496         list_for_each_entry_safe(cb, next, &cl->rd_pending, list)
497                 if (!fp || fp == cb->file_object)
498                         mei_io_cb_free(cb);
499 }
500
501 /**
502  * mei_cl_flush_queues - flushes queue lists belonging to cl.
503  *
504  * @cl: host client
505  * @fp: file pointer (matching cb file object), may be NULL
506  *
507  * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
508  */
509 int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
510 {
511         struct mei_device *dev;
512
513         if (WARN_ON(!cl || !cl->dev))
514                 return -EINVAL;
515
516         dev = cl->dev;
517
518         cl_dbg(dev, cl, "remove list entry belonging to cl\n");
519         mei_io_list_free(&cl->dev->write_list, cl);
520         mei_io_list_free(&cl->dev->write_waiting_list, cl);
521         mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
522         mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
523         mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
524         mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
525
526         mei_cl_read_cb_flush(cl, fp);
527
528         return 0;
529 }
530
531
532 /**
533  * mei_cl_init - initializes cl.
534  *
535  * @cl: host client to be initialized
536  * @dev: mei device
537  */
538 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
539 {
540         memset(cl, 0, sizeof(struct mei_cl));
541         init_waitqueue_head(&cl->wait);
542         init_waitqueue_head(&cl->rx_wait);
543         init_waitqueue_head(&cl->tx_wait);
544         INIT_LIST_HEAD(&cl->rd_completed);
545         INIT_LIST_HEAD(&cl->rd_pending);
546         INIT_LIST_HEAD(&cl->link);
547         INIT_LIST_HEAD(&cl->device_link);
548         cl->writing_state = MEI_IDLE;
549         cl->state = MEI_FILE_INITIALIZING;
550         cl->dev = dev;
551 }
552
553 /**
554  * mei_cl_allocate - allocates cl  structure and sets it up.
555  *
556  * @dev: mei device
557  * Return:  The allocated file or NULL on failure
558  */
559 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
560 {
561         struct mei_cl *cl;
562
563         cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
564         if (!cl)
565                 return NULL;
566
567         mei_cl_init(cl, dev);
568
569         return cl;
570 }
571
572 /**
573  * mei_cl_link - allocate host id in the host map
574  *
575  * @cl: host client
576  * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
577  *
578  * Return: 0 on success
579  *      -EINVAL on incorrect values
580  *      -EMFILE if open count exceeded.
581  */
582 int mei_cl_link(struct mei_cl *cl, int id)
583 {
584         struct mei_device *dev;
585         long open_handle_count;
586
587         if (WARN_ON(!cl || !cl->dev))
588                 return -EINVAL;
589
590         dev = cl->dev;
591
592         /* If Id is not assigned get one*/
593         if (id == MEI_HOST_CLIENT_ID_ANY)
594                 id = find_first_zero_bit(dev->host_clients_map,
595                                         MEI_CLIENTS_MAX);
596
597         if (id >= MEI_CLIENTS_MAX) {
598                 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
599                 return -EMFILE;
600         }
601
602         open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
603         if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
604                 dev_err(dev->dev, "open_handle_count exceeded %d",
605                         MEI_MAX_OPEN_HANDLE_COUNT);
606                 return -EMFILE;
607         }
608
609         dev->open_handle_count++;
610
611         cl->host_client_id = id;
612         list_add_tail(&cl->link, &dev->file_list);
613
614         set_bit(id, dev->host_clients_map);
615
616         cl->state = MEI_FILE_INITIALIZING;
617
618         cl_dbg(dev, cl, "link cl\n");
619         return 0;
620 }
621
622 /**
623  * mei_cl_unlink - remove me_cl from the list
624  *
625  * @cl: host client
626  *
627  * Return: always 0
628  */
629 int mei_cl_unlink(struct mei_cl *cl)
630 {
631         struct mei_device *dev;
632
633         /* don't shout on error exit path */
634         if (!cl)
635                 return 0;
636
637         /* wd and amthif might not be initialized */
638         if (!cl->dev)
639                 return 0;
640
641         dev = cl->dev;
642
643         cl_dbg(dev, cl, "unlink client");
644
645         if (dev->open_handle_count > 0)
646                 dev->open_handle_count--;
647
648         /* never clear the 0 bit */
649         if (cl->host_client_id)
650                 clear_bit(cl->host_client_id, dev->host_clients_map);
651
652         list_del_init(&cl->link);
653
654         cl->state = MEI_FILE_INITIALIZING;
655
656         return 0;
657 }
658
659
660 void mei_host_client_init(struct work_struct *work)
661 {
662         struct mei_device *dev =
663                 container_of(work, struct mei_device, init_work);
664         struct mei_me_client *me_cl;
665
666         mutex_lock(&dev->device_lock);
667
668
669         me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
670         if (me_cl)
671                 mei_amthif_host_init(dev);
672         mei_me_cl_put(me_cl);
673
674         me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
675         if (me_cl)
676                 mei_wd_host_init(dev);
677         mei_me_cl_put(me_cl);
678
679         me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid);
680         if (me_cl)
681                 mei_nfc_host_init(dev);
682         mei_me_cl_put(me_cl);
683
684
685         dev->dev_state = MEI_DEV_ENABLED;
686         dev->reset_count = 0;
687         mutex_unlock(&dev->device_lock);
688
689         pm_runtime_mark_last_busy(dev->dev);
690         dev_dbg(dev->dev, "rpm: autosuspend\n");
691         pm_runtime_autosuspend(dev->dev);
692 }
693
694 /**
695  * mei_hbuf_acquire - try to acquire host buffer
696  *
697  * @dev: the device structure
698  * Return: true if host buffer was acquired
699  */
700 bool mei_hbuf_acquire(struct mei_device *dev)
701 {
702         if (mei_pg_state(dev) == MEI_PG_ON ||
703             dev->pg_event == MEI_PG_EVENT_WAIT) {
704                 dev_dbg(dev->dev, "device is in pg\n");
705                 return false;
706         }
707
708         if (!dev->hbuf_is_ready) {
709                 dev_dbg(dev->dev, "hbuf is not ready\n");
710                 return false;
711         }
712
713         dev->hbuf_is_ready = false;
714
715         return true;
716 }
717
718 /**
719  * mei_cl_set_disconnected - set disconnected state and clear
720  *   associated states and resources
721  *
722  * @cl: host client
723  */
724 void mei_cl_set_disconnected(struct mei_cl *cl)
725 {
726         struct mei_device *dev = cl->dev;
727
728         if (cl->state == MEI_FILE_DISCONNECTED ||
729             cl->state == MEI_FILE_INITIALIZING)
730                 return;
731
732         cl->state = MEI_FILE_DISCONNECTED;
733         mei_io_list_flush(&dev->ctrl_rd_list, cl);
734         mei_io_list_flush(&dev->ctrl_wr_list, cl);
735         cl->mei_flow_ctrl_creds = 0;
736         cl->timer_count = 0;
737 }
738
739 /*
740  * mei_cl_send_disconnect - send disconnect request
741  *
742  * @cl: host client
743  * @cb: callback block
744  *
745  * Return: 0, OK; otherwise, error.
746  */
747 static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
748 {
749         struct mei_device *dev;
750         int ret;
751
752         dev = cl->dev;
753
754         ret = mei_hbm_cl_disconnect_req(dev, cl);
755         cl->status = ret;
756         if (ret) {
757                 cl->state = MEI_FILE_DISCONNECT_REPLY;
758                 return ret;
759         }
760
761         list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
762         cl->timer_count = MEI_CONNECT_TIMEOUT;
763
764         return 0;
765 }
766
767 /**
768  * mei_cl_irq_disconnect - processes close related operation from
769  *      interrupt thread context - send disconnect request
770  *
771  * @cl: client
772  * @cb: callback block.
773  * @cmpl_list: complete list.
774  *
775  * Return: 0, OK; otherwise, error.
776  */
777 int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
778                             struct mei_cl_cb *cmpl_list)
779 {
780         struct mei_device *dev = cl->dev;
781         u32 msg_slots;
782         int slots;
783         int ret;
784
785         msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
786         slots = mei_hbuf_empty_slots(dev);
787
788         if (slots < msg_slots)
789                 return -EMSGSIZE;
790
791         ret = mei_cl_send_disconnect(cl, cb);
792         if (ret)
793                 list_move_tail(&cb->list, &cmpl_list->list);
794
795         return ret;
796 }
797
798
799
800 /**
801  * mei_cl_disconnect - disconnect host client from the me one
802  *
803  * @cl: host client
804  *
805  * Locking: called under "dev->device_lock" lock
806  *
807  * Return: 0 on success, <0 on failure.
808  */
809 int mei_cl_disconnect(struct mei_cl *cl)
810 {
811         struct mei_device *dev;
812         struct mei_cl_cb *cb;
813         int rets;
814
815         if (WARN_ON(!cl || !cl->dev))
816                 return -ENODEV;
817
818         dev = cl->dev;
819
820         cl_dbg(dev, cl, "disconnecting");
821
822         if (!mei_cl_is_connected(cl))
823                 return 0;
824
825         rets = pm_runtime_get(dev->dev);
826         if (rets < 0 && rets != -EINPROGRESS) {
827                 pm_runtime_put_noidle(dev->dev);
828                 cl_err(dev, cl, "rpm: get failed %d\n", rets);
829                 return rets;
830         }
831
832         cl->state = MEI_FILE_DISCONNECTING;
833
834         cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL);
835         rets = cb ? 0 : -ENOMEM;
836         if (rets)
837                 goto out;
838
839         cl_dbg(dev, cl, "add disconnect cb to control write list\n");
840         list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
841
842         if (mei_hbuf_acquire(dev)) {
843                 rets = mei_cl_send_disconnect(cl, cb);
844                 if (rets) {
845                         cl_err(dev, cl, "failed to disconnect.\n");
846                         goto out;
847                 }
848         }
849
850         mutex_unlock(&dev->device_lock);
851         wait_event_timeout(cl->wait, cl->state == MEI_FILE_DISCONNECT_REPLY,
852                            mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
853         mutex_lock(&dev->device_lock);
854
855         rets = cl->status;
856         if (cl->state != MEI_FILE_DISCONNECT_REPLY) {
857                 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
858                 rets = -ETIME;
859         }
860
861 out:
862         /* we disconnect also on error */
863         mei_cl_set_disconnected(cl);
864         if (!rets)
865                 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
866
867         cl_dbg(dev, cl, "rpm: autosuspend\n");
868         pm_runtime_mark_last_busy(dev->dev);
869         pm_runtime_put_autosuspend(dev->dev);
870
871         mei_io_cb_free(cb);
872         return rets;
873 }
874
875
876 /**
877  * mei_cl_is_other_connecting - checks if other
878  *    client with the same me client id is connecting
879  *
880  * @cl: private data of the file object
881  *
882  * Return: true if other client is connected, false - otherwise.
883  */
884 static bool mei_cl_is_other_connecting(struct mei_cl *cl)
885 {
886         struct mei_device *dev;
887         struct mei_cl_cb *cb;
888
889         dev = cl->dev;
890
891         list_for_each_entry(cb, &dev->ctrl_rd_list.list, list) {
892                 if (cb->fop_type == MEI_FOP_CONNECT &&
893                     cl->me_client_id == cb->cl->me_client_id)
894                         return true;
895         }
896
897         return false;
898 }
899
900 /**
901  * mei_cl_send_connect - send connect request
902  *
903  * @cl: host client
904  * @cb: callback block
905  *
906  * Return: 0, OK; otherwise, error.
907  */
908 static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
909 {
910         struct mei_device *dev;
911         int ret;
912
913         dev = cl->dev;
914
915         ret = mei_hbm_cl_connect_req(dev, cl);
916         cl->status = ret;
917         if (ret) {
918                 cl->state = MEI_FILE_DISCONNECT_REPLY;
919                 return ret;
920         }
921
922         list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
923         cl->timer_count = MEI_CONNECT_TIMEOUT;
924         return 0;
925 }
926
927 /**
928  * mei_cl_irq_connect - send connect request in irq_thread context
929  *
930  * @cl: host client
931  * @cb: callback block
932  * @cmpl_list: complete list
933  *
934  * Return: 0, OK; otherwise, error.
935  */
936 int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
937                               struct mei_cl_cb *cmpl_list)
938 {
939         struct mei_device *dev = cl->dev;
940         u32 msg_slots;
941         int slots;
942         int rets;
943
944         msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
945         slots = mei_hbuf_empty_slots(dev);
946
947         if (mei_cl_is_other_connecting(cl))
948                 return 0;
949
950         if (slots < msg_slots)
951                 return -EMSGSIZE;
952
953         rets = mei_cl_send_connect(cl, cb);
954         if (rets)
955                 list_move_tail(&cb->list, &cmpl_list->list);
956
957         return rets;
958 }
959
960 /**
961  * mei_cl_connect - connect host client to the me one
962  *
963  * @cl: host client
964  * @file: pointer to file structure
965  *
966  * Locking: called under "dev->device_lock" lock
967  *
968  * Return: 0 on success, <0 on failure.
969  */
970 int mei_cl_connect(struct mei_cl *cl, struct file *file)
971 {
972         struct mei_device *dev;
973         struct mei_cl_cb *cb;
974         int rets;
975
976         if (WARN_ON(!cl || !cl->dev))
977                 return -ENODEV;
978
979         dev = cl->dev;
980
981         rets = pm_runtime_get(dev->dev);
982         if (rets < 0 && rets != -EINPROGRESS) {
983                 pm_runtime_put_noidle(dev->dev);
984                 cl_err(dev, cl, "rpm: get failed %d\n", rets);
985                 return rets;
986         }
987
988         cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file);
989         rets = cb ? 0 : -ENOMEM;
990         if (rets)
991                 goto out;
992
993         cl->state = MEI_FILE_CONNECTING;
994         list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
995
996         /* run hbuf acquire last so we don't have to undo */
997         if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
998                 rets = mei_cl_send_connect(cl, cb);
999                 if (rets)
1000                         goto out;
1001         }
1002
1003         mutex_unlock(&dev->device_lock);
1004         wait_event_timeout(cl->wait,
1005                         (cl->state == MEI_FILE_CONNECTED ||
1006                          cl->state == MEI_FILE_DISCONNECT_REPLY),
1007                         mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1008         mutex_lock(&dev->device_lock);
1009
1010         if (!mei_cl_is_connected(cl)) {
1011                 /* timeout or something went really wrong */
1012                 if (!cl->status)
1013                         cl->status = -EFAULT;
1014         }
1015
1016         rets = cl->status;
1017 out:
1018         cl_dbg(dev, cl, "rpm: autosuspend\n");
1019         pm_runtime_mark_last_busy(dev->dev);
1020         pm_runtime_put_autosuspend(dev->dev);
1021
1022         mei_io_cb_free(cb);
1023
1024         if (!mei_cl_is_connected(cl))
1025                 mei_cl_set_disconnected(cl);
1026
1027         return rets;
1028 }
1029
1030 /**
1031  * mei_cl_alloc_linked - allocate and link host client
1032  *
1033  * @dev: the device structure
1034  * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
1035  *
1036  * Return: cl on success ERR_PTR on failure
1037  */
1038 struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id)
1039 {
1040         struct mei_cl *cl;
1041         int ret;
1042
1043         cl = mei_cl_allocate(dev);
1044         if (!cl) {
1045                 ret = -ENOMEM;
1046                 goto err;
1047         }
1048
1049         ret = mei_cl_link(cl, id);
1050         if (ret)
1051                 goto err;
1052
1053         return cl;
1054 err:
1055         kfree(cl);
1056         return ERR_PTR(ret);
1057 }
1058
1059
1060
1061 /**
1062  * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
1063  *
1064  * @cl: private data of the file object
1065  *
1066  * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
1067  *      -ENOENT if mei_cl is not present
1068  *      -EINVAL if single_recv_buf == 0
1069  */
1070 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
1071 {
1072         struct mei_device *dev;
1073         struct mei_me_client *me_cl;
1074         int rets = 0;
1075
1076         if (WARN_ON(!cl || !cl->dev))
1077                 return -EINVAL;
1078
1079         dev = cl->dev;
1080
1081         if (cl->mei_flow_ctrl_creds > 0)
1082                 return 1;
1083
1084         me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
1085         if (!me_cl) {
1086                 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
1087                 return -ENOENT;
1088         }
1089
1090         if (me_cl->mei_flow_ctrl_creds > 0) {
1091                 rets = 1;
1092                 if (WARN_ON(me_cl->props.single_recv_buf == 0))
1093                         rets = -EINVAL;
1094         }
1095         mei_me_cl_put(me_cl);
1096         return rets;
1097 }
1098
1099 /**
1100  * mei_cl_flow_ctrl_reduce - reduces flow_control.
1101  *
1102  * @cl: private data of the file object
1103  *
1104  * Return:
1105  *      0 on success
1106  *      -ENOENT when me client is not found
1107  *      -EINVAL when ctrl credits are <= 0
1108  */
1109 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
1110 {
1111         struct mei_device *dev;
1112         struct mei_me_client *me_cl;
1113         int rets;
1114
1115         if (WARN_ON(!cl || !cl->dev))
1116                 return -EINVAL;
1117
1118         dev = cl->dev;
1119
1120         me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
1121         if (!me_cl) {
1122                 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
1123                 return -ENOENT;
1124         }
1125
1126         if (me_cl->props.single_recv_buf) {
1127                 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) {
1128                         rets = -EINVAL;
1129                         goto out;
1130                 }
1131                 me_cl->mei_flow_ctrl_creds--;
1132         } else {
1133                 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) {
1134                         rets = -EINVAL;
1135                         goto out;
1136                 }
1137                 cl->mei_flow_ctrl_creds--;
1138         }
1139         rets = 0;
1140 out:
1141         mei_me_cl_put(me_cl);
1142         return rets;
1143 }
1144
1145 /**
1146  * mei_cl_read_start - the start read client message function.
1147  *
1148  * @cl: host client
1149  * @length: number of bytes to read
1150  * @fp: pointer to file structure
1151  *
1152  * Return: 0 on success, <0 on failure.
1153  */
1154 int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp)
1155 {
1156         struct mei_device *dev;
1157         struct mei_cl_cb *cb;
1158         struct mei_me_client *me_cl;
1159         int rets;
1160
1161         if (WARN_ON(!cl || !cl->dev))
1162                 return -ENODEV;
1163
1164         dev = cl->dev;
1165
1166         if (!mei_cl_is_connected(cl))
1167                 return -ENODEV;
1168
1169         /* HW currently supports only one pending read */
1170         if (!list_empty(&cl->rd_pending))
1171                 return -EBUSY;
1172
1173         me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
1174         if (!me_cl) {
1175                 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
1176                 return  -ENOTTY;
1177         }
1178         /* always allocate at least client max message */
1179         length = max_t(size_t, length, me_cl->props.max_msg_length);
1180         mei_me_cl_put(me_cl);
1181
1182         rets = pm_runtime_get(dev->dev);
1183         if (rets < 0 && rets != -EINPROGRESS) {
1184                 pm_runtime_put_noidle(dev->dev);
1185                 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1186                 return rets;
1187         }
1188
1189         cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp);
1190         rets = cb ? 0 : -ENOMEM;
1191         if (rets)
1192                 goto out;
1193
1194         if (mei_hbuf_acquire(dev)) {
1195                 rets = mei_hbm_cl_flow_control_req(dev, cl);
1196                 if (rets < 0)
1197                         goto out;
1198
1199                 list_add_tail(&cb->list, &cl->rd_pending);
1200         } else {
1201                 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1202         }
1203
1204 out:
1205         cl_dbg(dev, cl, "rpm: autosuspend\n");
1206         pm_runtime_mark_last_busy(dev->dev);
1207         pm_runtime_put_autosuspend(dev->dev);
1208
1209         if (rets)
1210                 mei_io_cb_free(cb);
1211
1212         return rets;
1213 }
1214
1215 /**
1216  * mei_cl_irq_write - write a message to device
1217  *      from the interrupt thread context
1218  *
1219  * @cl: client
1220  * @cb: callback block.
1221  * @cmpl_list: complete list.
1222  *
1223  * Return: 0, OK; otherwise error.
1224  */
1225 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1226                      struct mei_cl_cb *cmpl_list)
1227 {
1228         struct mei_device *dev;
1229         struct mei_msg_data *buf;
1230         struct mei_msg_hdr mei_hdr;
1231         size_t len;
1232         u32 msg_slots;
1233         int slots;
1234         int rets;
1235
1236         if (WARN_ON(!cl || !cl->dev))
1237                 return -ENODEV;
1238
1239         dev = cl->dev;
1240
1241         buf = &cb->buf;
1242
1243         rets = mei_cl_flow_ctrl_creds(cl);
1244         if (rets < 0)
1245                 return rets;
1246
1247         if (rets == 0) {
1248                 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1249                 return 0;
1250         }
1251
1252         slots = mei_hbuf_empty_slots(dev);
1253         len = buf->size - cb->buf_idx;
1254         msg_slots = mei_data2slots(len);
1255
1256         mei_hdr.host_addr = cl->host_client_id;
1257         mei_hdr.me_addr = cl->me_client_id;
1258         mei_hdr.reserved = 0;
1259         mei_hdr.internal = cb->internal;
1260
1261         if (slots >= msg_slots) {
1262                 mei_hdr.length = len;
1263                 mei_hdr.msg_complete = 1;
1264         /* Split the message only if we can write the whole host buffer */
1265         } else if (slots == dev->hbuf_depth) {
1266                 msg_slots = slots;
1267                 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
1268                 mei_hdr.length = len;
1269                 mei_hdr.msg_complete = 0;
1270         } else {
1271                 /* wait for next time the host buffer is empty */
1272                 return 0;
1273         }
1274
1275         cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
1276                         cb->buf.size, cb->buf_idx);
1277
1278         rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
1279         if (rets) {
1280                 cl->status = rets;
1281                 list_move_tail(&cb->list, &cmpl_list->list);
1282                 return rets;
1283         }
1284
1285         cl->status = 0;
1286         cl->writing_state = MEI_WRITING;
1287         cb->buf_idx += mei_hdr.length;
1288         cb->completed = mei_hdr.msg_complete == 1;
1289
1290         if (mei_hdr.msg_complete) {
1291                 if (mei_cl_flow_ctrl_reduce(cl))
1292                         return -EIO;
1293                 list_move_tail(&cb->list, &dev->write_waiting_list.list);
1294         }
1295
1296         return 0;
1297 }
1298
1299 /**
1300  * mei_cl_write - submit a write cb to mei device
1301  *      assumes device_lock is locked
1302  *
1303  * @cl: host client
1304  * @cb: write callback with filled data
1305  * @blocking: block until completed
1306  *
1307  * Return: number of bytes sent on success, <0 on failure.
1308  */
1309 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
1310 {
1311         struct mei_device *dev;
1312         struct mei_msg_data *buf;
1313         struct mei_msg_hdr mei_hdr;
1314         int rets;
1315
1316
1317         if (WARN_ON(!cl || !cl->dev))
1318                 return -ENODEV;
1319
1320         if (WARN_ON(!cb))
1321                 return -EINVAL;
1322
1323         dev = cl->dev;
1324
1325
1326         buf = &cb->buf;
1327
1328         cl_dbg(dev, cl, "size=%d\n", buf->size);
1329
1330         rets = pm_runtime_get(dev->dev);
1331         if (rets < 0 && rets != -EINPROGRESS) {
1332                 pm_runtime_put_noidle(dev->dev);
1333                 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1334                 return rets;
1335         }
1336
1337         cb->buf_idx = 0;
1338         cl->writing_state = MEI_IDLE;
1339
1340         mei_hdr.host_addr = cl->host_client_id;
1341         mei_hdr.me_addr = cl->me_client_id;
1342         mei_hdr.reserved = 0;
1343         mei_hdr.msg_complete = 0;
1344         mei_hdr.internal = cb->internal;
1345
1346         rets = mei_cl_flow_ctrl_creds(cl);
1347         if (rets < 0)
1348                 goto err;
1349
1350         if (rets == 0) {
1351                 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1352                 rets = buf->size;
1353                 goto out;
1354         }
1355         if (!mei_hbuf_acquire(dev)) {
1356                 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1357                 rets = buf->size;
1358                 goto out;
1359         }
1360
1361         /* Check for a maximum length */
1362         if (buf->size > mei_hbuf_max_len(dev)) {
1363                 mei_hdr.length = mei_hbuf_max_len(dev);
1364                 mei_hdr.msg_complete = 0;
1365         } else {
1366                 mei_hdr.length = buf->size;
1367                 mei_hdr.msg_complete = 1;
1368         }
1369
1370         rets = mei_write_message(dev, &mei_hdr, buf->data);
1371         if (rets)
1372                 goto err;
1373
1374         cl->writing_state = MEI_WRITING;
1375         cb->buf_idx = mei_hdr.length;
1376         cb->completed = mei_hdr.msg_complete == 1;
1377
1378 out:
1379         if (mei_hdr.msg_complete) {
1380                 rets = mei_cl_flow_ctrl_reduce(cl);
1381                 if (rets < 0)
1382                         goto err;
1383
1384                 list_add_tail(&cb->list, &dev->write_waiting_list.list);
1385         } else {
1386                 list_add_tail(&cb->list, &dev->write_list.list);
1387         }
1388
1389
1390         if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1391
1392                 mutex_unlock(&dev->device_lock);
1393                 rets = wait_event_interruptible(cl->tx_wait,
1394                                 cl->writing_state == MEI_WRITE_COMPLETE);
1395                 mutex_lock(&dev->device_lock);
1396                 /* wait_event_interruptible returns -ERESTARTSYS */
1397                 if (rets) {
1398                         if (signal_pending(current))
1399                                 rets = -EINTR;
1400                         goto err;
1401                 }
1402         }
1403
1404         rets = buf->size;
1405 err:
1406         cl_dbg(dev, cl, "rpm: autosuspend\n");
1407         pm_runtime_mark_last_busy(dev->dev);
1408         pm_runtime_put_autosuspend(dev->dev);
1409
1410         return rets;
1411 }
1412
1413
1414 /**
1415  * mei_cl_complete - processes completed operation for a client
1416  *
1417  * @cl: private data of the file object.
1418  * @cb: callback block.
1419  */
1420 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1421 {
1422         switch (cb->fop_type) {
1423         case MEI_FOP_WRITE:
1424                 mei_io_cb_free(cb);
1425                 cl->writing_state = MEI_WRITE_COMPLETE;
1426                 if (waitqueue_active(&cl->tx_wait))
1427                         wake_up_interruptible(&cl->tx_wait);
1428                 break;
1429
1430         case MEI_FOP_READ:
1431                 list_add_tail(&cb->list, &cl->rd_completed);
1432                 if (waitqueue_active(&cl->rx_wait))
1433                         wake_up_interruptible_all(&cl->rx_wait);
1434                 else
1435                         mei_cl_bus_rx_event(cl);
1436                 break;
1437
1438         case MEI_FOP_CONNECT:
1439         case MEI_FOP_DISCONNECT:
1440                 if (waitqueue_active(&cl->wait))
1441                         wake_up(&cl->wait);
1442
1443                 break;
1444         default:
1445                 BUG_ON(0);
1446         }
1447 }
1448
1449
1450 /**
1451  * mei_cl_all_disconnect - disconnect forcefully all connected clients
1452  *
1453  * @dev: mei device
1454  */
1455 void mei_cl_all_disconnect(struct mei_device *dev)
1456 {
1457         struct mei_cl *cl;
1458
1459         list_for_each_entry(cl, &dev->file_list, link)
1460                 mei_cl_set_disconnected(cl);
1461 }
1462
1463
1464 /**
1465  * mei_cl_all_wakeup  - wake up all readers and writers they can be interrupted
1466  *
1467  * @dev: mei device
1468  */
1469 void mei_cl_all_wakeup(struct mei_device *dev)
1470 {
1471         struct mei_cl *cl;
1472
1473         list_for_each_entry(cl, &dev->file_list, link) {
1474                 if (waitqueue_active(&cl->rx_wait)) {
1475                         cl_dbg(dev, cl, "Waking up reading client!\n");
1476                         wake_up_interruptible(&cl->rx_wait);
1477                 }
1478                 if (waitqueue_active(&cl->tx_wait)) {
1479                         cl_dbg(dev, cl, "Waking up writing client!\n");
1480                         wake_up_interruptible(&cl->tx_wait);
1481                 }
1482         }
1483 }
1484
1485 /**
1486  * mei_cl_all_write_clear - clear all pending writes
1487  *
1488  * @dev: mei device
1489  */
1490 void mei_cl_all_write_clear(struct mei_device *dev)
1491 {
1492         mei_io_list_free(&dev->write_list, NULL);
1493         mei_io_list_free(&dev->write_waiting_list, NULL);
1494 }
1495
1496