mei: me: wait for hw ready non-interruptible
[linux-2.6-block.git] / drivers / misc / mei / client.c
CommitLineData
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/pci.h>
ab841160 18#include <linux/sched.h>
9ca9050b
TW
19#include <linux/wait.h>
20#include <linux/delay.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
29/**
30 * mei_me_cl_by_uuid - locate index of me client
31 *
32 * @dev: mei device
a27a76d3
AU
33 *
34 * Locking: called under "dev->device_lock" lock
35 *
90e0b5f1
TW
36 * returns me client index or -ENOENT if not found
37 */
38int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid)
39{
a27a76d3 40 int i;
90e0b5f1
TW
41
42 for (i = 0; i < dev->me_clients_num; ++i)
43 if (uuid_le_cmp(*uuid,
a27a76d3
AU
44 dev->me_clients[i].props.protocol_name) == 0)
45 return i;
90e0b5f1 46
a27a76d3 47 return -ENOENT;
90e0b5f1
TW
48}
49
50
51/**
52 * mei_me_cl_by_id return index to me_clients for client_id
53 *
54 * @dev: the device structure
55 * @client_id: me client id
56 *
57 * Locking: called under "dev->device_lock" lock
58 *
59 * returns index on success, -ENOENT on failure.
60 */
61
62int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
63{
64 int i;
a27a76d3 65
90e0b5f1
TW
66 for (i = 0; i < dev->me_clients_num; i++)
67 if (dev->me_clients[i].client_id == client_id)
a27a76d3 68 return i;
90e0b5f1 69
a27a76d3 70 return -ENOENT;
90e0b5f1 71}
ab841160 72
9ca9050b
TW
73
74/**
cc99ecfd 75 * mei_cl_cmp_id - tells if the clients are the same
9ca9050b 76 *
cc99ecfd
TW
77 * @cl1: host client 1
78 * @cl2: host client 2
79 *
80 * returns true - if the clients has same host and me ids
81 * false - otherwise
82 */
83static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
84 const struct mei_cl *cl2)
85{
86 return cl1 && cl2 &&
87 (cl1->host_client_id == cl2->host_client_id) &&
88 (cl1->me_client_id == cl2->me_client_id);
89}
90
91/**
92 * mei_io_list_flush - removes cbs belonging to cl.
93 *
94 * @list: an instance of our list structure
95 * @cl: host client, can be NULL for flushing the whole list
96 * @free: whether to free the cbs
9ca9050b 97 */
cc99ecfd
TW
98static void __mei_io_list_flush(struct mei_cl_cb *list,
99 struct mei_cl *cl, bool free)
9ca9050b
TW
100{
101 struct mei_cl_cb *cb;
102 struct mei_cl_cb *next;
103
cc99ecfd 104 /* enable removing everything if no cl is specified */
9ca9050b 105 list_for_each_entry_safe(cb, next, &list->list, list) {
cc99ecfd 106 if (!cl || (cb->cl && mei_cl_cmp_id(cl, cb->cl))) {
9ca9050b 107 list_del(&cb->list);
cc99ecfd
TW
108 if (free)
109 mei_io_cb_free(cb);
110 }
9ca9050b
TW
111 }
112}
113
cc99ecfd
TW
114/**
115 * mei_io_list_flush - removes list entry belonging to cl.
116 *
117 * @list: An instance of our list structure
118 * @cl: host client
119 */
120static inline void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
121{
122 __mei_io_list_flush(list, cl, false);
123}
124
125
126/**
127 * mei_io_list_free - removes cb belonging to cl and free them
128 *
129 * @list: An instance of our list structure
130 * @cl: host client
131 */
132static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
133{
134 __mei_io_list_flush(list, cl, true);
135}
136
601a1efa
TW
137/**
138 * mei_io_cb_free - free mei_cb_private related memory
139 *
140 * @cb: mei callback struct
141 */
142void mei_io_cb_free(struct mei_cl_cb *cb)
143{
144 if (cb == NULL)
145 return;
146
147 kfree(cb->request_buffer.data);
148 kfree(cb->response_buffer.data);
149 kfree(cb);
150}
9ca9050b 151
664df38b
TW
152/**
153 * mei_io_cb_init - allocate and initialize io callback
154 *
155 * @cl - mei client
393b148f 156 * @fp: pointer to file structure
664df38b
TW
157 *
158 * returns mei_cl_cb pointer or NULL;
159 */
160struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
161{
162 struct mei_cl_cb *cb;
163
164 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
165 if (!cb)
166 return NULL;
167
168 mei_io_list_init(cb);
169
170 cb->file_object = fp;
db3ed431 171 cb->cl = cl;
664df38b
TW
172 cb->buf_idx = 0;
173 return cb;
174}
175
664df38b
TW
176/**
177 * mei_io_cb_alloc_req_buf - allocate request buffer
178 *
393b148f
MI
179 * @cb: io callback structure
180 * @length: size of the buffer
664df38b
TW
181 *
182 * returns 0 on success
183 * -EINVAL if cb is NULL
184 * -ENOMEM if allocation failed
185 */
186int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
187{
188 if (!cb)
189 return -EINVAL;
190
191 if (length == 0)
192 return 0;
193
194 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
195 if (!cb->request_buffer.data)
196 return -ENOMEM;
197 cb->request_buffer.size = length;
198 return 0;
199}
200/**
83ce0741 201 * mei_io_cb_alloc_resp_buf - allocate response buffer
664df38b 202 *
393b148f
MI
203 * @cb: io callback structure
204 * @length: size of the buffer
664df38b
TW
205 *
206 * returns 0 on success
207 * -EINVAL if cb is NULL
208 * -ENOMEM if allocation failed
209 */
210int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
211{
212 if (!cb)
213 return -EINVAL;
214
215 if (length == 0)
216 return 0;
217
218 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
219 if (!cb->response_buffer.data)
220 return -ENOMEM;
221 cb->response_buffer.size = length;
222 return 0;
223}
224
601a1efa 225
9ca9050b
TW
226
227/**
228 * mei_cl_flush_queues - flushes queue lists belonging to cl.
229 *
9ca9050b
TW
230 * @cl: host client
231 */
232int mei_cl_flush_queues(struct mei_cl *cl)
233{
c0abffbd
AU
234 struct mei_device *dev;
235
90e0b5f1 236 if (WARN_ON(!cl || !cl->dev))
9ca9050b
TW
237 return -EINVAL;
238
c0abffbd
AU
239 dev = cl->dev;
240
241 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
9ca9050b 242 mei_io_list_flush(&cl->dev->read_list, cl);
cc99ecfd
TW
243 mei_io_list_free(&cl->dev->write_list, cl);
244 mei_io_list_free(&cl->dev->write_waiting_list, cl);
9ca9050b
TW
245 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
246 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
247 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
248 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
249 return 0;
250}
251
ab841160 252
9ca9050b 253/**
83ce0741 254 * mei_cl_init - initializes cl.
9ca9050b
TW
255 *
256 * @cl: host client to be initialized
257 * @dev: mei device
258 */
259void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
260{
261 memset(cl, 0, sizeof(struct mei_cl));
262 init_waitqueue_head(&cl->wait);
263 init_waitqueue_head(&cl->rx_wait);
264 init_waitqueue_head(&cl->tx_wait);
265 INIT_LIST_HEAD(&cl->link);
a7b71bc0 266 INIT_LIST_HEAD(&cl->device_link);
9ca9050b
TW
267 cl->reading_state = MEI_IDLE;
268 cl->writing_state = MEI_IDLE;
269 cl->dev = dev;
270}
271
272/**
273 * mei_cl_allocate - allocates cl structure and sets it up.
274 *
275 * @dev: mei device
276 * returns The allocated file or NULL on failure
277 */
278struct mei_cl *mei_cl_allocate(struct mei_device *dev)
279{
280 struct mei_cl *cl;
281
282 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
283 if (!cl)
284 return NULL;
285
286 mei_cl_init(cl, dev);
287
288 return cl;
289}
290
90e0b5f1
TW
291/**
292 * mei_cl_find_read_cb - find this cl's callback in the read list
293 *
393b148f
MI
294 * @cl: host client
295 *
90e0b5f1
TW
296 * returns cb on success, NULL on error
297 */
298struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
299{
300 struct mei_device *dev = cl->dev;
31f88f57 301 struct mei_cl_cb *cb;
90e0b5f1 302
31f88f57 303 list_for_each_entry(cb, &dev->read_list.list, list)
90e0b5f1
TW
304 if (mei_cl_cmp_id(cl, cb->cl))
305 return cb;
306 return NULL;
307}
308
83ce0741 309/** mei_cl_link: allocate host id in the host map
9ca9050b 310 *
781d0d89 311 * @cl - host client
83ce0741 312 * @id - fixed host id or -1 for generic one
393b148f 313 *
781d0d89 314 * returns 0 on success
9ca9050b
TW
315 * -EINVAL on incorrect values
316 * -ENONET if client not found
317 */
781d0d89 318int mei_cl_link(struct mei_cl *cl, int id)
9ca9050b 319{
90e0b5f1 320 struct mei_device *dev;
22f96a0e 321 long open_handle_count;
9ca9050b 322
781d0d89 323 if (WARN_ON(!cl || !cl->dev))
9ca9050b
TW
324 return -EINVAL;
325
90e0b5f1
TW
326 dev = cl->dev;
327
83ce0741 328 /* If Id is not assigned get one*/
781d0d89
TW
329 if (id == MEI_HOST_CLIENT_ID_ANY)
330 id = find_first_zero_bit(dev->host_clients_map,
331 MEI_CLIENTS_MAX);
9ca9050b 332
781d0d89 333 if (id >= MEI_CLIENTS_MAX) {
83ce0741 334 dev_err(&dev->pdev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
e036cc57
TW
335 return -EMFILE;
336 }
337
22f96a0e
TW
338 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
339 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
83ce0741 340 dev_err(&dev->pdev->dev, "open_handle_count exceeded %d",
e036cc57
TW
341 MEI_MAX_OPEN_HANDLE_COUNT);
342 return -EMFILE;
9ca9050b
TW
343 }
344
781d0d89
TW
345 dev->open_handle_count++;
346
347 cl->host_client_id = id;
348 list_add_tail(&cl->link, &dev->file_list);
349
350 set_bit(id, dev->host_clients_map);
351
352 cl->state = MEI_FILE_INITIALIZING;
353
c0abffbd 354 cl_dbg(dev, cl, "link cl\n");
781d0d89 355 return 0;
9ca9050b 356}
781d0d89 357
9ca9050b 358/**
90e0b5f1 359 * mei_cl_unlink - remove me_cl from the list
9ca9050b 360 *
393b148f 361 * @cl: host client
9ca9050b 362 */
90e0b5f1 363int mei_cl_unlink(struct mei_cl *cl)
9ca9050b 364{
90e0b5f1 365 struct mei_device *dev;
90e0b5f1 366
781d0d89
TW
367 /* don't shout on error exit path */
368 if (!cl)
369 return 0;
370
8e9a4a9a
TW
371 /* wd and amthif might not be initialized */
372 if (!cl->dev)
373 return 0;
90e0b5f1
TW
374
375 dev = cl->dev;
376
a14c44d8
TW
377 cl_dbg(dev, cl, "unlink client");
378
22f96a0e
TW
379 if (dev->open_handle_count > 0)
380 dev->open_handle_count--;
381
382 /* never clear the 0 bit */
383 if (cl->host_client_id)
384 clear_bit(cl->host_client_id, dev->host_clients_map);
385
386 list_del_init(&cl->link);
387
388 cl->state = MEI_FILE_INITIALIZING;
389
90e0b5f1 390 return 0;
9ca9050b
TW
391}
392
393
394void mei_host_client_init(struct work_struct *work)
395{
396 struct mei_device *dev = container_of(work,
397 struct mei_device, init_work);
398 struct mei_client_properties *client_props;
399 int i;
400
401 mutex_lock(&dev->device_lock);
402
9ca9050b
TW
403 for (i = 0; i < dev->me_clients_num; i++) {
404 client_props = &dev->me_clients[i].props;
405
1a1aca42 406 if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
9ca9050b
TW
407 mei_amthif_host_init(dev);
408 else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
409 mei_wd_host_init(dev);
59fcd7c6
SO
410 else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
411 mei_nfc_host_init(dev);
412
9ca9050b
TW
413 }
414
415 dev->dev_state = MEI_DEV_ENABLED;
6adb8efb 416 dev->reset_count = 0;
9ca9050b
TW
417
418 mutex_unlock(&dev->device_lock);
04bb139a
TW
419
420 pm_runtime_mark_last_busy(&dev->pdev->dev);
421 dev_dbg(&dev->pdev->dev, "rpm: autosuspend\n");
422 pm_runtime_autosuspend(&dev->pdev->dev);
9ca9050b
TW
423}
424
6aae48ff
TW
425/**
426 * mei_hbuf_acquire: try to acquire host buffer
427 *
428 * @dev: the device structure
429 * returns true if host buffer was acquired
430 */
431bool mei_hbuf_acquire(struct mei_device *dev)
432{
04bb139a
TW
433 if (mei_pg_state(dev) == MEI_PG_ON ||
434 dev->pg_event == MEI_PG_EVENT_WAIT) {
435 dev_dbg(&dev->pdev->dev, "device is in pg\n");
436 return false;
437 }
438
6aae48ff
TW
439 if (!dev->hbuf_is_ready) {
440 dev_dbg(&dev->pdev->dev, "hbuf is not ready\n");
441 return false;
442 }
443
444 dev->hbuf_is_ready = false;
445
446 return true;
447}
9ca9050b
TW
448
449/**
83ce0741 450 * mei_cl_disconnect - disconnect host client from the me one
9ca9050b 451 *
90e0b5f1 452 * @cl: host client
9ca9050b
TW
453 *
454 * Locking: called under "dev->device_lock" lock
455 *
456 * returns 0 on success, <0 on failure.
457 */
90e0b5f1 458int mei_cl_disconnect(struct mei_cl *cl)
9ca9050b 459{
90e0b5f1 460 struct mei_device *dev;
9ca9050b 461 struct mei_cl_cb *cb;
fe2f17eb 462 int rets;
9ca9050b 463
90e0b5f1 464 if (WARN_ON(!cl || !cl->dev))
9ca9050b
TW
465 return -ENODEV;
466
90e0b5f1
TW
467 dev = cl->dev;
468
c0abffbd
AU
469 cl_dbg(dev, cl, "disconnecting");
470
9ca9050b
TW
471 if (cl->state != MEI_FILE_DISCONNECTING)
472 return 0;
473
04bb139a
TW
474 rets = pm_runtime_get(&dev->pdev->dev);
475 if (rets < 0 && rets != -EINPROGRESS) {
476 pm_runtime_put_noidle(&dev->pdev->dev);
477 cl_err(dev, cl, "rpm: get failed %d\n", rets);
478 return rets;
479 }
480
9ca9050b 481 cb = mei_io_cb_init(cl, NULL);
04bb139a
TW
482 if (!cb) {
483 rets = -ENOMEM;
484 goto free;
485 }
9ca9050b
TW
486
487 cb->fop_type = MEI_FOP_CLOSE;
6aae48ff 488 if (mei_hbuf_acquire(dev)) {
9ca9050b
TW
489 if (mei_hbm_cl_disconnect_req(dev, cl)) {
490 rets = -ENODEV;
c0abffbd 491 cl_err(dev, cl, "failed to disconnect.\n");
9ca9050b
TW
492 goto free;
493 }
22b987a3 494 cl->timer_count = MEI_CONNECT_TIMEOUT;
9ca9050b
TW
495 mdelay(10); /* Wait for hardware disconnection ready */
496 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
497 } else {
c0abffbd 498 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
9ca9050b
TW
499 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
500
501 }
502 mutex_unlock(&dev->device_lock);
503
fe2f17eb 504 wait_event_timeout(dev->wait_recvd_msg,
9ca9050b
TW
505 MEI_FILE_DISCONNECTED == cl->state,
506 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
507
508 mutex_lock(&dev->device_lock);
fe2f17eb 509
9ca9050b
TW
510 if (MEI_FILE_DISCONNECTED == cl->state) {
511 rets = 0;
c0abffbd 512 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
9ca9050b 513 } else {
fe2f17eb
AU
514 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
515 rets = -ETIME;
9ca9050b
TW
516 }
517
518 mei_io_list_flush(&dev->ctrl_rd_list, cl);
519 mei_io_list_flush(&dev->ctrl_wr_list, cl);
520free:
04bb139a
TW
521 cl_dbg(dev, cl, "rpm: autosuspend\n");
522 pm_runtime_mark_last_busy(&dev->pdev->dev);
523 pm_runtime_put_autosuspend(&dev->pdev->dev);
524
9ca9050b
TW
525 mei_io_cb_free(cb);
526 return rets;
527}
528
529
530/**
90e0b5f1
TW
531 * mei_cl_is_other_connecting - checks if other
532 * client with the same me client id is connecting
9ca9050b 533 *
9ca9050b
TW
534 * @cl: private data of the file object
535 *
83ce0741 536 * returns true if other client is connected, false - otherwise.
9ca9050b 537 */
90e0b5f1 538bool mei_cl_is_other_connecting(struct mei_cl *cl)
9ca9050b 539{
90e0b5f1 540 struct mei_device *dev;
31f88f57 541 struct mei_cl *ocl; /* the other client */
9ca9050b 542
90e0b5f1
TW
543 if (WARN_ON(!cl || !cl->dev))
544 return false;
545
546 dev = cl->dev;
547
31f88f57
TW
548 list_for_each_entry(ocl, &dev->file_list, link) {
549 if (ocl->state == MEI_FILE_CONNECTING &&
550 ocl != cl &&
551 cl->me_client_id == ocl->me_client_id)
90e0b5f1 552 return true;
9ca9050b
TW
553
554 }
90e0b5f1
TW
555
556 return false;
9ca9050b
TW
557}
558
9f81abda 559/**
83ce0741 560 * mei_cl_connect - connect host client to the me one
9f81abda
TW
561 *
562 * @cl: host client
563 *
564 * Locking: called under "dev->device_lock" lock
565 *
566 * returns 0 on success, <0 on failure.
567 */
568int mei_cl_connect(struct mei_cl *cl, struct file *file)
569{
570 struct mei_device *dev;
571 struct mei_cl_cb *cb;
9f81abda
TW
572 int rets;
573
574 if (WARN_ON(!cl || !cl->dev))
575 return -ENODEV;
576
577 dev = cl->dev;
578
04bb139a
TW
579 rets = pm_runtime_get(&dev->pdev->dev);
580 if (rets < 0 && rets != -EINPROGRESS) {
581 pm_runtime_put_noidle(&dev->pdev->dev);
582 cl_err(dev, cl, "rpm: get failed %d\n", rets);
583 return rets;
584 }
585
9f81abda
TW
586 cb = mei_io_cb_init(cl, file);
587 if (!cb) {
588 rets = -ENOMEM;
589 goto out;
590 }
591
02a7eecc 592 cb->fop_type = MEI_FOP_CONNECT;
9f81abda 593
6aae48ff
TW
594 /* run hbuf acquire last so we don't have to undo */
595 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
e4d8270e 596 cl->state = MEI_FILE_CONNECTING;
9f81abda
TW
597 if (mei_hbm_cl_connect_req(dev, cl)) {
598 rets = -ENODEV;
599 goto out;
600 }
601 cl->timer_count = MEI_CONNECT_TIMEOUT;
602 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
603 } else {
73ab4232 604 cl->state = MEI_FILE_INITIALIZING;
9f81abda
TW
605 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
606 }
607
608 mutex_unlock(&dev->device_lock);
285e2996
AU
609 wait_event_timeout(dev->wait_recvd_msg,
610 (cl->state == MEI_FILE_CONNECTED ||
611 cl->state == MEI_FILE_DISCONNECTED),
612 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
9f81abda
TW
613 mutex_lock(&dev->device_lock);
614
615 if (cl->state != MEI_FILE_CONNECTED) {
3e37ebb7 616 cl->state = MEI_FILE_DISCONNECTED;
285e2996
AU
617 /* something went really wrong */
618 if (!cl->status)
619 cl->status = -EFAULT;
9f81abda
TW
620
621 mei_io_list_flush(&dev->ctrl_rd_list, cl);
622 mei_io_list_flush(&dev->ctrl_wr_list, cl);
9f81abda
TW
623 }
624
625 rets = cl->status;
626
627out:
04bb139a
TW
628 cl_dbg(dev, cl, "rpm: autosuspend\n");
629 pm_runtime_mark_last_busy(&dev->pdev->dev);
630 pm_runtime_put_autosuspend(&dev->pdev->dev);
631
9f81abda
TW
632 mei_io_cb_free(cb);
633 return rets;
634}
635
9ca9050b 636/**
90e0b5f1 637 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
9ca9050b 638 *
9ca9050b
TW
639 * @cl: private data of the file object
640 *
641 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
642 * -ENOENT if mei_cl is not present
643 * -EINVAL if single_recv_buf == 0
644 */
90e0b5f1 645int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
9ca9050b 646{
90e0b5f1 647 struct mei_device *dev;
12d00665
AU
648 struct mei_me_client *me_cl;
649 int id;
9ca9050b 650
90e0b5f1
TW
651 if (WARN_ON(!cl || !cl->dev))
652 return -EINVAL;
653
654 dev = cl->dev;
655
9ca9050b
TW
656 if (!dev->me_clients_num)
657 return 0;
658
659 if (cl->mei_flow_ctrl_creds > 0)
660 return 1;
661
12d00665
AU
662 id = mei_me_cl_by_id(dev, cl->me_client_id);
663 if (id < 0) {
664 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
665 return id;
9ca9050b 666 }
12d00665
AU
667
668 me_cl = &dev->me_clients[id];
669 if (me_cl->mei_flow_ctrl_creds) {
670 if (WARN_ON(me_cl->props.single_recv_buf == 0))
671 return -EINVAL;
672 return 1;
673 }
674 return 0;
9ca9050b
TW
675}
676
677/**
90e0b5f1 678 * mei_cl_flow_ctrl_reduce - reduces flow_control.
9ca9050b 679 *
9ca9050b 680 * @cl: private data of the file object
393b148f 681 *
9ca9050b
TW
682 * @returns
683 * 0 on success
684 * -ENOENT when me client is not found
685 * -EINVAL when ctrl credits are <= 0
686 */
90e0b5f1 687int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
9ca9050b 688{
90e0b5f1 689 struct mei_device *dev;
12d00665
AU
690 struct mei_me_client *me_cl;
691 int id;
9ca9050b 692
90e0b5f1
TW
693 if (WARN_ON(!cl || !cl->dev))
694 return -EINVAL;
695
696 dev = cl->dev;
697
12d00665
AU
698 id = mei_me_cl_by_id(dev, cl->me_client_id);
699 if (id < 0) {
700 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
701 return id;
702 }
9ca9050b 703
12d00665
AU
704 me_cl = &dev->me_clients[id];
705 if (me_cl->props.single_recv_buf != 0) {
706 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
707 return -EINVAL;
708 me_cl->mei_flow_ctrl_creds--;
709 } else {
710 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
711 return -EINVAL;
712 cl->mei_flow_ctrl_creds--;
9ca9050b 713 }
12d00665 714 return 0;
9ca9050b
TW
715}
716
ab841160 717/**
393b148f 718 * mei_cl_read_start - the start read client message function.
ab841160 719 *
90e0b5f1 720 * @cl: host client
ab841160
OW
721 *
722 * returns 0 on success, <0 on failure.
723 */
fcb136e1 724int mei_cl_read_start(struct mei_cl *cl, size_t length)
ab841160 725{
90e0b5f1 726 struct mei_device *dev;
ab841160 727 struct mei_cl_cb *cb;
664df38b 728 int rets;
ab841160
OW
729 int i;
730
90e0b5f1
TW
731 if (WARN_ON(!cl || !cl->dev))
732 return -ENODEV;
733
734 dev = cl->dev;
735
b950ac1d 736 if (!mei_cl_is_connected(cl))
ab841160
OW
737 return -ENODEV;
738
d91aaed3 739 if (cl->read_cb) {
c0abffbd 740 cl_dbg(dev, cl, "read is pending.\n");
ab841160
OW
741 return -EBUSY;
742 }
664df38b
TW
743 i = mei_me_cl_by_id(dev, cl->me_client_id);
744 if (i < 0) {
c0abffbd 745 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
7ca96aa2 746 return -ENOTTY;
664df38b 747 }
ab841160 748
04bb139a
TW
749 rets = pm_runtime_get(&dev->pdev->dev);
750 if (rets < 0 && rets != -EINPROGRESS) {
751 pm_runtime_put_noidle(&dev->pdev->dev);
752 cl_err(dev, cl, "rpm: get failed %d\n", rets);
753 return rets;
754 }
755
664df38b 756 cb = mei_io_cb_init(cl, NULL);
04bb139a
TW
757 if (!cb) {
758 rets = -ENOMEM;
759 goto out;
760 }
ab841160 761
fcb136e1
TW
762 /* always allocate at least client max message */
763 length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
764 rets = mei_io_cb_alloc_resp_buf(cb, length);
664df38b 765 if (rets)
04bb139a 766 goto out;
ab841160 767
4b8960b4 768 cb->fop_type = MEI_FOP_READ;
6aae48ff 769 if (mei_hbuf_acquire(dev)) {
86113500
AU
770 rets = mei_hbm_cl_flow_control_req(dev, cl);
771 if (rets < 0)
04bb139a 772 goto out;
04bb139a 773
fb601adb 774 list_add_tail(&cb->list, &dev->read_list.list);
ab841160 775 } else {
fb601adb 776 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
ab841160 777 }
accb884b
CB
778
779 cl->read_cb = cb;
780
04bb139a
TW
781out:
782 cl_dbg(dev, cl, "rpm: autosuspend\n");
783 pm_runtime_mark_last_busy(&dev->pdev->dev);
784 pm_runtime_put_autosuspend(&dev->pdev->dev);
785
786 if (rets)
787 mei_io_cb_free(cb);
788
ab841160
OW
789 return rets;
790}
791
21767546 792/**
9d098192 793 * mei_cl_irq_write - write a message to device
21767546
TW
794 * from the interrupt thread context
795 *
796 * @cl: client
797 * @cb: callback block.
21767546
TW
798 * @cmpl_list: complete list.
799 *
800 * returns 0, OK; otherwise error.
801 */
9d098192
TW
802int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
803 struct mei_cl_cb *cmpl_list)
21767546 804{
136698e5
TW
805 struct mei_device *dev;
806 struct mei_msg_data *buf;
21767546 807 struct mei_msg_hdr mei_hdr;
136698e5
TW
808 size_t len;
809 u32 msg_slots;
9d098192 810 int slots;
2ebf8c94 811 int rets;
21767546 812
136698e5
TW
813 if (WARN_ON(!cl || !cl->dev))
814 return -ENODEV;
815
816 dev = cl->dev;
817
818 buf = &cb->request_buffer;
819
820 rets = mei_cl_flow_ctrl_creds(cl);
821 if (rets < 0)
822 return rets;
823
824 if (rets == 0) {
04bb139a 825 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
136698e5
TW
826 return 0;
827 }
828
9d098192 829 slots = mei_hbuf_empty_slots(dev);
136698e5
TW
830 len = buf->size - cb->buf_idx;
831 msg_slots = mei_data2slots(len);
832
21767546
TW
833 mei_hdr.host_addr = cl->host_client_id;
834 mei_hdr.me_addr = cl->me_client_id;
835 mei_hdr.reserved = 0;
479327fc 836 mei_hdr.internal = cb->internal;
21767546 837
9d098192 838 if (slots >= msg_slots) {
21767546
TW
839 mei_hdr.length = len;
840 mei_hdr.msg_complete = 1;
841 /* Split the message only if we can write the whole host buffer */
9d098192
TW
842 } else if (slots == dev->hbuf_depth) {
843 msg_slots = slots;
844 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
21767546
TW
845 mei_hdr.length = len;
846 mei_hdr.msg_complete = 0;
847 } else {
848 /* wait for next time the host buffer is empty */
849 return 0;
850 }
851
c0abffbd 852 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
21767546 853 cb->request_buffer.size, cb->buf_idx);
21767546 854
136698e5 855 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
2ebf8c94
TW
856 if (rets) {
857 cl->status = rets;
21767546 858 list_move_tail(&cb->list, &cmpl_list->list);
2ebf8c94 859 return rets;
21767546
TW
860 }
861
862 cl->status = 0;
4dfaa9f7 863 cl->writing_state = MEI_WRITING;
21767546 864 cb->buf_idx += mei_hdr.length;
4dfaa9f7 865
21767546
TW
866 if (mei_hdr.msg_complete) {
867 if (mei_cl_flow_ctrl_reduce(cl))
2ebf8c94 868 return -EIO;
21767546
TW
869 list_move_tail(&cb->list, &dev->write_waiting_list.list);
870 }
871
872 return 0;
873}
874
4234a6de
TW
875/**
876 * mei_cl_write - submit a write cb to mei device
877 assumes device_lock is locked
878 *
879 * @cl: host client
880 * @cl: write callback with filled data
881 *
83ce0741 882 * returns number of bytes sent on success, <0 on failure.
4234a6de
TW
883 */
884int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
885{
886 struct mei_device *dev;
887 struct mei_msg_data *buf;
888 struct mei_msg_hdr mei_hdr;
889 int rets;
890
891
892 if (WARN_ON(!cl || !cl->dev))
893 return -ENODEV;
894
895 if (WARN_ON(!cb))
896 return -EINVAL;
897
898 dev = cl->dev;
899
900
901 buf = &cb->request_buffer;
902
c0abffbd 903 cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
4234a6de 904
04bb139a
TW
905 rets = pm_runtime_get(&dev->pdev->dev);
906 if (rets < 0 && rets != -EINPROGRESS) {
907 pm_runtime_put_noidle(&dev->pdev->dev);
908 cl_err(dev, cl, "rpm: get failed %d\n", rets);
909 return rets;
910 }
4234a6de
TW
911
912 cb->fop_type = MEI_FOP_WRITE;
6aae48ff
TW
913 cb->buf_idx = 0;
914 cl->writing_state = MEI_IDLE;
915
916 mei_hdr.host_addr = cl->host_client_id;
917 mei_hdr.me_addr = cl->me_client_id;
918 mei_hdr.reserved = 0;
919 mei_hdr.msg_complete = 0;
920 mei_hdr.internal = cb->internal;
4234a6de
TW
921
922 rets = mei_cl_flow_ctrl_creds(cl);
923 if (rets < 0)
924 goto err;
925
6aae48ff
TW
926 if (rets == 0) {
927 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
928 rets = buf->size;
929 goto out;
930 }
931 if (!mei_hbuf_acquire(dev)) {
932 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
4234a6de
TW
933 rets = buf->size;
934 goto out;
935 }
4234a6de
TW
936
937 /* Check for a maximum length */
938 if (buf->size > mei_hbuf_max_len(dev)) {
939 mei_hdr.length = mei_hbuf_max_len(dev);
940 mei_hdr.msg_complete = 0;
941 } else {
942 mei_hdr.length = buf->size;
943 mei_hdr.msg_complete = 1;
944 }
945
2ebf8c94
TW
946 rets = mei_write_message(dev, &mei_hdr, buf->data);
947 if (rets)
4234a6de 948 goto err;
4234a6de
TW
949
950 cl->writing_state = MEI_WRITING;
951 cb->buf_idx = mei_hdr.length;
952
4234a6de
TW
953out:
954 if (mei_hdr.msg_complete) {
7ca96aa2
AU
955 rets = mei_cl_flow_ctrl_reduce(cl);
956 if (rets < 0)
4234a6de 957 goto err;
7ca96aa2 958
4234a6de
TW
959 list_add_tail(&cb->list, &dev->write_waiting_list.list);
960 } else {
961 list_add_tail(&cb->list, &dev->write_list.list);
962 }
963
964
965 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
966
967 mutex_unlock(&dev->device_lock);
7ca96aa2
AU
968 rets = wait_event_interruptible(cl->tx_wait,
969 cl->writing_state == MEI_WRITE_COMPLETE);
4234a6de 970 mutex_lock(&dev->device_lock);
7ca96aa2
AU
971 /* wait_event_interruptible returns -ERESTARTSYS */
972 if (rets) {
973 if (signal_pending(current))
974 rets = -EINTR;
975 goto err;
976 }
4234a6de 977 }
7ca96aa2
AU
978
979 rets = buf->size;
4234a6de 980err:
04bb139a
TW
981 cl_dbg(dev, cl, "rpm: autosuspend\n");
982 pm_runtime_mark_last_busy(&dev->pdev->dev);
983 pm_runtime_put_autosuspend(&dev->pdev->dev);
984
4234a6de
TW
985 return rets;
986}
987
988
db086fa9
TW
989/**
990 * mei_cl_complete - processes completed operation for a client
991 *
992 * @cl: private data of the file object.
993 * @cb: callback block.
994 */
995void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
996{
997 if (cb->fop_type == MEI_FOP_WRITE) {
998 mei_io_cb_free(cb);
999 cb = NULL;
1000 cl->writing_state = MEI_WRITE_COMPLETE;
1001 if (waitqueue_active(&cl->tx_wait))
1002 wake_up_interruptible(&cl->tx_wait);
1003
1004 } else if (cb->fop_type == MEI_FOP_READ &&
1005 MEI_READING == cl->reading_state) {
1006 cl->reading_state = MEI_READ_COMPLETE;
1007 if (waitqueue_active(&cl->rx_wait))
1008 wake_up_interruptible(&cl->rx_wait);
1009 else
1010 mei_cl_bus_rx_event(cl);
1011
1012 }
1013}
1014
4234a6de 1015
074b4c01
TW
1016/**
1017 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1018 *
1019 * @dev - mei device
1020 */
1021
1022void mei_cl_all_disconnect(struct mei_device *dev)
1023{
31f88f57 1024 struct mei_cl *cl;
074b4c01 1025
31f88f57 1026 list_for_each_entry(cl, &dev->file_list, link) {
074b4c01
TW
1027 cl->state = MEI_FILE_DISCONNECTED;
1028 cl->mei_flow_ctrl_creds = 0;
074b4c01
TW
1029 cl->timer_count = 0;
1030 }
1031}
1032
1033
1034/**
5290801c 1035 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
074b4c01
TW
1036 *
1037 * @dev - mei device
1038 */
5290801c 1039void mei_cl_all_wakeup(struct mei_device *dev)
074b4c01 1040{
31f88f57
TW
1041 struct mei_cl *cl;
1042 list_for_each_entry(cl, &dev->file_list, link) {
074b4c01 1043 if (waitqueue_active(&cl->rx_wait)) {
c0abffbd 1044 cl_dbg(dev, cl, "Waking up reading client!\n");
074b4c01
TW
1045 wake_up_interruptible(&cl->rx_wait);
1046 }
5290801c 1047 if (waitqueue_active(&cl->tx_wait)) {
c0abffbd 1048 cl_dbg(dev, cl, "Waking up writing client!\n");
5290801c
TW
1049 wake_up_interruptible(&cl->tx_wait);
1050 }
074b4c01
TW
1051 }
1052}
1053
1054/**
1055 * mei_cl_all_write_clear - clear all pending writes
1056
1057 * @dev - mei device
1058 */
1059void mei_cl_all_write_clear(struct mei_device *dev)
1060{
cc99ecfd
TW
1061 mei_io_list_free(&dev->write_list, NULL);
1062 mei_io_list_free(&dev->write_waiting_list, NULL);
074b4c01
TW
1063}
1064
1065