blob: e572ecd5a68d2d1d8c33d2ef74cd7bf9c5e03ead [file] [log] [blame]
Tomas Winkler9ca90502013-01-08 23:07:13 +02001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
16
Tomas Winkler9ca90502013-01-08 23:07:13 +020017#include <linux/sched.h>
18#include <linux/wait.h>
19#include <linux/delay.h>
Tomas Winkler1f180352014-09-29 16:31:46 +030020#include <linux/slab.h>
Tomas Winkler04bb1392014-03-18 22:52:04 +020021#include <linux/pm_runtime.h>
Tomas Winkler9ca90502013-01-08 23:07:13 +020022
23#include <linux/mei.h>
24
25#include "mei_dev.h"
26#include "hbm.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020027#include "client.h"
28
29/**
Tomas Winkler79563db2015-01-11 00:07:16 +020030 * 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{
Tomas Winklerb7d88512015-02-10 10:39:31 +020051 if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
52 return me_cl;
Tomas Winkler79563db2015-01-11 00:07:16 +020053
Tomas Winklerb7d88512015-02-10 10:39:31 +020054 return NULL;
Tomas Winkler79563db2015-01-11 00:07:16 +020055}
56
57/**
Tomas Winklerb7d88512015-02-10 10:39:31 +020058 * mei_me_cl_release - free me client
Tomas Winkler79563db2015-01-11 00:07:16 +020059 *
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);
Tomas Winklerb7d88512015-02-10 10:39:31 +020068
Tomas Winkler79563db2015-01-11 00:07:16 +020069 kfree(me_cl);
70}
Tomas Winklerb7d88512015-02-10 10:39:31 +020071
Tomas Winkler79563db2015-01-11 00:07:16 +020072/**
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
85/**
Tomas Winklerb7d88512015-02-10 10:39:31 +020086 * __mei_me_cl_del - delete me client form the list and decrease
87 * reference counter
88 *
89 * @dev: mei device
90 * @me_cl: me client
91 *
92 * Locking: dev->me_clients_rwsem
93 */
94static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
95{
96 if (!me_cl)
97 return;
98
99 list_del(&me_cl->list);
100 mei_me_cl_put(me_cl);
101}
102
103/**
104 * mei_me_cl_add - add me client to the list
105 *
106 * @dev: mei device
107 * @me_cl: me client
108 */
109void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
110{
111 down_write(&dev->me_clients_rwsem);
112 list_add(&me_cl->list, &dev->me_clients);
113 up_write(&dev->me_clients_rwsem);
114}
115
116/**
117 * __mei_me_cl_by_uuid - locate me client by uuid
118 * increases ref count
119 *
120 * @dev: mei device
121 * @uuid: me client uuid
122 *
123 * Return: me client or NULL if not found
124 *
125 * Locking: dev->me_clients_rwsem
126 */
127static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
128 const uuid_le *uuid)
129{
130 struct mei_me_client *me_cl;
131 const uuid_le *pn;
132
133 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
134
135 list_for_each_entry(me_cl, &dev->me_clients, list) {
136 pn = &me_cl->props.protocol_name;
137 if (uuid_le_cmp(*uuid, *pn) == 0)
138 return mei_me_cl_get(me_cl);
139 }
140
141 return NULL;
142}
143
144/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300145 * mei_me_cl_by_uuid - locate me client by uuid
Tomas Winkler79563db2015-01-11 00:07:16 +0200146 * increases ref count
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200147 *
148 * @dev: mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300149 * @uuid: me client uuid
Alexander Usyskina27a76d2014-02-17 15:13:22 +0200150 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300151 * Return: me client or NULL if not found
Tomas Winklerb7d88512015-02-10 10:39:31 +0200152 *
153 * Locking: dev->me_clients_rwsem
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200154 */
Tomas Winklerb7d88512015-02-10 10:39:31 +0200155struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
Tomas Winklerd3208322014-08-24 12:08:55 +0300156 const uuid_le *uuid)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200157{
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300158 struct mei_me_client *me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200159
Tomas Winklerb7d88512015-02-10 10:39:31 +0200160 down_read(&dev->me_clients_rwsem);
161 me_cl = __mei_me_cl_by_uuid(dev, uuid);
162 up_read(&dev->me_clients_rwsem);
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200163
Tomas Winklerb7d88512015-02-10 10:39:31 +0200164 return me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200165}
166
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200167/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300168 * mei_me_cl_by_id - locate me client by client id
Tomas Winkler79563db2015-01-11 00:07:16 +0200169 * increases ref count
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200170 *
171 * @dev: the device structure
172 * @client_id: me client id
173 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300174 * Return: me client or NULL if not found
Tomas Winklerb7d88512015-02-10 10:39:31 +0200175 *
176 * Locking: dev->me_clients_rwsem
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200177 */
Tomas Winklerd3208322014-08-24 12:08:55 +0300178struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200179{
Alexander Usyskina27a76d2014-02-17 15:13:22 +0200180
Tomas Winklerb7d88512015-02-10 10:39:31 +0200181 struct mei_me_client *__me_cl, *me_cl = NULL;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200182
Tomas Winklerb7d88512015-02-10 10:39:31 +0200183 down_read(&dev->me_clients_rwsem);
184 list_for_each_entry(__me_cl, &dev->me_clients, list) {
185 if (__me_cl->client_id == client_id) {
186 me_cl = mei_me_cl_get(__me_cl);
187 break;
188 }
189 }
190 up_read(&dev->me_clients_rwsem);
191
192 return me_cl;
193}
194
195/**
196 * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
197 * increases ref count
198 *
199 * @dev: the device structure
200 * @uuid: me client uuid
201 * @client_id: me client id
202 *
203 * Return: me client or null if not found
204 *
205 * Locking: dev->me_clients_rwsem
206 */
207static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
208 const uuid_le *uuid, u8 client_id)
209{
210 struct mei_me_client *me_cl;
211 const uuid_le *pn;
212
213 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
214
215 list_for_each_entry(me_cl, &dev->me_clients, list) {
216 pn = &me_cl->props.protocol_name;
217 if (uuid_le_cmp(*uuid, *pn) == 0 &&
218 me_cl->client_id == client_id)
Tomas Winkler79563db2015-01-11 00:07:16 +0200219 return mei_me_cl_get(me_cl);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200220 }
Tomas Winkler79563db2015-01-11 00:07:16 +0200221
Tomas Winklerd3208322014-08-24 12:08:55 +0300222 return NULL;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200223}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200224
Tomas Winklerb7d88512015-02-10 10:39:31 +0200225
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300226/**
227 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
Tomas Winkler79563db2015-01-11 00:07:16 +0200228 * increases ref count
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300229 *
230 * @dev: the device structure
231 * @uuid: me client uuid
232 * @client_id: me client id
233 *
Tomas Winklerb7d88512015-02-10 10:39:31 +0200234 * Return: me client or null if not found
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300235 */
Tomas Winklerd880f322014-08-21 14:29:15 +0300236struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
237 const uuid_le *uuid, u8 client_id)
238{
239 struct mei_me_client *me_cl;
240
Tomas Winklerb7d88512015-02-10 10:39:31 +0200241 down_read(&dev->me_clients_rwsem);
242 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
243 up_read(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200244
Tomas Winklerb7d88512015-02-10 10:39:31 +0200245 return me_cl;
Tomas Winklerd880f322014-08-21 14:29:15 +0300246}
247
Tomas Winkler25ca6472014-08-21 14:29:14 +0300248/**
Tomas Winkler79563db2015-01-11 00:07:16 +0200249 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
Tomas Winkler25ca6472014-08-21 14:29:14 +0300250 *
251 * @dev: the device structure
252 * @uuid: me client uuid
Tomas Winkler79563db2015-01-11 00:07:16 +0200253 *
254 * Locking: called under "dev->device_lock" lock
Tomas Winkler25ca6472014-08-21 14:29:14 +0300255 */
Tomas Winkler79563db2015-01-11 00:07:16 +0200256void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
Tomas Winkler25ca6472014-08-21 14:29:14 +0300257{
Tomas Winklerb7d88512015-02-10 10:39:31 +0200258 struct mei_me_client *me_cl;
Tomas Winkler25ca6472014-08-21 14:29:14 +0300259
Tomas Winkler79563db2015-01-11 00:07:16 +0200260 dev_dbg(dev->dev, "remove %pUl\n", uuid);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200261
262 down_write(&dev->me_clients_rwsem);
263 me_cl = __mei_me_cl_by_uuid(dev, uuid);
264 __mei_me_cl_del(dev, me_cl);
265 up_write(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200266}
267
268/**
269 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
270 *
271 * @dev: the device structure
272 * @uuid: me client uuid
273 * @id: me client id
274 *
275 * Locking: called under "dev->device_lock" lock
276 */
277void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
278{
Tomas Winklerb7d88512015-02-10 10:39:31 +0200279 struct mei_me_client *me_cl;
Tomas Winkler79563db2015-01-11 00:07:16 +0200280
281 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200282
283 down_write(&dev->me_clients_rwsem);
284 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
285 __mei_me_cl_del(dev, me_cl);
286 up_write(&dev->me_clients_rwsem);
Tomas Winkler25ca6472014-08-21 14:29:14 +0300287}
288
Tomas Winkler79563db2015-01-11 00:07:16 +0200289/**
290 * mei_me_cl_rm_all - remove all me clients
291 *
292 * @dev: the device structure
293 *
294 * Locking: called under "dev->device_lock" lock
295 */
296void mei_me_cl_rm_all(struct mei_device *dev)
297{
298 struct mei_me_client *me_cl, *next;
299
Tomas Winklerb7d88512015-02-10 10:39:31 +0200300 down_write(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200301 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
Tomas Winklerb7d88512015-02-10 10:39:31 +0200302 __mei_me_cl_del(dev, me_cl);
303 up_write(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200304}
305
Tomas Winkler9ca90502013-01-08 23:07:13 +0200306/**
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200307 * mei_cl_cmp_id - tells if the clients are the same
308 *
309 * @cl1: host client 1
310 * @cl2: host client 2
311 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300312 * Return: true - if the clients has same host and me ids
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200313 * false - otherwise
314 */
315static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
316 const struct mei_cl *cl2)
317{
318 return cl1 && cl2 &&
319 (cl1->host_client_id == cl2->host_client_id) &&
320 (cl1->me_client_id == cl2->me_client_id);
321}
322
323/**
Tomas Winkler9ca90502013-01-08 23:07:13 +0200324 * mei_io_cb_free - free mei_cb_private related memory
325 *
326 * @cb: mei callback struct
327 */
328void mei_io_cb_free(struct mei_cl_cb *cb)
329{
330 if (cb == NULL)
331 return;
332
Tomas Winkler928fa662015-02-10 10:39:45 +0200333 list_del(&cb->list);
Tomas Winkler5db75142015-02-10 10:39:42 +0200334 kfree(cb->buf.data);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200335 kfree(cb);
336}
337
338/**
339 * mei_io_cb_init - allocate and initialize io callback
340 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300341 * @cl: mei client
Tomas Winklerbca67d62015-02-10 10:39:43 +0200342 * @type: operation type
Masanari Iida393b1482013-04-05 01:05:05 +0900343 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200344 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300345 * Return: mei_cl_cb pointer or NULL;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200346 */
Tomas Winklerbca67d62015-02-10 10:39:43 +0200347struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, enum mei_cb_file_ops type,
348 struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200349{
350 struct mei_cl_cb *cb;
351
352 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
353 if (!cb)
354 return NULL;
355
Tomas Winkler928fa662015-02-10 10:39:45 +0200356 INIT_LIST_HEAD(&cb->list);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200357 cb->file_object = fp;
358 cb->cl = cl;
359 cb->buf_idx = 0;
Tomas Winklerbca67d62015-02-10 10:39:43 +0200360 cb->fop_type = type;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200361 return cb;
362}
363
364/**
Tomas Winkler928fa662015-02-10 10:39:45 +0200365 * __mei_io_list_flush - removes and frees cbs belonging to cl.
366 *
367 * @list: an instance of our list structure
368 * @cl: host client, can be NULL for flushing the whole list
369 * @free: whether to free the cbs
370 */
371static void __mei_io_list_flush(struct mei_cl_cb *list,
372 struct mei_cl *cl, bool free)
373{
374 struct mei_cl_cb *cb, *next;
375
376 /* enable removing everything if no cl is specified */
377 list_for_each_entry_safe(cb, next, &list->list, list) {
378 if (!cl || mei_cl_cmp_id(cl, cb->cl)) {
379 list_del_init(&cb->list);
380 if (free)
381 mei_io_cb_free(cb);
382 }
383 }
384}
385
386/**
387 * mei_io_list_flush - removes list entry belonging to cl.
388 *
389 * @list: An instance of our list structure
390 * @cl: host client
391 */
392void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
393{
394 __mei_io_list_flush(list, cl, false);
395}
396
397/**
398 * mei_io_list_free - removes cb belonging to cl and free them
399 *
400 * @list: An instance of our list structure
401 * @cl: host client
402 */
403static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
404{
405 __mei_io_list_flush(list, cl, true);
406}
407
408/**
Tomas Winkler5db75142015-02-10 10:39:42 +0200409 * mei_io_cb_alloc_buf - allocate callback buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200410 *
Masanari Iida393b1482013-04-05 01:05:05 +0900411 * @cb: io callback structure
412 * @length: size of the buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200413 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300414 * Return: 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200415 * -EINVAL if cb is NULL
416 * -ENOMEM if allocation failed
417 */
Tomas Winkler5db75142015-02-10 10:39:42 +0200418int mei_io_cb_alloc_buf(struct mei_cl_cb *cb, size_t length)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200419{
420 if (!cb)
421 return -EINVAL;
422
423 if (length == 0)
424 return 0;
425
Tomas Winkler5db75142015-02-10 10:39:42 +0200426 cb->buf.data = kmalloc(length, GFP_KERNEL);
427 if (!cb->buf.data)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200428 return -ENOMEM;
Tomas Winkler5db75142015-02-10 10:39:42 +0200429 cb->buf.size = length;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200430 return 0;
431}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200432
433/**
Tomas Winklerbca67d62015-02-10 10:39:43 +0200434 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
435 *
436 * @cl: host client
437 * @length: size of the buffer
438 * @type: operation type
439 * @fp: associated file pointer (might be NULL)
440 *
441 * Return: cb on success and NULL on failure
442 */
443struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
444 enum mei_cb_file_ops type, struct file *fp)
445{
446 struct mei_cl_cb *cb;
447
448 cb = mei_io_cb_init(cl, type, fp);
449 if (!cb)
450 return NULL;
451
452 if (mei_io_cb_alloc_buf(cb, length)) {
453 mei_io_cb_free(cb);
454 return NULL;
455 }
456
457 return cb;
458}
459
460/**
Tomas Winklera9bed612015-02-10 10:39:46 +0200461 * mei_cl_read_cb - find this cl's callback in the read list
462 * for a specific file
463 *
464 * @cl: host client
465 * @fp: file pointer (matching cb file object), may be NULL
466 *
467 * Return: cb on success, NULL if cb is not found
468 */
469struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
470{
471 struct mei_cl_cb *cb;
472
473 list_for_each_entry(cb, &cl->rd_completed, list)
474 if (!fp || fp == cb->file_object)
475 return cb;
476
477 return NULL;
478}
479
480/**
481 * mei_cl_read_cb_flush - free client's read pending and completed cbs
482 * for a specific file
483 *
484 * @cl: host client
485 * @fp: file pointer (matching cb file object), may be NULL
486 */
487void mei_cl_read_cb_flush(const struct mei_cl *cl, const struct file *fp)
488{
489 struct mei_cl_cb *cb, *next;
490
491 list_for_each_entry_safe(cb, next, &cl->rd_completed, list)
492 if (!fp || fp == cb->file_object)
493 mei_io_cb_free(cb);
494
495
496 list_for_each_entry_safe(cb, next, &cl->rd_pending, list)
497 if (!fp || fp == cb->file_object)
498 mei_io_cb_free(cb);
499}
500
501/**
Tomas Winkler9ca90502013-01-08 23:07:13 +0200502 * mei_cl_flush_queues - flushes queue lists belonging to cl.
503 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200504 * @cl: host client
Tomas Winklera9bed612015-02-10 10:39:46 +0200505 * @fp: file pointer (matching cb file object), may be NULL
Alexander Usyskince231392014-09-29 16:31:50 +0300506 *
507 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200508 */
Tomas Winklera9bed612015-02-10 10:39:46 +0200509int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200510{
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300511 struct mei_device *dev;
512
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200513 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200514 return -EINVAL;
515
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300516 dev = cl->dev;
517
518 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200519 mei_io_list_free(&cl->dev->write_list, cl);
520 mei_io_list_free(&cl->dev->write_waiting_list, cl);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200521 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
522 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
523 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
524 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
Tomas Winklera9bed612015-02-10 10:39:46 +0200525
526 mei_cl_read_cb_flush(cl, fp);
527
Tomas Winkler9ca90502013-01-08 23:07:13 +0200528 return 0;
529}
530
Tomas Winkler9ca90502013-01-08 23:07:13 +0200531
532/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200533 * mei_cl_init - initializes cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200534 *
535 * @cl: host client to be initialized
536 * @dev: mei device
537 */
538void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
539{
540 memset(cl, 0, sizeof(struct mei_cl));
541 init_waitqueue_head(&cl->wait);
542 init_waitqueue_head(&cl->rx_wait);
543 init_waitqueue_head(&cl->tx_wait);
Tomas Winklera9bed612015-02-10 10:39:46 +0200544 INIT_LIST_HEAD(&cl->rd_completed);
545 INIT_LIST_HEAD(&cl->rd_pending);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200546 INIT_LIST_HEAD(&cl->link);
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200547 INIT_LIST_HEAD(&cl->device_link);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200548 cl->writing_state = MEI_IDLE;
Tomas Winkler3c666182015-05-04 09:43:52 +0300549 cl->state = MEI_FILE_INITIALIZING;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200550 cl->dev = dev;
551}
552
553/**
554 * mei_cl_allocate - allocates cl structure and sets it up.
555 *
556 * @dev: mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300557 * Return: The allocated file or NULL on failure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200558 */
559struct mei_cl *mei_cl_allocate(struct mei_device *dev)
560{
561 struct mei_cl *cl;
562
563 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
564 if (!cl)
565 return NULL;
566
567 mei_cl_init(cl, dev);
568
569 return cl;
570}
571
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200572/**
Alexander Usyskin3908be62015-02-10 10:39:35 +0200573 * mei_cl_link - allocate host id in the host map
Tomas Winkler9ca90502013-01-08 23:07:13 +0200574 *
Alexander Usyskin3908be62015-02-10 10:39:35 +0200575 * @cl: host client
Tomas Winkler03b8d342015-02-10 10:39:44 +0200576 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
Masanari Iida393b1482013-04-05 01:05:05 +0900577 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300578 * Return: 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200579 * -EINVAL on incorrect values
Tomas Winkler03b8d342015-02-10 10:39:44 +0200580 * -EMFILE if open count exceeded.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200581 */
Tomas Winkler781d0d82013-01-08 23:07:22 +0200582int mei_cl_link(struct mei_cl *cl, int id)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200583{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200584 struct mei_device *dev;
Tomas Winkler22f96a02013-09-16 23:44:47 +0300585 long open_handle_count;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200586
Tomas Winkler781d0d82013-01-08 23:07:22 +0200587 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200588 return -EINVAL;
589
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200590 dev = cl->dev;
591
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200592 /* If Id is not assigned get one*/
Tomas Winkler781d0d82013-01-08 23:07:22 +0200593 if (id == MEI_HOST_CLIENT_ID_ANY)
594 id = find_first_zero_bit(dev->host_clients_map,
595 MEI_CLIENTS_MAX);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200596
Tomas Winkler781d0d82013-01-08 23:07:22 +0200597 if (id >= MEI_CLIENTS_MAX) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300598 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
Tomas Winklere036cc52013-09-16 23:44:46 +0300599 return -EMFILE;
600 }
601
Tomas Winkler22f96a02013-09-16 23:44:47 +0300602 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
603 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300604 dev_err(dev->dev, "open_handle_count exceeded %d",
Tomas Winklere036cc52013-09-16 23:44:46 +0300605 MEI_MAX_OPEN_HANDLE_COUNT);
606 return -EMFILE;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200607 }
608
Tomas Winkler781d0d82013-01-08 23:07:22 +0200609 dev->open_handle_count++;
610
611 cl->host_client_id = id;
612 list_add_tail(&cl->link, &dev->file_list);
613
614 set_bit(id, dev->host_clients_map);
615
616 cl->state = MEI_FILE_INITIALIZING;
617
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300618 cl_dbg(dev, cl, "link cl\n");
Tomas Winkler781d0d82013-01-08 23:07:22 +0200619 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200620}
Tomas Winkler781d0d82013-01-08 23:07:22 +0200621
Tomas Winkler9ca90502013-01-08 23:07:13 +0200622/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200623 * mei_cl_unlink - remove me_cl from the list
Tomas Winkler9ca90502013-01-08 23:07:13 +0200624 *
Masanari Iida393b1482013-04-05 01:05:05 +0900625 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +0300626 *
627 * Return: always 0
Tomas Winkler9ca90502013-01-08 23:07:13 +0200628 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200629int mei_cl_unlink(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200630{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200631 struct mei_device *dev;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200632
Tomas Winkler781d0d82013-01-08 23:07:22 +0200633 /* don't shout on error exit path */
634 if (!cl)
635 return 0;
636
Tomas Winkler8e9a4a92013-01-10 17:32:14 +0200637 /* wd and amthif might not be initialized */
638 if (!cl->dev)
639 return 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200640
641 dev = cl->dev;
642
Tomas Winklera14c44d2013-09-16 23:44:45 +0300643 cl_dbg(dev, cl, "unlink client");
644
Tomas Winkler22f96a02013-09-16 23:44:47 +0300645 if (dev->open_handle_count > 0)
646 dev->open_handle_count--;
647
648 /* never clear the 0 bit */
649 if (cl->host_client_id)
650 clear_bit(cl->host_client_id, dev->host_clients_map);
651
652 list_del_init(&cl->link);
653
654 cl->state = MEI_FILE_INITIALIZING;
655
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200656 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200657}
658
659
660void mei_host_client_init(struct work_struct *work)
661{
Tomas Winklerb7d88512015-02-10 10:39:31 +0200662 struct mei_device *dev =
663 container_of(work, struct mei_device, init_work);
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300664 struct mei_me_client *me_cl;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200665
666 mutex_lock(&dev->device_lock);
667
Tomas Winkler9ca90502013-01-08 23:07:13 +0200668
Tomas Winklerb7d88512015-02-10 10:39:31 +0200669 me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
670 if (me_cl)
671 mei_amthif_host_init(dev);
Tomas Winklerb43baf62015-03-04 18:41:34 +0200672 mei_me_cl_put(me_cl);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200673
Tomas Winklerb7d88512015-02-10 10:39:31 +0200674 me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
675 if (me_cl)
676 mei_wd_host_init(dev);
Tomas Winklerb43baf62015-03-04 18:41:34 +0200677 mei_me_cl_put(me_cl);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200678
679 me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid);
680 if (me_cl)
681 mei_nfc_host_init(dev);
Tomas Winklerb43baf62015-03-04 18:41:34 +0200682 mei_me_cl_put(me_cl);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200683
Tomas Winkler9ca90502013-01-08 23:07:13 +0200684
685 dev->dev_state = MEI_DEV_ENABLED;
Tomas Winkler6adb8ef2014-01-12 00:36:10 +0200686 dev->reset_count = 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200687 mutex_unlock(&dev->device_lock);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200688
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300689 pm_runtime_mark_last_busy(dev->dev);
690 dev_dbg(dev->dev, "rpm: autosuspend\n");
691 pm_runtime_autosuspend(dev->dev);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200692}
693
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200694/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300695 * mei_hbuf_acquire - try to acquire host buffer
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200696 *
697 * @dev: the device structure
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300698 * Return: true if host buffer was acquired
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200699 */
700bool mei_hbuf_acquire(struct mei_device *dev)
701{
Tomas Winkler04bb1392014-03-18 22:52:04 +0200702 if (mei_pg_state(dev) == MEI_PG_ON ||
703 dev->pg_event == MEI_PG_EVENT_WAIT) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300704 dev_dbg(dev->dev, "device is in pg\n");
Tomas Winkler04bb1392014-03-18 22:52:04 +0200705 return false;
706 }
707
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200708 if (!dev->hbuf_is_ready) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300709 dev_dbg(dev->dev, "hbuf is not ready\n");
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200710 return false;
711 }
712
713 dev->hbuf_is_ready = false;
714
715 return true;
716}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200717
718/**
Tomas Winkler3c666182015-05-04 09:43:52 +0300719 * mei_cl_set_disconnected - set disconnected state and clear
720 * associated states and resources
721 *
722 * @cl: host client
723 */
724void mei_cl_set_disconnected(struct mei_cl *cl)
725{
726 struct mei_device *dev = cl->dev;
727
728 if (cl->state == MEI_FILE_DISCONNECTED ||
729 cl->state == MEI_FILE_INITIALIZING)
730 return;
731
732 cl->state = MEI_FILE_DISCONNECTED;
733 mei_io_list_flush(&dev->ctrl_rd_list, cl);
734 mei_io_list_flush(&dev->ctrl_wr_list, cl);
735 cl->mei_flow_ctrl_creds = 0;
736 cl->timer_count = 0;
737}
738
739/*
740 * mei_cl_send_disconnect - send disconnect request
741 *
742 * @cl: host client
743 * @cb: callback block
744 *
745 * Return: 0, OK; otherwise, error.
746 */
747static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
748{
749 struct mei_device *dev;
750 int ret;
751
752 dev = cl->dev;
753
754 ret = mei_hbm_cl_disconnect_req(dev, cl);
755 cl->status = ret;
756 if (ret) {
757 cl->state = MEI_FILE_DISCONNECT_REPLY;
758 return ret;
759 }
760
761 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
762 cl->timer_count = MEI_CONNECT_TIMEOUT;
763
764 return 0;
765}
766
767/**
768 * mei_cl_irq_disconnect - processes close related operation from
769 * interrupt thread context - send disconnect request
770 *
771 * @cl: client
772 * @cb: callback block.
773 * @cmpl_list: complete list.
774 *
775 * Return: 0, OK; otherwise, error.
776 */
777int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
778 struct mei_cl_cb *cmpl_list)
779{
780 struct mei_device *dev = cl->dev;
781 u32 msg_slots;
782 int slots;
783 int ret;
784
785 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
786 slots = mei_hbuf_empty_slots(dev);
787
788 if (slots < msg_slots)
789 return -EMSGSIZE;
790
791 ret = mei_cl_send_disconnect(cl, cb);
792 if (ret)
793 list_move_tail(&cb->list, &cmpl_list->list);
794
795 return ret;
796}
797
798
799
800/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200801 * mei_cl_disconnect - disconnect host client from the me one
Tomas Winkler9ca90502013-01-08 23:07:13 +0200802 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200803 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200804 *
805 * Locking: called under "dev->device_lock" lock
806 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300807 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200808 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200809int mei_cl_disconnect(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200810{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200811 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200812 struct mei_cl_cb *cb;
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300813 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200814
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200815 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200816 return -ENODEV;
817
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200818 dev = cl->dev;
819
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300820 cl_dbg(dev, cl, "disconnecting");
821
Tomas Winkler3c666182015-05-04 09:43:52 +0300822 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200823 return 0;
824
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300825 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200826 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300827 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200828 cl_err(dev, cl, "rpm: get failed %d\n", rets);
829 return rets;
830 }
831
Tomas Winkler3c666182015-05-04 09:43:52 +0300832 cl->state = MEI_FILE_DISCONNECTING;
833
Tomas Winklerbca67d62015-02-10 10:39:43 +0200834 cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL);
835 rets = cb ? 0 : -ENOMEM;
836 if (rets)
Tomas Winkler3c666182015-05-04 09:43:52 +0300837 goto out;
838
839 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
840 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300841
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200842 if (mei_hbuf_acquire(dev)) {
Tomas Winkler3c666182015-05-04 09:43:52 +0300843 rets = mei_cl_send_disconnect(cl, cb);
844 if (rets) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300845 cl_err(dev, cl, "failed to disconnect.\n");
Tomas Winkler3c666182015-05-04 09:43:52 +0300846 goto out;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200847 }
Tomas Winkler9ca90502013-01-08 23:07:13 +0200848 }
Tomas Winkler3c666182015-05-04 09:43:52 +0300849
Tomas Winkler9ca90502013-01-08 23:07:13 +0200850 mutex_unlock(&dev->device_lock);
Tomas Winkler3c666182015-05-04 09:43:52 +0300851 wait_event_timeout(cl->wait, cl->state == MEI_FILE_DISCONNECT_REPLY,
852 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9ca90502013-01-08 23:07:13 +0200853 mutex_lock(&dev->device_lock);
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300854
Tomas Winkler3c666182015-05-04 09:43:52 +0300855 rets = cl->status;
856 if (cl->state != MEI_FILE_DISCONNECT_REPLY) {
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300857 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
858 rets = -ETIME;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200859 }
860
Tomas Winkler3c666182015-05-04 09:43:52 +0300861out:
862 /* we disconnect also on error */
863 mei_cl_set_disconnected(cl);
864 if (!rets)
865 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
866
Tomas Winkler04bb1392014-03-18 22:52:04 +0200867 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300868 pm_runtime_mark_last_busy(dev->dev);
869 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200870
Tomas Winkler9ca90502013-01-08 23:07:13 +0200871 mei_io_cb_free(cb);
872 return rets;
873}
874
875
876/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200877 * mei_cl_is_other_connecting - checks if other
878 * client with the same me client id is connecting
Tomas Winkler9ca90502013-01-08 23:07:13 +0200879 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200880 * @cl: private data of the file object
881 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300882 * Return: true if other client is connected, false - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200883 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200884bool mei_cl_is_other_connecting(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200885{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200886 struct mei_device *dev;
Tomas Winkler31f88f52014-02-17 15:13:25 +0200887 struct mei_cl *ocl; /* the other client */
Tomas Winkler9ca90502013-01-08 23:07:13 +0200888
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200889 if (WARN_ON(!cl || !cl->dev))
890 return false;
891
892 dev = cl->dev;
893
Tomas Winkler31f88f52014-02-17 15:13:25 +0200894 list_for_each_entry(ocl, &dev->file_list, link) {
895 if (ocl->state == MEI_FILE_CONNECTING &&
896 ocl != cl &&
897 cl->me_client_id == ocl->me_client_id)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200898 return true;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200899
900 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200901
902 return false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200903}
904
905/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200906 * mei_cl_connect - connect host client to the me one
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200907 *
908 * @cl: host client
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300909 * @file: pointer to file structure
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200910 *
911 * Locking: called under "dev->device_lock" lock
912 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300913 * Return: 0 on success, <0 on failure.
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200914 */
915int mei_cl_connect(struct mei_cl *cl, struct file *file)
916{
917 struct mei_device *dev;
918 struct mei_cl_cb *cb;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200919 int rets;
920
921 if (WARN_ON(!cl || !cl->dev))
922 return -ENODEV;
923
924 dev = cl->dev;
925
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300926 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200927 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300928 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200929 cl_err(dev, cl, "rpm: get failed %d\n", rets);
930 return rets;
931 }
932
Tomas Winklerbca67d62015-02-10 10:39:43 +0200933 cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file);
934 rets = cb ? 0 : -ENOMEM;
935 if (rets)
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200936 goto out;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200937
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200938 /* run hbuf acquire last so we don't have to undo */
939 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
Alexander Usyskine4d82702014-04-27 15:42:21 +0300940 cl->state = MEI_FILE_CONNECTING;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200941 if (mei_hbm_cl_connect_req(dev, cl)) {
942 rets = -ENODEV;
943 goto out;
944 }
945 cl->timer_count = MEI_CONNECT_TIMEOUT;
946 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
947 } else {
Alexander Usyskin73ab4232014-08-12 18:07:56 +0300948 cl->state = MEI_FILE_INITIALIZING;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200949 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
950 }
951
952 mutex_unlock(&dev->device_lock);
Tomas Winkler12f45ed2014-08-21 14:29:18 +0300953 wait_event_timeout(cl->wait,
Alexander Usyskin285e2992014-02-17 15:13:20 +0200954 (cl->state == MEI_FILE_CONNECTED ||
Tomas Winkler3c666182015-05-04 09:43:52 +0300955 cl->state == MEI_FILE_DISCONNECT_REPLY),
Alexander Usyskin285e2992014-02-17 15:13:20 +0200956 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200957 mutex_lock(&dev->device_lock);
958
Tomas Winklerf3de9b62015-03-27 00:27:58 +0200959 if (!mei_cl_is_connected(cl)) {
Alexander Usyskin285e2992014-02-17 15:13:20 +0200960 /* something went really wrong */
961 if (!cl->status)
962 cl->status = -EFAULT;
Tomas Winkler3c666182015-05-04 09:43:52 +0300963 mei_cl_set_disconnected(cl);
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200964 }
965
966 rets = cl->status;
967
968out:
Tomas Winkler04bb1392014-03-18 22:52:04 +0200969 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300970 pm_runtime_mark_last_busy(dev->dev);
971 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200972
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200973 mei_io_cb_free(cb);
974 return rets;
975}
976
977/**
Tomas Winkler03b8d342015-02-10 10:39:44 +0200978 * mei_cl_alloc_linked - allocate and link host client
979 *
980 * @dev: the device structure
981 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
982 *
983 * Return: cl on success ERR_PTR on failure
984 */
985struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id)
986{
987 struct mei_cl *cl;
988 int ret;
989
990 cl = mei_cl_allocate(dev);
991 if (!cl) {
992 ret = -ENOMEM;
993 goto err;
994 }
995
996 ret = mei_cl_link(cl, id);
997 if (ret)
998 goto err;
999
1000 return cl;
1001err:
1002 kfree(cl);
1003 return ERR_PTR(ret);
1004}
1005
1006
1007
1008/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001009 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001010 *
Tomas Winkler9ca90502013-01-08 23:07:13 +02001011 * @cl: private data of the file object
1012 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001013 * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001014 * -ENOENT if mei_cl is not present
1015 * -EINVAL if single_recv_buf == 0
1016 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001017int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001018{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001019 struct mei_device *dev;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001020 struct mei_me_client *me_cl;
Tomas Winkler79563db2015-01-11 00:07:16 +02001021 int rets = 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001022
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001023 if (WARN_ON(!cl || !cl->dev))
1024 return -EINVAL;
1025
1026 dev = cl->dev;
1027
Tomas Winkler9ca90502013-01-08 23:07:13 +02001028 if (cl->mei_flow_ctrl_creds > 0)
1029 return 1;
1030
Tomas Winkler2e5df412014-12-07 16:40:14 +02001031 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +03001032 if (!me_cl) {
Alexander Usyskin12d00662014-02-17 15:13:23 +02001033 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +03001034 return -ENOENT;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001035 }
Alexander Usyskin12d00662014-02-17 15:13:23 +02001036
Tomas Winkler79563db2015-01-11 00:07:16 +02001037 if (me_cl->mei_flow_ctrl_creds > 0) {
1038 rets = 1;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001039 if (WARN_ON(me_cl->props.single_recv_buf == 0))
Tomas Winkler79563db2015-01-11 00:07:16 +02001040 rets = -EINVAL;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001041 }
Tomas Winkler79563db2015-01-11 00:07:16 +02001042 mei_me_cl_put(me_cl);
1043 return rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001044}
1045
1046/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001047 * mei_cl_flow_ctrl_reduce - reduces flow_control.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001048 *
Tomas Winkler9ca90502013-01-08 23:07:13 +02001049 * @cl: private data of the file object
Masanari Iida393b1482013-04-05 01:05:05 +09001050 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001051 * Return:
Tomas Winkler9ca90502013-01-08 23:07:13 +02001052 * 0 on success
1053 * -ENOENT when me client is not found
1054 * -EINVAL when ctrl credits are <= 0
1055 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001056int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001057{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001058 struct mei_device *dev;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001059 struct mei_me_client *me_cl;
Tomas Winkler79563db2015-01-11 00:07:16 +02001060 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001061
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001062 if (WARN_ON(!cl || !cl->dev))
1063 return -EINVAL;
1064
1065 dev = cl->dev;
1066
Tomas Winkler2e5df412014-12-07 16:40:14 +02001067 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +03001068 if (!me_cl) {
Alexander Usyskin12d00662014-02-17 15:13:23 +02001069 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +03001070 return -ENOENT;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001071 }
Alexander Usyskin12d00662014-02-17 15:13:23 +02001072
Tomas Winklerd3208322014-08-24 12:08:55 +03001073 if (me_cl->props.single_recv_buf) {
Tomas Winkler79563db2015-01-11 00:07:16 +02001074 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) {
1075 rets = -EINVAL;
1076 goto out;
1077 }
Alexander Usyskin12d00662014-02-17 15:13:23 +02001078 me_cl->mei_flow_ctrl_creds--;
1079 } else {
Tomas Winkler79563db2015-01-11 00:07:16 +02001080 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) {
1081 rets = -EINVAL;
1082 goto out;
1083 }
Alexander Usyskin12d00662014-02-17 15:13:23 +02001084 cl->mei_flow_ctrl_creds--;
1085 }
Tomas Winkler79563db2015-01-11 00:07:16 +02001086 rets = 0;
1087out:
1088 mei_me_cl_put(me_cl);
1089 return rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001090}
1091
Tomas Winkler9ca90502013-01-08 23:07:13 +02001092/**
Masanari Iida393b1482013-04-05 01:05:05 +09001093 * mei_cl_read_start - the start read client message function.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001094 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001095 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +03001096 * @length: number of bytes to read
Tomas Winklerbca67d62015-02-10 10:39:43 +02001097 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +02001098 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001099 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001100 */
Tomas Winklerbca67d62015-02-10 10:39:43 +02001101int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001102{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001103 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001104 struct mei_cl_cb *cb;
Tomas Winklerd3208322014-08-24 12:08:55 +03001105 struct mei_me_client *me_cl;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001106 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001107
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001108 if (WARN_ON(!cl || !cl->dev))
1109 return -ENODEV;
1110
1111 dev = cl->dev;
1112
Tomas Winklerb950ac12013-07-25 20:15:53 +03001113 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +02001114 return -ENODEV;
1115
Tomas Winklera9bed612015-02-10 10:39:46 +02001116 /* HW currently supports only one pending read */
1117 if (!list_empty(&cl->rd_pending))
Tomas Winkler9ca90502013-01-08 23:07:13 +02001118 return -EBUSY;
Tomas Winklera9bed612015-02-10 10:39:46 +02001119
Tomas Winklerd880f322014-08-21 14:29:15 +03001120 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +03001121 if (!me_cl) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001122 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001123 return -ENOTTY;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001124 }
Tomas Winkler79563db2015-01-11 00:07:16 +02001125 /* always allocate at least client max message */
1126 length = max_t(size_t, length, me_cl->props.max_msg_length);
1127 mei_me_cl_put(me_cl);
Tomas Winkler9ca90502013-01-08 23:07:13 +02001128
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001129 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001130 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001131 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001132 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1133 return rets;
1134 }
1135
Tomas Winklerbca67d62015-02-10 10:39:43 +02001136 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp);
1137 rets = cb ? 0 : -ENOMEM;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001138 if (rets)
Tomas Winkler04bb1392014-03-18 22:52:04 +02001139 goto out;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001140
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001141 if (mei_hbuf_acquire(dev)) {
Alexander Usyskin86113502014-03-31 17:59:24 +03001142 rets = mei_hbm_cl_flow_control_req(dev, cl);
1143 if (rets < 0)
Tomas Winkler04bb1392014-03-18 22:52:04 +02001144 goto out;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001145
Tomas Winklera9bed612015-02-10 10:39:46 +02001146 list_add_tail(&cb->list, &cl->rd_pending);
Tomas Winkler9ca90502013-01-08 23:07:13 +02001147 } else {
1148 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1149 }
Chao Biaccb8842014-02-12 21:27:25 +02001150
Tomas Winkler04bb1392014-03-18 22:52:04 +02001151out:
1152 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001153 pm_runtime_mark_last_busy(dev->dev);
1154 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001155
1156 if (rets)
1157 mei_io_cb_free(cb);
1158
Tomas Winkler9ca90502013-01-08 23:07:13 +02001159 return rets;
1160}
1161
Tomas Winkler074b4c02013-02-06 14:06:44 +02001162/**
Tomas Winkler9d098192014-02-19 17:35:48 +02001163 * mei_cl_irq_write - write a message to device
Tomas Winkler21767542013-06-23 09:36:59 +03001164 * from the interrupt thread context
1165 *
1166 * @cl: client
1167 * @cb: callback block.
Tomas Winkler21767542013-06-23 09:36:59 +03001168 * @cmpl_list: complete list.
1169 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001170 * Return: 0, OK; otherwise error.
Tomas Winkler21767542013-06-23 09:36:59 +03001171 */
Tomas Winkler9d098192014-02-19 17:35:48 +02001172int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1173 struct mei_cl_cb *cmpl_list)
Tomas Winkler21767542013-06-23 09:36:59 +03001174{
Tomas Winkler136698e2013-09-16 23:44:44 +03001175 struct mei_device *dev;
1176 struct mei_msg_data *buf;
Tomas Winkler21767542013-06-23 09:36:59 +03001177 struct mei_msg_hdr mei_hdr;
Tomas Winkler136698e2013-09-16 23:44:44 +03001178 size_t len;
1179 u32 msg_slots;
Tomas Winkler9d098192014-02-19 17:35:48 +02001180 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001181 int rets;
Tomas Winkler21767542013-06-23 09:36:59 +03001182
Tomas Winkler136698e2013-09-16 23:44:44 +03001183 if (WARN_ON(!cl || !cl->dev))
1184 return -ENODEV;
1185
1186 dev = cl->dev;
1187
Tomas Winkler5db75142015-02-10 10:39:42 +02001188 buf = &cb->buf;
Tomas Winkler136698e2013-09-16 23:44:44 +03001189
1190 rets = mei_cl_flow_ctrl_creds(cl);
1191 if (rets < 0)
1192 return rets;
1193
1194 if (rets == 0) {
Tomas Winkler04bb1392014-03-18 22:52:04 +02001195 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler136698e2013-09-16 23:44:44 +03001196 return 0;
1197 }
1198
Tomas Winkler9d098192014-02-19 17:35:48 +02001199 slots = mei_hbuf_empty_slots(dev);
Tomas Winkler136698e2013-09-16 23:44:44 +03001200 len = buf->size - cb->buf_idx;
1201 msg_slots = mei_data2slots(len);
1202
Tomas Winkler21767542013-06-23 09:36:59 +03001203 mei_hdr.host_addr = cl->host_client_id;
1204 mei_hdr.me_addr = cl->me_client_id;
1205 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +02001206 mei_hdr.internal = cb->internal;
Tomas Winkler21767542013-06-23 09:36:59 +03001207
Tomas Winkler9d098192014-02-19 17:35:48 +02001208 if (slots >= msg_slots) {
Tomas Winkler21767542013-06-23 09:36:59 +03001209 mei_hdr.length = len;
1210 mei_hdr.msg_complete = 1;
1211 /* Split the message only if we can write the whole host buffer */
Tomas Winkler9d098192014-02-19 17:35:48 +02001212 } else if (slots == dev->hbuf_depth) {
1213 msg_slots = slots;
1214 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winkler21767542013-06-23 09:36:59 +03001215 mei_hdr.length = len;
1216 mei_hdr.msg_complete = 0;
1217 } else {
1218 /* wait for next time the host buffer is empty */
1219 return 0;
1220 }
1221
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001222 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
Tomas Winkler5db75142015-02-10 10:39:42 +02001223 cb->buf.size, cb->buf_idx);
Tomas Winkler21767542013-06-23 09:36:59 +03001224
Tomas Winkler136698e2013-09-16 23:44:44 +03001225 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001226 if (rets) {
1227 cl->status = rets;
Tomas Winkler21767542013-06-23 09:36:59 +03001228 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001229 return rets;
Tomas Winkler21767542013-06-23 09:36:59 +03001230 }
1231
1232 cl->status = 0;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +03001233 cl->writing_state = MEI_WRITING;
Tomas Winkler21767542013-06-23 09:36:59 +03001234 cb->buf_idx += mei_hdr.length;
Tomas Winkler86601722015-02-10 10:39:40 +02001235 cb->completed = mei_hdr.msg_complete == 1;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +03001236
Tomas Winkler21767542013-06-23 09:36:59 +03001237 if (mei_hdr.msg_complete) {
1238 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001239 return -EIO;
Tomas Winkler21767542013-06-23 09:36:59 +03001240 list_move_tail(&cb->list, &dev->write_waiting_list.list);
1241 }
1242
1243 return 0;
1244}
1245
1246/**
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001247 * mei_cl_write - submit a write cb to mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001248 * assumes device_lock is locked
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001249 *
1250 * @cl: host client
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001251 * @cb: write callback with filled data
Alexander Usyskince231392014-09-29 16:31:50 +03001252 * @blocking: block until completed
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001253 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001254 * Return: number of bytes sent on success, <0 on failure.
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001255 */
1256int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
1257{
1258 struct mei_device *dev;
1259 struct mei_msg_data *buf;
1260 struct mei_msg_hdr mei_hdr;
1261 int rets;
1262
1263
1264 if (WARN_ON(!cl || !cl->dev))
1265 return -ENODEV;
1266
1267 if (WARN_ON(!cb))
1268 return -EINVAL;
1269
1270 dev = cl->dev;
1271
1272
Tomas Winkler5db75142015-02-10 10:39:42 +02001273 buf = &cb->buf;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001274
Alexander Usyskin0a01e972014-09-29 16:31:47 +03001275 cl_dbg(dev, cl, "size=%d\n", buf->size);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001276
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001277 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001278 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001279 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001280 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1281 return rets;
1282 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001283
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001284 cb->buf_idx = 0;
1285 cl->writing_state = MEI_IDLE;
1286
1287 mei_hdr.host_addr = cl->host_client_id;
1288 mei_hdr.me_addr = cl->me_client_id;
1289 mei_hdr.reserved = 0;
1290 mei_hdr.msg_complete = 0;
1291 mei_hdr.internal = cb->internal;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001292
1293 rets = mei_cl_flow_ctrl_creds(cl);
1294 if (rets < 0)
1295 goto err;
1296
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001297 if (rets == 0) {
1298 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001299 rets = buf->size;
1300 goto out;
1301 }
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001302 if (!mei_hbuf_acquire(dev)) {
1303 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1304 rets = buf->size;
1305 goto out;
1306 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001307
1308 /* Check for a maximum length */
1309 if (buf->size > mei_hbuf_max_len(dev)) {
1310 mei_hdr.length = mei_hbuf_max_len(dev);
1311 mei_hdr.msg_complete = 0;
1312 } else {
1313 mei_hdr.length = buf->size;
1314 mei_hdr.msg_complete = 1;
1315 }
1316
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001317 rets = mei_write_message(dev, &mei_hdr, buf->data);
1318 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001319 goto err;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001320
1321 cl->writing_state = MEI_WRITING;
1322 cb->buf_idx = mei_hdr.length;
Tomas Winkler86601722015-02-10 10:39:40 +02001323 cb->completed = mei_hdr.msg_complete == 1;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001324
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001325out:
1326 if (mei_hdr.msg_complete) {
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001327 rets = mei_cl_flow_ctrl_reduce(cl);
1328 if (rets < 0)
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001329 goto err;
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001330
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001331 list_add_tail(&cb->list, &dev->write_waiting_list.list);
1332 } else {
1333 list_add_tail(&cb->list, &dev->write_list.list);
1334 }
1335
1336
1337 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1338
1339 mutex_unlock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001340 rets = wait_event_interruptible(cl->tx_wait,
1341 cl->writing_state == MEI_WRITE_COMPLETE);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001342 mutex_lock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001343 /* wait_event_interruptible returns -ERESTARTSYS */
1344 if (rets) {
1345 if (signal_pending(current))
1346 rets = -EINTR;
1347 goto err;
1348 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001349 }
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001350
1351 rets = buf->size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001352err:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001353 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001354 pm_runtime_mark_last_busy(dev->dev);
1355 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001356
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001357 return rets;
1358}
1359
1360
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001361/**
1362 * mei_cl_complete - processes completed operation for a client
1363 *
1364 * @cl: private data of the file object.
1365 * @cb: callback block.
1366 */
1367void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1368{
Tomas Winkler3c666182015-05-04 09:43:52 +03001369 switch (cb->fop_type) {
1370 case MEI_FOP_WRITE:
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001371 mei_io_cb_free(cb);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001372 cl->writing_state = MEI_WRITE_COMPLETE;
1373 if (waitqueue_active(&cl->tx_wait))
1374 wake_up_interruptible(&cl->tx_wait);
Tomas Winkler3c666182015-05-04 09:43:52 +03001375 break;
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001376
Tomas Winkler3c666182015-05-04 09:43:52 +03001377 case MEI_FOP_READ:
Tomas Winklera9bed612015-02-10 10:39:46 +02001378 list_add_tail(&cb->list, &cl->rd_completed);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001379 if (waitqueue_active(&cl->rx_wait))
Tomas Winkler1d9013f2015-03-27 00:27:57 +02001380 wake_up_interruptible_all(&cl->rx_wait);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001381 else
1382 mei_cl_bus_rx_event(cl);
Tomas Winkler3c666182015-05-04 09:43:52 +03001383 break;
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001384
Tomas Winkler3c666182015-05-04 09:43:52 +03001385 case MEI_FOP_CONNECT:
1386 case MEI_FOP_DISCONNECT:
1387 if (waitqueue_active(&cl->wait))
1388 wake_up(&cl->wait);
1389
1390 break;
1391 default:
1392 BUG_ON(0);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001393 }
1394}
1395
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001396
1397/**
Tomas Winkler074b4c02013-02-06 14:06:44 +02001398 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1399 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001400 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001401 */
Tomas Winkler074b4c02013-02-06 14:06:44 +02001402void mei_cl_all_disconnect(struct mei_device *dev)
1403{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001404 struct mei_cl *cl;
Tomas Winkler074b4c02013-02-06 14:06:44 +02001405
Tomas Winkler3c666182015-05-04 09:43:52 +03001406 list_for_each_entry(cl, &dev->file_list, link)
1407 mei_cl_set_disconnected(cl);
Tomas Winkler074b4c02013-02-06 14:06:44 +02001408}
1409
1410
1411/**
Tomas Winkler52908012013-07-24 16:22:57 +03001412 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
Tomas Winkler074b4c02013-02-06 14:06:44 +02001413 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001414 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001415 */
Tomas Winkler52908012013-07-24 16:22:57 +03001416void mei_cl_all_wakeup(struct mei_device *dev)
Tomas Winkler074b4c02013-02-06 14:06:44 +02001417{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001418 struct mei_cl *cl;
Tomas Winkler92db1552014-09-29 16:31:37 +03001419
Tomas Winkler31f88f52014-02-17 15:13:25 +02001420 list_for_each_entry(cl, &dev->file_list, link) {
Tomas Winkler074b4c02013-02-06 14:06:44 +02001421 if (waitqueue_active(&cl->rx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001422 cl_dbg(dev, cl, "Waking up reading client!\n");
Tomas Winkler074b4c02013-02-06 14:06:44 +02001423 wake_up_interruptible(&cl->rx_wait);
1424 }
Tomas Winkler52908012013-07-24 16:22:57 +03001425 if (waitqueue_active(&cl->tx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001426 cl_dbg(dev, cl, "Waking up writing client!\n");
Tomas Winkler52908012013-07-24 16:22:57 +03001427 wake_up_interruptible(&cl->tx_wait);
1428 }
Tomas Winkler074b4c02013-02-06 14:06:44 +02001429 }
1430}
1431
1432/**
1433 * mei_cl_all_write_clear - clear all pending writes
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001434 *
1435 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001436 */
1437void mei_cl_all_write_clear(struct mei_device *dev)
1438{
Tomas Winklercc99ecf2014-03-10 15:10:40 +02001439 mei_io_list_free(&dev->write_list, NULL);
1440 mei_io_list_free(&dev->write_waiting_list, NULL);
Tomas Winkler074b4c02013-02-06 14:06:44 +02001441}
1442
1443