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 TW |
548 | cl->writing_state = MEI_IDLE; |
549 | cl->dev = dev; | |
550 | } | |
551 | ||
552 | /** | |
553 | * mei_cl_allocate - allocates cl structure and sets it up. | |
554 | * | |
555 | * @dev: mei device | |
a8605ea2 | 556 | * Return: The allocated file or NULL on failure |
9ca9050b TW |
557 | */ |
558 | struct mei_cl *mei_cl_allocate(struct mei_device *dev) | |
559 | { | |
560 | struct mei_cl *cl; | |
561 | ||
562 | cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL); | |
563 | if (!cl) | |
564 | return NULL; | |
565 | ||
566 | mei_cl_init(cl, dev); | |
567 | ||
568 | return cl; | |
569 | } | |
570 | ||
3908be6f AU |
571 | /** |
572 | * mei_cl_link - allocate host id in the host map | |
9ca9050b | 573 | * |
3908be6f | 574 | * @cl: host client |
03b8d341 | 575 | * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one |
393b148f | 576 | * |
a8605ea2 | 577 | * Return: 0 on success |
9ca9050b | 578 | * -EINVAL on incorrect values |
03b8d341 | 579 | * -EMFILE if open count exceeded. |
9ca9050b | 580 | */ |
781d0d89 | 581 | int mei_cl_link(struct mei_cl *cl, int id) |
9ca9050b | 582 | { |
90e0b5f1 | 583 | struct mei_device *dev; |
22f96a0e | 584 | long open_handle_count; |
9ca9050b | 585 | |
781d0d89 | 586 | if (WARN_ON(!cl || !cl->dev)) |
9ca9050b TW |
587 | return -EINVAL; |
588 | ||
90e0b5f1 TW |
589 | dev = cl->dev; |
590 | ||
83ce0741 | 591 | /* If Id is not assigned get one*/ |
781d0d89 TW |
592 | if (id == MEI_HOST_CLIENT_ID_ANY) |
593 | id = find_first_zero_bit(dev->host_clients_map, | |
594 | MEI_CLIENTS_MAX); | |
9ca9050b | 595 | |
781d0d89 | 596 | if (id >= MEI_CLIENTS_MAX) { |
2bf94cab | 597 | dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX); |
e036cc57 TW |
598 | return -EMFILE; |
599 | } | |
600 | ||
22f96a0e TW |
601 | open_handle_count = dev->open_handle_count + dev->iamthif_open_count; |
602 | if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) { | |
2bf94cab | 603 | dev_err(dev->dev, "open_handle_count exceeded %d", |
e036cc57 TW |
604 | MEI_MAX_OPEN_HANDLE_COUNT); |
605 | return -EMFILE; | |
9ca9050b TW |
606 | } |
607 | ||
781d0d89 TW |
608 | dev->open_handle_count++; |
609 | ||
610 | cl->host_client_id = id; | |
611 | list_add_tail(&cl->link, &dev->file_list); | |
612 | ||
613 | set_bit(id, dev->host_clients_map); | |
614 | ||
615 | cl->state = MEI_FILE_INITIALIZING; | |
616 | ||
c0abffbd | 617 | cl_dbg(dev, cl, "link cl\n"); |
781d0d89 | 618 | return 0; |
9ca9050b | 619 | } |
781d0d89 | 620 | |
9ca9050b | 621 | /** |
90e0b5f1 | 622 | * mei_cl_unlink - remove me_cl from the list |
9ca9050b | 623 | * |
393b148f | 624 | * @cl: host client |
ce23139c AU |
625 | * |
626 | * Return: always 0 | |
9ca9050b | 627 | */ |
90e0b5f1 | 628 | int mei_cl_unlink(struct mei_cl *cl) |
9ca9050b | 629 | { |
90e0b5f1 | 630 | struct mei_device *dev; |
90e0b5f1 | 631 | |
781d0d89 TW |
632 | /* don't shout on error exit path */ |
633 | if (!cl) | |
634 | return 0; | |
635 | ||
8e9a4a9a TW |
636 | /* wd and amthif might not be initialized */ |
637 | if (!cl->dev) | |
638 | return 0; | |
90e0b5f1 TW |
639 | |
640 | dev = cl->dev; | |
641 | ||
a14c44d8 TW |
642 | cl_dbg(dev, cl, "unlink client"); |
643 | ||
22f96a0e TW |
644 | if (dev->open_handle_count > 0) |
645 | dev->open_handle_count--; | |
646 | ||
647 | /* never clear the 0 bit */ | |
648 | if (cl->host_client_id) | |
649 | clear_bit(cl->host_client_id, dev->host_clients_map); | |
650 | ||
651 | list_del_init(&cl->link); | |
652 | ||
653 | cl->state = MEI_FILE_INITIALIZING; | |
654 | ||
90e0b5f1 | 655 | return 0; |
9ca9050b TW |
656 | } |
657 | ||
658 | ||
659 | void mei_host_client_init(struct work_struct *work) | |
660 | { | |
b7d88514 TW |
661 | struct mei_device *dev = |
662 | container_of(work, struct mei_device, init_work); | |
5ca2d388 | 663 | struct mei_me_client *me_cl; |
9ca9050b TW |
664 | |
665 | mutex_lock(&dev->device_lock); | |
666 | ||
9ca9050b | 667 | |
b7d88514 TW |
668 | me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid); |
669 | if (me_cl) | |
670 | mei_amthif_host_init(dev); | |
b43baf69 | 671 | mei_me_cl_put(me_cl); |
b7d88514 TW |
672 | |
673 | me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid); | |
674 | if (me_cl) | |
675 | mei_wd_host_init(dev); | |
b43baf69 | 676 | mei_me_cl_put(me_cl); |
b7d88514 TW |
677 | |
678 | me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid); | |
679 | if (me_cl) | |
680 | mei_nfc_host_init(dev); | |
b43baf69 | 681 | mei_me_cl_put(me_cl); |
59fcd7c6 | 682 | |
9ca9050b TW |
683 | |
684 | dev->dev_state = MEI_DEV_ENABLED; | |
6adb8efb | 685 | dev->reset_count = 0; |
9ca9050b | 686 | mutex_unlock(&dev->device_lock); |
04bb139a | 687 | |
2bf94cab TW |
688 | pm_runtime_mark_last_busy(dev->dev); |
689 | dev_dbg(dev->dev, "rpm: autosuspend\n"); | |
690 | pm_runtime_autosuspend(dev->dev); | |
9ca9050b TW |
691 | } |
692 | ||
6aae48ff | 693 | /** |
a8605ea2 | 694 | * mei_hbuf_acquire - try to acquire host buffer |
6aae48ff TW |
695 | * |
696 | * @dev: the device structure | |
a8605ea2 | 697 | * Return: true if host buffer was acquired |
6aae48ff TW |
698 | */ |
699 | bool mei_hbuf_acquire(struct mei_device *dev) | |
700 | { | |
04bb139a TW |
701 | if (mei_pg_state(dev) == MEI_PG_ON || |
702 | dev->pg_event == MEI_PG_EVENT_WAIT) { | |
2bf94cab | 703 | dev_dbg(dev->dev, "device is in pg\n"); |
04bb139a TW |
704 | return false; |
705 | } | |
706 | ||
6aae48ff | 707 | if (!dev->hbuf_is_ready) { |
2bf94cab | 708 | dev_dbg(dev->dev, "hbuf is not ready\n"); |
6aae48ff TW |
709 | return false; |
710 | } | |
711 | ||
712 | dev->hbuf_is_ready = false; | |
713 | ||
714 | return true; | |
715 | } | |
9ca9050b TW |
716 | |
717 | /** | |
83ce0741 | 718 | * mei_cl_disconnect - disconnect host client from the me one |
9ca9050b | 719 | * |
90e0b5f1 | 720 | * @cl: host client |
9ca9050b TW |
721 | * |
722 | * Locking: called under "dev->device_lock" lock | |
723 | * | |
a8605ea2 | 724 | * Return: 0 on success, <0 on failure. |
9ca9050b | 725 | */ |
90e0b5f1 | 726 | int mei_cl_disconnect(struct mei_cl *cl) |
9ca9050b | 727 | { |
90e0b5f1 | 728 | struct mei_device *dev; |
9ca9050b | 729 | struct mei_cl_cb *cb; |
fe2f17eb | 730 | int rets; |
9ca9050b | 731 | |
90e0b5f1 | 732 | if (WARN_ON(!cl || !cl->dev)) |
9ca9050b TW |
733 | return -ENODEV; |
734 | ||
90e0b5f1 TW |
735 | dev = cl->dev; |
736 | ||
c0abffbd AU |
737 | cl_dbg(dev, cl, "disconnecting"); |
738 | ||
9ca9050b TW |
739 | if (cl->state != MEI_FILE_DISCONNECTING) |
740 | return 0; | |
741 | ||
2bf94cab | 742 | rets = pm_runtime_get(dev->dev); |
04bb139a | 743 | if (rets < 0 && rets != -EINPROGRESS) { |
2bf94cab | 744 | pm_runtime_put_noidle(dev->dev); |
04bb139a TW |
745 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
746 | return rets; | |
747 | } | |
748 | ||
bca67d68 TW |
749 | cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL); |
750 | rets = cb ? 0 : -ENOMEM; | |
751 | if (rets) | |
04bb139a | 752 | goto free; |
5a8373fb | 753 | |
6aae48ff | 754 | if (mei_hbuf_acquire(dev)) { |
9ca9050b TW |
755 | if (mei_hbm_cl_disconnect_req(dev, cl)) { |
756 | rets = -ENODEV; | |
c0abffbd | 757 | cl_err(dev, cl, "failed to disconnect.\n"); |
9ca9050b TW |
758 | goto free; |
759 | } | |
22b987a3 | 760 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
9ca9050b TW |
761 | mdelay(10); /* Wait for hardware disconnection ready */ |
762 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); | |
763 | } else { | |
c0abffbd | 764 | cl_dbg(dev, cl, "add disconnect cb to control write list\n"); |
9ca9050b TW |
765 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
766 | ||
767 | } | |
768 | mutex_unlock(&dev->device_lock); | |
769 | ||
12f45ed4 | 770 | wait_event_timeout(cl->wait, |
9ca9050b TW |
771 | MEI_FILE_DISCONNECTED == cl->state, |
772 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); | |
773 | ||
774 | mutex_lock(&dev->device_lock); | |
fe2f17eb | 775 | |
9ca9050b TW |
776 | if (MEI_FILE_DISCONNECTED == cl->state) { |
777 | rets = 0; | |
c0abffbd | 778 | cl_dbg(dev, cl, "successfully disconnected from FW client.\n"); |
9ca9050b | 779 | } else { |
fe2f17eb AU |
780 | cl_dbg(dev, cl, "timeout on disconnect from FW client.\n"); |
781 | rets = -ETIME; | |
9ca9050b TW |
782 | } |
783 | ||
784 | mei_io_list_flush(&dev->ctrl_rd_list, cl); | |
785 | mei_io_list_flush(&dev->ctrl_wr_list, cl); | |
786 | free: | |
04bb139a | 787 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
2bf94cab TW |
788 | pm_runtime_mark_last_busy(dev->dev); |
789 | pm_runtime_put_autosuspend(dev->dev); | |
04bb139a | 790 | |
9ca9050b TW |
791 | mei_io_cb_free(cb); |
792 | return rets; | |
793 | } | |
794 | ||
795 | ||
796 | /** | |
90e0b5f1 TW |
797 | * mei_cl_is_other_connecting - checks if other |
798 | * client with the same me client id is connecting | |
9ca9050b | 799 | * |
9ca9050b TW |
800 | * @cl: private data of the file object |
801 | * | |
a8605ea2 | 802 | * Return: true if other client is connected, false - otherwise. |
9ca9050b | 803 | */ |
90e0b5f1 | 804 | bool mei_cl_is_other_connecting(struct mei_cl *cl) |
9ca9050b | 805 | { |
90e0b5f1 | 806 | struct mei_device *dev; |
31f88f57 | 807 | struct mei_cl *ocl; /* the other client */ |
9ca9050b | 808 | |
90e0b5f1 TW |
809 | if (WARN_ON(!cl || !cl->dev)) |
810 | return false; | |
811 | ||
812 | dev = cl->dev; | |
813 | ||
31f88f57 TW |
814 | list_for_each_entry(ocl, &dev->file_list, link) { |
815 | if (ocl->state == MEI_FILE_CONNECTING && | |
816 | ocl != cl && | |
817 | cl->me_client_id == ocl->me_client_id) | |
90e0b5f1 | 818 | return true; |
9ca9050b TW |
819 | |
820 | } | |
90e0b5f1 TW |
821 | |
822 | return false; | |
9ca9050b TW |
823 | } |
824 | ||
9f81abda | 825 | /** |
83ce0741 | 826 | * mei_cl_connect - connect host client to the me one |
9f81abda TW |
827 | * |
828 | * @cl: host client | |
a8605ea2 | 829 | * @file: pointer to file structure |
9f81abda TW |
830 | * |
831 | * Locking: called under "dev->device_lock" lock | |
832 | * | |
a8605ea2 | 833 | * Return: 0 on success, <0 on failure. |
9f81abda TW |
834 | */ |
835 | int mei_cl_connect(struct mei_cl *cl, struct file *file) | |
836 | { | |
837 | struct mei_device *dev; | |
838 | struct mei_cl_cb *cb; | |
9f81abda TW |
839 | int rets; |
840 | ||
841 | if (WARN_ON(!cl || !cl->dev)) | |
842 | return -ENODEV; | |
843 | ||
844 | dev = cl->dev; | |
845 | ||
2bf94cab | 846 | rets = pm_runtime_get(dev->dev); |
04bb139a | 847 | if (rets < 0 && rets != -EINPROGRESS) { |
2bf94cab | 848 | pm_runtime_put_noidle(dev->dev); |
04bb139a TW |
849 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
850 | return rets; | |
851 | } | |
852 | ||
bca67d68 TW |
853 | cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file); |
854 | rets = cb ? 0 : -ENOMEM; | |
855 | if (rets) | |
9f81abda | 856 | goto out; |
9f81abda | 857 | |
6aae48ff TW |
858 | /* run hbuf acquire last so we don't have to undo */ |
859 | if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) { | |
e4d8270e | 860 | cl->state = MEI_FILE_CONNECTING; |
9f81abda TW |
861 | if (mei_hbm_cl_connect_req(dev, cl)) { |
862 | rets = -ENODEV; | |
863 | goto out; | |
864 | } | |
865 | cl->timer_count = MEI_CONNECT_TIMEOUT; | |
866 | list_add_tail(&cb->list, &dev->ctrl_rd_list.list); | |
867 | } else { | |
73ab4232 | 868 | cl->state = MEI_FILE_INITIALIZING; |
9f81abda TW |
869 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
870 | } | |
871 | ||
872 | mutex_unlock(&dev->device_lock); | |
12f45ed4 | 873 | wait_event_timeout(cl->wait, |
285e2996 AU |
874 | (cl->state == MEI_FILE_CONNECTED || |
875 | cl->state == MEI_FILE_DISCONNECTED), | |
876 | mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT)); | |
9f81abda TW |
877 | mutex_lock(&dev->device_lock); |
878 | ||
879 | if (cl->state != MEI_FILE_CONNECTED) { | |
3e37ebb7 | 880 | cl->state = MEI_FILE_DISCONNECTED; |
285e2996 AU |
881 | /* something went really wrong */ |
882 | if (!cl->status) | |
883 | cl->status = -EFAULT; | |
9f81abda TW |
884 | |
885 | mei_io_list_flush(&dev->ctrl_rd_list, cl); | |
886 | mei_io_list_flush(&dev->ctrl_wr_list, cl); | |
9f81abda TW |
887 | } |
888 | ||
889 | rets = cl->status; | |
890 | ||
891 | out: | |
04bb139a | 892 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
2bf94cab TW |
893 | pm_runtime_mark_last_busy(dev->dev); |
894 | pm_runtime_put_autosuspend(dev->dev); | |
04bb139a | 895 | |
9f81abda TW |
896 | mei_io_cb_free(cb); |
897 | return rets; | |
898 | } | |
899 | ||
03b8d341 TW |
900 | /** |
901 | * mei_cl_alloc_linked - allocate and link host client | |
902 | * | |
903 | * @dev: the device structure | |
904 | * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one | |
905 | * | |
906 | * Return: cl on success ERR_PTR on failure | |
907 | */ | |
908 | struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id) | |
909 | { | |
910 | struct mei_cl *cl; | |
911 | int ret; | |
912 | ||
913 | cl = mei_cl_allocate(dev); | |
914 | if (!cl) { | |
915 | ret = -ENOMEM; | |
916 | goto err; | |
917 | } | |
918 | ||
919 | ret = mei_cl_link(cl, id); | |
920 | if (ret) | |
921 | goto err; | |
922 | ||
923 | return cl; | |
924 | err: | |
925 | kfree(cl); | |
926 | return ERR_PTR(ret); | |
927 | } | |
928 | ||
929 | ||
930 | ||
9ca9050b | 931 | /** |
90e0b5f1 | 932 | * mei_cl_flow_ctrl_creds - checks flow_control credits for cl. |
9ca9050b | 933 | * |
9ca9050b TW |
934 | * @cl: private data of the file object |
935 | * | |
a8605ea2 | 936 | * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise. |
9ca9050b TW |
937 | * -ENOENT if mei_cl is not present |
938 | * -EINVAL if single_recv_buf == 0 | |
939 | */ | |
90e0b5f1 | 940 | int mei_cl_flow_ctrl_creds(struct mei_cl *cl) |
9ca9050b | 941 | { |
90e0b5f1 | 942 | struct mei_device *dev; |
12d00665 | 943 | struct mei_me_client *me_cl; |
79563db9 | 944 | int rets = 0; |
9ca9050b | 945 | |
90e0b5f1 TW |
946 | if (WARN_ON(!cl || !cl->dev)) |
947 | return -EINVAL; | |
948 | ||
949 | dev = cl->dev; | |
950 | ||
9ca9050b TW |
951 | if (cl->mei_flow_ctrl_creds > 0) |
952 | return 1; | |
953 | ||
2e5df413 | 954 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
d320832f | 955 | if (!me_cl) { |
12d00665 | 956 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
d320832f | 957 | return -ENOENT; |
9ca9050b | 958 | } |
12d00665 | 959 | |
79563db9 TW |
960 | if (me_cl->mei_flow_ctrl_creds > 0) { |
961 | rets = 1; | |
12d00665 | 962 | if (WARN_ON(me_cl->props.single_recv_buf == 0)) |
79563db9 | 963 | rets = -EINVAL; |
12d00665 | 964 | } |
79563db9 TW |
965 | mei_me_cl_put(me_cl); |
966 | return rets; | |
9ca9050b TW |
967 | } |
968 | ||
969 | /** | |
90e0b5f1 | 970 | * mei_cl_flow_ctrl_reduce - reduces flow_control. |
9ca9050b | 971 | * |
9ca9050b | 972 | * @cl: private data of the file object |
393b148f | 973 | * |
a8605ea2 | 974 | * Return: |
9ca9050b TW |
975 | * 0 on success |
976 | * -ENOENT when me client is not found | |
977 | * -EINVAL when ctrl credits are <= 0 | |
978 | */ | |
90e0b5f1 | 979 | int mei_cl_flow_ctrl_reduce(struct mei_cl *cl) |
9ca9050b | 980 | { |
90e0b5f1 | 981 | struct mei_device *dev; |
12d00665 | 982 | struct mei_me_client *me_cl; |
79563db9 | 983 | int rets; |
9ca9050b | 984 | |
90e0b5f1 TW |
985 | if (WARN_ON(!cl || !cl->dev)) |
986 | return -EINVAL; | |
987 | ||
988 | dev = cl->dev; | |
989 | ||
2e5df413 | 990 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
d320832f | 991 | if (!me_cl) { |
12d00665 | 992 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
d320832f | 993 | return -ENOENT; |
12d00665 | 994 | } |
9ca9050b | 995 | |
d320832f | 996 | if (me_cl->props.single_recv_buf) { |
79563db9 TW |
997 | if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) { |
998 | rets = -EINVAL; | |
999 | goto out; | |
1000 | } | |
12d00665 AU |
1001 | me_cl->mei_flow_ctrl_creds--; |
1002 | } else { | |
79563db9 TW |
1003 | if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) { |
1004 | rets = -EINVAL; | |
1005 | goto out; | |
1006 | } | |
12d00665 | 1007 | cl->mei_flow_ctrl_creds--; |
9ca9050b | 1008 | } |
79563db9 TW |
1009 | rets = 0; |
1010 | out: | |
1011 | mei_me_cl_put(me_cl); | |
1012 | return rets; | |
9ca9050b TW |
1013 | } |
1014 | ||
ab841160 | 1015 | /** |
393b148f | 1016 | * mei_cl_read_start - the start read client message function. |
ab841160 | 1017 | * |
90e0b5f1 | 1018 | * @cl: host client |
ce23139c | 1019 | * @length: number of bytes to read |
bca67d68 | 1020 | * @fp: pointer to file structure |
ab841160 | 1021 | * |
a8605ea2 | 1022 | * Return: 0 on success, <0 on failure. |
ab841160 | 1023 | */ |
bca67d68 | 1024 | int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp) |
ab841160 | 1025 | { |
90e0b5f1 | 1026 | struct mei_device *dev; |
ab841160 | 1027 | struct mei_cl_cb *cb; |
d320832f | 1028 | struct mei_me_client *me_cl; |
664df38b | 1029 | int rets; |
ab841160 | 1030 | |
90e0b5f1 TW |
1031 | if (WARN_ON(!cl || !cl->dev)) |
1032 | return -ENODEV; | |
1033 | ||
1034 | dev = cl->dev; | |
1035 | ||
b950ac1d | 1036 | if (!mei_cl_is_connected(cl)) |
ab841160 OW |
1037 | return -ENODEV; |
1038 | ||
a9bed610 TW |
1039 | /* HW currently supports only one pending read */ |
1040 | if (!list_empty(&cl->rd_pending)) | |
ab841160 | 1041 | return -EBUSY; |
a9bed610 | 1042 | |
d880f329 | 1043 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
d320832f | 1044 | if (!me_cl) { |
c0abffbd | 1045 | cl_err(dev, cl, "no such me client %d\n", cl->me_client_id); |
7ca96aa2 | 1046 | return -ENOTTY; |
664df38b | 1047 | } |
79563db9 TW |
1048 | /* always allocate at least client max message */ |
1049 | length = max_t(size_t, length, me_cl->props.max_msg_length); | |
1050 | mei_me_cl_put(me_cl); | |
ab841160 | 1051 | |
2bf94cab | 1052 | rets = pm_runtime_get(dev->dev); |
04bb139a | 1053 | if (rets < 0 && rets != -EINPROGRESS) { |
2bf94cab | 1054 | pm_runtime_put_noidle(dev->dev); |
04bb139a TW |
1055 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
1056 | return rets; | |
1057 | } | |
1058 | ||
bca67d68 TW |
1059 | cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp); |
1060 | rets = cb ? 0 : -ENOMEM; | |
664df38b | 1061 | if (rets) |
04bb139a | 1062 | goto out; |
ab841160 | 1063 | |
6aae48ff | 1064 | if (mei_hbuf_acquire(dev)) { |
86113500 AU |
1065 | rets = mei_hbm_cl_flow_control_req(dev, cl); |
1066 | if (rets < 0) | |
04bb139a | 1067 | goto out; |
04bb139a | 1068 | |
a9bed610 | 1069 | list_add_tail(&cb->list, &cl->rd_pending); |
ab841160 | 1070 | } else { |
fb601adb | 1071 | list_add_tail(&cb->list, &dev->ctrl_wr_list.list); |
ab841160 | 1072 | } |
accb884b | 1073 | |
04bb139a TW |
1074 | out: |
1075 | cl_dbg(dev, cl, "rpm: autosuspend\n"); | |
2bf94cab TW |
1076 | pm_runtime_mark_last_busy(dev->dev); |
1077 | pm_runtime_put_autosuspend(dev->dev); | |
04bb139a TW |
1078 | |
1079 | if (rets) | |
1080 | mei_io_cb_free(cb); | |
1081 | ||
ab841160 OW |
1082 | return rets; |
1083 | } | |
1084 | ||
21767546 | 1085 | /** |
9d098192 | 1086 | * mei_cl_irq_write - write a message to device |
21767546 TW |
1087 | * from the interrupt thread context |
1088 | * | |
1089 | * @cl: client | |
1090 | * @cb: callback block. | |
21767546 TW |
1091 | * @cmpl_list: complete list. |
1092 | * | |
a8605ea2 | 1093 | * Return: 0, OK; otherwise error. |
21767546 | 1094 | */ |
9d098192 TW |
1095 | int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb, |
1096 | struct mei_cl_cb *cmpl_list) | |
21767546 | 1097 | { |
136698e5 TW |
1098 | struct mei_device *dev; |
1099 | struct mei_msg_data *buf; | |
21767546 | 1100 | struct mei_msg_hdr mei_hdr; |
136698e5 TW |
1101 | size_t len; |
1102 | u32 msg_slots; | |
9d098192 | 1103 | int slots; |
2ebf8c94 | 1104 | int rets; |
21767546 | 1105 | |
136698e5 TW |
1106 | if (WARN_ON(!cl || !cl->dev)) |
1107 | return -ENODEV; | |
1108 | ||
1109 | dev = cl->dev; | |
1110 | ||
5db7514d | 1111 | buf = &cb->buf; |
136698e5 TW |
1112 | |
1113 | rets = mei_cl_flow_ctrl_creds(cl); | |
1114 | if (rets < 0) | |
1115 | return rets; | |
1116 | ||
1117 | if (rets == 0) { | |
04bb139a | 1118 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); |
136698e5 TW |
1119 | return 0; |
1120 | } | |
1121 | ||
9d098192 | 1122 | slots = mei_hbuf_empty_slots(dev); |
136698e5 TW |
1123 | len = buf->size - cb->buf_idx; |
1124 | msg_slots = mei_data2slots(len); | |
1125 | ||
21767546 TW |
1126 | mei_hdr.host_addr = cl->host_client_id; |
1127 | mei_hdr.me_addr = cl->me_client_id; | |
1128 | mei_hdr.reserved = 0; | |
479327fc | 1129 | mei_hdr.internal = cb->internal; |
21767546 | 1130 | |
9d098192 | 1131 | if (slots >= msg_slots) { |
21767546 TW |
1132 | mei_hdr.length = len; |
1133 | mei_hdr.msg_complete = 1; | |
1134 | /* Split the message only if we can write the whole host buffer */ | |
9d098192 TW |
1135 | } else if (slots == dev->hbuf_depth) { |
1136 | msg_slots = slots; | |
1137 | len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr); | |
21767546 TW |
1138 | mei_hdr.length = len; |
1139 | mei_hdr.msg_complete = 0; | |
1140 | } else { | |
1141 | /* wait for next time the host buffer is empty */ | |
1142 | return 0; | |
1143 | } | |
1144 | ||
c0abffbd | 1145 | cl_dbg(dev, cl, "buf: size = %d idx = %lu\n", |
5db7514d | 1146 | cb->buf.size, cb->buf_idx); |
21767546 | 1147 | |
136698e5 | 1148 | rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx); |
2ebf8c94 TW |
1149 | if (rets) { |
1150 | cl->status = rets; | |
21767546 | 1151 | list_move_tail(&cb->list, &cmpl_list->list); |
2ebf8c94 | 1152 | return rets; |
21767546 TW |
1153 | } |
1154 | ||
1155 | cl->status = 0; | |
4dfaa9f7 | 1156 | cl->writing_state = MEI_WRITING; |
21767546 | 1157 | cb->buf_idx += mei_hdr.length; |
8660172e | 1158 | cb->completed = mei_hdr.msg_complete == 1; |
4dfaa9f7 | 1159 | |
21767546 TW |
1160 | if (mei_hdr.msg_complete) { |
1161 | if (mei_cl_flow_ctrl_reduce(cl)) | |
2ebf8c94 | 1162 | return -EIO; |
21767546 TW |
1163 | list_move_tail(&cb->list, &dev->write_waiting_list.list); |
1164 | } | |
1165 | ||
1166 | return 0; | |
1167 | } | |
1168 | ||
4234a6de TW |
1169 | /** |
1170 | * mei_cl_write - submit a write cb to mei device | |
a8605ea2 | 1171 | * assumes device_lock is locked |
4234a6de TW |
1172 | * |
1173 | * @cl: host client | |
a8605ea2 | 1174 | * @cb: write callback with filled data |
ce23139c | 1175 | * @blocking: block until completed |
4234a6de | 1176 | * |
a8605ea2 | 1177 | * Return: number of bytes sent on success, <0 on failure. |
4234a6de TW |
1178 | */ |
1179 | int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking) | |
1180 | { | |
1181 | struct mei_device *dev; | |
1182 | struct mei_msg_data *buf; | |
1183 | struct mei_msg_hdr mei_hdr; | |
1184 | int rets; | |
1185 | ||
1186 | ||
1187 | if (WARN_ON(!cl || !cl->dev)) | |
1188 | return -ENODEV; | |
1189 | ||
1190 | if (WARN_ON(!cb)) | |
1191 | return -EINVAL; | |
1192 | ||
1193 | dev = cl->dev; | |
1194 | ||
1195 | ||
5db7514d | 1196 | buf = &cb->buf; |
4234a6de | 1197 | |
0a01e974 | 1198 | cl_dbg(dev, cl, "size=%d\n", buf->size); |
4234a6de | 1199 | |
2bf94cab | 1200 | rets = pm_runtime_get(dev->dev); |
04bb139a | 1201 | if (rets < 0 && rets != -EINPROGRESS) { |
2bf94cab | 1202 | pm_runtime_put_noidle(dev->dev); |
04bb139a TW |
1203 | cl_err(dev, cl, "rpm: get failed %d\n", rets); |
1204 | return rets; | |
1205 | } | |
4234a6de | 1206 | |
6aae48ff TW |
1207 | cb->buf_idx = 0; |
1208 | cl->writing_state = MEI_IDLE; | |
1209 | ||
1210 | mei_hdr.host_addr = cl->host_client_id; | |
1211 | mei_hdr.me_addr = cl->me_client_id; | |
1212 | mei_hdr.reserved = 0; | |
1213 | mei_hdr.msg_complete = 0; | |
1214 | mei_hdr.internal = cb->internal; | |
4234a6de TW |
1215 | |
1216 | rets = mei_cl_flow_ctrl_creds(cl); | |
1217 | if (rets < 0) | |
1218 | goto err; | |
1219 | ||
6aae48ff TW |
1220 | if (rets == 0) { |
1221 | cl_dbg(dev, cl, "No flow control credentials: not sending.\n"); | |
1222 | rets = buf->size; | |
1223 | goto out; | |
1224 | } | |
1225 | if (!mei_hbuf_acquire(dev)) { | |
1226 | cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n"); | |
4234a6de TW |
1227 | rets = buf->size; |
1228 | goto out; | |
1229 | } | |
4234a6de TW |
1230 | |
1231 | /* Check for a maximum length */ | |
1232 | if (buf->size > mei_hbuf_max_len(dev)) { | |
1233 | mei_hdr.length = mei_hbuf_max_len(dev); | |
1234 | mei_hdr.msg_complete = 0; | |
1235 | } else { | |
1236 | mei_hdr.length = buf->size; | |
1237 | mei_hdr.msg_complete = 1; | |
1238 | } | |
1239 | ||
2ebf8c94 TW |
1240 | rets = mei_write_message(dev, &mei_hdr, buf->data); |
1241 | if (rets) | |
4234a6de | 1242 | goto err; |
4234a6de TW |
1243 | |
1244 | cl->writing_state = MEI_WRITING; | |
1245 | cb->buf_idx = mei_hdr.length; | |
8660172e | 1246 | cb->completed = mei_hdr.msg_complete == 1; |
4234a6de | 1247 | |
4234a6de TW |
1248 | out: |
1249 | if (mei_hdr.msg_complete) { | |
7ca96aa2 AU |
1250 | rets = mei_cl_flow_ctrl_reduce(cl); |
1251 | if (rets < 0) | |
4234a6de | 1252 | goto err; |
7ca96aa2 | 1253 | |
4234a6de TW |
1254 | list_add_tail(&cb->list, &dev->write_waiting_list.list); |
1255 | } else { | |
1256 | list_add_tail(&cb->list, &dev->write_list.list); | |
1257 | } | |
1258 | ||
1259 | ||
1260 | if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) { | |
1261 | ||
1262 | mutex_unlock(&dev->device_lock); | |
7ca96aa2 AU |
1263 | rets = wait_event_interruptible(cl->tx_wait, |
1264 | cl->writing_state == MEI_WRITE_COMPLETE); | |
4234a6de | 1265 | mutex_lock(&dev->device_lock); |
7ca96aa2 AU |
1266 | /* wait_event_interruptible returns -ERESTARTSYS */ |
1267 | if (rets) { | |
1268 | if (signal_pending(current)) | |
1269 | rets = -EINTR; | |
1270 | goto err; | |
1271 | } | |
4234a6de | 1272 | } |
7ca96aa2 AU |
1273 | |
1274 | rets = buf->size; | |
4234a6de | 1275 | err: |
04bb139a | 1276 | cl_dbg(dev, cl, "rpm: autosuspend\n"); |
2bf94cab TW |
1277 | pm_runtime_mark_last_busy(dev->dev); |
1278 | pm_runtime_put_autosuspend(dev->dev); | |
04bb139a | 1279 | |
4234a6de TW |
1280 | return rets; |
1281 | } | |
1282 | ||
1283 | ||
db086fa9 TW |
1284 | /** |
1285 | * mei_cl_complete - processes completed operation for a client | |
1286 | * | |
1287 | * @cl: private data of the file object. | |
1288 | * @cb: callback block. | |
1289 | */ | |
1290 | void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb) | |
1291 | { | |
1292 | if (cb->fop_type == MEI_FOP_WRITE) { | |
1293 | mei_io_cb_free(cb); | |
1294 | cb = NULL; | |
1295 | cl->writing_state = MEI_WRITE_COMPLETE; | |
1296 | if (waitqueue_active(&cl->tx_wait)) | |
1297 | wake_up_interruptible(&cl->tx_wait); | |
1298 | ||
a9bed610 TW |
1299 | } else if (cb->fop_type == MEI_FOP_READ) { |
1300 | list_add_tail(&cb->list, &cl->rd_completed); | |
db086fa9 | 1301 | if (waitqueue_active(&cl->rx_wait)) |
1d9013f0 | 1302 | wake_up_interruptible_all(&cl->rx_wait); |
db086fa9 TW |
1303 | else |
1304 | mei_cl_bus_rx_event(cl); | |
1305 | ||
1306 | } | |
1307 | } | |
1308 | ||
4234a6de | 1309 | |
074b4c01 TW |
1310 | /** |
1311 | * mei_cl_all_disconnect - disconnect forcefully all connected clients | |
1312 | * | |
a8605ea2 | 1313 | * @dev: mei device |
074b4c01 TW |
1314 | */ |
1315 | ||
1316 | void mei_cl_all_disconnect(struct mei_device *dev) | |
1317 | { | |
31f88f57 | 1318 | struct mei_cl *cl; |
074b4c01 | 1319 | |
31f88f57 | 1320 | list_for_each_entry(cl, &dev->file_list, link) { |
074b4c01 TW |
1321 | cl->state = MEI_FILE_DISCONNECTED; |
1322 | cl->mei_flow_ctrl_creds = 0; | |
074b4c01 TW |
1323 | cl->timer_count = 0; |
1324 | } | |
1325 | } | |
1326 | ||
1327 | ||
1328 | /** | |
5290801c | 1329 | * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted |
074b4c01 | 1330 | * |
a8605ea2 | 1331 | * @dev: mei device |
074b4c01 | 1332 | */ |
5290801c | 1333 | void mei_cl_all_wakeup(struct mei_device *dev) |
074b4c01 | 1334 | { |
31f88f57 | 1335 | struct mei_cl *cl; |
92db1555 | 1336 | |
31f88f57 | 1337 | list_for_each_entry(cl, &dev->file_list, link) { |
074b4c01 | 1338 | if (waitqueue_active(&cl->rx_wait)) { |
c0abffbd | 1339 | cl_dbg(dev, cl, "Waking up reading client!\n"); |
074b4c01 TW |
1340 | wake_up_interruptible(&cl->rx_wait); |
1341 | } | |
5290801c | 1342 | if (waitqueue_active(&cl->tx_wait)) { |
c0abffbd | 1343 | cl_dbg(dev, cl, "Waking up writing client!\n"); |
5290801c TW |
1344 | wake_up_interruptible(&cl->tx_wait); |
1345 | } | |
074b4c01 TW |
1346 | } |
1347 | } | |
1348 | ||
1349 | /** | |
1350 | * mei_cl_all_write_clear - clear all pending writes | |
a8605ea2 AU |
1351 | * |
1352 | * @dev: mei device | |
074b4c01 TW |
1353 | */ |
1354 | void mei_cl_all_write_clear(struct mei_device *dev) | |
1355 | { | |
cc99ecfd TW |
1356 | mei_io_list_free(&dev->write_list, NULL); |
1357 | mei_io_list_free(&dev->write_waiting_list, NULL); | |
074b4c01 TW |
1358 | } |
1359 | ||
1360 |