blob: 37ba457aa9147dccae7cd4e5d56f36425b4bea6c [file] [log] [blame]
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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#define DEBUG
14
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/string.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/types.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/sched.h>
25#include <linux/poll.h>
26#include <linux/wakelock.h>
27#include <linux/platform_device.h>
28#include <linux/uaccess.h>
29#include <linux/debugfs.h>
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -060030#include <linux/rwsem.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031
32#include <asm/uaccess.h>
33#include <asm/byteorder.h>
34
35#include <mach/smem_log.h>
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -060036#include <mach/subsystem_notif.h>
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -060037#include <mach/msm_ipc_router.h>
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060038#include <mach/msm_ipc_logging.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#include "ipc_router.h"
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -060041#include "modem_notifier.h"
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -060042#include "msm_ipc_router_security.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043
44enum {
45 SMEM_LOG = 1U << 0,
46 RTR_DBG = 1U << 1,
47 R2R_MSG = 1U << 2,
48 R2R_RAW = 1U << 3,
49 NTFY_MSG = 1U << 4,
50 R2R_RAW_HDR = 1U << 5,
51};
52
53static int msm_ipc_router_debug_mask;
54module_param_named(debug_mask, msm_ipc_router_debug_mask,
55 int, S_IRUGO | S_IWUSR | S_IWGRP);
56
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060057static void *ipc_rtr_log_ctxt;
58#define IPC_RTR_LOG_PAGES 5
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059#define DIAG(x...) pr_info("[RR] ERROR " x)
60
61#if defined(DEBUG)
62#define D(x...) do { \
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060063if (ipc_rtr_log_ctxt) \
64 ipc_log_string(ipc_rtr_log_ctxt, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065if (msm_ipc_router_debug_mask & RTR_DBG) \
66 pr_info(x); \
67} while (0)
68
69#define RR(x...) do { \
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060070if (ipc_rtr_log_ctxt) \
71 ipc_log_string(ipc_rtr_log_ctxt, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072if (msm_ipc_router_debug_mask & R2R_MSG) \
73 pr_info("[RR] "x); \
74} while (0)
75
76#define RAW(x...) do { \
77if (msm_ipc_router_debug_mask & R2R_RAW) \
78 pr_info("[RAW] "x); \
79} while (0)
80
81#define NTFY(x...) do { \
82if (msm_ipc_router_debug_mask & NTFY_MSG) \
83 pr_info("[NOTIFY] "x); \
84} while (0)
85
86#define RAW_HDR(x...) do { \
87if (msm_ipc_router_debug_mask & R2R_RAW_HDR) \
88 pr_info("[HDR] "x); \
89} while (0)
90#else
91#define D(x...) do { } while (0)
92#define RR(x...) do { } while (0)
93#define RAW(x...) do { } while (0)
94#define RAW_HDR(x...) do { } while (0)
95#define NTFY(x...) do { } while (0)
96#endif
97
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +053098#define IPC_ROUTER_LOG_EVENT_ERROR 0x00
99#define IPC_ROUTER_LOG_EVENT_TX 0x01
100#define IPC_ROUTER_LOG_EVENT_RX 0x02
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101
102static LIST_HEAD(control_ports);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600103static DECLARE_RWSEM(control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104
105#define LP_HASH_SIZE 32
106static struct list_head local_ports[LP_HASH_SIZE];
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600107static DECLARE_RWSEM(local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600109/*
110 * Server info is organized as a hash table. The server's service ID is
111 * used to index into the hash table. The instance ID of most of the servers
112 * are 1 or 2. The service IDs are well distributed compared to the instance
113 * IDs and hence choosing service ID to index into this hash table optimizes
114 * the hash table operations like add, lookup, destroy.
115 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116#define SRV_HASH_SIZE 32
117static struct list_head server_list[SRV_HASH_SIZE];
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600118static DECLARE_RWSEM(server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119
120struct msm_ipc_server {
121 struct list_head list;
122 struct msm_ipc_port_name name;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600123 int synced_sec_rule;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600124 char pdev_name[32];
125 int next_pdev_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126 struct list_head server_port_list;
127};
128
129struct msm_ipc_server_port {
130 struct list_head list;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600131 struct platform_device pdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132 struct msm_ipc_port_addr server_addr;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600133 struct msm_ipc_router_xprt_info *xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134};
135
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530136struct msm_ipc_resume_tx_port {
137 struct list_head list;
138 uint32_t port_id;
139 uint32_t node_id;
140};
141
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142#define RP_HASH_SIZE 32
143struct msm_ipc_router_remote_port {
144 struct list_head list;
145 uint32_t node_id;
146 uint32_t port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700147 uint32_t tx_quota_cnt;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600148 struct mutex quota_lock_lhb2;
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530149 struct list_head resume_tx_port_list;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600150 void *sec_rule;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600151 struct msm_ipc_server *server;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152};
153
154struct msm_ipc_router_xprt_info {
155 struct list_head list;
156 struct msm_ipc_router_xprt *xprt;
157 uint32_t remote_node_id;
158 uint32_t initialized;
159 struct list_head pkt_list;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160 struct wake_lock wakelock;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600161 struct mutex rx_lock_lhb2;
162 struct mutex tx_lock_lhb2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163 uint32_t need_len;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600164 uint32_t abort_data_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165 struct work_struct read_data;
166 struct workqueue_struct *workqueue;
167};
168
169#define RT_HASH_SIZE 4
170struct msm_ipc_routing_table_entry {
171 struct list_head list;
172 uint32_t node_id;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600173 uint32_t neighbor_node_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174 struct list_head remote_port_list[RP_HASH_SIZE];
175 struct msm_ipc_router_xprt_info *xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600176 struct rw_semaphore lock_lha4;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 unsigned long num_tx_bytes;
178 unsigned long num_rx_bytes;
179};
180
181static struct list_head routing_table[RT_HASH_SIZE];
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600182static DECLARE_RWSEM(routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183static int routing_table_inited;
184
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185static void do_read_data(struct work_struct *work);
186
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187static LIST_HEAD(xprt_info_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600188static DECLARE_RWSEM(xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -0600190static DECLARE_COMPLETION(msm_ipc_local_router_up);
191#define IPC_ROUTER_INIT_TIMEOUT (10 * HZ)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192
193static uint32_t next_port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600194static DEFINE_MUTEX(next_port_id_lock_lha1);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600195static struct workqueue_struct *msm_ipc_router_workqueue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196
197enum {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 DOWN,
199 UP,
200};
201
202static void init_routing_table(void)
203{
204 int i;
205 for (i = 0; i < RT_HASH_SIZE; i++)
206 INIT_LIST_HEAD(&routing_table[i]);
207}
208
209static struct msm_ipc_routing_table_entry *alloc_routing_table_entry(
210 uint32_t node_id)
211{
212 int i;
213 struct msm_ipc_routing_table_entry *rt_entry;
214
215 rt_entry = kmalloc(sizeof(struct msm_ipc_routing_table_entry),
216 GFP_KERNEL);
217 if (!rt_entry) {
218 pr_err("%s: rt_entry allocation failed for %d\n",
219 __func__, node_id);
220 return NULL;
221 }
222
223 for (i = 0; i < RP_HASH_SIZE; i++)
224 INIT_LIST_HEAD(&rt_entry->remote_port_list[i]);
225
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600226 init_rwsem(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 rt_entry->node_id = node_id;
228 rt_entry->xprt_info = NULL;
229 return rt_entry;
230}
231
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600232/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233static int add_routing_table_entry(
234 struct msm_ipc_routing_table_entry *rt_entry)
235{
236 uint32_t key;
237
238 if (!rt_entry)
239 return -EINVAL;
240
241 key = (rt_entry->node_id % RT_HASH_SIZE);
242 list_add_tail(&rt_entry->list, &routing_table[key]);
243 return 0;
244}
245
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600246/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247static struct msm_ipc_routing_table_entry *lookup_routing_table(
248 uint32_t node_id)
249{
250 uint32_t key = (node_id % RT_HASH_SIZE);
251 struct msm_ipc_routing_table_entry *rt_entry;
252
253 list_for_each_entry(rt_entry, &routing_table[key], list) {
254 if (rt_entry->node_id == node_id)
255 return rt_entry;
256 }
257 return NULL;
258}
259
260struct rr_packet *rr_read(struct msm_ipc_router_xprt_info *xprt_info)
261{
262 struct rr_packet *temp_pkt;
263
264 if (!xprt_info)
265 return NULL;
266
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600267 mutex_lock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600268 if (xprt_info->abort_data_read) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600269 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -0600270 pr_err("%s detected SSR & exiting now\n",
271 xprt_info->xprt->name);
272 return NULL;
273 }
274
275 if (list_empty(&xprt_info->pkt_list)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600276 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600277 return NULL;
278 }
279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 temp_pkt = list_first_entry(&xprt_info->pkt_list,
281 struct rr_packet, list);
282 list_del(&temp_pkt->list);
283 if (list_empty(&xprt_info->pkt_list))
284 wake_unlock(&xprt_info->wakelock);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600285 mutex_unlock(&xprt_info->rx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 return temp_pkt;
287}
288
289struct rr_packet *clone_pkt(struct rr_packet *pkt)
290{
291 struct rr_packet *cloned_pkt;
292 struct sk_buff *temp_skb, *cloned_skb;
293 struct sk_buff_head *pkt_fragment_q;
294
295 cloned_pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
296 if (!cloned_pkt) {
297 pr_err("%s: failure\n", __func__);
298 return NULL;
299 }
300
301 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
302 if (!pkt_fragment_q) {
303 pr_err("%s: pkt_frag_q alloc failure\n", __func__);
304 kfree(cloned_pkt);
305 return NULL;
306 }
307 skb_queue_head_init(pkt_fragment_q);
308
309 skb_queue_walk(pkt->pkt_fragment_q, temp_skb) {
310 cloned_skb = skb_clone(temp_skb, GFP_KERNEL);
311 if (!cloned_skb)
312 goto fail_clone;
313 skb_queue_tail(pkt_fragment_q, cloned_skb);
314 }
315 cloned_pkt->pkt_fragment_q = pkt_fragment_q;
316 cloned_pkt->length = pkt->length;
317 return cloned_pkt;
318
319fail_clone:
320 while (!skb_queue_empty(pkt_fragment_q)) {
321 temp_skb = skb_dequeue(pkt_fragment_q);
322 kfree_skb(temp_skb);
323 }
324 kfree(pkt_fragment_q);
325 kfree(cloned_pkt);
326 return NULL;
327}
328
329struct rr_packet *create_pkt(struct sk_buff_head *data)
330{
331 struct rr_packet *pkt;
332 struct sk_buff *temp_skb;
333
334 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
335 if (!pkt) {
336 pr_err("%s: failure\n", __func__);
337 return NULL;
338 }
339
340 pkt->pkt_fragment_q = data;
341 skb_queue_walk(pkt->pkt_fragment_q, temp_skb)
342 pkt->length += temp_skb->len;
343 return pkt;
344}
345
346void release_pkt(struct rr_packet *pkt)
347{
348 struct sk_buff *temp_skb;
349
350 if (!pkt)
351 return;
352
353 if (!pkt->pkt_fragment_q) {
354 kfree(pkt);
355 return;
356 }
357
358 while (!skb_queue_empty(pkt->pkt_fragment_q)) {
359 temp_skb = skb_dequeue(pkt->pkt_fragment_q);
360 kfree_skb(temp_skb);
361 }
362 kfree(pkt->pkt_fragment_q);
363 kfree(pkt);
364 return;
365}
366
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600367static struct sk_buff_head *msm_ipc_router_buf_to_skb(void *buf,
368 unsigned int buf_len)
369{
370 struct sk_buff_head *skb_head;
371 struct sk_buff *skb;
372 int first = 1, offset = 0;
373 int skb_size, data_size;
374 void *data;
375
376 skb_head = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
377 if (!skb_head) {
378 pr_err("%s: Couldnot allocate skb_head\n", __func__);
379 return NULL;
380 }
381 skb_queue_head_init(skb_head);
382
383 data_size = buf_len;
384 while (offset != buf_len) {
385 skb_size = data_size;
386 if (first)
387 skb_size += IPC_ROUTER_HDR_SIZE;
388
389 skb = alloc_skb(skb_size, GFP_KERNEL);
390 if (!skb) {
391 if (skb_size <= (PAGE_SIZE/2)) {
392 pr_err("%s: cannot allocate skb\n", __func__);
393 goto buf_to_skb_error;
394 }
395 data_size = data_size / 2;
396 continue;
397 }
398
399 if (first) {
400 skb_reserve(skb, IPC_ROUTER_HDR_SIZE);
401 first = 0;
402 }
403
404 data = skb_put(skb, data_size);
405 memcpy(skb->data, buf + offset, data_size);
406 skb_queue_tail(skb_head, skb);
407 offset += data_size;
408 data_size = buf_len - offset;
409 }
410 return skb_head;
411
412buf_to_skb_error:
413 while (!skb_queue_empty(skb_head)) {
414 skb = skb_dequeue(skb_head);
415 kfree_skb(skb);
416 }
417 kfree(skb_head);
418 return NULL;
419}
420
421static void *msm_ipc_router_skb_to_buf(struct sk_buff_head *skb_head,
422 unsigned int len)
423{
424 struct sk_buff *temp;
425 int offset = 0, buf_len = 0, copy_len;
426 void *buf;
427
428 if (!skb_head) {
429 pr_err("%s: NULL skb_head\n", __func__);
430 return NULL;
431 }
432
433 temp = skb_peek(skb_head);
434 buf_len = len;
435 buf = kmalloc(buf_len, GFP_KERNEL);
436 if (!buf) {
437 pr_err("%s: cannot allocate buf\n", __func__);
438 return NULL;
439 }
440 skb_queue_walk(skb_head, temp) {
441 copy_len = buf_len < temp->len ? buf_len : temp->len;
442 memcpy(buf + offset, temp->data, copy_len);
443 offset += copy_len;
444 buf_len -= copy_len;
445 }
446 return buf;
447}
448
449static void msm_ipc_router_free_skb(struct sk_buff_head *skb_head)
450{
451 struct sk_buff *temp_skb;
452
453 if (!skb_head)
454 return;
455
456 while (!skb_queue_empty(skb_head)) {
457 temp_skb = skb_dequeue(skb_head);
458 kfree_skb(temp_skb);
459 }
460 kfree(skb_head);
461}
462
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600463static int post_pkt_to_port(struct msm_ipc_port *port_ptr,
464 struct rr_packet *pkt, int clone)
465{
466 struct rr_packet *temp_pkt = pkt;
Zaheerulla Meer29302cf2013-05-24 21:19:18 +0530467 void (*notify)(unsigned event, void *priv);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600468
469 if (unlikely(!port_ptr || !pkt))
470 return -EINVAL;
471
472 if (clone) {
473 temp_pkt = clone_pkt(pkt);
474 if (!temp_pkt) {
475 pr_err("%s: Error cloning packet for port %08x:%08x\n",
476 __func__, port_ptr->this_port.node_id,
477 port_ptr->this_port.port_id);
478 return -ENOMEM;
479 }
480 }
481
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600482 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600483 wake_lock(&port_ptr->port_rx_wake_lock);
484 list_add_tail(&temp_pkt->list, &port_ptr->port_rx_q);
485 wake_up(&port_ptr->port_rx_wait_q);
Zaheerulla Meer29302cf2013-05-24 21:19:18 +0530486 notify = port_ptr->notify;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600487 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Zaheerulla Meer29302cf2013-05-24 21:19:18 +0530488 if (notify)
489 notify(MSM_IPC_ROUTER_READ_CB, port_ptr->priv);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600490 return 0;
491}
492
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493static int post_control_ports(struct rr_packet *pkt)
494{
495 struct msm_ipc_port *port_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496
497 if (!pkt)
498 return -EINVAL;
499
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600500 down_read(&control_ports_lock_lha5);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600501 list_for_each_entry(port_ptr, &control_ports, list)
502 post_pkt_to_port(port_ptr, pkt, 1);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600503 up_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 return 0;
505}
506
507static uint32_t allocate_port_id(void)
508{
509 uint32_t port_id = 0, prev_port_id, key;
510 struct msm_ipc_port *port_ptr;
511
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600512 mutex_lock(&next_port_id_lock_lha1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 prev_port_id = next_port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600514 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515 do {
516 next_port_id++;
517 if ((next_port_id & 0xFFFFFFFE) == 0xFFFFFFFE)
518 next_port_id = 1;
519
520 key = (next_port_id & (LP_HASH_SIZE - 1));
521 if (list_empty(&local_ports[key])) {
522 port_id = next_port_id;
523 break;
524 }
525 list_for_each_entry(port_ptr, &local_ports[key], list) {
526 if (port_ptr->this_port.port_id == next_port_id) {
527 port_id = next_port_id;
528 break;
529 }
530 }
531 if (!port_id) {
532 port_id = next_port_id;
533 break;
534 }
535 port_id = 0;
536 } while (next_port_id != prev_port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600537 up_read(&local_ports_lock_lha2);
538 mutex_unlock(&next_port_id_lock_lha1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539
540 return port_id;
541}
542
543void msm_ipc_router_add_local_port(struct msm_ipc_port *port_ptr)
544{
545 uint32_t key;
546
547 if (!port_ptr)
548 return;
549
550 key = (port_ptr->this_port.port_id & (LP_HASH_SIZE - 1));
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600551 down_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552 list_add_tail(&port_ptr->list, &local_ports[key]);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600553 up_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554}
555
556struct msm_ipc_port *msm_ipc_router_create_raw_port(void *endpoint,
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600557 void (*notify)(unsigned event, void *priv),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 void *priv)
559{
560 struct msm_ipc_port *port_ptr;
561
562 port_ptr = kzalloc(sizeof(struct msm_ipc_port), GFP_KERNEL);
563 if (!port_ptr)
564 return NULL;
565
566 port_ptr->this_port.node_id = IPC_ROUTER_NID_LOCAL;
567 port_ptr->this_port.port_id = allocate_port_id();
568 if (!port_ptr->this_port.port_id) {
569 pr_err("%s: All port ids are in use\n", __func__);
570 kfree(port_ptr);
571 return NULL;
572 }
573
574 spin_lock_init(&port_ptr->port_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575 INIT_LIST_HEAD(&port_ptr->port_rx_q);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600576 mutex_init(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577 init_waitqueue_head(&port_ptr->port_rx_wait_q);
Karthikeyan Ramasubramanianb4aeeb92012-03-13 12:31:43 -0600578 snprintf(port_ptr->rx_wakelock_name, MAX_WAKELOCK_NAME_SZ,
Karthikeyan Ramasubramanian090486e2013-02-14 13:53:20 -0700579 "ipc%08x_%s",
580 port_ptr->this_port.port_id,
581 current->comm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 wake_lock_init(&port_ptr->port_rx_wake_lock,
Karthikeyan Ramasubramanianb4aeeb92012-03-13 12:31:43 -0600583 WAKE_LOCK_SUSPEND, port_ptr->rx_wakelock_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
585 port_ptr->endpoint = endpoint;
586 port_ptr->notify = notify;
587 port_ptr->priv = priv;
588
589 msm_ipc_router_add_local_port(port_ptr);
590 return port_ptr;
591}
592
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600593/* Must be called with local_ports_lock_lha2 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594static struct msm_ipc_port *msm_ipc_router_lookup_local_port(uint32_t port_id)
595{
596 int key = (port_id & (LP_HASH_SIZE - 1));
597 struct msm_ipc_port *port_ptr;
598
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599 list_for_each_entry(port_ptr, &local_ports[key], list) {
600 if (port_ptr->this_port.port_id == port_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601 return port_ptr;
602 }
603 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 return NULL;
605}
606
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600607/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608static struct msm_ipc_router_remote_port *msm_ipc_router_lookup_remote_port(
609 uint32_t node_id,
610 uint32_t port_id)
611{
612 struct msm_ipc_router_remote_port *rport_ptr;
613 struct msm_ipc_routing_table_entry *rt_entry;
614 int key = (port_id & (RP_HASH_SIZE - 1));
615
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616 rt_entry = lookup_routing_table(node_id);
617 if (!rt_entry) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 pr_err("%s: Node is not up\n", __func__);
619 return NULL;
620 }
621
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600622 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 list_for_each_entry(rport_ptr,
624 &rt_entry->remote_port_list[key], list) {
625 if (rport_ptr->port_id == port_id) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600626 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 return rport_ptr;
628 }
629 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600630 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631 return NULL;
632}
633
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600634/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635static struct msm_ipc_router_remote_port *msm_ipc_router_create_remote_port(
636 uint32_t node_id,
637 uint32_t port_id)
638{
639 struct msm_ipc_router_remote_port *rport_ptr;
640 struct msm_ipc_routing_table_entry *rt_entry;
641 int key = (port_id & (RP_HASH_SIZE - 1));
642
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 rt_entry = lookup_routing_table(node_id);
644 if (!rt_entry) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 pr_err("%s: Node is not up\n", __func__);
646 return NULL;
647 }
648
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 rport_ptr = kmalloc(sizeof(struct msm_ipc_router_remote_port),
650 GFP_KERNEL);
651 if (!rport_ptr) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 pr_err("%s: Remote port alloc failed\n", __func__);
653 return NULL;
654 }
655 rport_ptr->port_id = port_id;
656 rport_ptr->node_id = node_id;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600657 rport_ptr->sec_rule = NULL;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600658 rport_ptr->server = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659 rport_ptr->tx_quota_cnt = 0;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600660 mutex_init(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530661 INIT_LIST_HEAD(&rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600662 down_write(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663 list_add_tail(&rport_ptr->list,
664 &rt_entry->remote_port_list[key]);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600665 up_write(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666 return rport_ptr;
667}
668
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530669/**
670 * msm_ipc_router_free_resume_tx_port() - Free the resume_tx ports
671 * @rport_ptr: Pointer to the remote port.
672 *
673 * This function deletes all the resume_tx ports associated with a remote port
674 * and frees the memory allocated to each resume_tx port.
675 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600676 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530677 */
678static void msm_ipc_router_free_resume_tx_port(
679 struct msm_ipc_router_remote_port *rport_ptr)
680{
681 struct msm_ipc_resume_tx_port *rtx_port, *tmp_rtx_port;
682
683 list_for_each_entry_safe(rtx_port, tmp_rtx_port,
684 &rport_ptr->resume_tx_port_list, list) {
685 list_del(&rtx_port->list);
686 kfree(rtx_port);
687 }
688}
689
690/**
691 * msm_ipc_router_lookup_resume_tx_port() - Lookup resume_tx port list
692 * @rport_ptr: Remote port whose resume_tx port list needs to be looked.
693 * @port_id: Port ID which needs to be looked from the list.
694 *
695 * return 1 if the port_id is found in the list, else 0.
696 *
697 * This function is used to lookup the existence of a local port in
698 * remote port's resume_tx list. This function is used to ensure that
699 * the same port is not added to the remote_port's resume_tx list repeatedly.
700 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600701 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530702 */
703static int msm_ipc_router_lookup_resume_tx_port(
704 struct msm_ipc_router_remote_port *rport_ptr, uint32_t port_id)
705{
706 struct msm_ipc_resume_tx_port *rtx_port;
707
708 list_for_each_entry(rtx_port, &rport_ptr->resume_tx_port_list, list) {
709 if (port_id == rtx_port->port_id)
710 return 1;
711 }
712 return 0;
713}
714
715/**
716 * post_resume_tx() - Post the resume_tx event
717 * @rport_ptr: Pointer to the remote port
718 * @pkt : The data packet that is received on a resume_tx event
719 *
720 * This function informs about the reception of the resume_tx message from a
721 * remote port pointed by rport_ptr to all the local ports that are in the
722 * resume_tx_ports_list of this remote port. On posting the information, this
723 * function sequentially deletes each entry in the resume_tx_port_list of the
724 * remote port.
725 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600726 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530727 */
728static void post_resume_tx(struct msm_ipc_router_remote_port *rport_ptr,
729 struct rr_packet *pkt)
730{
731 struct msm_ipc_resume_tx_port *rtx_port, *tmp_rtx_port;
732 struct msm_ipc_port *local_port;
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530733
734 list_for_each_entry_safe(rtx_port, tmp_rtx_port,
735 &rport_ptr->resume_tx_port_list, list) {
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530736 local_port =
737 msm_ipc_router_lookup_local_port(rtx_port->port_id);
Zaheerulla Meerc8400402013-05-08 19:27:27 +0530738 if (local_port && local_port->notify)
739 local_port->notify(MSM_IPC_ROUTER_RESUME_TX,
740 local_port->priv);
741 else if (local_port)
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600742 post_pkt_to_port(local_port, pkt, 1);
Zaheerulla Meerc8400402013-05-08 19:27:27 +0530743 else
744 pr_err("%s: Local Port %d not Found",
745 __func__, rtx_port->port_id);
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530746 list_del(&rtx_port->list);
747 kfree(rtx_port);
748 }
749}
750
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600751/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752static void msm_ipc_router_destroy_remote_port(
753 struct msm_ipc_router_remote_port *rport_ptr)
754{
755 uint32_t node_id;
756 struct msm_ipc_routing_table_entry *rt_entry;
757
758 if (!rport_ptr)
759 return;
760
761 node_id = rport_ptr->node_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 rt_entry = lookup_routing_table(node_id);
763 if (!rt_entry) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764 pr_err("%s: Node %d is not up\n", __func__, node_id);
765 return;
766 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600767 down_write(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 list_del(&rport_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600769 up_write(&rt_entry->lock_lha4);
770 mutex_lock(&rport_ptr->quota_lock_lhb2);
771 msm_ipc_router_free_resume_tx_port(rport_ptr);
772 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 kfree(rport_ptr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 return;
775}
776
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600777/**
778 * msm_ipc_router_lookup_server() - Lookup server information
779 * @service: Service ID of the server info to be looked up.
780 * @instance: Instance ID of the server info to be looked up.
781 * @node_id: Node/Processor ID in which the server is hosted.
782 * @port_id: Port ID within the node in which the server is hosted.
783 *
784 * @return: If found Pointer to server structure, else NULL.
785 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600786 * Note1: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600787 * Note2: If the <node_id:port_id> are <0:0>, then the lookup is restricted
788 * to <service:instance>. Used only when a client wants to send a
789 * message to any QMI server.
790 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791static struct msm_ipc_server *msm_ipc_router_lookup_server(
792 uint32_t service,
793 uint32_t instance,
794 uint32_t node_id,
795 uint32_t port_id)
796{
797 struct msm_ipc_server *server;
798 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600799 int key = (service & (SRV_HASH_SIZE - 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 list_for_each_entry(server, &server_list[key], list) {
802 if ((server->name.service != service) ||
803 (server->name.instance != instance))
804 continue;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600805 if ((node_id == 0) && (port_id == 0))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 return server;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 list_for_each_entry(server_port, &server->server_port_list,
808 list) {
809 if ((server_port->server_addr.node_id == node_id) &&
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600810 (server_port->server_addr.port_id == port_id))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 return server;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812 }
813 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 return NULL;
815}
816
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600817static void dummy_release(struct device *dev)
818{
819}
820
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600821/**
822 * msm_ipc_router_create_server() - Add server info to hash table
823 * @service: Service ID of the server info to be created.
824 * @instance: Instance ID of the server info to be created.
825 * @node_id: Node/Processor ID in which the server is hosted.
826 * @port_id: Port ID within the node in which the server is hosted.
827 * @xprt_info: XPRT through which the node hosting the server is reached.
828 *
829 * @return: Pointer to server structure on success, else NULL.
830 *
831 * This function adds the server info to the hash table. If the same
832 * server(i.e. <service_id:instance_id>) is hosted in different nodes,
833 * they are maintained as list of "server_port" under "server" structure.
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600834 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600835 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836static struct msm_ipc_server *msm_ipc_router_create_server(
837 uint32_t service,
838 uint32_t instance,
839 uint32_t node_id,
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600840 uint32_t port_id,
841 struct msm_ipc_router_xprt_info *xprt_info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842{
843 struct msm_ipc_server *server = NULL;
844 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600845 int key = (service & (SRV_HASH_SIZE - 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847 list_for_each_entry(server, &server_list[key], list) {
848 if ((server->name.service == service) &&
849 (server->name.instance == instance))
850 goto create_srv_port;
851 }
852
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600853 server = kzalloc(sizeof(struct msm_ipc_server), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 if (!server) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 pr_err("%s: Server allocation failed\n", __func__);
856 return NULL;
857 }
858 server->name.service = service;
859 server->name.instance = instance;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600860 server->synced_sec_rule = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861 INIT_LIST_HEAD(&server->server_port_list);
862 list_add_tail(&server->list, &server_list[key]);
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600863 scnprintf(server->pdev_name, sizeof(server->pdev_name),
864 "QMI%08x:%08x", service, instance);
865 server->next_pdev_id = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866
867create_srv_port:
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600868 server_port = kzalloc(sizeof(struct msm_ipc_server_port), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 if (!server_port) {
870 if (list_empty(&server->server_port_list)) {
871 list_del(&server->list);
872 kfree(server);
873 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 pr_err("%s: Server Port allocation failed\n", __func__);
875 return NULL;
876 }
877 server_port->server_addr.node_id = node_id;
878 server_port->server_addr.port_id = port_id;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600879 server_port->xprt_info = xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 list_add_tail(&server_port->list, &server->server_port_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600882 server_port->pdev.name = server->pdev_name;
883 server_port->pdev.id = server->next_pdev_id++;
884 server_port->pdev.dev.release = dummy_release;
885 platform_device_register(&server_port->pdev);
886
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 return server;
888}
889
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600890/**
891 * msm_ipc_router_destroy_server() - Remove server info from hash table
892 * @server: Server info to be removed.
893 * @node_id: Node/Processor ID in which the server is hosted.
894 * @port_id: Port ID within the node in which the server is hosted.
895 *
896 * This function removes the server_port identified using <node_id:port_id>
897 * from the server structure. If the server_port list under server structure
898 * is empty after removal, then remove the server structure from the server
899 * hash table.
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600900 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600901 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902static void msm_ipc_router_destroy_server(struct msm_ipc_server *server,
903 uint32_t node_id, uint32_t port_id)
904{
905 struct msm_ipc_server_port *server_port;
906
907 if (!server)
908 return;
909
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 list_for_each_entry(server_port, &server->server_port_list, list) {
911 if ((server_port->server_addr.node_id == node_id) &&
912 (server_port->server_addr.port_id == port_id))
913 break;
914 }
915 if (server_port) {
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600916 platform_device_unregister(&server_port->pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917 list_del(&server_port->list);
918 kfree(server_port);
919 }
920 if (list_empty(&server->server_port_list)) {
921 list_del(&server->list);
922 kfree(server);
923 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924 return;
925}
926
927static int msm_ipc_router_send_control_msg(
928 struct msm_ipc_router_xprt_info *xprt_info,
929 union rr_control_msg *msg)
930{
931 struct rr_packet *pkt;
932 struct sk_buff *ipc_rtr_pkt;
933 struct rr_header *hdr;
934 int pkt_size;
935 void *data;
936 struct sk_buff_head *pkt_fragment_q;
937 int ret;
938
939 if (!xprt_info || ((msg->cmd != IPC_ROUTER_CTRL_CMD_HELLO) &&
940 !xprt_info->initialized)) {
941 pr_err("%s: xprt_info not initialized\n", __func__);
942 return -EINVAL;
943 }
944
945 if (xprt_info->remote_node_id == IPC_ROUTER_NID_LOCAL)
946 return 0;
947
948 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
949 if (!pkt) {
950 pr_err("%s: pkt alloc failed\n", __func__);
951 return -ENOMEM;
952 }
953
954 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
955 if (!pkt_fragment_q) {
956 pr_err("%s: pkt_fragment_q alloc failed\n", __func__);
957 kfree(pkt);
958 return -ENOMEM;
959 }
960 skb_queue_head_init(pkt_fragment_q);
961
962 pkt_size = IPC_ROUTER_HDR_SIZE + sizeof(*msg);
963 ipc_rtr_pkt = alloc_skb(pkt_size, GFP_KERNEL);
964 if (!ipc_rtr_pkt) {
965 pr_err("%s: ipc_rtr_pkt alloc failed\n", __func__);
966 kfree(pkt_fragment_q);
967 kfree(pkt);
968 return -ENOMEM;
969 }
970
971 skb_reserve(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
972 data = skb_put(ipc_rtr_pkt, sizeof(*msg));
973 memcpy(data, msg, sizeof(*msg));
974 hdr = (struct rr_header *)skb_push(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
975 if (!hdr) {
976 pr_err("%s: skb_push failed\n", __func__);
977 kfree_skb(ipc_rtr_pkt);
978 kfree(pkt_fragment_q);
979 kfree(pkt);
980 return -ENOMEM;
981 }
982
983 hdr->version = IPC_ROUTER_VERSION;
984 hdr->type = msg->cmd;
985 hdr->src_node_id = IPC_ROUTER_NID_LOCAL;
986 hdr->src_port_id = IPC_ROUTER_ADDRESS;
987 hdr->confirm_rx = 0;
988 hdr->size = sizeof(*msg);
989 hdr->dst_node_id = xprt_info->remote_node_id;
990 hdr->dst_port_id = IPC_ROUTER_ADDRESS;
991 skb_queue_tail(pkt_fragment_q, ipc_rtr_pkt);
992 pkt->pkt_fragment_q = pkt_fragment_q;
993 pkt->length = pkt_size;
994
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600995 mutex_lock(&xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -0700996 ret = xprt_info->xprt->write(pkt, pkt_size, xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600997 mutex_unlock(&xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998
999 release_pkt(pkt);
1000 return ret;
1001}
1002
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001003static int msm_ipc_router_send_server_list(uint32_t node_id,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004 struct msm_ipc_router_xprt_info *xprt_info)
1005{
1006 union rr_control_msg ctl;
1007 struct msm_ipc_server *server;
1008 struct msm_ipc_server_port *server_port;
1009 int i;
1010
1011 if (!xprt_info || !xprt_info->initialized) {
1012 pr_err("%s: Xprt info not initialized\n", __func__);
1013 return -EINVAL;
1014 }
1015
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001016 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017 ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER;
1018
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 for (i = 0; i < SRV_HASH_SIZE; i++) {
1020 list_for_each_entry(server, &server_list[i], list) {
1021 ctl.srv.service = server->name.service;
1022 ctl.srv.instance = server->name.instance;
1023 list_for_each_entry(server_port,
1024 &server->server_port_list, list) {
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001025 if (server_port->server_addr.node_id !=
1026 node_id)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027 continue;
1028
1029 ctl.srv.node_id =
1030 server_port->server_addr.node_id;
1031 ctl.srv.port_id =
1032 server_port->server_addr.port_id;
1033 msm_ipc_router_send_control_msg(xprt_info,
1034 &ctl);
1035 }
1036 }
1037 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038
1039 return 0;
1040}
1041
1042#if defined(DEBUG)
1043static char *type_to_str(int i)
1044{
1045 switch (i) {
1046 case IPC_ROUTER_CTRL_CMD_DATA:
1047 return "data ";
1048 case IPC_ROUTER_CTRL_CMD_HELLO:
1049 return "hello ";
1050 case IPC_ROUTER_CTRL_CMD_BYE:
1051 return "bye ";
1052 case IPC_ROUTER_CTRL_CMD_NEW_SERVER:
1053 return "new_srvr";
1054 case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER:
1055 return "rmv_srvr";
1056 case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT:
1057 return "rmv_clnt";
1058 case IPC_ROUTER_CTRL_CMD_RESUME_TX:
1059 return "resum_tx";
1060 case IPC_ROUTER_CTRL_CMD_EXIT:
1061 return "cmd_exit";
1062 default:
1063 return "invalid";
1064 }
1065}
1066#endif
1067
1068static int broadcast_ctl_msg_locally(union rr_control_msg *msg)
1069{
1070 struct rr_packet *pkt;
1071 struct sk_buff *ipc_rtr_pkt;
1072 struct rr_header *hdr;
1073 int pkt_size;
1074 void *data;
1075 struct sk_buff_head *pkt_fragment_q;
1076 int ret;
1077
1078 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
1079 if (!pkt) {
1080 pr_err("%s: pkt alloc failed\n", __func__);
1081 return -ENOMEM;
1082 }
1083
1084 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
1085 if (!pkt_fragment_q) {
1086 pr_err("%s: pkt_fragment_q alloc failed\n", __func__);
1087 kfree(pkt);
1088 return -ENOMEM;
1089 }
1090 skb_queue_head_init(pkt_fragment_q);
1091
1092 pkt_size = IPC_ROUTER_HDR_SIZE + sizeof(*msg);
1093 ipc_rtr_pkt = alloc_skb(pkt_size, GFP_KERNEL);
1094 if (!ipc_rtr_pkt) {
1095 pr_err("%s: ipc_rtr_pkt alloc failed\n", __func__);
1096 kfree(pkt_fragment_q);
1097 kfree(pkt);
1098 return -ENOMEM;
1099 }
1100
1101 skb_reserve(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
1102 data = skb_put(ipc_rtr_pkt, sizeof(*msg));
1103 memcpy(data, msg, sizeof(*msg));
1104 hdr = (struct rr_header *)skb_push(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
1105 if (!hdr) {
1106 pr_err("%s: skb_push failed\n", __func__);
1107 kfree_skb(ipc_rtr_pkt);
1108 kfree(pkt_fragment_q);
1109 kfree(pkt);
1110 return -ENOMEM;
1111 }
1112 hdr->version = IPC_ROUTER_VERSION;
1113 hdr->type = msg->cmd;
1114 hdr->src_node_id = IPC_ROUTER_NID_LOCAL;
1115 hdr->src_port_id = IPC_ROUTER_ADDRESS;
1116 hdr->confirm_rx = 0;
1117 hdr->size = sizeof(*msg);
1118 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
1119 hdr->dst_port_id = IPC_ROUTER_ADDRESS;
1120 skb_queue_tail(pkt_fragment_q, ipc_rtr_pkt);
1121 pkt->pkt_fragment_q = pkt_fragment_q;
1122 pkt->length = pkt_size;
1123
1124 ret = post_control_ports(pkt);
1125 release_pkt(pkt);
1126 return ret;
1127}
1128
1129static int broadcast_ctl_msg(union rr_control_msg *ctl)
1130{
1131 struct msm_ipc_router_xprt_info *xprt_info;
1132
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001133 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001134 list_for_each_entry(xprt_info, &xprt_info_list, list) {
1135 msm_ipc_router_send_control_msg(xprt_info, ctl);
1136 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001137 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138
1139 return 0;
1140}
1141
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001142static int relay_ctl_msg(struct msm_ipc_router_xprt_info *xprt_info,
1143 union rr_control_msg *ctl)
1144{
1145 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1146
1147 if (!xprt_info || !ctl)
1148 return -EINVAL;
1149
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001150 down_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001151 list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) {
1152 if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id)
1153 msm_ipc_router_send_control_msg(fwd_xprt_info, ctl);
1154 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001155 up_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001156
1157 return 0;
1158}
1159
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160static int relay_msg(struct msm_ipc_router_xprt_info *xprt_info,
1161 struct rr_packet *pkt)
1162{
1163 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1164
1165 if (!xprt_info || !pkt)
1166 return -EINVAL;
1167
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001168 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169 list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001170 mutex_lock(&fwd_xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001171 if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id)
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001172 fwd_xprt_info->xprt->write(pkt, pkt->length,
1173 fwd_xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001174 mutex_unlock(&fwd_xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001176 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 return 0;
1178}
1179
1180static int forward_msg(struct msm_ipc_router_xprt_info *xprt_info,
1181 struct rr_packet *pkt)
1182{
1183 uint32_t dst_node_id;
1184 struct sk_buff *head_pkt;
1185 struct rr_header *hdr;
1186 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1187 struct msm_ipc_routing_table_entry *rt_entry;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001188 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189
1190 if (!xprt_info || !pkt)
1191 return -EINVAL;
1192
1193 head_pkt = skb_peek(pkt->pkt_fragment_q);
1194 if (!head_pkt)
1195 return -EINVAL;
1196
1197 hdr = (struct rr_header *)head_pkt->data;
1198 dst_node_id = hdr->dst_node_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001199 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 rt_entry = lookup_routing_table(dst_node_id);
1201 if (!(rt_entry) || !(rt_entry->xprt_info)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001202 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203 pr_err("%s: Routing table not initialized\n", __func__);
1204 return -ENODEV;
1205 }
1206
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001207 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 fwd_xprt_info = rt_entry->xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 if (xprt_info->remote_node_id == fwd_xprt_info->remote_node_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210 pr_err("%s: Discarding Command to route back\n", __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001211 ret = -EINVAL;
1212 goto fwd_msg_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213 }
1214
1215 if (xprt_info->xprt->link_id == fwd_xprt_info->xprt->link_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216 pr_err("%s: DST in the same cluster\n", __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001217 goto fwd_msg_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001219 mutex_lock(&fwd_xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001220 fwd_xprt_info->xprt->write(pkt, pkt->length, fwd_xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001221 mutex_unlock(&fwd_xprt_info->tx_lock_lhb2);
1222fwd_msg_out:
1223 up_read(&rt_entry->lock_lha4);
1224 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001226 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227}
1228
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001229static int msm_ipc_router_send_remove_client(struct comm_mode_info *mode_info,
1230 uint32_t node_id, uint32_t port_id)
1231{
1232 union rr_control_msg msg;
1233 struct msm_ipc_router_xprt_info *tmp_xprt_info;
1234 int mode;
1235 void *xprt_info;
1236 int rc = 0;
1237
1238 if (!mode_info) {
1239 pr_err("%s: NULL mode_info\n", __func__);
1240 return -EINVAL;
1241 }
1242 mode = mode_info->mode;
1243 xprt_info = mode_info->xprt_info;
1244
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001245 memset(&msg, 0, sizeof(msg));
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001246 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT;
1247 msg.cli.node_id = node_id;
1248 msg.cli.port_id = port_id;
1249
1250 if ((mode == SINGLE_LINK_MODE) && xprt_info) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001251 down_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001252 list_for_each_entry(tmp_xprt_info, &xprt_info_list, list) {
1253 if (tmp_xprt_info != xprt_info)
1254 continue;
1255 msm_ipc_router_send_control_msg(tmp_xprt_info, &msg);
1256 break;
1257 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001258 up_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001259 } else if ((mode == SINGLE_LINK_MODE) && !xprt_info) {
1260 broadcast_ctl_msg_locally(&msg);
1261 } else if (mode == MULTI_LINK_MODE) {
1262 broadcast_ctl_msg(&msg);
1263 broadcast_ctl_msg_locally(&msg);
1264 } else if (mode != NULL_MODE) {
1265 pr_err("%s: Invalid mode(%d) + xprt_inf(%p) for %08x:%08x\n",
1266 __func__, mode, xprt_info, node_id, port_id);
1267 rc = -EINVAL;
1268 }
1269 return rc;
1270}
1271
1272static void update_comm_mode_info(struct comm_mode_info *mode_info,
1273 struct msm_ipc_router_xprt_info *xprt_info)
1274{
1275 if (!mode_info) {
1276 pr_err("%s: NULL mode_info\n", __func__);
1277 return;
1278 }
1279
1280 if (mode_info->mode == NULL_MODE) {
1281 mode_info->xprt_info = xprt_info;
1282 mode_info->mode = SINGLE_LINK_MODE;
1283 } else if (mode_info->mode == SINGLE_LINK_MODE &&
1284 mode_info->xprt_info != xprt_info) {
1285 mode_info->mode = MULTI_LINK_MODE;
1286 }
1287
1288 return;
1289}
1290
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001291static void cleanup_rmt_server(struct msm_ipc_router_xprt_info *xprt_info,
1292 struct msm_ipc_router_remote_port *rport_ptr)
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001293{
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001294 union rr_control_msg ctl;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001295 struct msm_ipc_server *server = rport_ptr->server;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001296
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001297 D("Remove server %08x:%08x - %08x:%08x",
1298 server->name.service, server->name.instance,
1299 rport_ptr->node_id, rport_ptr->port_id);
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001300 memset(&ctl, 0, sizeof(ctl));
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001301 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001302 ctl.srv.service = server->name.service;
1303 ctl.srv.instance = server->name.instance;
1304 ctl.srv.node_id = rport_ptr->node_id;
1305 ctl.srv.port_id = rport_ptr->port_id;
1306 relay_ctl_msg(xprt_info, &ctl);
1307 broadcast_ctl_msg_locally(&ctl);
1308 msm_ipc_router_destroy_server(server,
1309 rport_ptr->node_id, rport_ptr->port_id);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001310}
1311
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001312static void cleanup_rmt_ports(struct msm_ipc_router_xprt_info *xprt_info,
1313 struct msm_ipc_routing_table_entry *rt_entry)
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001314{
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001315 struct msm_ipc_router_remote_port *rport_ptr, *tmp_rport_ptr;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001316 union rr_control_msg ctl;
1317 int j;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001318
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001319 memset(&ctl, 0, sizeof(ctl));
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001320 for (j = 0; j < RP_HASH_SIZE; j++) {
1321 list_for_each_entry_safe(rport_ptr, tmp_rport_ptr,
1322 &rt_entry->remote_port_list[j], list) {
1323 list_del(&rport_ptr->list);
1324 mutex_lock(&rport_ptr->quota_lock_lhb2);
1325 msm_ipc_router_free_resume_tx_port(rport_ptr);
1326 mutex_unlock(&rport_ptr->quota_lock_lhb2);
1327
1328 if (rport_ptr->server)
1329 cleanup_rmt_server(xprt_info, rport_ptr);
1330
1331 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT;
1332 ctl.cli.node_id = rport_ptr->node_id;
1333 ctl.cli.port_id = rport_ptr->port_id;
1334 relay_ctl_msg(xprt_info, &ctl);
1335 broadcast_ctl_msg_locally(&ctl);
1336 kfree(rport_ptr);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001337 }
1338 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001339}
1340
1341static void msm_ipc_cleanup_routing_table(
1342 struct msm_ipc_router_xprt_info *xprt_info)
1343{
1344 int i;
1345 struct msm_ipc_routing_table_entry *rt_entry;
1346
1347 if (!xprt_info) {
1348 pr_err("%s: Invalid xprt_info\n", __func__);
1349 return;
1350 }
1351
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001352 down_write(&server_list_lock_lha2);
1353 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001354 for (i = 0; i < RT_HASH_SIZE; i++) {
1355 list_for_each_entry(rt_entry, &routing_table[i], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001356 down_write(&rt_entry->lock_lha4);
1357 if (rt_entry->xprt_info != xprt_info) {
1358 up_write(&rt_entry->lock_lha4);
1359 continue;
1360 }
1361 cleanup_rmt_ports(xprt_info, rt_entry);
1362 rt_entry->xprt_info = NULL;
1363 up_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001364 }
1365 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001366 up_write(&routing_table_lock_lha3);
1367 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001368}
1369
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001370/**
1371 * sync_sec_rule() - Synchrnoize the security rule into the server structure
1372 * @server: Server structure where the rule has to be synchronized.
1373 * @rule: Security tule to be synchronized.
1374 *
1375 * This function is used to update the server structure with the security
1376 * rule configured for the <service:instance> corresponding to that server.
1377 */
1378static void sync_sec_rule(struct msm_ipc_server *server, void *rule)
1379{
1380 struct msm_ipc_server_port *server_port;
1381 struct msm_ipc_router_remote_port *rport_ptr = NULL;
1382
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001383 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001384 list_for_each_entry(server_port, &server->server_port_list, list) {
1385 rport_ptr = msm_ipc_router_lookup_remote_port(
1386 server_port->server_addr.node_id,
1387 server_port->server_addr.port_id);
1388 if (!rport_ptr)
1389 continue;
1390 rport_ptr->sec_rule = rule;
1391 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001392 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001393 server->synced_sec_rule = 1;
1394}
1395
1396/**
1397 * msm_ipc_sync_sec_rule() - Sync the security rule to the service
1398 * @service: Service for which the rule has to be synchronized.
1399 * @instance: Instance for which the rule has to be synchronized.
1400 * @rule: Security rule to be synchronized.
1401 *
1402 * This function is used to syncrhonize the security rule with the server
1403 * hash table, if the user-space script configures the rule after the service
1404 * has come up. This function is used to synchronize the security rule to a
1405 * specific service and optionally a specific instance.
1406 */
1407void msm_ipc_sync_sec_rule(uint32_t service, uint32_t instance, void *rule)
1408{
1409 int key = (service & (SRV_HASH_SIZE - 1));
1410 struct msm_ipc_server *server;
1411
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001412 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001413 list_for_each_entry(server, &server_list[key], list) {
1414 if (server->name.service != service)
1415 continue;
1416
1417 if (server->name.instance != instance &&
1418 instance != ALL_INSTANCE)
1419 continue;
1420
1421 /*
1422 * If the rule applies to all instances and if the specific
1423 * instance of a service has a rule synchronized already,
1424 * do not apply the rule for that specific instance.
1425 */
1426 if (instance == ALL_INSTANCE && server->synced_sec_rule)
1427 continue;
1428
1429 sync_sec_rule(server, rule);
1430 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001431 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001432}
1433
1434/**
1435 * msm_ipc_sync_default_sec_rule() - Default security rule to all services
1436 * @rule: Security rule to be synchronized.
1437 *
1438 * This function is used to syncrhonize the security rule with the server
1439 * hash table, if the user-space script configures the rule after the service
1440 * has come up. This function is used to synchronize the security rule that
1441 * applies to all services, if the concerned service do not have any rule
1442 * defined.
1443 */
1444void msm_ipc_sync_default_sec_rule(void *rule)
1445{
1446 int key;
1447 struct msm_ipc_server *server;
1448
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001449 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001450 for (key = 0; key < SRV_HASH_SIZE; key++) {
1451 list_for_each_entry(server, &server_list[key], list) {
1452 if (server->synced_sec_rule)
1453 continue;
1454
1455 sync_sec_rule(server, rule);
1456 }
1457 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001458 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001459}
1460
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001461static int process_hello_msg(struct msm_ipc_router_xprt_info *xprt_info,
1462 struct rr_header *hdr)
1463{
1464 int i, rc = 0;
1465 union rr_control_msg ctl;
1466 struct msm_ipc_routing_table_entry *rt_entry;
1467
1468 if (!hdr)
1469 return -EINVAL;
1470
1471 RR("o HELLO NID %d\n", hdr->src_node_id);
1472
1473 xprt_info->remote_node_id = hdr->src_node_id;
1474 /*
1475 * Find the entry from Routing Table corresponding to Node ID.
1476 * Under SSR, an entry will be found. When the system boots up
1477 * for the 1st time, an entry will not be found and hence allocate
1478 * an entry. Update the entry with the Node ID that it corresponds
1479 * to and the XPRT through which it can be reached.
1480 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001481 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001482 rt_entry = lookup_routing_table(hdr->src_node_id);
1483 if (!rt_entry) {
1484 rt_entry = alloc_routing_table_entry(hdr->src_node_id);
1485 if (!rt_entry) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001486 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001487 pr_err("%s: rt_entry allocation failed\n", __func__);
1488 return -ENOMEM;
1489 }
1490 add_routing_table_entry(rt_entry);
1491 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001492 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001493 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1494 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001495 up_write(&rt_entry->lock_lha4);
1496 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001497
1498 /* Send a reply HELLO message */
1499 memset(&ctl, 0, sizeof(ctl));
1500 ctl.cmd = IPC_ROUTER_CTRL_CMD_HELLO;
1501 rc = msm_ipc_router_send_control_msg(xprt_info, &ctl);
1502 if (rc < 0) {
1503 pr_err("%s: Error sending reply HELLO message\n", __func__);
1504 return rc;
1505 }
1506 xprt_info->initialized = 1;
1507
1508 /*
1509 * Send list of servers from the local node and from nodes
1510 * outside the mesh network in which this XPRT is part of.
1511 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001512 down_read(&server_list_lock_lha2);
1513 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001514 for (i = 0; i < RT_HASH_SIZE; i++) {
1515 list_for_each_entry(rt_entry, &routing_table[i], list) {
1516 if ((rt_entry->node_id != IPC_ROUTER_NID_LOCAL) &&
Karthikeyan Ramasubramanian72ad5792013-01-30 14:17:57 -07001517 (!rt_entry->xprt_info ||
1518 (rt_entry->xprt_info->xprt->link_id ==
1519 xprt_info->xprt->link_id)))
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001520 continue;
1521 rc = msm_ipc_router_send_server_list(rt_entry->node_id,
1522 xprt_info);
1523 if (rc < 0) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001524 up_read(&routing_table_lock_lha3);
1525 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001526 return rc;
1527 }
1528 }
1529 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001530 up_read(&routing_table_lock_lha3);
1531 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001532 RR("HELLO message processed\n");
1533 return rc;
1534}
1535
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001536static int process_resume_tx_msg(union rr_control_msg *msg,
1537 struct rr_packet *pkt)
1538{
1539 struct msm_ipc_router_remote_port *rport_ptr;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001540 int ret = 0;
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001541
1542 RR("o RESUME_TX id=%d:%08x\n", msg->cli.node_id, msg->cli.port_id);
1543
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001544 down_read(&local_ports_lock_lha2);
1545 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001546 rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id,
1547 msg->cli.port_id);
1548 if (!rport_ptr) {
1549 pr_err("%s: Unable to resume client\n", __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001550 ret = -ENODEV;
1551 goto prtm_out;
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001552 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001553 mutex_lock(&rport_ptr->quota_lock_lhb2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001554 rport_ptr->tx_quota_cnt = 0;
1555 post_resume_tx(rport_ptr, pkt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001556 mutex_unlock(&rport_ptr->quota_lock_lhb2);
1557prtm_out:
1558 up_read(&routing_table_lock_lha3);
1559 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001560 return 0;
1561}
1562
1563static int process_new_server_msg(struct msm_ipc_router_xprt_info *xprt_info,
1564 union rr_control_msg *msg, struct rr_packet *pkt)
1565{
1566 struct msm_ipc_routing_table_entry *rt_entry;
1567 struct msm_ipc_server *server;
1568 struct msm_ipc_router_remote_port *rport_ptr;
1569
1570 if (msg->srv.instance == 0) {
1571 pr_err("%s: Server %08x create rejected, version = 0\n",
1572 __func__, msg->srv.service);
1573 return -EINVAL;
1574 }
1575
1576 RR("o NEW_SERVER id=%d:%08x service=%08x:%08x\n", msg->srv.node_id,
1577 msg->srv.port_id, msg->srv.service, msg->srv.instance);
1578 /*
1579 * Find the entry from Routing Table corresponding to Node ID.
1580 * Under SSR, an entry will be found. When the subsystem hosting
1581 * service is not adjacent, an entry will not be found and hence
1582 * allocate an entry. Update the entry with the Node ID that it
1583 * corresponds to and the XPRT through which it can be reached.
1584 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001585 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001586 rt_entry = lookup_routing_table(msg->srv.node_id);
1587 if (!rt_entry) {
1588 rt_entry = alloc_routing_table_entry(msg->srv.node_id);
1589 if (!rt_entry) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001590 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001591 pr_err("%s: rt_entry allocation failed\n", __func__);
1592 return -ENOMEM;
1593 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001594 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001595 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1596 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001597 up_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001598 add_routing_table_entry(rt_entry);
1599 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001600 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001601
1602 /*
1603 * If the service does not exist already in the database, create and
1604 * store the service info. Create a remote port structure in which
1605 * the service is hosted and cache the security rule for the service
1606 * in that remote port structure.
1607 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001608 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001609 server = msm_ipc_router_lookup_server(msg->srv.service,
1610 msg->srv.instance, msg->srv.node_id, msg->srv.port_id);
1611 if (!server) {
1612 server = msm_ipc_router_create_server(
1613 msg->srv.service, msg->srv.instance,
1614 msg->srv.node_id, msg->srv.port_id, xprt_info);
1615 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001616 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001617 pr_err("%s: Server Create failed\n", __func__);
1618 return -ENOMEM;
1619 }
1620
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001621 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001622 if (!msm_ipc_router_lookup_remote_port(
1623 msg->srv.node_id, msg->srv.port_id)) {
1624 rport_ptr = msm_ipc_router_create_remote_port(
1625 msg->srv.node_id, msg->srv.port_id);
1626 if (!rport_ptr) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001627 up_read(&routing_table_lock_lha3);
1628 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001629 return -ENOMEM;
1630 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001631 rport_ptr->server = server;
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001632 rport_ptr->sec_rule = msm_ipc_get_security_rule(
1633 msg->srv.service,
1634 msg->srv.instance);
1635 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001636 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001637 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001638 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001639
1640 /*
1641 * Relay the new server message to other subsystems that do not belong
1642 * to the cluster from which this message is received. Notify the
1643 * local clients waiting for this service.
1644 */
1645 relay_msg(xprt_info, pkt);
1646 post_control_ports(pkt);
1647 return 0;
1648}
1649
1650static int process_rmv_server_msg(struct msm_ipc_router_xprt_info *xprt_info,
1651 union rr_control_msg *msg, struct rr_packet *pkt)
1652{
1653 struct msm_ipc_server *server;
1654
1655 RR("o REMOVE_SERVER service=%08x:%d\n",
1656 msg->srv.service, msg->srv.instance);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001657 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001658 server = msm_ipc_router_lookup_server(msg->srv.service,
1659 msg->srv.instance, msg->srv.node_id, msg->srv.port_id);
1660 if (server) {
1661 msm_ipc_router_destroy_server(server, msg->srv.node_id,
1662 msg->srv.port_id);
1663 /*
1664 * Relay the new server message to other subsystems that do not
1665 * belong to the cluster from which this message is received.
1666 * Notify the local clients communicating with the service.
1667 */
1668 relay_msg(xprt_info, pkt);
1669 post_control_ports(pkt);
1670 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001671 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001672 return 0;
1673}
1674
1675static int process_rmv_client_msg(struct msm_ipc_router_xprt_info *xprt_info,
1676 union rr_control_msg *msg, struct rr_packet *pkt)
1677{
1678 struct msm_ipc_router_remote_port *rport_ptr;
1679
1680 RR("o REMOVE_CLIENT id=%d:%08x\n", msg->cli.node_id, msg->cli.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001681 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001682 rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id,
1683 msg->cli.port_id);
1684 if (rport_ptr)
1685 msm_ipc_router_destroy_remote_port(rport_ptr);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001686 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001687
1688 relay_msg(xprt_info, pkt);
1689 post_control_ports(pkt);
1690 return 0;
1691}
1692
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001693static int process_control_msg(struct msm_ipc_router_xprt_info *xprt_info,
1694 struct rr_packet *pkt)
1695{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696 union rr_control_msg *msg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001697 int rc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001698 struct sk_buff *temp_ptr;
1699 struct rr_header *hdr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001700
1701 if (pkt->length != (IPC_ROUTER_HDR_SIZE + sizeof(*msg))) {
1702 pr_err("%s: r2r msg size %d != %d\n", __func__, pkt->length,
1703 (IPC_ROUTER_HDR_SIZE + sizeof(*msg)));
1704 return -EINVAL;
1705 }
1706
1707 temp_ptr = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001708 if (!temp_ptr) {
1709 pr_err("%s: pkt_fragment_q is empty\n", __func__);
1710 return -EINVAL;
1711 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001712 hdr = (struct rr_header *)temp_ptr->data;
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001713 if (!hdr) {
1714 pr_err("%s: No data inside the skb\n", __func__);
1715 return -EINVAL;
1716 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001717 msg = (union rr_control_msg *)((char *)hdr + IPC_ROUTER_HDR_SIZE);
1718
1719 switch (msg->cmd) {
1720 case IPC_ROUTER_CTRL_CMD_HELLO:
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001721 rc = process_hello_msg(xprt_info, hdr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722 break;
1723 case IPC_ROUTER_CTRL_CMD_RESUME_TX:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001724 rc = process_resume_tx_msg(msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001725 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001726 case IPC_ROUTER_CTRL_CMD_NEW_SERVER:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001727 rc = process_new_server_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001728 break;
1729 case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001730 rc = process_rmv_server_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001731 break;
1732 case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001733 rc = process_rmv_client_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734 break;
1735 case IPC_ROUTER_CTRL_CMD_PING:
1736 /* No action needed for ping messages received */
1737 RR("o PING\n");
1738 break;
1739 default:
1740 RR("o UNKNOWN(%08x)\n", msg->cmd);
1741 rc = -ENOSYS;
1742 }
1743
1744 return rc;
1745}
1746
1747static void do_read_data(struct work_struct *work)
1748{
1749 struct rr_header *hdr;
1750 struct rr_packet *pkt = NULL;
1751 struct msm_ipc_port *port_ptr;
1752 struct sk_buff *head_skb;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001753 struct msm_ipc_router_remote_port *rport_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001754 uint32_t resume_tx, resume_tx_node_id, resume_tx_port_id;
1755
1756 struct msm_ipc_router_xprt_info *xprt_info =
1757 container_of(work,
1758 struct msm_ipc_router_xprt_info,
1759 read_data);
1760
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001761 while ((pkt = rr_read(xprt_info)) != NULL) {
1762 if (pkt->length < IPC_ROUTER_HDR_SIZE ||
1763 pkt->length > MAX_IPC_PKT_SIZE) {
1764 pr_err("%s: Invalid pkt length %d\n",
1765 __func__, pkt->length);
1766 goto fail_data;
1767 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001768
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001769 head_skb = skb_peek(pkt->pkt_fragment_q);
1770 if (!head_skb) {
1771 pr_err("%s: head_skb is invalid\n", __func__);
1772 goto fail_data;
1773 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001774
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001775 hdr = (struct rr_header *)(head_skb->data);
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -06001776 RAW("ver=%d type=%d src=%d:%08x crx=%d siz=%d dst=%d:%08x\n",
1777 hdr->version, hdr->type, hdr->src_node_id,
1778 hdr->src_port_id, hdr->confirm_rx, hdr->size,
1779 hdr->dst_node_id, hdr->dst_port_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001780
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001781 if (hdr->version != IPC_ROUTER_VERSION) {
1782 pr_err("version %d != %d\n",
1783 hdr->version, IPC_ROUTER_VERSION);
1784 goto fail_data;
1785 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001787 if ((hdr->dst_node_id != IPC_ROUTER_NID_LOCAL) &&
1788 ((hdr->type == IPC_ROUTER_CTRL_CMD_RESUME_TX) ||
1789 (hdr->type == IPC_ROUTER_CTRL_CMD_DATA))) {
1790 forward_msg(xprt_info, pkt);
1791 release_pkt(pkt);
1792 continue;
1793 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001794
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001795 if ((hdr->dst_port_id == IPC_ROUTER_ADDRESS) ||
1796 (hdr->type == IPC_ROUTER_CTRL_CMD_HELLO)) {
1797 process_control_msg(xprt_info, pkt);
1798 release_pkt(pkt);
1799 continue;
1800 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001801#if defined(CONFIG_MSM_SMD_LOGGING)
1802#if defined(DEBUG)
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001803 if (msm_ipc_router_debug_mask & SMEM_LOG) {
1804 smem_log_event((SMEM_LOG_PROC_ID_APPS |
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +05301805 SMEM_LOG_IPC_ROUTER_EVENT_BASE |
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001806 IPC_ROUTER_LOG_EVENT_RX),
1807 (hdr->src_node_id << 24) |
1808 (hdr->src_port_id & 0xffffff),
1809 (hdr->dst_node_id << 24) |
1810 (hdr->dst_port_id & 0xffffff),
1811 (hdr->type << 24) | (hdr->confirm_rx << 16) |
1812 (hdr->size & 0xffff));
1813 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001814#endif
1815#endif
1816
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001817 resume_tx = hdr->confirm_rx;
1818 resume_tx_node_id = hdr->dst_node_id;
1819 resume_tx_port_id = hdr->dst_port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001820
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001821 down_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001822 port_ptr = msm_ipc_router_lookup_local_port(hdr->dst_port_id);
1823 if (!port_ptr) {
1824 pr_err("%s: No local port id %08x\n", __func__,
1825 hdr->dst_port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001826 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001827 release_pkt(pkt);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001828 goto process_done;
1829 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001830
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001831 down_read(&routing_table_lock_lha3);
1832 rport_ptr = msm_ipc_router_lookup_remote_port(hdr->src_node_id,
1833 hdr->src_port_id);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001834 if (!rport_ptr) {
1835 rport_ptr = msm_ipc_router_create_remote_port(
1836 hdr->src_node_id,
1837 hdr->src_port_id);
1838 if (!rport_ptr) {
1839 pr_err("%s: Rmt Prt %08x:%08x create failed\n",
1840 __func__, hdr->src_node_id,
1841 hdr->src_port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001842 up_read(&routing_table_lock_lha3);
1843 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001844 goto process_done;
1845 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001846 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001847 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -06001848 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001849 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001850
1851process_done:
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001852 if (resume_tx) {
1853 union rr_control_msg msg;
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001854 memset(&msg, 0, sizeof(msg));
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001855 msg.cmd = IPC_ROUTER_CTRL_CMD_RESUME_TX;
1856 msg.cli.node_id = resume_tx_node_id;
1857 msg.cli.port_id = resume_tx_port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001859 RR("x RESUME_TX id=%d:%08x\n",
1860 msg.cli.node_id, msg.cli.port_id);
1861 msm_ipc_router_send_control_msg(xprt_info, &msg);
1862 }
1863
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001864 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001865 return;
1866
1867fail_data:
1868 release_pkt(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001869 pr_err("ipc_router has died\n");
1870}
1871
1872int msm_ipc_router_register_server(struct msm_ipc_port *port_ptr,
1873 struct msm_ipc_addr *name)
1874{
1875 struct msm_ipc_server *server;
1876 unsigned long flags;
1877 union rr_control_msg ctl;
1878
1879 if (!port_ptr || !name)
1880 return -EINVAL;
1881
1882 if (name->addrtype != MSM_IPC_ADDR_NAME)
1883 return -EINVAL;
1884
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001885 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886 server = msm_ipc_router_lookup_server(name->addr.port_name.service,
1887 name->addr.port_name.instance,
1888 IPC_ROUTER_NID_LOCAL,
1889 port_ptr->this_port.port_id);
1890 if (server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001891 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001892 pr_err("%s: Server already present\n", __func__);
1893 return -EINVAL;
1894 }
1895
1896 server = msm_ipc_router_create_server(name->addr.port_name.service,
1897 name->addr.port_name.instance,
1898 IPC_ROUTER_NID_LOCAL,
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001899 port_ptr->this_port.port_id,
1900 NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001901 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001902 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001903 pr_err("%s: Server Creation failed\n", __func__);
1904 return -EINVAL;
1905 }
1906
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001907 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER;
1909 ctl.srv.service = server->name.service;
1910 ctl.srv.instance = server->name.instance;
1911 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1912 ctl.srv.port_id = port_ptr->this_port.port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001913 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001914 broadcast_ctl_msg(&ctl);
1915 spin_lock_irqsave(&port_ptr->port_lock, flags);
1916 port_ptr->type = SERVER_PORT;
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001917 port_ptr->mode_info.mode = MULTI_LINK_MODE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001918 port_ptr->port_name.service = server->name.service;
1919 port_ptr->port_name.instance = server->name.instance;
1920 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1921 return 0;
1922}
1923
1924int msm_ipc_router_unregister_server(struct msm_ipc_port *port_ptr)
1925{
1926 struct msm_ipc_server *server;
1927 unsigned long flags;
1928 union rr_control_msg ctl;
1929
1930 if (!port_ptr)
1931 return -EINVAL;
1932
1933 if (port_ptr->type != SERVER_PORT) {
1934 pr_err("%s: Trying to unregister a non-server port\n",
1935 __func__);
1936 return -EINVAL;
1937 }
1938
1939 if (port_ptr->this_port.node_id != IPC_ROUTER_NID_LOCAL) {
1940 pr_err("%s: Trying to unregister a remote server locally\n",
1941 __func__);
1942 return -EINVAL;
1943 }
1944
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001945 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946 server = msm_ipc_router_lookup_server(port_ptr->port_name.service,
1947 port_ptr->port_name.instance,
1948 port_ptr->this_port.node_id,
1949 port_ptr->this_port.port_id);
1950 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001951 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952 pr_err("%s: Server lookup failed\n", __func__);
1953 return -ENODEV;
1954 }
1955
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001956 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001957 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
1958 ctl.srv.service = server->name.service;
1959 ctl.srv.instance = server->name.instance;
1960 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1961 ctl.srv.port_id = port_ptr->this_port.port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001962 msm_ipc_router_destroy_server(server, port_ptr->this_port.node_id,
1963 port_ptr->this_port.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001964 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06001965 broadcast_ctl_msg(&ctl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001966 spin_lock_irqsave(&port_ptr->port_lock, flags);
1967 port_ptr->type = CLIENT_PORT;
1968 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1969 return 0;
1970}
1971
1972static int loopback_data(struct msm_ipc_port *src,
1973 uint32_t port_id,
1974 struct sk_buff_head *data)
1975{
1976 struct sk_buff *head_skb;
1977 struct rr_header *hdr;
1978 struct msm_ipc_port *port_ptr;
1979 struct rr_packet *pkt;
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07001980 int ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001981
1982 if (!data) {
1983 pr_err("%s: Invalid pkt pointer\n", __func__);
1984 return -EINVAL;
1985 }
1986
1987 pkt = create_pkt(data);
1988 if (!pkt) {
1989 pr_err("%s: New pkt create failed\n", __func__);
1990 return -ENOMEM;
1991 }
1992
1993 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001994 if (!head_skb) {
1995 pr_err("%s: pkt_fragment_q is empty\n", __func__);
Brent Hronik0e83d3b2013-05-01 16:25:00 -06001996 release_pkt(pkt);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001997 return -EINVAL;
1998 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001999 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
2000 if (!hdr) {
2001 pr_err("%s: Prepend Header failed\n", __func__);
2002 release_pkt(pkt);
2003 return -ENOMEM;
2004 }
2005 hdr->version = IPC_ROUTER_VERSION;
2006 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2007 hdr->src_node_id = src->this_port.node_id;
2008 hdr->src_port_id = src->this_port.port_id;
2009 hdr->size = pkt->length;
2010 hdr->confirm_rx = 0;
2011 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
2012 hdr->dst_port_id = port_id;
2013 pkt->length += IPC_ROUTER_HDR_SIZE;
2014
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002015 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002016 port_ptr = msm_ipc_router_lookup_local_port(port_id);
2017 if (!port_ptr) {
2018 pr_err("%s: Local port %d not present\n", __func__, port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002019 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002020 release_pkt(pkt);
2021 return -ENODEV;
2022 }
2023
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07002024 ret_len = pkt->length;
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -06002025 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002026 update_comm_mode_info(&src->mode_info, NULL);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002027 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002028
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07002029 return ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002030}
2031
2032static int msm_ipc_router_write_pkt(struct msm_ipc_port *src,
2033 struct msm_ipc_router_remote_port *rport_ptr,
2034 struct rr_packet *pkt)
2035{
2036 struct sk_buff *head_skb;
2037 struct rr_header *hdr;
2038 struct msm_ipc_router_xprt_info *xprt_info;
2039 struct msm_ipc_routing_table_entry *rt_entry;
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302040 struct msm_ipc_resume_tx_port *resume_tx_port;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002041 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002042
2043 if (!rport_ptr || !src || !pkt)
2044 return -EINVAL;
2045
2046 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07002047 if (!head_skb) {
2048 pr_err("%s: pkt_fragment_q is empty\n", __func__);
2049 return -EINVAL;
2050 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002051 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
2052 if (!hdr) {
2053 pr_err("%s: Prepend Header failed\n", __func__);
2054 return -ENOMEM;
2055 }
2056 hdr->version = IPC_ROUTER_VERSION;
2057 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2058 hdr->src_node_id = src->this_port.node_id;
2059 hdr->src_port_id = src->this_port.port_id;
2060 hdr->size = pkt->length;
2061 hdr->confirm_rx = 0;
2062 hdr->dst_node_id = rport_ptr->node_id;
2063 hdr->dst_port_id = rport_ptr->port_id;
2064 pkt->length += IPC_ROUTER_HDR_SIZE;
2065
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002066 mutex_lock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302067 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA) {
2068 if (msm_ipc_router_lookup_resume_tx_port(
2069 rport_ptr, src->this_port.port_id)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002070 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302071 return -EAGAIN;
2072 }
2073 resume_tx_port =
2074 kzalloc(sizeof(struct msm_ipc_resume_tx_port),
2075 GFP_KERNEL);
2076 if (!resume_tx_port) {
2077 pr_err("%s: Resume_Tx port allocation failed\n",
2078 __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002079 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302080 return -ENOMEM;
2081 }
2082 INIT_LIST_HEAD(&resume_tx_port->list);
2083 resume_tx_port->port_id = src->this_port.port_id;
2084 resume_tx_port->node_id = src->this_port.node_id;
2085 list_add_tail(&resume_tx_port->list,
2086 &rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002087 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302088 return -EAGAIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002089 }
2090 rport_ptr->tx_quota_cnt++;
2091 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA)
2092 hdr->confirm_rx = 1;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002093 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002094
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002095 rt_entry = lookup_routing_table(hdr->dst_node_id);
2096 if (!rt_entry || !rt_entry->xprt_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002097 pr_err("%s: Remote node %d not up\n",
2098 __func__, hdr->dst_node_id);
2099 return -ENODEV;
2100 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002101 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002102 xprt_info = rt_entry->xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002103 mutex_lock(&xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002104 ret = xprt_info->xprt->write(pkt, pkt->length, xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002105 mutex_unlock(&xprt_info->tx_lock_lhb2);
2106 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002107
2108 if (ret < 0) {
2109 pr_err("%s: Write on XPRT failed\n", __func__);
2110 return ret;
2111 }
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002112 update_comm_mode_info(&src->mode_info, xprt_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002113
2114 RAW_HDR("[w rr_h] "
2115 "ver=%i,type=%s,src_nid=%08x,src_port_id=%08x,"
2116 "confirm_rx=%i,size=%3i,dst_pid=%08x,dst_cid=%08x\n",
2117 hdr->version, type_to_str(hdr->type),
2118 hdr->src_node_id, hdr->src_port_id,
2119 hdr->confirm_rx, hdr->size,
2120 hdr->dst_node_id, hdr->dst_port_id);
2121
2122#if defined(CONFIG_MSM_SMD_LOGGING)
2123#if defined(DEBUG)
2124 if (msm_ipc_router_debug_mask & SMEM_LOG) {
2125 smem_log_event((SMEM_LOG_PROC_ID_APPS |
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +05302126 SMEM_LOG_IPC_ROUTER_EVENT_BASE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002127 IPC_ROUTER_LOG_EVENT_TX),
2128 (hdr->src_node_id << 24) |
2129 (hdr->src_port_id & 0xffffff),
2130 (hdr->dst_node_id << 24) |
2131 (hdr->dst_port_id & 0xffffff),
2132 (hdr->type << 24) | (hdr->confirm_rx << 16) |
2133 (hdr->size & 0xffff));
2134 }
2135#endif
2136#endif
2137
2138 return pkt->length;
2139}
2140
2141int msm_ipc_router_send_to(struct msm_ipc_port *src,
2142 struct sk_buff_head *data,
2143 struct msm_ipc_addr *dest)
2144{
2145 uint32_t dst_node_id = 0, dst_port_id = 0;
2146 struct msm_ipc_server *server;
2147 struct msm_ipc_server_port *server_port;
2148 struct msm_ipc_router_remote_port *rport_ptr = NULL;
2149 struct rr_packet *pkt;
2150 int ret;
2151
2152 if (!src || !data || !dest) {
2153 pr_err("%s: Invalid Parameters\n", __func__);
2154 return -EINVAL;
2155 }
2156
2157 /* Resolve Address*/
2158 if (dest->addrtype == MSM_IPC_ADDR_ID) {
2159 dst_node_id = dest->addr.port_addr.node_id;
2160 dst_port_id = dest->addr.port_addr.port_id;
2161 } else if (dest->addrtype == MSM_IPC_ADDR_NAME) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002162 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002163 server = msm_ipc_router_lookup_server(
2164 dest->addr.port_name.service,
2165 dest->addr.port_name.instance,
2166 0, 0);
2167 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002168 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002169 pr_err("%s: Destination not reachable\n", __func__);
2170 return -ENODEV;
2171 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172 server_port = list_first_entry(&server->server_port_list,
2173 struct msm_ipc_server_port,
2174 list);
2175 dst_node_id = server_port->server_addr.node_id;
2176 dst_port_id = server_port->server_addr.port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002177 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 }
2179 if (dst_node_id == IPC_ROUTER_NID_LOCAL) {
2180 ret = loopback_data(src, dst_port_id, data);
2181 return ret;
2182 }
2183
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002184 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185 rport_ptr = msm_ipc_router_lookup_remote_port(dst_node_id,
2186 dst_port_id);
2187 if (!rport_ptr) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002188 up_read(&routing_table_lock_lha3);
Zaheerulla Meer2c515312013-05-10 15:51:28 +05302189 pr_err("%s: Remote port not found\n", __func__);
2190 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002191 }
2192
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06002193 if (src->check_send_permissions) {
2194 ret = src->check_send_permissions(rport_ptr->sec_rule);
2195 if (ret <= 0) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002196 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06002197 pr_err("%s: permission failure for %s\n",
2198 __func__, current->comm);
2199 return -EPERM;
2200 }
2201 }
2202
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203 pkt = create_pkt(data);
2204 if (!pkt) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002205 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002206 pr_err("%s: Pkt creation failed\n", __func__);
2207 return -ENOMEM;
2208 }
2209
2210 ret = msm_ipc_router_write_pkt(src, rport_ptr, pkt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002211 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002212 release_pkt(pkt);
2213
2214 return ret;
2215}
2216
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002217int msm_ipc_router_send_msg(struct msm_ipc_port *src,
2218 struct msm_ipc_addr *dest,
2219 void *data, unsigned int data_len)
2220{
2221 struct sk_buff_head *out_skb_head;
2222 int ret;
2223
2224 out_skb_head = msm_ipc_router_buf_to_skb(data, data_len);
2225 if (!out_skb_head) {
2226 pr_err("%s: SKB conversion failed\n", __func__);
2227 return -EFAULT;
2228 }
2229
2230 ret = msm_ipc_router_send_to(src, out_skb_head, dest);
Zaheerulla Meerc8400402013-05-08 19:27:27 +05302231 if (ret == -EAGAIN)
2232 return ret;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002233 if (ret < 0) {
2234 pr_err("%s: msm_ipc_router_send_to failed - ret: %d\n",
2235 __func__, ret);
2236 msm_ipc_router_free_skb(out_skb_head);
Zaheerulla Meerfc59a8a2013-06-26 22:39:00 +05302237 return ret;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002238 }
2239 return 0;
2240}
2241
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002242int msm_ipc_router_read(struct msm_ipc_port *port_ptr,
2243 struct sk_buff_head **data,
2244 size_t buf_len)
2245{
2246 struct rr_packet *pkt;
2247 int ret;
2248
2249 if (!port_ptr || !data)
2250 return -EINVAL;
2251
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002252 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002253 if (list_empty(&port_ptr->port_rx_q)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002254 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002255 return -EAGAIN;
2256 }
2257
2258 pkt = list_first_entry(&port_ptr->port_rx_q, struct rr_packet, list);
2259 if ((buf_len) && ((pkt->length - IPC_ROUTER_HDR_SIZE) > buf_len)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002260 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002261 return -ETOOSMALL;
2262 }
2263 list_del(&pkt->list);
2264 if (list_empty(&port_ptr->port_rx_q))
2265 wake_unlock(&port_ptr->port_rx_wake_lock);
2266 *data = pkt->pkt_fragment_q;
2267 ret = pkt->length;
2268 kfree(pkt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002269 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002270
2271 return ret;
2272}
2273
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302274/**
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302275 * msm_ipc_router_rx_data_wait() - Wait for new message destined to a local port.
2276 * @port_ptr: Pointer to the local port
2277 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2278 * > 0 timeout indicates the wait time.
2279 * 0 indicates that we do not wait.
2280 * @return: 0 if there are pending messages to read,
2281 * standard Linux error code otherwise.
2282 *
2283 * Checks for the availability of messages that are destined to a local port.
2284 * If no messages are present then waits as per @timeout.
2285 */
2286int msm_ipc_router_rx_data_wait(struct msm_ipc_port *port_ptr, long timeout)
2287{
2288 int ret = 0;
2289
2290 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2291 while (list_empty(&port_ptr->port_rx_q)) {
2292 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2293 if (timeout < 0) {
2294 ret = wait_event_interruptible(
2295 port_ptr->port_rx_wait_q,
2296 !list_empty(&port_ptr->port_rx_q));
2297 if (ret)
2298 return ret;
2299 } else if (timeout > 0) {
2300 timeout = wait_event_interruptible_timeout(
2301 port_ptr->port_rx_wait_q,
2302 !list_empty(&port_ptr->port_rx_q),
2303 timeout);
2304 if (timeout < 0)
2305 return -EFAULT;
2306 }
2307 if (timeout == 0)
2308 return -ENOMSG;
2309 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2310 }
2311 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2312
2313 return ret;
2314}
2315
2316/**
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302317 * msm_ipc_router_recv_from() - Recieve messages destined to a local port.
2318 * @port_ptr: Pointer to the local port
2319 * @data : Pointer to the socket buffer head
2320 * @src: Pointer to local port address
2321 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2322 * > 0 timeout indicates the wait time.
2323 * 0 indicates that we do not wait.
2324 * @return: = Number of bytes read(On successful read operation).
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302325 * = -ENOMSG (If there are no pending messages and timeout is 0).
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302326 * = -EINVAL (If either of the arguments, port_ptr or data is invalid)
2327 * = -EFAULT (If there are no pending messages when timeout is > 0
2328 * and the wait_event_interruptible_timeout has returned value > 0)
2329 * = -ERESTARTSYS (If there are no pending messages when timeout
2330 * is < 0 and wait_event_interruptible was interrupted by a signal)
2331 *
2332 * This function reads the messages that are destined for a local port. It
2333 * is used by modules that exist with-in the kernel and use IPC Router for
2334 * transport. The function checks if there are any messages that are already
2335 * received. If yes, it reads them, else it waits as per the timeout value.
2336 * On a successful read, the return value of the function indicates the number
2337 * of bytes that are read.
2338 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002339int msm_ipc_router_recv_from(struct msm_ipc_port *port_ptr,
2340 struct sk_buff_head **data,
2341 struct msm_ipc_addr *src,
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002342 long timeout)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002343{
2344 int ret, data_len, align_size;
2345 struct sk_buff *temp_skb;
2346 struct rr_header *hdr = NULL;
2347
2348 if (!port_ptr || !data) {
2349 pr_err("%s: Invalid pointers being passed\n", __func__);
2350 return -EINVAL;
2351 }
2352
2353 *data = NULL;
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302354
2355 ret = msm_ipc_router_rx_data_wait(port_ptr, timeout);
2356 if (ret)
2357 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002358
2359 ret = msm_ipc_router_read(port_ptr, data, 0);
2360 if (ret <= 0 || !(*data))
2361 return ret;
2362
2363 temp_skb = skb_peek(*data);
2364 hdr = (struct rr_header *)(temp_skb->data);
2365 if (src) {
2366 src->addrtype = MSM_IPC_ADDR_ID;
2367 src->addr.port_addr.node_id = hdr->src_node_id;
2368 src->addr.port_addr.port_id = hdr->src_port_id;
2369 }
2370
2371 data_len = hdr->size;
2372 skb_pull(temp_skb, IPC_ROUTER_HDR_SIZE);
2373 align_size = ALIGN_SIZE(data_len);
2374 if (align_size) {
2375 temp_skb = skb_peek_tail(*data);
2376 skb_trim(temp_skb, (temp_skb->len - align_size));
2377 }
2378 return data_len;
2379}
2380
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002381int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr,
2382 struct msm_ipc_addr *src,
2383 unsigned char **data,
2384 unsigned int *len)
2385{
2386 struct sk_buff_head *in_skb_head;
2387 int ret;
2388
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302389 ret = msm_ipc_router_recv_from(port_ptr, &in_skb_head, src, 0);
2390
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002391 if (ret < 0) {
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302392 if (ret != -ENOMSG)
2393 pr_err("%s: msm_ipc_router_recv_from failed - ret: %d\n",
2394 __func__, ret);
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002395 return ret;
2396 }
2397
2398 *data = msm_ipc_router_skb_to_buf(in_skb_head, ret);
2399 if (!(*data))
2400 pr_err("%s: Buf conversion failed\n", __func__);
2401
2402 *len = ret;
2403 msm_ipc_router_free_skb(in_skb_head);
2404 return 0;
2405}
2406
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002407struct msm_ipc_port *msm_ipc_router_create_port(
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002408 void (*notify)(unsigned event, void *priv),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002409 void *priv)
2410{
2411 struct msm_ipc_port *port_ptr;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002412 int ret;
2413
2414 ret = wait_for_completion_interruptible(&msm_ipc_local_router_up);
2415 if (ret < 0) {
2416 pr_err("%s: Error waiting for local router\n", __func__);
2417 return NULL;
2418 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002419
2420 port_ptr = msm_ipc_router_create_raw_port(NULL, notify, priv);
2421 if (!port_ptr)
2422 pr_err("%s: port_ptr alloc failed\n", __func__);
2423
2424 return port_ptr;
2425}
2426
2427int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr)
2428{
2429 union rr_control_msg msg;
2430 struct rr_packet *pkt, *temp_pkt;
2431 struct msm_ipc_server *server;
2432
2433 if (!port_ptr)
2434 return -EINVAL;
2435
2436 if (port_ptr->type == SERVER_PORT || port_ptr->type == CLIENT_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002437 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002438 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002439 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002440
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002441 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06002442 memset(&msg, 0, sizeof(msg));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002443 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
2444 msg.srv.service = port_ptr->port_name.service;
2445 msg.srv.instance = port_ptr->port_name.instance;
2446 msg.srv.node_id = port_ptr->this_port.node_id;
2447 msg.srv.port_id = port_ptr->this_port.port_id;
2448 RR("x REMOVE_SERVER Name=%d:%08x Id=%d:%08x\n",
2449 msg.srv.service, msg.srv.instance,
2450 msg.srv.node_id, msg.srv.port_id);
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002451 broadcast_ctl_msg(&msg);
2452 broadcast_ctl_msg_locally(&msg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002453 }
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002454
2455 /*
2456 * Server port could have been a client port earlier.
2457 * Send REMOVE_CLIENT message in either case.
2458 */
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002459 RR("x REMOVE_CLIENT id=%d:%08x\n",
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002460 port_ptr->this_port.node_id, port_ptr->this_port.port_id);
2461 msm_ipc_router_send_remove_client(&port_ptr->mode_info,
2462 port_ptr->this_port.node_id,
2463 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002464 } else if (port_ptr->type == CONTROL_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002465 down_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002466 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002467 up_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002468 } else if (port_ptr->type == IRSC_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002469 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002470 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002471 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002472 signal_irsc_completion();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002473 }
2474
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002475 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002476 list_for_each_entry_safe(pkt, temp_pkt, &port_ptr->port_rx_q, list) {
2477 list_del(&pkt->list);
2478 release_pkt(pkt);
2479 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002480 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002482 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002483 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002484 server = msm_ipc_router_lookup_server(
2485 port_ptr->port_name.service,
2486 port_ptr->port_name.instance,
2487 port_ptr->this_port.node_id,
2488 port_ptr->this_port.port_id);
2489 if (server)
2490 msm_ipc_router_destroy_server(server,
2491 port_ptr->this_port.node_id,
2492 port_ptr->this_port.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002493 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002494 }
2495
Karthikeyan Ramasubramaniandd8c3b52011-11-30 16:26:12 -07002496 wake_lock_destroy(&port_ptr->port_rx_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002497 kfree(port_ptr);
2498 return 0;
2499}
2500
2501int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr)
2502{
2503 struct rr_packet *pkt;
2504 int rc = 0;
2505
2506 if (!port_ptr)
2507 return -EINVAL;
2508
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002509 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002510 if (!list_empty(&port_ptr->port_rx_q)) {
2511 pkt = list_first_entry(&port_ptr->port_rx_q,
2512 struct rr_packet, list);
2513 rc = pkt->length;
2514 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002515 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002516
2517 return rc;
2518}
2519
2520int msm_ipc_router_bind_control_port(struct msm_ipc_port *port_ptr)
2521{
2522 if (!port_ptr)
2523 return -EINVAL;
2524
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002525 down_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002526 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002527 up_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002528 port_ptr->type = CONTROL_PORT;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002529 down_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002530 list_add_tail(&port_ptr->list, &control_ports);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002531 up_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002532
2533 return 0;
2534}
2535
2536int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name,
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002537 struct msm_ipc_server_info *srv_info,
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002538 int num_entries_in_array,
2539 uint32_t lookup_mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002540{
2541 struct msm_ipc_server *server;
2542 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002543 int key, i = 0; /*num_entries_found*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002544
2545 if (!srv_name) {
2546 pr_err("%s: Invalid srv_name\n", __func__);
2547 return -EINVAL;
2548 }
2549
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002550 if (num_entries_in_array && !srv_info) {
2551 pr_err("%s: srv_info NULL\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002552 return -EINVAL;
2553 }
2554
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002555 down_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002556 if (!lookup_mask)
2557 lookup_mask = 0xFFFFFFFF;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002558 key = (srv_name->service & (SRV_HASH_SIZE - 1));
2559 list_for_each_entry(server, &server_list[key], list) {
2560 if ((server->name.service != srv_name->service) ||
2561 ((server->name.instance & lookup_mask) !=
2562 srv_name->instance))
2563 continue;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002564
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002565 list_for_each_entry(server_port,
2566 &server->server_port_list, list) {
2567 if (i < num_entries_in_array) {
2568 srv_info[i].node_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002569 server_port->server_addr.node_id;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002570 srv_info[i].port_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002571 server_port->server_addr.port_id;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002572 srv_info[i].service = server->name.service;
2573 srv_info[i].instance = server->name.instance;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002574 }
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002575 i++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002576 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002577 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002578 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002579
2580 return i;
2581}
2582
2583int msm_ipc_router_close(void)
2584{
2585 struct msm_ipc_router_xprt_info *xprt_info, *tmp_xprt_info;
2586
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002587 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002588 list_for_each_entry_safe(xprt_info, tmp_xprt_info,
2589 &xprt_info_list, list) {
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002590 xprt_info->xprt->close(xprt_info->xprt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002591 list_del(&xprt_info->list);
2592 kfree(xprt_info);
2593 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002594 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002595 return 0;
2596}
2597
2598#if defined(CONFIG_DEBUG_FS)
2599static int dump_routing_table(char *buf, int max)
2600{
2601 int i = 0, j;
2602 struct msm_ipc_routing_table_entry *rt_entry;
2603
2604 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002605 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002606 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002607 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002608 i += scnprintf(buf + i, max - i,
2609 "Node Id: 0x%08x\n", rt_entry->node_id);
Karthikeyan Ramasubramanianc1a4e3a2012-09-10 16:10:24 -06002610 if (rt_entry->node_id == IPC_ROUTER_NID_LOCAL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002611 i += scnprintf(buf + i, max - i,
2612 "XPRT Name: Loopback\n");
2613 i += scnprintf(buf + i, max - i,
2614 "Next Hop: %d\n", rt_entry->node_id);
2615 } else {
2616 i += scnprintf(buf + i, max - i,
2617 "XPRT Name: %s\n",
2618 rt_entry->xprt_info->xprt->name);
2619 i += scnprintf(buf + i, max - i,
2620 "Next Hop: 0x%08x\n",
2621 rt_entry->xprt_info->remote_node_id);
2622 }
2623 i += scnprintf(buf + i, max - i, "\n");
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002624 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002625 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002626 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002627 }
2628
2629 return i;
2630}
2631
2632static int dump_xprt_info(char *buf, int max)
2633{
2634 int i = 0;
2635 struct msm_ipc_router_xprt_info *xprt_info;
2636
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002637 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002638 list_for_each_entry(xprt_info, &xprt_info_list, list) {
2639 i += scnprintf(buf + i, max - i, "XPRT Name: %s\n",
2640 xprt_info->xprt->name);
2641 i += scnprintf(buf + i, max - i, "Link Id: %d\n",
2642 xprt_info->xprt->link_id);
2643 i += scnprintf(buf + i, max - i, "Initialized: %s\n",
2644 (xprt_info->initialized ? "Y" : "N"));
2645 i += scnprintf(buf + i, max - i, "Remote Node Id: 0x%08x\n",
2646 xprt_info->remote_node_id);
2647 i += scnprintf(buf + i, max - i, "\n");
2648 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002649 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002650
2651 return i;
2652}
2653
2654static int dump_servers(char *buf, int max)
2655{
2656 int i = 0, j;
2657 struct msm_ipc_server *server;
2658 struct msm_ipc_server_port *server_port;
2659
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002660 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002661 for (j = 0; j < SRV_HASH_SIZE; j++) {
2662 list_for_each_entry(server, &server_list[j], list) {
2663 list_for_each_entry(server_port,
2664 &server->server_port_list,
2665 list) {
2666 i += scnprintf(buf + i, max - i, "Service: "
2667 "0x%08x\n", server->name.service);
2668 i += scnprintf(buf + i, max - i, "Instance: "
2669 "0x%08x\n", server->name.instance);
2670 i += scnprintf(buf + i, max - i,
2671 "Node_id: 0x%08x\n",
2672 server_port->server_addr.node_id);
2673 i += scnprintf(buf + i, max - i,
2674 "Port_id: 0x%08x\n",
2675 server_port->server_addr.port_id);
2676 i += scnprintf(buf + i, max - i, "\n");
2677 }
2678 }
2679 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002680 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002681
2682 return i;
2683}
2684
2685static int dump_remote_ports(char *buf, int max)
2686{
2687 int i = 0, j, k;
2688 struct msm_ipc_router_remote_port *rport_ptr;
2689 struct msm_ipc_routing_table_entry *rt_entry;
2690
2691 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002692 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002693 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002694 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002695 for (k = 0; k < RP_HASH_SIZE; k++) {
2696 list_for_each_entry(rport_ptr,
2697 &rt_entry->remote_port_list[k],
2698 list) {
2699 i += scnprintf(buf + i, max - i,
2700 "Node_id: 0x%08x\n",
2701 rport_ptr->node_id);
2702 i += scnprintf(buf + i, max - i,
2703 "Port_id: 0x%08x\n",
2704 rport_ptr->port_id);
2705 i += scnprintf(buf + i, max - i,
2706 "Quota_cnt: %d\n",
2707 rport_ptr->tx_quota_cnt);
2708 i += scnprintf(buf + i, max - i, "\n");
2709 }
2710 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002711 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002712 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002713 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002714 }
2715
2716 return i;
2717}
2718
2719static int dump_control_ports(char *buf, int max)
2720{
2721 int i = 0;
2722 struct msm_ipc_port *port_ptr;
2723
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002724 down_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002725 list_for_each_entry(port_ptr, &control_ports, list) {
2726 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2727 port_ptr->this_port.node_id);
2728 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2729 port_ptr->this_port.port_id);
2730 i += scnprintf(buf + i, max - i, "\n");
2731 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002732 up_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002733
2734 return i;
2735}
2736
2737static int dump_local_ports(char *buf, int max)
2738{
2739 int i = 0, j;
2740 unsigned long flags;
2741 struct msm_ipc_port *port_ptr;
2742
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002743 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002744 for (j = 0; j < LP_HASH_SIZE; j++) {
2745 list_for_each_entry(port_ptr, &local_ports[j], list) {
2746 spin_lock_irqsave(&port_ptr->port_lock, flags);
2747 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2748 port_ptr->this_port.node_id);
2749 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2750 port_ptr->this_port.port_id);
2751 i += scnprintf(buf + i, max - i, "# pkts tx'd %d\n",
2752 port_ptr->num_tx);
2753 i += scnprintf(buf + i, max - i, "# pkts rx'd %d\n",
2754 port_ptr->num_rx);
2755 i += scnprintf(buf + i, max - i, "# bytes tx'd %ld\n",
2756 port_ptr->num_tx_bytes);
2757 i += scnprintf(buf + i, max - i, "# bytes rx'd %ld\n",
2758 port_ptr->num_rx_bytes);
2759 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
2760 i += scnprintf(buf + i, max - i, "\n");
2761 }
2762 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002763 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002764
2765 return i;
2766}
2767
2768#define DEBUG_BUFMAX 4096
2769static char debug_buffer[DEBUG_BUFMAX];
2770
2771static ssize_t debug_read(struct file *file, char __user *buf,
2772 size_t count, loff_t *ppos)
2773{
2774 int (*fill)(char *buf, int max) = file->private_data;
2775 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
2776 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
2777}
2778
2779static int debug_open(struct inode *inode, struct file *file)
2780{
2781 file->private_data = inode->i_private;
2782 return 0;
2783}
2784
2785static const struct file_operations debug_ops = {
2786 .read = debug_read,
2787 .open = debug_open,
2788};
2789
2790static void debug_create(const char *name, mode_t mode,
2791 struct dentry *dent,
2792 int (*fill)(char *buf, int max))
2793{
2794 debugfs_create_file(name, mode, dent, fill, &debug_ops);
2795}
2796
2797static void debugfs_init(void)
2798{
2799 struct dentry *dent;
2800
2801 dent = debugfs_create_dir("msm_ipc_router", 0);
2802 if (IS_ERR(dent))
2803 return;
2804
2805 debug_create("dump_local_ports", 0444, dent,
2806 dump_local_ports);
2807 debug_create("dump_remote_ports", 0444, dent,
2808 dump_remote_ports);
2809 debug_create("dump_control_ports", 0444, dent,
2810 dump_control_ports);
2811 debug_create("dump_servers", 0444, dent,
2812 dump_servers);
2813 debug_create("dump_xprt_info", 0444, dent,
2814 dump_xprt_info);
2815 debug_create("dump_routing_table", 0444, dent,
2816 dump_routing_table);
2817}
2818
2819#else
2820static void debugfs_init(void) {}
2821#endif
2822
2823static int msm_ipc_router_add_xprt(struct msm_ipc_router_xprt *xprt)
2824{
2825 struct msm_ipc_router_xprt_info *xprt_info;
2826 struct msm_ipc_routing_table_entry *rt_entry;
2827
2828 xprt_info = kmalloc(sizeof(struct msm_ipc_router_xprt_info),
2829 GFP_KERNEL);
2830 if (!xprt_info)
2831 return -ENOMEM;
2832
2833 xprt_info->xprt = xprt;
2834 xprt_info->initialized = 0;
2835 xprt_info->remote_node_id = -1;
2836 INIT_LIST_HEAD(&xprt_info->pkt_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002837 mutex_init(&xprt_info->rx_lock_lhb2);
2838 mutex_init(&xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002839 wake_lock_init(&xprt_info->wakelock,
2840 WAKE_LOCK_SUSPEND, xprt->name);
2841 xprt_info->need_len = 0;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002842 xprt_info->abort_data_read = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002843 INIT_WORK(&xprt_info->read_data, do_read_data);
2844 INIT_LIST_HEAD(&xprt_info->list);
2845
2846 xprt_info->workqueue = create_singlethread_workqueue(xprt->name);
2847 if (!xprt_info->workqueue) {
2848 kfree(xprt_info);
2849 return -ENOMEM;
2850 }
2851
2852 if (!strcmp(xprt->name, "msm_ipc_router_loopback_xprt")) {
2853 xprt_info->remote_node_id = IPC_ROUTER_NID_LOCAL;
2854 xprt_info->initialized = 1;
2855 }
2856
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002857 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002858 list_add_tail(&xprt_info->list, &xprt_info_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002859 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002860
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002861 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002862 if (!routing_table_inited) {
2863 init_routing_table();
2864 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
2865 add_routing_table_entry(rt_entry);
2866 routing_table_inited = 1;
2867 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002868 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002869
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002870 xprt->priv = xprt_info;
2871
2872 return 0;
2873}
2874
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002875static void msm_ipc_router_remove_xprt(struct msm_ipc_router_xprt *xprt)
2876{
2877 struct msm_ipc_router_xprt_info *xprt_info;
2878
2879 if (xprt && xprt->priv) {
2880 xprt_info = xprt->priv;
2881
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002882 mutex_lock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002883 xprt_info->abort_data_read = 1;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002884 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002885
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002886 down_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002887 list_del(&xprt_info->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002888 up_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002889
2890 flush_workqueue(xprt_info->workqueue);
2891 destroy_workqueue(xprt_info->workqueue);
2892 wake_lock_destroy(&xprt_info->wakelock);
2893
2894 xprt->priv = 0;
2895 kfree(xprt_info);
2896 }
2897}
2898
2899
2900struct msm_ipc_router_xprt_work {
2901 struct msm_ipc_router_xprt *xprt;
2902 struct work_struct work;
2903};
2904
2905static void xprt_open_worker(struct work_struct *work)
2906{
2907 struct msm_ipc_router_xprt_work *xprt_work =
2908 container_of(work, struct msm_ipc_router_xprt_work, work);
2909
2910 msm_ipc_router_add_xprt(xprt_work->xprt);
2911 kfree(xprt_work);
2912}
2913
2914static void xprt_close_worker(struct work_struct *work)
2915{
2916 struct msm_ipc_router_xprt_work *xprt_work =
2917 container_of(work, struct msm_ipc_router_xprt_work, work);
2918
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002919 msm_ipc_cleanup_routing_table(xprt_work->xprt->priv);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002920 msm_ipc_router_remove_xprt(xprt_work->xprt);
Zaheerulla Meer35893a62013-06-19 16:54:44 +05302921 xprt_work->xprt->sft_close_done(xprt_work->xprt);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002922 kfree(xprt_work);
2923}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002924
2925void msm_ipc_router_xprt_notify(struct msm_ipc_router_xprt *xprt,
2926 unsigned event,
2927 void *data)
2928{
2929 struct msm_ipc_router_xprt_info *xprt_info = xprt->priv;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002930 struct msm_ipc_router_xprt_work *xprt_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002931 struct rr_packet *pkt;
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002932 unsigned long ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002933
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002934 if (!msm_ipc_router_workqueue) {
2935 ret = wait_for_completion_timeout(&msm_ipc_local_router_up,
2936 IPC_ROUTER_INIT_TIMEOUT);
2937 if (!ret || !msm_ipc_router_workqueue) {
2938 pr_err("%s: IPC Router not initialized\n", __func__);
2939 return;
2940 }
2941 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002942
2943 switch (event) {
2944 case IPC_ROUTER_XPRT_EVENT_OPEN:
2945 D("open event for '%s'\n", xprt->name);
2946 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
2947 GFP_ATOMIC);
Karthikeyan Ramasubramanianc51456c2013-05-16 15:51:29 -06002948 if (xprt_work) {
2949 xprt_work->xprt = xprt;
2950 INIT_WORK(&xprt_work->work, xprt_open_worker);
2951 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
2952 } else {
2953 pr_err("%s: malloc failure - Couldn't notify OPEN event",
2954 __func__);
2955 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002956 break;
2957
2958 case IPC_ROUTER_XPRT_EVENT_CLOSE:
2959 D("close event for '%s'\n", xprt->name);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002960 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
2961 GFP_ATOMIC);
Karthikeyan Ramasubramanianc51456c2013-05-16 15:51:29 -06002962 if (xprt_work) {
2963 xprt_work->xprt = xprt;
2964 INIT_WORK(&xprt_work->work, xprt_close_worker);
2965 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
2966 } else {
2967 pr_err("%s: malloc failure - Couldn't notify CLOSE event",
2968 __func__);
2969 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002970 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002971 }
2972
2973 if (!data)
2974 return;
2975
2976 while (!xprt_info) {
2977 msleep(100);
2978 xprt_info = xprt->priv;
2979 }
2980
2981 pkt = clone_pkt((struct rr_packet *)data);
2982 if (!pkt)
2983 return;
2984
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002985 mutex_lock(&xprt_info->rx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002986 list_add_tail(&pkt->list, &xprt_info->pkt_list);
2987 wake_lock(&xprt_info->wakelock);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002988 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06002989 queue_work(xprt_info->workqueue, &xprt_info->read_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002990}
2991
2992static int __init msm_ipc_router_init(void)
2993{
2994 int i, ret;
2995 struct msm_ipc_routing_table_entry *rt_entry;
2996
2997 msm_ipc_router_debug_mask |= SMEM_LOG;
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -06002998 ipc_rtr_log_ctxt = ipc_log_context_create(IPC_RTR_LOG_PAGES,
2999 "ipc_router");
3000 if (!ipc_rtr_log_ctxt)
3001 pr_err("%s: Unable to create IPC logging for IPC RTR",
3002 __func__);
3003
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003004 msm_ipc_router_workqueue =
3005 create_singlethread_workqueue("msm_ipc_router");
3006 if (!msm_ipc_router_workqueue)
3007 return -ENOMEM;
3008
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003009 debugfs_init();
3010
3011 for (i = 0; i < SRV_HASH_SIZE; i++)
3012 INIT_LIST_HEAD(&server_list[i]);
3013
3014 for (i = 0; i < LP_HASH_SIZE; i++)
3015 INIT_LIST_HEAD(&local_ports[i]);
3016
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003017 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003018 if (!routing_table_inited) {
3019 init_routing_table();
3020 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
3021 add_routing_table_entry(rt_entry);
3022 routing_table_inited = 1;
3023 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003024 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003025
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003026 ret = msm_ipc_router_init_sockets();
3027 if (ret < 0)
3028 pr_err("%s: Init sockets failed\n", __func__);
3029
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06003030 ret = msm_ipc_router_security_init();
3031 if (ret < 0)
3032 pr_err("%s: Security Init failed\n", __func__);
3033
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06003034 complete_all(&msm_ipc_local_router_up);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003035 return ret;
3036}
3037
3038module_init(msm_ipc_router_init);
3039MODULE_DESCRIPTION("MSM IPC Router");
3040MODULE_LICENSE("GPL v2");