blob: 32c5004dda954237d1ebbac2104024b55cccc7df [file] [log] [blame]
Amir Levy9659e592016-10-27 18:08:27 +03001/* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
2 *
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
361/**
362 * ipa3_send_msg() - Send "message" from kernel client to IPA driver
363 * @meta: [in] message meta-data
364 * @buff: [in] the payload for message
365 * @callback: [in] free callback
366 *
367 * Client supplies the message meta-data and payload which IPA driver buffers
368 * till read by user-space. After read from user space IPA driver invokes the
369 * callback supplied to free the message payload. Client must not touch/free
370 * the message payload after calling this API.
371 *
372 * Returns: 0 on success, negative on failure
373 *
374 * Note: Should not be called from atomic context
375 */
376int ipa3_send_msg(struct ipa_msg_meta *meta, void *buff,
377 ipa_msg_free_fn callback)
378{
379 struct ipa3_push_msg *msg;
380
381 if (meta == NULL || (buff == NULL && callback != NULL) ||
382 (buff != NULL && callback == NULL)) {
383 IPAERR("invalid param meta=%p buff=%p, callback=%p\n",
384 meta, buff, callback);
385 return -EINVAL;
386 }
387
388 if (meta->msg_type >= IPA_EVENT_MAX_NUM) {
389 IPAERR("unsupported message type %d\n", meta->msg_type);
390 return -EINVAL;
391 }
392
393 msg = kzalloc(sizeof(struct ipa3_push_msg), GFP_KERNEL);
394 if (msg == NULL) {
395 IPAERR("fail to alloc ipa_msg container\n");
396 return -ENOMEM;
397 }
398
399 msg->meta = *meta;
400 msg->buff = buff;
401 msg->callback = callback;
402
403 mutex_lock(&ipa3_ctx->msg_lock);
404 list_add_tail(&msg->link, &ipa3_ctx->msg_list);
405 mutex_unlock(&ipa3_ctx->msg_lock);
406 IPA_STATS_INC_CNT(ipa3_ctx->stats.msg_w[meta->msg_type]);
407
408 wake_up(&ipa3_ctx->msg_waitq);
409
410 return 0;
411}
412
413/**
414 * ipa3_register_pull_msg() - register pull message type
415 * @meta: [in] message meta-data
416 * @callback: [in] pull callback
417 *
418 * Register message callback by kernel client with IPA driver for IPA driver to
419 * pull message on-demand.
420 *
421 * Returns: 0 on success, negative on failure
422 *
423 * Note: Should not be called from atomic context
424 */
425int ipa3_register_pull_msg(struct ipa_msg_meta *meta, ipa_msg_pull_fn callback)
426{
427 struct ipa3_pull_msg *msg;
428
429 if (meta == NULL || callback == NULL) {
430 IPAERR("invalid param meta=%p callback=%p\n", meta, callback);
431 return -EINVAL;
432 }
433
434 msg = kzalloc(sizeof(struct ipa3_pull_msg), GFP_KERNEL);
435 if (msg == NULL) {
436 IPAERR("fail to alloc ipa_msg container\n");
437 return -ENOMEM;
438 }
439
440 msg->meta = *meta;
441 msg->callback = callback;
442
443 mutex_lock(&ipa3_ctx->msg_lock);
444 list_add_tail(&msg->link, &ipa3_ctx->pull_msg_list);
445 mutex_unlock(&ipa3_ctx->msg_lock);
446
447 return 0;
448}
449
450/**
451 * ipa3_deregister_pull_msg() - De-register pull message type
452 * @meta: [in] message meta-data
453 *
454 * De-register "message" by kernel client from IPA driver
455 *
456 * Returns: 0 on success, negative on failure
457 *
458 * Note: Should not be called from atomic context
459 */
460int ipa3_deregister_pull_msg(struct ipa_msg_meta *meta)
461{
462 struct ipa3_pull_msg *entry;
463 struct ipa3_pull_msg *next;
464 int result = -EINVAL;
465
466 if (meta == NULL) {
467 IPAERR("invalid param name=%p\n", meta);
468 return result;
469 }
470
471 mutex_lock(&ipa3_ctx->msg_lock);
472 list_for_each_entry_safe(entry, next, &ipa3_ctx->pull_msg_list, link) {
473 if (entry->meta.msg_len == meta->msg_len &&
474 entry->meta.msg_type == meta->msg_type) {
475 list_del(&entry->link);
476 kfree(entry);
477 result = 0;
478 break;
479 }
480 }
481 mutex_unlock(&ipa3_ctx->msg_lock);
482 return result;
483}
484
485/**
486 * ipa3_read() - read message from IPA device
487 * @filp: [in] file pointer
488 * @buf: [out] buffer to read into
489 * @count: [in] size of above buffer
490 * @f_pos: [inout] file position
491 *
492 * Uer-space should continually read from /dev/ipa, read wll block when there
493 * are no messages to read. Upon return, user-space should read the ipa_msg_meta
494 * from the start of the buffer to know what type of message was read and its
495 * length in the remainder of the buffer. Buffer supplied must be big enough to
496 * hold the message meta-data and the largest defined message type
497 *
498 * Returns: how many bytes copied to buffer
499 *
500 * Note: Should not be called from atomic context
501 */
502ssize_t ipa3_read(struct file *filp, char __user *buf, size_t count,
503 loff_t *f_pos)
504{
505 char __user *start;
506 struct ipa3_push_msg *msg = NULL;
507 int ret;
508 DEFINE_WAIT(wait);
509 int locked;
510
511 start = buf;
512
513 while (1) {
514 prepare_to_wait(&ipa3_ctx->msg_waitq,
515 &wait,
516 TASK_INTERRUPTIBLE);
517
518 mutex_lock(&ipa3_ctx->msg_lock);
519 locked = 1;
520 if (!list_empty(&ipa3_ctx->msg_list)) {
521 msg = list_first_entry(&ipa3_ctx->msg_list,
522 struct ipa3_push_msg, link);
523 list_del(&msg->link);
524 }
525
526 IPADBG_LOW("msg=%p\n", msg);
527
528 if (msg) {
529 locked = 0;
530 mutex_unlock(&ipa3_ctx->msg_lock);
531 if (copy_to_user(buf, &msg->meta,
532 sizeof(struct ipa_msg_meta))) {
533 ret = -EFAULT;
534 break;
535 }
536 buf += sizeof(struct ipa_msg_meta);
537 count -= sizeof(struct ipa_msg_meta);
538 if (msg->buff) {
539 if (copy_to_user(buf, msg->buff,
540 msg->meta.msg_len)) {
541 ret = -EFAULT;
542 break;
543 }
544 buf += msg->meta.msg_len;
545 count -= msg->meta.msg_len;
546 msg->callback(msg->buff, msg->meta.msg_len,
547 msg->meta.msg_type);
548 }
549 IPA_STATS_INC_CNT(
550 ipa3_ctx->stats.msg_r[msg->meta.msg_type]);
551 kfree(msg);
552 }
553
554 ret = -EAGAIN;
555 if (filp->f_flags & O_NONBLOCK)
556 break;
557
558 ret = -EINTR;
559 if (signal_pending(current))
560 break;
561
562 if (start != buf)
563 break;
564
565 locked = 0;
566 mutex_unlock(&ipa3_ctx->msg_lock);
567 schedule();
568 }
569
570 finish_wait(&ipa3_ctx->msg_waitq, &wait);
571 if (start != buf && ret != -EFAULT)
572 ret = buf - start;
573
574 if (locked)
575 mutex_unlock(&ipa3_ctx->msg_lock);
576
577 return ret;
578}
579
580/**
581 * ipa3_pull_msg() - pull the specified message from client
582 * @meta: [in] message meta-data
583 * @buf: [out] buffer to read into
584 * @count: [in] size of above buffer
585 *
586 * Populate the supplied buffer with the pull message which is fetched
587 * from client, the message must have previously been registered with
588 * the IPA driver
589 *
590 * Returns: how many bytes copied to buffer
591 *
592 * Note: Should not be called from atomic context
593 */
594int ipa3_pull_msg(struct ipa_msg_meta *meta, char *buff, size_t count)
595{
596 struct ipa3_pull_msg *entry;
597 int result = -EINVAL;
598
599 if (meta == NULL || buff == NULL || !count) {
600 IPAERR("invalid param name=%p buff=%p count=%zu\n",
601 meta, buff, count);
602 return result;
603 }
604
605 mutex_lock(&ipa3_ctx->msg_lock);
606 list_for_each_entry(entry, &ipa3_ctx->pull_msg_list, link) {
607 if (entry->meta.msg_len == meta->msg_len &&
608 entry->meta.msg_type == meta->msg_type) {
609 result = entry->callback(buff, count, meta->msg_type);
610 break;
611 }
612 }
613 mutex_unlock(&ipa3_ctx->msg_lock);
614 return result;
615}