blob: 96f4e59c32a5436b1665e51fde4cb33d1dbb6069 [file] [log] [blame]
Tomas Winkler9fff0422019-03-12 00:10:41 +02001// SPDX-License-Identifier: GPL-2.0
Tomas Winkler9ca90502013-01-08 23:07:13 +02002/*
Tomas Winkler4b40b222020-07-23 17:59:25 +03003 * Copyright (c) 2003-2020, Intel Corporation. All rights reserved.
Tomas Winkler9ca90502013-01-08 23:07:13 +02004 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler9ca90502013-01-08 23:07:13 +02005 */
6
Ingo Molnar174cd4b2017-02-02 19:15:33 +01007#include <linux/sched/signal.h>
Tomas Winkler9ca90502013-01-08 23:07:13 +02008#include <linux/wait.h>
9#include <linux/delay.h>
Tomas Winkler1f180352014-09-29 16:31:46 +030010#include <linux/slab.h>
Tomas Winkler04bb1392014-03-18 22:52:04 +020011#include <linux/pm_runtime.h>
Alexander Usyskin369aea82021-02-06 16:43:24 +020012#include <linux/dma-mapping.h>
Tomas Winkler9ca90502013-01-08 23:07:13 +020013
14#include <linux/mei.h>
15
16#include "mei_dev.h"
17#include "hbm.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020018#include "client.h"
19
20/**
Tomas Winkler79563db2015-01-11 00:07:16 +020021 * mei_me_cl_init - initialize me client
22 *
23 * @me_cl: me client
24 */
25void mei_me_cl_init(struct mei_me_client *me_cl)
26{
27 INIT_LIST_HEAD(&me_cl->list);
28 kref_init(&me_cl->refcnt);
29}
30
31/**
32 * mei_me_cl_get - increases me client refcount
33 *
34 * @me_cl: me client
35 *
36 * Locking: called under "dev->device_lock" lock
37 *
38 * Return: me client or NULL
39 */
40struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
41{
Tomas Winklerb7d88512015-02-10 10:39:31 +020042 if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
43 return me_cl;
Tomas Winkler79563db2015-01-11 00:07:16 +020044
Tomas Winklerb7d88512015-02-10 10:39:31 +020045 return NULL;
Tomas Winkler79563db2015-01-11 00:07:16 +020046}
47
48/**
Tomas Winklerb7d88512015-02-10 10:39:31 +020049 * mei_me_cl_release - free me client
Tomas Winkler79563db2015-01-11 00:07:16 +020050 *
51 * Locking: called under "dev->device_lock" lock
52 *
53 * @ref: me_client refcount
54 */
55static void mei_me_cl_release(struct kref *ref)
56{
57 struct mei_me_client *me_cl =
58 container_of(ref, struct mei_me_client, refcnt);
Tomas Winklerb7d88512015-02-10 10:39:31 +020059
Tomas Winkler79563db2015-01-11 00:07:16 +020060 kfree(me_cl);
61}
Tomas Winklerb7d88512015-02-10 10:39:31 +020062
Tomas Winkler79563db2015-01-11 00:07:16 +020063/**
64 * mei_me_cl_put - decrease me client refcount and free client if necessary
65 *
66 * Locking: called under "dev->device_lock" lock
67 *
68 * @me_cl: me client
69 */
70void mei_me_cl_put(struct mei_me_client *me_cl)
71{
72 if (me_cl)
73 kref_put(&me_cl->refcnt, mei_me_cl_release);
74}
75
76/**
Alexander Usyskind49ed642015-05-04 09:43:54 +030077 * __mei_me_cl_del - delete me client from the list and decrease
Tomas Winklerb7d88512015-02-10 10:39:31 +020078 * reference counter
79 *
80 * @dev: mei device
81 * @me_cl: me client
82 *
83 * Locking: dev->me_clients_rwsem
84 */
85static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
86{
87 if (!me_cl)
88 return;
89
Alexander Usyskind49ed642015-05-04 09:43:54 +030090 list_del_init(&me_cl->list);
Tomas Winklerb7d88512015-02-10 10:39:31 +020091 mei_me_cl_put(me_cl);
92}
93
94/**
Alexander Usyskind49ed642015-05-04 09:43:54 +030095 * mei_me_cl_del - delete me client from the list and decrease
96 * reference counter
97 *
98 * @dev: mei device
99 * @me_cl: me client
100 */
101void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
102{
103 down_write(&dev->me_clients_rwsem);
104 __mei_me_cl_del(dev, me_cl);
105 up_write(&dev->me_clients_rwsem);
106}
107
108/**
Tomas Winklerb7d88512015-02-10 10:39:31 +0200109 * mei_me_cl_add - add me client to the list
110 *
111 * @dev: mei device
112 * @me_cl: me client
113 */
114void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
115{
116 down_write(&dev->me_clients_rwsem);
117 list_add(&me_cl->list, &dev->me_clients);
118 up_write(&dev->me_clients_rwsem);
119}
120
121/**
122 * __mei_me_cl_by_uuid - locate me client by uuid
123 * increases ref count
124 *
125 * @dev: mei device
126 * @uuid: me client uuid
127 *
128 * Return: me client or NULL if not found
129 *
130 * Locking: dev->me_clients_rwsem
131 */
132static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
133 const uuid_le *uuid)
134{
135 struct mei_me_client *me_cl;
136 const uuid_le *pn;
137
138 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
139
140 list_for_each_entry(me_cl, &dev->me_clients, list) {
141 pn = &me_cl->props.protocol_name;
142 if (uuid_le_cmp(*uuid, *pn) == 0)
143 return mei_me_cl_get(me_cl);
144 }
145
146 return NULL;
147}
148
149/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300150 * mei_me_cl_by_uuid - locate me client by uuid
Tomas Winkler79563db2015-01-11 00:07:16 +0200151 * increases ref count
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200152 *
153 * @dev: mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300154 * @uuid: me client uuid
Alexander Usyskina27a76d2014-02-17 15:13:22 +0200155 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300156 * Return: me client or NULL if not found
Tomas Winklerb7d88512015-02-10 10:39:31 +0200157 *
158 * Locking: dev->me_clients_rwsem
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200159 */
Tomas Winklerb7d88512015-02-10 10:39:31 +0200160struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
Tomas Winklerd3208322014-08-24 12:08:55 +0300161 const uuid_le *uuid)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200162{
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300163 struct mei_me_client *me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200164
Tomas Winklerb7d88512015-02-10 10:39:31 +0200165 down_read(&dev->me_clients_rwsem);
166 me_cl = __mei_me_cl_by_uuid(dev, uuid);
167 up_read(&dev->me_clients_rwsem);
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200168
Tomas Winklerb7d88512015-02-10 10:39:31 +0200169 return me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200170}
171
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200172/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300173 * mei_me_cl_by_id - locate me client by client id
Tomas Winkler79563db2015-01-11 00:07:16 +0200174 * increases ref count
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200175 *
176 * @dev: the device structure
177 * @client_id: me client id
178 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300179 * Return: me client or NULL if not found
Tomas Winklerb7d88512015-02-10 10:39:31 +0200180 *
181 * Locking: dev->me_clients_rwsem
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200182 */
Tomas Winklerd3208322014-08-24 12:08:55 +0300183struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200184{
Alexander Usyskina27a76d2014-02-17 15:13:22 +0200185
Tomas Winklerb7d88512015-02-10 10:39:31 +0200186 struct mei_me_client *__me_cl, *me_cl = NULL;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200187
Tomas Winklerb7d88512015-02-10 10:39:31 +0200188 down_read(&dev->me_clients_rwsem);
189 list_for_each_entry(__me_cl, &dev->me_clients, list) {
190 if (__me_cl->client_id == client_id) {
191 me_cl = mei_me_cl_get(__me_cl);
192 break;
193 }
194 }
195 up_read(&dev->me_clients_rwsem);
196
197 return me_cl;
198}
199
200/**
201 * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
202 * increases ref count
203 *
204 * @dev: the device structure
205 * @uuid: me client uuid
206 * @client_id: me client id
207 *
208 * Return: me client or null if not found
209 *
210 * Locking: dev->me_clients_rwsem
211 */
212static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
213 const uuid_le *uuid, u8 client_id)
214{
215 struct mei_me_client *me_cl;
216 const uuid_le *pn;
217
218 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
219
220 list_for_each_entry(me_cl, &dev->me_clients, list) {
221 pn = &me_cl->props.protocol_name;
222 if (uuid_le_cmp(*uuid, *pn) == 0 &&
223 me_cl->client_id == client_id)
Tomas Winkler79563db2015-01-11 00:07:16 +0200224 return mei_me_cl_get(me_cl);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200225 }
Tomas Winkler79563db2015-01-11 00:07:16 +0200226
Tomas Winklerd3208322014-08-24 12:08:55 +0300227 return NULL;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200228}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200229
Tomas Winklerb7d88512015-02-10 10:39:31 +0200230
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300231/**
232 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
Tomas Winkler79563db2015-01-11 00:07:16 +0200233 * increases ref count
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300234 *
235 * @dev: the device structure
236 * @uuid: me client uuid
237 * @client_id: me client id
238 *
Tomas Winklerb7d88512015-02-10 10:39:31 +0200239 * Return: me client or null if not found
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300240 */
Tomas Winklerd880f322014-08-21 14:29:15 +0300241struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
242 const uuid_le *uuid, u8 client_id)
243{
244 struct mei_me_client *me_cl;
245
Tomas Winklerb7d88512015-02-10 10:39:31 +0200246 down_read(&dev->me_clients_rwsem);
247 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
248 up_read(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200249
Tomas Winklerb7d88512015-02-10 10:39:31 +0200250 return me_cl;
Tomas Winklerd880f322014-08-21 14:29:15 +0300251}
252
Tomas Winkler25ca6472014-08-21 14:29:14 +0300253/**
Tomas Winkler79563db2015-01-11 00:07:16 +0200254 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
Tomas Winkler25ca6472014-08-21 14:29:14 +0300255 *
256 * @dev: the device structure
257 * @uuid: me client uuid
Tomas Winkler79563db2015-01-11 00:07:16 +0200258 *
259 * Locking: called under "dev->device_lock" lock
Tomas Winkler25ca6472014-08-21 14:29:14 +0300260 */
Tomas Winkler79563db2015-01-11 00:07:16 +0200261void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
Tomas Winkler25ca6472014-08-21 14:29:14 +0300262{
Tomas Winklerb7d88512015-02-10 10:39:31 +0200263 struct mei_me_client *me_cl;
Tomas Winkler25ca6472014-08-21 14:29:14 +0300264
Tomas Winkler79563db2015-01-11 00:07:16 +0200265 dev_dbg(dev->dev, "remove %pUl\n", uuid);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200266
267 down_write(&dev->me_clients_rwsem);
268 me_cl = __mei_me_cl_by_uuid(dev, uuid);
269 __mei_me_cl_del(dev, me_cl);
Alexander Usyskinfc9c03c2020-05-13 01:31:40 +0300270 mei_me_cl_put(me_cl);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200271 up_write(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200272}
273
274/**
275 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
276 *
277 * @dev: the device structure
278 * @uuid: me client uuid
279 * @id: me client id
280 *
281 * Locking: called under "dev->device_lock" lock
282 */
283void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
284{
Tomas Winklerb7d88512015-02-10 10:39:31 +0200285 struct mei_me_client *me_cl;
Tomas Winkler79563db2015-01-11 00:07:16 +0200286
287 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200288
289 down_write(&dev->me_clients_rwsem);
290 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
291 __mei_me_cl_del(dev, me_cl);
Alexander Usyskinfc9c03c2020-05-13 01:31:40 +0300292 mei_me_cl_put(me_cl);
Tomas Winklerb7d88512015-02-10 10:39:31 +0200293 up_write(&dev->me_clients_rwsem);
Tomas Winkler25ca6472014-08-21 14:29:14 +0300294}
295
Tomas Winkler79563db2015-01-11 00:07:16 +0200296/**
297 * mei_me_cl_rm_all - remove all me clients
298 *
299 * @dev: the device structure
300 *
301 * Locking: called under "dev->device_lock" lock
302 */
303void mei_me_cl_rm_all(struct mei_device *dev)
304{
305 struct mei_me_client *me_cl, *next;
306
Tomas Winklerb7d88512015-02-10 10:39:31 +0200307 down_write(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200308 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
Tomas Winklerb7d88512015-02-10 10:39:31 +0200309 __mei_me_cl_del(dev, me_cl);
310 up_write(&dev->me_clients_rwsem);
Tomas Winkler79563db2015-01-11 00:07:16 +0200311}
312
Tomas Winkler9ca90502013-01-08 23:07:13 +0200313/**
Tomas Winkler9ca90502013-01-08 23:07:13 +0200314 * mei_io_cb_free - free mei_cb_private related memory
315 *
316 * @cb: mei callback struct
317 */
318void mei_io_cb_free(struct mei_cl_cb *cb)
319{
320 if (cb == NULL)
321 return;
322
Tomas Winkler928fa662015-02-10 10:39:45 +0200323 list_del(&cb->list);
Tomas Winkler5db75142015-02-10 10:39:42 +0200324 kfree(cb->buf.data);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200325 kfree(cb);
326}
327
328/**
Tamar Mashiah09f8c332021-06-21 22:37:55 +0300329 * mei_tx_cb_enqueue - queue tx callback
Alexander Usyskinaf336ca2018-02-25 20:07:05 +0200330 *
331 * Locking: called under "dev->device_lock" lock
332 *
333 * @cb: mei callback struct
334 * @head: an instance of list to queue on
335 */
336static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb,
337 struct list_head *head)
338{
339 list_add_tail(&cb->list, head);
340 cb->cl->tx_cb_queued++;
341}
342
343/**
344 * mei_tx_cb_dequeue - dequeue tx callback
345 *
346 * Locking: called under "dev->device_lock" lock
347 *
348 * @cb: mei callback struct to dequeue and free
349 */
350static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb)
351{
352 if (!WARN_ON(cb->cl->tx_cb_queued == 0))
353 cb->cl->tx_cb_queued--;
354
355 mei_io_cb_free(cb);
356}
357
358/**
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +0300359 * mei_cl_set_read_by_fp - set pending_read flag to vtag struct for given fp
360 *
361 * Locking: called under "dev->device_lock" lock
362 *
363 * @cl: mei client
364 * @fp: pointer to file structure
365 */
366static void mei_cl_set_read_by_fp(const struct mei_cl *cl,
367 const struct file *fp)
368{
369 struct mei_cl_vtag *cl_vtag;
370
371 list_for_each_entry(cl_vtag, &cl->vtag_map, list) {
372 if (cl_vtag->fp == fp) {
373 cl_vtag->pending_read = true;
374 return;
375 }
376 }
377}
378
379/**
Tomas Winkler9ca90502013-01-08 23:07:13 +0200380 * mei_io_cb_init - allocate and initialize io callback
381 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300382 * @cl: mei client
Tomas Winklerbca67d62015-02-10 10:39:43 +0200383 * @type: operation type
Masanari Iida393b1482013-04-05 01:05:05 +0900384 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200385 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300386 * Return: mei_cl_cb pointer or NULL;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200387 */
Tomas Winkler3030dc02016-07-26 01:06:05 +0300388static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl,
389 enum mei_cb_file_ops type,
390 const struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200391{
392 struct mei_cl_cb *cb;
393
Tomas Winkler4b40b222020-07-23 17:59:25 +0300394 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200395 if (!cb)
396 return NULL;
397
Tomas Winkler928fa662015-02-10 10:39:45 +0200398 INIT_LIST_HEAD(&cb->list);
Tomas Winkler62e8e6a2016-02-07 23:35:24 +0200399 cb->fp = fp;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200400 cb->cl = cl;
401 cb->buf_idx = 0;
Tomas Winklerbca67d62015-02-10 10:39:43 +0200402 cb->fop_type = type;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300403 cb->vtag = 0;
404
Tomas Winkler9ca90502013-01-08 23:07:13 +0200405 return cb;
406}
407
408/**
Alexander Usyskinaf336ca2018-02-25 20:07:05 +0200409 * mei_io_list_flush_cl - removes cbs belonging to the cl.
Tomas Winkler928fa662015-02-10 10:39:45 +0200410 *
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200411 * @head: an instance of our list structure
Alexander Usyskinaf336ca2018-02-25 20:07:05 +0200412 * @cl: host client
Tomas Winkler928fa662015-02-10 10:39:45 +0200413 */
Alexander Usyskinaf336ca2018-02-25 20:07:05 +0200414static void mei_io_list_flush_cl(struct list_head *head,
415 const struct mei_cl *cl)
Tomas Winkler928fa662015-02-10 10:39:45 +0200416{
417 struct mei_cl_cb *cb, *next;
418
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200419 list_for_each_entry_safe(cb, next, head, list) {
Alexander Usyskincee4c4d62019-01-30 10:12:26 +0200420 if (cl == cb->cl) {
Tomas Winkler928fa662015-02-10 10:39:45 +0200421 list_del_init(&cb->list);
Alexander Usyskincee4c4d62019-01-30 10:12:26 +0200422 if (cb->fop_type == MEI_FOP_READ)
423 mei_io_cb_free(cb);
424 }
Tomas Winkler928fa662015-02-10 10:39:45 +0200425 }
426}
427
428/**
Alexander Usyskinaf336ca2018-02-25 20:07:05 +0200429 * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
Tomas Winkler928fa662015-02-10 10:39:45 +0200430 *
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200431 * @head: An instance of our list structure
Tomas Winkler928fa662015-02-10 10:39:45 +0200432 * @cl: host client
Alexander Usyskin15ffa992020-08-18 14:51:42 +0300433 * @fp: file pointer (matching cb file object), may be NULL
Tomas Winkler928fa662015-02-10 10:39:45 +0200434 */
Alexander Usyskinaf336ca2018-02-25 20:07:05 +0200435static void mei_io_tx_list_free_cl(struct list_head *head,
Alexander Usyskin15ffa992020-08-18 14:51:42 +0300436 const struct mei_cl *cl,
437 const struct file *fp)
Tomas Winkler928fa662015-02-10 10:39:45 +0200438{
Alexander Usyskinaf336ca2018-02-25 20:07:05 +0200439 struct mei_cl_cb *cb, *next;
Tomas Winkler928fa662015-02-10 10:39:45 +0200440
Alexander Usyskinaf336ca2018-02-25 20:07:05 +0200441 list_for_each_entry_safe(cb, next, head, list) {
Alexander Usyskin15ffa992020-08-18 14:51:42 +0300442 if (cl == cb->cl && (!fp || fp == cb->fp))
Alexander Usyskinaf336ca2018-02-25 20:07:05 +0200443 mei_tx_cb_dequeue(cb);
444 }
Tomas Winklerf0461922017-01-27 16:32:46 +0200445}
446
447/**
448 * mei_io_list_free_fp - free cb from a list that matches file pointer
449 *
450 * @head: io list
451 * @fp: file pointer (matching cb file object), may be NULL
452 */
Alexander Usyskin394a77d2017-03-20 15:04:03 +0200453static void mei_io_list_free_fp(struct list_head *head, const struct file *fp)
Tomas Winklerf0461922017-01-27 16:32:46 +0200454{
455 struct mei_cl_cb *cb, *next;
456
457 list_for_each_entry_safe(cb, next, head, list)
458 if (!fp || fp == cb->fp)
459 mei_io_cb_free(cb);
Tomas Winkler928fa662015-02-10 10:39:45 +0200460}
461
462/**
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +0300463 * mei_cl_free_pending - free pending cb
464 *
465 * @cl: host client
466 */
467static void mei_cl_free_pending(struct mei_cl *cl)
468{
469 struct mei_cl_cb *cb;
470
471 cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
472 mei_io_cb_free(cb);
473}
474
475/**
Tomas Winklerbca67d62015-02-10 10:39:43 +0200476 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
477 *
478 * @cl: host client
479 * @length: size of the buffer
Alexander Usyskin967b2742016-11-11 03:00:09 +0200480 * @fop_type: operation type
Tomas Winklerbca67d62015-02-10 10:39:43 +0200481 * @fp: associated file pointer (might be NULL)
482 *
483 * Return: cb on success and NULL on failure
484 */
485struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
Tomas Winkler3030dc02016-07-26 01:06:05 +0300486 enum mei_cb_file_ops fop_type,
Tomas Winklerf23e2cc2016-02-07 23:35:23 +0200487 const struct file *fp)
Tomas Winklerbca67d62015-02-10 10:39:43 +0200488{
489 struct mei_cl_cb *cb;
490
Tomas Winkler3030dc02016-07-26 01:06:05 +0300491 cb = mei_io_cb_init(cl, fop_type, fp);
Tomas Winklerbca67d62015-02-10 10:39:43 +0200492 if (!cb)
493 return NULL;
494
Alexander Usyskinaab3b1a2016-05-24 16:03:41 -0400495 if (length == 0)
496 return cb;
497
Tomas Winkler63163212018-11-22 13:11:39 +0200498 cb->buf.data = kmalloc(roundup(length, MEI_SLOT_SIZE), GFP_KERNEL);
Alexander Usyskinaab3b1a2016-05-24 16:03:41 -0400499 if (!cb->buf.data) {
Tomas Winklerbca67d62015-02-10 10:39:43 +0200500 mei_io_cb_free(cb);
501 return NULL;
502 }
Alexander Usyskinaab3b1a2016-05-24 16:03:41 -0400503 cb->buf.size = length;
Tomas Winklerbca67d62015-02-10 10:39:43 +0200504
505 return cb;
506}
507
508/**
Tomas Winkler3030dc02016-07-26 01:06:05 +0300509 * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
510 * and enqueuing of the control commands cb
511 *
512 * @cl: host client
513 * @length: size of the buffer
Alexander Usyskin967b2742016-11-11 03:00:09 +0200514 * @fop_type: operation type
Tomas Winkler3030dc02016-07-26 01:06:05 +0300515 * @fp: associated file pointer (might be NULL)
516 *
517 * Return: cb on success and NULL on failure
518 * Locking: called under "dev->device_lock" lock
519 */
520struct mei_cl_cb *mei_cl_enqueue_ctrl_wr_cb(struct mei_cl *cl, size_t length,
521 enum mei_cb_file_ops fop_type,
522 const struct file *fp)
523{
524 struct mei_cl_cb *cb;
525
526 /* for RX always allocate at least client's mtu */
527 if (length)
528 length = max_t(size_t, length, mei_cl_mtu(cl));
529
530 cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
531 if (!cb)
532 return NULL;
533
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200534 list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
Tomas Winkler3030dc02016-07-26 01:06:05 +0300535 return cb;
536}
537
538/**
Tomas Winklera9bed612015-02-10 10:39:46 +0200539 * mei_cl_read_cb - find this cl's callback in the read list
540 * for a specific file
541 *
542 * @cl: host client
543 * @fp: file pointer (matching cb file object), may be NULL
544 *
545 * Return: cb on success, NULL if cb is not found
546 */
Alexander Usyskind1376f32020-08-18 14:51:40 +0300547struct mei_cl_cb *mei_cl_read_cb(struct mei_cl *cl, const struct file *fp)
Tomas Winklera9bed612015-02-10 10:39:46 +0200548{
549 struct mei_cl_cb *cb;
Alexander Usyskind1376f32020-08-18 14:51:40 +0300550 struct mei_cl_cb *ret_cb = NULL;
Tomas Winklera9bed612015-02-10 10:39:46 +0200551
Alexander Usyskind1376f32020-08-18 14:51:40 +0300552 spin_lock(&cl->rd_completed_lock);
Tomas Winklera9bed612015-02-10 10:39:46 +0200553 list_for_each_entry(cb, &cl->rd_completed, list)
Alexander Usyskind1376f32020-08-18 14:51:40 +0300554 if (!fp || fp == cb->fp) {
555 ret_cb = cb;
556 break;
557 }
558 spin_unlock(&cl->rd_completed_lock);
559 return ret_cb;
Tomas Winklera9bed612015-02-10 10:39:46 +0200560}
561
562/**
Tomas Winkler9ca90502013-01-08 23:07:13 +0200563 * mei_cl_flush_queues - flushes queue lists belonging to cl.
564 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200565 * @cl: host client
Tomas Winklera9bed612015-02-10 10:39:46 +0200566 * @fp: file pointer (matching cb file object), may be NULL
Alexander Usyskince231392014-09-29 16:31:50 +0300567 *
568 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200569 */
Tomas Winklera9bed612015-02-10 10:39:46 +0200570int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200571{
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300572 struct mei_device *dev;
573
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200574 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200575 return -EINVAL;
576
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300577 dev = cl->dev;
578
579 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
Alexander Usyskin15ffa992020-08-18 14:51:42 +0300580 mei_io_tx_list_free_cl(&cl->dev->write_list, cl, fp);
581 mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl, fp);
582 /* free pending and control cb only in final flush */
583 if (!fp) {
584 mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl);
585 mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl);
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +0300586 mei_cl_free_pending(cl);
Alexander Usyskin15ffa992020-08-18 14:51:42 +0300587 }
Alexander Usyskind1376f32020-08-18 14:51:40 +0300588 spin_lock(&cl->rd_completed_lock);
Tomas Winklerf0461922017-01-27 16:32:46 +0200589 mei_io_list_free_fp(&cl->rd_completed, fp);
Alexander Usyskind1376f32020-08-18 14:51:40 +0300590 spin_unlock(&cl->rd_completed_lock);
Tomas Winklera9bed612015-02-10 10:39:46 +0200591
Tomas Winkler9ca90502013-01-08 23:07:13 +0200592 return 0;
593}
594
Tomas Winkler9ca90502013-01-08 23:07:13 +0200595/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200596 * mei_cl_init - initializes cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200597 *
598 * @cl: host client to be initialized
599 * @dev: mei device
600 */
Alexander Usyskin394a77d2017-03-20 15:04:03 +0200601static void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200602{
Tomas Winkler4b40b222020-07-23 17:59:25 +0300603 memset(cl, 0, sizeof(*cl));
Tomas Winkler9ca90502013-01-08 23:07:13 +0200604 init_waitqueue_head(&cl->wait);
605 init_waitqueue_head(&cl->rx_wait);
606 init_waitqueue_head(&cl->tx_wait);
Tomas Winklerb38a3622015-07-26 09:54:19 +0300607 init_waitqueue_head(&cl->ev_wait);
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +0300608 INIT_LIST_HEAD(&cl->vtag_map);
Alexander Usyskind1376f32020-08-18 14:51:40 +0300609 spin_lock_init(&cl->rd_completed_lock);
Tomas Winklera9bed612015-02-10 10:39:46 +0200610 INIT_LIST_HEAD(&cl->rd_completed);
611 INIT_LIST_HEAD(&cl->rd_pending);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200612 INIT_LIST_HEAD(&cl->link);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200613 cl->writing_state = MEI_IDLE;
Alexander Usyskinbd47b522016-11-16 22:51:27 +0200614 cl->state = MEI_FILE_UNINITIALIZED;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200615 cl->dev = dev;
616}
617
618/**
619 * mei_cl_allocate - allocates cl structure and sets it up.
620 *
621 * @dev: mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300622 * Return: The allocated file or NULL on failure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200623 */
624struct mei_cl *mei_cl_allocate(struct mei_device *dev)
625{
626 struct mei_cl *cl;
627
Tomas Winkler4b40b222020-07-23 17:59:25 +0300628 cl = kmalloc(sizeof(*cl), GFP_KERNEL);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200629 if (!cl)
630 return NULL;
631
632 mei_cl_init(cl, dev);
633
634 return cl;
635}
636
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200637/**
Alexander Usyskin3908be62015-02-10 10:39:35 +0200638 * mei_cl_link - allocate host id in the host map
Tomas Winkler9ca90502013-01-08 23:07:13 +0200639 *
Alexander Usyskin3908be62015-02-10 10:39:35 +0200640 * @cl: host client
Masanari Iida393b1482013-04-05 01:05:05 +0900641 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300642 * Return: 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200643 * -EINVAL on incorrect values
Tomas Winkler03b8d342015-02-10 10:39:44 +0200644 * -EMFILE if open count exceeded.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200645 */
Alexander Usyskin7851e002016-02-07 23:35:40 +0200646int mei_cl_link(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200647{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200648 struct mei_device *dev;
Alexander Usyskin7851e002016-02-07 23:35:40 +0200649 int id;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200650
Tomas Winkler781d0d82013-01-08 23:07:22 +0200651 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200652 return -EINVAL;
653
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200654 dev = cl->dev;
655
Alexander Usyskin7851e002016-02-07 23:35:40 +0200656 id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
Tomas Winkler781d0d82013-01-08 23:07:22 +0200657 if (id >= MEI_CLIENTS_MAX) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300658 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
Tomas Winklere036cc52013-09-16 23:44:46 +0300659 return -EMFILE;
660 }
661
Alexander Usyskin394a77d2017-03-20 15:04:03 +0200662 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300663 dev_err(dev->dev, "open_handle_count exceeded %d",
Tomas Winklere036cc52013-09-16 23:44:46 +0300664 MEI_MAX_OPEN_HANDLE_COUNT);
665 return -EMFILE;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200666 }
667
Tomas Winkler781d0d82013-01-08 23:07:22 +0200668 dev->open_handle_count++;
669
670 cl->host_client_id = id;
671 list_add_tail(&cl->link, &dev->file_list);
672
673 set_bit(id, dev->host_clients_map);
674
675 cl->state = MEI_FILE_INITIALIZING;
676
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300677 cl_dbg(dev, cl, "link cl\n");
Tomas Winkler781d0d82013-01-08 23:07:22 +0200678 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200679}
Tomas Winkler781d0d82013-01-08 23:07:22 +0200680
Tomas Winkler9ca90502013-01-08 23:07:13 +0200681/**
Alexander Usyskind49ed642015-05-04 09:43:54 +0300682 * mei_cl_unlink - remove host client from the list
Tomas Winkler9ca90502013-01-08 23:07:13 +0200683 *
Masanari Iida393b1482013-04-05 01:05:05 +0900684 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +0300685 *
686 * Return: always 0
Tomas Winkler9ca90502013-01-08 23:07:13 +0200687 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200688int mei_cl_unlink(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200689{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200690 struct mei_device *dev;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200691
Tomas Winkler781d0d82013-01-08 23:07:22 +0200692 /* don't shout on error exit path */
693 if (!cl)
694 return 0;
695
Alexander Usyskin394a77d2017-03-20 15:04:03 +0200696 if (WARN_ON(!cl->dev))
Tomas Winkler8e9a4a92013-01-10 17:32:14 +0200697 return 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200698
699 dev = cl->dev;
700
Tomas Winklera14c44d2013-09-16 23:44:45 +0300701 cl_dbg(dev, cl, "unlink client");
702
Tomas Winkler22f96a02013-09-16 23:44:47 +0300703 if (dev->open_handle_count > 0)
704 dev->open_handle_count--;
705
706 /* never clear the 0 bit */
707 if (cl->host_client_id)
708 clear_bit(cl->host_client_id, dev->host_clients_map);
709
710 list_del_init(&cl->link);
711
Alexander Usyskinbd47b522016-11-16 22:51:27 +0200712 cl->state = MEI_FILE_UNINITIALIZED;
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200713 cl->writing_state = MEI_IDLE;
714
715 WARN_ON(!list_empty(&cl->rd_completed) ||
716 !list_empty(&cl->rd_pending) ||
717 !list_empty(&cl->link));
Tomas Winkler22f96a02013-09-16 23:44:47 +0300718
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200719 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200720}
721
Alexander Usyskin025fb792016-02-07 23:35:43 +0200722void mei_host_client_init(struct mei_device *dev)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200723{
Alexander Usyskin43b8a7e2019-04-22 09:51:07 +0300724 mei_set_devstate(dev, MEI_DEV_ENABLED);
Tomas Winkler6adb8ef2014-01-12 00:36:10 +0200725 dev->reset_count = 0;
Tomas Winkler04bb1392014-03-18 22:52:04 +0200726
Alexander Usyskin025fb792016-02-07 23:35:43 +0200727 schedule_work(&dev->bus_rescan_work);
Tomas Winkler60095952015-07-23 15:08:47 +0300728
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300729 pm_runtime_mark_last_busy(dev->dev);
730 dev_dbg(dev->dev, "rpm: autosuspend\n");
Alexander Usyskind5f8e162016-11-24 13:34:02 +0200731 pm_request_autosuspend(dev->dev);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200732}
733
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200734/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300735 * mei_hbuf_acquire - try to acquire host buffer
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200736 *
737 * @dev: the device structure
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300738 * Return: true if host buffer was acquired
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200739 */
740bool mei_hbuf_acquire(struct mei_device *dev)
741{
Tomas Winkler04bb1392014-03-18 22:52:04 +0200742 if (mei_pg_state(dev) == MEI_PG_ON ||
Alexander Usyskin3dc196e2015-06-13 08:51:17 +0300743 mei_pg_in_transition(dev)) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300744 dev_dbg(dev->dev, "device is in pg\n");
Tomas Winkler04bb1392014-03-18 22:52:04 +0200745 return false;
746 }
747
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200748 if (!dev->hbuf_is_ready) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300749 dev_dbg(dev->dev, "hbuf is not ready\n");
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200750 return false;
751 }
752
753 dev->hbuf_is_ready = false;
754
755 return true;
756}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200757
758/**
Alexander Usyskina4307fe2016-02-07 23:35:35 +0200759 * mei_cl_wake_all - wake up readers, writers and event waiters so
760 * they can be interrupted
761 *
762 * @cl: host client
763 */
764static void mei_cl_wake_all(struct mei_cl *cl)
765{
766 struct mei_device *dev = cl->dev;
767
768 /* synchronized under device mutex */
769 if (waitqueue_active(&cl->rx_wait)) {
770 cl_dbg(dev, cl, "Waking up reading client!\n");
771 wake_up_interruptible(&cl->rx_wait);
772 }
773 /* synchronized under device mutex */
774 if (waitqueue_active(&cl->tx_wait)) {
775 cl_dbg(dev, cl, "Waking up writing client!\n");
776 wake_up_interruptible(&cl->tx_wait);
777 }
778 /* synchronized under device mutex */
779 if (waitqueue_active(&cl->ev_wait)) {
780 cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
781 wake_up_interruptible(&cl->ev_wait);
782 }
Alexander Usyskin7ff4bdd2016-04-20 11:03:55 -0400783 /* synchronized under device mutex */
784 if (waitqueue_active(&cl->wait)) {
785 cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
Alexander Usyskin69f18042016-05-09 00:07:46 -0400786 wake_up(&cl->wait);
Alexander Usyskin7ff4bdd2016-04-20 11:03:55 -0400787 }
Alexander Usyskina4307fe2016-02-07 23:35:35 +0200788}
789
790/**
Tomas Winkler3c666182015-05-04 09:43:52 +0300791 * mei_cl_set_disconnected - set disconnected state and clear
792 * associated states and resources
793 *
794 * @cl: host client
795 */
Alexander Usyskin669c2562017-01-20 02:17:17 +0200796static void mei_cl_set_disconnected(struct mei_cl *cl)
Tomas Winkler3c666182015-05-04 09:43:52 +0300797{
798 struct mei_device *dev = cl->dev;
799
800 if (cl->state == MEI_FILE_DISCONNECTED ||
Alexander Usyskinbd47b522016-11-16 22:51:27 +0200801 cl->state <= MEI_FILE_INITIALIZING)
Tomas Winkler3c666182015-05-04 09:43:52 +0300802 return;
803
804 cl->state = MEI_FILE_DISCONNECTED;
Alexander Usyskin15ffa992020-08-18 14:51:42 +0300805 mei_io_tx_list_free_cl(&dev->write_list, cl, NULL);
806 mei_io_tx_list_free_cl(&dev->write_waiting_list, cl, NULL);
Tomas Winklerf0461922017-01-27 16:32:46 +0200807 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
808 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
Alexander Usyskina4307fe2016-02-07 23:35:35 +0200809 mei_cl_wake_all(cl);
Alexander Usyskin46978ad2016-07-26 01:06:03 +0300810 cl->rx_flow_ctrl_creds = 0;
Tomas Winkler4034b812016-07-26 01:06:04 +0300811 cl->tx_flow_ctrl_creds = 0;
Tomas Winkler3c666182015-05-04 09:43:52 +0300812 cl->timer_count = 0;
Alexander Usyskind49ed642015-05-04 09:43:54 +0300813
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300814 if (!cl->me_cl)
815 return;
816
817 if (!WARN_ON(cl->me_cl->connect_count == 0))
818 cl->me_cl->connect_count--;
819
Alexander Usyskinc241e9b2015-06-13 21:18:40 +0300820 if (cl->me_cl->connect_count == 0)
Tomas Winkler4034b812016-07-26 01:06:04 +0300821 cl->me_cl->tx_flow_ctrl_creds = 0;
Alexander Usyskinc241e9b2015-06-13 21:18:40 +0300822
Alexander Usyskind49ed642015-05-04 09:43:54 +0300823 mei_me_cl_put(cl->me_cl);
824 cl->me_cl = NULL;
Tomas Winkler3c666182015-05-04 09:43:52 +0300825}
826
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300827static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
828{
Alexander Usyskin1df629e2015-05-04 09:43:56 +0300829 if (!mei_me_cl_get(me_cl))
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300830 return -ENOENT;
831
Alexander Usyskin1df629e2015-05-04 09:43:56 +0300832 /* only one connection is allowed for fixed address clients */
833 if (me_cl->props.fixed_address) {
834 if (me_cl->connect_count) {
835 mei_me_cl_put(me_cl);
836 return -EBUSY;
837 }
838 }
839
840 cl->me_cl = me_cl;
Alexander Usyskina03d77f62015-05-04 09:43:55 +0300841 cl->state = MEI_FILE_CONNECTING;
842 cl->me_cl->connect_count++;
843
844 return 0;
845}
846
Tomas Winkler3c666182015-05-04 09:43:52 +0300847/*
848 * mei_cl_send_disconnect - send disconnect request
849 *
850 * @cl: host client
851 * @cb: callback block
852 *
853 * Return: 0, OK; otherwise, error.
854 */
855static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
856{
857 struct mei_device *dev;
858 int ret;
859
860 dev = cl->dev;
861
862 ret = mei_hbm_cl_disconnect_req(dev, cl);
863 cl->status = ret;
864 if (ret) {
865 cl->state = MEI_FILE_DISCONNECT_REPLY;
866 return ret;
867 }
868
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200869 list_move_tail(&cb->list, &dev->ctrl_rd_list);
Tomas Winkler3c666182015-05-04 09:43:52 +0300870 cl->timer_count = MEI_CONNECT_TIMEOUT;
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300871 mei_schedule_stall_timer(dev);
Tomas Winkler3c666182015-05-04 09:43:52 +0300872
873 return 0;
874}
875
876/**
877 * mei_cl_irq_disconnect - processes close related operation from
878 * interrupt thread context - send disconnect request
879 *
880 * @cl: client
881 * @cb: callback block.
882 * @cmpl_list: complete list.
883 *
884 * Return: 0, OK; otherwise, error.
885 */
886int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200887 struct list_head *cmpl_list)
Tomas Winkler3c666182015-05-04 09:43:52 +0300888{
889 struct mei_device *dev = cl->dev;
890 u32 msg_slots;
891 int slots;
892 int ret;
893
Tomas Winkler98e70862018-07-31 09:35:33 +0300894 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
Tomas Winkler3c666182015-05-04 09:43:52 +0300895 slots = mei_hbuf_empty_slots(dev);
Tomas Winklerde877432018-07-12 17:10:08 +0300896 if (slots < 0)
897 return -EOVERFLOW;
Tomas Winkler3c666182015-05-04 09:43:52 +0300898
Tomas Winklerde877432018-07-12 17:10:08 +0300899 if ((u32)slots < msg_slots)
Tomas Winkler3c666182015-05-04 09:43:52 +0300900 return -EMSGSIZE;
901
902 ret = mei_cl_send_disconnect(cl, cb);
903 if (ret)
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200904 list_move_tail(&cb->list, cmpl_list);
Tomas Winkler3c666182015-05-04 09:43:52 +0300905
906 return ret;
907}
908
Tomas Winkler3c666182015-05-04 09:43:52 +0300909/**
Alexander Usyskin18901352015-07-23 21:37:13 +0300910 * __mei_cl_disconnect - disconnect host client from the me one
911 * internal function runtime pm has to be already acquired
Tomas Winkler9ca90502013-01-08 23:07:13 +0200912 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200913 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200914 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300915 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200916 */
Alexander Usyskin18901352015-07-23 21:37:13 +0300917static int __mei_cl_disconnect(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200918{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200919 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200920 struct mei_cl_cb *cb;
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300921 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200922
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200923 dev = cl->dev;
924
Tomas Winkler3c666182015-05-04 09:43:52 +0300925 cl->state = MEI_FILE_DISCONNECTING;
926
Tomas Winkler3030dc02016-07-26 01:06:05 +0300927 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
928 if (!cb) {
929 rets = -ENOMEM;
Tomas Winkler3c666182015-05-04 09:43:52 +0300930 goto out;
Tomas Winkler3030dc02016-07-26 01:06:05 +0300931 }
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300932
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200933 if (mei_hbuf_acquire(dev)) {
Tomas Winkler3c666182015-05-04 09:43:52 +0300934 rets = mei_cl_send_disconnect(cl, cb);
935 if (rets) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300936 cl_err(dev, cl, "failed to disconnect.\n");
Tomas Winkler3c666182015-05-04 09:43:52 +0300937 goto out;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200938 }
Tomas Winkler9ca90502013-01-08 23:07:13 +0200939 }
Tomas Winkler3c666182015-05-04 09:43:52 +0300940
Tomas Winkler9ca90502013-01-08 23:07:13 +0200941 mutex_unlock(&dev->device_lock);
Alexander Usyskin7ff4bdd2016-04-20 11:03:55 -0400942 wait_event_timeout(cl->wait,
943 cl->state == MEI_FILE_DISCONNECT_REPLY ||
944 cl->state == MEI_FILE_DISCONNECTED,
Tomas Winkler3c666182015-05-04 09:43:52 +0300945 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9ca90502013-01-08 23:07:13 +0200946 mutex_lock(&dev->device_lock);
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300947
Tomas Winkler3c666182015-05-04 09:43:52 +0300948 rets = cl->status;
Alexander Usyskin7ff4bdd2016-04-20 11:03:55 -0400949 if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
950 cl->state != MEI_FILE_DISCONNECTED) {
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300951 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
952 rets = -ETIME;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200953 }
954
Tomas Winkler3c666182015-05-04 09:43:52 +0300955out:
956 /* we disconnect also on error */
957 mei_cl_set_disconnected(cl);
958 if (!rets)
959 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
960
Alexander Usyskin18901352015-07-23 21:37:13 +0300961 mei_io_cb_free(cb);
962 return rets;
963}
964
965/**
966 * mei_cl_disconnect - disconnect host client from the me one
967 *
968 * @cl: host client
969 *
970 * Locking: called under "dev->device_lock" lock
971 *
972 * Return: 0 on success, <0 on failure.
973 */
974int mei_cl_disconnect(struct mei_cl *cl)
975{
976 struct mei_device *dev;
977 int rets;
978
979 if (WARN_ON(!cl || !cl->dev))
980 return -ENODEV;
981
982 dev = cl->dev;
983
984 cl_dbg(dev, cl, "disconnecting");
985
986 if (!mei_cl_is_connected(cl))
987 return 0;
988
989 if (mei_cl_is_fixed_address(cl)) {
990 mei_cl_set_disconnected(cl);
991 return 0;
992 }
993
Alexander Usyskin36edb142021-02-06 16:43:21 +0200994 if (dev->dev_state == MEI_DEV_POWERING_DOWN ||
995 dev->dev_state == MEI_DEV_POWER_DOWN) {
Tomas Winkler7ae079a2018-02-14 14:03:29 +0200996 cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n");
997 mei_cl_set_disconnected(cl);
998 return 0;
999 }
1000
Alexander Usyskin18901352015-07-23 21:37:13 +03001001 rets = pm_runtime_get(dev->dev);
1002 if (rets < 0 && rets != -EINPROGRESS) {
1003 pm_runtime_put_noidle(dev->dev);
1004 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1005 return rets;
1006 }
1007
1008 rets = __mei_cl_disconnect(cl);
1009
Tomas Winkler04bb1392014-03-18 22:52:04 +02001010 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001011 pm_runtime_mark_last_busy(dev->dev);
1012 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001013
Tomas Winkler9ca90502013-01-08 23:07:13 +02001014 return rets;
1015}
1016
1017
1018/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001019 * mei_cl_is_other_connecting - checks if other
1020 * client with the same me client id is connecting
Tomas Winkler9ca90502013-01-08 23:07:13 +02001021 *
Tomas Winkler9ca90502013-01-08 23:07:13 +02001022 * @cl: private data of the file object
1023 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001024 * Return: true if other client is connected, false - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001025 */
Tomas Winkler0c533572015-05-04 09:43:53 +03001026static bool mei_cl_is_other_connecting(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001027{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001028 struct mei_device *dev;
Tomas Winkler0c533572015-05-04 09:43:53 +03001029 struct mei_cl_cb *cb;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001030
1031 dev = cl->dev;
1032
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001033 list_for_each_entry(cb, &dev->ctrl_rd_list, list) {
Tomas Winkler0c533572015-05-04 09:43:53 +03001034 if (cb->fop_type == MEI_FOP_CONNECT &&
Alexander Usyskind49ed642015-05-04 09:43:54 +03001035 mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001036 return true;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001037 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001038
1039 return false;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001040}
1041
1042/**
Tomas Winkler0c533572015-05-04 09:43:53 +03001043 * mei_cl_send_connect - send connect request
1044 *
1045 * @cl: host client
1046 * @cb: callback block
1047 *
1048 * Return: 0, OK; otherwise, error.
1049 */
1050static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
1051{
1052 struct mei_device *dev;
1053 int ret;
1054
1055 dev = cl->dev;
1056
1057 ret = mei_hbm_cl_connect_req(dev, cl);
1058 cl->status = ret;
1059 if (ret) {
1060 cl->state = MEI_FILE_DISCONNECT_REPLY;
1061 return ret;
1062 }
1063
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001064 list_move_tail(&cb->list, &dev->ctrl_rd_list);
Tomas Winkler0c533572015-05-04 09:43:53 +03001065 cl->timer_count = MEI_CONNECT_TIMEOUT;
Alexander Usyskin1892fc22016-09-25 13:25:31 +03001066 mei_schedule_stall_timer(dev);
Tomas Winkler0c533572015-05-04 09:43:53 +03001067 return 0;
1068}
1069
1070/**
1071 * mei_cl_irq_connect - send connect request in irq_thread context
1072 *
1073 * @cl: host client
1074 * @cb: callback block
1075 * @cmpl_list: complete list
1076 *
1077 * Return: 0, OK; otherwise, error.
1078 */
1079int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001080 struct list_head *cmpl_list)
Tomas Winkler0c533572015-05-04 09:43:53 +03001081{
1082 struct mei_device *dev = cl->dev;
1083 u32 msg_slots;
1084 int slots;
1085 int rets;
1086
Tomas Winkler0c533572015-05-04 09:43:53 +03001087 if (mei_cl_is_other_connecting(cl))
1088 return 0;
1089
Tomas Winkler98e70862018-07-31 09:35:33 +03001090 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
Tomas Winklerde877432018-07-12 17:10:08 +03001091 slots = mei_hbuf_empty_slots(dev);
1092 if (slots < 0)
1093 return -EOVERFLOW;
1094
1095 if ((u32)slots < msg_slots)
Tomas Winkler0c533572015-05-04 09:43:53 +03001096 return -EMSGSIZE;
1097
1098 rets = mei_cl_send_connect(cl, cb);
1099 if (rets)
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001100 list_move_tail(&cb->list, cmpl_list);
Tomas Winkler0c533572015-05-04 09:43:53 +03001101
1102 return rets;
1103}
1104
1105/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +02001106 * mei_cl_connect - connect host client to the me one
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001107 *
1108 * @cl: host client
Alexander Usyskind49ed642015-05-04 09:43:54 +03001109 * @me_cl: me client
Tomas Winkler3030dc02016-07-26 01:06:05 +03001110 * @fp: pointer to file structure
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001111 *
1112 * Locking: called under "dev->device_lock" lock
1113 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001114 * Return: 0 on success, <0 on failure.
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001115 */
Alexander Usyskind49ed642015-05-04 09:43:54 +03001116int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
Tomas Winkler3030dc02016-07-26 01:06:05 +03001117 const struct file *fp)
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001118{
1119 struct mei_device *dev;
1120 struct mei_cl_cb *cb;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001121 int rets;
1122
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001123 if (WARN_ON(!cl || !cl->dev || !me_cl))
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001124 return -ENODEV;
1125
1126 dev = cl->dev;
1127
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001128 rets = mei_cl_set_connecting(cl, me_cl);
1129 if (rets)
Alexander Usyskin5d882462017-01-27 16:32:39 +02001130 goto nortpm;
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001131
1132 if (mei_cl_is_fixed_address(cl)) {
1133 cl->state = MEI_FILE_CONNECTED;
Alexander Usyskin5d882462017-01-27 16:32:39 +02001134 rets = 0;
1135 goto nortpm;
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001136 }
1137
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001138 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001139 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001140 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001141 cl_err(dev, cl, "rpm: get failed %d\n", rets);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001142 goto nortpm;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001143 }
1144
Tomas Winkler3030dc02016-07-26 01:06:05 +03001145 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
1146 if (!cb) {
1147 rets = -ENOMEM;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001148 goto out;
Tomas Winkler3030dc02016-07-26 01:06:05 +03001149 }
Tomas Winkler0c533572015-05-04 09:43:53 +03001150
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001151 /* run hbuf acquire last so we don't have to undo */
1152 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
Tomas Winkler0c533572015-05-04 09:43:53 +03001153 rets = mei_cl_send_connect(cl, cb);
1154 if (rets)
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001155 goto out;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001156 }
1157
1158 mutex_unlock(&dev->device_lock);
Tomas Winkler12f45ed2014-08-21 14:29:18 +03001159 wait_event_timeout(cl->wait,
Alexander Usyskin285e2992014-02-17 15:13:20 +02001160 (cl->state == MEI_FILE_CONNECTED ||
Alexander Usyskin7ff4bdd2016-04-20 11:03:55 -04001161 cl->state == MEI_FILE_DISCONNECTED ||
Alexander Usyskin18901352015-07-23 21:37:13 +03001162 cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
Tomas Winkler3c666182015-05-04 09:43:52 +03001163 cl->state == MEI_FILE_DISCONNECT_REPLY),
Alexander Usyskin285e2992014-02-17 15:13:20 +02001164 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001165 mutex_lock(&dev->device_lock);
1166
Tomas Winklerf3de9b62015-03-27 00:27:58 +02001167 if (!mei_cl_is_connected(cl)) {
Alexander Usyskin18901352015-07-23 21:37:13 +03001168 if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
Tomas Winklerf0461922017-01-27 16:32:46 +02001169 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
1170 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
Alexander Usyskin18901352015-07-23 21:37:13 +03001171 /* ignore disconnect return valuue;
1172 * in case of failure reset will be invoked
1173 */
1174 __mei_cl_disconnect(cl);
1175 rets = -EFAULT;
1176 goto out;
1177 }
1178
Tomas Winkler0c533572015-05-04 09:43:53 +03001179 /* timeout or something went really wrong */
Alexander Usyskin285e2992014-02-17 15:13:20 +02001180 if (!cl->status)
1181 cl->status = -EFAULT;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001182 }
1183
1184 rets = cl->status;
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001185out:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001186 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001187 pm_runtime_mark_last_busy(dev->dev);
1188 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001189
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001190 mei_io_cb_free(cb);
Tomas Winkler0c533572015-05-04 09:43:53 +03001191
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001192nortpm:
Tomas Winkler0c533572015-05-04 09:43:53 +03001193 if (!mei_cl_is_connected(cl))
1194 mei_cl_set_disconnected(cl);
1195
Tomas Winkler9f81abda2013-01-08 23:07:15 +02001196 return rets;
1197}
1198
1199/**
Tomas Winkler03b8d342015-02-10 10:39:44 +02001200 * mei_cl_alloc_linked - allocate and link host client
1201 *
1202 * @dev: the device structure
Tomas Winkler03b8d342015-02-10 10:39:44 +02001203 *
1204 * Return: cl on success ERR_PTR on failure
1205 */
Alexander Usyskin7851e002016-02-07 23:35:40 +02001206struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
Tomas Winkler03b8d342015-02-10 10:39:44 +02001207{
1208 struct mei_cl *cl;
1209 int ret;
1210
1211 cl = mei_cl_allocate(dev);
1212 if (!cl) {
1213 ret = -ENOMEM;
1214 goto err;
1215 }
1216
Alexander Usyskin7851e002016-02-07 23:35:40 +02001217 ret = mei_cl_link(cl);
Tomas Winkler03b8d342015-02-10 10:39:44 +02001218 if (ret)
1219 goto err;
1220
1221 return cl;
1222err:
1223 kfree(cl);
1224 return ERR_PTR(ret);
1225}
1226
Tomas Winkler03b8d342015-02-10 10:39:44 +02001227/**
Tomas Winkler4034b812016-07-26 01:06:04 +03001228 * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001229 *
Alexander Usyskin06ee5362016-02-07 23:35:37 +02001230 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +02001231 *
Tomas Winkler4034b812016-07-26 01:06:04 +03001232 * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001233 */
Tomas Winkler4034b812016-07-26 01:06:04 +03001234static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001235{
Alexander Usyskind49ed642015-05-04 09:43:54 +03001236 if (WARN_ON(!cl || !cl->me_cl))
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001237 return -EINVAL;
1238
Tomas Winkler4034b812016-07-26 01:06:04 +03001239 if (cl->tx_flow_ctrl_creds > 0)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001240 return 1;
1241
Alexander Usyskina808c802016-06-16 17:58:58 +03001242 if (mei_cl_is_fixed_address(cl))
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001243 return 1;
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001244
Alexander Usyskind49ed642015-05-04 09:43:54 +03001245 if (mei_cl_is_single_recv_buf(cl)) {
Tomas Winkler4034b812016-07-26 01:06:04 +03001246 if (cl->me_cl->tx_flow_ctrl_creds > 0)
Alexander Usyskind49ed642015-05-04 09:43:54 +03001247 return 1;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001248 }
Alexander Usyskind49ed642015-05-04 09:43:54 +03001249 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001250}
1251
1252/**
Tomas Winkler4034b812016-07-26 01:06:04 +03001253 * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
1254 * for a client
Tomas Winkler9ca90502013-01-08 23:07:13 +02001255 *
Tomas Winkler4034b812016-07-26 01:06:04 +03001256 * @cl: host client
Masanari Iida393b1482013-04-05 01:05:05 +09001257 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001258 * Return:
Tomas Winkler9ca90502013-01-08 23:07:13 +02001259 * 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +02001260 * -EINVAL when ctrl credits are <= 0
1261 */
Tomas Winkler4034b812016-07-26 01:06:04 +03001262static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001263{
Alexander Usyskind49ed642015-05-04 09:43:54 +03001264 if (WARN_ON(!cl || !cl->me_cl))
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001265 return -EINVAL;
1266
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001267 if (mei_cl_is_fixed_address(cl))
1268 return 0;
1269
Alexander Usyskind49ed642015-05-04 09:43:54 +03001270 if (mei_cl_is_single_recv_buf(cl)) {
Tomas Winkler4034b812016-07-26 01:06:04 +03001271 if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
Alexander Usyskind49ed642015-05-04 09:43:54 +03001272 return -EINVAL;
Tomas Winkler4034b812016-07-26 01:06:04 +03001273 cl->me_cl->tx_flow_ctrl_creds--;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001274 } else {
Tomas Winkler4034b812016-07-26 01:06:04 +03001275 if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
Alexander Usyskind49ed642015-05-04 09:43:54 +03001276 return -EINVAL;
Tomas Winkler4034b812016-07-26 01:06:04 +03001277 cl->tx_flow_ctrl_creds--;
Alexander Usyskin12d00662014-02-17 15:13:23 +02001278 }
Alexander Usyskind49ed642015-05-04 09:43:54 +03001279 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001280}
1281
Tomas Winkler9ca90502013-01-08 23:07:13 +02001282/**
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +03001283 * mei_cl_vtag_alloc - allocate and fill the vtag structure
1284 *
1285 * @fp: pointer to file structure
1286 * @vtag: vm tag
1287 *
1288 * Return:
1289 * * Pointer to allocated struct - on success
1290 * * ERR_PTR(-ENOMEM) on memory allocation failure
1291 */
1292struct mei_cl_vtag *mei_cl_vtag_alloc(struct file *fp, u8 vtag)
1293{
1294 struct mei_cl_vtag *cl_vtag;
1295
1296 cl_vtag = kzalloc(sizeof(*cl_vtag), GFP_KERNEL);
1297 if (!cl_vtag)
1298 return ERR_PTR(-ENOMEM);
1299
1300 INIT_LIST_HEAD(&cl_vtag->list);
1301 cl_vtag->vtag = vtag;
1302 cl_vtag->fp = fp;
1303
1304 return cl_vtag;
1305}
1306
1307/**
1308 * mei_cl_fp_by_vtag - obtain the file pointer by vtag
1309 *
1310 * @cl: host client
Alexander Usyskin85261c12020-11-16 14:56:11 +02001311 * @vtag: virtual tag
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +03001312 *
1313 * Return:
1314 * * A file pointer - on success
1315 * * ERR_PTR(-ENOENT) if vtag is not found in the client vtag list
1316 */
1317const struct file *mei_cl_fp_by_vtag(const struct mei_cl *cl, u8 vtag)
1318{
1319 struct mei_cl_vtag *vtag_l;
1320
1321 list_for_each_entry(vtag_l, &cl->vtag_map, list)
Alexander Usyskin85261c12020-11-16 14:56:11 +02001322 /* The client on bus has one fixed fp */
1323 if ((cl->cldev && mei_cldev_enabled(cl->cldev)) ||
1324 vtag_l->vtag == vtag)
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +03001325 return vtag_l->fp;
1326
1327 return ERR_PTR(-ENOENT);
1328}
1329
1330/**
1331 * mei_cl_reset_read_by_vtag - reset pending_read flag by given vtag
1332 *
1333 * @cl: host client
1334 * @vtag: vm tag
1335 */
1336static void mei_cl_reset_read_by_vtag(const struct mei_cl *cl, u8 vtag)
1337{
1338 struct mei_cl_vtag *vtag_l;
1339
1340 list_for_each_entry(vtag_l, &cl->vtag_map, list) {
1341 if (vtag_l->vtag == vtag) {
1342 vtag_l->pending_read = false;
1343 break;
1344 }
1345 }
1346}
1347
1348/**
1349 * mei_cl_read_vtag_add_fc - add flow control for next pending reader
1350 * in the vtag list
1351 *
1352 * @cl: host client
1353 */
1354static void mei_cl_read_vtag_add_fc(struct mei_cl *cl)
1355{
1356 struct mei_cl_vtag *cl_vtag;
1357
1358 list_for_each_entry(cl_vtag, &cl->vtag_map, list) {
1359 if (cl_vtag->pending_read) {
1360 if (mei_cl_enqueue_ctrl_wr_cb(cl,
1361 mei_cl_mtu(cl),
1362 MEI_FOP_READ,
1363 cl_vtag->fp))
1364 cl->rx_flow_ctrl_creds++;
1365 break;
1366 }
1367 }
1368}
1369
1370/**
1371 * mei_cl_vt_support_check - check if client support vtags
1372 *
1373 * @cl: host client
1374 *
1375 * Return:
1376 * * 0 - supported, or not connected at all
1377 * * -EOPNOTSUPP - vtags are not supported by client
1378 */
1379int mei_cl_vt_support_check(const struct mei_cl *cl)
1380{
1381 struct mei_device *dev = cl->dev;
1382
1383 if (!dev->hbm_f_vt_supported)
1384 return -EOPNOTSUPP;
1385
1386 if (!cl->me_cl)
1387 return 0;
1388
1389 return cl->me_cl->props.vt_supported ? 0 : -EOPNOTSUPP;
1390}
1391
1392/**
Alexander Usyskind1376f32020-08-18 14:51:40 +03001393 * mei_cl_add_rd_completed - add read completed callback to list with lock
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +03001394 * and vtag check
Alexander Usyskind1376f32020-08-18 14:51:40 +03001395 *
1396 * @cl: host client
1397 * @cb: callback block
1398 *
1399 */
1400void mei_cl_add_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
1401{
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +03001402 const struct file *fp;
1403
1404 if (!mei_cl_vt_support_check(cl)) {
1405 fp = mei_cl_fp_by_vtag(cl, cb->vtag);
1406 if (IS_ERR(fp)) {
1407 /* client already disconnected, discarding */
1408 mei_io_cb_free(cb);
1409 return;
1410 }
1411 cb->fp = fp;
1412 mei_cl_reset_read_by_vtag(cl, cb->vtag);
1413 mei_cl_read_vtag_add_fc(cl);
1414 }
1415
Alexander Usyskind1376f32020-08-18 14:51:40 +03001416 spin_lock(&cl->rd_completed_lock);
1417 list_add_tail(&cb->list, &cl->rd_completed);
1418 spin_unlock(&cl->rd_completed_lock);
1419}
1420
1421/**
1422 * mei_cl_del_rd_completed - free read completed callback with lock
1423 *
1424 * @cl: host client
1425 * @cb: callback block
1426 *
1427 */
1428void mei_cl_del_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
1429{
1430 spin_lock(&cl->rd_completed_lock);
1431 mei_io_cb_free(cb);
1432 spin_unlock(&cl->rd_completed_lock);
1433}
1434
1435/**
Tomas Winkler51678cc2015-07-26 09:54:18 +03001436 * mei_cl_notify_fop2req - convert fop to proper request
1437 *
1438 * @fop: client notification start response command
1439 *
1440 * Return: MEI_HBM_NOTIFICATION_START/STOP
1441 */
1442u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
1443{
1444 if (fop == MEI_FOP_NOTIFY_START)
1445 return MEI_HBM_NOTIFICATION_START;
1446 else
1447 return MEI_HBM_NOTIFICATION_STOP;
1448}
1449
1450/**
1451 * mei_cl_notify_req2fop - convert notification request top file operation type
1452 *
1453 * @req: hbm notification request type
1454 *
1455 * Return: MEI_FOP_NOTIFY_START/STOP
1456 */
1457enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
1458{
1459 if (req == MEI_HBM_NOTIFICATION_START)
1460 return MEI_FOP_NOTIFY_START;
1461 else
1462 return MEI_FOP_NOTIFY_STOP;
1463}
1464
1465/**
1466 * mei_cl_irq_notify - send notification request in irq_thread context
1467 *
1468 * @cl: client
1469 * @cb: callback block.
1470 * @cmpl_list: complete list.
1471 *
1472 * Return: 0 on such and error otherwise.
1473 */
1474int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001475 struct list_head *cmpl_list)
Tomas Winkler51678cc2015-07-26 09:54:18 +03001476{
1477 struct mei_device *dev = cl->dev;
1478 u32 msg_slots;
1479 int slots;
1480 int ret;
1481 bool request;
1482
Tomas Winkler98e70862018-07-31 09:35:33 +03001483 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
Tomas Winkler51678cc2015-07-26 09:54:18 +03001484 slots = mei_hbuf_empty_slots(dev);
Tomas Winklerde877432018-07-12 17:10:08 +03001485 if (slots < 0)
1486 return -EOVERFLOW;
Tomas Winkler51678cc2015-07-26 09:54:18 +03001487
Tomas Winklerde877432018-07-12 17:10:08 +03001488 if ((u32)slots < msg_slots)
Tomas Winkler51678cc2015-07-26 09:54:18 +03001489 return -EMSGSIZE;
1490
1491 request = mei_cl_notify_fop2req(cb->fop_type);
1492 ret = mei_hbm_cl_notify_req(dev, cl, request);
1493 if (ret) {
1494 cl->status = ret;
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001495 list_move_tail(&cb->list, cmpl_list);
Tomas Winkler51678cc2015-07-26 09:54:18 +03001496 return ret;
1497 }
1498
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001499 list_move_tail(&cb->list, &dev->ctrl_rd_list);
Tomas Winkler51678cc2015-07-26 09:54:18 +03001500 return 0;
1501}
1502
1503/**
1504 * mei_cl_notify_request - send notification stop/start request
1505 *
1506 * @cl: host client
Tomas Winkler3030dc02016-07-26 01:06:05 +03001507 * @fp: associate request with file
Tomas Winkler51678cc2015-07-26 09:54:18 +03001508 * @request: 1 for start or 0 for stop
1509 *
1510 * Locking: called under "dev->device_lock" lock
1511 *
1512 * Return: 0 on such and error otherwise.
1513 */
Tomas Winklerf23e2cc2016-02-07 23:35:23 +02001514int mei_cl_notify_request(struct mei_cl *cl,
Tomas Winkler3030dc02016-07-26 01:06:05 +03001515 const struct file *fp, u8 request)
Tomas Winkler51678cc2015-07-26 09:54:18 +03001516{
1517 struct mei_device *dev;
1518 struct mei_cl_cb *cb;
1519 enum mei_cb_file_ops fop_type;
1520 int rets;
1521
1522 if (WARN_ON(!cl || !cl->dev))
1523 return -ENODEV;
1524
1525 dev = cl->dev;
1526
1527 if (!dev->hbm_f_ev_supported) {
1528 cl_dbg(dev, cl, "notifications not supported\n");
1529 return -EOPNOTSUPP;
1530 }
1531
Alexander Usyskin7c47d2c2017-01-27 16:32:41 +02001532 if (!mei_cl_is_connected(cl))
1533 return -ENODEV;
1534
Tomas Winkler51678cc2015-07-26 09:54:18 +03001535 rets = pm_runtime_get(dev->dev);
1536 if (rets < 0 && rets != -EINPROGRESS) {
1537 pm_runtime_put_noidle(dev->dev);
1538 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1539 return rets;
1540 }
1541
1542 fop_type = mei_cl_notify_req2fop(request);
Tomas Winkler3030dc02016-07-26 01:06:05 +03001543 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
Tomas Winkler51678cc2015-07-26 09:54:18 +03001544 if (!cb) {
1545 rets = -ENOMEM;
1546 goto out;
1547 }
1548
1549 if (mei_hbuf_acquire(dev)) {
1550 if (mei_hbm_cl_notify_req(dev, cl, request)) {
1551 rets = -ENODEV;
1552 goto out;
1553 }
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001554 list_move_tail(&cb->list, &dev->ctrl_rd_list);
Tomas Winkler51678cc2015-07-26 09:54:18 +03001555 }
1556
1557 mutex_unlock(&dev->device_lock);
Alexander Usyskin7ff4bdd2016-04-20 11:03:55 -04001558 wait_event_timeout(cl->wait,
Alexander Usyskina19bf052018-11-06 12:04:40 +02001559 cl->notify_en == request ||
1560 cl->status ||
1561 !mei_cl_is_connected(cl),
Alexander Usyskin7ff4bdd2016-04-20 11:03:55 -04001562 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler51678cc2015-07-26 09:54:18 +03001563 mutex_lock(&dev->device_lock);
1564
Alexander Usyskin4a8eaa92016-04-20 11:03:54 -04001565 if (cl->notify_en != request && !cl->status)
1566 cl->status = -EFAULT;
Tomas Winkler51678cc2015-07-26 09:54:18 +03001567
1568 rets = cl->status;
1569
1570out:
1571 cl_dbg(dev, cl, "rpm: autosuspend\n");
1572 pm_runtime_mark_last_busy(dev->dev);
1573 pm_runtime_put_autosuspend(dev->dev);
1574
1575 mei_io_cb_free(cb);
1576 return rets;
1577}
1578
1579/**
Tomas Winkler237092b2015-07-26 09:54:22 +03001580 * mei_cl_notify - raise notification
1581 *
1582 * @cl: host client
1583 *
1584 * Locking: called under "dev->device_lock" lock
1585 */
1586void mei_cl_notify(struct mei_cl *cl)
1587{
1588 struct mei_device *dev;
1589
1590 if (!cl || !cl->dev)
1591 return;
1592
1593 dev = cl->dev;
1594
1595 if (!cl->notify_en)
1596 return;
1597
1598 cl_dbg(dev, cl, "notify event");
1599 cl->notify_ev = true;
Tomas Winkler850f8942016-02-07 23:35:31 +02001600 if (!mei_cl_bus_notify_event(cl))
1601 wake_up_interruptible(&cl->ev_wait);
Tomas Winkler237092b2015-07-26 09:54:22 +03001602
1603 if (cl->ev_async)
1604 kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +03001605
Tomas Winkler237092b2015-07-26 09:54:22 +03001606}
1607
1608/**
Tomas Winklerb38a3622015-07-26 09:54:19 +03001609 * mei_cl_notify_get - get or wait for notification event
1610 *
1611 * @cl: host client
1612 * @block: this request is blocking
1613 * @notify_ev: true if notification event was received
1614 *
1615 * Locking: called under "dev->device_lock" lock
1616 *
1617 * Return: 0 on such and error otherwise.
1618 */
1619int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
1620{
1621 struct mei_device *dev;
1622 int rets;
1623
1624 *notify_ev = false;
1625
1626 if (WARN_ON(!cl || !cl->dev))
1627 return -ENODEV;
1628
1629 dev = cl->dev;
1630
Alexander Usyskin6c0d6702017-01-27 16:32:42 +02001631 if (!dev->hbm_f_ev_supported) {
1632 cl_dbg(dev, cl, "notifications not supported\n");
1633 return -EOPNOTSUPP;
1634 }
1635
Tomas Winklerb38a3622015-07-26 09:54:19 +03001636 if (!mei_cl_is_connected(cl))
1637 return -ENODEV;
1638
1639 if (cl->notify_ev)
1640 goto out;
1641
1642 if (!block)
1643 return -EAGAIN;
1644
1645 mutex_unlock(&dev->device_lock);
1646 rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
1647 mutex_lock(&dev->device_lock);
1648
1649 if (rets < 0)
1650 return rets;
1651
1652out:
1653 *notify_ev = cl->notify_ev;
1654 cl->notify_ev = false;
1655 return 0;
1656}
1657
1658/**
Masanari Iida393b1482013-04-05 01:05:05 +09001659 * mei_cl_read_start - the start read client message function.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001660 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001661 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +03001662 * @length: number of bytes to read
Tomas Winklerbca67d62015-02-10 10:39:43 +02001663 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +02001664 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001665 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +02001666 */
Tomas Winklerf23e2cc2016-02-07 23:35:23 +02001667int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
Tomas Winkler9ca90502013-01-08 23:07:13 +02001668{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001669 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001670 struct mei_cl_cb *cb;
1671 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001672
Tomas Winkler90e0b5f2013-01-08 23:07:14 +02001673 if (WARN_ON(!cl || !cl->dev))
1674 return -ENODEV;
1675
1676 dev = cl->dev;
1677
Tomas Winklerb950ac12013-07-25 20:15:53 +03001678 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +02001679 return -ENODEV;
1680
Alexander Usyskind49ed642015-05-04 09:43:54 +03001681 if (!mei_me_cl_is_active(cl->me_cl)) {
1682 cl_err(dev, cl, "no such me client\n");
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001683 return -ENOTTY;
Tomas Winkler9ca90502013-01-08 23:07:13 +02001684 }
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001685
Alexander Usyskin394a77d2017-03-20 15:04:03 +02001686 if (mei_cl_is_fixed_address(cl))
Alexander Usyskine51dfa52016-07-26 01:06:02 +03001687 return 0;
1688
Alexander Usyskin46978ad2016-07-26 01:06:03 +03001689 /* HW currently supports only one pending read */
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +03001690 if (cl->rx_flow_ctrl_creds) {
1691 mei_cl_set_read_by_fp(cl, fp);
Alexander Usyskin46978ad2016-07-26 01:06:03 +03001692 return -EBUSY;
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +03001693 }
Alexander Usyskin46978ad2016-07-26 01:06:03 +03001694
Tomas Winkler3030dc02016-07-26 01:06:05 +03001695 cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001696 if (!cb)
1697 return -ENOMEM;
1698
Alexander Usyskinf35fe5f2020-08-18 14:51:41 +03001699 mei_cl_set_read_by_fp(cl, fp);
1700
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001701 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001702 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001703 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001704 cl_err(dev, cl, "rpm: get failed %d\n", rets);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001705 goto nortpm;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001706 }
1707
Alexander Usyskin46978ad2016-07-26 01:06:03 +03001708 rets = 0;
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001709 if (mei_hbuf_acquire(dev)) {
Alexander Usyskin86113502014-03-31 17:59:24 +03001710 rets = mei_hbm_cl_flow_control_req(dev, cl);
1711 if (rets < 0)
Tomas Winkler04bb1392014-03-18 22:52:04 +02001712 goto out;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001713
Alexander Usyskin46978ad2016-07-26 01:06:03 +03001714 list_move_tail(&cb->list, &cl->rd_pending);
Tomas Winkler9ca90502013-01-08 23:07:13 +02001715 }
Alexander Usyskin46978ad2016-07-26 01:06:03 +03001716 cl->rx_flow_ctrl_creds++;
Chao Biaccb8842014-02-12 21:27:25 +02001717
Tomas Winkler04bb1392014-03-18 22:52:04 +02001718out:
1719 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001720 pm_runtime_mark_last_busy(dev->dev);
1721 pm_runtime_put_autosuspend(dev->dev);
Alexander Usyskin1df629e2015-05-04 09:43:56 +03001722nortpm:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001723 if (rets)
1724 mei_io_cb_free(cb);
1725
Tomas Winkler9ca90502013-01-08 23:07:13 +02001726 return rets;
1727}
1728
Tomas Winkler40292382021-06-21 22:37:56 +03001729static inline u8 mei_ext_hdr_set_vtag(void *ext, u8 vtag)
Tomas Winklera1c4d082018-07-23 13:21:24 +03001730{
Tomas Winkler40292382021-06-21 22:37:56 +03001731 struct mei_ext_hdr_vtag *vtag_hdr = ext;
1732
1733 vtag_hdr->hdr.type = MEI_EXT_HDR_VTAG;
1734 vtag_hdr->hdr.length = mei_data2slots(sizeof(*vtag_hdr));
1735 vtag_hdr->vtag = vtag;
1736 vtag_hdr->reserved = 0;
1737 return vtag_hdr->hdr.length;
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001738}
1739
1740/**
1741 * mei_msg_hdr_init - allocate and initialize mei message header
1742 *
1743 * @cb: message callback structure
1744 *
Tomas Winkler7615da22021-01-29 14:07:47 +02001745 * Return: a pointer to initialized header or ERR_PTR on failure
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001746 */
1747static struct mei_msg_hdr *mei_msg_hdr_init(const struct mei_cl_cb *cb)
1748{
1749 size_t hdr_len;
1750 struct mei_ext_meta_hdr *meta;
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001751 struct mei_msg_hdr *mei_hdr;
1752 bool is_ext, is_vtag;
1753
1754 if (!cb)
1755 return ERR_PTR(-EINVAL);
1756
1757 /* Extended header for vtag is attached only on the first fragment */
1758 is_vtag = (cb->vtag && cb->buf_idx == 0);
1759 is_ext = is_vtag;
1760
1761 /* Compute extended header size */
1762 hdr_len = sizeof(*mei_hdr);
1763
1764 if (!is_ext)
1765 goto setup_hdr;
1766
1767 hdr_len += sizeof(*meta);
1768 if (is_vtag)
Tomas Winkler40292382021-06-21 22:37:56 +03001769 hdr_len += sizeof(struct mei_ext_hdr_vtag);
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001770
1771setup_hdr:
1772 mei_hdr = kzalloc(hdr_len, GFP_KERNEL);
1773 if (!mei_hdr)
1774 return ERR_PTR(-ENOMEM);
1775
Tomas Winklera1c4d082018-07-23 13:21:24 +03001776 mei_hdr->host_addr = mei_cl_host_addr(cb->cl);
1777 mei_hdr->me_addr = mei_cl_me_id(cb->cl);
Tomas Winklera1c4d082018-07-23 13:21:24 +03001778 mei_hdr->internal = cb->internal;
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001779 mei_hdr->extended = is_ext;
1780
1781 if (!is_ext)
1782 goto out;
1783
1784 meta = (struct mei_ext_meta_hdr *)mei_hdr->extension;
1785 if (is_vtag) {
1786 meta->count++;
1787 meta->size += mei_ext_hdr_set_vtag(meta->hdrs, cb->vtag);
1788 }
1789out:
1790 mei_hdr->length = hdr_len - sizeof(*mei_hdr);
1791 return mei_hdr;
Tomas Winklera1c4d082018-07-23 13:21:24 +03001792}
1793
1794/**
Tomas Winkler9d098192014-02-19 17:35:48 +02001795 * mei_cl_irq_write - write a message to device
Tomas Winkler21767542013-06-23 09:36:59 +03001796 * from the interrupt thread context
1797 *
1798 * @cl: client
1799 * @cb: callback block.
Tomas Winkler21767542013-06-23 09:36:59 +03001800 * @cmpl_list: complete list.
1801 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001802 * Return: 0, OK; otherwise error.
Tomas Winkler21767542013-06-23 09:36:59 +03001803 */
Tomas Winkler9d098192014-02-19 17:35:48 +02001804int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001805 struct list_head *cmpl_list)
Tomas Winkler21767542013-06-23 09:36:59 +03001806{
Tomas Winkler136698e2013-09-16 23:44:44 +03001807 struct mei_device *dev;
1808 struct mei_msg_data *buf;
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001809 struct mei_msg_hdr *mei_hdr = NULL;
1810 size_t hdr_len;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001811 size_t hbuf_len, dr_len;
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001812 size_t buf_len;
1813 size_t data_len;
Tomas Winkler8c8d9642018-07-23 13:21:23 +03001814 int hbuf_slots;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001815 u32 dr_slots;
1816 u32 dma_len;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03001817 int rets;
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001818 bool first_chunk;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001819 const void *data;
Tomas Winkler21767542013-06-23 09:36:59 +03001820
Tomas Winkler136698e2013-09-16 23:44:44 +03001821 if (WARN_ON(!cl || !cl->dev))
1822 return -ENODEV;
1823
1824 dev = cl->dev;
1825
Tomas Winkler5db75142015-02-10 10:39:42 +02001826 buf = &cb->buf;
Tomas Winkler136698e2013-09-16 23:44:44 +03001827
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001828 first_chunk = cb->buf_idx == 0;
1829
Tomas Winkler4034b812016-07-26 01:06:04 +03001830 rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
Tomas Winkler136698e2013-09-16 23:44:44 +03001831 if (rets < 0)
Alexander Usyskine09ee852016-12-14 17:56:52 +02001832 goto err;
Tomas Winkler136698e2013-09-16 23:44:44 +03001833
1834 if (rets == 0) {
Tomas Winkler04bb1392014-03-18 22:52:04 +02001835 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler136698e2013-09-16 23:44:44 +03001836 return 0;
1837 }
1838
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001839 buf_len = buf->size - cb->buf_idx;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001840 data = buf->data + cb->buf_idx;
Tomas Winkler8c8d9642018-07-23 13:21:23 +03001841 hbuf_slots = mei_hbuf_empty_slots(dev);
1842 if (hbuf_slots < 0) {
1843 rets = -EOVERFLOW;
1844 goto err;
1845 }
Tomas Winkler98e70862018-07-31 09:35:33 +03001846
Tomas Winkler3aef0212020-02-11 18:05:22 +02001847 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001848 dr_slots = mei_dma_ring_empty_slots(dev);
1849 dr_len = mei_slots2data(dr_slots);
Tomas Winkler8c8d9642018-07-23 13:21:23 +03001850
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001851 mei_hdr = mei_msg_hdr_init(cb);
1852 if (IS_ERR(mei_hdr)) {
1853 rets = PTR_ERR(mei_hdr);
1854 mei_hdr = NULL;
1855 goto err;
1856 }
1857
1858 cl_dbg(dev, cl, "Extended Header %d vtag = %d\n",
1859 mei_hdr->extended, cb->vtag);
1860
1861 hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
Tomas Winklera1c4d082018-07-23 13:21:24 +03001862
Tomas Winkler8c8d9642018-07-23 13:21:23 +03001863 /**
1864 * Split the message only if we can write the whole host buffer
1865 * otherwise wait for next time the host buffer is empty.
1866 */
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001867 if (hdr_len + buf_len <= hbuf_len) {
1868 data_len = buf_len;
1869 mei_hdr->msg_complete = 1;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001870 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001871 mei_hdr->dma_ring = 1;
1872 if (buf_len > dr_len)
1873 buf_len = dr_len;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001874 else
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001875 mei_hdr->msg_complete = 1;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001876
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001877 data_len = sizeof(dma_len);
1878 dma_len = buf_len;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001879 data = &dma_len;
Tomas Winkler8c8d9642018-07-23 13:21:23 +03001880 } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001881 buf_len = hbuf_len - hdr_len;
1882 data_len = buf_len;
Tomas Winkler21767542013-06-23 09:36:59 +03001883 } else {
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001884 kfree(mei_hdr);
Tomas Winkler21767542013-06-23 09:36:59 +03001885 return 0;
1886 }
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001887 mei_hdr->length += data_len;
Tomas Winkler21767542013-06-23 09:36:59 +03001888
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001889 if (mei_hdr->dma_ring)
1890 mei_dma_ring_write(dev, buf->data + cb->buf_idx, buf_len);
1891 rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
Tomas Winkler21767542013-06-23 09:36:59 +03001892
Alexander Usyskine09ee852016-12-14 17:56:52 +02001893 if (rets)
1894 goto err;
Tomas Winkler21767542013-06-23 09:36:59 +03001895
1896 cl->status = 0;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +03001897 cl->writing_state = MEI_WRITING;
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001898 cb->buf_idx += buf_len;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +03001899
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001900 if (first_chunk) {
Alexander Usyskine09ee852016-12-14 17:56:52 +02001901 if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
1902 rets = -EIO;
1903 goto err;
1904 }
Tomas Winkler21767542013-06-23 09:36:59 +03001905 }
1906
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001907 if (mei_hdr->msg_complete)
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001908 list_move_tail(&cb->list, &dev->write_waiting_list);
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03001909
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001910 kfree(mei_hdr);
Tomas Winkler21767542013-06-23 09:36:59 +03001911 return 0;
Alexander Usyskine09ee852016-12-14 17:56:52 +02001912
1913err:
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001914 kfree(mei_hdr);
Alexander Usyskine09ee852016-12-14 17:56:52 +02001915 cl->status = rets;
Alexander Usyskin962ff7b2017-01-27 16:32:45 +02001916 list_move_tail(&cb->list, cmpl_list);
Alexander Usyskine09ee852016-12-14 17:56:52 +02001917 return rets;
Tomas Winkler21767542013-06-23 09:36:59 +03001918}
1919
1920/**
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001921 * mei_cl_write - submit a write cb to mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001922 * assumes device_lock is locked
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001923 *
1924 * @cl: host client
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001925 * @cb: write callback with filled data
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001926 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001927 * Return: number of bytes sent on success, <0 on failure.
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001928 */
Tomas Winkler5151e2b2018-07-12 17:10:10 +03001929ssize_t mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb)
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001930{
1931 struct mei_device *dev;
1932 struct mei_msg_data *buf;
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001933 struct mei_msg_hdr *mei_hdr = NULL;
1934 size_t hdr_len;
1935 size_t hbuf_len, dr_len;
1936 size_t buf_len;
1937 size_t data_len;
Tomas Winkler8c8d9642018-07-23 13:21:23 +03001938 int hbuf_slots;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001939 u32 dr_slots;
1940 u32 dma_len;
Tomas Winkler5151e2b2018-07-12 17:10:10 +03001941 ssize_t rets;
Alexander Usyskine0cb6b22016-11-08 18:26:08 +02001942 bool blocking;
Tomas Winklerc30362c2018-11-22 13:11:40 +02001943 const void *data;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001944
1945 if (WARN_ON(!cl || !cl->dev))
1946 return -ENODEV;
1947
1948 if (WARN_ON(!cb))
1949 return -EINVAL;
1950
1951 dev = cl->dev;
1952
Tomas Winkler5db75142015-02-10 10:39:42 +02001953 buf = &cb->buf;
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001954 buf_len = buf->size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001955
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001956 cl_dbg(dev, cl, "buf_len=%zd\n", buf_len);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001957
Tomas Winklerc30362c2018-11-22 13:11:40 +02001958 blocking = cb->blocking;
1959 data = buf->data;
1960
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001961 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001962 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001963 pm_runtime_put_noidle(dev->dev);
Tomas Winkler5151e2b2018-07-12 17:10:10 +03001964 cl_err(dev, cl, "rpm: get failed %zd\n", rets);
Alexander Usyskin6cbb0972016-02-10 23:57:26 +02001965 goto free;
Tomas Winkler04bb1392014-03-18 22:52:04 +02001966 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001967
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001968 cb->buf_idx = 0;
1969 cl->writing_state = MEI_IDLE;
1970
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001971
Tomas Winkler4034b812016-07-26 01:06:04 +03001972 rets = mei_cl_tx_flow_ctrl_creds(cl);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001973 if (rets < 0)
1974 goto err;
1975
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001976 mei_hdr = mei_msg_hdr_init(cb);
1977 if (IS_ERR(mei_hdr)) {
1978 rets = -PTR_ERR(mei_hdr);
1979 mei_hdr = NULL;
1980 goto err;
1981 }
1982
1983 cl_dbg(dev, cl, "Extended Header %d vtag = %d\n",
1984 mei_hdr->extended, cb->vtag);
1985
1986 hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
Tomas Winklera1c4d082018-07-23 13:21:24 +03001987
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001988 if (rets == 0) {
1989 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001990 rets = buf_len;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001991 goto out;
1992 }
Tomas Winkler8c8d9642018-07-23 13:21:23 +03001993
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001994 if (!mei_hbuf_acquire(dev)) {
1995 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
Tomas Winkler0cd7c012020-08-18 14:51:38 +03001996 rets = buf_len;
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001997 goto out;
1998 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001999
Tomas Winkler8c8d9642018-07-23 13:21:23 +03002000 hbuf_slots = mei_hbuf_empty_slots(dev);
2001 if (hbuf_slots < 0) {
2002 rets = -EOVERFLOW;
2003 goto out;
2004 }
2005
Tomas Winkler3aef0212020-02-11 18:05:22 +02002006 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
Tomas Winklerc30362c2018-11-22 13:11:40 +02002007 dr_slots = mei_dma_ring_empty_slots(dev);
2008 dr_len = mei_slots2data(dr_slots);
Tomas Winkler98e70862018-07-31 09:35:33 +03002009
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002010 if (hdr_len + buf_len <= hbuf_len) {
2011 data_len = buf_len;
2012 mei_hdr->msg_complete = 1;
Tomas Winklerc30362c2018-11-22 13:11:40 +02002013 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002014 mei_hdr->dma_ring = 1;
2015 if (buf_len > dr_len)
2016 buf_len = dr_len;
Tomas Winklerc30362c2018-11-22 13:11:40 +02002017 else
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002018 mei_hdr->msg_complete = 1;
Tomas Winklerc30362c2018-11-22 13:11:40 +02002019
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002020 data_len = sizeof(dma_len);
2021 dma_len = buf_len;
Tomas Winklerc30362c2018-11-22 13:11:40 +02002022 data = &dma_len;
Tomas Winkler8c8d9642018-07-23 13:21:23 +03002023 } else {
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002024 buf_len = hbuf_len - hdr_len;
2025 data_len = buf_len;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002026 }
2027
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002028 mei_hdr->length += data_len;
Tomas Winklerc30362c2018-11-22 13:11:40 +02002029
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002030 if (mei_hdr->dma_ring)
2031 mei_dma_ring_write(dev, buf->data, buf_len);
2032 rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
2033
Tomas Winkler2ebf8c92013-09-16 23:44:43 +03002034 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002035 goto err;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002036
Tomas Winkler4034b812016-07-26 01:06:04 +03002037 rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03002038 if (rets)
2039 goto err;
2040
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002041 cl->writing_state = MEI_WRITING;
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002042 cb->buf_idx = buf_len;
Tomas Winklerc30362c2018-11-22 13:11:40 +02002043 /* restore return value */
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002044 buf_len = buf->size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002045
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002046out:
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002047 if (mei_hdr->msg_complete)
Alexander Usyskinaf336ca2018-02-25 20:07:05 +02002048 mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
Alexander Usyskinb8b7303572015-05-07 15:53:58 +03002049 else
Alexander Usyskinaf336ca2018-02-25 20:07:05 +02002050 mei_tx_cb_enqueue(cb, &dev->write_list);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002051
Alexander Usyskin23253c32015-07-23 10:43:11 +03002052 cb = NULL;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002053 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
2054
2055 mutex_unlock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02002056 rets = wait_event_interruptible(cl->tx_wait,
Alexander Usyskin0faf6a32016-02-07 23:35:34 +02002057 cl->writing_state == MEI_WRITE_COMPLETE ||
2058 (!mei_cl_is_connected(cl)));
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002059 mutex_lock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02002060 /* wait_event_interruptible returns -ERESTARTSYS */
2061 if (rets) {
2062 if (signal_pending(current))
2063 rets = -EINTR;
2064 goto err;
2065 }
Alexander Usyskin0faf6a32016-02-07 23:35:34 +02002066 if (cl->writing_state != MEI_WRITE_COMPLETE) {
2067 rets = -EFAULT;
2068 goto err;
2069 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002070 }
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02002071
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002072 rets = buf_len;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002073err:
Tomas Winkler04bb1392014-03-18 22:52:04 +02002074 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03002075 pm_runtime_mark_last_busy(dev->dev);
2076 pm_runtime_put_autosuspend(dev->dev);
Alexander Usyskin6cbb0972016-02-10 23:57:26 +02002077free:
2078 mei_io_cb_free(cb);
Tomas Winkler04bb1392014-03-18 22:52:04 +02002079
Tomas Winkler0cd7c012020-08-18 14:51:38 +03002080 kfree(mei_hdr);
2081
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002082 return rets;
2083}
2084
Tomas Winklerdb086fa2013-05-12 15:34:45 +03002085/**
2086 * mei_cl_complete - processes completed operation for a client
2087 *
2088 * @cl: private data of the file object.
2089 * @cb: callback block.
2090 */
2091void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
2092{
Alexander Usyskina1809d32015-05-07 15:53:59 +03002093 struct mei_device *dev = cl->dev;
2094
Tomas Winkler3c666182015-05-04 09:43:52 +03002095 switch (cb->fop_type) {
2096 case MEI_FOP_WRITE:
Alexander Usyskinaf336ca2018-02-25 20:07:05 +02002097 mei_tx_cb_dequeue(cb);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03002098 cl->writing_state = MEI_WRITE_COMPLETE;
Alexander Usyskina1809d32015-05-07 15:53:59 +03002099 if (waitqueue_active(&cl->tx_wait)) {
Tomas Winklerdb086fa2013-05-12 15:34:45 +03002100 wake_up_interruptible(&cl->tx_wait);
Alexander Usyskina1809d32015-05-07 15:53:59 +03002101 } else {
2102 pm_runtime_mark_last_busy(dev->dev);
2103 pm_request_autosuspend(dev->dev);
2104 }
Tomas Winkler3c666182015-05-04 09:43:52 +03002105 break;
Tomas Winklerdb086fa2013-05-12 15:34:45 +03002106
Tomas Winkler3c666182015-05-04 09:43:52 +03002107 case MEI_FOP_READ:
Alexander Usyskind1376f32020-08-18 14:51:40 +03002108 mei_cl_add_rd_completed(cl, cb);
Alexander Usyskin46978ad2016-07-26 01:06:03 +03002109 if (!mei_cl_is_fixed_address(cl) &&
2110 !WARN_ON(!cl->rx_flow_ctrl_creds))
2111 cl->rx_flow_ctrl_creds--;
Tomas Winklera1f9ae22016-02-07 23:35:30 +02002112 if (!mei_cl_bus_rx_event(cl))
2113 wake_up_interruptible(&cl->rx_wait);
Tomas Winkler3c666182015-05-04 09:43:52 +03002114 break;
Tomas Winklerdb086fa2013-05-12 15:34:45 +03002115
Tomas Winkler3c666182015-05-04 09:43:52 +03002116 case MEI_FOP_CONNECT:
2117 case MEI_FOP_DISCONNECT:
Tomas Winkler51678cc2015-07-26 09:54:18 +03002118 case MEI_FOP_NOTIFY_STOP:
2119 case MEI_FOP_NOTIFY_START:
Alexander Usyskin369aea82021-02-06 16:43:24 +02002120 case MEI_FOP_DMA_MAP:
2121 case MEI_FOP_DMA_UNMAP:
Tomas Winkler3c666182015-05-04 09:43:52 +03002122 if (waitqueue_active(&cl->wait))
2123 wake_up(&cl->wait);
2124
2125 break;
Alexander Usyskin6a8d6482016-04-17 12:16:03 -04002126 case MEI_FOP_DISCONNECT_RSP:
2127 mei_io_cb_free(cb);
2128 mei_cl_set_disconnected(cl);
2129 break;
Tomas Winkler3c666182015-05-04 09:43:52 +03002130 default:
2131 BUG_ON(0);
Tomas Winklerdb086fa2013-05-12 15:34:45 +03002132 }
2133}
2134
Tomas Winkler4234a6d2013-04-08 21:56:37 +03002135
2136/**
Tomas Winkler074b4c02013-02-06 14:06:44 +02002137 * mei_cl_all_disconnect - disconnect forcefully all connected clients
2138 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03002139 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02002140 */
Tomas Winkler074b4c02013-02-06 14:06:44 +02002141void mei_cl_all_disconnect(struct mei_device *dev)
2142{
Tomas Winkler31f88f52014-02-17 15:13:25 +02002143 struct mei_cl *cl;
Tomas Winkler074b4c02013-02-06 14:06:44 +02002144
Tomas Winkler3c666182015-05-04 09:43:52 +03002145 list_for_each_entry(cl, &dev->file_list, link)
2146 mei_cl_set_disconnected(cl);
Tomas Winkler074b4c02013-02-06 14:06:44 +02002147}
Alexander Usyskin369aea82021-02-06 16:43:24 +02002148
2149static struct mei_cl *mei_cl_dma_map_find(struct mei_device *dev, u8 buffer_id)
2150{
2151 struct mei_cl *cl;
2152
2153 list_for_each_entry(cl, &dev->file_list, link)
2154 if (cl->dma.buffer_id == buffer_id)
2155 return cl;
2156 return NULL;
2157}
2158
2159/**
2160 * mei_cl_irq_dma_map - send client dma map request in irq_thread context
2161 *
2162 * @cl: client
2163 * @cb: callback block.
2164 * @cmpl_list: complete list.
2165 *
2166 * Return: 0 on such and error otherwise.
2167 */
2168int mei_cl_irq_dma_map(struct mei_cl *cl, struct mei_cl_cb *cb,
2169 struct list_head *cmpl_list)
2170{
2171 struct mei_device *dev = cl->dev;
2172 u32 msg_slots;
2173 int slots;
2174 int ret;
2175
2176 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_map_request));
2177 slots = mei_hbuf_empty_slots(dev);
2178 if (slots < 0)
2179 return -EOVERFLOW;
2180
2181 if ((u32)slots < msg_slots)
2182 return -EMSGSIZE;
2183
2184 ret = mei_hbm_cl_dma_map_req(dev, cl);
2185 if (ret) {
2186 cl->status = ret;
2187 list_move_tail(&cb->list, cmpl_list);
2188 return ret;
2189 }
2190
2191 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2192 return 0;
2193}
2194
2195/**
2196 * mei_cl_irq_dma_unmap - send client dma unmap request in irq_thread context
2197 *
2198 * @cl: client
2199 * @cb: callback block.
2200 * @cmpl_list: complete list.
2201 *
2202 * Return: 0 on such and error otherwise.
2203 */
2204int mei_cl_irq_dma_unmap(struct mei_cl *cl, struct mei_cl_cb *cb,
2205 struct list_head *cmpl_list)
2206{
2207 struct mei_device *dev = cl->dev;
2208 u32 msg_slots;
2209 int slots;
2210 int ret;
2211
2212 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_unmap_request));
2213 slots = mei_hbuf_empty_slots(dev);
2214 if (slots < 0)
2215 return -EOVERFLOW;
2216
2217 if ((u32)slots < msg_slots)
2218 return -EMSGSIZE;
2219
2220 ret = mei_hbm_cl_dma_unmap_req(dev, cl);
2221 if (ret) {
2222 cl->status = ret;
2223 list_move_tail(&cb->list, cmpl_list);
2224 return ret;
2225 }
2226
2227 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2228 return 0;
2229}
2230
2231static int mei_cl_dma_alloc(struct mei_cl *cl, u8 buf_id, size_t size)
2232{
2233 cl->dma.vaddr = dmam_alloc_coherent(cl->dev->dev, size,
2234 &cl->dma.daddr, GFP_KERNEL);
2235 if (!cl->dma.vaddr)
2236 return -ENOMEM;
2237
2238 cl->dma.buffer_id = buf_id;
2239 cl->dma.size = size;
2240
2241 return 0;
2242}
2243
2244static void mei_cl_dma_free(struct mei_cl *cl)
2245{
2246 cl->dma.buffer_id = 0;
2247 dmam_free_coherent(cl->dev->dev,
2248 cl->dma.size, cl->dma.vaddr, cl->dma.daddr);
2249 cl->dma.size = 0;
2250 cl->dma.vaddr = NULL;
2251 cl->dma.daddr = 0;
2252}
2253
2254/**
Tamar Mashiah09f8c332021-06-21 22:37:55 +03002255 * mei_cl_dma_alloc_and_map - send client dma map request
Alexander Usyskin369aea82021-02-06 16:43:24 +02002256 *
2257 * @cl: host client
2258 * @fp: pointer to file structure
2259 * @buffer_id: id of the mapped buffer
2260 * @size: size of the buffer
2261 *
2262 * Locking: called under "dev->device_lock" lock
2263 *
2264 * Return:
2265 * * -ENODEV
2266 * * -EINVAL
2267 * * -EOPNOTSUPP
2268 * * -EPROTO
2269 * * -ENOMEM;
2270 */
2271int mei_cl_dma_alloc_and_map(struct mei_cl *cl, const struct file *fp,
2272 u8 buffer_id, size_t size)
2273{
2274 struct mei_device *dev;
2275 struct mei_cl_cb *cb;
2276 int rets;
2277
2278 if (WARN_ON(!cl || !cl->dev))
2279 return -ENODEV;
2280
2281 dev = cl->dev;
2282
2283 if (!dev->hbm_f_cd_supported) {
2284 cl_dbg(dev, cl, "client dma is not supported\n");
2285 return -EOPNOTSUPP;
2286 }
2287
2288 if (buffer_id == 0)
2289 return -EINVAL;
2290
Tomas Winklerce068bc2021-03-18 07:59:59 +02002291 if (mei_cl_is_connected(cl))
2292 return -EPROTO;
Alexander Usyskin369aea82021-02-06 16:43:24 +02002293
2294 if (cl->dma_mapped)
2295 return -EPROTO;
2296
2297 if (mei_cl_dma_map_find(dev, buffer_id)) {
2298 cl_dbg(dev, cl, "client dma with id %d is already allocated\n",
2299 cl->dma.buffer_id);
2300 return -EPROTO;
2301 }
2302
2303 rets = pm_runtime_get(dev->dev);
2304 if (rets < 0 && rets != -EINPROGRESS) {
2305 pm_runtime_put_noidle(dev->dev);
2306 cl_err(dev, cl, "rpm: get failed %d\n", rets);
2307 return rets;
2308 }
2309
2310 rets = mei_cl_dma_alloc(cl, buffer_id, size);
2311 if (rets) {
2312 pm_runtime_put_noidle(dev->dev);
2313 return rets;
2314 }
2315
2316 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_MAP, fp);
2317 if (!cb) {
2318 rets = -ENOMEM;
2319 goto out;
2320 }
2321
2322 if (mei_hbuf_acquire(dev)) {
2323 if (mei_hbm_cl_dma_map_req(dev, cl)) {
2324 rets = -ENODEV;
2325 goto out;
2326 }
2327 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2328 }
2329
2330 mutex_unlock(&dev->device_lock);
2331 wait_event_timeout(cl->wait,
Tomas Winklerce068bc2021-03-18 07:59:59 +02002332 cl->dma_mapped || cl->status,
Alexander Usyskin369aea82021-02-06 16:43:24 +02002333 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
2334 mutex_lock(&dev->device_lock);
2335
2336 if (!cl->dma_mapped && !cl->status)
2337 cl->status = -EFAULT;
2338
2339 rets = cl->status;
2340
2341out:
2342 if (rets)
2343 mei_cl_dma_free(cl);
2344
2345 cl_dbg(dev, cl, "rpm: autosuspend\n");
2346 pm_runtime_mark_last_busy(dev->dev);
2347 pm_runtime_put_autosuspend(dev->dev);
2348
2349 mei_io_cb_free(cb);
2350 return rets;
2351}
2352
2353/**
Tamar Mashiah09f8c332021-06-21 22:37:55 +03002354 * mei_cl_dma_unmap - send client dma unmap request
Alexander Usyskin369aea82021-02-06 16:43:24 +02002355 *
2356 * @cl: host client
2357 * @fp: pointer to file structure
2358 *
2359 * Locking: called under "dev->device_lock" lock
2360 *
2361 * Return: 0 on such and error otherwise.
2362 */
2363int mei_cl_dma_unmap(struct mei_cl *cl, const struct file *fp)
2364{
2365 struct mei_device *dev;
2366 struct mei_cl_cb *cb;
2367 int rets;
2368
2369 if (WARN_ON(!cl || !cl->dev))
2370 return -ENODEV;
2371
2372 dev = cl->dev;
2373
2374 if (!dev->hbm_f_cd_supported) {
2375 cl_dbg(dev, cl, "client dma is not supported\n");
2376 return -EOPNOTSUPP;
2377 }
2378
Tomas Winklerce068bc2021-03-18 07:59:59 +02002379 /* do not allow unmap for connected client */
2380 if (mei_cl_is_connected(cl))
2381 return -EPROTO;
Alexander Usyskin369aea82021-02-06 16:43:24 +02002382
2383 if (!cl->dma_mapped)
2384 return -EPROTO;
2385
2386 rets = pm_runtime_get(dev->dev);
2387 if (rets < 0 && rets != -EINPROGRESS) {
2388 pm_runtime_put_noidle(dev->dev);
2389 cl_err(dev, cl, "rpm: get failed %d\n", rets);
2390 return rets;
2391 }
2392
2393 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_UNMAP, fp);
2394 if (!cb) {
2395 rets = -ENOMEM;
2396 goto out;
2397 }
2398
2399 if (mei_hbuf_acquire(dev)) {
2400 if (mei_hbm_cl_dma_unmap_req(dev, cl)) {
2401 rets = -ENODEV;
2402 goto out;
2403 }
2404 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2405 }
2406
2407 mutex_unlock(&dev->device_lock);
2408 wait_event_timeout(cl->wait,
Tomas Winklerce068bc2021-03-18 07:59:59 +02002409 !cl->dma_mapped || cl->status,
Alexander Usyskin369aea82021-02-06 16:43:24 +02002410 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
2411 mutex_lock(&dev->device_lock);
2412
2413 if (cl->dma_mapped && !cl->status)
2414 cl->status = -EFAULT;
2415
2416 rets = cl->status;
2417
2418 if (!rets)
2419 mei_cl_dma_free(cl);
2420out:
2421 cl_dbg(dev, cl, "rpm: autosuspend\n");
2422 pm_runtime_mark_last_busy(dev->dev);
2423 pm_runtime_put_autosuspend(dev->dev);
2424
2425 mei_io_cb_free(cb);
2426 return rets;
2427}