mei: use consistent naming for TX control flow credits
[linux-2.6-block.git] / drivers / misc / mei / client.c
CommitLineData
ab841160
OW
1/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
733ba91c 4 * Copyright (c) 2003-2012, Intel Corporation.
ab841160
OW
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
ab841160 17#include <linux/sched.h>
9ca9050b
TW
18#include <linux/wait.h>
19#include <linux/delay.h>
1f180359 20#include <linux/slab.h>
04bb139a 21#include <linux/pm_runtime.h>
ab841160 22
4f3afe1d 23#include <linux/mei.h>
47a73801
TW
24
25#include "mei_dev.h"
0edb23fc 26#include "hbm.h"
90e0b5f1
TW
27#include "client.h"
28
79563db9
TW
29/**
30 * mei_me_cl_init - initialize me client
31 *
32 * @me_cl: me client
33 */
34void 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 */
49struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
50{
b7d88514
TW
51 if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
52 return me_cl;
79563db9 53
b7d88514 54 return NULL;
79563db9
TW
55}
56
57/**
b7d88514 58 * mei_me_cl_release - free me client
79563db9
TW
59 *
60 * Locking: called under "dev->device_lock" lock
61 *
62 * @ref: me_client refcount
63 */
64static 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);
b7d88514 68
79563db9
TW
69 kfree(me_cl);
70}
b7d88514 71
79563db9
TW
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 */
79void 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
90e0b5f1 85/**
d49ed64a 86 * __mei_me_cl_del - delete me client from the list and decrease
b7d88514
TW
87 * reference counter
88 *
89 * @dev: mei device
90 * @me_cl: me client
91 *
92 * Locking: dev->me_clients_rwsem
93 */
94static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
95{
96 if (!me_cl)
97 return;
98
d49ed64a 99 list_del_init(&me_cl->list);
b7d88514
TW
100 mei_me_cl_put(me_cl);
101}
102
d49ed64a
AU
103/**
104 * mei_me_cl_del - delete me client from the list and decrease
105 * reference counter
106 *
107 * @dev: mei device
108 * @me_cl: me client
109 */
110void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
111{
112 down_write(&dev->me_clients_rwsem);
113 __mei_me_cl_del(dev, me_cl);
114 up_write(&dev->me_clients_rwsem);
115}
116
b7d88514
TW
117/**
118 * mei_me_cl_add - add me client to the list
119 *
120 * @dev: mei device
121 * @me_cl: me client
122 */
123void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
124{
125 down_write(&dev->me_clients_rwsem);
126 list_add(&me_cl->list, &dev->me_clients);
127 up_write(&dev->me_clients_rwsem);
128}
129
130/**
131 * __mei_me_cl_by_uuid - locate me client by uuid
79563db9 132 * increases ref count
90e0b5f1
TW
133 *
134 * @dev: mei device
a8605ea2 135 * @uuid: me client uuid
a27a76d3 136 *
a8605ea2 137 * Return: me client or NULL if not found
b7d88514
TW
138 *
139 * Locking: dev->me_clients_rwsem
90e0b5f1 140 */
b7d88514 141static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
d320832f 142 const uuid_le *uuid)
90e0b5f1 143{
5ca2d388 144 struct mei_me_client *me_cl;
b7d88514 145 const uuid_le *pn;
90e0b5f1 146
b7d88514
TW
147 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
148
149 list_for_each_entry(me_cl, &dev->me_clients, list) {
150 pn = &me_cl->props.protocol_name;
151 if (uuid_le_cmp(*uuid, *pn) == 0)
79563db9 152 return mei_me_cl_get(me_cl);
b7d88514 153 }
90e0b5f1 154
d320832f 155 return NULL;
90e0b5f1
TW
156}
157
b7d88514
TW
158/**
159 * mei_me_cl_by_uuid - locate me client by uuid
160 * increases ref count
161 *
162 * @dev: mei device
163 * @uuid: me client uuid
164 *
165 * Return: me client or NULL if not found
166 *
167 * Locking: dev->me_clients_rwsem
168 */
169struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
170 const uuid_le *uuid)
171{
172 struct mei_me_client *me_cl;
173
174 down_read(&dev->me_clients_rwsem);
175 me_cl = __mei_me_cl_by_uuid(dev, uuid);
176 up_read(&dev->me_clients_rwsem);
177
178 return me_cl;
179}
180
90e0b5f1 181/**
a8605ea2 182 * mei_me_cl_by_id - locate me client by client id
79563db9 183 * increases ref count
90e0b5f1
TW
184 *
185 * @dev: the device structure
186 * @client_id: me client id
187 *
a8605ea2 188 * Return: me client or NULL if not found
b7d88514
TW
189 *
190 * Locking: dev->me_clients_rwsem
90e0b5f1 191 */
d320832f 192struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
90e0b5f1 193{
a27a76d3 194
b7d88514
TW
195 struct mei_me_client *__me_cl, *me_cl = NULL;
196
197 down_read(&dev->me_clients_rwsem);
198 list_for_each_entry(__me_cl, &dev->me_clients, list) {
199 if (__me_cl->client_id == client_id) {
200 me_cl = mei_me_cl_get(__me_cl);
201 break;
202 }
203 }
204 up_read(&dev->me_clients_rwsem);
205
206 return me_cl;
207}
208
209/**
210 * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
211 * increases ref count
212 *
213 * @dev: the device structure
214 * @uuid: me client uuid
215 * @client_id: me client id
216 *
217 * Return: me client or null if not found
218 *
219 * Locking: dev->me_clients_rwsem
220 */
221static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
222 const uuid_le *uuid, u8 client_id)
223{
5ca2d388 224 struct mei_me_client *me_cl;
b7d88514
TW
225 const uuid_le *pn;
226
227 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
90e0b5f1 228
b7d88514
TW
229 list_for_each_entry(me_cl, &dev->me_clients, list) {
230 pn = &me_cl->props.protocol_name;
231 if (uuid_le_cmp(*uuid, *pn) == 0 &&
232 me_cl->client_id == client_id)
79563db9 233 return mei_me_cl_get(me_cl);
b7d88514 234 }
79563db9 235
d320832f 236 return NULL;
90e0b5f1 237}
ab841160 238
b7d88514 239
a8605ea2
AU
240/**
241 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
79563db9 242 * increases ref count
a8605ea2
AU
243 *
244 * @dev: the device structure
245 * @uuid: me client uuid
246 * @client_id: me client id
247 *
b7d88514 248 * Return: me client or null if not found
a8605ea2 249 */
d880f329
TW
250struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
251 const uuid_le *uuid, u8 client_id)
252{
253 struct mei_me_client *me_cl;
254
b7d88514
TW
255 down_read(&dev->me_clients_rwsem);
256 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
257 up_read(&dev->me_clients_rwsem);
79563db9 258
b7d88514 259 return me_cl;
d880f329
TW
260}
261
25ca6472 262/**
79563db9 263 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
25ca6472
TW
264 *
265 * @dev: the device structure
266 * @uuid: me client uuid
79563db9
TW
267 *
268 * Locking: called under "dev->device_lock" lock
25ca6472 269 */
79563db9 270void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
25ca6472 271{
b7d88514 272 struct mei_me_client *me_cl;
25ca6472 273
79563db9 274 dev_dbg(dev->dev, "remove %pUl\n", uuid);
b7d88514
TW
275
276 down_write(&dev->me_clients_rwsem);
277 me_cl = __mei_me_cl_by_uuid(dev, uuid);
278 __mei_me_cl_del(dev, me_cl);
279 up_write(&dev->me_clients_rwsem);
79563db9
TW
280}
281
282/**
283 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
284 *
285 * @dev: the device structure
286 * @uuid: me client uuid
287 * @id: me client id
288 *
289 * Locking: called under "dev->device_lock" lock
290 */
291void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
292{
b7d88514 293 struct mei_me_client *me_cl;
79563db9
TW
294
295 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
b7d88514
TW
296
297 down_write(&dev->me_clients_rwsem);
298 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
299 __mei_me_cl_del(dev, me_cl);
300 up_write(&dev->me_clients_rwsem);
25ca6472
TW
301}
302
79563db9
TW
303/**
304 * mei_me_cl_rm_all - remove all me clients
305 *
306 * @dev: the device structure
307 *
308 * Locking: called under "dev->device_lock" lock
309 */
310void mei_me_cl_rm_all(struct mei_device *dev)
311{
312 struct mei_me_client *me_cl, *next;
313
b7d88514 314 down_write(&dev->me_clients_rwsem);
79563db9 315 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
b7d88514
TW
316 __mei_me_cl_del(dev, me_cl);
317 up_write(&dev->me_clients_rwsem);
79563db9
TW
318}
319
9ca9050b 320/**
cc99ecfd 321 * mei_cl_cmp_id - tells if the clients are the same
9ca9050b 322 *
cc99ecfd
TW
323 * @cl1: host client 1
324 * @cl2: host client 2
325 *
a8605ea2 326 * Return: true - if the clients has same host and me ids
cc99ecfd
TW
327 * false - otherwise
328 */
329static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
330 const struct mei_cl *cl2)
331{
332 return cl1 && cl2 &&
333 (cl1->host_client_id == cl2->host_client_id) &&
d49ed64a 334 (mei_cl_me_id(cl1) == mei_cl_me_id(cl2));
cc99ecfd
TW
335}
336
928fa666
TW
337/**
338 * mei_io_cb_free - free mei_cb_private related memory
339 *
340 * @cb: mei callback struct
341 */
342void mei_io_cb_free(struct mei_cl_cb *cb)
343{
344 if (cb == NULL)
345 return;
346
347 list_del(&cb->list);
348 kfree(cb->buf.data);
349 kfree(cb);
350}
351
352/**
353 * mei_io_cb_init - allocate and initialize io callback
354 *
355 * @cl: mei client
356 * @type: operation type
357 * @fp: pointer to file structure
358 *
359 * Return: mei_cl_cb pointer or NULL;
360 */
361struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, enum mei_cb_file_ops type,
f23e2cc4 362 const struct file *fp)
928fa666
TW
363{
364 struct mei_cl_cb *cb;
365
366 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
367 if (!cb)
368 return NULL;
369
370 INIT_LIST_HEAD(&cb->list);
62e8e6ad 371 cb->fp = fp;
928fa666
TW
372 cb->cl = cl;
373 cb->buf_idx = 0;
374 cb->fop_type = type;
375 return cb;
376}
377
cc99ecfd 378/**
3908be6f 379 * __mei_io_list_flush - removes and frees cbs belonging to cl.
cc99ecfd
TW
380 *
381 * @list: an instance of our list structure
382 * @cl: host client, can be NULL for flushing the whole list
383 * @free: whether to free the cbs
9ca9050b 384 */
cc99ecfd
TW
385static void __mei_io_list_flush(struct mei_cl_cb *list,
386 struct mei_cl *cl, bool free)
9ca9050b 387{
928fa666 388 struct mei_cl_cb *cb, *next;
9ca9050b 389
cc99ecfd 390 /* enable removing everything if no cl is specified */
9ca9050b 391 list_for_each_entry_safe(cb, next, &list->list, list) {
140c7553 392 if (!cl || mei_cl_cmp_id(cl, cb->cl)) {
928fa666 393 list_del_init(&cb->list);
cc99ecfd
TW
394 if (free)
395 mei_io_cb_free(cb);
396 }
9ca9050b
TW
397 }
398}
399
cc99ecfd
TW
400/**
401 * mei_io_list_flush - removes list entry belonging to cl.
402 *
403 * @list: An instance of our list structure
404 * @cl: host client
405 */
5456796b 406void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
cc99ecfd
TW
407{
408 __mei_io_list_flush(list, cl, false);
409}
410
cc99ecfd
TW
411/**
412 * mei_io_list_free - removes cb belonging to cl and free them
413 *
414 * @list: An instance of our list structure
415 * @cl: host client
416 */
417static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
418{
419 __mei_io_list_flush(list, cl, true);
420}
421
bca67d68
TW
422/**
423 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
424 *
425 * @cl: host client
426 * @length: size of the buffer
427 * @type: operation type
428 * @fp: associated file pointer (might be NULL)
429 *
430 * Return: cb on success and NULL on failure
431 */
432struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
f23e2cc4
TW
433 enum mei_cb_file_ops type,
434 const struct file *fp)
bca67d68
TW
435{
436 struct mei_cl_cb *cb;
437
438 cb = mei_io_cb_init(cl, type, fp);
439 if (!cb)
440 return NULL;
441
aab3b1a3
AU
442 if (length == 0)
443 return cb;
444
445 cb->buf.data = kmalloc(length, GFP_KERNEL);
446 if (!cb->buf.data) {
bca67d68
TW
447 mei_io_cb_free(cb);
448 return NULL;
449 }
aab3b1a3 450 cb->buf.size = length;
bca67d68
TW
451
452 return cb;
453}
454
a9bed610
TW
455/**
456 * mei_cl_read_cb - find this cl's callback in the read list
457 * for a specific file
458 *
459 * @cl: host client
460 * @fp: file pointer (matching cb file object), may be NULL
461 *
462 * Return: cb on success, NULL if cb is not found
463 */
464struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
465{
466 struct mei_cl_cb *cb;
467
468 list_for_each_entry(cb, &cl->rd_completed, list)
62e8e6ad 469 if (!fp || fp == cb->fp)
a9bed610
TW
470 return cb;
471
472 return NULL;
473}
474
475/**
476 * mei_cl_read_cb_flush - free client's read pending and completed cbs
477 * for a specific file
478 *
479 * @cl: host client
480 * @fp: file pointer (matching cb file object), may be NULL
481 */
482void mei_cl_read_cb_flush(const struct mei_cl *cl, const struct file *fp)
483{
484 struct mei_cl_cb *cb, *next;
485
486 list_for_each_entry_safe(cb, next, &cl->rd_completed, list)
62e8e6ad 487 if (!fp || fp == cb->fp)
a9bed610
TW
488 mei_io_cb_free(cb);
489
490
491 list_for_each_entry_safe(cb, next, &cl->rd_pending, list)
62e8e6ad 492 if (!fp || fp == cb->fp)
a9bed610
TW
493 mei_io_cb_free(cb);
494}
495
9ca9050b
TW
496/**
497 * mei_cl_flush_queues - flushes queue lists belonging to cl.
498 *
9ca9050b 499 * @cl: host client
a9bed610 500 * @fp: file pointer (matching cb file object), may be NULL
ce23139c
AU
501 *
502 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
9ca9050b 503 */
a9bed610 504int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
9ca9050b 505{
c0abffbd
AU
506 struct mei_device *dev;
507
90e0b5f1 508 if (WARN_ON(!cl || !cl->dev))
9ca9050b
TW
509 return -EINVAL;
510
c0abffbd
AU
511 dev = cl->dev;
512
513 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
cc99ecfd
TW
514 mei_io_list_free(&cl->dev->write_list, cl);
515 mei_io_list_free(&cl->dev->write_waiting_list, cl);
9ca9050b
TW
516 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
517 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
518 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
a9bed610
TW
519
520 mei_cl_read_cb_flush(cl, fp);
521
9ca9050b
TW
522 return 0;
523}
524
ab841160 525
9ca9050b 526/**
83ce0741 527 * mei_cl_init - initializes cl.
9ca9050b
TW
528 *
529 * @cl: host client to be initialized
530 * @dev: mei device
531 */
532void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
533{
534 memset(cl, 0, sizeof(struct mei_cl));
535 init_waitqueue_head(&cl->wait);
536 init_waitqueue_head(&cl->rx_wait);
537 init_waitqueue_head(&cl->tx_wait);
b38a362f 538 init_waitqueue_head(&cl->ev_wait);
a9bed610
TW
539 INIT_LIST_HEAD(&cl->rd_completed);
540 INIT_LIST_HEAD(&cl->rd_pending);
9ca9050b 541 INIT_LIST_HEAD(&cl->link);
9ca9050b 542 cl->writing_state = MEI_IDLE;
3c666182 543 cl->state = MEI_FILE_INITIALIZING;
9ca9050b
TW
544 cl->dev = dev;
545}
546
547/**
548 * mei_cl_allocate - allocates cl structure and sets it up.
549 *
550 * @dev: mei device
a8605ea2 551 * Return: The allocated file or NULL on failure
9ca9050b
TW
552 */
553struct mei_cl *mei_cl_allocate(struct mei_device *dev)
554{
555 struct mei_cl *cl;
556
557 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
558 if (!cl)
559 return NULL;
560
561 mei_cl_init(cl, dev);
562
563 return cl;
564}
565
3908be6f
AU
566/**
567 * mei_cl_link - allocate host id in the host map
9ca9050b 568 *
3908be6f 569 * @cl: host client
393b148f 570 *
a8605ea2 571 * Return: 0 on success
9ca9050b 572 * -EINVAL on incorrect values
03b8d341 573 * -EMFILE if open count exceeded.
9ca9050b 574 */
7851e008 575int mei_cl_link(struct mei_cl *cl)
9ca9050b 576{
90e0b5f1 577 struct mei_device *dev;
22f96a0e 578 long open_handle_count;
7851e008 579 int id;
9ca9050b 580
781d0d89 581 if (WARN_ON(!cl || !cl->dev))
9ca9050b
TW
582 return -EINVAL;
583
90e0b5f1
TW
584 dev = cl->dev;
585
7851e008 586 id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
781d0d89 587 if (id >= MEI_CLIENTS_MAX) {
2bf94cab 588 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
e036cc57
TW
589 return -EMFILE;
590 }
591
22f96a0e
TW
592 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
593 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
2bf94cab 594 dev_err(dev->dev, "open_handle_count exceeded %d",
e036cc57
TW
595 MEI_MAX_OPEN_HANDLE_COUNT);
596 return -EMFILE;
9ca9050b
TW
597 }
598
781d0d89
TW
599 dev->open_handle_count++;
600
601 cl->host_client_id = id;
602 list_add_tail(&cl->link, &dev->file_list);
603
604 set_bit(id, dev->host_clients_map);
605
606 cl->state = MEI_FILE_INITIALIZING;
607
c0abffbd 608 cl_dbg(dev, cl, "link cl\n");
781d0d89 609 return 0;
9ca9050b 610}
781d0d89 611
9ca9050b 612/**
d49ed64a 613 * mei_cl_unlink - remove host client from the list
9ca9050b 614 *
393b148f 615 * @cl: host client
ce23139c
AU
616 *
617 * Return: always 0
9ca9050b 618 */
90e0b5f1 619int mei_cl_unlink(struct mei_cl *cl)
9ca9050b 620{
90e0b5f1 621 struct mei_device *dev;
90e0b5f1 622
781d0d89
TW
623 /* don't shout on error exit path */
624 if (!cl)
625 return 0;
626
fdd9b865 627 /* amthif might not be initialized */
8e9a4a9a
TW
628 if (!cl->dev)
629 return 0;
90e0b5f1
TW
630
631 dev = cl->dev;
632
a14c44d8
TW
633 cl_dbg(dev, cl, "unlink client");
634
22f96a0e
TW
635 if (dev->open_handle_count > 0)
636 dev->open_handle_count--;
637
638 /* never clear the 0 bit */
639 if (cl->host_client_id)
640 clear_bit(cl->host_client_id, dev->host_clients_map);
641
642 list_del_init(&cl->link);
643
644 cl->state = MEI_FILE_INITIALIZING;
645
90e0b5f1 646 return 0;
9ca9050b
TW
647}
648
025fb792 649void mei_host_client_init(struct mei_device *dev)
9ca9050b 650{
9ca9050b 651 dev->dev_state = MEI_DEV_ENABLED;
6adb8efb 652 dev->reset_count = 0;
04bb139a 653
025fb792 654 schedule_work(&dev->bus_rescan_work);
6009595a 655
2bf94cab
TW
656 pm_runtime_mark_last_busy(dev->dev);
657 dev_dbg(dev->dev, "rpm: autosuspend\n");
658 pm_runtime_autosuspend(dev->dev);
9ca9050b
TW
659}
660
6aae48ff 661/**
a8605ea2 662 * mei_hbuf_acquire - try to acquire host buffer
6aae48ff
TW
663 *
664 * @dev: the device structure
a8605ea2 665 * Return: true if host buffer was acquired
6aae48ff
TW
666 */
667bool mei_hbuf_acquire(struct mei_device *dev)
668{
04bb139a 669 if (mei_pg_state(dev) == MEI_PG_ON ||
3dc196ea 670 mei_pg_in_transition(dev)) {
2bf94cab 671 dev_dbg(dev->dev, "device is in pg\n");
04bb139a
TW
672 return false;
673 }
674
6aae48ff 675 if (!dev->hbuf_is_ready) {
2bf94cab 676 dev_dbg(dev->dev, "hbuf is not ready\n");
6aae48ff
TW
677 return false;
678 }
679
680 dev->hbuf_is_ready = false;
681
682 return true;
683}
9ca9050b 684
a4307fe4
AU
685/**
686 * mei_cl_wake_all - wake up readers, writers and event waiters so
687 * they can be interrupted
688 *
689 * @cl: host client
690 */
691static void mei_cl_wake_all(struct mei_cl *cl)
692{
693 struct mei_device *dev = cl->dev;
694
695 /* synchronized under device mutex */
696 if (waitqueue_active(&cl->rx_wait)) {
697 cl_dbg(dev, cl, "Waking up reading client!\n");
698 wake_up_interruptible(&cl->rx_wait);
699 }
700 /* synchronized under device mutex */
701 if (waitqueue_active(&cl->tx_wait)) {
702 cl_dbg(dev, cl, "Waking up writing client!\n");
703 wake_up_interruptible(&cl->tx_wait);
704 }
705 /* synchronized under device mutex */
706 if (waitqueue_active(&cl->ev_wait)) {
707 cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
708 wake_up_interruptible(&cl->ev_wait);
709 }
7ff4bdd4
AU
710 /* synchronized under device mutex */
711 if (waitqueue_active(&cl->wait)) {
712 cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
69f1804a 713 wake_up(&cl->wait);
7ff4bdd4 714 }
a4307fe4
AU
715}
716
3c666182
TW
717/**
718 * mei_cl_set_disconnected - set disconnected state and clear
719 * associated states and resources
720 *
721 * @cl: host client
722 */
723void mei_cl_set_disconnected(struct mei_cl *cl)
724{
725 struct mei_device *dev = cl->dev;
726
727 if (cl->state == MEI_FILE_DISCONNECTED ||
728 cl->state == MEI_FILE_INITIALIZING)
729 return;
730
731 cl->state = MEI_FILE_DISCONNECTED;
a4307fe4
AU
732 mei_io_list_free(&dev->write_list, cl);
733 mei_io_list_free(&dev->write_waiting_list, cl);
3c666182
TW
734 mei_io_list_flush(&dev->ctrl_rd_list, cl);
735 mei_io_list_flush(&dev->ctrl_wr_list, cl);
a4307fe4 736 mei_cl_wake_all(cl);
46978ada 737 cl->rx_flow_ctrl_creds = 0;
4034b81b 738 cl->tx_flow_ctrl_creds = 0;
3c666182 739 cl->timer_count = 0;
d49ed64a 740
a03d77f6
AU
741 if (!cl->me_cl)
742 return;
743
744 if (!WARN_ON(cl->me_cl->connect_count == 0))
745 cl->me_cl->connect_count--;
746
c241e9b1 747 if (cl->me_cl->connect_count == 0)
4034b81b 748 cl->me_cl->tx_flow_ctrl_creds = 0;
c241e9b1 749
d49ed64a
AU
750 mei_me_cl_put(cl->me_cl);
751 cl->me_cl = NULL;
3c666182
TW
752}
753
a03d77f6
AU
754static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
755{
1df629ef 756 if (!mei_me_cl_get(me_cl))
a03d77f6
AU
757 return -ENOENT;
758
1df629ef
AU
759 /* only one connection is allowed for fixed address clients */
760 if (me_cl->props.fixed_address) {
761 if (me_cl->connect_count) {
762 mei_me_cl_put(me_cl);
763 return -EBUSY;
764 }
765 }
766
767 cl->me_cl = me_cl;
a03d77f6
AU
768 cl->state = MEI_FILE_CONNECTING;
769 cl->me_cl->connect_count++;
770
771 return 0;
772}
773
3c666182
TW
774/*
775 * mei_cl_send_disconnect - send disconnect request
776 *
777 * @cl: host client
778 * @cb: callback block
779 *
780 * Return: 0, OK; otherwise, error.
781 */
782static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
783{
784 struct mei_device *dev;
785 int ret;
786
787 dev = cl->dev;
788
789 ret = mei_hbm_cl_disconnect_req(dev, cl);
790 cl->status = ret;
791 if (ret) {
792 cl->state = MEI_FILE_DISCONNECT_REPLY;
793 return ret;
794 }
795
796 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
797 cl->timer_count = MEI_CONNECT_TIMEOUT;
798
799 return 0;
800}
801
802/**
803 * mei_cl_irq_disconnect - processes close related operation from
804 * interrupt thread context - send disconnect request
805 *
806 * @cl: client
807 * @cb: callback block.
808 * @cmpl_list: complete list.
809 *
810 * Return: 0, OK; otherwise, error.
811 */
812int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
813 struct mei_cl_cb *cmpl_list)
814{
815 struct mei_device *dev = cl->dev;
816 u32 msg_slots;
817 int slots;
818 int ret;
819
820 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
821 slots = mei_hbuf_empty_slots(dev);
822
823 if (slots < msg_slots)
824 return -EMSGSIZE;
825
826 ret = mei_cl_send_disconnect(cl, cb);
827 if (ret)
828 list_move_tail(&cb->list, &cmpl_list->list);
829
830 return ret;
831}
832
9ca9050b 833/**
18901357
AU
834 * __mei_cl_disconnect - disconnect host client from the me one
835 * internal function runtime pm has to be already acquired
9ca9050b 836 *
90e0b5f1 837 * @cl: host client
9ca9050b 838 *
a8605ea2 839 * Return: 0 on success, <0 on failure.
9ca9050b 840 */
18901357 841static int __mei_cl_disconnect(struct mei_cl *cl)
9ca9050b 842{
90e0b5f1 843 struct mei_device *dev;
9ca9050b 844 struct mei_cl_cb *cb;
fe2f17eb 845 int rets;
9ca9050b 846
90e0b5f1
TW
847 dev = cl->dev;
848
3c666182
TW
849 cl->state = MEI_FILE_DISCONNECTING;
850
bca67d68
TW
851 cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL);
852 rets = cb ? 0 : -ENOMEM;
853 if (rets)
3c666182
TW
854 goto out;
855
856 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
857 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
5a8373fb 858
6aae48ff 859 if (mei_hbuf_acquire(dev)) {
3c666182
TW
860 rets = mei_cl_send_disconnect(cl, cb);
861 if (rets) {
c0abffbd 862 cl_err(dev, cl, "failed to disconnect.\n");
3c666182 863 goto out;
9ca9050b 864 }
9ca9050b 865 }
9ca9050b 866
3c666182 867 mutex_unlock(&dev->device_lock);
7ff4bdd4
AU
868 wait_event_timeout(cl->wait,
869 cl->state == MEI_FILE_DISCONNECT_REPLY ||
870 cl->state == MEI_FILE_DISCONNECTED,
3c666182 871 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
9ca9050b 872 mutex_lock(&dev->device_lock);
fe2f17eb 873
3c666182 874 rets = cl->status;
7ff4bdd4
AU
875 if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
876 cl->state != MEI_FILE_DISCONNECTED) {
fe2f17eb
AU
877 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
878 rets = -ETIME;
9ca9050b
TW
879 }
880
3c666182
TW
881out:
882 /* we disconnect also on error */
883 mei_cl_set_disconnected(cl);
884 if (!rets)
885 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
886
18901357
AU
887 mei_io_cb_free(cb);
888 return rets;
889}
890
891/**
892 * mei_cl_disconnect - disconnect host client from the me one
893 *
894 * @cl: host client
895 *
896 * Locking: called under "dev->device_lock" lock
897 *
898 * Return: 0 on success, <0 on failure.
899 */
900int mei_cl_disconnect(struct mei_cl *cl)
901{
902 struct mei_device *dev;
903 int rets;
904
905 if (WARN_ON(!cl || !cl->dev))
906 return -ENODEV;
907
908 dev = cl->dev;
909
910 cl_dbg(dev, cl, "disconnecting");
911
912 if (!mei_cl_is_connected(cl))
913 return 0;
914
915 if (mei_cl_is_fixed_address(cl)) {
916 mei_cl_set_disconnected(cl);
917 return 0;
918 }
919
920 rets = pm_runtime_get(dev->dev);
921 if (rets < 0 && rets != -EINPROGRESS) {
922 pm_runtime_put_noidle(dev->dev);
923 cl_err(dev, cl, "rpm: get failed %d\n", rets);
924 return rets;
925 }
926
927 rets = __mei_cl_disconnect(cl);
928
04bb139a 929 cl_dbg(dev, cl, "rpm: autosuspend\n");
2bf94cab
TW
930 pm_runtime_mark_last_busy(dev->dev);
931 pm_runtime_put_autosuspend(dev->dev);
04bb139a 932
9ca9050b
TW
933 return rets;
934}
935
936
937/**
90e0b5f1
TW
938 * mei_cl_is_other_connecting - checks if other
939 * client with the same me client id is connecting
9ca9050b 940 *
9ca9050b
TW
941 * @cl: private data of the file object
942 *
a8605ea2 943 * Return: true if other client is connected, false - otherwise.
9ca9050b 944 */
0c53357c 945static bool mei_cl_is_other_connecting(struct mei_cl *cl)
9ca9050b 946{
90e0b5f1 947 struct mei_device *dev;
0c53357c 948 struct mei_cl_cb *cb;
90e0b5f1
TW
949
950 dev = cl->dev;
951
0c53357c
TW
952 list_for_each_entry(cb, &dev->ctrl_rd_list.list, list) {
953 if (cb->fop_type == MEI_FOP_CONNECT &&
d49ed64a 954 mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
90e0b5f1 955 return true;
9ca9050b 956 }
90e0b5f1
TW
957
958 return false;
9ca9050b
TW
959}
960
0c53357c
TW
961/**
962 * mei_cl_send_connect - send connect request
963 *
964 * @cl: host client
965 * @cb: callback block
966 *
967 * Return: 0, OK; otherwise, error.
968 */
969static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
970{
971 struct mei_device *dev;
972 int ret;
973
974 dev = cl->dev;
975
976 ret = mei_hbm_cl_connect_req(dev, cl);
977 cl->status = ret;
978 if (ret) {
979 cl->state = MEI_FILE_DISCONNECT_REPLY;
980 return ret;
981 }
982
983 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
984 cl->timer_count = MEI_CONNECT_TIMEOUT;
985 return 0;
986}
987
988/**
989 * mei_cl_irq_connect - send connect request in irq_thread context
990 *
991 * @cl: host client
992 * @cb: callback block
993 * @cmpl_list: complete list
994 *
995 * Return: 0, OK; otherwise, error.
996 */
997int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
998 struct mei_cl_cb *cmpl_list)
999{
1000 struct mei_device *dev = cl->dev;
1001 u32 msg_slots;
1002 int slots;
1003 int rets;
1004
1005 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
1006 slots = mei_hbuf_empty_slots(dev);
1007
1008 if (mei_cl_is_other_connecting(cl))
1009 return 0;
1010
1011 if (slots < msg_slots)
1012 return -EMSGSIZE;
1013
1014 rets = mei_cl_send_connect(cl, cb);
1015 if (rets)
1016 list_move_tail(&cb->list, &cmpl_list->list);
1017
1018 return rets;
1019}
1020
9f81abda 1021/**
83ce0741 1022 * mei_cl_connect - connect host client to the me one
9f81abda
TW
1023 *
1024 * @cl: host client
d49ed64a 1025 * @me_cl: me client
a8605ea2 1026 * @file: pointer to file structure
9f81abda
TW
1027 *
1028 * Locking: called under "dev->device_lock" lock
1029 *
a8605ea2 1030 * Return: 0 on success, <0 on failure.
9f81abda 1031 */
d49ed64a 1032int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
f23e2cc4 1033 const struct file *file)
9f81abda
TW
1034{
1035 struct mei_device *dev;
1036 struct mei_cl_cb *cb;
9f81abda
TW
1037 int rets;
1038
1df629ef 1039 if (WARN_ON(!cl || !cl->dev || !me_cl))
9f81abda
TW
1040 return -ENODEV;
1041
1042 dev = cl->dev;
1043
1df629ef
AU
1044 rets = mei_cl_set_connecting(cl, me_cl);
1045 if (rets)
1046 return rets;
1047
1048 if (mei_cl_is_fixed_address(cl)) {
1049 cl->state = MEI_FILE_CONNECTED;
1050 return 0;
1051 }
1052
2bf94cab 1053 rets = pm_runtime_get(dev->dev);
04bb139a 1054 if (rets < 0 && rets != -EINPROGRESS) {
2bf94cab 1055 pm_runtime_put_noidle(dev->dev);
04bb139a 1056 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1df629ef 1057 goto nortpm;
04bb139a
TW
1058 }
1059
bca67d68
TW
1060 cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file);
1061 rets = cb ? 0 : -ENOMEM;
1062 if (rets)
9f81abda 1063 goto out;
9f81abda 1064
0c53357c
TW
1065 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1066
6aae48ff
TW
1067 /* run hbuf acquire last so we don't have to undo */
1068 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
0c53357c
TW
1069 rets = mei_cl_send_connect(cl, cb);
1070 if (rets)
9f81abda 1071 goto out;
9f81abda
TW
1072 }
1073
1074 mutex_unlock(&dev->device_lock);
12f45ed4 1075 wait_event_timeout(cl->wait,
285e2996 1076 (cl->state == MEI_FILE_CONNECTED ||
7ff4bdd4 1077 cl->state == MEI_FILE_DISCONNECTED ||
18901357 1078 cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
3c666182 1079 cl->state == MEI_FILE_DISCONNECT_REPLY),
285e2996 1080 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
9f81abda
TW
1081 mutex_lock(&dev->device_lock);
1082
f3de9b63 1083 if (!mei_cl_is_connected(cl)) {
18901357
AU
1084 if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
1085 mei_io_list_flush(&dev->ctrl_rd_list, cl);
1086 mei_io_list_flush(&dev->ctrl_wr_list, cl);
1087 /* ignore disconnect return valuue;
1088 * in case of failure reset will be invoked
1089 */
1090 __mei_cl_disconnect(cl);
1091 rets = -EFAULT;
1092 goto out;
1093 }
1094
0c53357c 1095 /* timeout or something went really wrong */
285e2996
AU
1096 if (!cl->status)
1097 cl->status = -EFAULT;
9f81abda
TW
1098 }
1099
1100 rets = cl->status;
9f81abda 1101out:
04bb139a 1102 cl_dbg(dev, cl, "rpm: autosuspend\n");
2bf94cab
TW
1103 pm_runtime_mark_last_busy(dev->dev);
1104 pm_runtime_put_autosuspend(dev->dev);
04bb139a 1105
9f81abda 1106 mei_io_cb_free(cb);
0c53357c 1107
1df629ef 1108nortpm:
0c53357c
TW
1109 if (!mei_cl_is_connected(cl))
1110 mei_cl_set_disconnected(cl);
1111
9f81abda
TW
1112 return rets;
1113}
1114
03b8d341
TW
1115/**
1116 * mei_cl_alloc_linked - allocate and link host client
1117 *
1118 * @dev: the device structure
03b8d341
TW
1119 *
1120 * Return: cl on success ERR_PTR on failure
1121 */
7851e008 1122struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
03b8d341
TW
1123{
1124 struct mei_cl *cl;
1125 int ret;
1126
1127 cl = mei_cl_allocate(dev);
1128 if (!cl) {
1129 ret = -ENOMEM;
1130 goto err;
1131 }
1132
7851e008 1133 ret = mei_cl_link(cl);
03b8d341
TW
1134 if (ret)
1135 goto err;
1136
1137 return cl;
1138err:
1139 kfree(cl);
1140 return ERR_PTR(ret);
1141}
1142
9ca9050b 1143/**
4034b81b 1144 * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
9ca9050b 1145 *
06ee536b 1146 * @cl: host client
9ca9050b 1147 *
4034b81b 1148 * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
9ca9050b 1149 */
4034b81b 1150static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
9ca9050b 1151{
d49ed64a 1152 if (WARN_ON(!cl || !cl->me_cl))
90e0b5f1
TW
1153 return -EINVAL;
1154
4034b81b 1155 if (cl->tx_flow_ctrl_creds > 0)
9ca9050b
TW
1156 return 1;
1157
a808c80c 1158 if (mei_cl_is_fixed_address(cl))
1df629ef 1159 return 1;
1df629ef 1160
d49ed64a 1161 if (mei_cl_is_single_recv_buf(cl)) {
4034b81b 1162 if (cl->me_cl->tx_flow_ctrl_creds > 0)
d49ed64a 1163 return 1;
9ca9050b 1164 }
d49ed64a 1165 return 0;
9ca9050b
TW
1166}
1167
1168/**
4034b81b
TW
1169 * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
1170 * for a client
9ca9050b 1171 *
4034b81b 1172 * @cl: host client
393b148f 1173 *
a8605ea2 1174 * Return:
9ca9050b 1175 * 0 on success
9ca9050b
TW
1176 * -EINVAL when ctrl credits are <= 0
1177 */
4034b81b 1178static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
9ca9050b 1179{
d49ed64a 1180 if (WARN_ON(!cl || !cl->me_cl))
90e0b5f1
TW
1181 return -EINVAL;
1182
1df629ef
AU
1183 if (mei_cl_is_fixed_address(cl))
1184 return 0;
1185
d49ed64a 1186 if (mei_cl_is_single_recv_buf(cl)) {
4034b81b 1187 if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
d49ed64a 1188 return -EINVAL;
4034b81b 1189 cl->me_cl->tx_flow_ctrl_creds--;
12d00665 1190 } else {
4034b81b 1191 if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
d49ed64a 1192 return -EINVAL;
4034b81b 1193 cl->tx_flow_ctrl_creds--;
9ca9050b 1194 }
d49ed64a 1195 return 0;
9ca9050b
TW
1196}
1197
51678ccb
TW
1198/**
1199 * mei_cl_notify_fop2req - convert fop to proper request
1200 *
1201 * @fop: client notification start response command
1202 *
1203 * Return: MEI_HBM_NOTIFICATION_START/STOP
1204 */
1205u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
1206{
1207 if (fop == MEI_FOP_NOTIFY_START)
1208 return MEI_HBM_NOTIFICATION_START;
1209 else
1210 return MEI_HBM_NOTIFICATION_STOP;
1211}
1212
1213/**
1214 * mei_cl_notify_req2fop - convert notification request top file operation type
1215 *
1216 * @req: hbm notification request type
1217 *
1218 * Return: MEI_FOP_NOTIFY_START/STOP
1219 */
1220enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
1221{
1222 if (req == MEI_HBM_NOTIFICATION_START)
1223 return MEI_FOP_NOTIFY_START;
1224 else
1225 return MEI_FOP_NOTIFY_STOP;
1226}
1227
1228/**
1229 * mei_cl_irq_notify - send notification request in irq_thread context
1230 *
1231 * @cl: client
1232 * @cb: callback block.
1233 * @cmpl_list: complete list.
1234 *
1235 * Return: 0 on such and error otherwise.
1236 */
1237int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
1238 struct mei_cl_cb *cmpl_list)
1239{
1240 struct mei_device *dev = cl->dev;
1241 u32 msg_slots;
1242 int slots;
1243 int ret;
1244 bool request;
1245
1246 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
1247 slots = mei_hbuf_empty_slots(dev);
1248
1249 if (slots < msg_slots)
1250 return -EMSGSIZE;
1251
1252 request = mei_cl_notify_fop2req(cb->fop_type);
1253 ret = mei_hbm_cl_notify_req(dev, cl, request);
1254 if (ret) {
1255 cl->status = ret;
1256 list_move_tail(&cb->list, &cmpl_list->list);
1257 return ret;
1258 }
1259
1260 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
1261 return 0;
1262}
1263
1264/**
1265 * mei_cl_notify_request - send notification stop/start request
1266 *
1267 * @cl: host client
1268 * @file: associate request with file
1269 * @request: 1 for start or 0 for stop
1270 *
1271 * Locking: called under "dev->device_lock" lock
1272 *
1273 * Return: 0 on such and error otherwise.
1274 */
f23e2cc4
TW
1275int mei_cl_notify_request(struct mei_cl *cl,
1276 const struct file *file, u8 request)
51678ccb
TW
1277{
1278 struct mei_device *dev;
1279 struct mei_cl_cb *cb;
1280 enum mei_cb_file_ops fop_type;
1281 int rets;
1282
1283 if (WARN_ON(!cl || !cl->dev))
1284 return -ENODEV;
1285
1286 dev = cl->dev;
1287
1288 if (!dev->hbm_f_ev_supported) {
1289 cl_dbg(dev, cl, "notifications not supported\n");
1290 return -EOPNOTSUPP;
1291 }
1292
1293 rets = pm_runtime_get(dev->dev);
1294 if (rets < 0 && rets != -EINPROGRESS) {
1295 pm_runtime_put_noidle(dev->dev);
1296 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1297 return rets;
1298 }
1299
1300 fop_type = mei_cl_notify_req2fop(request);
1301 cb = mei_io_cb_init(cl, fop_type, file);
1302 if (!cb) {
1303 rets = -ENOMEM;
1304 goto out;
1305 }
1306
1307 if (mei_hbuf_acquire(dev)) {
1308 if (mei_hbm_cl_notify_req(dev, cl, request)) {
1309 rets = -ENODEV;
1310 goto out;
1311 }
1312 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
1313 } else {
1314 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1315 }
1316
1317 mutex_unlock(&dev->device_lock);
7ff4bdd4
AU
1318 wait_event_timeout(cl->wait,
1319 cl->notify_en == request || !mei_cl_is_connected(cl),
1320 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
51678ccb
TW
1321 mutex_lock(&dev->device_lock);
1322
4a8eaa96
AU
1323 if (cl->notify_en != request && !cl->status)
1324 cl->status = -EFAULT;
51678ccb
TW
1325
1326 rets = cl->status;
1327
1328out:
1329 cl_dbg(dev, cl, "rpm: autosuspend\n");
1330 pm_runtime_mark_last_busy(dev->dev);
1331 pm_runtime_put_autosuspend(dev->dev);
1332
1333 mei_io_cb_free(cb);
1334 return rets;
1335}
1336
237092bf
TW
1337/**
1338 * mei_cl_notify - raise notification
1339 *
1340 * @cl: host client
1341 *
1342 * Locking: called under "dev->device_lock" lock
1343 */
1344void mei_cl_notify(struct mei_cl *cl)
1345{
1346 struct mei_device *dev;
1347
1348 if (!cl || !cl->dev)
1349 return;
1350
1351 dev = cl->dev;
1352
1353 if (!cl->notify_en)
1354 return;
1355
1356 cl_dbg(dev, cl, "notify event");
1357 cl->notify_ev = true;
850f8940
TW
1358 if (!mei_cl_bus_notify_event(cl))
1359 wake_up_interruptible(&cl->ev_wait);
237092bf
TW
1360
1361 if (cl->ev_async)
1362 kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
bb2ef9c3 1363
237092bf
TW
1364}
1365
b38a362f
TW
1366/**
1367 * mei_cl_notify_get - get or wait for notification event
1368 *
1369 * @cl: host client
1370 * @block: this request is blocking
1371 * @notify_ev: true if notification event was received
1372 *
1373 * Locking: called under "dev->device_lock" lock
1374 *
1375 * Return: 0 on such and error otherwise.
1376 */
1377int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
1378{
1379 struct mei_device *dev;
1380 int rets;
1381
1382 *notify_ev = false;
1383
1384 if (WARN_ON(!cl || !cl->dev))
1385 return -ENODEV;
1386
1387 dev = cl->dev;
1388
1389 if (!mei_cl_is_connected(cl))
1390 return -ENODEV;
1391
1392 if (cl->notify_ev)
1393 goto out;
1394
1395 if (!block)
1396 return -EAGAIN;
1397
1398 mutex_unlock(&dev->device_lock);
1399 rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
1400 mutex_lock(&dev->device_lock);
1401
1402 if (rets < 0)
1403 return rets;
1404
1405out:
1406 *notify_ev = cl->notify_ev;
1407 cl->notify_ev = false;
1408 return 0;
1409}
1410
ab841160 1411/**
393b148f 1412 * mei_cl_read_start - the start read client message function.
ab841160 1413 *
90e0b5f1 1414 * @cl: host client
ce23139c 1415 * @length: number of bytes to read
bca67d68 1416 * @fp: pointer to file structure
ab841160 1417 *
a8605ea2 1418 * Return: 0 on success, <0 on failure.
ab841160 1419 */
f23e2cc4 1420int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
ab841160 1421{
90e0b5f1 1422 struct mei_device *dev;
ab841160 1423 struct mei_cl_cb *cb;
664df38b 1424 int rets;
ab841160 1425
90e0b5f1
TW
1426 if (WARN_ON(!cl || !cl->dev))
1427 return -ENODEV;
1428
1429 dev = cl->dev;
1430
b950ac1d 1431 if (!mei_cl_is_connected(cl))
ab841160
OW
1432 return -ENODEV;
1433
d49ed64a
AU
1434 if (!mei_me_cl_is_active(cl->me_cl)) {
1435 cl_err(dev, cl, "no such me client\n");
7ca96aa2 1436 return -ENOTTY;
664df38b 1437 }
1df629ef 1438
e51dfa5a
AU
1439 if (mei_cl_is_fixed_address(cl))
1440 return 0;
1441
46978ada
AU
1442 /* HW currently supports only one pending read */
1443 if (cl->rx_flow_ctrl_creds)
1444 return -EBUSY;
1445
79563db9 1446 /* always allocate at least client max message */
d49ed64a 1447 length = max_t(size_t, length, mei_cl_mtu(cl));
1df629ef
AU
1448 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp);
1449 if (!cb)
1450 return -ENOMEM;
1451
46978ada
AU
1452 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1453
2bf94cab 1454 rets = pm_runtime_get(dev->dev);
04bb139a 1455 if (rets < 0 && rets != -EINPROGRESS) {
2bf94cab 1456 pm_runtime_put_noidle(dev->dev);
04bb139a 1457 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1df629ef 1458 goto nortpm;
04bb139a
TW
1459 }
1460
46978ada 1461 rets = 0;
6aae48ff 1462 if (mei_hbuf_acquire(dev)) {
86113500
AU
1463 rets = mei_hbm_cl_flow_control_req(dev, cl);
1464 if (rets < 0)
04bb139a 1465 goto out;
04bb139a 1466
46978ada 1467 list_move_tail(&cb->list, &cl->rd_pending);
ab841160 1468 }
46978ada 1469 cl->rx_flow_ctrl_creds++;
accb884b 1470
04bb139a
TW
1471out:
1472 cl_dbg(dev, cl, "rpm: autosuspend\n");
2bf94cab
TW
1473 pm_runtime_mark_last_busy(dev->dev);
1474 pm_runtime_put_autosuspend(dev->dev);
1df629ef 1475nortpm:
04bb139a
TW
1476 if (rets)
1477 mei_io_cb_free(cb);
1478
ab841160
OW
1479 return rets;
1480}
1481
21767546 1482/**
9d098192 1483 * mei_cl_irq_write - write a message to device
21767546
TW
1484 * from the interrupt thread context
1485 *
1486 * @cl: client
1487 * @cb: callback block.
21767546
TW
1488 * @cmpl_list: complete list.
1489 *
a8605ea2 1490 * Return: 0, OK; otherwise error.
21767546 1491 */
9d098192
TW
1492int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1493 struct mei_cl_cb *cmpl_list)
21767546 1494{
136698e5
TW
1495 struct mei_device *dev;
1496 struct mei_msg_data *buf;
21767546 1497 struct mei_msg_hdr mei_hdr;
136698e5
TW
1498 size_t len;
1499 u32 msg_slots;
9d098192 1500 int slots;
2ebf8c94 1501 int rets;
b8b73035 1502 bool first_chunk;
21767546 1503
136698e5
TW
1504 if (WARN_ON(!cl || !cl->dev))
1505 return -ENODEV;
1506
1507 dev = cl->dev;
1508
5db7514d 1509 buf = &cb->buf;
136698e5 1510
b8b73035
AU
1511 first_chunk = cb->buf_idx == 0;
1512
4034b81b 1513 rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
136698e5
TW
1514 if (rets < 0)
1515 return rets;
1516
1517 if (rets == 0) {
04bb139a 1518 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
136698e5
TW
1519 return 0;
1520 }
1521
9d098192 1522 slots = mei_hbuf_empty_slots(dev);
136698e5
TW
1523 len = buf->size - cb->buf_idx;
1524 msg_slots = mei_data2slots(len);
1525
1df629ef 1526 mei_hdr.host_addr = mei_cl_host_addr(cl);
d49ed64a 1527 mei_hdr.me_addr = mei_cl_me_id(cl);
21767546 1528 mei_hdr.reserved = 0;
479327fc 1529 mei_hdr.internal = cb->internal;
21767546 1530
9d098192 1531 if (slots >= msg_slots) {
21767546
TW
1532 mei_hdr.length = len;
1533 mei_hdr.msg_complete = 1;
1534 /* Split the message only if we can write the whole host buffer */
9d098192
TW
1535 } else if (slots == dev->hbuf_depth) {
1536 msg_slots = slots;
1537 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
21767546
TW
1538 mei_hdr.length = len;
1539 mei_hdr.msg_complete = 0;
1540 } else {
1541 /* wait for next time the host buffer is empty */
1542 return 0;
1543 }
1544
35bf7692 1545 cl_dbg(dev, cl, "buf: size = %zu idx = %zu\n",
5db7514d 1546 cb->buf.size, cb->buf_idx);
21767546 1547
136698e5 1548 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
2ebf8c94
TW
1549 if (rets) {
1550 cl->status = rets;
21767546 1551 list_move_tail(&cb->list, &cmpl_list->list);
2ebf8c94 1552 return rets;
21767546
TW
1553 }
1554
1555 cl->status = 0;
4dfaa9f7 1556 cl->writing_state = MEI_WRITING;
21767546 1557 cb->buf_idx += mei_hdr.length;
8660172e 1558 cb->completed = mei_hdr.msg_complete == 1;
4dfaa9f7 1559
b8b73035 1560 if (first_chunk) {
4034b81b 1561 if (mei_cl_tx_flow_ctrl_creds_reduce(cl))
2ebf8c94 1562 return -EIO;
21767546
TW
1563 }
1564
b8b73035
AU
1565 if (mei_hdr.msg_complete)
1566 list_move_tail(&cb->list, &dev->write_waiting_list.list);
1567
21767546
TW
1568 return 0;
1569}
1570
4234a6de
TW
1571/**
1572 * mei_cl_write - submit a write cb to mei device
a8605ea2 1573 * assumes device_lock is locked
4234a6de
TW
1574 *
1575 * @cl: host client
a8605ea2 1576 * @cb: write callback with filled data
ce23139c 1577 * @blocking: block until completed
4234a6de 1578 *
a8605ea2 1579 * Return: number of bytes sent on success, <0 on failure.
4234a6de
TW
1580 */
1581int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
1582{
1583 struct mei_device *dev;
1584 struct mei_msg_data *buf;
1585 struct mei_msg_hdr mei_hdr;
23253c31 1586 int size;
4234a6de
TW
1587 int rets;
1588
1589
1590 if (WARN_ON(!cl || !cl->dev))
1591 return -ENODEV;
1592
1593 if (WARN_ON(!cb))
1594 return -EINVAL;
1595
1596 dev = cl->dev;
1597
5db7514d 1598 buf = &cb->buf;
23253c31 1599 size = buf->size;
4234a6de 1600
23253c31 1601 cl_dbg(dev, cl, "size=%d\n", size);
4234a6de 1602
2bf94cab 1603 rets = pm_runtime_get(dev->dev);
04bb139a 1604 if (rets < 0 && rets != -EINPROGRESS) {
2bf94cab 1605 pm_runtime_put_noidle(dev->dev);
04bb139a 1606 cl_err(dev, cl, "rpm: get failed %d\n", rets);
6cbb097f 1607 goto free;
04bb139a 1608 }
4234a6de 1609
6aae48ff
TW
1610 cb->buf_idx = 0;
1611 cl->writing_state = MEI_IDLE;
1612
1df629ef 1613 mei_hdr.host_addr = mei_cl_host_addr(cl);
d49ed64a 1614 mei_hdr.me_addr = mei_cl_me_id(cl);
6aae48ff
TW
1615 mei_hdr.reserved = 0;
1616 mei_hdr.msg_complete = 0;
1617 mei_hdr.internal = cb->internal;
4234a6de 1618
4034b81b 1619 rets = mei_cl_tx_flow_ctrl_creds(cl);
4234a6de
TW
1620 if (rets < 0)
1621 goto err;
1622
6aae48ff
TW
1623 if (rets == 0) {
1624 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
23253c31 1625 rets = size;
6aae48ff
TW
1626 goto out;
1627 }
1628 if (!mei_hbuf_acquire(dev)) {
1629 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
23253c31 1630 rets = size;
4234a6de
TW
1631 goto out;
1632 }
4234a6de
TW
1633
1634 /* Check for a maximum length */
23253c31 1635 if (size > mei_hbuf_max_len(dev)) {
4234a6de
TW
1636 mei_hdr.length = mei_hbuf_max_len(dev);
1637 mei_hdr.msg_complete = 0;
1638 } else {
23253c31 1639 mei_hdr.length = size;
4234a6de
TW
1640 mei_hdr.msg_complete = 1;
1641 }
1642
2ebf8c94
TW
1643 rets = mei_write_message(dev, &mei_hdr, buf->data);
1644 if (rets)
4234a6de 1645 goto err;
4234a6de 1646
4034b81b 1647 rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
b8b73035
AU
1648 if (rets)
1649 goto err;
1650
4234a6de
TW
1651 cl->writing_state = MEI_WRITING;
1652 cb->buf_idx = mei_hdr.length;
8660172e 1653 cb->completed = mei_hdr.msg_complete == 1;
4234a6de 1654
4234a6de 1655out:
b8b73035 1656 if (mei_hdr.msg_complete)
4234a6de 1657 list_add_tail(&cb->list, &dev->write_waiting_list.list);
b8b73035 1658 else
4234a6de 1659 list_add_tail(&cb->list, &dev->write_list.list);
4234a6de 1660
23253c31 1661 cb = NULL;
4234a6de
TW
1662 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1663
1664 mutex_unlock(&dev->device_lock);
7ca96aa2 1665 rets = wait_event_interruptible(cl->tx_wait,
0faf6a3b
AU
1666 cl->writing_state == MEI_WRITE_COMPLETE ||
1667 (!mei_cl_is_connected(cl)));
4234a6de 1668 mutex_lock(&dev->device_lock);
7ca96aa2
AU
1669 /* wait_event_interruptible returns -ERESTARTSYS */
1670 if (rets) {
1671 if (signal_pending(current))
1672 rets = -EINTR;
1673 goto err;
1674 }
0faf6a3b
AU
1675 if (cl->writing_state != MEI_WRITE_COMPLETE) {
1676 rets = -EFAULT;
1677 goto err;
1678 }
4234a6de 1679 }
7ca96aa2 1680
23253c31 1681 rets = size;
4234a6de 1682err:
04bb139a 1683 cl_dbg(dev, cl, "rpm: autosuspend\n");
2bf94cab
TW
1684 pm_runtime_mark_last_busy(dev->dev);
1685 pm_runtime_put_autosuspend(dev->dev);
6cbb097f
AU
1686free:
1687 mei_io_cb_free(cb);
04bb139a 1688
4234a6de
TW
1689 return rets;
1690}
1691
1692
db086fa9
TW
1693/**
1694 * mei_cl_complete - processes completed operation for a client
1695 *
1696 * @cl: private data of the file object.
1697 * @cb: callback block.
1698 */
1699void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1700{
a1809d38
AU
1701 struct mei_device *dev = cl->dev;
1702
3c666182
TW
1703 switch (cb->fop_type) {
1704 case MEI_FOP_WRITE:
db086fa9 1705 mei_io_cb_free(cb);
db086fa9 1706 cl->writing_state = MEI_WRITE_COMPLETE;
a1809d38 1707 if (waitqueue_active(&cl->tx_wait)) {
db086fa9 1708 wake_up_interruptible(&cl->tx_wait);
a1809d38
AU
1709 } else {
1710 pm_runtime_mark_last_busy(dev->dev);
1711 pm_request_autosuspend(dev->dev);
1712 }
3c666182 1713 break;
db086fa9 1714
3c666182 1715 case MEI_FOP_READ:
a9bed610 1716 list_add_tail(&cb->list, &cl->rd_completed);
46978ada
AU
1717 if (!mei_cl_is_fixed_address(cl) &&
1718 !WARN_ON(!cl->rx_flow_ctrl_creds))
1719 cl->rx_flow_ctrl_creds--;
a1f9ae2b
TW
1720 if (!mei_cl_bus_rx_event(cl))
1721 wake_up_interruptible(&cl->rx_wait);
3c666182
TW
1722 break;
1723
1724 case MEI_FOP_CONNECT:
1725 case MEI_FOP_DISCONNECT:
51678ccb
TW
1726 case MEI_FOP_NOTIFY_STOP:
1727 case MEI_FOP_NOTIFY_START:
3c666182
TW
1728 if (waitqueue_active(&cl->wait))
1729 wake_up(&cl->wait);
db086fa9 1730
6a8d648c
AU
1731 break;
1732 case MEI_FOP_DISCONNECT_RSP:
1733 mei_io_cb_free(cb);
1734 mei_cl_set_disconnected(cl);
3c666182
TW
1735 break;
1736 default:
1737 BUG_ON(0);
db086fa9
TW
1738 }
1739}
1740
4234a6de 1741
074b4c01
TW
1742/**
1743 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1744 *
a8605ea2 1745 * @dev: mei device
074b4c01 1746 */
074b4c01
TW
1747void mei_cl_all_disconnect(struct mei_device *dev)
1748{
31f88f57 1749 struct mei_cl *cl;
074b4c01 1750
3c666182
TW
1751 list_for_each_entry(cl, &dev->file_list, link)
1752 mei_cl_set_disconnected(cl);
074b4c01 1753}