blob: ce88c2199b2c40209582cf57a436fd1b001492b9 [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 ||
717 dev->pg_event == MEI_PG_EVENT_WAIT) {
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 Usyskind49ed642015-05-04 09:43:54 +0300758 mei_me_cl_put(cl->me_cl);
759 cl->me_cl = NULL;
Tomas Winkler3c666182015-05-04 09:43:52 +0300760}
761
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300762static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
763{
Alexander Usyskin1df629e2015-05-04 09:43:56 +0300764 if (!mei_me_cl_get(me_cl))
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300765 return -ENOENT;
766
Alexander Usyskin1df629e2015-05-04 09:43:56 +0300767 /* only one connection is allowed for fixed address clients */
768 if (me_cl->props.fixed_address) {
769 if (me_cl->connect_count) {
770 mei_me_cl_put(me_cl);
771 return -EBUSY;
772 }
773 }
774
775 cl->me_cl = me_cl;
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300776 cl->state = MEI_FILE_CONNECTING;
777 cl->me_cl->connect_count++;
778
779 return 0;
780}
781
Tomas Winkler3c666182015-05-04 09:43:52 +0300782/*
783 * mei_cl_send_disconnect - send disconnect request
784 *
785 * @cl: host client
786 * @cb: callback block
787 *
788 * Return: 0, OK; otherwise, error.
789 */
790static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
791{
792 struct mei_device *dev;
793 int ret;
794
795 dev = cl->dev;
796
797 ret = mei_hbm_cl_disconnect_req(dev, cl);
798 cl->status = ret;
799 if (ret) {
800 cl->state = MEI_FILE_DISCONNECT_REPLY;
801 return ret;
802 }
803
804 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
805 cl->timer_count = MEI_CONNECT_TIMEOUT;
806
807 return 0;
808}
809
810/**
811 * mei_cl_irq_disconnect - processes close related operation from
812 * interrupt thread context - send disconnect request
813 *
814 * @cl: client
815 * @cb: callback block.
816 * @cmpl_list: complete list.
817 *
818 * Return: 0, OK; otherwise, error.
819 */
820int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
821 struct mei_cl_cb *cmpl_list)
822{
823 struct mei_device *dev = cl->dev;
824 u32 msg_slots;
825 int slots;
826 int ret;
827
828 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
829 slots = mei_hbuf_empty_slots(dev);
830
831 if (slots < msg_slots)
832 return -EMSGSIZE;
833
834 ret = mei_cl_send_disconnect(cl, cb);
835 if (ret)
836 list_move_tail(&cb->list, &cmpl_list->list);
837
838 return ret;
839}
840
841
842
843/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200844 * mei_cl_disconnect - disconnect host client from the me one
Tomas Winkler9ca90502013-01-08 23:07:13 +0200845 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200846 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200847 *
848 * Locking: called under "dev->device_lock" lock
849 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300850 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200851 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200852int mei_cl_disconnect(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200853{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200854 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200855 struct mei_cl_cb *cb;
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300856 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200857
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200858 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200859 return -ENODEV;
860
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200861 dev = cl->dev;
862
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300863 cl_dbg(dev, cl, "disconnecting");
864
Tomas Winkler3c666182015-05-04 09:43:52 +0300865 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200866 return 0;
867
Alexander Usyskin1df629e2015-05-04 09:43:56 +0300868 if (mei_cl_is_fixed_address(cl)) {
869 mei_cl_set_disconnected(cl);
870 return 0;
871 }
872
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300873 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200874 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300875 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200876 cl_err(dev, cl, "rpm: get failed %d\n", rets);
877 return rets;
878 }
879
Tomas Winkler3c666182015-05-04 09:43:52 +0300880 cl->state = MEI_FILE_DISCONNECTING;
881
Tomas Winklerbca67d62015-02-10 10:39:43 +0200882 cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL);
883 rets = cb ? 0 : -ENOMEM;
884 if (rets)
Tomas Winkler3c666182015-05-04 09:43:52 +0300885 goto out;
886
887 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
888 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300889
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200890 if (mei_hbuf_acquire(dev)) {
Tomas Winkler3c666182015-05-04 09:43:52 +0300891 rets = mei_cl_send_disconnect(cl, cb);
892 if (rets) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300893 cl_err(dev, cl, "failed to disconnect.\n");
Tomas Winkler3c666182015-05-04 09:43:52 +0300894 goto out;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200895 }
Tomas Winkler9ca90502013-01-08 23:07:13 +0200896 }
Tomas Winkler3c666182015-05-04 09:43:52 +0300897
Tomas Winkler9ca90502013-01-08 23:07:13 +0200898 mutex_unlock(&dev->device_lock);
Tomas Winkler3c666182015-05-04 09:43:52 +0300899 wait_event_timeout(cl->wait, cl->state == MEI_FILE_DISCONNECT_REPLY,
900 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9ca90502013-01-08 23:07:13 +0200901 mutex_lock(&dev->device_lock);
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300902
Tomas Winkler3c666182015-05-04 09:43:52 +0300903 rets = cl->status;
904 if (cl->state != MEI_FILE_DISCONNECT_REPLY) {
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300905 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
906 rets = -ETIME;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200907 }
908
Tomas Winkler3c666182015-05-04 09:43:52 +0300909out:
910 /* we disconnect also on error */
911 mei_cl_set_disconnected(cl);
912 if (!rets)
913 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
914
Tomas Winkler04bb1392014-03-18 22:52:04 +0200915 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300916 pm_runtime_mark_last_busy(dev->dev);
917 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200918
Tomas Winkler9ca90502013-01-08 23:07:13 +0200919 mei_io_cb_free(cb);
920 return rets;
921}
922
923
924/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200925 * mei_cl_is_other_connecting - checks if other
926 * client with the same me client id is connecting
Tomas Winkler9ca90502013-01-08 23:07:13 +0200927 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200928 * @cl: private data of the file object
929 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300930 * Return: true if other client is connected, false - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200931 */
Tomas Winkler0c533572015-05-04 09:43:53 +0300932static bool mei_cl_is_other_connecting(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200933{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200934 struct mei_device *dev;
Tomas Winkler0c533572015-05-04 09:43:53 +0300935 struct mei_cl_cb *cb;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200936
937 dev = cl->dev;
938
Tomas Winkler0c533572015-05-04 09:43:53 +0300939 list_for_each_entry(cb, &dev->ctrl_rd_list.list, list) {
940 if (cb->fop_type == MEI_FOP_CONNECT &&
Alexander Usyskind49ed642015-05-04 09:43:54 +0300941 mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200942 return true;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200943 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200944
945 return false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200946}
947
948/**
Tomas Winkler0c533572015-05-04 09:43:53 +0300949 * mei_cl_send_connect - send connect request
950 *
951 * @cl: host client
952 * @cb: callback block
953 *
954 * Return: 0, OK; otherwise, error.
955 */
956static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
957{
958 struct mei_device *dev;
959 int ret;
960
961 dev = cl->dev;
962
963 ret = mei_hbm_cl_connect_req(dev, cl);
964 cl->status = ret;
965 if (ret) {
966 cl->state = MEI_FILE_DISCONNECT_REPLY;
967 return ret;
968 }
969
970 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
971 cl->timer_count = MEI_CONNECT_TIMEOUT;
972 return 0;
973}
974
975/**
976 * mei_cl_irq_connect - send connect request in irq_thread context
977 *
978 * @cl: host client
979 * @cb: callback block
980 * @cmpl_list: complete list
981 *
982 * Return: 0, OK; otherwise, error.
983 */
984int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
985 struct mei_cl_cb *cmpl_list)
986{
987 struct mei_device *dev = cl->dev;
988 u32 msg_slots;
989 int slots;
990 int rets;
991
992 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
993 slots = mei_hbuf_empty_slots(dev);
994
995 if (mei_cl_is_other_connecting(cl))
996 return 0;
997
998 if (slots < msg_slots)
999 return -EMSGSIZE;
1000
1001 rets = mei_cl_send_connect(cl, cb);
1002 if (rets)
1003 list_move_tail(&cb->list, &cmpl_list->list);
1004
1005 return rets;
1006}
1007
1008/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +02001009 * mei_cl_connect - connect host client to the me one
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001010 *
1011 * @cl: host client
Alexander Usyskind49ed642015-05-04 09:43:54 +03001012 * @me_cl: me client
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001013 * @file: pointer to file structure
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001014 *
1015 * Locking: called under "dev->device_lock" lock
1016 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001017 * Return: 0 on success, <0 on failure.
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001018 */
Alexander Usyskind49ed642015-05-04 09:43:54 +03001019int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
1020 struct file *file)
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001021{
1022 struct mei_device *dev;
1023 struct mei_cl_cb *cb;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001024 int rets;
1025
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001026 if (WARN_ON(!cl || !cl->dev || !me_cl))
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001027 return -ENODEV;
1028
1029 dev = cl->dev;
1030
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001031 rets = mei_cl_set_connecting(cl, me_cl);
1032 if (rets)
1033 return rets;
1034
1035 if (mei_cl_is_fixed_address(cl)) {
1036 cl->state = MEI_FILE_CONNECTED;
1037 return 0;
1038 }
1039
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001040 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001041 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001042 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001043 cl_err(dev, cl, "rpm: get failed %d\n", rets);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001044 goto nortpm;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001045 }
1046
Tomas Winklerbca67d62015-02-10 10:39:43 +02001047 cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file);
1048 rets = cb ? 0 : -ENOMEM;
1049 if (rets)
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001050 goto out;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001051
Tomas Winkler0c533572015-05-04 09:43:53 +03001052 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1053
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001054 /* run hbuf acquire last so we don't have to undo */
1055 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
Tomas Winkler0c533572015-05-04 09:43:53 +03001056 rets = mei_cl_send_connect(cl, cb);
1057 if (rets)
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001058 goto out;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001059 }
1060
1061 mutex_unlock(&dev->device_lock);
Tomas Winkler12f45ed2014-08-21 14:29:18 +03001062 wait_event_timeout(cl->wait,
Alexander Usyskin285e2992014-02-17 15:13:20 +02001063 (cl->state == MEI_FILE_CONNECTED ||
Tomas Winkler3c666182015-05-04 09:43:52 +03001064 cl->state == MEI_FILE_DISCONNECT_REPLY),
Alexander Usyskin285e2992014-02-17 15:13:20 +02001065 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001066 mutex_lock(&dev->device_lock);
1067
Tomas Winklerf3de9b62015-03-27 00:27:58 +02001068 if (!mei_cl_is_connected(cl)) {
Tomas Winkler0c533572015-05-04 09:43:53 +03001069 /* timeout or something went really wrong */
Alexander Usyskin285e2992014-02-17 15:13:20 +02001070 if (!cl->status)
1071 cl->status = -EFAULT;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001072 }
1073
1074 rets = cl->status;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001075out:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001076 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001077 pm_runtime_mark_last_busy(dev->dev);
1078 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001079
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001080 mei_io_cb_free(cb);
Tomas Winkler0c533572015-05-04 09:43:53 +03001081
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001082nortpm:
Tomas Winkler0c533572015-05-04 09:43:53 +03001083 if (!mei_cl_is_connected(cl))
1084 mei_cl_set_disconnected(cl);
1085
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001086 return rets;
1087}
1088
1089/**
Tomas Winkler03b8d342015-02-10 10:39:44 +02001090 * mei_cl_alloc_linked - allocate and link host client
1091 *
1092 * @dev: the device structure
1093 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
1094 *
1095 * Return: cl on success ERR_PTR on failure
1096 */
1097struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id)
1098{
1099 struct mei_cl *cl;
1100 int ret;
1101
1102 cl = mei_cl_allocate(dev);
1103 if (!cl) {
1104 ret = -ENOMEM;
1105 goto err;
1106 }
1107
1108 ret = mei_cl_link(cl, id);
1109 if (ret)
1110 goto err;
1111
1112 return cl;
1113err:
1114 kfree(cl);
1115 return ERR_PTR(ret);
1116}
1117
1118
1119
1120/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001121 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001122 *
Tomas Winkler9ca90502013-01-08 23:07:13 +02001123 * @cl: private data of the file object
1124 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001125 * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001126 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001127int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001128{
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001129 int rets;
1130
Alexander Usyskind49ed642015-05-04 09:43:54 +03001131 if (WARN_ON(!cl || !cl->me_cl))
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001132 return -EINVAL;
1133
Tomas Winkler9ca90502013-01-08 23:07:13 +02001134 if (cl->mei_flow_ctrl_creds > 0)
1135 return 1;
1136
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001137 if (mei_cl_is_fixed_address(cl)) {
1138 rets = mei_cl_read_start(cl, mei_cl_mtu(cl), NULL);
1139 if (rets && rets != -EBUSY)
1140 return rets;
1141 return 1;
1142 }
1143
Alexander Usyskind49ed642015-05-04 09:43:54 +03001144 if (mei_cl_is_single_recv_buf(cl)) {
1145 if (cl->me_cl->mei_flow_ctrl_creds > 0)
1146 return 1;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001147 }
Alexander Usyskind49ed642015-05-04 09:43:54 +03001148 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001149}
1150
1151/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001152 * mei_cl_flow_ctrl_reduce - reduces flow_control.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001153 *
Tomas Winkler9ca90502013-01-08 23:07:13 +02001154 * @cl: private data of the file object
Masanari Iida393b1482013-04-05 01:05:05 +09001155 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001156 * Return:
Tomas Winkler9ca90502013-01-08 23:07:13 +02001157 * 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +02001158 * -EINVAL when ctrl credits are <= 0
1159 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001160int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001161{
Alexander Usyskind49ed642015-05-04 09:43:54 +03001162 if (WARN_ON(!cl || !cl->me_cl))
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001163 return -EINVAL;
1164
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001165 if (mei_cl_is_fixed_address(cl))
1166 return 0;
1167
Alexander Usyskind49ed642015-05-04 09:43:54 +03001168 if (mei_cl_is_single_recv_buf(cl)) {
1169 if (WARN_ON(cl->me_cl->mei_flow_ctrl_creds <= 0))
1170 return -EINVAL;
1171 cl->me_cl->mei_flow_ctrl_creds--;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001172 } else {
Alexander Usyskind49ed642015-05-04 09:43:54 +03001173 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
1174 return -EINVAL;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001175 cl->mei_flow_ctrl_creds--;
1176 }
Alexander Usyskind49ed642015-05-04 09:43:54 +03001177 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001178}
1179
Tomas Winkler9ca90502013-01-08 23:07:13 +02001180/**
Masanari Iida393b1482013-04-05 01:05:05 +09001181 * mei_cl_read_start - the start read client message function.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001182 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001183 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +03001184 * @length: number of bytes to read
Tomas Winklerbca67d62015-02-10 10:39:43 +02001185 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +02001186 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001187 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001188 */
Tomas Winklerbca67d62015-02-10 10:39:43 +02001189int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001190{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001191 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001192 struct mei_cl_cb *cb;
1193 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001194
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001195 if (WARN_ON(!cl || !cl->dev))
1196 return -ENODEV;
1197
1198 dev = cl->dev;
1199
Tomas Winklerb950ac12013-07-25 20:15:53 +03001200 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +02001201 return -ENODEV;
1202
Tomas Winklera9bed612015-02-10 10:39:46 +02001203 /* HW currently supports only one pending read */
1204 if (!list_empty(&cl->rd_pending))
Tomas Winkler9ca90502013-01-08 23:07:13 +02001205 return -EBUSY;
Tomas Winklera9bed612015-02-10 10:39:46 +02001206
Alexander Usyskind49ed642015-05-04 09:43:54 +03001207 if (!mei_me_cl_is_active(cl->me_cl)) {
1208 cl_err(dev, cl, "no such me client\n");
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001209 return -ENOTTY;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001210 }
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001211
Tomas Winkler79563db2015-01-11 00:07:16 +02001212 /* always allocate at least client max message */
Alexander Usyskind49ed642015-05-04 09:43:54 +03001213 length = max_t(size_t, length, mei_cl_mtu(cl));
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001214 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp);
1215 if (!cb)
1216 return -ENOMEM;
1217
1218 if (mei_cl_is_fixed_address(cl)) {
1219 list_add_tail(&cb->list, &cl->rd_pending);
1220 return 0;
1221 }
Tomas Winkler9ca90502013-01-08 23:07:13 +02001222
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001223 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001224 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001225 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001226 cl_err(dev, cl, "rpm: get failed %d\n", rets);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001227 goto nortpm;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001228 }
1229
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001230 if (mei_hbuf_acquire(dev)) {
Alexander Usyskin86113502014-03-31 17:59:24 +03001231 rets = mei_hbm_cl_flow_control_req(dev, cl);
1232 if (rets < 0)
Tomas Winkler04bb1392014-03-18 22:52:04 +02001233 goto out;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001234
Tomas Winklera9bed612015-02-10 10:39:46 +02001235 list_add_tail(&cb->list, &cl->rd_pending);
Tomas Winkler9ca90502013-01-08 23:07:13 +02001236 } else {
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001237 rets = 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001238 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1239 }
Chao Biaccb8842014-02-12 21:27:25 +02001240
Tomas Winkler04bb1392014-03-18 22:52:04 +02001241out:
1242 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001243 pm_runtime_mark_last_busy(dev->dev);
1244 pm_runtime_put_autosuspend(dev->dev);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001245nortpm:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001246 if (rets)
1247 mei_io_cb_free(cb);
1248
Tomas Winkler9ca90502013-01-08 23:07:13 +02001249 return rets;
1250}
1251
Tomas Winkler074b4c02013-02-06 14:06:44 +02001252/**
Tomas Winkler9d098192014-02-19 17:35:48 +02001253 * mei_cl_irq_write - write a message to device
Tomas Winkler21767542013-06-23 09:36:59 +03001254 * from the interrupt thread context
1255 *
1256 * @cl: client
1257 * @cb: callback block.
Tomas Winkler21767542013-06-23 09:36:59 +03001258 * @cmpl_list: complete list.
1259 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001260 * Return: 0, OK; otherwise error.
Tomas Winkler21767542013-06-23 09:36:59 +03001261 */
Tomas Winkler9d098192014-02-19 17:35:48 +02001262int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1263 struct mei_cl_cb *cmpl_list)
Tomas Winkler21767542013-06-23 09:36:59 +03001264{
Tomas Winkler136698e2013-09-16 23:44:44 +03001265 struct mei_device *dev;
1266 struct mei_msg_data *buf;
Tomas Winkler21767542013-06-23 09:36:59 +03001267 struct mei_msg_hdr mei_hdr;
Tomas Winkler136698e2013-09-16 23:44:44 +03001268 size_t len;
1269 u32 msg_slots;
Tomas Winkler9d098192014-02-19 17:35:48 +02001270 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001271 int rets;
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001272 bool first_chunk;
Tomas Winkler21767542013-06-23 09:36:59 +03001273
Tomas Winkler136698e2013-09-16 23:44:44 +03001274 if (WARN_ON(!cl || !cl->dev))
1275 return -ENODEV;
1276
1277 dev = cl->dev;
1278
Tomas Winkler5db75142015-02-10 10:39:42 +02001279 buf = &cb->buf;
Tomas Winkler136698e2013-09-16 23:44:44 +03001280
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001281 first_chunk = cb->buf_idx == 0;
1282
1283 rets = first_chunk ? mei_cl_flow_ctrl_creds(cl) : 1;
Tomas Winkler136698e2013-09-16 23:44:44 +03001284 if (rets < 0)
1285 return rets;
1286
1287 if (rets == 0) {
Tomas Winkler04bb1392014-03-18 22:52:04 +02001288 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler136698e2013-09-16 23:44:44 +03001289 return 0;
1290 }
1291
Tomas Winkler9d098192014-02-19 17:35:48 +02001292 slots = mei_hbuf_empty_slots(dev);
Tomas Winkler136698e2013-09-16 23:44:44 +03001293 len = buf->size - cb->buf_idx;
1294 msg_slots = mei_data2slots(len);
1295
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001296 mei_hdr.host_addr = mei_cl_host_addr(cl);
Alexander Usyskind49ed642015-05-04 09:43:54 +03001297 mei_hdr.me_addr = mei_cl_me_id(cl);
Tomas Winkler21767542013-06-23 09:36:59 +03001298 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +02001299 mei_hdr.internal = cb->internal;
Tomas Winkler21767542013-06-23 09:36:59 +03001300
Tomas Winkler9d098192014-02-19 17:35:48 +02001301 if (slots >= msg_slots) {
Tomas Winkler21767542013-06-23 09:36:59 +03001302 mei_hdr.length = len;
1303 mei_hdr.msg_complete = 1;
1304 /* Split the message only if we can write the whole host buffer */
Tomas Winkler9d098192014-02-19 17:35:48 +02001305 } else if (slots == dev->hbuf_depth) {
1306 msg_slots = slots;
1307 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winkler21767542013-06-23 09:36:59 +03001308 mei_hdr.length = len;
1309 mei_hdr.msg_complete = 0;
1310 } else {
1311 /* wait for next time the host buffer is empty */
1312 return 0;
1313 }
1314
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001315 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
Tomas Winkler5db75142015-02-10 10:39:42 +02001316 cb->buf.size, cb->buf_idx);
Tomas Winkler21767542013-06-23 09:36:59 +03001317
Tomas Winkler136698e2013-09-16 23:44:44 +03001318 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001319 if (rets) {
1320 cl->status = rets;
Tomas Winkler21767542013-06-23 09:36:59 +03001321 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001322 return rets;
Tomas Winkler21767542013-06-23 09:36:59 +03001323 }
1324
1325 cl->status = 0;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +03001326 cl->writing_state = MEI_WRITING;
Tomas Winkler21767542013-06-23 09:36:59 +03001327 cb->buf_idx += mei_hdr.length;
Tomas Winkler86601722015-02-10 10:39:40 +02001328 cb->completed = mei_hdr.msg_complete == 1;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +03001329
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001330 if (first_chunk) {
Tomas Winkler21767542013-06-23 09:36:59 +03001331 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001332 return -EIO;
Tomas Winkler21767542013-06-23 09:36:59 +03001333 }
1334
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001335 if (mei_hdr.msg_complete)
1336 list_move_tail(&cb->list, &dev->write_waiting_list.list);
1337
Tomas Winkler21767542013-06-23 09:36:59 +03001338 return 0;
1339}
1340
1341/**
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001342 * mei_cl_write - submit a write cb to mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001343 * assumes device_lock is locked
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001344 *
1345 * @cl: host client
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001346 * @cb: write callback with filled data
Alexander Usyskince231392014-09-29 16:31:50 +03001347 * @blocking: block until completed
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001348 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001349 * Return: number of bytes sent on success, <0 on failure.
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001350 */
1351int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
1352{
1353 struct mei_device *dev;
1354 struct mei_msg_data *buf;
1355 struct mei_msg_hdr mei_hdr;
1356 int rets;
1357
1358
1359 if (WARN_ON(!cl || !cl->dev))
1360 return -ENODEV;
1361
1362 if (WARN_ON(!cb))
1363 return -EINVAL;
1364
1365 dev = cl->dev;
1366
1367
Tomas Winkler5db75142015-02-10 10:39:42 +02001368 buf = &cb->buf;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001369
Alexander Usyskin0a01e972014-09-29 16:31:47 +03001370 cl_dbg(dev, cl, "size=%d\n", buf->size);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001371
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001372 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001373 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001374 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001375 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1376 return rets;
1377 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001378
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001379 cb->buf_idx = 0;
1380 cl->writing_state = MEI_IDLE;
1381
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001382 mei_hdr.host_addr = mei_cl_host_addr(cl);
Alexander Usyskind49ed642015-05-04 09:43:54 +03001383 mei_hdr.me_addr = mei_cl_me_id(cl);
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001384 mei_hdr.reserved = 0;
1385 mei_hdr.msg_complete = 0;
1386 mei_hdr.internal = cb->internal;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001387
1388 rets = mei_cl_flow_ctrl_creds(cl);
1389 if (rets < 0)
1390 goto err;
1391
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001392 if (rets == 0) {
1393 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001394 rets = buf->size;
1395 goto out;
1396 }
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001397 if (!mei_hbuf_acquire(dev)) {
1398 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1399 rets = buf->size;
1400 goto out;
1401 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001402
1403 /* Check for a maximum length */
1404 if (buf->size > mei_hbuf_max_len(dev)) {
1405 mei_hdr.length = mei_hbuf_max_len(dev);
1406 mei_hdr.msg_complete = 0;
1407 } else {
1408 mei_hdr.length = buf->size;
1409 mei_hdr.msg_complete = 1;
1410 }
1411
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001412 rets = mei_write_message(dev, &mei_hdr, buf->data);
1413 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001414 goto err;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001415
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001416 rets = mei_cl_flow_ctrl_reduce(cl);
1417 if (rets)
1418 goto err;
1419
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001420 cl->writing_state = MEI_WRITING;
1421 cb->buf_idx = mei_hdr.length;
Tomas Winkler86601722015-02-10 10:39:40 +02001422 cb->completed = mei_hdr.msg_complete == 1;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001423
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001424out:
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001425 if (mei_hdr.msg_complete)
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001426 list_add_tail(&cb->list, &dev->write_waiting_list.list);
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001427 else
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001428 list_add_tail(&cb->list, &dev->write_list.list);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001429
1430 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1431
1432 mutex_unlock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001433 rets = wait_event_interruptible(cl->tx_wait,
1434 cl->writing_state == MEI_WRITE_COMPLETE);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001435 mutex_lock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001436 /* wait_event_interruptible returns -ERESTARTSYS */
1437 if (rets) {
1438 if (signal_pending(current))
1439 rets = -EINTR;
1440 goto err;
1441 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001442 }
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001443
1444 rets = buf->size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001445err:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001446 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001447 pm_runtime_mark_last_busy(dev->dev);
1448 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001449
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001450 return rets;
1451}
1452
1453
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001454/**
1455 * mei_cl_complete - processes completed operation for a client
1456 *
1457 * @cl: private data of the file object.
1458 * @cb: callback block.
1459 */
1460void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1461{
Tomas Winkler3c666182015-05-04 09:43:52 +03001462 switch (cb->fop_type) {
1463 case MEI_FOP_WRITE:
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001464 mei_io_cb_free(cb);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001465 cl->writing_state = MEI_WRITE_COMPLETE;
1466 if (waitqueue_active(&cl->tx_wait))
1467 wake_up_interruptible(&cl->tx_wait);
Tomas Winkler3c666182015-05-04 09:43:52 +03001468 break;
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001469
Tomas Winkler3c666182015-05-04 09:43:52 +03001470 case MEI_FOP_READ:
Tomas Winklera9bed612015-02-10 10:39:46 +02001471 list_add_tail(&cb->list, &cl->rd_completed);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001472 if (waitqueue_active(&cl->rx_wait))
Tomas Winkler1d9013f2015-03-27 00:27:57 +02001473 wake_up_interruptible_all(&cl->rx_wait);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001474 else
1475 mei_cl_bus_rx_event(cl);
Tomas Winkler3c666182015-05-04 09:43:52 +03001476 break;
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001477
Tomas Winkler3c666182015-05-04 09:43:52 +03001478 case MEI_FOP_CONNECT:
1479 case MEI_FOP_DISCONNECT:
1480 if (waitqueue_active(&cl->wait))
1481 wake_up(&cl->wait);
1482
1483 break;
1484 default:
1485 BUG_ON(0);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001486 }
1487}
1488
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001489
1490/**
Tomas Winkler074b4c02013-02-06 14:06:44 +02001491 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1492 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001493 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001494 */
Tomas Winkler074b4c02013-02-06 14:06:44 +02001495void mei_cl_all_disconnect(struct mei_device *dev)
1496{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001497 struct mei_cl *cl;
Tomas Winkler074b4c02013-02-06 14:06:44 +02001498
Tomas Winkler3c666182015-05-04 09:43:52 +03001499 list_for_each_entry(cl, &dev->file_list, link)
1500 mei_cl_set_disconnected(cl);
Tomas Winkler074b4c02013-02-06 14:06:44 +02001501}
1502
1503
1504/**
Tomas Winkler52908012013-07-24 16:22:57 +03001505 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
Tomas Winkler074b4c02013-02-06 14:06:44 +02001506 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001507 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001508 */
Tomas Winkler52908012013-07-24 16:22:57 +03001509void mei_cl_all_wakeup(struct mei_device *dev)
Tomas Winkler074b4c02013-02-06 14:06:44 +02001510{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001511 struct mei_cl *cl;
Tomas Winkler92db1552014-09-29 16:31:37 +03001512
Tomas Winkler31f88f52014-02-17 15:13:25 +02001513 list_for_each_entry(cl, &dev->file_list, link) {
Tomas Winkler074b4c02013-02-06 14:06:44 +02001514 if (waitqueue_active(&cl->rx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001515 cl_dbg(dev, cl, "Waking up reading client!\n");
Tomas Winkler074b4c02013-02-06 14:06:44 +02001516 wake_up_interruptible(&cl->rx_wait);
1517 }
Tomas Winkler52908012013-07-24 16:22:57 +03001518 if (waitqueue_active(&cl->tx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001519 cl_dbg(dev, cl, "Waking up writing client!\n");
Tomas Winkler52908012013-07-24 16:22:57 +03001520 wake_up_interruptible(&cl->tx_wait);
1521 }
Tomas Winkler074b4c02013-02-06 14:06:44 +02001522 }
1523}
1524
1525/**
1526 * mei_cl_all_write_clear - clear all pending writes
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001527 *
1528 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001529 */
1530void mei_cl_all_write_clear(struct mei_device *dev)
1531{
Tomas Winklercc99ecf2014-03-10 15:10:40 +02001532 mei_io_list_free(&dev->write_list, NULL);
1533 mei_io_list_free(&dev->write_waiting_list, NULL);
Tomas Winkler074b4c02013-02-06 14:06:44 +02001534}
1535
1536