1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2003-2022, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
5 */
6
7 #include <linux/sched/signal.h>
8 #include <linux/wait.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/dma-mapping.h>
13
14 #include <linux/mei.h>
15
16 #include "mei_dev.h"
17 #include "hbm.h"
18 #include "client.h"
19
20 /**
21 * mei_me_cl_init - initialize me client
22 *
23 * @me_cl: me client
24 */
mei_me_cl_init(struct mei_me_client * me_cl)25 void mei_me_cl_init(struct mei_me_client *me_cl)
26 {
27 INIT_LIST_HEAD(&me_cl->list);
28 kref_init(&me_cl->refcnt);
29 }
30
31 /**
32 * mei_me_cl_get - increases me client refcount
33 *
34 * @me_cl: me client
35 *
36 * Locking: called under "dev->device_lock" lock
37 *
38 * Return: me client or NULL
39 */
mei_me_cl_get(struct mei_me_client * me_cl)40 struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
41 {
42 if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
43 return me_cl;
44
45 return NULL;
46 }
47
48 /**
49 * mei_me_cl_release - free me client
50 *
51 * @ref: me_client refcount
52 *
53 * Locking: called under "dev->device_lock" lock
54 */
mei_me_cl_release(struct kref * ref)55 static void mei_me_cl_release(struct kref *ref)
56 {
57 struct mei_me_client *me_cl =
58 container_of(ref, struct mei_me_client, refcnt);
59
60 kfree(me_cl);
61 }
62
63 /**
64 * mei_me_cl_put - decrease me client refcount and free client if necessary
65 *
66 * @me_cl: me client
67 *
68 * Locking: called under "dev->device_lock" lock
69 */
mei_me_cl_put(struct mei_me_client * me_cl)70 void mei_me_cl_put(struct mei_me_client *me_cl)
71 {
72 if (me_cl)
73 kref_put(&me_cl->refcnt, mei_me_cl_release);
74 }
75
76 /**
77 * __mei_me_cl_del - delete me client from the list and decrease
78 * reference counter
79 *
80 * @dev: mei device
81 * @me_cl: me client
82 *
83 * Locking: dev->me_clients_rwsem
84 */
__mei_me_cl_del(struct mei_device * dev,struct mei_me_client * me_cl)85 static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
86 {
87 if (!me_cl)
88 return;
89
90 list_del_init(&me_cl->list);
91 mei_me_cl_put(me_cl);
92 }
93
94 /**
95 * mei_me_cl_del - delete me client from the list and decrease
96 * reference counter
97 *
98 * @dev: mei device
99 * @me_cl: me client
100 */
mei_me_cl_del(struct mei_device * dev,struct mei_me_client * me_cl)101 void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
102 {
103 down_write(&dev->me_clients_rwsem);
104 __mei_me_cl_del(dev, me_cl);
105 up_write(&dev->me_clients_rwsem);
106 }
107
108 /**
109 * mei_me_cl_add - add me client to the list
110 *
111 * @dev: mei device
112 * @me_cl: me client
113 */
mei_me_cl_add(struct mei_device * dev,struct mei_me_client * me_cl)114 void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
115 {
116 down_write(&dev->me_clients_rwsem);
117 list_add(&me_cl->list, &dev->me_clients);
118 up_write(&dev->me_clients_rwsem);
119 }
120
121 /**
122 * __mei_me_cl_by_uuid - locate me client by uuid
123 * increases ref count
124 *
125 * @dev: mei device
126 * @uuid: me client uuid
127 *
128 * Return: me client or NULL if not found
129 *
130 * Locking: dev->me_clients_rwsem
131 */
__mei_me_cl_by_uuid(struct mei_device * dev,const uuid_le * uuid)132 static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
133 const uuid_le *uuid)
134 {
135 struct mei_me_client *me_cl;
136 const uuid_le *pn;
137
138 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
139
140 list_for_each_entry(me_cl, &dev->me_clients, list) {
141 pn = &me_cl->props.protocol_name;
142 if (uuid_le_cmp(*uuid, *pn) == 0)
143 return mei_me_cl_get(me_cl);
144 }
145
146 return NULL;
147 }
148
149 /**
150 * mei_me_cl_by_uuid - locate me client by uuid
151 * increases ref count
152 *
153 * @dev: mei device
154 * @uuid: me client uuid
155 *
156 * Return: me client or NULL if not found
157 *
158 * Locking: dev->me_clients_rwsem
159 */
mei_me_cl_by_uuid(struct mei_device * dev,const uuid_le * uuid)160 struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
161 const uuid_le *uuid)
162 {
163 struct mei_me_client *me_cl;
164
165 down_read(&dev->me_clients_rwsem);
166 me_cl = __mei_me_cl_by_uuid(dev, uuid);
167 up_read(&dev->me_clients_rwsem);
168
169 return me_cl;
170 }
171
172 /**
173 * mei_me_cl_by_id - locate me client by client id
174 * increases ref count
175 *
176 * @dev: the device structure
177 * @client_id: me client id
178 *
179 * Return: me client or NULL if not found
180 *
181 * Locking: dev->me_clients_rwsem
182 */
mei_me_cl_by_id(struct mei_device * dev,u8 client_id)183 struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
184 {
185
186 struct mei_me_client *__me_cl, *me_cl = NULL;
187
188 down_read(&dev->me_clients_rwsem);
189 list_for_each_entry(__me_cl, &dev->me_clients, list) {
190 if (__me_cl->client_id == client_id) {
191 me_cl = mei_me_cl_get(__me_cl);
192 break;
193 }
194 }
195 up_read(&dev->me_clients_rwsem);
196
197 return me_cl;
198 }
199
200 /**
201 * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
202 * increases ref count
203 *
204 * @dev: the device structure
205 * @uuid: me client uuid
206 * @client_id: me client id
207 *
208 * Return: me client or null if not found
209 *
210 * Locking: dev->me_clients_rwsem
211 */
__mei_me_cl_by_uuid_id(struct mei_device * dev,const uuid_le * uuid,u8 client_id)212 static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
213 const uuid_le *uuid, u8 client_id)
214 {
215 struct mei_me_client *me_cl;
216 const uuid_le *pn;
217
218 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
219
220 list_for_each_entry(me_cl, &dev->me_clients, list) {
221 pn = &me_cl->props.protocol_name;
222 if (uuid_le_cmp(*uuid, *pn) == 0 &&
223 me_cl->client_id == client_id)
224 return mei_me_cl_get(me_cl);
225 }
226
227 return NULL;
228 }
229
230
231 /**
232 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
233 * increases ref count
234 *
235 * @dev: the device structure
236 * @uuid: me client uuid
237 * @client_id: me client id
238 *
239 * Return: me client or null if not found
240 */
mei_me_cl_by_uuid_id(struct mei_device * dev,const uuid_le * uuid,u8 client_id)241 struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
242 const uuid_le *uuid, u8 client_id)
243 {
244 struct mei_me_client *me_cl;
245
246 down_read(&dev->me_clients_rwsem);
247 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
248 up_read(&dev->me_clients_rwsem);
249
250 return me_cl;
251 }
252
253 /**
254 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
255 *
256 * @dev: the device structure
257 * @uuid: me client uuid
258 *
259 * Locking: called under "dev->device_lock" lock
260 */
mei_me_cl_rm_by_uuid(struct mei_device * dev,const uuid_le * uuid)261 void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
262 {
263 struct mei_me_client *me_cl;
264
265 dev_dbg(&dev->dev, "remove %pUl\n", uuid);
266
267 down_write(&dev->me_clients_rwsem);
268 me_cl = __mei_me_cl_by_uuid(dev, uuid);
269 __mei_me_cl_del(dev, me_cl);
270 mei_me_cl_put(me_cl);
271 up_write(&dev->me_clients_rwsem);
272 }
273
274 /**
275 * mei_me_cl_rm_all - remove all me clients
276 *
277 * @dev: the device structure
278 *
279 * Locking: called under "dev->device_lock" lock
280 */
mei_me_cl_rm_all(struct mei_device * dev)281 void mei_me_cl_rm_all(struct mei_device *dev)
282 {
283 struct mei_me_client *me_cl, *next;
284
285 down_write(&dev->me_clients_rwsem);
286 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
287 __mei_me_cl_del(dev, me_cl);
288 up_write(&dev->me_clients_rwsem);
289 }
290
291 /**
292 * mei_io_cb_free - free mei_cb_private related memory
293 *
294 * @cb: mei callback struct
295 */
mei_io_cb_free(struct mei_cl_cb * cb)296 void mei_io_cb_free(struct mei_cl_cb *cb)
297 {
298 if (cb == NULL)
299 return;
300
301 list_del(&cb->list);
302 kvfree(cb->buf.data);
303 kfree(cb->ext_hdr);
304 kfree(cb);
305 }
306
307 /**
308 * mei_tx_cb_enqueue - queue tx callback
309 *
310 * @cb: mei callback struct
311 * @head: an instance of list to queue on
312 *
313 * Locking: called under "dev->device_lock" lock
314 */
mei_tx_cb_enqueue(struct mei_cl_cb * cb,struct list_head * head)315 static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb,
316 struct list_head *head)
317 {
318 list_add_tail(&cb->list, head);
319 cb->cl->tx_cb_queued++;
320 }
321
322 /**
323 * mei_tx_cb_dequeue - dequeue tx callback
324 *
325 * @cb: mei callback struct to dequeue and free
326 *
327 * Locking: called under "dev->device_lock" lock
328 */
mei_tx_cb_dequeue(struct mei_cl_cb * cb)329 static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb)
330 {
331 if (!WARN_ON(cb->cl->tx_cb_queued == 0))
332 cb->cl->tx_cb_queued--;
333
334 mei_io_cb_free(cb);
335 }
336
337 /**
338 * mei_cl_set_read_by_fp - set pending_read flag to vtag struct for given fp
339 *
340 * @cl: mei client
341 * @fp: pointer to file structure
342 *
343 * Locking: called under "dev->device_lock" lock
344 */
mei_cl_set_read_by_fp(const struct mei_cl * cl,const struct file * fp)345 static void mei_cl_set_read_by_fp(const struct mei_cl *cl,
346 const struct file *fp)
347 {
348 struct mei_cl_vtag *cl_vtag;
349
350 list_for_each_entry(cl_vtag, &cl->vtag_map, list) {
351 if (cl_vtag->fp == fp) {
352 cl_vtag->pending_read = true;
353 return;
354 }
355 }
356 }
357
358 /**
359 * mei_io_cb_init - allocate and initialize io callback
360 *
361 * @cl: mei client
362 * @type: operation type
363 * @fp: pointer to file structure
364 *
365 * Return: mei_cl_cb pointer or NULL;
366 */
mei_io_cb_init(struct mei_cl * cl,enum mei_cb_file_ops type,const struct file * fp)367 static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl,
368 enum mei_cb_file_ops type,
369 const struct file *fp)
370 {
371 struct mei_cl_cb *cb;
372
373 cb = kzalloc_obj(*cb);
374 if (!cb)
375 return NULL;
376
377 INIT_LIST_HEAD(&cb->list);
378 cb->fp = fp;
379 cb->cl = cl;
380 cb->buf_idx = 0;
381 cb->fop_type = type;
382 cb->vtag = 0;
383 cb->ext_hdr = NULL;
384
385 return cb;
386 }
387
388 /**
389 * mei_io_list_flush_cl - removes cbs belonging to the cl.
390 *
391 * @head: an instance of our list structure
392 * @cl: host client
393 */
mei_io_list_flush_cl(struct list_head * head,const struct mei_cl * cl)394 static void mei_io_list_flush_cl(struct list_head *head,
395 const struct mei_cl *cl)
396 {
397 struct mei_cl_cb *cb, *next;
398
399 list_for_each_entry_safe(cb, next, head, list) {
400 if (cl == cb->cl) {
401 list_del_init(&cb->list);
402 if (cb->fop_type == MEI_FOP_READ)
403 mei_io_cb_free(cb);
404 }
405 }
406 }
407
408 /**
409 * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
410 *
411 * @head: An instance of our list structure
412 * @cl: host client
413 * @fp: file pointer (matching cb file object), may be NULL
414 */
mei_io_tx_list_free_cl(struct list_head * head,const struct mei_cl * cl,const struct file * fp)415 static void mei_io_tx_list_free_cl(struct list_head *head,
416 const struct mei_cl *cl,
417 const struct file *fp)
418 {
419 struct mei_cl_cb *cb, *next;
420
421 list_for_each_entry_safe(cb, next, head, list) {
422 if (cl == cb->cl && (!fp || fp == cb->fp))
423 mei_tx_cb_dequeue(cb);
424 }
425 }
426
427 /**
428 * mei_io_rd_list_free_fp - free cb from a rd_completed list that matches file pointer
429 *
430 * @cl: host client
431 * @fp: file pointer (matching cb file object), may be NULL
432 */
mei_io_rd_list_free_fp(struct mei_cl * cl,const struct file * fp)433 static void mei_io_rd_list_free_fp(struct mei_cl *cl, const struct file *fp)
434 {
435 struct mei_cl_cb *cb, *next;
436 LIST_HEAD(cmpl_list);
437
438 spin_lock(&cl->rd_completed_lock);
439 list_for_each_entry_safe(cb, next, &cl->rd_completed, list)
440 if (!fp || fp == cb->fp)
441 list_move(&cb->list, &cmpl_list);
442 spin_unlock(&cl->rd_completed_lock);
443
444 list_for_each_entry_safe(cb, next, &cmpl_list, list)
445 mei_io_cb_free(cb);
446 }
447
448 /**
449 * mei_cl_free_pending - free pending cb
450 *
451 * @cl: host client
452 */
mei_cl_free_pending(struct mei_cl * cl)453 static void mei_cl_free_pending(struct mei_cl *cl)
454 {
455 struct mei_cl_cb *cb;
456
457 cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
458 mei_io_cb_free(cb);
459 }
460
461 /**
462 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
463 *
464 * @cl: host client
465 * @length: size of the buffer
466 * @fop_type: operation type
467 * @fp: associated file pointer (might be NULL)
468 *
469 * Return: cb on success and NULL on failure
470 */
mei_cl_alloc_cb(struct mei_cl * cl,size_t length,enum mei_cb_file_ops fop_type,const struct file * fp)471 struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
472 enum mei_cb_file_ops fop_type,
473 const struct file *fp)
474 {
475 struct mei_cl_cb *cb;
476
477 cb = mei_io_cb_init(cl, fop_type, fp);
478 if (!cb)
479 return NULL;
480
481 if (length == 0)
482 return cb;
483
484 cb->buf.data = kvmalloc(roundup(length, MEI_SLOT_SIZE), GFP_KERNEL);
485 if (!cb->buf.data) {
486 mei_io_cb_free(cb);
487 return NULL;
488 }
489 cb->buf.size = length;
490
491 return cb;
492 }
493
494 /**
495 * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
496 * and enqueuing of the control commands cb
497 *
498 * @cl: host client
499 * @length: size of the buffer
500 * @fop_type: operation type
501 * @fp: associated file pointer (might be NULL)
502 *
503 * Return: cb on success and NULL on failure
504 * Locking: called under "dev->device_lock" lock
505 */
mei_cl_enqueue_ctrl_wr_cb(struct mei_cl * cl,size_t length,enum mei_cb_file_ops fop_type,const struct file * fp)506 struct mei_cl_cb *mei_cl_enqueue_ctrl_wr_cb(struct mei_cl *cl, size_t length,
507 enum mei_cb_file_ops fop_type,
508 const struct file *fp)
509 {
510 struct mei_cl_cb *cb;
511
512 /* for RX always allocate at least client's mtu */
513 if (length)
514 length = max_t(size_t, length, mei_cl_mtu(cl));
515
516 cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
517 if (!cb)
518 return NULL;
519
520 list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
521 return cb;
522 }
523
524 /**
525 * mei_cl_read_cb - find this cl's callback in the read list
526 * for a specific file
527 *
528 * @cl: host client
529 * @fp: file pointer (matching cb file object), may be NULL
530 *
531 * Return: cb on success, NULL if cb is not found
532 */
mei_cl_read_cb(struct mei_cl * cl,const struct file * fp)533 struct mei_cl_cb *mei_cl_read_cb(struct mei_cl *cl, const struct file *fp)
534 {
535 struct mei_cl_cb *cb;
536 struct mei_cl_cb *ret_cb = NULL;
537
538 spin_lock(&cl->rd_completed_lock);
539 list_for_each_entry(cb, &cl->rd_completed, list)
540 if (!fp || fp == cb->fp) {
541 ret_cb = cb;
542 break;
543 }
544 spin_unlock(&cl->rd_completed_lock);
545 return ret_cb;
546 }
547
548 /**
549 * mei_cl_flush_queues - flushes queue lists belonging to cl.
550 *
551 * @cl: host client
552 * @fp: file pointer (matching cb file object), may be NULL
553 *
554 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
555 */
mei_cl_flush_queues(struct mei_cl * cl,const struct file * fp)556 int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
557 {
558 struct mei_device *dev;
559
560 if (WARN_ON(!cl || !cl->dev))
561 return -EINVAL;
562
563 dev = cl->dev;
564
565 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
566 mei_io_tx_list_free_cl(&cl->dev->write_list, cl, fp);
567 mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl, fp);
568 /* free pending and control cb only in final flush */
569 if (!fp) {
570 mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl);
571 mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl);
572 mei_cl_free_pending(cl);
573 }
574 mei_io_rd_list_free_fp(cl, fp);
575
576 return 0;
577 }
578
579 /**
580 * mei_cl_init - initializes cl.
581 *
582 * @cl: host client to be initialized
583 * @dev: mei device
584 */
mei_cl_init(struct mei_cl * cl,struct mei_device * dev)585 static void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
586 {
587 memset(cl, 0, sizeof(*cl));
588 init_waitqueue_head(&cl->wait);
589 init_waitqueue_head(&cl->rx_wait);
590 init_waitqueue_head(&cl->tx_wait);
591 init_waitqueue_head(&cl->ev_wait);
592 INIT_LIST_HEAD(&cl->vtag_map);
593 spin_lock_init(&cl->rd_completed_lock);
594 INIT_LIST_HEAD(&cl->rd_completed);
595 INIT_LIST_HEAD(&cl->rd_pending);
596 INIT_LIST_HEAD(&cl->link);
597 cl->writing_state = MEI_IDLE;
598 cl->state = MEI_FILE_UNINITIALIZED;
599 cl->dev = dev;
600 }
601
602 /**
603 * mei_cl_allocate - allocates cl structure and sets it up.
604 *
605 * @dev: mei device
606 * Return: The allocated file or NULL on failure
607 */
mei_cl_allocate(struct mei_device * dev)608 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
609 {
610 struct mei_cl *cl;
611
612 cl = kmalloc_obj(*cl);
613 if (!cl)
614 return NULL;
615
616 mei_cl_init(cl, dev);
617
618 return cl;
619 }
620
621 /**
622 * mei_cl_link - allocate host id in the host map
623 *
624 * @cl: host client
625 *
626 * Return: 0 on success
627 * -EINVAL on incorrect values
628 * -EMFILE if open count exceeded.
629 */
mei_cl_link(struct mei_cl * cl)630 int mei_cl_link(struct mei_cl *cl)
631 {
632 struct mei_device *dev;
633 int id;
634
635 if (WARN_ON(!cl || !cl->dev))
636 return -EINVAL;
637
638 dev = cl->dev;
639
640 id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
641 if (id >= MEI_CLIENTS_MAX) {
642 dev_err(&dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
643 return -EMFILE;
644 }
645
646 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
647 dev_err(&dev->dev, "open_handle_count exceeded %d",
648 MEI_MAX_OPEN_HANDLE_COUNT);
649 return -EMFILE;
650 }
651
652 dev->open_handle_count++;
653
654 cl->host_client_id = id;
655 list_add_tail(&cl->link, &dev->file_list);
656
657 set_bit(id, dev->host_clients_map);
658
659 cl->state = MEI_FILE_INITIALIZING;
660
661 cl_dbg(dev, cl, "link cl\n");
662 return 0;
663 }
664
665 /**
666 * mei_cl_unlink - remove host client from the list
667 *
668 * @cl: host client
669 *
670 * Return: always 0
671 */
mei_cl_unlink(struct mei_cl * cl)672 int mei_cl_unlink(struct mei_cl *cl)
673 {
674 struct mei_device *dev;
675
676 /* don't shout on error exit path */
677 if (!cl)
678 return 0;
679
680 if (WARN_ON(!cl->dev))
681 return 0;
682
683 dev = cl->dev;
684
685 cl_dbg(dev, cl, "unlink client");
686
687 if (cl->state == MEI_FILE_UNINITIALIZED)
688 return 0;
689
690 if (dev->open_handle_count > 0)
691 dev->open_handle_count--;
692
693 /* never clear the 0 bit */
694 if (cl->host_client_id)
695 clear_bit(cl->host_client_id, dev->host_clients_map);
696
697 list_del_init(&cl->link);
698
699 cl->state = MEI_FILE_UNINITIALIZED;
700 cl->writing_state = MEI_IDLE;
701
702 WARN_ON(!list_empty(&cl->rd_completed) ||
703 !list_empty(&cl->rd_pending) ||
704 !list_empty(&cl->link));
705
706 return 0;
707 }
708
mei_host_client_init(struct mei_device * dev)709 void mei_host_client_init(struct mei_device *dev)
710 {
711 mei_set_devstate(dev, MEI_DEV_ENABLED);
712 dev->reset_count = 0;
713
714 schedule_work(&dev->bus_rescan_work);
715
716 dev_dbg(&dev->dev, "rpm: autosuspend\n");
717 pm_request_autosuspend(dev->parent);
718 }
719
720 /**
721 * mei_hbuf_acquire - try to acquire host buffer
722 *
723 * @dev: the device structure
724 * Return: true if host buffer was acquired
725 */
mei_hbuf_acquire(struct mei_device * dev)726 bool mei_hbuf_acquire(struct mei_device *dev)
727 {
728 if (mei_pg_state(dev) == MEI_PG_ON ||
729 mei_pg_in_transition(dev)) {
730 dev_dbg(&dev->dev, "device is in pg\n");
731 return false;
732 }
733
734 if (!dev->hbuf_is_ready) {
735 dev_dbg(&dev->dev, "hbuf is not ready\n");
736 return false;
737 }
738
739 dev->hbuf_is_ready = false;
740
741 return true;
742 }
743
744 /**
745 * mei_cl_wake_all - wake up readers, writers and event waiters so
746 * they can be interrupted
747 *
748 * @cl: host client
749 */
mei_cl_wake_all(struct mei_cl * cl)750 static void mei_cl_wake_all(struct mei_cl *cl)
751 {
752 struct mei_device *dev = cl->dev;
753
754 /* synchronized under device mutex */
755 if (waitqueue_active(&cl->rx_wait)) {
756 cl_dbg(dev, cl, "Waking up reading client!\n");
757 wake_up_interruptible(&cl->rx_wait);
758 }
759 /* synchronized under device mutex */
760 if (waitqueue_active(&cl->tx_wait)) {
761 cl_dbg(dev, cl, "Waking up writing client!\n");
762 wake_up_interruptible(&cl->tx_wait);
763 }
764 /* synchronized under device mutex */
765 if (waitqueue_active(&cl->ev_wait)) {
766 cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
767 wake_up_interruptible(&cl->ev_wait);
768 }
769 /* synchronized under device mutex */
770 if (waitqueue_active(&cl->wait)) {
771 cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
772 wake_up(&cl->wait);
773 }
774 }
775
776 /**
777 * mei_cl_set_disconnected - set disconnected state and clear
778 * associated states and resources
779 *
780 * @cl: host client
781 */
mei_cl_set_disconnected(struct mei_cl * cl)782 static void mei_cl_set_disconnected(struct mei_cl *cl)
783 {
784 struct mei_device *dev = cl->dev;
785
786 if (cl->state == MEI_FILE_DISCONNECTED ||
787 cl->state <= MEI_FILE_INITIALIZING)
788 return;
789
790 cl->state = MEI_FILE_DISCONNECTED;
791 mei_io_tx_list_free_cl(&dev->write_list, cl, NULL);
792 mei_io_tx_list_free_cl(&dev->write_waiting_list, cl, NULL);
793 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
794 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
795 mei_cl_wake_all(cl);
796 cl->rx_flow_ctrl_creds = 0;
797 cl->tx_flow_ctrl_creds = 0;
798 cl->timer_count = 0;
799
800 if (!cl->me_cl)
801 return;
802
803 if (!WARN_ON(cl->me_cl->connect_count == 0))
804 cl->me_cl->connect_count--;
805
806 if (cl->me_cl->connect_count == 0)
807 cl->me_cl->tx_flow_ctrl_creds = 0;
808
809 mei_me_cl_put(cl->me_cl);
810 cl->me_cl = NULL;
811 }
812
mei_cl_set_connecting(struct mei_cl * cl,struct mei_me_client * me_cl)813 static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
814 {
815 if (!mei_me_cl_get(me_cl))
816 return -ENOENT;
817
818 /* only one connection is allowed for fixed address clients */
819 if (me_cl->props.fixed_address) {
820 if (me_cl->connect_count) {
821 mei_me_cl_put(me_cl);
822 return -EBUSY;
823 }
824 }
825
826 cl->me_cl = me_cl;
827 cl->state = MEI_FILE_CONNECTING;
828 cl->me_cl->connect_count++;
829
830 return 0;
831 }
832
833 /*
834 * mei_cl_send_disconnect - send disconnect request
835 *
836 * @cl: host client
837 * @cb: callback block
838 *
839 * Return: 0, OK; otherwise, error.
840 */
mei_cl_send_disconnect(struct mei_cl * cl,struct mei_cl_cb * cb)841 static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
842 {
843 struct mei_device *dev;
844 int ret;
845
846 dev = cl->dev;
847
848 ret = mei_hbm_cl_disconnect_req(dev, cl);
849 cl->status = ret;
850 if (ret) {
851 cl->state = MEI_FILE_DISCONNECT_REPLY;
852 return ret;
853 }
854
855 list_move_tail(&cb->list, &dev->ctrl_rd_list);
856 cl->timer_count = dev->timeouts.connect;
857 mei_schedule_stall_timer(dev);
858
859 return 0;
860 }
861
862 /**
863 * mei_cl_irq_disconnect - processes close related operation from
864 * interrupt thread context - send disconnect request
865 *
866 * @cl: client
867 * @cb: callback block.
868 * @cmpl_list: complete list.
869 *
870 * Return: 0, OK; otherwise, error.
871 */
mei_cl_irq_disconnect(struct mei_cl * cl,struct mei_cl_cb * cb,struct list_head * cmpl_list)872 int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
873 struct list_head *cmpl_list)
874 {
875 struct mei_device *dev = cl->dev;
876 u32 msg_slots;
877 int slots;
878 int ret;
879
880 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
881 slots = mei_hbuf_empty_slots(dev);
882 if (slots < 0)
883 return -EOVERFLOW;
884
885 if ((u32)slots < msg_slots)
886 return -EMSGSIZE;
887
888 ret = mei_cl_send_disconnect(cl, cb);
889 if (ret)
890 list_move_tail(&cb->list, cmpl_list);
891
892 return ret;
893 }
894
895 /**
896 * __mei_cl_disconnect - disconnect host client from the me one
897 * internal function runtime pm has to be already acquired
898 *
899 * @cl: host client
900 *
901 * Return: 0 on success, <0 on failure.
902 */
__mei_cl_disconnect(struct mei_cl * cl)903 static int __mei_cl_disconnect(struct mei_cl *cl)
904 {
905 struct mei_device *dev;
906 struct mei_cl_cb *cb;
907 int rets;
908
909 dev = cl->dev;
910
911 cl->state = MEI_FILE_DISCONNECTING;
912
913 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
914 if (!cb) {
915 rets = -ENOMEM;
916 goto out;
917 }
918
919 if (mei_hbuf_acquire(dev)) {
920 rets = mei_cl_send_disconnect(cl, cb);
921 if (rets) {
922 cl_err(dev, cl, "failed to disconnect.\n");
923 goto out;
924 }
925 }
926
927 mutex_unlock(&dev->device_lock);
928 wait_event_timeout(cl->wait,
929 cl->state == MEI_FILE_DISCONNECT_REPLY ||
930 cl->state == MEI_FILE_DISCONNECTED,
931 dev->timeouts.cl_connect);
932 mutex_lock(&dev->device_lock);
933
934 rets = cl->status;
935 if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
936 cl->state != MEI_FILE_DISCONNECTED) {
937 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
938 rets = -ETIME;
939 }
940
941 out:
942 /* we disconnect also on error */
943 mei_cl_set_disconnected(cl);
944 if (!rets)
945 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
946
947 mei_io_cb_free(cb);
948 return rets;
949 }
950
951 /**
952 * mei_cl_disconnect - disconnect host client from the me one
953 *
954 * @cl: host client
955 *
956 * Locking: called under "dev->device_lock" lock
957 *
958 * Return: 0 on success, <0 on failure.
959 */
mei_cl_disconnect(struct mei_cl * cl)960 int mei_cl_disconnect(struct mei_cl *cl)
961 {
962 struct mei_device *dev;
963 int rets;
964
965 if (WARN_ON(!cl || !cl->dev))
966 return -ENODEV;
967
968 dev = cl->dev;
969
970 cl_dbg(dev, cl, "disconnecting");
971
972 if (!mei_cl_is_connected(cl))
973 return 0;
974
975 if (mei_cl_is_fixed_address(cl)) {
976 mei_cl_set_disconnected(cl);
977 return 0;
978 }
979
980 if (dev->dev_state == MEI_DEV_POWERING_DOWN ||
981 dev->dev_state == MEI_DEV_POWER_DOWN) {
982 cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n");
983 mei_cl_set_disconnected(cl);
984 return 0;
985 }
986
987 rets = pm_runtime_get(dev->parent);
988 if (rets < 0 && rets != -EINPROGRESS) {
989 pm_runtime_put_noidle(dev->parent);
990 cl_err(dev, cl, "rpm: get failed %d\n", rets);
991 return rets;
992 }
993
994 rets = __mei_cl_disconnect(cl);
995
996 cl_dbg(dev, cl, "rpm: autosuspend\n");
997 pm_runtime_put_autosuspend(dev->parent);
998
999 return rets;
1000 }
1001
1002
1003 /**
1004 * mei_cl_is_other_connecting - checks if other
1005 * client with the same me client id is connecting
1006 *
1007 * @cl: private data of the file object
1008 *
1009 * Return: true if other client is connected, false - otherwise.
1010 */
mei_cl_is_other_connecting(struct mei_cl * cl)1011 static bool mei_cl_is_other_connecting(struct mei_cl *cl)
1012 {
1013 struct mei_device *dev;
1014 struct mei_cl_cb *cb;
1015
1016 dev = cl->dev;
1017
1018 list_for_each_entry(cb, &dev->ctrl_rd_list, list) {
1019 if (cb->fop_type == MEI_FOP_CONNECT &&
1020 mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
1021 return true;
1022 }
1023
1024 return false;
1025 }
1026
1027 /**
1028 * mei_cl_send_connect - send connect request
1029 *
1030 * @cl: host client
1031 * @cb: callback block
1032 *
1033 * Return: 0, OK; otherwise, error.
1034 */
mei_cl_send_connect(struct mei_cl * cl,struct mei_cl_cb * cb)1035 static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
1036 {
1037 struct mei_device *dev;
1038 int ret;
1039
1040 dev = cl->dev;
1041
1042 ret = mei_hbm_cl_connect_req(dev, cl);
1043 cl->status = ret;
1044 if (ret) {
1045 cl->state = MEI_FILE_DISCONNECT_REPLY;
1046 return ret;
1047 }
1048
1049 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1050 cl->timer_count = dev->timeouts.connect;
1051 mei_schedule_stall_timer(dev);
1052 return 0;
1053 }
1054
1055 /**
1056 * mei_cl_irq_connect - send connect request in irq_thread context
1057 *
1058 * @cl: host client
1059 * @cb: callback block
1060 * @cmpl_list: complete list
1061 *
1062 * Return: 0, OK; otherwise, error.
1063 */
mei_cl_irq_connect(struct mei_cl * cl,struct mei_cl_cb * cb,struct list_head * cmpl_list)1064 int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
1065 struct list_head *cmpl_list)
1066 {
1067 struct mei_device *dev = cl->dev;
1068 u32 msg_slots;
1069 int slots;
1070 int rets;
1071
1072 if (mei_cl_is_other_connecting(cl))
1073 return 0;
1074
1075 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1076 slots = mei_hbuf_empty_slots(dev);
1077 if (slots < 0)
1078 return -EOVERFLOW;
1079
1080 if ((u32)slots < msg_slots)
1081 return -EMSGSIZE;
1082
1083 rets = mei_cl_send_connect(cl, cb);
1084 if (rets)
1085 list_move_tail(&cb->list, cmpl_list);
1086
1087 return rets;
1088 }
1089
1090 /**
1091 * mei_cl_connect - connect host client to the me one
1092 *
1093 * @cl: host client
1094 * @me_cl: me client
1095 * @fp: pointer to file structure
1096 *
1097 * Locking: called under "dev->device_lock" lock
1098 *
1099 * Return: 0 on success, <0 on failure.
1100 */
mei_cl_connect(struct mei_cl * cl,struct mei_me_client * me_cl,const struct file * fp)1101 int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
1102 const struct file *fp)
1103 {
1104 struct mei_device *dev;
1105 struct mei_cl_cb *cb;
1106 int rets;
1107
1108 if (WARN_ON(!cl || !cl->dev || !me_cl))
1109 return -ENODEV;
1110
1111 dev = cl->dev;
1112
1113 rets = mei_cl_set_connecting(cl, me_cl);
1114 if (rets)
1115 goto nortpm;
1116
1117 if (mei_cl_is_fixed_address(cl)) {
1118 cl->state = MEI_FILE_CONNECTED;
1119 rets = 0;
1120 goto nortpm;
1121 }
1122
1123 rets = pm_runtime_get(dev->parent);
1124 if (rets < 0 && rets != -EINPROGRESS) {
1125 pm_runtime_put_noidle(dev->parent);
1126 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1127 goto nortpm;
1128 }
1129
1130 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
1131 if (!cb) {
1132 rets = -ENOMEM;
1133 goto out;
1134 }
1135
1136 /* run hbuf acquire last so we don't have to undo */
1137 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
1138 rets = mei_cl_send_connect(cl, cb);
1139 if (rets)
1140 goto out;
1141 }
1142
1143 mutex_unlock(&dev->device_lock);
1144 wait_event_timeout(cl->wait,
1145 (cl->state == MEI_FILE_CONNECTED ||
1146 cl->state == MEI_FILE_DISCONNECTED ||
1147 cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
1148 cl->state == MEI_FILE_DISCONNECT_REPLY),
1149 dev->timeouts.cl_connect);
1150 mutex_lock(&dev->device_lock);
1151
1152 if (!mei_cl_is_connected(cl)) {
1153 if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
1154 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
1155 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
1156 /* ignore disconnect return valuue;
1157 * in case of failure reset will be invoked
1158 */
1159 __mei_cl_disconnect(cl);
1160 rets = -EFAULT;
1161 goto out;
1162 }
1163
1164 /* timeout or something went really wrong */
1165 if (!cl->status)
1166 cl->status = -EFAULT;
1167 }
1168
1169 rets = cl->status;
1170 out:
1171 cl_dbg(dev, cl, "rpm: autosuspend\n");
1172 pm_runtime_put_autosuspend(dev->parent);
1173
1174 mei_io_cb_free(cb);
1175
1176 nortpm:
1177 if (!mei_cl_is_connected(cl))
1178 mei_cl_set_disconnected(cl);
1179
1180 return rets;
1181 }
1182
1183 /**
1184 * mei_cl_alloc_linked - allocate and link host client
1185 *
1186 * @dev: the device structure
1187 *
1188 * Return: cl on success ERR_PTR on failure
1189 */
mei_cl_alloc_linked(struct mei_device * dev)1190 struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
1191 {
1192 struct mei_cl *cl;
1193 int ret;
1194
1195 cl = mei_cl_allocate(dev);
1196 if (!cl) {
1197 ret = -ENOMEM;
1198 goto err;
1199 }
1200
1201 ret = mei_cl_link(cl);
1202 if (ret)
1203 goto err;
1204
1205 return cl;
1206 err:
1207 kfree(cl);
1208 return ERR_PTR(ret);
1209 }
1210
1211 /**
1212 * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
1213 *
1214 * @cl: host client
1215 *
1216 * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
1217 */
mei_cl_tx_flow_ctrl_creds(struct mei_cl * cl)1218 static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
1219 {
1220 if (WARN_ON(!cl || !cl->me_cl))
1221 return -EINVAL;
1222
1223 if (cl->tx_flow_ctrl_creds > 0)
1224 return 1;
1225
1226 if (mei_cl_is_fixed_address(cl))
1227 return 1;
1228
1229 if (mei_cl_is_single_recv_buf(cl)) {
1230 if (cl->me_cl->tx_flow_ctrl_creds > 0)
1231 return 1;
1232 }
1233 return 0;
1234 }
1235
1236 /**
1237 * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
1238 * for a client
1239 *
1240 * @cl: host client
1241 *
1242 * Return:
1243 * 0 on success
1244 * -EINVAL when ctrl credits are <= 0
1245 */
mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl * cl)1246 static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
1247 {
1248 if (WARN_ON(!cl || !cl->me_cl))
1249 return -EINVAL;
1250
1251 if (mei_cl_is_fixed_address(cl))
1252 return 0;
1253
1254 if (mei_cl_is_single_recv_buf(cl)) {
1255 if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
1256 return -EINVAL;
1257 cl->me_cl->tx_flow_ctrl_creds--;
1258 } else {
1259 if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
1260 return -EINVAL;
1261 cl->tx_flow_ctrl_creds--;
1262 }
1263 return 0;
1264 }
1265
1266 /**
1267 * mei_cl_vtag_alloc - allocate and fill the vtag structure
1268 *
1269 * @fp: pointer to file structure
1270 * @vtag: vm tag
1271 *
1272 * Return:
1273 * * Pointer to allocated struct - on success
1274 * * ERR_PTR(-ENOMEM) on memory allocation failure
1275 */
mei_cl_vtag_alloc(struct file * fp,u8 vtag)1276 struct mei_cl_vtag *mei_cl_vtag_alloc(struct file *fp, u8 vtag)
1277 {
1278 struct mei_cl_vtag *cl_vtag;
1279
1280 cl_vtag = kzalloc_obj(*cl_vtag);
1281 if (!cl_vtag)
1282 return ERR_PTR(-ENOMEM);
1283
1284 INIT_LIST_HEAD(&cl_vtag->list);
1285 cl_vtag->vtag = vtag;
1286 cl_vtag->fp = fp;
1287
1288 return cl_vtag;
1289 }
1290
1291 /**
1292 * mei_cl_fp_by_vtag - obtain the file pointer by vtag
1293 *
1294 * @cl: host client
1295 * @vtag: virtual tag
1296 *
1297 * Return:
1298 * * A file pointer - on success
1299 * * ERR_PTR(-ENOENT) if vtag is not found in the client vtag list
1300 */
mei_cl_fp_by_vtag(const struct mei_cl * cl,u8 vtag)1301 const struct file *mei_cl_fp_by_vtag(const struct mei_cl *cl, u8 vtag)
1302 {
1303 struct mei_cl_vtag *vtag_l;
1304
1305 list_for_each_entry(vtag_l, &cl->vtag_map, list)
1306 /* The client on bus has one fixed fp */
1307 if ((cl->cldev && mei_cldev_enabled(cl->cldev)) ||
1308 vtag_l->vtag == vtag)
1309 return vtag_l->fp;
1310
1311 return ERR_PTR(-ENOENT);
1312 }
1313
1314 /**
1315 * mei_cl_reset_read_by_vtag - reset pending_read flag by given vtag
1316 *
1317 * @cl: host client
1318 * @vtag: vm tag
1319 */
mei_cl_reset_read_by_vtag(const struct mei_cl * cl,u8 vtag)1320 static void mei_cl_reset_read_by_vtag(const struct mei_cl *cl, u8 vtag)
1321 {
1322 struct mei_cl_vtag *vtag_l;
1323
1324 list_for_each_entry(vtag_l, &cl->vtag_map, list) {
1325 /* The client on bus has one fixed vtag map */
1326 if ((cl->cldev && mei_cldev_enabled(cl->cldev)) ||
1327 vtag_l->vtag == vtag) {
1328 vtag_l->pending_read = false;
1329 break;
1330 }
1331 }
1332 }
1333
1334 /**
1335 * mei_cl_read_vtag_add_fc - add flow control for next pending reader
1336 * in the vtag list
1337 *
1338 * @cl: host client
1339 */
mei_cl_read_vtag_add_fc(struct mei_cl * cl)1340 static void mei_cl_read_vtag_add_fc(struct mei_cl *cl)
1341 {
1342 struct mei_cl_vtag *cl_vtag;
1343
1344 list_for_each_entry(cl_vtag, &cl->vtag_map, list) {
1345 if (cl_vtag->pending_read) {
1346 if (mei_cl_enqueue_ctrl_wr_cb(cl,
1347 mei_cl_mtu(cl),
1348 MEI_FOP_READ,
1349 cl_vtag->fp))
1350 cl->rx_flow_ctrl_creds++;
1351 break;
1352 }
1353 }
1354 }
1355
1356 /**
1357 * mei_cl_vt_support_check - check if client support vtags
1358 *
1359 * @cl: host client
1360 *
1361 * Return:
1362 * * 0 - supported, or not connected at all
1363 * * -EOPNOTSUPP - vtags are not supported by client
1364 */
mei_cl_vt_support_check(const struct mei_cl * cl)1365 int mei_cl_vt_support_check(const struct mei_cl *cl)
1366 {
1367 struct mei_device *dev = cl->dev;
1368
1369 if (!dev->hbm_f_vt_supported)
1370 return -EOPNOTSUPP;
1371
1372 if (!cl->me_cl)
1373 return 0;
1374
1375 return cl->me_cl->props.vt_supported ? 0 : -EOPNOTSUPP;
1376 }
1377
1378 /**
1379 * mei_cl_add_rd_completed - add read completed callback to list with lock
1380 * and vtag check
1381 *
1382 * @cl: host client
1383 * @cb: callback block
1384 *
1385 */
mei_cl_add_rd_completed(struct mei_cl * cl,struct mei_cl_cb * cb)1386 void mei_cl_add_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
1387 {
1388 const struct file *fp;
1389
1390 if (!mei_cl_vt_support_check(cl)) {
1391 fp = mei_cl_fp_by_vtag(cl, cb->vtag);
1392 if (IS_ERR(fp)) {
1393 /* client already disconnected, discarding */
1394 mei_io_cb_free(cb);
1395 return;
1396 }
1397 cb->fp = fp;
1398 mei_cl_reset_read_by_vtag(cl, cb->vtag);
1399 mei_cl_read_vtag_add_fc(cl);
1400 }
1401
1402 spin_lock(&cl->rd_completed_lock);
1403 list_add_tail(&cb->list, &cl->rd_completed);
1404 spin_unlock(&cl->rd_completed_lock);
1405 }
1406
1407 /**
1408 * mei_cl_del_rd_completed - unlink read completed callback with lock and free it
1409 *
1410 * @cl: host client
1411 * @cb: callback block
1412 *
1413 */
mei_cl_del_rd_completed(struct mei_cl * cl,struct mei_cl_cb * cb)1414 void mei_cl_del_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
1415 {
1416 spin_lock(&cl->rd_completed_lock);
1417 list_del_init(&cb->list);
1418 spin_unlock(&cl->rd_completed_lock);
1419 mei_io_cb_free(cb);
1420 }
1421
1422 /**
1423 * mei_cl_notify_fop2req - convert fop to proper request
1424 *
1425 * @fop: client notification start response command
1426 *
1427 * Return: MEI_HBM_NOTIFICATION_START/STOP
1428 */
mei_cl_notify_fop2req(enum mei_cb_file_ops fop)1429 u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
1430 {
1431 if (fop == MEI_FOP_NOTIFY_START)
1432 return MEI_HBM_NOTIFICATION_START;
1433 else
1434 return MEI_HBM_NOTIFICATION_STOP;
1435 }
1436
1437 /**
1438 * mei_cl_notify_req2fop - convert notification request top file operation type
1439 *
1440 * @req: hbm notification request type
1441 *
1442 * Return: MEI_FOP_NOTIFY_START/STOP
1443 */
mei_cl_notify_req2fop(u8 req)1444 enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
1445 {
1446 if (req == MEI_HBM_NOTIFICATION_START)
1447 return MEI_FOP_NOTIFY_START;
1448 else
1449 return MEI_FOP_NOTIFY_STOP;
1450 }
1451
1452 /**
1453 * mei_cl_irq_notify - send notification request in irq_thread context
1454 *
1455 * @cl: client
1456 * @cb: callback block.
1457 * @cmpl_list: complete list.
1458 *
1459 * Return: 0 on such and error otherwise.
1460 */
mei_cl_irq_notify(struct mei_cl * cl,struct mei_cl_cb * cb,struct list_head * cmpl_list)1461 int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
1462 struct list_head *cmpl_list)
1463 {
1464 struct mei_device *dev = cl->dev;
1465 u32 msg_slots;
1466 int slots;
1467 int ret;
1468 bool request;
1469
1470 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1471 slots = mei_hbuf_empty_slots(dev);
1472 if (slots < 0)
1473 return -EOVERFLOW;
1474
1475 if ((u32)slots < msg_slots)
1476 return -EMSGSIZE;
1477
1478 request = mei_cl_notify_fop2req(cb->fop_type);
1479 ret = mei_hbm_cl_notify_req(dev, cl, request);
1480 if (ret) {
1481 cl->status = ret;
1482 list_move_tail(&cb->list, cmpl_list);
1483 return ret;
1484 }
1485
1486 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1487 return 0;
1488 }
1489
1490 /**
1491 * mei_cl_notify_request - send notification stop/start request
1492 *
1493 * @cl: host client
1494 * @fp: associate request with file
1495 * @request: 1 for start or 0 for stop
1496 *
1497 * Locking: called under "dev->device_lock" lock
1498 *
1499 * Return: 0 on such and error otherwise.
1500 */
mei_cl_notify_request(struct mei_cl * cl,const struct file * fp,u8 request)1501 int mei_cl_notify_request(struct mei_cl *cl,
1502 const struct file *fp, u8 request)
1503 {
1504 struct mei_device *dev;
1505 struct mei_cl_cb *cb;
1506 enum mei_cb_file_ops fop_type;
1507 int rets;
1508
1509 if (WARN_ON(!cl || !cl->dev))
1510 return -ENODEV;
1511
1512 dev = cl->dev;
1513
1514 if (!dev->hbm_f_ev_supported) {
1515 cl_dbg(dev, cl, "notifications not supported\n");
1516 return -EOPNOTSUPP;
1517 }
1518
1519 if (!mei_cl_is_connected(cl))
1520 return -ENODEV;
1521
1522 rets = pm_runtime_get(dev->parent);
1523 if (rets < 0 && rets != -EINPROGRESS) {
1524 pm_runtime_put_noidle(dev->parent);
1525 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1526 return rets;
1527 }
1528
1529 fop_type = mei_cl_notify_req2fop(request);
1530 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
1531 if (!cb) {
1532 rets = -ENOMEM;
1533 goto out;
1534 }
1535
1536 if (mei_hbuf_acquire(dev)) {
1537 if (mei_hbm_cl_notify_req(dev, cl, request)) {
1538 rets = -ENODEV;
1539 goto out;
1540 }
1541 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1542 }
1543
1544 mutex_unlock(&dev->device_lock);
1545 wait_event_timeout(cl->wait,
1546 cl->notify_en == request ||
1547 cl->status ||
1548 !mei_cl_is_connected(cl),
1549 dev->timeouts.cl_connect);
1550 mutex_lock(&dev->device_lock);
1551
1552 if (cl->notify_en != request && !cl->status)
1553 cl->status = -EFAULT;
1554
1555 rets = cl->status;
1556
1557 out:
1558 cl_dbg(dev, cl, "rpm: autosuspend\n");
1559 pm_runtime_put_autosuspend(dev->parent);
1560
1561 mei_io_cb_free(cb);
1562 return rets;
1563 }
1564
1565 /**
1566 * mei_cl_notify - raise notification
1567 *
1568 * @cl: host client
1569 *
1570 * Locking: called under "dev->device_lock" lock
1571 */
mei_cl_notify(struct mei_cl * cl)1572 void mei_cl_notify(struct mei_cl *cl)
1573 {
1574 struct mei_device *dev;
1575
1576 if (!cl || !cl->dev)
1577 return;
1578
1579 dev = cl->dev;
1580
1581 if (!cl->notify_en)
1582 return;
1583
1584 cl_dbg(dev, cl, "notify event");
1585 cl->notify_ev = true;
1586 if (!mei_cl_bus_notify_event(cl))
1587 wake_up_interruptible(&cl->ev_wait);
1588
1589 if (cl->ev_async)
1590 kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
1591
1592 }
1593
1594 /**
1595 * mei_cl_notify_get - get or wait for notification event
1596 *
1597 * @cl: host client
1598 * @block: this request is blocking
1599 * @notify_ev: true if notification event was received
1600 *
1601 * Locking: called under "dev->device_lock" lock
1602 *
1603 * Return: 0 on such and error otherwise.
1604 */
mei_cl_notify_get(struct mei_cl * cl,bool block,bool * notify_ev)1605 int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
1606 {
1607 struct mei_device *dev;
1608 int rets;
1609
1610 *notify_ev = false;
1611
1612 if (WARN_ON(!cl || !cl->dev))
1613 return -ENODEV;
1614
1615 dev = cl->dev;
1616
1617 if (!dev->hbm_f_ev_supported) {
1618 cl_dbg(dev, cl, "notifications not supported\n");
1619 return -EOPNOTSUPP;
1620 }
1621
1622 if (!mei_cl_is_connected(cl))
1623 return -ENODEV;
1624
1625 if (cl->notify_ev)
1626 goto out;
1627
1628 if (!block)
1629 return -EAGAIN;
1630
1631 mutex_unlock(&dev->device_lock);
1632 rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
1633 mutex_lock(&dev->device_lock);
1634
1635 if (rets < 0)
1636 return rets;
1637
1638 out:
1639 *notify_ev = cl->notify_ev;
1640 cl->notify_ev = false;
1641 return 0;
1642 }
1643
1644 /**
1645 * mei_cl_read_start - the start read client message function.
1646 *
1647 * @cl: host client
1648 * @length: number of bytes to read
1649 * @fp: pointer to file structure
1650 *
1651 * Return: 0 on success, <0 on failure.
1652 */
mei_cl_read_start(struct mei_cl * cl,size_t length,const struct file * fp)1653 int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
1654 {
1655 struct mei_device *dev;
1656 struct mei_cl_cb *cb;
1657 int rets;
1658
1659 if (WARN_ON(!cl || !cl->dev))
1660 return -ENODEV;
1661
1662 dev = cl->dev;
1663
1664 if (!mei_cl_is_connected(cl))
1665 return -ENODEV;
1666
1667 if (!mei_me_cl_is_active(cl->me_cl)) {
1668 cl_err(dev, cl, "no such me client\n");
1669 return -ENOTTY;
1670 }
1671
1672 if (mei_cl_is_fixed_address(cl))
1673 return 0;
1674
1675 /* HW currently supports only one pending read */
1676 if (cl->rx_flow_ctrl_creds) {
1677 mei_cl_set_read_by_fp(cl, fp);
1678 return -EBUSY;
1679 }
1680
1681 cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
1682 if (!cb)
1683 return -ENOMEM;
1684
1685 mei_cl_set_read_by_fp(cl, fp);
1686
1687 rets = pm_runtime_get(dev->parent);
1688 if (rets < 0 && rets != -EINPROGRESS) {
1689 pm_runtime_put_noidle(dev->parent);
1690 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1691 goto nortpm;
1692 }
1693
1694 rets = 0;
1695 if (mei_hbuf_acquire(dev)) {
1696 rets = mei_hbm_cl_flow_control_req(dev, cl);
1697 if (rets < 0)
1698 goto out;
1699
1700 list_move_tail(&cb->list, &cl->rd_pending);
1701 }
1702 cl->rx_flow_ctrl_creds++;
1703
1704 out:
1705 cl_dbg(dev, cl, "rpm: autosuspend\n");
1706 pm_runtime_put_autosuspend(dev->parent);
1707 nortpm:
1708 if (rets)
1709 mei_io_cb_free(cb);
1710
1711 return rets;
1712 }
1713
mei_ext_hdr_set_vtag(void * ext,u8 vtag)1714 static inline u8 mei_ext_hdr_set_vtag(void *ext, u8 vtag)
1715 {
1716 struct mei_ext_hdr_vtag *vtag_hdr = ext;
1717
1718 vtag_hdr->hdr.type = MEI_EXT_HDR_VTAG;
1719 vtag_hdr->hdr.length = mei_data2slots(sizeof(*vtag_hdr));
1720 vtag_hdr->vtag = vtag;
1721 vtag_hdr->reserved = 0;
1722 return vtag_hdr->hdr.length;
1723 }
1724
mei_ext_hdr_is_gsc(struct mei_ext_hdr * ext)1725 static inline bool mei_ext_hdr_is_gsc(struct mei_ext_hdr *ext)
1726 {
1727 return ext && ext->type == MEI_EXT_HDR_GSC;
1728 }
1729
mei_ext_hdr_set_gsc(struct mei_ext_hdr * ext,struct mei_ext_hdr * gsc_hdr)1730 static inline u8 mei_ext_hdr_set_gsc(struct mei_ext_hdr *ext, struct mei_ext_hdr *gsc_hdr)
1731 {
1732 memcpy(ext, gsc_hdr, mei_ext_hdr_len(gsc_hdr));
1733 return ext->length;
1734 }
1735
1736 /**
1737 * mei_msg_hdr_init - allocate and initialize mei message header
1738 *
1739 * @cb: message callback structure
1740 *
1741 * Return: a pointer to initialized header or ERR_PTR on failure
1742 */
mei_msg_hdr_init(const struct mei_cl_cb * cb)1743 static struct mei_msg_hdr *mei_msg_hdr_init(const struct mei_cl_cb *cb)
1744 {
1745 size_t hdr_len;
1746 struct mei_ext_meta_hdr *meta;
1747 struct mei_msg_hdr *mei_hdr;
1748 bool is_ext, is_hbm, is_gsc, is_vtag;
1749 struct mei_ext_hdr *next_ext;
1750
1751 if (!cb)
1752 return ERR_PTR(-EINVAL);
1753
1754 /* Extended header for vtag is attached only on the first fragment */
1755 is_vtag = (cb->vtag && cb->buf_idx == 0);
1756 is_hbm = cb->cl->me_cl->client_id == 0;
1757 is_gsc = ((!is_hbm) && cb->cl->dev->hbm_f_gsc_supported && mei_ext_hdr_is_gsc(cb->ext_hdr));
1758 is_ext = is_vtag || is_gsc;
1759
1760 /* Compute extended header size */
1761 hdr_len = sizeof(*mei_hdr);
1762
1763 if (!is_ext)
1764 goto setup_hdr;
1765
1766 hdr_len += sizeof(*meta);
1767 if (is_vtag)
1768 hdr_len += sizeof(struct mei_ext_hdr_vtag);
1769
1770 if (is_gsc)
1771 hdr_len += mei_ext_hdr_len(cb->ext_hdr);
1772
1773 setup_hdr:
1774 mei_hdr = kzalloc(hdr_len, GFP_KERNEL);
1775 if (!mei_hdr)
1776 return ERR_PTR(-ENOMEM);
1777
1778 mei_hdr->host_addr = mei_cl_host_addr(cb->cl);
1779 mei_hdr->me_addr = mei_cl_me_id(cb->cl);
1780 mei_hdr->internal = cb->internal;
1781 mei_hdr->extended = is_ext;
1782
1783 if (!is_ext)
1784 goto out;
1785
1786 meta = (struct mei_ext_meta_hdr *)mei_hdr->extension;
1787 meta->size = 0;
1788 next_ext = (struct mei_ext_hdr *)meta->hdrs;
1789 if (is_vtag) {
1790 meta->count++;
1791 meta->size += mei_ext_hdr_set_vtag(next_ext, cb->vtag);
1792 next_ext = mei_ext_next(next_ext);
1793 }
1794
1795 if (is_gsc) {
1796 meta->count++;
1797 meta->size += mei_ext_hdr_set_gsc(next_ext, cb->ext_hdr);
1798 next_ext = mei_ext_next(next_ext);
1799 }
1800
1801 out:
1802 mei_hdr->length = hdr_len - sizeof(*mei_hdr);
1803 return mei_hdr;
1804 }
1805
1806 /**
1807 * mei_cl_irq_write - write a message to device
1808 * from the interrupt thread context
1809 *
1810 * @cl: client
1811 * @cb: callback block.
1812 * @cmpl_list: complete list.
1813 *
1814 * Return: 0, OK; otherwise error.
1815 */
mei_cl_irq_write(struct mei_cl * cl,struct mei_cl_cb * cb,struct list_head * cmpl_list)1816 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1817 struct list_head *cmpl_list)
1818 {
1819 struct mei_device *dev;
1820 struct mei_msg_data *buf;
1821 struct mei_msg_hdr *mei_hdr = NULL;
1822 size_t hdr_len;
1823 size_t hbuf_len, dr_len;
1824 size_t buf_len = 0;
1825 size_t data_len;
1826 int hbuf_slots;
1827 u32 dr_slots;
1828 u32 dma_len;
1829 int rets;
1830 bool first_chunk;
1831 const void *data = NULL;
1832
1833 if (WARN_ON(!cl || !cl->dev))
1834 return -ENODEV;
1835
1836 dev = cl->dev;
1837
1838 buf = &cb->buf;
1839
1840 first_chunk = cb->buf_idx == 0;
1841
1842 rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
1843 if (rets < 0)
1844 goto err;
1845
1846 if (rets == 0) {
1847 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1848 return 0;
1849 }
1850
1851 if (buf->data) {
1852 buf_len = buf->size - cb->buf_idx;
1853 data = buf->data + cb->buf_idx;
1854 }
1855 hbuf_slots = mei_hbuf_empty_slots(dev);
1856 if (hbuf_slots < 0) {
1857 rets = -EOVERFLOW;
1858 goto err;
1859 }
1860
1861 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
1862 dr_slots = mei_dma_ring_empty_slots(dev);
1863 dr_len = mei_slots2data(dr_slots);
1864
1865 mei_hdr = mei_msg_hdr_init(cb);
1866 if (IS_ERR(mei_hdr)) {
1867 rets = PTR_ERR(mei_hdr);
1868 mei_hdr = NULL;
1869 goto err;
1870 }
1871
1872 hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
1873
1874 /**
1875 * Split the message only if we can write the whole host buffer
1876 * otherwise wait for next time the host buffer is empty.
1877 */
1878 if (hdr_len + buf_len <= hbuf_len) {
1879 data_len = buf_len;
1880 mei_hdr->msg_complete = 1;
1881 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
1882 mei_hdr->dma_ring = 1;
1883 if (buf_len > dr_len)
1884 buf_len = dr_len;
1885 else
1886 mei_hdr->msg_complete = 1;
1887
1888 data_len = sizeof(dma_len);
1889 dma_len = buf_len;
1890 data = &dma_len;
1891 } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
1892 buf_len = hbuf_len - hdr_len;
1893 data_len = buf_len;
1894 } else {
1895 kfree(mei_hdr);
1896 return 0;
1897 }
1898 mei_hdr->length += data_len;
1899
1900 if (mei_hdr->dma_ring && buf->data)
1901 mei_dma_ring_write(dev, buf->data + cb->buf_idx, buf_len);
1902 rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
1903
1904 if (rets)
1905 goto err;
1906
1907 cl->status = 0;
1908 cl->writing_state = MEI_WRITING;
1909 cb->buf_idx += buf_len;
1910
1911 if (first_chunk) {
1912 if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
1913 rets = -EIO;
1914 goto err;
1915 }
1916 }
1917
1918 if (mei_hdr->msg_complete)
1919 list_move_tail(&cb->list, &dev->write_waiting_list);
1920
1921 kfree(mei_hdr);
1922 return 0;
1923
1924 err:
1925 kfree(mei_hdr);
1926 cl->status = rets;
1927 list_move_tail(&cb->list, cmpl_list);
1928 return rets;
1929 }
1930
1931 /**
1932 * mei_cl_write - submit a write cb to mei device
1933 * assumes device_lock is locked
1934 *
1935 * @cl: host client
1936 * @cb: write callback with filled data
1937 * @timeout: send timeout in milliseconds.
1938 * effective only for blocking writes: the cb->blocking is set.
1939 * set timeout to the MAX_SCHEDULE_TIMEOUT to maixum allowed wait.
1940 *
1941 * Return: number of bytes sent on success, <0 on failure.
1942 */
mei_cl_write(struct mei_cl * cl,struct mei_cl_cb * cb,unsigned long timeout)1943 ssize_t mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, unsigned long timeout)
1944 {
1945 struct mei_device *dev;
1946 struct mei_msg_data *buf;
1947 struct mei_msg_hdr *mei_hdr = NULL;
1948 size_t hdr_len;
1949 size_t hbuf_len, dr_len;
1950 size_t buf_len;
1951 size_t data_len;
1952 int hbuf_slots;
1953 u32 dr_slots;
1954 u32 dma_len;
1955 ssize_t rets;
1956 bool blocking;
1957 const void *data;
1958
1959 if (WARN_ON(!cl || !cl->dev))
1960 return -ENODEV;
1961
1962 if (WARN_ON(!cb))
1963 return -EINVAL;
1964
1965 dev = cl->dev;
1966
1967 buf = &cb->buf;
1968 buf_len = buf->size;
1969
1970 cl_dbg(dev, cl, "buf_len=%zd\n", buf_len);
1971
1972 blocking = cb->blocking;
1973 data = buf->data;
1974
1975 rets = pm_runtime_get(dev->parent);
1976 if (rets < 0 && rets != -EINPROGRESS) {
1977 pm_runtime_put_noidle(dev->parent);
1978 cl_err(dev, cl, "rpm: get failed %zd\n", rets);
1979 goto free;
1980 }
1981
1982 cb->buf_idx = 0;
1983 cl->writing_state = MEI_IDLE;
1984
1985
1986 rets = mei_cl_tx_flow_ctrl_creds(cl);
1987 if (rets < 0)
1988 goto err;
1989
1990 mei_hdr = mei_msg_hdr_init(cb);
1991 if (IS_ERR(mei_hdr)) {
1992 rets = PTR_ERR(mei_hdr);
1993 mei_hdr = NULL;
1994 goto err;
1995 }
1996
1997 hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
1998
1999 if (rets == 0) {
2000 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
2001 rets = buf_len;
2002 goto out;
2003 }
2004
2005 if (!mei_hbuf_acquire(dev)) {
2006 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
2007 rets = buf_len;
2008 goto out;
2009 }
2010
2011 hbuf_slots = mei_hbuf_empty_slots(dev);
2012 if (hbuf_slots < 0) {
2013 buf_len = -EOVERFLOW;
2014 goto out;
2015 }
2016
2017 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
2018 dr_slots = mei_dma_ring_empty_slots(dev);
2019 dr_len = mei_slots2data(dr_slots);
2020
2021 if (hdr_len + buf_len <= hbuf_len) {
2022 data_len = buf_len;
2023 mei_hdr->msg_complete = 1;
2024 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
2025 mei_hdr->dma_ring = 1;
2026 if (buf_len > dr_len)
2027 buf_len = dr_len;
2028 else
2029 mei_hdr->msg_complete = 1;
2030
2031 data_len = sizeof(dma_len);
2032 dma_len = buf_len;
2033 data = &dma_len;
2034 } else {
2035 buf_len = hbuf_len - hdr_len;
2036 data_len = buf_len;
2037 }
2038
2039 mei_hdr->length += data_len;
2040
2041 if (mei_hdr->dma_ring && buf->data)
2042 mei_dma_ring_write(dev, buf->data, buf_len);
2043 rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
2044
2045 if (rets)
2046 goto err;
2047
2048 rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
2049 if (rets)
2050 goto err;
2051
2052 cl->writing_state = MEI_WRITING;
2053 cb->buf_idx = buf_len;
2054 /* restore return value */
2055 buf_len = buf->size;
2056
2057 out:
2058 if (mei_hdr->msg_complete)
2059 mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
2060 else
2061 mei_tx_cb_enqueue(cb, &dev->write_list);
2062
2063 cb = NULL;
2064 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
2065
2066 mutex_unlock(&dev->device_lock);
2067 rets = wait_event_interruptible_timeout(cl->tx_wait,
2068 cl->writing_state == MEI_WRITE_COMPLETE ||
2069 (!mei_cl_is_connected(cl)),
2070 msecs_to_jiffies(timeout));
2071 mutex_lock(&dev->device_lock);
2072 /* clean all queue on timeout as something fatal happened */
2073 if (rets == 0) {
2074 rets = -ETIME;
2075 mei_io_tx_list_free_cl(&dev->write_list, cl, NULL);
2076 mei_io_tx_list_free_cl(&dev->write_waiting_list, cl, NULL);
2077 }
2078 /* wait_event_interruptible returns -ERESTARTSYS */
2079 if (rets > 0)
2080 rets = 0;
2081 if (rets) {
2082 if (signal_pending(current))
2083 rets = -EINTR;
2084 goto err;
2085 }
2086 if (cl->writing_state != MEI_WRITE_COMPLETE) {
2087 rets = -EFAULT;
2088 goto err;
2089 }
2090 }
2091
2092 rets = buf_len;
2093 err:
2094 cl_dbg(dev, cl, "rpm: autosuspend\n");
2095 pm_runtime_put_autosuspend(dev->parent);
2096 free:
2097 mei_io_cb_free(cb);
2098
2099 kfree(mei_hdr);
2100
2101 return rets;
2102 }
2103
2104 /**
2105 * mei_cl_complete - processes completed operation for a client
2106 *
2107 * @cl: private data of the file object.
2108 * @cb: callback block.
2109 */
mei_cl_complete(struct mei_cl * cl,struct mei_cl_cb * cb)2110 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
2111 {
2112 struct mei_device *dev = cl->dev;
2113
2114 switch (cb->fop_type) {
2115 case MEI_FOP_WRITE:
2116 mei_tx_cb_dequeue(cb);
2117 cl->writing_state = MEI_WRITE_COMPLETE;
2118 if (waitqueue_active(&cl->tx_wait))
2119 wake_up_interruptible(&cl->tx_wait);
2120 else
2121 pm_request_autosuspend(dev->parent);
2122 break;
2123
2124 case MEI_FOP_READ:
2125 mei_cl_add_rd_completed(cl, cb);
2126 if (!mei_cl_is_fixed_address(cl) &&
2127 !WARN_ON(!cl->rx_flow_ctrl_creds))
2128 cl->rx_flow_ctrl_creds--;
2129 if (!mei_cl_bus_rx_event(cl))
2130 wake_up_interruptible(&cl->rx_wait);
2131 break;
2132
2133 case MEI_FOP_CONNECT:
2134 case MEI_FOP_DISCONNECT:
2135 case MEI_FOP_NOTIFY_STOP:
2136 case MEI_FOP_NOTIFY_START:
2137 case MEI_FOP_DMA_MAP:
2138 case MEI_FOP_DMA_UNMAP:
2139 if (waitqueue_active(&cl->wait))
2140 wake_up(&cl->wait);
2141
2142 break;
2143 case MEI_FOP_DISCONNECT_RSP:
2144 mei_io_cb_free(cb);
2145 mei_cl_set_disconnected(cl);
2146 break;
2147 default:
2148 BUG_ON(0);
2149 }
2150 }
2151
2152
2153 /**
2154 * mei_cl_all_disconnect - disconnect forcefully all connected clients
2155 *
2156 * @dev: mei device
2157 */
mei_cl_all_disconnect(struct mei_device * dev)2158 void mei_cl_all_disconnect(struct mei_device *dev)
2159 {
2160 struct mei_cl *cl;
2161
2162 list_for_each_entry(cl, &dev->file_list, link)
2163 mei_cl_set_disconnected(cl);
2164 }
2165 EXPORT_SYMBOL_GPL(mei_cl_all_disconnect);
2166
mei_cl_dma_map_find(struct mei_device * dev,u8 buffer_id)2167 static struct mei_cl *mei_cl_dma_map_find(struct mei_device *dev, u8 buffer_id)
2168 {
2169 struct mei_cl *cl;
2170
2171 list_for_each_entry(cl, &dev->file_list, link)
2172 if (cl->dma.buffer_id == buffer_id)
2173 return cl;
2174 return NULL;
2175 }
2176
2177 /**
2178 * mei_cl_irq_dma_map - send client dma map request in irq_thread context
2179 *
2180 * @cl: client
2181 * @cb: callback block.
2182 * @cmpl_list: complete list.
2183 *
2184 * Return: 0 on such and error otherwise.
2185 */
mei_cl_irq_dma_map(struct mei_cl * cl,struct mei_cl_cb * cb,struct list_head * cmpl_list)2186 int mei_cl_irq_dma_map(struct mei_cl *cl, struct mei_cl_cb *cb,
2187 struct list_head *cmpl_list)
2188 {
2189 struct mei_device *dev = cl->dev;
2190 u32 msg_slots;
2191 int slots;
2192 int ret;
2193
2194 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_map_request));
2195 slots = mei_hbuf_empty_slots(dev);
2196 if (slots < 0)
2197 return -EOVERFLOW;
2198
2199 if ((u32)slots < msg_slots)
2200 return -EMSGSIZE;
2201
2202 ret = mei_hbm_cl_dma_map_req(dev, cl);
2203 if (ret) {
2204 cl->status = ret;
2205 list_move_tail(&cb->list, cmpl_list);
2206 return ret;
2207 }
2208
2209 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2210 return 0;
2211 }
2212
2213 /**
2214 * mei_cl_irq_dma_unmap - send client dma unmap request in irq_thread context
2215 *
2216 * @cl: client
2217 * @cb: callback block.
2218 * @cmpl_list: complete list.
2219 *
2220 * Return: 0 on such and error otherwise.
2221 */
mei_cl_irq_dma_unmap(struct mei_cl * cl,struct mei_cl_cb * cb,struct list_head * cmpl_list)2222 int mei_cl_irq_dma_unmap(struct mei_cl *cl, struct mei_cl_cb *cb,
2223 struct list_head *cmpl_list)
2224 {
2225 struct mei_device *dev = cl->dev;
2226 u32 msg_slots;
2227 int slots;
2228 int ret;
2229
2230 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_unmap_request));
2231 slots = mei_hbuf_empty_slots(dev);
2232 if (slots < 0)
2233 return -EOVERFLOW;
2234
2235 if ((u32)slots < msg_slots)
2236 return -EMSGSIZE;
2237
2238 ret = mei_hbm_cl_dma_unmap_req(dev, cl);
2239 if (ret) {
2240 cl->status = ret;
2241 list_move_tail(&cb->list, cmpl_list);
2242 return ret;
2243 }
2244
2245 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2246 return 0;
2247 }
2248
mei_cl_dma_alloc(struct mei_cl * cl,u8 buf_id,size_t size)2249 static int mei_cl_dma_alloc(struct mei_cl *cl, u8 buf_id, size_t size)
2250 {
2251 cl->dma.vaddr = dmam_alloc_coherent(&cl->dev->dev, size,
2252 &cl->dma.daddr, GFP_KERNEL);
2253 if (!cl->dma.vaddr)
2254 return -ENOMEM;
2255
2256 cl->dma.buffer_id = buf_id;
2257 cl->dma.size = size;
2258
2259 return 0;
2260 }
2261
mei_cl_dma_free(struct mei_cl * cl)2262 static void mei_cl_dma_free(struct mei_cl *cl)
2263 {
2264 cl->dma.buffer_id = 0;
2265 dmam_free_coherent(&cl->dev->dev,
2266 cl->dma.size, cl->dma.vaddr, cl->dma.daddr);
2267 cl->dma.size = 0;
2268 cl->dma.vaddr = NULL;
2269 cl->dma.daddr = 0;
2270 }
2271
2272 /**
2273 * mei_cl_dma_alloc_and_map - send client dma map request
2274 *
2275 * @cl: host client
2276 * @fp: pointer to file structure
2277 * @buffer_id: id of the mapped buffer
2278 * @size: size of the buffer
2279 *
2280 * Locking: called under "dev->device_lock" lock
2281 *
2282 * Return:
2283 * * -ENODEV
2284 * * -EINVAL
2285 * * -EOPNOTSUPP
2286 * * -EPROTO
2287 * * -ENOMEM;
2288 */
mei_cl_dma_alloc_and_map(struct mei_cl * cl,const struct file * fp,u8 buffer_id,size_t size)2289 int mei_cl_dma_alloc_and_map(struct mei_cl *cl, const struct file *fp,
2290 u8 buffer_id, size_t size)
2291 {
2292 struct mei_device *dev;
2293 struct mei_cl_cb *cb;
2294 int rets;
2295
2296 if (WARN_ON(!cl || !cl->dev))
2297 return -ENODEV;
2298
2299 dev = cl->dev;
2300
2301 if (!dev->hbm_f_cd_supported) {
2302 cl_dbg(dev, cl, "client dma is not supported\n");
2303 return -EOPNOTSUPP;
2304 }
2305
2306 if (buffer_id == 0)
2307 return -EINVAL;
2308
2309 if (mei_cl_is_connected(cl))
2310 return -EPROTO;
2311
2312 if (cl->dma_mapped)
2313 return -EPROTO;
2314
2315 if (mei_cl_dma_map_find(dev, buffer_id)) {
2316 cl_dbg(dev, cl, "client dma with id %d is already allocated\n",
2317 cl->dma.buffer_id);
2318 return -EPROTO;
2319 }
2320
2321 rets = pm_runtime_get(dev->parent);
2322 if (rets < 0 && rets != -EINPROGRESS) {
2323 pm_runtime_put_noidle(dev->parent);
2324 cl_err(dev, cl, "rpm: get failed %d\n", rets);
2325 return rets;
2326 }
2327
2328 rets = mei_cl_dma_alloc(cl, buffer_id, size);
2329 if (rets) {
2330 pm_runtime_put_noidle(dev->parent);
2331 return rets;
2332 }
2333
2334 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_MAP, fp);
2335 if (!cb) {
2336 rets = -ENOMEM;
2337 goto out;
2338 }
2339
2340 if (mei_hbuf_acquire(dev)) {
2341 if (mei_hbm_cl_dma_map_req(dev, cl)) {
2342 rets = -ENODEV;
2343 goto out;
2344 }
2345 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2346 }
2347
2348 cl->status = 0;
2349
2350 mutex_unlock(&dev->device_lock);
2351 wait_event_timeout(cl->wait,
2352 cl->dma_mapped || cl->status,
2353 dev->timeouts.cl_connect);
2354 mutex_lock(&dev->device_lock);
2355
2356 if (!cl->dma_mapped && !cl->status)
2357 cl->status = -EFAULT;
2358
2359 rets = cl->status;
2360
2361 out:
2362 if (rets)
2363 mei_cl_dma_free(cl);
2364
2365 cl_dbg(dev, cl, "rpm: autosuspend\n");
2366 pm_runtime_put_autosuspend(dev->parent);
2367
2368 mei_io_cb_free(cb);
2369 return rets;
2370 }
2371
2372 /**
2373 * mei_cl_dma_unmap - send client dma unmap request
2374 *
2375 * @cl: host client
2376 * @fp: pointer to file structure
2377 *
2378 * Locking: called under "dev->device_lock" lock
2379 *
2380 * Return: 0 on such and error otherwise.
2381 */
mei_cl_dma_unmap(struct mei_cl * cl,const struct file * fp)2382 int mei_cl_dma_unmap(struct mei_cl *cl, const struct file *fp)
2383 {
2384 struct mei_device *dev;
2385 struct mei_cl_cb *cb;
2386 int rets;
2387
2388 if (WARN_ON(!cl || !cl->dev))
2389 return -ENODEV;
2390
2391 dev = cl->dev;
2392
2393 if (!dev->hbm_f_cd_supported) {
2394 cl_dbg(dev, cl, "client dma is not supported\n");
2395 return -EOPNOTSUPP;
2396 }
2397
2398 /* do not allow unmap for connected client */
2399 if (mei_cl_is_connected(cl))
2400 return -EPROTO;
2401
2402 if (!cl->dma_mapped)
2403 return -EPROTO;
2404
2405 rets = pm_runtime_get(dev->parent);
2406 if (rets < 0 && rets != -EINPROGRESS) {
2407 pm_runtime_put_noidle(dev->parent);
2408 cl_err(dev, cl, "rpm: get failed %d\n", rets);
2409 return rets;
2410 }
2411
2412 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_UNMAP, fp);
2413 if (!cb) {
2414 rets = -ENOMEM;
2415 goto out;
2416 }
2417
2418 if (mei_hbuf_acquire(dev)) {
2419 if (mei_hbm_cl_dma_unmap_req(dev, cl)) {
2420 rets = -ENODEV;
2421 goto out;
2422 }
2423 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2424 }
2425
2426 cl->status = 0;
2427
2428 mutex_unlock(&dev->device_lock);
2429 wait_event_timeout(cl->wait,
2430 !cl->dma_mapped || cl->status,
2431 dev->timeouts.cl_connect);
2432 mutex_lock(&dev->device_lock);
2433
2434 if (cl->dma_mapped && !cl->status)
2435 cl->status = -EFAULT;
2436
2437 rets = cl->status;
2438
2439 if (!rets)
2440 mei_cl_dma_free(cl);
2441 out:
2442 cl_dbg(dev, cl, "rpm: autosuspend\n");
2443 pm_runtime_put_autosuspend(dev->parent);
2444
2445 mei_io_cb_free(cb);
2446 return rets;
2447 }
2448