blob: fe6d245ee8fe5b8d59edbefdb5582a212815f964 [file] [log] [blame]
Utkarsh Saxena96194882017-04-03 13:21:58 +05301/* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
Amir Levy9659e592016-10-27 18:08:27 +03002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/fs.h>
14#include <linux/sched.h>
15#include "ipa_i.h"
16
17struct ipa3_intf {
18 char name[IPA_RESOURCE_NAME_MAX];
19 struct list_head link;
20 u32 num_tx_props;
21 u32 num_rx_props;
22 u32 num_ext_props;
23 struct ipa_ioc_tx_intf_prop *tx;
24 struct ipa_ioc_rx_intf_prop *rx;
25 struct ipa_ioc_ext_intf_prop *ext;
26 enum ipa_client_type excp_pipe;
27};
28
29struct ipa3_push_msg {
30 struct ipa_msg_meta meta;
31 ipa_msg_free_fn callback;
32 void *buff;
33 struct list_head link;
34};
35
36struct ipa3_pull_msg {
37 struct ipa_msg_meta meta;
38 ipa_msg_pull_fn callback;
39 struct list_head link;
40};
41
42/**
43 * ipa3_register_intf() - register "logical" interface
44 * @name: [in] interface name
45 * @tx: [in] TX properties of the interface
46 * @rx: [in] RX properties of the interface
47 *
48 * Register an interface and its tx and rx properties, this allows
49 * configuration of rules from user-space
50 *
51 * Returns: 0 on success, negative on failure
52 *
53 * Note: Should not be called from atomic context
54 */
55int ipa3_register_intf(const char *name, const struct ipa_tx_intf *tx,
56 const struct ipa_rx_intf *rx)
57{
58 return ipa3_register_intf_ext(name, tx, rx, NULL);
59}
60
61/**
62 * ipa3_register_intf_ext() - register "logical" interface which has only
63 * extended properties
64 * @name: [in] interface name
65 * @tx: [in] TX properties of the interface
66 * @rx: [in] RX properties of the interface
67 * @ext: [in] EXT properties of the interface
68 *
69 * Register an interface and its tx, rx and ext properties, this allows
70 * configuration of rules from user-space
71 *
72 * Returns: 0 on success, negative on failure
73 *
74 * Note: Should not be called from atomic context
75 */
76int ipa3_register_intf_ext(const char *name, const struct ipa_tx_intf *tx,
77 const struct ipa_rx_intf *rx,
78 const struct ipa_ext_intf *ext)
79{
80 struct ipa3_intf *intf;
81 u32 len;
82
83 if (name == NULL || (tx == NULL && rx == NULL && ext == NULL)) {
84 IPAERR("invalid params name=%p tx=%p rx=%p ext=%p\n", name,
85 tx, rx, ext);
86 return -EINVAL;
87 }
88
89 if (tx && tx->num_props > IPA_NUM_PROPS_MAX) {
90 IPAERR("invalid tx num_props=%d max=%d\n", tx->num_props,
91 IPA_NUM_PROPS_MAX);
92 return -EINVAL;
93 }
94
95 if (rx && rx->num_props > IPA_NUM_PROPS_MAX) {
96 IPAERR("invalid rx num_props=%d max=%d\n", rx->num_props,
97 IPA_NUM_PROPS_MAX);
98 return -EINVAL;
99 }
100
101 if (ext && ext->num_props > IPA_NUM_PROPS_MAX) {
102 IPAERR("invalid ext num_props=%d max=%d\n", ext->num_props,
103 IPA_NUM_PROPS_MAX);
104 return -EINVAL;
105 }
106
107 len = sizeof(struct ipa3_intf);
108 intf = kzalloc(len, GFP_KERNEL);
109 if (intf == NULL) {
110 IPAERR("fail to alloc 0x%x bytes\n", len);
111 return -ENOMEM;
112 }
113
114 strlcpy(intf->name, name, IPA_RESOURCE_NAME_MAX);
115
116 if (tx) {
117 intf->num_tx_props = tx->num_props;
118 len = tx->num_props * sizeof(struct ipa_ioc_tx_intf_prop);
119 intf->tx = kzalloc(len, GFP_KERNEL);
120 if (intf->tx == NULL) {
121 IPAERR("fail to alloc 0x%x bytes\n", len);
122 kfree(intf);
123 return -ENOMEM;
124 }
125 memcpy(intf->tx, tx->prop, len);
126 }
127
128 if (rx) {
129 intf->num_rx_props = rx->num_props;
130 len = rx->num_props * sizeof(struct ipa_ioc_rx_intf_prop);
131 intf->rx = kzalloc(len, GFP_KERNEL);
132 if (intf->rx == NULL) {
133 IPAERR("fail to alloc 0x%x bytes\n", len);
134 kfree(intf->tx);
135 kfree(intf);
136 return -ENOMEM;
137 }
138 memcpy(intf->rx, rx->prop, len);
139 }
140
141 if (ext) {
142 intf->num_ext_props = ext->num_props;
143 len = ext->num_props * sizeof(struct ipa_ioc_ext_intf_prop);
144 intf->ext = kzalloc(len, GFP_KERNEL);
145 if (intf->ext == NULL) {
146 IPAERR("fail to alloc 0x%x bytes\n", len);
147 kfree(intf->rx);
148 kfree(intf->tx);
149 kfree(intf);
150 return -ENOMEM;
151 }
152 memcpy(intf->ext, ext->prop, len);
153 }
154
155 if (ext && ext->excp_pipe_valid)
156 intf->excp_pipe = ext->excp_pipe;
157 else
158 intf->excp_pipe = IPA_CLIENT_APPS_LAN_CONS;
159
160 mutex_lock(&ipa3_ctx->lock);
161 list_add_tail(&intf->link, &ipa3_ctx->intf_list);
162 mutex_unlock(&ipa3_ctx->lock);
163
164 return 0;
165}
166
167/**
168 * ipa3_deregister_intf() - de-register previously registered logical interface
169 * @name: [in] interface name
170 *
171 * De-register a previously registered interface
172 *
173 * Returns: 0 on success, negative on failure
174 *
175 * Note: Should not be called from atomic context
176 */
177int ipa3_deregister_intf(const char *name)
178{
179 struct ipa3_intf *entry;
180 struct ipa3_intf *next;
181 int result = -EINVAL;
182
183 if ((name == NULL) ||
184 (strnlen(name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX)) {
185 IPAERR("invalid param name=%s\n", name);
186 return result;
187 }
188
189 mutex_lock(&ipa3_ctx->lock);
190 list_for_each_entry_safe(entry, next, &ipa3_ctx->intf_list, link) {
191 if (!strcmp(entry->name, name)) {
192 list_del(&entry->link);
193 kfree(entry->ext);
194 kfree(entry->rx);
195 kfree(entry->tx);
196 kfree(entry);
197 result = 0;
198 break;
199 }
200 }
201 mutex_unlock(&ipa3_ctx->lock);
202
203 return result;
204}
205
206/**
207 * ipa3_query_intf() - query logical interface properties
208 * @lookup: [inout] interface name and number of properties
209 *
210 * Obtain the handle and number of tx and rx properties for the named
211 * interface, used as part of querying the tx and rx properties for
212 * configuration of various rules from user-space
213 *
214 * Returns: 0 on success, negative on failure
215 *
216 * Note: Should not be called from atomic context
217 */
218int ipa3_query_intf(struct ipa_ioc_query_intf *lookup)
219{
220 struct ipa3_intf *entry;
221 int result = -EINVAL;
222
223 if (lookup == NULL) {
224 IPAERR("invalid param lookup=%p\n", lookup);
225 return result;
226 }
227
228 if (strnlen(lookup->name, IPA_RESOURCE_NAME_MAX) ==
229 IPA_RESOURCE_NAME_MAX) {
230 IPAERR("Interface name too long. (%s)\n", lookup->name);
231 return result;
232 }
233
234 mutex_lock(&ipa3_ctx->lock);
235 list_for_each_entry(entry, &ipa3_ctx->intf_list, link) {
236 if (!strcmp(entry->name, lookup->name)) {
237 lookup->num_tx_props = entry->num_tx_props;
238 lookup->num_rx_props = entry->num_rx_props;
239 lookup->num_ext_props = entry->num_ext_props;
240 lookup->excp_pipe = entry->excp_pipe;
241 result = 0;
242 break;
243 }
244 }
245 mutex_unlock(&ipa3_ctx->lock);
246
247 return result;
248}
249
250/**
251 * ipa3_query_intf_tx_props() - qeury TX props of an interface
252 * @tx: [inout] interface tx attributes
253 *
254 * Obtain the tx properties for the specified interface
255 *
256 * Returns: 0 on success, negative on failure
257 *
258 * Note: Should not be called from atomic context
259 */
260int ipa3_query_intf_tx_props(struct ipa_ioc_query_intf_tx_props *tx)
261{
262 struct ipa3_intf *entry;
263 int result = -EINVAL;
264
265 if (tx == NULL) {
266 IPAERR("invalid param tx=%p\n", tx);
267 return result;
268 }
269
270 if (strnlen(tx->name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX) {
271 IPAERR("Interface name too long. (%s)\n", tx->name);
272 return result;
273 }
274
275 mutex_lock(&ipa3_ctx->lock);
276 list_for_each_entry(entry, &ipa3_ctx->intf_list, link) {
277 if (!strcmp(entry->name, tx->name)) {
278 memcpy(tx->tx, entry->tx, entry->num_tx_props *
279 sizeof(struct ipa_ioc_tx_intf_prop));
280 result = 0;
281 break;
282 }
283 }
284 mutex_unlock(&ipa3_ctx->lock);
285
286 return result;
287}
288
289/**
290 * ipa3_query_intf_rx_props() - qeury RX props of an interface
291 * @rx: [inout] interface rx attributes
292 *
293 * Obtain the rx properties for the specified interface
294 *
295 * Returns: 0 on success, negative on failure
296 *
297 * Note: Should not be called from atomic context
298 */
299int ipa3_query_intf_rx_props(struct ipa_ioc_query_intf_rx_props *rx)
300{
301 struct ipa3_intf *entry;
302 int result = -EINVAL;
303
304 if (rx == NULL) {
305 IPAERR("invalid param rx=%p\n", rx);
306 return result;
307 }
308
309 if (strnlen(rx->name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX) {
310 IPAERR("Interface name too long. (%s)\n", rx->name);
311 return result;
312 }
313
314 mutex_lock(&ipa3_ctx->lock);
315 list_for_each_entry(entry, &ipa3_ctx->intf_list, link) {
316 if (!strcmp(entry->name, rx->name)) {
317 memcpy(rx->rx, entry->rx, entry->num_rx_props *
318 sizeof(struct ipa_ioc_rx_intf_prop));
319 result = 0;
320 break;
321 }
322 }
323 mutex_unlock(&ipa3_ctx->lock);
324
325 return result;
326}
327
328/**
329 * ipa3_query_intf_ext_props() - qeury EXT props of an interface
330 * @ext: [inout] interface ext attributes
331 *
332 * Obtain the ext properties for the specified interface
333 *
334 * Returns: 0 on success, negative on failure
335 *
336 * Note: Should not be called from atomic context
337 */
338int ipa3_query_intf_ext_props(struct ipa_ioc_query_intf_ext_props *ext)
339{
340 struct ipa3_intf *entry;
341 int result = -EINVAL;
342
343 if (ext == NULL) {
344 IPAERR("invalid param ext=%p\n", ext);
345 return result;
346 }
347
348 mutex_lock(&ipa3_ctx->lock);
349 list_for_each_entry(entry, &ipa3_ctx->intf_list, link) {
350 if (!strcmp(entry->name, ext->name)) {
351 memcpy(ext->ext, entry->ext, entry->num_ext_props *
352 sizeof(struct ipa_ioc_ext_intf_prop));
353 result = 0;
354 break;
355 }
356 }
357 mutex_unlock(&ipa3_ctx->lock);
358 return result;
359}
360
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200361static void ipa3_send_msg_free(void *buff, u32 len, u32 type)
362{
363 kfree(buff);
364}
365
Amir Levy9659e592016-10-27 18:08:27 +0300366/**
367 * ipa3_send_msg() - Send "message" from kernel client to IPA driver
368 * @meta: [in] message meta-data
369 * @buff: [in] the payload for message
370 * @callback: [in] free callback
371 *
372 * Client supplies the message meta-data and payload which IPA driver buffers
373 * till read by user-space. After read from user space IPA driver invokes the
374 * callback supplied to free the message payload. Client must not touch/free
375 * the message payload after calling this API.
376 *
377 * Returns: 0 on success, negative on failure
378 *
379 * Note: Should not be called from atomic context
380 */
381int ipa3_send_msg(struct ipa_msg_meta *meta, void *buff,
382 ipa_msg_free_fn callback)
383{
384 struct ipa3_push_msg *msg;
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200385 void *data = NULL;
Amir Levy9659e592016-10-27 18:08:27 +0300386
387 if (meta == NULL || (buff == NULL && callback != NULL) ||
388 (buff != NULL && callback == NULL)) {
389 IPAERR("invalid param meta=%p buff=%p, callback=%p\n",
390 meta, buff, callback);
391 return -EINVAL;
392 }
393
394 if (meta->msg_type >= IPA_EVENT_MAX_NUM) {
395 IPAERR("unsupported message type %d\n", meta->msg_type);
396 return -EINVAL;
397 }
398
399 msg = kzalloc(sizeof(struct ipa3_push_msg), GFP_KERNEL);
400 if (msg == NULL) {
401 IPAERR("fail to alloc ipa_msg container\n");
402 return -ENOMEM;
403 }
404
405 msg->meta = *meta;
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200406 if (meta->msg_len > 0 && buff) {
407 data = kmalloc(meta->msg_len, GFP_KERNEL);
408 if (data == NULL) {
409 IPAERR("fail to alloc data container\n");
410 kfree(msg);
411 return -ENOMEM;
412 }
413 memcpy(data, buff, meta->msg_len);
414 msg->buff = data;
415 msg->callback = ipa3_send_msg_free;
416 }
Amir Levy9659e592016-10-27 18:08:27 +0300417
418 mutex_lock(&ipa3_ctx->msg_lock);
419 list_add_tail(&msg->link, &ipa3_ctx->msg_list);
420 mutex_unlock(&ipa3_ctx->msg_lock);
421 IPA_STATS_INC_CNT(ipa3_ctx->stats.msg_w[meta->msg_type]);
422
423 wake_up(&ipa3_ctx->msg_waitq);
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200424 if (buff)
425 callback(buff, meta->msg_len, meta->msg_type);
Amir Levy9659e592016-10-27 18:08:27 +0300426
427 return 0;
428}
429
430/**
431 * ipa3_register_pull_msg() - register pull message type
432 * @meta: [in] message meta-data
433 * @callback: [in] pull callback
434 *
435 * Register message callback by kernel client with IPA driver for IPA driver to
436 * pull message on-demand.
437 *
438 * Returns: 0 on success, negative on failure
439 *
440 * Note: Should not be called from atomic context
441 */
442int ipa3_register_pull_msg(struct ipa_msg_meta *meta, ipa_msg_pull_fn callback)
443{
444 struct ipa3_pull_msg *msg;
445
446 if (meta == NULL || callback == NULL) {
447 IPAERR("invalid param meta=%p callback=%p\n", meta, callback);
448 return -EINVAL;
449 }
450
451 msg = kzalloc(sizeof(struct ipa3_pull_msg), GFP_KERNEL);
452 if (msg == NULL) {
453 IPAERR("fail to alloc ipa_msg container\n");
454 return -ENOMEM;
455 }
456
457 msg->meta = *meta;
458 msg->callback = callback;
459
460 mutex_lock(&ipa3_ctx->msg_lock);
461 list_add_tail(&msg->link, &ipa3_ctx->pull_msg_list);
462 mutex_unlock(&ipa3_ctx->msg_lock);
463
464 return 0;
465}
466
467/**
468 * ipa3_deregister_pull_msg() - De-register pull message type
469 * @meta: [in] message meta-data
470 *
471 * De-register "message" by kernel client from IPA driver
472 *
473 * Returns: 0 on success, negative on failure
474 *
475 * Note: Should not be called from atomic context
476 */
477int ipa3_deregister_pull_msg(struct ipa_msg_meta *meta)
478{
479 struct ipa3_pull_msg *entry;
480 struct ipa3_pull_msg *next;
481 int result = -EINVAL;
482
483 if (meta == NULL) {
484 IPAERR("invalid param name=%p\n", meta);
485 return result;
486 }
487
488 mutex_lock(&ipa3_ctx->msg_lock);
489 list_for_each_entry_safe(entry, next, &ipa3_ctx->pull_msg_list, link) {
490 if (entry->meta.msg_len == meta->msg_len &&
491 entry->meta.msg_type == meta->msg_type) {
492 list_del(&entry->link);
493 kfree(entry);
494 result = 0;
495 break;
496 }
497 }
498 mutex_unlock(&ipa3_ctx->msg_lock);
499 return result;
500}
501
502/**
503 * ipa3_read() - read message from IPA device
504 * @filp: [in] file pointer
505 * @buf: [out] buffer to read into
506 * @count: [in] size of above buffer
507 * @f_pos: [inout] file position
508 *
509 * Uer-space should continually read from /dev/ipa, read wll block when there
510 * are no messages to read. Upon return, user-space should read the ipa_msg_meta
511 * from the start of the buffer to know what type of message was read and its
512 * length in the remainder of the buffer. Buffer supplied must be big enough to
513 * hold the message meta-data and the largest defined message type
514 *
515 * Returns: how many bytes copied to buffer
516 *
517 * Note: Should not be called from atomic context
518 */
519ssize_t ipa3_read(struct file *filp, char __user *buf, size_t count,
520 loff_t *f_pos)
521{
522 char __user *start;
523 struct ipa3_push_msg *msg = NULL;
524 int ret;
Mohammed Javid60d8d0a2017-06-29 11:35:19 +0530525 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Amir Levy9659e592016-10-27 18:08:27 +0300526 int locked;
527
528 start = buf;
529
Mohammed Javid60d8d0a2017-06-29 11:35:19 +0530530 add_wait_queue(&ipa3_ctx->msg_waitq, &wait);
Amir Levy9659e592016-10-27 18:08:27 +0300531 while (1) {
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200532 mutex_lock(&ipa3_ctx->msg_lock);
533 locked = 1;
Amir Levy9659e592016-10-27 18:08:27 +0300534
Amir Levy9659e592016-10-27 18:08:27 +0300535 if (!list_empty(&ipa3_ctx->msg_list)) {
536 msg = list_first_entry(&ipa3_ctx->msg_list,
537 struct ipa3_push_msg, link);
538 list_del(&msg->link);
539 }
540
541 IPADBG_LOW("msg=%p\n", msg);
542
543 if (msg) {
544 locked = 0;
545 mutex_unlock(&ipa3_ctx->msg_lock);
546 if (copy_to_user(buf, &msg->meta,
547 sizeof(struct ipa_msg_meta))) {
548 ret = -EFAULT;
Utkarsh Saxena96194882017-04-03 13:21:58 +0530549 kfree(msg);
550 msg = NULL;
Amir Levy9659e592016-10-27 18:08:27 +0300551 break;
552 }
553 buf += sizeof(struct ipa_msg_meta);
554 count -= sizeof(struct ipa_msg_meta);
555 if (msg->buff) {
556 if (copy_to_user(buf, msg->buff,
557 msg->meta.msg_len)) {
558 ret = -EFAULT;
Utkarsh Saxena96194882017-04-03 13:21:58 +0530559 kfree(msg);
560 msg = NULL;
Amir Levy9659e592016-10-27 18:08:27 +0300561 break;
562 }
563 buf += msg->meta.msg_len;
564 count -= msg->meta.msg_len;
565 msg->callback(msg->buff, msg->meta.msg_len,
566 msg->meta.msg_type);
567 }
568 IPA_STATS_INC_CNT(
569 ipa3_ctx->stats.msg_r[msg->meta.msg_type]);
570 kfree(msg);
571 }
572
573 ret = -EAGAIN;
574 if (filp->f_flags & O_NONBLOCK)
575 break;
576
577 ret = -EINTR;
578 if (signal_pending(current))
579 break;
580
581 if (start != buf)
582 break;
583
584 locked = 0;
585 mutex_unlock(&ipa3_ctx->msg_lock);
Mohammed Javid60d8d0a2017-06-29 11:35:19 +0530586 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Amir Levy9659e592016-10-27 18:08:27 +0300587 }
588
Mohammed Javid60d8d0a2017-06-29 11:35:19 +0530589 remove_wait_queue(&ipa3_ctx->msg_waitq, &wait);
Amir Levy9659e592016-10-27 18:08:27 +0300590 if (start != buf && ret != -EFAULT)
591 ret = buf - start;
592
593 if (locked)
594 mutex_unlock(&ipa3_ctx->msg_lock);
595
596 return ret;
597}
598
599/**
600 * ipa3_pull_msg() - pull the specified message from client
601 * @meta: [in] message meta-data
602 * @buf: [out] buffer to read into
603 * @count: [in] size of above buffer
604 *
605 * Populate the supplied buffer with the pull message which is fetched
606 * from client, the message must have previously been registered with
607 * the IPA driver
608 *
609 * Returns: how many bytes copied to buffer
610 *
611 * Note: Should not be called from atomic context
612 */
613int ipa3_pull_msg(struct ipa_msg_meta *meta, char *buff, size_t count)
614{
615 struct ipa3_pull_msg *entry;
616 int result = -EINVAL;
617
618 if (meta == NULL || buff == NULL || !count) {
619 IPAERR("invalid param name=%p buff=%p count=%zu\n",
620 meta, buff, count);
621 return result;
622 }
623
624 mutex_lock(&ipa3_ctx->msg_lock);
625 list_for_each_entry(entry, &ipa3_ctx->pull_msg_list, link) {
626 if (entry->meta.msg_len == meta->msg_len &&
627 entry->meta.msg_type == meta->msg_type) {
628 result = entry->callback(buff, count, meta->msg_type);
629 break;
630 }
631 }
632 mutex_unlock(&ipa3_ctx->msg_lock);
633 return result;
634}