blob: f84d332ef05965debd42ffdb5772df864c7a59fb [file] [log] [blame]
Tomas Winkler9ca90502013-01-08 23:07:13 +02001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
16
17#include <linux/pci.h>
18#include <linux/sched.h>
19#include <linux/wait.h>
20#include <linux/delay.h>
21
22#include <linux/mei.h>
23
24#include "mei_dev.h"
25#include "hbm.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020026#include "client.h"
27
28/**
29 * mei_me_cl_by_uuid - locate index of me client
30 *
31 * @dev: mei device
32 * returns me client index or -ENOENT if not found
33 */
34int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid)
35{
36 int i, res = -ENOENT;
37
38 for (i = 0; i < dev->me_clients_num; ++i)
39 if (uuid_le_cmp(*uuid,
40 dev->me_clients[i].props.protocol_name) == 0) {
41 res = i;
42 break;
43 }
44
45 return res;
46}
47
48
49/**
50 * mei_me_cl_by_id return index to me_clients for client_id
51 *
52 * @dev: the device structure
53 * @client_id: me client id
54 *
55 * Locking: called under "dev->device_lock" lock
56 *
57 * returns index on success, -ENOENT on failure.
58 */
59
60int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
61{
62 int i;
63 for (i = 0; i < dev->me_clients_num; i++)
64 if (dev->me_clients[i].client_id == client_id)
65 break;
66 if (WARN_ON(dev->me_clients[i].client_id != client_id))
67 return -ENOENT;
68
69 if (i == dev->me_clients_num)
70 return -ENOENT;
71
72 return i;
73}
Tomas Winkler9ca90502013-01-08 23:07:13 +020074
75
76/**
77 * mei_io_list_flush - removes list entry belonging to cl.
78 *
79 * @list: An instance of our list structure
80 * @cl: host client
81 */
82void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
83{
84 struct mei_cl_cb *cb;
85 struct mei_cl_cb *next;
86
87 list_for_each_entry_safe(cb, next, &list->list, list) {
88 if (cb->cl && mei_cl_cmp_id(cl, cb->cl))
89 list_del(&cb->list);
90 }
91}
92
93/**
94 * mei_io_cb_free - free mei_cb_private related memory
95 *
96 * @cb: mei callback struct
97 */
98void mei_io_cb_free(struct mei_cl_cb *cb)
99{
100 if (cb == NULL)
101 return;
102
103 kfree(cb->request_buffer.data);
104 kfree(cb->response_buffer.data);
105 kfree(cb);
106}
107
108/**
109 * mei_io_cb_init - allocate and initialize io callback
110 *
111 * @cl - mei client
Masanari Iida393b1482013-04-05 01:05:05 +0900112 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200113 *
114 * returns mei_cl_cb pointer or NULL;
115 */
116struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
117{
118 struct mei_cl_cb *cb;
119
120 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
121 if (!cb)
122 return NULL;
123
124 mei_io_list_init(cb);
125
126 cb->file_object = fp;
127 cb->cl = cl;
128 cb->buf_idx = 0;
129 return cb;
130}
131
132/**
133 * mei_io_cb_alloc_req_buf - allocate request buffer
134 *
Masanari Iida393b1482013-04-05 01:05:05 +0900135 * @cb: io callback structure
136 * @length: size of the buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200137 *
138 * returns 0 on success
139 * -EINVAL if cb is NULL
140 * -ENOMEM if allocation failed
141 */
142int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
143{
144 if (!cb)
145 return -EINVAL;
146
147 if (length == 0)
148 return 0;
149
150 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
151 if (!cb->request_buffer.data)
152 return -ENOMEM;
153 cb->request_buffer.size = length;
154 return 0;
155}
156/**
Masanari Iida393b1482013-04-05 01:05:05 +0900157 * mei_io_cb_alloc_resp_buf - allocate respose buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200158 *
Masanari Iida393b1482013-04-05 01:05:05 +0900159 * @cb: io callback structure
160 * @length: size of the buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200161 *
162 * returns 0 on success
163 * -EINVAL if cb is NULL
164 * -ENOMEM if allocation failed
165 */
166int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
167{
168 if (!cb)
169 return -EINVAL;
170
171 if (length == 0)
172 return 0;
173
174 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
175 if (!cb->response_buffer.data)
176 return -ENOMEM;
177 cb->response_buffer.size = length;
178 return 0;
179}
180
181
182
183/**
184 * mei_cl_flush_queues - flushes queue lists belonging to cl.
185 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200186 * @cl: host client
187 */
188int mei_cl_flush_queues(struct mei_cl *cl)
189{
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300190 struct mei_device *dev;
191
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200192 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200193 return -EINVAL;
194
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300195 dev = cl->dev;
196
197 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200198 mei_io_list_flush(&cl->dev->read_list, cl);
199 mei_io_list_flush(&cl->dev->write_list, cl);
200 mei_io_list_flush(&cl->dev->write_waiting_list, cl);
201 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
202 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
203 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
204 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
205 return 0;
206}
207
Tomas Winkler9ca90502013-01-08 23:07:13 +0200208
209/**
210 * mei_cl_init - initializes intialize cl.
211 *
212 * @cl: host client to be initialized
213 * @dev: mei device
214 */
215void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
216{
217 memset(cl, 0, sizeof(struct mei_cl));
218 init_waitqueue_head(&cl->wait);
219 init_waitqueue_head(&cl->rx_wait);
220 init_waitqueue_head(&cl->tx_wait);
221 INIT_LIST_HEAD(&cl->link);
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200222 INIT_LIST_HEAD(&cl->device_link);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200223 cl->reading_state = MEI_IDLE;
224 cl->writing_state = MEI_IDLE;
225 cl->dev = dev;
226}
227
228/**
229 * mei_cl_allocate - allocates cl structure and sets it up.
230 *
231 * @dev: mei device
232 * returns The allocated file or NULL on failure
233 */
234struct mei_cl *mei_cl_allocate(struct mei_device *dev)
235{
236 struct mei_cl *cl;
237
238 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
239 if (!cl)
240 return NULL;
241
242 mei_cl_init(cl, dev);
243
244 return cl;
245}
246
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200247/**
248 * mei_cl_find_read_cb - find this cl's callback in the read list
249 *
Masanari Iida393b1482013-04-05 01:05:05 +0900250 * @cl: host client
251 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200252 * returns cb on success, NULL on error
253 */
254struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
255{
256 struct mei_device *dev = cl->dev;
257 struct mei_cl_cb *cb = NULL;
258 struct mei_cl_cb *next = NULL;
259
260 list_for_each_entry_safe(cb, next, &dev->read_list.list, list)
261 if (mei_cl_cmp_id(cl, cb->cl))
262 return cb;
263 return NULL;
264}
265
Tomas Winkler781d0d82013-01-08 23:07:22 +0200266/** mei_cl_link: allocte host id in the host map
Tomas Winkler9ca90502013-01-08 23:07:13 +0200267 *
Tomas Winkler781d0d82013-01-08 23:07:22 +0200268 * @cl - host client
269 * @id - fixed host id or -1 for genereting one
Masanari Iida393b1482013-04-05 01:05:05 +0900270 *
Tomas Winkler781d0d82013-01-08 23:07:22 +0200271 * returns 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200272 * -EINVAL on incorrect values
273 * -ENONET if client not found
274 */
Tomas Winkler781d0d82013-01-08 23:07:22 +0200275int mei_cl_link(struct mei_cl *cl, int id)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200276{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200277 struct mei_device *dev;
Tomas Winkler22f96a02013-09-16 23:44:47 +0300278 long open_handle_count;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200279
Tomas Winkler781d0d82013-01-08 23:07:22 +0200280 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200281 return -EINVAL;
282
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200283 dev = cl->dev;
284
Tomas Winkler781d0d82013-01-08 23:07:22 +0200285 /* If Id is not asigned get one*/
286 if (id == MEI_HOST_CLIENT_ID_ANY)
287 id = find_first_zero_bit(dev->host_clients_map,
288 MEI_CLIENTS_MAX);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200289
Tomas Winkler781d0d82013-01-08 23:07:22 +0200290 if (id >= MEI_CLIENTS_MAX) {
291 dev_err(&dev->pdev->dev, "id exceded %d", MEI_CLIENTS_MAX) ;
Tomas Winklere036cc52013-09-16 23:44:46 +0300292 return -EMFILE;
293 }
294
Tomas Winkler22f96a02013-09-16 23:44:47 +0300295 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
296 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
Tomas Winklere036cc52013-09-16 23:44:46 +0300297 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
298 MEI_MAX_OPEN_HANDLE_COUNT);
299 return -EMFILE;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200300 }
301
Tomas Winkler781d0d82013-01-08 23:07:22 +0200302 dev->open_handle_count++;
303
304 cl->host_client_id = id;
305 list_add_tail(&cl->link, &dev->file_list);
306
307 set_bit(id, dev->host_clients_map);
308
309 cl->state = MEI_FILE_INITIALIZING;
310
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300311 cl_dbg(dev, cl, "link cl\n");
Tomas Winkler781d0d82013-01-08 23:07:22 +0200312 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200313}
Tomas Winkler781d0d82013-01-08 23:07:22 +0200314
Tomas Winkler9ca90502013-01-08 23:07:13 +0200315/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200316 * mei_cl_unlink - remove me_cl from the list
Tomas Winkler9ca90502013-01-08 23:07:13 +0200317 *
Masanari Iida393b1482013-04-05 01:05:05 +0900318 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200319 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200320int mei_cl_unlink(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200321{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200322 struct mei_device *dev;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200323
Tomas Winkler781d0d82013-01-08 23:07:22 +0200324 /* don't shout on error exit path */
325 if (!cl)
326 return 0;
327
Tomas Winkler8e9a4a92013-01-10 17:32:14 +0200328 /* wd and amthif might not be initialized */
329 if (!cl->dev)
330 return 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200331
332 dev = cl->dev;
333
Tomas Winklera14c44d2013-09-16 23:44:45 +0300334 cl_dbg(dev, cl, "unlink client");
335
Tomas Winkler22f96a02013-09-16 23:44:47 +0300336 if (dev->open_handle_count > 0)
337 dev->open_handle_count--;
338
339 /* never clear the 0 bit */
340 if (cl->host_client_id)
341 clear_bit(cl->host_client_id, dev->host_clients_map);
342
343 list_del_init(&cl->link);
344
345 cl->state = MEI_FILE_INITIALIZING;
346
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200347 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200348}
349
350
351void mei_host_client_init(struct work_struct *work)
352{
353 struct mei_device *dev = container_of(work,
354 struct mei_device, init_work);
355 struct mei_client_properties *client_props;
356 int i;
357
358 mutex_lock(&dev->device_lock);
359
Tomas Winkler9ca90502013-01-08 23:07:13 +0200360 for (i = 0; i < dev->me_clients_num; i++) {
361 client_props = &dev->me_clients[i].props;
362
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200363 if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200364 mei_amthif_host_init(dev);
365 else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
366 mei_wd_host_init(dev);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200367 else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
368 mei_nfc_host_init(dev);
369
Tomas Winkler9ca90502013-01-08 23:07:13 +0200370 }
371
372 dev->dev_state = MEI_DEV_ENABLED;
373
374 mutex_unlock(&dev->device_lock);
375}
376
377
378/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200379 * mei_cl_disconnect - disconnect host clinet form the me one
Tomas Winkler9ca90502013-01-08 23:07:13 +0200380 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200381 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200382 *
383 * Locking: called under "dev->device_lock" lock
384 *
385 * returns 0 on success, <0 on failure.
386 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200387int mei_cl_disconnect(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200388{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200389 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200390 struct mei_cl_cb *cb;
391 int rets, err;
392
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200393 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200394 return -ENODEV;
395
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200396 dev = cl->dev;
397
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300398 cl_dbg(dev, cl, "disconnecting");
399
Tomas Winkler9ca90502013-01-08 23:07:13 +0200400 if (cl->state != MEI_FILE_DISCONNECTING)
401 return 0;
402
403 cb = mei_io_cb_init(cl, NULL);
404 if (!cb)
405 return -ENOMEM;
406
407 cb->fop_type = MEI_FOP_CLOSE;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200408 if (dev->hbuf_is_ready) {
409 dev->hbuf_is_ready = false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200410 if (mei_hbm_cl_disconnect_req(dev, cl)) {
411 rets = -ENODEV;
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300412 cl_err(dev, cl, "failed to disconnect.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200413 goto free;
414 }
415 mdelay(10); /* Wait for hardware disconnection ready */
416 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
417 } else {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300418 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200419 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
420
421 }
422 mutex_unlock(&dev->device_lock);
423
424 err = wait_event_timeout(dev->wait_recvd_msg,
425 MEI_FILE_DISCONNECTED == cl->state,
426 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
427
428 mutex_lock(&dev->device_lock);
429 if (MEI_FILE_DISCONNECTED == cl->state) {
430 rets = 0;
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300431 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200432 } else {
433 rets = -ENODEV;
434 if (MEI_FILE_DISCONNECTED != cl->state)
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300435 cl_err(dev, cl, "wrong status client disconnect.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200436
437 if (err)
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300438 cl_dbg(dev, cl, "wait failed disconnect err=%08x\n",
Tomas Winkler9ca90502013-01-08 23:07:13 +0200439 err);
440
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300441 cl_err(dev, cl, "failed to disconnect from FW client.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200442 }
443
444 mei_io_list_flush(&dev->ctrl_rd_list, cl);
445 mei_io_list_flush(&dev->ctrl_wr_list, cl);
446free:
447 mei_io_cb_free(cb);
448 return rets;
449}
450
451
452/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200453 * mei_cl_is_other_connecting - checks if other
454 * client with the same me client id is connecting
Tomas Winkler9ca90502013-01-08 23:07:13 +0200455 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200456 * @cl: private data of the file object
457 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200458 * returns ture if other client is connected, 0 - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200459 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200460bool mei_cl_is_other_connecting(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200461{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200462 struct mei_device *dev;
463 struct mei_cl *pos;
464 struct mei_cl *next;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200465
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200466 if (WARN_ON(!cl || !cl->dev))
467 return false;
468
469 dev = cl->dev;
470
471 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
472 if ((pos->state == MEI_FILE_CONNECTING) &&
473 (pos != cl) && cl->me_client_id == pos->me_client_id)
474 return true;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200475
476 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200477
478 return false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200479}
480
481/**
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200482 * mei_cl_connect - connect host clinet to the me one
483 *
484 * @cl: host client
485 *
486 * Locking: called under "dev->device_lock" lock
487 *
488 * returns 0 on success, <0 on failure.
489 */
490int mei_cl_connect(struct mei_cl *cl, struct file *file)
491{
492 struct mei_device *dev;
493 struct mei_cl_cb *cb;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200494 int rets;
495
496 if (WARN_ON(!cl || !cl->dev))
497 return -ENODEV;
498
499 dev = cl->dev;
500
501 cb = mei_io_cb_init(cl, file);
502 if (!cb) {
503 rets = -ENOMEM;
504 goto out;
505 }
506
507 cb->fop_type = MEI_FOP_IOCTL;
508
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200509 if (dev->hbuf_is_ready && !mei_cl_is_other_connecting(cl)) {
510 dev->hbuf_is_ready = false;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200511
512 if (mei_hbm_cl_connect_req(dev, cl)) {
513 rets = -ENODEV;
514 goto out;
515 }
516 cl->timer_count = MEI_CONNECT_TIMEOUT;
517 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
518 } else {
519 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
520 }
521
522 mutex_unlock(&dev->device_lock);
523 rets = wait_event_timeout(dev->wait_recvd_msg,
524 (cl->state == MEI_FILE_CONNECTED ||
525 cl->state == MEI_FILE_DISCONNECTED),
Frode Isaksen206ecfc2013-06-23 09:37:01 +0300526 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200527 mutex_lock(&dev->device_lock);
528
529 if (cl->state != MEI_FILE_CONNECTED) {
530 rets = -EFAULT;
531
532 mei_io_list_flush(&dev->ctrl_rd_list, cl);
533 mei_io_list_flush(&dev->ctrl_wr_list, cl);
534 goto out;
535 }
536
537 rets = cl->status;
538
539out:
540 mei_io_cb_free(cb);
541 return rets;
542}
543
544/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200545 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200546 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200547 * @cl: private data of the file object
548 *
549 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
550 * -ENOENT if mei_cl is not present
551 * -EINVAL if single_recv_buf == 0
552 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200553int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200554{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200555 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200556 int i;
557
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200558 if (WARN_ON(!cl || !cl->dev))
559 return -EINVAL;
560
561 dev = cl->dev;
562
Tomas Winkler9ca90502013-01-08 23:07:13 +0200563 if (!dev->me_clients_num)
564 return 0;
565
566 if (cl->mei_flow_ctrl_creds > 0)
567 return 1;
568
569 for (i = 0; i < dev->me_clients_num; i++) {
570 struct mei_me_client *me_cl = &dev->me_clients[i];
571 if (me_cl->client_id == cl->me_client_id) {
572 if (me_cl->mei_flow_ctrl_creds) {
573 if (WARN_ON(me_cl->props.single_recv_buf == 0))
574 return -EINVAL;
575 return 1;
576 } else {
577 return 0;
578 }
579 }
580 }
581 return -ENOENT;
582}
583
584/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200585 * mei_cl_flow_ctrl_reduce - reduces flow_control.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200586 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200587 * @cl: private data of the file object
Masanari Iida393b1482013-04-05 01:05:05 +0900588 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200589 * @returns
590 * 0 on success
591 * -ENOENT when me client is not found
592 * -EINVAL when ctrl credits are <= 0
593 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200594int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200595{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200596 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200597 int i;
598
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200599 if (WARN_ON(!cl || !cl->dev))
600 return -EINVAL;
601
602 dev = cl->dev;
603
Tomas Winkler9ca90502013-01-08 23:07:13 +0200604 if (!dev->me_clients_num)
605 return -ENOENT;
606
607 for (i = 0; i < dev->me_clients_num; i++) {
608 struct mei_me_client *me_cl = &dev->me_clients[i];
609 if (me_cl->client_id == cl->me_client_id) {
610 if (me_cl->props.single_recv_buf != 0) {
611 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
612 return -EINVAL;
613 dev->me_clients[i].mei_flow_ctrl_creds--;
614 } else {
615 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
616 return -EINVAL;
617 cl->mei_flow_ctrl_creds--;
618 }
619 return 0;
620 }
621 }
622 return -ENOENT;
623}
624
Tomas Winkler9ca90502013-01-08 23:07:13 +0200625/**
Masanari Iida393b1482013-04-05 01:05:05 +0900626 * mei_cl_read_start - the start read client message function.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200627 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200628 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200629 *
630 * returns 0 on success, <0 on failure.
631 */
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300632int mei_cl_read_start(struct mei_cl *cl, size_t length)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200633{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200634 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200635 struct mei_cl_cb *cb;
636 int rets;
637 int i;
638
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200639 if (WARN_ON(!cl || !cl->dev))
640 return -ENODEV;
641
642 dev = cl->dev;
643
Tomas Winklerb950ac12013-07-25 20:15:53 +0300644 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200645 return -ENODEV;
646
Tomas Winklerd91aaed2013-01-08 23:07:18 +0200647 if (cl->read_cb) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300648 cl_dbg(dev, cl, "read is pending.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200649 return -EBUSY;
650 }
651 i = mei_me_cl_by_id(dev, cl->me_client_id);
652 if (i < 0) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300653 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200654 return -ENODEV;
655 }
656
657 cb = mei_io_cb_init(cl, NULL);
658 if (!cb)
659 return -ENOMEM;
660
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300661 /* always allocate at least client max message */
662 length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
663 rets = mei_io_cb_alloc_resp_buf(cb, length);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200664 if (rets)
665 goto err;
666
667 cb->fop_type = MEI_FOP_READ;
668 cl->read_cb = cb;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200669 if (dev->hbuf_is_ready) {
670 dev->hbuf_is_ready = false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200671 if (mei_hbm_cl_flow_control_req(dev, cl)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300672 cl_err(dev, cl, "flow control send failed\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200673 rets = -ENODEV;
674 goto err;
675 }
676 list_add_tail(&cb->list, &dev->read_list.list);
677 } else {
678 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
679 }
680 return rets;
681err:
682 mei_io_cb_free(cb);
683 return rets;
684}
685
Tomas Winkler074b4c02013-02-06 14:06:44 +0200686/**
Tomas Winkler21767542013-06-23 09:36:59 +0300687 * mei_cl_irq_write_complete - write a message to device
688 * from the interrupt thread context
689 *
690 * @cl: client
691 * @cb: callback block.
692 * @slots: free slots.
693 * @cmpl_list: complete list.
694 *
695 * returns 0, OK; otherwise error.
696 */
697int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
698 s32 *slots, struct mei_cl_cb *cmpl_list)
699{
Tomas Winkler136698e2013-09-16 23:44:44 +0300700 struct mei_device *dev;
701 struct mei_msg_data *buf;
Tomas Winkler21767542013-06-23 09:36:59 +0300702 struct mei_msg_hdr mei_hdr;
Tomas Winkler136698e2013-09-16 23:44:44 +0300703 size_t len;
704 u32 msg_slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300705 int rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300706
Tomas Winkler136698e2013-09-16 23:44:44 +0300707
708 if (WARN_ON(!cl || !cl->dev))
709 return -ENODEV;
710
711 dev = cl->dev;
712
713 buf = &cb->request_buffer;
714
715 rets = mei_cl_flow_ctrl_creds(cl);
716 if (rets < 0)
717 return rets;
718
719 if (rets == 0) {
720 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
721 return 0;
722 }
723
724 len = buf->size - cb->buf_idx;
725 msg_slots = mei_data2slots(len);
726
Tomas Winkler21767542013-06-23 09:36:59 +0300727 mei_hdr.host_addr = cl->host_client_id;
728 mei_hdr.me_addr = cl->me_client_id;
729 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +0200730 mei_hdr.internal = cb->internal;
Tomas Winkler21767542013-06-23 09:36:59 +0300731
732 if (*slots >= msg_slots) {
733 mei_hdr.length = len;
734 mei_hdr.msg_complete = 1;
735 /* Split the message only if we can write the whole host buffer */
736 } else if (*slots == dev->hbuf_depth) {
737 msg_slots = *slots;
738 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
739 mei_hdr.length = len;
740 mei_hdr.msg_complete = 0;
741 } else {
742 /* wait for next time the host buffer is empty */
743 return 0;
744 }
745
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300746 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
Tomas Winkler21767542013-06-23 09:36:59 +0300747 cb->request_buffer.size, cb->buf_idx);
Tomas Winkler21767542013-06-23 09:36:59 +0300748
749 *slots -= msg_slots;
Tomas Winkler136698e2013-09-16 23:44:44 +0300750 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300751 if (rets) {
752 cl->status = rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300753 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300754 return rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300755 }
756
757 cl->status = 0;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +0300758 cl->writing_state = MEI_WRITING;
Tomas Winkler21767542013-06-23 09:36:59 +0300759 cb->buf_idx += mei_hdr.length;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +0300760
Tomas Winkler21767542013-06-23 09:36:59 +0300761 if (mei_hdr.msg_complete) {
762 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300763 return -EIO;
Tomas Winkler21767542013-06-23 09:36:59 +0300764 list_move_tail(&cb->list, &dev->write_waiting_list.list);
765 }
766
767 return 0;
768}
769
770/**
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300771 * mei_cl_write - submit a write cb to mei device
772 assumes device_lock is locked
773 *
774 * @cl: host client
775 * @cl: write callback with filled data
776 *
777 * returns numbe of bytes sent on success, <0 on failure.
778 */
779int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
780{
781 struct mei_device *dev;
782 struct mei_msg_data *buf;
783 struct mei_msg_hdr mei_hdr;
784 int rets;
785
786
787 if (WARN_ON(!cl || !cl->dev))
788 return -ENODEV;
789
790 if (WARN_ON(!cb))
791 return -EINVAL;
792
793 dev = cl->dev;
794
795
796 buf = &cb->request_buffer;
797
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300798 cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300799
800
801 cb->fop_type = MEI_FOP_WRITE;
802
803 rets = mei_cl_flow_ctrl_creds(cl);
804 if (rets < 0)
805 goto err;
806
807 /* Host buffer is not ready, we queue the request */
808 if (rets == 0 || !dev->hbuf_is_ready) {
809 cb->buf_idx = 0;
810 /* unseting complete will enqueue the cb for write */
811 mei_hdr.msg_complete = 0;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300812 rets = buf->size;
813 goto out;
814 }
815
816 dev->hbuf_is_ready = false;
817
818 /* Check for a maximum length */
819 if (buf->size > mei_hbuf_max_len(dev)) {
820 mei_hdr.length = mei_hbuf_max_len(dev);
821 mei_hdr.msg_complete = 0;
822 } else {
823 mei_hdr.length = buf->size;
824 mei_hdr.msg_complete = 1;
825 }
826
827 mei_hdr.host_addr = cl->host_client_id;
828 mei_hdr.me_addr = cl->me_client_id;
829 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +0200830 mei_hdr.internal = cb->internal;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300831
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300832
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300833 rets = mei_write_message(dev, &mei_hdr, buf->data);
834 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300835 goto err;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300836
837 cl->writing_state = MEI_WRITING;
838 cb->buf_idx = mei_hdr.length;
839
840 rets = buf->size;
841out:
842 if (mei_hdr.msg_complete) {
843 if (mei_cl_flow_ctrl_reduce(cl)) {
844 rets = -ENODEV;
845 goto err;
846 }
847 list_add_tail(&cb->list, &dev->write_waiting_list.list);
848 } else {
849 list_add_tail(&cb->list, &dev->write_list.list);
850 }
851
852
853 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
854
855 mutex_unlock(&dev->device_lock);
856 if (wait_event_interruptible(cl->tx_wait,
857 cl->writing_state == MEI_WRITE_COMPLETE)) {
858 if (signal_pending(current))
859 rets = -EINTR;
860 else
861 rets = -ERESTARTSYS;
862 }
863 mutex_lock(&dev->device_lock);
864 }
865err:
866 return rets;
867}
868
869
Tomas Winklerdb086fa2013-05-12 15:34:45 +0300870/**
871 * mei_cl_complete - processes completed operation for a client
872 *
873 * @cl: private data of the file object.
874 * @cb: callback block.
875 */
876void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
877{
878 if (cb->fop_type == MEI_FOP_WRITE) {
879 mei_io_cb_free(cb);
880 cb = NULL;
881 cl->writing_state = MEI_WRITE_COMPLETE;
882 if (waitqueue_active(&cl->tx_wait))
883 wake_up_interruptible(&cl->tx_wait);
884
885 } else if (cb->fop_type == MEI_FOP_READ &&
886 MEI_READING == cl->reading_state) {
887 cl->reading_state = MEI_READ_COMPLETE;
888 if (waitqueue_active(&cl->rx_wait))
889 wake_up_interruptible(&cl->rx_wait);
890 else
891 mei_cl_bus_rx_event(cl);
892
893 }
894}
895
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300896
897/**
Tomas Winkler074b4c02013-02-06 14:06:44 +0200898 * mei_cl_all_disconnect - disconnect forcefully all connected clients
899 *
900 * @dev - mei device
901 */
902
903void mei_cl_all_disconnect(struct mei_device *dev)
904{
905 struct mei_cl *cl, *next;
906
907 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
908 cl->state = MEI_FILE_DISCONNECTED;
909 cl->mei_flow_ctrl_creds = 0;
910 cl->read_cb = NULL;
911 cl->timer_count = 0;
912 }
913}
914
915
916/**
Tomas Winkler52908012013-07-24 16:22:57 +0300917 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
Tomas Winkler074b4c02013-02-06 14:06:44 +0200918 *
919 * @dev - mei device
920 */
Tomas Winkler52908012013-07-24 16:22:57 +0300921void mei_cl_all_wakeup(struct mei_device *dev)
Tomas Winkler074b4c02013-02-06 14:06:44 +0200922{
923 struct mei_cl *cl, *next;
924 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
925 if (waitqueue_active(&cl->rx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300926 cl_dbg(dev, cl, "Waking up reading client!\n");
Tomas Winkler074b4c02013-02-06 14:06:44 +0200927 wake_up_interruptible(&cl->rx_wait);
928 }
Tomas Winkler52908012013-07-24 16:22:57 +0300929 if (waitqueue_active(&cl->tx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300930 cl_dbg(dev, cl, "Waking up writing client!\n");
Tomas Winkler52908012013-07-24 16:22:57 +0300931 wake_up_interruptible(&cl->tx_wait);
932 }
Tomas Winkler074b4c02013-02-06 14:06:44 +0200933 }
934}
935
936/**
937 * mei_cl_all_write_clear - clear all pending writes
938
939 * @dev - mei device
940 */
941void mei_cl_all_write_clear(struct mei_device *dev)
942{
943 struct mei_cl_cb *cb, *next;
944
945 list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
946 list_del(&cb->list);
947 mei_io_cb_free(cb);
948 }
949}
950
951