blob: 1a53d961302a7e1e3d7d474dfed74afad22bb4eb [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 Winkler9ca90502013-01-08 23:07:13 +0200278
Tomas Winkler781d0d82013-01-08 23:07:22 +0200279 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200280 return -EINVAL;
281
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200282 dev = cl->dev;
283
Tomas Winkler781d0d82013-01-08 23:07:22 +0200284 /* If Id is not asigned get one*/
285 if (id == MEI_HOST_CLIENT_ID_ANY)
286 id = find_first_zero_bit(dev->host_clients_map,
287 MEI_CLIENTS_MAX);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200288
Tomas Winkler781d0d82013-01-08 23:07:22 +0200289 if (id >= MEI_CLIENTS_MAX) {
290 dev_err(&dev->pdev->dev, "id exceded %d", MEI_CLIENTS_MAX) ;
291 return -ENOENT;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200292 }
293
Tomas Winklercaaeb092013-09-02 03:11:00 +0300294 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
295 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
296 MEI_MAX_OPEN_HANDLE_COUNT);
297 return -ENOENT;
298 }
299
Tomas Winkler781d0d82013-01-08 23:07:22 +0200300 dev->open_handle_count++;
301
302 cl->host_client_id = id;
303 list_add_tail(&cl->link, &dev->file_list);
304
305 set_bit(id, dev->host_clients_map);
306
307 cl->state = MEI_FILE_INITIALIZING;
308
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300309 cl_dbg(dev, cl, "link cl\n");
Tomas Winkler781d0d82013-01-08 23:07:22 +0200310 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200311}
Tomas Winkler781d0d82013-01-08 23:07:22 +0200312
Tomas Winkler9ca90502013-01-08 23:07:13 +0200313/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200314 * mei_cl_unlink - remove me_cl from the list
Tomas Winkler9ca90502013-01-08 23:07:13 +0200315 *
Masanari Iida393b1482013-04-05 01:05:05 +0900316 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200317 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200318int mei_cl_unlink(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200319{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200320 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200321 struct mei_cl *pos, *next;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200322
Tomas Winkler781d0d82013-01-08 23:07:22 +0200323 /* don't shout on error exit path */
324 if (!cl)
325 return 0;
326
Tomas Winkler8e9a4a92013-01-10 17:32:14 +0200327 /* wd and amthif might not be initialized */
328 if (!cl->dev)
329 return 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200330
331 dev = cl->dev;
332
Tomas Winkler9ca90502013-01-08 23:07:13 +0200333 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
334 if (cl->host_client_id == pos->host_client_id) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300335 cl_dbg(dev, cl, "remove host client = %d, ME client = %d\n",
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200336 pos->host_client_id, pos->me_client_id);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200337 list_del_init(&pos->link);
338 break;
339 }
340 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200341 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200342}
343
344
345void mei_host_client_init(struct work_struct *work)
346{
347 struct mei_device *dev = container_of(work,
348 struct mei_device, init_work);
349 struct mei_client_properties *client_props;
350 int i;
351
352 mutex_lock(&dev->device_lock);
353
354 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
355 dev->open_handle_count = 0;
356
357 /*
358 * Reserving the first three client IDs
359 * 0: Reserved for MEI Bus Message communications
360 * 1: Reserved for Watchdog
361 * 2: Reserved for AMTHI
362 */
363 bitmap_set(dev->host_clients_map, 0, 3);
364
365 for (i = 0; i < dev->me_clients_num; i++) {
366 client_props = &dev->me_clients[i].props;
367
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200368 if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200369 mei_amthif_host_init(dev);
370 else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
371 mei_wd_host_init(dev);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200372 else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
373 mei_nfc_host_init(dev);
374
Tomas Winkler9ca90502013-01-08 23:07:13 +0200375 }
376
377 dev->dev_state = MEI_DEV_ENABLED;
378
379 mutex_unlock(&dev->device_lock);
380}
381
382
383/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200384 * mei_cl_disconnect - disconnect host clinet form the me one
Tomas Winkler9ca90502013-01-08 23:07:13 +0200385 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200386 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200387 *
388 * Locking: called under "dev->device_lock" lock
389 *
390 * returns 0 on success, <0 on failure.
391 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200392int mei_cl_disconnect(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200393{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200394 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200395 struct mei_cl_cb *cb;
396 int rets, err;
397
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200398 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200399 return -ENODEV;
400
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200401 dev = cl->dev;
402
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300403 cl_dbg(dev, cl, "disconnecting");
404
Tomas Winkler9ca90502013-01-08 23:07:13 +0200405 if (cl->state != MEI_FILE_DISCONNECTING)
406 return 0;
407
408 cb = mei_io_cb_init(cl, NULL);
409 if (!cb)
410 return -ENOMEM;
411
412 cb->fop_type = MEI_FOP_CLOSE;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200413 if (dev->hbuf_is_ready) {
414 dev->hbuf_is_ready = false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200415 if (mei_hbm_cl_disconnect_req(dev, cl)) {
416 rets = -ENODEV;
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300417 cl_err(dev, cl, "failed to disconnect.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200418 goto free;
419 }
420 mdelay(10); /* Wait for hardware disconnection ready */
421 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
422 } else {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300423 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200424 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
425
426 }
427 mutex_unlock(&dev->device_lock);
428
429 err = wait_event_timeout(dev->wait_recvd_msg,
430 MEI_FILE_DISCONNECTED == cl->state,
431 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
432
433 mutex_lock(&dev->device_lock);
434 if (MEI_FILE_DISCONNECTED == cl->state) {
435 rets = 0;
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300436 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200437 } else {
438 rets = -ENODEV;
439 if (MEI_FILE_DISCONNECTED != cl->state)
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300440 cl_err(dev, cl, "wrong status client disconnect.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200441
442 if (err)
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300443 cl_dbg(dev, cl, "wait failed disconnect err=%08x\n",
Tomas Winkler9ca90502013-01-08 23:07:13 +0200444 err);
445
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300446 cl_err(dev, cl, "failed to disconnect from FW client.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200447 }
448
449 mei_io_list_flush(&dev->ctrl_rd_list, cl);
450 mei_io_list_flush(&dev->ctrl_wr_list, cl);
451free:
452 mei_io_cb_free(cb);
453 return rets;
454}
455
456
457/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200458 * mei_cl_is_other_connecting - checks if other
459 * client with the same me client id is connecting
Tomas Winkler9ca90502013-01-08 23:07:13 +0200460 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200461 * @cl: private data of the file object
462 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200463 * returns ture if other client is connected, 0 - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200464 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200465bool mei_cl_is_other_connecting(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200466{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200467 struct mei_device *dev;
468 struct mei_cl *pos;
469 struct mei_cl *next;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200470
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200471 if (WARN_ON(!cl || !cl->dev))
472 return false;
473
474 dev = cl->dev;
475
476 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
477 if ((pos->state == MEI_FILE_CONNECTING) &&
478 (pos != cl) && cl->me_client_id == pos->me_client_id)
479 return true;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200480
481 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200482
483 return false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200484}
485
486/**
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200487 * mei_cl_connect - connect host clinet to the me one
488 *
489 * @cl: host client
490 *
491 * Locking: called under "dev->device_lock" lock
492 *
493 * returns 0 on success, <0 on failure.
494 */
495int mei_cl_connect(struct mei_cl *cl, struct file *file)
496{
497 struct mei_device *dev;
498 struct mei_cl_cb *cb;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200499 int rets;
500
501 if (WARN_ON(!cl || !cl->dev))
502 return -ENODEV;
503
504 dev = cl->dev;
505
506 cb = mei_io_cb_init(cl, file);
507 if (!cb) {
508 rets = -ENOMEM;
509 goto out;
510 }
511
512 cb->fop_type = MEI_FOP_IOCTL;
513
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200514 if (dev->hbuf_is_ready && !mei_cl_is_other_connecting(cl)) {
515 dev->hbuf_is_ready = false;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200516
517 if (mei_hbm_cl_connect_req(dev, cl)) {
518 rets = -ENODEV;
519 goto out;
520 }
521 cl->timer_count = MEI_CONNECT_TIMEOUT;
522 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
523 } else {
524 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
525 }
526
527 mutex_unlock(&dev->device_lock);
528 rets = wait_event_timeout(dev->wait_recvd_msg,
529 (cl->state == MEI_FILE_CONNECTED ||
530 cl->state == MEI_FILE_DISCONNECTED),
Frode Isaksen206ecfc2013-06-23 09:37:01 +0300531 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200532 mutex_lock(&dev->device_lock);
533
534 if (cl->state != MEI_FILE_CONNECTED) {
535 rets = -EFAULT;
536
537 mei_io_list_flush(&dev->ctrl_rd_list, cl);
538 mei_io_list_flush(&dev->ctrl_wr_list, cl);
539 goto out;
540 }
541
542 rets = cl->status;
543
544out:
545 mei_io_cb_free(cb);
546 return rets;
547}
548
549/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200550 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200551 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200552 * @cl: private data of the file object
553 *
554 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
555 * -ENOENT if mei_cl is not present
556 * -EINVAL if single_recv_buf == 0
557 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200558int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200559{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200560 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200561 int i;
562
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200563 if (WARN_ON(!cl || !cl->dev))
564 return -EINVAL;
565
566 dev = cl->dev;
567
Tomas Winkler9ca90502013-01-08 23:07:13 +0200568 if (!dev->me_clients_num)
569 return 0;
570
571 if (cl->mei_flow_ctrl_creds > 0)
572 return 1;
573
574 for (i = 0; i < dev->me_clients_num; i++) {
575 struct mei_me_client *me_cl = &dev->me_clients[i];
576 if (me_cl->client_id == cl->me_client_id) {
577 if (me_cl->mei_flow_ctrl_creds) {
578 if (WARN_ON(me_cl->props.single_recv_buf == 0))
579 return -EINVAL;
580 return 1;
581 } else {
582 return 0;
583 }
584 }
585 }
586 return -ENOENT;
587}
588
589/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200590 * mei_cl_flow_ctrl_reduce - reduces flow_control.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200591 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200592 * @cl: private data of the file object
Masanari Iida393b1482013-04-05 01:05:05 +0900593 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200594 * @returns
595 * 0 on success
596 * -ENOENT when me client is not found
597 * -EINVAL when ctrl credits are <= 0
598 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200599int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200600{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200601 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200602 int i;
603
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200604 if (WARN_ON(!cl || !cl->dev))
605 return -EINVAL;
606
607 dev = cl->dev;
608
Tomas Winkler9ca90502013-01-08 23:07:13 +0200609 if (!dev->me_clients_num)
610 return -ENOENT;
611
612 for (i = 0; i < dev->me_clients_num; i++) {
613 struct mei_me_client *me_cl = &dev->me_clients[i];
614 if (me_cl->client_id == cl->me_client_id) {
615 if (me_cl->props.single_recv_buf != 0) {
616 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
617 return -EINVAL;
618 dev->me_clients[i].mei_flow_ctrl_creds--;
619 } else {
620 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
621 return -EINVAL;
622 cl->mei_flow_ctrl_creds--;
623 }
624 return 0;
625 }
626 }
627 return -ENOENT;
628}
629
Tomas Winkler9ca90502013-01-08 23:07:13 +0200630/**
Masanari Iida393b1482013-04-05 01:05:05 +0900631 * mei_cl_read_start - the start read client message function.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200632 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200633 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200634 *
635 * returns 0 on success, <0 on failure.
636 */
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300637int mei_cl_read_start(struct mei_cl *cl, size_t length)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200638{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200639 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200640 struct mei_cl_cb *cb;
641 int rets;
642 int i;
643
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200644 if (WARN_ON(!cl || !cl->dev))
645 return -ENODEV;
646
647 dev = cl->dev;
648
Tomas Winklerb950ac12013-07-25 20:15:53 +0300649 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200650 return -ENODEV;
651
Tomas Winklerd91aaed2013-01-08 23:07:18 +0200652 if (cl->read_cb) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300653 cl_dbg(dev, cl, "read is pending.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200654 return -EBUSY;
655 }
656 i = mei_me_cl_by_id(dev, cl->me_client_id);
657 if (i < 0) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300658 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200659 return -ENODEV;
660 }
661
662 cb = mei_io_cb_init(cl, NULL);
663 if (!cb)
664 return -ENOMEM;
665
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300666 /* always allocate at least client max message */
667 length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
668 rets = mei_io_cb_alloc_resp_buf(cb, length);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200669 if (rets)
670 goto err;
671
672 cb->fop_type = MEI_FOP_READ;
673 cl->read_cb = cb;
Tomas Winkler330dd7d2013-02-06 14:06:43 +0200674 if (dev->hbuf_is_ready) {
675 dev->hbuf_is_ready = false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200676 if (mei_hbm_cl_flow_control_req(dev, cl)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300677 cl_err(dev, cl, "flow control send failed\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200678 rets = -ENODEV;
679 goto err;
680 }
681 list_add_tail(&cb->list, &dev->read_list.list);
682 } else {
683 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
684 }
685 return rets;
686err:
687 mei_io_cb_free(cb);
688 return rets;
689}
690
Tomas Winkler074b4c02013-02-06 14:06:44 +0200691/**
Tomas Winkler21767542013-06-23 09:36:59 +0300692 * mei_cl_irq_write_complete - write a message to device
693 * from the interrupt thread context
694 *
695 * @cl: client
696 * @cb: callback block.
697 * @slots: free slots.
698 * @cmpl_list: complete list.
699 *
700 * returns 0, OK; otherwise error.
701 */
702int mei_cl_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
703 s32 *slots, struct mei_cl_cb *cmpl_list)
704{
705 struct mei_device *dev = cl->dev;
706 struct mei_msg_hdr mei_hdr;
707 size_t len = cb->request_buffer.size - cb->buf_idx;
708 u32 msg_slots = mei_data2slots(len);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300709 int rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300710
711 mei_hdr.host_addr = cl->host_client_id;
712 mei_hdr.me_addr = cl->me_client_id;
713 mei_hdr.reserved = 0;
714
715 if (*slots >= msg_slots) {
716 mei_hdr.length = len;
717 mei_hdr.msg_complete = 1;
718 /* Split the message only if we can write the whole host buffer */
719 } else if (*slots == dev->hbuf_depth) {
720 msg_slots = *slots;
721 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
722 mei_hdr.length = len;
723 mei_hdr.msg_complete = 0;
724 } else {
725 /* wait for next time the host buffer is empty */
726 return 0;
727 }
728
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300729 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
Tomas Winkler21767542013-06-23 09:36:59 +0300730 cb->request_buffer.size, cb->buf_idx);
Tomas Winkler21767542013-06-23 09:36:59 +0300731
732 *slots -= msg_slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300733 rets = mei_write_message(dev, &mei_hdr,
734 cb->request_buffer.data + cb->buf_idx);
735 if (rets) {
736 cl->status = rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300737 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300738 return rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300739 }
740
741 cl->status = 0;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +0300742 cl->writing_state = MEI_WRITING;
Tomas Winkler21767542013-06-23 09:36:59 +0300743 cb->buf_idx += mei_hdr.length;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +0300744
Tomas Winkler21767542013-06-23 09:36:59 +0300745 if (mei_hdr.msg_complete) {
746 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300747 return -EIO;
Tomas Winkler21767542013-06-23 09:36:59 +0300748 list_move_tail(&cb->list, &dev->write_waiting_list.list);
749 }
750
751 return 0;
752}
753
754/**
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300755 * mei_cl_write - submit a write cb to mei device
756 assumes device_lock is locked
757 *
758 * @cl: host client
759 * @cl: write callback with filled data
760 *
761 * returns numbe of bytes sent on success, <0 on failure.
762 */
763int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
764{
765 struct mei_device *dev;
766 struct mei_msg_data *buf;
767 struct mei_msg_hdr mei_hdr;
768 int rets;
769
770
771 if (WARN_ON(!cl || !cl->dev))
772 return -ENODEV;
773
774 if (WARN_ON(!cb))
775 return -EINVAL;
776
777 dev = cl->dev;
778
779
780 buf = &cb->request_buffer;
781
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300782 cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300783
784
785 cb->fop_type = MEI_FOP_WRITE;
786
787 rets = mei_cl_flow_ctrl_creds(cl);
788 if (rets < 0)
789 goto err;
790
791 /* Host buffer is not ready, we queue the request */
792 if (rets == 0 || !dev->hbuf_is_ready) {
793 cb->buf_idx = 0;
794 /* unseting complete will enqueue the cb for write */
795 mei_hdr.msg_complete = 0;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300796 rets = buf->size;
797 goto out;
798 }
799
800 dev->hbuf_is_ready = false;
801
802 /* Check for a maximum length */
803 if (buf->size > mei_hbuf_max_len(dev)) {
804 mei_hdr.length = mei_hbuf_max_len(dev);
805 mei_hdr.msg_complete = 0;
806 } else {
807 mei_hdr.length = buf->size;
808 mei_hdr.msg_complete = 1;
809 }
810
811 mei_hdr.host_addr = cl->host_client_id;
812 mei_hdr.me_addr = cl->me_client_id;
813 mei_hdr.reserved = 0;
814
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300815
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300816 rets = mei_write_message(dev, &mei_hdr, buf->data);
817 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300818 goto err;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300819
820 cl->writing_state = MEI_WRITING;
821 cb->buf_idx = mei_hdr.length;
822
823 rets = buf->size;
824out:
825 if (mei_hdr.msg_complete) {
826 if (mei_cl_flow_ctrl_reduce(cl)) {
827 rets = -ENODEV;
828 goto err;
829 }
830 list_add_tail(&cb->list, &dev->write_waiting_list.list);
831 } else {
832 list_add_tail(&cb->list, &dev->write_list.list);
833 }
834
835
836 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
837
838 mutex_unlock(&dev->device_lock);
839 if (wait_event_interruptible(cl->tx_wait,
840 cl->writing_state == MEI_WRITE_COMPLETE)) {
841 if (signal_pending(current))
842 rets = -EINTR;
843 else
844 rets = -ERESTARTSYS;
845 }
846 mutex_lock(&dev->device_lock);
847 }
848err:
849 return rets;
850}
851
852
Tomas Winklerdb086fa2013-05-12 15:34:45 +0300853/**
854 * mei_cl_complete - processes completed operation for a client
855 *
856 * @cl: private data of the file object.
857 * @cb: callback block.
858 */
859void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
860{
861 if (cb->fop_type == MEI_FOP_WRITE) {
862 mei_io_cb_free(cb);
863 cb = NULL;
864 cl->writing_state = MEI_WRITE_COMPLETE;
865 if (waitqueue_active(&cl->tx_wait))
866 wake_up_interruptible(&cl->tx_wait);
867
868 } else if (cb->fop_type == MEI_FOP_READ &&
869 MEI_READING == cl->reading_state) {
870 cl->reading_state = MEI_READ_COMPLETE;
871 if (waitqueue_active(&cl->rx_wait))
872 wake_up_interruptible(&cl->rx_wait);
873 else
874 mei_cl_bus_rx_event(cl);
875
876 }
877}
878
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300879
880/**
Tomas Winkler074b4c02013-02-06 14:06:44 +0200881 * mei_cl_all_disconnect - disconnect forcefully all connected clients
882 *
883 * @dev - mei device
884 */
885
886void mei_cl_all_disconnect(struct mei_device *dev)
887{
888 struct mei_cl *cl, *next;
889
890 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
891 cl->state = MEI_FILE_DISCONNECTED;
892 cl->mei_flow_ctrl_creds = 0;
893 cl->read_cb = NULL;
894 cl->timer_count = 0;
895 }
896}
897
898
899/**
Tomas Winkler52908012013-07-24 16:22:57 +0300900 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
Tomas Winkler074b4c02013-02-06 14:06:44 +0200901 *
902 * @dev - mei device
903 */
Tomas Winkler52908012013-07-24 16:22:57 +0300904void mei_cl_all_wakeup(struct mei_device *dev)
Tomas Winkler074b4c02013-02-06 14:06:44 +0200905{
906 struct mei_cl *cl, *next;
907 list_for_each_entry_safe(cl, next, &dev->file_list, link) {
908 if (waitqueue_active(&cl->rx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300909 cl_dbg(dev, cl, "Waking up reading client!\n");
Tomas Winkler074b4c02013-02-06 14:06:44 +0200910 wake_up_interruptible(&cl->rx_wait);
911 }
Tomas Winkler52908012013-07-24 16:22:57 +0300912 if (waitqueue_active(&cl->tx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300913 cl_dbg(dev, cl, "Waking up writing client!\n");
Tomas Winkler52908012013-07-24 16:22:57 +0300914 wake_up_interruptible(&cl->tx_wait);
915 }
Tomas Winkler074b4c02013-02-06 14:06:44 +0200916 }
917}
918
919/**
920 * mei_cl_all_write_clear - clear all pending writes
921
922 * @dev - mei device
923 */
924void mei_cl_all_write_clear(struct mei_device *dev)
925{
926 struct mei_cl_cb *cb, *next;
927
928 list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
929 list_del(&cb->list);
930 mei_io_cb_free(cb);
931 }
932}
933
934