blob: 1987a201c7f3b7b980250ad0eaf95faa141c142c [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/**
Alexander Usyskind49ed642015-05-04 09:43:54 +030086 * __mei_me_cl_del - delete me client from the list and decrease
Tomas Winklerb7d88512015-02-10 10:39:31 +020087 * 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
Alexander Usyskind49ed642015-05-04 09:43:54 +030099 list_del_init(&me_cl->list);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200100 mei_me_cl_put(me_cl);
101}
102
103/**
Alexander Usyskind49ed642015-05-04 09:43:54 +0300104 * mei_me_cl_del - delete me client from the list and decrease
105 * reference counter
106 *
107 * @dev: mei device
108 * @me_cl: me client
109 */
110void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
111{
112 down_write(&dev->me_clients_rwsem);
113 __mei_me_cl_del(dev, me_cl);
114 up_write(&dev->me_clients_rwsem);
115}
116
117/**
Tomas Winklerb7d88512015-02-10 10:39:31 +0200118 * mei_me_cl_add - add me client to the list
119 *
120 * @dev: mei device
121 * @me_cl: me client
122 */
123void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
124{
125 down_write(&dev->me_clients_rwsem);
126 list_add(&me_cl->list, &dev->me_clients);
127 up_write(&dev->me_clients_rwsem);
128}
129
130/**
131 * __mei_me_cl_by_uuid - locate me client by uuid
132 * increases ref count
133 *
134 * @dev: mei device
135 * @uuid: me client uuid
136 *
137 * Return: me client or NULL if not found
138 *
139 * Locking: dev->me_clients_rwsem
140 */
141static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
142 const uuid_le *uuid)
143{
144 struct mei_me_client *me_cl;
145 const uuid_le *pn;
146
147 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
148
149 list_for_each_entry(me_cl, &dev->me_clients, list) {
150 pn = &me_cl->props.protocol_name;
151 if (uuid_le_cmp(*uuid, *pn) == 0)
152 return mei_me_cl_get(me_cl);
153 }
154
155 return NULL;
156}
157
158/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300159 * mei_me_cl_by_uuid - locate me client by uuid
Tomas Winkler79563db2015-01-11 00:07:16 +0200160 * increases ref count
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200161 *
162 * @dev: mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300163 * @uuid: me client uuid
Alexander Usyskina27a76d2014-02-17 15:13:22 +0200164 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300165 * Return: me client or NULL if not found
Tomas Winklerb7d88512015-02-10 10:39:31 +0200166 *
167 * Locking: dev->me_clients_rwsem
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200168 */
Tomas Winklerb7d88512015-02-10 10:39:31 +0200169struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
Tomas Winklerd3208322014-08-24 12:08:55 +0300170 const uuid_le *uuid)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200171{
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300172 struct mei_me_client *me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200173
Tomas Winklerb7d88512015-02-10 10:39:31 +0200174 down_read(&dev->me_clients_rwsem);
175 me_cl = __mei_me_cl_by_uuid(dev, uuid);
176 up_read(&dev->me_clients_rwsem);
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200177
Tomas Winklerb7d88512015-02-10 10:39:31 +0200178 return me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200179}
180
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200181/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300182 * mei_me_cl_by_id - locate me client by client id
Tomas Winkler79563db2015-01-11 00:07:16 +0200183 * increases ref count
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200184 *
185 * @dev: the device structure
186 * @client_id: me client id
187 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300188 * Return: me client or NULL if not found
Tomas Winklerb7d88512015-02-10 10:39:31 +0200189 *
190 * Locking: dev->me_clients_rwsem
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200191 */
Tomas Winklerd3208322014-08-24 12:08:55 +0300192struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200193{
Alexander Usyskina27a76d2014-02-17 15:13:22 +0200194
Tomas Winklerb7d88512015-02-10 10:39:31 +0200195 struct mei_me_client *__me_cl, *me_cl = NULL;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200196
Tomas Winklerb7d88512015-02-10 10:39:31 +0200197 down_read(&dev->me_clients_rwsem);
198 list_for_each_entry(__me_cl, &dev->me_clients, list) {
199 if (__me_cl->client_id == client_id) {
200 me_cl = mei_me_cl_get(__me_cl);
201 break;
202 }
203 }
204 up_read(&dev->me_clients_rwsem);
205
206 return me_cl;
207}
208
209/**
210 * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
211 * increases ref count
212 *
213 * @dev: the device structure
214 * @uuid: me client uuid
215 * @client_id: me client id
216 *
217 * Return: me client or null if not found
218 *
219 * Locking: dev->me_clients_rwsem
220 */
221static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
222 const uuid_le *uuid, u8 client_id)
223{
224 struct mei_me_client *me_cl;
225 const uuid_le *pn;
226
227 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
228
229 list_for_each_entry(me_cl, &dev->me_clients, list) {
230 pn = &me_cl->props.protocol_name;
231 if (uuid_le_cmp(*uuid, *pn) == 0 &&
232 me_cl->client_id == client_id)
Tomas Winkler79563db2015-01-11 00:07:16 +0200233 return mei_me_cl_get(me_cl);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200234 }
Tomas Winkler79563db2015-01-11 00:07:16 +0200235
Tomas Winklerd3208322014-08-24 12:08:55 +0300236 return NULL;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200237}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200238
Tomas Winklerb7d88512015-02-10 10:39:31 +0200239
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300240/**
241 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
Tomas Winkler79563db2015-01-11 00:07:16 +0200242 * increases ref count
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300243 *
244 * @dev: the device structure
245 * @uuid: me client uuid
246 * @client_id: me client id
247 *
Tomas Winklerb7d88512015-02-10 10:39:31 +0200248 * Return: me client or null if not found
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300249 */
Tomas Winklerd880f322014-08-21 14:29:15 +0300250struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
251 const uuid_le *uuid, u8 client_id)
252{
253 struct mei_me_client *me_cl;
254
Tomas Winklerb7d88512015-02-10 10:39:31 +0200255 down_read(&dev->me_clients_rwsem);
256 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
257 up_read(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200258
Tomas Winklerb7d88512015-02-10 10:39:31 +0200259 return me_cl;
Tomas Winklerd880f322014-08-21 14:29:15 +0300260}
261
Tomas Winkler25ca6472014-08-21 14:29:14 +0300262/**
Tomas Winkler79563db2015-01-11 00:07:16 +0200263 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
Tomas Winkler25ca6472014-08-21 14:29:14 +0300264 *
265 * @dev: the device structure
266 * @uuid: me client uuid
Tomas Winkler79563db2015-01-11 00:07:16 +0200267 *
268 * Locking: called under "dev->device_lock" lock
Tomas Winkler25ca6472014-08-21 14:29:14 +0300269 */
Tomas Winkler79563db2015-01-11 00:07:16 +0200270void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
Tomas Winkler25ca6472014-08-21 14:29:14 +0300271{
Tomas Winklerb7d88512015-02-10 10:39:31 +0200272 struct mei_me_client *me_cl;
Tomas Winkler25ca6472014-08-21 14:29:14 +0300273
Tomas Winkler79563db2015-01-11 00:07:16 +0200274 dev_dbg(dev->dev, "remove %pUl\n", uuid);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200275
276 down_write(&dev->me_clients_rwsem);
277 me_cl = __mei_me_cl_by_uuid(dev, uuid);
278 __mei_me_cl_del(dev, me_cl);
279 up_write(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200280}
281
282/**
283 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
284 *
285 * @dev: the device structure
286 * @uuid: me client uuid
287 * @id: me client id
288 *
289 * Locking: called under "dev->device_lock" lock
290 */
291void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
292{
Tomas Winklerb7d88512015-02-10 10:39:31 +0200293 struct mei_me_client *me_cl;
Tomas Winkler79563db2015-01-11 00:07:16 +0200294
295 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200296
297 down_write(&dev->me_clients_rwsem);
298 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
299 __mei_me_cl_del(dev, me_cl);
300 up_write(&dev->me_clients_rwsem);
Tomas Winkler25ca6472014-08-21 14:29:14 +0300301}
302
Tomas Winkler79563db2015-01-11 00:07:16 +0200303/**
304 * mei_me_cl_rm_all - remove all me clients
305 *
306 * @dev: the device structure
307 *
308 * Locking: called under "dev->device_lock" lock
309 */
310void mei_me_cl_rm_all(struct mei_device *dev)
311{
312 struct mei_me_client *me_cl, *next;
313
Tomas Winklerb7d88512015-02-10 10:39:31 +0200314 down_write(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200315 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
Tomas Winklerb7d88512015-02-10 10:39:31 +0200316 __mei_me_cl_del(dev, me_cl);
317 up_write(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200318}
319
Tomas Winkler9ca90502013-01-08 23:07:13 +0200320/**
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200321 * mei_cl_cmp_id - tells if the clients are the same
322 *
323 * @cl1: host client 1
324 * @cl2: host client 2
325 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300326 * Return: true - if the clients has same host and me ids
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200327 * false - otherwise
328 */
329static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
330 const struct mei_cl *cl2)
331{
332 return cl1 && cl2 &&
333 (cl1->host_client_id == cl2->host_client_id) &&
Alexander Usyskind49ed642015-05-04 09:43:54 +0300334 (mei_cl_me_id(cl1) == mei_cl_me_id(cl2));
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200335}
336
337/**
Tomas Winkler9ca90502013-01-08 23:07:13 +0200338 * mei_io_cb_free - free mei_cb_private related memory
339 *
340 * @cb: mei callback struct
341 */
342void mei_io_cb_free(struct mei_cl_cb *cb)
343{
344 if (cb == NULL)
345 return;
346
Tomas Winkler928fa662015-02-10 10:39:45 +0200347 list_del(&cb->list);
Tomas Winkler5db75142015-02-10 10:39:42 +0200348 kfree(cb->buf.data);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200349 kfree(cb);
350}
351
352/**
353 * mei_io_cb_init - allocate and initialize io callback
354 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300355 * @cl: mei client
Tomas Winklerbca67d62015-02-10 10:39:43 +0200356 * @type: operation type
Masanari Iida393b1482013-04-05 01:05:05 +0900357 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200358 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300359 * Return: mei_cl_cb pointer or NULL;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200360 */
Tomas Winklerbca67d62015-02-10 10:39:43 +0200361struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, enum mei_cb_file_ops type,
362 struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200363{
364 struct mei_cl_cb *cb;
365
366 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
367 if (!cb)
368 return NULL;
369
Tomas Winkler928fa662015-02-10 10:39:45 +0200370 INIT_LIST_HEAD(&cb->list);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200371 cb->file_object = fp;
372 cb->cl = cl;
373 cb->buf_idx = 0;
Tomas Winklerbca67d62015-02-10 10:39:43 +0200374 cb->fop_type = type;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200375 return cb;
376}
377
378/**
Tomas Winkler928fa662015-02-10 10:39:45 +0200379 * __mei_io_list_flush - removes and frees cbs belonging to cl.
380 *
381 * @list: an instance of our list structure
382 * @cl: host client, can be NULL for flushing the whole list
383 * @free: whether to free the cbs
384 */
385static void __mei_io_list_flush(struct mei_cl_cb *list,
386 struct mei_cl *cl, bool free)
387{
388 struct mei_cl_cb *cb, *next;
389
390 /* enable removing everything if no cl is specified */
391 list_for_each_entry_safe(cb, next, &list->list, list) {
392 if (!cl || mei_cl_cmp_id(cl, cb->cl)) {
393 list_del_init(&cb->list);
394 if (free)
395 mei_io_cb_free(cb);
396 }
397 }
398}
399
400/**
401 * mei_io_list_flush - removes list entry belonging to cl.
402 *
403 * @list: An instance of our list structure
404 * @cl: host client
405 */
406void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
407{
408 __mei_io_list_flush(list, cl, false);
409}
410
411/**
412 * mei_io_list_free - removes cb belonging to cl and free them
413 *
414 * @list: An instance of our list structure
415 * @cl: host client
416 */
417static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
418{
419 __mei_io_list_flush(list, cl, true);
420}
421
422/**
Tomas Winkler5db75142015-02-10 10:39:42 +0200423 * mei_io_cb_alloc_buf - allocate callback buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200424 *
Masanari Iida393b1482013-04-05 01:05:05 +0900425 * @cb: io callback structure
426 * @length: size of the buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200427 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300428 * Return: 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200429 * -EINVAL if cb is NULL
430 * -ENOMEM if allocation failed
431 */
Tomas Winkler5db75142015-02-10 10:39:42 +0200432int mei_io_cb_alloc_buf(struct mei_cl_cb *cb, size_t length)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200433{
434 if (!cb)
435 return -EINVAL;
436
437 if (length == 0)
438 return 0;
439
Tomas Winkler5db75142015-02-10 10:39:42 +0200440 cb->buf.data = kmalloc(length, GFP_KERNEL);
441 if (!cb->buf.data)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200442 return -ENOMEM;
Tomas Winkler5db75142015-02-10 10:39:42 +0200443 cb->buf.size = length;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200444 return 0;
445}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200446
447/**
Tomas Winklerbca67d62015-02-10 10:39:43 +0200448 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
449 *
450 * @cl: host client
451 * @length: size of the buffer
452 * @type: operation type
453 * @fp: associated file pointer (might be NULL)
454 *
455 * Return: cb on success and NULL on failure
456 */
457struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
458 enum mei_cb_file_ops type, struct file *fp)
459{
460 struct mei_cl_cb *cb;
461
462 cb = mei_io_cb_init(cl, type, fp);
463 if (!cb)
464 return NULL;
465
466 if (mei_io_cb_alloc_buf(cb, length)) {
467 mei_io_cb_free(cb);
468 return NULL;
469 }
470
471 return cb;
472}
473
474/**
Tomas Winklera9bed612015-02-10 10:39:46 +0200475 * mei_cl_read_cb - find this cl's callback in the read list
476 * for a specific file
477 *
478 * @cl: host client
479 * @fp: file pointer (matching cb file object), may be NULL
480 *
481 * Return: cb on success, NULL if cb is not found
482 */
483struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
484{
485 struct mei_cl_cb *cb;
486
487 list_for_each_entry(cb, &cl->rd_completed, list)
488 if (!fp || fp == cb->file_object)
489 return cb;
490
491 return NULL;
492}
493
494/**
495 * mei_cl_read_cb_flush - free client's read pending and completed cbs
496 * for a specific file
497 *
498 * @cl: host client
499 * @fp: file pointer (matching cb file object), may be NULL
500 */
501void mei_cl_read_cb_flush(const struct mei_cl *cl, const struct file *fp)
502{
503 struct mei_cl_cb *cb, *next;
504
505 list_for_each_entry_safe(cb, next, &cl->rd_completed, list)
506 if (!fp || fp == cb->file_object)
507 mei_io_cb_free(cb);
508
509
510 list_for_each_entry_safe(cb, next, &cl->rd_pending, list)
511 if (!fp || fp == cb->file_object)
512 mei_io_cb_free(cb);
513}
514
515/**
Tomas Winkler9ca90502013-01-08 23:07:13 +0200516 * mei_cl_flush_queues - flushes queue lists belonging to cl.
517 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200518 * @cl: host client
Tomas Winklera9bed612015-02-10 10:39:46 +0200519 * @fp: file pointer (matching cb file object), may be NULL
Alexander Usyskince231392014-09-29 16:31:50 +0300520 *
521 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200522 */
Tomas Winklera9bed612015-02-10 10:39:46 +0200523int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200524{
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300525 struct mei_device *dev;
526
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200527 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200528 return -EINVAL;
529
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300530 dev = cl->dev;
531
532 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200533 mei_io_list_free(&cl->dev->write_list, cl);
534 mei_io_list_free(&cl->dev->write_waiting_list, cl);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200535 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
536 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
537 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
538 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
Tomas Winklera9bed612015-02-10 10:39:46 +0200539
540 mei_cl_read_cb_flush(cl, fp);
541
Tomas Winkler9ca90502013-01-08 23:07:13 +0200542 return 0;
543}
544
Tomas Winkler9ca90502013-01-08 23:07:13 +0200545
546/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200547 * mei_cl_init - initializes cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200548 *
549 * @cl: host client to be initialized
550 * @dev: mei device
551 */
552void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
553{
554 memset(cl, 0, sizeof(struct mei_cl));
555 init_waitqueue_head(&cl->wait);
556 init_waitqueue_head(&cl->rx_wait);
557 init_waitqueue_head(&cl->tx_wait);
Tomas Winklera9bed612015-02-10 10:39:46 +0200558 INIT_LIST_HEAD(&cl->rd_completed);
559 INIT_LIST_HEAD(&cl->rd_pending);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200560 INIT_LIST_HEAD(&cl->link);
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200561 INIT_LIST_HEAD(&cl->device_link);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200562 cl->writing_state = MEI_IDLE;
Tomas Winkler3c666182015-05-04 09:43:52 +0300563 cl->state = MEI_FILE_INITIALIZING;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200564 cl->dev = dev;
565}
566
567/**
568 * mei_cl_allocate - allocates cl structure and sets it up.
569 *
570 * @dev: mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300571 * Return: The allocated file or NULL on failure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200572 */
573struct mei_cl *mei_cl_allocate(struct mei_device *dev)
574{
575 struct mei_cl *cl;
576
577 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
578 if (!cl)
579 return NULL;
580
581 mei_cl_init(cl, dev);
582
583 return cl;
584}
585
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200586/**
Alexander Usyskin3908be62015-02-10 10:39:35 +0200587 * mei_cl_link - allocate host id in the host map
Tomas Winkler9ca90502013-01-08 23:07:13 +0200588 *
Alexander Usyskin3908be62015-02-10 10:39:35 +0200589 * @cl: host client
Tomas Winkler03b8d342015-02-10 10:39:44 +0200590 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
Masanari Iida393b1482013-04-05 01:05:05 +0900591 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300592 * Return: 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200593 * -EINVAL on incorrect values
Tomas Winkler03b8d342015-02-10 10:39:44 +0200594 * -EMFILE if open count exceeded.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200595 */
Tomas Winkler781d0d82013-01-08 23:07:22 +0200596int mei_cl_link(struct mei_cl *cl, int id)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200597{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200598 struct mei_device *dev;
Tomas Winkler22f96a02013-09-16 23:44:47 +0300599 long open_handle_count;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200600
Tomas Winkler781d0d82013-01-08 23:07:22 +0200601 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200602 return -EINVAL;
603
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200604 dev = cl->dev;
605
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200606 /* If Id is not assigned get one*/
Tomas Winkler781d0d82013-01-08 23:07:22 +0200607 if (id == MEI_HOST_CLIENT_ID_ANY)
608 id = find_first_zero_bit(dev->host_clients_map,
609 MEI_CLIENTS_MAX);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200610
Tomas Winkler781d0d82013-01-08 23:07:22 +0200611 if (id >= MEI_CLIENTS_MAX) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300612 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
Tomas Winklere036cc52013-09-16 23:44:46 +0300613 return -EMFILE;
614 }
615
Tomas Winkler22f96a02013-09-16 23:44:47 +0300616 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
617 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300618 dev_err(dev->dev, "open_handle_count exceeded %d",
Tomas Winklere036cc52013-09-16 23:44:46 +0300619 MEI_MAX_OPEN_HANDLE_COUNT);
620 return -EMFILE;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200621 }
622
Tomas Winkler781d0d82013-01-08 23:07:22 +0200623 dev->open_handle_count++;
624
625 cl->host_client_id = id;
626 list_add_tail(&cl->link, &dev->file_list);
627
628 set_bit(id, dev->host_clients_map);
629
630 cl->state = MEI_FILE_INITIALIZING;
631
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300632 cl_dbg(dev, cl, "link cl\n");
Tomas Winkler781d0d82013-01-08 23:07:22 +0200633 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200634}
Tomas Winkler781d0d82013-01-08 23:07:22 +0200635
Tomas Winkler9ca90502013-01-08 23:07:13 +0200636/**
Alexander Usyskind49ed642015-05-04 09:43:54 +0300637 * mei_cl_unlink - remove host client from the list
Tomas Winkler9ca90502013-01-08 23:07:13 +0200638 *
Masanari Iida393b1482013-04-05 01:05:05 +0900639 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +0300640 *
641 * Return: always 0
Tomas Winkler9ca90502013-01-08 23:07:13 +0200642 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200643int mei_cl_unlink(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200644{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200645 struct mei_device *dev;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200646
Tomas Winkler781d0d82013-01-08 23:07:22 +0200647 /* don't shout on error exit path */
648 if (!cl)
649 return 0;
650
Tomas Winkler8e9a4a92013-01-10 17:32:14 +0200651 /* wd and amthif might not be initialized */
652 if (!cl->dev)
653 return 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200654
655 dev = cl->dev;
656
Tomas Winklera14c44d2013-09-16 23:44:45 +0300657 cl_dbg(dev, cl, "unlink client");
658
Tomas Winkler22f96a02013-09-16 23:44:47 +0300659 if (dev->open_handle_count > 0)
660 dev->open_handle_count--;
661
662 /* never clear the 0 bit */
663 if (cl->host_client_id)
664 clear_bit(cl->host_client_id, dev->host_clients_map);
665
666 list_del_init(&cl->link);
667
668 cl->state = MEI_FILE_INITIALIZING;
669
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200670 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200671}
672
673
674void mei_host_client_init(struct work_struct *work)
675{
Tomas Winklerb7d88512015-02-10 10:39:31 +0200676 struct mei_device *dev =
677 container_of(work, struct mei_device, init_work);
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300678 struct mei_me_client *me_cl;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200679
680 mutex_lock(&dev->device_lock);
681
Tomas Winkler9ca90502013-01-08 23:07:13 +0200682
Tomas Winklerb7d88512015-02-10 10:39:31 +0200683 me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
684 if (me_cl)
Alexander Usyskind49ed642015-05-04 09:43:54 +0300685 mei_amthif_host_init(dev, me_cl);
Tomas Winklerb43baf62015-03-04 18:41:34 +0200686 mei_me_cl_put(me_cl);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200687
Tomas Winklerb7d88512015-02-10 10:39:31 +0200688 me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
689 if (me_cl)
Alexander Usyskind49ed642015-05-04 09:43:54 +0300690 mei_wd_host_init(dev, me_cl);
Tomas Winklerb43baf62015-03-04 18:41:34 +0200691 mei_me_cl_put(me_cl);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200692
693 me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid);
694 if (me_cl)
Alexander Usyskind49ed642015-05-04 09:43:54 +0300695 mei_nfc_host_init(dev, me_cl);
Tomas Winklerb43baf62015-03-04 18:41:34 +0200696 mei_me_cl_put(me_cl);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200697
Tomas Winkler9ca90502013-01-08 23:07:13 +0200698
699 dev->dev_state = MEI_DEV_ENABLED;
Tomas Winkler6adb8ef2014-01-12 00:36:10 +0200700 dev->reset_count = 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200701 mutex_unlock(&dev->device_lock);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200702
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300703 pm_runtime_mark_last_busy(dev->dev);
704 dev_dbg(dev->dev, "rpm: autosuspend\n");
705 pm_runtime_autosuspend(dev->dev);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200706}
707
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200708/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300709 * mei_hbuf_acquire - try to acquire host buffer
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200710 *
711 * @dev: the device structure
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300712 * Return: true if host buffer was acquired
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200713 */
714bool mei_hbuf_acquire(struct mei_device *dev)
715{
Tomas Winkler04bb1392014-03-18 22:52:04 +0200716 if (mei_pg_state(dev) == MEI_PG_ON ||
Alexander Usyskin3dc196e2015-06-13 08:51:17 +0300717 mei_pg_in_transition(dev)) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300718 dev_dbg(dev->dev, "device is in pg\n");
Tomas Winkler04bb1392014-03-18 22:52:04 +0200719 return false;
720 }
721
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200722 if (!dev->hbuf_is_ready) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300723 dev_dbg(dev->dev, "hbuf is not ready\n");
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200724 return false;
725 }
726
727 dev->hbuf_is_ready = false;
728
729 return true;
730}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200731
732/**
Tomas Winkler3c666182015-05-04 09:43:52 +0300733 * mei_cl_set_disconnected - set disconnected state and clear
734 * associated states and resources
735 *
736 * @cl: host client
737 */
738void mei_cl_set_disconnected(struct mei_cl *cl)
739{
740 struct mei_device *dev = cl->dev;
741
742 if (cl->state == MEI_FILE_DISCONNECTED ||
743 cl->state == MEI_FILE_INITIALIZING)
744 return;
745
746 cl->state = MEI_FILE_DISCONNECTED;
747 mei_io_list_flush(&dev->ctrl_rd_list, cl);
748 mei_io_list_flush(&dev->ctrl_wr_list, cl);
749 cl->mei_flow_ctrl_creds = 0;
750 cl->timer_count = 0;
Alexander Usyskind49ed642015-05-04 09:43:54 +0300751
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300752 if (!cl->me_cl)
753 return;
754
755 if (!WARN_ON(cl->me_cl->connect_count == 0))
756 cl->me_cl->connect_count--;
757
Alexander Usyskinc241e9b2015-06-13 21:18:40 +0300758 if (cl->me_cl->connect_count == 0)
759 cl->me_cl->mei_flow_ctrl_creds = 0;
760
Alexander Usyskind49ed642015-05-04 09:43:54 +0300761 mei_me_cl_put(cl->me_cl);
762 cl->me_cl = NULL;
Tomas Winkler3c666182015-05-04 09:43:52 +0300763}
764
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300765static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
766{
Alexander Usyskin1df629e2015-05-04 09:43:56 +0300767 if (!mei_me_cl_get(me_cl))
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300768 return -ENOENT;
769
Alexander Usyskin1df629e2015-05-04 09:43:56 +0300770 /* only one connection is allowed for fixed address clients */
771 if (me_cl->props.fixed_address) {
772 if (me_cl->connect_count) {
773 mei_me_cl_put(me_cl);
774 return -EBUSY;
775 }
776 }
777
778 cl->me_cl = me_cl;
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300779 cl->state = MEI_FILE_CONNECTING;
780 cl->me_cl->connect_count++;
781
782 return 0;
783}
784
Tomas Winkler3c666182015-05-04 09:43:52 +0300785/*
786 * mei_cl_send_disconnect - send disconnect request
787 *
788 * @cl: host client
789 * @cb: callback block
790 *
791 * Return: 0, OK; otherwise, error.
792 */
793static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
794{
795 struct mei_device *dev;
796 int ret;
797
798 dev = cl->dev;
799
800 ret = mei_hbm_cl_disconnect_req(dev, cl);
801 cl->status = ret;
802 if (ret) {
803 cl->state = MEI_FILE_DISCONNECT_REPLY;
804 return ret;
805 }
806
807 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
808 cl->timer_count = MEI_CONNECT_TIMEOUT;
809
810 return 0;
811}
812
813/**
814 * mei_cl_irq_disconnect - processes close related operation from
815 * interrupt thread context - send disconnect request
816 *
817 * @cl: client
818 * @cb: callback block.
819 * @cmpl_list: complete list.
820 *
821 * Return: 0, OK; otherwise, error.
822 */
823int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
824 struct mei_cl_cb *cmpl_list)
825{
826 struct mei_device *dev = cl->dev;
827 u32 msg_slots;
828 int slots;
829 int ret;
830
831 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
832 slots = mei_hbuf_empty_slots(dev);
833
834 if (slots < msg_slots)
835 return -EMSGSIZE;
836
837 ret = mei_cl_send_disconnect(cl, cb);
838 if (ret)
839 list_move_tail(&cb->list, &cmpl_list->list);
840
841 return ret;
842}
843
844
845
846/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200847 * mei_cl_disconnect - disconnect host client from the me one
Tomas Winkler9ca90502013-01-08 23:07:13 +0200848 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200849 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200850 *
851 * Locking: called under "dev->device_lock" lock
852 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300853 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200854 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200855int mei_cl_disconnect(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200856{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200857 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200858 struct mei_cl_cb *cb;
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300859 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200860
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200861 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200862 return -ENODEV;
863
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200864 dev = cl->dev;
865
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300866 cl_dbg(dev, cl, "disconnecting");
867
Tomas Winkler3c666182015-05-04 09:43:52 +0300868 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200869 return 0;
870
Alexander Usyskin1df629e2015-05-04 09:43:56 +0300871 if (mei_cl_is_fixed_address(cl)) {
872 mei_cl_set_disconnected(cl);
873 return 0;
874 }
875
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300876 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200877 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300878 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200879 cl_err(dev, cl, "rpm: get failed %d\n", rets);
880 return rets;
881 }
882
Tomas Winkler3c666182015-05-04 09:43:52 +0300883 cl->state = MEI_FILE_DISCONNECTING;
884
Tomas Winklerbca67d62015-02-10 10:39:43 +0200885 cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL);
886 rets = cb ? 0 : -ENOMEM;
887 if (rets)
Tomas Winkler3c666182015-05-04 09:43:52 +0300888 goto out;
889
890 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
891 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300892
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200893 if (mei_hbuf_acquire(dev)) {
Tomas Winkler3c666182015-05-04 09:43:52 +0300894 rets = mei_cl_send_disconnect(cl, cb);
895 if (rets) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300896 cl_err(dev, cl, "failed to disconnect.\n");
Tomas Winkler3c666182015-05-04 09:43:52 +0300897 goto out;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200898 }
Tomas Winkler9ca90502013-01-08 23:07:13 +0200899 }
Tomas Winkler3c666182015-05-04 09:43:52 +0300900
Tomas Winkler9ca90502013-01-08 23:07:13 +0200901 mutex_unlock(&dev->device_lock);
Tomas Winkler3c666182015-05-04 09:43:52 +0300902 wait_event_timeout(cl->wait, cl->state == MEI_FILE_DISCONNECT_REPLY,
903 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9ca90502013-01-08 23:07:13 +0200904 mutex_lock(&dev->device_lock);
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300905
Tomas Winkler3c666182015-05-04 09:43:52 +0300906 rets = cl->status;
907 if (cl->state != MEI_FILE_DISCONNECT_REPLY) {
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300908 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
909 rets = -ETIME;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200910 }
911
Tomas Winkler3c666182015-05-04 09:43:52 +0300912out:
913 /* we disconnect also on error */
914 mei_cl_set_disconnected(cl);
915 if (!rets)
916 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
917
Tomas Winkler04bb1392014-03-18 22:52:04 +0200918 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300919 pm_runtime_mark_last_busy(dev->dev);
920 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200921
Tomas Winkler9ca90502013-01-08 23:07:13 +0200922 mei_io_cb_free(cb);
923 return rets;
924}
925
926
927/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200928 * mei_cl_is_other_connecting - checks if other
929 * client with the same me client id is connecting
Tomas Winkler9ca90502013-01-08 23:07:13 +0200930 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200931 * @cl: private data of the file object
932 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300933 * Return: true if other client is connected, false - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200934 */
Tomas Winkler0c533572015-05-04 09:43:53 +0300935static bool mei_cl_is_other_connecting(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200936{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200937 struct mei_device *dev;
Tomas Winkler0c533572015-05-04 09:43:53 +0300938 struct mei_cl_cb *cb;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200939
940 dev = cl->dev;
941
Tomas Winkler0c533572015-05-04 09:43:53 +0300942 list_for_each_entry(cb, &dev->ctrl_rd_list.list, list) {
943 if (cb->fop_type == MEI_FOP_CONNECT &&
Alexander Usyskind49ed642015-05-04 09:43:54 +0300944 mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200945 return true;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200946 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200947
948 return false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200949}
950
951/**
Tomas Winkler0c533572015-05-04 09:43:53 +0300952 * mei_cl_send_connect - send connect request
953 *
954 * @cl: host client
955 * @cb: callback block
956 *
957 * Return: 0, OK; otherwise, error.
958 */
959static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
960{
961 struct mei_device *dev;
962 int ret;
963
964 dev = cl->dev;
965
966 ret = mei_hbm_cl_connect_req(dev, cl);
967 cl->status = ret;
968 if (ret) {
969 cl->state = MEI_FILE_DISCONNECT_REPLY;
970 return ret;
971 }
972
973 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
974 cl->timer_count = MEI_CONNECT_TIMEOUT;
975 return 0;
976}
977
978/**
979 * mei_cl_irq_connect - send connect request in irq_thread context
980 *
981 * @cl: host client
982 * @cb: callback block
983 * @cmpl_list: complete list
984 *
985 * Return: 0, OK; otherwise, error.
986 */
987int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
988 struct mei_cl_cb *cmpl_list)
989{
990 struct mei_device *dev = cl->dev;
991 u32 msg_slots;
992 int slots;
993 int rets;
994
995 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
996 slots = mei_hbuf_empty_slots(dev);
997
998 if (mei_cl_is_other_connecting(cl))
999 return 0;
1000
1001 if (slots < msg_slots)
1002 return -EMSGSIZE;
1003
1004 rets = mei_cl_send_connect(cl, cb);
1005 if (rets)
1006 list_move_tail(&cb->list, &cmpl_list->list);
1007
1008 return rets;
1009}
1010
1011/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +02001012 * mei_cl_connect - connect host client to the me one
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001013 *
1014 * @cl: host client
Alexander Usyskind49ed642015-05-04 09:43:54 +03001015 * @me_cl: me client
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001016 * @file: pointer to file structure
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001017 *
1018 * Locking: called under "dev->device_lock" lock
1019 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001020 * Return: 0 on success, <0 on failure.
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001021 */
Alexander Usyskind49ed642015-05-04 09:43:54 +03001022int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
1023 struct file *file)
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001024{
1025 struct mei_device *dev;
1026 struct mei_cl_cb *cb;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001027 int rets;
1028
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001029 if (WARN_ON(!cl || !cl->dev || !me_cl))
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001030 return -ENODEV;
1031
1032 dev = cl->dev;
1033
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001034 rets = mei_cl_set_connecting(cl, me_cl);
1035 if (rets)
1036 return rets;
1037
1038 if (mei_cl_is_fixed_address(cl)) {
1039 cl->state = MEI_FILE_CONNECTED;
1040 return 0;
1041 }
1042
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001043 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001044 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001045 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001046 cl_err(dev, cl, "rpm: get failed %d\n", rets);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001047 goto nortpm;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001048 }
1049
Tomas Winklerbca67d62015-02-10 10:39:43 +02001050 cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file);
1051 rets = cb ? 0 : -ENOMEM;
1052 if (rets)
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001053 goto out;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001054
Tomas Winkler0c533572015-05-04 09:43:53 +03001055 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1056
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001057 /* run hbuf acquire last so we don't have to undo */
1058 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
Tomas Winkler0c533572015-05-04 09:43:53 +03001059 rets = mei_cl_send_connect(cl, cb);
1060 if (rets)
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001061 goto out;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001062 }
1063
1064 mutex_unlock(&dev->device_lock);
Tomas Winkler12f45ed2014-08-21 14:29:18 +03001065 wait_event_timeout(cl->wait,
Alexander Usyskin285e2992014-02-17 15:13:20 +02001066 (cl->state == MEI_FILE_CONNECTED ||
Tomas Winkler3c666182015-05-04 09:43:52 +03001067 cl->state == MEI_FILE_DISCONNECT_REPLY),
Alexander Usyskin285e2992014-02-17 15:13:20 +02001068 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001069 mutex_lock(&dev->device_lock);
1070
Tomas Winklerf3de9b62015-03-27 00:27:58 +02001071 if (!mei_cl_is_connected(cl)) {
Tomas Winkler0c533572015-05-04 09:43:53 +03001072 /* timeout or something went really wrong */
Alexander Usyskin285e2992014-02-17 15:13:20 +02001073 if (!cl->status)
1074 cl->status = -EFAULT;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001075 }
1076
1077 rets = cl->status;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001078out:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001079 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001080 pm_runtime_mark_last_busy(dev->dev);
1081 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001082
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001083 mei_io_cb_free(cb);
Tomas Winkler0c533572015-05-04 09:43:53 +03001084
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001085nortpm:
Tomas Winkler0c533572015-05-04 09:43:53 +03001086 if (!mei_cl_is_connected(cl))
1087 mei_cl_set_disconnected(cl);
1088
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001089 return rets;
1090}
1091
1092/**
Tomas Winkler03b8d342015-02-10 10:39:44 +02001093 * mei_cl_alloc_linked - allocate and link host client
1094 *
1095 * @dev: the device structure
1096 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
1097 *
1098 * Return: cl on success ERR_PTR on failure
1099 */
1100struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id)
1101{
1102 struct mei_cl *cl;
1103 int ret;
1104
1105 cl = mei_cl_allocate(dev);
1106 if (!cl) {
1107 ret = -ENOMEM;
1108 goto err;
1109 }
1110
1111 ret = mei_cl_link(cl, id);
1112 if (ret)
1113 goto err;
1114
1115 return cl;
1116err:
1117 kfree(cl);
1118 return ERR_PTR(ret);
1119}
1120
1121
1122
1123/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001124 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001125 *
Tomas Winkler9ca90502013-01-08 23:07:13 +02001126 * @cl: private data of the file object
1127 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001128 * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001129 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001130int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001131{
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001132 int rets;
1133
Alexander Usyskind49ed642015-05-04 09:43:54 +03001134 if (WARN_ON(!cl || !cl->me_cl))
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001135 return -EINVAL;
1136
Tomas Winkler9ca90502013-01-08 23:07:13 +02001137 if (cl->mei_flow_ctrl_creds > 0)
1138 return 1;
1139
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001140 if (mei_cl_is_fixed_address(cl)) {
1141 rets = mei_cl_read_start(cl, mei_cl_mtu(cl), NULL);
1142 if (rets && rets != -EBUSY)
1143 return rets;
1144 return 1;
1145 }
1146
Alexander Usyskind49ed642015-05-04 09:43:54 +03001147 if (mei_cl_is_single_recv_buf(cl)) {
1148 if (cl->me_cl->mei_flow_ctrl_creds > 0)
1149 return 1;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001150 }
Alexander Usyskind49ed642015-05-04 09:43:54 +03001151 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001152}
1153
1154/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001155 * mei_cl_flow_ctrl_reduce - reduces flow_control.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001156 *
Tomas Winkler9ca90502013-01-08 23:07:13 +02001157 * @cl: private data of the file object
Masanari Iida393b1482013-04-05 01:05:05 +09001158 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001159 * Return:
Tomas Winkler9ca90502013-01-08 23:07:13 +02001160 * 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +02001161 * -EINVAL when ctrl credits are <= 0
1162 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001163int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001164{
Alexander Usyskind49ed642015-05-04 09:43:54 +03001165 if (WARN_ON(!cl || !cl->me_cl))
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001166 return -EINVAL;
1167
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001168 if (mei_cl_is_fixed_address(cl))
1169 return 0;
1170
Alexander Usyskind49ed642015-05-04 09:43:54 +03001171 if (mei_cl_is_single_recv_buf(cl)) {
1172 if (WARN_ON(cl->me_cl->mei_flow_ctrl_creds <= 0))
1173 return -EINVAL;
1174 cl->me_cl->mei_flow_ctrl_creds--;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001175 } else {
Alexander Usyskind49ed642015-05-04 09:43:54 +03001176 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
1177 return -EINVAL;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001178 cl->mei_flow_ctrl_creds--;
1179 }
Alexander Usyskind49ed642015-05-04 09:43:54 +03001180 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001181}
1182
Tomas Winkler9ca90502013-01-08 23:07:13 +02001183/**
Masanari Iida393b1482013-04-05 01:05:05 +09001184 * mei_cl_read_start - the start read client message function.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001185 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001186 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +03001187 * @length: number of bytes to read
Tomas Winklerbca67d62015-02-10 10:39:43 +02001188 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +02001189 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001190 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001191 */
Tomas Winklerbca67d62015-02-10 10:39:43 +02001192int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001193{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001194 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001195 struct mei_cl_cb *cb;
1196 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001197
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001198 if (WARN_ON(!cl || !cl->dev))
1199 return -ENODEV;
1200
1201 dev = cl->dev;
1202
Tomas Winklerb950ac12013-07-25 20:15:53 +03001203 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +02001204 return -ENODEV;
1205
Tomas Winklera9bed612015-02-10 10:39:46 +02001206 /* HW currently supports only one pending read */
1207 if (!list_empty(&cl->rd_pending))
Tomas Winkler9ca90502013-01-08 23:07:13 +02001208 return -EBUSY;
Tomas Winklera9bed612015-02-10 10:39:46 +02001209
Alexander Usyskind49ed642015-05-04 09:43:54 +03001210 if (!mei_me_cl_is_active(cl->me_cl)) {
1211 cl_err(dev, cl, "no such me client\n");
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001212 return -ENOTTY;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001213 }
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001214
Tomas Winkler79563db2015-01-11 00:07:16 +02001215 /* always allocate at least client max message */
Alexander Usyskind49ed642015-05-04 09:43:54 +03001216 length = max_t(size_t, length, mei_cl_mtu(cl));
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001217 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp);
1218 if (!cb)
1219 return -ENOMEM;
1220
1221 if (mei_cl_is_fixed_address(cl)) {
1222 list_add_tail(&cb->list, &cl->rd_pending);
1223 return 0;
1224 }
Tomas Winkler9ca90502013-01-08 23:07:13 +02001225
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001226 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001227 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001228 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001229 cl_err(dev, cl, "rpm: get failed %d\n", rets);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001230 goto nortpm;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001231 }
1232
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001233 if (mei_hbuf_acquire(dev)) {
Alexander Usyskin86113502014-03-31 17:59:24 +03001234 rets = mei_hbm_cl_flow_control_req(dev, cl);
1235 if (rets < 0)
Tomas Winkler04bb1392014-03-18 22:52:04 +02001236 goto out;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001237
Tomas Winklera9bed612015-02-10 10:39:46 +02001238 list_add_tail(&cb->list, &cl->rd_pending);
Tomas Winkler9ca90502013-01-08 23:07:13 +02001239 } else {
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001240 rets = 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001241 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1242 }
Chao Biaccb8842014-02-12 21:27:25 +02001243
Tomas Winkler04bb1392014-03-18 22:52:04 +02001244out:
1245 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001246 pm_runtime_mark_last_busy(dev->dev);
1247 pm_runtime_put_autosuspend(dev->dev);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001248nortpm:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001249 if (rets)
1250 mei_io_cb_free(cb);
1251
Tomas Winkler9ca90502013-01-08 23:07:13 +02001252 return rets;
1253}
1254
Tomas Winkler074b4c02013-02-06 14:06:44 +02001255/**
Tomas Winkler9d098192014-02-19 17:35:48 +02001256 * mei_cl_irq_write - write a message to device
Tomas Winkler21767542013-06-23 09:36:59 +03001257 * from the interrupt thread context
1258 *
1259 * @cl: client
1260 * @cb: callback block.
Tomas Winkler21767542013-06-23 09:36:59 +03001261 * @cmpl_list: complete list.
1262 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001263 * Return: 0, OK; otherwise error.
Tomas Winkler21767542013-06-23 09:36:59 +03001264 */
Tomas Winkler9d098192014-02-19 17:35:48 +02001265int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1266 struct mei_cl_cb *cmpl_list)
Tomas Winkler21767542013-06-23 09:36:59 +03001267{
Tomas Winkler136698e2013-09-16 23:44:44 +03001268 struct mei_device *dev;
1269 struct mei_msg_data *buf;
Tomas Winkler21767542013-06-23 09:36:59 +03001270 struct mei_msg_hdr mei_hdr;
Tomas Winkler136698e2013-09-16 23:44:44 +03001271 size_t len;
1272 u32 msg_slots;
Tomas Winkler9d098192014-02-19 17:35:48 +02001273 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001274 int rets;
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001275 bool first_chunk;
Tomas Winkler21767542013-06-23 09:36:59 +03001276
Tomas Winkler136698e2013-09-16 23:44:44 +03001277 if (WARN_ON(!cl || !cl->dev))
1278 return -ENODEV;
1279
1280 dev = cl->dev;
1281
Tomas Winkler5db75142015-02-10 10:39:42 +02001282 buf = &cb->buf;
Tomas Winkler136698e2013-09-16 23:44:44 +03001283
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001284 first_chunk = cb->buf_idx == 0;
1285
1286 rets = first_chunk ? mei_cl_flow_ctrl_creds(cl) : 1;
Tomas Winkler136698e2013-09-16 23:44:44 +03001287 if (rets < 0)
1288 return rets;
1289
1290 if (rets == 0) {
Tomas Winkler04bb1392014-03-18 22:52:04 +02001291 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler136698e2013-09-16 23:44:44 +03001292 return 0;
1293 }
1294
Tomas Winkler9d098192014-02-19 17:35:48 +02001295 slots = mei_hbuf_empty_slots(dev);
Tomas Winkler136698e2013-09-16 23:44:44 +03001296 len = buf->size - cb->buf_idx;
1297 msg_slots = mei_data2slots(len);
1298
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001299 mei_hdr.host_addr = mei_cl_host_addr(cl);
Alexander Usyskind49ed642015-05-04 09:43:54 +03001300 mei_hdr.me_addr = mei_cl_me_id(cl);
Tomas Winkler21767542013-06-23 09:36:59 +03001301 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +02001302 mei_hdr.internal = cb->internal;
Tomas Winkler21767542013-06-23 09:36:59 +03001303
Tomas Winkler9d098192014-02-19 17:35:48 +02001304 if (slots >= msg_slots) {
Tomas Winkler21767542013-06-23 09:36:59 +03001305 mei_hdr.length = len;
1306 mei_hdr.msg_complete = 1;
1307 /* Split the message only if we can write the whole host buffer */
Tomas Winkler9d098192014-02-19 17:35:48 +02001308 } else if (slots == dev->hbuf_depth) {
1309 msg_slots = slots;
1310 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winkler21767542013-06-23 09:36:59 +03001311 mei_hdr.length = len;
1312 mei_hdr.msg_complete = 0;
1313 } else {
1314 /* wait for next time the host buffer is empty */
1315 return 0;
1316 }
1317
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001318 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
Tomas Winkler5db75142015-02-10 10:39:42 +02001319 cb->buf.size, cb->buf_idx);
Tomas Winkler21767542013-06-23 09:36:59 +03001320
Tomas Winkler136698e2013-09-16 23:44:44 +03001321 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001322 if (rets) {
1323 cl->status = rets;
Tomas Winkler21767542013-06-23 09:36:59 +03001324 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001325 return rets;
Tomas Winkler21767542013-06-23 09:36:59 +03001326 }
1327
1328 cl->status = 0;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +03001329 cl->writing_state = MEI_WRITING;
Tomas Winkler21767542013-06-23 09:36:59 +03001330 cb->buf_idx += mei_hdr.length;
Tomas Winkler86601722015-02-10 10:39:40 +02001331 cb->completed = mei_hdr.msg_complete == 1;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +03001332
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001333 if (first_chunk) {
Tomas Winkler21767542013-06-23 09:36:59 +03001334 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001335 return -EIO;
Tomas Winkler21767542013-06-23 09:36:59 +03001336 }
1337
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001338 if (mei_hdr.msg_complete)
1339 list_move_tail(&cb->list, &dev->write_waiting_list.list);
1340
Tomas Winkler21767542013-06-23 09:36:59 +03001341 return 0;
1342}
1343
1344/**
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001345 * mei_cl_write - submit a write cb to mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001346 * assumes device_lock is locked
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001347 *
1348 * @cl: host client
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001349 * @cb: write callback with filled data
Alexander Usyskince231392014-09-29 16:31:50 +03001350 * @blocking: block until completed
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001351 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001352 * Return: number of bytes sent on success, <0 on failure.
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001353 */
1354int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
1355{
1356 struct mei_device *dev;
1357 struct mei_msg_data *buf;
1358 struct mei_msg_hdr mei_hdr;
Alexander Usyskin23253c32015-07-23 10:43:11 +03001359 int size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001360 int rets;
1361
1362
1363 if (WARN_ON(!cl || !cl->dev))
1364 return -ENODEV;
1365
1366 if (WARN_ON(!cb))
1367 return -EINVAL;
1368
1369 dev = cl->dev;
1370
Tomas Winkler5db75142015-02-10 10:39:42 +02001371 buf = &cb->buf;
Alexander Usyskin23253c32015-07-23 10:43:11 +03001372 size = buf->size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001373
Alexander Usyskin23253c32015-07-23 10:43:11 +03001374 cl_dbg(dev, cl, "size=%d\n", size);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001375
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001376 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001377 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001378 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001379 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1380 return rets;
1381 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001382
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001383 cb->buf_idx = 0;
1384 cl->writing_state = MEI_IDLE;
1385
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001386 mei_hdr.host_addr = mei_cl_host_addr(cl);
Alexander Usyskind49ed642015-05-04 09:43:54 +03001387 mei_hdr.me_addr = mei_cl_me_id(cl);
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001388 mei_hdr.reserved = 0;
1389 mei_hdr.msg_complete = 0;
1390 mei_hdr.internal = cb->internal;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001391
1392 rets = mei_cl_flow_ctrl_creds(cl);
1393 if (rets < 0)
1394 goto err;
1395
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001396 if (rets == 0) {
1397 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Alexander Usyskin23253c32015-07-23 10:43:11 +03001398 rets = size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001399 goto out;
1400 }
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001401 if (!mei_hbuf_acquire(dev)) {
1402 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
Alexander Usyskin23253c32015-07-23 10:43:11 +03001403 rets = size;
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001404 goto out;
1405 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001406
1407 /* Check for a maximum length */
Alexander Usyskin23253c32015-07-23 10:43:11 +03001408 if (size > mei_hbuf_max_len(dev)) {
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001409 mei_hdr.length = mei_hbuf_max_len(dev);
1410 mei_hdr.msg_complete = 0;
1411 } else {
Alexander Usyskin23253c32015-07-23 10:43:11 +03001412 mei_hdr.length = size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001413 mei_hdr.msg_complete = 1;
1414 }
1415
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001416 rets = mei_write_message(dev, &mei_hdr, buf->data);
1417 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001418 goto err;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001419
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001420 rets = mei_cl_flow_ctrl_reduce(cl);
1421 if (rets)
1422 goto err;
1423
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001424 cl->writing_state = MEI_WRITING;
1425 cb->buf_idx = mei_hdr.length;
Tomas Winkler86601722015-02-10 10:39:40 +02001426 cb->completed = mei_hdr.msg_complete == 1;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001427
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001428out:
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001429 if (mei_hdr.msg_complete)
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001430 list_add_tail(&cb->list, &dev->write_waiting_list.list);
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001431 else
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001432 list_add_tail(&cb->list, &dev->write_list.list);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001433
Alexander Usyskin23253c32015-07-23 10:43:11 +03001434 cb = NULL;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001435 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1436
1437 mutex_unlock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001438 rets = wait_event_interruptible(cl->tx_wait,
1439 cl->writing_state == MEI_WRITE_COMPLETE);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001440 mutex_lock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001441 /* wait_event_interruptible returns -ERESTARTSYS */
1442 if (rets) {
1443 if (signal_pending(current))
1444 rets = -EINTR;
1445 goto err;
1446 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001447 }
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001448
Alexander Usyskin23253c32015-07-23 10:43:11 +03001449 rets = size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001450err:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001451 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001452 pm_runtime_mark_last_busy(dev->dev);
1453 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001454
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001455 return rets;
1456}
1457
1458
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001459/**
1460 * mei_cl_complete - processes completed operation for a client
1461 *
1462 * @cl: private data of the file object.
1463 * @cb: callback block.
1464 */
1465void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1466{
Alexander Usyskina1809d32015-05-07 15:53:59 +03001467 struct mei_device *dev = cl->dev;
1468
Tomas Winkler3c666182015-05-04 09:43:52 +03001469 switch (cb->fop_type) {
1470 case MEI_FOP_WRITE:
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001471 mei_io_cb_free(cb);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001472 cl->writing_state = MEI_WRITE_COMPLETE;
Alexander Usyskina1809d32015-05-07 15:53:59 +03001473 if (waitqueue_active(&cl->tx_wait)) {
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001474 wake_up_interruptible(&cl->tx_wait);
Alexander Usyskina1809d32015-05-07 15:53:59 +03001475 } else {
1476 pm_runtime_mark_last_busy(dev->dev);
1477 pm_request_autosuspend(dev->dev);
1478 }
Tomas Winkler3c666182015-05-04 09:43:52 +03001479 break;
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001480
Tomas Winkler3c666182015-05-04 09:43:52 +03001481 case MEI_FOP_READ:
Tomas Winklera9bed612015-02-10 10:39:46 +02001482 list_add_tail(&cb->list, &cl->rd_completed);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001483 if (waitqueue_active(&cl->rx_wait))
Tomas Winkler1d9013f2015-03-27 00:27:57 +02001484 wake_up_interruptible_all(&cl->rx_wait);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001485 else
1486 mei_cl_bus_rx_event(cl);
Tomas Winkler3c666182015-05-04 09:43:52 +03001487 break;
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001488
Tomas Winkler3c666182015-05-04 09:43:52 +03001489 case MEI_FOP_CONNECT:
1490 case MEI_FOP_DISCONNECT:
1491 if (waitqueue_active(&cl->wait))
1492 wake_up(&cl->wait);
1493
1494 break;
1495 default:
1496 BUG_ON(0);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001497 }
1498}
1499
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001500
1501/**
Tomas Winkler074b4c02013-02-06 14:06:44 +02001502 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1503 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001504 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001505 */
Tomas Winkler074b4c02013-02-06 14:06:44 +02001506void mei_cl_all_disconnect(struct mei_device *dev)
1507{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001508 struct mei_cl *cl;
Tomas Winkler074b4c02013-02-06 14:06:44 +02001509
Tomas Winkler3c666182015-05-04 09:43:52 +03001510 list_for_each_entry(cl, &dev->file_list, link)
1511 mei_cl_set_disconnected(cl);
Tomas Winkler074b4c02013-02-06 14:06:44 +02001512}
1513
1514
1515/**
Tomas Winkler52908012013-07-24 16:22:57 +03001516 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
Tomas Winkler074b4c02013-02-06 14:06:44 +02001517 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001518 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001519 */
Tomas Winkler52908012013-07-24 16:22:57 +03001520void mei_cl_all_wakeup(struct mei_device *dev)
Tomas Winkler074b4c02013-02-06 14:06:44 +02001521{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001522 struct mei_cl *cl;
Tomas Winkler92db1552014-09-29 16:31:37 +03001523
Tomas Winkler31f88f52014-02-17 15:13:25 +02001524 list_for_each_entry(cl, &dev->file_list, link) {
Tomas Winkler074b4c02013-02-06 14:06:44 +02001525 if (waitqueue_active(&cl->rx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001526 cl_dbg(dev, cl, "Waking up reading client!\n");
Tomas Winkler074b4c02013-02-06 14:06:44 +02001527 wake_up_interruptible(&cl->rx_wait);
1528 }
Tomas Winkler52908012013-07-24 16:22:57 +03001529 if (waitqueue_active(&cl->tx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001530 cl_dbg(dev, cl, "Waking up writing client!\n");
Tomas Winkler52908012013-07-24 16:22:57 +03001531 wake_up_interruptible(&cl->tx_wait);
1532 }
Tomas Winkler074b4c02013-02-06 14:06:44 +02001533 }
1534}
1535
1536/**
1537 * mei_cl_all_write_clear - clear all pending writes
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001538 *
1539 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001540 */
1541void mei_cl_all_write_clear(struct mei_device *dev)
1542{
Tomas Winklercc99ecf2014-03-10 15:10:40 +02001543 mei_io_list_free(&dev->write_list, NULL);
1544 mei_io_list_free(&dev->write_waiting_list, NULL);
Tomas Winkler074b4c02013-02-06 14:06:44 +02001545}
1546
1547