blob: 5a69ea361a522031a3660a6f4007e61e47ceb363 [file] [log] [blame]
Karthikeyan Ramasubramanian5c568fb2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanianefc493b2012-07-12 10:25:49 -060037#include <mach/msm_ipc_router.h>
Karthikeyan Ramasubramanian2bfe8ec2013-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 Ramasubramanian5b502d3642012-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 Ramasubramanian2bfe8ec2013-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 Ramasubramanian2bfe8ec2013-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 Ramasubramanian2bfe8ec2013-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
98#define IPC_ROUTER_LOG_EVENT_ERROR 0x10
99#define IPC_ROUTER_LOG_EVENT_TX 0x11
100#define IPC_ROUTER_LOG_EVENT_RX 0x12
101
102static LIST_HEAD(control_ports);
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600107static DECLARE_RWSEM(local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanian4b88e5d2013-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 Ramasubramanianefc493b2012-07-12 10:25:49 -0600123 char pdev_name[32];
124 int next_pdev_id;
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -0600125 int synced_sec_rule;
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 Ramasubramanianefc493b2012-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 Meere3f6c3a2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600148 struct mutex quota_lock_lhb2;
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +0530149 struct list_head resume_tx_port_list;
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -0600150 void *sec_rule;
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanianefc493b2012-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 Ramasubramanian6dbb00f2013-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 Meer84d0bd562013-05-24 21:19:18 +0530467 void (*notify)(unsigned event, void *priv);
Karthikeyan Ramasubramanian6dbb00f2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600482 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Karthikeyan Ramasubramanian6dbb00f2013-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 Meer84d0bd562013-05-24 21:19:18 +0530486 notify = port_ptr->notify;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600487 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Zaheerulla Meer84d0bd562013-05-24 21:19:18 +0530488 if (notify)
489 notify(MSM_IPC_ROUTER_READ_CB, port_ptr->priv);
Karthikeyan Ramasubramanian6dbb00f2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600500 down_read(&control_ports_lock_lha5);
Karthikeyan Ramasubramanian6dbb00f2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanianefc493b2012-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian6396bdd2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian5b502d3642012-09-23 22:23:36 -0600657 rport_ptr->sec_rule = NULL;
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600660 mutex_init(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +0530661 INIT_LIST_HEAD(&rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Meere3f6c3a2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600676 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meere3f6c3a2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600701 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meere3f6c3a2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600726 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meere3f6c3a2013-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 Meere3f6c3a2013-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 Meere3f6c3a2013-04-17 01:16:47 +0530736 local_port =
737 msm_ipc_router_lookup_local_port(rtx_port->port_id);
Zaheerulla Meer51dc3a12013-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 Ramasubramanian6dbb00f2013-05-17 15:19:56 -0600742 post_pkt_to_port(local_port, pkt, 1);
Zaheerulla Meer51dc3a12013-05-08 19:27:27 +0530743 else
744 pr_err("%s: Local Port %d not Found",
745 __func__, rtx_port->port_id);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +0530746 list_del(&rtx_port->list);
747 kfree(rtx_port);
748 }
749}
750
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanianbb8306b2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600786 * Note1: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanianefc493b2012-07-12 10:25:49 -0600817static void dummy_release(struct device *dev)
818{
819}
820
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600834 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanianefc493b2012-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 Ramasubramanian5b502d3642012-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 Ramasubramanianefc493b2012-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 Ramasubramanianefc493b2012-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 Ramasubramanianefc493b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600900 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanianefc493b2012-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramaniana2b7fdb2012-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
1016 ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER;
1017
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018 for (i = 0; i < SRV_HASH_SIZE; i++) {
1019 list_for_each_entry(server, &server_list[i], list) {
1020 ctl.srv.service = server->name.service;
1021 ctl.srv.instance = server->name.instance;
1022 list_for_each_entry(server_port,
1023 &server->server_port_list, list) {
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001024 if (server_port->server_addr.node_id !=
1025 node_id)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026 continue;
1027
1028 ctl.srv.node_id =
1029 server_port->server_addr.node_id;
1030 ctl.srv.port_id =
1031 server_port->server_addr.port_id;
1032 msm_ipc_router_send_control_msg(xprt_info,
1033 &ctl);
1034 }
1035 }
1036 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037
1038 return 0;
1039}
1040
1041#if defined(DEBUG)
1042static char *type_to_str(int i)
1043{
1044 switch (i) {
1045 case IPC_ROUTER_CTRL_CMD_DATA:
1046 return "data ";
1047 case IPC_ROUTER_CTRL_CMD_HELLO:
1048 return "hello ";
1049 case IPC_ROUTER_CTRL_CMD_BYE:
1050 return "bye ";
1051 case IPC_ROUTER_CTRL_CMD_NEW_SERVER:
1052 return "new_srvr";
1053 case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER:
1054 return "rmv_srvr";
1055 case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT:
1056 return "rmv_clnt";
1057 case IPC_ROUTER_CTRL_CMD_RESUME_TX:
1058 return "resum_tx";
1059 case IPC_ROUTER_CTRL_CMD_EXIT:
1060 return "cmd_exit";
1061 default:
1062 return "invalid";
1063 }
1064}
1065#endif
1066
1067static int broadcast_ctl_msg_locally(union rr_control_msg *msg)
1068{
1069 struct rr_packet *pkt;
1070 struct sk_buff *ipc_rtr_pkt;
1071 struct rr_header *hdr;
1072 int pkt_size;
1073 void *data;
1074 struct sk_buff_head *pkt_fragment_q;
1075 int ret;
1076
1077 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
1078 if (!pkt) {
1079 pr_err("%s: pkt alloc failed\n", __func__);
1080 return -ENOMEM;
1081 }
1082
1083 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
1084 if (!pkt_fragment_q) {
1085 pr_err("%s: pkt_fragment_q alloc failed\n", __func__);
1086 kfree(pkt);
1087 return -ENOMEM;
1088 }
1089 skb_queue_head_init(pkt_fragment_q);
1090
1091 pkt_size = IPC_ROUTER_HDR_SIZE + sizeof(*msg);
1092 ipc_rtr_pkt = alloc_skb(pkt_size, GFP_KERNEL);
1093 if (!ipc_rtr_pkt) {
1094 pr_err("%s: ipc_rtr_pkt alloc failed\n", __func__);
1095 kfree(pkt_fragment_q);
1096 kfree(pkt);
1097 return -ENOMEM;
1098 }
1099
1100 skb_reserve(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
1101 data = skb_put(ipc_rtr_pkt, sizeof(*msg));
1102 memcpy(data, msg, sizeof(*msg));
1103 hdr = (struct rr_header *)skb_push(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
1104 if (!hdr) {
1105 pr_err("%s: skb_push failed\n", __func__);
1106 kfree_skb(ipc_rtr_pkt);
1107 kfree(pkt_fragment_q);
1108 kfree(pkt);
1109 return -ENOMEM;
1110 }
1111 hdr->version = IPC_ROUTER_VERSION;
1112 hdr->type = msg->cmd;
1113 hdr->src_node_id = IPC_ROUTER_NID_LOCAL;
1114 hdr->src_port_id = IPC_ROUTER_ADDRESS;
1115 hdr->confirm_rx = 0;
1116 hdr->size = sizeof(*msg);
1117 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
1118 hdr->dst_port_id = IPC_ROUTER_ADDRESS;
1119 skb_queue_tail(pkt_fragment_q, ipc_rtr_pkt);
1120 pkt->pkt_fragment_q = pkt_fragment_q;
1121 pkt->length = pkt_size;
1122
1123 ret = post_control_ports(pkt);
1124 release_pkt(pkt);
1125 return ret;
1126}
1127
1128static int broadcast_ctl_msg(union rr_control_msg *ctl)
1129{
1130 struct msm_ipc_router_xprt_info *xprt_info;
1131
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001132 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133 list_for_each_entry(xprt_info, &xprt_info_list, list) {
1134 msm_ipc_router_send_control_msg(xprt_info, ctl);
1135 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001136 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137
1138 return 0;
1139}
1140
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001141static int relay_ctl_msg(struct msm_ipc_router_xprt_info *xprt_info,
1142 union rr_control_msg *ctl)
1143{
1144 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1145
1146 if (!xprt_info || !ctl)
1147 return -EINVAL;
1148
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001149 down_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001150 list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) {
1151 if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id)
1152 msm_ipc_router_send_control_msg(fwd_xprt_info, ctl);
1153 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001154 up_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001155
1156 return 0;
1157}
1158
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159static int relay_msg(struct msm_ipc_router_xprt_info *xprt_info,
1160 struct rr_packet *pkt)
1161{
1162 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1163
1164 if (!xprt_info || !pkt)
1165 return -EINVAL;
1166
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001167 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168 list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001169 mutex_lock(&fwd_xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170 if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id)
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001171 fwd_xprt_info->xprt->write(pkt, pkt->length,
1172 fwd_xprt_info->xprt);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001173 mutex_unlock(&fwd_xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001175 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 return 0;
1177}
1178
1179static int forward_msg(struct msm_ipc_router_xprt_info *xprt_info,
1180 struct rr_packet *pkt)
1181{
1182 uint32_t dst_node_id;
1183 struct sk_buff *head_pkt;
1184 struct rr_header *hdr;
1185 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1186 struct msm_ipc_routing_table_entry *rt_entry;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001187 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001188
1189 if (!xprt_info || !pkt)
1190 return -EINVAL;
1191
1192 head_pkt = skb_peek(pkt->pkt_fragment_q);
1193 if (!head_pkt)
1194 return -EINVAL;
1195
1196 hdr = (struct rr_header *)head_pkt->data;
1197 dst_node_id = hdr->dst_node_id;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001198 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001199 rt_entry = lookup_routing_table(dst_node_id);
1200 if (!(rt_entry) || !(rt_entry->xprt_info)) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001201 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202 pr_err("%s: Routing table not initialized\n", __func__);
1203 return -ENODEV;
1204 }
1205
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001206 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 fwd_xprt_info = rt_entry->xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 if (xprt_info->remote_node_id == fwd_xprt_info->remote_node_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 pr_err("%s: Discarding Command to route back\n", __func__);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001210 ret = -EINVAL;
1211 goto fwd_msg_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001212 }
1213
1214 if (xprt_info->xprt->link_id == fwd_xprt_info->xprt->link_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215 pr_err("%s: DST in the same cluster\n", __func__);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001216 goto fwd_msg_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001218 mutex_lock(&fwd_xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001219 fwd_xprt_info->xprt->write(pkt, pkt->length, fwd_xprt_info->xprt);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001220 mutex_unlock(&fwd_xprt_info->tx_lock_lhb2);
1221fwd_msg_out:
1222 up_read(&rt_entry->lock_lha4);
1223 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001225 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226}
1227
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06001228static int msm_ipc_router_send_remove_client(struct comm_mode_info *mode_info,
1229 uint32_t node_id, uint32_t port_id)
1230{
1231 union rr_control_msg msg;
1232 struct msm_ipc_router_xprt_info *tmp_xprt_info;
1233 int mode;
1234 void *xprt_info;
1235 int rc = 0;
1236
1237 if (!mode_info) {
1238 pr_err("%s: NULL mode_info\n", __func__);
1239 return -EINVAL;
1240 }
1241 mode = mode_info->mode;
1242 xprt_info = mode_info->xprt_info;
1243
1244 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT;
1245 msg.cli.node_id = node_id;
1246 msg.cli.port_id = port_id;
1247
1248 if ((mode == SINGLE_LINK_MODE) && xprt_info) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001249 down_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06001250 list_for_each_entry(tmp_xprt_info, &xprt_info_list, list) {
1251 if (tmp_xprt_info != xprt_info)
1252 continue;
1253 msm_ipc_router_send_control_msg(tmp_xprt_info, &msg);
1254 break;
1255 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001256 up_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06001257 } else if ((mode == SINGLE_LINK_MODE) && !xprt_info) {
1258 broadcast_ctl_msg_locally(&msg);
1259 } else if (mode == MULTI_LINK_MODE) {
1260 broadcast_ctl_msg(&msg);
1261 broadcast_ctl_msg_locally(&msg);
1262 } else if (mode != NULL_MODE) {
1263 pr_err("%s: Invalid mode(%d) + xprt_inf(%p) for %08x:%08x\n",
1264 __func__, mode, xprt_info, node_id, port_id);
1265 rc = -EINVAL;
1266 }
1267 return rc;
1268}
1269
1270static void update_comm_mode_info(struct comm_mode_info *mode_info,
1271 struct msm_ipc_router_xprt_info *xprt_info)
1272{
1273 if (!mode_info) {
1274 pr_err("%s: NULL mode_info\n", __func__);
1275 return;
1276 }
1277
1278 if (mode_info->mode == NULL_MODE) {
1279 mode_info->xprt_info = xprt_info;
1280 mode_info->mode = SINGLE_LINK_MODE;
1281 } else if (mode_info->mode == SINGLE_LINK_MODE &&
1282 mode_info->xprt_info != xprt_info) {
1283 mode_info->mode = MULTI_LINK_MODE;
1284 }
1285
1286 return;
1287}
1288
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001289static void cleanup_rmt_server(struct msm_ipc_router_xprt_info *xprt_info,
1290 struct msm_ipc_router_remote_port *rport_ptr)
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001291{
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001292 union rr_control_msg ctl;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001293 struct msm_ipc_server *server = rport_ptr->server;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001294
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001295 D("Remove server %08x:%08x - %08x:%08x",
1296 server->name.service, server->name.instance,
1297 rport_ptr->node_id, rport_ptr->port_id);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001298 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001299 ctl.srv.service = server->name.service;
1300 ctl.srv.instance = server->name.instance;
1301 ctl.srv.node_id = rport_ptr->node_id;
1302 ctl.srv.port_id = rport_ptr->port_id;
1303 relay_ctl_msg(xprt_info, &ctl);
1304 broadcast_ctl_msg_locally(&ctl);
1305 msm_ipc_router_destroy_server(server,
1306 rport_ptr->node_id, rport_ptr->port_id);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001307}
1308
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001309static void cleanup_rmt_ports(struct msm_ipc_router_xprt_info *xprt_info,
1310 struct msm_ipc_routing_table_entry *rt_entry)
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001311{
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001312 struct msm_ipc_router_remote_port *rport_ptr, *tmp_rport_ptr;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001313 union rr_control_msg ctl;
1314 int j;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001315
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001316 for (j = 0; j < RP_HASH_SIZE; j++) {
1317 list_for_each_entry_safe(rport_ptr, tmp_rport_ptr,
1318 &rt_entry->remote_port_list[j], list) {
1319 list_del(&rport_ptr->list);
1320 mutex_lock(&rport_ptr->quota_lock_lhb2);
1321 msm_ipc_router_free_resume_tx_port(rport_ptr);
1322 mutex_unlock(&rport_ptr->quota_lock_lhb2);
1323
1324 if (rport_ptr->server)
1325 cleanup_rmt_server(xprt_info, rport_ptr);
1326
1327 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT;
1328 ctl.cli.node_id = rport_ptr->node_id;
1329 ctl.cli.port_id = rport_ptr->port_id;
1330 relay_ctl_msg(xprt_info, &ctl);
1331 broadcast_ctl_msg_locally(&ctl);
1332 kfree(rport_ptr);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001333 }
1334 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001335}
1336
1337static void msm_ipc_cleanup_routing_table(
1338 struct msm_ipc_router_xprt_info *xprt_info)
1339{
1340 int i;
1341 struct msm_ipc_routing_table_entry *rt_entry;
1342
1343 if (!xprt_info) {
1344 pr_err("%s: Invalid xprt_info\n", __func__);
1345 return;
1346 }
1347
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001348 down_write(&server_list_lock_lha2);
1349 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001350 for (i = 0; i < RT_HASH_SIZE; i++) {
1351 list_for_each_entry(rt_entry, &routing_table[i], list) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001352 down_write(&rt_entry->lock_lha4);
1353 if (rt_entry->xprt_info != xprt_info) {
1354 up_write(&rt_entry->lock_lha4);
1355 continue;
1356 }
1357 cleanup_rmt_ports(xprt_info, rt_entry);
1358 rt_entry->xprt_info = NULL;
1359 up_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001360 }
1361 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001362 up_write(&routing_table_lock_lha3);
1363 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001364}
1365
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06001366/**
1367 * sync_sec_rule() - Synchrnoize the security rule into the server structure
1368 * @server: Server structure where the rule has to be synchronized.
1369 * @rule: Security tule to be synchronized.
1370 *
1371 * This function is used to update the server structure with the security
1372 * rule configured for the <service:instance> corresponding to that server.
1373 */
1374static void sync_sec_rule(struct msm_ipc_server *server, void *rule)
1375{
1376 struct msm_ipc_server_port *server_port;
1377 struct msm_ipc_router_remote_port *rport_ptr = NULL;
1378
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001379 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06001380 list_for_each_entry(server_port, &server->server_port_list, list) {
1381 rport_ptr = msm_ipc_router_lookup_remote_port(
1382 server_port->server_addr.node_id,
1383 server_port->server_addr.port_id);
1384 if (!rport_ptr)
1385 continue;
1386 rport_ptr->sec_rule = rule;
1387 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001388 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06001389 server->synced_sec_rule = 1;
1390}
1391
1392/**
1393 * msm_ipc_sync_sec_rule() - Sync the security rule to the service
1394 * @service: Service for which the rule has to be synchronized.
1395 * @instance: Instance for which the rule has to be synchronized.
1396 * @rule: Security rule to be synchronized.
1397 *
1398 * This function is used to syncrhonize the security rule with the server
1399 * hash table, if the user-space script configures the rule after the service
1400 * has come up. This function is used to synchronize the security rule to a
1401 * specific service and optionally a specific instance.
1402 */
1403void msm_ipc_sync_sec_rule(uint32_t service, uint32_t instance, void *rule)
1404{
1405 int key = (service & (SRV_HASH_SIZE - 1));
1406 struct msm_ipc_server *server;
1407
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001408 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06001409 list_for_each_entry(server, &server_list[key], list) {
1410 if (server->name.service != service)
1411 continue;
1412
1413 if (server->name.instance != instance &&
1414 instance != ALL_INSTANCE)
1415 continue;
1416
1417 /*
1418 * If the rule applies to all instances and if the specific
1419 * instance of a service has a rule synchronized already,
1420 * do not apply the rule for that specific instance.
1421 */
1422 if (instance == ALL_INSTANCE && server->synced_sec_rule)
1423 continue;
1424
1425 sync_sec_rule(server, rule);
1426 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001427 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06001428}
1429
1430/**
1431 * msm_ipc_sync_default_sec_rule() - Default security rule to all services
1432 * @rule: Security rule to be synchronized.
1433 *
1434 * This function is used to syncrhonize the security rule with the server
1435 * hash table, if the user-space script configures the rule after the service
1436 * has come up. This function is used to synchronize the security rule that
1437 * applies to all services, if the concerned service do not have any rule
1438 * defined.
1439 */
1440void msm_ipc_sync_default_sec_rule(void *rule)
1441{
1442 int key;
1443 struct msm_ipc_server *server;
1444
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001445 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06001446 for (key = 0; key < SRV_HASH_SIZE; key++) {
1447 list_for_each_entry(server, &server_list[key], list) {
1448 if (server->synced_sec_rule)
1449 continue;
1450
1451 sync_sec_rule(server, rule);
1452 }
1453 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001454 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06001455}
1456
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001457static int process_hello_msg(struct msm_ipc_router_xprt_info *xprt_info,
1458 struct rr_header *hdr)
1459{
1460 int i, rc = 0;
1461 union rr_control_msg ctl;
1462 struct msm_ipc_routing_table_entry *rt_entry;
1463
1464 if (!hdr)
1465 return -EINVAL;
1466
1467 RR("o HELLO NID %d\n", hdr->src_node_id);
1468
1469 xprt_info->remote_node_id = hdr->src_node_id;
1470 /*
1471 * Find the entry from Routing Table corresponding to Node ID.
1472 * Under SSR, an entry will be found. When the system boots up
1473 * for the 1st time, an entry will not be found and hence allocate
1474 * an entry. Update the entry with the Node ID that it corresponds
1475 * to and the XPRT through which it can be reached.
1476 */
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001477 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001478 rt_entry = lookup_routing_table(hdr->src_node_id);
1479 if (!rt_entry) {
1480 rt_entry = alloc_routing_table_entry(hdr->src_node_id);
1481 if (!rt_entry) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001482 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001483 pr_err("%s: rt_entry allocation failed\n", __func__);
1484 return -ENOMEM;
1485 }
1486 add_routing_table_entry(rt_entry);
1487 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001488 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001489 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1490 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001491 up_write(&rt_entry->lock_lha4);
1492 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001493
1494 /* Send a reply HELLO message */
1495 memset(&ctl, 0, sizeof(ctl));
1496 ctl.cmd = IPC_ROUTER_CTRL_CMD_HELLO;
1497 rc = msm_ipc_router_send_control_msg(xprt_info, &ctl);
1498 if (rc < 0) {
1499 pr_err("%s: Error sending reply HELLO message\n", __func__);
1500 return rc;
1501 }
1502 xprt_info->initialized = 1;
1503
1504 /*
1505 * Send list of servers from the local node and from nodes
1506 * outside the mesh network in which this XPRT is part of.
1507 */
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001508 down_read(&server_list_lock_lha2);
1509 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001510 for (i = 0; i < RT_HASH_SIZE; i++) {
1511 list_for_each_entry(rt_entry, &routing_table[i], list) {
1512 if ((rt_entry->node_id != IPC_ROUTER_NID_LOCAL) &&
Karthikeyan Ramasubramanianc4c0aaa2013-01-30 14:17:57 -07001513 (!rt_entry->xprt_info ||
1514 (rt_entry->xprt_info->xprt->link_id ==
1515 xprt_info->xprt->link_id)))
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001516 continue;
1517 rc = msm_ipc_router_send_server_list(rt_entry->node_id,
1518 xprt_info);
1519 if (rc < 0) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001520 up_read(&routing_table_lock_lha3);
1521 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001522 return rc;
1523 }
1524 }
1525 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001526 up_read(&routing_table_lock_lha3);
1527 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001528 RR("HELLO message processed\n");
1529 return rc;
1530}
1531
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001532static int process_resume_tx_msg(union rr_control_msg *msg,
1533 struct rr_packet *pkt)
1534{
1535 struct msm_ipc_router_remote_port *rport_ptr;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001536 int ret = 0;
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001537
1538 RR("o RESUME_TX id=%d:%08x\n", msg->cli.node_id, msg->cli.port_id);
1539
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001540 down_read(&local_ports_lock_lha2);
1541 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001542 rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id,
1543 msg->cli.port_id);
1544 if (!rport_ptr) {
1545 pr_err("%s: Unable to resume client\n", __func__);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001546 ret = -ENODEV;
1547 goto prtm_out;
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001548 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001549 mutex_lock(&rport_ptr->quota_lock_lhb2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001550 rport_ptr->tx_quota_cnt = 0;
1551 post_resume_tx(rport_ptr, pkt);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001552 mutex_unlock(&rport_ptr->quota_lock_lhb2);
1553prtm_out:
1554 up_read(&routing_table_lock_lha3);
1555 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001556 return 0;
1557}
1558
1559static int process_new_server_msg(struct msm_ipc_router_xprt_info *xprt_info,
1560 union rr_control_msg *msg, struct rr_packet *pkt)
1561{
1562 struct msm_ipc_routing_table_entry *rt_entry;
1563 struct msm_ipc_server *server;
1564 struct msm_ipc_router_remote_port *rport_ptr;
1565
1566 if (msg->srv.instance == 0) {
1567 pr_err("%s: Server %08x create rejected, version = 0\n",
1568 __func__, msg->srv.service);
1569 return -EINVAL;
1570 }
1571
1572 RR("o NEW_SERVER id=%d:%08x service=%08x:%08x\n", msg->srv.node_id,
1573 msg->srv.port_id, msg->srv.service, msg->srv.instance);
1574 /*
1575 * Find the entry from Routing Table corresponding to Node ID.
1576 * Under SSR, an entry will be found. When the subsystem hosting
1577 * service is not adjacent, an entry will not be found and hence
1578 * allocate an entry. Update the entry with the Node ID that it
1579 * corresponds to and the XPRT through which it can be reached.
1580 */
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001581 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001582 rt_entry = lookup_routing_table(msg->srv.node_id);
1583 if (!rt_entry) {
1584 rt_entry = alloc_routing_table_entry(msg->srv.node_id);
1585 if (!rt_entry) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001586 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001587 pr_err("%s: rt_entry allocation failed\n", __func__);
1588 return -ENOMEM;
1589 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001590 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001591 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1592 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001593 up_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001594 add_routing_table_entry(rt_entry);
1595 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001596 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001597
1598 /*
1599 * If the service does not exist already in the database, create and
1600 * store the service info. Create a remote port structure in which
1601 * the service is hosted and cache the security rule for the service
1602 * in that remote port structure.
1603 */
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001604 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001605 server = msm_ipc_router_lookup_server(msg->srv.service,
1606 msg->srv.instance, msg->srv.node_id, msg->srv.port_id);
1607 if (!server) {
1608 server = msm_ipc_router_create_server(
1609 msg->srv.service, msg->srv.instance,
1610 msg->srv.node_id, msg->srv.port_id, xprt_info);
1611 if (!server) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001612 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001613 pr_err("%s: Server Create failed\n", __func__);
1614 return -ENOMEM;
1615 }
1616
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001617 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001618 if (!msm_ipc_router_lookup_remote_port(
1619 msg->srv.node_id, msg->srv.port_id)) {
1620 rport_ptr = msm_ipc_router_create_remote_port(
1621 msg->srv.node_id, msg->srv.port_id);
1622 if (!rport_ptr) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001623 up_read(&routing_table_lock_lha3);
1624 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001625 return -ENOMEM;
1626 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001627 rport_ptr->server = server;
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001628 rport_ptr->sec_rule = msm_ipc_get_security_rule(
1629 msg->srv.service,
1630 msg->srv.instance);
1631 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001632 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001633 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001634 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001635
1636 /*
1637 * Relay the new server message to other subsystems that do not belong
1638 * to the cluster from which this message is received. Notify the
1639 * local clients waiting for this service.
1640 */
1641 relay_msg(xprt_info, pkt);
1642 post_control_ports(pkt);
1643 return 0;
1644}
1645
1646static int process_rmv_server_msg(struct msm_ipc_router_xprt_info *xprt_info,
1647 union rr_control_msg *msg, struct rr_packet *pkt)
1648{
1649 struct msm_ipc_server *server;
1650
1651 RR("o REMOVE_SERVER service=%08x:%d\n",
1652 msg->srv.service, msg->srv.instance);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001653 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001654 server = msm_ipc_router_lookup_server(msg->srv.service,
1655 msg->srv.instance, msg->srv.node_id, msg->srv.port_id);
1656 if (server) {
1657 msm_ipc_router_destroy_server(server, msg->srv.node_id,
1658 msg->srv.port_id);
1659 /*
1660 * Relay the new server message to other subsystems that do not
1661 * belong to the cluster from which this message is received.
1662 * Notify the local clients communicating with the service.
1663 */
1664 relay_msg(xprt_info, pkt);
1665 post_control_ports(pkt);
1666 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001667 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001668 return 0;
1669}
1670
1671static int process_rmv_client_msg(struct msm_ipc_router_xprt_info *xprt_info,
1672 union rr_control_msg *msg, struct rr_packet *pkt)
1673{
1674 struct msm_ipc_router_remote_port *rport_ptr;
1675
1676 RR("o REMOVE_CLIENT id=%d:%08x\n", msg->cli.node_id, msg->cli.port_id);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001677 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001678 rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id,
1679 msg->cli.port_id);
1680 if (rport_ptr)
1681 msm_ipc_router_destroy_remote_port(rport_ptr);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001682 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001683
1684 relay_msg(xprt_info, pkt);
1685 post_control_ports(pkt);
1686 return 0;
1687}
1688
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001689static int process_control_msg(struct msm_ipc_router_xprt_info *xprt_info,
1690 struct rr_packet *pkt)
1691{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001692 union rr_control_msg *msg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001693 int rc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001694 struct sk_buff *temp_ptr;
1695 struct rr_header *hdr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696
1697 if (pkt->length != (IPC_ROUTER_HDR_SIZE + sizeof(*msg))) {
1698 pr_err("%s: r2r msg size %d != %d\n", __func__, pkt->length,
1699 (IPC_ROUTER_HDR_SIZE + sizeof(*msg)));
1700 return -EINVAL;
1701 }
1702
1703 temp_ptr = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001704 if (!temp_ptr) {
1705 pr_err("%s: pkt_fragment_q is empty\n", __func__);
1706 return -EINVAL;
1707 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001708 hdr = (struct rr_header *)temp_ptr->data;
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001709 if (!hdr) {
1710 pr_err("%s: No data inside the skb\n", __func__);
1711 return -EINVAL;
1712 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713 msg = (union rr_control_msg *)((char *)hdr + IPC_ROUTER_HDR_SIZE);
1714
1715 switch (msg->cmd) {
1716 case IPC_ROUTER_CTRL_CMD_HELLO:
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001717 rc = process_hello_msg(xprt_info, hdr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718 break;
1719 case IPC_ROUTER_CTRL_CMD_RESUME_TX:
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001720 rc = process_resume_tx_msg(msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001721 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722 case IPC_ROUTER_CTRL_CMD_NEW_SERVER:
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001723 rc = process_new_server_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001724 break;
1725 case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER:
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001726 rc = process_rmv_server_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001727 break;
1728 case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT:
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001729 rc = process_rmv_client_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001730 break;
1731 case IPC_ROUTER_CTRL_CMD_PING:
1732 /* No action needed for ping messages received */
1733 RR("o PING\n");
1734 break;
1735 default:
1736 RR("o UNKNOWN(%08x)\n", msg->cmd);
1737 rc = -ENOSYS;
1738 }
1739
1740 return rc;
1741}
1742
1743static void do_read_data(struct work_struct *work)
1744{
1745 struct rr_header *hdr;
1746 struct rr_packet *pkt = NULL;
1747 struct msm_ipc_port *port_ptr;
1748 struct sk_buff *head_skb;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001749 struct msm_ipc_router_remote_port *rport_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001750 uint32_t resume_tx, resume_tx_node_id, resume_tx_port_id;
1751
1752 struct msm_ipc_router_xprt_info *xprt_info =
1753 container_of(work,
1754 struct msm_ipc_router_xprt_info,
1755 read_data);
1756
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001757 while ((pkt = rr_read(xprt_info)) != NULL) {
1758 if (pkt->length < IPC_ROUTER_HDR_SIZE ||
1759 pkt->length > MAX_IPC_PKT_SIZE) {
1760 pr_err("%s: Invalid pkt length %d\n",
1761 __func__, pkt->length);
1762 goto fail_data;
1763 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001765 head_skb = skb_peek(pkt->pkt_fragment_q);
1766 if (!head_skb) {
1767 pr_err("%s: head_skb is invalid\n", __func__);
1768 goto fail_data;
1769 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001770
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001771 hdr = (struct rr_header *)(head_skb->data);
Karthikeyan Ramasubramanian2bfe8ec2013-03-22 10:47:20 -06001772 RAW("ver=%d type=%d src=%d:%08x crx=%d siz=%d dst=%d:%08x\n",
1773 hdr->version, hdr->type, hdr->src_node_id,
1774 hdr->src_port_id, hdr->confirm_rx, hdr->size,
1775 hdr->dst_node_id, hdr->dst_port_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001777 if (hdr->version != IPC_ROUTER_VERSION) {
1778 pr_err("version %d != %d\n",
1779 hdr->version, IPC_ROUTER_VERSION);
1780 goto fail_data;
1781 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001783 if ((hdr->dst_node_id != IPC_ROUTER_NID_LOCAL) &&
1784 ((hdr->type == IPC_ROUTER_CTRL_CMD_RESUME_TX) ||
1785 (hdr->type == IPC_ROUTER_CTRL_CMD_DATA))) {
1786 forward_msg(xprt_info, pkt);
1787 release_pkt(pkt);
1788 continue;
1789 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001791 if ((hdr->dst_port_id == IPC_ROUTER_ADDRESS) ||
1792 (hdr->type == IPC_ROUTER_CTRL_CMD_HELLO)) {
1793 process_control_msg(xprt_info, pkt);
1794 release_pkt(pkt);
1795 continue;
1796 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797#if defined(CONFIG_MSM_SMD_LOGGING)
1798#if defined(DEBUG)
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001799 if (msm_ipc_router_debug_mask & SMEM_LOG) {
1800 smem_log_event((SMEM_LOG_PROC_ID_APPS |
1801 SMEM_LOG_RPC_ROUTER_EVENT_BASE |
1802 IPC_ROUTER_LOG_EVENT_RX),
1803 (hdr->src_node_id << 24) |
1804 (hdr->src_port_id & 0xffffff),
1805 (hdr->dst_node_id << 24) |
1806 (hdr->dst_port_id & 0xffffff),
1807 (hdr->type << 24) | (hdr->confirm_rx << 16) |
1808 (hdr->size & 0xffff));
1809 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001810#endif
1811#endif
1812
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001813 resume_tx = hdr->confirm_rx;
1814 resume_tx_node_id = hdr->dst_node_id;
1815 resume_tx_port_id = hdr->dst_port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001816
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001817 down_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001818 port_ptr = msm_ipc_router_lookup_local_port(hdr->dst_port_id);
1819 if (!port_ptr) {
1820 pr_err("%s: No local port id %08x\n", __func__,
1821 hdr->dst_port_id);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001822 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001823 release_pkt(pkt);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001824 goto process_done;
1825 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001826
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001827 down_read(&routing_table_lock_lha3);
1828 rport_ptr = msm_ipc_router_lookup_remote_port(hdr->src_node_id,
1829 hdr->src_port_id);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001830 if (!rport_ptr) {
1831 rport_ptr = msm_ipc_router_create_remote_port(
1832 hdr->src_node_id,
1833 hdr->src_port_id);
1834 if (!rport_ptr) {
1835 pr_err("%s: Rmt Prt %08x:%08x create failed\n",
1836 __func__, hdr->src_node_id,
1837 hdr->src_port_id);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001838 up_read(&routing_table_lock_lha3);
1839 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001840 goto process_done;
1841 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001843 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian6dbb00f2013-05-17 15:19:56 -06001844 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001845 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001846
1847process_done:
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001848 if (resume_tx) {
1849 union rr_control_msg msg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001850
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001851 msg.cmd = IPC_ROUTER_CTRL_CMD_RESUME_TX;
1852 msg.cli.node_id = resume_tx_node_id;
1853 msg.cli.port_id = resume_tx_port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001854
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001855 RR("x RESUME_TX id=%d:%08x\n",
1856 msg.cli.node_id, msg.cli.port_id);
1857 msm_ipc_router_send_control_msg(xprt_info, &msg);
1858 }
1859
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001860 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001861 return;
1862
1863fail_data:
1864 release_pkt(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001865 pr_err("ipc_router has died\n");
1866}
1867
1868int msm_ipc_router_register_server(struct msm_ipc_port *port_ptr,
1869 struct msm_ipc_addr *name)
1870{
1871 struct msm_ipc_server *server;
1872 unsigned long flags;
1873 union rr_control_msg ctl;
1874
1875 if (!port_ptr || !name)
1876 return -EINVAL;
1877
1878 if (name->addrtype != MSM_IPC_ADDR_NAME)
1879 return -EINVAL;
1880
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001881 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882 server = msm_ipc_router_lookup_server(name->addr.port_name.service,
1883 name->addr.port_name.instance,
1884 IPC_ROUTER_NID_LOCAL,
1885 port_ptr->this_port.port_id);
1886 if (server) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001887 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001888 pr_err("%s: Server already present\n", __func__);
1889 return -EINVAL;
1890 }
1891
1892 server = msm_ipc_router_create_server(name->addr.port_name.service,
1893 name->addr.port_name.instance,
1894 IPC_ROUTER_NID_LOCAL,
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001895 port_ptr->this_port.port_id,
1896 NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897 if (!server) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001898 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001899 pr_err("%s: Server Creation failed\n", __func__);
1900 return -EINVAL;
1901 }
1902
1903 ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER;
1904 ctl.srv.service = server->name.service;
1905 ctl.srv.instance = server->name.instance;
1906 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1907 ctl.srv.port_id = port_ptr->this_port.port_id;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001908 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001909 broadcast_ctl_msg(&ctl);
1910 spin_lock_irqsave(&port_ptr->port_lock, flags);
1911 port_ptr->type = SERVER_PORT;
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06001912 port_ptr->mode_info.mode = MULTI_LINK_MODE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001913 port_ptr->port_name.service = server->name.service;
1914 port_ptr->port_name.instance = server->name.instance;
1915 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1916 return 0;
1917}
1918
1919int msm_ipc_router_unregister_server(struct msm_ipc_port *port_ptr)
1920{
1921 struct msm_ipc_server *server;
1922 unsigned long flags;
1923 union rr_control_msg ctl;
1924
1925 if (!port_ptr)
1926 return -EINVAL;
1927
1928 if (port_ptr->type != SERVER_PORT) {
1929 pr_err("%s: Trying to unregister a non-server port\n",
1930 __func__);
1931 return -EINVAL;
1932 }
1933
1934 if (port_ptr->this_port.node_id != IPC_ROUTER_NID_LOCAL) {
1935 pr_err("%s: Trying to unregister a remote server locally\n",
1936 __func__);
1937 return -EINVAL;
1938 }
1939
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001940 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001941 server = msm_ipc_router_lookup_server(port_ptr->port_name.service,
1942 port_ptr->port_name.instance,
1943 port_ptr->this_port.node_id,
1944 port_ptr->this_port.port_id);
1945 if (!server) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001946 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001947 pr_err("%s: Server lookup failed\n", __func__);
1948 return -ENODEV;
1949 }
1950
1951 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
1952 ctl.srv.service = server->name.service;
1953 ctl.srv.instance = server->name.instance;
1954 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1955 ctl.srv.port_id = port_ptr->this_port.port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956 msm_ipc_router_destroy_server(server, port_ptr->this_port.node_id,
1957 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001958 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06001959 broadcast_ctl_msg(&ctl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001960 spin_lock_irqsave(&port_ptr->port_lock, flags);
1961 port_ptr->type = CLIENT_PORT;
1962 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1963 return 0;
1964}
1965
1966static int loopback_data(struct msm_ipc_port *src,
1967 uint32_t port_id,
1968 struct sk_buff_head *data)
1969{
1970 struct sk_buff *head_skb;
1971 struct rr_header *hdr;
1972 struct msm_ipc_port *port_ptr;
1973 struct rr_packet *pkt;
Karthikeyan Ramasubramanianb0e23c52013-02-08 13:07:42 -07001974 int ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001975
1976 if (!data) {
1977 pr_err("%s: Invalid pkt pointer\n", __func__);
1978 return -EINVAL;
1979 }
1980
1981 pkt = create_pkt(data);
1982 if (!pkt) {
1983 pr_err("%s: New pkt create failed\n", __func__);
1984 return -ENOMEM;
1985 }
1986
1987 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001988 if (!head_skb) {
1989 pr_err("%s: pkt_fragment_q is empty\n", __func__);
Brent Hronik1c9a3d42013-04-17 15:10:40 -06001990 release_pkt(pkt);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001991 return -EINVAL;
1992 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001993 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
1994 if (!hdr) {
1995 pr_err("%s: Prepend Header failed\n", __func__);
1996 release_pkt(pkt);
1997 return -ENOMEM;
1998 }
1999 hdr->version = IPC_ROUTER_VERSION;
2000 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2001 hdr->src_node_id = src->this_port.node_id;
2002 hdr->src_port_id = src->this_port.port_id;
2003 hdr->size = pkt->length;
2004 hdr->confirm_rx = 0;
2005 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
2006 hdr->dst_port_id = port_id;
2007 pkt->length += IPC_ROUTER_HDR_SIZE;
2008
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002009 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002010 port_ptr = msm_ipc_router_lookup_local_port(port_id);
2011 if (!port_ptr) {
2012 pr_err("%s: Local port %d not present\n", __func__, port_id);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002013 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002014 release_pkt(pkt);
2015 return -ENODEV;
2016 }
2017
Karthikeyan Ramasubramanianb0e23c52013-02-08 13:07:42 -07002018 ret_len = pkt->length;
Karthikeyan Ramasubramanian6dbb00f2013-05-17 15:19:56 -06002019 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06002020 update_comm_mode_info(&src->mode_info, NULL);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002021 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002022
Karthikeyan Ramasubramanianb0e23c52013-02-08 13:07:42 -07002023 return ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002024}
2025
2026static int msm_ipc_router_write_pkt(struct msm_ipc_port *src,
2027 struct msm_ipc_router_remote_port *rport_ptr,
2028 struct rr_packet *pkt)
2029{
2030 struct sk_buff *head_skb;
2031 struct rr_header *hdr;
2032 struct msm_ipc_router_xprt_info *xprt_info;
2033 struct msm_ipc_routing_table_entry *rt_entry;
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302034 struct msm_ipc_resume_tx_port *resume_tx_port;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002036
2037 if (!rport_ptr || !src || !pkt)
2038 return -EINVAL;
2039
2040 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07002041 if (!head_skb) {
2042 pr_err("%s: pkt_fragment_q is empty\n", __func__);
2043 return -EINVAL;
2044 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002045 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
2046 if (!hdr) {
2047 pr_err("%s: Prepend Header failed\n", __func__);
2048 return -ENOMEM;
2049 }
2050 hdr->version = IPC_ROUTER_VERSION;
2051 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2052 hdr->src_node_id = src->this_port.node_id;
2053 hdr->src_port_id = src->this_port.port_id;
2054 hdr->size = pkt->length;
2055 hdr->confirm_rx = 0;
2056 hdr->dst_node_id = rport_ptr->node_id;
2057 hdr->dst_port_id = rport_ptr->port_id;
2058 pkt->length += IPC_ROUTER_HDR_SIZE;
2059
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002060 mutex_lock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302061 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA) {
2062 if (msm_ipc_router_lookup_resume_tx_port(
2063 rport_ptr, src->this_port.port_id)) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002064 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302065 return -EAGAIN;
2066 }
2067 resume_tx_port =
2068 kzalloc(sizeof(struct msm_ipc_resume_tx_port),
2069 GFP_KERNEL);
2070 if (!resume_tx_port) {
2071 pr_err("%s: Resume_Tx port allocation failed\n",
2072 __func__);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002073 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302074 return -ENOMEM;
2075 }
2076 INIT_LIST_HEAD(&resume_tx_port->list);
2077 resume_tx_port->port_id = src->this_port.port_id;
2078 resume_tx_port->node_id = src->this_port.node_id;
2079 list_add_tail(&resume_tx_port->list,
2080 &rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002081 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302082 return -EAGAIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002083 }
2084 rport_ptr->tx_quota_cnt++;
2085 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA)
2086 hdr->confirm_rx = 1;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002087 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002089 rt_entry = lookup_routing_table(hdr->dst_node_id);
2090 if (!rt_entry || !rt_entry->xprt_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002091 pr_err("%s: Remote node %d not up\n",
2092 __func__, hdr->dst_node_id);
2093 return -ENODEV;
2094 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002095 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002096 xprt_info = rt_entry->xprt_info;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002097 mutex_lock(&xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002098 ret = xprt_info->xprt->write(pkt, pkt->length, xprt_info->xprt);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002099 mutex_unlock(&xprt_info->tx_lock_lhb2);
2100 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002101
2102 if (ret < 0) {
2103 pr_err("%s: Write on XPRT failed\n", __func__);
2104 return ret;
2105 }
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06002106 update_comm_mode_info(&src->mode_info, xprt_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002107
2108 RAW_HDR("[w rr_h] "
2109 "ver=%i,type=%s,src_nid=%08x,src_port_id=%08x,"
2110 "confirm_rx=%i,size=%3i,dst_pid=%08x,dst_cid=%08x\n",
2111 hdr->version, type_to_str(hdr->type),
2112 hdr->src_node_id, hdr->src_port_id,
2113 hdr->confirm_rx, hdr->size,
2114 hdr->dst_node_id, hdr->dst_port_id);
2115
2116#if defined(CONFIG_MSM_SMD_LOGGING)
2117#if defined(DEBUG)
2118 if (msm_ipc_router_debug_mask & SMEM_LOG) {
2119 smem_log_event((SMEM_LOG_PROC_ID_APPS |
2120 SMEM_LOG_RPC_ROUTER_EVENT_BASE |
2121 IPC_ROUTER_LOG_EVENT_TX),
2122 (hdr->src_node_id << 24) |
2123 (hdr->src_port_id & 0xffffff),
2124 (hdr->dst_node_id << 24) |
2125 (hdr->dst_port_id & 0xffffff),
2126 (hdr->type << 24) | (hdr->confirm_rx << 16) |
2127 (hdr->size & 0xffff));
2128 }
2129#endif
2130#endif
2131
2132 return pkt->length;
2133}
2134
2135int msm_ipc_router_send_to(struct msm_ipc_port *src,
2136 struct sk_buff_head *data,
2137 struct msm_ipc_addr *dest)
2138{
2139 uint32_t dst_node_id = 0, dst_port_id = 0;
2140 struct msm_ipc_server *server;
2141 struct msm_ipc_server_port *server_port;
2142 struct msm_ipc_router_remote_port *rport_ptr = NULL;
2143 struct rr_packet *pkt;
2144 int ret;
2145
2146 if (!src || !data || !dest) {
2147 pr_err("%s: Invalid Parameters\n", __func__);
2148 return -EINVAL;
2149 }
2150
2151 /* Resolve Address*/
2152 if (dest->addrtype == MSM_IPC_ADDR_ID) {
2153 dst_node_id = dest->addr.port_addr.node_id;
2154 dst_port_id = dest->addr.port_addr.port_id;
2155 } else if (dest->addrtype == MSM_IPC_ADDR_NAME) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002156 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157 server = msm_ipc_router_lookup_server(
2158 dest->addr.port_name.service,
2159 dest->addr.port_name.instance,
2160 0, 0);
2161 if (!server) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002162 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002163 pr_err("%s: Destination not reachable\n", __func__);
2164 return -ENODEV;
2165 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002166 server_port = list_first_entry(&server->server_port_list,
2167 struct msm_ipc_server_port,
2168 list);
2169 dst_node_id = server_port->server_addr.node_id;
2170 dst_port_id = server_port->server_addr.port_id;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002171 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172 }
2173 if (dst_node_id == IPC_ROUTER_NID_LOCAL) {
2174 ret = loopback_data(src, dst_port_id, data);
2175 return ret;
2176 }
2177
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002178 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002179 rport_ptr = msm_ipc_router_lookup_remote_port(dst_node_id,
2180 dst_port_id);
2181 if (!rport_ptr) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002182 up_read(&routing_table_lock_lha3);
Zaheerulla Meer3d8b0a02013-05-10 15:51:28 +05302183 pr_err("%s: Remote port not found\n", __func__);
2184 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185 }
2186
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06002187 if (src->check_send_permissions) {
2188 ret = src->check_send_permissions(rport_ptr->sec_rule);
2189 if (ret <= 0) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002190 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06002191 pr_err("%s: permission failure for %s\n",
2192 __func__, current->comm);
2193 return -EPERM;
2194 }
2195 }
2196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 pkt = create_pkt(data);
2198 if (!pkt) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002199 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002200 pr_err("%s: Pkt creation failed\n", __func__);
2201 return -ENOMEM;
2202 }
2203
2204 ret = msm_ipc_router_write_pkt(src, rport_ptr, pkt);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002205 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002206 release_pkt(pkt);
2207
2208 return ret;
2209}
2210
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002211int msm_ipc_router_send_msg(struct msm_ipc_port *src,
2212 struct msm_ipc_addr *dest,
2213 void *data, unsigned int data_len)
2214{
2215 struct sk_buff_head *out_skb_head;
2216 int ret;
2217
2218 out_skb_head = msm_ipc_router_buf_to_skb(data, data_len);
2219 if (!out_skb_head) {
2220 pr_err("%s: SKB conversion failed\n", __func__);
2221 return -EFAULT;
2222 }
2223
2224 ret = msm_ipc_router_send_to(src, out_skb_head, dest);
Zaheerulla Meer51dc3a12013-05-08 19:27:27 +05302225 if (ret == -EAGAIN)
2226 return ret;
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002227 if (ret < 0) {
2228 pr_err("%s: msm_ipc_router_send_to failed - ret: %d\n",
2229 __func__, ret);
2230 msm_ipc_router_free_skb(out_skb_head);
Zaheerulla Meer2f1eb072013-06-26 22:39:00 +05302231 return ret;
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002232 }
2233 return 0;
2234}
2235
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002236int msm_ipc_router_read(struct msm_ipc_port *port_ptr,
2237 struct sk_buff_head **data,
2238 size_t buf_len)
2239{
2240 struct rr_packet *pkt;
2241 int ret;
2242
2243 if (!port_ptr || !data)
2244 return -EINVAL;
2245
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002246 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002247 if (list_empty(&port_ptr->port_rx_q)) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002248 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002249 return -EAGAIN;
2250 }
2251
2252 pkt = list_first_entry(&port_ptr->port_rx_q, struct rr_packet, list);
2253 if ((buf_len) && ((pkt->length - IPC_ROUTER_HDR_SIZE) > buf_len)) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002254 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002255 return -ETOOSMALL;
2256 }
2257 list_del(&pkt->list);
2258 if (list_empty(&port_ptr->port_rx_q))
2259 wake_unlock(&port_ptr->port_rx_wake_lock);
2260 *data = pkt->pkt_fragment_q;
2261 ret = pkt->length;
2262 kfree(pkt);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002263 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002264
2265 return ret;
2266}
2267
Zaheerulla Meerabb0eba2013-05-22 23:31:59 +05302268/**
2269 * msm_ipc_router_recv_from() - Recieve messages destined to a local port.
2270 * @port_ptr: Pointer to the local port
2271 * @data : Pointer to the socket buffer head
2272 * @src: Pointer to local port address
2273 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2274 * > 0 timeout indicates the wait time.
2275 * 0 indicates that we do not wait.
2276 * @return: = Number of bytes read(On successful read operation).
2277 * = 0 (If there are no pending messages and timeout is 0).
2278 * = -EINVAL (If either of the arguments, port_ptr or data is invalid)
2279 * = -EFAULT (If there are no pending messages when timeout is > 0
2280 * and the wait_event_interruptible_timeout has returned value > 0)
2281 * = -ERESTARTSYS (If there are no pending messages when timeout
2282 * is < 0 and wait_event_interruptible was interrupted by a signal)
2283 *
2284 * This function reads the messages that are destined for a local port. It
2285 * is used by modules that exist with-in the kernel and use IPC Router for
2286 * transport. The function checks if there are any messages that are already
2287 * received. If yes, it reads them, else it waits as per the timeout value.
2288 * On a successful read, the return value of the function indicates the number
2289 * of bytes that are read.
2290 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002291int msm_ipc_router_recv_from(struct msm_ipc_port *port_ptr,
2292 struct sk_buff_head **data,
2293 struct msm_ipc_addr *src,
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002294 long timeout)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002295{
2296 int ret, data_len, align_size;
2297 struct sk_buff *temp_skb;
2298 struct rr_header *hdr = NULL;
2299
2300 if (!port_ptr || !data) {
2301 pr_err("%s: Invalid pointers being passed\n", __func__);
2302 return -EINVAL;
2303 }
2304
2305 *data = NULL;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002306 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002307 while (list_empty(&port_ptr->port_rx_q)) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002308 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002309 if (timeout < 0) {
2310 ret = wait_event_interruptible(
2311 port_ptr->port_rx_wait_q,
2312 !list_empty(&port_ptr->port_rx_q));
2313 if (ret)
2314 return ret;
2315 } else if (timeout > 0) {
2316 timeout = wait_event_interruptible_timeout(
2317 port_ptr->port_rx_wait_q,
2318 !list_empty(&port_ptr->port_rx_q),
2319 timeout);
2320 if (timeout < 0)
2321 return -EFAULT;
2322 }
2323 if (timeout == 0)
Zaheerulla Meerabb0eba2013-05-22 23:31:59 +05302324 return 0;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002325 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002326 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002327 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002328
2329 ret = msm_ipc_router_read(port_ptr, data, 0);
2330 if (ret <= 0 || !(*data))
2331 return ret;
2332
2333 temp_skb = skb_peek(*data);
2334 hdr = (struct rr_header *)(temp_skb->data);
2335 if (src) {
2336 src->addrtype = MSM_IPC_ADDR_ID;
2337 src->addr.port_addr.node_id = hdr->src_node_id;
2338 src->addr.port_addr.port_id = hdr->src_port_id;
2339 }
2340
2341 data_len = hdr->size;
2342 skb_pull(temp_skb, IPC_ROUTER_HDR_SIZE);
2343 align_size = ALIGN_SIZE(data_len);
2344 if (align_size) {
2345 temp_skb = skb_peek_tail(*data);
2346 skb_trim(temp_skb, (temp_skb->len - align_size));
2347 }
2348 return data_len;
2349}
2350
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002351int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr,
2352 struct msm_ipc_addr *src,
2353 unsigned char **data,
2354 unsigned int *len)
2355{
2356 struct sk_buff_head *in_skb_head;
2357 int ret;
2358
Zaheerulla Meerabb0eba2013-05-22 23:31:59 +05302359 ret = msm_ipc_router_recv_from(port_ptr, &in_skb_head, src, 0);
2360
2361 if (ret == 0)
2362 return -ENOMSG;
2363
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002364 if (ret < 0) {
2365 pr_err("%s: msm_ipc_router_recv_from failed - ret: %d\n",
2366 __func__, ret);
2367 return ret;
2368 }
2369
2370 *data = msm_ipc_router_skb_to_buf(in_skb_head, ret);
2371 if (!(*data))
2372 pr_err("%s: Buf conversion failed\n", __func__);
2373
2374 *len = ret;
2375 msm_ipc_router_free_skb(in_skb_head);
2376 return 0;
2377}
2378
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002379struct msm_ipc_port *msm_ipc_router_create_port(
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002380 void (*notify)(unsigned event, void *priv),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002381 void *priv)
2382{
2383 struct msm_ipc_port *port_ptr;
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002384 int ret;
2385
2386 ret = wait_for_completion_interruptible(&msm_ipc_local_router_up);
2387 if (ret < 0) {
2388 pr_err("%s: Error waiting for local router\n", __func__);
2389 return NULL;
2390 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002391
2392 port_ptr = msm_ipc_router_create_raw_port(NULL, notify, priv);
2393 if (!port_ptr)
2394 pr_err("%s: port_ptr alloc failed\n", __func__);
2395
2396 return port_ptr;
2397}
2398
2399int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr)
2400{
2401 union rr_control_msg msg;
2402 struct rr_packet *pkt, *temp_pkt;
2403 struct msm_ipc_server *server;
2404
2405 if (!port_ptr)
2406 return -EINVAL;
2407
2408 if (port_ptr->type == SERVER_PORT || port_ptr->type == CLIENT_PORT) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002409 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002410 list_del(&port_ptr->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002411 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413 if (port_ptr->type == SERVER_PORT) {
2414 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
2415 msg.srv.service = port_ptr->port_name.service;
2416 msg.srv.instance = port_ptr->port_name.instance;
2417 msg.srv.node_id = port_ptr->this_port.node_id;
2418 msg.srv.port_id = port_ptr->this_port.port_id;
2419 RR("x REMOVE_SERVER Name=%d:%08x Id=%d:%08x\n",
2420 msg.srv.service, msg.srv.instance,
2421 msg.srv.node_id, msg.srv.port_id);
Karthikeyan Ramasubramanian5c568fb2013-01-10 17:09:13 -07002422 broadcast_ctl_msg(&msg);
2423 broadcast_ctl_msg_locally(&msg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002424 }
Karthikeyan Ramasubramanian5c568fb2013-01-10 17:09:13 -07002425
2426 /*
2427 * Server port could have been a client port earlier.
2428 * Send REMOVE_CLIENT message in either case.
2429 */
Karthikeyan Ramasubramanian5c568fb2013-01-10 17:09:13 -07002430 RR("x REMOVE_CLIENT id=%d:%08x\n",
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06002431 port_ptr->this_port.node_id, port_ptr->this_port.port_id);
2432 msm_ipc_router_send_remove_client(&port_ptr->mode_info,
2433 port_ptr->this_port.node_id,
2434 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002435 } else if (port_ptr->type == CONTROL_PORT) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002436 down_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002437 list_del(&port_ptr->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002438 up_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramanianf7a4b6e2013-01-16 09:00:28 -07002439 } else if (port_ptr->type == IRSC_PORT) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002440 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianf7a4b6e2013-01-16 09:00:28 -07002441 list_del(&port_ptr->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002442 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianf7a4b6e2013-01-16 09:00:28 -07002443 signal_irsc_completion();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002444 }
2445
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002446 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002447 list_for_each_entry_safe(pkt, temp_pkt, &port_ptr->port_rx_q, list) {
2448 list_del(&pkt->list);
2449 release_pkt(pkt);
2450 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002451 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002452
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002453 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002454 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002455 server = msm_ipc_router_lookup_server(
2456 port_ptr->port_name.service,
2457 port_ptr->port_name.instance,
2458 port_ptr->this_port.node_id,
2459 port_ptr->this_port.port_id);
2460 if (server)
2461 msm_ipc_router_destroy_server(server,
2462 port_ptr->this_port.node_id,
2463 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002464 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002465 }
2466
Karthikeyan Ramasubramaniandd8c3b52011-11-30 16:26:12 -07002467 wake_lock_destroy(&port_ptr->port_rx_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468 kfree(port_ptr);
2469 return 0;
2470}
2471
2472int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr)
2473{
2474 struct rr_packet *pkt;
2475 int rc = 0;
2476
2477 if (!port_ptr)
2478 return -EINVAL;
2479
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002480 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481 if (!list_empty(&port_ptr->port_rx_q)) {
2482 pkt = list_first_entry(&port_ptr->port_rx_q,
2483 struct rr_packet, list);
2484 rc = pkt->length;
2485 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002486 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487
2488 return rc;
2489}
2490
2491int msm_ipc_router_bind_control_port(struct msm_ipc_port *port_ptr)
2492{
2493 if (!port_ptr)
2494 return -EINVAL;
2495
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002496 down_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002497 list_del(&port_ptr->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002498 up_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002499 port_ptr->type = CONTROL_PORT;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002500 down_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002501 list_add_tail(&port_ptr->list, &control_ports);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002502 up_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002503
2504 return 0;
2505}
2506
2507int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name,
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002508 struct msm_ipc_server_info *srv_info,
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002509 int num_entries_in_array,
2510 uint32_t lookup_mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002511{
2512 struct msm_ipc_server *server;
2513 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002514 int key, i = 0; /*num_entries_found*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002515
2516 if (!srv_name) {
2517 pr_err("%s: Invalid srv_name\n", __func__);
2518 return -EINVAL;
2519 }
2520
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002521 if (num_entries_in_array && !srv_info) {
2522 pr_err("%s: srv_info NULL\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002523 return -EINVAL;
2524 }
2525
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002526 down_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002527 if (!lookup_mask)
2528 lookup_mask = 0xFFFFFFFF;
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002529 key = (srv_name->service & (SRV_HASH_SIZE - 1));
2530 list_for_each_entry(server, &server_list[key], list) {
2531 if ((server->name.service != srv_name->service) ||
2532 ((server->name.instance & lookup_mask) !=
2533 srv_name->instance))
2534 continue;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002535
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002536 list_for_each_entry(server_port,
2537 &server->server_port_list, list) {
2538 if (i < num_entries_in_array) {
2539 srv_info[i].node_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002540 server_port->server_addr.node_id;
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002541 srv_info[i].port_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002542 server_port->server_addr.port_id;
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002543 srv_info[i].service = server->name.service;
2544 srv_info[i].instance = server->name.instance;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002545 }
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002546 i++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002547 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002548 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002549 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002550
2551 return i;
2552}
2553
2554int msm_ipc_router_close(void)
2555{
2556 struct msm_ipc_router_xprt_info *xprt_info, *tmp_xprt_info;
2557
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002558 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002559 list_for_each_entry_safe(xprt_info, tmp_xprt_info,
2560 &xprt_info_list, list) {
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002561 xprt_info->xprt->close(xprt_info->xprt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002562 list_del(&xprt_info->list);
2563 kfree(xprt_info);
2564 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002565 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002566 return 0;
2567}
2568
2569#if defined(CONFIG_DEBUG_FS)
2570static int dump_routing_table(char *buf, int max)
2571{
2572 int i = 0, j;
2573 struct msm_ipc_routing_table_entry *rt_entry;
2574
2575 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002576 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002577 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002578 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002579 i += scnprintf(buf + i, max - i,
2580 "Node Id: 0x%08x\n", rt_entry->node_id);
Karthikeyan Ramasubramanianbddeec72012-09-10 16:10:24 -06002581 if (rt_entry->node_id == IPC_ROUTER_NID_LOCAL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002582 i += scnprintf(buf + i, max - i,
2583 "XPRT Name: Loopback\n");
2584 i += scnprintf(buf + i, max - i,
2585 "Next Hop: %d\n", rt_entry->node_id);
2586 } else {
2587 i += scnprintf(buf + i, max - i,
2588 "XPRT Name: %s\n",
2589 rt_entry->xprt_info->xprt->name);
2590 i += scnprintf(buf + i, max - i,
2591 "Next Hop: 0x%08x\n",
2592 rt_entry->xprt_info->remote_node_id);
2593 }
2594 i += scnprintf(buf + i, max - i, "\n");
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002595 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002596 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002597 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002598 }
2599
2600 return i;
2601}
2602
2603static int dump_xprt_info(char *buf, int max)
2604{
2605 int i = 0;
2606 struct msm_ipc_router_xprt_info *xprt_info;
2607
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002608 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002609 list_for_each_entry(xprt_info, &xprt_info_list, list) {
2610 i += scnprintf(buf + i, max - i, "XPRT Name: %s\n",
2611 xprt_info->xprt->name);
2612 i += scnprintf(buf + i, max - i, "Link Id: %d\n",
2613 xprt_info->xprt->link_id);
2614 i += scnprintf(buf + i, max - i, "Initialized: %s\n",
2615 (xprt_info->initialized ? "Y" : "N"));
2616 i += scnprintf(buf + i, max - i, "Remote Node Id: 0x%08x\n",
2617 xprt_info->remote_node_id);
2618 i += scnprintf(buf + i, max - i, "\n");
2619 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002620 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002621
2622 return i;
2623}
2624
2625static int dump_servers(char *buf, int max)
2626{
2627 int i = 0, j;
2628 struct msm_ipc_server *server;
2629 struct msm_ipc_server_port *server_port;
2630
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002631 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002632 for (j = 0; j < SRV_HASH_SIZE; j++) {
2633 list_for_each_entry(server, &server_list[j], list) {
2634 list_for_each_entry(server_port,
2635 &server->server_port_list,
2636 list) {
2637 i += scnprintf(buf + i, max - i, "Service: "
2638 "0x%08x\n", server->name.service);
2639 i += scnprintf(buf + i, max - i, "Instance: "
2640 "0x%08x\n", server->name.instance);
2641 i += scnprintf(buf + i, max - i,
2642 "Node_id: 0x%08x\n",
2643 server_port->server_addr.node_id);
2644 i += scnprintf(buf + i, max - i,
2645 "Port_id: 0x%08x\n",
2646 server_port->server_addr.port_id);
2647 i += scnprintf(buf + i, max - i, "\n");
2648 }
2649 }
2650 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002651 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002652
2653 return i;
2654}
2655
2656static int dump_remote_ports(char *buf, int max)
2657{
2658 int i = 0, j, k;
2659 struct msm_ipc_router_remote_port *rport_ptr;
2660 struct msm_ipc_routing_table_entry *rt_entry;
2661
2662 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002663 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002664 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002665 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002666 for (k = 0; k < RP_HASH_SIZE; k++) {
2667 list_for_each_entry(rport_ptr,
2668 &rt_entry->remote_port_list[k],
2669 list) {
2670 i += scnprintf(buf + i, max - i,
2671 "Node_id: 0x%08x\n",
2672 rport_ptr->node_id);
2673 i += scnprintf(buf + i, max - i,
2674 "Port_id: 0x%08x\n",
2675 rport_ptr->port_id);
2676 i += scnprintf(buf + i, max - i,
2677 "Quota_cnt: %d\n",
2678 rport_ptr->tx_quota_cnt);
2679 i += scnprintf(buf + i, max - i, "\n");
2680 }
2681 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002682 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002683 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002684 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002685 }
2686
2687 return i;
2688}
2689
2690static int dump_control_ports(char *buf, int max)
2691{
2692 int i = 0;
2693 struct msm_ipc_port *port_ptr;
2694
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002695 down_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002696 list_for_each_entry(port_ptr, &control_ports, list) {
2697 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2698 port_ptr->this_port.node_id);
2699 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2700 port_ptr->this_port.port_id);
2701 i += scnprintf(buf + i, max - i, "\n");
2702 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002703 up_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002704
2705 return i;
2706}
2707
2708static int dump_local_ports(char *buf, int max)
2709{
2710 int i = 0, j;
2711 unsigned long flags;
2712 struct msm_ipc_port *port_ptr;
2713
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002714 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002715 for (j = 0; j < LP_HASH_SIZE; j++) {
2716 list_for_each_entry(port_ptr, &local_ports[j], list) {
2717 spin_lock_irqsave(&port_ptr->port_lock, flags);
2718 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2719 port_ptr->this_port.node_id);
2720 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2721 port_ptr->this_port.port_id);
2722 i += scnprintf(buf + i, max - i, "# pkts tx'd %d\n",
2723 port_ptr->num_tx);
2724 i += scnprintf(buf + i, max - i, "# pkts rx'd %d\n",
2725 port_ptr->num_rx);
2726 i += scnprintf(buf + i, max - i, "# bytes tx'd %ld\n",
2727 port_ptr->num_tx_bytes);
2728 i += scnprintf(buf + i, max - i, "# bytes rx'd %ld\n",
2729 port_ptr->num_rx_bytes);
2730 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
2731 i += scnprintf(buf + i, max - i, "\n");
2732 }
2733 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002734 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002735
2736 return i;
2737}
2738
2739#define DEBUG_BUFMAX 4096
2740static char debug_buffer[DEBUG_BUFMAX];
2741
2742static ssize_t debug_read(struct file *file, char __user *buf,
2743 size_t count, loff_t *ppos)
2744{
2745 int (*fill)(char *buf, int max) = file->private_data;
2746 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
2747 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
2748}
2749
2750static int debug_open(struct inode *inode, struct file *file)
2751{
2752 file->private_data = inode->i_private;
2753 return 0;
2754}
2755
2756static const struct file_operations debug_ops = {
2757 .read = debug_read,
2758 .open = debug_open,
2759};
2760
2761static void debug_create(const char *name, mode_t mode,
2762 struct dentry *dent,
2763 int (*fill)(char *buf, int max))
2764{
2765 debugfs_create_file(name, mode, dent, fill, &debug_ops);
2766}
2767
2768static void debugfs_init(void)
2769{
2770 struct dentry *dent;
2771
2772 dent = debugfs_create_dir("msm_ipc_router", 0);
2773 if (IS_ERR(dent))
2774 return;
2775
2776 debug_create("dump_local_ports", 0444, dent,
2777 dump_local_ports);
2778 debug_create("dump_remote_ports", 0444, dent,
2779 dump_remote_ports);
2780 debug_create("dump_control_ports", 0444, dent,
2781 dump_control_ports);
2782 debug_create("dump_servers", 0444, dent,
2783 dump_servers);
2784 debug_create("dump_xprt_info", 0444, dent,
2785 dump_xprt_info);
2786 debug_create("dump_routing_table", 0444, dent,
2787 dump_routing_table);
2788}
2789
2790#else
2791static void debugfs_init(void) {}
2792#endif
2793
2794static int msm_ipc_router_add_xprt(struct msm_ipc_router_xprt *xprt)
2795{
2796 struct msm_ipc_router_xprt_info *xprt_info;
2797 struct msm_ipc_routing_table_entry *rt_entry;
2798
2799 xprt_info = kmalloc(sizeof(struct msm_ipc_router_xprt_info),
2800 GFP_KERNEL);
2801 if (!xprt_info)
2802 return -ENOMEM;
2803
2804 xprt_info->xprt = xprt;
2805 xprt_info->initialized = 0;
2806 xprt_info->remote_node_id = -1;
2807 INIT_LIST_HEAD(&xprt_info->pkt_list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002808 mutex_init(&xprt_info->rx_lock_lhb2);
2809 mutex_init(&xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002810 wake_lock_init(&xprt_info->wakelock,
2811 WAKE_LOCK_SUSPEND, xprt->name);
2812 xprt_info->need_len = 0;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002813 xprt_info->abort_data_read = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002814 INIT_WORK(&xprt_info->read_data, do_read_data);
2815 INIT_LIST_HEAD(&xprt_info->list);
2816
2817 xprt_info->workqueue = create_singlethread_workqueue(xprt->name);
2818 if (!xprt_info->workqueue) {
2819 kfree(xprt_info);
2820 return -ENOMEM;
2821 }
2822
2823 if (!strcmp(xprt->name, "msm_ipc_router_loopback_xprt")) {
2824 xprt_info->remote_node_id = IPC_ROUTER_NID_LOCAL;
2825 xprt_info->initialized = 1;
2826 }
2827
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002828 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002829 list_add_tail(&xprt_info->list, &xprt_info_list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002830 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002831
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002832 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002833 if (!routing_table_inited) {
2834 init_routing_table();
2835 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
2836 add_routing_table_entry(rt_entry);
2837 routing_table_inited = 1;
2838 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002839 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002840
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002841 xprt->priv = xprt_info;
2842
2843 return 0;
2844}
2845
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002846static void msm_ipc_router_remove_xprt(struct msm_ipc_router_xprt *xprt)
2847{
2848 struct msm_ipc_router_xprt_info *xprt_info;
2849
2850 if (xprt && xprt->priv) {
2851 xprt_info = xprt->priv;
2852
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002853 mutex_lock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002854 xprt_info->abort_data_read = 1;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002855 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002856
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002857 down_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002858 list_del(&xprt_info->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002859 up_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002860
2861 flush_workqueue(xprt_info->workqueue);
2862 destroy_workqueue(xprt_info->workqueue);
2863 wake_lock_destroy(&xprt_info->wakelock);
2864
2865 xprt->priv = 0;
2866 kfree(xprt_info);
2867 }
2868}
2869
2870
2871struct msm_ipc_router_xprt_work {
2872 struct msm_ipc_router_xprt *xprt;
2873 struct work_struct work;
2874};
2875
2876static void xprt_open_worker(struct work_struct *work)
2877{
2878 struct msm_ipc_router_xprt_work *xprt_work =
2879 container_of(work, struct msm_ipc_router_xprt_work, work);
2880
2881 msm_ipc_router_add_xprt(xprt_work->xprt);
2882 kfree(xprt_work);
2883}
2884
2885static void xprt_close_worker(struct work_struct *work)
2886{
2887 struct msm_ipc_router_xprt_work *xprt_work =
2888 container_of(work, struct msm_ipc_router_xprt_work, work);
2889
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002890 msm_ipc_cleanup_routing_table(xprt_work->xprt->priv);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002891 msm_ipc_router_remove_xprt(xprt_work->xprt);
Karthikeyan Ramasubramaniane6ef0a22013-06-12 11:49:26 -06002892 xprt_work->xprt->sft_close_done(xprt_work->xprt);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002893 kfree(xprt_work);
2894}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002895
2896void msm_ipc_router_xprt_notify(struct msm_ipc_router_xprt *xprt,
2897 unsigned event,
2898 void *data)
2899{
2900 struct msm_ipc_router_xprt_info *xprt_info = xprt->priv;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002901 struct msm_ipc_router_xprt_work *xprt_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002902 struct rr_packet *pkt;
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002903 unsigned long ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002904
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002905 if (!msm_ipc_router_workqueue) {
2906 ret = wait_for_completion_timeout(&msm_ipc_local_router_up,
2907 IPC_ROUTER_INIT_TIMEOUT);
2908 if (!ret || !msm_ipc_router_workqueue) {
2909 pr_err("%s: IPC Router not initialized\n", __func__);
2910 return;
2911 }
2912 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002913
2914 switch (event) {
2915 case IPC_ROUTER_XPRT_EVENT_OPEN:
2916 D("open event for '%s'\n", xprt->name);
2917 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
2918 GFP_ATOMIC);
Karthikeyan Ramasubramanian840bed12013-05-16 15:51:29 -06002919 if (xprt_work) {
2920 xprt_work->xprt = xprt;
2921 INIT_WORK(&xprt_work->work, xprt_open_worker);
2922 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
2923 } else {
2924 pr_err("%s: malloc failure - Couldn't notify OPEN event",
2925 __func__);
2926 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002927 break;
2928
2929 case IPC_ROUTER_XPRT_EVENT_CLOSE:
2930 D("close event for '%s'\n", xprt->name);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002931 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
2932 GFP_ATOMIC);
Karthikeyan Ramasubramanian840bed12013-05-16 15:51:29 -06002933 if (xprt_work) {
2934 xprt_work->xprt = xprt;
2935 INIT_WORK(&xprt_work->work, xprt_close_worker);
2936 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
2937 } else {
2938 pr_err("%s: malloc failure - Couldn't notify CLOSE event",
2939 __func__);
2940 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002941 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002942 }
2943
2944 if (!data)
2945 return;
2946
2947 while (!xprt_info) {
2948 msleep(100);
2949 xprt_info = xprt->priv;
2950 }
2951
2952 pkt = clone_pkt((struct rr_packet *)data);
2953 if (!pkt)
2954 return;
2955
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002956 mutex_lock(&xprt_info->rx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002957 list_add_tail(&pkt->list, &xprt_info->pkt_list);
2958 wake_lock(&xprt_info->wakelock);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002959 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06002960 queue_work(xprt_info->workqueue, &xprt_info->read_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002961}
2962
2963static int __init msm_ipc_router_init(void)
2964{
2965 int i, ret;
2966 struct msm_ipc_routing_table_entry *rt_entry;
2967
2968 msm_ipc_router_debug_mask |= SMEM_LOG;
Karthikeyan Ramasubramanian2bfe8ec2013-03-22 10:47:20 -06002969 ipc_rtr_log_ctxt = ipc_log_context_create(IPC_RTR_LOG_PAGES,
2970 "ipc_router");
2971 if (!ipc_rtr_log_ctxt)
2972 pr_err("%s: Unable to create IPC logging for IPC RTR",
2973 __func__);
2974
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002975 msm_ipc_router_workqueue =
2976 create_singlethread_workqueue("msm_ipc_router");
2977 if (!msm_ipc_router_workqueue)
2978 return -ENOMEM;
2979
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002980 debugfs_init();
2981
2982 for (i = 0; i < SRV_HASH_SIZE; i++)
2983 INIT_LIST_HEAD(&server_list[i]);
2984
2985 for (i = 0; i < LP_HASH_SIZE; i++)
2986 INIT_LIST_HEAD(&local_ports[i]);
2987
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002988 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002989 if (!routing_table_inited) {
2990 init_routing_table();
2991 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
2992 add_routing_table_entry(rt_entry);
2993 routing_table_inited = 1;
2994 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002995 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002996
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002997 ret = msm_ipc_router_init_sockets();
2998 if (ret < 0)
2999 pr_err("%s: Init sockets failed\n", __func__);
3000
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06003001 ret = msm_ipc_router_security_init();
3002 if (ret < 0)
3003 pr_err("%s: Security Init failed\n", __func__);
3004
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06003005 complete_all(&msm_ipc_local_router_up);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003006 return ret;
3007}
3008
3009module_init(msm_ipc_router_init);
3010MODULE_DESCRIPTION("MSM IPC Router");
3011MODULE_LICENSE("GPL v2");