blob: bc9ba5359bc63982ffbac01ad0e91c9a6fa2ca19 [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
Tomas Winkler9ca90502013-01-08 23:07:13 +020017#include <linux/sched.h>
18#include <linux/wait.h>
19#include <linux/delay.h>
Tomas Winkler1f180352014-09-29 16:31:46 +030020#include <linux/slab.h>
Tomas Winkler04bb1392014-03-18 22:52:04 +020021#include <linux/pm_runtime.h>
Tomas Winkler9ca90502013-01-08 23:07:13 +020022
23#include <linux/mei.h>
24
25#include "mei_dev.h"
26#include "hbm.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020027#include "client.h"
28
29/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +030030 * mei_me_cl_by_uuid - locate me client by uuid
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020031 *
32 * @dev: mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +030033 * @uuid: me client uuid
Alexander Usyskina27a76d2014-02-17 15:13:22 +020034 *
35 * Locking: called under "dev->device_lock" lock
36 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030037 * Return: me client or NULL if not found
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020038 */
Tomas Winklerd3208322014-08-24 12:08:55 +030039struct mei_me_client *mei_me_cl_by_uuid(const struct mei_device *dev,
40 const uuid_le *uuid)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020041{
Tomas Winkler5ca2d382014-08-21 14:29:13 +030042 struct mei_me_client *me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020043
Tomas Winkler5ca2d382014-08-21 14:29:13 +030044 list_for_each_entry(me_cl, &dev->me_clients, list)
45 if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0)
46 return me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020047
Tomas Winklerd3208322014-08-24 12:08:55 +030048 return NULL;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020049}
50
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020051/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +030052 * mei_me_cl_by_id - locate me client by client id
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020053 *
54 * @dev: the device structure
55 * @client_id: me client id
56 *
57 * Locking: called under "dev->device_lock" lock
58 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030059 * Return: me client or NULL if not found
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020060 */
Tomas Winklerd3208322014-08-24 12:08:55 +030061struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020062{
Alexander Usyskina27a76d2014-02-17 15:13:22 +020063
Tomas Winkler5ca2d382014-08-21 14:29:13 +030064 struct mei_me_client *me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020065
Tomas Winkler5ca2d382014-08-21 14:29:13 +030066 list_for_each_entry(me_cl, &dev->me_clients, list)
67 if (me_cl->client_id == client_id)
68 return me_cl;
Tomas Winklerd3208322014-08-24 12:08:55 +030069 return NULL;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020070}
Tomas Winkler9ca90502013-01-08 23:07:13 +020071
Alexander Usyskina8605ea2014-09-29 16:31:49 +030072/**
73 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
74 *
75 * @dev: the device structure
76 * @uuid: me client uuid
77 * @client_id: me client id
78 *
79 * Locking: called under "dev->device_lock" lock
80 *
81 * Return: me client or NULL if not found
82 */
Tomas Winklerd880f322014-08-21 14:29:15 +030083struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
84 const uuid_le *uuid, u8 client_id)
85{
86 struct mei_me_client *me_cl;
87
88 list_for_each_entry(me_cl, &dev->me_clients, list)
89 if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0 &&
90 me_cl->client_id == client_id)
91 return me_cl;
92 return NULL;
93}
94
Tomas Winkler25ca6472014-08-21 14:29:14 +030095/**
96 * mei_me_cl_remove - remove me client matching uuid and client_id
97 *
98 * @dev: the device structure
99 * @uuid: me client uuid
100 * @client_id: me client address
101 */
102void mei_me_cl_remove(struct mei_device *dev, const uuid_le *uuid, u8 client_id)
103{
104 struct mei_me_client *me_cl, *next;
105
106 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list) {
107 if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0 &&
108 me_cl->client_id == client_id) {
109 list_del(&me_cl->list);
110 kfree(me_cl);
111 break;
112 }
113 }
114}
115
Tomas Winkler9ca90502013-01-08 23:07:13 +0200116
117/**
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200118 * mei_cl_cmp_id - tells if the clients are the same
119 *
120 * @cl1: host client 1
121 * @cl2: host client 2
122 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300123 * Return: true - if the clients has same host and me ids
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200124 * false - otherwise
125 */
126static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
127 const struct mei_cl *cl2)
128{
129 return cl1 && cl2 &&
130 (cl1->host_client_id == cl2->host_client_id) &&
131 (cl1->me_client_id == cl2->me_client_id);
132}
133
134/**
135 * mei_io_list_flush - removes cbs belonging to cl.
136 *
137 * @list: an instance of our list structure
138 * @cl: host client, can be NULL for flushing the whole list
139 * @free: whether to free the cbs
140 */
141static void __mei_io_list_flush(struct mei_cl_cb *list,
142 struct mei_cl *cl, bool free)
143{
144 struct mei_cl_cb *cb;
145 struct mei_cl_cb *next;
146
147 /* enable removing everything if no cl is specified */
148 list_for_each_entry_safe(cb, next, &list->list, list) {
149 if (!cl || (cb->cl && mei_cl_cmp_id(cl, cb->cl))) {
150 list_del(&cb->list);
151 if (free)
152 mei_io_cb_free(cb);
153 }
154 }
155}
156
157/**
Tomas Winkler9ca90502013-01-08 23:07:13 +0200158 * mei_io_list_flush - removes list entry belonging to cl.
159 *
160 * @list: An instance of our list structure
161 * @cl: host client
162 */
Alexander Usyskin54567962014-08-14 17:22:20 +0300163void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200164{
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200165 __mei_io_list_flush(list, cl, false);
166}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200167
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200168
169/**
170 * mei_io_list_free - removes cb belonging to cl and free them
171 *
172 * @list: An instance of our list structure
173 * @cl: host client
174 */
175static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
176{
177 __mei_io_list_flush(list, cl, true);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200178}
179
180/**
181 * mei_io_cb_free - free mei_cb_private related memory
182 *
183 * @cb: mei callback struct
184 */
185void mei_io_cb_free(struct mei_cl_cb *cb)
186{
187 if (cb == NULL)
188 return;
189
190 kfree(cb->request_buffer.data);
191 kfree(cb->response_buffer.data);
192 kfree(cb);
193}
194
195/**
196 * mei_io_cb_init - allocate and initialize io callback
197 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300198 * @cl: mei client
Masanari Iida393b1482013-04-05 01:05:05 +0900199 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200200 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300201 * Return: mei_cl_cb pointer or NULL;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200202 */
203struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
204{
205 struct mei_cl_cb *cb;
206
207 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
208 if (!cb)
209 return NULL;
210
211 mei_io_list_init(cb);
212
213 cb->file_object = fp;
214 cb->cl = cl;
215 cb->buf_idx = 0;
216 return cb;
217}
218
219/**
220 * mei_io_cb_alloc_req_buf - allocate request buffer
221 *
Masanari Iida393b1482013-04-05 01:05:05 +0900222 * @cb: io callback structure
223 * @length: size of the buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200224 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300225 * Return: 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200226 * -EINVAL if cb is NULL
227 * -ENOMEM if allocation failed
228 */
229int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
230{
231 if (!cb)
232 return -EINVAL;
233
234 if (length == 0)
235 return 0;
236
237 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
238 if (!cb->request_buffer.data)
239 return -ENOMEM;
240 cb->request_buffer.size = length;
241 return 0;
242}
243/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200244 * mei_io_cb_alloc_resp_buf - allocate response buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200245 *
Masanari Iida393b1482013-04-05 01:05:05 +0900246 * @cb: io callback structure
247 * @length: size of the buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200248 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300249 * Return: 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200250 * -EINVAL if cb is NULL
251 * -ENOMEM if allocation failed
252 */
253int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
254{
255 if (!cb)
256 return -EINVAL;
257
258 if (length == 0)
259 return 0;
260
261 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
262 if (!cb->response_buffer.data)
263 return -ENOMEM;
264 cb->response_buffer.size = length;
265 return 0;
266}
267
268
269
270/**
271 * mei_cl_flush_queues - flushes queue lists belonging to cl.
272 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200273 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +0300274 *
275 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200276 */
277int mei_cl_flush_queues(struct mei_cl *cl)
278{
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300279 struct mei_device *dev;
280
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200281 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200282 return -EINVAL;
283
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300284 dev = cl->dev;
285
286 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200287 mei_io_list_flush(&cl->dev->read_list, cl);
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200288 mei_io_list_free(&cl->dev->write_list, cl);
289 mei_io_list_free(&cl->dev->write_waiting_list, cl);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200290 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
291 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
292 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
293 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
294 return 0;
295}
296
Tomas Winkler9ca90502013-01-08 23:07:13 +0200297
298/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200299 * mei_cl_init - initializes cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200300 *
301 * @cl: host client to be initialized
302 * @dev: mei device
303 */
304void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
305{
306 memset(cl, 0, sizeof(struct mei_cl));
307 init_waitqueue_head(&cl->wait);
308 init_waitqueue_head(&cl->rx_wait);
309 init_waitqueue_head(&cl->tx_wait);
310 INIT_LIST_HEAD(&cl->link);
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200311 INIT_LIST_HEAD(&cl->device_link);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200312 cl->reading_state = MEI_IDLE;
313 cl->writing_state = MEI_IDLE;
314 cl->dev = dev;
315}
316
317/**
318 * mei_cl_allocate - allocates cl structure and sets it up.
319 *
320 * @dev: mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300321 * Return: The allocated file or NULL on failure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200322 */
323struct mei_cl *mei_cl_allocate(struct mei_device *dev)
324{
325 struct mei_cl *cl;
326
327 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
328 if (!cl)
329 return NULL;
330
331 mei_cl_init(cl, dev);
332
333 return cl;
334}
335
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200336/**
337 * mei_cl_find_read_cb - find this cl's callback in the read list
338 *
Masanari Iida393b1482013-04-05 01:05:05 +0900339 * @cl: host client
340 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300341 * Return: cb on success, NULL on error
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200342 */
343struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
344{
345 struct mei_device *dev = cl->dev;
Tomas Winkler31f88f52014-02-17 15:13:25 +0200346 struct mei_cl_cb *cb;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200347
Tomas Winkler31f88f52014-02-17 15:13:25 +0200348 list_for_each_entry(cb, &dev->read_list.list, list)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200349 if (mei_cl_cmp_id(cl, cb->cl))
350 return cb;
351 return NULL;
352}
353
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200354/** mei_cl_link: allocate host id in the host map
Tomas Winkler9ca90502013-01-08 23:07:13 +0200355 *
Tomas Winkler781d0d82013-01-08 23:07:22 +0200356 * @cl - host client
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200357 * @id - fixed host id or -1 for generic one
Masanari Iida393b1482013-04-05 01:05:05 +0900358 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300359 * Return: 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200360 * -EINVAL on incorrect values
361 * -ENONET if client not found
362 */
Tomas Winkler781d0d82013-01-08 23:07:22 +0200363int mei_cl_link(struct mei_cl *cl, int id)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200364{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200365 struct mei_device *dev;
Tomas Winkler22f96a02013-09-16 23:44:47 +0300366 long open_handle_count;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200367
Tomas Winkler781d0d82013-01-08 23:07:22 +0200368 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200369 return -EINVAL;
370
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200371 dev = cl->dev;
372
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200373 /* If Id is not assigned get one*/
Tomas Winkler781d0d82013-01-08 23:07:22 +0200374 if (id == MEI_HOST_CLIENT_ID_ANY)
375 id = find_first_zero_bit(dev->host_clients_map,
376 MEI_CLIENTS_MAX);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200377
Tomas Winkler781d0d82013-01-08 23:07:22 +0200378 if (id >= MEI_CLIENTS_MAX) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300379 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
Tomas Winklere036cc52013-09-16 23:44:46 +0300380 return -EMFILE;
381 }
382
Tomas Winkler22f96a02013-09-16 23:44:47 +0300383 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
384 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300385 dev_err(dev->dev, "open_handle_count exceeded %d",
Tomas Winklere036cc52013-09-16 23:44:46 +0300386 MEI_MAX_OPEN_HANDLE_COUNT);
387 return -EMFILE;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200388 }
389
Tomas Winkler781d0d82013-01-08 23:07:22 +0200390 dev->open_handle_count++;
391
392 cl->host_client_id = id;
393 list_add_tail(&cl->link, &dev->file_list);
394
395 set_bit(id, dev->host_clients_map);
396
397 cl->state = MEI_FILE_INITIALIZING;
398
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300399 cl_dbg(dev, cl, "link cl\n");
Tomas Winkler781d0d82013-01-08 23:07:22 +0200400 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200401}
Tomas Winkler781d0d82013-01-08 23:07:22 +0200402
Tomas Winkler9ca90502013-01-08 23:07:13 +0200403/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200404 * mei_cl_unlink - remove me_cl from the list
Tomas Winkler9ca90502013-01-08 23:07:13 +0200405 *
Masanari Iida393b1482013-04-05 01:05:05 +0900406 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +0300407 *
408 * Return: always 0
Tomas Winkler9ca90502013-01-08 23:07:13 +0200409 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200410int mei_cl_unlink(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200411{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200412 struct mei_device *dev;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200413
Tomas Winkler781d0d82013-01-08 23:07:22 +0200414 /* don't shout on error exit path */
415 if (!cl)
416 return 0;
417
Tomas Winkler8e9a4a92013-01-10 17:32:14 +0200418 /* wd and amthif might not be initialized */
419 if (!cl->dev)
420 return 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200421
422 dev = cl->dev;
423
Tomas Winklera14c44d2013-09-16 23:44:45 +0300424 cl_dbg(dev, cl, "unlink client");
425
Tomas Winkler22f96a02013-09-16 23:44:47 +0300426 if (dev->open_handle_count > 0)
427 dev->open_handle_count--;
428
429 /* never clear the 0 bit */
430 if (cl->host_client_id)
431 clear_bit(cl->host_client_id, dev->host_clients_map);
432
433 list_del_init(&cl->link);
434
435 cl->state = MEI_FILE_INITIALIZING;
436
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200437 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200438}
439
440
441void mei_host_client_init(struct work_struct *work)
442{
443 struct mei_device *dev = container_of(work,
444 struct mei_device, init_work);
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300445 struct mei_me_client *me_cl;
446 struct mei_client_properties *props;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200447
448 mutex_lock(&dev->device_lock);
449
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300450 list_for_each_entry(me_cl, &dev->me_clients, list) {
451 props = &me_cl->props;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200452
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300453 if (!uuid_le_cmp(props->protocol_name, mei_amthif_guid))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200454 mei_amthif_host_init(dev);
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300455 else if (!uuid_le_cmp(props->protocol_name, mei_wd_guid))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200456 mei_wd_host_init(dev);
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300457 else if (!uuid_le_cmp(props->protocol_name, mei_nfc_guid))
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200458 mei_nfc_host_init(dev);
459
Tomas Winkler9ca90502013-01-08 23:07:13 +0200460 }
461
462 dev->dev_state = MEI_DEV_ENABLED;
Tomas Winkler6adb8ef2014-01-12 00:36:10 +0200463 dev->reset_count = 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200464
465 mutex_unlock(&dev->device_lock);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200466
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300467 pm_runtime_mark_last_busy(dev->dev);
468 dev_dbg(dev->dev, "rpm: autosuspend\n");
469 pm_runtime_autosuspend(dev->dev);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200470}
471
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200472/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300473 * mei_hbuf_acquire - try to acquire host buffer
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200474 *
475 * @dev: the device structure
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300476 * Return: true if host buffer was acquired
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200477 */
478bool mei_hbuf_acquire(struct mei_device *dev)
479{
Tomas Winkler04bb1392014-03-18 22:52:04 +0200480 if (mei_pg_state(dev) == MEI_PG_ON ||
481 dev->pg_event == MEI_PG_EVENT_WAIT) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300482 dev_dbg(dev->dev, "device is in pg\n");
Tomas Winkler04bb1392014-03-18 22:52:04 +0200483 return false;
484 }
485
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200486 if (!dev->hbuf_is_ready) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300487 dev_dbg(dev->dev, "hbuf is not ready\n");
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200488 return false;
489 }
490
491 dev->hbuf_is_ready = false;
492
493 return true;
494}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200495
496/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200497 * mei_cl_disconnect - disconnect host client from the me one
Tomas Winkler9ca90502013-01-08 23:07:13 +0200498 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200499 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200500 *
501 * Locking: called under "dev->device_lock" lock
502 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300503 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200504 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200505int mei_cl_disconnect(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200506{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200507 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200508 struct mei_cl_cb *cb;
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300509 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200510
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200511 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200512 return -ENODEV;
513
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200514 dev = cl->dev;
515
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300516 cl_dbg(dev, cl, "disconnecting");
517
Tomas Winkler9ca90502013-01-08 23:07:13 +0200518 if (cl->state != MEI_FILE_DISCONNECTING)
519 return 0;
520
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300521 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200522 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300523 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200524 cl_err(dev, cl, "rpm: get failed %d\n", rets);
525 return rets;
526 }
527
Tomas Winkler9ca90502013-01-08 23:07:13 +0200528 cb = mei_io_cb_init(cl, NULL);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200529 if (!cb) {
530 rets = -ENOMEM;
531 goto free;
532 }
Tomas Winkler9ca90502013-01-08 23:07:13 +0200533
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300534 cb->fop_type = MEI_FOP_DISCONNECT;
535
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200536 if (mei_hbuf_acquire(dev)) {
Tomas Winkler9ca90502013-01-08 23:07:13 +0200537 if (mei_hbm_cl_disconnect_req(dev, cl)) {
538 rets = -ENODEV;
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300539 cl_err(dev, cl, "failed to disconnect.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200540 goto free;
541 }
Alexander Usyskin22b987a2014-07-17 10:53:35 +0300542 cl->timer_count = MEI_CONNECT_TIMEOUT;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200543 mdelay(10); /* Wait for hardware disconnection ready */
544 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
545 } else {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300546 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200547 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
548
549 }
550 mutex_unlock(&dev->device_lock);
551
Tomas Winkler12f45ed2014-08-21 14:29:18 +0300552 wait_event_timeout(cl->wait,
Tomas Winkler9ca90502013-01-08 23:07:13 +0200553 MEI_FILE_DISCONNECTED == cl->state,
554 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
555
556 mutex_lock(&dev->device_lock);
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300557
Tomas Winkler9ca90502013-01-08 23:07:13 +0200558 if (MEI_FILE_DISCONNECTED == cl->state) {
559 rets = 0;
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300560 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200561 } else {
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300562 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
563 rets = -ETIME;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200564 }
565
566 mei_io_list_flush(&dev->ctrl_rd_list, cl);
567 mei_io_list_flush(&dev->ctrl_wr_list, cl);
568free:
Tomas Winkler04bb1392014-03-18 22:52:04 +0200569 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300570 pm_runtime_mark_last_busy(dev->dev);
571 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200572
Tomas Winkler9ca90502013-01-08 23:07:13 +0200573 mei_io_cb_free(cb);
574 return rets;
575}
576
577
578/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200579 * mei_cl_is_other_connecting - checks if other
580 * client with the same me client id is connecting
Tomas Winkler9ca90502013-01-08 23:07:13 +0200581 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200582 * @cl: private data of the file object
583 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300584 * Return: true if other client is connected, false - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200585 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200586bool mei_cl_is_other_connecting(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200587{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200588 struct mei_device *dev;
Tomas Winkler31f88f52014-02-17 15:13:25 +0200589 struct mei_cl *ocl; /* the other client */
Tomas Winkler9ca90502013-01-08 23:07:13 +0200590
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200591 if (WARN_ON(!cl || !cl->dev))
592 return false;
593
594 dev = cl->dev;
595
Tomas Winkler31f88f52014-02-17 15:13:25 +0200596 list_for_each_entry(ocl, &dev->file_list, link) {
597 if (ocl->state == MEI_FILE_CONNECTING &&
598 ocl != cl &&
599 cl->me_client_id == ocl->me_client_id)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200600 return true;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200601
602 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200603
604 return false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200605}
606
607/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200608 * mei_cl_connect - connect host client to the me one
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200609 *
610 * @cl: host client
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300611 * @file: pointer to file structure
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200612 *
613 * Locking: called under "dev->device_lock" lock
614 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300615 * Return: 0 on success, <0 on failure.
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200616 */
617int mei_cl_connect(struct mei_cl *cl, struct file *file)
618{
619 struct mei_device *dev;
620 struct mei_cl_cb *cb;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200621 int rets;
622
623 if (WARN_ON(!cl || !cl->dev))
624 return -ENODEV;
625
626 dev = cl->dev;
627
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300628 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200629 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300630 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200631 cl_err(dev, cl, "rpm: get failed %d\n", rets);
632 return rets;
633 }
634
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200635 cb = mei_io_cb_init(cl, file);
636 if (!cb) {
637 rets = -ENOMEM;
638 goto out;
639 }
640
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200641 cb->fop_type = MEI_FOP_CONNECT;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200642
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200643 /* run hbuf acquire last so we don't have to undo */
644 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
Alexander Usyskine4d82702014-04-27 15:42:21 +0300645 cl->state = MEI_FILE_CONNECTING;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200646 if (mei_hbm_cl_connect_req(dev, cl)) {
647 rets = -ENODEV;
648 goto out;
649 }
650 cl->timer_count = MEI_CONNECT_TIMEOUT;
651 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
652 } else {
Alexander Usyskin73ab4232014-08-12 18:07:56 +0300653 cl->state = MEI_FILE_INITIALIZING;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200654 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
655 }
656
657 mutex_unlock(&dev->device_lock);
Tomas Winkler12f45ed2014-08-21 14:29:18 +0300658 wait_event_timeout(cl->wait,
Alexander Usyskin285e2992014-02-17 15:13:20 +0200659 (cl->state == MEI_FILE_CONNECTED ||
660 cl->state == MEI_FILE_DISCONNECTED),
661 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200662 mutex_lock(&dev->device_lock);
663
664 if (cl->state != MEI_FILE_CONNECTED) {
Alexander Usyskin3e37ebb2014-07-17 10:53:34 +0300665 cl->state = MEI_FILE_DISCONNECTED;
Alexander Usyskin285e2992014-02-17 15:13:20 +0200666 /* something went really wrong */
667 if (!cl->status)
668 cl->status = -EFAULT;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200669
670 mei_io_list_flush(&dev->ctrl_rd_list, cl);
671 mei_io_list_flush(&dev->ctrl_wr_list, cl);
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200672 }
673
674 rets = cl->status;
675
676out:
Tomas Winkler04bb1392014-03-18 22:52:04 +0200677 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300678 pm_runtime_mark_last_busy(dev->dev);
679 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200680
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200681 mei_io_cb_free(cb);
682 return rets;
683}
684
685/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200686 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200687 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200688 * @cl: private data of the file object
689 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300690 * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200691 * -ENOENT if mei_cl is not present
692 * -EINVAL if single_recv_buf == 0
693 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200694int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200695{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200696 struct mei_device *dev;
Alexander Usyskin12d00662014-02-17 15:13:23 +0200697 struct mei_me_client *me_cl;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200698
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200699 if (WARN_ON(!cl || !cl->dev))
700 return -EINVAL;
701
702 dev = cl->dev;
703
Tomas Winkler9ca90502013-01-08 23:07:13 +0200704 if (cl->mei_flow_ctrl_creds > 0)
705 return 1;
706
Tomas Winklerd3208322014-08-24 12:08:55 +0300707 me_cl = mei_me_cl_by_id(dev, cl->me_client_id);
708 if (!me_cl) {
Alexander Usyskin12d00662014-02-17 15:13:23 +0200709 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +0300710 return -ENOENT;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200711 }
Alexander Usyskin12d00662014-02-17 15:13:23 +0200712
Alexander Usyskin12d00662014-02-17 15:13:23 +0200713 if (me_cl->mei_flow_ctrl_creds) {
714 if (WARN_ON(me_cl->props.single_recv_buf == 0))
715 return -EINVAL;
716 return 1;
717 }
718 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200719}
720
721/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200722 * mei_cl_flow_ctrl_reduce - reduces flow_control.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200723 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200724 * @cl: private data of the file object
Masanari Iida393b1482013-04-05 01:05:05 +0900725 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300726 * Return:
Tomas Winkler9ca90502013-01-08 23:07:13 +0200727 * 0 on success
728 * -ENOENT when me client is not found
729 * -EINVAL when ctrl credits are <= 0
730 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200731int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200732{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200733 struct mei_device *dev;
Alexander Usyskin12d00662014-02-17 15:13:23 +0200734 struct mei_me_client *me_cl;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200735
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200736 if (WARN_ON(!cl || !cl->dev))
737 return -EINVAL;
738
739 dev = cl->dev;
740
Tomas Winklerd3208322014-08-24 12:08:55 +0300741 me_cl = mei_me_cl_by_id(dev, cl->me_client_id);
742 if (!me_cl) {
Alexander Usyskin12d00662014-02-17 15:13:23 +0200743 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +0300744 return -ENOENT;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200745 }
Alexander Usyskin12d00662014-02-17 15:13:23 +0200746
Tomas Winklerd3208322014-08-24 12:08:55 +0300747 if (me_cl->props.single_recv_buf) {
Alexander Usyskin12d00662014-02-17 15:13:23 +0200748 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
749 return -EINVAL;
750 me_cl->mei_flow_ctrl_creds--;
751 } else {
752 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
753 return -EINVAL;
754 cl->mei_flow_ctrl_creds--;
755 }
756 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200757}
758
Tomas Winkler9ca90502013-01-08 23:07:13 +0200759/**
Masanari Iida393b1482013-04-05 01:05:05 +0900760 * mei_cl_read_start - the start read client message function.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200761 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200762 * @cl: host client
Alexander Usyskince231392014-09-29 16:31:50 +0300763 * @length: number of bytes to read
Tomas Winkler9ca90502013-01-08 23:07:13 +0200764 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300765 * Return: 0 on success, <0 on failure.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200766 */
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300767int mei_cl_read_start(struct mei_cl *cl, size_t length)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200768{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200769 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200770 struct mei_cl_cb *cb;
Tomas Winklerd3208322014-08-24 12:08:55 +0300771 struct mei_me_client *me_cl;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200772 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200773
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200774 if (WARN_ON(!cl || !cl->dev))
775 return -ENODEV;
776
777 dev = cl->dev;
778
Tomas Winklerb950ac12013-07-25 20:15:53 +0300779 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200780 return -ENODEV;
781
Tomas Winklerd91aaed2013-01-08 23:07:18 +0200782 if (cl->read_cb) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300783 cl_dbg(dev, cl, "read is pending.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200784 return -EBUSY;
785 }
Tomas Winklerd880f322014-08-21 14:29:15 +0300786 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +0300787 if (!me_cl) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300788 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200789 return -ENOTTY;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200790 }
791
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300792 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200793 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300794 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200795 cl_err(dev, cl, "rpm: get failed %d\n", rets);
796 return rets;
797 }
798
Tomas Winkler9ca90502013-01-08 23:07:13 +0200799 cb = mei_io_cb_init(cl, NULL);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200800 if (!cb) {
801 rets = -ENOMEM;
802 goto out;
803 }
Tomas Winkler9ca90502013-01-08 23:07:13 +0200804
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300805 /* always allocate at least client max message */
Tomas Winklerd3208322014-08-24 12:08:55 +0300806 length = max_t(size_t, length, me_cl->props.max_msg_length);
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300807 rets = mei_io_cb_alloc_resp_buf(cb, length);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200808 if (rets)
Tomas Winkler04bb1392014-03-18 22:52:04 +0200809 goto out;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200810
811 cb->fop_type = MEI_FOP_READ;
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200812 if (mei_hbuf_acquire(dev)) {
Alexander Usyskin86113502014-03-31 17:59:24 +0300813 rets = mei_hbm_cl_flow_control_req(dev, cl);
814 if (rets < 0)
Tomas Winkler04bb1392014-03-18 22:52:04 +0200815 goto out;
Tomas Winkler04bb1392014-03-18 22:52:04 +0200816
Tomas Winkler9ca90502013-01-08 23:07:13 +0200817 list_add_tail(&cb->list, &dev->read_list.list);
818 } else {
819 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
820 }
Chao Biaccb8842014-02-12 21:27:25 +0200821
822 cl->read_cb = cb;
823
Tomas Winkler04bb1392014-03-18 22:52:04 +0200824out:
825 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300826 pm_runtime_mark_last_busy(dev->dev);
827 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200828
829 if (rets)
830 mei_io_cb_free(cb);
831
Tomas Winkler9ca90502013-01-08 23:07:13 +0200832 return rets;
833}
834
Tomas Winkler074b4c02013-02-06 14:06:44 +0200835/**
Tomas Winkler9d098192014-02-19 17:35:48 +0200836 * mei_cl_irq_write - write a message to device
Tomas Winkler21767542013-06-23 09:36:59 +0300837 * from the interrupt thread context
838 *
839 * @cl: client
840 * @cb: callback block.
Tomas Winkler21767542013-06-23 09:36:59 +0300841 * @cmpl_list: complete list.
842 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300843 * Return: 0, OK; otherwise error.
Tomas Winkler21767542013-06-23 09:36:59 +0300844 */
Tomas Winkler9d098192014-02-19 17:35:48 +0200845int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
846 struct mei_cl_cb *cmpl_list)
Tomas Winkler21767542013-06-23 09:36:59 +0300847{
Tomas Winkler136698e2013-09-16 23:44:44 +0300848 struct mei_device *dev;
849 struct mei_msg_data *buf;
Tomas Winkler21767542013-06-23 09:36:59 +0300850 struct mei_msg_hdr mei_hdr;
Tomas Winkler136698e2013-09-16 23:44:44 +0300851 size_t len;
852 u32 msg_slots;
Tomas Winkler9d098192014-02-19 17:35:48 +0200853 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300854 int rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300855
Tomas Winkler136698e2013-09-16 23:44:44 +0300856 if (WARN_ON(!cl || !cl->dev))
857 return -ENODEV;
858
859 dev = cl->dev;
860
861 buf = &cb->request_buffer;
862
863 rets = mei_cl_flow_ctrl_creds(cl);
864 if (rets < 0)
865 return rets;
866
867 if (rets == 0) {
Tomas Winkler04bb1392014-03-18 22:52:04 +0200868 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler136698e2013-09-16 23:44:44 +0300869 return 0;
870 }
871
Tomas Winkler9d098192014-02-19 17:35:48 +0200872 slots = mei_hbuf_empty_slots(dev);
Tomas Winkler136698e2013-09-16 23:44:44 +0300873 len = buf->size - cb->buf_idx;
874 msg_slots = mei_data2slots(len);
875
Tomas Winkler21767542013-06-23 09:36:59 +0300876 mei_hdr.host_addr = cl->host_client_id;
877 mei_hdr.me_addr = cl->me_client_id;
878 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +0200879 mei_hdr.internal = cb->internal;
Tomas Winkler21767542013-06-23 09:36:59 +0300880
Tomas Winkler9d098192014-02-19 17:35:48 +0200881 if (slots >= msg_slots) {
Tomas Winkler21767542013-06-23 09:36:59 +0300882 mei_hdr.length = len;
883 mei_hdr.msg_complete = 1;
884 /* Split the message only if we can write the whole host buffer */
Tomas Winkler9d098192014-02-19 17:35:48 +0200885 } else if (slots == dev->hbuf_depth) {
886 msg_slots = slots;
887 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winkler21767542013-06-23 09:36:59 +0300888 mei_hdr.length = len;
889 mei_hdr.msg_complete = 0;
890 } else {
891 /* wait for next time the host buffer is empty */
892 return 0;
893 }
894
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300895 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
Tomas Winkler21767542013-06-23 09:36:59 +0300896 cb->request_buffer.size, cb->buf_idx);
Tomas Winkler21767542013-06-23 09:36:59 +0300897
Tomas Winkler136698e2013-09-16 23:44:44 +0300898 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300899 if (rets) {
900 cl->status = rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300901 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300902 return rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300903 }
904
905 cl->status = 0;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +0300906 cl->writing_state = MEI_WRITING;
Tomas Winkler21767542013-06-23 09:36:59 +0300907 cb->buf_idx += mei_hdr.length;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +0300908
Tomas Winkler21767542013-06-23 09:36:59 +0300909 if (mei_hdr.msg_complete) {
910 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300911 return -EIO;
Tomas Winkler21767542013-06-23 09:36:59 +0300912 list_move_tail(&cb->list, &dev->write_waiting_list.list);
913 }
914
915 return 0;
916}
917
918/**
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300919 * mei_cl_write - submit a write cb to mei device
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300920 * assumes device_lock is locked
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300921 *
922 * @cl: host client
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300923 * @cb: write callback with filled data
Alexander Usyskince231392014-09-29 16:31:50 +0300924 * @blocking: block until completed
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300925 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300926 * Return: number of bytes sent on success, <0 on failure.
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300927 */
928int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
929{
930 struct mei_device *dev;
931 struct mei_msg_data *buf;
932 struct mei_msg_hdr mei_hdr;
933 int rets;
934
935
936 if (WARN_ON(!cl || !cl->dev))
937 return -ENODEV;
938
939 if (WARN_ON(!cb))
940 return -EINVAL;
941
942 dev = cl->dev;
943
944
945 buf = &cb->request_buffer;
946
Alexander Usyskin0a01e972014-09-29 16:31:47 +0300947 cl_dbg(dev, cl, "size=%d\n", buf->size);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300948
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300949 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200950 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300951 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200952 cl_err(dev, cl, "rpm: get failed %d\n", rets);
953 return rets;
954 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300955
956 cb->fop_type = MEI_FOP_WRITE;
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200957 cb->buf_idx = 0;
958 cl->writing_state = MEI_IDLE;
959
960 mei_hdr.host_addr = cl->host_client_id;
961 mei_hdr.me_addr = cl->me_client_id;
962 mei_hdr.reserved = 0;
963 mei_hdr.msg_complete = 0;
964 mei_hdr.internal = cb->internal;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300965
966 rets = mei_cl_flow_ctrl_creds(cl);
967 if (rets < 0)
968 goto err;
969
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200970 if (rets == 0) {
971 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300972 rets = buf->size;
973 goto out;
974 }
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200975 if (!mei_hbuf_acquire(dev)) {
976 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
977 rets = buf->size;
978 goto out;
979 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300980
981 /* Check for a maximum length */
982 if (buf->size > mei_hbuf_max_len(dev)) {
983 mei_hdr.length = mei_hbuf_max_len(dev);
984 mei_hdr.msg_complete = 0;
985 } else {
986 mei_hdr.length = buf->size;
987 mei_hdr.msg_complete = 1;
988 }
989
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300990 rets = mei_write_message(dev, &mei_hdr, buf->data);
991 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300992 goto err;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300993
994 cl->writing_state = MEI_WRITING;
995 cb->buf_idx = mei_hdr.length;
996
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300997out:
998 if (mei_hdr.msg_complete) {
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200999 rets = mei_cl_flow_ctrl_reduce(cl);
1000 if (rets < 0)
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001001 goto err;
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001002
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001003 list_add_tail(&cb->list, &dev->write_waiting_list.list);
1004 } else {
1005 list_add_tail(&cb->list, &dev->write_list.list);
1006 }
1007
1008
1009 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1010
1011 mutex_unlock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001012 rets = wait_event_interruptible(cl->tx_wait,
1013 cl->writing_state == MEI_WRITE_COMPLETE);
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001014 mutex_lock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001015 /* wait_event_interruptible returns -ERESTARTSYS */
1016 if (rets) {
1017 if (signal_pending(current))
1018 rets = -EINTR;
1019 goto err;
1020 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001021 }
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001022
1023 rets = buf->size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001024err:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001025 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001026 pm_runtime_mark_last_busy(dev->dev);
1027 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001028
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001029 return rets;
1030}
1031
1032
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001033/**
1034 * mei_cl_complete - processes completed operation for a client
1035 *
1036 * @cl: private data of the file object.
1037 * @cb: callback block.
1038 */
1039void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1040{
1041 if (cb->fop_type == MEI_FOP_WRITE) {
1042 mei_io_cb_free(cb);
1043 cb = NULL;
1044 cl->writing_state = MEI_WRITE_COMPLETE;
1045 if (waitqueue_active(&cl->tx_wait))
1046 wake_up_interruptible(&cl->tx_wait);
1047
1048 } else if (cb->fop_type == MEI_FOP_READ &&
1049 MEI_READING == cl->reading_state) {
1050 cl->reading_state = MEI_READ_COMPLETE;
1051 if (waitqueue_active(&cl->rx_wait))
1052 wake_up_interruptible(&cl->rx_wait);
1053 else
1054 mei_cl_bus_rx_event(cl);
1055
1056 }
1057}
1058
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001059
1060/**
Tomas Winkler074b4c02013-02-06 14:06:44 +02001061 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1062 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001063 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001064 */
1065
1066void mei_cl_all_disconnect(struct mei_device *dev)
1067{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001068 struct mei_cl *cl;
Tomas Winkler074b4c02013-02-06 14:06:44 +02001069
Tomas Winkler31f88f52014-02-17 15:13:25 +02001070 list_for_each_entry(cl, &dev->file_list, link) {
Tomas Winkler074b4c02013-02-06 14:06:44 +02001071 cl->state = MEI_FILE_DISCONNECTED;
1072 cl->mei_flow_ctrl_creds = 0;
Tomas Winkler074b4c02013-02-06 14:06:44 +02001073 cl->timer_count = 0;
1074 }
1075}
1076
1077
1078/**
Tomas Winkler52908012013-07-24 16:22:57 +03001079 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
Tomas Winkler074b4c02013-02-06 14:06:44 +02001080 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001081 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001082 */
Tomas Winkler52908012013-07-24 16:22:57 +03001083void mei_cl_all_wakeup(struct mei_device *dev)
Tomas Winkler074b4c02013-02-06 14:06:44 +02001084{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001085 struct mei_cl *cl;
Tomas Winkler92db1552014-09-29 16:31:37 +03001086
Tomas Winkler31f88f52014-02-17 15:13:25 +02001087 list_for_each_entry(cl, &dev->file_list, link) {
Tomas Winkler074b4c02013-02-06 14:06:44 +02001088 if (waitqueue_active(&cl->rx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001089 cl_dbg(dev, cl, "Waking up reading client!\n");
Tomas Winkler074b4c02013-02-06 14:06:44 +02001090 wake_up_interruptible(&cl->rx_wait);
1091 }
Tomas Winkler52908012013-07-24 16:22:57 +03001092 if (waitqueue_active(&cl->tx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001093 cl_dbg(dev, cl, "Waking up writing client!\n");
Tomas Winkler52908012013-07-24 16:22:57 +03001094 wake_up_interruptible(&cl->tx_wait);
1095 }
Tomas Winkler074b4c02013-02-06 14:06:44 +02001096 }
1097}
1098
1099/**
1100 * mei_cl_all_write_clear - clear all pending writes
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001101 *
1102 * @dev: mei device
Tomas Winkler074b4c02013-02-06 14:06:44 +02001103 */
1104void mei_cl_all_write_clear(struct mei_device *dev)
1105{
Tomas Winklercc99ecf2014-03-10 15:10:40 +02001106 mei_io_list_free(&dev->write_list, NULL);
1107 mei_io_list_free(&dev->write_waiting_list, NULL);
Tomas Winkler074b4c02013-02-06 14:06:44 +02001108}
1109
1110