blob: bfc3ad24db9c0c92b134129a8c81d5450e7e2e13 [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>
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/**
30 * mei_me_cl_by_uuid - locate index of me client
31 *
32 * @dev: mei device
Alexander Usyskina27a76d2014-02-17 15:13:22 +020033 *
34 * Locking: called under "dev->device_lock" lock
35 *
Tomas Winklerd3208322014-08-24 12:08:55 +030036 * returns me client or NULL if not found
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020037 */
Tomas Winklerd3208322014-08-24 12:08:55 +030038struct mei_me_client *mei_me_cl_by_uuid(const struct mei_device *dev,
39 const uuid_le *uuid)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020040{
Tomas Winkler5ca2d382014-08-21 14:29:13 +030041 struct mei_me_client *me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020042
Tomas Winkler5ca2d382014-08-21 14:29:13 +030043 list_for_each_entry(me_cl, &dev->me_clients, list)
44 if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0)
45 return me_cl;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020046
Tomas Winklerd3208322014-08-24 12:08:55 +030047 return NULL;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020048}
49
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020050/**
51 * mei_me_cl_by_id return index to me_clients for client_id
52 *
53 * @dev: the device structure
54 * @client_id: me client id
55 *
56 * Locking: called under "dev->device_lock" lock
57 *
Tomas Winklerd3208322014-08-24 12:08:55 +030058 * returns me client or NULL if not found
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020059 */
60
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
Tomas Winklerd880f322014-08-21 14:29:15 +030072struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
73 const uuid_le *uuid, u8 client_id)
74{
75 struct mei_me_client *me_cl;
76
77 list_for_each_entry(me_cl, &dev->me_clients, list)
78 if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0 &&
79 me_cl->client_id == client_id)
80 return me_cl;
81 return NULL;
82}
83
Tomas Winkler25ca6472014-08-21 14:29:14 +030084/**
85 * mei_me_cl_remove - remove me client matching uuid and client_id
86 *
87 * @dev: the device structure
88 * @uuid: me client uuid
89 * @client_id: me client address
90 */
91void mei_me_cl_remove(struct mei_device *dev, const uuid_le *uuid, u8 client_id)
92{
93 struct mei_me_client *me_cl, *next;
94
95 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list) {
96 if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0 &&
97 me_cl->client_id == client_id) {
98 list_del(&me_cl->list);
99 kfree(me_cl);
100 break;
101 }
102 }
103}
104
Tomas Winkler9ca90502013-01-08 23:07:13 +0200105
106/**
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200107 * mei_cl_cmp_id - tells if the clients are the same
108 *
109 * @cl1: host client 1
110 * @cl2: host client 2
111 *
112 * returns true - if the clients has same host and me ids
113 * false - otherwise
114 */
115static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
116 const struct mei_cl *cl2)
117{
118 return cl1 && cl2 &&
119 (cl1->host_client_id == cl2->host_client_id) &&
120 (cl1->me_client_id == cl2->me_client_id);
121}
122
123/**
124 * mei_io_list_flush - removes cbs belonging to cl.
125 *
126 * @list: an instance of our list structure
127 * @cl: host client, can be NULL for flushing the whole list
128 * @free: whether to free the cbs
129 */
130static void __mei_io_list_flush(struct mei_cl_cb *list,
131 struct mei_cl *cl, bool free)
132{
133 struct mei_cl_cb *cb;
134 struct mei_cl_cb *next;
135
136 /* enable removing everything if no cl is specified */
137 list_for_each_entry_safe(cb, next, &list->list, list) {
138 if (!cl || (cb->cl && mei_cl_cmp_id(cl, cb->cl))) {
139 list_del(&cb->list);
140 if (free)
141 mei_io_cb_free(cb);
142 }
143 }
144}
145
146/**
Tomas Winkler9ca90502013-01-08 23:07:13 +0200147 * mei_io_list_flush - removes list entry belonging to cl.
148 *
149 * @list: An instance of our list structure
150 * @cl: host client
151 */
Alexander Usyskin54567962014-08-14 17:22:20 +0300152void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200153{
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200154 __mei_io_list_flush(list, cl, false);
155}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200156
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200157
158/**
159 * mei_io_list_free - removes cb belonging to cl and free them
160 *
161 * @list: An instance of our list structure
162 * @cl: host client
163 */
164static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
165{
166 __mei_io_list_flush(list, cl, true);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200167}
168
169/**
170 * mei_io_cb_free - free mei_cb_private related memory
171 *
172 * @cb: mei callback struct
173 */
174void mei_io_cb_free(struct mei_cl_cb *cb)
175{
176 if (cb == NULL)
177 return;
178
179 kfree(cb->request_buffer.data);
180 kfree(cb->response_buffer.data);
181 kfree(cb);
182}
183
184/**
185 * mei_io_cb_init - allocate and initialize io callback
186 *
187 * @cl - mei client
Masanari Iida393b1482013-04-05 01:05:05 +0900188 * @fp: pointer to file structure
Tomas Winkler9ca90502013-01-08 23:07:13 +0200189 *
190 * returns mei_cl_cb pointer or NULL;
191 */
192struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
193{
194 struct mei_cl_cb *cb;
195
196 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
197 if (!cb)
198 return NULL;
199
200 mei_io_list_init(cb);
201
202 cb->file_object = fp;
203 cb->cl = cl;
204 cb->buf_idx = 0;
205 return cb;
206}
207
208/**
209 * mei_io_cb_alloc_req_buf - allocate request buffer
210 *
Masanari Iida393b1482013-04-05 01:05:05 +0900211 * @cb: io callback structure
212 * @length: size of the buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200213 *
214 * returns 0 on success
215 * -EINVAL if cb is NULL
216 * -ENOMEM if allocation failed
217 */
218int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
219{
220 if (!cb)
221 return -EINVAL;
222
223 if (length == 0)
224 return 0;
225
226 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
227 if (!cb->request_buffer.data)
228 return -ENOMEM;
229 cb->request_buffer.size = length;
230 return 0;
231}
232/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200233 * mei_io_cb_alloc_resp_buf - allocate response buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200234 *
Masanari Iida393b1482013-04-05 01:05:05 +0900235 * @cb: io callback structure
236 * @length: size of the buffer
Tomas Winkler9ca90502013-01-08 23:07:13 +0200237 *
238 * returns 0 on success
239 * -EINVAL if cb is NULL
240 * -ENOMEM if allocation failed
241 */
242int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
243{
244 if (!cb)
245 return -EINVAL;
246
247 if (length == 0)
248 return 0;
249
250 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
251 if (!cb->response_buffer.data)
252 return -ENOMEM;
253 cb->response_buffer.size = length;
254 return 0;
255}
256
257
258
259/**
260 * mei_cl_flush_queues - flushes queue lists belonging to cl.
261 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200262 * @cl: host client
263 */
264int mei_cl_flush_queues(struct mei_cl *cl)
265{
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300266 struct mei_device *dev;
267
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200268 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200269 return -EINVAL;
270
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300271 dev = cl->dev;
272
273 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200274 mei_io_list_flush(&cl->dev->read_list, cl);
Tomas Winklercc99ecf2014-03-10 15:10:40 +0200275 mei_io_list_free(&cl->dev->write_list, cl);
276 mei_io_list_free(&cl->dev->write_waiting_list, cl);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200277 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
278 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
279 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
280 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
281 return 0;
282}
283
Tomas Winkler9ca90502013-01-08 23:07:13 +0200284
285/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200286 * mei_cl_init - initializes cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200287 *
288 * @cl: host client to be initialized
289 * @dev: mei device
290 */
291void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
292{
293 memset(cl, 0, sizeof(struct mei_cl));
294 init_waitqueue_head(&cl->wait);
295 init_waitqueue_head(&cl->rx_wait);
296 init_waitqueue_head(&cl->tx_wait);
297 INIT_LIST_HEAD(&cl->link);
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200298 INIT_LIST_HEAD(&cl->device_link);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200299 cl->reading_state = MEI_IDLE;
300 cl->writing_state = MEI_IDLE;
301 cl->dev = dev;
302}
303
304/**
305 * mei_cl_allocate - allocates cl structure and sets it up.
306 *
307 * @dev: mei device
308 * returns The allocated file or NULL on failure
309 */
310struct mei_cl *mei_cl_allocate(struct mei_device *dev)
311{
312 struct mei_cl *cl;
313
314 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
315 if (!cl)
316 return NULL;
317
318 mei_cl_init(cl, dev);
319
320 return cl;
321}
322
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200323/**
324 * mei_cl_find_read_cb - find this cl's callback in the read list
325 *
Masanari Iida393b1482013-04-05 01:05:05 +0900326 * @cl: host client
327 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200328 * returns cb on success, NULL on error
329 */
330struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
331{
332 struct mei_device *dev = cl->dev;
Tomas Winkler31f88f52014-02-17 15:13:25 +0200333 struct mei_cl_cb *cb;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200334
Tomas Winkler31f88f52014-02-17 15:13:25 +0200335 list_for_each_entry(cb, &dev->read_list.list, list)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200336 if (mei_cl_cmp_id(cl, cb->cl))
337 return cb;
338 return NULL;
339}
340
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200341/** mei_cl_link: allocate host id in the host map
Tomas Winkler9ca90502013-01-08 23:07:13 +0200342 *
Tomas Winkler781d0d82013-01-08 23:07:22 +0200343 * @cl - host client
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200344 * @id - fixed host id or -1 for generic one
Masanari Iida393b1482013-04-05 01:05:05 +0900345 *
Tomas Winkler781d0d82013-01-08 23:07:22 +0200346 * returns 0 on success
Tomas Winkler9ca90502013-01-08 23:07:13 +0200347 * -EINVAL on incorrect values
348 * -ENONET if client not found
349 */
Tomas Winkler781d0d82013-01-08 23:07:22 +0200350int mei_cl_link(struct mei_cl *cl, int id)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200351{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200352 struct mei_device *dev;
Tomas Winkler22f96a02013-09-16 23:44:47 +0300353 long open_handle_count;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200354
Tomas Winkler781d0d82013-01-08 23:07:22 +0200355 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200356 return -EINVAL;
357
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200358 dev = cl->dev;
359
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200360 /* If Id is not assigned get one*/
Tomas Winkler781d0d82013-01-08 23:07:22 +0200361 if (id == MEI_HOST_CLIENT_ID_ANY)
362 id = find_first_zero_bit(dev->host_clients_map,
363 MEI_CLIENTS_MAX);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200364
Tomas Winkler781d0d82013-01-08 23:07:22 +0200365 if (id >= MEI_CLIENTS_MAX) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300366 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
Tomas Winklere036cc52013-09-16 23:44:46 +0300367 return -EMFILE;
368 }
369
Tomas Winkler22f96a02013-09-16 23:44:47 +0300370 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
371 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300372 dev_err(dev->dev, "open_handle_count exceeded %d",
Tomas Winklere036cc52013-09-16 23:44:46 +0300373 MEI_MAX_OPEN_HANDLE_COUNT);
374 return -EMFILE;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200375 }
376
Tomas Winkler781d0d82013-01-08 23:07:22 +0200377 dev->open_handle_count++;
378
379 cl->host_client_id = id;
380 list_add_tail(&cl->link, &dev->file_list);
381
382 set_bit(id, dev->host_clients_map);
383
384 cl->state = MEI_FILE_INITIALIZING;
385
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300386 cl_dbg(dev, cl, "link cl\n");
Tomas Winkler781d0d82013-01-08 23:07:22 +0200387 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200388}
Tomas Winkler781d0d82013-01-08 23:07:22 +0200389
Tomas Winkler9ca90502013-01-08 23:07:13 +0200390/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200391 * mei_cl_unlink - remove me_cl from the list
Tomas Winkler9ca90502013-01-08 23:07:13 +0200392 *
Masanari Iida393b1482013-04-05 01:05:05 +0900393 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200394 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200395int mei_cl_unlink(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200396{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200397 struct mei_device *dev;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200398
Tomas Winkler781d0d82013-01-08 23:07:22 +0200399 /* don't shout on error exit path */
400 if (!cl)
401 return 0;
402
Tomas Winkler8e9a4a92013-01-10 17:32:14 +0200403 /* wd and amthif might not be initialized */
404 if (!cl->dev)
405 return 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200406
407 dev = cl->dev;
408
Tomas Winklera14c44d2013-09-16 23:44:45 +0300409 cl_dbg(dev, cl, "unlink client");
410
Tomas Winkler22f96a02013-09-16 23:44:47 +0300411 if (dev->open_handle_count > 0)
412 dev->open_handle_count--;
413
414 /* never clear the 0 bit */
415 if (cl->host_client_id)
416 clear_bit(cl->host_client_id, dev->host_clients_map);
417
418 list_del_init(&cl->link);
419
420 cl->state = MEI_FILE_INITIALIZING;
421
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200422 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200423}
424
425
426void mei_host_client_init(struct work_struct *work)
427{
428 struct mei_device *dev = container_of(work,
429 struct mei_device, init_work);
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300430 struct mei_me_client *me_cl;
431 struct mei_client_properties *props;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200432
433 mutex_lock(&dev->device_lock);
434
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300435 list_for_each_entry(me_cl, &dev->me_clients, list) {
436 props = &me_cl->props;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200437
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300438 if (!uuid_le_cmp(props->protocol_name, mei_amthif_guid))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200439 mei_amthif_host_init(dev);
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300440 else if (!uuid_le_cmp(props->protocol_name, mei_wd_guid))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200441 mei_wd_host_init(dev);
Tomas Winkler5ca2d382014-08-21 14:29:13 +0300442 else if (!uuid_le_cmp(props->protocol_name, mei_nfc_guid))
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200443 mei_nfc_host_init(dev);
444
Tomas Winkler9ca90502013-01-08 23:07:13 +0200445 }
446
447 dev->dev_state = MEI_DEV_ENABLED;
Tomas Winkler6adb8ef2014-01-12 00:36:10 +0200448 dev->reset_count = 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200449
450 mutex_unlock(&dev->device_lock);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200451
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300452 pm_runtime_mark_last_busy(dev->dev);
453 dev_dbg(dev->dev, "rpm: autosuspend\n");
454 pm_runtime_autosuspend(dev->dev);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200455}
456
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200457/**
458 * mei_hbuf_acquire: try to acquire host buffer
459 *
460 * @dev: the device structure
461 * returns true if host buffer was acquired
462 */
463bool mei_hbuf_acquire(struct mei_device *dev)
464{
Tomas Winkler04bb1392014-03-18 22:52:04 +0200465 if (mei_pg_state(dev) == MEI_PG_ON ||
466 dev->pg_event == MEI_PG_EVENT_WAIT) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300467 dev_dbg(dev->dev, "device is in pg\n");
Tomas Winkler04bb1392014-03-18 22:52:04 +0200468 return false;
469 }
470
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200471 if (!dev->hbuf_is_ready) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300472 dev_dbg(dev->dev, "hbuf is not ready\n");
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200473 return false;
474 }
475
476 dev->hbuf_is_ready = false;
477
478 return true;
479}
Tomas Winkler9ca90502013-01-08 23:07:13 +0200480
481/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200482 * mei_cl_disconnect - disconnect host client from the me one
Tomas Winkler9ca90502013-01-08 23:07:13 +0200483 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200484 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200485 *
486 * Locking: called under "dev->device_lock" lock
487 *
488 * returns 0 on success, <0 on failure.
489 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200490int mei_cl_disconnect(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200491{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200492 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200493 struct mei_cl_cb *cb;
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300494 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200495
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200496 if (WARN_ON(!cl || !cl->dev))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200497 return -ENODEV;
498
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200499 dev = cl->dev;
500
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300501 cl_dbg(dev, cl, "disconnecting");
502
Tomas Winkler9ca90502013-01-08 23:07:13 +0200503 if (cl->state != MEI_FILE_DISCONNECTING)
504 return 0;
505
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300506 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200507 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300508 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200509 cl_err(dev, cl, "rpm: get failed %d\n", rets);
510 return rets;
511 }
512
Tomas Winkler9ca90502013-01-08 23:07:13 +0200513 cb = mei_io_cb_init(cl, NULL);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200514 if (!cb) {
515 rets = -ENOMEM;
516 goto free;
517 }
Tomas Winkler9ca90502013-01-08 23:07:13 +0200518
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300519 cb->fop_type = MEI_FOP_DISCONNECT;
520
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200521 if (mei_hbuf_acquire(dev)) {
Tomas Winkler9ca90502013-01-08 23:07:13 +0200522 if (mei_hbm_cl_disconnect_req(dev, cl)) {
523 rets = -ENODEV;
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300524 cl_err(dev, cl, "failed to disconnect.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200525 goto free;
526 }
Alexander Usyskin22b987a2014-07-17 10:53:35 +0300527 cl->timer_count = MEI_CONNECT_TIMEOUT;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200528 mdelay(10); /* Wait for hardware disconnection ready */
529 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
530 } else {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300531 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200532 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
533
534 }
535 mutex_unlock(&dev->device_lock);
536
Tomas Winkler12f45ed2014-08-21 14:29:18 +0300537 wait_event_timeout(cl->wait,
Tomas Winkler9ca90502013-01-08 23:07:13 +0200538 MEI_FILE_DISCONNECTED == cl->state,
539 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
540
541 mutex_lock(&dev->device_lock);
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300542
Tomas Winkler9ca90502013-01-08 23:07:13 +0200543 if (MEI_FILE_DISCONNECTED == cl->state) {
544 rets = 0;
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300545 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200546 } else {
Alexander Usyskinfe2f17eb32014-07-17 10:53:38 +0300547 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
548 rets = -ETIME;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200549 }
550
551 mei_io_list_flush(&dev->ctrl_rd_list, cl);
552 mei_io_list_flush(&dev->ctrl_wr_list, cl);
553free:
Tomas Winkler04bb1392014-03-18 22:52:04 +0200554 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300555 pm_runtime_mark_last_busy(dev->dev);
556 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200557
Tomas Winkler9ca90502013-01-08 23:07:13 +0200558 mei_io_cb_free(cb);
559 return rets;
560}
561
562
563/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200564 * mei_cl_is_other_connecting - checks if other
565 * client with the same me client id is connecting
Tomas Winkler9ca90502013-01-08 23:07:13 +0200566 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200567 * @cl: private data of the file object
568 *
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200569 * returns true if other client is connected, false - otherwise.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200570 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200571bool mei_cl_is_other_connecting(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200572{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200573 struct mei_device *dev;
Tomas Winkler31f88f52014-02-17 15:13:25 +0200574 struct mei_cl *ocl; /* the other client */
Tomas Winkler9ca90502013-01-08 23:07:13 +0200575
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200576 if (WARN_ON(!cl || !cl->dev))
577 return false;
578
579 dev = cl->dev;
580
Tomas Winkler31f88f52014-02-17 15:13:25 +0200581 list_for_each_entry(ocl, &dev->file_list, link) {
582 if (ocl->state == MEI_FILE_CONNECTING &&
583 ocl != cl &&
584 cl->me_client_id == ocl->me_client_id)
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200585 return true;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200586
587 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200588
589 return false;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200590}
591
592/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200593 * mei_cl_connect - connect host client to the me one
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200594 *
595 * @cl: host client
596 *
597 * Locking: called under "dev->device_lock" lock
598 *
599 * returns 0 on success, <0 on failure.
600 */
601int mei_cl_connect(struct mei_cl *cl, struct file *file)
602{
603 struct mei_device *dev;
604 struct mei_cl_cb *cb;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200605 int rets;
606
607 if (WARN_ON(!cl || !cl->dev))
608 return -ENODEV;
609
610 dev = cl->dev;
611
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300612 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200613 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300614 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200615 cl_err(dev, cl, "rpm: get failed %d\n", rets);
616 return rets;
617 }
618
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200619 cb = mei_io_cb_init(cl, file);
620 if (!cb) {
621 rets = -ENOMEM;
622 goto out;
623 }
624
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200625 cb->fop_type = MEI_FOP_CONNECT;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200626
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200627 /* run hbuf acquire last so we don't have to undo */
628 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
Alexander Usyskine4d82702014-04-27 15:42:21 +0300629 cl->state = MEI_FILE_CONNECTING;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200630 if (mei_hbm_cl_connect_req(dev, cl)) {
631 rets = -ENODEV;
632 goto out;
633 }
634 cl->timer_count = MEI_CONNECT_TIMEOUT;
635 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
636 } else {
Alexander Usyskin73ab4232014-08-12 18:07:56 +0300637 cl->state = MEI_FILE_INITIALIZING;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200638 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
639 }
640
641 mutex_unlock(&dev->device_lock);
Tomas Winkler12f45ed2014-08-21 14:29:18 +0300642 wait_event_timeout(cl->wait,
Alexander Usyskin285e2992014-02-17 15:13:20 +0200643 (cl->state == MEI_FILE_CONNECTED ||
644 cl->state == MEI_FILE_DISCONNECTED),
645 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200646 mutex_lock(&dev->device_lock);
647
648 if (cl->state != MEI_FILE_CONNECTED) {
Alexander Usyskin3e37ebb2014-07-17 10:53:34 +0300649 cl->state = MEI_FILE_DISCONNECTED;
Alexander Usyskin285e2992014-02-17 15:13:20 +0200650 /* something went really wrong */
651 if (!cl->status)
652 cl->status = -EFAULT;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200653
654 mei_io_list_flush(&dev->ctrl_rd_list, cl);
655 mei_io_list_flush(&dev->ctrl_wr_list, cl);
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200656 }
657
658 rets = cl->status;
659
660out:
Tomas Winkler04bb1392014-03-18 22:52:04 +0200661 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300662 pm_runtime_mark_last_busy(dev->dev);
663 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200664
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200665 mei_io_cb_free(cb);
666 return rets;
667}
668
669/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200670 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200671 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200672 * @cl: private data of the file object
673 *
674 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
675 * -ENOENT if mei_cl is not present
676 * -EINVAL if single_recv_buf == 0
677 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200678int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200679{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200680 struct mei_device *dev;
Alexander Usyskin12d00662014-02-17 15:13:23 +0200681 struct mei_me_client *me_cl;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200682
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200683 if (WARN_ON(!cl || !cl->dev))
684 return -EINVAL;
685
686 dev = cl->dev;
687
Tomas Winkler9ca90502013-01-08 23:07:13 +0200688 if (cl->mei_flow_ctrl_creds > 0)
689 return 1;
690
Tomas Winklerd3208322014-08-24 12:08:55 +0300691 me_cl = mei_me_cl_by_id(dev, cl->me_client_id);
692 if (!me_cl) {
Alexander Usyskin12d00662014-02-17 15:13:23 +0200693 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +0300694 return -ENOENT;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200695 }
Alexander Usyskin12d00662014-02-17 15:13:23 +0200696
Alexander Usyskin12d00662014-02-17 15:13:23 +0200697 if (me_cl->mei_flow_ctrl_creds) {
698 if (WARN_ON(me_cl->props.single_recv_buf == 0))
699 return -EINVAL;
700 return 1;
701 }
702 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200703}
704
705/**
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200706 * mei_cl_flow_ctrl_reduce - reduces flow_control.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200707 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200708 * @cl: private data of the file object
Masanari Iida393b1482013-04-05 01:05:05 +0900709 *
Tomas Winkler9ca90502013-01-08 23:07:13 +0200710 * @returns
711 * 0 on success
712 * -ENOENT when me client is not found
713 * -EINVAL when ctrl credits are <= 0
714 */
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200715int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200716{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200717 struct mei_device *dev;
Alexander Usyskin12d00662014-02-17 15:13:23 +0200718 struct mei_me_client *me_cl;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200719
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200720 if (WARN_ON(!cl || !cl->dev))
721 return -EINVAL;
722
723 dev = cl->dev;
724
Tomas Winklerd3208322014-08-24 12:08:55 +0300725 me_cl = mei_me_cl_by_id(dev, cl->me_client_id);
726 if (!me_cl) {
Alexander Usyskin12d00662014-02-17 15:13:23 +0200727 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +0300728 return -ENOENT;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200729 }
Alexander Usyskin12d00662014-02-17 15:13:23 +0200730
Tomas Winklerd3208322014-08-24 12:08:55 +0300731 if (me_cl->props.single_recv_buf) {
Alexander Usyskin12d00662014-02-17 15:13:23 +0200732 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
733 return -EINVAL;
734 me_cl->mei_flow_ctrl_creds--;
735 } else {
736 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
737 return -EINVAL;
738 cl->mei_flow_ctrl_creds--;
739 }
740 return 0;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200741}
742
Tomas Winkler9ca90502013-01-08 23:07:13 +0200743/**
Masanari Iida393b1482013-04-05 01:05:05 +0900744 * mei_cl_read_start - the start read client message function.
Tomas Winkler9ca90502013-01-08 23:07:13 +0200745 *
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200746 * @cl: host client
Tomas Winkler9ca90502013-01-08 23:07:13 +0200747 *
748 * returns 0 on success, <0 on failure.
749 */
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300750int mei_cl_read_start(struct mei_cl *cl, size_t length)
Tomas Winkler9ca90502013-01-08 23:07:13 +0200751{
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200752 struct mei_device *dev;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200753 struct mei_cl_cb *cb;
Tomas Winklerd3208322014-08-24 12:08:55 +0300754 struct mei_me_client *me_cl;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200755 int rets;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200756
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200757 if (WARN_ON(!cl || !cl->dev))
758 return -ENODEV;
759
760 dev = cl->dev;
761
Tomas Winklerb950ac12013-07-25 20:15:53 +0300762 if (!mei_cl_is_connected(cl))
Tomas Winkler9ca90502013-01-08 23:07:13 +0200763 return -ENODEV;
764
Tomas Winklerd91aaed2013-01-08 23:07:18 +0200765 if (cl->read_cb) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300766 cl_dbg(dev, cl, "read is pending.\n");
Tomas Winkler9ca90502013-01-08 23:07:13 +0200767 return -EBUSY;
768 }
Tomas Winklerd880f322014-08-21 14:29:15 +0300769 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +0300770 if (!me_cl) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300771 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200772 return -ENOTTY;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200773 }
774
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300775 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200776 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300777 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200778 cl_err(dev, cl, "rpm: get failed %d\n", rets);
779 return rets;
780 }
781
Tomas Winkler9ca90502013-01-08 23:07:13 +0200782 cb = mei_io_cb_init(cl, NULL);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200783 if (!cb) {
784 rets = -ENOMEM;
785 goto out;
786 }
Tomas Winkler9ca90502013-01-08 23:07:13 +0200787
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300788 /* always allocate at least client max message */
Tomas Winklerd3208322014-08-24 12:08:55 +0300789 length = max_t(size_t, length, me_cl->props.max_msg_length);
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300790 rets = mei_io_cb_alloc_resp_buf(cb, length);
Tomas Winkler9ca90502013-01-08 23:07:13 +0200791 if (rets)
Tomas Winkler04bb1392014-03-18 22:52:04 +0200792 goto out;
Tomas Winkler9ca90502013-01-08 23:07:13 +0200793
794 cb->fop_type = MEI_FOP_READ;
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200795 if (mei_hbuf_acquire(dev)) {
Alexander Usyskin86113502014-03-31 17:59:24 +0300796 rets = mei_hbm_cl_flow_control_req(dev, cl);
797 if (rets < 0)
Tomas Winkler04bb1392014-03-18 22:52:04 +0200798 goto out;
Tomas Winkler04bb1392014-03-18 22:52:04 +0200799
Tomas Winkler9ca90502013-01-08 23:07:13 +0200800 list_add_tail(&cb->list, &dev->read_list.list);
801 } else {
802 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
803 }
Chao Biaccb8842014-02-12 21:27:25 +0200804
805 cl->read_cb = cb;
806
Tomas Winkler04bb1392014-03-18 22:52:04 +0200807out:
808 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300809 pm_runtime_mark_last_busy(dev->dev);
810 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200811
812 if (rets)
813 mei_io_cb_free(cb);
814
Tomas Winkler9ca90502013-01-08 23:07:13 +0200815 return rets;
816}
817
Tomas Winkler074b4c02013-02-06 14:06:44 +0200818/**
Tomas Winkler9d098192014-02-19 17:35:48 +0200819 * mei_cl_irq_write - write a message to device
Tomas Winkler21767542013-06-23 09:36:59 +0300820 * from the interrupt thread context
821 *
822 * @cl: client
823 * @cb: callback block.
Tomas Winkler21767542013-06-23 09:36:59 +0300824 * @cmpl_list: complete list.
825 *
826 * returns 0, OK; otherwise error.
827 */
Tomas Winkler9d098192014-02-19 17:35:48 +0200828int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
829 struct mei_cl_cb *cmpl_list)
Tomas Winkler21767542013-06-23 09:36:59 +0300830{
Tomas Winkler136698e2013-09-16 23:44:44 +0300831 struct mei_device *dev;
832 struct mei_msg_data *buf;
Tomas Winkler21767542013-06-23 09:36:59 +0300833 struct mei_msg_hdr mei_hdr;
Tomas Winkler136698e2013-09-16 23:44:44 +0300834 size_t len;
835 u32 msg_slots;
Tomas Winkler9d098192014-02-19 17:35:48 +0200836 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300837 int rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300838
Tomas Winkler136698e2013-09-16 23:44:44 +0300839 if (WARN_ON(!cl || !cl->dev))
840 return -ENODEV;
841
842 dev = cl->dev;
843
844 buf = &cb->request_buffer;
845
846 rets = mei_cl_flow_ctrl_creds(cl);
847 if (rets < 0)
848 return rets;
849
850 if (rets == 0) {
Tomas Winkler04bb1392014-03-18 22:52:04 +0200851 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler136698e2013-09-16 23:44:44 +0300852 return 0;
853 }
854
Tomas Winkler9d098192014-02-19 17:35:48 +0200855 slots = mei_hbuf_empty_slots(dev);
Tomas Winkler136698e2013-09-16 23:44:44 +0300856 len = buf->size - cb->buf_idx;
857 msg_slots = mei_data2slots(len);
858
Tomas Winkler21767542013-06-23 09:36:59 +0300859 mei_hdr.host_addr = cl->host_client_id;
860 mei_hdr.me_addr = cl->me_client_id;
861 mei_hdr.reserved = 0;
Tomas Winkler479327f2013-12-17 15:56:56 +0200862 mei_hdr.internal = cb->internal;
Tomas Winkler21767542013-06-23 09:36:59 +0300863
Tomas Winkler9d098192014-02-19 17:35:48 +0200864 if (slots >= msg_slots) {
Tomas Winkler21767542013-06-23 09:36:59 +0300865 mei_hdr.length = len;
866 mei_hdr.msg_complete = 1;
867 /* Split the message only if we can write the whole host buffer */
Tomas Winkler9d098192014-02-19 17:35:48 +0200868 } else if (slots == dev->hbuf_depth) {
869 msg_slots = slots;
870 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winkler21767542013-06-23 09:36:59 +0300871 mei_hdr.length = len;
872 mei_hdr.msg_complete = 0;
873 } else {
874 /* wait for next time the host buffer is empty */
875 return 0;
876 }
877
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300878 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
Tomas Winkler21767542013-06-23 09:36:59 +0300879 cb->request_buffer.size, cb->buf_idx);
Tomas Winkler21767542013-06-23 09:36:59 +0300880
Tomas Winkler136698e2013-09-16 23:44:44 +0300881 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300882 if (rets) {
883 cl->status = rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300884 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300885 return rets;
Tomas Winkler21767542013-06-23 09:36:59 +0300886 }
887
888 cl->status = 0;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +0300889 cl->writing_state = MEI_WRITING;
Tomas Winkler21767542013-06-23 09:36:59 +0300890 cb->buf_idx += mei_hdr.length;
Tomas Winkler4dfaa9f2013-06-23 09:37:00 +0300891
Tomas Winkler21767542013-06-23 09:36:59 +0300892 if (mei_hdr.msg_complete) {
893 if (mei_cl_flow_ctrl_reduce(cl))
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300894 return -EIO;
Tomas Winkler21767542013-06-23 09:36:59 +0300895 list_move_tail(&cb->list, &dev->write_waiting_list.list);
896 }
897
898 return 0;
899}
900
901/**
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300902 * mei_cl_write - submit a write cb to mei device
903 assumes device_lock is locked
904 *
905 * @cl: host client
906 * @cl: write callback with filled data
907 *
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200908 * returns number of bytes sent on success, <0 on failure.
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300909 */
910int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
911{
912 struct mei_device *dev;
913 struct mei_msg_data *buf;
914 struct mei_msg_hdr mei_hdr;
915 int rets;
916
917
918 if (WARN_ON(!cl || !cl->dev))
919 return -ENODEV;
920
921 if (WARN_ON(!cb))
922 return -EINVAL;
923
924 dev = cl->dev;
925
926
927 buf = &cb->request_buffer;
928
Alexander Usyskinc0abffb2013-09-15 18:11:07 +0300929 cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300930
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300931 rets = pm_runtime_get(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200932 if (rets < 0 && rets != -EINPROGRESS) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300933 pm_runtime_put_noidle(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +0200934 cl_err(dev, cl, "rpm: get failed %d\n", rets);
935 return rets;
936 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300937
938 cb->fop_type = MEI_FOP_WRITE;
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200939 cb->buf_idx = 0;
940 cl->writing_state = MEI_IDLE;
941
942 mei_hdr.host_addr = cl->host_client_id;
943 mei_hdr.me_addr = cl->me_client_id;
944 mei_hdr.reserved = 0;
945 mei_hdr.msg_complete = 0;
946 mei_hdr.internal = cb->internal;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300947
948 rets = mei_cl_flow_ctrl_creds(cl);
949 if (rets < 0)
950 goto err;
951
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200952 if (rets == 0) {
953 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300954 rets = buf->size;
955 goto out;
956 }
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200957 if (!mei_hbuf_acquire(dev)) {
958 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
959 rets = buf->size;
960 goto out;
961 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300962
963 /* Check for a maximum length */
964 if (buf->size > mei_hbuf_max_len(dev)) {
965 mei_hdr.length = mei_hbuf_max_len(dev);
966 mei_hdr.msg_complete = 0;
967 } else {
968 mei_hdr.length = buf->size;
969 mei_hdr.msg_complete = 1;
970 }
971
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300972 rets = mei_write_message(dev, &mei_hdr, buf->data);
973 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300974 goto err;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300975
976 cl->writing_state = MEI_WRITING;
977 cb->buf_idx = mei_hdr.length;
978
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300979out:
980 if (mei_hdr.msg_complete) {
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200981 rets = mei_cl_flow_ctrl_reduce(cl);
982 if (rets < 0)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300983 goto err;
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200984
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300985 list_add_tail(&cb->list, &dev->write_waiting_list.list);
986 } else {
987 list_add_tail(&cb->list, &dev->write_list.list);
988 }
989
990
991 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
992
993 mutex_unlock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200994 rets = wait_event_interruptible(cl->tx_wait,
995 cl->writing_state == MEI_WRITE_COMPLETE);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300996 mutex_lock(&dev->device_lock);
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200997 /* wait_event_interruptible returns -ERESTARTSYS */
998 if (rets) {
999 if (signal_pending(current))
1000 rets = -EINTR;
1001 goto err;
1002 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001003 }
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +02001004
1005 rets = buf->size;
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001006err:
Tomas Winkler04bb1392014-03-18 22:52:04 +02001007 cl_dbg(dev, cl, "rpm: autosuspend\n");
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001008 pm_runtime_mark_last_busy(dev->dev);
1009 pm_runtime_put_autosuspend(dev->dev);
Tomas Winkler04bb1392014-03-18 22:52:04 +02001010
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001011 return rets;
1012}
1013
1014
Tomas Winklerdb086fa2013-05-12 15:34:45 +03001015/**
1016 * mei_cl_complete - processes completed operation for a client
1017 *
1018 * @cl: private data of the file object.
1019 * @cb: callback block.
1020 */
1021void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1022{
1023 if (cb->fop_type == MEI_FOP_WRITE) {
1024 mei_io_cb_free(cb);
1025 cb = NULL;
1026 cl->writing_state = MEI_WRITE_COMPLETE;
1027 if (waitqueue_active(&cl->tx_wait))
1028 wake_up_interruptible(&cl->tx_wait);
1029
1030 } else if (cb->fop_type == MEI_FOP_READ &&
1031 MEI_READING == cl->reading_state) {
1032 cl->reading_state = MEI_READ_COMPLETE;
1033 if (waitqueue_active(&cl->rx_wait))
1034 wake_up_interruptible(&cl->rx_wait);
1035 else
1036 mei_cl_bus_rx_event(cl);
1037
1038 }
1039}
1040
Tomas Winkler4234a6d2013-04-08 21:56:37 +03001041
1042/**
Tomas Winkler074b4c02013-02-06 14:06:44 +02001043 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1044 *
1045 * @dev - mei device
1046 */
1047
1048void mei_cl_all_disconnect(struct mei_device *dev)
1049{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001050 struct mei_cl *cl;
Tomas Winkler074b4c02013-02-06 14:06:44 +02001051
Tomas Winkler31f88f52014-02-17 15:13:25 +02001052 list_for_each_entry(cl, &dev->file_list, link) {
Tomas Winkler074b4c02013-02-06 14:06:44 +02001053 cl->state = MEI_FILE_DISCONNECTED;
1054 cl->mei_flow_ctrl_creds = 0;
Tomas Winkler074b4c02013-02-06 14:06:44 +02001055 cl->timer_count = 0;
1056 }
1057}
1058
1059
1060/**
Tomas Winkler52908012013-07-24 16:22:57 +03001061 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
Tomas Winkler074b4c02013-02-06 14:06:44 +02001062 *
1063 * @dev - mei device
1064 */
Tomas Winkler52908012013-07-24 16:22:57 +03001065void mei_cl_all_wakeup(struct mei_device *dev)
Tomas Winkler074b4c02013-02-06 14:06:44 +02001066{
Tomas Winkler31f88f52014-02-17 15:13:25 +02001067 struct mei_cl *cl;
Tomas Winkler92db1552014-09-29 16:31:37 +03001068
Tomas Winkler31f88f52014-02-17 15:13:25 +02001069 list_for_each_entry(cl, &dev->file_list, link) {
Tomas Winkler074b4c02013-02-06 14:06:44 +02001070 if (waitqueue_active(&cl->rx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001071 cl_dbg(dev, cl, "Waking up reading client!\n");
Tomas Winkler074b4c02013-02-06 14:06:44 +02001072 wake_up_interruptible(&cl->rx_wait);
1073 }
Tomas Winkler52908012013-07-24 16:22:57 +03001074 if (waitqueue_active(&cl->tx_wait)) {
Alexander Usyskinc0abffb2013-09-15 18:11:07 +03001075 cl_dbg(dev, cl, "Waking up writing client!\n");
Tomas Winkler52908012013-07-24 16:22:57 +03001076 wake_up_interruptible(&cl->tx_wait);
1077 }
Tomas Winkler074b4c02013-02-06 14:06:44 +02001078 }
1079}
1080
1081/**
1082 * mei_cl_all_write_clear - clear all pending writes
1083
1084 * @dev - mei device
1085 */
1086void mei_cl_all_write_clear(struct mei_device *dev)
1087{
Tomas Winklercc99ecf2014-03-10 15:10:40 +02001088 mei_io_list_free(&dev->write_list, NULL);
1089 mei_io_list_free(&dev->write_waiting_list, NULL);
Tomas Winkler074b4c02013-02-06 14:06:44 +02001090}
1091
1092