Commit | Line | Data |
---|---|---|
ab841160 OW |
1 | /* |
2 | * | |
3 | * Intel Management Engine Interface (Intel MEI) Linux driver | |
733ba91c | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
ab841160 OW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | */ | |
16 | ||
ab841160 | 17 | #include <linux/sched.h> |
9ca9050b TW |
18 | #include <linux/wait.h> |
19 | #include <linux/delay.h> | |
1f180359 | 20 | #include <linux/slab.h> |
04bb139a | 21 | #include <linux/pm_runtime.h> |
ab841160 | 22 | |
4f3afe1d | 23 | #include <linux/mei.h> |
47a73801 TW |
24 | |
25 | #include "mei_dev.h" | |
0edb23fc | 26 | #include "hbm.h" |
90e0b5f1 TW |
27 | #include "client.h" |
28 | ||
79563db9 TW |
29 | /** |
30 | * mei_me_cl_init - initialize me client | |
31 | * | |
32 | * @me_cl: me client | |
33 | */ | |
34 | void mei_me_cl_init(struct mei_me_client *me_cl) | |
35 | { | |
36 | INIT_LIST_HEAD(&me_cl->list); | |
37 | kref_init(&me_cl->refcnt); | |
38 | } | |
39 | ||
40 | /** | |
41 | * mei_me_cl_get - increases me client refcount | |
42 | * | |
43 | * @me_cl: me client | |
44 | * | |
45 | * Locking: called under "dev->device_lock" lock | |
46 | * | |
47 | * Return: me client or NULL | |
48 | */ | |
49 | struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl) | |
50 | { | |
b7d88514 TW |
51 | if (me_cl && kref_get_unless_zero(&me_cl->refcnt)) |
52 | return me_cl; | |
79563db9 | 53 | |
b7d88514 | 54 | return NULL; |
79563db9 TW |
55 | } |
56 | ||
57 | /** | |
b7d88514 | 58 | * mei_me_cl_release - free me client |
79563db9 TW |
59 | * |
60 | * Locking: called under "dev->device_lock" lock | |
61 | * | |
62 | * @ref: me_client refcount | |
63 | */ | |
64 | static void mei_me_cl_release(struct kref *ref) | |
65 | { | |
66 | struct mei_me_client *me_cl = | |
67 | container_of(ref, struct mei_me_client, refcnt); | |
b7d88514 | 68 | |
79563db9 TW |
69 | kfree(me_cl); |
70 | } | |
b7d88514 | 71 | |
79563db9 TW |
72 | /** |
73 | * mei_me_cl_put - decrease me client refcount and free client if necessary | |
74 | * | |
75 | * Locking: called under "dev->device_lock" lock | |
76 | * | |
77 | * @me_cl: me client | |
78 | */ | |
79 | void mei_me_cl_put(struct mei_me_client *me_cl) | |
80 | { | |
81 | if (me_cl) | |
82 | kref_put(&me_cl->refcnt, mei_me_cl_release); | |
83 | } | |
84 | ||
90e0b5f1 | 85 | /** |
b7d88514 TW |
86 | * __mei_me_cl_del - delete me client form the list and decrease |
87 | * reference counter | |
88 | * | |
89 | * @dev: mei device | |
90 | * @me_cl: me client | |
91 | * | |
92 | * Locking: dev->me_clients_rwsem | |
93 | */ | |
94 | static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl) | |
95 | { | |
96 | if (!me_cl) | |
97 | return; | |
98 | ||
99 | list_del(&me_cl->list); | |
100 | mei_me_cl_put(me_cl); | |
101 | } | |
102 | ||
103 | /** | |
104 | * mei_me_cl_add - add me client to the list | |
105 | * | |
106 | * @dev: mei device | |
107 | * @me_cl: me client | |
108 | */ | |
109 | void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl) | |
110 | { | |
111 | down_write(&dev->me_clients_rwsem); | |
112 | list_add(&me_cl->list, &dev->me_clients); | |
113 | up_write(&dev->me_clients_rwsem); | |
114 | } | |
115 | ||
116 | /** | |
117 | * __mei_me_cl_by_uuid - locate me client by uuid | |
79563db9 | 118 | * increases ref count |
90e0b5f1 TW |
119 | * |
120 | * @dev: mei device | |
a8605ea2 | 121 | * @uuid: me client uuid |
a27a76d3 | 122 | * |
a8605ea2 | 123 | * Return: me client or NULL if not found |
b7d88514 TW |
124 | * |
125 | * Locking: dev->me_clients_rwsem | |
90e0b5f1 | 126 | */ |
b7d88514 | 127 | static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev, |
d320832f | 128 | const uuid_le *uuid) |
90e0b5f1 | 129 | { |
5ca2d388 | 130 | struct mei_me_client *me_cl; |
b7d88514 | 131 | const uuid_le *pn; |
90e0b5f1 | 132 | |
b7d88514 TW |
133 | WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem)); |
134 | ||
135 | list_for_each_entry(me_cl, &dev->me_clients, list) { | |
136 | pn = &me_cl->props.protocol_name; | |
137 | if (uuid_le_cmp(*uuid, *pn) == 0) | |
79563db9 | 138 | return mei_me_cl_get(me_cl); |
b7d88514 | 139 | } |
90e0b5f1 | 140 | |
d320832f | 141 | return NULL; |
90e0b5f1 TW |
142 | } |
143 | ||
b7d88514 TW |
144 | /** |
145 | * mei_me_cl_by_uuid - locate me client by uuid | |
146 | * increases ref count | |
147 | * | |
148 | * @dev: mei device | |
149 | * @uuid: me client uuid | |
150 | * | |
151 | * Return: me client or NULL if not found | |
152 | * | |
153 | * Locking: dev->me_clients_rwsem | |
154 | */ | |
155 | struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev, | |
156 | const uuid_le *uuid) | |
157 | { | |
158 | struct mei_me_client *me_cl; | |
159 | ||
160 | down_read(&dev->me_clients_rwsem); | |
161 | me_cl = __mei_me_cl_by_uuid(dev, uuid); | |
162 | up_read(&dev->me_clients_rwsem); | |
163 | ||
164 | return me_cl; | |
165 | } | |
166 | ||
90e0b5f1 | 167 | /** |
a8605ea2 | 168 | * mei_me_cl_by_id - locate me client by client id |
79563db9 | 169 | * increases ref count |
90e0b5f1 TW |
170 | * |
171 | * @dev: the device structure | |
172 | * @client_id: me client id | |
173 | * | |
a8605ea2 | 174 | * Return: me client or NULL if not found |
b7d88514 TW |
175 | * |
176 | * Locking: dev->me_clients_rwsem | |
90e0b5f1 | 177 | */ |
d320832f | 178 | struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id) |
90e0b5f1 | 179 | { |
a27a76d3 | 180 | |
b7d88514 TW |
181 | struct mei_me_client *__me_cl, *me_cl = NULL; |
182 | ||
183 | down_read(&dev->me_clients_rwsem); | |
184 | list_for_each_entry(__me_cl, &dev->me_clients, list) { | |
185 | if (__me_cl->client_id == client_id) { | |
186 | me_cl = mei_me_cl_get(__me_cl); | |
187 | break; | |
188 | } | |
189 | } | |
190 | up_read(&dev->me_clients_rwsem); | |
191 | ||
192 | return me_cl; | |
193 | } | |
194 | ||
195 | /** | |
196 | * __mei_me_cl_by_uuid_id - locate me client by client id and uuid | |
197 | * increases ref count | |
198 | * | |
199 | * @dev: the device structure | |
200 | * @uuid: me client uuid | |
201 | * @client_id: me client id | |
202 | * | |
203 | * Return: me client or null if not found | |
204 | * | |
205 | * Locking: dev->me_clients_rwsem | |
206 | */ | |
207 | static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev, | |
208 | const uuid_le *uuid, u8 client_id) | |
209 | { | |
5ca2d388 | 210 | struct mei_me_client *me_cl; |
b7d88514 TW |
211 | const uuid_le *pn; |
212 | ||
213 | WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem)); | |
90e0b5f1 | 214 | |
b7d88514 TW |
215 | list_for_each_entry(me_cl, &dev->me_clients, list) { |
216 | pn = &me_cl->props.protocol_name; | |
217 | if (uuid_le_cmp(*uuid, *pn) == 0 && | |
218 | me_cl->client_id == client_id) | |
79563db9 | 219 | return mei_me_cl_get(me_cl); |
b7d88514 | 220 | } |
79563db9 | 221 | |
d320832f | 222 | return NULL; |
90e0b5f1 | 223 | } |
ab841160 | 224 | |
b7d88514 | 225 | |
a8605ea2 AU |
226 | /** |
227 | * mei_me_cl_by_uuid_id - locate me client by client id and uuid | |
79563db9 | 228 | * increases ref count |
a8605ea2 AU |
229 | * |
230 | * @dev: the device structure | |
231 | * @uuid: me client uuid | |
232 | * @client_id: me client id | |
233 | * | |
b7d88514 | 234 | * Return: me client or null if not found |
a8605ea2 | 235 | */ |
d880f329 TW |
236 | struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev, |
237 | const uuid_le *uuid, u8 client_id) | |
238 | { | |
239 | struct mei_me_client *me_cl; | |
240 | ||
b7d88514 TW |
241 | down_read(&dev->me_clients_rwsem); |
242 | me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id); | |
243 | up_read(&dev->me_clients_rwsem); | |
79563db9 | 244 | |
b7d88514 | 245 | return me_cl; |
d880f329 TW |
246 | } |
247 | ||
25ca6472 | 248 | /** |
79563db9 | 249 | * mei_me_cl_rm_by_uuid - remove all me clients matching uuid |
25ca6472 TW |
250 | * |
251 | * @dev: the device structure | |
252 | * @uuid: me client uuid | |
79563db9 TW |
253 | * |
254 | * Locking: called under "dev->device_lock" lock | |
25ca6472 | 255 | */ |
79563db9 | 256 | void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid) |
25ca6472 | 257 | { |
b7d88514 | 258 | struct mei_me_client *me_cl; |
25ca6472 | 259 | |
79563db9 | 260 | dev_dbg(dev->dev, "remove %pUl\n", uuid); |
b7d88514 TW |
261 | |
262 | down_write(&dev->me_clients_rwsem); | |
263 | me_cl = __mei_me_cl_by_uuid(dev, uuid); | |
264 | __mei_me_cl_del(dev, me_cl); | |
265 | up_write(&dev->me_clients_rwsem); | |
79563db9 TW |
266 | } |
267 | ||
268 | /** | |
269 | * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id | |
270 | * | |
271 | * @dev: the device structure | |
272 | * @uuid: me client uuid | |
273 | * @id: me client id | |
274 | * | |
275 | * Locking: called under "dev->device_lock" lock | |
276 | */ | |
277 | void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id) | |
278 | { | |
b7d88514 | 279 | struct mei_me_client *me_cl; |
79563db9 TW |
280 | |
281 | dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id); | |
b7d88514 TW |
282 | |
283 | down_write(&dev->me_clients_rwsem); | |
284 | me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id); | |
285 | __mei_me_cl_del(dev, me_cl); | |
286 | up_write(&dev->me_clients_rwsem); | |
25ca6472 TW |
287 | } |
288 | ||
79563db9 TW |
289 | /** |
290 | * mei_me_cl_rm_all - remove all me clients | |
291 | * | |
292 | * @dev: the device structure | |
293 | * | |
294 | * Locking: called under "dev->device_lock" lock | |
295 | */ | |
296 | void mei_me_cl_rm_all(struct mei_device *dev) | |
297 | { | |
298 | struct mei_me_client *me_cl, *next; | |
299 | ||
b7d88514 | 300 | down_write(&dev->me_clients_rwsem); |
79563db9 | 301 | list_for_each_entry_safe(me_cl, next, &dev->me_clients, list) |
b7d88514 TW |
302 | __mei_me_cl_del(dev, me_cl); |
303 | up_write(&dev->me_clients_rwsem); | |
79563db9 TW |
304 | } |
305 | ||
9ca9050b | 306 | /** |
cc99ecfd | 307 | * mei_cl_cmp_id - tells if the clients are the same |
9ca9050b | 308 | * |
cc99ecfd TW |
309 | * @cl1: host client 1 |
310 | * @cl2: host client 2 | |
311 | * | |
a8605ea2 | 312 | * Return: true - if the clients has same host and me ids |
cc99ecfd TW |
313 | * false - otherwise |
314 | */ | |
315 | static inline bool mei_cl_cmp_id(const struct mei_cl *cl1, | |
316 | const struct mei_cl *cl2) | |
317 | { | |
318 | return cl1 && cl2 && | |
319 | (cl1->host_client_id == cl2->host_client_id) && | |
320 | (cl1->me_client_id == cl2->me_client_id); | |
321 | } | |
322 | ||
928fa666 TW |
323 | /** |
324 | * mei_io_cb_free - free mei_cb_private related memory | |
325 | * | |
326 | * @cb: mei callback struct | |
327 | */ | |
328 | void mei_io_cb_free(struct mei_cl_cb *cb) | |
329 | { | |
330 | if (cb == NULL) | |
331 | return; | |
332 | ||
333 | list_del(&cb->list); | |
334 | kfree(cb->buf.data); | |
335 | kfree(cb); | |
336 | } | |
337 | ||
338 | /** | |
339 | * mei_io_cb_init - allocate and initialize io callback | |
340 | * | |
341 | * @cl: mei client | |
342 | * @type: operation type | |
343 | * @fp: pointer to file structure | |
344 | * | |
345 | * Return: mei_cl_cb pointer or NULL; | |
346 | */ | |
347 | struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, enum mei_cb_file_ops type, | |
348 | struct file *fp) | |
349 | { | |
350 | struct mei_cl_cb *cb; | |
351 | ||
352 | cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL); | |
353 | if (!cb) | |
354 | return NULL; | |
355 | ||
356 | INIT_LIST_HEAD(&cb->list); | |
357 | cb->file_object = fp; | |
358 | cb->cl = cl; | |
359 | cb->buf_idx = 0; | |
360 | cb->fop_type = type; | |
361 | return cb; | |
362 | } | |
363 | ||
cc99ecfd | 364 | /** |
3908be6f | 365 | * __mei_io_list_flush - removes and frees cbs belonging to cl. |
cc99ecfd TW |
366 | * |
367 | * @list: an instance of our list structure | |
368 | * @cl: host client, can be NULL for flushing the whole list | |
369 | * @free: whether to free the cbs | |
9ca9050b | 370 | */ |
cc99ecfd TW |
371 | static void __mei_io_list_flush(struct mei_cl_cb *list, |
372 | struct mei_cl *cl, bool free) | |
9ca9050b | 373 | { |
928fa666 | 374 | struct mei_cl_cb *cb, *next; |
9ca9050b | 375 | |
cc99ecfd | 376 | /* enable removing everything if no cl is specified */ |
9ca9050b | 377 | list_for_each_entry_safe(cb, next, &list->list, list) { |
140c7553 | 378 | if (!cl || mei_cl_cmp_id(cl, cb->cl)) { |
928fa666 | 379 | list_del_init(&cb->list); |
cc99ecfd TW |
380 | if (free) |
381 | mei_io_cb_free(cb); | |
382 | } | |
9ca9050b TW |
383 | } |
384 | } | |
385 | ||
cc99ecfd TW |
386 | /** |
387 | * mei_io_list_flush - removes list entry belonging to cl. | |
388 | * | |
389 | * @list: An instance of our list structure | |
390 | * @cl: host client | |
391 | */ | |
5456796b | 392 | void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl) |
cc99ecfd TW |
393 | { |
394 | __mei_io_list_flush(list, cl, false); | |
395 | } | |
396 | ||
cc99ecfd TW |
397 | /** |
398 | * mei_io_list_free - removes cb belonging to cl and free them | |
399 | * | |
400 | * @list: An instance of our list structure | |
401 | * @cl: host client | |
402 | */ | |
403 | static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl) | |
404 | { | |
405 | __mei_io_list_flush(list, cl, true); | |
406 | } | |
407 | ||
664df38b | 408 | /** |
5db7514d | 409 | * mei_io_cb_alloc_buf - allocate callback buffer |
664df38b | 410 | * |
393b148f MI |
411 | * @cb: io callback structure |
412 | * @length: size of the buffer | |
664df38b | 413 | * |
a8605ea2 | 414 | * Return: 0 on success |
664df38b TW |
415 | * -EINVAL if cb is NULL |
416 | * -ENOMEM if allocation failed | |
417 | */ | |
5db7514d | 418 | int mei_io_cb_alloc_buf(struct mei_cl_cb *cb, size_t length) |
664df38b TW |
419 | { |
420 | if (!cb) | |
421 | return -EINVAL; | |
422 | ||
423 | if (length == 0) | |
424 | return 0; | |
425 | ||
5db7514d TW |
426 | cb->buf.data = kmalloc(length, GFP_KERNEL); |
427 | if (!cb->buf.data) | |
664df38b | 428 | return -ENOMEM; |
5db7514d | 429 | cb->buf.size = length; |
664df38b TW |
430 | return 0; |
431 | } | |
9ca9050b | 432 | |
bca67d68 TW |
433 | /** |
434 | * mei_cl_alloc_cb - a convenient wrapper for allocating read cb | |
435 | * | |
436 | * @cl: host client | |
437 | * @length: size of the buffer | |
438 | * @type: operation type | |
439 | * @fp: associated file pointer (might be NULL) | |
440 | * | |
441 | * Return: cb on success and NULL on failure | |
442 | */ | |
443 | struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length, | |
444 | enum mei_cb_file_ops type, struct file *fp) | |
445 | { | |
446 | struct mei_cl_cb *cb; | |
447 | ||
448 | cb = mei_io_cb_init(cl, type, fp); | |
449 | if (!cb) | |
450 | return NULL; | |
451 | ||
452 | if (mei_io_cb_alloc_buf(cb, length)) { | |
453 | mei_io_cb_free(cb); | |
454 | return NULL; | |
455 | } | |
456 | ||
457 | return cb; | |
458 | } | |
459 | ||
a9bed610 TW |
460 | /** |
461 | * mei_cl_read_cb - find this cl's callback in the read list | |
462 | * for a specific file | |
463 | * | |
464 | * @cl: host client | |
465 | * @fp: file pointer (matching cb file object), may be NULL | |
466 | * | |
467 | * Return: cb on success, NULL if cb is not found | |
468 | */ | |
469 | struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp) | |
470 | { | |
471 | struct mei_cl_cb *cb; | |
472 | ||
473 | list_for_each_entry(cb, &cl->rd_completed, list) | |
474 | if (!fp || fp == cb->file_object) | |
475 | return cb; | |
476 | ||
477 | return NULL; | |
478 | } | |
479 | ||
480 | /** | |
481 | * mei_cl_read_cb_flush - free client's read pending and completed cbs | |
482 | * for a specific file | |
483 | * | |
484 | * @cl: host client | |
485 | * @fp: file pointer (matching cb file object), may be NULL | |
486 | */ | |
487 | void mei_cl_read_cb_flush(const struct mei_cl *cl, const struct file *fp) | |
488 | { | |
489 | struct mei_cl_cb *cb, *next; | |
490 | ||
491 | list_for_each_entry_safe(cb, next, &cl->rd_completed, list) | |
492 | if (!fp || fp == cb->file_object) | |
493 | mei_io_cb_free(cb); | |
494 | ||
495 | ||
496 | list_for_each_entry_safe(cb, next, &cl->rd_pending, list) | |
497 | if (!fp || fp == cb->file_object) | |
498 | mei_io_cb_free(cb); | |
499 | } | |
500 | ||
9ca9050b TW |
501 | /** |
502 | * mei_cl_flush_queues - flushes queue lists belonging to cl. | |
503 | * | |
9ca9050b | 504 | * @cl: host client |
a9bed610 | 505 | * @fp: file pointer (matching cb file object), may be NULL |
ce23139c AU |
506 | * |
507 | * Return: 0 on success, -EINVAL if cl or cl->dev is NULL. | |
9ca9050b | 508 | */ |
a9bed610 | 509 | int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp) |
9ca9050b | 510 | { |
c0abffbd AU |
511 | struct mei_device *dev; |
512 | ||
90e0b5f1 | 513 | if (WARN_ON(!cl || !cl->dev)) |
9ca9050b TW |
514 | return -EINVAL; |
515 | ||
c0abffbd AU |
516 | dev = cl->dev; |
517 | ||
518 | cl_dbg(dev, cl, "remove list entry belonging to cl\n"); | |
cc99ecfd TW |
519 | mei_io_list_free(&cl->dev->write_list, cl); |
520 | mei_io_list_free(&cl->dev->write_waiting_list, cl); | |
9ca9050b TW |
521 | mei_io_list_flush(&cl->dev->ctrl_wr_list, cl); |
522 | mei_io_list_flush(&cl->dev->ctrl_rd_list, cl); | |
523 | mei_io_list_flush(&cl->dev->amthif_cmd_list, cl); | |
524 | mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl); | |
a9bed610 TW |
525 | |
526 | mei_cl_read_cb_flush(cl, fp); | |
527 | ||
9ca9050b TW |
528 | return 0; |
529 | } | |
530 | ||
ab841160 | 531 | |
9ca9050b | 532 | /** |
83ce0741 | 533 | * mei_cl_init - initializes cl. |
9ca9050b TW |
534 | * |
535 | * @cl: host client to be initialized | |
536 | * @dev: mei device | |
537 | */ | |
538 | void mei_cl_init(struct mei_cl *cl, struct mei_device *dev) | |
539 | { | |
540 | memset(cl, 0, sizeof(struct mei_cl)); | |
541 | init_waitqueue_head(&cl->wait); | |
542 | init_waitqueue_head(&cl->rx_wait); | |
543 | init_waitqueue_head(&cl->tx_wait); | |
a9bed610 TW |
544 | INIT_LIST_HEAD(&cl->rd_completed); |
545 | INIT_LIST_HEAD(&cl->rd_pending); | |
9ca9050b | 546 | INIT_LIST_HEAD(&cl->link); |
a7b71bc0 | 547 | INIT_LIST_HEAD(&cl->device_link); |
9ca9050b | 548 | cl->writing_state = MEI_IDLE; |
3c666182 | 549 | cl->state = MEI_FILE_INITIALIZING; |
9ca9050b TW |
550 | cl->dev = dev; |
551 | } | |
552 | ||
553 | /** | |
554 | * mei_cl_allocate - allocates cl structure and sets it up. | |
555 | * | |
556 | * @dev: mei device | |
a8605ea2 | 557 | * Return: The allocated file or NULL on failure |
9ca9050b TW |
558 | */ |
559 | struct mei_cl *mei_cl_allocate(struct mei_device *dev) | |
560 | { | |
561 | struct mei_cl *cl; | |
562 | ||
563 | cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL); | |
564 | if (!cl) | |
565 | return NULL; | |
566 | ||
567 | mei_cl_init(cl, dev); | |
568 | ||
569 | return cl; | |
570 | } | |
571 | ||
3908be6f AU |
572 | /** |
573 | * mei_cl_link - allocate host id in the host map | |
9ca9050b | 574 | * |
3908be6f | 575 | * @cl: host client |
03b8d341 | 576 | * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one |
393b148f | 577 | * |
a8605ea2 | 578 | * Return: 0 on success |
9ca9050b | 579 | * -EINVAL on incorrect values |
03b8d341 | 580 | * -EMFILE if open count exceeded. |
9ca9050b | 581 | */ |
781d0d89 | 582 | int mei_cl_link(struct mei_cl *cl, int id) |
9ca9050b | 583 | { |
90e0b5f1 | 584 | struct mei_device *dev; |
22f96a0e | 585 | long open_handle_count; |
9ca9050b | 586 | |
781d0d89 | 587 | if (WARN_ON(!cl || !cl->dev)) |
9ca9050b TW |
588 | return -EINVAL; |
589 | ||
90e0b5f1 TW |
590 | dev = cl->dev; |
591 | ||
83ce0741 | 592 | /* If Id is not assigned get one*/ |
781d0d89 TW |
593 | if (id == MEI_HOST_CLIENT_ID_ANY) |
594 | id = find_first_zero_bit(dev->host_clients_map, | |
595 | MEI_CLIENTS_MAX); | |
9ca9050b | 596 | |
781d0d89 | 597 | if (id >= MEI_CLIENTS_MAX) { |
2bf94cab | 598 | dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX); |
e036cc57 TW |
599 | return -EMFILE; |
600 | } | |
601 | ||
22f96a0e TW |
602 | open_handle_count = dev->open_handle_count + dev->iamthif_open_count; |
603 | if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) { | |
2bf94cab | 604 | dev_err(dev->dev, "open_handle_count exceeded %d", |
e036cc57 TW |
605 | MEI_MAX_OPEN_HANDLE_COUNT); |
606 | return -EMFILE; | |
9ca9050b TW |
607 | } |
608 | ||
781d0d89 TW |
609 | dev->open_handle_count++; |
610 | ||
611 | cl->host_client_id = id; | |
612 | list_add_tail(&cl->link, &dev->file_list); | |
613 | ||
614 | set_bit(id, dev->host_clients_map); | |
615 | ||
616 | cl->state = MEI_FILE_INITIALIZING; | |
617 | ||
c0abffbd | 618 | cl_dbg(dev, cl, "link cl\n"); |
781d0d89 | 619 | return 0; |
9ca9050b | 620 | } |
781d0d89 | 621 | |
9ca9050b | 622 | /** |
90e0b5f1 | 623 | * mei_cl_unlink - remove me_cl from the list |
9ca9050b | 624 | * |
393b148f | 625 | * @cl: host client |
ce23139c AU |
626 | * |
627 | * Return: always 0 | |
9ca9050b | 628 | */ |
90e0b5f1 | 629 | int mei_cl_unlink(struct mei_cl *cl) |
9ca9050b | 630 | { |
90e0b5f1 | 631 | struct mei_device *dev; |
90e0b5f1 | 632 | |
781d0d89 TW |
633 | /* don't shout on error exit path */ |
634 | if (!cl) | |
635 | return 0; | |
636 | ||
8e9a4a9a TW |
637 | /* wd and amthif might not be initialized */ |
638 | if (!cl->dev) | |
639 | return 0; | |
90e0b5f1 TW |
640 | |
641 | dev = cl->dev; | |
642 | ||
a14c44d8 TW |
643 | cl_dbg(dev, cl, "unlink client"); |
644 | ||
22f96a0e TW |
645 | if (dev->open_handle_count > 0) |
646 | dev->open_handle_count--; | |
647 | ||
648 | /* never clear the 0 bit */ | |
649 | if (cl->host_client_id) | |
650 | clear_bit(cl->host_client_id, dev->host_clients_map); | |
651 | ||
652 | list_del_init(&cl->link); | |
653 | ||
654 | cl->state = MEI_FILE_INITIALIZING; | |
655 | ||
90e0b5f1 | 656 | return 0; |
9ca9050b TW |
657 | } |
658 | ||
659 | ||
660 | void mei_host_client_init(struct work_struct *work) | |
661 | { | |
b7d88514 TW |
662 | struct mei_device *dev = |
663 | container_of(work, struct mei_device, init_work); | |
5ca2d388 | 664 | struct mei_me_client *me_cl; |
9ca9050b TW |
665 | |
666 | mutex_lock(&dev->device_lock); | |
667 | ||
9ca9050b | 668 | |
b7d88514 TW |
669 | me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid); |
670 | if (me_cl) | |
671 | mei_amthif_host_init(dev); | |
b43baf69 | 672 | mei_me_cl_put(me_cl); |
b7d88514 TW |
673 | |
674 | me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid); | |
675 | if (me_cl) | |
676 | mei_wd_host_init(dev); | |
b43baf69 | 677 | mei_me_cl_put(me_cl); |
b7d88514 TW |
678 | |
679 | me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid); | |
680 | if (me_cl) | |
681 | mei_nfc_host_init(dev); | |
b43baf69 | 682 | mei_me_cl_put(me_cl); |
59fcd7c6 | 683 | |
9ca9050b TW |
684 | |
685 | dev->dev_state = MEI_DEV_ENABLED; | |
6adb8efb | 686 | dev->reset_count = 0; |
9ca9050b | 687 | mutex_unlock(&dev->device_lock); |
04bb139a | 688 | |
2bf94cab TW |
689 | pm_runtime_mark_last_busy(dev->dev); |
690 | dev_dbg(dev->dev, "rpm: autosuspend\n"); | |
691 | pm_runtime_autosuspend(dev->dev); | |
9ca9050b TW |
692 | } |
693 | ||
6aae48ff | 694 | /** |
a8605ea2 | 695 | * mei_hbuf_acquire - try to acquire host buffer |
6aae48ff TW |
696 | * |
697 | * @dev: the device structure | |
a8605ea2 | 698 | * Return: true if host buffer was acquired |
6aae48ff TW |
699 | */ |
700 | bool mei_hbuf_acquire(struct mei_device *dev) | |
701 | { | |
04bb139a TW |
702 | if (mei_pg_state(dev) == MEI_PG_ON || |
703 | dev->pg_event == MEI_PG_EVENT_WAIT) { | |
2bf94cab | 704 | dev_dbg(dev->dev, "device is in pg\n"); |
04bb139a TW |
705 | return false; |
706 | } | |
707 | ||
6aae48ff | 708 | if (!dev->hbuf_is_ready) { |
2bf94cab | 709 | dev_dbg(dev->dev, "hbuf is not ready\n"); |
6aae48ff TW |
710 | return false; |
711 | } | |
712 | ||
713 | dev->hbuf_is_ready = false; | |
714 | ||
715 | return true; | |
716 | } | |
9ca9050b | 717 | |
3c666182 TW |
718 | /** |
719 | * mei_cl_set_disconnected - set disconnected state and clear | |
720 | * associated states and resources | |
721 | * | |
722 | * @cl: host client | |
723 | */ | |
724 | void mei_cl_set_disconnected(struct mei_cl *cl) | |
725 | { | |
726 | struct mei_device *dev = cl->dev; | |
727 | ||
728 | if (cl->state == MEI_FILE_DISCONNECTED || | |
729 | cl->state == MEI_FILE_INITIALIZING) | |
730 | return; | |
731 | ||
732 | cl->state = MEI_FILE_DISCONNECTED; | |
733 | mei_io_list_flush(&dev->ctrl_rd_list, cl); | |
734 | mei_io_list_flush(&dev->ctrl_wr_list, cl); | |
735 | cl->mei_flow_ctrl_creds = 0; | |
736 | cl->timer_count = 0; | |
737 | } | |
738 | ||
739 | /* | |
740 | * mei_cl_send_disconnect - send disconnect request | |
741 | * | |
742 | * @cl: host client | |
743 | * @cb: callback block | |
744 | * | |
745 | * Return: 0, OK; otherwise, error. | |
746 | */ | |
747 | static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb) | |
748 | { | |
749 | struct mei_device *dev; | |
750 | int ret; | |
751 | ||
752 | dev = cl->dev; | |
753 | ||
754 | ret = mei_hbm_cl_disconnect_req(dev, cl); | |
755 | cl->status = ret; | |
756 | if (ret) { | |
757 | cl->state = MEI_FILE_DISCONNECT_REPLY; | |
758 | return ret; | |
759 | } | |
760 | ||
761 | list_move_tail(&cb->list, &dev->ctrl_rd_list.list); | |
762 | cl->timer_count = MEI_CONNECT_TIMEOUT; | |
763 | ||
764 | return 0; | |
765 | } | |
766 | ||
767 | /** | |
768 | * mei_cl_irq_disconnect - processes close related operation from | |
769 | * interrupt thread context - send disconnect request | |
770 | * | |
771 | * @cl: client | |
772 | * @cb: callback block. | |
773 | * @cmpl_list: complete list. | |
774 | * | |
775 | * Return: 0, OK; otherwise, error. | |
776 | */ | |
777 | int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb, | |
778 | struct mei_cl_cb *cmpl_list) | |
779 | { | |
780 | struct mei_device *dev = cl->dev; | |
781 | u32 msg_slots; | |
782 | int slots; | |
783 | int ret; | |
784 | ||
785 | msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request)); | |
786 | slots = mei_hbuf_empty_slots(dev); | |
787 | ||
788 | if (slots < msg_slots) | |
789 | return -EMSGSIZE; | |
790 | ||
791 | ret = mei_cl_send_disconnect(cl, cb); | |
792 | if (ret) | |
793 | list_move_tail(&cb->list, &cmpl_list->list); | |
794 | ||
795 | return ret; | |
796 | } | |
797 | ||
798 | ||
799 | ||
9ca9050b | 800 | /** |
83ce0741 | 801 | * mei_cl_disconnect - disconnect host client from the me one |
9ca9050b | 802 | * |
90e0b5f1 | 803 | * @cl: host client |
9ca9050b TW |
804 | * |
805 | * Locking: called under "dev->device_lock" lock | |
806 | * | |
a8605ea2 | 807 | * Return: 0 on success, <0 on failure. |
9ca9050b | 808 | */ |
90e0b5f1 | 809 | int mei_cl_disconnect(struct mei_cl *cl) |
9ca9050b | 810 | { |
90e0b5f1 | 811 | struct mei_device *dev; |
9ca9050b | 812 | struct mei_cl_cb *cb; |
fe2f17eb | 813 | int rets; |
9ca9050b | 814 | |
90e0b5f1 | 815 | if (WARN_ON(!cl || !cl->dev)) |
9ca9050b TW |
816 | return -ENODEV; |
817 | ||
90e0b5f1 TW |
818 | dev = cl->dev; |
819 | ||
c0abffbd AU |
820 | cl_dbg(dev, cl, "disconnecting"); |
821 | ||
3c666182 | 822 | if (!mei_cl_is_connected(cl)) |
9ca9050b TW |
823 | return 0; |
824 | ||
2bf94cab | 825 | rets = pm_runtime_get(dev->dev); |
04bb139a | 826 | if (rets < 0 && rets != -EINPROGRESS) { |
2bf94cab | 827 | pm_runtime_put_noidle(dev->dev); |
04bb139a TW |
828 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
829 | return rets; | |
830 | } | |
831 | ||
3c666182 TW |
832 | cl->state = MEI_FILE_DISCONNECTING; |
833 | ||
bca67d68 TW |
834 | cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL); |
835 | rets = cb ? 0 : -ENOMEM; | |
836 | if (rets) | |
3c666182 TW |
837 | goto out; |
838 | ||
839 | cl_dbg(dev, cl, "add disconnect cb to control write list\n"); | |
840 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); | |
5a8373fb | 841 | |
6aae48ff | 842 | if (mei_hbuf_acquire(dev)) { |
3c666182 TW |
843 | rets = mei_cl_send_disconnect(cl, cb); |
844 | if (rets) { | |
c0abffbd | 845 | cl_err(dev, cl, "failed to disconnect.\n"); |
3c666182 | 846 | goto out; |
9ca9050b | 847 | } |
9ca9050b | 848 | } |
9ca9050b | 849 | |
3c666182 TW |
850 | mutex_unlock(&dev->device_lock); |
851 | wait_event_timeout(cl->wait, cl->state == MEI_FILE_DISCONNECT_REPLY, | |
852 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); | |
9ca9050b | 853 | mutex_lock(&dev->device_lock); |
fe2f17eb | 854 | |
3c666182 TW |
855 | rets = cl->status; |
856 | if (cl->state != MEI_FILE_DISCONNECT_REPLY) { | |
fe2f17eb AU |
857 | cl_dbg(dev, cl, "timeout on disconnect from FW client.\n"); |
858 | rets = -ETIME; | |
9ca9050b TW |
859 | } |
860 | ||
3c666182 TW |
861 | out: |
862 | /* we disconnect also on error */ | |
863 | mei_cl_set_disconnected(cl); | |
864 | if (!rets) | |
865 | cl_dbg(dev, cl, "successfully disconnected from FW client.\n"); | |
866 | ||
04bb139a | 867 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
2bf94cab TW |
868 | pm_runtime_mark_last_busy(dev->dev); |
869 | pm_runtime_put_autosuspend(dev->dev); | |
04bb139a | 870 | |
9ca9050b TW |
871 | mei_io_cb_free(cb); |
872 | return rets; | |
873 | } | |
874 | ||
875 | ||
876 | /** | |
90e0b5f1 TW |
877 | * mei_cl_is_other_connecting - checks if other |
878 | * client with the same me client id is connecting | |
9ca9050b | 879 | * |
9ca9050b TW |
880 | * @cl: private data of the file object |
881 | * | |
a8605ea2 | 882 | * Return: true if other client is connected, false - otherwise. |
9ca9050b | 883 | */ |
0c53357c | 884 | static bool mei_cl_is_other_connecting(struct mei_cl *cl) |
9ca9050b | 885 | { |
90e0b5f1 | 886 | struct mei_device *dev; |
0c53357c | 887 | struct mei_cl_cb *cb; |
90e0b5f1 TW |
888 | |
889 | dev = cl->dev; | |
890 | ||
0c53357c TW |
891 | list_for_each_entry(cb, &dev->ctrl_rd_list.list, list) { |
892 | if (cb->fop_type == MEI_FOP_CONNECT && | |
893 | cl->me_client_id == cb->cl->me_client_id) | |
90e0b5f1 | 894 | return true; |
9ca9050b | 895 | } |
90e0b5f1 TW |
896 | |
897 | return false; | |
9ca9050b TW |
898 | } |
899 | ||
0c53357c TW |
900 | /** |
901 | * mei_cl_send_connect - send connect request | |
902 | * | |
903 | * @cl: host client | |
904 | * @cb: callback block | |
905 | * | |
906 | * Return: 0, OK; otherwise, error. | |
907 | */ | |
908 | static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb) | |
909 | { | |
910 | struct mei_device *dev; | |
911 | int ret; | |
912 | ||
913 | dev = cl->dev; | |
914 | ||
915 | ret = mei_hbm_cl_connect_req(dev, cl); | |
916 | cl->status = ret; | |
917 | if (ret) { | |
918 | cl->state = MEI_FILE_DISCONNECT_REPLY; | |
919 | return ret; | |
920 | } | |
921 | ||
922 | list_move_tail(&cb->list, &dev->ctrl_rd_list.list); | |
923 | cl->timer_count = MEI_CONNECT_TIMEOUT; | |
924 | return 0; | |
925 | } | |
926 | ||
927 | /** | |
928 | * mei_cl_irq_connect - send connect request in irq_thread context | |
929 | * | |
930 | * @cl: host client | |
931 | * @cb: callback block | |
932 | * @cmpl_list: complete list | |
933 | * | |
934 | * Return: 0, OK; otherwise, error. | |
935 | */ | |
936 | int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb, | |
937 | struct mei_cl_cb *cmpl_list) | |
938 | { | |
939 | struct mei_device *dev = cl->dev; | |
940 | u32 msg_slots; | |
941 | int slots; | |
942 | int rets; | |
943 | ||
944 | msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request)); | |
945 | slots = mei_hbuf_empty_slots(dev); | |
946 | ||
947 | if (mei_cl_is_other_connecting(cl)) | |
948 | return 0; | |
949 | ||
950 | if (slots < msg_slots) | |
951 | return -EMSGSIZE; | |
952 | ||
953 | rets = mei_cl_send_connect(cl, cb); | |
954 | if (rets) | |
955 | list_move_tail(&cb->list, &cmpl_list->list); | |
956 | ||
957 | return rets; | |
958 | } | |
959 | ||
9f81abda | 960 | /** |
83ce0741 | 961 | * mei_cl_connect - connect host client to the me one |
9f81abda TW |
962 | * |
963 | * @cl: host client | |
a8605ea2 | 964 | * @file: pointer to file structure |
9f81abda TW |
965 | * |
966 | * Locking: called under "dev->device_lock" lock | |
967 | * | |
a8605ea2 | 968 | * Return: 0 on success, <0 on failure. |
9f81abda TW |
969 | */ |
970 | int mei_cl_connect(struct mei_cl *cl, struct file *file) | |
971 | { | |
972 | struct mei_device *dev; | |
973 | struct mei_cl_cb *cb; | |
9f81abda TW |
974 | int rets; |
975 | ||
976 | if (WARN_ON(!cl || !cl->dev)) | |
977 | return -ENODEV; | |
978 | ||
979 | dev = cl->dev; | |
980 | ||
2bf94cab | 981 | rets = pm_runtime_get(dev->dev); |
04bb139a | 982 | if (rets < 0 && rets != -EINPROGRESS) { |
2bf94cab | 983 | pm_runtime_put_noidle(dev->dev); |
04bb139a TW |
984 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
985 | return rets; | |
986 | } | |
987 | ||
bca67d68 TW |
988 | cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file); |
989 | rets = cb ? 0 : -ENOMEM; | |
990 | if (rets) | |
9f81abda | 991 | goto out; |
9f81abda | 992 | |
0c53357c TW |
993 | cl->state = MEI_FILE_CONNECTING; |
994 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); | |
995 | ||
6aae48ff TW |
996 | /* run hbuf acquire last so we don't have to undo */ |
997 | if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) { | |
0c53357c TW |
998 | rets = mei_cl_send_connect(cl, cb); |
999 | if (rets) | |
9f81abda | 1000 | goto out; |
9f81abda TW |
1001 | } |
1002 | ||
1003 | mutex_unlock(&dev->device_lock); | |
12f45ed4 | 1004 | wait_event_timeout(cl->wait, |
285e2996 | 1005 | (cl->state == MEI_FILE_CONNECTED || |
3c666182 | 1006 | cl->state == MEI_FILE_DISCONNECT_REPLY), |
285e2996 | 1007 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); |
9f81abda TW |
1008 | mutex_lock(&dev->device_lock); |
1009 | ||
f3de9b63 | 1010 | if (!mei_cl_is_connected(cl)) { |
0c53357c | 1011 | /* timeout or something went really wrong */ |
285e2996 AU |
1012 | if (!cl->status) |
1013 | cl->status = -EFAULT; | |
9f81abda TW |
1014 | } |
1015 | ||
1016 | rets = cl->status; | |
9f81abda | 1017 | out: |
04bb139a | 1018 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
2bf94cab TW |
1019 | pm_runtime_mark_last_busy(dev->dev); |
1020 | pm_runtime_put_autosuspend(dev->dev); | |
04bb139a | 1021 | |
9f81abda | 1022 | mei_io_cb_free(cb); |
0c53357c TW |
1023 | |
1024 | if (!mei_cl_is_connected(cl)) | |
1025 | mei_cl_set_disconnected(cl); | |
1026 | ||
9f81abda TW |
1027 | return rets; |
1028 | } | |
1029 | ||
03b8d341 TW |
1030 | /** |
1031 | * mei_cl_alloc_linked - allocate and link host client | |
1032 | * | |
1033 | * @dev: the device structure | |
1034 | * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one | |
1035 | * | |
1036 | * Return: cl on success ERR_PTR on failure | |
1037 | */ | |
1038 | struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id) | |
1039 | { | |
1040 | struct mei_cl *cl; | |
1041 | int ret; | |
1042 | ||
1043 | cl = mei_cl_allocate(dev); | |
1044 | if (!cl) { | |
1045 | ret = -ENOMEM; | |
1046 | goto err; | |
1047 | } | |
1048 | ||
1049 | ret = mei_cl_link(cl, id); | |
1050 | if (ret) | |
1051 | goto err; | |
1052 | ||
1053 | return cl; | |
1054 | err: | |
1055 | kfree(cl); | |
1056 | return ERR_PTR(ret); | |
1057 | } | |
1058 | ||
1059 | ||
1060 | ||
9ca9050b | 1061 | /** |
90e0b5f1 | 1062 | * mei_cl_flow_ctrl_creds - checks flow_control credits for cl. |
9ca9050b | 1063 | * |
9ca9050b TW |
1064 | * @cl: private data of the file object |
1065 | * | |
a8605ea2 | 1066 | * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise. |
9ca9050b TW |
1067 | * -ENOENT if mei_cl is not present |
1068 | * -EINVAL if single_recv_buf == 0 | |
1069 | */ | |
90e0b5f1 | 1070 | int mei_cl_flow_ctrl_creds(struct mei_cl *cl) |
9ca9050b | 1071 | { |
90e0b5f1 | 1072 | struct mei_device *dev; |
12d00665 | 1073 | struct mei_me_client *me_cl; |
79563db9 | 1074 | int rets = 0; |
9ca9050b | 1075 | |
90e0b5f1 TW |
1076 | if (WARN_ON(!cl || !cl->dev)) |
1077 | return -EINVAL; | |
1078 | ||
1079 | dev = cl->dev; | |
1080 | ||
9ca9050b TW |
1081 | if (cl->mei_flow_ctrl_creds > 0) |
1082 | return 1; | |
1083 | ||
2e5df413 | 1084 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
d320832f | 1085 | if (!me_cl) { |
12d00665 | 1086 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
d320832f | 1087 | return -ENOENT; |
9ca9050b | 1088 | } |
12d00665 | 1089 | |
79563db9 TW |
1090 | if (me_cl->mei_flow_ctrl_creds > 0) { |
1091 | rets = 1; | |
12d00665 | 1092 | if (WARN_ON(me_cl->props.single_recv_buf == 0)) |
79563db9 | 1093 | rets = -EINVAL; |
12d00665 | 1094 | } |
79563db9 TW |
1095 | mei_me_cl_put(me_cl); |
1096 | return rets; | |
9ca9050b TW |
1097 | } |
1098 | ||
1099 | /** | |
90e0b5f1 | 1100 | * mei_cl_flow_ctrl_reduce - reduces flow_control. |
9ca9050b | 1101 | * |
9ca9050b | 1102 | * @cl: private data of the file object |
393b148f | 1103 | * |
a8605ea2 | 1104 | * Return: |
9ca9050b TW |
1105 | * 0 on success |
1106 | * -ENOENT when me client is not found | |
1107 | * -EINVAL when ctrl credits are <= 0 | |
1108 | */ | |
90e0b5f1 | 1109 | int mei_cl_flow_ctrl_reduce(struct mei_cl *cl) |
9ca9050b | 1110 | { |
90e0b5f1 | 1111 | struct mei_device *dev; |
12d00665 | 1112 | struct mei_me_client *me_cl; |
79563db9 | 1113 | int rets; |
9ca9050b | 1114 | |
90e0b5f1 TW |
1115 | if (WARN_ON(!cl || !cl->dev)) |
1116 | return -EINVAL; | |
1117 | ||
1118 | dev = cl->dev; | |
1119 | ||
2e5df413 | 1120 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
d320832f | 1121 | if (!me_cl) { |
12d00665 | 1122 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
d320832f | 1123 | return -ENOENT; |
12d00665 | 1124 | } |
9ca9050b | 1125 | |
d320832f | 1126 | if (me_cl->props.single_recv_buf) { |
79563db9 TW |
1127 | if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) { |
1128 | rets = -EINVAL; | |
1129 | goto out; | |
1130 | } | |
12d00665 AU |
1131 | me_cl->mei_flow_ctrl_creds--; |
1132 | } else { | |
79563db9 TW |
1133 | if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) { |
1134 | rets = -EINVAL; | |
1135 | goto out; | |
1136 | } | |
12d00665 | 1137 | cl->mei_flow_ctrl_creds--; |
9ca9050b | 1138 | } |
79563db9 TW |
1139 | rets = 0; |
1140 | out: | |
1141 | mei_me_cl_put(me_cl); | |
1142 | return rets; | |
9ca9050b TW |
1143 | } |
1144 | ||
ab841160 | 1145 | /** |
393b148f | 1146 | * mei_cl_read_start - the start read client message function. |
ab841160 | 1147 | * |
90e0b5f1 | 1148 | * @cl: host client |
ce23139c | 1149 | * @length: number of bytes to read |
bca67d68 | 1150 | * @fp: pointer to file structure |
ab841160 | 1151 | * |
a8605ea2 | 1152 | * Return: 0 on success, <0 on failure. |
ab841160 | 1153 | */ |
bca67d68 | 1154 | int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp) |
ab841160 | 1155 | { |
90e0b5f1 | 1156 | struct mei_device *dev; |
ab841160 | 1157 | struct mei_cl_cb *cb; |
d320832f | 1158 | struct mei_me_client *me_cl; |
664df38b | 1159 | int rets; |
ab841160 | 1160 | |
90e0b5f1 TW |
1161 | if (WARN_ON(!cl || !cl->dev)) |
1162 | return -ENODEV; | |
1163 | ||
1164 | dev = cl->dev; | |
1165 | ||
b950ac1d | 1166 | if (!mei_cl_is_connected(cl)) |
ab841160 OW |
1167 | return -ENODEV; |
1168 | ||
a9bed610 TW |
1169 | /* HW currently supports only one pending read */ |
1170 | if (!list_empty(&cl->rd_pending)) | |
ab841160 | 1171 | return -EBUSY; |
a9bed610 | 1172 | |
d880f329 | 1173 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
d320832f | 1174 | if (!me_cl) { |
c0abffbd | 1175 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
7ca96aa2 | 1176 | return -ENOTTY; |
664df38b | 1177 | } |
79563db9 TW |
1178 | /* always allocate at least client max message */ |
1179 | length = max_t(size_t, length, me_cl->props.max_msg_length); | |
1180 | mei_me_cl_put(me_cl); | |
ab841160 | 1181 | |
2bf94cab | 1182 | rets = pm_runtime_get(dev->dev); |
04bb139a | 1183 | if (rets < 0 && rets != -EINPROGRESS) { |
2bf94cab | 1184 | pm_runtime_put_noidle(dev->dev); |
04bb139a TW |
1185 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
1186 | return rets; | |
1187 | } | |
1188 | ||
bca67d68 TW |
1189 | cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp); |
1190 | rets = cb ? 0 : -ENOMEM; | |
664df38b | 1191 | if (rets) |
04bb139a | 1192 | goto out; |
ab841160 | 1193 | |
6aae48ff | 1194 | if (mei_hbuf_acquire(dev)) { |
86113500 AU |
1195 | rets = mei_hbm_cl_flow_control_req(dev, cl); |
1196 | if (rets < 0) | |
04bb139a | 1197 | goto out; |
04bb139a | 1198 | |
a9bed610 | 1199 | list_add_tail(&cb->list, &cl->rd_pending); |
ab841160 | 1200 | } else { |
fb601adb | 1201 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
ab841160 | 1202 | } |
accb884b | 1203 | |
04bb139a TW |
1204 | out: |
1205 | cl_dbg(dev, cl, "rpm: autosuspend\n"); | |
2bf94cab TW |
1206 | pm_runtime_mark_last_busy(dev->dev); |
1207 | pm_runtime_put_autosuspend(dev->dev); | |
04bb139a TW |
1208 | |
1209 | if (rets) | |
1210 | mei_io_cb_free(cb); | |
1211 | ||
ab841160 OW |
1212 | return rets; |
1213 | } | |
1214 | ||
21767546 | 1215 | /** |
9d098192 | 1216 | * mei_cl_irq_write - write a message to device |
21767546 TW |
1217 | * from the interrupt thread context |
1218 | * | |
1219 | * @cl: client | |
1220 | * @cb: callback block. | |
21767546 TW |
1221 | * @cmpl_list: complete list. |
1222 | * | |
a8605ea2 | 1223 | * Return: 0, OK; otherwise error. |
21767546 | 1224 | */ |
9d098192 TW |
1225 | int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb, |
1226 | struct mei_cl_cb *cmpl_list) | |
21767546 | 1227 | { |
136698e5 TW |
1228 | struct mei_device *dev; |
1229 | struct mei_msg_data *buf; | |
21767546 | 1230 | struct mei_msg_hdr mei_hdr; |
136698e5 TW |
1231 | size_t len; |
1232 | u32 msg_slots; | |
9d098192 | 1233 | int slots; |
2ebf8c94 | 1234 | int rets; |
21767546 | 1235 | |
136698e5 TW |
1236 | if (WARN_ON(!cl || !cl->dev)) |
1237 | return -ENODEV; | |
1238 | ||
1239 | dev = cl->dev; | |
1240 | ||
5db7514d | 1241 | buf = &cb->buf; |
136698e5 TW |
1242 | |
1243 | rets = mei_cl_flow_ctrl_creds(cl); | |
1244 | if (rets < 0) | |
1245 | return rets; | |
1246 | ||
1247 | if (rets == 0) { | |
04bb139a | 1248 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
136698e5 TW |
1249 | return 0; |
1250 | } | |
1251 | ||
9d098192 | 1252 | slots = mei_hbuf_empty_slots(dev); |
136698e5 TW |
1253 | len = buf->size - cb->buf_idx; |
1254 | msg_slots = mei_data2slots(len); | |
1255 | ||
21767546 TW |
1256 | mei_hdr.host_addr = cl->host_client_id; |
1257 | mei_hdr.me_addr = cl->me_client_id; | |
1258 | mei_hdr.reserved = 0; | |
479327fc | 1259 | mei_hdr.internal = cb->internal; |
21767546 | 1260 | |
9d098192 | 1261 | if (slots >= msg_slots) { |
21767546 TW |
1262 | mei_hdr.length = len; |
1263 | mei_hdr.msg_complete = 1; | |
1264 | /* Split the message only if we can write the whole host buffer */ | |
9d098192 TW |
1265 | } else if (slots == dev->hbuf_depth) { |
1266 | msg_slots = slots; | |
1267 | len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr); | |
21767546 TW |
1268 | mei_hdr.length = len; |
1269 | mei_hdr.msg_complete = 0; | |
1270 | } else { | |
1271 | /* wait for next time the host buffer is empty */ | |
1272 | return 0; | |
1273 | } | |
1274 | ||
c0abffbd | 1275 | cl_dbg(dev, cl, "buf: size = %d idx = %lu\n", |
5db7514d | 1276 | cb->buf.size, cb->buf_idx); |
21767546 | 1277 | |
136698e5 | 1278 | rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx); |
2ebf8c94 TW |
1279 | if (rets) { |
1280 | cl->status = rets; | |
21767546 | 1281 | list_move_tail(&cb->list, &cmpl_list->list); |
2ebf8c94 | 1282 | return rets; |
21767546 TW |
1283 | } |
1284 | ||
1285 | cl->status = 0; | |
4dfaa9f7 | 1286 | cl->writing_state = MEI_WRITING; |
21767546 | 1287 | cb->buf_idx += mei_hdr.length; |
8660172e | 1288 | cb->completed = mei_hdr.msg_complete == 1; |
4dfaa9f7 | 1289 | |
21767546 TW |
1290 | if (mei_hdr.msg_complete) { |
1291 | if (mei_cl_flow_ctrl_reduce(cl)) | |
2ebf8c94 | 1292 | return -EIO; |
21767546 TW |
1293 | list_move_tail(&cb->list, &dev->write_waiting_list.list); |
1294 | } | |
1295 | ||
1296 | return 0; | |
1297 | } | |
1298 | ||
4234a6de TW |
1299 | /** |
1300 | * mei_cl_write - submit a write cb to mei device | |
a8605ea2 | 1301 | * assumes device_lock is locked |
4234a6de TW |
1302 | * |
1303 | * @cl: host client | |
a8605ea2 | 1304 | * @cb: write callback with filled data |
ce23139c | 1305 | * @blocking: block until completed |
4234a6de | 1306 | * |
a8605ea2 | 1307 | * Return: number of bytes sent on success, <0 on failure. |
4234a6de TW |
1308 | */ |
1309 | int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking) | |
1310 | { | |
1311 | struct mei_device *dev; | |
1312 | struct mei_msg_data *buf; | |
1313 | struct mei_msg_hdr mei_hdr; | |
1314 | int rets; | |
1315 | ||
1316 | ||
1317 | if (WARN_ON(!cl || !cl->dev)) | |
1318 | return -ENODEV; | |
1319 | ||
1320 | if (WARN_ON(!cb)) | |
1321 | return -EINVAL; | |
1322 | ||
1323 | dev = cl->dev; | |
1324 | ||
1325 | ||
5db7514d | 1326 | buf = &cb->buf; |
4234a6de | 1327 | |
0a01e974 | 1328 | cl_dbg(dev, cl, "size=%d\n", buf->size); |
4234a6de | 1329 | |
2bf94cab | 1330 | rets = pm_runtime_get(dev->dev); |
04bb139a | 1331 | if (rets < 0 && rets != -EINPROGRESS) { |
2bf94cab | 1332 | pm_runtime_put_noidle(dev->dev); |
04bb139a TW |
1333 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
1334 | return rets; | |
1335 | } | |
4234a6de | 1336 | |
6aae48ff TW |
1337 | cb->buf_idx = 0; |
1338 | cl->writing_state = MEI_IDLE; | |
1339 | ||
1340 | mei_hdr.host_addr = cl->host_client_id; | |
1341 | mei_hdr.me_addr = cl->me_client_id; | |
1342 | mei_hdr.reserved = 0; | |
1343 | mei_hdr.msg_complete = 0; | |
1344 | mei_hdr.internal = cb->internal; | |
4234a6de TW |
1345 | |
1346 | rets = mei_cl_flow_ctrl_creds(cl); | |
1347 | if (rets < 0) | |
1348 | goto err; | |
1349 | ||
6aae48ff TW |
1350 | if (rets == 0) { |
1351 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); | |
1352 | rets = buf->size; | |
1353 | goto out; | |
1354 | } | |
1355 | if (!mei_hbuf_acquire(dev)) { | |
1356 | cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n"); | |
4234a6de TW |
1357 | rets = buf->size; |
1358 | goto out; | |
1359 | } | |
4234a6de TW |
1360 | |
1361 | /* Check for a maximum length */ | |
1362 | if (buf->size > mei_hbuf_max_len(dev)) { | |
1363 | mei_hdr.length = mei_hbuf_max_len(dev); | |
1364 | mei_hdr.msg_complete = 0; | |
1365 | } else { | |
1366 | mei_hdr.length = buf->size; | |
1367 | mei_hdr.msg_complete = 1; | |
1368 | } | |
1369 | ||
2ebf8c94 TW |
1370 | rets = mei_write_message(dev, &mei_hdr, buf->data); |
1371 | if (rets) | |
4234a6de | 1372 | goto err; |
4234a6de TW |
1373 | |
1374 | cl->writing_state = MEI_WRITING; | |
1375 | cb->buf_idx = mei_hdr.length; | |
8660172e | 1376 | cb->completed = mei_hdr.msg_complete == 1; |
4234a6de | 1377 | |
4234a6de TW |
1378 | out: |
1379 | if (mei_hdr.msg_complete) { | |
7ca96aa2 AU |
1380 | rets = mei_cl_flow_ctrl_reduce(cl); |
1381 | if (rets < 0) | |
4234a6de | 1382 | goto err; |
7ca96aa2 | 1383 | |
4234a6de TW |
1384 | list_add_tail(&cb->list, &dev->write_waiting_list.list); |
1385 | } else { | |
1386 | list_add_tail(&cb->list, &dev->write_list.list); | |
1387 | } | |
1388 | ||
1389 | ||
1390 | if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) { | |
1391 | ||
1392 | mutex_unlock(&dev->device_lock); | |
7ca96aa2 AU |
1393 | rets = wait_event_interruptible(cl->tx_wait, |
1394 | cl->writing_state == MEI_WRITE_COMPLETE); | |
4234a6de | 1395 | mutex_lock(&dev->device_lock); |
7ca96aa2 AU |
1396 | /* wait_event_interruptible returns -ERESTARTSYS */ |
1397 | if (rets) { | |
1398 | if (signal_pending(current)) | |
1399 | rets = -EINTR; | |
1400 | goto err; | |
1401 | } | |
4234a6de | 1402 | } |
7ca96aa2 AU |
1403 | |
1404 | rets = buf->size; | |
4234a6de | 1405 | err: |
04bb139a | 1406 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
2bf94cab TW |
1407 | pm_runtime_mark_last_busy(dev->dev); |
1408 | pm_runtime_put_autosuspend(dev->dev); | |
04bb139a | 1409 | |
4234a6de TW |
1410 | return rets; |
1411 | } | |
1412 | ||
1413 | ||
db086fa9 TW |
1414 | /** |
1415 | * mei_cl_complete - processes completed operation for a client | |
1416 | * | |
1417 | * @cl: private data of the file object. | |
1418 | * @cb: callback block. | |
1419 | */ | |
1420 | void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb) | |
1421 | { | |
3c666182 TW |
1422 | switch (cb->fop_type) { |
1423 | case MEI_FOP_WRITE: | |
db086fa9 | 1424 | mei_io_cb_free(cb); |
db086fa9 TW |
1425 | cl->writing_state = MEI_WRITE_COMPLETE; |
1426 | if (waitqueue_active(&cl->tx_wait)) | |
1427 | wake_up_interruptible(&cl->tx_wait); | |
3c666182 | 1428 | break; |
db086fa9 | 1429 | |
3c666182 | 1430 | case MEI_FOP_READ: |
a9bed610 | 1431 | list_add_tail(&cb->list, &cl->rd_completed); |
db086fa9 | 1432 | if (waitqueue_active(&cl->rx_wait)) |
1d9013f0 | 1433 | wake_up_interruptible_all(&cl->rx_wait); |
db086fa9 TW |
1434 | else |
1435 | mei_cl_bus_rx_event(cl); | |
3c666182 TW |
1436 | break; |
1437 | ||
1438 | case MEI_FOP_CONNECT: | |
1439 | case MEI_FOP_DISCONNECT: | |
1440 | if (waitqueue_active(&cl->wait)) | |
1441 | wake_up(&cl->wait); | |
db086fa9 | 1442 | |
3c666182 TW |
1443 | break; |
1444 | default: | |
1445 | BUG_ON(0); | |
db086fa9 TW |
1446 | } |
1447 | } | |
1448 | ||
4234a6de | 1449 | |
074b4c01 TW |
1450 | /** |
1451 | * mei_cl_all_disconnect - disconnect forcefully all connected clients | |
1452 | * | |
a8605ea2 | 1453 | * @dev: mei device |
074b4c01 | 1454 | */ |
074b4c01 TW |
1455 | void mei_cl_all_disconnect(struct mei_device *dev) |
1456 | { | |
31f88f57 | 1457 | struct mei_cl *cl; |
074b4c01 | 1458 | |
3c666182 TW |
1459 | list_for_each_entry(cl, &dev->file_list, link) |
1460 | mei_cl_set_disconnected(cl); | |
074b4c01 TW |
1461 | } |
1462 | ||
1463 | ||
1464 | /** | |
5290801c | 1465 | * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted |
074b4c01 | 1466 | * |
a8605ea2 | 1467 | * @dev: mei device |
074b4c01 | 1468 | */ |
5290801c | 1469 | void mei_cl_all_wakeup(struct mei_device *dev) |
074b4c01 | 1470 | { |
31f88f57 | 1471 | struct mei_cl *cl; |
92db1555 | 1472 | |
31f88f57 | 1473 | list_for_each_entry(cl, &dev->file_list, link) { |
074b4c01 | 1474 | if (waitqueue_active(&cl->rx_wait)) { |
c0abffbd | 1475 | cl_dbg(dev, cl, "Waking up reading client!\n"); |
074b4c01 TW |
1476 | wake_up_interruptible(&cl->rx_wait); |
1477 | } | |
5290801c | 1478 | if (waitqueue_active(&cl->tx_wait)) { |
c0abffbd | 1479 | cl_dbg(dev, cl, "Waking up writing client!\n"); |
5290801c TW |
1480 | wake_up_interruptible(&cl->tx_wait); |
1481 | } | |
074b4c01 TW |
1482 | } |
1483 | } | |
1484 | ||
1485 | /** | |
1486 | * mei_cl_all_write_clear - clear all pending writes | |
a8605ea2 AU |
1487 | * |
1488 | * @dev: mei device | |
074b4c01 TW |
1489 | */ |
1490 | void mei_cl_all_write_clear(struct mei_device *dev) | |
1491 | { | |
cc99ecfd TW |
1492 | mei_io_list_free(&dev->write_list, NULL); |
1493 | mei_io_list_free(&dev->write_waiting_list, NULL); | |
074b4c01 TW |
1494 | } |
1495 | ||
1496 |